1 /* $FreeBSD: src/sys/netipsec/key.c,v 1.3.2.1 2003/01/24 05:11:35 sam Exp $ */
2 /* $KAME: key.c,v 1.191 2001/06/27 10:46:49 sakane Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * This code is referd to RFC 2367
38 #include "opt_inet6.h"
39 #include "opt_ipsec.h"
41 #include <sys/types.h>
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
46 #include <sys/domain.h>
47 #include <sys/protosw.h>
48 #include <sys/malloc.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/sysctl.h>
52 #include <sys/errno.h>
54 #include <sys/queue.h>
55 #include <sys/syslog.h>
58 #include <net/route.h>
59 #include <net/raw_cb.h>
61 #include <netinet/in.h>
62 #include <netinet/in_systm.h>
63 #include <netinet/ip.h>
64 #include <netinet/in_var.h>
67 #include <netinet/ip6.h>
68 #include <netinet6/in6_var.h>
69 #include <netinet6/ip6_var.h>
73 #include <netinet/in_pcb.h>
76 #include <netinet6/in6_pcb.h>
79 #include <net/pfkeyv2.h>
80 #include <netipsec/keydb.h>
81 #include <netipsec/key.h>
82 #include <netipsec/keysock.h>
83 #include <netipsec/key_debug.h>
85 #include <netipsec/ipsec.h>
87 #include <netipsec/ipsec6.h>
90 #include <netipsec/xform.h>
92 #include <machine/stdarg.h>
95 #include <sys/random.h>
97 #include <net/net_osdep.h>
100 #define _BITS(bytes) ((bytes) << 3)
103 * Note on SA reference counting:
104 * - SAs that are not in DEAD state will have (total external reference + 1)
105 * following value in reference count field. they cannot be freed and are
106 * referenced from SA header.
107 * - SAs that are in DEAD state will have (total external reference)
108 * in reference count field. they are ready to be freed. reference from
109 * SA header will be removed in key_delsav(), when the reference count
110 * field hits 0 (= no external reference other than from SA header.
113 u_int32_t key_debug_level = 0;
114 static u_int key_spi_trycnt = 1000;
115 static u_int32_t key_spi_minval = 0x100;
116 static u_int32_t key_spi_maxval = 0x0fffffff; /* XXX */
117 static u_int32_t policy_id = 0;
118 static u_int key_int_random = 60; /*interval to initialize randseed,1(m)*/
119 static u_int key_larval_lifetime = 30; /* interval to expire acquiring, 30(s)*/
120 static int key_blockacq_count = 10; /* counter for blocking SADB_ACQUIRE.*/
121 static int key_blockacq_lifetime = 20; /* lifetime for blocking SADB_ACQUIRE.*/
122 static int key_prefered_oldsa = 1; /* prefered old sa rather than new sa.*/
124 static u_int32_t acq_seq = 0;
125 static int key_tick_init_random = 0;
127 static LIST_HEAD(_sptree, secpolicy) sptree[IPSEC_DIR_MAX]; /* SPD */
128 static LIST_HEAD(_sahtree, secashead) sahtree; /* SAD */
129 static LIST_HEAD(_regtree, secreg) regtree[SADB_SATYPE_MAX + 1];
131 #ifndef IPSEC_NONBLOCK_ACQUIRE
132 static LIST_HEAD(_acqtree, secacq) acqtree; /* acquiring list */
134 static LIST_HEAD(_spacqtree, secspacq) spacqtree; /* SP acquiring list */
136 /* search order for SAs */
137 static u_int saorder_state_valid[] = {
138 SADB_SASTATE_DYING, SADB_SASTATE_MATURE,
140 * This order is important because we must select the oldest SA
141 * for outbound processing. For inbound, This is not important.
144 static u_int saorder_state_alive[] = {
146 SADB_SASTATE_MATURE, SADB_SASTATE_DYING, SADB_SASTATE_LARVAL
148 static u_int saorder_state_any[] = {
149 SADB_SASTATE_MATURE, SADB_SASTATE_DYING,
150 SADB_SASTATE_LARVAL, SADB_SASTATE_DEAD
153 static const int minsize[] = {
154 sizeof(struct sadb_msg), /* SADB_EXT_RESERVED */
155 sizeof(struct sadb_sa), /* SADB_EXT_SA */
156 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_CURRENT */
157 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_HARD */
158 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_SOFT */
159 sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_SRC */
160 sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_DST */
161 sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_PROXY */
162 sizeof(struct sadb_key), /* SADB_EXT_KEY_AUTH */
163 sizeof(struct sadb_key), /* SADB_EXT_KEY_ENCRYPT */
164 sizeof(struct sadb_ident), /* SADB_EXT_IDENTITY_SRC */
165 sizeof(struct sadb_ident), /* SADB_EXT_IDENTITY_DST */
166 sizeof(struct sadb_sens), /* SADB_EXT_SENSITIVITY */
167 sizeof(struct sadb_prop), /* SADB_EXT_PROPOSAL */
168 sizeof(struct sadb_supported), /* SADB_EXT_SUPPORTED_AUTH */
169 sizeof(struct sadb_supported), /* SADB_EXT_SUPPORTED_ENCRYPT */
170 sizeof(struct sadb_spirange), /* SADB_EXT_SPIRANGE */
171 0, /* SADB_X_EXT_KMPRIVATE */
172 sizeof(struct sadb_x_policy), /* SADB_X_EXT_POLICY */
173 sizeof(struct sadb_x_sa2), /* SADB_X_SA2 */
175 static const int maxsize[] = {
176 sizeof(struct sadb_msg), /* SADB_EXT_RESERVED */
177 sizeof(struct sadb_sa), /* SADB_EXT_SA */
178 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_CURRENT */
179 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_HARD */
180 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_SOFT */
181 0, /* SADB_EXT_ADDRESS_SRC */
182 0, /* SADB_EXT_ADDRESS_DST */
183 0, /* SADB_EXT_ADDRESS_PROXY */
184 0, /* SADB_EXT_KEY_AUTH */
185 0, /* SADB_EXT_KEY_ENCRYPT */
186 0, /* SADB_EXT_IDENTITY_SRC */
187 0, /* SADB_EXT_IDENTITY_DST */
188 0, /* SADB_EXT_SENSITIVITY */
189 0, /* SADB_EXT_PROPOSAL */
190 0, /* SADB_EXT_SUPPORTED_AUTH */
191 0, /* SADB_EXT_SUPPORTED_ENCRYPT */
192 sizeof(struct sadb_spirange), /* SADB_EXT_SPIRANGE */
193 0, /* SADB_X_EXT_KMPRIVATE */
194 0, /* SADB_X_EXT_POLICY */
195 sizeof(struct sadb_x_sa2), /* SADB_X_SA2 */
198 static int ipsec_esp_keymin = 256;
199 static int ipsec_esp_auth = 0;
200 static int ipsec_ah_keymin = 128;
203 SYSCTL_DECL(_net_key);
206 SYSCTL_INT(_net_key, KEYCTL_DEBUG_LEVEL, debug, CTLFLAG_RW, \
207 &key_debug_level, 0, "");
209 /* max count of trial for the decision of spi value */
210 SYSCTL_INT(_net_key, KEYCTL_SPI_TRY, spi_trycnt, CTLFLAG_RW, \
211 &key_spi_trycnt, 0, "");
213 /* minimum spi value to allocate automatically. */
214 SYSCTL_INT(_net_key, KEYCTL_SPI_MIN_VALUE, spi_minval, CTLFLAG_RW, \
215 &key_spi_minval, 0, "");
217 /* maximun spi value to allocate automatically. */
218 SYSCTL_INT(_net_key, KEYCTL_SPI_MAX_VALUE, spi_maxval, CTLFLAG_RW, \
219 &key_spi_maxval, 0, "");
221 /* interval to initialize randseed */
222 SYSCTL_INT(_net_key, KEYCTL_RANDOM_INT, int_random, CTLFLAG_RW, \
223 &key_int_random, 0, "");
225 /* lifetime for larval SA */
226 SYSCTL_INT(_net_key, KEYCTL_LARVAL_LIFETIME, larval_lifetime, CTLFLAG_RW, \
227 &key_larval_lifetime, 0, "");
229 /* counter for blocking to send SADB_ACQUIRE to IKEd */
230 SYSCTL_INT(_net_key, KEYCTL_BLOCKACQ_COUNT, blockacq_count, CTLFLAG_RW, \
231 &key_blockacq_count, 0, "");
233 /* lifetime for blocking to send SADB_ACQUIRE to IKEd */
234 SYSCTL_INT(_net_key, KEYCTL_BLOCKACQ_LIFETIME, blockacq_lifetime, CTLFLAG_RW, \
235 &key_blockacq_lifetime, 0, "");
238 SYSCTL_INT(_net_key, KEYCTL_ESP_AUTH, esp_auth, CTLFLAG_RW, \
239 &ipsec_esp_auth, 0, "");
241 /* minimum ESP key length */
242 SYSCTL_INT(_net_key, KEYCTL_ESP_KEYMIN, esp_keymin, CTLFLAG_RW, \
243 &ipsec_esp_keymin, 0, "");
245 /* minimum AH key length */
246 SYSCTL_INT(_net_key, KEYCTL_AH_KEYMIN, ah_keymin, CTLFLAG_RW, \
247 &ipsec_ah_keymin, 0, "");
249 /* perfered old SA rather than new SA */
250 SYSCTL_INT(_net_key, KEYCTL_PREFERED_OLDSA, prefered_oldsa, CTLFLAG_RW,\
251 &key_prefered_oldsa, 0, "");
254 #define LIST_FOREACH(elm, head, field) \
255 for (elm = LIST_FIRST(head); elm; elm = LIST_NEXT(elm, field))
257 #define __LIST_CHAINED(elm) \
258 (!((elm)->chain.le_next == NULL && (elm)->chain.le_prev == NULL))
259 #define LIST_INSERT_TAIL(head, elm, type, field) \
261 struct type *curelm = LIST_FIRST(head); \
262 if (curelm == NULL) {\
263 LIST_INSERT_HEAD(head, elm, field); \
265 while (LIST_NEXT(curelm, field)) \
266 curelm = LIST_NEXT(curelm, field);\
267 LIST_INSERT_AFTER(curelm, elm, field);\
271 #define KEY_CHKSASTATE(head, sav, name) \
273 if ((head) != (sav)) { \
274 ipseclog((LOG_DEBUG, "%s: state mismatched (TREE=%d SA=%d)\n", \
275 (name), (head), (sav))); \
280 #define KEY_CHKSPDIR(head, sp, name) \
282 if ((head) != (sp)) { \
283 ipseclog((LOG_DEBUG, "%s: direction mismatched (TREE=%d SP=%d), " \
284 "anyway continue.\n", \
285 (name), (head), (sp))); \
289 MALLOC_DEFINE(M_SECA, "key mgmt", "security associations, key management");
292 #define KMALLOC(p, t, n) \
293 ((p) = (t) malloc((unsigned long)(n), M_SECA, M_NOWAIT))
295 free((caddr_t)(p), M_SECA)
297 #define KMALLOC(p, t, n) \
299 ((p) = (t)malloc((unsigned long)(n), M_SECA, M_NOWAIT)); \
300 printf("%s %d: %p <- KMALLOC(%s, %d)\n", \
301 __FILE__, __LINE__, (p), #t, n); \
306 printf("%s %d: %p -> KFREE()\n", __FILE__, __LINE__, (p)); \
307 free((caddr_t)(p), M_SECA); \
312 * set parameters into secpolicyindex buffer.
313 * Must allocate secpolicyindex buffer passed to this function.
315 #define KEY_SETSECSPIDX(_dir, s, d, ps, pd, ulp, idx) \
317 bzero((idx), sizeof(struct secpolicyindex)); \
318 (idx)->dir = (_dir); \
319 (idx)->prefs = (ps); \
320 (idx)->prefd = (pd); \
321 (idx)->ul_proto = (ulp); \
322 bcopy((s), &(idx)->src, ((const struct sockaddr *)(s))->sa_len); \
323 bcopy((d), &(idx)->dst, ((const struct sockaddr *)(d))->sa_len); \
327 * set parameters into secasindex buffer.
328 * Must allocate secasindex buffer before calling this function.
330 #define KEY_SETSECASIDX(p, m, r, s, d, idx) \
332 bzero((idx), sizeof(struct secasindex)); \
333 (idx)->proto = (p); \
335 (idx)->reqid = (r); \
336 bcopy((s), &(idx)->src, ((const struct sockaddr *)(s))->sa_len); \
337 bcopy((d), &(idx)->dst, ((const struct sockaddr *)(d))->sa_len); \
342 u_long getspi_count; /* the avarage of count to try to get new SPI */
346 struct sadb_msg *msg;
347 struct sadb_ext *ext[SADB_EXT_MAX + 1];
348 int extoff[SADB_EXT_MAX + 1];
349 int extlen[SADB_EXT_MAX + 1];
352 static struct secasvar *key_allocsa_policy __P((const struct secasindex *));
353 static void key_freesp_so __P((struct secpolicy **));
354 static struct secasvar *key_do_allocsa_policy __P((struct secashead *, u_int));
355 static void key_delsp __P((struct secpolicy *));
356 static struct secpolicy *key_getsp __P((struct secpolicyindex *));
357 static struct secpolicy *key_getspbyid __P((u_int32_t));
358 static u_int32_t key_newreqid __P((void));
359 static struct mbuf *key_gather_mbuf __P((struct mbuf *,
360 const struct sadb_msghdr *, int, int, ...));
361 static int key_spdadd __P((struct socket *, struct mbuf *,
362 const struct sadb_msghdr *));
363 static u_int32_t key_getnewspid __P((void));
364 static int key_spddelete __P((struct socket *, struct mbuf *,
365 const struct sadb_msghdr *));
366 static int key_spddelete2 __P((struct socket *, struct mbuf *,
367 const struct sadb_msghdr *));
368 static int key_spdget __P((struct socket *, struct mbuf *,
369 const struct sadb_msghdr *));
370 static int key_spdflush __P((struct socket *, struct mbuf *,
371 const struct sadb_msghdr *));
372 static int key_spddump __P((struct socket *, struct mbuf *,
373 const struct sadb_msghdr *));
374 static struct mbuf *key_setdumpsp __P((struct secpolicy *,
375 u_int8_t, u_int32_t, u_int32_t));
376 static u_int key_getspreqmsglen __P((struct secpolicy *));
377 static int key_spdexpire __P((struct secpolicy *));
378 static struct secashead *key_newsah __P((struct secasindex *));
379 static void key_delsah __P((struct secashead *));
380 static struct secasvar *key_newsav __P((struct mbuf *,
381 const struct sadb_msghdr *, struct secashead *, int *,
383 #define KEY_NEWSAV(m, sadb, sah, e) \
384 key_newsav(m, sadb, sah, e, __FILE__, __LINE__)
385 static void key_delsav __P((struct secasvar *));
386 static struct secashead *key_getsah __P((struct secasindex *));
387 static struct secasvar *key_checkspidup __P((struct secasindex *, u_int32_t));
388 static struct secasvar *key_getsavbyspi __P((struct secashead *, u_int32_t));
389 static int key_setsaval __P((struct secasvar *, struct mbuf *,
390 const struct sadb_msghdr *));
391 static int key_mature __P((struct secasvar *));
392 static struct mbuf *key_setdumpsa __P((struct secasvar *, u_int8_t,
393 u_int8_t, u_int32_t, u_int32_t));
394 static struct mbuf *key_setsadbmsg __P((u_int8_t, u_int16_t, u_int8_t,
395 u_int32_t, pid_t, u_int16_t));
396 static struct mbuf *key_setsadbsa __P((struct secasvar *));
397 static struct mbuf *key_setsadbaddr __P((u_int16_t,
398 const struct sockaddr *, u_int8_t, u_int16_t));
400 static struct mbuf *key_setsadbident __P((u_int16_t, u_int16_t, caddr_t,
403 static struct mbuf *key_setsadbxsa2 __P((u_int8_t, u_int32_t, u_int32_t));
404 static struct mbuf *key_setsadbxpolicy __P((u_int16_t, u_int8_t,
406 static void *key_newbuf __P((const void *, u_int));
408 static int key_ismyaddr6 __P((struct sockaddr_in6 *));
411 /* flags for key_cmpsaidx() */
412 #define CMP_HEAD 1 /* protocol, addresses. */
413 #define CMP_MODE_REQID 2 /* additionally HEAD, reqid, mode. */
414 #define CMP_REQID 3 /* additionally HEAD, reaid. */
415 #define CMP_EXACTLY 4 /* all elements. */
416 static int key_cmpsaidx
417 __P((const struct secasindex *, const struct secasindex *, int));
419 static int key_cmpspidx_exactly
420 __P((struct secpolicyindex *, struct secpolicyindex *));
421 static int key_cmpspidx_withmask
422 __P((struct secpolicyindex *, struct secpolicyindex *));
423 static int key_sockaddrcmp __P((const struct sockaddr *, const struct sockaddr *, int));
424 static int key_bbcmp __P((const void *, const void *, u_int));
425 static void key_srandom __P((void));
426 static u_int16_t key_satype2proto __P((u_int8_t));
427 static u_int8_t key_proto2satype __P((u_int16_t));
429 static int key_getspi __P((struct socket *, struct mbuf *,
430 const struct sadb_msghdr *));
431 static u_int32_t key_do_getnewspi __P((struct sadb_spirange *,
432 struct secasindex *));
433 static int key_update __P((struct socket *, struct mbuf *,
434 const struct sadb_msghdr *));
435 #ifdef IPSEC_DOSEQCHECK
436 static struct secasvar *key_getsavbyseq __P((struct secashead *, u_int32_t));
438 static int key_add __P((struct socket *, struct mbuf *,
439 const struct sadb_msghdr *));
440 static int key_setident __P((struct secashead *, struct mbuf *,
441 const struct sadb_msghdr *));
442 static struct mbuf *key_getmsgbuf_x1 __P((struct mbuf *,
443 const struct sadb_msghdr *));
444 static int key_delete __P((struct socket *, struct mbuf *,
445 const struct sadb_msghdr *));
446 static int key_get __P((struct socket *, struct mbuf *,
447 const struct sadb_msghdr *));
449 static void key_getcomb_setlifetime __P((struct sadb_comb *));
450 static struct mbuf *key_getcomb_esp __P((void));
451 static struct mbuf *key_getcomb_ah __P((void));
452 static struct mbuf *key_getcomb_ipcomp __P((void));
453 static struct mbuf *key_getprop __P((const struct secasindex *));
455 static int key_acquire __P((const struct secasindex *, struct secpolicy *));
456 #ifndef IPSEC_NONBLOCK_ACQUIRE
457 static struct secacq *key_newacq __P((const struct secasindex *));
458 static struct secacq *key_getacq __P((const struct secasindex *));
459 static struct secacq *key_getacqbyseq __P((u_int32_t));
461 static struct secspacq *key_newspacq __P((struct secpolicyindex *));
462 static struct secspacq *key_getspacq __P((struct secpolicyindex *));
463 static int key_acquire2 __P((struct socket *, struct mbuf *,
464 const struct sadb_msghdr *));
465 static int key_register __P((struct socket *, struct mbuf *,
466 const struct sadb_msghdr *));
467 static int key_expire __P((struct secasvar *));
468 static int key_flush __P((struct socket *, struct mbuf *,
469 const struct sadb_msghdr *));
470 static int key_dump __P((struct socket *, struct mbuf *,
471 const struct sadb_msghdr *));
472 static int key_promisc __P((struct socket *, struct mbuf *,
473 const struct sadb_msghdr *));
474 static int key_senderror __P((struct socket *, struct mbuf *, int));
475 static int key_validate_ext __P((const struct sadb_ext *, int));
476 static int key_align __P((struct mbuf *, struct sadb_msghdr *));
478 static const char *key_getfqdn __P((void));
479 static const char *key_getuserfqdn __P((void));
481 static void key_sa_chgstate __P((struct secasvar *, u_int8_t));
482 static struct mbuf *key_alloc_mbuf __P((int));
484 #define SA_ADDREF(p) do { \
486 KASSERT((p)->refcnt != 0, \
487 ("SA refcnt overflow at %s:%u", __FILE__, __LINE__)); \
489 #define SA_DELREF(p) do { \
490 KASSERT((p)->refcnt > 0, \
491 ("SA refcnt underflow at %s:%u", __FILE__, __LINE__)); \
495 #define SP_ADDREF(p) do { \
497 KASSERT((p)->refcnt != 0, \
498 ("SP refcnt overflow at %s:%u", __FILE__, __LINE__)); \
500 #define SP_DELREF(p) do { \
501 KASSERT((p)->refcnt > 0, \
502 ("SP refcnt underflow at %s:%u", __FILE__, __LINE__)); \
507 * Return 0 when there are known to be no SP's for the specified
508 * direction. Otherwise return 1. This is used by IPsec code
509 * to optimize performance.
512 key_havesp(u_int dir)
514 return (dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND ?
515 LIST_FIRST(&sptree[dir]) != NULL : 1);
518 /* %%% IPsec policy management */
520 * allocating a SP for OUTBOUND or INBOUND packet.
521 * Must call key_freesp() later.
522 * OUT: NULL: not found
523 * others: found and return the pointer.
526 key_allocsp(struct secpolicyindex *spidx, u_int dir, const char* where, int tag)
528 struct secpolicy *sp;
531 KASSERT(spidx != NULL, ("key_allocsp: null spidx"));
532 KASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
533 ("key_allocsp: invalid direction %u", dir));
535 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
536 printf("DP key_allocsp from %s:%u\n", where, tag));
539 s = splnet(); /*called from softclock()*/
540 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
541 printf("*** objects\n");
542 kdebug_secpolicyindex(spidx));
544 LIST_FOREACH(sp, &sptree[dir], chain) {
545 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
546 printf("*** in SPD\n");
547 kdebug_secpolicyindex(&sp->spidx));
549 if (sp->state == IPSEC_SPSTATE_DEAD)
551 if (key_cmpspidx_withmask(&sp->spidx, spidx))
558 KEY_CHKSPDIR(sp->spidx.dir, dir, "key_allocsp");
560 /* found a SPD entry */
561 sp->lastused = time_second;
566 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
567 printf("DP key_allocsp return SP:%p (ID=%u) refcnt %u\n",
568 sp, sp ? sp->id : 0, sp ? sp->refcnt : 0));
573 * allocating a SP for OUTBOUND or INBOUND packet.
574 * Must call key_freesp() later.
575 * OUT: NULL: not found
576 * others: found and return the pointer.
579 key_allocsp2(u_int32_t spi,
580 union sockaddr_union *dst,
583 const char* where, int tag)
585 struct secpolicy *sp;
588 KASSERT(dst != NULL, ("key_allocsp2: null dst"));
589 KASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
590 ("key_allocsp2: invalid direction %u", dir));
592 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
593 printf("DP key_allocsp2 from %s:%u\n", where, tag));
596 s = splnet(); /*called from softclock()*/
597 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
598 printf("*** objects\n");
599 printf("spi %u proto %u dir %u\n", spi, proto, dir);
600 kdebug_sockaddr(&dst->sa));
602 LIST_FOREACH(sp, &sptree[dir], chain) {
603 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
604 printf("*** in SPD\n");
605 kdebug_secpolicyindex(&sp->spidx));
607 if (sp->state == IPSEC_SPSTATE_DEAD)
609 /* compare simple values, then dst address */
610 if (sp->spidx.ul_proto != proto)
612 /* NB: spi's must exist and match */
613 if (!sp->req || !sp->req->sav || sp->req->sav->spi != spi)
615 if (key_sockaddrcmp(&sp->spidx.dst.sa, &dst->sa, 1) == 0)
622 KEY_CHKSPDIR(sp->spidx.dir, dir, "key_allocsp2");
624 /* found a SPD entry */
625 sp->lastused = time_second;
630 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
631 printf("DP key_allocsp2 return SP:%p (ID=%u) refcnt %u\n",
632 sp, sp ? sp->id : 0, sp ? sp->refcnt : 0));
637 * return a policy that matches this particular inbound packet.
641 key_gettunnel(const struct sockaddr *osrc,
642 const struct sockaddr *odst,
643 const struct sockaddr *isrc,
644 const struct sockaddr *idst,
645 const char* where, int tag)
647 struct secpolicy *sp;
648 const int dir = IPSEC_DIR_INBOUND;
650 struct ipsecrequest *r1, *r2, *p;
651 struct secpolicyindex spidx;
653 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
654 printf("DP key_gettunnel from %s:%u\n", where, tag));
656 if (isrc->sa_family != idst->sa_family) {
657 ipseclog((LOG_ERR, "protocol family mismatched %d != %d\n.",
658 isrc->sa_family, idst->sa_family));
663 s = splnet(); /*called from softclock()*/
664 LIST_FOREACH(sp, &sptree[dir], chain) {
665 if (sp->state == IPSEC_SPSTATE_DEAD)
669 for (p = sp->req; p; p = p->next) {
670 if (p->saidx.mode != IPSEC_MODE_TUNNEL)
677 /* here we look at address matches only */
679 if (isrc->sa_len > sizeof(spidx.src) ||
680 idst->sa_len > sizeof(spidx.dst))
682 bcopy(isrc, &spidx.src, isrc->sa_len);
683 bcopy(idst, &spidx.dst, idst->sa_len);
684 if (!key_cmpspidx_withmask(&sp->spidx, &spidx))
687 if (key_sockaddrcmp(&r1->saidx.src.sa, isrc, 0) ||
688 key_sockaddrcmp(&r1->saidx.dst.sa, idst, 0))
692 if (key_sockaddrcmp(&r2->saidx.src.sa, osrc, 0) ||
693 key_sockaddrcmp(&r2->saidx.dst.sa, odst, 0))
702 sp->lastused = time_second;
707 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
708 printf("DP key_gettunnel return SP:%p (ID=%u) refcnt %u\n",
709 sp, sp ? sp->id : 0, sp ? sp->refcnt : 0));
714 * allocating an SA entry for an *OUTBOUND* packet.
715 * checking each request entries in SP, and acquire an SA if need.
716 * OUT: 0: there are valid requests.
717 * ENOENT: policy may be valid, but SA with REQUIRE is on acquiring.
720 key_checkrequest(struct ipsecrequest *isr, const struct secasindex *saidx)
725 KASSERT(isr != NULL, ("key_checkrequest: null isr"));
726 KASSERT(saidx != NULL, ("key_checkrequest: null saidx"));
727 KASSERT(saidx->mode == IPSEC_MODE_TRANSPORT ||
728 saidx->mode == IPSEC_MODE_TUNNEL,
729 ("key_checkrequest: unexpected policy %u", saidx->mode));
731 /* get current level */
732 level = ipsec_get_reqlevel(isr);
735 * XXX guard against protocol callbacks from the crypto
736 * thread as they reference ipsecrequest.sav which we
737 * temporarily null out below. Need to rethink how we
738 * handle bundled SA's in the callback thread.
740 SPLASSERT(net, "key_checkrequest");
743 * We do allocate new SA only if the state of SA in the holder is
744 * SADB_SASTATE_DEAD. The SA for outbound must be the oldest.
746 if (isr->sav != NULL) {
747 if (isr->sav->sah == NULL)
748 panic("key_checkrequest: sah is null.\n");
749 if (isr->sav == (struct secasvar *)LIST_FIRST(
750 &isr->sav->sah->savtree[SADB_SASTATE_DEAD])) {
751 KEY_FREESAV(&isr->sav);
757 * we free any SA stashed in the IPsec request because a different
758 * SA may be involved each time this request is checked, either
759 * because new SAs are being configured, or this request is
760 * associated with an unconnected datagram socket, or this request
761 * is associated with a system default policy.
763 * The operation may have negative impact to performance. We may
764 * want to check cached SA carefully, rather than picking new SA
767 if (isr->sav != NULL) {
768 KEY_FREESAV(&isr->sav);
774 * new SA allocation if no SA found.
775 * key_allocsa_policy should allocate the oldest SA available.
776 * See key_do_allocsa_policy(), and draft-jenkins-ipsec-rekeying-03.txt.
778 if (isr->sav == NULL)
779 isr->sav = key_allocsa_policy(saidx);
781 /* When there is SA. */
782 if (isr->sav != NULL) {
783 if (isr->sav->state != SADB_SASTATE_MATURE &&
784 isr->sav->state != SADB_SASTATE_DYING)
790 error = key_acquire(saidx, isr->sp);
792 /* XXX What should I do ? */
793 ipseclog((LOG_DEBUG, "key_checkrequest: error %d returned "
794 "from key_acquire.\n", error));
798 if (level != IPSEC_LEVEL_REQUIRE) {
799 /* XXX sigh, the interface to this routine is botched */
800 KASSERT(isr->sav == NULL, ("key_checkrequest: unexpected SA"));
808 * allocating a SA for policy entry from SAD.
809 * NOTE: searching SAD of aliving state.
810 * OUT: NULL: not found.
811 * others: found and return the pointer.
813 static struct secasvar *
814 key_allocsa_policy(const struct secasindex *saidx)
816 struct secashead *sah;
817 struct secasvar *sav;
818 u_int stateidx, state;
820 LIST_FOREACH(sah, &sahtree, chain) {
821 if (sah->state == SADB_SASTATE_DEAD)
823 if (key_cmpsaidx(&sah->saidx, saidx, CMP_MODE_REQID))
831 /* search valid state */
833 stateidx < _ARRAYLEN(saorder_state_valid);
836 state = saorder_state_valid[stateidx];
838 sav = key_do_allocsa_policy(sah, state);
847 * searching SAD with direction, protocol, mode and state.
848 * called by key_allocsa_policy().
851 * others : found, pointer to a SA.
853 static struct secasvar *
854 key_do_allocsa_policy(struct secashead *sah, u_int state)
856 struct secasvar *sav, *nextsav, *candidate, *d;
861 for (sav = LIST_FIRST(&sah->savtree[state]);
865 nextsav = LIST_NEXT(sav, chain);
868 KEY_CHKSASTATE(sav->state, state, "key_do_allocsa_policy");
871 if (candidate == NULL) {
876 /* Which SA is the better ? */
879 if (candidate->lft_c == NULL || sav->lft_c == NULL)
880 panic("key_do_allocsa_policy: "
881 "lifetime_current is NULL.\n");
883 /* What the best method is to compare ? */
884 if (key_prefered_oldsa) {
885 if (candidate->lft_c->sadb_lifetime_addtime >
886 sav->lft_c->sadb_lifetime_addtime) {
893 /* prefered new sa rather than old sa */
894 if (candidate->lft_c->sadb_lifetime_addtime <
895 sav->lft_c->sadb_lifetime_addtime) {
902 * prepared to delete the SA when there is more
903 * suitable candidate and the lifetime of the SA is not
906 if (d->lft_c->sadb_lifetime_addtime != 0) {
907 struct mbuf *m, *result;
909 key_sa_chgstate(d, SADB_SASTATE_DEAD);
911 KASSERT(d->refcnt > 0,
912 ("key_do_allocsa_policy: bogus ref count"));
913 m = key_setsadbmsg(SADB_DELETE, 0,
914 d->sah->saidx.proto, 0, 0, d->refcnt - 1);
919 /* set sadb_address for saidx's. */
920 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
921 &d->sah->saidx.src.sa,
922 d->sah->saidx.src.sa.sa_len << 3,
928 /* set sadb_address for saidx's. */
929 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
930 &d->sah->saidx.src.sa,
931 d->sah->saidx.src.sa.sa_len << 3,
937 /* create SA extension */
938 m = key_setsadbsa(d);
943 if (result->m_len < sizeof(struct sadb_msg)) {
944 result = m_pullup(result,
945 sizeof(struct sadb_msg));
950 result->m_pkthdr.len = 0;
951 for (m = result; m; m = m->m_next)
952 result->m_pkthdr.len += m->m_len;
953 mtod(result, struct sadb_msg *)->sadb_msg_len =
954 PFKEY_UNIT64(result->m_pkthdr.len);
956 if (key_sendup_mbuf(NULL, result,
957 KEY_SENDUP_REGISTERED))
965 SA_ADDREF(candidate);
966 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
967 printf("DP allocsa_policy cause "
968 "refcnt++:%d SA:%p\n",
969 candidate->refcnt, candidate));
975 * allocating a usable SA entry for a *INBOUND* packet.
976 * Must call key_freesav() later.
977 * OUT: positive: pointer to a usable sav (i.e. MATURE or DYING state).
978 * NULL: not found, or error occured.
980 * In the comparison, no source address is used--for RFC2401 conformance.
981 * To quote, from section 4.1:
982 * A security association is uniquely identified by a triple consisting
983 * of a Security Parameter Index (SPI), an IP Destination Address, and a
984 * security protocol (AH or ESP) identifier.
985 * Note that, however, we do need to keep source address in IPsec SA.
986 * IKE specification and PF_KEY specification do assume that we
987 * keep source address in IPsec SA. We see a tricky situation here.
991 union sockaddr_union *dst,
994 const char* where, int tag)
996 struct secashead *sah;
997 struct secasvar *sav;
998 u_int stateidx, state;
1001 KASSERT(dst != NULL, ("key_allocsa: null dst address"));
1003 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1004 printf("DP key_allocsa from %s:%u\n", where, tag));
1008 * XXX: to be checked internal IP header somewhere. Also when
1009 * IPsec tunnel packet is received. But ESP tunnel mode is
1010 * encrypted so we can't check internal IP header.
1012 s = splnet(); /*called from softclock()*/
1013 LIST_FOREACH(sah, &sahtree, chain) {
1014 /* search valid state */
1016 stateidx < _ARRAYLEN(saorder_state_valid);
1018 state = saorder_state_valid[stateidx];
1019 LIST_FOREACH(sav, &sah->savtree[state], chain) {
1021 KEY_CHKSASTATE(sav->state, state, "key_allocsav");
1022 /* do not return entries w/ unusable state */
1023 if (sav->state != SADB_SASTATE_MATURE &&
1024 sav->state != SADB_SASTATE_DYING)
1026 if (proto != sav->sah->saidx.proto)
1028 if (spi != sav->spi)
1030 #if 0 /* don't check src */
1031 /* check src address */
1032 if (key_sockaddrcmp(&src->sa, &sav->sah->saidx.src.sa, 0) != 0)
1035 /* check dst address */
1036 if (key_sockaddrcmp(&dst->sa, &sav->sah->saidx.dst.sa, 0) != 0)
1047 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1048 printf("DP key_allocsa return SA:%p; refcnt %u\n",
1049 sav, sav ? sav->refcnt : 0));
1054 * Must be called after calling key_allocsp().
1055 * For both the packet without socket and key_freeso().
1058 _key_freesp(struct secpolicy **spp, const char* where, int tag)
1060 struct secpolicy *sp = *spp;
1062 KASSERT(sp != NULL, ("key_freesp: null sp"));
1066 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1067 printf("DP key_freesp SP:%p (ID=%u) from %s:%u; refcnt now %u\n",
1068 sp, sp->id, where, tag, sp->refcnt));
1070 if (sp->refcnt == 0) {
1077 * Must be called after calling key_allocsp().
1078 * For the packet with socket.
1081 key_freeso(struct socket *so)
1084 KASSERT(so != NULL, ("key_freeso: null so"));
1086 switch (so->so_proto->pr_domain->dom_family) {
1090 struct inpcb *pcb = sotoinpcb(so);
1092 /* Does it have a PCB ? */
1095 key_freesp_so(&pcb->inp_sp->sp_in);
1096 key_freesp_so(&pcb->inp_sp->sp_out);
1103 #ifdef HAVE_NRL_INPCB
1104 struct inpcb *pcb = sotoinpcb(so);
1106 /* Does it have a PCB ? */
1109 key_freesp_so(&pcb->inp_sp->sp_in);
1110 key_freesp_so(&pcb->inp_sp->sp_out);
1112 struct in6pcb *pcb = sotoin6pcb(so);
1114 /* Does it have a PCB ? */
1117 key_freesp_so(&pcb->in6p_sp->sp_in);
1118 key_freesp_so(&pcb->in6p_sp->sp_out);
1124 ipseclog((LOG_DEBUG, "key_freeso: unknown address family=%d.\n",
1125 so->so_proto->pr_domain->dom_family));
1131 key_freesp_so(struct secpolicy **sp)
1133 KASSERT(sp != NULL && *sp != NULL, ("key_freesp_so: null sp"));
1135 if ((*sp)->policy == IPSEC_POLICY_ENTRUST ||
1136 (*sp)->policy == IPSEC_POLICY_BYPASS)
1139 KASSERT((*sp)->policy == IPSEC_POLICY_IPSEC,
1140 ("key_freesp_so: invalid policy %u", (*sp)->policy));
1145 * Must be called after calling key_allocsa().
1146 * This function is called by key_freesp() to free some SA allocated
1150 key_freesav(struct secasvar **psav, const char* where, int tag)
1152 struct secasvar *sav = *psav;
1154 KASSERT(sav != NULL, ("key_freesav: null sav"));
1158 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1159 printf("DP key_freesav SA:%p (SPI %lu) from %s:%u; refcnt now %u\n",
1160 sav, ntohl(sav->spi), where, tag, sav->refcnt));
1162 if (sav->refcnt == 0) {
1168 /* %%% SPD management */
1170 * free security policy entry.
1173 key_delsp(struct secpolicy *sp)
1177 KASSERT(sp != NULL, ("key_delsp: null sp"));
1179 sp->state = IPSEC_SPSTATE_DEAD;
1181 KASSERT(sp->refcnt == 0,
1182 ("key_delsp: SP with references deleted (refcnt %u)",
1185 s = splnet(); /*called from softclock()*/
1186 /* remove from SP index */
1187 if (__LIST_CHAINED(sp))
1188 LIST_REMOVE(sp, chain);
1191 struct ipsecrequest *isr = sp->req, *nextisr;
1193 while (isr != NULL) {
1194 if (isr->sav != NULL) {
1195 KEY_FREESAV(&isr->sav);
1199 nextisr = isr->next;
1212 * OUT: NULL : not found
1213 * others : found, pointer to a SP.
1215 static struct secpolicy *
1216 key_getsp(struct secpolicyindex *spidx)
1218 struct secpolicy *sp;
1220 KASSERT(spidx != NULL, ("key_getsp: null spidx"));
1222 LIST_FOREACH(sp, &sptree[spidx->dir], chain) {
1223 if (sp->state == IPSEC_SPSTATE_DEAD)
1225 if (key_cmpspidx_exactly(spidx, &sp->spidx)) {
1236 * OUT: NULL : not found
1237 * others : found, pointer to a SP.
1239 static struct secpolicy *
1240 key_getspbyid(u_int32_t id)
1242 struct secpolicy *sp;
1244 LIST_FOREACH(sp, &sptree[IPSEC_DIR_INBOUND], chain) {
1245 if (sp->state == IPSEC_SPSTATE_DEAD)
1253 LIST_FOREACH(sp, &sptree[IPSEC_DIR_OUTBOUND], chain) {
1254 if (sp->state == IPSEC_SPSTATE_DEAD)
1266 key_newsp(const char* where, int tag)
1268 struct secpolicy *newsp = NULL;
1270 newsp = (struct secpolicy *)
1271 malloc(sizeof(struct secpolicy), M_SECA, M_NOWAIT|M_ZERO);
1277 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1278 printf("DP key_newsp from %s:%u return SP:%p\n",
1279 where, tag, newsp));
1284 * create secpolicy structure from sadb_x_policy structure.
1285 * NOTE: `state', `secpolicyindex' in secpolicy structure are not set,
1286 * so must be set properly later.
1289 key_msg2sp(xpl0, len, error)
1290 struct sadb_x_policy *xpl0;
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.
1525 struct secpolicy *sp;
1527 struct sadb_x_policy *xpl;
1534 panic("key_sp2msg: NULL pointer was passed.\n");
1536 tlen = key_getspreqmsglen(sp);
1538 m = key_alloc_mbuf(tlen);
1539 if (!m || m->m_next) { /*XXX*/
1547 xpl = mtod(m, struct sadb_x_policy *);
1550 xpl->sadb_x_policy_len = PFKEY_UNIT64(tlen);
1551 xpl->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
1552 xpl->sadb_x_policy_type = sp->policy;
1553 xpl->sadb_x_policy_dir = sp->spidx.dir;
1554 xpl->sadb_x_policy_id = sp->id;
1555 p = (caddr_t)xpl + sizeof(*xpl);
1557 /* if is the policy for ipsec ? */
1558 if (sp->policy == IPSEC_POLICY_IPSEC) {
1559 struct sadb_x_ipsecrequest *xisr;
1560 struct ipsecrequest *isr;
1562 for (isr = sp->req; isr != NULL; isr = isr->next) {
1564 xisr = (struct sadb_x_ipsecrequest *)p;
1566 xisr->sadb_x_ipsecrequest_proto = isr->saidx.proto;
1567 xisr->sadb_x_ipsecrequest_mode = isr->saidx.mode;
1568 xisr->sadb_x_ipsecrequest_level = isr->level;
1569 xisr->sadb_x_ipsecrequest_reqid = isr->saidx.reqid;
1572 bcopy(&isr->saidx.src, p, isr->saidx.src.sa.sa_len);
1573 p += isr->saidx.src.sa.sa_len;
1574 bcopy(&isr->saidx.dst, p, isr->saidx.dst.sa.sa_len);
1575 p += isr->saidx.src.sa.sa_len;
1577 xisr->sadb_x_ipsecrequest_len =
1578 PFKEY_ALIGN8(sizeof(*xisr)
1579 + isr->saidx.src.sa.sa_len
1580 + isr->saidx.dst.sa.sa_len);
1587 /* m will not be freed nor modified */
1588 static struct mbuf *
1590 key_gather_mbuf(struct mbuf *m, const struct sadb_msghdr *mhp,
1591 int ndeep, int nitem, ...)
1593 key_gather_mbuf(m, mhp, ndeep, nitem, va_alist)
1595 const struct sadb_msghdr *mhp;
1604 struct mbuf *result = NULL, *n;
1607 if (m == NULL || mhp == NULL)
1608 panic("null pointer passed to key_gather");
1610 va_start(ap, nitem);
1611 for (i = 0; i < nitem; i++) {
1612 idx = va_arg(ap, int);
1613 if (idx < 0 || idx > SADB_EXT_MAX)
1615 /* don't attempt to pull empty extension */
1616 if (idx == SADB_EXT_RESERVED && mhp->msg == NULL)
1618 if (idx != SADB_EXT_RESERVED &&
1619 (mhp->ext[idx] == NULL || mhp->extlen[idx] == 0))
1622 if (idx == SADB_EXT_RESERVED) {
1623 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
1626 panic("assumption failed");
1628 MGETHDR(n, M_DONTWAIT, MT_DATA);
1633 m_copydata(m, 0, sizeof(struct sadb_msg),
1635 } else if (i < ndeep) {
1636 len = mhp->extlen[idx];
1637 n = key_alloc_mbuf(len);
1638 if (!n || n->m_next) { /*XXX*/
1643 m_copydata(m, mhp->extoff[idx], mhp->extlen[idx],
1646 n = m_copym(m, mhp->extoff[idx], mhp->extlen[idx],
1659 if ((result->m_flags & M_PKTHDR) != 0) {
1660 result->m_pkthdr.len = 0;
1661 for (n = result; n; n = n->m_next)
1662 result->m_pkthdr.len += n->m_len;
1673 * SADB_X_SPDADD, SADB_X_SPDSETIDX or SADB_X_SPDUPDATE processing
1674 * add an entry to SP database, when received
1675 * <base, address(SD), (lifetime(H),) policy>
1677 * Adding to SP database,
1679 * <base, address(SD), (lifetime(H),) policy>
1680 * to the socket which was send.
1682 * SPDADD set a unique policy entry.
1683 * SPDSETIDX like SPDADD without a part of policy requests.
1684 * SPDUPDATE replace a unique policy entry.
1686 * m will always be freed.
1689 key_spdadd(so, m, mhp)
1692 const struct sadb_msghdr *mhp;
1694 struct sadb_address *src0, *dst0;
1695 struct sadb_x_policy *xpl0, *xpl;
1696 struct sadb_lifetime *lft = NULL;
1697 struct secpolicyindex spidx;
1698 struct secpolicy *newsp;
1702 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
1703 panic("key_spdadd: NULL pointer is passed.\n");
1705 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
1706 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
1707 mhp->ext[SADB_X_EXT_POLICY] == NULL) {
1708 ipseclog((LOG_DEBUG, "key_spdadd: invalid message is passed.\n"));
1709 return key_senderror(so, m, EINVAL);
1711 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
1712 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
1713 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
1714 ipseclog((LOG_DEBUG, "key_spdadd: invalid message is passed.\n"));
1715 return key_senderror(so, m, EINVAL);
1717 if (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL) {
1718 if (mhp->extlen[SADB_EXT_LIFETIME_HARD]
1719 < sizeof(struct sadb_lifetime)) {
1720 ipseclog((LOG_DEBUG, "key_spdadd: invalid message is passed.\n"));
1721 return key_senderror(so, m, EINVAL);
1723 lft = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_HARD];
1726 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
1727 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
1728 xpl0 = (struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY];
1731 /* XXX boundary check against sa_len */
1732 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
1735 src0->sadb_address_prefixlen,
1736 dst0->sadb_address_prefixlen,
1737 src0->sadb_address_proto,
1740 /* checking the direciton. */
1741 switch (xpl0->sadb_x_policy_dir) {
1742 case IPSEC_DIR_INBOUND:
1743 case IPSEC_DIR_OUTBOUND:
1746 ipseclog((LOG_DEBUG, "key_spdadd: Invalid SP direction.\n"));
1747 mhp->msg->sadb_msg_errno = EINVAL;
1752 /* key_spdadd() accepts DISCARD, NONE and IPSEC. */
1753 if (xpl0->sadb_x_policy_type == IPSEC_POLICY_ENTRUST
1754 || xpl0->sadb_x_policy_type == IPSEC_POLICY_BYPASS) {
1755 ipseclog((LOG_DEBUG, "key_spdadd: Invalid policy type.\n"));
1756 return key_senderror(so, m, EINVAL);
1759 /* policy requests are mandatory when action is ipsec. */
1760 if (mhp->msg->sadb_msg_type != SADB_X_SPDSETIDX
1761 && xpl0->sadb_x_policy_type == IPSEC_POLICY_IPSEC
1762 && mhp->extlen[SADB_X_EXT_POLICY] <= sizeof(*xpl0)) {
1763 ipseclog((LOG_DEBUG, "key_spdadd: some policy requests part required.\n"));
1764 return key_senderror(so, m, EINVAL);
1768 * checking there is SP already or not.
1769 * SPDUPDATE doesn't depend on whether there is a SP or not.
1770 * If the type is either SPDADD or SPDSETIDX AND a SP is found,
1773 newsp = key_getsp(&spidx);
1774 if (mhp->msg->sadb_msg_type == SADB_X_SPDUPDATE) {
1776 newsp->state = IPSEC_SPSTATE_DEAD;
1780 if (newsp != NULL) {
1782 ipseclog((LOG_DEBUG, "key_spdadd: a SP entry exists already.\n"));
1783 return key_senderror(so, m, EEXIST);
1787 /* allocation new SP entry */
1788 if ((newsp = key_msg2sp(xpl0, PFKEY_EXTLEN(xpl0), &error)) == NULL) {
1789 return key_senderror(so, m, error);
1792 if ((newsp->id = key_getnewspid()) == 0) {
1794 return key_senderror(so, m, ENOBUFS);
1797 /* XXX boundary check against sa_len */
1798 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
1801 src0->sadb_address_prefixlen,
1802 dst0->sadb_address_prefixlen,
1803 src0->sadb_address_proto,
1806 /* sanity check on addr pair */
1807 if (((struct sockaddr *)(src0 + 1))->sa_family !=
1808 ((struct sockaddr *)(dst0+ 1))->sa_family) {
1810 return key_senderror(so, m, EINVAL);
1812 if (((struct sockaddr *)(src0 + 1))->sa_len !=
1813 ((struct sockaddr *)(dst0+ 1))->sa_len) {
1815 return key_senderror(so, m, EINVAL);
1818 if (newsp->req && newsp->req->saidx.src.sa.sa_family) {
1819 struct sockaddr *sa;
1820 sa = (struct sockaddr *)(src0 + 1);
1821 if (sa->sa_family != newsp->req->saidx.src.sa.sa_family) {
1823 return key_senderror(so, m, EINVAL);
1826 if (newsp->req && newsp->req->saidx.dst.sa.sa_family) {
1827 struct sockaddr *sa;
1828 sa = (struct sockaddr *)(dst0 + 1);
1829 if (sa->sa_family != newsp->req->saidx.dst.sa.sa_family) {
1831 return key_senderror(so, m, EINVAL);
1836 newsp->created = time_second;
1837 newsp->lastused = newsp->created;
1838 newsp->lifetime = lft ? lft->sadb_lifetime_addtime : 0;
1839 newsp->validtime = lft ? lft->sadb_lifetime_usetime : 0;
1841 newsp->refcnt = 1; /* do not reclaim until I say I do */
1842 newsp->state = IPSEC_SPSTATE_ALIVE;
1843 LIST_INSERT_TAIL(&sptree[newsp->spidx.dir], newsp, secpolicy, chain);
1845 /* delete the entry in spacqtree */
1846 if (mhp->msg->sadb_msg_type == SADB_X_SPDUPDATE) {
1847 struct secspacq *spacq;
1848 if ((spacq = key_getspacq(&spidx)) != NULL) {
1849 /* reset counter in order to deletion by timehandler. */
1850 spacq->created = time_second;
1856 struct mbuf *n, *mpolicy;
1857 struct sadb_msg *newmsg;
1860 /* create new sadb_msg to reply. */
1862 n = key_gather_mbuf(m, mhp, 2, 5, SADB_EXT_RESERVED,
1863 SADB_X_EXT_POLICY, SADB_EXT_LIFETIME_HARD,
1864 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
1866 n = key_gather_mbuf(m, mhp, 2, 4, SADB_EXT_RESERVED,
1868 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
1871 return key_senderror(so, m, ENOBUFS);
1873 if (n->m_len < sizeof(*newmsg)) {
1874 n = m_pullup(n, sizeof(*newmsg));
1876 return key_senderror(so, m, ENOBUFS);
1878 newmsg = mtod(n, struct sadb_msg *);
1879 newmsg->sadb_msg_errno = 0;
1880 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
1883 mpolicy = m_pulldown(n, PFKEY_ALIGN8(sizeof(struct sadb_msg)),
1884 sizeof(*xpl), &off);
1885 if (mpolicy == NULL) {
1886 /* n is already freed */
1887 return key_senderror(so, m, ENOBUFS);
1889 xpl = (struct sadb_x_policy *)(mtod(mpolicy, caddr_t) + off);
1890 if (xpl->sadb_x_policy_exttype != SADB_X_EXT_POLICY) {
1892 return key_senderror(so, m, EINVAL);
1894 xpl->sadb_x_policy_id = newsp->id;
1897 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
1902 * get new policy id.
1910 u_int32_t newid = 0;
1911 int count = key_spi_trycnt; /* XXX */
1912 struct secpolicy *sp;
1914 /* when requesting to allocate spi ranged */
1916 newid = (policy_id = (policy_id == ~0 ? 1 : policy_id + 1));
1918 if ((sp = key_getspbyid(newid)) == NULL)
1924 if (count == 0 || newid == 0) {
1925 ipseclog((LOG_DEBUG, "key_getnewspid: to allocate policy id is failed.\n"));
1933 * SADB_SPDDELETE processing
1935 * <base, address(SD), policy(*)>
1936 * from the user(?), and set SADB_SASTATE_DEAD,
1938 * <base, address(SD), policy(*)>
1940 * policy(*) including direction of policy.
1942 * m will always be freed.
1945 key_spddelete(so, m, mhp)
1948 const struct sadb_msghdr *mhp;
1950 struct sadb_address *src0, *dst0;
1951 struct sadb_x_policy *xpl0;
1952 struct secpolicyindex spidx;
1953 struct secpolicy *sp;
1956 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
1957 panic("key_spddelete: NULL pointer is passed.\n");
1959 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
1960 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
1961 mhp->ext[SADB_X_EXT_POLICY] == NULL) {
1962 ipseclog((LOG_DEBUG, "key_spddelete: invalid message is passed.\n"));
1963 return key_senderror(so, m, EINVAL);
1965 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
1966 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
1967 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
1968 ipseclog((LOG_DEBUG, "key_spddelete: invalid message is passed.\n"));
1969 return key_senderror(so, m, EINVAL);
1972 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
1973 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
1974 xpl0 = (struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY];
1977 /* XXX boundary check against sa_len */
1978 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
1981 src0->sadb_address_prefixlen,
1982 dst0->sadb_address_prefixlen,
1983 src0->sadb_address_proto,
1986 /* checking the direciton. */
1987 switch (xpl0->sadb_x_policy_dir) {
1988 case IPSEC_DIR_INBOUND:
1989 case IPSEC_DIR_OUTBOUND:
1992 ipseclog((LOG_DEBUG, "key_spddelete: Invalid SP direction.\n"));
1993 return key_senderror(so, m, EINVAL);
1996 /* Is there SP in SPD ? */
1997 if ((sp = key_getsp(&spidx)) == NULL) {
1998 ipseclog((LOG_DEBUG, "key_spddelete: no SP found.\n"));
1999 return key_senderror(so, m, EINVAL);
2002 /* save policy id to buffer to be returned. */
2003 xpl0->sadb_x_policy_id = sp->id;
2005 sp->state = IPSEC_SPSTATE_DEAD;
2010 struct sadb_msg *newmsg;
2012 /* create new sadb_msg to reply. */
2013 n = key_gather_mbuf(m, mhp, 1, 4, SADB_EXT_RESERVED,
2014 SADB_X_EXT_POLICY, SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
2016 return key_senderror(so, m, ENOBUFS);
2018 newmsg = mtod(n, struct sadb_msg *);
2019 newmsg->sadb_msg_errno = 0;
2020 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
2023 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
2028 * SADB_SPDDELETE2 processing
2031 * from the user(?), and set SADB_SASTATE_DEAD,
2035 * policy(*) including direction of policy.
2037 * m will always be freed.
2040 key_spddelete2(so, m, mhp)
2043 const struct sadb_msghdr *mhp;
2046 struct secpolicy *sp;
2049 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
2050 panic("key_spddelete2: NULL pointer is passed.\n");
2052 if (mhp->ext[SADB_X_EXT_POLICY] == NULL ||
2053 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2054 ipseclog((LOG_DEBUG, "key_spddelete2: invalid message is passed.\n"));
2055 key_senderror(so, m, EINVAL);
2059 id = ((struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY])->sadb_x_policy_id;
2061 /* Is there SP in SPD ? */
2062 if ((sp = key_getspbyid(id)) == NULL) {
2063 ipseclog((LOG_DEBUG, "key_spddelete2: no SP found id:%u.\n", id));
2064 key_senderror(so, m, EINVAL);
2067 sp->state = IPSEC_SPSTATE_DEAD;
2071 struct mbuf *n, *nn;
2072 struct sadb_msg *newmsg;
2075 /* create new sadb_msg to reply. */
2076 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
2079 return key_senderror(so, m, ENOBUFS);
2080 MGETHDR(n, M_DONTWAIT, MT_DATA);
2081 if (n && len > MHLEN) {
2082 MCLGET(n, M_DONTWAIT);
2083 if ((n->m_flags & M_EXT) == 0) {
2089 return key_senderror(so, m, ENOBUFS);
2095 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
2096 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
2100 panic("length inconsistency in key_spddelete2");
2103 n->m_next = m_copym(m, mhp->extoff[SADB_X_EXT_POLICY],
2104 mhp->extlen[SADB_X_EXT_POLICY], M_DONTWAIT);
2107 return key_senderror(so, m, ENOBUFS);
2110 n->m_pkthdr.len = 0;
2111 for (nn = n; nn; nn = nn->m_next)
2112 n->m_pkthdr.len += nn->m_len;
2114 newmsg = mtod(n, struct sadb_msg *);
2115 newmsg->sadb_msg_errno = 0;
2116 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
2119 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
2124 * SADB_X_GET processing
2129 * <base, address(SD), policy>
2131 * policy(*) including direction of policy.
2133 * m will always be freed.
2136 key_spdget(so, m, mhp)
2139 const struct sadb_msghdr *mhp;
2142 struct secpolicy *sp;
2146 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
2147 panic("key_spdget: NULL pointer is passed.\n");
2149 if (mhp->ext[SADB_X_EXT_POLICY] == NULL ||
2150 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2151 ipseclog((LOG_DEBUG, "key_spdget: invalid message is passed.\n"));
2152 return key_senderror(so, m, EINVAL);
2155 id = ((struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY])->sadb_x_policy_id;
2157 /* Is there SP in SPD ? */
2158 if ((sp = key_getspbyid(id)) == NULL) {
2159 ipseclog((LOG_DEBUG, "key_spdget: no SP found id:%u.\n", id));
2160 return key_senderror(so, m, ENOENT);
2163 n = key_setdumpsp(sp, SADB_X_SPDGET, 0, mhp->msg->sadb_msg_pid);
2166 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
2168 return key_senderror(so, m, ENOBUFS);
2172 * SADB_X_SPDACQUIRE processing.
2173 * Acquire policy and SA(s) for a *OUTBOUND* packet.
2176 * to KMD, and expect to receive
2177 * <base> with SADB_X_SPDACQUIRE if error occured,
2180 * with SADB_X_SPDUPDATE from KMD by PF_KEY.
2181 * policy(*) is without policy requests.
2184 * others: error number
2188 struct secpolicy *sp;
2190 struct mbuf *result = NULL, *m;
2191 struct secspacq *newspacq;
2196 panic("key_spdacquire: NULL pointer is passed.\n");
2197 if (sp->req != NULL)
2198 panic("key_spdacquire: called but there is request.\n");
2199 if (sp->policy != IPSEC_POLICY_IPSEC)
2200 panic("key_spdacquire: policy mismathed. IPsec is expected.\n");
2202 /* Get an entry to check whether sent message or not. */
2203 if ((newspacq = key_getspacq(&sp->spidx)) != NULL) {
2204 if (key_blockacq_count < newspacq->count) {
2205 /* reset counter and do send message. */
2206 newspacq->count = 0;
2208 /* increment counter and do nothing. */
2213 /* make new entry for blocking to send SADB_ACQUIRE. */
2214 if ((newspacq = key_newspacq(&sp->spidx)) == NULL)
2217 /* add to acqtree */
2218 LIST_INSERT_HEAD(&spacqtree, newspacq, chain);
2221 /* create new sadb_msg to reply. */
2222 m = key_setsadbmsg(SADB_X_SPDACQUIRE, 0, 0, 0, 0, 0);
2229 result->m_pkthdr.len = 0;
2230 for (m = result; m; m = m->m_next)
2231 result->m_pkthdr.len += m->m_len;
2233 mtod(result, struct sadb_msg *)->sadb_msg_len =
2234 PFKEY_UNIT64(result->m_pkthdr.len);
2236 return key_sendup_mbuf(NULL, m, KEY_SENDUP_REGISTERED);
2245 * SADB_SPDFLUSH processing
2248 * from the user, and free all entries in secpctree.
2252 * NOTE: what to do is only marking SADB_SASTATE_DEAD.
2254 * m will always be freed.
2257 key_spdflush(so, m, mhp)
2260 const struct sadb_msghdr *mhp;
2262 struct sadb_msg *newmsg;
2263 struct secpolicy *sp;
2267 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
2268 panic("key_spdflush: NULL pointer is passed.\n");
2270 if (m->m_len != PFKEY_ALIGN8(sizeof(struct sadb_msg)))
2271 return key_senderror(so, m, EINVAL);
2273 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2274 LIST_FOREACH(sp, &sptree[dir], chain) {
2275 sp->state = IPSEC_SPSTATE_DEAD;
2279 if (sizeof(struct sadb_msg) > m->m_len + M_TRAILINGSPACE(m)) {
2280 ipseclog((LOG_DEBUG, "key_spdflush: No more memory.\n"));
2281 return key_senderror(so, m, ENOBUFS);
2287 m->m_pkthdr.len = m->m_len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
2288 newmsg = mtod(m, struct sadb_msg *);
2289 newmsg->sadb_msg_errno = 0;
2290 newmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
2292 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
2296 * SADB_SPDDUMP processing
2299 * from the user, and dump all SP leaves
2304 * m will always be freed.
2307 key_spddump(so, m, mhp)
2310 const struct sadb_msghdr *mhp;
2312 struct secpolicy *sp;
2318 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
2319 panic("key_spddump: NULL pointer is passed.\n");
2321 /* search SPD entry and get buffer size. */
2323 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2324 LIST_FOREACH(sp, &sptree[dir], chain) {
2330 return key_senderror(so, m, ENOENT);
2332 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2333 LIST_FOREACH(sp, &sptree[dir], chain) {
2335 n = key_setdumpsp(sp, SADB_X_SPDDUMP, cnt,
2336 mhp->msg->sadb_msg_pid);
2339 key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
2347 static struct mbuf *
2348 key_setdumpsp(sp, type, seq, pid)
2349 struct secpolicy *sp;
2353 struct mbuf *result = NULL, *m;
2355 m = key_setsadbmsg(type, 0, SADB_SATYPE_UNSPEC, seq, pid, sp->refcnt);
2360 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
2361 &sp->spidx.src.sa, sp->spidx.prefs,
2362 sp->spidx.ul_proto);
2367 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
2368 &sp->spidx.dst.sa, sp->spidx.prefd,
2369 sp->spidx.ul_proto);
2379 if ((result->m_flags & M_PKTHDR) == 0)
2382 if (result->m_len < sizeof(struct sadb_msg)) {
2383 result = m_pullup(result, sizeof(struct sadb_msg));
2388 result->m_pkthdr.len = 0;
2389 for (m = result; m; m = m->m_next)
2390 result->m_pkthdr.len += m->m_len;
2392 mtod(result, struct sadb_msg *)->sadb_msg_len =
2393 PFKEY_UNIT64(result->m_pkthdr.len);
2403 * get PFKEY message length for security policy and request.
2406 key_getspreqmsglen(sp)
2407 struct secpolicy *sp;
2411 tlen = sizeof(struct sadb_x_policy);
2413 /* if is the policy for ipsec ? */
2414 if (sp->policy != IPSEC_POLICY_IPSEC)
2417 /* get length of ipsec requests */
2419 struct ipsecrequest *isr;
2422 for (isr = sp->req; isr != NULL; isr = isr->next) {
2423 len = sizeof(struct sadb_x_ipsecrequest)
2424 + isr->saidx.src.sa.sa_len
2425 + isr->saidx.dst.sa.sa_len;
2427 tlen += PFKEY_ALIGN8(len);
2435 * SADB_SPDEXPIRE processing
2437 * <base, address(SD), lifetime(CH), policy>
2441 * others : error number
2445 struct secpolicy *sp;
2448 struct mbuf *result = NULL, *m;
2451 struct sadb_lifetime *lt;
2453 /* XXX: Why do we lock ? */
2454 s = splnet(); /*called from softclock()*/
2458 panic("key_spdexpire: NULL pointer is passed.\n");
2460 /* set msg header */
2461 m = key_setsadbmsg(SADB_X_SPDEXPIRE, 0, 0, 0, 0, 0);
2468 /* create lifetime extension (current and hard) */
2469 len = PFKEY_ALIGN8(sizeof(*lt)) * 2;
2470 m = key_alloc_mbuf(len);
2471 if (!m || m->m_next) { /*XXX*/
2477 bzero(mtod(m, caddr_t), len);
2478 lt = mtod(m, struct sadb_lifetime *);
2479 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
2480 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
2481 lt->sadb_lifetime_allocations = 0;
2482 lt->sadb_lifetime_bytes = 0;
2483 lt->sadb_lifetime_addtime = sp->created;
2484 lt->sadb_lifetime_usetime = sp->lastused;
2485 lt = (struct sadb_lifetime *)(mtod(m, caddr_t) + len / 2);
2486 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
2487 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
2488 lt->sadb_lifetime_allocations = 0;
2489 lt->sadb_lifetime_bytes = 0;
2490 lt->sadb_lifetime_addtime = sp->lifetime;
2491 lt->sadb_lifetime_usetime = sp->validtime;
2494 /* set sadb_address for source */
2495 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
2497 sp->spidx.prefs, sp->spidx.ul_proto);
2504 /* set sadb_address for destination */
2505 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
2507 sp->spidx.prefd, sp->spidx.ul_proto);
2522 if ((result->m_flags & M_PKTHDR) == 0) {
2527 if (result->m_len < sizeof(struct sadb_msg)) {
2528 result = m_pullup(result, sizeof(struct sadb_msg));
2529 if (result == NULL) {
2535 result->m_pkthdr.len = 0;
2536 for (m = result; m; m = m->m_next)
2537 result->m_pkthdr.len += m->m_len;
2539 mtod(result, struct sadb_msg *)->sadb_msg_len =
2540 PFKEY_UNIT64(result->m_pkthdr.len);
2542 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
2551 /* %%% SAD management */
2553 * allocating a memory for new SA head, and copy from the values of mhp.
2554 * OUT: NULL : failure due to the lack of memory.
2555 * others : pointer to new SA head.
2557 static struct secashead *
2559 struct secasindex *saidx;
2561 struct secashead *newsah;
2563 KASSERT(saidx != NULL, ("key_newsaidx: null saidx"));
2565 newsah = (struct secashead *)
2566 malloc(sizeof(struct secashead), M_SECA, M_NOWAIT|M_ZERO);
2567 if (newsah != NULL) {
2569 for (i = 0; i < sizeof(newsah->savtree)/sizeof(newsah->savtree[0]); i++)
2570 LIST_INIT(&newsah->savtree[i]);
2571 newsah->saidx = *saidx;
2573 /* add to saidxtree */
2574 newsah->state = SADB_SASTATE_MATURE;
2575 LIST_INSERT_HEAD(&sahtree, newsah, chain);
2581 * delete SA index and all SA registerd.
2585 struct secashead *sah;
2587 struct secasvar *sav, *nextsav;
2588 u_int stateidx, state;
2594 panic("key_delsah: NULL pointer is passed.\n");
2596 s = splnet(); /*called from softclock()*/
2598 /* searching all SA registerd in the secindex. */
2600 stateidx < _ARRAYLEN(saorder_state_any);
2603 state = saorder_state_any[stateidx];
2604 for (sav = (struct secasvar *)LIST_FIRST(&sah->savtree[state]);
2608 nextsav = LIST_NEXT(sav, chain);
2610 if (sav->refcnt == 0) {
2612 KEY_CHKSASTATE(state, sav->state, "key_delsah");
2615 /* give up to delete this sa */
2621 /* don't delete sah only if there are savs. */
2627 if (sah->sa_route.ro_rt) {
2628 RTFREE(sah->sa_route.ro_rt);
2629 sah->sa_route.ro_rt = (struct rtentry *)NULL;
2632 /* remove from tree of SA index */
2633 if (__LIST_CHAINED(sah))
2634 LIST_REMOVE(sah, chain);
2643 * allocating a new SA with LARVAL state. key_add() and key_getspi() call,
2644 * and copy the values of mhp into new buffer.
2645 * When SAD message type is GETSPI:
2646 * to set sequence number from acq_seq++,
2647 * to set zero to SPI.
2648 * not to call key_setsava().
2650 * others : pointer to new secasvar.
2652 * does not modify mbuf. does not free mbuf on error.
2654 static struct secasvar *
2655 key_newsav(m, mhp, sah, errp, where, tag)
2657 const struct sadb_msghdr *mhp;
2658 struct secashead *sah;
2663 struct secasvar *newsav;
2664 const struct sadb_sa *xsa;
2667 if (m == NULL || mhp == NULL || mhp->msg == NULL || sah == NULL)
2668 panic("key_newsa: NULL pointer is passed.\n");
2670 KMALLOC(newsav, struct secasvar *, sizeof(struct secasvar));
2671 if (newsav == NULL) {
2672 ipseclog((LOG_DEBUG, "key_newsa: No more memory.\n"));
2676 bzero((caddr_t)newsav, sizeof(struct secasvar));
2678 switch (mhp->msg->sadb_msg_type) {
2682 #ifdef IPSEC_DOSEQCHECK
2683 /* sync sequence number */
2684 if (mhp->msg->sadb_msg_seq == 0)
2686 (acq_seq = (acq_seq == ~0 ? 1 : ++acq_seq));
2689 newsav->seq = mhp->msg->sadb_msg_seq;
2694 if (mhp->ext[SADB_EXT_SA] == NULL) {
2695 KFREE(newsav), newsav = NULL;
2696 ipseclog((LOG_DEBUG, "key_newsa: invalid message is passed.\n"));
2700 xsa = (const struct sadb_sa *)mhp->ext[SADB_EXT_SA];
2701 newsav->spi = xsa->sadb_sa_spi;
2702 newsav->seq = mhp->msg->sadb_msg_seq;
2705 KFREE(newsav), newsav = NULL;
2710 /* copy sav values */
2711 if (mhp->msg->sadb_msg_type != SADB_GETSPI) {
2712 *errp = key_setsaval(newsav, m, mhp);
2714 KFREE(newsav), newsav = NULL;
2720 newsav->created = time_second;
2721 newsav->pid = mhp->msg->sadb_msg_pid;
2726 newsav->state = SADB_SASTATE_LARVAL;
2727 LIST_INSERT_TAIL(&sah->savtree[SADB_SASTATE_LARVAL], newsav,
2730 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
2731 printf("DP key_newsav from %s:%u return SP:%p\n",
2732 where, tag, newsav));
2738 * free() SA variable entry.
2742 struct secasvar *sav;
2744 KASSERT(sav != NULL, ("key_delsav: null sav"));
2745 KASSERT(sav->refcnt == 0,
2746 ("key_delsav: reference count %u > 0", sav->refcnt));
2748 /* remove from SA header */
2749 if (__LIST_CHAINED(sav))
2750 LIST_REMOVE(sav, chain);
2752 if (sav->key_auth != NULL) {
2753 bzero(_KEYBUF(sav->key_auth), _KEYLEN(sav->key_auth));
2754 KFREE(sav->key_auth);
2755 sav->key_auth = NULL;
2757 if (sav->key_enc != NULL) {
2758 bzero(_KEYBUF(sav->key_enc), _KEYLEN(sav->key_enc));
2759 KFREE(sav->key_enc);
2760 sav->key_enc = NULL;
2763 bzero(sav->sched, sav->schedlen);
2767 if (sav->replay != NULL) {
2771 if (sav->lft_c != NULL) {
2775 if (sav->lft_h != NULL) {
2779 if (sav->lft_s != NULL) {
2783 if (sav->iv != NULL) {
2797 * others : found, pointer to a SA.
2799 static struct secashead *
2801 struct secasindex *saidx;
2803 struct secashead *sah;
2805 LIST_FOREACH(sah, &sahtree, chain) {
2806 if (sah->state == SADB_SASTATE_DEAD)
2808 if (key_cmpsaidx(&sah->saidx, saidx, CMP_REQID))
2816 * check not to be duplicated SPI.
2817 * NOTE: this function is too slow due to searching all SAD.
2820 * others : found, pointer to a SA.
2822 static struct secasvar *
2823 key_checkspidup(saidx, spi)
2824 struct secasindex *saidx;
2827 struct secashead *sah;
2828 struct secasvar *sav;
2830 /* check address family */
2831 if (saidx->src.sa.sa_family != saidx->dst.sa.sa_family) {
2832 ipseclog((LOG_DEBUG, "key_checkspidup: address family mismatched.\n"));
2837 LIST_FOREACH(sah, &sahtree, chain) {
2838 if (!key_ismyaddr((struct sockaddr *)&sah->saidx.dst))
2840 sav = key_getsavbyspi(sah, spi);
2849 * search SAD litmited alive SA, protocol, SPI.
2852 * others : found, pointer to a SA.
2854 static struct secasvar *
2855 key_getsavbyspi(sah, spi)
2856 struct secashead *sah;
2859 struct secasvar *sav;
2860 u_int stateidx, state;
2862 /* search all status */
2864 stateidx < _ARRAYLEN(saorder_state_alive);
2867 state = saorder_state_alive[stateidx];
2868 LIST_FOREACH(sav, &sah->savtree[state], chain) {
2871 if (sav->state != state) {
2872 ipseclog((LOG_DEBUG, "key_getsavbyspi: "
2873 "invalid sav->state (queue: %d SA: %d)\n",
2874 state, sav->state));
2878 if (sav->spi == spi)
2887 * copy SA values from PF_KEY message except *SPI, SEQ, PID, STATE and TYPE*.
2888 * You must update these if need.
2892 * does not modify mbuf. does not free mbuf on error.
2895 key_setsaval(sav, m, mhp)
2896 struct secasvar *sav;
2898 const struct sadb_msghdr *mhp;
2903 if (m == NULL || mhp == NULL || mhp->msg == NULL)
2904 panic("key_setsaval: NULL pointer is passed.\n");
2906 /* initialization */
2908 sav->key_auth = NULL;
2909 sav->key_enc = NULL;
2916 sav->tdb_xform = NULL; /* transform */
2917 sav->tdb_encalgxform = NULL; /* encoding algorithm */
2918 sav->tdb_authalgxform = NULL; /* authentication algorithm */
2919 sav->tdb_compalgxform = NULL; /* compression algorithm */
2922 if (mhp->ext[SADB_EXT_SA] != NULL) {
2923 const struct sadb_sa *sa0;
2925 sa0 = (const struct sadb_sa *)mhp->ext[SADB_EXT_SA];
2926 if (mhp->extlen[SADB_EXT_SA] < sizeof(*sa0)) {
2931 sav->alg_auth = sa0->sadb_sa_auth;
2932 sav->alg_enc = sa0->sadb_sa_encrypt;
2933 sav->flags = sa0->sadb_sa_flags;
2936 if ((sa0->sadb_sa_flags & SADB_X_EXT_OLD) == 0) {
2937 sav->replay = (struct secreplay *)
2938 malloc(sizeof(struct secreplay)+sa0->sadb_sa_replay, M_SECA, M_NOWAIT|M_ZERO);
2939 if (sav->replay == NULL) {
2940 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
2944 if (sa0->sadb_sa_replay != 0)
2945 sav->replay->bitmap = (caddr_t)(sav->replay+1);
2946 sav->replay->wsize = sa0->sadb_sa_replay;
2950 /* Authentication keys */
2951 if (mhp->ext[SADB_EXT_KEY_AUTH] != NULL) {
2952 const struct sadb_key *key0;
2955 key0 = (const struct sadb_key *)mhp->ext[SADB_EXT_KEY_AUTH];
2956 len = mhp->extlen[SADB_EXT_KEY_AUTH];
2959 if (len < sizeof(*key0)) {
2963 switch (mhp->msg->sadb_msg_satype) {
2964 case SADB_SATYPE_AH:
2965 case SADB_SATYPE_ESP:
2966 if (len == PFKEY_ALIGN8(sizeof(struct sadb_key)) &&
2967 sav->alg_auth != SADB_X_AALG_NULL)
2970 case SADB_X_SATYPE_IPCOMP:
2976 ipseclog((LOG_DEBUG, "key_setsaval: invalid key_auth values.\n"));
2980 sav->key_auth = (struct sadb_key *)key_newbuf(key0, len);
2981 if (sav->key_auth == NULL) {
2982 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
2988 /* Encryption key */
2989 if (mhp->ext[SADB_EXT_KEY_ENCRYPT] != NULL) {
2990 const struct sadb_key *key0;
2993 key0 = (const struct sadb_key *)mhp->ext[SADB_EXT_KEY_ENCRYPT];
2994 len = mhp->extlen[SADB_EXT_KEY_ENCRYPT];
2997 if (len < sizeof(*key0)) {
3001 switch (mhp->msg->sadb_msg_satype) {
3002 case SADB_SATYPE_ESP:
3003 if (len == PFKEY_ALIGN8(sizeof(struct sadb_key)) &&
3004 sav->alg_enc != SADB_EALG_NULL) {
3008 sav->key_enc = (struct sadb_key *)key_newbuf(key0, len);
3009 if (sav->key_enc == NULL) {
3010 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
3015 case SADB_X_SATYPE_IPCOMP:
3016 if (len != PFKEY_ALIGN8(sizeof(struct sadb_key)))
3018 sav->key_enc = NULL; /*just in case*/
3020 case SADB_SATYPE_AH:
3026 ipseclog((LOG_DEBUG, "key_setsatval: invalid key_enc value.\n"));
3034 switch (mhp->msg->sadb_msg_satype) {
3035 case SADB_SATYPE_AH:
3036 error = xform_init(sav, XF_AH);
3038 case SADB_SATYPE_ESP:
3039 error = xform_init(sav, XF_ESP);
3041 case SADB_X_SATYPE_IPCOMP:
3042 error = xform_init(sav, XF_IPCOMP);
3046 ipseclog((LOG_DEBUG,
3047 "key_setsaval: unable to initialize SA type %u.\n",
3048 mhp->msg->sadb_msg_satype));
3053 sav->created = time_second;
3055 /* make lifetime for CURRENT */
3056 KMALLOC(sav->lft_c, struct sadb_lifetime *,
3057 sizeof(struct sadb_lifetime));
3058 if (sav->lft_c == NULL) {
3059 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
3064 sav->lft_c->sadb_lifetime_len =
3065 PFKEY_UNIT64(sizeof(struct sadb_lifetime));
3066 sav->lft_c->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
3067 sav->lft_c->sadb_lifetime_allocations = 0;
3068 sav->lft_c->sadb_lifetime_bytes = 0;
3069 sav->lft_c->sadb_lifetime_addtime = time_second;
3070 sav->lft_c->sadb_lifetime_usetime = 0;
3072 /* lifetimes for HARD and SOFT */
3074 const struct sadb_lifetime *lft0;
3076 lft0 = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_HARD];
3078 if (mhp->extlen[SADB_EXT_LIFETIME_HARD] < sizeof(*lft0)) {
3082 sav->lft_h = (struct sadb_lifetime *)key_newbuf(lft0,
3084 if (sav->lft_h == NULL) {
3085 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
3089 /* to be initialize ? */
3092 lft0 = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_SOFT];
3094 if (mhp->extlen[SADB_EXT_LIFETIME_SOFT] < sizeof(*lft0)) {
3098 sav->lft_s = (struct sadb_lifetime *)key_newbuf(lft0,
3100 if (sav->lft_s == NULL) {
3101 ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
3105 /* to be initialize ? */
3112 /* initialization */
3113 if (sav->replay != NULL) {
3117 if (sav->key_auth != NULL) {
3118 KFREE(sav->key_auth);
3119 sav->key_auth = NULL;
3121 if (sav->key_enc != NULL) {
3122 KFREE(sav->key_enc);
3123 sav->key_enc = NULL;
3129 if (sav->iv != NULL) {
3133 if (sav->lft_c != NULL) {
3137 if (sav->lft_h != NULL) {
3141 if (sav->lft_s != NULL) {
3150 * validation with a secasvar entry, and set SADB_SATYPE_MATURE.
3156 struct secasvar *sav;
3160 /* check SPI value */
3161 switch (sav->sah->saidx.proto) {
3164 if (ntohl(sav->spi) >= 0 && ntohl(sav->spi) <= 255) {
3165 ipseclog((LOG_DEBUG,
3166 "key_mature: illegal range of SPI %u.\n",
3167 (u_int32_t)ntohl(sav->spi)));
3174 switch (sav->sah->saidx.proto) {
3177 if ((sav->flags & (SADB_X_EXT_OLD|SADB_X_EXT_DERIV)) ==
3178 (SADB_X_EXT_OLD|SADB_X_EXT_DERIV)) {
3179 ipseclog((LOG_DEBUG, "key_mature: "
3180 "invalid flag (derived) given to old-esp.\n"));
3183 error = xform_init(sav, XF_ESP);
3187 if (sav->flags & SADB_X_EXT_DERIV) {
3188 ipseclog((LOG_DEBUG, "key_mature: "
3189 "invalid flag (derived) given to AH SA.\n"));
3192 if (sav->alg_enc != SADB_EALG_NONE) {
3193 ipseclog((LOG_DEBUG, "key_mature: "
3194 "protocol and algorithm mismated.\n"));
3197 error = xform_init(sav, XF_AH);
3199 case IPPROTO_IPCOMP:
3200 if (sav->alg_auth != SADB_AALG_NONE) {
3201 ipseclog((LOG_DEBUG, "key_mature: "
3202 "protocol and algorithm mismated.\n"));
3205 if ((sav->flags & SADB_X_EXT_RAWCPI) == 0
3206 && ntohl(sav->spi) >= 0x10000) {
3207 ipseclog((LOG_DEBUG, "key_mature: invalid cpi for IPComp.\n"));
3210 error = xform_init(sav, XF_IPCOMP);
3213 ipseclog((LOG_DEBUG, "key_mature: Invalid satype.\n"));
3214 error = EPROTONOSUPPORT;
3218 key_sa_chgstate(sav, SADB_SASTATE_MATURE);
3223 * subroutine for SADB_GET and SADB_DUMP.
3225 static struct mbuf *
3226 key_setdumpsa(sav, type, satype, seq, pid)
3227 struct secasvar *sav;
3228 u_int8_t type, satype;
3231 struct mbuf *result = NULL, *tres = NULL, *m;
3236 SADB_EXT_SA, SADB_X_EXT_SA2,
3237 SADB_EXT_LIFETIME_HARD, SADB_EXT_LIFETIME_SOFT,
3238 SADB_EXT_LIFETIME_CURRENT, SADB_EXT_ADDRESS_SRC,
3239 SADB_EXT_ADDRESS_DST, SADB_EXT_ADDRESS_PROXY, SADB_EXT_KEY_AUTH,
3240 SADB_EXT_KEY_ENCRYPT, SADB_EXT_IDENTITY_SRC,
3241 SADB_EXT_IDENTITY_DST, SADB_EXT_SENSITIVITY,
3244 m = key_setsadbmsg(type, 0, satype, seq, pid, sav->refcnt);
3249 for (i = sizeof(dumporder)/sizeof(dumporder[0]) - 1; i >= 0; i--) {
3252 switch (dumporder[i]) {
3254 m = key_setsadbsa(sav);
3259 case SADB_X_EXT_SA2:
3260 m = key_setsadbxsa2(sav->sah->saidx.mode,
3261 sav->replay ? sav->replay->count : 0,
3262 sav->sah->saidx.reqid);
3267 case SADB_EXT_ADDRESS_SRC:
3268 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
3269 &sav->sah->saidx.src.sa,
3270 FULLMASK, IPSEC_ULPROTO_ANY);
3275 case SADB_EXT_ADDRESS_DST:
3276 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
3277 &sav->sah->saidx.dst.sa,
3278 FULLMASK, IPSEC_ULPROTO_ANY);
3283 case SADB_EXT_KEY_AUTH:
3286 l = PFKEY_UNUNIT64(sav->key_auth->sadb_key_len);
3290 case SADB_EXT_KEY_ENCRYPT:
3293 l = PFKEY_UNUNIT64(sav->key_enc->sadb_key_len);
3297 case SADB_EXT_LIFETIME_CURRENT:
3300 l = PFKEY_UNUNIT64(((struct sadb_ext *)sav->lft_c)->sadb_ext_len);
3304 case SADB_EXT_LIFETIME_HARD:
3307 l = PFKEY_UNUNIT64(((struct sadb_ext *)sav->lft_h)->sadb_ext_len);
3311 case SADB_EXT_LIFETIME_SOFT:
3314 l = PFKEY_UNUNIT64(((struct sadb_ext *)sav->lft_s)->sadb_ext_len);
3318 case SADB_EXT_ADDRESS_PROXY:
3319 case SADB_EXT_IDENTITY_SRC:
3320 case SADB_EXT_IDENTITY_DST:
3321 /* XXX: should we brought from SPD ? */
3322 case SADB_EXT_SENSITIVITY:
3327 if ((!m && !p) || (m && p))
3330 M_PREPEND(tres, l, M_DONTWAIT);
3333 bcopy(p, mtod(tres, caddr_t), l);
3337 m = key_alloc_mbuf(l);
3340 m_copyback(m, 0, l, p);
3348 m_cat(result, tres);
3350 if (result->m_len < sizeof(struct sadb_msg)) {
3351 result = m_pullup(result, sizeof(struct sadb_msg));
3356 result->m_pkthdr.len = 0;
3357 for (m = result; m; m = m->m_next)
3358 result->m_pkthdr.len += m->m_len;
3360 mtod(result, struct sadb_msg *)->sadb_msg_len =
3361 PFKEY_UNIT64(result->m_pkthdr.len);
3372 * set data into sadb_msg.
3374 static struct mbuf *
3375 key_setsadbmsg(type, tlen, satype, seq, pid, reserved)
3376 u_int8_t type, satype;
3386 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
3389 MGETHDR(m, M_DONTWAIT, MT_DATA);
3390 if (m && len > MHLEN) {
3391 MCLGET(m, M_DONTWAIT);
3392 if ((m->m_flags & M_EXT) == 0) {
3399 m->m_pkthdr.len = m->m_len = len;
3402 p = mtod(m, struct sadb_msg *);
3405 p->sadb_msg_version = PF_KEY_V2;
3406 p->sadb_msg_type = type;
3407 p->sadb_msg_errno = 0;
3408 p->sadb_msg_satype = satype;
3409 p->sadb_msg_len = PFKEY_UNIT64(tlen);
3410 p->sadb_msg_reserved = reserved;
3411 p->sadb_msg_seq = seq;
3412 p->sadb_msg_pid = (u_int32_t)pid;
3418 * copy secasvar data into sadb_address.
3420 static struct mbuf *
3422 struct secasvar *sav;
3428 len = PFKEY_ALIGN8(sizeof(struct sadb_sa));
3429 m = key_alloc_mbuf(len);
3430 if (!m || m->m_next) { /*XXX*/
3436 p = mtod(m, struct sadb_sa *);
3439 p->sadb_sa_len = PFKEY_UNIT64(len);
3440 p->sadb_sa_exttype = SADB_EXT_SA;
3441 p->sadb_sa_spi = sav->spi;
3442 p->sadb_sa_replay = (sav->replay != NULL ? sav->replay->wsize : 0);
3443 p->sadb_sa_state = sav->state;
3444 p->sadb_sa_auth = sav->alg_auth;
3445 p->sadb_sa_encrypt = sav->alg_enc;
3446 p->sadb_sa_flags = sav->flags;
3452 * set data into sadb_address.
3454 static struct mbuf *
3455 key_setsadbaddr(exttype, saddr, prefixlen, ul_proto)
3457 const struct sockaddr *saddr;
3462 struct sadb_address *p;
3465 len = PFKEY_ALIGN8(sizeof(struct sadb_address)) +
3466 PFKEY_ALIGN8(saddr->sa_len);
3467 m = key_alloc_mbuf(len);
3468 if (!m || m->m_next) { /*XXX*/
3474 p = mtod(m, struct sadb_address *);
3477 p->sadb_address_len = PFKEY_UNIT64(len);
3478 p->sadb_address_exttype = exttype;
3479 p->sadb_address_proto = ul_proto;
3480 if (prefixlen == FULLMASK) {
3481 switch (saddr->sa_family) {
3483 prefixlen = sizeof(struct in_addr) << 3;
3486 prefixlen = sizeof(struct in6_addr) << 3;
3492 p->sadb_address_prefixlen = prefixlen;
3493 p->sadb_address_reserved = 0;
3496 mtod(m, caddr_t) + PFKEY_ALIGN8(sizeof(struct sadb_address)),
3504 * set data into sadb_ident.
3506 static struct mbuf *
3507 key_setsadbident(exttype, idtype, string, stringlen, id)
3508 u_int16_t exttype, idtype;
3514 struct sadb_ident *p;
3517 len = PFKEY_ALIGN8(sizeof(struct sadb_ident)) + PFKEY_ALIGN8(stringlen);
3518 m = key_alloc_mbuf(len);
3519 if (!m || m->m_next) { /*XXX*/
3525 p = mtod(m, struct sadb_ident *);
3528 p->sadb_ident_len = PFKEY_UNIT64(len);
3529 p->sadb_ident_exttype = exttype;
3530 p->sadb_ident_type = idtype;
3531 p->sadb_ident_reserved = 0;
3532 p->sadb_ident_id = id;
3535 mtod(m, caddr_t) + PFKEY_ALIGN8(sizeof(struct sadb_ident)),
3543 * set data into sadb_x_sa2.
3545 static struct mbuf *
3546 key_setsadbxsa2(mode, seq, reqid)
3548 u_int32_t seq, reqid;
3551 struct sadb_x_sa2 *p;
3554 len = PFKEY_ALIGN8(sizeof(struct sadb_x_sa2));
3555 m = key_alloc_mbuf(len);
3556 if (!m || m->m_next) { /*XXX*/
3562 p = mtod(m, struct sadb_x_sa2 *);
3565 p->sadb_x_sa2_len = PFKEY_UNIT64(len);
3566 p->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
3567 p->sadb_x_sa2_mode = mode;
3568 p->sadb_x_sa2_reserved1 = 0;
3569 p->sadb_x_sa2_reserved2 = 0;
3570 p->sadb_x_sa2_sequence = seq;
3571 p->sadb_x_sa2_reqid = reqid;
3577 * set data into sadb_x_policy
3579 static struct mbuf *
3580 key_setsadbxpolicy(type, dir, id)
3586 struct sadb_x_policy *p;
3589 len = PFKEY_ALIGN8(sizeof(struct sadb_x_policy));
3590 m = key_alloc_mbuf(len);
3591 if (!m || m->m_next) { /*XXX*/
3597 p = mtod(m, struct sadb_x_policy *);
3600 p->sadb_x_policy_len = PFKEY_UNIT64(len);
3601 p->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3602 p->sadb_x_policy_type = type;
3603 p->sadb_x_policy_dir = dir;
3604 p->sadb_x_policy_id = id;
3611 * copy a buffer into the new buffer allocated.
3614 key_newbuf(src, len)
3620 KMALLOC(new, caddr_t, len);
3622 ipseclog((LOG_DEBUG, "key_newbuf: No more memory.\n"));
3625 bcopy(src, new, len);
3630 /* compare my own address
3631 * OUT: 1: true, i.e. my address.
3636 struct sockaddr *sa;
3639 struct sockaddr_in *sin;
3640 struct in_ifaddr *ia;
3645 panic("key_ismyaddr: NULL pointer is passed.\n");
3647 switch (sa->sa_family) {
3650 sin = (struct sockaddr_in *)sa;
3651 for (ia = in_ifaddrhead.tqh_first; ia;
3652 ia = ia->ia_link.tqe_next)
3654 if (sin->sin_family == ia->ia_addr.sin_family &&
3655 sin->sin_len == ia->ia_addr.sin_len &&
3656 sin->sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr)
3665 return key_ismyaddr6((struct sockaddr_in6 *)sa);
3674 * compare my own address for IPv6.
3677 * NOTE: derived ip6_input() in KAME. This is necessary to modify more.
3679 #include <netinet6/in6_var.h>
3683 struct sockaddr_in6 *sin6;
3685 struct in6_ifaddr *ia;
3686 struct in6_multi *in6m;
3688 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
3689 if (key_sockaddrcmp((struct sockaddr *)&sin6,
3690 (struct sockaddr *)&ia->ia_addr, 0) == 0)
3695 * XXX why do we care about multlicast here while we don't care
3696 * about IPv4 multicast??
3700 IN6_LOOKUP_MULTI(sin6->sin6_addr, ia->ia_ifp, in6m);
3705 /* loopback, just for safety */
3706 if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
3714 * compare two secasindex structure.
3715 * flag can specify to compare 2 saidxes.
3716 * compare two secasindex structure without both mode and reqid.
3717 * don't compare port.
3719 * saidx0: source, it can be in SAD.
3727 const struct secasindex *saidx0,
3728 const struct secasindex *saidx1,
3732 if (saidx0 == NULL && saidx1 == NULL)
3735 if (saidx0 == NULL || saidx1 == NULL)
3738 if (saidx0->proto != saidx1->proto)
3741 if (flag == CMP_EXACTLY) {
3742 if (saidx0->mode != saidx1->mode)
3744 if (saidx0->reqid != saidx1->reqid)
3746 if (bcmp(&saidx0->src, &saidx1->src, saidx0->src.sa.sa_len) != 0 ||
3747 bcmp(&saidx0->dst, &saidx1->dst, saidx0->dst.sa.sa_len) != 0)
3751 /* CMP_MODE_REQID, CMP_REQID, CMP_HEAD */
3752 if (flag == CMP_MODE_REQID
3753 ||flag == CMP_REQID) {
3755 * If reqid of SPD is non-zero, unique SA is required.
3756 * The result must be of same reqid in this case.
3758 if (saidx1->reqid != 0 && saidx0->reqid != saidx1->reqid)
3762 if (flag == CMP_MODE_REQID) {
3763 if (saidx0->mode != IPSEC_MODE_ANY
3764 && saidx0->mode != saidx1->mode)
3768 if (key_sockaddrcmp(&saidx0->src.sa, &saidx1->src.sa, 0) != 0) {
3771 if (key_sockaddrcmp(&saidx0->dst.sa, &saidx1->dst.sa, 0) != 0) {
3780 * compare two secindex structure exactly.
3782 * spidx0: source, it is often in SPD.
3783 * spidx1: object, it is often from PFKEY message.
3789 key_cmpspidx_exactly(
3790 struct secpolicyindex *spidx0,
3791 struct secpolicyindex *spidx1)
3794 if (spidx0 == NULL && spidx1 == NULL)
3797 if (spidx0 == NULL || spidx1 == NULL)
3800 if (spidx0->prefs != spidx1->prefs
3801 || spidx0->prefd != spidx1->prefd
3802 || spidx0->ul_proto != spidx1->ul_proto)
3805 return key_sockaddrcmp(&spidx0->src.sa, &spidx1->src.sa, 1) == 0 &&
3806 key_sockaddrcmp(&spidx0->dst.sa, &spidx1->dst.sa, 1) == 0;
3810 * compare two secindex structure with mask.
3812 * spidx0: source, it is often in SPD.
3813 * spidx1: object, it is often from IP header.
3819 key_cmpspidx_withmask(
3820 struct secpolicyindex *spidx0,
3821 struct secpolicyindex *spidx1)
3824 if (spidx0 == NULL && spidx1 == NULL)
3827 if (spidx0 == NULL || spidx1 == NULL)
3830 if (spidx0->src.sa.sa_family != spidx1->src.sa.sa_family ||
3831 spidx0->dst.sa.sa_family != spidx1->dst.sa.sa_family ||
3832 spidx0->src.sa.sa_len != spidx1->src.sa.sa_len ||
3833 spidx0->dst.sa.sa_len != spidx1->dst.sa.sa_len)
3836 /* if spidx.ul_proto == IPSEC_ULPROTO_ANY, ignore. */
3837 if (spidx0->ul_proto != (u_int16_t)IPSEC_ULPROTO_ANY
3838 && spidx0->ul_proto != spidx1->ul_proto)
3841 switch (spidx0->src.sa.sa_family) {
3843 if (spidx0->src.sin.sin_port != IPSEC_PORT_ANY
3844 && spidx0->src.sin.sin_port != spidx1->src.sin.sin_port)
3846 if (!key_bbcmp(&spidx0->src.sin.sin_addr,
3847 &spidx1->src.sin.sin_addr, spidx0->prefs))
3851 if (spidx0->src.sin6.sin6_port != IPSEC_PORT_ANY
3852 && spidx0->src.sin6.sin6_port != spidx1->src.sin6.sin6_port)
3855 * scope_id check. if sin6_scope_id is 0, we regard it
3856 * as a wildcard scope, which matches any scope zone ID.
3858 if (spidx0->src.sin6.sin6_scope_id &&
3859 spidx1->src.sin6.sin6_scope_id &&
3860 spidx0->src.sin6.sin6_scope_id != spidx1->src.sin6.sin6_scope_id)
3862 if (!key_bbcmp(&spidx0->src.sin6.sin6_addr,
3863 &spidx1->src.sin6.sin6_addr, spidx0->prefs))
3868 if (bcmp(&spidx0->src, &spidx1->src, spidx0->src.sa.sa_len) != 0)
3873 switch (spidx0->dst.sa.sa_family) {
3875 if (spidx0->dst.sin.sin_port != IPSEC_PORT_ANY
3876 && spidx0->dst.sin.sin_port != spidx1->dst.sin.sin_port)
3878 if (!key_bbcmp(&spidx0->dst.sin.sin_addr,
3879 &spidx1->dst.sin.sin_addr, spidx0->prefd))
3883 if (spidx0->dst.sin6.sin6_port != IPSEC_PORT_ANY
3884 && spidx0->dst.sin6.sin6_port != spidx1->dst.sin6.sin6_port)
3887 * scope_id check. if sin6_scope_id is 0, we regard it
3888 * as a wildcard scope, which matches any scope zone ID.
3890 if (spidx0->src.sin6.sin6_scope_id &&
3891 spidx1->src.sin6.sin6_scope_id &&
3892 spidx0->dst.sin6.sin6_scope_id != spidx1->dst.sin6.sin6_scope_id)
3894 if (!key_bbcmp(&spidx0->dst.sin6.sin6_addr,
3895 &spidx1->dst.sin6.sin6_addr, spidx0->prefd))
3900 if (bcmp(&spidx0->dst, &spidx1->dst, spidx0->dst.sa.sa_len) != 0)
3905 /* XXX Do we check other field ? e.g. flowinfo */
3910 /* returns 0 on match */
3913 const struct sockaddr *sa1,
3914 const struct sockaddr *sa2,
3920 #define satosin(s) ((const struct sockaddr_in *)s)
3924 #define satosin6(s) ((const struct sockaddr_in6 *)s)
3925 if (sa1->sa_family != sa2->sa_family || sa1->sa_len != sa2->sa_len)
3928 switch (sa1->sa_family) {
3930 if (sa1->sa_len != sizeof(struct sockaddr_in))
3932 if (satosin(sa1)->sin_addr.s_addr !=
3933 satosin(sa2)->sin_addr.s_addr) {
3936 if (port && satosin(sa1)->sin_port != satosin(sa2)->sin_port)
3940 if (sa1->sa_len != sizeof(struct sockaddr_in6))
3941 return 1; /*EINVAL*/
3942 if (satosin6(sa1)->sin6_scope_id !=
3943 satosin6(sa2)->sin6_scope_id) {
3946 if (!IN6_ARE_ADDR_EQUAL(&satosin6(sa1)->sin6_addr,
3947 &satosin6(sa2)->sin6_addr)) {
3951 satosin6(sa1)->sin6_port != satosin6(sa2)->sin6_port) {
3955 if (bcmp(sa1, sa2, sa1->sa_len) != 0)
3966 * compare two buffers with mask.
3970 * bits: Number of bits to compare
3976 key_bbcmp(const void *a1, const void *a2, u_int bits)
3978 const unsigned char *p1 = a1;
3979 const unsigned char *p2 = a2;
3981 /* XXX: This could be considerably faster if we compare a word
3982 * at a time, but it is complicated on LSB Endian machines */
3984 /* Handle null pointers */
3985 if (p1 == NULL || p2 == NULL)
3995 u_int8_t mask = ~((1<<(8-bits))-1);
3996 if ((*p1 & mask) != (*p2 & mask))
3999 return 1; /* Match! */
4004 * scanning SPD and SAD to check status for each entries,
4005 * and do to remove or to expire.
4006 * XXX: year 2038 problem may remain.
4009 key_timehandler(void)
4013 time_t now = time_second;
4015 s = splnet(); /*called from softclock()*/
4019 struct secpolicy *sp, *nextsp;
4021 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
4022 for (sp = LIST_FIRST(&sptree[dir]);
4026 nextsp = LIST_NEXT(sp, chain);
4028 if (sp->state == IPSEC_SPSTATE_DEAD) {
4033 if (sp->lifetime == 0 && sp->validtime == 0)
4036 /* the deletion will occur next time */
4037 if ((sp->lifetime && now - sp->created > sp->lifetime)
4038 || (sp->validtime && now - sp->lastused > sp->validtime)) {
4039 sp->state = IPSEC_SPSTATE_DEAD;
4049 struct secashead *sah, *nextsah;
4050 struct secasvar *sav, *nextsav;
4052 for (sah = LIST_FIRST(&sahtree);
4056 nextsah = LIST_NEXT(sah, chain);
4058 /* if sah has been dead, then delete it and process next sah. */
4059 if (sah->state == SADB_SASTATE_DEAD) {
4064 /* if LARVAL entry doesn't become MATURE, delete it. */
4065 for (sav = LIST_FIRST(&sah->savtree[SADB_SASTATE_LARVAL]);
4069 nextsav = LIST_NEXT(sav, chain);
4071 if (now - sav->created > key_larval_lifetime) {
4077 * check MATURE entry to start to send expire message
4080 for (sav = LIST_FIRST(&sah->savtree[SADB_SASTATE_MATURE]);
4084 nextsav = LIST_NEXT(sav, chain);
4086 /* we don't need to check. */
4087 if (sav->lft_s == NULL)
4091 if (sav->lft_c == NULL) {
4092 ipseclog((LOG_DEBUG,"key_timehandler: "
4093 "There is no CURRENT time, why?\n"));
4097 /* check SOFT lifetime */
4098 if (sav->lft_s->sadb_lifetime_addtime != 0
4099 && now - sav->created > sav->lft_s->sadb_lifetime_addtime) {
4101 * check SA to be used whether or not.
4102 * when SA hasn't been used, delete it.
4104 if (sav->lft_c->sadb_lifetime_usetime == 0) {
4105 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4108 key_sa_chgstate(sav, SADB_SASTATE_DYING);
4110 * XXX If we keep to send expire
4111 * message in the status of
4112 * DYING. Do remove below code.
4117 /* check SOFT lifetime by bytes */
4119 * XXX I don't know the way to delete this SA
4120 * when new SA is installed. Caution when it's
4121 * installed too big lifetime by time.
4123 else if (sav->lft_s->sadb_lifetime_bytes != 0
4124 && sav->lft_s->sadb_lifetime_bytes < sav->lft_c->sadb_lifetime_bytes) {
4126 key_sa_chgstate(sav, SADB_SASTATE_DYING);
4128 * XXX If we keep to send expire
4129 * message in the status of
4130 * DYING. Do remove below code.
4136 /* check DYING entry to change status to DEAD. */
4137 for (sav = LIST_FIRST(&sah->savtree[SADB_SASTATE_DYING]);
4141 nextsav = LIST_NEXT(sav, chain);
4143 /* we don't need to check. */
4144 if (sav->lft_h == NULL)
4148 if (sav->lft_c == NULL) {
4149 ipseclog((LOG_DEBUG, "key_timehandler: "
4150 "There is no CURRENT time, why?\n"));
4154 if (sav->lft_h->sadb_lifetime_addtime != 0
4155 && now - sav->created > sav->lft_h->sadb_lifetime_addtime) {
4156 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4159 #if 0 /* XXX Should we keep to send expire message until HARD lifetime ? */
4160 else if (sav->lft_s != NULL
4161 && sav->lft_s->sadb_lifetime_addtime != 0
4162 && now - sav->created > sav->lft_s->sadb_lifetime_addtime) {
4164 * XXX: should be checked to be
4165 * installed the valid SA.
4169 * If there is no SA then sending
4175 /* check HARD lifetime by bytes */
4176 else if (sav->lft_h->sadb_lifetime_bytes != 0
4177 && sav->lft_h->sadb_lifetime_bytes < sav->lft_c->sadb_lifetime_bytes) {
4178 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4183 /* delete entry in DEAD */
4184 for (sav = LIST_FIRST(&sah->savtree[SADB_SASTATE_DEAD]);
4188 nextsav = LIST_NEXT(sav, chain);
4191 if (sav->state != SADB_SASTATE_DEAD) {
4192 ipseclog((LOG_DEBUG, "key_timehandler: "
4193 "invalid sav->state "
4194 "(queue: %d SA: %d): "
4196 SADB_SASTATE_DEAD, sav->state));
4200 * do not call key_freesav() here.
4201 * sav should already be freed, and sav->refcnt
4202 * shows other references to sav
4203 * (such as from SPD).
4209 #ifndef IPSEC_NONBLOCK_ACQUIRE
4212 struct secacq *acq, *nextacq;
4214 for (acq = LIST_FIRST(&acqtree);
4218 nextacq = LIST_NEXT(acq, chain);
4220 if (now - acq->created > key_blockacq_lifetime
4221 && __LIST_CHAINED(acq)) {
4222 LIST_REMOVE(acq, chain);
4231 struct secspacq *acq, *nextacq;
4233 for (acq = LIST_FIRST(&spacqtree);
4237 nextacq = LIST_NEXT(acq, chain);
4239 if (now - acq->created > key_blockacq_lifetime
4240 && __LIST_CHAINED(acq)) {
4241 LIST_REMOVE(acq, chain);
4247 /* initialize random seed */
4248 if (key_tick_init_random++ > key_int_random) {
4249 key_tick_init_random = 0;
4253 #ifndef IPSEC_DEBUG2
4254 /* do exchange to tick time !! */
4255 (void)timeout((void *)key_timehandler, (void *)0, hz);
4256 #endif /* IPSEC_DEBUG2 */
4263 * to initialize a seed for random()
4268 srandom(time_second);
4276 key_randomfill(&value, sizeof(value));
4281 key_randomfill(p, l)
4287 static int warn = 1;
4290 n = (size_t)read_random(p, (u_int)l);
4294 bcopy(&v, (u_int8_t *)p + n,
4295 l - n < sizeof(v) ? l - n : sizeof(v));
4299 printf("WARNING: pseudo-random number generator "
4300 "used for IPsec processing\n");
4307 * map SADB_SATYPE_* to IPPROTO_*.
4308 * if satype == SADB_SATYPE then satype is mapped to ~0.
4310 * 0: invalid satype.
4313 key_satype2proto(satype)
4317 case SADB_SATYPE_UNSPEC:
4318 return IPSEC_PROTO_ANY;
4319 case SADB_SATYPE_AH:
4321 case SADB_SATYPE_ESP:
4323 case SADB_X_SATYPE_IPCOMP:
4324 return IPPROTO_IPCOMP;
4332 * map IPPROTO_* to SADB_SATYPE_*
4334 * 0: invalid protocol type.
4337 key_proto2satype(proto)
4342 return SADB_SATYPE_AH;
4344 return SADB_SATYPE_ESP;
4345 case IPPROTO_IPCOMP:
4346 return SADB_X_SATYPE_IPCOMP;
4355 * SADB_GETSPI processing is to receive
4356 * <base, (SA2), src address, dst address, (SPI range)>
4357 * from the IKMPd, to assign a unique spi value, to hang on the INBOUND
4358 * tree with the status of LARVAL, and send
4359 * <base, SA(*), address(SD)>
4362 * IN: mhp: pointer to the pointer to each header.
4363 * OUT: NULL if fail.
4364 * other if success, return pointer to the message to send.
4367 key_getspi(so, m, mhp)
4370 const struct sadb_msghdr *mhp;
4372 struct sadb_address *src0, *dst0;
4373 struct secasindex saidx;
4374 struct secashead *newsah;
4375 struct secasvar *newsav;
4383 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
4384 panic("key_getspi: NULL pointer is passed.\n");
4386 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
4387 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
4388 ipseclog((LOG_DEBUG, "key_getspi: invalid message is passed.\n"));
4389 return key_senderror(so, m, EINVAL);
4391 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
4392 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
4393 ipseclog((LOG_DEBUG, "key_getspi: invalid message is passed.\n"));
4394 return key_senderror(so, m, EINVAL);
4396 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
4397 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
4398 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
4400 mode = IPSEC_MODE_ANY;
4404 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
4405 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
4407 /* map satype to proto */
4408 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
4409 ipseclog((LOG_DEBUG, "key_getspi: invalid satype is passed.\n"));
4410 return key_senderror(so, m, EINVAL);
4413 /* make sure if port number is zero. */
4414 switch (((struct sockaddr *)(src0 + 1))->sa_family) {
4416 if (((struct sockaddr *)(src0 + 1))->sa_len !=
4417 sizeof(struct sockaddr_in))
4418 return key_senderror(so, m, EINVAL);
4419 ((struct sockaddr_in *)(src0 + 1))->sin_port = 0;
4422 if (((struct sockaddr *)(src0 + 1))->sa_len !=
4423 sizeof(struct sockaddr_in6))
4424 return key_senderror(so, m, EINVAL);
4425 ((struct sockaddr_in6 *)(src0 + 1))->sin6_port = 0;
4430 switch (((struct sockaddr *)(dst0 + 1))->sa_family) {
4432 if (((struct sockaddr *)(dst0 + 1))->sa_len !=
4433 sizeof(struct sockaddr_in))
4434 return key_senderror(so, m, EINVAL);
4435 ((struct sockaddr_in *)(dst0 + 1))->sin_port = 0;
4438 if (((struct sockaddr *)(dst0 + 1))->sa_len !=
4439 sizeof(struct sockaddr_in6))
4440 return key_senderror(so, m, EINVAL);
4441 ((struct sockaddr_in6 *)(dst0 + 1))->sin6_port = 0;
4447 /* XXX boundary check against sa_len */
4448 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
4450 /* SPI allocation */
4451 spi = key_do_getnewspi((struct sadb_spirange *)mhp->ext[SADB_EXT_SPIRANGE],
4454 return key_senderror(so, m, EINVAL);
4456 /* get a SA index */
4457 if ((newsah = key_getsah(&saidx)) == NULL) {
4458 /* create a new SA index */
4459 if ((newsah = key_newsah(&saidx)) == NULL) {
4460 ipseclog((LOG_DEBUG, "key_getspi: No more memory.\n"));
4461 return key_senderror(so, m, ENOBUFS);
4467 newsav = KEY_NEWSAV(m, mhp, newsah, &error);
4468 if (newsav == NULL) {
4469 /* XXX don't free new SA index allocated in above. */
4470 return key_senderror(so, m, error);
4474 newsav->spi = htonl(spi);
4476 #ifndef IPSEC_NONBLOCK_ACQUIRE
4477 /* delete the entry in acqtree */
4478 if (mhp->msg->sadb_msg_seq != 0) {
4480 if ((acq = key_getacqbyseq(mhp->msg->sadb_msg_seq)) != NULL) {
4481 /* reset counter in order to deletion by timehandler. */
4482 acq->created = time_second;
4489 struct mbuf *n, *nn;
4490 struct sadb_sa *m_sa;
4491 struct sadb_msg *newmsg;
4494 /* create new sadb_msg to reply. */
4495 len = PFKEY_ALIGN8(sizeof(struct sadb_msg)) +
4496 PFKEY_ALIGN8(sizeof(struct sadb_sa));
4498 return key_senderror(so, m, ENOBUFS);
4500 MGETHDR(n, M_DONTWAIT, MT_DATA);
4502 MCLGET(n, M_DONTWAIT);
4503 if ((n->m_flags & M_EXT) == 0) {
4509 return key_senderror(so, m, ENOBUFS);
4515 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
4516 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
4518 m_sa = (struct sadb_sa *)(mtod(n, caddr_t) + off);
4519 m_sa->sadb_sa_len = PFKEY_UNIT64(sizeof(struct sadb_sa));
4520 m_sa->sadb_sa_exttype = SADB_EXT_SA;
4521 m_sa->sadb_sa_spi = htonl(spi);
4522 off += PFKEY_ALIGN8(sizeof(struct sadb_sa));
4526 panic("length inconsistency in key_getspi");
4529 n->m_next = key_gather_mbuf(m, mhp, 0, 2, SADB_EXT_ADDRESS_SRC,
4530 SADB_EXT_ADDRESS_DST);
4533 return key_senderror(so, m, ENOBUFS);
4536 if (n->m_len < sizeof(struct sadb_msg)) {
4537 n = m_pullup(n, sizeof(struct sadb_msg));
4539 return key_sendup_mbuf(so, m, KEY_SENDUP_ONE);
4542 n->m_pkthdr.len = 0;
4543 for (nn = n; nn; nn = nn->m_next)
4544 n->m_pkthdr.len += nn->m_len;
4546 newmsg = mtod(n, struct sadb_msg *);
4547 newmsg->sadb_msg_seq = newsav->seq;
4548 newmsg->sadb_msg_errno = 0;
4549 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
4552 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
4557 * allocating new SPI
4558 * called by key_getspi().
4564 key_do_getnewspi(spirange, saidx)
4565 struct sadb_spirange *spirange;
4566 struct secasindex *saidx;
4570 int count = key_spi_trycnt;
4572 /* set spi range to allocate */
4573 if (spirange != NULL) {
4574 min = spirange->sadb_spirange_min;
4575 max = spirange->sadb_spirange_max;
4577 min = key_spi_minval;
4578 max = key_spi_maxval;
4580 /* IPCOMP needs 2-byte SPI */
4581 if (saidx->proto == IPPROTO_IPCOMP) {
4588 t = min; min = max; max = t;
4593 if (key_checkspidup(saidx, min) != NULL) {
4594 ipseclog((LOG_DEBUG, "key_do_getnewspi: SPI %u exists already.\n", min));
4598 count--; /* taking one cost. */
4606 /* when requesting to allocate spi ranged */
4608 /* generate pseudo-random SPI value ranged. */
4609 newspi = min + (key_random() % (max - min + 1));
4611 if (key_checkspidup(saidx, newspi) == NULL)
4615 if (count == 0 || newspi == 0) {
4616 ipseclog((LOG_DEBUG, "key_do_getnewspi: to allocate spi is failed.\n"));
4622 keystat.getspi_count =
4623 (keystat.getspi_count + key_spi_trycnt - count) / 2;
4629 * SADB_UPDATE processing
4631 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4632 * key(AE), (identity(SD),) (sensitivity)>
4633 * from the ikmpd, and update a secasvar entry whose status is SADB_SASTATE_LARVAL.
4635 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4636 * (identity(SD),) (sensitivity)>
4639 * m will always be freed.
4642 key_update(so, m, mhp)
4645 const struct sadb_msghdr *mhp;
4647 struct sadb_sa *sa0;
4648 struct sadb_address *src0, *dst0;
4649 struct secasindex saidx;
4650 struct secashead *sah;
4651 struct secasvar *sav;
4658 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
4659 panic("key_update: NULL pointer is passed.\n");
4661 /* map satype to proto */
4662 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
4663 ipseclog((LOG_DEBUG, "key_update: invalid satype is passed.\n"));
4664 return key_senderror(so, m, EINVAL);
4667 if (mhp->ext[SADB_EXT_SA] == NULL ||
4668 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
4669 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
4670 (mhp->msg->sadb_msg_satype == SADB_SATYPE_ESP &&
4671 mhp->ext[SADB_EXT_KEY_ENCRYPT] == NULL) ||
4672 (mhp->msg->sadb_msg_satype == SADB_SATYPE_AH &&
4673 mhp->ext[SADB_EXT_KEY_AUTH] == NULL) ||
4674 (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL &&
4675 mhp->ext[SADB_EXT_LIFETIME_SOFT] == NULL) ||
4676 (mhp->ext[SADB_EXT_LIFETIME_HARD] == NULL &&
4677 mhp->ext[SADB_EXT_LIFETIME_SOFT] != NULL)) {
4678 ipseclog((LOG_DEBUG, "key_update: invalid message is passed.\n"));
4679 return key_senderror(so, m, EINVAL);
4681 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
4682 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
4683 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
4684 ipseclog((LOG_DEBUG, "key_update: invalid message is passed.\n"));
4685 return key_senderror(so, m, EINVAL);
4687 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
4688 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
4689 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
4691 mode = IPSEC_MODE_ANY;
4694 /* XXX boundary checking for other extensions */
4696 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
4697 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
4698 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
4700 /* XXX boundary check against sa_len */
4701 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
4703 /* get a SA header */
4704 if ((sah = key_getsah(&saidx)) == NULL) {
4705 ipseclog((LOG_DEBUG, "key_update: no SA index found.\n"));
4706 return key_senderror(so, m, ENOENT);
4709 /* set spidx if there */
4711 error = key_setident(sah, m, mhp);
4713 return key_senderror(so, m, error);
4715 /* find a SA with sequence number. */
4716 #ifdef IPSEC_DOSEQCHECK
4717 if (mhp->msg->sadb_msg_seq != 0
4718 && (sav = key_getsavbyseq(sah, mhp->msg->sadb_msg_seq)) == NULL) {
4719 ipseclog((LOG_DEBUG,
4720 "key_update: no larval SA with sequence %u exists.\n",
4721 mhp->msg->sadb_msg_seq));
4722 return key_senderror(so, m, ENOENT);
4725 if ((sav = key_getsavbyspi(sah, sa0->sadb_sa_spi)) == NULL) {
4726 ipseclog((LOG_DEBUG,
4727 "key_update: no such a SA found (spi:%u)\n",
4728 (u_int32_t)ntohl(sa0->sadb_sa_spi)));
4729 return key_senderror(so, m, EINVAL);
4733 /* validity check */
4734 if (sav->sah->saidx.proto != proto) {
4735 ipseclog((LOG_DEBUG,
4736 "key_update: protocol mismatched (DB=%u param=%u)\n",
4737 sav->sah->saidx.proto, proto));
4738 return key_senderror(so, m, EINVAL);
4740 #ifdef IPSEC_DOSEQCHECK
4741 if (sav->spi != sa0->sadb_sa_spi) {
4742 ipseclog((LOG_DEBUG,
4743 "key_update: SPI mismatched (DB:%u param:%u)\n",
4744 (u_int32_t)ntohl(sav->spi),
4745 (u_int32_t)ntohl(sa0->sadb_sa_spi)));
4746 return key_senderror(so, m, EINVAL);
4749 if (sav->pid != mhp->msg->sadb_msg_pid) {
4750 ipseclog((LOG_DEBUG,
4751 "key_update: pid mismatched (DB:%u param:%u)\n",
4752 sav->pid, mhp->msg->sadb_msg_pid));
4753 return key_senderror(so, m, EINVAL);
4756 /* copy sav values */
4757 error = key_setsaval(sav, m, mhp);
4760 return key_senderror(so, m, error);
4763 /* check SA values to be mature. */
4764 if ((mhp->msg->sadb_msg_errno = key_mature(sav)) != 0) {
4766 return key_senderror(so, m, 0);
4772 /* set msg buf from mhp */
4773 n = key_getmsgbuf_x1(m, mhp);
4775 ipseclog((LOG_DEBUG, "key_update: No more memory.\n"));
4776 return key_senderror(so, m, ENOBUFS);
4780 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
4785 * search SAD with sequence for a SA which state is SADB_SASTATE_LARVAL.
4786 * only called by key_update().
4789 * others : found, pointer to a SA.
4791 #ifdef IPSEC_DOSEQCHECK
4792 static struct secasvar *
4793 key_getsavbyseq(sah, seq)
4794 struct secashead *sah;
4797 struct secasvar *sav;
4800 state = SADB_SASTATE_LARVAL;
4802 /* search SAD with sequence number ? */
4803 LIST_FOREACH(sav, &sah->savtree[state], chain) {
4805 KEY_CHKSASTATE(state, sav->state, "key_getsabyseq");
4807 if (sav->seq == seq) {
4809 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
4810 printf("DP key_getsavbyseq cause "
4811 "refcnt++:%d SA:%p\n",
4822 * SADB_ADD processing
4823 * add an entry to SA database, when received
4824 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4825 * key(AE), (identity(SD),) (sensitivity)>
4828 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4829 * (identity(SD),) (sensitivity)>
4832 * IGNORE identity and sensitivity messages.
4834 * m will always be freed.
4840 const struct sadb_msghdr *mhp;
4842 struct sadb_sa *sa0;
4843 struct sadb_address *src0, *dst0;
4844 struct secasindex saidx;
4845 struct secashead *newsah;
4846 struct secasvar *newsav;
4853 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
4854 panic("key_add: NULL pointer is passed.\n");
4856 /* map satype to proto */
4857 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
4858 ipseclog((LOG_DEBUG, "key_add: invalid satype is passed.\n"));
4859 return key_senderror(so, m, EINVAL);
4862 if (mhp->ext[SADB_EXT_SA] == NULL ||
4863 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
4864 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
4865 (mhp->msg->sadb_msg_satype == SADB_SATYPE_ESP &&
4866 mhp->ext[SADB_EXT_KEY_ENCRYPT] == NULL) ||
4867 (mhp->msg->sadb_msg_satype == SADB_SATYPE_AH &&
4868 mhp->ext[SADB_EXT_KEY_AUTH] == NULL) ||
4869 (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL &&
4870 mhp->ext[SADB_EXT_LIFETIME_SOFT] == NULL) ||
4871 (mhp->ext[SADB_EXT_LIFETIME_HARD] == NULL &&
4872 mhp->ext[SADB_EXT_LIFETIME_SOFT] != NULL)) {
4873 ipseclog((LOG_DEBUG, "key_add: invalid message is passed.\n"));
4874 return key_senderror(so, m, EINVAL);
4876 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
4877 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
4878 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
4880 ipseclog((LOG_DEBUG, "key_add: invalid message is passed.\n"));
4881 return key_senderror(so, m, EINVAL);
4883 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
4884 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
4885 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
4887 mode = IPSEC_MODE_ANY;
4891 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
4892 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
4893 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
4895 /* XXX boundary check against sa_len */
4896 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
4898 /* get a SA header */
4899 if ((newsah = key_getsah(&saidx)) == NULL) {
4900 /* create a new SA header */
4901 if ((newsah = key_newsah(&saidx)) == NULL) {
4902 ipseclog((LOG_DEBUG, "key_add: No more memory.\n"));
4903 return key_senderror(so, m, ENOBUFS);
4907 /* set spidx if there */
4909 error = key_setident(newsah, m, mhp);
4911 return key_senderror(so, m, error);
4914 /* create new SA entry. */
4915 /* We can create new SA only if SPI is differenct. */
4916 if (key_getsavbyspi(newsah, sa0->sadb_sa_spi)) {
4917 ipseclog((LOG_DEBUG, "key_add: SA already exists.\n"));
4918 return key_senderror(so, m, EEXIST);
4920 newsav = KEY_NEWSAV(m, mhp, newsah, &error);
4921 if (newsav == NULL) {
4922 return key_senderror(so, m, error);
4925 /* check SA values to be mature. */
4926 if ((error = key_mature(newsav)) != 0) {
4927 KEY_FREESAV(&newsav);
4928 return key_senderror(so, m, error);
4932 * don't call key_freesav() here, as we would like to keep the SA
4933 * in the database on success.
4939 /* set msg buf from mhp */
4940 n = key_getmsgbuf_x1(m, mhp);
4942 ipseclog((LOG_DEBUG, "key_update: No more memory.\n"));
4943 return key_senderror(so, m, ENOBUFS);
4947 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
4953 key_setident(sah, m, mhp)
4954 struct secashead *sah;
4956 const struct sadb_msghdr *mhp;
4958 const struct sadb_ident *idsrc, *iddst;
4959 int idsrclen, iddstlen;
4962 if (sah == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
4963 panic("key_setident: NULL pointer is passed.\n");
4965 /* don't make buffer if not there */
4966 if (mhp->ext[SADB_EXT_IDENTITY_SRC] == NULL &&
4967 mhp->ext[SADB_EXT_IDENTITY_DST] == NULL) {
4973 if (mhp->ext[SADB_EXT_IDENTITY_SRC] == NULL ||
4974 mhp->ext[SADB_EXT_IDENTITY_DST] == NULL) {
4975 ipseclog((LOG_DEBUG, "key_setident: invalid identity.\n"));
4979 idsrc = (const struct sadb_ident *)mhp->ext[SADB_EXT_IDENTITY_SRC];
4980 iddst = (const struct sadb_ident *)mhp->ext[SADB_EXT_IDENTITY_DST];
4981 idsrclen = mhp->extlen[SADB_EXT_IDENTITY_SRC];
4982 iddstlen = mhp->extlen[SADB_EXT_IDENTITY_DST];
4984 /* validity check */
4985 if (idsrc->sadb_ident_type != iddst->sadb_ident_type) {
4986 ipseclog((LOG_DEBUG, "key_setident: ident type mismatch.\n"));
4990 switch (idsrc->sadb_ident_type) {
4991 case SADB_IDENTTYPE_PREFIX:
4992 case SADB_IDENTTYPE_FQDN:
4993 case SADB_IDENTTYPE_USERFQDN:
4995 /* XXX do nothing */
5001 /* make structure */
5002 KMALLOC(sah->idents, struct sadb_ident *, idsrclen);
5003 if (sah->idents == NULL) {
5004 ipseclog((LOG_DEBUG, "key_setident: No more memory.\n"));
5007 KMALLOC(sah->identd, struct sadb_ident *, iddstlen);
5008 if (sah->identd == NULL) {
5011 ipseclog((LOG_DEBUG, "key_setident: No more memory.\n"));
5014 bcopy(idsrc, sah->idents, idsrclen);
5015 bcopy(iddst, sah->identd, iddstlen);
5021 * m will not be freed on return.
5022 * it is caller's responsibility to free the result.
5024 static struct mbuf *
5025 key_getmsgbuf_x1(m, mhp)
5027 const struct sadb_msghdr *mhp;
5032 if (m == NULL || mhp == NULL || mhp->msg == NULL)
5033 panic("key_getmsgbuf_x1: NULL pointer is passed.\n");
5035 /* create new sadb_msg to reply. */
5036 n = key_gather_mbuf(m, mhp, 1, 9, SADB_EXT_RESERVED,
5037 SADB_EXT_SA, SADB_X_EXT_SA2,
5038 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST,
5039 SADB_EXT_LIFETIME_HARD, SADB_EXT_LIFETIME_SOFT,
5040 SADB_EXT_IDENTITY_SRC, SADB_EXT_IDENTITY_DST);
5044 if (n->m_len < sizeof(struct sadb_msg)) {
5045 n = m_pullup(n, sizeof(struct sadb_msg));
5049 mtod(n, struct sadb_msg *)->sadb_msg_errno = 0;
5050 mtod(n, struct sadb_msg *)->sadb_msg_len =
5051 PFKEY_UNIT64(n->m_pkthdr.len);
5056 static int key_delete_all __P((struct socket *, struct mbuf *,
5057 const struct sadb_msghdr *, u_int16_t));
5060 * SADB_DELETE processing
5062 * <base, SA(*), address(SD)>
5063 * from the ikmpd, and set SADB_SASTATE_DEAD,
5065 * <base, SA(*), address(SD)>
5068 * m will always be freed.
5071 key_delete(so, m, mhp)
5074 const struct sadb_msghdr *mhp;
5076 struct sadb_sa *sa0;
5077 struct sadb_address *src0, *dst0;
5078 struct secasindex saidx;
5079 struct secashead *sah;
5080 struct secasvar *sav = NULL;
5084 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
5085 panic("key_delete: NULL pointer is passed.\n");
5087 /* map satype to proto */
5088 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5089 ipseclog((LOG_DEBUG, "key_delete: invalid satype is passed.\n"));
5090 return key_senderror(so, m, EINVAL);
5093 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5094 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
5095 ipseclog((LOG_DEBUG, "key_delete: invalid message is passed.\n"));
5096 return key_senderror(so, m, EINVAL);
5099 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5100 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5101 ipseclog((LOG_DEBUG, "key_delete: invalid message is passed.\n"));
5102 return key_senderror(so, m, EINVAL);
5105 if (mhp->ext[SADB_EXT_SA] == NULL) {
5107 * Caller wants us to delete all non-LARVAL SAs
5108 * that match the src/dst. This is used during
5109 * IKE INITIAL-CONTACT.
5111 ipseclog((LOG_DEBUG, "key_delete: doing delete all.\n"));
5112 return key_delete_all(so, m, mhp, proto);
5113 } else if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa)) {
5114 ipseclog((LOG_DEBUG, "key_delete: invalid message is passed.\n"));
5115 return key_senderror(so, m, EINVAL);
5118 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5119 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
5120 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
5122 /* XXX boundary check against sa_len */
5123 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5125 /* get a SA header */
5126 LIST_FOREACH(sah, &sahtree, chain) {
5127 if (sah->state == SADB_SASTATE_DEAD)
5129 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5132 /* get a SA with SPI. */
5133 sav = key_getsavbyspi(sah, sa0->sadb_sa_spi);
5138 ipseclog((LOG_DEBUG, "key_delete: no SA found.\n"));
5139 return key_senderror(so, m, ENOENT);
5142 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
5147 struct sadb_msg *newmsg;
5149 /* create new sadb_msg to reply. */
5150 n = key_gather_mbuf(m, mhp, 1, 4, SADB_EXT_RESERVED,
5151 SADB_EXT_SA, SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
5153 return key_senderror(so, m, ENOBUFS);
5155 if (n->m_len < sizeof(struct sadb_msg)) {
5156 n = m_pullup(n, sizeof(struct sadb_msg));
5158 return key_senderror(so, m, ENOBUFS);
5160 newmsg = mtod(n, struct sadb_msg *);
5161 newmsg->sadb_msg_errno = 0;
5162 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
5165 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5170 * delete all SAs for src/dst. Called from key_delete().
5173 key_delete_all(so, m, mhp, proto)
5176 const struct sadb_msghdr *mhp;
5179 struct sadb_address *src0, *dst0;
5180 struct secasindex saidx;
5181 struct secashead *sah;
5182 struct secasvar *sav, *nextsav;
5183 u_int stateidx, state;
5185 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
5186 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
5188 /* XXX boundary check against sa_len */
5189 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5191 LIST_FOREACH(sah, &sahtree, chain) {
5192 if (sah->state == SADB_SASTATE_DEAD)
5194 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5197 /* Delete all non-LARVAL SAs. */
5199 stateidx < _ARRAYLEN(saorder_state_alive);
5201 state = saorder_state_alive[stateidx];
5202 if (state == SADB_SASTATE_LARVAL)
5204 for (sav = LIST_FIRST(&sah->savtree[state]);
5205 sav != NULL; sav = nextsav) {
5206 nextsav = LIST_NEXT(sav, chain);
5208 if (sav->state != state) {
5209 ipseclog((LOG_DEBUG, "key_delete_all: "
5210 "invalid sav->state "
5211 "(queue: %d SA: %d)\n",
5212 state, sav->state));
5216 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
5223 struct sadb_msg *newmsg;
5225 /* create new sadb_msg to reply. */
5226 n = key_gather_mbuf(m, mhp, 1, 3, SADB_EXT_RESERVED,
5227 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
5229 return key_senderror(so, m, ENOBUFS);
5231 if (n->m_len < sizeof(struct sadb_msg)) {
5232 n = m_pullup(n, sizeof(struct sadb_msg));
5234 return key_senderror(so, m, ENOBUFS);
5236 newmsg = mtod(n, struct sadb_msg *);
5237 newmsg->sadb_msg_errno = 0;
5238 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
5241 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5246 * SADB_GET processing
5248 * <base, SA(*), address(SD)>
5249 * from the ikmpd, and get a SP and a SA to respond,
5251 * <base, SA, (lifetime(HSC),) address(SD), (address(P),) key(AE),
5252 * (identity(SD),) (sensitivity)>
5255 * m will always be freed.
5261 const struct sadb_msghdr *mhp;
5263 struct sadb_sa *sa0;
5264 struct sadb_address *src0, *dst0;
5265 struct secasindex saidx;
5266 struct secashead *sah;
5267 struct secasvar *sav = NULL;
5271 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
5272 panic("key_get: NULL pointer is passed.\n");
5274 /* map satype to proto */
5275 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5276 ipseclog((LOG_DEBUG, "key_get: invalid satype is passed.\n"));
5277 return key_senderror(so, m, EINVAL);
5280 if (mhp->ext[SADB_EXT_SA] == NULL ||
5281 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5282 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
5283 ipseclog((LOG_DEBUG, "key_get: invalid message is passed.\n"));
5284 return key_senderror(so, m, EINVAL);
5286 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
5287 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5288 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5289 ipseclog((LOG_DEBUG, "key_get: invalid message is passed.\n"));
5290 return key_senderror(so, m, EINVAL);
5293 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5294 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
5295 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
5297 /* XXX boundary check against sa_len */
5298 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5300 /* get a SA header */
5301 LIST_FOREACH(sah, &sahtree, chain) {
5302 if (sah->state == SADB_SASTATE_DEAD)
5304 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5307 /* get a SA with SPI. */
5308 sav = key_getsavbyspi(sah, sa0->sadb_sa_spi);
5313 ipseclog((LOG_DEBUG, "key_get: no SA found.\n"));
5314 return key_senderror(so, m, ENOENT);
5321 /* map proto to satype */
5322 if ((satype = key_proto2satype(sah->saidx.proto)) == 0) {
5323 ipseclog((LOG_DEBUG, "key_get: there was invalid proto in SAD.\n"));
5324 return key_senderror(so, m, EINVAL);
5327 /* create new sadb_msg to reply. */
5328 n = key_setdumpsa(sav, SADB_GET, satype, mhp->msg->sadb_msg_seq,
5329 mhp->msg->sadb_msg_pid);
5331 return key_senderror(so, m, ENOBUFS);
5334 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
5338 /* XXX make it sysctl-configurable? */
5340 key_getcomb_setlifetime(comb)
5341 struct sadb_comb *comb;
5344 comb->sadb_comb_soft_allocations = 1;
5345 comb->sadb_comb_hard_allocations = 1;
5346 comb->sadb_comb_soft_bytes = 0;
5347 comb->sadb_comb_hard_bytes = 0;
5348 comb->sadb_comb_hard_addtime = 86400; /* 1 day */
5349 comb->sadb_comb_soft_addtime = comb->sadb_comb_soft_addtime * 80 / 100;
5350 comb->sadb_comb_soft_usetime = 28800; /* 8 hours */
5351 comb->sadb_comb_hard_usetime = comb->sadb_comb_hard_usetime * 80 / 100;
5355 * XXX reorder combinations by preference
5356 * XXX no idea if the user wants ESP authentication or not
5358 static struct mbuf *
5361 struct sadb_comb *comb;
5362 struct enc_xform *algo;
5363 struct mbuf *result = NULL, *m, *n;
5367 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
5370 for (i = 1; i <= SADB_EALG_MAX; i++) {
5371 algo = esp_algorithm_lookup(i);
5375 /* discard algorithms with key size smaller than system min */
5376 if (_BITS(algo->maxkey) < ipsec_esp_keymin)
5378 if (_BITS(algo->minkey) < ipsec_esp_keymin)
5379 encmin = ipsec_esp_keymin;
5381 encmin = _BITS(algo->minkey);
5384 m = key_getcomb_ah();
5387 ("key_getcomb_esp: l=%u > MLEN=%lu",
5389 MGET(m, M_DONTWAIT, MT_DATA);
5394 bzero(mtod(m, caddr_t), m->m_len);
5401 for (n = m; n; n = n->m_next)
5403 KASSERT((totlen % l) == 0,
5404 ("key_getcomb_esp: totlen=%u, l=%u", totlen, l));
5406 for (off = 0; off < totlen; off += l) {
5407 n = m_pulldown(m, off, l, &o);
5409 /* m is already freed */
5412 comb = (struct sadb_comb *)(mtod(n, caddr_t) + o);
5413 bzero(comb, sizeof(*comb));
5414 key_getcomb_setlifetime(comb);
5415 comb->sadb_comb_encrypt = i;
5416 comb->sadb_comb_encrypt_minbits = encmin;
5417 comb->sadb_comb_encrypt_maxbits = _BITS(algo->maxkey);
5436 const struct auth_hash *ah,
5441 *min = *max = ah->keysize;
5442 if (ah->keysize == 0) {
5444 * Transform takes arbitrary key size but algorithm
5445 * key size is restricted. Enforce this here.
5448 case SADB_X_AALG_MD5: *min = *max = 16; break;
5449 case SADB_X_AALG_SHA: *min = *max = 20; break;
5450 case SADB_X_AALG_NULL: *min = 1; *max = 256; break;
5452 DPRINTF(("key_getsizes_ah: unknown AH algorithm %u\n",
5460 * XXX reorder combinations by preference
5462 static struct mbuf *
5465 struct sadb_comb *comb;
5466 struct auth_hash *algo;
5468 u_int16_t minkeysize, maxkeysize;
5470 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
5473 for (i = 1; i <= SADB_AALG_MAX; i++) {
5475 /* we prefer HMAC algorithms, not old algorithms */
5476 if (i != SADB_AALG_SHA1HMAC && i != SADB_AALG_MD5HMAC)
5479 algo = ah_algorithm_lookup(i);
5482 key_getsizes_ah(algo, i, &minkeysize, &maxkeysize);
5483 /* discard algorithms with key size smaller than system min */
5484 if (_BITS(minkeysize) < ipsec_ah_keymin)
5489 ("key_getcomb_ah: l=%u > MLEN=%lu",
5491 MGET(m, M_DONTWAIT, MT_DATA);
5498 M_PREPEND(m, l, M_DONTWAIT);
5502 comb = mtod(m, struct sadb_comb *);
5503 bzero(comb, sizeof(*comb));
5504 key_getcomb_setlifetime(comb);
5505 comb->sadb_comb_auth = i;
5506 comb->sadb_comb_auth_minbits = _BITS(minkeysize);
5507 comb->sadb_comb_auth_maxbits = _BITS(maxkeysize);
5514 * not really an official behavior. discussed in pf_key@inner.net in Sep2000.
5515 * XXX reorder combinations by preference
5517 static struct mbuf *
5518 key_getcomb_ipcomp()
5520 struct sadb_comb *comb;
5521 struct comp_algo *algo;
5524 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
5527 for (i = 1; i <= SADB_X_CALG_MAX; i++) {
5528 algo = ipcomp_algorithm_lookup(i);
5534 ("key_getcomb_ipcomp: l=%u > MLEN=%lu",
5536 MGET(m, M_DONTWAIT, MT_DATA);
5543 M_PREPEND(m, l, M_DONTWAIT);
5547 comb = mtod(m, struct sadb_comb *);
5548 bzero(comb, sizeof(*comb));
5549 key_getcomb_setlifetime(comb);
5550 comb->sadb_comb_encrypt = i;
5551 /* what should we set into sadb_comb_*_{min,max}bits? */
5558 * XXX no way to pass mode (transport/tunnel) to userland
5559 * XXX replay checking?
5560 * XXX sysctl interface to ipsec_{ah,esp}_keymin
5562 static struct mbuf *
5564 const struct secasindex *saidx;
5566 struct sadb_prop *prop;
5568 const int l = PFKEY_ALIGN8(sizeof(struct sadb_prop));
5571 switch (saidx->proto) {
5573 m = key_getcomb_esp();
5576 m = key_getcomb_ah();
5578 case IPPROTO_IPCOMP:
5579 m = key_getcomb_ipcomp();
5587 M_PREPEND(m, l, M_DONTWAIT);
5592 for (n = m; n; n = n->m_next)
5595 prop = mtod(m, struct sadb_prop *);
5596 bzero(prop, sizeof(*prop));
5597 prop->sadb_prop_len = PFKEY_UNIT64(totlen);
5598 prop->sadb_prop_exttype = SADB_EXT_PROPOSAL;
5599 prop->sadb_prop_replay = 32; /* XXX */
5605 * SADB_ACQUIRE processing called by key_checkrequest() and key_acquire2().
5607 * <base, SA, address(SD), (address(P)), x_policy,
5608 * (identity(SD),) (sensitivity,) proposal>
5609 * to KMD, and expect to receive
5610 * <base> with SADB_ACQUIRE if error occured,
5612 * <base, src address, dst address, (SPI range)> with SADB_GETSPI
5613 * from KMD by PF_KEY.
5615 * XXX x_policy is outside of RFC2367 (KAME extension).
5616 * XXX sensitivity is not supported.
5617 * XXX for ipcomp, RFC2367 does not define how to fill in proposal.
5618 * see comment for key_getcomb_ipcomp().
5622 * others: error number
5625 key_acquire(const struct secasindex *saidx, struct secpolicy *sp)
5627 struct mbuf *result = NULL, *m;
5628 #ifndef IPSEC_NONBLOCK_ACQUIRE
5629 struct secacq *newacq;
5636 KASSERT(saidx != NULL, ("key_acquire: null saidx"));
5637 satype = key_proto2satype(saidx->proto);
5638 KASSERT(satype != 0,
5639 ("key_acquire: null satype, protocol %u", saidx->proto));
5641 #ifndef IPSEC_NONBLOCK_ACQUIRE
5643 * We never do anything about acquirng SA. There is anather
5644 * solution that kernel blocks to send SADB_ACQUIRE message until
5645 * getting something message from IKEd. In later case, to be
5646 * managed with ACQUIRING list.
5648 /* Get an entry to check whether sending message or not. */
5649 if ((newacq = key_getacq(saidx)) != NULL) {
5650 if (key_blockacq_count < newacq->count) {
5651 /* reset counter and do send message. */
5654 /* increment counter and do nothing. */
5659 /* make new entry for blocking to send SADB_ACQUIRE. */
5660 if ((newacq = key_newacq(saidx)) == NULL)
5663 /* add to acqtree */
5664 LIST_INSERT_HEAD(&acqtree, newacq, chain);
5669 #ifndef IPSEC_NONBLOCK_ACQUIRE
5672 seq = (acq_seq = (acq_seq == ~0 ? 1 : ++acq_seq));
5674 m = key_setsadbmsg(SADB_ACQUIRE, 0, satype, seq, 0, 0);
5681 /* set sadb_address for saidx's. */
5682 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
5683 &saidx->src.sa, FULLMASK, IPSEC_ULPROTO_ANY);
5690 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
5691 &saidx->dst.sa, FULLMASK, IPSEC_ULPROTO_ANY);
5698 /* XXX proxy address (optional) */
5700 /* set sadb_x_policy */
5702 m = key_setsadbxpolicy(sp->policy, sp->spidx.dir, sp->id);
5710 /* XXX identity (optional) */
5712 if (idexttype && fqdn) {
5713 /* create identity extension (FQDN) */
5714 struct sadb_ident *id;
5717 fqdnlen = strlen(fqdn) + 1; /* +1 for terminating-NUL */
5718 id = (struct sadb_ident *)p;
5719 bzero(id, sizeof(*id) + PFKEY_ALIGN8(fqdnlen));
5720 id->sadb_ident_len = PFKEY_UNIT64(sizeof(*id) + PFKEY_ALIGN8(fqdnlen));
5721 id->sadb_ident_exttype = idexttype;
5722 id->sadb_ident_type = SADB_IDENTTYPE_FQDN;
5723 bcopy(fqdn, id + 1, fqdnlen);
5724 p += sizeof(struct sadb_ident) + PFKEY_ALIGN8(fqdnlen);
5728 /* create identity extension (USERFQDN) */
5729 struct sadb_ident *id;
5733 /* +1 for terminating-NUL */
5734 userfqdnlen = strlen(userfqdn) + 1;
5737 id = (struct sadb_ident *)p;
5738 bzero(id, sizeof(*id) + PFKEY_ALIGN8(userfqdnlen));
5739 id->sadb_ident_len = PFKEY_UNIT64(sizeof(*id) + PFKEY_ALIGN8(userfqdnlen));
5740 id->sadb_ident_exttype = idexttype;
5741 id->sadb_ident_type = SADB_IDENTTYPE_USERFQDN;
5742 /* XXX is it correct? */
5743 if (curproc && curproc->p_cred)
5744 id->sadb_ident_id = curproc->p_cred->p_ruid;
5745 if (userfqdn && userfqdnlen)
5746 bcopy(userfqdn, id + 1, userfqdnlen);
5747 p += sizeof(struct sadb_ident) + PFKEY_ALIGN8(userfqdnlen);
5751 /* XXX sensitivity (optional) */
5753 /* create proposal/combination extension */
5754 m = key_getprop(saidx);
5757 * spec conformant: always attach proposal/combination extension,
5758 * the problem is that we have no way to attach it for ipcomp,
5759 * due to the way sadb_comb is declared in RFC2367.
5768 * outside of spec; make proposal/combination extension optional.
5774 if ((result->m_flags & M_PKTHDR) == 0) {
5779 if (result->m_len < sizeof(struct sadb_msg)) {
5780 result = m_pullup(result, sizeof(struct sadb_msg));
5781 if (result == NULL) {
5787 result->m_pkthdr.len = 0;
5788 for (m = result; m; m = m->m_next)
5789 result->m_pkthdr.len += m->m_len;
5791 mtod(result, struct sadb_msg *)->sadb_msg_len =
5792 PFKEY_UNIT64(result->m_pkthdr.len);
5794 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
5802 #ifndef IPSEC_NONBLOCK_ACQUIRE
5803 static struct secacq *
5804 key_newacq(const struct secasindex *saidx)
5806 struct secacq *newacq;
5809 KMALLOC(newacq, struct secacq *, sizeof(struct secacq));
5810 if (newacq == NULL) {
5811 ipseclog((LOG_DEBUG, "key_newacq: No more memory.\n"));
5814 bzero(newacq, sizeof(*newacq));
5817 bcopy(saidx, &newacq->saidx, sizeof(newacq->saidx));
5818 newacq->seq = (acq_seq == ~0 ? 1 : ++acq_seq);
5819 newacq->created = time_second;
5825 static struct secacq *
5826 key_getacq(const struct secasindex *saidx)
5830 LIST_FOREACH(acq, &acqtree, chain) {
5831 if (key_cmpsaidx(saidx, &acq->saidx, CMP_EXACTLY))
5838 static struct secacq *
5839 key_getacqbyseq(seq)
5844 LIST_FOREACH(acq, &acqtree, chain) {
5845 if (acq->seq == seq)
5853 static struct secspacq *
5855 struct secpolicyindex *spidx;
5857 struct secspacq *acq;
5860 KMALLOC(acq, struct secspacq *, sizeof(struct secspacq));
5862 ipseclog((LOG_DEBUG, "key_newspacq: No more memory.\n"));
5865 bzero(acq, sizeof(*acq));
5868 bcopy(spidx, &acq->spidx, sizeof(acq->spidx));
5869 acq->created = time_second;
5875 static struct secspacq *
5877 struct secpolicyindex *spidx;
5879 struct secspacq *acq;
5881 LIST_FOREACH(acq, &spacqtree, chain) {
5882 if (key_cmpspidx_exactly(spidx, &acq->spidx))
5890 * SADB_ACQUIRE processing,
5891 * in first situation, is receiving
5893 * from the ikmpd, and clear sequence of its secasvar entry.
5895 * In second situation, is receiving
5896 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
5897 * from a user land process, and return
5898 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
5901 * m will always be freed.
5904 key_acquire2(so, m, mhp)
5907 const struct sadb_msghdr *mhp;
5909 const struct sadb_address *src0, *dst0;
5910 struct secasindex saidx;
5911 struct secashead *sah;
5916 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
5917 panic("key_acquire2: NULL pointer is passed.\n");
5920 * Error message from KMd.
5921 * We assume that if error was occured in IKEd, the length of PFKEY
5922 * message is equal to the size of sadb_msg structure.
5923 * We do not raise error even if error occured in this function.
5925 if (mhp->msg->sadb_msg_len == PFKEY_UNIT64(sizeof(struct sadb_msg))) {
5926 #ifndef IPSEC_NONBLOCK_ACQUIRE
5929 /* check sequence number */
5930 if (mhp->msg->sadb_msg_seq == 0) {
5931 ipseclog((LOG_DEBUG, "key_acquire2: must specify sequence number.\n"));
5936 if ((acq = key_getacqbyseq(mhp->msg->sadb_msg_seq)) == NULL) {
5938 * the specified larval SA is already gone, or we got
5939 * a bogus sequence number. we can silently ignore it.
5945 /* reset acq counter in order to deletion by timehander. */
5946 acq->created = time_second;
5954 * This message is from user land.
5957 /* map satype to proto */
5958 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5959 ipseclog((LOG_DEBUG, "key_acquire2: invalid satype is passed.\n"));
5960 return key_senderror(so, m, EINVAL);
5963 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5964 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
5965 mhp->ext[SADB_EXT_PROPOSAL] == NULL) {
5967 ipseclog((LOG_DEBUG, "key_acquire2: invalid message is passed.\n"));
5968 return key_senderror(so, m, EINVAL);
5970 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5971 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
5972 mhp->extlen[SADB_EXT_PROPOSAL] < sizeof(struct sadb_prop)) {
5974 ipseclog((LOG_DEBUG, "key_acquire2: invalid message is passed.\n"));
5975 return key_senderror(so, m, EINVAL);
5978 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
5979 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
5981 /* XXX boundary check against sa_len */
5982 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5984 /* get a SA index */
5985 LIST_FOREACH(sah, &sahtree, chain) {
5986 if (sah->state == SADB_SASTATE_DEAD)
5988 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_MODE_REQID))
5992 ipseclog((LOG_DEBUG, "key_acquire2: a SA exists already.\n"));
5993 return key_senderror(so, m, EEXIST);
5996 error = key_acquire(&saidx, NULL);
5998 ipseclog((LOG_DEBUG, "key_acquire2: error %d returned "
5999 "from key_acquire.\n", mhp->msg->sadb_msg_errno));
6000 return key_senderror(so, m, error);
6003 return key_sendup_mbuf(so, m, KEY_SENDUP_REGISTERED);
6007 * SADB_REGISTER processing.
6008 * If SATYPE_UNSPEC has been passed as satype, only return sabd_supported.
6011 * from the ikmpd, and register a socket to send PF_KEY messages,
6015 * If socket is detached, must free from regnode.
6017 * m will always be freed.
6020 key_register(so, m, mhp)
6023 const struct sadb_msghdr *mhp;
6025 struct secreg *reg, *newreg = 0;
6028 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
6029 panic("key_register: NULL pointer is passed.\n");
6031 /* check for invalid register message */
6032 if (mhp->msg->sadb_msg_satype >= sizeof(regtree)/sizeof(regtree[0]))
6033 return key_senderror(so, m, EINVAL);
6035 /* When SATYPE_UNSPEC is specified, only return sabd_supported. */
6036 if (mhp->msg->sadb_msg_satype == SADB_SATYPE_UNSPEC)
6039 /* check whether existing or not */
6040 LIST_FOREACH(reg, ®tree[mhp->msg->sadb_msg_satype], chain) {
6041 if (reg->so == so) {
6042 ipseclog((LOG_DEBUG, "key_register: socket exists already.\n"));
6043 return key_senderror(so, m, EEXIST);
6047 /* create regnode */
6048 KMALLOC(newreg, struct secreg *, sizeof(*newreg));
6049 if (newreg == NULL) {
6050 ipseclog((LOG_DEBUG, "key_register: No more memory.\n"));
6051 return key_senderror(so, m, ENOBUFS);
6053 bzero((caddr_t)newreg, sizeof(*newreg));
6056 ((struct keycb *)sotorawcb(so))->kp_registered++;
6058 /* add regnode to regtree. */
6059 LIST_INSERT_HEAD(®tree[mhp->msg->sadb_msg_satype], newreg, chain);
6064 struct sadb_msg *newmsg;
6065 struct sadb_supported *sup;
6066 u_int len, alen, elen;
6069 struct sadb_alg *alg;
6071 /* create new sadb_msg to reply. */
6073 for (i = 1; i <= SADB_AALG_MAX; i++) {
6074 if (ah_algorithm_lookup(i))
6075 alen += sizeof(struct sadb_alg);
6078 alen += sizeof(struct sadb_supported);
6080 for (i = 1; i <= SADB_EALG_MAX; i++) {
6081 if (esp_algorithm_lookup(i))
6082 elen += sizeof(struct sadb_alg);
6085 elen += sizeof(struct sadb_supported);
6087 len = sizeof(struct sadb_msg) + alen + elen;
6090 return key_senderror(so, m, ENOBUFS);
6092 MGETHDR(n, M_DONTWAIT, MT_DATA);
6094 MCLGET(n, M_DONTWAIT);
6095 if ((n->m_flags & M_EXT) == 0) {
6101 return key_senderror(so, m, ENOBUFS);
6103 n->m_pkthdr.len = n->m_len = len;
6107 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
6108 newmsg = mtod(n, struct sadb_msg *);
6109 newmsg->sadb_msg_errno = 0;
6110 newmsg->sadb_msg_len = PFKEY_UNIT64(len);
6111 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
6113 /* for authentication algorithm */
6115 sup = (struct sadb_supported *)(mtod(n, caddr_t) + off);
6116 sup->sadb_supported_len = PFKEY_UNIT64(alen);
6117 sup->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
6118 off += PFKEY_ALIGN8(sizeof(*sup));
6120 for (i = 1; i <= SADB_AALG_MAX; i++) {
6121 struct auth_hash *aalgo;
6122 u_int16_t minkeysize, maxkeysize;
6124 aalgo = ah_algorithm_lookup(i);
6127 alg = (struct sadb_alg *)(mtod(n, caddr_t) + off);
6128 alg->sadb_alg_id = i;
6129 alg->sadb_alg_ivlen = 0;
6130 key_getsizes_ah(aalgo, i, &minkeysize, &maxkeysize);
6131 alg->sadb_alg_minbits = _BITS(minkeysize);
6132 alg->sadb_alg_maxbits = _BITS(maxkeysize);
6133 off += PFKEY_ALIGN8(sizeof(*alg));
6137 /* for encryption algorithm */
6139 sup = (struct sadb_supported *)(mtod(n, caddr_t) + off);
6140 sup->sadb_supported_len = PFKEY_UNIT64(elen);
6141 sup->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT;
6142 off += PFKEY_ALIGN8(sizeof(*sup));
6144 for (i = 1; i <= SADB_EALG_MAX; i++) {
6145 struct enc_xform *ealgo;
6147 ealgo = esp_algorithm_lookup(i);
6150 alg = (struct sadb_alg *)(mtod(n, caddr_t) + off);
6151 alg->sadb_alg_id = i;
6152 alg->sadb_alg_ivlen = ealgo->blocksize;
6153 alg->sadb_alg_minbits = _BITS(ealgo->minkey);
6154 alg->sadb_alg_maxbits = _BITS(ealgo->maxkey);
6155 off += PFKEY_ALIGN8(sizeof(struct sadb_alg));
6161 panic("length assumption failed in key_register");
6165 return key_sendup_mbuf(so, n, KEY_SENDUP_REGISTERED);
6170 * free secreg entry registered.
6171 * XXX: I want to do free a socket marked done SADB_RESIGER to socket.
6182 panic("key_freereg: NULL pointer is passed.\n");
6185 * check whether existing or not.
6186 * check all type of SA, because there is a potential that
6187 * one socket is registered to multiple type of SA.
6189 for (i = 0; i <= SADB_SATYPE_MAX; i++) {
6190 LIST_FOREACH(reg, ®tree[i], chain) {
6192 && __LIST_CHAINED(reg)) {
6193 LIST_REMOVE(reg, chain);
6204 * SADB_EXPIRE processing
6206 * <base, SA, SA2, lifetime(C and one of HS), address(SD)>
6208 * NOTE: We send only soft lifetime extension.
6211 * others : error number
6215 struct secasvar *sav;
6219 struct mbuf *result = NULL, *m;
6222 struct sadb_lifetime *lt;
6224 /* XXX: Why do we lock ? */
6225 s = splnet(); /*called from softclock()*/
6229 panic("key_expire: NULL pointer is passed.\n");
6230 if (sav->sah == NULL)
6231 panic("key_expire: Why was SA index in SA NULL.\n");
6232 if ((satype = key_proto2satype(sav->sah->saidx.proto)) == 0)
6233 panic("key_expire: invalid proto is passed.\n");
6235 /* set msg header */
6236 m = key_setsadbmsg(SADB_EXPIRE, 0, satype, sav->seq, 0, sav->refcnt);
6243 /* create SA extension */
6244 m = key_setsadbsa(sav);
6251 /* create SA extension */
6252 m = key_setsadbxsa2(sav->sah->saidx.mode,
6253 sav->replay ? sav->replay->count : 0,
6254 sav->sah->saidx.reqid);
6261 /* create lifetime extension (current and soft) */
6262 len = PFKEY_ALIGN8(sizeof(*lt)) * 2;
6263 m = key_alloc_mbuf(len);
6264 if (!m || m->m_next) { /*XXX*/
6270 bzero(mtod(m, caddr_t), len);
6271 lt = mtod(m, struct sadb_lifetime *);
6272 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
6273 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
6274 lt->sadb_lifetime_allocations = sav->lft_c->sadb_lifetime_allocations;
6275 lt->sadb_lifetime_bytes = sav->lft_c->sadb_lifetime_bytes;
6276 lt->sadb_lifetime_addtime = sav->lft_c->sadb_lifetime_addtime;
6277 lt->sadb_lifetime_usetime = sav->lft_c->sadb_lifetime_usetime;
6278 lt = (struct sadb_lifetime *)(mtod(m, caddr_t) + len / 2);
6279 bcopy(sav->lft_s, lt, sizeof(*lt));
6282 /* set sadb_address for source */
6283 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
6284 &sav->sah->saidx.src.sa,
6285 FULLMASK, IPSEC_ULPROTO_ANY);
6292 /* set sadb_address for destination */
6293 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
6294 &sav->sah->saidx.dst.sa,
6295 FULLMASK, IPSEC_ULPROTO_ANY);
6302 if ((result->m_flags & M_PKTHDR) == 0) {
6307 if (result->m_len < sizeof(struct sadb_msg)) {
6308 result = m_pullup(result, sizeof(struct sadb_msg));
6309 if (result == NULL) {
6315 result->m_pkthdr.len = 0;
6316 for (m = result; m; m = m->m_next)
6317 result->m_pkthdr.len += m->m_len;
6319 mtod(result, struct sadb_msg *)->sadb_msg_len =
6320 PFKEY_UNIT64(result->m_pkthdr.len);
6323 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
6333 * SADB_FLUSH processing
6336 * from the ikmpd, and free all entries in secastree.
6340 * NOTE: to do is only marking SADB_SASTATE_DEAD.
6342 * m will always be freed.
6345 key_flush(so, m, mhp)
6348 const struct sadb_msghdr *mhp;
6350 struct sadb_msg *newmsg;
6351 struct secashead *sah, *nextsah;
6352 struct secasvar *sav, *nextsav;
6358 if (so == NULL || mhp == NULL || mhp->msg == NULL)
6359 panic("key_flush: NULL pointer is passed.\n");
6361 /* map satype to proto */
6362 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
6363 ipseclog((LOG_DEBUG, "key_flush: invalid satype is passed.\n"));
6364 return key_senderror(so, m, EINVAL);
6367 /* no SATYPE specified, i.e. flushing all SA. */
6368 for (sah = LIST_FIRST(&sahtree);
6371 nextsah = LIST_NEXT(sah, chain);
6373 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
6374 && proto != sah->saidx.proto)
6378 stateidx < _ARRAYLEN(saorder_state_alive);
6380 state = saorder_state_any[stateidx];
6381 for (sav = LIST_FIRST(&sah->savtree[state]);
6385 nextsav = LIST_NEXT(sav, chain);
6387 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
6392 sah->state = SADB_SASTATE_DEAD;
6395 if (m->m_len < sizeof(struct sadb_msg) ||
6396 sizeof(struct sadb_msg) > m->m_len + M_TRAILINGSPACE(m)) {
6397 ipseclog((LOG_DEBUG, "key_flush: No more memory.\n"));
6398 return key_senderror(so, m, ENOBUFS);
6404 m->m_pkthdr.len = m->m_len = sizeof(struct sadb_msg);
6405 newmsg = mtod(m, struct sadb_msg *);
6406 newmsg->sadb_msg_errno = 0;
6407 newmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
6409 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
6413 * SADB_DUMP processing
6414 * dump all entries including status of DEAD in SAD.
6417 * from the ikmpd, and dump all secasvar leaves
6422 * m will always be freed.
6425 key_dump(so, m, mhp)
6428 const struct sadb_msghdr *mhp;
6430 struct secashead *sah;
6431 struct secasvar *sav;
6437 struct sadb_msg *newmsg;
6441 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
6442 panic("key_dump: NULL pointer is passed.\n");
6444 /* map satype to proto */
6445 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
6446 ipseclog((LOG_DEBUG, "key_dump: invalid satype is passed.\n"));
6447 return key_senderror(so, m, EINVAL);
6450 /* count sav entries to be sent to the userland. */
6452 LIST_FOREACH(sah, &sahtree, chain) {
6453 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
6454 && proto != sah->saidx.proto)
6458 stateidx < _ARRAYLEN(saorder_state_any);
6460 state = saorder_state_any[stateidx];
6461 LIST_FOREACH(sav, &sah->savtree[state], chain) {
6468 return key_senderror(so, m, ENOENT);
6470 /* send this to the userland, one at a time. */
6472 LIST_FOREACH(sah, &sahtree, chain) {
6473 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
6474 && proto != sah->saidx.proto)
6477 /* map proto to satype */
6478 if ((satype = key_proto2satype(sah->saidx.proto)) == 0) {
6479 ipseclog((LOG_DEBUG, "key_dump: there was invalid proto in SAD.\n"));
6480 return key_senderror(so, m, EINVAL);
6484 stateidx < _ARRAYLEN(saorder_state_any);
6486 state = saorder_state_any[stateidx];
6487 LIST_FOREACH(sav, &sah->savtree[state], chain) {
6488 n = key_setdumpsa(sav, SADB_DUMP, satype,
6489 --cnt, mhp->msg->sadb_msg_pid);
6491 return key_senderror(so, m, ENOBUFS);
6493 key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
6503 * SADB_X_PROMISC processing
6505 * m will always be freed.
6508 key_promisc(so, m, mhp)
6511 const struct sadb_msghdr *mhp;
6516 if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
6517 panic("key_promisc: NULL pointer is passed.\n");
6519 olen = PFKEY_UNUNIT64(mhp->msg->sadb_msg_len);
6521 if (olen < sizeof(struct sadb_msg)) {
6523 return key_senderror(so, m, EINVAL);
6528 } else if (olen == sizeof(struct sadb_msg)) {
6529 /* enable/disable promisc mode */
6532 if ((kp = (struct keycb *)sotorawcb(so)) == NULL)
6533 return key_senderror(so, m, EINVAL);
6534 mhp->msg->sadb_msg_errno = 0;
6535 switch (mhp->msg->sadb_msg_satype) {
6538 kp->kp_promisc = mhp->msg->sadb_msg_satype;
6541 return key_senderror(so, m, EINVAL);
6544 /* send the original message back to everyone */
6545 mhp->msg->sadb_msg_errno = 0;
6546 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
6548 /* send packet as is */
6550 m_adj(m, PFKEY_ALIGN8(sizeof(struct sadb_msg)));
6552 /* TODO: if sadb_msg_seq is specified, send to specific pid */
6553 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
6557 static int (*key_typesw[]) __P((struct socket *, struct mbuf *,
6558 const struct sadb_msghdr *)) = {
6559 NULL, /* SADB_RESERVED */
6560 key_getspi, /* SADB_GETSPI */
6561 key_update, /* SADB_UPDATE */
6562 key_add, /* SADB_ADD */
6563 key_delete, /* SADB_DELETE */
6564 key_get, /* SADB_GET */
6565 key_acquire2, /* SADB_ACQUIRE */
6566 key_register, /* SADB_REGISTER */
6567 NULL, /* SADB_EXPIRE */
6568 key_flush, /* SADB_FLUSH */
6569 key_dump, /* SADB_DUMP */
6570 key_promisc, /* SADB_X_PROMISC */
6571 NULL, /* SADB_X_PCHANGE */
6572 key_spdadd, /* SADB_X_SPDUPDATE */
6573 key_spdadd, /* SADB_X_SPDADD */
6574 key_spddelete, /* SADB_X_SPDDELETE */
6575 key_spdget, /* SADB_X_SPDGET */
6576 NULL, /* SADB_X_SPDACQUIRE */
6577 key_spddump, /* SADB_X_SPDDUMP */
6578 key_spdflush, /* SADB_X_SPDFLUSH */
6579 key_spdadd, /* SADB_X_SPDSETIDX */
6580 NULL, /* SADB_X_SPDEXPIRE */
6581 key_spddelete2, /* SADB_X_SPDDELETE2 */
6585 * parse sadb_msg buffer to process PFKEYv2,
6586 * and create a data to response if needed.
6587 * I think to be dealed with mbuf directly.
6589 * msgp : pointer to pointer to a received buffer pulluped.
6590 * This is rewrited to response.
6591 * so : pointer to socket.
6593 * length for buffer to send to user process.
6600 struct sadb_msg *msg;
6601 struct sadb_msghdr mh;
6607 if (m == NULL || so == NULL)
6608 panic("key_parse: NULL pointer is passed.\n");
6610 #if 0 /*kdebug_sadb assumes msg in linear buffer*/
6611 KEYDEBUG(KEYDEBUG_KEY_DUMP,
6612 ipseclog((LOG_DEBUG, "key_parse: passed sadb_msg\n"));
6616 if (m->m_len < sizeof(struct sadb_msg)) {
6617 m = m_pullup(m, sizeof(struct sadb_msg));
6621 msg = mtod(m, struct sadb_msg *);
6622 orglen = PFKEY_UNUNIT64(msg->sadb_msg_len);
6623 target = KEY_SENDUP_ONE;
6625 if ((m->m_flags & M_PKTHDR) == 0 ||
6626 m->m_pkthdr.len != m->m_pkthdr.len) {
6627 ipseclog((LOG_DEBUG, "key_parse: invalid message length.\n"));
6628 pfkeystat.out_invlen++;
6633 if (msg->sadb_msg_version != PF_KEY_V2) {
6634 ipseclog((LOG_DEBUG,
6635 "key_parse: PF_KEY version %u is mismatched.\n",
6636 msg->sadb_msg_version));
6637 pfkeystat.out_invver++;
6642 if (msg->sadb_msg_type > SADB_MAX) {
6643 ipseclog((LOG_DEBUG, "key_parse: invalid type %u is passed.\n",
6644 msg->sadb_msg_type));
6645 pfkeystat.out_invmsgtype++;
6650 /* for old-fashioned code - should be nuked */
6651 if (m->m_pkthdr.len > MCLBYTES) {
6658 MGETHDR(n, M_DONTWAIT, MT_DATA);
6659 if (n && m->m_pkthdr.len > MHLEN) {
6660 MCLGET(n, M_DONTWAIT);
6661 if ((n->m_flags & M_EXT) == 0) {
6670 m_copydata(m, 0, m->m_pkthdr.len, mtod(n, caddr_t));
6671 n->m_pkthdr.len = n->m_len = m->m_pkthdr.len;
6677 /* align the mbuf chain so that extensions are in contiguous region. */
6678 error = key_align(m, &mh);
6682 if (m->m_next) { /*XXX*/
6690 switch (msg->sadb_msg_satype) {
6691 case SADB_SATYPE_UNSPEC:
6692 switch (msg->sadb_msg_type) {
6700 ipseclog((LOG_DEBUG, "key_parse: must specify satype "
6701 "when msg type=%u.\n", msg->sadb_msg_type));
6702 pfkeystat.out_invsatype++;
6707 case SADB_SATYPE_AH:
6708 case SADB_SATYPE_ESP:
6709 case SADB_X_SATYPE_IPCOMP:
6710 switch (msg->sadb_msg_type) {
6712 case SADB_X_SPDDELETE:
6714 case SADB_X_SPDDUMP:
6715 case SADB_X_SPDFLUSH:
6716 case SADB_X_SPDSETIDX:
6717 case SADB_X_SPDUPDATE:
6718 case SADB_X_SPDDELETE2:
6719 ipseclog((LOG_DEBUG, "key_parse: illegal satype=%u\n",
6720 msg->sadb_msg_type));
6721 pfkeystat.out_invsatype++;
6726 case SADB_SATYPE_RSVP:
6727 case SADB_SATYPE_OSPFV2:
6728 case SADB_SATYPE_RIPV2:
6729 case SADB_SATYPE_MIP:
6730 ipseclog((LOG_DEBUG, "key_parse: type %u isn't supported.\n",
6731 msg->sadb_msg_satype));
6732 pfkeystat.out_invsatype++;
6735 case 1: /* XXX: What does it do? */
6736 if (msg->sadb_msg_type == SADB_X_PROMISC)
6740 ipseclog((LOG_DEBUG, "key_parse: invalid type %u is passed.\n",
6741 msg->sadb_msg_satype));
6742 pfkeystat.out_invsatype++;
6747 /* check field of upper layer protocol and address family */
6748 if (mh.ext[SADB_EXT_ADDRESS_SRC] != NULL
6749 && mh.ext[SADB_EXT_ADDRESS_DST] != NULL) {
6750 struct sadb_address *src0, *dst0;
6753 src0 = (struct sadb_address *)(mh.ext[SADB_EXT_ADDRESS_SRC]);
6754 dst0 = (struct sadb_address *)(mh.ext[SADB_EXT_ADDRESS_DST]);
6756 /* check upper layer protocol */
6757 if (src0->sadb_address_proto != dst0->sadb_address_proto) {
6758 ipseclog((LOG_DEBUG, "key_parse: upper layer protocol mismatched.\n"));
6759 pfkeystat.out_invaddr++;
6765 if (PFKEY_ADDR_SADDR(src0)->sa_family !=
6766 PFKEY_ADDR_SADDR(dst0)->sa_family) {
6767 ipseclog((LOG_DEBUG, "key_parse: address family mismatched.\n"));
6768 pfkeystat.out_invaddr++;
6772 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
6773 PFKEY_ADDR_SADDR(dst0)->sa_len) {
6774 ipseclog((LOG_DEBUG,
6775 "key_parse: address struct size mismatched.\n"));
6776 pfkeystat.out_invaddr++;
6781 switch (PFKEY_ADDR_SADDR(src0)->sa_family) {
6783 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
6784 sizeof(struct sockaddr_in)) {
6785 pfkeystat.out_invaddr++;
6791 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
6792 sizeof(struct sockaddr_in6)) {
6793 pfkeystat.out_invaddr++;
6799 ipseclog((LOG_DEBUG,
6800 "key_parse: unsupported address family.\n"));
6801 pfkeystat.out_invaddr++;
6802 error = EAFNOSUPPORT;
6806 switch (PFKEY_ADDR_SADDR(src0)->sa_family) {
6808 plen = sizeof(struct in_addr) << 3;
6811 plen = sizeof(struct in6_addr) << 3;
6814 plen = 0; /*fool gcc*/
6818 /* check max prefix length */
6819 if (src0->sadb_address_prefixlen > plen ||
6820 dst0->sadb_address_prefixlen > plen) {
6821 ipseclog((LOG_DEBUG,
6822 "key_parse: illegal prefixlen.\n"));
6823 pfkeystat.out_invaddr++;
6829 * prefixlen == 0 is valid because there can be a case when
6830 * all addresses are matched.
6834 if (msg->sadb_msg_type >= sizeof(key_typesw)/sizeof(key_typesw[0]) ||
6835 key_typesw[msg->sadb_msg_type] == NULL) {
6836 pfkeystat.out_invmsgtype++;
6841 return (*key_typesw[msg->sadb_msg_type])(so, m, &mh);
6844 msg->sadb_msg_errno = error;
6845 return key_sendup_mbuf(so, m, target);
6849 key_senderror(so, m, code)
6854 struct sadb_msg *msg;
6856 if (m->m_len < sizeof(struct sadb_msg))
6857 panic("invalid mbuf passed to key_senderror");
6859 msg = mtod(m, struct sadb_msg *);
6860 msg->sadb_msg_errno = code;
6861 return key_sendup_mbuf(so, m, KEY_SENDUP_ONE);
6865 * set the pointer to each header into message buffer.
6866 * m will be freed on error.
6867 * XXX larger-than-MCLBYTES extension?
6872 struct sadb_msghdr *mhp;
6875 struct sadb_ext *ext;
6881 if (m == NULL || mhp == NULL)
6882 panic("key_align: NULL pointer is passed.\n");
6883 if (m->m_len < sizeof(struct sadb_msg))
6884 panic("invalid mbuf passed to key_align");
6887 bzero(mhp, sizeof(*mhp));
6889 mhp->msg = mtod(m, struct sadb_msg *);
6890 mhp->ext[0] = (struct sadb_ext *)mhp->msg; /*XXX backward compat */
6892 end = PFKEY_UNUNIT64(mhp->msg->sadb_msg_len);
6893 extlen = end; /*just in case extlen is not updated*/
6894 for (off = sizeof(struct sadb_msg); off < end; off += extlen) {
6895 n = m_pulldown(m, off, sizeof(struct sadb_ext), &toff);
6897 /* m is already freed */
6900 ext = (struct sadb_ext *)(mtod(n, caddr_t) + toff);
6903 switch (ext->sadb_ext_type) {
6905 case SADB_EXT_ADDRESS_SRC:
6906 case SADB_EXT_ADDRESS_DST:
6907 case SADB_EXT_ADDRESS_PROXY:
6908 case SADB_EXT_LIFETIME_CURRENT:
6909 case SADB_EXT_LIFETIME_HARD:
6910 case SADB_EXT_LIFETIME_SOFT:
6911 case SADB_EXT_KEY_AUTH:
6912 case SADB_EXT_KEY_ENCRYPT:
6913 case SADB_EXT_IDENTITY_SRC:
6914 case SADB_EXT_IDENTITY_DST:
6915 case SADB_EXT_SENSITIVITY:
6916 case SADB_EXT_PROPOSAL:
6917 case SADB_EXT_SUPPORTED_AUTH:
6918 case SADB_EXT_SUPPORTED_ENCRYPT:
6919 case SADB_EXT_SPIRANGE:
6920 case SADB_X_EXT_POLICY:
6921 case SADB_X_EXT_SA2:
6922 /* duplicate check */
6924 * XXX Are there duplication payloads of either
6925 * KEY_AUTH or KEY_ENCRYPT ?
6927 if (mhp->ext[ext->sadb_ext_type] != NULL) {
6928 ipseclog((LOG_DEBUG,
6929 "key_align: duplicate ext_type %u "
6930 "is passed.\n", ext->sadb_ext_type));
6932 pfkeystat.out_dupext++;
6937 ipseclog((LOG_DEBUG,
6938 "key_align: invalid ext_type %u is passed.\n",
6939 ext->sadb_ext_type));
6941 pfkeystat.out_invexttype++;
6945 extlen = PFKEY_UNUNIT64(ext->sadb_ext_len);
6947 if (key_validate_ext(ext, extlen)) {
6949 pfkeystat.out_invlen++;
6953 n = m_pulldown(m, off, extlen, &toff);
6955 /* m is already freed */
6958 ext = (struct sadb_ext *)(mtod(n, caddr_t) + toff);
6960 mhp->ext[ext->sadb_ext_type] = ext;
6961 mhp->extoff[ext->sadb_ext_type] = off;
6962 mhp->extlen[ext->sadb_ext_type] = extlen;
6967 pfkeystat.out_invlen++;
6975 key_validate_ext(ext, len)
6976 const struct sadb_ext *ext;
6979 const struct sockaddr *sa;
6980 enum { NONE, ADDR } checktype = NONE;
6982 const int sal = offsetof(struct sockaddr, sa_len) + sizeof(sa->sa_len);
6984 if (len != PFKEY_UNUNIT64(ext->sadb_ext_len))
6987 /* if it does not match minimum/maximum length, bail */
6988 if (ext->sadb_ext_type >= sizeof(minsize) / sizeof(minsize[0]) ||
6989 ext->sadb_ext_type >= sizeof(maxsize) / sizeof(maxsize[0]))
6991 if (!minsize[ext->sadb_ext_type] || len < minsize[ext->sadb_ext_type])
6993 if (maxsize[ext->sadb_ext_type] && len > maxsize[ext->sadb_ext_type])
6996 /* more checks based on sadb_ext_type XXX need more */
6997 switch (ext->sadb_ext_type) {
6998 case SADB_EXT_ADDRESS_SRC:
6999 case SADB_EXT_ADDRESS_DST:
7000 case SADB_EXT_ADDRESS_PROXY:
7001 baselen = PFKEY_ALIGN8(sizeof(struct sadb_address));
7004 case SADB_EXT_IDENTITY_SRC:
7005 case SADB_EXT_IDENTITY_DST:
7006 if (((const struct sadb_ident *)ext)->sadb_ident_type ==
7007 SADB_X_IDENTTYPE_ADDR) {
7008 baselen = PFKEY_ALIGN8(sizeof(struct sadb_ident));
7018 switch (checktype) {
7022 sa = (const struct sockaddr *)(((const u_int8_t*)ext)+baselen);
7023 if (len < baselen + sal)
7025 if (baselen + PFKEY_ALIGN8(sa->sa_len) != len)
7038 for (i = 0; i < IPSEC_DIR_MAX; i++) {
7039 LIST_INIT(&sptree[i]);
7042 LIST_INIT(&sahtree);
7044 for (i = 0; i <= SADB_SATYPE_MAX; i++) {
7045 LIST_INIT(®tree[i]);
7048 #ifndef IPSEC_NONBLOCK_ACQUIRE
7049 LIST_INIT(&acqtree);
7051 LIST_INIT(&spacqtree);
7053 /* system default */
7054 ip4_def_policy.policy = IPSEC_POLICY_NONE;
7055 ip4_def_policy.refcnt++; /*never reclaim this*/
7057 #ifndef IPSEC_DEBUG2
7058 timeout((void *)key_timehandler, (void *)0, hz);
7059 #endif /*IPSEC_DEBUG2*/
7061 /* initialize key statistics */
7062 keystat.getspi_count = 1;
7064 printf("IPsec: Initialized Security Association Processing.\n");
7070 * XXX: maybe This function is called after INBOUND IPsec processing.
7072 * Special check for tunnel-mode packets.
7073 * We must make some checks for consistency between inner and outer IP header.
7075 * xxx more checks to be provided
7078 key_checktunnelsanity(sav, family, src, dst)
7079 struct secasvar *sav;
7085 if (sav->sah == NULL)
7086 panic("sav->sah == NULL at key_checktunnelsanity");
7088 /* XXX: check inner IP header */
7094 #define hostnamelen strlen(hostname)
7097 * Get FQDN for the host.
7098 * If the administrator configured hostname (by hostname(1)) without
7099 * domain name, returns nothing.
7106 static char fqdn[MAXHOSTNAMELEN + 1];
7111 /* check if it comes with domain name. */
7113 for (i = 0; i < hostnamelen; i++) {
7114 if (hostname[i] == '.')
7120 /* NOTE: hostname may not be NUL-terminated. */
7121 bzero(fqdn, sizeof(fqdn));
7122 bcopy(hostname, fqdn, hostnamelen);
7123 fqdn[hostnamelen] = '\0';
7128 * get username@FQDN for the host/user.
7134 static char userfqdn[MAXHOSTNAMELEN + MAXLOGNAME + 2];
7135 struct proc *p = curproc;
7138 if (!p || !p->p_pgrp || !p->p_pgrp->pg_session)
7140 if (!(host = key_getfqdn()))
7143 /* NOTE: s_login may not be-NUL terminated. */
7144 bzero(userfqdn, sizeof(userfqdn));
7145 bcopy(p->p_pgrp->pg_session->s_login, userfqdn, MAXLOGNAME);
7146 userfqdn[MAXLOGNAME] = '\0'; /* safeguard */
7147 q = userfqdn + strlen(userfqdn);
7149 bcopy(host, q, strlen(host));
7157 /* record data transfer on SA, and update timestamps */
7159 key_sa_recordxfer(sav, m)
7160 struct secasvar *sav;
7163 KASSERT(sav != NULL, ("key_sa_recordxfer: Null secasvar"));
7164 KASSERT(m != NULL, ("key_sa_recordxfer: Null mbuf"));
7169 * XXX Currently, there is a difference of bytes size
7170 * between inbound and outbound processing.
7172 sav->lft_c->sadb_lifetime_bytes += m->m_pkthdr.len;
7173 /* to check bytes lifetime is done in key_timehandler(). */
7176 * We use the number of packets as the unit of
7177 * sadb_lifetime_allocations. We increment the variable
7178 * whenever {esp,ah}_{in,out}put is called.
7180 sav->lft_c->sadb_lifetime_allocations++;
7181 /* XXX check for expires? */
7184 * NOTE: We record CURRENT sadb_lifetime_usetime by using wall clock,
7185 * in seconds. HARD and SOFT lifetime are measured by the time
7186 * difference (again in seconds) from sadb_lifetime_usetime.
7190 * -----+-----+--------+---> t
7191 * <--------------> HARD
7194 sav->lft_c->sadb_lifetime_usetime = time_second;
7195 /* XXX check for expires? */
7202 key_sa_routechange(dst)
7203 struct sockaddr *dst;
7205 struct secashead *sah;
7208 LIST_FOREACH(sah, &sahtree, chain) {
7209 ro = &sah->sa_route;
7210 if (ro->ro_rt && dst->sa_len == ro->ro_dst.sa_len
7211 && bcmp(dst, &ro->ro_dst, dst->sa_len) == 0) {
7213 ro->ro_rt = (struct rtentry *)NULL;
7221 key_sa_chgstate(sav, state)
7222 struct secasvar *sav;
7226 panic("key_sa_chgstate called with sav == NULL");
7228 if (sav->state == state)
7231 if (__LIST_CHAINED(sav))
7232 LIST_REMOVE(sav, chain);
7235 LIST_INSERT_HEAD(&sav->sah->savtree[state], sav, chain);
7240 struct secasvar *sav;
7244 panic("key_sa_stir_iv called with sav == NULL");
7245 key_randomfill(sav->iv, sav->ivlen);
7249 static struct mbuf *
7253 struct mbuf *m = NULL, *n;
7258 MGET(n, M_DONTWAIT, MT_DATA);
7259 if (n && len > MLEN)
7260 MCLGET(n, M_DONTWAIT);
7268 n->m_len = M_TRAILINGSPACE(n);
7269 /* use the bottom of mbuf, hoping we can prepend afterwards */
7270 if (n->m_len > len) {
7271 t = (n->m_len - len) & ~(sizeof(long) - 1);