1 /* $FreeBSD: src/sys/netipsec/key.c,v 1.3.2.1 2003/01/24 05:11:35 sam Exp $ */
2 /* $DragonFly: src/sys/netproto/ipsec/key.c,v 1.11 2005/02/08 22:56:19 hsu Exp $ */
3 /* $KAME: key.c,v 1.191 2001/06/27 10:46:49 sakane Exp $ */
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * This code is referd to RFC 2367
39 #include "opt_inet6.h"
40 #include "opt_ipsec.h"
42 #include <sys/types.h>
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
47 #include <sys/domain.h>
48 #include <sys/protosw.h>
49 #include <sys/malloc.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/sysctl.h>
53 #include <sys/errno.h>
55 #include <sys/queue.h>
56 #include <sys/syslog.h>
59 #include <net/route.h>
60 #include <net/raw_cb.h>
62 #include <netinet/in.h>
63 #include <netinet/in_systm.h>
64 #include <netinet/ip.h>
65 #include <netinet/in_var.h>
68 #include <netinet/ip6.h>
69 #include <netinet6/in6_var.h>
70 #include <netinet6/ip6_var.h>
74 #include <netinet/in_pcb.h>
77 #include <netinet6/in6_pcb.h>
80 #include <net/pfkeyv2.h>
81 #include <netproto/ipsec/keydb.h>
82 #include <netproto/ipsec/key.h>
83 #include <netproto/ipsec/keysock.h>
84 #include <netproto/ipsec/key_debug.h>
86 #include <netproto/ipsec/ipsec.h>
88 #include <netproto/ipsec/ipsec6.h>
91 #include <netproto/ipsec/xform.h>
93 #include <machine/stdarg.h>
96 #include <sys/random.h>
98 #include <net/net_osdep.h>
100 #define FULLMASK 0xff
101 #define _BITS(bytes) ((bytes) << 3)
104 * Note on SA reference counting:
105 * - SAs that are not in DEAD state will have (total external reference + 1)
106 * following value in reference count field. they cannot be freed and are
107 * referenced from SA header.
108 * - SAs that are in DEAD state will have (total external reference)
109 * in reference count field. they are ready to be freed. reference from
110 * SA header will be removed in key_delsav(), when the reference count
111 * field hits 0 (= no external reference other than from SA header.
115 static struct callout key_timehandler_ch;
117 u_int32_t key_debug_level = 0;
118 static u_int key_spi_trycnt = 1000;
119 static u_int32_t key_spi_minval = 0x100;
120 static u_int32_t key_spi_maxval = 0x0fffffff; /* XXX */
121 static u_int32_t policy_id = 0;
122 static u_int key_int_random = 60; /*interval to initialize randseed,1(m)*/
123 static u_int key_larval_lifetime = 30; /* interval to expire acquiring, 30(s)*/
124 static int key_blockacq_count = 10; /* counter for blocking SADB_ACQUIRE.*/
125 static int key_blockacq_lifetime = 20; /* lifetime for blocking SADB_ACQUIRE.*/
126 static int key_prefered_oldsa = 1; /* prefered old sa rather than new sa.*/
128 static u_int32_t acq_seq = 0;
129 static int key_tick_init_random = 0;
131 static LIST_HEAD(_sptree, secpolicy) sptree[IPSEC_DIR_MAX]; /* SPD */
132 static LIST_HEAD(_sahtree, secashead) sahtree; /* SAD */
133 static LIST_HEAD(_regtree, secreg) regtree[SADB_SATYPE_MAX + 1];
135 #ifndef IPSEC_NONBLOCK_ACQUIRE
136 static LIST_HEAD(_acqtree, secacq) acqtree; /* acquiring list */
138 static LIST_HEAD(_spacqtree, secspacq) spacqtree; /* SP acquiring list */
140 /* search order for SAs */
141 static u_int saorder_state_valid[] = {
142 SADB_SASTATE_DYING, SADB_SASTATE_MATURE,
144 * This order is important because we must select the oldest SA
145 * for outbound processing. For inbound, This is not important.
148 static u_int saorder_state_alive[] = {
150 SADB_SASTATE_MATURE, SADB_SASTATE_DYING, SADB_SASTATE_LARVAL
152 static u_int saorder_state_any[] = {
153 SADB_SASTATE_MATURE, SADB_SASTATE_DYING,
154 SADB_SASTATE_LARVAL, SADB_SASTATE_DEAD
157 static const int minsize[] = {
158 sizeof(struct sadb_msg), /* SADB_EXT_RESERVED */
159 sizeof(struct sadb_sa), /* SADB_EXT_SA */
160 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_CURRENT */
161 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_HARD */
162 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_SOFT */
163 sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_SRC */
164 sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_DST */
165 sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_PROXY */
166 sizeof(struct sadb_key), /* SADB_EXT_KEY_AUTH */
167 sizeof(struct sadb_key), /* SADB_EXT_KEY_ENCRYPT */
168 sizeof(struct sadb_ident), /* SADB_EXT_IDENTITY_SRC */
169 sizeof(struct sadb_ident), /* SADB_EXT_IDENTITY_DST */
170 sizeof(struct sadb_sens), /* SADB_EXT_SENSITIVITY */
171 sizeof(struct sadb_prop), /* SADB_EXT_PROPOSAL */
172 sizeof(struct sadb_supported), /* SADB_EXT_SUPPORTED_AUTH */
173 sizeof(struct sadb_supported), /* SADB_EXT_SUPPORTED_ENCRYPT */
174 sizeof(struct sadb_spirange), /* SADB_EXT_SPIRANGE */
175 0, /* SADB_X_EXT_KMPRIVATE */
176 sizeof(struct sadb_x_policy), /* SADB_X_EXT_POLICY */
177 sizeof(struct sadb_x_sa2), /* SADB_X_SA2 */
179 static const int maxsize[] = {
180 sizeof(struct sadb_msg), /* SADB_EXT_RESERVED */
181 sizeof(struct sadb_sa), /* SADB_EXT_SA */
182 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_CURRENT */
183 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_HARD */
184 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_SOFT */
185 0, /* SADB_EXT_ADDRESS_SRC */
186 0, /* SADB_EXT_ADDRESS_DST */
187 0, /* SADB_EXT_ADDRESS_PROXY */
188 0, /* SADB_EXT_KEY_AUTH */
189 0, /* SADB_EXT_KEY_ENCRYPT */
190 0, /* SADB_EXT_IDENTITY_SRC */
191 0, /* SADB_EXT_IDENTITY_DST */
192 0, /* SADB_EXT_SENSITIVITY */
193 0, /* SADB_EXT_PROPOSAL */
194 0, /* SADB_EXT_SUPPORTED_AUTH */
195 0, /* SADB_EXT_SUPPORTED_ENCRYPT */
196 sizeof(struct sadb_spirange), /* SADB_EXT_SPIRANGE */
197 0, /* SADB_X_EXT_KMPRIVATE */
198 0, /* SADB_X_EXT_POLICY */
199 sizeof(struct sadb_x_sa2), /* SADB_X_SA2 */
202 static int ipsec_esp_keymin = 256;
203 static int ipsec_esp_auth = 0;
204 static int ipsec_ah_keymin = 128;
207 SYSCTL_DECL(_net_key);
210 SYSCTL_INT(_net_key, KEYCTL_DEBUG_LEVEL, debug, CTLFLAG_RW, \
211 &key_debug_level, 0, "");
213 /* max count of trial for the decision of spi value */
214 SYSCTL_INT(_net_key, KEYCTL_SPI_TRY, spi_trycnt, CTLFLAG_RW, \
215 &key_spi_trycnt, 0, "");
217 /* minimum spi value to allocate automatically. */
218 SYSCTL_INT(_net_key, KEYCTL_SPI_MIN_VALUE, spi_minval, CTLFLAG_RW, \
219 &key_spi_minval, 0, "");
221 /* maximun spi value to allocate automatically. */
222 SYSCTL_INT(_net_key, KEYCTL_SPI_MAX_VALUE, spi_maxval, CTLFLAG_RW, \
223 &key_spi_maxval, 0, "");
225 /* interval to initialize randseed */
226 SYSCTL_INT(_net_key, KEYCTL_RANDOM_INT, int_random, CTLFLAG_RW, \
227 &key_int_random, 0, "");
229 /* lifetime for larval SA */
230 SYSCTL_INT(_net_key, KEYCTL_LARVAL_LIFETIME, larval_lifetime, CTLFLAG_RW, \
231 &key_larval_lifetime, 0, "");
233 /* counter for blocking to send SADB_ACQUIRE to IKEd */
234 SYSCTL_INT(_net_key, KEYCTL_BLOCKACQ_COUNT, blockacq_count, CTLFLAG_RW, \
235 &key_blockacq_count, 0, "");
237 /* lifetime for blocking to send SADB_ACQUIRE to IKEd */
238 SYSCTL_INT(_net_key, KEYCTL_BLOCKACQ_LIFETIME, blockacq_lifetime, CTLFLAG_RW, \
239 &key_blockacq_lifetime, 0, "");
242 SYSCTL_INT(_net_key, KEYCTL_ESP_AUTH, esp_auth, CTLFLAG_RW, \
243 &ipsec_esp_auth, 0, "");
245 /* minimum ESP key length */
246 SYSCTL_INT(_net_key, KEYCTL_ESP_KEYMIN, esp_keymin, CTLFLAG_RW, \
247 &ipsec_esp_keymin, 0, "");
249 /* minimum AH key length */
250 SYSCTL_INT(_net_key, KEYCTL_AH_KEYMIN, ah_keymin, CTLFLAG_RW, \
251 &ipsec_ah_keymin, 0, "");
253 /* perfered old SA rather than new SA */
254 SYSCTL_INT(_net_key, KEYCTL_PREFERED_OLDSA, prefered_oldsa, CTLFLAG_RW,\
255 &key_prefered_oldsa, 0, "");
258 #define LIST_FOREACH(elm, head, field) \
259 for (elm = LIST_FIRST(head); elm; elm = LIST_NEXT(elm, field))
261 #define __LIST_CHAINED(elm) \
262 (!((elm)->chain.le_next == NULL && (elm)->chain.le_prev == NULL))
263 #define LIST_INSERT_TAIL(head, elm, type, field) \
265 struct type *curelm = LIST_FIRST(head); \
266 if (curelm == NULL) {\
267 LIST_INSERT_HEAD(head, elm, field); \
269 while (LIST_NEXT(curelm, field)) \
270 curelm = LIST_NEXT(curelm, field);\
271 LIST_INSERT_AFTER(curelm, elm, field);\
275 #define KEY_CHKSASTATE(head, sav, name) \
277 if ((head) != (sav)) { \
278 ipseclog((LOG_DEBUG, "%s: state mismatched (TREE=%d SA=%d)\n", \
279 (name), (head), (sav))); \
284 #define KEY_CHKSPDIR(head, sp, name) \
286 if ((head) != (sp)) { \
287 ipseclog((LOG_DEBUG, "%s: direction mismatched (TREE=%d SP=%d), " \
288 "anyway continue.\n", \
289 (name), (head), (sp))); \
293 MALLOC_DEFINE(M_SECA, "key mgmt", "security associations, key management");
296 #define KMALLOC(p, t, n) \
297 ((p) = (t) malloc((unsigned long)(n), M_SECA, M_INTWAIT | M_NULLOK))
299 free((caddr_t)(p), M_SECA)
301 #define KMALLOC(p, t, n) \
303 ((p) = (t)malloc((unsigned long)(n), M_SECA, M_INTWAIT | M_NULLOK)); \
304 printf("%s %d: %p <- KMALLOC(%s, %d)\n", \
305 __FILE__, __LINE__, (p), #t, n); \
310 printf("%s %d: %p -> KFREE()\n", __FILE__, __LINE__, (p)); \
311 free((caddr_t)(p), M_SECA); \
316 * set parameters into secpolicyindex buffer.
317 * Must allocate secpolicyindex buffer passed to this function.
319 #define KEY_SETSECSPIDX(_dir, s, d, ps, pd, ulp, idx) \
321 bzero((idx), sizeof(struct secpolicyindex)); \
322 (idx)->dir = (_dir); \
323 (idx)->prefs = (ps); \
324 (idx)->prefd = (pd); \
325 (idx)->ul_proto = (ulp); \
326 bcopy((s), &(idx)->src, ((const struct sockaddr *)(s))->sa_len); \
327 bcopy((d), &(idx)->dst, ((const struct sockaddr *)(d))->sa_len); \
331 * set parameters into secasindex buffer.
332 * Must allocate secasindex buffer before calling this function.
334 #define KEY_SETSECASIDX(p, m, r, s, d, idx) \
336 bzero((idx), sizeof(struct secasindex)); \
337 (idx)->proto = (p); \
339 (idx)->reqid = (r); \
340 bcopy((s), &(idx)->src, ((const struct sockaddr *)(s))->sa_len); \
341 bcopy((d), &(idx)->dst, ((const struct sockaddr *)(d))->sa_len); \
346 u_long getspi_count; /* the avarage of count to try to get new SPI */
350 struct sadb_msg *msg;
351 struct sadb_ext *ext[SADB_EXT_MAX + 1];
352 int extoff[SADB_EXT_MAX + 1];
353 int extlen[SADB_EXT_MAX + 1];
356 static struct secasvar *key_allocsa_policy (const struct secasindex *);
357 static void key_freesp_so (struct secpolicy **);
358 static struct secasvar *key_do_allocsa_policy (struct secashead *, u_int);
359 static void key_delsp (struct secpolicy *);
360 static struct secpolicy *key_getsp (struct secpolicyindex *);
361 static struct secpolicy *key_getspbyid (u_int32_t);
362 static u_int32_t key_newreqid (void);
363 static struct mbuf *key_gather_mbuf (struct mbuf *,
364 const struct sadb_msghdr *, int, int, ...);
365 static int key_spdadd (struct socket *, struct mbuf *,
366 const struct sadb_msghdr *);
367 static u_int32_t key_getnewspid (void);
368 static int key_spddelete (struct socket *, struct mbuf *,
369 const struct sadb_msghdr *);
370 static int key_spddelete2 (struct socket *, struct mbuf *,
371 const struct sadb_msghdr *);
372 static int key_spdget (struct socket *, struct mbuf *,
373 const struct sadb_msghdr *);
374 static int key_spdflush (struct socket *, struct mbuf *,
375 const struct sadb_msghdr *);
376 static int key_spddump (struct socket *, struct mbuf *,
377 const struct sadb_msghdr *);
378 static struct mbuf *key_setdumpsp (struct secpolicy *,
379 u_int8_t, u_int32_t, u_int32_t);
380 static u_int key_getspreqmsglen (struct secpolicy *);
381 static int key_spdexpire (struct secpolicy *);
382 static struct secashead *key_newsah (struct secasindex *);
383 static void key_delsah (struct secashead *);
384 static struct secasvar *key_newsav (struct mbuf *,
385 const struct sadb_msghdr *, struct secashead *, int *,
387 #define KEY_NEWSAV(m, sadb, sah, e) \
388 key_newsav(m, sadb, sah, e, __FILE__, __LINE__)
389 static void key_delsav (struct secasvar *);
390 static struct secashead *key_getsah (struct secasindex *);
391 static struct secasvar *key_checkspidup (struct secasindex *, u_int32_t);
392 static struct secasvar *key_getsavbyspi (struct secashead *, u_int32_t);
393 static int key_setsaval (struct secasvar *, struct mbuf *,
394 const struct sadb_msghdr *);
395 static int key_mature (struct secasvar *);
396 static struct mbuf *key_setdumpsa (struct secasvar *, u_int8_t,
397 u_int8_t, u_int32_t, u_int32_t);
398 static struct mbuf *key_setsadbmsg (u_int8_t, u_int16_t, u_int8_t,
399 u_int32_t, pid_t, u_int16_t);
400 static struct mbuf *key_setsadbsa (struct secasvar *);
401 static struct mbuf *key_setsadbaddr (u_int16_t,
402 const struct sockaddr *, u_int8_t, u_int16_t);
404 static struct mbuf *key_setsadbident (u_int16_t, u_int16_t, caddr_t,
407 static struct mbuf *key_setsadbxsa2 (u_int8_t, u_int32_t, u_int32_t);
408 static struct mbuf *key_setsadbxpolicy (u_int16_t, u_int8_t,
410 static void *key_newbuf (const void *, u_int);
412 static int key_ismyaddr6 (struct sockaddr_in6 *);
415 /* flags for key_cmpsaidx() */
416 #define CMP_HEAD 1 /* protocol, addresses. */
417 #define CMP_MODE_REQID 2 /* additionally HEAD, reqid, mode. */
418 #define CMP_REQID 3 /* additionally HEAD, reaid. */
419 #define CMP_EXACTLY 4 /* all elements. */
420 static int key_cmpsaidx
421 (const struct secasindex *, const struct secasindex *, int);
423 static int key_cmpspidx_exactly
424 (struct secpolicyindex *, struct secpolicyindex *);
425 static int key_cmpspidx_withmask
426 (struct secpolicyindex *, struct secpolicyindex *);
427 static int key_sockaddrcmp (const struct sockaddr *, const struct sockaddr *, int);
428 static int key_bbcmp (const void *, const void *, u_int);
429 static void key_srandom (void);
430 static u_int16_t key_satype2proto (u_int8_t);
431 static u_int8_t key_proto2satype (u_int16_t);
433 static int key_getspi (struct socket *, struct mbuf *,
434 const struct sadb_msghdr *);
435 static u_int32_t key_do_getnewspi (struct sadb_spirange *,
436 struct secasindex *);
437 static int key_update (struct socket *, struct mbuf *,
438 const struct sadb_msghdr *);
439 #ifdef IPSEC_DOSEQCHECK
440 static struct secasvar *key_getsavbyseq (struct secashead *, u_int32_t);
442 static int key_add (struct socket *, struct mbuf *,
443 const struct sadb_msghdr *);
444 static int key_setident (struct secashead *, struct mbuf *,
445 const struct sadb_msghdr *);
446 static struct mbuf *key_getmsgbuf_x1 (struct mbuf *,
447 const struct sadb_msghdr *);
448 static int key_delete (struct socket *, struct mbuf *,
449 const struct sadb_msghdr *);
450 static int key_get (struct socket *, struct mbuf *,
451 const struct sadb_msghdr *);
453 static void key_getcomb_setlifetime (struct sadb_comb *);
454 static struct mbuf *key_getcomb_esp (void);
455 static struct mbuf *key_getcomb_ah (void);
456 static struct mbuf *key_getcomb_ipcomp (void);
457 static struct mbuf *key_getprop (const struct secasindex *);
459 static int key_acquire (const struct secasindex *, struct secpolicy *);
460 #ifndef IPSEC_NONBLOCK_ACQUIRE
461 static struct secacq *key_newacq (const struct secasindex *);
462 static struct secacq *key_getacq (const struct secasindex *);
463 static struct secacq *key_getacqbyseq (u_int32_t);
465 static struct secspacq *key_newspacq (struct secpolicyindex *);
466 static struct secspacq *key_getspacq (struct secpolicyindex *);
467 static int key_acquire2 (struct socket *, struct mbuf *,
468 const struct sadb_msghdr *);
469 static int key_register (struct socket *, struct mbuf *,
470 const struct sadb_msghdr *);
471 static int key_expire (struct secasvar *);
472 static int key_flush (struct socket *, struct mbuf *,
473 const struct sadb_msghdr *);
474 static int key_dump (struct socket *, struct mbuf *,
475 const struct sadb_msghdr *);
476 static int key_promisc (struct socket *, struct mbuf *,
477 const struct sadb_msghdr *);
478 static int key_senderror (struct socket *, struct mbuf *, int);
479 static int key_validate_ext (const struct sadb_ext *, int);
480 static int key_align (struct mbuf *, struct sadb_msghdr *);
482 static const char *key_getfqdn (void);
483 static const char *key_getuserfqdn (void);
485 static void key_sa_chgstate (struct secasvar *, u_int8_t);
486 static struct mbuf *key_alloc_mbuf (int);
488 #define SA_ADDREF(p) do { \
490 KASSERT((p)->refcnt != 0, \
491 ("SA refcnt overflow at %s:%u", __FILE__, __LINE__)); \
493 #define SA_DELREF(p) do { \
494 KASSERT((p)->refcnt > 0, \
495 ("SA refcnt underflow at %s:%u", __FILE__, __LINE__)); \
499 #define SP_ADDREF(p) do { \
501 KASSERT((p)->refcnt != 0, \
502 ("SP refcnt overflow at %s:%u", __FILE__, __LINE__)); \
504 #define SP_DELREF(p) do { \
505 KASSERT((p)->refcnt > 0, \
506 ("SP refcnt underflow at %s:%u", __FILE__, __LINE__)); \
511 * Return 0 when there are known to be no SP's for the specified
512 * direction. Otherwise return 1. This is used by IPsec code
513 * to optimize performance.
516 key_havesp(u_int dir)
518 return (dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND ?
519 LIST_FIRST(&sptree[dir]) != NULL : 1);
522 /* %%% IPsec policy management */
524 * allocating a SP for OUTBOUND or INBOUND packet.
525 * Must call key_freesp() later.
526 * OUT: NULL: not found
527 * others: found and return the pointer.
530 key_allocsp(struct secpolicyindex *spidx, u_int dir, const char* where, int tag)
532 struct secpolicy *sp;
535 KASSERT(spidx != NULL, ("key_allocsp: null spidx"));
536 KASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
537 ("key_allocsp: invalid direction %u", dir));
539 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
540 printf("DP key_allocsp from %s:%u\n", where, tag));
543 s = splnet(); /*called from softclock()*/
544 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
545 printf("*** objects\n");
546 kdebug_secpolicyindex(spidx));
548 LIST_FOREACH(sp, &sptree[dir], chain) {
549 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
550 printf("*** in SPD\n");
551 kdebug_secpolicyindex(&sp->spidx));
553 if (sp->state == IPSEC_SPSTATE_DEAD)
555 if (key_cmpspidx_withmask(&sp->spidx, spidx))
562 KEY_CHKSPDIR(sp->spidx.dir, dir, "key_allocsp");
564 /* found a SPD entry */
565 sp->lastused = time_second;
570 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
571 printf("DP key_allocsp return SP:%p (ID=%u) refcnt %u\n",
572 sp, sp ? sp->id : 0, sp ? sp->refcnt : 0));
577 * allocating a SP for OUTBOUND or INBOUND packet.
578 * Must call key_freesp() later.
579 * OUT: NULL: not found
580 * others: found and return the pointer.
583 key_allocsp2(u_int32_t spi,
584 union sockaddr_union *dst,
587 const char* where, int tag)
589 struct secpolicy *sp;
592 KASSERT(dst != NULL, ("key_allocsp2: null dst"));
593 KASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
594 ("key_allocsp2: invalid direction %u", dir));
596 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
597 printf("DP key_allocsp2 from %s:%u\n", where, tag));
600 s = splnet(); /*called from softclock()*/
601 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
602 printf("*** objects\n");
603 printf("spi %u proto %u dir %u\n", spi, proto, dir);
604 kdebug_sockaddr(&dst->sa));
606 LIST_FOREACH(sp, &sptree[dir], chain) {
607 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
608 printf("*** in SPD\n");
609 kdebug_secpolicyindex(&sp->spidx));
611 if (sp->state == IPSEC_SPSTATE_DEAD)
613 /* compare simple values, then dst address */
614 if (sp->spidx.ul_proto != proto)
616 /* NB: spi's must exist and match */
617 if (!sp->req || !sp->req->sav || sp->req->sav->spi != spi)
619 if (key_sockaddrcmp(&sp->spidx.dst.sa, &dst->sa, 1) == 0)
626 KEY_CHKSPDIR(sp->spidx.dir, dir, "key_allocsp2");
628 /* found a SPD entry */
629 sp->lastused = time_second;
634 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
635 printf("DP key_allocsp2 return SP:%p (ID=%u) refcnt %u\n",
636 sp, sp ? sp->id : 0, sp ? sp->refcnt : 0));
641 * return a policy that matches this particular inbound packet.
645 key_gettunnel(const struct sockaddr *osrc,
646 const struct sockaddr *odst,
647 const struct sockaddr *isrc,
648 const struct sockaddr *idst,
649 const char* where, int tag)
651 struct secpolicy *sp;
652 const int dir = IPSEC_DIR_INBOUND;
654 struct ipsecrequest *r1, *r2, *p;
655 struct secpolicyindex spidx;
657 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
658 printf("DP key_gettunnel from %s:%u\n", where, tag));
660 if (isrc->sa_family != idst->sa_family) {
661 ipseclog((LOG_ERR, "protocol family mismatched %d != %d\n.",
662 isrc->sa_family, idst->sa_family));
667 s = splnet(); /*called from softclock()*/
668 LIST_FOREACH(sp, &sptree[dir], chain) {
669 if (sp->state == IPSEC_SPSTATE_DEAD)
673 for (p = sp->req; p; p = p->next) {
674 if (p->saidx.mode != IPSEC_MODE_TUNNEL)
681 /* here we look at address matches only */
683 if (isrc->sa_len > sizeof(spidx.src) ||
684 idst->sa_len > sizeof(spidx.dst))
686 bcopy(isrc, &spidx.src, isrc->sa_len);
687 bcopy(idst, &spidx.dst, idst->sa_len);
688 if (!key_cmpspidx_withmask(&sp->spidx, &spidx))
691 if (key_sockaddrcmp(&r1->saidx.src.sa, isrc, 0) ||
692 key_sockaddrcmp(&r1->saidx.dst.sa, idst, 0))
696 if (key_sockaddrcmp(&r2->saidx.src.sa, osrc, 0) ||
697 key_sockaddrcmp(&r2->saidx.dst.sa, odst, 0))
706 sp->lastused = time_second;
711 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
712 printf("DP key_gettunnel return SP:%p (ID=%u) refcnt %u\n",
713 sp, sp ? sp->id : 0, sp ? sp->refcnt : 0));
718 * allocating an SA entry for an *OUTBOUND* packet.
719 * checking each request entries in SP, and acquire an SA if need.
720 * OUT: 0: there are valid requests.
721 * ENOENT: policy may be valid, but SA with REQUIRE is on acquiring.
724 key_checkrequest(struct ipsecrequest *isr, const struct secasindex *saidx)
729 KASSERT(isr != NULL, ("key_checkrequest: null isr"));
730 KASSERT(saidx != NULL, ("key_checkrequest: null saidx"));
731 KASSERT(saidx->mode == IPSEC_MODE_TRANSPORT ||
732 saidx->mode == IPSEC_MODE_TUNNEL,
733 ("key_checkrequest: unexpected policy %u", saidx->mode));
735 /* get current level */
736 level = ipsec_get_reqlevel(isr);
739 * XXX guard against protocol callbacks from the crypto
740 * thread as they reference ipsecrequest.sav which we
741 * temporarily null out below. Need to rethink how we
742 * handle bundled SA's in the callback thread.
744 SPLASSERT(net, "key_checkrequest");
747 * We do allocate new SA only if the state of SA in the holder is
748 * SADB_SASTATE_DEAD. The SA for outbound must be the oldest.
750 if (isr->sav != NULL) {
751 if (isr->sav->sah == NULL)
752 panic("key_checkrequest: sah is null.\n");
753 if (isr->sav == (struct secasvar *)LIST_FIRST(
754 &isr->sav->sah->savtree[SADB_SASTATE_DEAD])) {
755 KEY_FREESAV(&isr->sav);
761 * we free any SA stashed in the IPsec request because a different
762 * SA may be involved each time this request is checked, either
763 * because new SAs are being configured, or this request is
764 * associated with an unconnected datagram socket, or this request
765 * is associated with a system default policy.
767 * The operation may have negative impact to performance. We may
768 * want to check cached SA carefully, rather than picking new SA
771 if (isr->sav != NULL) {
772 KEY_FREESAV(&isr->sav);
778 * new SA allocation if no SA found.
779 * key_allocsa_policy should allocate the oldest SA available.
780 * See key_do_allocsa_policy(), and draft-jenkins-ipsec-rekeying-03.txt.
782 if (isr->sav == NULL)
783 isr->sav = key_allocsa_policy(saidx);
785 /* When there is SA. */
786 if (isr->sav != NULL) {
787 if (isr->sav->state != SADB_SASTATE_MATURE &&
788 isr->sav->state != SADB_SASTATE_DYING)
794 error = key_acquire(saidx, isr->sp);
796 /* XXX What should I do ? */
797 ipseclog((LOG_DEBUG, "key_checkrequest: error %d returned "
798 "from key_acquire.\n", error));
802 if (level != IPSEC_LEVEL_REQUIRE) {
803 /* XXX sigh, the interface to this routine is botched */
804 KASSERT(isr->sav == NULL, ("key_checkrequest: unexpected SA"));
812 * allocating a SA for policy entry from SAD.
813 * NOTE: searching SAD of aliving state.
814 * OUT: NULL: not found.
815 * others: found and return the pointer.
817 static struct secasvar *
818 key_allocsa_policy(const struct secasindex *saidx)
820 struct secashead *sah;
821 struct secasvar *sav;
822 u_int stateidx, state;
824 LIST_FOREACH(sah, &sahtree, chain) {
825 if (sah->state == SADB_SASTATE_DEAD)
827 if (key_cmpsaidx(&sah->saidx, saidx, CMP_MODE_REQID))
835 /* search valid state */
837 stateidx < _ARRAYLEN(saorder_state_valid);
840 state = saorder_state_valid[stateidx];
842 sav = key_do_allocsa_policy(sah, state);
851 * searching SAD with direction, protocol, mode and state.
852 * called by key_allocsa_policy().
855 * others : found, pointer to a SA.
857 static struct secasvar *
858 key_do_allocsa_policy(struct secashead *sah, u_int state)
860 struct secasvar *sav, *nextsav, *candidate, *d;
865 for (sav = LIST_FIRST(&sah->savtree[state]);
869 nextsav = LIST_NEXT(sav, chain);
872 KEY_CHKSASTATE(sav->state, state, "key_do_allocsa_policy");
875 if (candidate == NULL) {
880 /* Which SA is the better ? */
883 if (candidate->lft_c == NULL || sav->lft_c == NULL)
884 panic("key_do_allocsa_policy: "
885 "lifetime_current is NULL.\n");
887 /* What the best method is to compare ? */
888 if (key_prefered_oldsa) {
889 if (candidate->lft_c->sadb_lifetime_addtime >
890 sav->lft_c->sadb_lifetime_addtime) {
897 /* prefered new sa rather than old sa */
898 if (candidate->lft_c->sadb_lifetime_addtime <
899 sav->lft_c->sadb_lifetime_addtime) {
906 * prepared to delete the SA when there is more
907 * suitable candidate and the lifetime of the SA is not
910 if (d->lft_c->sadb_lifetime_addtime != 0) {
911 struct mbuf *m, *result;
913 key_sa_chgstate(d, SADB_SASTATE_DEAD);
915 KASSERT(d->refcnt > 0,
916 ("key_do_allocsa_policy: bogus ref count"));
917 m = key_setsadbmsg(SADB_DELETE, 0,
918 d->sah->saidx.proto, 0, 0, d->refcnt - 1);
923 /* set sadb_address for saidx's. */
924 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
925 &d->sah->saidx.src.sa,
926 d->sah->saidx.src.sa.sa_len << 3,
932 /* set sadb_address for saidx's. */
933 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
934 &d->sah->saidx.src.sa,
935 d->sah->saidx.src.sa.sa_len << 3,
941 /* create SA extension */
942 m = key_setsadbsa(d);
947 if (result->m_len < sizeof(struct sadb_msg)) {
948 result = m_pullup(result,
949 sizeof(struct sadb_msg));
954 result->m_pkthdr.len = 0;
955 for (m = result; m; m = m->m_next)
956 result->m_pkthdr.len += m->m_len;
957 mtod(result, struct sadb_msg *)->sadb_msg_len =
958 PFKEY_UNIT64(result->m_pkthdr.len);
960 if (key_sendup_mbuf(NULL, result,
961 KEY_SENDUP_REGISTERED))
969 SA_ADDREF(candidate);
970 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
971 printf("DP allocsa_policy cause "
972 "refcnt++:%d SA:%p\n",
973 candidate->refcnt, candidate));
979 * allocating a usable SA entry for a *INBOUND* packet.
980 * Must call key_freesav() later.
981 * OUT: positive: pointer to a usable sav (i.e. MATURE or DYING state).
982 * NULL: not found, or error occured.
984 * In the comparison, no source address is used--for RFC2401 conformance.
985 * To quote, from section 4.1:
986 * A security association is uniquely identified by a triple consisting
987 * of a Security Parameter Index (SPI), an IP Destination Address, and a
988 * security protocol (AH or ESP) identifier.
989 * Note that, however, we do need to keep source address in IPsec SA.
990 * IKE specification and PF_KEY specification do assume that we
991 * keep source address in IPsec SA. We see a tricky situation here.
995 union sockaddr_union *dst,
998 const char* where, int tag)
1000 struct secashead *sah;
1001 struct secasvar *sav;
1002 u_int stateidx, state;
1005 KASSERT(dst != NULL, ("key_allocsa: null dst address"));
1007 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1008 printf("DP key_allocsa from %s:%u\n", where, tag));
1012 * XXX: to be checked internal IP header somewhere. Also when
1013 * IPsec tunnel packet is received. But ESP tunnel mode is
1014 * encrypted so we can't check internal IP header.
1016 s = splnet(); /*called from softclock()*/
1017 LIST_FOREACH(sah, &sahtree, chain) {
1018 /* search valid state */
1020 stateidx < _ARRAYLEN(saorder_state_valid);
1022 state = saorder_state_valid[stateidx];
1023 LIST_FOREACH(sav, &sah->savtree[state], chain) {
1025 KEY_CHKSASTATE(sav->state, state, "key_allocsav");
1026 /* do not return entries w/ unusable state */
1027 if (sav->state != SADB_SASTATE_MATURE &&
1028 sav->state != SADB_SASTATE_DYING)
1030 if (proto != sav->sah->saidx.proto)
1032 if (spi != sav->spi)
1034 #if 0 /* don't check src */
1035 /* check src address */
1036 if (key_sockaddrcmp(&src->sa, &sav->sah->saidx.src.sa, 0) != 0)
1039 /* check dst address */
1040 if (key_sockaddrcmp(&dst->sa, &sav->sah->saidx.dst.sa, 0) != 0)
1051 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1052 printf("DP key_allocsa return SA:%p; refcnt %u\n",
1053 sav, sav ? sav->refcnt : 0));
1058 * Must be called after calling key_allocsp().
1059 * For both the packet without socket and key_freeso().
1062 _key_freesp(struct secpolicy **spp, const char* where, int tag)
1064 struct secpolicy *sp = *spp;
1066 KASSERT(sp != NULL, ("key_freesp: null sp"));
1070 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1071 printf("DP key_freesp SP:%p (ID=%u) from %s:%u; refcnt now %u\n",
1072 sp, sp->id, where, tag, sp->refcnt));
1074 if (sp->refcnt == 0) {
1081 * Must be called after calling key_allocsp().
1082 * For the packet with socket.
1085 key_freeso(struct socket *so)
1088 KASSERT(so != NULL, ("key_freeso: null so"));
1090 switch (so->so_proto->pr_domain->dom_family) {
1094 struct inpcb *pcb = so->so_pcb;
1096 /* Does it have a PCB ? */
1099 key_freesp_so(&pcb->inp_sp->sp_in);
1100 key_freesp_so(&pcb->inp_sp->sp_out);
1107 #ifdef HAVE_NRL_INPCB
1108 struct inpcb *pcb = so->so_pcb;
1110 /* Does it have a PCB ? */
1113 key_freesp_so(&pcb->inp_sp->sp_in);
1114 key_freesp_so(&pcb->inp_sp->sp_out);
1116 struct in6pcb *pcb = so->so_pcb;
1118 /* Does it have a PCB ? */
1121 key_freesp_so(&pcb->in6p_sp->sp_in);
1122 key_freesp_so(&pcb->in6p_sp->sp_out);
1128 ipseclog((LOG_DEBUG, "key_freeso: unknown address family=%d.\n",
1129 so->so_proto->pr_domain->dom_family));
1135 key_freesp_so(struct secpolicy **sp)
1137 KASSERT(sp != NULL && *sp != NULL, ("key_freesp_so: null sp"));
1139 if ((*sp)->policy == IPSEC_POLICY_ENTRUST ||
1140 (*sp)->policy == IPSEC_POLICY_BYPASS)
1143 KASSERT((*sp)->policy == IPSEC_POLICY_IPSEC,
1144 ("key_freesp_so: invalid policy %u", (*sp)->policy));
1149 * Must be called after calling key_allocsa().
1150 * This function is called by key_freesp() to free some SA allocated
1154 key_freesav(struct secasvar **psav, const char* where, int tag)
1156 struct secasvar *sav = *psav;
1158 KASSERT(sav != NULL, ("key_freesav: null sav"));
1162 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1163 printf("DP key_freesav SA:%p (SPI %u) from %s:%u; refcnt now %u\n",
1164 sav, ntohl(sav->spi), where, tag, sav->refcnt));
1166 if (sav->refcnt == 0) {
1172 /* %%% SPD management */
1174 * free security policy entry.
1177 key_delsp(struct secpolicy *sp)
1181 KASSERT(sp != NULL, ("key_delsp: null sp"));
1183 sp->state = IPSEC_SPSTATE_DEAD;
1185 KASSERT(sp->refcnt == 0,
1186 ("key_delsp: SP with references deleted (refcnt %u)",
1189 s = splnet(); /*called from softclock()*/
1190 /* remove from SP index */
1191 if (__LIST_CHAINED(sp))
1192 LIST_REMOVE(sp, chain);
1195 struct ipsecrequest *isr = sp->req, *nextisr;
1197 while (isr != NULL) {
1198 if (isr->sav != NULL) {
1199 KEY_FREESAV(&isr->sav);
1203 nextisr = isr->next;
1216 * OUT: NULL : not found
1217 * others : found, pointer to a SP.
1219 static struct secpolicy *
1220 key_getsp(struct secpolicyindex *spidx)
1222 struct secpolicy *sp;
1224 KASSERT(spidx != NULL, ("key_getsp: null spidx"));
1226 LIST_FOREACH(sp, &sptree[spidx->dir], chain) {
1227 if (sp->state == IPSEC_SPSTATE_DEAD)
1229 if (key_cmpspidx_exactly(spidx, &sp->spidx)) {
1240 * OUT: NULL : not found
1241 * others : found, pointer to a SP.
1243 static struct secpolicy *
1244 key_getspbyid(u_int32_t id)
1246 struct secpolicy *sp;
1248 LIST_FOREACH(sp, &sptree[IPSEC_DIR_INBOUND], chain) {
1249 if (sp->state == IPSEC_SPSTATE_DEAD)
1257 LIST_FOREACH(sp, &sptree[IPSEC_DIR_OUTBOUND], chain) {
1258 if (sp->state == IPSEC_SPSTATE_DEAD)
1270 key_newsp(const char* where, int tag)
1272 struct secpolicy *newsp = NULL;
1274 newsp = malloc(sizeof(struct secpolicy), M_SECA,
1275 M_INTWAIT | M_ZERO | M_NULLOK);
1281 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1282 printf("DP key_newsp from %s:%u return SP:%p\n",
1283 where, tag, newsp));
1288 * create secpolicy structure from sadb_x_policy structure.
1289 * NOTE: `state', `secpolicyindex' in secpolicy structure are not set,
1290 * so must be set properly later.
1293 key_msg2sp(xpl0, len, error)
1294 struct sadb_x_policy *xpl0;
1298 struct secpolicy *newsp;
1302 panic("key_msg2sp: NULL pointer was passed.\n");
1303 if (len < sizeof(*xpl0))
1304 panic("key_msg2sp: invalid length.\n");
1305 if (len != PFKEY_EXTLEN(xpl0)) {
1306 ipseclog((LOG_DEBUG, "key_msg2sp: Invalid msg length.\n"));
1311 if ((newsp = KEY_NEWSP()) == NULL) {
1316 newsp->spidx.dir = xpl0->sadb_x_policy_dir;
1317 newsp->policy = xpl0->sadb_x_policy_type;
1320 switch (xpl0->sadb_x_policy_type) {
1321 case IPSEC_POLICY_DISCARD:
1322 case IPSEC_POLICY_NONE:
1323 case IPSEC_POLICY_ENTRUST:
1324 case IPSEC_POLICY_BYPASS:
1328 case IPSEC_POLICY_IPSEC:
1331 struct sadb_x_ipsecrequest *xisr;
1332 struct ipsecrequest **p_isr = &newsp->req;
1334 /* validity check */
1335 if (PFKEY_EXTLEN(xpl0) < sizeof(*xpl0)) {
1336 ipseclog((LOG_DEBUG,
1337 "key_msg2sp: Invalid msg length.\n"));
1343 tlen = PFKEY_EXTLEN(xpl0) - sizeof(*xpl0);
1344 xisr = (struct sadb_x_ipsecrequest *)(xpl0 + 1);
1348 if (xisr->sadb_x_ipsecrequest_len < sizeof(*xisr)) {
1349 ipseclog((LOG_DEBUG, "key_msg2sp: "
1350 "invalid ipsecrequest length.\n"));
1356 /* allocate request buffer */
1357 KMALLOC(*p_isr, struct ipsecrequest *, sizeof(**p_isr));
1358 if ((*p_isr) == NULL) {
1359 ipseclog((LOG_DEBUG,
1360 "key_msg2sp: No more memory.\n"));
1365 bzero(*p_isr, sizeof(**p_isr));
1368 (*p_isr)->next = NULL;
1370 switch (xisr->sadb_x_ipsecrequest_proto) {
1373 case IPPROTO_IPCOMP:
1376 ipseclog((LOG_DEBUG,
1377 "key_msg2sp: invalid proto type=%u\n",
1378 xisr->sadb_x_ipsecrequest_proto));
1380 *error = EPROTONOSUPPORT;
1383 (*p_isr)->saidx.proto = xisr->sadb_x_ipsecrequest_proto;
1385 switch (xisr->sadb_x_ipsecrequest_mode) {
1386 case IPSEC_MODE_TRANSPORT:
1387 case IPSEC_MODE_TUNNEL:
1389 case IPSEC_MODE_ANY:
1391 ipseclog((LOG_DEBUG,
1392 "key_msg2sp: invalid mode=%u\n",
1393 xisr->sadb_x_ipsecrequest_mode));
1398 (*p_isr)->saidx.mode = xisr->sadb_x_ipsecrequest_mode;
1400 switch (xisr->sadb_x_ipsecrequest_level) {
1401 case IPSEC_LEVEL_DEFAULT:
1402 case IPSEC_LEVEL_USE:
1403 case IPSEC_LEVEL_REQUIRE:
1405 case IPSEC_LEVEL_UNIQUE:
1406 /* validity check */
1408 * If range violation of reqid, kernel will
1409 * update it, don't refuse it.
1411 if (xisr->sadb_x_ipsecrequest_reqid
1412 > IPSEC_MANUAL_REQID_MAX) {
1413 ipseclog((LOG_DEBUG,
1414 "key_msg2sp: reqid=%d range "
1415 "violation, updated by kernel.\n",
1416 xisr->sadb_x_ipsecrequest_reqid));
1417 xisr->sadb_x_ipsecrequest_reqid = 0;
1420 /* allocate new reqid id if reqid is zero. */
1421 if (xisr->sadb_x_ipsecrequest_reqid == 0) {
1423 if ((reqid = key_newreqid()) == 0) {
1428 (*p_isr)->saidx.reqid = reqid;
1429 xisr->sadb_x_ipsecrequest_reqid = reqid;
1431 /* set it for manual keying. */
1432 (*p_isr)->saidx.reqid =
1433 xisr->sadb_x_ipsecrequest_reqid;
1438 ipseclog((LOG_DEBUG, "key_msg2sp: invalid level=%u\n",
1439 xisr->sadb_x_ipsecrequest_level));
1444 (*p_isr)->level = xisr->sadb_x_ipsecrequest_level;
1446 /* set IP addresses if there */
1447 if (xisr->sadb_x_ipsecrequest_len > sizeof(*xisr)) {
1448 struct sockaddr *paddr;
1450 paddr = (struct sockaddr *)(xisr + 1);
1452 /* validity check */
1454 > sizeof((*p_isr)->saidx.src)) {
1455 ipseclog((LOG_DEBUG, "key_msg2sp: invalid request "
1456 "address length.\n"));
1461 bcopy(paddr, &(*p_isr)->saidx.src,
1464 paddr = (struct sockaddr *)((caddr_t)paddr
1467 /* validity check */
1469 > sizeof((*p_isr)->saidx.dst)) {
1470 ipseclog((LOG_DEBUG, "key_msg2sp: invalid request "
1471 "address length.\n"));
1476 bcopy(paddr, &(*p_isr)->saidx.dst,
1480 (*p_isr)->sav = NULL;
1481 (*p_isr)->sp = newsp;
1483 /* initialization for the next. */
1484 p_isr = &(*p_isr)->next;
1485 tlen -= xisr->sadb_x_ipsecrequest_len;
1487 /* validity check */
1489 ipseclog((LOG_DEBUG, "key_msg2sp: becoming tlen < 0.\n"));
1495 xisr = (struct sadb_x_ipsecrequest *)((caddr_t)xisr
1496 + xisr->sadb_x_ipsecrequest_len);
1501 ipseclog((LOG_DEBUG, "key_msg2sp: invalid policy type.\n"));
1514 static u_int32_t auto_reqid = IPSEC_MANUAL_REQID_MAX + 1;
1516 auto_reqid = (auto_reqid == ~0
1517 ? IPSEC_MANUAL_REQID_MAX + 1 : auto_reqid + 1);
1519 /* XXX should be unique check */
1525 * copy secpolicy struct to sadb_x_policy structure indicated.
1529 struct secpolicy *sp;
1531 struct sadb_x_policy *xpl;
1538 panic("key_sp2msg: NULL pointer was passed.\n");
1540 tlen = key_getspreqmsglen(sp);
1542 m = key_alloc_mbuf(tlen);
1543 if (!m || m->m_next) { /*XXX*/
1551 xpl = mtod(m, struct sadb_x_policy *);
1554 xpl->sadb_x_policy_len = PFKEY_UNIT64(tlen);
1555 xpl->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
1556 xpl->sadb_x_policy_type = sp->policy;
1557 xpl->sadb_x_policy_dir = sp->spidx.dir;
1558 xpl->sadb_x_policy_id = sp->id;
1559 p = (caddr_t)xpl + sizeof(*xpl);
1561 /* if is the policy for ipsec ? */
1562 if (sp->policy == IPSEC_POLICY_IPSEC) {
1563 struct sadb_x_ipsecrequest *xisr;
1564 struct ipsecrequest *isr;
1566 for (isr = sp->req; isr != NULL; isr = isr->next) {
1568 xisr = (struct sadb_x_ipsecrequest *)p;
1570 xisr->sadb_x_ipsecrequest_proto = isr->saidx.proto;
1571 xisr->sadb_x_ipsecrequest_mode = isr->saidx.mode;
1572 xisr->sadb_x_ipsecrequest_level = isr->level;
1573 xisr->sadb_x_ipsecrequest_reqid = isr->saidx.reqid;
1576 bcopy(&isr->saidx.src, p, isr->saidx.src.sa.sa_len);
1577 p += isr->saidx.src.sa.sa_len;
1578 bcopy(&isr->saidx.dst, p, isr->saidx.dst.sa.sa_len);
1579 p += isr->saidx.src.sa.sa_len;
1581 xisr->sadb_x_ipsecrequest_len =
1582 PFKEY_ALIGN8(sizeof(*xisr)
1583 + isr->saidx.src.sa.sa_len
1584 + isr->saidx.dst.sa.sa_len);
1591 /* m will not be freed nor modified */
1592 static struct mbuf *
1593 key_gather_mbuf(struct mbuf *m, const struct sadb_msghdr *mhp,
1594 int ndeep, int nitem, ...)
1599 struct mbuf *result = NULL, *n;
1602 if (m == NULL || mhp == NULL)
1603 panic("null pointer passed to key_gather");
1605 __va_start(ap, nitem);
1606 for (i = 0; i < nitem; i++) {
1607 idx = __va_arg(ap, int);
1608 if (idx < 0 || idx > SADB_EXT_MAX)
1610 /* don't attempt to pull empty extension */
1611 if (idx == SADB_EXT_RESERVED && mhp->msg == NULL)
1613 if (idx != SADB_EXT_RESERVED &&
1614 (mhp->ext[idx] == NULL || mhp->extlen[idx] == 0))
1617 if (idx == SADB_EXT_RESERVED) {
1618 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
1621 panic("assumption failed");
1623 MGETHDR(n, MB_DONTWAIT, MT_DATA);
1628 m_copydata(m, 0, sizeof(struct sadb_msg),
1630 } else if (i < ndeep) {
1631 len = mhp->extlen[idx];
1632 n = key_alloc_mbuf(len);
1633 if (!n || n->m_next) { /*XXX*/
1638 m_copydata(m, mhp->extoff[idx], mhp->extlen[idx],
1641 n = m_copym(m, mhp->extoff[idx], mhp->extlen[idx],
1654 if ((result->m_flags & M_PKTHDR) != 0) {
1655 result->m_pkthdr.len = 0;
1656 for (n = result; n; n = n->m_next)
1657 result->m_pkthdr.len += n->m_len;
1668 * SADB_X_SPDADD, SADB_X_SPDSETIDX or SADB_X_SPDUPDATE processing
1669 * add an entry to SP database, when received
1670 * <base, address(SD), (lifetime(H),) policy>
1672 * Adding to SP database,
1674 * <base, address(SD), (lifetime(H),) policy>
1675 * to the socket which was send.
1677 * SPDADD set a unique policy entry.
1678 * SPDSETIDX like SPDADD without a part of policy requests.
1679 * SPDUPDATE replace a unique policy entry.
1681 * m will always be freed.
1684 key_spdadd(so, m, mhp)
1687 const struct sadb_msghdr *mhp;
1689 struct sadb_address *src0, *dst0;
1690 struct sadb_x_policy *xpl0, *xpl;
1691 struct sadb_lifetime *lft = NULL;
1692 struct secpolicyindex spidx;
1693 struct secpolicy *newsp;
1697 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
1698 panic("key_spdadd: NULL pointer is passed.\n");
1700 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
1701 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
1702 mhp->ext[SADB_X_EXT_POLICY] == NULL) {
1703 ipseclog((LOG_DEBUG, "key_spdadd: invalid message is passed.\n"));
1704 return key_senderror(so, m, EINVAL);
1706 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
1707 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
1708 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
1709 ipseclog((LOG_DEBUG, "key_spdadd: invalid message is passed.\n"));
1710 return key_senderror(so, m, EINVAL);
1712 if (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL) {
1713 if (mhp->extlen[SADB_EXT_LIFETIME_HARD]
1714 < sizeof(struct sadb_lifetime)) {
1715 ipseclog((LOG_DEBUG, "key_spdadd: invalid message is passed.\n"));
1716 return key_senderror(so, m, EINVAL);
1718 lft = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_HARD];
1721 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
1722 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
1723 xpl0 = (struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY];
1726 /* XXX boundary check against sa_len */
1727 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
1730 src0->sadb_address_prefixlen,
1731 dst0->sadb_address_prefixlen,
1732 src0->sadb_address_proto,
1735 /* checking the direciton. */
1736 switch (xpl0->sadb_x_policy_dir) {
1737 case IPSEC_DIR_INBOUND:
1738 case IPSEC_DIR_OUTBOUND:
1741 ipseclog((LOG_DEBUG, "key_spdadd: Invalid SP direction.\n"));
1742 mhp->msg->sadb_msg_errno = EINVAL;
1747 /* key_spdadd() accepts DISCARD, NONE and IPSEC. */
1748 if (xpl0->sadb_x_policy_type == IPSEC_POLICY_ENTRUST
1749 || xpl0->sadb_x_policy_type == IPSEC_POLICY_BYPASS) {
1750 ipseclog((LOG_DEBUG, "key_spdadd: Invalid policy type.\n"));
1751 return key_senderror(so, m, EINVAL);
1754 /* policy requests are mandatory when action is ipsec. */
1755 if (mhp->msg->sadb_msg_type != SADB_X_SPDSETIDX
1756 && xpl0->sadb_x_policy_type == IPSEC_POLICY_IPSEC
1757 && mhp->extlen[SADB_X_EXT_POLICY] <= sizeof(*xpl0)) {
1758 ipseclog((LOG_DEBUG, "key_spdadd: some policy requests part required.\n"));
1759 return key_senderror(so, m, EINVAL);
1763 * checking there is SP already or not.
1764 * SPDUPDATE doesn't depend on whether there is a SP or not.
1765 * If the type is either SPDADD or SPDSETIDX AND a SP is found,
1768 newsp = key_getsp(&spidx);
1769 if (mhp->msg->sadb_msg_type == SADB_X_SPDUPDATE) {
1771 newsp->state = IPSEC_SPSTATE_DEAD;
1775 if (newsp != NULL) {
1777 ipseclog((LOG_DEBUG, "key_spdadd: a SP entry exists already.\n"));
1778 return key_senderror(so, m, EEXIST);
1782 /* allocation new SP entry */
1783 if ((newsp = key_msg2sp(xpl0, PFKEY_EXTLEN(xpl0), &error)) == NULL) {
1784 return key_senderror(so, m, error);
1787 if ((newsp->id = key_getnewspid()) == 0) {
1789 return key_senderror(so, m, ENOBUFS);
1792 /* XXX boundary check against sa_len */
1793 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
1796 src0->sadb_address_prefixlen,
1797 dst0->sadb_address_prefixlen,
1798 src0->sadb_address_proto,
1801 /* sanity check on addr pair */
1802 if (((struct sockaddr *)(src0 + 1))->sa_family !=
1803 ((struct sockaddr *)(dst0+ 1))->sa_family) {
1805 return key_senderror(so, m, EINVAL);
1807 if (((struct sockaddr *)(src0 + 1))->sa_len !=
1808 ((struct sockaddr *)(dst0+ 1))->sa_len) {
1810 return key_senderror(so, m, EINVAL);
1813 if (newsp->req && newsp->req->saidx.src.sa.sa_family) {
1814 struct sockaddr *sa;
1815 sa = (struct sockaddr *)(src0 + 1);
1816 if (sa->sa_family != newsp->req->saidx.src.sa.sa_family) {
1818 return key_senderror(so, m, EINVAL);
1821 if (newsp->req && newsp->req->saidx.dst.sa.sa_family) {
1822 struct sockaddr *sa;
1823 sa = (struct sockaddr *)(dst0 + 1);
1824 if (sa->sa_family != newsp->req->saidx.dst.sa.sa_family) {
1826 return key_senderror(so, m, EINVAL);
1831 newsp->created = time_second;
1832 newsp->lastused = newsp->created;
1833 newsp->lifetime = lft ? lft->sadb_lifetime_addtime : 0;
1834 newsp->validtime = lft ? lft->sadb_lifetime_usetime : 0;
1836 newsp->refcnt = 1; /* do not reclaim until I say I do */
1837 newsp->state = IPSEC_SPSTATE_ALIVE;
1838 LIST_INSERT_TAIL(&sptree[newsp->spidx.dir], newsp, secpolicy, chain);
1840 /* delete the entry in spacqtree */
1841 if (mhp->msg->sadb_msg_type == SADB_X_SPDUPDATE) {
1842 struct secspacq *spacq;
1843 if ((spacq = key_getspacq(&spidx)) != NULL) {
1844 /* reset counter in order to deletion by timehandler. */
1845 spacq->created = time_second;
1851 struct mbuf *n, *mpolicy;
1852 struct sadb_msg *newmsg;
1855 /* create new sadb_msg to reply. */
1857 n = key_gather_mbuf(m, mhp, 2, 5, SADB_EXT_RESERVED,
1858 SADB_X_EXT_POLICY, SADB_EXT_LIFETIME_HARD,
1859 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
1861 n = key_gather_mbuf(m, mhp, 2, 4, SADB_EXT_RESERVED,
1863 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
1866 return key_senderror(so, m, ENOBUFS);
1868 if (n->m_len < sizeof(*newmsg)) {
1869 n = m_pullup(n, sizeof(*newmsg));
1871 return key_senderror(so, m, ENOBUFS);
1873 newmsg = mtod(n, struct sadb_msg *);
1874 newmsg->sadb_msg_errno = 0;
1875 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
1878 mpolicy = m_pulldown(n, PFKEY_ALIGN8(sizeof(struct sadb_msg)),
1879 sizeof(*xpl), &off);
1880 if (mpolicy == NULL) {
1881 /* n is already freed */
1882 return key_senderror(so, m, ENOBUFS);
1884 xpl = (struct sadb_x_policy *)(mtod(mpolicy, caddr_t) + off);
1885 if (xpl->sadb_x_policy_exttype != SADB_X_EXT_POLICY) {
1887 return key_senderror(so, m, EINVAL);
1889 xpl->sadb_x_policy_id = newsp->id;
1892 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
1897 * get new policy id.
1905 u_int32_t newid = 0;
1906 int count = key_spi_trycnt; /* XXX */
1907 struct secpolicy *sp;
1909 /* when requesting to allocate spi ranged */
1911 newid = (policy_id = (policy_id == ~0 ? 1 : policy_id + 1));
1913 if ((sp = key_getspbyid(newid)) == NULL)
1919 if (count == 0 || newid == 0) {
1920 ipseclog((LOG_DEBUG, "key_getnewspid: to allocate policy id is failed.\n"));
1928 * SADB_SPDDELETE processing
1930 * <base, address(SD), policy(*)>
1931 * from the user(?), and set SADB_SASTATE_DEAD,
1933 * <base, address(SD), policy(*)>
1935 * policy(*) including direction of policy.
1937 * m will always be freed.
1940 key_spddelete(so, m, mhp)
1943 const struct sadb_msghdr *mhp;
1945 struct sadb_address *src0, *dst0;
1946 struct sadb_x_policy *xpl0;
1947 struct secpolicyindex spidx;
1948 struct secpolicy *sp;
1951 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
1952 panic("key_spddelete: NULL pointer is passed.\n");
1954 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
1955 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
1956 mhp->ext[SADB_X_EXT_POLICY] == NULL) {
1957 ipseclog((LOG_DEBUG, "key_spddelete: invalid message is passed.\n"));
1958 return key_senderror(so, m, EINVAL);
1960 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
1961 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
1962 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
1963 ipseclog((LOG_DEBUG, "key_spddelete: invalid message is passed.\n"));
1964 return key_senderror(so, m, EINVAL);
1967 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
1968 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
1969 xpl0 = (struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY];
1972 /* XXX boundary check against sa_len */
1973 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
1976 src0->sadb_address_prefixlen,
1977 dst0->sadb_address_prefixlen,
1978 src0->sadb_address_proto,
1981 /* checking the direciton. */
1982 switch (xpl0->sadb_x_policy_dir) {
1983 case IPSEC_DIR_INBOUND:
1984 case IPSEC_DIR_OUTBOUND:
1987 ipseclog((LOG_DEBUG, "key_spddelete: Invalid SP direction.\n"));
1988 return key_senderror(so, m, EINVAL);
1991 /* Is there SP in SPD ? */
1992 if ((sp = key_getsp(&spidx)) == NULL) {
1993 ipseclog((LOG_DEBUG, "key_spddelete: no SP found.\n"));
1994 return key_senderror(so, m, EINVAL);
1997 /* save policy id to buffer to be returned. */
1998 xpl0->sadb_x_policy_id = sp->id;
2000 sp->state = IPSEC_SPSTATE_DEAD;
2005 struct sadb_msg *newmsg;
2007 /* create new sadb_msg to reply. */
2008 n = key_gather_mbuf(m, mhp, 1, 4, SADB_EXT_RESERVED,
2009 SADB_X_EXT_POLICY, SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
2011 return key_senderror(so, m, ENOBUFS);
2013 newmsg = mtod(n, struct sadb_msg *);
2014 newmsg->sadb_msg_errno = 0;
2015 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
2018 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
2023 * SADB_SPDDELETE2 processing
2026 * from the user(?), and set SADB_SASTATE_DEAD,
2030 * policy(*) including direction of policy.
2032 * m will always be freed.
2035 key_spddelete2(so, m, mhp)
2038 const struct sadb_msghdr *mhp;
2041 struct secpolicy *sp;
2044 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
2045 panic("key_spddelete2: NULL pointer is passed.\n");
2047 if (mhp->ext[SADB_X_EXT_POLICY] == NULL ||
2048 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2049 ipseclog((LOG_DEBUG, "key_spddelete2: invalid message is passed.\n"));
2050 key_senderror(so, m, EINVAL);
2054 id = ((struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY])->sadb_x_policy_id;
2056 /* Is there SP in SPD ? */
2057 if ((sp = key_getspbyid(id)) == NULL) {
2058 ipseclog((LOG_DEBUG, "key_spddelete2: no SP found id:%u.\n", id));
2059 key_senderror(so, m, EINVAL);
2062 sp->state = IPSEC_SPSTATE_DEAD;
2066 struct mbuf *n, *nn;
2067 struct sadb_msg *newmsg;
2070 /* create new sadb_msg to reply. */
2071 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
2074 return key_senderror(so, m, ENOBUFS);
2075 MGETHDR(n, MB_DONTWAIT, MT_DATA);
2076 if (n && len > MHLEN) {
2077 MCLGET(n, MB_DONTWAIT);
2078 if ((n->m_flags & M_EXT) == 0) {
2084 return key_senderror(so, m, ENOBUFS);
2090 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
2091 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
2095 panic("length inconsistency in key_spddelete2");
2098 n->m_next = m_copym(m, mhp->extoff[SADB_X_EXT_POLICY],
2099 mhp->extlen[SADB_X_EXT_POLICY], MB_DONTWAIT);
2102 return key_senderror(so, m, ENOBUFS);
2105 n->m_pkthdr.len = 0;
2106 for (nn = n; nn; nn = nn->m_next)
2107 n->m_pkthdr.len += nn->m_len;
2109 newmsg = mtod(n, struct sadb_msg *);
2110 newmsg->sadb_msg_errno = 0;
2111 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
2114 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
2119 * SADB_X_GET processing
2124 * <base, address(SD), policy>
2126 * policy(*) including direction of policy.
2128 * m will always be freed.
2131 key_spdget(so, m, mhp)
2134 const struct sadb_msghdr *mhp;
2137 struct secpolicy *sp;
2141 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
2142 panic("key_spdget: NULL pointer is passed.\n");
2144 if (mhp->ext[SADB_X_EXT_POLICY] == NULL ||
2145 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2146 ipseclog((LOG_DEBUG, "key_spdget: invalid message is passed.\n"));
2147 return key_senderror(so, m, EINVAL);
2150 id = ((struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY])->sadb_x_policy_id;
2152 /* Is there SP in SPD ? */
2153 if ((sp = key_getspbyid(id)) == NULL) {
2154 ipseclog((LOG_DEBUG, "key_spdget: no SP found id:%u.\n", id));
2155 return key_senderror(so, m, ENOENT);
2158 n = key_setdumpsp(sp, SADB_X_SPDGET, 0, mhp->msg->sadb_msg_pid);
2161 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
2163 return key_senderror(so, m, ENOBUFS);
2167 * SADB_X_SPDACQUIRE processing.
2168 * Acquire policy and SA(s) for a *OUTBOUND* packet.
2171 * to KMD, and expect to receive
2172 * <base> with SADB_X_SPDACQUIRE if error occured,
2175 * with SADB_X_SPDUPDATE from KMD by PF_KEY.
2176 * policy(*) is without policy requests.
2179 * others: error number
2183 struct secpolicy *sp;
2185 struct mbuf *result = NULL, *m;
2186 struct secspacq *newspacq;
2191 panic("key_spdacquire: NULL pointer is passed.\n");
2192 if (sp->req != NULL)
2193 panic("key_spdacquire: called but there is request.\n");
2194 if (sp->policy != IPSEC_POLICY_IPSEC)
2195 panic("key_spdacquire: policy mismathed. IPsec is expected.\n");
2197 /* Get an entry to check whether sent message or not. */
2198 if ((newspacq = key_getspacq(&sp->spidx)) != NULL) {
2199 if (key_blockacq_count < newspacq->count) {
2200 /* reset counter and do send message. */
2201 newspacq->count = 0;
2203 /* increment counter and do nothing. */
2208 /* make new entry for blocking to send SADB_ACQUIRE. */
2209 if ((newspacq = key_newspacq(&sp->spidx)) == NULL)
2212 /* add to acqtree */
2213 LIST_INSERT_HEAD(&spacqtree, newspacq, chain);
2216 /* create new sadb_msg to reply. */
2217 m = key_setsadbmsg(SADB_X_SPDACQUIRE, 0, 0, 0, 0, 0);
2224 result->m_pkthdr.len = 0;
2225 for (m = result; m; m = m->m_next)
2226 result->m_pkthdr.len += m->m_len;
2228 mtod(result, struct sadb_msg *)->sadb_msg_len =
2229 PFKEY_UNIT64(result->m_pkthdr.len);
2231 return key_sendup_mbuf(NULL, m, KEY_SENDUP_REGISTERED);
2240 * SADB_SPDFLUSH processing
2243 * from the user, and free all entries in secpctree.
2247 * NOTE: what to do is only marking SADB_SASTATE_DEAD.
2249 * m will always be freed.
2252 key_spdflush(so, m, mhp)
2255 const struct sadb_msghdr *mhp;
2257 struct sadb_msg *newmsg;
2258 struct secpolicy *sp;
2262 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
2263 panic("key_spdflush: NULL pointer is passed.\n");
2265 if (m->m_len != PFKEY_ALIGN8(sizeof(struct sadb_msg)))
2266 return key_senderror(so, m, EINVAL);
2268 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2269 LIST_FOREACH(sp, &sptree[dir], chain) {
2270 sp->state = IPSEC_SPSTATE_DEAD;
2274 if (sizeof(struct sadb_msg) > m->m_len + M_TRAILINGSPACE(m)) {
2275 ipseclog((LOG_DEBUG, "key_spdflush: No more memory.\n"));
2276 return key_senderror(so, m, ENOBUFS);
2282 m->m_pkthdr.len = m->m_len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
2283 newmsg = mtod(m, struct sadb_msg *);
2284 newmsg->sadb_msg_errno = 0;
2285 newmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
2287 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
2291 * SADB_SPDDUMP processing
2294 * from the user, and dump all SP leaves
2299 * m will always be freed.
2302 key_spddump(so, m, mhp)
2305 const struct sadb_msghdr *mhp;
2307 struct secpolicy *sp;
2313 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
2314 panic("key_spddump: NULL pointer is passed.\n");
2316 /* search SPD entry and get buffer size. */
2318 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2319 LIST_FOREACH(sp, &sptree[dir], chain) {
2325 return key_senderror(so, m, ENOENT);
2327 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2328 LIST_FOREACH(sp, &sptree[dir], chain) {
2330 n = key_setdumpsp(sp, SADB_X_SPDDUMP, cnt,
2331 mhp->msg->sadb_msg_pid);
2334 key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
2342 static struct mbuf *
2343 key_setdumpsp(sp, type, seq, pid)
2344 struct secpolicy *sp;
2348 struct mbuf *result = NULL, *m;
2350 m = key_setsadbmsg(type, 0, SADB_SATYPE_UNSPEC, seq, pid, sp->refcnt);
2355 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
2356 &sp->spidx.src.sa, sp->spidx.prefs,
2357 sp->spidx.ul_proto);
2362 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
2363 &sp->spidx.dst.sa, sp->spidx.prefd,
2364 sp->spidx.ul_proto);
2374 if ((result->m_flags & M_PKTHDR) == 0)
2377 if (result->m_len < sizeof(struct sadb_msg)) {
2378 result = m_pullup(result, sizeof(struct sadb_msg));
2383 result->m_pkthdr.len = 0;
2384 for (m = result; m; m = m->m_next)
2385 result->m_pkthdr.len += m->m_len;
2387 mtod(result, struct sadb_msg *)->sadb_msg_len =
2388 PFKEY_UNIT64(result->m_pkthdr.len);
2398 * get PFKEY message length for security policy and request.
2401 key_getspreqmsglen(sp)
2402 struct secpolicy *sp;
2406 tlen = sizeof(struct sadb_x_policy);
2408 /* if is the policy for ipsec ? */
2409 if (sp->policy != IPSEC_POLICY_IPSEC)
2412 /* get length of ipsec requests */
2414 struct ipsecrequest *isr;
2417 for (isr = sp->req; isr != NULL; isr = isr->next) {
2418 len = sizeof(struct sadb_x_ipsecrequest)
2419 + isr->saidx.src.sa.sa_len
2420 + isr->saidx.dst.sa.sa_len;
2422 tlen += PFKEY_ALIGN8(len);
2430 * SADB_SPDEXPIRE processing
2432 * <base, address(SD), lifetime(CH), policy>
2436 * others : error number
2440 struct secpolicy *sp;
2443 struct mbuf *result = NULL, *m;
2446 struct sadb_lifetime *lt;
2448 /* XXX: Why do we lock ? */
2449 s = splnet(); /*called from softclock()*/
2453 panic("key_spdexpire: NULL pointer is passed.\n");
2455 /* set msg header */
2456 m = key_setsadbmsg(SADB_X_SPDEXPIRE, 0, 0, 0, 0, 0);
2463 /* create lifetime extension (current and hard) */
2464 len = PFKEY_ALIGN8(sizeof(*lt)) * 2;
2465 m = key_alloc_mbuf(len);
2466 if (!m || m->m_next) { /*XXX*/
2472 bzero(mtod(m, caddr_t), len);
2473 lt = mtod(m, struct sadb_lifetime *);
2474 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
2475 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
2476 lt->sadb_lifetime_allocations = 0;
2477 lt->sadb_lifetime_bytes = 0;
2478 lt->sadb_lifetime_addtime = sp->created;
2479 lt->sadb_lifetime_usetime = sp->lastused;
2480 lt = (struct sadb_lifetime *)(mtod(m, caddr_t) + len / 2);
2481 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
2482 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
2483 lt->sadb_lifetime_allocations = 0;
2484 lt->sadb_lifetime_bytes = 0;
2485 lt->sadb_lifetime_addtime = sp->lifetime;
2486 lt->sadb_lifetime_usetime = sp->validtime;
2489 /* set sadb_address for source */
2490 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
2492 sp->spidx.prefs, sp->spidx.ul_proto);
2499 /* set sadb_address for destination */
2500 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
2502 sp->spidx.prefd, sp->spidx.ul_proto);
2517 if ((result->m_flags & M_PKTHDR) == 0) {
2522 if (result->m_len < sizeof(struct sadb_msg)) {
2523 result = m_pullup(result, sizeof(struct sadb_msg));
2524 if (result == NULL) {
2530 result->m_pkthdr.len = 0;
2531 for (m = result; m; m = m->m_next)
2532 result->m_pkthdr.len += m->m_len;
2534 mtod(result, struct sadb_msg *)->sadb_msg_len =
2535 PFKEY_UNIT64(result->m_pkthdr.len);
2537 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
2546 /* %%% SAD management */
2548 * allocating a memory for new SA head, and copy from the values of mhp.
2549 * OUT: NULL : failure due to the lack of memory.
2550 * others : pointer to new SA head.
2552 static struct secashead *
2554 struct secasindex *saidx;
2556 struct secashead *newsah;
2558 KASSERT(saidx != NULL, ("key_newsaidx: null saidx"));
2560 newsah = malloc(sizeof(struct secashead), M_SECA,
2561 M_INTWAIT | M_ZERO | M_NULLOK);
2562 if (newsah != NULL) {
2564 for (i = 0; i < sizeof(newsah->savtree)/sizeof(newsah->savtree[0]); i++)
2565 LIST_INIT(&newsah->savtree[i]);
2566 newsah->saidx = *saidx;
2568 /* add to saidxtree */
2569 newsah->state = SADB_SASTATE_MATURE;
2570 LIST_INSERT_HEAD(&sahtree, newsah, chain);
2576 * delete SA index and all SA registerd.
2580 struct secashead *sah;
2582 struct secasvar *sav, *nextsav;
2583 u_int stateidx, state;
2589 panic("key_delsah: NULL pointer is passed.\n");
2591 s = splnet(); /*called from softclock()*/
2593 /* searching all SA registerd in the secindex. */
2595 stateidx < _ARRAYLEN(saorder_state_any);
2598 state = saorder_state_any[stateidx];
2599 for (sav = (struct secasvar *)LIST_FIRST(&sah->savtree[state]);
2603 nextsav = LIST_NEXT(sav, chain);
2605 if (sav->refcnt == 0) {
2607 KEY_CHKSASTATE(state, sav->state, "key_delsah");
2610 /* give up to delete this sa */
2616 /* don't delete sah only if there are savs. */
2622 if (sah->sa_route.ro_rt) {
2623 RTFREE(sah->sa_route.ro_rt);
2624 sah->sa_route.ro_rt = (struct rtentry *)NULL;
2627 /* remove from tree of SA index */
2628 if (__LIST_CHAINED(sah))
2629 LIST_REMOVE(sah, chain);
2638 * allocating a new SA with LARVAL state. key_add() and key_getspi() call,
2639 * and copy the values of mhp into new buffer.
2640 * When SAD message type is GETSPI:
2641 * to set sequence number from acq_seq++,
2642 * to set zero to SPI.
2643 * not to call key_setsava().
2645 * others : pointer to new secasvar.
2647 * does not modify mbuf. does not free mbuf on error.
2649 static struct secasvar *
2650 key_newsav(m, mhp, sah, errp, where, tag)
2652 const struct sadb_msghdr *mhp;
2653 struct secashead *sah;
2658 struct secasvar *newsav;
2659 const struct sadb_sa *xsa;
2662 if (m == NULL || mhp == NULL || mhp->msg == NULL || sah == NULL)
2663 panic("key_newsa: NULL pointer is passed.\n");
2665 KMALLOC(newsav, struct secasvar *, sizeof(struct secasvar));
2666 if (newsav == NULL) {
2667 ipseclog((LOG_DEBUG, "key_newsa: No more memory.\n"));
2671 bzero((caddr_t)newsav, sizeof(struct secasvar));
2673 switch (mhp->msg->sadb_msg_type) {
2677 #ifdef IPSEC_DOSEQCHECK
2678 /* sync sequence number */
2679 if (mhp->msg->sadb_msg_seq == 0)
2681 (acq_seq = (acq_seq == ~0 ? 1 : ++acq_seq));
2684 newsav->seq = mhp->msg->sadb_msg_seq;
2689 if (mhp->ext[SADB_EXT_SA] == NULL) {
2690 KFREE(newsav), newsav = NULL;
2691 ipseclog((LOG_DEBUG, "key_newsa: invalid message is passed.\n"));
2695 xsa = (const struct sadb_sa *)mhp->ext[SADB_EXT_SA];
2696 newsav->spi = xsa->sadb_sa_spi;
2697 newsav->seq = mhp->msg->sadb_msg_seq;
2700 KFREE(newsav), newsav = NULL;
2705 /* copy sav values */
2706 if (mhp->msg->sadb_msg_type != SADB_GETSPI) {
2707 *errp = key_setsaval(newsav, m, mhp);
2709 KFREE(newsav), newsav = NULL;
2715 newsav->created = time_second;
2716 newsav->pid = mhp->msg->sadb_msg_pid;
2721 newsav->state = SADB_SASTATE_LARVAL;
2722 LIST_INSERT_TAIL(&sah->savtree[SADB_SASTATE_LARVAL], newsav,
2725 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
2726 printf("DP key_newsav from %s:%u return SP:%p\n",
2727 where, tag, newsav));
2733 * free() SA variable entry.
2737 struct secasvar *sav;
2739 KASSERT(sav != NULL, ("key_delsav: null sav"));
2740 KASSERT(sav->refcnt == 0,
2741 ("key_delsav: reference count %u > 0", sav->refcnt));
2743 /* remove from SA header */
2744 if (__LIST_CHAINED(sav))
2745 LIST_REMOVE(sav, chain);
2747 if (sav->key_auth != NULL) {
2748 bzero(_KEYBUF(sav->key_auth), _KEYLEN(sav->key_auth));
2749 KFREE(sav->key_auth);
2750 sav->key_auth = NULL;
2752 if (sav->key_enc != NULL) {
2753 bzero(_KEYBUF(sav->key_enc), _KEYLEN(sav->key_enc));
2754 KFREE(sav->key_enc);
2755 sav->key_enc = NULL;
2758 bzero(sav->sched, sav->schedlen);
2762 if (sav->replay != NULL) {
2766 if (sav->lft_c != NULL) {
2770 if (sav->lft_h != NULL) {
2774 if (sav->lft_s != NULL) {
2778 if (sav->iv != NULL) {
2792 * others : found, pointer to a SA.
2794 static struct secashead *
2796 struct secasindex *saidx;
2798 struct secashead *sah;
2800 LIST_FOREACH(sah, &sahtree, chain) {
2801 if (sah->state == SADB_SASTATE_DEAD)
2803 if (key_cmpsaidx(&sah->saidx, saidx, CMP_REQID))
2811 * check not to be duplicated SPI.
2812 * NOTE: this function is too slow due to searching all SAD.
2815 * others : found, pointer to a SA.
2817 static struct secasvar *
2818 key_checkspidup(saidx, spi)
2819 struct secasindex *saidx;
2822 struct secashead *sah;
2823 struct secasvar *sav;
2825 /* check address family */
2826 if (saidx->src.sa.sa_family != saidx->dst.sa.sa_family) {
2827 ipseclog((LOG_DEBUG, "key_checkspidup: address family mismatched.\n"));
2832 LIST_FOREACH(sah, &sahtree, chain) {
2833 if (!key_ismyaddr((struct sockaddr *)&sah->saidx.dst))
2835 sav = key_getsavbyspi(sah, spi);
2844 * search SAD litmited alive SA, protocol, SPI.
2847 * others : found, pointer to a SA.
2849 static struct secasvar *
2850 key_getsavbyspi(sah, spi)
2851 struct secashead *sah;
2854 struct secasvar *sav;
2855 u_int stateidx, state;
2857 /* search all status */
2859 stateidx < _ARRAYLEN(saorder_state_alive);
2862 state = saorder_state_alive[stateidx];
2863 LIST_FOREACH(sav, &sah->savtree[state], chain) {
2866 if (sav->state != state) {
2867 ipseclog((LOG_DEBUG, "key_getsavbyspi: "
2868 "invalid sav->state (queue: %d SA: %d)\n",
2869 state, sav->state));
2873 if (sav->spi == spi)
2882 * copy SA values from PF_KEY message except *SPI, SEQ, PID, STATE and TYPE*.
2883 * You must update these if need.
2887 * does not modify mbuf. does not free mbuf on error.
2890 key_setsaval(sav, m, mhp)
2891 struct secasvar *sav;
2893 const struct sadb_msghdr *mhp;
2898 if (m == NULL || mhp == NULL || mhp->msg == NULL)
2899 panic("key_setsaval: NULL pointer is passed.\n");
2901 /* initialization */
2903 sav->key_auth = NULL;
2904 sav->key_enc = NULL;
2911 sav->tdb_xform = NULL; /* transform */
2912 sav->tdb_encalgxform = NULL; /* encoding algorithm */
2913 sav->tdb_authalgxform = NULL; /* authentication algorithm */
2914 sav->tdb_compalgxform = NULL; /* compression algorithm */
2917 if (mhp->ext[SADB_EXT_SA] != NULL) {
2918 const struct sadb_sa *sa0;
2920 sa0 = (const struct sadb_sa *)mhp->ext[SADB_EXT_SA];
2921 if (mhp->extlen[SADB_EXT_SA] < sizeof(*sa0)) {
2926 sav->alg_auth = sa0->sadb_sa_auth;
2927 sav->alg_enc = sa0->sadb_sa_encrypt;
2928 sav->flags = sa0->sadb_sa_flags;
2931 if ((sa0->sadb_sa_flags & SADB_X_EXT_OLD) == 0) {
2933 malloc(sizeof(struct secreplay)+sa0->sadb_sa_replay,
2934 M_SECA, M_INTWAIT | M_ZERO | M_NULLOK);
2935 if (sav->replay == NULL) {
2936 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
2940 if (sa0->sadb_sa_replay != 0)
2941 sav->replay->bitmap = (caddr_t)(sav->replay+1);
2942 sav->replay->wsize = sa0->sadb_sa_replay;
2946 /* Authentication keys */
2947 if (mhp->ext[SADB_EXT_KEY_AUTH] != NULL) {
2948 const struct sadb_key *key0;
2951 key0 = (const struct sadb_key *)mhp->ext[SADB_EXT_KEY_AUTH];
2952 len = mhp->extlen[SADB_EXT_KEY_AUTH];
2955 if (len < sizeof(*key0)) {
2959 switch (mhp->msg->sadb_msg_satype) {
2960 case SADB_SATYPE_AH:
2961 case SADB_SATYPE_ESP:
2962 if (len == PFKEY_ALIGN8(sizeof(struct sadb_key)) &&
2963 sav->alg_auth != SADB_X_AALG_NULL)
2966 case SADB_X_SATYPE_IPCOMP:
2972 ipseclog((LOG_DEBUG, "key_setsaval: invalid key_auth values.\n"));
2976 sav->key_auth = (struct sadb_key *)key_newbuf(key0, len);
2977 if (sav->key_auth == NULL) {
2978 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
2984 /* Encryption key */
2985 if (mhp->ext[SADB_EXT_KEY_ENCRYPT] != NULL) {
2986 const struct sadb_key *key0;
2989 key0 = (const struct sadb_key *)mhp->ext[SADB_EXT_KEY_ENCRYPT];
2990 len = mhp->extlen[SADB_EXT_KEY_ENCRYPT];
2993 if (len < sizeof(*key0)) {
2997 switch (mhp->msg->sadb_msg_satype) {
2998 case SADB_SATYPE_ESP:
2999 if (len == PFKEY_ALIGN8(sizeof(struct sadb_key)) &&
3000 sav->alg_enc != SADB_EALG_NULL) {
3004 sav->key_enc = (struct sadb_key *)key_newbuf(key0, len);
3005 if (sav->key_enc == NULL) {
3006 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
3011 case SADB_X_SATYPE_IPCOMP:
3012 if (len != PFKEY_ALIGN8(sizeof(struct sadb_key)))
3014 sav->key_enc = NULL; /*just in case*/
3016 case SADB_SATYPE_AH:
3022 ipseclog((LOG_DEBUG, "key_setsatval: invalid key_enc value.\n"));
3030 switch (mhp->msg->sadb_msg_satype) {
3031 case SADB_SATYPE_AH:
3032 error = xform_init(sav, XF_AH);
3034 case SADB_SATYPE_ESP:
3035 error = xform_init(sav, XF_ESP);
3037 case SADB_X_SATYPE_IPCOMP:
3038 error = xform_init(sav, XF_IPCOMP);
3042 ipseclog((LOG_DEBUG,
3043 "key_setsaval: unable to initialize SA type %u.\n",
3044 mhp->msg->sadb_msg_satype));
3049 sav->created = time_second;
3051 /* make lifetime for CURRENT */
3052 KMALLOC(sav->lft_c, struct sadb_lifetime *,
3053 sizeof(struct sadb_lifetime));
3054 if (sav->lft_c == NULL) {
3055 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
3060 sav->lft_c->sadb_lifetime_len =
3061 PFKEY_UNIT64(sizeof(struct sadb_lifetime));
3062 sav->lft_c->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
3063 sav->lft_c->sadb_lifetime_allocations = 0;
3064 sav->lft_c->sadb_lifetime_bytes = 0;
3065 sav->lft_c->sadb_lifetime_addtime = time_second;
3066 sav->lft_c->sadb_lifetime_usetime = 0;
3068 /* lifetimes for HARD and SOFT */
3070 const struct sadb_lifetime *lft0;
3072 lft0 = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_HARD];
3074 if (mhp->extlen[SADB_EXT_LIFETIME_HARD] < sizeof(*lft0)) {
3078 sav->lft_h = (struct sadb_lifetime *)key_newbuf(lft0,
3080 if (sav->lft_h == NULL) {
3081 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
3085 /* to be initialize ? */
3088 lft0 = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_SOFT];
3090 if (mhp->extlen[SADB_EXT_LIFETIME_SOFT] < sizeof(*lft0)) {
3094 sav->lft_s = (struct sadb_lifetime *)key_newbuf(lft0,
3096 if (sav->lft_s == NULL) {
3097 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
3101 /* to be initialize ? */
3108 /* initialization */
3109 if (sav->replay != NULL) {
3113 if (sav->key_auth != NULL) {
3114 KFREE(sav->key_auth);
3115 sav->key_auth = NULL;
3117 if (sav->key_enc != NULL) {
3118 KFREE(sav->key_enc);
3119 sav->key_enc = NULL;
3125 if (sav->iv != NULL) {
3129 if (sav->lft_c != NULL) {
3133 if (sav->lft_h != NULL) {
3137 if (sav->lft_s != NULL) {
3146 * validation with a secasvar entry, and set SADB_SATYPE_MATURE.
3152 struct secasvar *sav;
3156 /* check SPI value */
3157 switch (sav->sah->saidx.proto) {
3160 if (ntohl(sav->spi) >= 0 && ntohl(sav->spi) <= 255) {
3161 ipseclog((LOG_DEBUG,
3162 "key_mature: illegal range of SPI %u.\n",
3163 (u_int32_t)ntohl(sav->spi)));
3170 switch (sav->sah->saidx.proto) {
3173 if ((sav->flags & (SADB_X_EXT_OLD|SADB_X_EXT_DERIV)) ==
3174 (SADB_X_EXT_OLD|SADB_X_EXT_DERIV)) {
3175 ipseclog((LOG_DEBUG, "key_mature: "
3176 "invalid flag (derived) given to old-esp.\n"));
3179 error = xform_init(sav, XF_ESP);
3183 if (sav->flags & SADB_X_EXT_DERIV) {
3184 ipseclog((LOG_DEBUG, "key_mature: "
3185 "invalid flag (derived) given to AH SA.\n"));
3188 if (sav->alg_enc != SADB_EALG_NONE) {
3189 ipseclog((LOG_DEBUG, "key_mature: "
3190 "protocol and algorithm mismated.\n"));
3193 error = xform_init(sav, XF_AH);
3195 case IPPROTO_IPCOMP:
3196 if (sav->alg_auth != SADB_AALG_NONE) {
3197 ipseclog((LOG_DEBUG, "key_mature: "
3198 "protocol and algorithm mismated.\n"));
3201 if ((sav->flags & SADB_X_EXT_RAWCPI) == 0
3202 && ntohl(sav->spi) >= 0x10000) {
3203 ipseclog((LOG_DEBUG, "key_mature: invalid cpi for IPComp.\n"));
3206 error = xform_init(sav, XF_IPCOMP);
3209 ipseclog((LOG_DEBUG, "key_mature: Invalid satype.\n"));
3210 error = EPROTONOSUPPORT;
3214 key_sa_chgstate(sav, SADB_SASTATE_MATURE);
3219 * subroutine for SADB_GET and SADB_DUMP.
3221 static struct mbuf *
3222 key_setdumpsa(sav, type, satype, seq, pid)
3223 struct secasvar *sav;
3224 u_int8_t type, satype;
3227 struct mbuf *result = NULL, *tres = NULL, *m;
3232 SADB_EXT_SA, SADB_X_EXT_SA2,
3233 SADB_EXT_LIFETIME_HARD, SADB_EXT_LIFETIME_SOFT,
3234 SADB_EXT_LIFETIME_CURRENT, SADB_EXT_ADDRESS_SRC,
3235 SADB_EXT_ADDRESS_DST, SADB_EXT_ADDRESS_PROXY, SADB_EXT_KEY_AUTH,
3236 SADB_EXT_KEY_ENCRYPT, SADB_EXT_IDENTITY_SRC,
3237 SADB_EXT_IDENTITY_DST, SADB_EXT_SENSITIVITY,
3240 m = key_setsadbmsg(type, 0, satype, seq, pid, sav->refcnt);
3245 for (i = sizeof(dumporder)/sizeof(dumporder[0]) - 1; i >= 0; i--) {
3248 switch (dumporder[i]) {
3250 m = key_setsadbsa(sav);
3255 case SADB_X_EXT_SA2:
3256 m = key_setsadbxsa2(sav->sah->saidx.mode,
3257 sav->replay ? sav->replay->count : 0,
3258 sav->sah->saidx.reqid);
3263 case SADB_EXT_ADDRESS_SRC:
3264 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
3265 &sav->sah->saidx.src.sa,
3266 FULLMASK, IPSEC_ULPROTO_ANY);
3271 case SADB_EXT_ADDRESS_DST:
3272 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
3273 &sav->sah->saidx.dst.sa,
3274 FULLMASK, IPSEC_ULPROTO_ANY);
3279 case SADB_EXT_KEY_AUTH:
3282 l = PFKEY_UNUNIT64(sav->key_auth->sadb_key_len);
3286 case SADB_EXT_KEY_ENCRYPT:
3289 l = PFKEY_UNUNIT64(sav->key_enc->sadb_key_len);
3293 case SADB_EXT_LIFETIME_CURRENT:
3296 l = PFKEY_UNUNIT64(((struct sadb_ext *)sav->lft_c)->sadb_ext_len);
3300 case SADB_EXT_LIFETIME_HARD:
3303 l = PFKEY_UNUNIT64(((struct sadb_ext *)sav->lft_h)->sadb_ext_len);
3307 case SADB_EXT_LIFETIME_SOFT:
3310 l = PFKEY_UNUNIT64(((struct sadb_ext *)sav->lft_s)->sadb_ext_len);
3314 case SADB_EXT_ADDRESS_PROXY:
3315 case SADB_EXT_IDENTITY_SRC:
3316 case SADB_EXT_IDENTITY_DST:
3317 /* XXX: should we brought from SPD ? */
3318 case SADB_EXT_SENSITIVITY:
3323 if ((!m && !p) || (m && p))
3326 M_PREPEND(tres, l, MB_DONTWAIT);
3329 bcopy(p, mtod(tres, caddr_t), l);
3333 m = key_alloc_mbuf(l);
3336 m_copyback(m, 0, l, p);
3344 m_cat(result, tres);
3346 if (result->m_len < sizeof(struct sadb_msg)) {
3347 result = m_pullup(result, sizeof(struct sadb_msg));
3352 result->m_pkthdr.len = 0;
3353 for (m = result; m; m = m->m_next)
3354 result->m_pkthdr.len += m->m_len;
3356 mtod(result, struct sadb_msg *)->sadb_msg_len =
3357 PFKEY_UNIT64(result->m_pkthdr.len);
3368 * set data into sadb_msg.
3370 static struct mbuf *
3371 key_setsadbmsg(type, tlen, satype, seq, pid, reserved)
3372 u_int8_t type, satype;
3382 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
3385 MGETHDR(m, MB_DONTWAIT, MT_DATA);
3386 if (m && len > MHLEN) {
3387 MCLGET(m, MB_DONTWAIT);
3388 if ((m->m_flags & M_EXT) == 0) {
3395 m->m_pkthdr.len = m->m_len = len;
3398 p = mtod(m, struct sadb_msg *);
3401 p->sadb_msg_version = PF_KEY_V2;
3402 p->sadb_msg_type = type;
3403 p->sadb_msg_errno = 0;
3404 p->sadb_msg_satype = satype;
3405 p->sadb_msg_len = PFKEY_UNIT64(tlen);
3406 p->sadb_msg_reserved = reserved;
3407 p->sadb_msg_seq = seq;
3408 p->sadb_msg_pid = (u_int32_t)pid;
3414 * copy secasvar data into sadb_address.
3416 static struct mbuf *
3418 struct secasvar *sav;
3424 len = PFKEY_ALIGN8(sizeof(struct sadb_sa));
3425 m = key_alloc_mbuf(len);
3426 if (!m || m->m_next) { /*XXX*/
3432 p = mtod(m, struct sadb_sa *);
3435 p->sadb_sa_len = PFKEY_UNIT64(len);
3436 p->sadb_sa_exttype = SADB_EXT_SA;
3437 p->sadb_sa_spi = sav->spi;
3438 p->sadb_sa_replay = (sav->replay != NULL ? sav->replay->wsize : 0);
3439 p->sadb_sa_state = sav->state;
3440 p->sadb_sa_auth = sav->alg_auth;
3441 p->sadb_sa_encrypt = sav->alg_enc;
3442 p->sadb_sa_flags = sav->flags;
3448 * set data into sadb_address.
3450 static struct mbuf *
3451 key_setsadbaddr(exttype, saddr, prefixlen, ul_proto)
3453 const struct sockaddr *saddr;
3458 struct sadb_address *p;
3461 len = PFKEY_ALIGN8(sizeof(struct sadb_address)) +
3462 PFKEY_ALIGN8(saddr->sa_len);
3463 m = key_alloc_mbuf(len);
3464 if (!m || m->m_next) { /*XXX*/
3470 p = mtod(m, struct sadb_address *);
3473 p->sadb_address_len = PFKEY_UNIT64(len);
3474 p->sadb_address_exttype = exttype;
3475 p->sadb_address_proto = ul_proto;
3476 if (prefixlen == FULLMASK) {
3477 switch (saddr->sa_family) {
3479 prefixlen = sizeof(struct in_addr) << 3;
3482 prefixlen = sizeof(struct in6_addr) << 3;
3488 p->sadb_address_prefixlen = prefixlen;
3489 p->sadb_address_reserved = 0;
3492 mtod(m, caddr_t) + PFKEY_ALIGN8(sizeof(struct sadb_address)),
3500 * set data into sadb_ident.
3502 static struct mbuf *
3503 key_setsadbident(exttype, idtype, string, stringlen, id)
3504 u_int16_t exttype, idtype;
3510 struct sadb_ident *p;
3513 len = PFKEY_ALIGN8(sizeof(struct sadb_ident)) + PFKEY_ALIGN8(stringlen);
3514 m = key_alloc_mbuf(len);
3515 if (!m || m->m_next) { /*XXX*/
3521 p = mtod(m, struct sadb_ident *);
3524 p->sadb_ident_len = PFKEY_UNIT64(len);
3525 p->sadb_ident_exttype = exttype;
3526 p->sadb_ident_type = idtype;
3527 p->sadb_ident_reserved = 0;
3528 p->sadb_ident_id = id;
3531 mtod(m, caddr_t) + PFKEY_ALIGN8(sizeof(struct sadb_ident)),
3539 * set data into sadb_x_sa2.
3541 static struct mbuf *
3542 key_setsadbxsa2(mode, seq, reqid)
3544 u_int32_t seq, reqid;
3547 struct sadb_x_sa2 *p;
3550 len = PFKEY_ALIGN8(sizeof(struct sadb_x_sa2));
3551 m = key_alloc_mbuf(len);
3552 if (!m || m->m_next) { /*XXX*/
3558 p = mtod(m, struct sadb_x_sa2 *);
3561 p->sadb_x_sa2_len = PFKEY_UNIT64(len);
3562 p->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
3563 p->sadb_x_sa2_mode = mode;
3564 p->sadb_x_sa2_reserved1 = 0;
3565 p->sadb_x_sa2_reserved2 = 0;
3566 p->sadb_x_sa2_sequence = seq;
3567 p->sadb_x_sa2_reqid = reqid;
3573 * set data into sadb_x_policy
3575 static struct mbuf *
3576 key_setsadbxpolicy(type, dir, id)
3582 struct sadb_x_policy *p;
3585 len = PFKEY_ALIGN8(sizeof(struct sadb_x_policy));
3586 m = key_alloc_mbuf(len);
3587 if (!m || m->m_next) { /*XXX*/
3593 p = mtod(m, struct sadb_x_policy *);
3596 p->sadb_x_policy_len = PFKEY_UNIT64(len);
3597 p->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3598 p->sadb_x_policy_type = type;
3599 p->sadb_x_policy_dir = dir;
3600 p->sadb_x_policy_id = id;
3607 * copy a buffer into the new buffer allocated.
3610 key_newbuf(src, len)
3616 KMALLOC(new, caddr_t, len);
3618 ipseclog((LOG_DEBUG, "key_newbuf: No more memory.\n"));
3621 bcopy(src, new, len);
3626 /* compare my own address
3627 * OUT: 1: true, i.e. my address.
3632 struct sockaddr *sa;
3635 struct sockaddr_in *sin;
3636 struct in_ifaddr *ia;
3641 panic("key_ismyaddr: NULL pointer is passed.\n");
3643 switch (sa->sa_family) {
3646 sin = (struct sockaddr_in *)sa;
3647 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
3648 if (sin->sin_family == ia->ia_addr.sin_family &&
3649 sin->sin_len == ia->ia_addr.sin_len &&
3650 sin->sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr)
3659 return key_ismyaddr6((struct sockaddr_in6 *)sa);
3668 * compare my own address for IPv6.
3671 * NOTE: derived ip6_input() in KAME. This is necessary to modify more.
3673 #include <netinet6/in6_var.h>
3677 struct sockaddr_in6 *sin6;
3679 struct in6_ifaddr *ia;
3680 struct in6_multi *in6m;
3682 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
3683 if (key_sockaddrcmp((struct sockaddr *)&sin6,
3684 (struct sockaddr *)&ia->ia_addr, 0) == 0)
3689 * XXX why do we care about multlicast here while we don't care
3690 * about IPv4 multicast??
3694 IN6_LOOKUP_MULTI(sin6->sin6_addr, ia->ia_ifp, in6m);
3699 /* loopback, just for safety */
3700 if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
3708 * compare two secasindex structure.
3709 * flag can specify to compare 2 saidxes.
3710 * compare two secasindex structure without both mode and reqid.
3711 * don't compare port.
3713 * saidx0: source, it can be in SAD.
3721 const struct secasindex *saidx0,
3722 const struct secasindex *saidx1,
3726 if (saidx0 == NULL && saidx1 == NULL)
3729 if (saidx0 == NULL || saidx1 == NULL)
3732 if (saidx0->proto != saidx1->proto)
3735 if (flag == CMP_EXACTLY) {
3736 if (saidx0->mode != saidx1->mode)
3738 if (saidx0->reqid != saidx1->reqid)
3740 if (bcmp(&saidx0->src, &saidx1->src, saidx0->src.sa.sa_len) != 0 ||
3741 bcmp(&saidx0->dst, &saidx1->dst, saidx0->dst.sa.sa_len) != 0)
3745 /* CMP_MODE_REQID, CMP_REQID, CMP_HEAD */
3746 if (flag == CMP_MODE_REQID
3747 ||flag == CMP_REQID) {
3749 * If reqid of SPD is non-zero, unique SA is required.
3750 * The result must be of same reqid in this case.
3752 if (saidx1->reqid != 0 && saidx0->reqid != saidx1->reqid)
3756 if (flag == CMP_MODE_REQID) {
3757 if (saidx0->mode != IPSEC_MODE_ANY
3758 && saidx0->mode != saidx1->mode)
3762 if (key_sockaddrcmp(&saidx0->src.sa, &saidx1->src.sa, 0) != 0) {
3765 if (key_sockaddrcmp(&saidx0->dst.sa, &saidx1->dst.sa, 0) != 0) {
3774 * compare two secindex structure exactly.
3776 * spidx0: source, it is often in SPD.
3777 * spidx1: object, it is often from PFKEY message.
3783 key_cmpspidx_exactly(
3784 struct secpolicyindex *spidx0,
3785 struct secpolicyindex *spidx1)
3788 if (spidx0 == NULL && spidx1 == NULL)
3791 if (spidx0 == NULL || spidx1 == NULL)
3794 if (spidx0->prefs != spidx1->prefs
3795 || spidx0->prefd != spidx1->prefd
3796 || spidx0->ul_proto != spidx1->ul_proto)
3799 return key_sockaddrcmp(&spidx0->src.sa, &spidx1->src.sa, 1) == 0 &&
3800 key_sockaddrcmp(&spidx0->dst.sa, &spidx1->dst.sa, 1) == 0;
3804 * compare two secindex structure with mask.
3806 * spidx0: source, it is often in SPD.
3807 * spidx1: object, it is often from IP header.
3813 key_cmpspidx_withmask(
3814 struct secpolicyindex *spidx0,
3815 struct secpolicyindex *spidx1)
3818 if (spidx0 == NULL && spidx1 == NULL)
3821 if (spidx0 == NULL || spidx1 == NULL)
3824 if (spidx0->src.sa.sa_family != spidx1->src.sa.sa_family ||
3825 spidx0->dst.sa.sa_family != spidx1->dst.sa.sa_family ||
3826 spidx0->src.sa.sa_len != spidx1->src.sa.sa_len ||
3827 spidx0->dst.sa.sa_len != spidx1->dst.sa.sa_len)
3830 /* if spidx.ul_proto == IPSEC_ULPROTO_ANY, ignore. */
3831 if (spidx0->ul_proto != (u_int16_t)IPSEC_ULPROTO_ANY
3832 && spidx0->ul_proto != spidx1->ul_proto)
3835 switch (spidx0->src.sa.sa_family) {
3837 if (spidx0->src.sin.sin_port != IPSEC_PORT_ANY
3838 && spidx0->src.sin.sin_port != spidx1->src.sin.sin_port)
3840 if (!key_bbcmp(&spidx0->src.sin.sin_addr,
3841 &spidx1->src.sin.sin_addr, spidx0->prefs))
3845 if (spidx0->src.sin6.sin6_port != IPSEC_PORT_ANY
3846 && spidx0->src.sin6.sin6_port != spidx1->src.sin6.sin6_port)
3849 * scope_id check. if sin6_scope_id is 0, we regard it
3850 * as a wildcard scope, which matches any scope zone ID.
3852 if (spidx0->src.sin6.sin6_scope_id &&
3853 spidx1->src.sin6.sin6_scope_id &&
3854 spidx0->src.sin6.sin6_scope_id != spidx1->src.sin6.sin6_scope_id)
3856 if (!key_bbcmp(&spidx0->src.sin6.sin6_addr,
3857 &spidx1->src.sin6.sin6_addr, spidx0->prefs))
3862 if (bcmp(&spidx0->src, &spidx1->src, spidx0->src.sa.sa_len) != 0)
3867 switch (spidx0->dst.sa.sa_family) {
3869 if (spidx0->dst.sin.sin_port != IPSEC_PORT_ANY
3870 && spidx0->dst.sin.sin_port != spidx1->dst.sin.sin_port)
3872 if (!key_bbcmp(&spidx0->dst.sin.sin_addr,
3873 &spidx1->dst.sin.sin_addr, spidx0->prefd))
3877 if (spidx0->dst.sin6.sin6_port != IPSEC_PORT_ANY
3878 && spidx0->dst.sin6.sin6_port != spidx1->dst.sin6.sin6_port)
3881 * scope_id check. if sin6_scope_id is 0, we regard it
3882 * as a wildcard scope, which matches any scope zone ID.
3884 if (spidx0->src.sin6.sin6_scope_id &&
3885 spidx1->src.sin6.sin6_scope_id &&
3886 spidx0->dst.sin6.sin6_scope_id != spidx1->dst.sin6.sin6_scope_id)
3888 if (!key_bbcmp(&spidx0->dst.sin6.sin6_addr,
3889 &spidx1->dst.sin6.sin6_addr, spidx0->prefd))
3894 if (bcmp(&spidx0->dst, &spidx1->dst, spidx0->dst.sa.sa_len) != 0)
3899 /* XXX Do we check other field ? e.g. flowinfo */
3904 /* returns 0 on match */
3907 const struct sockaddr *sa1,
3908 const struct sockaddr *sa2,
3914 #define satosin(s) ((const struct sockaddr_in *)s)
3918 #define satosin6(s) ((const struct sockaddr_in6 *)s)
3919 if (sa1->sa_family != sa2->sa_family || sa1->sa_len != sa2->sa_len)
3922 switch (sa1->sa_family) {
3924 if (sa1->sa_len != sizeof(struct sockaddr_in))
3926 if (satosin(sa1)->sin_addr.s_addr !=
3927 satosin(sa2)->sin_addr.s_addr) {
3930 if (port && satosin(sa1)->sin_port != satosin(sa2)->sin_port)
3934 if (sa1->sa_len != sizeof(struct sockaddr_in6))
3935 return 1; /*EINVAL*/
3936 if (satosin6(sa1)->sin6_scope_id !=
3937 satosin6(sa2)->sin6_scope_id) {
3940 if (!IN6_ARE_ADDR_EQUAL(&satosin6(sa1)->sin6_addr,
3941 &satosin6(sa2)->sin6_addr)) {
3945 satosin6(sa1)->sin6_port != satosin6(sa2)->sin6_port) {
3949 if (bcmp(sa1, sa2, sa1->sa_len) != 0)
3960 * compare two buffers with mask.
3964 * bits: Number of bits to compare
3970 key_bbcmp(const void *a1, const void *a2, u_int bits)
3972 const unsigned char *p1 = a1;
3973 const unsigned char *p2 = a2;
3975 /* XXX: This could be considerably faster if we compare a word
3976 * at a time, but it is complicated on LSB Endian machines */
3978 /* Handle null pointers */
3979 if (p1 == NULL || p2 == NULL)
3989 u_int8_t mask = ~((1<<(8-bits))-1);
3990 if ((*p1 & mask) != (*p2 & mask))
3993 return 1; /* Match! */
3998 * scanning SPD and SAD to check status for each entries,
3999 * and do to remove or to expire.
4000 * XXX: year 2038 problem may remain.
4003 key_timehandler(void *unused)
4007 time_t now = time_second;
4009 s = splnet(); /*called from softclock()*/
4013 struct secpolicy *sp, *nextsp;
4015 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
4016 for (sp = LIST_FIRST(&sptree[dir]);
4020 nextsp = LIST_NEXT(sp, chain);
4022 if (sp->state == IPSEC_SPSTATE_DEAD) {
4027 if (sp->lifetime == 0 && sp->validtime == 0)
4030 /* the deletion will occur next time */
4031 if ((sp->lifetime && now - sp->created > sp->lifetime)
4032 || (sp->validtime && now - sp->lastused > sp->validtime)) {
4033 sp->state = IPSEC_SPSTATE_DEAD;
4043 struct secashead *sah, *nextsah;
4044 struct secasvar *sav, *nextsav;
4046 for (sah = LIST_FIRST(&sahtree);
4050 nextsah = LIST_NEXT(sah, chain);
4052 /* if sah has been dead, then delete it and process next sah. */
4053 if (sah->state == SADB_SASTATE_DEAD) {
4058 /* if LARVAL entry doesn't become MATURE, delete it. */
4059 for (sav = LIST_FIRST(&sah->savtree[SADB_SASTATE_LARVAL]);
4063 nextsav = LIST_NEXT(sav, chain);
4065 if (now - sav->created > key_larval_lifetime) {
4071 * check MATURE entry to start to send expire message
4074 for (sav = LIST_FIRST(&sah->savtree[SADB_SASTATE_MATURE]);
4078 nextsav = LIST_NEXT(sav, chain);
4080 /* we don't need to check. */
4081 if (sav->lft_s == NULL)
4085 if (sav->lft_c == NULL) {
4086 ipseclog((LOG_DEBUG,"key_timehandler: "
4087 "There is no CURRENT time, why?\n"));
4091 /* check SOFT lifetime */
4092 if (sav->lft_s->sadb_lifetime_addtime != 0
4093 && now - sav->created > sav->lft_s->sadb_lifetime_addtime) {
4095 * check SA to be used whether or not.
4096 * when SA hasn't been used, delete it.
4098 if (sav->lft_c->sadb_lifetime_usetime == 0) {
4099 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4102 key_sa_chgstate(sav, SADB_SASTATE_DYING);
4104 * XXX If we keep to send expire
4105 * message in the status of
4106 * DYING. Do remove below code.
4111 /* check SOFT lifetime by bytes */
4113 * XXX I don't know the way to delete this SA
4114 * when new SA is installed. Caution when it's
4115 * installed too big lifetime by time.
4117 else if (sav->lft_s->sadb_lifetime_bytes != 0
4118 && sav->lft_s->sadb_lifetime_bytes < sav->lft_c->sadb_lifetime_bytes) {
4120 key_sa_chgstate(sav, SADB_SASTATE_DYING);
4122 * XXX If we keep to send expire
4123 * message in the status of
4124 * DYING. Do remove below code.
4130 /* check DYING entry to change status to DEAD. */
4131 for (sav = LIST_FIRST(&sah->savtree[SADB_SASTATE_DYING]);
4135 nextsav = LIST_NEXT(sav, chain);
4137 /* we don't need to check. */
4138 if (sav->lft_h == NULL)
4142 if (sav->lft_c == NULL) {
4143 ipseclog((LOG_DEBUG, "key_timehandler: "
4144 "There is no CURRENT time, why?\n"));
4148 if (sav->lft_h->sadb_lifetime_addtime != 0
4149 && now - sav->created > sav->lft_h->sadb_lifetime_addtime) {
4150 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4153 #if 0 /* XXX Should we keep to send expire message until HARD lifetime ? */
4154 else if (sav->lft_s != NULL
4155 && sav->lft_s->sadb_lifetime_addtime != 0
4156 && now - sav->created > sav->lft_s->sadb_lifetime_addtime) {
4158 * XXX: should be checked to be
4159 * installed the valid SA.
4163 * If there is no SA then sending
4169 /* check HARD lifetime by bytes */
4170 else if (sav->lft_h->sadb_lifetime_bytes != 0
4171 && sav->lft_h->sadb_lifetime_bytes < sav->lft_c->sadb_lifetime_bytes) {
4172 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4177 /* delete entry in DEAD */
4178 for (sav = LIST_FIRST(&sah->savtree[SADB_SASTATE_DEAD]);
4182 nextsav = LIST_NEXT(sav, chain);
4185 if (sav->state != SADB_SASTATE_DEAD) {
4186 ipseclog((LOG_DEBUG, "key_timehandler: "
4187 "invalid sav->state "
4188 "(queue: %d SA: %d): "
4190 SADB_SASTATE_DEAD, sav->state));
4194 * do not call key_freesav() here.
4195 * sav should already be freed, and sav->refcnt
4196 * shows other references to sav
4197 * (such as from SPD).
4203 #ifndef IPSEC_NONBLOCK_ACQUIRE
4206 struct secacq *acq, *nextacq;
4208 for (acq = LIST_FIRST(&acqtree);
4212 nextacq = LIST_NEXT(acq, chain);
4214 if (now - acq->created > key_blockacq_lifetime
4215 && __LIST_CHAINED(acq)) {
4216 LIST_REMOVE(acq, chain);
4225 struct secspacq *acq, *nextacq;
4227 for (acq = LIST_FIRST(&spacqtree);
4231 nextacq = LIST_NEXT(acq, chain);
4233 if (now - acq->created > key_blockacq_lifetime
4234 && __LIST_CHAINED(acq)) {
4235 LIST_REMOVE(acq, chain);
4241 /* initialize random seed */
4242 if (key_tick_init_random++ > key_int_random) {
4243 key_tick_init_random = 0;
4247 #ifndef IPSEC_DEBUG2
4248 /* do exchange to tick time !! */
4249 callout_reset(&key_timehandler_ch, hz, key_timehandler, NULL);
4250 #endif /* IPSEC_DEBUG2 */
4257 * to initialize a seed for random()
4262 srandom(time_second);
4270 key_randomfill(&value, sizeof(value));
4275 key_randomfill(p, l)
4281 static int warn = 1;
4284 n = (size_t)read_random(p, (u_int)l);
4288 bcopy(&v, (u_int8_t *)p + n,
4289 l - n < sizeof(v) ? l - n : sizeof(v));
4293 printf("WARNING: pseudo-random number generator "
4294 "used for IPsec processing\n");
4301 * map SADB_SATYPE_* to IPPROTO_*.
4302 * if satype == SADB_SATYPE then satype is mapped to ~0.
4304 * 0: invalid satype.
4307 key_satype2proto(satype)
4311 case SADB_SATYPE_UNSPEC:
4312 return IPSEC_PROTO_ANY;
4313 case SADB_SATYPE_AH:
4315 case SADB_SATYPE_ESP:
4317 case SADB_X_SATYPE_IPCOMP:
4318 return IPPROTO_IPCOMP;
4326 * map IPPROTO_* to SADB_SATYPE_*
4328 * 0: invalid protocol type.
4331 key_proto2satype(proto)
4336 return SADB_SATYPE_AH;
4338 return SADB_SATYPE_ESP;
4339 case IPPROTO_IPCOMP:
4340 return SADB_X_SATYPE_IPCOMP;
4349 * SADB_GETSPI processing is to receive
4350 * <base, (SA2), src address, dst address, (SPI range)>
4351 * from the IKMPd, to assign a unique spi value, to hang on the INBOUND
4352 * tree with the status of LARVAL, and send
4353 * <base, SA(*), address(SD)>
4356 * IN: mhp: pointer to the pointer to each header.
4357 * OUT: NULL if fail.
4358 * other if success, return pointer to the message to send.
4361 key_getspi(so, m, mhp)
4364 const struct sadb_msghdr *mhp;
4366 struct sadb_address *src0, *dst0;
4367 struct secasindex saidx;
4368 struct secashead *newsah;
4369 struct secasvar *newsav;
4377 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
4378 panic("key_getspi: NULL pointer is passed.\n");
4380 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
4381 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
4382 ipseclog((LOG_DEBUG, "key_getspi: invalid message is passed.\n"));
4383 return key_senderror(so, m, EINVAL);
4385 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
4386 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
4387 ipseclog((LOG_DEBUG, "key_getspi: invalid message is passed.\n"));
4388 return key_senderror(so, m, EINVAL);
4390 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
4391 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
4392 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
4394 mode = IPSEC_MODE_ANY;
4398 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
4399 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
4401 /* map satype to proto */
4402 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
4403 ipseclog((LOG_DEBUG, "key_getspi: invalid satype is passed.\n"));
4404 return key_senderror(so, m, EINVAL);
4407 /* make sure if port number is zero. */
4408 switch (((struct sockaddr *)(src0 + 1))->sa_family) {
4410 if (((struct sockaddr *)(src0 + 1))->sa_len !=
4411 sizeof(struct sockaddr_in))
4412 return key_senderror(so, m, EINVAL);
4413 ((struct sockaddr_in *)(src0 + 1))->sin_port = 0;
4416 if (((struct sockaddr *)(src0 + 1))->sa_len !=
4417 sizeof(struct sockaddr_in6))
4418 return key_senderror(so, m, EINVAL);
4419 ((struct sockaddr_in6 *)(src0 + 1))->sin6_port = 0;
4424 switch (((struct sockaddr *)(dst0 + 1))->sa_family) {
4426 if (((struct sockaddr *)(dst0 + 1))->sa_len !=
4427 sizeof(struct sockaddr_in))
4428 return key_senderror(so, m, EINVAL);
4429 ((struct sockaddr_in *)(dst0 + 1))->sin_port = 0;
4432 if (((struct sockaddr *)(dst0 + 1))->sa_len !=
4433 sizeof(struct sockaddr_in6))
4434 return key_senderror(so, m, EINVAL);
4435 ((struct sockaddr_in6 *)(dst0 + 1))->sin6_port = 0;
4441 /* XXX boundary check against sa_len */
4442 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
4444 /* SPI allocation */
4445 spi = key_do_getnewspi((struct sadb_spirange *)mhp->ext[SADB_EXT_SPIRANGE],
4448 return key_senderror(so, m, EINVAL);
4450 /* get a SA index */
4451 if ((newsah = key_getsah(&saidx)) == NULL) {
4452 /* create a new SA index */
4453 if ((newsah = key_newsah(&saidx)) == NULL) {
4454 ipseclog((LOG_DEBUG, "key_getspi: No more memory.\n"));
4455 return key_senderror(so, m, ENOBUFS);
4461 newsav = KEY_NEWSAV(m, mhp, newsah, &error);
4462 if (newsav == NULL) {
4463 /* XXX don't free new SA index allocated in above. */
4464 return key_senderror(so, m, error);
4468 newsav->spi = htonl(spi);
4470 #ifndef IPSEC_NONBLOCK_ACQUIRE
4471 /* delete the entry in acqtree */
4472 if (mhp->msg->sadb_msg_seq != 0) {
4474 if ((acq = key_getacqbyseq(mhp->msg->sadb_msg_seq)) != NULL) {
4475 /* reset counter in order to deletion by timehandler. */
4476 acq->created = time_second;
4483 struct mbuf *n, *nn;
4484 struct sadb_sa *m_sa;
4485 struct sadb_msg *newmsg;
4488 /* create new sadb_msg to reply. */
4489 len = PFKEY_ALIGN8(sizeof(struct sadb_msg)) +
4490 PFKEY_ALIGN8(sizeof(struct sadb_sa));
4492 return key_senderror(so, m, ENOBUFS);
4494 MGETHDR(n, MB_DONTWAIT, MT_DATA);
4496 MCLGET(n, MB_DONTWAIT);
4497 if ((n->m_flags & M_EXT) == 0) {
4503 return key_senderror(so, m, ENOBUFS);
4509 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
4510 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
4512 m_sa = (struct sadb_sa *)(mtod(n, caddr_t) + off);
4513 m_sa->sadb_sa_len = PFKEY_UNIT64(sizeof(struct sadb_sa));
4514 m_sa->sadb_sa_exttype = SADB_EXT_SA;
4515 m_sa->sadb_sa_spi = htonl(spi);
4516 off += PFKEY_ALIGN8(sizeof(struct sadb_sa));
4520 panic("length inconsistency in key_getspi");
4523 n->m_next = key_gather_mbuf(m, mhp, 0, 2, SADB_EXT_ADDRESS_SRC,
4524 SADB_EXT_ADDRESS_DST);
4527 return key_senderror(so, m, ENOBUFS);
4530 if (n->m_len < sizeof(struct sadb_msg)) {
4531 n = m_pullup(n, sizeof(struct sadb_msg));
4533 return key_sendup_mbuf(so, m, KEY_SENDUP_ONE);
4536 n->m_pkthdr.len = 0;
4537 for (nn = n; nn; nn = nn->m_next)
4538 n->m_pkthdr.len += nn->m_len;
4540 newmsg = mtod(n, struct sadb_msg *);
4541 newmsg->sadb_msg_seq = newsav->seq;
4542 newmsg->sadb_msg_errno = 0;
4543 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
4546 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
4551 * allocating new SPI
4552 * called by key_getspi().
4558 key_do_getnewspi(spirange, saidx)
4559 struct sadb_spirange *spirange;
4560 struct secasindex *saidx;
4564 int count = key_spi_trycnt;
4566 /* set spi range to allocate */
4567 if (spirange != NULL) {
4568 min = spirange->sadb_spirange_min;
4569 max = spirange->sadb_spirange_max;
4571 min = key_spi_minval;
4572 max = key_spi_maxval;
4574 /* IPCOMP needs 2-byte SPI */
4575 if (saidx->proto == IPPROTO_IPCOMP) {
4582 t = min; min = max; max = t;
4587 if (key_checkspidup(saidx, min) != NULL) {
4588 ipseclog((LOG_DEBUG, "key_do_getnewspi: SPI %u exists already.\n", min));
4592 count--; /* taking one cost. */
4600 /* when requesting to allocate spi ranged */
4602 /* generate pseudo-random SPI value ranged. */
4603 newspi = min + (key_random() % (max - min + 1));
4605 if (key_checkspidup(saidx, newspi) == NULL)
4609 if (count == 0 || newspi == 0) {
4610 ipseclog((LOG_DEBUG, "key_do_getnewspi: to allocate spi is failed.\n"));
4616 keystat.getspi_count =
4617 (keystat.getspi_count + key_spi_trycnt - count) / 2;
4623 * SADB_UPDATE processing
4625 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4626 * key(AE), (identity(SD),) (sensitivity)>
4627 * from the ikmpd, and update a secasvar entry whose status is SADB_SASTATE_LARVAL.
4629 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4630 * (identity(SD),) (sensitivity)>
4633 * m will always be freed.
4636 key_update(so, m, mhp)
4639 const struct sadb_msghdr *mhp;
4641 struct sadb_sa *sa0;
4642 struct sadb_address *src0, *dst0;
4643 struct secasindex saidx;
4644 struct secashead *sah;
4645 struct secasvar *sav;
4652 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
4653 panic("key_update: NULL pointer is passed.\n");
4655 /* map satype to proto */
4656 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
4657 ipseclog((LOG_DEBUG, "key_update: invalid satype is passed.\n"));
4658 return key_senderror(so, m, EINVAL);
4661 if (mhp->ext[SADB_EXT_SA] == NULL ||
4662 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
4663 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
4664 (mhp->msg->sadb_msg_satype == SADB_SATYPE_ESP &&
4665 mhp->ext[SADB_EXT_KEY_ENCRYPT] == NULL) ||
4666 (mhp->msg->sadb_msg_satype == SADB_SATYPE_AH &&
4667 mhp->ext[SADB_EXT_KEY_AUTH] == NULL) ||
4668 (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL &&
4669 mhp->ext[SADB_EXT_LIFETIME_SOFT] == NULL) ||
4670 (mhp->ext[SADB_EXT_LIFETIME_HARD] == NULL &&
4671 mhp->ext[SADB_EXT_LIFETIME_SOFT] != NULL)) {
4672 ipseclog((LOG_DEBUG, "key_update: invalid message is passed.\n"));
4673 return key_senderror(so, m, EINVAL);
4675 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
4676 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
4677 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
4678 ipseclog((LOG_DEBUG, "key_update: invalid message is passed.\n"));
4679 return key_senderror(so, m, EINVAL);
4681 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
4682 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
4683 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
4685 mode = IPSEC_MODE_ANY;
4688 /* XXX boundary checking for other extensions */
4690 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
4691 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
4692 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
4694 /* XXX boundary check against sa_len */
4695 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
4697 /* get a SA header */
4698 if ((sah = key_getsah(&saidx)) == NULL) {
4699 ipseclog((LOG_DEBUG, "key_update: no SA index found.\n"));
4700 return key_senderror(so, m, ENOENT);
4703 /* set spidx if there */
4705 error = key_setident(sah, m, mhp);
4707 return key_senderror(so, m, error);
4709 /* find a SA with sequence number. */
4710 #ifdef IPSEC_DOSEQCHECK
4711 if (mhp->msg->sadb_msg_seq != 0
4712 && (sav = key_getsavbyseq(sah, mhp->msg->sadb_msg_seq)) == NULL) {
4713 ipseclog((LOG_DEBUG,
4714 "key_update: no larval SA with sequence %u exists.\n",
4715 mhp->msg->sadb_msg_seq));
4716 return key_senderror(so, m, ENOENT);
4719 if ((sav = key_getsavbyspi(sah, sa0->sadb_sa_spi)) == NULL) {
4720 ipseclog((LOG_DEBUG,
4721 "key_update: no such a SA found (spi:%u)\n",
4722 (u_int32_t)ntohl(sa0->sadb_sa_spi)));
4723 return key_senderror(so, m, EINVAL);
4727 /* validity check */
4728 if (sav->sah->saidx.proto != proto) {
4729 ipseclog((LOG_DEBUG,
4730 "key_update: protocol mismatched (DB=%u param=%u)\n",
4731 sav->sah->saidx.proto, proto));
4732 return key_senderror(so, m, EINVAL);
4734 #ifdef IPSEC_DOSEQCHECK
4735 if (sav->spi != sa0->sadb_sa_spi) {
4736 ipseclog((LOG_DEBUG,
4737 "key_update: SPI mismatched (DB:%u param:%u)\n",
4738 (u_int32_t)ntohl(sav->spi),
4739 (u_int32_t)ntohl(sa0->sadb_sa_spi)));
4740 return key_senderror(so, m, EINVAL);
4743 if (sav->pid != mhp->msg->sadb_msg_pid) {
4744 ipseclog((LOG_DEBUG,
4745 "key_update: pid mismatched (DB:%u param:%u)\n",
4746 sav->pid, mhp->msg->sadb_msg_pid));
4747 return key_senderror(so, m, EINVAL);
4750 /* copy sav values */
4751 error = key_setsaval(sav, m, mhp);
4754 return key_senderror(so, m, error);
4757 /* check SA values to be mature. */
4758 if ((mhp->msg->sadb_msg_errno = key_mature(sav)) != 0) {
4760 return key_senderror(so, m, 0);
4766 /* set msg buf from mhp */
4767 n = key_getmsgbuf_x1(m, mhp);
4769 ipseclog((LOG_DEBUG, "key_update: No more memory.\n"));
4770 return key_senderror(so, m, ENOBUFS);
4774 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
4779 * search SAD with sequence for a SA which state is SADB_SASTATE_LARVAL.
4780 * only called by key_update().
4783 * others : found, pointer to a SA.
4785 #ifdef IPSEC_DOSEQCHECK
4786 static struct secasvar *
4787 key_getsavbyseq(sah, seq)
4788 struct secashead *sah;
4791 struct secasvar *sav;
4794 state = SADB_SASTATE_LARVAL;
4796 /* search SAD with sequence number ? */
4797 LIST_FOREACH(sav, &sah->savtree[state], chain) {
4799 KEY_CHKSASTATE(state, sav->state, "key_getsabyseq");
4801 if (sav->seq == seq) {
4803 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
4804 printf("DP key_getsavbyseq cause "
4805 "refcnt++:%d SA:%p\n",
4816 * SADB_ADD processing
4817 * add an entry to SA database, when received
4818 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4819 * key(AE), (identity(SD),) (sensitivity)>
4822 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4823 * (identity(SD),) (sensitivity)>
4826 * IGNORE identity and sensitivity messages.
4828 * m will always be freed.
4834 const struct sadb_msghdr *mhp;
4836 struct sadb_sa *sa0;
4837 struct sadb_address *src0, *dst0;
4838 struct secasindex saidx;
4839 struct secashead *newsah;
4840 struct secasvar *newsav;
4847 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
4848 panic("key_add: NULL pointer is passed.\n");
4850 /* map satype to proto */
4851 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
4852 ipseclog((LOG_DEBUG, "key_add: invalid satype is passed.\n"));
4853 return key_senderror(so, m, EINVAL);
4856 if (mhp->ext[SADB_EXT_SA] == NULL ||
4857 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
4858 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
4859 (mhp->msg->sadb_msg_satype == SADB_SATYPE_ESP &&
4860 mhp->ext[SADB_EXT_KEY_ENCRYPT] == NULL) ||
4861 (mhp->msg->sadb_msg_satype == SADB_SATYPE_AH &&
4862 mhp->ext[SADB_EXT_KEY_AUTH] == NULL) ||
4863 (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL &&
4864 mhp->ext[SADB_EXT_LIFETIME_SOFT] == NULL) ||
4865 (mhp->ext[SADB_EXT_LIFETIME_HARD] == NULL &&
4866 mhp->ext[SADB_EXT_LIFETIME_SOFT] != NULL)) {
4867 ipseclog((LOG_DEBUG, "key_add: invalid message is passed.\n"));
4868 return key_senderror(so, m, EINVAL);
4870 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
4871 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
4872 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
4874 ipseclog((LOG_DEBUG, "key_add: invalid message is passed.\n"));
4875 return key_senderror(so, m, EINVAL);
4877 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
4878 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
4879 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
4881 mode = IPSEC_MODE_ANY;
4885 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
4886 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
4887 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
4889 /* XXX boundary check against sa_len */
4890 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
4892 /* get a SA header */
4893 if ((newsah = key_getsah(&saidx)) == NULL) {
4894 /* create a new SA header */
4895 if ((newsah = key_newsah(&saidx)) == NULL) {
4896 ipseclog((LOG_DEBUG, "key_add: No more memory.\n"));
4897 return key_senderror(so, m, ENOBUFS);
4901 /* set spidx if there */
4903 error = key_setident(newsah, m, mhp);
4905 return key_senderror(so, m, error);
4908 /* create new SA entry. */
4909 /* We can create new SA only if SPI is differenct. */
4910 if (key_getsavbyspi(newsah, sa0->sadb_sa_spi)) {
4911 ipseclog((LOG_DEBUG, "key_add: SA already exists.\n"));
4912 return key_senderror(so, m, EEXIST);
4914 newsav = KEY_NEWSAV(m, mhp, newsah, &error);
4915 if (newsav == NULL) {
4916 return key_senderror(so, m, error);
4919 /* check SA values to be mature. */
4920 if ((error = key_mature(newsav)) != 0) {
4921 KEY_FREESAV(&newsav);
4922 return key_senderror(so, m, error);
4926 * don't call key_freesav() here, as we would like to keep the SA
4927 * in the database on success.
4933 /* set msg buf from mhp */
4934 n = key_getmsgbuf_x1(m, mhp);
4936 ipseclog((LOG_DEBUG, "key_update: No more memory.\n"));
4937 return key_senderror(so, m, ENOBUFS);
4941 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
4947 key_setident(sah, m, mhp)
4948 struct secashead *sah;
4950 const struct sadb_msghdr *mhp;
4952 const struct sadb_ident *idsrc, *iddst;
4953 int idsrclen, iddstlen;
4956 if (sah == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
4957 panic("key_setident: NULL pointer is passed.\n");
4959 /* don't make buffer if not there */
4960 if (mhp->ext[SADB_EXT_IDENTITY_SRC] == NULL &&
4961 mhp->ext[SADB_EXT_IDENTITY_DST] == NULL) {
4967 if (mhp->ext[SADB_EXT_IDENTITY_SRC] == NULL ||
4968 mhp->ext[SADB_EXT_IDENTITY_DST] == NULL) {
4969 ipseclog((LOG_DEBUG, "key_setident: invalid identity.\n"));
4973 idsrc = (const struct sadb_ident *)mhp->ext[SADB_EXT_IDENTITY_SRC];
4974 iddst = (const struct sadb_ident *)mhp->ext[SADB_EXT_IDENTITY_DST];
4975 idsrclen = mhp->extlen[SADB_EXT_IDENTITY_SRC];
4976 iddstlen = mhp->extlen[SADB_EXT_IDENTITY_DST];
4978 /* validity check */
4979 if (idsrc->sadb_ident_type != iddst->sadb_ident_type) {
4980 ipseclog((LOG_DEBUG, "key_setident: ident type mismatch.\n"));
4984 switch (idsrc->sadb_ident_type) {
4985 case SADB_IDENTTYPE_PREFIX:
4986 case SADB_IDENTTYPE_FQDN:
4987 case SADB_IDENTTYPE_USERFQDN:
4989 /* XXX do nothing */
4995 /* make structure */
4996 KMALLOC(sah->idents, struct sadb_ident *, idsrclen);
4997 if (sah->idents == NULL) {
4998 ipseclog((LOG_DEBUG, "key_setident: No more memory.\n"));
5001 KMALLOC(sah->identd, struct sadb_ident *, iddstlen);
5002 if (sah->identd == NULL) {
5005 ipseclog((LOG_DEBUG, "key_setident: No more memory.\n"));
5008 bcopy(idsrc, sah->idents, idsrclen);
5009 bcopy(iddst, sah->identd, iddstlen);
5015 * m will not be freed on return.
5016 * it is caller's responsibility to free the result.
5018 static struct mbuf *
5019 key_getmsgbuf_x1(m, mhp)
5021 const struct sadb_msghdr *mhp;
5026 if (m == NULL || mhp == NULL || mhp->msg == NULL)
5027 panic("key_getmsgbuf_x1: NULL pointer is passed.\n");
5029 /* create new sadb_msg to reply. */
5030 n = key_gather_mbuf(m, mhp, 1, 9, SADB_EXT_RESERVED,
5031 SADB_EXT_SA, SADB_X_EXT_SA2,
5032 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST,
5033 SADB_EXT_LIFETIME_HARD, SADB_EXT_LIFETIME_SOFT,
5034 SADB_EXT_IDENTITY_SRC, SADB_EXT_IDENTITY_DST);
5038 if (n->m_len < sizeof(struct sadb_msg)) {
5039 n = m_pullup(n, sizeof(struct sadb_msg));
5043 mtod(n, struct sadb_msg *)->sadb_msg_errno = 0;
5044 mtod(n, struct sadb_msg *)->sadb_msg_len =
5045 PFKEY_UNIT64(n->m_pkthdr.len);
5050 static int key_delete_all (struct socket *, struct mbuf *,
5051 const struct sadb_msghdr *, u_int16_t);
5054 * SADB_DELETE processing
5056 * <base, SA(*), address(SD)>
5057 * from the ikmpd, and set SADB_SASTATE_DEAD,
5059 * <base, SA(*), address(SD)>
5062 * m will always be freed.
5065 key_delete(so, m, mhp)
5068 const struct sadb_msghdr *mhp;
5070 struct sadb_sa *sa0;
5071 struct sadb_address *src0, *dst0;
5072 struct secasindex saidx;
5073 struct secashead *sah;
5074 struct secasvar *sav = NULL;
5078 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
5079 panic("key_delete: NULL pointer is passed.\n");
5081 /* map satype to proto */
5082 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5083 ipseclog((LOG_DEBUG, "key_delete: invalid satype is passed.\n"));
5084 return key_senderror(so, m, EINVAL);
5087 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5088 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
5089 ipseclog((LOG_DEBUG, "key_delete: invalid message is passed.\n"));
5090 return key_senderror(so, m, EINVAL);
5093 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5094 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5095 ipseclog((LOG_DEBUG, "key_delete: invalid message is passed.\n"));
5096 return key_senderror(so, m, EINVAL);
5099 if (mhp->ext[SADB_EXT_SA] == NULL) {
5101 * Caller wants us to delete all non-LARVAL SAs
5102 * that match the src/dst. This is used during
5103 * IKE INITIAL-CONTACT.
5105 ipseclog((LOG_DEBUG, "key_delete: doing delete all.\n"));
5106 return key_delete_all(so, m, mhp, proto);
5107 } else if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa)) {
5108 ipseclog((LOG_DEBUG, "key_delete: invalid message is passed.\n"));
5109 return key_senderror(so, m, EINVAL);
5112 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5113 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
5114 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
5116 /* XXX boundary check against sa_len */
5117 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5119 /* get a SA header */
5120 LIST_FOREACH(sah, &sahtree, chain) {
5121 if (sah->state == SADB_SASTATE_DEAD)
5123 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5126 /* get a SA with SPI. */
5127 sav = key_getsavbyspi(sah, sa0->sadb_sa_spi);
5132 ipseclog((LOG_DEBUG, "key_delete: no SA found.\n"));
5133 return key_senderror(so, m, ENOENT);
5136 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
5141 struct sadb_msg *newmsg;
5143 /* create new sadb_msg to reply. */
5144 n = key_gather_mbuf(m, mhp, 1, 4, SADB_EXT_RESERVED,
5145 SADB_EXT_SA, SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
5147 return key_senderror(so, m, ENOBUFS);
5149 if (n->m_len < sizeof(struct sadb_msg)) {
5150 n = m_pullup(n, sizeof(struct sadb_msg));
5152 return key_senderror(so, m, ENOBUFS);
5154 newmsg = mtod(n, struct sadb_msg *);
5155 newmsg->sadb_msg_errno = 0;
5156 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
5159 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5164 * delete all SAs for src/dst. Called from key_delete().
5167 key_delete_all(so, m, mhp, proto)
5170 const struct sadb_msghdr *mhp;
5173 struct sadb_address *src0, *dst0;
5174 struct secasindex saidx;
5175 struct secashead *sah;
5176 struct secasvar *sav, *nextsav;
5177 u_int stateidx, state;
5179 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
5180 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
5182 /* XXX boundary check against sa_len */
5183 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5185 LIST_FOREACH(sah, &sahtree, chain) {
5186 if (sah->state == SADB_SASTATE_DEAD)
5188 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5191 /* Delete all non-LARVAL SAs. */
5193 stateidx < _ARRAYLEN(saorder_state_alive);
5195 state = saorder_state_alive[stateidx];
5196 if (state == SADB_SASTATE_LARVAL)
5198 for (sav = LIST_FIRST(&sah->savtree[state]);
5199 sav != NULL; sav = nextsav) {
5200 nextsav = LIST_NEXT(sav, chain);
5202 if (sav->state != state) {
5203 ipseclog((LOG_DEBUG, "key_delete_all: "
5204 "invalid sav->state "
5205 "(queue: %d SA: %d)\n",
5206 state, sav->state));
5210 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
5217 struct sadb_msg *newmsg;
5219 /* create new sadb_msg to reply. */
5220 n = key_gather_mbuf(m, mhp, 1, 3, SADB_EXT_RESERVED,
5221 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
5223 return key_senderror(so, m, ENOBUFS);
5225 if (n->m_len < sizeof(struct sadb_msg)) {
5226 n = m_pullup(n, sizeof(struct sadb_msg));
5228 return key_senderror(so, m, ENOBUFS);
5230 newmsg = mtod(n, struct sadb_msg *);
5231 newmsg->sadb_msg_errno = 0;
5232 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
5235 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5240 * SADB_GET processing
5242 * <base, SA(*), address(SD)>
5243 * from the ikmpd, and get a SP and a SA to respond,
5245 * <base, SA, (lifetime(HSC),) address(SD), (address(P),) key(AE),
5246 * (identity(SD),) (sensitivity)>
5249 * m will always be freed.
5255 const struct sadb_msghdr *mhp;
5257 struct sadb_sa *sa0;
5258 struct sadb_address *src0, *dst0;
5259 struct secasindex saidx;
5260 struct secashead *sah;
5261 struct secasvar *sav = NULL;
5265 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
5266 panic("key_get: NULL pointer is passed.\n");
5268 /* map satype to proto */
5269 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5270 ipseclog((LOG_DEBUG, "key_get: invalid satype is passed.\n"));
5271 return key_senderror(so, m, EINVAL);
5274 if (mhp->ext[SADB_EXT_SA] == NULL ||
5275 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5276 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
5277 ipseclog((LOG_DEBUG, "key_get: invalid message is passed.\n"));
5278 return key_senderror(so, m, EINVAL);
5280 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
5281 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5282 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5283 ipseclog((LOG_DEBUG, "key_get: invalid message is passed.\n"));
5284 return key_senderror(so, m, EINVAL);
5287 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5288 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
5289 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
5291 /* XXX boundary check against sa_len */
5292 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5294 /* get a SA header */
5295 LIST_FOREACH(sah, &sahtree, chain) {
5296 if (sah->state == SADB_SASTATE_DEAD)
5298 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5301 /* get a SA with SPI. */
5302 sav = key_getsavbyspi(sah, sa0->sadb_sa_spi);
5307 ipseclog((LOG_DEBUG, "key_get: no SA found.\n"));
5308 return key_senderror(so, m, ENOENT);
5315 /* map proto to satype */
5316 if ((satype = key_proto2satype(sah->saidx.proto)) == 0) {
5317 ipseclog((LOG_DEBUG, "key_get: there was invalid proto in SAD.\n"));
5318 return key_senderror(so, m, EINVAL);
5321 /* create new sadb_msg to reply. */
5322 n = key_setdumpsa(sav, SADB_GET, satype, mhp->msg->sadb_msg_seq,
5323 mhp->msg->sadb_msg_pid);
5325 return key_senderror(so, m, ENOBUFS);
5328 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
5332 /* XXX make it sysctl-configurable? */
5334 key_getcomb_setlifetime(comb)
5335 struct sadb_comb *comb;
5338 comb->sadb_comb_soft_allocations = 1;
5339 comb->sadb_comb_hard_allocations = 1;
5340 comb->sadb_comb_soft_bytes = 0;
5341 comb->sadb_comb_hard_bytes = 0;
5342 comb->sadb_comb_hard_addtime = 86400; /* 1 day */
5343 comb->sadb_comb_soft_addtime = comb->sadb_comb_soft_addtime * 80 / 100;
5344 comb->sadb_comb_soft_usetime = 28800; /* 8 hours */
5345 comb->sadb_comb_hard_usetime = comb->sadb_comb_hard_usetime * 80 / 100;
5349 * XXX reorder combinations by preference
5350 * XXX no idea if the user wants ESP authentication or not
5352 static struct mbuf *
5355 struct sadb_comb *comb;
5356 struct enc_xform *algo;
5357 struct mbuf *result = NULL, *m, *n;
5361 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
5364 for (i = 1; i <= SADB_EALG_MAX; i++) {
5365 algo = esp_algorithm_lookup(i);
5369 /* discard algorithms with key size smaller than system min */
5370 if (_BITS(algo->maxkey) < ipsec_esp_keymin)
5372 if (_BITS(algo->minkey) < ipsec_esp_keymin)
5373 encmin = ipsec_esp_keymin;
5375 encmin = _BITS(algo->minkey);
5378 m = key_getcomb_ah();
5381 ("key_getcomb_esp: l=%u > MLEN=%lu",
5383 MGET(m, MB_DONTWAIT, MT_DATA);
5388 bzero(mtod(m, caddr_t), m->m_len);
5395 for (n = m; n; n = n->m_next)
5397 KASSERT((totlen % l) == 0,
5398 ("key_getcomb_esp: totlen=%u, l=%u", totlen, l));
5400 for (off = 0; off < totlen; off += l) {
5401 n = m_pulldown(m, off, l, &o);
5403 /* m is already freed */
5406 comb = (struct sadb_comb *)(mtod(n, caddr_t) + o);
5407 bzero(comb, sizeof(*comb));
5408 key_getcomb_setlifetime(comb);
5409 comb->sadb_comb_encrypt = i;
5410 comb->sadb_comb_encrypt_minbits = encmin;
5411 comb->sadb_comb_encrypt_maxbits = _BITS(algo->maxkey);
5430 const struct auth_hash *ah,
5435 *min = *max = ah->keysize;
5436 if (ah->keysize == 0) {
5438 * Transform takes arbitrary key size but algorithm
5439 * key size is restricted. Enforce this here.
5442 case SADB_X_AALG_MD5: *min = *max = 16; break;
5443 case SADB_X_AALG_SHA: *min = *max = 20; break;
5444 case SADB_X_AALG_NULL: *min = 1; *max = 256; break;
5446 DPRINTF(("key_getsizes_ah: unknown AH algorithm %u\n",
5454 * XXX reorder combinations by preference
5456 static struct mbuf *
5459 struct sadb_comb *comb;
5460 struct auth_hash *algo;
5462 u_int16_t minkeysize, maxkeysize;
5464 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
5467 for (i = 1; i <= SADB_AALG_MAX; i++) {
5469 /* we prefer HMAC algorithms, not old algorithms */
5470 if (i != SADB_AALG_SHA1HMAC && i != SADB_AALG_MD5HMAC)
5473 algo = ah_algorithm_lookup(i);
5476 key_getsizes_ah(algo, i, &minkeysize, &maxkeysize);
5477 /* discard algorithms with key size smaller than system min */
5478 if (_BITS(minkeysize) < ipsec_ah_keymin)
5483 ("key_getcomb_ah: l=%u > MLEN=%lu",
5485 MGET(m, MB_DONTWAIT, MT_DATA);
5492 M_PREPEND(m, l, MB_DONTWAIT);
5496 comb = mtod(m, struct sadb_comb *);
5497 bzero(comb, sizeof(*comb));
5498 key_getcomb_setlifetime(comb);
5499 comb->sadb_comb_auth = i;
5500 comb->sadb_comb_auth_minbits = _BITS(minkeysize);
5501 comb->sadb_comb_auth_maxbits = _BITS(maxkeysize);
5508 * not really an official behavior. discussed in pf_key@inner.net in Sep2000.
5509 * XXX reorder combinations by preference
5511 static struct mbuf *
5512 key_getcomb_ipcomp()
5514 struct sadb_comb *comb;
5515 struct comp_algo *algo;
5518 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
5521 for (i = 1; i <= SADB_X_CALG_MAX; i++) {
5522 algo = ipcomp_algorithm_lookup(i);
5528 ("key_getcomb_ipcomp: l=%u > MLEN=%lu",
5530 MGET(m, MB_DONTWAIT, MT_DATA);
5537 M_PREPEND(m, l, MB_DONTWAIT);
5541 comb = mtod(m, struct sadb_comb *);
5542 bzero(comb, sizeof(*comb));
5543 key_getcomb_setlifetime(comb);
5544 comb->sadb_comb_encrypt = i;
5545 /* what should we set into sadb_comb_*_{min,max}bits? */
5552 * XXX no way to pass mode (transport/tunnel) to userland
5553 * XXX replay checking?
5554 * XXX sysctl interface to ipsec_{ah,esp}_keymin
5556 static struct mbuf *
5558 const struct secasindex *saidx;
5560 struct sadb_prop *prop;
5562 const int l = PFKEY_ALIGN8(sizeof(struct sadb_prop));
5565 switch (saidx->proto) {
5567 m = key_getcomb_esp();
5570 m = key_getcomb_ah();
5572 case IPPROTO_IPCOMP:
5573 m = key_getcomb_ipcomp();
5581 M_PREPEND(m, l, MB_DONTWAIT);
5586 for (n = m; n; n = n->m_next)
5589 prop = mtod(m, struct sadb_prop *);
5590 bzero(prop, sizeof(*prop));
5591 prop->sadb_prop_len = PFKEY_UNIT64(totlen);
5592 prop->sadb_prop_exttype = SADB_EXT_PROPOSAL;
5593 prop->sadb_prop_replay = 32; /* XXX */
5599 * SADB_ACQUIRE processing called by key_checkrequest() and key_acquire2().
5601 * <base, SA, address(SD), (address(P)), x_policy,
5602 * (identity(SD),) (sensitivity,) proposal>
5603 * to KMD, and expect to receive
5604 * <base> with SADB_ACQUIRE if error occured,
5606 * <base, src address, dst address, (SPI range)> with SADB_GETSPI
5607 * from KMD by PF_KEY.
5609 * XXX x_policy is outside of RFC2367 (KAME extension).
5610 * XXX sensitivity is not supported.
5611 * XXX for ipcomp, RFC2367 does not define how to fill in proposal.
5612 * see comment for key_getcomb_ipcomp().
5616 * others: error number
5619 key_acquire(const struct secasindex *saidx, struct secpolicy *sp)
5621 struct mbuf *result = NULL, *m;
5622 #ifndef IPSEC_NONBLOCK_ACQUIRE
5623 struct secacq *newacq;
5630 KASSERT(saidx != NULL, ("key_acquire: null saidx"));
5631 satype = key_proto2satype(saidx->proto);
5632 KASSERT(satype != 0,
5633 ("key_acquire: null satype, protocol %u", saidx->proto));
5635 #ifndef IPSEC_NONBLOCK_ACQUIRE
5637 * We never do anything about acquirng SA. There is anather
5638 * solution that kernel blocks to send SADB_ACQUIRE message until
5639 * getting something message from IKEd. In later case, to be
5640 * managed with ACQUIRING list.
5642 /* Get an entry to check whether sending message or not. */
5643 if ((newacq = key_getacq(saidx)) != NULL) {
5644 if (key_blockacq_count < newacq->count) {
5645 /* reset counter and do send message. */
5648 /* increment counter and do nothing. */
5653 /* make new entry for blocking to send SADB_ACQUIRE. */
5654 if ((newacq = key_newacq(saidx)) == NULL)
5657 /* add to acqtree */
5658 LIST_INSERT_HEAD(&acqtree, newacq, chain);
5663 #ifndef IPSEC_NONBLOCK_ACQUIRE
5666 seq = (acq_seq = (acq_seq == ~0 ? 1 : ++acq_seq));
5668 m = key_setsadbmsg(SADB_ACQUIRE, 0, satype, seq, 0, 0);
5675 /* set sadb_address for saidx's. */
5676 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
5677 &saidx->src.sa, FULLMASK, IPSEC_ULPROTO_ANY);
5684 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
5685 &saidx->dst.sa, FULLMASK, IPSEC_ULPROTO_ANY);
5692 /* XXX proxy address (optional) */
5694 /* set sadb_x_policy */
5696 m = key_setsadbxpolicy(sp->policy, sp->spidx.dir, sp->id);
5704 /* XXX identity (optional) */
5706 if (idexttype && fqdn) {
5707 /* create identity extension (FQDN) */
5708 struct sadb_ident *id;
5711 fqdnlen = strlen(fqdn) + 1; /* +1 for terminating-NUL */
5712 id = (struct sadb_ident *)p;
5713 bzero(id, sizeof(*id) + PFKEY_ALIGN8(fqdnlen));
5714 id->sadb_ident_len = PFKEY_UNIT64(sizeof(*id) + PFKEY_ALIGN8(fqdnlen));
5715 id->sadb_ident_exttype = idexttype;
5716 id->sadb_ident_type = SADB_IDENTTYPE_FQDN;
5717 bcopy(fqdn, id + 1, fqdnlen);
5718 p += sizeof(struct sadb_ident) + PFKEY_ALIGN8(fqdnlen);
5722 /* create identity extension (USERFQDN) */
5723 struct sadb_ident *id;
5727 /* +1 for terminating-NUL */
5728 userfqdnlen = strlen(userfqdn) + 1;
5731 id = (struct sadb_ident *)p;
5732 bzero(id, sizeof(*id) + PFKEY_ALIGN8(userfqdnlen));
5733 id->sadb_ident_len = PFKEY_UNIT64(sizeof(*id) + PFKEY_ALIGN8(userfqdnlen));
5734 id->sadb_ident_exttype = idexttype;
5735 id->sadb_ident_type = SADB_IDENTTYPE_USERFQDN;
5736 /* XXX is it correct? */
5737 if (curproc && curproc->p_cred)
5738 id->sadb_ident_id = curproc->p_cred->p_ruid;
5739 if (userfqdn && userfqdnlen)
5740 bcopy(userfqdn, id + 1, userfqdnlen);
5741 p += sizeof(struct sadb_ident) + PFKEY_ALIGN8(userfqdnlen);
5745 /* XXX sensitivity (optional) */
5747 /* create proposal/combination extension */
5748 m = key_getprop(saidx);
5751 * spec conformant: always attach proposal/combination extension,
5752 * the problem is that we have no way to attach it for ipcomp,
5753 * due to the way sadb_comb is declared in RFC2367.
5762 * outside of spec; make proposal/combination extension optional.
5768 if ((result->m_flags & M_PKTHDR) == 0) {
5773 if (result->m_len < sizeof(struct sadb_msg)) {
5774 result = m_pullup(result, sizeof(struct sadb_msg));
5775 if (result == NULL) {
5781 result->m_pkthdr.len = 0;
5782 for (m = result; m; m = m->m_next)
5783 result->m_pkthdr.len += m->m_len;
5785 mtod(result, struct sadb_msg *)->sadb_msg_len =
5786 PFKEY_UNIT64(result->m_pkthdr.len);
5788 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
5796 #ifndef IPSEC_NONBLOCK_ACQUIRE
5797 static struct secacq *
5798 key_newacq(const struct secasindex *saidx)
5800 struct secacq *newacq;
5803 KMALLOC(newacq, struct secacq *, sizeof(struct secacq));
5804 if (newacq == NULL) {
5805 ipseclog((LOG_DEBUG, "key_newacq: No more memory.\n"));
5808 bzero(newacq, sizeof(*newacq));
5811 bcopy(saidx, &newacq->saidx, sizeof(newacq->saidx));
5812 newacq->seq = (acq_seq == ~0 ? 1 : ++acq_seq);
5813 newacq->created = time_second;
5819 static struct secacq *
5820 key_getacq(const struct secasindex *saidx)
5824 LIST_FOREACH(acq, &acqtree, chain) {
5825 if (key_cmpsaidx(saidx, &acq->saidx, CMP_EXACTLY))
5832 static struct secacq *
5833 key_getacqbyseq(seq)
5838 LIST_FOREACH(acq, &acqtree, chain) {
5839 if (acq->seq == seq)
5847 static struct secspacq *
5849 struct secpolicyindex *spidx;
5851 struct secspacq *acq;
5854 KMALLOC(acq, struct secspacq *, sizeof(struct secspacq));
5856 ipseclog((LOG_DEBUG, "key_newspacq: No more memory.\n"));
5859 bzero(acq, sizeof(*acq));
5862 bcopy(spidx, &acq->spidx, sizeof(acq->spidx));
5863 acq->created = time_second;
5869 static struct secspacq *
5871 struct secpolicyindex *spidx;
5873 struct secspacq *acq;
5875 LIST_FOREACH(acq, &spacqtree, chain) {
5876 if (key_cmpspidx_exactly(spidx, &acq->spidx))
5884 * SADB_ACQUIRE processing,
5885 * in first situation, is receiving
5887 * from the ikmpd, and clear sequence of its secasvar entry.
5889 * In second situation, is receiving
5890 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
5891 * from a user land process, and return
5892 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
5895 * m will always be freed.
5898 key_acquire2(so, m, mhp)
5901 const struct sadb_msghdr *mhp;
5903 const struct sadb_address *src0, *dst0;
5904 struct secasindex saidx;
5905 struct secashead *sah;
5910 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
5911 panic("key_acquire2: NULL pointer is passed.\n");
5914 * Error message from KMd.
5915 * We assume that if error was occured in IKEd, the length of PFKEY
5916 * message is equal to the size of sadb_msg structure.
5917 * We do not raise error even if error occured in this function.
5919 if (mhp->msg->sadb_msg_len == PFKEY_UNIT64(sizeof(struct sadb_msg))) {
5920 #ifndef IPSEC_NONBLOCK_ACQUIRE
5923 /* check sequence number */
5924 if (mhp->msg->sadb_msg_seq == 0) {
5925 ipseclog((LOG_DEBUG, "key_acquire2: must specify sequence number.\n"));
5930 if ((acq = key_getacqbyseq(mhp->msg->sadb_msg_seq)) == NULL) {
5932 * the specified larval SA is already gone, or we got
5933 * a bogus sequence number. we can silently ignore it.
5939 /* reset acq counter in order to deletion by timehander. */
5940 acq->created = time_second;
5948 * This message is from user land.
5951 /* map satype to proto */
5952 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5953 ipseclog((LOG_DEBUG, "key_acquire2: invalid satype is passed.\n"));
5954 return key_senderror(so, m, EINVAL);
5957 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5958 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
5959 mhp->ext[SADB_EXT_PROPOSAL] == NULL) {
5961 ipseclog((LOG_DEBUG, "key_acquire2: invalid message is passed.\n"));
5962 return key_senderror(so, m, EINVAL);
5964 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5965 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
5966 mhp->extlen[SADB_EXT_PROPOSAL] < sizeof(struct sadb_prop)) {
5968 ipseclog((LOG_DEBUG, "key_acquire2: invalid message is passed.\n"));
5969 return key_senderror(so, m, EINVAL);
5972 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
5973 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
5975 /* XXX boundary check against sa_len */
5976 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5978 /* get a SA index */
5979 LIST_FOREACH(sah, &sahtree, chain) {
5980 if (sah->state == SADB_SASTATE_DEAD)
5982 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_MODE_REQID))
5986 ipseclog((LOG_DEBUG, "key_acquire2: a SA exists already.\n"));
5987 return key_senderror(so, m, EEXIST);
5990 error = key_acquire(&saidx, NULL);
5992 ipseclog((LOG_DEBUG, "key_acquire2: error %d returned "
5993 "from key_acquire.\n", mhp->msg->sadb_msg_errno));
5994 return key_senderror(so, m, error);
5997 return key_sendup_mbuf(so, m, KEY_SENDUP_REGISTERED);
6001 * SADB_REGISTER processing.
6002 * If SATYPE_UNSPEC has been passed as satype, only return sabd_supported.
6005 * from the ikmpd, and register a socket to send PF_KEY messages,
6009 * If socket is detached, must free from regnode.
6011 * m will always be freed.
6014 key_register(so, m, mhp)
6017 const struct sadb_msghdr *mhp;
6019 struct secreg *reg, *newreg = 0;
6022 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
6023 panic("key_register: NULL pointer is passed.\n");
6025 /* check for invalid register message */
6026 if (mhp->msg->sadb_msg_satype >= sizeof(regtree)/sizeof(regtree[0]))
6027 return key_senderror(so, m, EINVAL);
6029 /* When SATYPE_UNSPEC is specified, only return sabd_supported. */
6030 if (mhp->msg->sadb_msg_satype == SADB_SATYPE_UNSPEC)
6033 /* check whether existing or not */
6034 LIST_FOREACH(reg, ®tree[mhp->msg->sadb_msg_satype], chain) {
6035 if (reg->so == so) {
6036 ipseclog((LOG_DEBUG, "key_register: socket exists already.\n"));
6037 return key_senderror(so, m, EEXIST);
6041 /* create regnode */
6042 KMALLOC(newreg, struct secreg *, sizeof(*newreg));
6043 if (newreg == NULL) {
6044 ipseclog((LOG_DEBUG, "key_register: No more memory.\n"));
6045 return key_senderror(so, m, ENOBUFS);
6047 bzero((caddr_t)newreg, sizeof(*newreg));
6050 ((struct keycb *)sotorawcb(so))->kp_registered++;
6052 /* add regnode to regtree. */
6053 LIST_INSERT_HEAD(®tree[mhp->msg->sadb_msg_satype], newreg, chain);
6058 struct sadb_msg *newmsg;
6059 struct sadb_supported *sup;
6060 u_int len, alen, elen;
6063 struct sadb_alg *alg;
6065 /* create new sadb_msg to reply. */
6067 for (i = 1; i <= SADB_AALG_MAX; i++) {
6068 if (ah_algorithm_lookup(i))
6069 alen += sizeof(struct sadb_alg);
6072 alen += sizeof(struct sadb_supported);
6074 for (i = 1; i <= SADB_EALG_MAX; i++) {
6075 if (esp_algorithm_lookup(i))
6076 elen += sizeof(struct sadb_alg);
6079 elen += sizeof(struct sadb_supported);
6081 len = sizeof(struct sadb_msg) + alen + elen;
6084 return key_senderror(so, m, ENOBUFS);
6086 MGETHDR(n, MB_DONTWAIT, MT_DATA);
6088 MCLGET(n, MB_DONTWAIT);
6089 if ((n->m_flags & M_EXT) == 0) {
6095 return key_senderror(so, m, ENOBUFS);
6097 n->m_pkthdr.len = n->m_len = len;
6101 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
6102 newmsg = mtod(n, struct sadb_msg *);
6103 newmsg->sadb_msg_errno = 0;
6104 newmsg->sadb_msg_len = PFKEY_UNIT64(len);
6105 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
6107 /* for authentication algorithm */
6109 sup = (struct sadb_supported *)(mtod(n, caddr_t) + off);
6110 sup->sadb_supported_len = PFKEY_UNIT64(alen);
6111 sup->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
6112 off += PFKEY_ALIGN8(sizeof(*sup));
6114 for (i = 1; i <= SADB_AALG_MAX; i++) {
6115 struct auth_hash *aalgo;
6116 u_int16_t minkeysize, maxkeysize;
6118 aalgo = ah_algorithm_lookup(i);
6121 alg = (struct sadb_alg *)(mtod(n, caddr_t) + off);
6122 alg->sadb_alg_id = i;
6123 alg->sadb_alg_ivlen = 0;
6124 key_getsizes_ah(aalgo, i, &minkeysize, &maxkeysize);
6125 alg->sadb_alg_minbits = _BITS(minkeysize);
6126 alg->sadb_alg_maxbits = _BITS(maxkeysize);
6127 off += PFKEY_ALIGN8(sizeof(*alg));
6131 /* for encryption algorithm */
6133 sup = (struct sadb_supported *)(mtod(n, caddr_t) + off);
6134 sup->sadb_supported_len = PFKEY_UNIT64(elen);
6135 sup->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT;
6136 off += PFKEY_ALIGN8(sizeof(*sup));
6138 for (i = 1; i <= SADB_EALG_MAX; i++) {
6139 struct enc_xform *ealgo;
6141 ealgo = esp_algorithm_lookup(i);
6144 alg = (struct sadb_alg *)(mtod(n, caddr_t) + off);
6145 alg->sadb_alg_id = i;
6146 alg->sadb_alg_ivlen = ealgo->blocksize;
6147 alg->sadb_alg_minbits = _BITS(ealgo->minkey);
6148 alg->sadb_alg_maxbits = _BITS(ealgo->maxkey);
6149 off += PFKEY_ALIGN8(sizeof(struct sadb_alg));
6155 panic("length assumption failed in key_register");
6159 return key_sendup_mbuf(so, n, KEY_SENDUP_REGISTERED);
6164 * free secreg entry registered.
6165 * XXX: I want to do free a socket marked done SADB_RESIGER to socket.
6176 panic("key_freereg: NULL pointer is passed.\n");
6179 * check whether existing or not.
6180 * check all type of SA, because there is a potential that
6181 * one socket is registered to multiple type of SA.
6183 for (i = 0; i <= SADB_SATYPE_MAX; i++) {
6184 LIST_FOREACH(reg, ®tree[i], chain) {
6186 && __LIST_CHAINED(reg)) {
6187 LIST_REMOVE(reg, chain);
6198 * SADB_EXPIRE processing
6200 * <base, SA, SA2, lifetime(C and one of HS), address(SD)>
6202 * NOTE: We send only soft lifetime extension.
6205 * others : error number
6209 struct secasvar *sav;
6213 struct mbuf *result = NULL, *m;
6216 struct sadb_lifetime *lt;
6218 /* XXX: Why do we lock ? */
6219 s = splnet(); /*called from softclock()*/
6223 panic("key_expire: NULL pointer is passed.\n");
6224 if (sav->sah == NULL)
6225 panic("key_expire: Why was SA index in SA NULL.\n");
6226 if ((satype = key_proto2satype(sav->sah->saidx.proto)) == 0)
6227 panic("key_expire: invalid proto is passed.\n");
6229 /* set msg header */
6230 m = key_setsadbmsg(SADB_EXPIRE, 0, satype, sav->seq, 0, sav->refcnt);
6237 /* create SA extension */
6238 m = key_setsadbsa(sav);
6245 /* create SA extension */
6246 m = key_setsadbxsa2(sav->sah->saidx.mode,
6247 sav->replay ? sav->replay->count : 0,
6248 sav->sah->saidx.reqid);
6255 /* create lifetime extension (current and soft) */
6256 len = PFKEY_ALIGN8(sizeof(*lt)) * 2;
6257 m = key_alloc_mbuf(len);
6258 if (!m || m->m_next) { /*XXX*/
6264 bzero(mtod(m, caddr_t), len);
6265 lt = mtod(m, struct sadb_lifetime *);
6266 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
6267 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
6268 lt->sadb_lifetime_allocations = sav->lft_c->sadb_lifetime_allocations;
6269 lt->sadb_lifetime_bytes = sav->lft_c->sadb_lifetime_bytes;
6270 lt->sadb_lifetime_addtime = sav->lft_c->sadb_lifetime_addtime;
6271 lt->sadb_lifetime_usetime = sav->lft_c->sadb_lifetime_usetime;
6272 lt = (struct sadb_lifetime *)(mtod(m, caddr_t) + len / 2);
6273 bcopy(sav->lft_s, lt, sizeof(*lt));
6276 /* set sadb_address for source */
6277 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
6278 &sav->sah->saidx.src.sa,
6279 FULLMASK, IPSEC_ULPROTO_ANY);
6286 /* set sadb_address for destination */
6287 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
6288 &sav->sah->saidx.dst.sa,
6289 FULLMASK, IPSEC_ULPROTO_ANY);
6296 if ((result->m_flags & M_PKTHDR) == 0) {
6301 if (result->m_len < sizeof(struct sadb_msg)) {
6302 result = m_pullup(result, sizeof(struct sadb_msg));
6303 if (result == NULL) {
6309 result->m_pkthdr.len = 0;
6310 for (m = result; m; m = m->m_next)
6311 result->m_pkthdr.len += m->m_len;
6313 mtod(result, struct sadb_msg *)->sadb_msg_len =
6314 PFKEY_UNIT64(result->m_pkthdr.len);
6317 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
6327 * SADB_FLUSH processing
6330 * from the ikmpd, and free all entries in secastree.
6334 * NOTE: to do is only marking SADB_SASTATE_DEAD.
6336 * m will always be freed.
6339 key_flush(so, m, mhp)
6342 const struct sadb_msghdr *mhp;
6344 struct sadb_msg *newmsg;
6345 struct secashead *sah, *nextsah;
6346 struct secasvar *sav, *nextsav;
6352 if (so == NULL || mhp == NULL || mhp->msg == NULL)
6353 panic("key_flush: NULL pointer is passed.\n");
6355 /* map satype to proto */
6356 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
6357 ipseclog((LOG_DEBUG, "key_flush: invalid satype is passed.\n"));
6358 return key_senderror(so, m, EINVAL);
6361 /* no SATYPE specified, i.e. flushing all SA. */
6362 for (sah = LIST_FIRST(&sahtree);
6365 nextsah = LIST_NEXT(sah, chain);
6367 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
6368 && proto != sah->saidx.proto)
6372 stateidx < _ARRAYLEN(saorder_state_alive);
6374 state = saorder_state_any[stateidx];
6375 for (sav = LIST_FIRST(&sah->savtree[state]);
6379 nextsav = LIST_NEXT(sav, chain);
6381 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
6386 sah->state = SADB_SASTATE_DEAD;
6389 if (m->m_len < sizeof(struct sadb_msg) ||
6390 sizeof(struct sadb_msg) > m->m_len + M_TRAILINGSPACE(m)) {
6391 ipseclog((LOG_DEBUG, "key_flush: No more memory.\n"));
6392 return key_senderror(so, m, ENOBUFS);
6398 m->m_pkthdr.len = m->m_len = sizeof(struct sadb_msg);
6399 newmsg = mtod(m, struct sadb_msg *);
6400 newmsg->sadb_msg_errno = 0;
6401 newmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
6403 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
6407 * SADB_DUMP processing
6408 * dump all entries including status of DEAD in SAD.
6411 * from the ikmpd, and dump all secasvar leaves
6416 * m will always be freed.
6419 key_dump(so, m, mhp)
6422 const struct sadb_msghdr *mhp;
6424 struct secashead *sah;
6425 struct secasvar *sav;
6431 struct sadb_msg *newmsg;
6435 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
6436 panic("key_dump: NULL pointer is passed.\n");
6438 /* map satype to proto */
6439 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
6440 ipseclog((LOG_DEBUG, "key_dump: invalid satype is passed.\n"));
6441 return key_senderror(so, m, EINVAL);
6444 /* count sav entries to be sent to the userland. */
6446 LIST_FOREACH(sah, &sahtree, chain) {
6447 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
6448 && proto != sah->saidx.proto)
6452 stateidx < _ARRAYLEN(saorder_state_any);
6454 state = saorder_state_any[stateidx];
6455 LIST_FOREACH(sav, &sah->savtree[state], chain) {
6462 return key_senderror(so, m, ENOENT);
6464 /* send this to the userland, one at a time. */
6466 LIST_FOREACH(sah, &sahtree, chain) {
6467 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
6468 && proto != sah->saidx.proto)
6471 /* map proto to satype */
6472 if ((satype = key_proto2satype(sah->saidx.proto)) == 0) {
6473 ipseclog((LOG_DEBUG, "key_dump: there was invalid proto in SAD.\n"));
6474 return key_senderror(so, m, EINVAL);
6478 stateidx < _ARRAYLEN(saorder_state_any);
6480 state = saorder_state_any[stateidx];
6481 LIST_FOREACH(sav, &sah->savtree[state], chain) {
6482 n = key_setdumpsa(sav, SADB_DUMP, satype,
6483 --cnt, mhp->msg->sadb_msg_pid);
6485 return key_senderror(so, m, ENOBUFS);
6487 key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
6497 * SADB_X_PROMISC processing
6499 * m will always be freed.
6502 key_promisc(so, m, mhp)
6505 const struct sadb_msghdr *mhp;
6510 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
6511 panic("key_promisc: NULL pointer is passed.\n");
6513 olen = PFKEY_UNUNIT64(mhp->msg->sadb_msg_len);
6515 if (olen < sizeof(struct sadb_msg)) {
6517 return key_senderror(so, m, EINVAL);
6522 } else if (olen == sizeof(struct sadb_msg)) {
6523 /* enable/disable promisc mode */
6526 if ((kp = (struct keycb *)sotorawcb(so)) == NULL)
6527 return key_senderror(so, m, EINVAL);
6528 mhp->msg->sadb_msg_errno = 0;
6529 switch (mhp->msg->sadb_msg_satype) {
6532 kp->kp_promisc = mhp->msg->sadb_msg_satype;
6535 return key_senderror(so, m, EINVAL);
6538 /* send the original message back to everyone */
6539 mhp->msg->sadb_msg_errno = 0;
6540 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
6542 /* send packet as is */
6544 m_adj(m, PFKEY_ALIGN8(sizeof(struct sadb_msg)));
6546 /* TODO: if sadb_msg_seq is specified, send to specific pid */
6547 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
6551 static int (*key_typesw[]) (struct socket *, struct mbuf *,
6552 const struct sadb_msghdr *) = {
6553 NULL, /* SADB_RESERVED */
6554 key_getspi, /* SADB_GETSPI */
6555 key_update, /* SADB_UPDATE */
6556 key_add, /* SADB_ADD */
6557 key_delete, /* SADB_DELETE */
6558 key_get, /* SADB_GET */
6559 key_acquire2, /* SADB_ACQUIRE */
6560 key_register, /* SADB_REGISTER */
6561 NULL, /* SADB_EXPIRE */
6562 key_flush, /* SADB_FLUSH */
6563 key_dump, /* SADB_DUMP */
6564 key_promisc, /* SADB_X_PROMISC */
6565 NULL, /* SADB_X_PCHANGE */
6566 key_spdadd, /* SADB_X_SPDUPDATE */
6567 key_spdadd, /* SADB_X_SPDADD */
6568 key_spddelete, /* SADB_X_SPDDELETE */
6569 key_spdget, /* SADB_X_SPDGET */
6570 NULL, /* SADB_X_SPDACQUIRE */
6571 key_spddump, /* SADB_X_SPDDUMP */
6572 key_spdflush, /* SADB_X_SPDFLUSH */
6573 key_spdadd, /* SADB_X_SPDSETIDX */
6574 NULL, /* SADB_X_SPDEXPIRE */
6575 key_spddelete2, /* SADB_X_SPDDELETE2 */
6579 * parse sadb_msg buffer to process PFKEYv2,
6580 * and create a data to response if needed.
6581 * I think to be dealed with mbuf directly.
6583 * msgp : pointer to pointer to a received buffer pulluped.
6584 * This is rewrited to response.
6585 * so : pointer to socket.
6587 * length for buffer to send to user process.
6594 struct sadb_msg *msg;
6595 struct sadb_msghdr mh;
6601 if (m == NULL || so == NULL)
6602 panic("key_parse: NULL pointer is passed.\n");
6604 #if 0 /*kdebug_sadb assumes msg in linear buffer*/
6605 KEYDEBUG(KEYDEBUG_KEY_DUMP,
6606 ipseclog((LOG_DEBUG, "key_parse: passed sadb_msg\n"));
6610 if (m->m_len < sizeof(struct sadb_msg)) {
6611 m = m_pullup(m, sizeof(struct sadb_msg));
6615 msg = mtod(m, struct sadb_msg *);
6616 orglen = PFKEY_UNUNIT64(msg->sadb_msg_len);
6617 target = KEY_SENDUP_ONE;
6619 if ((m->m_flags & M_PKTHDR) == 0 ||
6620 m->m_pkthdr.len != m->m_pkthdr.len) {
6621 ipseclog((LOG_DEBUG, "key_parse: invalid message length.\n"));
6622 pfkeystat.out_invlen++;
6627 if (msg->sadb_msg_version != PF_KEY_V2) {
6628 ipseclog((LOG_DEBUG,
6629 "key_parse: PF_KEY version %u is mismatched.\n",
6630 msg->sadb_msg_version));
6631 pfkeystat.out_invver++;
6636 if (msg->sadb_msg_type > SADB_MAX) {
6637 ipseclog((LOG_DEBUG, "key_parse: invalid type %u is passed.\n",
6638 msg->sadb_msg_type));
6639 pfkeystat.out_invmsgtype++;
6644 /* for old-fashioned code - should be nuked */
6645 if (m->m_pkthdr.len > MCLBYTES) {
6652 MGETHDR(n, MB_DONTWAIT, MT_DATA);
6653 if (n && m->m_pkthdr.len > MHLEN) {
6654 MCLGET(n, MB_DONTWAIT);
6655 if ((n->m_flags & M_EXT) == 0) {
6664 m_copydata(m, 0, m->m_pkthdr.len, mtod(n, caddr_t));
6665 n->m_pkthdr.len = n->m_len = m->m_pkthdr.len;
6671 /* align the mbuf chain so that extensions are in contiguous region. */
6672 error = key_align(m, &mh);
6676 if (m->m_next) { /*XXX*/
6684 switch (msg->sadb_msg_satype) {
6685 case SADB_SATYPE_UNSPEC:
6686 switch (msg->sadb_msg_type) {
6694 ipseclog((LOG_DEBUG, "key_parse: must specify satype "
6695 "when msg type=%u.\n", msg->sadb_msg_type));
6696 pfkeystat.out_invsatype++;
6701 case SADB_SATYPE_AH:
6702 case SADB_SATYPE_ESP:
6703 case SADB_X_SATYPE_IPCOMP:
6704 switch (msg->sadb_msg_type) {
6706 case SADB_X_SPDDELETE:
6708 case SADB_X_SPDDUMP:
6709 case SADB_X_SPDFLUSH:
6710 case SADB_X_SPDSETIDX:
6711 case SADB_X_SPDUPDATE:
6712 case SADB_X_SPDDELETE2:
6713 ipseclog((LOG_DEBUG, "key_parse: illegal satype=%u\n",
6714 msg->sadb_msg_type));
6715 pfkeystat.out_invsatype++;
6720 case SADB_SATYPE_RSVP:
6721 case SADB_SATYPE_OSPFV2:
6722 case SADB_SATYPE_RIPV2:
6723 case SADB_SATYPE_MIP:
6724 ipseclog((LOG_DEBUG, "key_parse: type %u isn't supported.\n",
6725 msg->sadb_msg_satype));
6726 pfkeystat.out_invsatype++;
6729 case 1: /* XXX: What does it do? */
6730 if (msg->sadb_msg_type == SADB_X_PROMISC)
6734 ipseclog((LOG_DEBUG, "key_parse: invalid type %u is passed.\n",
6735 msg->sadb_msg_satype));
6736 pfkeystat.out_invsatype++;
6741 /* check field of upper layer protocol and address family */
6742 if (mh.ext[SADB_EXT_ADDRESS_SRC] != NULL
6743 && mh.ext[SADB_EXT_ADDRESS_DST] != NULL) {
6744 struct sadb_address *src0, *dst0;
6747 src0 = (struct sadb_address *)(mh.ext[SADB_EXT_ADDRESS_SRC]);
6748 dst0 = (struct sadb_address *)(mh.ext[SADB_EXT_ADDRESS_DST]);
6750 /* check upper layer protocol */
6751 if (src0->sadb_address_proto != dst0->sadb_address_proto) {
6752 ipseclog((LOG_DEBUG, "key_parse: upper layer protocol mismatched.\n"));
6753 pfkeystat.out_invaddr++;
6759 if (PFKEY_ADDR_SADDR(src0)->sa_family !=
6760 PFKEY_ADDR_SADDR(dst0)->sa_family) {
6761 ipseclog((LOG_DEBUG, "key_parse: address family mismatched.\n"));
6762 pfkeystat.out_invaddr++;
6766 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
6767 PFKEY_ADDR_SADDR(dst0)->sa_len) {
6768 ipseclog((LOG_DEBUG,
6769 "key_parse: address struct size mismatched.\n"));
6770 pfkeystat.out_invaddr++;
6775 switch (PFKEY_ADDR_SADDR(src0)->sa_family) {
6777 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
6778 sizeof(struct sockaddr_in)) {
6779 pfkeystat.out_invaddr++;
6785 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
6786 sizeof(struct sockaddr_in6)) {
6787 pfkeystat.out_invaddr++;
6793 ipseclog((LOG_DEBUG,
6794 "key_parse: unsupported address family.\n"));
6795 pfkeystat.out_invaddr++;
6796 error = EAFNOSUPPORT;
6800 switch (PFKEY_ADDR_SADDR(src0)->sa_family) {
6802 plen = sizeof(struct in_addr) << 3;
6805 plen = sizeof(struct in6_addr) << 3;
6808 plen = 0; /*fool gcc*/
6812 /* check max prefix length */
6813 if (src0->sadb_address_prefixlen > plen ||
6814 dst0->sadb_address_prefixlen > plen) {
6815 ipseclog((LOG_DEBUG,
6816 "key_parse: illegal prefixlen.\n"));
6817 pfkeystat.out_invaddr++;
6823 * prefixlen == 0 is valid because there can be a case when
6824 * all addresses are matched.
6828 if (msg->sadb_msg_type >= sizeof(key_typesw)/sizeof(key_typesw[0]) ||
6829 key_typesw[msg->sadb_msg_type] == NULL) {
6830 pfkeystat.out_invmsgtype++;
6835 return (*key_typesw[msg->sadb_msg_type])(so, m, &mh);
6838 msg->sadb_msg_errno = error;
6839 return key_sendup_mbuf(so, m, target);
6843 key_senderror(so, m, code)
6848 struct sadb_msg *msg;
6850 if (m->m_len < sizeof(struct sadb_msg))
6851 panic("invalid mbuf passed to key_senderror");
6853 msg = mtod(m, struct sadb_msg *);
6854 msg->sadb_msg_errno = code;
6855 return key_sendup_mbuf(so, m, KEY_SENDUP_ONE);
6859 * set the pointer to each header into message buffer.
6860 * m will be freed on error.
6861 * XXX larger-than-MCLBYTES extension?
6866 struct sadb_msghdr *mhp;
6869 struct sadb_ext *ext;
6875 if (m == NULL || mhp == NULL)
6876 panic("key_align: NULL pointer is passed.\n");
6877 if (m->m_len < sizeof(struct sadb_msg))
6878 panic("invalid mbuf passed to key_align");
6881 bzero(mhp, sizeof(*mhp));
6883 mhp->msg = mtod(m, struct sadb_msg *);
6884 mhp->ext[0] = (struct sadb_ext *)mhp->msg; /*XXX backward compat */
6886 end = PFKEY_UNUNIT64(mhp->msg->sadb_msg_len);
6887 extlen = end; /*just in case extlen is not updated*/
6888 for (off = sizeof(struct sadb_msg); off < end; off += extlen) {
6889 n = m_pulldown(m, off, sizeof(struct sadb_ext), &toff);
6891 /* m is already freed */
6894 ext = (struct sadb_ext *)(mtod(n, caddr_t) + toff);
6897 switch (ext->sadb_ext_type) {
6899 case SADB_EXT_ADDRESS_SRC:
6900 case SADB_EXT_ADDRESS_DST:
6901 case SADB_EXT_ADDRESS_PROXY:
6902 case SADB_EXT_LIFETIME_CURRENT:
6903 case SADB_EXT_LIFETIME_HARD:
6904 case SADB_EXT_LIFETIME_SOFT:
6905 case SADB_EXT_KEY_AUTH:
6906 case SADB_EXT_KEY_ENCRYPT:
6907 case SADB_EXT_IDENTITY_SRC:
6908 case SADB_EXT_IDENTITY_DST:
6909 case SADB_EXT_SENSITIVITY:
6910 case SADB_EXT_PROPOSAL:
6911 case SADB_EXT_SUPPORTED_AUTH:
6912 case SADB_EXT_SUPPORTED_ENCRYPT:
6913 case SADB_EXT_SPIRANGE:
6914 case SADB_X_EXT_POLICY:
6915 case SADB_X_EXT_SA2:
6916 /* duplicate check */
6918 * XXX Are there duplication payloads of either
6919 * KEY_AUTH or KEY_ENCRYPT ?
6921 if (mhp->ext[ext->sadb_ext_type] != NULL) {
6922 ipseclog((LOG_DEBUG,
6923 "key_align: duplicate ext_type %u "
6924 "is passed.\n", ext->sadb_ext_type));
6926 pfkeystat.out_dupext++;
6931 ipseclog((LOG_DEBUG,
6932 "key_align: invalid ext_type %u is passed.\n",
6933 ext->sadb_ext_type));
6935 pfkeystat.out_invexttype++;
6939 extlen = PFKEY_UNUNIT64(ext->sadb_ext_len);
6941 if (key_validate_ext(ext, extlen)) {
6943 pfkeystat.out_invlen++;
6947 n = m_pulldown(m, off, extlen, &toff);
6949 /* m is already freed */
6952 ext = (struct sadb_ext *)(mtod(n, caddr_t) + toff);
6954 mhp->ext[ext->sadb_ext_type] = ext;
6955 mhp->extoff[ext->sadb_ext_type] = off;
6956 mhp->extlen[ext->sadb_ext_type] = extlen;
6961 pfkeystat.out_invlen++;
6969 key_validate_ext(ext, len)
6970 const struct sadb_ext *ext;
6973 const struct sockaddr *sa;
6974 enum { NONE, ADDR } checktype = NONE;
6976 const int sal = offsetof(struct sockaddr, sa_len) + sizeof(sa->sa_len);
6978 if (len != PFKEY_UNUNIT64(ext->sadb_ext_len))
6981 /* if it does not match minimum/maximum length, bail */
6982 if (ext->sadb_ext_type >= sizeof(minsize) / sizeof(minsize[0]) ||
6983 ext->sadb_ext_type >= sizeof(maxsize) / sizeof(maxsize[0]))
6985 if (!minsize[ext->sadb_ext_type] || len < minsize[ext->sadb_ext_type])
6987 if (maxsize[ext->sadb_ext_type] && len > maxsize[ext->sadb_ext_type])
6990 /* more checks based on sadb_ext_type XXX need more */
6991 switch (ext->sadb_ext_type) {
6992 case SADB_EXT_ADDRESS_SRC:
6993 case SADB_EXT_ADDRESS_DST:
6994 case SADB_EXT_ADDRESS_PROXY:
6995 baselen = PFKEY_ALIGN8(sizeof(struct sadb_address));
6998 case SADB_EXT_IDENTITY_SRC:
6999 case SADB_EXT_IDENTITY_DST:
7000 if (((const struct sadb_ident *)ext)->sadb_ident_type ==
7001 SADB_X_IDENTTYPE_ADDR) {
7002 baselen = PFKEY_ALIGN8(sizeof(struct sadb_ident));
7012 switch (checktype) {
7016 sa = (const struct sockaddr *)(((const u_int8_t*)ext)+baselen);
7017 if (len < baselen + sal)
7019 if (baselen + PFKEY_ALIGN8(sa->sa_len) != len)
7032 for (i = 0; i < IPSEC_DIR_MAX; i++) {
7033 LIST_INIT(&sptree[i]);
7036 LIST_INIT(&sahtree);
7038 for (i = 0; i <= SADB_SATYPE_MAX; i++) {
7039 LIST_INIT(®tree[i]);
7042 #ifndef IPSEC_NONBLOCK_ACQUIRE
7043 LIST_INIT(&acqtree);
7045 LIST_INIT(&spacqtree);
7047 /* system default */
7048 ip4_def_policy.policy = IPSEC_POLICY_NONE;
7049 ip4_def_policy.refcnt++; /*never reclaim this*/
7051 #ifndef IPSEC_DEBUG2
7052 callout_init(&key_timehandler_ch);
7053 callout_reset(&key_timehandler_ch, hz, key_timehandler, NULL);
7054 #endif /*IPSEC_DEBUG2*/
7056 /* initialize key statistics */
7057 keystat.getspi_count = 1;
7059 printf("IPsec: Initialized Security Association Processing.\n");
7065 * XXX: maybe This function is called after INBOUND IPsec processing.
7067 * Special check for tunnel-mode packets.
7068 * We must make some checks for consistency between inner and outer IP header.
7070 * xxx more checks to be provided
7073 key_checktunnelsanity(sav, family, src, dst)
7074 struct secasvar *sav;
7080 if (sav->sah == NULL)
7081 panic("sav->sah == NULL at key_checktunnelsanity");
7083 /* XXX: check inner IP header */
7089 #define hostnamelen strlen(hostname)
7092 * Get FQDN for the host.
7093 * If the administrator configured hostname (by hostname(1)) without
7094 * domain name, returns nothing.
7101 static char fqdn[MAXHOSTNAMELEN + 1];
7106 /* check if it comes with domain name. */
7108 for (i = 0; i < hostnamelen; i++) {
7109 if (hostname[i] == '.')
7115 /* NOTE: hostname may not be NUL-terminated. */
7116 bzero(fqdn, sizeof(fqdn));
7117 bcopy(hostname, fqdn, hostnamelen);
7118 fqdn[hostnamelen] = '\0';
7123 * get username@FQDN for the host/user.
7129 static char userfqdn[MAXHOSTNAMELEN + MAXLOGNAME + 2];
7130 struct proc *p = curproc;
7133 if (!p || !p->p_pgrp || !p->p_pgrp->pg_session)
7135 if (!(host = key_getfqdn()))
7138 /* NOTE: s_login may not be-NUL terminated. */
7139 bzero(userfqdn, sizeof(userfqdn));
7140 bcopy(p->p_pgrp->pg_session->s_login, userfqdn, MAXLOGNAME);
7141 userfqdn[MAXLOGNAME] = '\0'; /* safeguard */
7142 q = userfqdn + strlen(userfqdn);
7144 bcopy(host, q, strlen(host));
7152 /* record data transfer on SA, and update timestamps */
7154 key_sa_recordxfer(sav, m)
7155 struct secasvar *sav;
7158 KASSERT(sav != NULL, ("key_sa_recordxfer: Null secasvar"));
7159 KASSERT(m != NULL, ("key_sa_recordxfer: Null mbuf"));
7164 * XXX Currently, there is a difference of bytes size
7165 * between inbound and outbound processing.
7167 sav->lft_c->sadb_lifetime_bytes += m->m_pkthdr.len;
7168 /* to check bytes lifetime is done in key_timehandler(). */
7171 * We use the number of packets as the unit of
7172 * sadb_lifetime_allocations. We increment the variable
7173 * whenever {esp,ah}_{in,out}put is called.
7175 sav->lft_c->sadb_lifetime_allocations++;
7176 /* XXX check for expires? */
7179 * NOTE: We record CURRENT sadb_lifetime_usetime by using wall clock,
7180 * in seconds. HARD and SOFT lifetime are measured by the time
7181 * difference (again in seconds) from sadb_lifetime_usetime.
7185 * -----+-----+--------+---> t
7186 * <--------------> HARD
7189 sav->lft_c->sadb_lifetime_usetime = time_second;
7190 /* XXX check for expires? */
7197 key_sa_routechange(dst)
7198 struct sockaddr *dst;
7200 struct secashead *sah;
7203 LIST_FOREACH(sah, &sahtree, chain) {
7204 ro = &sah->sa_route;
7205 if (ro->ro_rt && dst->sa_len == ro->ro_dst.sa_len
7206 && bcmp(dst, &ro->ro_dst, dst->sa_len) == 0) {
7208 ro->ro_rt = (struct rtentry *)NULL;
7216 key_sa_chgstate(sav, state)
7217 struct secasvar *sav;
7221 panic("key_sa_chgstate called with sav == NULL");
7223 if (sav->state == state)
7226 if (__LIST_CHAINED(sav))
7227 LIST_REMOVE(sav, chain);
7230 LIST_INSERT_HEAD(&sav->sah->savtree[state], sav, chain);
7235 struct secasvar *sav;
7239 panic("key_sa_stir_iv called with sav == NULL");
7240 key_randomfill(sav->iv, sav->ivlen);
7244 static struct mbuf *
7248 struct mbuf *m = NULL, *n;
7253 MGET(n, MB_DONTWAIT, MT_DATA);
7254 if (n && len > MLEN)
7255 MCLGET(n, MB_DONTWAIT);
7263 n->m_len = M_TRAILINGSPACE(n);
7264 /* use the bottom of mbuf, hoping we can prepend afterwards */
7265 if (n->m_len > len) {
7266 t = (n->m_len - len) & ~(sizeof(long) - 1);