1 /* $FreeBSD: src/sys/netipsec/key.c,v 1.3.2.1 2003/01/24 05:11:35 sam Exp $ */
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/domain.h>
47 #include <sys/protosw.h>
48 #include <sys/malloc.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/sysctl.h>
52 #include <sys/errno.h>
54 #include <sys/queue.h>
55 #include <sys/syslog.h>
58 #include <net/route.h>
59 #include <net/raw_cb.h>
61 #include <netinet/in.h>
62 #include <netinet/in_systm.h>
63 #include <netinet/ip.h>
64 #include <netinet/in_var.h>
67 #include <netinet/ip6.h>
68 #include <netinet6/in6_var.h>
69 #include <netinet6/ip6_var.h>
73 #include <netinet/in_pcb.h>
76 #include <netinet6/in6_pcb.h>
79 #include <net/pfkeyv2.h>
80 #include <netproto/ipsec/keydb.h>
81 #include <netproto/ipsec/key.h>
82 #include <netproto/ipsec/keysock.h>
83 #include <netproto/ipsec/key_debug.h>
85 #include <netproto/ipsec/ipsec.h>
87 #include <netproto/ipsec/ipsec6.h>
90 #include <netproto/ipsec/xform.h>
92 #include <machine/stdarg.h>
95 #include <sys/random.h>
97 #include <net/net_osdep.h>
100 #define _BITS(bytes) ((bytes) << 3)
103 * Note on SA reference counting:
104 * - SAs that are not in DEAD state will have (total external reference + 1)
105 * following value in reference count field. they cannot be freed and are
106 * referenced from SA header.
107 * - SAs that are in DEAD state will have (total external reference)
108 * in reference count field. they are ready to be freed. reference from
109 * SA header will be removed in key_delsav(), when the reference count
110 * field hits 0 (= no external reference other than from SA header.
114 static struct callout key_timehandler_ch;
116 u_int32_t key_debug_level = 0;
117 static u_int key_spi_trycnt = 1000;
118 static u_int32_t key_spi_minval = 0x100;
119 static u_int32_t key_spi_maxval = 0x0fffffff; /* XXX */
120 static u_int32_t policy_id = 0;
121 static u_int key_int_random = 60; /*interval to initialize randseed,1(m)*/
122 static u_int key_larval_lifetime = 30; /* interval to expire acquiring, 30(s)*/
123 static int key_blockacq_count = 10; /* counter for blocking SADB_ACQUIRE.*/
124 static int key_blockacq_lifetime = 20; /* lifetime for blocking SADB_ACQUIRE.*/
125 static int key_prefered_oldsa = 1; /* prefered old sa rather than new sa.*/
127 static u_int32_t acq_seq = 0;
128 static int key_tick_init_random = 0;
130 static LIST_HEAD(_sptree, secpolicy) sptree[IPSEC_DIR_MAX]; /* SPD */
131 static LIST_HEAD(_sahtree, secashead) sahtree; /* SAD */
132 static LIST_HEAD(_regtree, secreg) regtree[SADB_SATYPE_MAX + 1];
134 #ifndef IPSEC_NONBLOCK_ACQUIRE
135 static LIST_HEAD(_acqtree, secacq) acqtree; /* acquiring list */
137 static LIST_HEAD(_spacqtree, secspacq) spacqtree; /* SP acquiring list */
139 /* search order for SAs */
140 static u_int saorder_state_valid[] = {
141 SADB_SASTATE_DYING, SADB_SASTATE_MATURE,
143 * This order is important because we must select the oldest SA
144 * for outbound processing. For inbound, This is not important.
147 static u_int saorder_state_alive[] = {
149 SADB_SASTATE_MATURE, SADB_SASTATE_DYING, SADB_SASTATE_LARVAL
151 static u_int saorder_state_any[] = {
152 SADB_SASTATE_MATURE, SADB_SASTATE_DYING,
153 SADB_SASTATE_LARVAL, SADB_SASTATE_DEAD
156 static const int minsize[] = {
157 sizeof(struct sadb_msg), /* SADB_EXT_RESERVED */
158 sizeof(struct sadb_sa), /* SADB_EXT_SA */
159 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_CURRENT */
160 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_HARD */
161 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_SOFT */
162 sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_SRC */
163 sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_DST */
164 sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_PROXY */
165 sizeof(struct sadb_key), /* SADB_EXT_KEY_AUTH */
166 sizeof(struct sadb_key), /* SADB_EXT_KEY_ENCRYPT */
167 sizeof(struct sadb_ident), /* SADB_EXT_IDENTITY_SRC */
168 sizeof(struct sadb_ident), /* SADB_EXT_IDENTITY_DST */
169 sizeof(struct sadb_sens), /* SADB_EXT_SENSITIVITY */
170 sizeof(struct sadb_prop), /* SADB_EXT_PROPOSAL */
171 sizeof(struct sadb_supported), /* SADB_EXT_SUPPORTED_AUTH */
172 sizeof(struct sadb_supported), /* SADB_EXT_SUPPORTED_ENCRYPT */
173 sizeof(struct sadb_spirange), /* SADB_EXT_SPIRANGE */
174 0, /* SADB_X_EXT_KMPRIVATE */
175 sizeof(struct sadb_x_policy), /* SADB_X_EXT_POLICY */
176 sizeof(struct sadb_x_sa2), /* SADB_X_SA2 */
178 static const int maxsize[] = {
179 sizeof(struct sadb_msg), /* SADB_EXT_RESERVED */
180 sizeof(struct sadb_sa), /* SADB_EXT_SA */
181 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_CURRENT */
182 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_HARD */
183 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_SOFT */
184 0, /* SADB_EXT_ADDRESS_SRC */
185 0, /* SADB_EXT_ADDRESS_DST */
186 0, /* SADB_EXT_ADDRESS_PROXY */
187 0, /* SADB_EXT_KEY_AUTH */
188 0, /* SADB_EXT_KEY_ENCRYPT */
189 0, /* SADB_EXT_IDENTITY_SRC */
190 0, /* SADB_EXT_IDENTITY_DST */
191 0, /* SADB_EXT_SENSITIVITY */
192 0, /* SADB_EXT_PROPOSAL */
193 0, /* SADB_EXT_SUPPORTED_AUTH */
194 0, /* SADB_EXT_SUPPORTED_ENCRYPT */
195 sizeof(struct sadb_spirange), /* SADB_EXT_SPIRANGE */
196 0, /* SADB_X_EXT_KMPRIVATE */
197 0, /* SADB_X_EXT_POLICY */
198 sizeof(struct sadb_x_sa2), /* SADB_X_SA2 */
201 static int ipsec_esp_keymin = 256;
202 static int ipsec_esp_auth = 0;
203 static int ipsec_ah_keymin = 128;
206 SYSCTL_DECL(_net_key);
209 SYSCTL_INT(_net_key, KEYCTL_DEBUG_LEVEL, debug, CTLFLAG_RW, \
210 &key_debug_level, 0, "");
212 /* max count of trial for the decision of spi value */
213 SYSCTL_INT(_net_key, KEYCTL_SPI_TRY, spi_trycnt, CTLFLAG_RW, \
214 &key_spi_trycnt, 0, "");
216 /* minimum spi value to allocate automatically. */
217 SYSCTL_INT(_net_key, KEYCTL_SPI_MIN_VALUE, spi_minval, CTLFLAG_RW, \
218 &key_spi_minval, 0, "");
220 /* maximun spi value to allocate automatically. */
221 SYSCTL_INT(_net_key, KEYCTL_SPI_MAX_VALUE, spi_maxval, CTLFLAG_RW, \
222 &key_spi_maxval, 0, "");
224 /* interval to initialize randseed */
225 SYSCTL_INT(_net_key, KEYCTL_RANDOM_INT, int_random, CTLFLAG_RW, \
226 &key_int_random, 0, "");
228 /* lifetime for larval SA */
229 SYSCTL_INT(_net_key, KEYCTL_LARVAL_LIFETIME, larval_lifetime, CTLFLAG_RW, \
230 &key_larval_lifetime, 0, "");
232 /* counter for blocking to send SADB_ACQUIRE to IKEd */
233 SYSCTL_INT(_net_key, KEYCTL_BLOCKACQ_COUNT, blockacq_count, CTLFLAG_RW, \
234 &key_blockacq_count, 0, "");
236 /* lifetime for blocking to send SADB_ACQUIRE to IKEd */
237 SYSCTL_INT(_net_key, KEYCTL_BLOCKACQ_LIFETIME, blockacq_lifetime, CTLFLAG_RW, \
238 &key_blockacq_lifetime, 0, "");
241 SYSCTL_INT(_net_key, KEYCTL_ESP_AUTH, esp_auth, CTLFLAG_RW, \
242 &ipsec_esp_auth, 0, "");
244 /* minimum ESP key length */
245 SYSCTL_INT(_net_key, KEYCTL_ESP_KEYMIN, esp_keymin, CTLFLAG_RW, \
246 &ipsec_esp_keymin, 0, "");
248 /* minimum AH key length */
249 SYSCTL_INT(_net_key, KEYCTL_AH_KEYMIN, ah_keymin, CTLFLAG_RW, \
250 &ipsec_ah_keymin, 0, "");
252 /* perfered old SA rather than new SA */
253 SYSCTL_INT(_net_key, KEYCTL_PREFERED_OLDSA, prefered_oldsa, CTLFLAG_RW,\
254 &key_prefered_oldsa, 0, "");
256 #define __LIST_CHAINED(elm) \
257 (!((elm)->chain.le_next == NULL && (elm)->chain.le_prev == NULL))
258 #define LIST_INSERT_TAIL(head, elm, type, field) \
260 struct type *curelm = LIST_FIRST(head); \
261 if (curelm == NULL) {\
262 LIST_INSERT_HEAD(head, elm, field); \
264 while (LIST_NEXT(curelm, field)) \
265 curelm = LIST_NEXT(curelm, field);\
266 LIST_INSERT_AFTER(curelm, elm, field);\
270 #define KEY_CHKSASTATE(head, sav, name) \
272 if ((head) != (sav)) { \
273 ipseclog((LOG_DEBUG, "%s: state mismatched (TREE=%d SA=%d)\n", \
274 (name), (head), (sav))); \
279 #define KEY_CHKSPDIR(head, sp, name) \
281 if ((head) != (sp)) { \
282 ipseclog((LOG_DEBUG, "%s: direction mismatched (TREE=%d SP=%d), " \
283 "anyway continue.\n", \
284 (name), (head), (sp))); \
288 MALLOC_DEFINE(M_SECA, "key mgmt", "security associations, key management");
291 #define KMALLOC(p, t, n) \
292 ((p) = (t) kmalloc((unsigned long)(n), M_SECA, M_INTWAIT | M_NULLOK))
294 kfree((caddr_t)(p), M_SECA)
296 #define KMALLOC(p, t, n) \
298 ((p) = (t)kmalloc((unsigned long)(n), M_SECA, M_INTWAIT | M_NULLOK)); \
299 kprintf("%s %d: %p <- KMALLOC(%s, %d)\n", \
300 __FILE__, __LINE__, (p), #t, n); \
305 kprintf("%s %d: %p -> KFREE()\n", __FILE__, __LINE__, (p)); \
306 kfree((caddr_t)(p), M_SECA); \
311 * set parameters into secpolicyindex buffer.
312 * Must allocate secpolicyindex buffer passed to this function.
314 #define KEY_SETSECSPIDX(_dir, s, d, ps, pd, ulp, idx) \
316 bzero((idx), sizeof(struct secpolicyindex)); \
317 (idx)->dir = (_dir); \
318 (idx)->prefs = (ps); \
319 (idx)->prefd = (pd); \
320 (idx)->ul_proto = (ulp); \
321 bcopy((s), &(idx)->src, ((const struct sockaddr *)(s))->sa_len); \
322 bcopy((d), &(idx)->dst, ((const struct sockaddr *)(d))->sa_len); \
326 * set parameters into secasindex buffer.
327 * Must allocate secasindex buffer before calling this function.
329 #define KEY_SETSECASIDX(p, m, r, s, d, idx) \
331 bzero((idx), sizeof(struct secasindex)); \
332 (idx)->proto = (p); \
334 (idx)->reqid = (r); \
335 bcopy((s), &(idx)->src, ((const struct sockaddr *)(s))->sa_len); \
336 bcopy((d), &(idx)->dst, ((const struct sockaddr *)(d))->sa_len); \
341 u_long getspi_count; /* the avarage of count to try to get new SPI */
345 struct sadb_msg *msg;
346 struct sadb_ext *ext[SADB_EXT_MAX + 1];
347 int extoff[SADB_EXT_MAX + 1];
348 int extlen[SADB_EXT_MAX + 1];
351 static struct secasvar *key_allocsa_policy (const struct secasindex *);
352 static void key_freesp_so (struct secpolicy **);
353 static struct secasvar *key_do_allocsa_policy (struct secashead *, u_int);
354 static void key_delsp (struct secpolicy *);
355 static struct secpolicy *key_getsp (struct secpolicyindex *);
356 static struct secpolicy *key_getspbyid (u_int32_t);
357 static u_int32_t key_newreqid (void);
358 static struct mbuf *key_gather_mbuf (struct mbuf *,
359 const struct sadb_msghdr *, int, int, ...);
360 static int key_spdadd (struct socket *, struct mbuf *,
361 const struct sadb_msghdr *);
362 static u_int32_t key_getnewspid (void);
363 static int key_spddelete (struct socket *, struct mbuf *,
364 const struct sadb_msghdr *);
365 static int key_spddelete2 (struct socket *, struct mbuf *,
366 const struct sadb_msghdr *);
367 static int key_spdget (struct socket *, struct mbuf *,
368 const struct sadb_msghdr *);
369 static int key_spdflush (struct socket *, struct mbuf *,
370 const struct sadb_msghdr *);
371 static int key_spddump (struct socket *, struct mbuf *,
372 const struct sadb_msghdr *);
373 static struct mbuf *key_setdumpsp (struct secpolicy *,
374 u_int8_t, u_int32_t, u_int32_t);
375 static u_int key_getspreqmsglen (struct secpolicy *);
376 static int key_spdexpire (struct secpolicy *);
377 static struct secashead *key_newsah (struct secasindex *);
378 static void key_delsah (struct secashead *);
379 static struct secasvar *key_newsav (struct mbuf *,
380 const struct sadb_msghdr *, struct secashead *, int *,
382 #define KEY_NEWSAV(m, sadb, sah, e) \
383 key_newsav(m, sadb, sah, e, __FILE__, __LINE__)
384 static void key_delsav (struct secasvar *);
385 static struct secashead *key_getsah (struct secasindex *);
386 static struct secasvar *key_checkspidup (struct secasindex *, u_int32_t);
387 static struct secasvar *key_getsavbyspi (struct secashead *, u_int32_t);
388 static int key_setsaval (struct secasvar *, struct mbuf *,
389 const struct sadb_msghdr *);
390 static int key_mature (struct secasvar *);
391 static struct mbuf *key_setdumpsa (struct secasvar *, u_int8_t,
392 u_int8_t, u_int32_t, u_int32_t);
393 static struct mbuf *key_setsadbmsg (u_int8_t, u_int16_t, u_int8_t,
394 u_int32_t, pid_t, u_int16_t);
395 static struct mbuf *key_setsadbsa (struct secasvar *);
396 static struct mbuf *key_setsadbaddr (u_int16_t,
397 const struct sockaddr *, u_int8_t, u_int16_t);
399 static struct mbuf *key_setsadbident (u_int16_t, u_int16_t, caddr_t,
402 static struct mbuf *key_setsadbxsa2 (u_int8_t, u_int32_t, u_int32_t);
403 static struct mbuf *key_setsadbxpolicy (u_int16_t, u_int8_t,
405 static void *key_newbuf (const void *, u_int);
407 static int key_ismyaddr6 (struct sockaddr_in6 *);
410 /* flags for key_cmpsaidx() */
411 #define CMP_HEAD 1 /* protocol, addresses. */
412 #define CMP_MODE_REQID 2 /* additionally HEAD, reqid, mode. */
413 #define CMP_REQID 3 /* additionally HEAD, reaid. */
414 #define CMP_EXACTLY 4 /* all elements. */
415 static int key_cmpsaidx
416 (const struct secasindex *, const struct secasindex *, int);
418 static int key_cmpspidx_exactly
419 (struct secpolicyindex *, struct secpolicyindex *);
420 static int key_cmpspidx_withmask
421 (struct secpolicyindex *, struct secpolicyindex *);
422 static int key_sockaddrcmp (const struct sockaddr *, const struct sockaddr *, int);
423 static int key_bbcmp (const void *, const void *, u_int);
424 static void key_srandom (void);
425 static u_int16_t key_satype2proto (u_int8_t);
426 static u_int8_t key_proto2satype (u_int16_t);
428 static int key_getspi (struct socket *, struct mbuf *,
429 const struct sadb_msghdr *);
430 static u_int32_t key_do_getnewspi (struct sadb_spirange *,
431 struct secasindex *);
432 static int key_update (struct socket *, struct mbuf *,
433 const struct sadb_msghdr *);
434 #ifdef IPSEC_DOSEQCHECK
435 static struct secasvar *key_getsavbyseq (struct secashead *, u_int32_t);
437 static int key_add (struct socket *, struct mbuf *,
438 const struct sadb_msghdr *);
439 static int key_setident (struct secashead *, struct mbuf *,
440 const struct sadb_msghdr *);
441 static struct mbuf *key_getmsgbuf_x1 (struct mbuf *,
442 const struct sadb_msghdr *);
443 static int key_delete (struct socket *, struct mbuf *,
444 const struct sadb_msghdr *);
445 static int key_get (struct socket *, struct mbuf *,
446 const struct sadb_msghdr *);
448 static void key_getcomb_setlifetime (struct sadb_comb *);
449 static struct mbuf *key_getcomb_esp (void);
450 static struct mbuf *key_getcomb_ah (void);
451 static struct mbuf *key_getcomb_ipcomp (void);
452 static struct mbuf *key_getprop (const struct secasindex *);
454 static int key_acquire (const struct secasindex *, struct secpolicy *);
455 #ifndef IPSEC_NONBLOCK_ACQUIRE
456 static struct secacq *key_newacq (const struct secasindex *);
457 static struct secacq *key_getacq (const struct secasindex *);
458 static struct secacq *key_getacqbyseq (u_int32_t);
460 static struct secspacq *key_newspacq (struct secpolicyindex *);
461 static struct secspacq *key_getspacq (struct secpolicyindex *);
462 static int key_acquire2 (struct socket *, struct mbuf *,
463 const struct sadb_msghdr *);
464 static int key_register (struct socket *, struct mbuf *,
465 const struct sadb_msghdr *);
466 static int key_expire (struct secasvar *);
467 static int key_flush (struct socket *, struct mbuf *,
468 const struct sadb_msghdr *);
469 static int key_dump (struct socket *, struct mbuf *,
470 const struct sadb_msghdr *);
471 static int key_promisc (struct socket *, struct mbuf *,
472 const struct sadb_msghdr *);
473 static int key_senderror (struct socket *, struct mbuf *, int);
474 static int key_validate_ext (const struct sadb_ext *, int);
475 static int key_align (struct mbuf *, struct sadb_msghdr *);
477 static const char *key_getfqdn (void);
478 static const char *key_getuserfqdn (void);
480 static void key_sa_chgstate (struct secasvar *, u_int8_t);
481 static struct mbuf *key_alloc_mbuf (int);
483 #define SA_ADDREF(p) do { \
485 KASSERT((p)->refcnt != 0, \
486 ("SA refcnt overflow at %s:%u", __FILE__, __LINE__)); \
488 #define SA_DELREF(p) do { \
489 KASSERT((p)->refcnt > 0, \
490 ("SA refcnt underflow at %s:%u", __FILE__, __LINE__)); \
494 #define SP_ADDREF(p) do { \
496 KASSERT((p)->refcnt != 0, \
497 ("SP refcnt overflow at %s:%u", __FILE__, __LINE__)); \
499 #define SP_DELREF(p) do { \
500 KASSERT((p)->refcnt > 0, \
501 ("SP refcnt underflow at %s:%u", __FILE__, __LINE__)); \
506 * Return 0 when there are known to be no SP's for the specified
507 * direction. Otherwise return 1. This is used by IPsec code
508 * to optimize performance.
511 key_havesp(u_int dir)
513 return (dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND ?
514 LIST_FIRST(&sptree[dir]) != NULL : 1);
517 /* %%% IPsec policy management */
519 * allocating a SP for OUTBOUND or INBOUND packet.
520 * Must call key_freesp() later.
521 * OUT: NULL: not found
522 * others: found and return the pointer.
525 key_allocsp(struct secpolicyindex *spidx, u_int dir, const char* where, int tag)
527 struct secpolicy *sp;
530 KASSERT(spidx != NULL, ("key_allocsp: null spidx"));
531 KASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
532 ("key_allocsp: invalid direction %u", dir));
534 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
535 kprintf("DP key_allocsp from %s:%u\n", where, tag));
539 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
540 kprintf("*** objects\n"); kdebug_secpolicyindex(spidx));
542 LIST_FOREACH(sp, &sptree[dir], chain) {
543 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
544 kprintf("*** in SPD\n");
545 kdebug_secpolicyindex(&sp->spidx));
547 if (sp->state == IPSEC_SPSTATE_DEAD)
549 if (key_cmpspidx_withmask(&sp->spidx, spidx))
556 KEY_CHKSPDIR(sp->spidx.dir, dir, "key_allocsp");
558 /* found a SPD entry */
559 sp->lastused = time_second;
564 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
565 kprintf("DP key_allocsp return SP:%p (ID=%u) refcnt %u\n",
566 sp, sp ? sp->id : 0, sp ? sp->refcnt : 0));
571 * allocating a SP for OUTBOUND or INBOUND packet.
572 * Must call key_freesp() later.
573 * OUT: NULL: not found
574 * others: found and return the pointer.
577 key_allocsp2(u_int32_t spi,
578 union sockaddr_union *dst,
581 const char* where, int tag)
583 struct secpolicy *sp;
586 KASSERT(dst != NULL, ("key_allocsp2: null dst"));
587 KASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
588 ("key_allocsp2: invalid direction %u", dir));
590 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
591 kprintf("DP key_allocsp2 from %s:%u\n", where, tag));
595 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
596 kprintf("*** objects\n");
597 kprintf("spi %u proto %u dir %u\n", spi, proto, dir);
598 kdebug_sockaddr(&dst->sa));
600 LIST_FOREACH(sp, &sptree[dir], chain) {
601 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
602 kprintf("*** in SPD\n");
603 kdebug_secpolicyindex(&sp->spidx));
605 if (sp->state == IPSEC_SPSTATE_DEAD)
607 /* compare simple values, then dst address */
608 if (sp->spidx.ul_proto != proto)
610 /* NB: spi's must exist and match */
611 if (!sp->req || !sp->req->sav || sp->req->sav->spi != spi)
613 if (key_sockaddrcmp(&sp->spidx.dst.sa, &dst->sa, 1) == 0)
620 KEY_CHKSPDIR(sp->spidx.dir, dir, "key_allocsp2");
622 /* found a SPD entry */
623 sp->lastused = time_second;
628 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
629 kprintf("DP key_allocsp2 return SP:%p (ID=%u) refcnt %u\n",
630 sp, sp ? sp->id : 0, sp ? sp->refcnt : 0));
635 * return a policy that matches this particular inbound packet.
639 key_gettunnel(const struct sockaddr *osrc,
640 const struct sockaddr *odst,
641 const struct sockaddr *isrc,
642 const struct sockaddr *idst,
643 const char* where, int tag)
645 struct secpolicy *sp;
646 const int dir = IPSEC_DIR_INBOUND;
648 struct ipsecrequest *r1, *r2, *p;
649 struct secpolicyindex spidx;
651 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
652 kprintf("DP key_gettunnel from %s:%u\n", where, tag));
654 if (isrc->sa_family != idst->sa_family) {
655 ipseclog((LOG_ERR, "protocol family mismatched %d != %d\n.",
656 isrc->sa_family, idst->sa_family));
662 LIST_FOREACH(sp, &sptree[dir], chain) {
663 if (sp->state == IPSEC_SPSTATE_DEAD)
667 for (p = sp->req; p; p = p->next) {
668 if (p->saidx.mode != IPSEC_MODE_TUNNEL)
675 /* here we look at address matches only */
677 if (isrc->sa_len > sizeof(spidx.src) ||
678 idst->sa_len > sizeof(spidx.dst))
680 bcopy(isrc, &spidx.src, isrc->sa_len);
681 bcopy(idst, &spidx.dst, idst->sa_len);
682 if (!key_cmpspidx_withmask(&sp->spidx, &spidx))
685 if (key_sockaddrcmp(&r1->saidx.src.sa, isrc, 0) ||
686 key_sockaddrcmp(&r1->saidx.dst.sa, idst, 0))
690 if (key_sockaddrcmp(&r2->saidx.src.sa, osrc, 0) ||
691 key_sockaddrcmp(&r2->saidx.dst.sa, odst, 0))
700 sp->lastused = time_second;
705 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
706 kprintf("DP key_gettunnel return SP:%p (ID=%u) refcnt %u\n",
707 sp, sp ? sp->id : 0, sp ? sp->refcnt : 0));
712 * allocating an SA entry for an *OUTBOUND* packet.
713 * checking each request entries in SP, and acquire an SA if need.
714 * OUT: 0: there are valid requests.
715 * ENOENT: policy may be valid, but SA with REQUIRE is on acquiring.
718 key_checkrequest(struct ipsecrequest *isr, const struct secasindex *saidx)
723 KASSERT(isr != NULL, ("key_checkrequest: null isr"));
724 KASSERT(saidx != NULL, ("key_checkrequest: null saidx"));
725 KASSERT(saidx->mode == IPSEC_MODE_TRANSPORT ||
726 saidx->mode == IPSEC_MODE_TUNNEL,
727 ("key_checkrequest: unexpected policy %u", saidx->mode));
729 /* get current level */
730 level = ipsec_get_reqlevel(isr);
733 * XXX guard against protocol callbacks from the crypto
734 * thread as they reference ipsecrequest.sav which we
735 * temporarily null out below. Need to rethink how we
736 * handle bundled SA's in the callback thread.
740 * We do allocate new SA only if the state of SA in the holder is
741 * SADB_SASTATE_DEAD. The SA for outbound must be the oldest.
743 if (isr->sav != NULL) {
744 if (isr->sav->sah == NULL)
745 panic("key_checkrequest: sah is null.");
746 if (isr->sav == (struct secasvar *)LIST_FIRST(
747 &isr->sav->sah->savtree[SADB_SASTATE_DEAD])) {
748 KEY_FREESAV(&isr->sav);
754 * we free any SA stashed in the IPsec request because a different
755 * SA may be involved each time this request is checked, either
756 * because new SAs are being configured, or this request is
757 * associated with an unconnected datagram socket, or this request
758 * is associated with a system default policy.
760 * The operation may have negative impact to performance. We may
761 * want to check cached SA carefully, rather than picking new SA
764 if (isr->sav != NULL) {
765 KEY_FREESAV(&isr->sav);
771 * new SA allocation if no SA found.
772 * key_allocsa_policy should allocate the oldest SA available.
773 * See key_do_allocsa_policy(), and draft-jenkins-ipsec-rekeying-03.txt.
775 if (isr->sav == NULL)
776 isr->sav = key_allocsa_policy(saidx);
778 /* When there is SA. */
779 if (isr->sav != NULL) {
780 if (isr->sav->state != SADB_SASTATE_MATURE &&
781 isr->sav->state != SADB_SASTATE_DYING)
787 error = key_acquire(saidx, isr->sp);
789 /* XXX What should I do ? */
790 ipseclog((LOG_DEBUG, "key_checkrequest: error %d returned "
791 "from key_acquire.\n", error));
795 if (level != IPSEC_LEVEL_REQUIRE) {
796 /* XXX sigh, the interface to this routine is botched */
797 KASSERT(isr->sav == NULL, ("key_checkrequest: unexpected SA"));
805 * allocating a SA for policy entry from SAD.
806 * NOTE: searching SAD of aliving state.
807 * OUT: NULL: not found.
808 * others: found and return the pointer.
810 static struct secasvar *
811 key_allocsa_policy(const struct secasindex *saidx)
813 struct secashead *sah;
814 struct secasvar *sav;
815 u_int stateidx, state;
817 LIST_FOREACH(sah, &sahtree, chain) {
818 if (sah->state == SADB_SASTATE_DEAD)
820 if (key_cmpsaidx(&sah->saidx, saidx, CMP_MODE_REQID))
828 /* search valid state */
830 stateidx < NELEM(saorder_state_valid);
833 state = saorder_state_valid[stateidx];
835 sav = key_do_allocsa_policy(sah, state);
844 * searching SAD with direction, protocol, mode and state.
845 * called by key_allocsa_policy().
848 * others : found, pointer to a SA.
850 static struct secasvar *
851 key_do_allocsa_policy(struct secashead *sah, u_int state)
853 struct secasvar *sav, *nextsav, *candidate = NULL, *d;
855 LIST_FOREACH_MUTABLE(sav, &sah->savtree[state], chain, nextsav) {
857 KEY_CHKSASTATE(sav->state, state, "key_do_allocsa_policy");
860 if (candidate == NULL) {
865 /* Which SA is the better ? */
868 if (candidate->lft_c == NULL || sav->lft_c == NULL)
869 panic("key_do_allocsa_policy: "
870 "lifetime_current is NULL.\n");
872 /* What the best method is to compare ? */
873 if (key_prefered_oldsa) {
874 if (candidate->lft_c->sadb_lifetime_addtime >
875 sav->lft_c->sadb_lifetime_addtime) {
881 /* prefered new sa rather than old sa */
882 if (candidate->lft_c->sadb_lifetime_addtime <
883 sav->lft_c->sadb_lifetime_addtime) {
890 * prepared to delete the SA when there is more
891 * suitable candidate and the lifetime of the SA is not
894 if (d->lft_c->sadb_lifetime_addtime != 0) {
895 struct mbuf *m, *result;
898 key_sa_chgstate(d, SADB_SASTATE_DEAD);
900 KASSERT(d->refcnt > 0,
901 ("key_do_allocsa_policy: bogus ref count"));
903 satype = key_proto2satype(d->sah->saidx.proto);
907 m = key_setsadbmsg(SADB_DELETE, 0, satype, 0, 0,
913 /* set sadb_address for saidx's. */
914 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
915 &d->sah->saidx.src.sa,
916 d->sah->saidx.src.sa.sa_len << 3,
922 /* set sadb_address for saidx's. */
923 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
924 &d->sah->saidx.dst.sa,
925 d->sah->saidx.dst.sa.sa_len << 3,
931 /* create SA extension */
932 m = key_setsadbsa(d);
937 if (result->m_len < sizeof(struct sadb_msg)) {
938 result = m_pullup(result,
939 sizeof(struct sadb_msg));
944 result->m_pkthdr.len = m_lengthm(result, NULL);
945 mtod(result, struct sadb_msg *)->sadb_msg_len =
946 PFKEY_UNIT64(result->m_pkthdr.len);
948 if (key_sendup_mbuf(NULL, result,
949 KEY_SENDUP_REGISTERED))
957 SA_ADDREF(candidate);
958 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
959 kprintf("DP allocsa_policy cause refcnt++:%d SA:%p\n",
960 candidate->refcnt, candidate));
966 * allocating a usable SA entry for a *INBOUND* packet.
967 * Must call key_freesav() later.
968 * OUT: positive: pointer to a usable sav (i.e. MATURE or DYING state).
969 * NULL: not found, or error occured.
971 * In the comparison, no source address is used--for RFC2401 conformance.
972 * To quote, from section 4.1:
973 * A security association is uniquely identified by a triple consisting
974 * of a Security Parameter Index (SPI), an IP Destination Address, and a
975 * security protocol (AH or ESP) identifier.
976 * Note that, however, we do need to keep source address in IPsec SA.
977 * IKE specification and PF_KEY specification do assume that we
978 * keep source address in IPsec SA. We see a tricky situation here.
982 union sockaddr_union *dst,
985 const char* where, int tag)
987 struct secashead *sah;
988 struct secasvar *sav;
989 u_int stateidx, state;
992 KASSERT(dst != NULL, ("key_allocsa: null dst address"));
994 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
995 kprintf("DP key_allocsa from %s:%u\n", where, tag));
999 * XXX: to be checked internal IP header somewhere. Also when
1000 * IPsec tunnel packet is received. But ESP tunnel mode is
1001 * encrypted so we can't check internal IP header.
1004 LIST_FOREACH(sah, &sahtree, chain) {
1005 /* search valid state */
1007 stateidx < NELEM(saorder_state_valid);
1009 state = saorder_state_valid[stateidx];
1010 LIST_FOREACH(sav, &sah->savtree[state], chain) {
1012 KEY_CHKSASTATE(sav->state, state, "key_allocsav");
1013 /* do not return entries w/ unusable state */
1014 if (sav->state != SADB_SASTATE_MATURE &&
1015 sav->state != SADB_SASTATE_DYING)
1017 if (proto != sav->sah->saidx.proto)
1019 if (spi != sav->spi)
1021 #if 0 /* don't check src */
1022 /* check src address */
1023 if (key_sockaddrcmp(&src->sa, &sav->sah->saidx.src.sa, 0) != 0)
1026 /* check dst address */
1027 if (key_sockaddrcmp(&dst->sa, &sav->sah->saidx.dst.sa, 0) != 0)
1038 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1039 kprintf("DP key_allocsa return SA:%p; refcnt %u\n",
1040 sav, sav ? sav->refcnt : 0));
1045 * Must be called after calling key_allocsp().
1046 * For both the packet without socket and key_freeso().
1049 _key_freesp(struct secpolicy **spp, const char* where, int tag)
1051 struct secpolicy *sp = *spp;
1053 KASSERT(sp != NULL, ("key_freesp: null sp"));
1057 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1058 kprintf("DP key_freesp SP:%p (ID=%u) from %s:%u; refcnt now %u\n",
1059 sp, sp->id, where, tag, sp->refcnt));
1061 if (sp->refcnt == 0) {
1068 * Must be called after calling key_allocsp().
1069 * For the packet with socket.
1072 key_freeso(struct socket *so)
1075 KASSERT(so != NULL, ("key_freeso: null so"));
1077 switch (so->so_proto->pr_domain->dom_family) {
1081 struct inpcb *pcb = so->so_pcb;
1083 /* Does it have a PCB ? */
1086 key_freesp_so(&pcb->inp_sp->sp_in);
1087 key_freesp_so(&pcb->inp_sp->sp_out);
1094 #ifdef HAVE_NRL_INPCB
1095 struct inpcb *pcb = so->so_pcb;
1097 /* Does it have a PCB ? */
1100 key_freesp_so(&pcb->inp_sp->sp_in);
1101 key_freesp_so(&pcb->inp_sp->sp_out);
1103 struct in6pcb *pcb = so->so_pcb;
1105 /* Does it have a PCB ? */
1108 key_freesp_so(&pcb->in6p_sp->sp_in);
1109 key_freesp_so(&pcb->in6p_sp->sp_out);
1115 ipseclog((LOG_DEBUG, "key_freeso: unknown address family=%d.\n",
1116 so->so_proto->pr_domain->dom_family));
1122 key_freesp_so(struct secpolicy **sp)
1124 KASSERT(sp != NULL && *sp != NULL, ("key_freesp_so: null sp"));
1126 if ((*sp)->policy == IPSEC_POLICY_ENTRUST ||
1127 (*sp)->policy == IPSEC_POLICY_BYPASS)
1130 KASSERT((*sp)->policy == IPSEC_POLICY_IPSEC,
1131 ("key_freesp_so: invalid policy %u", (*sp)->policy));
1136 * Must be called after calling key_allocsa().
1137 * This function is called by key_freesp() to free some SA allocated
1141 key_freesav(struct secasvar **psav, const char* where, int tag)
1143 struct secasvar *sav = *psav;
1145 KASSERT(sav != NULL, ("key_freesav: null sav"));
1149 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1150 kprintf("DP key_freesav SA:%p (SPI %u) from %s:%u; refcnt now %u\n",
1151 sav, ntohl(sav->spi), where, tag, sav->refcnt));
1153 if (sav->refcnt == 0) {
1159 /* %%% SPD management */
1161 * free security policy entry.
1164 key_delsp(struct secpolicy *sp)
1168 KASSERT(sp != NULL, ("key_delsp: null sp"));
1170 sp->state = IPSEC_SPSTATE_DEAD;
1172 KASSERT(sp->refcnt == 0,
1173 ("key_delsp: SP with references deleted (refcnt %u)",
1177 /* remove from SP index */
1178 if (__LIST_CHAINED(sp))
1179 LIST_REMOVE(sp, chain);
1182 struct ipsecrequest *isr = sp->req, *nextisr;
1184 while (isr != NULL) {
1185 if (isr->sav != NULL) {
1186 KEY_FREESAV(&isr->sav);
1190 nextisr = isr->next;
1203 * OUT: NULL : not found
1204 * others : found, pointer to a SP.
1206 static struct secpolicy *
1207 key_getsp(struct secpolicyindex *spidx)
1209 struct secpolicy *sp;
1211 KASSERT(spidx != NULL, ("key_getsp: null spidx"));
1213 LIST_FOREACH(sp, &sptree[spidx->dir], chain) {
1214 if (sp->state == IPSEC_SPSTATE_DEAD)
1216 if (key_cmpspidx_exactly(spidx, &sp->spidx)) {
1227 * OUT: NULL : not found
1228 * others : found, pointer to a SP.
1230 static struct secpolicy *
1231 key_getspbyid(u_int32_t id)
1233 struct secpolicy *sp;
1235 LIST_FOREACH(sp, &sptree[IPSEC_DIR_INBOUND], chain) {
1236 if (sp->state == IPSEC_SPSTATE_DEAD)
1244 LIST_FOREACH(sp, &sptree[IPSEC_DIR_OUTBOUND], chain) {
1245 if (sp->state == IPSEC_SPSTATE_DEAD)
1257 key_newsp(const char* where, int tag)
1259 struct secpolicy *newsp = NULL;
1261 newsp = kmalloc(sizeof(struct secpolicy), M_SECA,
1262 M_INTWAIT | M_ZERO | M_NULLOK);
1268 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1269 kprintf("DP key_newsp from %s:%u return SP:%p\n",
1270 where, tag, newsp));
1275 * create secpolicy structure from sadb_x_policy structure.
1276 * NOTE: `state', `secpolicyindex' in secpolicy structure are not set,
1277 * so must be set properly later.
1280 key_msg2sp(struct sadb_x_policy *xpl0, size_t len, int *error)
1282 struct secpolicy *newsp;
1286 panic("key_msg2sp: NULL pointer was passed.");
1287 if (len < sizeof(*xpl0))
1288 panic("key_msg2sp: invalid length.");
1289 if (len != PFKEY_EXTLEN(xpl0)) {
1290 ipseclog((LOG_DEBUG, "key_msg2sp: Invalid msg length.\n"));
1295 if ((newsp = KEY_NEWSP()) == NULL) {
1300 newsp->spidx.dir = xpl0->sadb_x_policy_dir;
1301 newsp->policy = xpl0->sadb_x_policy_type;
1304 switch (xpl0->sadb_x_policy_type) {
1305 case IPSEC_POLICY_DISCARD:
1306 case IPSEC_POLICY_NONE:
1307 case IPSEC_POLICY_ENTRUST:
1308 case IPSEC_POLICY_BYPASS:
1312 case IPSEC_POLICY_IPSEC:
1315 struct sadb_x_ipsecrequest *xisr;
1316 struct ipsecrequest **p_isr = &newsp->req;
1318 /* validity check */
1319 if (PFKEY_EXTLEN(xpl0) < sizeof(*xpl0)) {
1320 ipseclog((LOG_DEBUG,
1321 "key_msg2sp: Invalid msg length.\n"));
1327 tlen = PFKEY_EXTLEN(xpl0) - sizeof(*xpl0);
1328 xisr = (struct sadb_x_ipsecrequest *)(xpl0 + 1);
1332 if (xisr->sadb_x_ipsecrequest_len < sizeof(*xisr)) {
1333 ipseclog((LOG_DEBUG, "key_msg2sp: "
1334 "invalid ipsecrequest length.\n"));
1340 /* allocate request buffer */
1341 KMALLOC(*p_isr, struct ipsecrequest *, sizeof(**p_isr));
1342 if ((*p_isr) == NULL) {
1343 ipseclog((LOG_DEBUG,
1344 "key_msg2sp: No more memory.\n"));
1349 bzero(*p_isr, sizeof(**p_isr));
1352 (*p_isr)->next = NULL;
1354 switch (xisr->sadb_x_ipsecrequest_proto) {
1357 case IPPROTO_IPCOMP:
1360 ipseclog((LOG_DEBUG,
1361 "key_msg2sp: invalid proto type=%u\n",
1362 xisr->sadb_x_ipsecrequest_proto));
1364 *error = EPROTONOSUPPORT;
1367 (*p_isr)->saidx.proto = xisr->sadb_x_ipsecrequest_proto;
1369 switch (xisr->sadb_x_ipsecrequest_mode) {
1370 case IPSEC_MODE_TRANSPORT:
1371 case IPSEC_MODE_TUNNEL:
1373 case IPSEC_MODE_ANY:
1375 ipseclog((LOG_DEBUG,
1376 "key_msg2sp: invalid mode=%u\n",
1377 xisr->sadb_x_ipsecrequest_mode));
1382 (*p_isr)->saidx.mode = xisr->sadb_x_ipsecrequest_mode;
1384 switch (xisr->sadb_x_ipsecrequest_level) {
1385 case IPSEC_LEVEL_DEFAULT:
1386 case IPSEC_LEVEL_USE:
1387 case IPSEC_LEVEL_REQUIRE:
1389 case IPSEC_LEVEL_UNIQUE:
1390 /* validity check */
1392 * If range violation of reqid, kernel will
1393 * update it, don't refuse it.
1395 if (xisr->sadb_x_ipsecrequest_reqid
1396 > IPSEC_MANUAL_REQID_MAX) {
1397 ipseclog((LOG_DEBUG,
1398 "key_msg2sp: reqid=%d range "
1399 "violation, updated by kernel.\n",
1400 xisr->sadb_x_ipsecrequest_reqid));
1401 xisr->sadb_x_ipsecrequest_reqid = 0;
1404 /* allocate new reqid id if reqid is zero. */
1405 if (xisr->sadb_x_ipsecrequest_reqid == 0) {
1407 if ((reqid = key_newreqid()) == 0) {
1412 (*p_isr)->saidx.reqid = reqid;
1413 xisr->sadb_x_ipsecrequest_reqid = reqid;
1415 /* set it for manual keying. */
1416 (*p_isr)->saidx.reqid =
1417 xisr->sadb_x_ipsecrequest_reqid;
1422 ipseclog((LOG_DEBUG, "key_msg2sp: invalid level=%u\n",
1423 xisr->sadb_x_ipsecrequest_level));
1428 (*p_isr)->level = xisr->sadb_x_ipsecrequest_level;
1430 /* set IP addresses if there */
1431 if (xisr->sadb_x_ipsecrequest_len > sizeof(*xisr)) {
1432 struct sockaddr *paddr;
1434 paddr = (struct sockaddr *)(xisr + 1);
1436 /* validity check */
1438 > sizeof((*p_isr)->saidx.src)) {
1439 ipseclog((LOG_DEBUG, "key_msg2sp: invalid request "
1440 "address length.\n"));
1445 bcopy(paddr, &(*p_isr)->saidx.src,
1448 paddr = (struct sockaddr *)((caddr_t)paddr
1451 /* validity check */
1453 > sizeof((*p_isr)->saidx.dst)) {
1454 ipseclog((LOG_DEBUG, "key_msg2sp: invalid request "
1455 "address length.\n"));
1460 bcopy(paddr, &(*p_isr)->saidx.dst,
1464 (*p_isr)->sav = NULL;
1465 (*p_isr)->sp = newsp;
1467 /* initialization for the next. */
1468 p_isr = &(*p_isr)->next;
1469 tlen -= xisr->sadb_x_ipsecrequest_len;
1471 /* validity check */
1473 ipseclog((LOG_DEBUG, "key_msg2sp: becoming tlen < 0.\n"));
1479 xisr = (struct sadb_x_ipsecrequest *)((caddr_t)xisr
1480 + xisr->sadb_x_ipsecrequest_len);
1485 ipseclog((LOG_DEBUG, "key_msg2sp: invalid policy type.\n"));
1498 static u_int32_t auto_reqid = IPSEC_MANUAL_REQID_MAX + 1;
1500 auto_reqid = (auto_reqid == ~0
1501 ? IPSEC_MANUAL_REQID_MAX + 1 : auto_reqid + 1);
1503 /* XXX should be unique check */
1509 * copy secpolicy struct to sadb_x_policy structure indicated.
1512 key_sp2msg(struct secpolicy *sp)
1514 struct sadb_x_policy *xpl;
1521 panic("key_sp2msg: NULL pointer was passed.");
1523 tlen = key_getspreqmsglen(sp);
1525 m = key_alloc_mbuf(tlen);
1526 if (!m || m->m_next) { /*XXX*/
1534 xpl = mtod(m, struct sadb_x_policy *);
1537 xpl->sadb_x_policy_len = PFKEY_UNIT64(tlen);
1538 xpl->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
1539 xpl->sadb_x_policy_type = sp->policy;
1540 xpl->sadb_x_policy_dir = sp->spidx.dir;
1541 xpl->sadb_x_policy_id = sp->id;
1542 p = (caddr_t)xpl + sizeof(*xpl);
1544 /* if is the policy for ipsec ? */
1545 if (sp->policy == IPSEC_POLICY_IPSEC) {
1546 struct sadb_x_ipsecrequest *xisr;
1547 struct ipsecrequest *isr;
1549 for (isr = sp->req; isr != NULL; isr = isr->next) {
1551 xisr = (struct sadb_x_ipsecrequest *)p;
1553 xisr->sadb_x_ipsecrequest_proto = isr->saidx.proto;
1554 xisr->sadb_x_ipsecrequest_mode = isr->saidx.mode;
1555 xisr->sadb_x_ipsecrequest_level = isr->level;
1556 xisr->sadb_x_ipsecrequest_reqid = isr->saidx.reqid;
1559 bcopy(&isr->saidx.src, p, isr->saidx.src.sa.sa_len);
1560 p += isr->saidx.src.sa.sa_len;
1561 bcopy(&isr->saidx.dst, p, isr->saidx.dst.sa.sa_len);
1562 p += isr->saidx.src.sa.sa_len;
1564 xisr->sadb_x_ipsecrequest_len =
1565 PFKEY_ALIGN8(sizeof(*xisr)
1566 + isr->saidx.src.sa.sa_len
1567 + isr->saidx.dst.sa.sa_len);
1574 /* m will not be freed nor modified */
1575 static struct mbuf *
1576 key_gather_mbuf(struct mbuf *m, const struct sadb_msghdr *mhp,
1577 int ndeep, int nitem, ...)
1582 struct mbuf *result = NULL, *n;
1585 if (m == NULL || mhp == NULL)
1586 panic("null pointer passed to key_gather");
1588 __va_start(ap, nitem);
1589 for (i = 0; i < nitem; i++) {
1590 idx = __va_arg(ap, int);
1591 if (idx < 0 || idx > SADB_EXT_MAX)
1593 /* don't attempt to pull empty extension */
1594 if (idx == SADB_EXT_RESERVED && mhp->msg == NULL)
1596 if (idx != SADB_EXT_RESERVED &&
1597 (mhp->ext[idx] == NULL || mhp->extlen[idx] == 0))
1600 if (idx == SADB_EXT_RESERVED) {
1601 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
1604 panic("assumption failed");
1606 MGETHDR(n, MB_DONTWAIT, MT_DATA);
1611 m_copydata(m, 0, sizeof(struct sadb_msg),
1613 } else if (i < ndeep) {
1614 len = mhp->extlen[idx];
1615 n = key_alloc_mbuf(len);
1616 if (!n || n->m_next) { /*XXX*/
1621 m_copydata(m, mhp->extoff[idx], mhp->extlen[idx],
1624 n = m_copym(m, mhp->extoff[idx], mhp->extlen[idx],
1637 if (result->m_flags & M_PKTHDR)
1638 result->m_pkthdr.len = m_lengthm(result, NULL);
1648 * SADB_X_SPDADD, SADB_X_SPDSETIDX or SADB_X_SPDUPDATE processing
1649 * add an entry to SP database, when received
1650 * <base, address(SD), (lifetime(H),) policy>
1652 * Adding to SP database,
1654 * <base, address(SD), (lifetime(H),) policy>
1655 * to the socket which was send.
1657 * SPDADD set a unique policy entry.
1658 * SPDSETIDX like SPDADD without a part of policy requests.
1659 * SPDUPDATE replace a unique policy entry.
1661 * m will always be freed.
1664 key_spdadd(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
1666 struct sadb_address *src0, *dst0;
1667 struct sadb_x_policy *xpl0, *xpl;
1668 struct sadb_lifetime *lft = NULL;
1669 struct secpolicyindex spidx;
1670 struct secpolicy *newsp;
1671 struct sockaddr *saddr, *daddr;
1675 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
1676 panic("key_spdadd: NULL pointer is passed.");
1678 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
1679 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
1680 mhp->ext[SADB_X_EXT_POLICY] == NULL) {
1681 ipseclog((LOG_DEBUG, "key_spdadd: invalid message is passed.\n"));
1682 return key_senderror(so, m, EINVAL);
1684 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
1685 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
1686 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
1687 ipseclog((LOG_DEBUG, "key_spdadd: invalid message is passed.\n"));
1688 return key_senderror(so, m, EINVAL);
1690 if (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL) {
1691 if (mhp->extlen[SADB_EXT_LIFETIME_HARD]
1692 < sizeof(struct sadb_lifetime)) {
1693 ipseclog((LOG_DEBUG, "key_spdadd: invalid message is passed.\n"));
1694 return key_senderror(so, m, EINVAL);
1696 lft = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_HARD];
1699 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
1700 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
1701 xpl0 = (struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY];
1704 /* XXX boundary check against sa_len */
1705 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
1708 src0->sadb_address_prefixlen,
1709 dst0->sadb_address_prefixlen,
1710 src0->sadb_address_proto,
1713 /* checking the direciton. */
1714 switch (xpl0->sadb_x_policy_dir) {
1715 case IPSEC_DIR_INBOUND:
1716 case IPSEC_DIR_OUTBOUND:
1719 ipseclog((LOG_DEBUG, "key_spdadd: Invalid SP direction.\n"));
1720 mhp->msg->sadb_msg_errno = EINVAL;
1725 /* key_spdadd() accepts DISCARD, NONE and IPSEC. */
1726 if (xpl0->sadb_x_policy_type == IPSEC_POLICY_ENTRUST
1727 || xpl0->sadb_x_policy_type == IPSEC_POLICY_BYPASS) {
1728 ipseclog((LOG_DEBUG, "key_spdadd: Invalid policy type.\n"));
1729 return key_senderror(so, m, EINVAL);
1732 /* policy requests are mandatory when action is ipsec. */
1733 if (mhp->msg->sadb_msg_type != SADB_X_SPDSETIDX
1734 && xpl0->sadb_x_policy_type == IPSEC_POLICY_IPSEC
1735 && mhp->extlen[SADB_X_EXT_POLICY] <= sizeof(*xpl0)) {
1736 ipseclog((LOG_DEBUG, "key_spdadd: some policy requests part required.\n"));
1737 return key_senderror(so, m, EINVAL);
1741 * checking there is SP already or not.
1742 * SPDUPDATE doesn't depend on whether there is a SP or not.
1743 * If the type is either SPDADD or SPDSETIDX AND a SP is found,
1746 newsp = key_getsp(&spidx);
1747 if (mhp->msg->sadb_msg_type == SADB_X_SPDUPDATE) {
1749 newsp->state = IPSEC_SPSTATE_DEAD;
1753 if (newsp != NULL) {
1755 ipseclog((LOG_DEBUG, "key_spdadd: a SP entry exists already.\n"));
1756 return key_senderror(so, m, EEXIST);
1760 /* allocation new SP entry */
1761 if ((newsp = key_msg2sp(xpl0, PFKEY_EXTLEN(xpl0), &error)) == NULL) {
1762 return key_senderror(so, m, error);
1765 if ((newsp->id = key_getnewspid()) == 0) {
1767 return key_senderror(so, m, ENOBUFS);
1770 /* XXX boundary check against sa_len */
1771 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
1774 src0->sadb_address_prefixlen,
1775 dst0->sadb_address_prefixlen,
1776 src0->sadb_address_proto,
1779 /* sanity check on addr pair */
1780 saddr = (struct sockaddr *)(src0 + 1);
1781 daddr = (struct sockaddr *)(dst0 + 1);
1782 if (saddr->sa_family != daddr->sa_family) {
1784 return key_senderror(so, m, EINVAL);
1786 if (saddr->sa_len != daddr->sa_len) {
1788 return key_senderror(so, m, EINVAL);
1791 if (newsp->req && newsp->req->saidx.src.sa.sa_family) {
1792 if (saddr->sa_family != newsp->req->saidx.src.sa.sa_family) {
1794 return key_senderror(so, m, EINVAL);
1797 if (newsp->req && newsp->req->saidx.dst.sa.sa_family) {
1798 if (daddr->sa_family != newsp->req->saidx.dst.sa.sa_family) {
1800 return key_senderror(so, m, EINVAL);
1805 newsp->created = time_second;
1806 newsp->lastused = newsp->created;
1807 newsp->lifetime = lft ? lft->sadb_lifetime_addtime : 0;
1808 newsp->validtime = lft ? lft->sadb_lifetime_usetime : 0;
1810 newsp->refcnt = 1; /* do not reclaim until I say I do */
1811 newsp->state = IPSEC_SPSTATE_ALIVE;
1812 LIST_INSERT_TAIL(&sptree[newsp->spidx.dir], newsp, secpolicy, chain);
1814 /* delete the entry in spacqtree */
1815 if (mhp->msg->sadb_msg_type == SADB_X_SPDUPDATE) {
1816 struct secspacq *spacq;
1817 if ((spacq = key_getspacq(&spidx)) != NULL) {
1818 /* reset counter in order to deletion by timehandler. */
1819 spacq->created = time_second;
1825 struct mbuf *n, *mpolicy;
1826 struct sadb_msg *newmsg;
1829 /* create new sadb_msg to reply. */
1831 n = key_gather_mbuf(m, mhp, 2, 5, SADB_EXT_RESERVED,
1832 SADB_X_EXT_POLICY, SADB_EXT_LIFETIME_HARD,
1833 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
1835 n = key_gather_mbuf(m, mhp, 2, 4, SADB_EXT_RESERVED,
1837 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
1840 return key_senderror(so, m, ENOBUFS);
1842 if (n->m_len < sizeof(*newmsg)) {
1843 n = m_pullup(n, sizeof(*newmsg));
1845 return key_senderror(so, m, ENOBUFS);
1847 newmsg = mtod(n, struct sadb_msg *);
1848 newmsg->sadb_msg_errno = 0;
1849 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
1852 mpolicy = m_pulldown(n, PFKEY_ALIGN8(sizeof(struct sadb_msg)),
1853 sizeof(*xpl), &off);
1854 if (mpolicy == NULL) {
1855 /* n is already freed */
1856 return key_senderror(so, m, ENOBUFS);
1858 xpl = (struct sadb_x_policy *)(mtod(mpolicy, caddr_t) + off);
1859 if (xpl->sadb_x_policy_exttype != SADB_X_EXT_POLICY) {
1861 return key_senderror(so, m, EINVAL);
1863 xpl->sadb_x_policy_id = newsp->id;
1866 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
1871 * get new policy id.
1877 key_getnewspid(void)
1879 u_int32_t newid = 0;
1880 int count = key_spi_trycnt; /* XXX */
1881 struct secpolicy *sp;
1883 /* when requesting to allocate spi ranged */
1885 newid = (policy_id = (policy_id == ~0 ? 1 : policy_id + 1));
1887 if ((sp = key_getspbyid(newid)) == NULL)
1893 if (count == 0 || newid == 0) {
1894 ipseclog((LOG_DEBUG, "key_getnewspid: to allocate policy id is failed.\n"));
1902 * SADB_SPDDELETE processing
1904 * <base, address(SD), policy(*)>
1905 * from the user(?), and set SADB_SASTATE_DEAD,
1907 * <base, address(SD), policy(*)>
1909 * policy(*) including direction of policy.
1911 * m will always be freed.
1914 key_spddelete(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
1916 struct sadb_address *src0, *dst0;
1917 struct sadb_x_policy *xpl0;
1918 struct secpolicyindex spidx;
1919 struct secpolicy *sp;
1922 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
1923 panic("key_spddelete: NULL pointer is passed.");
1925 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
1926 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
1927 mhp->ext[SADB_X_EXT_POLICY] == NULL) {
1928 ipseclog((LOG_DEBUG, "key_spddelete: invalid message is passed.\n"));
1929 return key_senderror(so, m, EINVAL);
1931 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
1932 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
1933 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
1934 ipseclog((LOG_DEBUG, "key_spddelete: invalid message is passed.\n"));
1935 return key_senderror(so, m, EINVAL);
1938 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
1939 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
1940 xpl0 = (struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY];
1943 /* XXX boundary check against sa_len */
1944 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
1947 src0->sadb_address_prefixlen,
1948 dst0->sadb_address_prefixlen,
1949 src0->sadb_address_proto,
1952 /* checking the direciton. */
1953 switch (xpl0->sadb_x_policy_dir) {
1954 case IPSEC_DIR_INBOUND:
1955 case IPSEC_DIR_OUTBOUND:
1958 ipseclog((LOG_DEBUG, "key_spddelete: Invalid SP direction.\n"));
1959 return key_senderror(so, m, EINVAL);
1962 /* Is there SP in SPD ? */
1963 if ((sp = key_getsp(&spidx)) == NULL) {
1964 ipseclog((LOG_DEBUG, "key_spddelete: no SP found.\n"));
1965 return key_senderror(so, m, EINVAL);
1968 /* save policy id to buffer to be returned. */
1969 xpl0->sadb_x_policy_id = sp->id;
1971 sp->state = IPSEC_SPSTATE_DEAD;
1976 struct sadb_msg *newmsg;
1978 /* create new sadb_msg to reply. */
1979 n = key_gather_mbuf(m, mhp, 1, 4, SADB_EXT_RESERVED,
1980 SADB_X_EXT_POLICY, SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
1982 return key_senderror(so, m, ENOBUFS);
1984 newmsg = mtod(n, struct sadb_msg *);
1985 newmsg->sadb_msg_errno = 0;
1986 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
1989 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
1994 * SADB_SPDDELETE2 processing
1997 * from the user(?), and set SADB_SASTATE_DEAD,
2001 * policy(*) including direction of policy.
2003 * m will always be freed.
2006 key_spddelete2(struct socket *so, struct mbuf *m,
2007 const struct sadb_msghdr *mhp)
2010 struct secpolicy *sp;
2013 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
2014 panic("key_spddelete2: NULL pointer is passed.");
2016 if (mhp->ext[SADB_X_EXT_POLICY] == NULL ||
2017 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2018 ipseclog((LOG_DEBUG, "key_spddelete2: invalid message is passed.\n"));
2019 key_senderror(so, m, EINVAL);
2023 id = ((struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY])->sadb_x_policy_id;
2025 /* Is there SP in SPD ? */
2026 if ((sp = key_getspbyid(id)) == NULL) {
2027 ipseclog((LOG_DEBUG, "key_spddelete2: no SP found id:%u.\n", id));
2028 key_senderror(so, m, EINVAL);
2031 sp->state = IPSEC_SPSTATE_DEAD;
2036 struct sadb_msg *newmsg;
2039 /* create new sadb_msg to reply. */
2040 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
2043 return key_senderror(so, m, ENOBUFS);
2044 n = m_getb(len, MB_DONTWAIT, MT_DATA, M_PKTHDR);
2046 return key_senderror(so, m, ENOBUFS);
2049 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t));
2050 off = PFKEY_ALIGN8(sizeof(struct sadb_msg));
2054 panic("length inconsistency in key_spddelete2");
2057 n->m_next = m_copym(m, mhp->extoff[SADB_X_EXT_POLICY],
2058 mhp->extlen[SADB_X_EXT_POLICY], MB_DONTWAIT);
2061 return key_senderror(so, m, ENOBUFS);
2063 n->m_pkthdr.len = m_lengthm(n, NULL);
2065 newmsg = mtod(n, struct sadb_msg *);
2066 newmsg->sadb_msg_errno = 0;
2067 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
2070 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
2075 * SADB_X_GET processing
2080 * <base, address(SD), policy>
2082 * policy(*) including direction of policy.
2084 * m will always be freed.
2087 key_spdget(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
2090 struct secpolicy *sp;
2094 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
2095 panic("key_spdget: NULL pointer is passed.");
2097 if (mhp->ext[SADB_X_EXT_POLICY] == NULL ||
2098 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2099 ipseclog((LOG_DEBUG, "key_spdget: invalid message is passed.\n"));
2100 return key_senderror(so, m, EINVAL);
2103 id = ((struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY])->sadb_x_policy_id;
2105 /* Is there SP in SPD ? */
2106 if ((sp = key_getspbyid(id)) == NULL) {
2107 ipseclog((LOG_DEBUG, "key_spdget: no SP found id:%u.\n", id));
2108 return key_senderror(so, m, ENOENT);
2111 n = key_setdumpsp(sp, SADB_X_SPDGET, 0, mhp->msg->sadb_msg_pid);
2114 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
2116 return key_senderror(so, m, ENOBUFS);
2120 * SADB_X_SPDACQUIRE processing.
2121 * Acquire policy and SA(s) for a *OUTBOUND* packet.
2124 * to KMD, and expect to receive
2125 * <base> with SADB_X_SPDACQUIRE if error occured,
2128 * with SADB_X_SPDUPDATE from KMD by PF_KEY.
2129 * policy(*) is without policy requests.
2132 * others: error number
2135 key_spdacquire(struct secpolicy *sp)
2137 struct mbuf *result = NULL, *m;
2138 struct secspacq *newspacq;
2143 panic("key_spdacquire: NULL pointer is passed.");
2144 if (sp->req != NULL)
2145 panic("key_spdacquire: called but there is request.");
2146 if (sp->policy != IPSEC_POLICY_IPSEC)
2147 panic("key_spdacquire: policy mismatched. IPsec is expected.");
2149 /* Get an entry to check whether sent message or not. */
2150 if ((newspacq = key_getspacq(&sp->spidx)) != NULL) {
2151 if (key_blockacq_count < newspacq->count) {
2152 /* reset counter and do send message. */
2153 newspacq->count = 0;
2155 /* increment counter and do nothing. */
2160 /* make new entry for blocking to send SADB_ACQUIRE. */
2161 if ((newspacq = key_newspacq(&sp->spidx)) == NULL)
2164 /* add to acqtree */
2165 LIST_INSERT_HEAD(&spacqtree, newspacq, chain);
2168 /* create new sadb_msg to reply. */
2169 m = key_setsadbmsg(SADB_X_SPDACQUIRE, 0, 0, 0, 0, 0);
2175 result->m_pkthdr.len = m_lengthm(result, NULL);
2176 mtod(result, struct sadb_msg *)->sadb_msg_len =
2177 PFKEY_UNIT64(result->m_pkthdr.len);
2179 return key_sendup_mbuf(NULL, m, KEY_SENDUP_REGISTERED);
2188 * SADB_SPDFLUSH processing
2191 * from the user, and free all entries in secpctree.
2195 * NOTE: what to do is only marking SADB_SASTATE_DEAD.
2197 * m will always be freed.
2200 key_spdflush(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
2202 struct sadb_msg *newmsg;
2203 struct secpolicy *sp;
2207 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
2208 panic("key_spdflush: NULL pointer is passed.");
2210 if (m->m_len != PFKEY_ALIGN8(sizeof(struct sadb_msg)))
2211 return key_senderror(so, m, EINVAL);
2213 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2214 LIST_FOREACH(sp, &sptree[dir], chain) {
2215 sp->state = IPSEC_SPSTATE_DEAD;
2219 if (sizeof(struct sadb_msg) > m->m_len + M_TRAILINGSPACE(m)) {
2220 ipseclog((LOG_DEBUG, "key_spdflush: No more memory.\n"));
2221 return key_senderror(so, m, ENOBUFS);
2227 m->m_pkthdr.len = m->m_len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
2228 newmsg = mtod(m, struct sadb_msg *);
2229 newmsg->sadb_msg_errno = 0;
2230 newmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
2232 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
2236 * SADB_SPDDUMP processing
2239 * from the user, and dump all SP leaves
2244 * m will always be freed.
2247 key_spddump(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
2249 struct secpolicy *sp;
2255 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
2256 panic("key_spddump: NULL pointer is passed.");
2258 /* search SPD entry and get buffer size. */
2260 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2261 LIST_FOREACH(sp, &sptree[dir], chain) {
2267 return key_senderror(so, m, ENOENT);
2269 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2270 LIST_FOREACH(sp, &sptree[dir], chain) {
2272 n = key_setdumpsp(sp, SADB_X_SPDDUMP, cnt,
2273 mhp->msg->sadb_msg_pid);
2276 key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
2284 static struct mbuf *
2285 key_setdumpsp(struct secpolicy *sp, u_int8_t type, u_int32_t seq,
2288 struct mbuf *result = NULL, *m;
2290 m = key_setsadbmsg(type, 0, SADB_SATYPE_UNSPEC, seq, pid, sp->refcnt);
2295 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
2296 &sp->spidx.src.sa, sp->spidx.prefs,
2297 sp->spidx.ul_proto);
2302 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
2303 &sp->spidx.dst.sa, sp->spidx.prefd,
2304 sp->spidx.ul_proto);
2314 if ((result->m_flags & M_PKTHDR) == 0)
2317 if (result->m_len < sizeof(struct sadb_msg)) {
2318 result = m_pullup(result, sizeof(struct sadb_msg));
2322 result->m_pkthdr.len = m_lengthm(result, NULL);
2323 mtod(result, struct sadb_msg *)->sadb_msg_len =
2324 PFKEY_UNIT64(result->m_pkthdr.len);
2334 * get PFKEY message length for security policy and request.
2337 key_getspreqmsglen(struct secpolicy *sp)
2339 struct ipsecrequest *isr;
2342 tlen = sizeof(struct sadb_x_policy);
2344 /* if is the policy for ipsec ? */
2345 if (sp->policy != IPSEC_POLICY_IPSEC)
2348 /* get length of ipsec requests */
2349 for (isr = sp->req; isr != NULL; isr = isr->next) {
2350 len = sizeof(struct sadb_x_ipsecrequest) +
2351 isr->saidx.src.sa.sa_len + isr->saidx.dst.sa.sa_len;
2353 tlen += PFKEY_ALIGN8(len);
2360 * SADB_SPDEXPIRE processing
2362 * <base, address(SD), lifetime(CH), policy>
2366 * others : error number
2369 key_spdexpire(struct secpolicy *sp)
2372 struct mbuf *result = NULL, *m;
2375 struct sadb_lifetime *lt;
2377 /* XXX: Why do we lock ? */
2382 panic("key_spdexpire: NULL pointer is passed.");
2384 /* set msg header */
2385 m = key_setsadbmsg(SADB_X_SPDEXPIRE, 0, 0, 0, 0, 0);
2392 /* create lifetime extension (current and hard) */
2393 len = PFKEY_ALIGN8(sizeof(*lt)) * 2;
2394 m = key_alloc_mbuf(len);
2395 if (!m || m->m_next) { /*XXX*/
2401 bzero(mtod(m, caddr_t), len);
2402 lt = mtod(m, struct sadb_lifetime *);
2403 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
2404 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
2405 lt->sadb_lifetime_allocations = 0;
2406 lt->sadb_lifetime_bytes = 0;
2407 lt->sadb_lifetime_addtime = sp->created;
2408 lt->sadb_lifetime_usetime = sp->lastused;
2409 lt = (struct sadb_lifetime *)(mtod(m, caddr_t) + len / 2);
2410 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
2411 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
2412 lt->sadb_lifetime_allocations = 0;
2413 lt->sadb_lifetime_bytes = 0;
2414 lt->sadb_lifetime_addtime = sp->lifetime;
2415 lt->sadb_lifetime_usetime = sp->validtime;
2418 /* set sadb_address for source */
2419 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
2421 sp->spidx.prefs, sp->spidx.ul_proto);
2428 /* set sadb_address for destination */
2429 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
2431 sp->spidx.prefd, sp->spidx.ul_proto);
2446 if ((result->m_flags & M_PKTHDR) == 0) {
2451 if (result->m_len < sizeof(struct sadb_msg)) {
2452 result = m_pullup(result, sizeof(struct sadb_msg));
2453 if (result == NULL) {
2458 result->m_pkthdr.len = m_lengthm(result, NULL);
2459 mtod(result, struct sadb_msg *)->sadb_msg_len =
2460 PFKEY_UNIT64(result->m_pkthdr.len);
2462 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
2471 /* %%% SAD management */
2473 * allocating a memory for new SA head, and copy from the values of mhp.
2474 * OUT: NULL : failure due to the lack of memory.
2475 * others : pointer to new SA head.
2477 static struct secashead *
2478 key_newsah(struct secasindex *saidx)
2480 struct secashead *newsah;
2482 KASSERT(saidx != NULL, ("key_newsaidx: null saidx"));
2484 newsah = kmalloc(sizeof(struct secashead), M_SECA,
2485 M_INTWAIT | M_ZERO | M_NULLOK);
2486 if (newsah != NULL) {
2488 for (i = 0; i < NELEM(newsah->savtree); i++)
2489 LIST_INIT(&newsah->savtree[i]);
2490 newsah->saidx = *saidx;
2492 /* add to saidxtree */
2493 newsah->state = SADB_SASTATE_MATURE;
2494 LIST_INSERT_HEAD(&sahtree, newsah, chain);
2500 * Delete SA index and all registered SAs.
2503 key_delsah(struct secashead *sah)
2505 struct secasvar *sav, *nextsav;
2511 panic("key_delsah: NULL pointer is passed.");
2515 /* searching all SA registerd in the secindex. */
2516 for (stateidx = 0; stateidx < NELEM(saorder_state_any);
2518 u_int state = saorder_state_any[stateidx];
2520 LIST_FOREACH_MUTABLE(sav, &sah->savtree[state], chain, nextsav)
2521 if (sav->refcnt == 0) {
2523 KEY_CHKSASTATE(state, sav->state, __func__);
2526 /* give up to delete this SA */
2531 /* Delete sah it has are no savs. */
2532 if (nzombies == 0) {
2533 /* remove from tree of SA index */
2534 if (__LIST_CHAINED(sah))
2535 LIST_REMOVE(sah, chain);
2536 if (sah->sa_route.ro_rt) {
2537 RTFREE(sah->sa_route.ro_rt);
2538 sah->sa_route.ro_rt = NULL;
2548 * allocating a new SA with LARVAL state. key_add() and key_getspi() call,
2549 * and copy the values of mhp into new buffer.
2550 * When SAD message type is GETSPI:
2551 * to set sequence number from acq_seq++,
2552 * to set zero to SPI.
2553 * not to call key_setsava().
2555 * others : pointer to new secasvar.
2557 * does not modify mbuf. does not free mbuf on error.
2559 static struct secasvar *
2560 key_newsav(struct mbuf *m, const struct sadb_msghdr *mhp, struct secashead *sah,
2561 int *errp, const char *where, int tag)
2563 struct secasvar *newsav;
2564 const struct sadb_sa *xsa;
2567 if (m == NULL || mhp == NULL || mhp->msg == NULL || sah == NULL)
2568 panic("key_newsa: NULL pointer is passed.");
2570 KMALLOC(newsav, struct secasvar *, sizeof(struct secasvar));
2571 if (newsav == NULL) {
2572 ipseclog((LOG_DEBUG, "key_newsa: No more memory.\n"));
2576 bzero((caddr_t)newsav, sizeof(struct secasvar));
2578 switch (mhp->msg->sadb_msg_type) {
2582 #ifdef IPSEC_DOSEQCHECK
2583 /* sync sequence number */
2584 if (mhp->msg->sadb_msg_seq == 0)
2586 (acq_seq = (acq_seq == ~0 ? 1 : ++acq_seq));
2589 newsav->seq = mhp->msg->sadb_msg_seq;
2594 if (mhp->ext[SADB_EXT_SA] == NULL) {
2595 KFREE(newsav), newsav = NULL;
2596 ipseclog((LOG_DEBUG, "key_newsa: invalid message is passed.\n"));
2600 xsa = (const struct sadb_sa *)mhp->ext[SADB_EXT_SA];
2601 newsav->spi = xsa->sadb_sa_spi;
2602 newsav->seq = mhp->msg->sadb_msg_seq;
2605 KFREE(newsav), newsav = NULL;
2610 /* copy sav values */
2611 if (mhp->msg->sadb_msg_type != SADB_GETSPI) {
2612 *errp = key_setsaval(newsav, m, mhp);
2614 KFREE(newsav), newsav = NULL;
2620 newsav->created = time_second;
2621 newsav->pid = mhp->msg->sadb_msg_pid;
2626 newsav->state = SADB_SASTATE_LARVAL;
2627 LIST_INSERT_TAIL(&sah->savtree[SADB_SASTATE_LARVAL], newsav,
2630 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
2631 kprintf("DP key_newsav from %s:%u return SP:%p\n",
2632 where, tag, newsav));
2638 * free() SA variable entry.
2641 key_delsav(struct secasvar *sav)
2643 KASSERT(sav != NULL, ("key_delsav: null sav"));
2644 KASSERT(sav->refcnt == 0,
2645 ("key_delsav: reference count %u > 0", sav->refcnt));
2647 /* remove from SA header */
2648 if (__LIST_CHAINED(sav))
2649 LIST_REMOVE(sav, chain);
2652 * Cleanup xform state. Note that zeroize'ing causes the
2653 * keys to be cleared; otherwise we must do it ourself.
2655 if (sav->tdb_xform != NULL) {
2656 sav->tdb_xform->xf_zeroize(sav);
2657 sav->tdb_xform = NULL;
2659 if (sav->key_auth != NULL)
2660 bzero(_KEYBUF(sav->key_auth), _KEYLEN(sav->key_auth));
2661 if (sav->key_enc != NULL)
2662 bzero(_KEYBUF(sav->key_enc), _KEYLEN(sav->key_enc));
2664 if (sav->key_auth != NULL) {
2665 KFREE(sav->key_auth);
2666 sav->key_auth = NULL;
2668 if (sav->key_enc != NULL) {
2669 KFREE(sav->key_enc);
2670 sav->key_enc = NULL;
2673 bzero(sav->sched, sav->schedlen);
2677 if (sav->replay != NULL) {
2681 if (sav->lft_c != NULL) {
2685 if (sav->lft_h != NULL) {
2689 if (sav->lft_s != NULL) {
2693 if (sav->iv != NULL) {
2707 * others : found, pointer to a SA.
2709 static struct secashead *
2710 key_getsah(struct secasindex *saidx)
2712 struct secashead *sah;
2714 LIST_FOREACH(sah, &sahtree, chain) {
2715 if (sah->state == SADB_SASTATE_DEAD)
2717 if (key_cmpsaidx(&sah->saidx, saidx, CMP_REQID))
2725 * check not to be duplicated SPI.
2726 * NOTE: this function is too slow due to searching all SAD.
2729 * others : found, pointer to a SA.
2731 static struct secasvar *
2732 key_checkspidup(struct secasindex *saidx, u_int32_t spi)
2734 struct secashead *sah;
2735 struct secasvar *sav;
2737 /* check address family */
2738 if (saidx->src.sa.sa_family != saidx->dst.sa.sa_family) {
2739 ipseclog((LOG_DEBUG, "key_checkspidup: address family mismatched.\n"));
2744 LIST_FOREACH(sah, &sahtree, chain) {
2745 if (!key_ismyaddr((struct sockaddr *)&sah->saidx.dst))
2747 sav = key_getsavbyspi(sah, spi);
2756 * search SAD litmited alive SA, protocol, SPI.
2759 * others : found, pointer to a SA.
2761 static struct secasvar *
2762 key_getsavbyspi(struct secashead *sah, u_int32_t spi)
2764 struct secasvar *sav;
2767 /* search all status */
2768 for (stateidx = 0; stateidx < NELEM(saorder_state_alive);
2770 u_int state = saorder_state_alive[stateidx];
2772 LIST_FOREACH(sav, &sah->savtree[state], chain) {
2774 if (sav->state != state) {
2775 ipseclog((LOG_DEBUG, "key_getsavbyspi: "
2776 "invalid sav->state (queue: %d SA: %d)\n",
2777 state, sav->state));
2781 if (sav->spi == spi)
2790 * copy SA values from PF_KEY message except *SPI, SEQ, PID, STATE and TYPE*.
2791 * You must update these if need.
2795 * does not modify mbuf. does not free mbuf on error.
2798 key_setsaval(struct secasvar *sav, struct mbuf *m,
2799 const struct sadb_msghdr *mhp)
2804 if (m == NULL || mhp == NULL || mhp->msg == NULL)
2805 panic("key_setsaval: NULL pointer is passed.");
2807 /* initialization */
2809 sav->key_auth = NULL;
2810 sav->key_enc = NULL;
2817 sav->tdb_xform = NULL; /* transform */
2818 sav->tdb_encalgxform = NULL; /* encoding algorithm */
2819 sav->tdb_authalgxform = NULL; /* authentication algorithm */
2820 sav->tdb_compalgxform = NULL; /* compression algorithm */
2823 if (mhp->ext[SADB_EXT_SA] != NULL) {
2824 const struct sadb_sa *sa0;
2826 sa0 = (const struct sadb_sa *)mhp->ext[SADB_EXT_SA];
2827 if (mhp->extlen[SADB_EXT_SA] < sizeof(*sa0)) {
2832 sav->alg_auth = sa0->sadb_sa_auth;
2833 sav->alg_enc = sa0->sadb_sa_encrypt;
2834 sav->flags = sa0->sadb_sa_flags;
2837 if ((sa0->sadb_sa_flags & SADB_X_EXT_OLD) == 0) {
2839 kmalloc(sizeof(struct secreplay)+sa0->sadb_sa_replay,
2840 M_SECA, M_INTWAIT | M_ZERO | M_NULLOK);
2841 if (sav->replay == NULL) {
2842 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
2846 if (sa0->sadb_sa_replay != 0)
2847 sav->replay->bitmap = (caddr_t)(sav->replay+1);
2848 sav->replay->wsize = sa0->sadb_sa_replay;
2852 /* Authentication keys */
2853 if (mhp->ext[SADB_EXT_KEY_AUTH] != NULL) {
2854 const struct sadb_key *key0;
2857 key0 = (const struct sadb_key *)mhp->ext[SADB_EXT_KEY_AUTH];
2858 len = mhp->extlen[SADB_EXT_KEY_AUTH];
2861 if (len < sizeof(*key0)) {
2865 switch (mhp->msg->sadb_msg_satype) {
2866 case SADB_SATYPE_AH:
2867 case SADB_SATYPE_ESP:
2868 if (len == PFKEY_ALIGN8(sizeof(struct sadb_key)) &&
2869 sav->alg_auth != SADB_X_AALG_NULL)
2872 case SADB_X_SATYPE_IPCOMP:
2878 ipseclog((LOG_DEBUG, "key_setsaval: invalid key_auth values.\n"));
2882 sav->key_auth = (struct sadb_key *)key_newbuf(key0, len);
2883 if (sav->key_auth == NULL) {
2884 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
2890 /* Encryption key */
2891 if (mhp->ext[SADB_EXT_KEY_ENCRYPT] != NULL) {
2892 const struct sadb_key *key0;
2895 key0 = (const struct sadb_key *)mhp->ext[SADB_EXT_KEY_ENCRYPT];
2896 len = mhp->extlen[SADB_EXT_KEY_ENCRYPT];
2899 if (len < sizeof(*key0)) {
2903 switch (mhp->msg->sadb_msg_satype) {
2904 case SADB_SATYPE_ESP:
2905 if (len == PFKEY_ALIGN8(sizeof(struct sadb_key)) &&
2906 sav->alg_enc != SADB_EALG_NULL) {
2910 sav->key_enc = (struct sadb_key *)key_newbuf(key0, len);
2911 if (sav->key_enc == NULL) {
2912 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
2917 case SADB_X_SATYPE_IPCOMP:
2918 if (len != PFKEY_ALIGN8(sizeof(struct sadb_key)))
2920 sav->key_enc = NULL; /*just in case*/
2922 case SADB_SATYPE_AH:
2928 ipseclog((LOG_DEBUG, "key_setsatval: invalid key_enc value.\n"));
2936 switch (mhp->msg->sadb_msg_satype) {
2937 case SADB_SATYPE_AH:
2938 error = xform_init(sav, XF_AH);
2940 case SADB_SATYPE_ESP:
2941 error = xform_init(sav, XF_ESP);
2943 case SADB_X_SATYPE_IPCOMP:
2944 error = xform_init(sav, XF_IPCOMP);
2948 ipseclog((LOG_DEBUG,
2949 "key_setsaval: unable to initialize SA type %u.\n",
2950 mhp->msg->sadb_msg_satype));
2955 sav->created = time_second;
2957 /* make lifetime for CURRENT */
2958 KMALLOC(sav->lft_c, struct sadb_lifetime *,
2959 sizeof(struct sadb_lifetime));
2960 if (sav->lft_c == NULL) {
2961 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
2966 sav->lft_c->sadb_lifetime_len =
2967 PFKEY_UNIT64(sizeof(struct sadb_lifetime));
2968 sav->lft_c->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
2969 sav->lft_c->sadb_lifetime_allocations = 0;
2970 sav->lft_c->sadb_lifetime_bytes = 0;
2971 sav->lft_c->sadb_lifetime_addtime = time_second;
2972 sav->lft_c->sadb_lifetime_usetime = 0;
2974 /* lifetimes for HARD and SOFT */
2976 const struct sadb_lifetime *lft0;
2978 lft0 = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_HARD];
2980 if (mhp->extlen[SADB_EXT_LIFETIME_HARD] < sizeof(*lft0)) {
2984 sav->lft_h = (struct sadb_lifetime *)key_newbuf(lft0,
2986 if (sav->lft_h == NULL) {
2987 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
2991 /* to be initialize ? */
2994 lft0 = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_SOFT];
2996 if (mhp->extlen[SADB_EXT_LIFETIME_SOFT] < sizeof(*lft0)) {
3000 sav->lft_s = (struct sadb_lifetime *)key_newbuf(lft0,
3002 if (sav->lft_s == NULL) {
3003 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
3007 /* to be initialize ? */
3014 /* initialization */
3015 if (sav->replay != NULL) {
3019 if (sav->key_auth != NULL) {
3020 KFREE(sav->key_auth);
3021 sav->key_auth = NULL;
3023 if (sav->key_enc != NULL) {
3024 KFREE(sav->key_enc);
3025 sav->key_enc = NULL;
3031 if (sav->iv != NULL) {
3035 if (sav->lft_c != NULL) {
3039 if (sav->lft_h != NULL) {
3043 if (sav->lft_s != NULL) {
3052 * validation with a secasvar entry, and set SADB_SATYPE_MATURE.
3057 key_mature(struct secasvar *sav)
3061 /* check SPI value */
3062 switch (sav->sah->saidx.proto) {
3065 if (ntohl(sav->spi) >= 0 && ntohl(sav->spi) <= 255) {
3066 ipseclog((LOG_DEBUG,
3067 "key_mature: illegal range of SPI %u.\n",
3068 (u_int32_t)ntohl(sav->spi)));
3075 switch (sav->sah->saidx.proto) {
3078 if ((sav->flags & (SADB_X_EXT_OLD|SADB_X_EXT_DERIV)) ==
3079 (SADB_X_EXT_OLD|SADB_X_EXT_DERIV)) {
3080 ipseclog((LOG_DEBUG, "key_mature: "
3081 "invalid flag (derived) given to old-esp.\n"));
3084 error = xform_init(sav, XF_ESP);
3088 if (sav->flags & SADB_X_EXT_DERIV) {
3089 ipseclog((LOG_DEBUG, "key_mature: "
3090 "invalid flag (derived) given to AH SA.\n"));
3093 if (sav->alg_enc != SADB_EALG_NONE) {
3094 ipseclog((LOG_DEBUG, "key_mature: "
3095 "protocol and algorithm mismated.\n"));
3098 error = xform_init(sav, XF_AH);
3100 case IPPROTO_IPCOMP:
3101 if (sav->alg_auth != SADB_AALG_NONE) {
3102 ipseclog((LOG_DEBUG, "key_mature: "
3103 "protocol and algorithm mismated.\n"));
3106 if ((sav->flags & SADB_X_EXT_RAWCPI) == 0
3107 && ntohl(sav->spi) >= 0x10000) {
3108 ipseclog((LOG_DEBUG, "key_mature: invalid cpi for IPComp.\n"));
3111 error = xform_init(sav, XF_IPCOMP);
3114 ipseclog((LOG_DEBUG, "key_mature: Invalid satype.\n"));
3115 error = EPROTONOSUPPORT;
3119 key_sa_chgstate(sav, SADB_SASTATE_MATURE);
3124 * subroutine for SADB_GET and SADB_DUMP.
3126 static struct mbuf *
3127 key_setdumpsa(struct secasvar *sav, u_int8_t type, u_int8_t satype,
3128 u_int32_t seq, u_int32_t pid)
3130 struct mbuf *result = NULL, *tres = NULL, *m;
3135 SADB_EXT_SA, SADB_X_EXT_SA2,
3136 SADB_EXT_LIFETIME_HARD, SADB_EXT_LIFETIME_SOFT,
3137 SADB_EXT_LIFETIME_CURRENT, SADB_EXT_ADDRESS_SRC,
3138 SADB_EXT_ADDRESS_DST, SADB_EXT_ADDRESS_PROXY, SADB_EXT_KEY_AUTH,
3139 SADB_EXT_KEY_ENCRYPT, SADB_EXT_IDENTITY_SRC,
3140 SADB_EXT_IDENTITY_DST, SADB_EXT_SENSITIVITY,
3143 m = key_setsadbmsg(type, 0, satype, seq, pid, sav->refcnt);
3148 for (i = NELEM(dumporder) - 1; i >= 0; i--) {
3151 switch (dumporder[i]) {
3153 m = key_setsadbsa(sav);
3158 case SADB_X_EXT_SA2:
3159 m = key_setsadbxsa2(sav->sah->saidx.mode,
3160 sav->replay ? sav->replay->count : 0,
3161 sav->sah->saidx.reqid);
3166 case SADB_EXT_ADDRESS_SRC:
3167 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
3168 &sav->sah->saidx.src.sa,
3169 FULLMASK, IPSEC_ULPROTO_ANY);
3174 case SADB_EXT_ADDRESS_DST:
3175 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
3176 &sav->sah->saidx.dst.sa,
3177 FULLMASK, IPSEC_ULPROTO_ANY);
3182 case SADB_EXT_KEY_AUTH:
3185 l = PFKEY_UNUNIT64(sav->key_auth->sadb_key_len);
3189 case SADB_EXT_KEY_ENCRYPT:
3192 l = PFKEY_UNUNIT64(sav->key_enc->sadb_key_len);
3196 case SADB_EXT_LIFETIME_CURRENT:
3199 l = PFKEY_UNUNIT64(((struct sadb_ext *)sav->lft_c)->sadb_ext_len);
3203 case SADB_EXT_LIFETIME_HARD:
3206 l = PFKEY_UNUNIT64(((struct sadb_ext *)sav->lft_h)->sadb_ext_len);
3210 case SADB_EXT_LIFETIME_SOFT:
3213 l = PFKEY_UNUNIT64(((struct sadb_ext *)sav->lft_s)->sadb_ext_len);
3217 case SADB_EXT_ADDRESS_PROXY:
3218 case SADB_EXT_IDENTITY_SRC:
3219 case SADB_EXT_IDENTITY_DST:
3220 /* XXX: should we brought from SPD ? */
3221 case SADB_EXT_SENSITIVITY:
3226 if ((!m && !p) || (m && p))
3229 M_PREPEND(tres, l, MB_DONTWAIT);
3232 bcopy(p, mtod(tres, caddr_t), l);
3236 m = key_alloc_mbuf(l);
3239 m_copyback(m, 0, l, p);
3247 m_cat(result, tres);
3249 if (result->m_len < sizeof(struct sadb_msg)) {
3250 result = m_pullup(result, sizeof(struct sadb_msg));
3254 result->m_pkthdr.len = m_lengthm(result, NULL);
3255 mtod(result, struct sadb_msg *)->sadb_msg_len =
3256 PFKEY_UNIT64(result->m_pkthdr.len);
3267 * set data into sadb_msg.
3269 static struct mbuf *
3270 key_setsadbmsg(u_int8_t type, u_int16_t tlen, u_int8_t satype, u_int32_t seq,
3271 pid_t pid, u_int16_t reserved)
3277 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
3280 m = m_getb(len, MB_DONTWAIT, MT_DATA, M_PKTHDR);
3283 m->m_pkthdr.len = m->m_len = len;
3285 p = mtod(m, struct sadb_msg *);
3288 p->sadb_msg_version = PF_KEY_V2;
3289 p->sadb_msg_type = type;
3290 p->sadb_msg_errno = 0;
3291 p->sadb_msg_satype = satype;
3292 p->sadb_msg_len = PFKEY_UNIT64(tlen);
3293 p->sadb_msg_reserved = reserved;
3294 p->sadb_msg_seq = seq;
3295 p->sadb_msg_pid = (u_int32_t)pid;
3301 * copy secasvar data into sadb_address.
3303 static struct mbuf *
3304 key_setsadbsa(struct secasvar *sav)
3310 len = PFKEY_ALIGN8(sizeof(struct sadb_sa));
3311 m = key_alloc_mbuf(len);
3312 if (!m || m->m_next) { /*XXX*/
3318 p = mtod(m, struct sadb_sa *);
3321 p->sadb_sa_len = PFKEY_UNIT64(len);
3322 p->sadb_sa_exttype = SADB_EXT_SA;
3323 p->sadb_sa_spi = sav->spi;
3324 p->sadb_sa_replay = (sav->replay != NULL ? sav->replay->wsize : 0);
3325 p->sadb_sa_state = sav->state;
3326 p->sadb_sa_auth = sav->alg_auth;
3327 p->sadb_sa_encrypt = sav->alg_enc;
3328 p->sadb_sa_flags = sav->flags;
3334 * set data into sadb_address.
3336 static struct mbuf *
3337 key_setsadbaddr(u_int16_t exttype, const struct sockaddr *saddr, u_int8_t prefixlen,
3341 struct sadb_address *p;
3344 len = PFKEY_ALIGN8(sizeof(struct sadb_address)) +
3345 PFKEY_ALIGN8(saddr->sa_len);
3346 m = key_alloc_mbuf(len);
3347 if (!m || m->m_next) { /*XXX*/
3353 p = mtod(m, struct sadb_address *);
3356 p->sadb_address_len = PFKEY_UNIT64(len);
3357 p->sadb_address_exttype = exttype;
3358 p->sadb_address_proto = ul_proto;
3359 if (prefixlen == FULLMASK) {
3360 switch (saddr->sa_family) {
3362 prefixlen = sizeof(struct in_addr) << 3;
3365 prefixlen = sizeof(struct in6_addr) << 3;
3371 p->sadb_address_prefixlen = prefixlen;
3372 p->sadb_address_reserved = 0;
3375 mtod(m, caddr_t) + PFKEY_ALIGN8(sizeof(struct sadb_address)),
3383 * set data into sadb_ident.
3385 static struct mbuf *
3386 key_setsadbident(u_int16_t exttype, u_int16_t idtype, caddr_t string,
3387 int stringlen, u_int64_t id)
3390 struct sadb_ident *p;
3393 len = PFKEY_ALIGN8(sizeof(struct sadb_ident)) + PFKEY_ALIGN8(stringlen);
3394 m = key_alloc_mbuf(len);
3395 if (!m || m->m_next) { /*XXX*/
3401 p = mtod(m, struct sadb_ident *);
3404 p->sadb_ident_len = PFKEY_UNIT64(len);
3405 p->sadb_ident_exttype = exttype;
3406 p->sadb_ident_type = idtype;
3407 p->sadb_ident_reserved = 0;
3408 p->sadb_ident_id = id;
3411 mtod(m, caddr_t) + PFKEY_ALIGN8(sizeof(struct sadb_ident)),
3419 * set data into sadb_x_sa2.
3421 static struct mbuf *
3422 key_setsadbxsa2(u_int8_t mode, u_int32_t seq, u_int32_t reqid)
3425 struct sadb_x_sa2 *p;
3428 len = PFKEY_ALIGN8(sizeof(struct sadb_x_sa2));
3429 m = key_alloc_mbuf(len);
3430 if (!m || m->m_next) { /*XXX*/
3436 p = mtod(m, struct sadb_x_sa2 *);
3439 p->sadb_x_sa2_len = PFKEY_UNIT64(len);
3440 p->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
3441 p->sadb_x_sa2_mode = mode;
3442 p->sadb_x_sa2_reserved1 = 0;
3443 p->sadb_x_sa2_reserved2 = 0;
3444 p->sadb_x_sa2_sequence = seq;
3445 p->sadb_x_sa2_reqid = reqid;
3451 * set data into sadb_x_policy
3453 static struct mbuf *
3454 key_setsadbxpolicy(u_int16_t type, u_int8_t dir, u_int32_t id)
3457 struct sadb_x_policy *p;
3460 len = PFKEY_ALIGN8(sizeof(struct sadb_x_policy));
3461 m = key_alloc_mbuf(len);
3462 if (!m || m->m_next) { /*XXX*/
3468 p = mtod(m, struct sadb_x_policy *);
3471 p->sadb_x_policy_len = PFKEY_UNIT64(len);
3472 p->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3473 p->sadb_x_policy_type = type;
3474 p->sadb_x_policy_dir = dir;
3475 p->sadb_x_policy_id = id;
3482 * copy a buffer into the new buffer allocated.
3485 key_newbuf(const void *src, u_int len)
3489 KMALLOC(new, caddr_t, len);
3491 ipseclog((LOG_DEBUG, "key_newbuf: No more memory.\n"));
3494 bcopy(src, new, len);
3499 /* compare my own address
3500 * OUT: 1: true, i.e. my address.
3504 key_ismyaddr(struct sockaddr *sa)
3507 struct sockaddr_in *sin;
3508 struct in_ifaddr_container *iac;
3513 panic("key_ismyaddr: NULL pointer is passed.");
3515 switch (sa->sa_family) {
3518 sin = (struct sockaddr_in *)sa;
3519 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
3520 struct in_ifaddr *ia = iac->ia;
3522 if (sin->sin_family == ia->ia_addr.sin_family &&
3523 sin->sin_len == ia->ia_addr.sin_len &&
3524 sin->sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr)
3533 return key_ismyaddr6((struct sockaddr_in6 *)sa);
3542 * compare my own address for IPv6.
3545 * NOTE: derived ip6_input() in KAME. This is necessary to modify more.
3548 key_ismyaddr6(struct sockaddr_in6 *sin6)
3550 struct in6_ifaddr *ia;
3551 struct in6_multi *in6m;
3553 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
3554 if (key_sockaddrcmp((struct sockaddr *)&sin6,
3555 (struct sockaddr *)&ia->ia_addr, 0) == 0)
3560 * XXX why do we care about multlicast here while we don't care
3561 * about IPv4 multicast??
3564 in6m = IN6_LOOKUP_MULTI(&sin6->sin6_addr, ia->ia_ifp);
3569 /* loopback, just for safety */
3570 if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
3578 * compare two secasindex structure.
3579 * flag can specify to compare 2 saidxes.
3580 * compare two secasindex structure without both mode and reqid.
3581 * don't compare port.
3583 * saidx0: source, it can be in SAD.
3591 const struct secasindex *saidx0,
3592 const struct secasindex *saidx1,
3596 if (saidx0 == NULL && saidx1 == NULL)
3599 if (saidx0 == NULL || saidx1 == NULL)
3602 if (saidx0->proto != saidx1->proto)
3605 if (flag == CMP_EXACTLY) {
3606 if (saidx0->mode != saidx1->mode)
3608 if (saidx0->reqid != saidx1->reqid)
3610 if (bcmp(&saidx0->src, &saidx1->src, saidx0->src.sa.sa_len) != 0 ||
3611 bcmp(&saidx0->dst, &saidx1->dst, saidx0->dst.sa.sa_len) != 0)
3615 /* CMP_MODE_REQID, CMP_REQID, CMP_HEAD */
3616 if (flag == CMP_MODE_REQID
3617 ||flag == CMP_REQID) {
3619 * If reqid of SPD is non-zero, unique SA is required.
3620 * The result must be of same reqid in this case.
3622 if (saidx1->reqid != 0 && saidx0->reqid != saidx1->reqid)
3626 if (flag == CMP_MODE_REQID) {
3627 if (saidx0->mode != IPSEC_MODE_ANY
3628 && saidx0->mode != saidx1->mode)
3632 if (key_sockaddrcmp(&saidx0->src.sa, &saidx1->src.sa, 0) != 0) {
3635 if (key_sockaddrcmp(&saidx0->dst.sa, &saidx1->dst.sa, 0) != 0) {
3644 * compare two secindex structure exactly.
3646 * spidx0: source, it is often in SPD.
3647 * spidx1: object, it is often from PFKEY message.
3653 key_cmpspidx_exactly(
3654 struct secpolicyindex *spidx0,
3655 struct secpolicyindex *spidx1)
3658 if (spidx0 == NULL && spidx1 == NULL)
3661 if (spidx0 == NULL || spidx1 == NULL)
3664 if (spidx0->prefs != spidx1->prefs
3665 || spidx0->prefd != spidx1->prefd
3666 || spidx0->ul_proto != spidx1->ul_proto)
3669 return key_sockaddrcmp(&spidx0->src.sa, &spidx1->src.sa, 1) == 0 &&
3670 key_sockaddrcmp(&spidx0->dst.sa, &spidx1->dst.sa, 1) == 0;
3674 * compare two secindex structure with mask.
3676 * spidx0: source, it is often in SPD.
3677 * spidx1: object, it is often from IP header.
3683 key_cmpspidx_withmask(
3684 struct secpolicyindex *spidx0,
3685 struct secpolicyindex *spidx1)
3688 if (spidx0 == NULL && spidx1 == NULL)
3691 if (spidx0 == NULL || spidx1 == NULL)
3694 if (spidx0->src.sa.sa_family != spidx1->src.sa.sa_family ||
3695 spidx0->dst.sa.sa_family != spidx1->dst.sa.sa_family ||
3696 spidx0->src.sa.sa_len != spidx1->src.sa.sa_len ||
3697 spidx0->dst.sa.sa_len != spidx1->dst.sa.sa_len)
3700 /* if spidx.ul_proto == IPSEC_ULPROTO_ANY, ignore. */
3701 if (spidx0->ul_proto != (u_int16_t)IPSEC_ULPROTO_ANY
3702 && spidx0->ul_proto != spidx1->ul_proto)
3705 switch (spidx0->src.sa.sa_family) {
3707 if (spidx0->src.sin.sin_port != IPSEC_PORT_ANY
3708 && spidx0->src.sin.sin_port != spidx1->src.sin.sin_port)
3710 if (!key_bbcmp(&spidx0->src.sin.sin_addr,
3711 &spidx1->src.sin.sin_addr, spidx0->prefs))
3715 if (spidx0->src.sin6.sin6_port != IPSEC_PORT_ANY
3716 && spidx0->src.sin6.sin6_port != spidx1->src.sin6.sin6_port)
3719 * scope_id check. if sin6_scope_id is 0, we regard it
3720 * as a wildcard scope, which matches any scope zone ID.
3722 if (spidx0->src.sin6.sin6_scope_id &&
3723 spidx1->src.sin6.sin6_scope_id &&
3724 spidx0->src.sin6.sin6_scope_id != spidx1->src.sin6.sin6_scope_id)
3726 if (!key_bbcmp(&spidx0->src.sin6.sin6_addr,
3727 &spidx1->src.sin6.sin6_addr, spidx0->prefs))
3732 if (bcmp(&spidx0->src, &spidx1->src, spidx0->src.sa.sa_len) != 0)
3737 switch (spidx0->dst.sa.sa_family) {
3739 if (spidx0->dst.sin.sin_port != IPSEC_PORT_ANY
3740 && spidx0->dst.sin.sin_port != spidx1->dst.sin.sin_port)
3742 if (!key_bbcmp(&spidx0->dst.sin.sin_addr,
3743 &spidx1->dst.sin.sin_addr, spidx0->prefd))
3747 if (spidx0->dst.sin6.sin6_port != IPSEC_PORT_ANY
3748 && spidx0->dst.sin6.sin6_port != spidx1->dst.sin6.sin6_port)
3751 * scope_id check. if sin6_scope_id is 0, we regard it
3752 * as a wildcard scope, which matches any scope zone ID.
3754 if (spidx0->dst.sin6.sin6_scope_id &&
3755 spidx1->dst.sin6.sin6_scope_id &&
3756 spidx0->dst.sin6.sin6_scope_id != spidx1->dst.sin6.sin6_scope_id)
3758 if (!key_bbcmp(&spidx0->dst.sin6.sin6_addr,
3759 &spidx1->dst.sin6.sin6_addr, spidx0->prefd))
3764 if (bcmp(&spidx0->dst, &spidx1->dst, spidx0->dst.sa.sa_len) != 0)
3769 /* XXX Do we check other field ? e.g. flowinfo */
3774 /* returns 0 on match */
3777 const struct sockaddr *sa1,
3778 const struct sockaddr *sa2,
3784 #define satosin(s) ((const struct sockaddr_in *)s)
3788 #define satosin6(s) ((const struct sockaddr_in6 *)s)
3789 if (sa1->sa_family != sa2->sa_family || sa1->sa_len != sa2->sa_len)
3792 switch (sa1->sa_family) {
3794 if (sa1->sa_len != sizeof(struct sockaddr_in))
3796 if (satosin(sa1)->sin_addr.s_addr !=
3797 satosin(sa2)->sin_addr.s_addr) {
3800 if (port && satosin(sa1)->sin_port != satosin(sa2)->sin_port)
3804 if (sa1->sa_len != sizeof(struct sockaddr_in6))
3805 return 1; /*EINVAL*/
3806 if (satosin6(sa1)->sin6_scope_id !=
3807 satosin6(sa2)->sin6_scope_id) {
3810 if (!IN6_ARE_ADDR_EQUAL(&satosin6(sa1)->sin6_addr,
3811 &satosin6(sa2)->sin6_addr)) {
3815 satosin6(sa1)->sin6_port != satosin6(sa2)->sin6_port) {
3819 if (bcmp(sa1, sa2, sa1->sa_len) != 0)
3830 * compare two buffers with mask.
3834 * bits: Number of bits to compare
3840 key_bbcmp(const void *a1, const void *a2, u_int bits)
3842 const unsigned char *p1 = a1;
3843 const unsigned char *p2 = a2;
3845 /* XXX: This could be considerably faster if we compare a word
3846 * at a time, but it is complicated on LSB Endian machines */
3848 /* Handle null pointers */
3849 if (p1 == NULL || p2 == NULL)
3859 u_int8_t mask = ~((1<<(8-bits))-1);
3860 if ((*p1 & mask) != (*p2 & mask))
3863 return 1; /* Match! */
3868 * scanning SPD and SAD to check status for each entries,
3869 * and do to remove or to expire.
3870 * XXX: year 2038 problem may remain.
3873 key_timehandler(void *unused)
3876 time_t now = time_second;
3877 struct secspacq *spacq, *nextspacq;
3883 struct secpolicy *sp, *nextsp;
3885 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
3886 LIST_FOREACH_MUTABLE(sp, &sptree[dir], chain, nextsp) {
3887 if (sp->state == IPSEC_SPSTATE_DEAD) {
3892 if (sp->lifetime == 0 && sp->validtime == 0)
3895 /* the deletion will occur next time */
3896 if ((sp->lifetime && now - sp->created > sp->lifetime)
3897 || (sp->validtime && now - sp->lastused > sp->validtime)) {
3898 sp->state = IPSEC_SPSTATE_DEAD;
3908 struct secashead *sah, *nextsah;
3909 struct secasvar *sav, *nextsav;
3911 LIST_FOREACH_MUTABLE(sah, &sahtree, chain, nextsah) {
3912 /* if sah has been dead, then delete it and process next sah. */
3913 if (sah->state == SADB_SASTATE_DEAD) {
3918 /* if LARVAL entry doesn't become MATURE, delete it. */
3919 LIST_FOREACH_MUTABLE(sav, &sah->savtree[SADB_SASTATE_LARVAL],
3921 if (now - sav->created > key_larval_lifetime) {
3927 * check MATURE entry to start to send expire message
3930 LIST_FOREACH_MUTABLE(sav, &sah->savtree[SADB_SASTATE_MATURE],
3932 /* we don't need to check. */
3933 if (sav->lft_s == NULL)
3937 if (sav->lft_c == NULL) {
3938 ipseclog((LOG_DEBUG,"key_timehandler: "
3939 "There is no CURRENT time, why?\n"));
3943 /* check SOFT lifetime */
3944 if (sav->lft_s->sadb_lifetime_addtime != 0
3945 && now - sav->created > sav->lft_s->sadb_lifetime_addtime) {
3947 * check SA to be used whether or not.
3948 * when SA hasn't been used, delete it.
3950 if (sav->lft_c->sadb_lifetime_usetime == 0) {
3951 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
3954 key_sa_chgstate(sav, SADB_SASTATE_DYING);
3956 * XXX If we keep to send expire
3957 * message in the status of
3958 * DYING. Do remove below code.
3963 /* check SOFT lifetime by bytes */
3965 * XXX I don't know the way to delete this SA
3966 * when new SA is installed. Caution when it's
3967 * installed too big lifetime by time.
3969 else if (sav->lft_s->sadb_lifetime_bytes != 0
3970 && sav->lft_s->sadb_lifetime_bytes < sav->lft_c->sadb_lifetime_bytes) {
3972 key_sa_chgstate(sav, SADB_SASTATE_DYING);
3974 * XXX If we keep to send expire
3975 * message in the status of
3976 * DYING. Do remove below code.
3982 /* check DYING entry to change status to DEAD. */
3983 LIST_FOREACH_MUTABLE(sav, &sah->savtree[SADB_SASTATE_DYING],
3985 /* we don't need to check. */
3986 if (sav->lft_h == NULL)
3990 if (sav->lft_c == NULL) {
3991 ipseclog((LOG_DEBUG, "key_timehandler: "
3992 "There is no CURRENT time, why?\n"));
3996 if (sav->lft_h->sadb_lifetime_addtime != 0
3997 && now - sav->created > sav->lft_h->sadb_lifetime_addtime) {
3998 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4001 #if 0 /* XXX Should we keep to send expire message until HARD lifetime ? */
4002 else if (sav->lft_s != NULL
4003 && sav->lft_s->sadb_lifetime_addtime != 0
4004 && now - sav->created > sav->lft_s->sadb_lifetime_addtime) {
4006 * XXX: should be checked to be
4007 * installed the valid SA.
4011 * If there is no SA then sending
4017 /* check HARD lifetime by bytes */
4018 else if (sav->lft_h->sadb_lifetime_bytes != 0
4019 && sav->lft_h->sadb_lifetime_bytes < sav->lft_c->sadb_lifetime_bytes) {
4020 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4025 /* delete entry in DEAD */
4026 LIST_FOREACH_MUTABLE(sav, &sah->savtree[SADB_SASTATE_DEAD],
4029 if (sav->state != SADB_SASTATE_DEAD) {
4030 ipseclog((LOG_DEBUG, "key_timehandler: "
4031 "invalid sav->state "
4032 "(queue: %d SA: %d): "
4034 SADB_SASTATE_DEAD, sav->state));
4038 * do not call key_freesav() here.
4039 * sav should already be freed, and sav->refcnt
4040 * shows other references to sav
4041 * (such as from SPD).
4047 #ifndef IPSEC_NONBLOCK_ACQUIRE
4050 struct secacq *acq, *nextacq;
4052 LIST_FOREACH_MUTABLE(acq, &acqtree, chain, nextacq) {
4053 if (now - acq->created > key_blockacq_lifetime &&
4054 __LIST_CHAINED(acq)) {
4055 LIST_REMOVE(acq, chain);
4063 LIST_FOREACH_MUTABLE(spacq, &spacqtree, chain, nextspacq) {
4064 if (now - spacq->created > key_blockacq_lifetime &&
4065 __LIST_CHAINED(spacq)) {
4066 LIST_REMOVE(spacq, chain);
4071 /* initialize random seed */
4072 if (key_tick_init_random++ > key_int_random) {
4073 key_tick_init_random = 0;
4077 #ifndef IPSEC_DEBUG2
4078 /* do exchange to tick time !! */
4079 callout_reset(&key_timehandler_ch, hz, key_timehandler, NULL);
4087 * to initialize a seed for random()
4092 skrandom(time_second);
4100 key_randomfill(&value, sizeof(value));
4105 key_randomfill(void *p, size_t l)
4109 static int warn = 1;
4111 n = (size_t)read_random(p, (u_int)l);
4115 bcopy(&v, (u_int8_t *)p + n,
4116 l - n < sizeof(v) ? l - n : sizeof(v));
4120 kprintf("WARNING: pseudo-random number generator "
4121 "used for IPsec processing\n");
4128 * map SADB_SATYPE_* to IPPROTO_*.
4129 * if satype == SADB_SATYPE then satype is mapped to ~0.
4131 * 0: invalid satype.
4134 key_satype2proto(u_int8_t satype)
4137 case SADB_SATYPE_UNSPEC:
4138 return IPSEC_PROTO_ANY;
4139 case SADB_SATYPE_AH:
4141 case SADB_SATYPE_ESP:
4143 case SADB_X_SATYPE_IPCOMP:
4144 return IPPROTO_IPCOMP;
4152 * map IPPROTO_* to SADB_SATYPE_*
4154 * 0: invalid protocol type.
4157 key_proto2satype(u_int16_t proto)
4161 return SADB_SATYPE_AH;
4163 return SADB_SATYPE_ESP;
4164 case IPPROTO_IPCOMP:
4165 return SADB_X_SATYPE_IPCOMP;
4174 * SADB_GETSPI processing is to receive
4175 * <base, (SA2), src address, dst address, (SPI range)>
4176 * from the IKMPd, to assign a unique spi value, to hang on the INBOUND
4177 * tree with the status of LARVAL, and send
4178 * <base, SA(*), address(SD)>
4181 * IN: mhp: pointer to the pointer to each header.
4182 * OUT: NULL if fail.
4183 * other if success, return pointer to the message to send.
4186 key_getspi(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
4188 struct sadb_address *src0, *dst0;
4189 struct secasindex saidx;
4190 struct secashead *newsah;
4191 struct secasvar *newsav;
4192 struct sockaddr *saddr, *daddr;
4200 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
4201 panic("key_getspi: NULL pointer is passed.");
4203 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
4204 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
4205 ipseclog((LOG_DEBUG, "key_getspi: invalid message is passed.\n"));
4206 return key_senderror(so, m, EINVAL);
4208 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
4209 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
4210 ipseclog((LOG_DEBUG, "key_getspi: invalid message is passed.\n"));
4211 return key_senderror(so, m, EINVAL);
4213 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
4214 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
4215 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
4217 mode = IPSEC_MODE_ANY;
4221 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
4222 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
4224 /* map satype to proto */
4225 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
4226 ipseclog((LOG_DEBUG, "key_getspi: invalid satype is passed.\n"));
4227 return key_senderror(so, m, EINVAL);
4230 /* make sure if port number is zero. */
4231 saddr = (struct sockaddr *)(src0 + 1);
4232 daddr = (struct sockaddr *)(dst0 + 1);
4233 switch (saddr->sa_family) {
4235 if (saddr->sa_len != sizeof(struct sockaddr_in))
4236 return key_senderror(so, m, EINVAL);
4237 ((struct sockaddr_in *)(src0 + 1))->sin_port = 0;
4240 if (saddr->sa_len != sizeof(struct sockaddr_in6))
4241 return key_senderror(so, m, EINVAL);
4242 ((struct sockaddr_in6 *)(src0 + 1))->sin6_port = 0;
4247 switch (daddr->sa_family) {
4249 if (daddr->sa_len != sizeof(struct sockaddr_in))
4250 return key_senderror(so, m, EINVAL);
4251 ((struct sockaddr_in *)(dst0 + 1))->sin_port = 0;
4254 if (daddr->sa_len != sizeof(struct sockaddr_in6))
4255 return key_senderror(so, m, EINVAL);
4256 ((struct sockaddr_in6 *)(dst0 + 1))->sin6_port = 0;
4262 /* XXX boundary check against sa_len */
4263 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
4265 /* SPI allocation */
4266 spi = key_do_getnewspi((struct sadb_spirange *)mhp->ext[SADB_EXT_SPIRANGE],
4269 return key_senderror(so, m, EINVAL);
4271 /* get a SA index */
4272 if ((newsah = key_getsah(&saidx)) == NULL) {
4273 /* create a new SA index */
4274 if ((newsah = key_newsah(&saidx)) == NULL) {
4275 ipseclog((LOG_DEBUG, "key_getspi: No more memory.\n"));
4276 return key_senderror(so, m, ENOBUFS);
4282 newsav = KEY_NEWSAV(m, mhp, newsah, &error);
4283 if (newsav == NULL) {
4284 /* XXX don't free new SA index allocated in above. */
4285 return key_senderror(so, m, error);
4289 newsav->spi = htonl(spi);
4291 #ifndef IPSEC_NONBLOCK_ACQUIRE
4292 /* delete the entry in acqtree */
4293 if (mhp->msg->sadb_msg_seq != 0) {
4295 if ((acq = key_getacqbyseq(mhp->msg->sadb_msg_seq)) != NULL) {
4296 /* reset counter in order to deletion by timehandler. */
4297 acq->created = time_second;
4305 struct sadb_sa *m_sa;
4306 struct sadb_msg *newmsg;
4309 /* create new sadb_msg to reply. */
4310 len = PFKEY_ALIGN8(sizeof(struct sadb_msg)) +
4311 PFKEY_ALIGN8(sizeof(struct sadb_sa));
4313 return key_senderror(so, m, ENOBUFS);
4314 n = m_getb(len, MB_DONTWAIT, MT_DATA, M_PKTHDR);
4316 return key_senderror(so, m, ENOBUFS);
4319 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t));
4320 off = PFKEY_ALIGN8(sizeof(struct sadb_msg));
4322 m_sa = (struct sadb_sa *)(mtod(n, caddr_t) + off);
4323 m_sa->sadb_sa_len = PFKEY_UNIT64(sizeof(struct sadb_sa));
4324 m_sa->sadb_sa_exttype = SADB_EXT_SA;
4325 m_sa->sadb_sa_spi = htonl(spi);
4326 off += PFKEY_ALIGN8(sizeof(struct sadb_sa));
4330 panic("length inconsistency in key_getspi");
4333 n->m_next = key_gather_mbuf(m, mhp, 0, 2, SADB_EXT_ADDRESS_SRC,
4334 SADB_EXT_ADDRESS_DST);
4337 return key_senderror(so, m, ENOBUFS);
4340 if (n->m_len < sizeof(struct sadb_msg)) {
4341 n = m_pullup(n, sizeof(struct sadb_msg));
4343 return key_sendup_mbuf(so, m, KEY_SENDUP_ONE);
4345 n->m_pkthdr.len = m_lengthm(n, NULL);
4347 newmsg = mtod(n, struct sadb_msg *);
4348 newmsg->sadb_msg_seq = newsav->seq;
4349 newmsg->sadb_msg_errno = 0;
4350 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
4353 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
4358 * allocating new SPI
4359 * called by key_getspi().
4365 key_do_getnewspi(struct sadb_spirange *spirange, struct secasindex *saidx)
4369 int count = key_spi_trycnt;
4371 /* set spi range to allocate */
4372 if (spirange != NULL) {
4373 min = spirange->sadb_spirange_min;
4374 max = spirange->sadb_spirange_max;
4376 min = key_spi_minval;
4377 max = key_spi_maxval;
4379 /* IPCOMP needs 2-byte SPI */
4380 if (saidx->proto == IPPROTO_IPCOMP) {
4387 t = min; min = max; max = t;
4392 if (key_checkspidup(saidx, min) != NULL) {
4393 ipseclog((LOG_DEBUG, "key_do_getnewspi: SPI %u exists already.\n", min));
4397 count--; /* taking one cost. */
4405 /* when requesting to allocate spi ranged */
4407 /* generate pseudo-random SPI value ranged. */
4408 newspi = min + (key_random() % (max - min + 1));
4410 if (key_checkspidup(saidx, newspi) == NULL)
4414 if (count == 0 || newspi == 0) {
4415 ipseclog((LOG_DEBUG, "key_do_getnewspi: to allocate spi is failed.\n"));
4421 keystat.getspi_count =
4422 (keystat.getspi_count + key_spi_trycnt - count) / 2;
4428 * SADB_UPDATE processing
4430 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4431 * key(AE), (identity(SD),) (sensitivity)>
4432 * from the ikmpd, and update a secasvar entry whose status is SADB_SASTATE_LARVAL.
4434 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4435 * (identity(SD),) (sensitivity)>
4438 * m will always be freed.
4441 key_update(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
4443 struct sadb_sa *sa0;
4444 struct sadb_address *src0, *dst0;
4445 struct secasindex saidx;
4446 struct secashead *sah;
4447 struct secasvar *sav;
4454 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
4455 panic("key_update: NULL pointer is passed.");
4457 /* map satype to proto */
4458 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
4459 ipseclog((LOG_DEBUG, "key_update: invalid satype is passed.\n"));
4460 return key_senderror(so, m, EINVAL);
4463 if (mhp->ext[SADB_EXT_SA] == NULL ||
4464 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
4465 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
4466 (mhp->msg->sadb_msg_satype == SADB_SATYPE_ESP &&
4467 mhp->ext[SADB_EXT_KEY_ENCRYPT] == NULL) ||
4468 (mhp->msg->sadb_msg_satype == SADB_SATYPE_AH &&
4469 mhp->ext[SADB_EXT_KEY_AUTH] == NULL) ||
4470 (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL &&
4471 mhp->ext[SADB_EXT_LIFETIME_SOFT] == NULL) ||
4472 (mhp->ext[SADB_EXT_LIFETIME_HARD] == NULL &&
4473 mhp->ext[SADB_EXT_LIFETIME_SOFT] != NULL)) {
4474 ipseclog((LOG_DEBUG, "key_update: invalid message is passed.\n"));
4475 return key_senderror(so, m, EINVAL);
4477 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
4478 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
4479 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
4480 ipseclog((LOG_DEBUG, "key_update: invalid message is passed.\n"));
4481 return key_senderror(so, m, EINVAL);
4483 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
4484 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
4485 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
4487 mode = IPSEC_MODE_ANY;
4490 /* XXX boundary checking for other extensions */
4492 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
4493 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
4494 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
4496 /* XXX boundary check against sa_len */
4497 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
4499 /* get a SA header */
4500 if ((sah = key_getsah(&saidx)) == NULL) {
4501 ipseclog((LOG_DEBUG, "key_update: no SA index found.\n"));
4502 return key_senderror(so, m, ENOENT);
4505 /* set spidx if there */
4507 error = key_setident(sah, m, mhp);
4509 return key_senderror(so, m, error);
4511 /* find a SA with sequence number. */
4512 #ifdef IPSEC_DOSEQCHECK
4513 if (mhp->msg->sadb_msg_seq != 0
4514 && (sav = key_getsavbyseq(sah, mhp->msg->sadb_msg_seq)) == NULL) {
4515 ipseclog((LOG_DEBUG,
4516 "key_update: no larval SA with sequence %u exists.\n",
4517 mhp->msg->sadb_msg_seq));
4518 return key_senderror(so, m, ENOENT);
4521 if ((sav = key_getsavbyspi(sah, sa0->sadb_sa_spi)) == NULL) {
4522 ipseclog((LOG_DEBUG,
4523 "key_update: no such a SA found (spi:%u)\n",
4524 (u_int32_t)ntohl(sa0->sadb_sa_spi)));
4525 return key_senderror(so, m, EINVAL);
4529 /* validity check */
4530 if (sav->sah->saidx.proto != proto) {
4531 ipseclog((LOG_DEBUG,
4532 "key_update: protocol mismatched (DB=%u param=%u)\n",
4533 sav->sah->saidx.proto, proto));
4534 return key_senderror(so, m, EINVAL);
4536 #ifdef IPSEC_DOSEQCHECK
4537 if (sav->spi != sa0->sadb_sa_spi) {
4538 ipseclog((LOG_DEBUG,
4539 "key_update: SPI mismatched (DB:%u param:%u)\n",
4540 (u_int32_t)ntohl(sav->spi),
4541 (u_int32_t)ntohl(sa0->sadb_sa_spi)));
4542 return key_senderror(so, m, EINVAL);
4545 if (sav->pid != mhp->msg->sadb_msg_pid) {
4546 ipseclog((LOG_DEBUG,
4547 "key_update: pid mismatched (DB:%u param:%u)\n",
4548 sav->pid, mhp->msg->sadb_msg_pid));
4549 return key_senderror(so, m, EINVAL);
4552 /* copy sav values */
4553 error = key_setsaval(sav, m, mhp);
4556 return key_senderror(so, m, error);
4559 /* check SA values to be mature. */
4560 if ((mhp->msg->sadb_msg_errno = key_mature(sav)) != 0) {
4562 return key_senderror(so, m, 0);
4568 /* set msg buf from mhp */
4569 n = key_getmsgbuf_x1(m, mhp);
4571 ipseclog((LOG_DEBUG, "key_update: No more memory.\n"));
4572 return key_senderror(so, m, ENOBUFS);
4576 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
4581 * search SAD with sequence for a SA which state is SADB_SASTATE_LARVAL.
4582 * only called by key_update().
4585 * others : found, pointer to a SA.
4587 #ifdef IPSEC_DOSEQCHECK
4588 static struct secasvar *
4589 key_getsavbyseq(struct secashead *sah, u_int32_t seq)
4591 struct secasvar *sav;
4594 state = SADB_SASTATE_LARVAL;
4596 /* search SAD with sequence number ? */
4597 LIST_FOREACH(sav, &sah->savtree[state], chain) {
4599 KEY_CHKSASTATE(state, sav->state, "key_getsabyseq");
4601 if (sav->seq == seq) {
4603 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
4604 kprintf("DP key_getsavbyseq cause "
4605 "refcnt++:%d SA:%p\n",
4616 * SADB_ADD processing
4617 * add an entry to SA database, when received
4618 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4619 * key(AE), (identity(SD),) (sensitivity)>
4622 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4623 * (identity(SD),) (sensitivity)>
4626 * IGNORE identity and sensitivity messages.
4628 * m will always be freed.
4631 key_add(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
4633 struct sadb_sa *sa0;
4634 struct sadb_address *src0, *dst0;
4635 struct secasindex saidx;
4636 struct secashead *newsah;
4637 struct secasvar *newsav;
4644 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
4645 panic("key_add: NULL pointer is passed.");
4647 /* map satype to proto */
4648 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
4649 ipseclog((LOG_DEBUG, "key_add: invalid satype is passed.\n"));
4650 return key_senderror(so, m, EINVAL);
4653 if (mhp->ext[SADB_EXT_SA] == NULL ||
4654 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
4655 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
4656 (mhp->msg->sadb_msg_satype == SADB_SATYPE_ESP &&
4657 mhp->ext[SADB_EXT_KEY_ENCRYPT] == NULL) ||
4658 (mhp->msg->sadb_msg_satype == SADB_SATYPE_AH &&
4659 mhp->ext[SADB_EXT_KEY_AUTH] == NULL) ||
4660 (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL &&
4661 mhp->ext[SADB_EXT_LIFETIME_SOFT] == NULL) ||
4662 (mhp->ext[SADB_EXT_LIFETIME_HARD] == NULL &&
4663 mhp->ext[SADB_EXT_LIFETIME_SOFT] != NULL)) {
4664 ipseclog((LOG_DEBUG, "key_add: invalid message is passed.\n"));
4665 return key_senderror(so, m, EINVAL);
4667 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
4668 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
4669 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
4671 ipseclog((LOG_DEBUG, "key_add: invalid message is passed.\n"));
4672 return key_senderror(so, m, EINVAL);
4674 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
4675 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
4676 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
4678 mode = IPSEC_MODE_ANY;
4682 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
4683 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
4684 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
4686 /* XXX boundary check against sa_len */
4687 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
4689 /* get a SA header */
4690 if ((newsah = key_getsah(&saidx)) == NULL) {
4691 /* create a new SA header */
4692 if ((newsah = key_newsah(&saidx)) == NULL) {
4693 ipseclog((LOG_DEBUG, "key_add: No more memory.\n"));
4694 return key_senderror(so, m, ENOBUFS);
4698 /* set spidx if there */
4700 error = key_setident(newsah, m, mhp);
4702 return key_senderror(so, m, error);
4705 /* create new SA entry. */
4706 /* We can create new SA only if SPI is differenct. */
4707 if (key_getsavbyspi(newsah, sa0->sadb_sa_spi)) {
4708 ipseclog((LOG_DEBUG, "key_add: SA already exists.\n"));
4709 return key_senderror(so, m, EEXIST);
4711 newsav = KEY_NEWSAV(m, mhp, newsah, &error);
4712 if (newsav == NULL) {
4713 return key_senderror(so, m, error);
4716 /* check SA values to be mature. */
4717 if ((error = key_mature(newsav)) != 0) {
4718 KEY_FREESAV(&newsav);
4719 return key_senderror(so, m, error);
4723 * don't call key_freesav() here, as we would like to keep the SA
4724 * in the database on success.
4730 /* set msg buf from mhp */
4731 n = key_getmsgbuf_x1(m, mhp);
4733 ipseclog((LOG_DEBUG, "key_update: No more memory.\n"));
4734 return key_senderror(so, m, ENOBUFS);
4738 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
4744 key_setident(struct secashead *sah, struct mbuf *m,
4745 const struct sadb_msghdr *mhp)
4747 const struct sadb_ident *idsrc, *iddst;
4748 int idsrclen, iddstlen;
4751 if (sah == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
4752 panic("key_setident: NULL pointer is passed.");
4754 /* don't make buffer if not there */
4755 if (mhp->ext[SADB_EXT_IDENTITY_SRC] == NULL &&
4756 mhp->ext[SADB_EXT_IDENTITY_DST] == NULL) {
4762 if (mhp->ext[SADB_EXT_IDENTITY_SRC] == NULL ||
4763 mhp->ext[SADB_EXT_IDENTITY_DST] == NULL) {
4764 ipseclog((LOG_DEBUG, "key_setident: invalid identity.\n"));
4768 idsrc = (const struct sadb_ident *)mhp->ext[SADB_EXT_IDENTITY_SRC];
4769 iddst = (const struct sadb_ident *)mhp->ext[SADB_EXT_IDENTITY_DST];
4770 idsrclen = mhp->extlen[SADB_EXT_IDENTITY_SRC];
4771 iddstlen = mhp->extlen[SADB_EXT_IDENTITY_DST];
4773 /* validity check */
4774 if (idsrc->sadb_ident_type != iddst->sadb_ident_type) {
4775 ipseclog((LOG_DEBUG, "key_setident: ident type mismatch.\n"));
4779 switch (idsrc->sadb_ident_type) {
4780 case SADB_IDENTTYPE_PREFIX:
4781 case SADB_IDENTTYPE_FQDN:
4782 case SADB_IDENTTYPE_USERFQDN:
4784 /* XXX do nothing */
4790 /* make structure */
4791 KMALLOC(sah->idents, struct sadb_ident *, idsrclen);
4792 if (sah->idents == NULL) {
4793 ipseclog((LOG_DEBUG, "key_setident: No more memory.\n"));
4796 KMALLOC(sah->identd, struct sadb_ident *, iddstlen);
4797 if (sah->identd == NULL) {
4800 ipseclog((LOG_DEBUG, "key_setident: No more memory.\n"));
4803 bcopy(idsrc, sah->idents, idsrclen);
4804 bcopy(iddst, sah->identd, iddstlen);
4810 * m will not be freed on return.
4811 * it is caller's responsibility to free the result.
4813 static struct mbuf *
4814 key_getmsgbuf_x1(struct mbuf *m, const struct sadb_msghdr *mhp)
4819 if (m == NULL || mhp == NULL || mhp->msg == NULL)
4820 panic("key_getmsgbuf_x1: NULL pointer is passed.");
4822 /* create new sadb_msg to reply. */
4823 n = key_gather_mbuf(m, mhp, 1, 9, SADB_EXT_RESERVED,
4824 SADB_EXT_SA, SADB_X_EXT_SA2,
4825 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST,
4826 SADB_EXT_LIFETIME_HARD, SADB_EXT_LIFETIME_SOFT,
4827 SADB_EXT_IDENTITY_SRC, SADB_EXT_IDENTITY_DST);
4831 if (n->m_len < sizeof(struct sadb_msg)) {
4832 n = m_pullup(n, sizeof(struct sadb_msg));
4836 mtod(n, struct sadb_msg *)->sadb_msg_errno = 0;
4837 mtod(n, struct sadb_msg *)->sadb_msg_len =
4838 PFKEY_UNIT64(n->m_pkthdr.len);
4843 static int key_delete_all (struct socket *, struct mbuf *,
4844 const struct sadb_msghdr *, u_int16_t);
4847 * SADB_DELETE processing
4849 * <base, SA(*), address(SD)>
4850 * from the ikmpd, and set SADB_SASTATE_DEAD,
4852 * <base, SA(*), address(SD)>
4855 * m will always be freed.
4858 key_delete(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
4860 struct sadb_sa *sa0;
4861 struct sadb_address *src0, *dst0;
4862 struct secasindex saidx;
4863 struct secashead *sah;
4864 struct secasvar *sav = NULL;
4868 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
4869 panic("key_delete: NULL pointer is passed.");
4871 /* map satype to proto */
4872 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
4873 ipseclog((LOG_DEBUG, "key_delete: invalid satype is passed.\n"));
4874 return key_senderror(so, m, EINVAL);
4877 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
4878 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
4879 ipseclog((LOG_DEBUG, "key_delete: invalid message is passed.\n"));
4880 return key_senderror(so, m, EINVAL);
4883 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
4884 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
4885 ipseclog((LOG_DEBUG, "key_delete: invalid message is passed.\n"));
4886 return key_senderror(so, m, EINVAL);
4889 if (mhp->ext[SADB_EXT_SA] == NULL) {
4891 * Caller wants us to delete all non-LARVAL SAs
4892 * that match the src/dst. This is used during
4893 * IKE INITIAL-CONTACT.
4895 ipseclog((LOG_DEBUG, "key_delete: doing delete all.\n"));
4896 return key_delete_all(so, m, mhp, proto);
4897 } else if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa)) {
4898 ipseclog((LOG_DEBUG, "key_delete: invalid message is passed.\n"));
4899 return key_senderror(so, m, EINVAL);
4902 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
4903 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
4904 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
4906 /* XXX boundary check against sa_len */
4907 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
4909 /* get a SA header */
4910 LIST_FOREACH(sah, &sahtree, chain) {
4911 if (sah->state == SADB_SASTATE_DEAD)
4913 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
4916 /* get a SA with SPI. */
4917 sav = key_getsavbyspi(sah, sa0->sadb_sa_spi);
4922 ipseclog((LOG_DEBUG, "key_delete: no SA found.\n"));
4923 return key_senderror(so, m, ENOENT);
4926 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4931 struct sadb_msg *newmsg;
4933 /* create new sadb_msg to reply. */
4934 n = key_gather_mbuf(m, mhp, 1, 4, SADB_EXT_RESERVED,
4935 SADB_EXT_SA, SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
4937 return key_senderror(so, m, ENOBUFS);
4939 if (n->m_len < sizeof(struct sadb_msg)) {
4940 n = m_pullup(n, sizeof(struct sadb_msg));
4942 return key_senderror(so, m, ENOBUFS);
4944 newmsg = mtod(n, struct sadb_msg *);
4945 newmsg->sadb_msg_errno = 0;
4946 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
4949 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
4954 * delete all SAs for src/dst. Called from key_delete().
4957 key_delete_all(struct socket *so, struct mbuf *m,
4958 const struct sadb_msghdr *mhp, u_int16_t proto)
4960 struct sadb_address *src0, *dst0;
4961 struct secasindex saidx;
4962 struct secashead *sah;
4963 struct secasvar *sav, *nextsav;
4964 u_int stateidx, state;
4966 struct sadb_msg *newmsg;
4968 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
4969 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
4971 /* XXX boundary check against sa_len */
4972 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
4974 LIST_FOREACH(sah, &sahtree, chain) {
4975 if (sah->state == SADB_SASTATE_DEAD)
4977 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
4980 /* Delete all non-LARVAL SAs. */
4981 for (stateidx = 0; stateidx < NELEM(saorder_state_alive);
4983 state = saorder_state_alive[stateidx];
4984 if (state == SADB_SASTATE_LARVAL)
4986 LIST_FOREACH_MUTABLE(sav, &sah->savtree[state], chain,
4989 if (sav->state != state) {
4990 ipseclog((LOG_DEBUG, "key_delete_all: "
4991 "invalid sav->state "
4992 "(queue: %d SA: %d)\n",
4993 state, sav->state));
4997 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
5003 /* create new sadb_msg to reply. */
5004 n = key_gather_mbuf(m, mhp, 1, 3, SADB_EXT_RESERVED,
5005 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
5007 return key_senderror(so, m, ENOBUFS);
5009 if (n->m_len < sizeof(struct sadb_msg)) {
5010 n = m_pullup(n, sizeof(struct sadb_msg));
5012 return key_senderror(so, m, ENOBUFS);
5014 newmsg = mtod(n, struct sadb_msg *);
5015 newmsg->sadb_msg_errno = 0;
5016 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
5019 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5023 * SADB_GET processing
5025 * <base, SA(*), address(SD)>
5026 * from the ikmpd, and get a SP and a SA to respond,
5028 * <base, SA, (lifetime(HSC),) address(SD), (address(P),) key(AE),
5029 * (identity(SD),) (sensitivity)>
5032 * m will always be freed.
5035 key_get(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
5037 struct sadb_sa *sa0;
5038 struct sadb_address *src0, *dst0;
5039 struct secasindex saidx;
5040 struct secashead *sah;
5041 struct secasvar *sav = NULL;
5045 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
5046 panic("key_get: NULL pointer is passed.");
5048 /* map satype to proto */
5049 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5050 ipseclog((LOG_DEBUG, "key_get: invalid satype is passed.\n"));
5051 return key_senderror(so, m, EINVAL);
5054 if (mhp->ext[SADB_EXT_SA] == NULL ||
5055 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5056 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
5057 ipseclog((LOG_DEBUG, "key_get: invalid message is passed.\n"));
5058 return key_senderror(so, m, EINVAL);
5060 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
5061 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5062 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5063 ipseclog((LOG_DEBUG, "key_get: invalid message is passed.\n"));
5064 return key_senderror(so, m, EINVAL);
5067 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5068 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
5069 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
5071 /* XXX boundary check against sa_len */
5072 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5074 /* get a SA header */
5075 LIST_FOREACH(sah, &sahtree, chain) {
5076 if (sah->state == SADB_SASTATE_DEAD)
5078 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5081 /* get a SA with SPI. */
5082 sav = key_getsavbyspi(sah, sa0->sadb_sa_spi);
5087 ipseclog((LOG_DEBUG, "key_get: no SA found.\n"));
5088 return key_senderror(so, m, ENOENT);
5095 /* map proto to satype */
5096 if ((satype = key_proto2satype(sah->saidx.proto)) == 0) {
5097 ipseclog((LOG_DEBUG, "key_get: there was invalid proto in SAD.\n"));
5098 return key_senderror(so, m, EINVAL);
5101 /* create new sadb_msg to reply. */
5102 n = key_setdumpsa(sav, SADB_GET, satype, mhp->msg->sadb_msg_seq,
5103 mhp->msg->sadb_msg_pid);
5105 return key_senderror(so, m, ENOBUFS);
5108 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
5112 /* XXX make it sysctl-configurable? */
5114 key_getcomb_setlifetime(struct sadb_comb *comb)
5117 comb->sadb_comb_soft_allocations = 1;
5118 comb->sadb_comb_hard_allocations = 1;
5119 comb->sadb_comb_soft_bytes = 0;
5120 comb->sadb_comb_hard_bytes = 0;
5121 comb->sadb_comb_hard_addtime = 86400; /* 1 day */
5122 comb->sadb_comb_soft_addtime = comb->sadb_comb_soft_addtime * 80 / 100;
5123 comb->sadb_comb_soft_usetime = 28800; /* 8 hours */
5124 comb->sadb_comb_hard_usetime = comb->sadb_comb_hard_usetime * 80 / 100;
5128 * XXX reorder combinations by preference
5129 * XXX no idea if the user wants ESP authentication or not
5131 static struct mbuf *
5132 key_getcomb_esp(void)
5134 struct sadb_comb *comb;
5135 struct enc_xform *algo;
5136 struct mbuf *result = NULL, *m, *n;
5140 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
5143 for (i = 1; i <= SADB_EALG_MAX; i++) {
5144 algo = esp_algorithm_lookup(i);
5148 /* discard algorithms with key size smaller than system min */
5149 if (_BITS(algo->maxkey) < ipsec_esp_keymin)
5151 if (_BITS(algo->minkey) < ipsec_esp_keymin)
5152 encmin = ipsec_esp_keymin;
5154 encmin = _BITS(algo->minkey);
5157 m = key_getcomb_ah();
5160 ("key_getcomb_esp: l=%u > MLEN=%lu",
5162 MGET(m, MB_DONTWAIT, MT_DATA);
5167 bzero(mtod(m, caddr_t), m->m_len);
5173 totlen = m_lengthm(m, NULL);
5174 KASSERT((totlen % l) == 0,
5175 ("key_getcomb_esp: totlen=%u, l=%u", totlen, l));
5177 for (off = 0; off < totlen; off += l) {
5178 n = m_pulldown(m, off, l, &o);
5180 /* m is already freed */
5183 comb = (struct sadb_comb *)(mtod(n, caddr_t) + o);
5184 bzero(comb, sizeof(*comb));
5185 key_getcomb_setlifetime(comb);
5186 comb->sadb_comb_encrypt = i;
5187 comb->sadb_comb_encrypt_minbits = encmin;
5188 comb->sadb_comb_encrypt_maxbits = _BITS(algo->maxkey);
5207 const struct auth_hash *ah,
5212 *min = *max = ah->keysize;
5213 if (ah->keysize == 0) {
5215 * Transform takes arbitrary key size but algorithm
5216 * key size is restricted. Enforce this here.
5219 case SADB_X_AALG_MD5: *min = *max = 16; break;
5220 case SADB_X_AALG_SHA: *min = *max = 20; break;
5221 case SADB_X_AALG_NULL: *min = 1; *max = 256; break;
5223 DPRINTF(("key_getsizes_ah: unknown AH algorithm %u\n",
5231 * XXX reorder combinations by preference
5233 static struct mbuf *
5234 key_getcomb_ah(void)
5236 struct sadb_comb *comb;
5237 struct auth_hash *algo;
5239 u_int16_t minkeysize, maxkeysize;
5241 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
5244 for (i = 1; i <= SADB_AALG_MAX; i++) {
5246 /* we prefer HMAC algorithms, not old algorithms */
5247 if (i != SADB_AALG_SHA1HMAC && i != SADB_AALG_MD5HMAC)
5250 algo = ah_algorithm_lookup(i);
5253 key_getsizes_ah(algo, i, &minkeysize, &maxkeysize);
5254 /* discard algorithms with key size smaller than system min */
5255 if (_BITS(minkeysize) < ipsec_ah_keymin)
5260 ("key_getcomb_ah: l=%u > MLEN=%lu",
5262 MGET(m, MB_DONTWAIT, MT_DATA);
5269 M_PREPEND(m, l, MB_DONTWAIT);
5273 comb = mtod(m, struct sadb_comb *);
5274 bzero(comb, sizeof(*comb));
5275 key_getcomb_setlifetime(comb);
5276 comb->sadb_comb_auth = i;
5277 comb->sadb_comb_auth_minbits = _BITS(minkeysize);
5278 comb->sadb_comb_auth_maxbits = _BITS(maxkeysize);
5285 * not really an official behavior. discussed in pf_key@inner.net in Sep2000.
5286 * XXX reorder combinations by preference
5288 static struct mbuf *
5289 key_getcomb_ipcomp(void)
5291 struct sadb_comb *comb;
5292 struct comp_algo *algo;
5295 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
5298 for (i = 1; i <= SADB_X_CALG_MAX; i++) {
5299 algo = ipcomp_algorithm_lookup(i);
5305 ("key_getcomb_ipcomp: l=%u > MLEN=%lu",
5307 MGET(m, MB_DONTWAIT, MT_DATA);
5314 M_PREPEND(m, l, MB_DONTWAIT);
5318 comb = mtod(m, struct sadb_comb *);
5319 bzero(comb, sizeof(*comb));
5320 key_getcomb_setlifetime(comb);
5321 comb->sadb_comb_encrypt = i;
5322 /* what should we set into sadb_comb_*_{min,max}bits? */
5329 * XXX no way to pass mode (transport/tunnel) to userland
5330 * XXX replay checking?
5331 * XXX sysctl interface to ipsec_{ah,esp}_keymin
5333 static struct mbuf *
5334 key_getprop(const struct secasindex *saidx)
5336 struct sadb_prop *prop;
5338 const int l = PFKEY_ALIGN8(sizeof(struct sadb_prop));
5340 switch (saidx->proto) {
5342 m = key_getcomb_esp();
5345 m = key_getcomb_ah();
5347 case IPPROTO_IPCOMP:
5348 m = key_getcomb_ipcomp();
5356 M_PREPEND(m, l, MB_DONTWAIT);
5360 prop = mtod(m, struct sadb_prop *);
5361 bzero(prop, sizeof(*prop));
5362 prop->sadb_prop_len = PFKEY_UNIT64(m_lengthm(m, NULL));
5363 prop->sadb_prop_exttype = SADB_EXT_PROPOSAL;
5364 prop->sadb_prop_replay = 32; /* XXX */
5370 * SADB_ACQUIRE processing called by key_checkrequest() and key_acquire2().
5372 * <base, SA, address(SD), (address(P)), x_policy,
5373 * (identity(SD),) (sensitivity,) proposal>
5374 * to KMD, and expect to receive
5375 * <base> with SADB_ACQUIRE if error occured,
5377 * <base, src address, dst address, (SPI range)> with SADB_GETSPI
5378 * from KMD by PF_KEY.
5380 * XXX x_policy is outside of RFC2367 (KAME extension).
5381 * XXX sensitivity is not supported.
5382 * XXX for ipcomp, RFC2367 does not define how to fill in proposal.
5383 * see comment for key_getcomb_ipcomp().
5387 * others: error number
5390 key_acquire(const struct secasindex *saidx, struct secpolicy *sp)
5392 struct mbuf *result = NULL, *m;
5393 #ifndef IPSEC_NONBLOCK_ACQUIRE
5394 struct secacq *newacq;
5401 KASSERT(saidx != NULL, ("key_acquire: null saidx"));
5402 satype = key_proto2satype(saidx->proto);
5403 KASSERT(satype != 0,
5404 ("key_acquire: null satype, protocol %u", saidx->proto));
5406 #ifndef IPSEC_NONBLOCK_ACQUIRE
5408 * We never do anything about acquirng SA. There is anather
5409 * solution that kernel blocks to send SADB_ACQUIRE message until
5410 * getting something message from IKEd. In later case, to be
5411 * managed with ACQUIRING list.
5413 /* Get an entry to check whether sending message or not. */
5414 if ((newacq = key_getacq(saidx)) != NULL) {
5415 if (key_blockacq_count < newacq->count) {
5416 /* reset counter and do send message. */
5419 /* increment counter and do nothing. */
5424 /* make new entry for blocking to send SADB_ACQUIRE. */
5425 if ((newacq = key_newacq(saidx)) == NULL)
5428 /* add to acqtree */
5429 LIST_INSERT_HEAD(&acqtree, newacq, chain);
5434 #ifndef IPSEC_NONBLOCK_ACQUIRE
5437 seq = (acq_seq = (acq_seq == ~0 ? 1 : ++acq_seq));
5439 m = key_setsadbmsg(SADB_ACQUIRE, 0, satype, seq, 0, 0);
5446 /* set sadb_address for saidx's. */
5447 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
5448 &saidx->src.sa, FULLMASK, IPSEC_ULPROTO_ANY);
5455 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
5456 &saidx->dst.sa, FULLMASK, IPSEC_ULPROTO_ANY);
5463 /* XXX proxy address (optional) */
5465 /* set sadb_x_policy */
5467 m = key_setsadbxpolicy(sp->policy, sp->spidx.dir, sp->id);
5475 /* XXX identity (optional) */
5477 if (idexttype && fqdn) {
5478 /* create identity extension (FQDN) */
5479 struct sadb_ident *id;
5482 fqdnlen = strlen(fqdn) + 1; /* +1 for terminating-NUL */
5483 id = (struct sadb_ident *)p;
5484 bzero(id, sizeof(*id) + PFKEY_ALIGN8(fqdnlen));
5485 id->sadb_ident_len = PFKEY_UNIT64(sizeof(*id) + PFKEY_ALIGN8(fqdnlen));
5486 id->sadb_ident_exttype = idexttype;
5487 id->sadb_ident_type = SADB_IDENTTYPE_FQDN;
5488 bcopy(fqdn, id + 1, fqdnlen);
5489 p += sizeof(struct sadb_ident) + PFKEY_ALIGN8(fqdnlen);
5493 /* create identity extension (USERFQDN) */
5494 struct sadb_ident *id;
5498 /* +1 for terminating-NUL */
5499 userfqdnlen = strlen(userfqdn) + 1;
5502 id = (struct sadb_ident *)p;
5503 bzero(id, sizeof(*id) + PFKEY_ALIGN8(userfqdnlen));
5504 id->sadb_ident_len = PFKEY_UNIT64(sizeof(*id) + PFKEY_ALIGN8(userfqdnlen));
5505 id->sadb_ident_exttype = idexttype;
5506 id->sadb_ident_type = SADB_IDENTTYPE_USERFQDN;
5507 /* XXX is it correct? */
5508 if (curproc && curproc->p_cred)
5509 id->sadb_ident_id = curproc->p_cred->p_ruid;
5510 if (userfqdn && userfqdnlen)
5511 bcopy(userfqdn, id + 1, userfqdnlen);
5512 p += sizeof(struct sadb_ident) + PFKEY_ALIGN8(userfqdnlen);
5516 /* XXX sensitivity (optional) */
5518 /* create proposal/combination extension */
5519 m = key_getprop(saidx);
5522 * spec conformant: always attach proposal/combination extension,
5523 * the problem is that we have no way to attach it for ipcomp,
5524 * due to the way sadb_comb is declared in RFC2367.
5533 * outside of spec; make proposal/combination extension optional.
5539 if ((result->m_flags & M_PKTHDR) == 0) {
5544 if (result->m_len < sizeof(struct sadb_msg)) {
5545 result = m_pullup(result, sizeof(struct sadb_msg));
5546 if (result == NULL) {
5551 result->m_pkthdr.len = m_lengthm(result, NULL);
5552 mtod(result, struct sadb_msg *)->sadb_msg_len =
5553 PFKEY_UNIT64(result->m_pkthdr.len);
5555 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
5563 #ifndef IPSEC_NONBLOCK_ACQUIRE
5564 static struct secacq *
5565 key_newacq(const struct secasindex *saidx)
5567 struct secacq *newacq;
5570 KMALLOC(newacq, struct secacq *, sizeof(struct secacq));
5571 if (newacq == NULL) {
5572 ipseclog((LOG_DEBUG, "key_newacq: No more memory.\n"));
5575 bzero(newacq, sizeof(*newacq));
5578 bcopy(saidx, &newacq->saidx, sizeof(newacq->saidx));
5579 newacq->seq = (acq_seq == ~0 ? 1 : ++acq_seq);
5580 newacq->created = time_second;
5586 static struct secacq *
5587 key_getacq(const struct secasindex *saidx)
5591 LIST_FOREACH(acq, &acqtree, chain) {
5592 if (key_cmpsaidx(saidx, &acq->saidx, CMP_EXACTLY))
5599 static struct secacq *
5600 key_getacqbyseq(u_int32_t seq)
5604 LIST_FOREACH(acq, &acqtree, chain) {
5605 if (acq->seq == seq)
5613 static struct secspacq *
5614 key_newspacq(struct secpolicyindex *spidx)
5616 struct secspacq *acq;
5619 KMALLOC(acq, struct secspacq *, sizeof(struct secspacq));
5621 ipseclog((LOG_DEBUG, "key_newspacq: No more memory.\n"));
5624 bzero(acq, sizeof(*acq));
5627 bcopy(spidx, &acq->spidx, sizeof(acq->spidx));
5628 acq->created = time_second;
5634 static struct secspacq *
5635 key_getspacq(struct secpolicyindex *spidx)
5637 struct secspacq *acq;
5639 LIST_FOREACH(acq, &spacqtree, chain) {
5640 if (key_cmpspidx_exactly(spidx, &acq->spidx))
5648 * SADB_ACQUIRE processing,
5649 * in first situation, is receiving
5651 * from the ikmpd, and clear sequence of its secasvar entry.
5653 * In second situation, is receiving
5654 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
5655 * from a user land process, and return
5656 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
5659 * m will always be freed.
5662 key_acquire2(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
5664 const struct sadb_address *src0, *dst0;
5665 struct secasindex saidx;
5666 struct secashead *sah;
5671 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
5672 panic("key_acquire2: NULL pointer is passed.");
5675 * Error message from KMd.
5676 * We assume that if error was occured in IKEd, the length of PFKEY
5677 * message is equal to the size of sadb_msg structure.
5678 * We do not raise error even if error occured in this function.
5680 if (mhp->msg->sadb_msg_len == PFKEY_UNIT64(sizeof(struct sadb_msg))) {
5681 #ifndef IPSEC_NONBLOCK_ACQUIRE
5684 /* check sequence number */
5685 if (mhp->msg->sadb_msg_seq == 0) {
5686 ipseclog((LOG_DEBUG, "key_acquire2: must specify sequence number.\n"));
5691 if ((acq = key_getacqbyseq(mhp->msg->sadb_msg_seq)) == NULL) {
5693 * the specified larval SA is already gone, or we got
5694 * a bogus sequence number. we can silently ignore it.
5700 /* reset acq counter in order to deletion by timehander. */
5701 acq->created = time_second;
5709 * This message is from user land.
5712 /* map satype to proto */
5713 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5714 ipseclog((LOG_DEBUG, "key_acquire2: invalid satype is passed.\n"));
5715 return key_senderror(so, m, EINVAL);
5718 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5719 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
5720 mhp->ext[SADB_EXT_PROPOSAL] == NULL) {
5722 ipseclog((LOG_DEBUG, "key_acquire2: invalid message is passed.\n"));
5723 return key_senderror(so, m, EINVAL);
5725 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5726 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
5727 mhp->extlen[SADB_EXT_PROPOSAL] < sizeof(struct sadb_prop)) {
5729 ipseclog((LOG_DEBUG, "key_acquire2: invalid message is passed.\n"));
5730 return key_senderror(so, m, EINVAL);
5733 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
5734 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
5736 /* XXX boundary check against sa_len */
5737 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5739 /* get a SA index */
5740 LIST_FOREACH(sah, &sahtree, chain) {
5741 if (sah->state == SADB_SASTATE_DEAD)
5743 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_MODE_REQID))
5747 ipseclog((LOG_DEBUG, "key_acquire2: a SA exists already.\n"));
5748 return key_senderror(so, m, EEXIST);
5751 error = key_acquire(&saidx, NULL);
5753 ipseclog((LOG_DEBUG, "key_acquire2: error %d returned "
5754 "from key_acquire.\n", mhp->msg->sadb_msg_errno));
5755 return key_senderror(so, m, error);
5758 return key_sendup_mbuf(so, m, KEY_SENDUP_REGISTERED);
5762 * SADB_REGISTER processing.
5763 * If SATYPE_UNSPEC has been passed as satype, only return sabd_supported.
5766 * from the ikmpd, and register a socket to send PF_KEY messages,
5770 * If socket is detached, must free from regnode.
5772 * m will always be freed.
5775 key_register(struct socket *so, struct mbuf *m,
5776 const struct sadb_msghdr *mhp)
5778 struct secreg *reg, *newreg = NULL;
5781 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
5782 panic("key_register: NULL pointer is passed.");
5784 /* check for invalid register message */
5785 if (mhp->msg->sadb_msg_satype >= NELEM(regtree))
5786 return key_senderror(so, m, EINVAL);
5788 /* When SATYPE_UNSPEC is specified, only return sabd_supported. */
5789 if (mhp->msg->sadb_msg_satype == SADB_SATYPE_UNSPEC)
5792 /* check whether existing or not */
5793 LIST_FOREACH(reg, ®tree[mhp->msg->sadb_msg_satype], chain) {
5794 if (reg->so == so) {
5795 ipseclog((LOG_DEBUG, "key_register: socket exists already.\n"));
5796 return key_senderror(so, m, EEXIST);
5800 /* create regnode */
5801 KMALLOC(newreg, struct secreg *, sizeof(*newreg));
5802 if (newreg == NULL) {
5803 ipseclog((LOG_DEBUG, "key_register: No more memory.\n"));
5804 return key_senderror(so, m, ENOBUFS);
5806 bzero((caddr_t)newreg, sizeof(*newreg));
5809 ((struct keycb *)sotorawcb(so))->kp_registered++;
5811 /* add regnode to regtree. */
5812 LIST_INSERT_HEAD(®tree[mhp->msg->sadb_msg_satype], newreg, chain);
5817 struct sadb_msg *newmsg;
5818 struct sadb_supported *sup;
5819 u_int len, alen, elen;
5822 struct sadb_alg *alg;
5824 /* create new sadb_msg to reply. */
5826 for (i = 1; i <= SADB_AALG_MAX; i++) {
5827 if (ah_algorithm_lookup(i))
5828 alen += sizeof(struct sadb_alg);
5831 alen += sizeof(struct sadb_supported);
5833 for (i = 1; i <= SADB_EALG_MAX; i++) {
5834 if (esp_algorithm_lookup(i))
5835 elen += sizeof(struct sadb_alg);
5838 elen += sizeof(struct sadb_supported);
5840 len = sizeof(struct sadb_msg) + alen + elen;
5843 return key_senderror(so, m, ENOBUFS);
5844 n = m_getb(len, MB_DONTWAIT, MT_DATA, M_PKTHDR);
5846 return key_senderror(so, m, ENOBUFS);
5847 n->m_pkthdr.len = n->m_len = len;
5849 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t));
5850 newmsg = mtod(n, struct sadb_msg *);
5851 newmsg->sadb_msg_errno = 0;
5852 newmsg->sadb_msg_len = PFKEY_UNIT64(len);
5853 off = PFKEY_ALIGN8(sizeof(struct sadb_msg));
5855 /* for authentication algorithm */
5857 sup = (struct sadb_supported *)(mtod(n, caddr_t) + off);
5858 sup->sadb_supported_len = PFKEY_UNIT64(alen);
5859 sup->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
5860 off += PFKEY_ALIGN8(sizeof(*sup));
5862 for (i = 1; i <= SADB_AALG_MAX; i++) {
5863 struct auth_hash *aalgo;
5864 u_int16_t minkeysize, maxkeysize;
5866 aalgo = ah_algorithm_lookup(i);
5869 alg = (struct sadb_alg *)(mtod(n, caddr_t) + off);
5870 alg->sadb_alg_id = i;
5871 alg->sadb_alg_ivlen = 0;
5872 key_getsizes_ah(aalgo, i, &minkeysize, &maxkeysize);
5873 alg->sadb_alg_minbits = _BITS(minkeysize);
5874 alg->sadb_alg_maxbits = _BITS(maxkeysize);
5875 off += PFKEY_ALIGN8(sizeof(*alg));
5879 /* for encryption algorithm */
5881 sup = (struct sadb_supported *)(mtod(n, caddr_t) + off);
5882 sup->sadb_supported_len = PFKEY_UNIT64(elen);
5883 sup->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT;
5884 off += PFKEY_ALIGN8(sizeof(*sup));
5886 for (i = 1; i <= SADB_EALG_MAX; i++) {
5887 struct enc_xform *ealgo;
5889 ealgo = esp_algorithm_lookup(i);
5892 alg = (struct sadb_alg *)(mtod(n, caddr_t) + off);
5893 alg->sadb_alg_id = i;
5894 alg->sadb_alg_ivlen = ealgo->blocksize;
5895 alg->sadb_alg_minbits = _BITS(ealgo->minkey);
5896 alg->sadb_alg_maxbits = _BITS(ealgo->maxkey);
5897 off += PFKEY_ALIGN8(sizeof(struct sadb_alg));
5903 panic("length assumption failed in key_register");
5907 return key_sendup_mbuf(so, n, KEY_SENDUP_REGISTERED);
5912 * free secreg entry registered.
5913 * XXX: I want to do free a socket marked done SADB_RESIGER to socket.
5916 key_freereg(struct socket *so)
5923 panic("key_freereg: NULL pointer is passed.");
5926 * check whether existing or not.
5927 * check all type of SA, because there is a potential that
5928 * one socket is registered to multiple type of SA.
5930 for (i = 0; i <= SADB_SATYPE_MAX; i++) {
5931 LIST_FOREACH(reg, ®tree[i], chain) {
5933 && __LIST_CHAINED(reg)) {
5934 LIST_REMOVE(reg, chain);
5945 * SADB_EXPIRE processing
5947 * <base, SA, SA2, lifetime(C and one of HS), address(SD)>
5949 * NOTE: We send only soft lifetime extension.
5952 * others : error number
5955 key_expire(struct secasvar *sav)
5959 struct mbuf *result = NULL, *m;
5962 struct sadb_lifetime *lt;
5964 /* XXX: Why do we lock ? */
5969 panic("key_expire: NULL pointer is passed.");
5970 if (sav->sah == NULL)
5971 panic("key_expire: Why was SA index in SA NULL.");
5972 if ((satype = key_proto2satype(sav->sah->saidx.proto)) == 0)
5973 panic("key_expire: invalid proto is passed.");
5975 /* set msg header */
5976 m = key_setsadbmsg(SADB_EXPIRE, 0, satype, sav->seq, 0, sav->refcnt);
5983 /* create SA extension */
5984 m = key_setsadbsa(sav);
5991 /* create SA extension */
5992 m = key_setsadbxsa2(sav->sah->saidx.mode,
5993 sav->replay ? sav->replay->count : 0,
5994 sav->sah->saidx.reqid);
6001 /* create lifetime extension (current and soft) */
6002 len = PFKEY_ALIGN8(sizeof(*lt)) * 2;
6003 m = key_alloc_mbuf(len);
6004 if (!m || m->m_next) { /*XXX*/
6010 bzero(mtod(m, caddr_t), len);
6011 lt = mtod(m, struct sadb_lifetime *);
6012 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
6013 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
6014 lt->sadb_lifetime_allocations = sav->lft_c->sadb_lifetime_allocations;
6015 lt->sadb_lifetime_bytes = sav->lft_c->sadb_lifetime_bytes;
6016 lt->sadb_lifetime_addtime = sav->lft_c->sadb_lifetime_addtime;
6017 lt->sadb_lifetime_usetime = sav->lft_c->sadb_lifetime_usetime;
6018 lt = (struct sadb_lifetime *)(mtod(m, caddr_t) + len / 2);
6019 bcopy(sav->lft_s, lt, sizeof(*lt));
6022 /* set sadb_address for source */
6023 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
6024 &sav->sah->saidx.src.sa,
6025 FULLMASK, IPSEC_ULPROTO_ANY);
6032 /* set sadb_address for destination */
6033 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
6034 &sav->sah->saidx.dst.sa,
6035 FULLMASK, IPSEC_ULPROTO_ANY);
6042 if ((result->m_flags & M_PKTHDR) == 0) {
6047 if (result->m_len < sizeof(struct sadb_msg)) {
6048 result = m_pullup(result, sizeof(struct sadb_msg));
6049 if (result == NULL) {
6054 result->m_pkthdr.len = m_lengthm(result, NULL);
6055 mtod(result, struct sadb_msg *)->sadb_msg_len =
6056 PFKEY_UNIT64(result->m_pkthdr.len);
6059 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
6069 * SADB_FLUSH processing
6072 * from the ikmpd, and free all entries in secastree.
6076 * NOTE: to do is only marking SADB_SASTATE_DEAD.
6078 * m will always be freed.
6081 key_flush(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
6083 struct sadb_msg *newmsg;
6084 struct secashead *sah;
6089 if (so == NULL || mhp == NULL || mhp->msg == NULL)
6090 panic("key_flush: NULL pointer is passed.");
6092 /* map satype to proto */
6093 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
6094 ipseclog((LOG_DEBUG, "key_flush: invalid satype is passed.\n"));
6095 return key_senderror(so, m, EINVAL);
6098 /* no SATYPE specified, i.e. flushing all SA. */
6099 LIST_FOREACH(sah, &sahtree, chain) {
6100 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC &&
6101 proto != sah->saidx.proto)
6104 for (stateidx = 0; stateidx < NELEM(saorder_state_alive);
6106 struct secasvar *sav, *nextsav;
6107 u_int8_t state = saorder_state_any[stateidx];
6109 LIST_FOREACH_MUTABLE(sav, &sah->savtree[state], chain,
6111 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
6116 sah->state = SADB_SASTATE_DEAD;
6119 if (m->m_len < sizeof(struct sadb_msg) ||
6120 sizeof(struct sadb_msg) > m->m_len + M_TRAILINGSPACE(m)) {
6121 ipseclog((LOG_DEBUG, "key_flush: No more memory.\n"));
6122 return key_senderror(so, m, ENOBUFS);
6128 m->m_pkthdr.len = m->m_len = sizeof(struct sadb_msg);
6129 newmsg = mtod(m, struct sadb_msg *);
6130 newmsg->sadb_msg_errno = 0;
6131 newmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
6133 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
6137 * SADB_DUMP processing
6138 * dump all entries including status of DEAD in SAD.
6141 * from the ikmpd, and dump all secasvar leaves
6146 * m will always be freed.
6149 key_dump(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
6151 struct secashead *sah;
6152 struct secasvar *sav;
6161 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
6162 panic("key_dump: NULL pointer is passed.");
6164 /* map satype to proto */
6165 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
6166 ipseclog((LOG_DEBUG, "key_dump: invalid satype is passed.\n"));
6167 return key_senderror(so, m, EINVAL);
6170 /* count sav entries to be sent to the userland. */
6172 LIST_FOREACH(sah, &sahtree, chain) {
6173 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC &&
6174 proto != sah->saidx.proto)
6177 for (stateidx = 0; stateidx < NELEM(saorder_state_any);
6179 state = saorder_state_any[stateidx];
6180 LIST_FOREACH(sav, &sah->savtree[state], chain) {
6187 return key_senderror(so, m, ENOENT);
6189 /* send this to the userland, one at a time. */
6190 LIST_FOREACH(sah, &sahtree, chain) {
6191 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
6192 && proto != sah->saidx.proto)
6195 /* map proto to satype */
6196 if ((satype = key_proto2satype(sah->saidx.proto)) == 0) {
6197 ipseclog((LOG_DEBUG, "key_dump: there was invalid proto in SAD.\n"));
6198 return key_senderror(so, m, EINVAL);
6201 for (stateidx = 0; stateidx < NELEM(saorder_state_any);
6203 state = saorder_state_any[stateidx];
6204 LIST_FOREACH(sav, &sah->savtree[state], chain) {
6205 n = key_setdumpsa(sav, SADB_DUMP, satype,
6206 --cnt, mhp->msg->sadb_msg_pid);
6208 return key_senderror(so, m, ENOBUFS);
6210 key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
6220 * SADB_X_PROMISC processing
6222 * m will always be freed.
6225 key_promisc(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
6230 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
6231 panic("key_promisc: NULL pointer is passed.");
6233 olen = PFKEY_UNUNIT64(mhp->msg->sadb_msg_len);
6235 if (olen < sizeof(struct sadb_msg)) {
6237 return key_senderror(so, m, EINVAL);
6242 } else if (olen == sizeof(struct sadb_msg)) {
6243 /* enable/disable promisc mode */
6246 if ((kp = (struct keycb *)sotorawcb(so)) == NULL)
6247 return key_senderror(so, m, EINVAL);
6248 mhp->msg->sadb_msg_errno = 0;
6249 switch (mhp->msg->sadb_msg_satype) {
6252 kp->kp_promisc = mhp->msg->sadb_msg_satype;
6255 return key_senderror(so, m, EINVAL);
6258 /* send the original message back to everyone */
6259 mhp->msg->sadb_msg_errno = 0;
6260 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
6262 /* send packet as is */
6264 m_adj(m, PFKEY_ALIGN8(sizeof(struct sadb_msg)));
6266 /* TODO: if sadb_msg_seq is specified, send to specific pid */
6267 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
6271 static int (*key_typesw[]) (struct socket *, struct mbuf *,
6272 const struct sadb_msghdr *) = {
6273 NULL, /* SADB_RESERVED */
6274 key_getspi, /* SADB_GETSPI */
6275 key_update, /* SADB_UPDATE */
6276 key_add, /* SADB_ADD */
6277 key_delete, /* SADB_DELETE */
6278 key_get, /* SADB_GET */
6279 key_acquire2, /* SADB_ACQUIRE */
6280 key_register, /* SADB_REGISTER */
6281 NULL, /* SADB_EXPIRE */
6282 key_flush, /* SADB_FLUSH */
6283 key_dump, /* SADB_DUMP */
6284 key_promisc, /* SADB_X_PROMISC */
6285 NULL, /* SADB_X_PCHANGE */
6286 key_spdadd, /* SADB_X_SPDUPDATE */
6287 key_spdadd, /* SADB_X_SPDADD */
6288 key_spddelete, /* SADB_X_SPDDELETE */
6289 key_spdget, /* SADB_X_SPDGET */
6290 NULL, /* SADB_X_SPDACQUIRE */
6291 key_spddump, /* SADB_X_SPDDUMP */
6292 key_spdflush, /* SADB_X_SPDFLUSH */
6293 key_spdadd, /* SADB_X_SPDSETIDX */
6294 NULL, /* SADB_X_SPDEXPIRE */
6295 key_spddelete2, /* SADB_X_SPDDELETE2 */
6299 * parse sadb_msg buffer to process PFKEYv2,
6300 * and create a data to response if needed.
6301 * I think to be dealed with mbuf directly.
6303 * msgp : pointer to pointer to a received buffer pulluped.
6304 * This is rewrited to response.
6305 * so : pointer to socket.
6307 * length for buffer to send to user process.
6310 key_parse(struct mbuf *m, struct socket *so)
6312 struct sadb_msg *msg;
6313 struct sadb_msghdr mh;
6319 if (m == NULL || so == NULL)
6320 panic("key_parse: NULL pointer is passed.");
6322 #if 0 /*kdebug_sadb assumes msg in linear buffer*/
6323 KEYDEBUG(KEYDEBUG_KEY_DUMP,
6324 ipseclog((LOG_DEBUG, "key_parse: passed sadb_msg\n"));
6328 if (m->m_len < sizeof(struct sadb_msg)) {
6329 m = m_pullup(m, sizeof(struct sadb_msg));
6333 msg = mtod(m, struct sadb_msg *);
6334 orglen = PFKEY_UNUNIT64(msg->sadb_msg_len);
6335 target = KEY_SENDUP_ONE;
6337 if ((m->m_flags & M_PKTHDR) == 0 ||
6338 m->m_pkthdr.len != m->m_pkthdr.len) {
6339 ipseclog((LOG_DEBUG, "key_parse: invalid message length.\n"));
6340 pfkeystat.out_invlen++;
6345 if (msg->sadb_msg_version != PF_KEY_V2) {
6346 ipseclog((LOG_DEBUG,
6347 "key_parse: PF_KEY version %u is mismatched.\n",
6348 msg->sadb_msg_version));
6349 pfkeystat.out_invver++;
6354 if (msg->sadb_msg_type > SADB_MAX) {
6355 ipseclog((LOG_DEBUG, "key_parse: invalid type %u is passed.\n",
6356 msg->sadb_msg_type));
6357 pfkeystat.out_invmsgtype++;
6362 /* for old-fashioned code - should be nuked */
6363 if (m->m_pkthdr.len > MCLBYTES) {
6370 n = m_getb(m->m_pkthdr.len, MB_DONTWAIT, MT_DATA, M_PKTHDR);
6375 m_copydata(m, 0, m->m_pkthdr.len, mtod(n, caddr_t));
6376 n->m_pkthdr.len = n->m_len = m->m_pkthdr.len;
6381 /* align the mbuf chain so that extensions are in contiguous region. */
6382 error = key_align(m, &mh);
6389 switch (msg->sadb_msg_satype) {
6390 case SADB_SATYPE_UNSPEC:
6391 switch (msg->sadb_msg_type) {
6399 ipseclog((LOG_DEBUG, "key_parse: must specify satype "
6400 "when msg type=%u.\n", msg->sadb_msg_type));
6401 pfkeystat.out_invsatype++;
6406 case SADB_SATYPE_AH:
6407 case SADB_SATYPE_ESP:
6408 case SADB_X_SATYPE_IPCOMP:
6409 switch (msg->sadb_msg_type) {
6411 case SADB_X_SPDDELETE:
6413 case SADB_X_SPDDUMP:
6414 case SADB_X_SPDFLUSH:
6415 case SADB_X_SPDSETIDX:
6416 case SADB_X_SPDUPDATE:
6417 case SADB_X_SPDDELETE2:
6418 ipseclog((LOG_DEBUG, "key_parse: illegal satype=%u\n",
6419 msg->sadb_msg_type));
6420 pfkeystat.out_invsatype++;
6425 case SADB_SATYPE_RSVP:
6426 case SADB_SATYPE_OSPFV2:
6427 case SADB_SATYPE_RIPV2:
6428 case SADB_SATYPE_MIP:
6429 ipseclog((LOG_DEBUG, "key_parse: type %u isn't supported.\n",
6430 msg->sadb_msg_satype));
6431 pfkeystat.out_invsatype++;
6434 case 1: /* XXX: What does it do? */
6435 if (msg->sadb_msg_type == SADB_X_PROMISC)
6439 ipseclog((LOG_DEBUG, "key_parse: invalid type %u is passed.\n",
6440 msg->sadb_msg_satype));
6441 pfkeystat.out_invsatype++;
6446 /* check field of upper layer protocol and address family */
6447 if (mh.ext[SADB_EXT_ADDRESS_SRC] != NULL
6448 && mh.ext[SADB_EXT_ADDRESS_DST] != NULL) {
6449 struct sadb_address *src0, *dst0;
6452 src0 = (struct sadb_address *)(mh.ext[SADB_EXT_ADDRESS_SRC]);
6453 dst0 = (struct sadb_address *)(mh.ext[SADB_EXT_ADDRESS_DST]);
6455 /* check upper layer protocol */
6456 if (src0->sadb_address_proto != dst0->sadb_address_proto) {
6457 ipseclog((LOG_DEBUG, "key_parse: upper layer protocol mismatched.\n"));
6458 pfkeystat.out_invaddr++;
6464 if (PFKEY_ADDR_SADDR(src0)->sa_family !=
6465 PFKEY_ADDR_SADDR(dst0)->sa_family) {
6466 ipseclog((LOG_DEBUG, "key_parse: address family mismatched.\n"));
6467 pfkeystat.out_invaddr++;
6471 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
6472 PFKEY_ADDR_SADDR(dst0)->sa_len) {
6473 ipseclog((LOG_DEBUG,
6474 "key_parse: address struct size mismatched.\n"));
6475 pfkeystat.out_invaddr++;
6480 switch (PFKEY_ADDR_SADDR(src0)->sa_family) {
6482 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
6483 sizeof(struct sockaddr_in)) {
6484 pfkeystat.out_invaddr++;
6490 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
6491 sizeof(struct sockaddr_in6)) {
6492 pfkeystat.out_invaddr++;
6498 ipseclog((LOG_DEBUG,
6499 "key_parse: unsupported address family.\n"));
6500 pfkeystat.out_invaddr++;
6501 error = EAFNOSUPPORT;
6505 switch (PFKEY_ADDR_SADDR(src0)->sa_family) {
6507 plen = sizeof(struct in_addr) << 3;
6510 plen = sizeof(struct in6_addr) << 3;
6513 plen = 0; /*fool gcc*/
6517 /* check max prefix length */
6518 if (src0->sadb_address_prefixlen > plen ||
6519 dst0->sadb_address_prefixlen > plen) {
6520 ipseclog((LOG_DEBUG,
6521 "key_parse: illegal prefixlen.\n"));
6522 pfkeystat.out_invaddr++;
6528 * prefixlen == 0 is valid because there can be a case when
6529 * all addresses are matched.
6533 if (msg->sadb_msg_type >= NELEM(key_typesw) ||
6534 key_typesw[msg->sadb_msg_type] == NULL) {
6535 pfkeystat.out_invmsgtype++;
6540 return (*key_typesw[msg->sadb_msg_type])(so, m, &mh);
6543 msg->sadb_msg_errno = error;
6544 return key_sendup_mbuf(so, m, target);
6548 key_senderror(struct socket *so, struct mbuf *m, int code)
6550 struct sadb_msg *msg;
6552 if (m->m_len < sizeof(struct sadb_msg))
6553 panic("invalid mbuf passed to key_senderror");
6555 msg = mtod(m, struct sadb_msg *);
6556 msg->sadb_msg_errno = code;
6557 return key_sendup_mbuf(so, m, KEY_SENDUP_ONE);
6561 * set the pointer to each header into message buffer.
6562 * m will be freed on error.
6563 * XXX larger-than-MCLBYTES extension?
6566 key_align(struct mbuf *m, struct sadb_msghdr *mhp)
6569 struct sadb_ext *ext;
6575 if (m == NULL || mhp == NULL)
6576 panic("key_align: NULL pointer is passed.");
6577 if (m->m_len < sizeof(struct sadb_msg))
6578 panic("invalid mbuf passed to key_align");
6581 bzero(mhp, sizeof(*mhp));
6583 mhp->msg = mtod(m, struct sadb_msg *);
6584 mhp->ext[0] = (struct sadb_ext *)mhp->msg; /*XXX backward compat */
6586 end = PFKEY_UNUNIT64(mhp->msg->sadb_msg_len);
6587 extlen = end; /*just in case extlen is not updated*/
6588 for (off = sizeof(struct sadb_msg); off < end; off += extlen) {
6589 n = m_pulldown(m, off, sizeof(struct sadb_ext), &toff);
6591 /* m is already freed */
6594 ext = (struct sadb_ext *)(mtod(n, caddr_t) + toff);
6597 switch (ext->sadb_ext_type) {
6599 case SADB_EXT_ADDRESS_SRC:
6600 case SADB_EXT_ADDRESS_DST:
6601 case SADB_EXT_ADDRESS_PROXY:
6602 case SADB_EXT_LIFETIME_CURRENT:
6603 case SADB_EXT_LIFETIME_HARD:
6604 case SADB_EXT_LIFETIME_SOFT:
6605 case SADB_EXT_KEY_AUTH:
6606 case SADB_EXT_KEY_ENCRYPT:
6607 case SADB_EXT_IDENTITY_SRC:
6608 case SADB_EXT_IDENTITY_DST:
6609 case SADB_EXT_SENSITIVITY:
6610 case SADB_EXT_PROPOSAL:
6611 case SADB_EXT_SUPPORTED_AUTH:
6612 case SADB_EXT_SUPPORTED_ENCRYPT:
6613 case SADB_EXT_SPIRANGE:
6614 case SADB_X_EXT_POLICY:
6615 case SADB_X_EXT_SA2:
6616 /* duplicate check */
6618 * XXX Are there duplication payloads of either
6619 * KEY_AUTH or KEY_ENCRYPT ?
6621 if (mhp->ext[ext->sadb_ext_type] != NULL) {
6622 ipseclog((LOG_DEBUG,
6623 "key_align: duplicate ext_type %u "
6624 "is passed.\n", ext->sadb_ext_type));
6626 pfkeystat.out_dupext++;
6631 ipseclog((LOG_DEBUG,
6632 "key_align: invalid ext_type %u is passed.\n",
6633 ext->sadb_ext_type));
6635 pfkeystat.out_invexttype++;
6639 extlen = PFKEY_UNUNIT64(ext->sadb_ext_len);
6641 if (key_validate_ext(ext, extlen)) {
6643 pfkeystat.out_invlen++;
6647 n = m_pulldown(m, off, extlen, &toff);
6649 /* m is already freed */
6652 ext = (struct sadb_ext *)(mtod(n, caddr_t) + toff);
6654 mhp->ext[ext->sadb_ext_type] = ext;
6655 mhp->extoff[ext->sadb_ext_type] = off;
6656 mhp->extlen[ext->sadb_ext_type] = extlen;
6661 pfkeystat.out_invlen++;
6669 key_validate_ext(const struct sadb_ext *ext, int len)
6671 const struct sockaddr *sa;
6672 enum { NONE, ADDR } checktype = NONE;
6674 const int sal = offsetof(struct sockaddr, sa_len) + sizeof(sa->sa_len);
6676 if (len != PFKEY_UNUNIT64(ext->sadb_ext_len))
6679 /* if it does not match minimum/maximum length, bail */
6680 if (ext->sadb_ext_type >= NELEM(minsize) ||
6681 ext->sadb_ext_type >= NELEM(maxsize))
6683 if (!minsize[ext->sadb_ext_type] || len < minsize[ext->sadb_ext_type])
6685 if (maxsize[ext->sadb_ext_type] && len > maxsize[ext->sadb_ext_type])
6688 /* more checks based on sadb_ext_type XXX need more */
6689 switch (ext->sadb_ext_type) {
6690 case SADB_EXT_ADDRESS_SRC:
6691 case SADB_EXT_ADDRESS_DST:
6692 case SADB_EXT_ADDRESS_PROXY:
6693 baselen = PFKEY_ALIGN8(sizeof(struct sadb_address));
6696 case SADB_EXT_IDENTITY_SRC:
6697 case SADB_EXT_IDENTITY_DST:
6698 if (((const struct sadb_ident *)ext)->sadb_ident_type ==
6699 SADB_X_IDENTTYPE_ADDR) {
6700 baselen = PFKEY_ALIGN8(sizeof(struct sadb_ident));
6710 switch (checktype) {
6714 sa = (const struct sockaddr *)(((const u_int8_t*)ext)+baselen);
6715 if (len < baselen + sal)
6717 if (baselen + PFKEY_ALIGN8(sa->sa_len) != len)
6730 for (i = 0; i < IPSEC_DIR_MAX; i++) {
6731 LIST_INIT(&sptree[i]);
6734 LIST_INIT(&sahtree);
6736 for (i = 0; i <= SADB_SATYPE_MAX; i++) {
6737 LIST_INIT(®tree[i]);
6740 #ifndef IPSEC_NONBLOCK_ACQUIRE
6741 LIST_INIT(&acqtree);
6743 LIST_INIT(&spacqtree);
6745 /* system default */
6746 ip4_def_policy.policy = IPSEC_POLICY_NONE;
6747 ip4_def_policy.refcnt++; /*never reclaim this*/
6749 #ifndef IPSEC_DEBUG2
6750 callout_init(&key_timehandler_ch);
6751 callout_reset(&key_timehandler_ch, hz, key_timehandler, NULL);
6752 #endif /*IPSEC_DEBUG2*/
6754 /* initialize key statistics */
6755 keystat.getspi_count = 1;
6757 kprintf("IPsec: Initialized Security Association Processing.\n");
6763 * XXX: maybe This function is called after INBOUND IPsec processing.
6765 * Special check for tunnel-mode packets.
6766 * We must make some checks for consistency between inner and outer IP header.
6768 * xxx more checks to be provided
6771 key_checktunnelsanity(struct secasvar *sav, u_int family, caddr_t src,
6775 if (sav->sah == NULL)
6776 panic("sav->sah == NULL at key_checktunnelsanity");
6778 /* XXX: check inner IP header */
6784 #define hostnamelen strlen(hostname)
6787 * Get FQDN for the host.
6788 * If the administrator configured hostname (by hostname(1)) without
6789 * domain name, returns nothing.
6796 static char fqdn[MAXHOSTNAMELEN + 1];
6801 /* check if it comes with domain name. */
6803 for (i = 0; i < hostnamelen; i++) {
6804 if (hostname[i] == '.')
6810 /* NOTE: hostname may not be NUL-terminated. */
6811 bzero(fqdn, sizeof(fqdn));
6812 bcopy(hostname, fqdn, hostnamelen);
6813 fqdn[hostnamelen] = '\0';
6818 * get username@FQDN for the host/user.
6821 key_getuserfqdn(void)
6824 static char userfqdn[MAXHOSTNAMELEN + MAXLOGNAME + 2];
6825 struct proc *p = curproc;
6828 if (!p || !p->p_pgrp || !p->p_pgrp->pg_session)
6830 if (!(host = key_getfqdn()))
6833 /* NOTE: s_login may not be-NUL terminated. */
6834 bzero(userfqdn, sizeof(userfqdn));
6835 bcopy(p->p_pgrp->pg_session->s_login, userfqdn, MAXLOGNAME);
6836 userfqdn[MAXLOGNAME] = '\0'; /* safeguard */
6837 q = userfqdn + strlen(userfqdn);
6839 bcopy(host, q, strlen(host));
6847 /* record data transfer on SA, and update timestamps */
6849 key_sa_recordxfer(struct secasvar *sav, struct mbuf *m)
6851 KASSERT(sav != NULL, ("key_sa_recordxfer: Null secasvar"));
6852 KASSERT(m != NULL, ("key_sa_recordxfer: Null mbuf"));
6857 * XXX Currently, there is a difference of bytes size
6858 * between inbound and outbound processing.
6860 sav->lft_c->sadb_lifetime_bytes += m->m_pkthdr.len;
6861 /* to check bytes lifetime is done in key_timehandler(). */
6864 * We use the number of packets as the unit of
6865 * sadb_lifetime_allocations. We increment the variable
6866 * whenever {esp,ah}_{in,out}put is called.
6868 sav->lft_c->sadb_lifetime_allocations++;
6869 /* XXX check for expires? */
6872 * NOTE: We record CURRENT sadb_lifetime_usetime by using wall clock,
6873 * in seconds. HARD and SOFT lifetime are measured by the time
6874 * difference (again in seconds) from sadb_lifetime_usetime.
6878 * -----+-----+--------+---> t
6879 * <--------------> HARD
6882 sav->lft_c->sadb_lifetime_usetime = time_second;
6883 /* XXX check for expires? */
6890 key_sa_routechange(struct sockaddr *dst)
6892 struct secashead *sah;
6895 LIST_FOREACH(sah, &sahtree, chain) {
6896 ro = &sah->sa_route;
6897 if (ro->ro_rt && dst->sa_len == ro->ro_dst.sa_len
6898 && bcmp(dst, &ro->ro_dst, dst->sa_len) == 0) {
6908 key_sa_chgstate(struct secasvar *sav, u_int8_t state)
6911 panic("key_sa_chgstate called with sav == NULL");
6913 if (sav->state == state)
6916 if (__LIST_CHAINED(sav))
6917 LIST_REMOVE(sav, chain);
6920 LIST_INSERT_HEAD(&sav->sah->savtree[state], sav, chain);
6924 key_sa_stir_iv(struct secasvar *sav)
6928 panic("key_sa_stir_iv called with sav == NULL");
6929 key_randomfill(sav->iv, sav->ivlen);
6933 static struct mbuf *
6934 key_alloc_mbuf(int l)
6936 struct mbuf *m = NULL, *n;
6941 n = m_getb(len, MB_DONTWAIT, MT_DATA, 0);
6947 n->m_len = M_TRAILINGSPACE(n);
6948 /* use the bottom of mbuf, hoping we can prepend afterwards */
6949 if (n->m_len > len) {
6950 t = (n->m_len - len) & ~(sizeof(long) - 1);