1 /* $FreeBSD: src/sys/netkey/key.c,v 1.16.2.13 2002/07/24 18:17:40 ume Exp $ */
2 /* $KAME: key.c,v 1.191 2001/06/27 10:46:49 sakane Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * This code is referd to RFC 2367
38 #include "opt_inet6.h"
39 #include "opt_ipsec.h"
41 #include <sys/types.h>
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
46 #include <sys/domain.h>
47 #include <sys/protosw.h>
48 #include <sys/malloc.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/sysctl.h>
52 #include <sys/errno.h>
54 #include <sys/queue.h>
55 #include <sys/syslog.h>
58 #include <net/route.h>
59 #include <net/raw_cb.h>
61 #include <netinet/in.h>
62 #include <netinet/in_systm.h>
63 #include <netinet/ip.h>
64 #include <netinet/in_var.h>
67 #include <netinet/ip6.h>
68 #include <netinet6/in6_var.h>
69 #include <netinet6/ip6_var.h>
73 #include <netinet/in_pcb.h>
76 #include <netinet6/in6_pcb.h>
79 #include <net/pfkeyv2.h>
80 #include <netkey/keydb.h>
81 #include <netkey/key.h>
82 #include <netkey/keysock.h>
83 #include <netkey/key_debug.h>
85 #include <netinet6/ipsec.h>
87 #include <netinet6/ipsec6.h>
89 #include <netinet6/ah.h>
91 #include <netinet6/ah6.h>
94 #include <netinet6/esp.h>
96 #include <netinet6/esp6.h>
99 #include <netinet6/ipcomp.h>
101 #include <netinet6/ipcomp6.h>
104 #include <machine/stdarg.h>
107 #include <sys/random.h>
109 #include <net/net_osdep.h>
112 #define satosin(s) ((struct sockaddr_in *)s)
115 #define FULLMASK 0xff
118 * Note on SA reference counting:
119 * - SAs that are not in DEAD state will have (total external reference + 1)
120 * following value in reference count field. they cannot be freed and are
121 * referenced from SA header.
122 * - SAs that are in DEAD state will have (total external reference)
123 * in reference count field. they are ready to be freed. reference from
124 * SA header will be removed in key_delsav(), when the reference count
125 * field hits 0 (= no external reference other than from SA header.
128 u_int32_t key_debug_level = 0;
129 static u_int key_spi_trycnt = 1000;
130 static u_int32_t key_spi_minval = 0x100;
131 static u_int32_t key_spi_maxval = 0x0fffffff; /* XXX */
132 static u_int32_t policy_id = 0;
133 static u_int key_int_random = 60; /*interval to initialize randseed,1(m)*/
134 static u_int key_larval_lifetime = 30; /* interval to expire acquiring, 30(s)*/
135 static int key_blockacq_count = 10; /* counter for blocking SADB_ACQUIRE.*/
136 static int key_blockacq_lifetime = 20; /* lifetime for blocking SADB_ACQUIRE.*/
137 static int key_preferred_oldsa = 1; /* preferred old sa rather than new sa.*/
139 static u_int32_t acq_seq = 0;
140 static int key_tick_init_random = 0;
142 static LIST_HEAD(_sptree, secpolicy) sptree[IPSEC_DIR_MAX]; /* SPD */
143 static LIST_HEAD(_sahtree, secashead) sahtree; /* SAD */
144 static LIST_HEAD(_regtree, secreg) regtree[SADB_SATYPE_MAX + 1];
146 #ifndef IPSEC_NONBLOCK_ACQUIRE
147 static LIST_HEAD(_acqtree, secacq) acqtree; /* acquiring list */
149 static LIST_HEAD(_spacqtree, secspacq) spacqtree; /* SP acquiring list */
151 struct key_cb key_cb;
153 /* search order for SAs */
154 static const u_int saorder_state_valid_prefer_old[] = {
155 SADB_SASTATE_DYING, SADB_SASTATE_MATURE,
157 static const u_int saorder_state_valid_prefer_new[] = {
158 SADB_SASTATE_MATURE, SADB_SASTATE_DYING,
160 static const u_int saorder_state_alive[] = {
162 SADB_SASTATE_MATURE, SADB_SASTATE_DYING, SADB_SASTATE_LARVAL
164 static const u_int saorder_state_any[] = {
165 SADB_SASTATE_MATURE, SADB_SASTATE_DYING,
166 SADB_SASTATE_LARVAL, SADB_SASTATE_DEAD
169 static const int minsize[] = {
170 sizeof(struct sadb_msg), /* SADB_EXT_RESERVED */
171 sizeof(struct sadb_sa), /* SADB_EXT_SA */
172 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_CURRENT */
173 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_HARD */
174 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_SOFT */
175 sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_SRC */
176 sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_DST */
177 sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_PROXY */
178 sizeof(struct sadb_key), /* SADB_EXT_KEY_AUTH */
179 sizeof(struct sadb_key), /* SADB_EXT_KEY_ENCRYPT */
180 sizeof(struct sadb_ident), /* SADB_EXT_IDENTITY_SRC */
181 sizeof(struct sadb_ident), /* SADB_EXT_IDENTITY_DST */
182 sizeof(struct sadb_sens), /* SADB_EXT_SENSITIVITY */
183 sizeof(struct sadb_prop), /* SADB_EXT_PROPOSAL */
184 sizeof(struct sadb_supported), /* SADB_EXT_SUPPORTED_AUTH */
185 sizeof(struct sadb_supported), /* SADB_EXT_SUPPORTED_ENCRYPT */
186 sizeof(struct sadb_spirange), /* SADB_EXT_SPIRANGE */
187 0, /* SADB_X_EXT_KMPRIVATE */
188 sizeof(struct sadb_x_policy), /* SADB_X_EXT_POLICY */
189 sizeof(struct sadb_x_sa2), /* SADB_X_SA2 */
191 static const int maxsize[] = {
192 sizeof(struct sadb_msg), /* SADB_EXT_RESERVED */
193 sizeof(struct sadb_sa), /* SADB_EXT_SA */
194 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_CURRENT */
195 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_HARD */
196 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_SOFT */
197 0, /* SADB_EXT_ADDRESS_SRC */
198 0, /* SADB_EXT_ADDRESS_DST */
199 0, /* SADB_EXT_ADDRESS_PROXY */
200 0, /* SADB_EXT_KEY_AUTH */
201 0, /* SADB_EXT_KEY_ENCRYPT */
202 0, /* SADB_EXT_IDENTITY_SRC */
203 0, /* SADB_EXT_IDENTITY_DST */
204 0, /* SADB_EXT_SENSITIVITY */
205 0, /* SADB_EXT_PROPOSAL */
206 0, /* SADB_EXT_SUPPORTED_AUTH */
207 0, /* SADB_EXT_SUPPORTED_ENCRYPT */
208 sizeof(struct sadb_spirange), /* SADB_EXT_SPIRANGE */
209 0, /* SADB_X_EXT_KMPRIVATE */
210 0, /* SADB_X_EXT_POLICY */
211 sizeof(struct sadb_x_sa2), /* SADB_X_SA2 */
214 static int ipsec_esp_keymin = 256;
215 static int ipsec_esp_auth = 0;
216 static int ipsec_ah_keymin = 128;
219 SYSCTL_DECL(_net_key);
222 SYSCTL_INT(_net_key, KEYCTL_DEBUG_LEVEL, debug, CTLFLAG_RW, \
223 &key_debug_level, 0, "");
225 /* max count of trial for the decision of spi value */
226 SYSCTL_INT(_net_key, KEYCTL_SPI_TRY, spi_trycnt, CTLFLAG_RW, \
227 &key_spi_trycnt, 0, "");
229 /* minimum spi value to allocate automatically. */
230 SYSCTL_INT(_net_key, KEYCTL_SPI_MIN_VALUE, spi_minval, CTLFLAG_RW, \
231 &key_spi_minval, 0, "");
233 /* maximun spi value to allocate automatically. */
234 SYSCTL_INT(_net_key, KEYCTL_SPI_MAX_VALUE, spi_maxval, CTLFLAG_RW, \
235 &key_spi_maxval, 0, "");
237 /* interval to initialize randseed */
238 SYSCTL_INT(_net_key, KEYCTL_RANDOM_INT, int_random, CTLFLAG_RW, \
239 &key_int_random, 0, "");
241 /* lifetime for larval SA */
242 SYSCTL_INT(_net_key, KEYCTL_LARVAL_LIFETIME, larval_lifetime, CTLFLAG_RW, \
243 &key_larval_lifetime, 0, "");
245 /* counter for blocking to send SADB_ACQUIRE to IKEd */
246 SYSCTL_INT(_net_key, KEYCTL_BLOCKACQ_COUNT, blockacq_count, CTLFLAG_RW, \
247 &key_blockacq_count, 0, "");
249 /* lifetime for blocking to send SADB_ACQUIRE to IKEd */
250 SYSCTL_INT(_net_key, KEYCTL_BLOCKACQ_LIFETIME, blockacq_lifetime, CTLFLAG_RW, \
251 &key_blockacq_lifetime, 0, "");
254 SYSCTL_INT(_net_key, KEYCTL_ESP_AUTH, esp_auth, CTLFLAG_RW, \
255 &ipsec_esp_auth, 0, "");
257 /* minimum ESP key length */
258 SYSCTL_INT(_net_key, KEYCTL_ESP_KEYMIN, esp_keymin, CTLFLAG_RW, \
259 &ipsec_esp_keymin, 0, "");
261 /* minimum AH key length */
262 SYSCTL_INT(_net_key, KEYCTL_AH_KEYMIN, ah_keymin, CTLFLAG_RW, \
263 &ipsec_ah_keymin, 0, "");
265 /* perfered old SA rather than new SA */
266 SYSCTL_INT(_net_key, KEYCTL_PREFERED_OLDSA, prefered_oldsa, CTLFLAG_RW,\
267 &key_preferred_oldsa, 0, "");
270 #define LIST_FOREACH(elm, head, field) \
271 for (elm = LIST_FIRST(head); elm; elm = LIST_NEXT(elm, field))
273 #define __LIST_CHAINED(elm) \
274 (!((elm)->chain.le_next == NULL && (elm)->chain.le_prev == NULL))
275 #define LIST_INSERT_TAIL(head, elm, type, field) \
277 struct type *curelm = LIST_FIRST(head); \
278 if (curelm == NULL) {\
279 LIST_INSERT_HEAD(head, elm, field); \
281 while (LIST_NEXT(curelm, field)) \
282 curelm = LIST_NEXT(curelm, field);\
283 LIST_INSERT_AFTER(curelm, elm, field);\
287 #define KEY_CHKSASTATE(head, sav, name) \
289 if ((head) != (sav)) { \
290 ipseclog((LOG_DEBUG, "%s: state mismatched (TREE=%d SA=%d)\n", \
291 (name), (head), (sav))); \
296 #define KEY_CHKSPDIR(head, sp, name) \
298 if ((head) != (sp)) { \
299 ipseclog((LOG_DEBUG, "%s: direction mismatched (TREE=%d SP=%d), " \
300 "anyway continue.\n", \
301 (name), (head), (sp))); \
306 #define KMALLOC(p, t, n) \
307 ((p) = (t) malloc((unsigned long)(n), M_SECA, M_NOWAIT))
309 free((caddr_t)(p), M_SECA);
311 #define KMALLOC(p, t, n) \
313 ((p) = (t)malloc((unsigned long)(n), M_SECA, M_NOWAIT)); \
314 printf("%s %d: %p <- KMALLOC(%s, %d)\n", \
315 __FILE__, __LINE__, (p), #t, n); \
320 printf("%s %d: %p -> KFREE()\n", __FILE__, __LINE__, (p)); \
321 free((caddr_t)(p), M_SECA); \
326 * set parameters into secpolicyindex buffer.
327 * Must allocate secpolicyindex buffer passed to this function.
329 #define KEY_SETSECSPIDX(_dir, s, d, ps, pd, ulp, idx) \
331 bzero((idx), sizeof(struct secpolicyindex)); \
332 (idx)->dir = (_dir); \
333 (idx)->prefs = (ps); \
334 (idx)->prefd = (pd); \
335 (idx)->ul_proto = (ulp); \
336 bcopy((s), &(idx)->src, ((struct sockaddr *)(s))->sa_len); \
337 bcopy((d), &(idx)->dst, ((struct sockaddr *)(d))->sa_len); \
341 * set parameters into secasindex buffer.
342 * Must allocate secasindex buffer before calling this function.
344 #define KEY_SETSECASIDX(p, m, r, s, d, idx) \
346 bzero((idx), sizeof(struct secasindex)); \
347 (idx)->proto = (p); \
349 (idx)->reqid = (r); \
350 bcopy((s), &(idx)->src, ((struct sockaddr *)(s))->sa_len); \
351 bcopy((d), &(idx)->dst, ((struct sockaddr *)(d))->sa_len); \
356 u_long getspi_count; /* the avarage of count to try to get new SPI */
360 struct sadb_msg *msg;
361 struct sadb_ext *ext[SADB_EXT_MAX + 1];
362 int extoff[SADB_EXT_MAX + 1];
363 int extlen[SADB_EXT_MAX + 1];
366 static struct secasvar *key_allocsa_policy __P((struct secasindex *));
367 static void key_freesp_so __P((struct secpolicy **));
368 static struct secasvar *key_do_allocsa_policy __P((struct secashead *, u_int));
369 static void key_delsp __P((struct secpolicy *));
370 static struct secpolicy *key_getsp __P((struct secpolicyindex *));
371 static struct secpolicy *key_getspbyid __P((u_int32_t));
372 static u_int32_t key_newreqid __P((void));
373 static struct mbuf *key_gather_mbuf __P((struct mbuf *,
374 const struct sadb_msghdr *, int, int, ...));
375 static int key_spdadd __P((struct socket *, struct mbuf *,
376 const struct sadb_msghdr *));
377 static u_int32_t key_getnewspid __P((void));
378 static int key_spddelete __P((struct socket *, struct mbuf *,
379 const struct sadb_msghdr *));
380 static int key_spddelete2 __P((struct socket *, struct mbuf *,
381 const struct sadb_msghdr *));
382 static int key_spdget __P((struct socket *, struct mbuf *,
383 const struct sadb_msghdr *));
384 static int key_spdflush __P((struct socket *, struct mbuf *,
385 const struct sadb_msghdr *));
386 static int key_spddump __P((struct socket *, struct mbuf *,
387 const struct sadb_msghdr *));
388 static struct mbuf *key_setdumpsp __P((struct secpolicy *,
389 u_int8_t, u_int32_t, u_int32_t));
390 static u_int key_getspreqmsglen __P((struct secpolicy *));
391 static int key_spdexpire __P((struct secpolicy *));
392 static struct secashead *key_newsah __P((struct secasindex *));
393 static void key_delsah __P((struct secashead *));
394 static struct secasvar *key_newsav __P((struct mbuf *,
395 const struct sadb_msghdr *, struct secashead *, int *));
396 static void key_delsav __P((struct secasvar *));
397 static struct secashead *key_getsah __P((struct secasindex *));
398 static struct secasvar *key_checkspidup __P((struct secasindex *, u_int32_t));
399 static struct secasvar *key_getsavbyspi __P((struct secashead *, u_int32_t));
400 static int key_setsaval __P((struct secasvar *, struct mbuf *,
401 const struct sadb_msghdr *));
402 static int key_mature __P((struct secasvar *));
403 static struct mbuf *key_setdumpsa __P((struct secasvar *, u_int8_t,
404 u_int8_t, u_int32_t, u_int32_t));
405 static struct mbuf *key_setsadbmsg __P((u_int8_t, u_int16_t, u_int8_t,
406 u_int32_t, pid_t, u_int16_t));
407 static struct mbuf *key_setsadbsa __P((struct secasvar *));
408 static struct mbuf *key_setsadbaddr __P((u_int16_t,
409 struct sockaddr *, u_int8_t, u_int16_t));
411 static struct mbuf *key_setsadbident __P((u_int16_t, u_int16_t, caddr_t,
414 static struct mbuf *key_setsadbxsa2 __P((u_int8_t, u_int32_t, u_int32_t));
415 static struct mbuf *key_setsadbxpolicy __P((u_int16_t, u_int8_t,
417 static void *key_newbuf __P((const void *, u_int));
419 static int key_ismyaddr6 __P((struct sockaddr_in6 *));
422 /* flags for key_cmpsaidx() */
423 #define CMP_HEAD 1 /* protocol, addresses. */
424 #define CMP_MODE_REQID 2 /* additionally HEAD, reqid, mode. */
425 #define CMP_REQID 3 /* additionally HEAD, reaid. */
426 #define CMP_EXACTLY 4 /* all elements. */
427 static int key_cmpsaidx
428 __P((struct secasindex *, struct secasindex *, int));
430 static int key_cmpspidx_exactly
431 __P((struct secpolicyindex *, struct secpolicyindex *));
432 static int key_cmpspidx_withmask
433 __P((struct secpolicyindex *, struct secpolicyindex *));
434 static int key_sockaddrcmp __P((struct sockaddr *, struct sockaddr *, int));
435 static int key_bbcmp __P((caddr_t, caddr_t, u_int));
436 static void key_srandom __P((void));
437 static u_int16_t key_satype2proto __P((u_int8_t));
438 static u_int8_t key_proto2satype __P((u_int16_t));
440 static int key_getspi __P((struct socket *, struct mbuf *,
441 const struct sadb_msghdr *));
442 static u_int32_t key_do_getnewspi __P((struct sadb_spirange *,
443 struct secasindex *));
444 static int key_update __P((struct socket *, struct mbuf *,
445 const struct sadb_msghdr *));
446 #ifdef IPSEC_DOSEQCHECK
447 static struct secasvar *key_getsavbyseq __P((struct secashead *, u_int32_t));
449 static int key_add __P((struct socket *, struct mbuf *,
450 const struct sadb_msghdr *));
451 static int key_setident __P((struct secashead *, struct mbuf *,
452 const struct sadb_msghdr *));
453 static struct mbuf *key_getmsgbuf_x1 __P((struct mbuf *,
454 const struct sadb_msghdr *));
455 static int key_delete __P((struct socket *, struct mbuf *,
456 const struct sadb_msghdr *));
457 static int key_get __P((struct socket *, struct mbuf *,
458 const struct sadb_msghdr *));
460 static void key_getcomb_setlifetime __P((struct sadb_comb *));
462 static struct mbuf *key_getcomb_esp __P((void));
464 static struct mbuf *key_getcomb_ah __P((void));
465 static struct mbuf *key_getcomb_ipcomp __P((void));
466 static struct mbuf *key_getprop __P((const struct secasindex *));
468 static int key_acquire __P((struct secasindex *, struct secpolicy *));
469 #ifndef IPSEC_NONBLOCK_ACQUIRE
470 static struct secacq *key_newacq __P((struct secasindex *));
471 static struct secacq *key_getacq __P((struct secasindex *));
472 static struct secacq *key_getacqbyseq __P((u_int32_t));
474 static struct secspacq *key_newspacq __P((struct secpolicyindex *));
475 static struct secspacq *key_getspacq __P((struct secpolicyindex *));
476 static int key_acquire2 __P((struct socket *, struct mbuf *,
477 const struct sadb_msghdr *));
478 static int key_register __P((struct socket *, struct mbuf *,
479 const struct sadb_msghdr *));
480 static int key_expire __P((struct secasvar *));
481 static int key_flush __P((struct socket *, struct mbuf *,
482 const struct sadb_msghdr *));
483 static int key_dump __P((struct socket *, struct mbuf *,
484 const struct sadb_msghdr *));
485 static int key_promisc __P((struct socket *, struct mbuf *,
486 const struct sadb_msghdr *));
487 static int key_senderror __P((struct socket *, struct mbuf *, int));
488 static int key_validate_ext __P((const struct sadb_ext *, int));
489 static int key_align __P((struct mbuf *, struct sadb_msghdr *));
491 static const char *key_getfqdn __P((void));
492 static const char *key_getuserfqdn __P((void));
494 static void key_sa_chgstate __P((struct secasvar *, u_int8_t));
495 static struct mbuf *key_alloc_mbuf __P((int));
497 /* %%% IPsec policy management */
499 * allocating a SP for OUTBOUND or INBOUND packet.
500 * Must call key_freesp() later.
501 * OUT: NULL: not found
502 * others: found and return the pointer.
505 key_allocsp(spidx, dir)
506 struct secpolicyindex *spidx;
509 struct secpolicy *sp;
515 panic("key_allocsp: NULL pointer is passed.\n");
517 /* check direction */
519 case IPSEC_DIR_INBOUND:
520 case IPSEC_DIR_OUTBOUND:
523 panic("key_allocsp: Invalid direction is passed.\n");
527 s = splnet(); /*called from softclock()*/
528 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
529 printf("*** objects\n");
530 kdebug_secpolicyindex(spidx));
532 LIST_FOREACH(sp, &sptree[dir], chain) {
533 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
534 printf("*** in SPD\n");
535 kdebug_secpolicyindex(&sp->spidx));
537 if (sp->state == IPSEC_SPSTATE_DEAD)
539 if (key_cmpspidx_withmask(&sp->spidx, spidx))
548 KEY_CHKSPDIR(sp->spidx.dir, dir, "key_allocsp");
550 /* found a SPD entry */
552 sp->lastused = tv.tv_sec;
555 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
556 printf("DP key_allocsp cause refcnt++:%d SP:%p\n",
563 * return a policy that matches this particular inbound packet.
567 key_gettunnel(osrc, odst, isrc, idst)
568 struct sockaddr *osrc, *odst, *isrc, *idst;
570 struct secpolicy *sp;
571 const int dir = IPSEC_DIR_INBOUND;
574 struct ipsecrequest *r1, *r2, *p;
575 struct sockaddr *os, *od, *is, *id;
576 struct secpolicyindex spidx;
578 if (isrc->sa_family != idst->sa_family) {
579 ipseclog((LOG_ERR, "protocol family mismatched %d != %d\n.",
580 isrc->sa_family, idst->sa_family));
584 s = splnet(); /*called from softclock()*/
585 LIST_FOREACH(sp, &sptree[dir], chain) {
586 if (sp->state == IPSEC_SPSTATE_DEAD)
590 for (p = sp->req; p; p = p->next) {
591 if (p->saidx.mode != IPSEC_MODE_TUNNEL)
598 /* here we look at address matches only */
600 if (isrc->sa_len > sizeof(spidx.src) ||
601 idst->sa_len > sizeof(spidx.dst))
603 bcopy(isrc, &spidx.src, isrc->sa_len);
604 bcopy(idst, &spidx.dst, idst->sa_len);
605 if (!key_cmpspidx_withmask(&sp->spidx, &spidx))
608 is = (struct sockaddr *)&r1->saidx.src;
609 id = (struct sockaddr *)&r1->saidx.dst;
610 if (key_sockaddrcmp(is, isrc, 0) ||
611 key_sockaddrcmp(id, idst, 0))
615 os = (struct sockaddr *)&r2->saidx.src;
616 od = (struct sockaddr *)&r2->saidx.dst;
617 if (key_sockaddrcmp(os, osrc, 0) ||
618 key_sockaddrcmp(od, odst, 0))
629 sp->lastused = tv.tv_sec;
636 * allocating an SA entry for an *OUTBOUND* packet.
637 * checking each request entries in SP, and acquire an SA if need.
638 * OUT: 0: there are valid requests.
639 * ENOENT: policy may be valid, but SA with REQUIRE is on acquiring.
642 key_checkrequest(isr, saidx)
643 struct ipsecrequest *isr;
644 struct secasindex *saidx;
650 if (isr == NULL || saidx == NULL)
651 panic("key_checkrequest: NULL pointer is passed.\n");
654 switch (saidx->mode) {
655 case IPSEC_MODE_TRANSPORT:
656 case IPSEC_MODE_TUNNEL:
660 panic("key_checkrequest: Invalid policy defined.\n");
663 /* get current level */
664 level = ipsec_get_reqlevel(isr);
668 * We do allocate new SA only if the state of SA in the holder is
669 * SADB_SASTATE_DEAD. The SA for outbound must be the oldest.
671 if (isr->sav != NULL) {
672 if (isr->sav->sah == NULL)
673 panic("key_checkrequest: sah is null.\n");
674 if (isr->sav == (struct secasvar *)LIST_FIRST(
675 &isr->sav->sah->savtree[SADB_SASTATE_DEAD])) {
676 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
677 printf("DP checkrequest calls free SA:%p\n",
679 key_freesav(isr->sav);
685 * we free any SA stashed in the IPsec request because a different
686 * SA may be involved each time this request is checked, either
687 * because new SAs are being configured, or this request is
688 * associated with an unconnected datagram socket, or this request
689 * is associated with a system default policy.
691 * The operation may have negative impact to performance. We may
692 * want to check cached SA carefully, rather than picking new SA
695 if (isr->sav != NULL) {
696 key_freesav(isr->sav);
702 * new SA allocation if no SA found.
703 * key_allocsa_policy should allocate the oldest SA available.
704 * See key_do_allocsa_policy(), and draft-jenkins-ipsec-rekeying-03.txt.
706 if (isr->sav == NULL)
707 isr->sav = key_allocsa_policy(saidx);
709 /* When there is SA. */
710 if (isr->sav != NULL)
714 if ((error = key_acquire(saidx, isr->sp)) != 0) {
715 /* XXX What should I do ? */
716 ipseclog((LOG_DEBUG, "key_checkrequest: error %d returned "
717 "from key_acquire.\n", error));
721 return level == IPSEC_LEVEL_REQUIRE ? ENOENT : 0;
725 * allocating a SA for policy entry from SAD.
726 * NOTE: searching SAD of aliving state.
727 * OUT: NULL: not found.
728 * others: found and return the pointer.
730 static struct secasvar *
731 key_allocsa_policy(saidx)
732 struct secasindex *saidx;
734 struct secashead *sah;
735 struct secasvar *sav;
736 u_int stateidx, state;
737 const u_int *saorder_state_valid;
740 LIST_FOREACH(sah, &sahtree, chain) {
741 if (sah->state == SADB_SASTATE_DEAD)
743 if (key_cmpsaidx(&sah->saidx, saidx, CMP_MODE_REQID))
752 * search a valid state list for outbound packet.
753 * This search order is important.
755 if (key_preferred_oldsa) {
756 saorder_state_valid = saorder_state_valid_prefer_old;
757 arraysize = _ARRAYLEN(saorder_state_valid_prefer_old);
759 saorder_state_valid = saorder_state_valid_prefer_new;
760 arraysize = _ARRAYLEN(saorder_state_valid_prefer_new);
763 for (stateidx = 0; stateidx < arraysize; stateidx++) {
765 state = saorder_state_valid[stateidx];
767 sav = key_do_allocsa_policy(sah, state);
776 * searching SAD with direction, protocol, mode and state.
777 * called by key_allocsa_policy().
780 * others : found, pointer to a SA.
782 static struct secasvar *
783 key_do_allocsa_policy(sah, state)
784 struct secashead *sah;
787 struct secasvar *sav, *nextsav, *candidate, *d;
792 for (sav = LIST_FIRST(&sah->savtree[state]);
796 nextsav = LIST_NEXT(sav, chain);
799 KEY_CHKSASTATE(sav->state, state, "key_do_allocsa_policy");
802 if (candidate == NULL) {
807 /* Which SA is the better ? */
810 if (candidate->lft_c == NULL || sav->lft_c == NULL)
811 panic("key_do_allocsa_policy: "
812 "lifetime_current is NULL.\n");
814 /* What the best method is to compare ? */
815 if (key_preferred_oldsa) {
816 if (candidate->lft_c->sadb_lifetime_addtime >
817 sav->lft_c->sadb_lifetime_addtime) {
824 /* prefered new sa rather than old sa */
825 if (candidate->lft_c->sadb_lifetime_addtime <
826 sav->lft_c->sadb_lifetime_addtime) {
833 * prepared to delete the SA when there is more
834 * suitable candidate and the lifetime of the SA is not
837 if (d->lft_c->sadb_lifetime_addtime != 0) {
838 struct mbuf *m, *result;
840 key_sa_chgstate(d, SADB_SASTATE_DEAD);
842 m = key_setsadbmsg(SADB_DELETE, 0,
843 d->sah->saidx.proto, 0, 0, d->refcnt - 1);
848 /* set sadb_address for saidx's. */
849 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
850 (struct sockaddr *)&d->sah->saidx.src,
851 d->sah->saidx.src.ss_len << 3,
857 /* set sadb_address for saidx's. */
858 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
859 (struct sockaddr *)&d->sah->saidx.src,
860 d->sah->saidx.src.ss_len << 3,
866 /* create SA extension */
867 m = key_setsadbsa(d);
872 if (result->m_len < sizeof(struct sadb_msg)) {
873 result = m_pullup(result,
874 sizeof(struct sadb_msg));
879 result->m_pkthdr.len = 0;
880 for (m = result; m; m = m->m_next)
881 result->m_pkthdr.len += m->m_len;
882 mtod(result, struct sadb_msg *)->sadb_msg_len =
883 PFKEY_UNIT64(result->m_pkthdr.len);
885 if (key_sendup_mbuf(NULL, result,
886 KEY_SENDUP_REGISTERED))
895 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
896 printf("DP allocsa_policy cause "
897 "refcnt++:%d SA:%p\n",
898 candidate->refcnt, candidate));
904 * allocating a SA entry for a *INBOUND* packet.
905 * Must call key_freesav() later.
906 * OUT: positive: pointer to a sav.
907 * NULL: not found, or error occured.
909 * In the comparison, source address will be ignored for RFC2401 conformance.
910 * To quote, from section 4.1:
911 * A security association is uniquely identified by a triple consisting
912 * of a Security Parameter Index (SPI), an IP Destination Address, and a
913 * security protocol (AH or ESP) identifier.
914 * Note that, however, we do need to keep source address in IPsec SA.
915 * IKE specification and PF_KEY specification do assume that we
916 * keep source address in IPsec SA. We see a tricky situation here.
919 key_allocsa(family, src, dst, proto, spi)
924 struct secashead *sah;
925 struct secasvar *sav;
926 u_int stateidx, state;
927 struct sockaddr_in sin;
928 struct sockaddr_in6 sin6;
930 const u_int *saorder_state_valid;
934 if (src == NULL || dst == NULL)
935 panic("key_allocsa: NULL pointer is passed.\n");
938 * when both systems employ similar strategy to use a SA.
939 * the search order is important even in the inbound case.
941 if (key_preferred_oldsa) {
942 saorder_state_valid = saorder_state_valid_prefer_old;
943 arraysize = _ARRAYLEN(saorder_state_valid_prefer_old);
945 saorder_state_valid = saorder_state_valid_prefer_new;
946 arraysize = _ARRAYLEN(saorder_state_valid_prefer_new);
951 * XXX: to be checked internal IP header somewhere. Also when
952 * IPsec tunnel packet is received. But ESP tunnel mode is
953 * encrypted so we can't check internal IP header.
955 s = splnet(); /*called from softclock()*/
956 LIST_FOREACH(sah, &sahtree, chain) {
958 * search a valid state list for inbound packet.
959 * the search order is not important.
961 for (stateidx = 0; stateidx < arraysize; stateidx++) {
962 state = saorder_state_valid[stateidx];
963 LIST_FOREACH(sav, &sah->savtree[state], chain) {
965 KEY_CHKSASTATE(sav->state, state, "key_allocsav");
966 if (proto != sav->sah->saidx.proto)
970 if (family != sav->sah->saidx.src.ss_family ||
971 family != sav->sah->saidx.dst.ss_family)
974 #if 0 /* don't check src */
975 /* check src address */
978 bzero(&sin, sizeof(sin));
979 sin.sin_family = AF_INET;
980 sin.sin_len = sizeof(sin);
981 bcopy(src, &sin.sin_addr,
982 sizeof(sin.sin_addr));
983 if (key_sockaddrcmp((struct sockaddr*)&sin,
984 (struct sockaddr *)&sav->sah->saidx.src, 0) != 0)
989 bzero(&sin6, sizeof(sin6));
990 sin6.sin6_family = AF_INET6;
991 sin6.sin6_len = sizeof(sin6);
992 bcopy(src, &sin6.sin6_addr,
993 sizeof(sin6.sin6_addr));
994 if (IN6_IS_SCOPE_LINKLOCAL(&sin6.sin6_addr)) {
995 /* kame fake scopeid */
997 ntohs(sin6.sin6_addr.s6_addr16[1]);
998 sin6.sin6_addr.s6_addr16[1] = 0;
1000 if (key_sockaddrcmp((struct sockaddr*)&sin6,
1001 (struct sockaddr *)&sav->sah->saidx.src, 0) != 0)
1005 ipseclog((LOG_DEBUG, "key_allocsa: "
1006 "unknown address family=%d.\n",
1012 /* check dst address */
1015 bzero(&sin, sizeof(sin));
1016 sin.sin_family = AF_INET;
1017 sin.sin_len = sizeof(sin);
1018 bcopy(dst, &sin.sin_addr,
1019 sizeof(sin.sin_addr));
1020 if (key_sockaddrcmp((struct sockaddr*)&sin,
1021 (struct sockaddr *)&sav->sah->saidx.dst, 0) != 0)
1026 bzero(&sin6, sizeof(sin6));
1027 sin6.sin6_family = AF_INET6;
1028 sin6.sin6_len = sizeof(sin6);
1029 bcopy(dst, &sin6.sin6_addr,
1030 sizeof(sin6.sin6_addr));
1031 if (IN6_IS_SCOPE_LINKLOCAL(&sin6.sin6_addr)) {
1032 /* kame fake scopeid */
1033 sin6.sin6_scope_id =
1034 ntohs(sin6.sin6_addr.s6_addr16[1]);
1035 sin6.sin6_addr.s6_addr16[1] = 0;
1037 if (key_sockaddrcmp((struct sockaddr*)&sin6,
1038 (struct sockaddr *)&sav->sah->saidx.dst, 0) != 0)
1042 ipseclog((LOG_DEBUG, "key_allocsa: "
1043 "unknown address family=%d.\n",
1060 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1061 printf("DP allocsa cause refcnt++:%d SA:%p\n",
1067 * Must be called after calling key_allocsp().
1068 * For both the packet without socket and key_freeso().
1072 struct secpolicy *sp;
1076 panic("key_freesp: NULL pointer is passed.\n");
1079 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1080 printf("DP freesp cause refcnt--:%d SP:%p\n",
1083 if (sp->refcnt == 0)
1090 * Must be called after calling key_allocsp().
1091 * For the packet with socket.
1099 panic("key_freeso: NULL pointer is passed.\n");
1101 switch (so->so_proto->pr_domain->dom_family) {
1105 struct inpcb *pcb = sotoinpcb(so);
1107 /* Does it have a PCB ? */
1110 key_freesp_so(&pcb->inp_sp->sp_in);
1111 key_freesp_so(&pcb->inp_sp->sp_out);
1118 #ifdef HAVE_NRL_INPCB
1119 struct inpcb *pcb = sotoinpcb(so);
1121 /* Does it have a PCB ? */
1124 key_freesp_so(&pcb->inp_sp->sp_in);
1125 key_freesp_so(&pcb->inp_sp->sp_out);
1127 struct in6pcb *pcb = sotoin6pcb(so);
1129 /* Does it have a PCB ? */
1132 key_freesp_so(&pcb->in6p_sp->sp_in);
1133 key_freesp_so(&pcb->in6p_sp->sp_out);
1139 ipseclog((LOG_DEBUG, "key_freeso: unknown address family=%d.\n",
1140 so->so_proto->pr_domain->dom_family));
1149 struct secpolicy **sp;
1152 if (sp == NULL || *sp == NULL)
1153 panic("key_freesp_so: sp == NULL\n");
1155 switch ((*sp)->policy) {
1156 case IPSEC_POLICY_IPSEC:
1157 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1158 printf("DP freeso calls free SP:%p\n", *sp));
1162 case IPSEC_POLICY_ENTRUST:
1163 case IPSEC_POLICY_BYPASS:
1166 panic("key_freesp_so: Invalid policy found %d", (*sp)->policy);
1173 * Must be called after calling key_allocsa().
1174 * This function is called by key_freesp() to free some SA allocated
1179 struct secasvar *sav;
1183 panic("key_freesav: NULL pointer is passed.\n");
1186 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1187 printf("DP freesav cause refcnt--:%d SA:%p SPI %u\n",
1188 sav->refcnt, sav, (u_int32_t)ntohl(sav->spi)));
1190 if (sav->refcnt == 0)
1196 /* %%% SPD management */
1198 * free security policy entry.
1202 struct secpolicy *sp;
1208 panic("key_delsp: NULL pointer is passed.\n");
1210 sp->state = IPSEC_SPSTATE_DEAD;
1213 return; /* can't free */
1215 s = splnet(); /*called from softclock()*/
1216 /* remove from SP index */
1217 if (__LIST_CHAINED(sp))
1218 LIST_REMOVE(sp, chain);
1221 struct ipsecrequest *isr = sp->req, *nextisr;
1223 while (isr != NULL) {
1224 if (isr->sav != NULL) {
1225 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1226 printf("DP delsp calls free SA:%p\n",
1228 key_freesav(isr->sav);
1232 nextisr = isr->next;
1238 keydb_delsecpolicy(sp);
1247 * OUT: NULL : not found
1248 * others : found, pointer to a SP.
1250 static struct secpolicy *
1252 struct secpolicyindex *spidx;
1254 struct secpolicy *sp;
1258 panic("key_getsp: NULL pointer is passed.\n");
1260 LIST_FOREACH(sp, &sptree[spidx->dir], chain) {
1261 if (sp->state == IPSEC_SPSTATE_DEAD)
1263 if (key_cmpspidx_exactly(spidx, &sp->spidx)) {
1274 * OUT: NULL : not found
1275 * others : found, pointer to a SP.
1277 static struct secpolicy *
1281 struct secpolicy *sp;
1283 LIST_FOREACH(sp, &sptree[IPSEC_DIR_INBOUND], chain) {
1284 if (sp->state == IPSEC_SPSTATE_DEAD)
1292 LIST_FOREACH(sp, &sptree[IPSEC_DIR_OUTBOUND], chain) {
1293 if (sp->state == IPSEC_SPSTATE_DEAD)
1307 struct secpolicy *newsp = NULL;
1309 newsp = keydb_newsecpolicy();
1320 * create secpolicy structure from sadb_x_policy structure.
1321 * NOTE: `state', `secpolicyindex' in secpolicy structure are not set,
1322 * so must be set properly later.
1325 key_msg2sp(xpl0, len, error)
1326 struct sadb_x_policy *xpl0;
1330 struct secpolicy *newsp;
1334 panic("key_msg2sp: NULL pointer was passed.\n");
1335 if (len < sizeof(*xpl0))
1336 panic("key_msg2sp: invalid length.\n");
1337 if (len != PFKEY_EXTLEN(xpl0)) {
1338 ipseclog((LOG_DEBUG, "key_msg2sp: Invalid msg length.\n"));
1343 if ((newsp = key_newsp()) == NULL) {
1348 newsp->spidx.dir = xpl0->sadb_x_policy_dir;
1349 newsp->policy = xpl0->sadb_x_policy_type;
1352 switch (xpl0->sadb_x_policy_type) {
1353 case IPSEC_POLICY_DISCARD:
1354 case IPSEC_POLICY_NONE:
1355 case IPSEC_POLICY_ENTRUST:
1356 case IPSEC_POLICY_BYPASS:
1360 case IPSEC_POLICY_IPSEC:
1363 struct sadb_x_ipsecrequest *xisr;
1364 struct ipsecrequest **p_isr = &newsp->req;
1366 /* validity check */
1367 if (PFKEY_EXTLEN(xpl0) < sizeof(*xpl0)) {
1368 ipseclog((LOG_DEBUG,
1369 "key_msg2sp: Invalid msg length.\n"));
1375 tlen = PFKEY_EXTLEN(xpl0) - sizeof(*xpl0);
1376 xisr = (struct sadb_x_ipsecrequest *)(xpl0 + 1);
1381 if (xisr->sadb_x_ipsecrequest_len < sizeof(*xisr)) {
1382 ipseclog((LOG_DEBUG, "key_msg2sp: "
1383 "invalid ipsecrequest length.\n"));
1389 /* allocate request buffer */
1390 KMALLOC(*p_isr, struct ipsecrequest *, sizeof(**p_isr));
1391 if ((*p_isr) == NULL) {
1392 ipseclog((LOG_DEBUG,
1393 "key_msg2sp: No more memory.\n"));
1398 bzero(*p_isr, sizeof(**p_isr));
1401 (*p_isr)->next = NULL;
1403 switch (xisr->sadb_x_ipsecrequest_proto) {
1406 case IPPROTO_IPCOMP:
1409 ipseclog((LOG_DEBUG,
1410 "key_msg2sp: invalid proto type=%u\n",
1411 xisr->sadb_x_ipsecrequest_proto));
1413 *error = EPROTONOSUPPORT;
1416 (*p_isr)->saidx.proto = xisr->sadb_x_ipsecrequest_proto;
1418 switch (xisr->sadb_x_ipsecrequest_mode) {
1419 case IPSEC_MODE_TRANSPORT:
1420 case IPSEC_MODE_TUNNEL:
1422 case IPSEC_MODE_ANY:
1424 ipseclog((LOG_DEBUG,
1425 "key_msg2sp: invalid mode=%u\n",
1426 xisr->sadb_x_ipsecrequest_mode));
1431 (*p_isr)->saidx.mode = xisr->sadb_x_ipsecrequest_mode;
1433 switch (xisr->sadb_x_ipsecrequest_level) {
1434 case IPSEC_LEVEL_DEFAULT:
1435 case IPSEC_LEVEL_USE:
1436 case IPSEC_LEVEL_REQUIRE:
1438 case IPSEC_LEVEL_UNIQUE:
1439 /* validity check */
1441 * If range violation of reqid, kernel will
1442 * update it, don't refuse it.
1444 if (xisr->sadb_x_ipsecrequest_reqid
1445 > IPSEC_MANUAL_REQID_MAX) {
1446 ipseclog((LOG_DEBUG,
1447 "key_msg2sp: reqid=%d range "
1448 "violation, updated by kernel.\n",
1449 xisr->sadb_x_ipsecrequest_reqid));
1450 xisr->sadb_x_ipsecrequest_reqid = 0;
1453 /* allocate new reqid id if reqid is zero. */
1454 if (xisr->sadb_x_ipsecrequest_reqid == 0) {
1456 if ((reqid = key_newreqid()) == 0) {
1461 (*p_isr)->saidx.reqid = reqid;
1462 xisr->sadb_x_ipsecrequest_reqid = reqid;
1464 /* set it for manual keying. */
1465 (*p_isr)->saidx.reqid =
1466 xisr->sadb_x_ipsecrequest_reqid;
1471 ipseclog((LOG_DEBUG, "key_msg2sp: invalid level=%u\n",
1472 xisr->sadb_x_ipsecrequest_level));
1477 (*p_isr)->level = xisr->sadb_x_ipsecrequest_level;
1479 /* set IP addresses if there */
1480 if (xisr->sadb_x_ipsecrequest_len > sizeof(*xisr)) {
1481 struct sockaddr *paddr;
1483 paddr = (struct sockaddr *)(xisr + 1);
1485 /* validity check */
1487 > sizeof((*p_isr)->saidx.src)) {
1488 ipseclog((LOG_DEBUG, "key_msg2sp: invalid request "
1489 "address length.\n"));
1494 bcopy(paddr, &(*p_isr)->saidx.src,
1497 paddr = (struct sockaddr *)((caddr_t)paddr
1500 /* validity check */
1502 > sizeof((*p_isr)->saidx.dst)) {
1503 ipseclog((LOG_DEBUG, "key_msg2sp: invalid request "
1504 "address length.\n"));
1509 bcopy(paddr, &(*p_isr)->saidx.dst,
1513 (*p_isr)->sav = NULL;
1514 (*p_isr)->sp = newsp;
1516 /* initialization for the next. */
1517 p_isr = &(*p_isr)->next;
1518 tlen -= xisr->sadb_x_ipsecrequest_len;
1520 /* validity check */
1522 ipseclog((LOG_DEBUG, "key_msg2sp: becoming tlen < 0.\n"));
1528 xisr = (struct sadb_x_ipsecrequest *)((caddr_t)xisr
1529 + xisr->sadb_x_ipsecrequest_len);
1534 ipseclog((LOG_DEBUG, "key_msg2sp: invalid policy type.\n"));
1547 static u_int32_t auto_reqid = IPSEC_MANUAL_REQID_MAX + 1;
1549 auto_reqid = (auto_reqid == ~0
1550 ? IPSEC_MANUAL_REQID_MAX + 1 : auto_reqid + 1);
1552 /* XXX should be unique check */
1558 * copy secpolicy struct to sadb_x_policy structure indicated.
1562 struct secpolicy *sp;
1564 struct sadb_x_policy *xpl;
1571 panic("key_sp2msg: NULL pointer was passed.\n");
1573 tlen = key_getspreqmsglen(sp);
1575 m = key_alloc_mbuf(tlen);
1576 if (!m || m->m_next) { /*XXX*/
1584 xpl = mtod(m, struct sadb_x_policy *);
1587 xpl->sadb_x_policy_len = PFKEY_UNIT64(tlen);
1588 xpl->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
1589 xpl->sadb_x_policy_type = sp->policy;
1590 xpl->sadb_x_policy_dir = sp->spidx.dir;
1591 xpl->sadb_x_policy_id = sp->id;
1592 p = (caddr_t)xpl + sizeof(*xpl);
1594 /* if is the policy for ipsec ? */
1595 if (sp->policy == IPSEC_POLICY_IPSEC) {
1596 struct sadb_x_ipsecrequest *xisr;
1597 struct ipsecrequest *isr;
1599 for (isr = sp->req; isr != NULL; isr = isr->next) {
1601 xisr = (struct sadb_x_ipsecrequest *)p;
1603 xisr->sadb_x_ipsecrequest_proto = isr->saidx.proto;
1604 xisr->sadb_x_ipsecrequest_mode = isr->saidx.mode;
1605 xisr->sadb_x_ipsecrequest_level = isr->level;
1606 xisr->sadb_x_ipsecrequest_reqid = isr->saidx.reqid;
1609 bcopy(&isr->saidx.src, p, isr->saidx.src.ss_len);
1610 p += isr->saidx.src.ss_len;
1611 bcopy(&isr->saidx.dst, p, isr->saidx.dst.ss_len);
1612 p += isr->saidx.src.ss_len;
1614 xisr->sadb_x_ipsecrequest_len =
1615 PFKEY_ALIGN8(sizeof(*xisr)
1616 + isr->saidx.src.ss_len
1617 + isr->saidx.dst.ss_len);
1624 /* m will not be freed nor modified */
1625 static struct mbuf *
1627 key_gather_mbuf(struct mbuf *m, const struct sadb_msghdr *mhp,
1628 int ndeep, int nitem, ...)
1630 key_gather_mbuf(m, mhp, ndeep, nitem, va_alist)
1632 const struct sadb_msghdr *mhp;
1641 struct mbuf *result = NULL, *n;
1644 if (m == NULL || mhp == NULL)
1645 panic("null pointer passed to key_gather");
1647 va_start(ap, nitem);
1648 for (i = 0; i < nitem; i++) {
1649 idx = va_arg(ap, int);
1650 if (idx < 0 || idx > SADB_EXT_MAX)
1652 /* don't attempt to pull empty extension */
1653 if (idx == SADB_EXT_RESERVED && mhp->msg == NULL)
1655 if (idx != SADB_EXT_RESERVED &&
1656 (mhp->ext[idx] == NULL || mhp->extlen[idx] == 0))
1659 if (idx == SADB_EXT_RESERVED) {
1660 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
1663 panic("assumption failed");
1665 MGETHDR(n, M_DONTWAIT, MT_DATA);
1670 m_copydata(m, 0, sizeof(struct sadb_msg),
1672 } else if (i < ndeep) {
1673 len = mhp->extlen[idx];
1674 n = key_alloc_mbuf(len);
1675 if (!n || n->m_next) { /*XXX*/
1680 m_copydata(m, mhp->extoff[idx], mhp->extlen[idx],
1683 n = m_copym(m, mhp->extoff[idx], mhp->extlen[idx],
1696 if ((result->m_flags & M_PKTHDR) != 0) {
1697 result->m_pkthdr.len = 0;
1698 for (n = result; n; n = n->m_next)
1699 result->m_pkthdr.len += n->m_len;
1710 * SADB_X_SPDADD, SADB_X_SPDSETIDX or SADB_X_SPDUPDATE processing
1711 * add a entry to SP database, when received
1712 * <base, address(SD), (lifetime(H),) policy>
1714 * Adding to SP database,
1716 * <base, address(SD), (lifetime(H),) policy>
1717 * to the socket which was send.
1719 * SPDADD set a unique policy entry.
1720 * SPDSETIDX like SPDADD without a part of policy requests.
1721 * SPDUPDATE replace a unique policy entry.
1723 * m will always be freed.
1726 key_spdadd(so, m, mhp)
1729 const struct sadb_msghdr *mhp;
1731 struct sadb_address *src0, *dst0;
1732 struct sadb_x_policy *xpl0, *xpl;
1733 struct sadb_lifetime *lft = NULL;
1734 struct secpolicyindex spidx;
1735 struct secpolicy *newsp;
1740 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
1741 panic("key_spdadd: NULL pointer is passed.\n");
1743 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
1744 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
1745 mhp->ext[SADB_X_EXT_POLICY] == NULL) {
1746 ipseclog((LOG_DEBUG, "key_spdadd: invalid message is passed.\n"));
1747 return key_senderror(so, m, EINVAL);
1749 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
1750 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
1751 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
1752 ipseclog((LOG_DEBUG, "key_spdadd: invalid message is passed.\n"));
1753 return key_senderror(so, m, EINVAL);
1755 if (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL) {
1756 if (mhp->extlen[SADB_EXT_LIFETIME_HARD]
1757 < sizeof(struct sadb_lifetime)) {
1758 ipseclog((LOG_DEBUG, "key_spdadd: invalid message is passed.\n"));
1759 return key_senderror(so, m, EINVAL);
1761 lft = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_HARD];
1764 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
1765 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
1766 xpl0 = (struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY];
1769 /* XXX boundary check against sa_len */
1770 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
1773 src0->sadb_address_prefixlen,
1774 dst0->sadb_address_prefixlen,
1775 src0->sadb_address_proto,
1778 /* checking the direciton. */
1779 switch (xpl0->sadb_x_policy_dir) {
1780 case IPSEC_DIR_INBOUND:
1781 case IPSEC_DIR_OUTBOUND:
1784 ipseclog((LOG_DEBUG, "key_spdadd: Invalid SP direction.\n"));
1785 mhp->msg->sadb_msg_errno = EINVAL;
1790 /* key_spdadd() accepts DISCARD, NONE and IPSEC. */
1791 if (xpl0->sadb_x_policy_type == IPSEC_POLICY_ENTRUST
1792 || xpl0->sadb_x_policy_type == IPSEC_POLICY_BYPASS) {
1793 ipseclog((LOG_DEBUG, "key_spdadd: Invalid policy type.\n"));
1794 return key_senderror(so, m, EINVAL);
1797 /* policy requests are mandatory when action is ipsec. */
1798 if (mhp->msg->sadb_msg_type != SADB_X_SPDSETIDX
1799 && xpl0->sadb_x_policy_type == IPSEC_POLICY_IPSEC
1800 && mhp->extlen[SADB_X_EXT_POLICY] <= sizeof(*xpl0)) {
1801 ipseclog((LOG_DEBUG, "key_spdadd: some policy requests part required.\n"));
1802 return key_senderror(so, m, EINVAL);
1806 * checking there is SP already or not.
1807 * SPDUPDATE doesn't depend on whether there is a SP or not.
1808 * If the type is either SPDADD or SPDSETIDX AND a SP is found,
1811 newsp = key_getsp(&spidx);
1812 if (mhp->msg->sadb_msg_type == SADB_X_SPDUPDATE) {
1814 newsp->state = IPSEC_SPSTATE_DEAD;
1818 if (newsp != NULL) {
1820 ipseclog((LOG_DEBUG, "key_spdadd: a SP entry exists already.\n"));
1821 return key_senderror(so, m, EEXIST);
1825 /* allocation new SP entry */
1826 if ((newsp = key_msg2sp(xpl0, PFKEY_EXTLEN(xpl0), &error)) == NULL) {
1827 return key_senderror(so, m, error);
1830 if ((newsp->id = key_getnewspid()) == 0) {
1831 keydb_delsecpolicy(newsp);
1832 return key_senderror(so, m, ENOBUFS);
1835 /* XXX boundary check against sa_len */
1836 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
1839 src0->sadb_address_prefixlen,
1840 dst0->sadb_address_prefixlen,
1841 src0->sadb_address_proto,
1844 /* sanity check on addr pair */
1845 if (((struct sockaddr *)(src0 + 1))->sa_family !=
1846 ((struct sockaddr *)(dst0+ 1))->sa_family) {
1847 keydb_delsecpolicy(newsp);
1848 return key_senderror(so, m, EINVAL);
1850 if (((struct sockaddr *)(src0 + 1))->sa_len !=
1851 ((struct sockaddr *)(dst0+ 1))->sa_len) {
1852 keydb_delsecpolicy(newsp);
1853 return key_senderror(so, m, EINVAL);
1856 if (newsp->req && newsp->req->saidx.src.ss_family) {
1857 struct sockaddr *sa;
1858 sa = (struct sockaddr *)(src0 + 1);
1859 if (sa->sa_family != newsp->req->saidx.src.ss_family) {
1860 keydb_delsecpolicy(newsp);
1861 return key_senderror(so, m, EINVAL);
1864 if (newsp->req && newsp->req->saidx.dst.ss_family) {
1865 struct sockaddr *sa;
1866 sa = (struct sockaddr *)(dst0 + 1);
1867 if (sa->sa_family != newsp->req->saidx.dst.ss_family) {
1868 keydb_delsecpolicy(newsp);
1869 return key_senderror(so, m, EINVAL);
1875 newsp->created = tv.tv_sec;
1876 newsp->lastused = tv.tv_sec;
1877 newsp->lifetime = lft ? lft->sadb_lifetime_addtime : 0;
1878 newsp->validtime = lft ? lft->sadb_lifetime_usetime : 0;
1880 newsp->refcnt = 1; /* do not reclaim until I say I do */
1881 newsp->state = IPSEC_SPSTATE_ALIVE;
1882 LIST_INSERT_TAIL(&sptree[newsp->spidx.dir], newsp, secpolicy, chain);
1884 /* delete the entry in spacqtree */
1885 if (mhp->msg->sadb_msg_type == SADB_X_SPDUPDATE) {
1886 struct secspacq *spacq;
1887 if ((spacq = key_getspacq(&spidx)) != NULL) {
1888 /* reset counter in order to deletion by timehandler. */
1890 spacq->created = tv.tv_sec;
1896 struct mbuf *n, *mpolicy;
1897 struct sadb_msg *newmsg;
1900 /* create new sadb_msg to reply. */
1902 n = key_gather_mbuf(m, mhp, 2, 5, SADB_EXT_RESERVED,
1903 SADB_X_EXT_POLICY, SADB_EXT_LIFETIME_HARD,
1904 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
1906 n = key_gather_mbuf(m, mhp, 2, 4, SADB_EXT_RESERVED,
1908 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
1911 return key_senderror(so, m, ENOBUFS);
1913 if (n->m_len < sizeof(*newmsg)) {
1914 n = m_pullup(n, sizeof(*newmsg));
1916 return key_senderror(so, m, ENOBUFS);
1918 newmsg = mtod(n, struct sadb_msg *);
1919 newmsg->sadb_msg_errno = 0;
1920 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
1923 mpolicy = m_pulldown(n, PFKEY_ALIGN8(sizeof(struct sadb_msg)),
1924 sizeof(*xpl), &off);
1925 if (mpolicy == NULL) {
1926 /* n is already freed */
1927 return key_senderror(so, m, ENOBUFS);
1929 xpl = (struct sadb_x_policy *)(mtod(mpolicy, caddr_t) + off);
1930 if (xpl->sadb_x_policy_exttype != SADB_X_EXT_POLICY) {
1932 return key_senderror(so, m, EINVAL);
1934 xpl->sadb_x_policy_id = newsp->id;
1937 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
1942 * get new policy id.
1950 u_int32_t newid = 0;
1951 int count = key_spi_trycnt; /* XXX */
1952 struct secpolicy *sp;
1954 /* when requesting to allocate spi ranged */
1956 newid = (policy_id = (policy_id == ~0 ? 1 : policy_id + 1));
1958 if ((sp = key_getspbyid(newid)) == NULL)
1964 if (count == 0 || newid == 0) {
1965 ipseclog((LOG_DEBUG, "key_getnewspid: to allocate policy id is failed.\n"));
1973 * SADB_SPDDELETE processing
1975 * <base, address(SD), policy(*)>
1976 * from the user(?), and set SADB_SASTATE_DEAD,
1978 * <base, address(SD), policy(*)>
1980 * policy(*) including direction of policy.
1982 * m will always be freed.
1985 key_spddelete(so, m, mhp)
1988 const struct sadb_msghdr *mhp;
1990 struct sadb_address *src0, *dst0;
1991 struct sadb_x_policy *xpl0;
1992 struct secpolicyindex spidx;
1993 struct secpolicy *sp;
1996 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
1997 panic("key_spddelete: NULL pointer is passed.\n");
1999 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
2000 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
2001 mhp->ext[SADB_X_EXT_POLICY] == NULL) {
2002 ipseclog((LOG_DEBUG, "key_spddelete: invalid message is passed.\n"));
2003 return key_senderror(so, m, EINVAL);
2005 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
2006 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
2007 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2008 ipseclog((LOG_DEBUG, "key_spddelete: invalid message is passed.\n"));
2009 return key_senderror(so, m, EINVAL);
2012 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
2013 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
2014 xpl0 = (struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY];
2017 /* XXX boundary check against sa_len */
2018 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
2021 src0->sadb_address_prefixlen,
2022 dst0->sadb_address_prefixlen,
2023 src0->sadb_address_proto,
2026 /* checking the direciton. */
2027 switch (xpl0->sadb_x_policy_dir) {
2028 case IPSEC_DIR_INBOUND:
2029 case IPSEC_DIR_OUTBOUND:
2032 ipseclog((LOG_DEBUG, "key_spddelete: Invalid SP direction.\n"));
2033 return key_senderror(so, m, EINVAL);
2036 /* Is there SP in SPD ? */
2037 if ((sp = key_getsp(&spidx)) == NULL) {
2038 ipseclog((LOG_DEBUG, "key_spddelete: no SP found.\n"));
2039 return key_senderror(so, m, EINVAL);
2042 /* save policy id to buffer to be returned. */
2043 xpl0->sadb_x_policy_id = sp->id;
2045 sp->state = IPSEC_SPSTATE_DEAD;
2050 struct sadb_msg *newmsg;
2052 /* create new sadb_msg to reply. */
2053 n = key_gather_mbuf(m, mhp, 1, 4, SADB_EXT_RESERVED,
2054 SADB_X_EXT_POLICY, SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
2056 return key_senderror(so, m, ENOBUFS);
2058 newmsg = mtod(n, struct sadb_msg *);
2059 newmsg->sadb_msg_errno = 0;
2060 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
2063 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
2068 * SADB_SPDDELETE2 processing
2071 * from the user(?), and set SADB_SASTATE_DEAD,
2075 * policy(*) including direction of policy.
2077 * m will always be freed.
2080 key_spddelete2(so, m, mhp)
2083 const struct sadb_msghdr *mhp;
2086 struct secpolicy *sp;
2089 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
2090 panic("key_spddelete2: NULL pointer is passed.\n");
2092 if (mhp->ext[SADB_X_EXT_POLICY] == NULL ||
2093 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2094 ipseclog((LOG_DEBUG, "key_spddelete2: invalid message is passed.\n"));
2095 key_senderror(so, m, EINVAL);
2099 id = ((struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY])->sadb_x_policy_id;
2101 /* Is there SP in SPD ? */
2102 if ((sp = key_getspbyid(id)) == NULL) {
2103 ipseclog((LOG_DEBUG, "key_spddelete2: no SP found id:%u.\n", id));
2104 key_senderror(so, m, EINVAL);
2107 sp->state = IPSEC_SPSTATE_DEAD;
2111 struct mbuf *n, *nn;
2112 struct sadb_msg *newmsg;
2115 /* create new sadb_msg to reply. */
2116 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
2119 return key_senderror(so, m, ENOBUFS);
2120 MGETHDR(n, M_DONTWAIT, MT_DATA);
2121 if (n && len > MHLEN) {
2122 MCLGET(n, M_DONTWAIT);
2123 if ((n->m_flags & M_EXT) == 0) {
2129 return key_senderror(so, m, ENOBUFS);
2135 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
2136 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
2140 panic("length inconsistency in key_spddelete2");
2143 n->m_next = m_copym(m, mhp->extoff[SADB_X_EXT_POLICY],
2144 mhp->extlen[SADB_X_EXT_POLICY], M_DONTWAIT);
2147 return key_senderror(so, m, ENOBUFS);
2150 n->m_pkthdr.len = 0;
2151 for (nn = n; nn; nn = nn->m_next)
2152 n->m_pkthdr.len += nn->m_len;
2154 newmsg = mtod(n, struct sadb_msg *);
2155 newmsg->sadb_msg_errno = 0;
2156 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
2159 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
2164 * SADB_X_GET processing
2169 * <base, address(SD), policy>
2171 * policy(*) including direction of policy.
2173 * m will always be freed.
2176 key_spdget(so, m, mhp)
2179 const struct sadb_msghdr *mhp;
2182 struct secpolicy *sp;
2186 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
2187 panic("key_spdget: NULL pointer is passed.\n");
2189 if (mhp->ext[SADB_X_EXT_POLICY] == NULL ||
2190 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2191 ipseclog((LOG_DEBUG, "key_spdget: invalid message is passed.\n"));
2192 return key_senderror(so, m, EINVAL);
2195 id = ((struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY])->sadb_x_policy_id;
2197 /* Is there SP in SPD ? */
2198 if ((sp = key_getspbyid(id)) == NULL) {
2199 ipseclog((LOG_DEBUG, "key_spdget: no SP found id:%u.\n", id));
2200 return key_senderror(so, m, ENOENT);
2203 n = key_setdumpsp(sp, SADB_X_SPDGET, 0, mhp->msg->sadb_msg_pid);
2206 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
2208 return key_senderror(so, m, ENOBUFS);
2212 * SADB_X_SPDACQUIRE processing.
2213 * Acquire policy and SA(s) for a *OUTBOUND* packet.
2216 * to KMD, and expect to receive
2217 * <base> with SADB_X_SPDACQUIRE if error occured,
2220 * with SADB_X_SPDUPDATE from KMD by PF_KEY.
2221 * policy(*) is without policy requests.
2224 * others: error number
2228 struct secpolicy *sp;
2230 struct mbuf *result = NULL, *m;
2231 struct secspacq *newspacq;
2236 panic("key_spdacquire: NULL pointer is passed.\n");
2237 if (sp->req != NULL)
2238 panic("key_spdacquire: called but there is request.\n");
2239 if (sp->policy != IPSEC_POLICY_IPSEC)
2240 panic("key_spdacquire: policy mismathed. IPsec is expected.\n");
2242 /* get a entry to check whether sent message or not. */
2243 if ((newspacq = key_getspacq(&sp->spidx)) != NULL) {
2244 if (key_blockacq_count < newspacq->count) {
2245 /* reset counter and do send message. */
2246 newspacq->count = 0;
2248 /* increment counter and do nothing. */
2253 /* make new entry for blocking to send SADB_ACQUIRE. */
2254 if ((newspacq = key_newspacq(&sp->spidx)) == NULL)
2257 /* add to acqtree */
2258 LIST_INSERT_HEAD(&spacqtree, newspacq, chain);
2261 /* create new sadb_msg to reply. */
2262 m = key_setsadbmsg(SADB_X_SPDACQUIRE, 0, 0, 0, 0, 0);
2269 result->m_pkthdr.len = 0;
2270 for (m = result; m; m = m->m_next)
2271 result->m_pkthdr.len += m->m_len;
2273 mtod(result, struct sadb_msg *)->sadb_msg_len =
2274 PFKEY_UNIT64(result->m_pkthdr.len);
2276 return key_sendup_mbuf(NULL, m, KEY_SENDUP_REGISTERED);
2285 * SADB_SPDFLUSH processing
2288 * from the user, and free all entries in secpctree.
2292 * NOTE: what to do is only marking SADB_SASTATE_DEAD.
2294 * m will always be freed.
2297 key_spdflush(so, m, mhp)
2300 const struct sadb_msghdr *mhp;
2302 struct sadb_msg *newmsg;
2303 struct secpolicy *sp;
2307 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
2308 panic("key_spdflush: NULL pointer is passed.\n");
2310 if (m->m_len != PFKEY_ALIGN8(sizeof(struct sadb_msg)))
2311 return key_senderror(so, m, EINVAL);
2313 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2314 LIST_FOREACH(sp, &sptree[dir], chain) {
2315 sp->state = IPSEC_SPSTATE_DEAD;
2319 if (sizeof(struct sadb_msg) > m->m_len + M_TRAILINGSPACE(m)) {
2320 ipseclog((LOG_DEBUG, "key_spdflush: No more memory.\n"));
2321 return key_senderror(so, m, ENOBUFS);
2327 m->m_pkthdr.len = m->m_len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
2328 newmsg = mtod(m, struct sadb_msg *);
2329 newmsg->sadb_msg_errno = 0;
2330 newmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
2332 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
2336 * SADB_SPDDUMP processing
2339 * from the user, and dump all SP leaves
2344 * m will always be freed.
2347 key_spddump(so, m, mhp)
2350 const struct sadb_msghdr *mhp;
2352 struct secpolicy *sp;
2358 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
2359 panic("key_spddump: NULL pointer is passed.\n");
2361 /* search SPD entry and get buffer size. */
2363 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2364 LIST_FOREACH(sp, &sptree[dir], chain) {
2370 return key_senderror(so, m, ENOENT);
2372 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2373 LIST_FOREACH(sp, &sptree[dir], chain) {
2375 n = key_setdumpsp(sp, SADB_X_SPDDUMP, cnt,
2376 mhp->msg->sadb_msg_pid);
2379 key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
2387 static struct mbuf *
2388 key_setdumpsp(sp, type, seq, pid)
2389 struct secpolicy *sp;
2393 struct mbuf *result = NULL, *m;
2395 m = key_setsadbmsg(type, 0, SADB_SATYPE_UNSPEC, seq, pid, sp->refcnt);
2400 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
2401 (struct sockaddr *)&sp->spidx.src, sp->spidx.prefs,
2402 sp->spidx.ul_proto);
2407 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
2408 (struct sockaddr *)&sp->spidx.dst, sp->spidx.prefd,
2409 sp->spidx.ul_proto);
2419 if ((result->m_flags & M_PKTHDR) == 0)
2422 if (result->m_len < sizeof(struct sadb_msg)) {
2423 result = m_pullup(result, sizeof(struct sadb_msg));
2428 result->m_pkthdr.len = 0;
2429 for (m = result; m; m = m->m_next)
2430 result->m_pkthdr.len += m->m_len;
2432 mtod(result, struct sadb_msg *)->sadb_msg_len =
2433 PFKEY_UNIT64(result->m_pkthdr.len);
2443 * get PFKEY message length for security policy and request.
2446 key_getspreqmsglen(sp)
2447 struct secpolicy *sp;
2451 tlen = sizeof(struct sadb_x_policy);
2453 /* if is the policy for ipsec ? */
2454 if (sp->policy != IPSEC_POLICY_IPSEC)
2457 /* get length of ipsec requests */
2459 struct ipsecrequest *isr;
2462 for (isr = sp->req; isr != NULL; isr = isr->next) {
2463 len = sizeof(struct sadb_x_ipsecrequest)
2464 + isr->saidx.src.ss_len
2465 + isr->saidx.dst.ss_len;
2467 tlen += PFKEY_ALIGN8(len);
2475 * SADB_SPDEXPIRE processing
2477 * <base, address(SD), lifetime(CH), policy>
2481 * others : error number
2485 struct secpolicy *sp;
2488 struct mbuf *result = NULL, *m;
2491 struct sadb_lifetime *lt;
2493 /* XXX: Why do we lock ? */
2494 s = splnet(); /*called from softclock()*/
2498 panic("key_spdexpire: NULL pointer is passed.\n");
2500 /* set msg header */
2501 m = key_setsadbmsg(SADB_X_SPDEXPIRE, 0, 0, 0, 0, 0);
2508 /* create lifetime extension (current and hard) */
2509 len = PFKEY_ALIGN8(sizeof(*lt)) * 2;
2510 m = key_alloc_mbuf(len);
2511 if (!m || m->m_next) { /*XXX*/
2517 bzero(mtod(m, caddr_t), len);
2518 lt = mtod(m, struct sadb_lifetime *);
2519 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
2520 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
2521 lt->sadb_lifetime_allocations = 0;
2522 lt->sadb_lifetime_bytes = 0;
2523 lt->sadb_lifetime_addtime = sp->created;
2524 lt->sadb_lifetime_usetime = sp->lastused;
2525 lt = (struct sadb_lifetime *)(mtod(m, caddr_t) + len / 2);
2526 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
2527 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
2528 lt->sadb_lifetime_allocations = 0;
2529 lt->sadb_lifetime_bytes = 0;
2530 lt->sadb_lifetime_addtime = sp->lifetime;
2531 lt->sadb_lifetime_usetime = sp->validtime;
2534 /* set sadb_address for source */
2535 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
2536 (struct sockaddr *)&sp->spidx.src,
2537 sp->spidx.prefs, sp->spidx.ul_proto);
2544 /* set sadb_address for destination */
2545 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
2546 (struct sockaddr *)&sp->spidx.dst,
2547 sp->spidx.prefd, sp->spidx.ul_proto);
2562 if ((result->m_flags & M_PKTHDR) == 0) {
2567 if (result->m_len < sizeof(struct sadb_msg)) {
2568 result = m_pullup(result, sizeof(struct sadb_msg));
2569 if (result == NULL) {
2575 result->m_pkthdr.len = 0;
2576 for (m = result; m; m = m->m_next)
2577 result->m_pkthdr.len += m->m_len;
2579 mtod(result, struct sadb_msg *)->sadb_msg_len =
2580 PFKEY_UNIT64(result->m_pkthdr.len);
2582 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
2591 /* %%% SAD management */
2593 * allocating a memory for new SA head, and copy from the values of mhp.
2594 * OUT: NULL : failure due to the lack of memory.
2595 * others : pointer to new SA head.
2597 static struct secashead *
2599 struct secasindex *saidx;
2601 struct secashead *newsah;
2605 panic("key_newsaidx: NULL pointer is passed.\n");
2607 newsah = keydb_newsecashead();
2611 bcopy(saidx, &newsah->saidx, sizeof(newsah->saidx));
2613 /* add to saidxtree */
2614 newsah->state = SADB_SASTATE_MATURE;
2615 LIST_INSERT_HEAD(&sahtree, newsah, chain);
2621 * delete SA index and all SA registerd.
2625 struct secashead *sah;
2627 struct secasvar *sav, *nextsav;
2628 u_int stateidx, state;
2634 panic("key_delsah: NULL pointer is passed.\n");
2636 s = splnet(); /*called from softclock()*/
2638 /* searching all SA registerd in the secindex. */
2640 stateidx < _ARRAYLEN(saorder_state_any);
2643 state = saorder_state_any[stateidx];
2644 for (sav = (struct secasvar *)LIST_FIRST(&sah->savtree[state]);
2648 nextsav = LIST_NEXT(sav, chain);
2650 if (sav->refcnt > 0) {
2651 /* give up to delete this sa */
2657 KEY_CHKSASTATE(state, sav->state, "key_delsah");
2661 /* remove back pointer */
2667 /* don't delete sah only if there are savs. */
2673 if (sah->sa_route.ro_rt) {
2674 RTFREE(sah->sa_route.ro_rt);
2675 sah->sa_route.ro_rt = (struct rtentry *)NULL;
2678 /* remove from tree of SA index */
2679 if (__LIST_CHAINED(sah))
2680 LIST_REMOVE(sah, chain);
2689 * allocating a new SA with LARVAL state. key_add() and key_getspi() call,
2690 * and copy the values of mhp into new buffer.
2691 * When SAD message type is GETSPI:
2692 * to set sequence number from acq_seq++,
2693 * to set zero to SPI.
2694 * not to call key_setsava().
2696 * others : pointer to new secasvar.
2698 * does not modify mbuf. does not free mbuf on error.
2700 static struct secasvar *
2701 key_newsav(m, mhp, sah, errp)
2703 const struct sadb_msghdr *mhp;
2704 struct secashead *sah;
2707 struct secasvar *newsav;
2708 const struct sadb_sa *xsa;
2711 if (m == NULL || mhp == NULL || mhp->msg == NULL || sah == NULL)
2712 panic("key_newsa: NULL pointer is passed.\n");
2714 KMALLOC(newsav, struct secasvar *, sizeof(struct secasvar));
2715 if (newsav == NULL) {
2716 ipseclog((LOG_DEBUG, "key_newsa: No more memory.\n"));
2720 bzero((caddr_t)newsav, sizeof(struct secasvar));
2722 switch (mhp->msg->sadb_msg_type) {
2726 #ifdef IPSEC_DOSEQCHECK
2727 /* sync sequence number */
2728 if (mhp->msg->sadb_msg_seq == 0)
2730 (acq_seq = (acq_seq == ~0 ? 1 : ++acq_seq));
2733 newsav->seq = mhp->msg->sadb_msg_seq;
2738 if (mhp->ext[SADB_EXT_SA] == NULL) {
2740 ipseclog((LOG_DEBUG, "key_newsa: invalid message is passed.\n"));
2744 xsa = (const struct sadb_sa *)mhp->ext[SADB_EXT_SA];
2745 newsav->spi = xsa->sadb_sa_spi;
2746 newsav->seq = mhp->msg->sadb_msg_seq;
2754 /* copy sav values */
2755 if (mhp->msg->sadb_msg_type != SADB_GETSPI) {
2756 *errp = key_setsaval(newsav, m, mhp);
2767 newsav->created = tv.tv_sec;
2770 newsav->pid = mhp->msg->sadb_msg_pid;
2775 newsav->state = SADB_SASTATE_LARVAL;
2776 LIST_INSERT_TAIL(&sah->savtree[SADB_SASTATE_LARVAL], newsav,
2783 * free() SA variable entry.
2787 struct secasvar *sav;
2791 panic("key_delsav: NULL pointer is passed.\n");
2793 if (sav->refcnt > 0)
2794 return; /* can't free */
2796 /* remove from SA header */
2797 if (__LIST_CHAINED(sav))
2798 LIST_REMOVE(sav, chain);
2800 if (sav->key_auth != NULL) {
2801 bzero(_KEYBUF(sav->key_auth), _KEYLEN(sav->key_auth));
2802 KFREE(sav->key_auth);
2803 sav->key_auth = NULL;
2805 if (sav->key_enc != NULL) {
2806 bzero(_KEYBUF(sav->key_enc), _KEYLEN(sav->key_enc));
2807 KFREE(sav->key_enc);
2808 sav->key_enc = NULL;
2811 bzero(sav->sched, sav->schedlen);
2815 if (sav->replay != NULL) {
2816 keydb_delsecreplay(sav->replay);
2819 if (sav->lft_c != NULL) {
2823 if (sav->lft_h != NULL) {
2827 if (sav->lft_s != NULL) {
2831 if (sav->iv != NULL) {
2845 * others : found, pointer to a SA.
2847 static struct secashead *
2849 struct secasindex *saidx;
2851 struct secashead *sah;
2853 LIST_FOREACH(sah, &sahtree, chain) {
2854 if (sah->state == SADB_SASTATE_DEAD)
2856 if (key_cmpsaidx(&sah->saidx, saidx, CMP_REQID))
2864 * check not to be duplicated SPI.
2865 * NOTE: this function is too slow due to searching all SAD.
2868 * others : found, pointer to a SA.
2870 static struct secasvar *
2871 key_checkspidup(saidx, spi)
2872 struct secasindex *saidx;
2875 struct secashead *sah;
2876 struct secasvar *sav;
2878 /* check address family */
2879 if (saidx->src.ss_family != saidx->dst.ss_family) {
2880 ipseclog((LOG_DEBUG, "key_checkspidup: address family mismatched.\n"));
2885 LIST_FOREACH(sah, &sahtree, chain) {
2886 if (!key_ismyaddr((struct sockaddr *)&sah->saidx.dst))
2888 sav = key_getsavbyspi(sah, spi);
2897 * search SAD litmited alive SA, protocol, SPI.
2900 * others : found, pointer to a SA.
2902 static struct secasvar *
2903 key_getsavbyspi(sah, spi)
2904 struct secashead *sah;
2907 struct secasvar *sav;
2908 u_int stateidx, state;
2910 /* search all status */
2912 stateidx < _ARRAYLEN(saorder_state_alive);
2915 state = saorder_state_alive[stateidx];
2916 LIST_FOREACH(sav, &sah->savtree[state], chain) {
2919 if (sav->state != state) {
2920 ipseclog((LOG_DEBUG, "key_getsavbyspi: "
2921 "invalid sav->state (queue: %d SA: %d)\n",
2922 state, sav->state));
2926 if (sav->spi == spi)
2935 * copy SA values from PF_KEY message except *SPI, SEQ, PID, STATE and TYPE*.
2936 * You must update these if need.
2940 * does not modify mbuf. does not free mbuf on error.
2943 key_setsaval(sav, m, mhp)
2944 struct secasvar *sav;
2946 const struct sadb_msghdr *mhp;
2949 const struct esp_algorithm *algo;
2955 if (m == NULL || mhp == NULL || mhp->msg == NULL)
2956 panic("key_setsaval: NULL pointer is passed.\n");
2958 /* initialization */
2960 sav->key_auth = NULL;
2961 sav->key_enc = NULL;
2970 if (mhp->ext[SADB_EXT_SA] != NULL) {
2971 const struct sadb_sa *sa0;
2973 sa0 = (const struct sadb_sa *)mhp->ext[SADB_EXT_SA];
2974 if (mhp->extlen[SADB_EXT_SA] < sizeof(*sa0)) {
2979 sav->alg_auth = sa0->sadb_sa_auth;
2980 sav->alg_enc = sa0->sadb_sa_encrypt;
2981 sav->flags = sa0->sadb_sa_flags;
2984 if ((sa0->sadb_sa_flags & SADB_X_EXT_OLD) == 0) {
2985 sav->replay = keydb_newsecreplay(sa0->sadb_sa_replay);
2986 if (sav->replay == NULL) {
2987 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
2994 /* Authentication keys */
2995 if (mhp->ext[SADB_EXT_KEY_AUTH] != NULL) {
2996 const struct sadb_key *key0;
2999 key0 = (const struct sadb_key *)mhp->ext[SADB_EXT_KEY_AUTH];
3000 len = mhp->extlen[SADB_EXT_KEY_AUTH];
3003 if (len < sizeof(*key0)) {
3007 switch (mhp->msg->sadb_msg_satype) {
3008 case SADB_SATYPE_AH:
3009 case SADB_SATYPE_ESP:
3010 if (len == PFKEY_ALIGN8(sizeof(struct sadb_key)) &&
3011 sav->alg_auth != SADB_X_AALG_NULL)
3014 case SADB_X_SATYPE_IPCOMP:
3020 ipseclog((LOG_DEBUG, "key_setsaval: invalid key_auth values.\n"));
3024 sav->key_auth = (struct sadb_key *)key_newbuf(key0, len);
3025 if (sav->key_auth == NULL) {
3026 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
3032 /* Encryption key */
3033 if (mhp->ext[SADB_EXT_KEY_ENCRYPT] != NULL) {
3034 const struct sadb_key *key0;
3037 key0 = (const struct sadb_key *)mhp->ext[SADB_EXT_KEY_ENCRYPT];
3038 len = mhp->extlen[SADB_EXT_KEY_ENCRYPT];
3041 if (len < sizeof(*key0)) {
3045 switch (mhp->msg->sadb_msg_satype) {
3046 case SADB_SATYPE_ESP:
3047 if (len == PFKEY_ALIGN8(sizeof(struct sadb_key)) &&
3048 sav->alg_enc != SADB_EALG_NULL) {
3052 sav->key_enc = (struct sadb_key *)key_newbuf(key0, len);
3053 if (sav->key_enc == NULL) {
3054 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
3059 case SADB_X_SATYPE_IPCOMP:
3060 if (len != PFKEY_ALIGN8(sizeof(struct sadb_key)))
3062 sav->key_enc = NULL; /*just in case*/
3064 case SADB_SATYPE_AH:
3070 ipseclog((LOG_DEBUG, "key_setsatval: invalid key_enc value.\n"));
3078 switch (mhp->msg->sadb_msg_satype) {
3079 case SADB_SATYPE_ESP:
3081 algo = esp_algorithm_lookup(sav->alg_enc);
3082 if (algo && algo->ivlen)
3083 sav->ivlen = (*algo->ivlen)(algo, sav);
3084 if (sav->ivlen == 0)
3086 KMALLOC(sav->iv, caddr_t, sav->ivlen);
3088 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
3094 key_randomfill(sav->iv, sav->ivlen);
3097 case SADB_SATYPE_AH:
3098 case SADB_X_SATYPE_IPCOMP:
3101 ipseclog((LOG_DEBUG, "key_setsaval: invalid SA type.\n"));
3108 sav->created = tv.tv_sec;
3110 /* make lifetime for CURRENT */
3111 KMALLOC(sav->lft_c, struct sadb_lifetime *,
3112 sizeof(struct sadb_lifetime));
3113 if (sav->lft_c == NULL) {
3114 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
3121 sav->lft_c->sadb_lifetime_len =
3122 PFKEY_UNIT64(sizeof(struct sadb_lifetime));
3123 sav->lft_c->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
3124 sav->lft_c->sadb_lifetime_allocations = 0;
3125 sav->lft_c->sadb_lifetime_bytes = 0;
3126 sav->lft_c->sadb_lifetime_addtime = tv.tv_sec;
3127 sav->lft_c->sadb_lifetime_usetime = 0;
3129 /* lifetimes for HARD and SOFT */
3131 const struct sadb_lifetime *lft0;
3133 lft0 = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_HARD];
3135 if (mhp->extlen[SADB_EXT_LIFETIME_HARD] < sizeof(*lft0)) {
3139 sav->lft_h = (struct sadb_lifetime *)key_newbuf(lft0,
3141 if (sav->lft_h == NULL) {
3142 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
3146 /* to be initialize ? */
3149 lft0 = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_SOFT];
3151 if (mhp->extlen[SADB_EXT_LIFETIME_SOFT] < sizeof(*lft0)) {
3155 sav->lft_s = (struct sadb_lifetime *)key_newbuf(lft0,
3157 if (sav->lft_s == NULL) {
3158 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
3162 /* to be initialize ? */
3169 /* initialization */
3170 if (sav->replay != NULL) {
3171 keydb_delsecreplay(sav->replay);
3174 if (sav->key_auth != NULL) {
3175 KFREE(sav->key_auth);
3176 sav->key_auth = NULL;
3178 if (sav->key_enc != NULL) {
3179 KFREE(sav->key_enc);
3180 sav->key_enc = NULL;
3186 if (sav->iv != NULL) {
3190 if (sav->lft_c != NULL) {
3194 if (sav->lft_h != NULL) {
3198 if (sav->lft_s != NULL) {
3207 * validation with a secasvar entry, and set SADB_SATYPE_MATURE.
3213 struct secasvar *sav;
3216 int checkmask = 0; /* 2^0: ealg 2^1: aalg 2^2: calg */
3217 int mustmask = 0; /* 2^0: ealg 2^1: aalg 2^2: calg */
3221 /* check SPI value */
3222 switch (sav->sah->saidx.proto) {
3225 if (ntohl(sav->spi) >= 0 && ntohl(sav->spi) <= 255) {
3226 ipseclog((LOG_DEBUG,
3227 "key_mature: illegal range of SPI %u.\n",
3228 (u_int32_t)ntohl(sav->spi)));
3235 switch (sav->sah->saidx.proto) {
3238 if ((sav->flags & SADB_X_EXT_OLD)
3239 && (sav->flags & SADB_X_EXT_DERIV)) {
3240 ipseclog((LOG_DEBUG, "key_mature: "
3241 "invalid flag (derived) given to old-esp.\n"));
3244 if (sav->alg_auth == SADB_AALG_NONE)
3252 if (sav->flags & SADB_X_EXT_DERIV) {
3253 ipseclog((LOG_DEBUG, "key_mature: "
3254 "invalid flag (derived) given to AH SA.\n"));
3257 if (sav->alg_enc != SADB_EALG_NONE) {
3258 ipseclog((LOG_DEBUG, "key_mature: "
3259 "protocol and algorithm mismated.\n"));
3265 case IPPROTO_IPCOMP:
3266 if (sav->alg_auth != SADB_AALG_NONE) {
3267 ipseclog((LOG_DEBUG, "key_mature: "
3268 "protocol and algorithm mismated.\n"));
3271 if ((sav->flags & SADB_X_EXT_RAWCPI) == 0
3272 && ntohl(sav->spi) >= 0x10000) {
3273 ipseclog((LOG_DEBUG, "key_mature: invalid cpi for IPComp.\n"));
3280 ipseclog((LOG_DEBUG, "key_mature: Invalid satype.\n"));
3281 return EPROTONOSUPPORT;
3284 /* check authentication algorithm */
3285 if ((checkmask & 2) != 0) {
3286 const struct ah_algorithm *algo;
3289 algo = ah_algorithm_lookup(sav->alg_auth);
3291 ipseclog((LOG_DEBUG,"key_mature: "
3292 "unknown authentication algorithm.\n"));
3296 /* algorithm-dependent check */
3298 keylen = sav->key_auth->sadb_key_bits;
3301 if (keylen < algo->keymin || algo->keymax < keylen) {
3302 ipseclog((LOG_DEBUG,
3303 "key_mature: invalid AH key length %d "
3304 "(%d-%d allowed)\n",
3305 keylen, algo->keymin, algo->keymax));
3310 if ((*algo->mature)(sav)) {
3311 /* message generated in per-algorithm function*/
3314 mature = SADB_SATYPE_AH;
3317 if ((mustmask & 2) != 0 && mature != SADB_SATYPE_AH) {
3318 ipseclog((LOG_DEBUG, "key_mature: no satisfy algorithm for AH\n"));
3323 /* check encryption algorithm */
3324 if ((checkmask & 1) != 0) {
3326 const struct esp_algorithm *algo;
3329 algo = esp_algorithm_lookup(sav->alg_enc);
3331 ipseclog((LOG_DEBUG, "key_mature: unknown encryption algorithm.\n"));
3335 /* algorithm-dependent check */
3337 keylen = sav->key_enc->sadb_key_bits;
3340 if (keylen < algo->keymin || algo->keymax < keylen) {
3341 ipseclog((LOG_DEBUG,
3342 "key_mature: invalid ESP key length %d "
3343 "(%d-%d allowed)\n",
3344 keylen, algo->keymin, algo->keymax));
3349 if ((*algo->mature)(sav)) {
3350 /* message generated in per-algorithm function*/
3353 mature = SADB_SATYPE_ESP;
3356 if ((mustmask & 1) != 0 && mature != SADB_SATYPE_ESP) {
3357 ipseclog((LOG_DEBUG, "key_mature: no satisfy algorithm for ESP\n"));
3361 ipseclog((LOG_DEBUG, "key_mature: ESP not supported in this configuration\n"));
3366 /* check compression algorithm */
3367 if ((checkmask & 4) != 0) {
3368 const struct ipcomp_algorithm *algo;
3370 /* algorithm-dependent check */
3371 algo = ipcomp_algorithm_lookup(sav->alg_enc);
3373 ipseclog((LOG_DEBUG, "key_mature: unknown compression algorithm.\n"));
3378 key_sa_chgstate(sav, SADB_SASTATE_MATURE);
3384 * subroutine for SADB_GET and SADB_DUMP.
3386 static struct mbuf *
3387 key_setdumpsa(sav, type, satype, seq, pid)
3388 struct secasvar *sav;
3389 u_int8_t type, satype;
3392 struct mbuf *result = NULL, *tres = NULL, *m;
3397 SADB_EXT_SA, SADB_X_EXT_SA2,
3398 SADB_EXT_LIFETIME_HARD, SADB_EXT_LIFETIME_SOFT,
3399 SADB_EXT_LIFETIME_CURRENT, SADB_EXT_ADDRESS_SRC,
3400 SADB_EXT_ADDRESS_DST, SADB_EXT_ADDRESS_PROXY, SADB_EXT_KEY_AUTH,
3401 SADB_EXT_KEY_ENCRYPT, SADB_EXT_IDENTITY_SRC,
3402 SADB_EXT_IDENTITY_DST, SADB_EXT_SENSITIVITY,
3405 m = key_setsadbmsg(type, 0, satype, seq, pid, sav->refcnt);
3410 for (i = sizeof(dumporder)/sizeof(dumporder[0]) - 1; i >= 0; i--) {
3413 switch (dumporder[i]) {
3415 m = key_setsadbsa(sav);
3420 case SADB_X_EXT_SA2:
3421 m = key_setsadbxsa2(sav->sah->saidx.mode,
3422 sav->replay ? sav->replay->count : 0,
3423 sav->sah->saidx.reqid);
3428 case SADB_EXT_ADDRESS_SRC:
3429 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
3430 (struct sockaddr *)&sav->sah->saidx.src,
3431 FULLMASK, IPSEC_ULPROTO_ANY);
3436 case SADB_EXT_ADDRESS_DST:
3437 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
3438 (struct sockaddr *)&sav->sah->saidx.dst,
3439 FULLMASK, IPSEC_ULPROTO_ANY);
3444 case SADB_EXT_KEY_AUTH:
3447 l = PFKEY_UNUNIT64(sav->key_auth->sadb_key_len);
3451 case SADB_EXT_KEY_ENCRYPT:
3454 l = PFKEY_UNUNIT64(sav->key_enc->sadb_key_len);
3458 case SADB_EXT_LIFETIME_CURRENT:
3461 l = PFKEY_UNUNIT64(((struct sadb_ext *)sav->lft_c)->sadb_ext_len);
3465 case SADB_EXT_LIFETIME_HARD:
3468 l = PFKEY_UNUNIT64(((struct sadb_ext *)sav->lft_h)->sadb_ext_len);
3472 case SADB_EXT_LIFETIME_SOFT:
3475 l = PFKEY_UNUNIT64(((struct sadb_ext *)sav->lft_s)->sadb_ext_len);
3479 case SADB_EXT_ADDRESS_PROXY:
3480 case SADB_EXT_IDENTITY_SRC:
3481 case SADB_EXT_IDENTITY_DST:
3482 /* XXX: should we brought from SPD ? */
3483 case SADB_EXT_SENSITIVITY:
3488 if ((!m && !p) || (m && p))
3491 M_PREPEND(tres, l, M_DONTWAIT);
3494 bcopy(p, mtod(tres, caddr_t), l);
3498 m = key_alloc_mbuf(l);
3501 m_copyback(m, 0, l, p);
3509 m_cat(result, tres);
3511 if (result->m_len < sizeof(struct sadb_msg)) {
3512 result = m_pullup(result, sizeof(struct sadb_msg));
3517 result->m_pkthdr.len = 0;
3518 for (m = result; m; m = m->m_next)
3519 result->m_pkthdr.len += m->m_len;
3521 mtod(result, struct sadb_msg *)->sadb_msg_len =
3522 PFKEY_UNIT64(result->m_pkthdr.len);
3533 * set data into sadb_msg.
3535 static struct mbuf *
3536 key_setsadbmsg(type, tlen, satype, seq, pid, reserved)
3537 u_int8_t type, satype;
3547 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
3550 MGETHDR(m, M_DONTWAIT, MT_DATA);
3551 if (m && len > MHLEN) {
3552 MCLGET(m, M_DONTWAIT);
3553 if ((m->m_flags & M_EXT) == 0) {
3560 m->m_pkthdr.len = m->m_len = len;
3563 p = mtod(m, struct sadb_msg *);
3566 p->sadb_msg_version = PF_KEY_V2;
3567 p->sadb_msg_type = type;
3568 p->sadb_msg_errno = 0;
3569 p->sadb_msg_satype = satype;
3570 p->sadb_msg_len = PFKEY_UNIT64(tlen);
3571 p->sadb_msg_reserved = reserved;
3572 p->sadb_msg_seq = seq;
3573 p->sadb_msg_pid = (u_int32_t)pid;
3579 * copy secasvar data into sadb_address.
3581 static struct mbuf *
3583 struct secasvar *sav;
3589 len = PFKEY_ALIGN8(sizeof(struct sadb_sa));
3590 m = key_alloc_mbuf(len);
3591 if (!m || m->m_next) { /*XXX*/
3597 p = mtod(m, struct sadb_sa *);
3600 p->sadb_sa_len = PFKEY_UNIT64(len);
3601 p->sadb_sa_exttype = SADB_EXT_SA;
3602 p->sadb_sa_spi = sav->spi;
3603 p->sadb_sa_replay = (sav->replay != NULL ? sav->replay->wsize : 0);
3604 p->sadb_sa_state = sav->state;
3605 p->sadb_sa_auth = sav->alg_auth;
3606 p->sadb_sa_encrypt = sav->alg_enc;
3607 p->sadb_sa_flags = sav->flags;
3613 * set data into sadb_address.
3615 static struct mbuf *
3616 key_setsadbaddr(exttype, saddr, prefixlen, ul_proto)
3618 struct sockaddr *saddr;
3623 struct sadb_address *p;
3626 len = PFKEY_ALIGN8(sizeof(struct sadb_address)) +
3627 PFKEY_ALIGN8(saddr->sa_len);
3628 m = key_alloc_mbuf(len);
3629 if (!m || m->m_next) { /*XXX*/
3635 p = mtod(m, struct sadb_address *);
3638 p->sadb_address_len = PFKEY_UNIT64(len);
3639 p->sadb_address_exttype = exttype;
3640 p->sadb_address_proto = ul_proto;
3641 if (prefixlen == FULLMASK) {
3642 switch (saddr->sa_family) {
3644 prefixlen = sizeof(struct in_addr) << 3;
3647 prefixlen = sizeof(struct in6_addr) << 3;
3653 p->sadb_address_prefixlen = prefixlen;
3654 p->sadb_address_reserved = 0;
3657 mtod(m, caddr_t) + PFKEY_ALIGN8(sizeof(struct sadb_address)),
3665 * set data into sadb_ident.
3667 static struct mbuf *
3668 key_setsadbident(exttype, idtype, string, stringlen, id)
3669 u_int16_t exttype, idtype;
3675 struct sadb_ident *p;
3678 len = PFKEY_ALIGN8(sizeof(struct sadb_ident)) + PFKEY_ALIGN8(stringlen);
3679 m = key_alloc_mbuf(len);
3680 if (!m || m->m_next) { /*XXX*/
3686 p = mtod(m, struct sadb_ident *);
3689 p->sadb_ident_len = PFKEY_UNIT64(len);
3690 p->sadb_ident_exttype = exttype;
3691 p->sadb_ident_type = idtype;
3692 p->sadb_ident_reserved = 0;
3693 p->sadb_ident_id = id;
3696 mtod(m, caddr_t) + PFKEY_ALIGN8(sizeof(struct sadb_ident)),
3704 * set data into sadb_x_sa2.
3706 static struct mbuf *
3707 key_setsadbxsa2(mode, seq, reqid)
3709 u_int32_t seq, reqid;
3712 struct sadb_x_sa2 *p;
3715 len = PFKEY_ALIGN8(sizeof(struct sadb_x_sa2));
3716 m = key_alloc_mbuf(len);
3717 if (!m || m->m_next) { /*XXX*/
3723 p = mtod(m, struct sadb_x_sa2 *);
3726 p->sadb_x_sa2_len = PFKEY_UNIT64(len);
3727 p->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
3728 p->sadb_x_sa2_mode = mode;
3729 p->sadb_x_sa2_reserved1 = 0;
3730 p->sadb_x_sa2_reserved2 = 0;
3731 p->sadb_x_sa2_sequence = seq;
3732 p->sadb_x_sa2_reqid = reqid;
3738 * set data into sadb_x_policy
3740 static struct mbuf *
3741 key_setsadbxpolicy(type, dir, id)
3747 struct sadb_x_policy *p;
3750 len = PFKEY_ALIGN8(sizeof(struct sadb_x_policy));
3751 m = key_alloc_mbuf(len);
3752 if (!m || m->m_next) { /*XXX*/
3758 p = mtod(m, struct sadb_x_policy *);
3761 p->sadb_x_policy_len = PFKEY_UNIT64(len);
3762 p->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3763 p->sadb_x_policy_type = type;
3764 p->sadb_x_policy_dir = dir;
3765 p->sadb_x_policy_id = id;
3772 * copy a buffer into the new buffer allocated.
3775 key_newbuf(src, len)
3781 KMALLOC(new, caddr_t, len);
3783 ipseclog((LOG_DEBUG, "key_newbuf: No more memory.\n"));
3786 bcopy(src, new, len);
3791 /* compare my own address
3792 * OUT: 1: true, i.e. my address.
3797 struct sockaddr *sa;
3800 struct sockaddr_in *sin;
3801 struct in_ifaddr *ia;
3806 panic("key_ismyaddr: NULL pointer is passed.\n");
3808 switch (sa->sa_family) {
3811 sin = (struct sockaddr_in *)sa;
3812 for (ia = in_ifaddrhead.tqh_first; ia;
3813 ia = ia->ia_link.tqe_next)
3815 if (sin->sin_family == ia->ia_addr.sin_family &&
3816 sin->sin_len == ia->ia_addr.sin_len &&
3817 sin->sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr)
3826 return key_ismyaddr6((struct sockaddr_in6 *)sa);
3835 * compare my own address for IPv6.
3838 * NOTE: derived ip6_input() in KAME. This is necessary to modify more.
3840 #include <netinet6/in6_var.h>
3844 struct sockaddr_in6 *sin6;
3846 struct in6_ifaddr *ia;
3847 struct in6_multi *in6m;
3849 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
3850 if (key_sockaddrcmp((struct sockaddr *)&sin6,
3851 (struct sockaddr *)&ia->ia_addr, 0) == 0)
3856 * XXX why do we care about multlicast here while we don't care
3857 * about IPv4 multicast??
3861 IN6_LOOKUP_MULTI(sin6->sin6_addr, ia->ia_ifp, in6m);
3866 /* loopback, just for safety */
3867 if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
3875 * compare two secasindex structure.
3876 * flag can specify to compare 2 saidxes.
3877 * compare two secasindex structure without both mode and reqid.
3878 * don't compare port.
3880 * saidx0: source, it can be in SAD.
3887 key_cmpsaidx(saidx0, saidx1, flag)
3888 struct secasindex *saidx0, *saidx1;
3892 if (saidx0 == NULL && saidx1 == NULL)
3895 if (saidx0 == NULL || saidx1 == NULL)
3898 if (saidx0->proto != saidx1->proto)
3901 if (flag == CMP_EXACTLY) {
3902 if (saidx0->mode != saidx1->mode)
3904 if (saidx0->reqid != saidx1->reqid)
3906 if (bcmp(&saidx0->src, &saidx1->src, saidx0->src.ss_len) != 0 ||
3907 bcmp(&saidx0->dst, &saidx1->dst, saidx0->dst.ss_len) != 0)
3911 /* CMP_MODE_REQID, CMP_REQID, CMP_HEAD */
3912 if (flag == CMP_MODE_REQID
3913 ||flag == CMP_REQID) {
3915 * If reqid of SPD is non-zero, unique SA is required.
3916 * The result must be of same reqid in this case.
3918 if (saidx1->reqid != 0 && saidx0->reqid != saidx1->reqid)
3922 if (flag == CMP_MODE_REQID) {
3923 if (saidx0->mode != IPSEC_MODE_ANY
3924 && saidx0->mode != saidx1->mode)
3928 if (key_sockaddrcmp((struct sockaddr *)&saidx0->src,
3929 (struct sockaddr *)&saidx1->src, 0) != 0) {
3932 if (key_sockaddrcmp((struct sockaddr *)&saidx0->dst,
3933 (struct sockaddr *)&saidx1->dst, 0) != 0) {
3942 * compare two secindex structure exactly.
3944 * spidx0: source, it is often in SPD.
3945 * spidx1: object, it is often from PFKEY message.
3951 key_cmpspidx_exactly(spidx0, spidx1)
3952 struct secpolicyindex *spidx0, *spidx1;
3955 if (spidx0 == NULL && spidx1 == NULL)
3958 if (spidx0 == NULL || spidx1 == NULL)
3961 if (spidx0->prefs != spidx1->prefs
3962 || spidx0->prefd != spidx1->prefd
3963 || spidx0->ul_proto != spidx1->ul_proto)
3966 if (key_sockaddrcmp((struct sockaddr *)&spidx0->src,
3967 (struct sockaddr *)&spidx1->src, 1) != 0) {
3970 if (key_sockaddrcmp((struct sockaddr *)&spidx0->dst,
3971 (struct sockaddr *)&spidx1->dst, 1) != 0) {
3979 * compare two secindex structure with mask.
3981 * spidx0: source, it is often in SPD.
3982 * spidx1: object, it is often from IP header.
3988 key_cmpspidx_withmask(spidx0, spidx1)
3989 struct secpolicyindex *spidx0, *spidx1;
3992 if (spidx0 == NULL && spidx1 == NULL)
3995 if (spidx0 == NULL || spidx1 == NULL)
3998 if (spidx0->src.ss_family != spidx1->src.ss_family ||
3999 spidx0->dst.ss_family != spidx1->dst.ss_family ||
4000 spidx0->src.ss_len != spidx1->src.ss_len ||
4001 spidx0->dst.ss_len != spidx1->dst.ss_len)
4004 /* if spidx.ul_proto == IPSEC_ULPROTO_ANY, ignore. */
4005 if (spidx0->ul_proto != (u_int16_t)IPSEC_ULPROTO_ANY
4006 && spidx0->ul_proto != spidx1->ul_proto)
4009 switch (spidx0->src.ss_family) {
4011 if (satosin(&spidx0->src)->sin_port != IPSEC_PORT_ANY
4012 && satosin(&spidx0->src)->sin_port !=
4013 satosin(&spidx1->src)->sin_port)
4015 if (!key_bbcmp((caddr_t)&satosin(&spidx0->src)->sin_addr,
4016 (caddr_t)&satosin(&spidx1->src)->sin_addr, spidx0->prefs))
4020 if (satosin6(&spidx0->src)->sin6_port != IPSEC_PORT_ANY
4021 && satosin6(&spidx0->src)->sin6_port !=
4022 satosin6(&spidx1->src)->sin6_port)
4025 * scope_id check. if sin6_scope_id is 0, we regard it
4026 * as a wildcard scope, which matches any scope zone ID.
4028 if (satosin6(&spidx0->src)->sin6_scope_id &&
4029 satosin6(&spidx1->src)->sin6_scope_id &&
4030 satosin6(&spidx0->src)->sin6_scope_id !=
4031 satosin6(&spidx1->src)->sin6_scope_id)
4033 if (!key_bbcmp((caddr_t)&satosin6(&spidx0->src)->sin6_addr,
4034 (caddr_t)&satosin6(&spidx1->src)->sin6_addr, spidx0->prefs))
4039 if (bcmp(&spidx0->src, &spidx1->src, spidx0->src.ss_len) != 0)
4044 switch (spidx0->dst.ss_family) {
4046 if (satosin(&spidx0->dst)->sin_port != IPSEC_PORT_ANY
4047 && satosin(&spidx0->dst)->sin_port !=
4048 satosin(&spidx1->dst)->sin_port)
4050 if (!key_bbcmp((caddr_t)&satosin(&spidx0->dst)->sin_addr,
4051 (caddr_t)&satosin(&spidx1->dst)->sin_addr, spidx0->prefd))
4055 if (satosin6(&spidx0->dst)->sin6_port != IPSEC_PORT_ANY
4056 && satosin6(&spidx0->dst)->sin6_port !=
4057 satosin6(&spidx1->dst)->sin6_port)
4060 * scope_id check. if sin6_scope_id is 0, we regard it
4061 * as a wildcard scope, which matches any scope zone ID.
4063 if (satosin6(&spidx0->src)->sin6_scope_id &&
4064 satosin6(&spidx1->src)->sin6_scope_id &&
4065 satosin6(&spidx0->dst)->sin6_scope_id !=
4066 satosin6(&spidx1->dst)->sin6_scope_id)
4068 if (!key_bbcmp((caddr_t)&satosin6(&spidx0->dst)->sin6_addr,
4069 (caddr_t)&satosin6(&spidx1->dst)->sin6_addr, spidx0->prefd))
4074 if (bcmp(&spidx0->dst, &spidx1->dst, spidx0->dst.ss_len) != 0)
4079 /* XXX Do we check other field ? e.g. flowinfo */
4084 /* returns 0 on match */
4086 key_sockaddrcmp(sa1, sa2, port)
4087 struct sockaddr *sa1;
4088 struct sockaddr *sa2;
4091 if (sa1->sa_family != sa2->sa_family || sa1->sa_len != sa2->sa_len)
4094 switch (sa1->sa_family) {
4096 if (sa1->sa_len != sizeof(struct sockaddr_in))
4098 if (satosin(sa1)->sin_addr.s_addr !=
4099 satosin(sa2)->sin_addr.s_addr) {
4102 if (port && satosin(sa1)->sin_port != satosin(sa2)->sin_port)
4106 if (sa1->sa_len != sizeof(struct sockaddr_in6))
4107 return 1; /*EINVAL*/
4108 if (satosin6(sa1)->sin6_scope_id !=
4109 satosin6(sa2)->sin6_scope_id) {
4112 if (!IN6_ARE_ADDR_EQUAL(&satosin6(sa1)->sin6_addr,
4113 &satosin6(sa2)->sin6_addr)) {
4117 satosin6(sa1)->sin6_port != satosin6(sa2)->sin6_port) {
4121 if (bcmp(sa1, sa2, sa1->sa_len) != 0)
4130 * compare two buffers with mask.
4134 * bits: Number of bits to compare
4140 key_bbcmp(p1, p2, bits)
4146 /* XXX: This could be considerably faster if we compare a word
4147 * at a time, but it is complicated on LSB Endian machines */
4149 /* Handle null pointers */
4150 if (p1 == NULL || p2 == NULL)
4160 mask = ~((1<<(8-bits))-1);
4161 if ((*p1 & mask) != (*p2 & mask))
4164 return 1; /* Match! */
4169 * scanning SPD and SAD to check status for each entries,
4170 * and do to remove or to expire.
4171 * XXX: year 2038 problem may remain.
4174 key_timehandler(void)
4182 s = splnet(); /*called from softclock()*/
4186 struct secpolicy *sp, *nextsp;
4188 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
4189 for (sp = LIST_FIRST(&sptree[dir]);
4193 nextsp = LIST_NEXT(sp, chain);
4195 if (sp->state == IPSEC_SPSTATE_DEAD) {
4200 if (sp->lifetime == 0 && sp->validtime == 0)
4203 /* the deletion will occur next time */
4205 && tv.tv_sec - sp->created > sp->lifetime)
4207 && tv.tv_sec - sp->lastused > sp->validtime)) {
4208 sp->state = IPSEC_SPSTATE_DEAD;
4218 struct secashead *sah, *nextsah;
4219 struct secasvar *sav, *nextsav;
4221 for (sah = LIST_FIRST(&sahtree);
4225 nextsah = LIST_NEXT(sah, chain);
4227 /* if sah has been dead, then delete it and process next sah. */
4228 if (sah->state == SADB_SASTATE_DEAD) {
4233 /* if LARVAL entry doesn't become MATURE, delete it. */
4234 for (sav = LIST_FIRST(&sah->savtree[SADB_SASTATE_LARVAL]);
4238 nextsav = LIST_NEXT(sav, chain);
4240 if (tv.tv_sec - sav->created > key_larval_lifetime) {
4246 * check MATURE entry to start to send expire message
4249 for (sav = LIST_FIRST(&sah->savtree[SADB_SASTATE_MATURE]);
4253 nextsav = LIST_NEXT(sav, chain);
4255 /* we don't need to check. */
4256 if (sav->lft_s == NULL)
4260 if (sav->lft_c == NULL) {
4261 ipseclog((LOG_DEBUG,"key_timehandler: "
4262 "There is no CURRENT time, why?\n"));
4266 /* check SOFT lifetime */
4267 if (sav->lft_s->sadb_lifetime_addtime != 0
4268 && tv.tv_sec - sav->created > sav->lft_s->sadb_lifetime_addtime) {
4270 * check the SA if it has been used.
4271 * when it hasn't been used, delete it.
4272 * i don't think such SA will be used.
4274 if (sav->lft_c->sadb_lifetime_usetime == 0) {
4275 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4279 key_sa_chgstate(sav, SADB_SASTATE_DYING);
4281 * XXX If we keep to send expire
4282 * message in the status of
4283 * DYING. Do remove below code.
4289 /* check SOFT lifetime by bytes */
4291 * XXX I don't know the way to delete this SA
4292 * when new SA is installed. Caution when it's
4293 * installed too big lifetime by time.
4295 else if (sav->lft_s->sadb_lifetime_bytes != 0
4296 && sav->lft_s->sadb_lifetime_bytes < sav->lft_c->sadb_lifetime_bytes) {
4298 key_sa_chgstate(sav, SADB_SASTATE_DYING);
4300 * XXX If we keep to send expire
4301 * message in the status of
4302 * DYING. Do remove below code.
4308 /* check DYING entry to change status to DEAD. */
4309 for (sav = LIST_FIRST(&sah->savtree[SADB_SASTATE_DYING]);
4313 nextsav = LIST_NEXT(sav, chain);
4315 /* we don't need to check. */
4316 if (sav->lft_h == NULL)
4320 if (sav->lft_c == NULL) {
4321 ipseclog((LOG_DEBUG, "key_timehandler: "
4322 "There is no CURRENT time, why?\n"));
4326 if (sav->lft_h->sadb_lifetime_addtime != 0
4327 && tv.tv_sec - sav->created > sav->lft_h->sadb_lifetime_addtime) {
4328 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4332 #if 0 /* XXX Should we keep to send expire message until HARD lifetime ? */
4333 else if (sav->lft_s != NULL
4334 && sav->lft_s->sadb_lifetime_addtime != 0
4335 && tv.tv_sec - sav->created > sav->lft_s->sadb_lifetime_addtime) {
4337 * XXX: should be checked to be
4338 * installed the valid SA.
4342 * If there is no SA then sending
4348 /* check HARD lifetime by bytes */
4349 else if (sav->lft_h->sadb_lifetime_bytes != 0
4350 && sav->lft_h->sadb_lifetime_bytes < sav->lft_c->sadb_lifetime_bytes) {
4351 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4357 /* delete entry in DEAD */
4358 for (sav = LIST_FIRST(&sah->savtree[SADB_SASTATE_DEAD]);
4362 nextsav = LIST_NEXT(sav, chain);
4365 if (sav->state != SADB_SASTATE_DEAD) {
4366 ipseclog((LOG_DEBUG, "key_timehandler: "
4367 "invalid sav->state "
4368 "(queue: %d SA: %d): "
4370 SADB_SASTATE_DEAD, sav->state));
4374 * do not call key_freesav() here.
4375 * sav should already be freed, and sav->refcnt
4376 * shows other references to sav
4377 * (such as from SPD).
4383 #ifndef IPSEC_NONBLOCK_ACQUIRE
4386 struct secacq *acq, *nextacq;
4388 for (acq = LIST_FIRST(&acqtree);
4392 nextacq = LIST_NEXT(acq, chain);
4394 if (tv.tv_sec - acq->created > key_blockacq_lifetime
4395 && __LIST_CHAINED(acq)) {
4396 LIST_REMOVE(acq, chain);
4405 struct secspacq *acq, *nextacq;
4407 for (acq = LIST_FIRST(&spacqtree);
4411 nextacq = LIST_NEXT(acq, chain);
4413 if (tv.tv_sec - acq->created > key_blockacq_lifetime
4414 && __LIST_CHAINED(acq)) {
4415 LIST_REMOVE(acq, chain);
4421 /* initialize random seed */
4422 if (key_tick_init_random++ > key_int_random) {
4423 key_tick_init_random = 0;
4427 #ifndef IPSEC_DEBUG2
4428 /* do exchange to tick time !! */
4429 (void)timeout((void *)key_timehandler, (void *)0, hz);
4430 #endif /* IPSEC_DEBUG2 */
4437 * to initialize a seed for random()
4446 srandom(tv.tv_usec);
4456 key_randomfill(&value, sizeof(value));
4461 key_randomfill(p, l)
4467 static int warn = 1;
4470 n = (size_t)read_random(p, (u_int)l);
4474 bcopy(&v, (u_int8_t *)p + n,
4475 l - n < sizeof(v) ? l - n : sizeof(v));
4479 printf("WARNING: pseudo-random number generator "
4480 "used for IPsec processing\n");
4487 * map SADB_SATYPE_* to IPPROTO_*.
4488 * if satype == SADB_SATYPE then satype is mapped to ~0.
4490 * 0: invalid satype.
4493 key_satype2proto(satype)
4497 case SADB_SATYPE_UNSPEC:
4498 return IPSEC_PROTO_ANY;
4499 case SADB_SATYPE_AH:
4501 case SADB_SATYPE_ESP:
4503 case SADB_X_SATYPE_IPCOMP:
4504 return IPPROTO_IPCOMP;
4513 * map IPPROTO_* to SADB_SATYPE_*
4515 * 0: invalid protocol type.
4518 key_proto2satype(proto)
4523 return SADB_SATYPE_AH;
4525 return SADB_SATYPE_ESP;
4526 case IPPROTO_IPCOMP:
4527 return SADB_X_SATYPE_IPCOMP;
4537 * SADB_GETSPI processing is to receive
4538 * <base, (SA2), src address, dst address, (SPI range)>
4539 * from the IKMPd, to assign a unique spi value, to hang on the INBOUND
4540 * tree with the status of LARVAL, and send
4541 * <base, SA(*), address(SD)>
4544 * IN: mhp: pointer to the pointer to each header.
4545 * OUT: NULL if fail.
4546 * other if success, return pointer to the message to send.
4549 key_getspi(so, m, mhp)
4552 const struct sadb_msghdr *mhp;
4554 struct sadb_address *src0, *dst0;
4555 struct secasindex saidx;
4556 struct secashead *newsah;
4557 struct secasvar *newsav;
4565 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
4566 panic("key_getspi: NULL pointer is passed.\n");
4568 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
4569 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
4570 ipseclog((LOG_DEBUG, "key_getspi: invalid message is passed.\n"));
4571 return key_senderror(so, m, EINVAL);
4573 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
4574 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
4575 ipseclog((LOG_DEBUG, "key_getspi: invalid message is passed.\n"));
4576 return key_senderror(so, m, EINVAL);
4578 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
4579 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
4580 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
4582 mode = IPSEC_MODE_ANY;
4586 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
4587 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
4589 /* map satype to proto */
4590 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
4591 ipseclog((LOG_DEBUG, "key_getspi: invalid satype is passed.\n"));
4592 return key_senderror(so, m, EINVAL);
4595 /* make sure if port number is zero. */
4596 switch (((struct sockaddr *)(src0 + 1))->sa_family) {
4598 if (((struct sockaddr *)(src0 + 1))->sa_len !=
4599 sizeof(struct sockaddr_in))
4600 return key_senderror(so, m, EINVAL);
4601 ((struct sockaddr_in *)(src0 + 1))->sin_port = 0;
4604 if (((struct sockaddr *)(src0 + 1))->sa_len !=
4605 sizeof(struct sockaddr_in6))
4606 return key_senderror(so, m, EINVAL);
4607 ((struct sockaddr_in6 *)(src0 + 1))->sin6_port = 0;
4612 switch (((struct sockaddr *)(dst0 + 1))->sa_family) {
4614 if (((struct sockaddr *)(dst0 + 1))->sa_len !=
4615 sizeof(struct sockaddr_in))
4616 return key_senderror(so, m, EINVAL);
4617 ((struct sockaddr_in *)(dst0 + 1))->sin_port = 0;
4620 if (((struct sockaddr *)(dst0 + 1))->sa_len !=
4621 sizeof(struct sockaddr_in6))
4622 return key_senderror(so, m, EINVAL);
4623 ((struct sockaddr_in6 *)(dst0 + 1))->sin6_port = 0;
4629 /* XXX boundary check against sa_len */
4630 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
4632 /* SPI allocation */
4633 spi = key_do_getnewspi((struct sadb_spirange *)mhp->ext[SADB_EXT_SPIRANGE],
4636 return key_senderror(so, m, EINVAL);
4638 /* get a SA index */
4639 if ((newsah = key_getsah(&saidx)) == NULL) {
4640 /* create a new SA index */
4641 if ((newsah = key_newsah(&saidx)) == NULL) {
4642 ipseclog((LOG_DEBUG, "key_getspi: No more memory.\n"));
4643 return key_senderror(so, m, ENOBUFS);
4649 newsav = key_newsav(m, mhp, newsah, &error);
4650 if (newsav == NULL) {
4651 /* XXX don't free new SA index allocated in above. */
4652 return key_senderror(so, m, error);
4656 newsav->spi = htonl(spi);
4658 #ifndef IPSEC_NONBLOCK_ACQUIRE
4659 /* delete the entry in acqtree */
4660 if (mhp->msg->sadb_msg_seq != 0) {
4662 if ((acq = key_getacqbyseq(mhp->msg->sadb_msg_seq)) != NULL) {
4663 /* reset counter in order to deletion by timehandler. */
4666 acq->created = tv.tv_sec;
4673 struct mbuf *n, *nn;
4674 struct sadb_sa *m_sa;
4675 struct sadb_msg *newmsg;
4678 /* create new sadb_msg to reply. */
4679 len = PFKEY_ALIGN8(sizeof(struct sadb_msg)) +
4680 PFKEY_ALIGN8(sizeof(struct sadb_sa));
4682 return key_senderror(so, m, ENOBUFS);
4684 MGETHDR(n, M_DONTWAIT, MT_DATA);
4686 MCLGET(n, M_DONTWAIT);
4687 if ((n->m_flags & M_EXT) == 0) {
4693 return key_senderror(so, m, ENOBUFS);
4699 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
4700 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
4702 m_sa = (struct sadb_sa *)(mtod(n, caddr_t) + off);
4703 m_sa->sadb_sa_len = PFKEY_UNIT64(sizeof(struct sadb_sa));
4704 m_sa->sadb_sa_exttype = SADB_EXT_SA;
4705 m_sa->sadb_sa_spi = htonl(spi);
4706 off += PFKEY_ALIGN8(sizeof(struct sadb_sa));
4710 panic("length inconsistency in key_getspi");
4713 n->m_next = key_gather_mbuf(m, mhp, 0, 2, SADB_EXT_ADDRESS_SRC,
4714 SADB_EXT_ADDRESS_DST);
4717 return key_senderror(so, m, ENOBUFS);
4720 if (n->m_len < sizeof(struct sadb_msg)) {
4721 n = m_pullup(n, sizeof(struct sadb_msg));
4723 return key_sendup_mbuf(so, m, KEY_SENDUP_ONE);
4726 n->m_pkthdr.len = 0;
4727 for (nn = n; nn; nn = nn->m_next)
4728 n->m_pkthdr.len += nn->m_len;
4730 newmsg = mtod(n, struct sadb_msg *);
4731 newmsg->sadb_msg_seq = newsav->seq;
4732 newmsg->sadb_msg_errno = 0;
4733 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
4736 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
4741 * allocating new SPI
4742 * called by key_getspi().
4748 key_do_getnewspi(spirange, saidx)
4749 struct sadb_spirange *spirange;
4750 struct secasindex *saidx;
4754 int count = key_spi_trycnt;
4756 /* set spi range to allocate */
4757 if (spirange != NULL) {
4758 min = spirange->sadb_spirange_min;
4759 max = spirange->sadb_spirange_max;
4761 min = key_spi_minval;
4762 max = key_spi_maxval;
4764 /* IPCOMP needs 2-byte SPI */
4765 if (saidx->proto == IPPROTO_IPCOMP) {
4772 t = min; min = max; max = t;
4777 if (key_checkspidup(saidx, min) != NULL) {
4778 ipseclog((LOG_DEBUG, "key_do_getnewspi: SPI %u exists already.\n", min));
4782 count--; /* taking one cost. */
4790 /* when requesting to allocate spi ranged */
4792 /* generate pseudo-random SPI value ranged. */
4793 newspi = min + (key_random() % (max - min + 1));
4795 if (key_checkspidup(saidx, newspi) == NULL)
4799 if (count == 0 || newspi == 0) {
4800 ipseclog((LOG_DEBUG, "key_do_getnewspi: to allocate spi is failed.\n"));
4806 keystat.getspi_count =
4807 (keystat.getspi_count + key_spi_trycnt - count) / 2;
4813 * SADB_UPDATE processing
4815 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4816 * key(AE), (identity(SD),) (sensitivity)>
4817 * from the ikmpd, and update a secasvar entry whose status is SADB_SASTATE_LARVAL.
4819 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4820 * (identity(SD),) (sensitivity)>
4823 * m will always be freed.
4826 key_update(so, m, mhp)
4829 const struct sadb_msghdr *mhp;
4831 struct sadb_sa *sa0;
4832 struct sadb_address *src0, *dst0;
4833 struct secasindex saidx;
4834 struct secashead *sah;
4835 struct secasvar *sav;
4842 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
4843 panic("key_update: NULL pointer is passed.\n");
4845 /* map satype to proto */
4846 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
4847 ipseclog((LOG_DEBUG, "key_update: invalid satype is passed.\n"));
4848 return key_senderror(so, m, EINVAL);
4851 if (mhp->ext[SADB_EXT_SA] == NULL ||
4852 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
4853 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
4854 (mhp->msg->sadb_msg_satype == SADB_SATYPE_ESP &&
4855 mhp->ext[SADB_EXT_KEY_ENCRYPT] == NULL) ||
4856 (mhp->msg->sadb_msg_satype == SADB_SATYPE_AH &&
4857 mhp->ext[SADB_EXT_KEY_AUTH] == NULL) ||
4858 (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL &&
4859 mhp->ext[SADB_EXT_LIFETIME_SOFT] == NULL) ||
4860 (mhp->ext[SADB_EXT_LIFETIME_HARD] == NULL &&
4861 mhp->ext[SADB_EXT_LIFETIME_SOFT] != NULL)) {
4862 ipseclog((LOG_DEBUG, "key_update: invalid message is passed.\n"));
4863 return key_senderror(so, m, EINVAL);
4865 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
4866 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
4867 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
4868 ipseclog((LOG_DEBUG, "key_update: invalid message is passed.\n"));
4869 return key_senderror(so, m, EINVAL);
4871 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
4872 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
4873 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
4875 mode = IPSEC_MODE_ANY;
4878 /* XXX boundary checking for other extensions */
4880 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
4881 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
4882 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
4884 /* XXX boundary check against sa_len */
4885 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
4887 /* get a SA header */
4888 if ((sah = key_getsah(&saidx)) == NULL) {
4889 ipseclog((LOG_DEBUG, "key_update: no SA index found.\n"));
4890 return key_senderror(so, m, ENOENT);
4893 /* set spidx if there */
4895 error = key_setident(sah, m, mhp);
4897 return key_senderror(so, m, error);
4899 /* find a SA with sequence number. */
4900 #ifdef IPSEC_DOSEQCHECK
4901 if (mhp->msg->sadb_msg_seq != 0
4902 && (sav = key_getsavbyseq(sah, mhp->msg->sadb_msg_seq)) == NULL) {
4903 ipseclog((LOG_DEBUG,
4904 "key_update: no larval SA with sequence %u exists.\n",
4905 mhp->msg->sadb_msg_seq));
4906 return key_senderror(so, m, ENOENT);
4909 if ((sav = key_getsavbyspi(sah, sa0->sadb_sa_spi)) == NULL) {
4910 ipseclog((LOG_DEBUG,
4911 "key_update: no such a SA found (spi:%u)\n",
4912 (u_int32_t)ntohl(sa0->sadb_sa_spi)));
4913 return key_senderror(so, m, EINVAL);
4917 /* validity check */
4918 if (sav->sah->saidx.proto != proto) {
4919 ipseclog((LOG_DEBUG,
4920 "key_update: protocol mismatched (DB=%u param=%u)\n",
4921 sav->sah->saidx.proto, proto));
4922 return key_senderror(so, m, EINVAL);
4924 #ifdef IPSEC_DOSEQCHECK
4925 if (sav->spi != sa0->sadb_sa_spi) {
4926 ipseclog((LOG_DEBUG,
4927 "key_update: SPI mismatched (DB:%u param:%u)\n",
4928 (u_int32_t)ntohl(sav->spi),
4929 (u_int32_t)ntohl(sa0->sadb_sa_spi)));
4930 return key_senderror(so, m, EINVAL);
4933 if (sav->pid != mhp->msg->sadb_msg_pid) {
4934 ipseclog((LOG_DEBUG,
4935 "key_update: pid mismatched (DB:%u param:%u)\n",
4936 sav->pid, mhp->msg->sadb_msg_pid));
4937 return key_senderror(so, m, EINVAL);
4940 /* copy sav values */
4941 error = key_setsaval(sav, m, mhp);
4944 return key_senderror(so, m, error);
4947 /* check SA values to be mature. */
4948 if ((mhp->msg->sadb_msg_errno = key_mature(sav)) != 0) {
4950 return key_senderror(so, m, 0);
4956 /* set msg buf from mhp */
4957 n = key_getmsgbuf_x1(m, mhp);
4959 ipseclog((LOG_DEBUG, "key_update: No more memory.\n"));
4960 return key_senderror(so, m, ENOBUFS);
4964 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
4969 * search SAD with sequence for a SA which state is SADB_SASTATE_LARVAL.
4970 * only called by key_update().
4973 * others : found, pointer to a SA.
4975 #ifdef IPSEC_DOSEQCHECK
4976 static struct secasvar *
4977 key_getsavbyseq(sah, seq)
4978 struct secashead *sah;
4981 struct secasvar *sav;
4984 state = SADB_SASTATE_LARVAL;
4986 /* search SAD with sequence number ? */
4987 LIST_FOREACH(sav, &sah->savtree[state], chain) {
4989 KEY_CHKSASTATE(state, sav->state, "key_getsabyseq");
4991 if (sav->seq == seq) {
4993 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
4994 printf("DP key_getsavbyseq cause "
4995 "refcnt++:%d SA:%p\n",
5006 * SADB_ADD processing
5007 * add a entry to SA database, when received
5008 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
5009 * key(AE), (identity(SD),) (sensitivity)>
5012 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
5013 * (identity(SD),) (sensitivity)>
5016 * IGNORE identity and sensitivity messages.
5018 * m will always be freed.
5024 const struct sadb_msghdr *mhp;
5026 struct sadb_sa *sa0;
5027 struct sadb_address *src0, *dst0;
5028 struct secasindex saidx;
5029 struct secashead *newsah;
5030 struct secasvar *newsav;
5037 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
5038 panic("key_add: NULL pointer is passed.\n");
5040 /* map satype to proto */
5041 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5042 ipseclog((LOG_DEBUG, "key_add: invalid satype is passed.\n"));
5043 return key_senderror(so, m, EINVAL);
5046 if (mhp->ext[SADB_EXT_SA] == NULL ||
5047 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5048 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
5049 (mhp->msg->sadb_msg_satype == SADB_SATYPE_ESP &&
5050 mhp->ext[SADB_EXT_KEY_ENCRYPT] == NULL) ||
5051 (mhp->msg->sadb_msg_satype == SADB_SATYPE_AH &&
5052 mhp->ext[SADB_EXT_KEY_AUTH] == NULL) ||
5053 (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL &&
5054 mhp->ext[SADB_EXT_LIFETIME_SOFT] == NULL) ||
5055 (mhp->ext[SADB_EXT_LIFETIME_HARD] == NULL &&
5056 mhp->ext[SADB_EXT_LIFETIME_SOFT] != NULL)) {
5057 ipseclog((LOG_DEBUG, "key_add: invalid message is passed.\n"));
5058 return key_senderror(so, m, EINVAL);
5060 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
5061 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5062 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5064 ipseclog((LOG_DEBUG, "key_add: invalid message is passed.\n"));
5065 return key_senderror(so, m, EINVAL);
5067 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
5068 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
5069 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
5071 mode = IPSEC_MODE_ANY;
5075 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5076 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
5077 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
5079 /* XXX boundary check against sa_len */
5080 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
5082 /* get a SA header */
5083 if ((newsah = key_getsah(&saidx)) == NULL) {
5084 /* create a new SA header */
5085 if ((newsah = key_newsah(&saidx)) == NULL) {
5086 ipseclog((LOG_DEBUG, "key_add: No more memory.\n"));
5087 return key_senderror(so, m, ENOBUFS);
5091 /* set spidx if there */
5093 error = key_setident(newsah, m, mhp);
5095 return key_senderror(so, m, error);
5098 /* create new SA entry. */
5099 /* We can create new SA only if SPI is differenct. */
5100 if (key_getsavbyspi(newsah, sa0->sadb_sa_spi)) {
5101 ipseclog((LOG_DEBUG, "key_add: SA already exists.\n"));
5102 return key_senderror(so, m, EEXIST);
5104 newsav = key_newsav(m, mhp, newsah, &error);
5105 if (newsav == NULL) {
5106 return key_senderror(so, m, error);
5109 /* check SA values to be mature. */
5110 if ((error = key_mature(newsav)) != 0) {
5111 key_freesav(newsav);
5112 return key_senderror(so, m, error);
5116 * don't call key_freesav() here, as we would like to keep the SA
5117 * in the database on success.
5123 /* set msg buf from mhp */
5124 n = key_getmsgbuf_x1(m, mhp);
5126 ipseclog((LOG_DEBUG, "key_update: No more memory.\n"));
5127 return key_senderror(so, m, ENOBUFS);
5131 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5137 key_setident(sah, m, mhp)
5138 struct secashead *sah;
5140 const struct sadb_msghdr *mhp;
5142 const struct sadb_ident *idsrc, *iddst;
5143 int idsrclen, iddstlen;
5146 if (sah == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
5147 panic("key_setident: NULL pointer is passed.\n");
5149 /* don't make buffer if not there */
5150 if (mhp->ext[SADB_EXT_IDENTITY_SRC] == NULL &&
5151 mhp->ext[SADB_EXT_IDENTITY_DST] == NULL) {
5157 if (mhp->ext[SADB_EXT_IDENTITY_SRC] == NULL ||
5158 mhp->ext[SADB_EXT_IDENTITY_DST] == NULL) {
5159 ipseclog((LOG_DEBUG, "key_setident: invalid identity.\n"));
5163 idsrc = (const struct sadb_ident *)mhp->ext[SADB_EXT_IDENTITY_SRC];
5164 iddst = (const struct sadb_ident *)mhp->ext[SADB_EXT_IDENTITY_DST];
5165 idsrclen = mhp->extlen[SADB_EXT_IDENTITY_SRC];
5166 iddstlen = mhp->extlen[SADB_EXT_IDENTITY_DST];
5168 /* validity check */
5169 if (idsrc->sadb_ident_type != iddst->sadb_ident_type) {
5170 ipseclog((LOG_DEBUG, "key_setident: ident type mismatch.\n"));
5174 switch (idsrc->sadb_ident_type) {
5175 case SADB_IDENTTYPE_PREFIX:
5176 case SADB_IDENTTYPE_FQDN:
5177 case SADB_IDENTTYPE_USERFQDN:
5179 /* XXX do nothing */
5185 /* make structure */
5186 KMALLOC(sah->idents, struct sadb_ident *, idsrclen);
5187 if (sah->idents == NULL) {
5188 ipseclog((LOG_DEBUG, "key_setident: No more memory.\n"));
5191 KMALLOC(sah->identd, struct sadb_ident *, iddstlen);
5192 if (sah->identd == NULL) {
5195 ipseclog((LOG_DEBUG, "key_setident: No more memory.\n"));
5198 bcopy(idsrc, sah->idents, idsrclen);
5199 bcopy(iddst, sah->identd, iddstlen);
5205 * m will not be freed on return.
5206 * it is caller's responsibility to free the result.
5208 static struct mbuf *
5209 key_getmsgbuf_x1(m, mhp)
5211 const struct sadb_msghdr *mhp;
5216 if (m == NULL || mhp == NULL || mhp->msg == NULL)
5217 panic("key_getmsgbuf_x1: NULL pointer is passed.\n");
5219 /* create new sadb_msg to reply. */
5220 n = key_gather_mbuf(m, mhp, 1, 9, SADB_EXT_RESERVED,
5221 SADB_EXT_SA, SADB_X_EXT_SA2,
5222 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST,
5223 SADB_EXT_LIFETIME_HARD, SADB_EXT_LIFETIME_SOFT,
5224 SADB_EXT_IDENTITY_SRC, SADB_EXT_IDENTITY_DST);
5228 if (n->m_len < sizeof(struct sadb_msg)) {
5229 n = m_pullup(n, sizeof(struct sadb_msg));
5233 mtod(n, struct sadb_msg *)->sadb_msg_errno = 0;
5234 mtod(n, struct sadb_msg *)->sadb_msg_len =
5235 PFKEY_UNIT64(n->m_pkthdr.len);
5240 static int key_delete_all __P((struct socket *, struct mbuf *,
5241 const struct sadb_msghdr *, u_int16_t));
5244 * SADB_DELETE processing
5246 * <base, SA(*), address(SD)>
5247 * from the ikmpd, and set SADB_SASTATE_DEAD,
5249 * <base, SA(*), address(SD)>
5252 * m will always be freed.
5255 key_delete(so, m, mhp)
5258 const struct sadb_msghdr *mhp;
5260 struct sadb_sa *sa0;
5261 struct sadb_address *src0, *dst0;
5262 struct secasindex saidx;
5263 struct secashead *sah;
5264 struct secasvar *sav = NULL;
5268 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
5269 panic("key_delete: NULL pointer is passed.\n");
5271 /* map satype to proto */
5272 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5273 ipseclog((LOG_DEBUG, "key_delete: invalid satype is passed.\n"));
5274 return key_senderror(so, m, EINVAL);
5277 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5278 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
5279 ipseclog((LOG_DEBUG, "key_delete: invalid message is passed.\n"));
5280 return key_senderror(so, m, EINVAL);
5283 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5284 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5285 ipseclog((LOG_DEBUG, "key_delete: invalid message is passed.\n"));
5286 return key_senderror(so, m, EINVAL);
5289 if (mhp->ext[SADB_EXT_SA] == NULL) {
5291 * Caller wants us to delete all non-LARVAL SAs
5292 * that match the src/dst. This is used during
5293 * IKE INITIAL-CONTACT.
5295 ipseclog((LOG_DEBUG, "key_delete: doing delete all.\n"));
5296 return key_delete_all(so, m, mhp, proto);
5297 } else if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa)) {
5298 ipseclog((LOG_DEBUG, "key_delete: invalid message is passed.\n"));
5299 return key_senderror(so, m, EINVAL);
5302 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5303 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
5304 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
5306 /* XXX boundary check against sa_len */
5307 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5309 /* get a SA header */
5310 LIST_FOREACH(sah, &sahtree, chain) {
5311 if (sah->state == SADB_SASTATE_DEAD)
5313 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5316 /* get a SA with SPI. */
5317 sav = key_getsavbyspi(sah, sa0->sadb_sa_spi);
5322 ipseclog((LOG_DEBUG, "key_delete: no SA found.\n"));
5323 return key_senderror(so, m, ENOENT);
5326 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
5332 struct sadb_msg *newmsg;
5334 /* create new sadb_msg to reply. */
5335 n = key_gather_mbuf(m, mhp, 1, 4, SADB_EXT_RESERVED,
5336 SADB_EXT_SA, SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
5338 return key_senderror(so, m, ENOBUFS);
5340 if (n->m_len < sizeof(struct sadb_msg)) {
5341 n = m_pullup(n, sizeof(struct sadb_msg));
5343 return key_senderror(so, m, ENOBUFS);
5345 newmsg = mtod(n, struct sadb_msg *);
5346 newmsg->sadb_msg_errno = 0;
5347 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
5350 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5355 * delete all SAs for src/dst. Called from key_delete().
5358 key_delete_all(so, m, mhp, proto)
5361 const struct sadb_msghdr *mhp;
5364 struct sadb_address *src0, *dst0;
5365 struct secasindex saidx;
5366 struct secashead *sah;
5367 struct secasvar *sav, *nextsav;
5368 u_int stateidx, state;
5370 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
5371 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
5373 /* XXX boundary check against sa_len */
5374 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5376 LIST_FOREACH(sah, &sahtree, chain) {
5377 if (sah->state == SADB_SASTATE_DEAD)
5379 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5382 /* Delete all non-LARVAL SAs. */
5384 stateidx < _ARRAYLEN(saorder_state_alive);
5386 state = saorder_state_alive[stateidx];
5387 if (state == SADB_SASTATE_LARVAL)
5389 for (sav = LIST_FIRST(&sah->savtree[state]);
5390 sav != NULL; sav = nextsav) {
5391 nextsav = LIST_NEXT(sav, chain);
5393 if (sav->state != state) {
5394 ipseclog((LOG_DEBUG, "key_delete_all: "
5395 "invalid sav->state "
5396 "(queue: %d SA: %d)\n",
5397 state, sav->state));
5401 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
5408 struct sadb_msg *newmsg;
5410 /* create new sadb_msg to reply. */
5411 n = key_gather_mbuf(m, mhp, 1, 3, SADB_EXT_RESERVED,
5412 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
5414 return key_senderror(so, m, ENOBUFS);
5416 if (n->m_len < sizeof(struct sadb_msg)) {
5417 n = m_pullup(n, sizeof(struct sadb_msg));
5419 return key_senderror(so, m, ENOBUFS);
5421 newmsg = mtod(n, struct sadb_msg *);
5422 newmsg->sadb_msg_errno = 0;
5423 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
5426 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5431 * SADB_GET processing
5433 * <base, SA(*), address(SD)>
5434 * from the ikmpd, and get a SP and a SA to respond,
5436 * <base, SA, (lifetime(HSC),) address(SD), (address(P),) key(AE),
5437 * (identity(SD),) (sensitivity)>
5440 * m will always be freed.
5446 const struct sadb_msghdr *mhp;
5448 struct sadb_sa *sa0;
5449 struct sadb_address *src0, *dst0;
5450 struct secasindex saidx;
5451 struct secashead *sah;
5452 struct secasvar *sav = NULL;
5456 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
5457 panic("key_get: NULL pointer is passed.\n");
5459 /* map satype to proto */
5460 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5461 ipseclog((LOG_DEBUG, "key_get: invalid satype is passed.\n"));
5462 return key_senderror(so, m, EINVAL);
5465 if (mhp->ext[SADB_EXT_SA] == NULL ||
5466 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5467 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
5468 ipseclog((LOG_DEBUG, "key_get: invalid message is passed.\n"));
5469 return key_senderror(so, m, EINVAL);
5471 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
5472 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5473 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5474 ipseclog((LOG_DEBUG, "key_get: invalid message is passed.\n"));
5475 return key_senderror(so, m, EINVAL);
5478 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5479 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
5480 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
5482 /* XXX boundary check against sa_len */
5483 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5485 /* get a SA header */
5486 LIST_FOREACH(sah, &sahtree, chain) {
5487 if (sah->state == SADB_SASTATE_DEAD)
5489 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5492 /* get a SA with SPI. */
5493 sav = key_getsavbyspi(sah, sa0->sadb_sa_spi);
5498 ipseclog((LOG_DEBUG, "key_get: no SA found.\n"));
5499 return key_senderror(so, m, ENOENT);
5506 /* map proto to satype */
5507 if ((satype = key_proto2satype(sah->saidx.proto)) == 0) {
5508 ipseclog((LOG_DEBUG, "key_get: there was invalid proto in SAD.\n"));
5509 return key_senderror(so, m, EINVAL);
5512 /* create new sadb_msg to reply. */
5513 n = key_setdumpsa(sav, SADB_GET, satype, mhp->msg->sadb_msg_seq,
5514 mhp->msg->sadb_msg_pid);
5516 return key_senderror(so, m, ENOBUFS);
5519 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
5523 /* XXX make it sysctl-configurable? */
5525 key_getcomb_setlifetime(comb)
5526 struct sadb_comb *comb;
5529 comb->sadb_comb_soft_allocations = 1;
5530 comb->sadb_comb_hard_allocations = 1;
5531 comb->sadb_comb_soft_bytes = 0;
5532 comb->sadb_comb_hard_bytes = 0;
5533 comb->sadb_comb_hard_addtime = 86400; /* 1 day */
5534 comb->sadb_comb_soft_addtime = comb->sadb_comb_soft_addtime * 80 / 100;
5535 comb->sadb_comb_soft_usetime = 28800; /* 8 hours */
5536 comb->sadb_comb_hard_usetime = comb->sadb_comb_hard_usetime * 80 / 100;
5541 * XXX reorder combinations by preference
5542 * XXX no idea if the user wants ESP authentication or not
5544 static struct mbuf *
5547 struct sadb_comb *comb;
5548 const struct esp_algorithm *algo;
5549 struct mbuf *result = NULL, *m, *n;
5553 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
5556 for (i = 1; i <= SADB_EALG_MAX; i++) {
5557 algo = esp_algorithm_lookup(i);
5561 if (algo->keymax < ipsec_esp_keymin)
5563 if (algo->keymin < ipsec_esp_keymin)
5564 encmin = ipsec_esp_keymin;
5566 encmin = algo->keymin;
5569 m = key_getcomb_ah();
5573 panic("assumption failed in key_getcomb_esp");
5575 MGET(m, M_DONTWAIT, MT_DATA);
5580 bzero(mtod(m, caddr_t), m->m_len);
5587 for (n = m; n; n = n->m_next)
5591 panic("assumption failed in key_getcomb_esp");
5594 for (off = 0; off < totlen; off += l) {
5595 n = m_pulldown(m, off, l, &o);
5597 /* m is already freed */
5600 comb = (struct sadb_comb *)(mtod(n, caddr_t) + o);
5601 bzero(comb, sizeof(*comb));
5602 key_getcomb_setlifetime(comb);
5603 comb->sadb_comb_encrypt = i;
5604 comb->sadb_comb_encrypt_minbits = encmin;
5605 comb->sadb_comb_encrypt_maxbits = algo->keymax;
5624 * XXX reorder combinations by preference
5626 static struct mbuf *
5629 struct sadb_comb *comb;
5630 const struct ah_algorithm *algo;
5634 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
5637 for (i = 1; i <= SADB_AALG_MAX; i++) {
5639 /* we prefer HMAC algorithms, not old algorithms */
5640 if (i != SADB_AALG_SHA1HMAC && i != SADB_AALG_MD5HMAC)
5643 algo = ah_algorithm_lookup(i);
5647 if (algo->keymax < ipsec_ah_keymin)
5649 if (algo->keymin < ipsec_ah_keymin)
5650 min = ipsec_ah_keymin;
5657 panic("assumption failed in key_getcomb_ah");
5659 MGET(m, M_DONTWAIT, MT_DATA);
5666 M_PREPEND(m, l, M_DONTWAIT);
5670 comb = mtod(m, struct sadb_comb *);
5671 bzero(comb, sizeof(*comb));
5672 key_getcomb_setlifetime(comb);
5673 comb->sadb_comb_auth = i;
5674 comb->sadb_comb_auth_minbits = min;
5675 comb->sadb_comb_auth_maxbits = algo->keymax;
5682 * not really an official behavior. discussed in pf_key@inner.net in Sep2000.
5683 * XXX reorder combinations by preference
5685 static struct mbuf *
5686 key_getcomb_ipcomp()
5688 struct sadb_comb *comb;
5689 const struct ipcomp_algorithm *algo;
5692 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
5695 for (i = 1; i <= SADB_X_CALG_MAX; i++) {
5696 algo = ipcomp_algorithm_lookup(i);
5703 panic("assumption failed in key_getcomb_ipcomp");
5705 MGET(m, M_DONTWAIT, MT_DATA);
5712 M_PREPEND(m, l, M_DONTWAIT);
5716 comb = mtod(m, struct sadb_comb *);
5717 bzero(comb, sizeof(*comb));
5718 key_getcomb_setlifetime(comb);
5719 comb->sadb_comb_encrypt = i;
5720 /* what should we set into sadb_comb_*_{min,max}bits? */
5727 * XXX no way to pass mode (transport/tunnel) to userland
5728 * XXX replay checking?
5729 * XXX sysctl interface to ipsec_{ah,esp}_keymin
5731 static struct mbuf *
5733 const struct secasindex *saidx;
5735 struct sadb_prop *prop;
5737 const int l = PFKEY_ALIGN8(sizeof(struct sadb_prop));
5740 switch (saidx->proto) {
5743 m = key_getcomb_esp();
5747 m = key_getcomb_ah();
5749 case IPPROTO_IPCOMP:
5750 m = key_getcomb_ipcomp();
5758 M_PREPEND(m, l, M_DONTWAIT);
5763 for (n = m; n; n = n->m_next)
5766 prop = mtod(m, struct sadb_prop *);
5767 bzero(prop, sizeof(*prop));
5768 prop->sadb_prop_len = PFKEY_UNIT64(totlen);
5769 prop->sadb_prop_exttype = SADB_EXT_PROPOSAL;
5770 prop->sadb_prop_replay = 32; /* XXX */
5776 * SADB_ACQUIRE processing called by key_checkrequest() and key_acquire2().
5778 * <base, SA, address(SD), (address(P)), x_policy,
5779 * (identity(SD),) (sensitivity,) proposal>
5780 * to KMD, and expect to receive
5781 * <base> with SADB_ACQUIRE if error occured,
5783 * <base, src address, dst address, (SPI range)> with SADB_GETSPI
5784 * from KMD by PF_KEY.
5786 * XXX x_policy is outside of RFC2367 (KAME extension).
5787 * XXX sensitivity is not supported.
5788 * XXX for ipcomp, RFC2367 does not define how to fill in proposal.
5789 * see comment for key_getcomb_ipcomp().
5793 * others: error number
5796 key_acquire(saidx, sp)
5797 struct secasindex *saidx;
5798 struct secpolicy *sp;
5800 struct mbuf *result = NULL, *m;
5801 #ifndef IPSEC_NONBLOCK_ACQUIRE
5802 struct secacq *newacq;
5810 panic("key_acquire: NULL pointer is passed.\n");
5811 if ((satype = key_proto2satype(saidx->proto)) == 0)
5812 panic("key_acquire: invalid proto is passed.\n");
5814 #ifndef IPSEC_NONBLOCK_ACQUIRE
5816 * We never do anything about acquirng SA. There is anather
5817 * solution that kernel blocks to send SADB_ACQUIRE message until
5818 * getting something message from IKEd. In later case, to be
5819 * managed with ACQUIRING list.
5821 /* get a entry to check whether sending message or not. */
5822 if ((newacq = key_getacq(saidx)) != NULL) {
5823 if (key_blockacq_count < newacq->count) {
5824 /* reset counter and do send message. */
5827 /* increment counter and do nothing. */
5832 /* make new entry for blocking to send SADB_ACQUIRE. */
5833 if ((newacq = key_newacq(saidx)) == NULL)
5836 /* add to acqtree */
5837 LIST_INSERT_HEAD(&acqtree, newacq, chain);
5842 #ifndef IPSEC_NONBLOCK_ACQUIRE
5845 seq = (acq_seq = (acq_seq == ~0 ? 1 : ++acq_seq));
5847 m = key_setsadbmsg(SADB_ACQUIRE, 0, satype, seq, 0, 0);
5854 /* set sadb_address for saidx's. */
5855 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
5856 (struct sockaddr *)&saidx->src, FULLMASK, IPSEC_ULPROTO_ANY);
5863 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
5864 (struct sockaddr *)&saidx->dst, FULLMASK, IPSEC_ULPROTO_ANY);
5871 /* XXX proxy address (optional) */
5873 /* set sadb_x_policy */
5875 m = key_setsadbxpolicy(sp->policy, sp->spidx.dir, sp->id);
5883 /* XXX identity (optional) */
5885 if (idexttype && fqdn) {
5886 /* create identity extension (FQDN) */
5887 struct sadb_ident *id;
5890 fqdnlen = strlen(fqdn) + 1; /* +1 for terminating-NUL */
5891 id = (struct sadb_ident *)p;
5892 bzero(id, sizeof(*id) + PFKEY_ALIGN8(fqdnlen));
5893 id->sadb_ident_len = PFKEY_UNIT64(sizeof(*id) + PFKEY_ALIGN8(fqdnlen));
5894 id->sadb_ident_exttype = idexttype;
5895 id->sadb_ident_type = SADB_IDENTTYPE_FQDN;
5896 bcopy(fqdn, id + 1, fqdnlen);
5897 p += sizeof(struct sadb_ident) + PFKEY_ALIGN8(fqdnlen);
5901 /* create identity extension (USERFQDN) */
5902 struct sadb_ident *id;
5906 /* +1 for terminating-NUL */
5907 userfqdnlen = strlen(userfqdn) + 1;
5910 id = (struct sadb_ident *)p;
5911 bzero(id, sizeof(*id) + PFKEY_ALIGN8(userfqdnlen));
5912 id->sadb_ident_len = PFKEY_UNIT64(sizeof(*id) + PFKEY_ALIGN8(userfqdnlen));
5913 id->sadb_ident_exttype = idexttype;
5914 id->sadb_ident_type = SADB_IDENTTYPE_USERFQDN;
5915 /* XXX is it correct? */
5916 if (curproc && curproc->p_cred)
5917 id->sadb_ident_id = curproc->p_cred->p_ruid;
5918 if (userfqdn && userfqdnlen)
5919 bcopy(userfqdn, id + 1, userfqdnlen);
5920 p += sizeof(struct sadb_ident) + PFKEY_ALIGN8(userfqdnlen);
5924 /* XXX sensitivity (optional) */
5926 /* create proposal/combination extension */
5927 m = key_getprop(saidx);
5930 * spec conformant: always attach proposal/combination extension,
5931 * the problem is that we have no way to attach it for ipcomp,
5932 * due to the way sadb_comb is declared in RFC2367.
5941 * outside of spec; make proposal/combination extension optional.
5947 if ((result->m_flags & M_PKTHDR) == 0) {
5952 if (result->m_len < sizeof(struct sadb_msg)) {
5953 result = m_pullup(result, sizeof(struct sadb_msg));
5954 if (result == NULL) {
5960 result->m_pkthdr.len = 0;
5961 for (m = result; m; m = m->m_next)
5962 result->m_pkthdr.len += m->m_len;
5964 mtod(result, struct sadb_msg *)->sadb_msg_len =
5965 PFKEY_UNIT64(result->m_pkthdr.len);
5967 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
5975 #ifndef IPSEC_NONBLOCK_ACQUIRE
5976 static struct secacq *
5978 struct secasindex *saidx;
5980 struct secacq *newacq;
5984 KMALLOC(newacq, struct secacq *, sizeof(struct secacq));
5985 if (newacq == NULL) {
5986 ipseclog((LOG_DEBUG, "key_newacq: No more memory.\n"));
5989 bzero(newacq, sizeof(*newacq));
5992 bcopy(saidx, &newacq->saidx, sizeof(newacq->saidx));
5993 newacq->seq = (acq_seq == ~0 ? 1 : ++acq_seq);
5995 newacq->created = tv.tv_sec;
6001 static struct secacq *
6003 struct secasindex *saidx;
6007 LIST_FOREACH(acq, &acqtree, chain) {
6008 if (key_cmpsaidx(saidx, &acq->saidx, CMP_EXACTLY))
6015 static struct secacq *
6016 key_getacqbyseq(seq)
6021 LIST_FOREACH(acq, &acqtree, chain) {
6022 if (acq->seq == seq)
6030 static struct secspacq *
6032 struct secpolicyindex *spidx;
6034 struct secspacq *acq;
6038 KMALLOC(acq, struct secspacq *, sizeof(struct secspacq));
6040 ipseclog((LOG_DEBUG, "key_newspacq: No more memory.\n"));
6043 bzero(acq, sizeof(*acq));
6046 bcopy(spidx, &acq->spidx, sizeof(acq->spidx));
6048 acq->created = tv.tv_sec;
6054 static struct secspacq *
6056 struct secpolicyindex *spidx;
6058 struct secspacq *acq;
6060 LIST_FOREACH(acq, &spacqtree, chain) {
6061 if (key_cmpspidx_exactly(spidx, &acq->spidx))
6069 * SADB_ACQUIRE processing,
6070 * in first situation, is receiving
6072 * from the ikmpd, and clear sequence of its secasvar entry.
6074 * In second situation, is receiving
6075 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
6076 * from a user land process, and return
6077 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
6080 * m will always be freed.
6083 key_acquire2(so, m, mhp)
6086 const struct sadb_msghdr *mhp;
6088 const struct sadb_address *src0, *dst0;
6089 struct secasindex saidx;
6090 struct secashead *sah;
6095 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
6096 panic("key_acquire2: NULL pointer is passed.\n");
6099 * Error message from KMd.
6100 * We assume that if error was occured in IKEd, the length of PFKEY
6101 * message is equal to the size of sadb_msg structure.
6102 * We do not raise error even if error occured in this function.
6104 if (mhp->msg->sadb_msg_len == PFKEY_UNIT64(sizeof(struct sadb_msg))) {
6105 #ifndef IPSEC_NONBLOCK_ACQUIRE
6109 /* check sequence number */
6110 if (mhp->msg->sadb_msg_seq == 0) {
6111 ipseclog((LOG_DEBUG, "key_acquire2: must specify sequence number.\n"));
6116 if ((acq = key_getacqbyseq(mhp->msg->sadb_msg_seq)) == NULL) {
6118 * the specified larval SA is already gone, or we got
6119 * a bogus sequence number. we can silently ignore it.
6125 /* reset acq counter in order to deletion by timehander. */
6127 acq->created = tv.tv_sec;
6135 * This message is from user land.
6138 /* map satype to proto */
6139 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
6140 ipseclog((LOG_DEBUG, "key_acquire2: invalid satype is passed.\n"));
6141 return key_senderror(so, m, EINVAL);
6144 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
6145 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
6146 mhp->ext[SADB_EXT_PROPOSAL] == NULL) {
6148 ipseclog((LOG_DEBUG, "key_acquire2: invalid message is passed.\n"));
6149 return key_senderror(so, m, EINVAL);
6151 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
6152 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
6153 mhp->extlen[SADB_EXT_PROPOSAL] < sizeof(struct sadb_prop)) {
6155 ipseclog((LOG_DEBUG, "key_acquire2: invalid message is passed.\n"));
6156 return key_senderror(so, m, EINVAL);
6159 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
6160 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
6162 /* XXX boundary check against sa_len */
6163 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
6165 /* get a SA index */
6166 LIST_FOREACH(sah, &sahtree, chain) {
6167 if (sah->state == SADB_SASTATE_DEAD)
6169 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_MODE_REQID))
6173 ipseclog((LOG_DEBUG, "key_acquire2: a SA exists already.\n"));
6174 return key_senderror(so, m, EEXIST);
6177 error = key_acquire(&saidx, NULL);
6179 ipseclog((LOG_DEBUG, "key_acquire2: error %d returned "
6180 "from key_acquire.\n", mhp->msg->sadb_msg_errno));
6181 return key_senderror(so, m, error);
6184 return key_sendup_mbuf(so, m, KEY_SENDUP_REGISTERED);
6188 * SADB_REGISTER processing.
6189 * If SATYPE_UNSPEC has been passed as satype, only return sabd_supported.
6192 * from the ikmpd, and register a socket to send PF_KEY messages,
6196 * If socket is detached, must free from regnode.
6198 * m will always be freed.
6201 key_register(so, m, mhp)
6204 const struct sadb_msghdr *mhp;
6206 struct secreg *reg, *newreg = 0;
6209 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
6210 panic("key_register: NULL pointer is passed.\n");
6212 /* check for invalid register message */
6213 if (mhp->msg->sadb_msg_satype >= sizeof(regtree)/sizeof(regtree[0]))
6214 return key_senderror(so, m, EINVAL);
6216 /* When SATYPE_UNSPEC is specified, only return sabd_supported. */
6217 if (mhp->msg->sadb_msg_satype == SADB_SATYPE_UNSPEC)
6220 /* check whether existing or not */
6221 LIST_FOREACH(reg, ®tree[mhp->msg->sadb_msg_satype], chain) {
6222 if (reg->so == so) {
6223 ipseclog((LOG_DEBUG, "key_register: socket exists already.\n"));
6224 return key_senderror(so, m, EEXIST);
6228 /* create regnode */
6229 KMALLOC(newreg, struct secreg *, sizeof(*newreg));
6230 if (newreg == NULL) {
6231 ipseclog((LOG_DEBUG, "key_register: No more memory.\n"));
6232 return key_senderror(so, m, ENOBUFS);
6234 bzero((caddr_t)newreg, sizeof(*newreg));
6237 ((struct keycb *)sotorawcb(so))->kp_registered++;
6239 /* add regnode to regtree. */
6240 LIST_INSERT_HEAD(®tree[mhp->msg->sadb_msg_satype], newreg, chain);
6245 struct sadb_msg *newmsg;
6246 struct sadb_supported *sup;
6247 u_int len, alen, elen;
6250 struct sadb_alg *alg;
6252 /* create new sadb_msg to reply. */
6254 for (i = 1; i <= SADB_AALG_MAX; i++) {
6255 if (ah_algorithm_lookup(i))
6256 alen += sizeof(struct sadb_alg);
6259 alen += sizeof(struct sadb_supported);
6262 for (i = 1; i <= SADB_EALG_MAX; i++) {
6263 if (esp_algorithm_lookup(i))
6264 elen += sizeof(struct sadb_alg);
6267 elen += sizeof(struct sadb_supported);
6270 len = sizeof(struct sadb_msg) + alen + elen;
6273 return key_senderror(so, m, ENOBUFS);
6275 MGETHDR(n, M_DONTWAIT, MT_DATA);
6277 MCLGET(n, M_DONTWAIT);
6278 if ((n->m_flags & M_EXT) == 0) {
6284 return key_senderror(so, m, ENOBUFS);
6286 n->m_pkthdr.len = n->m_len = len;
6290 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
6291 newmsg = mtod(n, struct sadb_msg *);
6292 newmsg->sadb_msg_errno = 0;
6293 newmsg->sadb_msg_len = PFKEY_UNIT64(len);
6294 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
6296 /* for authentication algorithm */
6298 sup = (struct sadb_supported *)(mtod(n, caddr_t) + off);
6299 sup->sadb_supported_len = PFKEY_UNIT64(alen);
6300 sup->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
6301 off += PFKEY_ALIGN8(sizeof(*sup));
6303 for (i = 1; i <= SADB_AALG_MAX; i++) {
6304 const struct ah_algorithm *aalgo;
6306 aalgo = ah_algorithm_lookup(i);
6309 alg = (struct sadb_alg *)(mtod(n, caddr_t) + off);
6310 alg->sadb_alg_id = i;
6311 alg->sadb_alg_ivlen = 0;
6312 alg->sadb_alg_minbits = aalgo->keymin;
6313 alg->sadb_alg_maxbits = aalgo->keymax;
6314 off += PFKEY_ALIGN8(sizeof(*alg));
6319 /* for encryption algorithm */
6321 sup = (struct sadb_supported *)(mtod(n, caddr_t) + off);
6322 sup->sadb_supported_len = PFKEY_UNIT64(elen);
6323 sup->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT;
6324 off += PFKEY_ALIGN8(sizeof(*sup));
6326 for (i = 1; i <= SADB_EALG_MAX; i++) {
6327 const struct esp_algorithm *ealgo;
6329 ealgo = esp_algorithm_lookup(i);
6332 alg = (struct sadb_alg *)(mtod(n, caddr_t) + off);
6333 alg->sadb_alg_id = i;
6334 if (ealgo && ealgo->ivlen) {
6336 * give NULL to get the value preferred by
6337 * algorithm XXX SADB_X_EXT_DERIV ?
6339 alg->sadb_alg_ivlen =
6340 (*ealgo->ivlen)(ealgo, NULL);
6342 alg->sadb_alg_ivlen = 0;
6343 alg->sadb_alg_minbits = ealgo->keymin;
6344 alg->sadb_alg_maxbits = ealgo->keymax;
6345 off += PFKEY_ALIGN8(sizeof(struct sadb_alg));
6352 panic("length assumption failed in key_register");
6356 return key_sendup_mbuf(so, n, KEY_SENDUP_REGISTERED);
6361 * free secreg entry registered.
6362 * XXX: I want to do free a socket marked done SADB_RESIGER to socket.
6373 panic("key_freereg: NULL pointer is passed.\n");
6376 * check whether existing or not.
6377 * check all type of SA, because there is a potential that
6378 * one socket is registered to multiple type of SA.
6380 for (i = 0; i <= SADB_SATYPE_MAX; i++) {
6381 LIST_FOREACH(reg, ®tree[i], chain) {
6383 && __LIST_CHAINED(reg)) {
6384 LIST_REMOVE(reg, chain);
6395 * SADB_EXPIRE processing
6397 * <base, SA, SA2, lifetime(C and one of HS), address(SD)>
6399 * NOTE: We send only soft lifetime extension.
6402 * others : error number
6406 struct secasvar *sav;
6410 struct mbuf *result = NULL, *m;
6413 struct sadb_lifetime *lt;
6415 /* XXX: Why do we lock ? */
6416 s = splnet(); /*called from softclock()*/
6420 panic("key_expire: NULL pointer is passed.\n");
6421 if (sav->sah == NULL)
6422 panic("key_expire: Why was SA index in SA NULL.\n");
6423 if ((satype = key_proto2satype(sav->sah->saidx.proto)) == 0)
6424 panic("key_expire: invalid proto is passed.\n");
6426 /* set msg header */
6427 m = key_setsadbmsg(SADB_EXPIRE, 0, satype, sav->seq, 0, sav->refcnt);
6434 /* create SA extension */
6435 m = key_setsadbsa(sav);
6442 /* create SA extension */
6443 m = key_setsadbxsa2(sav->sah->saidx.mode,
6444 sav->replay ? sav->replay->count : 0,
6445 sav->sah->saidx.reqid);
6452 /* create lifetime extension (current and soft) */
6453 len = PFKEY_ALIGN8(sizeof(*lt)) * 2;
6454 m = key_alloc_mbuf(len);
6455 if (!m || m->m_next) { /*XXX*/
6461 bzero(mtod(m, caddr_t), len);
6462 lt = mtod(m, struct sadb_lifetime *);
6463 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
6464 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
6465 lt->sadb_lifetime_allocations = sav->lft_c->sadb_lifetime_allocations;
6466 lt->sadb_lifetime_bytes = sav->lft_c->sadb_lifetime_bytes;
6467 lt->sadb_lifetime_addtime = sav->lft_c->sadb_lifetime_addtime;
6468 lt->sadb_lifetime_usetime = sav->lft_c->sadb_lifetime_usetime;
6469 lt = (struct sadb_lifetime *)(mtod(m, caddr_t) + len / 2);
6470 bcopy(sav->lft_s, lt, sizeof(*lt));
6473 /* set sadb_address for source */
6474 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
6475 (struct sockaddr *)&sav->sah->saidx.src,
6476 FULLMASK, IPSEC_ULPROTO_ANY);
6483 /* set sadb_address for destination */
6484 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
6485 (struct sockaddr *)&sav->sah->saidx.dst,
6486 FULLMASK, IPSEC_ULPROTO_ANY);
6493 if ((result->m_flags & M_PKTHDR) == 0) {
6498 if (result->m_len < sizeof(struct sadb_msg)) {
6499 result = m_pullup(result, sizeof(struct sadb_msg));
6500 if (result == NULL) {
6506 result->m_pkthdr.len = 0;
6507 for (m = result; m; m = m->m_next)
6508 result->m_pkthdr.len += m->m_len;
6510 mtod(result, struct sadb_msg *)->sadb_msg_len =
6511 PFKEY_UNIT64(result->m_pkthdr.len);
6514 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
6524 * SADB_FLUSH processing
6527 * from the ikmpd, and free all entries in secastree.
6531 * NOTE: to do is only marking SADB_SASTATE_DEAD.
6533 * m will always be freed.
6536 key_flush(so, m, mhp)
6539 const struct sadb_msghdr *mhp;
6541 struct sadb_msg *newmsg;
6542 struct secashead *sah, *nextsah;
6543 struct secasvar *sav, *nextsav;
6549 if (so == NULL || mhp == NULL || mhp->msg == NULL)
6550 panic("key_flush: NULL pointer is passed.\n");
6552 /* map satype to proto */
6553 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
6554 ipseclog((LOG_DEBUG, "key_flush: invalid satype is passed.\n"));
6555 return key_senderror(so, m, EINVAL);
6558 /* no SATYPE specified, i.e. flushing all SA. */
6559 for (sah = LIST_FIRST(&sahtree);
6562 nextsah = LIST_NEXT(sah, chain);
6564 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
6565 && proto != sah->saidx.proto)
6569 stateidx < _ARRAYLEN(saorder_state_alive);
6571 state = saorder_state_any[stateidx];
6572 for (sav = LIST_FIRST(&sah->savtree[state]);
6576 nextsav = LIST_NEXT(sav, chain);
6578 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
6583 sah->state = SADB_SASTATE_DEAD;
6586 if (m->m_len < sizeof(struct sadb_msg) ||
6587 sizeof(struct sadb_msg) > m->m_len + M_TRAILINGSPACE(m)) {
6588 ipseclog((LOG_DEBUG, "key_flush: No more memory.\n"));
6589 return key_senderror(so, m, ENOBUFS);
6595 m->m_pkthdr.len = m->m_len = sizeof(struct sadb_msg);
6596 newmsg = mtod(m, struct sadb_msg *);
6597 newmsg->sadb_msg_errno = 0;
6598 newmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
6600 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
6604 * SADB_DUMP processing
6605 * dump all entries including status of DEAD in SAD.
6608 * from the ikmpd, and dump all secasvar leaves
6613 * m will always be freed.
6616 key_dump(so, m, mhp)
6619 const struct sadb_msghdr *mhp;
6621 struct secashead *sah;
6622 struct secasvar *sav;
6628 struct sadb_msg *newmsg;
6632 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
6633 panic("key_dump: NULL pointer is passed.\n");
6635 /* map satype to proto */
6636 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
6637 ipseclog((LOG_DEBUG, "key_dump: invalid satype is passed.\n"));
6638 return key_senderror(so, m, EINVAL);
6641 /* count sav entries to be sent to the userland. */
6643 LIST_FOREACH(sah, &sahtree, chain) {
6644 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
6645 && proto != sah->saidx.proto)
6649 stateidx < _ARRAYLEN(saorder_state_any);
6651 state = saorder_state_any[stateidx];
6652 LIST_FOREACH(sav, &sah->savtree[state], chain) {
6659 return key_senderror(so, m, ENOENT);
6661 /* send this to the userland, one at a time. */
6663 LIST_FOREACH(sah, &sahtree, chain) {
6664 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
6665 && proto != sah->saidx.proto)
6668 /* map proto to satype */
6669 if ((satype = key_proto2satype(sah->saidx.proto)) == 0) {
6670 ipseclog((LOG_DEBUG, "key_dump: there was invalid proto in SAD.\n"));
6671 return key_senderror(so, m, EINVAL);
6675 stateidx < _ARRAYLEN(saorder_state_any);
6677 state = saorder_state_any[stateidx];
6678 LIST_FOREACH(sav, &sah->savtree[state], chain) {
6679 n = key_setdumpsa(sav, SADB_DUMP, satype,
6680 --cnt, mhp->msg->sadb_msg_pid);
6682 return key_senderror(so, m, ENOBUFS);
6684 key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
6694 * SADB_X_PROMISC processing
6696 * m will always be freed.
6699 key_promisc(so, m, mhp)
6702 const struct sadb_msghdr *mhp;
6707 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
6708 panic("key_promisc: NULL pointer is passed.\n");
6710 olen = PFKEY_UNUNIT64(mhp->msg->sadb_msg_len);
6712 if (olen < sizeof(struct sadb_msg)) {
6714 return key_senderror(so, m, EINVAL);
6719 } else if (olen == sizeof(struct sadb_msg)) {
6720 /* enable/disable promisc mode */
6723 if ((kp = (struct keycb *)sotorawcb(so)) == NULL)
6724 return key_senderror(so, m, EINVAL);
6725 mhp->msg->sadb_msg_errno = 0;
6726 switch (mhp->msg->sadb_msg_satype) {
6729 kp->kp_promisc = mhp->msg->sadb_msg_satype;
6732 return key_senderror(so, m, EINVAL);
6735 /* send the original message back to everyone */
6736 mhp->msg->sadb_msg_errno = 0;
6737 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
6739 /* send packet as is */
6741 m_adj(m, PFKEY_ALIGN8(sizeof(struct sadb_msg)));
6743 /* TODO: if sadb_msg_seq is specified, send to specific pid */
6744 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
6748 static int (*key_typesw[]) __P((struct socket *, struct mbuf *,
6749 const struct sadb_msghdr *)) = {
6750 NULL, /* SADB_RESERVED */
6751 key_getspi, /* SADB_GETSPI */
6752 key_update, /* SADB_UPDATE */
6753 key_add, /* SADB_ADD */
6754 key_delete, /* SADB_DELETE */
6755 key_get, /* SADB_GET */
6756 key_acquire2, /* SADB_ACQUIRE */
6757 key_register, /* SADB_REGISTER */
6758 NULL, /* SADB_EXPIRE */
6759 key_flush, /* SADB_FLUSH */
6760 key_dump, /* SADB_DUMP */
6761 key_promisc, /* SADB_X_PROMISC */
6762 NULL, /* SADB_X_PCHANGE */
6763 key_spdadd, /* SADB_X_SPDUPDATE */
6764 key_spdadd, /* SADB_X_SPDADD */
6765 key_spddelete, /* SADB_X_SPDDELETE */
6766 key_spdget, /* SADB_X_SPDGET */
6767 NULL, /* SADB_X_SPDACQUIRE */
6768 key_spddump, /* SADB_X_SPDDUMP */
6769 key_spdflush, /* SADB_X_SPDFLUSH */
6770 key_spdadd, /* SADB_X_SPDSETIDX */
6771 NULL, /* SADB_X_SPDEXPIRE */
6772 key_spddelete2, /* SADB_X_SPDDELETE2 */
6776 * parse sadb_msg buffer to process PFKEYv2,
6777 * and create a data to response if needed.
6778 * I think to be dealed with mbuf directly.
6780 * msgp : pointer to pointer to a received buffer pulluped.
6781 * This is rewrited to response.
6782 * so : pointer to socket.
6784 * length for buffer to send to user process.
6791 struct sadb_msg *msg;
6792 struct sadb_msghdr mh;
6798 if (m == NULL || so == NULL)
6799 panic("key_parse: NULL pointer is passed.\n");
6801 #if 0 /*kdebug_sadb assumes msg in linear buffer*/
6802 KEYDEBUG(KEYDEBUG_KEY_DUMP,
6803 ipseclog((LOG_DEBUG, "key_parse: passed sadb_msg\n"));
6807 if (m->m_len < sizeof(struct sadb_msg)) {
6808 m = m_pullup(m, sizeof(struct sadb_msg));
6812 msg = mtod(m, struct sadb_msg *);
6813 orglen = PFKEY_UNUNIT64(msg->sadb_msg_len);
6814 target = KEY_SENDUP_ONE;
6816 if ((m->m_flags & M_PKTHDR) == 0 ||
6817 m->m_pkthdr.len != m->m_pkthdr.len) {
6818 ipseclog((LOG_DEBUG, "key_parse: invalid message length.\n"));
6819 pfkeystat.out_invlen++;
6824 if (msg->sadb_msg_version != PF_KEY_V2) {
6825 ipseclog((LOG_DEBUG,
6826 "key_parse: PF_KEY version %u is mismatched.\n",
6827 msg->sadb_msg_version));
6828 pfkeystat.out_invver++;
6833 if (msg->sadb_msg_type > SADB_MAX) {
6834 ipseclog((LOG_DEBUG, "key_parse: invalid type %u is passed.\n",
6835 msg->sadb_msg_type));
6836 pfkeystat.out_invmsgtype++;
6841 /* for old-fashioned code - should be nuked */
6842 if (m->m_pkthdr.len > MCLBYTES) {
6849 MGETHDR(n, M_DONTWAIT, MT_DATA);
6850 if (n && m->m_pkthdr.len > MHLEN) {
6851 MCLGET(n, M_DONTWAIT);
6852 if ((n->m_flags & M_EXT) == 0) {
6861 m_copydata(m, 0, m->m_pkthdr.len, mtod(n, caddr_t));
6862 n->m_pkthdr.len = n->m_len = m->m_pkthdr.len;
6868 /* align the mbuf chain so that extensions are in contiguous region. */
6869 error = key_align(m, &mh);
6873 if (m->m_next) { /*XXX*/
6881 switch (msg->sadb_msg_satype) {
6882 case SADB_SATYPE_UNSPEC:
6883 switch (msg->sadb_msg_type) {
6891 ipseclog((LOG_DEBUG, "key_parse: must specify satype "
6892 "when msg type=%u.\n", msg->sadb_msg_type));
6893 pfkeystat.out_invsatype++;
6898 case SADB_SATYPE_AH:
6899 case SADB_SATYPE_ESP:
6900 case SADB_X_SATYPE_IPCOMP:
6901 switch (msg->sadb_msg_type) {
6903 case SADB_X_SPDDELETE:
6905 case SADB_X_SPDDUMP:
6906 case SADB_X_SPDFLUSH:
6907 case SADB_X_SPDSETIDX:
6908 case SADB_X_SPDUPDATE:
6909 case SADB_X_SPDDELETE2:
6910 ipseclog((LOG_DEBUG, "key_parse: illegal satype=%u\n",
6911 msg->sadb_msg_type));
6912 pfkeystat.out_invsatype++;
6917 case SADB_SATYPE_RSVP:
6918 case SADB_SATYPE_OSPFV2:
6919 case SADB_SATYPE_RIPV2:
6920 case SADB_SATYPE_MIP:
6921 ipseclog((LOG_DEBUG, "key_parse: type %u isn't supported.\n",
6922 msg->sadb_msg_satype));
6923 pfkeystat.out_invsatype++;
6926 case 1: /* XXX: What does it do? */
6927 if (msg->sadb_msg_type == SADB_X_PROMISC)
6931 ipseclog((LOG_DEBUG, "key_parse: invalid type %u is passed.\n",
6932 msg->sadb_msg_satype));
6933 pfkeystat.out_invsatype++;
6938 /* check field of upper layer protocol and address family */
6939 if (mh.ext[SADB_EXT_ADDRESS_SRC] != NULL
6940 && mh.ext[SADB_EXT_ADDRESS_DST] != NULL) {
6941 struct sadb_address *src0, *dst0;
6944 src0 = (struct sadb_address *)(mh.ext[SADB_EXT_ADDRESS_SRC]);
6945 dst0 = (struct sadb_address *)(mh.ext[SADB_EXT_ADDRESS_DST]);
6947 /* check upper layer protocol */
6948 if (src0->sadb_address_proto != dst0->sadb_address_proto) {
6949 ipseclog((LOG_DEBUG, "key_parse: upper layer protocol mismatched.\n"));
6950 pfkeystat.out_invaddr++;
6956 if (PFKEY_ADDR_SADDR(src0)->sa_family !=
6957 PFKEY_ADDR_SADDR(dst0)->sa_family) {
6958 ipseclog((LOG_DEBUG, "key_parse: address family mismatched.\n"));
6959 pfkeystat.out_invaddr++;
6963 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
6964 PFKEY_ADDR_SADDR(dst0)->sa_len) {
6965 ipseclog((LOG_DEBUG,
6966 "key_parse: address struct size mismatched.\n"));
6967 pfkeystat.out_invaddr++;
6972 switch (PFKEY_ADDR_SADDR(src0)->sa_family) {
6974 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
6975 sizeof(struct sockaddr_in)) {
6976 pfkeystat.out_invaddr++;
6982 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
6983 sizeof(struct sockaddr_in6)) {
6984 pfkeystat.out_invaddr++;
6990 ipseclog((LOG_DEBUG,
6991 "key_parse: unsupported address family.\n"));
6992 pfkeystat.out_invaddr++;
6993 error = EAFNOSUPPORT;
6997 switch (PFKEY_ADDR_SADDR(src0)->sa_family) {
6999 plen = sizeof(struct in_addr) << 3;
7002 plen = sizeof(struct in6_addr) << 3;
7005 plen = 0; /*fool gcc*/
7009 /* check max prefix length */
7010 if (src0->sadb_address_prefixlen > plen ||
7011 dst0->sadb_address_prefixlen > plen) {
7012 ipseclog((LOG_DEBUG,
7013 "key_parse: illegal prefixlen.\n"));
7014 pfkeystat.out_invaddr++;
7020 * prefixlen == 0 is valid because there can be a case when
7021 * all addresses are matched.
7025 if (msg->sadb_msg_type >= sizeof(key_typesw)/sizeof(key_typesw[0]) ||
7026 key_typesw[msg->sadb_msg_type] == NULL) {
7027 pfkeystat.out_invmsgtype++;
7032 return (*key_typesw[msg->sadb_msg_type])(so, m, &mh);
7035 msg->sadb_msg_errno = error;
7036 return key_sendup_mbuf(so, m, target);
7040 key_senderror(so, m, code)
7045 struct sadb_msg *msg;
7047 if (m->m_len < sizeof(struct sadb_msg))
7048 panic("invalid mbuf passed to key_senderror");
7050 msg = mtod(m, struct sadb_msg *);
7051 msg->sadb_msg_errno = code;
7052 return key_sendup_mbuf(so, m, KEY_SENDUP_ONE);
7056 * set the pointer to each header into message buffer.
7057 * m will be freed on error.
7058 * XXX larger-than-MCLBYTES extension?
7063 struct sadb_msghdr *mhp;
7066 struct sadb_ext *ext;
7072 if (m == NULL || mhp == NULL)
7073 panic("key_align: NULL pointer is passed.\n");
7074 if (m->m_len < sizeof(struct sadb_msg))
7075 panic("invalid mbuf passed to key_align");
7078 bzero(mhp, sizeof(*mhp));
7080 mhp->msg = mtod(m, struct sadb_msg *);
7081 mhp->ext[0] = (struct sadb_ext *)mhp->msg; /*XXX backward compat */
7083 end = PFKEY_UNUNIT64(mhp->msg->sadb_msg_len);
7084 extlen = end; /*just in case extlen is not updated*/
7085 for (off = sizeof(struct sadb_msg); off < end; off += extlen) {
7086 n = m_pulldown(m, off, sizeof(struct sadb_ext), &toff);
7088 /* m is already freed */
7091 ext = (struct sadb_ext *)(mtod(n, caddr_t) + toff);
7094 switch (ext->sadb_ext_type) {
7096 case SADB_EXT_ADDRESS_SRC:
7097 case SADB_EXT_ADDRESS_DST:
7098 case SADB_EXT_ADDRESS_PROXY:
7099 case SADB_EXT_LIFETIME_CURRENT:
7100 case SADB_EXT_LIFETIME_HARD:
7101 case SADB_EXT_LIFETIME_SOFT:
7102 case SADB_EXT_KEY_AUTH:
7103 case SADB_EXT_KEY_ENCRYPT:
7104 case SADB_EXT_IDENTITY_SRC:
7105 case SADB_EXT_IDENTITY_DST:
7106 case SADB_EXT_SENSITIVITY:
7107 case SADB_EXT_PROPOSAL:
7108 case SADB_EXT_SUPPORTED_AUTH:
7109 case SADB_EXT_SUPPORTED_ENCRYPT:
7110 case SADB_EXT_SPIRANGE:
7111 case SADB_X_EXT_POLICY:
7112 case SADB_X_EXT_SA2:
7113 /* duplicate check */
7115 * XXX Are there duplication payloads of either
7116 * KEY_AUTH or KEY_ENCRYPT ?
7118 if (mhp->ext[ext->sadb_ext_type] != NULL) {
7119 ipseclog((LOG_DEBUG,
7120 "key_align: duplicate ext_type %u "
7121 "is passed.\n", ext->sadb_ext_type));
7123 pfkeystat.out_dupext++;
7128 ipseclog((LOG_DEBUG,
7129 "key_align: invalid ext_type %u is passed.\n",
7130 ext->sadb_ext_type));
7132 pfkeystat.out_invexttype++;
7136 extlen = PFKEY_UNUNIT64(ext->sadb_ext_len);
7138 if (key_validate_ext(ext, extlen)) {
7140 pfkeystat.out_invlen++;
7144 n = m_pulldown(m, off, extlen, &toff);
7146 /* m is already freed */
7149 ext = (struct sadb_ext *)(mtod(n, caddr_t) + toff);
7151 mhp->ext[ext->sadb_ext_type] = ext;
7152 mhp->extoff[ext->sadb_ext_type] = off;
7153 mhp->extlen[ext->sadb_ext_type] = extlen;
7158 pfkeystat.out_invlen++;
7166 key_validate_ext(ext, len)
7167 const struct sadb_ext *ext;
7170 struct sockaddr *sa;
7171 enum { NONE, ADDR } checktype = NONE;
7173 const int sal = offsetof(struct sockaddr, sa_len) + sizeof(sa->sa_len);
7175 if (len != PFKEY_UNUNIT64(ext->sadb_ext_len))
7178 /* if it does not match minimum/maximum length, bail */
7179 if (ext->sadb_ext_type >= sizeof(minsize) / sizeof(minsize[0]) ||
7180 ext->sadb_ext_type >= sizeof(maxsize) / sizeof(maxsize[0]))
7182 if (!minsize[ext->sadb_ext_type] || len < minsize[ext->sadb_ext_type])
7184 if (maxsize[ext->sadb_ext_type] && len > maxsize[ext->sadb_ext_type])
7187 /* more checks based on sadb_ext_type XXX need more */
7188 switch (ext->sadb_ext_type) {
7189 case SADB_EXT_ADDRESS_SRC:
7190 case SADB_EXT_ADDRESS_DST:
7191 case SADB_EXT_ADDRESS_PROXY:
7192 baselen = PFKEY_ALIGN8(sizeof(struct sadb_address));
7195 case SADB_EXT_IDENTITY_SRC:
7196 case SADB_EXT_IDENTITY_DST:
7197 if (((struct sadb_ident *)ext)->sadb_ident_type ==
7198 SADB_X_IDENTTYPE_ADDR) {
7199 baselen = PFKEY_ALIGN8(sizeof(struct sadb_ident));
7209 switch (checktype) {
7213 sa = (struct sockaddr *)((caddr_t)ext + baselen);
7214 if (len < baselen + sal)
7216 if (baselen + PFKEY_ALIGN8(sa->sa_len) != len)
7229 bzero((caddr_t)&key_cb, sizeof(key_cb));
7231 for (i = 0; i < IPSEC_DIR_MAX; i++) {
7232 LIST_INIT(&sptree[i]);
7235 LIST_INIT(&sahtree);
7237 for (i = 0; i <= SADB_SATYPE_MAX; i++) {
7238 LIST_INIT(®tree[i]);
7241 #ifndef IPSEC_NONBLOCK_ACQUIRE
7242 LIST_INIT(&acqtree);
7244 LIST_INIT(&spacqtree);
7246 /* system default */
7248 ip4_def_policy.policy = IPSEC_POLICY_NONE;
7249 ip4_def_policy.refcnt++; /*never reclaim this*/
7252 ip6_def_policy.policy = IPSEC_POLICY_NONE;
7253 ip6_def_policy.refcnt++; /*never reclaim this*/
7256 #ifndef IPSEC_DEBUG2
7257 timeout((void *)key_timehandler, (void *)0, hz);
7258 #endif /*IPSEC_DEBUG2*/
7260 /* initialize key statistics */
7261 keystat.getspi_count = 1;
7263 printf("IPsec: Initialized Security Association Processing.\n");
7269 * XXX: maybe This function is called after INBOUND IPsec processing.
7271 * Special check for tunnel-mode packets.
7272 * We must make some checks for consistency between inner and outer IP header.
7274 * xxx more checks to be provided
7277 key_checktunnelsanity(sav, family, src, dst)
7278 struct secasvar *sav;
7284 if (sav->sah == NULL)
7285 panic("sav->sah == NULL at key_checktunnelsanity");
7287 /* XXX: check inner IP header */
7293 #define hostnamelen strlen(hostname)
7296 * Get FQDN for the host.
7297 * If the administrator configured hostname (by hostname(1)) without
7298 * domain name, returns nothing.
7305 static char fqdn[MAXHOSTNAMELEN + 1];
7310 /* check if it comes with domain name. */
7312 for (i = 0; i < hostnamelen; i++) {
7313 if (hostname[i] == '.')
7319 /* NOTE: hostname may not be NUL-terminated. */
7320 bzero(fqdn, sizeof(fqdn));
7321 bcopy(hostname, fqdn, hostnamelen);
7322 fqdn[hostnamelen] = '\0';
7327 * get username@FQDN for the host/user.
7333 static char userfqdn[MAXHOSTNAMELEN + MAXLOGNAME + 2];
7334 struct proc *p = curproc;
7337 if (!p || !p->p_pgrp || !p->p_pgrp->pg_session)
7339 if (!(host = key_getfqdn()))
7342 /* NOTE: s_login may not be-NUL terminated. */
7343 bzero(userfqdn, sizeof(userfqdn));
7344 bcopy(p->p_pgrp->pg_session->s_login, userfqdn, MAXLOGNAME);
7345 userfqdn[MAXLOGNAME] = '\0'; /* safeguard */
7346 q = userfqdn + strlen(userfqdn);
7348 bcopy(host, q, strlen(host));
7356 /* record data transfer on SA, and update timestamps */
7358 key_sa_recordxfer(sav, m)
7359 struct secasvar *sav;
7363 panic("key_sa_recordxfer called with sav == NULL");
7365 panic("key_sa_recordxfer called with m == NULL");
7370 * XXX Currently, there is a difference of bytes size
7371 * between inbound and outbound processing.
7373 sav->lft_c->sadb_lifetime_bytes += m->m_pkthdr.len;
7374 /* to check bytes lifetime is done in key_timehandler(). */
7377 * We use the number of packets as the unit of
7378 * sadb_lifetime_allocations. We increment the variable
7379 * whenever {esp,ah}_{in,out}put is called.
7381 sav->lft_c->sadb_lifetime_allocations++;
7382 /* XXX check for expires? */
7385 * NOTE: We record CURRENT sadb_lifetime_usetime by using wall clock,
7386 * in seconds. HARD and SOFT lifetime are measured by the time
7387 * difference (again in seconds) from sadb_lifetime_usetime.
7391 * -----+-----+--------+---> t
7392 * <--------------> HARD
7398 sav->lft_c->sadb_lifetime_usetime = tv.tv_sec;
7399 /* XXX check for expires? */
7407 key_sa_routechange(dst)
7408 struct sockaddr *dst;
7410 struct secashead *sah;
7413 LIST_FOREACH(sah, &sahtree, chain) {
7414 ro = &sah->sa_route;
7415 if (ro->ro_rt && dst->sa_len == ro->ro_dst.sa_len
7416 && bcmp(dst, &ro->ro_dst, dst->sa_len) == 0) {
7418 ro->ro_rt = (struct rtentry *)NULL;
7426 key_sa_chgstate(sav, state)
7427 struct secasvar *sav;
7431 panic("key_sa_chgstate called with sav == NULL");
7433 if (sav->state == state)
7436 if (__LIST_CHAINED(sav))
7437 LIST_REMOVE(sav, chain);
7440 LIST_INSERT_HEAD(&sav->sah->savtree[state], sav, chain);
7445 struct secasvar *sav;
7449 panic("key_sa_stir_iv called with sav == NULL");
7450 key_randomfill(sav->iv, sav->ivlen);
7454 static struct mbuf *
7458 struct mbuf *m = NULL, *n;
7463 MGET(n, M_DONTWAIT, MT_DATA);
7464 if (n && len > MLEN)
7465 MCLGET(n, M_DONTWAIT);
7473 n->m_len = M_TRAILINGSPACE(n);
7474 /* use the bottom of mbuf, hoping we can prepend afterwards */
7475 if (n->m_len > len) {
7476 t = (n->m_len - len) & ~(sizeof(long) - 1);