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.8 2004/06/02 14:43:03 eirikn Exp $ */
3 /* $KAME: key.c,v 1.191 2001/06/27 10:46:49 sakane Exp $ */
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * This code is referd to RFC 2367
39 #include "opt_inet6.h"
40 #include "opt_ipsec.h"
42 #include <sys/types.h>
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
47 #include <sys/domain.h>
48 #include <sys/protosw.h>
49 #include <sys/malloc.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/sysctl.h>
53 #include <sys/errno.h>
55 #include <sys/queue.h>
56 #include <sys/syslog.h>
59 #include <net/route.h>
60 #include <net/raw_cb.h>
62 #include <netinet/in.h>
63 #include <netinet/in_systm.h>
64 #include <netinet/ip.h>
65 #include <netinet/in_var.h>
68 #include <netinet/ip6.h>
69 #include <netinet6/in6_var.h>
70 #include <netinet6/ip6_var.h>
74 #include <netinet/in_pcb.h>
77 #include <netinet6/in6_pcb.h>
80 #include <net/pfkeyv2.h>
84 #include "key_debug.h"
86 #include <netinet6/ipsec.h>
88 #include <netinet6/ipsec6.h>
90 #include <netinet6/ah.h>
92 #include <netinet6/ah6.h>
95 #include <netinet6/esp.h>
97 #include <netinet6/esp6.h>
100 #include <netinet6/ipcomp.h>
102 #include <netinet6/ipcomp6.h>
105 #include <machine/stdarg.h>
108 #include <sys/random.h>
110 #include <net/net_osdep.h>
113 #define satosin(s) ((struct sockaddr_in *)s)
116 #define FULLMASK 0xff
119 * Note on SA reference counting:
120 * - SAs that are not in DEAD state will have (total external reference + 1)
121 * following value in reference count field. they cannot be freed and are
122 * referenced from SA header.
123 * - SAs that are in DEAD state will have (total external reference)
124 * in reference count field. they are ready to be freed. reference from
125 * SA header will be removed in key_delsav(), when the reference count
126 * field hits 0 (= no external reference other than from SA header.
129 u_int32_t key_debug_level = 0;
130 static u_int key_spi_trycnt = 1000;
131 static u_int32_t key_spi_minval = 0x100;
132 static u_int32_t key_spi_maxval = 0x0fffffff; /* XXX */
133 static u_int32_t policy_id = 0;
134 static u_int key_int_random = 60; /*interval to initialize randseed,1(m)*/
135 static u_int key_larval_lifetime = 30; /* interval to expire acquiring, 30(s)*/
136 static int key_blockacq_count = 10; /* counter for blocking SADB_ACQUIRE.*/
137 static int key_blockacq_lifetime = 20; /* lifetime for blocking SADB_ACQUIRE.*/
138 static int key_preferred_oldsa = 1; /* preferred old sa rather than new sa.*/
140 static u_int32_t acq_seq = 0;
141 static int key_tick_init_random = 0;
143 static LIST_HEAD(_sptree, secpolicy) sptree[IPSEC_DIR_MAX]; /* SPD */
144 static LIST_HEAD(_sahtree, secashead) sahtree; /* SAD */
145 static LIST_HEAD(_regtree, secreg) regtree[SADB_SATYPE_MAX + 1];
147 #ifndef IPSEC_NONBLOCK_ACQUIRE
148 static LIST_HEAD(_acqtree, secacq) acqtree; /* acquiring list */
150 static LIST_HEAD(_spacqtree, secspacq) spacqtree; /* SP acquiring list */
152 struct key_cb key_cb;
154 /* search order for SAs */
155 static const u_int saorder_state_valid_prefer_old[] = {
156 SADB_SASTATE_DYING, SADB_SASTATE_MATURE,
158 static const u_int saorder_state_valid_prefer_new[] = {
159 SADB_SASTATE_MATURE, SADB_SASTATE_DYING,
161 static const u_int saorder_state_alive[] = {
163 SADB_SASTATE_MATURE, SADB_SASTATE_DYING, SADB_SASTATE_LARVAL
165 static const u_int saorder_state_any[] = {
166 SADB_SASTATE_MATURE, SADB_SASTATE_DYING,
167 SADB_SASTATE_LARVAL, SADB_SASTATE_DEAD
170 static const int minsize[] = {
171 sizeof(struct sadb_msg), /* SADB_EXT_RESERVED */
172 sizeof(struct sadb_sa), /* SADB_EXT_SA */
173 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_CURRENT */
174 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_HARD */
175 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_SOFT */
176 sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_SRC */
177 sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_DST */
178 sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_PROXY */
179 sizeof(struct sadb_key), /* SADB_EXT_KEY_AUTH */
180 sizeof(struct sadb_key), /* SADB_EXT_KEY_ENCRYPT */
181 sizeof(struct sadb_ident), /* SADB_EXT_IDENTITY_SRC */
182 sizeof(struct sadb_ident), /* SADB_EXT_IDENTITY_DST */
183 sizeof(struct sadb_sens), /* SADB_EXT_SENSITIVITY */
184 sizeof(struct sadb_prop), /* SADB_EXT_PROPOSAL */
185 sizeof(struct sadb_supported), /* SADB_EXT_SUPPORTED_AUTH */
186 sizeof(struct sadb_supported), /* SADB_EXT_SUPPORTED_ENCRYPT */
187 sizeof(struct sadb_spirange), /* SADB_EXT_SPIRANGE */
188 0, /* SADB_X_EXT_KMPRIVATE */
189 sizeof(struct sadb_x_policy), /* SADB_X_EXT_POLICY */
190 sizeof(struct sadb_x_sa2), /* SADB_X_SA2 */
192 static const int maxsize[] = {
193 sizeof(struct sadb_msg), /* SADB_EXT_RESERVED */
194 sizeof(struct sadb_sa), /* SADB_EXT_SA */
195 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_CURRENT */
196 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_HARD */
197 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_SOFT */
198 0, /* SADB_EXT_ADDRESS_SRC */
199 0, /* SADB_EXT_ADDRESS_DST */
200 0, /* SADB_EXT_ADDRESS_PROXY */
201 0, /* SADB_EXT_KEY_AUTH */
202 0, /* SADB_EXT_KEY_ENCRYPT */
203 0, /* SADB_EXT_IDENTITY_SRC */
204 0, /* SADB_EXT_IDENTITY_DST */
205 0, /* SADB_EXT_SENSITIVITY */
206 0, /* SADB_EXT_PROPOSAL */
207 0, /* SADB_EXT_SUPPORTED_AUTH */
208 0, /* SADB_EXT_SUPPORTED_ENCRYPT */
209 sizeof(struct sadb_spirange), /* SADB_EXT_SPIRANGE */
210 0, /* SADB_X_EXT_KMPRIVATE */
211 0, /* SADB_X_EXT_POLICY */
212 sizeof(struct sadb_x_sa2), /* SADB_X_SA2 */
215 static int ipsec_esp_keymin = 256;
216 static int ipsec_esp_auth = 0;
217 static int ipsec_ah_keymin = 128;
220 SYSCTL_DECL(_net_key);
223 SYSCTL_INT(_net_key, KEYCTL_DEBUG_LEVEL, debug, CTLFLAG_RW, \
224 &key_debug_level, 0, "");
226 /* max count of trial for the decision of spi value */
227 SYSCTL_INT(_net_key, KEYCTL_SPI_TRY, spi_trycnt, CTLFLAG_RW, \
228 &key_spi_trycnt, 0, "");
230 /* minimum spi value to allocate automatically. */
231 SYSCTL_INT(_net_key, KEYCTL_SPI_MIN_VALUE, spi_minval, CTLFLAG_RW, \
232 &key_spi_minval, 0, "");
234 /* maximun spi value to allocate automatically. */
235 SYSCTL_INT(_net_key, KEYCTL_SPI_MAX_VALUE, spi_maxval, CTLFLAG_RW, \
236 &key_spi_maxval, 0, "");
238 /* interval to initialize randseed */
239 SYSCTL_INT(_net_key, KEYCTL_RANDOM_INT, int_random, CTLFLAG_RW, \
240 &key_int_random, 0, "");
242 /* lifetime for larval SA */
243 SYSCTL_INT(_net_key, KEYCTL_LARVAL_LIFETIME, larval_lifetime, CTLFLAG_RW, \
244 &key_larval_lifetime, 0, "");
246 /* counter for blocking to send SADB_ACQUIRE to IKEd */
247 SYSCTL_INT(_net_key, KEYCTL_BLOCKACQ_COUNT, blockacq_count, CTLFLAG_RW, \
248 &key_blockacq_count, 0, "");
250 /* lifetime for blocking to send SADB_ACQUIRE to IKEd */
251 SYSCTL_INT(_net_key, KEYCTL_BLOCKACQ_LIFETIME, blockacq_lifetime, CTLFLAG_RW, \
252 &key_blockacq_lifetime, 0, "");
255 SYSCTL_INT(_net_key, KEYCTL_ESP_AUTH, esp_auth, CTLFLAG_RW, \
256 &ipsec_esp_auth, 0, "");
258 /* minimum ESP key length */
259 SYSCTL_INT(_net_key, KEYCTL_ESP_KEYMIN, esp_keymin, CTLFLAG_RW, \
260 &ipsec_esp_keymin, 0, "");
262 /* minimum AH key length */
263 SYSCTL_INT(_net_key, KEYCTL_AH_KEYMIN, ah_keymin, CTLFLAG_RW, \
264 &ipsec_ah_keymin, 0, "");
266 /* perfered old SA rather than new SA */
267 SYSCTL_INT(_net_key, KEYCTL_PREFERED_OLDSA, prefered_oldsa, CTLFLAG_RW,\
268 &key_preferred_oldsa, 0, "");
271 #define LIST_FOREACH(elm, head, field) \
272 for (elm = LIST_FIRST(head); elm; elm = LIST_NEXT(elm, field))
274 #define __LIST_CHAINED(elm) \
275 (!((elm)->chain.le_next == NULL && (elm)->chain.le_prev == NULL))
276 #define LIST_INSERT_TAIL(head, elm, type, field) \
278 struct type *curelm = LIST_FIRST(head); \
279 if (curelm == NULL) {\
280 LIST_INSERT_HEAD(head, elm, field); \
282 while (LIST_NEXT(curelm, field)) \
283 curelm = LIST_NEXT(curelm, field);\
284 LIST_INSERT_AFTER(curelm, elm, field);\
288 #define KEY_CHKSASTATE(head, sav, name) \
290 if ((head) != (sav)) { \
291 ipseclog((LOG_DEBUG, "%s: state mismatched (TREE=%d SA=%d)\n", \
292 (name), (head), (sav))); \
297 #define KEY_CHKSPDIR(head, sp, name) \
299 if ((head) != (sp)) { \
300 ipseclog((LOG_DEBUG, "%s: direction mismatched (TREE=%d SP=%d), " \
301 "anyway continue.\n", \
302 (name), (head), (sp))); \
307 #define KMALLOC(p, t, n) \
308 ((p) = (t) malloc((unsigned long)(n), M_SECA, M_INTWAIT | M_NULLOK))
310 free((caddr_t)(p), M_SECA);
312 #define KMALLOC(p, t, n) \
314 ((p) = (t)malloc((unsigned long)(n), M_SECA, M_INTWAIT | M_NULLOK)); \
315 printf("%s %d: %p <- KMALLOC(%s, %d)\n", \
316 __FILE__, __LINE__, (p), #t, n); \
321 printf("%s %d: %p -> KFREE()\n", __FILE__, __LINE__, (p)); \
322 free((caddr_t)(p), M_SECA); \
327 * set parameters into secpolicyindex buffer.
328 * Must allocate secpolicyindex buffer passed to this function.
330 #define KEY_SETSECSPIDX(_dir, s, d, ps, pd, ulp, idx) \
332 bzero((idx), sizeof(struct secpolicyindex)); \
333 (idx)->dir = (_dir); \
334 (idx)->prefs = (ps); \
335 (idx)->prefd = (pd); \
336 (idx)->ul_proto = (ulp); \
337 bcopy((s), &(idx)->src, ((struct sockaddr *)(s))->sa_len); \
338 bcopy((d), &(idx)->dst, ((struct sockaddr *)(d))->sa_len); \
342 * set parameters into secasindex buffer.
343 * Must allocate secasindex buffer before calling this function.
345 #define KEY_SETSECASIDX(p, m, r, s, d, idx) \
347 bzero((idx), sizeof(struct secasindex)); \
348 (idx)->proto = (p); \
350 (idx)->reqid = (r); \
351 bcopy((s), &(idx)->src, ((struct sockaddr *)(s))->sa_len); \
352 bcopy((d), &(idx)->dst, ((struct sockaddr *)(d))->sa_len); \
357 u_long getspi_count; /* the avarage of count to try to get new SPI */
361 struct sadb_msg *msg;
362 struct sadb_ext *ext[SADB_EXT_MAX + 1];
363 int extoff[SADB_EXT_MAX + 1];
364 int extlen[SADB_EXT_MAX + 1];
367 static struct secasvar *key_allocsa_policy (struct secasindex *);
368 static void key_freesp_so (struct secpolicy **);
369 static struct secasvar *key_do_allocsa_policy (struct secashead *, u_int);
370 static void key_delsp (struct secpolicy *);
371 static struct secpolicy *key_getsp (struct secpolicyindex *);
372 static struct secpolicy *key_getspbyid (u_int32_t);
373 static u_int32_t key_newreqid (void);
374 static struct mbuf *key_gather_mbuf (struct mbuf *,
375 const struct sadb_msghdr *, int, int, ...);
376 static int key_spdadd (struct socket *, struct mbuf *,
377 const struct sadb_msghdr *);
378 static u_int32_t key_getnewspid (void);
379 static int key_spddelete (struct socket *, struct mbuf *,
380 const struct sadb_msghdr *);
381 static int key_spddelete2 (struct socket *, struct mbuf *,
382 const struct sadb_msghdr *);
383 static int key_spdget (struct socket *, struct mbuf *,
384 const struct sadb_msghdr *);
385 static int key_spdflush (struct socket *, struct mbuf *,
386 const struct sadb_msghdr *);
387 static int key_spddump (struct socket *, struct mbuf *,
388 const struct sadb_msghdr *);
389 static struct mbuf *key_setdumpsp (struct secpolicy *,
390 u_int8_t, u_int32_t, u_int32_t);
391 static u_int key_getspreqmsglen (struct secpolicy *);
392 static int key_spdexpire (struct secpolicy *);
393 static struct secashead *key_newsah (struct secasindex *);
394 static void key_delsah (struct secashead *);
395 static struct secasvar *key_newsav (struct mbuf *,
396 const struct sadb_msghdr *, struct secashead *, int *);
397 static void key_delsav (struct secasvar *);
398 static struct secashead *key_getsah (struct secasindex *);
399 static struct secasvar *key_checkspidup (struct secasindex *, u_int32_t);
400 static struct secasvar *key_getsavbyspi (struct secashead *, u_int32_t);
401 static int key_setsaval (struct secasvar *, struct mbuf *,
402 const struct sadb_msghdr *);
403 static int key_mature (struct secasvar *);
404 static struct mbuf *key_setdumpsa (struct secasvar *, u_int8_t,
405 u_int8_t, u_int32_t, u_int32_t);
406 static struct mbuf *key_setsadbmsg (u_int8_t, u_int16_t, u_int8_t,
407 u_int32_t, pid_t, u_int16_t);
408 static struct mbuf *key_setsadbsa (struct secasvar *);
409 static struct mbuf *key_setsadbaddr (u_int16_t,
410 struct sockaddr *, u_int8_t, u_int16_t);
412 static struct mbuf *key_setsadbident (u_int16_t, u_int16_t, caddr_t,
415 static struct mbuf *key_setsadbxsa2 (u_int8_t, u_int32_t, u_int32_t);
416 static struct mbuf *key_setsadbxpolicy (u_int16_t, u_int8_t,
418 static void *key_newbuf (const void *, u_int);
420 static int key_ismyaddr6 (struct sockaddr_in6 *);
423 /* flags for key_cmpsaidx() */
424 #define CMP_HEAD 1 /* protocol, addresses. */
425 #define CMP_MODE_REQID 2 /* additionally HEAD, reqid, mode. */
426 #define CMP_REQID 3 /* additionally HEAD, reaid. */
427 #define CMP_EXACTLY 4 /* all elements. */
428 static int key_cmpsaidx
429 (struct secasindex *, struct secasindex *, int);
431 static int key_cmpspidx_exactly
432 (struct secpolicyindex *, struct secpolicyindex *);
433 static int key_cmpspidx_withmask
434 (struct secpolicyindex *, struct secpolicyindex *);
435 static int key_sockaddrcmp (struct sockaddr *, struct sockaddr *, int);
436 static int key_bbcmp (caddr_t, caddr_t, u_int);
437 static void key_srandom (void);
438 static u_int16_t key_satype2proto (u_int8_t);
439 static u_int8_t key_proto2satype (u_int16_t);
441 static int key_getspi (struct socket *, struct mbuf *,
442 const struct sadb_msghdr *);
443 static u_int32_t key_do_getnewspi (struct sadb_spirange *,
444 struct secasindex *);
445 static int key_update (struct socket *, struct mbuf *,
446 const struct sadb_msghdr *);
447 #ifdef IPSEC_DOSEQCHECK
448 static struct secasvar *key_getsavbyseq (struct secashead *, u_int32_t);
450 static int key_add (struct socket *, struct mbuf *,
451 const struct sadb_msghdr *);
452 static int key_setident (struct secashead *, struct mbuf *,
453 const struct sadb_msghdr *);
454 static struct mbuf *key_getmsgbuf_x1 (struct mbuf *,
455 const struct sadb_msghdr *);
456 static int key_delete (struct socket *, struct mbuf *,
457 const struct sadb_msghdr *);
458 static int key_get (struct socket *, struct mbuf *,
459 const struct sadb_msghdr *);
461 static void key_getcomb_setlifetime (struct sadb_comb *);
463 static struct mbuf *key_getcomb_esp (void);
465 static struct mbuf *key_getcomb_ah (void);
466 static struct mbuf *key_getcomb_ipcomp (void);
467 static struct mbuf *key_getprop (const struct secasindex *);
469 static int key_acquire (struct secasindex *, struct secpolicy *);
470 #ifndef IPSEC_NONBLOCK_ACQUIRE
471 static struct secacq *key_newacq (struct secasindex *);
472 static struct secacq *key_getacq (struct secasindex *);
473 static struct secacq *key_getacqbyseq (u_int32_t);
475 static struct secspacq *key_newspacq (struct secpolicyindex *);
476 static struct secspacq *key_getspacq (struct secpolicyindex *);
477 static int key_acquire2 (struct socket *, struct mbuf *,
478 const struct sadb_msghdr *);
479 static int key_register (struct socket *, struct mbuf *,
480 const struct sadb_msghdr *);
481 static int key_expire (struct secasvar *);
482 static int key_flush (struct socket *, struct mbuf *,
483 const struct sadb_msghdr *);
484 static int key_dump (struct socket *, struct mbuf *,
485 const struct sadb_msghdr *);
486 static int key_promisc (struct socket *, struct mbuf *,
487 const struct sadb_msghdr *);
488 static int key_senderror (struct socket *, struct mbuf *, int);
489 static int key_validate_ext (const struct sadb_ext *, int);
490 static int key_align (struct mbuf *, struct sadb_msghdr *);
492 static const char *key_getfqdn (void);
493 static const char *key_getuserfqdn (void);
495 static void key_sa_chgstate (struct secasvar *, u_int8_t);
496 static struct mbuf *key_alloc_mbuf (int);
498 /* %%% IPsec policy management */
500 * allocating a SP for OUTBOUND or INBOUND packet.
501 * Must call key_freesp() later.
502 * OUT: NULL: not found
503 * others: found and return the pointer.
506 key_allocsp(spidx, dir)
507 struct secpolicyindex *spidx;
510 struct secpolicy *sp;
516 panic("key_allocsp: NULL pointer is passed.\n");
518 /* check direction */
520 case IPSEC_DIR_INBOUND:
521 case IPSEC_DIR_OUTBOUND:
524 panic("key_allocsp: Invalid direction is passed.\n");
528 s = splnet(); /*called from softclock()*/
529 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
530 printf("*** objects\n");
531 kdebug_secpolicyindex(spidx));
533 LIST_FOREACH(sp, &sptree[dir], chain) {
534 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
535 printf("*** in SPD\n");
536 kdebug_secpolicyindex(&sp->spidx));
538 if (sp->state == IPSEC_SPSTATE_DEAD)
540 if (key_cmpspidx_withmask(&sp->spidx, spidx))
549 KEY_CHKSPDIR(sp->spidx.dir, dir, "key_allocsp");
551 /* found a SPD entry */
553 sp->lastused = tv.tv_sec;
556 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
557 printf("DP key_allocsp cause refcnt++:%d SP:%p\n",
564 * return a policy that matches this particular inbound packet.
568 key_gettunnel(osrc, odst, isrc, idst)
569 struct sockaddr *osrc, *odst, *isrc, *idst;
571 struct secpolicy *sp;
572 const int dir = IPSEC_DIR_INBOUND;
575 struct ipsecrequest *r1, *r2, *p;
576 struct sockaddr *os, *od, *is, *id;
577 struct secpolicyindex spidx;
579 if (isrc->sa_family != idst->sa_family) {
580 ipseclog((LOG_ERR, "protocol family mismatched %d != %d\n.",
581 isrc->sa_family, idst->sa_family));
585 s = splnet(); /*called from softclock()*/
586 LIST_FOREACH(sp, &sptree[dir], chain) {
587 if (sp->state == IPSEC_SPSTATE_DEAD)
591 for (p = sp->req; p; p = p->next) {
592 if (p->saidx.mode != IPSEC_MODE_TUNNEL)
599 /* here we look at address matches only */
601 if (isrc->sa_len > sizeof(spidx.src) ||
602 idst->sa_len > sizeof(spidx.dst))
604 bcopy(isrc, &spidx.src, isrc->sa_len);
605 bcopy(idst, &spidx.dst, idst->sa_len);
606 if (!key_cmpspidx_withmask(&sp->spidx, &spidx))
609 is = (struct sockaddr *)&r1->saidx.src;
610 id = (struct sockaddr *)&r1->saidx.dst;
611 if (key_sockaddrcmp(is, isrc, 0) ||
612 key_sockaddrcmp(id, idst, 0))
616 os = (struct sockaddr *)&r2->saidx.src;
617 od = (struct sockaddr *)&r2->saidx.dst;
618 if (key_sockaddrcmp(os, osrc, 0) ||
619 key_sockaddrcmp(od, odst, 0))
630 sp->lastused = tv.tv_sec;
637 * allocating an SA entry for an *OUTBOUND* packet.
638 * checking each request entries in SP, and acquire an SA if need.
639 * OUT: 0: there are valid requests.
640 * ENOENT: policy may be valid, but SA with REQUIRE is on acquiring.
643 key_checkrequest(isr, saidx)
644 struct ipsecrequest *isr;
645 struct secasindex *saidx;
651 if (isr == NULL || saidx == NULL)
652 panic("key_checkrequest: NULL pointer is passed.\n");
655 switch (saidx->mode) {
656 case IPSEC_MODE_TRANSPORT:
657 case IPSEC_MODE_TUNNEL:
661 panic("key_checkrequest: Invalid policy defined.\n");
664 /* get current level */
665 level = ipsec_get_reqlevel(isr);
669 * We do allocate new SA only if the state of SA in the holder is
670 * SADB_SASTATE_DEAD. The SA for outbound must be the oldest.
672 if (isr->sav != NULL) {
673 if (isr->sav->sah == NULL)
674 panic("key_checkrequest: sah is null.\n");
675 if (isr->sav == (struct secasvar *)LIST_FIRST(
676 &isr->sav->sah->savtree[SADB_SASTATE_DEAD])) {
677 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
678 printf("DP checkrequest calls free SA:%p\n",
680 key_freesav(isr->sav);
686 * we free any SA stashed in the IPsec request because a different
687 * SA may be involved each time this request is checked, either
688 * because new SAs are being configured, or this request is
689 * associated with an unconnected datagram socket, or this request
690 * is associated with a system default policy.
692 * The operation may have negative impact to performance. We may
693 * want to check cached SA carefully, rather than picking new SA
696 if (isr->sav != NULL) {
697 key_freesav(isr->sav);
703 * new SA allocation if no SA found.
704 * key_allocsa_policy should allocate the oldest SA available.
705 * See key_do_allocsa_policy(), and draft-jenkins-ipsec-rekeying-03.txt.
707 if (isr->sav == NULL)
708 isr->sav = key_allocsa_policy(saidx);
710 /* When there is SA. */
711 if (isr->sav != NULL)
715 if ((error = key_acquire(saidx, isr->sp)) != 0) {
716 /* XXX What should I do ? */
717 ipseclog((LOG_DEBUG, "key_checkrequest: error %d returned "
718 "from key_acquire.\n", error));
722 return level == IPSEC_LEVEL_REQUIRE ? ENOENT : 0;
726 * allocating a SA for policy entry from SAD.
727 * NOTE: searching SAD of aliving state.
728 * OUT: NULL: not found.
729 * others: found and return the pointer.
731 static struct secasvar *
732 key_allocsa_policy(saidx)
733 struct secasindex *saidx;
735 struct secashead *sah;
736 struct secasvar *sav;
737 u_int stateidx, state;
738 const u_int *saorder_state_valid;
741 LIST_FOREACH(sah, &sahtree, chain) {
742 if (sah->state == SADB_SASTATE_DEAD)
744 if (key_cmpsaidx(&sah->saidx, saidx, CMP_MODE_REQID))
753 * search a valid state list for outbound packet.
754 * This search order is important.
756 if (key_preferred_oldsa) {
757 saorder_state_valid = saorder_state_valid_prefer_old;
758 arraysize = _ARRAYLEN(saorder_state_valid_prefer_old);
760 saorder_state_valid = saorder_state_valid_prefer_new;
761 arraysize = _ARRAYLEN(saorder_state_valid_prefer_new);
764 for (stateidx = 0; stateidx < arraysize; stateidx++) {
766 state = saorder_state_valid[stateidx];
768 sav = key_do_allocsa_policy(sah, state);
777 * searching SAD with direction, protocol, mode and state.
778 * called by key_allocsa_policy().
781 * others : found, pointer to a SA.
783 static struct secasvar *
784 key_do_allocsa_policy(sah, state)
785 struct secashead *sah;
788 struct secasvar *sav, *nextsav, *candidate, *d;
793 for (sav = LIST_FIRST(&sah->savtree[state]);
797 nextsav = LIST_NEXT(sav, chain);
800 KEY_CHKSASTATE(sav->state, state, "key_do_allocsa_policy");
803 if (candidate == NULL) {
808 /* Which SA is the better ? */
811 if (candidate->lft_c == NULL || sav->lft_c == NULL)
812 panic("key_do_allocsa_policy: "
813 "lifetime_current is NULL.\n");
815 /* What the best method is to compare ? */
816 if (key_preferred_oldsa) {
817 if (candidate->lft_c->sadb_lifetime_addtime >
818 sav->lft_c->sadb_lifetime_addtime) {
825 /* prefered new sa rather than old sa */
826 if (candidate->lft_c->sadb_lifetime_addtime <
827 sav->lft_c->sadb_lifetime_addtime) {
834 * prepared to delete the SA when there is more
835 * suitable candidate and the lifetime of the SA is not
838 if (d->lft_c->sadb_lifetime_addtime != 0) {
839 struct mbuf *m, *result;
841 key_sa_chgstate(d, SADB_SASTATE_DEAD);
843 m = key_setsadbmsg(SADB_DELETE, 0,
844 d->sah->saidx.proto, 0, 0, d->refcnt - 1);
849 /* set sadb_address for saidx's. */
850 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
851 (struct sockaddr *)&d->sah->saidx.src,
852 d->sah->saidx.src.ss_len << 3,
858 /* set sadb_address for saidx's. */
859 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
860 (struct sockaddr *)&d->sah->saidx.src,
861 d->sah->saidx.src.ss_len << 3,
867 /* create SA extension */
868 m = key_setsadbsa(d);
873 if (result->m_len < sizeof(struct sadb_msg)) {
874 result = m_pullup(result,
875 sizeof(struct sadb_msg));
880 result->m_pkthdr.len = 0;
881 for (m = result; m; m = m->m_next)
882 result->m_pkthdr.len += m->m_len;
883 mtod(result, struct sadb_msg *)->sadb_msg_len =
884 PFKEY_UNIT64(result->m_pkthdr.len);
886 if (key_sendup_mbuf(NULL, result,
887 KEY_SENDUP_REGISTERED))
896 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
897 printf("DP allocsa_policy cause "
898 "refcnt++:%d SA:%p\n",
899 candidate->refcnt, candidate));
905 * allocating a SA entry for a *INBOUND* packet.
906 * Must call key_freesav() later.
907 * OUT: positive: pointer to a sav.
908 * NULL: not found, or error occured.
910 * In the comparison, source address will be ignored for RFC2401 conformance.
911 * To quote, from section 4.1:
912 * A security association is uniquely identified by a triple consisting
913 * of a Security Parameter Index (SPI), an IP Destination Address, and a
914 * security protocol (AH or ESP) identifier.
915 * Note that, however, we do need to keep source address in IPsec SA.
916 * IKE specification and PF_KEY specification do assume that we
917 * keep source address in IPsec SA. We see a tricky situation here.
920 key_allocsa(family, src, dst, proto, spi)
925 struct secashead *sah;
926 struct secasvar *sav;
927 u_int stateidx, state;
928 struct sockaddr_in sin;
929 struct sockaddr_in6 sin6;
931 const u_int *saorder_state_valid;
935 if (src == NULL || dst == NULL)
936 panic("key_allocsa: NULL pointer is passed.\n");
939 * when both systems employ similar strategy to use a SA.
940 * the search order is important even in the inbound case.
942 if (key_preferred_oldsa) {
943 saorder_state_valid = saorder_state_valid_prefer_old;
944 arraysize = _ARRAYLEN(saorder_state_valid_prefer_old);
946 saorder_state_valid = saorder_state_valid_prefer_new;
947 arraysize = _ARRAYLEN(saorder_state_valid_prefer_new);
952 * XXX: to be checked internal IP header somewhere. Also when
953 * IPsec tunnel packet is received. But ESP tunnel mode is
954 * encrypted so we can't check internal IP header.
956 s = splnet(); /*called from softclock()*/
957 LIST_FOREACH(sah, &sahtree, chain) {
959 * search a valid state list for inbound packet.
960 * the search order is not important.
962 for (stateidx = 0; stateidx < arraysize; stateidx++) {
963 state = saorder_state_valid[stateidx];
964 LIST_FOREACH(sav, &sah->savtree[state], chain) {
966 KEY_CHKSASTATE(sav->state, state, "key_allocsav");
967 if (proto != sav->sah->saidx.proto)
971 if (family != sav->sah->saidx.src.ss_family ||
972 family != sav->sah->saidx.dst.ss_family)
975 #if 0 /* don't check src */
976 /* check src address */
979 bzero(&sin, sizeof(sin));
980 sin.sin_family = AF_INET;
981 sin.sin_len = sizeof(sin);
982 bcopy(src, &sin.sin_addr,
983 sizeof(sin.sin_addr));
984 if (key_sockaddrcmp((struct sockaddr*)&sin,
985 (struct sockaddr *)&sav->sah->saidx.src, 0) != 0)
990 bzero(&sin6, sizeof(sin6));
991 sin6.sin6_family = AF_INET6;
992 sin6.sin6_len = sizeof(sin6);
993 bcopy(src, &sin6.sin6_addr,
994 sizeof(sin6.sin6_addr));
995 if (IN6_IS_SCOPE_LINKLOCAL(&sin6.sin6_addr)) {
996 /* kame fake scopeid */
998 ntohs(sin6.sin6_addr.s6_addr16[1]);
999 sin6.sin6_addr.s6_addr16[1] = 0;
1001 if (key_sockaddrcmp((struct sockaddr*)&sin6,
1002 (struct sockaddr *)&sav->sah->saidx.src, 0) != 0)
1006 ipseclog((LOG_DEBUG, "key_allocsa: "
1007 "unknown address family=%d.\n",
1013 /* check dst address */
1016 bzero(&sin, sizeof(sin));
1017 sin.sin_family = AF_INET;
1018 sin.sin_len = sizeof(sin);
1019 bcopy(dst, &sin.sin_addr,
1020 sizeof(sin.sin_addr));
1021 if (key_sockaddrcmp((struct sockaddr*)&sin,
1022 (struct sockaddr *)&sav->sah->saidx.dst, 0) != 0)
1027 bzero(&sin6, sizeof(sin6));
1028 sin6.sin6_family = AF_INET6;
1029 sin6.sin6_len = sizeof(sin6);
1030 bcopy(dst, &sin6.sin6_addr,
1031 sizeof(sin6.sin6_addr));
1032 if (IN6_IS_SCOPE_LINKLOCAL(&sin6.sin6_addr)) {
1033 /* kame fake scopeid */
1034 sin6.sin6_scope_id =
1035 ntohs(sin6.sin6_addr.s6_addr16[1]);
1036 sin6.sin6_addr.s6_addr16[1] = 0;
1038 if (key_sockaddrcmp((struct sockaddr*)&sin6,
1039 (struct sockaddr *)&sav->sah->saidx.dst, 0) != 0)
1043 ipseclog((LOG_DEBUG, "key_allocsa: "
1044 "unknown address family=%d.\n",
1061 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1062 printf("DP allocsa cause refcnt++:%d SA:%p\n",
1068 * Must be called after calling key_allocsp().
1069 * For both the packet without socket and key_freeso().
1073 struct secpolicy *sp;
1077 panic("key_freesp: NULL pointer is passed.\n");
1080 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1081 printf("DP freesp cause refcnt--:%d SP:%p\n",
1084 if (sp->refcnt == 0)
1091 * Must be called after calling key_allocsp().
1092 * For the packet with socket.
1100 panic("key_freeso: NULL pointer is passed.\n");
1102 switch (so->so_proto->pr_domain->dom_family) {
1106 struct inpcb *pcb = sotoinpcb(so);
1108 /* Does it have a PCB ? */
1111 key_freesp_so(&pcb->inp_sp->sp_in);
1112 key_freesp_so(&pcb->inp_sp->sp_out);
1119 #ifdef HAVE_NRL_INPCB
1120 struct inpcb *pcb = sotoinpcb(so);
1122 /* Does it have a PCB ? */
1125 key_freesp_so(&pcb->inp_sp->sp_in);
1126 key_freesp_so(&pcb->inp_sp->sp_out);
1128 struct in6pcb *pcb = sotoin6pcb(so);
1130 /* Does it have a PCB ? */
1133 key_freesp_so(&pcb->in6p_sp->sp_in);
1134 key_freesp_so(&pcb->in6p_sp->sp_out);
1140 ipseclog((LOG_DEBUG, "key_freeso: unknown address family=%d.\n",
1141 so->so_proto->pr_domain->dom_family));
1150 struct secpolicy **sp;
1153 if (sp == NULL || *sp == NULL)
1154 panic("key_freesp_so: sp == NULL\n");
1156 switch ((*sp)->policy) {
1157 case IPSEC_POLICY_IPSEC:
1158 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1159 printf("DP freeso calls free SP:%p\n", *sp));
1163 case IPSEC_POLICY_ENTRUST:
1164 case IPSEC_POLICY_BYPASS:
1167 panic("key_freesp_so: Invalid policy found %d", (*sp)->policy);
1174 * Must be called after calling key_allocsa().
1175 * This function is called by key_freesp() to free some SA allocated
1180 struct secasvar *sav;
1184 panic("key_freesav: NULL pointer is passed.\n");
1187 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1188 printf("DP freesav cause refcnt--:%d SA:%p SPI %u\n",
1189 sav->refcnt, sav, (u_int32_t)ntohl(sav->spi)));
1191 if (sav->refcnt == 0)
1197 /* %%% SPD management */
1199 * free security policy entry.
1203 struct secpolicy *sp;
1209 panic("key_delsp: NULL pointer is passed.\n");
1211 sp->state = IPSEC_SPSTATE_DEAD;
1214 return; /* can't free */
1216 s = splnet(); /*called from softclock()*/
1217 /* remove from SP index */
1218 if (__LIST_CHAINED(sp))
1219 LIST_REMOVE(sp, chain);
1222 struct ipsecrequest *isr = sp->req, *nextisr;
1224 while (isr != NULL) {
1225 if (isr->sav != NULL) {
1226 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1227 printf("DP delsp calls free SA:%p\n",
1229 key_freesav(isr->sav);
1233 nextisr = isr->next;
1239 keydb_delsecpolicy(sp);
1248 * OUT: NULL : not found
1249 * others : found, pointer to a SP.
1251 static struct secpolicy *
1253 struct secpolicyindex *spidx;
1255 struct secpolicy *sp;
1259 panic("key_getsp: NULL pointer is passed.\n");
1261 LIST_FOREACH(sp, &sptree[spidx->dir], chain) {
1262 if (sp->state == IPSEC_SPSTATE_DEAD)
1264 if (key_cmpspidx_exactly(spidx, &sp->spidx)) {
1275 * OUT: NULL : not found
1276 * others : found, pointer to a SP.
1278 static struct secpolicy *
1282 struct secpolicy *sp;
1284 LIST_FOREACH(sp, &sptree[IPSEC_DIR_INBOUND], chain) {
1285 if (sp->state == IPSEC_SPSTATE_DEAD)
1293 LIST_FOREACH(sp, &sptree[IPSEC_DIR_OUTBOUND], chain) {
1294 if (sp->state == IPSEC_SPSTATE_DEAD)
1308 struct secpolicy *newsp = NULL;
1310 newsp = keydb_newsecpolicy();
1321 * create secpolicy structure from sadb_x_policy structure.
1322 * NOTE: `state', `secpolicyindex' in secpolicy structure are not set,
1323 * so must be set properly later.
1326 key_msg2sp(xpl0, len, error)
1327 struct sadb_x_policy *xpl0;
1331 struct secpolicy *newsp;
1335 panic("key_msg2sp: NULL pointer was passed.\n");
1336 if (len < sizeof(*xpl0))
1337 panic("key_msg2sp: invalid length.\n");
1338 if (len != PFKEY_EXTLEN(xpl0)) {
1339 ipseclog((LOG_DEBUG, "key_msg2sp: Invalid msg length.\n"));
1344 if ((newsp = key_newsp()) == NULL) {
1349 newsp->spidx.dir = xpl0->sadb_x_policy_dir;
1350 newsp->policy = xpl0->sadb_x_policy_type;
1353 switch (xpl0->sadb_x_policy_type) {
1354 case IPSEC_POLICY_DISCARD:
1355 case IPSEC_POLICY_NONE:
1356 case IPSEC_POLICY_ENTRUST:
1357 case IPSEC_POLICY_BYPASS:
1361 case IPSEC_POLICY_IPSEC:
1364 struct sadb_x_ipsecrequest *xisr;
1365 struct ipsecrequest **p_isr = &newsp->req;
1367 /* validity check */
1368 if (PFKEY_EXTLEN(xpl0) < sizeof(*xpl0)) {
1369 ipseclog((LOG_DEBUG,
1370 "key_msg2sp: Invalid msg length.\n"));
1376 tlen = PFKEY_EXTLEN(xpl0) - sizeof(*xpl0);
1377 xisr = (struct sadb_x_ipsecrequest *)(xpl0 + 1);
1382 if (xisr->sadb_x_ipsecrequest_len < sizeof(*xisr)) {
1383 ipseclog((LOG_DEBUG, "key_msg2sp: "
1384 "invalid ipsecrequest length.\n"));
1390 /* allocate request buffer */
1391 KMALLOC(*p_isr, struct ipsecrequest *, sizeof(**p_isr));
1392 if ((*p_isr) == NULL) {
1393 ipseclog((LOG_DEBUG,
1394 "key_msg2sp: No more memory.\n"));
1399 bzero(*p_isr, sizeof(**p_isr));
1402 (*p_isr)->next = NULL;
1404 switch (xisr->sadb_x_ipsecrequest_proto) {
1407 case IPPROTO_IPCOMP:
1410 ipseclog((LOG_DEBUG,
1411 "key_msg2sp: invalid proto type=%u\n",
1412 xisr->sadb_x_ipsecrequest_proto));
1414 *error = EPROTONOSUPPORT;
1417 (*p_isr)->saidx.proto = xisr->sadb_x_ipsecrequest_proto;
1419 switch (xisr->sadb_x_ipsecrequest_mode) {
1420 case IPSEC_MODE_TRANSPORT:
1421 case IPSEC_MODE_TUNNEL:
1423 case IPSEC_MODE_ANY:
1425 ipseclog((LOG_DEBUG,
1426 "key_msg2sp: invalid mode=%u\n",
1427 xisr->sadb_x_ipsecrequest_mode));
1432 (*p_isr)->saidx.mode = xisr->sadb_x_ipsecrequest_mode;
1434 switch (xisr->sadb_x_ipsecrequest_level) {
1435 case IPSEC_LEVEL_DEFAULT:
1436 case IPSEC_LEVEL_USE:
1437 case IPSEC_LEVEL_REQUIRE:
1439 case IPSEC_LEVEL_UNIQUE:
1440 /* validity check */
1442 * If range violation of reqid, kernel will
1443 * update it, don't refuse it.
1445 if (xisr->sadb_x_ipsecrequest_reqid
1446 > IPSEC_MANUAL_REQID_MAX) {
1447 ipseclog((LOG_DEBUG,
1448 "key_msg2sp: reqid=%d range "
1449 "violation, updated by kernel.\n",
1450 xisr->sadb_x_ipsecrequest_reqid));
1451 xisr->sadb_x_ipsecrequest_reqid = 0;
1454 /* allocate new reqid id if reqid is zero. */
1455 if (xisr->sadb_x_ipsecrequest_reqid == 0) {
1457 if ((reqid = key_newreqid()) == 0) {
1462 (*p_isr)->saidx.reqid = reqid;
1463 xisr->sadb_x_ipsecrequest_reqid = reqid;
1465 /* set it for manual keying. */
1466 (*p_isr)->saidx.reqid =
1467 xisr->sadb_x_ipsecrequest_reqid;
1472 ipseclog((LOG_DEBUG, "key_msg2sp: invalid level=%u\n",
1473 xisr->sadb_x_ipsecrequest_level));
1478 (*p_isr)->level = xisr->sadb_x_ipsecrequest_level;
1480 /* set IP addresses if there */
1481 if (xisr->sadb_x_ipsecrequest_len > sizeof(*xisr)) {
1482 struct sockaddr *paddr;
1484 paddr = (struct sockaddr *)(xisr + 1);
1486 /* validity check */
1488 > sizeof((*p_isr)->saidx.src)) {
1489 ipseclog((LOG_DEBUG, "key_msg2sp: invalid request "
1490 "address length.\n"));
1495 bcopy(paddr, &(*p_isr)->saidx.src,
1498 paddr = (struct sockaddr *)((caddr_t)paddr
1501 /* validity check */
1503 > sizeof((*p_isr)->saidx.dst)) {
1504 ipseclog((LOG_DEBUG, "key_msg2sp: invalid request "
1505 "address length.\n"));
1510 bcopy(paddr, &(*p_isr)->saidx.dst,
1514 (*p_isr)->sav = NULL;
1515 (*p_isr)->sp = newsp;
1517 /* initialization for the next. */
1518 p_isr = &(*p_isr)->next;
1519 tlen -= xisr->sadb_x_ipsecrequest_len;
1521 /* validity check */
1523 ipseclog((LOG_DEBUG, "key_msg2sp: becoming tlen < 0.\n"));
1529 xisr = (struct sadb_x_ipsecrequest *)((caddr_t)xisr
1530 + xisr->sadb_x_ipsecrequest_len);
1535 ipseclog((LOG_DEBUG, "key_msg2sp: invalid policy type.\n"));
1548 static u_int32_t auto_reqid = IPSEC_MANUAL_REQID_MAX + 1;
1550 auto_reqid = (auto_reqid == ~0
1551 ? IPSEC_MANUAL_REQID_MAX + 1 : auto_reqid + 1);
1553 /* XXX should be unique check */
1559 * copy secpolicy struct to sadb_x_policy structure indicated.
1563 struct secpolicy *sp;
1565 struct sadb_x_policy *xpl;
1572 panic("key_sp2msg: NULL pointer was passed.\n");
1574 tlen = key_getspreqmsglen(sp);
1576 m = key_alloc_mbuf(tlen);
1577 if (!m || m->m_next) { /*XXX*/
1585 xpl = mtod(m, struct sadb_x_policy *);
1588 xpl->sadb_x_policy_len = PFKEY_UNIT64(tlen);
1589 xpl->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
1590 xpl->sadb_x_policy_type = sp->policy;
1591 xpl->sadb_x_policy_dir = sp->spidx.dir;
1592 xpl->sadb_x_policy_id = sp->id;
1593 p = (caddr_t)xpl + sizeof(*xpl);
1595 /* if is the policy for ipsec ? */
1596 if (sp->policy == IPSEC_POLICY_IPSEC) {
1597 struct sadb_x_ipsecrequest *xisr;
1598 struct ipsecrequest *isr;
1600 for (isr = sp->req; isr != NULL; isr = isr->next) {
1602 xisr = (struct sadb_x_ipsecrequest *)p;
1604 xisr->sadb_x_ipsecrequest_proto = isr->saidx.proto;
1605 xisr->sadb_x_ipsecrequest_mode = isr->saidx.mode;
1606 xisr->sadb_x_ipsecrequest_level = isr->level;
1607 xisr->sadb_x_ipsecrequest_reqid = isr->saidx.reqid;
1610 bcopy(&isr->saidx.src, p, isr->saidx.src.ss_len);
1611 p += isr->saidx.src.ss_len;
1612 bcopy(&isr->saidx.dst, p, isr->saidx.dst.ss_len);
1613 p += isr->saidx.src.ss_len;
1615 xisr->sadb_x_ipsecrequest_len =
1616 PFKEY_ALIGN8(sizeof(*xisr)
1617 + isr->saidx.src.ss_len
1618 + isr->saidx.dst.ss_len);
1625 /* m will not be freed nor modified */
1626 static struct mbuf *
1627 key_gather_mbuf(struct mbuf *m, const struct sadb_msghdr *mhp,
1628 int ndeep, int nitem, ...)
1633 struct mbuf *result = NULL, *n;
1636 if (m == NULL || mhp == NULL)
1637 panic("null pointer passed to key_gather");
1639 __va_start(ap, nitem);
1640 for (i = 0; i < nitem; i++) {
1641 idx = __va_arg(ap, int);
1642 if (idx < 0 || idx > SADB_EXT_MAX)
1644 /* don't attempt to pull empty extension */
1645 if (idx == SADB_EXT_RESERVED && mhp->msg == NULL)
1647 if (idx != SADB_EXT_RESERVED &&
1648 (mhp->ext[idx] == NULL || mhp->extlen[idx] == 0))
1651 if (idx == SADB_EXT_RESERVED) {
1652 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
1655 panic("assumption failed");
1657 MGETHDR(n, MB_DONTWAIT, MT_DATA);
1662 m_copydata(m, 0, sizeof(struct sadb_msg),
1664 } else if (i < ndeep) {
1665 len = mhp->extlen[idx];
1666 n = key_alloc_mbuf(len);
1667 if (!n || n->m_next) { /*XXX*/
1672 m_copydata(m, mhp->extoff[idx], mhp->extlen[idx],
1675 n = m_copym(m, mhp->extoff[idx], mhp->extlen[idx],
1688 if ((result->m_flags & M_PKTHDR) != 0) {
1689 result->m_pkthdr.len = 0;
1690 for (n = result; n; n = n->m_next)
1691 result->m_pkthdr.len += n->m_len;
1702 * SADB_X_SPDADD, SADB_X_SPDSETIDX or SADB_X_SPDUPDATE processing
1703 * add a entry to SP database, when received
1704 * <base, address(SD), (lifetime(H),) policy>
1706 * Adding to SP database,
1708 * <base, address(SD), (lifetime(H),) policy>
1709 * to the socket which was send.
1711 * SPDADD set a unique policy entry.
1712 * SPDSETIDX like SPDADD without a part of policy requests.
1713 * SPDUPDATE replace a unique policy entry.
1715 * m will always be freed.
1718 key_spdadd(so, m, mhp)
1721 const struct sadb_msghdr *mhp;
1723 struct sadb_address *src0, *dst0;
1724 struct sadb_x_policy *xpl0, *xpl;
1725 struct sadb_lifetime *lft = NULL;
1726 struct secpolicyindex spidx;
1727 struct secpolicy *newsp;
1732 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
1733 panic("key_spdadd: NULL pointer is passed.\n");
1735 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
1736 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
1737 mhp->ext[SADB_X_EXT_POLICY] == NULL) {
1738 ipseclog((LOG_DEBUG, "key_spdadd: invalid message is passed.\n"));
1739 return key_senderror(so, m, EINVAL);
1741 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
1742 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
1743 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
1744 ipseclog((LOG_DEBUG, "key_spdadd: invalid message is passed.\n"));
1745 return key_senderror(so, m, EINVAL);
1747 if (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL) {
1748 if (mhp->extlen[SADB_EXT_LIFETIME_HARD]
1749 < sizeof(struct sadb_lifetime)) {
1750 ipseclog((LOG_DEBUG, "key_spdadd: invalid message is passed.\n"));
1751 return key_senderror(so, m, EINVAL);
1753 lft = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_HARD];
1756 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
1757 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
1758 xpl0 = (struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY];
1761 /* XXX boundary check against sa_len */
1762 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
1765 src0->sadb_address_prefixlen,
1766 dst0->sadb_address_prefixlen,
1767 src0->sadb_address_proto,
1770 /* checking the direciton. */
1771 switch (xpl0->sadb_x_policy_dir) {
1772 case IPSEC_DIR_INBOUND:
1773 case IPSEC_DIR_OUTBOUND:
1776 ipseclog((LOG_DEBUG, "key_spdadd: Invalid SP direction.\n"));
1777 mhp->msg->sadb_msg_errno = EINVAL;
1782 /* key_spdadd() accepts DISCARD, NONE and IPSEC. */
1783 if (xpl0->sadb_x_policy_type == IPSEC_POLICY_ENTRUST
1784 || xpl0->sadb_x_policy_type == IPSEC_POLICY_BYPASS) {
1785 ipseclog((LOG_DEBUG, "key_spdadd: Invalid policy type.\n"));
1786 return key_senderror(so, m, EINVAL);
1789 /* policy requests are mandatory when action is ipsec. */
1790 if (mhp->msg->sadb_msg_type != SADB_X_SPDSETIDX
1791 && xpl0->sadb_x_policy_type == IPSEC_POLICY_IPSEC
1792 && mhp->extlen[SADB_X_EXT_POLICY] <= sizeof(*xpl0)) {
1793 ipseclog((LOG_DEBUG, "key_spdadd: some policy requests part required.\n"));
1794 return key_senderror(so, m, EINVAL);
1798 * checking there is SP already or not.
1799 * SPDUPDATE doesn't depend on whether there is a SP or not.
1800 * If the type is either SPDADD or SPDSETIDX AND a SP is found,
1803 newsp = key_getsp(&spidx);
1804 if (mhp->msg->sadb_msg_type == SADB_X_SPDUPDATE) {
1806 newsp->state = IPSEC_SPSTATE_DEAD;
1810 if (newsp != NULL) {
1812 ipseclog((LOG_DEBUG, "key_spdadd: a SP entry exists already.\n"));
1813 return key_senderror(so, m, EEXIST);
1817 /* allocation new SP entry */
1818 if ((newsp = key_msg2sp(xpl0, PFKEY_EXTLEN(xpl0), &error)) == NULL) {
1819 return key_senderror(so, m, error);
1822 if ((newsp->id = key_getnewspid()) == 0) {
1823 keydb_delsecpolicy(newsp);
1824 return key_senderror(so, m, ENOBUFS);
1827 /* XXX boundary check against sa_len */
1828 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
1831 src0->sadb_address_prefixlen,
1832 dst0->sadb_address_prefixlen,
1833 src0->sadb_address_proto,
1836 /* sanity check on addr pair */
1837 if (((struct sockaddr *)(src0 + 1))->sa_family !=
1838 ((struct sockaddr *)(dst0+ 1))->sa_family) {
1839 keydb_delsecpolicy(newsp);
1840 return key_senderror(so, m, EINVAL);
1842 if (((struct sockaddr *)(src0 + 1))->sa_len !=
1843 ((struct sockaddr *)(dst0+ 1))->sa_len) {
1844 keydb_delsecpolicy(newsp);
1845 return key_senderror(so, m, EINVAL);
1848 if (newsp->req && newsp->req->saidx.src.ss_family) {
1849 struct sockaddr *sa;
1850 sa = (struct sockaddr *)(src0 + 1);
1851 if (sa->sa_family != newsp->req->saidx.src.ss_family) {
1852 keydb_delsecpolicy(newsp);
1853 return key_senderror(so, m, EINVAL);
1856 if (newsp->req && newsp->req->saidx.dst.ss_family) {
1857 struct sockaddr *sa;
1858 sa = (struct sockaddr *)(dst0 + 1);
1859 if (sa->sa_family != newsp->req->saidx.dst.ss_family) {
1860 keydb_delsecpolicy(newsp);
1861 return key_senderror(so, m, EINVAL);
1867 newsp->created = tv.tv_sec;
1868 newsp->lastused = tv.tv_sec;
1869 newsp->lifetime = lft ? lft->sadb_lifetime_addtime : 0;
1870 newsp->validtime = lft ? lft->sadb_lifetime_usetime : 0;
1872 newsp->refcnt = 1; /* do not reclaim until I say I do */
1873 newsp->state = IPSEC_SPSTATE_ALIVE;
1874 LIST_INSERT_TAIL(&sptree[newsp->spidx.dir], newsp, secpolicy, chain);
1876 /* delete the entry in spacqtree */
1877 if (mhp->msg->sadb_msg_type == SADB_X_SPDUPDATE) {
1878 struct secspacq *spacq;
1879 if ((spacq = key_getspacq(&spidx)) != NULL) {
1880 /* reset counter in order to deletion by timehandler. */
1882 spacq->created = tv.tv_sec;
1888 struct mbuf *n, *mpolicy;
1889 struct sadb_msg *newmsg;
1892 /* create new sadb_msg to reply. */
1894 n = key_gather_mbuf(m, mhp, 2, 5, SADB_EXT_RESERVED,
1895 SADB_X_EXT_POLICY, SADB_EXT_LIFETIME_HARD,
1896 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
1898 n = key_gather_mbuf(m, mhp, 2, 4, SADB_EXT_RESERVED,
1900 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
1903 return key_senderror(so, m, ENOBUFS);
1905 if (n->m_len < sizeof(*newmsg)) {
1906 n = m_pullup(n, sizeof(*newmsg));
1908 return key_senderror(so, m, ENOBUFS);
1910 newmsg = mtod(n, struct sadb_msg *);
1911 newmsg->sadb_msg_errno = 0;
1912 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
1915 mpolicy = m_pulldown(n, PFKEY_ALIGN8(sizeof(struct sadb_msg)),
1916 sizeof(*xpl), &off);
1917 if (mpolicy == NULL) {
1918 /* n is already freed */
1919 return key_senderror(so, m, ENOBUFS);
1921 xpl = (struct sadb_x_policy *)(mtod(mpolicy, caddr_t) + off);
1922 if (xpl->sadb_x_policy_exttype != SADB_X_EXT_POLICY) {
1924 return key_senderror(so, m, EINVAL);
1926 xpl->sadb_x_policy_id = newsp->id;
1929 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
1934 * get new policy id.
1942 u_int32_t newid = 0;
1943 int count = key_spi_trycnt; /* XXX */
1944 struct secpolicy *sp;
1946 /* when requesting to allocate spi ranged */
1948 newid = (policy_id = (policy_id == ~0 ? 1 : policy_id + 1));
1950 if ((sp = key_getspbyid(newid)) == NULL)
1956 if (count == 0 || newid == 0) {
1957 ipseclog((LOG_DEBUG, "key_getnewspid: to allocate policy id is failed.\n"));
1965 * SADB_SPDDELETE processing
1967 * <base, address(SD), policy(*)>
1968 * from the user(?), and set SADB_SASTATE_DEAD,
1970 * <base, address(SD), policy(*)>
1972 * policy(*) including direction of policy.
1974 * m will always be freed.
1977 key_spddelete(so, m, mhp)
1980 const struct sadb_msghdr *mhp;
1982 struct sadb_address *src0, *dst0;
1983 struct sadb_x_policy *xpl0;
1984 struct secpolicyindex spidx;
1985 struct secpolicy *sp;
1988 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
1989 panic("key_spddelete: NULL pointer is passed.\n");
1991 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
1992 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
1993 mhp->ext[SADB_X_EXT_POLICY] == NULL) {
1994 ipseclog((LOG_DEBUG, "key_spddelete: invalid message is passed.\n"));
1995 return key_senderror(so, m, EINVAL);
1997 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
1998 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
1999 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2000 ipseclog((LOG_DEBUG, "key_spddelete: invalid message is passed.\n"));
2001 return key_senderror(so, m, EINVAL);
2004 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
2005 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
2006 xpl0 = (struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY];
2009 /* XXX boundary check against sa_len */
2010 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
2013 src0->sadb_address_prefixlen,
2014 dst0->sadb_address_prefixlen,
2015 src0->sadb_address_proto,
2018 /* checking the direciton. */
2019 switch (xpl0->sadb_x_policy_dir) {
2020 case IPSEC_DIR_INBOUND:
2021 case IPSEC_DIR_OUTBOUND:
2024 ipseclog((LOG_DEBUG, "key_spddelete: Invalid SP direction.\n"));
2025 return key_senderror(so, m, EINVAL);
2028 /* Is there SP in SPD ? */
2029 if ((sp = key_getsp(&spidx)) == NULL) {
2030 ipseclog((LOG_DEBUG, "key_spddelete: no SP found.\n"));
2031 return key_senderror(so, m, EINVAL);
2034 /* save policy id to buffer to be returned. */
2035 xpl0->sadb_x_policy_id = sp->id;
2037 sp->state = IPSEC_SPSTATE_DEAD;
2042 struct sadb_msg *newmsg;
2044 /* create new sadb_msg to reply. */
2045 n = key_gather_mbuf(m, mhp, 1, 4, SADB_EXT_RESERVED,
2046 SADB_X_EXT_POLICY, SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
2048 return key_senderror(so, m, ENOBUFS);
2050 newmsg = mtod(n, struct sadb_msg *);
2051 newmsg->sadb_msg_errno = 0;
2052 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
2055 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
2060 * SADB_SPDDELETE2 processing
2063 * from the user(?), and set SADB_SASTATE_DEAD,
2067 * policy(*) including direction of policy.
2069 * m will always be freed.
2072 key_spddelete2(so, m, mhp)
2075 const struct sadb_msghdr *mhp;
2078 struct secpolicy *sp;
2081 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
2082 panic("key_spddelete2: NULL pointer is passed.\n");
2084 if (mhp->ext[SADB_X_EXT_POLICY] == NULL ||
2085 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2086 ipseclog((LOG_DEBUG, "key_spddelete2: invalid message is passed.\n"));
2087 key_senderror(so, m, EINVAL);
2091 id = ((struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY])->sadb_x_policy_id;
2093 /* Is there SP in SPD ? */
2094 if ((sp = key_getspbyid(id)) == NULL) {
2095 ipseclog((LOG_DEBUG, "key_spddelete2: no SP found id:%u.\n", id));
2096 key_senderror(so, m, EINVAL);
2099 sp->state = IPSEC_SPSTATE_DEAD;
2103 struct mbuf *n, *nn;
2104 struct sadb_msg *newmsg;
2107 /* create new sadb_msg to reply. */
2108 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
2111 return key_senderror(so, m, ENOBUFS);
2112 MGETHDR(n, MB_DONTWAIT, MT_DATA);
2113 if (n && len > MHLEN) {
2114 MCLGET(n, MB_DONTWAIT);
2115 if ((n->m_flags & M_EXT) == 0) {
2121 return key_senderror(so, m, ENOBUFS);
2127 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
2128 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
2132 panic("length inconsistency in key_spddelete2");
2135 n->m_next = m_copym(m, mhp->extoff[SADB_X_EXT_POLICY],
2136 mhp->extlen[SADB_X_EXT_POLICY], MB_DONTWAIT);
2139 return key_senderror(so, m, ENOBUFS);
2142 n->m_pkthdr.len = 0;
2143 for (nn = n; nn; nn = nn->m_next)
2144 n->m_pkthdr.len += nn->m_len;
2146 newmsg = mtod(n, struct sadb_msg *);
2147 newmsg->sadb_msg_errno = 0;
2148 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
2151 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
2156 * SADB_X_GET processing
2161 * <base, address(SD), policy>
2163 * policy(*) including direction of policy.
2165 * m will always be freed.
2168 key_spdget(so, m, mhp)
2171 const struct sadb_msghdr *mhp;
2174 struct secpolicy *sp;
2178 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
2179 panic("key_spdget: NULL pointer is passed.\n");
2181 if (mhp->ext[SADB_X_EXT_POLICY] == NULL ||
2182 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2183 ipseclog((LOG_DEBUG, "key_spdget: invalid message is passed.\n"));
2184 return key_senderror(so, m, EINVAL);
2187 id = ((struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY])->sadb_x_policy_id;
2189 /* Is there SP in SPD ? */
2190 if ((sp = key_getspbyid(id)) == NULL) {
2191 ipseclog((LOG_DEBUG, "key_spdget: no SP found id:%u.\n", id));
2192 return key_senderror(so, m, ENOENT);
2195 n = key_setdumpsp(sp, SADB_X_SPDGET, 0, mhp->msg->sadb_msg_pid);
2198 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
2200 return key_senderror(so, m, ENOBUFS);
2204 * SADB_X_SPDACQUIRE processing.
2205 * Acquire policy and SA(s) for a *OUTBOUND* packet.
2208 * to KMD, and expect to receive
2209 * <base> with SADB_X_SPDACQUIRE if error occured,
2212 * with SADB_X_SPDUPDATE from KMD by PF_KEY.
2213 * policy(*) is without policy requests.
2216 * others: error number
2220 struct secpolicy *sp;
2222 struct mbuf *result = NULL, *m;
2223 struct secspacq *newspacq;
2228 panic("key_spdacquire: NULL pointer is passed.\n");
2229 if (sp->req != NULL)
2230 panic("key_spdacquire: called but there is request.\n");
2231 if (sp->policy != IPSEC_POLICY_IPSEC)
2232 panic("key_spdacquire: policy mismathed. IPsec is expected.\n");
2234 /* get a entry to check whether sent message or not. */
2235 if ((newspacq = key_getspacq(&sp->spidx)) != NULL) {
2236 if (key_blockacq_count < newspacq->count) {
2237 /* reset counter and do send message. */
2238 newspacq->count = 0;
2240 /* increment counter and do nothing. */
2245 /* make new entry for blocking to send SADB_ACQUIRE. */
2246 if ((newspacq = key_newspacq(&sp->spidx)) == NULL)
2249 /* add to acqtree */
2250 LIST_INSERT_HEAD(&spacqtree, newspacq, chain);
2253 /* create new sadb_msg to reply. */
2254 m = key_setsadbmsg(SADB_X_SPDACQUIRE, 0, 0, 0, 0, 0);
2261 result->m_pkthdr.len = 0;
2262 for (m = result; m; m = m->m_next)
2263 result->m_pkthdr.len += m->m_len;
2265 mtod(result, struct sadb_msg *)->sadb_msg_len =
2266 PFKEY_UNIT64(result->m_pkthdr.len);
2268 return key_sendup_mbuf(NULL, m, KEY_SENDUP_REGISTERED);
2277 * SADB_SPDFLUSH processing
2280 * from the user, and free all entries in secpctree.
2284 * NOTE: what to do is only marking SADB_SASTATE_DEAD.
2286 * m will always be freed.
2289 key_spdflush(so, m, mhp)
2292 const struct sadb_msghdr *mhp;
2294 struct sadb_msg *newmsg;
2295 struct secpolicy *sp;
2299 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
2300 panic("key_spdflush: NULL pointer is passed.\n");
2302 if (m->m_len != PFKEY_ALIGN8(sizeof(struct sadb_msg)))
2303 return key_senderror(so, m, EINVAL);
2305 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2306 LIST_FOREACH(sp, &sptree[dir], chain) {
2307 sp->state = IPSEC_SPSTATE_DEAD;
2311 if (sizeof(struct sadb_msg) > m->m_len + M_TRAILINGSPACE(m)) {
2312 ipseclog((LOG_DEBUG, "key_spdflush: No more memory.\n"));
2313 return key_senderror(so, m, ENOBUFS);
2319 m->m_pkthdr.len = m->m_len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
2320 newmsg = mtod(m, struct sadb_msg *);
2321 newmsg->sadb_msg_errno = 0;
2322 newmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
2324 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
2328 * SADB_SPDDUMP processing
2331 * from the user, and dump all SP leaves
2336 * m will always be freed.
2339 key_spddump(so, m, mhp)
2342 const struct sadb_msghdr *mhp;
2344 struct secpolicy *sp;
2350 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
2351 panic("key_spddump: NULL pointer is passed.\n");
2353 /* search SPD entry and get buffer size. */
2355 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2356 LIST_FOREACH(sp, &sptree[dir], chain) {
2362 return key_senderror(so, m, ENOENT);
2364 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2365 LIST_FOREACH(sp, &sptree[dir], chain) {
2367 n = key_setdumpsp(sp, SADB_X_SPDDUMP, cnt,
2368 mhp->msg->sadb_msg_pid);
2371 key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
2379 static struct mbuf *
2380 key_setdumpsp(sp, type, seq, pid)
2381 struct secpolicy *sp;
2385 struct mbuf *result = NULL, *m;
2387 m = key_setsadbmsg(type, 0, SADB_SATYPE_UNSPEC, seq, pid, sp->refcnt);
2392 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
2393 (struct sockaddr *)&sp->spidx.src, sp->spidx.prefs,
2394 sp->spidx.ul_proto);
2399 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
2400 (struct sockaddr *)&sp->spidx.dst, sp->spidx.prefd,
2401 sp->spidx.ul_proto);
2411 if ((result->m_flags & M_PKTHDR) == 0)
2414 if (result->m_len < sizeof(struct sadb_msg)) {
2415 result = m_pullup(result, sizeof(struct sadb_msg));
2420 result->m_pkthdr.len = 0;
2421 for (m = result; m; m = m->m_next)
2422 result->m_pkthdr.len += m->m_len;
2424 mtod(result, struct sadb_msg *)->sadb_msg_len =
2425 PFKEY_UNIT64(result->m_pkthdr.len);
2435 * get PFKEY message length for security policy and request.
2438 key_getspreqmsglen(sp)
2439 struct secpolicy *sp;
2443 tlen = sizeof(struct sadb_x_policy);
2445 /* if is the policy for ipsec ? */
2446 if (sp->policy != IPSEC_POLICY_IPSEC)
2449 /* get length of ipsec requests */
2451 struct ipsecrequest *isr;
2454 for (isr = sp->req; isr != NULL; isr = isr->next) {
2455 len = sizeof(struct sadb_x_ipsecrequest)
2456 + isr->saidx.src.ss_len
2457 + isr->saidx.dst.ss_len;
2459 tlen += PFKEY_ALIGN8(len);
2467 * SADB_SPDEXPIRE processing
2469 * <base, address(SD), lifetime(CH), policy>
2473 * others : error number
2477 struct secpolicy *sp;
2480 struct mbuf *result = NULL, *m;
2483 struct sadb_lifetime *lt;
2485 /* XXX: Why do we lock ? */
2486 s = splnet(); /*called from softclock()*/
2490 panic("key_spdexpire: NULL pointer is passed.\n");
2492 /* set msg header */
2493 m = key_setsadbmsg(SADB_X_SPDEXPIRE, 0, 0, 0, 0, 0);
2500 /* create lifetime extension (current and hard) */
2501 len = PFKEY_ALIGN8(sizeof(*lt)) * 2;
2502 m = key_alloc_mbuf(len);
2503 if (!m || m->m_next) { /*XXX*/
2509 bzero(mtod(m, caddr_t), len);
2510 lt = mtod(m, struct sadb_lifetime *);
2511 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
2512 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
2513 lt->sadb_lifetime_allocations = 0;
2514 lt->sadb_lifetime_bytes = 0;
2515 lt->sadb_lifetime_addtime = sp->created;
2516 lt->sadb_lifetime_usetime = sp->lastused;
2517 lt = (struct sadb_lifetime *)(mtod(m, caddr_t) + len / 2);
2518 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
2519 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
2520 lt->sadb_lifetime_allocations = 0;
2521 lt->sadb_lifetime_bytes = 0;
2522 lt->sadb_lifetime_addtime = sp->lifetime;
2523 lt->sadb_lifetime_usetime = sp->validtime;
2526 /* set sadb_address for source */
2527 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
2528 (struct sockaddr *)&sp->spidx.src,
2529 sp->spidx.prefs, sp->spidx.ul_proto);
2536 /* set sadb_address for destination */
2537 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
2538 (struct sockaddr *)&sp->spidx.dst,
2539 sp->spidx.prefd, sp->spidx.ul_proto);
2554 if ((result->m_flags & M_PKTHDR) == 0) {
2559 if (result->m_len < sizeof(struct sadb_msg)) {
2560 result = m_pullup(result, sizeof(struct sadb_msg));
2561 if (result == NULL) {
2567 result->m_pkthdr.len = 0;
2568 for (m = result; m; m = m->m_next)
2569 result->m_pkthdr.len += m->m_len;
2571 mtod(result, struct sadb_msg *)->sadb_msg_len =
2572 PFKEY_UNIT64(result->m_pkthdr.len);
2574 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
2583 /* %%% SAD management */
2585 * allocating a memory for new SA head, and copy from the values of mhp.
2586 * OUT: NULL : failure due to the lack of memory.
2587 * others : pointer to new SA head.
2589 static struct secashead *
2591 struct secasindex *saidx;
2593 struct secashead *newsah;
2597 panic("key_newsaidx: NULL pointer is passed.\n");
2599 newsah = keydb_newsecashead();
2603 bcopy(saidx, &newsah->saidx, sizeof(newsah->saidx));
2605 /* add to saidxtree */
2606 newsah->state = SADB_SASTATE_MATURE;
2607 LIST_INSERT_HEAD(&sahtree, newsah, chain);
2613 * delete SA index and all SA registerd.
2617 struct secashead *sah;
2619 struct secasvar *sav, *nextsav;
2620 u_int stateidx, state;
2626 panic("key_delsah: NULL pointer is passed.\n");
2628 s = splnet(); /*called from softclock()*/
2630 /* searching all SA registerd in the secindex. */
2632 stateidx < _ARRAYLEN(saorder_state_any);
2635 state = saorder_state_any[stateidx];
2636 for (sav = (struct secasvar *)LIST_FIRST(&sah->savtree[state]);
2640 nextsav = LIST_NEXT(sav, chain);
2642 if (sav->refcnt > 0) {
2643 /* give up to delete this sa */
2649 KEY_CHKSASTATE(state, sav->state, "key_delsah");
2653 /* remove back pointer */
2659 /* don't delete sah only if there are savs. */
2665 if (sah->sa_route.ro_rt) {
2666 RTFREE(sah->sa_route.ro_rt);
2667 sah->sa_route.ro_rt = (struct rtentry *)NULL;
2670 /* remove from tree of SA index */
2671 if (__LIST_CHAINED(sah))
2672 LIST_REMOVE(sah, chain);
2681 * allocating a new SA with LARVAL state. key_add() and key_getspi() call,
2682 * and copy the values of mhp into new buffer.
2683 * When SAD message type is GETSPI:
2684 * to set sequence number from acq_seq++,
2685 * to set zero to SPI.
2686 * not to call key_setsava().
2688 * others : pointer to new secasvar.
2690 * does not modify mbuf. does not free mbuf on error.
2692 static struct secasvar *
2693 key_newsav(m, mhp, sah, errp)
2695 const struct sadb_msghdr *mhp;
2696 struct secashead *sah;
2699 struct secasvar *newsav;
2700 const struct sadb_sa *xsa;
2703 if (m == NULL || mhp == NULL || mhp->msg == NULL || sah == NULL)
2704 panic("key_newsa: NULL pointer is passed.\n");
2706 KMALLOC(newsav, struct secasvar *, sizeof(struct secasvar));
2707 if (newsav == NULL) {
2708 ipseclog((LOG_DEBUG, "key_newsa: No more memory.\n"));
2712 bzero((caddr_t)newsav, sizeof(struct secasvar));
2714 switch (mhp->msg->sadb_msg_type) {
2718 #ifdef IPSEC_DOSEQCHECK
2719 /* sync sequence number */
2720 if (mhp->msg->sadb_msg_seq == 0)
2722 (acq_seq = (acq_seq == ~0 ? 1 : ++acq_seq));
2725 newsav->seq = mhp->msg->sadb_msg_seq;
2730 if (mhp->ext[SADB_EXT_SA] == NULL) {
2732 ipseclog((LOG_DEBUG, "key_newsa: invalid message is passed.\n"));
2736 xsa = (const struct sadb_sa *)mhp->ext[SADB_EXT_SA];
2737 newsav->spi = xsa->sadb_sa_spi;
2738 newsav->seq = mhp->msg->sadb_msg_seq;
2746 /* copy sav values */
2747 if (mhp->msg->sadb_msg_type != SADB_GETSPI) {
2748 *errp = key_setsaval(newsav, m, mhp);
2759 newsav->created = tv.tv_sec;
2762 newsav->pid = mhp->msg->sadb_msg_pid;
2767 newsav->state = SADB_SASTATE_LARVAL;
2768 LIST_INSERT_TAIL(&sah->savtree[SADB_SASTATE_LARVAL], newsav,
2775 * free() SA variable entry.
2779 struct secasvar *sav;
2783 panic("key_delsav: NULL pointer is passed.\n");
2785 if (sav->refcnt > 0)
2786 return; /* can't free */
2788 /* remove from SA header */
2789 if (__LIST_CHAINED(sav))
2790 LIST_REMOVE(sav, chain);
2792 if (sav->key_auth != NULL) {
2793 bzero(_KEYBUF(sav->key_auth), _KEYLEN(sav->key_auth));
2794 KFREE(sav->key_auth);
2795 sav->key_auth = NULL;
2797 if (sav->key_enc != NULL) {
2798 bzero(_KEYBUF(sav->key_enc), _KEYLEN(sav->key_enc));
2799 KFREE(sav->key_enc);
2800 sav->key_enc = NULL;
2803 bzero(sav->sched, sav->schedlen);
2807 if (sav->replay != NULL) {
2808 keydb_delsecreplay(sav->replay);
2811 if (sav->lft_c != NULL) {
2815 if (sav->lft_h != NULL) {
2819 if (sav->lft_s != NULL) {
2823 if (sav->iv != NULL) {
2837 * others : found, pointer to a SA.
2839 static struct secashead *
2841 struct secasindex *saidx;
2843 struct secashead *sah;
2845 LIST_FOREACH(sah, &sahtree, chain) {
2846 if (sah->state == SADB_SASTATE_DEAD)
2848 if (key_cmpsaidx(&sah->saidx, saidx, CMP_REQID))
2856 * check not to be duplicated SPI.
2857 * NOTE: this function is too slow due to searching all SAD.
2860 * others : found, pointer to a SA.
2862 static struct secasvar *
2863 key_checkspidup(saidx, spi)
2864 struct secasindex *saidx;
2867 struct secashead *sah;
2868 struct secasvar *sav;
2870 /* check address family */
2871 if (saidx->src.ss_family != saidx->dst.ss_family) {
2872 ipseclog((LOG_DEBUG, "key_checkspidup: address family mismatched.\n"));
2877 LIST_FOREACH(sah, &sahtree, chain) {
2878 if (!key_ismyaddr((struct sockaddr *)&sah->saidx.dst))
2880 sav = key_getsavbyspi(sah, spi);
2889 * search SAD litmited alive SA, protocol, SPI.
2892 * others : found, pointer to a SA.
2894 static struct secasvar *
2895 key_getsavbyspi(sah, spi)
2896 struct secashead *sah;
2899 struct secasvar *sav;
2900 u_int stateidx, state;
2902 /* search all status */
2904 stateidx < _ARRAYLEN(saorder_state_alive);
2907 state = saorder_state_alive[stateidx];
2908 LIST_FOREACH(sav, &sah->savtree[state], chain) {
2911 if (sav->state != state) {
2912 ipseclog((LOG_DEBUG, "key_getsavbyspi: "
2913 "invalid sav->state (queue: %d SA: %d)\n",
2914 state, sav->state));
2918 if (sav->spi == spi)
2927 * copy SA values from PF_KEY message except *SPI, SEQ, PID, STATE and TYPE*.
2928 * You must update these if need.
2932 * does not modify mbuf. does not free mbuf on error.
2935 key_setsaval(sav, m, mhp)
2936 struct secasvar *sav;
2938 const struct sadb_msghdr *mhp;
2941 const struct esp_algorithm *algo;
2947 if (m == NULL || mhp == NULL || mhp->msg == NULL)
2948 panic("key_setsaval: NULL pointer is passed.\n");
2950 /* initialization */
2952 sav->key_auth = NULL;
2953 sav->key_enc = NULL;
2962 if (mhp->ext[SADB_EXT_SA] != NULL) {
2963 const struct sadb_sa *sa0;
2965 sa0 = (const struct sadb_sa *)mhp->ext[SADB_EXT_SA];
2966 if (mhp->extlen[SADB_EXT_SA] < sizeof(*sa0)) {
2971 sav->alg_auth = sa0->sadb_sa_auth;
2972 sav->alg_enc = sa0->sadb_sa_encrypt;
2973 sav->flags = sa0->sadb_sa_flags;
2976 if ((sa0->sadb_sa_flags & SADB_X_EXT_OLD) == 0) {
2977 sav->replay = keydb_newsecreplay(sa0->sadb_sa_replay);
2978 if (sav->replay == NULL) {
2979 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
2986 /* Authentication keys */
2987 if (mhp->ext[SADB_EXT_KEY_AUTH] != NULL) {
2988 const struct sadb_key *key0;
2991 key0 = (const struct sadb_key *)mhp->ext[SADB_EXT_KEY_AUTH];
2992 len = mhp->extlen[SADB_EXT_KEY_AUTH];
2995 if (len < sizeof(*key0)) {
2999 switch (mhp->msg->sadb_msg_satype) {
3000 case SADB_SATYPE_AH:
3001 case SADB_SATYPE_ESP:
3002 if (len == PFKEY_ALIGN8(sizeof(struct sadb_key)) &&
3003 sav->alg_auth != SADB_X_AALG_NULL)
3006 case SADB_X_SATYPE_IPCOMP:
3012 ipseclog((LOG_DEBUG, "key_setsaval: invalid key_auth values.\n"));
3016 sav->key_auth = (struct sadb_key *)key_newbuf(key0, len);
3017 if (sav->key_auth == NULL) {
3018 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
3024 /* Encryption key */
3025 if (mhp->ext[SADB_EXT_KEY_ENCRYPT] != NULL) {
3026 const struct sadb_key *key0;
3029 key0 = (const struct sadb_key *)mhp->ext[SADB_EXT_KEY_ENCRYPT];
3030 len = mhp->extlen[SADB_EXT_KEY_ENCRYPT];
3033 if (len < sizeof(*key0)) {
3037 switch (mhp->msg->sadb_msg_satype) {
3038 case SADB_SATYPE_ESP:
3039 if (len == PFKEY_ALIGN8(sizeof(struct sadb_key)) &&
3040 sav->alg_enc != SADB_EALG_NULL) {
3044 sav->key_enc = (struct sadb_key *)key_newbuf(key0, len);
3045 if (sav->key_enc == NULL) {
3046 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
3051 case SADB_X_SATYPE_IPCOMP:
3052 if (len != PFKEY_ALIGN8(sizeof(struct sadb_key)))
3054 sav->key_enc = NULL; /*just in case*/
3056 case SADB_SATYPE_AH:
3062 ipseclog((LOG_DEBUG, "key_setsatval: invalid key_enc value.\n"));
3070 switch (mhp->msg->sadb_msg_satype) {
3071 case SADB_SATYPE_ESP:
3073 algo = esp_algorithm_lookup(sav->alg_enc);
3074 if (algo && algo->ivlen)
3075 sav->ivlen = (*algo->ivlen)(algo, sav);
3076 if (sav->ivlen == 0)
3078 KMALLOC(sav->iv, caddr_t, sav->ivlen);
3080 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
3086 key_randomfill(sav->iv, sav->ivlen);
3089 case SADB_SATYPE_AH:
3090 case SADB_X_SATYPE_IPCOMP:
3093 ipseclog((LOG_DEBUG, "key_setsaval: invalid SA type.\n"));
3100 sav->created = tv.tv_sec;
3102 /* make lifetime for CURRENT */
3103 KMALLOC(sav->lft_c, struct sadb_lifetime *,
3104 sizeof(struct sadb_lifetime));
3105 if (sav->lft_c == NULL) {
3106 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
3113 sav->lft_c->sadb_lifetime_len =
3114 PFKEY_UNIT64(sizeof(struct sadb_lifetime));
3115 sav->lft_c->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
3116 sav->lft_c->sadb_lifetime_allocations = 0;
3117 sav->lft_c->sadb_lifetime_bytes = 0;
3118 sav->lft_c->sadb_lifetime_addtime = tv.tv_sec;
3119 sav->lft_c->sadb_lifetime_usetime = 0;
3121 /* lifetimes for HARD and SOFT */
3123 const struct sadb_lifetime *lft0;
3125 lft0 = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_HARD];
3127 if (mhp->extlen[SADB_EXT_LIFETIME_HARD] < sizeof(*lft0)) {
3131 sav->lft_h = (struct sadb_lifetime *)key_newbuf(lft0,
3133 if (sav->lft_h == NULL) {
3134 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
3138 /* to be initialize ? */
3141 lft0 = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_SOFT];
3143 if (mhp->extlen[SADB_EXT_LIFETIME_SOFT] < sizeof(*lft0)) {
3147 sav->lft_s = (struct sadb_lifetime *)key_newbuf(lft0,
3149 if (sav->lft_s == NULL) {
3150 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
3154 /* to be initialize ? */
3161 /* initialization */
3162 if (sav->replay != NULL) {
3163 keydb_delsecreplay(sav->replay);
3166 if (sav->key_auth != NULL) {
3167 KFREE(sav->key_auth);
3168 sav->key_auth = NULL;
3170 if (sav->key_enc != NULL) {
3171 KFREE(sav->key_enc);
3172 sav->key_enc = NULL;
3178 if (sav->iv != NULL) {
3182 if (sav->lft_c != NULL) {
3186 if (sav->lft_h != NULL) {
3190 if (sav->lft_s != NULL) {
3199 * validation with a secasvar entry, and set SADB_SATYPE_MATURE.
3205 struct secasvar *sav;
3208 int checkmask = 0; /* 2^0: ealg 2^1: aalg 2^2: calg */
3209 int mustmask = 0; /* 2^0: ealg 2^1: aalg 2^2: calg */
3213 /* check SPI value */
3214 switch (sav->sah->saidx.proto) {
3217 if (ntohl(sav->spi) >= 0 && ntohl(sav->spi) <= 255) {
3218 ipseclog((LOG_DEBUG,
3219 "key_mature: illegal range of SPI %u.\n",
3220 (u_int32_t)ntohl(sav->spi)));
3227 switch (sav->sah->saidx.proto) {
3230 if ((sav->flags & SADB_X_EXT_OLD)
3231 && (sav->flags & SADB_X_EXT_DERIV)) {
3232 ipseclog((LOG_DEBUG, "key_mature: "
3233 "invalid flag (derived) given to old-esp.\n"));
3236 if (sav->alg_auth == SADB_AALG_NONE)
3244 if (sav->flags & SADB_X_EXT_DERIV) {
3245 ipseclog((LOG_DEBUG, "key_mature: "
3246 "invalid flag (derived) given to AH SA.\n"));
3249 if (sav->alg_enc != SADB_EALG_NONE) {
3250 ipseclog((LOG_DEBUG, "key_mature: "
3251 "protocol and algorithm mismated.\n"));
3257 case IPPROTO_IPCOMP:
3258 if (sav->alg_auth != SADB_AALG_NONE) {
3259 ipseclog((LOG_DEBUG, "key_mature: "
3260 "protocol and algorithm mismated.\n"));
3263 if ((sav->flags & SADB_X_EXT_RAWCPI) == 0
3264 && ntohl(sav->spi) >= 0x10000) {
3265 ipseclog((LOG_DEBUG, "key_mature: invalid cpi for IPComp.\n"));
3272 ipseclog((LOG_DEBUG, "key_mature: Invalid satype.\n"));
3273 return EPROTONOSUPPORT;
3276 /* check authentication algorithm */
3277 if ((checkmask & 2) != 0) {
3278 const struct ah_algorithm *algo;
3281 algo = ah_algorithm_lookup(sav->alg_auth);
3283 ipseclog((LOG_DEBUG,"key_mature: "
3284 "unknown authentication algorithm.\n"));
3288 /* algorithm-dependent check */
3290 keylen = sav->key_auth->sadb_key_bits;
3293 if (keylen < algo->keymin || algo->keymax < keylen) {
3294 ipseclog((LOG_DEBUG,
3295 "key_mature: invalid AH key length %d "
3296 "(%d-%d allowed)\n",
3297 keylen, algo->keymin, algo->keymax));
3302 if ((*algo->mature)(sav)) {
3303 /* message generated in per-algorithm function*/
3306 mature = SADB_SATYPE_AH;
3309 if ((mustmask & 2) != 0 && mature != SADB_SATYPE_AH) {
3310 ipseclog((LOG_DEBUG, "key_mature: no satisfy algorithm for AH\n"));
3315 /* check encryption algorithm */
3316 if ((checkmask & 1) != 0) {
3318 const struct esp_algorithm *algo;
3321 algo = esp_algorithm_lookup(sav->alg_enc);
3323 ipseclog((LOG_DEBUG, "key_mature: unknown encryption algorithm.\n"));
3327 /* algorithm-dependent check */
3329 keylen = sav->key_enc->sadb_key_bits;
3332 if (keylen < algo->keymin || algo->keymax < keylen) {
3333 ipseclog((LOG_DEBUG,
3334 "key_mature: invalid ESP key length %d "
3335 "(%d-%d allowed)\n",
3336 keylen, algo->keymin, algo->keymax));
3341 if ((*algo->mature)(sav)) {
3342 /* message generated in per-algorithm function*/
3345 mature = SADB_SATYPE_ESP;
3348 if ((mustmask & 1) != 0 && mature != SADB_SATYPE_ESP) {
3349 ipseclog((LOG_DEBUG, "key_mature: no satisfy algorithm for ESP\n"));
3353 ipseclog((LOG_DEBUG, "key_mature: ESP not supported in this configuration\n"));
3358 /* check compression algorithm */
3359 if ((checkmask & 4) != 0) {
3360 const struct ipcomp_algorithm *algo;
3362 /* algorithm-dependent check */
3363 algo = ipcomp_algorithm_lookup(sav->alg_enc);
3365 ipseclog((LOG_DEBUG, "key_mature: unknown compression algorithm.\n"));
3370 key_sa_chgstate(sav, SADB_SASTATE_MATURE);
3376 * subroutine for SADB_GET and SADB_DUMP.
3378 static struct mbuf *
3379 key_setdumpsa(sav, type, satype, seq, pid)
3380 struct secasvar *sav;
3381 u_int8_t type, satype;
3384 struct mbuf *result = NULL, *tres = NULL, *m;
3389 SADB_EXT_SA, SADB_X_EXT_SA2,
3390 SADB_EXT_LIFETIME_HARD, SADB_EXT_LIFETIME_SOFT,
3391 SADB_EXT_LIFETIME_CURRENT, SADB_EXT_ADDRESS_SRC,
3392 SADB_EXT_ADDRESS_DST, SADB_EXT_ADDRESS_PROXY, SADB_EXT_KEY_AUTH,
3393 SADB_EXT_KEY_ENCRYPT, SADB_EXT_IDENTITY_SRC,
3394 SADB_EXT_IDENTITY_DST, SADB_EXT_SENSITIVITY,
3397 m = key_setsadbmsg(type, 0, satype, seq, pid, sav->refcnt);
3402 for (i = sizeof(dumporder)/sizeof(dumporder[0]) - 1; i >= 0; i--) {
3405 switch (dumporder[i]) {
3407 m = key_setsadbsa(sav);
3412 case SADB_X_EXT_SA2:
3413 m = key_setsadbxsa2(sav->sah->saidx.mode,
3414 sav->replay ? sav->replay->count : 0,
3415 sav->sah->saidx.reqid);
3420 case SADB_EXT_ADDRESS_SRC:
3421 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
3422 (struct sockaddr *)&sav->sah->saidx.src,
3423 FULLMASK, IPSEC_ULPROTO_ANY);
3428 case SADB_EXT_ADDRESS_DST:
3429 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
3430 (struct sockaddr *)&sav->sah->saidx.dst,
3431 FULLMASK, IPSEC_ULPROTO_ANY);
3436 case SADB_EXT_KEY_AUTH:
3439 l = PFKEY_UNUNIT64(sav->key_auth->sadb_key_len);
3443 case SADB_EXT_KEY_ENCRYPT:
3446 l = PFKEY_UNUNIT64(sav->key_enc->sadb_key_len);
3450 case SADB_EXT_LIFETIME_CURRENT:
3453 l = PFKEY_UNUNIT64(((struct sadb_ext *)sav->lft_c)->sadb_ext_len);
3457 case SADB_EXT_LIFETIME_HARD:
3460 l = PFKEY_UNUNIT64(((struct sadb_ext *)sav->lft_h)->sadb_ext_len);
3464 case SADB_EXT_LIFETIME_SOFT:
3467 l = PFKEY_UNUNIT64(((struct sadb_ext *)sav->lft_s)->sadb_ext_len);
3471 case SADB_EXT_ADDRESS_PROXY:
3472 case SADB_EXT_IDENTITY_SRC:
3473 case SADB_EXT_IDENTITY_DST:
3474 /* XXX: should we brought from SPD ? */
3475 case SADB_EXT_SENSITIVITY:
3480 if ((!m && !p) || (m && p))
3483 M_PREPEND(tres, l, MB_DONTWAIT);
3486 bcopy(p, mtod(tres, caddr_t), l);
3490 m = key_alloc_mbuf(l);
3493 m_copyback(m, 0, l, p);
3501 m_cat(result, tres);
3503 if (result->m_len < sizeof(struct sadb_msg)) {
3504 result = m_pullup(result, sizeof(struct sadb_msg));
3509 result->m_pkthdr.len = 0;
3510 for (m = result; m; m = m->m_next)
3511 result->m_pkthdr.len += m->m_len;
3513 mtod(result, struct sadb_msg *)->sadb_msg_len =
3514 PFKEY_UNIT64(result->m_pkthdr.len);
3525 * set data into sadb_msg.
3527 static struct mbuf *
3528 key_setsadbmsg(type, tlen, satype, seq, pid, reserved)
3529 u_int8_t type, satype;
3539 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
3542 MGETHDR(m, MB_DONTWAIT, MT_DATA);
3543 if (m && len > MHLEN) {
3544 MCLGET(m, MB_DONTWAIT);
3545 if ((m->m_flags & M_EXT) == 0) {
3552 m->m_pkthdr.len = m->m_len = len;
3555 p = mtod(m, struct sadb_msg *);
3558 p->sadb_msg_version = PF_KEY_V2;
3559 p->sadb_msg_type = type;
3560 p->sadb_msg_errno = 0;
3561 p->sadb_msg_satype = satype;
3562 p->sadb_msg_len = PFKEY_UNIT64(tlen);
3563 p->sadb_msg_reserved = reserved;
3564 p->sadb_msg_seq = seq;
3565 p->sadb_msg_pid = (u_int32_t)pid;
3571 * copy secasvar data into sadb_address.
3573 static struct mbuf *
3575 struct secasvar *sav;
3581 len = PFKEY_ALIGN8(sizeof(struct sadb_sa));
3582 m = key_alloc_mbuf(len);
3583 if (!m || m->m_next) { /*XXX*/
3589 p = mtod(m, struct sadb_sa *);
3592 p->sadb_sa_len = PFKEY_UNIT64(len);
3593 p->sadb_sa_exttype = SADB_EXT_SA;
3594 p->sadb_sa_spi = sav->spi;
3595 p->sadb_sa_replay = (sav->replay != NULL ? sav->replay->wsize : 0);
3596 p->sadb_sa_state = sav->state;
3597 p->sadb_sa_auth = sav->alg_auth;
3598 p->sadb_sa_encrypt = sav->alg_enc;
3599 p->sadb_sa_flags = sav->flags;
3605 * set data into sadb_address.
3607 static struct mbuf *
3608 key_setsadbaddr(exttype, saddr, prefixlen, ul_proto)
3610 struct sockaddr *saddr;
3615 struct sadb_address *p;
3618 len = PFKEY_ALIGN8(sizeof(struct sadb_address)) +
3619 PFKEY_ALIGN8(saddr->sa_len);
3620 m = key_alloc_mbuf(len);
3621 if (!m || m->m_next) { /*XXX*/
3627 p = mtod(m, struct sadb_address *);
3630 p->sadb_address_len = PFKEY_UNIT64(len);
3631 p->sadb_address_exttype = exttype;
3632 p->sadb_address_proto = ul_proto;
3633 if (prefixlen == FULLMASK) {
3634 switch (saddr->sa_family) {
3636 prefixlen = sizeof(struct in_addr) << 3;
3639 prefixlen = sizeof(struct in6_addr) << 3;
3645 p->sadb_address_prefixlen = prefixlen;
3646 p->sadb_address_reserved = 0;
3649 mtod(m, caddr_t) + PFKEY_ALIGN8(sizeof(struct sadb_address)),
3657 * set data into sadb_ident.
3659 static struct mbuf *
3660 key_setsadbident(exttype, idtype, string, stringlen, id)
3661 u_int16_t exttype, idtype;
3667 struct sadb_ident *p;
3670 len = PFKEY_ALIGN8(sizeof(struct sadb_ident)) + PFKEY_ALIGN8(stringlen);
3671 m = key_alloc_mbuf(len);
3672 if (!m || m->m_next) { /*XXX*/
3678 p = mtod(m, struct sadb_ident *);
3681 p->sadb_ident_len = PFKEY_UNIT64(len);
3682 p->sadb_ident_exttype = exttype;
3683 p->sadb_ident_type = idtype;
3684 p->sadb_ident_reserved = 0;
3685 p->sadb_ident_id = id;
3688 mtod(m, caddr_t) + PFKEY_ALIGN8(sizeof(struct sadb_ident)),
3696 * set data into sadb_x_sa2.
3698 static struct mbuf *
3699 key_setsadbxsa2(mode, seq, reqid)
3701 u_int32_t seq, reqid;
3704 struct sadb_x_sa2 *p;
3707 len = PFKEY_ALIGN8(sizeof(struct sadb_x_sa2));
3708 m = key_alloc_mbuf(len);
3709 if (!m || m->m_next) { /*XXX*/
3715 p = mtod(m, struct sadb_x_sa2 *);
3718 p->sadb_x_sa2_len = PFKEY_UNIT64(len);
3719 p->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
3720 p->sadb_x_sa2_mode = mode;
3721 p->sadb_x_sa2_reserved1 = 0;
3722 p->sadb_x_sa2_reserved2 = 0;
3723 p->sadb_x_sa2_sequence = seq;
3724 p->sadb_x_sa2_reqid = reqid;
3730 * set data into sadb_x_policy
3732 static struct mbuf *
3733 key_setsadbxpolicy(type, dir, id)
3739 struct sadb_x_policy *p;
3742 len = PFKEY_ALIGN8(sizeof(struct sadb_x_policy));
3743 m = key_alloc_mbuf(len);
3744 if (!m || m->m_next) { /*XXX*/
3750 p = mtod(m, struct sadb_x_policy *);
3753 p->sadb_x_policy_len = PFKEY_UNIT64(len);
3754 p->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3755 p->sadb_x_policy_type = type;
3756 p->sadb_x_policy_dir = dir;
3757 p->sadb_x_policy_id = id;
3764 * copy a buffer into the new buffer allocated.
3767 key_newbuf(src, len)
3773 KMALLOC(new, caddr_t, len);
3775 ipseclog((LOG_DEBUG, "key_newbuf: No more memory.\n"));
3778 bcopy(src, new, len);
3783 /* compare my own address
3784 * OUT: 1: true, i.e. my address.
3789 struct sockaddr *sa;
3792 struct sockaddr_in *sin;
3793 struct in_ifaddr *ia;
3798 panic("key_ismyaddr: NULL pointer is passed.\n");
3800 switch (sa->sa_family) {
3803 sin = (struct sockaddr_in *)sa;
3804 for (ia = in_ifaddrhead.tqh_first; ia;
3805 ia = ia->ia_link.tqe_next)
3807 if (sin->sin_family == ia->ia_addr.sin_family &&
3808 sin->sin_len == ia->ia_addr.sin_len &&
3809 sin->sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr)
3818 return key_ismyaddr6((struct sockaddr_in6 *)sa);
3827 * compare my own address for IPv6.
3830 * NOTE: derived ip6_input() in KAME. This is necessary to modify more.
3832 #include <netinet6/in6_var.h>
3836 struct sockaddr_in6 *sin6;
3838 struct in6_ifaddr *ia;
3839 struct in6_multi *in6m;
3841 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
3842 if (key_sockaddrcmp((struct sockaddr *)&sin6,
3843 (struct sockaddr *)&ia->ia_addr, 0) == 0)
3848 * XXX why do we care about multlicast here while we don't care
3849 * about IPv4 multicast??
3853 IN6_LOOKUP_MULTI(sin6->sin6_addr, ia->ia_ifp, in6m);
3858 /* loopback, just for safety */
3859 if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
3867 * compare two secasindex structure.
3868 * flag can specify to compare 2 saidxes.
3869 * compare two secasindex structure without both mode and reqid.
3870 * don't compare port.
3872 * saidx0: source, it can be in SAD.
3879 key_cmpsaidx(saidx0, saidx1, flag)
3880 struct secasindex *saidx0, *saidx1;
3884 if (saidx0 == NULL && saidx1 == NULL)
3887 if (saidx0 == NULL || saidx1 == NULL)
3890 if (saidx0->proto != saidx1->proto)
3893 if (flag == CMP_EXACTLY) {
3894 if (saidx0->mode != saidx1->mode)
3896 if (saidx0->reqid != saidx1->reqid)
3898 if (bcmp(&saidx0->src, &saidx1->src, saidx0->src.ss_len) != 0 ||
3899 bcmp(&saidx0->dst, &saidx1->dst, saidx0->dst.ss_len) != 0)
3903 /* CMP_MODE_REQID, CMP_REQID, CMP_HEAD */
3904 if (flag == CMP_MODE_REQID
3905 ||flag == CMP_REQID) {
3907 * If reqid of SPD is non-zero, unique SA is required.
3908 * The result must be of same reqid in this case.
3910 if (saidx1->reqid != 0 && saidx0->reqid != saidx1->reqid)
3914 if (flag == CMP_MODE_REQID) {
3915 if (saidx0->mode != IPSEC_MODE_ANY
3916 && saidx0->mode != saidx1->mode)
3920 if (key_sockaddrcmp((struct sockaddr *)&saidx0->src,
3921 (struct sockaddr *)&saidx1->src, 0) != 0) {
3924 if (key_sockaddrcmp((struct sockaddr *)&saidx0->dst,
3925 (struct sockaddr *)&saidx1->dst, 0) != 0) {
3934 * compare two secindex structure exactly.
3936 * spidx0: source, it is often in SPD.
3937 * spidx1: object, it is often from PFKEY message.
3943 key_cmpspidx_exactly(spidx0, spidx1)
3944 struct secpolicyindex *spidx0, *spidx1;
3947 if (spidx0 == NULL && spidx1 == NULL)
3950 if (spidx0 == NULL || spidx1 == NULL)
3953 if (spidx0->prefs != spidx1->prefs
3954 || spidx0->prefd != spidx1->prefd
3955 || spidx0->ul_proto != spidx1->ul_proto)
3958 if (key_sockaddrcmp((struct sockaddr *)&spidx0->src,
3959 (struct sockaddr *)&spidx1->src, 1) != 0) {
3962 if (key_sockaddrcmp((struct sockaddr *)&spidx0->dst,
3963 (struct sockaddr *)&spidx1->dst, 1) != 0) {
3971 * compare two secindex structure with mask.
3973 * spidx0: source, it is often in SPD.
3974 * spidx1: object, it is often from IP header.
3980 key_cmpspidx_withmask(spidx0, spidx1)
3981 struct secpolicyindex *spidx0, *spidx1;
3984 if (spidx0 == NULL && spidx1 == NULL)
3987 if (spidx0 == NULL || spidx1 == NULL)
3990 if (spidx0->src.ss_family != spidx1->src.ss_family ||
3991 spidx0->dst.ss_family != spidx1->dst.ss_family ||
3992 spidx0->src.ss_len != spidx1->src.ss_len ||
3993 spidx0->dst.ss_len != spidx1->dst.ss_len)
3996 /* if spidx.ul_proto == IPSEC_ULPROTO_ANY, ignore. */
3997 if (spidx0->ul_proto != (u_int16_t)IPSEC_ULPROTO_ANY
3998 && spidx0->ul_proto != spidx1->ul_proto)
4001 switch (spidx0->src.ss_family) {
4003 if (satosin(&spidx0->src)->sin_port != IPSEC_PORT_ANY
4004 && satosin(&spidx0->src)->sin_port !=
4005 satosin(&spidx1->src)->sin_port)
4007 if (!key_bbcmp((caddr_t)&satosin(&spidx0->src)->sin_addr,
4008 (caddr_t)&satosin(&spidx1->src)->sin_addr, spidx0->prefs))
4012 if (satosin6(&spidx0->src)->sin6_port != IPSEC_PORT_ANY
4013 && satosin6(&spidx0->src)->sin6_port !=
4014 satosin6(&spidx1->src)->sin6_port)
4017 * scope_id check. if sin6_scope_id is 0, we regard it
4018 * as a wildcard scope, which matches any scope zone ID.
4020 if (satosin6(&spidx0->src)->sin6_scope_id &&
4021 satosin6(&spidx1->src)->sin6_scope_id &&
4022 satosin6(&spidx0->src)->sin6_scope_id !=
4023 satosin6(&spidx1->src)->sin6_scope_id)
4025 if (!key_bbcmp((caddr_t)&satosin6(&spidx0->src)->sin6_addr,
4026 (caddr_t)&satosin6(&spidx1->src)->sin6_addr, spidx0->prefs))
4031 if (bcmp(&spidx0->src, &spidx1->src, spidx0->src.ss_len) != 0)
4036 switch (spidx0->dst.ss_family) {
4038 if (satosin(&spidx0->dst)->sin_port != IPSEC_PORT_ANY
4039 && satosin(&spidx0->dst)->sin_port !=
4040 satosin(&spidx1->dst)->sin_port)
4042 if (!key_bbcmp((caddr_t)&satosin(&spidx0->dst)->sin_addr,
4043 (caddr_t)&satosin(&spidx1->dst)->sin_addr, spidx0->prefd))
4047 if (satosin6(&spidx0->dst)->sin6_port != IPSEC_PORT_ANY
4048 && satosin6(&spidx0->dst)->sin6_port !=
4049 satosin6(&spidx1->dst)->sin6_port)
4052 * scope_id check. if sin6_scope_id is 0, we regard it
4053 * as a wildcard scope, which matches any scope zone ID.
4055 if (satosin6(&spidx0->src)->sin6_scope_id &&
4056 satosin6(&spidx1->src)->sin6_scope_id &&
4057 satosin6(&spidx0->dst)->sin6_scope_id !=
4058 satosin6(&spidx1->dst)->sin6_scope_id)
4060 if (!key_bbcmp((caddr_t)&satosin6(&spidx0->dst)->sin6_addr,
4061 (caddr_t)&satosin6(&spidx1->dst)->sin6_addr, spidx0->prefd))
4066 if (bcmp(&spidx0->dst, &spidx1->dst, spidx0->dst.ss_len) != 0)
4071 /* XXX Do we check other field ? e.g. flowinfo */
4076 /* returns 0 on match */
4078 key_sockaddrcmp(sa1, sa2, port)
4079 struct sockaddr *sa1;
4080 struct sockaddr *sa2;
4083 if (sa1->sa_family != sa2->sa_family || sa1->sa_len != sa2->sa_len)
4086 switch (sa1->sa_family) {
4088 if (sa1->sa_len != sizeof(struct sockaddr_in))
4090 if (satosin(sa1)->sin_addr.s_addr !=
4091 satosin(sa2)->sin_addr.s_addr) {
4094 if (port && satosin(sa1)->sin_port != satosin(sa2)->sin_port)
4098 if (sa1->sa_len != sizeof(struct sockaddr_in6))
4099 return 1; /*EINVAL*/
4100 if (satosin6(sa1)->sin6_scope_id !=
4101 satosin6(sa2)->sin6_scope_id) {
4104 if (!IN6_ARE_ADDR_EQUAL(&satosin6(sa1)->sin6_addr,
4105 &satosin6(sa2)->sin6_addr)) {
4109 satosin6(sa1)->sin6_port != satosin6(sa2)->sin6_port) {
4113 if (bcmp(sa1, sa2, sa1->sa_len) != 0)
4122 * compare two buffers with mask.
4126 * bits: Number of bits to compare
4132 key_bbcmp(p1, p2, bits)
4138 /* XXX: This could be considerably faster if we compare a word
4139 * at a time, but it is complicated on LSB Endian machines */
4141 /* Handle null pointers */
4142 if (p1 == NULL || p2 == NULL)
4152 mask = ~((1<<(8-bits))-1);
4153 if ((*p1 & mask) != (*p2 & mask))
4156 return 1; /* Match! */
4161 * scanning SPD and SAD to check status for each entries,
4162 * and do to remove or to expire.
4163 * XXX: year 2038 problem may remain.
4166 key_timehandler(void)
4174 s = splnet(); /*called from softclock()*/
4178 struct secpolicy *sp, *nextsp;
4180 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
4181 for (sp = LIST_FIRST(&sptree[dir]);
4185 nextsp = LIST_NEXT(sp, chain);
4187 if (sp->state == IPSEC_SPSTATE_DEAD) {
4192 if (sp->lifetime == 0 && sp->validtime == 0)
4195 /* the deletion will occur next time */
4197 && tv.tv_sec - sp->created > sp->lifetime)
4199 && tv.tv_sec - sp->lastused > sp->validtime)) {
4200 sp->state = IPSEC_SPSTATE_DEAD;
4210 struct secashead *sah, *nextsah;
4211 struct secasvar *sav, *nextsav;
4213 for (sah = LIST_FIRST(&sahtree);
4217 nextsah = LIST_NEXT(sah, chain);
4219 /* if sah has been dead, then delete it and process next sah. */
4220 if (sah->state == SADB_SASTATE_DEAD) {
4225 /* if LARVAL entry doesn't become MATURE, delete it. */
4226 for (sav = LIST_FIRST(&sah->savtree[SADB_SASTATE_LARVAL]);
4230 nextsav = LIST_NEXT(sav, chain);
4232 if (tv.tv_sec - sav->created > key_larval_lifetime) {
4238 * check MATURE entry to start to send expire message
4241 for (sav = LIST_FIRST(&sah->savtree[SADB_SASTATE_MATURE]);
4245 nextsav = LIST_NEXT(sav, chain);
4247 /* we don't need to check. */
4248 if (sav->lft_s == NULL)
4252 if (sav->lft_c == NULL) {
4253 ipseclog((LOG_DEBUG,"key_timehandler: "
4254 "There is no CURRENT time, why?\n"));
4258 /* check SOFT lifetime */
4259 if (sav->lft_s->sadb_lifetime_addtime != 0
4260 && tv.tv_sec - sav->created > sav->lft_s->sadb_lifetime_addtime) {
4262 * check the SA if it has been used.
4263 * when it hasn't been used, delete it.
4264 * i don't think such SA will be used.
4266 if (sav->lft_c->sadb_lifetime_usetime == 0) {
4267 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4271 key_sa_chgstate(sav, SADB_SASTATE_DYING);
4273 * XXX If we keep to send expire
4274 * message in the status of
4275 * DYING. Do remove below code.
4281 /* check SOFT lifetime by bytes */
4283 * XXX I don't know the way to delete this SA
4284 * when new SA is installed. Caution when it's
4285 * installed too big lifetime by time.
4287 else if (sav->lft_s->sadb_lifetime_bytes != 0
4288 && sav->lft_s->sadb_lifetime_bytes < sav->lft_c->sadb_lifetime_bytes) {
4290 key_sa_chgstate(sav, SADB_SASTATE_DYING);
4292 * XXX If we keep to send expire
4293 * message in the status of
4294 * DYING. Do remove below code.
4300 /* check DYING entry to change status to DEAD. */
4301 for (sav = LIST_FIRST(&sah->savtree[SADB_SASTATE_DYING]);
4305 nextsav = LIST_NEXT(sav, chain);
4307 /* we don't need to check. */
4308 if (sav->lft_h == NULL)
4312 if (sav->lft_c == NULL) {
4313 ipseclog((LOG_DEBUG, "key_timehandler: "
4314 "There is no CURRENT time, why?\n"));
4318 if (sav->lft_h->sadb_lifetime_addtime != 0
4319 && tv.tv_sec - sav->created > sav->lft_h->sadb_lifetime_addtime) {
4320 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4324 #if 0 /* XXX Should we keep to send expire message until HARD lifetime ? */
4325 else if (sav->lft_s != NULL
4326 && sav->lft_s->sadb_lifetime_addtime != 0
4327 && tv.tv_sec - sav->created > sav->lft_s->sadb_lifetime_addtime) {
4329 * XXX: should be checked to be
4330 * installed the valid SA.
4334 * If there is no SA then sending
4340 /* check HARD lifetime by bytes */
4341 else if (sav->lft_h->sadb_lifetime_bytes != 0
4342 && sav->lft_h->sadb_lifetime_bytes < sav->lft_c->sadb_lifetime_bytes) {
4343 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4349 /* delete entry in DEAD */
4350 for (sav = LIST_FIRST(&sah->savtree[SADB_SASTATE_DEAD]);
4354 nextsav = LIST_NEXT(sav, chain);
4357 if (sav->state != SADB_SASTATE_DEAD) {
4358 ipseclog((LOG_DEBUG, "key_timehandler: "
4359 "invalid sav->state "
4360 "(queue: %d SA: %d): "
4362 SADB_SASTATE_DEAD, sav->state));
4366 * do not call key_freesav() here.
4367 * sav should already be freed, and sav->refcnt
4368 * shows other references to sav
4369 * (such as from SPD).
4375 #ifndef IPSEC_NONBLOCK_ACQUIRE
4378 struct secacq *acq, *nextacq;
4380 for (acq = LIST_FIRST(&acqtree);
4384 nextacq = LIST_NEXT(acq, chain);
4386 if (tv.tv_sec - acq->created > key_blockacq_lifetime
4387 && __LIST_CHAINED(acq)) {
4388 LIST_REMOVE(acq, chain);
4397 struct secspacq *acq, *nextacq;
4399 for (acq = LIST_FIRST(&spacqtree);
4403 nextacq = LIST_NEXT(acq, chain);
4405 if (tv.tv_sec - acq->created > key_blockacq_lifetime
4406 && __LIST_CHAINED(acq)) {
4407 LIST_REMOVE(acq, chain);
4413 /* initialize random seed */
4414 if (key_tick_init_random++ > key_int_random) {
4415 key_tick_init_random = 0;
4419 #ifndef IPSEC_DEBUG2
4420 /* do exchange to tick time !! */
4421 (void)timeout((void *)key_timehandler, (void *)0, hz);
4422 #endif /* IPSEC_DEBUG2 */
4429 * to initialize a seed for random()
4438 srandom(tv.tv_usec);
4448 key_randomfill(&value, sizeof(value));
4453 key_randomfill(p, l)
4459 static int warn = 1;
4462 n = (size_t)read_random(p, (u_int)l);
4466 bcopy(&v, (u_int8_t *)p + n,
4467 l - n < sizeof(v) ? l - n : sizeof(v));
4471 printf("WARNING: pseudo-random number generator "
4472 "used for IPsec processing\n");
4479 * map SADB_SATYPE_* to IPPROTO_*.
4480 * if satype == SADB_SATYPE then satype is mapped to ~0.
4482 * 0: invalid satype.
4485 key_satype2proto(satype)
4489 case SADB_SATYPE_UNSPEC:
4490 return IPSEC_PROTO_ANY;
4491 case SADB_SATYPE_AH:
4493 case SADB_SATYPE_ESP:
4495 case SADB_X_SATYPE_IPCOMP:
4496 return IPPROTO_IPCOMP;
4505 * map IPPROTO_* to SADB_SATYPE_*
4507 * 0: invalid protocol type.
4510 key_proto2satype(proto)
4515 return SADB_SATYPE_AH;
4517 return SADB_SATYPE_ESP;
4518 case IPPROTO_IPCOMP:
4519 return SADB_X_SATYPE_IPCOMP;
4529 * SADB_GETSPI processing is to receive
4530 * <base, (SA2), src address, dst address, (SPI range)>
4531 * from the IKMPd, to assign a unique spi value, to hang on the INBOUND
4532 * tree with the status of LARVAL, and send
4533 * <base, SA(*), address(SD)>
4536 * IN: mhp: pointer to the pointer to each header.
4537 * OUT: NULL if fail.
4538 * other if success, return pointer to the message to send.
4541 key_getspi(so, m, mhp)
4544 const struct sadb_msghdr *mhp;
4546 struct sadb_address *src0, *dst0;
4547 struct secasindex saidx;
4548 struct secashead *newsah;
4549 struct secasvar *newsav;
4557 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
4558 panic("key_getspi: NULL pointer is passed.\n");
4560 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
4561 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
4562 ipseclog((LOG_DEBUG, "key_getspi: invalid message is passed.\n"));
4563 return key_senderror(so, m, EINVAL);
4565 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
4566 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
4567 ipseclog((LOG_DEBUG, "key_getspi: invalid message is passed.\n"));
4568 return key_senderror(so, m, EINVAL);
4570 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
4571 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
4572 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
4574 mode = IPSEC_MODE_ANY;
4578 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
4579 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
4581 /* map satype to proto */
4582 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
4583 ipseclog((LOG_DEBUG, "key_getspi: invalid satype is passed.\n"));
4584 return key_senderror(so, m, EINVAL);
4587 /* make sure if port number is zero. */
4588 switch (((struct sockaddr *)(src0 + 1))->sa_family) {
4590 if (((struct sockaddr *)(src0 + 1))->sa_len !=
4591 sizeof(struct sockaddr_in))
4592 return key_senderror(so, m, EINVAL);
4593 ((struct sockaddr_in *)(src0 + 1))->sin_port = 0;
4596 if (((struct sockaddr *)(src0 + 1))->sa_len !=
4597 sizeof(struct sockaddr_in6))
4598 return key_senderror(so, m, EINVAL);
4599 ((struct sockaddr_in6 *)(src0 + 1))->sin6_port = 0;
4604 switch (((struct sockaddr *)(dst0 + 1))->sa_family) {
4606 if (((struct sockaddr *)(dst0 + 1))->sa_len !=
4607 sizeof(struct sockaddr_in))
4608 return key_senderror(so, m, EINVAL);
4609 ((struct sockaddr_in *)(dst0 + 1))->sin_port = 0;
4612 if (((struct sockaddr *)(dst0 + 1))->sa_len !=
4613 sizeof(struct sockaddr_in6))
4614 return key_senderror(so, m, EINVAL);
4615 ((struct sockaddr_in6 *)(dst0 + 1))->sin6_port = 0;
4621 /* XXX boundary check against sa_len */
4622 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
4624 /* SPI allocation */
4625 spi = key_do_getnewspi((struct sadb_spirange *)mhp->ext[SADB_EXT_SPIRANGE],
4628 return key_senderror(so, m, EINVAL);
4630 /* get a SA index */
4631 if ((newsah = key_getsah(&saidx)) == NULL) {
4632 /* create a new SA index */
4633 if ((newsah = key_newsah(&saidx)) == NULL) {
4634 ipseclog((LOG_DEBUG, "key_getspi: No more memory.\n"));
4635 return key_senderror(so, m, ENOBUFS);
4641 newsav = key_newsav(m, mhp, newsah, &error);
4642 if (newsav == NULL) {
4643 /* XXX don't free new SA index allocated in above. */
4644 return key_senderror(so, m, error);
4648 newsav->spi = htonl(spi);
4650 #ifndef IPSEC_NONBLOCK_ACQUIRE
4651 /* delete the entry in acqtree */
4652 if (mhp->msg->sadb_msg_seq != 0) {
4654 if ((acq = key_getacqbyseq(mhp->msg->sadb_msg_seq)) != NULL) {
4655 /* reset counter in order to deletion by timehandler. */
4658 acq->created = tv.tv_sec;
4665 struct mbuf *n, *nn;
4666 struct sadb_sa *m_sa;
4667 struct sadb_msg *newmsg;
4670 /* create new sadb_msg to reply. */
4671 len = PFKEY_ALIGN8(sizeof(struct sadb_msg)) +
4672 PFKEY_ALIGN8(sizeof(struct sadb_sa));
4674 return key_senderror(so, m, ENOBUFS);
4676 MGETHDR(n, MB_DONTWAIT, MT_DATA);
4678 MCLGET(n, MB_DONTWAIT);
4679 if ((n->m_flags & M_EXT) == 0) {
4685 return key_senderror(so, m, ENOBUFS);
4691 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
4692 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
4694 m_sa = (struct sadb_sa *)(mtod(n, caddr_t) + off);
4695 m_sa->sadb_sa_len = PFKEY_UNIT64(sizeof(struct sadb_sa));
4696 m_sa->sadb_sa_exttype = SADB_EXT_SA;
4697 m_sa->sadb_sa_spi = htonl(spi);
4698 off += PFKEY_ALIGN8(sizeof(struct sadb_sa));
4702 panic("length inconsistency in key_getspi");
4705 n->m_next = key_gather_mbuf(m, mhp, 0, 2, SADB_EXT_ADDRESS_SRC,
4706 SADB_EXT_ADDRESS_DST);
4709 return key_senderror(so, m, ENOBUFS);
4712 if (n->m_len < sizeof(struct sadb_msg)) {
4713 n = m_pullup(n, sizeof(struct sadb_msg));
4715 return key_sendup_mbuf(so, m, KEY_SENDUP_ONE);
4718 n->m_pkthdr.len = 0;
4719 for (nn = n; nn; nn = nn->m_next)
4720 n->m_pkthdr.len += nn->m_len;
4722 newmsg = mtod(n, struct sadb_msg *);
4723 newmsg->sadb_msg_seq = newsav->seq;
4724 newmsg->sadb_msg_errno = 0;
4725 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
4728 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
4733 * allocating new SPI
4734 * called by key_getspi().
4740 key_do_getnewspi(spirange, saidx)
4741 struct sadb_spirange *spirange;
4742 struct secasindex *saidx;
4746 int count = key_spi_trycnt;
4748 /* set spi range to allocate */
4749 if (spirange != NULL) {
4750 min = spirange->sadb_spirange_min;
4751 max = spirange->sadb_spirange_max;
4753 min = key_spi_minval;
4754 max = key_spi_maxval;
4756 /* IPCOMP needs 2-byte SPI */
4757 if (saidx->proto == IPPROTO_IPCOMP) {
4764 t = min; min = max; max = t;
4769 if (key_checkspidup(saidx, min) != NULL) {
4770 ipseclog((LOG_DEBUG, "key_do_getnewspi: SPI %u exists already.\n", min));
4774 count--; /* taking one cost. */
4782 /* when requesting to allocate spi ranged */
4784 /* generate pseudo-random SPI value ranged. */
4785 newspi = min + (key_random() % (max - min + 1));
4787 if (key_checkspidup(saidx, newspi) == NULL)
4791 if (count == 0 || newspi == 0) {
4792 ipseclog((LOG_DEBUG, "key_do_getnewspi: to allocate spi is failed.\n"));
4798 keystat.getspi_count =
4799 (keystat.getspi_count + key_spi_trycnt - count) / 2;
4805 * SADB_UPDATE processing
4807 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4808 * key(AE), (identity(SD),) (sensitivity)>
4809 * from the ikmpd, and update a secasvar entry whose status is SADB_SASTATE_LARVAL.
4811 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4812 * (identity(SD),) (sensitivity)>
4815 * m will always be freed.
4818 key_update(so, m, mhp)
4821 const struct sadb_msghdr *mhp;
4823 struct sadb_sa *sa0;
4824 struct sadb_address *src0, *dst0;
4825 struct secasindex saidx;
4826 struct secashead *sah;
4827 struct secasvar *sav;
4834 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
4835 panic("key_update: NULL pointer is passed.\n");
4837 /* map satype to proto */
4838 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
4839 ipseclog((LOG_DEBUG, "key_update: invalid satype is passed.\n"));
4840 return key_senderror(so, m, EINVAL);
4843 if (mhp->ext[SADB_EXT_SA] == NULL ||
4844 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
4845 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
4846 (mhp->msg->sadb_msg_satype == SADB_SATYPE_ESP &&
4847 mhp->ext[SADB_EXT_KEY_ENCRYPT] == NULL) ||
4848 (mhp->msg->sadb_msg_satype == SADB_SATYPE_AH &&
4849 mhp->ext[SADB_EXT_KEY_AUTH] == NULL) ||
4850 (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL &&
4851 mhp->ext[SADB_EXT_LIFETIME_SOFT] == NULL) ||
4852 (mhp->ext[SADB_EXT_LIFETIME_HARD] == NULL &&
4853 mhp->ext[SADB_EXT_LIFETIME_SOFT] != NULL)) {
4854 ipseclog((LOG_DEBUG, "key_update: invalid message is passed.\n"));
4855 return key_senderror(so, m, EINVAL);
4857 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
4858 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
4859 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
4860 ipseclog((LOG_DEBUG, "key_update: invalid message is passed.\n"));
4861 return key_senderror(so, m, EINVAL);
4863 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
4864 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
4865 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
4867 mode = IPSEC_MODE_ANY;
4870 /* XXX boundary checking for other extensions */
4872 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
4873 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
4874 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
4876 /* XXX boundary check against sa_len */
4877 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
4879 /* get a SA header */
4880 if ((sah = key_getsah(&saidx)) == NULL) {
4881 ipseclog((LOG_DEBUG, "key_update: no SA index found.\n"));
4882 return key_senderror(so, m, ENOENT);
4885 /* set spidx if there */
4887 error = key_setident(sah, m, mhp);
4889 return key_senderror(so, m, error);
4891 /* find a SA with sequence number. */
4892 #ifdef IPSEC_DOSEQCHECK
4893 if (mhp->msg->sadb_msg_seq != 0
4894 && (sav = key_getsavbyseq(sah, mhp->msg->sadb_msg_seq)) == NULL) {
4895 ipseclog((LOG_DEBUG,
4896 "key_update: no larval SA with sequence %u exists.\n",
4897 mhp->msg->sadb_msg_seq));
4898 return key_senderror(so, m, ENOENT);
4901 if ((sav = key_getsavbyspi(sah, sa0->sadb_sa_spi)) == NULL) {
4902 ipseclog((LOG_DEBUG,
4903 "key_update: no such a SA found (spi:%u)\n",
4904 (u_int32_t)ntohl(sa0->sadb_sa_spi)));
4905 return key_senderror(so, m, EINVAL);
4909 /* validity check */
4910 if (sav->sah->saidx.proto != proto) {
4911 ipseclog((LOG_DEBUG,
4912 "key_update: protocol mismatched (DB=%u param=%u)\n",
4913 sav->sah->saidx.proto, proto));
4914 return key_senderror(so, m, EINVAL);
4916 #ifdef IPSEC_DOSEQCHECK
4917 if (sav->spi != sa0->sadb_sa_spi) {
4918 ipseclog((LOG_DEBUG,
4919 "key_update: SPI mismatched (DB:%u param:%u)\n",
4920 (u_int32_t)ntohl(sav->spi),
4921 (u_int32_t)ntohl(sa0->sadb_sa_spi)));
4922 return key_senderror(so, m, EINVAL);
4925 if (sav->pid != mhp->msg->sadb_msg_pid) {
4926 ipseclog((LOG_DEBUG,
4927 "key_update: pid mismatched (DB:%u param:%u)\n",
4928 sav->pid, mhp->msg->sadb_msg_pid));
4929 return key_senderror(so, m, EINVAL);
4932 /* copy sav values */
4933 error = key_setsaval(sav, m, mhp);
4936 return key_senderror(so, m, error);
4939 /* check SA values to be mature. */
4940 if ((mhp->msg->sadb_msg_errno = key_mature(sav)) != 0) {
4942 return key_senderror(so, m, 0);
4948 /* set msg buf from mhp */
4949 n = key_getmsgbuf_x1(m, mhp);
4951 ipseclog((LOG_DEBUG, "key_update: No more memory.\n"));
4952 return key_senderror(so, m, ENOBUFS);
4956 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
4961 * search SAD with sequence for a SA which state is SADB_SASTATE_LARVAL.
4962 * only called by key_update().
4965 * others : found, pointer to a SA.
4967 #ifdef IPSEC_DOSEQCHECK
4968 static struct secasvar *
4969 key_getsavbyseq(sah, seq)
4970 struct secashead *sah;
4973 struct secasvar *sav;
4976 state = SADB_SASTATE_LARVAL;
4978 /* search SAD with sequence number ? */
4979 LIST_FOREACH(sav, &sah->savtree[state], chain) {
4981 KEY_CHKSASTATE(state, sav->state, "key_getsabyseq");
4983 if (sav->seq == seq) {
4985 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
4986 printf("DP key_getsavbyseq cause "
4987 "refcnt++:%d SA:%p\n",
4998 * SADB_ADD processing
4999 * add a entry to SA database, when received
5000 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
5001 * key(AE), (identity(SD),) (sensitivity)>
5004 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
5005 * (identity(SD),) (sensitivity)>
5008 * IGNORE identity and sensitivity messages.
5010 * m will always be freed.
5016 const struct sadb_msghdr *mhp;
5018 struct sadb_sa *sa0;
5019 struct sadb_address *src0, *dst0;
5020 struct secasindex saidx;
5021 struct secashead *newsah;
5022 struct secasvar *newsav;
5029 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
5030 panic("key_add: NULL pointer is passed.\n");
5032 /* map satype to proto */
5033 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5034 ipseclog((LOG_DEBUG, "key_add: invalid satype is passed.\n"));
5035 return key_senderror(so, m, EINVAL);
5038 if (mhp->ext[SADB_EXT_SA] == NULL ||
5039 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5040 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
5041 (mhp->msg->sadb_msg_satype == SADB_SATYPE_ESP &&
5042 mhp->ext[SADB_EXT_KEY_ENCRYPT] == NULL) ||
5043 (mhp->msg->sadb_msg_satype == SADB_SATYPE_AH &&
5044 mhp->ext[SADB_EXT_KEY_AUTH] == NULL) ||
5045 (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL &&
5046 mhp->ext[SADB_EXT_LIFETIME_SOFT] == NULL) ||
5047 (mhp->ext[SADB_EXT_LIFETIME_HARD] == NULL &&
5048 mhp->ext[SADB_EXT_LIFETIME_SOFT] != NULL)) {
5049 ipseclog((LOG_DEBUG, "key_add: invalid message is passed.\n"));
5050 return key_senderror(so, m, EINVAL);
5052 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
5053 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5054 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5056 ipseclog((LOG_DEBUG, "key_add: invalid message is passed.\n"));
5057 return key_senderror(so, m, EINVAL);
5059 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
5060 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
5061 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
5063 mode = IPSEC_MODE_ANY;
5067 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5068 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
5069 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
5071 /* XXX boundary check against sa_len */
5072 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
5074 /* get a SA header */
5075 if ((newsah = key_getsah(&saidx)) == NULL) {
5076 /* create a new SA header */
5077 if ((newsah = key_newsah(&saidx)) == NULL) {
5078 ipseclog((LOG_DEBUG, "key_add: No more memory.\n"));
5079 return key_senderror(so, m, ENOBUFS);
5083 /* set spidx if there */
5085 error = key_setident(newsah, m, mhp);
5087 return key_senderror(so, m, error);
5090 /* create new SA entry. */
5091 /* We can create new SA only if SPI is differenct. */
5092 if (key_getsavbyspi(newsah, sa0->sadb_sa_spi)) {
5093 ipseclog((LOG_DEBUG, "key_add: SA already exists.\n"));
5094 return key_senderror(so, m, EEXIST);
5096 newsav = key_newsav(m, mhp, newsah, &error);
5097 if (newsav == NULL) {
5098 return key_senderror(so, m, error);
5101 /* check SA values to be mature. */
5102 if ((error = key_mature(newsav)) != 0) {
5103 key_freesav(newsav);
5104 return key_senderror(so, m, error);
5108 * don't call key_freesav() here, as we would like to keep the SA
5109 * in the database on success.
5115 /* set msg buf from mhp */
5116 n = key_getmsgbuf_x1(m, mhp);
5118 ipseclog((LOG_DEBUG, "key_update: No more memory.\n"));
5119 return key_senderror(so, m, ENOBUFS);
5123 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5129 key_setident(sah, m, mhp)
5130 struct secashead *sah;
5132 const struct sadb_msghdr *mhp;
5134 const struct sadb_ident *idsrc, *iddst;
5135 int idsrclen, iddstlen;
5138 if (sah == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
5139 panic("key_setident: NULL pointer is passed.\n");
5141 /* don't make buffer if not there */
5142 if (mhp->ext[SADB_EXT_IDENTITY_SRC] == NULL &&
5143 mhp->ext[SADB_EXT_IDENTITY_DST] == NULL) {
5149 if (mhp->ext[SADB_EXT_IDENTITY_SRC] == NULL ||
5150 mhp->ext[SADB_EXT_IDENTITY_DST] == NULL) {
5151 ipseclog((LOG_DEBUG, "key_setident: invalid identity.\n"));
5155 idsrc = (const struct sadb_ident *)mhp->ext[SADB_EXT_IDENTITY_SRC];
5156 iddst = (const struct sadb_ident *)mhp->ext[SADB_EXT_IDENTITY_DST];
5157 idsrclen = mhp->extlen[SADB_EXT_IDENTITY_SRC];
5158 iddstlen = mhp->extlen[SADB_EXT_IDENTITY_DST];
5160 /* validity check */
5161 if (idsrc->sadb_ident_type != iddst->sadb_ident_type) {
5162 ipseclog((LOG_DEBUG, "key_setident: ident type mismatch.\n"));
5166 switch (idsrc->sadb_ident_type) {
5167 case SADB_IDENTTYPE_PREFIX:
5168 case SADB_IDENTTYPE_FQDN:
5169 case SADB_IDENTTYPE_USERFQDN:
5171 /* XXX do nothing */
5177 /* make structure */
5178 KMALLOC(sah->idents, struct sadb_ident *, idsrclen);
5179 if (sah->idents == NULL) {
5180 ipseclog((LOG_DEBUG, "key_setident: No more memory.\n"));
5183 KMALLOC(sah->identd, struct sadb_ident *, iddstlen);
5184 if (sah->identd == NULL) {
5187 ipseclog((LOG_DEBUG, "key_setident: No more memory.\n"));
5190 bcopy(idsrc, sah->idents, idsrclen);
5191 bcopy(iddst, sah->identd, iddstlen);
5197 * m will not be freed on return.
5198 * it is caller's responsibility to free the result.
5200 static struct mbuf *
5201 key_getmsgbuf_x1(m, mhp)
5203 const struct sadb_msghdr *mhp;
5208 if (m == NULL || mhp == NULL || mhp->msg == NULL)
5209 panic("key_getmsgbuf_x1: NULL pointer is passed.\n");
5211 /* create new sadb_msg to reply. */
5212 n = key_gather_mbuf(m, mhp, 1, 9, SADB_EXT_RESERVED,
5213 SADB_EXT_SA, SADB_X_EXT_SA2,
5214 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST,
5215 SADB_EXT_LIFETIME_HARD, SADB_EXT_LIFETIME_SOFT,
5216 SADB_EXT_IDENTITY_SRC, SADB_EXT_IDENTITY_DST);
5220 if (n->m_len < sizeof(struct sadb_msg)) {
5221 n = m_pullup(n, sizeof(struct sadb_msg));
5225 mtod(n, struct sadb_msg *)->sadb_msg_errno = 0;
5226 mtod(n, struct sadb_msg *)->sadb_msg_len =
5227 PFKEY_UNIT64(n->m_pkthdr.len);
5232 static int key_delete_all (struct socket *, struct mbuf *,
5233 const struct sadb_msghdr *, u_int16_t);
5236 * SADB_DELETE processing
5238 * <base, SA(*), address(SD)>
5239 * from the ikmpd, and set SADB_SASTATE_DEAD,
5241 * <base, SA(*), address(SD)>
5244 * m will always be freed.
5247 key_delete(so, m, mhp)
5250 const struct sadb_msghdr *mhp;
5252 struct sadb_sa *sa0;
5253 struct sadb_address *src0, *dst0;
5254 struct secasindex saidx;
5255 struct secashead *sah;
5256 struct secasvar *sav = NULL;
5260 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
5261 panic("key_delete: NULL pointer is passed.\n");
5263 /* map satype to proto */
5264 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5265 ipseclog((LOG_DEBUG, "key_delete: invalid satype is passed.\n"));
5266 return key_senderror(so, m, EINVAL);
5269 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5270 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
5271 ipseclog((LOG_DEBUG, "key_delete: invalid message is passed.\n"));
5272 return key_senderror(so, m, EINVAL);
5275 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5276 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5277 ipseclog((LOG_DEBUG, "key_delete: invalid message is passed.\n"));
5278 return key_senderror(so, m, EINVAL);
5281 if (mhp->ext[SADB_EXT_SA] == NULL) {
5283 * Caller wants us to delete all non-LARVAL SAs
5284 * that match the src/dst. This is used during
5285 * IKE INITIAL-CONTACT.
5287 ipseclog((LOG_DEBUG, "key_delete: doing delete all.\n"));
5288 return key_delete_all(so, m, mhp, proto);
5289 } else if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa)) {
5290 ipseclog((LOG_DEBUG, "key_delete: invalid message is passed.\n"));
5291 return key_senderror(so, m, EINVAL);
5294 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5295 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
5296 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
5298 /* XXX boundary check against sa_len */
5299 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5301 /* get a SA header */
5302 LIST_FOREACH(sah, &sahtree, chain) {
5303 if (sah->state == SADB_SASTATE_DEAD)
5305 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5308 /* get a SA with SPI. */
5309 sav = key_getsavbyspi(sah, sa0->sadb_sa_spi);
5314 ipseclog((LOG_DEBUG, "key_delete: no SA found.\n"));
5315 return key_senderror(so, m, ENOENT);
5318 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
5324 struct sadb_msg *newmsg;
5326 /* create new sadb_msg to reply. */
5327 n = key_gather_mbuf(m, mhp, 1, 4, SADB_EXT_RESERVED,
5328 SADB_EXT_SA, SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
5330 return key_senderror(so, m, ENOBUFS);
5332 if (n->m_len < sizeof(struct sadb_msg)) {
5333 n = m_pullup(n, sizeof(struct sadb_msg));
5335 return key_senderror(so, m, ENOBUFS);
5337 newmsg = mtod(n, struct sadb_msg *);
5338 newmsg->sadb_msg_errno = 0;
5339 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
5342 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5347 * delete all SAs for src/dst. Called from key_delete().
5350 key_delete_all(so, m, mhp, proto)
5353 const struct sadb_msghdr *mhp;
5356 struct sadb_address *src0, *dst0;
5357 struct secasindex saidx;
5358 struct secashead *sah;
5359 struct secasvar *sav, *nextsav;
5360 u_int stateidx, state;
5362 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
5363 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
5365 /* XXX boundary check against sa_len */
5366 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5368 LIST_FOREACH(sah, &sahtree, chain) {
5369 if (sah->state == SADB_SASTATE_DEAD)
5371 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5374 /* Delete all non-LARVAL SAs. */
5376 stateidx < _ARRAYLEN(saorder_state_alive);
5378 state = saorder_state_alive[stateidx];
5379 if (state == SADB_SASTATE_LARVAL)
5381 for (sav = LIST_FIRST(&sah->savtree[state]);
5382 sav != NULL; sav = nextsav) {
5383 nextsav = LIST_NEXT(sav, chain);
5385 if (sav->state != state) {
5386 ipseclog((LOG_DEBUG, "key_delete_all: "
5387 "invalid sav->state "
5388 "(queue: %d SA: %d)\n",
5389 state, sav->state));
5393 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
5400 struct sadb_msg *newmsg;
5402 /* create new sadb_msg to reply. */
5403 n = key_gather_mbuf(m, mhp, 1, 3, SADB_EXT_RESERVED,
5404 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
5406 return key_senderror(so, m, ENOBUFS);
5408 if (n->m_len < sizeof(struct sadb_msg)) {
5409 n = m_pullup(n, sizeof(struct sadb_msg));
5411 return key_senderror(so, m, ENOBUFS);
5413 newmsg = mtod(n, struct sadb_msg *);
5414 newmsg->sadb_msg_errno = 0;
5415 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
5418 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5423 * SADB_GET processing
5425 * <base, SA(*), address(SD)>
5426 * from the ikmpd, and get a SP and a SA to respond,
5428 * <base, SA, (lifetime(HSC),) address(SD), (address(P),) key(AE),
5429 * (identity(SD),) (sensitivity)>
5432 * m will always be freed.
5438 const struct sadb_msghdr *mhp;
5440 struct sadb_sa *sa0;
5441 struct sadb_address *src0, *dst0;
5442 struct secasindex saidx;
5443 struct secashead *sah;
5444 struct secasvar *sav = NULL;
5448 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
5449 panic("key_get: NULL pointer is passed.\n");
5451 /* map satype to proto */
5452 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5453 ipseclog((LOG_DEBUG, "key_get: invalid satype is passed.\n"));
5454 return key_senderror(so, m, EINVAL);
5457 if (mhp->ext[SADB_EXT_SA] == NULL ||
5458 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5459 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
5460 ipseclog((LOG_DEBUG, "key_get: invalid message is passed.\n"));
5461 return key_senderror(so, m, EINVAL);
5463 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
5464 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5465 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5466 ipseclog((LOG_DEBUG, "key_get: invalid message is passed.\n"));
5467 return key_senderror(so, m, EINVAL);
5470 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5471 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
5472 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
5474 /* XXX boundary check against sa_len */
5475 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5477 /* get a SA header */
5478 LIST_FOREACH(sah, &sahtree, chain) {
5479 if (sah->state == SADB_SASTATE_DEAD)
5481 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5484 /* get a SA with SPI. */
5485 sav = key_getsavbyspi(sah, sa0->sadb_sa_spi);
5490 ipseclog((LOG_DEBUG, "key_get: no SA found.\n"));
5491 return key_senderror(so, m, ENOENT);
5498 /* map proto to satype */
5499 if ((satype = key_proto2satype(sah->saidx.proto)) == 0) {
5500 ipseclog((LOG_DEBUG, "key_get: there was invalid proto in SAD.\n"));
5501 return key_senderror(so, m, EINVAL);
5504 /* create new sadb_msg to reply. */
5505 n = key_setdumpsa(sav, SADB_GET, satype, mhp->msg->sadb_msg_seq,
5506 mhp->msg->sadb_msg_pid);
5508 return key_senderror(so, m, ENOBUFS);
5511 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
5515 /* XXX make it sysctl-configurable? */
5517 key_getcomb_setlifetime(comb)
5518 struct sadb_comb *comb;
5521 comb->sadb_comb_soft_allocations = 1;
5522 comb->sadb_comb_hard_allocations = 1;
5523 comb->sadb_comb_soft_bytes = 0;
5524 comb->sadb_comb_hard_bytes = 0;
5525 comb->sadb_comb_hard_addtime = 86400; /* 1 day */
5526 comb->sadb_comb_soft_addtime = comb->sadb_comb_soft_addtime * 80 / 100;
5527 comb->sadb_comb_soft_usetime = 28800; /* 8 hours */
5528 comb->sadb_comb_hard_usetime = comb->sadb_comb_hard_usetime * 80 / 100;
5533 * XXX reorder combinations by preference
5534 * XXX no idea if the user wants ESP authentication or not
5536 static struct mbuf *
5539 struct sadb_comb *comb;
5540 const struct esp_algorithm *algo;
5541 struct mbuf *result = NULL, *m, *n;
5545 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
5548 for (i = 1; i <= SADB_EALG_MAX; i++) {
5549 algo = esp_algorithm_lookup(i);
5553 if (algo->keymax < ipsec_esp_keymin)
5555 if (algo->keymin < ipsec_esp_keymin)
5556 encmin = ipsec_esp_keymin;
5558 encmin = algo->keymin;
5561 m = key_getcomb_ah();
5565 panic("assumption failed in key_getcomb_esp");
5567 MGET(m, MB_DONTWAIT, MT_DATA);
5572 bzero(mtod(m, caddr_t), m->m_len);
5579 for (n = m; n; n = n->m_next)
5583 panic("assumption failed in key_getcomb_esp");
5586 for (off = 0; off < totlen; off += l) {
5587 n = m_pulldown(m, off, l, &o);
5589 /* m is already freed */
5592 comb = (struct sadb_comb *)(mtod(n, caddr_t) + o);
5593 bzero(comb, sizeof(*comb));
5594 key_getcomb_setlifetime(comb);
5595 comb->sadb_comb_encrypt = i;
5596 comb->sadb_comb_encrypt_minbits = encmin;
5597 comb->sadb_comb_encrypt_maxbits = algo->keymax;
5616 * XXX reorder combinations by preference
5618 static struct mbuf *
5621 struct sadb_comb *comb;
5622 const struct ah_algorithm *algo;
5626 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
5629 for (i = 1; i <= SADB_AALG_MAX; i++) {
5631 /* we prefer HMAC algorithms, not old algorithms */
5632 if (i != SADB_AALG_SHA1HMAC && i != SADB_AALG_MD5HMAC)
5635 algo = ah_algorithm_lookup(i);
5639 if (algo->keymax < ipsec_ah_keymin)
5641 if (algo->keymin < ipsec_ah_keymin)
5642 min = ipsec_ah_keymin;
5649 panic("assumption failed in key_getcomb_ah");
5651 MGET(m, MB_DONTWAIT, MT_DATA);
5658 M_PREPEND(m, l, MB_DONTWAIT);
5662 comb = mtod(m, struct sadb_comb *);
5663 bzero(comb, sizeof(*comb));
5664 key_getcomb_setlifetime(comb);
5665 comb->sadb_comb_auth = i;
5666 comb->sadb_comb_auth_minbits = min;
5667 comb->sadb_comb_auth_maxbits = algo->keymax;
5674 * not really an official behavior. discussed in pf_key@inner.net in Sep2000.
5675 * XXX reorder combinations by preference
5677 static struct mbuf *
5678 key_getcomb_ipcomp()
5680 struct sadb_comb *comb;
5681 const struct ipcomp_algorithm *algo;
5684 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
5687 for (i = 1; i <= SADB_X_CALG_MAX; i++) {
5688 algo = ipcomp_algorithm_lookup(i);
5695 panic("assumption failed in key_getcomb_ipcomp");
5697 MGET(m, MB_DONTWAIT, MT_DATA);
5704 M_PREPEND(m, l, MB_DONTWAIT);
5708 comb = mtod(m, struct sadb_comb *);
5709 bzero(comb, sizeof(*comb));
5710 key_getcomb_setlifetime(comb);
5711 comb->sadb_comb_encrypt = i;
5712 /* what should we set into sadb_comb_*_{min,max}bits? */
5719 * XXX no way to pass mode (transport/tunnel) to userland
5720 * XXX replay checking?
5721 * XXX sysctl interface to ipsec_{ah,esp}_keymin
5723 static struct mbuf *
5725 const struct secasindex *saidx;
5727 struct sadb_prop *prop;
5729 const int l = PFKEY_ALIGN8(sizeof(struct sadb_prop));
5732 switch (saidx->proto) {
5735 m = key_getcomb_esp();
5739 m = key_getcomb_ah();
5741 case IPPROTO_IPCOMP:
5742 m = key_getcomb_ipcomp();
5750 M_PREPEND(m, l, MB_DONTWAIT);
5755 for (n = m; n; n = n->m_next)
5758 prop = mtod(m, struct sadb_prop *);
5759 bzero(prop, sizeof(*prop));
5760 prop->sadb_prop_len = PFKEY_UNIT64(totlen);
5761 prop->sadb_prop_exttype = SADB_EXT_PROPOSAL;
5762 prop->sadb_prop_replay = 32; /* XXX */
5768 * SADB_ACQUIRE processing called by key_checkrequest() and key_acquire2().
5770 * <base, SA, address(SD), (address(P)), x_policy,
5771 * (identity(SD),) (sensitivity,) proposal>
5772 * to KMD, and expect to receive
5773 * <base> with SADB_ACQUIRE if error occured,
5775 * <base, src address, dst address, (SPI range)> with SADB_GETSPI
5776 * from KMD by PF_KEY.
5778 * XXX x_policy is outside of RFC2367 (KAME extension).
5779 * XXX sensitivity is not supported.
5780 * XXX for ipcomp, RFC2367 does not define how to fill in proposal.
5781 * see comment for key_getcomb_ipcomp().
5785 * others: error number
5788 key_acquire(saidx, sp)
5789 struct secasindex *saidx;
5790 struct secpolicy *sp;
5792 struct mbuf *result = NULL, *m;
5793 #ifndef IPSEC_NONBLOCK_ACQUIRE
5794 struct secacq *newacq;
5802 panic("key_acquire: NULL pointer is passed.\n");
5803 if ((satype = key_proto2satype(saidx->proto)) == 0)
5804 panic("key_acquire: invalid proto is passed.\n");
5806 #ifndef IPSEC_NONBLOCK_ACQUIRE
5808 * We never do anything about acquirng SA. There is anather
5809 * solution that kernel blocks to send SADB_ACQUIRE message until
5810 * getting something message from IKEd. In later case, to be
5811 * managed with ACQUIRING list.
5813 /* get a entry to check whether sending message or not. */
5814 if ((newacq = key_getacq(saidx)) != NULL) {
5815 if (key_blockacq_count < newacq->count) {
5816 /* reset counter and do send message. */
5819 /* increment counter and do nothing. */
5824 /* make new entry for blocking to send SADB_ACQUIRE. */
5825 if ((newacq = key_newacq(saidx)) == NULL)
5828 /* add to acqtree */
5829 LIST_INSERT_HEAD(&acqtree, newacq, chain);
5834 #ifndef IPSEC_NONBLOCK_ACQUIRE
5837 seq = (acq_seq = (acq_seq == ~0 ? 1 : ++acq_seq));
5839 m = key_setsadbmsg(SADB_ACQUIRE, 0, satype, seq, 0, 0);
5846 /* set sadb_address for saidx's. */
5847 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
5848 (struct sockaddr *)&saidx->src, FULLMASK, IPSEC_ULPROTO_ANY);
5855 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
5856 (struct sockaddr *)&saidx->dst, FULLMASK, IPSEC_ULPROTO_ANY);
5863 /* XXX proxy address (optional) */
5865 /* set sadb_x_policy */
5867 m = key_setsadbxpolicy(sp->policy, sp->spidx.dir, sp->id);
5875 /* XXX identity (optional) */
5877 if (idexttype && fqdn) {
5878 /* create identity extension (FQDN) */
5879 struct sadb_ident *id;
5882 fqdnlen = strlen(fqdn) + 1; /* +1 for terminating-NUL */
5883 id = (struct sadb_ident *)p;
5884 bzero(id, sizeof(*id) + PFKEY_ALIGN8(fqdnlen));
5885 id->sadb_ident_len = PFKEY_UNIT64(sizeof(*id) + PFKEY_ALIGN8(fqdnlen));
5886 id->sadb_ident_exttype = idexttype;
5887 id->sadb_ident_type = SADB_IDENTTYPE_FQDN;
5888 bcopy(fqdn, id + 1, fqdnlen);
5889 p += sizeof(struct sadb_ident) + PFKEY_ALIGN8(fqdnlen);
5893 /* create identity extension (USERFQDN) */
5894 struct sadb_ident *id;
5898 /* +1 for terminating-NUL */
5899 userfqdnlen = strlen(userfqdn) + 1;
5902 id = (struct sadb_ident *)p;
5903 bzero(id, sizeof(*id) + PFKEY_ALIGN8(userfqdnlen));
5904 id->sadb_ident_len = PFKEY_UNIT64(sizeof(*id) + PFKEY_ALIGN8(userfqdnlen));
5905 id->sadb_ident_exttype = idexttype;
5906 id->sadb_ident_type = SADB_IDENTTYPE_USERFQDN;
5907 /* XXX is it correct? */
5908 if (curproc && curproc->p_cred)
5909 id->sadb_ident_id = curproc->p_cred->p_ruid;
5910 if (userfqdn && userfqdnlen)
5911 bcopy(userfqdn, id + 1, userfqdnlen);
5912 p += sizeof(struct sadb_ident) + PFKEY_ALIGN8(userfqdnlen);
5916 /* XXX sensitivity (optional) */
5918 /* create proposal/combination extension */
5919 m = key_getprop(saidx);
5922 * spec conformant: always attach proposal/combination extension,
5923 * the problem is that we have no way to attach it for ipcomp,
5924 * due to the way sadb_comb is declared in RFC2367.
5933 * outside of spec; make proposal/combination extension optional.
5939 if ((result->m_flags & M_PKTHDR) == 0) {
5944 if (result->m_len < sizeof(struct sadb_msg)) {
5945 result = m_pullup(result, sizeof(struct sadb_msg));
5946 if (result == NULL) {
5952 result->m_pkthdr.len = 0;
5953 for (m = result; m; m = m->m_next)
5954 result->m_pkthdr.len += m->m_len;
5956 mtod(result, struct sadb_msg *)->sadb_msg_len =
5957 PFKEY_UNIT64(result->m_pkthdr.len);
5959 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
5967 #ifndef IPSEC_NONBLOCK_ACQUIRE
5968 static struct secacq *
5970 struct secasindex *saidx;
5972 struct secacq *newacq;
5976 KMALLOC(newacq, struct secacq *, sizeof(struct secacq));
5977 if (newacq == NULL) {
5978 ipseclog((LOG_DEBUG, "key_newacq: No more memory.\n"));
5981 bzero(newacq, sizeof(*newacq));
5984 bcopy(saidx, &newacq->saidx, sizeof(newacq->saidx));
5985 newacq->seq = (acq_seq == ~0 ? 1 : ++acq_seq);
5987 newacq->created = tv.tv_sec;
5993 static struct secacq *
5995 struct secasindex *saidx;
5999 LIST_FOREACH(acq, &acqtree, chain) {
6000 if (key_cmpsaidx(saidx, &acq->saidx, CMP_EXACTLY))
6007 static struct secacq *
6008 key_getacqbyseq(seq)
6013 LIST_FOREACH(acq, &acqtree, chain) {
6014 if (acq->seq == seq)
6022 static struct secspacq *
6024 struct secpolicyindex *spidx;
6026 struct secspacq *acq;
6030 KMALLOC(acq, struct secspacq *, sizeof(struct secspacq));
6032 ipseclog((LOG_DEBUG, "key_newspacq: No more memory.\n"));
6035 bzero(acq, sizeof(*acq));
6038 bcopy(spidx, &acq->spidx, sizeof(acq->spidx));
6040 acq->created = tv.tv_sec;
6046 static struct secspacq *
6048 struct secpolicyindex *spidx;
6050 struct secspacq *acq;
6052 LIST_FOREACH(acq, &spacqtree, chain) {
6053 if (key_cmpspidx_exactly(spidx, &acq->spidx))
6061 * SADB_ACQUIRE processing,
6062 * in first situation, is receiving
6064 * from the ikmpd, and clear sequence of its secasvar entry.
6066 * In second situation, is receiving
6067 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
6068 * from a user land process, and return
6069 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
6072 * m will always be freed.
6075 key_acquire2(so, m, mhp)
6078 const struct sadb_msghdr *mhp;
6080 struct sadb_address *src0, *dst0;
6081 struct secasindex saidx;
6082 struct secashead *sah;
6087 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
6088 panic("key_acquire2: NULL pointer is passed.\n");
6091 * Error message from KMd.
6092 * We assume that if error was occured in IKEd, the length of PFKEY
6093 * message is equal to the size of sadb_msg structure.
6094 * We do not raise error even if error occured in this function.
6096 if (mhp->msg->sadb_msg_len == PFKEY_UNIT64(sizeof(struct sadb_msg))) {
6097 #ifndef IPSEC_NONBLOCK_ACQUIRE
6101 /* check sequence number */
6102 if (mhp->msg->sadb_msg_seq == 0) {
6103 ipseclog((LOG_DEBUG, "key_acquire2: must specify sequence number.\n"));
6108 if ((acq = key_getacqbyseq(mhp->msg->sadb_msg_seq)) == NULL) {
6110 * the specified larval SA is already gone, or we got
6111 * a bogus sequence number. we can silently ignore it.
6117 /* reset acq counter in order to deletion by timehander. */
6119 acq->created = tv.tv_sec;
6127 * This message is from user land.
6130 /* map satype to proto */
6131 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
6132 ipseclog((LOG_DEBUG, "key_acquire2: invalid satype is passed.\n"));
6133 return key_senderror(so, m, EINVAL);
6136 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
6137 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
6138 mhp->ext[SADB_EXT_PROPOSAL] == NULL) {
6140 ipseclog((LOG_DEBUG, "key_acquire2: invalid message is passed.\n"));
6141 return key_senderror(so, m, EINVAL);
6143 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
6144 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
6145 mhp->extlen[SADB_EXT_PROPOSAL] < sizeof(struct sadb_prop)) {
6147 ipseclog((LOG_DEBUG, "key_acquire2: invalid message is passed.\n"));
6148 return key_senderror(so, m, EINVAL);
6151 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
6152 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
6154 /* XXX boundary check against sa_len */
6155 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
6157 /* get a SA index */
6158 LIST_FOREACH(sah, &sahtree, chain) {
6159 if (sah->state == SADB_SASTATE_DEAD)
6161 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_MODE_REQID))
6165 ipseclog((LOG_DEBUG, "key_acquire2: a SA exists already.\n"));
6166 return key_senderror(so, m, EEXIST);
6169 error = key_acquire(&saidx, NULL);
6171 ipseclog((LOG_DEBUG, "key_acquire2: error %d returned "
6172 "from key_acquire.\n", mhp->msg->sadb_msg_errno));
6173 return key_senderror(so, m, error);
6176 return key_sendup_mbuf(so, m, KEY_SENDUP_REGISTERED);
6180 * SADB_REGISTER processing.
6181 * If SATYPE_UNSPEC has been passed as satype, only return sabd_supported.
6184 * from the ikmpd, and register a socket to send PF_KEY messages,
6188 * If socket is detached, must free from regnode.
6190 * m will always be freed.
6193 key_register(so, m, mhp)
6196 const struct sadb_msghdr *mhp;
6198 struct secreg *reg, *newreg = 0;
6201 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
6202 panic("key_register: NULL pointer is passed.\n");
6204 /* check for invalid register message */
6205 if (mhp->msg->sadb_msg_satype >= sizeof(regtree)/sizeof(regtree[0]))
6206 return key_senderror(so, m, EINVAL);
6208 /* When SATYPE_UNSPEC is specified, only return sabd_supported. */
6209 if (mhp->msg->sadb_msg_satype == SADB_SATYPE_UNSPEC)
6212 /* check whether existing or not */
6213 LIST_FOREACH(reg, ®tree[mhp->msg->sadb_msg_satype], chain) {
6214 if (reg->so == so) {
6215 ipseclog((LOG_DEBUG, "key_register: socket exists already.\n"));
6216 return key_senderror(so, m, EEXIST);
6220 /* create regnode */
6221 KMALLOC(newreg, struct secreg *, sizeof(*newreg));
6222 if (newreg == NULL) {
6223 ipseclog((LOG_DEBUG, "key_register: No more memory.\n"));
6224 return key_senderror(so, m, ENOBUFS);
6226 bzero((caddr_t)newreg, sizeof(*newreg));
6229 ((struct keycb *)sotorawcb(so))->kp_registered++;
6231 /* add regnode to regtree. */
6232 LIST_INSERT_HEAD(®tree[mhp->msg->sadb_msg_satype], newreg, chain);
6237 struct sadb_msg *newmsg;
6238 struct sadb_supported *sup;
6239 u_int len, alen, elen;
6242 struct sadb_alg *alg;
6244 /* create new sadb_msg to reply. */
6246 for (i = 1; i <= SADB_AALG_MAX; i++) {
6247 if (ah_algorithm_lookup(i))
6248 alen += sizeof(struct sadb_alg);
6251 alen += sizeof(struct sadb_supported);
6254 for (i = 1; i <= SADB_EALG_MAX; i++) {
6255 if (esp_algorithm_lookup(i))
6256 elen += sizeof(struct sadb_alg);
6259 elen += sizeof(struct sadb_supported);
6262 len = sizeof(struct sadb_msg) + alen + elen;
6265 return key_senderror(so, m, ENOBUFS);
6267 MGETHDR(n, MB_DONTWAIT, MT_DATA);
6269 MCLGET(n, MB_DONTWAIT);
6270 if ((n->m_flags & M_EXT) == 0) {
6276 return key_senderror(so, m, ENOBUFS);
6278 n->m_pkthdr.len = n->m_len = len;
6282 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
6283 newmsg = mtod(n, struct sadb_msg *);
6284 newmsg->sadb_msg_errno = 0;
6285 newmsg->sadb_msg_len = PFKEY_UNIT64(len);
6286 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
6288 /* for authentication algorithm */
6290 sup = (struct sadb_supported *)(mtod(n, caddr_t) + off);
6291 sup->sadb_supported_len = PFKEY_UNIT64(alen);
6292 sup->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
6293 off += PFKEY_ALIGN8(sizeof(*sup));
6295 for (i = 1; i <= SADB_AALG_MAX; i++) {
6296 const struct ah_algorithm *aalgo;
6298 aalgo = ah_algorithm_lookup(i);
6301 alg = (struct sadb_alg *)(mtod(n, caddr_t) + off);
6302 alg->sadb_alg_id = i;
6303 alg->sadb_alg_ivlen = 0;
6304 alg->sadb_alg_minbits = aalgo->keymin;
6305 alg->sadb_alg_maxbits = aalgo->keymax;
6306 off += PFKEY_ALIGN8(sizeof(*alg));
6311 /* for encryption algorithm */
6313 sup = (struct sadb_supported *)(mtod(n, caddr_t) + off);
6314 sup->sadb_supported_len = PFKEY_UNIT64(elen);
6315 sup->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT;
6316 off += PFKEY_ALIGN8(sizeof(*sup));
6318 for (i = 1; i <= SADB_EALG_MAX; i++) {
6319 const struct esp_algorithm *ealgo;
6321 ealgo = esp_algorithm_lookup(i);
6324 alg = (struct sadb_alg *)(mtod(n, caddr_t) + off);
6325 alg->sadb_alg_id = i;
6326 if (ealgo && ealgo->ivlen) {
6328 * give NULL to get the value preferred by
6329 * algorithm XXX SADB_X_EXT_DERIV ?
6331 alg->sadb_alg_ivlen =
6332 (*ealgo->ivlen)(ealgo, NULL);
6334 alg->sadb_alg_ivlen = 0;
6335 alg->sadb_alg_minbits = ealgo->keymin;
6336 alg->sadb_alg_maxbits = ealgo->keymax;
6337 off += PFKEY_ALIGN8(sizeof(struct sadb_alg));
6344 panic("length assumption failed in key_register");
6348 return key_sendup_mbuf(so, n, KEY_SENDUP_REGISTERED);
6353 * free secreg entry registered.
6354 * XXX: I want to do free a socket marked done SADB_RESIGER to socket.
6365 panic("key_freereg: NULL pointer is passed.\n");
6368 * check whether existing or not.
6369 * check all type of SA, because there is a potential that
6370 * one socket is registered to multiple type of SA.
6372 for (i = 0; i <= SADB_SATYPE_MAX; i++) {
6373 LIST_FOREACH(reg, ®tree[i], chain) {
6375 && __LIST_CHAINED(reg)) {
6376 LIST_REMOVE(reg, chain);
6387 * SADB_EXPIRE processing
6389 * <base, SA, SA2, lifetime(C and one of HS), address(SD)>
6391 * NOTE: We send only soft lifetime extension.
6394 * others : error number
6398 struct secasvar *sav;
6402 struct mbuf *result = NULL, *m;
6405 struct sadb_lifetime *lt;
6407 /* XXX: Why do we lock ? */
6408 s = splnet(); /*called from softclock()*/
6412 panic("key_expire: NULL pointer is passed.\n");
6413 if (sav->sah == NULL)
6414 panic("key_expire: Why was SA index in SA NULL.\n");
6415 if ((satype = key_proto2satype(sav->sah->saidx.proto)) == 0)
6416 panic("key_expire: invalid proto is passed.\n");
6418 /* set msg header */
6419 m = key_setsadbmsg(SADB_EXPIRE, 0, satype, sav->seq, 0, sav->refcnt);
6426 /* create SA extension */
6427 m = key_setsadbsa(sav);
6434 /* create SA extension */
6435 m = key_setsadbxsa2(sav->sah->saidx.mode,
6436 sav->replay ? sav->replay->count : 0,
6437 sav->sah->saidx.reqid);
6444 /* create lifetime extension (current and soft) */
6445 len = PFKEY_ALIGN8(sizeof(*lt)) * 2;
6446 m = key_alloc_mbuf(len);
6447 if (!m || m->m_next) { /*XXX*/
6453 bzero(mtod(m, caddr_t), len);
6454 lt = mtod(m, struct sadb_lifetime *);
6455 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
6456 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
6457 lt->sadb_lifetime_allocations = sav->lft_c->sadb_lifetime_allocations;
6458 lt->sadb_lifetime_bytes = sav->lft_c->sadb_lifetime_bytes;
6459 lt->sadb_lifetime_addtime = sav->lft_c->sadb_lifetime_addtime;
6460 lt->sadb_lifetime_usetime = sav->lft_c->sadb_lifetime_usetime;
6461 lt = (struct sadb_lifetime *)(mtod(m, caddr_t) + len / 2);
6462 bcopy(sav->lft_s, lt, sizeof(*lt));
6465 /* set sadb_address for source */
6466 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
6467 (struct sockaddr *)&sav->sah->saidx.src,
6468 FULLMASK, IPSEC_ULPROTO_ANY);
6475 /* set sadb_address for destination */
6476 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
6477 (struct sockaddr *)&sav->sah->saidx.dst,
6478 FULLMASK, IPSEC_ULPROTO_ANY);
6485 if ((result->m_flags & M_PKTHDR) == 0) {
6490 if (result->m_len < sizeof(struct sadb_msg)) {
6491 result = m_pullup(result, sizeof(struct sadb_msg));
6492 if (result == NULL) {
6498 result->m_pkthdr.len = 0;
6499 for (m = result; m; m = m->m_next)
6500 result->m_pkthdr.len += m->m_len;
6502 mtod(result, struct sadb_msg *)->sadb_msg_len =
6503 PFKEY_UNIT64(result->m_pkthdr.len);
6506 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
6516 * SADB_FLUSH processing
6519 * from the ikmpd, and free all entries in secastree.
6523 * NOTE: to do is only marking SADB_SASTATE_DEAD.
6525 * m will always be freed.
6528 key_flush(so, m, mhp)
6531 const struct sadb_msghdr *mhp;
6533 struct sadb_msg *newmsg;
6534 struct secashead *sah, *nextsah;
6535 struct secasvar *sav, *nextsav;
6541 if (so == NULL || mhp == NULL || mhp->msg == NULL)
6542 panic("key_flush: NULL pointer is passed.\n");
6544 /* map satype to proto */
6545 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
6546 ipseclog((LOG_DEBUG, "key_flush: invalid satype is passed.\n"));
6547 return key_senderror(so, m, EINVAL);
6550 /* no SATYPE specified, i.e. flushing all SA. */
6551 for (sah = LIST_FIRST(&sahtree);
6554 nextsah = LIST_NEXT(sah, chain);
6556 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
6557 && proto != sah->saidx.proto)
6561 stateidx < _ARRAYLEN(saorder_state_alive);
6563 state = saorder_state_any[stateidx];
6564 for (sav = LIST_FIRST(&sah->savtree[state]);
6568 nextsav = LIST_NEXT(sav, chain);
6570 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
6575 sah->state = SADB_SASTATE_DEAD;
6578 if (m->m_len < sizeof(struct sadb_msg) ||
6579 sizeof(struct sadb_msg) > m->m_len + M_TRAILINGSPACE(m)) {
6580 ipseclog((LOG_DEBUG, "key_flush: No more memory.\n"));
6581 return key_senderror(so, m, ENOBUFS);
6587 m->m_pkthdr.len = m->m_len = sizeof(struct sadb_msg);
6588 newmsg = mtod(m, struct sadb_msg *);
6589 newmsg->sadb_msg_errno = 0;
6590 newmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
6592 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
6596 * SADB_DUMP processing
6597 * dump all entries including status of DEAD in SAD.
6600 * from the ikmpd, and dump all secasvar leaves
6605 * m will always be freed.
6608 key_dump(so, m, mhp)
6611 const struct sadb_msghdr *mhp;
6613 struct secashead *sah;
6614 struct secasvar *sav;
6620 struct sadb_msg *newmsg;
6624 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
6625 panic("key_dump: NULL pointer is passed.\n");
6627 /* map satype to proto */
6628 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
6629 ipseclog((LOG_DEBUG, "key_dump: invalid satype is passed.\n"));
6630 return key_senderror(so, m, EINVAL);
6633 /* count sav entries to be sent to the userland. */
6635 LIST_FOREACH(sah, &sahtree, chain) {
6636 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
6637 && proto != sah->saidx.proto)
6641 stateidx < _ARRAYLEN(saorder_state_any);
6643 state = saorder_state_any[stateidx];
6644 LIST_FOREACH(sav, &sah->savtree[state], chain) {
6651 return key_senderror(so, m, ENOENT);
6653 /* send this to the userland, one at a time. */
6655 LIST_FOREACH(sah, &sahtree, chain) {
6656 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
6657 && proto != sah->saidx.proto)
6660 /* map proto to satype */
6661 if ((satype = key_proto2satype(sah->saidx.proto)) == 0) {
6662 ipseclog((LOG_DEBUG, "key_dump: there was invalid proto in SAD.\n"));
6663 return key_senderror(so, m, EINVAL);
6667 stateidx < _ARRAYLEN(saorder_state_any);
6669 state = saorder_state_any[stateidx];
6670 LIST_FOREACH(sav, &sah->savtree[state], chain) {
6671 n = key_setdumpsa(sav, SADB_DUMP, satype,
6672 --cnt, mhp->msg->sadb_msg_pid);
6674 return key_senderror(so, m, ENOBUFS);
6676 key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
6686 * SADB_X_PROMISC processing
6688 * m will always be freed.
6691 key_promisc(so, m, mhp)
6694 const struct sadb_msghdr *mhp;
6699 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
6700 panic("key_promisc: NULL pointer is passed.\n");
6702 olen = PFKEY_UNUNIT64(mhp->msg->sadb_msg_len);
6704 if (olen < sizeof(struct sadb_msg)) {
6706 return key_senderror(so, m, EINVAL);
6711 } else if (olen == sizeof(struct sadb_msg)) {
6712 /* enable/disable promisc mode */
6715 if ((kp = (struct keycb *)sotorawcb(so)) == NULL)
6716 return key_senderror(so, m, EINVAL);
6717 mhp->msg->sadb_msg_errno = 0;
6718 switch (mhp->msg->sadb_msg_satype) {
6721 kp->kp_promisc = mhp->msg->sadb_msg_satype;
6724 return key_senderror(so, m, EINVAL);
6727 /* send the original message back to everyone */
6728 mhp->msg->sadb_msg_errno = 0;
6729 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
6731 /* send packet as is */
6733 m_adj(m, PFKEY_ALIGN8(sizeof(struct sadb_msg)));
6735 /* TODO: if sadb_msg_seq is specified, send to specific pid */
6736 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
6740 static int (*key_typesw[]) (struct socket *, struct mbuf *,
6741 const struct sadb_msghdr *) = {
6742 NULL, /* SADB_RESERVED */
6743 key_getspi, /* SADB_GETSPI */
6744 key_update, /* SADB_UPDATE */
6745 key_add, /* SADB_ADD */
6746 key_delete, /* SADB_DELETE */
6747 key_get, /* SADB_GET */
6748 key_acquire2, /* SADB_ACQUIRE */
6749 key_register, /* SADB_REGISTER */
6750 NULL, /* SADB_EXPIRE */
6751 key_flush, /* SADB_FLUSH */
6752 key_dump, /* SADB_DUMP */
6753 key_promisc, /* SADB_X_PROMISC */
6754 NULL, /* SADB_X_PCHANGE */
6755 key_spdadd, /* SADB_X_SPDUPDATE */
6756 key_spdadd, /* SADB_X_SPDADD */
6757 key_spddelete, /* SADB_X_SPDDELETE */
6758 key_spdget, /* SADB_X_SPDGET */
6759 NULL, /* SADB_X_SPDACQUIRE */
6760 key_spddump, /* SADB_X_SPDDUMP */
6761 key_spdflush, /* SADB_X_SPDFLUSH */
6762 key_spdadd, /* SADB_X_SPDSETIDX */
6763 NULL, /* SADB_X_SPDEXPIRE */
6764 key_spddelete2, /* SADB_X_SPDDELETE2 */
6768 * parse sadb_msg buffer to process PFKEYv2,
6769 * and create a data to response if needed.
6770 * I think to be dealed with mbuf directly.
6772 * msgp : pointer to pointer to a received buffer pulluped.
6773 * This is rewrited to response.
6774 * so : pointer to socket.
6776 * length for buffer to send to user process.
6783 struct sadb_msg *msg;
6784 struct sadb_msghdr mh;
6790 if (m == NULL || so == NULL)
6791 panic("key_parse: NULL pointer is passed.\n");
6793 #if 0 /*kdebug_sadb assumes msg in linear buffer*/
6794 KEYDEBUG(KEYDEBUG_KEY_DUMP,
6795 ipseclog((LOG_DEBUG, "key_parse: passed sadb_msg\n"));
6799 if (m->m_len < sizeof(struct sadb_msg)) {
6800 m = m_pullup(m, sizeof(struct sadb_msg));
6804 msg = mtod(m, struct sadb_msg *);
6805 orglen = PFKEY_UNUNIT64(msg->sadb_msg_len);
6806 target = KEY_SENDUP_ONE;
6808 if ((m->m_flags & M_PKTHDR) == 0 ||
6809 m->m_pkthdr.len != m->m_pkthdr.len) {
6810 ipseclog((LOG_DEBUG, "key_parse: invalid message length.\n"));
6811 pfkeystat.out_invlen++;
6816 if (msg->sadb_msg_version != PF_KEY_V2) {
6817 ipseclog((LOG_DEBUG,
6818 "key_parse: PF_KEY version %u is mismatched.\n",
6819 msg->sadb_msg_version));
6820 pfkeystat.out_invver++;
6825 if (msg->sadb_msg_type > SADB_MAX) {
6826 ipseclog((LOG_DEBUG, "key_parse: invalid type %u is passed.\n",
6827 msg->sadb_msg_type));
6828 pfkeystat.out_invmsgtype++;
6833 /* for old-fashioned code - should be nuked */
6834 if (m->m_pkthdr.len > MCLBYTES) {
6841 MGETHDR(n, MB_DONTWAIT, MT_DATA);
6842 if (n && m->m_pkthdr.len > MHLEN) {
6843 MCLGET(n, MB_DONTWAIT);
6844 if ((n->m_flags & M_EXT) == 0) {
6853 m_copydata(m, 0, m->m_pkthdr.len, mtod(n, caddr_t));
6854 n->m_pkthdr.len = n->m_len = m->m_pkthdr.len;
6860 /* align the mbuf chain so that extensions are in contiguous region. */
6861 error = key_align(m, &mh);
6865 if (m->m_next) { /*XXX*/
6873 switch (msg->sadb_msg_satype) {
6874 case SADB_SATYPE_UNSPEC:
6875 switch (msg->sadb_msg_type) {
6883 ipseclog((LOG_DEBUG, "key_parse: must specify satype "
6884 "when msg type=%u.\n", msg->sadb_msg_type));
6885 pfkeystat.out_invsatype++;
6890 case SADB_SATYPE_AH:
6891 case SADB_SATYPE_ESP:
6892 case SADB_X_SATYPE_IPCOMP:
6893 switch (msg->sadb_msg_type) {
6895 case SADB_X_SPDDELETE:
6897 case SADB_X_SPDDUMP:
6898 case SADB_X_SPDFLUSH:
6899 case SADB_X_SPDSETIDX:
6900 case SADB_X_SPDUPDATE:
6901 case SADB_X_SPDDELETE2:
6902 ipseclog((LOG_DEBUG, "key_parse: illegal satype=%u\n",
6903 msg->sadb_msg_type));
6904 pfkeystat.out_invsatype++;
6909 case SADB_SATYPE_RSVP:
6910 case SADB_SATYPE_OSPFV2:
6911 case SADB_SATYPE_RIPV2:
6912 case SADB_SATYPE_MIP:
6913 ipseclog((LOG_DEBUG, "key_parse: type %u isn't supported.\n",
6914 msg->sadb_msg_satype));
6915 pfkeystat.out_invsatype++;
6918 case 1: /* XXX: What does it do? */
6919 if (msg->sadb_msg_type == SADB_X_PROMISC)
6923 ipseclog((LOG_DEBUG, "key_parse: invalid type %u is passed.\n",
6924 msg->sadb_msg_satype));
6925 pfkeystat.out_invsatype++;
6930 /* check field of upper layer protocol and address family */
6931 if (mh.ext[SADB_EXT_ADDRESS_SRC] != NULL
6932 && mh.ext[SADB_EXT_ADDRESS_DST] != NULL) {
6933 struct sadb_address *src0, *dst0;
6936 src0 = (struct sadb_address *)(mh.ext[SADB_EXT_ADDRESS_SRC]);
6937 dst0 = (struct sadb_address *)(mh.ext[SADB_EXT_ADDRESS_DST]);
6939 /* check upper layer protocol */
6940 if (src0->sadb_address_proto != dst0->sadb_address_proto) {
6941 ipseclog((LOG_DEBUG, "key_parse: upper layer protocol mismatched.\n"));
6942 pfkeystat.out_invaddr++;
6948 if (PFKEY_ADDR_SADDR(src0)->sa_family !=
6949 PFKEY_ADDR_SADDR(dst0)->sa_family) {
6950 ipseclog((LOG_DEBUG, "key_parse: address family mismatched.\n"));
6951 pfkeystat.out_invaddr++;
6955 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
6956 PFKEY_ADDR_SADDR(dst0)->sa_len) {
6957 ipseclog((LOG_DEBUG,
6958 "key_parse: address struct size mismatched.\n"));
6959 pfkeystat.out_invaddr++;
6964 switch (PFKEY_ADDR_SADDR(src0)->sa_family) {
6966 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
6967 sizeof(struct sockaddr_in)) {
6968 pfkeystat.out_invaddr++;
6974 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
6975 sizeof(struct sockaddr_in6)) {
6976 pfkeystat.out_invaddr++;
6982 ipseclog((LOG_DEBUG,
6983 "key_parse: unsupported address family.\n"));
6984 pfkeystat.out_invaddr++;
6985 error = EAFNOSUPPORT;
6989 switch (PFKEY_ADDR_SADDR(src0)->sa_family) {
6991 plen = sizeof(struct in_addr) << 3;
6994 plen = sizeof(struct in6_addr) << 3;
6997 plen = 0; /*fool gcc*/
7001 /* check max prefix length */
7002 if (src0->sadb_address_prefixlen > plen ||
7003 dst0->sadb_address_prefixlen > plen) {
7004 ipseclog((LOG_DEBUG,
7005 "key_parse: illegal prefixlen.\n"));
7006 pfkeystat.out_invaddr++;
7012 * prefixlen == 0 is valid because there can be a case when
7013 * all addresses are matched.
7017 if (msg->sadb_msg_type >= sizeof(key_typesw)/sizeof(key_typesw[0]) ||
7018 key_typesw[msg->sadb_msg_type] == NULL) {
7019 pfkeystat.out_invmsgtype++;
7024 return (*key_typesw[msg->sadb_msg_type])(so, m, &mh);
7027 msg->sadb_msg_errno = error;
7028 return key_sendup_mbuf(so, m, target);
7032 key_senderror(so, m, code)
7037 struct sadb_msg *msg;
7039 if (m->m_len < sizeof(struct sadb_msg))
7040 panic("invalid mbuf passed to key_senderror");
7042 msg = mtod(m, struct sadb_msg *);
7043 msg->sadb_msg_errno = code;
7044 return key_sendup_mbuf(so, m, KEY_SENDUP_ONE);
7048 * set the pointer to each header into message buffer.
7049 * m will be freed on error.
7050 * XXX larger-than-MCLBYTES extension?
7055 struct sadb_msghdr *mhp;
7058 struct sadb_ext *ext;
7064 if (m == NULL || mhp == NULL)
7065 panic("key_align: NULL pointer is passed.\n");
7066 if (m->m_len < sizeof(struct sadb_msg))
7067 panic("invalid mbuf passed to key_align");
7070 bzero(mhp, sizeof(*mhp));
7072 mhp->msg = mtod(m, struct sadb_msg *);
7073 mhp->ext[0] = (struct sadb_ext *)mhp->msg; /*XXX backward compat */
7075 end = PFKEY_UNUNIT64(mhp->msg->sadb_msg_len);
7076 extlen = end; /*just in case extlen is not updated*/
7077 for (off = sizeof(struct sadb_msg); off < end; off += extlen) {
7078 n = m_pulldown(m, off, sizeof(struct sadb_ext), &toff);
7080 /* m is already freed */
7083 ext = (struct sadb_ext *)(mtod(n, caddr_t) + toff);
7086 switch (ext->sadb_ext_type) {
7088 case SADB_EXT_ADDRESS_SRC:
7089 case SADB_EXT_ADDRESS_DST:
7090 case SADB_EXT_ADDRESS_PROXY:
7091 case SADB_EXT_LIFETIME_CURRENT:
7092 case SADB_EXT_LIFETIME_HARD:
7093 case SADB_EXT_LIFETIME_SOFT:
7094 case SADB_EXT_KEY_AUTH:
7095 case SADB_EXT_KEY_ENCRYPT:
7096 case SADB_EXT_IDENTITY_SRC:
7097 case SADB_EXT_IDENTITY_DST:
7098 case SADB_EXT_SENSITIVITY:
7099 case SADB_EXT_PROPOSAL:
7100 case SADB_EXT_SUPPORTED_AUTH:
7101 case SADB_EXT_SUPPORTED_ENCRYPT:
7102 case SADB_EXT_SPIRANGE:
7103 case SADB_X_EXT_POLICY:
7104 case SADB_X_EXT_SA2:
7105 /* duplicate check */
7107 * XXX Are there duplication payloads of either
7108 * KEY_AUTH or KEY_ENCRYPT ?
7110 if (mhp->ext[ext->sadb_ext_type] != NULL) {
7111 ipseclog((LOG_DEBUG,
7112 "key_align: duplicate ext_type %u "
7113 "is passed.\n", ext->sadb_ext_type));
7115 pfkeystat.out_dupext++;
7120 ipseclog((LOG_DEBUG,
7121 "key_align: invalid ext_type %u is passed.\n",
7122 ext->sadb_ext_type));
7124 pfkeystat.out_invexttype++;
7128 extlen = PFKEY_UNUNIT64(ext->sadb_ext_len);
7130 if (key_validate_ext(ext, extlen)) {
7132 pfkeystat.out_invlen++;
7136 n = m_pulldown(m, off, extlen, &toff);
7138 /* m is already freed */
7141 ext = (struct sadb_ext *)(mtod(n, caddr_t) + toff);
7143 mhp->ext[ext->sadb_ext_type] = ext;
7144 mhp->extoff[ext->sadb_ext_type] = off;
7145 mhp->extlen[ext->sadb_ext_type] = extlen;
7150 pfkeystat.out_invlen++;
7158 key_validate_ext(ext, len)
7159 const struct sadb_ext *ext;
7162 const struct sockaddr *sa;
7163 enum { NONE, ADDR } checktype = NONE;
7165 const int sal = offsetof(struct sockaddr, sa_len) + sizeof(sa->sa_len);
7167 if (len != PFKEY_UNUNIT64(ext->sadb_ext_len))
7170 /* if it does not match minimum/maximum length, bail */
7171 if (ext->sadb_ext_type >= sizeof(minsize) / sizeof(minsize[0]) ||
7172 ext->sadb_ext_type >= sizeof(maxsize) / sizeof(maxsize[0]))
7174 if (!minsize[ext->sadb_ext_type] || len < minsize[ext->sadb_ext_type])
7176 if (maxsize[ext->sadb_ext_type] && len > maxsize[ext->sadb_ext_type])
7179 /* more checks based on sadb_ext_type XXX need more */
7180 switch (ext->sadb_ext_type) {
7181 case SADB_EXT_ADDRESS_SRC:
7182 case SADB_EXT_ADDRESS_DST:
7183 case SADB_EXT_ADDRESS_PROXY:
7184 baselen = PFKEY_ALIGN8(sizeof(struct sadb_address));
7187 case SADB_EXT_IDENTITY_SRC:
7188 case SADB_EXT_IDENTITY_DST:
7189 if (((const struct sadb_ident *)ext)->sadb_ident_type ==
7190 SADB_X_IDENTTYPE_ADDR) {
7191 baselen = PFKEY_ALIGN8(sizeof(struct sadb_ident));
7201 switch (checktype) {
7205 sa = (const struct sockaddr *)((c_caddr_t)ext + baselen);
7206 if (len < baselen + sal)
7208 if (baselen + PFKEY_ALIGN8(sa->sa_len) != len)
7221 bzero((caddr_t)&key_cb, sizeof(key_cb));
7223 for (i = 0; i < IPSEC_DIR_MAX; i++) {
7224 LIST_INIT(&sptree[i]);
7227 LIST_INIT(&sahtree);
7229 for (i = 0; i <= SADB_SATYPE_MAX; i++) {
7230 LIST_INIT(®tree[i]);
7233 #ifndef IPSEC_NONBLOCK_ACQUIRE
7234 LIST_INIT(&acqtree);
7236 LIST_INIT(&spacqtree);
7238 /* system default */
7240 ip4_def_policy.policy = IPSEC_POLICY_NONE;
7241 ip4_def_policy.refcnt++; /*never reclaim this*/
7244 ip6_def_policy.policy = IPSEC_POLICY_NONE;
7245 ip6_def_policy.refcnt++; /*never reclaim this*/
7248 #ifndef IPSEC_DEBUG2
7249 timeout((void *)key_timehandler, (void *)0, hz);
7250 #endif /*IPSEC_DEBUG2*/
7252 /* initialize key statistics */
7253 keystat.getspi_count = 1;
7255 printf("IPsec: Initialized Security Association Processing.\n");
7261 * XXX: maybe This function is called after INBOUND IPsec processing.
7263 * Special check for tunnel-mode packets.
7264 * We must make some checks for consistency between inner and outer IP header.
7266 * xxx more checks to be provided
7269 key_checktunnelsanity(sav, family, src, dst)
7270 struct secasvar *sav;
7276 if (sav->sah == NULL)
7277 panic("sav->sah == NULL at key_checktunnelsanity");
7279 /* XXX: check inner IP header */
7285 #define hostnamelen strlen(hostname)
7288 * Get FQDN for the host.
7289 * If the administrator configured hostname (by hostname(1)) without
7290 * domain name, returns nothing.
7297 static char fqdn[MAXHOSTNAMELEN + 1];
7302 /* check if it comes with domain name. */
7304 for (i = 0; i < hostnamelen; i++) {
7305 if (hostname[i] == '.')
7311 /* NOTE: hostname may not be NUL-terminated. */
7312 bzero(fqdn, sizeof(fqdn));
7313 bcopy(hostname, fqdn, hostnamelen);
7314 fqdn[hostnamelen] = '\0';
7319 * get username@FQDN for the host/user.
7325 static char userfqdn[MAXHOSTNAMELEN + MAXLOGNAME + 2];
7326 struct proc *p = curproc;
7329 if (!p || !p->p_pgrp || !p->p_pgrp->pg_session)
7331 if (!(host = key_getfqdn()))
7334 /* NOTE: s_login may not be-NUL terminated. */
7335 bzero(userfqdn, sizeof(userfqdn));
7336 bcopy(p->p_pgrp->pg_session->s_login, userfqdn, MAXLOGNAME);
7337 userfqdn[MAXLOGNAME] = '\0'; /* safeguard */
7338 q = userfqdn + strlen(userfqdn);
7340 bcopy(host, q, strlen(host));
7348 /* record data transfer on SA, and update timestamps */
7350 key_sa_recordxfer(sav, m)
7351 struct secasvar *sav;
7355 panic("key_sa_recordxfer called with sav == NULL");
7357 panic("key_sa_recordxfer called with m == NULL");
7362 * XXX Currently, there is a difference of bytes size
7363 * between inbound and outbound processing.
7365 sav->lft_c->sadb_lifetime_bytes += m->m_pkthdr.len;
7366 /* to check bytes lifetime is done in key_timehandler(). */
7369 * We use the number of packets as the unit of
7370 * sadb_lifetime_allocations. We increment the variable
7371 * whenever {esp,ah}_{in,out}put is called.
7373 sav->lft_c->sadb_lifetime_allocations++;
7374 /* XXX check for expires? */
7377 * NOTE: We record CURRENT sadb_lifetime_usetime by using wall clock,
7378 * in seconds. HARD and SOFT lifetime are measured by the time
7379 * difference (again in seconds) from sadb_lifetime_usetime.
7383 * -----+-----+--------+---> t
7384 * <--------------> HARD
7390 sav->lft_c->sadb_lifetime_usetime = tv.tv_sec;
7391 /* XXX check for expires? */
7399 key_sa_routechange(dst)
7400 struct sockaddr *dst;
7402 struct secashead *sah;
7405 LIST_FOREACH(sah, &sahtree, chain) {
7406 ro = &sah->sa_route;
7407 if (ro->ro_rt && dst->sa_len == ro->ro_dst.sa_len
7408 && bcmp(dst, &ro->ro_dst, dst->sa_len) == 0) {
7410 ro->ro_rt = (struct rtentry *)NULL;
7418 key_sa_chgstate(sav, state)
7419 struct secasvar *sav;
7423 panic("key_sa_chgstate called with sav == NULL");
7425 if (sav->state == state)
7428 if (__LIST_CHAINED(sav))
7429 LIST_REMOVE(sav, chain);
7432 LIST_INSERT_HEAD(&sav->sah->savtree[state], sav, chain);
7437 struct secasvar *sav;
7441 panic("key_sa_stir_iv called with sav == NULL");
7442 key_randomfill(sav->iv, sav->ivlen);
7446 static struct mbuf *
7450 struct mbuf *m = NULL, *n;
7455 MGET(n, MB_DONTWAIT, MT_DATA);
7456 if (n && len > MLEN)
7457 MCLGET(n, MB_DONTWAIT);
7465 n->m_len = M_TRAILINGSPACE(n);
7466 /* use the bottom of mbuf, hoping we can prepend afterwards */
7467 if (n->m_len > len) {
7468 t = (n->m_len - len) & ~(sizeof(long) - 1);