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.6 2004/04/22 05:09:48 dillon 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>
84 #include "key_debug.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.
114 u_int32_t key_debug_level = 0;
115 static u_int key_spi_trycnt = 1000;
116 static u_int32_t key_spi_minval = 0x100;
117 static u_int32_t key_spi_maxval = 0x0fffffff; /* XXX */
118 static u_int32_t policy_id = 0;
119 static u_int key_int_random = 60; /*interval to initialize randseed,1(m)*/
120 static u_int key_larval_lifetime = 30; /* interval to expire acquiring, 30(s)*/
121 static int key_blockacq_count = 10; /* counter for blocking SADB_ACQUIRE.*/
122 static int key_blockacq_lifetime = 20; /* lifetime for blocking SADB_ACQUIRE.*/
123 static int key_prefered_oldsa = 1; /* prefered old sa rather than new sa.*/
125 static u_int32_t acq_seq = 0;
126 static int key_tick_init_random = 0;
128 static LIST_HEAD(_sptree, secpolicy) sptree[IPSEC_DIR_MAX]; /* SPD */
129 static LIST_HEAD(_sahtree, secashead) sahtree; /* SAD */
130 static LIST_HEAD(_regtree, secreg) regtree[SADB_SATYPE_MAX + 1];
132 #ifndef IPSEC_NONBLOCK_ACQUIRE
133 static LIST_HEAD(_acqtree, secacq) acqtree; /* acquiring list */
135 static LIST_HEAD(_spacqtree, secspacq) spacqtree; /* SP acquiring list */
137 /* search order for SAs */
138 static u_int saorder_state_valid[] = {
139 SADB_SASTATE_DYING, SADB_SASTATE_MATURE,
141 * This order is important because we must select the oldest SA
142 * for outbound processing. For inbound, This is not important.
145 static u_int saorder_state_alive[] = {
147 SADB_SASTATE_MATURE, SADB_SASTATE_DYING, SADB_SASTATE_LARVAL
149 static u_int saorder_state_any[] = {
150 SADB_SASTATE_MATURE, SADB_SASTATE_DYING,
151 SADB_SASTATE_LARVAL, SADB_SASTATE_DEAD
154 static const int minsize[] = {
155 sizeof(struct sadb_msg), /* SADB_EXT_RESERVED */
156 sizeof(struct sadb_sa), /* SADB_EXT_SA */
157 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_CURRENT */
158 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_HARD */
159 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_SOFT */
160 sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_SRC */
161 sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_DST */
162 sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_PROXY */
163 sizeof(struct sadb_key), /* SADB_EXT_KEY_AUTH */
164 sizeof(struct sadb_key), /* SADB_EXT_KEY_ENCRYPT */
165 sizeof(struct sadb_ident), /* SADB_EXT_IDENTITY_SRC */
166 sizeof(struct sadb_ident), /* SADB_EXT_IDENTITY_DST */
167 sizeof(struct sadb_sens), /* SADB_EXT_SENSITIVITY */
168 sizeof(struct sadb_prop), /* SADB_EXT_PROPOSAL */
169 sizeof(struct sadb_supported), /* SADB_EXT_SUPPORTED_AUTH */
170 sizeof(struct sadb_supported), /* SADB_EXT_SUPPORTED_ENCRYPT */
171 sizeof(struct sadb_spirange), /* SADB_EXT_SPIRANGE */
172 0, /* SADB_X_EXT_KMPRIVATE */
173 sizeof(struct sadb_x_policy), /* SADB_X_EXT_POLICY */
174 sizeof(struct sadb_x_sa2), /* SADB_X_SA2 */
176 static const int maxsize[] = {
177 sizeof(struct sadb_msg), /* SADB_EXT_RESERVED */
178 sizeof(struct sadb_sa), /* SADB_EXT_SA */
179 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_CURRENT */
180 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_HARD */
181 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_SOFT */
182 0, /* SADB_EXT_ADDRESS_SRC */
183 0, /* SADB_EXT_ADDRESS_DST */
184 0, /* SADB_EXT_ADDRESS_PROXY */
185 0, /* SADB_EXT_KEY_AUTH */
186 0, /* SADB_EXT_KEY_ENCRYPT */
187 0, /* SADB_EXT_IDENTITY_SRC */
188 0, /* SADB_EXT_IDENTITY_DST */
189 0, /* SADB_EXT_SENSITIVITY */
190 0, /* SADB_EXT_PROPOSAL */
191 0, /* SADB_EXT_SUPPORTED_AUTH */
192 0, /* SADB_EXT_SUPPORTED_ENCRYPT */
193 sizeof(struct sadb_spirange), /* SADB_EXT_SPIRANGE */
194 0, /* SADB_X_EXT_KMPRIVATE */
195 0, /* SADB_X_EXT_POLICY */
196 sizeof(struct sadb_x_sa2), /* SADB_X_SA2 */
199 static int ipsec_esp_keymin = 256;
200 static int ipsec_esp_auth = 0;
201 static int ipsec_ah_keymin = 128;
204 SYSCTL_DECL(_net_key);
207 SYSCTL_INT(_net_key, KEYCTL_DEBUG_LEVEL, debug, CTLFLAG_RW, \
208 &key_debug_level, 0, "");
210 /* max count of trial for the decision of spi value */
211 SYSCTL_INT(_net_key, KEYCTL_SPI_TRY, spi_trycnt, CTLFLAG_RW, \
212 &key_spi_trycnt, 0, "");
214 /* minimum spi value to allocate automatically. */
215 SYSCTL_INT(_net_key, KEYCTL_SPI_MIN_VALUE, spi_minval, CTLFLAG_RW, \
216 &key_spi_minval, 0, "");
218 /* maximun spi value to allocate automatically. */
219 SYSCTL_INT(_net_key, KEYCTL_SPI_MAX_VALUE, spi_maxval, CTLFLAG_RW, \
220 &key_spi_maxval, 0, "");
222 /* interval to initialize randseed */
223 SYSCTL_INT(_net_key, KEYCTL_RANDOM_INT, int_random, CTLFLAG_RW, \
224 &key_int_random, 0, "");
226 /* lifetime for larval SA */
227 SYSCTL_INT(_net_key, KEYCTL_LARVAL_LIFETIME, larval_lifetime, CTLFLAG_RW, \
228 &key_larval_lifetime, 0, "");
230 /* counter for blocking to send SADB_ACQUIRE to IKEd */
231 SYSCTL_INT(_net_key, KEYCTL_BLOCKACQ_COUNT, blockacq_count, CTLFLAG_RW, \
232 &key_blockacq_count, 0, "");
234 /* lifetime for blocking to send SADB_ACQUIRE to IKEd */
235 SYSCTL_INT(_net_key, KEYCTL_BLOCKACQ_LIFETIME, blockacq_lifetime, CTLFLAG_RW, \
236 &key_blockacq_lifetime, 0, "");
239 SYSCTL_INT(_net_key, KEYCTL_ESP_AUTH, esp_auth, CTLFLAG_RW, \
240 &ipsec_esp_auth, 0, "");
242 /* minimum ESP key length */
243 SYSCTL_INT(_net_key, KEYCTL_ESP_KEYMIN, esp_keymin, CTLFLAG_RW, \
244 &ipsec_esp_keymin, 0, "");
246 /* minimum AH key length */
247 SYSCTL_INT(_net_key, KEYCTL_AH_KEYMIN, ah_keymin, CTLFLAG_RW, \
248 &ipsec_ah_keymin, 0, "");
250 /* perfered old SA rather than new SA */
251 SYSCTL_INT(_net_key, KEYCTL_PREFERED_OLDSA, prefered_oldsa, CTLFLAG_RW,\
252 &key_prefered_oldsa, 0, "");
255 #define LIST_FOREACH(elm, head, field) \
256 for (elm = LIST_FIRST(head); elm; elm = LIST_NEXT(elm, field))
258 #define __LIST_CHAINED(elm) \
259 (!((elm)->chain.le_next == NULL && (elm)->chain.le_prev == NULL))
260 #define LIST_INSERT_TAIL(head, elm, type, field) \
262 struct type *curelm = LIST_FIRST(head); \
263 if (curelm == NULL) {\
264 LIST_INSERT_HEAD(head, elm, field); \
266 while (LIST_NEXT(curelm, field)) \
267 curelm = LIST_NEXT(curelm, field);\
268 LIST_INSERT_AFTER(curelm, elm, field);\
272 #define KEY_CHKSASTATE(head, sav, name) \
274 if ((head) != (sav)) { \
275 ipseclog((LOG_DEBUG, "%s: state mismatched (TREE=%d SA=%d)\n", \
276 (name), (head), (sav))); \
281 #define KEY_CHKSPDIR(head, sp, name) \
283 if ((head) != (sp)) { \
284 ipseclog((LOG_DEBUG, "%s: direction mismatched (TREE=%d SP=%d), " \
285 "anyway continue.\n", \
286 (name), (head), (sp))); \
290 MALLOC_DEFINE(M_SECA, "key mgmt", "security associations, key management");
293 #define KMALLOC(p, t, n) \
294 ((p) = (t) malloc((unsigned long)(n), M_SECA, M_INTWAIT | M_NULLOK))
296 free((caddr_t)(p), M_SECA)
298 #define KMALLOC(p, t, n) \
300 ((p) = (t)malloc((unsigned long)(n), M_SECA, M_INTWAIT | M_NULLOK)); \
301 printf("%s %d: %p <- KMALLOC(%s, %d)\n", \
302 __FILE__, __LINE__, (p), #t, n); \
307 printf("%s %d: %p -> KFREE()\n", __FILE__, __LINE__, (p)); \
308 free((caddr_t)(p), M_SECA); \
313 * set parameters into secpolicyindex buffer.
314 * Must allocate secpolicyindex buffer passed to this function.
316 #define KEY_SETSECSPIDX(_dir, s, d, ps, pd, ulp, idx) \
318 bzero((idx), sizeof(struct secpolicyindex)); \
319 (idx)->dir = (_dir); \
320 (idx)->prefs = (ps); \
321 (idx)->prefd = (pd); \
322 (idx)->ul_proto = (ulp); \
323 bcopy((s), &(idx)->src, ((const struct sockaddr *)(s))->sa_len); \
324 bcopy((d), &(idx)->dst, ((const struct sockaddr *)(d))->sa_len); \
328 * set parameters into secasindex buffer.
329 * Must allocate secasindex buffer before calling this function.
331 #define KEY_SETSECASIDX(p, m, r, s, d, idx) \
333 bzero((idx), sizeof(struct secasindex)); \
334 (idx)->proto = (p); \
336 (idx)->reqid = (r); \
337 bcopy((s), &(idx)->src, ((const struct sockaddr *)(s))->sa_len); \
338 bcopy((d), &(idx)->dst, ((const struct sockaddr *)(d))->sa_len); \
343 u_long getspi_count; /* the avarage of count to try to get new SPI */
347 struct sadb_msg *msg;
348 struct sadb_ext *ext[SADB_EXT_MAX + 1];
349 int extoff[SADB_EXT_MAX + 1];
350 int extlen[SADB_EXT_MAX + 1];
353 static struct secasvar *key_allocsa_policy (const struct secasindex *);
354 static void key_freesp_so (struct secpolicy **);
355 static struct secasvar *key_do_allocsa_policy (struct secashead *, u_int);
356 static void key_delsp (struct secpolicy *);
357 static struct secpolicy *key_getsp (struct secpolicyindex *);
358 static struct secpolicy *key_getspbyid (u_int32_t);
359 static u_int32_t key_newreqid (void);
360 static struct mbuf *key_gather_mbuf (struct mbuf *,
361 const struct sadb_msghdr *, int, int, ...);
362 static int key_spdadd (struct socket *, struct mbuf *,
363 const struct sadb_msghdr *);
364 static u_int32_t key_getnewspid (void);
365 static int key_spddelete (struct socket *, struct mbuf *,
366 const struct sadb_msghdr *);
367 static int key_spddelete2 (struct socket *, struct mbuf *,
368 const struct sadb_msghdr *);
369 static int key_spdget (struct socket *, struct mbuf *,
370 const struct sadb_msghdr *);
371 static int key_spdflush (struct socket *, struct mbuf *,
372 const struct sadb_msghdr *);
373 static int key_spddump (struct socket *, struct mbuf *,
374 const struct sadb_msghdr *);
375 static struct mbuf *key_setdumpsp (struct secpolicy *,
376 u_int8_t, u_int32_t, u_int32_t);
377 static u_int key_getspreqmsglen (struct secpolicy *);
378 static int key_spdexpire (struct secpolicy *);
379 static struct secashead *key_newsah (struct secasindex *);
380 static void key_delsah (struct secashead *);
381 static struct secasvar *key_newsav (struct mbuf *,
382 const struct sadb_msghdr *, struct secashead *, int *,
384 #define KEY_NEWSAV(m, sadb, sah, e) \
385 key_newsav(m, sadb, sah, e, __FILE__, __LINE__)
386 static void key_delsav (struct secasvar *);
387 static struct secashead *key_getsah (struct secasindex *);
388 static struct secasvar *key_checkspidup (struct secasindex *, u_int32_t);
389 static struct secasvar *key_getsavbyspi (struct secashead *, u_int32_t);
390 static int key_setsaval (struct secasvar *, struct mbuf *,
391 const struct sadb_msghdr *);
392 static int key_mature (struct secasvar *);
393 static struct mbuf *key_setdumpsa (struct secasvar *, u_int8_t,
394 u_int8_t, u_int32_t, u_int32_t);
395 static struct mbuf *key_setsadbmsg (u_int8_t, u_int16_t, u_int8_t,
396 u_int32_t, pid_t, u_int16_t);
397 static struct mbuf *key_setsadbsa (struct secasvar *);
398 static struct mbuf *key_setsadbaddr (u_int16_t,
399 const struct sockaddr *, u_int8_t, u_int16_t);
401 static struct mbuf *key_setsadbident (u_int16_t, u_int16_t, caddr_t,
404 static struct mbuf *key_setsadbxsa2 (u_int8_t, u_int32_t, u_int32_t);
405 static struct mbuf *key_setsadbxpolicy (u_int16_t, u_int8_t,
407 static void *key_newbuf (const void *, u_int);
409 static int key_ismyaddr6 (struct sockaddr_in6 *);
412 /* flags for key_cmpsaidx() */
413 #define CMP_HEAD 1 /* protocol, addresses. */
414 #define CMP_MODE_REQID 2 /* additionally HEAD, reqid, mode. */
415 #define CMP_REQID 3 /* additionally HEAD, reaid. */
416 #define CMP_EXACTLY 4 /* all elements. */
417 static int key_cmpsaidx
418 (const struct secasindex *, const struct secasindex *, int);
420 static int key_cmpspidx_exactly
421 (struct secpolicyindex *, struct secpolicyindex *);
422 static int key_cmpspidx_withmask
423 (struct secpolicyindex *, struct secpolicyindex *);
424 static int key_sockaddrcmp (const struct sockaddr *, const struct sockaddr *, int);
425 static int key_bbcmp (const void *, const void *, u_int);
426 static void key_srandom (void);
427 static u_int16_t key_satype2proto (u_int8_t);
428 static u_int8_t key_proto2satype (u_int16_t);
430 static int key_getspi (struct socket *, struct mbuf *,
431 const struct sadb_msghdr *);
432 static u_int32_t key_do_getnewspi (struct sadb_spirange *,
433 struct secasindex *);
434 static int key_update (struct socket *, struct mbuf *,
435 const struct sadb_msghdr *);
436 #ifdef IPSEC_DOSEQCHECK
437 static struct secasvar *key_getsavbyseq (struct secashead *, u_int32_t);
439 static int key_add (struct socket *, struct mbuf *,
440 const struct sadb_msghdr *);
441 static int key_setident (struct secashead *, struct mbuf *,
442 const struct sadb_msghdr *);
443 static struct mbuf *key_getmsgbuf_x1 (struct mbuf *,
444 const struct sadb_msghdr *);
445 static int key_delete (struct socket *, struct mbuf *,
446 const struct sadb_msghdr *);
447 static int key_get (struct socket *, struct mbuf *,
448 const struct sadb_msghdr *);
450 static void key_getcomb_setlifetime (struct sadb_comb *);
451 static struct mbuf *key_getcomb_esp (void);
452 static struct mbuf *key_getcomb_ah (void);
453 static struct mbuf *key_getcomb_ipcomp (void);
454 static struct mbuf *key_getprop (const struct secasindex *);
456 static int key_acquire (const struct secasindex *, struct secpolicy *);
457 #ifndef IPSEC_NONBLOCK_ACQUIRE
458 static struct secacq *key_newacq (const struct secasindex *);
459 static struct secacq *key_getacq (const struct secasindex *);
460 static struct secacq *key_getacqbyseq (u_int32_t);
462 static struct secspacq *key_newspacq (struct secpolicyindex *);
463 static struct secspacq *key_getspacq (struct secpolicyindex *);
464 static int key_acquire2 (struct socket *, struct mbuf *,
465 const struct sadb_msghdr *);
466 static int key_register (struct socket *, struct mbuf *,
467 const struct sadb_msghdr *);
468 static int key_expire (struct secasvar *);
469 static int key_flush (struct socket *, struct mbuf *,
470 const struct sadb_msghdr *);
471 static int key_dump (struct socket *, struct mbuf *,
472 const struct sadb_msghdr *);
473 static int key_promisc (struct socket *, struct mbuf *,
474 const struct sadb_msghdr *);
475 static int key_senderror (struct socket *, struct mbuf *, int);
476 static int key_validate_ext (const struct sadb_ext *, int);
477 static int key_align (struct mbuf *, struct sadb_msghdr *);
479 static const char *key_getfqdn (void);
480 static const char *key_getuserfqdn (void);
482 static void key_sa_chgstate (struct secasvar *, u_int8_t);
483 static struct mbuf *key_alloc_mbuf (int);
485 #define SA_ADDREF(p) do { \
487 KASSERT((p)->refcnt != 0, \
488 ("SA refcnt overflow at %s:%u", __FILE__, __LINE__)); \
490 #define SA_DELREF(p) do { \
491 KASSERT((p)->refcnt > 0, \
492 ("SA refcnt underflow at %s:%u", __FILE__, __LINE__)); \
496 #define SP_ADDREF(p) do { \
498 KASSERT((p)->refcnt != 0, \
499 ("SP refcnt overflow at %s:%u", __FILE__, __LINE__)); \
501 #define SP_DELREF(p) do { \
502 KASSERT((p)->refcnt > 0, \
503 ("SP refcnt underflow at %s:%u", __FILE__, __LINE__)); \
508 * Return 0 when there are known to be no SP's for the specified
509 * direction. Otherwise return 1. This is used by IPsec code
510 * to optimize performance.
513 key_havesp(u_int dir)
515 return (dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND ?
516 LIST_FIRST(&sptree[dir]) != NULL : 1);
519 /* %%% IPsec policy management */
521 * allocating a SP for OUTBOUND or INBOUND packet.
522 * Must call key_freesp() later.
523 * OUT: NULL: not found
524 * others: found and return the pointer.
527 key_allocsp(struct secpolicyindex *spidx, u_int dir, const char* where, int tag)
529 struct secpolicy *sp;
532 KASSERT(spidx != NULL, ("key_allocsp: null spidx"));
533 KASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
534 ("key_allocsp: invalid direction %u", dir));
536 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
537 printf("DP key_allocsp from %s:%u\n", where, tag));
540 s = splnet(); /*called from softclock()*/
541 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
542 printf("*** objects\n");
543 kdebug_secpolicyindex(spidx));
545 LIST_FOREACH(sp, &sptree[dir], chain) {
546 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
547 printf("*** in SPD\n");
548 kdebug_secpolicyindex(&sp->spidx));
550 if (sp->state == IPSEC_SPSTATE_DEAD)
552 if (key_cmpspidx_withmask(&sp->spidx, spidx))
559 KEY_CHKSPDIR(sp->spidx.dir, dir, "key_allocsp");
561 /* found a SPD entry */
562 sp->lastused = time_second;
567 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
568 printf("DP key_allocsp return SP:%p (ID=%u) refcnt %u\n",
569 sp, sp ? sp->id : 0, sp ? sp->refcnt : 0));
574 * allocating a SP for OUTBOUND or INBOUND packet.
575 * Must call key_freesp() later.
576 * OUT: NULL: not found
577 * others: found and return the pointer.
580 key_allocsp2(u_int32_t spi,
581 union sockaddr_union *dst,
584 const char* where, int tag)
586 struct secpolicy *sp;
589 KASSERT(dst != NULL, ("key_allocsp2: null dst"));
590 KASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
591 ("key_allocsp2: invalid direction %u", dir));
593 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
594 printf("DP key_allocsp2 from %s:%u\n", where, tag));
597 s = splnet(); /*called from softclock()*/
598 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
599 printf("*** objects\n");
600 printf("spi %u proto %u dir %u\n", spi, proto, dir);
601 kdebug_sockaddr(&dst->sa));
603 LIST_FOREACH(sp, &sptree[dir], chain) {
604 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
605 printf("*** in SPD\n");
606 kdebug_secpolicyindex(&sp->spidx));
608 if (sp->state == IPSEC_SPSTATE_DEAD)
610 /* compare simple values, then dst address */
611 if (sp->spidx.ul_proto != proto)
613 /* NB: spi's must exist and match */
614 if (!sp->req || !sp->req->sav || sp->req->sav->spi != spi)
616 if (key_sockaddrcmp(&sp->spidx.dst.sa, &dst->sa, 1) == 0)
623 KEY_CHKSPDIR(sp->spidx.dir, dir, "key_allocsp2");
625 /* found a SPD entry */
626 sp->lastused = time_second;
631 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
632 printf("DP key_allocsp2 return SP:%p (ID=%u) refcnt %u\n",
633 sp, sp ? sp->id : 0, sp ? sp->refcnt : 0));
638 * return a policy that matches this particular inbound packet.
642 key_gettunnel(const struct sockaddr *osrc,
643 const struct sockaddr *odst,
644 const struct sockaddr *isrc,
645 const struct sockaddr *idst,
646 const char* where, int tag)
648 struct secpolicy *sp;
649 const int dir = IPSEC_DIR_INBOUND;
651 struct ipsecrequest *r1, *r2, *p;
652 struct secpolicyindex spidx;
654 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
655 printf("DP key_gettunnel from %s:%u\n", where, tag));
657 if (isrc->sa_family != idst->sa_family) {
658 ipseclog((LOG_ERR, "protocol family mismatched %d != %d\n.",
659 isrc->sa_family, idst->sa_family));
664 s = splnet(); /*called from softclock()*/
665 LIST_FOREACH(sp, &sptree[dir], chain) {
666 if (sp->state == IPSEC_SPSTATE_DEAD)
670 for (p = sp->req; p; p = p->next) {
671 if (p->saidx.mode != IPSEC_MODE_TUNNEL)
678 /* here we look at address matches only */
680 if (isrc->sa_len > sizeof(spidx.src) ||
681 idst->sa_len > sizeof(spidx.dst))
683 bcopy(isrc, &spidx.src, isrc->sa_len);
684 bcopy(idst, &spidx.dst, idst->sa_len);
685 if (!key_cmpspidx_withmask(&sp->spidx, &spidx))
688 if (key_sockaddrcmp(&r1->saidx.src.sa, isrc, 0) ||
689 key_sockaddrcmp(&r1->saidx.dst.sa, idst, 0))
693 if (key_sockaddrcmp(&r2->saidx.src.sa, osrc, 0) ||
694 key_sockaddrcmp(&r2->saidx.dst.sa, odst, 0))
703 sp->lastused = time_second;
708 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
709 printf("DP key_gettunnel return SP:%p (ID=%u) refcnt %u\n",
710 sp, sp ? sp->id : 0, sp ? sp->refcnt : 0));
715 * allocating an SA entry for an *OUTBOUND* packet.
716 * checking each request entries in SP, and acquire an SA if need.
717 * OUT: 0: there are valid requests.
718 * ENOENT: policy may be valid, but SA with REQUIRE is on acquiring.
721 key_checkrequest(struct ipsecrequest *isr, const struct secasindex *saidx)
726 KASSERT(isr != NULL, ("key_checkrequest: null isr"));
727 KASSERT(saidx != NULL, ("key_checkrequest: null saidx"));
728 KASSERT(saidx->mode == IPSEC_MODE_TRANSPORT ||
729 saidx->mode == IPSEC_MODE_TUNNEL,
730 ("key_checkrequest: unexpected policy %u", saidx->mode));
732 /* get current level */
733 level = ipsec_get_reqlevel(isr);
736 * XXX guard against protocol callbacks from the crypto
737 * thread as they reference ipsecrequest.sav which we
738 * temporarily null out below. Need to rethink how we
739 * handle bundled SA's in the callback thread.
741 SPLASSERT(net, "key_checkrequest");
744 * We do allocate new SA only if the state of SA in the holder is
745 * SADB_SASTATE_DEAD. The SA for outbound must be the oldest.
747 if (isr->sav != NULL) {
748 if (isr->sav->sah == NULL)
749 panic("key_checkrequest: sah is null.\n");
750 if (isr->sav == (struct secasvar *)LIST_FIRST(
751 &isr->sav->sah->savtree[SADB_SASTATE_DEAD])) {
752 KEY_FREESAV(&isr->sav);
758 * we free any SA stashed in the IPsec request because a different
759 * SA may be involved each time this request is checked, either
760 * because new SAs are being configured, or this request is
761 * associated with an unconnected datagram socket, or this request
762 * is associated with a system default policy.
764 * The operation may have negative impact to performance. We may
765 * want to check cached SA carefully, rather than picking new SA
768 if (isr->sav != NULL) {
769 KEY_FREESAV(&isr->sav);
775 * new SA allocation if no SA found.
776 * key_allocsa_policy should allocate the oldest SA available.
777 * See key_do_allocsa_policy(), and draft-jenkins-ipsec-rekeying-03.txt.
779 if (isr->sav == NULL)
780 isr->sav = key_allocsa_policy(saidx);
782 /* When there is SA. */
783 if (isr->sav != NULL) {
784 if (isr->sav->state != SADB_SASTATE_MATURE &&
785 isr->sav->state != SADB_SASTATE_DYING)
791 error = key_acquire(saidx, isr->sp);
793 /* XXX What should I do ? */
794 ipseclog((LOG_DEBUG, "key_checkrequest: error %d returned "
795 "from key_acquire.\n", error));
799 if (level != IPSEC_LEVEL_REQUIRE) {
800 /* XXX sigh, the interface to this routine is botched */
801 KASSERT(isr->sav == NULL, ("key_checkrequest: unexpected SA"));
809 * allocating a SA for policy entry from SAD.
810 * NOTE: searching SAD of aliving state.
811 * OUT: NULL: not found.
812 * others: found and return the pointer.
814 static struct secasvar *
815 key_allocsa_policy(const struct secasindex *saidx)
817 struct secashead *sah;
818 struct secasvar *sav;
819 u_int stateidx, state;
821 LIST_FOREACH(sah, &sahtree, chain) {
822 if (sah->state == SADB_SASTATE_DEAD)
824 if (key_cmpsaidx(&sah->saidx, saidx, CMP_MODE_REQID))
832 /* search valid state */
834 stateidx < _ARRAYLEN(saorder_state_valid);
837 state = saorder_state_valid[stateidx];
839 sav = key_do_allocsa_policy(sah, state);
848 * searching SAD with direction, protocol, mode and state.
849 * called by key_allocsa_policy().
852 * others : found, pointer to a SA.
854 static struct secasvar *
855 key_do_allocsa_policy(struct secashead *sah, u_int state)
857 struct secasvar *sav, *nextsav, *candidate, *d;
862 for (sav = LIST_FIRST(&sah->savtree[state]);
866 nextsav = LIST_NEXT(sav, chain);
869 KEY_CHKSASTATE(sav->state, state, "key_do_allocsa_policy");
872 if (candidate == NULL) {
877 /* Which SA is the better ? */
880 if (candidate->lft_c == NULL || sav->lft_c == NULL)
881 panic("key_do_allocsa_policy: "
882 "lifetime_current is NULL.\n");
884 /* What the best method is to compare ? */
885 if (key_prefered_oldsa) {
886 if (candidate->lft_c->sadb_lifetime_addtime >
887 sav->lft_c->sadb_lifetime_addtime) {
894 /* prefered new sa rather than old sa */
895 if (candidate->lft_c->sadb_lifetime_addtime <
896 sav->lft_c->sadb_lifetime_addtime) {
903 * prepared to delete the SA when there is more
904 * suitable candidate and the lifetime of the SA is not
907 if (d->lft_c->sadb_lifetime_addtime != 0) {
908 struct mbuf *m, *result;
910 key_sa_chgstate(d, SADB_SASTATE_DEAD);
912 KASSERT(d->refcnt > 0,
913 ("key_do_allocsa_policy: bogus ref count"));
914 m = key_setsadbmsg(SADB_DELETE, 0,
915 d->sah->saidx.proto, 0, 0, d->refcnt - 1);
920 /* set sadb_address for saidx's. */
921 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
922 &d->sah->saidx.src.sa,
923 d->sah->saidx.src.sa.sa_len << 3,
929 /* set sadb_address for saidx's. */
930 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
931 &d->sah->saidx.src.sa,
932 d->sah->saidx.src.sa.sa_len << 3,
938 /* create SA extension */
939 m = key_setsadbsa(d);
944 if (result->m_len < sizeof(struct sadb_msg)) {
945 result = m_pullup(result,
946 sizeof(struct sadb_msg));
951 result->m_pkthdr.len = 0;
952 for (m = result; m; m = m->m_next)
953 result->m_pkthdr.len += m->m_len;
954 mtod(result, struct sadb_msg *)->sadb_msg_len =
955 PFKEY_UNIT64(result->m_pkthdr.len);
957 if (key_sendup_mbuf(NULL, result,
958 KEY_SENDUP_REGISTERED))
966 SA_ADDREF(candidate);
967 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
968 printf("DP allocsa_policy cause "
969 "refcnt++:%d SA:%p\n",
970 candidate->refcnt, candidate));
976 * allocating a usable SA entry for a *INBOUND* packet.
977 * Must call key_freesav() later.
978 * OUT: positive: pointer to a usable sav (i.e. MATURE or DYING state).
979 * NULL: not found, or error occured.
981 * In the comparison, no source address is used--for RFC2401 conformance.
982 * To quote, from section 4.1:
983 * A security association is uniquely identified by a triple consisting
984 * of a Security Parameter Index (SPI), an IP Destination Address, and a
985 * security protocol (AH or ESP) identifier.
986 * Note that, however, we do need to keep source address in IPsec SA.
987 * IKE specification and PF_KEY specification do assume that we
988 * keep source address in IPsec SA. We see a tricky situation here.
992 union sockaddr_union *dst,
995 const char* where, int tag)
997 struct secashead *sah;
998 struct secasvar *sav;
999 u_int stateidx, state;
1002 KASSERT(dst != NULL, ("key_allocsa: null dst address"));
1004 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1005 printf("DP key_allocsa from %s:%u\n", where, tag));
1009 * XXX: to be checked internal IP header somewhere. Also when
1010 * IPsec tunnel packet is received. But ESP tunnel mode is
1011 * encrypted so we can't check internal IP header.
1013 s = splnet(); /*called from softclock()*/
1014 LIST_FOREACH(sah, &sahtree, chain) {
1015 /* search valid state */
1017 stateidx < _ARRAYLEN(saorder_state_valid);
1019 state = saorder_state_valid[stateidx];
1020 LIST_FOREACH(sav, &sah->savtree[state], chain) {
1022 KEY_CHKSASTATE(sav->state, state, "key_allocsav");
1023 /* do not return entries w/ unusable state */
1024 if (sav->state != SADB_SASTATE_MATURE &&
1025 sav->state != SADB_SASTATE_DYING)
1027 if (proto != sav->sah->saidx.proto)
1029 if (spi != sav->spi)
1031 #if 0 /* don't check src */
1032 /* check src address */
1033 if (key_sockaddrcmp(&src->sa, &sav->sah->saidx.src.sa, 0) != 0)
1036 /* check dst address */
1037 if (key_sockaddrcmp(&dst->sa, &sav->sah->saidx.dst.sa, 0) != 0)
1048 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1049 printf("DP key_allocsa return SA:%p; refcnt %u\n",
1050 sav, sav ? sav->refcnt : 0));
1055 * Must be called after calling key_allocsp().
1056 * For both the packet without socket and key_freeso().
1059 _key_freesp(struct secpolicy **spp, const char* where, int tag)
1061 struct secpolicy *sp = *spp;
1063 KASSERT(sp != NULL, ("key_freesp: null sp"));
1067 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1068 printf("DP key_freesp SP:%p (ID=%u) from %s:%u; refcnt now %u\n",
1069 sp, sp->id, where, tag, sp->refcnt));
1071 if (sp->refcnt == 0) {
1078 * Must be called after calling key_allocsp().
1079 * For the packet with socket.
1082 key_freeso(struct socket *so)
1085 KASSERT(so != NULL, ("key_freeso: null so"));
1087 switch (so->so_proto->pr_domain->dom_family) {
1091 struct inpcb *pcb = sotoinpcb(so);
1093 /* Does it have a PCB ? */
1096 key_freesp_so(&pcb->inp_sp->sp_in);
1097 key_freesp_so(&pcb->inp_sp->sp_out);
1104 #ifdef HAVE_NRL_INPCB
1105 struct inpcb *pcb = sotoinpcb(so);
1107 /* Does it have a PCB ? */
1110 key_freesp_so(&pcb->inp_sp->sp_in);
1111 key_freesp_so(&pcb->inp_sp->sp_out);
1113 struct in6pcb *pcb = sotoin6pcb(so);
1115 /* Does it have a PCB ? */
1118 key_freesp_so(&pcb->in6p_sp->sp_in);
1119 key_freesp_so(&pcb->in6p_sp->sp_out);
1125 ipseclog((LOG_DEBUG, "key_freeso: unknown address family=%d.\n",
1126 so->so_proto->pr_domain->dom_family));
1132 key_freesp_so(struct secpolicy **sp)
1134 KASSERT(sp != NULL && *sp != NULL, ("key_freesp_so: null sp"));
1136 if ((*sp)->policy == IPSEC_POLICY_ENTRUST ||
1137 (*sp)->policy == IPSEC_POLICY_BYPASS)
1140 KASSERT((*sp)->policy == IPSEC_POLICY_IPSEC,
1141 ("key_freesp_so: invalid policy %u", (*sp)->policy));
1146 * Must be called after calling key_allocsa().
1147 * This function is called by key_freesp() to free some SA allocated
1151 key_freesav(struct secasvar **psav, const char* where, int tag)
1153 struct secasvar *sav = *psav;
1155 KASSERT(sav != NULL, ("key_freesav: null sav"));
1159 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1160 printf("DP key_freesav SA:%p (SPI %lu) from %s:%u; refcnt now %u\n",
1161 sav, ntohl(sav->spi), where, tag, sav->refcnt));
1163 if (sav->refcnt == 0) {
1169 /* %%% SPD management */
1171 * free security policy entry.
1174 key_delsp(struct secpolicy *sp)
1178 KASSERT(sp != NULL, ("key_delsp: null sp"));
1180 sp->state = IPSEC_SPSTATE_DEAD;
1182 KASSERT(sp->refcnt == 0,
1183 ("key_delsp: SP with references deleted (refcnt %u)",
1186 s = splnet(); /*called from softclock()*/
1187 /* remove from SP index */
1188 if (__LIST_CHAINED(sp))
1189 LIST_REMOVE(sp, chain);
1192 struct ipsecrequest *isr = sp->req, *nextisr;
1194 while (isr != NULL) {
1195 if (isr->sav != NULL) {
1196 KEY_FREESAV(&isr->sav);
1200 nextisr = isr->next;
1213 * OUT: NULL : not found
1214 * others : found, pointer to a SP.
1216 static struct secpolicy *
1217 key_getsp(struct secpolicyindex *spidx)
1219 struct secpolicy *sp;
1221 KASSERT(spidx != NULL, ("key_getsp: null spidx"));
1223 LIST_FOREACH(sp, &sptree[spidx->dir], chain) {
1224 if (sp->state == IPSEC_SPSTATE_DEAD)
1226 if (key_cmpspidx_exactly(spidx, &sp->spidx)) {
1237 * OUT: NULL : not found
1238 * others : found, pointer to a SP.
1240 static struct secpolicy *
1241 key_getspbyid(u_int32_t id)
1243 struct secpolicy *sp;
1245 LIST_FOREACH(sp, &sptree[IPSEC_DIR_INBOUND], chain) {
1246 if (sp->state == IPSEC_SPSTATE_DEAD)
1254 LIST_FOREACH(sp, &sptree[IPSEC_DIR_OUTBOUND], chain) {
1255 if (sp->state == IPSEC_SPSTATE_DEAD)
1267 key_newsp(const char* where, int tag)
1269 struct secpolicy *newsp = NULL;
1271 newsp = malloc(sizeof(struct secpolicy), M_SECA,
1272 M_INTWAIT | M_ZERO | M_NULLOK);
1278 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1279 printf("DP key_newsp from %s:%u return SP:%p\n",
1280 where, tag, newsp));
1285 * create secpolicy structure from sadb_x_policy structure.
1286 * NOTE: `state', `secpolicyindex' in secpolicy structure are not set,
1287 * so must be set properly later.
1290 key_msg2sp(xpl0, len, error)
1291 struct sadb_x_policy *xpl0;
1295 struct secpolicy *newsp;
1299 panic("key_msg2sp: NULL pointer was passed.\n");
1300 if (len < sizeof(*xpl0))
1301 panic("key_msg2sp: invalid length.\n");
1302 if (len != PFKEY_EXTLEN(xpl0)) {
1303 ipseclog((LOG_DEBUG, "key_msg2sp: Invalid msg length.\n"));
1308 if ((newsp = KEY_NEWSP()) == NULL) {
1313 newsp->spidx.dir = xpl0->sadb_x_policy_dir;
1314 newsp->policy = xpl0->sadb_x_policy_type;
1317 switch (xpl0->sadb_x_policy_type) {
1318 case IPSEC_POLICY_DISCARD:
1319 case IPSEC_POLICY_NONE:
1320 case IPSEC_POLICY_ENTRUST:
1321 case IPSEC_POLICY_BYPASS:
1325 case IPSEC_POLICY_IPSEC:
1328 struct sadb_x_ipsecrequest *xisr;
1329 struct ipsecrequest **p_isr = &newsp->req;
1331 /* validity check */
1332 if (PFKEY_EXTLEN(xpl0) < sizeof(*xpl0)) {
1333 ipseclog((LOG_DEBUG,
1334 "key_msg2sp: Invalid msg length.\n"));
1340 tlen = PFKEY_EXTLEN(xpl0) - sizeof(*xpl0);
1341 xisr = (struct sadb_x_ipsecrequest *)(xpl0 + 1);
1345 if (xisr->sadb_x_ipsecrequest_len < sizeof(*xisr)) {
1346 ipseclog((LOG_DEBUG, "key_msg2sp: "
1347 "invalid ipsecrequest length.\n"));
1353 /* allocate request buffer */
1354 KMALLOC(*p_isr, struct ipsecrequest *, sizeof(**p_isr));
1355 if ((*p_isr) == NULL) {
1356 ipseclog((LOG_DEBUG,
1357 "key_msg2sp: No more memory.\n"));
1362 bzero(*p_isr, sizeof(**p_isr));
1365 (*p_isr)->next = NULL;
1367 switch (xisr->sadb_x_ipsecrequest_proto) {
1370 case IPPROTO_IPCOMP:
1373 ipseclog((LOG_DEBUG,
1374 "key_msg2sp: invalid proto type=%u\n",
1375 xisr->sadb_x_ipsecrequest_proto));
1377 *error = EPROTONOSUPPORT;
1380 (*p_isr)->saidx.proto = xisr->sadb_x_ipsecrequest_proto;
1382 switch (xisr->sadb_x_ipsecrequest_mode) {
1383 case IPSEC_MODE_TRANSPORT:
1384 case IPSEC_MODE_TUNNEL:
1386 case IPSEC_MODE_ANY:
1388 ipseclog((LOG_DEBUG,
1389 "key_msg2sp: invalid mode=%u\n",
1390 xisr->sadb_x_ipsecrequest_mode));
1395 (*p_isr)->saidx.mode = xisr->sadb_x_ipsecrequest_mode;
1397 switch (xisr->sadb_x_ipsecrequest_level) {
1398 case IPSEC_LEVEL_DEFAULT:
1399 case IPSEC_LEVEL_USE:
1400 case IPSEC_LEVEL_REQUIRE:
1402 case IPSEC_LEVEL_UNIQUE:
1403 /* validity check */
1405 * If range violation of reqid, kernel will
1406 * update it, don't refuse it.
1408 if (xisr->sadb_x_ipsecrequest_reqid
1409 > IPSEC_MANUAL_REQID_MAX) {
1410 ipseclog((LOG_DEBUG,
1411 "key_msg2sp: reqid=%d range "
1412 "violation, updated by kernel.\n",
1413 xisr->sadb_x_ipsecrequest_reqid));
1414 xisr->sadb_x_ipsecrequest_reqid = 0;
1417 /* allocate new reqid id if reqid is zero. */
1418 if (xisr->sadb_x_ipsecrequest_reqid == 0) {
1420 if ((reqid = key_newreqid()) == 0) {
1425 (*p_isr)->saidx.reqid = reqid;
1426 xisr->sadb_x_ipsecrequest_reqid = reqid;
1428 /* set it for manual keying. */
1429 (*p_isr)->saidx.reqid =
1430 xisr->sadb_x_ipsecrequest_reqid;
1435 ipseclog((LOG_DEBUG, "key_msg2sp: invalid level=%u\n",
1436 xisr->sadb_x_ipsecrequest_level));
1441 (*p_isr)->level = xisr->sadb_x_ipsecrequest_level;
1443 /* set IP addresses if there */
1444 if (xisr->sadb_x_ipsecrequest_len > sizeof(*xisr)) {
1445 struct sockaddr *paddr;
1447 paddr = (struct sockaddr *)(xisr + 1);
1449 /* validity check */
1451 > sizeof((*p_isr)->saidx.src)) {
1452 ipseclog((LOG_DEBUG, "key_msg2sp: invalid request "
1453 "address length.\n"));
1458 bcopy(paddr, &(*p_isr)->saidx.src,
1461 paddr = (struct sockaddr *)((caddr_t)paddr
1464 /* validity check */
1466 > sizeof((*p_isr)->saidx.dst)) {
1467 ipseclog((LOG_DEBUG, "key_msg2sp: invalid request "
1468 "address length.\n"));
1473 bcopy(paddr, &(*p_isr)->saidx.dst,
1477 (*p_isr)->sav = NULL;
1478 (*p_isr)->sp = newsp;
1480 /* initialization for the next. */
1481 p_isr = &(*p_isr)->next;
1482 tlen -= xisr->sadb_x_ipsecrequest_len;
1484 /* validity check */
1486 ipseclog((LOG_DEBUG, "key_msg2sp: becoming tlen < 0.\n"));
1492 xisr = (struct sadb_x_ipsecrequest *)((caddr_t)xisr
1493 + xisr->sadb_x_ipsecrequest_len);
1498 ipseclog((LOG_DEBUG, "key_msg2sp: invalid policy type.\n"));
1511 static u_int32_t auto_reqid = IPSEC_MANUAL_REQID_MAX + 1;
1513 auto_reqid = (auto_reqid == ~0
1514 ? IPSEC_MANUAL_REQID_MAX + 1 : auto_reqid + 1);
1516 /* XXX should be unique check */
1522 * copy secpolicy struct to sadb_x_policy structure indicated.
1526 struct secpolicy *sp;
1528 struct sadb_x_policy *xpl;
1535 panic("key_sp2msg: NULL pointer was passed.\n");
1537 tlen = key_getspreqmsglen(sp);
1539 m = key_alloc_mbuf(tlen);
1540 if (!m || m->m_next) { /*XXX*/
1548 xpl = mtod(m, struct sadb_x_policy *);
1551 xpl->sadb_x_policy_len = PFKEY_UNIT64(tlen);
1552 xpl->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
1553 xpl->sadb_x_policy_type = sp->policy;
1554 xpl->sadb_x_policy_dir = sp->spidx.dir;
1555 xpl->sadb_x_policy_id = sp->id;
1556 p = (caddr_t)xpl + sizeof(*xpl);
1558 /* if is the policy for ipsec ? */
1559 if (sp->policy == IPSEC_POLICY_IPSEC) {
1560 struct sadb_x_ipsecrequest *xisr;
1561 struct ipsecrequest *isr;
1563 for (isr = sp->req; isr != NULL; isr = isr->next) {
1565 xisr = (struct sadb_x_ipsecrequest *)p;
1567 xisr->sadb_x_ipsecrequest_proto = isr->saidx.proto;
1568 xisr->sadb_x_ipsecrequest_mode = isr->saidx.mode;
1569 xisr->sadb_x_ipsecrequest_level = isr->level;
1570 xisr->sadb_x_ipsecrequest_reqid = isr->saidx.reqid;
1573 bcopy(&isr->saidx.src, p, isr->saidx.src.sa.sa_len);
1574 p += isr->saidx.src.sa.sa_len;
1575 bcopy(&isr->saidx.dst, p, isr->saidx.dst.sa.sa_len);
1576 p += isr->saidx.src.sa.sa_len;
1578 xisr->sadb_x_ipsecrequest_len =
1579 PFKEY_ALIGN8(sizeof(*xisr)
1580 + isr->saidx.src.sa.sa_len
1581 + isr->saidx.dst.sa.sa_len);
1588 /* m will not be freed nor modified */
1589 static struct mbuf *
1590 key_gather_mbuf(struct mbuf *m, const struct sadb_msghdr *mhp,
1591 int ndeep, int nitem, ...)
1596 struct mbuf *result = NULL, *n;
1599 if (m == NULL || mhp == NULL)
1600 panic("null pointer passed to key_gather");
1602 __va_start(ap, nitem);
1603 for (i = 0; i < nitem; i++) {
1604 idx = __va_arg(ap, int);
1605 if (idx < 0 || idx > SADB_EXT_MAX)
1607 /* don't attempt to pull empty extension */
1608 if (idx == SADB_EXT_RESERVED && mhp->msg == NULL)
1610 if (idx != SADB_EXT_RESERVED &&
1611 (mhp->ext[idx] == NULL || mhp->extlen[idx] == 0))
1614 if (idx == SADB_EXT_RESERVED) {
1615 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
1618 panic("assumption failed");
1620 MGETHDR(n, M_DONTWAIT, MT_DATA);
1625 m_copydata(m, 0, sizeof(struct sadb_msg),
1627 } else if (i < ndeep) {
1628 len = mhp->extlen[idx];
1629 n = key_alloc_mbuf(len);
1630 if (!n || n->m_next) { /*XXX*/
1635 m_copydata(m, mhp->extoff[idx], mhp->extlen[idx],
1638 n = m_copym(m, mhp->extoff[idx], mhp->extlen[idx],
1651 if ((result->m_flags & M_PKTHDR) != 0) {
1652 result->m_pkthdr.len = 0;
1653 for (n = result; n; n = n->m_next)
1654 result->m_pkthdr.len += n->m_len;
1665 * SADB_X_SPDADD, SADB_X_SPDSETIDX or SADB_X_SPDUPDATE processing
1666 * add an entry to SP database, when received
1667 * <base, address(SD), (lifetime(H),) policy>
1669 * Adding to SP database,
1671 * <base, address(SD), (lifetime(H),) policy>
1672 * to the socket which was send.
1674 * SPDADD set a unique policy entry.
1675 * SPDSETIDX like SPDADD without a part of policy requests.
1676 * SPDUPDATE replace a unique policy entry.
1678 * m will always be freed.
1681 key_spdadd(so, m, mhp)
1684 const struct sadb_msghdr *mhp;
1686 struct sadb_address *src0, *dst0;
1687 struct sadb_x_policy *xpl0, *xpl;
1688 struct sadb_lifetime *lft = NULL;
1689 struct secpolicyindex spidx;
1690 struct secpolicy *newsp;
1694 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
1695 panic("key_spdadd: NULL pointer is passed.\n");
1697 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
1698 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
1699 mhp->ext[SADB_X_EXT_POLICY] == NULL) {
1700 ipseclog((LOG_DEBUG, "key_spdadd: invalid message is passed.\n"));
1701 return key_senderror(so, m, EINVAL);
1703 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
1704 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
1705 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
1706 ipseclog((LOG_DEBUG, "key_spdadd: invalid message is passed.\n"));
1707 return key_senderror(so, m, EINVAL);
1709 if (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL) {
1710 if (mhp->extlen[SADB_EXT_LIFETIME_HARD]
1711 < sizeof(struct sadb_lifetime)) {
1712 ipseclog((LOG_DEBUG, "key_spdadd: invalid message is passed.\n"));
1713 return key_senderror(so, m, EINVAL);
1715 lft = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_HARD];
1718 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
1719 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
1720 xpl0 = (struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY];
1723 /* XXX boundary check against sa_len */
1724 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
1727 src0->sadb_address_prefixlen,
1728 dst0->sadb_address_prefixlen,
1729 src0->sadb_address_proto,
1732 /* checking the direciton. */
1733 switch (xpl0->sadb_x_policy_dir) {
1734 case IPSEC_DIR_INBOUND:
1735 case IPSEC_DIR_OUTBOUND:
1738 ipseclog((LOG_DEBUG, "key_spdadd: Invalid SP direction.\n"));
1739 mhp->msg->sadb_msg_errno = EINVAL;
1744 /* key_spdadd() accepts DISCARD, NONE and IPSEC. */
1745 if (xpl0->sadb_x_policy_type == IPSEC_POLICY_ENTRUST
1746 || xpl0->sadb_x_policy_type == IPSEC_POLICY_BYPASS) {
1747 ipseclog((LOG_DEBUG, "key_spdadd: Invalid policy type.\n"));
1748 return key_senderror(so, m, EINVAL);
1751 /* policy requests are mandatory when action is ipsec. */
1752 if (mhp->msg->sadb_msg_type != SADB_X_SPDSETIDX
1753 && xpl0->sadb_x_policy_type == IPSEC_POLICY_IPSEC
1754 && mhp->extlen[SADB_X_EXT_POLICY] <= sizeof(*xpl0)) {
1755 ipseclog((LOG_DEBUG, "key_spdadd: some policy requests part required.\n"));
1756 return key_senderror(so, m, EINVAL);
1760 * checking there is SP already or not.
1761 * SPDUPDATE doesn't depend on whether there is a SP or not.
1762 * If the type is either SPDADD or SPDSETIDX AND a SP is found,
1765 newsp = key_getsp(&spidx);
1766 if (mhp->msg->sadb_msg_type == SADB_X_SPDUPDATE) {
1768 newsp->state = IPSEC_SPSTATE_DEAD;
1772 if (newsp != NULL) {
1774 ipseclog((LOG_DEBUG, "key_spdadd: a SP entry exists already.\n"));
1775 return key_senderror(so, m, EEXIST);
1779 /* allocation new SP entry */
1780 if ((newsp = key_msg2sp(xpl0, PFKEY_EXTLEN(xpl0), &error)) == NULL) {
1781 return key_senderror(so, m, error);
1784 if ((newsp->id = key_getnewspid()) == 0) {
1786 return key_senderror(so, m, ENOBUFS);
1789 /* XXX boundary check against sa_len */
1790 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
1793 src0->sadb_address_prefixlen,
1794 dst0->sadb_address_prefixlen,
1795 src0->sadb_address_proto,
1798 /* sanity check on addr pair */
1799 if (((struct sockaddr *)(src0 + 1))->sa_family !=
1800 ((struct sockaddr *)(dst0+ 1))->sa_family) {
1802 return key_senderror(so, m, EINVAL);
1804 if (((struct sockaddr *)(src0 + 1))->sa_len !=
1805 ((struct sockaddr *)(dst0+ 1))->sa_len) {
1807 return key_senderror(so, m, EINVAL);
1810 if (newsp->req && newsp->req->saidx.src.sa.sa_family) {
1811 struct sockaddr *sa;
1812 sa = (struct sockaddr *)(src0 + 1);
1813 if (sa->sa_family != newsp->req->saidx.src.sa.sa_family) {
1815 return key_senderror(so, m, EINVAL);
1818 if (newsp->req && newsp->req->saidx.dst.sa.sa_family) {
1819 struct sockaddr *sa;
1820 sa = (struct sockaddr *)(dst0 + 1);
1821 if (sa->sa_family != newsp->req->saidx.dst.sa.sa_family) {
1823 return key_senderror(so, m, EINVAL);
1828 newsp->created = time_second;
1829 newsp->lastused = newsp->created;
1830 newsp->lifetime = lft ? lft->sadb_lifetime_addtime : 0;
1831 newsp->validtime = lft ? lft->sadb_lifetime_usetime : 0;
1833 newsp->refcnt = 1; /* do not reclaim until I say I do */
1834 newsp->state = IPSEC_SPSTATE_ALIVE;
1835 LIST_INSERT_TAIL(&sptree[newsp->spidx.dir], newsp, secpolicy, chain);
1837 /* delete the entry in spacqtree */
1838 if (mhp->msg->sadb_msg_type == SADB_X_SPDUPDATE) {
1839 struct secspacq *spacq;
1840 if ((spacq = key_getspacq(&spidx)) != NULL) {
1841 /* reset counter in order to deletion by timehandler. */
1842 spacq->created = time_second;
1848 struct mbuf *n, *mpolicy;
1849 struct sadb_msg *newmsg;
1852 /* create new sadb_msg to reply. */
1854 n = key_gather_mbuf(m, mhp, 2, 5, SADB_EXT_RESERVED,
1855 SADB_X_EXT_POLICY, SADB_EXT_LIFETIME_HARD,
1856 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
1858 n = key_gather_mbuf(m, mhp, 2, 4, SADB_EXT_RESERVED,
1860 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
1863 return key_senderror(so, m, ENOBUFS);
1865 if (n->m_len < sizeof(*newmsg)) {
1866 n = m_pullup(n, sizeof(*newmsg));
1868 return key_senderror(so, m, ENOBUFS);
1870 newmsg = mtod(n, struct sadb_msg *);
1871 newmsg->sadb_msg_errno = 0;
1872 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
1875 mpolicy = m_pulldown(n, PFKEY_ALIGN8(sizeof(struct sadb_msg)),
1876 sizeof(*xpl), &off);
1877 if (mpolicy == NULL) {
1878 /* n is already freed */
1879 return key_senderror(so, m, ENOBUFS);
1881 xpl = (struct sadb_x_policy *)(mtod(mpolicy, caddr_t) + off);
1882 if (xpl->sadb_x_policy_exttype != SADB_X_EXT_POLICY) {
1884 return key_senderror(so, m, EINVAL);
1886 xpl->sadb_x_policy_id = newsp->id;
1889 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
1894 * get new policy id.
1902 u_int32_t newid = 0;
1903 int count = key_spi_trycnt; /* XXX */
1904 struct secpolicy *sp;
1906 /* when requesting to allocate spi ranged */
1908 newid = (policy_id = (policy_id == ~0 ? 1 : policy_id + 1));
1910 if ((sp = key_getspbyid(newid)) == NULL)
1916 if (count == 0 || newid == 0) {
1917 ipseclog((LOG_DEBUG, "key_getnewspid: to allocate policy id is failed.\n"));
1925 * SADB_SPDDELETE processing
1927 * <base, address(SD), policy(*)>
1928 * from the user(?), and set SADB_SASTATE_DEAD,
1930 * <base, address(SD), policy(*)>
1932 * policy(*) including direction of policy.
1934 * m will always be freed.
1937 key_spddelete(so, m, mhp)
1940 const struct sadb_msghdr *mhp;
1942 struct sadb_address *src0, *dst0;
1943 struct sadb_x_policy *xpl0;
1944 struct secpolicyindex spidx;
1945 struct secpolicy *sp;
1948 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
1949 panic("key_spddelete: NULL pointer is passed.\n");
1951 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
1952 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
1953 mhp->ext[SADB_X_EXT_POLICY] == NULL) {
1954 ipseclog((LOG_DEBUG, "key_spddelete: invalid message is passed.\n"));
1955 return key_senderror(so, m, EINVAL);
1957 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
1958 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
1959 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
1960 ipseclog((LOG_DEBUG, "key_spddelete: invalid message is passed.\n"));
1961 return key_senderror(so, m, EINVAL);
1964 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
1965 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
1966 xpl0 = (struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY];
1969 /* XXX boundary check against sa_len */
1970 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
1973 src0->sadb_address_prefixlen,
1974 dst0->sadb_address_prefixlen,
1975 src0->sadb_address_proto,
1978 /* checking the direciton. */
1979 switch (xpl0->sadb_x_policy_dir) {
1980 case IPSEC_DIR_INBOUND:
1981 case IPSEC_DIR_OUTBOUND:
1984 ipseclog((LOG_DEBUG, "key_spddelete: Invalid SP direction.\n"));
1985 return key_senderror(so, m, EINVAL);
1988 /* Is there SP in SPD ? */
1989 if ((sp = key_getsp(&spidx)) == NULL) {
1990 ipseclog((LOG_DEBUG, "key_spddelete: no SP found.\n"));
1991 return key_senderror(so, m, EINVAL);
1994 /* save policy id to buffer to be returned. */
1995 xpl0->sadb_x_policy_id = sp->id;
1997 sp->state = IPSEC_SPSTATE_DEAD;
2002 struct sadb_msg *newmsg;
2004 /* create new sadb_msg to reply. */
2005 n = key_gather_mbuf(m, mhp, 1, 4, SADB_EXT_RESERVED,
2006 SADB_X_EXT_POLICY, SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
2008 return key_senderror(so, m, ENOBUFS);
2010 newmsg = mtod(n, struct sadb_msg *);
2011 newmsg->sadb_msg_errno = 0;
2012 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
2015 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
2020 * SADB_SPDDELETE2 processing
2023 * from the user(?), and set SADB_SASTATE_DEAD,
2027 * policy(*) including direction of policy.
2029 * m will always be freed.
2032 key_spddelete2(so, m, mhp)
2035 const struct sadb_msghdr *mhp;
2038 struct secpolicy *sp;
2041 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
2042 panic("key_spddelete2: NULL pointer is passed.\n");
2044 if (mhp->ext[SADB_X_EXT_POLICY] == NULL ||
2045 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2046 ipseclog((LOG_DEBUG, "key_spddelete2: invalid message is passed.\n"));
2047 key_senderror(so, m, EINVAL);
2051 id = ((struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY])->sadb_x_policy_id;
2053 /* Is there SP in SPD ? */
2054 if ((sp = key_getspbyid(id)) == NULL) {
2055 ipseclog((LOG_DEBUG, "key_spddelete2: no SP found id:%u.\n", id));
2056 key_senderror(so, m, EINVAL);
2059 sp->state = IPSEC_SPSTATE_DEAD;
2063 struct mbuf *n, *nn;
2064 struct sadb_msg *newmsg;
2067 /* create new sadb_msg to reply. */
2068 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
2071 return key_senderror(so, m, ENOBUFS);
2072 MGETHDR(n, M_DONTWAIT, MT_DATA);
2073 if (n && len > MHLEN) {
2074 MCLGET(n, M_DONTWAIT);
2075 if ((n->m_flags & M_EXT) == 0) {
2081 return key_senderror(so, m, ENOBUFS);
2087 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
2088 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
2092 panic("length inconsistency in key_spddelete2");
2095 n->m_next = m_copym(m, mhp->extoff[SADB_X_EXT_POLICY],
2096 mhp->extlen[SADB_X_EXT_POLICY], M_DONTWAIT);
2099 return key_senderror(so, m, ENOBUFS);
2102 n->m_pkthdr.len = 0;
2103 for (nn = n; nn; nn = nn->m_next)
2104 n->m_pkthdr.len += nn->m_len;
2106 newmsg = mtod(n, struct sadb_msg *);
2107 newmsg->sadb_msg_errno = 0;
2108 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
2111 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
2116 * SADB_X_GET processing
2121 * <base, address(SD), policy>
2123 * policy(*) including direction of policy.
2125 * m will always be freed.
2128 key_spdget(so, m, mhp)
2131 const struct sadb_msghdr *mhp;
2134 struct secpolicy *sp;
2138 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
2139 panic("key_spdget: NULL pointer is passed.\n");
2141 if (mhp->ext[SADB_X_EXT_POLICY] == NULL ||
2142 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2143 ipseclog((LOG_DEBUG, "key_spdget: invalid message is passed.\n"));
2144 return key_senderror(so, m, EINVAL);
2147 id = ((struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY])->sadb_x_policy_id;
2149 /* Is there SP in SPD ? */
2150 if ((sp = key_getspbyid(id)) == NULL) {
2151 ipseclog((LOG_DEBUG, "key_spdget: no SP found id:%u.\n", id));
2152 return key_senderror(so, m, ENOENT);
2155 n = key_setdumpsp(sp, SADB_X_SPDGET, 0, mhp->msg->sadb_msg_pid);
2158 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
2160 return key_senderror(so, m, ENOBUFS);
2164 * SADB_X_SPDACQUIRE processing.
2165 * Acquire policy and SA(s) for a *OUTBOUND* packet.
2168 * to KMD, and expect to receive
2169 * <base> with SADB_X_SPDACQUIRE if error occured,
2172 * with SADB_X_SPDUPDATE from KMD by PF_KEY.
2173 * policy(*) is without policy requests.
2176 * others: error number
2180 struct secpolicy *sp;
2182 struct mbuf *result = NULL, *m;
2183 struct secspacq *newspacq;
2188 panic("key_spdacquire: NULL pointer is passed.\n");
2189 if (sp->req != NULL)
2190 panic("key_spdacquire: called but there is request.\n");
2191 if (sp->policy != IPSEC_POLICY_IPSEC)
2192 panic("key_spdacquire: policy mismathed. IPsec is expected.\n");
2194 /* Get an entry to check whether sent message or not. */
2195 if ((newspacq = key_getspacq(&sp->spidx)) != NULL) {
2196 if (key_blockacq_count < newspacq->count) {
2197 /* reset counter and do send message. */
2198 newspacq->count = 0;
2200 /* increment counter and do nothing. */
2205 /* make new entry for blocking to send SADB_ACQUIRE. */
2206 if ((newspacq = key_newspacq(&sp->spidx)) == NULL)
2209 /* add to acqtree */
2210 LIST_INSERT_HEAD(&spacqtree, newspacq, chain);
2213 /* create new sadb_msg to reply. */
2214 m = key_setsadbmsg(SADB_X_SPDACQUIRE, 0, 0, 0, 0, 0);
2221 result->m_pkthdr.len = 0;
2222 for (m = result; m; m = m->m_next)
2223 result->m_pkthdr.len += m->m_len;
2225 mtod(result, struct sadb_msg *)->sadb_msg_len =
2226 PFKEY_UNIT64(result->m_pkthdr.len);
2228 return key_sendup_mbuf(NULL, m, KEY_SENDUP_REGISTERED);
2237 * SADB_SPDFLUSH processing
2240 * from the user, and free all entries in secpctree.
2244 * NOTE: what to do is only marking SADB_SASTATE_DEAD.
2246 * m will always be freed.
2249 key_spdflush(so, m, mhp)
2252 const struct sadb_msghdr *mhp;
2254 struct sadb_msg *newmsg;
2255 struct secpolicy *sp;
2259 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
2260 panic("key_spdflush: NULL pointer is passed.\n");
2262 if (m->m_len != PFKEY_ALIGN8(sizeof(struct sadb_msg)))
2263 return key_senderror(so, m, EINVAL);
2265 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2266 LIST_FOREACH(sp, &sptree[dir], chain) {
2267 sp->state = IPSEC_SPSTATE_DEAD;
2271 if (sizeof(struct sadb_msg) > m->m_len + M_TRAILINGSPACE(m)) {
2272 ipseclog((LOG_DEBUG, "key_spdflush: No more memory.\n"));
2273 return key_senderror(so, m, ENOBUFS);
2279 m->m_pkthdr.len = m->m_len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
2280 newmsg = mtod(m, struct sadb_msg *);
2281 newmsg->sadb_msg_errno = 0;
2282 newmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
2284 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
2288 * SADB_SPDDUMP processing
2291 * from the user, and dump all SP leaves
2296 * m will always be freed.
2299 key_spddump(so, m, mhp)
2302 const struct sadb_msghdr *mhp;
2304 struct secpolicy *sp;
2310 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
2311 panic("key_spddump: NULL pointer is passed.\n");
2313 /* search SPD entry and get buffer size. */
2315 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2316 LIST_FOREACH(sp, &sptree[dir], chain) {
2322 return key_senderror(so, m, ENOENT);
2324 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2325 LIST_FOREACH(sp, &sptree[dir], chain) {
2327 n = key_setdumpsp(sp, SADB_X_SPDDUMP, cnt,
2328 mhp->msg->sadb_msg_pid);
2331 key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
2339 static struct mbuf *
2340 key_setdumpsp(sp, type, seq, pid)
2341 struct secpolicy *sp;
2345 struct mbuf *result = NULL, *m;
2347 m = key_setsadbmsg(type, 0, SADB_SATYPE_UNSPEC, seq, pid, sp->refcnt);
2352 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
2353 &sp->spidx.src.sa, sp->spidx.prefs,
2354 sp->spidx.ul_proto);
2359 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
2360 &sp->spidx.dst.sa, sp->spidx.prefd,
2361 sp->spidx.ul_proto);
2371 if ((result->m_flags & M_PKTHDR) == 0)
2374 if (result->m_len < sizeof(struct sadb_msg)) {
2375 result = m_pullup(result, sizeof(struct sadb_msg));
2380 result->m_pkthdr.len = 0;
2381 for (m = result; m; m = m->m_next)
2382 result->m_pkthdr.len += m->m_len;
2384 mtod(result, struct sadb_msg *)->sadb_msg_len =
2385 PFKEY_UNIT64(result->m_pkthdr.len);
2395 * get PFKEY message length for security policy and request.
2398 key_getspreqmsglen(sp)
2399 struct secpolicy *sp;
2403 tlen = sizeof(struct sadb_x_policy);
2405 /* if is the policy for ipsec ? */
2406 if (sp->policy != IPSEC_POLICY_IPSEC)
2409 /* get length of ipsec requests */
2411 struct ipsecrequest *isr;
2414 for (isr = sp->req; isr != NULL; isr = isr->next) {
2415 len = sizeof(struct sadb_x_ipsecrequest)
2416 + isr->saidx.src.sa.sa_len
2417 + isr->saidx.dst.sa.sa_len;
2419 tlen += PFKEY_ALIGN8(len);
2427 * SADB_SPDEXPIRE processing
2429 * <base, address(SD), lifetime(CH), policy>
2433 * others : error number
2437 struct secpolicy *sp;
2440 struct mbuf *result = NULL, *m;
2443 struct sadb_lifetime *lt;
2445 /* XXX: Why do we lock ? */
2446 s = splnet(); /*called from softclock()*/
2450 panic("key_spdexpire: NULL pointer is passed.\n");
2452 /* set msg header */
2453 m = key_setsadbmsg(SADB_X_SPDEXPIRE, 0, 0, 0, 0, 0);
2460 /* create lifetime extension (current and hard) */
2461 len = PFKEY_ALIGN8(sizeof(*lt)) * 2;
2462 m = key_alloc_mbuf(len);
2463 if (!m || m->m_next) { /*XXX*/
2469 bzero(mtod(m, caddr_t), len);
2470 lt = mtod(m, struct sadb_lifetime *);
2471 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
2472 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
2473 lt->sadb_lifetime_allocations = 0;
2474 lt->sadb_lifetime_bytes = 0;
2475 lt->sadb_lifetime_addtime = sp->created;
2476 lt->sadb_lifetime_usetime = sp->lastused;
2477 lt = (struct sadb_lifetime *)(mtod(m, caddr_t) + len / 2);
2478 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
2479 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
2480 lt->sadb_lifetime_allocations = 0;
2481 lt->sadb_lifetime_bytes = 0;
2482 lt->sadb_lifetime_addtime = sp->lifetime;
2483 lt->sadb_lifetime_usetime = sp->validtime;
2486 /* set sadb_address for source */
2487 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
2489 sp->spidx.prefs, sp->spidx.ul_proto);
2496 /* set sadb_address for destination */
2497 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
2499 sp->spidx.prefd, sp->spidx.ul_proto);
2514 if ((result->m_flags & M_PKTHDR) == 0) {
2519 if (result->m_len < sizeof(struct sadb_msg)) {
2520 result = m_pullup(result, sizeof(struct sadb_msg));
2521 if (result == NULL) {
2527 result->m_pkthdr.len = 0;
2528 for (m = result; m; m = m->m_next)
2529 result->m_pkthdr.len += m->m_len;
2531 mtod(result, struct sadb_msg *)->sadb_msg_len =
2532 PFKEY_UNIT64(result->m_pkthdr.len);
2534 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
2543 /* %%% SAD management */
2545 * allocating a memory for new SA head, and copy from the values of mhp.
2546 * OUT: NULL : failure due to the lack of memory.
2547 * others : pointer to new SA head.
2549 static struct secashead *
2551 struct secasindex *saidx;
2553 struct secashead *newsah;
2555 KASSERT(saidx != NULL, ("key_newsaidx: null saidx"));
2557 newsah = malloc(sizeof(struct secashead), M_SECA,
2558 M_INTWAIT | M_ZERO | M_NULLOK);
2559 if (newsah != NULL) {
2561 for (i = 0; i < sizeof(newsah->savtree)/sizeof(newsah->savtree[0]); i++)
2562 LIST_INIT(&newsah->savtree[i]);
2563 newsah->saidx = *saidx;
2565 /* add to saidxtree */
2566 newsah->state = SADB_SASTATE_MATURE;
2567 LIST_INSERT_HEAD(&sahtree, newsah, chain);
2573 * delete SA index and all SA registerd.
2577 struct secashead *sah;
2579 struct secasvar *sav, *nextsav;
2580 u_int stateidx, state;
2586 panic("key_delsah: NULL pointer is passed.\n");
2588 s = splnet(); /*called from softclock()*/
2590 /* searching all SA registerd in the secindex. */
2592 stateidx < _ARRAYLEN(saorder_state_any);
2595 state = saorder_state_any[stateidx];
2596 for (sav = (struct secasvar *)LIST_FIRST(&sah->savtree[state]);
2600 nextsav = LIST_NEXT(sav, chain);
2602 if (sav->refcnt == 0) {
2604 KEY_CHKSASTATE(state, sav->state, "key_delsah");
2607 /* give up to delete this sa */
2613 /* don't delete sah only if there are savs. */
2619 if (sah->sa_route.ro_rt) {
2620 RTFREE(sah->sa_route.ro_rt);
2621 sah->sa_route.ro_rt = (struct rtentry *)NULL;
2624 /* remove from tree of SA index */
2625 if (__LIST_CHAINED(sah))
2626 LIST_REMOVE(sah, chain);
2635 * allocating a new SA with LARVAL state. key_add() and key_getspi() call,
2636 * and copy the values of mhp into new buffer.
2637 * When SAD message type is GETSPI:
2638 * to set sequence number from acq_seq++,
2639 * to set zero to SPI.
2640 * not to call key_setsava().
2642 * others : pointer to new secasvar.
2644 * does not modify mbuf. does not free mbuf on error.
2646 static struct secasvar *
2647 key_newsav(m, mhp, sah, errp, where, tag)
2649 const struct sadb_msghdr *mhp;
2650 struct secashead *sah;
2655 struct secasvar *newsav;
2656 const struct sadb_sa *xsa;
2659 if (m == NULL || mhp == NULL || mhp->msg == NULL || sah == NULL)
2660 panic("key_newsa: NULL pointer is passed.\n");
2662 KMALLOC(newsav, struct secasvar *, sizeof(struct secasvar));
2663 if (newsav == NULL) {
2664 ipseclog((LOG_DEBUG, "key_newsa: No more memory.\n"));
2668 bzero((caddr_t)newsav, sizeof(struct secasvar));
2670 switch (mhp->msg->sadb_msg_type) {
2674 #ifdef IPSEC_DOSEQCHECK
2675 /* sync sequence number */
2676 if (mhp->msg->sadb_msg_seq == 0)
2678 (acq_seq = (acq_seq == ~0 ? 1 : ++acq_seq));
2681 newsav->seq = mhp->msg->sadb_msg_seq;
2686 if (mhp->ext[SADB_EXT_SA] == NULL) {
2687 KFREE(newsav), newsav = NULL;
2688 ipseclog((LOG_DEBUG, "key_newsa: invalid message is passed.\n"));
2692 xsa = (const struct sadb_sa *)mhp->ext[SADB_EXT_SA];
2693 newsav->spi = xsa->sadb_sa_spi;
2694 newsav->seq = mhp->msg->sadb_msg_seq;
2697 KFREE(newsav), newsav = NULL;
2702 /* copy sav values */
2703 if (mhp->msg->sadb_msg_type != SADB_GETSPI) {
2704 *errp = key_setsaval(newsav, m, mhp);
2706 KFREE(newsav), newsav = NULL;
2712 newsav->created = time_second;
2713 newsav->pid = mhp->msg->sadb_msg_pid;
2718 newsav->state = SADB_SASTATE_LARVAL;
2719 LIST_INSERT_TAIL(&sah->savtree[SADB_SASTATE_LARVAL], newsav,
2722 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
2723 printf("DP key_newsav from %s:%u return SP:%p\n",
2724 where, tag, newsav));
2730 * free() SA variable entry.
2734 struct secasvar *sav;
2736 KASSERT(sav != NULL, ("key_delsav: null sav"));
2737 KASSERT(sav->refcnt == 0,
2738 ("key_delsav: reference count %u > 0", sav->refcnt));
2740 /* remove from SA header */
2741 if (__LIST_CHAINED(sav))
2742 LIST_REMOVE(sav, chain);
2744 if (sav->key_auth != NULL) {
2745 bzero(_KEYBUF(sav->key_auth), _KEYLEN(sav->key_auth));
2746 KFREE(sav->key_auth);
2747 sav->key_auth = NULL;
2749 if (sav->key_enc != NULL) {
2750 bzero(_KEYBUF(sav->key_enc), _KEYLEN(sav->key_enc));
2751 KFREE(sav->key_enc);
2752 sav->key_enc = NULL;
2755 bzero(sav->sched, sav->schedlen);
2759 if (sav->replay != NULL) {
2763 if (sav->lft_c != NULL) {
2767 if (sav->lft_h != NULL) {
2771 if (sav->lft_s != NULL) {
2775 if (sav->iv != NULL) {
2789 * others : found, pointer to a SA.
2791 static struct secashead *
2793 struct secasindex *saidx;
2795 struct secashead *sah;
2797 LIST_FOREACH(sah, &sahtree, chain) {
2798 if (sah->state == SADB_SASTATE_DEAD)
2800 if (key_cmpsaidx(&sah->saidx, saidx, CMP_REQID))
2808 * check not to be duplicated SPI.
2809 * NOTE: this function is too slow due to searching all SAD.
2812 * others : found, pointer to a SA.
2814 static struct secasvar *
2815 key_checkspidup(saidx, spi)
2816 struct secasindex *saidx;
2819 struct secashead *sah;
2820 struct secasvar *sav;
2822 /* check address family */
2823 if (saidx->src.sa.sa_family != saidx->dst.sa.sa_family) {
2824 ipseclog((LOG_DEBUG, "key_checkspidup: address family mismatched.\n"));
2829 LIST_FOREACH(sah, &sahtree, chain) {
2830 if (!key_ismyaddr((struct sockaddr *)&sah->saidx.dst))
2832 sav = key_getsavbyspi(sah, spi);
2841 * search SAD litmited alive SA, protocol, SPI.
2844 * others : found, pointer to a SA.
2846 static struct secasvar *
2847 key_getsavbyspi(sah, spi)
2848 struct secashead *sah;
2851 struct secasvar *sav;
2852 u_int stateidx, state;
2854 /* search all status */
2856 stateidx < _ARRAYLEN(saorder_state_alive);
2859 state = saorder_state_alive[stateidx];
2860 LIST_FOREACH(sav, &sah->savtree[state], chain) {
2863 if (sav->state != state) {
2864 ipseclog((LOG_DEBUG, "key_getsavbyspi: "
2865 "invalid sav->state (queue: %d SA: %d)\n",
2866 state, sav->state));
2870 if (sav->spi == spi)
2879 * copy SA values from PF_KEY message except *SPI, SEQ, PID, STATE and TYPE*.
2880 * You must update these if need.
2884 * does not modify mbuf. does not free mbuf on error.
2887 key_setsaval(sav, m, mhp)
2888 struct secasvar *sav;
2890 const struct sadb_msghdr *mhp;
2895 if (m == NULL || mhp == NULL || mhp->msg == NULL)
2896 panic("key_setsaval: NULL pointer is passed.\n");
2898 /* initialization */
2900 sav->key_auth = NULL;
2901 sav->key_enc = NULL;
2908 sav->tdb_xform = NULL; /* transform */
2909 sav->tdb_encalgxform = NULL; /* encoding algorithm */
2910 sav->tdb_authalgxform = NULL; /* authentication algorithm */
2911 sav->tdb_compalgxform = NULL; /* compression algorithm */
2914 if (mhp->ext[SADB_EXT_SA] != NULL) {
2915 const struct sadb_sa *sa0;
2917 sa0 = (const struct sadb_sa *)mhp->ext[SADB_EXT_SA];
2918 if (mhp->extlen[SADB_EXT_SA] < sizeof(*sa0)) {
2923 sav->alg_auth = sa0->sadb_sa_auth;
2924 sav->alg_enc = sa0->sadb_sa_encrypt;
2925 sav->flags = sa0->sadb_sa_flags;
2928 if ((sa0->sadb_sa_flags & SADB_X_EXT_OLD) == 0) {
2930 malloc(sizeof(struct secreplay)+sa0->sadb_sa_replay,
2931 M_SECA, M_INTWAIT | M_ZERO | M_NULLOK);
2932 if (sav->replay == NULL) {
2933 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
2937 if (sa0->sadb_sa_replay != 0)
2938 sav->replay->bitmap = (caddr_t)(sav->replay+1);
2939 sav->replay->wsize = sa0->sadb_sa_replay;
2943 /* Authentication keys */
2944 if (mhp->ext[SADB_EXT_KEY_AUTH] != NULL) {
2945 const struct sadb_key *key0;
2948 key0 = (const struct sadb_key *)mhp->ext[SADB_EXT_KEY_AUTH];
2949 len = mhp->extlen[SADB_EXT_KEY_AUTH];
2952 if (len < sizeof(*key0)) {
2956 switch (mhp->msg->sadb_msg_satype) {
2957 case SADB_SATYPE_AH:
2958 case SADB_SATYPE_ESP:
2959 if (len == PFKEY_ALIGN8(sizeof(struct sadb_key)) &&
2960 sav->alg_auth != SADB_X_AALG_NULL)
2963 case SADB_X_SATYPE_IPCOMP:
2969 ipseclog((LOG_DEBUG, "key_setsaval: invalid key_auth values.\n"));
2973 sav->key_auth = (struct sadb_key *)key_newbuf(key0, len);
2974 if (sav->key_auth == NULL) {
2975 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
2981 /* Encryption key */
2982 if (mhp->ext[SADB_EXT_KEY_ENCRYPT] != NULL) {
2983 const struct sadb_key *key0;
2986 key0 = (const struct sadb_key *)mhp->ext[SADB_EXT_KEY_ENCRYPT];
2987 len = mhp->extlen[SADB_EXT_KEY_ENCRYPT];
2990 if (len < sizeof(*key0)) {
2994 switch (mhp->msg->sadb_msg_satype) {
2995 case SADB_SATYPE_ESP:
2996 if (len == PFKEY_ALIGN8(sizeof(struct sadb_key)) &&
2997 sav->alg_enc != SADB_EALG_NULL) {
3001 sav->key_enc = (struct sadb_key *)key_newbuf(key0, len);
3002 if (sav->key_enc == NULL) {
3003 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
3008 case SADB_X_SATYPE_IPCOMP:
3009 if (len != PFKEY_ALIGN8(sizeof(struct sadb_key)))
3011 sav->key_enc = NULL; /*just in case*/
3013 case SADB_SATYPE_AH:
3019 ipseclog((LOG_DEBUG, "key_setsatval: invalid key_enc value.\n"));
3027 switch (mhp->msg->sadb_msg_satype) {
3028 case SADB_SATYPE_AH:
3029 error = xform_init(sav, XF_AH);
3031 case SADB_SATYPE_ESP:
3032 error = xform_init(sav, XF_ESP);
3034 case SADB_X_SATYPE_IPCOMP:
3035 error = xform_init(sav, XF_IPCOMP);
3039 ipseclog((LOG_DEBUG,
3040 "key_setsaval: unable to initialize SA type %u.\n",
3041 mhp->msg->sadb_msg_satype));
3046 sav->created = time_second;
3048 /* make lifetime for CURRENT */
3049 KMALLOC(sav->lft_c, struct sadb_lifetime *,
3050 sizeof(struct sadb_lifetime));
3051 if (sav->lft_c == NULL) {
3052 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
3057 sav->lft_c->sadb_lifetime_len =
3058 PFKEY_UNIT64(sizeof(struct sadb_lifetime));
3059 sav->lft_c->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
3060 sav->lft_c->sadb_lifetime_allocations = 0;
3061 sav->lft_c->sadb_lifetime_bytes = 0;
3062 sav->lft_c->sadb_lifetime_addtime = time_second;
3063 sav->lft_c->sadb_lifetime_usetime = 0;
3065 /* lifetimes for HARD and SOFT */
3067 const struct sadb_lifetime *lft0;
3069 lft0 = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_HARD];
3071 if (mhp->extlen[SADB_EXT_LIFETIME_HARD] < sizeof(*lft0)) {
3075 sav->lft_h = (struct sadb_lifetime *)key_newbuf(lft0,
3077 if (sav->lft_h == NULL) {
3078 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
3082 /* to be initialize ? */
3085 lft0 = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_SOFT];
3087 if (mhp->extlen[SADB_EXT_LIFETIME_SOFT] < sizeof(*lft0)) {
3091 sav->lft_s = (struct sadb_lifetime *)key_newbuf(lft0,
3093 if (sav->lft_s == NULL) {
3094 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
3098 /* to be initialize ? */
3105 /* initialization */
3106 if (sav->replay != NULL) {
3110 if (sav->key_auth != NULL) {
3111 KFREE(sav->key_auth);
3112 sav->key_auth = NULL;
3114 if (sav->key_enc != NULL) {
3115 KFREE(sav->key_enc);
3116 sav->key_enc = NULL;
3122 if (sav->iv != NULL) {
3126 if (sav->lft_c != NULL) {
3130 if (sav->lft_h != NULL) {
3134 if (sav->lft_s != NULL) {
3143 * validation with a secasvar entry, and set SADB_SATYPE_MATURE.
3149 struct secasvar *sav;
3153 /* check SPI value */
3154 switch (sav->sah->saidx.proto) {
3157 if (ntohl(sav->spi) >= 0 && ntohl(sav->spi) <= 255) {
3158 ipseclog((LOG_DEBUG,
3159 "key_mature: illegal range of SPI %u.\n",
3160 (u_int32_t)ntohl(sav->spi)));
3167 switch (sav->sah->saidx.proto) {
3170 if ((sav->flags & (SADB_X_EXT_OLD|SADB_X_EXT_DERIV)) ==
3171 (SADB_X_EXT_OLD|SADB_X_EXT_DERIV)) {
3172 ipseclog((LOG_DEBUG, "key_mature: "
3173 "invalid flag (derived) given to old-esp.\n"));
3176 error = xform_init(sav, XF_ESP);
3180 if (sav->flags & SADB_X_EXT_DERIV) {
3181 ipseclog((LOG_DEBUG, "key_mature: "
3182 "invalid flag (derived) given to AH SA.\n"));
3185 if (sav->alg_enc != SADB_EALG_NONE) {
3186 ipseclog((LOG_DEBUG, "key_mature: "
3187 "protocol and algorithm mismated.\n"));
3190 error = xform_init(sav, XF_AH);
3192 case IPPROTO_IPCOMP:
3193 if (sav->alg_auth != SADB_AALG_NONE) {
3194 ipseclog((LOG_DEBUG, "key_mature: "
3195 "protocol and algorithm mismated.\n"));
3198 if ((sav->flags & SADB_X_EXT_RAWCPI) == 0
3199 && ntohl(sav->spi) >= 0x10000) {
3200 ipseclog((LOG_DEBUG, "key_mature: invalid cpi for IPComp.\n"));
3203 error = xform_init(sav, XF_IPCOMP);
3206 ipseclog((LOG_DEBUG, "key_mature: Invalid satype.\n"));
3207 error = EPROTONOSUPPORT;
3211 key_sa_chgstate(sav, SADB_SASTATE_MATURE);
3216 * subroutine for SADB_GET and SADB_DUMP.
3218 static struct mbuf *
3219 key_setdumpsa(sav, type, satype, seq, pid)
3220 struct secasvar *sav;
3221 u_int8_t type, satype;
3224 struct mbuf *result = NULL, *tres = NULL, *m;
3229 SADB_EXT_SA, SADB_X_EXT_SA2,
3230 SADB_EXT_LIFETIME_HARD, SADB_EXT_LIFETIME_SOFT,
3231 SADB_EXT_LIFETIME_CURRENT, SADB_EXT_ADDRESS_SRC,
3232 SADB_EXT_ADDRESS_DST, SADB_EXT_ADDRESS_PROXY, SADB_EXT_KEY_AUTH,
3233 SADB_EXT_KEY_ENCRYPT, SADB_EXT_IDENTITY_SRC,
3234 SADB_EXT_IDENTITY_DST, SADB_EXT_SENSITIVITY,
3237 m = key_setsadbmsg(type, 0, satype, seq, pid, sav->refcnt);
3242 for (i = sizeof(dumporder)/sizeof(dumporder[0]) - 1; i >= 0; i--) {
3245 switch (dumporder[i]) {
3247 m = key_setsadbsa(sav);
3252 case SADB_X_EXT_SA2:
3253 m = key_setsadbxsa2(sav->sah->saidx.mode,
3254 sav->replay ? sav->replay->count : 0,
3255 sav->sah->saidx.reqid);
3260 case SADB_EXT_ADDRESS_SRC:
3261 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
3262 &sav->sah->saidx.src.sa,
3263 FULLMASK, IPSEC_ULPROTO_ANY);
3268 case SADB_EXT_ADDRESS_DST:
3269 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
3270 &sav->sah->saidx.dst.sa,
3271 FULLMASK, IPSEC_ULPROTO_ANY);
3276 case SADB_EXT_KEY_AUTH:
3279 l = PFKEY_UNUNIT64(sav->key_auth->sadb_key_len);
3283 case SADB_EXT_KEY_ENCRYPT:
3286 l = PFKEY_UNUNIT64(sav->key_enc->sadb_key_len);
3290 case SADB_EXT_LIFETIME_CURRENT:
3293 l = PFKEY_UNUNIT64(((struct sadb_ext *)sav->lft_c)->sadb_ext_len);
3297 case SADB_EXT_LIFETIME_HARD:
3300 l = PFKEY_UNUNIT64(((struct sadb_ext *)sav->lft_h)->sadb_ext_len);
3304 case SADB_EXT_LIFETIME_SOFT:
3307 l = PFKEY_UNUNIT64(((struct sadb_ext *)sav->lft_s)->sadb_ext_len);
3311 case SADB_EXT_ADDRESS_PROXY:
3312 case SADB_EXT_IDENTITY_SRC:
3313 case SADB_EXT_IDENTITY_DST:
3314 /* XXX: should we brought from SPD ? */
3315 case SADB_EXT_SENSITIVITY:
3320 if ((!m && !p) || (m && p))
3323 M_PREPEND(tres, l, M_DONTWAIT);
3326 bcopy(p, mtod(tres, caddr_t), l);
3330 m = key_alloc_mbuf(l);
3333 m_copyback(m, 0, l, p);
3341 m_cat(result, tres);
3343 if (result->m_len < sizeof(struct sadb_msg)) {
3344 result = m_pullup(result, sizeof(struct sadb_msg));
3349 result->m_pkthdr.len = 0;
3350 for (m = result; m; m = m->m_next)
3351 result->m_pkthdr.len += m->m_len;
3353 mtod(result, struct sadb_msg *)->sadb_msg_len =
3354 PFKEY_UNIT64(result->m_pkthdr.len);
3365 * set data into sadb_msg.
3367 static struct mbuf *
3368 key_setsadbmsg(type, tlen, satype, seq, pid, reserved)
3369 u_int8_t type, satype;
3379 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
3382 MGETHDR(m, M_DONTWAIT, MT_DATA);
3383 if (m && len > MHLEN) {
3384 MCLGET(m, M_DONTWAIT);
3385 if ((m->m_flags & M_EXT) == 0) {
3392 m->m_pkthdr.len = m->m_len = len;
3395 p = mtod(m, struct sadb_msg *);
3398 p->sadb_msg_version = PF_KEY_V2;
3399 p->sadb_msg_type = type;
3400 p->sadb_msg_errno = 0;
3401 p->sadb_msg_satype = satype;
3402 p->sadb_msg_len = PFKEY_UNIT64(tlen);
3403 p->sadb_msg_reserved = reserved;
3404 p->sadb_msg_seq = seq;
3405 p->sadb_msg_pid = (u_int32_t)pid;
3411 * copy secasvar data into sadb_address.
3413 static struct mbuf *
3415 struct secasvar *sav;
3421 len = PFKEY_ALIGN8(sizeof(struct sadb_sa));
3422 m = key_alloc_mbuf(len);
3423 if (!m || m->m_next) { /*XXX*/
3429 p = mtod(m, struct sadb_sa *);
3432 p->sadb_sa_len = PFKEY_UNIT64(len);
3433 p->sadb_sa_exttype = SADB_EXT_SA;
3434 p->sadb_sa_spi = sav->spi;
3435 p->sadb_sa_replay = (sav->replay != NULL ? sav->replay->wsize : 0);
3436 p->sadb_sa_state = sav->state;
3437 p->sadb_sa_auth = sav->alg_auth;
3438 p->sadb_sa_encrypt = sav->alg_enc;
3439 p->sadb_sa_flags = sav->flags;
3445 * set data into sadb_address.
3447 static struct mbuf *
3448 key_setsadbaddr(exttype, saddr, prefixlen, ul_proto)
3450 const struct sockaddr *saddr;
3455 struct sadb_address *p;
3458 len = PFKEY_ALIGN8(sizeof(struct sadb_address)) +
3459 PFKEY_ALIGN8(saddr->sa_len);
3460 m = key_alloc_mbuf(len);
3461 if (!m || m->m_next) { /*XXX*/
3467 p = mtod(m, struct sadb_address *);
3470 p->sadb_address_len = PFKEY_UNIT64(len);
3471 p->sadb_address_exttype = exttype;
3472 p->sadb_address_proto = ul_proto;
3473 if (prefixlen == FULLMASK) {
3474 switch (saddr->sa_family) {
3476 prefixlen = sizeof(struct in_addr) << 3;
3479 prefixlen = sizeof(struct in6_addr) << 3;
3485 p->sadb_address_prefixlen = prefixlen;
3486 p->sadb_address_reserved = 0;
3489 mtod(m, caddr_t) + PFKEY_ALIGN8(sizeof(struct sadb_address)),
3497 * set data into sadb_ident.
3499 static struct mbuf *
3500 key_setsadbident(exttype, idtype, string, stringlen, id)
3501 u_int16_t exttype, idtype;
3507 struct sadb_ident *p;
3510 len = PFKEY_ALIGN8(sizeof(struct sadb_ident)) + PFKEY_ALIGN8(stringlen);
3511 m = key_alloc_mbuf(len);
3512 if (!m || m->m_next) { /*XXX*/
3518 p = mtod(m, struct sadb_ident *);
3521 p->sadb_ident_len = PFKEY_UNIT64(len);
3522 p->sadb_ident_exttype = exttype;
3523 p->sadb_ident_type = idtype;
3524 p->sadb_ident_reserved = 0;
3525 p->sadb_ident_id = id;
3528 mtod(m, caddr_t) + PFKEY_ALIGN8(sizeof(struct sadb_ident)),
3536 * set data into sadb_x_sa2.
3538 static struct mbuf *
3539 key_setsadbxsa2(mode, seq, reqid)
3541 u_int32_t seq, reqid;
3544 struct sadb_x_sa2 *p;
3547 len = PFKEY_ALIGN8(sizeof(struct sadb_x_sa2));
3548 m = key_alloc_mbuf(len);
3549 if (!m || m->m_next) { /*XXX*/
3555 p = mtod(m, struct sadb_x_sa2 *);
3558 p->sadb_x_sa2_len = PFKEY_UNIT64(len);
3559 p->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
3560 p->sadb_x_sa2_mode = mode;
3561 p->sadb_x_sa2_reserved1 = 0;
3562 p->sadb_x_sa2_reserved2 = 0;
3563 p->sadb_x_sa2_sequence = seq;
3564 p->sadb_x_sa2_reqid = reqid;
3570 * set data into sadb_x_policy
3572 static struct mbuf *
3573 key_setsadbxpolicy(type, dir, id)
3579 struct sadb_x_policy *p;
3582 len = PFKEY_ALIGN8(sizeof(struct sadb_x_policy));
3583 m = key_alloc_mbuf(len);
3584 if (!m || m->m_next) { /*XXX*/
3590 p = mtod(m, struct sadb_x_policy *);
3593 p->sadb_x_policy_len = PFKEY_UNIT64(len);
3594 p->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3595 p->sadb_x_policy_type = type;
3596 p->sadb_x_policy_dir = dir;
3597 p->sadb_x_policy_id = id;
3604 * copy a buffer into the new buffer allocated.
3607 key_newbuf(src, len)
3613 KMALLOC(new, caddr_t, len);
3615 ipseclog((LOG_DEBUG, "key_newbuf: No more memory.\n"));
3618 bcopy(src, new, len);
3623 /* compare my own address
3624 * OUT: 1: true, i.e. my address.
3629 struct sockaddr *sa;
3632 struct sockaddr_in *sin;
3633 struct in_ifaddr *ia;
3638 panic("key_ismyaddr: NULL pointer is passed.\n");
3640 switch (sa->sa_family) {
3643 sin = (struct sockaddr_in *)sa;
3644 for (ia = in_ifaddrhead.tqh_first; ia;
3645 ia = ia->ia_link.tqe_next)
3647 if (sin->sin_family == ia->ia_addr.sin_family &&
3648 sin->sin_len == ia->ia_addr.sin_len &&
3649 sin->sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr)
3658 return key_ismyaddr6((struct sockaddr_in6 *)sa);
3667 * compare my own address for IPv6.
3670 * NOTE: derived ip6_input() in KAME. This is necessary to modify more.
3672 #include <netinet6/in6_var.h>
3676 struct sockaddr_in6 *sin6;
3678 struct in6_ifaddr *ia;
3679 struct in6_multi *in6m;
3681 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
3682 if (key_sockaddrcmp((struct sockaddr *)&sin6,
3683 (struct sockaddr *)&ia->ia_addr, 0) == 0)
3688 * XXX why do we care about multlicast here while we don't care
3689 * about IPv4 multicast??
3693 IN6_LOOKUP_MULTI(sin6->sin6_addr, ia->ia_ifp, in6m);
3698 /* loopback, just for safety */
3699 if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
3707 * compare two secasindex structure.
3708 * flag can specify to compare 2 saidxes.
3709 * compare two secasindex structure without both mode and reqid.
3710 * don't compare port.
3712 * saidx0: source, it can be in SAD.
3720 const struct secasindex *saidx0,
3721 const struct secasindex *saidx1,
3725 if (saidx0 == NULL && saidx1 == NULL)
3728 if (saidx0 == NULL || saidx1 == NULL)
3731 if (saidx0->proto != saidx1->proto)
3734 if (flag == CMP_EXACTLY) {
3735 if (saidx0->mode != saidx1->mode)
3737 if (saidx0->reqid != saidx1->reqid)
3739 if (bcmp(&saidx0->src, &saidx1->src, saidx0->src.sa.sa_len) != 0 ||
3740 bcmp(&saidx0->dst, &saidx1->dst, saidx0->dst.sa.sa_len) != 0)
3744 /* CMP_MODE_REQID, CMP_REQID, CMP_HEAD */
3745 if (flag == CMP_MODE_REQID
3746 ||flag == CMP_REQID) {
3748 * If reqid of SPD is non-zero, unique SA is required.
3749 * The result must be of same reqid in this case.
3751 if (saidx1->reqid != 0 && saidx0->reqid != saidx1->reqid)
3755 if (flag == CMP_MODE_REQID) {
3756 if (saidx0->mode != IPSEC_MODE_ANY
3757 && saidx0->mode != saidx1->mode)
3761 if (key_sockaddrcmp(&saidx0->src.sa, &saidx1->src.sa, 0) != 0) {
3764 if (key_sockaddrcmp(&saidx0->dst.sa, &saidx1->dst.sa, 0) != 0) {
3773 * compare two secindex structure exactly.
3775 * spidx0: source, it is often in SPD.
3776 * spidx1: object, it is often from PFKEY message.
3782 key_cmpspidx_exactly(
3783 struct secpolicyindex *spidx0,
3784 struct secpolicyindex *spidx1)
3787 if (spidx0 == NULL && spidx1 == NULL)
3790 if (spidx0 == NULL || spidx1 == NULL)
3793 if (spidx0->prefs != spidx1->prefs
3794 || spidx0->prefd != spidx1->prefd
3795 || spidx0->ul_proto != spidx1->ul_proto)
3798 return key_sockaddrcmp(&spidx0->src.sa, &spidx1->src.sa, 1) == 0 &&
3799 key_sockaddrcmp(&spidx0->dst.sa, &spidx1->dst.sa, 1) == 0;
3803 * compare two secindex structure with mask.
3805 * spidx0: source, it is often in SPD.
3806 * spidx1: object, it is often from IP header.
3812 key_cmpspidx_withmask(
3813 struct secpolicyindex *spidx0,
3814 struct secpolicyindex *spidx1)
3817 if (spidx0 == NULL && spidx1 == NULL)
3820 if (spidx0 == NULL || spidx1 == NULL)
3823 if (spidx0->src.sa.sa_family != spidx1->src.sa.sa_family ||
3824 spidx0->dst.sa.sa_family != spidx1->dst.sa.sa_family ||
3825 spidx0->src.sa.sa_len != spidx1->src.sa.sa_len ||
3826 spidx0->dst.sa.sa_len != spidx1->dst.sa.sa_len)
3829 /* if spidx.ul_proto == IPSEC_ULPROTO_ANY, ignore. */
3830 if (spidx0->ul_proto != (u_int16_t)IPSEC_ULPROTO_ANY
3831 && spidx0->ul_proto != spidx1->ul_proto)
3834 switch (spidx0->src.sa.sa_family) {
3836 if (spidx0->src.sin.sin_port != IPSEC_PORT_ANY
3837 && spidx0->src.sin.sin_port != spidx1->src.sin.sin_port)
3839 if (!key_bbcmp(&spidx0->src.sin.sin_addr,
3840 &spidx1->src.sin.sin_addr, spidx0->prefs))
3844 if (spidx0->src.sin6.sin6_port != IPSEC_PORT_ANY
3845 && spidx0->src.sin6.sin6_port != spidx1->src.sin6.sin6_port)
3848 * scope_id check. if sin6_scope_id is 0, we regard it
3849 * as a wildcard scope, which matches any scope zone ID.
3851 if (spidx0->src.sin6.sin6_scope_id &&
3852 spidx1->src.sin6.sin6_scope_id &&
3853 spidx0->src.sin6.sin6_scope_id != spidx1->src.sin6.sin6_scope_id)
3855 if (!key_bbcmp(&spidx0->src.sin6.sin6_addr,
3856 &spidx1->src.sin6.sin6_addr, spidx0->prefs))
3861 if (bcmp(&spidx0->src, &spidx1->src, spidx0->src.sa.sa_len) != 0)
3866 switch (spidx0->dst.sa.sa_family) {
3868 if (spidx0->dst.sin.sin_port != IPSEC_PORT_ANY
3869 && spidx0->dst.sin.sin_port != spidx1->dst.sin.sin_port)
3871 if (!key_bbcmp(&spidx0->dst.sin.sin_addr,
3872 &spidx1->dst.sin.sin_addr, spidx0->prefd))
3876 if (spidx0->dst.sin6.sin6_port != IPSEC_PORT_ANY
3877 && spidx0->dst.sin6.sin6_port != spidx1->dst.sin6.sin6_port)
3880 * scope_id check. if sin6_scope_id is 0, we regard it
3881 * as a wildcard scope, which matches any scope zone ID.
3883 if (spidx0->src.sin6.sin6_scope_id &&
3884 spidx1->src.sin6.sin6_scope_id &&
3885 spidx0->dst.sin6.sin6_scope_id != spidx1->dst.sin6.sin6_scope_id)
3887 if (!key_bbcmp(&spidx0->dst.sin6.sin6_addr,
3888 &spidx1->dst.sin6.sin6_addr, spidx0->prefd))
3893 if (bcmp(&spidx0->dst, &spidx1->dst, spidx0->dst.sa.sa_len) != 0)
3898 /* XXX Do we check other field ? e.g. flowinfo */
3903 /* returns 0 on match */
3906 const struct sockaddr *sa1,
3907 const struct sockaddr *sa2,
3913 #define satosin(s) ((const struct sockaddr_in *)s)
3917 #define satosin6(s) ((const struct sockaddr_in6 *)s)
3918 if (sa1->sa_family != sa2->sa_family || sa1->sa_len != sa2->sa_len)
3921 switch (sa1->sa_family) {
3923 if (sa1->sa_len != sizeof(struct sockaddr_in))
3925 if (satosin(sa1)->sin_addr.s_addr !=
3926 satosin(sa2)->sin_addr.s_addr) {
3929 if (port && satosin(sa1)->sin_port != satosin(sa2)->sin_port)
3933 if (sa1->sa_len != sizeof(struct sockaddr_in6))
3934 return 1; /*EINVAL*/
3935 if (satosin6(sa1)->sin6_scope_id !=
3936 satosin6(sa2)->sin6_scope_id) {
3939 if (!IN6_ARE_ADDR_EQUAL(&satosin6(sa1)->sin6_addr,
3940 &satosin6(sa2)->sin6_addr)) {
3944 satosin6(sa1)->sin6_port != satosin6(sa2)->sin6_port) {
3948 if (bcmp(sa1, sa2, sa1->sa_len) != 0)
3959 * compare two buffers with mask.
3963 * bits: Number of bits to compare
3969 key_bbcmp(const void *a1, const void *a2, u_int bits)
3971 const unsigned char *p1 = a1;
3972 const unsigned char *p2 = a2;
3974 /* XXX: This could be considerably faster if we compare a word
3975 * at a time, but it is complicated on LSB Endian machines */
3977 /* Handle null pointers */
3978 if (p1 == NULL || p2 == NULL)
3988 u_int8_t mask = ~((1<<(8-bits))-1);
3989 if ((*p1 & mask) != (*p2 & mask))
3992 return 1; /* Match! */
3997 * scanning SPD and SAD to check status for each entries,
3998 * and do to remove or to expire.
3999 * XXX: year 2038 problem may remain.
4002 key_timehandler(void)
4006 time_t now = time_second;
4008 s = splnet(); /*called from softclock()*/
4012 struct secpolicy *sp, *nextsp;
4014 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
4015 for (sp = LIST_FIRST(&sptree[dir]);
4019 nextsp = LIST_NEXT(sp, chain);
4021 if (sp->state == IPSEC_SPSTATE_DEAD) {
4026 if (sp->lifetime == 0 && sp->validtime == 0)
4029 /* the deletion will occur next time */
4030 if ((sp->lifetime && now - sp->created > sp->lifetime)
4031 || (sp->validtime && now - sp->lastused > sp->validtime)) {
4032 sp->state = IPSEC_SPSTATE_DEAD;
4042 struct secashead *sah, *nextsah;
4043 struct secasvar *sav, *nextsav;
4045 for (sah = LIST_FIRST(&sahtree);
4049 nextsah = LIST_NEXT(sah, chain);
4051 /* if sah has been dead, then delete it and process next sah. */
4052 if (sah->state == SADB_SASTATE_DEAD) {
4057 /* if LARVAL entry doesn't become MATURE, delete it. */
4058 for (sav = LIST_FIRST(&sah->savtree[SADB_SASTATE_LARVAL]);
4062 nextsav = LIST_NEXT(sav, chain);
4064 if (now - sav->created > key_larval_lifetime) {
4070 * check MATURE entry to start to send expire message
4073 for (sav = LIST_FIRST(&sah->savtree[SADB_SASTATE_MATURE]);
4077 nextsav = LIST_NEXT(sav, chain);
4079 /* we don't need to check. */
4080 if (sav->lft_s == NULL)
4084 if (sav->lft_c == NULL) {
4085 ipseclog((LOG_DEBUG,"key_timehandler: "
4086 "There is no CURRENT time, why?\n"));
4090 /* check SOFT lifetime */
4091 if (sav->lft_s->sadb_lifetime_addtime != 0
4092 && now - sav->created > sav->lft_s->sadb_lifetime_addtime) {
4094 * check SA to be used whether or not.
4095 * when SA hasn't been used, delete it.
4097 if (sav->lft_c->sadb_lifetime_usetime == 0) {
4098 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4101 key_sa_chgstate(sav, SADB_SASTATE_DYING);
4103 * XXX If we keep to send expire
4104 * message in the status of
4105 * DYING. Do remove below code.
4110 /* check SOFT lifetime by bytes */
4112 * XXX I don't know the way to delete this SA
4113 * when new SA is installed. Caution when it's
4114 * installed too big lifetime by time.
4116 else if (sav->lft_s->sadb_lifetime_bytes != 0
4117 && sav->lft_s->sadb_lifetime_bytes < sav->lft_c->sadb_lifetime_bytes) {
4119 key_sa_chgstate(sav, SADB_SASTATE_DYING);
4121 * XXX If we keep to send expire
4122 * message in the status of
4123 * DYING. Do remove below code.
4129 /* check DYING entry to change status to DEAD. */
4130 for (sav = LIST_FIRST(&sah->savtree[SADB_SASTATE_DYING]);
4134 nextsav = LIST_NEXT(sav, chain);
4136 /* we don't need to check. */
4137 if (sav->lft_h == NULL)
4141 if (sav->lft_c == NULL) {
4142 ipseclog((LOG_DEBUG, "key_timehandler: "
4143 "There is no CURRENT time, why?\n"));
4147 if (sav->lft_h->sadb_lifetime_addtime != 0
4148 && now - sav->created > sav->lft_h->sadb_lifetime_addtime) {
4149 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4152 #if 0 /* XXX Should we keep to send expire message until HARD lifetime ? */
4153 else if (sav->lft_s != NULL
4154 && sav->lft_s->sadb_lifetime_addtime != 0
4155 && now - sav->created > sav->lft_s->sadb_lifetime_addtime) {
4157 * XXX: should be checked to be
4158 * installed the valid SA.
4162 * If there is no SA then sending
4168 /* check HARD lifetime by bytes */
4169 else if (sav->lft_h->sadb_lifetime_bytes != 0
4170 && sav->lft_h->sadb_lifetime_bytes < sav->lft_c->sadb_lifetime_bytes) {
4171 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4176 /* delete entry in DEAD */
4177 for (sav = LIST_FIRST(&sah->savtree[SADB_SASTATE_DEAD]);
4181 nextsav = LIST_NEXT(sav, chain);
4184 if (sav->state != SADB_SASTATE_DEAD) {
4185 ipseclog((LOG_DEBUG, "key_timehandler: "
4186 "invalid sav->state "
4187 "(queue: %d SA: %d): "
4189 SADB_SASTATE_DEAD, sav->state));
4193 * do not call key_freesav() here.
4194 * sav should already be freed, and sav->refcnt
4195 * shows other references to sav
4196 * (such as from SPD).
4202 #ifndef IPSEC_NONBLOCK_ACQUIRE
4205 struct secacq *acq, *nextacq;
4207 for (acq = LIST_FIRST(&acqtree);
4211 nextacq = LIST_NEXT(acq, chain);
4213 if (now - acq->created > key_blockacq_lifetime
4214 && __LIST_CHAINED(acq)) {
4215 LIST_REMOVE(acq, chain);
4224 struct secspacq *acq, *nextacq;
4226 for (acq = LIST_FIRST(&spacqtree);
4230 nextacq = LIST_NEXT(acq, chain);
4232 if (now - acq->created > key_blockacq_lifetime
4233 && __LIST_CHAINED(acq)) {
4234 LIST_REMOVE(acq, chain);
4240 /* initialize random seed */
4241 if (key_tick_init_random++ > key_int_random) {
4242 key_tick_init_random = 0;
4246 #ifndef IPSEC_DEBUG2
4247 /* do exchange to tick time !! */
4248 (void)timeout((void *)key_timehandler, (void *)0, hz);
4249 #endif /* IPSEC_DEBUG2 */
4256 * to initialize a seed for random()
4261 srandom(time_second);
4269 key_randomfill(&value, sizeof(value));
4274 key_randomfill(p, l)
4280 static int warn = 1;
4283 n = (size_t)read_random(p, (u_int)l);
4287 bcopy(&v, (u_int8_t *)p + n,
4288 l - n < sizeof(v) ? l - n : sizeof(v));
4292 printf("WARNING: pseudo-random number generator "
4293 "used for IPsec processing\n");
4300 * map SADB_SATYPE_* to IPPROTO_*.
4301 * if satype == SADB_SATYPE then satype is mapped to ~0.
4303 * 0: invalid satype.
4306 key_satype2proto(satype)
4310 case SADB_SATYPE_UNSPEC:
4311 return IPSEC_PROTO_ANY;
4312 case SADB_SATYPE_AH:
4314 case SADB_SATYPE_ESP:
4316 case SADB_X_SATYPE_IPCOMP:
4317 return IPPROTO_IPCOMP;
4325 * map IPPROTO_* to SADB_SATYPE_*
4327 * 0: invalid protocol type.
4330 key_proto2satype(proto)
4335 return SADB_SATYPE_AH;
4337 return SADB_SATYPE_ESP;
4338 case IPPROTO_IPCOMP:
4339 return SADB_X_SATYPE_IPCOMP;
4348 * SADB_GETSPI processing is to receive
4349 * <base, (SA2), src address, dst address, (SPI range)>
4350 * from the IKMPd, to assign a unique spi value, to hang on the INBOUND
4351 * tree with the status of LARVAL, and send
4352 * <base, SA(*), address(SD)>
4355 * IN: mhp: pointer to the pointer to each header.
4356 * OUT: NULL if fail.
4357 * other if success, return pointer to the message to send.
4360 key_getspi(so, m, mhp)
4363 const struct sadb_msghdr *mhp;
4365 struct sadb_address *src0, *dst0;
4366 struct secasindex saidx;
4367 struct secashead *newsah;
4368 struct secasvar *newsav;
4376 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
4377 panic("key_getspi: NULL pointer is passed.\n");
4379 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
4380 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
4381 ipseclog((LOG_DEBUG, "key_getspi: invalid message is passed.\n"));
4382 return key_senderror(so, m, EINVAL);
4384 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
4385 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
4386 ipseclog((LOG_DEBUG, "key_getspi: invalid message is passed.\n"));
4387 return key_senderror(so, m, EINVAL);
4389 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
4390 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
4391 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
4393 mode = IPSEC_MODE_ANY;
4397 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
4398 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
4400 /* map satype to proto */
4401 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
4402 ipseclog((LOG_DEBUG, "key_getspi: invalid satype is passed.\n"));
4403 return key_senderror(so, m, EINVAL);
4406 /* make sure if port number is zero. */
4407 switch (((struct sockaddr *)(src0 + 1))->sa_family) {
4409 if (((struct sockaddr *)(src0 + 1))->sa_len !=
4410 sizeof(struct sockaddr_in))
4411 return key_senderror(so, m, EINVAL);
4412 ((struct sockaddr_in *)(src0 + 1))->sin_port = 0;
4415 if (((struct sockaddr *)(src0 + 1))->sa_len !=
4416 sizeof(struct sockaddr_in6))
4417 return key_senderror(so, m, EINVAL);
4418 ((struct sockaddr_in6 *)(src0 + 1))->sin6_port = 0;
4423 switch (((struct sockaddr *)(dst0 + 1))->sa_family) {
4425 if (((struct sockaddr *)(dst0 + 1))->sa_len !=
4426 sizeof(struct sockaddr_in))
4427 return key_senderror(so, m, EINVAL);
4428 ((struct sockaddr_in *)(dst0 + 1))->sin_port = 0;
4431 if (((struct sockaddr *)(dst0 + 1))->sa_len !=
4432 sizeof(struct sockaddr_in6))
4433 return key_senderror(so, m, EINVAL);
4434 ((struct sockaddr_in6 *)(dst0 + 1))->sin6_port = 0;
4440 /* XXX boundary check against sa_len */
4441 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
4443 /* SPI allocation */
4444 spi = key_do_getnewspi((struct sadb_spirange *)mhp->ext[SADB_EXT_SPIRANGE],
4447 return key_senderror(so, m, EINVAL);
4449 /* get a SA index */
4450 if ((newsah = key_getsah(&saidx)) == NULL) {
4451 /* create a new SA index */
4452 if ((newsah = key_newsah(&saidx)) == NULL) {
4453 ipseclog((LOG_DEBUG, "key_getspi: No more memory.\n"));
4454 return key_senderror(so, m, ENOBUFS);
4460 newsav = KEY_NEWSAV(m, mhp, newsah, &error);
4461 if (newsav == NULL) {
4462 /* XXX don't free new SA index allocated in above. */
4463 return key_senderror(so, m, error);
4467 newsav->spi = htonl(spi);
4469 #ifndef IPSEC_NONBLOCK_ACQUIRE
4470 /* delete the entry in acqtree */
4471 if (mhp->msg->sadb_msg_seq != 0) {
4473 if ((acq = key_getacqbyseq(mhp->msg->sadb_msg_seq)) != NULL) {
4474 /* reset counter in order to deletion by timehandler. */
4475 acq->created = time_second;
4482 struct mbuf *n, *nn;
4483 struct sadb_sa *m_sa;
4484 struct sadb_msg *newmsg;
4487 /* create new sadb_msg to reply. */
4488 len = PFKEY_ALIGN8(sizeof(struct sadb_msg)) +
4489 PFKEY_ALIGN8(sizeof(struct sadb_sa));
4491 return key_senderror(so, m, ENOBUFS);
4493 MGETHDR(n, M_DONTWAIT, MT_DATA);
4495 MCLGET(n, M_DONTWAIT);
4496 if ((n->m_flags & M_EXT) == 0) {
4502 return key_senderror(so, m, ENOBUFS);
4508 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
4509 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
4511 m_sa = (struct sadb_sa *)(mtod(n, caddr_t) + off);
4512 m_sa->sadb_sa_len = PFKEY_UNIT64(sizeof(struct sadb_sa));
4513 m_sa->sadb_sa_exttype = SADB_EXT_SA;
4514 m_sa->sadb_sa_spi = htonl(spi);
4515 off += PFKEY_ALIGN8(sizeof(struct sadb_sa));
4519 panic("length inconsistency in key_getspi");
4522 n->m_next = key_gather_mbuf(m, mhp, 0, 2, SADB_EXT_ADDRESS_SRC,
4523 SADB_EXT_ADDRESS_DST);
4526 return key_senderror(so, m, ENOBUFS);
4529 if (n->m_len < sizeof(struct sadb_msg)) {
4530 n = m_pullup(n, sizeof(struct sadb_msg));
4532 return key_sendup_mbuf(so, m, KEY_SENDUP_ONE);
4535 n->m_pkthdr.len = 0;
4536 for (nn = n; nn; nn = nn->m_next)
4537 n->m_pkthdr.len += nn->m_len;
4539 newmsg = mtod(n, struct sadb_msg *);
4540 newmsg->sadb_msg_seq = newsav->seq;
4541 newmsg->sadb_msg_errno = 0;
4542 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
4545 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
4550 * allocating new SPI
4551 * called by key_getspi().
4557 key_do_getnewspi(spirange, saidx)
4558 struct sadb_spirange *spirange;
4559 struct secasindex *saidx;
4563 int count = key_spi_trycnt;
4565 /* set spi range to allocate */
4566 if (spirange != NULL) {
4567 min = spirange->sadb_spirange_min;
4568 max = spirange->sadb_spirange_max;
4570 min = key_spi_minval;
4571 max = key_spi_maxval;
4573 /* IPCOMP needs 2-byte SPI */
4574 if (saidx->proto == IPPROTO_IPCOMP) {
4581 t = min; min = max; max = t;
4586 if (key_checkspidup(saidx, min) != NULL) {
4587 ipseclog((LOG_DEBUG, "key_do_getnewspi: SPI %u exists already.\n", min));
4591 count--; /* taking one cost. */
4599 /* when requesting to allocate spi ranged */
4601 /* generate pseudo-random SPI value ranged. */
4602 newspi = min + (key_random() % (max - min + 1));
4604 if (key_checkspidup(saidx, newspi) == NULL)
4608 if (count == 0 || newspi == 0) {
4609 ipseclog((LOG_DEBUG, "key_do_getnewspi: to allocate spi is failed.\n"));
4615 keystat.getspi_count =
4616 (keystat.getspi_count + key_spi_trycnt - count) / 2;
4622 * SADB_UPDATE processing
4624 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4625 * key(AE), (identity(SD),) (sensitivity)>
4626 * from the ikmpd, and update a secasvar entry whose status is SADB_SASTATE_LARVAL.
4628 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4629 * (identity(SD),) (sensitivity)>
4632 * m will always be freed.
4635 key_update(so, m, mhp)
4638 const struct sadb_msghdr *mhp;
4640 struct sadb_sa *sa0;
4641 struct sadb_address *src0, *dst0;
4642 struct secasindex saidx;
4643 struct secashead *sah;
4644 struct secasvar *sav;
4651 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
4652 panic("key_update: NULL pointer is passed.\n");
4654 /* map satype to proto */
4655 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
4656 ipseclog((LOG_DEBUG, "key_update: invalid satype is passed.\n"));
4657 return key_senderror(so, m, EINVAL);
4660 if (mhp->ext[SADB_EXT_SA] == NULL ||
4661 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
4662 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
4663 (mhp->msg->sadb_msg_satype == SADB_SATYPE_ESP &&
4664 mhp->ext[SADB_EXT_KEY_ENCRYPT] == NULL) ||
4665 (mhp->msg->sadb_msg_satype == SADB_SATYPE_AH &&
4666 mhp->ext[SADB_EXT_KEY_AUTH] == NULL) ||
4667 (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL &&
4668 mhp->ext[SADB_EXT_LIFETIME_SOFT] == NULL) ||
4669 (mhp->ext[SADB_EXT_LIFETIME_HARD] == NULL &&
4670 mhp->ext[SADB_EXT_LIFETIME_SOFT] != NULL)) {
4671 ipseclog((LOG_DEBUG, "key_update: invalid message is passed.\n"));
4672 return key_senderror(so, m, EINVAL);
4674 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
4675 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
4676 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
4677 ipseclog((LOG_DEBUG, "key_update: invalid message is passed.\n"));
4678 return key_senderror(so, m, EINVAL);
4680 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
4681 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
4682 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
4684 mode = IPSEC_MODE_ANY;
4687 /* XXX boundary checking for other extensions */
4689 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
4690 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
4691 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
4693 /* XXX boundary check against sa_len */
4694 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
4696 /* get a SA header */
4697 if ((sah = key_getsah(&saidx)) == NULL) {
4698 ipseclog((LOG_DEBUG, "key_update: no SA index found.\n"));
4699 return key_senderror(so, m, ENOENT);
4702 /* set spidx if there */
4704 error = key_setident(sah, m, mhp);
4706 return key_senderror(so, m, error);
4708 /* find a SA with sequence number. */
4709 #ifdef IPSEC_DOSEQCHECK
4710 if (mhp->msg->sadb_msg_seq != 0
4711 && (sav = key_getsavbyseq(sah, mhp->msg->sadb_msg_seq)) == NULL) {
4712 ipseclog((LOG_DEBUG,
4713 "key_update: no larval SA with sequence %u exists.\n",
4714 mhp->msg->sadb_msg_seq));
4715 return key_senderror(so, m, ENOENT);
4718 if ((sav = key_getsavbyspi(sah, sa0->sadb_sa_spi)) == NULL) {
4719 ipseclog((LOG_DEBUG,
4720 "key_update: no such a SA found (spi:%u)\n",
4721 (u_int32_t)ntohl(sa0->sadb_sa_spi)));
4722 return key_senderror(so, m, EINVAL);
4726 /* validity check */
4727 if (sav->sah->saidx.proto != proto) {
4728 ipseclog((LOG_DEBUG,
4729 "key_update: protocol mismatched (DB=%u param=%u)\n",
4730 sav->sah->saidx.proto, proto));
4731 return key_senderror(so, m, EINVAL);
4733 #ifdef IPSEC_DOSEQCHECK
4734 if (sav->spi != sa0->sadb_sa_spi) {
4735 ipseclog((LOG_DEBUG,
4736 "key_update: SPI mismatched (DB:%u param:%u)\n",
4737 (u_int32_t)ntohl(sav->spi),
4738 (u_int32_t)ntohl(sa0->sadb_sa_spi)));
4739 return key_senderror(so, m, EINVAL);
4742 if (sav->pid != mhp->msg->sadb_msg_pid) {
4743 ipseclog((LOG_DEBUG,
4744 "key_update: pid mismatched (DB:%u param:%u)\n",
4745 sav->pid, mhp->msg->sadb_msg_pid));
4746 return key_senderror(so, m, EINVAL);
4749 /* copy sav values */
4750 error = key_setsaval(sav, m, mhp);
4753 return key_senderror(so, m, error);
4756 /* check SA values to be mature. */
4757 if ((mhp->msg->sadb_msg_errno = key_mature(sav)) != 0) {
4759 return key_senderror(so, m, 0);
4765 /* set msg buf from mhp */
4766 n = key_getmsgbuf_x1(m, mhp);
4768 ipseclog((LOG_DEBUG, "key_update: No more memory.\n"));
4769 return key_senderror(so, m, ENOBUFS);
4773 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
4778 * search SAD with sequence for a SA which state is SADB_SASTATE_LARVAL.
4779 * only called by key_update().
4782 * others : found, pointer to a SA.
4784 #ifdef IPSEC_DOSEQCHECK
4785 static struct secasvar *
4786 key_getsavbyseq(sah, seq)
4787 struct secashead *sah;
4790 struct secasvar *sav;
4793 state = SADB_SASTATE_LARVAL;
4795 /* search SAD with sequence number ? */
4796 LIST_FOREACH(sav, &sah->savtree[state], chain) {
4798 KEY_CHKSASTATE(state, sav->state, "key_getsabyseq");
4800 if (sav->seq == seq) {
4802 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
4803 printf("DP key_getsavbyseq cause "
4804 "refcnt++:%d SA:%p\n",
4815 * SADB_ADD processing
4816 * add an entry to SA database, when received
4817 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4818 * key(AE), (identity(SD),) (sensitivity)>
4821 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4822 * (identity(SD),) (sensitivity)>
4825 * IGNORE identity and sensitivity messages.
4827 * m will always be freed.
4833 const struct sadb_msghdr *mhp;
4835 struct sadb_sa *sa0;
4836 struct sadb_address *src0, *dst0;
4837 struct secasindex saidx;
4838 struct secashead *newsah;
4839 struct secasvar *newsav;
4846 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
4847 panic("key_add: NULL pointer is passed.\n");
4849 /* map satype to proto */
4850 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
4851 ipseclog((LOG_DEBUG, "key_add: invalid satype is passed.\n"));
4852 return key_senderror(so, m, EINVAL);
4855 if (mhp->ext[SADB_EXT_SA] == NULL ||
4856 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
4857 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
4858 (mhp->msg->sadb_msg_satype == SADB_SATYPE_ESP &&
4859 mhp->ext[SADB_EXT_KEY_ENCRYPT] == NULL) ||
4860 (mhp->msg->sadb_msg_satype == SADB_SATYPE_AH &&
4861 mhp->ext[SADB_EXT_KEY_AUTH] == NULL) ||
4862 (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL &&
4863 mhp->ext[SADB_EXT_LIFETIME_SOFT] == NULL) ||
4864 (mhp->ext[SADB_EXT_LIFETIME_HARD] == NULL &&
4865 mhp->ext[SADB_EXT_LIFETIME_SOFT] != NULL)) {
4866 ipseclog((LOG_DEBUG, "key_add: invalid message is passed.\n"));
4867 return key_senderror(so, m, EINVAL);
4869 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
4870 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
4871 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
4873 ipseclog((LOG_DEBUG, "key_add: invalid message is passed.\n"));
4874 return key_senderror(so, m, EINVAL);
4876 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
4877 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
4878 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
4880 mode = IPSEC_MODE_ANY;
4884 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
4885 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
4886 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
4888 /* XXX boundary check against sa_len */
4889 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
4891 /* get a SA header */
4892 if ((newsah = key_getsah(&saidx)) == NULL) {
4893 /* create a new SA header */
4894 if ((newsah = key_newsah(&saidx)) == NULL) {
4895 ipseclog((LOG_DEBUG, "key_add: No more memory.\n"));
4896 return key_senderror(so, m, ENOBUFS);
4900 /* set spidx if there */
4902 error = key_setident(newsah, m, mhp);
4904 return key_senderror(so, m, error);
4907 /* create new SA entry. */
4908 /* We can create new SA only if SPI is differenct. */
4909 if (key_getsavbyspi(newsah, sa0->sadb_sa_spi)) {
4910 ipseclog((LOG_DEBUG, "key_add: SA already exists.\n"));
4911 return key_senderror(so, m, EEXIST);
4913 newsav = KEY_NEWSAV(m, mhp, newsah, &error);
4914 if (newsav == NULL) {
4915 return key_senderror(so, m, error);
4918 /* check SA values to be mature. */
4919 if ((error = key_mature(newsav)) != 0) {
4920 KEY_FREESAV(&newsav);
4921 return key_senderror(so, m, error);
4925 * don't call key_freesav() here, as we would like to keep the SA
4926 * in the database on success.
4932 /* set msg buf from mhp */
4933 n = key_getmsgbuf_x1(m, mhp);
4935 ipseclog((LOG_DEBUG, "key_update: No more memory.\n"));
4936 return key_senderror(so, m, ENOBUFS);
4940 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
4946 key_setident(sah, m, mhp)
4947 struct secashead *sah;
4949 const struct sadb_msghdr *mhp;
4951 const struct sadb_ident *idsrc, *iddst;
4952 int idsrclen, iddstlen;
4955 if (sah == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
4956 panic("key_setident: NULL pointer is passed.\n");
4958 /* don't make buffer if not there */
4959 if (mhp->ext[SADB_EXT_IDENTITY_SRC] == NULL &&
4960 mhp->ext[SADB_EXT_IDENTITY_DST] == NULL) {
4966 if (mhp->ext[SADB_EXT_IDENTITY_SRC] == NULL ||
4967 mhp->ext[SADB_EXT_IDENTITY_DST] == NULL) {
4968 ipseclog((LOG_DEBUG, "key_setident: invalid identity.\n"));
4972 idsrc = (const struct sadb_ident *)mhp->ext[SADB_EXT_IDENTITY_SRC];
4973 iddst = (const struct sadb_ident *)mhp->ext[SADB_EXT_IDENTITY_DST];
4974 idsrclen = mhp->extlen[SADB_EXT_IDENTITY_SRC];
4975 iddstlen = mhp->extlen[SADB_EXT_IDENTITY_DST];
4977 /* validity check */
4978 if (idsrc->sadb_ident_type != iddst->sadb_ident_type) {
4979 ipseclog((LOG_DEBUG, "key_setident: ident type mismatch.\n"));
4983 switch (idsrc->sadb_ident_type) {
4984 case SADB_IDENTTYPE_PREFIX:
4985 case SADB_IDENTTYPE_FQDN:
4986 case SADB_IDENTTYPE_USERFQDN:
4988 /* XXX do nothing */
4994 /* make structure */
4995 KMALLOC(sah->idents, struct sadb_ident *, idsrclen);
4996 if (sah->idents == NULL) {
4997 ipseclog((LOG_DEBUG, "key_setident: No more memory.\n"));
5000 KMALLOC(sah->identd, struct sadb_ident *, iddstlen);
5001 if (sah->identd == NULL) {
5004 ipseclog((LOG_DEBUG, "key_setident: No more memory.\n"));
5007 bcopy(idsrc, sah->idents, idsrclen);
5008 bcopy(iddst, sah->identd, iddstlen);
5014 * m will not be freed on return.
5015 * it is caller's responsibility to free the result.
5017 static struct mbuf *
5018 key_getmsgbuf_x1(m, mhp)
5020 const struct sadb_msghdr *mhp;
5025 if (m == NULL || mhp == NULL || mhp->msg == NULL)
5026 panic("key_getmsgbuf_x1: NULL pointer is passed.\n");
5028 /* create new sadb_msg to reply. */
5029 n = key_gather_mbuf(m, mhp, 1, 9, SADB_EXT_RESERVED,
5030 SADB_EXT_SA, SADB_X_EXT_SA2,
5031 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST,
5032 SADB_EXT_LIFETIME_HARD, SADB_EXT_LIFETIME_SOFT,
5033 SADB_EXT_IDENTITY_SRC, SADB_EXT_IDENTITY_DST);
5037 if (n->m_len < sizeof(struct sadb_msg)) {
5038 n = m_pullup(n, sizeof(struct sadb_msg));
5042 mtod(n, struct sadb_msg *)->sadb_msg_errno = 0;
5043 mtod(n, struct sadb_msg *)->sadb_msg_len =
5044 PFKEY_UNIT64(n->m_pkthdr.len);
5049 static int key_delete_all (struct socket *, struct mbuf *,
5050 const struct sadb_msghdr *, u_int16_t);
5053 * SADB_DELETE processing
5055 * <base, SA(*), address(SD)>
5056 * from the ikmpd, and set SADB_SASTATE_DEAD,
5058 * <base, SA(*), address(SD)>
5061 * m will always be freed.
5064 key_delete(so, m, mhp)
5067 const struct sadb_msghdr *mhp;
5069 struct sadb_sa *sa0;
5070 struct sadb_address *src0, *dst0;
5071 struct secasindex saidx;
5072 struct secashead *sah;
5073 struct secasvar *sav = NULL;
5077 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
5078 panic("key_delete: NULL pointer is passed.\n");
5080 /* map satype to proto */
5081 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5082 ipseclog((LOG_DEBUG, "key_delete: invalid satype is passed.\n"));
5083 return key_senderror(so, m, EINVAL);
5086 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5087 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
5088 ipseclog((LOG_DEBUG, "key_delete: invalid message is passed.\n"));
5089 return key_senderror(so, m, EINVAL);
5092 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5093 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5094 ipseclog((LOG_DEBUG, "key_delete: invalid message is passed.\n"));
5095 return key_senderror(so, m, EINVAL);
5098 if (mhp->ext[SADB_EXT_SA] == NULL) {
5100 * Caller wants us to delete all non-LARVAL SAs
5101 * that match the src/dst. This is used during
5102 * IKE INITIAL-CONTACT.
5104 ipseclog((LOG_DEBUG, "key_delete: doing delete all.\n"));
5105 return key_delete_all(so, m, mhp, proto);
5106 } else if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa)) {
5107 ipseclog((LOG_DEBUG, "key_delete: invalid message is passed.\n"));
5108 return key_senderror(so, m, EINVAL);
5111 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5112 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
5113 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
5115 /* XXX boundary check against sa_len */
5116 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5118 /* get a SA header */
5119 LIST_FOREACH(sah, &sahtree, chain) {
5120 if (sah->state == SADB_SASTATE_DEAD)
5122 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5125 /* get a SA with SPI. */
5126 sav = key_getsavbyspi(sah, sa0->sadb_sa_spi);
5131 ipseclog((LOG_DEBUG, "key_delete: no SA found.\n"));
5132 return key_senderror(so, m, ENOENT);
5135 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
5140 struct sadb_msg *newmsg;
5142 /* create new sadb_msg to reply. */
5143 n = key_gather_mbuf(m, mhp, 1, 4, SADB_EXT_RESERVED,
5144 SADB_EXT_SA, SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
5146 return key_senderror(so, m, ENOBUFS);
5148 if (n->m_len < sizeof(struct sadb_msg)) {
5149 n = m_pullup(n, sizeof(struct sadb_msg));
5151 return key_senderror(so, m, ENOBUFS);
5153 newmsg = mtod(n, struct sadb_msg *);
5154 newmsg->sadb_msg_errno = 0;
5155 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
5158 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5163 * delete all SAs for src/dst. Called from key_delete().
5166 key_delete_all(so, m, mhp, proto)
5169 const struct sadb_msghdr *mhp;
5172 struct sadb_address *src0, *dst0;
5173 struct secasindex saidx;
5174 struct secashead *sah;
5175 struct secasvar *sav, *nextsav;
5176 u_int stateidx, state;
5178 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
5179 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
5181 /* XXX boundary check against sa_len */
5182 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5184 LIST_FOREACH(sah, &sahtree, chain) {
5185 if (sah->state == SADB_SASTATE_DEAD)
5187 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5190 /* Delete all non-LARVAL SAs. */
5192 stateidx < _ARRAYLEN(saorder_state_alive);
5194 state = saorder_state_alive[stateidx];
5195 if (state == SADB_SASTATE_LARVAL)
5197 for (sav = LIST_FIRST(&sah->savtree[state]);
5198 sav != NULL; sav = nextsav) {
5199 nextsav = LIST_NEXT(sav, chain);
5201 if (sav->state != state) {
5202 ipseclog((LOG_DEBUG, "key_delete_all: "
5203 "invalid sav->state "
5204 "(queue: %d SA: %d)\n",
5205 state, sav->state));
5209 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
5216 struct sadb_msg *newmsg;
5218 /* create new sadb_msg to reply. */
5219 n = key_gather_mbuf(m, mhp, 1, 3, SADB_EXT_RESERVED,
5220 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
5222 return key_senderror(so, m, ENOBUFS);
5224 if (n->m_len < sizeof(struct sadb_msg)) {
5225 n = m_pullup(n, sizeof(struct sadb_msg));
5227 return key_senderror(so, m, ENOBUFS);
5229 newmsg = mtod(n, struct sadb_msg *);
5230 newmsg->sadb_msg_errno = 0;
5231 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
5234 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5239 * SADB_GET processing
5241 * <base, SA(*), address(SD)>
5242 * from the ikmpd, and get a SP and a SA to respond,
5244 * <base, SA, (lifetime(HSC),) address(SD), (address(P),) key(AE),
5245 * (identity(SD),) (sensitivity)>
5248 * m will always be freed.
5254 const struct sadb_msghdr *mhp;
5256 struct sadb_sa *sa0;
5257 struct sadb_address *src0, *dst0;
5258 struct secasindex saidx;
5259 struct secashead *sah;
5260 struct secasvar *sav = NULL;
5264 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
5265 panic("key_get: NULL pointer is passed.\n");
5267 /* map satype to proto */
5268 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5269 ipseclog((LOG_DEBUG, "key_get: invalid satype is passed.\n"));
5270 return key_senderror(so, m, EINVAL);
5273 if (mhp->ext[SADB_EXT_SA] == NULL ||
5274 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5275 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
5276 ipseclog((LOG_DEBUG, "key_get: invalid message is passed.\n"));
5277 return key_senderror(so, m, EINVAL);
5279 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
5280 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5281 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5282 ipseclog((LOG_DEBUG, "key_get: invalid message is passed.\n"));
5283 return key_senderror(so, m, EINVAL);
5286 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5287 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
5288 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
5290 /* XXX boundary check against sa_len */
5291 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5293 /* get a SA header */
5294 LIST_FOREACH(sah, &sahtree, chain) {
5295 if (sah->state == SADB_SASTATE_DEAD)
5297 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5300 /* get a SA with SPI. */
5301 sav = key_getsavbyspi(sah, sa0->sadb_sa_spi);
5306 ipseclog((LOG_DEBUG, "key_get: no SA found.\n"));
5307 return key_senderror(so, m, ENOENT);
5314 /* map proto to satype */
5315 if ((satype = key_proto2satype(sah->saidx.proto)) == 0) {
5316 ipseclog((LOG_DEBUG, "key_get: there was invalid proto in SAD.\n"));
5317 return key_senderror(so, m, EINVAL);
5320 /* create new sadb_msg to reply. */
5321 n = key_setdumpsa(sav, SADB_GET, satype, mhp->msg->sadb_msg_seq,
5322 mhp->msg->sadb_msg_pid);
5324 return key_senderror(so, m, ENOBUFS);
5327 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
5331 /* XXX make it sysctl-configurable? */
5333 key_getcomb_setlifetime(comb)
5334 struct sadb_comb *comb;
5337 comb->sadb_comb_soft_allocations = 1;
5338 comb->sadb_comb_hard_allocations = 1;
5339 comb->sadb_comb_soft_bytes = 0;
5340 comb->sadb_comb_hard_bytes = 0;
5341 comb->sadb_comb_hard_addtime = 86400; /* 1 day */
5342 comb->sadb_comb_soft_addtime = comb->sadb_comb_soft_addtime * 80 / 100;
5343 comb->sadb_comb_soft_usetime = 28800; /* 8 hours */
5344 comb->sadb_comb_hard_usetime = comb->sadb_comb_hard_usetime * 80 / 100;
5348 * XXX reorder combinations by preference
5349 * XXX no idea if the user wants ESP authentication or not
5351 static struct mbuf *
5354 struct sadb_comb *comb;
5355 struct enc_xform *algo;
5356 struct mbuf *result = NULL, *m, *n;
5360 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
5363 for (i = 1; i <= SADB_EALG_MAX; i++) {
5364 algo = esp_algorithm_lookup(i);
5368 /* discard algorithms with key size smaller than system min */
5369 if (_BITS(algo->maxkey) < ipsec_esp_keymin)
5371 if (_BITS(algo->minkey) < ipsec_esp_keymin)
5372 encmin = ipsec_esp_keymin;
5374 encmin = _BITS(algo->minkey);
5377 m = key_getcomb_ah();
5380 ("key_getcomb_esp: l=%u > MLEN=%lu",
5382 MGET(m, M_DONTWAIT, MT_DATA);
5387 bzero(mtod(m, caddr_t), m->m_len);
5394 for (n = m; n; n = n->m_next)
5396 KASSERT((totlen % l) == 0,
5397 ("key_getcomb_esp: totlen=%u, l=%u", totlen, l));
5399 for (off = 0; off < totlen; off += l) {
5400 n = m_pulldown(m, off, l, &o);
5402 /* m is already freed */
5405 comb = (struct sadb_comb *)(mtod(n, caddr_t) + o);
5406 bzero(comb, sizeof(*comb));
5407 key_getcomb_setlifetime(comb);
5408 comb->sadb_comb_encrypt = i;
5409 comb->sadb_comb_encrypt_minbits = encmin;
5410 comb->sadb_comb_encrypt_maxbits = _BITS(algo->maxkey);
5429 const struct auth_hash *ah,
5434 *min = *max = ah->keysize;
5435 if (ah->keysize == 0) {
5437 * Transform takes arbitrary key size but algorithm
5438 * key size is restricted. Enforce this here.
5441 case SADB_X_AALG_MD5: *min = *max = 16; break;
5442 case SADB_X_AALG_SHA: *min = *max = 20; break;
5443 case SADB_X_AALG_NULL: *min = 1; *max = 256; break;
5445 DPRINTF(("key_getsizes_ah: unknown AH algorithm %u\n",
5453 * XXX reorder combinations by preference
5455 static struct mbuf *
5458 struct sadb_comb *comb;
5459 struct auth_hash *algo;
5461 u_int16_t minkeysize, maxkeysize;
5463 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
5466 for (i = 1; i <= SADB_AALG_MAX; i++) {
5468 /* we prefer HMAC algorithms, not old algorithms */
5469 if (i != SADB_AALG_SHA1HMAC && i != SADB_AALG_MD5HMAC)
5472 algo = ah_algorithm_lookup(i);
5475 key_getsizes_ah(algo, i, &minkeysize, &maxkeysize);
5476 /* discard algorithms with key size smaller than system min */
5477 if (_BITS(minkeysize) < ipsec_ah_keymin)
5482 ("key_getcomb_ah: l=%u > MLEN=%lu",
5484 MGET(m, M_DONTWAIT, MT_DATA);
5491 M_PREPEND(m, l, M_DONTWAIT);
5495 comb = mtod(m, struct sadb_comb *);
5496 bzero(comb, sizeof(*comb));
5497 key_getcomb_setlifetime(comb);
5498 comb->sadb_comb_auth = i;
5499 comb->sadb_comb_auth_minbits = _BITS(minkeysize);
5500 comb->sadb_comb_auth_maxbits = _BITS(maxkeysize);
5507 * not really an official behavior. discussed in pf_key@inner.net in Sep2000.
5508 * XXX reorder combinations by preference
5510 static struct mbuf *
5511 key_getcomb_ipcomp()
5513 struct sadb_comb *comb;
5514 struct comp_algo *algo;
5517 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
5520 for (i = 1; i <= SADB_X_CALG_MAX; i++) {
5521 algo = ipcomp_algorithm_lookup(i);
5527 ("key_getcomb_ipcomp: l=%u > MLEN=%lu",
5529 MGET(m, M_DONTWAIT, MT_DATA);
5536 M_PREPEND(m, l, M_DONTWAIT);
5540 comb = mtod(m, struct sadb_comb *);
5541 bzero(comb, sizeof(*comb));
5542 key_getcomb_setlifetime(comb);
5543 comb->sadb_comb_encrypt = i;
5544 /* what should we set into sadb_comb_*_{min,max}bits? */
5551 * XXX no way to pass mode (transport/tunnel) to userland
5552 * XXX replay checking?
5553 * XXX sysctl interface to ipsec_{ah,esp}_keymin
5555 static struct mbuf *
5557 const struct secasindex *saidx;
5559 struct sadb_prop *prop;
5561 const int l = PFKEY_ALIGN8(sizeof(struct sadb_prop));
5564 switch (saidx->proto) {
5566 m = key_getcomb_esp();
5569 m = key_getcomb_ah();
5571 case IPPROTO_IPCOMP:
5572 m = key_getcomb_ipcomp();
5580 M_PREPEND(m, l, M_DONTWAIT);
5585 for (n = m; n; n = n->m_next)
5588 prop = mtod(m, struct sadb_prop *);
5589 bzero(prop, sizeof(*prop));
5590 prop->sadb_prop_len = PFKEY_UNIT64(totlen);
5591 prop->sadb_prop_exttype = SADB_EXT_PROPOSAL;
5592 prop->sadb_prop_replay = 32; /* XXX */
5598 * SADB_ACQUIRE processing called by key_checkrequest() and key_acquire2().
5600 * <base, SA, address(SD), (address(P)), x_policy,
5601 * (identity(SD),) (sensitivity,) proposal>
5602 * to KMD, and expect to receive
5603 * <base> with SADB_ACQUIRE if error occured,
5605 * <base, src address, dst address, (SPI range)> with SADB_GETSPI
5606 * from KMD by PF_KEY.
5608 * XXX x_policy is outside of RFC2367 (KAME extension).
5609 * XXX sensitivity is not supported.
5610 * XXX for ipcomp, RFC2367 does not define how to fill in proposal.
5611 * see comment for key_getcomb_ipcomp().
5615 * others: error number
5618 key_acquire(const struct secasindex *saidx, struct secpolicy *sp)
5620 struct mbuf *result = NULL, *m;
5621 #ifndef IPSEC_NONBLOCK_ACQUIRE
5622 struct secacq *newacq;
5629 KASSERT(saidx != NULL, ("key_acquire: null saidx"));
5630 satype = key_proto2satype(saidx->proto);
5631 KASSERT(satype != 0,
5632 ("key_acquire: null satype, protocol %u", saidx->proto));
5634 #ifndef IPSEC_NONBLOCK_ACQUIRE
5636 * We never do anything about acquirng SA. There is anather
5637 * solution that kernel blocks to send SADB_ACQUIRE message until
5638 * getting something message from IKEd. In later case, to be
5639 * managed with ACQUIRING list.
5641 /* Get an entry to check whether sending message or not. */
5642 if ((newacq = key_getacq(saidx)) != NULL) {
5643 if (key_blockacq_count < newacq->count) {
5644 /* reset counter and do send message. */
5647 /* increment counter and do nothing. */
5652 /* make new entry for blocking to send SADB_ACQUIRE. */
5653 if ((newacq = key_newacq(saidx)) == NULL)
5656 /* add to acqtree */
5657 LIST_INSERT_HEAD(&acqtree, newacq, chain);
5662 #ifndef IPSEC_NONBLOCK_ACQUIRE
5665 seq = (acq_seq = (acq_seq == ~0 ? 1 : ++acq_seq));
5667 m = key_setsadbmsg(SADB_ACQUIRE, 0, satype, seq, 0, 0);
5674 /* set sadb_address for saidx's. */
5675 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
5676 &saidx->src.sa, FULLMASK, IPSEC_ULPROTO_ANY);
5683 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
5684 &saidx->dst.sa, FULLMASK, IPSEC_ULPROTO_ANY);
5691 /* XXX proxy address (optional) */
5693 /* set sadb_x_policy */
5695 m = key_setsadbxpolicy(sp->policy, sp->spidx.dir, sp->id);
5703 /* XXX identity (optional) */
5705 if (idexttype && fqdn) {
5706 /* create identity extension (FQDN) */
5707 struct sadb_ident *id;
5710 fqdnlen = strlen(fqdn) + 1; /* +1 for terminating-NUL */
5711 id = (struct sadb_ident *)p;
5712 bzero(id, sizeof(*id) + PFKEY_ALIGN8(fqdnlen));
5713 id->sadb_ident_len = PFKEY_UNIT64(sizeof(*id) + PFKEY_ALIGN8(fqdnlen));
5714 id->sadb_ident_exttype = idexttype;
5715 id->sadb_ident_type = SADB_IDENTTYPE_FQDN;
5716 bcopy(fqdn, id + 1, fqdnlen);
5717 p += sizeof(struct sadb_ident) + PFKEY_ALIGN8(fqdnlen);
5721 /* create identity extension (USERFQDN) */
5722 struct sadb_ident *id;
5726 /* +1 for terminating-NUL */
5727 userfqdnlen = strlen(userfqdn) + 1;
5730 id = (struct sadb_ident *)p;
5731 bzero(id, sizeof(*id) + PFKEY_ALIGN8(userfqdnlen));
5732 id->sadb_ident_len = PFKEY_UNIT64(sizeof(*id) + PFKEY_ALIGN8(userfqdnlen));
5733 id->sadb_ident_exttype = idexttype;
5734 id->sadb_ident_type = SADB_IDENTTYPE_USERFQDN;
5735 /* XXX is it correct? */
5736 if (curproc && curproc->p_cred)
5737 id->sadb_ident_id = curproc->p_cred->p_ruid;
5738 if (userfqdn && userfqdnlen)
5739 bcopy(userfqdn, id + 1, userfqdnlen);
5740 p += sizeof(struct sadb_ident) + PFKEY_ALIGN8(userfqdnlen);
5744 /* XXX sensitivity (optional) */
5746 /* create proposal/combination extension */
5747 m = key_getprop(saidx);
5750 * spec conformant: always attach proposal/combination extension,
5751 * the problem is that we have no way to attach it for ipcomp,
5752 * due to the way sadb_comb is declared in RFC2367.
5761 * outside of spec; make proposal/combination extension optional.
5767 if ((result->m_flags & M_PKTHDR) == 0) {
5772 if (result->m_len < sizeof(struct sadb_msg)) {
5773 result = m_pullup(result, sizeof(struct sadb_msg));
5774 if (result == NULL) {
5780 result->m_pkthdr.len = 0;
5781 for (m = result; m; m = m->m_next)
5782 result->m_pkthdr.len += m->m_len;
5784 mtod(result, struct sadb_msg *)->sadb_msg_len =
5785 PFKEY_UNIT64(result->m_pkthdr.len);
5787 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
5795 #ifndef IPSEC_NONBLOCK_ACQUIRE
5796 static struct secacq *
5797 key_newacq(const struct secasindex *saidx)
5799 struct secacq *newacq;
5802 KMALLOC(newacq, struct secacq *, sizeof(struct secacq));
5803 if (newacq == NULL) {
5804 ipseclog((LOG_DEBUG, "key_newacq: No more memory.\n"));
5807 bzero(newacq, sizeof(*newacq));
5810 bcopy(saidx, &newacq->saidx, sizeof(newacq->saidx));
5811 newacq->seq = (acq_seq == ~0 ? 1 : ++acq_seq);
5812 newacq->created = time_second;
5818 static struct secacq *
5819 key_getacq(const struct secasindex *saidx)
5823 LIST_FOREACH(acq, &acqtree, chain) {
5824 if (key_cmpsaidx(saidx, &acq->saidx, CMP_EXACTLY))
5831 static struct secacq *
5832 key_getacqbyseq(seq)
5837 LIST_FOREACH(acq, &acqtree, chain) {
5838 if (acq->seq == seq)
5846 static struct secspacq *
5848 struct secpolicyindex *spidx;
5850 struct secspacq *acq;
5853 KMALLOC(acq, struct secspacq *, sizeof(struct secspacq));
5855 ipseclog((LOG_DEBUG, "key_newspacq: No more memory.\n"));
5858 bzero(acq, sizeof(*acq));
5861 bcopy(spidx, &acq->spidx, sizeof(acq->spidx));
5862 acq->created = time_second;
5868 static struct secspacq *
5870 struct secpolicyindex *spidx;
5872 struct secspacq *acq;
5874 LIST_FOREACH(acq, &spacqtree, chain) {
5875 if (key_cmpspidx_exactly(spidx, &acq->spidx))
5883 * SADB_ACQUIRE processing,
5884 * in first situation, is receiving
5886 * from the ikmpd, and clear sequence of its secasvar entry.
5888 * In second situation, is receiving
5889 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
5890 * from a user land process, and return
5891 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
5894 * m will always be freed.
5897 key_acquire2(so, m, mhp)
5900 const struct sadb_msghdr *mhp;
5902 const struct sadb_address *src0, *dst0;
5903 struct secasindex saidx;
5904 struct secashead *sah;
5909 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
5910 panic("key_acquire2: NULL pointer is passed.\n");
5913 * Error message from KMd.
5914 * We assume that if error was occured in IKEd, the length of PFKEY
5915 * message is equal to the size of sadb_msg structure.
5916 * We do not raise error even if error occured in this function.
5918 if (mhp->msg->sadb_msg_len == PFKEY_UNIT64(sizeof(struct sadb_msg))) {
5919 #ifndef IPSEC_NONBLOCK_ACQUIRE
5922 /* check sequence number */
5923 if (mhp->msg->sadb_msg_seq == 0) {
5924 ipseclog((LOG_DEBUG, "key_acquire2: must specify sequence number.\n"));
5929 if ((acq = key_getacqbyseq(mhp->msg->sadb_msg_seq)) == NULL) {
5931 * the specified larval SA is already gone, or we got
5932 * a bogus sequence number. we can silently ignore it.
5938 /* reset acq counter in order to deletion by timehander. */
5939 acq->created = time_second;
5947 * This message is from user land.
5950 /* map satype to proto */
5951 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5952 ipseclog((LOG_DEBUG, "key_acquire2: invalid satype is passed.\n"));
5953 return key_senderror(so, m, EINVAL);
5956 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5957 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
5958 mhp->ext[SADB_EXT_PROPOSAL] == NULL) {
5960 ipseclog((LOG_DEBUG, "key_acquire2: invalid message is passed.\n"));
5961 return key_senderror(so, m, EINVAL);
5963 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5964 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
5965 mhp->extlen[SADB_EXT_PROPOSAL] < sizeof(struct sadb_prop)) {
5967 ipseclog((LOG_DEBUG, "key_acquire2: invalid message is passed.\n"));
5968 return key_senderror(so, m, EINVAL);
5971 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
5972 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
5974 /* XXX boundary check against sa_len */
5975 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5977 /* get a SA index */
5978 LIST_FOREACH(sah, &sahtree, chain) {
5979 if (sah->state == SADB_SASTATE_DEAD)
5981 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_MODE_REQID))
5985 ipseclog((LOG_DEBUG, "key_acquire2: a SA exists already.\n"));
5986 return key_senderror(so, m, EEXIST);
5989 error = key_acquire(&saidx, NULL);
5991 ipseclog((LOG_DEBUG, "key_acquire2: error %d returned "
5992 "from key_acquire.\n", mhp->msg->sadb_msg_errno));
5993 return key_senderror(so, m, error);
5996 return key_sendup_mbuf(so, m, KEY_SENDUP_REGISTERED);
6000 * SADB_REGISTER processing.
6001 * If SATYPE_UNSPEC has been passed as satype, only return sabd_supported.
6004 * from the ikmpd, and register a socket to send PF_KEY messages,
6008 * If socket is detached, must free from regnode.
6010 * m will always be freed.
6013 key_register(so, m, mhp)
6016 const struct sadb_msghdr *mhp;
6018 struct secreg *reg, *newreg = 0;
6021 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
6022 panic("key_register: NULL pointer is passed.\n");
6024 /* check for invalid register message */
6025 if (mhp->msg->sadb_msg_satype >= sizeof(regtree)/sizeof(regtree[0]))
6026 return key_senderror(so, m, EINVAL);
6028 /* When SATYPE_UNSPEC is specified, only return sabd_supported. */
6029 if (mhp->msg->sadb_msg_satype == SADB_SATYPE_UNSPEC)
6032 /* check whether existing or not */
6033 LIST_FOREACH(reg, ®tree[mhp->msg->sadb_msg_satype], chain) {
6034 if (reg->so == so) {
6035 ipseclog((LOG_DEBUG, "key_register: socket exists already.\n"));
6036 return key_senderror(so, m, EEXIST);
6040 /* create regnode */
6041 KMALLOC(newreg, struct secreg *, sizeof(*newreg));
6042 if (newreg == NULL) {
6043 ipseclog((LOG_DEBUG, "key_register: No more memory.\n"));
6044 return key_senderror(so, m, ENOBUFS);
6046 bzero((caddr_t)newreg, sizeof(*newreg));
6049 ((struct keycb *)sotorawcb(so))->kp_registered++;
6051 /* add regnode to regtree. */
6052 LIST_INSERT_HEAD(®tree[mhp->msg->sadb_msg_satype], newreg, chain);
6057 struct sadb_msg *newmsg;
6058 struct sadb_supported *sup;
6059 u_int len, alen, elen;
6062 struct sadb_alg *alg;
6064 /* create new sadb_msg to reply. */
6066 for (i = 1; i <= SADB_AALG_MAX; i++) {
6067 if (ah_algorithm_lookup(i))
6068 alen += sizeof(struct sadb_alg);
6071 alen += sizeof(struct sadb_supported);
6073 for (i = 1; i <= SADB_EALG_MAX; i++) {
6074 if (esp_algorithm_lookup(i))
6075 elen += sizeof(struct sadb_alg);
6078 elen += sizeof(struct sadb_supported);
6080 len = sizeof(struct sadb_msg) + alen + elen;
6083 return key_senderror(so, m, ENOBUFS);
6085 MGETHDR(n, M_DONTWAIT, MT_DATA);
6087 MCLGET(n, M_DONTWAIT);
6088 if ((n->m_flags & M_EXT) == 0) {
6094 return key_senderror(so, m, ENOBUFS);
6096 n->m_pkthdr.len = n->m_len = len;
6100 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
6101 newmsg = mtod(n, struct sadb_msg *);
6102 newmsg->sadb_msg_errno = 0;
6103 newmsg->sadb_msg_len = PFKEY_UNIT64(len);
6104 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
6106 /* for authentication algorithm */
6108 sup = (struct sadb_supported *)(mtod(n, caddr_t) + off);
6109 sup->sadb_supported_len = PFKEY_UNIT64(alen);
6110 sup->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
6111 off += PFKEY_ALIGN8(sizeof(*sup));
6113 for (i = 1; i <= SADB_AALG_MAX; i++) {
6114 struct auth_hash *aalgo;
6115 u_int16_t minkeysize, maxkeysize;
6117 aalgo = ah_algorithm_lookup(i);
6120 alg = (struct sadb_alg *)(mtod(n, caddr_t) + off);
6121 alg->sadb_alg_id = i;
6122 alg->sadb_alg_ivlen = 0;
6123 key_getsizes_ah(aalgo, i, &minkeysize, &maxkeysize);
6124 alg->sadb_alg_minbits = _BITS(minkeysize);
6125 alg->sadb_alg_maxbits = _BITS(maxkeysize);
6126 off += PFKEY_ALIGN8(sizeof(*alg));
6130 /* for encryption algorithm */
6132 sup = (struct sadb_supported *)(mtod(n, caddr_t) + off);
6133 sup->sadb_supported_len = PFKEY_UNIT64(elen);
6134 sup->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT;
6135 off += PFKEY_ALIGN8(sizeof(*sup));
6137 for (i = 1; i <= SADB_EALG_MAX; i++) {
6138 struct enc_xform *ealgo;
6140 ealgo = esp_algorithm_lookup(i);
6143 alg = (struct sadb_alg *)(mtod(n, caddr_t) + off);
6144 alg->sadb_alg_id = i;
6145 alg->sadb_alg_ivlen = ealgo->blocksize;
6146 alg->sadb_alg_minbits = _BITS(ealgo->minkey);
6147 alg->sadb_alg_maxbits = _BITS(ealgo->maxkey);
6148 off += PFKEY_ALIGN8(sizeof(struct sadb_alg));
6154 panic("length assumption failed in key_register");
6158 return key_sendup_mbuf(so, n, KEY_SENDUP_REGISTERED);
6163 * free secreg entry registered.
6164 * XXX: I want to do free a socket marked done SADB_RESIGER to socket.
6175 panic("key_freereg: NULL pointer is passed.\n");
6178 * check whether existing or not.
6179 * check all type of SA, because there is a potential that
6180 * one socket is registered to multiple type of SA.
6182 for (i = 0; i <= SADB_SATYPE_MAX; i++) {
6183 LIST_FOREACH(reg, ®tree[i], chain) {
6185 && __LIST_CHAINED(reg)) {
6186 LIST_REMOVE(reg, chain);
6197 * SADB_EXPIRE processing
6199 * <base, SA, SA2, lifetime(C and one of HS), address(SD)>
6201 * NOTE: We send only soft lifetime extension.
6204 * others : error number
6208 struct secasvar *sav;
6212 struct mbuf *result = NULL, *m;
6215 struct sadb_lifetime *lt;
6217 /* XXX: Why do we lock ? */
6218 s = splnet(); /*called from softclock()*/
6222 panic("key_expire: NULL pointer is passed.\n");
6223 if (sav->sah == NULL)
6224 panic("key_expire: Why was SA index in SA NULL.\n");
6225 if ((satype = key_proto2satype(sav->sah->saidx.proto)) == 0)
6226 panic("key_expire: invalid proto is passed.\n");
6228 /* set msg header */
6229 m = key_setsadbmsg(SADB_EXPIRE, 0, satype, sav->seq, 0, sav->refcnt);
6236 /* create SA extension */
6237 m = key_setsadbsa(sav);
6244 /* create SA extension */
6245 m = key_setsadbxsa2(sav->sah->saidx.mode,
6246 sav->replay ? sav->replay->count : 0,
6247 sav->sah->saidx.reqid);
6254 /* create lifetime extension (current and soft) */
6255 len = PFKEY_ALIGN8(sizeof(*lt)) * 2;
6256 m = key_alloc_mbuf(len);
6257 if (!m || m->m_next) { /*XXX*/
6263 bzero(mtod(m, caddr_t), len);
6264 lt = mtod(m, struct sadb_lifetime *);
6265 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
6266 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
6267 lt->sadb_lifetime_allocations = sav->lft_c->sadb_lifetime_allocations;
6268 lt->sadb_lifetime_bytes = sav->lft_c->sadb_lifetime_bytes;
6269 lt->sadb_lifetime_addtime = sav->lft_c->sadb_lifetime_addtime;
6270 lt->sadb_lifetime_usetime = sav->lft_c->sadb_lifetime_usetime;
6271 lt = (struct sadb_lifetime *)(mtod(m, caddr_t) + len / 2);
6272 bcopy(sav->lft_s, lt, sizeof(*lt));
6275 /* set sadb_address for source */
6276 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
6277 &sav->sah->saidx.src.sa,
6278 FULLMASK, IPSEC_ULPROTO_ANY);
6285 /* set sadb_address for destination */
6286 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
6287 &sav->sah->saidx.dst.sa,
6288 FULLMASK, IPSEC_ULPROTO_ANY);
6295 if ((result->m_flags & M_PKTHDR) == 0) {
6300 if (result->m_len < sizeof(struct sadb_msg)) {
6301 result = m_pullup(result, sizeof(struct sadb_msg));
6302 if (result == NULL) {
6308 result->m_pkthdr.len = 0;
6309 for (m = result; m; m = m->m_next)
6310 result->m_pkthdr.len += m->m_len;
6312 mtod(result, struct sadb_msg *)->sadb_msg_len =
6313 PFKEY_UNIT64(result->m_pkthdr.len);
6316 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
6326 * SADB_FLUSH processing
6329 * from the ikmpd, and free all entries in secastree.
6333 * NOTE: to do is only marking SADB_SASTATE_DEAD.
6335 * m will always be freed.
6338 key_flush(so, m, mhp)
6341 const struct sadb_msghdr *mhp;
6343 struct sadb_msg *newmsg;
6344 struct secashead *sah, *nextsah;
6345 struct secasvar *sav, *nextsav;
6351 if (so == NULL || mhp == NULL || mhp->msg == NULL)
6352 panic("key_flush: NULL pointer is passed.\n");
6354 /* map satype to proto */
6355 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
6356 ipseclog((LOG_DEBUG, "key_flush: invalid satype is passed.\n"));
6357 return key_senderror(so, m, EINVAL);
6360 /* no SATYPE specified, i.e. flushing all SA. */
6361 for (sah = LIST_FIRST(&sahtree);
6364 nextsah = LIST_NEXT(sah, chain);
6366 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
6367 && proto != sah->saidx.proto)
6371 stateidx < _ARRAYLEN(saorder_state_alive);
6373 state = saorder_state_any[stateidx];
6374 for (sav = LIST_FIRST(&sah->savtree[state]);
6378 nextsav = LIST_NEXT(sav, chain);
6380 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
6385 sah->state = SADB_SASTATE_DEAD;
6388 if (m->m_len < sizeof(struct sadb_msg) ||
6389 sizeof(struct sadb_msg) > m->m_len + M_TRAILINGSPACE(m)) {
6390 ipseclog((LOG_DEBUG, "key_flush: No more memory.\n"));
6391 return key_senderror(so, m, ENOBUFS);
6397 m->m_pkthdr.len = m->m_len = sizeof(struct sadb_msg);
6398 newmsg = mtod(m, struct sadb_msg *);
6399 newmsg->sadb_msg_errno = 0;
6400 newmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
6402 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
6406 * SADB_DUMP processing
6407 * dump all entries including status of DEAD in SAD.
6410 * from the ikmpd, and dump all secasvar leaves
6415 * m will always be freed.
6418 key_dump(so, m, mhp)
6421 const struct sadb_msghdr *mhp;
6423 struct secashead *sah;
6424 struct secasvar *sav;
6430 struct sadb_msg *newmsg;
6434 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
6435 panic("key_dump: NULL pointer is passed.\n");
6437 /* map satype to proto */
6438 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
6439 ipseclog((LOG_DEBUG, "key_dump: invalid satype is passed.\n"));
6440 return key_senderror(so, m, EINVAL);
6443 /* count sav entries to be sent to the userland. */
6445 LIST_FOREACH(sah, &sahtree, chain) {
6446 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
6447 && proto != sah->saidx.proto)
6451 stateidx < _ARRAYLEN(saorder_state_any);
6453 state = saorder_state_any[stateidx];
6454 LIST_FOREACH(sav, &sah->savtree[state], chain) {
6461 return key_senderror(so, m, ENOENT);
6463 /* send this to the userland, one at a time. */
6465 LIST_FOREACH(sah, &sahtree, chain) {
6466 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
6467 && proto != sah->saidx.proto)
6470 /* map proto to satype */
6471 if ((satype = key_proto2satype(sah->saidx.proto)) == 0) {
6472 ipseclog((LOG_DEBUG, "key_dump: there was invalid proto in SAD.\n"));
6473 return key_senderror(so, m, EINVAL);
6477 stateidx < _ARRAYLEN(saorder_state_any);
6479 state = saorder_state_any[stateidx];
6480 LIST_FOREACH(sav, &sah->savtree[state], chain) {
6481 n = key_setdumpsa(sav, SADB_DUMP, satype,
6482 --cnt, mhp->msg->sadb_msg_pid);
6484 return key_senderror(so, m, ENOBUFS);
6486 key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
6496 * SADB_X_PROMISC processing
6498 * m will always be freed.
6501 key_promisc(so, m, mhp)
6504 const struct sadb_msghdr *mhp;
6509 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
6510 panic("key_promisc: NULL pointer is passed.\n");
6512 olen = PFKEY_UNUNIT64(mhp->msg->sadb_msg_len);
6514 if (olen < sizeof(struct sadb_msg)) {
6516 return key_senderror(so, m, EINVAL);
6521 } else if (olen == sizeof(struct sadb_msg)) {
6522 /* enable/disable promisc mode */
6525 if ((kp = (struct keycb *)sotorawcb(so)) == NULL)
6526 return key_senderror(so, m, EINVAL);
6527 mhp->msg->sadb_msg_errno = 0;
6528 switch (mhp->msg->sadb_msg_satype) {
6531 kp->kp_promisc = mhp->msg->sadb_msg_satype;
6534 return key_senderror(so, m, EINVAL);
6537 /* send the original message back to everyone */
6538 mhp->msg->sadb_msg_errno = 0;
6539 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
6541 /* send packet as is */
6543 m_adj(m, PFKEY_ALIGN8(sizeof(struct sadb_msg)));
6545 /* TODO: if sadb_msg_seq is specified, send to specific pid */
6546 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
6550 static int (*key_typesw[]) (struct socket *, struct mbuf *,
6551 const struct sadb_msghdr *) = {
6552 NULL, /* SADB_RESERVED */
6553 key_getspi, /* SADB_GETSPI */
6554 key_update, /* SADB_UPDATE */
6555 key_add, /* SADB_ADD */
6556 key_delete, /* SADB_DELETE */
6557 key_get, /* SADB_GET */
6558 key_acquire2, /* SADB_ACQUIRE */
6559 key_register, /* SADB_REGISTER */
6560 NULL, /* SADB_EXPIRE */
6561 key_flush, /* SADB_FLUSH */
6562 key_dump, /* SADB_DUMP */
6563 key_promisc, /* SADB_X_PROMISC */
6564 NULL, /* SADB_X_PCHANGE */
6565 key_spdadd, /* SADB_X_SPDUPDATE */
6566 key_spdadd, /* SADB_X_SPDADD */
6567 key_spddelete, /* SADB_X_SPDDELETE */
6568 key_spdget, /* SADB_X_SPDGET */
6569 NULL, /* SADB_X_SPDACQUIRE */
6570 key_spddump, /* SADB_X_SPDDUMP */
6571 key_spdflush, /* SADB_X_SPDFLUSH */
6572 key_spdadd, /* SADB_X_SPDSETIDX */
6573 NULL, /* SADB_X_SPDEXPIRE */
6574 key_spddelete2, /* SADB_X_SPDDELETE2 */
6578 * parse sadb_msg buffer to process PFKEYv2,
6579 * and create a data to response if needed.
6580 * I think to be dealed with mbuf directly.
6582 * msgp : pointer to pointer to a received buffer pulluped.
6583 * This is rewrited to response.
6584 * so : pointer to socket.
6586 * length for buffer to send to user process.
6593 struct sadb_msg *msg;
6594 struct sadb_msghdr mh;
6600 if (m == NULL || so == NULL)
6601 panic("key_parse: NULL pointer is passed.\n");
6603 #if 0 /*kdebug_sadb assumes msg in linear buffer*/
6604 KEYDEBUG(KEYDEBUG_KEY_DUMP,
6605 ipseclog((LOG_DEBUG, "key_parse: passed sadb_msg\n"));
6609 if (m->m_len < sizeof(struct sadb_msg)) {
6610 m = m_pullup(m, sizeof(struct sadb_msg));
6614 msg = mtod(m, struct sadb_msg *);
6615 orglen = PFKEY_UNUNIT64(msg->sadb_msg_len);
6616 target = KEY_SENDUP_ONE;
6618 if ((m->m_flags & M_PKTHDR) == 0 ||
6619 m->m_pkthdr.len != m->m_pkthdr.len) {
6620 ipseclog((LOG_DEBUG, "key_parse: invalid message length.\n"));
6621 pfkeystat.out_invlen++;
6626 if (msg->sadb_msg_version != PF_KEY_V2) {
6627 ipseclog((LOG_DEBUG,
6628 "key_parse: PF_KEY version %u is mismatched.\n",
6629 msg->sadb_msg_version));
6630 pfkeystat.out_invver++;
6635 if (msg->sadb_msg_type > SADB_MAX) {
6636 ipseclog((LOG_DEBUG, "key_parse: invalid type %u is passed.\n",
6637 msg->sadb_msg_type));
6638 pfkeystat.out_invmsgtype++;
6643 /* for old-fashioned code - should be nuked */
6644 if (m->m_pkthdr.len > MCLBYTES) {
6651 MGETHDR(n, M_DONTWAIT, MT_DATA);
6652 if (n && m->m_pkthdr.len > MHLEN) {
6653 MCLGET(n, M_DONTWAIT);
6654 if ((n->m_flags & M_EXT) == 0) {
6663 m_copydata(m, 0, m->m_pkthdr.len, mtod(n, caddr_t));
6664 n->m_pkthdr.len = n->m_len = m->m_pkthdr.len;
6670 /* align the mbuf chain so that extensions are in contiguous region. */
6671 error = key_align(m, &mh);
6675 if (m->m_next) { /*XXX*/
6683 switch (msg->sadb_msg_satype) {
6684 case SADB_SATYPE_UNSPEC:
6685 switch (msg->sadb_msg_type) {
6693 ipseclog((LOG_DEBUG, "key_parse: must specify satype "
6694 "when msg type=%u.\n", msg->sadb_msg_type));
6695 pfkeystat.out_invsatype++;
6700 case SADB_SATYPE_AH:
6701 case SADB_SATYPE_ESP:
6702 case SADB_X_SATYPE_IPCOMP:
6703 switch (msg->sadb_msg_type) {
6705 case SADB_X_SPDDELETE:
6707 case SADB_X_SPDDUMP:
6708 case SADB_X_SPDFLUSH:
6709 case SADB_X_SPDSETIDX:
6710 case SADB_X_SPDUPDATE:
6711 case SADB_X_SPDDELETE2:
6712 ipseclog((LOG_DEBUG, "key_parse: illegal satype=%u\n",
6713 msg->sadb_msg_type));
6714 pfkeystat.out_invsatype++;
6719 case SADB_SATYPE_RSVP:
6720 case SADB_SATYPE_OSPFV2:
6721 case SADB_SATYPE_RIPV2:
6722 case SADB_SATYPE_MIP:
6723 ipseclog((LOG_DEBUG, "key_parse: type %u isn't supported.\n",
6724 msg->sadb_msg_satype));
6725 pfkeystat.out_invsatype++;
6728 case 1: /* XXX: What does it do? */
6729 if (msg->sadb_msg_type == SADB_X_PROMISC)
6733 ipseclog((LOG_DEBUG, "key_parse: invalid type %u is passed.\n",
6734 msg->sadb_msg_satype));
6735 pfkeystat.out_invsatype++;
6740 /* check field of upper layer protocol and address family */
6741 if (mh.ext[SADB_EXT_ADDRESS_SRC] != NULL
6742 && mh.ext[SADB_EXT_ADDRESS_DST] != NULL) {
6743 struct sadb_address *src0, *dst0;
6746 src0 = (struct sadb_address *)(mh.ext[SADB_EXT_ADDRESS_SRC]);
6747 dst0 = (struct sadb_address *)(mh.ext[SADB_EXT_ADDRESS_DST]);
6749 /* check upper layer protocol */
6750 if (src0->sadb_address_proto != dst0->sadb_address_proto) {
6751 ipseclog((LOG_DEBUG, "key_parse: upper layer protocol mismatched.\n"));
6752 pfkeystat.out_invaddr++;
6758 if (PFKEY_ADDR_SADDR(src0)->sa_family !=
6759 PFKEY_ADDR_SADDR(dst0)->sa_family) {
6760 ipseclog((LOG_DEBUG, "key_parse: address family mismatched.\n"));
6761 pfkeystat.out_invaddr++;
6765 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
6766 PFKEY_ADDR_SADDR(dst0)->sa_len) {
6767 ipseclog((LOG_DEBUG,
6768 "key_parse: address struct size mismatched.\n"));
6769 pfkeystat.out_invaddr++;
6774 switch (PFKEY_ADDR_SADDR(src0)->sa_family) {
6776 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
6777 sizeof(struct sockaddr_in)) {
6778 pfkeystat.out_invaddr++;
6784 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
6785 sizeof(struct sockaddr_in6)) {
6786 pfkeystat.out_invaddr++;
6792 ipseclog((LOG_DEBUG,
6793 "key_parse: unsupported address family.\n"));
6794 pfkeystat.out_invaddr++;
6795 error = EAFNOSUPPORT;
6799 switch (PFKEY_ADDR_SADDR(src0)->sa_family) {
6801 plen = sizeof(struct in_addr) << 3;
6804 plen = sizeof(struct in6_addr) << 3;
6807 plen = 0; /*fool gcc*/
6811 /* check max prefix length */
6812 if (src0->sadb_address_prefixlen > plen ||
6813 dst0->sadb_address_prefixlen > plen) {
6814 ipseclog((LOG_DEBUG,
6815 "key_parse: illegal prefixlen.\n"));
6816 pfkeystat.out_invaddr++;
6822 * prefixlen == 0 is valid because there can be a case when
6823 * all addresses are matched.
6827 if (msg->sadb_msg_type >= sizeof(key_typesw)/sizeof(key_typesw[0]) ||
6828 key_typesw[msg->sadb_msg_type] == NULL) {
6829 pfkeystat.out_invmsgtype++;
6834 return (*key_typesw[msg->sadb_msg_type])(so, m, &mh);
6837 msg->sadb_msg_errno = error;
6838 return key_sendup_mbuf(so, m, target);
6842 key_senderror(so, m, code)
6847 struct sadb_msg *msg;
6849 if (m->m_len < sizeof(struct sadb_msg))
6850 panic("invalid mbuf passed to key_senderror");
6852 msg = mtod(m, struct sadb_msg *);
6853 msg->sadb_msg_errno = code;
6854 return key_sendup_mbuf(so, m, KEY_SENDUP_ONE);
6858 * set the pointer to each header into message buffer.
6859 * m will be freed on error.
6860 * XXX larger-than-MCLBYTES extension?
6865 struct sadb_msghdr *mhp;
6868 struct sadb_ext *ext;
6874 if (m == NULL || mhp == NULL)
6875 panic("key_align: NULL pointer is passed.\n");
6876 if (m->m_len < sizeof(struct sadb_msg))
6877 panic("invalid mbuf passed to key_align");
6880 bzero(mhp, sizeof(*mhp));
6882 mhp->msg = mtod(m, struct sadb_msg *);
6883 mhp->ext[0] = (struct sadb_ext *)mhp->msg; /*XXX backward compat */
6885 end = PFKEY_UNUNIT64(mhp->msg->sadb_msg_len);
6886 extlen = end; /*just in case extlen is not updated*/
6887 for (off = sizeof(struct sadb_msg); off < end; off += extlen) {
6888 n = m_pulldown(m, off, sizeof(struct sadb_ext), &toff);
6890 /* m is already freed */
6893 ext = (struct sadb_ext *)(mtod(n, caddr_t) + toff);
6896 switch (ext->sadb_ext_type) {
6898 case SADB_EXT_ADDRESS_SRC:
6899 case SADB_EXT_ADDRESS_DST:
6900 case SADB_EXT_ADDRESS_PROXY:
6901 case SADB_EXT_LIFETIME_CURRENT:
6902 case SADB_EXT_LIFETIME_HARD:
6903 case SADB_EXT_LIFETIME_SOFT:
6904 case SADB_EXT_KEY_AUTH:
6905 case SADB_EXT_KEY_ENCRYPT:
6906 case SADB_EXT_IDENTITY_SRC:
6907 case SADB_EXT_IDENTITY_DST:
6908 case SADB_EXT_SENSITIVITY:
6909 case SADB_EXT_PROPOSAL:
6910 case SADB_EXT_SUPPORTED_AUTH:
6911 case SADB_EXT_SUPPORTED_ENCRYPT:
6912 case SADB_EXT_SPIRANGE:
6913 case SADB_X_EXT_POLICY:
6914 case SADB_X_EXT_SA2:
6915 /* duplicate check */
6917 * XXX Are there duplication payloads of either
6918 * KEY_AUTH or KEY_ENCRYPT ?
6920 if (mhp->ext[ext->sadb_ext_type] != NULL) {
6921 ipseclog((LOG_DEBUG,
6922 "key_align: duplicate ext_type %u "
6923 "is passed.\n", ext->sadb_ext_type));
6925 pfkeystat.out_dupext++;
6930 ipseclog((LOG_DEBUG,
6931 "key_align: invalid ext_type %u is passed.\n",
6932 ext->sadb_ext_type));
6934 pfkeystat.out_invexttype++;
6938 extlen = PFKEY_UNUNIT64(ext->sadb_ext_len);
6940 if (key_validate_ext(ext, extlen)) {
6942 pfkeystat.out_invlen++;
6946 n = m_pulldown(m, off, extlen, &toff);
6948 /* m is already freed */
6951 ext = (struct sadb_ext *)(mtod(n, caddr_t) + toff);
6953 mhp->ext[ext->sadb_ext_type] = ext;
6954 mhp->extoff[ext->sadb_ext_type] = off;
6955 mhp->extlen[ext->sadb_ext_type] = extlen;
6960 pfkeystat.out_invlen++;
6968 key_validate_ext(ext, len)
6969 const struct sadb_ext *ext;
6972 const struct sockaddr *sa;
6973 enum { NONE, ADDR } checktype = NONE;
6975 const int sal = offsetof(struct sockaddr, sa_len) + sizeof(sa->sa_len);
6977 if (len != PFKEY_UNUNIT64(ext->sadb_ext_len))
6980 /* if it does not match minimum/maximum length, bail */
6981 if (ext->sadb_ext_type >= sizeof(minsize) / sizeof(minsize[0]) ||
6982 ext->sadb_ext_type >= sizeof(maxsize) / sizeof(maxsize[0]))
6984 if (!minsize[ext->sadb_ext_type] || len < minsize[ext->sadb_ext_type])
6986 if (maxsize[ext->sadb_ext_type] && len > maxsize[ext->sadb_ext_type])
6989 /* more checks based on sadb_ext_type XXX need more */
6990 switch (ext->sadb_ext_type) {
6991 case SADB_EXT_ADDRESS_SRC:
6992 case SADB_EXT_ADDRESS_DST:
6993 case SADB_EXT_ADDRESS_PROXY:
6994 baselen = PFKEY_ALIGN8(sizeof(struct sadb_address));
6997 case SADB_EXT_IDENTITY_SRC:
6998 case SADB_EXT_IDENTITY_DST:
6999 if (((const struct sadb_ident *)ext)->sadb_ident_type ==
7000 SADB_X_IDENTTYPE_ADDR) {
7001 baselen = PFKEY_ALIGN8(sizeof(struct sadb_ident));
7011 switch (checktype) {
7015 sa = (const struct sockaddr *)(((const u_int8_t*)ext)+baselen);
7016 if (len < baselen + sal)
7018 if (baselen + PFKEY_ALIGN8(sa->sa_len) != len)
7031 for (i = 0; i < IPSEC_DIR_MAX; i++) {
7032 LIST_INIT(&sptree[i]);
7035 LIST_INIT(&sahtree);
7037 for (i = 0; i <= SADB_SATYPE_MAX; i++) {
7038 LIST_INIT(®tree[i]);
7041 #ifndef IPSEC_NONBLOCK_ACQUIRE
7042 LIST_INIT(&acqtree);
7044 LIST_INIT(&spacqtree);
7046 /* system default */
7047 ip4_def_policy.policy = IPSEC_POLICY_NONE;
7048 ip4_def_policy.refcnt++; /*never reclaim this*/
7050 #ifndef IPSEC_DEBUG2
7051 timeout((void *)key_timehandler, (void *)0, hz);
7052 #endif /*IPSEC_DEBUG2*/
7054 /* initialize key statistics */
7055 keystat.getspi_count = 1;
7057 printf("IPsec: Initialized Security Association Processing.\n");
7063 * XXX: maybe This function is called after INBOUND IPsec processing.
7065 * Special check for tunnel-mode packets.
7066 * We must make some checks for consistency between inner and outer IP header.
7068 * xxx more checks to be provided
7071 key_checktunnelsanity(sav, family, src, dst)
7072 struct secasvar *sav;
7078 if (sav->sah == NULL)
7079 panic("sav->sah == NULL at key_checktunnelsanity");
7081 /* XXX: check inner IP header */
7087 #define hostnamelen strlen(hostname)
7090 * Get FQDN for the host.
7091 * If the administrator configured hostname (by hostname(1)) without
7092 * domain name, returns nothing.
7099 static char fqdn[MAXHOSTNAMELEN + 1];
7104 /* check if it comes with domain name. */
7106 for (i = 0; i < hostnamelen; i++) {
7107 if (hostname[i] == '.')
7113 /* NOTE: hostname may not be NUL-terminated. */
7114 bzero(fqdn, sizeof(fqdn));
7115 bcopy(hostname, fqdn, hostnamelen);
7116 fqdn[hostnamelen] = '\0';
7121 * get username@FQDN for the host/user.
7127 static char userfqdn[MAXHOSTNAMELEN + MAXLOGNAME + 2];
7128 struct proc *p = curproc;
7131 if (!p || !p->p_pgrp || !p->p_pgrp->pg_session)
7133 if (!(host = key_getfqdn()))
7136 /* NOTE: s_login may not be-NUL terminated. */
7137 bzero(userfqdn, sizeof(userfqdn));
7138 bcopy(p->p_pgrp->pg_session->s_login, userfqdn, MAXLOGNAME);
7139 userfqdn[MAXLOGNAME] = '\0'; /* safeguard */
7140 q = userfqdn + strlen(userfqdn);
7142 bcopy(host, q, strlen(host));
7150 /* record data transfer on SA, and update timestamps */
7152 key_sa_recordxfer(sav, m)
7153 struct secasvar *sav;
7156 KASSERT(sav != NULL, ("key_sa_recordxfer: Null secasvar"));
7157 KASSERT(m != NULL, ("key_sa_recordxfer: Null mbuf"));
7162 * XXX Currently, there is a difference of bytes size
7163 * between inbound and outbound processing.
7165 sav->lft_c->sadb_lifetime_bytes += m->m_pkthdr.len;
7166 /* to check bytes lifetime is done in key_timehandler(). */
7169 * We use the number of packets as the unit of
7170 * sadb_lifetime_allocations. We increment the variable
7171 * whenever {esp,ah}_{in,out}put is called.
7173 sav->lft_c->sadb_lifetime_allocations++;
7174 /* XXX check for expires? */
7177 * NOTE: We record CURRENT sadb_lifetime_usetime by using wall clock,
7178 * in seconds. HARD and SOFT lifetime are measured by the time
7179 * difference (again in seconds) from sadb_lifetime_usetime.
7183 * -----+-----+--------+---> t
7184 * <--------------> HARD
7187 sav->lft_c->sadb_lifetime_usetime = time_second;
7188 /* XXX check for expires? */
7195 key_sa_routechange(dst)
7196 struct sockaddr *dst;
7198 struct secashead *sah;
7201 LIST_FOREACH(sah, &sahtree, chain) {
7202 ro = &sah->sa_route;
7203 if (ro->ro_rt && dst->sa_len == ro->ro_dst.sa_len
7204 && bcmp(dst, &ro->ro_dst, dst->sa_len) == 0) {
7206 ro->ro_rt = (struct rtentry *)NULL;
7214 key_sa_chgstate(sav, state)
7215 struct secasvar *sav;
7219 panic("key_sa_chgstate called with sav == NULL");
7221 if (sav->state == state)
7224 if (__LIST_CHAINED(sav))
7225 LIST_REMOVE(sav, chain);
7228 LIST_INSERT_HEAD(&sav->sah->savtree[state], sav, chain);
7233 struct secasvar *sav;
7237 panic("key_sa_stir_iv called with sav == NULL");
7238 key_randomfill(sav->iv, sav->ivlen);
7242 static struct mbuf *
7246 struct mbuf *m = NULL, *n;
7251 MGET(n, M_DONTWAIT, MT_DATA);
7252 if (n && len > MLEN)
7253 MCLGET(n, M_DONTWAIT);
7261 n->m_len = M_TRAILINGSPACE(n);
7262 /* use the bottom of mbuf, hoping we can prepend afterwards */
7263 if (n->m_len > len) {
7264 t = (n->m_len - len) & ~(sizeof(long) - 1);