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.4 2003/08/23 10:06:24 rob 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_NOWAIT))
310 free((caddr_t)(p), M_SECA);
312 #define KMALLOC(p, t, n) \
314 ((p) = (t)malloc((unsigned long)(n), M_SECA, M_NOWAIT)); \
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 *
1628 key_gather_mbuf(struct mbuf *m, const struct sadb_msghdr *mhp,
1629 int ndeep, int nitem, ...)
1631 key_gather_mbuf(m, mhp, ndeep, nitem, va_alist)
1633 const struct sadb_msghdr *mhp;
1642 struct mbuf *result = NULL, *n;
1645 if (m == NULL || mhp == NULL)
1646 panic("null pointer passed to key_gather");
1648 va_start(ap, nitem);
1649 for (i = 0; i < nitem; i++) {
1650 idx = va_arg(ap, int);
1651 if (idx < 0 || idx > SADB_EXT_MAX)
1653 /* don't attempt to pull empty extension */
1654 if (idx == SADB_EXT_RESERVED && mhp->msg == NULL)
1656 if (idx != SADB_EXT_RESERVED &&
1657 (mhp->ext[idx] == NULL || mhp->extlen[idx] == 0))
1660 if (idx == SADB_EXT_RESERVED) {
1661 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
1664 panic("assumption failed");
1666 MGETHDR(n, M_DONTWAIT, MT_DATA);
1671 m_copydata(m, 0, sizeof(struct sadb_msg),
1673 } else if (i < ndeep) {
1674 len = mhp->extlen[idx];
1675 n = key_alloc_mbuf(len);
1676 if (!n || n->m_next) { /*XXX*/
1681 m_copydata(m, mhp->extoff[idx], mhp->extlen[idx],
1684 n = m_copym(m, mhp->extoff[idx], mhp->extlen[idx],
1697 if ((result->m_flags & M_PKTHDR) != 0) {
1698 result->m_pkthdr.len = 0;
1699 for (n = result; n; n = n->m_next)
1700 result->m_pkthdr.len += n->m_len;
1711 * SADB_X_SPDADD, SADB_X_SPDSETIDX or SADB_X_SPDUPDATE processing
1712 * add a entry to SP database, when received
1713 * <base, address(SD), (lifetime(H),) policy>
1715 * Adding to SP database,
1717 * <base, address(SD), (lifetime(H),) policy>
1718 * to the socket which was send.
1720 * SPDADD set a unique policy entry.
1721 * SPDSETIDX like SPDADD without a part of policy requests.
1722 * SPDUPDATE replace a unique policy entry.
1724 * m will always be freed.
1727 key_spdadd(so, m, mhp)
1730 const struct sadb_msghdr *mhp;
1732 struct sadb_address *src0, *dst0;
1733 struct sadb_x_policy *xpl0, *xpl;
1734 struct sadb_lifetime *lft = NULL;
1735 struct secpolicyindex spidx;
1736 struct secpolicy *newsp;
1741 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
1742 panic("key_spdadd: NULL pointer is passed.\n");
1744 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
1745 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
1746 mhp->ext[SADB_X_EXT_POLICY] == NULL) {
1747 ipseclog((LOG_DEBUG, "key_spdadd: invalid message is passed.\n"));
1748 return key_senderror(so, m, EINVAL);
1750 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
1751 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
1752 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
1753 ipseclog((LOG_DEBUG, "key_spdadd: invalid message is passed.\n"));
1754 return key_senderror(so, m, EINVAL);
1756 if (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL) {
1757 if (mhp->extlen[SADB_EXT_LIFETIME_HARD]
1758 < sizeof(struct sadb_lifetime)) {
1759 ipseclog((LOG_DEBUG, "key_spdadd: invalid message is passed.\n"));
1760 return key_senderror(so, m, EINVAL);
1762 lft = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_HARD];
1765 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
1766 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
1767 xpl0 = (struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY];
1770 /* XXX boundary check against sa_len */
1771 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
1774 src0->sadb_address_prefixlen,
1775 dst0->sadb_address_prefixlen,
1776 src0->sadb_address_proto,
1779 /* checking the direciton. */
1780 switch (xpl0->sadb_x_policy_dir) {
1781 case IPSEC_DIR_INBOUND:
1782 case IPSEC_DIR_OUTBOUND:
1785 ipseclog((LOG_DEBUG, "key_spdadd: Invalid SP direction.\n"));
1786 mhp->msg->sadb_msg_errno = EINVAL;
1791 /* key_spdadd() accepts DISCARD, NONE and IPSEC. */
1792 if (xpl0->sadb_x_policy_type == IPSEC_POLICY_ENTRUST
1793 || xpl0->sadb_x_policy_type == IPSEC_POLICY_BYPASS) {
1794 ipseclog((LOG_DEBUG, "key_spdadd: Invalid policy type.\n"));
1795 return key_senderror(so, m, EINVAL);
1798 /* policy requests are mandatory when action is ipsec. */
1799 if (mhp->msg->sadb_msg_type != SADB_X_SPDSETIDX
1800 && xpl0->sadb_x_policy_type == IPSEC_POLICY_IPSEC
1801 && mhp->extlen[SADB_X_EXT_POLICY] <= sizeof(*xpl0)) {
1802 ipseclog((LOG_DEBUG, "key_spdadd: some policy requests part required.\n"));
1803 return key_senderror(so, m, EINVAL);
1807 * checking there is SP already or not.
1808 * SPDUPDATE doesn't depend on whether there is a SP or not.
1809 * If the type is either SPDADD or SPDSETIDX AND a SP is found,
1812 newsp = key_getsp(&spidx);
1813 if (mhp->msg->sadb_msg_type == SADB_X_SPDUPDATE) {
1815 newsp->state = IPSEC_SPSTATE_DEAD;
1819 if (newsp != NULL) {
1821 ipseclog((LOG_DEBUG, "key_spdadd: a SP entry exists already.\n"));
1822 return key_senderror(so, m, EEXIST);
1826 /* allocation new SP entry */
1827 if ((newsp = key_msg2sp(xpl0, PFKEY_EXTLEN(xpl0), &error)) == NULL) {
1828 return key_senderror(so, m, error);
1831 if ((newsp->id = key_getnewspid()) == 0) {
1832 keydb_delsecpolicy(newsp);
1833 return key_senderror(so, m, ENOBUFS);
1836 /* XXX boundary check against sa_len */
1837 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
1840 src0->sadb_address_prefixlen,
1841 dst0->sadb_address_prefixlen,
1842 src0->sadb_address_proto,
1845 /* sanity check on addr pair */
1846 if (((struct sockaddr *)(src0 + 1))->sa_family !=
1847 ((struct sockaddr *)(dst0+ 1))->sa_family) {
1848 keydb_delsecpolicy(newsp);
1849 return key_senderror(so, m, EINVAL);
1851 if (((struct sockaddr *)(src0 + 1))->sa_len !=
1852 ((struct sockaddr *)(dst0+ 1))->sa_len) {
1853 keydb_delsecpolicy(newsp);
1854 return key_senderror(so, m, EINVAL);
1857 if (newsp->req && newsp->req->saidx.src.ss_family) {
1858 struct sockaddr *sa;
1859 sa = (struct sockaddr *)(src0 + 1);
1860 if (sa->sa_family != newsp->req->saidx.src.ss_family) {
1861 keydb_delsecpolicy(newsp);
1862 return key_senderror(so, m, EINVAL);
1865 if (newsp->req && newsp->req->saidx.dst.ss_family) {
1866 struct sockaddr *sa;
1867 sa = (struct sockaddr *)(dst0 + 1);
1868 if (sa->sa_family != newsp->req->saidx.dst.ss_family) {
1869 keydb_delsecpolicy(newsp);
1870 return key_senderror(so, m, EINVAL);
1876 newsp->created = tv.tv_sec;
1877 newsp->lastused = tv.tv_sec;
1878 newsp->lifetime = lft ? lft->sadb_lifetime_addtime : 0;
1879 newsp->validtime = lft ? lft->sadb_lifetime_usetime : 0;
1881 newsp->refcnt = 1; /* do not reclaim until I say I do */
1882 newsp->state = IPSEC_SPSTATE_ALIVE;
1883 LIST_INSERT_TAIL(&sptree[newsp->spidx.dir], newsp, secpolicy, chain);
1885 /* delete the entry in spacqtree */
1886 if (mhp->msg->sadb_msg_type == SADB_X_SPDUPDATE) {
1887 struct secspacq *spacq;
1888 if ((spacq = key_getspacq(&spidx)) != NULL) {
1889 /* reset counter in order to deletion by timehandler. */
1891 spacq->created = tv.tv_sec;
1897 struct mbuf *n, *mpolicy;
1898 struct sadb_msg *newmsg;
1901 /* create new sadb_msg to reply. */
1903 n = key_gather_mbuf(m, mhp, 2, 5, SADB_EXT_RESERVED,
1904 SADB_X_EXT_POLICY, SADB_EXT_LIFETIME_HARD,
1905 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
1907 n = key_gather_mbuf(m, mhp, 2, 4, SADB_EXT_RESERVED,
1909 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
1912 return key_senderror(so, m, ENOBUFS);
1914 if (n->m_len < sizeof(*newmsg)) {
1915 n = m_pullup(n, sizeof(*newmsg));
1917 return key_senderror(so, m, ENOBUFS);
1919 newmsg = mtod(n, struct sadb_msg *);
1920 newmsg->sadb_msg_errno = 0;
1921 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
1924 mpolicy = m_pulldown(n, PFKEY_ALIGN8(sizeof(struct sadb_msg)),
1925 sizeof(*xpl), &off);
1926 if (mpolicy == NULL) {
1927 /* n is already freed */
1928 return key_senderror(so, m, ENOBUFS);
1930 xpl = (struct sadb_x_policy *)(mtod(mpolicy, caddr_t) + off);
1931 if (xpl->sadb_x_policy_exttype != SADB_X_EXT_POLICY) {
1933 return key_senderror(so, m, EINVAL);
1935 xpl->sadb_x_policy_id = newsp->id;
1938 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
1943 * get new policy id.
1951 u_int32_t newid = 0;
1952 int count = key_spi_trycnt; /* XXX */
1953 struct secpolicy *sp;
1955 /* when requesting to allocate spi ranged */
1957 newid = (policy_id = (policy_id == ~0 ? 1 : policy_id + 1));
1959 if ((sp = key_getspbyid(newid)) == NULL)
1965 if (count == 0 || newid == 0) {
1966 ipseclog((LOG_DEBUG, "key_getnewspid: to allocate policy id is failed.\n"));
1974 * SADB_SPDDELETE processing
1976 * <base, address(SD), policy(*)>
1977 * from the user(?), and set SADB_SASTATE_DEAD,
1979 * <base, address(SD), policy(*)>
1981 * policy(*) including direction of policy.
1983 * m will always be freed.
1986 key_spddelete(so, m, mhp)
1989 const struct sadb_msghdr *mhp;
1991 struct sadb_address *src0, *dst0;
1992 struct sadb_x_policy *xpl0;
1993 struct secpolicyindex spidx;
1994 struct secpolicy *sp;
1997 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
1998 panic("key_spddelete: NULL pointer is passed.\n");
2000 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
2001 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
2002 mhp->ext[SADB_X_EXT_POLICY] == NULL) {
2003 ipseclog((LOG_DEBUG, "key_spddelete: invalid message is passed.\n"));
2004 return key_senderror(so, m, EINVAL);
2006 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
2007 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
2008 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2009 ipseclog((LOG_DEBUG, "key_spddelete: invalid message is passed.\n"));
2010 return key_senderror(so, m, EINVAL);
2013 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
2014 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
2015 xpl0 = (struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY];
2018 /* XXX boundary check against sa_len */
2019 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
2022 src0->sadb_address_prefixlen,
2023 dst0->sadb_address_prefixlen,
2024 src0->sadb_address_proto,
2027 /* checking the direciton. */
2028 switch (xpl0->sadb_x_policy_dir) {
2029 case IPSEC_DIR_INBOUND:
2030 case IPSEC_DIR_OUTBOUND:
2033 ipseclog((LOG_DEBUG, "key_spddelete: Invalid SP direction.\n"));
2034 return key_senderror(so, m, EINVAL);
2037 /* Is there SP in SPD ? */
2038 if ((sp = key_getsp(&spidx)) == NULL) {
2039 ipseclog((LOG_DEBUG, "key_spddelete: no SP found.\n"));
2040 return key_senderror(so, m, EINVAL);
2043 /* save policy id to buffer to be returned. */
2044 xpl0->sadb_x_policy_id = sp->id;
2046 sp->state = IPSEC_SPSTATE_DEAD;
2051 struct sadb_msg *newmsg;
2053 /* create new sadb_msg to reply. */
2054 n = key_gather_mbuf(m, mhp, 1, 4, SADB_EXT_RESERVED,
2055 SADB_X_EXT_POLICY, SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
2057 return key_senderror(so, m, ENOBUFS);
2059 newmsg = mtod(n, struct sadb_msg *);
2060 newmsg->sadb_msg_errno = 0;
2061 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
2064 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
2069 * SADB_SPDDELETE2 processing
2072 * from the user(?), and set SADB_SASTATE_DEAD,
2076 * policy(*) including direction of policy.
2078 * m will always be freed.
2081 key_spddelete2(so, m, mhp)
2084 const struct sadb_msghdr *mhp;
2087 struct secpolicy *sp;
2090 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
2091 panic("key_spddelete2: NULL pointer is passed.\n");
2093 if (mhp->ext[SADB_X_EXT_POLICY] == NULL ||
2094 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2095 ipseclog((LOG_DEBUG, "key_spddelete2: invalid message is passed.\n"));
2096 key_senderror(so, m, EINVAL);
2100 id = ((struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY])->sadb_x_policy_id;
2102 /* Is there SP in SPD ? */
2103 if ((sp = key_getspbyid(id)) == NULL) {
2104 ipseclog((LOG_DEBUG, "key_spddelete2: no SP found id:%u.\n", id));
2105 key_senderror(so, m, EINVAL);
2108 sp->state = IPSEC_SPSTATE_DEAD;
2112 struct mbuf *n, *nn;
2113 struct sadb_msg *newmsg;
2116 /* create new sadb_msg to reply. */
2117 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
2120 return key_senderror(so, m, ENOBUFS);
2121 MGETHDR(n, M_DONTWAIT, MT_DATA);
2122 if (n && len > MHLEN) {
2123 MCLGET(n, M_DONTWAIT);
2124 if ((n->m_flags & M_EXT) == 0) {
2130 return key_senderror(so, m, ENOBUFS);
2136 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
2137 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
2141 panic("length inconsistency in key_spddelete2");
2144 n->m_next = m_copym(m, mhp->extoff[SADB_X_EXT_POLICY],
2145 mhp->extlen[SADB_X_EXT_POLICY], M_DONTWAIT);
2148 return key_senderror(so, m, ENOBUFS);
2151 n->m_pkthdr.len = 0;
2152 for (nn = n; nn; nn = nn->m_next)
2153 n->m_pkthdr.len += nn->m_len;
2155 newmsg = mtod(n, struct sadb_msg *);
2156 newmsg->sadb_msg_errno = 0;
2157 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
2160 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
2165 * SADB_X_GET processing
2170 * <base, address(SD), policy>
2172 * policy(*) including direction of policy.
2174 * m will always be freed.
2177 key_spdget(so, m, mhp)
2180 const struct sadb_msghdr *mhp;
2183 struct secpolicy *sp;
2187 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
2188 panic("key_spdget: NULL pointer is passed.\n");
2190 if (mhp->ext[SADB_X_EXT_POLICY] == NULL ||
2191 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2192 ipseclog((LOG_DEBUG, "key_spdget: invalid message is passed.\n"));
2193 return key_senderror(so, m, EINVAL);
2196 id = ((struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY])->sadb_x_policy_id;
2198 /* Is there SP in SPD ? */
2199 if ((sp = key_getspbyid(id)) == NULL) {
2200 ipseclog((LOG_DEBUG, "key_spdget: no SP found id:%u.\n", id));
2201 return key_senderror(so, m, ENOENT);
2204 n = key_setdumpsp(sp, SADB_X_SPDGET, 0, mhp->msg->sadb_msg_pid);
2207 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
2209 return key_senderror(so, m, ENOBUFS);
2213 * SADB_X_SPDACQUIRE processing.
2214 * Acquire policy and SA(s) for a *OUTBOUND* packet.
2217 * to KMD, and expect to receive
2218 * <base> with SADB_X_SPDACQUIRE if error occured,
2221 * with SADB_X_SPDUPDATE from KMD by PF_KEY.
2222 * policy(*) is without policy requests.
2225 * others: error number
2229 struct secpolicy *sp;
2231 struct mbuf *result = NULL, *m;
2232 struct secspacq *newspacq;
2237 panic("key_spdacquire: NULL pointer is passed.\n");
2238 if (sp->req != NULL)
2239 panic("key_spdacquire: called but there is request.\n");
2240 if (sp->policy != IPSEC_POLICY_IPSEC)
2241 panic("key_spdacquire: policy mismathed. IPsec is expected.\n");
2243 /* get a entry to check whether sent message or not. */
2244 if ((newspacq = key_getspacq(&sp->spidx)) != NULL) {
2245 if (key_blockacq_count < newspacq->count) {
2246 /* reset counter and do send message. */
2247 newspacq->count = 0;
2249 /* increment counter and do nothing. */
2254 /* make new entry for blocking to send SADB_ACQUIRE. */
2255 if ((newspacq = key_newspacq(&sp->spidx)) == NULL)
2258 /* add to acqtree */
2259 LIST_INSERT_HEAD(&spacqtree, newspacq, chain);
2262 /* create new sadb_msg to reply. */
2263 m = key_setsadbmsg(SADB_X_SPDACQUIRE, 0, 0, 0, 0, 0);
2270 result->m_pkthdr.len = 0;
2271 for (m = result; m; m = m->m_next)
2272 result->m_pkthdr.len += m->m_len;
2274 mtod(result, struct sadb_msg *)->sadb_msg_len =
2275 PFKEY_UNIT64(result->m_pkthdr.len);
2277 return key_sendup_mbuf(NULL, m, KEY_SENDUP_REGISTERED);
2286 * SADB_SPDFLUSH processing
2289 * from the user, and free all entries in secpctree.
2293 * NOTE: what to do is only marking SADB_SASTATE_DEAD.
2295 * m will always be freed.
2298 key_spdflush(so, m, mhp)
2301 const struct sadb_msghdr *mhp;
2303 struct sadb_msg *newmsg;
2304 struct secpolicy *sp;
2308 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
2309 panic("key_spdflush: NULL pointer is passed.\n");
2311 if (m->m_len != PFKEY_ALIGN8(sizeof(struct sadb_msg)))
2312 return key_senderror(so, m, EINVAL);
2314 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2315 LIST_FOREACH(sp, &sptree[dir], chain) {
2316 sp->state = IPSEC_SPSTATE_DEAD;
2320 if (sizeof(struct sadb_msg) > m->m_len + M_TRAILINGSPACE(m)) {
2321 ipseclog((LOG_DEBUG, "key_spdflush: No more memory.\n"));
2322 return key_senderror(so, m, ENOBUFS);
2328 m->m_pkthdr.len = m->m_len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
2329 newmsg = mtod(m, struct sadb_msg *);
2330 newmsg->sadb_msg_errno = 0;
2331 newmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
2333 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
2337 * SADB_SPDDUMP processing
2340 * from the user, and dump all SP leaves
2345 * m will always be freed.
2348 key_spddump(so, m, mhp)
2351 const struct sadb_msghdr *mhp;
2353 struct secpolicy *sp;
2359 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
2360 panic("key_spddump: NULL pointer is passed.\n");
2362 /* search SPD entry and get buffer size. */
2364 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2365 LIST_FOREACH(sp, &sptree[dir], chain) {
2371 return key_senderror(so, m, ENOENT);
2373 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2374 LIST_FOREACH(sp, &sptree[dir], chain) {
2376 n = key_setdumpsp(sp, SADB_X_SPDDUMP, cnt,
2377 mhp->msg->sadb_msg_pid);
2380 key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
2388 static struct mbuf *
2389 key_setdumpsp(sp, type, seq, pid)
2390 struct secpolicy *sp;
2394 struct mbuf *result = NULL, *m;
2396 m = key_setsadbmsg(type, 0, SADB_SATYPE_UNSPEC, seq, pid, sp->refcnt);
2401 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
2402 (struct sockaddr *)&sp->spidx.src, sp->spidx.prefs,
2403 sp->spidx.ul_proto);
2408 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
2409 (struct sockaddr *)&sp->spidx.dst, sp->spidx.prefd,
2410 sp->spidx.ul_proto);
2420 if ((result->m_flags & M_PKTHDR) == 0)
2423 if (result->m_len < sizeof(struct sadb_msg)) {
2424 result = m_pullup(result, sizeof(struct sadb_msg));
2429 result->m_pkthdr.len = 0;
2430 for (m = result; m; m = m->m_next)
2431 result->m_pkthdr.len += m->m_len;
2433 mtod(result, struct sadb_msg *)->sadb_msg_len =
2434 PFKEY_UNIT64(result->m_pkthdr.len);
2444 * get PFKEY message length for security policy and request.
2447 key_getspreqmsglen(sp)
2448 struct secpolicy *sp;
2452 tlen = sizeof(struct sadb_x_policy);
2454 /* if is the policy for ipsec ? */
2455 if (sp->policy != IPSEC_POLICY_IPSEC)
2458 /* get length of ipsec requests */
2460 struct ipsecrequest *isr;
2463 for (isr = sp->req; isr != NULL; isr = isr->next) {
2464 len = sizeof(struct sadb_x_ipsecrequest)
2465 + isr->saidx.src.ss_len
2466 + isr->saidx.dst.ss_len;
2468 tlen += PFKEY_ALIGN8(len);
2476 * SADB_SPDEXPIRE processing
2478 * <base, address(SD), lifetime(CH), policy>
2482 * others : error number
2486 struct secpolicy *sp;
2489 struct mbuf *result = NULL, *m;
2492 struct sadb_lifetime *lt;
2494 /* XXX: Why do we lock ? */
2495 s = splnet(); /*called from softclock()*/
2499 panic("key_spdexpire: NULL pointer is passed.\n");
2501 /* set msg header */
2502 m = key_setsadbmsg(SADB_X_SPDEXPIRE, 0, 0, 0, 0, 0);
2509 /* create lifetime extension (current and hard) */
2510 len = PFKEY_ALIGN8(sizeof(*lt)) * 2;
2511 m = key_alloc_mbuf(len);
2512 if (!m || m->m_next) { /*XXX*/
2518 bzero(mtod(m, caddr_t), len);
2519 lt = mtod(m, struct sadb_lifetime *);
2520 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
2521 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
2522 lt->sadb_lifetime_allocations = 0;
2523 lt->sadb_lifetime_bytes = 0;
2524 lt->sadb_lifetime_addtime = sp->created;
2525 lt->sadb_lifetime_usetime = sp->lastused;
2526 lt = (struct sadb_lifetime *)(mtod(m, caddr_t) + len / 2);
2527 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
2528 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
2529 lt->sadb_lifetime_allocations = 0;
2530 lt->sadb_lifetime_bytes = 0;
2531 lt->sadb_lifetime_addtime = sp->lifetime;
2532 lt->sadb_lifetime_usetime = sp->validtime;
2535 /* set sadb_address for source */
2536 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
2537 (struct sockaddr *)&sp->spidx.src,
2538 sp->spidx.prefs, sp->spidx.ul_proto);
2545 /* set sadb_address for destination */
2546 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
2547 (struct sockaddr *)&sp->spidx.dst,
2548 sp->spidx.prefd, sp->spidx.ul_proto);
2563 if ((result->m_flags & M_PKTHDR) == 0) {
2568 if (result->m_len < sizeof(struct sadb_msg)) {
2569 result = m_pullup(result, sizeof(struct sadb_msg));
2570 if (result == NULL) {
2576 result->m_pkthdr.len = 0;
2577 for (m = result; m; m = m->m_next)
2578 result->m_pkthdr.len += m->m_len;
2580 mtod(result, struct sadb_msg *)->sadb_msg_len =
2581 PFKEY_UNIT64(result->m_pkthdr.len);
2583 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
2592 /* %%% SAD management */
2594 * allocating a memory for new SA head, and copy from the values of mhp.
2595 * OUT: NULL : failure due to the lack of memory.
2596 * others : pointer to new SA head.
2598 static struct secashead *
2600 struct secasindex *saidx;
2602 struct secashead *newsah;
2606 panic("key_newsaidx: NULL pointer is passed.\n");
2608 newsah = keydb_newsecashead();
2612 bcopy(saidx, &newsah->saidx, sizeof(newsah->saidx));
2614 /* add to saidxtree */
2615 newsah->state = SADB_SASTATE_MATURE;
2616 LIST_INSERT_HEAD(&sahtree, newsah, chain);
2622 * delete SA index and all SA registerd.
2626 struct secashead *sah;
2628 struct secasvar *sav, *nextsav;
2629 u_int stateidx, state;
2635 panic("key_delsah: NULL pointer is passed.\n");
2637 s = splnet(); /*called from softclock()*/
2639 /* searching all SA registerd in the secindex. */
2641 stateidx < _ARRAYLEN(saorder_state_any);
2644 state = saorder_state_any[stateidx];
2645 for (sav = (struct secasvar *)LIST_FIRST(&sah->savtree[state]);
2649 nextsav = LIST_NEXT(sav, chain);
2651 if (sav->refcnt > 0) {
2652 /* give up to delete this sa */
2658 KEY_CHKSASTATE(state, sav->state, "key_delsah");
2662 /* remove back pointer */
2668 /* don't delete sah only if there are savs. */
2674 if (sah->sa_route.ro_rt) {
2675 RTFREE(sah->sa_route.ro_rt);
2676 sah->sa_route.ro_rt = (struct rtentry *)NULL;
2679 /* remove from tree of SA index */
2680 if (__LIST_CHAINED(sah))
2681 LIST_REMOVE(sah, chain);
2690 * allocating a new SA with LARVAL state. key_add() and key_getspi() call,
2691 * and copy the values of mhp into new buffer.
2692 * When SAD message type is GETSPI:
2693 * to set sequence number from acq_seq++,
2694 * to set zero to SPI.
2695 * not to call key_setsava().
2697 * others : pointer to new secasvar.
2699 * does not modify mbuf. does not free mbuf on error.
2701 static struct secasvar *
2702 key_newsav(m, mhp, sah, errp)
2704 const struct sadb_msghdr *mhp;
2705 struct secashead *sah;
2708 struct secasvar *newsav;
2709 const struct sadb_sa *xsa;
2712 if (m == NULL || mhp == NULL || mhp->msg == NULL || sah == NULL)
2713 panic("key_newsa: NULL pointer is passed.\n");
2715 KMALLOC(newsav, struct secasvar *, sizeof(struct secasvar));
2716 if (newsav == NULL) {
2717 ipseclog((LOG_DEBUG, "key_newsa: No more memory.\n"));
2721 bzero((caddr_t)newsav, sizeof(struct secasvar));
2723 switch (mhp->msg->sadb_msg_type) {
2727 #ifdef IPSEC_DOSEQCHECK
2728 /* sync sequence number */
2729 if (mhp->msg->sadb_msg_seq == 0)
2731 (acq_seq = (acq_seq == ~0 ? 1 : ++acq_seq));
2734 newsav->seq = mhp->msg->sadb_msg_seq;
2739 if (mhp->ext[SADB_EXT_SA] == NULL) {
2741 ipseclog((LOG_DEBUG, "key_newsa: invalid message is passed.\n"));
2745 xsa = (const struct sadb_sa *)mhp->ext[SADB_EXT_SA];
2746 newsav->spi = xsa->sadb_sa_spi;
2747 newsav->seq = mhp->msg->sadb_msg_seq;
2755 /* copy sav values */
2756 if (mhp->msg->sadb_msg_type != SADB_GETSPI) {
2757 *errp = key_setsaval(newsav, m, mhp);
2768 newsav->created = tv.tv_sec;
2771 newsav->pid = mhp->msg->sadb_msg_pid;
2776 newsav->state = SADB_SASTATE_LARVAL;
2777 LIST_INSERT_TAIL(&sah->savtree[SADB_SASTATE_LARVAL], newsav,
2784 * free() SA variable entry.
2788 struct secasvar *sav;
2792 panic("key_delsav: NULL pointer is passed.\n");
2794 if (sav->refcnt > 0)
2795 return; /* can't free */
2797 /* remove from SA header */
2798 if (__LIST_CHAINED(sav))
2799 LIST_REMOVE(sav, chain);
2801 if (sav->key_auth != NULL) {
2802 bzero(_KEYBUF(sav->key_auth), _KEYLEN(sav->key_auth));
2803 KFREE(sav->key_auth);
2804 sav->key_auth = NULL;
2806 if (sav->key_enc != NULL) {
2807 bzero(_KEYBUF(sav->key_enc), _KEYLEN(sav->key_enc));
2808 KFREE(sav->key_enc);
2809 sav->key_enc = NULL;
2812 bzero(sav->sched, sav->schedlen);
2816 if (sav->replay != NULL) {
2817 keydb_delsecreplay(sav->replay);
2820 if (sav->lft_c != NULL) {
2824 if (sav->lft_h != NULL) {
2828 if (sav->lft_s != NULL) {
2832 if (sav->iv != NULL) {
2846 * others : found, pointer to a SA.
2848 static struct secashead *
2850 struct secasindex *saidx;
2852 struct secashead *sah;
2854 LIST_FOREACH(sah, &sahtree, chain) {
2855 if (sah->state == SADB_SASTATE_DEAD)
2857 if (key_cmpsaidx(&sah->saidx, saidx, CMP_REQID))
2865 * check not to be duplicated SPI.
2866 * NOTE: this function is too slow due to searching all SAD.
2869 * others : found, pointer to a SA.
2871 static struct secasvar *
2872 key_checkspidup(saidx, spi)
2873 struct secasindex *saidx;
2876 struct secashead *sah;
2877 struct secasvar *sav;
2879 /* check address family */
2880 if (saidx->src.ss_family != saidx->dst.ss_family) {
2881 ipseclog((LOG_DEBUG, "key_checkspidup: address family mismatched.\n"));
2886 LIST_FOREACH(sah, &sahtree, chain) {
2887 if (!key_ismyaddr((struct sockaddr *)&sah->saidx.dst))
2889 sav = key_getsavbyspi(sah, spi);
2898 * search SAD litmited alive SA, protocol, SPI.
2901 * others : found, pointer to a SA.
2903 static struct secasvar *
2904 key_getsavbyspi(sah, spi)
2905 struct secashead *sah;
2908 struct secasvar *sav;
2909 u_int stateidx, state;
2911 /* search all status */
2913 stateidx < _ARRAYLEN(saorder_state_alive);
2916 state = saorder_state_alive[stateidx];
2917 LIST_FOREACH(sav, &sah->savtree[state], chain) {
2920 if (sav->state != state) {
2921 ipseclog((LOG_DEBUG, "key_getsavbyspi: "
2922 "invalid sav->state (queue: %d SA: %d)\n",
2923 state, sav->state));
2927 if (sav->spi == spi)
2936 * copy SA values from PF_KEY message except *SPI, SEQ, PID, STATE and TYPE*.
2937 * You must update these if need.
2941 * does not modify mbuf. does not free mbuf on error.
2944 key_setsaval(sav, m, mhp)
2945 struct secasvar *sav;
2947 const struct sadb_msghdr *mhp;
2950 const struct esp_algorithm *algo;
2956 if (m == NULL || mhp == NULL || mhp->msg == NULL)
2957 panic("key_setsaval: NULL pointer is passed.\n");
2959 /* initialization */
2961 sav->key_auth = NULL;
2962 sav->key_enc = NULL;
2971 if (mhp->ext[SADB_EXT_SA] != NULL) {
2972 const struct sadb_sa *sa0;
2974 sa0 = (const struct sadb_sa *)mhp->ext[SADB_EXT_SA];
2975 if (mhp->extlen[SADB_EXT_SA] < sizeof(*sa0)) {
2980 sav->alg_auth = sa0->sadb_sa_auth;
2981 sav->alg_enc = sa0->sadb_sa_encrypt;
2982 sav->flags = sa0->sadb_sa_flags;
2985 if ((sa0->sadb_sa_flags & SADB_X_EXT_OLD) == 0) {
2986 sav->replay = keydb_newsecreplay(sa0->sadb_sa_replay);
2987 if (sav->replay == NULL) {
2988 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
2995 /* Authentication keys */
2996 if (mhp->ext[SADB_EXT_KEY_AUTH] != NULL) {
2997 const struct sadb_key *key0;
3000 key0 = (const struct sadb_key *)mhp->ext[SADB_EXT_KEY_AUTH];
3001 len = mhp->extlen[SADB_EXT_KEY_AUTH];
3004 if (len < sizeof(*key0)) {
3008 switch (mhp->msg->sadb_msg_satype) {
3009 case SADB_SATYPE_AH:
3010 case SADB_SATYPE_ESP:
3011 if (len == PFKEY_ALIGN8(sizeof(struct sadb_key)) &&
3012 sav->alg_auth != SADB_X_AALG_NULL)
3015 case SADB_X_SATYPE_IPCOMP:
3021 ipseclog((LOG_DEBUG, "key_setsaval: invalid key_auth values.\n"));
3025 sav->key_auth = (struct sadb_key *)key_newbuf(key0, len);
3026 if (sav->key_auth == NULL) {
3027 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
3033 /* Encryption key */
3034 if (mhp->ext[SADB_EXT_KEY_ENCRYPT] != NULL) {
3035 const struct sadb_key *key0;
3038 key0 = (const struct sadb_key *)mhp->ext[SADB_EXT_KEY_ENCRYPT];
3039 len = mhp->extlen[SADB_EXT_KEY_ENCRYPT];
3042 if (len < sizeof(*key0)) {
3046 switch (mhp->msg->sadb_msg_satype) {
3047 case SADB_SATYPE_ESP:
3048 if (len == PFKEY_ALIGN8(sizeof(struct sadb_key)) &&
3049 sav->alg_enc != SADB_EALG_NULL) {
3053 sav->key_enc = (struct sadb_key *)key_newbuf(key0, len);
3054 if (sav->key_enc == NULL) {
3055 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
3060 case SADB_X_SATYPE_IPCOMP:
3061 if (len != PFKEY_ALIGN8(sizeof(struct sadb_key)))
3063 sav->key_enc = NULL; /*just in case*/
3065 case SADB_SATYPE_AH:
3071 ipseclog((LOG_DEBUG, "key_setsatval: invalid key_enc value.\n"));
3079 switch (mhp->msg->sadb_msg_satype) {
3080 case SADB_SATYPE_ESP:
3082 algo = esp_algorithm_lookup(sav->alg_enc);
3083 if (algo && algo->ivlen)
3084 sav->ivlen = (*algo->ivlen)(algo, sav);
3085 if (sav->ivlen == 0)
3087 KMALLOC(sav->iv, caddr_t, sav->ivlen);
3089 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
3095 key_randomfill(sav->iv, sav->ivlen);
3098 case SADB_SATYPE_AH:
3099 case SADB_X_SATYPE_IPCOMP:
3102 ipseclog((LOG_DEBUG, "key_setsaval: invalid SA type.\n"));
3109 sav->created = tv.tv_sec;
3111 /* make lifetime for CURRENT */
3112 KMALLOC(sav->lft_c, struct sadb_lifetime *,
3113 sizeof(struct sadb_lifetime));
3114 if (sav->lft_c == NULL) {
3115 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
3122 sav->lft_c->sadb_lifetime_len =
3123 PFKEY_UNIT64(sizeof(struct sadb_lifetime));
3124 sav->lft_c->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
3125 sav->lft_c->sadb_lifetime_allocations = 0;
3126 sav->lft_c->sadb_lifetime_bytes = 0;
3127 sav->lft_c->sadb_lifetime_addtime = tv.tv_sec;
3128 sav->lft_c->sadb_lifetime_usetime = 0;
3130 /* lifetimes for HARD and SOFT */
3132 const struct sadb_lifetime *lft0;
3134 lft0 = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_HARD];
3136 if (mhp->extlen[SADB_EXT_LIFETIME_HARD] < sizeof(*lft0)) {
3140 sav->lft_h = (struct sadb_lifetime *)key_newbuf(lft0,
3142 if (sav->lft_h == NULL) {
3143 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
3147 /* to be initialize ? */
3150 lft0 = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_SOFT];
3152 if (mhp->extlen[SADB_EXT_LIFETIME_SOFT] < sizeof(*lft0)) {
3156 sav->lft_s = (struct sadb_lifetime *)key_newbuf(lft0,
3158 if (sav->lft_s == NULL) {
3159 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
3163 /* to be initialize ? */
3170 /* initialization */
3171 if (sav->replay != NULL) {
3172 keydb_delsecreplay(sav->replay);
3175 if (sav->key_auth != NULL) {
3176 KFREE(sav->key_auth);
3177 sav->key_auth = NULL;
3179 if (sav->key_enc != NULL) {
3180 KFREE(sav->key_enc);
3181 sav->key_enc = NULL;
3187 if (sav->iv != NULL) {
3191 if (sav->lft_c != NULL) {
3195 if (sav->lft_h != NULL) {
3199 if (sav->lft_s != NULL) {
3208 * validation with a secasvar entry, and set SADB_SATYPE_MATURE.
3214 struct secasvar *sav;
3217 int checkmask = 0; /* 2^0: ealg 2^1: aalg 2^2: calg */
3218 int mustmask = 0; /* 2^0: ealg 2^1: aalg 2^2: calg */
3222 /* check SPI value */
3223 switch (sav->sah->saidx.proto) {
3226 if (ntohl(sav->spi) >= 0 && ntohl(sav->spi) <= 255) {
3227 ipseclog((LOG_DEBUG,
3228 "key_mature: illegal range of SPI %u.\n",
3229 (u_int32_t)ntohl(sav->spi)));
3236 switch (sav->sah->saidx.proto) {
3239 if ((sav->flags & SADB_X_EXT_OLD)
3240 && (sav->flags & SADB_X_EXT_DERIV)) {
3241 ipseclog((LOG_DEBUG, "key_mature: "
3242 "invalid flag (derived) given to old-esp.\n"));
3245 if (sav->alg_auth == SADB_AALG_NONE)
3253 if (sav->flags & SADB_X_EXT_DERIV) {
3254 ipseclog((LOG_DEBUG, "key_mature: "
3255 "invalid flag (derived) given to AH SA.\n"));
3258 if (sav->alg_enc != SADB_EALG_NONE) {
3259 ipseclog((LOG_DEBUG, "key_mature: "
3260 "protocol and algorithm mismated.\n"));
3266 case IPPROTO_IPCOMP:
3267 if (sav->alg_auth != SADB_AALG_NONE) {
3268 ipseclog((LOG_DEBUG, "key_mature: "
3269 "protocol and algorithm mismated.\n"));
3272 if ((sav->flags & SADB_X_EXT_RAWCPI) == 0
3273 && ntohl(sav->spi) >= 0x10000) {
3274 ipseclog((LOG_DEBUG, "key_mature: invalid cpi for IPComp.\n"));
3281 ipseclog((LOG_DEBUG, "key_mature: Invalid satype.\n"));
3282 return EPROTONOSUPPORT;
3285 /* check authentication algorithm */
3286 if ((checkmask & 2) != 0) {
3287 const struct ah_algorithm *algo;
3290 algo = ah_algorithm_lookup(sav->alg_auth);
3292 ipseclog((LOG_DEBUG,"key_mature: "
3293 "unknown authentication algorithm.\n"));
3297 /* algorithm-dependent check */
3299 keylen = sav->key_auth->sadb_key_bits;
3302 if (keylen < algo->keymin || algo->keymax < keylen) {
3303 ipseclog((LOG_DEBUG,
3304 "key_mature: invalid AH key length %d "
3305 "(%d-%d allowed)\n",
3306 keylen, algo->keymin, algo->keymax));
3311 if ((*algo->mature)(sav)) {
3312 /* message generated in per-algorithm function*/
3315 mature = SADB_SATYPE_AH;
3318 if ((mustmask & 2) != 0 && mature != SADB_SATYPE_AH) {
3319 ipseclog((LOG_DEBUG, "key_mature: no satisfy algorithm for AH\n"));
3324 /* check encryption algorithm */
3325 if ((checkmask & 1) != 0) {
3327 const struct esp_algorithm *algo;
3330 algo = esp_algorithm_lookup(sav->alg_enc);
3332 ipseclog((LOG_DEBUG, "key_mature: unknown encryption algorithm.\n"));
3336 /* algorithm-dependent check */
3338 keylen = sav->key_enc->sadb_key_bits;
3341 if (keylen < algo->keymin || algo->keymax < keylen) {
3342 ipseclog((LOG_DEBUG,
3343 "key_mature: invalid ESP key length %d "
3344 "(%d-%d allowed)\n",
3345 keylen, algo->keymin, algo->keymax));
3350 if ((*algo->mature)(sav)) {
3351 /* message generated in per-algorithm function*/
3354 mature = SADB_SATYPE_ESP;
3357 if ((mustmask & 1) != 0 && mature != SADB_SATYPE_ESP) {
3358 ipseclog((LOG_DEBUG, "key_mature: no satisfy algorithm for ESP\n"));
3362 ipseclog((LOG_DEBUG, "key_mature: ESP not supported in this configuration\n"));
3367 /* check compression algorithm */
3368 if ((checkmask & 4) != 0) {
3369 const struct ipcomp_algorithm *algo;
3371 /* algorithm-dependent check */
3372 algo = ipcomp_algorithm_lookup(sav->alg_enc);
3374 ipseclog((LOG_DEBUG, "key_mature: unknown compression algorithm.\n"));
3379 key_sa_chgstate(sav, SADB_SASTATE_MATURE);
3385 * subroutine for SADB_GET and SADB_DUMP.
3387 static struct mbuf *
3388 key_setdumpsa(sav, type, satype, seq, pid)
3389 struct secasvar *sav;
3390 u_int8_t type, satype;
3393 struct mbuf *result = NULL, *tres = NULL, *m;
3398 SADB_EXT_SA, SADB_X_EXT_SA2,
3399 SADB_EXT_LIFETIME_HARD, SADB_EXT_LIFETIME_SOFT,
3400 SADB_EXT_LIFETIME_CURRENT, SADB_EXT_ADDRESS_SRC,
3401 SADB_EXT_ADDRESS_DST, SADB_EXT_ADDRESS_PROXY, SADB_EXT_KEY_AUTH,
3402 SADB_EXT_KEY_ENCRYPT, SADB_EXT_IDENTITY_SRC,
3403 SADB_EXT_IDENTITY_DST, SADB_EXT_SENSITIVITY,
3406 m = key_setsadbmsg(type, 0, satype, seq, pid, sav->refcnt);
3411 for (i = sizeof(dumporder)/sizeof(dumporder[0]) - 1; i >= 0; i--) {
3414 switch (dumporder[i]) {
3416 m = key_setsadbsa(sav);
3421 case SADB_X_EXT_SA2:
3422 m = key_setsadbxsa2(sav->sah->saidx.mode,
3423 sav->replay ? sav->replay->count : 0,
3424 sav->sah->saidx.reqid);
3429 case SADB_EXT_ADDRESS_SRC:
3430 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
3431 (struct sockaddr *)&sav->sah->saidx.src,
3432 FULLMASK, IPSEC_ULPROTO_ANY);
3437 case SADB_EXT_ADDRESS_DST:
3438 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
3439 (struct sockaddr *)&sav->sah->saidx.dst,
3440 FULLMASK, IPSEC_ULPROTO_ANY);
3445 case SADB_EXT_KEY_AUTH:
3448 l = PFKEY_UNUNIT64(sav->key_auth->sadb_key_len);
3452 case SADB_EXT_KEY_ENCRYPT:
3455 l = PFKEY_UNUNIT64(sav->key_enc->sadb_key_len);
3459 case SADB_EXT_LIFETIME_CURRENT:
3462 l = PFKEY_UNUNIT64(((struct sadb_ext *)sav->lft_c)->sadb_ext_len);
3466 case SADB_EXT_LIFETIME_HARD:
3469 l = PFKEY_UNUNIT64(((struct sadb_ext *)sav->lft_h)->sadb_ext_len);
3473 case SADB_EXT_LIFETIME_SOFT:
3476 l = PFKEY_UNUNIT64(((struct sadb_ext *)sav->lft_s)->sadb_ext_len);
3480 case SADB_EXT_ADDRESS_PROXY:
3481 case SADB_EXT_IDENTITY_SRC:
3482 case SADB_EXT_IDENTITY_DST:
3483 /* XXX: should we brought from SPD ? */
3484 case SADB_EXT_SENSITIVITY:
3489 if ((!m && !p) || (m && p))
3492 M_PREPEND(tres, l, M_DONTWAIT);
3495 bcopy(p, mtod(tres, caddr_t), l);
3499 m = key_alloc_mbuf(l);
3502 m_copyback(m, 0, l, p);
3510 m_cat(result, tres);
3512 if (result->m_len < sizeof(struct sadb_msg)) {
3513 result = m_pullup(result, sizeof(struct sadb_msg));
3518 result->m_pkthdr.len = 0;
3519 for (m = result; m; m = m->m_next)
3520 result->m_pkthdr.len += m->m_len;
3522 mtod(result, struct sadb_msg *)->sadb_msg_len =
3523 PFKEY_UNIT64(result->m_pkthdr.len);
3534 * set data into sadb_msg.
3536 static struct mbuf *
3537 key_setsadbmsg(type, tlen, satype, seq, pid, reserved)
3538 u_int8_t type, satype;
3548 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
3551 MGETHDR(m, M_DONTWAIT, MT_DATA);
3552 if (m && len > MHLEN) {
3553 MCLGET(m, M_DONTWAIT);
3554 if ((m->m_flags & M_EXT) == 0) {
3561 m->m_pkthdr.len = m->m_len = len;
3564 p = mtod(m, struct sadb_msg *);
3567 p->sadb_msg_version = PF_KEY_V2;
3568 p->sadb_msg_type = type;
3569 p->sadb_msg_errno = 0;
3570 p->sadb_msg_satype = satype;
3571 p->sadb_msg_len = PFKEY_UNIT64(tlen);
3572 p->sadb_msg_reserved = reserved;
3573 p->sadb_msg_seq = seq;
3574 p->sadb_msg_pid = (u_int32_t)pid;
3580 * copy secasvar data into sadb_address.
3582 static struct mbuf *
3584 struct secasvar *sav;
3590 len = PFKEY_ALIGN8(sizeof(struct sadb_sa));
3591 m = key_alloc_mbuf(len);
3592 if (!m || m->m_next) { /*XXX*/
3598 p = mtod(m, struct sadb_sa *);
3601 p->sadb_sa_len = PFKEY_UNIT64(len);
3602 p->sadb_sa_exttype = SADB_EXT_SA;
3603 p->sadb_sa_spi = sav->spi;
3604 p->sadb_sa_replay = (sav->replay != NULL ? sav->replay->wsize : 0);
3605 p->sadb_sa_state = sav->state;
3606 p->sadb_sa_auth = sav->alg_auth;
3607 p->sadb_sa_encrypt = sav->alg_enc;
3608 p->sadb_sa_flags = sav->flags;
3614 * set data into sadb_address.
3616 static struct mbuf *
3617 key_setsadbaddr(exttype, saddr, prefixlen, ul_proto)
3619 struct sockaddr *saddr;
3624 struct sadb_address *p;
3627 len = PFKEY_ALIGN8(sizeof(struct sadb_address)) +
3628 PFKEY_ALIGN8(saddr->sa_len);
3629 m = key_alloc_mbuf(len);
3630 if (!m || m->m_next) { /*XXX*/
3636 p = mtod(m, struct sadb_address *);
3639 p->sadb_address_len = PFKEY_UNIT64(len);
3640 p->sadb_address_exttype = exttype;
3641 p->sadb_address_proto = ul_proto;
3642 if (prefixlen == FULLMASK) {
3643 switch (saddr->sa_family) {
3645 prefixlen = sizeof(struct in_addr) << 3;
3648 prefixlen = sizeof(struct in6_addr) << 3;
3654 p->sadb_address_prefixlen = prefixlen;
3655 p->sadb_address_reserved = 0;
3658 mtod(m, caddr_t) + PFKEY_ALIGN8(sizeof(struct sadb_address)),
3666 * set data into sadb_ident.
3668 static struct mbuf *
3669 key_setsadbident(exttype, idtype, string, stringlen, id)
3670 u_int16_t exttype, idtype;
3676 struct sadb_ident *p;
3679 len = PFKEY_ALIGN8(sizeof(struct sadb_ident)) + PFKEY_ALIGN8(stringlen);
3680 m = key_alloc_mbuf(len);
3681 if (!m || m->m_next) { /*XXX*/
3687 p = mtod(m, struct sadb_ident *);
3690 p->sadb_ident_len = PFKEY_UNIT64(len);
3691 p->sadb_ident_exttype = exttype;
3692 p->sadb_ident_type = idtype;
3693 p->sadb_ident_reserved = 0;
3694 p->sadb_ident_id = id;
3697 mtod(m, caddr_t) + PFKEY_ALIGN8(sizeof(struct sadb_ident)),
3705 * set data into sadb_x_sa2.
3707 static struct mbuf *
3708 key_setsadbxsa2(mode, seq, reqid)
3710 u_int32_t seq, reqid;
3713 struct sadb_x_sa2 *p;
3716 len = PFKEY_ALIGN8(sizeof(struct sadb_x_sa2));
3717 m = key_alloc_mbuf(len);
3718 if (!m || m->m_next) { /*XXX*/
3724 p = mtod(m, struct sadb_x_sa2 *);
3727 p->sadb_x_sa2_len = PFKEY_UNIT64(len);
3728 p->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
3729 p->sadb_x_sa2_mode = mode;
3730 p->sadb_x_sa2_reserved1 = 0;
3731 p->sadb_x_sa2_reserved2 = 0;
3732 p->sadb_x_sa2_sequence = seq;
3733 p->sadb_x_sa2_reqid = reqid;
3739 * set data into sadb_x_policy
3741 static struct mbuf *
3742 key_setsadbxpolicy(type, dir, id)
3748 struct sadb_x_policy *p;
3751 len = PFKEY_ALIGN8(sizeof(struct sadb_x_policy));
3752 m = key_alloc_mbuf(len);
3753 if (!m || m->m_next) { /*XXX*/
3759 p = mtod(m, struct sadb_x_policy *);
3762 p->sadb_x_policy_len = PFKEY_UNIT64(len);
3763 p->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3764 p->sadb_x_policy_type = type;
3765 p->sadb_x_policy_dir = dir;
3766 p->sadb_x_policy_id = id;
3773 * copy a buffer into the new buffer allocated.
3776 key_newbuf(src, len)
3782 KMALLOC(new, caddr_t, len);
3784 ipseclog((LOG_DEBUG, "key_newbuf: No more memory.\n"));
3787 bcopy(src, new, len);
3792 /* compare my own address
3793 * OUT: 1: true, i.e. my address.
3798 struct sockaddr *sa;
3801 struct sockaddr_in *sin;
3802 struct in_ifaddr *ia;
3807 panic("key_ismyaddr: NULL pointer is passed.\n");
3809 switch (sa->sa_family) {
3812 sin = (struct sockaddr_in *)sa;
3813 for (ia = in_ifaddrhead.tqh_first; ia;
3814 ia = ia->ia_link.tqe_next)
3816 if (sin->sin_family == ia->ia_addr.sin_family &&
3817 sin->sin_len == ia->ia_addr.sin_len &&
3818 sin->sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr)
3827 return key_ismyaddr6((struct sockaddr_in6 *)sa);
3836 * compare my own address for IPv6.
3839 * NOTE: derived ip6_input() in KAME. This is necessary to modify more.
3841 #include <netinet6/in6_var.h>
3845 struct sockaddr_in6 *sin6;
3847 struct in6_ifaddr *ia;
3848 struct in6_multi *in6m;
3850 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
3851 if (key_sockaddrcmp((struct sockaddr *)&sin6,
3852 (struct sockaddr *)&ia->ia_addr, 0) == 0)
3857 * XXX why do we care about multlicast here while we don't care
3858 * about IPv4 multicast??
3862 IN6_LOOKUP_MULTI(sin6->sin6_addr, ia->ia_ifp, in6m);
3867 /* loopback, just for safety */
3868 if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
3876 * compare two secasindex structure.
3877 * flag can specify to compare 2 saidxes.
3878 * compare two secasindex structure without both mode and reqid.
3879 * don't compare port.
3881 * saidx0: source, it can be in SAD.
3888 key_cmpsaidx(saidx0, saidx1, flag)
3889 struct secasindex *saidx0, *saidx1;
3893 if (saidx0 == NULL && saidx1 == NULL)
3896 if (saidx0 == NULL || saidx1 == NULL)
3899 if (saidx0->proto != saidx1->proto)
3902 if (flag == CMP_EXACTLY) {
3903 if (saidx0->mode != saidx1->mode)
3905 if (saidx0->reqid != saidx1->reqid)
3907 if (bcmp(&saidx0->src, &saidx1->src, saidx0->src.ss_len) != 0 ||
3908 bcmp(&saidx0->dst, &saidx1->dst, saidx0->dst.ss_len) != 0)
3912 /* CMP_MODE_REQID, CMP_REQID, CMP_HEAD */
3913 if (flag == CMP_MODE_REQID
3914 ||flag == CMP_REQID) {
3916 * If reqid of SPD is non-zero, unique SA is required.
3917 * The result must be of same reqid in this case.
3919 if (saidx1->reqid != 0 && saidx0->reqid != saidx1->reqid)
3923 if (flag == CMP_MODE_REQID) {
3924 if (saidx0->mode != IPSEC_MODE_ANY
3925 && saidx0->mode != saidx1->mode)
3929 if (key_sockaddrcmp((struct sockaddr *)&saidx0->src,
3930 (struct sockaddr *)&saidx1->src, 0) != 0) {
3933 if (key_sockaddrcmp((struct sockaddr *)&saidx0->dst,
3934 (struct sockaddr *)&saidx1->dst, 0) != 0) {
3943 * compare two secindex structure exactly.
3945 * spidx0: source, it is often in SPD.
3946 * spidx1: object, it is often from PFKEY message.
3952 key_cmpspidx_exactly(spidx0, spidx1)
3953 struct secpolicyindex *spidx0, *spidx1;
3956 if (spidx0 == NULL && spidx1 == NULL)
3959 if (spidx0 == NULL || spidx1 == NULL)
3962 if (spidx0->prefs != spidx1->prefs
3963 || spidx0->prefd != spidx1->prefd
3964 || spidx0->ul_proto != spidx1->ul_proto)
3967 if (key_sockaddrcmp((struct sockaddr *)&spidx0->src,
3968 (struct sockaddr *)&spidx1->src, 1) != 0) {
3971 if (key_sockaddrcmp((struct sockaddr *)&spidx0->dst,
3972 (struct sockaddr *)&spidx1->dst, 1) != 0) {
3980 * compare two secindex structure with mask.
3982 * spidx0: source, it is often in SPD.
3983 * spidx1: object, it is often from IP header.
3989 key_cmpspidx_withmask(spidx0, spidx1)
3990 struct secpolicyindex *spidx0, *spidx1;
3993 if (spidx0 == NULL && spidx1 == NULL)
3996 if (spidx0 == NULL || spidx1 == NULL)
3999 if (spidx0->src.ss_family != spidx1->src.ss_family ||
4000 spidx0->dst.ss_family != spidx1->dst.ss_family ||
4001 spidx0->src.ss_len != spidx1->src.ss_len ||
4002 spidx0->dst.ss_len != spidx1->dst.ss_len)
4005 /* if spidx.ul_proto == IPSEC_ULPROTO_ANY, ignore. */
4006 if (spidx0->ul_proto != (u_int16_t)IPSEC_ULPROTO_ANY
4007 && spidx0->ul_proto != spidx1->ul_proto)
4010 switch (spidx0->src.ss_family) {
4012 if (satosin(&spidx0->src)->sin_port != IPSEC_PORT_ANY
4013 && satosin(&spidx0->src)->sin_port !=
4014 satosin(&spidx1->src)->sin_port)
4016 if (!key_bbcmp((caddr_t)&satosin(&spidx0->src)->sin_addr,
4017 (caddr_t)&satosin(&spidx1->src)->sin_addr, spidx0->prefs))
4021 if (satosin6(&spidx0->src)->sin6_port != IPSEC_PORT_ANY
4022 && satosin6(&spidx0->src)->sin6_port !=
4023 satosin6(&spidx1->src)->sin6_port)
4026 * scope_id check. if sin6_scope_id is 0, we regard it
4027 * as a wildcard scope, which matches any scope zone ID.
4029 if (satosin6(&spidx0->src)->sin6_scope_id &&
4030 satosin6(&spidx1->src)->sin6_scope_id &&
4031 satosin6(&spidx0->src)->sin6_scope_id !=
4032 satosin6(&spidx1->src)->sin6_scope_id)
4034 if (!key_bbcmp((caddr_t)&satosin6(&spidx0->src)->sin6_addr,
4035 (caddr_t)&satosin6(&spidx1->src)->sin6_addr, spidx0->prefs))
4040 if (bcmp(&spidx0->src, &spidx1->src, spidx0->src.ss_len) != 0)
4045 switch (spidx0->dst.ss_family) {
4047 if (satosin(&spidx0->dst)->sin_port != IPSEC_PORT_ANY
4048 && satosin(&spidx0->dst)->sin_port !=
4049 satosin(&spidx1->dst)->sin_port)
4051 if (!key_bbcmp((caddr_t)&satosin(&spidx0->dst)->sin_addr,
4052 (caddr_t)&satosin(&spidx1->dst)->sin_addr, spidx0->prefd))
4056 if (satosin6(&spidx0->dst)->sin6_port != IPSEC_PORT_ANY
4057 && satosin6(&spidx0->dst)->sin6_port !=
4058 satosin6(&spidx1->dst)->sin6_port)
4061 * scope_id check. if sin6_scope_id is 0, we regard it
4062 * as a wildcard scope, which matches any scope zone ID.
4064 if (satosin6(&spidx0->src)->sin6_scope_id &&
4065 satosin6(&spidx1->src)->sin6_scope_id &&
4066 satosin6(&spidx0->dst)->sin6_scope_id !=
4067 satosin6(&spidx1->dst)->sin6_scope_id)
4069 if (!key_bbcmp((caddr_t)&satosin6(&spidx0->dst)->sin6_addr,
4070 (caddr_t)&satosin6(&spidx1->dst)->sin6_addr, spidx0->prefd))
4075 if (bcmp(&spidx0->dst, &spidx1->dst, spidx0->dst.ss_len) != 0)
4080 /* XXX Do we check other field ? e.g. flowinfo */
4085 /* returns 0 on match */
4087 key_sockaddrcmp(sa1, sa2, port)
4088 struct sockaddr *sa1;
4089 struct sockaddr *sa2;
4092 if (sa1->sa_family != sa2->sa_family || sa1->sa_len != sa2->sa_len)
4095 switch (sa1->sa_family) {
4097 if (sa1->sa_len != sizeof(struct sockaddr_in))
4099 if (satosin(sa1)->sin_addr.s_addr !=
4100 satosin(sa2)->sin_addr.s_addr) {
4103 if (port && satosin(sa1)->sin_port != satosin(sa2)->sin_port)
4107 if (sa1->sa_len != sizeof(struct sockaddr_in6))
4108 return 1; /*EINVAL*/
4109 if (satosin6(sa1)->sin6_scope_id !=
4110 satosin6(sa2)->sin6_scope_id) {
4113 if (!IN6_ARE_ADDR_EQUAL(&satosin6(sa1)->sin6_addr,
4114 &satosin6(sa2)->sin6_addr)) {
4118 satosin6(sa1)->sin6_port != satosin6(sa2)->sin6_port) {
4122 if (bcmp(sa1, sa2, sa1->sa_len) != 0)
4131 * compare two buffers with mask.
4135 * bits: Number of bits to compare
4141 key_bbcmp(p1, p2, bits)
4147 /* XXX: This could be considerably faster if we compare a word
4148 * at a time, but it is complicated on LSB Endian machines */
4150 /* Handle null pointers */
4151 if (p1 == NULL || p2 == NULL)
4161 mask = ~((1<<(8-bits))-1);
4162 if ((*p1 & mask) != (*p2 & mask))
4165 return 1; /* Match! */
4170 * scanning SPD and SAD to check status for each entries,
4171 * and do to remove or to expire.
4172 * XXX: year 2038 problem may remain.
4175 key_timehandler(void)
4183 s = splnet(); /*called from softclock()*/
4187 struct secpolicy *sp, *nextsp;
4189 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
4190 for (sp = LIST_FIRST(&sptree[dir]);
4194 nextsp = LIST_NEXT(sp, chain);
4196 if (sp->state == IPSEC_SPSTATE_DEAD) {
4201 if (sp->lifetime == 0 && sp->validtime == 0)
4204 /* the deletion will occur next time */
4206 && tv.tv_sec - sp->created > sp->lifetime)
4208 && tv.tv_sec - sp->lastused > sp->validtime)) {
4209 sp->state = IPSEC_SPSTATE_DEAD;
4219 struct secashead *sah, *nextsah;
4220 struct secasvar *sav, *nextsav;
4222 for (sah = LIST_FIRST(&sahtree);
4226 nextsah = LIST_NEXT(sah, chain);
4228 /* if sah has been dead, then delete it and process next sah. */
4229 if (sah->state == SADB_SASTATE_DEAD) {
4234 /* if LARVAL entry doesn't become MATURE, delete it. */
4235 for (sav = LIST_FIRST(&sah->savtree[SADB_SASTATE_LARVAL]);
4239 nextsav = LIST_NEXT(sav, chain);
4241 if (tv.tv_sec - sav->created > key_larval_lifetime) {
4247 * check MATURE entry to start to send expire message
4250 for (sav = LIST_FIRST(&sah->savtree[SADB_SASTATE_MATURE]);
4254 nextsav = LIST_NEXT(sav, chain);
4256 /* we don't need to check. */
4257 if (sav->lft_s == NULL)
4261 if (sav->lft_c == NULL) {
4262 ipseclog((LOG_DEBUG,"key_timehandler: "
4263 "There is no CURRENT time, why?\n"));
4267 /* check SOFT lifetime */
4268 if (sav->lft_s->sadb_lifetime_addtime != 0
4269 && tv.tv_sec - sav->created > sav->lft_s->sadb_lifetime_addtime) {
4271 * check the SA if it has been used.
4272 * when it hasn't been used, delete it.
4273 * i don't think such SA will be used.
4275 if (sav->lft_c->sadb_lifetime_usetime == 0) {
4276 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4280 key_sa_chgstate(sav, SADB_SASTATE_DYING);
4282 * XXX If we keep to send expire
4283 * message in the status of
4284 * DYING. Do remove below code.
4290 /* check SOFT lifetime by bytes */
4292 * XXX I don't know the way to delete this SA
4293 * when new SA is installed. Caution when it's
4294 * installed too big lifetime by time.
4296 else if (sav->lft_s->sadb_lifetime_bytes != 0
4297 && sav->lft_s->sadb_lifetime_bytes < sav->lft_c->sadb_lifetime_bytes) {
4299 key_sa_chgstate(sav, SADB_SASTATE_DYING);
4301 * XXX If we keep to send expire
4302 * message in the status of
4303 * DYING. Do remove below code.
4309 /* check DYING entry to change status to DEAD. */
4310 for (sav = LIST_FIRST(&sah->savtree[SADB_SASTATE_DYING]);
4314 nextsav = LIST_NEXT(sav, chain);
4316 /* we don't need to check. */
4317 if (sav->lft_h == NULL)
4321 if (sav->lft_c == NULL) {
4322 ipseclog((LOG_DEBUG, "key_timehandler: "
4323 "There is no CURRENT time, why?\n"));
4327 if (sav->lft_h->sadb_lifetime_addtime != 0
4328 && tv.tv_sec - sav->created > sav->lft_h->sadb_lifetime_addtime) {
4329 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4333 #if 0 /* XXX Should we keep to send expire message until HARD lifetime ? */
4334 else if (sav->lft_s != NULL
4335 && sav->lft_s->sadb_lifetime_addtime != 0
4336 && tv.tv_sec - sav->created > sav->lft_s->sadb_lifetime_addtime) {
4338 * XXX: should be checked to be
4339 * installed the valid SA.
4343 * If there is no SA then sending
4349 /* check HARD lifetime by bytes */
4350 else if (sav->lft_h->sadb_lifetime_bytes != 0
4351 && sav->lft_h->sadb_lifetime_bytes < sav->lft_c->sadb_lifetime_bytes) {
4352 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4358 /* delete entry in DEAD */
4359 for (sav = LIST_FIRST(&sah->savtree[SADB_SASTATE_DEAD]);
4363 nextsav = LIST_NEXT(sav, chain);
4366 if (sav->state != SADB_SASTATE_DEAD) {
4367 ipseclog((LOG_DEBUG, "key_timehandler: "
4368 "invalid sav->state "
4369 "(queue: %d SA: %d): "
4371 SADB_SASTATE_DEAD, sav->state));
4375 * do not call key_freesav() here.
4376 * sav should already be freed, and sav->refcnt
4377 * shows other references to sav
4378 * (such as from SPD).
4384 #ifndef IPSEC_NONBLOCK_ACQUIRE
4387 struct secacq *acq, *nextacq;
4389 for (acq = LIST_FIRST(&acqtree);
4393 nextacq = LIST_NEXT(acq, chain);
4395 if (tv.tv_sec - acq->created > key_blockacq_lifetime
4396 && __LIST_CHAINED(acq)) {
4397 LIST_REMOVE(acq, chain);
4406 struct secspacq *acq, *nextacq;
4408 for (acq = LIST_FIRST(&spacqtree);
4412 nextacq = LIST_NEXT(acq, chain);
4414 if (tv.tv_sec - acq->created > key_blockacq_lifetime
4415 && __LIST_CHAINED(acq)) {
4416 LIST_REMOVE(acq, chain);
4422 /* initialize random seed */
4423 if (key_tick_init_random++ > key_int_random) {
4424 key_tick_init_random = 0;
4428 #ifndef IPSEC_DEBUG2
4429 /* do exchange to tick time !! */
4430 (void)timeout((void *)key_timehandler, (void *)0, hz);
4431 #endif /* IPSEC_DEBUG2 */
4438 * to initialize a seed for random()
4447 srandom(tv.tv_usec);
4457 key_randomfill(&value, sizeof(value));
4462 key_randomfill(p, l)
4468 static int warn = 1;
4471 n = (size_t)read_random(p, (u_int)l);
4475 bcopy(&v, (u_int8_t *)p + n,
4476 l - n < sizeof(v) ? l - n : sizeof(v));
4480 printf("WARNING: pseudo-random number generator "
4481 "used for IPsec processing\n");
4488 * map SADB_SATYPE_* to IPPROTO_*.
4489 * if satype == SADB_SATYPE then satype is mapped to ~0.
4491 * 0: invalid satype.
4494 key_satype2proto(satype)
4498 case SADB_SATYPE_UNSPEC:
4499 return IPSEC_PROTO_ANY;
4500 case SADB_SATYPE_AH:
4502 case SADB_SATYPE_ESP:
4504 case SADB_X_SATYPE_IPCOMP:
4505 return IPPROTO_IPCOMP;
4514 * map IPPROTO_* to SADB_SATYPE_*
4516 * 0: invalid protocol type.
4519 key_proto2satype(proto)
4524 return SADB_SATYPE_AH;
4526 return SADB_SATYPE_ESP;
4527 case IPPROTO_IPCOMP:
4528 return SADB_X_SATYPE_IPCOMP;
4538 * SADB_GETSPI processing is to receive
4539 * <base, (SA2), src address, dst address, (SPI range)>
4540 * from the IKMPd, to assign a unique spi value, to hang on the INBOUND
4541 * tree with the status of LARVAL, and send
4542 * <base, SA(*), address(SD)>
4545 * IN: mhp: pointer to the pointer to each header.
4546 * OUT: NULL if fail.
4547 * other if success, return pointer to the message to send.
4550 key_getspi(so, m, mhp)
4553 const struct sadb_msghdr *mhp;
4555 struct sadb_address *src0, *dst0;
4556 struct secasindex saidx;
4557 struct secashead *newsah;
4558 struct secasvar *newsav;
4566 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
4567 panic("key_getspi: NULL pointer is passed.\n");
4569 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
4570 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
4571 ipseclog((LOG_DEBUG, "key_getspi: invalid message is passed.\n"));
4572 return key_senderror(so, m, EINVAL);
4574 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
4575 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
4576 ipseclog((LOG_DEBUG, "key_getspi: invalid message is passed.\n"));
4577 return key_senderror(so, m, EINVAL);
4579 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
4580 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
4581 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
4583 mode = IPSEC_MODE_ANY;
4587 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
4588 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
4590 /* map satype to proto */
4591 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
4592 ipseclog((LOG_DEBUG, "key_getspi: invalid satype is passed.\n"));
4593 return key_senderror(so, m, EINVAL);
4596 /* make sure if port number is zero. */
4597 switch (((struct sockaddr *)(src0 + 1))->sa_family) {
4599 if (((struct sockaddr *)(src0 + 1))->sa_len !=
4600 sizeof(struct sockaddr_in))
4601 return key_senderror(so, m, EINVAL);
4602 ((struct sockaddr_in *)(src0 + 1))->sin_port = 0;
4605 if (((struct sockaddr *)(src0 + 1))->sa_len !=
4606 sizeof(struct sockaddr_in6))
4607 return key_senderror(so, m, EINVAL);
4608 ((struct sockaddr_in6 *)(src0 + 1))->sin6_port = 0;
4613 switch (((struct sockaddr *)(dst0 + 1))->sa_family) {
4615 if (((struct sockaddr *)(dst0 + 1))->sa_len !=
4616 sizeof(struct sockaddr_in))
4617 return key_senderror(so, m, EINVAL);
4618 ((struct sockaddr_in *)(dst0 + 1))->sin_port = 0;
4621 if (((struct sockaddr *)(dst0 + 1))->sa_len !=
4622 sizeof(struct sockaddr_in6))
4623 return key_senderror(so, m, EINVAL);
4624 ((struct sockaddr_in6 *)(dst0 + 1))->sin6_port = 0;
4630 /* XXX boundary check against sa_len */
4631 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
4633 /* SPI allocation */
4634 spi = key_do_getnewspi((struct sadb_spirange *)mhp->ext[SADB_EXT_SPIRANGE],
4637 return key_senderror(so, m, EINVAL);
4639 /* get a SA index */
4640 if ((newsah = key_getsah(&saidx)) == NULL) {
4641 /* create a new SA index */
4642 if ((newsah = key_newsah(&saidx)) == NULL) {
4643 ipseclog((LOG_DEBUG, "key_getspi: No more memory.\n"));
4644 return key_senderror(so, m, ENOBUFS);
4650 newsav = key_newsav(m, mhp, newsah, &error);
4651 if (newsav == NULL) {
4652 /* XXX don't free new SA index allocated in above. */
4653 return key_senderror(so, m, error);
4657 newsav->spi = htonl(spi);
4659 #ifndef IPSEC_NONBLOCK_ACQUIRE
4660 /* delete the entry in acqtree */
4661 if (mhp->msg->sadb_msg_seq != 0) {
4663 if ((acq = key_getacqbyseq(mhp->msg->sadb_msg_seq)) != NULL) {
4664 /* reset counter in order to deletion by timehandler. */
4667 acq->created = tv.tv_sec;
4674 struct mbuf *n, *nn;
4675 struct sadb_sa *m_sa;
4676 struct sadb_msg *newmsg;
4679 /* create new sadb_msg to reply. */
4680 len = PFKEY_ALIGN8(sizeof(struct sadb_msg)) +
4681 PFKEY_ALIGN8(sizeof(struct sadb_sa));
4683 return key_senderror(so, m, ENOBUFS);
4685 MGETHDR(n, M_DONTWAIT, MT_DATA);
4687 MCLGET(n, M_DONTWAIT);
4688 if ((n->m_flags & M_EXT) == 0) {
4694 return key_senderror(so, m, ENOBUFS);
4700 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
4701 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
4703 m_sa = (struct sadb_sa *)(mtod(n, caddr_t) + off);
4704 m_sa->sadb_sa_len = PFKEY_UNIT64(sizeof(struct sadb_sa));
4705 m_sa->sadb_sa_exttype = SADB_EXT_SA;
4706 m_sa->sadb_sa_spi = htonl(spi);
4707 off += PFKEY_ALIGN8(sizeof(struct sadb_sa));
4711 panic("length inconsistency in key_getspi");
4714 n->m_next = key_gather_mbuf(m, mhp, 0, 2, SADB_EXT_ADDRESS_SRC,
4715 SADB_EXT_ADDRESS_DST);
4718 return key_senderror(so, m, ENOBUFS);
4721 if (n->m_len < sizeof(struct sadb_msg)) {
4722 n = m_pullup(n, sizeof(struct sadb_msg));
4724 return key_sendup_mbuf(so, m, KEY_SENDUP_ONE);
4727 n->m_pkthdr.len = 0;
4728 for (nn = n; nn; nn = nn->m_next)
4729 n->m_pkthdr.len += nn->m_len;
4731 newmsg = mtod(n, struct sadb_msg *);
4732 newmsg->sadb_msg_seq = newsav->seq;
4733 newmsg->sadb_msg_errno = 0;
4734 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
4737 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
4742 * allocating new SPI
4743 * called by key_getspi().
4749 key_do_getnewspi(spirange, saidx)
4750 struct sadb_spirange *spirange;
4751 struct secasindex *saidx;
4755 int count = key_spi_trycnt;
4757 /* set spi range to allocate */
4758 if (spirange != NULL) {
4759 min = spirange->sadb_spirange_min;
4760 max = spirange->sadb_spirange_max;
4762 min = key_spi_minval;
4763 max = key_spi_maxval;
4765 /* IPCOMP needs 2-byte SPI */
4766 if (saidx->proto == IPPROTO_IPCOMP) {
4773 t = min; min = max; max = t;
4778 if (key_checkspidup(saidx, min) != NULL) {
4779 ipseclog((LOG_DEBUG, "key_do_getnewspi: SPI %u exists already.\n", min));
4783 count--; /* taking one cost. */
4791 /* when requesting to allocate spi ranged */
4793 /* generate pseudo-random SPI value ranged. */
4794 newspi = min + (key_random() % (max - min + 1));
4796 if (key_checkspidup(saidx, newspi) == NULL)
4800 if (count == 0 || newspi == 0) {
4801 ipseclog((LOG_DEBUG, "key_do_getnewspi: to allocate spi is failed.\n"));
4807 keystat.getspi_count =
4808 (keystat.getspi_count + key_spi_trycnt - count) / 2;
4814 * SADB_UPDATE processing
4816 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4817 * key(AE), (identity(SD),) (sensitivity)>
4818 * from the ikmpd, and update a secasvar entry whose status is SADB_SASTATE_LARVAL.
4820 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4821 * (identity(SD),) (sensitivity)>
4824 * m will always be freed.
4827 key_update(so, m, mhp)
4830 const struct sadb_msghdr *mhp;
4832 struct sadb_sa *sa0;
4833 struct sadb_address *src0, *dst0;
4834 struct secasindex saidx;
4835 struct secashead *sah;
4836 struct secasvar *sav;
4843 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
4844 panic("key_update: NULL pointer is passed.\n");
4846 /* map satype to proto */
4847 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
4848 ipseclog((LOG_DEBUG, "key_update: invalid satype is passed.\n"));
4849 return key_senderror(so, m, EINVAL);
4852 if (mhp->ext[SADB_EXT_SA] == NULL ||
4853 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
4854 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
4855 (mhp->msg->sadb_msg_satype == SADB_SATYPE_ESP &&
4856 mhp->ext[SADB_EXT_KEY_ENCRYPT] == NULL) ||
4857 (mhp->msg->sadb_msg_satype == SADB_SATYPE_AH &&
4858 mhp->ext[SADB_EXT_KEY_AUTH] == NULL) ||
4859 (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL &&
4860 mhp->ext[SADB_EXT_LIFETIME_SOFT] == NULL) ||
4861 (mhp->ext[SADB_EXT_LIFETIME_HARD] == NULL &&
4862 mhp->ext[SADB_EXT_LIFETIME_SOFT] != NULL)) {
4863 ipseclog((LOG_DEBUG, "key_update: invalid message is passed.\n"));
4864 return key_senderror(so, m, EINVAL);
4866 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
4867 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
4868 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
4869 ipseclog((LOG_DEBUG, "key_update: invalid message is passed.\n"));
4870 return key_senderror(so, m, EINVAL);
4872 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
4873 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
4874 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
4876 mode = IPSEC_MODE_ANY;
4879 /* XXX boundary checking for other extensions */
4881 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
4882 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
4883 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
4885 /* XXX boundary check against sa_len */
4886 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
4888 /* get a SA header */
4889 if ((sah = key_getsah(&saidx)) == NULL) {
4890 ipseclog((LOG_DEBUG, "key_update: no SA index found.\n"));
4891 return key_senderror(so, m, ENOENT);
4894 /* set spidx if there */
4896 error = key_setident(sah, m, mhp);
4898 return key_senderror(so, m, error);
4900 /* find a SA with sequence number. */
4901 #ifdef IPSEC_DOSEQCHECK
4902 if (mhp->msg->sadb_msg_seq != 0
4903 && (sav = key_getsavbyseq(sah, mhp->msg->sadb_msg_seq)) == NULL) {
4904 ipseclog((LOG_DEBUG,
4905 "key_update: no larval SA with sequence %u exists.\n",
4906 mhp->msg->sadb_msg_seq));
4907 return key_senderror(so, m, ENOENT);
4910 if ((sav = key_getsavbyspi(sah, sa0->sadb_sa_spi)) == NULL) {
4911 ipseclog((LOG_DEBUG,
4912 "key_update: no such a SA found (spi:%u)\n",
4913 (u_int32_t)ntohl(sa0->sadb_sa_spi)));
4914 return key_senderror(so, m, EINVAL);
4918 /* validity check */
4919 if (sav->sah->saidx.proto != proto) {
4920 ipseclog((LOG_DEBUG,
4921 "key_update: protocol mismatched (DB=%u param=%u)\n",
4922 sav->sah->saidx.proto, proto));
4923 return key_senderror(so, m, EINVAL);
4925 #ifdef IPSEC_DOSEQCHECK
4926 if (sav->spi != sa0->sadb_sa_spi) {
4927 ipseclog((LOG_DEBUG,
4928 "key_update: SPI mismatched (DB:%u param:%u)\n",
4929 (u_int32_t)ntohl(sav->spi),
4930 (u_int32_t)ntohl(sa0->sadb_sa_spi)));
4931 return key_senderror(so, m, EINVAL);
4934 if (sav->pid != mhp->msg->sadb_msg_pid) {
4935 ipseclog((LOG_DEBUG,
4936 "key_update: pid mismatched (DB:%u param:%u)\n",
4937 sav->pid, mhp->msg->sadb_msg_pid));
4938 return key_senderror(so, m, EINVAL);
4941 /* copy sav values */
4942 error = key_setsaval(sav, m, mhp);
4945 return key_senderror(so, m, error);
4948 /* check SA values to be mature. */
4949 if ((mhp->msg->sadb_msg_errno = key_mature(sav)) != 0) {
4951 return key_senderror(so, m, 0);
4957 /* set msg buf from mhp */
4958 n = key_getmsgbuf_x1(m, mhp);
4960 ipseclog((LOG_DEBUG, "key_update: No more memory.\n"));
4961 return key_senderror(so, m, ENOBUFS);
4965 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
4970 * search SAD with sequence for a SA which state is SADB_SASTATE_LARVAL.
4971 * only called by key_update().
4974 * others : found, pointer to a SA.
4976 #ifdef IPSEC_DOSEQCHECK
4977 static struct secasvar *
4978 key_getsavbyseq(sah, seq)
4979 struct secashead *sah;
4982 struct secasvar *sav;
4985 state = SADB_SASTATE_LARVAL;
4987 /* search SAD with sequence number ? */
4988 LIST_FOREACH(sav, &sah->savtree[state], chain) {
4990 KEY_CHKSASTATE(state, sav->state, "key_getsabyseq");
4992 if (sav->seq == seq) {
4994 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
4995 printf("DP key_getsavbyseq cause "
4996 "refcnt++:%d SA:%p\n",
5007 * SADB_ADD processing
5008 * add a entry to SA database, when received
5009 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
5010 * key(AE), (identity(SD),) (sensitivity)>
5013 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
5014 * (identity(SD),) (sensitivity)>
5017 * IGNORE identity and sensitivity messages.
5019 * m will always be freed.
5025 const struct sadb_msghdr *mhp;
5027 struct sadb_sa *sa0;
5028 struct sadb_address *src0, *dst0;
5029 struct secasindex saidx;
5030 struct secashead *newsah;
5031 struct secasvar *newsav;
5038 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
5039 panic("key_add: NULL pointer is passed.\n");
5041 /* map satype to proto */
5042 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5043 ipseclog((LOG_DEBUG, "key_add: invalid satype is passed.\n"));
5044 return key_senderror(so, m, EINVAL);
5047 if (mhp->ext[SADB_EXT_SA] == NULL ||
5048 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5049 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
5050 (mhp->msg->sadb_msg_satype == SADB_SATYPE_ESP &&
5051 mhp->ext[SADB_EXT_KEY_ENCRYPT] == NULL) ||
5052 (mhp->msg->sadb_msg_satype == SADB_SATYPE_AH &&
5053 mhp->ext[SADB_EXT_KEY_AUTH] == NULL) ||
5054 (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL &&
5055 mhp->ext[SADB_EXT_LIFETIME_SOFT] == NULL) ||
5056 (mhp->ext[SADB_EXT_LIFETIME_HARD] == NULL &&
5057 mhp->ext[SADB_EXT_LIFETIME_SOFT] != NULL)) {
5058 ipseclog((LOG_DEBUG, "key_add: invalid message is passed.\n"));
5059 return key_senderror(so, m, EINVAL);
5061 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
5062 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5063 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5065 ipseclog((LOG_DEBUG, "key_add: invalid message is passed.\n"));
5066 return key_senderror(so, m, EINVAL);
5068 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
5069 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
5070 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
5072 mode = IPSEC_MODE_ANY;
5076 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5077 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
5078 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
5080 /* XXX boundary check against sa_len */
5081 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
5083 /* get a SA header */
5084 if ((newsah = key_getsah(&saidx)) == NULL) {
5085 /* create a new SA header */
5086 if ((newsah = key_newsah(&saidx)) == NULL) {
5087 ipseclog((LOG_DEBUG, "key_add: No more memory.\n"));
5088 return key_senderror(so, m, ENOBUFS);
5092 /* set spidx if there */
5094 error = key_setident(newsah, m, mhp);
5096 return key_senderror(so, m, error);
5099 /* create new SA entry. */
5100 /* We can create new SA only if SPI is differenct. */
5101 if (key_getsavbyspi(newsah, sa0->sadb_sa_spi)) {
5102 ipseclog((LOG_DEBUG, "key_add: SA already exists.\n"));
5103 return key_senderror(so, m, EEXIST);
5105 newsav = key_newsav(m, mhp, newsah, &error);
5106 if (newsav == NULL) {
5107 return key_senderror(so, m, error);
5110 /* check SA values to be mature. */
5111 if ((error = key_mature(newsav)) != 0) {
5112 key_freesav(newsav);
5113 return key_senderror(so, m, error);
5117 * don't call key_freesav() here, as we would like to keep the SA
5118 * in the database on success.
5124 /* set msg buf from mhp */
5125 n = key_getmsgbuf_x1(m, mhp);
5127 ipseclog((LOG_DEBUG, "key_update: No more memory.\n"));
5128 return key_senderror(so, m, ENOBUFS);
5132 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5138 key_setident(sah, m, mhp)
5139 struct secashead *sah;
5141 const struct sadb_msghdr *mhp;
5143 const struct sadb_ident *idsrc, *iddst;
5144 int idsrclen, iddstlen;
5147 if (sah == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
5148 panic("key_setident: NULL pointer is passed.\n");
5150 /* don't make buffer if not there */
5151 if (mhp->ext[SADB_EXT_IDENTITY_SRC] == NULL &&
5152 mhp->ext[SADB_EXT_IDENTITY_DST] == NULL) {
5158 if (mhp->ext[SADB_EXT_IDENTITY_SRC] == NULL ||
5159 mhp->ext[SADB_EXT_IDENTITY_DST] == NULL) {
5160 ipseclog((LOG_DEBUG, "key_setident: invalid identity.\n"));
5164 idsrc = (const struct sadb_ident *)mhp->ext[SADB_EXT_IDENTITY_SRC];
5165 iddst = (const struct sadb_ident *)mhp->ext[SADB_EXT_IDENTITY_DST];
5166 idsrclen = mhp->extlen[SADB_EXT_IDENTITY_SRC];
5167 iddstlen = mhp->extlen[SADB_EXT_IDENTITY_DST];
5169 /* validity check */
5170 if (idsrc->sadb_ident_type != iddst->sadb_ident_type) {
5171 ipseclog((LOG_DEBUG, "key_setident: ident type mismatch.\n"));
5175 switch (idsrc->sadb_ident_type) {
5176 case SADB_IDENTTYPE_PREFIX:
5177 case SADB_IDENTTYPE_FQDN:
5178 case SADB_IDENTTYPE_USERFQDN:
5180 /* XXX do nothing */
5186 /* make structure */
5187 KMALLOC(sah->idents, struct sadb_ident *, idsrclen);
5188 if (sah->idents == NULL) {
5189 ipseclog((LOG_DEBUG, "key_setident: No more memory.\n"));
5192 KMALLOC(sah->identd, struct sadb_ident *, iddstlen);
5193 if (sah->identd == NULL) {
5196 ipseclog((LOG_DEBUG, "key_setident: No more memory.\n"));
5199 bcopy(idsrc, sah->idents, idsrclen);
5200 bcopy(iddst, sah->identd, iddstlen);
5206 * m will not be freed on return.
5207 * it is caller's responsibility to free the result.
5209 static struct mbuf *
5210 key_getmsgbuf_x1(m, mhp)
5212 const struct sadb_msghdr *mhp;
5217 if (m == NULL || mhp == NULL || mhp->msg == NULL)
5218 panic("key_getmsgbuf_x1: NULL pointer is passed.\n");
5220 /* create new sadb_msg to reply. */
5221 n = key_gather_mbuf(m, mhp, 1, 9, SADB_EXT_RESERVED,
5222 SADB_EXT_SA, SADB_X_EXT_SA2,
5223 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST,
5224 SADB_EXT_LIFETIME_HARD, SADB_EXT_LIFETIME_SOFT,
5225 SADB_EXT_IDENTITY_SRC, SADB_EXT_IDENTITY_DST);
5229 if (n->m_len < sizeof(struct sadb_msg)) {
5230 n = m_pullup(n, sizeof(struct sadb_msg));
5234 mtod(n, struct sadb_msg *)->sadb_msg_errno = 0;
5235 mtod(n, struct sadb_msg *)->sadb_msg_len =
5236 PFKEY_UNIT64(n->m_pkthdr.len);
5241 static int key_delete_all (struct socket *, struct mbuf *,
5242 const struct sadb_msghdr *, u_int16_t);
5245 * SADB_DELETE processing
5247 * <base, SA(*), address(SD)>
5248 * from the ikmpd, and set SADB_SASTATE_DEAD,
5250 * <base, SA(*), address(SD)>
5253 * m will always be freed.
5256 key_delete(so, m, mhp)
5259 const struct sadb_msghdr *mhp;
5261 struct sadb_sa *sa0;
5262 struct sadb_address *src0, *dst0;
5263 struct secasindex saidx;
5264 struct secashead *sah;
5265 struct secasvar *sav = NULL;
5269 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
5270 panic("key_delete: NULL pointer is passed.\n");
5272 /* map satype to proto */
5273 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5274 ipseclog((LOG_DEBUG, "key_delete: invalid satype is passed.\n"));
5275 return key_senderror(so, m, EINVAL);
5278 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5279 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
5280 ipseclog((LOG_DEBUG, "key_delete: invalid message is passed.\n"));
5281 return key_senderror(so, m, EINVAL);
5284 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5285 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5286 ipseclog((LOG_DEBUG, "key_delete: invalid message is passed.\n"));
5287 return key_senderror(so, m, EINVAL);
5290 if (mhp->ext[SADB_EXT_SA] == NULL) {
5292 * Caller wants us to delete all non-LARVAL SAs
5293 * that match the src/dst. This is used during
5294 * IKE INITIAL-CONTACT.
5296 ipseclog((LOG_DEBUG, "key_delete: doing delete all.\n"));
5297 return key_delete_all(so, m, mhp, proto);
5298 } else if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa)) {
5299 ipseclog((LOG_DEBUG, "key_delete: invalid message is passed.\n"));
5300 return key_senderror(so, m, EINVAL);
5303 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5304 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
5305 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
5307 /* XXX boundary check against sa_len */
5308 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5310 /* get a SA header */
5311 LIST_FOREACH(sah, &sahtree, chain) {
5312 if (sah->state == SADB_SASTATE_DEAD)
5314 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5317 /* get a SA with SPI. */
5318 sav = key_getsavbyspi(sah, sa0->sadb_sa_spi);
5323 ipseclog((LOG_DEBUG, "key_delete: no SA found.\n"));
5324 return key_senderror(so, m, ENOENT);
5327 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
5333 struct sadb_msg *newmsg;
5335 /* create new sadb_msg to reply. */
5336 n = key_gather_mbuf(m, mhp, 1, 4, SADB_EXT_RESERVED,
5337 SADB_EXT_SA, SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
5339 return key_senderror(so, m, ENOBUFS);
5341 if (n->m_len < sizeof(struct sadb_msg)) {
5342 n = m_pullup(n, sizeof(struct sadb_msg));
5344 return key_senderror(so, m, ENOBUFS);
5346 newmsg = mtod(n, struct sadb_msg *);
5347 newmsg->sadb_msg_errno = 0;
5348 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
5351 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5356 * delete all SAs for src/dst. Called from key_delete().
5359 key_delete_all(so, m, mhp, proto)
5362 const struct sadb_msghdr *mhp;
5365 struct sadb_address *src0, *dst0;
5366 struct secasindex saidx;
5367 struct secashead *sah;
5368 struct secasvar *sav, *nextsav;
5369 u_int stateidx, state;
5371 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
5372 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
5374 /* XXX boundary check against sa_len */
5375 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5377 LIST_FOREACH(sah, &sahtree, chain) {
5378 if (sah->state == SADB_SASTATE_DEAD)
5380 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5383 /* Delete all non-LARVAL SAs. */
5385 stateidx < _ARRAYLEN(saorder_state_alive);
5387 state = saorder_state_alive[stateidx];
5388 if (state == SADB_SASTATE_LARVAL)
5390 for (sav = LIST_FIRST(&sah->savtree[state]);
5391 sav != NULL; sav = nextsav) {
5392 nextsav = LIST_NEXT(sav, chain);
5394 if (sav->state != state) {
5395 ipseclog((LOG_DEBUG, "key_delete_all: "
5396 "invalid sav->state "
5397 "(queue: %d SA: %d)\n",
5398 state, sav->state));
5402 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
5409 struct sadb_msg *newmsg;
5411 /* create new sadb_msg to reply. */
5412 n = key_gather_mbuf(m, mhp, 1, 3, SADB_EXT_RESERVED,
5413 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
5415 return key_senderror(so, m, ENOBUFS);
5417 if (n->m_len < sizeof(struct sadb_msg)) {
5418 n = m_pullup(n, sizeof(struct sadb_msg));
5420 return key_senderror(so, m, ENOBUFS);
5422 newmsg = mtod(n, struct sadb_msg *);
5423 newmsg->sadb_msg_errno = 0;
5424 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
5427 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5432 * SADB_GET processing
5434 * <base, SA(*), address(SD)>
5435 * from the ikmpd, and get a SP and a SA to respond,
5437 * <base, SA, (lifetime(HSC),) address(SD), (address(P),) key(AE),
5438 * (identity(SD),) (sensitivity)>
5441 * m will always be freed.
5447 const struct sadb_msghdr *mhp;
5449 struct sadb_sa *sa0;
5450 struct sadb_address *src0, *dst0;
5451 struct secasindex saidx;
5452 struct secashead *sah;
5453 struct secasvar *sav = NULL;
5457 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
5458 panic("key_get: NULL pointer is passed.\n");
5460 /* map satype to proto */
5461 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5462 ipseclog((LOG_DEBUG, "key_get: invalid satype is passed.\n"));
5463 return key_senderror(so, m, EINVAL);
5466 if (mhp->ext[SADB_EXT_SA] == NULL ||
5467 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5468 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
5469 ipseclog((LOG_DEBUG, "key_get: invalid message is passed.\n"));
5470 return key_senderror(so, m, EINVAL);
5472 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
5473 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5474 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5475 ipseclog((LOG_DEBUG, "key_get: invalid message is passed.\n"));
5476 return key_senderror(so, m, EINVAL);
5479 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5480 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
5481 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
5483 /* XXX boundary check against sa_len */
5484 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5486 /* get a SA header */
5487 LIST_FOREACH(sah, &sahtree, chain) {
5488 if (sah->state == SADB_SASTATE_DEAD)
5490 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5493 /* get a SA with SPI. */
5494 sav = key_getsavbyspi(sah, sa0->sadb_sa_spi);
5499 ipseclog((LOG_DEBUG, "key_get: no SA found.\n"));
5500 return key_senderror(so, m, ENOENT);
5507 /* map proto to satype */
5508 if ((satype = key_proto2satype(sah->saidx.proto)) == 0) {
5509 ipseclog((LOG_DEBUG, "key_get: there was invalid proto in SAD.\n"));
5510 return key_senderror(so, m, EINVAL);
5513 /* create new sadb_msg to reply. */
5514 n = key_setdumpsa(sav, SADB_GET, satype, mhp->msg->sadb_msg_seq,
5515 mhp->msg->sadb_msg_pid);
5517 return key_senderror(so, m, ENOBUFS);
5520 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
5524 /* XXX make it sysctl-configurable? */
5526 key_getcomb_setlifetime(comb)
5527 struct sadb_comb *comb;
5530 comb->sadb_comb_soft_allocations = 1;
5531 comb->sadb_comb_hard_allocations = 1;
5532 comb->sadb_comb_soft_bytes = 0;
5533 comb->sadb_comb_hard_bytes = 0;
5534 comb->sadb_comb_hard_addtime = 86400; /* 1 day */
5535 comb->sadb_comb_soft_addtime = comb->sadb_comb_soft_addtime * 80 / 100;
5536 comb->sadb_comb_soft_usetime = 28800; /* 8 hours */
5537 comb->sadb_comb_hard_usetime = comb->sadb_comb_hard_usetime * 80 / 100;
5542 * XXX reorder combinations by preference
5543 * XXX no idea if the user wants ESP authentication or not
5545 static struct mbuf *
5548 struct sadb_comb *comb;
5549 const struct esp_algorithm *algo;
5550 struct mbuf *result = NULL, *m, *n;
5554 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
5557 for (i = 1; i <= SADB_EALG_MAX; i++) {
5558 algo = esp_algorithm_lookup(i);
5562 if (algo->keymax < ipsec_esp_keymin)
5564 if (algo->keymin < ipsec_esp_keymin)
5565 encmin = ipsec_esp_keymin;
5567 encmin = algo->keymin;
5570 m = key_getcomb_ah();
5574 panic("assumption failed in key_getcomb_esp");
5576 MGET(m, M_DONTWAIT, MT_DATA);
5581 bzero(mtod(m, caddr_t), m->m_len);
5588 for (n = m; n; n = n->m_next)
5592 panic("assumption failed in key_getcomb_esp");
5595 for (off = 0; off < totlen; off += l) {
5596 n = m_pulldown(m, off, l, &o);
5598 /* m is already freed */
5601 comb = (struct sadb_comb *)(mtod(n, caddr_t) + o);
5602 bzero(comb, sizeof(*comb));
5603 key_getcomb_setlifetime(comb);
5604 comb->sadb_comb_encrypt = i;
5605 comb->sadb_comb_encrypt_minbits = encmin;
5606 comb->sadb_comb_encrypt_maxbits = algo->keymax;
5625 * XXX reorder combinations by preference
5627 static struct mbuf *
5630 struct sadb_comb *comb;
5631 const struct ah_algorithm *algo;
5635 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
5638 for (i = 1; i <= SADB_AALG_MAX; i++) {
5640 /* we prefer HMAC algorithms, not old algorithms */
5641 if (i != SADB_AALG_SHA1HMAC && i != SADB_AALG_MD5HMAC)
5644 algo = ah_algorithm_lookup(i);
5648 if (algo->keymax < ipsec_ah_keymin)
5650 if (algo->keymin < ipsec_ah_keymin)
5651 min = ipsec_ah_keymin;
5658 panic("assumption failed in key_getcomb_ah");
5660 MGET(m, M_DONTWAIT, MT_DATA);
5667 M_PREPEND(m, l, M_DONTWAIT);
5671 comb = mtod(m, struct sadb_comb *);
5672 bzero(comb, sizeof(*comb));
5673 key_getcomb_setlifetime(comb);
5674 comb->sadb_comb_auth = i;
5675 comb->sadb_comb_auth_minbits = min;
5676 comb->sadb_comb_auth_maxbits = algo->keymax;
5683 * not really an official behavior. discussed in pf_key@inner.net in Sep2000.
5684 * XXX reorder combinations by preference
5686 static struct mbuf *
5687 key_getcomb_ipcomp()
5689 struct sadb_comb *comb;
5690 const struct ipcomp_algorithm *algo;
5693 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
5696 for (i = 1; i <= SADB_X_CALG_MAX; i++) {
5697 algo = ipcomp_algorithm_lookup(i);
5704 panic("assumption failed in key_getcomb_ipcomp");
5706 MGET(m, M_DONTWAIT, MT_DATA);
5713 M_PREPEND(m, l, M_DONTWAIT);
5717 comb = mtod(m, struct sadb_comb *);
5718 bzero(comb, sizeof(*comb));
5719 key_getcomb_setlifetime(comb);
5720 comb->sadb_comb_encrypt = i;
5721 /* what should we set into sadb_comb_*_{min,max}bits? */
5728 * XXX no way to pass mode (transport/tunnel) to userland
5729 * XXX replay checking?
5730 * XXX sysctl interface to ipsec_{ah,esp}_keymin
5732 static struct mbuf *
5734 const struct secasindex *saidx;
5736 struct sadb_prop *prop;
5738 const int l = PFKEY_ALIGN8(sizeof(struct sadb_prop));
5741 switch (saidx->proto) {
5744 m = key_getcomb_esp();
5748 m = key_getcomb_ah();
5750 case IPPROTO_IPCOMP:
5751 m = key_getcomb_ipcomp();
5759 M_PREPEND(m, l, M_DONTWAIT);
5764 for (n = m; n; n = n->m_next)
5767 prop = mtod(m, struct sadb_prop *);
5768 bzero(prop, sizeof(*prop));
5769 prop->sadb_prop_len = PFKEY_UNIT64(totlen);
5770 prop->sadb_prop_exttype = SADB_EXT_PROPOSAL;
5771 prop->sadb_prop_replay = 32; /* XXX */
5777 * SADB_ACQUIRE processing called by key_checkrequest() and key_acquire2().
5779 * <base, SA, address(SD), (address(P)), x_policy,
5780 * (identity(SD),) (sensitivity,) proposal>
5781 * to KMD, and expect to receive
5782 * <base> with SADB_ACQUIRE if error occured,
5784 * <base, src address, dst address, (SPI range)> with SADB_GETSPI
5785 * from KMD by PF_KEY.
5787 * XXX x_policy is outside of RFC2367 (KAME extension).
5788 * XXX sensitivity is not supported.
5789 * XXX for ipcomp, RFC2367 does not define how to fill in proposal.
5790 * see comment for key_getcomb_ipcomp().
5794 * others: error number
5797 key_acquire(saidx, sp)
5798 struct secasindex *saidx;
5799 struct secpolicy *sp;
5801 struct mbuf *result = NULL, *m;
5802 #ifndef IPSEC_NONBLOCK_ACQUIRE
5803 struct secacq *newacq;
5811 panic("key_acquire: NULL pointer is passed.\n");
5812 if ((satype = key_proto2satype(saidx->proto)) == 0)
5813 panic("key_acquire: invalid proto is passed.\n");
5815 #ifndef IPSEC_NONBLOCK_ACQUIRE
5817 * We never do anything about acquirng SA. There is anather
5818 * solution that kernel blocks to send SADB_ACQUIRE message until
5819 * getting something message from IKEd. In later case, to be
5820 * managed with ACQUIRING list.
5822 /* get a entry to check whether sending message or not. */
5823 if ((newacq = key_getacq(saidx)) != NULL) {
5824 if (key_blockacq_count < newacq->count) {
5825 /* reset counter and do send message. */
5828 /* increment counter and do nothing. */
5833 /* make new entry for blocking to send SADB_ACQUIRE. */
5834 if ((newacq = key_newacq(saidx)) == NULL)
5837 /* add to acqtree */
5838 LIST_INSERT_HEAD(&acqtree, newacq, chain);
5843 #ifndef IPSEC_NONBLOCK_ACQUIRE
5846 seq = (acq_seq = (acq_seq == ~0 ? 1 : ++acq_seq));
5848 m = key_setsadbmsg(SADB_ACQUIRE, 0, satype, seq, 0, 0);
5855 /* set sadb_address for saidx's. */
5856 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
5857 (struct sockaddr *)&saidx->src, FULLMASK, IPSEC_ULPROTO_ANY);
5864 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
5865 (struct sockaddr *)&saidx->dst, FULLMASK, IPSEC_ULPROTO_ANY);
5872 /* XXX proxy address (optional) */
5874 /* set sadb_x_policy */
5876 m = key_setsadbxpolicy(sp->policy, sp->spidx.dir, sp->id);
5884 /* XXX identity (optional) */
5886 if (idexttype && fqdn) {
5887 /* create identity extension (FQDN) */
5888 struct sadb_ident *id;
5891 fqdnlen = strlen(fqdn) + 1; /* +1 for terminating-NUL */
5892 id = (struct sadb_ident *)p;
5893 bzero(id, sizeof(*id) + PFKEY_ALIGN8(fqdnlen));
5894 id->sadb_ident_len = PFKEY_UNIT64(sizeof(*id) + PFKEY_ALIGN8(fqdnlen));
5895 id->sadb_ident_exttype = idexttype;
5896 id->sadb_ident_type = SADB_IDENTTYPE_FQDN;
5897 bcopy(fqdn, id + 1, fqdnlen);
5898 p += sizeof(struct sadb_ident) + PFKEY_ALIGN8(fqdnlen);
5902 /* create identity extension (USERFQDN) */
5903 struct sadb_ident *id;
5907 /* +1 for terminating-NUL */
5908 userfqdnlen = strlen(userfqdn) + 1;
5911 id = (struct sadb_ident *)p;
5912 bzero(id, sizeof(*id) + PFKEY_ALIGN8(userfqdnlen));
5913 id->sadb_ident_len = PFKEY_UNIT64(sizeof(*id) + PFKEY_ALIGN8(userfqdnlen));
5914 id->sadb_ident_exttype = idexttype;
5915 id->sadb_ident_type = SADB_IDENTTYPE_USERFQDN;
5916 /* XXX is it correct? */
5917 if (curproc && curproc->p_cred)
5918 id->sadb_ident_id = curproc->p_cred->p_ruid;
5919 if (userfqdn && userfqdnlen)
5920 bcopy(userfqdn, id + 1, userfqdnlen);
5921 p += sizeof(struct sadb_ident) + PFKEY_ALIGN8(userfqdnlen);
5925 /* XXX sensitivity (optional) */
5927 /* create proposal/combination extension */
5928 m = key_getprop(saidx);
5931 * spec conformant: always attach proposal/combination extension,
5932 * the problem is that we have no way to attach it for ipcomp,
5933 * due to the way sadb_comb is declared in RFC2367.
5942 * outside of spec; make proposal/combination extension optional.
5948 if ((result->m_flags & M_PKTHDR) == 0) {
5953 if (result->m_len < sizeof(struct sadb_msg)) {
5954 result = m_pullup(result, sizeof(struct sadb_msg));
5955 if (result == NULL) {
5961 result->m_pkthdr.len = 0;
5962 for (m = result; m; m = m->m_next)
5963 result->m_pkthdr.len += m->m_len;
5965 mtod(result, struct sadb_msg *)->sadb_msg_len =
5966 PFKEY_UNIT64(result->m_pkthdr.len);
5968 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
5976 #ifndef IPSEC_NONBLOCK_ACQUIRE
5977 static struct secacq *
5979 struct secasindex *saidx;
5981 struct secacq *newacq;
5985 KMALLOC(newacq, struct secacq *, sizeof(struct secacq));
5986 if (newacq == NULL) {
5987 ipseclog((LOG_DEBUG, "key_newacq: No more memory.\n"));
5990 bzero(newacq, sizeof(*newacq));
5993 bcopy(saidx, &newacq->saidx, sizeof(newacq->saidx));
5994 newacq->seq = (acq_seq == ~0 ? 1 : ++acq_seq);
5996 newacq->created = tv.tv_sec;
6002 static struct secacq *
6004 struct secasindex *saidx;
6008 LIST_FOREACH(acq, &acqtree, chain) {
6009 if (key_cmpsaidx(saidx, &acq->saidx, CMP_EXACTLY))
6016 static struct secacq *
6017 key_getacqbyseq(seq)
6022 LIST_FOREACH(acq, &acqtree, chain) {
6023 if (acq->seq == seq)
6031 static struct secspacq *
6033 struct secpolicyindex *spidx;
6035 struct secspacq *acq;
6039 KMALLOC(acq, struct secspacq *, sizeof(struct secspacq));
6041 ipseclog((LOG_DEBUG, "key_newspacq: No more memory.\n"));
6044 bzero(acq, sizeof(*acq));
6047 bcopy(spidx, &acq->spidx, sizeof(acq->spidx));
6049 acq->created = tv.tv_sec;
6055 static struct secspacq *
6057 struct secpolicyindex *spidx;
6059 struct secspacq *acq;
6061 LIST_FOREACH(acq, &spacqtree, chain) {
6062 if (key_cmpspidx_exactly(spidx, &acq->spidx))
6070 * SADB_ACQUIRE processing,
6071 * in first situation, is receiving
6073 * from the ikmpd, and clear sequence of its secasvar entry.
6075 * In second situation, is receiving
6076 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
6077 * from a user land process, and return
6078 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
6081 * m will always be freed.
6084 key_acquire2(so, m, mhp)
6087 const struct sadb_msghdr *mhp;
6089 const struct sadb_address *src0, *dst0;
6090 struct secasindex saidx;
6091 struct secashead *sah;
6096 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
6097 panic("key_acquire2: NULL pointer is passed.\n");
6100 * Error message from KMd.
6101 * We assume that if error was occured in IKEd, the length of PFKEY
6102 * message is equal to the size of sadb_msg structure.
6103 * We do not raise error even if error occured in this function.
6105 if (mhp->msg->sadb_msg_len == PFKEY_UNIT64(sizeof(struct sadb_msg))) {
6106 #ifndef IPSEC_NONBLOCK_ACQUIRE
6110 /* check sequence number */
6111 if (mhp->msg->sadb_msg_seq == 0) {
6112 ipseclog((LOG_DEBUG, "key_acquire2: must specify sequence number.\n"));
6117 if ((acq = key_getacqbyseq(mhp->msg->sadb_msg_seq)) == NULL) {
6119 * the specified larval SA is already gone, or we got
6120 * a bogus sequence number. we can silently ignore it.
6126 /* reset acq counter in order to deletion by timehander. */
6128 acq->created = tv.tv_sec;
6136 * This message is from user land.
6139 /* map satype to proto */
6140 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
6141 ipseclog((LOG_DEBUG, "key_acquire2: invalid satype is passed.\n"));
6142 return key_senderror(so, m, EINVAL);
6145 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
6146 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
6147 mhp->ext[SADB_EXT_PROPOSAL] == NULL) {
6149 ipseclog((LOG_DEBUG, "key_acquire2: invalid message is passed.\n"));
6150 return key_senderror(so, m, EINVAL);
6152 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
6153 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
6154 mhp->extlen[SADB_EXT_PROPOSAL] < sizeof(struct sadb_prop)) {
6156 ipseclog((LOG_DEBUG, "key_acquire2: invalid message is passed.\n"));
6157 return key_senderror(so, m, EINVAL);
6160 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
6161 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
6163 /* XXX boundary check against sa_len */
6164 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
6166 /* get a SA index */
6167 LIST_FOREACH(sah, &sahtree, chain) {
6168 if (sah->state == SADB_SASTATE_DEAD)
6170 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_MODE_REQID))
6174 ipseclog((LOG_DEBUG, "key_acquire2: a SA exists already.\n"));
6175 return key_senderror(so, m, EEXIST);
6178 error = key_acquire(&saidx, NULL);
6180 ipseclog((LOG_DEBUG, "key_acquire2: error %d returned "
6181 "from key_acquire.\n", mhp->msg->sadb_msg_errno));
6182 return key_senderror(so, m, error);
6185 return key_sendup_mbuf(so, m, KEY_SENDUP_REGISTERED);
6189 * SADB_REGISTER processing.
6190 * If SATYPE_UNSPEC has been passed as satype, only return sabd_supported.
6193 * from the ikmpd, and register a socket to send PF_KEY messages,
6197 * If socket is detached, must free from regnode.
6199 * m will always be freed.
6202 key_register(so, m, mhp)
6205 const struct sadb_msghdr *mhp;
6207 struct secreg *reg, *newreg = 0;
6210 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
6211 panic("key_register: NULL pointer is passed.\n");
6213 /* check for invalid register message */
6214 if (mhp->msg->sadb_msg_satype >= sizeof(regtree)/sizeof(regtree[0]))
6215 return key_senderror(so, m, EINVAL);
6217 /* When SATYPE_UNSPEC is specified, only return sabd_supported. */
6218 if (mhp->msg->sadb_msg_satype == SADB_SATYPE_UNSPEC)
6221 /* check whether existing or not */
6222 LIST_FOREACH(reg, ®tree[mhp->msg->sadb_msg_satype], chain) {
6223 if (reg->so == so) {
6224 ipseclog((LOG_DEBUG, "key_register: socket exists already.\n"));
6225 return key_senderror(so, m, EEXIST);
6229 /* create regnode */
6230 KMALLOC(newreg, struct secreg *, sizeof(*newreg));
6231 if (newreg == NULL) {
6232 ipseclog((LOG_DEBUG, "key_register: No more memory.\n"));
6233 return key_senderror(so, m, ENOBUFS);
6235 bzero((caddr_t)newreg, sizeof(*newreg));
6238 ((struct keycb *)sotorawcb(so))->kp_registered++;
6240 /* add regnode to regtree. */
6241 LIST_INSERT_HEAD(®tree[mhp->msg->sadb_msg_satype], newreg, chain);
6246 struct sadb_msg *newmsg;
6247 struct sadb_supported *sup;
6248 u_int len, alen, elen;
6251 struct sadb_alg *alg;
6253 /* create new sadb_msg to reply. */
6255 for (i = 1; i <= SADB_AALG_MAX; i++) {
6256 if (ah_algorithm_lookup(i))
6257 alen += sizeof(struct sadb_alg);
6260 alen += sizeof(struct sadb_supported);
6263 for (i = 1; i <= SADB_EALG_MAX; i++) {
6264 if (esp_algorithm_lookup(i))
6265 elen += sizeof(struct sadb_alg);
6268 elen += sizeof(struct sadb_supported);
6271 len = sizeof(struct sadb_msg) + alen + elen;
6274 return key_senderror(so, m, ENOBUFS);
6276 MGETHDR(n, M_DONTWAIT, MT_DATA);
6278 MCLGET(n, M_DONTWAIT);
6279 if ((n->m_flags & M_EXT) == 0) {
6285 return key_senderror(so, m, ENOBUFS);
6287 n->m_pkthdr.len = n->m_len = len;
6291 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
6292 newmsg = mtod(n, struct sadb_msg *);
6293 newmsg->sadb_msg_errno = 0;
6294 newmsg->sadb_msg_len = PFKEY_UNIT64(len);
6295 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
6297 /* for authentication algorithm */
6299 sup = (struct sadb_supported *)(mtod(n, caddr_t) + off);
6300 sup->sadb_supported_len = PFKEY_UNIT64(alen);
6301 sup->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
6302 off += PFKEY_ALIGN8(sizeof(*sup));
6304 for (i = 1; i <= SADB_AALG_MAX; i++) {
6305 const struct ah_algorithm *aalgo;
6307 aalgo = ah_algorithm_lookup(i);
6310 alg = (struct sadb_alg *)(mtod(n, caddr_t) + off);
6311 alg->sadb_alg_id = i;
6312 alg->sadb_alg_ivlen = 0;
6313 alg->sadb_alg_minbits = aalgo->keymin;
6314 alg->sadb_alg_maxbits = aalgo->keymax;
6315 off += PFKEY_ALIGN8(sizeof(*alg));
6320 /* for encryption algorithm */
6322 sup = (struct sadb_supported *)(mtod(n, caddr_t) + off);
6323 sup->sadb_supported_len = PFKEY_UNIT64(elen);
6324 sup->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT;
6325 off += PFKEY_ALIGN8(sizeof(*sup));
6327 for (i = 1; i <= SADB_EALG_MAX; i++) {
6328 const struct esp_algorithm *ealgo;
6330 ealgo = esp_algorithm_lookup(i);
6333 alg = (struct sadb_alg *)(mtod(n, caddr_t) + off);
6334 alg->sadb_alg_id = i;
6335 if (ealgo && ealgo->ivlen) {
6337 * give NULL to get the value preferred by
6338 * algorithm XXX SADB_X_EXT_DERIV ?
6340 alg->sadb_alg_ivlen =
6341 (*ealgo->ivlen)(ealgo, NULL);
6343 alg->sadb_alg_ivlen = 0;
6344 alg->sadb_alg_minbits = ealgo->keymin;
6345 alg->sadb_alg_maxbits = ealgo->keymax;
6346 off += PFKEY_ALIGN8(sizeof(struct sadb_alg));
6353 panic("length assumption failed in key_register");
6357 return key_sendup_mbuf(so, n, KEY_SENDUP_REGISTERED);
6362 * free secreg entry registered.
6363 * XXX: I want to do free a socket marked done SADB_RESIGER to socket.
6374 panic("key_freereg: NULL pointer is passed.\n");
6377 * check whether existing or not.
6378 * check all type of SA, because there is a potential that
6379 * one socket is registered to multiple type of SA.
6381 for (i = 0; i <= SADB_SATYPE_MAX; i++) {
6382 LIST_FOREACH(reg, ®tree[i], chain) {
6384 && __LIST_CHAINED(reg)) {
6385 LIST_REMOVE(reg, chain);
6396 * SADB_EXPIRE processing
6398 * <base, SA, SA2, lifetime(C and one of HS), address(SD)>
6400 * NOTE: We send only soft lifetime extension.
6403 * others : error number
6407 struct secasvar *sav;
6411 struct mbuf *result = NULL, *m;
6414 struct sadb_lifetime *lt;
6416 /* XXX: Why do we lock ? */
6417 s = splnet(); /*called from softclock()*/
6421 panic("key_expire: NULL pointer is passed.\n");
6422 if (sav->sah == NULL)
6423 panic("key_expire: Why was SA index in SA NULL.\n");
6424 if ((satype = key_proto2satype(sav->sah->saidx.proto)) == 0)
6425 panic("key_expire: invalid proto is passed.\n");
6427 /* set msg header */
6428 m = key_setsadbmsg(SADB_EXPIRE, 0, satype, sav->seq, 0, sav->refcnt);
6435 /* create SA extension */
6436 m = key_setsadbsa(sav);
6443 /* create SA extension */
6444 m = key_setsadbxsa2(sav->sah->saidx.mode,
6445 sav->replay ? sav->replay->count : 0,
6446 sav->sah->saidx.reqid);
6453 /* create lifetime extension (current and soft) */
6454 len = PFKEY_ALIGN8(sizeof(*lt)) * 2;
6455 m = key_alloc_mbuf(len);
6456 if (!m || m->m_next) { /*XXX*/
6462 bzero(mtod(m, caddr_t), len);
6463 lt = mtod(m, struct sadb_lifetime *);
6464 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
6465 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
6466 lt->sadb_lifetime_allocations = sav->lft_c->sadb_lifetime_allocations;
6467 lt->sadb_lifetime_bytes = sav->lft_c->sadb_lifetime_bytes;
6468 lt->sadb_lifetime_addtime = sav->lft_c->sadb_lifetime_addtime;
6469 lt->sadb_lifetime_usetime = sav->lft_c->sadb_lifetime_usetime;
6470 lt = (struct sadb_lifetime *)(mtod(m, caddr_t) + len / 2);
6471 bcopy(sav->lft_s, lt, sizeof(*lt));
6474 /* set sadb_address for source */
6475 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
6476 (struct sockaddr *)&sav->sah->saidx.src,
6477 FULLMASK, IPSEC_ULPROTO_ANY);
6484 /* set sadb_address for destination */
6485 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
6486 (struct sockaddr *)&sav->sah->saidx.dst,
6487 FULLMASK, IPSEC_ULPROTO_ANY);
6494 if ((result->m_flags & M_PKTHDR) == 0) {
6499 if (result->m_len < sizeof(struct sadb_msg)) {
6500 result = m_pullup(result, sizeof(struct sadb_msg));
6501 if (result == NULL) {
6507 result->m_pkthdr.len = 0;
6508 for (m = result; m; m = m->m_next)
6509 result->m_pkthdr.len += m->m_len;
6511 mtod(result, struct sadb_msg *)->sadb_msg_len =
6512 PFKEY_UNIT64(result->m_pkthdr.len);
6515 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
6525 * SADB_FLUSH processing
6528 * from the ikmpd, and free all entries in secastree.
6532 * NOTE: to do is only marking SADB_SASTATE_DEAD.
6534 * m will always be freed.
6537 key_flush(so, m, mhp)
6540 const struct sadb_msghdr *mhp;
6542 struct sadb_msg *newmsg;
6543 struct secashead *sah, *nextsah;
6544 struct secasvar *sav, *nextsav;
6550 if (so == NULL || mhp == NULL || mhp->msg == NULL)
6551 panic("key_flush: NULL pointer is passed.\n");
6553 /* map satype to proto */
6554 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
6555 ipseclog((LOG_DEBUG, "key_flush: invalid satype is passed.\n"));
6556 return key_senderror(so, m, EINVAL);
6559 /* no SATYPE specified, i.e. flushing all SA. */
6560 for (sah = LIST_FIRST(&sahtree);
6563 nextsah = LIST_NEXT(sah, chain);
6565 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
6566 && proto != sah->saidx.proto)
6570 stateidx < _ARRAYLEN(saorder_state_alive);
6572 state = saorder_state_any[stateidx];
6573 for (sav = LIST_FIRST(&sah->savtree[state]);
6577 nextsav = LIST_NEXT(sav, chain);
6579 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
6584 sah->state = SADB_SASTATE_DEAD;
6587 if (m->m_len < sizeof(struct sadb_msg) ||
6588 sizeof(struct sadb_msg) > m->m_len + M_TRAILINGSPACE(m)) {
6589 ipseclog((LOG_DEBUG, "key_flush: No more memory.\n"));
6590 return key_senderror(so, m, ENOBUFS);
6596 m->m_pkthdr.len = m->m_len = sizeof(struct sadb_msg);
6597 newmsg = mtod(m, struct sadb_msg *);
6598 newmsg->sadb_msg_errno = 0;
6599 newmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
6601 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
6605 * SADB_DUMP processing
6606 * dump all entries including status of DEAD in SAD.
6609 * from the ikmpd, and dump all secasvar leaves
6614 * m will always be freed.
6617 key_dump(so, m, mhp)
6620 const struct sadb_msghdr *mhp;
6622 struct secashead *sah;
6623 struct secasvar *sav;
6629 struct sadb_msg *newmsg;
6633 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
6634 panic("key_dump: NULL pointer is passed.\n");
6636 /* map satype to proto */
6637 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
6638 ipseclog((LOG_DEBUG, "key_dump: invalid satype is passed.\n"));
6639 return key_senderror(so, m, EINVAL);
6642 /* count sav entries to be sent to the userland. */
6644 LIST_FOREACH(sah, &sahtree, chain) {
6645 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
6646 && proto != sah->saidx.proto)
6650 stateidx < _ARRAYLEN(saorder_state_any);
6652 state = saorder_state_any[stateidx];
6653 LIST_FOREACH(sav, &sah->savtree[state], chain) {
6660 return key_senderror(so, m, ENOENT);
6662 /* send this to the userland, one at a time. */
6664 LIST_FOREACH(sah, &sahtree, chain) {
6665 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
6666 && proto != sah->saidx.proto)
6669 /* map proto to satype */
6670 if ((satype = key_proto2satype(sah->saidx.proto)) == 0) {
6671 ipseclog((LOG_DEBUG, "key_dump: there was invalid proto in SAD.\n"));
6672 return key_senderror(so, m, EINVAL);
6676 stateidx < _ARRAYLEN(saorder_state_any);
6678 state = saorder_state_any[stateidx];
6679 LIST_FOREACH(sav, &sah->savtree[state], chain) {
6680 n = key_setdumpsa(sav, SADB_DUMP, satype,
6681 --cnt, mhp->msg->sadb_msg_pid);
6683 return key_senderror(so, m, ENOBUFS);
6685 key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
6695 * SADB_X_PROMISC processing
6697 * m will always be freed.
6700 key_promisc(so, m, mhp)
6703 const struct sadb_msghdr *mhp;
6708 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
6709 panic("key_promisc: NULL pointer is passed.\n");
6711 olen = PFKEY_UNUNIT64(mhp->msg->sadb_msg_len);
6713 if (olen < sizeof(struct sadb_msg)) {
6715 return key_senderror(so, m, EINVAL);
6720 } else if (olen == sizeof(struct sadb_msg)) {
6721 /* enable/disable promisc mode */
6724 if ((kp = (struct keycb *)sotorawcb(so)) == NULL)
6725 return key_senderror(so, m, EINVAL);
6726 mhp->msg->sadb_msg_errno = 0;
6727 switch (mhp->msg->sadb_msg_satype) {
6730 kp->kp_promisc = mhp->msg->sadb_msg_satype;
6733 return key_senderror(so, m, EINVAL);
6736 /* send the original message back to everyone */
6737 mhp->msg->sadb_msg_errno = 0;
6738 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
6740 /* send packet as is */
6742 m_adj(m, PFKEY_ALIGN8(sizeof(struct sadb_msg)));
6744 /* TODO: if sadb_msg_seq is specified, send to specific pid */
6745 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
6749 static int (*key_typesw[]) (struct socket *, struct mbuf *,
6750 const struct sadb_msghdr *) = {
6751 NULL, /* SADB_RESERVED */
6752 key_getspi, /* SADB_GETSPI */
6753 key_update, /* SADB_UPDATE */
6754 key_add, /* SADB_ADD */
6755 key_delete, /* SADB_DELETE */
6756 key_get, /* SADB_GET */
6757 key_acquire2, /* SADB_ACQUIRE */
6758 key_register, /* SADB_REGISTER */
6759 NULL, /* SADB_EXPIRE */
6760 key_flush, /* SADB_FLUSH */
6761 key_dump, /* SADB_DUMP */
6762 key_promisc, /* SADB_X_PROMISC */
6763 NULL, /* SADB_X_PCHANGE */
6764 key_spdadd, /* SADB_X_SPDUPDATE */
6765 key_spdadd, /* SADB_X_SPDADD */
6766 key_spddelete, /* SADB_X_SPDDELETE */
6767 key_spdget, /* SADB_X_SPDGET */
6768 NULL, /* SADB_X_SPDACQUIRE */
6769 key_spddump, /* SADB_X_SPDDUMP */
6770 key_spdflush, /* SADB_X_SPDFLUSH */
6771 key_spdadd, /* SADB_X_SPDSETIDX */
6772 NULL, /* SADB_X_SPDEXPIRE */
6773 key_spddelete2, /* SADB_X_SPDDELETE2 */
6777 * parse sadb_msg buffer to process PFKEYv2,
6778 * and create a data to response if needed.
6779 * I think to be dealed with mbuf directly.
6781 * msgp : pointer to pointer to a received buffer pulluped.
6782 * This is rewrited to response.
6783 * so : pointer to socket.
6785 * length for buffer to send to user process.
6792 struct sadb_msg *msg;
6793 struct sadb_msghdr mh;
6799 if (m == NULL || so == NULL)
6800 panic("key_parse: NULL pointer is passed.\n");
6802 #if 0 /*kdebug_sadb assumes msg in linear buffer*/
6803 KEYDEBUG(KEYDEBUG_KEY_DUMP,
6804 ipseclog((LOG_DEBUG, "key_parse: passed sadb_msg\n"));
6808 if (m->m_len < sizeof(struct sadb_msg)) {
6809 m = m_pullup(m, sizeof(struct sadb_msg));
6813 msg = mtod(m, struct sadb_msg *);
6814 orglen = PFKEY_UNUNIT64(msg->sadb_msg_len);
6815 target = KEY_SENDUP_ONE;
6817 if ((m->m_flags & M_PKTHDR) == 0 ||
6818 m->m_pkthdr.len != m->m_pkthdr.len) {
6819 ipseclog((LOG_DEBUG, "key_parse: invalid message length.\n"));
6820 pfkeystat.out_invlen++;
6825 if (msg->sadb_msg_version != PF_KEY_V2) {
6826 ipseclog((LOG_DEBUG,
6827 "key_parse: PF_KEY version %u is mismatched.\n",
6828 msg->sadb_msg_version));
6829 pfkeystat.out_invver++;
6834 if (msg->sadb_msg_type > SADB_MAX) {
6835 ipseclog((LOG_DEBUG, "key_parse: invalid type %u is passed.\n",
6836 msg->sadb_msg_type));
6837 pfkeystat.out_invmsgtype++;
6842 /* for old-fashioned code - should be nuked */
6843 if (m->m_pkthdr.len > MCLBYTES) {
6850 MGETHDR(n, M_DONTWAIT, MT_DATA);
6851 if (n && m->m_pkthdr.len > MHLEN) {
6852 MCLGET(n, M_DONTWAIT);
6853 if ((n->m_flags & M_EXT) == 0) {
6862 m_copydata(m, 0, m->m_pkthdr.len, mtod(n, caddr_t));
6863 n->m_pkthdr.len = n->m_len = m->m_pkthdr.len;
6869 /* align the mbuf chain so that extensions are in contiguous region. */
6870 error = key_align(m, &mh);
6874 if (m->m_next) { /*XXX*/
6882 switch (msg->sadb_msg_satype) {
6883 case SADB_SATYPE_UNSPEC:
6884 switch (msg->sadb_msg_type) {
6892 ipseclog((LOG_DEBUG, "key_parse: must specify satype "
6893 "when msg type=%u.\n", msg->sadb_msg_type));
6894 pfkeystat.out_invsatype++;
6899 case SADB_SATYPE_AH:
6900 case SADB_SATYPE_ESP:
6901 case SADB_X_SATYPE_IPCOMP:
6902 switch (msg->sadb_msg_type) {
6904 case SADB_X_SPDDELETE:
6906 case SADB_X_SPDDUMP:
6907 case SADB_X_SPDFLUSH:
6908 case SADB_X_SPDSETIDX:
6909 case SADB_X_SPDUPDATE:
6910 case SADB_X_SPDDELETE2:
6911 ipseclog((LOG_DEBUG, "key_parse: illegal satype=%u\n",
6912 msg->sadb_msg_type));
6913 pfkeystat.out_invsatype++;
6918 case SADB_SATYPE_RSVP:
6919 case SADB_SATYPE_OSPFV2:
6920 case SADB_SATYPE_RIPV2:
6921 case SADB_SATYPE_MIP:
6922 ipseclog((LOG_DEBUG, "key_parse: type %u isn't supported.\n",
6923 msg->sadb_msg_satype));
6924 pfkeystat.out_invsatype++;
6927 case 1: /* XXX: What does it do? */
6928 if (msg->sadb_msg_type == SADB_X_PROMISC)
6932 ipseclog((LOG_DEBUG, "key_parse: invalid type %u is passed.\n",
6933 msg->sadb_msg_satype));
6934 pfkeystat.out_invsatype++;
6939 /* check field of upper layer protocol and address family */
6940 if (mh.ext[SADB_EXT_ADDRESS_SRC] != NULL
6941 && mh.ext[SADB_EXT_ADDRESS_DST] != NULL) {
6942 struct sadb_address *src0, *dst0;
6945 src0 = (struct sadb_address *)(mh.ext[SADB_EXT_ADDRESS_SRC]);
6946 dst0 = (struct sadb_address *)(mh.ext[SADB_EXT_ADDRESS_DST]);
6948 /* check upper layer protocol */
6949 if (src0->sadb_address_proto != dst0->sadb_address_proto) {
6950 ipseclog((LOG_DEBUG, "key_parse: upper layer protocol mismatched.\n"));
6951 pfkeystat.out_invaddr++;
6957 if (PFKEY_ADDR_SADDR(src0)->sa_family !=
6958 PFKEY_ADDR_SADDR(dst0)->sa_family) {
6959 ipseclog((LOG_DEBUG, "key_parse: address family mismatched.\n"));
6960 pfkeystat.out_invaddr++;
6964 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
6965 PFKEY_ADDR_SADDR(dst0)->sa_len) {
6966 ipseclog((LOG_DEBUG,
6967 "key_parse: address struct size mismatched.\n"));
6968 pfkeystat.out_invaddr++;
6973 switch (PFKEY_ADDR_SADDR(src0)->sa_family) {
6975 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
6976 sizeof(struct sockaddr_in)) {
6977 pfkeystat.out_invaddr++;
6983 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
6984 sizeof(struct sockaddr_in6)) {
6985 pfkeystat.out_invaddr++;
6991 ipseclog((LOG_DEBUG,
6992 "key_parse: unsupported address family.\n"));
6993 pfkeystat.out_invaddr++;
6994 error = EAFNOSUPPORT;
6998 switch (PFKEY_ADDR_SADDR(src0)->sa_family) {
7000 plen = sizeof(struct in_addr) << 3;
7003 plen = sizeof(struct in6_addr) << 3;
7006 plen = 0; /*fool gcc*/
7010 /* check max prefix length */
7011 if (src0->sadb_address_prefixlen > plen ||
7012 dst0->sadb_address_prefixlen > plen) {
7013 ipseclog((LOG_DEBUG,
7014 "key_parse: illegal prefixlen.\n"));
7015 pfkeystat.out_invaddr++;
7021 * prefixlen == 0 is valid because there can be a case when
7022 * all addresses are matched.
7026 if (msg->sadb_msg_type >= sizeof(key_typesw)/sizeof(key_typesw[0]) ||
7027 key_typesw[msg->sadb_msg_type] == NULL) {
7028 pfkeystat.out_invmsgtype++;
7033 return (*key_typesw[msg->sadb_msg_type])(so, m, &mh);
7036 msg->sadb_msg_errno = error;
7037 return key_sendup_mbuf(so, m, target);
7041 key_senderror(so, m, code)
7046 struct sadb_msg *msg;
7048 if (m->m_len < sizeof(struct sadb_msg))
7049 panic("invalid mbuf passed to key_senderror");
7051 msg = mtod(m, struct sadb_msg *);
7052 msg->sadb_msg_errno = code;
7053 return key_sendup_mbuf(so, m, KEY_SENDUP_ONE);
7057 * set the pointer to each header into message buffer.
7058 * m will be freed on error.
7059 * XXX larger-than-MCLBYTES extension?
7064 struct sadb_msghdr *mhp;
7067 struct sadb_ext *ext;
7073 if (m == NULL || mhp == NULL)
7074 panic("key_align: NULL pointer is passed.\n");
7075 if (m->m_len < sizeof(struct sadb_msg))
7076 panic("invalid mbuf passed to key_align");
7079 bzero(mhp, sizeof(*mhp));
7081 mhp->msg = mtod(m, struct sadb_msg *);
7082 mhp->ext[0] = (struct sadb_ext *)mhp->msg; /*XXX backward compat */
7084 end = PFKEY_UNUNIT64(mhp->msg->sadb_msg_len);
7085 extlen = end; /*just in case extlen is not updated*/
7086 for (off = sizeof(struct sadb_msg); off < end; off += extlen) {
7087 n = m_pulldown(m, off, sizeof(struct sadb_ext), &toff);
7089 /* m is already freed */
7092 ext = (struct sadb_ext *)(mtod(n, caddr_t) + toff);
7095 switch (ext->sadb_ext_type) {
7097 case SADB_EXT_ADDRESS_SRC:
7098 case SADB_EXT_ADDRESS_DST:
7099 case SADB_EXT_ADDRESS_PROXY:
7100 case SADB_EXT_LIFETIME_CURRENT:
7101 case SADB_EXT_LIFETIME_HARD:
7102 case SADB_EXT_LIFETIME_SOFT:
7103 case SADB_EXT_KEY_AUTH:
7104 case SADB_EXT_KEY_ENCRYPT:
7105 case SADB_EXT_IDENTITY_SRC:
7106 case SADB_EXT_IDENTITY_DST:
7107 case SADB_EXT_SENSITIVITY:
7108 case SADB_EXT_PROPOSAL:
7109 case SADB_EXT_SUPPORTED_AUTH:
7110 case SADB_EXT_SUPPORTED_ENCRYPT:
7111 case SADB_EXT_SPIRANGE:
7112 case SADB_X_EXT_POLICY:
7113 case SADB_X_EXT_SA2:
7114 /* duplicate check */
7116 * XXX Are there duplication payloads of either
7117 * KEY_AUTH or KEY_ENCRYPT ?
7119 if (mhp->ext[ext->sadb_ext_type] != NULL) {
7120 ipseclog((LOG_DEBUG,
7121 "key_align: duplicate ext_type %u "
7122 "is passed.\n", ext->sadb_ext_type));
7124 pfkeystat.out_dupext++;
7129 ipseclog((LOG_DEBUG,
7130 "key_align: invalid ext_type %u is passed.\n",
7131 ext->sadb_ext_type));
7133 pfkeystat.out_invexttype++;
7137 extlen = PFKEY_UNUNIT64(ext->sadb_ext_len);
7139 if (key_validate_ext(ext, extlen)) {
7141 pfkeystat.out_invlen++;
7145 n = m_pulldown(m, off, extlen, &toff);
7147 /* m is already freed */
7150 ext = (struct sadb_ext *)(mtod(n, caddr_t) + toff);
7152 mhp->ext[ext->sadb_ext_type] = ext;
7153 mhp->extoff[ext->sadb_ext_type] = off;
7154 mhp->extlen[ext->sadb_ext_type] = extlen;
7159 pfkeystat.out_invlen++;
7167 key_validate_ext(ext, len)
7168 const struct sadb_ext *ext;
7171 struct sockaddr *sa;
7172 enum { NONE, ADDR } checktype = NONE;
7174 const int sal = offsetof(struct sockaddr, sa_len) + sizeof(sa->sa_len);
7176 if (len != PFKEY_UNUNIT64(ext->sadb_ext_len))
7179 /* if it does not match minimum/maximum length, bail */
7180 if (ext->sadb_ext_type >= sizeof(minsize) / sizeof(minsize[0]) ||
7181 ext->sadb_ext_type >= sizeof(maxsize) / sizeof(maxsize[0]))
7183 if (!minsize[ext->sadb_ext_type] || len < minsize[ext->sadb_ext_type])
7185 if (maxsize[ext->sadb_ext_type] && len > maxsize[ext->sadb_ext_type])
7188 /* more checks based on sadb_ext_type XXX need more */
7189 switch (ext->sadb_ext_type) {
7190 case SADB_EXT_ADDRESS_SRC:
7191 case SADB_EXT_ADDRESS_DST:
7192 case SADB_EXT_ADDRESS_PROXY:
7193 baselen = PFKEY_ALIGN8(sizeof(struct sadb_address));
7196 case SADB_EXT_IDENTITY_SRC:
7197 case SADB_EXT_IDENTITY_DST:
7198 if (((struct sadb_ident *)ext)->sadb_ident_type ==
7199 SADB_X_IDENTTYPE_ADDR) {
7200 baselen = PFKEY_ALIGN8(sizeof(struct sadb_ident));
7210 switch (checktype) {
7214 sa = (struct sockaddr *)((caddr_t)ext + baselen);
7215 if (len < baselen + sal)
7217 if (baselen + PFKEY_ALIGN8(sa->sa_len) != len)
7230 bzero((caddr_t)&key_cb, sizeof(key_cb));
7232 for (i = 0; i < IPSEC_DIR_MAX; i++) {
7233 LIST_INIT(&sptree[i]);
7236 LIST_INIT(&sahtree);
7238 for (i = 0; i <= SADB_SATYPE_MAX; i++) {
7239 LIST_INIT(®tree[i]);
7242 #ifndef IPSEC_NONBLOCK_ACQUIRE
7243 LIST_INIT(&acqtree);
7245 LIST_INIT(&spacqtree);
7247 /* system default */
7249 ip4_def_policy.policy = IPSEC_POLICY_NONE;
7250 ip4_def_policy.refcnt++; /*never reclaim this*/
7253 ip6_def_policy.policy = IPSEC_POLICY_NONE;
7254 ip6_def_policy.refcnt++; /*never reclaim this*/
7257 #ifndef IPSEC_DEBUG2
7258 timeout((void *)key_timehandler, (void *)0, hz);
7259 #endif /*IPSEC_DEBUG2*/
7261 /* initialize key statistics */
7262 keystat.getspi_count = 1;
7264 printf("IPsec: Initialized Security Association Processing.\n");
7270 * XXX: maybe This function is called after INBOUND IPsec processing.
7272 * Special check for tunnel-mode packets.
7273 * We must make some checks for consistency between inner and outer IP header.
7275 * xxx more checks to be provided
7278 key_checktunnelsanity(sav, family, src, dst)
7279 struct secasvar *sav;
7285 if (sav->sah == NULL)
7286 panic("sav->sah == NULL at key_checktunnelsanity");
7288 /* XXX: check inner IP header */
7294 #define hostnamelen strlen(hostname)
7297 * Get FQDN for the host.
7298 * If the administrator configured hostname (by hostname(1)) without
7299 * domain name, returns nothing.
7306 static char fqdn[MAXHOSTNAMELEN + 1];
7311 /* check if it comes with domain name. */
7313 for (i = 0; i < hostnamelen; i++) {
7314 if (hostname[i] == '.')
7320 /* NOTE: hostname may not be NUL-terminated. */
7321 bzero(fqdn, sizeof(fqdn));
7322 bcopy(hostname, fqdn, hostnamelen);
7323 fqdn[hostnamelen] = '\0';
7328 * get username@FQDN for the host/user.
7334 static char userfqdn[MAXHOSTNAMELEN + MAXLOGNAME + 2];
7335 struct proc *p = curproc;
7338 if (!p || !p->p_pgrp || !p->p_pgrp->pg_session)
7340 if (!(host = key_getfqdn()))
7343 /* NOTE: s_login may not be-NUL terminated. */
7344 bzero(userfqdn, sizeof(userfqdn));
7345 bcopy(p->p_pgrp->pg_session->s_login, userfqdn, MAXLOGNAME);
7346 userfqdn[MAXLOGNAME] = '\0'; /* safeguard */
7347 q = userfqdn + strlen(userfqdn);
7349 bcopy(host, q, strlen(host));
7357 /* record data transfer on SA, and update timestamps */
7359 key_sa_recordxfer(sav, m)
7360 struct secasvar *sav;
7364 panic("key_sa_recordxfer called with sav == NULL");
7366 panic("key_sa_recordxfer called with m == NULL");
7371 * XXX Currently, there is a difference of bytes size
7372 * between inbound and outbound processing.
7374 sav->lft_c->sadb_lifetime_bytes += m->m_pkthdr.len;
7375 /* to check bytes lifetime is done in key_timehandler(). */
7378 * We use the number of packets as the unit of
7379 * sadb_lifetime_allocations. We increment the variable
7380 * whenever {esp,ah}_{in,out}put is called.
7382 sav->lft_c->sadb_lifetime_allocations++;
7383 /* XXX check for expires? */
7386 * NOTE: We record CURRENT sadb_lifetime_usetime by using wall clock,
7387 * in seconds. HARD and SOFT lifetime are measured by the time
7388 * difference (again in seconds) from sadb_lifetime_usetime.
7392 * -----+-----+--------+---> t
7393 * <--------------> HARD
7399 sav->lft_c->sadb_lifetime_usetime = tv.tv_sec;
7400 /* XXX check for expires? */
7408 key_sa_routechange(dst)
7409 struct sockaddr *dst;
7411 struct secashead *sah;
7414 LIST_FOREACH(sah, &sahtree, chain) {
7415 ro = &sah->sa_route;
7416 if (ro->ro_rt && dst->sa_len == ro->ro_dst.sa_len
7417 && bcmp(dst, &ro->ro_dst, dst->sa_len) == 0) {
7419 ro->ro_rt = (struct rtentry *)NULL;
7427 key_sa_chgstate(sav, state)
7428 struct secasvar *sav;
7432 panic("key_sa_chgstate called with sav == NULL");
7434 if (sav->state == state)
7437 if (__LIST_CHAINED(sav))
7438 LIST_REMOVE(sav, chain);
7441 LIST_INSERT_HEAD(&sav->sah->savtree[state], sav, chain);
7446 struct secasvar *sav;
7450 panic("key_sa_stir_iv called with sav == NULL");
7451 key_randomfill(sav->iv, sav->ivlen);
7455 static struct mbuf *
7459 struct mbuf *m = NULL, *n;
7464 MGET(n, M_DONTWAIT, MT_DATA);
7465 if (n && len > MLEN)
7466 MCLGET(n, M_DONTWAIT);
7474 n->m_len = M_TRAILINGSPACE(n);
7475 /* use the bottom of mbuf, hoping we can prepend afterwards */
7476 if (n->m_len > len) {
7477 t = (n->m_len - len) & ~(sizeof(long) - 1);