1 /* $FreeBSD: src/sys/netkey/key.c,v 1.16.2.13 2002/07/24 18:17:40 ume Exp $ */
2 /* $DragonFly: src/sys/netproto/key/key.c,v 1.9 2004/08/02 13:22:33 joerg 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"
86 #include <netinet6/ipsec.h>
88 #include <netinet6/ipsec6.h>
90 #include <netinet6/ah.h>
92 #include <netinet6/ah6.h>
95 #include <netinet6/esp.h>
97 #include <netinet6/esp6.h>
100 #include <netinet6/ipcomp.h>
102 #include <netinet6/ipcomp6.h>
105 #include <machine/stdarg.h>
108 #include <sys/random.h>
110 #include <net/net_osdep.h>
113 #define satosin(s) ((struct sockaddr_in *)s)
116 #define FULLMASK 0xff
119 * Note on SA reference counting:
120 * - SAs that are not in DEAD state will have (total external reference + 1)
121 * following value in reference count field. they cannot be freed and are
122 * referenced from SA header.
123 * - SAs that are in DEAD state will have (total external reference)
124 * in reference count field. they are ready to be freed. reference from
125 * SA header will be removed in key_delsav(), when the reference count
126 * field hits 0 (= no external reference other than from SA header.
129 u_int32_t key_debug_level = 0;
130 static u_int key_spi_trycnt = 1000;
131 static u_int32_t key_spi_minval = 0x100;
132 static u_int32_t key_spi_maxval = 0x0fffffff; /* XXX */
133 static u_int32_t policy_id = 0;
134 static u_int key_int_random = 60; /*interval to initialize randseed,1(m)*/
135 static u_int key_larval_lifetime = 30; /* interval to expire acquiring, 30(s)*/
136 static int key_blockacq_count = 10; /* counter for blocking SADB_ACQUIRE.*/
137 static int key_blockacq_lifetime = 20; /* lifetime for blocking SADB_ACQUIRE.*/
138 static int key_preferred_oldsa = 1; /* preferred old sa rather than new sa.*/
140 static u_int32_t acq_seq = 0;
141 static int key_tick_init_random = 0;
143 static LIST_HEAD(_sptree, secpolicy) sptree[IPSEC_DIR_MAX]; /* SPD */
144 static LIST_HEAD(_sahtree, secashead) sahtree; /* SAD */
145 static LIST_HEAD(_regtree, secreg) regtree[SADB_SATYPE_MAX + 1];
147 #ifndef IPSEC_NONBLOCK_ACQUIRE
148 static LIST_HEAD(_acqtree, secacq) acqtree; /* acquiring list */
150 static LIST_HEAD(_spacqtree, secspacq) spacqtree; /* SP acquiring list */
152 struct key_cb key_cb;
154 /* search order for SAs */
155 static const u_int saorder_state_valid_prefer_old[] = {
156 SADB_SASTATE_DYING, SADB_SASTATE_MATURE,
158 static const u_int saorder_state_valid_prefer_new[] = {
159 SADB_SASTATE_MATURE, SADB_SASTATE_DYING,
161 static const u_int saorder_state_alive[] = {
163 SADB_SASTATE_MATURE, SADB_SASTATE_DYING, SADB_SASTATE_LARVAL
165 static const u_int saorder_state_any[] = {
166 SADB_SASTATE_MATURE, SADB_SASTATE_DYING,
167 SADB_SASTATE_LARVAL, SADB_SASTATE_DEAD
170 static const int minsize[] = {
171 sizeof(struct sadb_msg), /* SADB_EXT_RESERVED */
172 sizeof(struct sadb_sa), /* SADB_EXT_SA */
173 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_CURRENT */
174 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_HARD */
175 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_SOFT */
176 sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_SRC */
177 sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_DST */
178 sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_PROXY */
179 sizeof(struct sadb_key), /* SADB_EXT_KEY_AUTH */
180 sizeof(struct sadb_key), /* SADB_EXT_KEY_ENCRYPT */
181 sizeof(struct sadb_ident), /* SADB_EXT_IDENTITY_SRC */
182 sizeof(struct sadb_ident), /* SADB_EXT_IDENTITY_DST */
183 sizeof(struct sadb_sens), /* SADB_EXT_SENSITIVITY */
184 sizeof(struct sadb_prop), /* SADB_EXT_PROPOSAL */
185 sizeof(struct sadb_supported), /* SADB_EXT_SUPPORTED_AUTH */
186 sizeof(struct sadb_supported), /* SADB_EXT_SUPPORTED_ENCRYPT */
187 sizeof(struct sadb_spirange), /* SADB_EXT_SPIRANGE */
188 0, /* SADB_X_EXT_KMPRIVATE */
189 sizeof(struct sadb_x_policy), /* SADB_X_EXT_POLICY */
190 sizeof(struct sadb_x_sa2), /* SADB_X_SA2 */
192 static const int maxsize[] = {
193 sizeof(struct sadb_msg), /* SADB_EXT_RESERVED */
194 sizeof(struct sadb_sa), /* SADB_EXT_SA */
195 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_CURRENT */
196 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_HARD */
197 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_SOFT */
198 0, /* SADB_EXT_ADDRESS_SRC */
199 0, /* SADB_EXT_ADDRESS_DST */
200 0, /* SADB_EXT_ADDRESS_PROXY */
201 0, /* SADB_EXT_KEY_AUTH */
202 0, /* SADB_EXT_KEY_ENCRYPT */
203 0, /* SADB_EXT_IDENTITY_SRC */
204 0, /* SADB_EXT_IDENTITY_DST */
205 0, /* SADB_EXT_SENSITIVITY */
206 0, /* SADB_EXT_PROPOSAL */
207 0, /* SADB_EXT_SUPPORTED_AUTH */
208 0, /* SADB_EXT_SUPPORTED_ENCRYPT */
209 sizeof(struct sadb_spirange), /* SADB_EXT_SPIRANGE */
210 0, /* SADB_X_EXT_KMPRIVATE */
211 0, /* SADB_X_EXT_POLICY */
212 sizeof(struct sadb_x_sa2), /* SADB_X_SA2 */
215 static int ipsec_esp_keymin = 256;
216 static int ipsec_esp_auth = 0;
217 static int ipsec_ah_keymin = 128;
220 SYSCTL_DECL(_net_key);
223 SYSCTL_INT(_net_key, KEYCTL_DEBUG_LEVEL, debug, CTLFLAG_RW, \
224 &key_debug_level, 0, "");
226 /* max count of trial for the decision of spi value */
227 SYSCTL_INT(_net_key, KEYCTL_SPI_TRY, spi_trycnt, CTLFLAG_RW, \
228 &key_spi_trycnt, 0, "");
230 /* minimum spi value to allocate automatically. */
231 SYSCTL_INT(_net_key, KEYCTL_SPI_MIN_VALUE, spi_minval, CTLFLAG_RW, \
232 &key_spi_minval, 0, "");
234 /* maximun spi value to allocate automatically. */
235 SYSCTL_INT(_net_key, KEYCTL_SPI_MAX_VALUE, spi_maxval, CTLFLAG_RW, \
236 &key_spi_maxval, 0, "");
238 /* interval to initialize randseed */
239 SYSCTL_INT(_net_key, KEYCTL_RANDOM_INT, int_random, CTLFLAG_RW, \
240 &key_int_random, 0, "");
242 /* lifetime for larval SA */
243 SYSCTL_INT(_net_key, KEYCTL_LARVAL_LIFETIME, larval_lifetime, CTLFLAG_RW, \
244 &key_larval_lifetime, 0, "");
246 /* counter for blocking to send SADB_ACQUIRE to IKEd */
247 SYSCTL_INT(_net_key, KEYCTL_BLOCKACQ_COUNT, blockacq_count, CTLFLAG_RW, \
248 &key_blockacq_count, 0, "");
250 /* lifetime for blocking to send SADB_ACQUIRE to IKEd */
251 SYSCTL_INT(_net_key, KEYCTL_BLOCKACQ_LIFETIME, blockacq_lifetime, CTLFLAG_RW, \
252 &key_blockacq_lifetime, 0, "");
255 SYSCTL_INT(_net_key, KEYCTL_ESP_AUTH, esp_auth, CTLFLAG_RW, \
256 &ipsec_esp_auth, 0, "");
258 /* minimum ESP key length */
259 SYSCTL_INT(_net_key, KEYCTL_ESP_KEYMIN, esp_keymin, CTLFLAG_RW, \
260 &ipsec_esp_keymin, 0, "");
262 /* minimum AH key length */
263 SYSCTL_INT(_net_key, KEYCTL_AH_KEYMIN, ah_keymin, CTLFLAG_RW, \
264 &ipsec_ah_keymin, 0, "");
266 /* perfered old SA rather than new SA */
267 SYSCTL_INT(_net_key, KEYCTL_PREFERED_OLDSA, prefered_oldsa, CTLFLAG_RW,\
268 &key_preferred_oldsa, 0, "");
271 #define LIST_FOREACH(elm, head, field) \
272 for (elm = LIST_FIRST(head); elm; elm = LIST_NEXT(elm, field))
274 #define __LIST_CHAINED(elm) \
275 (!((elm)->chain.le_next == NULL && (elm)->chain.le_prev == NULL))
276 #define LIST_INSERT_TAIL(head, elm, type, field) \
278 struct type *curelm = LIST_FIRST(head); \
279 if (curelm == NULL) {\
280 LIST_INSERT_HEAD(head, elm, field); \
282 while (LIST_NEXT(curelm, field)) \
283 curelm = LIST_NEXT(curelm, field);\
284 LIST_INSERT_AFTER(curelm, elm, field);\
288 #define KEY_CHKSASTATE(head, sav, name) \
290 if ((head) != (sav)) { \
291 ipseclog((LOG_DEBUG, "%s: state mismatched (TREE=%d SA=%d)\n", \
292 (name), (head), (sav))); \
297 #define KEY_CHKSPDIR(head, sp, name) \
299 if ((head) != (sp)) { \
300 ipseclog((LOG_DEBUG, "%s: direction mismatched (TREE=%d SP=%d), " \
301 "anyway continue.\n", \
302 (name), (head), (sp))); \
307 #define KMALLOC(p, t, n) \
308 ((p) = (t) malloc((unsigned long)(n), M_SECA, M_INTWAIT | M_NULLOK))
310 free((caddr_t)(p), M_SECA);
312 #define KMALLOC(p, t, n) \
314 ((p) = (t)malloc((unsigned long)(n), M_SECA, M_INTWAIT | M_NULLOK)); \
315 printf("%s %d: %p <- KMALLOC(%s, %d)\n", \
316 __FILE__, __LINE__, (p), #t, n); \
321 printf("%s %d: %p -> KFREE()\n", __FILE__, __LINE__, (p)); \
322 free((caddr_t)(p), M_SECA); \
327 * set parameters into secpolicyindex buffer.
328 * Must allocate secpolicyindex buffer passed to this function.
330 #define KEY_SETSECSPIDX(_dir, s, d, ps, pd, ulp, idx) \
332 bzero((idx), sizeof(struct secpolicyindex)); \
333 (idx)->dir = (_dir); \
334 (idx)->prefs = (ps); \
335 (idx)->prefd = (pd); \
336 (idx)->ul_proto = (ulp); \
337 bcopy((s), &(idx)->src, ((struct sockaddr *)(s))->sa_len); \
338 bcopy((d), &(idx)->dst, ((struct sockaddr *)(d))->sa_len); \
342 * set parameters into secasindex buffer.
343 * Must allocate secasindex buffer before calling this function.
345 #define KEY_SETSECASIDX(p, m, r, s, d, idx) \
347 bzero((idx), sizeof(struct secasindex)); \
348 (idx)->proto = (p); \
350 (idx)->reqid = (r); \
351 bcopy((s), &(idx)->src, ((struct sockaddr *)(s))->sa_len); \
352 bcopy((d), &(idx)->dst, ((struct sockaddr *)(d))->sa_len); \
357 u_long getspi_count; /* the avarage of count to try to get new SPI */
361 struct sadb_msg *msg;
362 struct sadb_ext *ext[SADB_EXT_MAX + 1];
363 int extoff[SADB_EXT_MAX + 1];
364 int extlen[SADB_EXT_MAX + 1];
367 static struct secasvar *key_allocsa_policy (struct secasindex *);
368 static void key_freesp_so (struct secpolicy **);
369 static struct secasvar *key_do_allocsa_policy (struct secashead *, u_int);
370 static void key_delsp (struct secpolicy *);
371 static struct secpolicy *key_getsp (struct secpolicyindex *);
372 static struct secpolicy *key_getspbyid (u_int32_t);
373 static u_int32_t key_newreqid (void);
374 static struct mbuf *key_gather_mbuf (struct mbuf *,
375 const struct sadb_msghdr *, int, int, ...);
376 static int key_spdadd (struct socket *, struct mbuf *,
377 const struct sadb_msghdr *);
378 static u_int32_t key_getnewspid (void);
379 static int key_spddelete (struct socket *, struct mbuf *,
380 const struct sadb_msghdr *);
381 static int key_spddelete2 (struct socket *, struct mbuf *,
382 const struct sadb_msghdr *);
383 static int key_spdget (struct socket *, struct mbuf *,
384 const struct sadb_msghdr *);
385 static int key_spdflush (struct socket *, struct mbuf *,
386 const struct sadb_msghdr *);
387 static int key_spddump (struct socket *, struct mbuf *,
388 const struct sadb_msghdr *);
389 static struct mbuf *key_setdumpsp (struct secpolicy *,
390 u_int8_t, u_int32_t, u_int32_t);
391 static u_int key_getspreqmsglen (struct secpolicy *);
392 static int key_spdexpire (struct secpolicy *);
393 static struct secashead *key_newsah (struct secasindex *);
394 static void key_delsah (struct secashead *);
395 static struct secasvar *key_newsav (struct mbuf *,
396 const struct sadb_msghdr *, struct secashead *, int *);
397 static void key_delsav (struct secasvar *);
398 static struct secashead *key_getsah (struct secasindex *);
399 static struct secasvar *key_checkspidup (struct secasindex *, u_int32_t);
400 static struct secasvar *key_getsavbyspi (struct secashead *, u_int32_t);
401 static int key_setsaval (struct secasvar *, struct mbuf *,
402 const struct sadb_msghdr *);
403 static int key_mature (struct secasvar *);
404 static struct mbuf *key_setdumpsa (struct secasvar *, u_int8_t,
405 u_int8_t, u_int32_t, u_int32_t);
406 static struct mbuf *key_setsadbmsg (u_int8_t, u_int16_t, u_int8_t,
407 u_int32_t, pid_t, u_int16_t);
408 static struct mbuf *key_setsadbsa (struct secasvar *);
409 static struct mbuf *key_setsadbaddr (u_int16_t,
410 struct sockaddr *, u_int8_t, u_int16_t);
412 static struct mbuf *key_setsadbident (u_int16_t, u_int16_t, caddr_t,
415 static struct mbuf *key_setsadbxsa2 (u_int8_t, u_int32_t, u_int32_t);
416 static struct mbuf *key_setsadbxpolicy (u_int16_t, u_int8_t,
418 static void *key_newbuf (const void *, u_int);
420 static int key_ismyaddr6 (struct sockaddr_in6 *);
423 /* flags for key_cmpsaidx() */
424 #define CMP_HEAD 1 /* protocol, addresses. */
425 #define CMP_MODE_REQID 2 /* additionally HEAD, reqid, mode. */
426 #define CMP_REQID 3 /* additionally HEAD, reaid. */
427 #define CMP_EXACTLY 4 /* all elements. */
428 static int key_cmpsaidx
429 (struct secasindex *, struct secasindex *, int);
431 static int key_cmpspidx_exactly
432 (struct secpolicyindex *, struct secpolicyindex *);
433 static int key_cmpspidx_withmask
434 (struct secpolicyindex *, struct secpolicyindex *);
435 static int key_sockaddrcmp (struct sockaddr *, struct sockaddr *, int);
436 static int key_bbcmp (caddr_t, caddr_t, u_int);
437 static void key_srandom (void);
438 static u_int16_t key_satype2proto (u_int8_t);
439 static u_int8_t key_proto2satype (u_int16_t);
441 static int key_getspi (struct socket *, struct mbuf *,
442 const struct sadb_msghdr *);
443 static u_int32_t key_do_getnewspi (struct sadb_spirange *,
444 struct secasindex *);
445 static int key_update (struct socket *, struct mbuf *,
446 const struct sadb_msghdr *);
447 #ifdef IPSEC_DOSEQCHECK
448 static struct secasvar *key_getsavbyseq (struct secashead *, u_int32_t);
450 static int key_add (struct socket *, struct mbuf *,
451 const struct sadb_msghdr *);
452 static int key_setident (struct secashead *, struct mbuf *,
453 const struct sadb_msghdr *);
454 static struct mbuf *key_getmsgbuf_x1 (struct mbuf *,
455 const struct sadb_msghdr *);
456 static int key_delete (struct socket *, struct mbuf *,
457 const struct sadb_msghdr *);
458 static int key_get (struct socket *, struct mbuf *,
459 const struct sadb_msghdr *);
461 static void key_getcomb_setlifetime (struct sadb_comb *);
463 static struct mbuf *key_getcomb_esp (void);
465 static struct mbuf *key_getcomb_ah (void);
466 static struct mbuf *key_getcomb_ipcomp (void);
467 static struct mbuf *key_getprop (const struct secasindex *);
469 static int key_acquire (struct secasindex *, struct secpolicy *);
470 #ifndef IPSEC_NONBLOCK_ACQUIRE
471 static struct secacq *key_newacq (struct secasindex *);
472 static struct secacq *key_getacq (struct secasindex *);
473 static struct secacq *key_getacqbyseq (u_int32_t);
475 static struct secspacq *key_newspacq (struct secpolicyindex *);
476 static struct secspacq *key_getspacq (struct secpolicyindex *);
477 static int key_acquire2 (struct socket *, struct mbuf *,
478 const struct sadb_msghdr *);
479 static int key_register (struct socket *, struct mbuf *,
480 const struct sadb_msghdr *);
481 static int key_expire (struct secasvar *);
482 static int key_flush (struct socket *, struct mbuf *,
483 const struct sadb_msghdr *);
484 static int key_dump (struct socket *, struct mbuf *,
485 const struct sadb_msghdr *);
486 static int key_promisc (struct socket *, struct mbuf *,
487 const struct sadb_msghdr *);
488 static int key_senderror (struct socket *, struct mbuf *, int);
489 static int key_validate_ext (const struct sadb_ext *, int);
490 static int key_align (struct mbuf *, struct sadb_msghdr *);
492 static const char *key_getfqdn (void);
493 static const char *key_getuserfqdn (void);
495 static void key_sa_chgstate (struct secasvar *, u_int8_t);
496 static struct mbuf *key_alloc_mbuf (int);
498 /* %%% IPsec policy management */
500 * allocating a SP for OUTBOUND or INBOUND packet.
501 * Must call key_freesp() later.
502 * OUT: NULL: not found
503 * others: found and return the pointer.
506 key_allocsp(spidx, dir)
507 struct secpolicyindex *spidx;
510 struct secpolicy *sp;
516 panic("key_allocsp: NULL pointer is passed.\n");
518 /* check direction */
520 case IPSEC_DIR_INBOUND:
521 case IPSEC_DIR_OUTBOUND:
524 panic("key_allocsp: Invalid direction is passed.\n");
528 s = splnet(); /*called from softclock()*/
529 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
530 printf("*** objects\n");
531 kdebug_secpolicyindex(spidx));
533 LIST_FOREACH(sp, &sptree[dir], chain) {
534 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
535 printf("*** in SPD\n");
536 kdebug_secpolicyindex(&sp->spidx));
538 if (sp->state == IPSEC_SPSTATE_DEAD)
540 if (key_cmpspidx_withmask(&sp->spidx, spidx))
549 KEY_CHKSPDIR(sp->spidx.dir, dir, "key_allocsp");
551 /* found a SPD entry */
553 sp->lastused = tv.tv_sec;
556 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
557 printf("DP key_allocsp cause refcnt++:%d SP:%p\n",
564 * return a policy that matches this particular inbound packet.
568 key_gettunnel(osrc, odst, isrc, idst)
569 struct sockaddr *osrc, *odst, *isrc, *idst;
571 struct secpolicy *sp;
572 const int dir = IPSEC_DIR_INBOUND;
575 struct ipsecrequest *r1, *r2, *p;
576 struct sockaddr *os, *od, *is, *id;
577 struct secpolicyindex spidx;
579 if (isrc->sa_family != idst->sa_family) {
580 ipseclog((LOG_ERR, "protocol family mismatched %d != %d\n.",
581 isrc->sa_family, idst->sa_family));
585 s = splnet(); /*called from softclock()*/
586 LIST_FOREACH(sp, &sptree[dir], chain) {
587 if (sp->state == IPSEC_SPSTATE_DEAD)
591 for (p = sp->req; p; p = p->next) {
592 if (p->saidx.mode != IPSEC_MODE_TUNNEL)
599 /* here we look at address matches only */
601 if (isrc->sa_len > sizeof(spidx.src) ||
602 idst->sa_len > sizeof(spidx.dst))
604 bcopy(isrc, &spidx.src, isrc->sa_len);
605 bcopy(idst, &spidx.dst, idst->sa_len);
606 if (!key_cmpspidx_withmask(&sp->spidx, &spidx))
609 is = (struct sockaddr *)&r1->saidx.src;
610 id = (struct sockaddr *)&r1->saidx.dst;
611 if (key_sockaddrcmp(is, isrc, 0) ||
612 key_sockaddrcmp(id, idst, 0))
616 os = (struct sockaddr *)&r2->saidx.src;
617 od = (struct sockaddr *)&r2->saidx.dst;
618 if (key_sockaddrcmp(os, osrc, 0) ||
619 key_sockaddrcmp(od, odst, 0))
630 sp->lastused = tv.tv_sec;
637 * allocating an SA entry for an *OUTBOUND* packet.
638 * checking each request entries in SP, and acquire an SA if need.
639 * OUT: 0: there are valid requests.
640 * ENOENT: policy may be valid, but SA with REQUIRE is on acquiring.
643 key_checkrequest(isr, saidx)
644 struct ipsecrequest *isr;
645 struct secasindex *saidx;
651 if (isr == NULL || saidx == NULL)
652 panic("key_checkrequest: NULL pointer is passed.\n");
655 switch (saidx->mode) {
656 case IPSEC_MODE_TRANSPORT:
657 case IPSEC_MODE_TUNNEL:
661 panic("key_checkrequest: Invalid policy defined.\n");
664 /* get current level */
665 level = ipsec_get_reqlevel(isr);
669 * We do allocate new SA only if the state of SA in the holder is
670 * SADB_SASTATE_DEAD. The SA for outbound must be the oldest.
672 if (isr->sav != NULL) {
673 if (isr->sav->sah == NULL)
674 panic("key_checkrequest: sah is null.\n");
675 if (isr->sav == (struct secasvar *)LIST_FIRST(
676 &isr->sav->sah->savtree[SADB_SASTATE_DEAD])) {
677 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
678 printf("DP checkrequest calls free SA:%p\n",
680 key_freesav(isr->sav);
686 * we free any SA stashed in the IPsec request because a different
687 * SA may be involved each time this request is checked, either
688 * because new SAs are being configured, or this request is
689 * associated with an unconnected datagram socket, or this request
690 * is associated with a system default policy.
692 * The operation may have negative impact to performance. We may
693 * want to check cached SA carefully, rather than picking new SA
696 if (isr->sav != NULL) {
697 key_freesav(isr->sav);
703 * new SA allocation if no SA found.
704 * key_allocsa_policy should allocate the oldest SA available.
705 * See key_do_allocsa_policy(), and draft-jenkins-ipsec-rekeying-03.txt.
707 if (isr->sav == NULL)
708 isr->sav = key_allocsa_policy(saidx);
710 /* When there is SA. */
711 if (isr->sav != NULL)
715 if ((error = key_acquire(saidx, isr->sp)) != 0) {
716 /* XXX What should I do ? */
717 ipseclog((LOG_DEBUG, "key_checkrequest: error %d returned "
718 "from key_acquire.\n", error));
722 return level == IPSEC_LEVEL_REQUIRE ? ENOENT : 0;
726 * allocating a SA for policy entry from SAD.
727 * NOTE: searching SAD of aliving state.
728 * OUT: NULL: not found.
729 * others: found and return the pointer.
731 static struct secasvar *
732 key_allocsa_policy(saidx)
733 struct secasindex *saidx;
735 struct secashead *sah;
736 struct secasvar *sav;
737 u_int stateidx, state;
738 const u_int *saorder_state_valid;
741 LIST_FOREACH(sah, &sahtree, chain) {
742 if (sah->state == SADB_SASTATE_DEAD)
744 if (key_cmpsaidx(&sah->saidx, saidx, CMP_MODE_REQID))
753 * search a valid state list for outbound packet.
754 * This search order is important.
756 if (key_preferred_oldsa) {
757 saorder_state_valid = saorder_state_valid_prefer_old;
758 arraysize = _ARRAYLEN(saorder_state_valid_prefer_old);
760 saorder_state_valid = saorder_state_valid_prefer_new;
761 arraysize = _ARRAYLEN(saorder_state_valid_prefer_new);
764 for (stateidx = 0; stateidx < arraysize; stateidx++) {
766 state = saorder_state_valid[stateidx];
768 sav = key_do_allocsa_policy(sah, state);
777 * searching SAD with direction, protocol, mode and state.
778 * called by key_allocsa_policy().
781 * others : found, pointer to a SA.
783 static struct secasvar *
784 key_do_allocsa_policy(sah, state)
785 struct secashead *sah;
788 struct secasvar *sav, *nextsav, *candidate, *d;
793 for (sav = LIST_FIRST(&sah->savtree[state]);
797 nextsav = LIST_NEXT(sav, chain);
800 KEY_CHKSASTATE(sav->state, state, "key_do_allocsa_policy");
803 if (candidate == NULL) {
808 /* Which SA is the better ? */
811 if (candidate->lft_c == NULL || sav->lft_c == NULL)
812 panic("key_do_allocsa_policy: "
813 "lifetime_current is NULL.\n");
815 /* What the best method is to compare ? */
816 if (key_preferred_oldsa) {
817 if (candidate->lft_c->sadb_lifetime_addtime >
818 sav->lft_c->sadb_lifetime_addtime) {
825 /* prefered new sa rather than old sa */
826 if (candidate->lft_c->sadb_lifetime_addtime <
827 sav->lft_c->sadb_lifetime_addtime) {
834 * prepared to delete the SA when there is more
835 * suitable candidate and the lifetime of the SA is not
838 if (d->lft_c->sadb_lifetime_addtime != 0) {
839 struct mbuf *m, *result;
841 key_sa_chgstate(d, SADB_SASTATE_DEAD);
843 m = key_setsadbmsg(SADB_DELETE, 0,
844 d->sah->saidx.proto, 0, 0, d->refcnt - 1);
849 /* set sadb_address for saidx's. */
850 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
851 (struct sockaddr *)&d->sah->saidx.src,
852 d->sah->saidx.src.ss_len << 3,
858 /* set sadb_address for saidx's. */
859 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
860 (struct sockaddr *)&d->sah->saidx.src,
861 d->sah->saidx.src.ss_len << 3,
867 /* create SA extension */
868 m = key_setsadbsa(d);
873 if (result->m_len < sizeof(struct sadb_msg)) {
874 result = m_pullup(result,
875 sizeof(struct sadb_msg));
880 result->m_pkthdr.len = 0;
881 for (m = result; m; m = m->m_next)
882 result->m_pkthdr.len += m->m_len;
883 mtod(result, struct sadb_msg *)->sadb_msg_len =
884 PFKEY_UNIT64(result->m_pkthdr.len);
886 if (key_sendup_mbuf(NULL, result,
887 KEY_SENDUP_REGISTERED))
896 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
897 printf("DP allocsa_policy cause "
898 "refcnt++:%d SA:%p\n",
899 candidate->refcnt, candidate));
905 * allocating a SA entry for a *INBOUND* packet.
906 * Must call key_freesav() later.
907 * OUT: positive: pointer to a sav.
908 * NULL: not found, or error occured.
910 * In the comparison, source address will be ignored for RFC2401 conformance.
911 * To quote, from section 4.1:
912 * A security association is uniquely identified by a triple consisting
913 * of a Security Parameter Index (SPI), an IP Destination Address, and a
914 * security protocol (AH or ESP) identifier.
915 * Note that, however, we do need to keep source address in IPsec SA.
916 * IKE specification and PF_KEY specification do assume that we
917 * keep source address in IPsec SA. We see a tricky situation here.
920 key_allocsa(family, src, dst, proto, spi)
925 struct secashead *sah;
926 struct secasvar *sav;
927 u_int stateidx, state;
928 struct sockaddr_in sin;
929 struct sockaddr_in6 sin6;
931 const u_int *saorder_state_valid;
935 if (src == NULL || dst == NULL)
936 panic("key_allocsa: NULL pointer is passed.\n");
939 * when both systems employ similar strategy to use a SA.
940 * the search order is important even in the inbound case.
942 if (key_preferred_oldsa) {
943 saorder_state_valid = saorder_state_valid_prefer_old;
944 arraysize = _ARRAYLEN(saorder_state_valid_prefer_old);
946 saorder_state_valid = saorder_state_valid_prefer_new;
947 arraysize = _ARRAYLEN(saorder_state_valid_prefer_new);
952 * XXX: to be checked internal IP header somewhere. Also when
953 * IPsec tunnel packet is received. But ESP tunnel mode is
954 * encrypted so we can't check internal IP header.
956 s = splnet(); /*called from softclock()*/
957 LIST_FOREACH(sah, &sahtree, chain) {
959 * search a valid state list for inbound packet.
960 * the search order is not important.
962 for (stateidx = 0; stateidx < arraysize; stateidx++) {
963 state = saorder_state_valid[stateidx];
964 LIST_FOREACH(sav, &sah->savtree[state], chain) {
966 KEY_CHKSASTATE(sav->state, state, "key_allocsav");
967 if (proto != sav->sah->saidx.proto)
971 if (family != sav->sah->saidx.src.ss_family ||
972 family != sav->sah->saidx.dst.ss_family)
975 #if 0 /* don't check src */
976 /* check src address */
979 bzero(&sin, sizeof(sin));
980 sin.sin_family = AF_INET;
981 sin.sin_len = sizeof(sin);
982 bcopy(src, &sin.sin_addr,
983 sizeof(sin.sin_addr));
984 if (key_sockaddrcmp((struct sockaddr*)&sin,
985 (struct sockaddr *)&sav->sah->saidx.src, 0) != 0)
990 bzero(&sin6, sizeof(sin6));
991 sin6.sin6_family = AF_INET6;
992 sin6.sin6_len = sizeof(sin6);
993 bcopy(src, &sin6.sin6_addr,
994 sizeof(sin6.sin6_addr));
995 if (IN6_IS_SCOPE_LINKLOCAL(&sin6.sin6_addr)) {
996 /* kame fake scopeid */
998 ntohs(sin6.sin6_addr.s6_addr16[1]);
999 sin6.sin6_addr.s6_addr16[1] = 0;
1001 if (key_sockaddrcmp((struct sockaddr*)&sin6,
1002 (struct sockaddr *)&sav->sah->saidx.src, 0) != 0)
1006 ipseclog((LOG_DEBUG, "key_allocsa: "
1007 "unknown address family=%d.\n",
1013 /* check dst address */
1016 bzero(&sin, sizeof(sin));
1017 sin.sin_family = AF_INET;
1018 sin.sin_len = sizeof(sin);
1019 bcopy(dst, &sin.sin_addr,
1020 sizeof(sin.sin_addr));
1021 if (key_sockaddrcmp((struct sockaddr*)&sin,
1022 (struct sockaddr *)&sav->sah->saidx.dst, 0) != 0)
1027 bzero(&sin6, sizeof(sin6));
1028 sin6.sin6_family = AF_INET6;
1029 sin6.sin6_len = sizeof(sin6);
1030 bcopy(dst, &sin6.sin6_addr,
1031 sizeof(sin6.sin6_addr));
1032 if (IN6_IS_SCOPE_LINKLOCAL(&sin6.sin6_addr)) {
1033 /* kame fake scopeid */
1034 sin6.sin6_scope_id =
1035 ntohs(sin6.sin6_addr.s6_addr16[1]);
1036 sin6.sin6_addr.s6_addr16[1] = 0;
1038 if (key_sockaddrcmp((struct sockaddr*)&sin6,
1039 (struct sockaddr *)&sav->sah->saidx.dst, 0) != 0)
1043 ipseclog((LOG_DEBUG, "key_allocsa: "
1044 "unknown address family=%d.\n",
1061 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1062 printf("DP allocsa cause refcnt++:%d SA:%p\n",
1068 * Must be called after calling key_allocsp().
1069 * For both the packet without socket and key_freeso().
1073 struct secpolicy *sp;
1077 panic("key_freesp: NULL pointer is passed.\n");
1080 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1081 printf("DP freesp cause refcnt--:%d SP:%p\n",
1084 if (sp->refcnt == 0)
1091 * Must be called after calling key_allocsp().
1092 * For the packet with socket.
1100 panic("key_freeso: NULL pointer is passed.\n");
1102 switch (so->so_proto->pr_domain->dom_family) {
1106 struct inpcb *pcb = sotoinpcb(so);
1108 /* Does it have a PCB ? */
1111 key_freesp_so(&pcb->inp_sp->sp_in);
1112 key_freesp_so(&pcb->inp_sp->sp_out);
1119 #ifdef HAVE_NRL_INPCB
1120 struct inpcb *pcb = sotoinpcb(so);
1122 /* Does it have a PCB ? */
1125 key_freesp_so(&pcb->inp_sp->sp_in);
1126 key_freesp_so(&pcb->inp_sp->sp_out);
1128 struct in6pcb *pcb = sotoin6pcb(so);
1130 /* Does it have a PCB ? */
1133 key_freesp_so(&pcb->in6p_sp->sp_in);
1134 key_freesp_so(&pcb->in6p_sp->sp_out);
1140 ipseclog((LOG_DEBUG, "key_freeso: unknown address family=%d.\n",
1141 so->so_proto->pr_domain->dom_family));
1150 struct secpolicy **sp;
1153 if (sp == NULL || *sp == NULL)
1154 panic("key_freesp_so: sp == NULL\n");
1156 switch ((*sp)->policy) {
1157 case IPSEC_POLICY_IPSEC:
1158 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1159 printf("DP freeso calls free SP:%p\n", *sp));
1163 case IPSEC_POLICY_ENTRUST:
1164 case IPSEC_POLICY_BYPASS:
1167 panic("key_freesp_so: Invalid policy found %d", (*sp)->policy);
1174 * Must be called after calling key_allocsa().
1175 * This function is called by key_freesp() to free some SA allocated
1180 struct secasvar *sav;
1184 panic("key_freesav: NULL pointer is passed.\n");
1187 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1188 printf("DP freesav cause refcnt--:%d SA:%p SPI %u\n",
1189 sav->refcnt, sav, (u_int32_t)ntohl(sav->spi)));
1191 if (sav->refcnt == 0)
1197 /* %%% SPD management */
1199 * free security policy entry.
1203 struct secpolicy *sp;
1209 panic("key_delsp: NULL pointer is passed.\n");
1211 sp->state = IPSEC_SPSTATE_DEAD;
1214 return; /* can't free */
1216 s = splnet(); /*called from softclock()*/
1217 /* remove from SP index */
1218 if (__LIST_CHAINED(sp))
1219 LIST_REMOVE(sp, chain);
1222 struct ipsecrequest *isr = sp->req, *nextisr;
1224 while (isr != NULL) {
1225 if (isr->sav != NULL) {
1226 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1227 printf("DP delsp calls free SA:%p\n",
1229 key_freesav(isr->sav);
1233 nextisr = isr->next;
1239 keydb_delsecpolicy(sp);
1248 * OUT: NULL : not found
1249 * others : found, pointer to a SP.
1251 static struct secpolicy *
1253 struct secpolicyindex *spidx;
1255 struct secpolicy *sp;
1259 panic("key_getsp: NULL pointer is passed.\n");
1261 LIST_FOREACH(sp, &sptree[spidx->dir], chain) {
1262 if (sp->state == IPSEC_SPSTATE_DEAD)
1264 if (key_cmpspidx_exactly(spidx, &sp->spidx)) {
1275 * OUT: NULL : not found
1276 * others : found, pointer to a SP.
1278 static struct secpolicy *
1282 struct secpolicy *sp;
1284 LIST_FOREACH(sp, &sptree[IPSEC_DIR_INBOUND], chain) {
1285 if (sp->state == IPSEC_SPSTATE_DEAD)
1293 LIST_FOREACH(sp, &sptree[IPSEC_DIR_OUTBOUND], chain) {
1294 if (sp->state == IPSEC_SPSTATE_DEAD)
1308 struct secpolicy *newsp = NULL;
1310 newsp = keydb_newsecpolicy();
1321 * create secpolicy structure from sadb_x_policy structure.
1322 * NOTE: `state', `secpolicyindex' in secpolicy structure are not set,
1323 * so must be set properly later.
1326 key_msg2sp(xpl0, len, error)
1327 struct sadb_x_policy *xpl0;
1331 struct secpolicy *newsp;
1335 panic("key_msg2sp: NULL pointer was passed.\n");
1336 if (len < sizeof(*xpl0))
1337 panic("key_msg2sp: invalid length.\n");
1338 if (len != PFKEY_EXTLEN(xpl0)) {
1339 ipseclog((LOG_DEBUG, "key_msg2sp: Invalid msg length.\n"));
1344 if ((newsp = key_newsp()) == NULL) {
1349 newsp->spidx.dir = xpl0->sadb_x_policy_dir;
1350 newsp->policy = xpl0->sadb_x_policy_type;
1353 switch (xpl0->sadb_x_policy_type) {
1354 case IPSEC_POLICY_DISCARD:
1355 case IPSEC_POLICY_NONE:
1356 case IPSEC_POLICY_ENTRUST:
1357 case IPSEC_POLICY_BYPASS:
1361 case IPSEC_POLICY_IPSEC:
1364 struct sadb_x_ipsecrequest *xisr;
1365 struct ipsecrequest **p_isr = &newsp->req;
1367 /* validity check */
1368 if (PFKEY_EXTLEN(xpl0) < sizeof(*xpl0)) {
1369 ipseclog((LOG_DEBUG,
1370 "key_msg2sp: Invalid msg length.\n"));
1376 tlen = PFKEY_EXTLEN(xpl0) - sizeof(*xpl0);
1377 xisr = (struct sadb_x_ipsecrequest *)(xpl0 + 1);
1382 if (xisr->sadb_x_ipsecrequest_len < sizeof(*xisr)) {
1383 ipseclog((LOG_DEBUG, "key_msg2sp: "
1384 "invalid ipsecrequest length.\n"));
1390 /* allocate request buffer */
1391 KMALLOC(*p_isr, struct ipsecrequest *, sizeof(**p_isr));
1392 if ((*p_isr) == NULL) {
1393 ipseclog((LOG_DEBUG,
1394 "key_msg2sp: No more memory.\n"));
1399 bzero(*p_isr, sizeof(**p_isr));
1402 (*p_isr)->next = NULL;
1404 switch (xisr->sadb_x_ipsecrequest_proto) {
1407 case IPPROTO_IPCOMP:
1410 ipseclog((LOG_DEBUG,
1411 "key_msg2sp: invalid proto type=%u\n",
1412 xisr->sadb_x_ipsecrequest_proto));
1414 *error = EPROTONOSUPPORT;
1417 (*p_isr)->saidx.proto = xisr->sadb_x_ipsecrequest_proto;
1419 switch (xisr->sadb_x_ipsecrequest_mode) {
1420 case IPSEC_MODE_TRANSPORT:
1421 case IPSEC_MODE_TUNNEL:
1423 case IPSEC_MODE_ANY:
1425 ipseclog((LOG_DEBUG,
1426 "key_msg2sp: invalid mode=%u\n",
1427 xisr->sadb_x_ipsecrequest_mode));
1432 (*p_isr)->saidx.mode = xisr->sadb_x_ipsecrequest_mode;
1434 switch (xisr->sadb_x_ipsecrequest_level) {
1435 case IPSEC_LEVEL_DEFAULT:
1436 case IPSEC_LEVEL_USE:
1437 case IPSEC_LEVEL_REQUIRE:
1439 case IPSEC_LEVEL_UNIQUE:
1440 /* validity check */
1442 * If range violation of reqid, kernel will
1443 * update it, don't refuse it.
1445 if (xisr->sadb_x_ipsecrequest_reqid
1446 > IPSEC_MANUAL_REQID_MAX) {
1447 ipseclog((LOG_DEBUG,
1448 "key_msg2sp: reqid=%d range "
1449 "violation, updated by kernel.\n",
1450 xisr->sadb_x_ipsecrequest_reqid));
1451 xisr->sadb_x_ipsecrequest_reqid = 0;
1454 /* allocate new reqid id if reqid is zero. */
1455 if (xisr->sadb_x_ipsecrequest_reqid == 0) {
1457 if ((reqid = key_newreqid()) == 0) {
1462 (*p_isr)->saidx.reqid = reqid;
1463 xisr->sadb_x_ipsecrequest_reqid = reqid;
1465 /* set it for manual keying. */
1466 (*p_isr)->saidx.reqid =
1467 xisr->sadb_x_ipsecrequest_reqid;
1472 ipseclog((LOG_DEBUG, "key_msg2sp: invalid level=%u\n",
1473 xisr->sadb_x_ipsecrequest_level));
1478 (*p_isr)->level = xisr->sadb_x_ipsecrequest_level;
1480 /* set IP addresses if there */
1481 if (xisr->sadb_x_ipsecrequest_len > sizeof(*xisr)) {
1482 struct sockaddr *paddr;
1484 paddr = (struct sockaddr *)(xisr + 1);
1486 /* validity check */
1488 > sizeof((*p_isr)->saidx.src)) {
1489 ipseclog((LOG_DEBUG, "key_msg2sp: invalid request "
1490 "address length.\n"));
1495 bcopy(paddr, &(*p_isr)->saidx.src,
1498 paddr = (struct sockaddr *)((caddr_t)paddr
1501 /* validity check */
1503 > sizeof((*p_isr)->saidx.dst)) {
1504 ipseclog((LOG_DEBUG, "key_msg2sp: invalid request "
1505 "address length.\n"));
1510 bcopy(paddr, &(*p_isr)->saidx.dst,
1514 (*p_isr)->sav = NULL;
1515 (*p_isr)->sp = newsp;
1517 /* initialization for the next. */
1518 p_isr = &(*p_isr)->next;
1519 tlen -= xisr->sadb_x_ipsecrequest_len;
1521 /* validity check */
1523 ipseclog((LOG_DEBUG, "key_msg2sp: becoming tlen < 0.\n"));
1529 xisr = (struct sadb_x_ipsecrequest *)((caddr_t)xisr
1530 + xisr->sadb_x_ipsecrequest_len);
1535 ipseclog((LOG_DEBUG, "key_msg2sp: invalid policy type.\n"));
1548 static u_int32_t auto_reqid = IPSEC_MANUAL_REQID_MAX + 1;
1550 auto_reqid = (auto_reqid == ~0
1551 ? IPSEC_MANUAL_REQID_MAX + 1 : auto_reqid + 1);
1553 /* XXX should be unique check */
1559 * copy secpolicy struct to sadb_x_policy structure indicated.
1563 struct secpolicy *sp;
1565 struct sadb_x_policy *xpl;
1572 panic("key_sp2msg: NULL pointer was passed.\n");
1574 tlen = key_getspreqmsglen(sp);
1576 m = key_alloc_mbuf(tlen);
1577 if (!m || m->m_next) { /*XXX*/
1585 xpl = mtod(m, struct sadb_x_policy *);
1588 xpl->sadb_x_policy_len = PFKEY_UNIT64(tlen);
1589 xpl->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
1590 xpl->sadb_x_policy_type = sp->policy;
1591 xpl->sadb_x_policy_dir = sp->spidx.dir;
1592 xpl->sadb_x_policy_id = sp->id;
1593 p = (caddr_t)xpl + sizeof(*xpl);
1595 /* if is the policy for ipsec ? */
1596 if (sp->policy == IPSEC_POLICY_IPSEC) {
1597 struct sadb_x_ipsecrequest *xisr;
1598 struct ipsecrequest *isr;
1600 for (isr = sp->req; isr != NULL; isr = isr->next) {
1602 xisr = (struct sadb_x_ipsecrequest *)p;
1604 xisr->sadb_x_ipsecrequest_proto = isr->saidx.proto;
1605 xisr->sadb_x_ipsecrequest_mode = isr->saidx.mode;
1606 xisr->sadb_x_ipsecrequest_level = isr->level;
1607 xisr->sadb_x_ipsecrequest_reqid = isr->saidx.reqid;
1610 bcopy(&isr->saidx.src, p, isr->saidx.src.ss_len);
1611 p += isr->saidx.src.ss_len;
1612 bcopy(&isr->saidx.dst, p, isr->saidx.dst.ss_len);
1613 p += isr->saidx.src.ss_len;
1615 xisr->sadb_x_ipsecrequest_len =
1616 PFKEY_ALIGN8(sizeof(*xisr)
1617 + isr->saidx.src.ss_len
1618 + isr->saidx.dst.ss_len);
1625 /* m will not be freed nor modified */
1626 static struct mbuf *
1627 key_gather_mbuf(struct mbuf *m, const struct sadb_msghdr *mhp,
1628 int ndeep, int nitem, ...)
1633 struct mbuf *result = NULL, *n;
1636 if (m == NULL || mhp == NULL)
1637 panic("null pointer passed to key_gather");
1639 __va_start(ap, nitem);
1640 for (i = 0; i < nitem; i++) {
1641 idx = __va_arg(ap, int);
1642 if (idx < 0 || idx > SADB_EXT_MAX)
1644 /* don't attempt to pull empty extension */
1645 if (idx == SADB_EXT_RESERVED && mhp->msg == NULL)
1647 if (idx != SADB_EXT_RESERVED &&
1648 (mhp->ext[idx] == NULL || mhp->extlen[idx] == 0))
1651 if (idx == SADB_EXT_RESERVED) {
1652 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
1655 panic("assumption failed");
1657 MGETHDR(n, MB_DONTWAIT, MT_DATA);
1662 m_copydata(m, 0, sizeof(struct sadb_msg),
1664 } else if (i < ndeep) {
1665 len = mhp->extlen[idx];
1666 n = key_alloc_mbuf(len);
1667 if (!n || n->m_next) { /*XXX*/
1672 m_copydata(m, mhp->extoff[idx], mhp->extlen[idx],
1675 n = m_copym(m, mhp->extoff[idx], mhp->extlen[idx],
1688 if ((result->m_flags & M_PKTHDR) != 0) {
1689 result->m_pkthdr.len = 0;
1690 for (n = result; n; n = n->m_next)
1691 result->m_pkthdr.len += n->m_len;
1702 * SADB_X_SPDADD, SADB_X_SPDSETIDX or SADB_X_SPDUPDATE processing
1703 * add a entry to SP database, when received
1704 * <base, address(SD), (lifetime(H),) policy>
1706 * Adding to SP database,
1708 * <base, address(SD), (lifetime(H),) policy>
1709 * to the socket which was send.
1711 * SPDADD set a unique policy entry.
1712 * SPDSETIDX like SPDADD without a part of policy requests.
1713 * SPDUPDATE replace a unique policy entry.
1715 * m will always be freed.
1718 key_spdadd(so, m, mhp)
1721 const struct sadb_msghdr *mhp;
1723 struct sadb_address *src0, *dst0;
1724 struct sadb_x_policy *xpl0, *xpl;
1725 struct sadb_lifetime *lft = NULL;
1726 struct secpolicyindex spidx;
1727 struct secpolicy *newsp;
1732 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
1733 panic("key_spdadd: NULL pointer is passed.\n");
1735 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
1736 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
1737 mhp->ext[SADB_X_EXT_POLICY] == NULL) {
1738 ipseclog((LOG_DEBUG, "key_spdadd: invalid message is passed.\n"));
1739 return key_senderror(so, m, EINVAL);
1741 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
1742 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
1743 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
1744 ipseclog((LOG_DEBUG, "key_spdadd: invalid message is passed.\n"));
1745 return key_senderror(so, m, EINVAL);
1747 if (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL) {
1748 if (mhp->extlen[SADB_EXT_LIFETIME_HARD]
1749 < sizeof(struct sadb_lifetime)) {
1750 ipseclog((LOG_DEBUG, "key_spdadd: invalid message is passed.\n"));
1751 return key_senderror(so, m, EINVAL);
1753 lft = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_HARD];
1756 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
1757 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
1758 xpl0 = (struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY];
1761 /* XXX boundary check against sa_len */
1762 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
1765 src0->sadb_address_prefixlen,
1766 dst0->sadb_address_prefixlen,
1767 src0->sadb_address_proto,
1770 /* checking the direciton. */
1771 switch (xpl0->sadb_x_policy_dir) {
1772 case IPSEC_DIR_INBOUND:
1773 case IPSEC_DIR_OUTBOUND:
1776 ipseclog((LOG_DEBUG, "key_spdadd: Invalid SP direction.\n"));
1777 mhp->msg->sadb_msg_errno = EINVAL;
1782 /* key_spdadd() accepts DISCARD, NONE and IPSEC. */
1783 if (xpl0->sadb_x_policy_type == IPSEC_POLICY_ENTRUST
1784 || xpl0->sadb_x_policy_type == IPSEC_POLICY_BYPASS) {
1785 ipseclog((LOG_DEBUG, "key_spdadd: Invalid policy type.\n"));
1786 return key_senderror(so, m, EINVAL);
1789 /* policy requests are mandatory when action is ipsec. */
1790 if (mhp->msg->sadb_msg_type != SADB_X_SPDSETIDX
1791 && xpl0->sadb_x_policy_type == IPSEC_POLICY_IPSEC
1792 && mhp->extlen[SADB_X_EXT_POLICY] <= sizeof(*xpl0)) {
1793 ipseclog((LOG_DEBUG, "key_spdadd: some policy requests part required.\n"));
1794 return key_senderror(so, m, EINVAL);
1798 * checking there is SP already or not.
1799 * SPDUPDATE doesn't depend on whether there is a SP or not.
1800 * If the type is either SPDADD or SPDSETIDX AND a SP is found,
1803 newsp = key_getsp(&spidx);
1804 if (mhp->msg->sadb_msg_type == SADB_X_SPDUPDATE) {
1806 newsp->state = IPSEC_SPSTATE_DEAD;
1810 if (newsp != NULL) {
1812 ipseclog((LOG_DEBUG, "key_spdadd: a SP entry exists already.\n"));
1813 return key_senderror(so, m, EEXIST);
1817 /* allocation new SP entry */
1818 if ((newsp = key_msg2sp(xpl0, PFKEY_EXTLEN(xpl0), &error)) == NULL) {
1819 return key_senderror(so, m, error);
1822 if ((newsp->id = key_getnewspid()) == 0) {
1823 keydb_delsecpolicy(newsp);
1824 return key_senderror(so, m, ENOBUFS);
1827 /* XXX boundary check against sa_len */
1828 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
1831 src0->sadb_address_prefixlen,
1832 dst0->sadb_address_prefixlen,
1833 src0->sadb_address_proto,
1836 /* sanity check on addr pair */
1837 if (((struct sockaddr *)(src0 + 1))->sa_family !=
1838 ((struct sockaddr *)(dst0+ 1))->sa_family) {
1839 keydb_delsecpolicy(newsp);
1840 return key_senderror(so, m, EINVAL);
1842 if (((struct sockaddr *)(src0 + 1))->sa_len !=
1843 ((struct sockaddr *)(dst0+ 1))->sa_len) {
1844 keydb_delsecpolicy(newsp);
1845 return key_senderror(so, m, EINVAL);
1848 if (newsp->req && newsp->req->saidx.src.ss_family) {
1849 struct sockaddr *sa;
1850 sa = (struct sockaddr *)(src0 + 1);
1851 if (sa->sa_family != newsp->req->saidx.src.ss_family) {
1852 keydb_delsecpolicy(newsp);
1853 return key_senderror(so, m, EINVAL);
1856 if (newsp->req && newsp->req->saidx.dst.ss_family) {
1857 struct sockaddr *sa;
1858 sa = (struct sockaddr *)(dst0 + 1);
1859 if (sa->sa_family != newsp->req->saidx.dst.ss_family) {
1860 keydb_delsecpolicy(newsp);
1861 return key_senderror(so, m, EINVAL);
1867 newsp->created = tv.tv_sec;
1868 newsp->lastused = tv.tv_sec;
1869 newsp->lifetime = lft ? lft->sadb_lifetime_addtime : 0;
1870 newsp->validtime = lft ? lft->sadb_lifetime_usetime : 0;
1872 newsp->refcnt = 1; /* do not reclaim until I say I do */
1873 newsp->state = IPSEC_SPSTATE_ALIVE;
1874 LIST_INSERT_TAIL(&sptree[newsp->spidx.dir], newsp, secpolicy, chain);
1876 /* delete the entry in spacqtree */
1877 if (mhp->msg->sadb_msg_type == SADB_X_SPDUPDATE) {
1878 struct secspacq *spacq;
1879 if ((spacq = key_getspacq(&spidx)) != NULL) {
1880 /* reset counter in order to deletion by timehandler. */
1882 spacq->created = tv.tv_sec;
1888 struct mbuf *n, *mpolicy;
1889 struct sadb_msg *newmsg;
1892 /* create new sadb_msg to reply. */
1894 n = key_gather_mbuf(m, mhp, 2, 5, SADB_EXT_RESERVED,
1895 SADB_X_EXT_POLICY, SADB_EXT_LIFETIME_HARD,
1896 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
1898 n = key_gather_mbuf(m, mhp, 2, 4, SADB_EXT_RESERVED,
1900 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
1903 return key_senderror(so, m, ENOBUFS);
1905 if (n->m_len < sizeof(*newmsg)) {
1906 n = m_pullup(n, sizeof(*newmsg));
1908 return key_senderror(so, m, ENOBUFS);
1910 newmsg = mtod(n, struct sadb_msg *);
1911 newmsg->sadb_msg_errno = 0;
1912 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
1915 mpolicy = m_pulldown(n, PFKEY_ALIGN8(sizeof(struct sadb_msg)),
1916 sizeof(*xpl), &off);
1917 if (mpolicy == NULL) {
1918 /* n is already freed */
1919 return key_senderror(so, m, ENOBUFS);
1921 xpl = (struct sadb_x_policy *)(mtod(mpolicy, caddr_t) + off);
1922 if (xpl->sadb_x_policy_exttype != SADB_X_EXT_POLICY) {
1924 return key_senderror(so, m, EINVAL);
1926 xpl->sadb_x_policy_id = newsp->id;
1929 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
1934 * get new policy id.
1942 u_int32_t newid = 0;
1943 int count = key_spi_trycnt; /* XXX */
1944 struct secpolicy *sp;
1946 /* when requesting to allocate spi ranged */
1948 newid = (policy_id = (policy_id == ~0 ? 1 : policy_id + 1));
1950 if ((sp = key_getspbyid(newid)) == NULL)
1956 if (count == 0 || newid == 0) {
1957 ipseclog((LOG_DEBUG, "key_getnewspid: to allocate policy id is failed.\n"));
1965 * SADB_SPDDELETE processing
1967 * <base, address(SD), policy(*)>
1968 * from the user(?), and set SADB_SASTATE_DEAD,
1970 * <base, address(SD), policy(*)>
1972 * policy(*) including direction of policy.
1974 * m will always be freed.
1977 key_spddelete(so, m, mhp)
1980 const struct sadb_msghdr *mhp;
1982 struct sadb_address *src0, *dst0;
1983 struct sadb_x_policy *xpl0;
1984 struct secpolicyindex spidx;
1985 struct secpolicy *sp;
1988 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
1989 panic("key_spddelete: NULL pointer is passed.\n");
1991 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
1992 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
1993 mhp->ext[SADB_X_EXT_POLICY] == NULL) {
1994 ipseclog((LOG_DEBUG, "key_spddelete: invalid message is passed.\n"));
1995 return key_senderror(so, m, EINVAL);
1997 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
1998 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
1999 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2000 ipseclog((LOG_DEBUG, "key_spddelete: invalid message is passed.\n"));
2001 return key_senderror(so, m, EINVAL);
2004 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
2005 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
2006 xpl0 = (struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY];
2009 /* XXX boundary check against sa_len */
2010 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
2013 src0->sadb_address_prefixlen,
2014 dst0->sadb_address_prefixlen,
2015 src0->sadb_address_proto,
2018 /* checking the direciton. */
2019 switch (xpl0->sadb_x_policy_dir) {
2020 case IPSEC_DIR_INBOUND:
2021 case IPSEC_DIR_OUTBOUND:
2024 ipseclog((LOG_DEBUG, "key_spddelete: Invalid SP direction.\n"));
2025 return key_senderror(so, m, EINVAL);
2028 /* Is there SP in SPD ? */
2029 if ((sp = key_getsp(&spidx)) == NULL) {
2030 ipseclog((LOG_DEBUG, "key_spddelete: no SP found.\n"));
2031 return key_senderror(so, m, EINVAL);
2034 /* save policy id to buffer to be returned. */
2035 xpl0->sadb_x_policy_id = sp->id;
2037 sp->state = IPSEC_SPSTATE_DEAD;
2042 struct sadb_msg *newmsg;
2044 /* create new sadb_msg to reply. */
2045 n = key_gather_mbuf(m, mhp, 1, 4, SADB_EXT_RESERVED,
2046 SADB_X_EXT_POLICY, SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
2048 return key_senderror(so, m, ENOBUFS);
2050 newmsg = mtod(n, struct sadb_msg *);
2051 newmsg->sadb_msg_errno = 0;
2052 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
2055 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
2060 * SADB_SPDDELETE2 processing
2063 * from the user(?), and set SADB_SASTATE_DEAD,
2067 * policy(*) including direction of policy.
2069 * m will always be freed.
2072 key_spddelete2(so, m, mhp)
2075 const struct sadb_msghdr *mhp;
2078 struct secpolicy *sp;
2081 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
2082 panic("key_spddelete2: NULL pointer is passed.\n");
2084 if (mhp->ext[SADB_X_EXT_POLICY] == NULL ||
2085 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2086 ipseclog((LOG_DEBUG, "key_spddelete2: invalid message is passed.\n"));
2087 key_senderror(so, m, EINVAL);
2091 id = ((struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY])->sadb_x_policy_id;
2093 /* Is there SP in SPD ? */
2094 if ((sp = key_getspbyid(id)) == NULL) {
2095 ipseclog((LOG_DEBUG, "key_spddelete2: no SP found id:%u.\n", id));
2096 key_senderror(so, m, EINVAL);
2099 sp->state = IPSEC_SPSTATE_DEAD;
2103 struct mbuf *n, *nn;
2104 struct sadb_msg *newmsg;
2107 /* create new sadb_msg to reply. */
2108 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
2111 return key_senderror(so, m, ENOBUFS);
2112 MGETHDR(n, MB_DONTWAIT, MT_DATA);
2113 if (n && len > MHLEN) {
2114 MCLGET(n, MB_DONTWAIT);
2115 if ((n->m_flags & M_EXT) == 0) {
2121 return key_senderror(so, m, ENOBUFS);
2127 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
2128 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
2132 panic("length inconsistency in key_spddelete2");
2135 n->m_next = m_copym(m, mhp->extoff[SADB_X_EXT_POLICY],
2136 mhp->extlen[SADB_X_EXT_POLICY], MB_DONTWAIT);
2139 return key_senderror(so, m, ENOBUFS);
2142 n->m_pkthdr.len = 0;
2143 for (nn = n; nn; nn = nn->m_next)
2144 n->m_pkthdr.len += nn->m_len;
2146 newmsg = mtod(n, struct sadb_msg *);
2147 newmsg->sadb_msg_errno = 0;
2148 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
2151 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
2156 * SADB_X_GET processing
2161 * <base, address(SD), policy>
2163 * policy(*) including direction of policy.
2165 * m will always be freed.
2168 key_spdget(so, m, mhp)
2171 const struct sadb_msghdr *mhp;
2174 struct secpolicy *sp;
2178 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
2179 panic("key_spdget: NULL pointer is passed.\n");
2181 if (mhp->ext[SADB_X_EXT_POLICY] == NULL ||
2182 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2183 ipseclog((LOG_DEBUG, "key_spdget: invalid message is passed.\n"));
2184 return key_senderror(so, m, EINVAL);
2187 id = ((struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY])->sadb_x_policy_id;
2189 /* Is there SP in SPD ? */
2190 if ((sp = key_getspbyid(id)) == NULL) {
2191 ipseclog((LOG_DEBUG, "key_spdget: no SP found id:%u.\n", id));
2192 return key_senderror(so, m, ENOENT);
2195 n = key_setdumpsp(sp, SADB_X_SPDGET, 0, mhp->msg->sadb_msg_pid);
2198 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
2200 return key_senderror(so, m, ENOBUFS);
2204 * SADB_X_SPDACQUIRE processing.
2205 * Acquire policy and SA(s) for a *OUTBOUND* packet.
2208 * to KMD, and expect to receive
2209 * <base> with SADB_X_SPDACQUIRE if error occured,
2212 * with SADB_X_SPDUPDATE from KMD by PF_KEY.
2213 * policy(*) is without policy requests.
2216 * others: error number
2220 struct secpolicy *sp;
2222 struct mbuf *result = NULL, *m;
2223 struct secspacq *newspacq;
2228 panic("key_spdacquire: NULL pointer is passed.\n");
2229 if (sp->req != NULL)
2230 panic("key_spdacquire: called but there is request.\n");
2231 if (sp->policy != IPSEC_POLICY_IPSEC)
2232 panic("key_spdacquire: policy mismathed. IPsec is expected.\n");
2234 /* get a entry to check whether sent message or not. */
2235 if ((newspacq = key_getspacq(&sp->spidx)) != NULL) {
2236 if (key_blockacq_count < newspacq->count) {
2237 /* reset counter and do send message. */
2238 newspacq->count = 0;
2240 /* increment counter and do nothing. */
2245 /* make new entry for blocking to send SADB_ACQUIRE. */
2246 if ((newspacq = key_newspacq(&sp->spidx)) == NULL)
2249 /* add to acqtree */
2250 LIST_INSERT_HEAD(&spacqtree, newspacq, chain);
2253 /* create new sadb_msg to reply. */
2254 m = key_setsadbmsg(SADB_X_SPDACQUIRE, 0, 0, 0, 0, 0);
2261 result->m_pkthdr.len = 0;
2262 for (m = result; m; m = m->m_next)
2263 result->m_pkthdr.len += m->m_len;
2265 mtod(result, struct sadb_msg *)->sadb_msg_len =
2266 PFKEY_UNIT64(result->m_pkthdr.len);
2268 return key_sendup_mbuf(NULL, m, KEY_SENDUP_REGISTERED);
2277 * SADB_SPDFLUSH processing
2280 * from the user, and free all entries in secpctree.
2284 * NOTE: what to do is only marking SADB_SASTATE_DEAD.
2286 * m will always be freed.
2289 key_spdflush(so, m, mhp)
2292 const struct sadb_msghdr *mhp;
2294 struct sadb_msg *newmsg;
2295 struct secpolicy *sp;
2299 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
2300 panic("key_spdflush: NULL pointer is passed.\n");
2302 if (m->m_len != PFKEY_ALIGN8(sizeof(struct sadb_msg)))
2303 return key_senderror(so, m, EINVAL);
2305 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2306 LIST_FOREACH(sp, &sptree[dir], chain) {
2307 sp->state = IPSEC_SPSTATE_DEAD;
2311 if (sizeof(struct sadb_msg) > m->m_len + M_TRAILINGSPACE(m)) {
2312 ipseclog((LOG_DEBUG, "key_spdflush: No more memory.\n"));
2313 return key_senderror(so, m, ENOBUFS);
2319 m->m_pkthdr.len = m->m_len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
2320 newmsg = mtod(m, struct sadb_msg *);
2321 newmsg->sadb_msg_errno = 0;
2322 newmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
2324 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
2328 * SADB_SPDDUMP processing
2331 * from the user, and dump all SP leaves
2336 * m will always be freed.
2339 key_spddump(so, m, mhp)
2342 const struct sadb_msghdr *mhp;
2344 struct secpolicy *sp;
2350 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
2351 panic("key_spddump: NULL pointer is passed.\n");
2353 /* search SPD entry and get buffer size. */
2355 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2356 LIST_FOREACH(sp, &sptree[dir], chain) {
2362 return key_senderror(so, m, ENOENT);
2364 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2365 LIST_FOREACH(sp, &sptree[dir], chain) {
2367 n = key_setdumpsp(sp, SADB_X_SPDDUMP, cnt,
2368 mhp->msg->sadb_msg_pid);
2371 key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
2379 static struct mbuf *
2380 key_setdumpsp(sp, type, seq, pid)
2381 struct secpolicy *sp;
2385 struct mbuf *result = NULL, *m;
2387 m = key_setsadbmsg(type, 0, SADB_SATYPE_UNSPEC, seq, pid, sp->refcnt);
2392 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
2393 (struct sockaddr *)&sp->spidx.src, sp->spidx.prefs,
2394 sp->spidx.ul_proto);
2399 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
2400 (struct sockaddr *)&sp->spidx.dst, sp->spidx.prefd,
2401 sp->spidx.ul_proto);
2411 if ((result->m_flags & M_PKTHDR) == 0)
2414 if (result->m_len < sizeof(struct sadb_msg)) {
2415 result = m_pullup(result, sizeof(struct sadb_msg));
2420 result->m_pkthdr.len = 0;
2421 for (m = result; m; m = m->m_next)
2422 result->m_pkthdr.len += m->m_len;
2424 mtod(result, struct sadb_msg *)->sadb_msg_len =
2425 PFKEY_UNIT64(result->m_pkthdr.len);
2435 * get PFKEY message length for security policy and request.
2438 key_getspreqmsglen(sp)
2439 struct secpolicy *sp;
2443 tlen = sizeof(struct sadb_x_policy);
2445 /* if is the policy for ipsec ? */
2446 if (sp->policy != IPSEC_POLICY_IPSEC)
2449 /* get length of ipsec requests */
2451 struct ipsecrequest *isr;
2454 for (isr = sp->req; isr != NULL; isr = isr->next) {
2455 len = sizeof(struct sadb_x_ipsecrequest)
2456 + isr->saidx.src.ss_len
2457 + isr->saidx.dst.ss_len;
2459 tlen += PFKEY_ALIGN8(len);
2467 * SADB_SPDEXPIRE processing
2469 * <base, address(SD), lifetime(CH), policy>
2473 * others : error number
2477 struct secpolicy *sp;
2480 struct mbuf *result = NULL, *m;
2483 struct sadb_lifetime *lt;
2485 /* XXX: Why do we lock ? */
2486 s = splnet(); /*called from softclock()*/
2490 panic("key_spdexpire: NULL pointer is passed.\n");
2492 /* set msg header */
2493 m = key_setsadbmsg(SADB_X_SPDEXPIRE, 0, 0, 0, 0, 0);
2500 /* create lifetime extension (current and hard) */
2501 len = PFKEY_ALIGN8(sizeof(*lt)) * 2;
2502 m = key_alloc_mbuf(len);
2503 if (!m || m->m_next) { /*XXX*/
2509 bzero(mtod(m, caddr_t), len);
2510 lt = mtod(m, struct sadb_lifetime *);
2511 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
2512 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
2513 lt->sadb_lifetime_allocations = 0;
2514 lt->sadb_lifetime_bytes = 0;
2515 lt->sadb_lifetime_addtime = sp->created;
2516 lt->sadb_lifetime_usetime = sp->lastused;
2517 lt = (struct sadb_lifetime *)(mtod(m, caddr_t) + len / 2);
2518 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
2519 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
2520 lt->sadb_lifetime_allocations = 0;
2521 lt->sadb_lifetime_bytes = 0;
2522 lt->sadb_lifetime_addtime = sp->lifetime;
2523 lt->sadb_lifetime_usetime = sp->validtime;
2526 /* set sadb_address for source */
2527 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
2528 (struct sockaddr *)&sp->spidx.src,
2529 sp->spidx.prefs, sp->spidx.ul_proto);
2536 /* set sadb_address for destination */
2537 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
2538 (struct sockaddr *)&sp->spidx.dst,
2539 sp->spidx.prefd, sp->spidx.ul_proto);
2554 if ((result->m_flags & M_PKTHDR) == 0) {
2559 if (result->m_len < sizeof(struct sadb_msg)) {
2560 result = m_pullup(result, sizeof(struct sadb_msg));
2561 if (result == NULL) {
2567 result->m_pkthdr.len = 0;
2568 for (m = result; m; m = m->m_next)
2569 result->m_pkthdr.len += m->m_len;
2571 mtod(result, struct sadb_msg *)->sadb_msg_len =
2572 PFKEY_UNIT64(result->m_pkthdr.len);
2574 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
2583 /* %%% SAD management */
2585 * allocating a memory for new SA head, and copy from the values of mhp.
2586 * OUT: NULL : failure due to the lack of memory.
2587 * others : pointer to new SA head.
2589 static struct secashead *
2591 struct secasindex *saidx;
2593 struct secashead *newsah;
2597 panic("key_newsaidx: NULL pointer is passed.\n");
2599 newsah = keydb_newsecashead();
2603 bcopy(saidx, &newsah->saidx, sizeof(newsah->saidx));
2605 /* add to saidxtree */
2606 newsah->state = SADB_SASTATE_MATURE;
2607 LIST_INSERT_HEAD(&sahtree, newsah, chain);
2613 * delete SA index and all SA registerd.
2617 struct secashead *sah;
2619 struct secasvar *sav, *nextsav;
2620 u_int stateidx, state;
2626 panic("key_delsah: NULL pointer is passed.\n");
2628 s = splnet(); /*called from softclock()*/
2630 /* searching all SA registerd in the secindex. */
2632 stateidx < _ARRAYLEN(saorder_state_any);
2635 state = saorder_state_any[stateidx];
2636 for (sav = (struct secasvar *)LIST_FIRST(&sah->savtree[state]);
2640 nextsav = LIST_NEXT(sav, chain);
2642 if (sav->refcnt > 0) {
2643 /* give up to delete this sa */
2649 KEY_CHKSASTATE(state, sav->state, "key_delsah");
2653 /* remove back pointer */
2659 /* don't delete sah only if there are savs. */
2665 if (sah->sa_route.ro_rt) {
2666 RTFREE(sah->sa_route.ro_rt);
2667 sah->sa_route.ro_rt = (struct rtentry *)NULL;
2670 /* remove from tree of SA index */
2671 if (__LIST_CHAINED(sah))
2672 LIST_REMOVE(sah, chain);
2681 * allocating a new SA with LARVAL state. key_add() and key_getspi() call,
2682 * and copy the values of mhp into new buffer.
2683 * When SAD message type is GETSPI:
2684 * to set sequence number from acq_seq++,
2685 * to set zero to SPI.
2686 * not to call key_setsava().
2688 * others : pointer to new secasvar.
2690 * does not modify mbuf. does not free mbuf on error.
2692 static struct secasvar *
2693 key_newsav(m, mhp, sah, errp)
2695 const struct sadb_msghdr *mhp;
2696 struct secashead *sah;
2699 struct secasvar *newsav;
2700 const struct sadb_sa *xsa;
2703 if (m == NULL || mhp == NULL || mhp->msg == NULL || sah == NULL)
2704 panic("key_newsa: NULL pointer is passed.\n");
2706 KMALLOC(newsav, struct secasvar *, sizeof(struct secasvar));
2707 if (newsav == NULL) {
2708 ipseclog((LOG_DEBUG, "key_newsa: No more memory.\n"));
2712 bzero((caddr_t)newsav, sizeof(struct secasvar));
2714 switch (mhp->msg->sadb_msg_type) {
2718 #ifdef IPSEC_DOSEQCHECK
2719 /* sync sequence number */
2720 if (mhp->msg->sadb_msg_seq == 0)
2722 (acq_seq = (acq_seq == ~0 ? 1 : ++acq_seq));
2725 newsav->seq = mhp->msg->sadb_msg_seq;
2730 if (mhp->ext[SADB_EXT_SA] == NULL) {
2732 ipseclog((LOG_DEBUG, "key_newsa: invalid message is passed.\n"));
2736 xsa = (const struct sadb_sa *)mhp->ext[SADB_EXT_SA];
2737 newsav->spi = xsa->sadb_sa_spi;
2738 newsav->seq = mhp->msg->sadb_msg_seq;
2746 /* copy sav values */
2747 if (mhp->msg->sadb_msg_type != SADB_GETSPI) {
2748 *errp = key_setsaval(newsav, m, mhp);
2759 newsav->created = tv.tv_sec;
2762 newsav->pid = mhp->msg->sadb_msg_pid;
2767 newsav->state = SADB_SASTATE_LARVAL;
2768 LIST_INSERT_TAIL(&sah->savtree[SADB_SASTATE_LARVAL], newsav,
2775 * free() SA variable entry.
2779 struct secasvar *sav;
2783 panic("key_delsav: NULL pointer is passed.\n");
2785 if (sav->refcnt > 0)
2786 return; /* can't free */
2788 /* remove from SA header */
2789 if (__LIST_CHAINED(sav))
2790 LIST_REMOVE(sav, chain);
2792 if (sav->key_auth != NULL) {
2793 bzero(_KEYBUF(sav->key_auth), _KEYLEN(sav->key_auth));
2794 KFREE(sav->key_auth);
2795 sav->key_auth = NULL;
2797 if (sav->key_enc != NULL) {
2798 bzero(_KEYBUF(sav->key_enc), _KEYLEN(sav->key_enc));
2799 KFREE(sav->key_enc);
2800 sav->key_enc = NULL;
2803 bzero(sav->sched, sav->schedlen);
2807 if (sav->replay != NULL) {
2808 keydb_delsecreplay(sav->replay);
2811 if (sav->lft_c != NULL) {
2815 if (sav->lft_h != NULL) {
2819 if (sav->lft_s != NULL) {
2823 if (sav->iv != NULL) {
2837 * others : found, pointer to a SA.
2839 static struct secashead *
2841 struct secasindex *saidx;
2843 struct secashead *sah;
2845 LIST_FOREACH(sah, &sahtree, chain) {
2846 if (sah->state == SADB_SASTATE_DEAD)
2848 if (key_cmpsaidx(&sah->saidx, saidx, CMP_REQID))
2856 * check not to be duplicated SPI.
2857 * NOTE: this function is too slow due to searching all SAD.
2860 * others : found, pointer to a SA.
2862 static struct secasvar *
2863 key_checkspidup(saidx, spi)
2864 struct secasindex *saidx;
2867 struct secashead *sah;
2868 struct secasvar *sav;
2870 /* check address family */
2871 if (saidx->src.ss_family != saidx->dst.ss_family) {
2872 ipseclog((LOG_DEBUG, "key_checkspidup: address family mismatched.\n"));
2877 LIST_FOREACH(sah, &sahtree, chain) {
2878 if (!key_ismyaddr((struct sockaddr *)&sah->saidx.dst))
2880 sav = key_getsavbyspi(sah, spi);
2889 * search SAD litmited alive SA, protocol, SPI.
2892 * others : found, pointer to a SA.
2894 static struct secasvar *
2895 key_getsavbyspi(sah, spi)
2896 struct secashead *sah;
2899 struct secasvar *sav;
2900 u_int stateidx, state;
2902 /* search all status */
2904 stateidx < _ARRAYLEN(saorder_state_alive);
2907 state = saorder_state_alive[stateidx];
2908 LIST_FOREACH(sav, &sah->savtree[state], chain) {
2911 if (sav->state != state) {
2912 ipseclog((LOG_DEBUG, "key_getsavbyspi: "
2913 "invalid sav->state (queue: %d SA: %d)\n",
2914 state, sav->state));
2918 if (sav->spi == spi)
2927 * copy SA values from PF_KEY message except *SPI, SEQ, PID, STATE and TYPE*.
2928 * You must update these if need.
2932 * does not modify mbuf. does not free mbuf on error.
2935 key_setsaval(sav, m, mhp)
2936 struct secasvar *sav;
2938 const struct sadb_msghdr *mhp;
2941 const struct esp_algorithm *algo;
2947 if (m == NULL || mhp == NULL || mhp->msg == NULL)
2948 panic("key_setsaval: NULL pointer is passed.\n");
2950 /* initialization */
2952 sav->key_auth = NULL;
2953 sav->key_enc = NULL;
2962 if (mhp->ext[SADB_EXT_SA] != NULL) {
2963 const struct sadb_sa *sa0;
2965 sa0 = (const struct sadb_sa *)mhp->ext[SADB_EXT_SA];
2966 if (mhp->extlen[SADB_EXT_SA] < sizeof(*sa0)) {
2971 sav->alg_auth = sa0->sadb_sa_auth;
2972 sav->alg_enc = sa0->sadb_sa_encrypt;
2973 sav->flags = sa0->sadb_sa_flags;
2976 if ((sa0->sadb_sa_flags & SADB_X_EXT_OLD) == 0) {
2977 sav->replay = keydb_newsecreplay(sa0->sadb_sa_replay);
2978 if (sav->replay == NULL) {
2979 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
2986 /* Authentication keys */
2987 if (mhp->ext[SADB_EXT_KEY_AUTH] != NULL) {
2988 const struct sadb_key *key0;
2991 key0 = (const struct sadb_key *)mhp->ext[SADB_EXT_KEY_AUTH];
2992 len = mhp->extlen[SADB_EXT_KEY_AUTH];
2995 if (len < sizeof(*key0)) {
2999 switch (mhp->msg->sadb_msg_satype) {
3000 case SADB_SATYPE_AH:
3001 case SADB_SATYPE_ESP:
3002 if (len == PFKEY_ALIGN8(sizeof(struct sadb_key)) &&
3003 sav->alg_auth != SADB_X_AALG_NULL)
3006 case SADB_X_SATYPE_IPCOMP:
3012 ipseclog((LOG_DEBUG, "key_setsaval: invalid key_auth values.\n"));
3016 sav->key_auth = (struct sadb_key *)key_newbuf(key0, len);
3017 if (sav->key_auth == NULL) {
3018 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
3024 /* Encryption key */
3025 if (mhp->ext[SADB_EXT_KEY_ENCRYPT] != NULL) {
3026 const struct sadb_key *key0;
3029 key0 = (const struct sadb_key *)mhp->ext[SADB_EXT_KEY_ENCRYPT];
3030 len = mhp->extlen[SADB_EXT_KEY_ENCRYPT];
3033 if (len < sizeof(*key0)) {
3037 switch (mhp->msg->sadb_msg_satype) {
3038 case SADB_SATYPE_ESP:
3039 if (len == PFKEY_ALIGN8(sizeof(struct sadb_key)) &&
3040 sav->alg_enc != SADB_EALG_NULL) {
3044 sav->key_enc = (struct sadb_key *)key_newbuf(key0, len);
3045 if (sav->key_enc == NULL) {
3046 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
3051 case SADB_X_SATYPE_IPCOMP:
3052 if (len != PFKEY_ALIGN8(sizeof(struct sadb_key)))
3054 sav->key_enc = NULL; /*just in case*/
3056 case SADB_SATYPE_AH:
3062 ipseclog((LOG_DEBUG, "key_setsatval: invalid key_enc value.\n"));
3070 switch (mhp->msg->sadb_msg_satype) {
3071 case SADB_SATYPE_ESP:
3073 algo = esp_algorithm_lookup(sav->alg_enc);
3074 if (algo && algo->ivlen)
3075 sav->ivlen = (*algo->ivlen)(algo, sav);
3076 if (sav->ivlen == 0)
3078 KMALLOC(sav->iv, caddr_t, sav->ivlen);
3080 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
3086 key_randomfill(sav->iv, sav->ivlen);
3089 case SADB_SATYPE_AH:
3090 case SADB_X_SATYPE_IPCOMP:
3093 ipseclog((LOG_DEBUG, "key_setsaval: invalid SA type.\n"));
3100 sav->created = tv.tv_sec;
3102 /* make lifetime for CURRENT */
3103 KMALLOC(sav->lft_c, struct sadb_lifetime *,
3104 sizeof(struct sadb_lifetime));
3105 if (sav->lft_c == NULL) {
3106 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
3113 sav->lft_c->sadb_lifetime_len =
3114 PFKEY_UNIT64(sizeof(struct sadb_lifetime));
3115 sav->lft_c->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
3116 sav->lft_c->sadb_lifetime_allocations = 0;
3117 sav->lft_c->sadb_lifetime_bytes = 0;
3118 sav->lft_c->sadb_lifetime_addtime = tv.tv_sec;
3119 sav->lft_c->sadb_lifetime_usetime = 0;
3121 /* lifetimes for HARD and SOFT */
3123 const struct sadb_lifetime *lft0;
3125 lft0 = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_HARD];
3127 if (mhp->extlen[SADB_EXT_LIFETIME_HARD] < sizeof(*lft0)) {
3131 sav->lft_h = (struct sadb_lifetime *)key_newbuf(lft0,
3133 if (sav->lft_h == NULL) {
3134 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
3138 /* to be initialize ? */
3141 lft0 = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_SOFT];
3143 if (mhp->extlen[SADB_EXT_LIFETIME_SOFT] < sizeof(*lft0)) {
3147 sav->lft_s = (struct sadb_lifetime *)key_newbuf(lft0,
3149 if (sav->lft_s == NULL) {
3150 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
3154 /* to be initialize ? */
3161 /* initialization */
3162 if (sav->replay != NULL) {
3163 keydb_delsecreplay(sav->replay);
3166 if (sav->key_auth != NULL) {
3167 KFREE(sav->key_auth);
3168 sav->key_auth = NULL;
3170 if (sav->key_enc != NULL) {
3171 KFREE(sav->key_enc);
3172 sav->key_enc = NULL;
3178 if (sav->iv != NULL) {
3182 if (sav->lft_c != NULL) {
3186 if (sav->lft_h != NULL) {
3190 if (sav->lft_s != NULL) {
3199 * validation with a secasvar entry, and set SADB_SATYPE_MATURE.
3205 struct secasvar *sav;
3208 int checkmask = 0; /* 2^0: ealg 2^1: aalg 2^2: calg */
3209 int mustmask = 0; /* 2^0: ealg 2^1: aalg 2^2: calg */
3213 /* check SPI value */
3214 switch (sav->sah->saidx.proto) {
3217 if (ntohl(sav->spi) >= 0 && ntohl(sav->spi) <= 255) {
3218 ipseclog((LOG_DEBUG,
3219 "key_mature: illegal range of SPI %u.\n",
3220 (u_int32_t)ntohl(sav->spi)));
3227 switch (sav->sah->saidx.proto) {
3230 if ((sav->flags & SADB_X_EXT_OLD)
3231 && (sav->flags & SADB_X_EXT_DERIV)) {
3232 ipseclog((LOG_DEBUG, "key_mature: "
3233 "invalid flag (derived) given to old-esp.\n"));
3236 if (sav->alg_auth == SADB_AALG_NONE)
3244 if (sav->flags & SADB_X_EXT_DERIV) {
3245 ipseclog((LOG_DEBUG, "key_mature: "
3246 "invalid flag (derived) given to AH SA.\n"));
3249 if (sav->alg_enc != SADB_EALG_NONE) {
3250 ipseclog((LOG_DEBUG, "key_mature: "
3251 "protocol and algorithm mismated.\n"));
3257 case IPPROTO_IPCOMP:
3258 if (sav->alg_auth != SADB_AALG_NONE) {
3259 ipseclog((LOG_DEBUG, "key_mature: "
3260 "protocol and algorithm mismated.\n"));
3263 if ((sav->flags & SADB_X_EXT_RAWCPI) == 0
3264 && ntohl(sav->spi) >= 0x10000) {
3265 ipseclog((LOG_DEBUG, "key_mature: invalid cpi for IPComp.\n"));
3272 ipseclog((LOG_DEBUG, "key_mature: Invalid satype.\n"));
3273 return EPROTONOSUPPORT;
3276 /* check authentication algorithm */
3277 if ((checkmask & 2) != 0) {
3278 const struct ah_algorithm *algo;
3281 algo = ah_algorithm_lookup(sav->alg_auth);
3283 ipseclog((LOG_DEBUG,"key_mature: "
3284 "unknown authentication algorithm.\n"));
3288 /* algorithm-dependent check */
3290 keylen = sav->key_auth->sadb_key_bits;
3293 if (keylen < algo->keymin || algo->keymax < keylen) {
3294 ipseclog((LOG_DEBUG,
3295 "key_mature: invalid AH key length %d "
3296 "(%d-%d allowed)\n",
3297 keylen, algo->keymin, algo->keymax));
3302 if ((*algo->mature)(sav)) {
3303 /* message generated in per-algorithm function*/
3306 mature = SADB_SATYPE_AH;
3309 if ((mustmask & 2) != 0 && mature != SADB_SATYPE_AH) {
3310 ipseclog((LOG_DEBUG, "key_mature: no satisfy algorithm for AH\n"));
3315 /* check encryption algorithm */
3316 if ((checkmask & 1) != 0) {
3318 const struct esp_algorithm *algo;
3321 algo = esp_algorithm_lookup(sav->alg_enc);
3323 ipseclog((LOG_DEBUG, "key_mature: unknown encryption algorithm.\n"));
3327 /* algorithm-dependent check */
3329 keylen = sav->key_enc->sadb_key_bits;
3332 if (keylen < algo->keymin || algo->keymax < keylen) {
3333 ipseclog((LOG_DEBUG,
3334 "key_mature: invalid ESP key length %d "
3335 "(%d-%d allowed)\n",
3336 keylen, algo->keymin, algo->keymax));
3341 if ((*algo->mature)(sav)) {
3342 /* message generated in per-algorithm function*/
3345 mature = SADB_SATYPE_ESP;
3348 if ((mustmask & 1) != 0 && mature != SADB_SATYPE_ESP) {
3349 ipseclog((LOG_DEBUG, "key_mature: no satisfy algorithm for ESP\n"));
3353 ipseclog((LOG_DEBUG, "key_mature: ESP not supported in this configuration\n"));
3358 /* check compression algorithm */
3359 if ((checkmask & 4) != 0) {
3360 const struct ipcomp_algorithm *algo;
3362 /* algorithm-dependent check */
3363 algo = ipcomp_algorithm_lookup(sav->alg_enc);
3365 ipseclog((LOG_DEBUG, "key_mature: unknown compression algorithm.\n"));
3370 key_sa_chgstate(sav, SADB_SASTATE_MATURE);
3376 * subroutine for SADB_GET and SADB_DUMP.
3378 static struct mbuf *
3379 key_setdumpsa(sav, type, satype, seq, pid)
3380 struct secasvar *sav;
3381 u_int8_t type, satype;
3384 struct mbuf *result = NULL, *tres = NULL, *m;
3389 SADB_EXT_SA, SADB_X_EXT_SA2,
3390 SADB_EXT_LIFETIME_HARD, SADB_EXT_LIFETIME_SOFT,
3391 SADB_EXT_LIFETIME_CURRENT, SADB_EXT_ADDRESS_SRC,
3392 SADB_EXT_ADDRESS_DST, SADB_EXT_ADDRESS_PROXY, SADB_EXT_KEY_AUTH,
3393 SADB_EXT_KEY_ENCRYPT, SADB_EXT_IDENTITY_SRC,
3394 SADB_EXT_IDENTITY_DST, SADB_EXT_SENSITIVITY,
3397 m = key_setsadbmsg(type, 0, satype, seq, pid, sav->refcnt);
3402 for (i = sizeof(dumporder)/sizeof(dumporder[0]) - 1; i >= 0; i--) {
3405 switch (dumporder[i]) {
3407 m = key_setsadbsa(sav);
3412 case SADB_X_EXT_SA2:
3413 m = key_setsadbxsa2(sav->sah->saidx.mode,
3414 sav->replay ? sav->replay->count : 0,
3415 sav->sah->saidx.reqid);
3420 case SADB_EXT_ADDRESS_SRC:
3421 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
3422 (struct sockaddr *)&sav->sah->saidx.src,
3423 FULLMASK, IPSEC_ULPROTO_ANY);
3428 case SADB_EXT_ADDRESS_DST:
3429 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
3430 (struct sockaddr *)&sav->sah->saidx.dst,
3431 FULLMASK, IPSEC_ULPROTO_ANY);
3436 case SADB_EXT_KEY_AUTH:
3439 l = PFKEY_UNUNIT64(sav->key_auth->sadb_key_len);
3443 case SADB_EXT_KEY_ENCRYPT:
3446 l = PFKEY_UNUNIT64(sav->key_enc->sadb_key_len);
3450 case SADB_EXT_LIFETIME_CURRENT:
3453 l = PFKEY_UNUNIT64(((struct sadb_ext *)sav->lft_c)->sadb_ext_len);
3457 case SADB_EXT_LIFETIME_HARD:
3460 l = PFKEY_UNUNIT64(((struct sadb_ext *)sav->lft_h)->sadb_ext_len);
3464 case SADB_EXT_LIFETIME_SOFT:
3467 l = PFKEY_UNUNIT64(((struct sadb_ext *)sav->lft_s)->sadb_ext_len);
3471 case SADB_EXT_ADDRESS_PROXY:
3472 case SADB_EXT_IDENTITY_SRC:
3473 case SADB_EXT_IDENTITY_DST:
3474 /* XXX: should we brought from SPD ? */
3475 case SADB_EXT_SENSITIVITY:
3480 if ((!m && !p) || (m && p))
3483 M_PREPEND(tres, l, MB_DONTWAIT);
3486 bcopy(p, mtod(tres, caddr_t), l);
3490 m = key_alloc_mbuf(l);
3493 m_copyback(m, 0, l, p);
3501 m_cat(result, tres);
3503 if (result->m_len < sizeof(struct sadb_msg)) {
3504 result = m_pullup(result, sizeof(struct sadb_msg));
3509 result->m_pkthdr.len = 0;
3510 for (m = result; m; m = m->m_next)
3511 result->m_pkthdr.len += m->m_len;
3513 mtod(result, struct sadb_msg *)->sadb_msg_len =
3514 PFKEY_UNIT64(result->m_pkthdr.len);
3525 * set data into sadb_msg.
3527 static struct mbuf *
3528 key_setsadbmsg(type, tlen, satype, seq, pid, reserved)
3529 u_int8_t type, satype;
3539 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
3542 MGETHDR(m, MB_DONTWAIT, MT_DATA);
3543 if (m && len > MHLEN) {
3544 MCLGET(m, MB_DONTWAIT);
3545 if ((m->m_flags & M_EXT) == 0) {
3552 m->m_pkthdr.len = m->m_len = len;
3555 p = mtod(m, struct sadb_msg *);
3558 p->sadb_msg_version = PF_KEY_V2;
3559 p->sadb_msg_type = type;
3560 p->sadb_msg_errno = 0;
3561 p->sadb_msg_satype = satype;
3562 p->sadb_msg_len = PFKEY_UNIT64(tlen);
3563 p->sadb_msg_reserved = reserved;
3564 p->sadb_msg_seq = seq;
3565 p->sadb_msg_pid = (u_int32_t)pid;
3571 * copy secasvar data into sadb_address.
3573 static struct mbuf *
3575 struct secasvar *sav;
3581 len = PFKEY_ALIGN8(sizeof(struct sadb_sa));
3582 m = key_alloc_mbuf(len);
3583 if (!m || m->m_next) { /*XXX*/
3589 p = mtod(m, struct sadb_sa *);
3592 p->sadb_sa_len = PFKEY_UNIT64(len);
3593 p->sadb_sa_exttype = SADB_EXT_SA;
3594 p->sadb_sa_spi = sav->spi;
3595 p->sadb_sa_replay = (sav->replay != NULL ? sav->replay->wsize : 0);
3596 p->sadb_sa_state = sav->state;
3597 p->sadb_sa_auth = sav->alg_auth;
3598 p->sadb_sa_encrypt = sav->alg_enc;
3599 p->sadb_sa_flags = sav->flags;
3605 * set data into sadb_address.
3607 static struct mbuf *
3608 key_setsadbaddr(exttype, saddr, prefixlen, ul_proto)
3610 struct sockaddr *saddr;
3615 struct sadb_address *p;
3618 len = PFKEY_ALIGN8(sizeof(struct sadb_address)) +
3619 PFKEY_ALIGN8(saddr->sa_len);
3620 m = key_alloc_mbuf(len);
3621 if (!m || m->m_next) { /*XXX*/
3627 p = mtod(m, struct sadb_address *);
3630 p->sadb_address_len = PFKEY_UNIT64(len);
3631 p->sadb_address_exttype = exttype;
3632 p->sadb_address_proto = ul_proto;
3633 if (prefixlen == FULLMASK) {
3634 switch (saddr->sa_family) {
3636 prefixlen = sizeof(struct in_addr) << 3;
3639 prefixlen = sizeof(struct in6_addr) << 3;
3645 p->sadb_address_prefixlen = prefixlen;
3646 p->sadb_address_reserved = 0;
3649 mtod(m, caddr_t) + PFKEY_ALIGN8(sizeof(struct sadb_address)),
3657 * set data into sadb_ident.
3659 static struct mbuf *
3660 key_setsadbident(exttype, idtype, string, stringlen, id)
3661 u_int16_t exttype, idtype;
3667 struct sadb_ident *p;
3670 len = PFKEY_ALIGN8(sizeof(struct sadb_ident)) + PFKEY_ALIGN8(stringlen);
3671 m = key_alloc_mbuf(len);
3672 if (!m || m->m_next) { /*XXX*/
3678 p = mtod(m, struct sadb_ident *);
3681 p->sadb_ident_len = PFKEY_UNIT64(len);
3682 p->sadb_ident_exttype = exttype;
3683 p->sadb_ident_type = idtype;
3684 p->sadb_ident_reserved = 0;
3685 p->sadb_ident_id = id;
3688 mtod(m, caddr_t) + PFKEY_ALIGN8(sizeof(struct sadb_ident)),
3696 * set data into sadb_x_sa2.
3698 static struct mbuf *
3699 key_setsadbxsa2(mode, seq, reqid)
3701 u_int32_t seq, reqid;
3704 struct sadb_x_sa2 *p;
3707 len = PFKEY_ALIGN8(sizeof(struct sadb_x_sa2));
3708 m = key_alloc_mbuf(len);
3709 if (!m || m->m_next) { /*XXX*/
3715 p = mtod(m, struct sadb_x_sa2 *);
3718 p->sadb_x_sa2_len = PFKEY_UNIT64(len);
3719 p->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
3720 p->sadb_x_sa2_mode = mode;
3721 p->sadb_x_sa2_reserved1 = 0;
3722 p->sadb_x_sa2_reserved2 = 0;
3723 p->sadb_x_sa2_sequence = seq;
3724 p->sadb_x_sa2_reqid = reqid;
3730 * set data into sadb_x_policy
3732 static struct mbuf *
3733 key_setsadbxpolicy(type, dir, id)
3739 struct sadb_x_policy *p;
3742 len = PFKEY_ALIGN8(sizeof(struct sadb_x_policy));
3743 m = key_alloc_mbuf(len);
3744 if (!m || m->m_next) { /*XXX*/
3750 p = mtod(m, struct sadb_x_policy *);
3753 p->sadb_x_policy_len = PFKEY_UNIT64(len);
3754 p->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3755 p->sadb_x_policy_type = type;
3756 p->sadb_x_policy_dir = dir;
3757 p->sadb_x_policy_id = id;
3764 * copy a buffer into the new buffer allocated.
3767 key_newbuf(src, len)
3773 KMALLOC(new, caddr_t, len);
3775 ipseclog((LOG_DEBUG, "key_newbuf: No more memory.\n"));
3778 bcopy(src, new, len);
3783 /* compare my own address
3784 * OUT: 1: true, i.e. my address.
3789 struct sockaddr *sa;
3792 struct sockaddr_in *sin;
3793 struct in_ifaddr *ia;
3798 panic("key_ismyaddr: NULL pointer is passed.\n");
3800 switch (sa->sa_family) {
3803 sin = (struct sockaddr_in *)sa;
3804 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
3805 if (sin->sin_family == ia->ia_addr.sin_family &&
3806 sin->sin_len == ia->ia_addr.sin_len &&
3807 sin->sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr)
3816 return key_ismyaddr6((struct sockaddr_in6 *)sa);
3825 * compare my own address for IPv6.
3828 * NOTE: derived ip6_input() in KAME. This is necessary to modify more.
3830 #include <netinet6/in6_var.h>
3834 struct sockaddr_in6 *sin6;
3836 struct in6_ifaddr *ia;
3837 struct in6_multi *in6m;
3839 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
3840 if (key_sockaddrcmp((struct sockaddr *)&sin6,
3841 (struct sockaddr *)&ia->ia_addr, 0) == 0)
3846 * XXX why do we care about multlicast here while we don't care
3847 * about IPv4 multicast??
3851 IN6_LOOKUP_MULTI(sin6->sin6_addr, ia->ia_ifp, in6m);
3856 /* loopback, just for safety */
3857 if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
3865 * compare two secasindex structure.
3866 * flag can specify to compare 2 saidxes.
3867 * compare two secasindex structure without both mode and reqid.
3868 * don't compare port.
3870 * saidx0: source, it can be in SAD.
3877 key_cmpsaidx(saidx0, saidx1, flag)
3878 struct secasindex *saidx0, *saidx1;
3882 if (saidx0 == NULL && saidx1 == NULL)
3885 if (saidx0 == NULL || saidx1 == NULL)
3888 if (saidx0->proto != saidx1->proto)
3891 if (flag == CMP_EXACTLY) {
3892 if (saidx0->mode != saidx1->mode)
3894 if (saidx0->reqid != saidx1->reqid)
3896 if (bcmp(&saidx0->src, &saidx1->src, saidx0->src.ss_len) != 0 ||
3897 bcmp(&saidx0->dst, &saidx1->dst, saidx0->dst.ss_len) != 0)
3901 /* CMP_MODE_REQID, CMP_REQID, CMP_HEAD */
3902 if (flag == CMP_MODE_REQID
3903 ||flag == CMP_REQID) {
3905 * If reqid of SPD is non-zero, unique SA is required.
3906 * The result must be of same reqid in this case.
3908 if (saidx1->reqid != 0 && saidx0->reqid != saidx1->reqid)
3912 if (flag == CMP_MODE_REQID) {
3913 if (saidx0->mode != IPSEC_MODE_ANY
3914 && saidx0->mode != saidx1->mode)
3918 if (key_sockaddrcmp((struct sockaddr *)&saidx0->src,
3919 (struct sockaddr *)&saidx1->src, 0) != 0) {
3922 if (key_sockaddrcmp((struct sockaddr *)&saidx0->dst,
3923 (struct sockaddr *)&saidx1->dst, 0) != 0) {
3932 * compare two secindex structure exactly.
3934 * spidx0: source, it is often in SPD.
3935 * spidx1: object, it is often from PFKEY message.
3941 key_cmpspidx_exactly(spidx0, spidx1)
3942 struct secpolicyindex *spidx0, *spidx1;
3945 if (spidx0 == NULL && spidx1 == NULL)
3948 if (spidx0 == NULL || spidx1 == NULL)
3951 if (spidx0->prefs != spidx1->prefs
3952 || spidx0->prefd != spidx1->prefd
3953 || spidx0->ul_proto != spidx1->ul_proto)
3956 if (key_sockaddrcmp((struct sockaddr *)&spidx0->src,
3957 (struct sockaddr *)&spidx1->src, 1) != 0) {
3960 if (key_sockaddrcmp((struct sockaddr *)&spidx0->dst,
3961 (struct sockaddr *)&spidx1->dst, 1) != 0) {
3969 * compare two secindex structure with mask.
3971 * spidx0: source, it is often in SPD.
3972 * spidx1: object, it is often from IP header.
3978 key_cmpspidx_withmask(spidx0, spidx1)
3979 struct secpolicyindex *spidx0, *spidx1;
3982 if (spidx0 == NULL && spidx1 == NULL)
3985 if (spidx0 == NULL || spidx1 == NULL)
3988 if (spidx0->src.ss_family != spidx1->src.ss_family ||
3989 spidx0->dst.ss_family != spidx1->dst.ss_family ||
3990 spidx0->src.ss_len != spidx1->src.ss_len ||
3991 spidx0->dst.ss_len != spidx1->dst.ss_len)
3994 /* if spidx.ul_proto == IPSEC_ULPROTO_ANY, ignore. */
3995 if (spidx0->ul_proto != (u_int16_t)IPSEC_ULPROTO_ANY
3996 && spidx0->ul_proto != spidx1->ul_proto)
3999 switch (spidx0->src.ss_family) {
4001 if (satosin(&spidx0->src)->sin_port != IPSEC_PORT_ANY
4002 && satosin(&spidx0->src)->sin_port !=
4003 satosin(&spidx1->src)->sin_port)
4005 if (!key_bbcmp((caddr_t)&satosin(&spidx0->src)->sin_addr,
4006 (caddr_t)&satosin(&spidx1->src)->sin_addr, spidx0->prefs))
4010 if (satosin6(&spidx0->src)->sin6_port != IPSEC_PORT_ANY
4011 && satosin6(&spidx0->src)->sin6_port !=
4012 satosin6(&spidx1->src)->sin6_port)
4015 * scope_id check. if sin6_scope_id is 0, we regard it
4016 * as a wildcard scope, which matches any scope zone ID.
4018 if (satosin6(&spidx0->src)->sin6_scope_id &&
4019 satosin6(&spidx1->src)->sin6_scope_id &&
4020 satosin6(&spidx0->src)->sin6_scope_id !=
4021 satosin6(&spidx1->src)->sin6_scope_id)
4023 if (!key_bbcmp((caddr_t)&satosin6(&spidx0->src)->sin6_addr,
4024 (caddr_t)&satosin6(&spidx1->src)->sin6_addr, spidx0->prefs))
4029 if (bcmp(&spidx0->src, &spidx1->src, spidx0->src.ss_len) != 0)
4034 switch (spidx0->dst.ss_family) {
4036 if (satosin(&spidx0->dst)->sin_port != IPSEC_PORT_ANY
4037 && satosin(&spidx0->dst)->sin_port !=
4038 satosin(&spidx1->dst)->sin_port)
4040 if (!key_bbcmp((caddr_t)&satosin(&spidx0->dst)->sin_addr,
4041 (caddr_t)&satosin(&spidx1->dst)->sin_addr, spidx0->prefd))
4045 if (satosin6(&spidx0->dst)->sin6_port != IPSEC_PORT_ANY
4046 && satosin6(&spidx0->dst)->sin6_port !=
4047 satosin6(&spidx1->dst)->sin6_port)
4050 * scope_id check. if sin6_scope_id is 0, we regard it
4051 * as a wildcard scope, which matches any scope zone ID.
4053 if (satosin6(&spidx0->src)->sin6_scope_id &&
4054 satosin6(&spidx1->src)->sin6_scope_id &&
4055 satosin6(&spidx0->dst)->sin6_scope_id !=
4056 satosin6(&spidx1->dst)->sin6_scope_id)
4058 if (!key_bbcmp((caddr_t)&satosin6(&spidx0->dst)->sin6_addr,
4059 (caddr_t)&satosin6(&spidx1->dst)->sin6_addr, spidx0->prefd))
4064 if (bcmp(&spidx0->dst, &spidx1->dst, spidx0->dst.ss_len) != 0)
4069 /* XXX Do we check other field ? e.g. flowinfo */
4074 /* returns 0 on match */
4076 key_sockaddrcmp(sa1, sa2, port)
4077 struct sockaddr *sa1;
4078 struct sockaddr *sa2;
4081 if (sa1->sa_family != sa2->sa_family || sa1->sa_len != sa2->sa_len)
4084 switch (sa1->sa_family) {
4086 if (sa1->sa_len != sizeof(struct sockaddr_in))
4088 if (satosin(sa1)->sin_addr.s_addr !=
4089 satosin(sa2)->sin_addr.s_addr) {
4092 if (port && satosin(sa1)->sin_port != satosin(sa2)->sin_port)
4096 if (sa1->sa_len != sizeof(struct sockaddr_in6))
4097 return 1; /*EINVAL*/
4098 if (satosin6(sa1)->sin6_scope_id !=
4099 satosin6(sa2)->sin6_scope_id) {
4102 if (!IN6_ARE_ADDR_EQUAL(&satosin6(sa1)->sin6_addr,
4103 &satosin6(sa2)->sin6_addr)) {
4107 satosin6(sa1)->sin6_port != satosin6(sa2)->sin6_port) {
4111 if (bcmp(sa1, sa2, sa1->sa_len) != 0)
4120 * compare two buffers with mask.
4124 * bits: Number of bits to compare
4130 key_bbcmp(p1, p2, bits)
4136 /* XXX: This could be considerably faster if we compare a word
4137 * at a time, but it is complicated on LSB Endian machines */
4139 /* Handle null pointers */
4140 if (p1 == NULL || p2 == NULL)
4150 mask = ~((1<<(8-bits))-1);
4151 if ((*p1 & mask) != (*p2 & mask))
4154 return 1; /* Match! */
4159 * scanning SPD and SAD to check status for each entries,
4160 * and do to remove or to expire.
4161 * XXX: year 2038 problem may remain.
4164 key_timehandler(void)
4172 s = splnet(); /*called from softclock()*/
4176 struct secpolicy *sp, *nextsp;
4178 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
4179 for (sp = LIST_FIRST(&sptree[dir]);
4183 nextsp = LIST_NEXT(sp, chain);
4185 if (sp->state == IPSEC_SPSTATE_DEAD) {
4190 if (sp->lifetime == 0 && sp->validtime == 0)
4193 /* the deletion will occur next time */
4195 && tv.tv_sec - sp->created > sp->lifetime)
4197 && tv.tv_sec - sp->lastused > sp->validtime)) {
4198 sp->state = IPSEC_SPSTATE_DEAD;
4208 struct secashead *sah, *nextsah;
4209 struct secasvar *sav, *nextsav;
4211 for (sah = LIST_FIRST(&sahtree);
4215 nextsah = LIST_NEXT(sah, chain);
4217 /* if sah has been dead, then delete it and process next sah. */
4218 if (sah->state == SADB_SASTATE_DEAD) {
4223 /* if LARVAL entry doesn't become MATURE, delete it. */
4224 for (sav = LIST_FIRST(&sah->savtree[SADB_SASTATE_LARVAL]);
4228 nextsav = LIST_NEXT(sav, chain);
4230 if (tv.tv_sec - sav->created > key_larval_lifetime) {
4236 * check MATURE entry to start to send expire message
4239 for (sav = LIST_FIRST(&sah->savtree[SADB_SASTATE_MATURE]);
4243 nextsav = LIST_NEXT(sav, chain);
4245 /* we don't need to check. */
4246 if (sav->lft_s == NULL)
4250 if (sav->lft_c == NULL) {
4251 ipseclog((LOG_DEBUG,"key_timehandler: "
4252 "There is no CURRENT time, why?\n"));
4256 /* check SOFT lifetime */
4257 if (sav->lft_s->sadb_lifetime_addtime != 0
4258 && tv.tv_sec - sav->created > sav->lft_s->sadb_lifetime_addtime) {
4260 * check the SA if it has been used.
4261 * when it hasn't been used, delete it.
4262 * i don't think such SA will be used.
4264 if (sav->lft_c->sadb_lifetime_usetime == 0) {
4265 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4269 key_sa_chgstate(sav, SADB_SASTATE_DYING);
4271 * XXX If we keep to send expire
4272 * message in the status of
4273 * DYING. Do remove below code.
4279 /* check SOFT lifetime by bytes */
4281 * XXX I don't know the way to delete this SA
4282 * when new SA is installed. Caution when it's
4283 * installed too big lifetime by time.
4285 else if (sav->lft_s->sadb_lifetime_bytes != 0
4286 && sav->lft_s->sadb_lifetime_bytes < sav->lft_c->sadb_lifetime_bytes) {
4288 key_sa_chgstate(sav, SADB_SASTATE_DYING);
4290 * XXX If we keep to send expire
4291 * message in the status of
4292 * DYING. Do remove below code.
4298 /* check DYING entry to change status to DEAD. */
4299 for (sav = LIST_FIRST(&sah->savtree[SADB_SASTATE_DYING]);
4303 nextsav = LIST_NEXT(sav, chain);
4305 /* we don't need to check. */
4306 if (sav->lft_h == NULL)
4310 if (sav->lft_c == NULL) {
4311 ipseclog((LOG_DEBUG, "key_timehandler: "
4312 "There is no CURRENT time, why?\n"));
4316 if (sav->lft_h->sadb_lifetime_addtime != 0
4317 && tv.tv_sec - sav->created > sav->lft_h->sadb_lifetime_addtime) {
4318 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4322 #if 0 /* XXX Should we keep to send expire message until HARD lifetime ? */
4323 else if (sav->lft_s != NULL
4324 && sav->lft_s->sadb_lifetime_addtime != 0
4325 && tv.tv_sec - sav->created > sav->lft_s->sadb_lifetime_addtime) {
4327 * XXX: should be checked to be
4328 * installed the valid SA.
4332 * If there is no SA then sending
4338 /* check HARD lifetime by bytes */
4339 else if (sav->lft_h->sadb_lifetime_bytes != 0
4340 && sav->lft_h->sadb_lifetime_bytes < sav->lft_c->sadb_lifetime_bytes) {
4341 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4347 /* delete entry in DEAD */
4348 for (sav = LIST_FIRST(&sah->savtree[SADB_SASTATE_DEAD]);
4352 nextsav = LIST_NEXT(sav, chain);
4355 if (sav->state != SADB_SASTATE_DEAD) {
4356 ipseclog((LOG_DEBUG, "key_timehandler: "
4357 "invalid sav->state "
4358 "(queue: %d SA: %d): "
4360 SADB_SASTATE_DEAD, sav->state));
4364 * do not call key_freesav() here.
4365 * sav should already be freed, and sav->refcnt
4366 * shows other references to sav
4367 * (such as from SPD).
4373 #ifndef IPSEC_NONBLOCK_ACQUIRE
4376 struct secacq *acq, *nextacq;
4378 for (acq = LIST_FIRST(&acqtree);
4382 nextacq = LIST_NEXT(acq, chain);
4384 if (tv.tv_sec - acq->created > key_blockacq_lifetime
4385 && __LIST_CHAINED(acq)) {
4386 LIST_REMOVE(acq, chain);
4395 struct secspacq *acq, *nextacq;
4397 for (acq = LIST_FIRST(&spacqtree);
4401 nextacq = LIST_NEXT(acq, chain);
4403 if (tv.tv_sec - acq->created > key_blockacq_lifetime
4404 && __LIST_CHAINED(acq)) {
4405 LIST_REMOVE(acq, chain);
4411 /* initialize random seed */
4412 if (key_tick_init_random++ > key_int_random) {
4413 key_tick_init_random = 0;
4417 #ifndef IPSEC_DEBUG2
4418 /* do exchange to tick time !! */
4419 (void)timeout((void *)key_timehandler, (void *)0, hz);
4420 #endif /* IPSEC_DEBUG2 */
4427 * to initialize a seed for random()
4436 srandom(tv.tv_usec);
4446 key_randomfill(&value, sizeof(value));
4451 key_randomfill(p, l)
4457 static int warn = 1;
4460 n = (size_t)read_random(p, (u_int)l);
4464 bcopy(&v, (u_int8_t *)p + n,
4465 l - n < sizeof(v) ? l - n : sizeof(v));
4469 printf("WARNING: pseudo-random number generator "
4470 "used for IPsec processing\n");
4477 * map SADB_SATYPE_* to IPPROTO_*.
4478 * if satype == SADB_SATYPE then satype is mapped to ~0.
4480 * 0: invalid satype.
4483 key_satype2proto(satype)
4487 case SADB_SATYPE_UNSPEC:
4488 return IPSEC_PROTO_ANY;
4489 case SADB_SATYPE_AH:
4491 case SADB_SATYPE_ESP:
4493 case SADB_X_SATYPE_IPCOMP:
4494 return IPPROTO_IPCOMP;
4503 * map IPPROTO_* to SADB_SATYPE_*
4505 * 0: invalid protocol type.
4508 key_proto2satype(proto)
4513 return SADB_SATYPE_AH;
4515 return SADB_SATYPE_ESP;
4516 case IPPROTO_IPCOMP:
4517 return SADB_X_SATYPE_IPCOMP;
4527 * SADB_GETSPI processing is to receive
4528 * <base, (SA2), src address, dst address, (SPI range)>
4529 * from the IKMPd, to assign a unique spi value, to hang on the INBOUND
4530 * tree with the status of LARVAL, and send
4531 * <base, SA(*), address(SD)>
4534 * IN: mhp: pointer to the pointer to each header.
4535 * OUT: NULL if fail.
4536 * other if success, return pointer to the message to send.
4539 key_getspi(so, m, mhp)
4542 const struct sadb_msghdr *mhp;
4544 struct sadb_address *src0, *dst0;
4545 struct secasindex saidx;
4546 struct secashead *newsah;
4547 struct secasvar *newsav;
4555 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
4556 panic("key_getspi: NULL pointer is passed.\n");
4558 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
4559 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
4560 ipseclog((LOG_DEBUG, "key_getspi: invalid message is passed.\n"));
4561 return key_senderror(so, m, EINVAL);
4563 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
4564 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
4565 ipseclog((LOG_DEBUG, "key_getspi: invalid message is passed.\n"));
4566 return key_senderror(so, m, EINVAL);
4568 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
4569 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
4570 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
4572 mode = IPSEC_MODE_ANY;
4576 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
4577 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
4579 /* map satype to proto */
4580 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
4581 ipseclog((LOG_DEBUG, "key_getspi: invalid satype is passed.\n"));
4582 return key_senderror(so, m, EINVAL);
4585 /* make sure if port number is zero. */
4586 switch (((struct sockaddr *)(src0 + 1))->sa_family) {
4588 if (((struct sockaddr *)(src0 + 1))->sa_len !=
4589 sizeof(struct sockaddr_in))
4590 return key_senderror(so, m, EINVAL);
4591 ((struct sockaddr_in *)(src0 + 1))->sin_port = 0;
4594 if (((struct sockaddr *)(src0 + 1))->sa_len !=
4595 sizeof(struct sockaddr_in6))
4596 return key_senderror(so, m, EINVAL);
4597 ((struct sockaddr_in6 *)(src0 + 1))->sin6_port = 0;
4602 switch (((struct sockaddr *)(dst0 + 1))->sa_family) {
4604 if (((struct sockaddr *)(dst0 + 1))->sa_len !=
4605 sizeof(struct sockaddr_in))
4606 return key_senderror(so, m, EINVAL);
4607 ((struct sockaddr_in *)(dst0 + 1))->sin_port = 0;
4610 if (((struct sockaddr *)(dst0 + 1))->sa_len !=
4611 sizeof(struct sockaddr_in6))
4612 return key_senderror(so, m, EINVAL);
4613 ((struct sockaddr_in6 *)(dst0 + 1))->sin6_port = 0;
4619 /* XXX boundary check against sa_len */
4620 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
4622 /* SPI allocation */
4623 spi = key_do_getnewspi((struct sadb_spirange *)mhp->ext[SADB_EXT_SPIRANGE],
4626 return key_senderror(so, m, EINVAL);
4628 /* get a SA index */
4629 if ((newsah = key_getsah(&saidx)) == NULL) {
4630 /* create a new SA index */
4631 if ((newsah = key_newsah(&saidx)) == NULL) {
4632 ipseclog((LOG_DEBUG, "key_getspi: No more memory.\n"));
4633 return key_senderror(so, m, ENOBUFS);
4639 newsav = key_newsav(m, mhp, newsah, &error);
4640 if (newsav == NULL) {
4641 /* XXX don't free new SA index allocated in above. */
4642 return key_senderror(so, m, error);
4646 newsav->spi = htonl(spi);
4648 #ifndef IPSEC_NONBLOCK_ACQUIRE
4649 /* delete the entry in acqtree */
4650 if (mhp->msg->sadb_msg_seq != 0) {
4652 if ((acq = key_getacqbyseq(mhp->msg->sadb_msg_seq)) != NULL) {
4653 /* reset counter in order to deletion by timehandler. */
4656 acq->created = tv.tv_sec;
4663 struct mbuf *n, *nn;
4664 struct sadb_sa *m_sa;
4665 struct sadb_msg *newmsg;
4668 /* create new sadb_msg to reply. */
4669 len = PFKEY_ALIGN8(sizeof(struct sadb_msg)) +
4670 PFKEY_ALIGN8(sizeof(struct sadb_sa));
4672 return key_senderror(so, m, ENOBUFS);
4674 MGETHDR(n, MB_DONTWAIT, MT_DATA);
4676 MCLGET(n, MB_DONTWAIT);
4677 if ((n->m_flags & M_EXT) == 0) {
4683 return key_senderror(so, m, ENOBUFS);
4689 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
4690 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
4692 m_sa = (struct sadb_sa *)(mtod(n, caddr_t) + off);
4693 m_sa->sadb_sa_len = PFKEY_UNIT64(sizeof(struct sadb_sa));
4694 m_sa->sadb_sa_exttype = SADB_EXT_SA;
4695 m_sa->sadb_sa_spi = htonl(spi);
4696 off += PFKEY_ALIGN8(sizeof(struct sadb_sa));
4700 panic("length inconsistency in key_getspi");
4703 n->m_next = key_gather_mbuf(m, mhp, 0, 2, SADB_EXT_ADDRESS_SRC,
4704 SADB_EXT_ADDRESS_DST);
4707 return key_senderror(so, m, ENOBUFS);
4710 if (n->m_len < sizeof(struct sadb_msg)) {
4711 n = m_pullup(n, sizeof(struct sadb_msg));
4713 return key_sendup_mbuf(so, m, KEY_SENDUP_ONE);
4716 n->m_pkthdr.len = 0;
4717 for (nn = n; nn; nn = nn->m_next)
4718 n->m_pkthdr.len += nn->m_len;
4720 newmsg = mtod(n, struct sadb_msg *);
4721 newmsg->sadb_msg_seq = newsav->seq;
4722 newmsg->sadb_msg_errno = 0;
4723 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
4726 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
4731 * allocating new SPI
4732 * called by key_getspi().
4738 key_do_getnewspi(spirange, saidx)
4739 struct sadb_spirange *spirange;
4740 struct secasindex *saidx;
4744 int count = key_spi_trycnt;
4746 /* set spi range to allocate */
4747 if (spirange != NULL) {
4748 min = spirange->sadb_spirange_min;
4749 max = spirange->sadb_spirange_max;
4751 min = key_spi_minval;
4752 max = key_spi_maxval;
4754 /* IPCOMP needs 2-byte SPI */
4755 if (saidx->proto == IPPROTO_IPCOMP) {
4762 t = min; min = max; max = t;
4767 if (key_checkspidup(saidx, min) != NULL) {
4768 ipseclog((LOG_DEBUG, "key_do_getnewspi: SPI %u exists already.\n", min));
4772 count--; /* taking one cost. */
4780 /* when requesting to allocate spi ranged */
4782 /* generate pseudo-random SPI value ranged. */
4783 newspi = min + (key_random() % (max - min + 1));
4785 if (key_checkspidup(saidx, newspi) == NULL)
4789 if (count == 0 || newspi == 0) {
4790 ipseclog((LOG_DEBUG, "key_do_getnewspi: to allocate spi is failed.\n"));
4796 keystat.getspi_count =
4797 (keystat.getspi_count + key_spi_trycnt - count) / 2;
4803 * SADB_UPDATE processing
4805 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4806 * key(AE), (identity(SD),) (sensitivity)>
4807 * from the ikmpd, and update a secasvar entry whose status is SADB_SASTATE_LARVAL.
4809 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4810 * (identity(SD),) (sensitivity)>
4813 * m will always be freed.
4816 key_update(so, m, mhp)
4819 const struct sadb_msghdr *mhp;
4821 struct sadb_sa *sa0;
4822 struct sadb_address *src0, *dst0;
4823 struct secasindex saidx;
4824 struct secashead *sah;
4825 struct secasvar *sav;
4832 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
4833 panic("key_update: NULL pointer is passed.\n");
4835 /* map satype to proto */
4836 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
4837 ipseclog((LOG_DEBUG, "key_update: invalid satype is passed.\n"));
4838 return key_senderror(so, m, EINVAL);
4841 if (mhp->ext[SADB_EXT_SA] == NULL ||
4842 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
4843 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
4844 (mhp->msg->sadb_msg_satype == SADB_SATYPE_ESP &&
4845 mhp->ext[SADB_EXT_KEY_ENCRYPT] == NULL) ||
4846 (mhp->msg->sadb_msg_satype == SADB_SATYPE_AH &&
4847 mhp->ext[SADB_EXT_KEY_AUTH] == NULL) ||
4848 (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL &&
4849 mhp->ext[SADB_EXT_LIFETIME_SOFT] == NULL) ||
4850 (mhp->ext[SADB_EXT_LIFETIME_HARD] == NULL &&
4851 mhp->ext[SADB_EXT_LIFETIME_SOFT] != NULL)) {
4852 ipseclog((LOG_DEBUG, "key_update: invalid message is passed.\n"));
4853 return key_senderror(so, m, EINVAL);
4855 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
4856 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
4857 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
4858 ipseclog((LOG_DEBUG, "key_update: invalid message is passed.\n"));
4859 return key_senderror(so, m, EINVAL);
4861 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
4862 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
4863 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
4865 mode = IPSEC_MODE_ANY;
4868 /* XXX boundary checking for other extensions */
4870 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
4871 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
4872 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
4874 /* XXX boundary check against sa_len */
4875 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
4877 /* get a SA header */
4878 if ((sah = key_getsah(&saidx)) == NULL) {
4879 ipseclog((LOG_DEBUG, "key_update: no SA index found.\n"));
4880 return key_senderror(so, m, ENOENT);
4883 /* set spidx if there */
4885 error = key_setident(sah, m, mhp);
4887 return key_senderror(so, m, error);
4889 /* find a SA with sequence number. */
4890 #ifdef IPSEC_DOSEQCHECK
4891 if (mhp->msg->sadb_msg_seq != 0
4892 && (sav = key_getsavbyseq(sah, mhp->msg->sadb_msg_seq)) == NULL) {
4893 ipseclog((LOG_DEBUG,
4894 "key_update: no larval SA with sequence %u exists.\n",
4895 mhp->msg->sadb_msg_seq));
4896 return key_senderror(so, m, ENOENT);
4899 if ((sav = key_getsavbyspi(sah, sa0->sadb_sa_spi)) == NULL) {
4900 ipseclog((LOG_DEBUG,
4901 "key_update: no such a SA found (spi:%u)\n",
4902 (u_int32_t)ntohl(sa0->sadb_sa_spi)));
4903 return key_senderror(so, m, EINVAL);
4907 /* validity check */
4908 if (sav->sah->saidx.proto != proto) {
4909 ipseclog((LOG_DEBUG,
4910 "key_update: protocol mismatched (DB=%u param=%u)\n",
4911 sav->sah->saidx.proto, proto));
4912 return key_senderror(so, m, EINVAL);
4914 #ifdef IPSEC_DOSEQCHECK
4915 if (sav->spi != sa0->sadb_sa_spi) {
4916 ipseclog((LOG_DEBUG,
4917 "key_update: SPI mismatched (DB:%u param:%u)\n",
4918 (u_int32_t)ntohl(sav->spi),
4919 (u_int32_t)ntohl(sa0->sadb_sa_spi)));
4920 return key_senderror(so, m, EINVAL);
4923 if (sav->pid != mhp->msg->sadb_msg_pid) {
4924 ipseclog((LOG_DEBUG,
4925 "key_update: pid mismatched (DB:%u param:%u)\n",
4926 sav->pid, mhp->msg->sadb_msg_pid));
4927 return key_senderror(so, m, EINVAL);
4930 /* copy sav values */
4931 error = key_setsaval(sav, m, mhp);
4934 return key_senderror(so, m, error);
4937 /* check SA values to be mature. */
4938 if ((mhp->msg->sadb_msg_errno = key_mature(sav)) != 0) {
4940 return key_senderror(so, m, 0);
4946 /* set msg buf from mhp */
4947 n = key_getmsgbuf_x1(m, mhp);
4949 ipseclog((LOG_DEBUG, "key_update: No more memory.\n"));
4950 return key_senderror(so, m, ENOBUFS);
4954 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
4959 * search SAD with sequence for a SA which state is SADB_SASTATE_LARVAL.
4960 * only called by key_update().
4963 * others : found, pointer to a SA.
4965 #ifdef IPSEC_DOSEQCHECK
4966 static struct secasvar *
4967 key_getsavbyseq(sah, seq)
4968 struct secashead *sah;
4971 struct secasvar *sav;
4974 state = SADB_SASTATE_LARVAL;
4976 /* search SAD with sequence number ? */
4977 LIST_FOREACH(sav, &sah->savtree[state], chain) {
4979 KEY_CHKSASTATE(state, sav->state, "key_getsabyseq");
4981 if (sav->seq == seq) {
4983 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
4984 printf("DP key_getsavbyseq cause "
4985 "refcnt++:%d SA:%p\n",
4996 * SADB_ADD processing
4997 * add a entry to SA database, when received
4998 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4999 * key(AE), (identity(SD),) (sensitivity)>
5002 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
5003 * (identity(SD),) (sensitivity)>
5006 * IGNORE identity and sensitivity messages.
5008 * m will always be freed.
5014 const struct sadb_msghdr *mhp;
5016 struct sadb_sa *sa0;
5017 struct sadb_address *src0, *dst0;
5018 struct secasindex saidx;
5019 struct secashead *newsah;
5020 struct secasvar *newsav;
5027 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
5028 panic("key_add: NULL pointer is passed.\n");
5030 /* map satype to proto */
5031 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5032 ipseclog((LOG_DEBUG, "key_add: invalid satype is passed.\n"));
5033 return key_senderror(so, m, EINVAL);
5036 if (mhp->ext[SADB_EXT_SA] == NULL ||
5037 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5038 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
5039 (mhp->msg->sadb_msg_satype == SADB_SATYPE_ESP &&
5040 mhp->ext[SADB_EXT_KEY_ENCRYPT] == NULL) ||
5041 (mhp->msg->sadb_msg_satype == SADB_SATYPE_AH &&
5042 mhp->ext[SADB_EXT_KEY_AUTH] == NULL) ||
5043 (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL &&
5044 mhp->ext[SADB_EXT_LIFETIME_SOFT] == NULL) ||
5045 (mhp->ext[SADB_EXT_LIFETIME_HARD] == NULL &&
5046 mhp->ext[SADB_EXT_LIFETIME_SOFT] != NULL)) {
5047 ipseclog((LOG_DEBUG, "key_add: invalid message is passed.\n"));
5048 return key_senderror(so, m, EINVAL);
5050 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
5051 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5052 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5054 ipseclog((LOG_DEBUG, "key_add: invalid message is passed.\n"));
5055 return key_senderror(so, m, EINVAL);
5057 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
5058 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
5059 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
5061 mode = IPSEC_MODE_ANY;
5065 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5066 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
5067 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
5069 /* XXX boundary check against sa_len */
5070 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
5072 /* get a SA header */
5073 if ((newsah = key_getsah(&saidx)) == NULL) {
5074 /* create a new SA header */
5075 if ((newsah = key_newsah(&saidx)) == NULL) {
5076 ipseclog((LOG_DEBUG, "key_add: No more memory.\n"));
5077 return key_senderror(so, m, ENOBUFS);
5081 /* set spidx if there */
5083 error = key_setident(newsah, m, mhp);
5085 return key_senderror(so, m, error);
5088 /* create new SA entry. */
5089 /* We can create new SA only if SPI is differenct. */
5090 if (key_getsavbyspi(newsah, sa0->sadb_sa_spi)) {
5091 ipseclog((LOG_DEBUG, "key_add: SA already exists.\n"));
5092 return key_senderror(so, m, EEXIST);
5094 newsav = key_newsav(m, mhp, newsah, &error);
5095 if (newsav == NULL) {
5096 return key_senderror(so, m, error);
5099 /* check SA values to be mature. */
5100 if ((error = key_mature(newsav)) != 0) {
5101 key_freesav(newsav);
5102 return key_senderror(so, m, error);
5106 * don't call key_freesav() here, as we would like to keep the SA
5107 * in the database on success.
5113 /* set msg buf from mhp */
5114 n = key_getmsgbuf_x1(m, mhp);
5116 ipseclog((LOG_DEBUG, "key_update: No more memory.\n"));
5117 return key_senderror(so, m, ENOBUFS);
5121 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5127 key_setident(sah, m, mhp)
5128 struct secashead *sah;
5130 const struct sadb_msghdr *mhp;
5132 const struct sadb_ident *idsrc, *iddst;
5133 int idsrclen, iddstlen;
5136 if (sah == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
5137 panic("key_setident: NULL pointer is passed.\n");
5139 /* don't make buffer if not there */
5140 if (mhp->ext[SADB_EXT_IDENTITY_SRC] == NULL &&
5141 mhp->ext[SADB_EXT_IDENTITY_DST] == NULL) {
5147 if (mhp->ext[SADB_EXT_IDENTITY_SRC] == NULL ||
5148 mhp->ext[SADB_EXT_IDENTITY_DST] == NULL) {
5149 ipseclog((LOG_DEBUG, "key_setident: invalid identity.\n"));
5153 idsrc = (const struct sadb_ident *)mhp->ext[SADB_EXT_IDENTITY_SRC];
5154 iddst = (const struct sadb_ident *)mhp->ext[SADB_EXT_IDENTITY_DST];
5155 idsrclen = mhp->extlen[SADB_EXT_IDENTITY_SRC];
5156 iddstlen = mhp->extlen[SADB_EXT_IDENTITY_DST];
5158 /* validity check */
5159 if (idsrc->sadb_ident_type != iddst->sadb_ident_type) {
5160 ipseclog((LOG_DEBUG, "key_setident: ident type mismatch.\n"));
5164 switch (idsrc->sadb_ident_type) {
5165 case SADB_IDENTTYPE_PREFIX:
5166 case SADB_IDENTTYPE_FQDN:
5167 case SADB_IDENTTYPE_USERFQDN:
5169 /* XXX do nothing */
5175 /* make structure */
5176 KMALLOC(sah->idents, struct sadb_ident *, idsrclen);
5177 if (sah->idents == NULL) {
5178 ipseclog((LOG_DEBUG, "key_setident: No more memory.\n"));
5181 KMALLOC(sah->identd, struct sadb_ident *, iddstlen);
5182 if (sah->identd == NULL) {
5185 ipseclog((LOG_DEBUG, "key_setident: No more memory.\n"));
5188 bcopy(idsrc, sah->idents, idsrclen);
5189 bcopy(iddst, sah->identd, iddstlen);
5195 * m will not be freed on return.
5196 * it is caller's responsibility to free the result.
5198 static struct mbuf *
5199 key_getmsgbuf_x1(m, mhp)
5201 const struct sadb_msghdr *mhp;
5206 if (m == NULL || mhp == NULL || mhp->msg == NULL)
5207 panic("key_getmsgbuf_x1: NULL pointer is passed.\n");
5209 /* create new sadb_msg to reply. */
5210 n = key_gather_mbuf(m, mhp, 1, 9, SADB_EXT_RESERVED,
5211 SADB_EXT_SA, SADB_X_EXT_SA2,
5212 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST,
5213 SADB_EXT_LIFETIME_HARD, SADB_EXT_LIFETIME_SOFT,
5214 SADB_EXT_IDENTITY_SRC, SADB_EXT_IDENTITY_DST);
5218 if (n->m_len < sizeof(struct sadb_msg)) {
5219 n = m_pullup(n, sizeof(struct sadb_msg));
5223 mtod(n, struct sadb_msg *)->sadb_msg_errno = 0;
5224 mtod(n, struct sadb_msg *)->sadb_msg_len =
5225 PFKEY_UNIT64(n->m_pkthdr.len);
5230 static int key_delete_all (struct socket *, struct mbuf *,
5231 const struct sadb_msghdr *, u_int16_t);
5234 * SADB_DELETE processing
5236 * <base, SA(*), address(SD)>
5237 * from the ikmpd, and set SADB_SASTATE_DEAD,
5239 * <base, SA(*), address(SD)>
5242 * m will always be freed.
5245 key_delete(so, m, mhp)
5248 const struct sadb_msghdr *mhp;
5250 struct sadb_sa *sa0;
5251 struct sadb_address *src0, *dst0;
5252 struct secasindex saidx;
5253 struct secashead *sah;
5254 struct secasvar *sav = NULL;
5258 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
5259 panic("key_delete: NULL pointer is passed.\n");
5261 /* map satype to proto */
5262 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5263 ipseclog((LOG_DEBUG, "key_delete: invalid satype is passed.\n"));
5264 return key_senderror(so, m, EINVAL);
5267 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5268 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
5269 ipseclog((LOG_DEBUG, "key_delete: invalid message is passed.\n"));
5270 return key_senderror(so, m, EINVAL);
5273 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5274 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5275 ipseclog((LOG_DEBUG, "key_delete: invalid message is passed.\n"));
5276 return key_senderror(so, m, EINVAL);
5279 if (mhp->ext[SADB_EXT_SA] == NULL) {
5281 * Caller wants us to delete all non-LARVAL SAs
5282 * that match the src/dst. This is used during
5283 * IKE INITIAL-CONTACT.
5285 ipseclog((LOG_DEBUG, "key_delete: doing delete all.\n"));
5286 return key_delete_all(so, m, mhp, proto);
5287 } else if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa)) {
5288 ipseclog((LOG_DEBUG, "key_delete: invalid message is passed.\n"));
5289 return key_senderror(so, m, EINVAL);
5292 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5293 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
5294 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
5296 /* XXX boundary check against sa_len */
5297 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5299 /* get a SA header */
5300 LIST_FOREACH(sah, &sahtree, chain) {
5301 if (sah->state == SADB_SASTATE_DEAD)
5303 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5306 /* get a SA with SPI. */
5307 sav = key_getsavbyspi(sah, sa0->sadb_sa_spi);
5312 ipseclog((LOG_DEBUG, "key_delete: no SA found.\n"));
5313 return key_senderror(so, m, ENOENT);
5316 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
5322 struct sadb_msg *newmsg;
5324 /* create new sadb_msg to reply. */
5325 n = key_gather_mbuf(m, mhp, 1, 4, SADB_EXT_RESERVED,
5326 SADB_EXT_SA, SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
5328 return key_senderror(so, m, ENOBUFS);
5330 if (n->m_len < sizeof(struct sadb_msg)) {
5331 n = m_pullup(n, sizeof(struct sadb_msg));
5333 return key_senderror(so, m, ENOBUFS);
5335 newmsg = mtod(n, struct sadb_msg *);
5336 newmsg->sadb_msg_errno = 0;
5337 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
5340 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5345 * delete all SAs for src/dst. Called from key_delete().
5348 key_delete_all(so, m, mhp, proto)
5351 const struct sadb_msghdr *mhp;
5354 struct sadb_address *src0, *dst0;
5355 struct secasindex saidx;
5356 struct secashead *sah;
5357 struct secasvar *sav, *nextsav;
5358 u_int stateidx, state;
5360 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
5361 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
5363 /* XXX boundary check against sa_len */
5364 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5366 LIST_FOREACH(sah, &sahtree, chain) {
5367 if (sah->state == SADB_SASTATE_DEAD)
5369 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5372 /* Delete all non-LARVAL SAs. */
5374 stateidx < _ARRAYLEN(saorder_state_alive);
5376 state = saorder_state_alive[stateidx];
5377 if (state == SADB_SASTATE_LARVAL)
5379 for (sav = LIST_FIRST(&sah->savtree[state]);
5380 sav != NULL; sav = nextsav) {
5381 nextsav = LIST_NEXT(sav, chain);
5383 if (sav->state != state) {
5384 ipseclog((LOG_DEBUG, "key_delete_all: "
5385 "invalid sav->state "
5386 "(queue: %d SA: %d)\n",
5387 state, sav->state));
5391 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
5398 struct sadb_msg *newmsg;
5400 /* create new sadb_msg to reply. */
5401 n = key_gather_mbuf(m, mhp, 1, 3, SADB_EXT_RESERVED,
5402 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
5404 return key_senderror(so, m, ENOBUFS);
5406 if (n->m_len < sizeof(struct sadb_msg)) {
5407 n = m_pullup(n, sizeof(struct sadb_msg));
5409 return key_senderror(so, m, ENOBUFS);
5411 newmsg = mtod(n, struct sadb_msg *);
5412 newmsg->sadb_msg_errno = 0;
5413 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
5416 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5421 * SADB_GET processing
5423 * <base, SA(*), address(SD)>
5424 * from the ikmpd, and get a SP and a SA to respond,
5426 * <base, SA, (lifetime(HSC),) address(SD), (address(P),) key(AE),
5427 * (identity(SD),) (sensitivity)>
5430 * m will always be freed.
5436 const struct sadb_msghdr *mhp;
5438 struct sadb_sa *sa0;
5439 struct sadb_address *src0, *dst0;
5440 struct secasindex saidx;
5441 struct secashead *sah;
5442 struct secasvar *sav = NULL;
5446 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
5447 panic("key_get: NULL pointer is passed.\n");
5449 /* map satype to proto */
5450 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5451 ipseclog((LOG_DEBUG, "key_get: invalid satype is passed.\n"));
5452 return key_senderror(so, m, EINVAL);
5455 if (mhp->ext[SADB_EXT_SA] == NULL ||
5456 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5457 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
5458 ipseclog((LOG_DEBUG, "key_get: invalid message is passed.\n"));
5459 return key_senderror(so, m, EINVAL);
5461 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
5462 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5463 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5464 ipseclog((LOG_DEBUG, "key_get: invalid message is passed.\n"));
5465 return key_senderror(so, m, EINVAL);
5468 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5469 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
5470 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
5472 /* XXX boundary check against sa_len */
5473 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5475 /* get a SA header */
5476 LIST_FOREACH(sah, &sahtree, chain) {
5477 if (sah->state == SADB_SASTATE_DEAD)
5479 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5482 /* get a SA with SPI. */
5483 sav = key_getsavbyspi(sah, sa0->sadb_sa_spi);
5488 ipseclog((LOG_DEBUG, "key_get: no SA found.\n"));
5489 return key_senderror(so, m, ENOENT);
5496 /* map proto to satype */
5497 if ((satype = key_proto2satype(sah->saidx.proto)) == 0) {
5498 ipseclog((LOG_DEBUG, "key_get: there was invalid proto in SAD.\n"));
5499 return key_senderror(so, m, EINVAL);
5502 /* create new sadb_msg to reply. */
5503 n = key_setdumpsa(sav, SADB_GET, satype, mhp->msg->sadb_msg_seq,
5504 mhp->msg->sadb_msg_pid);
5506 return key_senderror(so, m, ENOBUFS);
5509 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
5513 /* XXX make it sysctl-configurable? */
5515 key_getcomb_setlifetime(comb)
5516 struct sadb_comb *comb;
5519 comb->sadb_comb_soft_allocations = 1;
5520 comb->sadb_comb_hard_allocations = 1;
5521 comb->sadb_comb_soft_bytes = 0;
5522 comb->sadb_comb_hard_bytes = 0;
5523 comb->sadb_comb_hard_addtime = 86400; /* 1 day */
5524 comb->sadb_comb_soft_addtime = comb->sadb_comb_soft_addtime * 80 / 100;
5525 comb->sadb_comb_soft_usetime = 28800; /* 8 hours */
5526 comb->sadb_comb_hard_usetime = comb->sadb_comb_hard_usetime * 80 / 100;
5531 * XXX reorder combinations by preference
5532 * XXX no idea if the user wants ESP authentication or not
5534 static struct mbuf *
5537 struct sadb_comb *comb;
5538 const struct esp_algorithm *algo;
5539 struct mbuf *result = NULL, *m, *n;
5543 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
5546 for (i = 1; i <= SADB_EALG_MAX; i++) {
5547 algo = esp_algorithm_lookup(i);
5551 if (algo->keymax < ipsec_esp_keymin)
5553 if (algo->keymin < ipsec_esp_keymin)
5554 encmin = ipsec_esp_keymin;
5556 encmin = algo->keymin;
5559 m = key_getcomb_ah();
5563 panic("assumption failed in key_getcomb_esp");
5565 MGET(m, MB_DONTWAIT, MT_DATA);
5570 bzero(mtod(m, caddr_t), m->m_len);
5577 for (n = m; n; n = n->m_next)
5581 panic("assumption failed in key_getcomb_esp");
5584 for (off = 0; off < totlen; off += l) {
5585 n = m_pulldown(m, off, l, &o);
5587 /* m is already freed */
5590 comb = (struct sadb_comb *)(mtod(n, caddr_t) + o);
5591 bzero(comb, sizeof(*comb));
5592 key_getcomb_setlifetime(comb);
5593 comb->sadb_comb_encrypt = i;
5594 comb->sadb_comb_encrypt_minbits = encmin;
5595 comb->sadb_comb_encrypt_maxbits = algo->keymax;
5614 * XXX reorder combinations by preference
5616 static struct mbuf *
5619 struct sadb_comb *comb;
5620 const struct ah_algorithm *algo;
5624 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
5627 for (i = 1; i <= SADB_AALG_MAX; i++) {
5629 /* we prefer HMAC algorithms, not old algorithms */
5630 if (i != SADB_AALG_SHA1HMAC && i != SADB_AALG_MD5HMAC)
5633 algo = ah_algorithm_lookup(i);
5637 if (algo->keymax < ipsec_ah_keymin)
5639 if (algo->keymin < ipsec_ah_keymin)
5640 min = ipsec_ah_keymin;
5647 panic("assumption failed in key_getcomb_ah");
5649 MGET(m, MB_DONTWAIT, MT_DATA);
5656 M_PREPEND(m, l, MB_DONTWAIT);
5660 comb = mtod(m, struct sadb_comb *);
5661 bzero(comb, sizeof(*comb));
5662 key_getcomb_setlifetime(comb);
5663 comb->sadb_comb_auth = i;
5664 comb->sadb_comb_auth_minbits = min;
5665 comb->sadb_comb_auth_maxbits = algo->keymax;
5672 * not really an official behavior. discussed in pf_key@inner.net in Sep2000.
5673 * XXX reorder combinations by preference
5675 static struct mbuf *
5676 key_getcomb_ipcomp()
5678 struct sadb_comb *comb;
5679 const struct ipcomp_algorithm *algo;
5682 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
5685 for (i = 1; i <= SADB_X_CALG_MAX; i++) {
5686 algo = ipcomp_algorithm_lookup(i);
5693 panic("assumption failed in key_getcomb_ipcomp");
5695 MGET(m, MB_DONTWAIT, MT_DATA);
5702 M_PREPEND(m, l, MB_DONTWAIT);
5706 comb = mtod(m, struct sadb_comb *);
5707 bzero(comb, sizeof(*comb));
5708 key_getcomb_setlifetime(comb);
5709 comb->sadb_comb_encrypt = i;
5710 /* what should we set into sadb_comb_*_{min,max}bits? */
5717 * XXX no way to pass mode (transport/tunnel) to userland
5718 * XXX replay checking?
5719 * XXX sysctl interface to ipsec_{ah,esp}_keymin
5721 static struct mbuf *
5723 const struct secasindex *saidx;
5725 struct sadb_prop *prop;
5727 const int l = PFKEY_ALIGN8(sizeof(struct sadb_prop));
5730 switch (saidx->proto) {
5733 m = key_getcomb_esp();
5737 m = key_getcomb_ah();
5739 case IPPROTO_IPCOMP:
5740 m = key_getcomb_ipcomp();
5748 M_PREPEND(m, l, MB_DONTWAIT);
5753 for (n = m; n; n = n->m_next)
5756 prop = mtod(m, struct sadb_prop *);
5757 bzero(prop, sizeof(*prop));
5758 prop->sadb_prop_len = PFKEY_UNIT64(totlen);
5759 prop->sadb_prop_exttype = SADB_EXT_PROPOSAL;
5760 prop->sadb_prop_replay = 32; /* XXX */
5766 * SADB_ACQUIRE processing called by key_checkrequest() and key_acquire2().
5768 * <base, SA, address(SD), (address(P)), x_policy,
5769 * (identity(SD),) (sensitivity,) proposal>
5770 * to KMD, and expect to receive
5771 * <base> with SADB_ACQUIRE if error occured,
5773 * <base, src address, dst address, (SPI range)> with SADB_GETSPI
5774 * from KMD by PF_KEY.
5776 * XXX x_policy is outside of RFC2367 (KAME extension).
5777 * XXX sensitivity is not supported.
5778 * XXX for ipcomp, RFC2367 does not define how to fill in proposal.
5779 * see comment for key_getcomb_ipcomp().
5783 * others: error number
5786 key_acquire(saidx, sp)
5787 struct secasindex *saidx;
5788 struct secpolicy *sp;
5790 struct mbuf *result = NULL, *m;
5791 #ifndef IPSEC_NONBLOCK_ACQUIRE
5792 struct secacq *newacq;
5800 panic("key_acquire: NULL pointer is passed.\n");
5801 if ((satype = key_proto2satype(saidx->proto)) == 0)
5802 panic("key_acquire: invalid proto is passed.\n");
5804 #ifndef IPSEC_NONBLOCK_ACQUIRE
5806 * We never do anything about acquirng SA. There is anather
5807 * solution that kernel blocks to send SADB_ACQUIRE message until
5808 * getting something message from IKEd. In later case, to be
5809 * managed with ACQUIRING list.
5811 /* get a entry to check whether sending message or not. */
5812 if ((newacq = key_getacq(saidx)) != NULL) {
5813 if (key_blockacq_count < newacq->count) {
5814 /* reset counter and do send message. */
5817 /* increment counter and do nothing. */
5822 /* make new entry for blocking to send SADB_ACQUIRE. */
5823 if ((newacq = key_newacq(saidx)) == NULL)
5826 /* add to acqtree */
5827 LIST_INSERT_HEAD(&acqtree, newacq, chain);
5832 #ifndef IPSEC_NONBLOCK_ACQUIRE
5835 seq = (acq_seq = (acq_seq == ~0 ? 1 : ++acq_seq));
5837 m = key_setsadbmsg(SADB_ACQUIRE, 0, satype, seq, 0, 0);
5844 /* set sadb_address for saidx's. */
5845 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
5846 (struct sockaddr *)&saidx->src, FULLMASK, IPSEC_ULPROTO_ANY);
5853 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
5854 (struct sockaddr *)&saidx->dst, FULLMASK, IPSEC_ULPROTO_ANY);
5861 /* XXX proxy address (optional) */
5863 /* set sadb_x_policy */
5865 m = key_setsadbxpolicy(sp->policy, sp->spidx.dir, sp->id);
5873 /* XXX identity (optional) */
5875 if (idexttype && fqdn) {
5876 /* create identity extension (FQDN) */
5877 struct sadb_ident *id;
5880 fqdnlen = strlen(fqdn) + 1; /* +1 for terminating-NUL */
5881 id = (struct sadb_ident *)p;
5882 bzero(id, sizeof(*id) + PFKEY_ALIGN8(fqdnlen));
5883 id->sadb_ident_len = PFKEY_UNIT64(sizeof(*id) + PFKEY_ALIGN8(fqdnlen));
5884 id->sadb_ident_exttype = idexttype;
5885 id->sadb_ident_type = SADB_IDENTTYPE_FQDN;
5886 bcopy(fqdn, id + 1, fqdnlen);
5887 p += sizeof(struct sadb_ident) + PFKEY_ALIGN8(fqdnlen);
5891 /* create identity extension (USERFQDN) */
5892 struct sadb_ident *id;
5896 /* +1 for terminating-NUL */
5897 userfqdnlen = strlen(userfqdn) + 1;
5900 id = (struct sadb_ident *)p;
5901 bzero(id, sizeof(*id) + PFKEY_ALIGN8(userfqdnlen));
5902 id->sadb_ident_len = PFKEY_UNIT64(sizeof(*id) + PFKEY_ALIGN8(userfqdnlen));
5903 id->sadb_ident_exttype = idexttype;
5904 id->sadb_ident_type = SADB_IDENTTYPE_USERFQDN;
5905 /* XXX is it correct? */
5906 if (curproc && curproc->p_cred)
5907 id->sadb_ident_id = curproc->p_cred->p_ruid;
5908 if (userfqdn && userfqdnlen)
5909 bcopy(userfqdn, id + 1, userfqdnlen);
5910 p += sizeof(struct sadb_ident) + PFKEY_ALIGN8(userfqdnlen);
5914 /* XXX sensitivity (optional) */
5916 /* create proposal/combination extension */
5917 m = key_getprop(saidx);
5920 * spec conformant: always attach proposal/combination extension,
5921 * the problem is that we have no way to attach it for ipcomp,
5922 * due to the way sadb_comb is declared in RFC2367.
5931 * outside of spec; make proposal/combination extension optional.
5937 if ((result->m_flags & M_PKTHDR) == 0) {
5942 if (result->m_len < sizeof(struct sadb_msg)) {
5943 result = m_pullup(result, sizeof(struct sadb_msg));
5944 if (result == NULL) {
5950 result->m_pkthdr.len = 0;
5951 for (m = result; m; m = m->m_next)
5952 result->m_pkthdr.len += m->m_len;
5954 mtod(result, struct sadb_msg *)->sadb_msg_len =
5955 PFKEY_UNIT64(result->m_pkthdr.len);
5957 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
5965 #ifndef IPSEC_NONBLOCK_ACQUIRE
5966 static struct secacq *
5968 struct secasindex *saidx;
5970 struct secacq *newacq;
5974 KMALLOC(newacq, struct secacq *, sizeof(struct secacq));
5975 if (newacq == NULL) {
5976 ipseclog((LOG_DEBUG, "key_newacq: No more memory.\n"));
5979 bzero(newacq, sizeof(*newacq));
5982 bcopy(saidx, &newacq->saidx, sizeof(newacq->saidx));
5983 newacq->seq = (acq_seq == ~0 ? 1 : ++acq_seq);
5985 newacq->created = tv.tv_sec;
5991 static struct secacq *
5993 struct secasindex *saidx;
5997 LIST_FOREACH(acq, &acqtree, chain) {
5998 if (key_cmpsaidx(saidx, &acq->saidx, CMP_EXACTLY))
6005 static struct secacq *
6006 key_getacqbyseq(seq)
6011 LIST_FOREACH(acq, &acqtree, chain) {
6012 if (acq->seq == seq)
6020 static struct secspacq *
6022 struct secpolicyindex *spidx;
6024 struct secspacq *acq;
6028 KMALLOC(acq, struct secspacq *, sizeof(struct secspacq));
6030 ipseclog((LOG_DEBUG, "key_newspacq: No more memory.\n"));
6033 bzero(acq, sizeof(*acq));
6036 bcopy(spidx, &acq->spidx, sizeof(acq->spidx));
6038 acq->created = tv.tv_sec;
6044 static struct secspacq *
6046 struct secpolicyindex *spidx;
6048 struct secspacq *acq;
6050 LIST_FOREACH(acq, &spacqtree, chain) {
6051 if (key_cmpspidx_exactly(spidx, &acq->spidx))
6059 * SADB_ACQUIRE processing,
6060 * in first situation, is receiving
6062 * from the ikmpd, and clear sequence of its secasvar entry.
6064 * In second situation, is receiving
6065 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
6066 * from a user land process, and return
6067 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
6070 * m will always be freed.
6073 key_acquire2(so, m, mhp)
6076 const struct sadb_msghdr *mhp;
6078 struct sadb_address *src0, *dst0;
6079 struct secasindex saidx;
6080 struct secashead *sah;
6085 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
6086 panic("key_acquire2: NULL pointer is passed.\n");
6089 * Error message from KMd.
6090 * We assume that if error was occured in IKEd, the length of PFKEY
6091 * message is equal to the size of sadb_msg structure.
6092 * We do not raise error even if error occured in this function.
6094 if (mhp->msg->sadb_msg_len == PFKEY_UNIT64(sizeof(struct sadb_msg))) {
6095 #ifndef IPSEC_NONBLOCK_ACQUIRE
6099 /* check sequence number */
6100 if (mhp->msg->sadb_msg_seq == 0) {
6101 ipseclog((LOG_DEBUG, "key_acquire2: must specify sequence number.\n"));
6106 if ((acq = key_getacqbyseq(mhp->msg->sadb_msg_seq)) == NULL) {
6108 * the specified larval SA is already gone, or we got
6109 * a bogus sequence number. we can silently ignore it.
6115 /* reset acq counter in order to deletion by timehander. */
6117 acq->created = tv.tv_sec;
6125 * This message is from user land.
6128 /* map satype to proto */
6129 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
6130 ipseclog((LOG_DEBUG, "key_acquire2: invalid satype is passed.\n"));
6131 return key_senderror(so, m, EINVAL);
6134 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
6135 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
6136 mhp->ext[SADB_EXT_PROPOSAL] == NULL) {
6138 ipseclog((LOG_DEBUG, "key_acquire2: invalid message is passed.\n"));
6139 return key_senderror(so, m, EINVAL);
6141 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
6142 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
6143 mhp->extlen[SADB_EXT_PROPOSAL] < sizeof(struct sadb_prop)) {
6145 ipseclog((LOG_DEBUG, "key_acquire2: invalid message is passed.\n"));
6146 return key_senderror(so, m, EINVAL);
6149 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
6150 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
6152 /* XXX boundary check against sa_len */
6153 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
6155 /* get a SA index */
6156 LIST_FOREACH(sah, &sahtree, chain) {
6157 if (sah->state == SADB_SASTATE_DEAD)
6159 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_MODE_REQID))
6163 ipseclog((LOG_DEBUG, "key_acquire2: a SA exists already.\n"));
6164 return key_senderror(so, m, EEXIST);
6167 error = key_acquire(&saidx, NULL);
6169 ipseclog((LOG_DEBUG, "key_acquire2: error %d returned "
6170 "from key_acquire.\n", mhp->msg->sadb_msg_errno));
6171 return key_senderror(so, m, error);
6174 return key_sendup_mbuf(so, m, KEY_SENDUP_REGISTERED);
6178 * SADB_REGISTER processing.
6179 * If SATYPE_UNSPEC has been passed as satype, only return sabd_supported.
6182 * from the ikmpd, and register a socket to send PF_KEY messages,
6186 * If socket is detached, must free from regnode.
6188 * m will always be freed.
6191 key_register(so, m, mhp)
6194 const struct sadb_msghdr *mhp;
6196 struct secreg *reg, *newreg = 0;
6199 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
6200 panic("key_register: NULL pointer is passed.\n");
6202 /* check for invalid register message */
6203 if (mhp->msg->sadb_msg_satype >= sizeof(regtree)/sizeof(regtree[0]))
6204 return key_senderror(so, m, EINVAL);
6206 /* When SATYPE_UNSPEC is specified, only return sabd_supported. */
6207 if (mhp->msg->sadb_msg_satype == SADB_SATYPE_UNSPEC)
6210 /* check whether existing or not */
6211 LIST_FOREACH(reg, ®tree[mhp->msg->sadb_msg_satype], chain) {
6212 if (reg->so == so) {
6213 ipseclog((LOG_DEBUG, "key_register: socket exists already.\n"));
6214 return key_senderror(so, m, EEXIST);
6218 /* create regnode */
6219 KMALLOC(newreg, struct secreg *, sizeof(*newreg));
6220 if (newreg == NULL) {
6221 ipseclog((LOG_DEBUG, "key_register: No more memory.\n"));
6222 return key_senderror(so, m, ENOBUFS);
6224 bzero((caddr_t)newreg, sizeof(*newreg));
6227 ((struct keycb *)sotorawcb(so))->kp_registered++;
6229 /* add regnode to regtree. */
6230 LIST_INSERT_HEAD(®tree[mhp->msg->sadb_msg_satype], newreg, chain);
6235 struct sadb_msg *newmsg;
6236 struct sadb_supported *sup;
6237 u_int len, alen, elen;
6240 struct sadb_alg *alg;
6242 /* create new sadb_msg to reply. */
6244 for (i = 1; i <= SADB_AALG_MAX; i++) {
6245 if (ah_algorithm_lookup(i))
6246 alen += sizeof(struct sadb_alg);
6249 alen += sizeof(struct sadb_supported);
6252 for (i = 1; i <= SADB_EALG_MAX; i++) {
6253 if (esp_algorithm_lookup(i))
6254 elen += sizeof(struct sadb_alg);
6257 elen += sizeof(struct sadb_supported);
6260 len = sizeof(struct sadb_msg) + alen + elen;
6263 return key_senderror(so, m, ENOBUFS);
6265 MGETHDR(n, MB_DONTWAIT, MT_DATA);
6267 MCLGET(n, MB_DONTWAIT);
6268 if ((n->m_flags & M_EXT) == 0) {
6274 return key_senderror(so, m, ENOBUFS);
6276 n->m_pkthdr.len = n->m_len = len;
6280 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
6281 newmsg = mtod(n, struct sadb_msg *);
6282 newmsg->sadb_msg_errno = 0;
6283 newmsg->sadb_msg_len = PFKEY_UNIT64(len);
6284 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
6286 /* for authentication algorithm */
6288 sup = (struct sadb_supported *)(mtod(n, caddr_t) + off);
6289 sup->sadb_supported_len = PFKEY_UNIT64(alen);
6290 sup->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
6291 off += PFKEY_ALIGN8(sizeof(*sup));
6293 for (i = 1; i <= SADB_AALG_MAX; i++) {
6294 const struct ah_algorithm *aalgo;
6296 aalgo = ah_algorithm_lookup(i);
6299 alg = (struct sadb_alg *)(mtod(n, caddr_t) + off);
6300 alg->sadb_alg_id = i;
6301 alg->sadb_alg_ivlen = 0;
6302 alg->sadb_alg_minbits = aalgo->keymin;
6303 alg->sadb_alg_maxbits = aalgo->keymax;
6304 off += PFKEY_ALIGN8(sizeof(*alg));
6309 /* for encryption algorithm */
6311 sup = (struct sadb_supported *)(mtod(n, caddr_t) + off);
6312 sup->sadb_supported_len = PFKEY_UNIT64(elen);
6313 sup->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT;
6314 off += PFKEY_ALIGN8(sizeof(*sup));
6316 for (i = 1; i <= SADB_EALG_MAX; i++) {
6317 const struct esp_algorithm *ealgo;
6319 ealgo = esp_algorithm_lookup(i);
6322 alg = (struct sadb_alg *)(mtod(n, caddr_t) + off);
6323 alg->sadb_alg_id = i;
6324 if (ealgo && ealgo->ivlen) {
6326 * give NULL to get the value preferred by
6327 * algorithm XXX SADB_X_EXT_DERIV ?
6329 alg->sadb_alg_ivlen =
6330 (*ealgo->ivlen)(ealgo, NULL);
6332 alg->sadb_alg_ivlen = 0;
6333 alg->sadb_alg_minbits = ealgo->keymin;
6334 alg->sadb_alg_maxbits = ealgo->keymax;
6335 off += PFKEY_ALIGN8(sizeof(struct sadb_alg));
6342 panic("length assumption failed in key_register");
6346 return key_sendup_mbuf(so, n, KEY_SENDUP_REGISTERED);
6351 * free secreg entry registered.
6352 * XXX: I want to do free a socket marked done SADB_RESIGER to socket.
6363 panic("key_freereg: NULL pointer is passed.\n");
6366 * check whether existing or not.
6367 * check all type of SA, because there is a potential that
6368 * one socket is registered to multiple type of SA.
6370 for (i = 0; i <= SADB_SATYPE_MAX; i++) {
6371 LIST_FOREACH(reg, ®tree[i], chain) {
6373 && __LIST_CHAINED(reg)) {
6374 LIST_REMOVE(reg, chain);
6385 * SADB_EXPIRE processing
6387 * <base, SA, SA2, lifetime(C and one of HS), address(SD)>
6389 * NOTE: We send only soft lifetime extension.
6392 * others : error number
6396 struct secasvar *sav;
6400 struct mbuf *result = NULL, *m;
6403 struct sadb_lifetime *lt;
6405 /* XXX: Why do we lock ? */
6406 s = splnet(); /*called from softclock()*/
6410 panic("key_expire: NULL pointer is passed.\n");
6411 if (sav->sah == NULL)
6412 panic("key_expire: Why was SA index in SA NULL.\n");
6413 if ((satype = key_proto2satype(sav->sah->saidx.proto)) == 0)
6414 panic("key_expire: invalid proto is passed.\n");
6416 /* set msg header */
6417 m = key_setsadbmsg(SADB_EXPIRE, 0, satype, sav->seq, 0, sav->refcnt);
6424 /* create SA extension */
6425 m = key_setsadbsa(sav);
6432 /* create SA extension */
6433 m = key_setsadbxsa2(sav->sah->saidx.mode,
6434 sav->replay ? sav->replay->count : 0,
6435 sav->sah->saidx.reqid);
6442 /* create lifetime extension (current and soft) */
6443 len = PFKEY_ALIGN8(sizeof(*lt)) * 2;
6444 m = key_alloc_mbuf(len);
6445 if (!m || m->m_next) { /*XXX*/
6451 bzero(mtod(m, caddr_t), len);
6452 lt = mtod(m, struct sadb_lifetime *);
6453 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
6454 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
6455 lt->sadb_lifetime_allocations = sav->lft_c->sadb_lifetime_allocations;
6456 lt->sadb_lifetime_bytes = sav->lft_c->sadb_lifetime_bytes;
6457 lt->sadb_lifetime_addtime = sav->lft_c->sadb_lifetime_addtime;
6458 lt->sadb_lifetime_usetime = sav->lft_c->sadb_lifetime_usetime;
6459 lt = (struct sadb_lifetime *)(mtod(m, caddr_t) + len / 2);
6460 bcopy(sav->lft_s, lt, sizeof(*lt));
6463 /* set sadb_address for source */
6464 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
6465 (struct sockaddr *)&sav->sah->saidx.src,
6466 FULLMASK, IPSEC_ULPROTO_ANY);
6473 /* set sadb_address for destination */
6474 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
6475 (struct sockaddr *)&sav->sah->saidx.dst,
6476 FULLMASK, IPSEC_ULPROTO_ANY);
6483 if ((result->m_flags & M_PKTHDR) == 0) {
6488 if (result->m_len < sizeof(struct sadb_msg)) {
6489 result = m_pullup(result, sizeof(struct sadb_msg));
6490 if (result == NULL) {
6496 result->m_pkthdr.len = 0;
6497 for (m = result; m; m = m->m_next)
6498 result->m_pkthdr.len += m->m_len;
6500 mtod(result, struct sadb_msg *)->sadb_msg_len =
6501 PFKEY_UNIT64(result->m_pkthdr.len);
6504 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
6514 * SADB_FLUSH processing
6517 * from the ikmpd, and free all entries in secastree.
6521 * NOTE: to do is only marking SADB_SASTATE_DEAD.
6523 * m will always be freed.
6526 key_flush(so, m, mhp)
6529 const struct sadb_msghdr *mhp;
6531 struct sadb_msg *newmsg;
6532 struct secashead *sah, *nextsah;
6533 struct secasvar *sav, *nextsav;
6539 if (so == NULL || mhp == NULL || mhp->msg == NULL)
6540 panic("key_flush: NULL pointer is passed.\n");
6542 /* map satype to proto */
6543 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
6544 ipseclog((LOG_DEBUG, "key_flush: invalid satype is passed.\n"));
6545 return key_senderror(so, m, EINVAL);
6548 /* no SATYPE specified, i.e. flushing all SA. */
6549 for (sah = LIST_FIRST(&sahtree);
6552 nextsah = LIST_NEXT(sah, chain);
6554 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
6555 && proto != sah->saidx.proto)
6559 stateidx < _ARRAYLEN(saorder_state_alive);
6561 state = saorder_state_any[stateidx];
6562 for (sav = LIST_FIRST(&sah->savtree[state]);
6566 nextsav = LIST_NEXT(sav, chain);
6568 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
6573 sah->state = SADB_SASTATE_DEAD;
6576 if (m->m_len < sizeof(struct sadb_msg) ||
6577 sizeof(struct sadb_msg) > m->m_len + M_TRAILINGSPACE(m)) {
6578 ipseclog((LOG_DEBUG, "key_flush: No more memory.\n"));
6579 return key_senderror(so, m, ENOBUFS);
6585 m->m_pkthdr.len = m->m_len = sizeof(struct sadb_msg);
6586 newmsg = mtod(m, struct sadb_msg *);
6587 newmsg->sadb_msg_errno = 0;
6588 newmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
6590 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
6594 * SADB_DUMP processing
6595 * dump all entries including status of DEAD in SAD.
6598 * from the ikmpd, and dump all secasvar leaves
6603 * m will always be freed.
6606 key_dump(so, m, mhp)
6609 const struct sadb_msghdr *mhp;
6611 struct secashead *sah;
6612 struct secasvar *sav;
6618 struct sadb_msg *newmsg;
6622 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
6623 panic("key_dump: NULL pointer is passed.\n");
6625 /* map satype to proto */
6626 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
6627 ipseclog((LOG_DEBUG, "key_dump: invalid satype is passed.\n"));
6628 return key_senderror(so, m, EINVAL);
6631 /* count sav entries to be sent to the userland. */
6633 LIST_FOREACH(sah, &sahtree, chain) {
6634 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
6635 && proto != sah->saidx.proto)
6639 stateidx < _ARRAYLEN(saorder_state_any);
6641 state = saorder_state_any[stateidx];
6642 LIST_FOREACH(sav, &sah->savtree[state], chain) {
6649 return key_senderror(so, m, ENOENT);
6651 /* send this to the userland, one at a time. */
6653 LIST_FOREACH(sah, &sahtree, chain) {
6654 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
6655 && proto != sah->saidx.proto)
6658 /* map proto to satype */
6659 if ((satype = key_proto2satype(sah->saidx.proto)) == 0) {
6660 ipseclog((LOG_DEBUG, "key_dump: there was invalid proto in SAD.\n"));
6661 return key_senderror(so, m, EINVAL);
6665 stateidx < _ARRAYLEN(saorder_state_any);
6667 state = saorder_state_any[stateidx];
6668 LIST_FOREACH(sav, &sah->savtree[state], chain) {
6669 n = key_setdumpsa(sav, SADB_DUMP, satype,
6670 --cnt, mhp->msg->sadb_msg_pid);
6672 return key_senderror(so, m, ENOBUFS);
6674 key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
6684 * SADB_X_PROMISC processing
6686 * m will always be freed.
6689 key_promisc(so, m, mhp)
6692 const struct sadb_msghdr *mhp;
6697 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
6698 panic("key_promisc: NULL pointer is passed.\n");
6700 olen = PFKEY_UNUNIT64(mhp->msg->sadb_msg_len);
6702 if (olen < sizeof(struct sadb_msg)) {
6704 return key_senderror(so, m, EINVAL);
6709 } else if (olen == sizeof(struct sadb_msg)) {
6710 /* enable/disable promisc mode */
6713 if ((kp = (struct keycb *)sotorawcb(so)) == NULL)
6714 return key_senderror(so, m, EINVAL);
6715 mhp->msg->sadb_msg_errno = 0;
6716 switch (mhp->msg->sadb_msg_satype) {
6719 kp->kp_promisc = mhp->msg->sadb_msg_satype;
6722 return key_senderror(so, m, EINVAL);
6725 /* send the original message back to everyone */
6726 mhp->msg->sadb_msg_errno = 0;
6727 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
6729 /* send packet as is */
6731 m_adj(m, PFKEY_ALIGN8(sizeof(struct sadb_msg)));
6733 /* TODO: if sadb_msg_seq is specified, send to specific pid */
6734 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
6738 static int (*key_typesw[]) (struct socket *, struct mbuf *,
6739 const struct sadb_msghdr *) = {
6740 NULL, /* SADB_RESERVED */
6741 key_getspi, /* SADB_GETSPI */
6742 key_update, /* SADB_UPDATE */
6743 key_add, /* SADB_ADD */
6744 key_delete, /* SADB_DELETE */
6745 key_get, /* SADB_GET */
6746 key_acquire2, /* SADB_ACQUIRE */
6747 key_register, /* SADB_REGISTER */
6748 NULL, /* SADB_EXPIRE */
6749 key_flush, /* SADB_FLUSH */
6750 key_dump, /* SADB_DUMP */
6751 key_promisc, /* SADB_X_PROMISC */
6752 NULL, /* SADB_X_PCHANGE */
6753 key_spdadd, /* SADB_X_SPDUPDATE */
6754 key_spdadd, /* SADB_X_SPDADD */
6755 key_spddelete, /* SADB_X_SPDDELETE */
6756 key_spdget, /* SADB_X_SPDGET */
6757 NULL, /* SADB_X_SPDACQUIRE */
6758 key_spddump, /* SADB_X_SPDDUMP */
6759 key_spdflush, /* SADB_X_SPDFLUSH */
6760 key_spdadd, /* SADB_X_SPDSETIDX */
6761 NULL, /* SADB_X_SPDEXPIRE */
6762 key_spddelete2, /* SADB_X_SPDDELETE2 */
6766 * parse sadb_msg buffer to process PFKEYv2,
6767 * and create a data to response if needed.
6768 * I think to be dealed with mbuf directly.
6770 * msgp : pointer to pointer to a received buffer pulluped.
6771 * This is rewrited to response.
6772 * so : pointer to socket.
6774 * length for buffer to send to user process.
6781 struct sadb_msg *msg;
6782 struct sadb_msghdr mh;
6788 if (m == NULL || so == NULL)
6789 panic("key_parse: NULL pointer is passed.\n");
6791 #if 0 /*kdebug_sadb assumes msg in linear buffer*/
6792 KEYDEBUG(KEYDEBUG_KEY_DUMP,
6793 ipseclog((LOG_DEBUG, "key_parse: passed sadb_msg\n"));
6797 if (m->m_len < sizeof(struct sadb_msg)) {
6798 m = m_pullup(m, sizeof(struct sadb_msg));
6802 msg = mtod(m, struct sadb_msg *);
6803 orglen = PFKEY_UNUNIT64(msg->sadb_msg_len);
6804 target = KEY_SENDUP_ONE;
6806 if ((m->m_flags & M_PKTHDR) == 0 ||
6807 m->m_pkthdr.len != m->m_pkthdr.len) {
6808 ipseclog((LOG_DEBUG, "key_parse: invalid message length.\n"));
6809 pfkeystat.out_invlen++;
6814 if (msg->sadb_msg_version != PF_KEY_V2) {
6815 ipseclog((LOG_DEBUG,
6816 "key_parse: PF_KEY version %u is mismatched.\n",
6817 msg->sadb_msg_version));
6818 pfkeystat.out_invver++;
6823 if (msg->sadb_msg_type > SADB_MAX) {
6824 ipseclog((LOG_DEBUG, "key_parse: invalid type %u is passed.\n",
6825 msg->sadb_msg_type));
6826 pfkeystat.out_invmsgtype++;
6831 /* for old-fashioned code - should be nuked */
6832 if (m->m_pkthdr.len > MCLBYTES) {
6839 MGETHDR(n, MB_DONTWAIT, MT_DATA);
6840 if (n && m->m_pkthdr.len > MHLEN) {
6841 MCLGET(n, MB_DONTWAIT);
6842 if ((n->m_flags & M_EXT) == 0) {
6851 m_copydata(m, 0, m->m_pkthdr.len, mtod(n, caddr_t));
6852 n->m_pkthdr.len = n->m_len = m->m_pkthdr.len;
6858 /* align the mbuf chain so that extensions are in contiguous region. */
6859 error = key_align(m, &mh);
6863 if (m->m_next) { /*XXX*/
6871 switch (msg->sadb_msg_satype) {
6872 case SADB_SATYPE_UNSPEC:
6873 switch (msg->sadb_msg_type) {
6881 ipseclog((LOG_DEBUG, "key_parse: must specify satype "
6882 "when msg type=%u.\n", msg->sadb_msg_type));
6883 pfkeystat.out_invsatype++;
6888 case SADB_SATYPE_AH:
6889 case SADB_SATYPE_ESP:
6890 case SADB_X_SATYPE_IPCOMP:
6891 switch (msg->sadb_msg_type) {
6893 case SADB_X_SPDDELETE:
6895 case SADB_X_SPDDUMP:
6896 case SADB_X_SPDFLUSH:
6897 case SADB_X_SPDSETIDX:
6898 case SADB_X_SPDUPDATE:
6899 case SADB_X_SPDDELETE2:
6900 ipseclog((LOG_DEBUG, "key_parse: illegal satype=%u\n",
6901 msg->sadb_msg_type));
6902 pfkeystat.out_invsatype++;
6907 case SADB_SATYPE_RSVP:
6908 case SADB_SATYPE_OSPFV2:
6909 case SADB_SATYPE_RIPV2:
6910 case SADB_SATYPE_MIP:
6911 ipseclog((LOG_DEBUG, "key_parse: type %u isn't supported.\n",
6912 msg->sadb_msg_satype));
6913 pfkeystat.out_invsatype++;
6916 case 1: /* XXX: What does it do? */
6917 if (msg->sadb_msg_type == SADB_X_PROMISC)
6921 ipseclog((LOG_DEBUG, "key_parse: invalid type %u is passed.\n",
6922 msg->sadb_msg_satype));
6923 pfkeystat.out_invsatype++;
6928 /* check field of upper layer protocol and address family */
6929 if (mh.ext[SADB_EXT_ADDRESS_SRC] != NULL
6930 && mh.ext[SADB_EXT_ADDRESS_DST] != NULL) {
6931 struct sadb_address *src0, *dst0;
6934 src0 = (struct sadb_address *)(mh.ext[SADB_EXT_ADDRESS_SRC]);
6935 dst0 = (struct sadb_address *)(mh.ext[SADB_EXT_ADDRESS_DST]);
6937 /* check upper layer protocol */
6938 if (src0->sadb_address_proto != dst0->sadb_address_proto) {
6939 ipseclog((LOG_DEBUG, "key_parse: upper layer protocol mismatched.\n"));
6940 pfkeystat.out_invaddr++;
6946 if (PFKEY_ADDR_SADDR(src0)->sa_family !=
6947 PFKEY_ADDR_SADDR(dst0)->sa_family) {
6948 ipseclog((LOG_DEBUG, "key_parse: address family mismatched.\n"));
6949 pfkeystat.out_invaddr++;
6953 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
6954 PFKEY_ADDR_SADDR(dst0)->sa_len) {
6955 ipseclog((LOG_DEBUG,
6956 "key_parse: address struct size mismatched.\n"));
6957 pfkeystat.out_invaddr++;
6962 switch (PFKEY_ADDR_SADDR(src0)->sa_family) {
6964 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
6965 sizeof(struct sockaddr_in)) {
6966 pfkeystat.out_invaddr++;
6972 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
6973 sizeof(struct sockaddr_in6)) {
6974 pfkeystat.out_invaddr++;
6980 ipseclog((LOG_DEBUG,
6981 "key_parse: unsupported address family.\n"));
6982 pfkeystat.out_invaddr++;
6983 error = EAFNOSUPPORT;
6987 switch (PFKEY_ADDR_SADDR(src0)->sa_family) {
6989 plen = sizeof(struct in_addr) << 3;
6992 plen = sizeof(struct in6_addr) << 3;
6995 plen = 0; /*fool gcc*/
6999 /* check max prefix length */
7000 if (src0->sadb_address_prefixlen > plen ||
7001 dst0->sadb_address_prefixlen > plen) {
7002 ipseclog((LOG_DEBUG,
7003 "key_parse: illegal prefixlen.\n"));
7004 pfkeystat.out_invaddr++;
7010 * prefixlen == 0 is valid because there can be a case when
7011 * all addresses are matched.
7015 if (msg->sadb_msg_type >= sizeof(key_typesw)/sizeof(key_typesw[0]) ||
7016 key_typesw[msg->sadb_msg_type] == NULL) {
7017 pfkeystat.out_invmsgtype++;
7022 return (*key_typesw[msg->sadb_msg_type])(so, m, &mh);
7025 msg->sadb_msg_errno = error;
7026 return key_sendup_mbuf(so, m, target);
7030 key_senderror(so, m, code)
7035 struct sadb_msg *msg;
7037 if (m->m_len < sizeof(struct sadb_msg))
7038 panic("invalid mbuf passed to key_senderror");
7040 msg = mtod(m, struct sadb_msg *);
7041 msg->sadb_msg_errno = code;
7042 return key_sendup_mbuf(so, m, KEY_SENDUP_ONE);
7046 * set the pointer to each header into message buffer.
7047 * m will be freed on error.
7048 * XXX larger-than-MCLBYTES extension?
7053 struct sadb_msghdr *mhp;
7056 struct sadb_ext *ext;
7062 if (m == NULL || mhp == NULL)
7063 panic("key_align: NULL pointer is passed.\n");
7064 if (m->m_len < sizeof(struct sadb_msg))
7065 panic("invalid mbuf passed to key_align");
7068 bzero(mhp, sizeof(*mhp));
7070 mhp->msg = mtod(m, struct sadb_msg *);
7071 mhp->ext[0] = (struct sadb_ext *)mhp->msg; /*XXX backward compat */
7073 end = PFKEY_UNUNIT64(mhp->msg->sadb_msg_len);
7074 extlen = end; /*just in case extlen is not updated*/
7075 for (off = sizeof(struct sadb_msg); off < end; off += extlen) {
7076 n = m_pulldown(m, off, sizeof(struct sadb_ext), &toff);
7078 /* m is already freed */
7081 ext = (struct sadb_ext *)(mtod(n, caddr_t) + toff);
7084 switch (ext->sadb_ext_type) {
7086 case SADB_EXT_ADDRESS_SRC:
7087 case SADB_EXT_ADDRESS_DST:
7088 case SADB_EXT_ADDRESS_PROXY:
7089 case SADB_EXT_LIFETIME_CURRENT:
7090 case SADB_EXT_LIFETIME_HARD:
7091 case SADB_EXT_LIFETIME_SOFT:
7092 case SADB_EXT_KEY_AUTH:
7093 case SADB_EXT_KEY_ENCRYPT:
7094 case SADB_EXT_IDENTITY_SRC:
7095 case SADB_EXT_IDENTITY_DST:
7096 case SADB_EXT_SENSITIVITY:
7097 case SADB_EXT_PROPOSAL:
7098 case SADB_EXT_SUPPORTED_AUTH:
7099 case SADB_EXT_SUPPORTED_ENCRYPT:
7100 case SADB_EXT_SPIRANGE:
7101 case SADB_X_EXT_POLICY:
7102 case SADB_X_EXT_SA2:
7103 /* duplicate check */
7105 * XXX Are there duplication payloads of either
7106 * KEY_AUTH or KEY_ENCRYPT ?
7108 if (mhp->ext[ext->sadb_ext_type] != NULL) {
7109 ipseclog((LOG_DEBUG,
7110 "key_align: duplicate ext_type %u "
7111 "is passed.\n", ext->sadb_ext_type));
7113 pfkeystat.out_dupext++;
7118 ipseclog((LOG_DEBUG,
7119 "key_align: invalid ext_type %u is passed.\n",
7120 ext->sadb_ext_type));
7122 pfkeystat.out_invexttype++;
7126 extlen = PFKEY_UNUNIT64(ext->sadb_ext_len);
7128 if (key_validate_ext(ext, extlen)) {
7130 pfkeystat.out_invlen++;
7134 n = m_pulldown(m, off, extlen, &toff);
7136 /* m is already freed */
7139 ext = (struct sadb_ext *)(mtod(n, caddr_t) + toff);
7141 mhp->ext[ext->sadb_ext_type] = ext;
7142 mhp->extoff[ext->sadb_ext_type] = off;
7143 mhp->extlen[ext->sadb_ext_type] = extlen;
7148 pfkeystat.out_invlen++;
7156 key_validate_ext(ext, len)
7157 const struct sadb_ext *ext;
7160 const struct sockaddr *sa;
7161 enum { NONE, ADDR } checktype = NONE;
7163 const int sal = offsetof(struct sockaddr, sa_len) + sizeof(sa->sa_len);
7165 if (len != PFKEY_UNUNIT64(ext->sadb_ext_len))
7168 /* if it does not match minimum/maximum length, bail */
7169 if (ext->sadb_ext_type >= sizeof(minsize) / sizeof(minsize[0]) ||
7170 ext->sadb_ext_type >= sizeof(maxsize) / sizeof(maxsize[0]))
7172 if (!minsize[ext->sadb_ext_type] || len < minsize[ext->sadb_ext_type])
7174 if (maxsize[ext->sadb_ext_type] && len > maxsize[ext->sadb_ext_type])
7177 /* more checks based on sadb_ext_type XXX need more */
7178 switch (ext->sadb_ext_type) {
7179 case SADB_EXT_ADDRESS_SRC:
7180 case SADB_EXT_ADDRESS_DST:
7181 case SADB_EXT_ADDRESS_PROXY:
7182 baselen = PFKEY_ALIGN8(sizeof(struct sadb_address));
7185 case SADB_EXT_IDENTITY_SRC:
7186 case SADB_EXT_IDENTITY_DST:
7187 if (((const struct sadb_ident *)ext)->sadb_ident_type ==
7188 SADB_X_IDENTTYPE_ADDR) {
7189 baselen = PFKEY_ALIGN8(sizeof(struct sadb_ident));
7199 switch (checktype) {
7203 sa = (const struct sockaddr *)((c_caddr_t)ext + baselen);
7204 if (len < baselen + sal)
7206 if (baselen + PFKEY_ALIGN8(sa->sa_len) != len)
7219 bzero((caddr_t)&key_cb, sizeof(key_cb));
7221 for (i = 0; i < IPSEC_DIR_MAX; i++) {
7222 LIST_INIT(&sptree[i]);
7225 LIST_INIT(&sahtree);
7227 for (i = 0; i <= SADB_SATYPE_MAX; i++) {
7228 LIST_INIT(®tree[i]);
7231 #ifndef IPSEC_NONBLOCK_ACQUIRE
7232 LIST_INIT(&acqtree);
7234 LIST_INIT(&spacqtree);
7236 /* system default */
7238 ip4_def_policy.policy = IPSEC_POLICY_NONE;
7239 ip4_def_policy.refcnt++; /*never reclaim this*/
7242 ip6_def_policy.policy = IPSEC_POLICY_NONE;
7243 ip6_def_policy.refcnt++; /*never reclaim this*/
7246 #ifndef IPSEC_DEBUG2
7247 timeout((void *)key_timehandler, (void *)0, hz);
7248 #endif /*IPSEC_DEBUG2*/
7250 /* initialize key statistics */
7251 keystat.getspi_count = 1;
7253 printf("IPsec: Initialized Security Association Processing.\n");
7259 * XXX: maybe This function is called after INBOUND IPsec processing.
7261 * Special check for tunnel-mode packets.
7262 * We must make some checks for consistency between inner and outer IP header.
7264 * xxx more checks to be provided
7267 key_checktunnelsanity(sav, family, src, dst)
7268 struct secasvar *sav;
7274 if (sav->sah == NULL)
7275 panic("sav->sah == NULL at key_checktunnelsanity");
7277 /* XXX: check inner IP header */
7283 #define hostnamelen strlen(hostname)
7286 * Get FQDN for the host.
7287 * If the administrator configured hostname (by hostname(1)) without
7288 * domain name, returns nothing.
7295 static char fqdn[MAXHOSTNAMELEN + 1];
7300 /* check if it comes with domain name. */
7302 for (i = 0; i < hostnamelen; i++) {
7303 if (hostname[i] == '.')
7309 /* NOTE: hostname may not be NUL-terminated. */
7310 bzero(fqdn, sizeof(fqdn));
7311 bcopy(hostname, fqdn, hostnamelen);
7312 fqdn[hostnamelen] = '\0';
7317 * get username@FQDN for the host/user.
7323 static char userfqdn[MAXHOSTNAMELEN + MAXLOGNAME + 2];
7324 struct proc *p = curproc;
7327 if (!p || !p->p_pgrp || !p->p_pgrp->pg_session)
7329 if (!(host = key_getfqdn()))
7332 /* NOTE: s_login may not be-NUL terminated. */
7333 bzero(userfqdn, sizeof(userfqdn));
7334 bcopy(p->p_pgrp->pg_session->s_login, userfqdn, MAXLOGNAME);
7335 userfqdn[MAXLOGNAME] = '\0'; /* safeguard */
7336 q = userfqdn + strlen(userfqdn);
7338 bcopy(host, q, strlen(host));
7346 /* record data transfer on SA, and update timestamps */
7348 key_sa_recordxfer(sav, m)
7349 struct secasvar *sav;
7353 panic("key_sa_recordxfer called with sav == NULL");
7355 panic("key_sa_recordxfer called with m == NULL");
7360 * XXX Currently, there is a difference of bytes size
7361 * between inbound and outbound processing.
7363 sav->lft_c->sadb_lifetime_bytes += m->m_pkthdr.len;
7364 /* to check bytes lifetime is done in key_timehandler(). */
7367 * We use the number of packets as the unit of
7368 * sadb_lifetime_allocations. We increment the variable
7369 * whenever {esp,ah}_{in,out}put is called.
7371 sav->lft_c->sadb_lifetime_allocations++;
7372 /* XXX check for expires? */
7375 * NOTE: We record CURRENT sadb_lifetime_usetime by using wall clock,
7376 * in seconds. HARD and SOFT lifetime are measured by the time
7377 * difference (again in seconds) from sadb_lifetime_usetime.
7381 * -----+-----+--------+---> t
7382 * <--------------> HARD
7388 sav->lft_c->sadb_lifetime_usetime = tv.tv_sec;
7389 /* XXX check for expires? */
7397 key_sa_routechange(dst)
7398 struct sockaddr *dst;
7400 struct secashead *sah;
7403 LIST_FOREACH(sah, &sahtree, chain) {
7404 ro = &sah->sa_route;
7405 if (ro->ro_rt && dst->sa_len == ro->ro_dst.sa_len
7406 && bcmp(dst, &ro->ro_dst, dst->sa_len) == 0) {
7408 ro->ro_rt = (struct rtentry *)NULL;
7416 key_sa_chgstate(sav, state)
7417 struct secasvar *sav;
7421 panic("key_sa_chgstate called with sav == NULL");
7423 if (sav->state == state)
7426 if (__LIST_CHAINED(sav))
7427 LIST_REMOVE(sav, chain);
7430 LIST_INSERT_HEAD(&sav->sah->savtree[state], sav, chain);
7435 struct secasvar *sav;
7439 panic("key_sa_stir_iv called with sav == NULL");
7440 key_randomfill(sav->iv, sav->ivlen);
7444 static struct mbuf *
7448 struct mbuf *m = NULL, *n;
7453 MGET(n, MB_DONTWAIT, MT_DATA);
7454 if (n && len > MLEN)
7455 MCLGET(n, MB_DONTWAIT);
7463 n->m_len = M_TRAILINGSPACE(n);
7464 /* use the bottom of mbuf, hoping we can prepend afterwards */
7465 if (n->m_len > len) {
7466 t = (n->m_len - len) & ~(sizeof(long) - 1);