2 /* $KAME: key.c,v 1.191 2001/06/27 10:46:49 sakane Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * This code is referd to RFC 2367
38 #include "opt_inet6.h"
39 #include "opt_ipsec.h"
41 #include <sys/types.h>
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
46 #include <sys/mutex.h>
48 #include <sys/domain.h>
49 #include <sys/protosw.h>
50 #include <sys/malloc.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
53 #include <sys/sysctl.h>
54 #include <sys/errno.h>
56 #include <sys/queue.h>
57 #include <sys/refcount.h>
58 #include <sys/syslog.h>
61 #include <net/route.h>
62 #include <net/raw_cb.h>
65 #include <netinet/in.h>
66 #include <netinet/in_systm.h>
67 #include <netinet/ip.h>
68 #include <netinet/in_var.h>
71 #include <netinet/ip6.h>
72 #include <netinet6/in6_var.h>
73 #include <netinet6/ip6_var.h>
76 #if defined(INET) || defined(INET6)
77 #include <netinet/in_pcb.h>
80 #include <netinet6/in6_pcb.h>
83 #include <net/pfkeyv2.h>
84 #include <netipsec/keydb.h>
85 #include <netipsec/key.h>
86 #include <netipsec/keysock.h>
87 #include <netipsec/key_debug.h>
89 #include <netipsec/ipsec.h>
91 #include <netipsec/ipsec6.h>
94 #include <netipsec/xform.h>
96 #include <machine/stdarg.h>
99 #include <sys/random.h>
101 #define FULLMASK 0xff
102 #define _BITS(bytes) ((bytes) << 3)
105 * Note on SA reference counting:
106 * - SAs that are not in DEAD state will have (total external reference + 1)
107 * following value in reference count field. they cannot be freed and are
108 * referenced from SA header.
109 * - SAs that are in DEAD state will have (total external reference)
110 * in reference count field. they are ready to be freed. reference from
111 * SA header will be removed in key_delsav(), when the reference count
112 * field hits 0 (= no external reference other than from SA header.
115 VNET_DEFINE(u_int32_t, key_debug_level) = 0;
116 static VNET_DEFINE(u_int, key_spi_trycnt) = 1000;
117 static VNET_DEFINE(u_int32_t, key_spi_minval) = 0x100;
118 static VNET_DEFINE(u_int32_t, key_spi_maxval) = 0x0fffffff; /* XXX */
119 static VNET_DEFINE(u_int32_t, policy_id) = 0;
120 /*interval to initialize randseed,1(m)*/
121 static VNET_DEFINE(u_int, key_int_random) = 60;
122 /* interval to expire acquiring, 30(s)*/
123 static VNET_DEFINE(u_int, key_larval_lifetime) = 30;
124 /* counter for blocking SADB_ACQUIRE.*/
125 static VNET_DEFINE(int, key_blockacq_count) = 10;
126 /* lifetime for blocking SADB_ACQUIRE.*/
127 static VNET_DEFINE(int, key_blockacq_lifetime) = 20;
128 /* preferred old sa rather than new sa.*/
129 static VNET_DEFINE(int, key_preferred_oldsa) = 1;
130 #define V_key_spi_trycnt VNET(key_spi_trycnt)
131 #define V_key_spi_minval VNET(key_spi_minval)
132 #define V_key_spi_maxval VNET(key_spi_maxval)
133 #define V_policy_id VNET(policy_id)
134 #define V_key_int_random VNET(key_int_random)
135 #define V_key_larval_lifetime VNET(key_larval_lifetime)
136 #define V_key_blockacq_count VNET(key_blockacq_count)
137 #define V_key_blockacq_lifetime VNET(key_blockacq_lifetime)
138 #define V_key_preferred_oldsa VNET(key_preferred_oldsa)
140 static VNET_DEFINE(u_int32_t, acq_seq) = 0;
141 #define V_acq_seq VNET(acq_seq)
144 static VNET_DEFINE(LIST_HEAD(_sptree, secpolicy), sptree[IPSEC_DIR_MAX]);
145 #define V_sptree VNET(sptree)
146 static struct mtx sptree_lock;
147 #define SPTREE_LOCK_INIT() \
148 mtx_init(&sptree_lock, "sptree", \
149 "fast ipsec security policy database", MTX_DEF)
150 #define SPTREE_LOCK_DESTROY() mtx_destroy(&sptree_lock)
151 #define SPTREE_LOCK() mtx_lock(&sptree_lock)
152 #define SPTREE_UNLOCK() mtx_unlock(&sptree_lock)
153 #define SPTREE_LOCK_ASSERT() mtx_assert(&sptree_lock, MA_OWNED)
155 static VNET_DEFINE(LIST_HEAD(_sahtree, secashead), sahtree); /* SAD */
156 #define V_sahtree VNET(sahtree)
157 static struct mtx sahtree_lock;
158 #define SAHTREE_LOCK_INIT() \
159 mtx_init(&sahtree_lock, "sahtree", \
160 "fast ipsec security association database", MTX_DEF)
161 #define SAHTREE_LOCK_DESTROY() mtx_destroy(&sahtree_lock)
162 #define SAHTREE_LOCK() mtx_lock(&sahtree_lock)
163 #define SAHTREE_UNLOCK() mtx_unlock(&sahtree_lock)
164 #define SAHTREE_LOCK_ASSERT() mtx_assert(&sahtree_lock, MA_OWNED)
167 static VNET_DEFINE(LIST_HEAD(_regtree, secreg), regtree[SADB_SATYPE_MAX + 1]);
168 #define V_regtree VNET(regtree)
169 static struct mtx regtree_lock;
170 #define REGTREE_LOCK_INIT() \
171 mtx_init(®tree_lock, "regtree", "fast ipsec regtree", MTX_DEF)
172 #define REGTREE_LOCK_DESTROY() mtx_destroy(®tree_lock)
173 #define REGTREE_LOCK() mtx_lock(®tree_lock)
174 #define REGTREE_UNLOCK() mtx_unlock(®tree_lock)
175 #define REGTREE_LOCK_ASSERT() mtx_assert(®tree_lock, MA_OWNED)
177 static VNET_DEFINE(LIST_HEAD(_acqtree, secacq), acqtree); /* acquiring list */
178 #define V_acqtree VNET(acqtree)
179 static struct mtx acq_lock;
180 #define ACQ_LOCK_INIT() \
181 mtx_init(&acq_lock, "acqtree", "fast ipsec acquire list", MTX_DEF)
182 #define ACQ_LOCK_DESTROY() mtx_destroy(&acq_lock)
183 #define ACQ_LOCK() mtx_lock(&acq_lock)
184 #define ACQ_UNLOCK() mtx_unlock(&acq_lock)
185 #define ACQ_LOCK_ASSERT() mtx_assert(&acq_lock, MA_OWNED)
187 /* SP acquiring list */
188 static VNET_DEFINE(LIST_HEAD(_spacqtree, secspacq), spacqtree);
189 #define V_spacqtree VNET(spacqtree)
190 static struct mtx spacq_lock;
191 #define SPACQ_LOCK_INIT() \
192 mtx_init(&spacq_lock, "spacqtree", \
193 "fast ipsec security policy acquire list", MTX_DEF)
194 #define SPACQ_LOCK_DESTROY() mtx_destroy(&spacq_lock)
195 #define SPACQ_LOCK() mtx_lock(&spacq_lock)
196 #define SPACQ_UNLOCK() mtx_unlock(&spacq_lock)
197 #define SPACQ_LOCK_ASSERT() mtx_assert(&spacq_lock, MA_OWNED)
199 /* search order for SAs */
200 static const u_int saorder_state_valid_prefer_old[] = {
201 SADB_SASTATE_DYING, SADB_SASTATE_MATURE,
203 static const u_int saorder_state_valid_prefer_new[] = {
204 SADB_SASTATE_MATURE, SADB_SASTATE_DYING,
206 static const u_int saorder_state_alive[] = {
208 SADB_SASTATE_MATURE, SADB_SASTATE_DYING, SADB_SASTATE_LARVAL
210 static const u_int saorder_state_any[] = {
211 SADB_SASTATE_MATURE, SADB_SASTATE_DYING,
212 SADB_SASTATE_LARVAL, SADB_SASTATE_DEAD
215 static const int minsize[] = {
216 sizeof(struct sadb_msg), /* SADB_EXT_RESERVED */
217 sizeof(struct sadb_sa), /* SADB_EXT_SA */
218 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_CURRENT */
219 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_HARD */
220 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_SOFT */
221 sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_SRC */
222 sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_DST */
223 sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_PROXY */
224 sizeof(struct sadb_key), /* SADB_EXT_KEY_AUTH */
225 sizeof(struct sadb_key), /* SADB_EXT_KEY_ENCRYPT */
226 sizeof(struct sadb_ident), /* SADB_EXT_IDENTITY_SRC */
227 sizeof(struct sadb_ident), /* SADB_EXT_IDENTITY_DST */
228 sizeof(struct sadb_sens), /* SADB_EXT_SENSITIVITY */
229 sizeof(struct sadb_prop), /* SADB_EXT_PROPOSAL */
230 sizeof(struct sadb_supported), /* SADB_EXT_SUPPORTED_AUTH */
231 sizeof(struct sadb_supported), /* SADB_EXT_SUPPORTED_ENCRYPT */
232 sizeof(struct sadb_spirange), /* SADB_EXT_SPIRANGE */
233 0, /* SADB_X_EXT_KMPRIVATE */
234 sizeof(struct sadb_x_policy), /* SADB_X_EXT_POLICY */
235 sizeof(struct sadb_x_sa2), /* SADB_X_SA2 */
236 sizeof(struct sadb_x_nat_t_type),/* SADB_X_EXT_NAT_T_TYPE */
237 sizeof(struct sadb_x_nat_t_port),/* SADB_X_EXT_NAT_T_SPORT */
238 sizeof(struct sadb_x_nat_t_port),/* SADB_X_EXT_NAT_T_DPORT */
239 sizeof(struct sadb_address), /* SADB_X_EXT_NAT_T_OAI */
240 sizeof(struct sadb_address), /* SADB_X_EXT_NAT_T_OAR */
241 sizeof(struct sadb_x_nat_t_frag),/* SADB_X_EXT_NAT_T_FRAG */
243 static const int maxsize[] = {
244 sizeof(struct sadb_msg), /* SADB_EXT_RESERVED */
245 sizeof(struct sadb_sa), /* SADB_EXT_SA */
246 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_CURRENT */
247 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_HARD */
248 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_SOFT */
249 0, /* SADB_EXT_ADDRESS_SRC */
250 0, /* SADB_EXT_ADDRESS_DST */
251 0, /* SADB_EXT_ADDRESS_PROXY */
252 0, /* SADB_EXT_KEY_AUTH */
253 0, /* SADB_EXT_KEY_ENCRYPT */
254 0, /* SADB_EXT_IDENTITY_SRC */
255 0, /* SADB_EXT_IDENTITY_DST */
256 0, /* SADB_EXT_SENSITIVITY */
257 0, /* SADB_EXT_PROPOSAL */
258 0, /* SADB_EXT_SUPPORTED_AUTH */
259 0, /* SADB_EXT_SUPPORTED_ENCRYPT */
260 sizeof(struct sadb_spirange), /* SADB_EXT_SPIRANGE */
261 0, /* SADB_X_EXT_KMPRIVATE */
262 0, /* SADB_X_EXT_POLICY */
263 sizeof(struct sadb_x_sa2), /* SADB_X_SA2 */
264 sizeof(struct sadb_x_nat_t_type),/* SADB_X_EXT_NAT_T_TYPE */
265 sizeof(struct sadb_x_nat_t_port),/* SADB_X_EXT_NAT_T_SPORT */
266 sizeof(struct sadb_x_nat_t_port),/* SADB_X_EXT_NAT_T_DPORT */
267 0, /* SADB_X_EXT_NAT_T_OAI */
268 0, /* SADB_X_EXT_NAT_T_OAR */
269 sizeof(struct sadb_x_nat_t_frag),/* SADB_X_EXT_NAT_T_FRAG */
272 static VNET_DEFINE(int, ipsec_esp_keymin) = 256;
273 static VNET_DEFINE(int, ipsec_esp_auth) = 0;
274 static VNET_DEFINE(int, ipsec_ah_keymin) = 128;
276 #define V_ipsec_esp_keymin VNET(ipsec_esp_keymin)
277 #define V_ipsec_esp_auth VNET(ipsec_esp_auth)
278 #define V_ipsec_ah_keymin VNET(ipsec_ah_keymin)
281 SYSCTL_DECL(_net_key);
284 SYSCTL_VNET_INT(_net_key, KEYCTL_DEBUG_LEVEL, debug,
285 CTLFLAG_RW, &VNET_NAME(key_debug_level), 0, "");
287 /* max count of trial for the decision of spi value */
288 SYSCTL_VNET_INT(_net_key, KEYCTL_SPI_TRY, spi_trycnt,
289 CTLFLAG_RW, &VNET_NAME(key_spi_trycnt), 0, "");
291 /* minimum spi value to allocate automatically. */
292 SYSCTL_VNET_INT(_net_key, KEYCTL_SPI_MIN_VALUE,
293 spi_minval, CTLFLAG_RW, &VNET_NAME(key_spi_minval), 0, "");
295 /* maximun spi value to allocate automatically. */
296 SYSCTL_VNET_INT(_net_key, KEYCTL_SPI_MAX_VALUE,
297 spi_maxval, CTLFLAG_RW, &VNET_NAME(key_spi_maxval), 0, "");
299 /* interval to initialize randseed */
300 SYSCTL_VNET_INT(_net_key, KEYCTL_RANDOM_INT,
301 int_random, CTLFLAG_RW, &VNET_NAME(key_int_random), 0, "");
303 /* lifetime for larval SA */
304 SYSCTL_VNET_INT(_net_key, KEYCTL_LARVAL_LIFETIME,
305 larval_lifetime, CTLFLAG_RW, &VNET_NAME(key_larval_lifetime), 0, "");
307 /* counter for blocking to send SADB_ACQUIRE to IKEd */
308 SYSCTL_VNET_INT(_net_key, KEYCTL_BLOCKACQ_COUNT,
309 blockacq_count, CTLFLAG_RW, &VNET_NAME(key_blockacq_count), 0, "");
311 /* lifetime for blocking to send SADB_ACQUIRE to IKEd */
312 SYSCTL_VNET_INT(_net_key, KEYCTL_BLOCKACQ_LIFETIME,
313 blockacq_lifetime, CTLFLAG_RW, &VNET_NAME(key_blockacq_lifetime), 0, "");
316 SYSCTL_VNET_INT(_net_key, KEYCTL_ESP_AUTH, esp_auth,
317 CTLFLAG_RW, &VNET_NAME(ipsec_esp_auth), 0, "");
319 /* minimum ESP key length */
320 SYSCTL_VNET_INT(_net_key, KEYCTL_ESP_KEYMIN,
321 esp_keymin, CTLFLAG_RW, &VNET_NAME(ipsec_esp_keymin), 0, "");
323 /* minimum AH key length */
324 SYSCTL_VNET_INT(_net_key, KEYCTL_AH_KEYMIN, ah_keymin,
325 CTLFLAG_RW, &VNET_NAME(ipsec_ah_keymin), 0, "");
327 /* perfered old SA rather than new SA */
328 SYSCTL_VNET_INT(_net_key, KEYCTL_PREFERED_OLDSA,
329 preferred_oldsa, CTLFLAG_RW, &VNET_NAME(key_preferred_oldsa), 0, "");
331 #define __LIST_CHAINED(elm) \
332 (!((elm)->chain.le_next == NULL && (elm)->chain.le_prev == NULL))
333 #define LIST_INSERT_TAIL(head, elm, type, field) \
335 struct type *curelm = LIST_FIRST(head); \
336 if (curelm == NULL) {\
337 LIST_INSERT_HEAD(head, elm, field); \
339 while (LIST_NEXT(curelm, field)) \
340 curelm = LIST_NEXT(curelm, field);\
341 LIST_INSERT_AFTER(curelm, elm, field);\
345 #define KEY_CHKSASTATE(head, sav, name) \
347 if ((head) != (sav)) { \
348 ipseclog((LOG_DEBUG, "%s: state mismatched (TREE=%d SA=%d)\n", \
349 (name), (head), (sav))); \
354 #define KEY_CHKSPDIR(head, sp, name) \
356 if ((head) != (sp)) { \
357 ipseclog((LOG_DEBUG, "%s: direction mismatched (TREE=%d SP=%d), " \
358 "anyway continue.\n", \
359 (name), (head), (sp))); \
363 MALLOC_DEFINE(M_IPSEC_SA, "secasvar", "ipsec security association");
364 MALLOC_DEFINE(M_IPSEC_SAH, "sahead", "ipsec sa head");
365 MALLOC_DEFINE(M_IPSEC_SP, "ipsecpolicy", "ipsec security policy");
366 MALLOC_DEFINE(M_IPSEC_SR, "ipsecrequest", "ipsec security request");
367 MALLOC_DEFINE(M_IPSEC_MISC, "ipsec-misc", "ipsec miscellaneous");
368 MALLOC_DEFINE(M_IPSEC_SAQ, "ipsec-saq", "ipsec sa acquire");
369 MALLOC_DEFINE(M_IPSEC_SAR, "ipsec-reg", "ipsec sa acquire");
372 * set parameters into secpolicyindex buffer.
373 * Must allocate secpolicyindex buffer passed to this function.
375 #define KEY_SETSECSPIDX(_dir, s, d, ps, pd, ulp, idx) \
377 bzero((idx), sizeof(struct secpolicyindex)); \
378 (idx)->dir = (_dir); \
379 (idx)->prefs = (ps); \
380 (idx)->prefd = (pd); \
381 (idx)->ul_proto = (ulp); \
382 bcopy((s), &(idx)->src, ((const struct sockaddr *)(s))->sa_len); \
383 bcopy((d), &(idx)->dst, ((const struct sockaddr *)(d))->sa_len); \
387 * set parameters into secasindex buffer.
388 * Must allocate secasindex buffer before calling this function.
390 #define KEY_SETSECASIDX(p, m, r, s, d, idx) \
392 bzero((idx), sizeof(struct secasindex)); \
393 (idx)->proto = (p); \
395 (idx)->reqid = (r); \
396 bcopy((s), &(idx)->src, ((const struct sockaddr *)(s))->sa_len); \
397 bcopy((d), &(idx)->dst, ((const struct sockaddr *)(d))->sa_len); \
402 u_long getspi_count; /* the avarage of count to try to get new SPI */
406 struct sadb_msg *msg;
407 struct sadb_ext *ext[SADB_EXT_MAX + 1];
408 int extoff[SADB_EXT_MAX + 1];
409 int extlen[SADB_EXT_MAX + 1];
412 static struct secasvar *key_allocsa_policy __P((const struct secasindex *));
413 static void key_freesp_so __P((struct secpolicy **));
414 static struct secasvar *key_do_allocsa_policy __P((struct secashead *, u_int));
415 static void key_delsp __P((struct secpolicy *));
416 static struct secpolicy *key_getsp __P((struct secpolicyindex *));
417 static void _key_delsp(struct secpolicy *sp);
418 static struct secpolicy *key_getspbyid __P((u_int32_t));
419 static u_int32_t key_newreqid __P((void));
420 static struct mbuf *key_gather_mbuf __P((struct mbuf *,
421 const struct sadb_msghdr *, int, int, ...));
422 static int key_spdadd __P((struct socket *, struct mbuf *,
423 const struct sadb_msghdr *));
424 static u_int32_t key_getnewspid __P((void));
425 static int key_spddelete __P((struct socket *, struct mbuf *,
426 const struct sadb_msghdr *));
427 static int key_spddelete2 __P((struct socket *, struct mbuf *,
428 const struct sadb_msghdr *));
429 static int key_spdget __P((struct socket *, struct mbuf *,
430 const struct sadb_msghdr *));
431 static int key_spdflush __P((struct socket *, struct mbuf *,
432 const struct sadb_msghdr *));
433 static int key_spddump __P((struct socket *, struct mbuf *,
434 const struct sadb_msghdr *));
435 static struct mbuf *key_setdumpsp __P((struct secpolicy *,
436 u_int8_t, u_int32_t, u_int32_t));
437 static u_int key_getspreqmsglen __P((struct secpolicy *));
438 static int key_spdexpire __P((struct secpolicy *));
439 static struct secashead *key_newsah __P((struct secasindex *));
440 static void key_delsah __P((struct secashead *));
441 static struct secasvar *key_newsav __P((struct mbuf *,
442 const struct sadb_msghdr *, struct secashead *, int *,
444 #define KEY_NEWSAV(m, sadb, sah, e) \
445 key_newsav(m, sadb, sah, e, __FILE__, __LINE__)
446 static void key_delsav __P((struct secasvar *));
447 static struct secashead *key_getsah __P((struct secasindex *));
448 static struct secasvar *key_checkspidup __P((struct secasindex *, u_int32_t));
449 static struct secasvar *key_getsavbyspi __P((struct secashead *, u_int32_t));
450 static int key_setsaval __P((struct secasvar *, struct mbuf *,
451 const struct sadb_msghdr *));
452 static int key_mature __P((struct secasvar *));
453 static struct mbuf *key_setdumpsa __P((struct secasvar *, u_int8_t,
454 u_int8_t, u_int32_t, u_int32_t));
455 static struct mbuf *key_setsadbmsg __P((u_int8_t, u_int16_t, u_int8_t,
456 u_int32_t, pid_t, u_int16_t));
457 static struct mbuf *key_setsadbsa __P((struct secasvar *));
458 static struct mbuf *key_setsadbaddr __P((u_int16_t,
459 const struct sockaddr *, u_int8_t, u_int16_t));
461 static struct mbuf *key_setsadbxport(u_int16_t, u_int16_t);
462 static struct mbuf *key_setsadbxtype(u_int16_t);
464 static void key_porttosaddr(struct sockaddr *, u_int16_t);
465 #define KEY_PORTTOSADDR(saddr, port) \
466 key_porttosaddr((struct sockaddr *)(saddr), (port))
467 static struct mbuf *key_setsadbxsa2 __P((u_int8_t, u_int32_t, u_int32_t));
468 static struct mbuf *key_setsadbxpolicy __P((u_int16_t, u_int8_t,
470 static struct seckey *key_dup_keymsg(const struct sadb_key *, u_int,
471 struct malloc_type *);
472 static struct seclifetime *key_dup_lifemsg(const struct sadb_lifetime *src,
473 struct malloc_type *type);
475 static int key_ismyaddr6 __P((struct sockaddr_in6 *));
478 /* flags for key_cmpsaidx() */
479 #define CMP_HEAD 1 /* protocol, addresses. */
480 #define CMP_MODE_REQID 2 /* additionally HEAD, reqid, mode. */
481 #define CMP_REQID 3 /* additionally HEAD, reaid. */
482 #define CMP_EXACTLY 4 /* all elements. */
483 static int key_cmpsaidx
484 __P((const struct secasindex *, const struct secasindex *, int));
486 static int key_cmpspidx_exactly
487 __P((struct secpolicyindex *, struct secpolicyindex *));
488 static int key_cmpspidx_withmask
489 __P((struct secpolicyindex *, struct secpolicyindex *));
490 static int key_sockaddrcmp __P((const struct sockaddr *, const struct sockaddr *, int));
491 static int key_bbcmp __P((const void *, const void *, u_int));
492 static u_int16_t key_satype2proto __P((u_int8_t));
493 static u_int8_t key_proto2satype __P((u_int16_t));
495 static int key_getspi __P((struct socket *, struct mbuf *,
496 const struct sadb_msghdr *));
497 static u_int32_t key_do_getnewspi __P((struct sadb_spirange *,
498 struct secasindex *));
499 static int key_update __P((struct socket *, struct mbuf *,
500 const struct sadb_msghdr *));
501 #ifdef IPSEC_DOSEQCHECK
502 static struct secasvar *key_getsavbyseq __P((struct secashead *, u_int32_t));
504 static int key_add __P((struct socket *, struct mbuf *,
505 const struct sadb_msghdr *));
506 static int key_setident __P((struct secashead *, struct mbuf *,
507 const struct sadb_msghdr *));
508 static struct mbuf *key_getmsgbuf_x1 __P((struct mbuf *,
509 const struct sadb_msghdr *));
510 static int key_delete __P((struct socket *, struct mbuf *,
511 const struct sadb_msghdr *));
512 static int key_get __P((struct socket *, struct mbuf *,
513 const struct sadb_msghdr *));
515 static void key_getcomb_setlifetime __P((struct sadb_comb *));
516 static struct mbuf *key_getcomb_esp __P((void));
517 static struct mbuf *key_getcomb_ah __P((void));
518 static struct mbuf *key_getcomb_ipcomp __P((void));
519 static struct mbuf *key_getprop __P((const struct secasindex *));
521 static int key_acquire __P((const struct secasindex *, struct secpolicy *));
522 static struct secacq *key_newacq __P((const struct secasindex *));
523 static struct secacq *key_getacq __P((const struct secasindex *));
524 static struct secacq *key_getacqbyseq __P((u_int32_t));
525 static struct secspacq *key_newspacq __P((struct secpolicyindex *));
526 static struct secspacq *key_getspacq __P((struct secpolicyindex *));
527 static int key_acquire2 __P((struct socket *, struct mbuf *,
528 const struct sadb_msghdr *));
529 static int key_register __P((struct socket *, struct mbuf *,
530 const struct sadb_msghdr *));
531 static int key_expire __P((struct secasvar *));
532 static int key_flush __P((struct socket *, struct mbuf *,
533 const struct sadb_msghdr *));
534 static int key_dump __P((struct socket *, struct mbuf *,
535 const struct sadb_msghdr *));
536 static int key_promisc __P((struct socket *, struct mbuf *,
537 const struct sadb_msghdr *));
538 static int key_senderror __P((struct socket *, struct mbuf *, int));
539 static int key_validate_ext __P((const struct sadb_ext *, int));
540 static int key_align __P((struct mbuf *, struct sadb_msghdr *));
541 static struct mbuf *key_setlifetime(struct seclifetime *src,
543 static struct mbuf *key_setkey(struct seckey *src, u_int16_t exttype);
546 static const char *key_getfqdn __P((void));
547 static const char *key_getuserfqdn __P((void));
549 static void key_sa_chgstate __P((struct secasvar *, u_int8_t));
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)
811 struct secasvar *sav;
813 IPSEC_ASSERT(isr != NULL, ("null isr"));
814 IPSEC_ASSERT(saidx != NULL, ("null saidx"));
815 IPSEC_ASSERT(saidx->mode == IPSEC_MODE_TRANSPORT ||
816 saidx->mode == IPSEC_MODE_TUNNEL,
817 ("unexpected policy %u", saidx->mode));
820 * XXX guard against protocol callbacks from the crypto
821 * thread as they reference ipsecrequest.sav which we
822 * temporarily null out below. Need to rethink how we
823 * handle bundled SA's in the callback thread.
825 IPSECREQUEST_LOCK_ASSERT(isr);
827 /* get current level */
828 level = ipsec_get_reqlevel(isr);
831 * We check new SA in the IPsec request because a different
832 * SA may be involved each time this request is checked, either
833 * because new SAs are being configured, or this request is
834 * associated with an unconnected datagram socket, or this request
835 * is associated with a system default policy.
837 * key_allocsa_policy should allocate the oldest SA available.
838 * See key_do_allocsa_policy(), and draft-jenkins-ipsec-rekeying-03.txt.
840 sav = key_allocsa_policy(saidx);
841 if (sav != isr->sav) {
842 /* SA need to be updated. */
843 if (!IPSECREQUEST_UPGRADE(isr)) {
844 /* Kick everyone off. */
845 IPSECREQUEST_UNLOCK(isr);
846 IPSECREQUEST_WLOCK(isr);
848 if (isr->sav != NULL)
849 KEY_FREESAV(&isr->sav);
851 IPSECREQUEST_DOWNGRADE(isr);
852 } else if (sav != NULL)
855 /* When there is SA. */
856 if (isr->sav != NULL) {
857 if (isr->sav->state != SADB_SASTATE_MATURE &&
858 isr->sav->state != SADB_SASTATE_DYING)
864 error = key_acquire(saidx, isr->sp);
866 /* XXX What should I do ? */
867 ipseclog((LOG_DEBUG, "%s: error %d returned from key_acquire\n",
872 if (level != IPSEC_LEVEL_REQUIRE) {
873 /* XXX sigh, the interface to this routine is botched */
874 IPSEC_ASSERT(isr->sav == NULL, ("unexpected SA"));
882 * allocating a SA for policy entry from SAD.
883 * NOTE: searching SAD of aliving state.
884 * OUT: NULL: not found.
885 * others: found and return the pointer.
887 static struct secasvar *
888 key_allocsa_policy(const struct secasindex *saidx)
890 #define N(a) _ARRAYLEN(a)
891 struct secashead *sah;
892 struct secasvar *sav;
893 u_int stateidx, arraysize;
894 const u_int *state_valid;
896 state_valid = NULL; /* silence gcc */
897 arraysize = 0; /* silence gcc */
900 LIST_FOREACH(sah, &V_sahtree, chain) {
901 if (sah->state == SADB_SASTATE_DEAD)
903 if (key_cmpsaidx(&sah->saidx, saidx, CMP_MODE_REQID)) {
904 if (V_key_preferred_oldsa) {
905 state_valid = saorder_state_valid_prefer_old;
906 arraysize = N(saorder_state_valid_prefer_old);
908 state_valid = saorder_state_valid_prefer_new;
909 arraysize = N(saorder_state_valid_prefer_new);
918 /* search valid state */
919 for (stateidx = 0; stateidx < arraysize; stateidx++) {
920 sav = key_do_allocsa_policy(sah, state_valid[stateidx]);
930 * searching SAD with direction, protocol, mode and state.
931 * called by key_allocsa_policy().
934 * others : found, pointer to a SA.
936 static struct secasvar *
937 key_do_allocsa_policy(struct secashead *sah, u_int state)
939 struct secasvar *sav, *nextsav, *candidate, *d;
945 for (sav = LIST_FIRST(&sah->savtree[state]);
949 nextsav = LIST_NEXT(sav, chain);
952 KEY_CHKSASTATE(sav->state, state, __func__);
955 if (candidate == NULL) {
960 /* Which SA is the better ? */
962 IPSEC_ASSERT(candidate->lft_c != NULL,
963 ("null candidate lifetime"));
964 IPSEC_ASSERT(sav->lft_c != NULL, ("null sav lifetime"));
966 /* What the best method is to compare ? */
967 if (V_key_preferred_oldsa) {
968 if (candidate->lft_c->addtime >
969 sav->lft_c->addtime) {
976 /* preferred new sa rather than old sa */
977 if (candidate->lft_c->addtime <
978 sav->lft_c->addtime) {
985 * prepared to delete the SA when there is more
986 * suitable candidate and the lifetime of the SA is not
989 if (d->lft_h->addtime != 0) {
990 struct mbuf *m, *result;
993 key_sa_chgstate(d, SADB_SASTATE_DEAD);
995 IPSEC_ASSERT(d->refcnt > 0, ("bogus ref count"));
997 satype = key_proto2satype(d->sah->saidx.proto);
1001 m = key_setsadbmsg(SADB_DELETE, 0,
1002 satype, 0, 0, d->refcnt - 1);
1007 /* set sadb_address for saidx's. */
1008 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
1009 &d->sah->saidx.src.sa,
1010 d->sah->saidx.src.sa.sa_len << 3,
1016 /* set sadb_address for saidx's. */
1017 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
1018 &d->sah->saidx.dst.sa,
1019 d->sah->saidx.dst.sa.sa_len << 3,
1025 /* create SA extension */
1026 m = key_setsadbsa(d);
1031 if (result->m_len < sizeof(struct sadb_msg)) {
1032 result = m_pullup(result,
1033 sizeof(struct sadb_msg));
1038 result->m_pkthdr.len = 0;
1039 for (m = result; m; m = m->m_next)
1040 result->m_pkthdr.len += m->m_len;
1041 mtod(result, struct sadb_msg *)->sadb_msg_len =
1042 PFKEY_UNIT64(result->m_pkthdr.len);
1044 if (key_sendup_mbuf(NULL, result,
1045 KEY_SENDUP_REGISTERED))
1052 sa_addref(candidate);
1053 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1054 printf("DP %s cause refcnt++:%d SA:%p\n",
1055 __func__, candidate->refcnt, candidate));
1063 * allocating a usable SA entry for a *INBOUND* packet.
1064 * Must call key_freesav() later.
1065 * OUT: positive: pointer to a usable sav (i.e. MATURE or DYING state).
1066 * NULL: not found, or error occured.
1068 * In the comparison, no source address is used--for RFC2401 conformance.
1069 * To quote, from section 4.1:
1070 * A security association is uniquely identified by a triple consisting
1071 * of a Security Parameter Index (SPI), an IP Destination Address, and a
1072 * security protocol (AH or ESP) identifier.
1073 * Note that, however, we do need to keep source address in IPsec SA.
1074 * IKE specification and PF_KEY specification do assume that we
1075 * keep source address in IPsec SA. We see a tricky situation here.
1079 union sockaddr_union *dst,
1082 const char* where, int tag)
1084 struct secashead *sah;
1085 struct secasvar *sav;
1086 u_int stateidx, arraysize, state;
1087 const u_int *saorder_state_valid;
1092 IPSEC_ASSERT(dst != NULL, ("null dst address"));
1094 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1095 printf("DP %s from %s:%u\n", __func__, where, tag));
1098 natt_chkport = (dst->sa.sa_family == AF_INET &&
1099 dst->sa.sa_len == sizeof(struct sockaddr_in) &&
1100 dst->sin.sin_port != 0);
1105 * XXX: to be checked internal IP header somewhere. Also when
1106 * IPsec tunnel packet is received. But ESP tunnel mode is
1107 * encrypted so we can't check internal IP header.
1110 if (V_key_preferred_oldsa) {
1111 saorder_state_valid = saorder_state_valid_prefer_old;
1112 arraysize = _ARRAYLEN(saorder_state_valid_prefer_old);
1114 saorder_state_valid = saorder_state_valid_prefer_new;
1115 arraysize = _ARRAYLEN(saorder_state_valid_prefer_new);
1117 LIST_FOREACH(sah, &V_sahtree, chain) {
1120 /* search valid state */
1121 for (stateidx = 0; stateidx < arraysize; stateidx++) {
1122 state = saorder_state_valid[stateidx];
1123 LIST_FOREACH(sav, &sah->savtree[state], chain) {
1125 KEY_CHKSASTATE(sav->state, state, __func__);
1126 /* do not return entries w/ unusable state */
1127 if (sav->state != SADB_SASTATE_MATURE &&
1128 sav->state != SADB_SASTATE_DYING)
1130 if (proto != sav->sah->saidx.proto)
1132 if (spi != sav->spi)
1137 * Really only check ports when this is a NAT-T
1138 * SA. Otherwise other lookups providing ports
1141 if (sav->natt_type && natt_chkport)
1144 #if 0 /* don't check src */
1145 /* check src address */
1146 if (key_sockaddrcmp(&src->sa,
1147 &sav->sah->saidx.src.sa, checkport) != 0)
1150 /* check dst address */
1151 if (key_sockaddrcmp(&dst->sa,
1152 &sav->sah->saidx.dst.sa, checkport) != 0)
1163 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1164 printf("DP %s return SA:%p; refcnt %u\n", __func__,
1165 sav, sav ? sav->refcnt : 0));
1170 * Must be called after calling key_allocsp().
1171 * For both the packet without socket and key_freeso().
1174 _key_freesp(struct secpolicy **spp, const char* where, int tag)
1176 struct secpolicy *sp = *spp;
1178 IPSEC_ASSERT(sp != NULL, ("null sp"));
1183 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1184 printf("DP %s SP:%p (ID=%u) from %s:%u; refcnt now %u\n",
1185 __func__, sp, sp->id, where, tag, sp->refcnt));
1187 if (sp->refcnt == 0) {
1195 * Must be called after calling key_allocsp().
1196 * For the packet with socket.
1199 key_freeso(struct socket *so)
1201 IPSEC_ASSERT(so != NULL, ("null so"));
1203 switch (so->so_proto->pr_domain->dom_family) {
1204 #if defined(INET) || defined(INET6)
1212 struct inpcb *pcb = sotoinpcb(so);
1214 /* Does it have a PCB ? */
1217 key_freesp_so(&pcb->inp_sp->sp_in);
1218 key_freesp_so(&pcb->inp_sp->sp_out);
1221 #endif /* INET || INET6 */
1223 ipseclog((LOG_DEBUG, "%s: unknown address family=%d.\n",
1224 __func__, so->so_proto->pr_domain->dom_family));
1230 key_freesp_so(struct secpolicy **sp)
1232 IPSEC_ASSERT(sp != NULL && *sp != NULL, ("null sp"));
1234 if ((*sp)->policy == IPSEC_POLICY_ENTRUST ||
1235 (*sp)->policy == IPSEC_POLICY_BYPASS)
1238 IPSEC_ASSERT((*sp)->policy == IPSEC_POLICY_IPSEC,
1239 ("invalid policy %u", (*sp)->policy));
1244 key_addrefsa(struct secasvar *sav, const char* where, int tag)
1247 IPSEC_ASSERT(sav != NULL, ("null sav"));
1248 IPSEC_ASSERT(sav->refcnt > 0, ("refcount must exist"));
1254 * Must be called after calling key_allocsa().
1255 * This function is called by key_freesp() to free some SA allocated
1259 key_freesav(struct secasvar **psav, const char* where, int tag)
1261 struct secasvar *sav = *psav;
1263 IPSEC_ASSERT(sav != NULL, ("null sav"));
1265 if (sa_delref(sav)) {
1266 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1267 printf("DP %s SA:%p (SPI %u) from %s:%u; refcnt now %u\n",
1268 __func__, sav, ntohl(sav->spi), where, tag, sav->refcnt));
1272 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1273 printf("DP %s SA:%p (SPI %u) from %s:%u; refcnt now %u\n",
1274 __func__, sav, ntohl(sav->spi), where, tag, sav->refcnt));
1278 /* %%% SPD management */
1280 * free security policy entry.
1283 key_delsp(struct secpolicy *sp)
1285 struct ipsecrequest *isr, *nextisr;
1287 IPSEC_ASSERT(sp != NULL, ("null sp"));
1288 SPTREE_LOCK_ASSERT();
1290 sp->state = IPSEC_SPSTATE_DEAD;
1292 IPSEC_ASSERT(sp->refcnt == 0,
1293 ("SP with references deleted (refcnt %u)", sp->refcnt));
1295 /* remove from SP index */
1296 if (__LIST_CHAINED(sp))
1297 LIST_REMOVE(sp, chain);
1299 for (isr = sp->req; isr != NULL; isr = nextisr) {
1300 if (isr->sav != NULL) {
1301 KEY_FREESAV(&isr->sav);
1305 nextisr = isr->next;
1313 * OUT: NULL : not found
1314 * others : found, pointer to a SP.
1316 static struct secpolicy *
1317 key_getsp(struct secpolicyindex *spidx)
1319 struct secpolicy *sp;
1321 IPSEC_ASSERT(spidx != NULL, ("null spidx"));
1324 LIST_FOREACH(sp, &V_sptree[spidx->dir], chain) {
1325 if (sp->state == IPSEC_SPSTATE_DEAD)
1327 if (key_cmpspidx_exactly(spidx, &sp->spidx)) {
1339 * OUT: NULL : not found
1340 * others : found, pointer to a SP.
1342 static struct secpolicy *
1343 key_getspbyid(u_int32_t id)
1345 struct secpolicy *sp;
1348 LIST_FOREACH(sp, &V_sptree[IPSEC_DIR_INBOUND], chain) {
1349 if (sp->state == IPSEC_SPSTATE_DEAD)
1357 LIST_FOREACH(sp, &V_sptree[IPSEC_DIR_OUTBOUND], chain) {
1358 if (sp->state == IPSEC_SPSTATE_DEAD)
1372 key_newsp(const char* where, int tag)
1374 struct secpolicy *newsp = NULL;
1376 newsp = (struct secpolicy *)
1377 malloc(sizeof(struct secpolicy), M_IPSEC_SP, M_NOWAIT|M_ZERO);
1379 SECPOLICY_LOCK_INIT(newsp);
1384 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1385 printf("DP %s from %s:%u return SP:%p\n", __func__,
1386 where, tag, newsp));
1391 _key_delsp(struct secpolicy *sp)
1393 SECPOLICY_LOCK_DESTROY(sp);
1394 free(sp, M_IPSEC_SP);
1398 * create secpolicy structure from sadb_x_policy structure.
1399 * NOTE: `state', `secpolicyindex' in secpolicy structure are not set,
1400 * so must be set properly later.
1403 key_msg2sp(xpl0, len, error)
1404 struct sadb_x_policy *xpl0;
1408 struct secpolicy *newsp;
1410 IPSEC_ASSERT(xpl0 != NULL, ("null xpl0"));
1411 IPSEC_ASSERT(len >= sizeof(*xpl0), ("policy too short: %zu", len));
1413 if (len != PFKEY_EXTLEN(xpl0)) {
1414 ipseclog((LOG_DEBUG, "%s: Invalid msg length.\n", __func__));
1419 if ((newsp = KEY_NEWSP()) == NULL) {
1424 newsp->spidx.dir = xpl0->sadb_x_policy_dir;
1425 newsp->policy = xpl0->sadb_x_policy_type;
1428 switch (xpl0->sadb_x_policy_type) {
1429 case IPSEC_POLICY_DISCARD:
1430 case IPSEC_POLICY_NONE:
1431 case IPSEC_POLICY_ENTRUST:
1432 case IPSEC_POLICY_BYPASS:
1436 case IPSEC_POLICY_IPSEC:
1439 struct sadb_x_ipsecrequest *xisr;
1440 struct ipsecrequest **p_isr = &newsp->req;
1442 /* validity check */
1443 if (PFKEY_EXTLEN(xpl0) < sizeof(*xpl0)) {
1444 ipseclog((LOG_DEBUG, "%s: Invalid msg length.\n",
1451 tlen = PFKEY_EXTLEN(xpl0) - sizeof(*xpl0);
1452 xisr = (struct sadb_x_ipsecrequest *)(xpl0 + 1);
1456 if (xisr->sadb_x_ipsecrequest_len < sizeof(*xisr)) {
1457 ipseclog((LOG_DEBUG, "%s: invalid ipsecrequest "
1458 "length.\n", __func__));
1464 /* allocate request buffer */
1465 /* NB: data structure is zero'd */
1466 *p_isr = ipsec_newisr();
1467 if ((*p_isr) == NULL) {
1468 ipseclog((LOG_DEBUG,
1469 "%s: No more memory.\n", __func__));
1476 switch (xisr->sadb_x_ipsecrequest_proto) {
1479 case IPPROTO_IPCOMP:
1482 ipseclog((LOG_DEBUG,
1483 "%s: invalid proto type=%u\n", __func__,
1484 xisr->sadb_x_ipsecrequest_proto));
1486 *error = EPROTONOSUPPORT;
1489 (*p_isr)->saidx.proto = xisr->sadb_x_ipsecrequest_proto;
1491 switch (xisr->sadb_x_ipsecrequest_mode) {
1492 case IPSEC_MODE_TRANSPORT:
1493 case IPSEC_MODE_TUNNEL:
1495 case IPSEC_MODE_ANY:
1497 ipseclog((LOG_DEBUG,
1498 "%s: invalid mode=%u\n", __func__,
1499 xisr->sadb_x_ipsecrequest_mode));
1504 (*p_isr)->saidx.mode = xisr->sadb_x_ipsecrequest_mode;
1506 switch (xisr->sadb_x_ipsecrequest_level) {
1507 case IPSEC_LEVEL_DEFAULT:
1508 case IPSEC_LEVEL_USE:
1509 case IPSEC_LEVEL_REQUIRE:
1511 case IPSEC_LEVEL_UNIQUE:
1512 /* validity check */
1514 * If range violation of reqid, kernel will
1515 * update it, don't refuse it.
1517 if (xisr->sadb_x_ipsecrequest_reqid
1518 > IPSEC_MANUAL_REQID_MAX) {
1519 ipseclog((LOG_DEBUG,
1520 "%s: reqid=%d range "
1521 "violation, updated by kernel.\n",
1523 xisr->sadb_x_ipsecrequest_reqid));
1524 xisr->sadb_x_ipsecrequest_reqid = 0;
1527 /* allocate new reqid id if reqid is zero. */
1528 if (xisr->sadb_x_ipsecrequest_reqid == 0) {
1530 if ((reqid = key_newreqid()) == 0) {
1535 (*p_isr)->saidx.reqid = reqid;
1536 xisr->sadb_x_ipsecrequest_reqid = reqid;
1538 /* set it for manual keying. */
1539 (*p_isr)->saidx.reqid =
1540 xisr->sadb_x_ipsecrequest_reqid;
1545 ipseclog((LOG_DEBUG, "%s: invalid level=%u\n",
1547 xisr->sadb_x_ipsecrequest_level));
1552 (*p_isr)->level = xisr->sadb_x_ipsecrequest_level;
1554 /* set IP addresses if there */
1555 if (xisr->sadb_x_ipsecrequest_len > sizeof(*xisr)) {
1556 struct sockaddr *paddr;
1558 paddr = (struct sockaddr *)(xisr + 1);
1560 /* validity check */
1562 > sizeof((*p_isr)->saidx.src)) {
1563 ipseclog((LOG_DEBUG, "%s: invalid "
1564 "request address length.\n",
1570 bcopy(paddr, &(*p_isr)->saidx.src,
1573 paddr = (struct sockaddr *)((caddr_t)paddr
1576 /* validity check */
1578 > sizeof((*p_isr)->saidx.dst)) {
1579 ipseclog((LOG_DEBUG, "%s: invalid "
1580 "request address length.\n",
1586 bcopy(paddr, &(*p_isr)->saidx.dst,
1590 (*p_isr)->sp = newsp;
1592 /* initialization for the next. */
1593 p_isr = &(*p_isr)->next;
1594 tlen -= xisr->sadb_x_ipsecrequest_len;
1596 /* validity check */
1598 ipseclog((LOG_DEBUG, "%s: becoming tlen < 0.\n",
1605 xisr = (struct sadb_x_ipsecrequest *)((caddr_t)xisr
1606 + xisr->sadb_x_ipsecrequest_len);
1611 ipseclog((LOG_DEBUG, "%s: invalid policy type.\n", __func__));
1624 static u_int32_t auto_reqid = IPSEC_MANUAL_REQID_MAX + 1;
1626 auto_reqid = (auto_reqid == ~0
1627 ? IPSEC_MANUAL_REQID_MAX + 1 : auto_reqid + 1);
1629 /* XXX should be unique check */
1635 * copy secpolicy struct to sadb_x_policy structure indicated.
1639 struct secpolicy *sp;
1641 struct sadb_x_policy *xpl;
1646 IPSEC_ASSERT(sp != NULL, ("null policy"));
1648 tlen = key_getspreqmsglen(sp);
1650 m = m_get2(tlen, M_NOWAIT, MT_DATA, 0);
1655 xpl = mtod(m, struct sadb_x_policy *);
1658 xpl->sadb_x_policy_len = PFKEY_UNIT64(tlen);
1659 xpl->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
1660 xpl->sadb_x_policy_type = sp->policy;
1661 xpl->sadb_x_policy_dir = sp->spidx.dir;
1662 xpl->sadb_x_policy_id = sp->id;
1663 p = (caddr_t)xpl + sizeof(*xpl);
1665 /* if is the policy for ipsec ? */
1666 if (sp->policy == IPSEC_POLICY_IPSEC) {
1667 struct sadb_x_ipsecrequest *xisr;
1668 struct ipsecrequest *isr;
1670 for (isr = sp->req; isr != NULL; isr = isr->next) {
1672 xisr = (struct sadb_x_ipsecrequest *)p;
1674 xisr->sadb_x_ipsecrequest_proto = isr->saidx.proto;
1675 xisr->sadb_x_ipsecrequest_mode = isr->saidx.mode;
1676 xisr->sadb_x_ipsecrequest_level = isr->level;
1677 xisr->sadb_x_ipsecrequest_reqid = isr->saidx.reqid;
1680 bcopy(&isr->saidx.src, p, isr->saidx.src.sa.sa_len);
1681 p += isr->saidx.src.sa.sa_len;
1682 bcopy(&isr->saidx.dst, p, isr->saidx.dst.sa.sa_len);
1683 p += isr->saidx.src.sa.sa_len;
1685 xisr->sadb_x_ipsecrequest_len =
1686 PFKEY_ALIGN8(sizeof(*xisr)
1687 + isr->saidx.src.sa.sa_len
1688 + isr->saidx.dst.sa.sa_len);
1695 /* m will not be freed nor modified */
1696 static struct mbuf *
1698 key_gather_mbuf(struct mbuf *m, const struct sadb_msghdr *mhp,
1699 int ndeep, int nitem, ...)
1701 key_gather_mbuf(m, mhp, ndeep, nitem, va_alist)
1703 const struct sadb_msghdr *mhp;
1712 struct mbuf *result = NULL, *n;
1715 IPSEC_ASSERT(m != NULL, ("null mbuf"));
1716 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
1718 va_start(ap, nitem);
1719 for (i = 0; i < nitem; i++) {
1720 idx = va_arg(ap, int);
1721 if (idx < 0 || idx > SADB_EXT_MAX)
1723 /* don't attempt to pull empty extension */
1724 if (idx == SADB_EXT_RESERVED && mhp->msg == NULL)
1726 if (idx != SADB_EXT_RESERVED &&
1727 (mhp->ext[idx] == NULL || mhp->extlen[idx] == 0))
1730 if (idx == SADB_EXT_RESERVED) {
1731 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
1733 IPSEC_ASSERT(len <= MHLEN, ("header too big %u", len));
1735 MGETHDR(n, M_NOWAIT, MT_DATA);
1740 m_copydata(m, 0, sizeof(struct sadb_msg),
1742 } else if (i < ndeep) {
1743 len = mhp->extlen[idx];
1744 n = m_get2(len, M_NOWAIT, MT_DATA, 0);
1749 m_copydata(m, mhp->extoff[idx], mhp->extlen[idx],
1752 n = m_copym(m, mhp->extoff[idx], mhp->extlen[idx],
1765 if ((result->m_flags & M_PKTHDR) != 0) {
1766 result->m_pkthdr.len = 0;
1767 for (n = result; n; n = n->m_next)
1768 result->m_pkthdr.len += n->m_len;
1780 * SADB_X_SPDADD, SADB_X_SPDSETIDX or SADB_X_SPDUPDATE processing
1781 * add an entry to SP database, when received
1782 * <base, address(SD), (lifetime(H),) policy>
1784 * Adding to SP database,
1786 * <base, address(SD), (lifetime(H),) policy>
1787 * to the socket which was send.
1789 * SPDADD set a unique policy entry.
1790 * SPDSETIDX like SPDADD without a part of policy requests.
1791 * SPDUPDATE replace a unique policy entry.
1793 * m will always be freed.
1796 key_spdadd(so, m, mhp)
1799 const struct sadb_msghdr *mhp;
1801 struct sadb_address *src0, *dst0;
1802 struct sadb_x_policy *xpl0, *xpl;
1803 struct sadb_lifetime *lft = NULL;
1804 struct secpolicyindex spidx;
1805 struct secpolicy *newsp;
1808 IPSEC_ASSERT(so != NULL, ("null socket"));
1809 IPSEC_ASSERT(m != NULL, ("null mbuf"));
1810 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
1811 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
1813 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
1814 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
1815 mhp->ext[SADB_X_EXT_POLICY] == NULL) {
1816 ipseclog((LOG_DEBUG, "key_spdadd: invalid message is passed.\n"));
1817 return key_senderror(so, m, EINVAL);
1819 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
1820 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
1821 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
1822 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
1824 return key_senderror(so, m, EINVAL);
1826 if (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL) {
1827 if (mhp->extlen[SADB_EXT_LIFETIME_HARD]
1828 < sizeof(struct sadb_lifetime)) {
1829 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
1831 return key_senderror(so, m, EINVAL);
1833 lft = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_HARD];
1836 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
1837 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
1838 xpl0 = (struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY];
1841 * Note: do not parse SADB_X_EXT_NAT_T_* here:
1842 * we are processing traffic endpoints.
1846 /* XXX boundary check against sa_len */
1847 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
1850 src0->sadb_address_prefixlen,
1851 dst0->sadb_address_prefixlen,
1852 src0->sadb_address_proto,
1855 /* checking the direciton. */
1856 switch (xpl0->sadb_x_policy_dir) {
1857 case IPSEC_DIR_INBOUND:
1858 case IPSEC_DIR_OUTBOUND:
1861 ipseclog((LOG_DEBUG, "%s: Invalid SP direction.\n", __func__));
1862 mhp->msg->sadb_msg_errno = EINVAL;
1867 /* key_spdadd() accepts DISCARD, NONE and IPSEC. */
1868 if (xpl0->sadb_x_policy_type == IPSEC_POLICY_ENTRUST
1869 || xpl0->sadb_x_policy_type == IPSEC_POLICY_BYPASS) {
1870 ipseclog((LOG_DEBUG, "%s: Invalid policy type.\n", __func__));
1871 return key_senderror(so, m, EINVAL);
1874 /* policy requests are mandatory when action is ipsec. */
1875 if (mhp->msg->sadb_msg_type != SADB_X_SPDSETIDX
1876 && xpl0->sadb_x_policy_type == IPSEC_POLICY_IPSEC
1877 && mhp->extlen[SADB_X_EXT_POLICY] <= sizeof(*xpl0)) {
1878 ipseclog((LOG_DEBUG, "%s: some policy requests part required\n",
1880 return key_senderror(so, m, EINVAL);
1884 * checking there is SP already or not.
1885 * SPDUPDATE doesn't depend on whether there is a SP or not.
1886 * If the type is either SPDADD or SPDSETIDX AND a SP is found,
1889 newsp = key_getsp(&spidx);
1890 if (mhp->msg->sadb_msg_type == SADB_X_SPDUPDATE) {
1893 newsp->state = IPSEC_SPSTATE_DEAD;
1898 if (newsp != NULL) {
1900 ipseclog((LOG_DEBUG, "%s: a SP entry exists already.\n",
1902 return key_senderror(so, m, EEXIST);
1906 /* allocation new SP entry */
1907 if ((newsp = key_msg2sp(xpl0, PFKEY_EXTLEN(xpl0), &error)) == NULL) {
1908 return key_senderror(so, m, error);
1911 if ((newsp->id = key_getnewspid()) == 0) {
1913 return key_senderror(so, m, ENOBUFS);
1916 /* XXX boundary check against sa_len */
1917 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
1920 src0->sadb_address_prefixlen,
1921 dst0->sadb_address_prefixlen,
1922 src0->sadb_address_proto,
1925 /* sanity check on addr pair */
1926 if (((struct sockaddr *)(src0 + 1))->sa_family !=
1927 ((struct sockaddr *)(dst0+ 1))->sa_family) {
1929 return key_senderror(so, m, EINVAL);
1931 if (((struct sockaddr *)(src0 + 1))->sa_len !=
1932 ((struct sockaddr *)(dst0+ 1))->sa_len) {
1934 return key_senderror(so, m, EINVAL);
1937 if (newsp->req && newsp->req->saidx.src.sa.sa_family && newsp->req->saidx.dst.sa.sa_family) {
1938 if (newsp->req->saidx.src.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;
2130 sp->state = IPSEC_SPSTATE_DEAD;
2136 struct sadb_msg *newmsg;
2139 * Note: do not send SADB_X_EXT_NAT_T_* here:
2140 * we are sending traffic endpoints.
2143 /* create new sadb_msg to reply. */
2144 n = key_gather_mbuf(m, mhp, 1, 4, SADB_EXT_RESERVED,
2145 SADB_X_EXT_POLICY, SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
2147 return key_senderror(so, m, ENOBUFS);
2149 newmsg = mtod(n, struct sadb_msg *);
2150 newmsg->sadb_msg_errno = 0;
2151 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
2154 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
2159 * SADB_SPDDELETE2 processing
2162 * from the user(?), and set SADB_SASTATE_DEAD,
2166 * policy(*) including direction of policy.
2168 * m will always be freed.
2171 key_spddelete2(so, m, mhp)
2174 const struct sadb_msghdr *mhp;
2177 struct secpolicy *sp;
2179 IPSEC_ASSERT(so != NULL, ("null socket"));
2180 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2181 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2182 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2184 if (mhp->ext[SADB_X_EXT_POLICY] == NULL ||
2185 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2186 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n", __func__));
2187 return key_senderror(so, m, EINVAL);
2190 id = ((struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY])->sadb_x_policy_id;
2192 /* Is there SP in SPD ? */
2193 if ((sp = key_getspbyid(id)) == NULL) {
2194 ipseclog((LOG_DEBUG, "%s: no SP found id:%u.\n", __func__, id));
2195 return key_senderror(so, m, EINVAL);
2199 sp->state = IPSEC_SPSTATE_DEAD;
2204 struct mbuf *n, *nn;
2205 struct sadb_msg *newmsg;
2208 /* create new sadb_msg to reply. */
2209 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
2211 MGETHDR(n, M_NOWAIT, MT_DATA);
2212 if (n && len > MHLEN) {
2213 MCLGET(n, M_NOWAIT);
2214 if ((n->m_flags & M_EXT) == 0) {
2220 return key_senderror(so, m, ENOBUFS);
2226 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
2227 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
2229 IPSEC_ASSERT(off == len, ("length inconsistency (off %u len %u)",
2232 n->m_next = m_copym(m, mhp->extoff[SADB_X_EXT_POLICY],
2233 mhp->extlen[SADB_X_EXT_POLICY], M_NOWAIT);
2236 return key_senderror(so, m, ENOBUFS);
2239 n->m_pkthdr.len = 0;
2240 for (nn = n; nn; nn = nn->m_next)
2241 n->m_pkthdr.len += nn->m_len;
2243 newmsg = mtod(n, struct sadb_msg *);
2244 newmsg->sadb_msg_errno = 0;
2245 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
2248 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
2253 * SADB_X_GET processing
2258 * <base, address(SD), policy>
2260 * policy(*) including direction of policy.
2262 * m will always be freed.
2265 key_spdget(so, m, mhp)
2268 const struct sadb_msghdr *mhp;
2271 struct secpolicy *sp;
2274 IPSEC_ASSERT(so != NULL, ("null socket"));
2275 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2276 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2277 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2279 if (mhp->ext[SADB_X_EXT_POLICY] == NULL ||
2280 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2281 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
2283 return key_senderror(so, m, EINVAL);
2286 id = ((struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY])->sadb_x_policy_id;
2288 /* Is there SP in SPD ? */
2289 if ((sp = key_getspbyid(id)) == NULL) {
2290 ipseclog((LOG_DEBUG, "%s: no SP found id:%u.\n", __func__, id));
2291 return key_senderror(so, m, ENOENT);
2294 n = key_setdumpsp(sp, SADB_X_SPDGET, 0, mhp->msg->sadb_msg_pid);
2298 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
2300 return key_senderror(so, m, ENOBUFS);
2304 * SADB_X_SPDACQUIRE processing.
2305 * Acquire policy and SA(s) for a *OUTBOUND* packet.
2308 * to KMD, and expect to receive
2309 * <base> with SADB_X_SPDACQUIRE if error occured,
2312 * with SADB_X_SPDUPDATE from KMD by PF_KEY.
2313 * policy(*) is without policy requests.
2316 * others: error number
2320 struct secpolicy *sp;
2322 struct mbuf *result = NULL, *m;
2323 struct secspacq *newspacq;
2325 IPSEC_ASSERT(sp != NULL, ("null secpolicy"));
2326 IPSEC_ASSERT(sp->req == NULL, ("policy exists"));
2327 IPSEC_ASSERT(sp->policy == IPSEC_POLICY_IPSEC,
2328 ("policy not IPSEC %u", sp->policy));
2330 /* Get an entry to check whether sent message or not. */
2331 newspacq = key_getspacq(&sp->spidx);
2332 if (newspacq != NULL) {
2333 if (V_key_blockacq_count < newspacq->count) {
2334 /* reset counter and do send message. */
2335 newspacq->count = 0;
2337 /* increment counter and do nothing. */
2344 /* make new entry for blocking to send SADB_ACQUIRE. */
2345 newspacq = key_newspacq(&sp->spidx);
2346 if (newspacq == NULL)
2350 /* create new sadb_msg to reply. */
2351 m = key_setsadbmsg(SADB_X_SPDACQUIRE, 0, 0, 0, 0, 0);
2357 result->m_pkthdr.len = 0;
2358 for (m = result; m; m = m->m_next)
2359 result->m_pkthdr.len += m->m_len;
2361 mtod(result, struct sadb_msg *)->sadb_msg_len =
2362 PFKEY_UNIT64(result->m_pkthdr.len);
2364 return key_sendup_mbuf(NULL, m, KEY_SENDUP_REGISTERED);
2368 * SADB_SPDFLUSH processing
2371 * from the user, and free all entries in secpctree.
2375 * NOTE: what to do is only marking SADB_SASTATE_DEAD.
2377 * m will always be freed.
2380 key_spdflush(so, m, mhp)
2383 const struct sadb_msghdr *mhp;
2385 struct sadb_msg *newmsg;
2386 struct secpolicy *sp;
2389 IPSEC_ASSERT(so != NULL, ("null socket"));
2390 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2391 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2392 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2394 if (m->m_len != PFKEY_ALIGN8(sizeof(struct sadb_msg)))
2395 return key_senderror(so, m, EINVAL);
2397 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2399 LIST_FOREACH(sp, &V_sptree[dir], chain)
2400 sp->state = IPSEC_SPSTATE_DEAD;
2404 if (sizeof(struct sadb_msg) > m->m_len + M_TRAILINGSPACE(m)) {
2405 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
2406 return key_senderror(so, m, ENOBUFS);
2412 m->m_pkthdr.len = m->m_len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
2413 newmsg = mtod(m, struct sadb_msg *);
2414 newmsg->sadb_msg_errno = 0;
2415 newmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
2417 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
2421 * SADB_SPDDUMP processing
2424 * from the user, and dump all SP leaves
2429 * m will always be freed.
2432 key_spddump(so, m, mhp)
2435 const struct sadb_msghdr *mhp;
2437 struct secpolicy *sp;
2442 IPSEC_ASSERT(so != NULL, ("null socket"));
2443 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2444 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2445 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2447 /* search SPD entry and get buffer size. */
2450 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2451 LIST_FOREACH(sp, &V_sptree[dir], chain) {
2458 return key_senderror(so, m, ENOENT);
2461 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2462 LIST_FOREACH(sp, &V_sptree[dir], chain) {
2464 n = key_setdumpsp(sp, SADB_X_SPDDUMP, cnt,
2465 mhp->msg->sadb_msg_pid);
2468 key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
2477 static struct mbuf *
2478 key_setdumpsp(struct secpolicy *sp, u_int8_t type, u_int32_t seq, u_int32_t pid)
2480 struct mbuf *result = NULL, *m;
2481 struct seclifetime lt;
2483 m = key_setsadbmsg(type, 0, SADB_SATYPE_UNSPEC, seq, pid, sp->refcnt);
2489 * Note: do not send SADB_X_EXT_NAT_T_* here:
2490 * we are sending traffic endpoints.
2492 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
2493 &sp->spidx.src.sa, sp->spidx.prefs,
2494 sp->spidx.ul_proto);
2499 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
2500 &sp->spidx.dst.sa, sp->spidx.prefd,
2501 sp->spidx.ul_proto);
2512 lt.addtime=sp->created;
2513 lt.usetime= sp->lastused;
2514 m = key_setlifetime(<, SADB_EXT_LIFETIME_CURRENT);
2519 lt.addtime=sp->lifetime;
2520 lt.usetime= sp->validtime;
2521 m = key_setlifetime(<, SADB_EXT_LIFETIME_HARD);
2527 if ((result->m_flags & M_PKTHDR) == 0)
2530 if (result->m_len < sizeof(struct sadb_msg)) {
2531 result = m_pullup(result, sizeof(struct sadb_msg));
2536 result->m_pkthdr.len = 0;
2537 for (m = result; m; m = m->m_next)
2538 result->m_pkthdr.len += m->m_len;
2540 mtod(result, struct sadb_msg *)->sadb_msg_len =
2541 PFKEY_UNIT64(result->m_pkthdr.len);
2551 * get PFKEY message length for security policy and request.
2554 key_getspreqmsglen(sp)
2555 struct secpolicy *sp;
2559 tlen = sizeof(struct sadb_x_policy);
2561 /* if is the policy for ipsec ? */
2562 if (sp->policy != IPSEC_POLICY_IPSEC)
2565 /* get length of ipsec requests */
2567 struct ipsecrequest *isr;
2570 for (isr = sp->req; isr != NULL; isr = isr->next) {
2571 len = sizeof(struct sadb_x_ipsecrequest)
2572 + isr->saidx.src.sa.sa_len
2573 + isr->saidx.dst.sa.sa_len;
2575 tlen += PFKEY_ALIGN8(len);
2583 * SADB_SPDEXPIRE processing
2585 * <base, address(SD), lifetime(CH), policy>
2589 * others : error number
2593 struct secpolicy *sp;
2595 struct mbuf *result = NULL, *m;
2598 struct sadb_lifetime *lt;
2600 /* XXX: Why do we lock ? */
2602 IPSEC_ASSERT(sp != NULL, ("null secpolicy"));
2604 /* set msg header */
2605 m = key_setsadbmsg(SADB_X_SPDEXPIRE, 0, 0, 0, 0, 0);
2612 /* create lifetime extension (current and hard) */
2613 len = PFKEY_ALIGN8(sizeof(*lt)) * 2;
2614 m = m_get2(len, M_NOWAIT, MT_DATA, 0);
2621 bzero(mtod(m, caddr_t), len);
2622 lt = mtod(m, struct sadb_lifetime *);
2623 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
2624 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
2625 lt->sadb_lifetime_allocations = 0;
2626 lt->sadb_lifetime_bytes = 0;
2627 lt->sadb_lifetime_addtime = sp->created;
2628 lt->sadb_lifetime_usetime = sp->lastused;
2629 lt = (struct sadb_lifetime *)(mtod(m, caddr_t) + len / 2);
2630 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
2631 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
2632 lt->sadb_lifetime_allocations = 0;
2633 lt->sadb_lifetime_bytes = 0;
2634 lt->sadb_lifetime_addtime = sp->lifetime;
2635 lt->sadb_lifetime_usetime = sp->validtime;
2639 * Note: do not send SADB_X_EXT_NAT_T_* here:
2640 * we are sending traffic endpoints.
2643 /* set sadb_address for source */
2644 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
2646 sp->spidx.prefs, sp->spidx.ul_proto);
2653 /* set sadb_address for destination */
2654 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
2656 sp->spidx.prefd, sp->spidx.ul_proto);
2671 if ((result->m_flags & M_PKTHDR) == 0) {
2676 if (result->m_len < sizeof(struct sadb_msg)) {
2677 result = m_pullup(result, sizeof(struct sadb_msg));
2678 if (result == NULL) {
2684 result->m_pkthdr.len = 0;
2685 for (m = result; m; m = m->m_next)
2686 result->m_pkthdr.len += m->m_len;
2688 mtod(result, struct sadb_msg *)->sadb_msg_len =
2689 PFKEY_UNIT64(result->m_pkthdr.len);
2691 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
2699 /* %%% SAD management */
2701 * allocating a memory for new SA head, and copy from the values of mhp.
2702 * OUT: NULL : failure due to the lack of memory.
2703 * others : pointer to new SA head.
2705 static struct secashead *
2707 struct secasindex *saidx;
2709 struct secashead *newsah;
2711 IPSEC_ASSERT(saidx != NULL, ("null saidx"));
2713 newsah = malloc(sizeof(struct secashead), M_IPSEC_SAH, M_NOWAIT|M_ZERO);
2714 if (newsah != NULL) {
2716 for (i = 0; i < sizeof(newsah->savtree)/sizeof(newsah->savtree[0]); i++)
2717 LIST_INIT(&newsah->savtree[i]);
2718 newsah->saidx = *saidx;
2720 /* add to saidxtree */
2721 newsah->state = SADB_SASTATE_MATURE;
2724 LIST_INSERT_HEAD(&V_sahtree, newsah, chain);
2731 * delete SA index and all SA registerd.
2735 struct secashead *sah;
2737 struct secasvar *sav, *nextsav;
2741 IPSEC_ASSERT(sah != NULL, ("NULL sah"));
2742 SAHTREE_LOCK_ASSERT();
2744 /* searching all SA registerd in the secindex. */
2746 stateidx < _ARRAYLEN(saorder_state_any);
2748 u_int state = saorder_state_any[stateidx];
2749 LIST_FOREACH_SAFE(sav, &sah->savtree[state], chain, nextsav) {
2750 if (sav->refcnt == 0) {
2752 KEY_CHKSASTATE(state, sav->state, __func__);
2754 * do NOT call KEY_FREESAV here:
2755 * it will only delete the sav if refcnt == 1,
2756 * where we already know that refcnt == 0
2760 /* give up to delete this sa */
2765 if (!zombie) { /* delete only if there are savs */
2766 /* remove from tree of SA index */
2767 if (__LIST_CHAINED(sah))
2768 LIST_REMOVE(sah, chain);
2769 if (sah->route_cache.sa_route.ro_rt) {
2770 RTFREE(sah->route_cache.sa_route.ro_rt);
2771 sah->route_cache.sa_route.ro_rt = (struct rtentry *)NULL;
2773 free(sah, M_IPSEC_SAH);
2778 * allocating a new SA with LARVAL state. key_add() and key_getspi() call,
2779 * and copy the values of mhp into new buffer.
2780 * When SAD message type is GETSPI:
2781 * to set sequence number from acq_seq++,
2782 * to set zero to SPI.
2783 * not to call key_setsava().
2785 * others : pointer to new secasvar.
2787 * does not modify mbuf. does not free mbuf on error.
2789 static struct secasvar *
2790 key_newsav(m, mhp, sah, errp, where, tag)
2792 const struct sadb_msghdr *mhp;
2793 struct secashead *sah;
2798 struct secasvar *newsav;
2799 const struct sadb_sa *xsa;
2801 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2802 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2803 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2804 IPSEC_ASSERT(sah != NULL, ("null secashead"));
2806 newsav = malloc(sizeof(struct secasvar), M_IPSEC_SA, M_NOWAIT|M_ZERO);
2807 if (newsav == NULL) {
2808 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
2813 switch (mhp->msg->sadb_msg_type) {
2817 #ifdef IPSEC_DOSEQCHECK
2818 /* sync sequence number */
2819 if (mhp->msg->sadb_msg_seq == 0)
2821 (V_acq_seq = (V_acq_seq == ~0 ? 1 : ++V_acq_seq));
2824 newsav->seq = mhp->msg->sadb_msg_seq;
2829 if (mhp->ext[SADB_EXT_SA] == NULL) {
2830 free(newsav, M_IPSEC_SA);
2832 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
2837 xsa = (const struct sadb_sa *)mhp->ext[SADB_EXT_SA];
2838 newsav->spi = xsa->sadb_sa_spi;
2839 newsav->seq = mhp->msg->sadb_msg_seq;
2842 free(newsav, M_IPSEC_SA);
2849 /* copy sav values */
2850 if (mhp->msg->sadb_msg_type != SADB_GETSPI) {
2851 *errp = key_setsaval(newsav, m, mhp);
2853 free(newsav, M_IPSEC_SA);
2859 SECASVAR_LOCK_INIT(newsav);
2862 newsav->created = time_second;
2863 newsav->pid = mhp->msg->sadb_msg_pid;
2868 newsav->state = SADB_SASTATE_LARVAL;
2871 LIST_INSERT_TAIL(&sah->savtree[SADB_SASTATE_LARVAL], newsav,
2875 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
2876 printf("DP %s from %s:%u return SP:%p\n", __func__,
2877 where, tag, newsav));
2883 * free() SA variable entry.
2886 key_cleansav(struct secasvar *sav)
2889 * Cleanup xform state. Note that zeroize'ing causes the
2890 * keys to be cleared; otherwise we must do it ourself.
2892 if (sav->tdb_xform != NULL) {
2893 sav->tdb_xform->xf_zeroize(sav);
2894 sav->tdb_xform = NULL;
2896 KASSERT(sav->iv == NULL, ("iv but no xform"));
2897 if (sav->key_auth != NULL)
2898 bzero(sav->key_auth->key_data, _KEYLEN(sav->key_auth));
2899 if (sav->key_enc != NULL)
2900 bzero(sav->key_enc->key_data, _KEYLEN(sav->key_enc));
2902 if (sav->key_auth != NULL) {
2903 if (sav->key_auth->key_data != NULL)
2904 free(sav->key_auth->key_data, M_IPSEC_MISC);
2905 free(sav->key_auth, M_IPSEC_MISC);
2906 sav->key_auth = NULL;
2908 if (sav->key_enc != NULL) {
2909 if (sav->key_enc->key_data != NULL)
2910 free(sav->key_enc->key_data, M_IPSEC_MISC);
2911 free(sav->key_enc, M_IPSEC_MISC);
2912 sav->key_enc = NULL;
2915 bzero(sav->sched, sav->schedlen);
2916 free(sav->sched, M_IPSEC_MISC);
2919 if (sav->replay != NULL) {
2920 free(sav->replay, M_IPSEC_MISC);
2923 if (sav->lft_c != NULL) {
2924 free(sav->lft_c, M_IPSEC_MISC);
2927 if (sav->lft_h != NULL) {
2928 free(sav->lft_h, M_IPSEC_MISC);
2931 if (sav->lft_s != NULL) {
2932 free(sav->lft_s, M_IPSEC_MISC);
2938 * free() SA variable entry.
2942 struct secasvar *sav;
2944 IPSEC_ASSERT(sav != NULL, ("null sav"));
2945 IPSEC_ASSERT(sav->refcnt == 0, ("reference count %u > 0", sav->refcnt));
2947 /* remove from SA header */
2948 if (__LIST_CHAINED(sav))
2949 LIST_REMOVE(sav, chain);
2951 SECASVAR_LOCK_DESTROY(sav);
2952 free(sav, M_IPSEC_SA);
2959 * others : found, pointer to a SA.
2961 static struct secashead *
2963 struct secasindex *saidx;
2965 struct secashead *sah;
2968 LIST_FOREACH(sah, &V_sahtree, chain) {
2969 if (sah->state == SADB_SASTATE_DEAD)
2971 if (key_cmpsaidx(&sah->saidx, saidx, CMP_REQID))
2980 * check not to be duplicated SPI.
2981 * NOTE: this function is too slow due to searching all SAD.
2984 * others : found, pointer to a SA.
2986 static struct secasvar *
2987 key_checkspidup(saidx, spi)
2988 struct secasindex *saidx;
2991 struct secashead *sah;
2992 struct secasvar *sav;
2994 /* check address family */
2995 if (saidx->src.sa.sa_family != saidx->dst.sa.sa_family) {
2996 ipseclog((LOG_DEBUG, "%s: address family mismatched.\n",
3004 LIST_FOREACH(sah, &V_sahtree, chain) {
3005 if (!key_ismyaddr((struct sockaddr *)&sah->saidx.dst))
3007 sav = key_getsavbyspi(sah, spi);
3017 * search SAD litmited alive SA, protocol, SPI.
3020 * others : found, pointer to a SA.
3022 static struct secasvar *
3023 key_getsavbyspi(sah, spi)
3024 struct secashead *sah;
3027 struct secasvar *sav;
3028 u_int stateidx, state;
3031 SAHTREE_LOCK_ASSERT();
3032 /* search all status */
3034 stateidx < _ARRAYLEN(saorder_state_alive);
3037 state = saorder_state_alive[stateidx];
3038 LIST_FOREACH(sav, &sah->savtree[state], chain) {
3041 if (sav->state != state) {
3042 ipseclog((LOG_DEBUG, "%s: "
3043 "invalid sav->state (queue: %d SA: %d)\n",
3044 __func__, state, sav->state));
3048 if (sav->spi == spi)
3057 * copy SA values from PF_KEY message except *SPI, SEQ, PID, STATE and TYPE*.
3058 * You must update these if need.
3062 * does not modify mbuf. does not free mbuf on error.
3065 key_setsaval(sav, m, mhp)
3066 struct secasvar *sav;
3068 const struct sadb_msghdr *mhp;
3072 IPSEC_ASSERT(m != NULL, ("null mbuf"));
3073 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
3074 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
3076 /* initialization */
3078 sav->key_auth = NULL;
3079 sav->key_enc = NULL;
3086 sav->tdb_xform = NULL; /* transform */
3087 sav->tdb_encalgxform = NULL; /* encoding algorithm */
3088 sav->tdb_authalgxform = NULL; /* authentication algorithm */
3089 sav->tdb_compalgxform = NULL; /* compression algorithm */
3090 /* Initialize even if NAT-T not compiled in: */
3092 sav->natt_esp_frag_len = 0;
3095 if (mhp->ext[SADB_EXT_SA] != NULL) {
3096 const struct sadb_sa *sa0;
3098 sa0 = (const struct sadb_sa *)mhp->ext[SADB_EXT_SA];
3099 if (mhp->extlen[SADB_EXT_SA] < sizeof(*sa0)) {
3104 sav->alg_auth = sa0->sadb_sa_auth;
3105 sav->alg_enc = sa0->sadb_sa_encrypt;
3106 sav->flags = sa0->sadb_sa_flags;
3109 if ((sa0->sadb_sa_flags & SADB_X_EXT_OLD) == 0) {
3110 sav->replay = (struct secreplay *)
3111 malloc(sizeof(struct secreplay)+sa0->sadb_sa_replay, M_IPSEC_MISC, M_NOWAIT|M_ZERO);
3112 if (sav->replay == NULL) {
3113 ipseclog((LOG_DEBUG, "%s: No more memory.\n",
3118 if (sa0->sadb_sa_replay != 0)
3119 sav->replay->bitmap = (caddr_t)(sav->replay+1);
3120 sav->replay->wsize = sa0->sadb_sa_replay;
3124 /* Authentication keys */
3125 if (mhp->ext[SADB_EXT_KEY_AUTH] != NULL) {
3126 const struct sadb_key *key0;
3129 key0 = (const struct sadb_key *)mhp->ext[SADB_EXT_KEY_AUTH];
3130 len = mhp->extlen[SADB_EXT_KEY_AUTH];
3133 if (len < sizeof(*key0)) {
3137 switch (mhp->msg->sadb_msg_satype) {
3138 case SADB_SATYPE_AH:
3139 case SADB_SATYPE_ESP:
3140 case SADB_X_SATYPE_TCPSIGNATURE:
3141 if (len == PFKEY_ALIGN8(sizeof(struct sadb_key)) &&
3142 sav->alg_auth != SADB_X_AALG_NULL)
3145 case SADB_X_SATYPE_IPCOMP:
3151 ipseclog((LOG_DEBUG, "%s: invalid key_auth values.\n",
3156 sav->key_auth = (struct seckey *)key_dup_keymsg(key0, len,
3158 if (sav->key_auth == NULL ) {
3159 ipseclog((LOG_DEBUG, "%s: No more memory.\n",
3166 /* Encryption key */
3167 if (mhp->ext[SADB_EXT_KEY_ENCRYPT] != NULL) {
3168 const struct sadb_key *key0;
3171 key0 = (const struct sadb_key *)mhp->ext[SADB_EXT_KEY_ENCRYPT];
3172 len = mhp->extlen[SADB_EXT_KEY_ENCRYPT];
3175 if (len < sizeof(*key0)) {
3179 switch (mhp->msg->sadb_msg_satype) {
3180 case SADB_SATYPE_ESP:
3181 if (len == PFKEY_ALIGN8(sizeof(struct sadb_key)) &&
3182 sav->alg_enc != SADB_EALG_NULL) {
3186 sav->key_enc = (struct seckey *)key_dup_keymsg(key0,
3189 if (sav->key_enc == NULL) {
3190 ipseclog((LOG_DEBUG, "%s: No more memory.\n",
3196 case SADB_X_SATYPE_IPCOMP:
3197 if (len != PFKEY_ALIGN8(sizeof(struct sadb_key)))
3199 sav->key_enc = NULL; /*just in case*/
3201 case SADB_SATYPE_AH:
3202 case SADB_X_SATYPE_TCPSIGNATURE:
3208 ipseclog((LOG_DEBUG, "%s: invalid key_enc value.\n",
3217 switch (mhp->msg->sadb_msg_satype) {
3218 case SADB_SATYPE_AH:
3219 error = xform_init(sav, XF_AH);
3221 case SADB_SATYPE_ESP:
3222 error = xform_init(sav, XF_ESP);
3224 case SADB_X_SATYPE_IPCOMP:
3225 error = xform_init(sav, XF_IPCOMP);
3227 case SADB_X_SATYPE_TCPSIGNATURE:
3228 error = xform_init(sav, XF_TCPSIGNATURE);
3232 ipseclog((LOG_DEBUG, "%s: unable to initialize SA type %u.\n",
3233 __func__, mhp->msg->sadb_msg_satype));
3238 sav->created = time_second;
3240 /* make lifetime for CURRENT */
3241 sav->lft_c = malloc(sizeof(struct seclifetime), M_IPSEC_MISC, M_NOWAIT);
3242 if (sav->lft_c == NULL) {
3243 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
3248 sav->lft_c->allocations = 0;
3249 sav->lft_c->bytes = 0;
3250 sav->lft_c->addtime = time_second;
3251 sav->lft_c->usetime = 0;
3253 /* lifetimes for HARD and SOFT */
3255 const struct sadb_lifetime *lft0;
3257 lft0 = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_HARD];
3259 if (mhp->extlen[SADB_EXT_LIFETIME_HARD] < sizeof(*lft0)) {
3263 sav->lft_h = key_dup_lifemsg(lft0, M_IPSEC_MISC);
3264 if (sav->lft_h == NULL) {
3265 ipseclog((LOG_DEBUG, "%s: No more memory.\n",__func__));
3269 /* to be initialize ? */
3272 lft0 = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_SOFT];
3274 if (mhp->extlen[SADB_EXT_LIFETIME_SOFT] < sizeof(*lft0)) {
3278 sav->lft_s = key_dup_lifemsg(lft0, M_IPSEC_MISC);
3279 if (sav->lft_s == NULL) {
3280 ipseclog((LOG_DEBUG, "%s: No more memory.\n",__func__));
3284 /* to be initialize ? */
3291 /* initialization */
3298 * validation with a secasvar entry, and set SADB_SATYPE_MATURE.
3303 key_mature(struct secasvar *sav)
3307 /* check SPI value */
3308 switch (sav->sah->saidx.proto) {
3312 * RFC 4302, 2.4. Security Parameters Index (SPI), SPI values
3313 * 1-255 reserved by IANA for future use,
3314 * 0 for implementation specific, local use.
3316 if (ntohl(sav->spi) <= 255) {
3317 ipseclog((LOG_DEBUG, "%s: illegal range of SPI %u.\n",
3318 __func__, (u_int32_t)ntohl(sav->spi)));
3325 switch (sav->sah->saidx.proto) {
3328 if ((sav->flags & (SADB_X_EXT_OLD|SADB_X_EXT_DERIV)) ==
3329 (SADB_X_EXT_OLD|SADB_X_EXT_DERIV)) {
3330 ipseclog((LOG_DEBUG, "%s: invalid flag (derived) "
3331 "given to old-esp.\n", __func__));
3334 error = xform_init(sav, XF_ESP);
3338 if (sav->flags & SADB_X_EXT_DERIV) {
3339 ipseclog((LOG_DEBUG, "%s: invalid flag (derived) "
3340 "given to AH SA.\n", __func__));
3343 if (sav->alg_enc != SADB_EALG_NONE) {
3344 ipseclog((LOG_DEBUG, "%s: protocol and algorithm "
3345 "mismated.\n", __func__));
3348 error = xform_init(sav, XF_AH);
3350 case IPPROTO_IPCOMP:
3351 if (sav->alg_auth != SADB_AALG_NONE) {
3352 ipseclog((LOG_DEBUG, "%s: protocol and algorithm "
3353 "mismated.\n", __func__));
3356 if ((sav->flags & SADB_X_EXT_RAWCPI) == 0
3357 && ntohl(sav->spi) >= 0x10000) {
3358 ipseclog((LOG_DEBUG, "%s: invalid cpi for IPComp.\n",
3362 error = xform_init(sav, XF_IPCOMP);
3365 if (sav->alg_enc != SADB_EALG_NONE) {
3366 ipseclog((LOG_DEBUG, "%s: protocol and algorithm "
3367 "mismated.\n", __func__));
3370 error = xform_init(sav, XF_TCPSIGNATURE);
3373 ipseclog((LOG_DEBUG, "%s: Invalid satype.\n", __func__));
3374 error = EPROTONOSUPPORT;
3379 key_sa_chgstate(sav, SADB_SASTATE_MATURE);
3386 * subroutine for SADB_GET and SADB_DUMP.
3388 static struct mbuf *
3389 key_setdumpsa(struct secasvar *sav, u_int8_t type, u_int8_t satype,
3390 u_int32_t seq, u_int32_t pid)
3392 struct mbuf *result = NULL, *tres = NULL, *m;
3395 SADB_EXT_SA, SADB_X_EXT_SA2,
3396 SADB_EXT_LIFETIME_HARD, SADB_EXT_LIFETIME_SOFT,
3397 SADB_EXT_LIFETIME_CURRENT, SADB_EXT_ADDRESS_SRC,
3398 SADB_EXT_ADDRESS_DST, SADB_EXT_ADDRESS_PROXY, SADB_EXT_KEY_AUTH,
3399 SADB_EXT_KEY_ENCRYPT, SADB_EXT_IDENTITY_SRC,
3400 SADB_EXT_IDENTITY_DST, SADB_EXT_SENSITIVITY,
3402 SADB_X_EXT_NAT_T_TYPE,
3403 SADB_X_EXT_NAT_T_SPORT, SADB_X_EXT_NAT_T_DPORT,
3404 SADB_X_EXT_NAT_T_OAI, SADB_X_EXT_NAT_T_OAR,
3405 SADB_X_EXT_NAT_T_FRAG,
3409 m = key_setsadbmsg(type, 0, satype, seq, pid, sav->refcnt);
3414 for (i = sizeof(dumporder)/sizeof(dumporder[0]) - 1; i >= 0; i--) {
3416 switch (dumporder[i]) {
3418 m = key_setsadbsa(sav);
3423 case SADB_X_EXT_SA2:
3424 m = key_setsadbxsa2(sav->sah->saidx.mode,
3425 sav->replay ? sav->replay->count : 0,
3426 sav->sah->saidx.reqid);
3431 case SADB_EXT_ADDRESS_SRC:
3432 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
3433 &sav->sah->saidx.src.sa,
3434 FULLMASK, IPSEC_ULPROTO_ANY);
3439 case SADB_EXT_ADDRESS_DST:
3440 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
3441 &sav->sah->saidx.dst.sa,
3442 FULLMASK, IPSEC_ULPROTO_ANY);
3447 case SADB_EXT_KEY_AUTH:
3450 m = key_setkey(sav->key_auth, SADB_EXT_KEY_AUTH);
3455 case SADB_EXT_KEY_ENCRYPT:
3458 m = key_setkey(sav->key_enc, SADB_EXT_KEY_ENCRYPT);
3463 case SADB_EXT_LIFETIME_CURRENT:
3466 m = key_setlifetime(sav->lft_c,
3467 SADB_EXT_LIFETIME_CURRENT);
3472 case SADB_EXT_LIFETIME_HARD:
3475 m = key_setlifetime(sav->lft_h,
3476 SADB_EXT_LIFETIME_HARD);
3481 case SADB_EXT_LIFETIME_SOFT:
3484 m = key_setlifetime(sav->lft_s,
3485 SADB_EXT_LIFETIME_SOFT);
3492 case SADB_X_EXT_NAT_T_TYPE:
3493 m = key_setsadbxtype(sav->natt_type);
3498 case SADB_X_EXT_NAT_T_DPORT:
3499 m = key_setsadbxport(
3500 KEY_PORTFROMSADDR(&sav->sah->saidx.dst),
3501 SADB_X_EXT_NAT_T_DPORT);
3506 case SADB_X_EXT_NAT_T_SPORT:
3507 m = key_setsadbxport(
3508 KEY_PORTFROMSADDR(&sav->sah->saidx.src),
3509 SADB_X_EXT_NAT_T_SPORT);
3514 case SADB_X_EXT_NAT_T_OAI:
3515 case SADB_X_EXT_NAT_T_OAR:
3516 case SADB_X_EXT_NAT_T_FRAG:
3517 /* We do not (yet) support those. */
3521 case SADB_EXT_ADDRESS_PROXY:
3522 case SADB_EXT_IDENTITY_SRC:
3523 case SADB_EXT_IDENTITY_DST:
3524 /* XXX: should we brought from SPD ? */
3525 case SADB_EXT_SENSITIVITY:
3538 m_cat(result, tres);
3539 if (result->m_len < sizeof(struct sadb_msg)) {
3540 result = m_pullup(result, sizeof(struct sadb_msg));
3545 result->m_pkthdr.len = 0;
3546 for (m = result; m; m = m->m_next)
3547 result->m_pkthdr.len += m->m_len;
3549 mtod(result, struct sadb_msg *)->sadb_msg_len =
3550 PFKEY_UNIT64(result->m_pkthdr.len);
3561 * set data into sadb_msg.
3563 static struct mbuf *
3564 key_setsadbmsg(u_int8_t type, u_int16_t tlen, u_int8_t satype, u_int32_t seq,
3565 pid_t pid, u_int16_t reserved)
3571 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
3574 MGETHDR(m, M_NOWAIT, MT_DATA);
3575 if (m && len > MHLEN) {
3576 MCLGET(m, M_NOWAIT);
3577 if ((m->m_flags & M_EXT) == 0) {
3584 m->m_pkthdr.len = m->m_len = len;
3587 p = mtod(m, struct sadb_msg *);
3590 p->sadb_msg_version = PF_KEY_V2;
3591 p->sadb_msg_type = type;
3592 p->sadb_msg_errno = 0;
3593 p->sadb_msg_satype = satype;
3594 p->sadb_msg_len = PFKEY_UNIT64(tlen);
3595 p->sadb_msg_reserved = reserved;
3596 p->sadb_msg_seq = seq;
3597 p->sadb_msg_pid = (u_int32_t)pid;
3603 * copy secasvar data into sadb_address.
3605 static struct mbuf *
3607 struct secasvar *sav;
3613 len = PFKEY_ALIGN8(sizeof(struct sadb_sa));
3614 m = m_get2(len, M_NOWAIT, MT_DATA, 0);
3619 p = mtod(m, struct sadb_sa *);
3621 p->sadb_sa_len = PFKEY_UNIT64(len);
3622 p->sadb_sa_exttype = SADB_EXT_SA;
3623 p->sadb_sa_spi = sav->spi;
3624 p->sadb_sa_replay = (sav->replay != NULL ? sav->replay->wsize : 0);
3625 p->sadb_sa_state = sav->state;
3626 p->sadb_sa_auth = sav->alg_auth;
3627 p->sadb_sa_encrypt = sav->alg_enc;
3628 p->sadb_sa_flags = sav->flags;
3634 * set data into sadb_address.
3636 static struct mbuf *
3637 key_setsadbaddr(u_int16_t exttype, const struct sockaddr *saddr, u_int8_t prefixlen, u_int16_t ul_proto)
3640 struct sadb_address *p;
3643 len = PFKEY_ALIGN8(sizeof(struct sadb_address)) +
3644 PFKEY_ALIGN8(saddr->sa_len);
3645 m = m_get2(len, M_NOWAIT, MT_DATA, 0);
3650 p = mtod(m, struct sadb_address *);
3653 p->sadb_address_len = PFKEY_UNIT64(len);
3654 p->sadb_address_exttype = exttype;
3655 p->sadb_address_proto = ul_proto;
3656 if (prefixlen == FULLMASK) {
3657 switch (saddr->sa_family) {
3659 prefixlen = sizeof(struct in_addr) << 3;
3662 prefixlen = sizeof(struct in6_addr) << 3;
3668 p->sadb_address_prefixlen = prefixlen;
3669 p->sadb_address_reserved = 0;
3672 mtod(m, caddr_t) + PFKEY_ALIGN8(sizeof(struct sadb_address)),
3679 * set data into sadb_x_sa2.
3681 static struct mbuf *
3682 key_setsadbxsa2(u_int8_t mode, u_int32_t seq, u_int32_t reqid)
3685 struct sadb_x_sa2 *p;
3688 len = PFKEY_ALIGN8(sizeof(struct sadb_x_sa2));
3689 m = m_get2(len, M_NOWAIT, MT_DATA, 0);
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 = m_get2(len, M_NOWAIT, MT_DATA, 0);
3726 p = mtod(m, struct sadb_x_nat_t_type *);
3729 p->sadb_x_nat_t_type_len = PFKEY_UNIT64(len);
3730 p->sadb_x_nat_t_type_exttype = SADB_X_EXT_NAT_T_TYPE;
3731 p->sadb_x_nat_t_type_type = type;
3736 * Set a port in sadb_x_nat_t_port.
3737 * In contrast to default RFC 2367 behaviour, port is in network byte order.
3739 static struct mbuf *
3740 key_setsadbxport(u_int16_t port, u_int16_t type)
3744 struct sadb_x_nat_t_port *p;
3746 len = PFKEY_ALIGN8(sizeof(struct sadb_x_nat_t_port));
3748 m = m_get2(len, M_NOWAIT, MT_DATA, 0);
3753 p = mtod(m, struct sadb_x_nat_t_port *);
3756 p->sadb_x_nat_t_port_len = PFKEY_UNIT64(len);
3757 p->sadb_x_nat_t_port_exttype = type;
3758 p->sadb_x_nat_t_port_port = port;
3764 * Get port from sockaddr. Port is in network byte order.
3767 key_portfromsaddr(struct sockaddr *sa)
3770 switch (sa->sa_family) {
3773 return ((struct sockaddr_in *)sa)->sin_port;
3777 return ((struct sockaddr_in6 *)sa)->sin6_port;
3780 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
3781 printf("DP %s unexpected address family %d\n",
3782 __func__, sa->sa_family));
3785 #endif /* IPSEC_NAT_T */
3788 * Set port in struct sockaddr. Port is in network byte order.
3791 key_porttosaddr(struct sockaddr *sa, u_int16_t port)
3794 switch (sa->sa_family) {
3797 ((struct sockaddr_in *)sa)->sin_port = port;
3802 ((struct sockaddr_in6 *)sa)->sin6_port = port;
3806 ipseclog((LOG_DEBUG, "%s: unexpected address family %d.\n",
3807 __func__, sa->sa_family));
3813 * set data into sadb_x_policy
3815 static struct mbuf *
3816 key_setsadbxpolicy(u_int16_t type, u_int8_t dir, u_int32_t id)
3819 struct sadb_x_policy *p;
3822 len = PFKEY_ALIGN8(sizeof(struct sadb_x_policy));
3823 m = m_get2(len, M_NOWAIT, MT_DATA, 0);
3828 p = mtod(m, struct sadb_x_policy *);
3831 p->sadb_x_policy_len = PFKEY_UNIT64(len);
3832 p->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3833 p->sadb_x_policy_type = type;
3834 p->sadb_x_policy_dir = dir;
3835 p->sadb_x_policy_id = id;
3841 /* Take a key message (sadb_key) from the socket and turn it into one
3842 * of the kernel's key structures (seckey).
3844 * IN: pointer to the src
3845 * OUT: NULL no more memory
3848 key_dup_keymsg(const struct sadb_key *src, u_int len,
3849 struct malloc_type *type)
3852 dst = (struct seckey *)malloc(sizeof(struct seckey), type, M_NOWAIT);
3854 dst->bits = src->sadb_key_bits;
3855 dst->key_data = (char *)malloc(len, type, M_NOWAIT);
3856 if (dst->key_data != NULL) {
3857 bcopy((const char *)src + sizeof(struct sadb_key),
3858 dst->key_data, len);
3860 ipseclog((LOG_DEBUG, "%s: No more memory.\n",
3866 ipseclog((LOG_DEBUG, "%s: No more memory.\n",
3873 /* Take a lifetime message (sadb_lifetime) passed in on a socket and
3874 * turn it into one of the kernel's lifetime structures (seclifetime).
3876 * IN: pointer to the destination, source and malloc type
3877 * OUT: NULL, no more memory
3880 static struct seclifetime *
3881 key_dup_lifemsg(const struct sadb_lifetime *src,
3882 struct malloc_type *type)
3884 struct seclifetime *dst = NULL;
3886 dst = (struct seclifetime *)malloc(sizeof(struct seclifetime),
3890 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
3892 dst->allocations = src->sadb_lifetime_allocations;
3893 dst->bytes = src->sadb_lifetime_bytes;
3894 dst->addtime = src->sadb_lifetime_addtime;
3895 dst->usetime = src->sadb_lifetime_usetime;
3900 /* compare my own address
3901 * OUT: 1: true, i.e. my address.
3905 key_ismyaddr(struct sockaddr *sa)
3908 IPSEC_ASSERT(sa != NULL, ("null sockaddr"));
3909 switch (sa->sa_family) {
3912 return (in_localip(satosin(sa)->sin_addr));
3916 return key_ismyaddr6((struct sockaddr_in6 *)sa);
3925 * compare my own address for IPv6.
3928 * NOTE: derived ip6_input() in KAME. This is necessary to modify more.
3930 #include <netinet6/in6_var.h>
3934 struct sockaddr_in6 *sin6;
3936 struct in6_ifaddr *ia;
3938 struct in6_multi *in6m;
3942 TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) {
3943 if (key_sockaddrcmp((struct sockaddr *)&sin6,
3944 (struct sockaddr *)&ia->ia_addr, 0) == 0) {
3945 IN6_IFADDR_RUNLOCK();
3952 * XXX why do we care about multlicast here while we don't care
3953 * about IPv4 multicast??
3957 IN6_LOOKUP_MULTI(sin6->sin6_addr, ia->ia_ifp, in6m);
3959 IN6_IFADDR_RUNLOCK();
3964 IN6_IFADDR_RUNLOCK();
3966 /* loopback, just for safety */
3967 if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
3975 * compare two secasindex structure.
3976 * flag can specify to compare 2 saidxes.
3977 * compare two secasindex structure without both mode and reqid.
3978 * don't compare port.
3980 * saidx0: source, it can be in SAD.
3988 const struct secasindex *saidx0,
3989 const struct secasindex *saidx1,
3995 if (saidx0 == NULL && saidx1 == NULL)
3998 if (saidx0 == NULL || saidx1 == NULL)
4001 if (saidx0->proto != saidx1->proto)
4004 if (flag == CMP_EXACTLY) {
4005 if (saidx0->mode != saidx1->mode)
4007 if (saidx0->reqid != saidx1->reqid)
4009 if (bcmp(&saidx0->src, &saidx1->src, saidx0->src.sa.sa_len) != 0 ||
4010 bcmp(&saidx0->dst, &saidx1->dst, saidx0->dst.sa.sa_len) != 0)
4014 /* CMP_MODE_REQID, CMP_REQID, CMP_HEAD */
4015 if (flag == CMP_MODE_REQID
4016 ||flag == CMP_REQID) {
4018 * If reqid of SPD is non-zero, unique SA is required.
4019 * The result must be of same reqid in this case.
4021 if (saidx1->reqid != 0 && saidx0->reqid != saidx1->reqid)
4025 if (flag == CMP_MODE_REQID) {
4026 if (saidx0->mode != IPSEC_MODE_ANY
4027 && saidx0->mode != saidx1->mode)
4033 * If NAT-T is enabled, check ports for tunnel mode.
4034 * Do not check ports if they are set to zero in the SPD.
4035 * Also do not do it for native transport mode, as there
4036 * is no port information available in the SP.
4038 if ((saidx1->mode == IPSEC_MODE_TUNNEL ||
4039 (saidx1->mode == IPSEC_MODE_TRANSPORT &&
4040 saidx1->proto == IPPROTO_ESP)) &&
4041 saidx1->src.sa.sa_family == AF_INET &&
4042 saidx1->dst.sa.sa_family == AF_INET &&
4043 ((const struct sockaddr_in *)(&saidx1->src))->sin_port &&
4044 ((const struct sockaddr_in *)(&saidx1->dst))->sin_port)
4046 #endif /* IPSEC_NAT_T */
4048 if (key_sockaddrcmp(&saidx0->src.sa, &saidx1->src.sa, chkport) != 0) {
4051 if (key_sockaddrcmp(&saidx0->dst.sa, &saidx1->dst.sa, chkport) != 0) {
4060 * compare two secindex structure exactly.
4062 * spidx0: source, it is often in SPD.
4063 * spidx1: object, it is often from PFKEY message.
4069 key_cmpspidx_exactly(
4070 struct secpolicyindex *spidx0,
4071 struct secpolicyindex *spidx1)
4074 if (spidx0 == NULL && spidx1 == NULL)
4077 if (spidx0 == NULL || spidx1 == NULL)
4080 if (spidx0->prefs != spidx1->prefs
4081 || spidx0->prefd != spidx1->prefd
4082 || spidx0->ul_proto != spidx1->ul_proto)
4085 return key_sockaddrcmp(&spidx0->src.sa, &spidx1->src.sa, 1) == 0 &&
4086 key_sockaddrcmp(&spidx0->dst.sa, &spidx1->dst.sa, 1) == 0;
4090 * compare two secindex structure with mask.
4092 * spidx0: source, it is often in SPD.
4093 * spidx1: object, it is often from IP header.
4099 key_cmpspidx_withmask(
4100 struct secpolicyindex *spidx0,
4101 struct secpolicyindex *spidx1)
4104 if (spidx0 == NULL && spidx1 == NULL)
4107 if (spidx0 == NULL || spidx1 == NULL)
4110 if (spidx0->src.sa.sa_family != spidx1->src.sa.sa_family ||
4111 spidx0->dst.sa.sa_family != spidx1->dst.sa.sa_family ||
4112 spidx0->src.sa.sa_len != spidx1->src.sa.sa_len ||
4113 spidx0->dst.sa.sa_len != spidx1->dst.sa.sa_len)
4116 /* if spidx.ul_proto == IPSEC_ULPROTO_ANY, ignore. */
4117 if (spidx0->ul_proto != (u_int16_t)IPSEC_ULPROTO_ANY
4118 && spidx0->ul_proto != spidx1->ul_proto)
4121 switch (spidx0->src.sa.sa_family) {
4123 if (spidx0->src.sin.sin_port != IPSEC_PORT_ANY
4124 && spidx0->src.sin.sin_port != spidx1->src.sin.sin_port)
4126 if (!key_bbcmp(&spidx0->src.sin.sin_addr,
4127 &spidx1->src.sin.sin_addr, spidx0->prefs))
4131 if (spidx0->src.sin6.sin6_port != IPSEC_PORT_ANY
4132 && spidx0->src.sin6.sin6_port != spidx1->src.sin6.sin6_port)
4135 * scope_id check. if sin6_scope_id is 0, we regard it
4136 * as a wildcard scope, which matches any scope zone ID.
4138 if (spidx0->src.sin6.sin6_scope_id &&
4139 spidx1->src.sin6.sin6_scope_id &&
4140 spidx0->src.sin6.sin6_scope_id != spidx1->src.sin6.sin6_scope_id)
4142 if (!key_bbcmp(&spidx0->src.sin6.sin6_addr,
4143 &spidx1->src.sin6.sin6_addr, spidx0->prefs))
4148 if (bcmp(&spidx0->src, &spidx1->src, spidx0->src.sa.sa_len) != 0)
4153 switch (spidx0->dst.sa.sa_family) {
4155 if (spidx0->dst.sin.sin_port != IPSEC_PORT_ANY
4156 && spidx0->dst.sin.sin_port != spidx1->dst.sin.sin_port)
4158 if (!key_bbcmp(&spidx0->dst.sin.sin_addr,
4159 &spidx1->dst.sin.sin_addr, spidx0->prefd))
4163 if (spidx0->dst.sin6.sin6_port != IPSEC_PORT_ANY
4164 && spidx0->dst.sin6.sin6_port != spidx1->dst.sin6.sin6_port)
4167 * scope_id check. if sin6_scope_id is 0, we regard it
4168 * as a wildcard scope, which matches any scope zone ID.
4170 if (spidx0->dst.sin6.sin6_scope_id &&
4171 spidx1->dst.sin6.sin6_scope_id &&
4172 spidx0->dst.sin6.sin6_scope_id != spidx1->dst.sin6.sin6_scope_id)
4174 if (!key_bbcmp(&spidx0->dst.sin6.sin6_addr,
4175 &spidx1->dst.sin6.sin6_addr, spidx0->prefd))
4180 if (bcmp(&spidx0->dst, &spidx1->dst, spidx0->dst.sa.sa_len) != 0)
4185 /* XXX Do we check other field ? e.g. flowinfo */
4190 /* returns 0 on match */
4193 const struct sockaddr *sa1,
4194 const struct sockaddr *sa2,
4200 #define satosin(s) ((const struct sockaddr_in *)s)
4204 #define satosin6(s) ((const struct sockaddr_in6 *)s)
4205 if (sa1->sa_family != sa2->sa_family || sa1->sa_len != sa2->sa_len)
4208 switch (sa1->sa_family) {
4210 if (sa1->sa_len != sizeof(struct sockaddr_in))
4212 if (satosin(sa1)->sin_addr.s_addr !=
4213 satosin(sa2)->sin_addr.s_addr) {
4216 if (port && satosin(sa1)->sin_port != satosin(sa2)->sin_port)
4220 if (sa1->sa_len != sizeof(struct sockaddr_in6))
4221 return 1; /*EINVAL*/
4222 if (satosin6(sa1)->sin6_scope_id !=
4223 satosin6(sa2)->sin6_scope_id) {
4226 if (!IN6_ARE_ADDR_EQUAL(&satosin6(sa1)->sin6_addr,
4227 &satosin6(sa2)->sin6_addr)) {
4231 satosin6(sa1)->sin6_port != satosin6(sa2)->sin6_port) {
4236 if (bcmp(sa1, sa2, sa1->sa_len) != 0)
4247 * compare two buffers with mask.
4251 * bits: Number of bits to compare
4257 key_bbcmp(const void *a1, const void *a2, u_int bits)
4259 const unsigned char *p1 = a1;
4260 const unsigned char *p2 = a2;
4262 /* XXX: This could be considerably faster if we compare a word
4263 * at a time, but it is complicated on LSB Endian machines */
4265 /* Handle null pointers */
4266 if (p1 == NULL || p2 == NULL)
4276 u_int8_t mask = ~((1<<(8-bits))-1);
4277 if ((*p1 & mask) != (*p2 & mask))
4280 return 1; /* Match! */
4284 key_flush_spd(time_t now)
4286 static u_int16_t sptree_scangen = 0;
4287 u_int16_t gen = sptree_scangen++;
4288 struct secpolicy *sp;
4292 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
4295 LIST_FOREACH(sp, &V_sptree[dir], chain) {
4296 if (sp->scangen == gen) /* previously handled */
4299 if (sp->state == IPSEC_SPSTATE_DEAD &&
4302 * Ensure that we only decrease refcnt once,
4303 * when we're the last consumer.
4304 * Directly call SP_DELREF/key_delsp instead
4305 * of KEY_FREESP to avoid unlocking/relocking
4306 * SPTREE_LOCK before key_delsp: may refcnt
4307 * be increased again during that time ?
4308 * NB: also clean entries created by
4316 if (sp->lifetime == 0 && sp->validtime == 0)
4318 if ((sp->lifetime && now - sp->created > sp->lifetime)
4319 || (sp->validtime && now - sp->lastused > sp->validtime)) {
4320 sp->state = IPSEC_SPSTATE_DEAD;
4331 key_flush_sad(time_t now)
4333 struct secashead *sah, *nextsah;
4334 struct secasvar *sav, *nextsav;
4338 LIST_FOREACH_SAFE(sah, &V_sahtree, chain, nextsah) {
4339 /* if sah has been dead, then delete it and process next sah. */
4340 if (sah->state == SADB_SASTATE_DEAD) {
4345 /* if LARVAL entry doesn't become MATURE, delete it. */
4346 LIST_FOREACH_SAFE(sav, &sah->savtree[SADB_SASTATE_LARVAL], chain, nextsav) {
4347 /* Need to also check refcnt for a larval SA ??? */
4348 if (now - sav->created > V_key_larval_lifetime)
4353 * check MATURE entry to start to send expire message
4356 LIST_FOREACH_SAFE(sav, &sah->savtree[SADB_SASTATE_MATURE], chain, nextsav) {
4357 /* we don't need to check. */
4358 if (sav->lft_s == NULL)
4362 if (sav->lft_c == NULL) {
4363 ipseclog((LOG_DEBUG,"%s: there is no CURRENT "
4364 "time, why?\n", __func__));
4368 /* check SOFT lifetime */
4369 if (sav->lft_s->addtime != 0 &&
4370 now - sav->created > sav->lft_s->addtime) {
4371 key_sa_chgstate(sav, SADB_SASTATE_DYING);
4373 * Actually, only send expire message if
4374 * SA has been used, as it was done before,
4375 * but should we always send such message,
4376 * and let IKE daemon decide if it should be
4377 * renegotiated or not ?
4378 * XXX expire message will actually NOT be
4379 * sent if SA is only used after soft
4380 * lifetime has been reached, see below
4383 if (sav->lft_c->usetime != 0)
4386 /* check SOFT lifetime by bytes */
4388 * XXX I don't know the way to delete this SA
4389 * when new SA is installed. Caution when it's
4390 * installed too big lifetime by time.
4392 else if (sav->lft_s->bytes != 0 &&
4393 sav->lft_s->bytes < sav->lft_c->bytes) {
4395 key_sa_chgstate(sav, SADB_SASTATE_DYING);
4397 * XXX If we keep to send expire
4398 * message in the status of
4399 * DYING. Do remove below code.
4405 /* check DYING entry to change status to DEAD. */
4406 LIST_FOREACH_SAFE(sav, &sah->savtree[SADB_SASTATE_DYING], chain, nextsav) {
4407 /* we don't need to check. */
4408 if (sav->lft_h == NULL)
4412 if (sav->lft_c == NULL) {
4413 ipseclog((LOG_DEBUG, "%s: there is no CURRENT "
4414 "time, why?\n", __func__));
4418 if (sav->lft_h->addtime != 0 &&
4419 now - sav->created > sav->lft_h->addtime) {
4420 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4423 #if 0 /* XXX Should we keep to send expire message until HARD lifetime ? */
4424 else if (sav->lft_s != NULL
4425 && sav->lft_s->addtime != 0
4426 && now - sav->created > sav->lft_s->addtime) {
4428 * XXX: should be checked to be
4429 * installed the valid SA.
4433 * If there is no SA then sending
4439 /* check HARD lifetime by bytes */
4440 else if (sav->lft_h->bytes != 0 &&
4441 sav->lft_h->bytes < sav->lft_c->bytes) {
4442 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4447 /* delete entry in DEAD */
4448 LIST_FOREACH_SAFE(sav, &sah->savtree[SADB_SASTATE_DEAD], chain, nextsav) {
4450 if (sav->state != SADB_SASTATE_DEAD) {
4451 ipseclog((LOG_DEBUG, "%s: invalid sav->state "
4452 "(queue: %d SA: %d): kill it anyway\n",
4454 SADB_SASTATE_DEAD, sav->state));
4457 * do not call key_freesav() here.
4458 * sav should already be freed, and sav->refcnt
4459 * shows other references to sav
4460 * (such as from SPD).
4468 key_flush_acq(time_t now)
4470 struct secacq *acq, *nextacq;
4474 for (acq = LIST_FIRST(&V_acqtree); acq != NULL; acq = nextacq) {
4475 nextacq = LIST_NEXT(acq, chain);
4476 if (now - acq->created > V_key_blockacq_lifetime
4477 && __LIST_CHAINED(acq)) {
4478 LIST_REMOVE(acq, chain);
4479 free(acq, M_IPSEC_SAQ);
4486 key_flush_spacq(time_t now)
4488 struct secspacq *acq, *nextacq;
4492 for (acq = LIST_FIRST(&V_spacqtree); acq != NULL; acq = nextacq) {
4493 nextacq = LIST_NEXT(acq, chain);
4494 if (now - acq->created > V_key_blockacq_lifetime
4495 && __LIST_CHAINED(acq)) {
4496 LIST_REMOVE(acq, chain);
4497 free(acq, M_IPSEC_SAQ);
4505 * scanning SPD and SAD to check status for each entries,
4506 * and do to remove or to expire.
4507 * XXX: year 2038 problem may remain.
4510 key_timehandler(void)
4512 VNET_ITERATOR_DECL(vnet_iter);
4513 time_t now = time_second;
4515 VNET_LIST_RLOCK_NOSLEEP();
4516 VNET_FOREACH(vnet_iter) {
4517 CURVNET_SET(vnet_iter);
4521 key_flush_spacq(now);
4524 VNET_LIST_RUNLOCK_NOSLEEP();
4526 #ifndef IPSEC_DEBUG2
4527 /* do exchange to tick time !! */
4528 (void)timeout((void *)key_timehandler, (void *)0, hz);
4529 #endif /* IPSEC_DEBUG2 */
4537 key_randomfill(&value, sizeof(value));
4542 key_randomfill(p, l)
4548 static int warn = 1;
4551 n = (size_t)read_random(p, (u_int)l);
4555 bcopy(&v, (u_int8_t *)p + n,
4556 l - n < sizeof(v) ? l - n : sizeof(v));
4560 printf("WARNING: pseudo-random number generator "
4561 "used for IPsec processing\n");
4568 * map SADB_SATYPE_* to IPPROTO_*.
4569 * if satype == SADB_SATYPE then satype is mapped to ~0.
4571 * 0: invalid satype.
4574 key_satype2proto(u_int8_t satype)
4577 case SADB_SATYPE_UNSPEC:
4578 return IPSEC_PROTO_ANY;
4579 case SADB_SATYPE_AH:
4581 case SADB_SATYPE_ESP:
4583 case SADB_X_SATYPE_IPCOMP:
4584 return IPPROTO_IPCOMP;
4585 case SADB_X_SATYPE_TCPSIGNATURE:
4594 * map IPPROTO_* to SADB_SATYPE_*
4596 * 0: invalid protocol type.
4599 key_proto2satype(u_int16_t proto)
4603 return SADB_SATYPE_AH;
4605 return SADB_SATYPE_ESP;
4606 case IPPROTO_IPCOMP:
4607 return SADB_X_SATYPE_IPCOMP;
4609 return SADB_X_SATYPE_TCPSIGNATURE;
4618 * SADB_GETSPI processing is to receive
4619 * <base, (SA2), src address, dst address, (SPI range)>
4620 * from the IKMPd, to assign a unique spi value, to hang on the INBOUND
4621 * tree with the status of LARVAL, and send
4622 * <base, SA(*), address(SD)>
4625 * IN: mhp: pointer to the pointer to each header.
4626 * OUT: NULL if fail.
4627 * other if success, return pointer to the message to send.
4630 key_getspi(so, m, mhp)
4633 const struct sadb_msghdr *mhp;
4635 struct sadb_address *src0, *dst0;
4636 struct secasindex saidx;
4637 struct secashead *newsah;
4638 struct secasvar *newsav;
4645 IPSEC_ASSERT(so != NULL, ("null socket"));
4646 IPSEC_ASSERT(m != NULL, ("null mbuf"));
4647 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
4648 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
4650 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
4651 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
4652 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
4654 return key_senderror(so, m, EINVAL);
4656 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
4657 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
4658 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
4660 return key_senderror(so, m, EINVAL);
4662 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
4663 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
4664 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
4666 mode = IPSEC_MODE_ANY;
4670 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
4671 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
4673 /* map satype to proto */
4674 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
4675 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
4677 return key_senderror(so, m, EINVAL);
4681 * Make sure the port numbers are zero.
4682 * In case of NAT-T we will update them later if needed.
4684 switch (((struct sockaddr *)(src0 + 1))->sa_family) {
4686 if (((struct sockaddr *)(src0 + 1))->sa_len !=
4687 sizeof(struct sockaddr_in))
4688 return key_senderror(so, m, EINVAL);
4689 ((struct sockaddr_in *)(src0 + 1))->sin_port = 0;
4692 if (((struct sockaddr *)(src0 + 1))->sa_len !=
4693 sizeof(struct sockaddr_in6))
4694 return key_senderror(so, m, EINVAL);
4695 ((struct sockaddr_in6 *)(src0 + 1))->sin6_port = 0;
4700 switch (((struct sockaddr *)(dst0 + 1))->sa_family) {
4702 if (((struct sockaddr *)(dst0 + 1))->sa_len !=
4703 sizeof(struct sockaddr_in))
4704 return key_senderror(so, m, EINVAL);
4705 ((struct sockaddr_in *)(dst0 + 1))->sin_port = 0;
4708 if (((struct sockaddr *)(dst0 + 1))->sa_len !=
4709 sizeof(struct sockaddr_in6))
4710 return key_senderror(so, m, EINVAL);
4711 ((struct sockaddr_in6 *)(dst0 + 1))->sin6_port = 0;
4717 /* XXX boundary check against sa_len */
4718 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
4722 * Handle NAT-T info if present.
4723 * We made sure the port numbers are zero above, so we do
4724 * not have to worry in case we do not update them.
4726 if (mhp->ext[SADB_X_EXT_NAT_T_OAI] != NULL)
4727 ipseclog((LOG_DEBUG, "%s: NAT-T OAi present\n", __func__));
4728 if (mhp->ext[SADB_X_EXT_NAT_T_OAR] != NULL)
4729 ipseclog((LOG_DEBUG, "%s: NAT-T OAr present\n", __func__));
4731 if (mhp->ext[SADB_X_EXT_NAT_T_TYPE] != NULL &&
4732 mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
4733 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
4734 struct sadb_x_nat_t_type *type;
4735 struct sadb_x_nat_t_port *sport, *dport;
4737 if (mhp->extlen[SADB_X_EXT_NAT_T_TYPE] < sizeof(*type) ||
4738 mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
4739 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
4740 ipseclog((LOG_DEBUG, "%s: invalid nat-t message "
4741 "passed.\n", __func__));
4742 return key_senderror(so, m, EINVAL);
4745 sport = (struct sadb_x_nat_t_port *)
4746 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
4747 dport = (struct sadb_x_nat_t_port *)
4748 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
4751 KEY_PORTTOSADDR(&saidx.src, sport->sadb_x_nat_t_port_port);
4753 KEY_PORTTOSADDR(&saidx.dst, dport->sadb_x_nat_t_port_port);
4757 /* SPI allocation */
4758 spi = key_do_getnewspi((struct sadb_spirange *)mhp->ext[SADB_EXT_SPIRANGE],
4761 return key_senderror(so, m, EINVAL);
4763 /* get a SA index */
4764 if ((newsah = key_getsah(&saidx)) == NULL) {
4765 /* create a new SA index */
4766 if ((newsah = key_newsah(&saidx)) == NULL) {
4767 ipseclog((LOG_DEBUG, "%s: No more memory.\n",__func__));
4768 return key_senderror(so, m, ENOBUFS);
4774 newsav = KEY_NEWSAV(m, mhp, newsah, &error);
4775 if (newsav == NULL) {
4776 /* XXX don't free new SA index allocated in above. */
4777 return key_senderror(so, m, error);
4781 newsav->spi = htonl(spi);
4783 /* delete the entry in acqtree */
4784 if (mhp->msg->sadb_msg_seq != 0) {
4786 if ((acq = key_getacqbyseq(mhp->msg->sadb_msg_seq)) != NULL) {
4787 /* reset counter in order to deletion by timehandler. */
4788 acq->created = time_second;
4794 struct mbuf *n, *nn;
4795 struct sadb_sa *m_sa;
4796 struct sadb_msg *newmsg;
4799 /* create new sadb_msg to reply. */
4800 len = PFKEY_ALIGN8(sizeof(struct sadb_msg)) +
4801 PFKEY_ALIGN8(sizeof(struct sadb_sa));
4803 MGETHDR(n, M_NOWAIT, MT_DATA);
4805 MCLGET(n, M_NOWAIT);
4806 if ((n->m_flags & M_EXT) == 0) {
4812 return key_senderror(so, m, ENOBUFS);
4818 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
4819 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
4821 m_sa = (struct sadb_sa *)(mtod(n, caddr_t) + off);
4822 m_sa->sadb_sa_len = PFKEY_UNIT64(sizeof(struct sadb_sa));
4823 m_sa->sadb_sa_exttype = SADB_EXT_SA;
4824 m_sa->sadb_sa_spi = htonl(spi);
4825 off += PFKEY_ALIGN8(sizeof(struct sadb_sa));
4827 IPSEC_ASSERT(off == len,
4828 ("length inconsistency (off %u len %u)", off, len));
4830 n->m_next = key_gather_mbuf(m, mhp, 0, 2, SADB_EXT_ADDRESS_SRC,
4831 SADB_EXT_ADDRESS_DST);
4834 return key_senderror(so, m, ENOBUFS);
4837 if (n->m_len < sizeof(struct sadb_msg)) {
4838 n = m_pullup(n, sizeof(struct sadb_msg));
4840 return key_sendup_mbuf(so, m, KEY_SENDUP_ONE);
4843 n->m_pkthdr.len = 0;
4844 for (nn = n; nn; nn = nn->m_next)
4845 n->m_pkthdr.len += nn->m_len;
4847 newmsg = mtod(n, struct sadb_msg *);
4848 newmsg->sadb_msg_seq = newsav->seq;
4849 newmsg->sadb_msg_errno = 0;
4850 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
4853 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
4858 * allocating new SPI
4859 * called by key_getspi().
4865 key_do_getnewspi(spirange, saidx)
4866 struct sadb_spirange *spirange;
4867 struct secasindex *saidx;
4871 int count = V_key_spi_trycnt;
4873 /* set spi range to allocate */
4874 if (spirange != NULL) {
4875 min = spirange->sadb_spirange_min;
4876 max = spirange->sadb_spirange_max;
4878 min = V_key_spi_minval;
4879 max = V_key_spi_maxval;
4881 /* IPCOMP needs 2-byte SPI */
4882 if (saidx->proto == IPPROTO_IPCOMP) {
4889 t = min; min = max; max = t;
4894 if (key_checkspidup(saidx, min) != NULL) {
4895 ipseclog((LOG_DEBUG, "%s: SPI %u exists already.\n",
4900 count--; /* taking one cost. */
4908 /* when requesting to allocate spi ranged */
4910 /* generate pseudo-random SPI value ranged. */
4911 newspi = min + (key_random() % (max - min + 1));
4913 if (key_checkspidup(saidx, newspi) == NULL)
4917 if (count == 0 || newspi == 0) {
4918 ipseclog((LOG_DEBUG, "%s: to allocate spi is failed.\n",
4925 keystat.getspi_count =
4926 (keystat.getspi_count + V_key_spi_trycnt - count) / 2;
4932 * SADB_UPDATE processing
4934 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4935 * key(AE), (identity(SD),) (sensitivity)>
4936 * from the ikmpd, and update a secasvar entry whose status is SADB_SASTATE_LARVAL.
4938 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4939 * (identity(SD),) (sensitivity)>
4942 * m will always be freed.
4945 key_update(so, m, mhp)
4948 const struct sadb_msghdr *mhp;
4950 struct sadb_sa *sa0;
4951 struct sadb_address *src0, *dst0;
4953 struct sadb_x_nat_t_type *type;
4954 struct sadb_x_nat_t_port *sport, *dport;
4955 struct sadb_address *iaddr, *raddr;
4956 struct sadb_x_nat_t_frag *frag;
4958 struct secasindex saidx;
4959 struct secashead *sah;
4960 struct secasvar *sav;
4966 IPSEC_ASSERT(so != NULL, ("null socket"));
4967 IPSEC_ASSERT(m != NULL, ("null mbuf"));
4968 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
4969 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
4971 /* map satype to proto */
4972 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
4973 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
4975 return key_senderror(so, m, EINVAL);
4978 if (mhp->ext[SADB_EXT_SA] == NULL ||
4979 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
4980 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
4981 (mhp->msg->sadb_msg_satype == SADB_SATYPE_ESP &&
4982 mhp->ext[SADB_EXT_KEY_ENCRYPT] == NULL) ||
4983 (mhp->msg->sadb_msg_satype == SADB_SATYPE_AH &&
4984 mhp->ext[SADB_EXT_KEY_AUTH] == NULL) ||
4985 (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL &&
4986 mhp->ext[SADB_EXT_LIFETIME_SOFT] == NULL) ||
4987 (mhp->ext[SADB_EXT_LIFETIME_HARD] == NULL &&
4988 mhp->ext[SADB_EXT_LIFETIME_SOFT] != NULL)) {
4989 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
4991 return key_senderror(so, m, EINVAL);
4993 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
4994 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
4995 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
4996 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
4998 return key_senderror(so, m, EINVAL);
5000 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
5001 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
5002 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
5004 mode = IPSEC_MODE_ANY;
5007 /* XXX boundary checking for other extensions */
5009 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5010 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
5011 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
5013 /* XXX boundary check against sa_len */
5014 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
5017 * Make sure the port numbers are zero.
5018 * In case of NAT-T we will update them later if needed.
5020 KEY_PORTTOSADDR(&saidx.src, 0);
5021 KEY_PORTTOSADDR(&saidx.dst, 0);
5025 * Handle NAT-T info if present.
5027 if (mhp->ext[SADB_X_EXT_NAT_T_TYPE] != NULL &&
5028 mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
5029 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
5031 if (mhp->extlen[SADB_X_EXT_NAT_T_TYPE] < sizeof(*type) ||
5032 mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
5033 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
5034 ipseclog((LOG_DEBUG, "%s: invalid message.\n",
5036 return key_senderror(so, m, EINVAL);
5039 type = (struct sadb_x_nat_t_type *)
5040 mhp->ext[SADB_X_EXT_NAT_T_TYPE];
5041 sport = (struct sadb_x_nat_t_port *)
5042 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
5043 dport = (struct sadb_x_nat_t_port *)
5044 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
5049 if (mhp->ext[SADB_X_EXT_NAT_T_OAI] != NULL &&
5050 mhp->ext[SADB_X_EXT_NAT_T_OAR] != NULL) {
5051 if (mhp->extlen[SADB_X_EXT_NAT_T_OAI] < sizeof(*iaddr) ||
5052 mhp->extlen[SADB_X_EXT_NAT_T_OAR] < sizeof(*raddr)) {
5053 ipseclog((LOG_DEBUG, "%s: invalid message\n",
5055 return key_senderror(so, m, EINVAL);
5057 iaddr = (struct sadb_address *)mhp->ext[SADB_X_EXT_NAT_T_OAI];
5058 raddr = (struct sadb_address *)mhp->ext[SADB_X_EXT_NAT_T_OAR];
5059 ipseclog((LOG_DEBUG, "%s: NAT-T OAi/r present\n", __func__));
5061 iaddr = raddr = NULL;
5063 if (mhp->ext[SADB_X_EXT_NAT_T_FRAG] != NULL) {
5064 if (mhp->extlen[SADB_X_EXT_NAT_T_FRAG] < sizeof(*frag)) {
5065 ipseclog((LOG_DEBUG, "%s: invalid message\n",
5067 return key_senderror(so, m, EINVAL);
5069 frag = (struct sadb_x_nat_t_frag *)
5070 mhp->ext[SADB_X_EXT_NAT_T_FRAG];
5076 /* get a SA header */
5077 if ((sah = key_getsah(&saidx)) == NULL) {
5078 ipseclog((LOG_DEBUG, "%s: no SA index found.\n", __func__));
5079 return key_senderror(so, m, ENOENT);
5082 /* set spidx if there */
5084 error = key_setident(sah, m, mhp);
5086 return key_senderror(so, m, error);
5088 /* find a SA with sequence number. */
5089 #ifdef IPSEC_DOSEQCHECK
5090 if (mhp->msg->sadb_msg_seq != 0
5091 && (sav = key_getsavbyseq(sah, mhp->msg->sadb_msg_seq)) == NULL) {
5092 ipseclog((LOG_DEBUG, "%s: no larval SA with sequence %u "
5093 "exists.\n", __func__, mhp->msg->sadb_msg_seq));
5094 return key_senderror(so, m, ENOENT);
5098 sav = key_getsavbyspi(sah, sa0->sadb_sa_spi);
5101 ipseclog((LOG_DEBUG, "%s: no such a SA found (spi:%u)\n",
5102 __func__, (u_int32_t)ntohl(sa0->sadb_sa_spi)));
5103 return key_senderror(so, m, EINVAL);
5107 /* validity check */
5108 if (sav->sah->saidx.proto != proto) {
5109 ipseclog((LOG_DEBUG, "%s: protocol mismatched "
5110 "(DB=%u param=%u)\n", __func__,
5111 sav->sah->saidx.proto, proto));
5112 return key_senderror(so, m, EINVAL);
5114 #ifdef IPSEC_DOSEQCHECK
5115 if (sav->spi != sa0->sadb_sa_spi) {
5116 ipseclog((LOG_DEBUG, "%s: SPI mismatched (DB:%u param:%u)\n",
5118 (u_int32_t)ntohl(sav->spi),
5119 (u_int32_t)ntohl(sa0->sadb_sa_spi)));
5120 return key_senderror(so, m, EINVAL);
5123 if (sav->pid != mhp->msg->sadb_msg_pid) {
5124 ipseclog((LOG_DEBUG, "%s: pid mismatched (DB:%u param:%u)\n",
5125 __func__, sav->pid, mhp->msg->sadb_msg_pid));
5126 return key_senderror(so, m, EINVAL);
5129 /* copy sav values */
5130 error = key_setsaval(sav, m, mhp);
5133 return key_senderror(so, m, error);
5138 * Handle more NAT-T info if present,
5139 * now that we have a sav to fill.
5142 sav->natt_type = type->sadb_x_nat_t_type_type;
5145 KEY_PORTTOSADDR(&sav->sah->saidx.src,
5146 sport->sadb_x_nat_t_port_port);
5148 KEY_PORTTOSADDR(&sav->sah->saidx.dst,
5149 dport->sadb_x_nat_t_port_port);
5153 * In case SADB_X_EXT_NAT_T_FRAG was not given, leave it at 0.
5154 * We should actually check for a minimum MTU here, if we
5155 * want to support it in ip_output.
5158 sav->natt_esp_frag_len = frag->sadb_x_nat_t_frag_fraglen;
5162 /* check SA values to be mature. */
5163 if ((mhp->msg->sadb_msg_errno = key_mature(sav)) != 0) {
5165 return key_senderror(so, m, 0);
5171 /* set msg buf from mhp */
5172 n = key_getmsgbuf_x1(m, mhp);
5174 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
5175 return key_senderror(so, m, ENOBUFS);
5179 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5184 * search SAD with sequence for a SA which state is SADB_SASTATE_LARVAL.
5185 * only called by key_update().
5188 * others : found, pointer to a SA.
5190 #ifdef IPSEC_DOSEQCHECK
5191 static struct secasvar *
5192 key_getsavbyseq(sah, seq)
5193 struct secashead *sah;
5196 struct secasvar *sav;
5199 state = SADB_SASTATE_LARVAL;
5201 /* search SAD with sequence number ? */
5202 LIST_FOREACH(sav, &sah->savtree[state], chain) {
5204 KEY_CHKSASTATE(state, sav->state, __func__);
5206 if (sav->seq == seq) {
5208 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
5209 printf("DP %s cause refcnt++:%d SA:%p\n",
5210 __func__, sav->refcnt, sav));
5220 * SADB_ADD processing
5221 * add an entry to SA database, when received
5222 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
5223 * key(AE), (identity(SD),) (sensitivity)>
5226 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
5227 * (identity(SD),) (sensitivity)>
5230 * IGNORE identity and sensitivity messages.
5232 * m will always be freed.
5238 const struct sadb_msghdr *mhp;
5240 struct sadb_sa *sa0;
5241 struct sadb_address *src0, *dst0;
5243 struct sadb_x_nat_t_type *type;
5244 struct sadb_address *iaddr, *raddr;
5245 struct sadb_x_nat_t_frag *frag;
5247 struct secasindex saidx;
5248 struct secashead *newsah;
5249 struct secasvar *newsav;
5255 IPSEC_ASSERT(so != NULL, ("null socket"));
5256 IPSEC_ASSERT(m != NULL, ("null mbuf"));
5257 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5258 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5260 /* map satype to proto */
5261 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5262 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
5264 return key_senderror(so, m, EINVAL);
5267 if (mhp->ext[SADB_EXT_SA] == NULL ||
5268 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5269 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
5270 (mhp->msg->sadb_msg_satype == SADB_SATYPE_ESP &&
5271 mhp->ext[SADB_EXT_KEY_ENCRYPT] == NULL) ||
5272 (mhp->msg->sadb_msg_satype == SADB_SATYPE_AH &&
5273 mhp->ext[SADB_EXT_KEY_AUTH] == NULL) ||
5274 (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL &&
5275 mhp->ext[SADB_EXT_LIFETIME_SOFT] == NULL) ||
5276 (mhp->ext[SADB_EXT_LIFETIME_HARD] == NULL &&
5277 mhp->ext[SADB_EXT_LIFETIME_SOFT] != NULL)) {
5278 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5280 return key_senderror(so, m, EINVAL);
5282 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
5283 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5284 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5286 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5288 return key_senderror(so, m, EINVAL);
5290 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
5291 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
5292 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
5294 mode = IPSEC_MODE_ANY;
5298 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5299 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
5300 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
5302 /* XXX boundary check against sa_len */
5303 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
5306 * Make sure the port numbers are zero.
5307 * In case of NAT-T we will update them later if needed.
5309 KEY_PORTTOSADDR(&saidx.src, 0);
5310 KEY_PORTTOSADDR(&saidx.dst, 0);
5314 * Handle NAT-T info if present.
5316 if (mhp->ext[SADB_X_EXT_NAT_T_TYPE] != NULL &&
5317 mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
5318 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
5319 struct sadb_x_nat_t_port *sport, *dport;
5321 if (mhp->extlen[SADB_X_EXT_NAT_T_TYPE] < sizeof(*type) ||
5322 mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
5323 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
5324 ipseclog((LOG_DEBUG, "%s: invalid message.\n",
5326 return key_senderror(so, m, EINVAL);
5329 type = (struct sadb_x_nat_t_type *)
5330 mhp->ext[SADB_X_EXT_NAT_T_TYPE];
5331 sport = (struct sadb_x_nat_t_port *)
5332 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
5333 dport = (struct sadb_x_nat_t_port *)
5334 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
5337 KEY_PORTTOSADDR(&saidx.src,
5338 sport->sadb_x_nat_t_port_port);
5340 KEY_PORTTOSADDR(&saidx.dst,
5341 dport->sadb_x_nat_t_port_port);
5345 if (mhp->ext[SADB_X_EXT_NAT_T_OAI] != NULL &&
5346 mhp->ext[SADB_X_EXT_NAT_T_OAR] != NULL) {
5347 if (mhp->extlen[SADB_X_EXT_NAT_T_OAI] < sizeof(*iaddr) ||
5348 mhp->extlen[SADB_X_EXT_NAT_T_OAR] < sizeof(*raddr)) {
5349 ipseclog((LOG_DEBUG, "%s: invalid message\n",
5351 return key_senderror(so, m, EINVAL);
5353 iaddr = (struct sadb_address *)mhp->ext[SADB_X_EXT_NAT_T_OAI];
5354 raddr = (struct sadb_address *)mhp->ext[SADB_X_EXT_NAT_T_OAR];
5355 ipseclog((LOG_DEBUG, "%s: NAT-T OAi/r present\n", __func__));
5357 iaddr = raddr = NULL;
5359 if (mhp->ext[SADB_X_EXT_NAT_T_FRAG] != NULL) {
5360 if (mhp->extlen[SADB_X_EXT_NAT_T_FRAG] < sizeof(*frag)) {
5361 ipseclog((LOG_DEBUG, "%s: invalid message\n",
5363 return key_senderror(so, m, EINVAL);
5365 frag = (struct sadb_x_nat_t_frag *)
5366 mhp->ext[SADB_X_EXT_NAT_T_FRAG];
5372 /* get a SA header */
5373 if ((newsah = key_getsah(&saidx)) == NULL) {
5374 /* create a new SA header */
5375 if ((newsah = key_newsah(&saidx)) == NULL) {
5376 ipseclog((LOG_DEBUG, "%s: No more memory.\n",__func__));
5377 return key_senderror(so, m, ENOBUFS);
5381 /* set spidx if there */
5383 error = key_setident(newsah, m, mhp);
5385 return key_senderror(so, m, error);
5388 /* create new SA entry. */
5389 /* We can create new SA only if SPI is differenct. */
5391 newsav = key_getsavbyspi(newsah, sa0->sadb_sa_spi);
5393 if (newsav != NULL) {
5394 ipseclog((LOG_DEBUG, "%s: SA already exists.\n", __func__));
5395 return key_senderror(so, m, EEXIST);
5397 newsav = KEY_NEWSAV(m, mhp, newsah, &error);
5398 if (newsav == NULL) {
5399 return key_senderror(so, m, error);
5404 * Handle more NAT-T info if present,
5405 * now that we have a sav to fill.
5408 newsav->natt_type = type->sadb_x_nat_t_type_type;
5412 * In case SADB_X_EXT_NAT_T_FRAG was not given, leave it at 0.
5413 * We should actually check for a minimum MTU here, if we
5414 * want to support it in ip_output.
5417 newsav->natt_esp_frag_len = frag->sadb_x_nat_t_frag_fraglen;
5421 /* check SA values to be mature. */
5422 if ((error = key_mature(newsav)) != 0) {
5423 KEY_FREESAV(&newsav);
5424 return key_senderror(so, m, error);
5428 * don't call key_freesav() here, as we would like to keep the SA
5429 * in the database on success.
5435 /* set msg buf from mhp */
5436 n = key_getmsgbuf_x1(m, mhp);
5438 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
5439 return key_senderror(so, m, ENOBUFS);
5443 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5449 key_setident(sah, m, mhp)
5450 struct secashead *sah;
5452 const struct sadb_msghdr *mhp;
5454 const struct sadb_ident *idsrc, *iddst;
5455 int idsrclen, iddstlen;
5457 IPSEC_ASSERT(sah != NULL, ("null secashead"));
5458 IPSEC_ASSERT(m != NULL, ("null mbuf"));
5459 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5460 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5462 /* don't make buffer if not there */
5463 if (mhp->ext[SADB_EXT_IDENTITY_SRC] == NULL &&
5464 mhp->ext[SADB_EXT_IDENTITY_DST] == NULL) {
5470 if (mhp->ext[SADB_EXT_IDENTITY_SRC] == NULL ||
5471 mhp->ext[SADB_EXT_IDENTITY_DST] == NULL) {
5472 ipseclog((LOG_DEBUG, "%s: invalid identity.\n", __func__));
5476 idsrc = (const struct sadb_ident *)mhp->ext[SADB_EXT_IDENTITY_SRC];
5477 iddst = (const struct sadb_ident *)mhp->ext[SADB_EXT_IDENTITY_DST];
5478 idsrclen = mhp->extlen[SADB_EXT_IDENTITY_SRC];
5479 iddstlen = mhp->extlen[SADB_EXT_IDENTITY_DST];
5481 /* validity check */
5482 if (idsrc->sadb_ident_type != iddst->sadb_ident_type) {
5483 ipseclog((LOG_DEBUG, "%s: ident type mismatch.\n", __func__));
5487 switch (idsrc->sadb_ident_type) {
5488 case SADB_IDENTTYPE_PREFIX:
5489 case SADB_IDENTTYPE_FQDN:
5490 case SADB_IDENTTYPE_USERFQDN:
5492 /* XXX do nothing */
5498 /* make structure */
5499 sah->idents = malloc(sizeof(struct secident), M_IPSEC_MISC, M_NOWAIT);
5500 if (sah->idents == NULL) {
5501 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
5504 sah->identd = malloc(sizeof(struct secident), M_IPSEC_MISC, M_NOWAIT);
5505 if (sah->identd == NULL) {
5506 free(sah->idents, M_IPSEC_MISC);
5508 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
5511 sah->idents->type = idsrc->sadb_ident_type;
5512 sah->idents->id = idsrc->sadb_ident_id;
5514 sah->identd->type = iddst->sadb_ident_type;
5515 sah->identd->id = iddst->sadb_ident_id;
5521 * m will not be freed on return.
5522 * it is caller's responsibility to free the result.
5524 static struct mbuf *
5525 key_getmsgbuf_x1(m, mhp)
5527 const struct sadb_msghdr *mhp;
5531 IPSEC_ASSERT(m != NULL, ("null mbuf"));
5532 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5533 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5535 /* create new sadb_msg to reply. */
5536 n = key_gather_mbuf(m, mhp, 1, 9, SADB_EXT_RESERVED,
5537 SADB_EXT_SA, SADB_X_EXT_SA2,
5538 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST,
5539 SADB_EXT_LIFETIME_HARD, SADB_EXT_LIFETIME_SOFT,
5540 SADB_EXT_IDENTITY_SRC, SADB_EXT_IDENTITY_DST);
5544 if (n->m_len < sizeof(struct sadb_msg)) {
5545 n = m_pullup(n, sizeof(struct sadb_msg));
5549 mtod(n, struct sadb_msg *)->sadb_msg_errno = 0;
5550 mtod(n, struct sadb_msg *)->sadb_msg_len =
5551 PFKEY_UNIT64(n->m_pkthdr.len);
5556 static int key_delete_all __P((struct socket *, struct mbuf *,
5557 const struct sadb_msghdr *, u_int16_t));
5560 * SADB_DELETE processing
5562 * <base, SA(*), address(SD)>
5563 * from the ikmpd, and set SADB_SASTATE_DEAD,
5565 * <base, SA(*), address(SD)>
5568 * m will always be freed.
5571 key_delete(so, m, mhp)
5574 const struct sadb_msghdr *mhp;
5576 struct sadb_sa *sa0;
5577 struct sadb_address *src0, *dst0;
5578 struct secasindex saidx;
5579 struct secashead *sah;
5580 struct secasvar *sav = NULL;
5583 IPSEC_ASSERT(so != NULL, ("null socket"));
5584 IPSEC_ASSERT(m != NULL, ("null mbuf"));
5585 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5586 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5588 /* map satype to proto */
5589 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5590 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
5592 return key_senderror(so, m, EINVAL);
5595 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5596 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
5597 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5599 return key_senderror(so, m, EINVAL);
5602 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5603 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5604 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5606 return key_senderror(so, m, EINVAL);
5609 if (mhp->ext[SADB_EXT_SA] == NULL) {
5611 * Caller wants us to delete all non-LARVAL SAs
5612 * that match the src/dst. This is used during
5613 * IKE INITIAL-CONTACT.
5615 ipseclog((LOG_DEBUG, "%s: doing delete all.\n", __func__));
5616 return key_delete_all(so, m, mhp, proto);
5617 } else if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa)) {
5618 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5620 return key_senderror(so, m, EINVAL);
5623 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5624 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
5625 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
5627 /* XXX boundary check against sa_len */
5628 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5631 * Make sure the port numbers are zero.
5632 * In case of NAT-T we will update them later if needed.
5634 KEY_PORTTOSADDR(&saidx.src, 0);
5635 KEY_PORTTOSADDR(&saidx.dst, 0);
5639 * Handle NAT-T info if present.
5641 if (mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
5642 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
5643 struct sadb_x_nat_t_port *sport, *dport;
5645 if (mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
5646 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
5647 ipseclog((LOG_DEBUG, "%s: invalid message.\n",
5649 return key_senderror(so, m, EINVAL);
5652 sport = (struct sadb_x_nat_t_port *)
5653 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
5654 dport = (struct sadb_x_nat_t_port *)
5655 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
5658 KEY_PORTTOSADDR(&saidx.src,
5659 sport->sadb_x_nat_t_port_port);
5661 KEY_PORTTOSADDR(&saidx.dst,
5662 dport->sadb_x_nat_t_port_port);
5666 /* get a SA header */
5668 LIST_FOREACH(sah, &V_sahtree, chain) {
5669 if (sah->state == SADB_SASTATE_DEAD)
5671 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5674 /* get a SA with SPI. */
5675 sav = key_getsavbyspi(sah, sa0->sadb_sa_spi);
5681 ipseclog((LOG_DEBUG, "%s: no SA found.\n", __func__));
5682 return key_senderror(so, m, ENOENT);
5685 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
5691 struct sadb_msg *newmsg;
5693 /* create new sadb_msg to reply. */
5694 /* XXX-BZ NAT-T extensions? */
5695 n = key_gather_mbuf(m, mhp, 1, 4, SADB_EXT_RESERVED,
5696 SADB_EXT_SA, SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
5698 return key_senderror(so, m, ENOBUFS);
5700 if (n->m_len < sizeof(struct sadb_msg)) {
5701 n = m_pullup(n, sizeof(struct sadb_msg));
5703 return key_senderror(so, m, ENOBUFS);
5705 newmsg = mtod(n, struct sadb_msg *);
5706 newmsg->sadb_msg_errno = 0;
5707 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
5710 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5715 * delete all SAs for src/dst. Called from key_delete().
5718 key_delete_all(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp,
5721 struct sadb_address *src0, *dst0;
5722 struct secasindex saidx;
5723 struct secashead *sah;
5724 struct secasvar *sav, *nextsav;
5725 u_int stateidx, state;
5727 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
5728 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
5730 /* XXX boundary check against sa_len */
5731 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5734 * Make sure the port numbers are zero.
5735 * In case of NAT-T we will update them later if needed.
5737 KEY_PORTTOSADDR(&saidx.src, 0);
5738 KEY_PORTTOSADDR(&saidx.dst, 0);
5742 * Handle NAT-T info if present.
5745 if (mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
5746 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
5747 struct sadb_x_nat_t_port *sport, *dport;
5749 if (mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
5750 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
5751 ipseclog((LOG_DEBUG, "%s: invalid message.\n",
5753 return key_senderror(so, m, EINVAL);
5756 sport = (struct sadb_x_nat_t_port *)
5757 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
5758 dport = (struct sadb_x_nat_t_port *)
5759 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
5762 KEY_PORTTOSADDR(&saidx.src,
5763 sport->sadb_x_nat_t_port_port);
5765 KEY_PORTTOSADDR(&saidx.dst,
5766 dport->sadb_x_nat_t_port_port);
5771 LIST_FOREACH(sah, &V_sahtree, chain) {
5772 if (sah->state == SADB_SASTATE_DEAD)
5774 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5777 /* Delete all non-LARVAL SAs. */
5779 stateidx < _ARRAYLEN(saorder_state_alive);
5781 state = saorder_state_alive[stateidx];
5782 if (state == SADB_SASTATE_LARVAL)
5784 for (sav = LIST_FIRST(&sah->savtree[state]);
5785 sav != NULL; sav = nextsav) {
5786 nextsav = LIST_NEXT(sav, chain);
5788 if (sav->state != state) {
5789 ipseclog((LOG_DEBUG, "%s: invalid "
5790 "sav->state (queue %d SA %d)\n",
5791 __func__, state, sav->state));
5795 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
5803 struct sadb_msg *newmsg;
5805 /* create new sadb_msg to reply. */
5806 /* XXX-BZ NAT-T extensions? */
5807 n = key_gather_mbuf(m, mhp, 1, 3, SADB_EXT_RESERVED,
5808 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
5810 return key_senderror(so, m, ENOBUFS);
5812 if (n->m_len < sizeof(struct sadb_msg)) {
5813 n = m_pullup(n, sizeof(struct sadb_msg));
5815 return key_senderror(so, m, ENOBUFS);
5817 newmsg = mtod(n, struct sadb_msg *);
5818 newmsg->sadb_msg_errno = 0;
5819 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
5822 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5827 * SADB_GET processing
5829 * <base, SA(*), address(SD)>
5830 * from the ikmpd, and get a SP and a SA to respond,
5832 * <base, SA, (lifetime(HSC),) address(SD), (address(P),) key(AE),
5833 * (identity(SD),) (sensitivity)>
5836 * m will always be freed.
5842 const struct sadb_msghdr *mhp;
5844 struct sadb_sa *sa0;
5845 struct sadb_address *src0, *dst0;
5846 struct secasindex saidx;
5847 struct secashead *sah;
5848 struct secasvar *sav = NULL;
5851 IPSEC_ASSERT(so != NULL, ("null socket"));
5852 IPSEC_ASSERT(m != NULL, ("null mbuf"));
5853 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5854 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5856 /* map satype to proto */
5857 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5858 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
5860 return key_senderror(so, m, EINVAL);
5863 if (mhp->ext[SADB_EXT_SA] == NULL ||
5864 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5865 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
5866 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5868 return key_senderror(so, m, EINVAL);
5870 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
5871 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5872 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5873 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5875 return key_senderror(so, m, EINVAL);
5878 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5879 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
5880 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
5882 /* XXX boundary check against sa_len */
5883 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5886 * Make sure the port numbers are zero.
5887 * In case of NAT-T we will update them later if needed.
5889 KEY_PORTTOSADDR(&saidx.src, 0);
5890 KEY_PORTTOSADDR(&saidx.dst, 0);
5894 * Handle NAT-T info if present.
5897 if (mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
5898 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
5899 struct sadb_x_nat_t_port *sport, *dport;
5901 if (mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
5902 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
5903 ipseclog((LOG_DEBUG, "%s: invalid message.\n",
5905 return key_senderror(so, m, EINVAL);
5908 sport = (struct sadb_x_nat_t_port *)
5909 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
5910 dport = (struct sadb_x_nat_t_port *)
5911 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
5914 KEY_PORTTOSADDR(&saidx.src,
5915 sport->sadb_x_nat_t_port_port);
5917 KEY_PORTTOSADDR(&saidx.dst,
5918 dport->sadb_x_nat_t_port_port);
5922 /* get a SA header */
5924 LIST_FOREACH(sah, &V_sahtree, chain) {
5925 if (sah->state == SADB_SASTATE_DEAD)
5927 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5930 /* get a SA with SPI. */
5931 sav = key_getsavbyspi(sah, sa0->sadb_sa_spi);
5937 ipseclog((LOG_DEBUG, "%s: no SA found.\n", __func__));
5938 return key_senderror(so, m, ENOENT);
5945 /* map proto to satype */
5946 if ((satype = key_proto2satype(sah->saidx.proto)) == 0) {
5947 ipseclog((LOG_DEBUG, "%s: there was invalid proto in SAD.\n",
5949 return key_senderror(so, m, EINVAL);
5952 /* create new sadb_msg to reply. */
5953 n = key_setdumpsa(sav, SADB_GET, satype, mhp->msg->sadb_msg_seq,
5954 mhp->msg->sadb_msg_pid);
5956 return key_senderror(so, m, ENOBUFS);
5959 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
5963 /* XXX make it sysctl-configurable? */
5965 key_getcomb_setlifetime(comb)
5966 struct sadb_comb *comb;
5969 comb->sadb_comb_soft_allocations = 1;
5970 comb->sadb_comb_hard_allocations = 1;
5971 comb->sadb_comb_soft_bytes = 0;
5972 comb->sadb_comb_hard_bytes = 0;
5973 comb->sadb_comb_hard_addtime = 86400; /* 1 day */
5974 comb->sadb_comb_soft_addtime = comb->sadb_comb_soft_addtime * 80 / 100;
5975 comb->sadb_comb_soft_usetime = 28800; /* 8 hours */
5976 comb->sadb_comb_hard_usetime = comb->sadb_comb_hard_usetime * 80 / 100;
5980 * XXX reorder combinations by preference
5981 * XXX no idea if the user wants ESP authentication or not
5983 static struct mbuf *
5986 struct sadb_comb *comb;
5987 struct enc_xform *algo;
5988 struct mbuf *result = NULL, *m, *n;
5992 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
5995 for (i = 1; i <= SADB_EALG_MAX; i++) {
5996 algo = esp_algorithm_lookup(i);
6000 /* discard algorithms with key size smaller than system min */
6001 if (_BITS(algo->maxkey) < V_ipsec_esp_keymin)
6003 if (_BITS(algo->minkey) < V_ipsec_esp_keymin)
6004 encmin = V_ipsec_esp_keymin;
6006 encmin = _BITS(algo->minkey);
6008 if (V_ipsec_esp_auth)
6009 m = key_getcomb_ah();
6011 IPSEC_ASSERT(l <= MLEN,
6012 ("l=%u > MLEN=%lu", l, (u_long) MLEN));
6013 MGET(m, M_NOWAIT, MT_DATA);
6018 bzero(mtod(m, caddr_t), m->m_len);
6025 for (n = m; n; n = n->m_next)
6027 IPSEC_ASSERT((totlen % l) == 0, ("totlen=%u, l=%u", totlen, l));
6029 for (off = 0; off < totlen; off += l) {
6030 n = m_pulldown(m, off, l, &o);
6032 /* m is already freed */
6035 comb = (struct sadb_comb *)(mtod(n, caddr_t) + o);
6036 bzero(comb, sizeof(*comb));
6037 key_getcomb_setlifetime(comb);
6038 comb->sadb_comb_encrypt = i;
6039 comb->sadb_comb_encrypt_minbits = encmin;
6040 comb->sadb_comb_encrypt_maxbits = _BITS(algo->maxkey);
6059 const struct auth_hash *ah,
6065 *min = *max = ah->keysize;
6066 if (ah->keysize == 0) {
6068 * Transform takes arbitrary key size but algorithm
6069 * key size is restricted. Enforce this here.
6072 case SADB_X_AALG_MD5: *min = *max = 16; break;
6073 case SADB_X_AALG_SHA: *min = *max = 20; break;
6074 case SADB_X_AALG_NULL: *min = 1; *max = 256; break;
6075 case SADB_X_AALG_SHA2_256: *min = *max = 32; break;
6076 case SADB_X_AALG_SHA2_384: *min = *max = 48; break;
6077 case SADB_X_AALG_SHA2_512: *min = *max = 64; break;
6079 DPRINTF(("%s: unknown AH algorithm %u\n",
6087 * XXX reorder combinations by preference
6089 static struct mbuf *
6092 struct sadb_comb *comb;
6093 struct auth_hash *algo;
6095 u_int16_t minkeysize, maxkeysize;
6097 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
6100 for (i = 1; i <= SADB_AALG_MAX; i++) {
6102 /* we prefer HMAC algorithms, not old algorithms */
6103 if (i != SADB_AALG_SHA1HMAC &&
6104 i != SADB_AALG_MD5HMAC &&
6105 i != SADB_X_AALG_SHA2_256 &&
6106 i != SADB_X_AALG_SHA2_384 &&
6107 i != SADB_X_AALG_SHA2_512)
6110 algo = ah_algorithm_lookup(i);
6113 key_getsizes_ah(algo, i, &minkeysize, &maxkeysize);
6114 /* discard algorithms with key size smaller than system min */
6115 if (_BITS(minkeysize) < V_ipsec_ah_keymin)
6119 IPSEC_ASSERT(l <= MLEN,
6120 ("l=%u > MLEN=%lu", l, (u_long) MLEN));
6121 MGET(m, M_NOWAIT, MT_DATA);
6128 M_PREPEND(m, l, M_NOWAIT);
6132 comb = mtod(m, struct sadb_comb *);
6133 bzero(comb, sizeof(*comb));
6134 key_getcomb_setlifetime(comb);
6135 comb->sadb_comb_auth = i;
6136 comb->sadb_comb_auth_minbits = _BITS(minkeysize);
6137 comb->sadb_comb_auth_maxbits = _BITS(maxkeysize);
6144 * not really an official behavior. discussed in pf_key@inner.net in Sep2000.
6145 * XXX reorder combinations by preference
6147 static struct mbuf *
6148 key_getcomb_ipcomp()
6150 struct sadb_comb *comb;
6151 struct comp_algo *algo;
6154 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
6157 for (i = 1; i <= SADB_X_CALG_MAX; i++) {
6158 algo = ipcomp_algorithm_lookup(i);
6163 IPSEC_ASSERT(l <= MLEN,
6164 ("l=%u > MLEN=%lu", l, (u_long) MLEN));
6165 MGET(m, M_NOWAIT, MT_DATA);
6172 M_PREPEND(m, l, M_NOWAIT);
6176 comb = mtod(m, struct sadb_comb *);
6177 bzero(comb, sizeof(*comb));
6178 key_getcomb_setlifetime(comb);
6179 comb->sadb_comb_encrypt = i;
6180 /* what should we set into sadb_comb_*_{min,max}bits? */
6187 * XXX no way to pass mode (transport/tunnel) to userland
6188 * XXX replay checking?
6189 * XXX sysctl interface to ipsec_{ah,esp}_keymin
6191 static struct mbuf *
6193 const struct secasindex *saidx;
6195 struct sadb_prop *prop;
6197 const int l = PFKEY_ALIGN8(sizeof(struct sadb_prop));
6200 switch (saidx->proto) {
6202 m = key_getcomb_esp();
6205 m = key_getcomb_ah();
6207 case IPPROTO_IPCOMP:
6208 m = key_getcomb_ipcomp();
6216 M_PREPEND(m, l, M_NOWAIT);
6221 for (n = m; n; n = n->m_next)
6224 prop = mtod(m, struct sadb_prop *);
6225 bzero(prop, sizeof(*prop));
6226 prop->sadb_prop_len = PFKEY_UNIT64(totlen);
6227 prop->sadb_prop_exttype = SADB_EXT_PROPOSAL;
6228 prop->sadb_prop_replay = 32; /* XXX */
6234 * SADB_ACQUIRE processing called by key_checkrequest() and key_acquire2().
6236 * <base, SA, address(SD), (address(P)), x_policy,
6237 * (identity(SD),) (sensitivity,) proposal>
6238 * to KMD, and expect to receive
6239 * <base> with SADB_ACQUIRE if error occured,
6241 * <base, src address, dst address, (SPI range)> with SADB_GETSPI
6242 * from KMD by PF_KEY.
6244 * XXX x_policy is outside of RFC2367 (KAME extension).
6245 * XXX sensitivity is not supported.
6246 * XXX for ipcomp, RFC2367 does not define how to fill in proposal.
6247 * see comment for key_getcomb_ipcomp().
6251 * others: error number
6254 key_acquire(const struct secasindex *saidx, struct secpolicy *sp)
6256 struct mbuf *result = NULL, *m;
6257 struct secacq *newacq;
6262 IPSEC_ASSERT(saidx != NULL, ("null saidx"));
6263 satype = key_proto2satype(saidx->proto);
6264 IPSEC_ASSERT(satype != 0, ("null satype, protocol %u", saidx->proto));
6267 * We never do anything about acquirng SA. There is anather
6268 * solution that kernel blocks to send SADB_ACQUIRE message until
6269 * getting something message from IKEd. In later case, to be
6270 * managed with ACQUIRING list.
6272 /* Get an entry to check whether sending message or not. */
6273 if ((newacq = key_getacq(saidx)) != NULL) {
6274 if (V_key_blockacq_count < newacq->count) {
6275 /* reset counter and do send message. */
6278 /* increment counter and do nothing. */
6283 /* make new entry for blocking to send SADB_ACQUIRE. */
6284 if ((newacq = key_newacq(saidx)) == NULL)
6290 m = key_setsadbmsg(SADB_ACQUIRE, 0, satype, seq, 0, 0);
6298 * No SADB_X_EXT_NAT_T_* here: we do not know
6299 * anything related to NAT-T at this time.
6302 /* set sadb_address for saidx's. */
6303 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
6304 &saidx->src.sa, FULLMASK, IPSEC_ULPROTO_ANY);
6311 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
6312 &saidx->dst.sa, FULLMASK, IPSEC_ULPROTO_ANY);
6319 /* XXX proxy address (optional) */
6321 /* set sadb_x_policy */
6323 m = key_setsadbxpolicy(sp->policy, sp->spidx.dir, sp->id);
6331 /* XXX identity (optional) */
6333 if (idexttype && fqdn) {
6334 /* create identity extension (FQDN) */
6335 struct sadb_ident *id;
6338 fqdnlen = strlen(fqdn) + 1; /* +1 for terminating-NUL */
6339 id = (struct sadb_ident *)p;
6340 bzero(id, sizeof(*id) + PFKEY_ALIGN8(fqdnlen));
6341 id->sadb_ident_len = PFKEY_UNIT64(sizeof(*id) + PFKEY_ALIGN8(fqdnlen));
6342 id->sadb_ident_exttype = idexttype;
6343 id->sadb_ident_type = SADB_IDENTTYPE_FQDN;
6344 bcopy(fqdn, id + 1, fqdnlen);
6345 p += sizeof(struct sadb_ident) + PFKEY_ALIGN8(fqdnlen);
6349 /* create identity extension (USERFQDN) */
6350 struct sadb_ident *id;
6354 /* +1 for terminating-NUL */
6355 userfqdnlen = strlen(userfqdn) + 1;
6358 id = (struct sadb_ident *)p;
6359 bzero(id, sizeof(*id) + PFKEY_ALIGN8(userfqdnlen));
6360 id->sadb_ident_len = PFKEY_UNIT64(sizeof(*id) + PFKEY_ALIGN8(userfqdnlen));
6361 id->sadb_ident_exttype = idexttype;
6362 id->sadb_ident_type = SADB_IDENTTYPE_USERFQDN;
6363 /* XXX is it correct? */
6364 if (curproc && curproc->p_cred)
6365 id->sadb_ident_id = curproc->p_cred->p_ruid;
6366 if (userfqdn && userfqdnlen)
6367 bcopy(userfqdn, id + 1, userfqdnlen);
6368 p += sizeof(struct sadb_ident) + PFKEY_ALIGN8(userfqdnlen);
6372 /* XXX sensitivity (optional) */
6374 /* create proposal/combination extension */
6375 m = key_getprop(saidx);
6378 * spec conformant: always attach proposal/combination extension,
6379 * the problem is that we have no way to attach it for ipcomp,
6380 * due to the way sadb_comb is declared in RFC2367.
6389 * outside of spec; make proposal/combination extension optional.
6395 if ((result->m_flags & M_PKTHDR) == 0) {
6400 if (result->m_len < sizeof(struct sadb_msg)) {
6401 result = m_pullup(result, sizeof(struct sadb_msg));
6402 if (result == NULL) {
6408 result->m_pkthdr.len = 0;
6409 for (m = result; m; m = m->m_next)
6410 result->m_pkthdr.len += m->m_len;
6412 mtod(result, struct sadb_msg *)->sadb_msg_len =
6413 PFKEY_UNIT64(result->m_pkthdr.len);
6415 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
6423 static struct secacq *
6424 key_newacq(const struct secasindex *saidx)
6426 struct secacq *newacq;
6429 newacq = malloc(sizeof(struct secacq), M_IPSEC_SAQ, M_NOWAIT|M_ZERO);
6430 if (newacq == NULL) {
6431 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
6436 bcopy(saidx, &newacq->saidx, sizeof(newacq->saidx));
6437 newacq->seq = (V_acq_seq == ~0 ? 1 : ++V_acq_seq);
6438 newacq->created = time_second;
6441 /* add to acqtree */
6443 LIST_INSERT_HEAD(&V_acqtree, newacq, chain);
6449 static struct secacq *
6450 key_getacq(const struct secasindex *saidx)
6455 LIST_FOREACH(acq, &V_acqtree, chain) {
6456 if (key_cmpsaidx(saidx, &acq->saidx, CMP_EXACTLY))
6464 static struct secacq *
6465 key_getacqbyseq(seq)
6471 LIST_FOREACH(acq, &V_acqtree, chain) {
6472 if (acq->seq == seq)
6480 static struct secspacq *
6482 struct secpolicyindex *spidx;
6484 struct secspacq *acq;
6487 acq = malloc(sizeof(struct secspacq), M_IPSEC_SAQ, M_NOWAIT|M_ZERO);
6489 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
6494 bcopy(spidx, &acq->spidx, sizeof(acq->spidx));
6495 acq->created = time_second;
6498 /* add to spacqtree */
6500 LIST_INSERT_HEAD(&V_spacqtree, acq, chain);
6506 static struct secspacq *
6508 struct secpolicyindex *spidx;
6510 struct secspacq *acq;
6513 LIST_FOREACH(acq, &V_spacqtree, chain) {
6514 if (key_cmpspidx_exactly(spidx, &acq->spidx)) {
6515 /* NB: return holding spacq_lock */
6525 * SADB_ACQUIRE processing,
6526 * in first situation, is receiving
6528 * from the ikmpd, and clear sequence of its secasvar entry.
6530 * In second situation, is receiving
6531 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
6532 * from a user land process, and return
6533 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
6536 * m will always be freed.
6539 key_acquire2(so, m, mhp)
6542 const struct sadb_msghdr *mhp;
6544 const struct sadb_address *src0, *dst0;
6545 struct secasindex saidx;
6546 struct secashead *sah;
6550 IPSEC_ASSERT(so != NULL, ("null socket"));
6551 IPSEC_ASSERT(m != NULL, ("null mbuf"));
6552 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
6553 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
6556 * Error message from KMd.
6557 * We assume that if error was occured in IKEd, the length of PFKEY
6558 * message is equal to the size of sadb_msg structure.
6559 * We do not raise error even if error occured in this function.
6561 if (mhp->msg->sadb_msg_len == PFKEY_UNIT64(sizeof(struct sadb_msg))) {
6564 /* check sequence number */
6565 if (mhp->msg->sadb_msg_seq == 0) {
6566 ipseclog((LOG_DEBUG, "%s: must specify sequence "
6567 "number.\n", __func__));
6572 if ((acq = key_getacqbyseq(mhp->msg->sadb_msg_seq)) == NULL) {
6574 * the specified larval SA is already gone, or we got
6575 * a bogus sequence number. we can silently ignore it.
6581 /* reset acq counter in order to deletion by timehander. */
6582 acq->created = time_second;
6589 * This message is from user land.
6592 /* map satype to proto */
6593 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
6594 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
6596 return key_senderror(so, m, EINVAL);
6599 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
6600 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
6601 mhp->ext[SADB_EXT_PROPOSAL] == NULL) {
6603 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
6605 return key_senderror(so, m, EINVAL);
6607 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
6608 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
6609 mhp->extlen[SADB_EXT_PROPOSAL] < sizeof(struct sadb_prop)) {
6611 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
6613 return key_senderror(so, m, EINVAL);
6616 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
6617 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
6619 /* XXX boundary check against sa_len */
6620 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
6623 * Make sure the port numbers are zero.
6624 * In case of NAT-T we will update them later if needed.
6626 KEY_PORTTOSADDR(&saidx.src, 0);
6627 KEY_PORTTOSADDR(&saidx.dst, 0);
6631 * Handle NAT-T info if present.
6634 if (mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
6635 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
6636 struct sadb_x_nat_t_port *sport, *dport;
6638 if (mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
6639 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
6640 ipseclog((LOG_DEBUG, "%s: invalid message.\n",
6642 return key_senderror(so, m, EINVAL);
6645 sport = (struct sadb_x_nat_t_port *)
6646 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
6647 dport = (struct sadb_x_nat_t_port *)
6648 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
6651 KEY_PORTTOSADDR(&saidx.src,
6652 sport->sadb_x_nat_t_port_port);
6654 KEY_PORTTOSADDR(&saidx.dst,
6655 dport->sadb_x_nat_t_port_port);
6659 /* get a SA index */
6661 LIST_FOREACH(sah, &V_sahtree, chain) {
6662 if (sah->state == SADB_SASTATE_DEAD)
6664 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_MODE_REQID))
6669 ipseclog((LOG_DEBUG, "%s: a SA exists already.\n", __func__));
6670 return key_senderror(so, m, EEXIST);
6673 error = key_acquire(&saidx, NULL);
6675 ipseclog((LOG_DEBUG, "%s: error %d returned from key_acquire\n",
6676 __func__, mhp->msg->sadb_msg_errno));
6677 return key_senderror(so, m, error);
6680 return key_sendup_mbuf(so, m, KEY_SENDUP_REGISTERED);
6684 * SADB_REGISTER processing.
6685 * If SATYPE_UNSPEC has been passed as satype, only return sabd_supported.
6688 * from the ikmpd, and register a socket to send PF_KEY messages,
6692 * If socket is detached, must free from regnode.
6694 * m will always be freed.
6697 key_register(so, m, mhp)
6700 const struct sadb_msghdr *mhp;
6702 struct secreg *reg, *newreg = 0;
6704 IPSEC_ASSERT(so != NULL, ("null socket"));
6705 IPSEC_ASSERT(m != NULL, ("null mbuf"));
6706 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
6707 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
6709 /* check for invalid register message */
6710 if (mhp->msg->sadb_msg_satype >= sizeof(V_regtree)/sizeof(V_regtree[0]))
6711 return key_senderror(so, m, EINVAL);
6713 /* When SATYPE_UNSPEC is specified, only return sabd_supported. */
6714 if (mhp->msg->sadb_msg_satype == SADB_SATYPE_UNSPEC)
6717 /* check whether existing or not */
6719 LIST_FOREACH(reg, &V_regtree[mhp->msg->sadb_msg_satype], chain) {
6720 if (reg->so == so) {
6722 ipseclog((LOG_DEBUG, "%s: socket exists already.\n",
6724 return key_senderror(so, m, EEXIST);
6728 /* create regnode */
6729 newreg = malloc(sizeof(struct secreg), M_IPSEC_SAR, M_NOWAIT|M_ZERO);
6730 if (newreg == NULL) {
6732 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
6733 return key_senderror(so, m, ENOBUFS);
6737 ((struct keycb *)sotorawcb(so))->kp_registered++;
6739 /* add regnode to regtree. */
6740 LIST_INSERT_HEAD(&V_regtree[mhp->msg->sadb_msg_satype], newreg, chain);
6746 struct sadb_msg *newmsg;
6747 struct sadb_supported *sup;
6748 u_int len, alen, elen;
6751 struct sadb_alg *alg;
6753 /* create new sadb_msg to reply. */
6755 for (i = 1; i <= SADB_AALG_MAX; i++) {
6756 if (ah_algorithm_lookup(i))
6757 alen += sizeof(struct sadb_alg);
6760 alen += sizeof(struct sadb_supported);
6762 for (i = 1; i <= SADB_EALG_MAX; i++) {
6763 if (esp_algorithm_lookup(i))
6764 elen += sizeof(struct sadb_alg);
6767 elen += sizeof(struct sadb_supported);
6769 len = sizeof(struct sadb_msg) + alen + elen;
6772 return key_senderror(so, m, ENOBUFS);
6774 MGETHDR(n, M_NOWAIT, MT_DATA);
6776 MCLGET(n, M_NOWAIT);
6777 if ((n->m_flags & M_EXT) == 0) {
6783 return key_senderror(so, m, ENOBUFS);
6785 n->m_pkthdr.len = n->m_len = len;
6789 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
6790 newmsg = mtod(n, struct sadb_msg *);
6791 newmsg->sadb_msg_errno = 0;
6792 newmsg->sadb_msg_len = PFKEY_UNIT64(len);
6793 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
6795 /* for authentication algorithm */
6797 sup = (struct sadb_supported *)(mtod(n, caddr_t) + off);
6798 sup->sadb_supported_len = PFKEY_UNIT64(alen);
6799 sup->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
6800 off += PFKEY_ALIGN8(sizeof(*sup));
6802 for (i = 1; i <= SADB_AALG_MAX; i++) {
6803 struct auth_hash *aalgo;
6804 u_int16_t minkeysize, maxkeysize;
6806 aalgo = ah_algorithm_lookup(i);
6809 alg = (struct sadb_alg *)(mtod(n, caddr_t) + off);
6810 alg->sadb_alg_id = i;
6811 alg->sadb_alg_ivlen = 0;
6812 key_getsizes_ah(aalgo, i, &minkeysize, &maxkeysize);
6813 alg->sadb_alg_minbits = _BITS(minkeysize);
6814 alg->sadb_alg_maxbits = _BITS(maxkeysize);
6815 off += PFKEY_ALIGN8(sizeof(*alg));
6819 /* for encryption algorithm */
6821 sup = (struct sadb_supported *)(mtod(n, caddr_t) + off);
6822 sup->sadb_supported_len = PFKEY_UNIT64(elen);
6823 sup->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT;
6824 off += PFKEY_ALIGN8(sizeof(*sup));
6826 for (i = 1; i <= SADB_EALG_MAX; i++) {
6827 struct enc_xform *ealgo;
6829 ealgo = esp_algorithm_lookup(i);
6832 alg = (struct sadb_alg *)(mtod(n, caddr_t) + off);
6833 alg->sadb_alg_id = i;
6834 alg->sadb_alg_ivlen = ealgo->blocksize;
6835 alg->sadb_alg_minbits = _BITS(ealgo->minkey);
6836 alg->sadb_alg_maxbits = _BITS(ealgo->maxkey);
6837 off += PFKEY_ALIGN8(sizeof(struct sadb_alg));
6841 IPSEC_ASSERT(off == len,
6842 ("length assumption failed (off %u len %u)", off, len));
6845 return key_sendup_mbuf(so, n, KEY_SENDUP_REGISTERED);
6850 * free secreg entry registered.
6851 * XXX: I want to do free a socket marked done SADB_RESIGER to socket.
6854 key_freereg(struct socket *so)
6859 IPSEC_ASSERT(so != NULL, ("NULL so"));
6862 * check whether existing or not.
6863 * check all type of SA, because there is a potential that
6864 * one socket is registered to multiple type of SA.
6867 for (i = 0; i <= SADB_SATYPE_MAX; i++) {
6868 LIST_FOREACH(reg, &V_regtree[i], chain) {
6869 if (reg->so == so && __LIST_CHAINED(reg)) {
6870 LIST_REMOVE(reg, chain);
6871 free(reg, M_IPSEC_SAR);
6880 * SADB_EXPIRE processing
6882 * <base, SA, SA2, lifetime(C and one of HS), address(SD)>
6884 * NOTE: We send only soft lifetime extension.
6887 * others : error number
6890 key_expire(struct secasvar *sav)
6893 struct mbuf *result = NULL, *m;
6896 struct sadb_lifetime *lt;
6898 IPSEC_ASSERT (sav != NULL, ("null sav"));
6899 IPSEC_ASSERT (sav->sah != NULL, ("null sa header"));
6901 /* set msg header */
6902 satype = key_proto2satype(sav->sah->saidx.proto);
6903 IPSEC_ASSERT(satype != 0, ("invalid proto, satype %u", satype));
6904 m = key_setsadbmsg(SADB_EXPIRE, 0, satype, sav->seq, 0, sav->refcnt);
6911 /* create SA extension */
6912 m = key_setsadbsa(sav);
6919 /* create SA extension */
6920 m = key_setsadbxsa2(sav->sah->saidx.mode,
6921 sav->replay ? sav->replay->count : 0,
6922 sav->sah->saidx.reqid);
6929 /* create lifetime extension (current and soft) */
6930 len = PFKEY_ALIGN8(sizeof(*lt)) * 2;
6931 m = m_get2(len, M_NOWAIT, MT_DATA, 0);
6938 bzero(mtod(m, caddr_t), len);
6939 lt = mtod(m, struct sadb_lifetime *);
6940 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
6941 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
6942 lt->sadb_lifetime_allocations = sav->lft_c->allocations;
6943 lt->sadb_lifetime_bytes = sav->lft_c->bytes;
6944 lt->sadb_lifetime_addtime = sav->lft_c->addtime;
6945 lt->sadb_lifetime_usetime = sav->lft_c->usetime;
6946 lt = (struct sadb_lifetime *)(mtod(m, caddr_t) + len / 2);
6947 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
6948 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
6949 lt->sadb_lifetime_allocations = sav->lft_s->allocations;
6950 lt->sadb_lifetime_bytes = sav->lft_s->bytes;
6951 lt->sadb_lifetime_addtime = sav->lft_s->addtime;
6952 lt->sadb_lifetime_usetime = sav->lft_s->usetime;
6955 /* set sadb_address for source */
6956 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
6957 &sav->sah->saidx.src.sa,
6958 FULLMASK, IPSEC_ULPROTO_ANY);
6965 /* set sadb_address for destination */
6966 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
6967 &sav->sah->saidx.dst.sa,
6968 FULLMASK, IPSEC_ULPROTO_ANY);
6976 * XXX-BZ Handle NAT-T extensions here.
6979 if ((result->m_flags & M_PKTHDR) == 0) {
6984 if (result->m_len < sizeof(struct sadb_msg)) {
6985 result = m_pullup(result, sizeof(struct sadb_msg));
6986 if (result == NULL) {
6992 result->m_pkthdr.len = 0;
6993 for (m = result; m; m = m->m_next)
6994 result->m_pkthdr.len += m->m_len;
6996 mtod(result, struct sadb_msg *)->sadb_msg_len =
6997 PFKEY_UNIT64(result->m_pkthdr.len);
6999 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
7008 * SADB_FLUSH processing
7011 * from the ikmpd, and free all entries in secastree.
7015 * NOTE: to do is only marking SADB_SASTATE_DEAD.
7017 * m will always be freed.
7020 key_flush(so, m, mhp)
7023 const struct sadb_msghdr *mhp;
7025 struct sadb_msg *newmsg;
7026 struct secashead *sah, *nextsah;
7027 struct secasvar *sav, *nextsav;
7032 IPSEC_ASSERT(so != NULL, ("null socket"));
7033 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
7034 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
7036 /* map satype to proto */
7037 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
7038 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
7040 return key_senderror(so, m, EINVAL);
7043 /* no SATYPE specified, i.e. flushing all SA. */
7045 for (sah = LIST_FIRST(&V_sahtree);
7048 nextsah = LIST_NEXT(sah, chain);
7050 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
7051 && proto != sah->saidx.proto)
7055 stateidx < _ARRAYLEN(saorder_state_alive);
7057 state = saorder_state_any[stateidx];
7058 for (sav = LIST_FIRST(&sah->savtree[state]);
7062 nextsav = LIST_NEXT(sav, chain);
7064 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
7069 sah->state = SADB_SASTATE_DEAD;
7073 if (m->m_len < sizeof(struct sadb_msg) ||
7074 sizeof(struct sadb_msg) > m->m_len + M_TRAILINGSPACE(m)) {
7075 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
7076 return key_senderror(so, m, ENOBUFS);
7082 m->m_pkthdr.len = m->m_len = sizeof(struct sadb_msg);
7083 newmsg = mtod(m, struct sadb_msg *);
7084 newmsg->sadb_msg_errno = 0;
7085 newmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
7087 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
7091 * SADB_DUMP processing
7092 * dump all entries including status of DEAD in SAD.
7095 * from the ikmpd, and dump all secasvar leaves
7100 * m will always be freed.
7103 key_dump(so, m, mhp)
7106 const struct sadb_msghdr *mhp;
7108 struct secashead *sah;
7109 struct secasvar *sav;
7115 struct sadb_msg *newmsg;
7118 IPSEC_ASSERT(so != NULL, ("null socket"));
7119 IPSEC_ASSERT(m != NULL, ("null mbuf"));
7120 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
7121 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
7123 /* map satype to proto */
7124 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
7125 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
7127 return key_senderror(so, m, EINVAL);
7130 /* count sav entries to be sent to the userland. */
7133 LIST_FOREACH(sah, &V_sahtree, chain) {
7134 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
7135 && proto != sah->saidx.proto)
7139 stateidx < _ARRAYLEN(saorder_state_any);
7141 state = saorder_state_any[stateidx];
7142 LIST_FOREACH(sav, &sah->savtree[state], chain) {
7150 return key_senderror(so, m, ENOENT);
7153 /* send this to the userland, one at a time. */
7155 LIST_FOREACH(sah, &V_sahtree, chain) {
7156 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
7157 && proto != sah->saidx.proto)
7160 /* map proto to satype */
7161 if ((satype = key_proto2satype(sah->saidx.proto)) == 0) {
7163 ipseclog((LOG_DEBUG, "%s: there was invalid proto in "
7164 "SAD.\n", __func__));
7165 return key_senderror(so, m, EINVAL);
7169 stateidx < _ARRAYLEN(saorder_state_any);
7171 state = saorder_state_any[stateidx];
7172 LIST_FOREACH(sav, &sah->savtree[state], chain) {
7173 n = key_setdumpsa(sav, SADB_DUMP, satype,
7174 --cnt, mhp->msg->sadb_msg_pid);
7177 return key_senderror(so, m, ENOBUFS);
7179 key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
7190 * SADB_X_PROMISC processing
7192 * m will always be freed.
7195 key_promisc(so, m, mhp)
7198 const struct sadb_msghdr *mhp;
7202 IPSEC_ASSERT(so != NULL, ("null socket"));
7203 IPSEC_ASSERT(m != NULL, ("null mbuf"));
7204 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
7205 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
7207 olen = PFKEY_UNUNIT64(mhp->msg->sadb_msg_len);
7209 if (olen < sizeof(struct sadb_msg)) {
7211 return key_senderror(so, m, EINVAL);
7216 } else if (olen == sizeof(struct sadb_msg)) {
7217 /* enable/disable promisc mode */
7220 if ((kp = (struct keycb *)sotorawcb(so)) == NULL)
7221 return key_senderror(so, m, EINVAL);
7222 mhp->msg->sadb_msg_errno = 0;
7223 switch (mhp->msg->sadb_msg_satype) {
7226 kp->kp_promisc = mhp->msg->sadb_msg_satype;
7229 return key_senderror(so, m, EINVAL);
7232 /* send the original message back to everyone */
7233 mhp->msg->sadb_msg_errno = 0;
7234 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
7236 /* send packet as is */
7238 m_adj(m, PFKEY_ALIGN8(sizeof(struct sadb_msg)));
7240 /* TODO: if sadb_msg_seq is specified, send to specific pid */
7241 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
7245 static int (*key_typesw[]) __P((struct socket *, struct mbuf *,
7246 const struct sadb_msghdr *)) = {
7247 NULL, /* SADB_RESERVED */
7248 key_getspi, /* SADB_GETSPI */
7249 key_update, /* SADB_UPDATE */
7250 key_add, /* SADB_ADD */
7251 key_delete, /* SADB_DELETE */
7252 key_get, /* SADB_GET */
7253 key_acquire2, /* SADB_ACQUIRE */
7254 key_register, /* SADB_REGISTER */
7255 NULL, /* SADB_EXPIRE */
7256 key_flush, /* SADB_FLUSH */
7257 key_dump, /* SADB_DUMP */
7258 key_promisc, /* SADB_X_PROMISC */
7259 NULL, /* SADB_X_PCHANGE */
7260 key_spdadd, /* SADB_X_SPDUPDATE */
7261 key_spdadd, /* SADB_X_SPDADD */
7262 key_spddelete, /* SADB_X_SPDDELETE */
7263 key_spdget, /* SADB_X_SPDGET */
7264 NULL, /* SADB_X_SPDACQUIRE */
7265 key_spddump, /* SADB_X_SPDDUMP */
7266 key_spdflush, /* SADB_X_SPDFLUSH */
7267 key_spdadd, /* SADB_X_SPDSETIDX */
7268 NULL, /* SADB_X_SPDEXPIRE */
7269 key_spddelete2, /* SADB_X_SPDDELETE2 */
7273 * parse sadb_msg buffer to process PFKEYv2,
7274 * and create a data to response if needed.
7275 * I think to be dealed with mbuf directly.
7277 * msgp : pointer to pointer to a received buffer pulluped.
7278 * This is rewrited to response.
7279 * so : pointer to socket.
7281 * length for buffer to send to user process.
7288 struct sadb_msg *msg;
7289 struct sadb_msghdr mh;
7294 IPSEC_ASSERT(so != NULL, ("null socket"));
7295 IPSEC_ASSERT(m != NULL, ("null mbuf"));
7297 #if 0 /*kdebug_sadb assumes msg in linear buffer*/
7298 KEYDEBUG(KEYDEBUG_KEY_DUMP,
7299 ipseclog((LOG_DEBUG, "%s: passed sadb_msg\n", __func__));
7303 if (m->m_len < sizeof(struct sadb_msg)) {
7304 m = m_pullup(m, sizeof(struct sadb_msg));
7308 msg = mtod(m, struct sadb_msg *);
7309 orglen = PFKEY_UNUNIT64(msg->sadb_msg_len);
7310 target = KEY_SENDUP_ONE;
7312 if ((m->m_flags & M_PKTHDR) == 0 ||
7313 m->m_pkthdr.len != m->m_pkthdr.len) {
7314 ipseclog((LOG_DEBUG, "%s: invalid message length.\n",__func__));
7315 PFKEYSTAT_INC(out_invlen);
7320 if (msg->sadb_msg_version != PF_KEY_V2) {
7321 ipseclog((LOG_DEBUG, "%s: PF_KEY version %u is mismatched.\n",
7322 __func__, msg->sadb_msg_version));
7323 PFKEYSTAT_INC(out_invver);
7328 if (msg->sadb_msg_type > SADB_MAX) {
7329 ipseclog((LOG_DEBUG, "%s: invalid type %u is passed.\n",
7330 __func__, msg->sadb_msg_type));
7331 PFKEYSTAT_INC(out_invmsgtype);
7336 /* for old-fashioned code - should be nuked */
7337 if (m->m_pkthdr.len > MCLBYTES) {
7344 MGETHDR(n, M_NOWAIT, MT_DATA);
7345 if (n && m->m_pkthdr.len > MHLEN) {
7346 MCLGET(n, M_NOWAIT);
7347 if ((n->m_flags & M_EXT) == 0) {
7356 m_copydata(m, 0, m->m_pkthdr.len, mtod(n, caddr_t));
7357 n->m_pkthdr.len = n->m_len = m->m_pkthdr.len;
7363 /* align the mbuf chain so that extensions are in contiguous region. */
7364 error = key_align(m, &mh);
7371 switch (msg->sadb_msg_satype) {
7372 case SADB_SATYPE_UNSPEC:
7373 switch (msg->sadb_msg_type) {
7381 ipseclog((LOG_DEBUG, "%s: must specify satype "
7382 "when msg type=%u.\n", __func__,
7383 msg->sadb_msg_type));
7384 PFKEYSTAT_INC(out_invsatype);
7389 case SADB_SATYPE_AH:
7390 case SADB_SATYPE_ESP:
7391 case SADB_X_SATYPE_IPCOMP:
7392 case SADB_X_SATYPE_TCPSIGNATURE:
7393 switch (msg->sadb_msg_type) {
7395 case SADB_X_SPDDELETE:
7397 case SADB_X_SPDDUMP:
7398 case SADB_X_SPDFLUSH:
7399 case SADB_X_SPDSETIDX:
7400 case SADB_X_SPDUPDATE:
7401 case SADB_X_SPDDELETE2:
7402 ipseclog((LOG_DEBUG, "%s: illegal satype=%u\n",
7403 __func__, msg->sadb_msg_type));
7404 PFKEYSTAT_INC(out_invsatype);
7409 case SADB_SATYPE_RSVP:
7410 case SADB_SATYPE_OSPFV2:
7411 case SADB_SATYPE_RIPV2:
7412 case SADB_SATYPE_MIP:
7413 ipseclog((LOG_DEBUG, "%s: type %u isn't supported.\n",
7414 __func__, msg->sadb_msg_satype));
7415 PFKEYSTAT_INC(out_invsatype);
7418 case 1: /* XXX: What does it do? */
7419 if (msg->sadb_msg_type == SADB_X_PROMISC)
7423 ipseclog((LOG_DEBUG, "%s: invalid type %u is passed.\n",
7424 __func__, msg->sadb_msg_satype));
7425 PFKEYSTAT_INC(out_invsatype);
7430 /* check field of upper layer protocol and address family */
7431 if (mh.ext[SADB_EXT_ADDRESS_SRC] != NULL
7432 && mh.ext[SADB_EXT_ADDRESS_DST] != NULL) {
7433 struct sadb_address *src0, *dst0;
7436 src0 = (struct sadb_address *)(mh.ext[SADB_EXT_ADDRESS_SRC]);
7437 dst0 = (struct sadb_address *)(mh.ext[SADB_EXT_ADDRESS_DST]);
7439 /* check upper layer protocol */
7440 if (src0->sadb_address_proto != dst0->sadb_address_proto) {
7441 ipseclog((LOG_DEBUG, "%s: upper layer protocol "
7442 "mismatched.\n", __func__));
7443 PFKEYSTAT_INC(out_invaddr);
7449 if (PFKEY_ADDR_SADDR(src0)->sa_family !=
7450 PFKEY_ADDR_SADDR(dst0)->sa_family) {
7451 ipseclog((LOG_DEBUG, "%s: address family mismatched.\n",
7453 PFKEYSTAT_INC(out_invaddr);
7457 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
7458 PFKEY_ADDR_SADDR(dst0)->sa_len) {
7459 ipseclog((LOG_DEBUG, "%s: address struct size "
7460 "mismatched.\n", __func__));
7461 PFKEYSTAT_INC(out_invaddr);
7466 switch (PFKEY_ADDR_SADDR(src0)->sa_family) {
7468 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
7469 sizeof(struct sockaddr_in)) {
7470 PFKEYSTAT_INC(out_invaddr);
7476 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
7477 sizeof(struct sockaddr_in6)) {
7478 PFKEYSTAT_INC(out_invaddr);
7484 ipseclog((LOG_DEBUG, "%s: unsupported address family\n",
7486 PFKEYSTAT_INC(out_invaddr);
7487 error = EAFNOSUPPORT;
7491 switch (PFKEY_ADDR_SADDR(src0)->sa_family) {
7493 plen = sizeof(struct in_addr) << 3;
7496 plen = sizeof(struct in6_addr) << 3;
7499 plen = 0; /*fool gcc*/
7503 /* check max prefix length */
7504 if (src0->sadb_address_prefixlen > plen ||
7505 dst0->sadb_address_prefixlen > plen) {
7506 ipseclog((LOG_DEBUG, "%s: illegal prefixlen.\n",
7508 PFKEYSTAT_INC(out_invaddr);
7514 * prefixlen == 0 is valid because there can be a case when
7515 * all addresses are matched.
7519 if (msg->sadb_msg_type >= sizeof(key_typesw)/sizeof(key_typesw[0]) ||
7520 key_typesw[msg->sadb_msg_type] == NULL) {
7521 PFKEYSTAT_INC(out_invmsgtype);
7526 return (*key_typesw[msg->sadb_msg_type])(so, m, &mh);
7529 msg->sadb_msg_errno = error;
7530 return key_sendup_mbuf(so, m, target);
7534 key_senderror(so, m, code)
7539 struct sadb_msg *msg;
7541 IPSEC_ASSERT(m->m_len >= sizeof(struct sadb_msg),
7542 ("mbuf too small, len %u", m->m_len));
7544 msg = mtod(m, struct sadb_msg *);
7545 msg->sadb_msg_errno = code;
7546 return key_sendup_mbuf(so, m, KEY_SENDUP_ONE);
7550 * set the pointer to each header into message buffer.
7551 * m will be freed on error.
7552 * XXX larger-than-MCLBYTES extension?
7557 struct sadb_msghdr *mhp;
7560 struct sadb_ext *ext;
7565 IPSEC_ASSERT(m != NULL, ("null mbuf"));
7566 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
7567 IPSEC_ASSERT(m->m_len >= sizeof(struct sadb_msg),
7568 ("mbuf too small, len %u", m->m_len));
7571 bzero(mhp, sizeof(*mhp));
7573 mhp->msg = mtod(m, struct sadb_msg *);
7574 mhp->ext[0] = (struct sadb_ext *)mhp->msg; /*XXX backward compat */
7576 end = PFKEY_UNUNIT64(mhp->msg->sadb_msg_len);
7577 extlen = end; /*just in case extlen is not updated*/
7578 for (off = sizeof(struct sadb_msg); off < end; off += extlen) {
7579 n = m_pulldown(m, off, sizeof(struct sadb_ext), &toff);
7581 /* m is already freed */
7584 ext = (struct sadb_ext *)(mtod(n, caddr_t) + toff);
7587 switch (ext->sadb_ext_type) {
7589 case SADB_EXT_ADDRESS_SRC:
7590 case SADB_EXT_ADDRESS_DST:
7591 case SADB_EXT_ADDRESS_PROXY:
7592 case SADB_EXT_LIFETIME_CURRENT:
7593 case SADB_EXT_LIFETIME_HARD:
7594 case SADB_EXT_LIFETIME_SOFT:
7595 case SADB_EXT_KEY_AUTH:
7596 case SADB_EXT_KEY_ENCRYPT:
7597 case SADB_EXT_IDENTITY_SRC:
7598 case SADB_EXT_IDENTITY_DST:
7599 case SADB_EXT_SENSITIVITY:
7600 case SADB_EXT_PROPOSAL:
7601 case SADB_EXT_SUPPORTED_AUTH:
7602 case SADB_EXT_SUPPORTED_ENCRYPT:
7603 case SADB_EXT_SPIRANGE:
7604 case SADB_X_EXT_POLICY:
7605 case SADB_X_EXT_SA2:
7607 case SADB_X_EXT_NAT_T_TYPE:
7608 case SADB_X_EXT_NAT_T_SPORT:
7609 case SADB_X_EXT_NAT_T_DPORT:
7610 case SADB_X_EXT_NAT_T_OAI:
7611 case SADB_X_EXT_NAT_T_OAR:
7612 case SADB_X_EXT_NAT_T_FRAG:
7614 /* duplicate check */
7616 * XXX Are there duplication payloads of either
7617 * KEY_AUTH or KEY_ENCRYPT ?
7619 if (mhp->ext[ext->sadb_ext_type] != NULL) {
7620 ipseclog((LOG_DEBUG, "%s: duplicate ext_type "
7621 "%u\n", __func__, ext->sadb_ext_type));
7623 PFKEYSTAT_INC(out_dupext);
7628 ipseclog((LOG_DEBUG, "%s: invalid ext_type %u\n",
7629 __func__, ext->sadb_ext_type));
7631 PFKEYSTAT_INC(out_invexttype);
7635 extlen = PFKEY_UNUNIT64(ext->sadb_ext_len);
7637 if (key_validate_ext(ext, extlen)) {
7639 PFKEYSTAT_INC(out_invlen);
7643 n = m_pulldown(m, off, extlen, &toff);
7645 /* m is already freed */
7648 ext = (struct sadb_ext *)(mtod(n, caddr_t) + toff);
7650 mhp->ext[ext->sadb_ext_type] = ext;
7651 mhp->extoff[ext->sadb_ext_type] = off;
7652 mhp->extlen[ext->sadb_ext_type] = extlen;
7657 PFKEYSTAT_INC(out_invlen);
7665 key_validate_ext(ext, len)
7666 const struct sadb_ext *ext;
7669 const struct sockaddr *sa;
7670 enum { NONE, ADDR } checktype = NONE;
7672 const int sal = offsetof(struct sockaddr, sa_len) + sizeof(sa->sa_len);
7674 if (len != PFKEY_UNUNIT64(ext->sadb_ext_len))
7677 /* if it does not match minimum/maximum length, bail */
7678 if (ext->sadb_ext_type >= sizeof(minsize) / sizeof(minsize[0]) ||
7679 ext->sadb_ext_type >= sizeof(maxsize) / sizeof(maxsize[0]))
7681 if (!minsize[ext->sadb_ext_type] || len < minsize[ext->sadb_ext_type])
7683 if (maxsize[ext->sadb_ext_type] && len > maxsize[ext->sadb_ext_type])
7686 /* more checks based on sadb_ext_type XXX need more */
7687 switch (ext->sadb_ext_type) {
7688 case SADB_EXT_ADDRESS_SRC:
7689 case SADB_EXT_ADDRESS_DST:
7690 case SADB_EXT_ADDRESS_PROXY:
7691 baselen = PFKEY_ALIGN8(sizeof(struct sadb_address));
7694 case SADB_EXT_IDENTITY_SRC:
7695 case SADB_EXT_IDENTITY_DST:
7696 if (((const struct sadb_ident *)ext)->sadb_ident_type ==
7697 SADB_X_IDENTTYPE_ADDR) {
7698 baselen = PFKEY_ALIGN8(sizeof(struct sadb_ident));
7708 switch (checktype) {
7712 sa = (const struct sockaddr *)(((const u_int8_t*)ext)+baselen);
7713 if (len < baselen + sal)
7715 if (baselen + PFKEY_ALIGN8(sa->sa_len) != len)
7728 for (i = 0; i < IPSEC_DIR_MAX; i++)
7729 LIST_INIT(&V_sptree[i]);
7731 LIST_INIT(&V_sahtree);
7733 for (i = 0; i <= SADB_SATYPE_MAX; i++)
7734 LIST_INIT(&V_regtree[i]);
7736 LIST_INIT(&V_acqtree);
7737 LIST_INIT(&V_spacqtree);
7739 /* system default */
7740 V_ip4_def_policy.policy = IPSEC_POLICY_NONE;
7741 V_ip4_def_policy.refcnt++; /*never reclaim this*/
7743 if (!IS_DEFAULT_VNET(curvnet))
7747 REGTREE_LOCK_INIT();
7748 SAHTREE_LOCK_INIT();
7752 #ifndef IPSEC_DEBUG2
7753 timeout((void *)key_timehandler, (void *)0, hz);
7754 #endif /*IPSEC_DEBUG2*/
7756 /* initialize key statistics */
7757 keystat.getspi_count = 1;
7759 printf("IPsec: Initialized Security Association Processing.\n");
7766 struct secpolicy *sp, *nextsp;
7767 struct secacq *acq, *nextacq;
7768 struct secspacq *spacq, *nextspacq;
7769 struct secashead *sah, *nextsah;
7774 for (i = 0; i < IPSEC_DIR_MAX; i++) {
7775 for (sp = LIST_FIRST(&V_sptree[i]);
7776 sp != NULL; sp = nextsp) {
7777 nextsp = LIST_NEXT(sp, chain);
7778 if (__LIST_CHAINED(sp)) {
7779 LIST_REMOVE(sp, chain);
7780 free(sp, M_IPSEC_SP);
7787 for (sah = LIST_FIRST(&V_sahtree); sah != NULL; sah = nextsah) {
7788 nextsah = LIST_NEXT(sah, chain);
7789 if (__LIST_CHAINED(sah)) {
7790 LIST_REMOVE(sah, chain);
7791 free(sah, M_IPSEC_SAH);
7797 for (i = 0; i <= SADB_SATYPE_MAX; i++) {
7798 LIST_FOREACH(reg, &V_regtree[i], chain) {
7799 if (__LIST_CHAINED(reg)) {
7800 LIST_REMOVE(reg, chain);
7801 free(reg, M_IPSEC_SAR);
7809 for (acq = LIST_FIRST(&V_acqtree); acq != NULL; acq = nextacq) {
7810 nextacq = LIST_NEXT(acq, chain);
7811 if (__LIST_CHAINED(acq)) {
7812 LIST_REMOVE(acq, chain);
7813 free(acq, M_IPSEC_SAQ);
7819 for (spacq = LIST_FIRST(&V_spacqtree); spacq != NULL;
7820 spacq = nextspacq) {
7821 nextspacq = LIST_NEXT(spacq, chain);
7822 if (__LIST_CHAINED(spacq)) {
7823 LIST_REMOVE(spacq, chain);
7824 free(spacq, M_IPSEC_SAQ);
7832 * XXX: maybe This function is called after INBOUND IPsec processing.
7834 * Special check for tunnel-mode packets.
7835 * We must make some checks for consistency between inner and outer IP header.
7837 * xxx more checks to be provided
7840 key_checktunnelsanity(sav, family, src, dst)
7841 struct secasvar *sav;
7846 IPSEC_ASSERT(sav->sah != NULL, ("null SA header"));
7848 /* XXX: check inner IP header */
7853 /* record data transfer on SA, and update timestamps */
7855 key_sa_recordxfer(sav, m)
7856 struct secasvar *sav;
7859 IPSEC_ASSERT(sav != NULL, ("Null secasvar"));
7860 IPSEC_ASSERT(m != NULL, ("Null mbuf"));
7865 * XXX Currently, there is a difference of bytes size
7866 * between inbound and outbound processing.
7868 sav->lft_c->bytes += m->m_pkthdr.len;
7869 /* to check bytes lifetime is done in key_timehandler(). */
7872 * We use the number of packets as the unit of
7873 * allocations. We increment the variable
7874 * whenever {esp,ah}_{in,out}put is called.
7876 sav->lft_c->allocations++;
7877 /* XXX check for expires? */
7880 * NOTE: We record CURRENT usetime by using wall clock,
7881 * in seconds. HARD and SOFT lifetime are measured by the time
7882 * difference (again in seconds) from usetime.
7886 * -----+-----+--------+---> t
7887 * <--------------> HARD
7890 sav->lft_c->usetime = time_second;
7891 /* XXX check for expires? */
7898 key_sa_routechange(dst)
7899 struct sockaddr *dst;
7901 struct secashead *sah;
7905 LIST_FOREACH(sah, &V_sahtree, chain) {
7906 ro = &sah->route_cache.sa_route;
7907 if (ro->ro_rt && dst->sa_len == ro->ro_dst.sa_len
7908 && bcmp(dst, &ro->ro_dst, dst->sa_len) == 0) {
7910 ro->ro_rt = (struct rtentry *)NULL;
7917 key_sa_chgstate(struct secasvar *sav, u_int8_t state)
7919 IPSEC_ASSERT(sav != NULL, ("NULL sav"));
7920 SAHTREE_LOCK_ASSERT();
7922 if (sav->state != state) {
7923 if (__LIST_CHAINED(sav))
7924 LIST_REMOVE(sav, chain);
7926 LIST_INSERT_HEAD(&sav->sah->savtree[state], sav, chain);
7932 struct secasvar *sav;
7935 IPSEC_ASSERT(sav->iv != NULL, ("null IV"));
7936 key_randomfill(sav->iv, sav->ivlen);
7940 * Take one of the kernel's security keys and convert it into a PF_KEY
7941 * structure within an mbuf, suitable for sending up to a waiting
7942 * application in user land.
7945 * src: A pointer to a kernel security key.
7946 * exttype: Which type of key this is. Refer to the PF_KEY data structures.
7948 * a valid mbuf or NULL indicating an error
7952 static struct mbuf *
7953 key_setkey(struct seckey *src, u_int16_t exttype)
7962 len = PFKEY_ALIGN8(sizeof(struct sadb_key) + _KEYLEN(src));
7963 m = m_get2(len, M_NOWAIT, MT_DATA, 0);
7968 p = mtod(m, struct sadb_key *);
7970 p->sadb_key_len = PFKEY_UNIT64(len);
7971 p->sadb_key_exttype = exttype;
7972 p->sadb_key_bits = src->bits;
7973 bcopy(src->key_data, _KEYBUF(p), _KEYLEN(src));
7979 * Take one of the kernel's lifetime data structures and convert it
7980 * into a PF_KEY structure within an mbuf, suitable for sending up to
7981 * a waiting application in user land.
7984 * src: A pointer to a kernel lifetime structure.
7985 * exttype: Which type of lifetime this is. Refer to the PF_KEY
7986 * data structures for more information.
7988 * a valid mbuf or NULL indicating an error
7992 static struct mbuf *
7993 key_setlifetime(struct seclifetime *src, u_int16_t exttype)
7995 struct mbuf *m = NULL;
7996 struct sadb_lifetime *p;
7997 int len = PFKEY_ALIGN8(sizeof(struct sadb_lifetime));
8002 m = m_get2(len, M_NOWAIT, MT_DATA, 0);
8007 p = mtod(m, struct sadb_lifetime *);
8010 p->sadb_lifetime_len = PFKEY_UNIT64(len);
8011 p->sadb_lifetime_exttype = exttype;
8012 p->sadb_lifetime_allocations = src->allocations;
8013 p->sadb_lifetime_bytes = src->bytes;
8014 p->sadb_lifetime_addtime = src->addtime;
8015 p->sadb_lifetime_usetime = src->usetime;