1 /* $FreeBSD: src/sys/netipsec/key.c,v 1.3.2.1 2003/01/24 05:11:35 sam Exp $ */
2 /* $DragonFly: src/sys/netproto/ipsec/key.c,v 1.16 2006/09/05 00:55:49 dillon Exp $ */
3 /* $KAME: key.c,v 1.191 2001/06/27 10:46:49 sakane Exp $ */
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * This code is referd to RFC 2367
39 #include "opt_inet6.h"
40 #include "opt_ipsec.h"
42 #include <sys/types.h>
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
47 #include <sys/domain.h>
48 #include <sys/protosw.h>
49 #include <sys/malloc.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/sysctl.h>
53 #include <sys/errno.h>
55 #include <sys/queue.h>
56 #include <sys/syslog.h>
59 #include <net/route.h>
60 #include <net/raw_cb.h>
62 #include <netinet/in.h>
63 #include <netinet/in_systm.h>
64 #include <netinet/ip.h>
65 #include <netinet/in_var.h>
68 #include <netinet/ip6.h>
69 #include <netinet6/in6_var.h>
70 #include <netinet6/ip6_var.h>
74 #include <netinet/in_pcb.h>
77 #include <netinet6/in6_pcb.h>
80 #include <net/pfkeyv2.h>
81 #include <netproto/ipsec/keydb.h>
82 #include <netproto/ipsec/key.h>
83 #include <netproto/ipsec/keysock.h>
84 #include <netproto/ipsec/key_debug.h>
86 #include <netproto/ipsec/ipsec.h>
88 #include <netproto/ipsec/ipsec6.h>
91 #include <netproto/ipsec/xform.h>
93 #include <machine/stdarg.h>
96 #include <sys/random.h>
98 #include <net/net_osdep.h>
100 #define FULLMASK 0xff
101 #define _BITS(bytes) ((bytes) << 3)
104 * Note on SA reference counting:
105 * - SAs that are not in DEAD state will have (total external reference + 1)
106 * following value in reference count field. they cannot be freed and are
107 * referenced from SA header.
108 * - SAs that are in DEAD state will have (total external reference)
109 * in reference count field. they are ready to be freed. reference from
110 * SA header will be removed in key_delsav(), when the reference count
111 * field hits 0 (= no external reference other than from SA header.
115 static struct callout key_timehandler_ch;
117 u_int32_t key_debug_level = 0;
118 static u_int key_spi_trycnt = 1000;
119 static u_int32_t key_spi_minval = 0x100;
120 static u_int32_t key_spi_maxval = 0x0fffffff; /* XXX */
121 static u_int32_t policy_id = 0;
122 static u_int key_int_random = 60; /*interval to initialize randseed,1(m)*/
123 static u_int key_larval_lifetime = 30; /* interval to expire acquiring, 30(s)*/
124 static int key_blockacq_count = 10; /* counter for blocking SADB_ACQUIRE.*/
125 static int key_blockacq_lifetime = 20; /* lifetime for blocking SADB_ACQUIRE.*/
126 static int key_prefered_oldsa = 1; /* prefered old sa rather than new sa.*/
128 static u_int32_t acq_seq = 0;
129 static int key_tick_init_random = 0;
131 static LIST_HEAD(_sptree, secpolicy) sptree[IPSEC_DIR_MAX]; /* SPD */
132 static LIST_HEAD(_sahtree, secashead) sahtree; /* SAD */
133 static LIST_HEAD(_regtree, secreg) regtree[SADB_SATYPE_MAX + 1];
135 #ifndef IPSEC_NONBLOCK_ACQUIRE
136 static LIST_HEAD(_acqtree, secacq) acqtree; /* acquiring list */
138 static LIST_HEAD(_spacqtree, secspacq) spacqtree; /* SP acquiring list */
140 /* search order for SAs */
141 static u_int saorder_state_valid[] = {
142 SADB_SASTATE_DYING, SADB_SASTATE_MATURE,
144 * This order is important because we must select the oldest SA
145 * for outbound processing. For inbound, This is not important.
148 static u_int saorder_state_alive[] = {
150 SADB_SASTATE_MATURE, SADB_SASTATE_DYING, SADB_SASTATE_LARVAL
152 static u_int saorder_state_any[] = {
153 SADB_SASTATE_MATURE, SADB_SASTATE_DYING,
154 SADB_SASTATE_LARVAL, SADB_SASTATE_DEAD
157 static const int minsize[] = {
158 sizeof(struct sadb_msg), /* SADB_EXT_RESERVED */
159 sizeof(struct sadb_sa), /* SADB_EXT_SA */
160 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_CURRENT */
161 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_HARD */
162 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_SOFT */
163 sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_SRC */
164 sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_DST */
165 sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_PROXY */
166 sizeof(struct sadb_key), /* SADB_EXT_KEY_AUTH */
167 sizeof(struct sadb_key), /* SADB_EXT_KEY_ENCRYPT */
168 sizeof(struct sadb_ident), /* SADB_EXT_IDENTITY_SRC */
169 sizeof(struct sadb_ident), /* SADB_EXT_IDENTITY_DST */
170 sizeof(struct sadb_sens), /* SADB_EXT_SENSITIVITY */
171 sizeof(struct sadb_prop), /* SADB_EXT_PROPOSAL */
172 sizeof(struct sadb_supported), /* SADB_EXT_SUPPORTED_AUTH */
173 sizeof(struct sadb_supported), /* SADB_EXT_SUPPORTED_ENCRYPT */
174 sizeof(struct sadb_spirange), /* SADB_EXT_SPIRANGE */
175 0, /* SADB_X_EXT_KMPRIVATE */
176 sizeof(struct sadb_x_policy), /* SADB_X_EXT_POLICY */
177 sizeof(struct sadb_x_sa2), /* SADB_X_SA2 */
179 static const int maxsize[] = {
180 sizeof(struct sadb_msg), /* SADB_EXT_RESERVED */
181 sizeof(struct sadb_sa), /* SADB_EXT_SA */
182 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_CURRENT */
183 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_HARD */
184 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_SOFT */
185 0, /* SADB_EXT_ADDRESS_SRC */
186 0, /* SADB_EXT_ADDRESS_DST */
187 0, /* SADB_EXT_ADDRESS_PROXY */
188 0, /* SADB_EXT_KEY_AUTH */
189 0, /* SADB_EXT_KEY_ENCRYPT */
190 0, /* SADB_EXT_IDENTITY_SRC */
191 0, /* SADB_EXT_IDENTITY_DST */
192 0, /* SADB_EXT_SENSITIVITY */
193 0, /* SADB_EXT_PROPOSAL */
194 0, /* SADB_EXT_SUPPORTED_AUTH */
195 0, /* SADB_EXT_SUPPORTED_ENCRYPT */
196 sizeof(struct sadb_spirange), /* SADB_EXT_SPIRANGE */
197 0, /* SADB_X_EXT_KMPRIVATE */
198 0, /* SADB_X_EXT_POLICY */
199 sizeof(struct sadb_x_sa2), /* SADB_X_SA2 */
202 static int ipsec_esp_keymin = 256;
203 static int ipsec_esp_auth = 0;
204 static int ipsec_ah_keymin = 128;
207 SYSCTL_DECL(_net_key);
210 SYSCTL_INT(_net_key, KEYCTL_DEBUG_LEVEL, debug, CTLFLAG_RW, \
211 &key_debug_level, 0, "");
213 /* max count of trial for the decision of spi value */
214 SYSCTL_INT(_net_key, KEYCTL_SPI_TRY, spi_trycnt, CTLFLAG_RW, \
215 &key_spi_trycnt, 0, "");
217 /* minimum spi value to allocate automatically. */
218 SYSCTL_INT(_net_key, KEYCTL_SPI_MIN_VALUE, spi_minval, CTLFLAG_RW, \
219 &key_spi_minval, 0, "");
221 /* maximun spi value to allocate automatically. */
222 SYSCTL_INT(_net_key, KEYCTL_SPI_MAX_VALUE, spi_maxval, CTLFLAG_RW, \
223 &key_spi_maxval, 0, "");
225 /* interval to initialize randseed */
226 SYSCTL_INT(_net_key, KEYCTL_RANDOM_INT, int_random, CTLFLAG_RW, \
227 &key_int_random, 0, "");
229 /* lifetime for larval SA */
230 SYSCTL_INT(_net_key, KEYCTL_LARVAL_LIFETIME, larval_lifetime, CTLFLAG_RW, \
231 &key_larval_lifetime, 0, "");
233 /* counter for blocking to send SADB_ACQUIRE to IKEd */
234 SYSCTL_INT(_net_key, KEYCTL_BLOCKACQ_COUNT, blockacq_count, CTLFLAG_RW, \
235 &key_blockacq_count, 0, "");
237 /* lifetime for blocking to send SADB_ACQUIRE to IKEd */
238 SYSCTL_INT(_net_key, KEYCTL_BLOCKACQ_LIFETIME, blockacq_lifetime, CTLFLAG_RW, \
239 &key_blockacq_lifetime, 0, "");
242 SYSCTL_INT(_net_key, KEYCTL_ESP_AUTH, esp_auth, CTLFLAG_RW, \
243 &ipsec_esp_auth, 0, "");
245 /* minimum ESP key length */
246 SYSCTL_INT(_net_key, KEYCTL_ESP_KEYMIN, esp_keymin, CTLFLAG_RW, \
247 &ipsec_esp_keymin, 0, "");
249 /* minimum AH key length */
250 SYSCTL_INT(_net_key, KEYCTL_AH_KEYMIN, ah_keymin, CTLFLAG_RW, \
251 &ipsec_ah_keymin, 0, "");
253 /* perfered old SA rather than new SA */
254 SYSCTL_INT(_net_key, KEYCTL_PREFERED_OLDSA, prefered_oldsa, CTLFLAG_RW,\
255 &key_prefered_oldsa, 0, "");
258 #define LIST_FOREACH(elm, head, field) \
259 for (elm = LIST_FIRST(head); elm; elm = LIST_NEXT(elm, field))
261 #define __LIST_CHAINED(elm) \
262 (!((elm)->chain.le_next == NULL && (elm)->chain.le_prev == NULL))
263 #define LIST_INSERT_TAIL(head, elm, type, field) \
265 struct type *curelm = LIST_FIRST(head); \
266 if (curelm == NULL) {\
267 LIST_INSERT_HEAD(head, elm, field); \
269 while (LIST_NEXT(curelm, field)) \
270 curelm = LIST_NEXT(curelm, field);\
271 LIST_INSERT_AFTER(curelm, elm, field);\
275 #define KEY_CHKSASTATE(head, sav, name) \
277 if ((head) != (sav)) { \
278 ipseclog((LOG_DEBUG, "%s: state mismatched (TREE=%d SA=%d)\n", \
279 (name), (head), (sav))); \
284 #define KEY_CHKSPDIR(head, sp, name) \
286 if ((head) != (sp)) { \
287 ipseclog((LOG_DEBUG, "%s: direction mismatched (TREE=%d SP=%d), " \
288 "anyway continue.\n", \
289 (name), (head), (sp))); \
293 MALLOC_DEFINE(M_SECA, "key mgmt", "security associations, key management");
296 #define KMALLOC(p, t, n) \
297 ((p) = (t) kmalloc((unsigned long)(n), M_SECA, M_INTWAIT | M_NULLOK))
299 kfree((caddr_t)(p), M_SECA)
301 #define KMALLOC(p, t, n) \
303 ((p) = (t)kmalloc((unsigned long)(n), M_SECA, M_INTWAIT | M_NULLOK)); \
304 printf("%s %d: %p <- KMALLOC(%s, %d)\n", \
305 __FILE__, __LINE__, (p), #t, n); \
310 printf("%s %d: %p -> KFREE()\n", __FILE__, __LINE__, (p)); \
311 kfree((caddr_t)(p), M_SECA); \
316 * set parameters into secpolicyindex buffer.
317 * Must allocate secpolicyindex buffer passed to this function.
319 #define KEY_SETSECSPIDX(_dir, s, d, ps, pd, ulp, idx) \
321 bzero((idx), sizeof(struct secpolicyindex)); \
322 (idx)->dir = (_dir); \
323 (idx)->prefs = (ps); \
324 (idx)->prefd = (pd); \
325 (idx)->ul_proto = (ulp); \
326 bcopy((s), &(idx)->src, ((const struct sockaddr *)(s))->sa_len); \
327 bcopy((d), &(idx)->dst, ((const struct sockaddr *)(d))->sa_len); \
331 * set parameters into secasindex buffer.
332 * Must allocate secasindex buffer before calling this function.
334 #define KEY_SETSECASIDX(p, m, r, s, d, idx) \
336 bzero((idx), sizeof(struct secasindex)); \
337 (idx)->proto = (p); \
339 (idx)->reqid = (r); \
340 bcopy((s), &(idx)->src, ((const struct sockaddr *)(s))->sa_len); \
341 bcopy((d), &(idx)->dst, ((const struct sockaddr *)(d))->sa_len); \
346 u_long getspi_count; /* the avarage of count to try to get new SPI */
350 struct sadb_msg *msg;
351 struct sadb_ext *ext[SADB_EXT_MAX + 1];
352 int extoff[SADB_EXT_MAX + 1];
353 int extlen[SADB_EXT_MAX + 1];
356 static struct secasvar *key_allocsa_policy (const struct secasindex *);
357 static void key_freesp_so (struct secpolicy **);
358 static struct secasvar *key_do_allocsa_policy (struct secashead *, u_int);
359 static void key_delsp (struct secpolicy *);
360 static struct secpolicy *key_getsp (struct secpolicyindex *);
361 static struct secpolicy *key_getspbyid (u_int32_t);
362 static u_int32_t key_newreqid (void);
363 static struct mbuf *key_gather_mbuf (struct mbuf *,
364 const struct sadb_msghdr *, int, int, ...);
365 static int key_spdadd (struct socket *, struct mbuf *,
366 const struct sadb_msghdr *);
367 static u_int32_t key_getnewspid (void);
368 static int key_spddelete (struct socket *, struct mbuf *,
369 const struct sadb_msghdr *);
370 static int key_spddelete2 (struct socket *, struct mbuf *,
371 const struct sadb_msghdr *);
372 static int key_spdget (struct socket *, struct mbuf *,
373 const struct sadb_msghdr *);
374 static int key_spdflush (struct socket *, struct mbuf *,
375 const struct sadb_msghdr *);
376 static int key_spddump (struct socket *, struct mbuf *,
377 const struct sadb_msghdr *);
378 static struct mbuf *key_setdumpsp (struct secpolicy *,
379 u_int8_t, u_int32_t, u_int32_t);
380 static u_int key_getspreqmsglen (struct secpolicy *);
381 static int key_spdexpire (struct secpolicy *);
382 static struct secashead *key_newsah (struct secasindex *);
383 static void key_delsah (struct secashead *);
384 static struct secasvar *key_newsav (struct mbuf *,
385 const struct sadb_msghdr *, struct secashead *, int *,
387 #define KEY_NEWSAV(m, sadb, sah, e) \
388 key_newsav(m, sadb, sah, e, __FILE__, __LINE__)
389 static void key_delsav (struct secasvar *);
390 static struct secashead *key_getsah (struct secasindex *);
391 static struct secasvar *key_checkspidup (struct secasindex *, u_int32_t);
392 static struct secasvar *key_getsavbyspi (struct secashead *, u_int32_t);
393 static int key_setsaval (struct secasvar *, struct mbuf *,
394 const struct sadb_msghdr *);
395 static int key_mature (struct secasvar *);
396 static struct mbuf *key_setdumpsa (struct secasvar *, u_int8_t,
397 u_int8_t, u_int32_t, u_int32_t);
398 static struct mbuf *key_setsadbmsg (u_int8_t, u_int16_t, u_int8_t,
399 u_int32_t, pid_t, u_int16_t);
400 static struct mbuf *key_setsadbsa (struct secasvar *);
401 static struct mbuf *key_setsadbaddr (u_int16_t,
402 const struct sockaddr *, u_int8_t, u_int16_t);
404 static struct mbuf *key_setsadbident (u_int16_t, u_int16_t, caddr_t,
407 static struct mbuf *key_setsadbxsa2 (u_int8_t, u_int32_t, u_int32_t);
408 static struct mbuf *key_setsadbxpolicy (u_int16_t, u_int8_t,
410 static void *key_newbuf (const void *, u_int);
412 static int key_ismyaddr6 (struct sockaddr_in6 *);
415 /* flags for key_cmpsaidx() */
416 #define CMP_HEAD 1 /* protocol, addresses. */
417 #define CMP_MODE_REQID 2 /* additionally HEAD, reqid, mode. */
418 #define CMP_REQID 3 /* additionally HEAD, reaid. */
419 #define CMP_EXACTLY 4 /* all elements. */
420 static int key_cmpsaidx
421 (const struct secasindex *, const struct secasindex *, int);
423 static int key_cmpspidx_exactly
424 (struct secpolicyindex *, struct secpolicyindex *);
425 static int key_cmpspidx_withmask
426 (struct secpolicyindex *, struct secpolicyindex *);
427 static int key_sockaddrcmp (const struct sockaddr *, const struct sockaddr *, int);
428 static int key_bbcmp (const void *, const void *, u_int);
429 static void key_srandom (void);
430 static u_int16_t key_satype2proto (u_int8_t);
431 static u_int8_t key_proto2satype (u_int16_t);
433 static int key_getspi (struct socket *, struct mbuf *,
434 const struct sadb_msghdr *);
435 static u_int32_t key_do_getnewspi (struct sadb_spirange *,
436 struct secasindex *);
437 static int key_update (struct socket *, struct mbuf *,
438 const struct sadb_msghdr *);
439 #ifdef IPSEC_DOSEQCHECK
440 static struct secasvar *key_getsavbyseq (struct secashead *, u_int32_t);
442 static int key_add (struct socket *, struct mbuf *,
443 const struct sadb_msghdr *);
444 static int key_setident (struct secashead *, struct mbuf *,
445 const struct sadb_msghdr *);
446 static struct mbuf *key_getmsgbuf_x1 (struct mbuf *,
447 const struct sadb_msghdr *);
448 static int key_delete (struct socket *, struct mbuf *,
449 const struct sadb_msghdr *);
450 static int key_get (struct socket *, struct mbuf *,
451 const struct sadb_msghdr *);
453 static void key_getcomb_setlifetime (struct sadb_comb *);
454 static struct mbuf *key_getcomb_esp (void);
455 static struct mbuf *key_getcomb_ah (void);
456 static struct mbuf *key_getcomb_ipcomp (void);
457 static struct mbuf *key_getprop (const struct secasindex *);
459 static int key_acquire (const struct secasindex *, struct secpolicy *);
460 #ifndef IPSEC_NONBLOCK_ACQUIRE
461 static struct secacq *key_newacq (const struct secasindex *);
462 static struct secacq *key_getacq (const struct secasindex *);
463 static struct secacq *key_getacqbyseq (u_int32_t);
465 static struct secspacq *key_newspacq (struct secpolicyindex *);
466 static struct secspacq *key_getspacq (struct secpolicyindex *);
467 static int key_acquire2 (struct socket *, struct mbuf *,
468 const struct sadb_msghdr *);
469 static int key_register (struct socket *, struct mbuf *,
470 const struct sadb_msghdr *);
471 static int key_expire (struct secasvar *);
472 static int key_flush (struct socket *, struct mbuf *,
473 const struct sadb_msghdr *);
474 static int key_dump (struct socket *, struct mbuf *,
475 const struct sadb_msghdr *);
476 static int key_promisc (struct socket *, struct mbuf *,
477 const struct sadb_msghdr *);
478 static int key_senderror (struct socket *, struct mbuf *, int);
479 static int key_validate_ext (const struct sadb_ext *, int);
480 static int key_align (struct mbuf *, struct sadb_msghdr *);
482 static const char *key_getfqdn (void);
483 static const char *key_getuserfqdn (void);
485 static void key_sa_chgstate (struct secasvar *, u_int8_t);
486 static struct mbuf *key_alloc_mbuf (int);
488 #define SA_ADDREF(p) do { \
490 KASSERT((p)->refcnt != 0, \
491 ("SA refcnt overflow at %s:%u", __FILE__, __LINE__)); \
493 #define SA_DELREF(p) do { \
494 KASSERT((p)->refcnt > 0, \
495 ("SA refcnt underflow at %s:%u", __FILE__, __LINE__)); \
499 #define SP_ADDREF(p) do { \
501 KASSERT((p)->refcnt != 0, \
502 ("SP refcnt overflow at %s:%u", __FILE__, __LINE__)); \
504 #define SP_DELREF(p) do { \
505 KASSERT((p)->refcnt > 0, \
506 ("SP refcnt underflow at %s:%u", __FILE__, __LINE__)); \
511 * Return 0 when there are known to be no SP's for the specified
512 * direction. Otherwise return 1. This is used by IPsec code
513 * to optimize performance.
516 key_havesp(u_int dir)
518 return (dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND ?
519 LIST_FIRST(&sptree[dir]) != NULL : 1);
522 /* %%% IPsec policy management */
524 * allocating a SP for OUTBOUND or INBOUND packet.
525 * Must call key_freesp() later.
526 * OUT: NULL: not found
527 * others: found and return the pointer.
530 key_allocsp(struct secpolicyindex *spidx, u_int dir, const char* where, int tag)
532 struct secpolicy *sp;
535 KASSERT(spidx != NULL, ("key_allocsp: null spidx"));
536 KASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
537 ("key_allocsp: invalid direction %u", dir));
539 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
540 printf("DP key_allocsp from %s:%u\n", where, tag));
544 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
545 printf("*** objects\n");
546 kdebug_secpolicyindex(spidx));
548 LIST_FOREACH(sp, &sptree[dir], chain) {
549 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
550 printf("*** in SPD\n");
551 kdebug_secpolicyindex(&sp->spidx));
553 if (sp->state == IPSEC_SPSTATE_DEAD)
555 if (key_cmpspidx_withmask(&sp->spidx, spidx))
562 KEY_CHKSPDIR(sp->spidx.dir, dir, "key_allocsp");
564 /* found a SPD entry */
565 sp->lastused = time_second;
570 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
571 printf("DP key_allocsp return SP:%p (ID=%u) refcnt %u\n",
572 sp, sp ? sp->id : 0, sp ? sp->refcnt : 0));
577 * allocating a SP for OUTBOUND or INBOUND packet.
578 * Must call key_freesp() later.
579 * OUT: NULL: not found
580 * others: found and return the pointer.
583 key_allocsp2(u_int32_t spi,
584 union sockaddr_union *dst,
587 const char* where, int tag)
589 struct secpolicy *sp;
592 KASSERT(dst != NULL, ("key_allocsp2: null dst"));
593 KASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
594 ("key_allocsp2: invalid direction %u", dir));
596 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
597 printf("DP key_allocsp2 from %s:%u\n", where, tag));
601 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
602 printf("*** objects\n");
603 printf("spi %u proto %u dir %u\n", spi, proto, dir);
604 kdebug_sockaddr(&dst->sa));
606 LIST_FOREACH(sp, &sptree[dir], chain) {
607 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
608 printf("*** in SPD\n");
609 kdebug_secpolicyindex(&sp->spidx));
611 if (sp->state == IPSEC_SPSTATE_DEAD)
613 /* compare simple values, then dst address */
614 if (sp->spidx.ul_proto != proto)
616 /* NB: spi's must exist and match */
617 if (!sp->req || !sp->req->sav || sp->req->sav->spi != spi)
619 if (key_sockaddrcmp(&sp->spidx.dst.sa, &dst->sa, 1) == 0)
626 KEY_CHKSPDIR(sp->spidx.dir, dir, "key_allocsp2");
628 /* found a SPD entry */
629 sp->lastused = time_second;
634 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
635 printf("DP key_allocsp2 return SP:%p (ID=%u) refcnt %u\n",
636 sp, sp ? sp->id : 0, sp ? sp->refcnt : 0));
641 * return a policy that matches this particular inbound packet.
645 key_gettunnel(const struct sockaddr *osrc,
646 const struct sockaddr *odst,
647 const struct sockaddr *isrc,
648 const struct sockaddr *idst,
649 const char* where, int tag)
651 struct secpolicy *sp;
652 const int dir = IPSEC_DIR_INBOUND;
654 struct ipsecrequest *r1, *r2, *p;
655 struct secpolicyindex spidx;
657 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
658 printf("DP key_gettunnel from %s:%u\n", where, tag));
660 if (isrc->sa_family != idst->sa_family) {
661 ipseclog((LOG_ERR, "protocol family mismatched %d != %d\n.",
662 isrc->sa_family, idst->sa_family));
668 LIST_FOREACH(sp, &sptree[dir], chain) {
669 if (sp->state == IPSEC_SPSTATE_DEAD)
673 for (p = sp->req; p; p = p->next) {
674 if (p->saidx.mode != IPSEC_MODE_TUNNEL)
681 /* here we look at address matches only */
683 if (isrc->sa_len > sizeof(spidx.src) ||
684 idst->sa_len > sizeof(spidx.dst))
686 bcopy(isrc, &spidx.src, isrc->sa_len);
687 bcopy(idst, &spidx.dst, idst->sa_len);
688 if (!key_cmpspidx_withmask(&sp->spidx, &spidx))
691 if (key_sockaddrcmp(&r1->saidx.src.sa, isrc, 0) ||
692 key_sockaddrcmp(&r1->saidx.dst.sa, idst, 0))
696 if (key_sockaddrcmp(&r2->saidx.src.sa, osrc, 0) ||
697 key_sockaddrcmp(&r2->saidx.dst.sa, odst, 0))
706 sp->lastused = time_second;
711 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
712 printf("DP key_gettunnel return SP:%p (ID=%u) refcnt %u\n",
713 sp, sp ? sp->id : 0, sp ? sp->refcnt : 0));
718 * allocating an SA entry for an *OUTBOUND* packet.
719 * checking each request entries in SP, and acquire an SA if need.
720 * OUT: 0: there are valid requests.
721 * ENOENT: policy may be valid, but SA with REQUIRE is on acquiring.
724 key_checkrequest(struct ipsecrequest *isr, const struct secasindex *saidx)
729 KASSERT(isr != NULL, ("key_checkrequest: null isr"));
730 KASSERT(saidx != NULL, ("key_checkrequest: null saidx"));
731 KASSERT(saidx->mode == IPSEC_MODE_TRANSPORT ||
732 saidx->mode == IPSEC_MODE_TUNNEL,
733 ("key_checkrequest: unexpected policy %u", saidx->mode));
735 /* get current level */
736 level = ipsec_get_reqlevel(isr);
739 * XXX guard against protocol callbacks from the crypto
740 * thread as they reference ipsecrequest.sav which we
741 * temporarily null out below. Need to rethink how we
742 * handle bundled SA's in the callback thread.
746 * We do allocate new SA only if the state of SA in the holder is
747 * SADB_SASTATE_DEAD. The SA for outbound must be the oldest.
749 if (isr->sav != NULL) {
750 if (isr->sav->sah == NULL)
751 panic("key_checkrequest: sah is null.\n");
752 if (isr->sav == (struct secasvar *)LIST_FIRST(
753 &isr->sav->sah->savtree[SADB_SASTATE_DEAD])) {
754 KEY_FREESAV(&isr->sav);
760 * we free any SA stashed in the IPsec request because a different
761 * SA may be involved each time this request is checked, either
762 * because new SAs are being configured, or this request is
763 * associated with an unconnected datagram socket, or this request
764 * is associated with a system default policy.
766 * The operation may have negative impact to performance. We may
767 * want to check cached SA carefully, rather than picking new SA
770 if (isr->sav != NULL) {
771 KEY_FREESAV(&isr->sav);
777 * new SA allocation if no SA found.
778 * key_allocsa_policy should allocate the oldest SA available.
779 * See key_do_allocsa_policy(), and draft-jenkins-ipsec-rekeying-03.txt.
781 if (isr->sav == NULL)
782 isr->sav = key_allocsa_policy(saidx);
784 /* When there is SA. */
785 if (isr->sav != NULL) {
786 if (isr->sav->state != SADB_SASTATE_MATURE &&
787 isr->sav->state != SADB_SASTATE_DYING)
793 error = key_acquire(saidx, isr->sp);
795 /* XXX What should I do ? */
796 ipseclog((LOG_DEBUG, "key_checkrequest: error %d returned "
797 "from key_acquire.\n", error));
801 if (level != IPSEC_LEVEL_REQUIRE) {
802 /* XXX sigh, the interface to this routine is botched */
803 KASSERT(isr->sav == NULL, ("key_checkrequest: unexpected SA"));
811 * allocating a SA for policy entry from SAD.
812 * NOTE: searching SAD of aliving state.
813 * OUT: NULL: not found.
814 * others: found and return the pointer.
816 static struct secasvar *
817 key_allocsa_policy(const struct secasindex *saidx)
819 struct secashead *sah;
820 struct secasvar *sav;
821 u_int stateidx, state;
823 LIST_FOREACH(sah, &sahtree, chain) {
824 if (sah->state == SADB_SASTATE_DEAD)
826 if (key_cmpsaidx(&sah->saidx, saidx, CMP_MODE_REQID))
834 /* search valid state */
836 stateidx < _ARRAYLEN(saorder_state_valid);
839 state = saorder_state_valid[stateidx];
841 sav = key_do_allocsa_policy(sah, state);
850 * searching SAD with direction, protocol, mode and state.
851 * called by key_allocsa_policy().
854 * others : found, pointer to a SA.
856 static struct secasvar *
857 key_do_allocsa_policy(struct secashead *sah, u_int state)
859 struct secasvar *sav, *nextsav, *candidate, *d;
864 for (sav = LIST_FIRST(&sah->savtree[state]);
868 nextsav = LIST_NEXT(sav, chain);
871 KEY_CHKSASTATE(sav->state, state, "key_do_allocsa_policy");
874 if (candidate == NULL) {
879 /* Which SA is the better ? */
882 if (candidate->lft_c == NULL || sav->lft_c == NULL)
883 panic("key_do_allocsa_policy: "
884 "lifetime_current is NULL.\n");
886 /* What the best method is to compare ? */
887 if (key_prefered_oldsa) {
888 if (candidate->lft_c->sadb_lifetime_addtime >
889 sav->lft_c->sadb_lifetime_addtime) {
896 /* prefered new sa rather than old sa */
897 if (candidate->lft_c->sadb_lifetime_addtime <
898 sav->lft_c->sadb_lifetime_addtime) {
905 * prepared to delete the SA when there is more
906 * suitable candidate and the lifetime of the SA is not
909 if (d->lft_c->sadb_lifetime_addtime != 0) {
910 struct mbuf *m, *result;
912 key_sa_chgstate(d, SADB_SASTATE_DEAD);
914 KASSERT(d->refcnt > 0,
915 ("key_do_allocsa_policy: bogus ref count"));
916 m = key_setsadbmsg(SADB_DELETE, 0,
917 d->sah->saidx.proto, 0, 0, d->refcnt - 1);
922 /* set sadb_address for saidx's. */
923 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
924 &d->sah->saidx.src.sa,
925 d->sah->saidx.src.sa.sa_len << 3,
931 /* set sadb_address for saidx's. */
932 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
933 &d->sah->saidx.src.sa,
934 d->sah->saidx.src.sa.sa_len << 3,
940 /* create SA extension */
941 m = key_setsadbsa(d);
946 if (result->m_len < sizeof(struct sadb_msg)) {
947 result = m_pullup(result,
948 sizeof(struct sadb_msg));
953 result->m_pkthdr.len = 0;
954 for (m = result; m; m = m->m_next)
955 result->m_pkthdr.len += m->m_len;
956 mtod(result, struct sadb_msg *)->sadb_msg_len =
957 PFKEY_UNIT64(result->m_pkthdr.len);
959 if (key_sendup_mbuf(NULL, result,
960 KEY_SENDUP_REGISTERED))
968 SA_ADDREF(candidate);
969 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
970 printf("DP allocsa_policy cause "
971 "refcnt++:%d SA:%p\n",
972 candidate->refcnt, candidate));
978 * allocating a usable SA entry for a *INBOUND* packet.
979 * Must call key_freesav() later.
980 * OUT: positive: pointer to a usable sav (i.e. MATURE or DYING state).
981 * NULL: not found, or error occured.
983 * In the comparison, no source address is used--for RFC2401 conformance.
984 * To quote, from section 4.1:
985 * A security association is uniquely identified by a triple consisting
986 * of a Security Parameter Index (SPI), an IP Destination Address, and a
987 * security protocol (AH or ESP) identifier.
988 * Note that, however, we do need to keep source address in IPsec SA.
989 * IKE specification and PF_KEY specification do assume that we
990 * keep source address in IPsec SA. We see a tricky situation here.
994 union sockaddr_union *dst,
997 const char* where, int tag)
999 struct secashead *sah;
1000 struct secasvar *sav;
1001 u_int stateidx, state;
1004 KASSERT(dst != NULL, ("key_allocsa: null dst address"));
1006 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1007 printf("DP key_allocsa from %s:%u\n", where, tag));
1011 * XXX: to be checked internal IP header somewhere. Also when
1012 * IPsec tunnel packet is received. But ESP tunnel mode is
1013 * encrypted so we can't check internal IP header.
1016 LIST_FOREACH(sah, &sahtree, chain) {
1017 /* search valid state */
1019 stateidx < _ARRAYLEN(saorder_state_valid);
1021 state = saorder_state_valid[stateidx];
1022 LIST_FOREACH(sav, &sah->savtree[state], chain) {
1024 KEY_CHKSASTATE(sav->state, state, "key_allocsav");
1025 /* do not return entries w/ unusable state */
1026 if (sav->state != SADB_SASTATE_MATURE &&
1027 sav->state != SADB_SASTATE_DYING)
1029 if (proto != sav->sah->saidx.proto)
1031 if (spi != sav->spi)
1033 #if 0 /* don't check src */
1034 /* check src address */
1035 if (key_sockaddrcmp(&src->sa, &sav->sah->saidx.src.sa, 0) != 0)
1038 /* check dst address */
1039 if (key_sockaddrcmp(&dst->sa, &sav->sah->saidx.dst.sa, 0) != 0)
1050 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1051 printf("DP key_allocsa return SA:%p; refcnt %u\n",
1052 sav, sav ? sav->refcnt : 0));
1057 * Must be called after calling key_allocsp().
1058 * For both the packet without socket and key_freeso().
1061 _key_freesp(struct secpolicy **spp, const char* where, int tag)
1063 struct secpolicy *sp = *spp;
1065 KASSERT(sp != NULL, ("key_freesp: null sp"));
1069 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1070 printf("DP key_freesp SP:%p (ID=%u) from %s:%u; refcnt now %u\n",
1071 sp, sp->id, where, tag, sp->refcnt));
1073 if (sp->refcnt == 0) {
1080 * Must be called after calling key_allocsp().
1081 * For the packet with socket.
1084 key_freeso(struct socket *so)
1087 KASSERT(so != NULL, ("key_freeso: null so"));
1089 switch (so->so_proto->pr_domain->dom_family) {
1093 struct inpcb *pcb = so->so_pcb;
1095 /* Does it have a PCB ? */
1098 key_freesp_so(&pcb->inp_sp->sp_in);
1099 key_freesp_so(&pcb->inp_sp->sp_out);
1106 #ifdef HAVE_NRL_INPCB
1107 struct inpcb *pcb = so->so_pcb;
1109 /* Does it have a PCB ? */
1112 key_freesp_so(&pcb->inp_sp->sp_in);
1113 key_freesp_so(&pcb->inp_sp->sp_out);
1115 struct in6pcb *pcb = so->so_pcb;
1117 /* Does it have a PCB ? */
1120 key_freesp_so(&pcb->in6p_sp->sp_in);
1121 key_freesp_so(&pcb->in6p_sp->sp_out);
1127 ipseclog((LOG_DEBUG, "key_freeso: unknown address family=%d.\n",
1128 so->so_proto->pr_domain->dom_family));
1134 key_freesp_so(struct secpolicy **sp)
1136 KASSERT(sp != NULL && *sp != NULL, ("key_freesp_so: null sp"));
1138 if ((*sp)->policy == IPSEC_POLICY_ENTRUST ||
1139 (*sp)->policy == IPSEC_POLICY_BYPASS)
1142 KASSERT((*sp)->policy == IPSEC_POLICY_IPSEC,
1143 ("key_freesp_so: invalid policy %u", (*sp)->policy));
1148 * Must be called after calling key_allocsa().
1149 * This function is called by key_freesp() to free some SA allocated
1153 key_freesav(struct secasvar **psav, const char* where, int tag)
1155 struct secasvar *sav = *psav;
1157 KASSERT(sav != NULL, ("key_freesav: null sav"));
1161 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1162 printf("DP key_freesav SA:%p (SPI %u) from %s:%u; refcnt now %u\n",
1163 sav, ntohl(sav->spi), where, tag, sav->refcnt));
1165 if (sav->refcnt == 0) {
1171 /* %%% SPD management */
1173 * free security policy entry.
1176 key_delsp(struct secpolicy *sp)
1180 KASSERT(sp != NULL, ("key_delsp: null sp"));
1182 sp->state = IPSEC_SPSTATE_DEAD;
1184 KASSERT(sp->refcnt == 0,
1185 ("key_delsp: SP with references deleted (refcnt %u)",
1189 /* remove from SP index */
1190 if (__LIST_CHAINED(sp))
1191 LIST_REMOVE(sp, chain);
1194 struct ipsecrequest *isr = sp->req, *nextisr;
1196 while (isr != NULL) {
1197 if (isr->sav != NULL) {
1198 KEY_FREESAV(&isr->sav);
1202 nextisr = isr->next;
1215 * OUT: NULL : not found
1216 * others : found, pointer to a SP.
1218 static struct secpolicy *
1219 key_getsp(struct secpolicyindex *spidx)
1221 struct secpolicy *sp;
1223 KASSERT(spidx != NULL, ("key_getsp: null spidx"));
1225 LIST_FOREACH(sp, &sptree[spidx->dir], chain) {
1226 if (sp->state == IPSEC_SPSTATE_DEAD)
1228 if (key_cmpspidx_exactly(spidx, &sp->spidx)) {
1239 * OUT: NULL : not found
1240 * others : found, pointer to a SP.
1242 static struct secpolicy *
1243 key_getspbyid(u_int32_t id)
1245 struct secpolicy *sp;
1247 LIST_FOREACH(sp, &sptree[IPSEC_DIR_INBOUND], chain) {
1248 if (sp->state == IPSEC_SPSTATE_DEAD)
1256 LIST_FOREACH(sp, &sptree[IPSEC_DIR_OUTBOUND], chain) {
1257 if (sp->state == IPSEC_SPSTATE_DEAD)
1269 key_newsp(const char* where, int tag)
1271 struct secpolicy *newsp = NULL;
1273 newsp = kmalloc(sizeof(struct secpolicy), M_SECA,
1274 M_INTWAIT | M_ZERO | M_NULLOK);
1280 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1281 printf("DP key_newsp from %s:%u return SP:%p\n",
1282 where, tag, newsp));
1287 * create secpolicy structure from sadb_x_policy structure.
1288 * NOTE: `state', `secpolicyindex' in secpolicy structure are not set,
1289 * so must be set properly later.
1292 key_msg2sp(struct sadb_x_policy *xpl0, size_t len, int *error)
1294 struct secpolicy *newsp;
1298 panic("key_msg2sp: NULL pointer was passed.\n");
1299 if (len < sizeof(*xpl0))
1300 panic("key_msg2sp: invalid length.\n");
1301 if (len != PFKEY_EXTLEN(xpl0)) {
1302 ipseclog((LOG_DEBUG, "key_msg2sp: Invalid msg length.\n"));
1307 if ((newsp = KEY_NEWSP()) == NULL) {
1312 newsp->spidx.dir = xpl0->sadb_x_policy_dir;
1313 newsp->policy = xpl0->sadb_x_policy_type;
1316 switch (xpl0->sadb_x_policy_type) {
1317 case IPSEC_POLICY_DISCARD:
1318 case IPSEC_POLICY_NONE:
1319 case IPSEC_POLICY_ENTRUST:
1320 case IPSEC_POLICY_BYPASS:
1324 case IPSEC_POLICY_IPSEC:
1327 struct sadb_x_ipsecrequest *xisr;
1328 struct ipsecrequest **p_isr = &newsp->req;
1330 /* validity check */
1331 if (PFKEY_EXTLEN(xpl0) < sizeof(*xpl0)) {
1332 ipseclog((LOG_DEBUG,
1333 "key_msg2sp: Invalid msg length.\n"));
1339 tlen = PFKEY_EXTLEN(xpl0) - sizeof(*xpl0);
1340 xisr = (struct sadb_x_ipsecrequest *)(xpl0 + 1);
1344 if (xisr->sadb_x_ipsecrequest_len < sizeof(*xisr)) {
1345 ipseclog((LOG_DEBUG, "key_msg2sp: "
1346 "invalid ipsecrequest length.\n"));
1352 /* allocate request buffer */
1353 KMALLOC(*p_isr, struct ipsecrequest *, sizeof(**p_isr));
1354 if ((*p_isr) == NULL) {
1355 ipseclog((LOG_DEBUG,
1356 "key_msg2sp: No more memory.\n"));
1361 bzero(*p_isr, sizeof(**p_isr));
1364 (*p_isr)->next = NULL;
1366 switch (xisr->sadb_x_ipsecrequest_proto) {
1369 case IPPROTO_IPCOMP:
1372 ipseclog((LOG_DEBUG,
1373 "key_msg2sp: invalid proto type=%u\n",
1374 xisr->sadb_x_ipsecrequest_proto));
1376 *error = EPROTONOSUPPORT;
1379 (*p_isr)->saidx.proto = xisr->sadb_x_ipsecrequest_proto;
1381 switch (xisr->sadb_x_ipsecrequest_mode) {
1382 case IPSEC_MODE_TRANSPORT:
1383 case IPSEC_MODE_TUNNEL:
1385 case IPSEC_MODE_ANY:
1387 ipseclog((LOG_DEBUG,
1388 "key_msg2sp: invalid mode=%u\n",
1389 xisr->sadb_x_ipsecrequest_mode));
1394 (*p_isr)->saidx.mode = xisr->sadb_x_ipsecrequest_mode;
1396 switch (xisr->sadb_x_ipsecrequest_level) {
1397 case IPSEC_LEVEL_DEFAULT:
1398 case IPSEC_LEVEL_USE:
1399 case IPSEC_LEVEL_REQUIRE:
1401 case IPSEC_LEVEL_UNIQUE:
1402 /* validity check */
1404 * If range violation of reqid, kernel will
1405 * update it, don't refuse it.
1407 if (xisr->sadb_x_ipsecrequest_reqid
1408 > IPSEC_MANUAL_REQID_MAX) {
1409 ipseclog((LOG_DEBUG,
1410 "key_msg2sp: reqid=%d range "
1411 "violation, updated by kernel.\n",
1412 xisr->sadb_x_ipsecrequest_reqid));
1413 xisr->sadb_x_ipsecrequest_reqid = 0;
1416 /* allocate new reqid id if reqid is zero. */
1417 if (xisr->sadb_x_ipsecrequest_reqid == 0) {
1419 if ((reqid = key_newreqid()) == 0) {
1424 (*p_isr)->saidx.reqid = reqid;
1425 xisr->sadb_x_ipsecrequest_reqid = reqid;
1427 /* set it for manual keying. */
1428 (*p_isr)->saidx.reqid =
1429 xisr->sadb_x_ipsecrequest_reqid;
1434 ipseclog((LOG_DEBUG, "key_msg2sp: invalid level=%u\n",
1435 xisr->sadb_x_ipsecrequest_level));
1440 (*p_isr)->level = xisr->sadb_x_ipsecrequest_level;
1442 /* set IP addresses if there */
1443 if (xisr->sadb_x_ipsecrequest_len > sizeof(*xisr)) {
1444 struct sockaddr *paddr;
1446 paddr = (struct sockaddr *)(xisr + 1);
1448 /* validity check */
1450 > sizeof((*p_isr)->saidx.src)) {
1451 ipseclog((LOG_DEBUG, "key_msg2sp: invalid request "
1452 "address length.\n"));
1457 bcopy(paddr, &(*p_isr)->saidx.src,
1460 paddr = (struct sockaddr *)((caddr_t)paddr
1463 /* validity check */
1465 > sizeof((*p_isr)->saidx.dst)) {
1466 ipseclog((LOG_DEBUG, "key_msg2sp: invalid request "
1467 "address length.\n"));
1472 bcopy(paddr, &(*p_isr)->saidx.dst,
1476 (*p_isr)->sav = NULL;
1477 (*p_isr)->sp = newsp;
1479 /* initialization for the next. */
1480 p_isr = &(*p_isr)->next;
1481 tlen -= xisr->sadb_x_ipsecrequest_len;
1483 /* validity check */
1485 ipseclog((LOG_DEBUG, "key_msg2sp: becoming tlen < 0.\n"));
1491 xisr = (struct sadb_x_ipsecrequest *)((caddr_t)xisr
1492 + xisr->sadb_x_ipsecrequest_len);
1497 ipseclog((LOG_DEBUG, "key_msg2sp: invalid policy type.\n"));
1510 static u_int32_t auto_reqid = IPSEC_MANUAL_REQID_MAX + 1;
1512 auto_reqid = (auto_reqid == ~0
1513 ? IPSEC_MANUAL_REQID_MAX + 1 : auto_reqid + 1);
1515 /* XXX should be unique check */
1521 * copy secpolicy struct to sadb_x_policy structure indicated.
1524 key_sp2msg(struct secpolicy *sp)
1526 struct sadb_x_policy *xpl;
1533 panic("key_sp2msg: NULL pointer was passed.\n");
1535 tlen = key_getspreqmsglen(sp);
1537 m = key_alloc_mbuf(tlen);
1538 if (!m || m->m_next) { /*XXX*/
1546 xpl = mtod(m, struct sadb_x_policy *);
1549 xpl->sadb_x_policy_len = PFKEY_UNIT64(tlen);
1550 xpl->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
1551 xpl->sadb_x_policy_type = sp->policy;
1552 xpl->sadb_x_policy_dir = sp->spidx.dir;
1553 xpl->sadb_x_policy_id = sp->id;
1554 p = (caddr_t)xpl + sizeof(*xpl);
1556 /* if is the policy for ipsec ? */
1557 if (sp->policy == IPSEC_POLICY_IPSEC) {
1558 struct sadb_x_ipsecrequest *xisr;
1559 struct ipsecrequest *isr;
1561 for (isr = sp->req; isr != NULL; isr = isr->next) {
1563 xisr = (struct sadb_x_ipsecrequest *)p;
1565 xisr->sadb_x_ipsecrequest_proto = isr->saidx.proto;
1566 xisr->sadb_x_ipsecrequest_mode = isr->saidx.mode;
1567 xisr->sadb_x_ipsecrequest_level = isr->level;
1568 xisr->sadb_x_ipsecrequest_reqid = isr->saidx.reqid;
1571 bcopy(&isr->saidx.src, p, isr->saidx.src.sa.sa_len);
1572 p += isr->saidx.src.sa.sa_len;
1573 bcopy(&isr->saidx.dst, p, isr->saidx.dst.sa.sa_len);
1574 p += isr->saidx.src.sa.sa_len;
1576 xisr->sadb_x_ipsecrequest_len =
1577 PFKEY_ALIGN8(sizeof(*xisr)
1578 + isr->saidx.src.sa.sa_len
1579 + isr->saidx.dst.sa.sa_len);
1586 /* m will not be freed nor modified */
1587 static struct mbuf *
1588 key_gather_mbuf(struct mbuf *m, const struct sadb_msghdr *mhp,
1589 int ndeep, int nitem, ...)
1594 struct mbuf *result = NULL, *n;
1597 if (m == NULL || mhp == NULL)
1598 panic("null pointer passed to key_gather");
1600 __va_start(ap, nitem);
1601 for (i = 0; i < nitem; i++) {
1602 idx = __va_arg(ap, int);
1603 if (idx < 0 || idx > SADB_EXT_MAX)
1605 /* don't attempt to pull empty extension */
1606 if (idx == SADB_EXT_RESERVED && mhp->msg == NULL)
1608 if (idx != SADB_EXT_RESERVED &&
1609 (mhp->ext[idx] == NULL || mhp->extlen[idx] == 0))
1612 if (idx == SADB_EXT_RESERVED) {
1613 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
1616 panic("assumption failed");
1618 MGETHDR(n, MB_DONTWAIT, MT_DATA);
1623 m_copydata(m, 0, sizeof(struct sadb_msg),
1625 } else if (i < ndeep) {
1626 len = mhp->extlen[idx];
1627 n = key_alloc_mbuf(len);
1628 if (!n || n->m_next) { /*XXX*/
1633 m_copydata(m, mhp->extoff[idx], mhp->extlen[idx],
1636 n = m_copym(m, mhp->extoff[idx], mhp->extlen[idx],
1649 if ((result->m_flags & M_PKTHDR) != 0) {
1650 result->m_pkthdr.len = 0;
1651 for (n = result; n; n = n->m_next)
1652 result->m_pkthdr.len += n->m_len;
1663 * SADB_X_SPDADD, SADB_X_SPDSETIDX or SADB_X_SPDUPDATE processing
1664 * add an entry to SP database, when received
1665 * <base, address(SD), (lifetime(H),) policy>
1667 * Adding to SP database,
1669 * <base, address(SD), (lifetime(H),) policy>
1670 * to the socket which was send.
1672 * SPDADD set a unique policy entry.
1673 * SPDSETIDX like SPDADD without a part of policy requests.
1674 * SPDUPDATE replace a unique policy entry.
1676 * m will always be freed.
1679 key_spdadd(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
1681 struct sadb_address *src0, *dst0;
1682 struct sadb_x_policy *xpl0, *xpl;
1683 struct sadb_lifetime *lft = NULL;
1684 struct secpolicyindex spidx;
1685 struct secpolicy *newsp;
1689 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
1690 panic("key_spdadd: NULL pointer is passed.\n");
1692 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
1693 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
1694 mhp->ext[SADB_X_EXT_POLICY] == NULL) {
1695 ipseclog((LOG_DEBUG, "key_spdadd: invalid message is passed.\n"));
1696 return key_senderror(so, m, EINVAL);
1698 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
1699 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
1700 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
1701 ipseclog((LOG_DEBUG, "key_spdadd: invalid message is passed.\n"));
1702 return key_senderror(so, m, EINVAL);
1704 if (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL) {
1705 if (mhp->extlen[SADB_EXT_LIFETIME_HARD]
1706 < sizeof(struct sadb_lifetime)) {
1707 ipseclog((LOG_DEBUG, "key_spdadd: invalid message is passed.\n"));
1708 return key_senderror(so, m, EINVAL);
1710 lft = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_HARD];
1713 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
1714 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
1715 xpl0 = (struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY];
1718 /* XXX boundary check against sa_len */
1719 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
1722 src0->sadb_address_prefixlen,
1723 dst0->sadb_address_prefixlen,
1724 src0->sadb_address_proto,
1727 /* checking the direciton. */
1728 switch (xpl0->sadb_x_policy_dir) {
1729 case IPSEC_DIR_INBOUND:
1730 case IPSEC_DIR_OUTBOUND:
1733 ipseclog((LOG_DEBUG, "key_spdadd: Invalid SP direction.\n"));
1734 mhp->msg->sadb_msg_errno = EINVAL;
1739 /* key_spdadd() accepts DISCARD, NONE and IPSEC. */
1740 if (xpl0->sadb_x_policy_type == IPSEC_POLICY_ENTRUST
1741 || xpl0->sadb_x_policy_type == IPSEC_POLICY_BYPASS) {
1742 ipseclog((LOG_DEBUG, "key_spdadd: Invalid policy type.\n"));
1743 return key_senderror(so, m, EINVAL);
1746 /* policy requests are mandatory when action is ipsec. */
1747 if (mhp->msg->sadb_msg_type != SADB_X_SPDSETIDX
1748 && xpl0->sadb_x_policy_type == IPSEC_POLICY_IPSEC
1749 && mhp->extlen[SADB_X_EXT_POLICY] <= sizeof(*xpl0)) {
1750 ipseclog((LOG_DEBUG, "key_spdadd: some policy requests part required.\n"));
1751 return key_senderror(so, m, EINVAL);
1755 * checking there is SP already or not.
1756 * SPDUPDATE doesn't depend on whether there is a SP or not.
1757 * If the type is either SPDADD or SPDSETIDX AND a SP is found,
1760 newsp = key_getsp(&spidx);
1761 if (mhp->msg->sadb_msg_type == SADB_X_SPDUPDATE) {
1763 newsp->state = IPSEC_SPSTATE_DEAD;
1767 if (newsp != NULL) {
1769 ipseclog((LOG_DEBUG, "key_spdadd: a SP entry exists already.\n"));
1770 return key_senderror(so, m, EEXIST);
1774 /* allocation new SP entry */
1775 if ((newsp = key_msg2sp(xpl0, PFKEY_EXTLEN(xpl0), &error)) == NULL) {
1776 return key_senderror(so, m, error);
1779 if ((newsp->id = key_getnewspid()) == 0) {
1781 return key_senderror(so, m, ENOBUFS);
1784 /* XXX boundary check against sa_len */
1785 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
1788 src0->sadb_address_prefixlen,
1789 dst0->sadb_address_prefixlen,
1790 src0->sadb_address_proto,
1793 /* sanity check on addr pair */
1794 if (((struct sockaddr *)(src0 + 1))->sa_family !=
1795 ((struct sockaddr *)(dst0+ 1))->sa_family) {
1797 return key_senderror(so, m, EINVAL);
1799 if (((struct sockaddr *)(src0 + 1))->sa_len !=
1800 ((struct sockaddr *)(dst0+ 1))->sa_len) {
1802 return key_senderror(so, m, EINVAL);
1805 if (newsp->req && newsp->req->saidx.src.sa.sa_family) {
1806 struct sockaddr *sa;
1807 sa = (struct sockaddr *)(src0 + 1);
1808 if (sa->sa_family != newsp->req->saidx.src.sa.sa_family) {
1810 return key_senderror(so, m, EINVAL);
1813 if (newsp->req && newsp->req->saidx.dst.sa.sa_family) {
1814 struct sockaddr *sa;
1815 sa = (struct sockaddr *)(dst0 + 1);
1816 if (sa->sa_family != newsp->req->saidx.dst.sa.sa_family) {
1818 return key_senderror(so, m, EINVAL);
1823 newsp->created = time_second;
1824 newsp->lastused = newsp->created;
1825 newsp->lifetime = lft ? lft->sadb_lifetime_addtime : 0;
1826 newsp->validtime = lft ? lft->sadb_lifetime_usetime : 0;
1828 newsp->refcnt = 1; /* do not reclaim until I say I do */
1829 newsp->state = IPSEC_SPSTATE_ALIVE;
1830 LIST_INSERT_TAIL(&sptree[newsp->spidx.dir], newsp, secpolicy, chain);
1832 /* delete the entry in spacqtree */
1833 if (mhp->msg->sadb_msg_type == SADB_X_SPDUPDATE) {
1834 struct secspacq *spacq;
1835 if ((spacq = key_getspacq(&spidx)) != NULL) {
1836 /* reset counter in order to deletion by timehandler. */
1837 spacq->created = time_second;
1843 struct mbuf *n, *mpolicy;
1844 struct sadb_msg *newmsg;
1847 /* create new sadb_msg to reply. */
1849 n = key_gather_mbuf(m, mhp, 2, 5, SADB_EXT_RESERVED,
1850 SADB_X_EXT_POLICY, SADB_EXT_LIFETIME_HARD,
1851 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
1853 n = key_gather_mbuf(m, mhp, 2, 4, SADB_EXT_RESERVED,
1855 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
1858 return key_senderror(so, m, ENOBUFS);
1860 if (n->m_len < sizeof(*newmsg)) {
1861 n = m_pullup(n, sizeof(*newmsg));
1863 return key_senderror(so, m, ENOBUFS);
1865 newmsg = mtod(n, struct sadb_msg *);
1866 newmsg->sadb_msg_errno = 0;
1867 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
1870 mpolicy = m_pulldown(n, PFKEY_ALIGN8(sizeof(struct sadb_msg)),
1871 sizeof(*xpl), &off);
1872 if (mpolicy == NULL) {
1873 /* n is already freed */
1874 return key_senderror(so, m, ENOBUFS);
1876 xpl = (struct sadb_x_policy *)(mtod(mpolicy, caddr_t) + off);
1877 if (xpl->sadb_x_policy_exttype != SADB_X_EXT_POLICY) {
1879 return key_senderror(so, m, EINVAL);
1881 xpl->sadb_x_policy_id = newsp->id;
1884 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
1889 * get new policy id.
1895 key_getnewspid(void)
1897 u_int32_t newid = 0;
1898 int count = key_spi_trycnt; /* XXX */
1899 struct secpolicy *sp;
1901 /* when requesting to allocate spi ranged */
1903 newid = (policy_id = (policy_id == ~0 ? 1 : policy_id + 1));
1905 if ((sp = key_getspbyid(newid)) == NULL)
1911 if (count == 0 || newid == 0) {
1912 ipseclog((LOG_DEBUG, "key_getnewspid: to allocate policy id is failed.\n"));
1920 * SADB_SPDDELETE processing
1922 * <base, address(SD), policy(*)>
1923 * from the user(?), and set SADB_SASTATE_DEAD,
1925 * <base, address(SD), policy(*)>
1927 * policy(*) including direction of policy.
1929 * m will always be freed.
1932 key_spddelete(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
1934 struct sadb_address *src0, *dst0;
1935 struct sadb_x_policy *xpl0;
1936 struct secpolicyindex spidx;
1937 struct secpolicy *sp;
1940 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
1941 panic("key_spddelete: NULL pointer is passed.\n");
1943 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
1944 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
1945 mhp->ext[SADB_X_EXT_POLICY] == NULL) {
1946 ipseclog((LOG_DEBUG, "key_spddelete: invalid message is passed.\n"));
1947 return key_senderror(so, m, EINVAL);
1949 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
1950 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
1951 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
1952 ipseclog((LOG_DEBUG, "key_spddelete: invalid message is passed.\n"));
1953 return key_senderror(so, m, EINVAL);
1956 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
1957 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
1958 xpl0 = (struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY];
1961 /* XXX boundary check against sa_len */
1962 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
1965 src0->sadb_address_prefixlen,
1966 dst0->sadb_address_prefixlen,
1967 src0->sadb_address_proto,
1970 /* checking the direciton. */
1971 switch (xpl0->sadb_x_policy_dir) {
1972 case IPSEC_DIR_INBOUND:
1973 case IPSEC_DIR_OUTBOUND:
1976 ipseclog((LOG_DEBUG, "key_spddelete: Invalid SP direction.\n"));
1977 return key_senderror(so, m, EINVAL);
1980 /* Is there SP in SPD ? */
1981 if ((sp = key_getsp(&spidx)) == NULL) {
1982 ipseclog((LOG_DEBUG, "key_spddelete: no SP found.\n"));
1983 return key_senderror(so, m, EINVAL);
1986 /* save policy id to buffer to be returned. */
1987 xpl0->sadb_x_policy_id = sp->id;
1989 sp->state = IPSEC_SPSTATE_DEAD;
1994 struct sadb_msg *newmsg;
1996 /* create new sadb_msg to reply. */
1997 n = key_gather_mbuf(m, mhp, 1, 4, SADB_EXT_RESERVED,
1998 SADB_X_EXT_POLICY, SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
2000 return key_senderror(so, m, ENOBUFS);
2002 newmsg = mtod(n, struct sadb_msg *);
2003 newmsg->sadb_msg_errno = 0;
2004 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
2007 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
2012 * SADB_SPDDELETE2 processing
2015 * from the user(?), and set SADB_SASTATE_DEAD,
2019 * policy(*) including direction of policy.
2021 * m will always be freed.
2024 key_spddelete2(struct socket *so, struct mbuf *m,
2025 const struct sadb_msghdr *mhp)
2028 struct secpolicy *sp;
2031 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
2032 panic("key_spddelete2: NULL pointer is passed.\n");
2034 if (mhp->ext[SADB_X_EXT_POLICY] == NULL ||
2035 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2036 ipseclog((LOG_DEBUG, "key_spddelete2: invalid message is passed.\n"));
2037 key_senderror(so, m, EINVAL);
2041 id = ((struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY])->sadb_x_policy_id;
2043 /* Is there SP in SPD ? */
2044 if ((sp = key_getspbyid(id)) == NULL) {
2045 ipseclog((LOG_DEBUG, "key_spddelete2: no SP found id:%u.\n", id));
2046 key_senderror(so, m, EINVAL);
2049 sp->state = IPSEC_SPSTATE_DEAD;
2053 struct mbuf *n, *nn;
2054 struct sadb_msg *newmsg;
2057 /* create new sadb_msg to reply. */
2058 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
2061 return key_senderror(so, m, ENOBUFS);
2062 MGETHDR(n, MB_DONTWAIT, MT_DATA);
2063 if (n && len > MHLEN) {
2064 MCLGET(n, MB_DONTWAIT);
2065 if ((n->m_flags & M_EXT) == 0) {
2071 return key_senderror(so, m, ENOBUFS);
2077 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
2078 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
2082 panic("length inconsistency in key_spddelete2");
2085 n->m_next = m_copym(m, mhp->extoff[SADB_X_EXT_POLICY],
2086 mhp->extlen[SADB_X_EXT_POLICY], MB_DONTWAIT);
2089 return key_senderror(so, m, ENOBUFS);
2092 n->m_pkthdr.len = 0;
2093 for (nn = n; nn; nn = nn->m_next)
2094 n->m_pkthdr.len += nn->m_len;
2096 newmsg = mtod(n, struct sadb_msg *);
2097 newmsg->sadb_msg_errno = 0;
2098 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
2101 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
2106 * SADB_X_GET processing
2111 * <base, address(SD), policy>
2113 * policy(*) including direction of policy.
2115 * m will always be freed.
2118 key_spdget(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
2121 struct secpolicy *sp;
2125 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
2126 panic("key_spdget: NULL pointer is passed.\n");
2128 if (mhp->ext[SADB_X_EXT_POLICY] == NULL ||
2129 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2130 ipseclog((LOG_DEBUG, "key_spdget: invalid message is passed.\n"));
2131 return key_senderror(so, m, EINVAL);
2134 id = ((struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY])->sadb_x_policy_id;
2136 /* Is there SP in SPD ? */
2137 if ((sp = key_getspbyid(id)) == NULL) {
2138 ipseclog((LOG_DEBUG, "key_spdget: no SP found id:%u.\n", id));
2139 return key_senderror(so, m, ENOENT);
2142 n = key_setdumpsp(sp, SADB_X_SPDGET, 0, mhp->msg->sadb_msg_pid);
2145 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
2147 return key_senderror(so, m, ENOBUFS);
2151 * SADB_X_SPDACQUIRE processing.
2152 * Acquire policy and SA(s) for a *OUTBOUND* packet.
2155 * to KMD, and expect to receive
2156 * <base> with SADB_X_SPDACQUIRE if error occured,
2159 * with SADB_X_SPDUPDATE from KMD by PF_KEY.
2160 * policy(*) is without policy requests.
2163 * others: error number
2166 key_spdacquire(struct secpolicy *sp)
2168 struct mbuf *result = NULL, *m;
2169 struct secspacq *newspacq;
2174 panic("key_spdacquire: NULL pointer is passed.\n");
2175 if (sp->req != NULL)
2176 panic("key_spdacquire: called but there is request.\n");
2177 if (sp->policy != IPSEC_POLICY_IPSEC)
2178 panic("key_spdacquire: policy mismathed. IPsec is expected.\n");
2180 /* Get an entry to check whether sent message or not. */
2181 if ((newspacq = key_getspacq(&sp->spidx)) != NULL) {
2182 if (key_blockacq_count < newspacq->count) {
2183 /* reset counter and do send message. */
2184 newspacq->count = 0;
2186 /* increment counter and do nothing. */
2191 /* make new entry for blocking to send SADB_ACQUIRE. */
2192 if ((newspacq = key_newspacq(&sp->spidx)) == NULL)
2195 /* add to acqtree */
2196 LIST_INSERT_HEAD(&spacqtree, newspacq, chain);
2199 /* create new sadb_msg to reply. */
2200 m = key_setsadbmsg(SADB_X_SPDACQUIRE, 0, 0, 0, 0, 0);
2207 result->m_pkthdr.len = 0;
2208 for (m = result; m; m = m->m_next)
2209 result->m_pkthdr.len += m->m_len;
2211 mtod(result, struct sadb_msg *)->sadb_msg_len =
2212 PFKEY_UNIT64(result->m_pkthdr.len);
2214 return key_sendup_mbuf(NULL, m, KEY_SENDUP_REGISTERED);
2223 * SADB_SPDFLUSH processing
2226 * from the user, and free all entries in secpctree.
2230 * NOTE: what to do is only marking SADB_SASTATE_DEAD.
2232 * m will always be freed.
2235 key_spdflush(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
2237 struct sadb_msg *newmsg;
2238 struct secpolicy *sp;
2242 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
2243 panic("key_spdflush: NULL pointer is passed.\n");
2245 if (m->m_len != PFKEY_ALIGN8(sizeof(struct sadb_msg)))
2246 return key_senderror(so, m, EINVAL);
2248 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2249 LIST_FOREACH(sp, &sptree[dir], chain) {
2250 sp->state = IPSEC_SPSTATE_DEAD;
2254 if (sizeof(struct sadb_msg) > m->m_len + M_TRAILINGSPACE(m)) {
2255 ipseclog((LOG_DEBUG, "key_spdflush: No more memory.\n"));
2256 return key_senderror(so, m, ENOBUFS);
2262 m->m_pkthdr.len = m->m_len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
2263 newmsg = mtod(m, struct sadb_msg *);
2264 newmsg->sadb_msg_errno = 0;
2265 newmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
2267 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
2271 * SADB_SPDDUMP processing
2274 * from the user, and dump all SP leaves
2279 * m will always be freed.
2282 key_spddump(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
2284 struct secpolicy *sp;
2290 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
2291 panic("key_spddump: NULL pointer is passed.\n");
2293 /* search SPD entry and get buffer size. */
2295 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2296 LIST_FOREACH(sp, &sptree[dir], chain) {
2302 return key_senderror(so, m, ENOENT);
2304 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2305 LIST_FOREACH(sp, &sptree[dir], chain) {
2307 n = key_setdumpsp(sp, SADB_X_SPDDUMP, cnt,
2308 mhp->msg->sadb_msg_pid);
2311 key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
2319 static struct mbuf *
2320 key_setdumpsp(struct secpolicy *sp, u_int8_t type, u_int32_t seq,
2323 struct mbuf *result = NULL, *m;
2325 m = key_setsadbmsg(type, 0, SADB_SATYPE_UNSPEC, seq, pid, sp->refcnt);
2330 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
2331 &sp->spidx.src.sa, sp->spidx.prefs,
2332 sp->spidx.ul_proto);
2337 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
2338 &sp->spidx.dst.sa, sp->spidx.prefd,
2339 sp->spidx.ul_proto);
2349 if ((result->m_flags & M_PKTHDR) == 0)
2352 if (result->m_len < sizeof(struct sadb_msg)) {
2353 result = m_pullup(result, sizeof(struct sadb_msg));
2358 result->m_pkthdr.len = 0;
2359 for (m = result; m; m = m->m_next)
2360 result->m_pkthdr.len += m->m_len;
2362 mtod(result, struct sadb_msg *)->sadb_msg_len =
2363 PFKEY_UNIT64(result->m_pkthdr.len);
2373 * get PFKEY message length for security policy and request.
2376 key_getspreqmsglen(struct secpolicy *sp)
2380 tlen = sizeof(struct sadb_x_policy);
2382 /* if is the policy for ipsec ? */
2383 if (sp->policy != IPSEC_POLICY_IPSEC)
2386 /* get length of ipsec requests */
2388 struct ipsecrequest *isr;
2391 for (isr = sp->req; isr != NULL; isr = isr->next) {
2392 len = sizeof(struct sadb_x_ipsecrequest)
2393 + isr->saidx.src.sa.sa_len
2394 + isr->saidx.dst.sa.sa_len;
2396 tlen += PFKEY_ALIGN8(len);
2404 * SADB_SPDEXPIRE processing
2406 * <base, address(SD), lifetime(CH), policy>
2410 * others : error number
2413 key_spdexpire(struct secpolicy *sp)
2416 struct mbuf *result = NULL, *m;
2419 struct sadb_lifetime *lt;
2421 /* XXX: Why do we lock ? */
2426 panic("key_spdexpire: NULL pointer is passed.\n");
2428 /* set msg header */
2429 m = key_setsadbmsg(SADB_X_SPDEXPIRE, 0, 0, 0, 0, 0);
2436 /* create lifetime extension (current and hard) */
2437 len = PFKEY_ALIGN8(sizeof(*lt)) * 2;
2438 m = key_alloc_mbuf(len);
2439 if (!m || m->m_next) { /*XXX*/
2445 bzero(mtod(m, caddr_t), len);
2446 lt = mtod(m, struct sadb_lifetime *);
2447 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
2448 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
2449 lt->sadb_lifetime_allocations = 0;
2450 lt->sadb_lifetime_bytes = 0;
2451 lt->sadb_lifetime_addtime = sp->created;
2452 lt->sadb_lifetime_usetime = sp->lastused;
2453 lt = (struct sadb_lifetime *)(mtod(m, caddr_t) + len / 2);
2454 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
2455 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
2456 lt->sadb_lifetime_allocations = 0;
2457 lt->sadb_lifetime_bytes = 0;
2458 lt->sadb_lifetime_addtime = sp->lifetime;
2459 lt->sadb_lifetime_usetime = sp->validtime;
2462 /* set sadb_address for source */
2463 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
2465 sp->spidx.prefs, sp->spidx.ul_proto);
2472 /* set sadb_address for destination */
2473 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
2475 sp->spidx.prefd, sp->spidx.ul_proto);
2490 if ((result->m_flags & M_PKTHDR) == 0) {
2495 if (result->m_len < sizeof(struct sadb_msg)) {
2496 result = m_pullup(result, sizeof(struct sadb_msg));
2497 if (result == NULL) {
2503 result->m_pkthdr.len = 0;
2504 for (m = result; m; m = m->m_next)
2505 result->m_pkthdr.len += m->m_len;
2507 mtod(result, struct sadb_msg *)->sadb_msg_len =
2508 PFKEY_UNIT64(result->m_pkthdr.len);
2510 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
2519 /* %%% SAD management */
2521 * allocating a memory for new SA head, and copy from the values of mhp.
2522 * OUT: NULL : failure due to the lack of memory.
2523 * others : pointer to new SA head.
2525 static struct secashead *
2526 key_newsah(struct secasindex *saidx)
2528 struct secashead *newsah;
2530 KASSERT(saidx != NULL, ("key_newsaidx: null saidx"));
2532 newsah = kmalloc(sizeof(struct secashead), M_SECA,
2533 M_INTWAIT | M_ZERO | M_NULLOK);
2534 if (newsah != NULL) {
2536 for (i = 0; i < sizeof(newsah->savtree)/sizeof(newsah->savtree[0]); i++)
2537 LIST_INIT(&newsah->savtree[i]);
2538 newsah->saidx = *saidx;
2540 /* add to saidxtree */
2541 newsah->state = SADB_SASTATE_MATURE;
2542 LIST_INSERT_HEAD(&sahtree, newsah, chain);
2548 * delete SA index and all SA registerd.
2551 key_delsah(struct secashead *sah)
2553 struct secasvar *sav, *nextsav;
2554 u_int stateidx, state;
2560 panic("key_delsah: NULL pointer is passed.\n");
2564 /* searching all SA registerd in the secindex. */
2566 stateidx < _ARRAYLEN(saorder_state_any);
2569 state = saorder_state_any[stateidx];
2570 for (sav = (struct secasvar *)LIST_FIRST(&sah->savtree[state]);
2574 nextsav = LIST_NEXT(sav, chain);
2576 if (sav->refcnt == 0) {
2578 KEY_CHKSASTATE(state, sav->state, "key_delsah");
2581 /* give up to delete this sa */
2587 /* don't delete sah only if there are savs. */
2593 if (sah->sa_route.ro_rt) {
2594 RTFREE(sah->sa_route.ro_rt);
2595 sah->sa_route.ro_rt = (struct rtentry *)NULL;
2598 /* remove from tree of SA index */
2599 if (__LIST_CHAINED(sah))
2600 LIST_REMOVE(sah, chain);
2609 * allocating a new SA with LARVAL state. key_add() and key_getspi() call,
2610 * and copy the values of mhp into new buffer.
2611 * When SAD message type is GETSPI:
2612 * to set sequence number from acq_seq++,
2613 * to set zero to SPI.
2614 * not to call key_setsava().
2616 * others : pointer to new secasvar.
2618 * does not modify mbuf. does not free mbuf on error.
2620 static struct secasvar *
2621 key_newsav(struct mbuf *m, const struct sadb_msghdr *mhp, struct secashead *sah,
2622 int *errp, const char *where, int tag)
2624 struct secasvar *newsav;
2625 const struct sadb_sa *xsa;
2628 if (m == NULL || mhp == NULL || mhp->msg == NULL || sah == NULL)
2629 panic("key_newsa: NULL pointer is passed.\n");
2631 KMALLOC(newsav, struct secasvar *, sizeof(struct secasvar));
2632 if (newsav == NULL) {
2633 ipseclog((LOG_DEBUG, "key_newsa: No more memory.\n"));
2637 bzero((caddr_t)newsav, sizeof(struct secasvar));
2639 switch (mhp->msg->sadb_msg_type) {
2643 #ifdef IPSEC_DOSEQCHECK
2644 /* sync sequence number */
2645 if (mhp->msg->sadb_msg_seq == 0)
2647 (acq_seq = (acq_seq == ~0 ? 1 : ++acq_seq));
2650 newsav->seq = mhp->msg->sadb_msg_seq;
2655 if (mhp->ext[SADB_EXT_SA] == NULL) {
2656 KFREE(newsav), newsav = NULL;
2657 ipseclog((LOG_DEBUG, "key_newsa: invalid message is passed.\n"));
2661 xsa = (const struct sadb_sa *)mhp->ext[SADB_EXT_SA];
2662 newsav->spi = xsa->sadb_sa_spi;
2663 newsav->seq = mhp->msg->sadb_msg_seq;
2666 KFREE(newsav), newsav = NULL;
2671 /* copy sav values */
2672 if (mhp->msg->sadb_msg_type != SADB_GETSPI) {
2673 *errp = key_setsaval(newsav, m, mhp);
2675 KFREE(newsav), newsav = NULL;
2681 newsav->created = time_second;
2682 newsav->pid = mhp->msg->sadb_msg_pid;
2687 newsav->state = SADB_SASTATE_LARVAL;
2688 LIST_INSERT_TAIL(&sah->savtree[SADB_SASTATE_LARVAL], newsav,
2691 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
2692 printf("DP key_newsav from %s:%u return SP:%p\n",
2693 where, tag, newsav));
2699 * free() SA variable entry.
2702 key_delsav(struct secasvar *sav)
2704 KASSERT(sav != NULL, ("key_delsav: null sav"));
2705 KASSERT(sav->refcnt == 0,
2706 ("key_delsav: reference count %u > 0", sav->refcnt));
2708 /* remove from SA header */
2709 if (__LIST_CHAINED(sav))
2710 LIST_REMOVE(sav, chain);
2712 if (sav->key_auth != NULL) {
2713 bzero(_KEYBUF(sav->key_auth), _KEYLEN(sav->key_auth));
2714 KFREE(sav->key_auth);
2715 sav->key_auth = NULL;
2717 if (sav->key_enc != NULL) {
2718 bzero(_KEYBUF(sav->key_enc), _KEYLEN(sav->key_enc));
2719 KFREE(sav->key_enc);
2720 sav->key_enc = NULL;
2723 bzero(sav->sched, sav->schedlen);
2727 if (sav->replay != NULL) {
2731 if (sav->lft_c != NULL) {
2735 if (sav->lft_h != NULL) {
2739 if (sav->lft_s != NULL) {
2743 if (sav->iv != NULL) {
2757 * others : found, pointer to a SA.
2759 static struct secashead *
2760 key_getsah(struct secasindex *saidx)
2762 struct secashead *sah;
2764 LIST_FOREACH(sah, &sahtree, chain) {
2765 if (sah->state == SADB_SASTATE_DEAD)
2767 if (key_cmpsaidx(&sah->saidx, saidx, CMP_REQID))
2775 * check not to be duplicated SPI.
2776 * NOTE: this function is too slow due to searching all SAD.
2779 * others : found, pointer to a SA.
2781 static struct secasvar *
2782 key_checkspidup(struct secasindex *saidx, u_int32_t spi)
2784 struct secashead *sah;
2785 struct secasvar *sav;
2787 /* check address family */
2788 if (saidx->src.sa.sa_family != saidx->dst.sa.sa_family) {
2789 ipseclog((LOG_DEBUG, "key_checkspidup: address family mismatched.\n"));
2794 LIST_FOREACH(sah, &sahtree, chain) {
2795 if (!key_ismyaddr((struct sockaddr *)&sah->saidx.dst))
2797 sav = key_getsavbyspi(sah, spi);
2806 * search SAD litmited alive SA, protocol, SPI.
2809 * others : found, pointer to a SA.
2811 static struct secasvar *
2812 key_getsavbyspi(struct secashead *sah, u_int32_t spi)
2814 struct secasvar *sav;
2815 u_int stateidx, state;
2817 /* search all status */
2819 stateidx < _ARRAYLEN(saorder_state_alive);
2822 state = saorder_state_alive[stateidx];
2823 LIST_FOREACH(sav, &sah->savtree[state], chain) {
2826 if (sav->state != state) {
2827 ipseclog((LOG_DEBUG, "key_getsavbyspi: "
2828 "invalid sav->state (queue: %d SA: %d)\n",
2829 state, sav->state));
2833 if (sav->spi == spi)
2842 * copy SA values from PF_KEY message except *SPI, SEQ, PID, STATE and TYPE*.
2843 * You must update these if need.
2847 * does not modify mbuf. does not free mbuf on error.
2850 key_setsaval(struct secasvar *sav, struct mbuf *m,
2851 const struct sadb_msghdr *mhp)
2856 if (m == NULL || mhp == NULL || mhp->msg == NULL)
2857 panic("key_setsaval: NULL pointer is passed.\n");
2859 /* initialization */
2861 sav->key_auth = NULL;
2862 sav->key_enc = NULL;
2869 sav->tdb_xform = NULL; /* transform */
2870 sav->tdb_encalgxform = NULL; /* encoding algorithm */
2871 sav->tdb_authalgxform = NULL; /* authentication algorithm */
2872 sav->tdb_compalgxform = NULL; /* compression algorithm */
2875 if (mhp->ext[SADB_EXT_SA] != NULL) {
2876 const struct sadb_sa *sa0;
2878 sa0 = (const struct sadb_sa *)mhp->ext[SADB_EXT_SA];
2879 if (mhp->extlen[SADB_EXT_SA] < sizeof(*sa0)) {
2884 sav->alg_auth = sa0->sadb_sa_auth;
2885 sav->alg_enc = sa0->sadb_sa_encrypt;
2886 sav->flags = sa0->sadb_sa_flags;
2889 if ((sa0->sadb_sa_flags & SADB_X_EXT_OLD) == 0) {
2891 malloc(sizeof(struct secreplay)+sa0->sadb_sa_replay,
2892 M_SECA, M_INTWAIT | M_ZERO | M_NULLOK);
2893 if (sav->replay == NULL) {
2894 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
2898 if (sa0->sadb_sa_replay != 0)
2899 sav->replay->bitmap = (caddr_t)(sav->replay+1);
2900 sav->replay->wsize = sa0->sadb_sa_replay;
2904 /* Authentication keys */
2905 if (mhp->ext[SADB_EXT_KEY_AUTH] != NULL) {
2906 const struct sadb_key *key0;
2909 key0 = (const struct sadb_key *)mhp->ext[SADB_EXT_KEY_AUTH];
2910 len = mhp->extlen[SADB_EXT_KEY_AUTH];
2913 if (len < sizeof(*key0)) {
2917 switch (mhp->msg->sadb_msg_satype) {
2918 case SADB_SATYPE_AH:
2919 case SADB_SATYPE_ESP:
2920 if (len == PFKEY_ALIGN8(sizeof(struct sadb_key)) &&
2921 sav->alg_auth != SADB_X_AALG_NULL)
2924 case SADB_X_SATYPE_IPCOMP:
2930 ipseclog((LOG_DEBUG, "key_setsaval: invalid key_auth values.\n"));
2934 sav->key_auth = (struct sadb_key *)key_newbuf(key0, len);
2935 if (sav->key_auth == NULL) {
2936 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
2942 /* Encryption key */
2943 if (mhp->ext[SADB_EXT_KEY_ENCRYPT] != NULL) {
2944 const struct sadb_key *key0;
2947 key0 = (const struct sadb_key *)mhp->ext[SADB_EXT_KEY_ENCRYPT];
2948 len = mhp->extlen[SADB_EXT_KEY_ENCRYPT];
2951 if (len < sizeof(*key0)) {
2955 switch (mhp->msg->sadb_msg_satype) {
2956 case SADB_SATYPE_ESP:
2957 if (len == PFKEY_ALIGN8(sizeof(struct sadb_key)) &&
2958 sav->alg_enc != SADB_EALG_NULL) {
2962 sav->key_enc = (struct sadb_key *)key_newbuf(key0, len);
2963 if (sav->key_enc == NULL) {
2964 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
2969 case SADB_X_SATYPE_IPCOMP:
2970 if (len != PFKEY_ALIGN8(sizeof(struct sadb_key)))
2972 sav->key_enc = NULL; /*just in case*/
2974 case SADB_SATYPE_AH:
2980 ipseclog((LOG_DEBUG, "key_setsatval: invalid key_enc value.\n"));
2988 switch (mhp->msg->sadb_msg_satype) {
2989 case SADB_SATYPE_AH:
2990 error = xform_init(sav, XF_AH);
2992 case SADB_SATYPE_ESP:
2993 error = xform_init(sav, XF_ESP);
2995 case SADB_X_SATYPE_IPCOMP:
2996 error = xform_init(sav, XF_IPCOMP);
3000 ipseclog((LOG_DEBUG,
3001 "key_setsaval: unable to initialize SA type %u.\n",
3002 mhp->msg->sadb_msg_satype));
3007 sav->created = time_second;
3009 /* make lifetime for CURRENT */
3010 KMALLOC(sav->lft_c, struct sadb_lifetime *,
3011 sizeof(struct sadb_lifetime));
3012 if (sav->lft_c == NULL) {
3013 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
3018 sav->lft_c->sadb_lifetime_len =
3019 PFKEY_UNIT64(sizeof(struct sadb_lifetime));
3020 sav->lft_c->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
3021 sav->lft_c->sadb_lifetime_allocations = 0;
3022 sav->lft_c->sadb_lifetime_bytes = 0;
3023 sav->lft_c->sadb_lifetime_addtime = time_second;
3024 sav->lft_c->sadb_lifetime_usetime = 0;
3026 /* lifetimes for HARD and SOFT */
3028 const struct sadb_lifetime *lft0;
3030 lft0 = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_HARD];
3032 if (mhp->extlen[SADB_EXT_LIFETIME_HARD] < sizeof(*lft0)) {
3036 sav->lft_h = (struct sadb_lifetime *)key_newbuf(lft0,
3038 if (sav->lft_h == NULL) {
3039 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
3043 /* to be initialize ? */
3046 lft0 = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_SOFT];
3048 if (mhp->extlen[SADB_EXT_LIFETIME_SOFT] < sizeof(*lft0)) {
3052 sav->lft_s = (struct sadb_lifetime *)key_newbuf(lft0,
3054 if (sav->lft_s == NULL) {
3055 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
3059 /* to be initialize ? */
3066 /* initialization */
3067 if (sav->replay != NULL) {
3071 if (sav->key_auth != NULL) {
3072 KFREE(sav->key_auth);
3073 sav->key_auth = NULL;
3075 if (sav->key_enc != NULL) {
3076 KFREE(sav->key_enc);
3077 sav->key_enc = NULL;
3083 if (sav->iv != NULL) {
3087 if (sav->lft_c != NULL) {
3091 if (sav->lft_h != NULL) {
3095 if (sav->lft_s != NULL) {
3104 * validation with a secasvar entry, and set SADB_SATYPE_MATURE.
3109 key_mature(struct secasvar *sav)
3113 /* check SPI value */
3114 switch (sav->sah->saidx.proto) {
3117 if (ntohl(sav->spi) >= 0 && ntohl(sav->spi) <= 255) {
3118 ipseclog((LOG_DEBUG,
3119 "key_mature: illegal range of SPI %u.\n",
3120 (u_int32_t)ntohl(sav->spi)));
3127 switch (sav->sah->saidx.proto) {
3130 if ((sav->flags & (SADB_X_EXT_OLD|SADB_X_EXT_DERIV)) ==
3131 (SADB_X_EXT_OLD|SADB_X_EXT_DERIV)) {
3132 ipseclog((LOG_DEBUG, "key_mature: "
3133 "invalid flag (derived) given to old-esp.\n"));
3136 error = xform_init(sav, XF_ESP);
3140 if (sav->flags & SADB_X_EXT_DERIV) {
3141 ipseclog((LOG_DEBUG, "key_mature: "
3142 "invalid flag (derived) given to AH SA.\n"));
3145 if (sav->alg_enc != SADB_EALG_NONE) {
3146 ipseclog((LOG_DEBUG, "key_mature: "
3147 "protocol and algorithm mismated.\n"));
3150 error = xform_init(sav, XF_AH);
3152 case IPPROTO_IPCOMP:
3153 if (sav->alg_auth != SADB_AALG_NONE) {
3154 ipseclog((LOG_DEBUG, "key_mature: "
3155 "protocol and algorithm mismated.\n"));
3158 if ((sav->flags & SADB_X_EXT_RAWCPI) == 0
3159 && ntohl(sav->spi) >= 0x10000) {
3160 ipseclog((LOG_DEBUG, "key_mature: invalid cpi for IPComp.\n"));
3163 error = xform_init(sav, XF_IPCOMP);
3166 ipseclog((LOG_DEBUG, "key_mature: Invalid satype.\n"));
3167 error = EPROTONOSUPPORT;
3171 key_sa_chgstate(sav, SADB_SASTATE_MATURE);
3176 * subroutine for SADB_GET and SADB_DUMP.
3178 static struct mbuf *
3179 key_setdumpsa(struct secasvar *sav, u_int8_t type, u_int8_t satype,
3180 u_int32_t seq, u_int32_t pid)
3182 struct mbuf *result = NULL, *tres = NULL, *m;
3187 SADB_EXT_SA, SADB_X_EXT_SA2,
3188 SADB_EXT_LIFETIME_HARD, SADB_EXT_LIFETIME_SOFT,
3189 SADB_EXT_LIFETIME_CURRENT, SADB_EXT_ADDRESS_SRC,
3190 SADB_EXT_ADDRESS_DST, SADB_EXT_ADDRESS_PROXY, SADB_EXT_KEY_AUTH,
3191 SADB_EXT_KEY_ENCRYPT, SADB_EXT_IDENTITY_SRC,
3192 SADB_EXT_IDENTITY_DST, SADB_EXT_SENSITIVITY,
3195 m = key_setsadbmsg(type, 0, satype, seq, pid, sav->refcnt);
3200 for (i = sizeof(dumporder)/sizeof(dumporder[0]) - 1; i >= 0; i--) {
3203 switch (dumporder[i]) {
3205 m = key_setsadbsa(sav);
3210 case SADB_X_EXT_SA2:
3211 m = key_setsadbxsa2(sav->sah->saidx.mode,
3212 sav->replay ? sav->replay->count : 0,
3213 sav->sah->saidx.reqid);
3218 case SADB_EXT_ADDRESS_SRC:
3219 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
3220 &sav->sah->saidx.src.sa,
3221 FULLMASK, IPSEC_ULPROTO_ANY);
3226 case SADB_EXT_ADDRESS_DST:
3227 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
3228 &sav->sah->saidx.dst.sa,
3229 FULLMASK, IPSEC_ULPROTO_ANY);
3234 case SADB_EXT_KEY_AUTH:
3237 l = PFKEY_UNUNIT64(sav->key_auth->sadb_key_len);
3241 case SADB_EXT_KEY_ENCRYPT:
3244 l = PFKEY_UNUNIT64(sav->key_enc->sadb_key_len);
3248 case SADB_EXT_LIFETIME_CURRENT:
3251 l = PFKEY_UNUNIT64(((struct sadb_ext *)sav->lft_c)->sadb_ext_len);
3255 case SADB_EXT_LIFETIME_HARD:
3258 l = PFKEY_UNUNIT64(((struct sadb_ext *)sav->lft_h)->sadb_ext_len);
3262 case SADB_EXT_LIFETIME_SOFT:
3265 l = PFKEY_UNUNIT64(((struct sadb_ext *)sav->lft_s)->sadb_ext_len);
3269 case SADB_EXT_ADDRESS_PROXY:
3270 case SADB_EXT_IDENTITY_SRC:
3271 case SADB_EXT_IDENTITY_DST:
3272 /* XXX: should we brought from SPD ? */
3273 case SADB_EXT_SENSITIVITY:
3278 if ((!m && !p) || (m && p))
3281 M_PREPEND(tres, l, MB_DONTWAIT);
3284 bcopy(p, mtod(tres, caddr_t), l);
3288 m = key_alloc_mbuf(l);
3291 m_copyback(m, 0, l, p);
3299 m_cat(result, tres);
3301 if (result->m_len < sizeof(struct sadb_msg)) {
3302 result = m_pullup(result, sizeof(struct sadb_msg));
3307 result->m_pkthdr.len = 0;
3308 for (m = result; m; m = m->m_next)
3309 result->m_pkthdr.len += m->m_len;
3311 mtod(result, struct sadb_msg *)->sadb_msg_len =
3312 PFKEY_UNIT64(result->m_pkthdr.len);
3323 * set data into sadb_msg.
3325 static struct mbuf *
3326 key_setsadbmsg(u_int8_t type, u_int16_t tlen, u_int8_t satype, u_int32_t seq,
3327 pid_t pid, u_int16_t reserved)
3333 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
3336 MGETHDR(m, MB_DONTWAIT, MT_DATA);
3337 if (m && len > MHLEN) {
3338 MCLGET(m, MB_DONTWAIT);
3339 if ((m->m_flags & M_EXT) == 0) {
3346 m->m_pkthdr.len = m->m_len = len;
3349 p = mtod(m, struct sadb_msg *);
3352 p->sadb_msg_version = PF_KEY_V2;
3353 p->sadb_msg_type = type;
3354 p->sadb_msg_errno = 0;
3355 p->sadb_msg_satype = satype;
3356 p->sadb_msg_len = PFKEY_UNIT64(tlen);
3357 p->sadb_msg_reserved = reserved;
3358 p->sadb_msg_seq = seq;
3359 p->sadb_msg_pid = (u_int32_t)pid;
3365 * copy secasvar data into sadb_address.
3367 static struct mbuf *
3368 key_setsadbsa(struct secasvar *sav)
3374 len = PFKEY_ALIGN8(sizeof(struct sadb_sa));
3375 m = key_alloc_mbuf(len);
3376 if (!m || m->m_next) { /*XXX*/
3382 p = mtod(m, struct sadb_sa *);
3385 p->sadb_sa_len = PFKEY_UNIT64(len);
3386 p->sadb_sa_exttype = SADB_EXT_SA;
3387 p->sadb_sa_spi = sav->spi;
3388 p->sadb_sa_replay = (sav->replay != NULL ? sav->replay->wsize : 0);
3389 p->sadb_sa_state = sav->state;
3390 p->sadb_sa_auth = sav->alg_auth;
3391 p->sadb_sa_encrypt = sav->alg_enc;
3392 p->sadb_sa_flags = sav->flags;
3398 * set data into sadb_address.
3400 static struct mbuf *
3401 key_setsadbaddr(u_int16_t exttype, const struct sockaddr *saddr, u_int8_t prefixlen,
3405 struct sadb_address *p;
3408 len = PFKEY_ALIGN8(sizeof(struct sadb_address)) +
3409 PFKEY_ALIGN8(saddr->sa_len);
3410 m = key_alloc_mbuf(len);
3411 if (!m || m->m_next) { /*XXX*/
3417 p = mtod(m, struct sadb_address *);
3420 p->sadb_address_len = PFKEY_UNIT64(len);
3421 p->sadb_address_exttype = exttype;
3422 p->sadb_address_proto = ul_proto;
3423 if (prefixlen == FULLMASK) {
3424 switch (saddr->sa_family) {
3426 prefixlen = sizeof(struct in_addr) << 3;
3429 prefixlen = sizeof(struct in6_addr) << 3;
3435 p->sadb_address_prefixlen = prefixlen;
3436 p->sadb_address_reserved = 0;
3439 mtod(m, caddr_t) + PFKEY_ALIGN8(sizeof(struct sadb_address)),
3447 * set data into sadb_ident.
3449 static struct mbuf *
3450 key_setsadbident(u_int16_t exttype, u_int16_t idtype, caddr_t string,
3451 int stringlen, u_int64_t id)
3454 struct sadb_ident *p;
3457 len = PFKEY_ALIGN8(sizeof(struct sadb_ident)) + PFKEY_ALIGN8(stringlen);
3458 m = key_alloc_mbuf(len);
3459 if (!m || m->m_next) { /*XXX*/
3465 p = mtod(m, struct sadb_ident *);
3468 p->sadb_ident_len = PFKEY_UNIT64(len);
3469 p->sadb_ident_exttype = exttype;
3470 p->sadb_ident_type = idtype;
3471 p->sadb_ident_reserved = 0;
3472 p->sadb_ident_id = id;
3475 mtod(m, caddr_t) + PFKEY_ALIGN8(sizeof(struct sadb_ident)),
3483 * set data into sadb_x_sa2.
3485 static struct mbuf *
3486 key_setsadbxsa2(u_int8_t mode, u_int32_t seq, u_int32_t reqid)
3489 struct sadb_x_sa2 *p;
3492 len = PFKEY_ALIGN8(sizeof(struct sadb_x_sa2));
3493 m = key_alloc_mbuf(len);
3494 if (!m || m->m_next) { /*XXX*/
3500 p = mtod(m, struct sadb_x_sa2 *);
3503 p->sadb_x_sa2_len = PFKEY_UNIT64(len);
3504 p->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
3505 p->sadb_x_sa2_mode = mode;
3506 p->sadb_x_sa2_reserved1 = 0;
3507 p->sadb_x_sa2_reserved2 = 0;
3508 p->sadb_x_sa2_sequence = seq;
3509 p->sadb_x_sa2_reqid = reqid;
3515 * set data into sadb_x_policy
3517 static struct mbuf *
3518 key_setsadbxpolicy(u_int16_t type, u_int8_t dir, u_int32_t id)
3521 struct sadb_x_policy *p;
3524 len = PFKEY_ALIGN8(sizeof(struct sadb_x_policy));
3525 m = key_alloc_mbuf(len);
3526 if (!m || m->m_next) { /*XXX*/
3532 p = mtod(m, struct sadb_x_policy *);
3535 p->sadb_x_policy_len = PFKEY_UNIT64(len);
3536 p->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3537 p->sadb_x_policy_type = type;
3538 p->sadb_x_policy_dir = dir;
3539 p->sadb_x_policy_id = id;
3546 * copy a buffer into the new buffer allocated.
3549 key_newbuf(const void *src, u_int len)
3553 KMALLOC(new, caddr_t, len);
3555 ipseclog((LOG_DEBUG, "key_newbuf: No more memory.\n"));
3558 bcopy(src, new, len);
3563 /* compare my own address
3564 * OUT: 1: true, i.e. my address.
3568 key_ismyaddr(struct sockaddr *sa)
3571 struct sockaddr_in *sin;
3572 struct in_ifaddr *ia;
3577 panic("key_ismyaddr: NULL pointer is passed.\n");
3579 switch (sa->sa_family) {
3582 sin = (struct sockaddr_in *)sa;
3583 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
3584 if (sin->sin_family == ia->ia_addr.sin_family &&
3585 sin->sin_len == ia->ia_addr.sin_len &&
3586 sin->sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr)
3595 return key_ismyaddr6((struct sockaddr_in6 *)sa);
3604 * compare my own address for IPv6.
3607 * NOTE: derived ip6_input() in KAME. This is necessary to modify more.
3609 #include <netinet6/in6_var.h>
3612 key_ismyaddr6(struct sockaddr_in6 *sin6)
3614 struct in6_ifaddr *ia;
3615 struct in6_multi *in6m;
3617 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
3618 if (key_sockaddrcmp((struct sockaddr *)&sin6,
3619 (struct sockaddr *)&ia->ia_addr, 0) == 0)
3624 * XXX why do we care about multlicast here while we don't care
3625 * about IPv4 multicast??
3629 IN6_LOOKUP_MULTI(sin6->sin6_addr, ia->ia_ifp, in6m);
3634 /* loopback, just for safety */
3635 if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
3643 * compare two secasindex structure.
3644 * flag can specify to compare 2 saidxes.
3645 * compare two secasindex structure without both mode and reqid.
3646 * don't compare port.
3648 * saidx0: source, it can be in SAD.
3656 const struct secasindex *saidx0,
3657 const struct secasindex *saidx1,
3661 if (saidx0 == NULL && saidx1 == NULL)
3664 if (saidx0 == NULL || saidx1 == NULL)
3667 if (saidx0->proto != saidx1->proto)
3670 if (flag == CMP_EXACTLY) {
3671 if (saidx0->mode != saidx1->mode)
3673 if (saidx0->reqid != saidx1->reqid)
3675 if (bcmp(&saidx0->src, &saidx1->src, saidx0->src.sa.sa_len) != 0 ||
3676 bcmp(&saidx0->dst, &saidx1->dst, saidx0->dst.sa.sa_len) != 0)
3680 /* CMP_MODE_REQID, CMP_REQID, CMP_HEAD */
3681 if (flag == CMP_MODE_REQID
3682 ||flag == CMP_REQID) {
3684 * If reqid of SPD is non-zero, unique SA is required.
3685 * The result must be of same reqid in this case.
3687 if (saidx1->reqid != 0 && saidx0->reqid != saidx1->reqid)
3691 if (flag == CMP_MODE_REQID) {
3692 if (saidx0->mode != IPSEC_MODE_ANY
3693 && saidx0->mode != saidx1->mode)
3697 if (key_sockaddrcmp(&saidx0->src.sa, &saidx1->src.sa, 0) != 0) {
3700 if (key_sockaddrcmp(&saidx0->dst.sa, &saidx1->dst.sa, 0) != 0) {
3709 * compare two secindex structure exactly.
3711 * spidx0: source, it is often in SPD.
3712 * spidx1: object, it is often from PFKEY message.
3718 key_cmpspidx_exactly(
3719 struct secpolicyindex *spidx0,
3720 struct secpolicyindex *spidx1)
3723 if (spidx0 == NULL && spidx1 == NULL)
3726 if (spidx0 == NULL || spidx1 == NULL)
3729 if (spidx0->prefs != spidx1->prefs
3730 || spidx0->prefd != spidx1->prefd
3731 || spidx0->ul_proto != spidx1->ul_proto)
3734 return key_sockaddrcmp(&spidx0->src.sa, &spidx1->src.sa, 1) == 0 &&
3735 key_sockaddrcmp(&spidx0->dst.sa, &spidx1->dst.sa, 1) == 0;
3739 * compare two secindex structure with mask.
3741 * spidx0: source, it is often in SPD.
3742 * spidx1: object, it is often from IP header.
3748 key_cmpspidx_withmask(
3749 struct secpolicyindex *spidx0,
3750 struct secpolicyindex *spidx1)
3753 if (spidx0 == NULL && spidx1 == NULL)
3756 if (spidx0 == NULL || spidx1 == NULL)
3759 if (spidx0->src.sa.sa_family != spidx1->src.sa.sa_family ||
3760 spidx0->dst.sa.sa_family != spidx1->dst.sa.sa_family ||
3761 spidx0->src.sa.sa_len != spidx1->src.sa.sa_len ||
3762 spidx0->dst.sa.sa_len != spidx1->dst.sa.sa_len)
3765 /* if spidx.ul_proto == IPSEC_ULPROTO_ANY, ignore. */
3766 if (spidx0->ul_proto != (u_int16_t)IPSEC_ULPROTO_ANY
3767 && spidx0->ul_proto != spidx1->ul_proto)
3770 switch (spidx0->src.sa.sa_family) {
3772 if (spidx0->src.sin.sin_port != IPSEC_PORT_ANY
3773 && spidx0->src.sin.sin_port != spidx1->src.sin.sin_port)
3775 if (!key_bbcmp(&spidx0->src.sin.sin_addr,
3776 &spidx1->src.sin.sin_addr, spidx0->prefs))
3780 if (spidx0->src.sin6.sin6_port != IPSEC_PORT_ANY
3781 && spidx0->src.sin6.sin6_port != spidx1->src.sin6.sin6_port)
3784 * scope_id check. if sin6_scope_id is 0, we regard it
3785 * as a wildcard scope, which matches any scope zone ID.
3787 if (spidx0->src.sin6.sin6_scope_id &&
3788 spidx1->src.sin6.sin6_scope_id &&
3789 spidx0->src.sin6.sin6_scope_id != spidx1->src.sin6.sin6_scope_id)
3791 if (!key_bbcmp(&spidx0->src.sin6.sin6_addr,
3792 &spidx1->src.sin6.sin6_addr, spidx0->prefs))
3797 if (bcmp(&spidx0->src, &spidx1->src, spidx0->src.sa.sa_len) != 0)
3802 switch (spidx0->dst.sa.sa_family) {
3804 if (spidx0->dst.sin.sin_port != IPSEC_PORT_ANY
3805 && spidx0->dst.sin.sin_port != spidx1->dst.sin.sin_port)
3807 if (!key_bbcmp(&spidx0->dst.sin.sin_addr,
3808 &spidx1->dst.sin.sin_addr, spidx0->prefd))
3812 if (spidx0->dst.sin6.sin6_port != IPSEC_PORT_ANY
3813 && spidx0->dst.sin6.sin6_port != spidx1->dst.sin6.sin6_port)
3816 * scope_id check. if sin6_scope_id is 0, we regard it
3817 * as a wildcard scope, which matches any scope zone ID.
3819 if (spidx0->src.sin6.sin6_scope_id &&
3820 spidx1->src.sin6.sin6_scope_id &&
3821 spidx0->dst.sin6.sin6_scope_id != spidx1->dst.sin6.sin6_scope_id)
3823 if (!key_bbcmp(&spidx0->dst.sin6.sin6_addr,
3824 &spidx1->dst.sin6.sin6_addr, spidx0->prefd))
3829 if (bcmp(&spidx0->dst, &spidx1->dst, spidx0->dst.sa.sa_len) != 0)
3834 /* XXX Do we check other field ? e.g. flowinfo */
3839 /* returns 0 on match */
3842 const struct sockaddr *sa1,
3843 const struct sockaddr *sa2,
3849 #define satosin(s) ((const struct sockaddr_in *)s)
3853 #define satosin6(s) ((const struct sockaddr_in6 *)s)
3854 if (sa1->sa_family != sa2->sa_family || sa1->sa_len != sa2->sa_len)
3857 switch (sa1->sa_family) {
3859 if (sa1->sa_len != sizeof(struct sockaddr_in))
3861 if (satosin(sa1)->sin_addr.s_addr !=
3862 satosin(sa2)->sin_addr.s_addr) {
3865 if (port && satosin(sa1)->sin_port != satosin(sa2)->sin_port)
3869 if (sa1->sa_len != sizeof(struct sockaddr_in6))
3870 return 1; /*EINVAL*/
3871 if (satosin6(sa1)->sin6_scope_id !=
3872 satosin6(sa2)->sin6_scope_id) {
3875 if (!IN6_ARE_ADDR_EQUAL(&satosin6(sa1)->sin6_addr,
3876 &satosin6(sa2)->sin6_addr)) {
3880 satosin6(sa1)->sin6_port != satosin6(sa2)->sin6_port) {
3884 if (bcmp(sa1, sa2, sa1->sa_len) != 0)
3895 * compare two buffers with mask.
3899 * bits: Number of bits to compare
3905 key_bbcmp(const void *a1, const void *a2, u_int bits)
3907 const unsigned char *p1 = a1;
3908 const unsigned char *p2 = a2;
3910 /* XXX: This could be considerably faster if we compare a word
3911 * at a time, but it is complicated on LSB Endian machines */
3913 /* Handle null pointers */
3914 if (p1 == NULL || p2 == NULL)
3924 u_int8_t mask = ~((1<<(8-bits))-1);
3925 if ((*p1 & mask) != (*p2 & mask))
3928 return 1; /* Match! */
3933 * scanning SPD and SAD to check status for each entries,
3934 * and do to remove or to expire.
3935 * XXX: year 2038 problem may remain.
3938 key_timehandler(void *unused)
3942 time_t now = time_second;
3948 struct secpolicy *sp, *nextsp;
3950 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
3951 for (sp = LIST_FIRST(&sptree[dir]);
3955 nextsp = LIST_NEXT(sp, chain);
3957 if (sp->state == IPSEC_SPSTATE_DEAD) {
3962 if (sp->lifetime == 0 && sp->validtime == 0)
3965 /* the deletion will occur next time */
3966 if ((sp->lifetime && now - sp->created > sp->lifetime)
3967 || (sp->validtime && now - sp->lastused > sp->validtime)) {
3968 sp->state = IPSEC_SPSTATE_DEAD;
3978 struct secashead *sah, *nextsah;
3979 struct secasvar *sav, *nextsav;
3981 for (sah = LIST_FIRST(&sahtree);
3985 nextsah = LIST_NEXT(sah, chain);
3987 /* if sah has been dead, then delete it and process next sah. */
3988 if (sah->state == SADB_SASTATE_DEAD) {
3993 /* if LARVAL entry doesn't become MATURE, delete it. */
3994 for (sav = LIST_FIRST(&sah->savtree[SADB_SASTATE_LARVAL]);
3998 nextsav = LIST_NEXT(sav, chain);
4000 if (now - sav->created > key_larval_lifetime) {
4006 * check MATURE entry to start to send expire message
4009 for (sav = LIST_FIRST(&sah->savtree[SADB_SASTATE_MATURE]);
4013 nextsav = LIST_NEXT(sav, chain);
4015 /* we don't need to check. */
4016 if (sav->lft_s == NULL)
4020 if (sav->lft_c == NULL) {
4021 ipseclog((LOG_DEBUG,"key_timehandler: "
4022 "There is no CURRENT time, why?\n"));
4026 /* check SOFT lifetime */
4027 if (sav->lft_s->sadb_lifetime_addtime != 0
4028 && now - sav->created > sav->lft_s->sadb_lifetime_addtime) {
4030 * check SA to be used whether or not.
4031 * when SA hasn't been used, delete it.
4033 if (sav->lft_c->sadb_lifetime_usetime == 0) {
4034 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4037 key_sa_chgstate(sav, SADB_SASTATE_DYING);
4039 * XXX If we keep to send expire
4040 * message in the status of
4041 * DYING. Do remove below code.
4046 /* check SOFT lifetime by bytes */
4048 * XXX I don't know the way to delete this SA
4049 * when new SA is installed. Caution when it's
4050 * installed too big lifetime by time.
4052 else if (sav->lft_s->sadb_lifetime_bytes != 0
4053 && sav->lft_s->sadb_lifetime_bytes < sav->lft_c->sadb_lifetime_bytes) {
4055 key_sa_chgstate(sav, SADB_SASTATE_DYING);
4057 * XXX If we keep to send expire
4058 * message in the status of
4059 * DYING. Do remove below code.
4065 /* check DYING entry to change status to DEAD. */
4066 for (sav = LIST_FIRST(&sah->savtree[SADB_SASTATE_DYING]);
4070 nextsav = LIST_NEXT(sav, chain);
4072 /* we don't need to check. */
4073 if (sav->lft_h == NULL)
4077 if (sav->lft_c == NULL) {
4078 ipseclog((LOG_DEBUG, "key_timehandler: "
4079 "There is no CURRENT time, why?\n"));
4083 if (sav->lft_h->sadb_lifetime_addtime != 0
4084 && now - sav->created > sav->lft_h->sadb_lifetime_addtime) {
4085 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4088 #if 0 /* XXX Should we keep to send expire message until HARD lifetime ? */
4089 else if (sav->lft_s != NULL
4090 && sav->lft_s->sadb_lifetime_addtime != 0
4091 && now - sav->created > sav->lft_s->sadb_lifetime_addtime) {
4093 * XXX: should be checked to be
4094 * installed the valid SA.
4098 * If there is no SA then sending
4104 /* check HARD lifetime by bytes */
4105 else if (sav->lft_h->sadb_lifetime_bytes != 0
4106 && sav->lft_h->sadb_lifetime_bytes < sav->lft_c->sadb_lifetime_bytes) {
4107 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4112 /* delete entry in DEAD */
4113 for (sav = LIST_FIRST(&sah->savtree[SADB_SASTATE_DEAD]);
4117 nextsav = LIST_NEXT(sav, chain);
4120 if (sav->state != SADB_SASTATE_DEAD) {
4121 ipseclog((LOG_DEBUG, "key_timehandler: "
4122 "invalid sav->state "
4123 "(queue: %d SA: %d): "
4125 SADB_SASTATE_DEAD, sav->state));
4129 * do not call key_freesav() here.
4130 * sav should already be freed, and sav->refcnt
4131 * shows other references to sav
4132 * (such as from SPD).
4138 #ifndef IPSEC_NONBLOCK_ACQUIRE
4141 struct secacq *acq, *nextacq;
4143 for (acq = LIST_FIRST(&acqtree);
4147 nextacq = LIST_NEXT(acq, chain);
4149 if (now - acq->created > key_blockacq_lifetime
4150 && __LIST_CHAINED(acq)) {
4151 LIST_REMOVE(acq, chain);
4160 struct secspacq *acq, *nextacq;
4162 for (acq = LIST_FIRST(&spacqtree);
4166 nextacq = LIST_NEXT(acq, chain);
4168 if (now - acq->created > key_blockacq_lifetime
4169 && __LIST_CHAINED(acq)) {
4170 LIST_REMOVE(acq, chain);
4176 /* initialize random seed */
4177 if (key_tick_init_random++ > key_int_random) {
4178 key_tick_init_random = 0;
4182 #ifndef IPSEC_DEBUG2
4183 /* do exchange to tick time !! */
4184 callout_reset(&key_timehandler_ch, hz, key_timehandler, NULL);
4185 #endif /* IPSEC_DEBUG2 */
4192 * to initialize a seed for random()
4197 skrandom(time_second);
4205 key_randomfill(&value, sizeof(value));
4210 key_randomfill(void *p, size_t l)
4214 static int warn = 1;
4217 n = (size_t)read_random(p, (u_int)l);
4221 bcopy(&v, (u_int8_t *)p + n,
4222 l - n < sizeof(v) ? l - n : sizeof(v));
4226 printf("WARNING: pseudo-random number generator "
4227 "used for IPsec processing\n");
4234 * map SADB_SATYPE_* to IPPROTO_*.
4235 * if satype == SADB_SATYPE then satype is mapped to ~0.
4237 * 0: invalid satype.
4240 key_satype2proto(u_int8_t satype)
4243 case SADB_SATYPE_UNSPEC:
4244 return IPSEC_PROTO_ANY;
4245 case SADB_SATYPE_AH:
4247 case SADB_SATYPE_ESP:
4249 case SADB_X_SATYPE_IPCOMP:
4250 return IPPROTO_IPCOMP;
4258 * map IPPROTO_* to SADB_SATYPE_*
4260 * 0: invalid protocol type.
4263 key_proto2satype(u_int16_t proto)
4267 return SADB_SATYPE_AH;
4269 return SADB_SATYPE_ESP;
4270 case IPPROTO_IPCOMP:
4271 return SADB_X_SATYPE_IPCOMP;
4280 * SADB_GETSPI processing is to receive
4281 * <base, (SA2), src address, dst address, (SPI range)>
4282 * from the IKMPd, to assign a unique spi value, to hang on the INBOUND
4283 * tree with the status of LARVAL, and send
4284 * <base, SA(*), address(SD)>
4287 * IN: mhp: pointer to the pointer to each header.
4288 * OUT: NULL if fail.
4289 * other if success, return pointer to the message to send.
4292 key_getspi(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
4294 struct sadb_address *src0, *dst0;
4295 struct secasindex saidx;
4296 struct secashead *newsah;
4297 struct secasvar *newsav;
4305 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
4306 panic("key_getspi: NULL pointer is passed.\n");
4308 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
4309 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
4310 ipseclog((LOG_DEBUG, "key_getspi: invalid message is passed.\n"));
4311 return key_senderror(so, m, EINVAL);
4313 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
4314 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
4315 ipseclog((LOG_DEBUG, "key_getspi: invalid message is passed.\n"));
4316 return key_senderror(so, m, EINVAL);
4318 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
4319 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
4320 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
4322 mode = IPSEC_MODE_ANY;
4326 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
4327 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
4329 /* map satype to proto */
4330 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
4331 ipseclog((LOG_DEBUG, "key_getspi: invalid satype is passed.\n"));
4332 return key_senderror(so, m, EINVAL);
4335 /* make sure if port number is zero. */
4336 switch (((struct sockaddr *)(src0 + 1))->sa_family) {
4338 if (((struct sockaddr *)(src0 + 1))->sa_len !=
4339 sizeof(struct sockaddr_in))
4340 return key_senderror(so, m, EINVAL);
4341 ((struct sockaddr_in *)(src0 + 1))->sin_port = 0;
4344 if (((struct sockaddr *)(src0 + 1))->sa_len !=
4345 sizeof(struct sockaddr_in6))
4346 return key_senderror(so, m, EINVAL);
4347 ((struct sockaddr_in6 *)(src0 + 1))->sin6_port = 0;
4352 switch (((struct sockaddr *)(dst0 + 1))->sa_family) {
4354 if (((struct sockaddr *)(dst0 + 1))->sa_len !=
4355 sizeof(struct sockaddr_in))
4356 return key_senderror(so, m, EINVAL);
4357 ((struct sockaddr_in *)(dst0 + 1))->sin_port = 0;
4360 if (((struct sockaddr *)(dst0 + 1))->sa_len !=
4361 sizeof(struct sockaddr_in6))
4362 return key_senderror(so, m, EINVAL);
4363 ((struct sockaddr_in6 *)(dst0 + 1))->sin6_port = 0;
4369 /* XXX boundary check against sa_len */
4370 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
4372 /* SPI allocation */
4373 spi = key_do_getnewspi((struct sadb_spirange *)mhp->ext[SADB_EXT_SPIRANGE],
4376 return key_senderror(so, m, EINVAL);
4378 /* get a SA index */
4379 if ((newsah = key_getsah(&saidx)) == NULL) {
4380 /* create a new SA index */
4381 if ((newsah = key_newsah(&saidx)) == NULL) {
4382 ipseclog((LOG_DEBUG, "key_getspi: No more memory.\n"));
4383 return key_senderror(so, m, ENOBUFS);
4389 newsav = KEY_NEWSAV(m, mhp, newsah, &error);
4390 if (newsav == NULL) {
4391 /* XXX don't free new SA index allocated in above. */
4392 return key_senderror(so, m, error);
4396 newsav->spi = htonl(spi);
4398 #ifndef IPSEC_NONBLOCK_ACQUIRE
4399 /* delete the entry in acqtree */
4400 if (mhp->msg->sadb_msg_seq != 0) {
4402 if ((acq = key_getacqbyseq(mhp->msg->sadb_msg_seq)) != NULL) {
4403 /* reset counter in order to deletion by timehandler. */
4404 acq->created = time_second;
4411 struct mbuf *n, *nn;
4412 struct sadb_sa *m_sa;
4413 struct sadb_msg *newmsg;
4416 /* create new sadb_msg to reply. */
4417 len = PFKEY_ALIGN8(sizeof(struct sadb_msg)) +
4418 PFKEY_ALIGN8(sizeof(struct sadb_sa));
4420 return key_senderror(so, m, ENOBUFS);
4422 MGETHDR(n, MB_DONTWAIT, MT_DATA);
4424 MCLGET(n, MB_DONTWAIT);
4425 if ((n->m_flags & M_EXT) == 0) {
4431 return key_senderror(so, m, ENOBUFS);
4437 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
4438 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
4440 m_sa = (struct sadb_sa *)(mtod(n, caddr_t) + off);
4441 m_sa->sadb_sa_len = PFKEY_UNIT64(sizeof(struct sadb_sa));
4442 m_sa->sadb_sa_exttype = SADB_EXT_SA;
4443 m_sa->sadb_sa_spi = htonl(spi);
4444 off += PFKEY_ALIGN8(sizeof(struct sadb_sa));
4448 panic("length inconsistency in key_getspi");
4451 n->m_next = key_gather_mbuf(m, mhp, 0, 2, SADB_EXT_ADDRESS_SRC,
4452 SADB_EXT_ADDRESS_DST);
4455 return key_senderror(so, m, ENOBUFS);
4458 if (n->m_len < sizeof(struct sadb_msg)) {
4459 n = m_pullup(n, sizeof(struct sadb_msg));
4461 return key_sendup_mbuf(so, m, KEY_SENDUP_ONE);
4464 n->m_pkthdr.len = 0;
4465 for (nn = n; nn; nn = nn->m_next)
4466 n->m_pkthdr.len += nn->m_len;
4468 newmsg = mtod(n, struct sadb_msg *);
4469 newmsg->sadb_msg_seq = newsav->seq;
4470 newmsg->sadb_msg_errno = 0;
4471 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
4474 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
4479 * allocating new SPI
4480 * called by key_getspi().
4486 key_do_getnewspi(struct sadb_spirange *spirange, struct secasindex *saidx)
4490 int count = key_spi_trycnt;
4492 /* set spi range to allocate */
4493 if (spirange != NULL) {
4494 min = spirange->sadb_spirange_min;
4495 max = spirange->sadb_spirange_max;
4497 min = key_spi_minval;
4498 max = key_spi_maxval;
4500 /* IPCOMP needs 2-byte SPI */
4501 if (saidx->proto == IPPROTO_IPCOMP) {
4508 t = min; min = max; max = t;
4513 if (key_checkspidup(saidx, min) != NULL) {
4514 ipseclog((LOG_DEBUG, "key_do_getnewspi: SPI %u exists already.\n", min));
4518 count--; /* taking one cost. */
4526 /* when requesting to allocate spi ranged */
4528 /* generate pseudo-random SPI value ranged. */
4529 newspi = min + (key_random() % (max - min + 1));
4531 if (key_checkspidup(saidx, newspi) == NULL)
4535 if (count == 0 || newspi == 0) {
4536 ipseclog((LOG_DEBUG, "key_do_getnewspi: to allocate spi is failed.\n"));
4542 keystat.getspi_count =
4543 (keystat.getspi_count + key_spi_trycnt - count) / 2;
4549 * SADB_UPDATE processing
4551 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4552 * key(AE), (identity(SD),) (sensitivity)>
4553 * from the ikmpd, and update a secasvar entry whose status is SADB_SASTATE_LARVAL.
4555 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4556 * (identity(SD),) (sensitivity)>
4559 * m will always be freed.
4562 key_update(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
4564 struct sadb_sa *sa0;
4565 struct sadb_address *src0, *dst0;
4566 struct secasindex saidx;
4567 struct secashead *sah;
4568 struct secasvar *sav;
4575 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
4576 panic("key_update: NULL pointer is passed.\n");
4578 /* map satype to proto */
4579 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
4580 ipseclog((LOG_DEBUG, "key_update: invalid satype is passed.\n"));
4581 return key_senderror(so, m, EINVAL);
4584 if (mhp->ext[SADB_EXT_SA] == NULL ||
4585 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
4586 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
4587 (mhp->msg->sadb_msg_satype == SADB_SATYPE_ESP &&
4588 mhp->ext[SADB_EXT_KEY_ENCRYPT] == NULL) ||
4589 (mhp->msg->sadb_msg_satype == SADB_SATYPE_AH &&
4590 mhp->ext[SADB_EXT_KEY_AUTH] == NULL) ||
4591 (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL &&
4592 mhp->ext[SADB_EXT_LIFETIME_SOFT] == NULL) ||
4593 (mhp->ext[SADB_EXT_LIFETIME_HARD] == NULL &&
4594 mhp->ext[SADB_EXT_LIFETIME_SOFT] != NULL)) {
4595 ipseclog((LOG_DEBUG, "key_update: invalid message is passed.\n"));
4596 return key_senderror(so, m, EINVAL);
4598 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
4599 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
4600 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
4601 ipseclog((LOG_DEBUG, "key_update: invalid message is passed.\n"));
4602 return key_senderror(so, m, EINVAL);
4604 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
4605 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
4606 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
4608 mode = IPSEC_MODE_ANY;
4611 /* XXX boundary checking for other extensions */
4613 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
4614 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
4615 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
4617 /* XXX boundary check against sa_len */
4618 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
4620 /* get a SA header */
4621 if ((sah = key_getsah(&saidx)) == NULL) {
4622 ipseclog((LOG_DEBUG, "key_update: no SA index found.\n"));
4623 return key_senderror(so, m, ENOENT);
4626 /* set spidx if there */
4628 error = key_setident(sah, m, mhp);
4630 return key_senderror(so, m, error);
4632 /* find a SA with sequence number. */
4633 #ifdef IPSEC_DOSEQCHECK
4634 if (mhp->msg->sadb_msg_seq != 0
4635 && (sav = key_getsavbyseq(sah, mhp->msg->sadb_msg_seq)) == NULL) {
4636 ipseclog((LOG_DEBUG,
4637 "key_update: no larval SA with sequence %u exists.\n",
4638 mhp->msg->sadb_msg_seq));
4639 return key_senderror(so, m, ENOENT);
4642 if ((sav = key_getsavbyspi(sah, sa0->sadb_sa_spi)) == NULL) {
4643 ipseclog((LOG_DEBUG,
4644 "key_update: no such a SA found (spi:%u)\n",
4645 (u_int32_t)ntohl(sa0->sadb_sa_spi)));
4646 return key_senderror(so, m, EINVAL);
4650 /* validity check */
4651 if (sav->sah->saidx.proto != proto) {
4652 ipseclog((LOG_DEBUG,
4653 "key_update: protocol mismatched (DB=%u param=%u)\n",
4654 sav->sah->saidx.proto, proto));
4655 return key_senderror(so, m, EINVAL);
4657 #ifdef IPSEC_DOSEQCHECK
4658 if (sav->spi != sa0->sadb_sa_spi) {
4659 ipseclog((LOG_DEBUG,
4660 "key_update: SPI mismatched (DB:%u param:%u)\n",
4661 (u_int32_t)ntohl(sav->spi),
4662 (u_int32_t)ntohl(sa0->sadb_sa_spi)));
4663 return key_senderror(so, m, EINVAL);
4666 if (sav->pid != mhp->msg->sadb_msg_pid) {
4667 ipseclog((LOG_DEBUG,
4668 "key_update: pid mismatched (DB:%u param:%u)\n",
4669 sav->pid, mhp->msg->sadb_msg_pid));
4670 return key_senderror(so, m, EINVAL);
4673 /* copy sav values */
4674 error = key_setsaval(sav, m, mhp);
4677 return key_senderror(so, m, error);
4680 /* check SA values to be mature. */
4681 if ((mhp->msg->sadb_msg_errno = key_mature(sav)) != 0) {
4683 return key_senderror(so, m, 0);
4689 /* set msg buf from mhp */
4690 n = key_getmsgbuf_x1(m, mhp);
4692 ipseclog((LOG_DEBUG, "key_update: No more memory.\n"));
4693 return key_senderror(so, m, ENOBUFS);
4697 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
4702 * search SAD with sequence for a SA which state is SADB_SASTATE_LARVAL.
4703 * only called by key_update().
4706 * others : found, pointer to a SA.
4708 #ifdef IPSEC_DOSEQCHECK
4709 static struct secasvar *
4710 key_getsavbyseq(struct secashead *sah, u_int32_t seq)
4712 struct secasvar *sav;
4715 state = SADB_SASTATE_LARVAL;
4717 /* search SAD with sequence number ? */
4718 LIST_FOREACH(sav, &sah->savtree[state], chain) {
4720 KEY_CHKSASTATE(state, sav->state, "key_getsabyseq");
4722 if (sav->seq == seq) {
4724 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
4725 printf("DP key_getsavbyseq cause "
4726 "refcnt++:%d SA:%p\n",
4737 * SADB_ADD processing
4738 * add an entry to SA database, when received
4739 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4740 * key(AE), (identity(SD),) (sensitivity)>
4743 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4744 * (identity(SD),) (sensitivity)>
4747 * IGNORE identity and sensitivity messages.
4749 * m will always be freed.
4752 key_add(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
4754 struct sadb_sa *sa0;
4755 struct sadb_address *src0, *dst0;
4756 struct secasindex saidx;
4757 struct secashead *newsah;
4758 struct secasvar *newsav;
4765 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
4766 panic("key_add: NULL pointer is passed.\n");
4768 /* map satype to proto */
4769 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
4770 ipseclog((LOG_DEBUG, "key_add: invalid satype is passed.\n"));
4771 return key_senderror(so, m, EINVAL);
4774 if (mhp->ext[SADB_EXT_SA] == NULL ||
4775 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
4776 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
4777 (mhp->msg->sadb_msg_satype == SADB_SATYPE_ESP &&
4778 mhp->ext[SADB_EXT_KEY_ENCRYPT] == NULL) ||
4779 (mhp->msg->sadb_msg_satype == SADB_SATYPE_AH &&
4780 mhp->ext[SADB_EXT_KEY_AUTH] == NULL) ||
4781 (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL &&
4782 mhp->ext[SADB_EXT_LIFETIME_SOFT] == NULL) ||
4783 (mhp->ext[SADB_EXT_LIFETIME_HARD] == NULL &&
4784 mhp->ext[SADB_EXT_LIFETIME_SOFT] != NULL)) {
4785 ipseclog((LOG_DEBUG, "key_add: invalid message is passed.\n"));
4786 return key_senderror(so, m, EINVAL);
4788 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
4789 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
4790 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
4792 ipseclog((LOG_DEBUG, "key_add: invalid message is passed.\n"));
4793 return key_senderror(so, m, EINVAL);
4795 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
4796 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
4797 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
4799 mode = IPSEC_MODE_ANY;
4803 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
4804 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
4805 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
4807 /* XXX boundary check against sa_len */
4808 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
4810 /* get a SA header */
4811 if ((newsah = key_getsah(&saidx)) == NULL) {
4812 /* create a new SA header */
4813 if ((newsah = key_newsah(&saidx)) == NULL) {
4814 ipseclog((LOG_DEBUG, "key_add: No more memory.\n"));
4815 return key_senderror(so, m, ENOBUFS);
4819 /* set spidx if there */
4821 error = key_setident(newsah, m, mhp);
4823 return key_senderror(so, m, error);
4826 /* create new SA entry. */
4827 /* We can create new SA only if SPI is differenct. */
4828 if (key_getsavbyspi(newsah, sa0->sadb_sa_spi)) {
4829 ipseclog((LOG_DEBUG, "key_add: SA already exists.\n"));
4830 return key_senderror(so, m, EEXIST);
4832 newsav = KEY_NEWSAV(m, mhp, newsah, &error);
4833 if (newsav == NULL) {
4834 return key_senderror(so, m, error);
4837 /* check SA values to be mature. */
4838 if ((error = key_mature(newsav)) != 0) {
4839 KEY_FREESAV(&newsav);
4840 return key_senderror(so, m, error);
4844 * don't call key_freesav() here, as we would like to keep the SA
4845 * in the database on success.
4851 /* set msg buf from mhp */
4852 n = key_getmsgbuf_x1(m, mhp);
4854 ipseclog((LOG_DEBUG, "key_update: No more memory.\n"));
4855 return key_senderror(so, m, ENOBUFS);
4859 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
4865 key_setident(struct secashead *sah, struct mbuf *m,
4866 const struct sadb_msghdr *mhp)
4868 const struct sadb_ident *idsrc, *iddst;
4869 int idsrclen, iddstlen;
4872 if (sah == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
4873 panic("key_setident: NULL pointer is passed.\n");
4875 /* don't make buffer if not there */
4876 if (mhp->ext[SADB_EXT_IDENTITY_SRC] == NULL &&
4877 mhp->ext[SADB_EXT_IDENTITY_DST] == NULL) {
4883 if (mhp->ext[SADB_EXT_IDENTITY_SRC] == NULL ||
4884 mhp->ext[SADB_EXT_IDENTITY_DST] == NULL) {
4885 ipseclog((LOG_DEBUG, "key_setident: invalid identity.\n"));
4889 idsrc = (const struct sadb_ident *)mhp->ext[SADB_EXT_IDENTITY_SRC];
4890 iddst = (const struct sadb_ident *)mhp->ext[SADB_EXT_IDENTITY_DST];
4891 idsrclen = mhp->extlen[SADB_EXT_IDENTITY_SRC];
4892 iddstlen = mhp->extlen[SADB_EXT_IDENTITY_DST];
4894 /* validity check */
4895 if (idsrc->sadb_ident_type != iddst->sadb_ident_type) {
4896 ipseclog((LOG_DEBUG, "key_setident: ident type mismatch.\n"));
4900 switch (idsrc->sadb_ident_type) {
4901 case SADB_IDENTTYPE_PREFIX:
4902 case SADB_IDENTTYPE_FQDN:
4903 case SADB_IDENTTYPE_USERFQDN:
4905 /* XXX do nothing */
4911 /* make structure */
4912 KMALLOC(sah->idents, struct sadb_ident *, idsrclen);
4913 if (sah->idents == NULL) {
4914 ipseclog((LOG_DEBUG, "key_setident: No more memory.\n"));
4917 KMALLOC(sah->identd, struct sadb_ident *, iddstlen);
4918 if (sah->identd == NULL) {
4921 ipseclog((LOG_DEBUG, "key_setident: No more memory.\n"));
4924 bcopy(idsrc, sah->idents, idsrclen);
4925 bcopy(iddst, sah->identd, iddstlen);
4931 * m will not be freed on return.
4932 * it is caller's responsibility to free the result.
4934 static struct mbuf *
4935 key_getmsgbuf_x1(struct mbuf *m, const struct sadb_msghdr *mhp)
4940 if (m == NULL || mhp == NULL || mhp->msg == NULL)
4941 panic("key_getmsgbuf_x1: NULL pointer is passed.\n");
4943 /* create new sadb_msg to reply. */
4944 n = key_gather_mbuf(m, mhp, 1, 9, SADB_EXT_RESERVED,
4945 SADB_EXT_SA, SADB_X_EXT_SA2,
4946 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST,
4947 SADB_EXT_LIFETIME_HARD, SADB_EXT_LIFETIME_SOFT,
4948 SADB_EXT_IDENTITY_SRC, SADB_EXT_IDENTITY_DST);
4952 if (n->m_len < sizeof(struct sadb_msg)) {
4953 n = m_pullup(n, sizeof(struct sadb_msg));
4957 mtod(n, struct sadb_msg *)->sadb_msg_errno = 0;
4958 mtod(n, struct sadb_msg *)->sadb_msg_len =
4959 PFKEY_UNIT64(n->m_pkthdr.len);
4964 static int key_delete_all (struct socket *, struct mbuf *,
4965 const struct sadb_msghdr *, u_int16_t);
4968 * SADB_DELETE processing
4970 * <base, SA(*), address(SD)>
4971 * from the ikmpd, and set SADB_SASTATE_DEAD,
4973 * <base, SA(*), address(SD)>
4976 * m will always be freed.
4979 key_delete(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
4981 struct sadb_sa *sa0;
4982 struct sadb_address *src0, *dst0;
4983 struct secasindex saidx;
4984 struct secashead *sah;
4985 struct secasvar *sav = NULL;
4989 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
4990 panic("key_delete: NULL pointer is passed.\n");
4992 /* map satype to proto */
4993 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
4994 ipseclog((LOG_DEBUG, "key_delete: invalid satype is passed.\n"));
4995 return key_senderror(so, m, EINVAL);
4998 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
4999 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
5000 ipseclog((LOG_DEBUG, "key_delete: invalid message is passed.\n"));
5001 return key_senderror(so, m, EINVAL);
5004 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5005 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5006 ipseclog((LOG_DEBUG, "key_delete: invalid message is passed.\n"));
5007 return key_senderror(so, m, EINVAL);
5010 if (mhp->ext[SADB_EXT_SA] == NULL) {
5012 * Caller wants us to delete all non-LARVAL SAs
5013 * that match the src/dst. This is used during
5014 * IKE INITIAL-CONTACT.
5016 ipseclog((LOG_DEBUG, "key_delete: doing delete all.\n"));
5017 return key_delete_all(so, m, mhp, proto);
5018 } else if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa)) {
5019 ipseclog((LOG_DEBUG, "key_delete: invalid message is passed.\n"));
5020 return key_senderror(so, m, EINVAL);
5023 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5024 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
5025 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
5027 /* XXX boundary check against sa_len */
5028 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5030 /* get a SA header */
5031 LIST_FOREACH(sah, &sahtree, chain) {
5032 if (sah->state == SADB_SASTATE_DEAD)
5034 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5037 /* get a SA with SPI. */
5038 sav = key_getsavbyspi(sah, sa0->sadb_sa_spi);
5043 ipseclog((LOG_DEBUG, "key_delete: no SA found.\n"));
5044 return key_senderror(so, m, ENOENT);
5047 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
5052 struct sadb_msg *newmsg;
5054 /* create new sadb_msg to reply. */
5055 n = key_gather_mbuf(m, mhp, 1, 4, SADB_EXT_RESERVED,
5056 SADB_EXT_SA, SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
5058 return key_senderror(so, m, ENOBUFS);
5060 if (n->m_len < sizeof(struct sadb_msg)) {
5061 n = m_pullup(n, sizeof(struct sadb_msg));
5063 return key_senderror(so, m, ENOBUFS);
5065 newmsg = mtod(n, struct sadb_msg *);
5066 newmsg->sadb_msg_errno = 0;
5067 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
5070 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5075 * delete all SAs for src/dst. Called from key_delete().
5078 key_delete_all(struct socket *so, struct mbuf *m,
5079 const struct sadb_msghdr *mhp, u_int16_t proto)
5081 struct sadb_address *src0, *dst0;
5082 struct secasindex saidx;
5083 struct secashead *sah;
5084 struct secasvar *sav, *nextsav;
5085 u_int stateidx, state;
5087 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
5088 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
5090 /* XXX boundary check against sa_len */
5091 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5093 LIST_FOREACH(sah, &sahtree, chain) {
5094 if (sah->state == SADB_SASTATE_DEAD)
5096 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5099 /* Delete all non-LARVAL SAs. */
5101 stateidx < _ARRAYLEN(saorder_state_alive);
5103 state = saorder_state_alive[stateidx];
5104 if (state == SADB_SASTATE_LARVAL)
5106 for (sav = LIST_FIRST(&sah->savtree[state]);
5107 sav != NULL; sav = nextsav) {
5108 nextsav = LIST_NEXT(sav, chain);
5110 if (sav->state != state) {
5111 ipseclog((LOG_DEBUG, "key_delete_all: "
5112 "invalid sav->state "
5113 "(queue: %d SA: %d)\n",
5114 state, sav->state));
5118 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
5125 struct sadb_msg *newmsg;
5127 /* create new sadb_msg to reply. */
5128 n = key_gather_mbuf(m, mhp, 1, 3, SADB_EXT_RESERVED,
5129 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
5131 return key_senderror(so, m, ENOBUFS);
5133 if (n->m_len < sizeof(struct sadb_msg)) {
5134 n = m_pullup(n, sizeof(struct sadb_msg));
5136 return key_senderror(so, m, ENOBUFS);
5138 newmsg = mtod(n, struct sadb_msg *);
5139 newmsg->sadb_msg_errno = 0;
5140 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
5143 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5148 * SADB_GET processing
5150 * <base, SA(*), address(SD)>
5151 * from the ikmpd, and get a SP and a SA to respond,
5153 * <base, SA, (lifetime(HSC),) address(SD), (address(P),) key(AE),
5154 * (identity(SD),) (sensitivity)>
5157 * m will always be freed.
5160 key_get(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
5162 struct sadb_sa *sa0;
5163 struct sadb_address *src0, *dst0;
5164 struct secasindex saidx;
5165 struct secashead *sah;
5166 struct secasvar *sav = NULL;
5170 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
5171 panic("key_get: NULL pointer is passed.\n");
5173 /* map satype to proto */
5174 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5175 ipseclog((LOG_DEBUG, "key_get: invalid satype is passed.\n"));
5176 return key_senderror(so, m, EINVAL);
5179 if (mhp->ext[SADB_EXT_SA] == NULL ||
5180 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5181 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
5182 ipseclog((LOG_DEBUG, "key_get: invalid message is passed.\n"));
5183 return key_senderror(so, m, EINVAL);
5185 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
5186 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5187 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5188 ipseclog((LOG_DEBUG, "key_get: invalid message is passed.\n"));
5189 return key_senderror(so, m, EINVAL);
5192 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5193 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
5194 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
5196 /* XXX boundary check against sa_len */
5197 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5199 /* get a SA header */
5200 LIST_FOREACH(sah, &sahtree, chain) {
5201 if (sah->state == SADB_SASTATE_DEAD)
5203 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5206 /* get a SA with SPI. */
5207 sav = key_getsavbyspi(sah, sa0->sadb_sa_spi);
5212 ipseclog((LOG_DEBUG, "key_get: no SA found.\n"));
5213 return key_senderror(so, m, ENOENT);
5220 /* map proto to satype */
5221 if ((satype = key_proto2satype(sah->saidx.proto)) == 0) {
5222 ipseclog((LOG_DEBUG, "key_get: there was invalid proto in SAD.\n"));
5223 return key_senderror(so, m, EINVAL);
5226 /* create new sadb_msg to reply. */
5227 n = key_setdumpsa(sav, SADB_GET, satype, mhp->msg->sadb_msg_seq,
5228 mhp->msg->sadb_msg_pid);
5230 return key_senderror(so, m, ENOBUFS);
5233 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
5237 /* XXX make it sysctl-configurable? */
5239 key_getcomb_setlifetime(struct sadb_comb *comb)
5242 comb->sadb_comb_soft_allocations = 1;
5243 comb->sadb_comb_hard_allocations = 1;
5244 comb->sadb_comb_soft_bytes = 0;
5245 comb->sadb_comb_hard_bytes = 0;
5246 comb->sadb_comb_hard_addtime = 86400; /* 1 day */
5247 comb->sadb_comb_soft_addtime = comb->sadb_comb_soft_addtime * 80 / 100;
5248 comb->sadb_comb_soft_usetime = 28800; /* 8 hours */
5249 comb->sadb_comb_hard_usetime = comb->sadb_comb_hard_usetime * 80 / 100;
5253 * XXX reorder combinations by preference
5254 * XXX no idea if the user wants ESP authentication or not
5256 static struct mbuf *
5257 key_getcomb_esp(void)
5259 struct sadb_comb *comb;
5260 struct enc_xform *algo;
5261 struct mbuf *result = NULL, *m, *n;
5265 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
5268 for (i = 1; i <= SADB_EALG_MAX; i++) {
5269 algo = esp_algorithm_lookup(i);
5273 /* discard algorithms with key size smaller than system min */
5274 if (_BITS(algo->maxkey) < ipsec_esp_keymin)
5276 if (_BITS(algo->minkey) < ipsec_esp_keymin)
5277 encmin = ipsec_esp_keymin;
5279 encmin = _BITS(algo->minkey);
5282 m = key_getcomb_ah();
5285 ("key_getcomb_esp: l=%u > MLEN=%lu",
5287 MGET(m, MB_DONTWAIT, MT_DATA);
5292 bzero(mtod(m, caddr_t), m->m_len);
5299 for (n = m; n; n = n->m_next)
5301 KASSERT((totlen % l) == 0,
5302 ("key_getcomb_esp: totlen=%u, l=%u", totlen, l));
5304 for (off = 0; off < totlen; off += l) {
5305 n = m_pulldown(m, off, l, &o);
5307 /* m is already freed */
5310 comb = (struct sadb_comb *)(mtod(n, caddr_t) + o);
5311 bzero(comb, sizeof(*comb));
5312 key_getcomb_setlifetime(comb);
5313 comb->sadb_comb_encrypt = i;
5314 comb->sadb_comb_encrypt_minbits = encmin;
5315 comb->sadb_comb_encrypt_maxbits = _BITS(algo->maxkey);
5334 const struct auth_hash *ah,
5339 *min = *max = ah->keysize;
5340 if (ah->keysize == 0) {
5342 * Transform takes arbitrary key size but algorithm
5343 * key size is restricted. Enforce this here.
5346 case SADB_X_AALG_MD5: *min = *max = 16; break;
5347 case SADB_X_AALG_SHA: *min = *max = 20; break;
5348 case SADB_X_AALG_NULL: *min = 1; *max = 256; break;
5350 DPRINTF(("key_getsizes_ah: unknown AH algorithm %u\n",
5358 * XXX reorder combinations by preference
5360 static struct mbuf *
5361 key_getcomb_ah(void)
5363 struct sadb_comb *comb;
5364 struct auth_hash *algo;
5366 u_int16_t minkeysize, maxkeysize;
5368 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
5371 for (i = 1; i <= SADB_AALG_MAX; i++) {
5373 /* we prefer HMAC algorithms, not old algorithms */
5374 if (i != SADB_AALG_SHA1HMAC && i != SADB_AALG_MD5HMAC)
5377 algo = ah_algorithm_lookup(i);
5380 key_getsizes_ah(algo, i, &minkeysize, &maxkeysize);
5381 /* discard algorithms with key size smaller than system min */
5382 if (_BITS(minkeysize) < ipsec_ah_keymin)
5387 ("key_getcomb_ah: l=%u > MLEN=%lu",
5389 MGET(m, MB_DONTWAIT, MT_DATA);
5396 M_PREPEND(m, l, MB_DONTWAIT);
5400 comb = mtod(m, struct sadb_comb *);
5401 bzero(comb, sizeof(*comb));
5402 key_getcomb_setlifetime(comb);
5403 comb->sadb_comb_auth = i;
5404 comb->sadb_comb_auth_minbits = _BITS(minkeysize);
5405 comb->sadb_comb_auth_maxbits = _BITS(maxkeysize);
5412 * not really an official behavior. discussed in pf_key@inner.net in Sep2000.
5413 * XXX reorder combinations by preference
5415 static struct mbuf *
5416 key_getcomb_ipcomp(void)
5418 struct sadb_comb *comb;
5419 struct comp_algo *algo;
5422 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
5425 for (i = 1; i <= SADB_X_CALG_MAX; i++) {
5426 algo = ipcomp_algorithm_lookup(i);
5432 ("key_getcomb_ipcomp: l=%u > MLEN=%lu",
5434 MGET(m, MB_DONTWAIT, MT_DATA);
5441 M_PREPEND(m, l, MB_DONTWAIT);
5445 comb = mtod(m, struct sadb_comb *);
5446 bzero(comb, sizeof(*comb));
5447 key_getcomb_setlifetime(comb);
5448 comb->sadb_comb_encrypt = i;
5449 /* what should we set into sadb_comb_*_{min,max}bits? */
5456 * XXX no way to pass mode (transport/tunnel) to userland
5457 * XXX replay checking?
5458 * XXX sysctl interface to ipsec_{ah,esp}_keymin
5460 static struct mbuf *
5461 key_getprop(const struct secasindex *saidx)
5463 struct sadb_prop *prop;
5465 const int l = PFKEY_ALIGN8(sizeof(struct sadb_prop));
5468 switch (saidx->proto) {
5470 m = key_getcomb_esp();
5473 m = key_getcomb_ah();
5475 case IPPROTO_IPCOMP:
5476 m = key_getcomb_ipcomp();
5484 M_PREPEND(m, l, MB_DONTWAIT);
5489 for (n = m; n; n = n->m_next)
5492 prop = mtod(m, struct sadb_prop *);
5493 bzero(prop, sizeof(*prop));
5494 prop->sadb_prop_len = PFKEY_UNIT64(totlen);
5495 prop->sadb_prop_exttype = SADB_EXT_PROPOSAL;
5496 prop->sadb_prop_replay = 32; /* XXX */
5502 * SADB_ACQUIRE processing called by key_checkrequest() and key_acquire2().
5504 * <base, SA, address(SD), (address(P)), x_policy,
5505 * (identity(SD),) (sensitivity,) proposal>
5506 * to KMD, and expect to receive
5507 * <base> with SADB_ACQUIRE if error occured,
5509 * <base, src address, dst address, (SPI range)> with SADB_GETSPI
5510 * from KMD by PF_KEY.
5512 * XXX x_policy is outside of RFC2367 (KAME extension).
5513 * XXX sensitivity is not supported.
5514 * XXX for ipcomp, RFC2367 does not define how to fill in proposal.
5515 * see comment for key_getcomb_ipcomp().
5519 * others: error number
5522 key_acquire(const struct secasindex *saidx, struct secpolicy *sp)
5524 struct mbuf *result = NULL, *m;
5525 #ifndef IPSEC_NONBLOCK_ACQUIRE
5526 struct secacq *newacq;
5533 KASSERT(saidx != NULL, ("key_acquire: null saidx"));
5534 satype = key_proto2satype(saidx->proto);
5535 KASSERT(satype != 0,
5536 ("key_acquire: null satype, protocol %u", saidx->proto));
5538 #ifndef IPSEC_NONBLOCK_ACQUIRE
5540 * We never do anything about acquirng SA. There is anather
5541 * solution that kernel blocks to send SADB_ACQUIRE message until
5542 * getting something message from IKEd. In later case, to be
5543 * managed with ACQUIRING list.
5545 /* Get an entry to check whether sending message or not. */
5546 if ((newacq = key_getacq(saidx)) != NULL) {
5547 if (key_blockacq_count < newacq->count) {
5548 /* reset counter and do send message. */
5551 /* increment counter and do nothing. */
5556 /* make new entry for blocking to send SADB_ACQUIRE. */
5557 if ((newacq = key_newacq(saidx)) == NULL)
5560 /* add to acqtree */
5561 LIST_INSERT_HEAD(&acqtree, newacq, chain);
5566 #ifndef IPSEC_NONBLOCK_ACQUIRE
5569 seq = (acq_seq = (acq_seq == ~0 ? 1 : ++acq_seq));
5571 m = key_setsadbmsg(SADB_ACQUIRE, 0, satype, seq, 0, 0);
5578 /* set sadb_address for saidx's. */
5579 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
5580 &saidx->src.sa, FULLMASK, IPSEC_ULPROTO_ANY);
5587 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
5588 &saidx->dst.sa, FULLMASK, IPSEC_ULPROTO_ANY);
5595 /* XXX proxy address (optional) */
5597 /* set sadb_x_policy */
5599 m = key_setsadbxpolicy(sp->policy, sp->spidx.dir, sp->id);
5607 /* XXX identity (optional) */
5609 if (idexttype && fqdn) {
5610 /* create identity extension (FQDN) */
5611 struct sadb_ident *id;
5614 fqdnlen = strlen(fqdn) + 1; /* +1 for terminating-NUL */
5615 id = (struct sadb_ident *)p;
5616 bzero(id, sizeof(*id) + PFKEY_ALIGN8(fqdnlen));
5617 id->sadb_ident_len = PFKEY_UNIT64(sizeof(*id) + PFKEY_ALIGN8(fqdnlen));
5618 id->sadb_ident_exttype = idexttype;
5619 id->sadb_ident_type = SADB_IDENTTYPE_FQDN;
5620 bcopy(fqdn, id + 1, fqdnlen);
5621 p += sizeof(struct sadb_ident) + PFKEY_ALIGN8(fqdnlen);
5625 /* create identity extension (USERFQDN) */
5626 struct sadb_ident *id;
5630 /* +1 for terminating-NUL */
5631 userfqdnlen = strlen(userfqdn) + 1;
5634 id = (struct sadb_ident *)p;
5635 bzero(id, sizeof(*id) + PFKEY_ALIGN8(userfqdnlen));
5636 id->sadb_ident_len = PFKEY_UNIT64(sizeof(*id) + PFKEY_ALIGN8(userfqdnlen));
5637 id->sadb_ident_exttype = idexttype;
5638 id->sadb_ident_type = SADB_IDENTTYPE_USERFQDN;
5639 /* XXX is it correct? */
5640 if (curproc && curproc->p_cred)
5641 id->sadb_ident_id = curproc->p_cred->p_ruid;
5642 if (userfqdn && userfqdnlen)
5643 bcopy(userfqdn, id + 1, userfqdnlen);
5644 p += sizeof(struct sadb_ident) + PFKEY_ALIGN8(userfqdnlen);
5648 /* XXX sensitivity (optional) */
5650 /* create proposal/combination extension */
5651 m = key_getprop(saidx);
5654 * spec conformant: always attach proposal/combination extension,
5655 * the problem is that we have no way to attach it for ipcomp,
5656 * due to the way sadb_comb is declared in RFC2367.
5665 * outside of spec; make proposal/combination extension optional.
5671 if ((result->m_flags & M_PKTHDR) == 0) {
5676 if (result->m_len < sizeof(struct sadb_msg)) {
5677 result = m_pullup(result, sizeof(struct sadb_msg));
5678 if (result == NULL) {
5684 result->m_pkthdr.len = 0;
5685 for (m = result; m; m = m->m_next)
5686 result->m_pkthdr.len += m->m_len;
5688 mtod(result, struct sadb_msg *)->sadb_msg_len =
5689 PFKEY_UNIT64(result->m_pkthdr.len);
5691 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
5699 #ifndef IPSEC_NONBLOCK_ACQUIRE
5700 static struct secacq *
5701 key_newacq(const struct secasindex *saidx)
5703 struct secacq *newacq;
5706 KMALLOC(newacq, struct secacq *, sizeof(struct secacq));
5707 if (newacq == NULL) {
5708 ipseclog((LOG_DEBUG, "key_newacq: No more memory.\n"));
5711 bzero(newacq, sizeof(*newacq));
5714 bcopy(saidx, &newacq->saidx, sizeof(newacq->saidx));
5715 newacq->seq = (acq_seq == ~0 ? 1 : ++acq_seq);
5716 newacq->created = time_second;
5722 static struct secacq *
5723 key_getacq(const struct secasindex *saidx)
5727 LIST_FOREACH(acq, &acqtree, chain) {
5728 if (key_cmpsaidx(saidx, &acq->saidx, CMP_EXACTLY))
5735 static struct secacq *
5736 key_getacqbyseq(u_int32_t seq)
5740 LIST_FOREACH(acq, &acqtree, chain) {
5741 if (acq->seq == seq)
5749 static struct secspacq *
5750 key_newspacq(struct secpolicyindex *spidx)
5752 struct secspacq *acq;
5755 KMALLOC(acq, struct secspacq *, sizeof(struct secspacq));
5757 ipseclog((LOG_DEBUG, "key_newspacq: No more memory.\n"));
5760 bzero(acq, sizeof(*acq));
5763 bcopy(spidx, &acq->spidx, sizeof(acq->spidx));
5764 acq->created = time_second;
5770 static struct secspacq *
5771 key_getspacq(struct secpolicyindex *spidx)
5773 struct secspacq *acq;
5775 LIST_FOREACH(acq, &spacqtree, chain) {
5776 if (key_cmpspidx_exactly(spidx, &acq->spidx))
5784 * SADB_ACQUIRE processing,
5785 * in first situation, is receiving
5787 * from the ikmpd, and clear sequence of its secasvar entry.
5789 * In second situation, is receiving
5790 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
5791 * from a user land process, and return
5792 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
5795 * m will always be freed.
5798 key_acquire2(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
5800 const struct sadb_address *src0, *dst0;
5801 struct secasindex saidx;
5802 struct secashead *sah;
5807 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
5808 panic("key_acquire2: NULL pointer is passed.\n");
5811 * Error message from KMd.
5812 * We assume that if error was occured in IKEd, the length of PFKEY
5813 * message is equal to the size of sadb_msg structure.
5814 * We do not raise error even if error occured in this function.
5816 if (mhp->msg->sadb_msg_len == PFKEY_UNIT64(sizeof(struct sadb_msg))) {
5817 #ifndef IPSEC_NONBLOCK_ACQUIRE
5820 /* check sequence number */
5821 if (mhp->msg->sadb_msg_seq == 0) {
5822 ipseclog((LOG_DEBUG, "key_acquire2: must specify sequence number.\n"));
5827 if ((acq = key_getacqbyseq(mhp->msg->sadb_msg_seq)) == NULL) {
5829 * the specified larval SA is already gone, or we got
5830 * a bogus sequence number. we can silently ignore it.
5836 /* reset acq counter in order to deletion by timehander. */
5837 acq->created = time_second;
5845 * This message is from user land.
5848 /* map satype to proto */
5849 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5850 ipseclog((LOG_DEBUG, "key_acquire2: invalid satype is passed.\n"));
5851 return key_senderror(so, m, EINVAL);
5854 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5855 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
5856 mhp->ext[SADB_EXT_PROPOSAL] == NULL) {
5858 ipseclog((LOG_DEBUG, "key_acquire2: invalid message is passed.\n"));
5859 return key_senderror(so, m, EINVAL);
5861 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5862 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
5863 mhp->extlen[SADB_EXT_PROPOSAL] < sizeof(struct sadb_prop)) {
5865 ipseclog((LOG_DEBUG, "key_acquire2: invalid message is passed.\n"));
5866 return key_senderror(so, m, EINVAL);
5869 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
5870 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
5872 /* XXX boundary check against sa_len */
5873 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5875 /* get a SA index */
5876 LIST_FOREACH(sah, &sahtree, chain) {
5877 if (sah->state == SADB_SASTATE_DEAD)
5879 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_MODE_REQID))
5883 ipseclog((LOG_DEBUG, "key_acquire2: a SA exists already.\n"));
5884 return key_senderror(so, m, EEXIST);
5887 error = key_acquire(&saidx, NULL);
5889 ipseclog((LOG_DEBUG, "key_acquire2: error %d returned "
5890 "from key_acquire.\n", mhp->msg->sadb_msg_errno));
5891 return key_senderror(so, m, error);
5894 return key_sendup_mbuf(so, m, KEY_SENDUP_REGISTERED);
5898 * SADB_REGISTER processing.
5899 * If SATYPE_UNSPEC has been passed as satype, only return sabd_supported.
5902 * from the ikmpd, and register a socket to send PF_KEY messages,
5906 * If socket is detached, must free from regnode.
5908 * m will always be freed.
5911 key_register(struct socket *so, struct mbuf *m,
5912 const struct sadb_msghdr *mhp)
5914 struct secreg *reg, *newreg = 0;
5917 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
5918 panic("key_register: NULL pointer is passed.\n");
5920 /* check for invalid register message */
5921 if (mhp->msg->sadb_msg_satype >= sizeof(regtree)/sizeof(regtree[0]))
5922 return key_senderror(so, m, EINVAL);
5924 /* When SATYPE_UNSPEC is specified, only return sabd_supported. */
5925 if (mhp->msg->sadb_msg_satype == SADB_SATYPE_UNSPEC)
5928 /* check whether existing or not */
5929 LIST_FOREACH(reg, ®tree[mhp->msg->sadb_msg_satype], chain) {
5930 if (reg->so == so) {
5931 ipseclog((LOG_DEBUG, "key_register: socket exists already.\n"));
5932 return key_senderror(so, m, EEXIST);
5936 /* create regnode */
5937 KMALLOC(newreg, struct secreg *, sizeof(*newreg));
5938 if (newreg == NULL) {
5939 ipseclog((LOG_DEBUG, "key_register: No more memory.\n"));
5940 return key_senderror(so, m, ENOBUFS);
5942 bzero((caddr_t)newreg, sizeof(*newreg));
5945 ((struct keycb *)sotorawcb(so))->kp_registered++;
5947 /* add regnode to regtree. */
5948 LIST_INSERT_HEAD(®tree[mhp->msg->sadb_msg_satype], newreg, chain);
5953 struct sadb_msg *newmsg;
5954 struct sadb_supported *sup;
5955 u_int len, alen, elen;
5958 struct sadb_alg *alg;
5960 /* create new sadb_msg to reply. */
5962 for (i = 1; i <= SADB_AALG_MAX; i++) {
5963 if (ah_algorithm_lookup(i))
5964 alen += sizeof(struct sadb_alg);
5967 alen += sizeof(struct sadb_supported);
5969 for (i = 1; i <= SADB_EALG_MAX; i++) {
5970 if (esp_algorithm_lookup(i))
5971 elen += sizeof(struct sadb_alg);
5974 elen += sizeof(struct sadb_supported);
5976 len = sizeof(struct sadb_msg) + alen + elen;
5979 return key_senderror(so, m, ENOBUFS);
5981 MGETHDR(n, MB_DONTWAIT, MT_DATA);
5983 MCLGET(n, MB_DONTWAIT);
5984 if ((n->m_flags & M_EXT) == 0) {
5990 return key_senderror(so, m, ENOBUFS);
5992 n->m_pkthdr.len = n->m_len = len;
5996 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
5997 newmsg = mtod(n, struct sadb_msg *);
5998 newmsg->sadb_msg_errno = 0;
5999 newmsg->sadb_msg_len = PFKEY_UNIT64(len);
6000 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
6002 /* for authentication algorithm */
6004 sup = (struct sadb_supported *)(mtod(n, caddr_t) + off);
6005 sup->sadb_supported_len = PFKEY_UNIT64(alen);
6006 sup->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
6007 off += PFKEY_ALIGN8(sizeof(*sup));
6009 for (i = 1; i <= SADB_AALG_MAX; i++) {
6010 struct auth_hash *aalgo;
6011 u_int16_t minkeysize, maxkeysize;
6013 aalgo = ah_algorithm_lookup(i);
6016 alg = (struct sadb_alg *)(mtod(n, caddr_t) + off);
6017 alg->sadb_alg_id = i;
6018 alg->sadb_alg_ivlen = 0;
6019 key_getsizes_ah(aalgo, i, &minkeysize, &maxkeysize);
6020 alg->sadb_alg_minbits = _BITS(minkeysize);
6021 alg->sadb_alg_maxbits = _BITS(maxkeysize);
6022 off += PFKEY_ALIGN8(sizeof(*alg));
6026 /* for encryption algorithm */
6028 sup = (struct sadb_supported *)(mtod(n, caddr_t) + off);
6029 sup->sadb_supported_len = PFKEY_UNIT64(elen);
6030 sup->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT;
6031 off += PFKEY_ALIGN8(sizeof(*sup));
6033 for (i = 1; i <= SADB_EALG_MAX; i++) {
6034 struct enc_xform *ealgo;
6036 ealgo = esp_algorithm_lookup(i);
6039 alg = (struct sadb_alg *)(mtod(n, caddr_t) + off);
6040 alg->sadb_alg_id = i;
6041 alg->sadb_alg_ivlen = ealgo->blocksize;
6042 alg->sadb_alg_minbits = _BITS(ealgo->minkey);
6043 alg->sadb_alg_maxbits = _BITS(ealgo->maxkey);
6044 off += PFKEY_ALIGN8(sizeof(struct sadb_alg));
6050 panic("length assumption failed in key_register");
6054 return key_sendup_mbuf(so, n, KEY_SENDUP_REGISTERED);
6059 * free secreg entry registered.
6060 * XXX: I want to do free a socket marked done SADB_RESIGER to socket.
6063 key_freereg(struct socket *so)
6070 panic("key_freereg: NULL pointer is passed.\n");
6073 * check whether existing or not.
6074 * check all type of SA, because there is a potential that
6075 * one socket is registered to multiple type of SA.
6077 for (i = 0; i <= SADB_SATYPE_MAX; i++) {
6078 LIST_FOREACH(reg, ®tree[i], chain) {
6080 && __LIST_CHAINED(reg)) {
6081 LIST_REMOVE(reg, chain);
6092 * SADB_EXPIRE processing
6094 * <base, SA, SA2, lifetime(C and one of HS), address(SD)>
6096 * NOTE: We send only soft lifetime extension.
6099 * others : error number
6102 key_expire(struct secasvar *sav)
6106 struct mbuf *result = NULL, *m;
6109 struct sadb_lifetime *lt;
6111 /* XXX: Why do we lock ? */
6116 panic("key_expire: NULL pointer is passed.\n");
6117 if (sav->sah == NULL)
6118 panic("key_expire: Why was SA index in SA NULL.\n");
6119 if ((satype = key_proto2satype(sav->sah->saidx.proto)) == 0)
6120 panic("key_expire: invalid proto is passed.\n");
6122 /* set msg header */
6123 m = key_setsadbmsg(SADB_EXPIRE, 0, satype, sav->seq, 0, sav->refcnt);
6130 /* create SA extension */
6131 m = key_setsadbsa(sav);
6138 /* create SA extension */
6139 m = key_setsadbxsa2(sav->sah->saidx.mode,
6140 sav->replay ? sav->replay->count : 0,
6141 sav->sah->saidx.reqid);
6148 /* create lifetime extension (current and soft) */
6149 len = PFKEY_ALIGN8(sizeof(*lt)) * 2;
6150 m = key_alloc_mbuf(len);
6151 if (!m || m->m_next) { /*XXX*/
6157 bzero(mtod(m, caddr_t), len);
6158 lt = mtod(m, struct sadb_lifetime *);
6159 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
6160 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
6161 lt->sadb_lifetime_allocations = sav->lft_c->sadb_lifetime_allocations;
6162 lt->sadb_lifetime_bytes = sav->lft_c->sadb_lifetime_bytes;
6163 lt->sadb_lifetime_addtime = sav->lft_c->sadb_lifetime_addtime;
6164 lt->sadb_lifetime_usetime = sav->lft_c->sadb_lifetime_usetime;
6165 lt = (struct sadb_lifetime *)(mtod(m, caddr_t) + len / 2);
6166 bcopy(sav->lft_s, lt, sizeof(*lt));
6169 /* set sadb_address for source */
6170 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
6171 &sav->sah->saidx.src.sa,
6172 FULLMASK, IPSEC_ULPROTO_ANY);
6179 /* set sadb_address for destination */
6180 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
6181 &sav->sah->saidx.dst.sa,
6182 FULLMASK, IPSEC_ULPROTO_ANY);
6189 if ((result->m_flags & M_PKTHDR) == 0) {
6194 if (result->m_len < sizeof(struct sadb_msg)) {
6195 result = m_pullup(result, sizeof(struct sadb_msg));
6196 if (result == NULL) {
6202 result->m_pkthdr.len = 0;
6203 for (m = result; m; m = m->m_next)
6204 result->m_pkthdr.len += m->m_len;
6206 mtod(result, struct sadb_msg *)->sadb_msg_len =
6207 PFKEY_UNIT64(result->m_pkthdr.len);
6210 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
6220 * SADB_FLUSH processing
6223 * from the ikmpd, and free all entries in secastree.
6227 * NOTE: to do is only marking SADB_SASTATE_DEAD.
6229 * m will always be freed.
6232 key_flush(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
6234 struct sadb_msg *newmsg;
6235 struct secashead *sah, *nextsah;
6236 struct secasvar *sav, *nextsav;
6242 if (so == NULL || mhp == NULL || mhp->msg == NULL)
6243 panic("key_flush: NULL pointer is passed.\n");
6245 /* map satype to proto */
6246 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
6247 ipseclog((LOG_DEBUG, "key_flush: invalid satype is passed.\n"));
6248 return key_senderror(so, m, EINVAL);
6251 /* no SATYPE specified, i.e. flushing all SA. */
6252 for (sah = LIST_FIRST(&sahtree);
6255 nextsah = LIST_NEXT(sah, chain);
6257 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
6258 && proto != sah->saidx.proto)
6262 stateidx < _ARRAYLEN(saorder_state_alive);
6264 state = saorder_state_any[stateidx];
6265 for (sav = LIST_FIRST(&sah->savtree[state]);
6269 nextsav = LIST_NEXT(sav, chain);
6271 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
6276 sah->state = SADB_SASTATE_DEAD;
6279 if (m->m_len < sizeof(struct sadb_msg) ||
6280 sizeof(struct sadb_msg) > m->m_len + M_TRAILINGSPACE(m)) {
6281 ipseclog((LOG_DEBUG, "key_flush: No more memory.\n"));
6282 return key_senderror(so, m, ENOBUFS);
6288 m->m_pkthdr.len = m->m_len = sizeof(struct sadb_msg);
6289 newmsg = mtod(m, struct sadb_msg *);
6290 newmsg->sadb_msg_errno = 0;
6291 newmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
6293 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
6297 * SADB_DUMP processing
6298 * dump all entries including status of DEAD in SAD.
6301 * from the ikmpd, and dump all secasvar leaves
6306 * m will always be freed.
6309 key_dump(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
6311 struct secashead *sah;
6312 struct secasvar *sav;
6318 struct sadb_msg *newmsg;
6322 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
6323 panic("key_dump: NULL pointer is passed.\n");
6325 /* map satype to proto */
6326 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
6327 ipseclog((LOG_DEBUG, "key_dump: invalid satype is passed.\n"));
6328 return key_senderror(so, m, EINVAL);
6331 /* count sav entries to be sent to the userland. */
6333 LIST_FOREACH(sah, &sahtree, chain) {
6334 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
6335 && proto != sah->saidx.proto)
6339 stateidx < _ARRAYLEN(saorder_state_any);
6341 state = saorder_state_any[stateidx];
6342 LIST_FOREACH(sav, &sah->savtree[state], chain) {
6349 return key_senderror(so, m, ENOENT);
6351 /* send this to the userland, one at a time. */
6353 LIST_FOREACH(sah, &sahtree, chain) {
6354 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
6355 && proto != sah->saidx.proto)
6358 /* map proto to satype */
6359 if ((satype = key_proto2satype(sah->saidx.proto)) == 0) {
6360 ipseclog((LOG_DEBUG, "key_dump: there was invalid proto in SAD.\n"));
6361 return key_senderror(so, m, EINVAL);
6365 stateidx < _ARRAYLEN(saorder_state_any);
6367 state = saorder_state_any[stateidx];
6368 LIST_FOREACH(sav, &sah->savtree[state], chain) {
6369 n = key_setdumpsa(sav, SADB_DUMP, satype,
6370 --cnt, mhp->msg->sadb_msg_pid);
6372 return key_senderror(so, m, ENOBUFS);
6374 key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
6384 * SADB_X_PROMISC processing
6386 * m will always be freed.
6389 key_promisc(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
6394 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
6395 panic("key_promisc: NULL pointer is passed.\n");
6397 olen = PFKEY_UNUNIT64(mhp->msg->sadb_msg_len);
6399 if (olen < sizeof(struct sadb_msg)) {
6401 return key_senderror(so, m, EINVAL);
6406 } else if (olen == sizeof(struct sadb_msg)) {
6407 /* enable/disable promisc mode */
6410 if ((kp = (struct keycb *)sotorawcb(so)) == NULL)
6411 return key_senderror(so, m, EINVAL);
6412 mhp->msg->sadb_msg_errno = 0;
6413 switch (mhp->msg->sadb_msg_satype) {
6416 kp->kp_promisc = mhp->msg->sadb_msg_satype;
6419 return key_senderror(so, m, EINVAL);
6422 /* send the original message back to everyone */
6423 mhp->msg->sadb_msg_errno = 0;
6424 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
6426 /* send packet as is */
6428 m_adj(m, PFKEY_ALIGN8(sizeof(struct sadb_msg)));
6430 /* TODO: if sadb_msg_seq is specified, send to specific pid */
6431 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
6435 static int (*key_typesw[]) (struct socket *, struct mbuf *,
6436 const struct sadb_msghdr *) = {
6437 NULL, /* SADB_RESERVED */
6438 key_getspi, /* SADB_GETSPI */
6439 key_update, /* SADB_UPDATE */
6440 key_add, /* SADB_ADD */
6441 key_delete, /* SADB_DELETE */
6442 key_get, /* SADB_GET */
6443 key_acquire2, /* SADB_ACQUIRE */
6444 key_register, /* SADB_REGISTER */
6445 NULL, /* SADB_EXPIRE */
6446 key_flush, /* SADB_FLUSH */
6447 key_dump, /* SADB_DUMP */
6448 key_promisc, /* SADB_X_PROMISC */
6449 NULL, /* SADB_X_PCHANGE */
6450 key_spdadd, /* SADB_X_SPDUPDATE */
6451 key_spdadd, /* SADB_X_SPDADD */
6452 key_spddelete, /* SADB_X_SPDDELETE */
6453 key_spdget, /* SADB_X_SPDGET */
6454 NULL, /* SADB_X_SPDACQUIRE */
6455 key_spddump, /* SADB_X_SPDDUMP */
6456 key_spdflush, /* SADB_X_SPDFLUSH */
6457 key_spdadd, /* SADB_X_SPDSETIDX */
6458 NULL, /* SADB_X_SPDEXPIRE */
6459 key_spddelete2, /* SADB_X_SPDDELETE2 */
6463 * parse sadb_msg buffer to process PFKEYv2,
6464 * and create a data to response if needed.
6465 * I think to be dealed with mbuf directly.
6467 * msgp : pointer to pointer to a received buffer pulluped.
6468 * This is rewrited to response.
6469 * so : pointer to socket.
6471 * length for buffer to send to user process.
6474 key_parse(struct mbuf *m, struct socket *so)
6476 struct sadb_msg *msg;
6477 struct sadb_msghdr mh;
6483 if (m == NULL || so == NULL)
6484 panic("key_parse: NULL pointer is passed.\n");
6486 #if 0 /*kdebug_sadb assumes msg in linear buffer*/
6487 KEYDEBUG(KEYDEBUG_KEY_DUMP,
6488 ipseclog((LOG_DEBUG, "key_parse: passed sadb_msg\n"));
6492 if (m->m_len < sizeof(struct sadb_msg)) {
6493 m = m_pullup(m, sizeof(struct sadb_msg));
6497 msg = mtod(m, struct sadb_msg *);
6498 orglen = PFKEY_UNUNIT64(msg->sadb_msg_len);
6499 target = KEY_SENDUP_ONE;
6501 if ((m->m_flags & M_PKTHDR) == 0 ||
6502 m->m_pkthdr.len != m->m_pkthdr.len) {
6503 ipseclog((LOG_DEBUG, "key_parse: invalid message length.\n"));
6504 pfkeystat.out_invlen++;
6509 if (msg->sadb_msg_version != PF_KEY_V2) {
6510 ipseclog((LOG_DEBUG,
6511 "key_parse: PF_KEY version %u is mismatched.\n",
6512 msg->sadb_msg_version));
6513 pfkeystat.out_invver++;
6518 if (msg->sadb_msg_type > SADB_MAX) {
6519 ipseclog((LOG_DEBUG, "key_parse: invalid type %u is passed.\n",
6520 msg->sadb_msg_type));
6521 pfkeystat.out_invmsgtype++;
6526 /* for old-fashioned code - should be nuked */
6527 if (m->m_pkthdr.len > MCLBYTES) {
6534 MGETHDR(n, MB_DONTWAIT, MT_DATA);
6535 if (n && m->m_pkthdr.len > MHLEN) {
6536 MCLGET(n, MB_DONTWAIT);
6537 if ((n->m_flags & M_EXT) == 0) {
6546 m_copydata(m, 0, m->m_pkthdr.len, mtod(n, caddr_t));
6547 n->m_pkthdr.len = n->m_len = m->m_pkthdr.len;
6553 /* align the mbuf chain so that extensions are in contiguous region. */
6554 error = key_align(m, &mh);
6558 if (m->m_next) { /*XXX*/
6566 switch (msg->sadb_msg_satype) {
6567 case SADB_SATYPE_UNSPEC:
6568 switch (msg->sadb_msg_type) {
6576 ipseclog((LOG_DEBUG, "key_parse: must specify satype "
6577 "when msg type=%u.\n", msg->sadb_msg_type));
6578 pfkeystat.out_invsatype++;
6583 case SADB_SATYPE_AH:
6584 case SADB_SATYPE_ESP:
6585 case SADB_X_SATYPE_IPCOMP:
6586 switch (msg->sadb_msg_type) {
6588 case SADB_X_SPDDELETE:
6590 case SADB_X_SPDDUMP:
6591 case SADB_X_SPDFLUSH:
6592 case SADB_X_SPDSETIDX:
6593 case SADB_X_SPDUPDATE:
6594 case SADB_X_SPDDELETE2:
6595 ipseclog((LOG_DEBUG, "key_parse: illegal satype=%u\n",
6596 msg->sadb_msg_type));
6597 pfkeystat.out_invsatype++;
6602 case SADB_SATYPE_RSVP:
6603 case SADB_SATYPE_OSPFV2:
6604 case SADB_SATYPE_RIPV2:
6605 case SADB_SATYPE_MIP:
6606 ipseclog((LOG_DEBUG, "key_parse: type %u isn't supported.\n",
6607 msg->sadb_msg_satype));
6608 pfkeystat.out_invsatype++;
6611 case 1: /* XXX: What does it do? */
6612 if (msg->sadb_msg_type == SADB_X_PROMISC)
6616 ipseclog((LOG_DEBUG, "key_parse: invalid type %u is passed.\n",
6617 msg->sadb_msg_satype));
6618 pfkeystat.out_invsatype++;
6623 /* check field of upper layer protocol and address family */
6624 if (mh.ext[SADB_EXT_ADDRESS_SRC] != NULL
6625 && mh.ext[SADB_EXT_ADDRESS_DST] != NULL) {
6626 struct sadb_address *src0, *dst0;
6629 src0 = (struct sadb_address *)(mh.ext[SADB_EXT_ADDRESS_SRC]);
6630 dst0 = (struct sadb_address *)(mh.ext[SADB_EXT_ADDRESS_DST]);
6632 /* check upper layer protocol */
6633 if (src0->sadb_address_proto != dst0->sadb_address_proto) {
6634 ipseclog((LOG_DEBUG, "key_parse: upper layer protocol mismatched.\n"));
6635 pfkeystat.out_invaddr++;
6641 if (PFKEY_ADDR_SADDR(src0)->sa_family !=
6642 PFKEY_ADDR_SADDR(dst0)->sa_family) {
6643 ipseclog((LOG_DEBUG, "key_parse: address family mismatched.\n"));
6644 pfkeystat.out_invaddr++;
6648 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
6649 PFKEY_ADDR_SADDR(dst0)->sa_len) {
6650 ipseclog((LOG_DEBUG,
6651 "key_parse: address struct size mismatched.\n"));
6652 pfkeystat.out_invaddr++;
6657 switch (PFKEY_ADDR_SADDR(src0)->sa_family) {
6659 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
6660 sizeof(struct sockaddr_in)) {
6661 pfkeystat.out_invaddr++;
6667 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
6668 sizeof(struct sockaddr_in6)) {
6669 pfkeystat.out_invaddr++;
6675 ipseclog((LOG_DEBUG,
6676 "key_parse: unsupported address family.\n"));
6677 pfkeystat.out_invaddr++;
6678 error = EAFNOSUPPORT;
6682 switch (PFKEY_ADDR_SADDR(src0)->sa_family) {
6684 plen = sizeof(struct in_addr) << 3;
6687 plen = sizeof(struct in6_addr) << 3;
6690 plen = 0; /*fool gcc*/
6694 /* check max prefix length */
6695 if (src0->sadb_address_prefixlen > plen ||
6696 dst0->sadb_address_prefixlen > plen) {
6697 ipseclog((LOG_DEBUG,
6698 "key_parse: illegal prefixlen.\n"));
6699 pfkeystat.out_invaddr++;
6705 * prefixlen == 0 is valid because there can be a case when
6706 * all addresses are matched.
6710 if (msg->sadb_msg_type >= sizeof(key_typesw)/sizeof(key_typesw[0]) ||
6711 key_typesw[msg->sadb_msg_type] == NULL) {
6712 pfkeystat.out_invmsgtype++;
6717 return (*key_typesw[msg->sadb_msg_type])(so, m, &mh);
6720 msg->sadb_msg_errno = error;
6721 return key_sendup_mbuf(so, m, target);
6725 key_senderror(struct socket *so, struct mbuf *m, int code)
6727 struct sadb_msg *msg;
6729 if (m->m_len < sizeof(struct sadb_msg))
6730 panic("invalid mbuf passed to key_senderror");
6732 msg = mtod(m, struct sadb_msg *);
6733 msg->sadb_msg_errno = code;
6734 return key_sendup_mbuf(so, m, KEY_SENDUP_ONE);
6738 * set the pointer to each header into message buffer.
6739 * m will be freed on error.
6740 * XXX larger-than-MCLBYTES extension?
6743 key_align(struct mbuf *m, struct sadb_msghdr *mhp)
6746 struct sadb_ext *ext;
6752 if (m == NULL || mhp == NULL)
6753 panic("key_align: NULL pointer is passed.\n");
6754 if (m->m_len < sizeof(struct sadb_msg))
6755 panic("invalid mbuf passed to key_align");
6758 bzero(mhp, sizeof(*mhp));
6760 mhp->msg = mtod(m, struct sadb_msg *);
6761 mhp->ext[0] = (struct sadb_ext *)mhp->msg; /*XXX backward compat */
6763 end = PFKEY_UNUNIT64(mhp->msg->sadb_msg_len);
6764 extlen = end; /*just in case extlen is not updated*/
6765 for (off = sizeof(struct sadb_msg); off < end; off += extlen) {
6766 n = m_pulldown(m, off, sizeof(struct sadb_ext), &toff);
6768 /* m is already freed */
6771 ext = (struct sadb_ext *)(mtod(n, caddr_t) + toff);
6774 switch (ext->sadb_ext_type) {
6776 case SADB_EXT_ADDRESS_SRC:
6777 case SADB_EXT_ADDRESS_DST:
6778 case SADB_EXT_ADDRESS_PROXY:
6779 case SADB_EXT_LIFETIME_CURRENT:
6780 case SADB_EXT_LIFETIME_HARD:
6781 case SADB_EXT_LIFETIME_SOFT:
6782 case SADB_EXT_KEY_AUTH:
6783 case SADB_EXT_KEY_ENCRYPT:
6784 case SADB_EXT_IDENTITY_SRC:
6785 case SADB_EXT_IDENTITY_DST:
6786 case SADB_EXT_SENSITIVITY:
6787 case SADB_EXT_PROPOSAL:
6788 case SADB_EXT_SUPPORTED_AUTH:
6789 case SADB_EXT_SUPPORTED_ENCRYPT:
6790 case SADB_EXT_SPIRANGE:
6791 case SADB_X_EXT_POLICY:
6792 case SADB_X_EXT_SA2:
6793 /* duplicate check */
6795 * XXX Are there duplication payloads of either
6796 * KEY_AUTH or KEY_ENCRYPT ?
6798 if (mhp->ext[ext->sadb_ext_type] != NULL) {
6799 ipseclog((LOG_DEBUG,
6800 "key_align: duplicate ext_type %u "
6801 "is passed.\n", ext->sadb_ext_type));
6803 pfkeystat.out_dupext++;
6808 ipseclog((LOG_DEBUG,
6809 "key_align: invalid ext_type %u is passed.\n",
6810 ext->sadb_ext_type));
6812 pfkeystat.out_invexttype++;
6816 extlen = PFKEY_UNUNIT64(ext->sadb_ext_len);
6818 if (key_validate_ext(ext, extlen)) {
6820 pfkeystat.out_invlen++;
6824 n = m_pulldown(m, off, extlen, &toff);
6826 /* m is already freed */
6829 ext = (struct sadb_ext *)(mtod(n, caddr_t) + toff);
6831 mhp->ext[ext->sadb_ext_type] = ext;
6832 mhp->extoff[ext->sadb_ext_type] = off;
6833 mhp->extlen[ext->sadb_ext_type] = extlen;
6838 pfkeystat.out_invlen++;
6846 key_validate_ext(const struct sadb_ext *ext, int len)
6848 const struct sockaddr *sa;
6849 enum { NONE, ADDR } checktype = NONE;
6851 const int sal = offsetof(struct sockaddr, sa_len) + sizeof(sa->sa_len);
6853 if (len != PFKEY_UNUNIT64(ext->sadb_ext_len))
6856 /* if it does not match minimum/maximum length, bail */
6857 if (ext->sadb_ext_type >= sizeof(minsize) / sizeof(minsize[0]) ||
6858 ext->sadb_ext_type >= sizeof(maxsize) / sizeof(maxsize[0]))
6860 if (!minsize[ext->sadb_ext_type] || len < minsize[ext->sadb_ext_type])
6862 if (maxsize[ext->sadb_ext_type] && len > maxsize[ext->sadb_ext_type])
6865 /* more checks based on sadb_ext_type XXX need more */
6866 switch (ext->sadb_ext_type) {
6867 case SADB_EXT_ADDRESS_SRC:
6868 case SADB_EXT_ADDRESS_DST:
6869 case SADB_EXT_ADDRESS_PROXY:
6870 baselen = PFKEY_ALIGN8(sizeof(struct sadb_address));
6873 case SADB_EXT_IDENTITY_SRC:
6874 case SADB_EXT_IDENTITY_DST:
6875 if (((const struct sadb_ident *)ext)->sadb_ident_type ==
6876 SADB_X_IDENTTYPE_ADDR) {
6877 baselen = PFKEY_ALIGN8(sizeof(struct sadb_ident));
6887 switch (checktype) {
6891 sa = (const struct sockaddr *)(((const u_int8_t*)ext)+baselen);
6892 if (len < baselen + sal)
6894 if (baselen + PFKEY_ALIGN8(sa->sa_len) != len)
6907 for (i = 0; i < IPSEC_DIR_MAX; i++) {
6908 LIST_INIT(&sptree[i]);
6911 LIST_INIT(&sahtree);
6913 for (i = 0; i <= SADB_SATYPE_MAX; i++) {
6914 LIST_INIT(®tree[i]);
6917 #ifndef IPSEC_NONBLOCK_ACQUIRE
6918 LIST_INIT(&acqtree);
6920 LIST_INIT(&spacqtree);
6922 /* system default */
6923 ip4_def_policy.policy = IPSEC_POLICY_NONE;
6924 ip4_def_policy.refcnt++; /*never reclaim this*/
6926 #ifndef IPSEC_DEBUG2
6927 callout_init(&key_timehandler_ch);
6928 callout_reset(&key_timehandler_ch, hz, key_timehandler, NULL);
6929 #endif /*IPSEC_DEBUG2*/
6931 /* initialize key statistics */
6932 keystat.getspi_count = 1;
6934 printf("IPsec: Initialized Security Association Processing.\n");
6940 * XXX: maybe This function is called after INBOUND IPsec processing.
6942 * Special check for tunnel-mode packets.
6943 * We must make some checks for consistency between inner and outer IP header.
6945 * xxx more checks to be provided
6948 key_checktunnelsanity(struct secasvar *sav, u_int family, caddr_t src,
6952 if (sav->sah == NULL)
6953 panic("sav->sah == NULL at key_checktunnelsanity");
6955 /* XXX: check inner IP header */
6961 #define hostnamelen strlen(hostname)
6964 * Get FQDN for the host.
6965 * If the administrator configured hostname (by hostname(1)) without
6966 * domain name, returns nothing.
6973 static char fqdn[MAXHOSTNAMELEN + 1];
6978 /* check if it comes with domain name. */
6980 for (i = 0; i < hostnamelen; i++) {
6981 if (hostname[i] == '.')
6987 /* NOTE: hostname may not be NUL-terminated. */
6988 bzero(fqdn, sizeof(fqdn));
6989 bcopy(hostname, fqdn, hostnamelen);
6990 fqdn[hostnamelen] = '\0';
6995 * get username@FQDN for the host/user.
6998 key_getuserfqdn(void)
7001 static char userfqdn[MAXHOSTNAMELEN + MAXLOGNAME + 2];
7002 struct proc *p = curproc;
7005 if (!p || !p->p_pgrp || !p->p_pgrp->pg_session)
7007 if (!(host = key_getfqdn()))
7010 /* NOTE: s_login may not be-NUL terminated. */
7011 bzero(userfqdn, sizeof(userfqdn));
7012 bcopy(p->p_pgrp->pg_session->s_login, userfqdn, MAXLOGNAME);
7013 userfqdn[MAXLOGNAME] = '\0'; /* safeguard */
7014 q = userfqdn + strlen(userfqdn);
7016 bcopy(host, q, strlen(host));
7024 /* record data transfer on SA, and update timestamps */
7026 key_sa_recordxfer(struct secasvar *sav, struct mbuf *m)
7028 KASSERT(sav != NULL, ("key_sa_recordxfer: Null secasvar"));
7029 KASSERT(m != NULL, ("key_sa_recordxfer: Null mbuf"));
7034 * XXX Currently, there is a difference of bytes size
7035 * between inbound and outbound processing.
7037 sav->lft_c->sadb_lifetime_bytes += m->m_pkthdr.len;
7038 /* to check bytes lifetime is done in key_timehandler(). */
7041 * We use the number of packets as the unit of
7042 * sadb_lifetime_allocations. We increment the variable
7043 * whenever {esp,ah}_{in,out}put is called.
7045 sav->lft_c->sadb_lifetime_allocations++;
7046 /* XXX check for expires? */
7049 * NOTE: We record CURRENT sadb_lifetime_usetime by using wall clock,
7050 * in seconds. HARD and SOFT lifetime are measured by the time
7051 * difference (again in seconds) from sadb_lifetime_usetime.
7055 * -----+-----+--------+---> t
7056 * <--------------> HARD
7059 sav->lft_c->sadb_lifetime_usetime = time_second;
7060 /* XXX check for expires? */
7067 key_sa_routechange(struct sockaddr *dst)
7069 struct secashead *sah;
7072 LIST_FOREACH(sah, &sahtree, chain) {
7073 ro = &sah->sa_route;
7074 if (ro->ro_rt && dst->sa_len == ro->ro_dst.sa_len
7075 && bcmp(dst, &ro->ro_dst, dst->sa_len) == 0) {
7077 ro->ro_rt = (struct rtentry *)NULL;
7085 key_sa_chgstate(struct secasvar *sav, u_int8_t state)
7088 panic("key_sa_chgstate called with sav == NULL");
7090 if (sav->state == state)
7093 if (__LIST_CHAINED(sav))
7094 LIST_REMOVE(sav, chain);
7097 LIST_INSERT_HEAD(&sav->sah->savtree[state], sav, chain);
7101 key_sa_stir_iv(struct secasvar *sav)
7105 panic("key_sa_stir_iv called with sav == NULL");
7106 key_randomfill(sav->iv, sav->ivlen);
7110 static struct mbuf *
7111 key_alloc_mbuf(int l)
7113 struct mbuf *m = NULL, *n;
7118 MGET(n, MB_DONTWAIT, MT_DATA);
7119 if (n && len > MLEN)
7120 MCLGET(n, MB_DONTWAIT);
7128 n->m_len = M_TRAILINGSPACE(n);
7129 /* use the bottom of mbuf, hoping we can prepend afterwards */
7130 if (n->m_len > len) {
7131 t = (n->m_len - len) & ~(sizeof(long) - 1);