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.11 2004/09/16 23:01:34 joerg Exp $ */
3 /* $KAME: key.c,v 1.191 2001/06/27 10:46:49 sakane Exp $ */
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * This code is referd to RFC 2367
39 #include "opt_inet6.h"
40 #include "opt_ipsec.h"
42 #include <sys/types.h>
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
47 #include <sys/domain.h>
48 #include <sys/protosw.h>
49 #include <sys/malloc.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/sysctl.h>
53 #include <sys/errno.h>
55 #include <sys/queue.h>
56 #include <sys/syslog.h>
59 #include <net/route.h>
60 #include <net/raw_cb.h>
62 #include <netinet/in.h>
63 #include <netinet/in_systm.h>
64 #include <netinet/ip.h>
65 #include <netinet/in_var.h>
68 #include <netinet/ip6.h>
69 #include <netinet6/in6_var.h>
70 #include <netinet6/ip6_var.h>
74 #include <netinet/in_pcb.h>
77 #include <netinet6/in6_pcb.h>
80 #include <net/pfkeyv2.h>
84 #include "key_debug.h"
86 #include <netinet6/ipsec.h>
88 #include <netinet6/ipsec6.h>
90 #include <netinet6/ah.h>
92 #include <netinet6/ah6.h>
95 #include <netinet6/esp.h>
97 #include <netinet6/esp6.h>
100 #include <netinet6/ipcomp.h>
102 #include <netinet6/ipcomp6.h>
105 #include <machine/stdarg.h>
108 #include <sys/random.h>
110 #include <net/net_osdep.h>
113 #define satosin(s) ((struct sockaddr_in *)s)
116 #define FULLMASK 0xff
119 * Note on SA reference counting:
120 * - SAs that are not in DEAD state will have (total external reference + 1)
121 * following value in reference count field. they cannot be freed and are
122 * referenced from SA header.
123 * - SAs that are in DEAD state will have (total external reference)
124 * in reference count field. they are ready to be freed. reference from
125 * SA header will be removed in key_delsav(), when the reference count
126 * field hits 0 (= no external reference other than from SA header.
130 static struct callout key_timehandler_ch;
132 u_int32_t key_debug_level = 0;
133 static u_int key_spi_trycnt = 1000;
134 static u_int32_t key_spi_minval = 0x100;
135 static u_int32_t key_spi_maxval = 0x0fffffff; /* XXX */
136 static u_int32_t policy_id = 0;
137 static u_int key_int_random = 60; /*interval to initialize randseed,1(m)*/
138 static u_int key_larval_lifetime = 30; /* interval to expire acquiring, 30(s)*/
139 static int key_blockacq_count = 10; /* counter for blocking SADB_ACQUIRE.*/
140 static int key_blockacq_lifetime = 20; /* lifetime for blocking SADB_ACQUIRE.*/
141 static int key_preferred_oldsa = 1; /* preferred old sa rather than new sa.*/
143 static u_int32_t acq_seq = 0;
144 static int key_tick_init_random = 0;
146 static LIST_HEAD(_sptree, secpolicy) sptree[IPSEC_DIR_MAX]; /* SPD */
147 static LIST_HEAD(_sahtree, secashead) sahtree; /* SAD */
148 static LIST_HEAD(_regtree, secreg) regtree[SADB_SATYPE_MAX + 1];
150 #ifndef IPSEC_NONBLOCK_ACQUIRE
151 static LIST_HEAD(_acqtree, secacq) acqtree; /* acquiring list */
153 static LIST_HEAD(_spacqtree, secspacq) spacqtree; /* SP acquiring list */
155 struct key_cb key_cb;
157 /* search order for SAs */
158 static const u_int saorder_state_valid_prefer_old[] = {
159 SADB_SASTATE_DYING, SADB_SASTATE_MATURE,
161 static const u_int saorder_state_valid_prefer_new[] = {
162 SADB_SASTATE_MATURE, SADB_SASTATE_DYING,
164 static const u_int saorder_state_alive[] = {
166 SADB_SASTATE_MATURE, SADB_SASTATE_DYING, SADB_SASTATE_LARVAL
168 static const u_int saorder_state_any[] = {
169 SADB_SASTATE_MATURE, SADB_SASTATE_DYING,
170 SADB_SASTATE_LARVAL, SADB_SASTATE_DEAD
173 static const int minsize[] = {
174 sizeof(struct sadb_msg), /* SADB_EXT_RESERVED */
175 sizeof(struct sadb_sa), /* SADB_EXT_SA */
176 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_CURRENT */
177 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_HARD */
178 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_SOFT */
179 sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_SRC */
180 sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_DST */
181 sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_PROXY */
182 sizeof(struct sadb_key), /* SADB_EXT_KEY_AUTH */
183 sizeof(struct sadb_key), /* SADB_EXT_KEY_ENCRYPT */
184 sizeof(struct sadb_ident), /* SADB_EXT_IDENTITY_SRC */
185 sizeof(struct sadb_ident), /* SADB_EXT_IDENTITY_DST */
186 sizeof(struct sadb_sens), /* SADB_EXT_SENSITIVITY */
187 sizeof(struct sadb_prop), /* SADB_EXT_PROPOSAL */
188 sizeof(struct sadb_supported), /* SADB_EXT_SUPPORTED_AUTH */
189 sizeof(struct sadb_supported), /* SADB_EXT_SUPPORTED_ENCRYPT */
190 sizeof(struct sadb_spirange), /* SADB_EXT_SPIRANGE */
191 0, /* SADB_X_EXT_KMPRIVATE */
192 sizeof(struct sadb_x_policy), /* SADB_X_EXT_POLICY */
193 sizeof(struct sadb_x_sa2), /* SADB_X_SA2 */
195 static const int maxsize[] = {
196 sizeof(struct sadb_msg), /* SADB_EXT_RESERVED */
197 sizeof(struct sadb_sa), /* SADB_EXT_SA */
198 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_CURRENT */
199 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_HARD */
200 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_SOFT */
201 0, /* SADB_EXT_ADDRESS_SRC */
202 0, /* SADB_EXT_ADDRESS_DST */
203 0, /* SADB_EXT_ADDRESS_PROXY */
204 0, /* SADB_EXT_KEY_AUTH */
205 0, /* SADB_EXT_KEY_ENCRYPT */
206 0, /* SADB_EXT_IDENTITY_SRC */
207 0, /* SADB_EXT_IDENTITY_DST */
208 0, /* SADB_EXT_SENSITIVITY */
209 0, /* SADB_EXT_PROPOSAL */
210 0, /* SADB_EXT_SUPPORTED_AUTH */
211 0, /* SADB_EXT_SUPPORTED_ENCRYPT */
212 sizeof(struct sadb_spirange), /* SADB_EXT_SPIRANGE */
213 0, /* SADB_X_EXT_KMPRIVATE */
214 0, /* SADB_X_EXT_POLICY */
215 sizeof(struct sadb_x_sa2), /* SADB_X_SA2 */
218 static int ipsec_esp_keymin = 256;
219 static int ipsec_esp_auth = 0;
220 static int ipsec_ah_keymin = 128;
223 SYSCTL_DECL(_net_key);
226 SYSCTL_INT(_net_key, KEYCTL_DEBUG_LEVEL, debug, CTLFLAG_RW, \
227 &key_debug_level, 0, "");
229 /* max count of trial for the decision of spi value */
230 SYSCTL_INT(_net_key, KEYCTL_SPI_TRY, spi_trycnt, CTLFLAG_RW, \
231 &key_spi_trycnt, 0, "");
233 /* minimum spi value to allocate automatically. */
234 SYSCTL_INT(_net_key, KEYCTL_SPI_MIN_VALUE, spi_minval, CTLFLAG_RW, \
235 &key_spi_minval, 0, "");
237 /* maximun spi value to allocate automatically. */
238 SYSCTL_INT(_net_key, KEYCTL_SPI_MAX_VALUE, spi_maxval, CTLFLAG_RW, \
239 &key_spi_maxval, 0, "");
241 /* interval to initialize randseed */
242 SYSCTL_INT(_net_key, KEYCTL_RANDOM_INT, int_random, CTLFLAG_RW, \
243 &key_int_random, 0, "");
245 /* lifetime for larval SA */
246 SYSCTL_INT(_net_key, KEYCTL_LARVAL_LIFETIME, larval_lifetime, CTLFLAG_RW, \
247 &key_larval_lifetime, 0, "");
249 /* counter for blocking to send SADB_ACQUIRE to IKEd */
250 SYSCTL_INT(_net_key, KEYCTL_BLOCKACQ_COUNT, blockacq_count, CTLFLAG_RW, \
251 &key_blockacq_count, 0, "");
253 /* lifetime for blocking to send SADB_ACQUIRE to IKEd */
254 SYSCTL_INT(_net_key, KEYCTL_BLOCKACQ_LIFETIME, blockacq_lifetime, CTLFLAG_RW, \
255 &key_blockacq_lifetime, 0, "");
258 SYSCTL_INT(_net_key, KEYCTL_ESP_AUTH, esp_auth, CTLFLAG_RW, \
259 &ipsec_esp_auth, 0, "");
261 /* minimum ESP key length */
262 SYSCTL_INT(_net_key, KEYCTL_ESP_KEYMIN, esp_keymin, CTLFLAG_RW, \
263 &ipsec_esp_keymin, 0, "");
265 /* minimum AH key length */
266 SYSCTL_INT(_net_key, KEYCTL_AH_KEYMIN, ah_keymin, CTLFLAG_RW, \
267 &ipsec_ah_keymin, 0, "");
269 /* perfered old SA rather than new SA */
270 SYSCTL_INT(_net_key, KEYCTL_PREFERED_OLDSA, prefered_oldsa, CTLFLAG_RW,\
271 &key_preferred_oldsa, 0, "");
274 #define LIST_FOREACH(elm, head, field) \
275 for (elm = LIST_FIRST(head); elm; elm = LIST_NEXT(elm, field))
277 #define __LIST_CHAINED(elm) \
278 (!((elm)->chain.le_next == NULL && (elm)->chain.le_prev == NULL))
279 #define LIST_INSERT_TAIL(head, elm, type, field) \
281 struct type *curelm = LIST_FIRST(head); \
282 if (curelm == NULL) {\
283 LIST_INSERT_HEAD(head, elm, field); \
285 while (LIST_NEXT(curelm, field)) \
286 curelm = LIST_NEXT(curelm, field);\
287 LIST_INSERT_AFTER(curelm, elm, field);\
291 #define KEY_CHKSASTATE(head, sav, name) \
293 if ((head) != (sav)) { \
294 ipseclog((LOG_DEBUG, "%s: state mismatched (TREE=%d SA=%d)\n", \
295 (name), (head), (sav))); \
300 #define KEY_CHKSPDIR(head, sp, name) \
302 if ((head) != (sp)) { \
303 ipseclog((LOG_DEBUG, "%s: direction mismatched (TREE=%d SP=%d), " \
304 "anyway continue.\n", \
305 (name), (head), (sp))); \
310 #define KMALLOC(p, t, n) \
311 ((p) = (t) malloc((unsigned long)(n), M_SECA, M_INTWAIT | M_NULLOK))
313 free((caddr_t)(p), M_SECA);
315 #define KMALLOC(p, t, n) \
317 ((p) = (t)malloc((unsigned long)(n), M_SECA, M_INTWAIT | M_NULLOK)); \
318 printf("%s %d: %p <- KMALLOC(%s, %d)\n", \
319 __FILE__, __LINE__, (p), #t, n); \
324 printf("%s %d: %p -> KFREE()\n", __FILE__, __LINE__, (p)); \
325 free((caddr_t)(p), M_SECA); \
330 * set parameters into secpolicyindex buffer.
331 * Must allocate secpolicyindex buffer passed to this function.
333 #define KEY_SETSECSPIDX(_dir, s, d, ps, pd, ulp, idx) \
335 bzero((idx), sizeof(struct secpolicyindex)); \
336 (idx)->dir = (_dir); \
337 (idx)->prefs = (ps); \
338 (idx)->prefd = (pd); \
339 (idx)->ul_proto = (ulp); \
340 bcopy((s), &(idx)->src, ((struct sockaddr *)(s))->sa_len); \
341 bcopy((d), &(idx)->dst, ((struct sockaddr *)(d))->sa_len); \
345 * set parameters into secasindex buffer.
346 * Must allocate secasindex buffer before calling this function.
348 #define KEY_SETSECASIDX(p, m, r, s, d, idx) \
350 bzero((idx), sizeof(struct secasindex)); \
351 (idx)->proto = (p); \
353 (idx)->reqid = (r); \
354 bcopy((s), &(idx)->src, ((struct sockaddr *)(s))->sa_len); \
355 bcopy((d), &(idx)->dst, ((struct sockaddr *)(d))->sa_len); \
360 u_long getspi_count; /* the avarage of count to try to get new SPI */
364 struct sadb_msg *msg;
365 struct sadb_ext *ext[SADB_EXT_MAX + 1];
366 int extoff[SADB_EXT_MAX + 1];
367 int extlen[SADB_EXT_MAX + 1];
370 static struct secasvar *key_allocsa_policy (struct secasindex *);
371 static void key_freesp_so (struct secpolicy **);
372 static struct secasvar *key_do_allocsa_policy (struct secashead *, u_int);
373 static void key_delsp (struct secpolicy *);
374 static struct secpolicy *key_getsp (struct secpolicyindex *);
375 static struct secpolicy *key_getspbyid (u_int32_t);
376 static u_int32_t key_newreqid (void);
377 static struct mbuf *key_gather_mbuf (struct mbuf *,
378 const struct sadb_msghdr *, int, int, ...);
379 static int key_spdadd (struct socket *, struct mbuf *,
380 const struct sadb_msghdr *);
381 static u_int32_t key_getnewspid (void);
382 static int key_spddelete (struct socket *, struct mbuf *,
383 const struct sadb_msghdr *);
384 static int key_spddelete2 (struct socket *, struct mbuf *,
385 const struct sadb_msghdr *);
386 static int key_spdget (struct socket *, struct mbuf *,
387 const struct sadb_msghdr *);
388 static int key_spdflush (struct socket *, struct mbuf *,
389 const struct sadb_msghdr *);
390 static int key_spddump (struct socket *, struct mbuf *,
391 const struct sadb_msghdr *);
392 static struct mbuf *key_setdumpsp (struct secpolicy *,
393 u_int8_t, u_int32_t, u_int32_t);
394 static u_int key_getspreqmsglen (struct secpolicy *);
395 static int key_spdexpire (struct secpolicy *);
396 static struct secashead *key_newsah (struct secasindex *);
397 static void key_delsah (struct secashead *);
398 static struct secasvar *key_newsav (struct mbuf *,
399 const struct sadb_msghdr *, struct secashead *, int *);
400 static void key_delsav (struct secasvar *);
401 static struct secashead *key_getsah (struct secasindex *);
402 static struct secasvar *key_checkspidup (struct secasindex *, u_int32_t);
403 static struct secasvar *key_getsavbyspi (struct secashead *, u_int32_t);
404 static int key_setsaval (struct secasvar *, struct mbuf *,
405 const struct sadb_msghdr *);
406 static int key_mature (struct secasvar *);
407 static struct mbuf *key_setdumpsa (struct secasvar *, u_int8_t,
408 u_int8_t, u_int32_t, u_int32_t);
409 static struct mbuf *key_setsadbmsg (u_int8_t, u_int16_t, u_int8_t,
410 u_int32_t, pid_t, u_int16_t);
411 static struct mbuf *key_setsadbsa (struct secasvar *);
412 static struct mbuf *key_setsadbaddr (u_int16_t,
413 struct sockaddr *, u_int8_t, u_int16_t);
415 static struct mbuf *key_setsadbident (u_int16_t, u_int16_t, caddr_t,
418 static struct mbuf *key_setsadbxsa2 (u_int8_t, u_int32_t, u_int32_t);
419 static struct mbuf *key_setsadbxpolicy (u_int16_t, u_int8_t,
421 static void *key_newbuf (const void *, u_int);
423 static int key_ismyaddr6 (struct sockaddr_in6 *);
426 /* flags for key_cmpsaidx() */
427 #define CMP_HEAD 1 /* protocol, addresses. */
428 #define CMP_MODE_REQID 2 /* additionally HEAD, reqid, mode. */
429 #define CMP_REQID 3 /* additionally HEAD, reaid. */
430 #define CMP_EXACTLY 4 /* all elements. */
431 static int key_cmpsaidx
432 (struct secasindex *, struct secasindex *, int);
434 static int key_cmpspidx_exactly
435 (struct secpolicyindex *, struct secpolicyindex *);
436 static int key_cmpspidx_withmask
437 (struct secpolicyindex *, struct secpolicyindex *);
438 static int key_sockaddrcmp (struct sockaddr *, struct sockaddr *, int);
439 static int key_bbcmp (caddr_t, caddr_t, u_int);
440 static void key_srandom (void);
441 static u_int16_t key_satype2proto (u_int8_t);
442 static u_int8_t key_proto2satype (u_int16_t);
444 static int key_getspi (struct socket *, struct mbuf *,
445 const struct sadb_msghdr *);
446 static u_int32_t key_do_getnewspi (struct sadb_spirange *,
447 struct secasindex *);
448 static int key_update (struct socket *, struct mbuf *,
449 const struct sadb_msghdr *);
450 #ifdef IPSEC_DOSEQCHECK
451 static struct secasvar *key_getsavbyseq (struct secashead *, u_int32_t);
453 static int key_add (struct socket *, struct mbuf *,
454 const struct sadb_msghdr *);
455 static int key_setident (struct secashead *, struct mbuf *,
456 const struct sadb_msghdr *);
457 static struct mbuf *key_getmsgbuf_x1 (struct mbuf *,
458 const struct sadb_msghdr *);
459 static int key_delete (struct socket *, struct mbuf *,
460 const struct sadb_msghdr *);
461 static int key_get (struct socket *, struct mbuf *,
462 const struct sadb_msghdr *);
464 static void key_getcomb_setlifetime (struct sadb_comb *);
466 static struct mbuf *key_getcomb_esp (void);
468 static struct mbuf *key_getcomb_ah (void);
469 static struct mbuf *key_getcomb_ipcomp (void);
470 static struct mbuf *key_getprop (const struct secasindex *);
472 static int key_acquire (struct secasindex *, struct secpolicy *);
473 #ifndef IPSEC_NONBLOCK_ACQUIRE
474 static struct secacq *key_newacq (struct secasindex *);
475 static struct secacq *key_getacq (struct secasindex *);
476 static struct secacq *key_getacqbyseq (u_int32_t);
478 static struct secspacq *key_newspacq (struct secpolicyindex *);
479 static struct secspacq *key_getspacq (struct secpolicyindex *);
480 static int key_acquire2 (struct socket *, struct mbuf *,
481 const struct sadb_msghdr *);
482 static int key_register (struct socket *, struct mbuf *,
483 const struct sadb_msghdr *);
484 static int key_expire (struct secasvar *);
485 static int key_flush (struct socket *, struct mbuf *,
486 const struct sadb_msghdr *);
487 static int key_dump (struct socket *, struct mbuf *,
488 const struct sadb_msghdr *);
489 static int key_promisc (struct socket *, struct mbuf *,
490 const struct sadb_msghdr *);
491 static int key_senderror (struct socket *, struct mbuf *, int);
492 static int key_validate_ext (const struct sadb_ext *, int);
493 static int key_align (struct mbuf *, struct sadb_msghdr *);
495 static const char *key_getfqdn (void);
496 static const char *key_getuserfqdn (void);
498 static void key_sa_chgstate (struct secasvar *, u_int8_t);
499 static struct mbuf *key_alloc_mbuf (int);
501 /* %%% IPsec policy management */
503 * allocating a SP for OUTBOUND or INBOUND packet.
504 * Must call key_freesp() later.
505 * OUT: NULL: not found
506 * others: found and return the pointer.
509 key_allocsp(spidx, dir)
510 struct secpolicyindex *spidx;
513 struct secpolicy *sp;
519 panic("key_allocsp: NULL pointer is passed.\n");
521 /* check direction */
523 case IPSEC_DIR_INBOUND:
524 case IPSEC_DIR_OUTBOUND:
527 panic("key_allocsp: Invalid direction is passed.\n");
531 s = splnet(); /*called from softclock()*/
532 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
533 printf("*** objects\n");
534 kdebug_secpolicyindex(spidx));
536 LIST_FOREACH(sp, &sptree[dir], chain) {
537 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
538 printf("*** in SPD\n");
539 kdebug_secpolicyindex(&sp->spidx));
541 if (sp->state == IPSEC_SPSTATE_DEAD)
543 if (key_cmpspidx_withmask(&sp->spidx, spidx))
552 KEY_CHKSPDIR(sp->spidx.dir, dir, "key_allocsp");
554 /* found a SPD entry */
556 sp->lastused = tv.tv_sec;
559 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
560 printf("DP key_allocsp cause refcnt++:%d SP:%p\n",
567 * return a policy that matches this particular inbound packet.
571 key_gettunnel(osrc, odst, isrc, idst)
572 struct sockaddr *osrc, *odst, *isrc, *idst;
574 struct secpolicy *sp;
575 const int dir = IPSEC_DIR_INBOUND;
578 struct ipsecrequest *r1, *r2, *p;
579 struct sockaddr *os, *od, *is, *id;
580 struct secpolicyindex spidx;
582 if (isrc->sa_family != idst->sa_family) {
583 ipseclog((LOG_ERR, "protocol family mismatched %d != %d\n.",
584 isrc->sa_family, idst->sa_family));
588 s = splnet(); /*called from softclock()*/
589 LIST_FOREACH(sp, &sptree[dir], chain) {
590 if (sp->state == IPSEC_SPSTATE_DEAD)
594 for (p = sp->req; p; p = p->next) {
595 if (p->saidx.mode != IPSEC_MODE_TUNNEL)
602 /* here we look at address matches only */
604 if (isrc->sa_len > sizeof(spidx.src) ||
605 idst->sa_len > sizeof(spidx.dst))
607 bcopy(isrc, &spidx.src, isrc->sa_len);
608 bcopy(idst, &spidx.dst, idst->sa_len);
609 if (!key_cmpspidx_withmask(&sp->spidx, &spidx))
612 is = (struct sockaddr *)&r1->saidx.src;
613 id = (struct sockaddr *)&r1->saidx.dst;
614 if (key_sockaddrcmp(is, isrc, 0) ||
615 key_sockaddrcmp(id, idst, 0))
619 os = (struct sockaddr *)&r2->saidx.src;
620 od = (struct sockaddr *)&r2->saidx.dst;
621 if (key_sockaddrcmp(os, osrc, 0) ||
622 key_sockaddrcmp(od, odst, 0))
633 sp->lastused = tv.tv_sec;
640 * allocating an SA entry for an *OUTBOUND* packet.
641 * checking each request entries in SP, and acquire an SA if need.
642 * OUT: 0: there are valid requests.
643 * ENOENT: policy may be valid, but SA with REQUIRE is on acquiring.
646 key_checkrequest(isr, saidx)
647 struct ipsecrequest *isr;
648 struct secasindex *saidx;
654 if (isr == NULL || saidx == NULL)
655 panic("key_checkrequest: NULL pointer is passed.\n");
658 switch (saidx->mode) {
659 case IPSEC_MODE_TRANSPORT:
660 case IPSEC_MODE_TUNNEL:
664 panic("key_checkrequest: Invalid policy defined.\n");
667 /* get current level */
668 level = ipsec_get_reqlevel(isr);
672 * We do allocate new SA only if the state of SA in the holder is
673 * SADB_SASTATE_DEAD. The SA for outbound must be the oldest.
675 if (isr->sav != NULL) {
676 if (isr->sav->sah == NULL)
677 panic("key_checkrequest: sah is null.\n");
678 if (isr->sav == (struct secasvar *)LIST_FIRST(
679 &isr->sav->sah->savtree[SADB_SASTATE_DEAD])) {
680 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
681 printf("DP checkrequest calls free SA:%p\n",
683 key_freesav(isr->sav);
689 * we free any SA stashed in the IPsec request because a different
690 * SA may be involved each time this request is checked, either
691 * because new SAs are being configured, or this request is
692 * associated with an unconnected datagram socket, or this request
693 * is associated with a system default policy.
695 * The operation may have negative impact to performance. We may
696 * want to check cached SA carefully, rather than picking new SA
699 if (isr->sav != NULL) {
700 key_freesav(isr->sav);
706 * new SA allocation if no SA found.
707 * key_allocsa_policy should allocate the oldest SA available.
708 * See key_do_allocsa_policy(), and draft-jenkins-ipsec-rekeying-03.txt.
710 if (isr->sav == NULL)
711 isr->sav = key_allocsa_policy(saidx);
713 /* When there is SA. */
714 if (isr->sav != NULL)
718 if ((error = key_acquire(saidx, isr->sp)) != 0) {
719 /* XXX What should I do ? */
720 ipseclog((LOG_DEBUG, "key_checkrequest: error %d returned "
721 "from key_acquire.\n", error));
725 return level == IPSEC_LEVEL_REQUIRE ? ENOENT : 0;
729 * allocating a SA for policy entry from SAD.
730 * NOTE: searching SAD of aliving state.
731 * OUT: NULL: not found.
732 * others: found and return the pointer.
734 static struct secasvar *
735 key_allocsa_policy(saidx)
736 struct secasindex *saidx;
738 struct secashead *sah;
739 struct secasvar *sav;
740 u_int stateidx, state;
741 const u_int *saorder_state_valid;
744 LIST_FOREACH(sah, &sahtree, chain) {
745 if (sah->state == SADB_SASTATE_DEAD)
747 if (key_cmpsaidx(&sah->saidx, saidx, CMP_MODE_REQID))
756 * search a valid state list for outbound packet.
757 * This search order is important.
759 if (key_preferred_oldsa) {
760 saorder_state_valid = saorder_state_valid_prefer_old;
761 arraysize = _ARRAYLEN(saorder_state_valid_prefer_old);
763 saorder_state_valid = saorder_state_valid_prefer_new;
764 arraysize = _ARRAYLEN(saorder_state_valid_prefer_new);
767 for (stateidx = 0; stateidx < arraysize; stateidx++) {
769 state = saorder_state_valid[stateidx];
771 sav = key_do_allocsa_policy(sah, state);
780 * searching SAD with direction, protocol, mode and state.
781 * called by key_allocsa_policy().
784 * others : found, pointer to a SA.
786 static struct secasvar *
787 key_do_allocsa_policy(sah, state)
788 struct secashead *sah;
791 struct secasvar *sav, *nextsav, *candidate, *d;
796 for (sav = LIST_FIRST(&sah->savtree[state]);
800 nextsav = LIST_NEXT(sav, chain);
803 KEY_CHKSASTATE(sav->state, state, "key_do_allocsa_policy");
806 if (candidate == NULL) {
811 /* Which SA is the better ? */
814 if (candidate->lft_c == NULL || sav->lft_c == NULL)
815 panic("key_do_allocsa_policy: "
816 "lifetime_current is NULL.\n");
818 /* What the best method is to compare ? */
819 if (key_preferred_oldsa) {
820 if (candidate->lft_c->sadb_lifetime_addtime >
821 sav->lft_c->sadb_lifetime_addtime) {
828 /* prefered new sa rather than old sa */
829 if (candidate->lft_c->sadb_lifetime_addtime <
830 sav->lft_c->sadb_lifetime_addtime) {
837 * prepared to delete the SA when there is more
838 * suitable candidate and the lifetime of the SA is not
841 if (d->lft_c->sadb_lifetime_addtime != 0) {
842 struct mbuf *m, *result;
844 key_sa_chgstate(d, SADB_SASTATE_DEAD);
846 m = key_setsadbmsg(SADB_DELETE, 0,
847 d->sah->saidx.proto, 0, 0, d->refcnt - 1);
852 /* set sadb_address for saidx's. */
853 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
854 (struct sockaddr *)&d->sah->saidx.src,
855 d->sah->saidx.src.ss_len << 3,
861 /* set sadb_address for saidx's. */
862 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
863 (struct sockaddr *)&d->sah->saidx.src,
864 d->sah->saidx.src.ss_len << 3,
870 /* create SA extension */
871 m = key_setsadbsa(d);
876 if (result->m_len < sizeof(struct sadb_msg)) {
877 result = m_pullup(result,
878 sizeof(struct sadb_msg));
883 result->m_pkthdr.len = 0;
884 for (m = result; m; m = m->m_next)
885 result->m_pkthdr.len += m->m_len;
886 mtod(result, struct sadb_msg *)->sadb_msg_len =
887 PFKEY_UNIT64(result->m_pkthdr.len);
889 if (key_sendup_mbuf(NULL, result,
890 KEY_SENDUP_REGISTERED))
899 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
900 printf("DP allocsa_policy cause "
901 "refcnt++:%d SA:%p\n",
902 candidate->refcnt, candidate));
908 * allocating a SA entry for a *INBOUND* packet.
909 * Must call key_freesav() later.
910 * OUT: positive: pointer to a sav.
911 * NULL: not found, or error occured.
913 * In the comparison, source address will be ignored for RFC2401 conformance.
914 * To quote, from section 4.1:
915 * A security association is uniquely identified by a triple consisting
916 * of a Security Parameter Index (SPI), an IP Destination Address, and a
917 * security protocol (AH or ESP) identifier.
918 * Note that, however, we do need to keep source address in IPsec SA.
919 * IKE specification and PF_KEY specification do assume that we
920 * keep source address in IPsec SA. We see a tricky situation here.
923 key_allocsa(family, src, dst, proto, spi)
928 struct secashead *sah;
929 struct secasvar *sav;
930 u_int stateidx, state;
931 struct sockaddr_in sin;
932 struct sockaddr_in6 sin6;
934 const u_int *saorder_state_valid;
938 if (src == NULL || dst == NULL)
939 panic("key_allocsa: NULL pointer is passed.\n");
942 * when both systems employ similar strategy to use a SA.
943 * the search order is important even in the inbound case.
945 if (key_preferred_oldsa) {
946 saorder_state_valid = saorder_state_valid_prefer_old;
947 arraysize = _ARRAYLEN(saorder_state_valid_prefer_old);
949 saorder_state_valid = saorder_state_valid_prefer_new;
950 arraysize = _ARRAYLEN(saorder_state_valid_prefer_new);
955 * XXX: to be checked internal IP header somewhere. Also when
956 * IPsec tunnel packet is received. But ESP tunnel mode is
957 * encrypted so we can't check internal IP header.
959 s = splnet(); /*called from softclock()*/
960 LIST_FOREACH(sah, &sahtree, chain) {
962 * search a valid state list for inbound packet.
963 * the search order is not important.
965 for (stateidx = 0; stateidx < arraysize; stateidx++) {
966 state = saorder_state_valid[stateidx];
967 LIST_FOREACH(sav, &sah->savtree[state], chain) {
969 KEY_CHKSASTATE(sav->state, state, "key_allocsav");
970 if (proto != sav->sah->saidx.proto)
974 if (family != sav->sah->saidx.src.ss_family ||
975 family != sav->sah->saidx.dst.ss_family)
978 #if 0 /* don't check src */
979 /* check src address */
982 bzero(&sin, sizeof(sin));
983 sin.sin_family = AF_INET;
984 sin.sin_len = sizeof(sin);
985 bcopy(src, &sin.sin_addr,
986 sizeof(sin.sin_addr));
987 if (key_sockaddrcmp((struct sockaddr*)&sin,
988 (struct sockaddr *)&sav->sah->saidx.src, 0) != 0)
993 bzero(&sin6, sizeof(sin6));
994 sin6.sin6_family = AF_INET6;
995 sin6.sin6_len = sizeof(sin6);
996 bcopy(src, &sin6.sin6_addr,
997 sizeof(sin6.sin6_addr));
998 if (IN6_IS_SCOPE_LINKLOCAL(&sin6.sin6_addr)) {
999 /* kame fake scopeid */
1000 sin6.sin6_scope_id =
1001 ntohs(sin6.sin6_addr.s6_addr16[1]);
1002 sin6.sin6_addr.s6_addr16[1] = 0;
1004 if (key_sockaddrcmp((struct sockaddr*)&sin6,
1005 (struct sockaddr *)&sav->sah->saidx.src, 0) != 0)
1009 ipseclog((LOG_DEBUG, "key_allocsa: "
1010 "unknown address family=%d.\n",
1016 /* check dst address */
1019 bzero(&sin, sizeof(sin));
1020 sin.sin_family = AF_INET;
1021 sin.sin_len = sizeof(sin);
1022 bcopy(dst, &sin.sin_addr,
1023 sizeof(sin.sin_addr));
1024 if (key_sockaddrcmp((struct sockaddr*)&sin,
1025 (struct sockaddr *)&sav->sah->saidx.dst, 0) != 0)
1030 bzero(&sin6, sizeof(sin6));
1031 sin6.sin6_family = AF_INET6;
1032 sin6.sin6_len = sizeof(sin6);
1033 bcopy(dst, &sin6.sin6_addr,
1034 sizeof(sin6.sin6_addr));
1035 if (IN6_IS_SCOPE_LINKLOCAL(&sin6.sin6_addr)) {
1036 /* kame fake scopeid */
1037 sin6.sin6_scope_id =
1038 ntohs(sin6.sin6_addr.s6_addr16[1]);
1039 sin6.sin6_addr.s6_addr16[1] = 0;
1041 if (key_sockaddrcmp((struct sockaddr*)&sin6,
1042 (struct sockaddr *)&sav->sah->saidx.dst, 0) != 0)
1046 ipseclog((LOG_DEBUG, "key_allocsa: "
1047 "unknown address family=%d.\n",
1064 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1065 printf("DP allocsa cause refcnt++:%d SA:%p\n",
1071 * Must be called after calling key_allocsp().
1072 * For both the packet without socket and key_freeso().
1076 struct secpolicy *sp;
1080 panic("key_freesp: NULL pointer is passed.\n");
1083 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1084 printf("DP freesp cause refcnt--:%d SP:%p\n",
1087 if (sp->refcnt == 0)
1094 * Must be called after calling key_allocsp().
1095 * For the packet with socket.
1103 panic("key_freeso: NULL pointer is passed.\n");
1105 switch (so->so_proto->pr_domain->dom_family) {
1109 struct inpcb *pcb = sotoinpcb(so);
1111 /* Does it have a PCB ? */
1114 key_freesp_so(&pcb->inp_sp->sp_in);
1115 key_freesp_so(&pcb->inp_sp->sp_out);
1122 #ifdef HAVE_NRL_INPCB
1123 struct inpcb *pcb = sotoinpcb(so);
1125 /* Does it have a PCB ? */
1128 key_freesp_so(&pcb->inp_sp->sp_in);
1129 key_freesp_so(&pcb->inp_sp->sp_out);
1131 struct in6pcb *pcb = sotoin6pcb(so);
1133 /* Does it have a PCB ? */
1136 key_freesp_so(&pcb->in6p_sp->sp_in);
1137 key_freesp_so(&pcb->in6p_sp->sp_out);
1143 ipseclog((LOG_DEBUG, "key_freeso: unknown address family=%d.\n",
1144 so->so_proto->pr_domain->dom_family));
1153 struct secpolicy **sp;
1156 if (sp == NULL || *sp == NULL)
1157 panic("key_freesp_so: sp == NULL\n");
1159 switch ((*sp)->policy) {
1160 case IPSEC_POLICY_IPSEC:
1161 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1162 printf("DP freeso calls free SP:%p\n", *sp));
1166 case IPSEC_POLICY_ENTRUST:
1167 case IPSEC_POLICY_BYPASS:
1170 panic("key_freesp_so: Invalid policy found %d", (*sp)->policy);
1177 * Must be called after calling key_allocsa().
1178 * This function is called by key_freesp() to free some SA allocated
1183 struct secasvar *sav;
1187 panic("key_freesav: NULL pointer is passed.\n");
1190 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1191 printf("DP freesav cause refcnt--:%d SA:%p SPI %u\n",
1192 sav->refcnt, sav, (u_int32_t)ntohl(sav->spi)));
1194 if (sav->refcnt == 0)
1200 /* %%% SPD management */
1202 * free security policy entry.
1206 struct secpolicy *sp;
1212 panic("key_delsp: NULL pointer is passed.\n");
1214 sp->state = IPSEC_SPSTATE_DEAD;
1217 return; /* can't free */
1219 s = splnet(); /*called from softclock()*/
1220 /* remove from SP index */
1221 if (__LIST_CHAINED(sp))
1222 LIST_REMOVE(sp, chain);
1225 struct ipsecrequest *isr = sp->req, *nextisr;
1227 while (isr != NULL) {
1228 if (isr->sav != NULL) {
1229 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1230 printf("DP delsp calls free SA:%p\n",
1232 key_freesav(isr->sav);
1236 nextisr = isr->next;
1242 keydb_delsecpolicy(sp);
1251 * OUT: NULL : not found
1252 * others : found, pointer to a SP.
1254 static struct secpolicy *
1256 struct secpolicyindex *spidx;
1258 struct secpolicy *sp;
1262 panic("key_getsp: NULL pointer is passed.\n");
1264 LIST_FOREACH(sp, &sptree[spidx->dir], chain) {
1265 if (sp->state == IPSEC_SPSTATE_DEAD)
1267 if (key_cmpspidx_exactly(spidx, &sp->spidx)) {
1278 * OUT: NULL : not found
1279 * others : found, pointer to a SP.
1281 static struct secpolicy *
1285 struct secpolicy *sp;
1287 LIST_FOREACH(sp, &sptree[IPSEC_DIR_INBOUND], chain) {
1288 if (sp->state == IPSEC_SPSTATE_DEAD)
1296 LIST_FOREACH(sp, &sptree[IPSEC_DIR_OUTBOUND], chain) {
1297 if (sp->state == IPSEC_SPSTATE_DEAD)
1311 struct secpolicy *newsp = NULL;
1313 newsp = keydb_newsecpolicy();
1324 * create secpolicy structure from sadb_x_policy structure.
1325 * NOTE: `state', `secpolicyindex' in secpolicy structure are not set,
1326 * so must be set properly later.
1329 key_msg2sp(xpl0, len, error)
1330 struct sadb_x_policy *xpl0;
1334 struct secpolicy *newsp;
1338 panic("key_msg2sp: NULL pointer was passed.\n");
1339 if (len < sizeof(*xpl0))
1340 panic("key_msg2sp: invalid length.\n");
1341 if (len != PFKEY_EXTLEN(xpl0)) {
1342 ipseclog((LOG_DEBUG, "key_msg2sp: Invalid msg length.\n"));
1347 if ((newsp = key_newsp()) == NULL) {
1352 newsp->spidx.dir = xpl0->sadb_x_policy_dir;
1353 newsp->policy = xpl0->sadb_x_policy_type;
1356 switch (xpl0->sadb_x_policy_type) {
1357 case IPSEC_POLICY_DISCARD:
1358 case IPSEC_POLICY_NONE:
1359 case IPSEC_POLICY_ENTRUST:
1360 case IPSEC_POLICY_BYPASS:
1364 case IPSEC_POLICY_IPSEC:
1367 struct sadb_x_ipsecrequest *xisr;
1368 struct ipsecrequest **p_isr = &newsp->req;
1370 /* validity check */
1371 if (PFKEY_EXTLEN(xpl0) < sizeof(*xpl0)) {
1372 ipseclog((LOG_DEBUG,
1373 "key_msg2sp: Invalid msg length.\n"));
1379 tlen = PFKEY_EXTLEN(xpl0) - sizeof(*xpl0);
1380 xisr = (struct sadb_x_ipsecrequest *)(xpl0 + 1);
1385 if (xisr->sadb_x_ipsecrequest_len < sizeof(*xisr)) {
1386 ipseclog((LOG_DEBUG, "key_msg2sp: "
1387 "invalid ipsecrequest length.\n"));
1393 /* allocate request buffer */
1394 KMALLOC(*p_isr, struct ipsecrequest *, sizeof(**p_isr));
1395 if ((*p_isr) == NULL) {
1396 ipseclog((LOG_DEBUG,
1397 "key_msg2sp: No more memory.\n"));
1402 bzero(*p_isr, sizeof(**p_isr));
1405 (*p_isr)->next = NULL;
1407 switch (xisr->sadb_x_ipsecrequest_proto) {
1410 case IPPROTO_IPCOMP:
1413 ipseclog((LOG_DEBUG,
1414 "key_msg2sp: invalid proto type=%u\n",
1415 xisr->sadb_x_ipsecrequest_proto));
1417 *error = EPROTONOSUPPORT;
1420 (*p_isr)->saidx.proto = xisr->sadb_x_ipsecrequest_proto;
1422 switch (xisr->sadb_x_ipsecrequest_mode) {
1423 case IPSEC_MODE_TRANSPORT:
1424 case IPSEC_MODE_TUNNEL:
1426 case IPSEC_MODE_ANY:
1428 ipseclog((LOG_DEBUG,
1429 "key_msg2sp: invalid mode=%u\n",
1430 xisr->sadb_x_ipsecrequest_mode));
1435 (*p_isr)->saidx.mode = xisr->sadb_x_ipsecrequest_mode;
1437 switch (xisr->sadb_x_ipsecrequest_level) {
1438 case IPSEC_LEVEL_DEFAULT:
1439 case IPSEC_LEVEL_USE:
1440 case IPSEC_LEVEL_REQUIRE:
1442 case IPSEC_LEVEL_UNIQUE:
1443 /* validity check */
1445 * If range violation of reqid, kernel will
1446 * update it, don't refuse it.
1448 if (xisr->sadb_x_ipsecrequest_reqid
1449 > IPSEC_MANUAL_REQID_MAX) {
1450 ipseclog((LOG_DEBUG,
1451 "key_msg2sp: reqid=%d range "
1452 "violation, updated by kernel.\n",
1453 xisr->sadb_x_ipsecrequest_reqid));
1454 xisr->sadb_x_ipsecrequest_reqid = 0;
1457 /* allocate new reqid id if reqid is zero. */
1458 if (xisr->sadb_x_ipsecrequest_reqid == 0) {
1460 if ((reqid = key_newreqid()) == 0) {
1465 (*p_isr)->saidx.reqid = reqid;
1466 xisr->sadb_x_ipsecrequest_reqid = reqid;
1468 /* set it for manual keying. */
1469 (*p_isr)->saidx.reqid =
1470 xisr->sadb_x_ipsecrequest_reqid;
1475 ipseclog((LOG_DEBUG, "key_msg2sp: invalid level=%u\n",
1476 xisr->sadb_x_ipsecrequest_level));
1481 (*p_isr)->level = xisr->sadb_x_ipsecrequest_level;
1483 /* set IP addresses if there */
1484 if (xisr->sadb_x_ipsecrequest_len > sizeof(*xisr)) {
1485 struct sockaddr *paddr;
1487 paddr = (struct sockaddr *)(xisr + 1);
1489 /* validity check */
1491 > sizeof((*p_isr)->saidx.src)) {
1492 ipseclog((LOG_DEBUG, "key_msg2sp: invalid request "
1493 "address length.\n"));
1498 bcopy(paddr, &(*p_isr)->saidx.src,
1501 paddr = (struct sockaddr *)((caddr_t)paddr
1504 /* validity check */
1506 > sizeof((*p_isr)->saidx.dst)) {
1507 ipseclog((LOG_DEBUG, "key_msg2sp: invalid request "
1508 "address length.\n"));
1513 bcopy(paddr, &(*p_isr)->saidx.dst,
1517 (*p_isr)->sav = NULL;
1518 (*p_isr)->sp = newsp;
1520 /* initialization for the next. */
1521 p_isr = &(*p_isr)->next;
1522 tlen -= xisr->sadb_x_ipsecrequest_len;
1524 /* validity check */
1526 ipseclog((LOG_DEBUG, "key_msg2sp: becoming tlen < 0.\n"));
1532 xisr = (struct sadb_x_ipsecrequest *)((caddr_t)xisr
1533 + xisr->sadb_x_ipsecrequest_len);
1538 ipseclog((LOG_DEBUG, "key_msg2sp: invalid policy type.\n"));
1551 static u_int32_t auto_reqid = IPSEC_MANUAL_REQID_MAX + 1;
1553 auto_reqid = (auto_reqid == ~0
1554 ? IPSEC_MANUAL_REQID_MAX + 1 : auto_reqid + 1);
1556 /* XXX should be unique check */
1562 * copy secpolicy struct to sadb_x_policy structure indicated.
1566 struct secpolicy *sp;
1568 struct sadb_x_policy *xpl;
1575 panic("key_sp2msg: NULL pointer was passed.\n");
1577 tlen = key_getspreqmsglen(sp);
1579 m = key_alloc_mbuf(tlen);
1580 if (!m || m->m_next) { /*XXX*/
1588 xpl = mtod(m, struct sadb_x_policy *);
1591 xpl->sadb_x_policy_len = PFKEY_UNIT64(tlen);
1592 xpl->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
1593 xpl->sadb_x_policy_type = sp->policy;
1594 xpl->sadb_x_policy_dir = sp->spidx.dir;
1595 xpl->sadb_x_policy_id = sp->id;
1596 p = (caddr_t)xpl + sizeof(*xpl);
1598 /* if is the policy for ipsec ? */
1599 if (sp->policy == IPSEC_POLICY_IPSEC) {
1600 struct sadb_x_ipsecrequest *xisr;
1601 struct ipsecrequest *isr;
1603 for (isr = sp->req; isr != NULL; isr = isr->next) {
1605 xisr = (struct sadb_x_ipsecrequest *)p;
1607 xisr->sadb_x_ipsecrequest_proto = isr->saidx.proto;
1608 xisr->sadb_x_ipsecrequest_mode = isr->saidx.mode;
1609 xisr->sadb_x_ipsecrequest_level = isr->level;
1610 xisr->sadb_x_ipsecrequest_reqid = isr->saidx.reqid;
1613 bcopy(&isr->saidx.src, p, isr->saidx.src.ss_len);
1614 p += isr->saidx.src.ss_len;
1615 bcopy(&isr->saidx.dst, p, isr->saidx.dst.ss_len);
1616 p += isr->saidx.src.ss_len;
1618 xisr->sadb_x_ipsecrequest_len =
1619 PFKEY_ALIGN8(sizeof(*xisr)
1620 + isr->saidx.src.ss_len
1621 + isr->saidx.dst.ss_len);
1628 /* m will not be freed nor modified */
1629 static struct mbuf *
1630 key_gather_mbuf(struct mbuf *m, const struct sadb_msghdr *mhp,
1631 int ndeep, int nitem, ...)
1636 struct mbuf *result = NULL, *n;
1639 if (m == NULL || mhp == NULL)
1640 panic("null pointer passed to key_gather");
1642 __va_start(ap, nitem);
1643 for (i = 0; i < nitem; i++) {
1644 idx = __va_arg(ap, int);
1645 if (idx < 0 || idx > SADB_EXT_MAX)
1647 /* don't attempt to pull empty extension */
1648 if (idx == SADB_EXT_RESERVED && mhp->msg == NULL)
1650 if (idx != SADB_EXT_RESERVED &&
1651 (mhp->ext[idx] == NULL || mhp->extlen[idx] == 0))
1654 if (idx == SADB_EXT_RESERVED) {
1655 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
1658 panic("assumption failed");
1660 MGETHDR(n, MB_DONTWAIT, MT_DATA);
1665 m_copydata(m, 0, sizeof(struct sadb_msg),
1667 } else if (i < ndeep) {
1668 len = mhp->extlen[idx];
1669 n = key_alloc_mbuf(len);
1670 if (!n || n->m_next) { /*XXX*/
1675 m_copydata(m, mhp->extoff[idx], mhp->extlen[idx],
1678 n = m_copym(m, mhp->extoff[idx], mhp->extlen[idx],
1691 if ((result->m_flags & M_PKTHDR) != 0) {
1692 result->m_pkthdr.len = 0;
1693 for (n = result; n; n = n->m_next)
1694 result->m_pkthdr.len += n->m_len;
1705 * SADB_X_SPDADD, SADB_X_SPDSETIDX or SADB_X_SPDUPDATE processing
1706 * add a entry to SP database, when received
1707 * <base, address(SD), (lifetime(H),) policy>
1709 * Adding to SP database,
1711 * <base, address(SD), (lifetime(H),) policy>
1712 * to the socket which was send.
1714 * SPDADD set a unique policy entry.
1715 * SPDSETIDX like SPDADD without a part of policy requests.
1716 * SPDUPDATE replace a unique policy entry.
1718 * m will always be freed.
1721 key_spdadd(so, m, mhp)
1724 const struct sadb_msghdr *mhp;
1726 struct sadb_address *src0, *dst0;
1727 struct sadb_x_policy *xpl0, *xpl;
1728 struct sadb_lifetime *lft = NULL;
1729 struct secpolicyindex spidx;
1730 struct secpolicy *newsp;
1735 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
1736 panic("key_spdadd: NULL pointer is passed.\n");
1738 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
1739 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
1740 mhp->ext[SADB_X_EXT_POLICY] == NULL) {
1741 ipseclog((LOG_DEBUG, "key_spdadd: invalid message is passed.\n"));
1742 return key_senderror(so, m, EINVAL);
1744 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
1745 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
1746 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
1747 ipseclog((LOG_DEBUG, "key_spdadd: invalid message is passed.\n"));
1748 return key_senderror(so, m, EINVAL);
1750 if (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL) {
1751 if (mhp->extlen[SADB_EXT_LIFETIME_HARD]
1752 < sizeof(struct sadb_lifetime)) {
1753 ipseclog((LOG_DEBUG, "key_spdadd: invalid message is passed.\n"));
1754 return key_senderror(so, m, EINVAL);
1756 lft = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_HARD];
1759 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
1760 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
1761 xpl0 = (struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY];
1764 /* XXX boundary check against sa_len */
1765 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
1768 src0->sadb_address_prefixlen,
1769 dst0->sadb_address_prefixlen,
1770 src0->sadb_address_proto,
1773 /* checking the direciton. */
1774 switch (xpl0->sadb_x_policy_dir) {
1775 case IPSEC_DIR_INBOUND:
1776 case IPSEC_DIR_OUTBOUND:
1779 ipseclog((LOG_DEBUG, "key_spdadd: Invalid SP direction.\n"));
1780 mhp->msg->sadb_msg_errno = EINVAL;
1785 /* key_spdadd() accepts DISCARD, NONE and IPSEC. */
1786 if (xpl0->sadb_x_policy_type == IPSEC_POLICY_ENTRUST
1787 || xpl0->sadb_x_policy_type == IPSEC_POLICY_BYPASS) {
1788 ipseclog((LOG_DEBUG, "key_spdadd: Invalid policy type.\n"));
1789 return key_senderror(so, m, EINVAL);
1792 /* policy requests are mandatory when action is ipsec. */
1793 if (mhp->msg->sadb_msg_type != SADB_X_SPDSETIDX
1794 && xpl0->sadb_x_policy_type == IPSEC_POLICY_IPSEC
1795 && mhp->extlen[SADB_X_EXT_POLICY] <= sizeof(*xpl0)) {
1796 ipseclog((LOG_DEBUG, "key_spdadd: some policy requests part required.\n"));
1797 return key_senderror(so, m, EINVAL);
1801 * checking there is SP already or not.
1802 * SPDUPDATE doesn't depend on whether there is a SP or not.
1803 * If the type is either SPDADD or SPDSETIDX AND a SP is found,
1806 newsp = key_getsp(&spidx);
1807 if (mhp->msg->sadb_msg_type == SADB_X_SPDUPDATE) {
1809 newsp->state = IPSEC_SPSTATE_DEAD;
1813 if (newsp != NULL) {
1815 ipseclog((LOG_DEBUG, "key_spdadd: a SP entry exists already.\n"));
1816 return key_senderror(so, m, EEXIST);
1820 /* allocation new SP entry */
1821 if ((newsp = key_msg2sp(xpl0, PFKEY_EXTLEN(xpl0), &error)) == NULL) {
1822 return key_senderror(so, m, error);
1825 if ((newsp->id = key_getnewspid()) == 0) {
1826 keydb_delsecpolicy(newsp);
1827 return key_senderror(so, m, ENOBUFS);
1830 /* XXX boundary check against sa_len */
1831 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
1834 src0->sadb_address_prefixlen,
1835 dst0->sadb_address_prefixlen,
1836 src0->sadb_address_proto,
1839 /* sanity check on addr pair */
1840 if (((struct sockaddr *)(src0 + 1))->sa_family !=
1841 ((struct sockaddr *)(dst0+ 1))->sa_family) {
1842 keydb_delsecpolicy(newsp);
1843 return key_senderror(so, m, EINVAL);
1845 if (((struct sockaddr *)(src0 + 1))->sa_len !=
1846 ((struct sockaddr *)(dst0+ 1))->sa_len) {
1847 keydb_delsecpolicy(newsp);
1848 return key_senderror(so, m, EINVAL);
1851 if (newsp->req && newsp->req->saidx.src.ss_family) {
1852 struct sockaddr *sa;
1853 sa = (struct sockaddr *)(src0 + 1);
1854 if (sa->sa_family != newsp->req->saidx.src.ss_family) {
1855 keydb_delsecpolicy(newsp);
1856 return key_senderror(so, m, EINVAL);
1859 if (newsp->req && newsp->req->saidx.dst.ss_family) {
1860 struct sockaddr *sa;
1861 sa = (struct sockaddr *)(dst0 + 1);
1862 if (sa->sa_family != newsp->req->saidx.dst.ss_family) {
1863 keydb_delsecpolicy(newsp);
1864 return key_senderror(so, m, EINVAL);
1870 newsp->created = tv.tv_sec;
1871 newsp->lastused = tv.tv_sec;
1872 newsp->lifetime = lft ? lft->sadb_lifetime_addtime : 0;
1873 newsp->validtime = lft ? lft->sadb_lifetime_usetime : 0;
1875 newsp->refcnt = 1; /* do not reclaim until I say I do */
1876 newsp->state = IPSEC_SPSTATE_ALIVE;
1877 LIST_INSERT_TAIL(&sptree[newsp->spidx.dir], newsp, secpolicy, chain);
1879 /* delete the entry in spacqtree */
1880 if (mhp->msg->sadb_msg_type == SADB_X_SPDUPDATE) {
1881 struct secspacq *spacq;
1882 if ((spacq = key_getspacq(&spidx)) != NULL) {
1883 /* reset counter in order to deletion by timehandler. */
1885 spacq->created = tv.tv_sec;
1891 struct mbuf *n, *mpolicy;
1892 struct sadb_msg *newmsg;
1895 /* create new sadb_msg to reply. */
1897 n = key_gather_mbuf(m, mhp, 2, 5, SADB_EXT_RESERVED,
1898 SADB_X_EXT_POLICY, SADB_EXT_LIFETIME_HARD,
1899 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
1901 n = key_gather_mbuf(m, mhp, 2, 4, SADB_EXT_RESERVED,
1903 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
1906 return key_senderror(so, m, ENOBUFS);
1908 if (n->m_len < sizeof(*newmsg)) {
1909 n = m_pullup(n, sizeof(*newmsg));
1911 return key_senderror(so, m, ENOBUFS);
1913 newmsg = mtod(n, struct sadb_msg *);
1914 newmsg->sadb_msg_errno = 0;
1915 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
1918 mpolicy = m_pulldown(n, PFKEY_ALIGN8(sizeof(struct sadb_msg)),
1919 sizeof(*xpl), &off);
1920 if (mpolicy == NULL) {
1921 /* n is already freed */
1922 return key_senderror(so, m, ENOBUFS);
1924 xpl = (struct sadb_x_policy *)(mtod(mpolicy, caddr_t) + off);
1925 if (xpl->sadb_x_policy_exttype != SADB_X_EXT_POLICY) {
1927 return key_senderror(so, m, EINVAL);
1929 xpl->sadb_x_policy_id = newsp->id;
1932 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
1937 * get new policy id.
1945 u_int32_t newid = 0;
1946 int count = key_spi_trycnt; /* XXX */
1947 struct secpolicy *sp;
1949 /* when requesting to allocate spi ranged */
1951 newid = (policy_id = (policy_id == ~0 ? 1 : policy_id + 1));
1953 if ((sp = key_getspbyid(newid)) == NULL)
1959 if (count == 0 || newid == 0) {
1960 ipseclog((LOG_DEBUG, "key_getnewspid: to allocate policy id is failed.\n"));
1968 * SADB_SPDDELETE processing
1970 * <base, address(SD), policy(*)>
1971 * from the user(?), and set SADB_SASTATE_DEAD,
1973 * <base, address(SD), policy(*)>
1975 * policy(*) including direction of policy.
1977 * m will always be freed.
1980 key_spddelete(so, m, mhp)
1983 const struct sadb_msghdr *mhp;
1985 struct sadb_address *src0, *dst0;
1986 struct sadb_x_policy *xpl0;
1987 struct secpolicyindex spidx;
1988 struct secpolicy *sp;
1991 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
1992 panic("key_spddelete: NULL pointer is passed.\n");
1994 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
1995 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
1996 mhp->ext[SADB_X_EXT_POLICY] == NULL) {
1997 ipseclog((LOG_DEBUG, "key_spddelete: invalid message is passed.\n"));
1998 return key_senderror(so, m, EINVAL);
2000 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
2001 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
2002 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2003 ipseclog((LOG_DEBUG, "key_spddelete: invalid message is passed.\n"));
2004 return key_senderror(so, m, EINVAL);
2007 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
2008 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
2009 xpl0 = (struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY];
2012 /* XXX boundary check against sa_len */
2013 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
2016 src0->sadb_address_prefixlen,
2017 dst0->sadb_address_prefixlen,
2018 src0->sadb_address_proto,
2021 /* checking the direciton. */
2022 switch (xpl0->sadb_x_policy_dir) {
2023 case IPSEC_DIR_INBOUND:
2024 case IPSEC_DIR_OUTBOUND:
2027 ipseclog((LOG_DEBUG, "key_spddelete: Invalid SP direction.\n"));
2028 return key_senderror(so, m, EINVAL);
2031 /* Is there SP in SPD ? */
2032 if ((sp = key_getsp(&spidx)) == NULL) {
2033 ipseclog((LOG_DEBUG, "key_spddelete: no SP found.\n"));
2034 return key_senderror(so, m, EINVAL);
2037 /* save policy id to buffer to be returned. */
2038 xpl0->sadb_x_policy_id = sp->id;
2040 sp->state = IPSEC_SPSTATE_DEAD;
2045 struct sadb_msg *newmsg;
2047 /* create new sadb_msg to reply. */
2048 n = key_gather_mbuf(m, mhp, 1, 4, SADB_EXT_RESERVED,
2049 SADB_X_EXT_POLICY, SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
2051 return key_senderror(so, m, ENOBUFS);
2053 newmsg = mtod(n, struct sadb_msg *);
2054 newmsg->sadb_msg_errno = 0;
2055 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
2058 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
2063 * SADB_SPDDELETE2 processing
2066 * from the user(?), and set SADB_SASTATE_DEAD,
2070 * policy(*) including direction of policy.
2072 * m will always be freed.
2075 key_spddelete2(so, m, mhp)
2078 const struct sadb_msghdr *mhp;
2081 struct secpolicy *sp;
2084 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
2085 panic("key_spddelete2: NULL pointer is passed.\n");
2087 if (mhp->ext[SADB_X_EXT_POLICY] == NULL ||
2088 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2089 ipseclog((LOG_DEBUG, "key_spddelete2: invalid message is passed.\n"));
2090 key_senderror(so, m, EINVAL);
2094 id = ((struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY])->sadb_x_policy_id;
2096 /* Is there SP in SPD ? */
2097 if ((sp = key_getspbyid(id)) == NULL) {
2098 ipseclog((LOG_DEBUG, "key_spddelete2: no SP found id:%u.\n", id));
2099 key_senderror(so, m, EINVAL);
2102 sp->state = IPSEC_SPSTATE_DEAD;
2106 struct mbuf *n, *nn;
2107 struct sadb_msg *newmsg;
2110 /* create new sadb_msg to reply. */
2111 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
2114 return key_senderror(so, m, ENOBUFS);
2115 MGETHDR(n, MB_DONTWAIT, MT_DATA);
2116 if (n && len > MHLEN) {
2117 MCLGET(n, MB_DONTWAIT);
2118 if ((n->m_flags & M_EXT) == 0) {
2124 return key_senderror(so, m, ENOBUFS);
2130 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
2131 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
2135 panic("length inconsistency in key_spddelete2");
2138 n->m_next = m_copym(m, mhp->extoff[SADB_X_EXT_POLICY],
2139 mhp->extlen[SADB_X_EXT_POLICY], MB_DONTWAIT);
2142 return key_senderror(so, m, ENOBUFS);
2145 n->m_pkthdr.len = 0;
2146 for (nn = n; nn; nn = nn->m_next)
2147 n->m_pkthdr.len += nn->m_len;
2149 newmsg = mtod(n, struct sadb_msg *);
2150 newmsg->sadb_msg_errno = 0;
2151 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
2154 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
2159 * SADB_X_GET processing
2164 * <base, address(SD), policy>
2166 * policy(*) including direction of policy.
2168 * m will always be freed.
2171 key_spdget(so, m, mhp)
2174 const struct sadb_msghdr *mhp;
2177 struct secpolicy *sp;
2181 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
2182 panic("key_spdget: NULL pointer is passed.\n");
2184 if (mhp->ext[SADB_X_EXT_POLICY] == NULL ||
2185 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2186 ipseclog((LOG_DEBUG, "key_spdget: invalid message is passed.\n"));
2187 return key_senderror(so, m, EINVAL);
2190 id = ((struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY])->sadb_x_policy_id;
2192 /* Is there SP in SPD ? */
2193 if ((sp = key_getspbyid(id)) == NULL) {
2194 ipseclog((LOG_DEBUG, "key_spdget: no SP found id:%u.\n", id));
2195 return key_senderror(so, m, ENOENT);
2198 n = key_setdumpsp(sp, SADB_X_SPDGET, 0, mhp->msg->sadb_msg_pid);
2201 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
2203 return key_senderror(so, m, ENOBUFS);
2207 * SADB_X_SPDACQUIRE processing.
2208 * Acquire policy and SA(s) for a *OUTBOUND* packet.
2211 * to KMD, and expect to receive
2212 * <base> with SADB_X_SPDACQUIRE if error occured,
2215 * with SADB_X_SPDUPDATE from KMD by PF_KEY.
2216 * policy(*) is without policy requests.
2219 * others: error number
2223 struct secpolicy *sp;
2225 struct mbuf *result = NULL, *m;
2226 struct secspacq *newspacq;
2231 panic("key_spdacquire: NULL pointer is passed.\n");
2232 if (sp->req != NULL)
2233 panic("key_spdacquire: called but there is request.\n");
2234 if (sp->policy != IPSEC_POLICY_IPSEC)
2235 panic("key_spdacquire: policy mismathed. IPsec is expected.\n");
2237 /* get a entry to check whether sent message or not. */
2238 if ((newspacq = key_getspacq(&sp->spidx)) != NULL) {
2239 if (key_blockacq_count < newspacq->count) {
2240 /* reset counter and do send message. */
2241 newspacq->count = 0;
2243 /* increment counter and do nothing. */
2248 /* make new entry for blocking to send SADB_ACQUIRE. */
2249 if ((newspacq = key_newspacq(&sp->spidx)) == NULL)
2252 /* add to acqtree */
2253 LIST_INSERT_HEAD(&spacqtree, newspacq, chain);
2256 /* create new sadb_msg to reply. */
2257 m = key_setsadbmsg(SADB_X_SPDACQUIRE, 0, 0, 0, 0, 0);
2264 result->m_pkthdr.len = 0;
2265 for (m = result; m; m = m->m_next)
2266 result->m_pkthdr.len += m->m_len;
2268 mtod(result, struct sadb_msg *)->sadb_msg_len =
2269 PFKEY_UNIT64(result->m_pkthdr.len);
2271 return key_sendup_mbuf(NULL, m, KEY_SENDUP_REGISTERED);
2280 * SADB_SPDFLUSH processing
2283 * from the user, and free all entries in secpctree.
2287 * NOTE: what to do is only marking SADB_SASTATE_DEAD.
2289 * m will always be freed.
2292 key_spdflush(so, m, mhp)
2295 const struct sadb_msghdr *mhp;
2297 struct sadb_msg *newmsg;
2298 struct secpolicy *sp;
2302 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
2303 panic("key_spdflush: NULL pointer is passed.\n");
2305 if (m->m_len != PFKEY_ALIGN8(sizeof(struct sadb_msg)))
2306 return key_senderror(so, m, EINVAL);
2308 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2309 LIST_FOREACH(sp, &sptree[dir], chain) {
2310 sp->state = IPSEC_SPSTATE_DEAD;
2314 if (sizeof(struct sadb_msg) > m->m_len + M_TRAILINGSPACE(m)) {
2315 ipseclog((LOG_DEBUG, "key_spdflush: No more memory.\n"));
2316 return key_senderror(so, m, ENOBUFS);
2322 m->m_pkthdr.len = m->m_len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
2323 newmsg = mtod(m, struct sadb_msg *);
2324 newmsg->sadb_msg_errno = 0;
2325 newmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
2327 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
2331 * SADB_SPDDUMP processing
2334 * from the user, and dump all SP leaves
2339 * m will always be freed.
2342 key_spddump(so, m, mhp)
2345 const struct sadb_msghdr *mhp;
2347 struct secpolicy *sp;
2353 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
2354 panic("key_spddump: NULL pointer is passed.\n");
2356 /* search SPD entry and get buffer size. */
2358 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2359 LIST_FOREACH(sp, &sptree[dir], chain) {
2365 return key_senderror(so, m, ENOENT);
2367 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2368 LIST_FOREACH(sp, &sptree[dir], chain) {
2370 n = key_setdumpsp(sp, SADB_X_SPDDUMP, cnt,
2371 mhp->msg->sadb_msg_pid);
2374 key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
2382 static struct mbuf *
2383 key_setdumpsp(sp, type, seq, pid)
2384 struct secpolicy *sp;
2388 struct mbuf *result = NULL, *m;
2390 m = key_setsadbmsg(type, 0, SADB_SATYPE_UNSPEC, seq, pid, sp->refcnt);
2395 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
2396 (struct sockaddr *)&sp->spidx.src, sp->spidx.prefs,
2397 sp->spidx.ul_proto);
2402 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
2403 (struct sockaddr *)&sp->spidx.dst, sp->spidx.prefd,
2404 sp->spidx.ul_proto);
2414 if ((result->m_flags & M_PKTHDR) == 0)
2417 if (result->m_len < sizeof(struct sadb_msg)) {
2418 result = m_pullup(result, sizeof(struct sadb_msg));
2423 result->m_pkthdr.len = 0;
2424 for (m = result; m; m = m->m_next)
2425 result->m_pkthdr.len += m->m_len;
2427 mtod(result, struct sadb_msg *)->sadb_msg_len =
2428 PFKEY_UNIT64(result->m_pkthdr.len);
2438 * get PFKEY message length for security policy and request.
2441 key_getspreqmsglen(sp)
2442 struct secpolicy *sp;
2446 tlen = sizeof(struct sadb_x_policy);
2448 /* if is the policy for ipsec ? */
2449 if (sp->policy != IPSEC_POLICY_IPSEC)
2452 /* get length of ipsec requests */
2454 struct ipsecrequest *isr;
2457 for (isr = sp->req; isr != NULL; isr = isr->next) {
2458 len = sizeof(struct sadb_x_ipsecrequest)
2459 + isr->saidx.src.ss_len
2460 + isr->saidx.dst.ss_len;
2462 tlen += PFKEY_ALIGN8(len);
2470 * SADB_SPDEXPIRE processing
2472 * <base, address(SD), lifetime(CH), policy>
2476 * others : error number
2480 struct secpolicy *sp;
2483 struct mbuf *result = NULL, *m;
2486 struct sadb_lifetime *lt;
2488 /* XXX: Why do we lock ? */
2489 s = splnet(); /*called from softclock()*/
2493 panic("key_spdexpire: NULL pointer is passed.\n");
2495 /* set msg header */
2496 m = key_setsadbmsg(SADB_X_SPDEXPIRE, 0, 0, 0, 0, 0);
2503 /* create lifetime extension (current and hard) */
2504 len = PFKEY_ALIGN8(sizeof(*lt)) * 2;
2505 m = key_alloc_mbuf(len);
2506 if (!m || m->m_next) { /*XXX*/
2512 bzero(mtod(m, caddr_t), len);
2513 lt = mtod(m, struct sadb_lifetime *);
2514 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
2515 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
2516 lt->sadb_lifetime_allocations = 0;
2517 lt->sadb_lifetime_bytes = 0;
2518 lt->sadb_lifetime_addtime = sp->created;
2519 lt->sadb_lifetime_usetime = sp->lastused;
2520 lt = (struct sadb_lifetime *)(mtod(m, caddr_t) + len / 2);
2521 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
2522 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
2523 lt->sadb_lifetime_allocations = 0;
2524 lt->sadb_lifetime_bytes = 0;
2525 lt->sadb_lifetime_addtime = sp->lifetime;
2526 lt->sadb_lifetime_usetime = sp->validtime;
2529 /* set sadb_address for source */
2530 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
2531 (struct sockaddr *)&sp->spidx.src,
2532 sp->spidx.prefs, sp->spidx.ul_proto);
2539 /* set sadb_address for destination */
2540 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
2541 (struct sockaddr *)&sp->spidx.dst,
2542 sp->spidx.prefd, sp->spidx.ul_proto);
2557 if ((result->m_flags & M_PKTHDR) == 0) {
2562 if (result->m_len < sizeof(struct sadb_msg)) {
2563 result = m_pullup(result, sizeof(struct sadb_msg));
2564 if (result == NULL) {
2570 result->m_pkthdr.len = 0;
2571 for (m = result; m; m = m->m_next)
2572 result->m_pkthdr.len += m->m_len;
2574 mtod(result, struct sadb_msg *)->sadb_msg_len =
2575 PFKEY_UNIT64(result->m_pkthdr.len);
2577 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
2586 /* %%% SAD management */
2588 * allocating a memory for new SA head, and copy from the values of mhp.
2589 * OUT: NULL : failure due to the lack of memory.
2590 * others : pointer to new SA head.
2592 static struct secashead *
2594 struct secasindex *saidx;
2596 struct secashead *newsah;
2600 panic("key_newsaidx: NULL pointer is passed.\n");
2602 newsah = keydb_newsecashead();
2606 bcopy(saidx, &newsah->saidx, sizeof(newsah->saidx));
2608 /* add to saidxtree */
2609 newsah->state = SADB_SASTATE_MATURE;
2610 LIST_INSERT_HEAD(&sahtree, newsah, chain);
2616 * delete SA index and all SA registerd.
2620 struct secashead *sah;
2622 struct secasvar *sav, *nextsav;
2623 u_int stateidx, state;
2629 panic("key_delsah: NULL pointer is passed.\n");
2631 s = splnet(); /*called from softclock()*/
2633 /* searching all SA registerd in the secindex. */
2635 stateidx < _ARRAYLEN(saorder_state_any);
2638 state = saorder_state_any[stateidx];
2639 for (sav = (struct secasvar *)LIST_FIRST(&sah->savtree[state]);
2643 nextsav = LIST_NEXT(sav, chain);
2645 if (sav->refcnt > 0) {
2646 /* give up to delete this sa */
2652 KEY_CHKSASTATE(state, sav->state, "key_delsah");
2656 /* remove back pointer */
2662 /* don't delete sah only if there are savs. */
2668 if (sah->sa_route.ro_rt) {
2669 RTFREE(sah->sa_route.ro_rt);
2670 sah->sa_route.ro_rt = (struct rtentry *)NULL;
2673 /* remove from tree of SA index */
2674 if (__LIST_CHAINED(sah))
2675 LIST_REMOVE(sah, chain);
2684 * allocating a new SA with LARVAL state. key_add() and key_getspi() call,
2685 * and copy the values of mhp into new buffer.
2686 * When SAD message type is GETSPI:
2687 * to set sequence number from acq_seq++,
2688 * to set zero to SPI.
2689 * not to call key_setsava().
2691 * others : pointer to new secasvar.
2693 * does not modify mbuf. does not free mbuf on error.
2695 static struct secasvar *
2696 key_newsav(m, mhp, sah, errp)
2698 const struct sadb_msghdr *mhp;
2699 struct secashead *sah;
2702 struct secasvar *newsav;
2703 const struct sadb_sa *xsa;
2706 if (m == NULL || mhp == NULL || mhp->msg == NULL || sah == NULL)
2707 panic("key_newsa: NULL pointer is passed.\n");
2709 KMALLOC(newsav, struct secasvar *, sizeof(struct secasvar));
2710 if (newsav == NULL) {
2711 ipseclog((LOG_DEBUG, "key_newsa: No more memory.\n"));
2715 bzero((caddr_t)newsav, sizeof(struct secasvar));
2717 switch (mhp->msg->sadb_msg_type) {
2721 #ifdef IPSEC_DOSEQCHECK
2722 /* sync sequence number */
2723 if (mhp->msg->sadb_msg_seq == 0)
2725 (acq_seq = (acq_seq == ~0 ? 1 : ++acq_seq));
2728 newsav->seq = mhp->msg->sadb_msg_seq;
2733 if (mhp->ext[SADB_EXT_SA] == NULL) {
2735 ipseclog((LOG_DEBUG, "key_newsa: invalid message is passed.\n"));
2739 xsa = (const struct sadb_sa *)mhp->ext[SADB_EXT_SA];
2740 newsav->spi = xsa->sadb_sa_spi;
2741 newsav->seq = mhp->msg->sadb_msg_seq;
2749 /* copy sav values */
2750 if (mhp->msg->sadb_msg_type != SADB_GETSPI) {
2751 *errp = key_setsaval(newsav, m, mhp);
2762 newsav->created = tv.tv_sec;
2765 newsav->pid = mhp->msg->sadb_msg_pid;
2770 newsav->state = SADB_SASTATE_LARVAL;
2771 LIST_INSERT_TAIL(&sah->savtree[SADB_SASTATE_LARVAL], newsav,
2778 * free() SA variable entry.
2782 struct secasvar *sav;
2786 panic("key_delsav: NULL pointer is passed.\n");
2788 if (sav->refcnt > 0)
2789 return; /* can't free */
2791 /* remove from SA header */
2792 if (__LIST_CHAINED(sav))
2793 LIST_REMOVE(sav, chain);
2795 if (sav->key_auth != NULL) {
2796 bzero(_KEYBUF(sav->key_auth), _KEYLEN(sav->key_auth));
2797 KFREE(sav->key_auth);
2798 sav->key_auth = NULL;
2800 if (sav->key_enc != NULL) {
2801 bzero(_KEYBUF(sav->key_enc), _KEYLEN(sav->key_enc));
2802 KFREE(sav->key_enc);
2803 sav->key_enc = NULL;
2806 bzero(sav->sched, sav->schedlen);
2810 if (sav->replay != NULL) {
2811 keydb_delsecreplay(sav->replay);
2814 if (sav->lft_c != NULL) {
2818 if (sav->lft_h != NULL) {
2822 if (sav->lft_s != NULL) {
2826 if (sav->iv != NULL) {
2840 * others : found, pointer to a SA.
2842 static struct secashead *
2844 struct secasindex *saidx;
2846 struct secashead *sah;
2848 LIST_FOREACH(sah, &sahtree, chain) {
2849 if (sah->state == SADB_SASTATE_DEAD)
2851 if (key_cmpsaidx(&sah->saidx, saidx, CMP_REQID))
2859 * check not to be duplicated SPI.
2860 * NOTE: this function is too slow due to searching all SAD.
2863 * others : found, pointer to a SA.
2865 static struct secasvar *
2866 key_checkspidup(saidx, spi)
2867 struct secasindex *saidx;
2870 struct secashead *sah;
2871 struct secasvar *sav;
2873 /* check address family */
2874 if (saidx->src.ss_family != saidx->dst.ss_family) {
2875 ipseclog((LOG_DEBUG, "key_checkspidup: address family mismatched.\n"));
2880 LIST_FOREACH(sah, &sahtree, chain) {
2881 if (!key_ismyaddr((struct sockaddr *)&sah->saidx.dst))
2883 sav = key_getsavbyspi(sah, spi);
2892 * search SAD litmited alive SA, protocol, SPI.
2895 * others : found, pointer to a SA.
2897 static struct secasvar *
2898 key_getsavbyspi(sah, spi)
2899 struct secashead *sah;
2902 struct secasvar *sav;
2903 u_int stateidx, state;
2905 /* search all status */
2907 stateidx < _ARRAYLEN(saorder_state_alive);
2910 state = saorder_state_alive[stateidx];
2911 LIST_FOREACH(sav, &sah->savtree[state], chain) {
2914 if (sav->state != state) {
2915 ipseclog((LOG_DEBUG, "key_getsavbyspi: "
2916 "invalid sav->state (queue: %d SA: %d)\n",
2917 state, sav->state));
2921 if (sav->spi == spi)
2930 * copy SA values from PF_KEY message except *SPI, SEQ, PID, STATE and TYPE*.
2931 * You must update these if need.
2935 * does not modify mbuf. does not free mbuf on error.
2938 key_setsaval(sav, m, mhp)
2939 struct secasvar *sav;
2941 const struct sadb_msghdr *mhp;
2944 const struct esp_algorithm *algo;
2950 if (m == NULL || mhp == NULL || mhp->msg == NULL)
2951 panic("key_setsaval: NULL pointer is passed.\n");
2953 /* initialization */
2955 sav->key_auth = NULL;
2956 sav->key_enc = NULL;
2965 if (mhp->ext[SADB_EXT_SA] != NULL) {
2966 const struct sadb_sa *sa0;
2968 sa0 = (const struct sadb_sa *)mhp->ext[SADB_EXT_SA];
2969 if (mhp->extlen[SADB_EXT_SA] < sizeof(*sa0)) {
2974 sav->alg_auth = sa0->sadb_sa_auth;
2975 sav->alg_enc = sa0->sadb_sa_encrypt;
2976 sav->flags = sa0->sadb_sa_flags;
2979 if ((sa0->sadb_sa_flags & SADB_X_EXT_OLD) == 0) {
2980 sav->replay = keydb_newsecreplay(sa0->sadb_sa_replay);
2981 if (sav->replay == NULL) {
2982 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
2989 /* Authentication keys */
2990 if (mhp->ext[SADB_EXT_KEY_AUTH] != NULL) {
2991 const struct sadb_key *key0;
2994 key0 = (const struct sadb_key *)mhp->ext[SADB_EXT_KEY_AUTH];
2995 len = mhp->extlen[SADB_EXT_KEY_AUTH];
2998 if (len < sizeof(*key0)) {
3002 switch (mhp->msg->sadb_msg_satype) {
3003 case SADB_SATYPE_AH:
3004 case SADB_SATYPE_ESP:
3005 if (len == PFKEY_ALIGN8(sizeof(struct sadb_key)) &&
3006 sav->alg_auth != SADB_X_AALG_NULL)
3009 case SADB_X_SATYPE_IPCOMP:
3015 ipseclog((LOG_DEBUG, "key_setsaval: invalid key_auth values.\n"));
3019 sav->key_auth = (struct sadb_key *)key_newbuf(key0, len);
3020 if (sav->key_auth == NULL) {
3021 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
3027 /* Encryption key */
3028 if (mhp->ext[SADB_EXT_KEY_ENCRYPT] != NULL) {
3029 const struct sadb_key *key0;
3032 key0 = (const struct sadb_key *)mhp->ext[SADB_EXT_KEY_ENCRYPT];
3033 len = mhp->extlen[SADB_EXT_KEY_ENCRYPT];
3036 if (len < sizeof(*key0)) {
3040 switch (mhp->msg->sadb_msg_satype) {
3041 case SADB_SATYPE_ESP:
3042 if (len == PFKEY_ALIGN8(sizeof(struct sadb_key)) &&
3043 sav->alg_enc != SADB_EALG_NULL) {
3047 sav->key_enc = (struct sadb_key *)key_newbuf(key0, len);
3048 if (sav->key_enc == NULL) {
3049 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
3054 case SADB_X_SATYPE_IPCOMP:
3055 if (len != PFKEY_ALIGN8(sizeof(struct sadb_key)))
3057 sav->key_enc = NULL; /*just in case*/
3059 case SADB_SATYPE_AH:
3065 ipseclog((LOG_DEBUG, "key_setsatval: invalid key_enc value.\n"));
3073 switch (mhp->msg->sadb_msg_satype) {
3074 case SADB_SATYPE_ESP:
3076 algo = esp_algorithm_lookup(sav->alg_enc);
3077 if (algo && algo->ivlen)
3078 sav->ivlen = (*algo->ivlen)(algo, sav);
3079 if (sav->ivlen == 0)
3081 KMALLOC(sav->iv, caddr_t, sav->ivlen);
3083 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
3089 key_randomfill(sav->iv, sav->ivlen);
3092 case SADB_SATYPE_AH:
3093 case SADB_X_SATYPE_IPCOMP:
3096 ipseclog((LOG_DEBUG, "key_setsaval: invalid SA type.\n"));
3103 sav->created = tv.tv_sec;
3105 /* make lifetime for CURRENT */
3106 KMALLOC(sav->lft_c, struct sadb_lifetime *,
3107 sizeof(struct sadb_lifetime));
3108 if (sav->lft_c == NULL) {
3109 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
3116 sav->lft_c->sadb_lifetime_len =
3117 PFKEY_UNIT64(sizeof(struct sadb_lifetime));
3118 sav->lft_c->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
3119 sav->lft_c->sadb_lifetime_allocations = 0;
3120 sav->lft_c->sadb_lifetime_bytes = 0;
3121 sav->lft_c->sadb_lifetime_addtime = tv.tv_sec;
3122 sav->lft_c->sadb_lifetime_usetime = 0;
3124 /* lifetimes for HARD and SOFT */
3126 const struct sadb_lifetime *lft0;
3128 lft0 = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_HARD];
3130 if (mhp->extlen[SADB_EXT_LIFETIME_HARD] < sizeof(*lft0)) {
3134 sav->lft_h = (struct sadb_lifetime *)key_newbuf(lft0,
3136 if (sav->lft_h == NULL) {
3137 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
3141 /* to be initialize ? */
3144 lft0 = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_SOFT];
3146 if (mhp->extlen[SADB_EXT_LIFETIME_SOFT] < sizeof(*lft0)) {
3150 sav->lft_s = (struct sadb_lifetime *)key_newbuf(lft0,
3152 if (sav->lft_s == NULL) {
3153 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
3157 /* to be initialize ? */
3164 /* initialization */
3165 if (sav->replay != NULL) {
3166 keydb_delsecreplay(sav->replay);
3169 if (sav->key_auth != NULL) {
3170 KFREE(sav->key_auth);
3171 sav->key_auth = NULL;
3173 if (sav->key_enc != NULL) {
3174 KFREE(sav->key_enc);
3175 sav->key_enc = NULL;
3181 if (sav->iv != NULL) {
3185 if (sav->lft_c != NULL) {
3189 if (sav->lft_h != NULL) {
3193 if (sav->lft_s != NULL) {
3202 * validation with a secasvar entry, and set SADB_SATYPE_MATURE.
3208 struct secasvar *sav;
3211 int checkmask = 0; /* 2^0: ealg 2^1: aalg 2^2: calg */
3212 int mustmask = 0; /* 2^0: ealg 2^1: aalg 2^2: calg */
3216 /* check SPI value */
3217 switch (sav->sah->saidx.proto) {
3220 if (ntohl(sav->spi) >= 0 && ntohl(sav->spi) <= 255) {
3221 ipseclog((LOG_DEBUG,
3222 "key_mature: illegal range of SPI %u.\n",
3223 (u_int32_t)ntohl(sav->spi)));
3230 switch (sav->sah->saidx.proto) {
3233 if ((sav->flags & SADB_X_EXT_OLD)
3234 && (sav->flags & SADB_X_EXT_DERIV)) {
3235 ipseclog((LOG_DEBUG, "key_mature: "
3236 "invalid flag (derived) given to old-esp.\n"));
3239 if (sav->alg_auth == SADB_AALG_NONE)
3247 if (sav->flags & SADB_X_EXT_DERIV) {
3248 ipseclog((LOG_DEBUG, "key_mature: "
3249 "invalid flag (derived) given to AH SA.\n"));
3252 if (sav->alg_enc != SADB_EALG_NONE) {
3253 ipseclog((LOG_DEBUG, "key_mature: "
3254 "protocol and algorithm mismated.\n"));
3260 case IPPROTO_IPCOMP:
3261 if (sav->alg_auth != SADB_AALG_NONE) {
3262 ipseclog((LOG_DEBUG, "key_mature: "
3263 "protocol and algorithm mismated.\n"));
3266 if ((sav->flags & SADB_X_EXT_RAWCPI) == 0
3267 && ntohl(sav->spi) >= 0x10000) {
3268 ipseclog((LOG_DEBUG, "key_mature: invalid cpi for IPComp.\n"));
3275 ipseclog((LOG_DEBUG, "key_mature: Invalid satype.\n"));
3276 return EPROTONOSUPPORT;
3279 /* check authentication algorithm */
3280 if ((checkmask & 2) != 0) {
3281 const struct ah_algorithm *algo;
3284 algo = ah_algorithm_lookup(sav->alg_auth);
3286 ipseclog((LOG_DEBUG,"key_mature: "
3287 "unknown authentication algorithm.\n"));
3291 /* algorithm-dependent check */
3293 keylen = sav->key_auth->sadb_key_bits;
3296 if (keylen < algo->keymin || algo->keymax < keylen) {
3297 ipseclog((LOG_DEBUG,
3298 "key_mature: invalid AH key length %d "
3299 "(%d-%d allowed)\n",
3300 keylen, algo->keymin, algo->keymax));
3305 if ((*algo->mature)(sav)) {
3306 /* message generated in per-algorithm function*/
3309 mature = SADB_SATYPE_AH;
3312 if ((mustmask & 2) != 0 && mature != SADB_SATYPE_AH) {
3313 ipseclog((LOG_DEBUG, "key_mature: no satisfy algorithm for AH\n"));
3318 /* check encryption algorithm */
3319 if ((checkmask & 1) != 0) {
3321 const struct esp_algorithm *algo;
3324 algo = esp_algorithm_lookup(sav->alg_enc);
3326 ipseclog((LOG_DEBUG, "key_mature: unknown encryption algorithm.\n"));
3330 /* algorithm-dependent check */
3332 keylen = sav->key_enc->sadb_key_bits;
3335 if (keylen < algo->keymin || algo->keymax < keylen) {
3336 ipseclog((LOG_DEBUG,
3337 "key_mature: invalid ESP key length %d "
3338 "(%d-%d allowed)\n",
3339 keylen, algo->keymin, algo->keymax));
3344 if ((*algo->mature)(sav)) {
3345 /* message generated in per-algorithm function*/
3348 mature = SADB_SATYPE_ESP;
3351 if ((mustmask & 1) != 0 && mature != SADB_SATYPE_ESP) {
3352 ipseclog((LOG_DEBUG, "key_mature: no satisfy algorithm for ESP\n"));
3356 ipseclog((LOG_DEBUG, "key_mature: ESP not supported in this configuration\n"));
3361 /* check compression algorithm */
3362 if ((checkmask & 4) != 0) {
3363 const struct ipcomp_algorithm *algo;
3365 /* algorithm-dependent check */
3366 algo = ipcomp_algorithm_lookup(sav->alg_enc);
3368 ipseclog((LOG_DEBUG, "key_mature: unknown compression algorithm.\n"));
3373 key_sa_chgstate(sav, SADB_SASTATE_MATURE);
3379 * subroutine for SADB_GET and SADB_DUMP.
3381 static struct mbuf *
3382 key_setdumpsa(sav, type, satype, seq, pid)
3383 struct secasvar *sav;
3384 u_int8_t type, satype;
3387 struct mbuf *result = NULL, *tres = NULL, *m;
3392 SADB_EXT_SA, SADB_X_EXT_SA2,
3393 SADB_EXT_LIFETIME_HARD, SADB_EXT_LIFETIME_SOFT,
3394 SADB_EXT_LIFETIME_CURRENT, SADB_EXT_ADDRESS_SRC,
3395 SADB_EXT_ADDRESS_DST, SADB_EXT_ADDRESS_PROXY, SADB_EXT_KEY_AUTH,
3396 SADB_EXT_KEY_ENCRYPT, SADB_EXT_IDENTITY_SRC,
3397 SADB_EXT_IDENTITY_DST, SADB_EXT_SENSITIVITY,
3400 m = key_setsadbmsg(type, 0, satype, seq, pid, sav->refcnt);
3405 for (i = sizeof(dumporder)/sizeof(dumporder[0]) - 1; i >= 0; i--) {
3408 switch (dumporder[i]) {
3410 m = key_setsadbsa(sav);
3415 case SADB_X_EXT_SA2:
3416 m = key_setsadbxsa2(sav->sah->saidx.mode,
3417 sav->replay ? sav->replay->count : 0,
3418 sav->sah->saidx.reqid);
3423 case SADB_EXT_ADDRESS_SRC:
3424 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
3425 (struct sockaddr *)&sav->sah->saidx.src,
3426 FULLMASK, IPSEC_ULPROTO_ANY);
3431 case SADB_EXT_ADDRESS_DST:
3432 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
3433 (struct sockaddr *)&sav->sah->saidx.dst,
3434 FULLMASK, IPSEC_ULPROTO_ANY);
3439 case SADB_EXT_KEY_AUTH:
3442 l = PFKEY_UNUNIT64(sav->key_auth->sadb_key_len);
3446 case SADB_EXT_KEY_ENCRYPT:
3449 l = PFKEY_UNUNIT64(sav->key_enc->sadb_key_len);
3453 case SADB_EXT_LIFETIME_CURRENT:
3456 l = PFKEY_UNUNIT64(((struct sadb_ext *)sav->lft_c)->sadb_ext_len);
3460 case SADB_EXT_LIFETIME_HARD:
3463 l = PFKEY_UNUNIT64(((struct sadb_ext *)sav->lft_h)->sadb_ext_len);
3467 case SADB_EXT_LIFETIME_SOFT:
3470 l = PFKEY_UNUNIT64(((struct sadb_ext *)sav->lft_s)->sadb_ext_len);
3474 case SADB_EXT_ADDRESS_PROXY:
3475 case SADB_EXT_IDENTITY_SRC:
3476 case SADB_EXT_IDENTITY_DST:
3477 /* XXX: should we brought from SPD ? */
3478 case SADB_EXT_SENSITIVITY:
3483 if ((!m && !p) || (m && p))
3486 M_PREPEND(tres, l, MB_DONTWAIT);
3489 bcopy(p, mtod(tres, caddr_t), l);
3493 m = key_alloc_mbuf(l);
3496 m_copyback(m, 0, l, p);
3504 m_cat(result, tres);
3506 if (result->m_len < sizeof(struct sadb_msg)) {
3507 result = m_pullup(result, sizeof(struct sadb_msg));
3512 result->m_pkthdr.len = 0;
3513 for (m = result; m; m = m->m_next)
3514 result->m_pkthdr.len += m->m_len;
3516 mtod(result, struct sadb_msg *)->sadb_msg_len =
3517 PFKEY_UNIT64(result->m_pkthdr.len);
3528 * set data into sadb_msg.
3530 static struct mbuf *
3531 key_setsadbmsg(type, tlen, satype, seq, pid, reserved)
3532 u_int8_t type, satype;
3542 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
3545 MGETHDR(m, MB_DONTWAIT, MT_DATA);
3546 if (m && len > MHLEN) {
3547 MCLGET(m, MB_DONTWAIT);
3548 if ((m->m_flags & M_EXT) == 0) {
3555 m->m_pkthdr.len = m->m_len = len;
3558 p = mtod(m, struct sadb_msg *);
3561 p->sadb_msg_version = PF_KEY_V2;
3562 p->sadb_msg_type = type;
3563 p->sadb_msg_errno = 0;
3564 p->sadb_msg_satype = satype;
3565 p->sadb_msg_len = PFKEY_UNIT64(tlen);
3566 p->sadb_msg_reserved = reserved;
3567 p->sadb_msg_seq = seq;
3568 p->sadb_msg_pid = (u_int32_t)pid;
3574 * copy secasvar data into sadb_address.
3576 static struct mbuf *
3578 struct secasvar *sav;
3584 len = PFKEY_ALIGN8(sizeof(struct sadb_sa));
3585 m = key_alloc_mbuf(len);
3586 if (!m || m->m_next) { /*XXX*/
3592 p = mtod(m, struct sadb_sa *);
3595 p->sadb_sa_len = PFKEY_UNIT64(len);
3596 p->sadb_sa_exttype = SADB_EXT_SA;
3597 p->sadb_sa_spi = sav->spi;
3598 p->sadb_sa_replay = (sav->replay != NULL ? sav->replay->wsize : 0);
3599 p->sadb_sa_state = sav->state;
3600 p->sadb_sa_auth = sav->alg_auth;
3601 p->sadb_sa_encrypt = sav->alg_enc;
3602 p->sadb_sa_flags = sav->flags;
3608 * set data into sadb_address.
3610 static struct mbuf *
3611 key_setsadbaddr(exttype, saddr, prefixlen, ul_proto)
3613 struct sockaddr *saddr;
3618 struct sadb_address *p;
3621 len = PFKEY_ALIGN8(sizeof(struct sadb_address)) +
3622 PFKEY_ALIGN8(saddr->sa_len);
3623 m = key_alloc_mbuf(len);
3624 if (!m || m->m_next) { /*XXX*/
3630 p = mtod(m, struct sadb_address *);
3633 p->sadb_address_len = PFKEY_UNIT64(len);
3634 p->sadb_address_exttype = exttype;
3635 p->sadb_address_proto = ul_proto;
3636 if (prefixlen == FULLMASK) {
3637 switch (saddr->sa_family) {
3639 prefixlen = sizeof(struct in_addr) << 3;
3642 prefixlen = sizeof(struct in6_addr) << 3;
3648 p->sadb_address_prefixlen = prefixlen;
3649 p->sadb_address_reserved = 0;
3652 mtod(m, caddr_t) + PFKEY_ALIGN8(sizeof(struct sadb_address)),
3660 * set data into sadb_ident.
3662 static struct mbuf *
3663 key_setsadbident(exttype, idtype, string, stringlen, id)
3664 u_int16_t exttype, idtype;
3670 struct sadb_ident *p;
3673 len = PFKEY_ALIGN8(sizeof(struct sadb_ident)) + PFKEY_ALIGN8(stringlen);
3674 m = key_alloc_mbuf(len);
3675 if (!m || m->m_next) { /*XXX*/
3681 p = mtod(m, struct sadb_ident *);
3684 p->sadb_ident_len = PFKEY_UNIT64(len);
3685 p->sadb_ident_exttype = exttype;
3686 p->sadb_ident_type = idtype;
3687 p->sadb_ident_reserved = 0;
3688 p->sadb_ident_id = id;
3691 mtod(m, caddr_t) + PFKEY_ALIGN8(sizeof(struct sadb_ident)),
3699 * set data into sadb_x_sa2.
3701 static struct mbuf *
3702 key_setsadbxsa2(mode, seq, reqid)
3704 u_int32_t seq, reqid;
3707 struct sadb_x_sa2 *p;
3710 len = PFKEY_ALIGN8(sizeof(struct sadb_x_sa2));
3711 m = key_alloc_mbuf(len);
3712 if (!m || m->m_next) { /*XXX*/
3718 p = mtod(m, struct sadb_x_sa2 *);
3721 p->sadb_x_sa2_len = PFKEY_UNIT64(len);
3722 p->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
3723 p->sadb_x_sa2_mode = mode;
3724 p->sadb_x_sa2_reserved1 = 0;
3725 p->sadb_x_sa2_reserved2 = 0;
3726 p->sadb_x_sa2_sequence = seq;
3727 p->sadb_x_sa2_reqid = reqid;
3733 * set data into sadb_x_policy
3735 static struct mbuf *
3736 key_setsadbxpolicy(type, dir, id)
3742 struct sadb_x_policy *p;
3745 len = PFKEY_ALIGN8(sizeof(struct sadb_x_policy));
3746 m = key_alloc_mbuf(len);
3747 if (!m || m->m_next) { /*XXX*/
3753 p = mtod(m, struct sadb_x_policy *);
3756 p->sadb_x_policy_len = PFKEY_UNIT64(len);
3757 p->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3758 p->sadb_x_policy_type = type;
3759 p->sadb_x_policy_dir = dir;
3760 p->sadb_x_policy_id = id;
3767 * copy a buffer into the new buffer allocated.
3770 key_newbuf(src, len)
3776 KMALLOC(new, caddr_t, len);
3778 ipseclog((LOG_DEBUG, "key_newbuf: No more memory.\n"));
3781 bcopy(src, new, len);
3786 /* compare my own address
3787 * OUT: 1: true, i.e. my address.
3792 struct sockaddr *sa;
3795 struct sockaddr_in *sin;
3796 struct in_ifaddr *ia;
3801 panic("key_ismyaddr: NULL pointer is passed.\n");
3803 switch (sa->sa_family) {
3806 sin = (struct sockaddr_in *)sa;
3807 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
3808 if (sin->sin_family == ia->ia_addr.sin_family &&
3809 sin->sin_len == ia->ia_addr.sin_len &&
3810 sin->sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr)
3819 return key_ismyaddr6((struct sockaddr_in6 *)sa);
3828 * compare my own address for IPv6.
3831 * NOTE: derived ip6_input() in KAME. This is necessary to modify more.
3833 #include <netinet6/in6_var.h>
3837 struct sockaddr_in6 *sin6;
3839 struct in6_ifaddr *ia;
3840 struct in6_multi *in6m;
3842 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
3843 if (key_sockaddrcmp((struct sockaddr *)&sin6,
3844 (struct sockaddr *)&ia->ia_addr, 0) == 0)
3849 * XXX why do we care about multlicast here while we don't care
3850 * about IPv4 multicast??
3854 IN6_LOOKUP_MULTI(sin6->sin6_addr, ia->ia_ifp, in6m);
3859 /* loopback, just for safety */
3860 if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
3868 * compare two secasindex structure.
3869 * flag can specify to compare 2 saidxes.
3870 * compare two secasindex structure without both mode and reqid.
3871 * don't compare port.
3873 * saidx0: source, it can be in SAD.
3880 key_cmpsaidx(saidx0, saidx1, flag)
3881 struct secasindex *saidx0, *saidx1;
3885 if (saidx0 == NULL && saidx1 == NULL)
3888 if (saidx0 == NULL || saidx1 == NULL)
3891 if (saidx0->proto != saidx1->proto)
3894 if (flag == CMP_EXACTLY) {
3895 if (saidx0->mode != saidx1->mode)
3897 if (saidx0->reqid != saidx1->reqid)
3899 if (bcmp(&saidx0->src, &saidx1->src, saidx0->src.ss_len) != 0 ||
3900 bcmp(&saidx0->dst, &saidx1->dst, saidx0->dst.ss_len) != 0)
3904 /* CMP_MODE_REQID, CMP_REQID, CMP_HEAD */
3905 if (flag == CMP_MODE_REQID
3906 ||flag == CMP_REQID) {
3908 * If reqid of SPD is non-zero, unique SA is required.
3909 * The result must be of same reqid in this case.
3911 if (saidx1->reqid != 0 && saidx0->reqid != saidx1->reqid)
3915 if (flag == CMP_MODE_REQID) {
3916 if (saidx0->mode != IPSEC_MODE_ANY
3917 && saidx0->mode != saidx1->mode)
3921 if (key_sockaddrcmp((struct sockaddr *)&saidx0->src,
3922 (struct sockaddr *)&saidx1->src, 0) != 0) {
3925 if (key_sockaddrcmp((struct sockaddr *)&saidx0->dst,
3926 (struct sockaddr *)&saidx1->dst, 0) != 0) {
3935 * compare two secindex structure exactly.
3937 * spidx0: source, it is often in SPD.
3938 * spidx1: object, it is often from PFKEY message.
3944 key_cmpspidx_exactly(spidx0, spidx1)
3945 struct secpolicyindex *spidx0, *spidx1;
3948 if (spidx0 == NULL && spidx1 == NULL)
3951 if (spidx0 == NULL || spidx1 == NULL)
3954 if (spidx0->prefs != spidx1->prefs
3955 || spidx0->prefd != spidx1->prefd
3956 || spidx0->ul_proto != spidx1->ul_proto)
3959 if (key_sockaddrcmp((struct sockaddr *)&spidx0->src,
3960 (struct sockaddr *)&spidx1->src, 1) != 0) {
3963 if (key_sockaddrcmp((struct sockaddr *)&spidx0->dst,
3964 (struct sockaddr *)&spidx1->dst, 1) != 0) {
3972 * compare two secindex structure with mask.
3974 * spidx0: source, it is often in SPD.
3975 * spidx1: object, it is often from IP header.
3981 key_cmpspidx_withmask(spidx0, spidx1)
3982 struct secpolicyindex *spidx0, *spidx1;
3985 if (spidx0 == NULL && spidx1 == NULL)
3988 if (spidx0 == NULL || spidx1 == NULL)
3991 if (spidx0->src.ss_family != spidx1->src.ss_family ||
3992 spidx0->dst.ss_family != spidx1->dst.ss_family ||
3993 spidx0->src.ss_len != spidx1->src.ss_len ||
3994 spidx0->dst.ss_len != spidx1->dst.ss_len)
3997 /* if spidx.ul_proto == IPSEC_ULPROTO_ANY, ignore. */
3998 if (spidx0->ul_proto != (u_int16_t)IPSEC_ULPROTO_ANY
3999 && spidx0->ul_proto != spidx1->ul_proto)
4002 switch (spidx0->src.ss_family) {
4004 if (satosin(&spidx0->src)->sin_port != IPSEC_PORT_ANY
4005 && satosin(&spidx0->src)->sin_port !=
4006 satosin(&spidx1->src)->sin_port)
4008 if (!key_bbcmp((caddr_t)&satosin(&spidx0->src)->sin_addr,
4009 (caddr_t)&satosin(&spidx1->src)->sin_addr, spidx0->prefs))
4013 if (satosin6(&spidx0->src)->sin6_port != IPSEC_PORT_ANY
4014 && satosin6(&spidx0->src)->sin6_port !=
4015 satosin6(&spidx1->src)->sin6_port)
4018 * scope_id check. if sin6_scope_id is 0, we regard it
4019 * as a wildcard scope, which matches any scope zone ID.
4021 if (satosin6(&spidx0->src)->sin6_scope_id &&
4022 satosin6(&spidx1->src)->sin6_scope_id &&
4023 satosin6(&spidx0->src)->sin6_scope_id !=
4024 satosin6(&spidx1->src)->sin6_scope_id)
4026 if (!key_bbcmp((caddr_t)&satosin6(&spidx0->src)->sin6_addr,
4027 (caddr_t)&satosin6(&spidx1->src)->sin6_addr, spidx0->prefs))
4032 if (bcmp(&spidx0->src, &spidx1->src, spidx0->src.ss_len) != 0)
4037 switch (spidx0->dst.ss_family) {
4039 if (satosin(&spidx0->dst)->sin_port != IPSEC_PORT_ANY
4040 && satosin(&spidx0->dst)->sin_port !=
4041 satosin(&spidx1->dst)->sin_port)
4043 if (!key_bbcmp((caddr_t)&satosin(&spidx0->dst)->sin_addr,
4044 (caddr_t)&satosin(&spidx1->dst)->sin_addr, spidx0->prefd))
4048 if (satosin6(&spidx0->dst)->sin6_port != IPSEC_PORT_ANY
4049 && satosin6(&spidx0->dst)->sin6_port !=
4050 satosin6(&spidx1->dst)->sin6_port)
4053 * scope_id check. if sin6_scope_id is 0, we regard it
4054 * as a wildcard scope, which matches any scope zone ID.
4056 if (satosin6(&spidx0->src)->sin6_scope_id &&
4057 satosin6(&spidx1->src)->sin6_scope_id &&
4058 satosin6(&spidx0->dst)->sin6_scope_id !=
4059 satosin6(&spidx1->dst)->sin6_scope_id)
4061 if (!key_bbcmp((caddr_t)&satosin6(&spidx0->dst)->sin6_addr,
4062 (caddr_t)&satosin6(&spidx1->dst)->sin6_addr, spidx0->prefd))
4067 if (bcmp(&spidx0->dst, &spidx1->dst, spidx0->dst.ss_len) != 0)
4072 /* XXX Do we check other field ? e.g. flowinfo */
4077 /* returns 0 on match */
4079 key_sockaddrcmp(sa1, sa2, port)
4080 struct sockaddr *sa1;
4081 struct sockaddr *sa2;
4084 if (sa1->sa_family != sa2->sa_family || sa1->sa_len != sa2->sa_len)
4087 switch (sa1->sa_family) {
4089 if (sa1->sa_len != sizeof(struct sockaddr_in))
4091 if (satosin(sa1)->sin_addr.s_addr !=
4092 satosin(sa2)->sin_addr.s_addr) {
4095 if (port && satosin(sa1)->sin_port != satosin(sa2)->sin_port)
4099 if (sa1->sa_len != sizeof(struct sockaddr_in6))
4100 return 1; /*EINVAL*/
4101 if (satosin6(sa1)->sin6_scope_id !=
4102 satosin6(sa2)->sin6_scope_id) {
4105 if (!IN6_ARE_ADDR_EQUAL(&satosin6(sa1)->sin6_addr,
4106 &satosin6(sa2)->sin6_addr)) {
4110 satosin6(sa1)->sin6_port != satosin6(sa2)->sin6_port) {
4114 if (bcmp(sa1, sa2, sa1->sa_len) != 0)
4123 * compare two buffers with mask.
4127 * bits: Number of bits to compare
4133 key_bbcmp(p1, p2, bits)
4139 /* XXX: This could be considerably faster if we compare a word
4140 * at a time, but it is complicated on LSB Endian machines */
4142 /* Handle null pointers */
4143 if (p1 == NULL || p2 == NULL)
4153 mask = ~((1<<(8-bits))-1);
4154 if ((*p1 & mask) != (*p2 & mask))
4157 return 1; /* Match! */
4162 * scanning SPD and SAD to check status for each entries,
4163 * and do to remove or to expire.
4164 * XXX: year 2038 problem may remain.
4167 key_timehandler(void *__dummy)
4175 s = splnet(); /*called from softclock()*/
4179 struct secpolicy *sp, *nextsp;
4181 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
4182 for (sp = LIST_FIRST(&sptree[dir]);
4186 nextsp = LIST_NEXT(sp, chain);
4188 if (sp->state == IPSEC_SPSTATE_DEAD) {
4193 if (sp->lifetime == 0 && sp->validtime == 0)
4196 /* the deletion will occur next time */
4198 && tv.tv_sec - sp->created > sp->lifetime)
4200 && tv.tv_sec - sp->lastused > sp->validtime)) {
4201 sp->state = IPSEC_SPSTATE_DEAD;
4211 struct secashead *sah, *nextsah;
4212 struct secasvar *sav, *nextsav;
4214 for (sah = LIST_FIRST(&sahtree);
4218 nextsah = LIST_NEXT(sah, chain);
4220 /* if sah has been dead, then delete it and process next sah. */
4221 if (sah->state == SADB_SASTATE_DEAD) {
4226 /* if LARVAL entry doesn't become MATURE, delete it. */
4227 for (sav = LIST_FIRST(&sah->savtree[SADB_SASTATE_LARVAL]);
4231 nextsav = LIST_NEXT(sav, chain);
4233 if (tv.tv_sec - sav->created > key_larval_lifetime) {
4239 * check MATURE entry to start to send expire message
4242 for (sav = LIST_FIRST(&sah->savtree[SADB_SASTATE_MATURE]);
4246 nextsav = LIST_NEXT(sav, chain);
4248 /* we don't need to check. */
4249 if (sav->lft_s == NULL)
4253 if (sav->lft_c == NULL) {
4254 ipseclog((LOG_DEBUG,"key_timehandler: "
4255 "There is no CURRENT time, why?\n"));
4259 /* check SOFT lifetime */
4260 if (sav->lft_s->sadb_lifetime_addtime != 0
4261 && tv.tv_sec - sav->created > sav->lft_s->sadb_lifetime_addtime) {
4263 * check the SA if it has been used.
4264 * when it hasn't been used, delete it.
4265 * i don't think such SA will be used.
4267 if (sav->lft_c->sadb_lifetime_usetime == 0) {
4268 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4272 key_sa_chgstate(sav, SADB_SASTATE_DYING);
4274 * XXX If we keep to send expire
4275 * message in the status of
4276 * DYING. Do remove below code.
4282 /* check SOFT lifetime by bytes */
4284 * XXX I don't know the way to delete this SA
4285 * when new SA is installed. Caution when it's
4286 * installed too big lifetime by time.
4288 else if (sav->lft_s->sadb_lifetime_bytes != 0
4289 && sav->lft_s->sadb_lifetime_bytes < sav->lft_c->sadb_lifetime_bytes) {
4291 key_sa_chgstate(sav, SADB_SASTATE_DYING);
4293 * XXX If we keep to send expire
4294 * message in the status of
4295 * DYING. Do remove below code.
4301 /* check DYING entry to change status to DEAD. */
4302 for (sav = LIST_FIRST(&sah->savtree[SADB_SASTATE_DYING]);
4306 nextsav = LIST_NEXT(sav, chain);
4308 /* we don't need to check. */
4309 if (sav->lft_h == NULL)
4313 if (sav->lft_c == NULL) {
4314 ipseclog((LOG_DEBUG, "key_timehandler: "
4315 "There is no CURRENT time, why?\n"));
4319 if (sav->lft_h->sadb_lifetime_addtime != 0
4320 && tv.tv_sec - sav->created > sav->lft_h->sadb_lifetime_addtime) {
4321 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4325 #if 0 /* XXX Should we keep to send expire message until HARD lifetime ? */
4326 else if (sav->lft_s != NULL
4327 && sav->lft_s->sadb_lifetime_addtime != 0
4328 && tv.tv_sec - sav->created > sav->lft_s->sadb_lifetime_addtime) {
4330 * XXX: should be checked to be
4331 * installed the valid SA.
4335 * If there is no SA then sending
4341 /* check HARD lifetime by bytes */
4342 else if (sav->lft_h->sadb_lifetime_bytes != 0
4343 && sav->lft_h->sadb_lifetime_bytes < sav->lft_c->sadb_lifetime_bytes) {
4344 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4350 /* delete entry in DEAD */
4351 for (sav = LIST_FIRST(&sah->savtree[SADB_SASTATE_DEAD]);
4355 nextsav = LIST_NEXT(sav, chain);
4358 if (sav->state != SADB_SASTATE_DEAD) {
4359 ipseclog((LOG_DEBUG, "key_timehandler: "
4360 "invalid sav->state "
4361 "(queue: %d SA: %d): "
4363 SADB_SASTATE_DEAD, sav->state));
4367 * do not call key_freesav() here.
4368 * sav should already be freed, and sav->refcnt
4369 * shows other references to sav
4370 * (such as from SPD).
4376 #ifndef IPSEC_NONBLOCK_ACQUIRE
4379 struct secacq *acq, *nextacq;
4381 for (acq = LIST_FIRST(&acqtree);
4385 nextacq = LIST_NEXT(acq, chain);
4387 if (tv.tv_sec - acq->created > key_blockacq_lifetime
4388 && __LIST_CHAINED(acq)) {
4389 LIST_REMOVE(acq, chain);
4398 struct secspacq *acq, *nextacq;
4400 for (acq = LIST_FIRST(&spacqtree);
4404 nextacq = LIST_NEXT(acq, chain);
4406 if (tv.tv_sec - acq->created > key_blockacq_lifetime
4407 && __LIST_CHAINED(acq)) {
4408 LIST_REMOVE(acq, chain);
4414 /* initialize random seed */
4415 if (key_tick_init_random++ > key_int_random) {
4416 key_tick_init_random = 0;
4420 #ifndef IPSEC_DEBUG2
4421 /* do exchange to tick time !! */
4422 callout_reset(&key_timehandler_ch, hz, key_timehandler, NULL);
4423 #endif /* IPSEC_DEBUG2 */
4430 * to initialize a seed for random()
4439 srandom(tv.tv_usec);
4449 key_randomfill(&value, sizeof(value));
4454 key_randomfill(p, l)
4460 static int warn = 1;
4463 n = (size_t)read_random(p, (u_int)l);
4467 bcopy(&v, (u_int8_t *)p + n,
4468 l - n < sizeof(v) ? l - n : sizeof(v));
4472 printf("WARNING: pseudo-random number generator "
4473 "used for IPsec processing\n");
4480 * map SADB_SATYPE_* to IPPROTO_*.
4481 * if satype == SADB_SATYPE then satype is mapped to ~0.
4483 * 0: invalid satype.
4486 key_satype2proto(satype)
4490 case SADB_SATYPE_UNSPEC:
4491 return IPSEC_PROTO_ANY;
4492 case SADB_SATYPE_AH:
4494 case SADB_SATYPE_ESP:
4496 case SADB_X_SATYPE_IPCOMP:
4497 return IPPROTO_IPCOMP;
4506 * map IPPROTO_* to SADB_SATYPE_*
4508 * 0: invalid protocol type.
4511 key_proto2satype(proto)
4516 return SADB_SATYPE_AH;
4518 return SADB_SATYPE_ESP;
4519 case IPPROTO_IPCOMP:
4520 return SADB_X_SATYPE_IPCOMP;
4530 * SADB_GETSPI processing is to receive
4531 * <base, (SA2), src address, dst address, (SPI range)>
4532 * from the IKMPd, to assign a unique spi value, to hang on the INBOUND
4533 * tree with the status of LARVAL, and send
4534 * <base, SA(*), address(SD)>
4537 * IN: mhp: pointer to the pointer to each header.
4538 * OUT: NULL if fail.
4539 * other if success, return pointer to the message to send.
4542 key_getspi(so, m, mhp)
4545 const struct sadb_msghdr *mhp;
4547 struct sadb_address *src0, *dst0;
4548 struct secasindex saidx;
4549 struct secashead *newsah;
4550 struct secasvar *newsav;
4558 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
4559 panic("key_getspi: NULL pointer is passed.\n");
4561 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
4562 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
4563 ipseclog((LOG_DEBUG, "key_getspi: invalid message is passed.\n"));
4564 return key_senderror(so, m, EINVAL);
4566 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
4567 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
4568 ipseclog((LOG_DEBUG, "key_getspi: invalid message is passed.\n"));
4569 return key_senderror(so, m, EINVAL);
4571 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
4572 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
4573 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
4575 mode = IPSEC_MODE_ANY;
4579 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
4580 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
4582 /* map satype to proto */
4583 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
4584 ipseclog((LOG_DEBUG, "key_getspi: invalid satype is passed.\n"));
4585 return key_senderror(so, m, EINVAL);
4588 /* make sure if port number is zero. */
4589 switch (((struct sockaddr *)(src0 + 1))->sa_family) {
4591 if (((struct sockaddr *)(src0 + 1))->sa_len !=
4592 sizeof(struct sockaddr_in))
4593 return key_senderror(so, m, EINVAL);
4594 ((struct sockaddr_in *)(src0 + 1))->sin_port = 0;
4597 if (((struct sockaddr *)(src0 + 1))->sa_len !=
4598 sizeof(struct sockaddr_in6))
4599 return key_senderror(so, m, EINVAL);
4600 ((struct sockaddr_in6 *)(src0 + 1))->sin6_port = 0;
4605 switch (((struct sockaddr *)(dst0 + 1))->sa_family) {
4607 if (((struct sockaddr *)(dst0 + 1))->sa_len !=
4608 sizeof(struct sockaddr_in))
4609 return key_senderror(so, m, EINVAL);
4610 ((struct sockaddr_in *)(dst0 + 1))->sin_port = 0;
4613 if (((struct sockaddr *)(dst0 + 1))->sa_len !=
4614 sizeof(struct sockaddr_in6))
4615 return key_senderror(so, m, EINVAL);
4616 ((struct sockaddr_in6 *)(dst0 + 1))->sin6_port = 0;
4622 /* XXX boundary check against sa_len */
4623 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
4625 /* SPI allocation */
4626 spi = key_do_getnewspi((struct sadb_spirange *)mhp->ext[SADB_EXT_SPIRANGE],
4629 return key_senderror(so, m, EINVAL);
4631 /* get a SA index */
4632 if ((newsah = key_getsah(&saidx)) == NULL) {
4633 /* create a new SA index */
4634 if ((newsah = key_newsah(&saidx)) == NULL) {
4635 ipseclog((LOG_DEBUG, "key_getspi: No more memory.\n"));
4636 return key_senderror(so, m, ENOBUFS);
4642 newsav = key_newsav(m, mhp, newsah, &error);
4643 if (newsav == NULL) {
4644 /* XXX don't free new SA index allocated in above. */
4645 return key_senderror(so, m, error);
4649 newsav->spi = htonl(spi);
4651 #ifndef IPSEC_NONBLOCK_ACQUIRE
4652 /* delete the entry in acqtree */
4653 if (mhp->msg->sadb_msg_seq != 0) {
4655 if ((acq = key_getacqbyseq(mhp->msg->sadb_msg_seq)) != NULL) {
4656 /* reset counter in order to deletion by timehandler. */
4659 acq->created = tv.tv_sec;
4666 struct mbuf *n, *nn;
4667 struct sadb_sa *m_sa;
4668 struct sadb_msg *newmsg;
4671 /* create new sadb_msg to reply. */
4672 len = PFKEY_ALIGN8(sizeof(struct sadb_msg)) +
4673 PFKEY_ALIGN8(sizeof(struct sadb_sa));
4675 return key_senderror(so, m, ENOBUFS);
4677 MGETHDR(n, MB_DONTWAIT, MT_DATA);
4679 MCLGET(n, MB_DONTWAIT);
4680 if ((n->m_flags & M_EXT) == 0) {
4686 return key_senderror(so, m, ENOBUFS);
4692 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
4693 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
4695 m_sa = (struct sadb_sa *)(mtod(n, caddr_t) + off);
4696 m_sa->sadb_sa_len = PFKEY_UNIT64(sizeof(struct sadb_sa));
4697 m_sa->sadb_sa_exttype = SADB_EXT_SA;
4698 m_sa->sadb_sa_spi = htonl(spi);
4699 off += PFKEY_ALIGN8(sizeof(struct sadb_sa));
4703 panic("length inconsistency in key_getspi");
4706 n->m_next = key_gather_mbuf(m, mhp, 0, 2, SADB_EXT_ADDRESS_SRC,
4707 SADB_EXT_ADDRESS_DST);
4710 return key_senderror(so, m, ENOBUFS);
4713 if (n->m_len < sizeof(struct sadb_msg)) {
4714 n = m_pullup(n, sizeof(struct sadb_msg));
4716 return key_sendup_mbuf(so, m, KEY_SENDUP_ONE);
4719 n->m_pkthdr.len = 0;
4720 for (nn = n; nn; nn = nn->m_next)
4721 n->m_pkthdr.len += nn->m_len;
4723 newmsg = mtod(n, struct sadb_msg *);
4724 newmsg->sadb_msg_seq = newsav->seq;
4725 newmsg->sadb_msg_errno = 0;
4726 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
4729 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
4734 * allocating new SPI
4735 * called by key_getspi().
4741 key_do_getnewspi(spirange, saidx)
4742 struct sadb_spirange *spirange;
4743 struct secasindex *saidx;
4747 int count = key_spi_trycnt;
4749 /* set spi range to allocate */
4750 if (spirange != NULL) {
4751 min = spirange->sadb_spirange_min;
4752 max = spirange->sadb_spirange_max;
4754 min = key_spi_minval;
4755 max = key_spi_maxval;
4757 /* IPCOMP needs 2-byte SPI */
4758 if (saidx->proto == IPPROTO_IPCOMP) {
4765 t = min; min = max; max = t;
4770 if (key_checkspidup(saidx, min) != NULL) {
4771 ipseclog((LOG_DEBUG, "key_do_getnewspi: SPI %u exists already.\n", min));
4775 count--; /* taking one cost. */
4783 /* when requesting to allocate spi ranged */
4785 /* generate pseudo-random SPI value ranged. */
4786 newspi = min + (key_random() % (max - min + 1));
4788 if (key_checkspidup(saidx, newspi) == NULL)
4792 if (count == 0 || newspi == 0) {
4793 ipseclog((LOG_DEBUG, "key_do_getnewspi: to allocate spi is failed.\n"));
4799 keystat.getspi_count =
4800 (keystat.getspi_count + key_spi_trycnt - count) / 2;
4806 * SADB_UPDATE processing
4808 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4809 * key(AE), (identity(SD),) (sensitivity)>
4810 * from the ikmpd, and update a secasvar entry whose status is SADB_SASTATE_LARVAL.
4812 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4813 * (identity(SD),) (sensitivity)>
4816 * m will always be freed.
4819 key_update(so, m, mhp)
4822 const struct sadb_msghdr *mhp;
4824 struct sadb_sa *sa0;
4825 struct sadb_address *src0, *dst0;
4826 struct secasindex saidx;
4827 struct secashead *sah;
4828 struct secasvar *sav;
4835 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
4836 panic("key_update: NULL pointer is passed.\n");
4838 /* map satype to proto */
4839 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
4840 ipseclog((LOG_DEBUG, "key_update: invalid satype is passed.\n"));
4841 return key_senderror(so, m, EINVAL);
4844 if (mhp->ext[SADB_EXT_SA] == NULL ||
4845 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
4846 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
4847 (mhp->msg->sadb_msg_satype == SADB_SATYPE_ESP &&
4848 mhp->ext[SADB_EXT_KEY_ENCRYPT] == NULL) ||
4849 (mhp->msg->sadb_msg_satype == SADB_SATYPE_AH &&
4850 mhp->ext[SADB_EXT_KEY_AUTH] == NULL) ||
4851 (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL &&
4852 mhp->ext[SADB_EXT_LIFETIME_SOFT] == NULL) ||
4853 (mhp->ext[SADB_EXT_LIFETIME_HARD] == NULL &&
4854 mhp->ext[SADB_EXT_LIFETIME_SOFT] != NULL)) {
4855 ipseclog((LOG_DEBUG, "key_update: invalid message is passed.\n"));
4856 return key_senderror(so, m, EINVAL);
4858 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
4859 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
4860 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
4861 ipseclog((LOG_DEBUG, "key_update: invalid message is passed.\n"));
4862 return key_senderror(so, m, EINVAL);
4864 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
4865 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
4866 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
4868 mode = IPSEC_MODE_ANY;
4871 /* XXX boundary checking for other extensions */
4873 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
4874 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
4875 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
4877 /* XXX boundary check against sa_len */
4878 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
4880 /* get a SA header */
4881 if ((sah = key_getsah(&saidx)) == NULL) {
4882 ipseclog((LOG_DEBUG, "key_update: no SA index found.\n"));
4883 return key_senderror(so, m, ENOENT);
4886 /* set spidx if there */
4888 error = key_setident(sah, m, mhp);
4890 return key_senderror(so, m, error);
4892 /* find a SA with sequence number. */
4893 #ifdef IPSEC_DOSEQCHECK
4894 if (mhp->msg->sadb_msg_seq != 0
4895 && (sav = key_getsavbyseq(sah, mhp->msg->sadb_msg_seq)) == NULL) {
4896 ipseclog((LOG_DEBUG,
4897 "key_update: no larval SA with sequence %u exists.\n",
4898 mhp->msg->sadb_msg_seq));
4899 return key_senderror(so, m, ENOENT);
4902 if ((sav = key_getsavbyspi(sah, sa0->sadb_sa_spi)) == NULL) {
4903 ipseclog((LOG_DEBUG,
4904 "key_update: no such a SA found (spi:%u)\n",
4905 (u_int32_t)ntohl(sa0->sadb_sa_spi)));
4906 return key_senderror(so, m, EINVAL);
4910 /* validity check */
4911 if (sav->sah->saidx.proto != proto) {
4912 ipseclog((LOG_DEBUG,
4913 "key_update: protocol mismatched (DB=%u param=%u)\n",
4914 sav->sah->saidx.proto, proto));
4915 return key_senderror(so, m, EINVAL);
4917 #ifdef IPSEC_DOSEQCHECK
4918 if (sav->spi != sa0->sadb_sa_spi) {
4919 ipseclog((LOG_DEBUG,
4920 "key_update: SPI mismatched (DB:%u param:%u)\n",
4921 (u_int32_t)ntohl(sav->spi),
4922 (u_int32_t)ntohl(sa0->sadb_sa_spi)));
4923 return key_senderror(so, m, EINVAL);
4926 if (sav->pid != mhp->msg->sadb_msg_pid) {
4927 ipseclog((LOG_DEBUG,
4928 "key_update: pid mismatched (DB:%u param:%u)\n",
4929 sav->pid, mhp->msg->sadb_msg_pid));
4930 return key_senderror(so, m, EINVAL);
4933 /* copy sav values */
4934 error = key_setsaval(sav, m, mhp);
4937 return key_senderror(so, m, error);
4940 /* check SA values to be mature. */
4941 if ((mhp->msg->sadb_msg_errno = key_mature(sav)) != 0) {
4943 return key_senderror(so, m, 0);
4949 /* set msg buf from mhp */
4950 n = key_getmsgbuf_x1(m, mhp);
4952 ipseclog((LOG_DEBUG, "key_update: No more memory.\n"));
4953 return key_senderror(so, m, ENOBUFS);
4957 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
4962 * search SAD with sequence for a SA which state is SADB_SASTATE_LARVAL.
4963 * only called by key_update().
4966 * others : found, pointer to a SA.
4968 #ifdef IPSEC_DOSEQCHECK
4969 static struct secasvar *
4970 key_getsavbyseq(sah, seq)
4971 struct secashead *sah;
4974 struct secasvar *sav;
4977 state = SADB_SASTATE_LARVAL;
4979 /* search SAD with sequence number ? */
4980 LIST_FOREACH(sav, &sah->savtree[state], chain) {
4982 KEY_CHKSASTATE(state, sav->state, "key_getsabyseq");
4984 if (sav->seq == seq) {
4986 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
4987 printf("DP key_getsavbyseq cause "
4988 "refcnt++:%d SA:%p\n",
4999 * SADB_ADD processing
5000 * add a entry to SA database, when received
5001 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
5002 * key(AE), (identity(SD),) (sensitivity)>
5005 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
5006 * (identity(SD),) (sensitivity)>
5009 * IGNORE identity and sensitivity messages.
5011 * m will always be freed.
5017 const struct sadb_msghdr *mhp;
5019 struct sadb_sa *sa0;
5020 struct sadb_address *src0, *dst0;
5021 struct secasindex saidx;
5022 struct secashead *newsah;
5023 struct secasvar *newsav;
5030 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
5031 panic("key_add: NULL pointer is passed.\n");
5033 /* map satype to proto */
5034 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5035 ipseclog((LOG_DEBUG, "key_add: invalid satype is passed.\n"));
5036 return key_senderror(so, m, EINVAL);
5039 if (mhp->ext[SADB_EXT_SA] == NULL ||
5040 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5041 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
5042 (mhp->msg->sadb_msg_satype == SADB_SATYPE_ESP &&
5043 mhp->ext[SADB_EXT_KEY_ENCRYPT] == NULL) ||
5044 (mhp->msg->sadb_msg_satype == SADB_SATYPE_AH &&
5045 mhp->ext[SADB_EXT_KEY_AUTH] == NULL) ||
5046 (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL &&
5047 mhp->ext[SADB_EXT_LIFETIME_SOFT] == NULL) ||
5048 (mhp->ext[SADB_EXT_LIFETIME_HARD] == NULL &&
5049 mhp->ext[SADB_EXT_LIFETIME_SOFT] != NULL)) {
5050 ipseclog((LOG_DEBUG, "key_add: invalid message is passed.\n"));
5051 return key_senderror(so, m, EINVAL);
5053 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
5054 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5055 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5057 ipseclog((LOG_DEBUG, "key_add: invalid message is passed.\n"));
5058 return key_senderror(so, m, EINVAL);
5060 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
5061 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
5062 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
5064 mode = IPSEC_MODE_ANY;
5068 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5069 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
5070 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
5072 /* XXX boundary check against sa_len */
5073 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
5075 /* get a SA header */
5076 if ((newsah = key_getsah(&saidx)) == NULL) {
5077 /* create a new SA header */
5078 if ((newsah = key_newsah(&saidx)) == NULL) {
5079 ipseclog((LOG_DEBUG, "key_add: No more memory.\n"));
5080 return key_senderror(so, m, ENOBUFS);
5084 /* set spidx if there */
5086 error = key_setident(newsah, m, mhp);
5088 return key_senderror(so, m, error);
5091 /* create new SA entry. */
5092 /* We can create new SA only if SPI is differenct. */
5093 if (key_getsavbyspi(newsah, sa0->sadb_sa_spi)) {
5094 ipseclog((LOG_DEBUG, "key_add: SA already exists.\n"));
5095 return key_senderror(so, m, EEXIST);
5097 newsav = key_newsav(m, mhp, newsah, &error);
5098 if (newsav == NULL) {
5099 return key_senderror(so, m, error);
5102 /* check SA values to be mature. */
5103 if ((error = key_mature(newsav)) != 0) {
5104 key_freesav(newsav);
5105 return key_senderror(so, m, error);
5109 * don't call key_freesav() here, as we would like to keep the SA
5110 * in the database on success.
5116 /* set msg buf from mhp */
5117 n = key_getmsgbuf_x1(m, mhp);
5119 ipseclog((LOG_DEBUG, "key_update: No more memory.\n"));
5120 return key_senderror(so, m, ENOBUFS);
5124 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5130 key_setident(sah, m, mhp)
5131 struct secashead *sah;
5133 const struct sadb_msghdr *mhp;
5135 const struct sadb_ident *idsrc, *iddst;
5136 int idsrclen, iddstlen;
5139 if (sah == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
5140 panic("key_setident: NULL pointer is passed.\n");
5142 /* don't make buffer if not there */
5143 if (mhp->ext[SADB_EXT_IDENTITY_SRC] == NULL &&
5144 mhp->ext[SADB_EXT_IDENTITY_DST] == NULL) {
5150 if (mhp->ext[SADB_EXT_IDENTITY_SRC] == NULL ||
5151 mhp->ext[SADB_EXT_IDENTITY_DST] == NULL) {
5152 ipseclog((LOG_DEBUG, "key_setident: invalid identity.\n"));
5156 idsrc = (const struct sadb_ident *)mhp->ext[SADB_EXT_IDENTITY_SRC];
5157 iddst = (const struct sadb_ident *)mhp->ext[SADB_EXT_IDENTITY_DST];
5158 idsrclen = mhp->extlen[SADB_EXT_IDENTITY_SRC];
5159 iddstlen = mhp->extlen[SADB_EXT_IDENTITY_DST];
5161 /* validity check */
5162 if (idsrc->sadb_ident_type != iddst->sadb_ident_type) {
5163 ipseclog((LOG_DEBUG, "key_setident: ident type mismatch.\n"));
5167 switch (idsrc->sadb_ident_type) {
5168 case SADB_IDENTTYPE_PREFIX:
5169 case SADB_IDENTTYPE_FQDN:
5170 case SADB_IDENTTYPE_USERFQDN:
5172 /* XXX do nothing */
5178 /* make structure */
5179 KMALLOC(sah->idents, struct sadb_ident *, idsrclen);
5180 if (sah->idents == NULL) {
5181 ipseclog((LOG_DEBUG, "key_setident: No more memory.\n"));
5184 KMALLOC(sah->identd, struct sadb_ident *, iddstlen);
5185 if (sah->identd == NULL) {
5188 ipseclog((LOG_DEBUG, "key_setident: No more memory.\n"));
5191 bcopy(idsrc, sah->idents, idsrclen);
5192 bcopy(iddst, sah->identd, iddstlen);
5198 * m will not be freed on return.
5199 * it is caller's responsibility to free the result.
5201 static struct mbuf *
5202 key_getmsgbuf_x1(m, mhp)
5204 const struct sadb_msghdr *mhp;
5209 if (m == NULL || mhp == NULL || mhp->msg == NULL)
5210 panic("key_getmsgbuf_x1: NULL pointer is passed.\n");
5212 /* create new sadb_msg to reply. */
5213 n = key_gather_mbuf(m, mhp, 1, 9, SADB_EXT_RESERVED,
5214 SADB_EXT_SA, SADB_X_EXT_SA2,
5215 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST,
5216 SADB_EXT_LIFETIME_HARD, SADB_EXT_LIFETIME_SOFT,
5217 SADB_EXT_IDENTITY_SRC, SADB_EXT_IDENTITY_DST);
5221 if (n->m_len < sizeof(struct sadb_msg)) {
5222 n = m_pullup(n, sizeof(struct sadb_msg));
5226 mtod(n, struct sadb_msg *)->sadb_msg_errno = 0;
5227 mtod(n, struct sadb_msg *)->sadb_msg_len =
5228 PFKEY_UNIT64(n->m_pkthdr.len);
5233 static int key_delete_all (struct socket *, struct mbuf *,
5234 const struct sadb_msghdr *, u_int16_t);
5237 * SADB_DELETE processing
5239 * <base, SA(*), address(SD)>
5240 * from the ikmpd, and set SADB_SASTATE_DEAD,
5242 * <base, SA(*), address(SD)>
5245 * m will always be freed.
5248 key_delete(so, m, mhp)
5251 const struct sadb_msghdr *mhp;
5253 struct sadb_sa *sa0;
5254 struct sadb_address *src0, *dst0;
5255 struct secasindex saidx;
5256 struct secashead *sah;
5257 struct secasvar *sav = NULL;
5261 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
5262 panic("key_delete: NULL pointer is passed.\n");
5264 /* map satype to proto */
5265 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5266 ipseclog((LOG_DEBUG, "key_delete: invalid satype is passed.\n"));
5267 return key_senderror(so, m, EINVAL);
5270 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5271 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
5272 ipseclog((LOG_DEBUG, "key_delete: invalid message is passed.\n"));
5273 return key_senderror(so, m, EINVAL);
5276 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5277 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5278 ipseclog((LOG_DEBUG, "key_delete: invalid message is passed.\n"));
5279 return key_senderror(so, m, EINVAL);
5282 if (mhp->ext[SADB_EXT_SA] == NULL) {
5284 * Caller wants us to delete all non-LARVAL SAs
5285 * that match the src/dst. This is used during
5286 * IKE INITIAL-CONTACT.
5288 ipseclog((LOG_DEBUG, "key_delete: doing delete all.\n"));
5289 return key_delete_all(so, m, mhp, proto);
5290 } else if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa)) {
5291 ipseclog((LOG_DEBUG, "key_delete: invalid message is passed.\n"));
5292 return key_senderror(so, m, EINVAL);
5295 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5296 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
5297 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
5299 /* XXX boundary check against sa_len */
5300 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5302 /* get a SA header */
5303 LIST_FOREACH(sah, &sahtree, chain) {
5304 if (sah->state == SADB_SASTATE_DEAD)
5306 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5309 /* get a SA with SPI. */
5310 sav = key_getsavbyspi(sah, sa0->sadb_sa_spi);
5315 ipseclog((LOG_DEBUG, "key_delete: no SA found.\n"));
5316 return key_senderror(so, m, ENOENT);
5319 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
5325 struct sadb_msg *newmsg;
5327 /* create new sadb_msg to reply. */
5328 n = key_gather_mbuf(m, mhp, 1, 4, SADB_EXT_RESERVED,
5329 SADB_EXT_SA, SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
5331 return key_senderror(so, m, ENOBUFS);
5333 if (n->m_len < sizeof(struct sadb_msg)) {
5334 n = m_pullup(n, sizeof(struct sadb_msg));
5336 return key_senderror(so, m, ENOBUFS);
5338 newmsg = mtod(n, struct sadb_msg *);
5339 newmsg->sadb_msg_errno = 0;
5340 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
5343 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5348 * delete all SAs for src/dst. Called from key_delete().
5351 key_delete_all(so, m, mhp, proto)
5354 const struct sadb_msghdr *mhp;
5357 struct sadb_address *src0, *dst0;
5358 struct secasindex saidx;
5359 struct secashead *sah;
5360 struct secasvar *sav, *nextsav;
5361 u_int stateidx, state;
5363 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
5364 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
5366 /* XXX boundary check against sa_len */
5367 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5369 LIST_FOREACH(sah, &sahtree, chain) {
5370 if (sah->state == SADB_SASTATE_DEAD)
5372 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5375 /* Delete all non-LARVAL SAs. */
5377 stateidx < _ARRAYLEN(saorder_state_alive);
5379 state = saorder_state_alive[stateidx];
5380 if (state == SADB_SASTATE_LARVAL)
5382 for (sav = LIST_FIRST(&sah->savtree[state]);
5383 sav != NULL; sav = nextsav) {
5384 nextsav = LIST_NEXT(sav, chain);
5386 if (sav->state != state) {
5387 ipseclog((LOG_DEBUG, "key_delete_all: "
5388 "invalid sav->state "
5389 "(queue: %d SA: %d)\n",
5390 state, sav->state));
5394 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
5401 struct sadb_msg *newmsg;
5403 /* create new sadb_msg to reply. */
5404 n = key_gather_mbuf(m, mhp, 1, 3, SADB_EXT_RESERVED,
5405 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
5407 return key_senderror(so, m, ENOBUFS);
5409 if (n->m_len < sizeof(struct sadb_msg)) {
5410 n = m_pullup(n, sizeof(struct sadb_msg));
5412 return key_senderror(so, m, ENOBUFS);
5414 newmsg = mtod(n, struct sadb_msg *);
5415 newmsg->sadb_msg_errno = 0;
5416 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
5419 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5424 * SADB_GET processing
5426 * <base, SA(*), address(SD)>
5427 * from the ikmpd, and get a SP and a SA to respond,
5429 * <base, SA, (lifetime(HSC),) address(SD), (address(P),) key(AE),
5430 * (identity(SD),) (sensitivity)>
5433 * m will always be freed.
5439 const struct sadb_msghdr *mhp;
5441 struct sadb_sa *sa0;
5442 struct sadb_address *src0, *dst0;
5443 struct secasindex saidx;
5444 struct secashead *sah;
5445 struct secasvar *sav = NULL;
5449 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
5450 panic("key_get: NULL pointer is passed.\n");
5452 /* map satype to proto */
5453 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5454 ipseclog((LOG_DEBUG, "key_get: invalid satype is passed.\n"));
5455 return key_senderror(so, m, EINVAL);
5458 if (mhp->ext[SADB_EXT_SA] == NULL ||
5459 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5460 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
5461 ipseclog((LOG_DEBUG, "key_get: invalid message is passed.\n"));
5462 return key_senderror(so, m, EINVAL);
5464 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
5465 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5466 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5467 ipseclog((LOG_DEBUG, "key_get: invalid message is passed.\n"));
5468 return key_senderror(so, m, EINVAL);
5471 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5472 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
5473 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
5475 /* XXX boundary check against sa_len */
5476 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5478 /* get a SA header */
5479 LIST_FOREACH(sah, &sahtree, chain) {
5480 if (sah->state == SADB_SASTATE_DEAD)
5482 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5485 /* get a SA with SPI. */
5486 sav = key_getsavbyspi(sah, sa0->sadb_sa_spi);
5491 ipseclog((LOG_DEBUG, "key_get: no SA found.\n"));
5492 return key_senderror(so, m, ENOENT);
5499 /* map proto to satype */
5500 if ((satype = key_proto2satype(sah->saidx.proto)) == 0) {
5501 ipseclog((LOG_DEBUG, "key_get: there was invalid proto in SAD.\n"));
5502 return key_senderror(so, m, EINVAL);
5505 /* create new sadb_msg to reply. */
5506 n = key_setdumpsa(sav, SADB_GET, satype, mhp->msg->sadb_msg_seq,
5507 mhp->msg->sadb_msg_pid);
5509 return key_senderror(so, m, ENOBUFS);
5512 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
5516 /* XXX make it sysctl-configurable? */
5518 key_getcomb_setlifetime(comb)
5519 struct sadb_comb *comb;
5522 comb->sadb_comb_soft_allocations = 1;
5523 comb->sadb_comb_hard_allocations = 1;
5524 comb->sadb_comb_soft_bytes = 0;
5525 comb->sadb_comb_hard_bytes = 0;
5526 comb->sadb_comb_hard_addtime = 86400; /* 1 day */
5527 comb->sadb_comb_soft_addtime = comb->sadb_comb_soft_addtime * 80 / 100;
5528 comb->sadb_comb_soft_usetime = 28800; /* 8 hours */
5529 comb->sadb_comb_hard_usetime = comb->sadb_comb_hard_usetime * 80 / 100;
5534 * XXX reorder combinations by preference
5535 * XXX no idea if the user wants ESP authentication or not
5537 static struct mbuf *
5540 struct sadb_comb *comb;
5541 const struct esp_algorithm *algo;
5542 struct mbuf *result = NULL, *m, *n;
5546 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
5549 for (i = 1; i <= SADB_EALG_MAX; i++) {
5550 algo = esp_algorithm_lookup(i);
5554 if (algo->keymax < ipsec_esp_keymin)
5556 if (algo->keymin < ipsec_esp_keymin)
5557 encmin = ipsec_esp_keymin;
5559 encmin = algo->keymin;
5562 m = key_getcomb_ah();
5566 panic("assumption failed in key_getcomb_esp");
5568 MGET(m, MB_DONTWAIT, MT_DATA);
5573 bzero(mtod(m, caddr_t), m->m_len);
5580 for (n = m; n; n = n->m_next)
5584 panic("assumption failed in key_getcomb_esp");
5587 for (off = 0; off < totlen; off += l) {
5588 n = m_pulldown(m, off, l, &o);
5590 /* m is already freed */
5593 comb = (struct sadb_comb *)(mtod(n, caddr_t) + o);
5594 bzero(comb, sizeof(*comb));
5595 key_getcomb_setlifetime(comb);
5596 comb->sadb_comb_encrypt = i;
5597 comb->sadb_comb_encrypt_minbits = encmin;
5598 comb->sadb_comb_encrypt_maxbits = algo->keymax;
5617 * XXX reorder combinations by preference
5619 static struct mbuf *
5622 struct sadb_comb *comb;
5623 const struct ah_algorithm *algo;
5627 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
5630 for (i = 1; i <= SADB_AALG_MAX; i++) {
5632 /* we prefer HMAC algorithms, not old algorithms */
5633 if (i != SADB_AALG_SHA1HMAC && i != SADB_AALG_MD5HMAC)
5636 algo = ah_algorithm_lookup(i);
5640 if (algo->keymax < ipsec_ah_keymin)
5642 if (algo->keymin < ipsec_ah_keymin)
5643 min = ipsec_ah_keymin;
5650 panic("assumption failed in key_getcomb_ah");
5652 MGET(m, MB_DONTWAIT, MT_DATA);
5659 M_PREPEND(m, l, MB_DONTWAIT);
5663 comb = mtod(m, struct sadb_comb *);
5664 bzero(comb, sizeof(*comb));
5665 key_getcomb_setlifetime(comb);
5666 comb->sadb_comb_auth = i;
5667 comb->sadb_comb_auth_minbits = min;
5668 comb->sadb_comb_auth_maxbits = algo->keymax;
5675 * not really an official behavior. discussed in pf_key@inner.net in Sep2000.
5676 * XXX reorder combinations by preference
5678 static struct mbuf *
5679 key_getcomb_ipcomp()
5681 struct sadb_comb *comb;
5682 const struct ipcomp_algorithm *algo;
5685 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
5688 for (i = 1; i <= SADB_X_CALG_MAX; i++) {
5689 algo = ipcomp_algorithm_lookup(i);
5696 panic("assumption failed in key_getcomb_ipcomp");
5698 MGET(m, MB_DONTWAIT, MT_DATA);
5705 M_PREPEND(m, l, MB_DONTWAIT);
5709 comb = mtod(m, struct sadb_comb *);
5710 bzero(comb, sizeof(*comb));
5711 key_getcomb_setlifetime(comb);
5712 comb->sadb_comb_encrypt = i;
5713 /* what should we set into sadb_comb_*_{min,max}bits? */
5720 * XXX no way to pass mode (transport/tunnel) to userland
5721 * XXX replay checking?
5722 * XXX sysctl interface to ipsec_{ah,esp}_keymin
5724 static struct mbuf *
5726 const struct secasindex *saidx;
5728 struct sadb_prop *prop;
5730 const int l = PFKEY_ALIGN8(sizeof(struct sadb_prop));
5733 switch (saidx->proto) {
5736 m = key_getcomb_esp();
5740 m = key_getcomb_ah();
5742 case IPPROTO_IPCOMP:
5743 m = key_getcomb_ipcomp();
5751 M_PREPEND(m, l, MB_DONTWAIT);
5756 for (n = m; n; n = n->m_next)
5759 prop = mtod(m, struct sadb_prop *);
5760 bzero(prop, sizeof(*prop));
5761 prop->sadb_prop_len = PFKEY_UNIT64(totlen);
5762 prop->sadb_prop_exttype = SADB_EXT_PROPOSAL;
5763 prop->sadb_prop_replay = 32; /* XXX */
5769 * SADB_ACQUIRE processing called by key_checkrequest() and key_acquire2().
5771 * <base, SA, address(SD), (address(P)), x_policy,
5772 * (identity(SD),) (sensitivity,) proposal>
5773 * to KMD, and expect to receive
5774 * <base> with SADB_ACQUIRE if error occured,
5776 * <base, src address, dst address, (SPI range)> with SADB_GETSPI
5777 * from KMD by PF_KEY.
5779 * XXX x_policy is outside of RFC2367 (KAME extension).
5780 * XXX sensitivity is not supported.
5781 * XXX for ipcomp, RFC2367 does not define how to fill in proposal.
5782 * see comment for key_getcomb_ipcomp().
5786 * others: error number
5789 key_acquire(saidx, sp)
5790 struct secasindex *saidx;
5791 struct secpolicy *sp;
5793 struct mbuf *result = NULL, *m;
5794 #ifndef IPSEC_NONBLOCK_ACQUIRE
5795 struct secacq *newacq;
5803 panic("key_acquire: NULL pointer is passed.\n");
5804 if ((satype = key_proto2satype(saidx->proto)) == 0)
5805 panic("key_acquire: invalid proto is passed.\n");
5807 #ifndef IPSEC_NONBLOCK_ACQUIRE
5809 * We never do anything about acquirng SA. There is anather
5810 * solution that kernel blocks to send SADB_ACQUIRE message until
5811 * getting something message from IKEd. In later case, to be
5812 * managed with ACQUIRING list.
5814 /* get a entry to check whether sending message or not. */
5815 if ((newacq = key_getacq(saidx)) != NULL) {
5816 if (key_blockacq_count < newacq->count) {
5817 /* reset counter and do send message. */
5820 /* increment counter and do nothing. */
5825 /* make new entry for blocking to send SADB_ACQUIRE. */
5826 if ((newacq = key_newacq(saidx)) == NULL)
5829 /* add to acqtree */
5830 LIST_INSERT_HEAD(&acqtree, newacq, chain);
5835 #ifndef IPSEC_NONBLOCK_ACQUIRE
5838 seq = (acq_seq = (acq_seq == ~0 ? 1 : ++acq_seq));
5840 m = key_setsadbmsg(SADB_ACQUIRE, 0, satype, seq, 0, 0);
5847 /* set sadb_address for saidx's. */
5848 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
5849 (struct sockaddr *)&saidx->src, FULLMASK, IPSEC_ULPROTO_ANY);
5856 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
5857 (struct sockaddr *)&saidx->dst, FULLMASK, IPSEC_ULPROTO_ANY);
5864 /* XXX proxy address (optional) */
5866 /* set sadb_x_policy */
5868 m = key_setsadbxpolicy(sp->policy, sp->spidx.dir, sp->id);
5876 /* XXX identity (optional) */
5878 if (idexttype && fqdn) {
5879 /* create identity extension (FQDN) */
5880 struct sadb_ident *id;
5883 fqdnlen = strlen(fqdn) + 1; /* +1 for terminating-NUL */
5884 id = (struct sadb_ident *)p;
5885 bzero(id, sizeof(*id) + PFKEY_ALIGN8(fqdnlen));
5886 id->sadb_ident_len = PFKEY_UNIT64(sizeof(*id) + PFKEY_ALIGN8(fqdnlen));
5887 id->sadb_ident_exttype = idexttype;
5888 id->sadb_ident_type = SADB_IDENTTYPE_FQDN;
5889 bcopy(fqdn, id + 1, fqdnlen);
5890 p += sizeof(struct sadb_ident) + PFKEY_ALIGN8(fqdnlen);
5894 /* create identity extension (USERFQDN) */
5895 struct sadb_ident *id;
5899 /* +1 for terminating-NUL */
5900 userfqdnlen = strlen(userfqdn) + 1;
5903 id = (struct sadb_ident *)p;
5904 bzero(id, sizeof(*id) + PFKEY_ALIGN8(userfqdnlen));
5905 id->sadb_ident_len = PFKEY_UNIT64(sizeof(*id) + PFKEY_ALIGN8(userfqdnlen));
5906 id->sadb_ident_exttype = idexttype;
5907 id->sadb_ident_type = SADB_IDENTTYPE_USERFQDN;
5908 /* XXX is it correct? */
5909 if (curproc && curproc->p_cred)
5910 id->sadb_ident_id = curproc->p_cred->p_ruid;
5911 if (userfqdn && userfqdnlen)
5912 bcopy(userfqdn, id + 1, userfqdnlen);
5913 p += sizeof(struct sadb_ident) + PFKEY_ALIGN8(userfqdnlen);
5917 /* XXX sensitivity (optional) */
5919 /* create proposal/combination extension */
5920 m = key_getprop(saidx);
5923 * spec conformant: always attach proposal/combination extension,
5924 * the problem is that we have no way to attach it for ipcomp,
5925 * due to the way sadb_comb is declared in RFC2367.
5934 * outside of spec; make proposal/combination extension optional.
5940 if ((result->m_flags & M_PKTHDR) == 0) {
5945 if (result->m_len < sizeof(struct sadb_msg)) {
5946 result = m_pullup(result, sizeof(struct sadb_msg));
5947 if (result == NULL) {
5953 result->m_pkthdr.len = 0;
5954 for (m = result; m; m = m->m_next)
5955 result->m_pkthdr.len += m->m_len;
5957 mtod(result, struct sadb_msg *)->sadb_msg_len =
5958 PFKEY_UNIT64(result->m_pkthdr.len);
5960 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
5968 #ifndef IPSEC_NONBLOCK_ACQUIRE
5969 static struct secacq *
5971 struct secasindex *saidx;
5973 struct secacq *newacq;
5977 KMALLOC(newacq, struct secacq *, sizeof(struct secacq));
5978 if (newacq == NULL) {
5979 ipseclog((LOG_DEBUG, "key_newacq: No more memory.\n"));
5982 bzero(newacq, sizeof(*newacq));
5985 bcopy(saidx, &newacq->saidx, sizeof(newacq->saidx));
5986 newacq->seq = (acq_seq == ~0 ? 1 : ++acq_seq);
5988 newacq->created = tv.tv_sec;
5994 static struct secacq *
5996 struct secasindex *saidx;
6000 LIST_FOREACH(acq, &acqtree, chain) {
6001 if (key_cmpsaidx(saidx, &acq->saidx, CMP_EXACTLY))
6008 static struct secacq *
6009 key_getacqbyseq(seq)
6014 LIST_FOREACH(acq, &acqtree, chain) {
6015 if (acq->seq == seq)
6023 static struct secspacq *
6025 struct secpolicyindex *spidx;
6027 struct secspacq *acq;
6031 KMALLOC(acq, struct secspacq *, sizeof(struct secspacq));
6033 ipseclog((LOG_DEBUG, "key_newspacq: No more memory.\n"));
6036 bzero(acq, sizeof(*acq));
6039 bcopy(spidx, &acq->spidx, sizeof(acq->spidx));
6041 acq->created = tv.tv_sec;
6047 static struct secspacq *
6049 struct secpolicyindex *spidx;
6051 struct secspacq *acq;
6053 LIST_FOREACH(acq, &spacqtree, chain) {
6054 if (key_cmpspidx_exactly(spidx, &acq->spidx))
6062 * SADB_ACQUIRE processing,
6063 * in first situation, is receiving
6065 * from the ikmpd, and clear sequence of its secasvar entry.
6067 * In second situation, is receiving
6068 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
6069 * from a user land process, and return
6070 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
6073 * m will always be freed.
6076 key_acquire2(so, m, mhp)
6079 const struct sadb_msghdr *mhp;
6081 struct sadb_address *src0, *dst0;
6082 struct secasindex saidx;
6083 struct secashead *sah;
6088 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
6089 panic("key_acquire2: NULL pointer is passed.\n");
6092 * Error message from KMd.
6093 * We assume that if error was occured in IKEd, the length of PFKEY
6094 * message is equal to the size of sadb_msg structure.
6095 * We do not raise error even if error occured in this function.
6097 if (mhp->msg->sadb_msg_len == PFKEY_UNIT64(sizeof(struct sadb_msg))) {
6098 #ifndef IPSEC_NONBLOCK_ACQUIRE
6102 /* check sequence number */
6103 if (mhp->msg->sadb_msg_seq == 0) {
6104 ipseclog((LOG_DEBUG, "key_acquire2: must specify sequence number.\n"));
6109 if ((acq = key_getacqbyseq(mhp->msg->sadb_msg_seq)) == NULL) {
6111 * the specified larval SA is already gone, or we got
6112 * a bogus sequence number. we can silently ignore it.
6118 /* reset acq counter in order to deletion by timehander. */
6120 acq->created = tv.tv_sec;
6128 * This message is from user land.
6131 /* map satype to proto */
6132 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
6133 ipseclog((LOG_DEBUG, "key_acquire2: invalid satype is passed.\n"));
6134 return key_senderror(so, m, EINVAL);
6137 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
6138 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
6139 mhp->ext[SADB_EXT_PROPOSAL] == NULL) {
6141 ipseclog((LOG_DEBUG, "key_acquire2: invalid message is passed.\n"));
6142 return key_senderror(so, m, EINVAL);
6144 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
6145 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
6146 mhp->extlen[SADB_EXT_PROPOSAL] < sizeof(struct sadb_prop)) {
6148 ipseclog((LOG_DEBUG, "key_acquire2: invalid message is passed.\n"));
6149 return key_senderror(so, m, EINVAL);
6152 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
6153 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
6155 /* XXX boundary check against sa_len */
6156 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
6158 /* get a SA index */
6159 LIST_FOREACH(sah, &sahtree, chain) {
6160 if (sah->state == SADB_SASTATE_DEAD)
6162 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_MODE_REQID))
6166 ipseclog((LOG_DEBUG, "key_acquire2: a SA exists already.\n"));
6167 return key_senderror(so, m, EEXIST);
6170 error = key_acquire(&saidx, NULL);
6172 ipseclog((LOG_DEBUG, "key_acquire2: error %d returned "
6173 "from key_acquire.\n", mhp->msg->sadb_msg_errno));
6174 return key_senderror(so, m, error);
6177 return key_sendup_mbuf(so, m, KEY_SENDUP_REGISTERED);
6181 * SADB_REGISTER processing.
6182 * If SATYPE_UNSPEC has been passed as satype, only return sabd_supported.
6185 * from the ikmpd, and register a socket to send PF_KEY messages,
6189 * If socket is detached, must free from regnode.
6191 * m will always be freed.
6194 key_register(so, m, mhp)
6197 const struct sadb_msghdr *mhp;
6199 struct secreg *reg, *newreg = 0;
6202 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
6203 panic("key_register: NULL pointer is passed.\n");
6205 /* check for invalid register message */
6206 if (mhp->msg->sadb_msg_satype >= sizeof(regtree)/sizeof(regtree[0]))
6207 return key_senderror(so, m, EINVAL);
6209 /* When SATYPE_UNSPEC is specified, only return sabd_supported. */
6210 if (mhp->msg->sadb_msg_satype == SADB_SATYPE_UNSPEC)
6213 /* check whether existing or not */
6214 LIST_FOREACH(reg, ®tree[mhp->msg->sadb_msg_satype], chain) {
6215 if (reg->so == so) {
6216 ipseclog((LOG_DEBUG, "key_register: socket exists already.\n"));
6217 return key_senderror(so, m, EEXIST);
6221 /* create regnode */
6222 KMALLOC(newreg, struct secreg *, sizeof(*newreg));
6223 if (newreg == NULL) {
6224 ipseclog((LOG_DEBUG, "key_register: No more memory.\n"));
6225 return key_senderror(so, m, ENOBUFS);
6227 bzero((caddr_t)newreg, sizeof(*newreg));
6230 ((struct keycb *)sotorawcb(so))->kp_registered++;
6232 /* add regnode to regtree. */
6233 LIST_INSERT_HEAD(®tree[mhp->msg->sadb_msg_satype], newreg, chain);
6238 struct sadb_msg *newmsg;
6239 struct sadb_supported *sup;
6240 u_int len, alen, elen;
6243 struct sadb_alg *alg;
6245 /* create new sadb_msg to reply. */
6247 for (i = 1; i <= SADB_AALG_MAX; i++) {
6248 if (ah_algorithm_lookup(i))
6249 alen += sizeof(struct sadb_alg);
6252 alen += sizeof(struct sadb_supported);
6255 for (i = 1; i <= SADB_EALG_MAX; i++) {
6256 if (esp_algorithm_lookup(i))
6257 elen += sizeof(struct sadb_alg);
6260 elen += sizeof(struct sadb_supported);
6263 len = sizeof(struct sadb_msg) + alen + elen;
6266 return key_senderror(so, m, ENOBUFS);
6268 MGETHDR(n, MB_DONTWAIT, MT_DATA);
6270 MCLGET(n, MB_DONTWAIT);
6271 if ((n->m_flags & M_EXT) == 0) {
6277 return key_senderror(so, m, ENOBUFS);
6279 n->m_pkthdr.len = n->m_len = len;
6283 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
6284 newmsg = mtod(n, struct sadb_msg *);
6285 newmsg->sadb_msg_errno = 0;
6286 newmsg->sadb_msg_len = PFKEY_UNIT64(len);
6287 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
6289 /* for authentication algorithm */
6291 sup = (struct sadb_supported *)(mtod(n, caddr_t) + off);
6292 sup->sadb_supported_len = PFKEY_UNIT64(alen);
6293 sup->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
6294 off += PFKEY_ALIGN8(sizeof(*sup));
6296 for (i = 1; i <= SADB_AALG_MAX; i++) {
6297 const struct ah_algorithm *aalgo;
6299 aalgo = ah_algorithm_lookup(i);
6302 alg = (struct sadb_alg *)(mtod(n, caddr_t) + off);
6303 alg->sadb_alg_id = i;
6304 alg->sadb_alg_ivlen = 0;
6305 alg->sadb_alg_minbits = aalgo->keymin;
6306 alg->sadb_alg_maxbits = aalgo->keymax;
6307 off += PFKEY_ALIGN8(sizeof(*alg));
6312 /* for encryption algorithm */
6314 sup = (struct sadb_supported *)(mtod(n, caddr_t) + off);
6315 sup->sadb_supported_len = PFKEY_UNIT64(elen);
6316 sup->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT;
6317 off += PFKEY_ALIGN8(sizeof(*sup));
6319 for (i = 1; i <= SADB_EALG_MAX; i++) {
6320 const struct esp_algorithm *ealgo;
6322 ealgo = esp_algorithm_lookup(i);
6325 alg = (struct sadb_alg *)(mtod(n, caddr_t) + off);
6326 alg->sadb_alg_id = i;
6327 if (ealgo && ealgo->ivlen) {
6329 * give NULL to get the value preferred by
6330 * algorithm XXX SADB_X_EXT_DERIV ?
6332 alg->sadb_alg_ivlen =
6333 (*ealgo->ivlen)(ealgo, NULL);
6335 alg->sadb_alg_ivlen = 0;
6336 alg->sadb_alg_minbits = ealgo->keymin;
6337 alg->sadb_alg_maxbits = ealgo->keymax;
6338 off += PFKEY_ALIGN8(sizeof(struct sadb_alg));
6345 panic("length assumption failed in key_register");
6349 return key_sendup_mbuf(so, n, KEY_SENDUP_REGISTERED);
6354 * free secreg entry registered.
6355 * XXX: I want to do free a socket marked done SADB_RESIGER to socket.
6366 panic("key_freereg: NULL pointer is passed.\n");
6369 * check whether existing or not.
6370 * check all type of SA, because there is a potential that
6371 * one socket is registered to multiple type of SA.
6373 for (i = 0; i <= SADB_SATYPE_MAX; i++) {
6374 LIST_FOREACH(reg, ®tree[i], chain) {
6376 && __LIST_CHAINED(reg)) {
6377 LIST_REMOVE(reg, chain);
6388 * SADB_EXPIRE processing
6390 * <base, SA, SA2, lifetime(C and one of HS), address(SD)>
6392 * NOTE: We send only soft lifetime extension.
6395 * others : error number
6399 struct secasvar *sav;
6403 struct mbuf *result = NULL, *m;
6406 struct sadb_lifetime *lt;
6408 /* XXX: Why do we lock ? */
6409 s = splnet(); /*called from softclock()*/
6413 panic("key_expire: NULL pointer is passed.\n");
6414 if (sav->sah == NULL)
6415 panic("key_expire: Why was SA index in SA NULL.\n");
6416 if ((satype = key_proto2satype(sav->sah->saidx.proto)) == 0)
6417 panic("key_expire: invalid proto is passed.\n");
6419 /* set msg header */
6420 m = key_setsadbmsg(SADB_EXPIRE, 0, satype, sav->seq, 0, sav->refcnt);
6427 /* create SA extension */
6428 m = key_setsadbsa(sav);
6435 /* create SA extension */
6436 m = key_setsadbxsa2(sav->sah->saidx.mode,
6437 sav->replay ? sav->replay->count : 0,
6438 sav->sah->saidx.reqid);
6445 /* create lifetime extension (current and soft) */
6446 len = PFKEY_ALIGN8(sizeof(*lt)) * 2;
6447 m = key_alloc_mbuf(len);
6448 if (!m || m->m_next) { /*XXX*/
6454 bzero(mtod(m, caddr_t), len);
6455 lt = mtod(m, struct sadb_lifetime *);
6456 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
6457 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
6458 lt->sadb_lifetime_allocations = sav->lft_c->sadb_lifetime_allocations;
6459 lt->sadb_lifetime_bytes = sav->lft_c->sadb_lifetime_bytes;
6460 lt->sadb_lifetime_addtime = sav->lft_c->sadb_lifetime_addtime;
6461 lt->sadb_lifetime_usetime = sav->lft_c->sadb_lifetime_usetime;
6462 lt = (struct sadb_lifetime *)(mtod(m, caddr_t) + len / 2);
6463 bcopy(sav->lft_s, lt, sizeof(*lt));
6466 /* set sadb_address for source */
6467 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
6468 (struct sockaddr *)&sav->sah->saidx.src,
6469 FULLMASK, IPSEC_ULPROTO_ANY);
6476 /* set sadb_address for destination */
6477 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
6478 (struct sockaddr *)&sav->sah->saidx.dst,
6479 FULLMASK, IPSEC_ULPROTO_ANY);
6486 if ((result->m_flags & M_PKTHDR) == 0) {
6491 if (result->m_len < sizeof(struct sadb_msg)) {
6492 result = m_pullup(result, sizeof(struct sadb_msg));
6493 if (result == NULL) {
6499 result->m_pkthdr.len = 0;
6500 for (m = result; m; m = m->m_next)
6501 result->m_pkthdr.len += m->m_len;
6503 mtod(result, struct sadb_msg *)->sadb_msg_len =
6504 PFKEY_UNIT64(result->m_pkthdr.len);
6507 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
6517 * SADB_FLUSH processing
6520 * from the ikmpd, and free all entries in secastree.
6524 * NOTE: to do is only marking SADB_SASTATE_DEAD.
6526 * m will always be freed.
6529 key_flush(so, m, mhp)
6532 const struct sadb_msghdr *mhp;
6534 struct sadb_msg *newmsg;
6535 struct secashead *sah, *nextsah;
6536 struct secasvar *sav, *nextsav;
6542 if (so == NULL || mhp == NULL || mhp->msg == NULL)
6543 panic("key_flush: NULL pointer is passed.\n");
6545 /* map satype to proto */
6546 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
6547 ipseclog((LOG_DEBUG, "key_flush: invalid satype is passed.\n"));
6548 return key_senderror(so, m, EINVAL);
6551 /* no SATYPE specified, i.e. flushing all SA. */
6552 for (sah = LIST_FIRST(&sahtree);
6555 nextsah = LIST_NEXT(sah, chain);
6557 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
6558 && proto != sah->saidx.proto)
6562 stateidx < _ARRAYLEN(saorder_state_alive);
6564 state = saorder_state_any[stateidx];
6565 for (sav = LIST_FIRST(&sah->savtree[state]);
6569 nextsav = LIST_NEXT(sav, chain);
6571 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
6576 sah->state = SADB_SASTATE_DEAD;
6579 if (m->m_len < sizeof(struct sadb_msg) ||
6580 sizeof(struct sadb_msg) > m->m_len + M_TRAILINGSPACE(m)) {
6581 ipseclog((LOG_DEBUG, "key_flush: No more memory.\n"));
6582 return key_senderror(so, m, ENOBUFS);
6588 m->m_pkthdr.len = m->m_len = sizeof(struct sadb_msg);
6589 newmsg = mtod(m, struct sadb_msg *);
6590 newmsg->sadb_msg_errno = 0;
6591 newmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
6593 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
6597 * SADB_DUMP processing
6598 * dump all entries including status of DEAD in SAD.
6601 * from the ikmpd, and dump all secasvar leaves
6606 * m will always be freed.
6609 key_dump(so, m, mhp)
6612 const struct sadb_msghdr *mhp;
6614 struct secashead *sah;
6615 struct secasvar *sav;
6621 struct sadb_msg *newmsg;
6625 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
6626 panic("key_dump: NULL pointer is passed.\n");
6628 /* map satype to proto */
6629 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
6630 ipseclog((LOG_DEBUG, "key_dump: invalid satype is passed.\n"));
6631 return key_senderror(so, m, EINVAL);
6634 /* count sav entries to be sent to the userland. */
6636 LIST_FOREACH(sah, &sahtree, chain) {
6637 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
6638 && proto != sah->saidx.proto)
6642 stateidx < _ARRAYLEN(saorder_state_any);
6644 state = saorder_state_any[stateidx];
6645 LIST_FOREACH(sav, &sah->savtree[state], chain) {
6652 return key_senderror(so, m, ENOENT);
6654 /* send this to the userland, one at a time. */
6656 LIST_FOREACH(sah, &sahtree, chain) {
6657 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
6658 && proto != sah->saidx.proto)
6661 /* map proto to satype */
6662 if ((satype = key_proto2satype(sah->saidx.proto)) == 0) {
6663 ipseclog((LOG_DEBUG, "key_dump: there was invalid proto in SAD.\n"));
6664 return key_senderror(so, m, EINVAL);
6668 stateidx < _ARRAYLEN(saorder_state_any);
6670 state = saorder_state_any[stateidx];
6671 LIST_FOREACH(sav, &sah->savtree[state], chain) {
6672 n = key_setdumpsa(sav, SADB_DUMP, satype,
6673 --cnt, mhp->msg->sadb_msg_pid);
6675 return key_senderror(so, m, ENOBUFS);
6677 key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
6687 * SADB_X_PROMISC processing
6689 * m will always be freed.
6692 key_promisc(so, m, mhp)
6695 const struct sadb_msghdr *mhp;
6700 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
6701 panic("key_promisc: NULL pointer is passed.\n");
6703 olen = PFKEY_UNUNIT64(mhp->msg->sadb_msg_len);
6705 if (olen < sizeof(struct sadb_msg)) {
6707 return key_senderror(so, m, EINVAL);
6712 } else if (olen == sizeof(struct sadb_msg)) {
6713 /* enable/disable promisc mode */
6716 if ((kp = (struct keycb *)sotorawcb(so)) == NULL)
6717 return key_senderror(so, m, EINVAL);
6718 mhp->msg->sadb_msg_errno = 0;
6719 switch (mhp->msg->sadb_msg_satype) {
6722 kp->kp_promisc = mhp->msg->sadb_msg_satype;
6725 return key_senderror(so, m, EINVAL);
6728 /* send the original message back to everyone */
6729 mhp->msg->sadb_msg_errno = 0;
6730 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
6732 /* send packet as is */
6734 m_adj(m, PFKEY_ALIGN8(sizeof(struct sadb_msg)));
6736 /* TODO: if sadb_msg_seq is specified, send to specific pid */
6737 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
6741 static int (*key_typesw[]) (struct socket *, struct mbuf *,
6742 const struct sadb_msghdr *) = {
6743 NULL, /* SADB_RESERVED */
6744 key_getspi, /* SADB_GETSPI */
6745 key_update, /* SADB_UPDATE */
6746 key_add, /* SADB_ADD */
6747 key_delete, /* SADB_DELETE */
6748 key_get, /* SADB_GET */
6749 key_acquire2, /* SADB_ACQUIRE */
6750 key_register, /* SADB_REGISTER */
6751 NULL, /* SADB_EXPIRE */
6752 key_flush, /* SADB_FLUSH */
6753 key_dump, /* SADB_DUMP */
6754 key_promisc, /* SADB_X_PROMISC */
6755 NULL, /* SADB_X_PCHANGE */
6756 key_spdadd, /* SADB_X_SPDUPDATE */
6757 key_spdadd, /* SADB_X_SPDADD */
6758 key_spddelete, /* SADB_X_SPDDELETE */
6759 key_spdget, /* SADB_X_SPDGET */
6760 NULL, /* SADB_X_SPDACQUIRE */
6761 key_spddump, /* SADB_X_SPDDUMP */
6762 key_spdflush, /* SADB_X_SPDFLUSH */
6763 key_spdadd, /* SADB_X_SPDSETIDX */
6764 NULL, /* SADB_X_SPDEXPIRE */
6765 key_spddelete2, /* SADB_X_SPDDELETE2 */
6769 * parse sadb_msg buffer to process PFKEYv2,
6770 * and create a data to response if needed.
6771 * I think to be dealed with mbuf directly.
6773 * msgp : pointer to pointer to a received buffer pulluped.
6774 * This is rewrited to response.
6775 * so : pointer to socket.
6777 * length for buffer to send to user process.
6784 struct sadb_msg *msg;
6785 struct sadb_msghdr mh;
6791 if (m == NULL || so == NULL)
6792 panic("key_parse: NULL pointer is passed.\n");
6794 #if 0 /*kdebug_sadb assumes msg in linear buffer*/
6795 KEYDEBUG(KEYDEBUG_KEY_DUMP,
6796 ipseclog((LOG_DEBUG, "key_parse: passed sadb_msg\n"));
6800 if (m->m_len < sizeof(struct sadb_msg)) {
6801 m = m_pullup(m, sizeof(struct sadb_msg));
6805 msg = mtod(m, struct sadb_msg *);
6806 orglen = PFKEY_UNUNIT64(msg->sadb_msg_len);
6807 target = KEY_SENDUP_ONE;
6809 if ((m->m_flags & M_PKTHDR) == 0 ||
6810 m->m_pkthdr.len != m->m_pkthdr.len) {
6811 ipseclog((LOG_DEBUG, "key_parse: invalid message length.\n"));
6812 pfkeystat.out_invlen++;
6817 if (msg->sadb_msg_version != PF_KEY_V2) {
6818 ipseclog((LOG_DEBUG,
6819 "key_parse: PF_KEY version %u is mismatched.\n",
6820 msg->sadb_msg_version));
6821 pfkeystat.out_invver++;
6826 if (msg->sadb_msg_type > SADB_MAX) {
6827 ipseclog((LOG_DEBUG, "key_parse: invalid type %u is passed.\n",
6828 msg->sadb_msg_type));
6829 pfkeystat.out_invmsgtype++;
6834 /* for old-fashioned code - should be nuked */
6835 if (m->m_pkthdr.len > MCLBYTES) {
6842 MGETHDR(n, MB_DONTWAIT, MT_DATA);
6843 if (n && m->m_pkthdr.len > MHLEN) {
6844 MCLGET(n, MB_DONTWAIT);
6845 if ((n->m_flags & M_EXT) == 0) {
6854 m_copydata(m, 0, m->m_pkthdr.len, mtod(n, caddr_t));
6855 n->m_pkthdr.len = n->m_len = m->m_pkthdr.len;
6861 /* align the mbuf chain so that extensions are in contiguous region. */
6862 error = key_align(m, &mh);
6866 if (m->m_next) { /*XXX*/
6874 switch (msg->sadb_msg_satype) {
6875 case SADB_SATYPE_UNSPEC:
6876 switch (msg->sadb_msg_type) {
6884 ipseclog((LOG_DEBUG, "key_parse: must specify satype "
6885 "when msg type=%u.\n", msg->sadb_msg_type));
6886 pfkeystat.out_invsatype++;
6891 case SADB_SATYPE_AH:
6892 case SADB_SATYPE_ESP:
6893 case SADB_X_SATYPE_IPCOMP:
6894 switch (msg->sadb_msg_type) {
6896 case SADB_X_SPDDELETE:
6898 case SADB_X_SPDDUMP:
6899 case SADB_X_SPDFLUSH:
6900 case SADB_X_SPDSETIDX:
6901 case SADB_X_SPDUPDATE:
6902 case SADB_X_SPDDELETE2:
6903 ipseclog((LOG_DEBUG, "key_parse: illegal satype=%u\n",
6904 msg->sadb_msg_type));
6905 pfkeystat.out_invsatype++;
6910 case SADB_SATYPE_RSVP:
6911 case SADB_SATYPE_OSPFV2:
6912 case SADB_SATYPE_RIPV2:
6913 case SADB_SATYPE_MIP:
6914 ipseclog((LOG_DEBUG, "key_parse: type %u isn't supported.\n",
6915 msg->sadb_msg_satype));
6916 pfkeystat.out_invsatype++;
6919 case 1: /* XXX: What does it do? */
6920 if (msg->sadb_msg_type == SADB_X_PROMISC)
6924 ipseclog((LOG_DEBUG, "key_parse: invalid type %u is passed.\n",
6925 msg->sadb_msg_satype));
6926 pfkeystat.out_invsatype++;
6931 /* check field of upper layer protocol and address family */
6932 if (mh.ext[SADB_EXT_ADDRESS_SRC] != NULL
6933 && mh.ext[SADB_EXT_ADDRESS_DST] != NULL) {
6934 struct sadb_address *src0, *dst0;
6937 src0 = (struct sadb_address *)(mh.ext[SADB_EXT_ADDRESS_SRC]);
6938 dst0 = (struct sadb_address *)(mh.ext[SADB_EXT_ADDRESS_DST]);
6940 /* check upper layer protocol */
6941 if (src0->sadb_address_proto != dst0->sadb_address_proto) {
6942 ipseclog((LOG_DEBUG, "key_parse: upper layer protocol mismatched.\n"));
6943 pfkeystat.out_invaddr++;
6949 if (PFKEY_ADDR_SADDR(src0)->sa_family !=
6950 PFKEY_ADDR_SADDR(dst0)->sa_family) {
6951 ipseclog((LOG_DEBUG, "key_parse: address family mismatched.\n"));
6952 pfkeystat.out_invaddr++;
6956 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
6957 PFKEY_ADDR_SADDR(dst0)->sa_len) {
6958 ipseclog((LOG_DEBUG,
6959 "key_parse: address struct size mismatched.\n"));
6960 pfkeystat.out_invaddr++;
6965 switch (PFKEY_ADDR_SADDR(src0)->sa_family) {
6967 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
6968 sizeof(struct sockaddr_in)) {
6969 pfkeystat.out_invaddr++;
6975 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
6976 sizeof(struct sockaddr_in6)) {
6977 pfkeystat.out_invaddr++;
6983 ipseclog((LOG_DEBUG,
6984 "key_parse: unsupported address family.\n"));
6985 pfkeystat.out_invaddr++;
6986 error = EAFNOSUPPORT;
6990 switch (PFKEY_ADDR_SADDR(src0)->sa_family) {
6992 plen = sizeof(struct in_addr) << 3;
6995 plen = sizeof(struct in6_addr) << 3;
6998 plen = 0; /*fool gcc*/
7002 /* check max prefix length */
7003 if (src0->sadb_address_prefixlen > plen ||
7004 dst0->sadb_address_prefixlen > plen) {
7005 ipseclog((LOG_DEBUG,
7006 "key_parse: illegal prefixlen.\n"));
7007 pfkeystat.out_invaddr++;
7013 * prefixlen == 0 is valid because there can be a case when
7014 * all addresses are matched.
7018 if (msg->sadb_msg_type >= sizeof(key_typesw)/sizeof(key_typesw[0]) ||
7019 key_typesw[msg->sadb_msg_type] == NULL) {
7020 pfkeystat.out_invmsgtype++;
7025 return (*key_typesw[msg->sadb_msg_type])(so, m, &mh);
7028 msg->sadb_msg_errno = error;
7029 return key_sendup_mbuf(so, m, target);
7033 key_senderror(so, m, code)
7038 struct sadb_msg *msg;
7040 if (m->m_len < sizeof(struct sadb_msg))
7041 panic("invalid mbuf passed to key_senderror");
7043 msg = mtod(m, struct sadb_msg *);
7044 msg->sadb_msg_errno = code;
7045 return key_sendup_mbuf(so, m, KEY_SENDUP_ONE);
7049 * set the pointer to each header into message buffer.
7050 * m will be freed on error.
7051 * XXX larger-than-MCLBYTES extension?
7056 struct sadb_msghdr *mhp;
7059 struct sadb_ext *ext;
7065 if (m == NULL || mhp == NULL)
7066 panic("key_align: NULL pointer is passed.\n");
7067 if (m->m_len < sizeof(struct sadb_msg))
7068 panic("invalid mbuf passed to key_align");
7071 bzero(mhp, sizeof(*mhp));
7073 mhp->msg = mtod(m, struct sadb_msg *);
7074 mhp->ext[0] = (struct sadb_ext *)mhp->msg; /*XXX backward compat */
7076 end = PFKEY_UNUNIT64(mhp->msg->sadb_msg_len);
7077 extlen = end; /*just in case extlen is not updated*/
7078 for (off = sizeof(struct sadb_msg); off < end; off += extlen) {
7079 n = m_pulldown(m, off, sizeof(struct sadb_ext), &toff);
7081 /* m is already freed */
7084 ext = (struct sadb_ext *)(mtod(n, caddr_t) + toff);
7087 switch (ext->sadb_ext_type) {
7089 case SADB_EXT_ADDRESS_SRC:
7090 case SADB_EXT_ADDRESS_DST:
7091 case SADB_EXT_ADDRESS_PROXY:
7092 case SADB_EXT_LIFETIME_CURRENT:
7093 case SADB_EXT_LIFETIME_HARD:
7094 case SADB_EXT_LIFETIME_SOFT:
7095 case SADB_EXT_KEY_AUTH:
7096 case SADB_EXT_KEY_ENCRYPT:
7097 case SADB_EXT_IDENTITY_SRC:
7098 case SADB_EXT_IDENTITY_DST:
7099 case SADB_EXT_SENSITIVITY:
7100 case SADB_EXT_PROPOSAL:
7101 case SADB_EXT_SUPPORTED_AUTH:
7102 case SADB_EXT_SUPPORTED_ENCRYPT:
7103 case SADB_EXT_SPIRANGE:
7104 case SADB_X_EXT_POLICY:
7105 case SADB_X_EXT_SA2:
7106 /* duplicate check */
7108 * XXX Are there duplication payloads of either
7109 * KEY_AUTH or KEY_ENCRYPT ?
7111 if (mhp->ext[ext->sadb_ext_type] != NULL) {
7112 ipseclog((LOG_DEBUG,
7113 "key_align: duplicate ext_type %u "
7114 "is passed.\n", ext->sadb_ext_type));
7116 pfkeystat.out_dupext++;
7121 ipseclog((LOG_DEBUG,
7122 "key_align: invalid ext_type %u is passed.\n",
7123 ext->sadb_ext_type));
7125 pfkeystat.out_invexttype++;
7129 extlen = PFKEY_UNUNIT64(ext->sadb_ext_len);
7131 if (key_validate_ext(ext, extlen)) {
7133 pfkeystat.out_invlen++;
7137 n = m_pulldown(m, off, extlen, &toff);
7139 /* m is already freed */
7142 ext = (struct sadb_ext *)(mtod(n, caddr_t) + toff);
7144 mhp->ext[ext->sadb_ext_type] = ext;
7145 mhp->extoff[ext->sadb_ext_type] = off;
7146 mhp->extlen[ext->sadb_ext_type] = extlen;
7151 pfkeystat.out_invlen++;
7159 key_validate_ext(ext, len)
7160 const struct sadb_ext *ext;
7163 const struct sockaddr *sa;
7164 enum { NONE, ADDR } checktype = NONE;
7166 const int sal = offsetof(struct sockaddr, sa_len) + sizeof(sa->sa_len);
7168 if (len != PFKEY_UNUNIT64(ext->sadb_ext_len))
7171 /* if it does not match minimum/maximum length, bail */
7172 if (ext->sadb_ext_type >= sizeof(minsize) / sizeof(minsize[0]) ||
7173 ext->sadb_ext_type >= sizeof(maxsize) / sizeof(maxsize[0]))
7175 if (!minsize[ext->sadb_ext_type] || len < minsize[ext->sadb_ext_type])
7177 if (maxsize[ext->sadb_ext_type] && len > maxsize[ext->sadb_ext_type])
7180 /* more checks based on sadb_ext_type XXX need more */
7181 switch (ext->sadb_ext_type) {
7182 case SADB_EXT_ADDRESS_SRC:
7183 case SADB_EXT_ADDRESS_DST:
7184 case SADB_EXT_ADDRESS_PROXY:
7185 baselen = PFKEY_ALIGN8(sizeof(struct sadb_address));
7188 case SADB_EXT_IDENTITY_SRC:
7189 case SADB_EXT_IDENTITY_DST:
7190 if (((const struct sadb_ident *)ext)->sadb_ident_type ==
7191 SADB_X_IDENTTYPE_ADDR) {
7192 baselen = PFKEY_ALIGN8(sizeof(struct sadb_ident));
7202 switch (checktype) {
7206 sa = (const struct sockaddr *)((c_caddr_t)ext + baselen);
7207 if (len < baselen + sal)
7209 if (baselen + PFKEY_ALIGN8(sa->sa_len) != len)
7222 bzero((caddr_t)&key_cb, sizeof(key_cb));
7224 for (i = 0; i < IPSEC_DIR_MAX; i++) {
7225 LIST_INIT(&sptree[i]);
7228 LIST_INIT(&sahtree);
7230 for (i = 0; i <= SADB_SATYPE_MAX; i++) {
7231 LIST_INIT(®tree[i]);
7234 #ifndef IPSEC_NONBLOCK_ACQUIRE
7235 LIST_INIT(&acqtree);
7237 LIST_INIT(&spacqtree);
7239 /* system default */
7241 ip4_def_policy.policy = IPSEC_POLICY_NONE;
7242 ip4_def_policy.refcnt++; /*never reclaim this*/
7245 ip6_def_policy.policy = IPSEC_POLICY_NONE;
7246 ip6_def_policy.refcnt++; /*never reclaim this*/
7249 #ifndef IPSEC_DEBUG2
7250 callout_reset(&key_timehandler_ch, hz, key_timehandler, NULL);
7251 #endif /*IPSEC_DEBUG2*/
7253 /* initialize key statistics */
7254 keystat.getspi_count = 1;
7256 printf("IPsec: Initialized Security Association Processing.\n");
7262 * XXX: maybe This function is called after INBOUND IPsec processing.
7264 * Special check for tunnel-mode packets.
7265 * We must make some checks for consistency between inner and outer IP header.
7267 * xxx more checks to be provided
7270 key_checktunnelsanity(sav, family, src, dst)
7271 struct secasvar *sav;
7277 if (sav->sah == NULL)
7278 panic("sav->sah == NULL at key_checktunnelsanity");
7280 /* XXX: check inner IP header */
7286 #define hostnamelen strlen(hostname)
7289 * Get FQDN for the host.
7290 * If the administrator configured hostname (by hostname(1)) without
7291 * domain name, returns nothing.
7298 static char fqdn[MAXHOSTNAMELEN + 1];
7303 /* check if it comes with domain name. */
7305 for (i = 0; i < hostnamelen; i++) {
7306 if (hostname[i] == '.')
7312 /* NOTE: hostname may not be NUL-terminated. */
7313 bzero(fqdn, sizeof(fqdn));
7314 bcopy(hostname, fqdn, hostnamelen);
7315 fqdn[hostnamelen] = '\0';
7320 * get username@FQDN for the host/user.
7326 static char userfqdn[MAXHOSTNAMELEN + MAXLOGNAME + 2];
7327 struct proc *p = curproc;
7330 if (!p || !p->p_pgrp || !p->p_pgrp->pg_session)
7332 if (!(host = key_getfqdn()))
7335 /* NOTE: s_login may not be-NUL terminated. */
7336 bzero(userfqdn, sizeof(userfqdn));
7337 bcopy(p->p_pgrp->pg_session->s_login, userfqdn, MAXLOGNAME);
7338 userfqdn[MAXLOGNAME] = '\0'; /* safeguard */
7339 q = userfqdn + strlen(userfqdn);
7341 bcopy(host, q, strlen(host));
7349 /* record data transfer on SA, and update timestamps */
7351 key_sa_recordxfer(sav, m)
7352 struct secasvar *sav;
7356 panic("key_sa_recordxfer called with sav == NULL");
7358 panic("key_sa_recordxfer called with m == NULL");
7363 * XXX Currently, there is a difference of bytes size
7364 * between inbound and outbound processing.
7366 sav->lft_c->sadb_lifetime_bytes += m->m_pkthdr.len;
7367 /* to check bytes lifetime is done in key_timehandler(). */
7370 * We use the number of packets as the unit of
7371 * sadb_lifetime_allocations. We increment the variable
7372 * whenever {esp,ah}_{in,out}put is called.
7374 sav->lft_c->sadb_lifetime_allocations++;
7375 /* XXX check for expires? */
7378 * NOTE: We record CURRENT sadb_lifetime_usetime by using wall clock,
7379 * in seconds. HARD and SOFT lifetime are measured by the time
7380 * difference (again in seconds) from sadb_lifetime_usetime.
7384 * -----+-----+--------+---> t
7385 * <--------------> HARD
7391 sav->lft_c->sadb_lifetime_usetime = tv.tv_sec;
7392 /* XXX check for expires? */
7400 key_sa_routechange(dst)
7401 struct sockaddr *dst;
7403 struct secashead *sah;
7406 LIST_FOREACH(sah, &sahtree, chain) {
7407 ro = &sah->sa_route;
7408 if (ro->ro_rt && dst->sa_len == ro->ro_dst.sa_len
7409 && bcmp(dst, &ro->ro_dst, dst->sa_len) == 0) {
7411 ro->ro_rt = (struct rtentry *)NULL;
7419 key_sa_chgstate(sav, state)
7420 struct secasvar *sav;
7424 panic("key_sa_chgstate called with sav == NULL");
7426 if (sav->state == state)
7429 if (__LIST_CHAINED(sav))
7430 LIST_REMOVE(sav, chain);
7433 LIST_INSERT_HEAD(&sav->sah->savtree[state], sav, chain);
7438 struct secasvar *sav;
7442 panic("key_sa_stir_iv called with sav == NULL");
7443 key_randomfill(sav->iv, sav->ivlen);
7447 static struct mbuf *
7451 struct mbuf *m = NULL, *n;
7456 MGET(n, MB_DONTWAIT, MT_DATA);
7457 if (n && len > MLEN)
7458 MCLGET(n, MB_DONTWAIT);
7466 n->m_len = M_TRAILINGSPACE(n);
7467 /* use the bottom of mbuf, hoping we can prepend afterwards */
7468 if (n->m_len > len) {
7469 t = (n->m_len - len) & ~(sizeof(long) - 1);