1 /* $NetBSD: key.c,v 1.61 2009/03/18 10:22:43 cegger Exp $ */
2 /* $FreeBSD: src/sys/netipsec/key.c,v 1.3.2.3 2004/02/14 22:23:23 bms Exp $ */
3 /* $KAME: key.c,v 1.191 2001/06/27 10:46:49 sakane Exp $ */
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: key.c,v 1.61 2009/03/18 10:22:43 cegger Exp $");
38 * This code is referd to RFC 2367
43 #include "opt_inet6.h"
45 #include "opt_ipsec.h"
47 #include "opt_gateway.h"
50 #include <sys/types.h>
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/callout.h>
54 #include <sys/kernel.h>
56 #include <sys/domain.h>
57 #include <sys/protosw.h>
58 #include <sys/malloc.h>
59 #include <sys/socket.h>
60 #include <sys/socketvar.h>
61 #include <sys/sysctl.h>
62 #include <sys/errno.h>
64 #include <sys/queue.h>
65 #include <sys/syslog.h>
69 #include <net/route.h>
70 #include <net/raw_cb.h>
72 #include <netinet/in.h>
73 #include <netinet/in_systm.h>
74 #include <netinet/ip.h>
75 #include <netinet/in_var.h>
77 #include <netinet/ip_var.h>
81 #include <netinet/ip6.h>
82 #include <netinet6/in6_var.h>
83 #include <netinet6/ip6_var.h>
87 #include <netinet/in_pcb.h>
90 #include <netinet6/in6_pcb.h>
93 #include <net/pfkeyv2.h>
94 #include <netipsec/keydb.h>
95 #include <netipsec/key.h>
96 #include <netipsec/keysock.h>
97 #include <netipsec/key_debug.h>
99 #include <netipsec/ipsec.h>
101 #include <netipsec/ipsec6.h>
103 #include <netipsec/ipsec_private.h>
105 #include <netipsec/xform.h>
106 #include <netipsec/ipsec_osdep.h>
107 #include <netipsec/ipcomp.h>
110 #include <machine/stdarg.h>
113 #include <net/net_osdep.h>
115 #define FULLMASK 0xff
116 #define _BITS(bytes) ((bytes) << 3)
118 percpu_t
*pfkeystat_percpu
;
121 * Note on SA reference counting:
122 * - SAs that are not in DEAD state will have (total external reference + 1)
123 * following value in reference count field. they cannot be freed and are
124 * referenced from SA header.
125 * - SAs that are in DEAD state will have (total external reference)
126 * in reference count field. they are ready to be freed. reference from
127 * SA header will be removed in key_delsav(), when the reference count
128 * field hits 0 (= no external reference other than from SA header.
131 u_int32_t key_debug_level
= 0;
132 static u_int key_spi_trycnt
= 1000;
133 static u_int32_t key_spi_minval
= 0x100;
134 static u_int32_t key_spi_maxval
= 0x0fffffff; /* XXX */
135 static u_int32_t policy_id
= 0;
136 static u_int key_int_random
= 60; /*interval to initialize randseed,1(m)*/
137 static u_int key_larval_lifetime
= 30; /* interval to expire acquiring, 30(s)*/
138 static int key_blockacq_count
= 10; /* counter for blocking SADB_ACQUIRE.*/
139 static int key_blockacq_lifetime
= 20; /* lifetime for blocking SADB_ACQUIRE.*/
140 static int key_prefered_oldsa
= 0; /* prefered old sa rather than new sa.*/
142 static u_int32_t acq_seq
= 0;
143 static int key_tick_init_random
= 0;
145 static LIST_HEAD(_sptree
, secpolicy
) sptree
[IPSEC_DIR_MAX
]; /* SPD */
146 static LIST_HEAD(_sahtree
, secashead
) sahtree
; /* SAD */
147 static LIST_HEAD(_regtree
, secreg
) regtree
[SADB_SATYPE_MAX
+ 1];
149 #ifndef IPSEC_NONBLOCK_ACQUIRE
150 static LIST_HEAD(_acqtree
, secacq
) acqtree
; /* acquiring list */
152 static LIST_HEAD(_spacqtree
, secspacq
) spacqtree
; /* SP acquiring list */
154 /* search order for SAs */
155 static u_int saorder_state_valid
[] = {
156 SADB_SASTATE_DYING
, SADB_SASTATE_MATURE
,
158 * This order is important because we must select the oldest SA
159 * for outbound processing. For inbound, This is not important.
162 static u_int saorder_state_alive
[] = {
164 SADB_SASTATE_MATURE
, SADB_SASTATE_DYING
, SADB_SASTATE_LARVAL
166 static u_int saorder_state_any
[] = {
167 SADB_SASTATE_MATURE
, SADB_SASTATE_DYING
,
168 SADB_SASTATE_LARVAL
, SADB_SASTATE_DEAD
171 static const int minsize
[] = {
172 sizeof(struct sadb_msg
), /* SADB_EXT_RESERVED */
173 sizeof(struct sadb_sa
), /* SADB_EXT_SA */
174 sizeof(struct sadb_lifetime
), /* SADB_EXT_LIFETIME_CURRENT */
175 sizeof(struct sadb_lifetime
), /* SADB_EXT_LIFETIME_HARD */
176 sizeof(struct sadb_lifetime
), /* SADB_EXT_LIFETIME_SOFT */
177 sizeof(struct sadb_address
), /* SADB_EXT_ADDRESS_SRC */
178 sizeof(struct sadb_address
), /* SADB_EXT_ADDRESS_DST */
179 sizeof(struct sadb_address
), /* SADB_EXT_ADDRESS_PROXY */
180 sizeof(struct sadb_key
), /* SADB_EXT_KEY_AUTH */
181 sizeof(struct sadb_key
), /* SADB_EXT_KEY_ENCRYPT */
182 sizeof(struct sadb_ident
), /* SADB_EXT_IDENTITY_SRC */
183 sizeof(struct sadb_ident
), /* SADB_EXT_IDENTITY_DST */
184 sizeof(struct sadb_sens
), /* SADB_EXT_SENSITIVITY */
185 sizeof(struct sadb_prop
), /* SADB_EXT_PROPOSAL */
186 sizeof(struct sadb_supported
), /* SADB_EXT_SUPPORTED_AUTH */
187 sizeof(struct sadb_supported
), /* SADB_EXT_SUPPORTED_ENCRYPT */
188 sizeof(struct sadb_spirange
), /* SADB_EXT_SPIRANGE */
189 0, /* SADB_X_EXT_KMPRIVATE */
190 sizeof(struct sadb_x_policy
), /* SADB_X_EXT_POLICY */
191 sizeof(struct sadb_x_sa2
), /* SADB_X_SA2 */
192 sizeof(struct sadb_x_nat_t_type
), /* SADB_X_EXT_NAT_T_TYPE */
193 sizeof(struct sadb_x_nat_t_port
), /* SADB_X_EXT_NAT_T_SPORT */
194 sizeof(struct sadb_x_nat_t_port
), /* SADB_X_EXT_NAT_T_DPORT */
195 sizeof(struct sadb_address
), /* SADB_X_EXT_NAT_T_OA */
196 sizeof(struct sadb_x_nat_t_frag
), /* SADB_X_EXT_NAT_T_FRAG */
198 static const int maxsize
[] = {
199 sizeof(struct sadb_msg
), /* SADB_EXT_RESERVED */
200 sizeof(struct sadb_sa
), /* SADB_EXT_SA */
201 sizeof(struct sadb_lifetime
), /* SADB_EXT_LIFETIME_CURRENT */
202 sizeof(struct sadb_lifetime
), /* SADB_EXT_LIFETIME_HARD */
203 sizeof(struct sadb_lifetime
), /* SADB_EXT_LIFETIME_SOFT */
204 0, /* SADB_EXT_ADDRESS_SRC */
205 0, /* SADB_EXT_ADDRESS_DST */
206 0, /* SADB_EXT_ADDRESS_PROXY */
207 0, /* SADB_EXT_KEY_AUTH */
208 0, /* SADB_EXT_KEY_ENCRYPT */
209 0, /* SADB_EXT_IDENTITY_SRC */
210 0, /* SADB_EXT_IDENTITY_DST */
211 0, /* SADB_EXT_SENSITIVITY */
212 0, /* SADB_EXT_PROPOSAL */
213 0, /* SADB_EXT_SUPPORTED_AUTH */
214 0, /* SADB_EXT_SUPPORTED_ENCRYPT */
215 sizeof(struct sadb_spirange
), /* SADB_EXT_SPIRANGE */
216 0, /* SADB_X_EXT_KMPRIVATE */
217 0, /* SADB_X_EXT_POLICY */
218 sizeof(struct sadb_x_sa2
), /* SADB_X_SA2 */
219 sizeof(struct sadb_x_nat_t_type
), /* SADB_X_EXT_NAT_T_TYPE */
220 sizeof(struct sadb_x_nat_t_port
), /* SADB_X_EXT_NAT_T_SPORT */
221 sizeof(struct sadb_x_nat_t_port
), /* SADB_X_EXT_NAT_T_DPORT */
222 0, /* SADB_X_EXT_NAT_T_OA */
223 sizeof(struct sadb_x_nat_t_frag
), /* SADB_X_EXT_NAT_T_FRAG */
226 static int ipsec_esp_keymin
= 256;
227 static int ipsec_esp_auth
= 0;
228 static int ipsec_ah_keymin
= 128;
231 SYSCTL_DECL(_net_key
);
235 SYSCTL_INT(_net_key
, KEYCTL_DEBUG_LEVEL
, debug
, CTLFLAG_RW
, \
236 &key_debug_level
, 0, "");
238 /* max count of trial for the decision of spi value */
239 SYSCTL_INT(_net_key
, KEYCTL_SPI_TRY
, spi_trycnt
, CTLFLAG_RW
, \
240 &key_spi_trycnt
, 0, "");
242 /* minimum spi value to allocate automatically. */
243 SYSCTL_INT(_net_key
, KEYCTL_SPI_MIN_VALUE
, spi_minval
, CTLFLAG_RW
, \
244 &key_spi_minval
, 0, "");
246 /* maximun spi value to allocate automatically. */
247 SYSCTL_INT(_net_key
, KEYCTL_SPI_MAX_VALUE
, spi_maxval
, CTLFLAG_RW
, \
248 &key_spi_maxval
, 0, "");
250 /* interval to initialize randseed */
251 SYSCTL_INT(_net_key
, KEYCTL_RANDOM_INT
, int_random
, CTLFLAG_RW
, \
252 &key_int_random
, 0, "");
254 /* lifetime for larval SA */
255 SYSCTL_INT(_net_key
, KEYCTL_LARVAL_LIFETIME
, larval_lifetime
, CTLFLAG_RW
, \
256 &key_larval_lifetime
, 0, "");
258 /* counter for blocking to send SADB_ACQUIRE to IKEd */
259 SYSCTL_INT(_net_key
, KEYCTL_BLOCKACQ_COUNT
, blockacq_count
, CTLFLAG_RW
, \
260 &key_blockacq_count
, 0, "");
262 /* lifetime for blocking to send SADB_ACQUIRE to IKEd */
263 SYSCTL_INT(_net_key
, KEYCTL_BLOCKACQ_LIFETIME
, blockacq_lifetime
, CTLFLAG_RW
, \
264 &key_blockacq_lifetime
, 0, "");
267 SYSCTL_INT(_net_key
, KEYCTL_ESP_AUTH
, esp_auth
, CTLFLAG_RW
, \
268 &ipsec_esp_auth
, 0, "");
270 /* minimum ESP key length */
271 SYSCTL_INT(_net_key
, KEYCTL_ESP_KEYMIN
, esp_keymin
, CTLFLAG_RW
, \
272 &ipsec_esp_keymin
, 0, "");
274 /* minimum AH key length */
275 SYSCTL_INT(_net_key
, KEYCTL_AH_KEYMIN
, ah_keymin
, CTLFLAG_RW
, \
276 &ipsec_ah_keymin
, 0, "");
278 /* perfered old SA rather than new SA */
279 SYSCTL_INT(_net_key
, KEYCTL_PREFERED_OLDSA
, prefered_oldsa
, CTLFLAG_RW
,\
280 &key_prefered_oldsa
, 0, "");
281 #endif /* SYSCTL_INT */
284 #define LIST_FOREACH(elm, head, field) \
285 for (elm = LIST_FIRST(head); elm; elm = LIST_NEXT(elm, field))
287 #define __LIST_CHAINED(elm) \
288 (!((elm)->chain.le_next == NULL && (elm)->chain.le_prev == NULL))
289 #define LIST_INSERT_TAIL(head, elm, type, field) \
291 struct type *curelm = LIST_FIRST(head); \
292 if (curelm == NULL) {\
293 LIST_INSERT_HEAD(head, elm, field); \
295 while (LIST_NEXT(curelm, field)) \
296 curelm = LIST_NEXT(curelm, field);\
297 LIST_INSERT_AFTER(curelm, elm, field);\
301 #define KEY_CHKSASTATE(head, sav, name) \
303 if ((head) != (sav)) { \
304 ipseclog((LOG_DEBUG, "%s: state mismatched (TREE=%d SA=%d)\n", \
305 (name), (head), (sav))); \
310 #define KEY_CHKSPDIR(head, sp, name) \
312 if ((head) != (sp)) { \
313 ipseclog((LOG_DEBUG, "%s: direction mismatched (TREE=%d SP=%d), " \
314 "anyway continue.\n", \
315 (name), (head), (sp))); \
319 MALLOC_DEFINE(M_SECA
, "key mgmt", "security associations, key management");
322 #define KMALLOC(p, t, n) \
323 ((p) = (t) malloc((unsigned long)(n), M_SECA, M_NOWAIT))
327 #define KMALLOC(p, t, n) \
329 ((p) = (t)malloc((unsigned long)(n), M_SECA, M_NOWAIT)); \
330 printf("%s %d: %p <- KMALLOC(%s, %d)\n", \
331 __FILE__, __LINE__, (p), #t, n); \
336 printf("%s %d: %p -> KFREE()\n", __FILE__, __LINE__, (p)); \
342 * set parameters into secpolicyindex buffer.
343 * Must allocate secpolicyindex buffer passed to this function.
345 #define KEY_SETSECSPIDX(_dir, s, d, ps, pd, ulp, idx) \
347 memset((idx), 0, sizeof(struct secpolicyindex)); \
348 (idx)->dir = (_dir); \
349 (idx)->prefs = (ps); \
350 (idx)->prefd = (pd); \
351 (idx)->ul_proto = (ulp); \
352 memcpy(&(idx)->src, (s), ((const struct sockaddr *)(s))->sa_len); \
353 memcpy(&(idx)->dst, (d), ((const struct sockaddr *)(d))->sa_len); \
357 * set parameters into secasindex buffer.
358 * Must allocate secasindex buffer before calling this function.
361 key_setsecasidx (int, int, int, const struct sadb_address
*,
362 const struct sadb_address
*, struct secasindex
*);
366 u_long getspi_count
; /* the avarage of count to try to get new SPI */
370 struct sadb_msg
*msg
;
371 struct sadb_ext
*ext
[SADB_EXT_MAX
+ 1];
372 int extoff
[SADB_EXT_MAX
+ 1];
373 int extlen
[SADB_EXT_MAX
+ 1];
376 static struct secasvar
*key_allocsa_policy (const struct secasindex
*);
377 static void key_freesp_so (struct secpolicy
**);
378 static struct secasvar
*key_do_allocsa_policy (struct secashead
*, u_int
);
379 static void key_delsp (struct secpolicy
*);
380 static struct secpolicy
*key_getsp (struct secpolicyindex
*);
381 static struct secpolicy
*key_getspbyid (u_int32_t
);
382 static u_int16_t
key_newreqid (void);
383 static struct mbuf
*key_gather_mbuf (struct mbuf
*,
384 const struct sadb_msghdr
*, int, int, ...);
385 static int key_spdadd (struct socket
*, struct mbuf
*,
386 const struct sadb_msghdr
*);
387 static u_int32_t
key_getnewspid (void);
388 static int key_spddelete (struct socket
*, struct mbuf
*,
389 const struct sadb_msghdr
*);
390 static int key_spddelete2 (struct socket
*, struct mbuf
*,
391 const struct sadb_msghdr
*);
392 static int key_spdget (struct socket
*, struct mbuf
*,
393 const struct sadb_msghdr
*);
394 static int key_spdflush (struct socket
*, struct mbuf
*,
395 const struct sadb_msghdr
*);
396 static int key_spddump (struct socket
*, struct mbuf
*,
397 const struct sadb_msghdr
*);
398 static struct mbuf
* key_setspddump (int *errorp
, pid_t
);
399 static struct mbuf
* key_setspddump_chain (int *errorp
, int *lenp
, pid_t pid
);
401 static int key_nat_map (struct socket
*, struct mbuf
*,
402 const struct sadb_msghdr
*);
404 static struct mbuf
*key_setdumpsp (struct secpolicy
*,
405 u_int8_t
, u_int32_t
, pid_t
);
406 static u_int
key_getspreqmsglen (struct secpolicy
*);
407 static int key_spdexpire (struct secpolicy
*);
408 static struct secashead
*key_newsah (struct secasindex
*);
409 static void key_delsah (struct secashead
*);
410 static struct secasvar
*key_newsav (struct mbuf
*,
411 const struct sadb_msghdr
*, struct secashead
*, int *,
413 #define KEY_NEWSAV(m, sadb, sah, e) \
414 key_newsav(m, sadb, sah, e, __FILE__, __LINE__)
415 static void key_delsav (struct secasvar
*);
416 static struct secashead
*key_getsah (struct secasindex
*);
417 static struct secasvar
*key_checkspidup (struct secasindex
*, u_int32_t
);
418 static struct secasvar
*key_getsavbyspi (struct secashead
*, u_int32_t
);
419 static int key_setsaval (struct secasvar
*, struct mbuf
*,
420 const struct sadb_msghdr
*);
421 static int key_mature (struct secasvar
*);
422 static struct mbuf
*key_setdumpsa (struct secasvar
*, u_int8_t
,
423 u_int8_t
, u_int32_t
, u_int32_t
);
425 static struct mbuf
*key_setsadbxport (u_int16_t
, u_int16_t
);
426 static struct mbuf
*key_setsadbxtype (u_int16_t
);
428 static void key_porttosaddr (union sockaddr_union
*, u_int16_t
);
429 static int key_checksalen (const union sockaddr_union
*);
430 static struct mbuf
*key_setsadbmsg (u_int8_t
, u_int16_t
, u_int8_t
,
431 u_int32_t
, pid_t
, u_int16_t
);
432 static struct mbuf
*key_setsadbsa (struct secasvar
*);
433 static struct mbuf
*key_setsadbaddr (u_int16_t
,
434 const struct sockaddr
*, u_int8_t
, u_int16_t
);
436 static struct mbuf
*key_setsadbident (u_int16_t
, u_int16_t
, void *,
439 static struct mbuf
*key_setsadbxsa2 (u_int8_t
, u_int32_t
, u_int16_t
);
440 static struct mbuf
*key_setsadbxpolicy (u_int16_t
, u_int8_t
,
442 static void *key_newbuf (const void *, u_int
);
444 static int key_ismyaddr6 (struct sockaddr_in6
*);
447 /* flags for key_cmpsaidx() */
448 #define CMP_HEAD 1 /* protocol, addresses. */
449 #define CMP_MODE_REQID 2 /* additionally HEAD, reqid, mode. */
450 #define CMP_REQID 3 /* additionally HEAD, reaid. */
451 #define CMP_EXACTLY 4 /* all elements. */
452 static int key_cmpsaidx
453 (const struct secasindex
*, const struct secasindex
*, int);
455 static int key_sockaddrcmp (const struct sockaddr
*, const struct sockaddr
*, int);
456 static int key_bbcmp (const void *, const void *, u_int
);
457 static void key_srandom (void);
458 static u_int16_t
key_satype2proto (u_int8_t
);
459 static u_int8_t
key_proto2satype (u_int16_t
);
461 static int key_getspi (struct socket
*, struct mbuf
*,
462 const struct sadb_msghdr
*);
463 static u_int32_t
key_do_getnewspi (struct sadb_spirange
*,
464 struct secasindex
*);
466 static int key_handle_natt_info (struct secasvar
*,
467 const struct sadb_msghdr
*);
469 static int key_update (struct socket
*, struct mbuf
*,
470 const struct sadb_msghdr
*);
471 #ifdef IPSEC_DOSEQCHECK
472 static struct secasvar
*key_getsavbyseq (struct secashead
*, u_int32_t
);
474 static int key_add (struct socket
*, struct mbuf
*,
475 const struct sadb_msghdr
*);
476 static int key_setident (struct secashead
*, struct mbuf
*,
477 const struct sadb_msghdr
*);
478 static struct mbuf
*key_getmsgbuf_x1 (struct mbuf
*,
479 const struct sadb_msghdr
*);
480 static int key_delete (struct socket
*, struct mbuf
*,
481 const struct sadb_msghdr
*);
482 static int key_get (struct socket
*, struct mbuf
*,
483 const struct sadb_msghdr
*);
485 static void key_getcomb_setlifetime (struct sadb_comb
*);
486 static struct mbuf
*key_getcomb_esp (void);
487 static struct mbuf
*key_getcomb_ah (void);
488 static struct mbuf
*key_getcomb_ipcomp (void);
489 static struct mbuf
*key_getprop (const struct secasindex
*);
491 static int key_acquire (const struct secasindex
*, struct secpolicy
*);
492 #ifndef IPSEC_NONBLOCK_ACQUIRE
493 static struct secacq
*key_newacq (const struct secasindex
*);
494 static struct secacq
*key_getacq (const struct secasindex
*);
495 static struct secacq
*key_getacqbyseq (u_int32_t
);
497 static struct secspacq
*key_newspacq (struct secpolicyindex
*);
498 static struct secspacq
*key_getspacq (struct secpolicyindex
*);
499 static int key_acquire2 (struct socket
*, struct mbuf
*,
500 const struct sadb_msghdr
*);
501 static int key_register (struct socket
*, struct mbuf
*,
502 const struct sadb_msghdr
*);
503 static int key_expire (struct secasvar
*);
504 static int key_flush (struct socket
*, struct mbuf
*,
505 const struct sadb_msghdr
*);
506 static struct mbuf
*key_setdump_chain (u_int8_t req_satype
, int *errorp
,
507 int *lenp
, pid_t pid
);
508 static int key_dump (struct socket
*, struct mbuf
*,
509 const struct sadb_msghdr
*);
510 static int key_promisc (struct socket
*, struct mbuf
*,
511 const struct sadb_msghdr
*);
512 static int key_senderror (struct socket
*, struct mbuf
*, int);
513 static int key_validate_ext (const struct sadb_ext
*, int);
514 static int key_align (struct mbuf
*, struct sadb_msghdr
*);
516 static const char *key_getfqdn (void);
517 static const char *key_getuserfqdn (void);
519 static void key_sa_chgstate (struct secasvar
*, u_int8_t
);
520 static inline void key_sp_dead (struct secpolicy
*);
521 static void key_sp_unlink (struct secpolicy
*sp
);
523 static struct mbuf
*key_alloc_mbuf (int);
524 struct callout key_timehandler_ch
;
526 #define SA_ADDREF(p) do { \
528 IPSEC_ASSERT((p)->refcnt != 0, \
529 ("SA refcnt overflow at %s:%u", __FILE__, __LINE__)); \
531 #define SA_DELREF(p) do { \
532 IPSEC_ASSERT((p)->refcnt > 0, \
533 ("SA refcnt underflow at %s:%u", __FILE__, __LINE__)); \
537 #define SP_ADDREF(p) do { \
539 IPSEC_ASSERT((p)->refcnt != 0, \
540 ("SP refcnt overflow at %s:%u", __FILE__, __LINE__)); \
542 #define SP_DELREF(p) do { \
543 IPSEC_ASSERT((p)->refcnt > 0, \
544 ("SP refcnt underflow at %s:%u", __FILE__, __LINE__)); \
550 key_sp_dead(struct secpolicy
*sp
)
553 /* mark the SP dead */
554 sp
->state
= IPSEC_SPSTATE_DEAD
;
558 key_sp_unlink(struct secpolicy
*sp
)
561 /* remove from SP index */
562 if (__LIST_CHAINED(sp
)) {
563 LIST_REMOVE(sp
, chain
);
564 /* Release refcount held just for being on chain */
571 * Return 0 when there are known to be no SP's for the specified
572 * direction. Otherwise return 1. This is used by IPsec code
573 * to optimize performance.
576 key_havesp(u_int dir
)
578 return (dir
== IPSEC_DIR_INBOUND
|| dir
== IPSEC_DIR_OUTBOUND
?
579 LIST_FIRST(&sptree
[dir
]) != NULL
: 1);
582 /* %%% IPsec policy management */
584 * allocating a SP for OUTBOUND or INBOUND packet.
585 * Must call key_freesp() later.
586 * OUT: NULL: not found
587 * others: found and return the pointer.
590 key_allocsp(struct secpolicyindex
*spidx
, u_int dir
, const char* where
, int tag
)
592 struct secpolicy
*sp
;
595 IPSEC_ASSERT(spidx
!= NULL
, ("key_allocsp: null spidx"));
596 IPSEC_ASSERT(dir
== IPSEC_DIR_INBOUND
|| dir
== IPSEC_DIR_OUTBOUND
,
597 ("key_allocsp: invalid direction %u", dir
));
599 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
600 printf("DP key_allocsp from %s:%u\n", where
, tag
));
603 s
= splsoftnet(); /*called from softclock()*/
604 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
605 printf("*** objects\n");
606 kdebug_secpolicyindex(spidx
));
608 LIST_FOREACH(sp
, &sptree
[dir
], chain
) {
609 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
610 printf("*** in SPD\n");
611 kdebug_secpolicyindex(&sp
->spidx
));
613 if (sp
->state
== IPSEC_SPSTATE_DEAD
)
615 if (key_cmpspidx_withmask(&sp
->spidx
, spidx
))
622 KEY_CHKSPDIR(sp
->spidx
.dir
, dir
, "key_allocsp");
624 /* found a SPD entry */
625 sp
->lastused
= time_second
;
630 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
631 printf("DP key_allocsp return SP:%p (ID=%u) refcnt %u\n",
632 sp
, sp
? sp
->id
: 0, sp
? sp
->refcnt
: 0));
637 * allocating a SP for OUTBOUND or INBOUND packet.
638 * Must call key_freesp() later.
639 * OUT: NULL: not found
640 * others: found and return the pointer.
643 key_allocsp2(u_int32_t spi
,
644 union sockaddr_union
*dst
,
647 const char* where
, int tag
)
649 struct secpolicy
*sp
;
652 IPSEC_ASSERT(dst
!= NULL
, ("key_allocsp2: null dst"));
653 IPSEC_ASSERT(dir
== IPSEC_DIR_INBOUND
|| dir
== IPSEC_DIR_OUTBOUND
,
654 ("key_allocsp2: invalid direction %u", dir
));
656 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
657 printf("DP key_allocsp2 from %s:%u\n", where
, tag
));
660 s
= splsoftnet(); /*called from softclock()*/
661 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
662 printf("*** objects\n");
663 printf("spi %u proto %u dir %u\n", spi
, proto
, dir
);
664 kdebug_sockaddr(&dst
->sa
));
666 LIST_FOREACH(sp
, &sptree
[dir
], chain
) {
667 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
668 printf("*** in SPD\n");
669 kdebug_secpolicyindex(&sp
->spidx
));
671 if (sp
->state
== IPSEC_SPSTATE_DEAD
)
673 /* compare simple values, then dst address */
674 if (sp
->spidx
.ul_proto
!= proto
)
676 /* NB: spi's must exist and match */
677 if (!sp
->req
|| !sp
->req
->sav
|| sp
->req
->sav
->spi
!= spi
)
679 if (key_sockaddrcmp(&sp
->spidx
.dst
.sa
, &dst
->sa
, 1) == 0)
686 KEY_CHKSPDIR(sp
->spidx
.dir
, dir
, "key_allocsp2");
688 /* found a SPD entry */
689 sp
->lastused
= time_second
;
694 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
695 printf("DP key_allocsp2 return SP:%p (ID=%u) refcnt %u\n",
696 sp
, sp
? sp
->id
: 0, sp
? sp
->refcnt
: 0));
701 * return a policy that matches this particular inbound packet.
705 key_gettunnel(const struct sockaddr
*osrc
,
706 const struct sockaddr
*odst
,
707 const struct sockaddr
*isrc
,
708 const struct sockaddr
*idst
,
709 const char* where
, int tag
)
711 struct secpolicy
*sp
;
712 const int dir
= IPSEC_DIR_INBOUND
;
714 struct ipsecrequest
*r1
, *r2
, *p
;
715 struct secpolicyindex spidx
;
717 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
718 printf("DP key_gettunnel from %s:%u\n", where
, tag
));
720 if (isrc
->sa_family
!= idst
->sa_family
) {
721 ipseclog((LOG_ERR
, "protocol family mismatched %d != %d\n.",
722 isrc
->sa_family
, idst
->sa_family
));
727 s
= splsoftnet(); /*called from softclock()*/
728 LIST_FOREACH(sp
, &sptree
[dir
], chain
) {
729 if (sp
->state
== IPSEC_SPSTATE_DEAD
)
733 for (p
= sp
->req
; p
; p
= p
->next
) {
734 if (p
->saidx
.mode
!= IPSEC_MODE_TUNNEL
)
741 /* here we look at address matches only */
743 if (isrc
->sa_len
> sizeof(spidx
.src
) ||
744 idst
->sa_len
> sizeof(spidx
.dst
))
746 memcpy(&spidx
.src
, isrc
, isrc
->sa_len
);
747 memcpy(&spidx
.dst
, idst
, idst
->sa_len
);
748 if (!key_cmpspidx_withmask(&sp
->spidx
, &spidx
))
751 if (key_sockaddrcmp(&r1
->saidx
.src
.sa
, isrc
, 0) ||
752 key_sockaddrcmp(&r1
->saidx
.dst
.sa
, idst
, 0))
756 if (key_sockaddrcmp(&r2
->saidx
.src
.sa
, osrc
, 0) ||
757 key_sockaddrcmp(&r2
->saidx
.dst
.sa
, odst
, 0))
766 sp
->lastused
= time_second
;
771 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
772 printf("DP key_gettunnel return SP:%p (ID=%u) refcnt %u\n",
773 sp
, sp
? sp
->id
: 0, sp
? sp
->refcnt
: 0));
778 * allocating an SA entry for an *OUTBOUND* packet.
779 * checking each request entries in SP, and acquire an SA if need.
780 * OUT: 0: there are valid requests.
781 * ENOENT: policy may be valid, but SA with REQUIRE is on acquiring.
784 key_checkrequest(struct ipsecrequest
*isr
, const struct secasindex
*saidx
)
789 IPSEC_ASSERT(isr
!= NULL
, ("key_checkrequest: null isr"));
790 IPSEC_ASSERT(saidx
!= NULL
, ("key_checkrequest: null saidx"));
791 IPSEC_ASSERT(saidx
->mode
== IPSEC_MODE_TRANSPORT
||
792 saidx
->mode
== IPSEC_MODE_TUNNEL
,
793 ("key_checkrequest: unexpected policy %u", saidx
->mode
));
795 /* get current level */
796 level
= ipsec_get_reqlevel(isr
);
799 * XXX guard against protocol callbacks from the crypto
800 * thread as they reference ipsecrequest.sav which we
801 * temporarily null out below. Need to rethink how we
802 * handle bundled SA's in the callback thread.
804 IPSEC_SPLASSERT_SOFTNET("key_checkrequest");
807 * We do allocate new SA only if the state of SA in the holder is
808 * SADB_SASTATE_DEAD. The SA for outbound must be the oldest.
810 if (isr
->sav
!= NULL
) {
811 if (isr
->sav
->sah
== NULL
)
812 panic("key_checkrequest: sah is null");
813 if (isr
->sav
== (struct secasvar
*)LIST_FIRST(
814 &isr
->sav
->sah
->savtree
[SADB_SASTATE_DEAD
])) {
815 KEY_FREESAV(&isr
->sav
);
821 * we free any SA stashed in the IPsec request because a different
822 * SA may be involved each time this request is checked, either
823 * because new SAs are being configured, or this request is
824 * associated with an unconnected datagram socket, or this request
825 * is associated with a system default policy.
827 * The operation may have negative impact to performance. We may
828 * want to check cached SA carefully, rather than picking new SA
831 if (isr
->sav
!= NULL
) {
832 KEY_FREESAV(&isr
->sav
);
838 * new SA allocation if no SA found.
839 * key_allocsa_policy should allocate the oldest SA available.
840 * See key_do_allocsa_policy(), and draft-jenkins-ipsec-rekeying-03.txt.
842 if (isr
->sav
== NULL
)
843 isr
->sav
= key_allocsa_policy(saidx
);
845 /* When there is SA. */
846 if (isr
->sav
!= NULL
) {
847 if (isr
->sav
->state
!= SADB_SASTATE_MATURE
&&
848 isr
->sav
->state
!= SADB_SASTATE_DYING
)
854 error
= key_acquire(saidx
, isr
->sp
);
856 /* XXX What should I do ? */
857 ipseclog((LOG_DEBUG
, "key_checkrequest: error %d returned "
858 "from key_acquire.\n", error
));
862 if (level
!= IPSEC_LEVEL_REQUIRE
) {
863 /* XXX sigh, the interface to this routine is botched */
864 IPSEC_ASSERT(isr
->sav
== NULL
, ("key_checkrequest: unexpected SA"));
872 * allocating a SA for policy entry from SAD.
873 * NOTE: searching SAD of aliving state.
874 * OUT: NULL: not found.
875 * others: found and return the pointer.
877 static struct secasvar
*
878 key_allocsa_policy(const struct secasindex
*saidx
)
880 struct secashead
*sah
;
881 struct secasvar
*sav
;
882 u_int stateidx
, state
;
884 LIST_FOREACH(sah
, &sahtree
, chain
) {
885 if (sah
->state
== SADB_SASTATE_DEAD
)
887 if (key_cmpsaidx(&sah
->saidx
, saidx
, CMP_MODE_REQID
))
895 /* search valid state */
897 stateidx
< _ARRAYLEN(saorder_state_valid
);
900 state
= saorder_state_valid
[stateidx
];
902 sav
= key_do_allocsa_policy(sah
, state
);
911 * searching SAD with direction, protocol, mode and state.
912 * called by key_allocsa_policy().
915 * others : found, pointer to a SA.
917 static struct secasvar
*
918 key_do_allocsa_policy(struct secashead
*sah
, u_int state
)
920 struct secasvar
*sav
, *nextsav
, *candidate
, *d
;
925 for (sav
= LIST_FIRST(&sah
->savtree
[state
]);
929 nextsav
= LIST_NEXT(sav
, chain
);
932 KEY_CHKSASTATE(sav
->state
, state
, "key_do_allocsa_policy");
935 if (candidate
== NULL
) {
940 /* Which SA is the better ? */
943 if (candidate
->lft_c
== NULL
|| sav
->lft_c
== NULL
)
944 panic("key_do_allocsa_policy: "
945 "lifetime_current is NULL");
947 /* What the best method is to compare ? */
948 if (key_prefered_oldsa
) {
949 if (candidate
->lft_c
->sadb_lifetime_addtime
>
950 sav
->lft_c
->sadb_lifetime_addtime
) {
957 /* prefered new sa rather than old sa */
958 if (candidate
->lft_c
->sadb_lifetime_addtime
<
959 sav
->lft_c
->sadb_lifetime_addtime
) {
966 * prepared to delete the SA when there is more
967 * suitable candidate and the lifetime of the SA is not
970 if (d
->lft_c
->sadb_lifetime_addtime
!= 0) {
971 struct mbuf
*m
, *result
;
974 key_sa_chgstate(d
, SADB_SASTATE_DEAD
);
976 IPSEC_ASSERT(d
->refcnt
> 0,
977 ("key_do_allocsa_policy: bogus ref count"));
979 satype
= key_proto2satype(d
->sah
->saidx
.proto
);
983 m
= key_setsadbmsg(SADB_DELETE
, 0,
984 satype
, 0, 0, d
->refcnt
- 1);
989 /* set sadb_address for saidx's. */
990 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
991 &d
->sah
->saidx
.src
.sa
,
992 d
->sah
->saidx
.src
.sa
.sa_len
<< 3,
998 /* set sadb_address for saidx's. */
999 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
1000 &d
->sah
->saidx
.src
.sa
,
1001 d
->sah
->saidx
.src
.sa
.sa_len
<< 3,
1007 /* create SA extension */
1008 m
= key_setsadbsa(d
);
1013 if (result
->m_len
< sizeof(struct sadb_msg
)) {
1014 result
= m_pullup(result
,
1015 sizeof(struct sadb_msg
));
1020 result
->m_pkthdr
.len
= 0;
1021 for (m
= result
; m
; m
= m
->m_next
)
1022 result
->m_pkthdr
.len
+= m
->m_len
;
1023 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
1024 PFKEY_UNIT64(result
->m_pkthdr
.len
);
1026 if (key_sendup_mbuf(NULL
, result
,
1027 KEY_SENDUP_REGISTERED
))
1035 SA_ADDREF(candidate
);
1036 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1037 printf("DP allocsa_policy cause "
1038 "refcnt++:%d SA:%p\n",
1039 candidate
->refcnt
, candidate
));
1045 * allocating a usable SA entry for a *INBOUND* packet.
1046 * Must call key_freesav() later.
1047 * OUT: positive: pointer to a usable sav (i.e. MATURE or DYING state).
1048 * NULL: not found, or error occurred.
1050 * In the comparison, no source address is used--for RFC2401 conformance.
1051 * To quote, from section 4.1:
1052 * A security association is uniquely identified by a triple consisting
1053 * of a Security Parameter Index (SPI), an IP Destination Address, and a
1054 * security protocol (AH or ESP) identifier.
1055 * Note that, however, we do need to keep source address in IPsec SA.
1056 * IKE specification and PF_KEY specification do assume that we
1057 * keep source address in IPsec SA. We see a tricky situation here.
1059 * sport and dport are used for NAT-T. network order is always used.
1063 const union sockaddr_union
*dst
,
1068 const char* where
, int tag
)
1070 struct secashead
*sah
;
1071 struct secasvar
*sav
;
1072 u_int stateidx
, state
;
1076 int must_check_spi
= 1;
1077 int must_check_alg
= 0;
1082 if ((sport
!= 0) && (dport
!= 0))
1086 IPSEC_ASSERT(dst
!= NULL
, ("key_allocsa: null dst address"));
1088 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1089 printf("DP key_allocsa from %s:%u\n", where
, tag
));
1093 * We use cpi to define spi here. In the case where cpi <=
1094 * IPCOMP_CPI_NEGOTIATE_MIN, cpi just define the algorithm used, not
1095 * the real spi. In this case, don't check the spi but check the
1099 if (proto
== IPPROTO_IPCOMP
) {
1102 cpi
= (u_int16_t
) tmp
;
1103 if (cpi
< IPCOMP_CPI_NEGOTIATE_MIN
) {
1104 algo
= (u_int8_t
) cpi
;
1112 * XXX: to be checked internal IP header somewhere. Also when
1113 * IPsec tunnel packet is received. But ESP tunnel mode is
1114 * encrypted so we can't check internal IP header.
1116 s
= splsoftnet(); /*called from softclock()*/
1117 LIST_FOREACH(sah
, &sahtree
, chain
) {
1118 /* search valid state */
1120 stateidx
< _ARRAYLEN(saorder_state_valid
);
1122 state
= saorder_state_valid
[stateidx
];
1123 LIST_FOREACH(sav
, &sah
->savtree
[state
], chain
) {
1125 KEY_CHKSASTATE(sav
->state
, state
, "key_allocsav");
1126 /* do not return entries w/ unusable state */
1127 if (sav
->state
!= SADB_SASTATE_MATURE
&&
1128 sav
->state
!= SADB_SASTATE_DYING
)
1130 if (proto
!= sav
->sah
->saidx
.proto
)
1132 if (must_check_spi
&& spi
!= sav
->spi
)
1134 /* XXX only on the ipcomp case */
1135 if (must_check_alg
&& algo
!= sav
->alg_comp
)
1138 #if 0 /* don't check src */
1139 /* Fix port in src->sa */
1141 /* check src address */
1142 if (key_sockaddrcmp(&src
->sa
, &sav
->sah
->saidx
.src
.sa
, 0) != 0)
1145 /* fix port of dst address XXX*/
1146 key_porttosaddr(__UNCONST(dst
), dport
);
1147 /* check dst address */
1148 if (key_sockaddrcmp(&dst
->sa
, &sav
->sah
->saidx
.dst
.sa
, chkport
) != 0)
1159 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1160 printf("DP key_allocsa return SA:%p; refcnt %u\n",
1161 sav
, sav
? sav
->refcnt
: 0));
1166 * Must be called after calling key_allocsp().
1167 * For both the packet without socket and key_freeso().
1170 _key_freesp(struct secpolicy
**spp
, const char* where
, int tag
)
1172 struct secpolicy
*sp
= *spp
;
1174 IPSEC_ASSERT(sp
!= NULL
, ("key_freesp: null sp"));
1178 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1179 printf("DP key_freesp SP:%p (ID=%u) from %s:%u; refcnt now %u\n",
1180 sp
, sp
->id
, where
, tag
, sp
->refcnt
));
1182 if (sp
->refcnt
== 0) {
1189 * Must be called after calling key_allocsp().
1190 * For the packet with socket.
1193 key_freeso(struct socket
*so
)
1196 IPSEC_ASSERT(so
!= NULL
, ("key_freeso: null so"));
1198 switch (so
->so_proto
->pr_domain
->dom_family
) {
1202 struct inpcb
*pcb
= sotoinpcb(so
);
1204 /* Does it have a PCB ? */
1207 key_freesp_so(&pcb
->inp_sp
->sp_in
);
1208 key_freesp_so(&pcb
->inp_sp
->sp_out
);
1215 #ifdef HAVE_NRL_INPCB
1216 struct inpcb
*pcb
= sotoinpcb(so
);
1218 /* Does it have a PCB ? */
1221 key_freesp_so(&pcb
->inp_sp
->sp_in
);
1222 key_freesp_so(&pcb
->inp_sp
->sp_out
);
1224 struct in6pcb
*pcb
= sotoin6pcb(so
);
1226 /* Does it have a PCB ? */
1229 key_freesp_so(&pcb
->in6p_sp
->sp_in
);
1230 key_freesp_so(&pcb
->in6p_sp
->sp_out
);
1236 ipseclog((LOG_DEBUG
, "key_freeso: unknown address family=%d.\n",
1237 so
->so_proto
->pr_domain
->dom_family
));
1243 key_freesp_so(struct secpolicy
**sp
)
1245 IPSEC_ASSERT(sp
!= NULL
&& *sp
!= NULL
, ("key_freesp_so: null sp"));
1247 if ((*sp
)->policy
== IPSEC_POLICY_ENTRUST
||
1248 (*sp
)->policy
== IPSEC_POLICY_BYPASS
)
1251 IPSEC_ASSERT((*sp
)->policy
== IPSEC_POLICY_IPSEC
,
1252 ("key_freesp_so: invalid policy %u", (*sp
)->policy
));
1257 * Must be called after calling key_allocsa().
1258 * This function is called by key_freesp() to free some SA allocated
1262 key_freesav(struct secasvar
**psav
, const char* where
, int tag
)
1264 struct secasvar
*sav
= *psav
;
1266 IPSEC_ASSERT(sav
!= NULL
, ("key_freesav: null sav"));
1270 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1271 printf("DP key_freesav SA:%p (SPI %lu) from %s:%u; refcnt now %u\n",
1272 sav
, (u_long
)ntohl(sav
->spi
),
1273 where
, tag
, sav
->refcnt
));
1275 if (sav
->refcnt
== 0) {
1281 /* %%% SPD management */
1283 * free security policy entry.
1286 key_delsp(struct secpolicy
*sp
)
1290 IPSEC_ASSERT(sp
!= NULL
, ("key_delsp: null sp"));
1294 IPSEC_ASSERT(sp
->refcnt
== 0,
1295 ("key_delsp: SP with references deleted (refcnt %u)",
1298 s
= splsoftnet(); /*called from softclock()*/
1301 struct ipsecrequest
*isr
= sp
->req
, *nextisr
;
1303 while (isr
!= NULL
) {
1304 if (isr
->sav
!= NULL
) {
1305 KEY_FREESAV(&isr
->sav
);
1309 nextisr
= isr
->next
;
1322 * OUT: NULL : not found
1323 * others : found, pointer to a SP.
1325 static struct secpolicy
*
1326 key_getsp(struct secpolicyindex
*spidx
)
1328 struct secpolicy
*sp
;
1330 IPSEC_ASSERT(spidx
!= NULL
, ("key_getsp: null spidx"));
1332 LIST_FOREACH(sp
, &sptree
[spidx
->dir
], chain
) {
1333 if (sp
->state
== IPSEC_SPSTATE_DEAD
)
1335 if (key_cmpspidx_exactly(spidx
, &sp
->spidx
)) {
1346 * OUT: NULL : not found
1347 * others : found, pointer to a SP.
1349 static struct secpolicy
*
1350 key_getspbyid(u_int32_t id
)
1352 struct secpolicy
*sp
;
1354 LIST_FOREACH(sp
, &sptree
[IPSEC_DIR_INBOUND
], chain
) {
1355 if (sp
->state
== IPSEC_SPSTATE_DEAD
)
1363 LIST_FOREACH(sp
, &sptree
[IPSEC_DIR_OUTBOUND
], chain
) {
1364 if (sp
->state
== IPSEC_SPSTATE_DEAD
)
1376 key_newsp(const char* where
, int tag
)
1378 struct secpolicy
*newsp
= NULL
;
1380 newsp
= (struct secpolicy
*)
1381 malloc(sizeof(struct secpolicy
), M_SECA
, M_NOWAIT
|M_ZERO
);
1387 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1388 printf("DP key_newsp from %s:%u return SP:%p\n",
1389 where
, tag
, newsp
));
1394 * create secpolicy structure from sadb_x_policy structure.
1395 * NOTE: `state', `secpolicyindex' in secpolicy structure are not set,
1396 * so must be set properly later.
1399 key_msg2sp(struct sadb_x_policy
*xpl0
, size_t len
, int *error
)
1401 struct secpolicy
*newsp
;
1405 panic("key_msg2sp: NULL pointer was passed");
1406 if (len
< sizeof(*xpl0
))
1407 panic("key_msg2sp: invalid length");
1408 if (len
!= PFKEY_EXTLEN(xpl0
)) {
1409 ipseclog((LOG_DEBUG
, "key_msg2sp: Invalid msg length.\n"));
1414 if ((newsp
= KEY_NEWSP()) == NULL
) {
1419 newsp
->spidx
.dir
= xpl0
->sadb_x_policy_dir
;
1420 newsp
->policy
= xpl0
->sadb_x_policy_type
;
1423 switch (xpl0
->sadb_x_policy_type
) {
1424 case IPSEC_POLICY_DISCARD
:
1425 case IPSEC_POLICY_NONE
:
1426 case IPSEC_POLICY_ENTRUST
:
1427 case IPSEC_POLICY_BYPASS
:
1431 case IPSEC_POLICY_IPSEC
:
1434 struct sadb_x_ipsecrequest
*xisr
;
1435 struct ipsecrequest
**p_isr
= &newsp
->req
;
1437 /* validity check */
1438 if (PFKEY_EXTLEN(xpl0
) < sizeof(*xpl0
)) {
1439 ipseclog((LOG_DEBUG
,
1440 "key_msg2sp: Invalid msg length.\n"));
1446 tlen
= PFKEY_EXTLEN(xpl0
) - sizeof(*xpl0
);
1447 xisr
= (struct sadb_x_ipsecrequest
*)(xpl0
+ 1);
1451 if (xisr
->sadb_x_ipsecrequest_len
< sizeof(*xisr
)) {
1452 ipseclog((LOG_DEBUG
, "key_msg2sp: "
1453 "invalid ipsecrequest length.\n"));
1459 /* allocate request buffer */
1460 KMALLOC(*p_isr
, struct ipsecrequest
*, sizeof(**p_isr
));
1461 if ((*p_isr
) == NULL
) {
1462 ipseclog((LOG_DEBUG
,
1463 "key_msg2sp: No more memory.\n"));
1468 memset(*p_isr
, 0, sizeof(**p_isr
));
1471 (*p_isr
)->next
= NULL
;
1473 switch (xisr
->sadb_x_ipsecrequest_proto
) {
1476 case IPPROTO_IPCOMP
:
1479 ipseclog((LOG_DEBUG
,
1480 "key_msg2sp: invalid proto type=%u\n",
1481 xisr
->sadb_x_ipsecrequest_proto
));
1483 *error
= EPROTONOSUPPORT
;
1486 (*p_isr
)->saidx
.proto
= xisr
->sadb_x_ipsecrequest_proto
;
1488 switch (xisr
->sadb_x_ipsecrequest_mode
) {
1489 case IPSEC_MODE_TRANSPORT
:
1490 case IPSEC_MODE_TUNNEL
:
1492 case IPSEC_MODE_ANY
:
1494 ipseclog((LOG_DEBUG
,
1495 "key_msg2sp: invalid mode=%u\n",
1496 xisr
->sadb_x_ipsecrequest_mode
));
1501 (*p_isr
)->saidx
.mode
= xisr
->sadb_x_ipsecrequest_mode
;
1503 switch (xisr
->sadb_x_ipsecrequest_level
) {
1504 case IPSEC_LEVEL_DEFAULT
:
1505 case IPSEC_LEVEL_USE
:
1506 case IPSEC_LEVEL_REQUIRE
:
1508 case IPSEC_LEVEL_UNIQUE
:
1509 /* validity check */
1511 * If range violation of reqid, kernel will
1512 * update it, don't refuse it.
1514 if (xisr
->sadb_x_ipsecrequest_reqid
1515 > IPSEC_MANUAL_REQID_MAX
) {
1516 ipseclog((LOG_DEBUG
,
1517 "key_msg2sp: reqid=%d range "
1518 "violation, updated by kernel.\n",
1519 xisr
->sadb_x_ipsecrequest_reqid
));
1520 xisr
->sadb_x_ipsecrequest_reqid
= 0;
1523 /* allocate new reqid id if reqid is zero. */
1524 if (xisr
->sadb_x_ipsecrequest_reqid
== 0) {
1526 if ((reqid
= key_newreqid()) == 0) {
1531 (*p_isr
)->saidx
.reqid
= reqid
;
1532 xisr
->sadb_x_ipsecrequest_reqid
= reqid
;
1534 /* set it for manual keying. */
1535 (*p_isr
)->saidx
.reqid
=
1536 xisr
->sadb_x_ipsecrequest_reqid
;
1541 ipseclog((LOG_DEBUG
, "key_msg2sp: invalid level=%u\n",
1542 xisr
->sadb_x_ipsecrequest_level
));
1547 (*p_isr
)->level
= xisr
->sadb_x_ipsecrequest_level
;
1549 /* set IP addresses if there */
1550 if (xisr
->sadb_x_ipsecrequest_len
> sizeof(*xisr
)) {
1551 struct sockaddr
*paddr
;
1553 paddr
= (struct sockaddr
*)(xisr
+ 1);
1555 /* validity check */
1557 > sizeof((*p_isr
)->saidx
.src
)) {
1558 ipseclog((LOG_DEBUG
, "key_msg2sp: invalid request "
1559 "address length.\n"));
1564 memcpy(&(*p_isr
)->saidx
.src
, paddr
, paddr
->sa_len
);
1566 paddr
= (struct sockaddr
*)((char *)paddr
1569 /* validity check */
1571 > sizeof((*p_isr
)->saidx
.dst
)) {
1572 ipseclog((LOG_DEBUG
, "key_msg2sp: invalid request "
1573 "address length.\n"));
1578 memcpy(&(*p_isr
)->saidx
.dst
, paddr
, paddr
->sa_len
);
1581 (*p_isr
)->sav
= NULL
;
1582 (*p_isr
)->sp
= newsp
;
1584 /* initialization for the next. */
1585 p_isr
= &(*p_isr
)->next
;
1586 tlen
-= xisr
->sadb_x_ipsecrequest_len
;
1588 /* validity check */
1590 ipseclog((LOG_DEBUG
, "key_msg2sp: becoming tlen < 0.\n"));
1596 xisr
= (struct sadb_x_ipsecrequest
*)((char *)xisr
1597 + xisr
->sadb_x_ipsecrequest_len
);
1602 ipseclog((LOG_DEBUG
, "key_msg2sp: invalid policy type.\n"));
1615 static u_int16_t auto_reqid
= IPSEC_MANUAL_REQID_MAX
+ 1;
1617 auto_reqid
= (auto_reqid
== 0xffff
1618 ? IPSEC_MANUAL_REQID_MAX
+ 1 : auto_reqid
+ 1);
1620 /* XXX should be unique check */
1626 * copy secpolicy struct to sadb_x_policy structure indicated.
1629 key_sp2msg(struct secpolicy
*sp
)
1631 struct sadb_x_policy
*xpl
;
1638 panic("key_sp2msg: NULL pointer was passed");
1640 tlen
= key_getspreqmsglen(sp
);
1642 m
= key_alloc_mbuf(tlen
);
1643 if (!m
|| m
->m_next
) { /*XXX*/
1651 xpl
= mtod(m
, struct sadb_x_policy
*);
1652 memset(xpl
, 0, tlen
);
1654 xpl
->sadb_x_policy_len
= PFKEY_UNIT64(tlen
);
1655 xpl
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
1656 xpl
->sadb_x_policy_type
= sp
->policy
;
1657 xpl
->sadb_x_policy_dir
= sp
->spidx
.dir
;
1658 xpl
->sadb_x_policy_id
= sp
->id
;
1659 p
= (char *)xpl
+ sizeof(*xpl
);
1661 /* if is the policy for ipsec ? */
1662 if (sp
->policy
== IPSEC_POLICY_IPSEC
) {
1663 struct sadb_x_ipsecrequest
*xisr
;
1664 struct ipsecrequest
*isr
;
1666 for (isr
= sp
->req
; isr
!= NULL
; isr
= isr
->next
) {
1668 xisr
= (struct sadb_x_ipsecrequest
*)p
;
1670 xisr
->sadb_x_ipsecrequest_proto
= isr
->saidx
.proto
;
1671 xisr
->sadb_x_ipsecrequest_mode
= isr
->saidx
.mode
;
1672 xisr
->sadb_x_ipsecrequest_level
= isr
->level
;
1673 xisr
->sadb_x_ipsecrequest_reqid
= isr
->saidx
.reqid
;
1676 memcpy(p
, &isr
->saidx
.src
, isr
->saidx
.src
.sa
.sa_len
);
1677 p
+= isr
->saidx
.src
.sa
.sa_len
;
1678 memcpy(p
, &isr
->saidx
.dst
, isr
->saidx
.dst
.sa
.sa_len
);
1679 p
+= isr
->saidx
.src
.sa
.sa_len
;
1681 xisr
->sadb_x_ipsecrequest_len
=
1682 PFKEY_ALIGN8(sizeof(*xisr
)
1683 + isr
->saidx
.src
.sa
.sa_len
1684 + isr
->saidx
.dst
.sa
.sa_len
);
1691 /* m will not be freed nor modified */
1692 static struct mbuf
*
1693 key_gather_mbuf(struct mbuf
*m
, const struct sadb_msghdr
*mhp
,
1694 int ndeep
, int nitem
, ...)
1699 struct mbuf
*result
= NULL
, *n
;
1702 if (m
== NULL
|| mhp
== NULL
)
1703 panic("null pointer passed to key_gather");
1705 va_start(ap
, nitem
);
1706 for (i
= 0; i
< nitem
; i
++) {
1707 idx
= va_arg(ap
, int);
1708 if (idx
< 0 || idx
> SADB_EXT_MAX
)
1710 /* don't attempt to pull empty extension */
1711 if (idx
== SADB_EXT_RESERVED
&& mhp
->msg
== NULL
)
1713 if (idx
!= SADB_EXT_RESERVED
&&
1714 (mhp
->ext
[idx
] == NULL
|| mhp
->extlen
[idx
] == 0))
1717 if (idx
== SADB_EXT_RESERVED
) {
1718 len
= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
1721 panic("assumption failed");
1723 MGETHDR(n
, M_DONTWAIT
, MT_DATA
);
1728 m_copydata(m
, 0, sizeof(struct sadb_msg
),
1730 } else if (i
< ndeep
) {
1731 len
= mhp
->extlen
[idx
];
1732 n
= key_alloc_mbuf(len
);
1733 if (!n
|| n
->m_next
) { /*XXX*/
1738 m_copydata(m
, mhp
->extoff
[idx
], mhp
->extlen
[idx
],
1741 n
= m_copym(m
, mhp
->extoff
[idx
], mhp
->extlen
[idx
],
1754 if ((result
->m_flags
& M_PKTHDR
) != 0) {
1755 result
->m_pkthdr
.len
= 0;
1756 for (n
= result
; n
; n
= n
->m_next
)
1757 result
->m_pkthdr
.len
+= n
->m_len
;
1769 * SADB_X_SPDADD, SADB_X_SPDSETIDX or SADB_X_SPDUPDATE processing
1770 * add an entry to SP database, when received
1771 * <base, address(SD), (lifetime(H),) policy>
1773 * Adding to SP database,
1775 * <base, address(SD), (lifetime(H),) policy>
1776 * to the socket which was send.
1778 * SPDADD set a unique policy entry.
1779 * SPDSETIDX like SPDADD without a part of policy requests.
1780 * SPDUPDATE replace a unique policy entry.
1782 * m will always be freed.
1785 key_spdadd(struct socket
*so
, struct mbuf
*m
,
1786 const struct sadb_msghdr
*mhp
)
1788 struct sadb_address
*src0
, *dst0
;
1789 struct sadb_x_policy
*xpl0
, *xpl
;
1790 struct sadb_lifetime
*lft
= NULL
;
1791 struct secpolicyindex spidx
;
1792 struct secpolicy
*newsp
;
1796 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
1797 panic("key_spdadd: NULL pointer is passed");
1799 if (mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
1800 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
||
1801 mhp
->ext
[SADB_X_EXT_POLICY
] == NULL
) {
1802 ipseclog((LOG_DEBUG
, "key_spdadd: invalid message is passed.\n"));
1803 return key_senderror(so
, m
, EINVAL
);
1805 if (mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
1806 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
) ||
1807 mhp
->extlen
[SADB_X_EXT_POLICY
] < sizeof(struct sadb_x_policy
)) {
1808 ipseclog((LOG_DEBUG
, "key_spdadd: invalid message is passed.\n"));
1809 return key_senderror(so
, m
, EINVAL
);
1811 if (mhp
->ext
[SADB_EXT_LIFETIME_HARD
] != NULL
) {
1812 if (mhp
->extlen
[SADB_EXT_LIFETIME_HARD
]
1813 < sizeof(struct sadb_lifetime
)) {
1814 ipseclog((LOG_DEBUG
, "key_spdadd: invalid message is passed.\n"));
1815 return key_senderror(so
, m
, EINVAL
);
1817 lft
= (struct sadb_lifetime
*)mhp
->ext
[SADB_EXT_LIFETIME_HARD
];
1820 src0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_SRC
];
1821 dst0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_DST
];
1822 xpl0
= (struct sadb_x_policy
*)mhp
->ext
[SADB_X_EXT_POLICY
];
1825 /* XXX boundary check against sa_len */
1826 KEY_SETSECSPIDX(xpl0
->sadb_x_policy_dir
,
1829 src0
->sadb_address_prefixlen
,
1830 dst0
->sadb_address_prefixlen
,
1831 src0
->sadb_address_proto
,
1834 /* checking the direciton. */
1835 switch (xpl0
->sadb_x_policy_dir
) {
1836 case IPSEC_DIR_INBOUND
:
1837 case IPSEC_DIR_OUTBOUND
:
1840 ipseclog((LOG_DEBUG
, "key_spdadd: Invalid SP direction.\n"));
1841 mhp
->msg
->sadb_msg_errno
= EINVAL
;
1846 /* key_spdadd() accepts DISCARD, NONE and IPSEC. */
1847 if (xpl0
->sadb_x_policy_type
== IPSEC_POLICY_ENTRUST
1848 || xpl0
->sadb_x_policy_type
== IPSEC_POLICY_BYPASS
) {
1849 ipseclog((LOG_DEBUG
, "key_spdadd: Invalid policy type.\n"));
1850 return key_senderror(so
, m
, EINVAL
);
1853 /* policy requests are mandatory when action is ipsec. */
1854 if (mhp
->msg
->sadb_msg_type
!= SADB_X_SPDSETIDX
1855 && xpl0
->sadb_x_policy_type
== IPSEC_POLICY_IPSEC
1856 && mhp
->extlen
[SADB_X_EXT_POLICY
] <= sizeof(*xpl0
)) {
1857 ipseclog((LOG_DEBUG
, "key_spdadd: some policy requests part required.\n"));
1858 return key_senderror(so
, m
, EINVAL
);
1862 * checking there is SP already or not.
1863 * SPDUPDATE doesn't depend on whether there is a SP or not.
1864 * If the type is either SPDADD or SPDSETIDX AND a SP is found,
1867 newsp
= key_getsp(&spidx
);
1868 if (mhp
->msg
->sadb_msg_type
== SADB_X_SPDUPDATE
) {
1871 key_sp_unlink(newsp
); /* XXX jrs ordering */
1876 if (newsp
!= NULL
) {
1878 ipseclog((LOG_DEBUG
, "key_spdadd: a SP entry exists already.\n"));
1879 return key_senderror(so
, m
, EEXIST
);
1883 /* allocation new SP entry */
1884 if ((newsp
= key_msg2sp(xpl0
, PFKEY_EXTLEN(xpl0
), &error
)) == NULL
) {
1885 return key_senderror(so
, m
, error
);
1888 if ((newsp
->id
= key_getnewspid()) == 0) {
1890 return key_senderror(so
, m
, ENOBUFS
);
1893 /* XXX boundary check against sa_len */
1894 KEY_SETSECSPIDX(xpl0
->sadb_x_policy_dir
,
1897 src0
->sadb_address_prefixlen
,
1898 dst0
->sadb_address_prefixlen
,
1899 src0
->sadb_address_proto
,
1902 /* sanity check on addr pair */
1903 if (((struct sockaddr
*)(src0
+ 1))->sa_family
!=
1904 ((struct sockaddr
*)(dst0
+ 1))->sa_family
) {
1906 return key_senderror(so
, m
, EINVAL
);
1908 if (((struct sockaddr
*)(src0
+ 1))->sa_len
!=
1909 ((struct sockaddr
*)(dst0
+ 1))->sa_len
) {
1911 return key_senderror(so
, m
, EINVAL
);
1914 if (newsp
->req
&& newsp
->req
->saidx
.src
.sa
.sa_family
) {
1915 struct sockaddr
*sa
;
1916 sa
= (struct sockaddr
*)(src0
+ 1);
1917 if (sa
->sa_family
!= newsp
->req
->saidx
.src
.sa
.sa_family
) {
1919 return key_senderror(so
, m
, EINVAL
);
1922 if (newsp
->req
&& newsp
->req
->saidx
.dst
.sa
.sa_family
) {
1923 struct sockaddr
*sa
;
1924 sa
= (struct sockaddr
*)(dst0
+ 1);
1925 if (sa
->sa_family
!= newsp
->req
->saidx
.dst
.sa
.sa_family
) {
1927 return key_senderror(so
, m
, EINVAL
);
1932 newsp
->created
= time_second
;
1933 newsp
->lastused
= newsp
->created
;
1934 newsp
->lifetime
= lft
? lft
->sadb_lifetime_addtime
: 0;
1935 newsp
->validtime
= lft
? lft
->sadb_lifetime_usetime
: 0;
1937 newsp
->refcnt
= 1; /* do not reclaim until I say I do */
1938 newsp
->state
= IPSEC_SPSTATE_ALIVE
;
1939 LIST_INSERT_TAIL(&sptree
[newsp
->spidx
.dir
], newsp
, secpolicy
, chain
);
1941 /* delete the entry in spacqtree */
1942 if (mhp
->msg
->sadb_msg_type
== SADB_X_SPDUPDATE
) {
1943 struct secspacq
*spacq
;
1944 if ((spacq
= key_getspacq(&spidx
)) != NULL
) {
1945 /* reset counter in order to deletion by timehandler. */
1946 spacq
->created
= time_second
;
1951 #if defined(__NetBSD__)
1952 /* Invalidate all cached SPD pointers in the PCBs. */
1953 ipsec_invalpcbcacheall();
1955 #if defined(GATEWAY)
1956 /* Invalidate the ipflow cache, as well. */
1957 ipflow_invalidate_all(0);
1959 ip6flow_invalidate_all(0);
1961 #endif /* GATEWAY */
1962 #endif /* __NetBSD__ */
1965 struct mbuf
*n
, *mpolicy
;
1966 struct sadb_msg
*newmsg
;
1969 /* create new sadb_msg to reply. */
1971 n
= key_gather_mbuf(m
, mhp
, 2, 5, SADB_EXT_RESERVED
,
1972 SADB_X_EXT_POLICY
, SADB_EXT_LIFETIME_HARD
,
1973 SADB_EXT_ADDRESS_SRC
, SADB_EXT_ADDRESS_DST
);
1975 n
= key_gather_mbuf(m
, mhp
, 2, 4, SADB_EXT_RESERVED
,
1977 SADB_EXT_ADDRESS_SRC
, SADB_EXT_ADDRESS_DST
);
1980 return key_senderror(so
, m
, ENOBUFS
);
1982 if (n
->m_len
< sizeof(*newmsg
)) {
1983 n
= m_pullup(n
, sizeof(*newmsg
));
1985 return key_senderror(so
, m
, ENOBUFS
);
1987 newmsg
= mtod(n
, struct sadb_msg
*);
1988 newmsg
->sadb_msg_errno
= 0;
1989 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
1992 mpolicy
= m_pulldown(n
, PFKEY_ALIGN8(sizeof(struct sadb_msg
)),
1993 sizeof(*xpl
), &off
);
1994 if (mpolicy
== NULL
) {
1995 /* n is already freed */
1996 return key_senderror(so
, m
, ENOBUFS
);
1998 xpl
= (struct sadb_x_policy
*)(mtod(mpolicy
, char *) + off
);
1999 if (xpl
->sadb_x_policy_exttype
!= SADB_X_EXT_POLICY
) {
2001 return key_senderror(so
, m
, EINVAL
);
2003 xpl
->sadb_x_policy_id
= newsp
->id
;
2006 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
2011 * get new policy id.
2017 key_getnewspid(void)
2019 u_int32_t newid
= 0;
2020 int count
= key_spi_trycnt
; /* XXX */
2021 struct secpolicy
*sp
;
2023 /* when requesting to allocate spi ranged */
2025 newid
= (policy_id
= (policy_id
== ~0 ? 1 : policy_id
+ 1));
2027 if ((sp
= key_getspbyid(newid
)) == NULL
)
2033 if (count
== 0 || newid
== 0) {
2034 ipseclog((LOG_DEBUG
, "key_getnewspid: to allocate policy id is failed.\n"));
2042 * SADB_SPDDELETE processing
2044 * <base, address(SD), policy(*)>
2045 * from the user(?), and set SADB_SASTATE_DEAD,
2047 * <base, address(SD), policy(*)>
2049 * policy(*) including direction of policy.
2051 * m will always be freed.
2054 key_spddelete(struct socket
*so
, struct mbuf
*m
,
2055 const struct sadb_msghdr
*mhp
)
2057 struct sadb_address
*src0
, *dst0
;
2058 struct sadb_x_policy
*xpl0
;
2059 struct secpolicyindex spidx
;
2060 struct secpolicy
*sp
;
2063 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
2064 panic("key_spddelete: NULL pointer is passed");
2066 if (mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
2067 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
||
2068 mhp
->ext
[SADB_X_EXT_POLICY
] == NULL
) {
2069 ipseclog((LOG_DEBUG
, "key_spddelete: invalid message is passed.\n"));
2070 return key_senderror(so
, m
, EINVAL
);
2072 if (mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
2073 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
) ||
2074 mhp
->extlen
[SADB_X_EXT_POLICY
] < sizeof(struct sadb_x_policy
)) {
2075 ipseclog((LOG_DEBUG
, "key_spddelete: invalid message is passed.\n"));
2076 return key_senderror(so
, m
, EINVAL
);
2079 src0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_SRC
];
2080 dst0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_DST
];
2081 xpl0
= (struct sadb_x_policy
*)mhp
->ext
[SADB_X_EXT_POLICY
];
2084 /* XXX boundary check against sa_len */
2085 KEY_SETSECSPIDX(xpl0
->sadb_x_policy_dir
,
2088 src0
->sadb_address_prefixlen
,
2089 dst0
->sadb_address_prefixlen
,
2090 src0
->sadb_address_proto
,
2093 /* checking the direciton. */
2094 switch (xpl0
->sadb_x_policy_dir
) {
2095 case IPSEC_DIR_INBOUND
:
2096 case IPSEC_DIR_OUTBOUND
:
2099 ipseclog((LOG_DEBUG
, "key_spddelete: Invalid SP direction.\n"));
2100 return key_senderror(so
, m
, EINVAL
);
2103 /* Is there SP in SPD ? */
2104 if ((sp
= key_getsp(&spidx
)) == NULL
) {
2105 ipseclog((LOG_DEBUG
, "key_spddelete: no SP found.\n"));
2106 return key_senderror(so
, m
, EINVAL
);
2109 /* save policy id to buffer to be returned. */
2110 xpl0
->sadb_x_policy_id
= sp
->id
;
2113 key_sp_unlink(sp
); /* XXX jrs ordering */
2114 KEY_FREESP(&sp
); /* ref gained by key_getspbyid */
2116 #if defined(__NetBSD__)
2117 /* Invalidate all cached SPD pointers in the PCBs. */
2118 ipsec_invalpcbcacheall();
2120 /* We're deleting policy; no need to invalidate the ipflow cache. */
2121 #endif /* __NetBSD__ */
2125 struct sadb_msg
*newmsg
;
2127 /* create new sadb_msg to reply. */
2128 n
= key_gather_mbuf(m
, mhp
, 1, 4, SADB_EXT_RESERVED
,
2129 SADB_X_EXT_POLICY
, SADB_EXT_ADDRESS_SRC
, SADB_EXT_ADDRESS_DST
);
2131 return key_senderror(so
, m
, ENOBUFS
);
2133 newmsg
= mtod(n
, struct sadb_msg
*);
2134 newmsg
->sadb_msg_errno
= 0;
2135 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
2138 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
2143 * SADB_SPDDELETE2 processing
2146 * from the user(?), and set SADB_SASTATE_DEAD,
2150 * policy(*) including direction of policy.
2152 * m will always be freed.
2155 key_spddelete2(struct socket
*so
, struct mbuf
*m
,
2156 const struct sadb_msghdr
*mhp
)
2159 struct secpolicy
*sp
;
2162 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
2163 panic("key_spddelete2: NULL pointer is passed");
2165 if (mhp
->ext
[SADB_X_EXT_POLICY
] == NULL
||
2166 mhp
->extlen
[SADB_X_EXT_POLICY
] < sizeof(struct sadb_x_policy
)) {
2167 ipseclog((LOG_DEBUG
, "key_spddelete2: invalid message is passed.\n"));
2168 key_senderror(so
, m
, EINVAL
);
2172 id
= ((struct sadb_x_policy
*)mhp
->ext
[SADB_X_EXT_POLICY
])->sadb_x_policy_id
;
2174 /* Is there SP in SPD ? */
2175 if ((sp
= key_getspbyid(id
)) == NULL
) {
2176 ipseclog((LOG_DEBUG
, "key_spddelete2: no SP found id:%u.\n", id
));
2177 return key_senderror(so
, m
, EINVAL
);
2181 key_sp_unlink(sp
); /* XXX jrs ordering */
2182 KEY_FREESP(&sp
); /* ref gained by key_getsp */
2185 #if defined(__NetBSD__)
2186 /* Invalidate all cached SPD pointers in the PCBs. */
2187 ipsec_invalpcbcacheall();
2189 /* We're deleting policy; no need to invalidate the ipflow cache. */
2190 #endif /* __NetBSD__ */
2193 struct mbuf
*n
, *nn
;
2194 struct sadb_msg
*newmsg
;
2197 /* create new sadb_msg to reply. */
2198 len
= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
2201 return key_senderror(so
, m
, ENOBUFS
);
2202 MGETHDR(n
, M_DONTWAIT
, MT_DATA
);
2203 if (n
&& len
> MHLEN
) {
2204 MCLGET(n
, M_DONTWAIT
);
2205 if ((n
->m_flags
& M_EXT
) == 0) {
2211 return key_senderror(so
, m
, ENOBUFS
);
2217 m_copydata(m
, 0, sizeof(struct sadb_msg
), mtod(n
, char *) + off
);
2218 off
+= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
2222 panic("length inconsistency in key_spddelete2");
2225 n
->m_next
= m_copym(m
, mhp
->extoff
[SADB_X_EXT_POLICY
],
2226 mhp
->extlen
[SADB_X_EXT_POLICY
], M_DONTWAIT
);
2229 return key_senderror(so
, m
, ENOBUFS
);
2232 n
->m_pkthdr
.len
= 0;
2233 for (nn
= n
; nn
; nn
= nn
->m_next
)
2234 n
->m_pkthdr
.len
+= nn
->m_len
;
2236 newmsg
= mtod(n
, struct sadb_msg
*);
2237 newmsg
->sadb_msg_errno
= 0;
2238 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
2241 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
2246 * SADB_X_GET processing
2251 * <base, address(SD), policy>
2253 * policy(*) including direction of policy.
2255 * m will always be freed.
2258 key_spdget(struct socket
*so
, struct mbuf
*m
,
2259 const struct sadb_msghdr
*mhp
)
2262 struct secpolicy
*sp
;
2266 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
2267 panic("key_spdget: NULL pointer is passed");
2269 if (mhp
->ext
[SADB_X_EXT_POLICY
] == NULL
||
2270 mhp
->extlen
[SADB_X_EXT_POLICY
] < sizeof(struct sadb_x_policy
)) {
2271 ipseclog((LOG_DEBUG
, "key_spdget: invalid message is passed.\n"));
2272 return key_senderror(so
, m
, EINVAL
);
2275 id
= ((struct sadb_x_policy
*)mhp
->ext
[SADB_X_EXT_POLICY
])->sadb_x_policy_id
;
2277 /* Is there SP in SPD ? */
2278 if ((sp
= key_getspbyid(id
)) == NULL
) {
2279 ipseclog((LOG_DEBUG
, "key_spdget: no SP found id:%u.\n", id
));
2280 return key_senderror(so
, m
, ENOENT
);
2283 n
= key_setdumpsp(sp
, SADB_X_SPDGET
, mhp
->msg
->sadb_msg_seq
,
2284 mhp
->msg
->sadb_msg_pid
);
2285 KEY_FREESP(&sp
); /* ref gained by key_getspbyid */
2288 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ONE
);
2290 return key_senderror(so
, m
, ENOBUFS
);
2294 * SADB_X_SPDACQUIRE processing.
2295 * Acquire policy and SA(s) for a *OUTBOUND* packet.
2298 * to KMD, and expect to receive
2299 * <base> with SADB_X_SPDACQUIRE if error occurred,
2302 * with SADB_X_SPDUPDATE from KMD by PF_KEY.
2303 * policy(*) is without policy requests.
2306 * others: error number
2309 key_spdacquire(struct secpolicy
*sp
)
2311 struct mbuf
*result
= NULL
, *m
;
2312 struct secspacq
*newspacq
;
2317 panic("key_spdacquire: NULL pointer is passed");
2318 if (sp
->req
!= NULL
)
2319 panic("key_spdacquire: called but there is request");
2320 if (sp
->policy
!= IPSEC_POLICY_IPSEC
)
2321 panic("key_spdacquire: policy mismathed. IPsec is expected");
2323 /* Get an entry to check whether sent message or not. */
2324 if ((newspacq
= key_getspacq(&sp
->spidx
)) != NULL
) {
2325 if (key_blockacq_count
< newspacq
->count
) {
2326 /* reset counter and do send message. */
2327 newspacq
->count
= 0;
2329 /* increment counter and do nothing. */
2334 /* make new entry for blocking to send SADB_ACQUIRE. */
2335 if ((newspacq
= key_newspacq(&sp
->spidx
)) == NULL
)
2338 /* add to acqtree */
2339 LIST_INSERT_HEAD(&spacqtree
, newspacq
, chain
);
2342 /* create new sadb_msg to reply. */
2343 m
= key_setsadbmsg(SADB_X_SPDACQUIRE
, 0, 0, 0, 0, 0);
2350 result
->m_pkthdr
.len
= 0;
2351 for (m
= result
; m
; m
= m
->m_next
)
2352 result
->m_pkthdr
.len
+= m
->m_len
;
2354 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
2355 PFKEY_UNIT64(result
->m_pkthdr
.len
);
2357 return key_sendup_mbuf(NULL
, m
, KEY_SENDUP_REGISTERED
);
2366 * SADB_SPDFLUSH processing
2369 * from the user, and free all entries in secpctree.
2373 * NOTE: what to do is only marking SADB_SASTATE_DEAD.
2375 * m will always be freed.
2378 key_spdflush(struct socket
*so
, struct mbuf
*m
,
2379 const struct sadb_msghdr
*mhp
)
2381 struct sadb_msg
*newmsg
;
2382 struct secpolicy
*sp
;
2386 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
2387 panic("key_spdflush: NULL pointer is passed");
2389 if (m
->m_len
!= PFKEY_ALIGN8(sizeof(struct sadb_msg
)))
2390 return key_senderror(so
, m
, EINVAL
);
2392 for (dir
= 0; dir
< IPSEC_DIR_MAX
; dir
++) {
2393 struct secpolicy
* nextsp
;
2394 for (sp
= LIST_FIRST(&sptree
[dir
]);
2398 nextsp
= LIST_NEXT(sp
, chain
);
2399 if (sp
->state
== IPSEC_SPSTATE_DEAD
)
2403 /* 'sp' dead; continue transfers to 'sp = nextsp' */
2408 #if defined(__NetBSD__)
2409 /* Invalidate all cached SPD pointers in the PCBs. */
2410 ipsec_invalpcbcacheall();
2412 /* We're deleting policy; no need to invalidate the ipflow cache. */
2413 #endif /* __NetBSD__ */
2415 if (sizeof(struct sadb_msg
) > m
->m_len
+ M_TRAILINGSPACE(m
)) {
2416 ipseclog((LOG_DEBUG
, "key_spdflush: No more memory.\n"));
2417 return key_senderror(so
, m
, ENOBUFS
);
2423 m
->m_pkthdr
.len
= m
->m_len
= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
2424 newmsg
= mtod(m
, struct sadb_msg
*);
2425 newmsg
->sadb_msg_errno
= 0;
2426 newmsg
->sadb_msg_len
= PFKEY_UNIT64(m
->m_pkthdr
.len
);
2428 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ALL
);
2431 static struct sockaddr key_src
= {
2433 .sa_family
= PF_KEY
,
2436 static struct mbuf
*
2437 key_setspddump_chain(int *errorp
, int *lenp
, pid_t pid
)
2439 struct secpolicy
*sp
;
2442 struct mbuf
*m
, *n
, *prev
;
2447 /* search SPD entry and get buffer size. */
2449 for (dir
= 0; dir
< IPSEC_DIR_MAX
; dir
++) {
2450 LIST_FOREACH(sp
, &sptree
[dir
], chain
) {
2463 for (dir
= 0; dir
< IPSEC_DIR_MAX
; dir
++) {
2464 LIST_FOREACH(sp
, &sptree
[dir
], chain
) {
2466 n
= key_setdumpsp(sp
, SADB_X_SPDDUMP
, cnt
, pid
);
2474 totlen
+= n
->m_pkthdr
.len
;
2478 prev
->m_nextpkt
= n
;
2490 * SADB_SPDDUMP processing
2493 * from the user, and dump all SP leaves
2498 * m will always be freed.
2501 key_spddump(struct socket
*so
, struct mbuf
*m0
,
2502 const struct sadb_msghdr
*mhp
)
2510 if (so
== NULL
|| m0
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
2511 panic("key_spddump: NULL pointer is passed");
2514 pid
= mhp
->msg
->sadb_msg_pid
;
2516 * If the requestor has insufficient socket-buffer space
2517 * for the entire chain, nobody gets any response to the DUMP.
2518 * XXX For now, only the requestor ever gets anything.
2519 * Moreover, if the requestor has any space at all, they receive
2520 * the entire chain, otherwise the request is refused with ENOBUFS.
2522 if (sbspace(&so
->so_rcv
) <= 0) {
2523 return key_senderror(so
, m0
, ENOBUFS
);
2527 n
= key_setspddump_chain(&error
, &len
, pid
);
2531 return key_senderror(so
, m0
, ENOENT
);
2534 uint64_t *ps
= PFKEY_STAT_GETREF();
2535 ps
[PFKEY_STAT_IN_TOTAL
]++;
2536 ps
[PFKEY_STAT_IN_BYTES
] += len
;
2537 PFKEY_STAT_PUTREF();
2541 * PF_KEY DUMP responses are no longer broadcast to all PF_KEY sockets.
2542 * The requestor receives either the entire chain, or an
2543 * error message with ENOBUFS.
2547 * sbappendchainwith record takes the chain of entries, one
2548 * packet-record per SPD entry, prepends the key_src sockaddr
2549 * to each packet-record, links the sockaddr mbufs into a new
2550 * list of records, then appends the entire resulting
2551 * list to the requesting socket.
2553 ok
= sbappendaddrchain(&so
->so_rcv
, (struct sockaddr
*)&key_src
,
2554 n
, SB_PRIO_ONESHOT_OVERFLOW
);
2557 PFKEY_STATINC(PFKEY_STAT_IN_NOMEM
);
2559 return key_senderror(so
, m0
, ENOBUFS
);
2568 * SADB_X_NAT_T_NEW_MAPPING. Unused by racoon as of 2005/04/23
2571 key_nat_map(struct socket
*so
, struct mbuf
*m
,
2572 const struct sadb_msghdr
*mhp
)
2574 struct sadb_x_nat_t_type
*type
;
2575 struct sadb_x_nat_t_port
*sport
;
2576 struct sadb_x_nat_t_port
*dport
;
2577 struct sadb_address
*addr
;
2578 struct sadb_x_nat_t_frag
*frag
;
2581 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
2582 panic("key_nat_map: NULL pointer is passed.");
2584 if (mhp
->ext
[SADB_X_EXT_NAT_T_TYPE
] == NULL
||
2585 mhp
->ext
[SADB_X_EXT_NAT_T_SPORT
] == NULL
||
2586 mhp
->ext
[SADB_X_EXT_NAT_T_DPORT
] == NULL
) {
2587 ipseclog((LOG_DEBUG
, "key_nat_map: invalid message.\n"));
2588 return key_senderror(so
, m
, EINVAL
);
2590 if ((mhp
->extlen
[SADB_X_EXT_NAT_T_TYPE
] < sizeof(*type
)) ||
2591 (mhp
->extlen
[SADB_X_EXT_NAT_T_SPORT
] < sizeof(*sport
)) ||
2592 (mhp
->extlen
[SADB_X_EXT_NAT_T_DPORT
] < sizeof(*dport
))) {
2593 ipseclog((LOG_DEBUG
, "key_nat_map: invalid message.\n"));
2594 return key_senderror(so
, m
, EINVAL
);
2597 if ((mhp
->ext
[SADB_X_EXT_NAT_T_OA
] != NULL
) &&
2598 (mhp
->extlen
[SADB_X_EXT_NAT_T_OA
] < sizeof(*addr
))) {
2599 ipseclog((LOG_DEBUG
, "key_nat_map: invalid message\n"));
2600 return key_senderror(so
, m
, EINVAL
);
2603 if ((mhp
->ext
[SADB_X_EXT_NAT_T_FRAG
] != NULL
) &&
2604 (mhp
->extlen
[SADB_X_EXT_NAT_T_FRAG
] < sizeof(*frag
))) {
2605 ipseclog((LOG_DEBUG
, "key_nat_map: invalid message\n"));
2606 return key_senderror(so
, m
, EINVAL
);
2609 type
= (struct sadb_x_nat_t_type
*)mhp
->ext
[SADB_X_EXT_NAT_T_TYPE
];
2610 sport
= (struct sadb_x_nat_t_port
*)mhp
->ext
[SADB_X_EXT_NAT_T_SPORT
];
2611 dport
= (struct sadb_x_nat_t_port
*)mhp
->ext
[SADB_X_EXT_NAT_T_DPORT
];
2612 addr
= (struct sadb_address
*)mhp
->ext
[SADB_X_EXT_NAT_T_OA
];
2613 frag
= (struct sadb_x_nat_t_frag
*) mhp
->ext
[SADB_X_EXT_NAT_T_FRAG
];
2615 printf("sadb_nat_map called\n");
2618 * XXX handle that, it should also contain a SA, or anything
2619 * that enable to update the SA information.
2624 #endif /* IPSEC_NAT_T */
2626 static struct mbuf
*
2627 key_setdumpsp(struct secpolicy
*sp
, u_int8_t type
, u_int32_t seq
, pid_t pid
)
2629 struct mbuf
*result
= NULL
, *m
;
2631 m
= key_setsadbmsg(type
, 0, SADB_SATYPE_UNSPEC
, seq
, pid
, sp
->refcnt
);
2636 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
2637 &sp
->spidx
.src
.sa
, sp
->spidx
.prefs
,
2638 sp
->spidx
.ul_proto
);
2643 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
2644 &sp
->spidx
.dst
.sa
, sp
->spidx
.prefd
,
2645 sp
->spidx
.ul_proto
);
2655 if ((result
->m_flags
& M_PKTHDR
) == 0)
2658 if (result
->m_len
< sizeof(struct sadb_msg
)) {
2659 result
= m_pullup(result
, sizeof(struct sadb_msg
));
2664 result
->m_pkthdr
.len
= 0;
2665 for (m
= result
; m
; m
= m
->m_next
)
2666 result
->m_pkthdr
.len
+= m
->m_len
;
2668 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
2669 PFKEY_UNIT64(result
->m_pkthdr
.len
);
2679 * get PFKEY message length for security policy and request.
2682 key_getspreqmsglen(struct secpolicy
*sp
)
2686 tlen
= sizeof(struct sadb_x_policy
);
2688 /* if is the policy for ipsec ? */
2689 if (sp
->policy
!= IPSEC_POLICY_IPSEC
)
2692 /* get length of ipsec requests */
2694 struct ipsecrequest
*isr
;
2697 for (isr
= sp
->req
; isr
!= NULL
; isr
= isr
->next
) {
2698 len
= sizeof(struct sadb_x_ipsecrequest
)
2699 + isr
->saidx
.src
.sa
.sa_len
2700 + isr
->saidx
.dst
.sa
.sa_len
;
2702 tlen
+= PFKEY_ALIGN8(len
);
2710 * SADB_SPDEXPIRE processing
2712 * <base, address(SD), lifetime(CH), policy>
2716 * others : error number
2719 key_spdexpire(struct secpolicy
*sp
)
2722 struct mbuf
*result
= NULL
, *m
;
2725 struct sadb_lifetime
*lt
;
2727 /* XXX: Why do we lock ? */
2728 s
= splsoftnet(); /*called from softclock()*/
2732 panic("key_spdexpire: NULL pointer is passed");
2734 /* set msg header */
2735 m
= key_setsadbmsg(SADB_X_SPDEXPIRE
, 0, 0, 0, 0, 0);
2742 /* create lifetime extension (current and hard) */
2743 len
= PFKEY_ALIGN8(sizeof(*lt
)) * 2;
2744 m
= key_alloc_mbuf(len
);
2745 if (!m
|| m
->m_next
) { /*XXX*/
2751 memset(mtod(m
, void *), 0, len
);
2752 lt
= mtod(m
, struct sadb_lifetime
*);
2753 lt
->sadb_lifetime_len
= PFKEY_UNIT64(sizeof(struct sadb_lifetime
));
2754 lt
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
2755 lt
->sadb_lifetime_allocations
= 0;
2756 lt
->sadb_lifetime_bytes
= 0;
2757 lt
->sadb_lifetime_addtime
= sp
->created
;
2758 lt
->sadb_lifetime_usetime
= sp
->lastused
;
2759 lt
= (struct sadb_lifetime
*)(mtod(m
, char *) + len
/ 2);
2760 lt
->sadb_lifetime_len
= PFKEY_UNIT64(sizeof(struct sadb_lifetime
));
2761 lt
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_HARD
;
2762 lt
->sadb_lifetime_allocations
= 0;
2763 lt
->sadb_lifetime_bytes
= 0;
2764 lt
->sadb_lifetime_addtime
= sp
->lifetime
;
2765 lt
->sadb_lifetime_usetime
= sp
->validtime
;
2768 /* set sadb_address for source */
2769 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
2771 sp
->spidx
.prefs
, sp
->spidx
.ul_proto
);
2778 /* set sadb_address for destination */
2779 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
2781 sp
->spidx
.prefd
, sp
->spidx
.ul_proto
);
2796 if ((result
->m_flags
& M_PKTHDR
) == 0) {
2801 if (result
->m_len
< sizeof(struct sadb_msg
)) {
2802 result
= m_pullup(result
, sizeof(struct sadb_msg
));
2803 if (result
== NULL
) {
2809 result
->m_pkthdr
.len
= 0;
2810 for (m
= result
; m
; m
= m
->m_next
)
2811 result
->m_pkthdr
.len
+= m
->m_len
;
2813 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
2814 PFKEY_UNIT64(result
->m_pkthdr
.len
);
2816 return key_sendup_mbuf(NULL
, result
, KEY_SENDUP_REGISTERED
);
2825 /* %%% SAD management */
2827 * allocating a memory for new SA head, and copy from the values of mhp.
2828 * OUT: NULL : failure due to the lack of memory.
2829 * others : pointer to new SA head.
2831 static struct secashead
*
2832 key_newsah(struct secasindex
*saidx
)
2834 struct secashead
*newsah
;
2836 IPSEC_ASSERT(saidx
!= NULL
, ("key_newsaidx: null saidx"));
2838 newsah
= (struct secashead
*)
2839 malloc(sizeof(struct secashead
), M_SECA
, M_NOWAIT
|M_ZERO
);
2840 if (newsah
!= NULL
) {
2842 for (i
= 0; i
< sizeof(newsah
->savtree
)/sizeof(newsah
->savtree
[0]); i
++)
2843 LIST_INIT(&newsah
->savtree
[i
]);
2844 newsah
->saidx
= *saidx
;
2846 /* add to saidxtree */
2847 newsah
->state
= SADB_SASTATE_MATURE
;
2848 LIST_INSERT_HEAD(&sahtree
, newsah
, chain
);
2854 * delete SA index and all SA registerd.
2857 key_delsah(struct secashead
*sah
)
2859 struct secasvar
*sav
, *nextsav
;
2860 u_int stateidx
, state
;
2866 panic("key_delsah: NULL pointer is passed");
2868 s
= splsoftnet(); /*called from softclock()*/
2870 /* searching all SA registerd in the secindex. */
2872 stateidx
< _ARRAYLEN(saorder_state_any
);
2875 state
= saorder_state_any
[stateidx
];
2876 for (sav
= (struct secasvar
*)LIST_FIRST(&sah
->savtree
[state
]);
2880 nextsav
= LIST_NEXT(sav
, chain
);
2882 if (sav
->refcnt
== 0) {
2884 KEY_CHKSASTATE(state
, sav
->state
, "key_delsah");
2887 /* give up to delete this sa */
2893 /* don't delete sah only if there are savs. */
2899 rtcache_free(&sah
->sa_route
);
2901 /* remove from tree of SA index */
2902 if (__LIST_CHAINED(sah
))
2903 LIST_REMOVE(sah
, chain
);
2912 * allocating a new SA with LARVAL state. key_add() and key_getspi() call,
2913 * and copy the values of mhp into new buffer.
2914 * When SAD message type is GETSPI:
2915 * to set sequence number from acq_seq++,
2916 * to set zero to SPI.
2917 * not to call key_setsava().
2919 * others : pointer to new secasvar.
2921 * does not modify mbuf. does not free mbuf on error.
2923 static struct secasvar
*
2924 key_newsav(struct mbuf
*m
, const struct sadb_msghdr
*mhp
,
2925 struct secashead
*sah
, int *errp
,
2926 const char* where
, int tag
)
2928 struct secasvar
*newsav
;
2929 const struct sadb_sa
*xsa
;
2932 if (m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
|| sah
== NULL
)
2933 panic("key_newsa: NULL pointer is passed");
2935 KMALLOC(newsav
, struct secasvar
*, sizeof(struct secasvar
));
2936 if (newsav
== NULL
) {
2937 ipseclog((LOG_DEBUG
, "key_newsa: No more memory.\n"));
2941 memset(newsav
, 0, sizeof(struct secasvar
));
2943 switch (mhp
->msg
->sadb_msg_type
) {
2947 #ifdef IPSEC_DOSEQCHECK
2948 /* sync sequence number */
2949 if (mhp
->msg
->sadb_msg_seq
== 0)
2951 (acq_seq
= (acq_seq
== ~0 ? 1 : ++acq_seq
));
2954 newsav
->seq
= mhp
->msg
->sadb_msg_seq
;
2959 if (mhp
->ext
[SADB_EXT_SA
] == NULL
) {
2960 KFREE(newsav
), newsav
= NULL
;
2961 ipseclog((LOG_DEBUG
, "key_newsa: invalid message is passed.\n"));
2965 xsa
= (const struct sadb_sa
*)mhp
->ext
[SADB_EXT_SA
];
2966 newsav
->spi
= xsa
->sadb_sa_spi
;
2967 newsav
->seq
= mhp
->msg
->sadb_msg_seq
;
2970 KFREE(newsav
), newsav
= NULL
;
2975 /* copy sav values */
2976 if (mhp
->msg
->sadb_msg_type
!= SADB_GETSPI
) {
2977 *errp
= key_setsaval(newsav
, m
, mhp
);
2979 KFREE(newsav
), newsav
= NULL
;
2985 newsav
->created
= time_second
;
2986 newsav
->pid
= mhp
->msg
->sadb_msg_pid
;
2991 newsav
->state
= SADB_SASTATE_LARVAL
;
2992 LIST_INSERT_TAIL(&sah
->savtree
[SADB_SASTATE_LARVAL
], newsav
,
2995 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
2996 printf("DP key_newsav from %s:%u return SP:%p\n",
2997 where
, tag
, newsav
));
3003 * free() SA variable entry.
3006 key_delsav(struct secasvar
*sav
)
3008 IPSEC_ASSERT(sav
!= NULL
, ("key_delsav: null sav"));
3009 IPSEC_ASSERT(sav
->refcnt
== 0,
3010 ("key_delsav: reference count %u > 0", sav
->refcnt
));
3012 /* remove from SA header */
3013 if (__LIST_CHAINED(sav
))
3014 LIST_REMOVE(sav
, chain
);
3017 * Cleanup xform state. Note that zeroize'ing causes the
3018 * keys to be cleared; otherwise we must do it ourself.
3020 if (sav
->tdb_xform
!= NULL
) {
3021 sav
->tdb_xform
->xf_zeroize(sav
);
3022 sav
->tdb_xform
= NULL
;
3024 if (sav
->key_auth
!= NULL
)
3025 memset(_KEYBUF(sav
->key_auth
), 0, _KEYLEN(sav
->key_auth
));
3026 if (sav
->key_enc
!= NULL
)
3027 memset(_KEYBUF(sav
->key_enc
), 0, _KEYLEN(sav
->key_enc
));
3029 if (sav
->key_auth
!= NULL
) {
3030 KFREE(sav
->key_auth
);
3031 sav
->key_auth
= NULL
;
3033 if (sav
->key_enc
!= NULL
) {
3034 KFREE(sav
->key_enc
);
3035 sav
->key_enc
= NULL
;
3038 memset(sav
->sched
, 0, sav
->schedlen
);
3042 if (sav
->replay
!= NULL
) {
3046 if (sav
->lft_c
!= NULL
) {
3050 if (sav
->lft_h
!= NULL
) {
3054 if (sav
->lft_s
!= NULL
) {
3058 if (sav
->iv
!= NULL
) {
3072 * others : found, pointer to a SA.
3074 static struct secashead
*
3075 key_getsah(struct secasindex
*saidx
)
3077 struct secashead
*sah
;
3079 LIST_FOREACH(sah
, &sahtree
, chain
) {
3080 if (sah
->state
== SADB_SASTATE_DEAD
)
3082 if (key_cmpsaidx(&sah
->saidx
, saidx
, CMP_REQID
))
3090 * check not to be duplicated SPI.
3091 * NOTE: this function is too slow due to searching all SAD.
3094 * others : found, pointer to a SA.
3096 static struct secasvar
*
3097 key_checkspidup(struct secasindex
*saidx
, u_int32_t spi
)
3099 struct secashead
*sah
;
3100 struct secasvar
*sav
;
3102 /* check address family */
3103 if (saidx
->src
.sa
.sa_family
!= saidx
->dst
.sa
.sa_family
) {
3104 ipseclog((LOG_DEBUG
, "key_checkspidup: address family mismatched.\n"));
3109 LIST_FOREACH(sah
, &sahtree
, chain
) {
3110 if (!key_ismyaddr((struct sockaddr
*)&sah
->saidx
.dst
))
3112 sav
= key_getsavbyspi(sah
, spi
);
3121 * search SAD litmited alive SA, protocol, SPI.
3124 * others : found, pointer to a SA.
3126 static struct secasvar
*
3127 key_getsavbyspi(struct secashead
*sah
, u_int32_t spi
)
3129 struct secasvar
*sav
;
3130 u_int stateidx
, state
;
3132 /* search all status */
3134 stateidx
< _ARRAYLEN(saorder_state_alive
);
3137 state
= saorder_state_alive
[stateidx
];
3138 LIST_FOREACH(sav
, &sah
->savtree
[state
], chain
) {
3141 if (sav
->state
!= state
) {
3142 ipseclog((LOG_DEBUG
, "key_getsavbyspi: "
3143 "invalid sav->state (queue: %d SA: %d)\n",
3144 state
, sav
->state
));
3148 if (sav
->spi
== spi
)
3157 * copy SA values from PF_KEY message except *SPI, SEQ, PID, STATE and TYPE*.
3158 * You must update these if need.
3162 * does not modify mbuf. does not free mbuf on error.
3165 key_setsaval(struct secasvar
*sav
, struct mbuf
*m
,
3166 const struct sadb_msghdr
*mhp
)
3171 if (m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
3172 panic("key_setsaval: NULL pointer is passed");
3174 /* initialization */
3176 sav
->key_auth
= NULL
;
3177 sav
->key_enc
= NULL
;
3184 sav
->tdb_xform
= NULL
; /* transform */
3185 sav
->tdb_encalgxform
= NULL
; /* encoding algorithm */
3186 sav
->tdb_authalgxform
= NULL
; /* authentication algorithm */
3187 sav
->tdb_compalgxform
= NULL
; /* compression algorithm */
3194 if (mhp
->ext
[SADB_EXT_SA
] != NULL
) {
3195 const struct sadb_sa
*sa0
;
3197 sa0
= (const struct sadb_sa
*)mhp
->ext
[SADB_EXT_SA
];
3198 if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(*sa0
)) {
3203 sav
->alg_auth
= sa0
->sadb_sa_auth
;
3204 sav
->alg_enc
= sa0
->sadb_sa_encrypt
;
3205 sav
->flags
= sa0
->sadb_sa_flags
;
3208 if ((sa0
->sadb_sa_flags
& SADB_X_EXT_OLD
) == 0) {
3209 sav
->replay
= (struct secreplay
*)
3210 malloc(sizeof(struct secreplay
)+sa0
->sadb_sa_replay
, M_SECA
, M_NOWAIT
|M_ZERO
);
3211 if (sav
->replay
== NULL
) {
3212 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
3216 if (sa0
->sadb_sa_replay
!= 0)
3217 sav
->replay
->bitmap
= (char*)(sav
->replay
+1);
3218 sav
->replay
->wsize
= sa0
->sadb_sa_replay
;
3222 /* Authentication keys */
3223 if (mhp
->ext
[SADB_EXT_KEY_AUTH
] != NULL
) {
3224 const struct sadb_key
*key0
;
3227 key0
= (const struct sadb_key
*)mhp
->ext
[SADB_EXT_KEY_AUTH
];
3228 len
= mhp
->extlen
[SADB_EXT_KEY_AUTH
];
3231 if (len
< sizeof(*key0
)) {
3235 switch (mhp
->msg
->sadb_msg_satype
) {
3236 case SADB_SATYPE_AH
:
3237 case SADB_SATYPE_ESP
:
3238 case SADB_X_SATYPE_TCPSIGNATURE
:
3239 if (len
== PFKEY_ALIGN8(sizeof(struct sadb_key
)) &&
3240 sav
->alg_auth
!= SADB_X_AALG_NULL
)
3243 case SADB_X_SATYPE_IPCOMP
:
3249 ipseclog((LOG_DEBUG
, "key_setsaval: invalid key_auth values.\n"));
3253 sav
->key_auth
= (struct sadb_key
*)key_newbuf(key0
, len
);
3254 if (sav
->key_auth
== NULL
) {
3255 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
3261 /* Encryption key */
3262 if (mhp
->ext
[SADB_EXT_KEY_ENCRYPT
] != NULL
) {
3263 const struct sadb_key
*key0
;
3266 key0
= (const struct sadb_key
*)mhp
->ext
[SADB_EXT_KEY_ENCRYPT
];
3267 len
= mhp
->extlen
[SADB_EXT_KEY_ENCRYPT
];
3270 if (len
< sizeof(*key0
)) {
3274 switch (mhp
->msg
->sadb_msg_satype
) {
3275 case SADB_SATYPE_ESP
:
3276 if (len
== PFKEY_ALIGN8(sizeof(struct sadb_key
)) &&
3277 sav
->alg_enc
!= SADB_EALG_NULL
) {
3281 sav
->key_enc
= (struct sadb_key
*)key_newbuf(key0
, len
);
3282 if (sav
->key_enc
== NULL
) {
3283 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
3288 case SADB_X_SATYPE_IPCOMP
:
3289 if (len
!= PFKEY_ALIGN8(sizeof(struct sadb_key
)))
3291 sav
->key_enc
= NULL
; /*just in case*/
3293 case SADB_SATYPE_AH
:
3294 case SADB_X_SATYPE_TCPSIGNATURE
:
3300 ipseclog((LOG_DEBUG
, "key_setsatval: invalid key_enc value.\n"));
3308 switch (mhp
->msg
->sadb_msg_satype
) {
3309 case SADB_SATYPE_AH
:
3310 error
= xform_init(sav
, XF_AH
);
3312 case SADB_SATYPE_ESP
:
3313 error
= xform_init(sav
, XF_ESP
);
3315 case SADB_X_SATYPE_IPCOMP
:
3316 error
= xform_init(sav
, XF_IPCOMP
);
3318 case SADB_X_SATYPE_TCPSIGNATURE
:
3319 error
= xform_init(sav
, XF_TCPSIGNATURE
);
3323 ipseclog((LOG_DEBUG
,
3324 "key_setsaval: unable to initialize SA type %u.\n",
3325 mhp
->msg
->sadb_msg_satype
));
3330 sav
->created
= time_second
;
3332 /* make lifetime for CURRENT */
3333 KMALLOC(sav
->lft_c
, struct sadb_lifetime
*,
3334 sizeof(struct sadb_lifetime
));
3335 if (sav
->lft_c
== NULL
) {
3336 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
3341 sav
->lft_c
->sadb_lifetime_len
=
3342 PFKEY_UNIT64(sizeof(struct sadb_lifetime
));
3343 sav
->lft_c
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
3344 sav
->lft_c
->sadb_lifetime_allocations
= 0;
3345 sav
->lft_c
->sadb_lifetime_bytes
= 0;
3346 sav
->lft_c
->sadb_lifetime_addtime
= time_second
;
3347 sav
->lft_c
->sadb_lifetime_usetime
= 0;
3349 /* lifetimes for HARD and SOFT */
3351 const struct sadb_lifetime
*lft0
;
3353 lft0
= (struct sadb_lifetime
*)mhp
->ext
[SADB_EXT_LIFETIME_HARD
];
3355 if (mhp
->extlen
[SADB_EXT_LIFETIME_HARD
] < sizeof(*lft0
)) {
3359 sav
->lft_h
= (struct sadb_lifetime
*)key_newbuf(lft0
,
3361 if (sav
->lft_h
== NULL
) {
3362 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
3366 /* to be initialize ? */
3369 lft0
= (struct sadb_lifetime
*)mhp
->ext
[SADB_EXT_LIFETIME_SOFT
];
3371 if (mhp
->extlen
[SADB_EXT_LIFETIME_SOFT
] < sizeof(*lft0
)) {
3375 sav
->lft_s
= (struct sadb_lifetime
*)key_newbuf(lft0
,
3377 if (sav
->lft_s
== NULL
) {
3378 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
3382 /* to be initialize ? */
3389 /* initialization */
3390 if (sav
->replay
!= NULL
) {
3394 if (sav
->key_auth
!= NULL
) {
3395 KFREE(sav
->key_auth
);
3396 sav
->key_auth
= NULL
;
3398 if (sav
->key_enc
!= NULL
) {
3399 KFREE(sav
->key_enc
);
3400 sav
->key_enc
= NULL
;
3406 if (sav
->iv
!= NULL
) {
3410 if (sav
->lft_c
!= NULL
) {
3414 if (sav
->lft_h
!= NULL
) {
3418 if (sav
->lft_s
!= NULL
) {
3427 * validation with a secasvar entry, and set SADB_SATYPE_MATURE.
3432 key_mature(struct secasvar
*sav
)
3436 /* check SPI value */
3437 switch (sav
->sah
->saidx
.proto
) {
3440 if (ntohl(sav
->spi
) <= 255) {
3441 ipseclog((LOG_DEBUG
,
3442 "key_mature: illegal range of SPI %u.\n",
3443 (u_int32_t
)ntohl(sav
->spi
)));
3450 switch (sav
->sah
->saidx
.proto
) {
3453 if ((sav
->flags
& (SADB_X_EXT_OLD
|SADB_X_EXT_DERIV
)) ==
3454 (SADB_X_EXT_OLD
|SADB_X_EXT_DERIV
)) {
3455 ipseclog((LOG_DEBUG
, "key_mature: "
3456 "invalid flag (derived) given to old-esp.\n"));
3459 error
= xform_init(sav
, XF_ESP
);
3463 if (sav
->flags
& SADB_X_EXT_DERIV
) {
3464 ipseclog((LOG_DEBUG
, "key_mature: "
3465 "invalid flag (derived) given to AH SA.\n"));
3468 if (sav
->alg_enc
!= SADB_EALG_NONE
) {
3469 ipseclog((LOG_DEBUG
, "key_mature: "
3470 "protocol and algorithm mismated.\n"));
3473 error
= xform_init(sav
, XF_AH
);
3475 case IPPROTO_IPCOMP
:
3476 if (sav
->alg_auth
!= SADB_AALG_NONE
) {
3477 ipseclog((LOG_DEBUG
, "key_mature: "
3478 "protocol and algorithm mismated.\n"));
3481 if ((sav
->flags
& SADB_X_EXT_RAWCPI
) == 0
3482 && ntohl(sav
->spi
) >= 0x10000) {
3483 ipseclog((LOG_DEBUG
, "key_mature: invalid cpi for IPComp.\n"));
3486 error
= xform_init(sav
, XF_IPCOMP
);
3489 if (sav
->alg_enc
!= SADB_EALG_NONE
) {
3490 ipseclog((LOG_DEBUG
, "%s: protocol and algorithm "
3491 "mismated.\n", __func__
));
3494 error
= xform_init(sav
, XF_TCPSIGNATURE
);
3497 ipseclog((LOG_DEBUG
, "key_mature: Invalid satype.\n"));
3498 error
= EPROTONOSUPPORT
;
3502 key_sa_chgstate(sav
, SADB_SASTATE_MATURE
);
3507 * subroutine for SADB_GET and SADB_DUMP.
3509 static struct mbuf
*
3510 key_setdumpsa(struct secasvar
*sav
, u_int8_t type
, u_int8_t satype
,
3511 u_int32_t seq
, u_int32_t pid
)
3513 struct mbuf
*result
= NULL
, *tres
= NULL
, *m
;
3518 SADB_EXT_SA
, SADB_X_EXT_SA2
,
3519 SADB_EXT_LIFETIME_HARD
, SADB_EXT_LIFETIME_SOFT
,
3520 SADB_EXT_LIFETIME_CURRENT
, SADB_EXT_ADDRESS_SRC
,
3521 SADB_EXT_ADDRESS_DST
, SADB_EXT_ADDRESS_PROXY
, SADB_EXT_KEY_AUTH
,
3522 SADB_EXT_KEY_ENCRYPT
, SADB_EXT_IDENTITY_SRC
,
3523 SADB_EXT_IDENTITY_DST
, SADB_EXT_SENSITIVITY
,
3525 SADB_X_EXT_NAT_T_TYPE
, SADB_X_EXT_NAT_T_SPORT
,
3526 SADB_X_EXT_NAT_T_DPORT
, SADB_X_EXT_NAT_T_OA
,
3527 SADB_X_EXT_NAT_T_FRAG
,
3532 m
= key_setsadbmsg(type
, 0, satype
, seq
, pid
, sav
->refcnt
);
3537 for (i
= sizeof(dumporder
)/sizeof(dumporder
[0]) - 1; i
>= 0; i
--) {
3540 switch (dumporder
[i
]) {
3542 m
= key_setsadbsa(sav
);
3547 case SADB_X_EXT_SA2
:
3548 m
= key_setsadbxsa2(sav
->sah
->saidx
.mode
,
3549 sav
->replay
? sav
->replay
->count
: 0,
3550 sav
->sah
->saidx
.reqid
);
3555 case SADB_EXT_ADDRESS_SRC
:
3556 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
3557 &sav
->sah
->saidx
.src
.sa
,
3558 FULLMASK
, IPSEC_ULPROTO_ANY
);
3563 case SADB_EXT_ADDRESS_DST
:
3564 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
3565 &sav
->sah
->saidx
.dst
.sa
,
3566 FULLMASK
, IPSEC_ULPROTO_ANY
);
3571 case SADB_EXT_KEY_AUTH
:
3574 l
= PFKEY_UNUNIT64(sav
->key_auth
->sadb_key_len
);
3578 case SADB_EXT_KEY_ENCRYPT
:
3581 l
= PFKEY_UNUNIT64(sav
->key_enc
->sadb_key_len
);
3585 case SADB_EXT_LIFETIME_CURRENT
:
3588 l
= PFKEY_UNUNIT64(((struct sadb_ext
*)sav
->lft_c
)->sadb_ext_len
);
3592 case SADB_EXT_LIFETIME_HARD
:
3595 l
= PFKEY_UNUNIT64(((struct sadb_ext
*)sav
->lft_h
)->sadb_ext_len
);
3599 case SADB_EXT_LIFETIME_SOFT
:
3602 l
= PFKEY_UNUNIT64(((struct sadb_ext
*)sav
->lft_s
)->sadb_ext_len
);
3607 case SADB_X_EXT_NAT_T_TYPE
:
3608 if ((m
= key_setsadbxtype(sav
->natt_type
)) == NULL
)
3612 case SADB_X_EXT_NAT_T_DPORT
:
3613 if ((m
= key_setsadbxport(
3614 key_portfromsaddr(&sav
->sah
->saidx
.dst
),
3615 SADB_X_EXT_NAT_T_DPORT
)) == NULL
)
3619 case SADB_X_EXT_NAT_T_SPORT
:
3620 if ((m
= key_setsadbxport(
3621 key_portfromsaddr(&sav
->sah
->saidx
.src
),
3622 SADB_X_EXT_NAT_T_SPORT
)) == NULL
)
3626 case SADB_X_EXT_NAT_T_OA
:
3627 case SADB_X_EXT_NAT_T_FRAG
:
3631 case SADB_EXT_ADDRESS_PROXY
:
3632 case SADB_EXT_IDENTITY_SRC
:
3633 case SADB_EXT_IDENTITY_DST
:
3634 /* XXX: should we brought from SPD ? */
3635 case SADB_EXT_SENSITIVITY
:
3640 if ((!m
&& !p
) || (m
&& p
))
3643 M_PREPEND(tres
, l
, M_DONTWAIT
);
3646 memcpy(mtod(tres
, void *), p
, l
);
3650 m
= key_alloc_mbuf(l
);
3653 m_copyback(m
, 0, l
, p
);
3661 m_cat(result
, tres
);
3663 if (result
->m_len
< sizeof(struct sadb_msg
)) {
3664 result
= m_pullup(result
, sizeof(struct sadb_msg
));
3669 result
->m_pkthdr
.len
= 0;
3670 for (m
= result
; m
; m
= m
->m_next
)
3671 result
->m_pkthdr
.len
+= m
->m_len
;
3673 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
3674 PFKEY_UNIT64(result
->m_pkthdr
.len
);
3687 * set a type in sadb_x_nat_t_type
3689 static struct mbuf
*
3690 key_setsadbxtype(u_int16_t type
)
3694 struct sadb_x_nat_t_type
*p
;
3696 len
= PFKEY_ALIGN8(sizeof(struct sadb_x_nat_t_type
));
3698 m
= key_alloc_mbuf(len
);
3699 if (!m
|| m
->m_next
) { /*XXX*/
3705 p
= mtod(m
, struct sadb_x_nat_t_type
*);
3708 p
->sadb_x_nat_t_type_len
= PFKEY_UNIT64(len
);
3709 p
->sadb_x_nat_t_type_exttype
= SADB_X_EXT_NAT_T_TYPE
;
3710 p
->sadb_x_nat_t_type_type
= type
;
3715 * set a port in sadb_x_nat_t_port. port is in network order
3717 static struct mbuf
*
3718 key_setsadbxport(u_int16_t port
, u_int16_t type
)
3722 struct sadb_x_nat_t_port
*p
;
3724 len
= PFKEY_ALIGN8(sizeof(struct sadb_x_nat_t_port
));
3726 m
= key_alloc_mbuf(len
);
3727 if (!m
|| m
->m_next
) { /*XXX*/
3733 p
= mtod(m
, struct sadb_x_nat_t_port
*);
3736 p
->sadb_x_nat_t_port_len
= PFKEY_UNIT64(len
);
3737 p
->sadb_x_nat_t_port_exttype
= type
;
3738 p
->sadb_x_nat_t_port_port
= port
;
3744 * Get port from sockaddr, port is in network order
3747 key_portfromsaddr(const union sockaddr_union
*saddr
)
3751 switch (saddr
->sa
.sa_family
) {
3753 port
= saddr
->sin
.sin_port
;
3758 port
= saddr
->sin6
.sin6_port
;
3763 printf("key_portfromsaddr: unexpected address family\n");
3771 #endif /* IPSEC_NAT_T */
3774 * Set port is struct sockaddr. port is in network order
3777 key_porttosaddr(union sockaddr_union
*saddr
, u_int16_t port
)
3779 switch (saddr
->sa
.sa_family
) {
3781 saddr
->sin
.sin_port
= port
;
3786 saddr
->sin6
.sin6_port
= port
;
3791 printf("key_porttosaddr: unexpected address family %d\n",
3792 saddr
->sa
.sa_family
);
3800 * Safety check sa_len
3803 key_checksalen(const union sockaddr_union
*saddr
)
3805 switch (saddr
->sa
.sa_family
) {
3807 if (saddr
->sa
.sa_len
!= sizeof(struct sockaddr_in
))
3812 if (saddr
->sa
.sa_len
!= sizeof(struct sockaddr_in6
))
3817 printf("key_checksalen: unexpected sa_family %d\n",
3818 saddr
->sa
.sa_family
);
3827 * set data into sadb_msg.
3829 static struct mbuf
*
3830 key_setsadbmsg(u_int8_t type
, u_int16_t tlen
, u_int8_t satype
,
3831 u_int32_t seq
, pid_t pid
, u_int16_t reserved
)
3837 len
= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
3840 MGETHDR(m
, M_DONTWAIT
, MT_DATA
);
3841 if (m
&& len
> MHLEN
) {
3842 MCLGET(m
, M_DONTWAIT
);
3843 if ((m
->m_flags
& M_EXT
) == 0) {
3850 m
->m_pkthdr
.len
= m
->m_len
= len
;
3853 p
= mtod(m
, struct sadb_msg
*);
3856 p
->sadb_msg_version
= PF_KEY_V2
;
3857 p
->sadb_msg_type
= type
;
3858 p
->sadb_msg_errno
= 0;
3859 p
->sadb_msg_satype
= satype
;
3860 p
->sadb_msg_len
= PFKEY_UNIT64(tlen
);
3861 p
->sadb_msg_reserved
= reserved
;
3862 p
->sadb_msg_seq
= seq
;
3863 p
->sadb_msg_pid
= (u_int32_t
)pid
;
3869 * copy secasvar data into sadb_address.
3871 static struct mbuf
*
3872 key_setsadbsa(struct secasvar
*sav
)
3878 len
= PFKEY_ALIGN8(sizeof(struct sadb_sa
));
3879 m
= key_alloc_mbuf(len
);
3880 if (!m
|| m
->m_next
) { /*XXX*/
3886 p
= mtod(m
, struct sadb_sa
*);
3889 p
->sadb_sa_len
= PFKEY_UNIT64(len
);
3890 p
->sadb_sa_exttype
= SADB_EXT_SA
;
3891 p
->sadb_sa_spi
= sav
->spi
;
3892 p
->sadb_sa_replay
= (sav
->replay
!= NULL
? sav
->replay
->wsize
: 0);
3893 p
->sadb_sa_state
= sav
->state
;
3894 p
->sadb_sa_auth
= sav
->alg_auth
;
3895 p
->sadb_sa_encrypt
= sav
->alg_enc
;
3896 p
->sadb_sa_flags
= sav
->flags
;
3902 * set data into sadb_address.
3904 static struct mbuf
*
3905 key_setsadbaddr(u_int16_t exttype
, const struct sockaddr
*saddr
,
3906 u_int8_t prefixlen
, u_int16_t ul_proto
)
3909 struct sadb_address
*p
;
3912 len
= PFKEY_ALIGN8(sizeof(struct sadb_address
)) +
3913 PFKEY_ALIGN8(saddr
->sa_len
);
3914 m
= key_alloc_mbuf(len
);
3915 if (!m
|| m
->m_next
) { /*XXX*/
3921 p
= mtod(m
, struct sadb_address
*);
3924 p
->sadb_address_len
= PFKEY_UNIT64(len
);
3925 p
->sadb_address_exttype
= exttype
;
3926 p
->sadb_address_proto
= ul_proto
;
3927 if (prefixlen
== FULLMASK
) {
3928 switch (saddr
->sa_family
) {
3930 prefixlen
= sizeof(struct in_addr
) << 3;
3933 prefixlen
= sizeof(struct in6_addr
) << 3;
3939 p
->sadb_address_prefixlen
= prefixlen
;
3940 p
->sadb_address_reserved
= 0;
3942 memcpy(mtod(m
, char *) + PFKEY_ALIGN8(sizeof(struct sadb_address
)),
3943 saddr
, saddr
->sa_len
);
3950 * set data into sadb_ident.
3952 static struct mbuf
*
3953 key_setsadbident(u_int16_t exttype
, u_int16_t idtype
,
3954 void *string
, int stringlen
, u_int64_t id
)
3957 struct sadb_ident
*p
;
3960 len
= PFKEY_ALIGN8(sizeof(struct sadb_ident
)) + PFKEY_ALIGN8(stringlen
);
3961 m
= key_alloc_mbuf(len
);
3962 if (!m
|| m
->m_next
) { /*XXX*/
3968 p
= mtod(m
, struct sadb_ident
*);
3971 p
->sadb_ident_len
= PFKEY_UNIT64(len
);
3972 p
->sadb_ident_exttype
= exttype
;
3973 p
->sadb_ident_type
= idtype
;
3974 p
->sadb_ident_reserved
= 0;
3975 p
->sadb_ident_id
= id
;
3977 memcpy(mtod(m
, void *) + PFKEY_ALIGN8(sizeof(struct sadb_ident
)),
3985 * set data into sadb_x_sa2.
3987 static struct mbuf
*
3988 key_setsadbxsa2(u_int8_t mode
, u_int32_t seq
, u_int16_t reqid
)
3991 struct sadb_x_sa2
*p
;
3994 len
= PFKEY_ALIGN8(sizeof(struct sadb_x_sa2
));
3995 m
= key_alloc_mbuf(len
);
3996 if (!m
|| m
->m_next
) { /*XXX*/
4002 p
= mtod(m
, struct sadb_x_sa2
*);
4005 p
->sadb_x_sa2_len
= PFKEY_UNIT64(len
);
4006 p
->sadb_x_sa2_exttype
= SADB_X_EXT_SA2
;
4007 p
->sadb_x_sa2_mode
= mode
;
4008 p
->sadb_x_sa2_reserved1
= 0;
4009 p
->sadb_x_sa2_reserved2
= 0;
4010 p
->sadb_x_sa2_sequence
= seq
;
4011 p
->sadb_x_sa2_reqid
= reqid
;
4017 * set data into sadb_x_policy
4019 static struct mbuf
*
4020 key_setsadbxpolicy(u_int16_t type
, u_int8_t dir
, u_int32_t id
)
4023 struct sadb_x_policy
*p
;
4026 len
= PFKEY_ALIGN8(sizeof(struct sadb_x_policy
));
4027 m
= key_alloc_mbuf(len
);
4028 if (!m
|| m
->m_next
) { /*XXX*/
4034 p
= mtod(m
, struct sadb_x_policy
*);
4037 p
->sadb_x_policy_len
= PFKEY_UNIT64(len
);
4038 p
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
4039 p
->sadb_x_policy_type
= type
;
4040 p
->sadb_x_policy_dir
= dir
;
4041 p
->sadb_x_policy_id
= id
;
4048 * copy a buffer into the new buffer allocated.
4051 key_newbuf(const void *src
, u_int len
)
4055 KMALLOC(new, void *, len
);
4057 ipseclog((LOG_DEBUG
, "key_newbuf: No more memory.\n"));
4060 memcpy(new, src
, len
);
4065 /* compare my own address
4066 * OUT: 1: true, i.e. my address.
4070 key_ismyaddr(struct sockaddr
*sa
)
4073 struct sockaddr_in
*sin
;
4074 struct in_ifaddr
*ia
;
4079 panic("key_ismyaddr: NULL pointer is passed");
4081 switch (sa
->sa_family
) {
4084 sin
= (struct sockaddr_in
*)sa
;
4085 for (ia
= in_ifaddrhead
.tqh_first
; ia
;
4086 ia
= ia
->ia_link
.tqe_next
)
4088 if (sin
->sin_family
== ia
->ia_addr
.sin_family
&&
4089 sin
->sin_len
== ia
->ia_addr
.sin_len
&&
4090 sin
->sin_addr
.s_addr
== ia
->ia_addr
.sin_addr
.s_addr
)
4099 return key_ismyaddr6((struct sockaddr_in6
*)sa
);
4108 * compare my own address for IPv6.
4111 * NOTE: derived ip6_input() in KAME. This is necessary to modify more.
4113 #include <netinet6/in6_var.h>
4116 key_ismyaddr6(struct sockaddr_in6
*sin6
)
4118 struct in6_ifaddr
*ia
;
4119 struct in6_multi
*in6m
;
4121 for (ia
= in6_ifaddr
; ia
; ia
= ia
->ia_next
) {
4122 if (key_sockaddrcmp((struct sockaddr
*)&sin6
,
4123 (struct sockaddr
*)&ia
->ia_addr
, 0) == 0)
4128 * XXX why do we care about multlicast here while we don't care
4129 * about IPv4 multicast??
4134 IN6_LOOKUP_MULTI(sin6
->sin6_addr
, ia
->ia_ifp
, in6m
);
4136 for ((in6m
) = ia
->ia6_multiaddrs
.lh_first
;
4138 !IN6_ARE_ADDR_EQUAL(&(in6m
)->in6m_addr
, &sin6
->sin6_addr
);
4139 (in6m
) = in6m
->in6m_entry
.le_next
)
4146 /* loopback, just for safety */
4147 if (IN6_IS_ADDR_LOOPBACK(&sin6
->sin6_addr
))
4155 * compare two secasindex structure.
4156 * flag can specify to compare 2 saidxes.
4157 * compare two secasindex structure without both mode and reqid.
4158 * don't compare port.
4160 * saidx0: source, it can be in SAD.
4168 const struct secasindex
*saidx0
,
4169 const struct secasindex
*saidx1
,
4175 if (saidx0
== NULL
&& saidx1
== NULL
)
4178 if (saidx0
== NULL
|| saidx1
== NULL
)
4181 if (saidx0
->proto
!= saidx1
->proto
)
4184 if (flag
== CMP_EXACTLY
) {
4185 if (saidx0
->mode
!= saidx1
->mode
)
4187 if (saidx0
->reqid
!= saidx1
->reqid
)
4189 if (memcmp(&saidx0
->src
, &saidx1
->src
, saidx0
->src
.sa
.sa_len
) != 0 ||
4190 memcmp(&saidx0
->dst
, &saidx1
->dst
, saidx0
->dst
.sa
.sa_len
) != 0)
4194 /* CMP_MODE_REQID, CMP_REQID, CMP_HEAD */
4195 if (flag
== CMP_MODE_REQID
4196 ||flag
== CMP_REQID
) {
4198 * If reqid of SPD is non-zero, unique SA is required.
4199 * The result must be of same reqid in this case.
4201 if (saidx1
->reqid
!= 0 && saidx0
->reqid
!= saidx1
->reqid
)
4205 if (flag
== CMP_MODE_REQID
) {
4206 if (saidx0
->mode
!= IPSEC_MODE_ANY
4207 && saidx0
->mode
!= saidx1
->mode
)
4212 * If NAT-T is enabled, check ports for tunnel mode.
4213 * Don't do it for transport mode, as there is no
4214 * port information available in the SP.
4217 if (saidx1
->mode
== IPSEC_MODE_TUNNEL
)
4221 if (key_sockaddrcmp(&saidx0
->src
.sa
, &saidx1
->src
.sa
, chkport
) != 0) {
4224 if (key_sockaddrcmp(&saidx0
->dst
.sa
, &saidx1
->dst
.sa
, chkport
) != 0) {
4233 * compare two secindex structure exactly.
4235 * spidx0: source, it is often in SPD.
4236 * spidx1: object, it is often from PFKEY message.
4242 key_cmpspidx_exactly(
4243 struct secpolicyindex
*spidx0
,
4244 struct secpolicyindex
*spidx1
)
4247 if (spidx0
== NULL
&& spidx1
== NULL
)
4250 if (spidx0
== NULL
|| spidx1
== NULL
)
4253 if (spidx0
->prefs
!= spidx1
->prefs
4254 || spidx0
->prefd
!= spidx1
->prefd
4255 || spidx0
->ul_proto
!= spidx1
->ul_proto
)
4258 return key_sockaddrcmp(&spidx0
->src
.sa
, &spidx1
->src
.sa
, 1) == 0 &&
4259 key_sockaddrcmp(&spidx0
->dst
.sa
, &spidx1
->dst
.sa
, 1) == 0;
4263 * compare two secindex structure with mask.
4265 * spidx0: source, it is often in SPD.
4266 * spidx1: object, it is often from IP header.
4272 key_cmpspidx_withmask(
4273 struct secpolicyindex
*spidx0
,
4274 struct secpolicyindex
*spidx1
)
4277 if (spidx0
== NULL
&& spidx1
== NULL
)
4280 if (spidx0
== NULL
|| spidx1
== NULL
)
4283 if (spidx0
->src
.sa
.sa_family
!= spidx1
->src
.sa
.sa_family
||
4284 spidx0
->dst
.sa
.sa_family
!= spidx1
->dst
.sa
.sa_family
||
4285 spidx0
->src
.sa
.sa_len
!= spidx1
->src
.sa
.sa_len
||
4286 spidx0
->dst
.sa
.sa_len
!= spidx1
->dst
.sa
.sa_len
)
4289 /* if spidx.ul_proto == IPSEC_ULPROTO_ANY, ignore. */
4290 if (spidx0
->ul_proto
!= (u_int16_t
)IPSEC_ULPROTO_ANY
4291 && spidx0
->ul_proto
!= spidx1
->ul_proto
)
4294 switch (spidx0
->src
.sa
.sa_family
) {
4296 if (spidx0
->src
.sin
.sin_port
!= IPSEC_PORT_ANY
4297 && spidx0
->src
.sin
.sin_port
!= spidx1
->src
.sin
.sin_port
)
4299 if (!key_bbcmp(&spidx0
->src
.sin
.sin_addr
,
4300 &spidx1
->src
.sin
.sin_addr
, spidx0
->prefs
))
4304 if (spidx0
->src
.sin6
.sin6_port
!= IPSEC_PORT_ANY
4305 && spidx0
->src
.sin6
.sin6_port
!= spidx1
->src
.sin6
.sin6_port
)
4308 * scope_id check. if sin6_scope_id is 0, we regard it
4309 * as a wildcard scope, which matches any scope zone ID.
4311 if (spidx0
->src
.sin6
.sin6_scope_id
&&
4312 spidx1
->src
.sin6
.sin6_scope_id
&&
4313 spidx0
->src
.sin6
.sin6_scope_id
!= spidx1
->src
.sin6
.sin6_scope_id
)
4315 if (!key_bbcmp(&spidx0
->src
.sin6
.sin6_addr
,
4316 &spidx1
->src
.sin6
.sin6_addr
, spidx0
->prefs
))
4321 if (memcmp(&spidx0
->src
, &spidx1
->src
, spidx0
->src
.sa
.sa_len
) != 0)
4326 switch (spidx0
->dst
.sa
.sa_family
) {
4328 if (spidx0
->dst
.sin
.sin_port
!= IPSEC_PORT_ANY
4329 && spidx0
->dst
.sin
.sin_port
!= spidx1
->dst
.sin
.sin_port
)
4331 if (!key_bbcmp(&spidx0
->dst
.sin
.sin_addr
,
4332 &spidx1
->dst
.sin
.sin_addr
, spidx0
->prefd
))
4336 if (spidx0
->dst
.sin6
.sin6_port
!= IPSEC_PORT_ANY
4337 && spidx0
->dst
.sin6
.sin6_port
!= spidx1
->dst
.sin6
.sin6_port
)
4340 * scope_id check. if sin6_scope_id is 0, we regard it
4341 * as a wildcard scope, which matches any scope zone ID.
4343 if (spidx0
->src
.sin6
.sin6_scope_id
&&
4344 spidx1
->src
.sin6
.sin6_scope_id
&&
4345 spidx0
->dst
.sin6
.sin6_scope_id
!= spidx1
->dst
.sin6
.sin6_scope_id
)
4347 if (!key_bbcmp(&spidx0
->dst
.sin6
.sin6_addr
,
4348 &spidx1
->dst
.sin6
.sin6_addr
, spidx0
->prefd
))
4353 if (memcmp(&spidx0
->dst
, &spidx1
->dst
, spidx0
->dst
.sa
.sa_len
) != 0)
4358 /* XXX Do we check other field ? e.g. flowinfo */
4363 /* returns 0 on match */
4366 const struct sockaddr
*sa1
,
4367 const struct sockaddr
*sa2
,
4373 #define satosin(s) ((const struct sockaddr_in *)s)
4377 #define satosin6(s) ((const struct sockaddr_in6 *)s)
4378 if (sa1
->sa_family
!= sa2
->sa_family
|| sa1
->sa_len
!= sa2
->sa_len
)
4381 switch (sa1
->sa_family
) {
4383 if (sa1
->sa_len
!= sizeof(struct sockaddr_in
))
4385 if (satosin(sa1
)->sin_addr
.s_addr
!=
4386 satosin(sa2
)->sin_addr
.s_addr
) {
4389 if (port
&& satosin(sa1
)->sin_port
!= satosin(sa2
)->sin_port
)
4393 if (sa1
->sa_len
!= sizeof(struct sockaddr_in6
))
4394 return 1; /*EINVAL*/
4395 if (satosin6(sa1
)->sin6_scope_id
!=
4396 satosin6(sa2
)->sin6_scope_id
) {
4399 if (!IN6_ARE_ADDR_EQUAL(&satosin6(sa1
)->sin6_addr
,
4400 &satosin6(sa2
)->sin6_addr
)) {
4404 satosin6(sa1
)->sin6_port
!= satosin6(sa2
)->sin6_port
) {
4409 if (memcmp(sa1
, sa2
, sa1
->sa_len
) != 0)
4420 * compare two buffers with mask.
4424 * bits: Number of bits to compare
4430 key_bbcmp(const void *a1
, const void *a2
, u_int bits
)
4432 const unsigned char *p1
= a1
;
4433 const unsigned char *p2
= a2
;
4435 /* XXX: This could be considerably faster if we compare a word
4436 * at a time, but it is complicated on LSB Endian machines */
4438 /* Handle null pointers */
4439 if (p1
== NULL
|| p2
== NULL
)
4449 u_int8_t mask
= ~((1<<(8-bits
))-1);
4450 if ((*p1
& mask
) != (*p2
& mask
))
4453 return 1; /* Match! */
4458 * scanning SPD and SAD to check status for each entries,
4459 * and do to remove or to expire.
4462 key_timehandler(void* arg
)
4466 time_t now
= time_second
;
4468 s
= splsoftnet(); /*called from softclock()*/
4469 mutex_enter(softnet_lock
);
4473 struct secpolicy
*sp
, *nextsp
;
4475 for (dir
= 0; dir
< IPSEC_DIR_MAX
; dir
++) {
4476 for (sp
= LIST_FIRST(&sptree
[dir
]);
4480 nextsp
= LIST_NEXT(sp
, chain
);
4482 if (sp
->state
== IPSEC_SPSTATE_DEAD
) {
4483 key_sp_unlink(sp
); /*XXX*/
4485 /* 'sp' dead; continue transfers to
4491 if (sp
->lifetime
== 0 && sp
->validtime
== 0)
4494 /* the deletion will occur next time */
4495 if ((sp
->lifetime
&& now
- sp
->created
> sp
->lifetime
)
4496 || (sp
->validtime
&& now
- sp
->lastused
> sp
->validtime
)) {
4507 struct secashead
*sah
, *nextsah
;
4508 struct secasvar
*sav
, *nextsav
;
4510 for (sah
= LIST_FIRST(&sahtree
);
4514 nextsah
= LIST_NEXT(sah
, chain
);
4516 /* if sah has been dead, then delete it and process next sah. */
4517 if (sah
->state
== SADB_SASTATE_DEAD
) {
4522 /* if LARVAL entry doesn't become MATURE, delete it. */
4523 for (sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_LARVAL
]);
4527 nextsav
= LIST_NEXT(sav
, chain
);
4529 if (now
- sav
->created
> key_larval_lifetime
) {
4535 * check MATURE entry to start to send expire message
4538 for (sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_MATURE
]);
4542 nextsav
= LIST_NEXT(sav
, chain
);
4544 /* we don't need to check. */
4545 if (sav
->lft_s
== NULL
)
4549 if (sav
->lft_c
== NULL
) {
4550 ipseclog((LOG_DEBUG
,"key_timehandler: "
4551 "There is no CURRENT time, why?\n"));
4555 /* check SOFT lifetime */
4556 if (sav
->lft_s
->sadb_lifetime_addtime
!= 0
4557 && now
- sav
->created
> sav
->lft_s
->sadb_lifetime_addtime
) {
4559 * check SA to be used whether or not.
4560 * when SA hasn't been used, delete it.
4562 if (sav
->lft_c
->sadb_lifetime_usetime
== 0) {
4563 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
4566 key_sa_chgstate(sav
, SADB_SASTATE_DYING
);
4568 * XXX If we keep to send expire
4569 * message in the status of
4570 * DYING. Do remove below code.
4575 /* check SOFT lifetime by bytes */
4577 * XXX I don't know the way to delete this SA
4578 * when new SA is installed. Caution when it's
4579 * installed too big lifetime by time.
4581 else if (sav
->lft_s
->sadb_lifetime_bytes
!= 0
4582 && sav
->lft_s
->sadb_lifetime_bytes
< sav
->lft_c
->sadb_lifetime_bytes
) {
4584 key_sa_chgstate(sav
, SADB_SASTATE_DYING
);
4586 * XXX If we keep to send expire
4587 * message in the status of
4588 * DYING. Do remove below code.
4594 /* check DYING entry to change status to DEAD. */
4595 for (sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_DYING
]);
4599 nextsav
= LIST_NEXT(sav
, chain
);
4601 /* we don't need to check. */
4602 if (sav
->lft_h
== NULL
)
4606 if (sav
->lft_c
== NULL
) {
4607 ipseclog((LOG_DEBUG
, "key_timehandler: "
4608 "There is no CURRENT time, why?\n"));
4612 if (sav
->lft_h
->sadb_lifetime_addtime
!= 0
4613 && now
- sav
->created
> sav
->lft_h
->sadb_lifetime_addtime
) {
4614 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
4617 #if 0 /* XXX Should we keep to send expire message until HARD lifetime ? */
4618 else if (sav
->lft_s
!= NULL
4619 && sav
->lft_s
->sadb_lifetime_addtime
!= 0
4620 && now
- sav
->created
> sav
->lft_s
->sadb_lifetime_addtime
) {
4622 * XXX: should be checked to be
4623 * installed the valid SA.
4627 * If there is no SA then sending
4633 /* check HARD lifetime by bytes */
4634 else if (sav
->lft_h
->sadb_lifetime_bytes
!= 0
4635 && sav
->lft_h
->sadb_lifetime_bytes
< sav
->lft_c
->sadb_lifetime_bytes
) {
4636 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
4641 /* delete entry in DEAD */
4642 for (sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_DEAD
]);
4646 nextsav
= LIST_NEXT(sav
, chain
);
4649 if (sav
->state
!= SADB_SASTATE_DEAD
) {
4650 ipseclog((LOG_DEBUG
, "key_timehandler: "
4651 "invalid sav->state "
4652 "(queue: %d SA: %d): "
4654 SADB_SASTATE_DEAD
, sav
->state
));
4658 * do not call key_freesav() here.
4659 * sav should already be freed, and sav->refcnt
4660 * shows other references to sav
4661 * (such as from SPD).
4667 #ifndef IPSEC_NONBLOCK_ACQUIRE
4670 struct secacq
*acq
, *nextacq
;
4672 for (acq
= LIST_FIRST(&acqtree
);
4676 nextacq
= LIST_NEXT(acq
, chain
);
4678 if (now
- acq
->created
> key_blockacq_lifetime
4679 && __LIST_CHAINED(acq
)) {
4680 LIST_REMOVE(acq
, chain
);
4689 struct secspacq
*acq
, *nextacq
;
4691 for (acq
= LIST_FIRST(&spacqtree
);
4695 nextacq
= LIST_NEXT(acq
, chain
);
4697 if (now
- acq
->created
> key_blockacq_lifetime
4698 && __LIST_CHAINED(acq
)) {
4699 LIST_REMOVE(acq
, chain
);
4705 /* initialize random seed */
4706 if (key_tick_init_random
++ > key_int_random
) {
4707 key_tick_init_random
= 0;
4711 #ifndef IPSEC_DEBUG2
4712 /* do exchange to tick time !! */
4713 callout_reset(&key_timehandler_ch
, hz
, key_timehandler
, NULL
);
4714 #endif /* IPSEC_DEBUG2 */
4716 mutex_exit(softnet_lock
);
4723 void srandom(int arg
) {return;}
4727 * to initialize a seed for random()
4732 srandom(time_second
);
4740 key_randomfill(&value
, sizeof(value
));
4745 key_randomfill(void *p
, size_t l
)
4749 static int warn
= 1;
4752 n
= (size_t)read_random(p
, (u_int
)l
);
4756 memcpy((u_int8_t
*)p
+ n
, &v
,
4757 l
- n
< sizeof(v
) ? l
- n
: sizeof(v
));
4761 printf("WARNING: pseudo-random number generator "
4762 "used for IPsec processing\n");
4769 * map SADB_SATYPE_* to IPPROTO_*.
4770 * if satype == SADB_SATYPE then satype is mapped to ~0.
4772 * 0: invalid satype.
4775 key_satype2proto(u_int8_t satype
)
4778 case SADB_SATYPE_UNSPEC
:
4779 return IPSEC_PROTO_ANY
;
4780 case SADB_SATYPE_AH
:
4782 case SADB_SATYPE_ESP
:
4784 case SADB_X_SATYPE_IPCOMP
:
4785 return IPPROTO_IPCOMP
;
4786 case SADB_X_SATYPE_TCPSIGNATURE
:
4795 * map IPPROTO_* to SADB_SATYPE_*
4797 * 0: invalid protocol type.
4800 key_proto2satype(u_int16_t proto
)
4804 return SADB_SATYPE_AH
;
4806 return SADB_SATYPE_ESP
;
4807 case IPPROTO_IPCOMP
:
4808 return SADB_X_SATYPE_IPCOMP
;
4810 return SADB_X_SATYPE_TCPSIGNATURE
;
4818 key_setsecasidx(int proto
, int mode
, int reqid
,
4819 const struct sadb_address
* src
,
4820 const struct sadb_address
* dst
,
4821 struct secasindex
* saidx
)
4823 const union sockaddr_union
* src_u
=
4824 (const union sockaddr_union
*) src
;
4825 const union sockaddr_union
* dst_u
=
4826 (const union sockaddr_union
*) dst
;
4828 /* sa len safety check */
4829 if (key_checksalen(src_u
) != 0)
4831 if (key_checksalen(dst_u
) != 0)
4834 memset(saidx
, 0, sizeof(*saidx
));
4835 saidx
->proto
= proto
;
4837 saidx
->reqid
= reqid
;
4838 memcpy(&saidx
->src
, src_u
, src_u
->sa
.sa_len
);
4839 memcpy(&saidx
->dst
, dst_u
, dst_u
->sa
.sa_len
);
4842 key_porttosaddr(&((saidx
)->src
),0);
4843 key_porttosaddr(&((saidx
)->dst
),0);
4850 * SADB_GETSPI processing is to receive
4851 * <base, (SA2), src address, dst address, (SPI range)>
4852 * from the IKMPd, to assign a unique spi value, to hang on the INBOUND
4853 * tree with the status of LARVAL, and send
4854 * <base, SA(*), address(SD)>
4857 * IN: mhp: pointer to the pointer to each header.
4858 * OUT: NULL if fail.
4859 * other if success, return pointer to the message to send.
4862 key_getspi(struct socket
*so
, struct mbuf
*m
,
4863 const struct sadb_msghdr
*mhp
)
4865 struct sadb_address
*src0
, *dst0
;
4866 struct secasindex saidx
;
4867 struct secashead
*newsah
;
4868 struct secasvar
*newsav
;
4876 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
4877 panic("key_getspi: NULL pointer is passed");
4879 if (mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
4880 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
) {
4881 ipseclog((LOG_DEBUG
, "key_getspi: invalid message is passed.\n"));
4882 return key_senderror(so
, m
, EINVAL
);
4884 if (mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
4885 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
4886 ipseclog((LOG_DEBUG
, "key_getspi: invalid message is passed.\n"));
4887 return key_senderror(so
, m
, EINVAL
);
4889 if (mhp
->ext
[SADB_X_EXT_SA2
] != NULL
) {
4890 mode
= ((struct sadb_x_sa2
*)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_mode
;
4891 reqid
= ((struct sadb_x_sa2
*)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_reqid
;
4893 mode
= IPSEC_MODE_ANY
;
4897 src0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_SRC
]);
4898 dst0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_DST
]);
4900 /* map satype to proto */
4901 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
4902 ipseclog((LOG_DEBUG
, "key_getspi: invalid satype is passed.\n"));
4903 return key_senderror(so
, m
, EINVAL
);
4907 if ((error
= key_setsecasidx(proto
, mode
, reqid
, src0
+ 1,
4908 dst0
+ 1, &saidx
)) != 0)
4909 return key_senderror(so
, m
, EINVAL
);
4911 /* SPI allocation */
4912 spi
= key_do_getnewspi((struct sadb_spirange
*)mhp
->ext
[SADB_EXT_SPIRANGE
],
4915 return key_senderror(so
, m
, EINVAL
);
4917 /* get a SA index */
4918 if ((newsah
= key_getsah(&saidx
)) == NULL
) {
4919 /* create a new SA index */
4920 if ((newsah
= key_newsah(&saidx
)) == NULL
) {
4921 ipseclog((LOG_DEBUG
, "key_getspi: No more memory.\n"));
4922 return key_senderror(so
, m
, ENOBUFS
);
4928 newsav
= KEY_NEWSAV(m
, mhp
, newsah
, &error
);
4929 if (newsav
== NULL
) {
4930 /* XXX don't free new SA index allocated in above. */
4931 return key_senderror(so
, m
, error
);
4935 newsav
->spi
= htonl(spi
);
4937 #ifndef IPSEC_NONBLOCK_ACQUIRE
4938 /* delete the entry in acqtree */
4939 if (mhp
->msg
->sadb_msg_seq
!= 0) {
4941 if ((acq
= key_getacqbyseq(mhp
->msg
->sadb_msg_seq
)) != NULL
) {
4942 /* reset counter in order to deletion by timehandler. */
4943 acq
->created
= time_second
;
4950 struct mbuf
*n
, *nn
;
4951 struct sadb_sa
*m_sa
;
4952 struct sadb_msg
*newmsg
;
4955 /* create new sadb_msg to reply. */
4956 len
= PFKEY_ALIGN8(sizeof(struct sadb_msg
)) +
4957 PFKEY_ALIGN8(sizeof(struct sadb_sa
));
4959 return key_senderror(so
, m
, ENOBUFS
);
4961 MGETHDR(n
, M_DONTWAIT
, MT_DATA
);
4963 MCLGET(n
, M_DONTWAIT
);
4964 if ((n
->m_flags
& M_EXT
) == 0) {
4970 return key_senderror(so
, m
, ENOBUFS
);
4976 m_copydata(m
, 0, sizeof(struct sadb_msg
), mtod(n
, char *) + off
);
4977 off
+= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
4979 m_sa
= (struct sadb_sa
*)(mtod(n
, char *) + off
);
4980 m_sa
->sadb_sa_len
= PFKEY_UNIT64(sizeof(struct sadb_sa
));
4981 m_sa
->sadb_sa_exttype
= SADB_EXT_SA
;
4982 m_sa
->sadb_sa_spi
= htonl(spi
);
4983 off
+= PFKEY_ALIGN8(sizeof(struct sadb_sa
));
4987 panic("length inconsistency in key_getspi");
4990 n
->m_next
= key_gather_mbuf(m
, mhp
, 0, 2, SADB_EXT_ADDRESS_SRC
,
4991 SADB_EXT_ADDRESS_DST
);
4994 return key_senderror(so
, m
, ENOBUFS
);
4997 if (n
->m_len
< sizeof(struct sadb_msg
)) {
4998 n
= m_pullup(n
, sizeof(struct sadb_msg
));
5000 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ONE
);
5003 n
->m_pkthdr
.len
= 0;
5004 for (nn
= n
; nn
; nn
= nn
->m_next
)
5005 n
->m_pkthdr
.len
+= nn
->m_len
;
5007 newmsg
= mtod(n
, struct sadb_msg
*);
5008 newmsg
->sadb_msg_seq
= newsav
->seq
;
5009 newmsg
->sadb_msg_errno
= 0;
5010 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
5013 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ONE
);
5018 * allocating new SPI
5019 * called by key_getspi().
5025 key_do_getnewspi(struct sadb_spirange
*spirange
,
5026 struct secasindex
*saidx
)
5029 u_int32_t spmin
, spmax
;
5030 int count
= key_spi_trycnt
;
5032 /* set spi range to allocate */
5033 if (spirange
!= NULL
) {
5034 spmin
= spirange
->sadb_spirange_min
;
5035 spmax
= spirange
->sadb_spirange_max
;
5037 spmin
= key_spi_minval
;
5038 spmax
= key_spi_maxval
;
5040 /* IPCOMP needs 2-byte SPI */
5041 if (saidx
->proto
== IPPROTO_IPCOMP
) {
5043 if (spmin
>= 0x10000)
5045 if (spmax
>= 0x10000)
5047 if (spmin
> spmax
) {
5048 t
= spmin
; spmin
= spmax
; spmax
= t
;
5052 if (spmin
== spmax
) {
5053 if (key_checkspidup(saidx
, htonl(spmin
)) != NULL
) {
5054 ipseclog((LOG_DEBUG
, "key_do_getnewspi: SPI %u exists already.\n", spmin
));
5058 count
--; /* taking one cost. */
5066 /* when requesting to allocate spi ranged */
5068 /* generate pseudo-random SPI value ranged. */
5069 newspi
= spmin
+ (key_random() % (spmax
- spmin
+ 1));
5071 if (key_checkspidup(saidx
, htonl(newspi
)) == NULL
)
5075 if (count
== 0 || newspi
== 0) {
5076 ipseclog((LOG_DEBUG
, "key_do_getnewspi: to allocate spi is failed.\n"));
5082 keystat
.getspi_count
=
5083 (keystat
.getspi_count
+ key_spi_trycnt
- count
) / 2;
5089 /* Handle IPSEC_NAT_T info if present */
5091 key_handle_natt_info(struct secasvar
*sav
,
5092 const struct sadb_msghdr
*mhp
)
5095 if (mhp
->ext
[SADB_X_EXT_NAT_T_OA
] != NULL
)
5096 printf("update: NAT-T OA present\n");
5098 if ((mhp
->ext
[SADB_X_EXT_NAT_T_TYPE
] != NULL
) &&
5099 (mhp
->ext
[SADB_X_EXT_NAT_T_SPORT
] != NULL
) &&
5100 (mhp
->ext
[SADB_X_EXT_NAT_T_DPORT
] != NULL
)) {
5101 struct sadb_x_nat_t_type
*type
;
5102 struct sadb_x_nat_t_port
*sport
;
5103 struct sadb_x_nat_t_port
*dport
;
5104 struct sadb_address
*addr
;
5105 struct sadb_x_nat_t_frag
*frag
;
5107 if ((mhp
->extlen
[SADB_X_EXT_NAT_T_TYPE
] < sizeof(*type
)) ||
5108 (mhp
->extlen
[SADB_X_EXT_NAT_T_SPORT
] < sizeof(*sport
)) ||
5109 (mhp
->extlen
[SADB_X_EXT_NAT_T_DPORT
] < sizeof(*dport
))) {
5110 ipseclog((LOG_DEBUG
, "key_update: "
5111 "invalid message.\n"));
5115 if ((mhp
->ext
[SADB_X_EXT_NAT_T_OA
] != NULL
) &&
5116 (mhp
->extlen
[SADB_X_EXT_NAT_T_OA
] < sizeof(*addr
))) {
5117 ipseclog((LOG_DEBUG
, "key_update: invalid message\n"));
5121 if ((mhp
->ext
[SADB_X_EXT_NAT_T_FRAG
] != NULL
) &&
5122 (mhp
->extlen
[SADB_X_EXT_NAT_T_FRAG
] < sizeof(*frag
))) {
5123 ipseclog((LOG_DEBUG
, "key_update: invalid message\n"));
5127 type
= (struct sadb_x_nat_t_type
*)
5128 mhp
->ext
[SADB_X_EXT_NAT_T_TYPE
];
5129 sport
= (struct sadb_x_nat_t_port
*)
5130 mhp
->ext
[SADB_X_EXT_NAT_T_SPORT
];
5131 dport
= (struct sadb_x_nat_t_port
*)
5132 mhp
->ext
[SADB_X_EXT_NAT_T_DPORT
];
5133 addr
= (struct sadb_address
*)
5134 mhp
->ext
[SADB_X_EXT_NAT_T_OA
];
5135 frag
= (struct sadb_x_nat_t_frag
*)
5136 mhp
->ext
[SADB_X_EXT_NAT_T_FRAG
];
5139 sav
->natt_type
= type
->sadb_x_nat_t_type_type
;
5141 key_porttosaddr(&sav
->sah
->saidx
.src
,
5142 sport
->sadb_x_nat_t_port_port
);
5144 key_porttosaddr(&sav
->sah
->saidx
.dst
,
5145 dport
->sadb_x_nat_t_port_port
);
5147 sav
->esp_frag
= frag
->sadb_x_nat_t_frag_fraglen
;
5149 sav
->esp_frag
= IP_MAXPACKET
;
5158 * SADB_UPDATE processing
5160 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
5161 * key(AE), (identity(SD),) (sensitivity)>
5162 * from the ikmpd, and update a secasvar entry whose status is SADB_SASTATE_LARVAL.
5164 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
5165 * (identity(SD),) (sensitivity)>
5168 * m will always be freed.
5171 key_update(struct socket
*so
, struct mbuf
*m
, const struct sadb_msghdr
*mhp
)
5173 struct sadb_sa
*sa0
;
5174 struct sadb_address
*src0
, *dst0
;
5175 struct secasindex saidx
;
5176 struct secashead
*sah
;
5177 struct secasvar
*sav
;
5184 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
5185 panic("key_update: NULL pointer is passed");
5187 /* map satype to proto */
5188 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
5189 ipseclog((LOG_DEBUG
, "key_update: invalid satype is passed.\n"));
5190 return key_senderror(so
, m
, EINVAL
);
5193 if (mhp
->ext
[SADB_EXT_SA
] == NULL
||
5194 mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
5195 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
||
5196 (mhp
->msg
->sadb_msg_satype
== SADB_SATYPE_ESP
&&
5197 mhp
->ext
[SADB_EXT_KEY_ENCRYPT
] == NULL
) ||
5198 (mhp
->msg
->sadb_msg_satype
== SADB_SATYPE_AH
&&
5199 mhp
->ext
[SADB_EXT_KEY_AUTH
] == NULL
) ||
5200 (mhp
->ext
[SADB_EXT_LIFETIME_HARD
] != NULL
&&
5201 mhp
->ext
[SADB_EXT_LIFETIME_SOFT
] == NULL
) ||
5202 (mhp
->ext
[SADB_EXT_LIFETIME_HARD
] == NULL
&&
5203 mhp
->ext
[SADB_EXT_LIFETIME_SOFT
] != NULL
)) {
5204 ipseclog((LOG_DEBUG
, "key_update: invalid message is passed.\n"));
5205 return key_senderror(so
, m
, EINVAL
);
5207 if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(struct sadb_sa
) ||
5208 mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
5209 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
5210 ipseclog((LOG_DEBUG
, "key_update: invalid message is passed.\n"));
5211 return key_senderror(so
, m
, EINVAL
);
5213 if (mhp
->ext
[SADB_X_EXT_SA2
] != NULL
) {
5214 mode
= ((struct sadb_x_sa2
*)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_mode
;
5215 reqid
= ((struct sadb_x_sa2
*)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_reqid
;
5217 mode
= IPSEC_MODE_ANY
;
5220 /* XXX boundary checking for other extensions */
5222 sa0
= (struct sadb_sa
*)mhp
->ext
[SADB_EXT_SA
];
5223 src0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_SRC
]);
5224 dst0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_DST
]);
5226 if ((error
= key_setsecasidx(proto
, mode
, reqid
, src0
+ 1,
5227 dst0
+ 1, &saidx
)) != 0)
5228 return key_senderror(so
, m
, EINVAL
);
5231 /* get a SA header */
5232 if ((sah
= key_getsah(&saidx
)) == NULL
) {
5233 ipseclog((LOG_DEBUG
, "key_update: no SA index found.\n"));
5234 return key_senderror(so
, m
, ENOENT
);
5237 /* set spidx if there */
5239 error
= key_setident(sah
, m
, mhp
);
5241 return key_senderror(so
, m
, error
);
5243 /* find a SA with sequence number. */
5244 #ifdef IPSEC_DOSEQCHECK
5245 if (mhp
->msg
->sadb_msg_seq
!= 0
5246 && (sav
= key_getsavbyseq(sah
, mhp
->msg
->sadb_msg_seq
)) == NULL
) {
5247 ipseclog((LOG_DEBUG
,
5248 "key_update: no larval SA with sequence %u exists.\n",
5249 mhp
->msg
->sadb_msg_seq
));
5250 return key_senderror(so
, m
, ENOENT
);
5253 if ((sav
= key_getsavbyspi(sah
, sa0
->sadb_sa_spi
)) == NULL
) {
5254 ipseclog((LOG_DEBUG
,
5255 "key_update: no such a SA found (spi:%u)\n",
5256 (u_int32_t
)ntohl(sa0
->sadb_sa_spi
)));
5257 return key_senderror(so
, m
, EINVAL
);
5261 /* validity check */
5262 if (sav
->sah
->saidx
.proto
!= proto
) {
5263 ipseclog((LOG_DEBUG
,
5264 "key_update: protocol mismatched (DB=%u param=%u)\n",
5265 sav
->sah
->saidx
.proto
, proto
));
5266 return key_senderror(so
, m
, EINVAL
);
5268 #ifdef IPSEC_DOSEQCHECK
5269 if (sav
->spi
!= sa0
->sadb_sa_spi
) {
5270 ipseclog((LOG_DEBUG
,
5271 "key_update: SPI mismatched (DB:%u param:%u)\n",
5272 (u_int32_t
)ntohl(sav
->spi
),
5273 (u_int32_t
)ntohl(sa0
->sadb_sa_spi
)));
5274 return key_senderror(so
, m
, EINVAL
);
5277 if (sav
->pid
!= mhp
->msg
->sadb_msg_pid
) {
5278 ipseclog((LOG_DEBUG
,
5279 "key_update: pid mismatched (DB:%u param:%u)\n",
5280 sav
->pid
, mhp
->msg
->sadb_msg_pid
));
5281 return key_senderror(so
, m
, EINVAL
);
5284 /* copy sav values */
5285 error
= key_setsaval(sav
, m
, mhp
);
5288 return key_senderror(so
, m
, error
);
5291 /* check SA values to be mature. */
5292 if ((mhp
->msg
->sadb_msg_errno
= key_mature(sav
)) != 0) {
5294 return key_senderror(so
, m
, 0);
5298 if ((error
= key_handle_natt_info(sav
,mhp
)) != 0)
5299 return key_senderror(so
, m
, EINVAL
);
5300 #endif /* IPSEC_NAT_T */
5305 /* set msg buf from mhp */
5306 n
= key_getmsgbuf_x1(m
, mhp
);
5308 ipseclog((LOG_DEBUG
, "key_update: No more memory.\n"));
5309 return key_senderror(so
, m
, ENOBUFS
);
5313 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
5318 * search SAD with sequence for a SA which state is SADB_SASTATE_LARVAL.
5319 * only called by key_update().
5322 * others : found, pointer to a SA.
5324 #ifdef IPSEC_DOSEQCHECK
5325 static struct secasvar
*
5326 key_getsavbyseq(struct secashead
*sah
, u_int32_t seq
)
5328 struct secasvar
*sav
;
5331 state
= SADB_SASTATE_LARVAL
;
5333 /* search SAD with sequence number ? */
5334 LIST_FOREACH(sav
, &sah
->savtree
[state
], chain
) {
5336 KEY_CHKSASTATE(state
, sav
->state
, "key_getsabyseq");
5338 if (sav
->seq
== seq
) {
5340 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
5341 printf("DP key_getsavbyseq cause "
5342 "refcnt++:%d SA:%p\n",
5353 * SADB_ADD processing
5354 * add an entry to SA database, when received
5355 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
5356 * key(AE), (identity(SD),) (sensitivity)>
5359 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
5360 * (identity(SD),) (sensitivity)>
5363 * IGNORE identity and sensitivity messages.
5365 * m will always be freed.
5368 key_add(struct socket
*so
, struct mbuf
*m
,
5369 const struct sadb_msghdr
*mhp
)
5371 struct sadb_sa
*sa0
;
5372 struct sadb_address
*src0
, *dst0
;
5373 struct secasindex saidx
;
5374 struct secashead
*newsah
;
5375 struct secasvar
*newsav
;
5382 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
5383 panic("key_add: NULL pointer is passed");
5385 /* map satype to proto */
5386 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
5387 ipseclog((LOG_DEBUG
, "key_add: invalid satype is passed.\n"));
5388 return key_senderror(so
, m
, EINVAL
);
5391 if (mhp
->ext
[SADB_EXT_SA
] == NULL
||
5392 mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
5393 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
||
5394 (mhp
->msg
->sadb_msg_satype
== SADB_SATYPE_ESP
&&
5395 mhp
->ext
[SADB_EXT_KEY_ENCRYPT
] == NULL
) ||
5396 (mhp
->msg
->sadb_msg_satype
== SADB_SATYPE_AH
&&
5397 mhp
->ext
[SADB_EXT_KEY_AUTH
] == NULL
) ||
5398 (mhp
->ext
[SADB_EXT_LIFETIME_HARD
] != NULL
&&
5399 mhp
->ext
[SADB_EXT_LIFETIME_SOFT
] == NULL
) ||
5400 (mhp
->ext
[SADB_EXT_LIFETIME_HARD
] == NULL
&&
5401 mhp
->ext
[SADB_EXT_LIFETIME_SOFT
] != NULL
)) {
5402 ipseclog((LOG_DEBUG
, "key_add: invalid message is passed.\n"));
5403 return key_senderror(so
, m
, EINVAL
);
5405 if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(struct sadb_sa
) ||
5406 mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
5407 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
5409 ipseclog((LOG_DEBUG
, "key_add: invalid message is passed.\n"));
5410 return key_senderror(so
, m
, EINVAL
);
5412 if (mhp
->ext
[SADB_X_EXT_SA2
] != NULL
) {
5413 mode
= ((struct sadb_x_sa2
*)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_mode
;
5414 reqid
= ((struct sadb_x_sa2
*)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_reqid
;
5416 mode
= IPSEC_MODE_ANY
;
5420 sa0
= (struct sadb_sa
*)mhp
->ext
[SADB_EXT_SA
];
5421 src0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_SRC
];
5422 dst0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_DST
];
5424 if ((error
= key_setsecasidx(proto
, mode
, reqid
, src0
+ 1,
5425 dst0
+ 1, &saidx
)) != 0)
5426 return key_senderror(so
, m
, EINVAL
);
5428 /* get a SA header */
5429 if ((newsah
= key_getsah(&saidx
)) == NULL
) {
5430 /* create a new SA header */
5431 if ((newsah
= key_newsah(&saidx
)) == NULL
) {
5432 ipseclog((LOG_DEBUG
, "key_add: No more memory.\n"));
5433 return key_senderror(so
, m
, ENOBUFS
);
5437 /* set spidx if there */
5439 error
= key_setident(newsah
, m
, mhp
);
5441 return key_senderror(so
, m
, error
);
5444 /* create new SA entry. */
5445 /* We can create new SA only if SPI is differenct. */
5446 if (key_getsavbyspi(newsah
, sa0
->sadb_sa_spi
)) {
5447 ipseclog((LOG_DEBUG
, "key_add: SA already exists.\n"));
5448 return key_senderror(so
, m
, EEXIST
);
5450 newsav
= KEY_NEWSAV(m
, mhp
, newsah
, &error
);
5451 if (newsav
== NULL
) {
5452 return key_senderror(so
, m
, error
);
5455 /* check SA values to be mature. */
5456 if ((error
= key_mature(newsav
)) != 0) {
5457 KEY_FREESAV(&newsav
);
5458 return key_senderror(so
, m
, error
);
5462 if ((error
= key_handle_natt_info(newsav
, mhp
)) != 0)
5463 return key_senderror(so
, m
, EINVAL
);
5464 #endif /* IPSEC_NAT_T */
5467 * don't call key_freesav() here, as we would like to keep the SA
5468 * in the database on success.
5474 /* set msg buf from mhp */
5475 n
= key_getmsgbuf_x1(m
, mhp
);
5477 ipseclog((LOG_DEBUG
, "key_update: No more memory.\n"));
5478 return key_senderror(so
, m
, ENOBUFS
);
5482 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
5488 key_setident(struct secashead
*sah
, struct mbuf
*m
,
5489 const struct sadb_msghdr
*mhp
)
5491 const struct sadb_ident
*idsrc
, *iddst
;
5492 int idsrclen
, iddstlen
;
5495 if (sah
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
5496 panic("key_setident: NULL pointer is passed");
5498 /* don't make buffer if not there */
5499 if (mhp
->ext
[SADB_EXT_IDENTITY_SRC
] == NULL
&&
5500 mhp
->ext
[SADB_EXT_IDENTITY_DST
] == NULL
) {
5506 if (mhp
->ext
[SADB_EXT_IDENTITY_SRC
] == NULL
||
5507 mhp
->ext
[SADB_EXT_IDENTITY_DST
] == NULL
) {
5508 ipseclog((LOG_DEBUG
, "key_setident: invalid identity.\n"));
5512 idsrc
= (const struct sadb_ident
*)mhp
->ext
[SADB_EXT_IDENTITY_SRC
];
5513 iddst
= (const struct sadb_ident
*)mhp
->ext
[SADB_EXT_IDENTITY_DST
];
5514 idsrclen
= mhp
->extlen
[SADB_EXT_IDENTITY_SRC
];
5515 iddstlen
= mhp
->extlen
[SADB_EXT_IDENTITY_DST
];
5517 /* validity check */
5518 if (idsrc
->sadb_ident_type
!= iddst
->sadb_ident_type
) {
5519 ipseclog((LOG_DEBUG
, "key_setident: ident type mismatch.\n"));
5523 switch (idsrc
->sadb_ident_type
) {
5524 case SADB_IDENTTYPE_PREFIX
:
5525 case SADB_IDENTTYPE_FQDN
:
5526 case SADB_IDENTTYPE_USERFQDN
:
5528 /* XXX do nothing */
5534 /* make structure */
5535 KMALLOC(sah
->idents
, struct sadb_ident
*, idsrclen
);
5536 if (sah
->idents
== NULL
) {
5537 ipseclog((LOG_DEBUG
, "key_setident: No more memory.\n"));
5540 KMALLOC(sah
->identd
, struct sadb_ident
*, iddstlen
);
5541 if (sah
->identd
== NULL
) {
5544 ipseclog((LOG_DEBUG
, "key_setident: No more memory.\n"));
5547 memcpy(sah
->idents
, idsrc
, idsrclen
);
5548 memcpy(sah
->identd
, iddst
, iddstlen
);
5554 * m will not be freed on return.
5555 * it is caller's responsibility to free the result.
5557 static struct mbuf
*
5558 key_getmsgbuf_x1(struct mbuf
*m
, const struct sadb_msghdr
*mhp
)
5563 if (m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
5564 panic("key_getmsgbuf_x1: NULL pointer is passed");
5566 /* create new sadb_msg to reply. */
5567 n
= key_gather_mbuf(m
, mhp
, 1, 9, SADB_EXT_RESERVED
,
5568 SADB_EXT_SA
, SADB_X_EXT_SA2
,
5569 SADB_EXT_ADDRESS_SRC
, SADB_EXT_ADDRESS_DST
,
5570 SADB_EXT_LIFETIME_HARD
, SADB_EXT_LIFETIME_SOFT
,
5571 SADB_EXT_IDENTITY_SRC
, SADB_EXT_IDENTITY_DST
);
5575 if (n
->m_len
< sizeof(struct sadb_msg
)) {
5576 n
= m_pullup(n
, sizeof(struct sadb_msg
));
5580 mtod(n
, struct sadb_msg
*)->sadb_msg_errno
= 0;
5581 mtod(n
, struct sadb_msg
*)->sadb_msg_len
=
5582 PFKEY_UNIT64(n
->m_pkthdr
.len
);
5587 static int key_delete_all (struct socket
*, struct mbuf
*,
5588 const struct sadb_msghdr
*, u_int16_t
);
5591 * SADB_DELETE processing
5593 * <base, SA(*), address(SD)>
5594 * from the ikmpd, and set SADB_SASTATE_DEAD,
5596 * <base, SA(*), address(SD)>
5599 * m will always be freed.
5602 key_delete(struct socket
*so
, struct mbuf
*m
,
5603 const struct sadb_msghdr
*mhp
)
5605 struct sadb_sa
*sa0
;
5606 struct sadb_address
*src0
, *dst0
;
5607 struct secasindex saidx
;
5608 struct secashead
*sah
;
5609 struct secasvar
*sav
= NULL
;
5614 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
5615 panic("key_delete: NULL pointer is passed");
5617 /* map satype to proto */
5618 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
5619 ipseclog((LOG_DEBUG
, "key_delete: invalid satype is passed.\n"));
5620 return key_senderror(so
, m
, EINVAL
);
5623 if (mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
5624 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
) {
5625 ipseclog((LOG_DEBUG
, "key_delete: invalid message is passed.\n"));
5626 return key_senderror(so
, m
, EINVAL
);
5629 if (mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
5630 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
5631 ipseclog((LOG_DEBUG
, "key_delete: invalid message is passed.\n"));
5632 return key_senderror(so
, m
, EINVAL
);
5635 if (mhp
->ext
[SADB_EXT_SA
] == NULL
) {
5637 * Caller wants us to delete all non-LARVAL SAs
5638 * that match the src/dst. This is used during
5639 * IKE INITIAL-CONTACT.
5641 ipseclog((LOG_DEBUG
, "key_delete: doing delete all.\n"));
5642 return key_delete_all(so
, m
, mhp
, proto
);
5643 } else if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(struct sadb_sa
)) {
5644 ipseclog((LOG_DEBUG
, "key_delete: invalid message is passed.\n"));
5645 return key_senderror(so
, m
, EINVAL
);
5648 sa0
= (struct sadb_sa
*)mhp
->ext
[SADB_EXT_SA
];
5649 src0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_SRC
]);
5650 dst0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_DST
]);
5652 if ((error
= key_setsecasidx(proto
, IPSEC_MODE_ANY
, 0, src0
+ 1,
5653 dst0
+ 1, &saidx
)) != 0)
5654 return key_senderror(so
, m
, EINVAL
);
5656 /* get a SA header */
5657 LIST_FOREACH(sah
, &sahtree
, chain
) {
5658 if (sah
->state
== SADB_SASTATE_DEAD
)
5660 if (key_cmpsaidx(&sah
->saidx
, &saidx
, CMP_HEAD
) == 0)
5663 /* get a SA with SPI. */
5664 sav
= key_getsavbyspi(sah
, sa0
->sadb_sa_spi
);
5669 ipseclog((LOG_DEBUG
, "key_delete: no SA found.\n"));
5670 return key_senderror(so
, m
, ENOENT
);
5673 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
5678 struct sadb_msg
*newmsg
;
5680 /* create new sadb_msg to reply. */
5681 n
= key_gather_mbuf(m
, mhp
, 1, 4, SADB_EXT_RESERVED
,
5682 SADB_EXT_SA
, SADB_EXT_ADDRESS_SRC
, SADB_EXT_ADDRESS_DST
);
5684 return key_senderror(so
, m
, ENOBUFS
);
5686 if (n
->m_len
< sizeof(struct sadb_msg
)) {
5687 n
= m_pullup(n
, sizeof(struct sadb_msg
));
5689 return key_senderror(so
, m
, ENOBUFS
);
5691 newmsg
= mtod(n
, struct sadb_msg
*);
5692 newmsg
->sadb_msg_errno
= 0;
5693 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
5696 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
5701 * delete all SAs for src/dst. Called from key_delete().
5704 key_delete_all(struct socket
*so
, struct mbuf
*m
,
5705 const struct sadb_msghdr
*mhp
, u_int16_t proto
)
5707 struct sadb_address
*src0
, *dst0
;
5708 struct secasindex saidx
;
5709 struct secashead
*sah
;
5710 struct secasvar
*sav
, *nextsav
;
5711 u_int stateidx
, state
;
5714 src0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_SRC
]);
5715 dst0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_DST
]);
5717 if ((error
= key_setsecasidx(proto
, IPSEC_MODE_ANY
, 0, src0
+ 1,
5718 dst0
+ 1, &saidx
)) != 0)
5719 return key_senderror(so
, m
, EINVAL
);
5721 LIST_FOREACH(sah
, &sahtree
, chain
) {
5722 if (sah
->state
== SADB_SASTATE_DEAD
)
5724 if (key_cmpsaidx(&sah
->saidx
, &saidx
, CMP_HEAD
) == 0)
5727 /* Delete all non-LARVAL SAs. */
5729 stateidx
< _ARRAYLEN(saorder_state_alive
);
5731 state
= saorder_state_alive
[stateidx
];
5732 if (state
== SADB_SASTATE_LARVAL
)
5734 for (sav
= LIST_FIRST(&sah
->savtree
[state
]);
5735 sav
!= NULL
; sav
= nextsav
) {
5736 nextsav
= LIST_NEXT(sav
, chain
);
5738 if (sav
->state
!= state
) {
5739 ipseclog((LOG_DEBUG
, "key_delete_all: "
5740 "invalid sav->state "
5741 "(queue: %d SA: %d)\n",
5742 state
, sav
->state
));
5746 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
5753 struct sadb_msg
*newmsg
;
5755 /* create new sadb_msg to reply. */
5756 n
= key_gather_mbuf(m
, mhp
, 1, 3, SADB_EXT_RESERVED
,
5757 SADB_EXT_ADDRESS_SRC
, SADB_EXT_ADDRESS_DST
);
5759 return key_senderror(so
, m
, ENOBUFS
);
5761 if (n
->m_len
< sizeof(struct sadb_msg
)) {
5762 n
= m_pullup(n
, sizeof(struct sadb_msg
));
5764 return key_senderror(so
, m
, ENOBUFS
);
5766 newmsg
= mtod(n
, struct sadb_msg
*);
5767 newmsg
->sadb_msg_errno
= 0;
5768 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
5771 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
5776 * SADB_GET processing
5778 * <base, SA(*), address(SD)>
5779 * from the ikmpd, and get a SP and a SA to respond,
5781 * <base, SA, (lifetime(HSC),) address(SD), (address(P),) key(AE),
5782 * (identity(SD),) (sensitivity)>
5785 * m will always be freed.
5788 key_get(struct socket
*so
, struct mbuf
*m
,
5789 const struct sadb_msghdr
*mhp
)
5791 struct sadb_sa
*sa0
;
5792 struct sadb_address
*src0
, *dst0
;
5793 struct secasindex saidx
;
5794 struct secashead
*sah
;
5795 struct secasvar
*sav
= NULL
;
5800 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
5801 panic("key_get: NULL pointer is passed");
5803 /* map satype to proto */
5804 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
5805 ipseclog((LOG_DEBUG
, "key_get: invalid satype is passed.\n"));
5806 return key_senderror(so
, m
, EINVAL
);
5809 if (mhp
->ext
[SADB_EXT_SA
] == NULL
||
5810 mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
5811 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
) {
5812 ipseclog((LOG_DEBUG
, "key_get: invalid message is passed.\n"));
5813 return key_senderror(so
, m
, EINVAL
);
5815 if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(struct sadb_sa
) ||
5816 mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
5817 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
5818 ipseclog((LOG_DEBUG
, "key_get: invalid message is passed.\n"));
5819 return key_senderror(so
, m
, EINVAL
);
5822 sa0
= (struct sadb_sa
*)mhp
->ext
[SADB_EXT_SA
];
5823 src0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_SRC
];
5824 dst0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_DST
];
5827 if ((error
= key_setsecasidx(proto
, IPSEC_MODE_ANY
, 0, src0
+ 1,
5828 dst0
+ 1, &saidx
)) != 0)
5829 return key_senderror(so
, m
, EINVAL
);
5831 /* get a SA header */
5832 LIST_FOREACH(sah
, &sahtree
, chain
) {
5833 if (sah
->state
== SADB_SASTATE_DEAD
)
5835 if (key_cmpsaidx(&sah
->saidx
, &saidx
, CMP_HEAD
) == 0)
5838 /* get a SA with SPI. */
5839 sav
= key_getsavbyspi(sah
, sa0
->sadb_sa_spi
);
5844 ipseclog((LOG_DEBUG
, "key_get: no SA found.\n"));
5845 return key_senderror(so
, m
, ENOENT
);
5852 /* map proto to satype */
5853 if ((satype
= key_proto2satype(sah
->saidx
.proto
)) == 0) {
5854 ipseclog((LOG_DEBUG
, "key_get: there was invalid proto in SAD.\n"));
5855 return key_senderror(so
, m
, EINVAL
);
5858 /* create new sadb_msg to reply. */
5859 n
= key_setdumpsa(sav
, SADB_GET
, satype
, mhp
->msg
->sadb_msg_seq
,
5860 mhp
->msg
->sadb_msg_pid
);
5862 return key_senderror(so
, m
, ENOBUFS
);
5865 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ONE
);
5869 /* XXX make it sysctl-configurable? */
5871 key_getcomb_setlifetime(struct sadb_comb
*comb
)
5874 comb
->sadb_comb_soft_allocations
= 1;
5875 comb
->sadb_comb_hard_allocations
= 1;
5876 comb
->sadb_comb_soft_bytes
= 0;
5877 comb
->sadb_comb_hard_bytes
= 0;
5878 comb
->sadb_comb_hard_addtime
= 86400; /* 1 day */
5879 comb
->sadb_comb_soft_addtime
= comb
->sadb_comb_soft_addtime
* 80 / 100;
5880 comb
->sadb_comb_soft_usetime
= 28800; /* 8 hours */
5881 comb
->sadb_comb_hard_usetime
= comb
->sadb_comb_hard_usetime
* 80 / 100;
5885 * XXX reorder combinations by preference
5886 * XXX no idea if the user wants ESP authentication or not
5888 static struct mbuf
*
5889 key_getcomb_esp(void)
5891 struct sadb_comb
*comb
;
5892 struct enc_xform
*algo
;
5893 struct mbuf
*result
= NULL
, *m
, *n
;
5897 const int l
= PFKEY_ALIGN8(sizeof(struct sadb_comb
));
5900 for (i
= 1; i
<= SADB_EALG_MAX
; i
++) {
5901 algo
= esp_algorithm_lookup(i
);
5905 /* discard algorithms with key size smaller than system min */
5906 if (_BITS(algo
->maxkey
) < ipsec_esp_keymin
)
5908 if (_BITS(algo
->minkey
) < ipsec_esp_keymin
)
5909 encmin
= ipsec_esp_keymin
;
5911 encmin
= _BITS(algo
->minkey
);
5914 m
= key_getcomb_ah();
5916 IPSEC_ASSERT(l
<= MLEN
,
5917 ("key_getcomb_esp: l=%u > MLEN=%lu",
5919 MGET(m
, M_DONTWAIT
, MT_DATA
);
5924 memset(mtod(m
, void *), 0, m
->m_len
);
5931 for (n
= m
; n
; n
= n
->m_next
)
5933 IPSEC_ASSERT((totlen
% l
) == 0,
5934 ("key_getcomb_esp: totlen=%u, l=%u", totlen
, l
));
5936 for (off
= 0; off
< totlen
; off
+= l
) {
5937 n
= m_pulldown(m
, off
, l
, &o
);
5939 /* m is already freed */
5942 comb
= (struct sadb_comb
*)(mtod(n
, char *) + o
);
5943 memset(comb
, 0, sizeof(*comb
));
5944 key_getcomb_setlifetime(comb
);
5945 comb
->sadb_comb_encrypt
= i
;
5946 comb
->sadb_comb_encrypt_minbits
= encmin
;
5947 comb
->sadb_comb_encrypt_maxbits
= _BITS(algo
->maxkey
);
5965 key_getsizes_ah(const struct auth_hash
*ah
, int alg
,
5966 u_int16_t
* ksmin
, u_int16_t
* ksmax
)
5968 *ksmin
= *ksmax
= ah
->keysize
;
5969 if (ah
->keysize
== 0) {
5971 * Transform takes arbitrary key size but algorithm
5972 * key size is restricted. Enforce this here.
5975 case SADB_X_AALG_MD5
: *ksmin
= *ksmax
= 16; break;
5976 case SADB_X_AALG_SHA
: *ksmin
= *ksmax
= 20; break;
5977 case SADB_X_AALG_NULL
: *ksmin
= 1; *ksmax
= 256; break;
5979 DPRINTF(("key_getsizes_ah: unknown AH algorithm %u\n",
5987 * XXX reorder combinations by preference
5989 static struct mbuf
*
5990 key_getcomb_ah(void)
5992 struct sadb_comb
*comb
;
5993 struct auth_hash
*algo
;
5995 u_int16_t minkeysize
, maxkeysize
;
5997 const int l
= PFKEY_ALIGN8(sizeof(struct sadb_comb
));
6000 for (i
= 1; i
<= SADB_AALG_MAX
; i
++) {
6002 /* we prefer HMAC algorithms, not old algorithms */
6003 if (i
!= SADB_AALG_SHA1HMAC
&& i
!= SADB_AALG_MD5HMAC
)
6006 algo
= ah_algorithm_lookup(i
);
6009 key_getsizes_ah(algo
, i
, &minkeysize
, &maxkeysize
);
6010 /* discard algorithms with key size smaller than system min */
6011 if (_BITS(minkeysize
) < ipsec_ah_keymin
)
6015 IPSEC_ASSERT(l
<= MLEN
,
6016 ("key_getcomb_ah: l=%u > MLEN=%lu",
6018 MGET(m
, M_DONTWAIT
, MT_DATA
);
6025 M_PREPEND(m
, l
, M_DONTWAIT
);
6029 comb
= mtod(m
, struct sadb_comb
*);
6030 memset(comb
, 0, sizeof(*comb
));
6031 key_getcomb_setlifetime(comb
);
6032 comb
->sadb_comb_auth
= i
;
6033 comb
->sadb_comb_auth_minbits
= _BITS(minkeysize
);
6034 comb
->sadb_comb_auth_maxbits
= _BITS(maxkeysize
);
6041 * not really an official behavior. discussed in pf_key@inner.net in Sep2000.
6042 * XXX reorder combinations by preference
6044 static struct mbuf
*
6045 key_getcomb_ipcomp(void)
6047 struct sadb_comb
*comb
;
6048 struct comp_algo
*algo
;
6051 const int l
= PFKEY_ALIGN8(sizeof(struct sadb_comb
));
6054 for (i
= 1; i
<= SADB_X_CALG_MAX
; i
++) {
6055 algo
= ipcomp_algorithm_lookup(i
);
6060 IPSEC_ASSERT(l
<= MLEN
,
6061 ("key_getcomb_ipcomp: l=%u > MLEN=%lu",
6063 MGET(m
, M_DONTWAIT
, MT_DATA
);
6070 M_PREPEND(m
, l
, M_DONTWAIT
);
6074 comb
= mtod(m
, struct sadb_comb
*);
6075 memset(comb
, 0, sizeof(*comb
));
6076 key_getcomb_setlifetime(comb
);
6077 comb
->sadb_comb_encrypt
= i
;
6078 /* what should we set into sadb_comb_*_{min,max}bits? */
6085 * XXX no way to pass mode (transport/tunnel) to userland
6086 * XXX replay checking?
6087 * XXX sysctl interface to ipsec_{ah,esp}_keymin
6089 static struct mbuf
*
6090 key_getprop(const struct secasindex
*saidx
)
6092 struct sadb_prop
*prop
;
6094 const int l
= PFKEY_ALIGN8(sizeof(struct sadb_prop
));
6097 switch (saidx
->proto
) {
6099 m
= key_getcomb_esp();
6102 m
= key_getcomb_ah();
6104 case IPPROTO_IPCOMP
:
6105 m
= key_getcomb_ipcomp();
6113 M_PREPEND(m
, l
, M_DONTWAIT
);
6118 for (n
= m
; n
; n
= n
->m_next
)
6121 prop
= mtod(m
, struct sadb_prop
*);
6122 memset(prop
, 0, sizeof(*prop
));
6123 prop
->sadb_prop_len
= PFKEY_UNIT64(totlen
);
6124 prop
->sadb_prop_exttype
= SADB_EXT_PROPOSAL
;
6125 prop
->sadb_prop_replay
= 32; /* XXX */
6131 * SADB_ACQUIRE processing called by key_checkrequest() and key_acquire2().
6133 * <base, SA, address(SD), (address(P)), x_policy,
6134 * (identity(SD),) (sensitivity,) proposal>
6135 * to KMD, and expect to receive
6136 * <base> with SADB_ACQUIRE if error occurred,
6138 * <base, src address, dst address, (SPI range)> with SADB_GETSPI
6139 * from KMD by PF_KEY.
6141 * XXX x_policy is outside of RFC2367 (KAME extension).
6142 * XXX sensitivity is not supported.
6143 * XXX for ipcomp, RFC2367 does not define how to fill in proposal.
6144 * see comment for key_getcomb_ipcomp().
6148 * others: error number
6151 key_acquire(const struct secasindex
*saidx
, struct secpolicy
*sp
)
6153 struct mbuf
*result
= NULL
, *m
;
6154 #ifndef IPSEC_NONBLOCK_ACQUIRE
6155 struct secacq
*newacq
;
6162 IPSEC_ASSERT(saidx
!= NULL
, ("key_acquire: null saidx"));
6163 satype
= key_proto2satype(saidx
->proto
);
6164 IPSEC_ASSERT(satype
!= 0,
6165 ("key_acquire: null satype, protocol %u", saidx
->proto
));
6167 #ifndef IPSEC_NONBLOCK_ACQUIRE
6169 * We never do anything about acquirng SA. There is anather
6170 * solution that kernel blocks to send SADB_ACQUIRE message until
6171 * getting something message from IKEd. In later case, to be
6172 * managed with ACQUIRING list.
6174 /* Get an entry to check whether sending message or not. */
6175 if ((newacq
= key_getacq(saidx
)) != NULL
) {
6176 if (key_blockacq_count
< newacq
->count
) {
6177 /* reset counter and do send message. */
6180 /* increment counter and do nothing. */
6185 /* make new entry for blocking to send SADB_ACQUIRE. */
6186 if ((newacq
= key_newacq(saidx
)) == NULL
)
6189 /* add to acqtree */
6190 LIST_INSERT_HEAD(&acqtree
, newacq
, chain
);
6195 #ifndef IPSEC_NONBLOCK_ACQUIRE
6198 seq
= (acq_seq
= (acq_seq
== ~0 ? 1 : ++acq_seq
));
6200 m
= key_setsadbmsg(SADB_ACQUIRE
, 0, satype
, seq
, 0, 0);
6207 /* set sadb_address for saidx's. */
6208 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
6209 &saidx
->src
.sa
, FULLMASK
, IPSEC_ULPROTO_ANY
);
6216 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
6217 &saidx
->dst
.sa
, FULLMASK
, IPSEC_ULPROTO_ANY
);
6224 /* XXX proxy address (optional) */
6226 /* set sadb_x_policy */
6228 m
= key_setsadbxpolicy(sp
->policy
, sp
->spidx
.dir
, sp
->id
);
6236 /* XXX identity (optional) */
6238 if (idexttype
&& fqdn
) {
6239 /* create identity extension (FQDN) */
6240 struct sadb_ident
*id
;
6243 fqdnlen
= strlen(fqdn
) + 1; /* +1 for terminating-NUL */
6244 id
= (struct sadb_ident
*)p
;
6245 memset(id
, 0, sizeof(*id
) + PFKEY_ALIGN8(fqdnlen
));
6246 id
->sadb_ident_len
= PFKEY_UNIT64(sizeof(*id
) + PFKEY_ALIGN8(fqdnlen
));
6247 id
->sadb_ident_exttype
= idexttype
;
6248 id
->sadb_ident_type
= SADB_IDENTTYPE_FQDN
;
6249 memcpy(id
+ 1, fqdn
, fqdnlen
);
6250 p
+= sizeof(struct sadb_ident
) + PFKEY_ALIGN8(fqdnlen
);
6254 /* create identity extension (USERFQDN) */
6255 struct sadb_ident
*id
;
6259 /* +1 for terminating-NUL */
6260 userfqdnlen
= strlen(userfqdn
) + 1;
6263 id
= (struct sadb_ident
*)p
;
6264 memset(id
, 0, sizeof(*id
) + PFKEY_ALIGN8(userfqdnlen
));
6265 id
->sadb_ident_len
= PFKEY_UNIT64(sizeof(*id
) + PFKEY_ALIGN8(userfqdnlen
));
6266 id
->sadb_ident_exttype
= idexttype
;
6267 id
->sadb_ident_type
= SADB_IDENTTYPE_USERFQDN
;
6268 /* XXX is it correct? */
6270 id
->sadb_ident_id
= kauth_cred_getuid(curlwp
->l_cred
);
6271 if (userfqdn
&& userfqdnlen
)
6272 memcpy(id
+ 1, userfqdn
, userfqdnlen
);
6273 p
+= sizeof(struct sadb_ident
) + PFKEY_ALIGN8(userfqdnlen
);
6277 /* XXX sensitivity (optional) */
6279 /* create proposal/combination extension */
6280 m
= key_getprop(saidx
);
6283 * spec conformant: always attach proposal/combination extension,
6284 * the problem is that we have no way to attach it for ipcomp,
6285 * due to the way sadb_comb is declared in RFC2367.
6294 * outside of spec; make proposal/combination extension optional.
6300 if ((result
->m_flags
& M_PKTHDR
) == 0) {
6305 if (result
->m_len
< sizeof(struct sadb_msg
)) {
6306 result
= m_pullup(result
, sizeof(struct sadb_msg
));
6307 if (result
== NULL
) {
6313 result
->m_pkthdr
.len
= 0;
6314 for (m
= result
; m
; m
= m
->m_next
)
6315 result
->m_pkthdr
.len
+= m
->m_len
;
6317 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
6318 PFKEY_UNIT64(result
->m_pkthdr
.len
);
6320 return key_sendup_mbuf(NULL
, result
, KEY_SENDUP_REGISTERED
);
6328 #ifndef IPSEC_NONBLOCK_ACQUIRE
6329 static struct secacq
*
6330 key_newacq(const struct secasindex
*saidx
)
6332 struct secacq
*newacq
;
6335 KMALLOC(newacq
, struct secacq
*, sizeof(struct secacq
));
6336 if (newacq
== NULL
) {
6337 ipseclog((LOG_DEBUG
, "key_newacq: No more memory.\n"));
6340 memset(newacq
, 0, sizeof(*newacq
));
6343 memcpy(&newacq
->saidx
, saidx
, sizeof(newacq
->saidx
));
6344 newacq
->seq
= (acq_seq
== ~0 ? 1 : ++acq_seq
);
6345 newacq
->created
= time_second
;
6351 static struct secacq
*
6352 key_getacq(const struct secasindex
*saidx
)
6356 LIST_FOREACH(acq
, &acqtree
, chain
) {
6357 if (key_cmpsaidx(saidx
, &acq
->saidx
, CMP_EXACTLY
))
6364 static struct secacq
*
6365 key_getacqbyseq(u_int32_t seq
)
6369 LIST_FOREACH(acq
, &acqtree
, chain
) {
6370 if (acq
->seq
== seq
)
6378 static struct secspacq
*
6379 key_newspacq(struct secpolicyindex
*spidx
)
6381 struct secspacq
*acq
;
6384 KMALLOC(acq
, struct secspacq
*, sizeof(struct secspacq
));
6386 ipseclog((LOG_DEBUG
, "key_newspacq: No more memory.\n"));
6389 memset(acq
, 0, sizeof(*acq
));
6392 memcpy(&acq
->spidx
, spidx
, sizeof(acq
->spidx
));
6393 acq
->created
= time_second
;
6399 static struct secspacq
*
6400 key_getspacq(struct secpolicyindex
*spidx
)
6402 struct secspacq
*acq
;
6404 LIST_FOREACH(acq
, &spacqtree
, chain
) {
6405 if (key_cmpspidx_exactly(spidx
, &acq
->spidx
))
6413 * SADB_ACQUIRE processing,
6414 * in first situation, is receiving
6416 * from the ikmpd, and clear sequence of its secasvar entry.
6418 * In second situation, is receiving
6419 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
6420 * from a user land process, and return
6421 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
6424 * m will always be freed.
6427 key_acquire2(struct socket
*so
, struct mbuf
*m
,
6428 const struct sadb_msghdr
*mhp
)
6430 const struct sadb_address
*src0
, *dst0
;
6431 struct secasindex saidx
;
6432 struct secashead
*sah
;
6437 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
6438 panic("key_acquire2: NULL pointer is passed");
6441 * Error message from KMd.
6442 * We assume that if error was occurred in IKEd, the length of PFKEY
6443 * message is equal to the size of sadb_msg structure.
6444 * We do not raise error even if error occurred in this function.
6446 if (mhp
->msg
->sadb_msg_len
== PFKEY_UNIT64(sizeof(struct sadb_msg
))) {
6447 #ifndef IPSEC_NONBLOCK_ACQUIRE
6450 /* check sequence number */
6451 if (mhp
->msg
->sadb_msg_seq
== 0) {
6452 ipseclog((LOG_DEBUG
, "key_acquire2: must specify sequence number.\n"));
6457 if ((acq
= key_getacqbyseq(mhp
->msg
->sadb_msg_seq
)) == NULL
) {
6459 * the specified larval SA is already gone, or we got
6460 * a bogus sequence number. we can silently ignore it.
6466 /* reset acq counter in order to deletion by timehander. */
6467 acq
->created
= time_second
;
6475 * This message is from user land.
6478 /* map satype to proto */
6479 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
6480 ipseclog((LOG_DEBUG
, "key_acquire2: invalid satype is passed.\n"));
6481 return key_senderror(so
, m
, EINVAL
);
6484 if (mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
6485 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
||
6486 mhp
->ext
[SADB_EXT_PROPOSAL
] == NULL
) {
6488 ipseclog((LOG_DEBUG
, "key_acquire2: invalid message is passed.\n"));
6489 return key_senderror(so
, m
, EINVAL
);
6491 if (mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
6492 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
) ||
6493 mhp
->extlen
[SADB_EXT_PROPOSAL
] < sizeof(struct sadb_prop
)) {
6495 ipseclog((LOG_DEBUG
, "key_acquire2: invalid message is passed.\n"));
6496 return key_senderror(so
, m
, EINVAL
);
6499 src0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_SRC
];
6500 dst0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_DST
];
6502 if ((error
= key_setsecasidx(proto
, IPSEC_MODE_ANY
, 0, src0
+ 1,
6503 dst0
+ 1, &saidx
)) != 0)
6504 return key_senderror(so
, m
, EINVAL
);
6506 /* get a SA index */
6507 LIST_FOREACH(sah
, &sahtree
, chain
) {
6508 if (sah
->state
== SADB_SASTATE_DEAD
)
6510 if (key_cmpsaidx(&sah
->saidx
, &saidx
, CMP_MODE_REQID
))
6514 ipseclog((LOG_DEBUG
, "key_acquire2: a SA exists already.\n"));
6515 return key_senderror(so
, m
, EEXIST
);
6518 error
= key_acquire(&saidx
, NULL
);
6520 ipseclog((LOG_DEBUG
, "key_acquire2: error %d returned "
6521 "from key_acquire.\n", mhp
->msg
->sadb_msg_errno
));
6522 return key_senderror(so
, m
, error
);
6525 return key_sendup_mbuf(so
, m
, KEY_SENDUP_REGISTERED
);
6529 * SADB_REGISTER processing.
6530 * If SATYPE_UNSPEC has been passed as satype, only return sabd_supported.
6533 * from the ikmpd, and register a socket to send PF_KEY messages,
6537 * If socket is detached, must free from regnode.
6539 * m will always be freed.
6542 key_register(struct socket
*so
, struct mbuf
*m
,
6543 const struct sadb_msghdr
*mhp
)
6545 struct secreg
*reg
, *newreg
= 0;
6548 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
6549 panic("key_register: NULL pointer is passed");
6551 /* check for invalid register message */
6552 if (mhp
->msg
->sadb_msg_satype
>= sizeof(regtree
)/sizeof(regtree
[0]))
6553 return key_senderror(so
, m
, EINVAL
);
6555 /* When SATYPE_UNSPEC is specified, only return sabd_supported. */
6556 if (mhp
->msg
->sadb_msg_satype
== SADB_SATYPE_UNSPEC
)
6559 /* check whether existing or not */
6560 LIST_FOREACH(reg
, ®tree
[mhp
->msg
->sadb_msg_satype
], chain
) {
6561 if (reg
->so
== so
) {
6562 ipseclog((LOG_DEBUG
, "key_register: socket exists already.\n"));
6563 return key_senderror(so
, m
, EEXIST
);
6567 /* create regnode */
6568 KMALLOC(newreg
, struct secreg
*, sizeof(*newreg
));
6569 if (newreg
== NULL
) {
6570 ipseclog((LOG_DEBUG
, "key_register: No more memory.\n"));
6571 return key_senderror(so
, m
, ENOBUFS
);
6573 memset(newreg
, 0, sizeof(*newreg
));
6576 ((struct keycb
*)sotorawcb(so
))->kp_registered
++;
6578 /* add regnode to regtree. */
6579 LIST_INSERT_HEAD(®tree
[mhp
->msg
->sadb_msg_satype
], newreg
, chain
);
6584 struct sadb_msg
*newmsg
;
6585 struct sadb_supported
*sup
;
6586 u_int len
, alen
, elen
;
6589 struct sadb_alg
*alg
;
6591 /* create new sadb_msg to reply. */
6593 for (i
= 1; i
<= SADB_AALG_MAX
; i
++) {
6594 if (ah_algorithm_lookup(i
))
6595 alen
+= sizeof(struct sadb_alg
);
6598 alen
+= sizeof(struct sadb_supported
);
6600 for (i
= 1; i
<= SADB_EALG_MAX
; i
++) {
6601 if (esp_algorithm_lookup(i
))
6602 elen
+= sizeof(struct sadb_alg
);
6605 elen
+= sizeof(struct sadb_supported
);
6607 len
= sizeof(struct sadb_msg
) + alen
+ elen
;
6610 return key_senderror(so
, m
, ENOBUFS
);
6612 MGETHDR(n
, M_DONTWAIT
, MT_DATA
);
6614 MCLGET(n
, M_DONTWAIT
);
6615 if ((n
->m_flags
& M_EXT
) == 0) {
6621 return key_senderror(so
, m
, ENOBUFS
);
6623 n
->m_pkthdr
.len
= n
->m_len
= len
;
6627 m_copydata(m
, 0, sizeof(struct sadb_msg
), mtod(n
, char *) + off
);
6628 newmsg
= mtod(n
, struct sadb_msg
*);
6629 newmsg
->sadb_msg_errno
= 0;
6630 newmsg
->sadb_msg_len
= PFKEY_UNIT64(len
);
6631 off
+= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
6633 /* for authentication algorithm */
6635 sup
= (struct sadb_supported
*)(mtod(n
, char *) + off
);
6636 sup
->sadb_supported_len
= PFKEY_UNIT64(alen
);
6637 sup
->sadb_supported_exttype
= SADB_EXT_SUPPORTED_AUTH
;
6638 off
+= PFKEY_ALIGN8(sizeof(*sup
));
6640 for (i
= 1; i
<= SADB_AALG_MAX
; i
++) {
6641 struct auth_hash
*aalgo
;
6642 u_int16_t minkeysize
, maxkeysize
;
6644 aalgo
= ah_algorithm_lookup(i
);
6647 alg
= (struct sadb_alg
*)(mtod(n
, char *) + off
);
6648 alg
->sadb_alg_id
= i
;
6649 alg
->sadb_alg_ivlen
= 0;
6650 key_getsizes_ah(aalgo
, i
, &minkeysize
, &maxkeysize
);
6651 alg
->sadb_alg_minbits
= _BITS(minkeysize
);
6652 alg
->sadb_alg_maxbits
= _BITS(maxkeysize
);
6653 off
+= PFKEY_ALIGN8(sizeof(*alg
));
6657 /* for encryption algorithm */
6659 sup
= (struct sadb_supported
*)(mtod(n
, char *) + off
);
6660 sup
->sadb_supported_len
= PFKEY_UNIT64(elen
);
6661 sup
->sadb_supported_exttype
= SADB_EXT_SUPPORTED_ENCRYPT
;
6662 off
+= PFKEY_ALIGN8(sizeof(*sup
));
6664 for (i
= 1; i
<= SADB_EALG_MAX
; i
++) {
6665 struct enc_xform
*ealgo
;
6667 ealgo
= esp_algorithm_lookup(i
);
6670 alg
= (struct sadb_alg
*)(mtod(n
, char *) + off
);
6671 alg
->sadb_alg_id
= i
;
6672 alg
->sadb_alg_ivlen
= ealgo
->blocksize
;
6673 alg
->sadb_alg_minbits
= _BITS(ealgo
->minkey
);
6674 alg
->sadb_alg_maxbits
= _BITS(ealgo
->maxkey
);
6675 off
+= PFKEY_ALIGN8(sizeof(struct sadb_alg
));
6681 panic("length assumption failed in key_register");
6685 return key_sendup_mbuf(so
, n
, KEY_SENDUP_REGISTERED
);
6690 * free secreg entry registered.
6691 * XXX: I want to do free a socket marked done SADB_RESIGER to socket.
6694 key_freereg(struct socket
*so
)
6701 panic("key_freereg: NULL pointer is passed");
6704 * check whether existing or not.
6705 * check all type of SA, because there is a potential that
6706 * one socket is registered to multiple type of SA.
6708 for (i
= 0; i
<= SADB_SATYPE_MAX
; i
++) {
6709 LIST_FOREACH(reg
, ®tree
[i
], chain
) {
6711 && __LIST_CHAINED(reg
)) {
6712 LIST_REMOVE(reg
, chain
);
6723 * SADB_EXPIRE processing
6725 * <base, SA, SA2, lifetime(C and one of HS), address(SD)>
6727 * NOTE: We send only soft lifetime extension.
6730 * others : error number
6733 key_expire(struct secasvar
*sav
)
6737 struct mbuf
*result
= NULL
, *m
;
6740 struct sadb_lifetime
*lt
;
6742 /* XXX: Why do we lock ? */
6743 s
= splsoftnet(); /*called from softclock()*/
6747 panic("key_expire: NULL pointer is passed");
6748 if (sav
->sah
== NULL
)
6749 panic("key_expire: Why was SA index in SA NULL");
6750 if ((satype
= key_proto2satype(sav
->sah
->saidx
.proto
)) == 0)
6751 panic("key_expire: invalid proto is passed");
6753 /* set msg header */
6754 m
= key_setsadbmsg(SADB_EXPIRE
, 0, satype
, sav
->seq
, 0, sav
->refcnt
);
6761 /* create SA extension */
6762 m
= key_setsadbsa(sav
);
6769 /* create SA extension */
6770 m
= key_setsadbxsa2(sav
->sah
->saidx
.mode
,
6771 sav
->replay
? sav
->replay
->count
: 0,
6772 sav
->sah
->saidx
.reqid
);
6779 /* create lifetime extension (current and soft) */
6780 len
= PFKEY_ALIGN8(sizeof(*lt
)) * 2;
6781 m
= key_alloc_mbuf(len
);
6782 if (!m
|| m
->m_next
) { /*XXX*/
6788 memset(mtod(m
, void *), 0, len
);
6789 lt
= mtod(m
, struct sadb_lifetime
*);
6790 lt
->sadb_lifetime_len
= PFKEY_UNIT64(sizeof(struct sadb_lifetime
));
6791 lt
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
6792 lt
->sadb_lifetime_allocations
= sav
->lft_c
->sadb_lifetime_allocations
;
6793 lt
->sadb_lifetime_bytes
= sav
->lft_c
->sadb_lifetime_bytes
;
6794 lt
->sadb_lifetime_addtime
= sav
->lft_c
->sadb_lifetime_addtime
;
6795 lt
->sadb_lifetime_usetime
= sav
->lft_c
->sadb_lifetime_usetime
;
6796 lt
= (struct sadb_lifetime
*)(mtod(m
, char *) + len
/ 2);
6797 memcpy(lt
, sav
->lft_s
, sizeof(*lt
));
6800 /* set sadb_address for source */
6801 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
6802 &sav
->sah
->saidx
.src
.sa
,
6803 FULLMASK
, IPSEC_ULPROTO_ANY
);
6810 /* set sadb_address for destination */
6811 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
6812 &sav
->sah
->saidx
.dst
.sa
,
6813 FULLMASK
, IPSEC_ULPROTO_ANY
);
6820 if ((result
->m_flags
& M_PKTHDR
) == 0) {
6825 if (result
->m_len
< sizeof(struct sadb_msg
)) {
6826 result
= m_pullup(result
, sizeof(struct sadb_msg
));
6827 if (result
== NULL
) {
6833 result
->m_pkthdr
.len
= 0;
6834 for (m
= result
; m
; m
= m
->m_next
)
6835 result
->m_pkthdr
.len
+= m
->m_len
;
6837 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
6838 PFKEY_UNIT64(result
->m_pkthdr
.len
);
6841 return key_sendup_mbuf(NULL
, result
, KEY_SENDUP_REGISTERED
);
6851 * SADB_FLUSH processing
6854 * from the ikmpd, and free all entries in secastree.
6858 * NOTE: to do is only marking SADB_SASTATE_DEAD.
6860 * m will always be freed.
6863 key_flush(struct socket
*so
, struct mbuf
*m
,
6864 const struct sadb_msghdr
*mhp
)
6866 struct sadb_msg
*newmsg
;
6867 struct secashead
*sah
, *nextsah
;
6868 struct secasvar
*sav
, *nextsav
;
6874 if (so
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
6875 panic("key_flush: NULL pointer is passed");
6877 /* map satype to proto */
6878 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
6879 ipseclog((LOG_DEBUG
, "key_flush: invalid satype is passed.\n"));
6880 return key_senderror(so
, m
, EINVAL
);
6883 /* no SATYPE specified, i.e. flushing all SA. */
6884 for (sah
= LIST_FIRST(&sahtree
);
6887 nextsah
= LIST_NEXT(sah
, chain
);
6889 if (mhp
->msg
->sadb_msg_satype
!= SADB_SATYPE_UNSPEC
6890 && proto
!= sah
->saidx
.proto
)
6894 stateidx
< _ARRAYLEN(saorder_state_alive
);
6896 state
= saorder_state_any
[stateidx
];
6897 for (sav
= LIST_FIRST(&sah
->savtree
[state
]);
6901 nextsav
= LIST_NEXT(sav
, chain
);
6903 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
6908 sah
->state
= SADB_SASTATE_DEAD
;
6911 if (m
->m_len
< sizeof(struct sadb_msg
) ||
6912 sizeof(struct sadb_msg
) > m
->m_len
+ M_TRAILINGSPACE(m
)) {
6913 ipseclog((LOG_DEBUG
, "key_flush: No more memory.\n"));
6914 return key_senderror(so
, m
, ENOBUFS
);
6920 m
->m_pkthdr
.len
= m
->m_len
= sizeof(struct sadb_msg
);
6921 newmsg
= mtod(m
, struct sadb_msg
*);
6922 newmsg
->sadb_msg_errno
= 0;
6923 newmsg
->sadb_msg_len
= PFKEY_UNIT64(m
->m_pkthdr
.len
);
6925 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ALL
);
6929 static struct mbuf
*
6930 key_setdump_chain(u_int8_t req_satype
, int *errorp
, int *lenp
, pid_t pid
)
6932 struct secashead
*sah
;
6933 struct secasvar
*sav
;
6939 struct mbuf
*m
, *n
, *prev
;
6944 /* map satype to proto */
6945 if ((proto
= key_satype2proto(req_satype
)) == 0) {
6950 /* count sav entries to be sent to userland. */
6952 LIST_FOREACH(sah
, &sahtree
, chain
) {
6953 if (req_satype
!= SADB_SATYPE_UNSPEC
&&
6954 proto
!= sah
->saidx
.proto
)
6958 stateidx
< _ARRAYLEN(saorder_state_any
);
6960 state
= saorder_state_any
[stateidx
];
6961 LIST_FOREACH(sav
, &sah
->savtree
[state
], chain
) {
6972 /* send this to the userland, one at a time. */
6975 LIST_FOREACH(sah
, &sahtree
, chain
) {
6976 if (req_satype
!= SADB_SATYPE_UNSPEC
&&
6977 proto
!= sah
->saidx
.proto
)
6980 /* map proto to satype */
6981 if ((satype
= key_proto2satype(sah
->saidx
.proto
)) == 0) {
6988 stateidx
< _ARRAYLEN(saorder_state_any
);
6990 state
= saorder_state_any
[stateidx
];
6991 LIST_FOREACH(sav
, &sah
->savtree
[state
], chain
) {
6992 n
= key_setdumpsa(sav
, SADB_DUMP
, satype
,
7000 totlen
+= n
->m_pkthdr
.len
;
7004 prev
->m_nextpkt
= n
;
7015 if ((m
->m_flags
& M_PKTHDR
) != 0) {
7016 m
->m_pkthdr
.len
= 0;
7017 for (n
= m
; n
; n
= n
->m_next
)
7018 m
->m_pkthdr
.len
+= n
->m_len
;
7026 * SADB_DUMP processing
7027 * dump all entries including status of DEAD in SAD.
7030 * from the ikmpd, and dump all secasvar leaves
7035 * m will always be freed.
7038 key_dump(struct socket
*so
, struct mbuf
*m0
,
7039 const struct sadb_msghdr
*mhp
)
7048 if (so
== NULL
|| m0
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
7049 panic("key_dump: NULL pointer is passed");
7051 /* map satype to proto */
7052 satype
= mhp
->msg
->sadb_msg_satype
;
7053 if ((proto
= key_satype2proto(satype
)) == 0) {
7054 ipseclog((LOG_DEBUG
, "key_dump: invalid satype is passed.\n"));
7055 return key_senderror(so
, m0
, EINVAL
);
7059 * If the requestor has insufficient socket-buffer space
7060 * for the entire chain, nobody gets any response to the DUMP.
7061 * XXX For now, only the requestor ever gets anything.
7062 * Moreover, if the requestor has any space at all, they receive
7063 * the entire chain, otherwise the request is refused with ENOBUFS.
7065 if (sbspace(&so
->so_rcv
) <= 0) {
7066 return key_senderror(so
, m0
, ENOBUFS
);
7070 n
= key_setdump_chain(satype
, &error
, &len
, mhp
->msg
->sadb_msg_pid
);
7074 return key_senderror(so
, m0
, ENOENT
);
7077 uint64_t *ps
= PFKEY_STAT_GETREF();
7078 ps
[PFKEY_STAT_IN_TOTAL
]++;
7079 ps
[PFKEY_STAT_IN_BYTES
] += len
;
7080 PFKEY_STAT_PUTREF();
7084 * PF_KEY DUMP responses are no longer broadcast to all PF_KEY sockets.
7085 * The requestor receives either the entire chain, or an
7086 * error message with ENOBUFS.
7088 * sbappendaddrchain() takes the chain of entries, one
7089 * packet-record per SPD entry, prepends the key_src sockaddr
7090 * to each packet-record, links the sockaddr mbufs into a new
7091 * list of records, then appends the entire resulting
7092 * list to the requesting socket.
7094 ok
= sbappendaddrchain(&so
->so_rcv
, (struct sockaddr
*)&key_src
,
7095 n
, SB_PRIO_ONESHOT_OVERFLOW
);
7098 PFKEY_STATINC(PFKEY_STAT_IN_NOMEM
);
7100 return key_senderror(so
, m0
, ENOBUFS
);
7108 * SADB_X_PROMISC processing
7110 * m will always be freed.
7113 key_promisc(struct socket
*so
, struct mbuf
*m
,
7114 const struct sadb_msghdr
*mhp
)
7119 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
7120 panic("key_promisc: NULL pointer is passed");
7122 olen
= PFKEY_UNUNIT64(mhp
->msg
->sadb_msg_len
);
7124 if (olen
< sizeof(struct sadb_msg
)) {
7126 return key_senderror(so
, m
, EINVAL
);
7131 } else if (olen
== sizeof(struct sadb_msg
)) {
7132 /* enable/disable promisc mode */
7135 if ((kp
= (struct keycb
*)sotorawcb(so
)) == NULL
)
7136 return key_senderror(so
, m
, EINVAL
);
7137 mhp
->msg
->sadb_msg_errno
= 0;
7138 switch (mhp
->msg
->sadb_msg_satype
) {
7141 kp
->kp_promisc
= mhp
->msg
->sadb_msg_satype
;
7144 return key_senderror(so
, m
, EINVAL
);
7147 /* send the original message back to everyone */
7148 mhp
->msg
->sadb_msg_errno
= 0;
7149 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ALL
);
7151 /* send packet as is */
7153 m_adj(m
, PFKEY_ALIGN8(sizeof(struct sadb_msg
)));
7155 /* TODO: if sadb_msg_seq is specified, send to specific pid */
7156 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ALL
);
7160 static int (*key_typesw
[]) (struct socket
*, struct mbuf
*,
7161 const struct sadb_msghdr
*) = {
7162 NULL
, /* SADB_RESERVED */
7163 key_getspi
, /* SADB_GETSPI */
7164 key_update
, /* SADB_UPDATE */
7165 key_add
, /* SADB_ADD */
7166 key_delete
, /* SADB_DELETE */
7167 key_get
, /* SADB_GET */
7168 key_acquire2
, /* SADB_ACQUIRE */
7169 key_register
, /* SADB_REGISTER */
7170 NULL
, /* SADB_EXPIRE */
7171 key_flush
, /* SADB_FLUSH */
7172 key_dump
, /* SADB_DUMP */
7173 key_promisc
, /* SADB_X_PROMISC */
7174 NULL
, /* SADB_X_PCHANGE */
7175 key_spdadd
, /* SADB_X_SPDUPDATE */
7176 key_spdadd
, /* SADB_X_SPDADD */
7177 key_spddelete
, /* SADB_X_SPDDELETE */
7178 key_spdget
, /* SADB_X_SPDGET */
7179 NULL
, /* SADB_X_SPDACQUIRE */
7180 key_spddump
, /* SADB_X_SPDDUMP */
7181 key_spdflush
, /* SADB_X_SPDFLUSH */
7182 key_spdadd
, /* SADB_X_SPDSETIDX */
7183 NULL
, /* SADB_X_SPDEXPIRE */
7184 key_spddelete2
, /* SADB_X_SPDDELETE2 */
7186 key_nat_map
, /* SADB_X_NAT_T_NEW_MAPPING */
7191 * parse sadb_msg buffer to process PFKEYv2,
7192 * and create a data to response if needed.
7193 * I think to be dealed with mbuf directly.
7195 * msgp : pointer to pointer to a received buffer pulluped.
7196 * This is rewrited to response.
7197 * so : pointer to socket.
7199 * length for buffer to send to user process.
7202 key_parse(struct mbuf
*m
, struct socket
*so
)
7204 struct sadb_msg
*msg
;
7205 struct sadb_msghdr mh
;
7211 if (m
== NULL
|| so
== NULL
)
7212 panic("key_parse: NULL pointer is passed");
7214 #if 0 /*kdebug_sadb assumes msg in linear buffer*/
7215 KEYDEBUG(KEYDEBUG_KEY_DUMP
,
7216 ipseclog((LOG_DEBUG
, "key_parse: passed sadb_msg\n"));
7220 if (m
->m_len
< sizeof(struct sadb_msg
)) {
7221 m
= m_pullup(m
, sizeof(struct sadb_msg
));
7225 msg
= mtod(m
, struct sadb_msg
*);
7226 orglen
= PFKEY_UNUNIT64(msg
->sadb_msg_len
);
7227 target
= KEY_SENDUP_ONE
;
7229 if ((m
->m_flags
& M_PKTHDR
) == 0 ||
7230 m
->m_pkthdr
.len
!= m
->m_pkthdr
.len
) {
7231 ipseclog((LOG_DEBUG
, "key_parse: invalid message length.\n"));
7232 PFKEY_STATINC(PFKEY_STAT_OUT_INVLEN
);
7237 if (msg
->sadb_msg_version
!= PF_KEY_V2
) {
7238 ipseclog((LOG_DEBUG
,
7239 "key_parse: PF_KEY version %u is mismatched.\n",
7240 msg
->sadb_msg_version
));
7241 PFKEY_STATINC(PFKEY_STAT_OUT_INVVER
);
7246 if (msg
->sadb_msg_type
> SADB_MAX
) {
7247 ipseclog((LOG_DEBUG
, "key_parse: invalid type %u is passed.\n",
7248 msg
->sadb_msg_type
));
7249 PFKEY_STATINC(PFKEY_STAT_OUT_INVMSGTYPE
);
7254 /* for old-fashioned code - should be nuked */
7255 if (m
->m_pkthdr
.len
> MCLBYTES
) {
7262 MGETHDR(n
, M_DONTWAIT
, MT_DATA
);
7263 if (n
&& m
->m_pkthdr
.len
> MHLEN
) {
7264 MCLGET(n
, M_DONTWAIT
);
7265 if ((n
->m_flags
& M_EXT
) == 0) {
7274 m_copydata(m
, 0, m
->m_pkthdr
.len
, mtod(n
, void *));
7275 n
->m_pkthdr
.len
= n
->m_len
= m
->m_pkthdr
.len
;
7281 /* align the mbuf chain so that extensions are in contiguous region. */
7282 error
= key_align(m
, &mh
);
7286 if (m
->m_next
) { /*XXX*/
7294 switch (msg
->sadb_msg_satype
) {
7295 case SADB_SATYPE_UNSPEC
:
7296 switch (msg
->sadb_msg_type
) {
7304 ipseclog((LOG_DEBUG
, "key_parse: must specify satype "
7305 "when msg type=%u.\n", msg
->sadb_msg_type
));
7306 PFKEY_STATINC(PFKEY_STAT_OUT_INVSATYPE
);
7311 case SADB_SATYPE_AH
:
7312 case SADB_SATYPE_ESP
:
7313 case SADB_X_SATYPE_IPCOMP
:
7314 case SADB_X_SATYPE_TCPSIGNATURE
:
7315 switch (msg
->sadb_msg_type
) {
7317 case SADB_X_SPDDELETE
:
7319 case SADB_X_SPDDUMP
:
7320 case SADB_X_SPDFLUSH
:
7321 case SADB_X_SPDSETIDX
:
7322 case SADB_X_SPDUPDATE
:
7323 case SADB_X_SPDDELETE2
:
7324 ipseclog((LOG_DEBUG
, "key_parse: illegal satype=%u\n",
7325 msg
->sadb_msg_type
));
7326 PFKEY_STATINC(PFKEY_STAT_OUT_INVSATYPE
);
7331 case SADB_SATYPE_RSVP
:
7332 case SADB_SATYPE_OSPFV2
:
7333 case SADB_SATYPE_RIPV2
:
7334 case SADB_SATYPE_MIP
:
7335 ipseclog((LOG_DEBUG
, "key_parse: type %u isn't supported.\n",
7336 msg
->sadb_msg_satype
));
7337 PFKEY_STATINC(PFKEY_STAT_OUT_INVSATYPE
);
7340 case 1: /* XXX: What does it do? */
7341 if (msg
->sadb_msg_type
== SADB_X_PROMISC
)
7345 ipseclog((LOG_DEBUG
, "key_parse: invalid type %u is passed.\n",
7346 msg
->sadb_msg_satype
));
7347 PFKEY_STATINC(PFKEY_STAT_OUT_INVSATYPE
);
7352 /* check field of upper layer protocol and address family */
7353 if (mh
.ext
[SADB_EXT_ADDRESS_SRC
] != NULL
7354 && mh
.ext
[SADB_EXT_ADDRESS_DST
] != NULL
) {
7355 struct sadb_address
*src0
, *dst0
;
7358 src0
= (struct sadb_address
*)(mh
.ext
[SADB_EXT_ADDRESS_SRC
]);
7359 dst0
= (struct sadb_address
*)(mh
.ext
[SADB_EXT_ADDRESS_DST
]);
7361 /* check upper layer protocol */
7362 if (src0
->sadb_address_proto
!= dst0
->sadb_address_proto
) {
7363 ipseclog((LOG_DEBUG
, "key_parse: upper layer protocol mismatched.\n"));
7364 PFKEY_STATINC(PFKEY_STAT_OUT_INVADDR
);
7370 if (PFKEY_ADDR_SADDR(src0
)->sa_family
!=
7371 PFKEY_ADDR_SADDR(dst0
)->sa_family
) {
7372 ipseclog((LOG_DEBUG
, "key_parse: address family mismatched.\n"));
7373 PFKEY_STATINC(PFKEY_STAT_OUT_INVADDR
);
7377 if (PFKEY_ADDR_SADDR(src0
)->sa_len
!=
7378 PFKEY_ADDR_SADDR(dst0
)->sa_len
) {
7379 ipseclog((LOG_DEBUG
,
7380 "key_parse: address struct size mismatched.\n"));
7381 PFKEY_STATINC(PFKEY_STAT_OUT_INVADDR
);
7386 switch (PFKEY_ADDR_SADDR(src0
)->sa_family
) {
7388 if (PFKEY_ADDR_SADDR(src0
)->sa_len
!=
7389 sizeof(struct sockaddr_in
)) {
7390 PFKEY_STATINC(PFKEY_STAT_OUT_INVADDR
);
7396 if (PFKEY_ADDR_SADDR(src0
)->sa_len
!=
7397 sizeof(struct sockaddr_in6
)) {
7398 PFKEY_STATINC(PFKEY_STAT_OUT_INVADDR
);
7404 ipseclog((LOG_DEBUG
,
7405 "key_parse: unsupported address family.\n"));
7406 PFKEY_STATINC(PFKEY_STAT_OUT_INVADDR
);
7407 error
= EAFNOSUPPORT
;
7411 switch (PFKEY_ADDR_SADDR(src0
)->sa_family
) {
7413 plen
= sizeof(struct in_addr
) << 3;
7416 plen
= sizeof(struct in6_addr
) << 3;
7419 plen
= 0; /*fool gcc*/
7423 /* check max prefix length */
7424 if (src0
->sadb_address_prefixlen
> plen
||
7425 dst0
->sadb_address_prefixlen
> plen
) {
7426 ipseclog((LOG_DEBUG
,
7427 "key_parse: illegal prefixlen.\n"));
7428 PFKEY_STATINC(PFKEY_STAT_OUT_INVADDR
);
7434 * prefixlen == 0 is valid because there can be a case when
7435 * all addresses are matched.
7439 if (msg
->sadb_msg_type
>= sizeof(key_typesw
)/sizeof(key_typesw
[0]) ||
7440 key_typesw
[msg
->sadb_msg_type
] == NULL
) {
7441 PFKEY_STATINC(PFKEY_STAT_OUT_INVMSGTYPE
);
7446 return (*key_typesw
[msg
->sadb_msg_type
])(so
, m
, &mh
);
7449 msg
->sadb_msg_errno
= error
;
7450 return key_sendup_mbuf(so
, m
, target
);
7454 key_senderror(struct socket
*so
, struct mbuf
*m
, int code
)
7456 struct sadb_msg
*msg
;
7458 if (m
->m_len
< sizeof(struct sadb_msg
))
7459 panic("invalid mbuf passed to key_senderror");
7461 msg
= mtod(m
, struct sadb_msg
*);
7462 msg
->sadb_msg_errno
= code
;
7463 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ONE
);
7467 * set the pointer to each header into message buffer.
7468 * m will be freed on error.
7469 * XXX larger-than-MCLBYTES extension?
7472 key_align(struct mbuf
*m
, struct sadb_msghdr
*mhp
)
7475 struct sadb_ext
*ext
;
7481 if (m
== NULL
|| mhp
== NULL
)
7482 panic("key_align: NULL pointer is passed");
7483 if (m
->m_len
< sizeof(struct sadb_msg
))
7484 panic("invalid mbuf passed to key_align");
7487 memset(mhp
, 0, sizeof(*mhp
));
7489 mhp
->msg
= mtod(m
, struct sadb_msg
*);
7490 mhp
->ext
[0] = (struct sadb_ext
*)mhp
->msg
; /*XXX backward compat */
7492 end
= PFKEY_UNUNIT64(mhp
->msg
->sadb_msg_len
);
7493 extlen
= end
; /*just in case extlen is not updated*/
7494 for (off
= sizeof(struct sadb_msg
); off
< end
; off
+= extlen
) {
7495 n
= m_pulldown(m
, off
, sizeof(struct sadb_ext
), &toff
);
7497 /* m is already freed */
7500 ext
= (struct sadb_ext
*)(mtod(n
, char *) + toff
);
7503 switch (ext
->sadb_ext_type
) {
7505 case SADB_EXT_ADDRESS_SRC
:
7506 case SADB_EXT_ADDRESS_DST
:
7507 case SADB_EXT_ADDRESS_PROXY
:
7508 case SADB_EXT_LIFETIME_CURRENT
:
7509 case SADB_EXT_LIFETIME_HARD
:
7510 case SADB_EXT_LIFETIME_SOFT
:
7511 case SADB_EXT_KEY_AUTH
:
7512 case SADB_EXT_KEY_ENCRYPT
:
7513 case SADB_EXT_IDENTITY_SRC
:
7514 case SADB_EXT_IDENTITY_DST
:
7515 case SADB_EXT_SENSITIVITY
:
7516 case SADB_EXT_PROPOSAL
:
7517 case SADB_EXT_SUPPORTED_AUTH
:
7518 case SADB_EXT_SUPPORTED_ENCRYPT
:
7519 case SADB_EXT_SPIRANGE
:
7520 case SADB_X_EXT_POLICY
:
7521 case SADB_X_EXT_SA2
:
7523 case SADB_X_EXT_NAT_T_TYPE
:
7524 case SADB_X_EXT_NAT_T_SPORT
:
7525 case SADB_X_EXT_NAT_T_DPORT
:
7526 case SADB_X_EXT_NAT_T_OA
:
7527 case SADB_X_EXT_NAT_T_FRAG
:
7529 /* duplicate check */
7531 * XXX Are there duplication payloads of either
7532 * KEY_AUTH or KEY_ENCRYPT ?
7534 if (mhp
->ext
[ext
->sadb_ext_type
] != NULL
) {
7535 ipseclog((LOG_DEBUG
,
7536 "key_align: duplicate ext_type %u "
7537 "is passed.\n", ext
->sadb_ext_type
));
7539 PFKEY_STATINC(PFKEY_STAT_OUT_DUPEXT
);
7544 ipseclog((LOG_DEBUG
,
7545 "key_align: invalid ext_type %u is passed.\n",
7546 ext
->sadb_ext_type
));
7548 PFKEY_STATINC(PFKEY_STAT_OUT_INVEXTTYPE
);
7552 extlen
= PFKEY_UNUNIT64(ext
->sadb_ext_len
);
7554 if (key_validate_ext(ext
, extlen
)) {
7556 PFKEY_STATINC(PFKEY_STAT_OUT_INVLEN
);
7560 n
= m_pulldown(m
, off
, extlen
, &toff
);
7562 /* m is already freed */
7565 ext
= (struct sadb_ext
*)(mtod(n
, char *) + toff
);
7567 mhp
->ext
[ext
->sadb_ext_type
] = ext
;
7568 mhp
->extoff
[ext
->sadb_ext_type
] = off
;
7569 mhp
->extlen
[ext
->sadb_ext_type
] = extlen
;
7574 PFKEY_STATINC(PFKEY_STAT_OUT_INVLEN
);
7582 key_validate_ext(const struct sadb_ext
*ext
, int len
)
7584 const struct sockaddr
*sa
;
7585 enum { NONE
, ADDR
} checktype
= NONE
;
7587 const int sal
= offsetof(struct sockaddr
, sa_len
) + sizeof(sa
->sa_len
);
7589 if (len
!= PFKEY_UNUNIT64(ext
->sadb_ext_len
))
7592 /* if it does not match minimum/maximum length, bail */
7593 if (ext
->sadb_ext_type
>= sizeof(minsize
) / sizeof(minsize
[0]) ||
7594 ext
->sadb_ext_type
>= sizeof(maxsize
) / sizeof(maxsize
[0]))
7596 if (!minsize
[ext
->sadb_ext_type
] || len
< minsize
[ext
->sadb_ext_type
])
7598 if (maxsize
[ext
->sadb_ext_type
] && len
> maxsize
[ext
->sadb_ext_type
])
7601 /* more checks based on sadb_ext_type XXX need more */
7602 switch (ext
->sadb_ext_type
) {
7603 case SADB_EXT_ADDRESS_SRC
:
7604 case SADB_EXT_ADDRESS_DST
:
7605 case SADB_EXT_ADDRESS_PROXY
:
7606 baselen
= PFKEY_ALIGN8(sizeof(struct sadb_address
));
7609 case SADB_EXT_IDENTITY_SRC
:
7610 case SADB_EXT_IDENTITY_DST
:
7611 if (((const struct sadb_ident
*)ext
)->sadb_ident_type
==
7612 SADB_X_IDENTTYPE_ADDR
) {
7613 baselen
= PFKEY_ALIGN8(sizeof(struct sadb_ident
));
7623 switch (checktype
) {
7627 sa
= (const struct sockaddr
*)(((const u_int8_t
*)ext
)+baselen
);
7628 if (len
< baselen
+ sal
)
7630 if (baselen
+ PFKEY_ALIGN8(sa
->sa_len
) != len
)
7643 pfkeystat_percpu
= percpu_alloc(sizeof(uint64_t) * PFKEY_NSTATS
);
7645 callout_init(&key_timehandler_ch
, 0);
7647 for (i
= 0; i
< IPSEC_DIR_MAX
; i
++) {
7648 LIST_INIT(&sptree
[i
]);
7651 LIST_INIT(&sahtree
);
7653 for (i
= 0; i
<= SADB_SATYPE_MAX
; i
++) {
7654 LIST_INIT(®tree
[i
]);
7657 #ifndef IPSEC_NONBLOCK_ACQUIRE
7658 LIST_INIT(&acqtree
);
7660 LIST_INIT(&spacqtree
);
7662 /* system default */
7663 ip4_def_policy
.policy
= IPSEC_POLICY_NONE
;
7664 ip4_def_policy
.refcnt
++; /*never reclaim this*/
7667 ip6_def_policy
.policy
= IPSEC_POLICY_NONE
;
7668 ip6_def_policy
.refcnt
++; /*never reclaim this*/
7672 #ifndef IPSEC_DEBUG2
7673 callout_reset(&key_timehandler_ch
, hz
, key_timehandler
, NULL
);
7674 #endif /*IPSEC_DEBUG2*/
7676 /* initialize key statistics */
7677 keystat
.getspi_count
= 1;
7679 printf("IPsec: Initialized Security Association Processing.\n");
7687 static ONCE_DECL(key_init_once
);
7689 RUN_ONCE(&key_init_once
, key_do_init
);
7693 * XXX: maybe This function is called after INBOUND IPsec processing.
7695 * Special check for tunnel-mode packets.
7696 * We must make some checks for consistency between inner and outer IP header.
7698 * xxx more checks to be provided
7701 key_checktunnelsanity(
7702 struct secasvar
*sav
,
7709 if (sav
->sah
== NULL
)
7710 panic("sav->sah == NULL at key_checktunnelsanity");
7712 /* XXX: check inner IP header */
7718 #define hostnamelen strlen(hostname)
7721 * Get FQDN for the host.
7722 * If the administrator configured hostname (by hostname(1)) without
7723 * domain name, returns nothing.
7730 static char fqdn
[MAXHOSTNAMELEN
+ 1];
7735 /* check if it comes with domain name. */
7737 for (i
= 0; i
< hostnamelen
; i
++) {
7738 if (hostname
[i
] == '.')
7744 /* NOTE: hostname may not be NUL-terminated. */
7745 memset(fqdn
, 0, sizeof(fqdn
));
7746 memcpy(fqdn
, hostname
, hostnamelen
);
7747 fqdn
[hostnamelen
] = '\0';
7752 * get username@FQDN for the host/user.
7755 key_getuserfqdn(void)
7758 static char userfqdn
[MAXHOSTNAMELEN
+ MAXLOGNAME
+ 2];
7759 struct proc
*p
= curproc
;
7762 if (!p
|| !p
->p_pgrp
|| !p
->p_pgrp
->pg_session
)
7764 if (!(host
= key_getfqdn()))
7767 /* NOTE: s_login may not be-NUL terminated. */
7768 memset(userfqdn
, 0, sizeof(userfqdn
));
7769 memcpy(userfqdn
, Mp
->p_pgrp
->pg_session
->s_login
, AXLOGNAME
);
7770 userfqdn
[MAXLOGNAME
] = '\0'; /* safeguard */
7771 q
= userfqdn
+ strlen(userfqdn
);
7773 memcpy(q
, host
, strlen(host
));
7781 /* record data transfer on SA, and update timestamps */
7783 key_sa_recordxfer(struct secasvar
*sav
, struct mbuf
*m
)
7785 IPSEC_ASSERT(sav
!= NULL
, ("key_sa_recordxfer: Null secasvar"));
7786 IPSEC_ASSERT(m
!= NULL
, ("key_sa_recordxfer: Null mbuf"));
7791 * XXX Currently, there is a difference of bytes size
7792 * between inbound and outbound processing.
7794 sav
->lft_c
->sadb_lifetime_bytes
+= m
->m_pkthdr
.len
;
7795 /* to check bytes lifetime is done in key_timehandler(). */
7798 * We use the number of packets as the unit of
7799 * sadb_lifetime_allocations. We increment the variable
7800 * whenever {esp,ah}_{in,out}put is called.
7802 sav
->lft_c
->sadb_lifetime_allocations
++;
7803 /* XXX check for expires? */
7806 * NOTE: We record CURRENT sadb_lifetime_usetime by using wall clock,
7807 * in seconds. HARD and SOFT lifetime are measured by the time
7808 * difference (again in seconds) from sadb_lifetime_usetime.
7812 * -----+-----+--------+---> t
7813 * <--------------> HARD
7816 sav
->lft_c
->sadb_lifetime_usetime
= time_second
;
7817 /* XXX check for expires? */
7824 key_sa_routechange(struct sockaddr
*dst
)
7826 struct secashead
*sah
;
7828 const struct sockaddr
*sa
;
7830 LIST_FOREACH(sah
, &sahtree
, chain
) {
7831 ro
= &sah
->sa_route
;
7832 sa
= rtcache_getdst(ro
);
7833 if (sa
!= NULL
&& dst
->sa_len
== sa
->sa_len
&&
7834 memcmp(dst
, sa
, dst
->sa_len
) == 0)
7842 key_sa_chgstate(struct secasvar
*sav
, u_int8_t state
)
7845 panic("key_sa_chgstate called with sav == NULL");
7847 if (sav
->state
== state
)
7850 if (__LIST_CHAINED(sav
))
7851 LIST_REMOVE(sav
, chain
);
7854 LIST_INSERT_HEAD(&sav
->sah
->savtree
[state
], sav
, chain
);
7858 key_sa_stir_iv(struct secasvar
*sav
)
7862 panic("key_sa_stir_iv called with sav == NULL");
7863 key_randomfill(sav
->iv
, sav
->ivlen
);
7867 static struct mbuf
*
7868 key_alloc_mbuf(int l
)
7870 struct mbuf
*m
= NULL
, *n
;
7875 MGET(n
, M_DONTWAIT
, MT_DATA
);
7876 if (n
&& len
> MLEN
)
7877 MCLGET(n
, M_DONTWAIT
);
7885 n
->m_len
= M_TRAILINGSPACE(n
);
7886 /* use the bottom of mbuf, hoping we can prepend afterwards */
7887 if (n
->m_len
> len
) {
7888 t
= (n
->m_len
- len
) & ~(sizeof(long) - 1);
7904 static struct mbuf
*
7905 key_setdump(u_int8_t req_satype
, int *errorp
, uint32_t pid
)
7907 struct secashead
*sah
;
7908 struct secasvar
*sav
;
7916 /* map satype to proto */
7917 if ((proto
= key_satype2proto(req_satype
)) == 0) {
7922 /* count sav entries to be sent to the userland. */
7924 LIST_FOREACH(sah
, &sahtree
, chain
) {
7925 if (req_satype
!= SADB_SATYPE_UNSPEC
&&
7926 proto
!= sah
->saidx
.proto
)
7930 stateidx
< _ARRAYLEN(saorder_state_any
);
7932 state
= saorder_state_any
[stateidx
];
7933 LIST_FOREACH(sav
, &sah
->savtree
[state
], chain
) {
7944 /* send this to the userland, one at a time. */
7946 LIST_FOREACH(sah
, &sahtree
, chain
) {
7947 if (req_satype
!= SADB_SATYPE_UNSPEC
&&
7948 proto
!= sah
->saidx
.proto
)
7951 /* map proto to satype */
7952 if ((satype
= key_proto2satype(sah
->saidx
.proto
)) == 0) {
7959 stateidx
< _ARRAYLEN(saorder_state_any
);
7961 state
= saorder_state_any
[stateidx
];
7962 LIST_FOREACH(sav
, &sah
->savtree
[state
], chain
) {
7963 n
= key_setdumpsa(sav
, SADB_DUMP
, satype
,
7984 if ((m
->m_flags
& M_PKTHDR
) != 0) {
7985 m
->m_pkthdr
.len
= 0;
7986 for (n
= m
; n
; n
= n
->m_next
)
7987 m
->m_pkthdr
.len
+= n
->m_len
;
7994 static struct mbuf
*
7995 key_setspddump(int *errorp
, pid_t pid
)
7997 struct secpolicy
*sp
;
8002 /* search SPD entry and get buffer size. */
8004 for (dir
= 0; dir
< IPSEC_DIR_MAX
; dir
++) {
8005 LIST_FOREACH(sp
, &sptree
[dir
], chain
) {
8016 for (dir
= 0; dir
< IPSEC_DIR_MAX
; dir
++) {
8017 LIST_FOREACH(sp
, &sptree
[dir
], chain
) {
8019 n
= key_setdumpsp(sp
, SADB_X_SPDDUMP
, cnt
, pid
);
8029 m
->m_pkthdr
.len
+= n
->m_pkthdr
.len
;
8040 sysctl_net_key_dumpsa(SYSCTLFN_ARGS
)
8054 m
= key_setdump(name
[0], &error
, l
->l_proc
->p_pid
);
8059 *oldlenp
= m
->m_pkthdr
.len
;
8062 if (*oldlenp
< m
->m_pkthdr
.len
) {
8066 *oldlenp
= m
->m_pkthdr
.len
;
8067 ep
= p
+ m
->m_pkthdr
.len
;
8069 for (n
= m
; n
; n
= n
->m_next
) {
8070 len
= (ep
- p
< n
->m_len
) ?
8072 error
= copyout(mtod(n
, const void *), p
, len
);
8086 sysctl_net_key_dumpsp(SYSCTLFN_ARGS
)
8100 m
= key_setspddump(&error
, l
->l_proc
->p_pid
);
8105 *oldlenp
= m
->m_pkthdr
.len
;
8108 if (*oldlenp
< m
->m_pkthdr
.len
) {
8112 *oldlenp
= m
->m_pkthdr
.len
;
8113 ep
= p
+ m
->m_pkthdr
.len
;
8115 for (n
= m
; n
; n
= n
->m_next
) {
8116 len
= (ep
- p
< n
->m_len
) ?
8118 error
= copyout(mtod(n
, const void *), p
, len
);
8132 * Create sysctl tree for native FAST_IPSEC key knobs, originally
8133 * under name "net.keyv2" * with MIB number { CTL_NET, PF_KEY_V2. }.
8134 * However, sysctl(8) never checked for nodes under { CTL_NET, PF_KEY_V2 };
8135 * and in any case the part of our sysctl namespace used for dumping the
8136 * SPD and SA database *HAS* to be compatible with the KAME sysctl
8137 * namespace, for API reasons.
8139 * Pending a consensus on the right way to fix this, add a level of
8140 * indirection in how we number the `native' FAST_IPSEC key nodes;
8141 * and (as requested by Andrew Brown) move registration of the
8142 * KAME-compatible names to a separate function.
8145 # define FAST_IPSEC_PFKEY PF_KEY_V2
8146 # define FAST_IPSEC_PFKEY_NAME "keyv2"
8148 # define FAST_IPSEC_PFKEY PF_KEY
8149 # define FAST_IPSEC_PFKEY_NAME "key"
8153 sysctl_net_key_stats(SYSCTLFN_ARGS
)
8156 return (NETSTAT_SYSCTL(pfkeystat_percpu
, PFKEY_NSTATS
));
8159 SYSCTL_SETUP(sysctl_net_keyv2_setup
, "sysctl net.keyv2 subtree setup")
8162 sysctl_createv(clog
, 0, NULL
, NULL
,
8164 CTLTYPE_NODE
, "net", NULL
,
8167 sysctl_createv(clog
, 0, NULL
, NULL
,
8169 CTLTYPE_NODE
, FAST_IPSEC_PFKEY_NAME
, NULL
,
8171 CTL_NET
, FAST_IPSEC_PFKEY
, CTL_EOL
);
8173 sysctl_createv(clog
, 0, NULL
, NULL
,
8174 CTLFLAG_PERMANENT
|CTLFLAG_READWRITE
,
8175 CTLTYPE_INT
, "debug", NULL
,
8176 NULL
, 0, &key_debug_level
, 0,
8177 CTL_NET
, FAST_IPSEC_PFKEY
, KEYCTL_DEBUG_LEVEL
, CTL_EOL
);
8178 sysctl_createv(clog
, 0, NULL
, NULL
,
8179 CTLFLAG_PERMANENT
|CTLFLAG_READWRITE
,
8180 CTLTYPE_INT
, "spi_try", NULL
,
8181 NULL
, 0, &key_spi_trycnt
, 0,
8182 CTL_NET
, FAST_IPSEC_PFKEY
, KEYCTL_SPI_TRY
, CTL_EOL
);
8183 sysctl_createv(clog
, 0, NULL
, NULL
,
8184 CTLFLAG_PERMANENT
|CTLFLAG_READWRITE
,
8185 CTLTYPE_INT
, "spi_min_value", NULL
,
8186 NULL
, 0, &key_spi_minval
, 0,
8187 CTL_NET
, FAST_IPSEC_PFKEY
, KEYCTL_SPI_MIN_VALUE
, CTL_EOL
);
8188 sysctl_createv(clog
, 0, NULL
, NULL
,
8189 CTLFLAG_PERMANENT
|CTLFLAG_READWRITE
,
8190 CTLTYPE_INT
, "spi_max_value", NULL
,
8191 NULL
, 0, &key_spi_maxval
, 0,
8192 CTL_NET
, FAST_IPSEC_PFKEY
, KEYCTL_SPI_MAX_VALUE
, CTL_EOL
);
8193 sysctl_createv(clog
, 0, NULL
, NULL
,
8194 CTLFLAG_PERMANENT
|CTLFLAG_READWRITE
,
8195 CTLTYPE_INT
, "random_int", NULL
,
8196 NULL
, 0, &key_int_random
, 0,
8197 CTL_NET
, FAST_IPSEC_PFKEY
, KEYCTL_RANDOM_INT
, CTL_EOL
);
8198 sysctl_createv(clog
, 0, NULL
, NULL
,
8199 CTLFLAG_PERMANENT
|CTLFLAG_READWRITE
,
8200 CTLTYPE_INT
, "larval_lifetime", NULL
,
8201 NULL
, 0, &key_larval_lifetime
, 0,
8202 CTL_NET
, FAST_IPSEC_PFKEY
, KEYCTL_LARVAL_LIFETIME
, CTL_EOL
);
8203 sysctl_createv(clog
, 0, NULL
, NULL
,
8204 CTLFLAG_PERMANENT
|CTLFLAG_READWRITE
,
8205 CTLTYPE_INT
, "blockacq_count", NULL
,
8206 NULL
, 0, &key_blockacq_count
, 0,
8207 CTL_NET
, FAST_IPSEC_PFKEY
, KEYCTL_BLOCKACQ_COUNT
, CTL_EOL
);
8208 sysctl_createv(clog
, 0, NULL
, NULL
,
8209 CTLFLAG_PERMANENT
|CTLFLAG_READWRITE
,
8210 CTLTYPE_INT
, "blockacq_lifetime", NULL
,
8211 NULL
, 0, &key_blockacq_lifetime
, 0,
8212 CTL_NET
, FAST_IPSEC_PFKEY
, KEYCTL_BLOCKACQ_LIFETIME
, CTL_EOL
);
8213 sysctl_createv(clog
, 0, NULL
, NULL
,
8214 CTLFLAG_PERMANENT
|CTLFLAG_READWRITE
,
8215 CTLTYPE_INT
, "esp_keymin", NULL
,
8216 NULL
, 0, &ipsec_esp_keymin
, 0,
8217 CTL_NET
, FAST_IPSEC_PFKEY
, KEYCTL_ESP_KEYMIN
, CTL_EOL
);
8218 sysctl_createv(clog
, 0, NULL
, NULL
,
8219 CTLFLAG_PERMANENT
|CTLFLAG_READWRITE
,
8220 CTLTYPE_INT
, "prefered_oldsa", NULL
,
8221 NULL
, 0, &key_prefered_oldsa
, 0,
8222 CTL_NET
, PF_KEY
, KEYCTL_PREFERED_OLDSA
, CTL_EOL
);
8223 sysctl_createv(clog
, 0, NULL
, NULL
,
8224 CTLFLAG_PERMANENT
|CTLFLAG_READWRITE
,
8225 CTLTYPE_INT
, "esp_auth", NULL
,
8226 NULL
, 0, &ipsec_esp_auth
, 0,
8227 CTL_NET
, FAST_IPSEC_PFKEY
, KEYCTL_ESP_AUTH
, CTL_EOL
);
8228 sysctl_createv(clog
, 0, NULL
, NULL
,
8229 CTLFLAG_PERMANENT
|CTLFLAG_READWRITE
,
8230 CTLTYPE_INT
, "ah_keymin", NULL
,
8231 NULL
, 0, &ipsec_ah_keymin
, 0,
8232 CTL_NET
, FAST_IPSEC_PFKEY
, KEYCTL_AH_KEYMIN
, CTL_EOL
);
8233 sysctl_createv(clog
, 0, NULL
, NULL
,
8235 CTLTYPE_STRUCT
, "stats",
8236 SYSCTL_DESCR("PF_KEY statistics"),
8237 sysctl_net_key_stats
, 0, NULL
, 0,
8238 CTL_NET
, FAST_IPSEC_PFKEY
, CTL_CREATE
, CTL_EOL
);
8242 * Register sysctl names used by setkey(8). For historical reasons,
8243 * and to share a single API, these names appear under { CTL_NET, PF_KEY }
8244 * for both FAST_IPSEC and KAME IPSEC.
8246 SYSCTL_SETUP(sysctl_net_key_compat_setup
, "sysctl net.key subtree setup for FAST_IPSEC")
8249 /* Make sure net.key exists before we register nodes underneath it. */
8250 sysctl_createv(clog
, 0, NULL
, NULL
,
8252 CTLTYPE_NODE
, "net", NULL
,
8255 sysctl_createv(clog
, 0, NULL
, NULL
,
8257 CTLTYPE_NODE
, "key", NULL
,
8259 CTL_NET
, PF_KEY
, CTL_EOL
);
8261 /* Register the net.key.dump{sa,sp} nodes used by setkey(8). */
8262 sysctl_createv(clog
, 0, NULL
, NULL
,
8264 CTLTYPE_STRUCT
, "dumpsa", NULL
,
8265 sysctl_net_key_dumpsa
, 0, NULL
, 0,
8266 CTL_NET
, PF_KEY
, KEYCTL_DUMPSA
, CTL_EOL
);
8267 sysctl_createv(clog
, 0, NULL
, NULL
,
8269 CTLTYPE_STRUCT
, "dumpsp", NULL
,
8270 sysctl_net_key_dumpsp
, 0, NULL
, 0,
8271 CTL_NET
, PF_KEY
, KEYCTL_DUMPSP
, CTL_EOL
);