1 /* $FreeBSD: src/sys/netkey/key.c,v 1.16.2.13 2002/07/24 18:17:40 ume Exp $ */
2 /* $KAME: key.c,v 1.191 2001/06/27 10:46:49 sakane Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * This code is referd to RFC 2367
38 #include "opt_inet6.h"
39 #include "opt_ipsec.h"
41 #include <sys/types.h>
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
46 #include <sys/domain.h>
47 #include <sys/protosw.h>
48 #include <sys/malloc.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/sysctl.h>
52 #include <sys/errno.h>
54 #include <sys/queue.h>
55 #include <sys/syslog.h>
56 #include <sys/thread2.h>
59 #include <net/route.h>
60 #include <net/raw_cb.h>
62 #include <netinet/in.h>
63 #include <netinet/in_systm.h>
64 #include <netinet/ip.h>
65 #include <netinet/in_var.h>
68 #include <netinet/ip6.h>
69 #include <netinet6/in6_var.h>
70 #include <netinet6/ip6_var.h>
74 #include <netinet/in_pcb.h>
77 #include <netinet6/in6_pcb.h>
80 #include <net/pfkeyv2.h>
84 #include "key_debug.h"
86 #include <netinet6/ipsec.h>
88 #include <netinet6/ipsec6.h>
90 #include <netinet6/ah.h>
92 #include <netinet6/ah6.h>
95 #include <netinet6/esp.h>
97 #include <netinet6/esp6.h>
100 #include <netinet6/ipcomp.h>
102 #include <netinet6/ipcomp6.h>
105 #include <machine/stdarg.h>
108 #include <sys/random.h>
110 #include <net/net_osdep.h>
113 #define satosin(s) ((struct sockaddr_in *)s)
116 #define FULLMASK 0xff
119 * Note on SA reference counting:
120 * - SAs that are not in DEAD state will have (total external reference + 1)
121 * following value in reference count field. they cannot be freed and are
122 * referenced from SA header.
123 * - SAs that are in DEAD state will have (total external reference)
124 * in reference count field. they are ready to be freed. reference from
125 * SA header will be removed in key_delsav(), when the reference count
126 * field hits 0 (= no external reference other than from SA header.
130 static struct callout key_timehandler_ch
;
132 u_int32_t key_debug_level
= 0;
133 static u_int key_spi_trycnt
= 1000;
134 static u_int32_t key_spi_minval
= 0x100;
135 static u_int32_t key_spi_maxval
= 0x0fffffff; /* XXX */
136 static u_int32_t policy_id
= 0;
137 static u_int key_int_random
= 60; /*interval to initialize randseed,1(m)*/
138 static u_int key_larval_lifetime
= 30; /* interval to expire acquiring, 30(s)*/
139 static int key_blockacq_count
= 10; /* counter for blocking SADB_ACQUIRE.*/
140 static int key_blockacq_lifetime
= 20; /* lifetime for blocking SADB_ACQUIRE.*/
141 static int key_preferred_oldsa
= 1; /* preferred old sa rather than new sa.*/
143 static u_int32_t acq_seq
= 0;
144 static int key_tick_init_random
= 0;
146 static LIST_HEAD(_sptree
, secpolicy
) sptree
[IPSEC_DIR_MAX
]; /* SPD */
147 static LIST_HEAD(_sahtree
, secashead
) sahtree
; /* SAD */
148 static LIST_HEAD(_regtree
, secreg
) regtree
[SADB_SATYPE_MAX
+ 1];
150 #ifndef IPSEC_NONBLOCK_ACQUIRE
151 static LIST_HEAD(_acqtree
, secacq
) acqtree
; /* acquiring list */
153 static LIST_HEAD(_spacqtree
, secspacq
) spacqtree
; /* SP acquiring list */
155 struct lwkt_token key_token
= LWKT_TOKEN_INITIALIZER(key_token
);
157 struct key_cb key_cb
;
159 /* search order for SAs */
160 static const u_int saorder_state_valid_prefer_old
[] = {
161 SADB_SASTATE_DYING
, SADB_SASTATE_MATURE
,
163 static const u_int saorder_state_valid_prefer_new
[] = {
164 SADB_SASTATE_MATURE
, SADB_SASTATE_DYING
,
166 static const u_int saorder_state_alive
[] = {
168 SADB_SASTATE_MATURE
, SADB_SASTATE_DYING
, SADB_SASTATE_LARVAL
170 static const u_int saorder_state_any
[] = {
171 SADB_SASTATE_MATURE
, SADB_SASTATE_DYING
,
172 SADB_SASTATE_LARVAL
, SADB_SASTATE_DEAD
175 static const int minsize
[] = {
176 sizeof(struct sadb_msg
), /* SADB_EXT_RESERVED */
177 sizeof(struct sadb_sa
), /* SADB_EXT_SA */
178 sizeof(struct sadb_lifetime
), /* SADB_EXT_LIFETIME_CURRENT */
179 sizeof(struct sadb_lifetime
), /* SADB_EXT_LIFETIME_HARD */
180 sizeof(struct sadb_lifetime
), /* SADB_EXT_LIFETIME_SOFT */
181 sizeof(struct sadb_address
), /* SADB_EXT_ADDRESS_SRC */
182 sizeof(struct sadb_address
), /* SADB_EXT_ADDRESS_DST */
183 sizeof(struct sadb_address
), /* SADB_EXT_ADDRESS_PROXY */
184 sizeof(struct sadb_key
), /* SADB_EXT_KEY_AUTH */
185 sizeof(struct sadb_key
), /* SADB_EXT_KEY_ENCRYPT */
186 sizeof(struct sadb_ident
), /* SADB_EXT_IDENTITY_SRC */
187 sizeof(struct sadb_ident
), /* SADB_EXT_IDENTITY_DST */
188 sizeof(struct sadb_sens
), /* SADB_EXT_SENSITIVITY */
189 sizeof(struct sadb_prop
), /* SADB_EXT_PROPOSAL */
190 sizeof(struct sadb_supported
), /* SADB_EXT_SUPPORTED_AUTH */
191 sizeof(struct sadb_supported
), /* SADB_EXT_SUPPORTED_ENCRYPT */
192 sizeof(struct sadb_spirange
), /* SADB_EXT_SPIRANGE */
193 0, /* SADB_X_EXT_KMPRIVATE */
194 sizeof(struct sadb_x_policy
), /* SADB_X_EXT_POLICY */
195 sizeof(struct sadb_x_sa2
), /* SADB_X_SA2 */
197 static const int maxsize
[] = {
198 sizeof(struct sadb_msg
), /* SADB_EXT_RESERVED */
199 sizeof(struct sadb_sa
), /* SADB_EXT_SA */
200 sizeof(struct sadb_lifetime
), /* SADB_EXT_LIFETIME_CURRENT */
201 sizeof(struct sadb_lifetime
), /* SADB_EXT_LIFETIME_HARD */
202 sizeof(struct sadb_lifetime
), /* SADB_EXT_LIFETIME_SOFT */
203 0, /* SADB_EXT_ADDRESS_SRC */
204 0, /* SADB_EXT_ADDRESS_DST */
205 0, /* SADB_EXT_ADDRESS_PROXY */
206 0, /* SADB_EXT_KEY_AUTH */
207 0, /* SADB_EXT_KEY_ENCRYPT */
208 0, /* SADB_EXT_IDENTITY_SRC */
209 0, /* SADB_EXT_IDENTITY_DST */
210 0, /* SADB_EXT_SENSITIVITY */
211 0, /* SADB_EXT_PROPOSAL */
212 0, /* SADB_EXT_SUPPORTED_AUTH */
213 0, /* SADB_EXT_SUPPORTED_ENCRYPT */
214 sizeof(struct sadb_spirange
), /* SADB_EXT_SPIRANGE */
215 0, /* SADB_X_EXT_KMPRIVATE */
216 0, /* SADB_X_EXT_POLICY */
217 sizeof(struct sadb_x_sa2
), /* SADB_X_SA2 */
220 static int ipsec_esp_keymin
= 256;
221 static int ipsec_esp_auth
= 0;
222 static int ipsec_ah_keymin
= 128;
225 SYSCTL_DECL(_net_key
);
228 SYSCTL_INT(_net_key
, KEYCTL_DEBUG_LEVEL
, debug
, CTLFLAG_RW
, \
229 &key_debug_level
, 0, "");
231 /* max count of trial for the decision of spi value */
232 SYSCTL_INT(_net_key
, KEYCTL_SPI_TRY
, spi_trycnt
, CTLFLAG_RW
, \
233 &key_spi_trycnt
, 0, "");
235 /* minimum spi value to allocate automatically. */
236 SYSCTL_INT(_net_key
, KEYCTL_SPI_MIN_VALUE
, spi_minval
, CTLFLAG_RW
, \
237 &key_spi_minval
, 0, "");
239 /* maximun spi value to allocate automatically. */
240 SYSCTL_INT(_net_key
, KEYCTL_SPI_MAX_VALUE
, spi_maxval
, CTLFLAG_RW
, \
241 &key_spi_maxval
, 0, "");
243 /* interval to initialize randseed */
244 SYSCTL_INT(_net_key
, KEYCTL_RANDOM_INT
, int_random
, CTLFLAG_RW
, \
245 &key_int_random
, 0, "");
247 /* lifetime for larval SA */
248 SYSCTL_INT(_net_key
, KEYCTL_LARVAL_LIFETIME
, larval_lifetime
, CTLFLAG_RW
, \
249 &key_larval_lifetime
, 0, "");
251 /* counter for blocking to send SADB_ACQUIRE to IKEd */
252 SYSCTL_INT(_net_key
, KEYCTL_BLOCKACQ_COUNT
, blockacq_count
, CTLFLAG_RW
, \
253 &key_blockacq_count
, 0, "");
255 /* lifetime for blocking to send SADB_ACQUIRE to IKEd */
256 SYSCTL_INT(_net_key
, KEYCTL_BLOCKACQ_LIFETIME
, blockacq_lifetime
, CTLFLAG_RW
, \
257 &key_blockacq_lifetime
, 0, "");
260 SYSCTL_INT(_net_key
, KEYCTL_ESP_AUTH
, esp_auth
, CTLFLAG_RW
, \
261 &ipsec_esp_auth
, 0, "");
263 /* minimum ESP key length */
264 SYSCTL_INT(_net_key
, KEYCTL_ESP_KEYMIN
, esp_keymin
, CTLFLAG_RW
, \
265 &ipsec_esp_keymin
, 0, "");
267 /* minimum AH key length */
268 SYSCTL_INT(_net_key
, KEYCTL_AH_KEYMIN
, ah_keymin
, CTLFLAG_RW
, \
269 &ipsec_ah_keymin
, 0, "");
271 /* perfered old SA rather than new SA */
272 SYSCTL_INT(_net_key
, KEYCTL_PREFERED_OLDSA
, prefered_oldsa
, CTLFLAG_RW
,\
273 &key_preferred_oldsa
, 0, "");
276 #define LIST_FOREACH(elm, head, field) \
277 for (elm = LIST_FIRST(head); elm; elm = LIST_NEXT(elm, field))
279 #define __LIST_CHAINED(elm) \
280 (!((elm)->chain.le_next == NULL && (elm)->chain.le_prev == NULL))
281 #define LIST_INSERT_TAIL(head, elm, type, field) \
283 struct type *curelm = LIST_FIRST(head); \
284 if (curelm == NULL) {\
285 LIST_INSERT_HEAD(head, elm, field); \
287 while (LIST_NEXT(curelm, field)) \
288 curelm = LIST_NEXT(curelm, field);\
289 LIST_INSERT_AFTER(curelm, elm, field);\
293 #define KEY_CHKSASTATE(head, sav, name) \
295 if ((head) != (sav)) { \
296 ipseclog((LOG_DEBUG, "%s: state mismatched (TREE=%d SA=%d)\n", \
297 (name), (head), (sav))); \
302 #define KEY_CHKSPDIR(head, sp, name) \
304 if ((head) != (sp)) { \
305 ipseclog((LOG_DEBUG, "%s: direction mismatched (TREE=%d SP=%d), " \
306 "anyway continue.\n", \
307 (name), (head), (sp))); \
312 #define KMALLOC(p, t, n) \
313 ((p) = (t) kmalloc((unsigned long)(n), M_SECA, M_INTWAIT | M_NULLOK))
315 kfree((caddr_t)(p), M_SECA);
317 #define KMALLOC(p, t, n) \
319 ((p) = (t)kmalloc((unsigned long)(n), M_SECA, M_INTWAIT | M_NULLOK)); \
320 kprintf("%s %d: %p <- KMALLOC(%s, %d)\n", \
321 __FILE__, __LINE__, (p), #t, n); \
326 kprintf("%s %d: %p -> KFREE()\n", __FILE__, __LINE__, (p)); \
327 kfree((caddr_t)(p), M_SECA); \
332 * set parameters into secpolicyindex buffer.
333 * Must allocate secpolicyindex buffer passed to this function.
335 #define KEY_SETSECSPIDX(_dir, s, d, ps, pd, ulp, idx) \
337 bzero((idx), sizeof(struct secpolicyindex)); \
338 (idx)->dir = (_dir); \
339 (idx)->prefs = (ps); \
340 (idx)->prefd = (pd); \
341 (idx)->ul_proto = (ulp); \
342 bcopy((s), &(idx)->src, ((struct sockaddr *)(s))->sa_len); \
343 bcopy((d), &(idx)->dst, ((struct sockaddr *)(d))->sa_len); \
347 * set parameters into secasindex buffer.
348 * Must allocate secasindex buffer before calling this function.
350 #define KEY_SETSECASIDX(p, m, r, s, d, idx) \
352 bzero((idx), sizeof(struct secasindex)); \
353 (idx)->proto = (p); \
355 (idx)->reqid = (r); \
356 bcopy((s), &(idx)->src, ((struct sockaddr *)(s))->sa_len); \
357 bcopy((d), &(idx)->dst, ((struct sockaddr *)(d))->sa_len); \
362 u_long getspi_count
; /* the avarage of count to try to get new SPI */
366 struct sadb_msg
*msg
;
367 struct sadb_ext
*ext
[SADB_EXT_MAX
+ 1];
368 int extoff
[SADB_EXT_MAX
+ 1];
369 int extlen
[SADB_EXT_MAX
+ 1];
372 static struct secasvar
*key_allocsa_policy (struct secasindex
*);
373 static void key_freesp_so (struct secpolicy
**);
374 static struct secasvar
*key_do_allocsa_policy (struct secashead
*, u_int
);
375 static void key_delsp (struct secpolicy
*);
376 static struct secpolicy
*key_getsp (struct secpolicyindex
*);
377 static struct secpolicy
*key_getspbyid (u_int32_t
);
378 static u_int32_t
key_newreqid (void);
379 static struct mbuf
*key_gather_mbuf (struct mbuf
*,
380 const struct sadb_msghdr
*, int, int, ...);
381 static int key_spdadd (struct socket
*, struct mbuf
*,
382 const struct sadb_msghdr
*);
383 static u_int32_t
key_getnewspid (void);
384 static int key_spddelete (struct socket
*, struct mbuf
*,
385 const struct sadb_msghdr
*);
386 static int key_spddelete2 (struct socket
*, struct mbuf
*,
387 const struct sadb_msghdr
*);
388 static int key_spdget (struct socket
*, struct mbuf
*,
389 const struct sadb_msghdr
*);
390 static int key_spdflush (struct socket
*, struct mbuf
*,
391 const struct sadb_msghdr
*);
392 static int key_spddump (struct socket
*, struct mbuf
*,
393 const struct sadb_msghdr
*);
394 static struct mbuf
*key_setdumpsp (struct secpolicy
*,
395 u_int8_t
, u_int32_t
, u_int32_t
);
396 static u_int
key_getspreqmsglen (struct secpolicy
*);
397 static int key_spdexpire (struct secpolicy
*);
398 static struct secashead
*key_newsah (struct secasindex
*);
399 static void key_delsah (struct secashead
*);
400 static struct secasvar
*key_newsav (struct mbuf
*,
401 const struct sadb_msghdr
*, struct secashead
*, int *);
402 static void key_delsav (struct secasvar
*);
403 static struct secashead
*key_getsah (struct secasindex
*);
404 static struct secasvar
*key_checkspidup (struct secasindex
*, u_int32_t
);
405 static struct secasvar
*key_getsavbyspi (struct secashead
*, u_int32_t
);
406 static int key_setsaval (struct secasvar
*, struct mbuf
*,
407 const struct sadb_msghdr
*);
408 static int key_mature (struct secasvar
*);
409 static struct mbuf
*key_setdumpsa (struct secasvar
*, u_int8_t
,
410 u_int8_t
, u_int32_t
, u_int32_t
);
411 static struct mbuf
*key_setsadbmsg (u_int8_t
, u_int16_t
, u_int8_t
,
412 u_int32_t
, pid_t
, u_int16_t
);
413 static struct mbuf
*key_setsadbsa (struct secasvar
*);
414 static struct mbuf
*key_setsadbaddr (u_int16_t
,
415 struct sockaddr
*, u_int8_t
, u_int16_t
);
417 static struct mbuf
*key_setsadbident (u_int16_t
, u_int16_t
, caddr_t
,
420 static struct mbuf
*key_setsadbxsa2 (u_int8_t
, u_int32_t
, u_int32_t
);
421 static struct mbuf
*key_setsadbxpolicy (u_int16_t
, u_int8_t
,
423 static void *key_newbuf (const void *, u_int
);
425 static int key_ismyaddr6 (struct sockaddr_in6
*);
428 /* flags for key_cmpsaidx() */
429 #define CMP_HEAD 1 /* protocol, addresses. */
430 #define CMP_MODE_REQID 2 /* additionally HEAD, reqid, mode. */
431 #define CMP_REQID 3 /* additionally HEAD, reaid. */
432 #define CMP_EXACTLY 4 /* all elements. */
433 static int key_cmpsaidx
434 (struct secasindex
*, struct secasindex
*, int);
436 static int key_cmpspidx_exactly
437 (struct secpolicyindex
*, struct secpolicyindex
*);
438 static int key_cmpspidx_withmask
439 (struct secpolicyindex
*, struct secpolicyindex
*);
440 static int key_sockaddrcmp (struct sockaddr
*, struct sockaddr
*, int);
441 static int key_bbcmp (caddr_t
, caddr_t
, u_int
);
442 static void key_srandom (void);
443 static u_int16_t
key_satype2proto (u_int8_t
);
444 static u_int8_t
key_proto2satype (u_int16_t
);
446 static int key_getspi (struct socket
*, struct mbuf
*,
447 const struct sadb_msghdr
*);
448 static u_int32_t
key_do_getnewspi (struct sadb_spirange
*,
449 struct secasindex
*);
450 static int key_update (struct socket
*, struct mbuf
*,
451 const struct sadb_msghdr
*);
452 #ifdef IPSEC_DOSEQCHECK
453 static struct secasvar
*key_getsavbyseq (struct secashead
*, u_int32_t
);
455 static int key_add (struct socket
*, struct mbuf
*,
456 const struct sadb_msghdr
*);
457 static int key_setident (struct secashead
*, struct mbuf
*,
458 const struct sadb_msghdr
*);
459 static struct mbuf
*key_getmsgbuf_x1 (struct mbuf
*,
460 const struct sadb_msghdr
*);
461 static int key_delete (struct socket
*, struct mbuf
*,
462 const struct sadb_msghdr
*);
463 static int key_get (struct socket
*, struct mbuf
*,
464 const struct sadb_msghdr
*);
466 static void key_getcomb_setlifetime (struct sadb_comb
*);
468 static struct mbuf
*key_getcomb_esp (void);
470 static struct mbuf
*key_getcomb_ah (void);
471 static struct mbuf
*key_getcomb_ipcomp (void);
472 static struct mbuf
*key_getprop (const struct secasindex
*);
474 static int key_acquire (struct secasindex
*, struct secpolicy
*);
475 #ifndef IPSEC_NONBLOCK_ACQUIRE
476 static struct secacq
*key_newacq (struct secasindex
*);
477 static struct secacq
*key_getacq (struct secasindex
*);
478 static struct secacq
*key_getacqbyseq (u_int32_t
);
480 static struct secspacq
*key_newspacq (struct secpolicyindex
*);
481 static struct secspacq
*key_getspacq (struct secpolicyindex
*);
482 static int key_acquire2 (struct socket
*, struct mbuf
*,
483 const struct sadb_msghdr
*);
484 static int key_register (struct socket
*, struct mbuf
*,
485 const struct sadb_msghdr
*);
486 static int key_expire (struct secasvar
*);
487 static int key_flush (struct socket
*, struct mbuf
*,
488 const struct sadb_msghdr
*);
489 static int key_dump (struct socket
*, struct mbuf
*,
490 const struct sadb_msghdr
*);
491 static int key_promisc (struct socket
*, struct mbuf
*,
492 const struct sadb_msghdr
*);
493 static int key_senderror (struct socket
*, struct mbuf
*, int);
494 static int key_validate_ext (const struct sadb_ext
*, int);
495 static int key_align (struct mbuf
*, struct sadb_msghdr
*);
497 static const char *key_getfqdn (void);
498 static const char *key_getuserfqdn (void);
500 static void key_sa_chgstate (struct secasvar
*, u_int8_t
);
501 static struct mbuf
*key_alloc_mbuf (int);
503 /* %%% IPsec policy management */
505 * allocating a SP for OUTBOUND or INBOUND packet.
506 * Must call key_freesp() later.
507 * OUT: NULL: not found
508 * others: found and return the pointer.
511 key_allocsp(struct secpolicyindex
*spidx
, u_int dir
)
513 struct secpolicy
*sp
;
518 panic("key_allocsp: NULL pointer is passed.");
520 /* check direction */
522 case IPSEC_DIR_INBOUND
:
523 case IPSEC_DIR_OUTBOUND
:
526 panic("key_allocsp: Invalid direction is passed.");
530 lwkt_gettoken(&key_token
);
531 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
532 kprintf("*** objects\n");
533 kdebug_secpolicyindex(spidx
));
535 LIST_FOREACH(sp
, &sptree
[dir
], chain
) {
536 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
537 kprintf("*** in SPD\n");
538 kdebug_secpolicyindex(&sp
->spidx
));
540 if (sp
->state
== IPSEC_SPSTATE_DEAD
)
542 if (key_cmpspidx_withmask(&sp
->spidx
, spidx
))
546 lwkt_reltoken(&key_token
);
551 KEY_CHKSPDIR(sp
->spidx
.dir
, dir
, "key_allocsp");
553 /* found a SPD entry */
556 sp
->lastused
= tv
.tv_sec
;
557 lwkt_reltoken(&key_token
);
558 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
559 kprintf("DP key_allocsp cause refcnt++:%d SP:%p\n",
566 * return a policy that matches this particular inbound packet.
570 key_gettunnel(struct sockaddr
*osrc
, struct sockaddr
*odst
,
571 struct sockaddr
*isrc
, struct sockaddr
*idst
)
573 struct secpolicy
*sp
;
574 const int dir
= IPSEC_DIR_INBOUND
;
576 struct ipsecrequest
*r1
, *r2
, *p
;
577 struct sockaddr
*os
, *od
, *is
, *id
;
578 struct secpolicyindex spidx
;
580 if (isrc
->sa_family
!= idst
->sa_family
) {
581 ipseclog((LOG_ERR
, "protocol family mismatched %d != %d\n.",
582 isrc
->sa_family
, idst
->sa_family
));
586 lwkt_gettoken(&key_token
);
587 LIST_FOREACH(sp
, &sptree
[dir
], chain
) {
588 if (sp
->state
== IPSEC_SPSTATE_DEAD
)
592 for (p
= sp
->req
; p
; p
= p
->next
) {
593 if (p
->saidx
.mode
!= IPSEC_MODE_TUNNEL
)
600 /* here we look at address matches only */
602 if (isrc
->sa_len
> sizeof(spidx
.src
) ||
603 idst
->sa_len
> sizeof(spidx
.dst
))
605 bcopy(isrc
, &spidx
.src
, isrc
->sa_len
);
606 bcopy(idst
, &spidx
.dst
, idst
->sa_len
);
607 if (!key_cmpspidx_withmask(&sp
->spidx
, &spidx
))
610 is
= (struct sockaddr
*)&r1
->saidx
.src
;
611 id
= (struct sockaddr
*)&r1
->saidx
.dst
;
612 if (key_sockaddrcmp(is
, isrc
, 0) ||
613 key_sockaddrcmp(id
, idst
, 0))
617 os
= (struct sockaddr
*)&r2
->saidx
.src
;
618 od
= (struct sockaddr
*)&r2
->saidx
.dst
;
619 if (key_sockaddrcmp(os
, osrc
, 0) ||
620 key_sockaddrcmp(od
, odst
, 0))
626 lwkt_reltoken(&key_token
);
632 sp
->lastused
= tv
.tv_sec
;
633 lwkt_reltoken(&key_token
);
638 * allocating an SA entry for an *OUTBOUND* packet.
639 * checking each request entries in SP, and acquire an SA if need.
640 * OUT: 0: there are valid requests.
641 * ENOENT: policy may be valid, but SA with REQUIRE is on acquiring.
644 key_checkrequest(struct ipsecrequest
*isr
, struct secasindex
*saidx
)
650 if (isr
== NULL
|| saidx
== NULL
)
651 panic("key_checkrequest: NULL pointer is passed.");
654 switch (saidx
->mode
) {
655 case IPSEC_MODE_TRANSPORT
:
656 case IPSEC_MODE_TUNNEL
:
660 panic("key_checkrequest: Invalid policy defined.");
663 lwkt_gettoken(&key_token
);
665 /* get current level */
666 level
= ipsec_get_reqlevel(isr
);
670 * We do allocate new SA only if the state of SA in the holder is
671 * SADB_SASTATE_DEAD. The SA for outbound must be the oldest.
673 if (isr
->sav
!= NULL
) {
674 if (isr
->sav
->sah
== NULL
)
675 panic("key_checkrequest: sah is null.");
676 if (isr
->sav
== (struct secasvar
*)LIST_FIRST(
677 &isr
->sav
->sah
->savtree
[SADB_SASTATE_DEAD
])) {
678 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
679 kprintf("DP checkrequest calls free SA:%p\n",
681 key_freesav(isr
->sav
);
687 * we free any SA stashed in the IPsec request because a different
688 * SA may be involved each time this request is checked, either
689 * because new SAs are being configured, or this request is
690 * associated with an unconnected datagram socket, or this request
691 * is associated with a system default policy.
693 * The operation may have negative impact to performance. We may
694 * want to check cached SA carefully, rather than picking new SA
697 if (isr
->sav
!= NULL
) {
698 key_freesav(isr
->sav
);
704 * new SA allocation if no SA found.
705 * key_allocsa_policy should allocate the oldest SA available.
706 * See key_do_allocsa_policy(), and draft-jenkins-ipsec-rekeying-03.txt.
708 if (isr
->sav
== NULL
)
709 isr
->sav
= key_allocsa_policy(saidx
);
711 /* When there is SA. */
712 if (isr
->sav
!= NULL
) {
713 lwkt_reltoken(&key_token
);
718 if ((error
= key_acquire(saidx
, isr
->sp
)) != 0) {
719 /* XXX What should I do ? */
720 ipseclog((LOG_DEBUG
, "key_checkrequest: error %d returned "
721 "from key_acquire.\n", error
));
722 lwkt_reltoken(&key_token
);
726 lwkt_reltoken(&key_token
);
727 return level
== IPSEC_LEVEL_REQUIRE
? ENOENT
: 0;
731 * allocating a SA for policy entry from SAD.
732 * NOTE: searching SAD of aliving state.
733 * OUT: NULL: not found.
734 * others: found and return the pointer.
736 static struct secasvar
*
737 key_allocsa_policy(struct secasindex
*saidx
)
739 struct secashead
*sah
;
740 struct secasvar
*sav
;
741 u_int stateidx
, state
;
742 const u_int
*saorder_state_valid
;
745 LIST_FOREACH(sah
, &sahtree
, chain
) {
746 if (sah
->state
== SADB_SASTATE_DEAD
)
748 if (key_cmpsaidx(&sah
->saidx
, saidx
, CMP_MODE_REQID
))
757 * search a valid state list for outbound packet.
758 * This search order is important.
760 if (key_preferred_oldsa
) {
761 saorder_state_valid
= saorder_state_valid_prefer_old
;
762 arraysize
= NELEM(saorder_state_valid_prefer_old
);
764 saorder_state_valid
= saorder_state_valid_prefer_new
;
765 arraysize
= NELEM(saorder_state_valid_prefer_new
);
768 for (stateidx
= 0; stateidx
< arraysize
; stateidx
++) {
770 state
= saorder_state_valid
[stateidx
];
772 sav
= key_do_allocsa_policy(sah
, state
);
781 * searching SAD with direction, protocol, mode and state.
782 * called by key_allocsa_policy().
785 * others : found, pointer to a SA.
787 static struct secasvar
*
788 key_do_allocsa_policy(struct secashead
*sah
, u_int state
)
790 struct secasvar
*sav
, *nextsav
, *candidate
, *d
;
795 for (sav
= LIST_FIRST(&sah
->savtree
[state
]);
799 nextsav
= LIST_NEXT(sav
, chain
);
802 KEY_CHKSASTATE(sav
->state
, state
, "key_do_allocsa_policy");
805 if (candidate
== NULL
) {
810 /* Which SA is the better ? */
813 if (candidate
->lft_c
== NULL
|| sav
->lft_c
== NULL
)
814 panic("key_do_allocsa_policy: "
815 "lifetime_current is NULL.\n");
817 /* What the best method is to compare ? */
818 if (key_preferred_oldsa
) {
819 if (candidate
->lft_c
->sadb_lifetime_addtime
>
820 sav
->lft_c
->sadb_lifetime_addtime
) {
827 /* prefered new sa rather than old sa */
828 if (candidate
->lft_c
->sadb_lifetime_addtime
<
829 sav
->lft_c
->sadb_lifetime_addtime
) {
836 * prepared to delete the SA when there is more
837 * suitable candidate and the lifetime of the SA is not
840 if (d
->lft_c
->sadb_lifetime_addtime
!= 0) {
841 struct mbuf
*m
, *result
;
843 key_sa_chgstate(d
, SADB_SASTATE_DEAD
);
845 m
= key_setsadbmsg(SADB_DELETE
, 0,
846 d
->sah
->saidx
.proto
, 0, 0, d
->refcnt
- 1);
851 /* set sadb_address for saidx's. */
852 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
853 (struct sockaddr
*)&d
->sah
->saidx
.src
,
854 d
->sah
->saidx
.src
.ss_len
<< 3,
860 /* set sadb_address for saidx's. */
861 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
862 (struct sockaddr
*)&d
->sah
->saidx
.src
,
863 d
->sah
->saidx
.src
.ss_len
<< 3,
869 /* create SA extension */
870 m
= key_setsadbsa(d
);
875 if (result
->m_len
< sizeof(struct sadb_msg
)) {
876 result
= m_pullup(result
,
877 sizeof(struct sadb_msg
));
882 result
->m_pkthdr
.len
= 0;
883 for (m
= result
; m
; m
= m
->m_next
)
884 result
->m_pkthdr
.len
+= m
->m_len
;
885 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
886 PFKEY_UNIT64(result
->m_pkthdr
.len
);
888 if (key_sendup_mbuf(NULL
, result
,
889 KEY_SENDUP_REGISTERED
))
898 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
899 kprintf("DP allocsa_policy cause "
900 "refcnt++:%d SA:%p\n",
901 candidate
->refcnt
, candidate
));
907 * allocating a SA entry for a *INBOUND* packet.
908 * Must call key_freesav() later.
909 * OUT: positive: pointer to a sav.
910 * NULL: not found, or error occured.
912 * In the comparison, source address will be ignored for RFC2401 conformance.
913 * To quote, from section 4.1:
914 * A security association is uniquely identified by a triple consisting
915 * of a Security Parameter Index (SPI), an IP Destination Address, and a
916 * security protocol (AH or ESP) identifier.
917 * Note that, however, we do need to keep source address in IPsec SA.
918 * IKE specification and PF_KEY specification do assume that we
919 * keep source address in IPsec SA. We see a tricky situation here.
922 key_allocsa(u_int family
, caddr_t src
, caddr_t dst
, u_int proto
,
925 struct secashead
*sah
;
926 struct secasvar
*sav
;
927 u_int stateidx
, state
;
928 struct sockaddr_in sin
;
929 struct sockaddr_in6 sin6
;
930 const u_int
*saorder_state_valid
;
934 if (src
== NULL
|| dst
== NULL
)
935 panic("key_allocsa: NULL pointer is passed.");
938 * when both systems employ similar strategy to use a SA.
939 * the search order is important even in the inbound case.
941 if (key_preferred_oldsa
) {
942 saorder_state_valid
= saorder_state_valid_prefer_old
;
943 arraysize
= NELEM(saorder_state_valid_prefer_old
);
945 saorder_state_valid
= saorder_state_valid_prefer_new
;
946 arraysize
= NELEM(saorder_state_valid_prefer_new
);
951 * XXX: to be checked internal IP header somewhere. Also when
952 * IPsec tunnel packet is received. But ESP tunnel mode is
953 * encrypted so we can't check internal IP header.
955 lwkt_gettoken(&key_token
);
956 LIST_FOREACH(sah
, &sahtree
, chain
) {
958 * search a valid state list for inbound packet.
959 * the search order is not important.
961 for (stateidx
= 0; stateidx
< arraysize
; stateidx
++) {
962 state
= saorder_state_valid
[stateidx
];
963 LIST_FOREACH(sav
, &sah
->savtree
[state
], chain
) {
965 KEY_CHKSASTATE(sav
->state
, state
, "key_allocsav");
966 if (proto
!= sav
->sah
->saidx
.proto
)
970 if (family
!= sav
->sah
->saidx
.src
.ss_family
||
971 family
!= sav
->sah
->saidx
.dst
.ss_family
)
974 #if 0 /* don't check src */
975 /* check src address */
978 bzero(&sin
, sizeof(sin
));
979 sin
.sin_family
= AF_INET
;
980 sin
.sin_len
= sizeof(sin
);
981 bcopy(src
, &sin
.sin_addr
,
982 sizeof(sin
.sin_addr
));
983 if (key_sockaddrcmp((struct sockaddr
*)&sin
,
984 (struct sockaddr
*)&sav
->sah
->saidx
.src
, 0) != 0)
989 bzero(&sin6
, sizeof(sin6
));
990 sin6
.sin6_family
= AF_INET6
;
991 sin6
.sin6_len
= sizeof(sin6
);
992 bcopy(src
, &sin6
.sin6_addr
,
993 sizeof(sin6
.sin6_addr
));
994 if (IN6_IS_SCOPE_LINKLOCAL(&sin6
.sin6_addr
)) {
995 /* kame fake scopeid */
997 ntohs(sin6
.sin6_addr
.s6_addr16
[1]);
998 sin6
.sin6_addr
.s6_addr16
[1] = 0;
1000 if (key_sockaddrcmp((struct sockaddr
*)&sin6
,
1001 (struct sockaddr
*)&sav
->sah
->saidx
.src
, 0) != 0)
1005 ipseclog((LOG_DEBUG
, "key_allocsa: "
1006 "unknown address family=%d.\n",
1012 /* check dst address */
1015 bzero(&sin
, sizeof(sin
));
1016 sin
.sin_family
= AF_INET
;
1017 sin
.sin_len
= sizeof(sin
);
1018 bcopy(dst
, &sin
.sin_addr
,
1019 sizeof(sin
.sin_addr
));
1020 if (key_sockaddrcmp((struct sockaddr
*)&sin
,
1021 (struct sockaddr
*)&sav
->sah
->saidx
.dst
, 0) != 0)
1026 bzero(&sin6
, sizeof(sin6
));
1027 sin6
.sin6_family
= AF_INET6
;
1028 sin6
.sin6_len
= sizeof(sin6
);
1029 bcopy(dst
, &sin6
.sin6_addr
,
1030 sizeof(sin6
.sin6_addr
));
1031 if (IN6_IS_SCOPE_LINKLOCAL(&sin6
.sin6_addr
)) {
1032 /* kame fake scopeid */
1033 sin6
.sin6_scope_id
=
1034 ntohs(sin6
.sin6_addr
.s6_addr16
[1]);
1035 sin6
.sin6_addr
.s6_addr16
[1] = 0;
1037 if (key_sockaddrcmp((struct sockaddr
*)&sin6
,
1038 (struct sockaddr
*)&sav
->sah
->saidx
.dst
, 0) != 0)
1042 ipseclog((LOG_DEBUG
, "key_allocsa: "
1043 "unknown address family=%d.\n",
1054 lwkt_reltoken(&key_token
);
1059 lwkt_reltoken(&key_token
);
1060 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1061 kprintf("DP allocsa cause refcnt++:%d SA:%p\n",
1067 * Must be called after calling key_allocsp().
1068 * For both the packet without socket and key_freeso().
1071 key_freesp(struct secpolicy
*sp
)
1075 panic("key_freesp: NULL pointer is passed.");
1077 lwkt_gettoken(&key_token
);
1079 if (sp
->refcnt
== 0)
1081 lwkt_reltoken(&key_token
);
1085 * Must be called after calling key_allocsp().
1086 * For the packet with socket.
1089 key_freeso(struct socket
*so
)
1093 panic("key_freeso: NULL pointer is passed.");
1095 lwkt_gettoken(&key_token
);
1096 switch (so
->so_proto
->pr_domain
->dom_family
) {
1100 struct inpcb
*pcb
= so
->so_pcb
;
1102 /* Does it have a PCB ? */
1105 key_freesp_so(&pcb
->inp_sp
->sp_in
);
1106 key_freesp_so(&pcb
->inp_sp
->sp_out
);
1113 #ifdef HAVE_NRL_INPCB
1114 struct inpcb
*pcb
= so
->so_pcb
;
1116 /* Does it have a PCB ? */
1119 key_freesp_so(&pcb
->inp_sp
->sp_in
);
1120 key_freesp_so(&pcb
->inp_sp
->sp_out
);
1122 struct in6pcb
*pcb
= so
->so_pcb
;
1124 /* Does it have a PCB ? */
1127 key_freesp_so(&pcb
->in6p_sp
->sp_in
);
1128 key_freesp_so(&pcb
->in6p_sp
->sp_out
);
1134 ipseclog((LOG_DEBUG
, "key_freeso: unknown address family=%d.\n",
1135 so
->so_proto
->pr_domain
->dom_family
));
1138 lwkt_reltoken(&key_token
);
1142 key_freesp_so(struct secpolicy
**sp
)
1145 if (sp
== NULL
|| *sp
== NULL
)
1146 panic("key_freesp_so: sp == NULL");
1148 switch ((*sp
)->policy
) {
1149 case IPSEC_POLICY_IPSEC
:
1150 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1151 kprintf("DP freeso calls free SP:%p\n", *sp
));
1155 case IPSEC_POLICY_ENTRUST
:
1156 case IPSEC_POLICY_BYPASS
:
1159 panic("key_freesp_so: Invalid policy found %d", (*sp
)->policy
);
1166 * Must be called after calling key_allocsa().
1167 * This function is called by key_freesp() to free some SA allocated
1171 key_freesav(struct secasvar
*sav
)
1175 panic("key_freesav: NULL pointer is passed.");
1177 lwkt_gettoken(&key_token
);
1179 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1180 kprintf("DP freesav cause refcnt--:%d SA:%p SPI %u\n",
1181 sav
->refcnt
, sav
, (u_int32_t
)ntohl(sav
->spi
)));
1183 if (sav
->refcnt
== 0)
1186 lwkt_reltoken(&key_token
);
1189 /* %%% SPD management */
1191 * free security policy entry.
1194 key_delsp(struct secpolicy
*sp
)
1198 panic("key_delsp: NULL pointer is passed.");
1200 sp
->state
= IPSEC_SPSTATE_DEAD
;
1203 return; /* can't free */
1205 /* remove from SP index */
1206 if (__LIST_CHAINED(sp
))
1207 LIST_REMOVE(sp
, chain
);
1210 struct ipsecrequest
*isr
= sp
->req
, *nextisr
;
1212 while (isr
!= NULL
) {
1213 if (isr
->sav
!= NULL
) {
1214 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1215 kprintf("DP delsp calls free SA:%p\n",
1217 key_freesav(isr
->sav
);
1221 nextisr
= isr
->next
;
1226 keydb_delsecpolicy(sp
);
1231 * OUT: NULL : not found
1232 * others : found, pointer to a SP.
1234 static struct secpolicy
*
1235 key_getsp(struct secpolicyindex
*spidx
)
1237 struct secpolicy
*sp
;
1241 panic("key_getsp: NULL pointer is passed.");
1243 LIST_FOREACH(sp
, &sptree
[spidx
->dir
], chain
) {
1244 if (sp
->state
== IPSEC_SPSTATE_DEAD
)
1246 if (key_cmpspidx_exactly(spidx
, &sp
->spidx
)) {
1257 * OUT: NULL : not found
1258 * others : found, pointer to a SP.
1260 static struct secpolicy
*
1261 key_getspbyid(u_int32_t id
)
1263 struct secpolicy
*sp
;
1265 LIST_FOREACH(sp
, &sptree
[IPSEC_DIR_INBOUND
], chain
) {
1266 if (sp
->state
== IPSEC_SPSTATE_DEAD
)
1274 LIST_FOREACH(sp
, &sptree
[IPSEC_DIR_OUTBOUND
], chain
) {
1275 if (sp
->state
== IPSEC_SPSTATE_DEAD
)
1289 struct secpolicy
*newsp
= NULL
;
1291 lwkt_gettoken(&key_token
);
1292 newsp
= keydb_newsecpolicy();
1297 lwkt_reltoken(&key_token
);
1303 * create secpolicy structure from sadb_x_policy structure.
1304 * NOTE: `state', `secpolicyindex' in secpolicy structure are not set,
1305 * so must be set properly later.
1308 key_msg2sp(struct sadb_x_policy
*xpl0
, size_t len
, int *error
)
1310 struct secpolicy
*newsp
;
1314 panic("key_msg2sp: NULL pointer was passed.");
1315 if (len
< sizeof(*xpl0
))
1316 panic("key_msg2sp: invalid length.");
1317 if (len
!= PFKEY_EXTLEN(xpl0
)) {
1318 ipseclog((LOG_DEBUG
, "key_msg2sp: Invalid msg length.\n"));
1323 lwkt_gettoken(&key_token
);
1324 if ((newsp
= key_newsp()) == NULL
) {
1325 lwkt_reltoken(&key_token
);
1330 newsp
->spidx
.dir
= xpl0
->sadb_x_policy_dir
;
1331 newsp
->policy
= xpl0
->sadb_x_policy_type
;
1334 switch (xpl0
->sadb_x_policy_type
) {
1335 case IPSEC_POLICY_DISCARD
:
1336 case IPSEC_POLICY_NONE
:
1337 case IPSEC_POLICY_ENTRUST
:
1338 case IPSEC_POLICY_BYPASS
:
1342 case IPSEC_POLICY_IPSEC
:
1345 struct sadb_x_ipsecrequest
*xisr
;
1346 struct ipsecrequest
**p_isr
= &newsp
->req
;
1348 /* validity check */
1349 if (PFKEY_EXTLEN(xpl0
) < sizeof(*xpl0
)) {
1350 ipseclog((LOG_DEBUG
,
1351 "key_msg2sp: Invalid msg length.\n"));
1353 lwkt_reltoken(&key_token
);
1358 tlen
= PFKEY_EXTLEN(xpl0
) - sizeof(*xpl0
);
1359 xisr
= (struct sadb_x_ipsecrequest
*)(xpl0
+ 1);
1364 if (xisr
->sadb_x_ipsecrequest_len
< sizeof(*xisr
)) {
1365 ipseclog((LOG_DEBUG
, "key_msg2sp: "
1366 "invalid ipsecrequest length.\n"));
1368 lwkt_reltoken(&key_token
);
1373 /* allocate request buffer */
1374 KMALLOC(*p_isr
, struct ipsecrequest
*, sizeof(**p_isr
));
1375 if ((*p_isr
) == NULL
) {
1376 ipseclog((LOG_DEBUG
,
1377 "key_msg2sp: No more memory.\n"));
1379 lwkt_reltoken(&key_token
);
1383 bzero(*p_isr
, sizeof(**p_isr
));
1386 (*p_isr
)->next
= NULL
;
1388 switch (xisr
->sadb_x_ipsecrequest_proto
) {
1391 case IPPROTO_IPCOMP
:
1394 ipseclog((LOG_DEBUG
,
1395 "key_msg2sp: invalid proto type=%u\n",
1396 xisr
->sadb_x_ipsecrequest_proto
));
1398 lwkt_reltoken(&key_token
);
1399 *error
= EPROTONOSUPPORT
;
1402 (*p_isr
)->saidx
.proto
= xisr
->sadb_x_ipsecrequest_proto
;
1404 switch (xisr
->sadb_x_ipsecrequest_mode
) {
1405 case IPSEC_MODE_TRANSPORT
:
1406 case IPSEC_MODE_TUNNEL
:
1408 case IPSEC_MODE_ANY
:
1410 ipseclog((LOG_DEBUG
,
1411 "key_msg2sp: invalid mode=%u\n",
1412 xisr
->sadb_x_ipsecrequest_mode
));
1414 lwkt_reltoken(&key_token
);
1418 (*p_isr
)->saidx
.mode
= xisr
->sadb_x_ipsecrequest_mode
;
1420 switch (xisr
->sadb_x_ipsecrequest_level
) {
1421 case IPSEC_LEVEL_DEFAULT
:
1422 case IPSEC_LEVEL_USE
:
1423 case IPSEC_LEVEL_REQUIRE
:
1425 case IPSEC_LEVEL_UNIQUE
:
1426 /* validity check */
1428 * If range violation of reqid, kernel will
1429 * update it, don't refuse it.
1431 if (xisr
->sadb_x_ipsecrequest_reqid
1432 > IPSEC_MANUAL_REQID_MAX
) {
1433 ipseclog((LOG_DEBUG
,
1434 "key_msg2sp: reqid=%d range "
1435 "violation, updated by kernel.\n",
1436 xisr
->sadb_x_ipsecrequest_reqid
));
1437 xisr
->sadb_x_ipsecrequest_reqid
= 0;
1440 /* allocate new reqid id if reqid is zero. */
1441 if (xisr
->sadb_x_ipsecrequest_reqid
== 0) {
1443 if ((reqid
= key_newreqid()) == 0) {
1445 lwkt_reltoken(&key_token
);
1449 (*p_isr
)->saidx
.reqid
= reqid
;
1450 xisr
->sadb_x_ipsecrequest_reqid
= reqid
;
1452 /* set it for manual keying. */
1453 (*p_isr
)->saidx
.reqid
=
1454 xisr
->sadb_x_ipsecrequest_reqid
;
1459 ipseclog((LOG_DEBUG
, "key_msg2sp: invalid level=%u\n",
1460 xisr
->sadb_x_ipsecrequest_level
));
1462 lwkt_reltoken(&key_token
);
1466 (*p_isr
)->level
= xisr
->sadb_x_ipsecrequest_level
;
1468 /* set IP addresses if there */
1469 if (xisr
->sadb_x_ipsecrequest_len
> sizeof(*xisr
)) {
1470 struct sockaddr
*paddr
;
1472 paddr
= (struct sockaddr
*)(xisr
+ 1);
1474 /* validity check */
1476 > sizeof((*p_isr
)->saidx
.src
)) {
1477 ipseclog((LOG_DEBUG
, "key_msg2sp: invalid request "
1478 "address length.\n"));
1480 lwkt_reltoken(&key_token
);
1484 bcopy(paddr
, &(*p_isr
)->saidx
.src
,
1487 paddr
= (struct sockaddr
*)((caddr_t
)paddr
1490 /* validity check */
1492 > sizeof((*p_isr
)->saidx
.dst
)) {
1493 ipseclog((LOG_DEBUG
, "key_msg2sp: invalid request "
1494 "address length.\n"));
1496 lwkt_reltoken(&key_token
);
1500 bcopy(paddr
, &(*p_isr
)->saidx
.dst
,
1504 (*p_isr
)->sav
= NULL
;
1505 (*p_isr
)->sp
= newsp
;
1507 /* initialization for the next. */
1508 p_isr
= &(*p_isr
)->next
;
1509 tlen
-= xisr
->sadb_x_ipsecrequest_len
;
1511 /* validity check */
1513 ipseclog((LOG_DEBUG
, "key_msg2sp: becoming tlen < 0.\n"));
1515 lwkt_reltoken(&key_token
);
1520 xisr
= (struct sadb_x_ipsecrequest
*)((caddr_t
)xisr
1521 + xisr
->sadb_x_ipsecrequest_len
);
1526 ipseclog((LOG_DEBUG
, "key_msg2sp: invalid policy type.\n"));
1528 lwkt_reltoken(&key_token
);
1532 lwkt_reltoken(&key_token
);
1540 static u_int32_t auto_reqid
= IPSEC_MANUAL_REQID_MAX
+ 1;
1542 auto_reqid
= (auto_reqid
== ~0
1543 ? IPSEC_MANUAL_REQID_MAX
+ 1 : auto_reqid
+ 1);
1545 /* XXX should be unique check */
1551 * copy secpolicy struct to sadb_x_policy structure indicated.
1554 key_sp2msg(struct secpolicy
*sp
)
1556 struct sadb_x_policy
*xpl
;
1563 panic("key_sp2msg: NULL pointer was passed.");
1565 lwkt_gettoken(&key_token
);
1566 tlen
= key_getspreqmsglen(sp
);
1568 m
= key_alloc_mbuf(tlen
);
1569 if (!m
|| m
->m_next
) { /*XXX*/
1572 lwkt_reltoken(&key_token
);
1578 xpl
= mtod(m
, struct sadb_x_policy
*);
1581 xpl
->sadb_x_policy_len
= PFKEY_UNIT64(tlen
);
1582 xpl
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
1583 xpl
->sadb_x_policy_type
= sp
->policy
;
1584 xpl
->sadb_x_policy_dir
= sp
->spidx
.dir
;
1585 xpl
->sadb_x_policy_id
= sp
->id
;
1586 p
= (caddr_t
)xpl
+ sizeof(*xpl
);
1588 /* if is the policy for ipsec ? */
1589 if (sp
->policy
== IPSEC_POLICY_IPSEC
) {
1590 struct sadb_x_ipsecrequest
*xisr
;
1591 struct ipsecrequest
*isr
;
1593 for (isr
= sp
->req
; isr
!= NULL
; isr
= isr
->next
) {
1595 xisr
= (struct sadb_x_ipsecrequest
*)p
;
1597 xisr
->sadb_x_ipsecrequest_proto
= isr
->saidx
.proto
;
1598 xisr
->sadb_x_ipsecrequest_mode
= isr
->saidx
.mode
;
1599 xisr
->sadb_x_ipsecrequest_level
= isr
->level
;
1600 xisr
->sadb_x_ipsecrequest_reqid
= isr
->saidx
.reqid
;
1603 bcopy(&isr
->saidx
.src
, p
, isr
->saidx
.src
.ss_len
);
1604 p
+= isr
->saidx
.src
.ss_len
;
1605 bcopy(&isr
->saidx
.dst
, p
, isr
->saidx
.dst
.ss_len
);
1606 p
+= isr
->saidx
.src
.ss_len
;
1608 xisr
->sadb_x_ipsecrequest_len
=
1609 PFKEY_ALIGN8(sizeof(*xisr
)
1610 + isr
->saidx
.src
.ss_len
1611 + isr
->saidx
.dst
.ss_len
);
1614 lwkt_reltoken(&key_token
);
1618 /* m will not be freed nor modified */
1619 static struct mbuf
*
1620 key_gather_mbuf(struct mbuf
*m
, const struct sadb_msghdr
*mhp
,
1621 int ndeep
, int nitem
, ...)
1626 struct mbuf
*result
= NULL
, *n
;
1629 if (m
== NULL
|| mhp
== NULL
)
1630 panic("null pointer passed to key_gather");
1632 __va_start(ap
, nitem
);
1633 for (i
= 0; i
< nitem
; i
++) {
1634 idx
= __va_arg(ap
, int);
1635 if (idx
< 0 || idx
> SADB_EXT_MAX
)
1637 /* don't attempt to pull empty extension */
1638 if (idx
== SADB_EXT_RESERVED
&& mhp
->msg
== NULL
)
1640 if (idx
!= SADB_EXT_RESERVED
&&
1641 (mhp
->ext
[idx
] == NULL
|| mhp
->extlen
[idx
] == 0))
1644 if (idx
== SADB_EXT_RESERVED
) {
1645 len
= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
1648 panic("assumption failed");
1650 MGETHDR(n
, M_NOWAIT
, MT_DATA
);
1655 m_copydata(m
, 0, sizeof(struct sadb_msg
),
1657 } else if (i
< ndeep
) {
1658 len
= mhp
->extlen
[idx
];
1659 n
= key_alloc_mbuf(len
);
1660 if (!n
|| n
->m_next
) { /*XXX*/
1665 m_copydata(m
, mhp
->extoff
[idx
], mhp
->extlen
[idx
],
1668 n
= m_copym(m
, mhp
->extoff
[idx
], mhp
->extlen
[idx
],
1681 if ((result
->m_flags
& M_PKTHDR
) != 0) {
1682 result
->m_pkthdr
.len
= 0;
1683 for (n
= result
; n
; n
= n
->m_next
)
1684 result
->m_pkthdr
.len
+= n
->m_len
;
1695 * SADB_X_SPDADD, SADB_X_SPDSETIDX or SADB_X_SPDUPDATE processing
1696 * add a entry to SP database, when received
1697 * <base, address(SD), (lifetime(H),) policy>
1699 * Adding to SP database,
1701 * <base, address(SD), (lifetime(H),) policy>
1702 * to the socket which was send.
1704 * SPDADD set a unique policy entry.
1705 * SPDSETIDX like SPDADD without a part of policy requests.
1706 * SPDUPDATE replace a unique policy entry.
1708 * m will always be freed.
1711 key_spdadd(struct socket
*so
, struct mbuf
*m
, const struct sadb_msghdr
*mhp
)
1713 struct sadb_address
*src0
, *dst0
;
1714 struct sadb_x_policy
*xpl0
, *xpl
;
1715 struct sadb_lifetime
*lft
= NULL
;
1716 struct secpolicyindex spidx
;
1717 struct secpolicy
*newsp
;
1722 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
1723 panic("key_spdadd: NULL pointer is passed.");
1725 if (mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
1726 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
||
1727 mhp
->ext
[SADB_X_EXT_POLICY
] == NULL
) {
1728 ipseclog((LOG_DEBUG
, "key_spdadd: invalid message is passed.\n"));
1729 return key_senderror(so
, m
, EINVAL
);
1731 if (mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
1732 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
) ||
1733 mhp
->extlen
[SADB_X_EXT_POLICY
] < sizeof(struct sadb_x_policy
)) {
1734 ipseclog((LOG_DEBUG
, "key_spdadd: invalid message is passed.\n"));
1735 return key_senderror(so
, m
, EINVAL
);
1737 if (mhp
->ext
[SADB_EXT_LIFETIME_HARD
] != NULL
) {
1738 if (mhp
->extlen
[SADB_EXT_LIFETIME_HARD
]
1739 < sizeof(struct sadb_lifetime
)) {
1740 ipseclog((LOG_DEBUG
, "key_spdadd: invalid message is passed.\n"));
1741 return key_senderror(so
, m
, EINVAL
);
1743 lft
= (struct sadb_lifetime
*)mhp
->ext
[SADB_EXT_LIFETIME_HARD
];
1746 src0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_SRC
];
1747 dst0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_DST
];
1748 xpl0
= (struct sadb_x_policy
*)mhp
->ext
[SADB_X_EXT_POLICY
];
1751 /* XXX boundary check against sa_len */
1752 KEY_SETSECSPIDX(xpl0
->sadb_x_policy_dir
,
1755 src0
->sadb_address_prefixlen
,
1756 dst0
->sadb_address_prefixlen
,
1757 src0
->sadb_address_proto
,
1760 /* checking the direciton. */
1761 switch (xpl0
->sadb_x_policy_dir
) {
1762 case IPSEC_DIR_INBOUND
:
1763 case IPSEC_DIR_OUTBOUND
:
1766 ipseclog((LOG_DEBUG
, "key_spdadd: Invalid SP direction.\n"));
1767 mhp
->msg
->sadb_msg_errno
= EINVAL
;
1772 /* key_spdadd() accepts DISCARD, NONE and IPSEC. */
1773 if (xpl0
->sadb_x_policy_type
== IPSEC_POLICY_ENTRUST
1774 || xpl0
->sadb_x_policy_type
== IPSEC_POLICY_BYPASS
) {
1775 ipseclog((LOG_DEBUG
, "key_spdadd: Invalid policy type.\n"));
1776 return key_senderror(so
, m
, EINVAL
);
1779 /* policy requests are mandatory when action is ipsec. */
1780 if (mhp
->msg
->sadb_msg_type
!= SADB_X_SPDSETIDX
1781 && xpl0
->sadb_x_policy_type
== IPSEC_POLICY_IPSEC
1782 && mhp
->extlen
[SADB_X_EXT_POLICY
] <= sizeof(*xpl0
)) {
1783 ipseclog((LOG_DEBUG
, "key_spdadd: some policy requests part required.\n"));
1784 return key_senderror(so
, m
, EINVAL
);
1788 * checking there is SP already or not.
1789 * SPDUPDATE doesn't depend on whether there is a SP or not.
1790 * If the type is either SPDADD or SPDSETIDX AND a SP is found,
1793 newsp
= key_getsp(&spidx
);
1794 if (mhp
->msg
->sadb_msg_type
== SADB_X_SPDUPDATE
) {
1796 newsp
->state
= IPSEC_SPSTATE_DEAD
;
1800 if (newsp
!= NULL
) {
1802 ipseclog((LOG_DEBUG
, "key_spdadd: a SP entry exists already.\n"));
1803 return key_senderror(so
, m
, EEXIST
);
1807 /* allocation new SP entry */
1808 if ((newsp
= key_msg2sp(xpl0
, PFKEY_EXTLEN(xpl0
), &error
)) == NULL
) {
1809 return key_senderror(so
, m
, error
);
1812 if ((newsp
->id
= key_getnewspid()) == 0) {
1813 keydb_delsecpolicy(newsp
);
1814 return key_senderror(so
, m
, ENOBUFS
);
1817 /* XXX boundary check against sa_len */
1818 KEY_SETSECSPIDX(xpl0
->sadb_x_policy_dir
,
1821 src0
->sadb_address_prefixlen
,
1822 dst0
->sadb_address_prefixlen
,
1823 src0
->sadb_address_proto
,
1826 /* sanity check on addr pair */
1827 if (((struct sockaddr
*)(src0
+ 1))->sa_family
!=
1828 ((struct sockaddr
*)(dst0
+ 1))->sa_family
) {
1829 keydb_delsecpolicy(newsp
);
1830 return key_senderror(so
, m
, EINVAL
);
1832 if (((struct sockaddr
*)(src0
+ 1))->sa_len
!=
1833 ((struct sockaddr
*)(dst0
+ 1))->sa_len
) {
1834 keydb_delsecpolicy(newsp
);
1835 return key_senderror(so
, m
, EINVAL
);
1838 if (newsp
->req
&& newsp
->req
->saidx
.src
.ss_family
) {
1839 struct sockaddr
*sa
;
1840 sa
= (struct sockaddr
*)(src0
+ 1);
1841 if (sa
->sa_family
!= newsp
->req
->saidx
.src
.ss_family
) {
1842 keydb_delsecpolicy(newsp
);
1843 return key_senderror(so
, m
, EINVAL
);
1846 if (newsp
->req
&& newsp
->req
->saidx
.dst
.ss_family
) {
1847 struct sockaddr
*sa
;
1848 sa
= (struct sockaddr
*)(dst0
+ 1);
1849 if (sa
->sa_family
!= newsp
->req
->saidx
.dst
.ss_family
) {
1850 keydb_delsecpolicy(newsp
);
1851 return key_senderror(so
, m
, EINVAL
);
1857 newsp
->created
= tv
.tv_sec
;
1858 newsp
->lastused
= tv
.tv_sec
;
1859 newsp
->lifetime
= lft
? lft
->sadb_lifetime_addtime
: 0;
1860 newsp
->validtime
= lft
? lft
->sadb_lifetime_usetime
: 0;
1862 newsp
->refcnt
= 1; /* do not reclaim until I say I do */
1863 newsp
->state
= IPSEC_SPSTATE_ALIVE
;
1864 LIST_INSERT_TAIL(&sptree
[newsp
->spidx
.dir
], newsp
, secpolicy
, chain
);
1866 /* delete the entry in spacqtree */
1867 if (mhp
->msg
->sadb_msg_type
== SADB_X_SPDUPDATE
) {
1868 struct secspacq
*spacq
;
1869 if ((spacq
= key_getspacq(&spidx
)) != NULL
) {
1870 /* reset counter in order to deletion by timehandler. */
1872 spacq
->created
= tv
.tv_sec
;
1878 struct mbuf
*n
, *mpolicy
;
1879 struct sadb_msg
*newmsg
;
1882 /* create new sadb_msg to reply. */
1884 n
= key_gather_mbuf(m
, mhp
, 2, 5, SADB_EXT_RESERVED
,
1885 SADB_X_EXT_POLICY
, SADB_EXT_LIFETIME_HARD
,
1886 SADB_EXT_ADDRESS_SRC
, SADB_EXT_ADDRESS_DST
);
1888 n
= key_gather_mbuf(m
, mhp
, 2, 4, SADB_EXT_RESERVED
,
1890 SADB_EXT_ADDRESS_SRC
, SADB_EXT_ADDRESS_DST
);
1893 return key_senderror(so
, m
, ENOBUFS
);
1895 if (n
->m_len
< sizeof(*newmsg
)) {
1896 n
= m_pullup(n
, sizeof(*newmsg
));
1898 return key_senderror(so
, m
, ENOBUFS
);
1900 newmsg
= mtod(n
, struct sadb_msg
*);
1901 newmsg
->sadb_msg_errno
= 0;
1902 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
1905 mpolicy
= m_pulldown(n
, PFKEY_ALIGN8(sizeof(struct sadb_msg
)),
1906 sizeof(*xpl
), &off
);
1907 if (mpolicy
== NULL
) {
1908 /* n is already freed */
1909 return key_senderror(so
, m
, ENOBUFS
);
1911 xpl
= (struct sadb_x_policy
*)(mtod(mpolicy
, caddr_t
) + off
);
1912 if (xpl
->sadb_x_policy_exttype
!= SADB_X_EXT_POLICY
) {
1914 return key_senderror(so
, m
, EINVAL
);
1916 xpl
->sadb_x_policy_id
= newsp
->id
;
1919 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
1924 * get new policy id.
1930 key_getnewspid(void)
1932 u_int32_t newid
= 0;
1933 int count
= key_spi_trycnt
; /* XXX */
1934 struct secpolicy
*sp
;
1936 /* when requesting to allocate spi ranged */
1938 newid
= (policy_id
= (policy_id
== ~0 ? 1 : policy_id
+ 1));
1940 if ((sp
= key_getspbyid(newid
)) == NULL
)
1946 if (count
== 0 || newid
== 0) {
1947 ipseclog((LOG_DEBUG
, "key_getnewspid: to allocate policy id is failed.\n"));
1955 * SADB_SPDDELETE processing
1957 * <base, address(SD), policy(*)>
1958 * from the user(?), and set SADB_SASTATE_DEAD,
1960 * <base, address(SD), policy(*)>
1962 * policy(*) including direction of policy.
1964 * m will always be freed.
1967 key_spddelete(struct socket
*so
, struct mbuf
*m
,
1968 const struct sadb_msghdr
*mhp
)
1970 struct sadb_address
*src0
, *dst0
;
1971 struct sadb_x_policy
*xpl0
;
1972 struct secpolicyindex spidx
;
1973 struct secpolicy
*sp
;
1976 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
1977 panic("key_spddelete: NULL pointer is passed.");
1979 if (mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
1980 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
||
1981 mhp
->ext
[SADB_X_EXT_POLICY
] == NULL
) {
1982 ipseclog((LOG_DEBUG
, "key_spddelete: invalid message is passed.\n"));
1983 return key_senderror(so
, m
, EINVAL
);
1985 if (mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
1986 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
) ||
1987 mhp
->extlen
[SADB_X_EXT_POLICY
] < sizeof(struct sadb_x_policy
)) {
1988 ipseclog((LOG_DEBUG
, "key_spddelete: invalid message is passed.\n"));
1989 return key_senderror(so
, m
, EINVAL
);
1992 src0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_SRC
];
1993 dst0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_DST
];
1994 xpl0
= (struct sadb_x_policy
*)mhp
->ext
[SADB_X_EXT_POLICY
];
1997 /* XXX boundary check against sa_len */
1998 KEY_SETSECSPIDX(xpl0
->sadb_x_policy_dir
,
2001 src0
->sadb_address_prefixlen
,
2002 dst0
->sadb_address_prefixlen
,
2003 src0
->sadb_address_proto
,
2006 /* checking the direciton. */
2007 switch (xpl0
->sadb_x_policy_dir
) {
2008 case IPSEC_DIR_INBOUND
:
2009 case IPSEC_DIR_OUTBOUND
:
2012 ipseclog((LOG_DEBUG
, "key_spddelete: Invalid SP direction.\n"));
2013 return key_senderror(so
, m
, EINVAL
);
2016 /* Is there SP in SPD ? */
2017 if ((sp
= key_getsp(&spidx
)) == NULL
) {
2018 ipseclog((LOG_DEBUG
, "key_spddelete: no SP found.\n"));
2019 return key_senderror(so
, m
, EINVAL
);
2022 /* save policy id to buffer to be returned. */
2023 xpl0
->sadb_x_policy_id
= sp
->id
;
2025 sp
->state
= IPSEC_SPSTATE_DEAD
;
2030 struct sadb_msg
*newmsg
;
2032 /* create new sadb_msg to reply. */
2033 n
= key_gather_mbuf(m
, mhp
, 1, 4, SADB_EXT_RESERVED
,
2034 SADB_X_EXT_POLICY
, SADB_EXT_ADDRESS_SRC
, SADB_EXT_ADDRESS_DST
);
2036 return key_senderror(so
, m
, ENOBUFS
);
2038 newmsg
= mtod(n
, struct sadb_msg
*);
2039 newmsg
->sadb_msg_errno
= 0;
2040 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
2043 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
2048 * SADB_SPDDELETE2 processing
2051 * from the user(?), and set SADB_SASTATE_DEAD,
2055 * policy(*) including direction of policy.
2057 * m will always be freed.
2060 key_spddelete2(struct socket
*so
, struct mbuf
*m
,
2061 const struct sadb_msghdr
*mhp
)
2064 struct secpolicy
*sp
;
2067 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
2068 panic("key_spddelete2: NULL pointer is passed.");
2070 if (mhp
->ext
[SADB_X_EXT_POLICY
] == NULL
||
2071 mhp
->extlen
[SADB_X_EXT_POLICY
] < sizeof(struct sadb_x_policy
)) {
2072 ipseclog((LOG_DEBUG
, "key_spddelete2: invalid message is passed.\n"));
2073 key_senderror(so
, m
, EINVAL
);
2077 id
= ((struct sadb_x_policy
*)mhp
->ext
[SADB_X_EXT_POLICY
])->sadb_x_policy_id
;
2079 /* Is there SP in SPD ? */
2080 if ((sp
= key_getspbyid(id
)) == NULL
) {
2081 ipseclog((LOG_DEBUG
, "key_spddelete2: no SP found id:%u.\n", id
));
2082 key_senderror(so
, m
, EINVAL
);
2085 sp
->state
= IPSEC_SPSTATE_DEAD
;
2089 struct mbuf
*n
, *nn
;
2090 struct sadb_msg
*newmsg
;
2093 /* create new sadb_msg to reply. */
2094 len
= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
2097 return key_senderror(so
, m
, ENOBUFS
);
2098 MGETHDR(n
, M_NOWAIT
, MT_DATA
);
2099 if (n
&& len
> MHLEN
) {
2100 MCLGET(n
, M_NOWAIT
);
2101 if ((n
->m_flags
& M_EXT
) == 0) {
2107 return key_senderror(so
, m
, ENOBUFS
);
2113 m_copydata(m
, 0, sizeof(struct sadb_msg
), mtod(n
, caddr_t
) + off
);
2114 off
+= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
2118 panic("length inconsistency in key_spddelete2");
2121 n
->m_next
= m_copym(m
, mhp
->extoff
[SADB_X_EXT_POLICY
],
2122 mhp
->extlen
[SADB_X_EXT_POLICY
], M_NOWAIT
);
2125 return key_senderror(so
, m
, ENOBUFS
);
2128 n
->m_pkthdr
.len
= 0;
2129 for (nn
= n
; nn
; nn
= nn
->m_next
)
2130 n
->m_pkthdr
.len
+= nn
->m_len
;
2132 newmsg
= mtod(n
, struct sadb_msg
*);
2133 newmsg
->sadb_msg_errno
= 0;
2134 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
2137 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
2142 * SADB_X_GET processing
2147 * <base, address(SD), policy>
2149 * policy(*) including direction of policy.
2151 * m will always be freed.
2154 key_spdget(struct socket
*so
, struct mbuf
*m
,
2155 const struct sadb_msghdr
*mhp
)
2158 struct secpolicy
*sp
;
2162 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
2163 panic("key_spdget: 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_spdget: invalid message is passed.\n"));
2168 return key_senderror(so
, m
, EINVAL
);
2171 id
= ((struct sadb_x_policy
*)mhp
->ext
[SADB_X_EXT_POLICY
])->sadb_x_policy_id
;
2173 /* Is there SP in SPD ? */
2174 if ((sp
= key_getspbyid(id
)) == NULL
) {
2175 ipseclog((LOG_DEBUG
, "key_spdget: no SP found id:%u.\n", id
));
2176 return key_senderror(so
, m
, ENOENT
);
2179 n
= key_setdumpsp(sp
, SADB_X_SPDGET
, 0, mhp
->msg
->sadb_msg_pid
);
2182 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ONE
);
2184 return key_senderror(so
, m
, ENOBUFS
);
2188 * SADB_X_SPDACQUIRE processing.
2189 * Acquire policy and SA(s) for a *OUTBOUND* packet.
2192 * to KMD, and expect to receive
2193 * <base> with SADB_X_SPDACQUIRE if error occured,
2196 * with SADB_X_SPDUPDATE from KMD by PF_KEY.
2197 * policy(*) is without policy requests.
2200 * others: error number
2203 key_spdacquire(struct secpolicy
*sp
)
2205 struct mbuf
*result
= NULL
, *m
;
2206 struct secspacq
*newspacq
;
2211 panic("key_spdacquire: NULL pointer is passed.");
2212 if (sp
->req
!= NULL
)
2213 panic("key_spdacquire: called but there is request.");
2214 if (sp
->policy
!= IPSEC_POLICY_IPSEC
)
2215 panic("key_spdacquire: policy mismatched. IPsec is expected.");
2217 lwkt_gettoken(&key_token
);
2218 /* get a entry to check whether sent message or not. */
2219 if ((newspacq
= key_getspacq(&sp
->spidx
)) != NULL
) {
2220 if (key_blockacq_count
< newspacq
->count
) {
2221 /* reset counter and do send message. */
2222 newspacq
->count
= 0;
2224 /* increment counter and do nothing. */
2226 lwkt_reltoken(&key_token
);
2230 /* make new entry for blocking to send SADB_ACQUIRE. */
2231 if ((newspacq
= key_newspacq(&sp
->spidx
)) == NULL
) {
2232 lwkt_reltoken(&key_token
);
2236 /* add to acqtree */
2237 LIST_INSERT_HEAD(&spacqtree
, newspacq
, chain
);
2240 /* create new sadb_msg to reply. */
2241 m
= key_setsadbmsg(SADB_X_SPDACQUIRE
, 0, 0, 0, 0, 0);
2248 result
->m_pkthdr
.len
= 0;
2249 for (m
= result
; m
; m
= m
->m_next
)
2250 result
->m_pkthdr
.len
+= m
->m_len
;
2252 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
2253 PFKEY_UNIT64(result
->m_pkthdr
.len
);
2255 error
= key_sendup_mbuf(NULL
, m
, KEY_SENDUP_REGISTERED
);
2256 lwkt_reltoken(&key_token
);
2260 lwkt_reltoken(&key_token
);
2267 * SADB_SPDFLUSH processing
2270 * from the user, and free all entries in secpctree.
2274 * NOTE: what to do is only marking SADB_SASTATE_DEAD.
2276 * m will always be freed.
2279 key_spdflush(struct socket
*so
, struct mbuf
*m
,
2280 const struct sadb_msghdr
*mhp
)
2282 struct sadb_msg
*newmsg
;
2283 struct secpolicy
*sp
;
2287 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
2288 panic("key_spdflush: NULL pointer is passed.");
2290 if (m
->m_len
!= PFKEY_ALIGN8(sizeof(struct sadb_msg
)))
2291 return key_senderror(so
, m
, EINVAL
);
2293 for (dir
= 0; dir
< IPSEC_DIR_MAX
; dir
++) {
2294 LIST_FOREACH(sp
, &sptree
[dir
], chain
) {
2295 sp
->state
= IPSEC_SPSTATE_DEAD
;
2299 if (sizeof(struct sadb_msg
) > m
->m_len
+ M_TRAILINGSPACE(m
)) {
2300 ipseclog((LOG_DEBUG
, "key_spdflush: No more memory.\n"));
2301 return key_senderror(so
, m
, ENOBUFS
);
2307 m
->m_pkthdr
.len
= m
->m_len
= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
2308 newmsg
= mtod(m
, struct sadb_msg
*);
2309 newmsg
->sadb_msg_errno
= 0;
2310 newmsg
->sadb_msg_len
= PFKEY_UNIT64(m
->m_pkthdr
.len
);
2312 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ALL
);
2316 * SADB_SPDDUMP processing
2319 * from the user, and dump all SP leaves
2324 * m will always be freed.
2327 key_spddump(struct socket
*so
, struct mbuf
*m
,
2328 const struct sadb_msghdr
*mhp
)
2330 struct secpolicy
*sp
;
2336 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
2337 panic("key_spddump: NULL pointer is passed.");
2339 /* search SPD entry and get buffer size. */
2341 for (dir
= 0; dir
< IPSEC_DIR_MAX
; dir
++) {
2342 LIST_FOREACH(sp
, &sptree
[dir
], chain
) {
2348 return key_senderror(so
, m
, ENOENT
);
2350 for (dir
= 0; dir
< IPSEC_DIR_MAX
; dir
++) {
2351 LIST_FOREACH(sp
, &sptree
[dir
], chain
) {
2353 n
= key_setdumpsp(sp
, SADB_X_SPDDUMP
, cnt
,
2354 mhp
->msg
->sadb_msg_pid
);
2357 key_sendup_mbuf(so
, n
, KEY_SENDUP_ONE
);
2365 static struct mbuf
*
2366 key_setdumpsp(struct secpolicy
*sp
, u_int8_t type
, u_int32_t seq
,
2369 struct mbuf
*result
= NULL
, *m
;
2371 m
= key_setsadbmsg(type
, 0, SADB_SATYPE_UNSPEC
, seq
, pid
, sp
->refcnt
);
2376 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
2377 (struct sockaddr
*)&sp
->spidx
.src
, sp
->spidx
.prefs
,
2378 sp
->spidx
.ul_proto
);
2383 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
2384 (struct sockaddr
*)&sp
->spidx
.dst
, sp
->spidx
.prefd
,
2385 sp
->spidx
.ul_proto
);
2395 if ((result
->m_flags
& M_PKTHDR
) == 0)
2398 if (result
->m_len
< sizeof(struct sadb_msg
)) {
2399 result
= m_pullup(result
, sizeof(struct sadb_msg
));
2404 result
->m_pkthdr
.len
= 0;
2405 for (m
= result
; m
; m
= m
->m_next
)
2406 result
->m_pkthdr
.len
+= m
->m_len
;
2408 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
2409 PFKEY_UNIT64(result
->m_pkthdr
.len
);
2419 * get PFKEY message length for security policy and request.
2422 key_getspreqmsglen(struct secpolicy
*sp
)
2426 tlen
= sizeof(struct sadb_x_policy
);
2428 /* if is the policy for ipsec ? */
2429 if (sp
->policy
!= IPSEC_POLICY_IPSEC
)
2432 /* get length of ipsec requests */
2434 struct ipsecrequest
*isr
;
2437 for (isr
= sp
->req
; isr
!= NULL
; isr
= isr
->next
) {
2438 len
= sizeof(struct sadb_x_ipsecrequest
)
2439 + isr
->saidx
.src
.ss_len
2440 + isr
->saidx
.dst
.ss_len
;
2442 tlen
+= PFKEY_ALIGN8(len
);
2450 * SADB_SPDEXPIRE processing
2452 * <base, address(SD), lifetime(CH), policy>
2456 * others : error number
2459 key_spdexpire(struct secpolicy
*sp
)
2461 struct mbuf
*result
= NULL
, *m
;
2464 struct sadb_lifetime
*lt
;
2466 /* XXX: Why do we lock ? */
2470 panic("key_spdexpire: NULL pointer is passed.");
2472 /* set msg header */
2473 m
= key_setsadbmsg(SADB_X_SPDEXPIRE
, 0, 0, 0, 0, 0);
2480 /* create lifetime extension (current and hard) */
2481 len
= PFKEY_ALIGN8(sizeof(*lt
)) * 2;
2482 m
= key_alloc_mbuf(len
);
2483 if (!m
|| m
->m_next
) { /*XXX*/
2489 bzero(mtod(m
, caddr_t
), len
);
2490 lt
= mtod(m
, struct sadb_lifetime
*);
2491 lt
->sadb_lifetime_len
= PFKEY_UNIT64(sizeof(struct sadb_lifetime
));
2492 lt
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
2493 lt
->sadb_lifetime_allocations
= 0;
2494 lt
->sadb_lifetime_bytes
= 0;
2495 lt
->sadb_lifetime_addtime
= sp
->created
;
2496 lt
->sadb_lifetime_usetime
= sp
->lastused
;
2497 lt
= (struct sadb_lifetime
*)(mtod(m
, caddr_t
) + len
/ 2);
2498 lt
->sadb_lifetime_len
= PFKEY_UNIT64(sizeof(struct sadb_lifetime
));
2499 lt
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_HARD
;
2500 lt
->sadb_lifetime_allocations
= 0;
2501 lt
->sadb_lifetime_bytes
= 0;
2502 lt
->sadb_lifetime_addtime
= sp
->lifetime
;
2503 lt
->sadb_lifetime_usetime
= sp
->validtime
;
2506 /* set sadb_address for source */
2507 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
2508 (struct sockaddr
*)&sp
->spidx
.src
,
2509 sp
->spidx
.prefs
, sp
->spidx
.ul_proto
);
2516 /* set sadb_address for destination */
2517 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
2518 (struct sockaddr
*)&sp
->spidx
.dst
,
2519 sp
->spidx
.prefd
, sp
->spidx
.ul_proto
);
2534 if ((result
->m_flags
& M_PKTHDR
) == 0) {
2539 if (result
->m_len
< sizeof(struct sadb_msg
)) {
2540 result
= m_pullup(result
, sizeof(struct sadb_msg
));
2541 if (result
== NULL
) {
2547 result
->m_pkthdr
.len
= 0;
2548 for (m
= result
; m
; m
= m
->m_next
)
2549 result
->m_pkthdr
.len
+= m
->m_len
;
2551 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
2552 PFKEY_UNIT64(result
->m_pkthdr
.len
);
2554 return key_sendup_mbuf(NULL
, result
, KEY_SENDUP_REGISTERED
);
2562 /* %%% SAD management */
2564 * allocating a memory for new SA head, and copy from the values of mhp.
2565 * OUT: NULL : failure due to the lack of memory.
2566 * others : pointer to new SA head.
2568 static struct secashead
*
2569 key_newsah(struct secasindex
*saidx
)
2571 struct secashead
*newsah
;
2575 panic("key_newsaidx: NULL pointer is passed.");
2577 newsah
= keydb_newsecashead();
2581 bcopy(saidx
, &newsah
->saidx
, sizeof(newsah
->saidx
));
2583 /* add to saidxtree */
2584 newsah
->state
= SADB_SASTATE_MATURE
;
2585 LIST_INSERT_HEAD(&sahtree
, newsah
, chain
);
2591 * delete SA index and all SA registerd.
2594 key_delsah(struct secashead
*sah
)
2596 struct secasvar
*sav
, *nextsav
;
2597 u_int stateidx
, state
;
2602 panic("key_delsah: NULL pointer is passed.");
2604 /* searching all SA registerd in the secindex. */
2606 stateidx
< NELEM(saorder_state_any
);
2609 state
= saorder_state_any
[stateidx
];
2610 for (sav
= (struct secasvar
*)LIST_FIRST(&sah
->savtree
[state
]);
2614 nextsav
= LIST_NEXT(sav
, chain
);
2616 if (sav
->refcnt
> 0) {
2617 /* give up to delete this sa */
2623 KEY_CHKSASTATE(state
, sav
->state
, "key_delsah");
2627 /* remove back pointer */
2633 /* don't delete sah only if there are savs. */
2637 if (sah
->sa_route
.ro_rt
) {
2638 RTFREE(sah
->sa_route
.ro_rt
);
2639 sah
->sa_route
.ro_rt
= NULL
;
2642 /* remove from tree of SA index */
2643 if (__LIST_CHAINED(sah
))
2644 LIST_REMOVE(sah
, chain
);
2652 * allocating a new SA with LARVAL state. key_add() and key_getspi() call,
2653 * and copy the values of mhp into new buffer.
2654 * When SAD message type is GETSPI:
2655 * to set sequence number from acq_seq++,
2656 * to set zero to SPI.
2657 * not to call key_setsava().
2659 * others : pointer to new secasvar.
2661 * does not modify mbuf. does not free mbuf on error.
2663 static struct secasvar
*
2664 key_newsav(struct mbuf
*m
, const struct sadb_msghdr
*mhp
,
2665 struct secashead
*sah
, int *errp
)
2667 struct secasvar
*newsav
;
2668 const struct sadb_sa
*xsa
;
2671 if (m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
|| sah
== NULL
)
2672 panic("key_newsa: NULL pointer is passed.");
2674 KMALLOC(newsav
, struct secasvar
*, sizeof(struct secasvar
));
2675 if (newsav
== NULL
) {
2676 ipseclog((LOG_DEBUG
, "key_newsa: No more memory.\n"));
2680 bzero((caddr_t
)newsav
, sizeof(struct secasvar
));
2682 switch (mhp
->msg
->sadb_msg_type
) {
2686 #ifdef IPSEC_DOSEQCHECK
2687 /* sync sequence number */
2688 if (mhp
->msg
->sadb_msg_seq
== 0)
2690 (acq_seq
= (acq_seq
== ~0 ? 1 : ++acq_seq
));
2693 newsav
->seq
= mhp
->msg
->sadb_msg_seq
;
2698 if (mhp
->ext
[SADB_EXT_SA
] == NULL
) {
2700 ipseclog((LOG_DEBUG
, "key_newsa: invalid message is passed.\n"));
2704 xsa
= (const struct sadb_sa
*)mhp
->ext
[SADB_EXT_SA
];
2705 newsav
->spi
= xsa
->sadb_sa_spi
;
2706 newsav
->seq
= mhp
->msg
->sadb_msg_seq
;
2714 /* copy sav values */
2715 if (mhp
->msg
->sadb_msg_type
!= SADB_GETSPI
) {
2716 *errp
= key_setsaval(newsav
, m
, mhp
);
2727 newsav
->created
= tv
.tv_sec
;
2730 newsav
->pid
= mhp
->msg
->sadb_msg_pid
;
2735 newsav
->state
= SADB_SASTATE_LARVAL
;
2736 LIST_INSERT_TAIL(&sah
->savtree
[SADB_SASTATE_LARVAL
], newsav
,
2743 * free() SA variable entry.
2746 key_delsav(struct secasvar
*sav
)
2750 panic("key_delsav: NULL pointer is passed.");
2752 if (sav
->refcnt
> 0)
2753 return; /* can't free */
2755 /* remove from SA header */
2756 if (__LIST_CHAINED(sav
))
2757 LIST_REMOVE(sav
, chain
);
2759 if (sav
->key_auth
!= NULL
) {
2760 bzero(_KEYBUF(sav
->key_auth
), _KEYLEN(sav
->key_auth
));
2761 KFREE(sav
->key_auth
);
2762 sav
->key_auth
= NULL
;
2764 if (sav
->key_enc
!= NULL
) {
2765 bzero(_KEYBUF(sav
->key_enc
), _KEYLEN(sav
->key_enc
));
2766 KFREE(sav
->key_enc
);
2767 sav
->key_enc
= NULL
;
2770 bzero(sav
->sched
, sav
->schedlen
);
2774 if (sav
->replay
!= NULL
) {
2775 keydb_delsecreplay(sav
->replay
);
2778 if (sav
->lft_c
!= NULL
) {
2782 if (sav
->lft_h
!= NULL
) {
2786 if (sav
->lft_s
!= NULL
) {
2790 if (sav
->iv
!= NULL
) {
2804 * others : found, pointer to a SA.
2806 static struct secashead
*
2807 key_getsah(struct secasindex
*saidx
)
2809 struct secashead
*sah
;
2811 LIST_FOREACH(sah
, &sahtree
, chain
) {
2812 if (sah
->state
== SADB_SASTATE_DEAD
)
2814 if (key_cmpsaidx(&sah
->saidx
, saidx
, CMP_REQID
))
2822 * check not to be duplicated SPI.
2823 * NOTE: this function is too slow due to searching all SAD.
2826 * others : found, pointer to a SA.
2828 static struct secasvar
*
2829 key_checkspidup(struct secasindex
*saidx
, u_int32_t spi
)
2831 struct secashead
*sah
;
2832 struct secasvar
*sav
;
2834 /* check address family */
2835 if (saidx
->src
.ss_family
!= saidx
->dst
.ss_family
) {
2836 ipseclog((LOG_DEBUG
, "key_checkspidup: address family mismatched.\n"));
2841 LIST_FOREACH(sah
, &sahtree
, chain
) {
2842 if (!key_ismyaddr((struct sockaddr
*)&sah
->saidx
.dst
))
2844 sav
= key_getsavbyspi(sah
, spi
);
2853 * search SAD litmited alive SA, protocol, SPI.
2856 * others : found, pointer to a SA.
2858 static struct secasvar
*
2859 key_getsavbyspi(struct secashead
*sah
, u_int32_t spi
)
2861 struct secasvar
*sav
;
2862 u_int stateidx
, state
;
2864 /* search all status */
2866 stateidx
< NELEM(saorder_state_alive
);
2869 state
= saorder_state_alive
[stateidx
];
2870 LIST_FOREACH(sav
, &sah
->savtree
[state
], chain
) {
2873 if (sav
->state
!= state
) {
2874 ipseclog((LOG_DEBUG
, "key_getsavbyspi: "
2875 "invalid sav->state (queue: %d SA: %d)\n",
2876 state
, sav
->state
));
2880 if (sav
->spi
== spi
)
2889 * copy SA values from PF_KEY message except *SPI, SEQ, PID, STATE and TYPE*.
2890 * You must update these if need.
2894 * does not modify mbuf. does not free mbuf on error.
2897 key_setsaval(struct secasvar
*sav
, struct mbuf
*m
,
2898 const struct sadb_msghdr
*mhp
)
2901 const struct esp_algorithm
*algo
;
2907 if (m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
2908 panic("key_setsaval: NULL pointer is passed.");
2910 /* initialization */
2912 sav
->key_auth
= NULL
;
2913 sav
->key_enc
= NULL
;
2922 if (mhp
->ext
[SADB_EXT_SA
] != NULL
) {
2923 const struct sadb_sa
*sa0
;
2925 sa0
= (const struct sadb_sa
*)mhp
->ext
[SADB_EXT_SA
];
2926 if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(*sa0
)) {
2931 sav
->alg_auth
= sa0
->sadb_sa_auth
;
2932 sav
->alg_enc
= sa0
->sadb_sa_encrypt
;
2933 sav
->flags
= sa0
->sadb_sa_flags
;
2936 if ((sa0
->sadb_sa_flags
& SADB_X_EXT_OLD
) == 0) {
2937 sav
->replay
= keydb_newsecreplay(sa0
->sadb_sa_replay
);
2938 if (sav
->replay
== NULL
) {
2939 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
2946 /* Authentication keys */
2947 if (mhp
->ext
[SADB_EXT_KEY_AUTH
] != NULL
) {
2948 const struct sadb_key
*key0
;
2951 key0
= (const struct sadb_key
*)mhp
->ext
[SADB_EXT_KEY_AUTH
];
2952 len
= mhp
->extlen
[SADB_EXT_KEY_AUTH
];
2955 if (len
< sizeof(*key0
)) {
2959 switch (mhp
->msg
->sadb_msg_satype
) {
2960 case SADB_SATYPE_AH
:
2961 case SADB_SATYPE_ESP
:
2962 case SADB_X_SATYPE_TCPSIGNATURE
:
2963 if (len
== PFKEY_ALIGN8(sizeof(struct sadb_key
)) &&
2964 sav
->alg_auth
!= SADB_X_AALG_NULL
)
2967 case SADB_X_SATYPE_IPCOMP
:
2973 ipseclog((LOG_DEBUG
, "key_setsaval: invalid key_auth values.\n"));
2977 sav
->key_auth
= (struct sadb_key
*)key_newbuf(key0
, len
);
2978 if (sav
->key_auth
== NULL
) {
2979 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
2985 /* Encryption key */
2986 if (mhp
->ext
[SADB_EXT_KEY_ENCRYPT
] != NULL
) {
2987 const struct sadb_key
*key0
;
2990 key0
= (const struct sadb_key
*)mhp
->ext
[SADB_EXT_KEY_ENCRYPT
];
2991 len
= mhp
->extlen
[SADB_EXT_KEY_ENCRYPT
];
2994 if (len
< sizeof(*key0
)) {
2998 switch (mhp
->msg
->sadb_msg_satype
) {
2999 case SADB_SATYPE_ESP
:
3000 if (len
== PFKEY_ALIGN8(sizeof(struct sadb_key
)) &&
3001 sav
->alg_enc
!= SADB_EALG_NULL
) {
3005 sav
->key_enc
= (struct sadb_key
*)key_newbuf(key0
, len
);
3006 if (sav
->key_enc
== NULL
) {
3007 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
3012 case SADB_X_SATYPE_IPCOMP
:
3013 if (len
!= PFKEY_ALIGN8(sizeof(struct sadb_key
)))
3015 sav
->key_enc
= NULL
; /*just in case*/
3017 case SADB_SATYPE_AH
:
3018 case SADB_X_SATYPE_TCPSIGNATURE
:
3024 ipseclog((LOG_DEBUG
, "key_setsatval: invalid key_enc value.\n"));
3032 switch (mhp
->msg
->sadb_msg_satype
) {
3033 case SADB_SATYPE_ESP
:
3035 algo
= esp_algorithm_lookup(sav
->alg_enc
);
3036 if (algo
&& algo
->ivlen
)
3037 sav
->ivlen
= (*algo
->ivlen
)(algo
, sav
);
3038 if (sav
->ivlen
== 0)
3040 KMALLOC(sav
->iv
, caddr_t
, sav
->ivlen
);
3041 if (sav
->iv
== NULL
) {
3042 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
3048 key_randomfill(sav
->iv
, sav
->ivlen
);
3051 case SADB_SATYPE_AH
:
3052 case SADB_X_SATYPE_IPCOMP
:
3053 case SADB_X_SATYPE_TCPSIGNATURE
:
3056 ipseclog((LOG_DEBUG
, "key_setsaval: invalid SA type.\n"));
3063 sav
->created
= tv
.tv_sec
;
3065 /* make lifetime for CURRENT */
3066 KMALLOC(sav
->lft_c
, struct sadb_lifetime
*,
3067 sizeof(struct sadb_lifetime
));
3068 if (sav
->lft_c
== NULL
) {
3069 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
3076 sav
->lft_c
->sadb_lifetime_len
=
3077 PFKEY_UNIT64(sizeof(struct sadb_lifetime
));
3078 sav
->lft_c
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
3079 sav
->lft_c
->sadb_lifetime_allocations
= 0;
3080 sav
->lft_c
->sadb_lifetime_bytes
= 0;
3081 sav
->lft_c
->sadb_lifetime_addtime
= tv
.tv_sec
;
3082 sav
->lft_c
->sadb_lifetime_usetime
= 0;
3084 /* lifetimes for HARD and SOFT */
3086 const struct sadb_lifetime
*lft0
;
3088 lft0
= (struct sadb_lifetime
*)mhp
->ext
[SADB_EXT_LIFETIME_HARD
];
3090 if (mhp
->extlen
[SADB_EXT_LIFETIME_HARD
] < sizeof(*lft0
)) {
3094 sav
->lft_h
= (struct sadb_lifetime
*)key_newbuf(lft0
,
3096 if (sav
->lft_h
== NULL
) {
3097 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
3101 /* to be initialize ? */
3104 lft0
= (struct sadb_lifetime
*)mhp
->ext
[SADB_EXT_LIFETIME_SOFT
];
3106 if (mhp
->extlen
[SADB_EXT_LIFETIME_SOFT
] < sizeof(*lft0
)) {
3110 sav
->lft_s
= (struct sadb_lifetime
*)key_newbuf(lft0
,
3112 if (sav
->lft_s
== NULL
) {
3113 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
3117 /* to be initialize ? */
3124 /* initialization */
3125 if (sav
->replay
!= NULL
) {
3126 keydb_delsecreplay(sav
->replay
);
3129 if (sav
->key_auth
!= NULL
) {
3130 KFREE(sav
->key_auth
);
3131 sav
->key_auth
= NULL
;
3133 if (sav
->key_enc
!= NULL
) {
3134 KFREE(sav
->key_enc
);
3135 sav
->key_enc
= NULL
;
3141 if (sav
->iv
!= NULL
) {
3145 if (sav
->lft_c
!= NULL
) {
3149 if (sav
->lft_h
!= NULL
) {
3153 if (sav
->lft_s
!= NULL
) {
3162 * validation with a secasvar entry, and set SADB_SATYPE_MATURE.
3167 key_mature(struct secasvar
*sav
)
3170 int checkmask
= 0; /* 2^0: ealg 2^1: aalg 2^2: calg */
3171 int mustmask
= 0; /* 2^0: ealg 2^1: aalg 2^2: calg */
3175 /* check SPI value */
3176 switch (sav
->sah
->saidx
.proto
) {
3179 if (ntohl(sav
->spi
) >= 0 && ntohl(sav
->spi
) <= 255) {
3180 ipseclog((LOG_DEBUG
,
3181 "key_mature: illegal range of SPI %u.\n",
3182 (u_int32_t
)ntohl(sav
->spi
)));
3189 switch (sav
->sah
->saidx
.proto
) {
3192 if ((sav
->flags
& SADB_X_EXT_OLD
)
3193 && (sav
->flags
& SADB_X_EXT_DERIV
)) {
3194 ipseclog((LOG_DEBUG
, "key_mature: "
3195 "invalid flag (derived) given to old-esp.\n"));
3198 if (sav
->alg_auth
== SADB_AALG_NONE
)
3206 if (sav
->flags
& SADB_X_EXT_DERIV
) {
3207 ipseclog((LOG_DEBUG
, "key_mature: "
3208 "invalid flag (derived) given to AH SA.\n"));
3211 if (sav
->alg_enc
!= SADB_EALG_NONE
) {
3212 ipseclog((LOG_DEBUG
, "key_mature: "
3213 "protocol and algorithm mismated.\n"));
3219 case IPPROTO_IPCOMP
:
3220 if (sav
->alg_auth
!= SADB_AALG_NONE
) {
3221 ipseclog((LOG_DEBUG
, "key_mature: "
3222 "protocol and algorithm mismated.\n"));
3225 if ((sav
->flags
& SADB_X_EXT_RAWCPI
) == 0
3226 && ntohl(sav
->spi
) >= 0x10000) {
3227 ipseclog((LOG_DEBUG
, "key_mature: invalid cpi for IPComp.\n"));
3234 if (sav
->alg_auth
!= SADB_X_AALG_TCP_MD5
) {
3235 ipseclog((LOG_DEBUG
, "key_mature: "
3236 "protocol and algorithm mismated.\n"));
3243 ipseclog((LOG_DEBUG
, "key_mature: Invalid satype.\n"));
3244 return EPROTONOSUPPORT
;
3247 /* check authentication algorithm */
3248 if ((checkmask
& 2) != 0) {
3249 const struct ah_algorithm
*algo
;
3252 algo
= ah_algorithm_lookup(sav
->alg_auth
);
3254 ipseclog((LOG_DEBUG
,"key_mature: "
3255 "unknown authentication algorithm.\n"));
3259 /* algorithm-dependent check */
3261 keylen
= sav
->key_auth
->sadb_key_bits
;
3264 if (keylen
< algo
->keymin
|| algo
->keymax
< keylen
) {
3265 ipseclog((LOG_DEBUG
,
3266 "key_mature: invalid AH key length %d "
3267 "(%d-%d allowed)\n",
3268 keylen
, algo
->keymin
, algo
->keymax
));
3273 if ((*algo
->mature
)(sav
)) {
3274 /* message generated in per-algorithm function*/
3277 mature
= SADB_SATYPE_AH
;
3280 if ((mustmask
& 2) != 0 && mature
!= SADB_SATYPE_AH
) {
3281 ipseclog((LOG_DEBUG
, "key_mature: no satisfy algorithm for AH\n"));
3286 /* check encryption algorithm */
3287 if ((checkmask
& 1) != 0) {
3289 const struct esp_algorithm
*algo
;
3292 algo
= esp_algorithm_lookup(sav
->alg_enc
);
3294 ipseclog((LOG_DEBUG
, "key_mature: unknown encryption algorithm.\n"));
3298 /* algorithm-dependent check */
3300 keylen
= sav
->key_enc
->sadb_key_bits
;
3303 if (keylen
< algo
->keymin
|| algo
->keymax
< keylen
) {
3304 ipseclog((LOG_DEBUG
,
3305 "key_mature: invalid ESP key length %d "
3306 "(%d-%d allowed)\n",
3307 keylen
, algo
->keymin
, algo
->keymax
));
3312 if ((*algo
->mature
)(sav
)) {
3313 /* message generated in per-algorithm function*/
3316 mature
= SADB_SATYPE_ESP
;
3319 if ((mustmask
& 1) != 0 && mature
!= SADB_SATYPE_ESP
) {
3320 ipseclog((LOG_DEBUG
, "key_mature: no satisfy algorithm for ESP\n"));
3324 ipseclog((LOG_DEBUG
, "key_mature: ESP not supported in this configuration\n"));
3329 /* check compression algorithm */
3330 if ((checkmask
& 4) != 0) {
3331 const struct ipcomp_algorithm
*algo
;
3333 /* algorithm-dependent check */
3334 algo
= ipcomp_algorithm_lookup(sav
->alg_enc
);
3336 ipseclog((LOG_DEBUG
, "key_mature: unknown compression algorithm.\n"));
3341 key_sa_chgstate(sav
, SADB_SASTATE_MATURE
);
3347 * subroutine for SADB_GET and SADB_DUMP.
3349 static struct mbuf
*
3350 key_setdumpsa(struct secasvar
*sav
, u_int8_t type
, u_int8_t satype
,
3351 u_int32_t seq
, u_int32_t pid
)
3353 struct mbuf
*result
= NULL
, *tres
= NULL
, *m
;
3358 SADB_EXT_SA
, SADB_X_EXT_SA2
,
3359 SADB_EXT_LIFETIME_HARD
, SADB_EXT_LIFETIME_SOFT
,
3360 SADB_EXT_LIFETIME_CURRENT
, SADB_EXT_ADDRESS_SRC
,
3361 SADB_EXT_ADDRESS_DST
, SADB_EXT_ADDRESS_PROXY
, SADB_EXT_KEY_AUTH
,
3362 SADB_EXT_KEY_ENCRYPT
, SADB_EXT_IDENTITY_SRC
,
3363 SADB_EXT_IDENTITY_DST
, SADB_EXT_SENSITIVITY
,
3366 m
= key_setsadbmsg(type
, 0, satype
, seq
, pid
, sav
->refcnt
);
3371 for (i
= NELEM(dumporder
) - 1; i
>= 0; i
--) {
3374 switch (dumporder
[i
]) {
3376 m
= key_setsadbsa(sav
);
3381 case SADB_X_EXT_SA2
:
3382 m
= key_setsadbxsa2(sav
->sah
->saidx
.mode
,
3383 sav
->replay
? sav
->replay
->count
: 0,
3384 sav
->sah
->saidx
.reqid
);
3389 case SADB_EXT_ADDRESS_SRC
:
3390 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
3391 (struct sockaddr
*)&sav
->sah
->saidx
.src
,
3392 FULLMASK
, IPSEC_ULPROTO_ANY
);
3397 case SADB_EXT_ADDRESS_DST
:
3398 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
3399 (struct sockaddr
*)&sav
->sah
->saidx
.dst
,
3400 FULLMASK
, IPSEC_ULPROTO_ANY
);
3405 case SADB_EXT_KEY_AUTH
:
3408 l
= PFKEY_UNUNIT64(sav
->key_auth
->sadb_key_len
);
3412 case SADB_EXT_KEY_ENCRYPT
:
3415 l
= PFKEY_UNUNIT64(sav
->key_enc
->sadb_key_len
);
3419 case SADB_EXT_LIFETIME_CURRENT
:
3422 l
= PFKEY_UNUNIT64(((struct sadb_ext
*)sav
->lft_c
)->sadb_ext_len
);
3426 case SADB_EXT_LIFETIME_HARD
:
3429 l
= PFKEY_UNUNIT64(((struct sadb_ext
*)sav
->lft_h
)->sadb_ext_len
);
3433 case SADB_EXT_LIFETIME_SOFT
:
3436 l
= PFKEY_UNUNIT64(((struct sadb_ext
*)sav
->lft_s
)->sadb_ext_len
);
3440 case SADB_EXT_ADDRESS_PROXY
:
3441 case SADB_EXT_IDENTITY_SRC
:
3442 case SADB_EXT_IDENTITY_DST
:
3443 /* XXX: should we brought from SPD ? */
3444 case SADB_EXT_SENSITIVITY
:
3449 if ((!m
&& !p
) || (m
&& p
))
3452 M_PREPEND(tres
, l
, M_NOWAIT
);
3455 bcopy(p
, mtod(tres
, caddr_t
), l
);
3459 m
= key_alloc_mbuf(l
);
3462 m_copyback(m
, 0, l
, p
);
3470 m_cat(result
, tres
);
3472 if (result
->m_len
< sizeof(struct sadb_msg
)) {
3473 result
= m_pullup(result
, sizeof(struct sadb_msg
));
3478 result
->m_pkthdr
.len
= 0;
3479 for (m
= result
; m
; m
= m
->m_next
)
3480 result
->m_pkthdr
.len
+= m
->m_len
;
3482 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
3483 PFKEY_UNIT64(result
->m_pkthdr
.len
);
3494 * set data into sadb_msg.
3496 static struct mbuf
*
3497 key_setsadbmsg(u_int8_t type
, u_int16_t tlen
, u_int8_t satype
,
3498 u_int32_t seq
, pid_t pid
, u_int16_t reserved
)
3504 len
= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
3507 MGETHDR(m
, M_NOWAIT
, MT_DATA
);
3508 if (m
&& len
> MHLEN
) {
3509 MCLGET(m
, M_NOWAIT
);
3510 if ((m
->m_flags
& M_EXT
) == 0) {
3517 m
->m_pkthdr
.len
= m
->m_len
= len
;
3520 p
= mtod(m
, struct sadb_msg
*);
3523 p
->sadb_msg_version
= PF_KEY_V2
;
3524 p
->sadb_msg_type
= type
;
3525 p
->sadb_msg_errno
= 0;
3526 p
->sadb_msg_satype
= satype
;
3527 p
->sadb_msg_len
= PFKEY_UNIT64(tlen
);
3528 p
->sadb_msg_reserved
= reserved
;
3529 p
->sadb_msg_seq
= seq
;
3530 p
->sadb_msg_pid
= (u_int32_t
)pid
;
3536 * copy secasvar data into sadb_address.
3538 static struct mbuf
*
3539 key_setsadbsa(struct secasvar
*sav
)
3545 len
= PFKEY_ALIGN8(sizeof(struct sadb_sa
));
3546 m
= key_alloc_mbuf(len
);
3547 if (!m
|| m
->m_next
) { /*XXX*/
3553 p
= mtod(m
, struct sadb_sa
*);
3556 p
->sadb_sa_len
= PFKEY_UNIT64(len
);
3557 p
->sadb_sa_exttype
= SADB_EXT_SA
;
3558 p
->sadb_sa_spi
= sav
->spi
;
3559 p
->sadb_sa_replay
= (sav
->replay
!= NULL
? sav
->replay
->wsize
: 0);
3560 p
->sadb_sa_state
= sav
->state
;
3561 p
->sadb_sa_auth
= sav
->alg_auth
;
3562 p
->sadb_sa_encrypt
= sav
->alg_enc
;
3563 p
->sadb_sa_flags
= sav
->flags
;
3569 * set data into sadb_address.
3571 static struct mbuf
*
3572 key_setsadbaddr(u_int16_t exttype
, struct sockaddr
*saddr
,
3573 u_int8_t prefixlen
, u_int16_t ul_proto
)
3576 struct sadb_address
*p
;
3579 len
= PFKEY_ALIGN8(sizeof(struct sadb_address
)) +
3580 PFKEY_ALIGN8(saddr
->sa_len
);
3581 m
= key_alloc_mbuf(len
);
3582 if (!m
|| m
->m_next
) { /*XXX*/
3588 p
= mtod(m
, struct sadb_address
*);
3591 p
->sadb_address_len
= PFKEY_UNIT64(len
);
3592 p
->sadb_address_exttype
= exttype
;
3593 p
->sadb_address_proto
= ul_proto
;
3594 if (prefixlen
== FULLMASK
) {
3595 switch (saddr
->sa_family
) {
3597 prefixlen
= sizeof(struct in_addr
) << 3;
3600 prefixlen
= sizeof(struct in6_addr
) << 3;
3606 p
->sadb_address_prefixlen
= prefixlen
;
3607 p
->sadb_address_reserved
= 0;
3610 mtod(m
, caddr_t
) + PFKEY_ALIGN8(sizeof(struct sadb_address
)),
3618 * set data into sadb_ident.
3620 static struct mbuf
*
3621 key_setsadbident(u_int16_t exttype
, u_int16_t idtype
, caddr_t string
,
3622 int stringlen
, u_int64_t id
)
3625 struct sadb_ident
*p
;
3628 len
= PFKEY_ALIGN8(sizeof(struct sadb_ident
)) + PFKEY_ALIGN8(stringlen
);
3629 m
= key_alloc_mbuf(len
);
3630 if (!m
|| m
->m_next
) { /*XXX*/
3636 p
= mtod(m
, struct sadb_ident
*);
3639 p
->sadb_ident_len
= PFKEY_UNIT64(len
);
3640 p
->sadb_ident_exttype
= exttype
;
3641 p
->sadb_ident_type
= idtype
;
3642 p
->sadb_ident_reserved
= 0;
3643 p
->sadb_ident_id
= id
;
3646 mtod(m
, caddr_t
) + PFKEY_ALIGN8(sizeof(struct sadb_ident
)),
3654 * set data into sadb_x_sa2.
3656 static struct mbuf
*
3657 key_setsadbxsa2(u_int8_t mode
, u_int32_t seq
, u_int32_t reqid
)
3660 struct sadb_x_sa2
*p
;
3663 len
= PFKEY_ALIGN8(sizeof(struct sadb_x_sa2
));
3664 m
= key_alloc_mbuf(len
);
3665 if (!m
|| m
->m_next
) { /*XXX*/
3671 p
= mtod(m
, struct sadb_x_sa2
*);
3674 p
->sadb_x_sa2_len
= PFKEY_UNIT64(len
);
3675 p
->sadb_x_sa2_exttype
= SADB_X_EXT_SA2
;
3676 p
->sadb_x_sa2_mode
= mode
;
3677 p
->sadb_x_sa2_reserved1
= 0;
3678 p
->sadb_x_sa2_reserved2
= 0;
3679 p
->sadb_x_sa2_sequence
= seq
;
3680 p
->sadb_x_sa2_reqid
= reqid
;
3686 * set data into sadb_x_policy
3688 static struct mbuf
*
3689 key_setsadbxpolicy(u_int16_t type
, u_int8_t dir
, u_int32_t id
)
3692 struct sadb_x_policy
*p
;
3695 len
= PFKEY_ALIGN8(sizeof(struct sadb_x_policy
));
3696 m
= key_alloc_mbuf(len
);
3697 if (!m
|| m
->m_next
) { /*XXX*/
3703 p
= mtod(m
, struct sadb_x_policy
*);
3706 p
->sadb_x_policy_len
= PFKEY_UNIT64(len
);
3707 p
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
3708 p
->sadb_x_policy_type
= type
;
3709 p
->sadb_x_policy_dir
= dir
;
3710 p
->sadb_x_policy_id
= id
;
3717 * copy a buffer into the new buffer allocated.
3720 key_newbuf(const void *src
, u_int len
)
3724 KMALLOC(new, caddr_t
, len
);
3726 ipseclog((LOG_DEBUG
, "key_newbuf: No more memory.\n"));
3729 bcopy(src
, new, len
);
3734 /* compare my own address
3735 * OUT: 1: true, i.e. my address.
3739 key_ismyaddr(struct sockaddr
*sa
)
3742 struct sockaddr_in
*sin
;
3743 struct in_ifaddr_container
*iac
;
3749 panic("key_ismyaddr: NULL pointer is passed.");
3751 lwkt_gettoken(&key_token
);
3752 switch (sa
->sa_family
) {
3755 sin
= (struct sockaddr_in
*)sa
;
3756 TAILQ_FOREACH(iac
, &in_ifaddrheads
[mycpuid
], ia_link
) {
3757 struct in_ifaddr
*ia
= iac
->ia
;
3759 if (sin
->sin_family
== ia
->ia_addr
.sin_family
&&
3760 sin
->sin_len
== ia
->ia_addr
.sin_len
&&
3761 sin
->sin_addr
.s_addr
== ia
->ia_addr
.sin_addr
.s_addr
)
3763 lwkt_reltoken(&key_token
);
3772 res
= key_ismyaddr6((struct sockaddr_in6
*)sa
);
3779 lwkt_reltoken(&key_token
);
3785 * compare my own address for IPv6.
3788 * NOTE: derived ip6_input() in KAME. This is necessary to modify more.
3791 key_ismyaddr6(struct sockaddr_in6
*sin6
)
3793 struct in6_ifaddr
*ia
;
3794 struct in6_multi
*in6m
;
3796 for (ia
= in6_ifaddr
; ia
; ia
= ia
->ia_next
) {
3797 if (key_sockaddrcmp((struct sockaddr
*)&sin6
,
3798 (struct sockaddr
*)&ia
->ia_addr
, 0) == 0)
3803 * XXX why do we care about multlicast here while we don't care
3804 * about IPv4 multicast??
3807 in6m
= IN6_LOOKUP_MULTI(&sin6
->sin6_addr
, ia
->ia_ifp
);
3812 /* loopback, just for safety */
3813 if (IN6_IS_ADDR_LOOPBACK(&sin6
->sin6_addr
))
3821 * compare two secasindex structure.
3822 * flag can specify to compare 2 saidxes.
3823 * compare two secasindex structure without both mode and reqid.
3824 * don't compare port.
3826 * saidx0: source, it can be in SAD.
3833 key_cmpsaidx(struct secasindex
*saidx0
, struct secasindex
*saidx1
,
3837 if (saidx0
== NULL
&& saidx1
== NULL
)
3840 if (saidx0
== NULL
|| saidx1
== NULL
)
3843 if (saidx0
->proto
!= saidx1
->proto
)
3846 if (flag
== CMP_EXACTLY
) {
3847 if (saidx0
->mode
!= saidx1
->mode
)
3849 if (saidx0
->reqid
!= saidx1
->reqid
)
3851 if (bcmp(&saidx0
->src
, &saidx1
->src
, saidx0
->src
.ss_len
) != 0 ||
3852 bcmp(&saidx0
->dst
, &saidx1
->dst
, saidx0
->dst
.ss_len
) != 0)
3856 /* CMP_MODE_REQID, CMP_REQID, CMP_HEAD */
3857 if (flag
== CMP_MODE_REQID
3858 ||flag
== CMP_REQID
) {
3860 * If reqid of SPD is non-zero, unique SA is required.
3861 * The result must be of same reqid in this case.
3863 if (saidx1
->reqid
!= 0 && saidx0
->reqid
!= saidx1
->reqid
)
3867 if (flag
== CMP_MODE_REQID
) {
3868 if (saidx0
->mode
!= IPSEC_MODE_ANY
3869 && saidx0
->mode
!= saidx1
->mode
)
3873 if (key_sockaddrcmp((struct sockaddr
*)&saidx0
->src
,
3874 (struct sockaddr
*)&saidx1
->src
, 0) != 0) {
3877 if (key_sockaddrcmp((struct sockaddr
*)&saidx0
->dst
,
3878 (struct sockaddr
*)&saidx1
->dst
, 0) != 0) {
3887 * compare two secindex structure exactly.
3889 * spidx0: source, it is often in SPD.
3890 * spidx1: object, it is often from PFKEY message.
3896 key_cmpspidx_exactly(struct secpolicyindex
*spidx0
,
3897 struct secpolicyindex
*spidx1
)
3900 if (spidx0
== NULL
&& spidx1
== NULL
)
3903 if (spidx0
== NULL
|| spidx1
== NULL
)
3906 if (spidx0
->prefs
!= spidx1
->prefs
3907 || spidx0
->prefd
!= spidx1
->prefd
3908 || spidx0
->ul_proto
!= spidx1
->ul_proto
)
3911 if (key_sockaddrcmp((struct sockaddr
*)&spidx0
->src
,
3912 (struct sockaddr
*)&spidx1
->src
, 1) != 0) {
3915 if (key_sockaddrcmp((struct sockaddr
*)&spidx0
->dst
,
3916 (struct sockaddr
*)&spidx1
->dst
, 1) != 0) {
3924 * compare two secindex structure with mask.
3926 * spidx0: source, it is often in SPD.
3927 * spidx1: object, it is often from IP header.
3933 key_cmpspidx_withmask(struct secpolicyindex
*spidx0
,
3934 struct secpolicyindex
*spidx1
)
3937 if (spidx0
== NULL
&& spidx1
== NULL
)
3940 if (spidx0
== NULL
|| spidx1
== NULL
)
3943 if (spidx0
->src
.ss_family
!= spidx1
->src
.ss_family
||
3944 spidx0
->dst
.ss_family
!= spidx1
->dst
.ss_family
||
3945 spidx0
->src
.ss_len
!= spidx1
->src
.ss_len
||
3946 spidx0
->dst
.ss_len
!= spidx1
->dst
.ss_len
)
3949 /* if spidx.ul_proto == IPSEC_ULPROTO_ANY, ignore. */
3950 if (spidx0
->ul_proto
!= (u_int16_t
)IPSEC_ULPROTO_ANY
3951 && spidx0
->ul_proto
!= spidx1
->ul_proto
)
3954 switch (spidx0
->src
.ss_family
) {
3956 if (satosin(&spidx0
->src
)->sin_port
!= IPSEC_PORT_ANY
3957 && satosin(&spidx0
->src
)->sin_port
!=
3958 satosin(&spidx1
->src
)->sin_port
)
3960 if (!key_bbcmp((caddr_t
)&satosin(&spidx0
->src
)->sin_addr
,
3961 (caddr_t
)&satosin(&spidx1
->src
)->sin_addr
, spidx0
->prefs
))
3965 if (satosin6(&spidx0
->src
)->sin6_port
!= IPSEC_PORT_ANY
3966 && satosin6(&spidx0
->src
)->sin6_port
!=
3967 satosin6(&spidx1
->src
)->sin6_port
)
3970 * scope_id check. if sin6_scope_id is 0, we regard it
3971 * as a wildcard scope, which matches any scope zone ID.
3973 if (satosin6(&spidx0
->src
)->sin6_scope_id
&&
3974 satosin6(&spidx1
->src
)->sin6_scope_id
&&
3975 satosin6(&spidx0
->src
)->sin6_scope_id
!=
3976 satosin6(&spidx1
->src
)->sin6_scope_id
)
3978 if (!key_bbcmp((caddr_t
)&satosin6(&spidx0
->src
)->sin6_addr
,
3979 (caddr_t
)&satosin6(&spidx1
->src
)->sin6_addr
, spidx0
->prefs
))
3984 if (bcmp(&spidx0
->src
, &spidx1
->src
, spidx0
->src
.ss_len
) != 0)
3989 switch (spidx0
->dst
.ss_family
) {
3991 if (satosin(&spidx0
->dst
)->sin_port
!= IPSEC_PORT_ANY
3992 && satosin(&spidx0
->dst
)->sin_port
!=
3993 satosin(&spidx1
->dst
)->sin_port
)
3995 if (!key_bbcmp((caddr_t
)&satosin(&spidx0
->dst
)->sin_addr
,
3996 (caddr_t
)&satosin(&spidx1
->dst
)->sin_addr
, spidx0
->prefd
))
4000 if (satosin6(&spidx0
->dst
)->sin6_port
!= IPSEC_PORT_ANY
4001 && satosin6(&spidx0
->dst
)->sin6_port
!=
4002 satosin6(&spidx1
->dst
)->sin6_port
)
4005 * scope_id check. if sin6_scope_id is 0, we regard it
4006 * as a wildcard scope, which matches any scope zone ID.
4008 if (satosin6(&spidx0
->src
)->sin6_scope_id
&&
4009 satosin6(&spidx1
->src
)->sin6_scope_id
&&
4010 satosin6(&spidx0
->dst
)->sin6_scope_id
!=
4011 satosin6(&spidx1
->dst
)->sin6_scope_id
)
4013 if (!key_bbcmp((caddr_t
)&satosin6(&spidx0
->dst
)->sin6_addr
,
4014 (caddr_t
)&satosin6(&spidx1
->dst
)->sin6_addr
, spidx0
->prefd
))
4019 if (bcmp(&spidx0
->dst
, &spidx1
->dst
, spidx0
->dst
.ss_len
) != 0)
4024 /* XXX Do we check other field ? e.g. flowinfo */
4029 /* returns 0 on match */
4031 key_sockaddrcmp(struct sockaddr
*sa1
, struct sockaddr
*sa2
, int port
)
4033 if (sa1
->sa_family
!= sa2
->sa_family
|| sa1
->sa_len
!= sa2
->sa_len
)
4036 switch (sa1
->sa_family
) {
4038 if (sa1
->sa_len
!= sizeof(struct sockaddr_in
))
4040 if (satosin(sa1
)->sin_addr
.s_addr
!=
4041 satosin(sa2
)->sin_addr
.s_addr
) {
4044 if (port
&& satosin(sa1
)->sin_port
!= satosin(sa2
)->sin_port
)
4048 if (sa1
->sa_len
!= sizeof(struct sockaddr_in6
))
4049 return 1; /*EINVAL*/
4050 if (satosin6(sa1
)->sin6_scope_id
!=
4051 satosin6(sa2
)->sin6_scope_id
) {
4054 if (!IN6_ARE_ADDR_EQUAL(&satosin6(sa1
)->sin6_addr
,
4055 &satosin6(sa2
)->sin6_addr
)) {
4059 satosin6(sa1
)->sin6_port
!= satosin6(sa2
)->sin6_port
) {
4063 if (bcmp(sa1
, sa2
, sa1
->sa_len
) != 0)
4072 * compare two buffers with mask.
4076 * bits: Number of bits to compare
4082 key_bbcmp(caddr_t p1
, caddr_t p2
, u_int bits
)
4086 /* XXX: This could be considerably faster if we compare a word
4087 * at a time, but it is complicated on LSB Endian machines */
4089 /* Handle null pointers */
4090 if (p1
== NULL
|| p2
== NULL
)
4100 mask
= ~((1<<(8-bits
))-1);
4101 if ((*p1
& mask
) != (*p2
& mask
))
4104 return 1; /* Match! */
4109 * scanning SPD and SAD to check status for each entries,
4110 * and do to remove or to expire.
4111 * XXX: year 2038 problem may remain.
4114 key_timehandler(void *__dummy
)
4121 lwkt_gettoken(&key_token
);
4125 struct secpolicy
*sp
, *nextsp
;
4127 for (dir
= 0; dir
< IPSEC_DIR_MAX
; dir
++) {
4128 for (sp
= LIST_FIRST(&sptree
[dir
]);
4132 nextsp
= LIST_NEXT(sp
, chain
);
4134 if (sp
->state
== IPSEC_SPSTATE_DEAD
) {
4139 if (sp
->lifetime
== 0 && sp
->validtime
== 0)
4142 /* the deletion will occur next time */
4144 && tv
.tv_sec
- sp
->created
> sp
->lifetime
)
4146 && tv
.tv_sec
- sp
->lastused
> sp
->validtime
)) {
4147 sp
->state
= IPSEC_SPSTATE_DEAD
;
4157 struct secashead
*sah
, *nextsah
;
4158 struct secasvar
*sav
, *nextsav
;
4160 for (sah
= LIST_FIRST(&sahtree
);
4164 nextsah
= LIST_NEXT(sah
, chain
);
4166 /* if sah has been dead, then delete it and process next sah. */
4167 if (sah
->state
== SADB_SASTATE_DEAD
) {
4172 /* if LARVAL entry doesn't become MATURE, delete it. */
4173 for (sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_LARVAL
]);
4177 nextsav
= LIST_NEXT(sav
, chain
);
4179 if (tv
.tv_sec
- sav
->created
> key_larval_lifetime
) {
4185 * check MATURE entry to start to send expire message
4188 for (sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_MATURE
]);
4192 nextsav
= LIST_NEXT(sav
, chain
);
4194 /* we don't need to check. */
4195 if (sav
->lft_s
== NULL
)
4199 if (sav
->lft_c
== NULL
) {
4200 ipseclog((LOG_DEBUG
,"key_timehandler: "
4201 "There is no CURRENT time, why?\n"));
4205 /* check SOFT lifetime */
4206 if (sav
->lft_s
->sadb_lifetime_addtime
!= 0
4207 && tv
.tv_sec
- sav
->created
> sav
->lft_s
->sadb_lifetime_addtime
) {
4209 * check the SA if it has been used.
4210 * when it hasn't been used, delete it.
4211 * i don't think such SA will be used.
4213 if (sav
->lft_c
->sadb_lifetime_usetime
== 0) {
4214 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
4218 key_sa_chgstate(sav
, SADB_SASTATE_DYING
);
4220 * XXX If we keep to send expire
4221 * message in the status of
4222 * DYING. Do remove below code.
4228 /* check SOFT lifetime by bytes */
4230 * XXX I don't know the way to delete this SA
4231 * when new SA is installed. Caution when it's
4232 * installed too big lifetime by time.
4234 else if (sav
->lft_s
->sadb_lifetime_bytes
!= 0
4235 && sav
->lft_s
->sadb_lifetime_bytes
< sav
->lft_c
->sadb_lifetime_bytes
) {
4237 key_sa_chgstate(sav
, SADB_SASTATE_DYING
);
4239 * XXX If we keep to send expire
4240 * message in the status of
4241 * DYING. Do remove below code.
4247 /* check DYING entry to change status to DEAD. */
4248 for (sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_DYING
]);
4252 nextsav
= LIST_NEXT(sav
, chain
);
4254 /* we don't need to check. */
4255 if (sav
->lft_h
== NULL
)
4259 if (sav
->lft_c
== NULL
) {
4260 ipseclog((LOG_DEBUG
, "key_timehandler: "
4261 "There is no CURRENT time, why?\n"));
4265 if (sav
->lft_h
->sadb_lifetime_addtime
!= 0
4266 && tv
.tv_sec
- sav
->created
> sav
->lft_h
->sadb_lifetime_addtime
) {
4267 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
4271 #if 0 /* XXX Should we keep to send expire message until HARD lifetime ? */
4272 else if (sav
->lft_s
!= NULL
4273 && sav
->lft_s
->sadb_lifetime_addtime
!= 0
4274 && tv
.tv_sec
- sav
->created
> sav
->lft_s
->sadb_lifetime_addtime
) {
4276 * XXX: should be checked to be
4277 * installed the valid SA.
4281 * If there is no SA then sending
4287 /* check HARD lifetime by bytes */
4288 else if (sav
->lft_h
->sadb_lifetime_bytes
!= 0
4289 && sav
->lft_h
->sadb_lifetime_bytes
< sav
->lft_c
->sadb_lifetime_bytes
) {
4290 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
4296 /* delete entry in DEAD */
4297 for (sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_DEAD
]);
4301 nextsav
= LIST_NEXT(sav
, chain
);
4304 if (sav
->state
!= SADB_SASTATE_DEAD
) {
4305 ipseclog((LOG_DEBUG
, "key_timehandler: "
4306 "invalid sav->state "
4307 "(queue: %d SA: %d): "
4309 SADB_SASTATE_DEAD
, sav
->state
));
4313 * do not call key_freesav() here.
4314 * sav should already be freed, and sav->refcnt
4315 * shows other references to sav
4316 * (such as from SPD).
4322 #ifndef IPSEC_NONBLOCK_ACQUIRE
4325 struct secacq
*acq
, *nextacq
;
4327 for (acq
= LIST_FIRST(&acqtree
);
4331 nextacq
= LIST_NEXT(acq
, chain
);
4333 if (tv
.tv_sec
- acq
->created
> key_blockacq_lifetime
4334 && __LIST_CHAINED(acq
)) {
4335 LIST_REMOVE(acq
, chain
);
4344 struct secspacq
*acq
, *nextacq
;
4346 for (acq
= LIST_FIRST(&spacqtree
);
4350 nextacq
= LIST_NEXT(acq
, chain
);
4352 if (tv
.tv_sec
- acq
->created
> key_blockacq_lifetime
4353 && __LIST_CHAINED(acq
)) {
4354 LIST_REMOVE(acq
, chain
);
4360 /* initialize random seed */
4361 if (key_tick_init_random
++ > key_int_random
) {
4362 key_tick_init_random
= 0;
4366 #ifndef IPSEC_DEBUG2
4367 /* do exchange to tick time !! */
4368 callout_reset(&key_timehandler_ch
, hz
, key_timehandler
, NULL
);
4369 #endif /* IPSEC_DEBUG2 */
4371 lwkt_reltoken(&key_token
);
4376 * to initialize a seed for random()
4385 skrandom(tv
.tv_usec
);
4395 key_randomfill(&value
, sizeof(value
));
4400 key_randomfill(void *p
, size_t l
)
4404 static int warn
= 1;
4407 n
= (size_t)read_random(p
, (u_int
)l
);
4411 bcopy(&v
, (u_int8_t
*)p
+ n
,
4412 l
- n
< sizeof(v
) ? l
- n
: sizeof(v
));
4416 kprintf("WARNING: pseudo-random number generator "
4417 "used for IPsec processing\n");
4424 * map SADB_SATYPE_* to IPPROTO_*.
4425 * if satype == SADB_SATYPE then satype is mapped to ~0.
4427 * 0: invalid satype.
4430 key_satype2proto(u_int8_t satype
)
4433 case SADB_SATYPE_UNSPEC
:
4434 return IPSEC_PROTO_ANY
;
4435 case SADB_SATYPE_AH
:
4437 case SADB_SATYPE_ESP
:
4439 case SADB_X_SATYPE_IPCOMP
:
4440 return IPPROTO_IPCOMP
;
4441 case SADB_X_SATYPE_TCPSIGNATURE
:
4451 * map IPPROTO_* to SADB_SATYPE_*
4453 * 0: invalid protocol type.
4456 key_proto2satype(u_int16_t proto
)
4460 return SADB_SATYPE_AH
;
4462 return SADB_SATYPE_ESP
;
4463 case IPPROTO_IPCOMP
:
4464 return SADB_X_SATYPE_IPCOMP
;
4466 return SADB_X_SATYPE_TCPSIGNATURE
;
4476 * SADB_GETSPI processing is to receive
4477 * <base, (SA2), src address, dst address, (SPI range)>
4478 * from the IKMPd, to assign a unique spi value, to hang on the INBOUND
4479 * tree with the status of LARVAL, and send
4480 * <base, SA(*), address(SD)>
4483 * IN: mhp: pointer to the pointer to each header.
4484 * OUT: NULL if fail.
4485 * other if success, return pointer to the message to send.
4488 key_getspi(struct socket
*so
, struct mbuf
*m
,
4489 const struct sadb_msghdr
*mhp
)
4491 struct sadb_address
*src0
, *dst0
;
4492 struct secasindex saidx
;
4493 struct secashead
*newsah
;
4494 struct secasvar
*newsav
;
4502 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
4503 panic("key_getspi: NULL pointer is passed.");
4505 if (mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
4506 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
) {
4507 ipseclog((LOG_DEBUG
, "key_getspi: invalid message is passed.\n"));
4508 return key_senderror(so
, m
, EINVAL
);
4510 if (mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
4511 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
4512 ipseclog((LOG_DEBUG
, "key_getspi: invalid message is passed.\n"));
4513 return key_senderror(so
, m
, EINVAL
);
4515 if (mhp
->ext
[SADB_X_EXT_SA2
] != NULL
) {
4516 mode
= ((struct sadb_x_sa2
*)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_mode
;
4517 reqid
= ((struct sadb_x_sa2
*)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_reqid
;
4519 mode
= IPSEC_MODE_ANY
;
4523 src0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_SRC
]);
4524 dst0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_DST
]);
4526 /* map satype to proto */
4527 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
4528 ipseclog((LOG_DEBUG
, "key_getspi: invalid satype is passed.\n"));
4529 return key_senderror(so
, m
, EINVAL
);
4532 /* make sure if port number is zero. */
4533 switch (((struct sockaddr
*)(src0
+ 1))->sa_family
) {
4535 if (((struct sockaddr
*)(src0
+ 1))->sa_len
!=
4536 sizeof(struct sockaddr_in
))
4537 return key_senderror(so
, m
, EINVAL
);
4538 ((struct sockaddr_in
*)(src0
+ 1))->sin_port
= 0;
4541 if (((struct sockaddr
*)(src0
+ 1))->sa_len
!=
4542 sizeof(struct sockaddr_in6
))
4543 return key_senderror(so
, m
, EINVAL
);
4544 ((struct sockaddr_in6
*)(src0
+ 1))->sin6_port
= 0;
4549 switch (((struct sockaddr
*)(dst0
+ 1))->sa_family
) {
4551 if (((struct sockaddr
*)(dst0
+ 1))->sa_len
!=
4552 sizeof(struct sockaddr_in
))
4553 return key_senderror(so
, m
, EINVAL
);
4554 ((struct sockaddr_in
*)(dst0
+ 1))->sin_port
= 0;
4557 if (((struct sockaddr
*)(dst0
+ 1))->sa_len
!=
4558 sizeof(struct sockaddr_in6
))
4559 return key_senderror(so
, m
, EINVAL
);
4560 ((struct sockaddr_in6
*)(dst0
+ 1))->sin6_port
= 0;
4566 /* XXX boundary check against sa_len */
4567 KEY_SETSECASIDX(proto
, mode
, reqid
, src0
+ 1, dst0
+ 1, &saidx
);
4569 /* SPI allocation */
4570 spi
= key_do_getnewspi((struct sadb_spirange
*)mhp
->ext
[SADB_EXT_SPIRANGE
],
4573 return key_senderror(so
, m
, EINVAL
);
4575 /* get a SA index */
4576 if ((newsah
= key_getsah(&saidx
)) == NULL
) {
4577 /* create a new SA index */
4578 if ((newsah
= key_newsah(&saidx
)) == NULL
) {
4579 ipseclog((LOG_DEBUG
, "key_getspi: No more memory.\n"));
4580 return key_senderror(so
, m
, ENOBUFS
);
4586 newsav
= key_newsav(m
, mhp
, newsah
, &error
);
4587 if (newsav
== NULL
) {
4588 /* XXX don't free new SA index allocated in above. */
4589 return key_senderror(so
, m
, error
);
4593 newsav
->spi
= htonl(spi
);
4595 #ifndef IPSEC_NONBLOCK_ACQUIRE
4596 /* delete the entry in acqtree */
4597 if (mhp
->msg
->sadb_msg_seq
!= 0) {
4599 if ((acq
= key_getacqbyseq(mhp
->msg
->sadb_msg_seq
)) != NULL
) {
4600 /* reset counter in order to deletion by timehandler. */
4603 acq
->created
= tv
.tv_sec
;
4610 struct mbuf
*n
, *nn
;
4611 struct sadb_sa
*m_sa
;
4612 struct sadb_msg
*newmsg
;
4615 /* create new sadb_msg to reply. */
4616 len
= PFKEY_ALIGN8(sizeof(struct sadb_msg
)) +
4617 PFKEY_ALIGN8(sizeof(struct sadb_sa
));
4619 return key_senderror(so
, m
, ENOBUFS
);
4621 MGETHDR(n
, M_NOWAIT
, MT_DATA
);
4623 MCLGET(n
, M_NOWAIT
);
4624 if ((n
->m_flags
& M_EXT
) == 0) {
4630 return key_senderror(so
, m
, ENOBUFS
);
4636 m_copydata(m
, 0, sizeof(struct sadb_msg
), mtod(n
, caddr_t
) + off
);
4637 off
+= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
4639 m_sa
= (struct sadb_sa
*)(mtod(n
, caddr_t
) + off
);
4640 m_sa
->sadb_sa_len
= PFKEY_UNIT64(sizeof(struct sadb_sa
));
4641 m_sa
->sadb_sa_exttype
= SADB_EXT_SA
;
4642 m_sa
->sadb_sa_spi
= htonl(spi
);
4643 off
+= PFKEY_ALIGN8(sizeof(struct sadb_sa
));
4647 panic("length inconsistency in key_getspi");
4650 n
->m_next
= key_gather_mbuf(m
, mhp
, 0, 2, SADB_EXT_ADDRESS_SRC
,
4651 SADB_EXT_ADDRESS_DST
);
4654 return key_senderror(so
, m
, ENOBUFS
);
4657 if (n
->m_len
< sizeof(struct sadb_msg
)) {
4658 n
= m_pullup(n
, sizeof(struct sadb_msg
));
4660 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ONE
);
4663 n
->m_pkthdr
.len
= 0;
4664 for (nn
= n
; nn
; nn
= nn
->m_next
)
4665 n
->m_pkthdr
.len
+= nn
->m_len
;
4667 newmsg
= mtod(n
, struct sadb_msg
*);
4668 newmsg
->sadb_msg_seq
= newsav
->seq
;
4669 newmsg
->sadb_msg_errno
= 0;
4670 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
4673 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ONE
);
4678 * allocating new SPI
4679 * called by key_getspi().
4685 key_do_getnewspi(struct sadb_spirange
*spirange
,
4686 struct secasindex
*saidx
)
4690 int count
= key_spi_trycnt
;
4692 /* set spi range to allocate */
4693 if (spirange
!= NULL
) {
4694 min
= spirange
->sadb_spirange_min
;
4695 max
= spirange
->sadb_spirange_max
;
4697 min
= key_spi_minval
;
4698 max
= key_spi_maxval
;
4700 /* IPCOMP needs 2-byte SPI */
4701 if (saidx
->proto
== IPPROTO_IPCOMP
) {
4708 t
= min
; min
= max
; max
= t
;
4713 if (key_checkspidup(saidx
, min
) != NULL
) {
4714 ipseclog((LOG_DEBUG
, "key_do_getnewspi: SPI %u exists already.\n", min
));
4718 count
--; /* taking one cost. */
4726 /* when requesting to allocate spi ranged */
4728 /* generate pseudo-random SPI value ranged. */
4729 newspi
= min
+ (key_random() % (max
- min
+ 1));
4731 if (key_checkspidup(saidx
, newspi
) == NULL
)
4735 if (count
== 0 || newspi
== 0) {
4736 ipseclog((LOG_DEBUG
, "key_do_getnewspi: to allocate spi is failed.\n"));
4742 keystat
.getspi_count
=
4743 (keystat
.getspi_count
+ key_spi_trycnt
- count
) / 2;
4749 * SADB_UPDATE processing
4751 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4752 * key(AE), (identity(SD),) (sensitivity)>
4753 * from the ikmpd, and update a secasvar entry whose status is SADB_SASTATE_LARVAL.
4755 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4756 * (identity(SD),) (sensitivity)>
4759 * m will always be freed.
4762 key_update(struct socket
*so
, struct mbuf
*m
,
4763 const struct sadb_msghdr
*mhp
)
4765 struct sadb_sa
*sa0
;
4766 struct sadb_address
*src0
, *dst0
;
4767 struct secasindex saidx
;
4768 struct secashead
*sah
;
4769 struct secasvar
*sav
;
4776 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
4777 panic("key_update: NULL pointer is passed.");
4779 /* map satype to proto */
4780 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
4781 ipseclog((LOG_DEBUG
, "key_update: invalid satype is passed.\n"));
4782 return key_senderror(so
, m
, EINVAL
);
4785 if (mhp
->ext
[SADB_EXT_SA
] == NULL
||
4786 mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
4787 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
||
4788 (mhp
->msg
->sadb_msg_satype
== SADB_SATYPE_ESP
&&
4789 mhp
->ext
[SADB_EXT_KEY_ENCRYPT
] == NULL
) ||
4790 (mhp
->msg
->sadb_msg_satype
== SADB_SATYPE_AH
&&
4791 mhp
->ext
[SADB_EXT_KEY_AUTH
] == NULL
) ||
4792 (mhp
->ext
[SADB_EXT_LIFETIME_HARD
] != NULL
&&
4793 mhp
->ext
[SADB_EXT_LIFETIME_SOFT
] == NULL
) ||
4794 (mhp
->ext
[SADB_EXT_LIFETIME_HARD
] == NULL
&&
4795 mhp
->ext
[SADB_EXT_LIFETIME_SOFT
] != NULL
)) {
4796 ipseclog((LOG_DEBUG
, "key_update: invalid message is passed.\n"));
4797 return key_senderror(so
, m
, EINVAL
);
4799 if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(struct sadb_sa
) ||
4800 mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
4801 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
4802 ipseclog((LOG_DEBUG
, "key_update: invalid message is passed.\n"));
4803 return key_senderror(so
, m
, EINVAL
);
4805 if (mhp
->ext
[SADB_X_EXT_SA2
] != NULL
) {
4806 mode
= ((struct sadb_x_sa2
*)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_mode
;
4807 reqid
= ((struct sadb_x_sa2
*)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_reqid
;
4809 mode
= IPSEC_MODE_ANY
;
4812 /* XXX boundary checking for other extensions */
4814 sa0
= (struct sadb_sa
*)mhp
->ext
[SADB_EXT_SA
];
4815 src0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_SRC
]);
4816 dst0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_DST
]);
4818 /* XXX boundary check against sa_len */
4819 KEY_SETSECASIDX(proto
, mode
, reqid
, src0
+ 1, dst0
+ 1, &saidx
);
4821 /* get a SA header */
4822 if ((sah
= key_getsah(&saidx
)) == NULL
) {
4823 ipseclog((LOG_DEBUG
, "key_update: no SA index found.\n"));
4824 return key_senderror(so
, m
, ENOENT
);
4827 /* set spidx if there */
4829 error
= key_setident(sah
, m
, mhp
);
4831 return key_senderror(so
, m
, error
);
4833 /* find a SA with sequence number. */
4834 #ifdef IPSEC_DOSEQCHECK
4835 if (mhp
->msg
->sadb_msg_seq
!= 0
4836 && (sav
= key_getsavbyseq(sah
, mhp
->msg
->sadb_msg_seq
)) == NULL
) {
4837 ipseclog((LOG_DEBUG
,
4838 "key_update: no larval SA with sequence %u exists.\n",
4839 mhp
->msg
->sadb_msg_seq
));
4840 return key_senderror(so
, m
, ENOENT
);
4843 if ((sav
= key_getsavbyspi(sah
, sa0
->sadb_sa_spi
)) == NULL
) {
4844 ipseclog((LOG_DEBUG
,
4845 "key_update: no such a SA found (spi:%u)\n",
4846 (u_int32_t
)ntohl(sa0
->sadb_sa_spi
)));
4847 return key_senderror(so
, m
, EINVAL
);
4851 /* validity check */
4852 if (sav
->sah
->saidx
.proto
!= proto
) {
4853 ipseclog((LOG_DEBUG
,
4854 "key_update: protocol mismatched (DB=%u param=%u)\n",
4855 sav
->sah
->saidx
.proto
, proto
));
4856 return key_senderror(so
, m
, EINVAL
);
4858 #ifdef IPSEC_DOSEQCHECK
4859 if (sav
->spi
!= sa0
->sadb_sa_spi
) {
4860 ipseclog((LOG_DEBUG
,
4861 "key_update: SPI mismatched (DB:%u param:%u)\n",
4862 (u_int32_t
)ntohl(sav
->spi
),
4863 (u_int32_t
)ntohl(sa0
->sadb_sa_spi
)));
4864 return key_senderror(so
, m
, EINVAL
);
4867 if (sav
->pid
!= mhp
->msg
->sadb_msg_pid
) {
4868 ipseclog((LOG_DEBUG
,
4869 "key_update: pid mismatched (DB:%u param:%u)\n",
4870 sav
->pid
, mhp
->msg
->sadb_msg_pid
));
4871 return key_senderror(so
, m
, EINVAL
);
4874 /* copy sav values */
4875 error
= key_setsaval(sav
, m
, mhp
);
4878 return key_senderror(so
, m
, error
);
4881 /* check SA values to be mature. */
4882 if ((mhp
->msg
->sadb_msg_errno
= key_mature(sav
)) != 0) {
4884 return key_senderror(so
, m
, 0);
4890 /* set msg buf from mhp */
4891 n
= key_getmsgbuf_x1(m
, mhp
);
4893 ipseclog((LOG_DEBUG
, "key_update: No more memory.\n"));
4894 return key_senderror(so
, m
, ENOBUFS
);
4898 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
4903 * search SAD with sequence for a SA which state is SADB_SASTATE_LARVAL.
4904 * only called by key_update().
4907 * others : found, pointer to a SA.
4909 #ifdef IPSEC_DOSEQCHECK
4910 static struct secasvar
*
4911 key_getsavbyseq(struct secashead
*sah
, u_int32_t seq
)
4913 struct secasvar
*sav
;
4916 state
= SADB_SASTATE_LARVAL
;
4918 /* search SAD with sequence number ? */
4919 LIST_FOREACH(sav
, &sah
->savtree
[state
], chain
) {
4921 KEY_CHKSASTATE(state
, sav
->state
, "key_getsabyseq");
4923 if (sav
->seq
== seq
) {
4925 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
4926 kprintf("DP key_getsavbyseq cause "
4927 "refcnt++:%d SA:%p\n",
4938 * SADB_ADD processing
4939 * add a entry to SA database, when received
4940 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4941 * key(AE), (identity(SD),) (sensitivity)>
4944 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4945 * (identity(SD),) (sensitivity)>
4948 * IGNORE identity and sensitivity messages.
4950 * m will always be freed.
4953 key_add(struct socket
*so
, struct mbuf
*m
,
4954 const struct sadb_msghdr
*mhp
)
4956 struct sadb_sa
*sa0
;
4957 struct sadb_address
*src0
, *dst0
;
4958 struct secasindex saidx
;
4959 struct secashead
*newsah
;
4960 struct secasvar
*newsav
;
4967 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
4968 panic("key_add: NULL pointer is passed.");
4970 /* map satype to proto */
4971 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
4972 ipseclog((LOG_DEBUG
, "key_add: invalid satype is passed.\n"));
4973 return key_senderror(so
, m
, EINVAL
);
4976 if (mhp
->ext
[SADB_EXT_SA
] == NULL
||
4977 mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
4978 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
||
4979 (mhp
->msg
->sadb_msg_satype
== SADB_SATYPE_ESP
&&
4980 mhp
->ext
[SADB_EXT_KEY_ENCRYPT
] == NULL
) ||
4981 (mhp
->msg
->sadb_msg_satype
== SADB_SATYPE_AH
&&
4982 mhp
->ext
[SADB_EXT_KEY_AUTH
] == NULL
) ||
4983 (mhp
->ext
[SADB_EXT_LIFETIME_HARD
] != NULL
&&
4984 mhp
->ext
[SADB_EXT_LIFETIME_SOFT
] == NULL
) ||
4985 (mhp
->ext
[SADB_EXT_LIFETIME_HARD
] == NULL
&&
4986 mhp
->ext
[SADB_EXT_LIFETIME_SOFT
] != NULL
)) {
4987 ipseclog((LOG_DEBUG
, "key_add: invalid message is passed.\n"));
4988 return key_senderror(so
, m
, EINVAL
);
4990 if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(struct sadb_sa
) ||
4991 mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
4992 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
4994 ipseclog((LOG_DEBUG
, "key_add: invalid message is passed.\n"));
4995 return key_senderror(so
, m
, EINVAL
);
4997 if (mhp
->ext
[SADB_X_EXT_SA2
] != NULL
) {
4998 mode
= ((struct sadb_x_sa2
*)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_mode
;
4999 reqid
= ((struct sadb_x_sa2
*)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_reqid
;
5001 mode
= IPSEC_MODE_ANY
;
5005 sa0
= (struct sadb_sa
*)mhp
->ext
[SADB_EXT_SA
];
5006 src0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_SRC
];
5007 dst0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_DST
];
5009 /* XXX boundary check against sa_len */
5010 KEY_SETSECASIDX(proto
, mode
, reqid
, src0
+ 1, dst0
+ 1, &saidx
);
5012 /* get a SA header */
5013 if ((newsah
= key_getsah(&saidx
)) == NULL
) {
5014 /* create a new SA header */
5015 if ((newsah
= key_newsah(&saidx
)) == NULL
) {
5016 ipseclog((LOG_DEBUG
, "key_add: No more memory.\n"));
5017 return key_senderror(so
, m
, ENOBUFS
);
5021 /* set spidx if there */
5023 error
= key_setident(newsah
, m
, mhp
);
5025 return key_senderror(so
, m
, error
);
5028 /* create new SA entry. */
5029 /* We can create new SA only if SPI is differenct. */
5030 if (key_getsavbyspi(newsah
, sa0
->sadb_sa_spi
)) {
5031 ipseclog((LOG_DEBUG
, "key_add: SA already exists.\n"));
5032 return key_senderror(so
, m
, EEXIST
);
5034 newsav
= key_newsav(m
, mhp
, newsah
, &error
);
5035 if (newsav
== NULL
) {
5036 return key_senderror(so
, m
, error
);
5039 /* check SA values to be mature. */
5040 if ((error
= key_mature(newsav
)) != 0) {
5041 key_freesav(newsav
);
5042 return key_senderror(so
, m
, error
);
5046 * don't call key_freesav() here, as we would like to keep the SA
5047 * in the database on success.
5053 /* set msg buf from mhp */
5054 n
= key_getmsgbuf_x1(m
, mhp
);
5056 ipseclog((LOG_DEBUG
, "key_update: No more memory.\n"));
5057 return key_senderror(so
, m
, ENOBUFS
);
5061 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
5067 key_setident(struct secashead
*sah
, struct mbuf
*m
,
5068 const struct sadb_msghdr
*mhp
)
5070 const struct sadb_ident
*idsrc
, *iddst
;
5071 int idsrclen
, iddstlen
;
5074 if (sah
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
5075 panic("key_setident: NULL pointer is passed.");
5077 /* don't make buffer if not there */
5078 if (mhp
->ext
[SADB_EXT_IDENTITY_SRC
] == NULL
&&
5079 mhp
->ext
[SADB_EXT_IDENTITY_DST
] == NULL
) {
5085 if (mhp
->ext
[SADB_EXT_IDENTITY_SRC
] == NULL
||
5086 mhp
->ext
[SADB_EXT_IDENTITY_DST
] == NULL
) {
5087 ipseclog((LOG_DEBUG
, "key_setident: invalid identity.\n"));
5091 idsrc
= (const struct sadb_ident
*)mhp
->ext
[SADB_EXT_IDENTITY_SRC
];
5092 iddst
= (const struct sadb_ident
*)mhp
->ext
[SADB_EXT_IDENTITY_DST
];
5093 idsrclen
= mhp
->extlen
[SADB_EXT_IDENTITY_SRC
];
5094 iddstlen
= mhp
->extlen
[SADB_EXT_IDENTITY_DST
];
5096 /* validity check */
5097 if (idsrc
->sadb_ident_type
!= iddst
->sadb_ident_type
) {
5098 ipseclog((LOG_DEBUG
, "key_setident: ident type mismatch.\n"));
5102 switch (idsrc
->sadb_ident_type
) {
5103 case SADB_IDENTTYPE_PREFIX
:
5104 case SADB_IDENTTYPE_FQDN
:
5105 case SADB_IDENTTYPE_USERFQDN
:
5107 /* XXX do nothing */
5113 /* make structure */
5114 KMALLOC(sah
->idents
, struct sadb_ident
*, idsrclen
);
5115 if (sah
->idents
== NULL
) {
5116 ipseclog((LOG_DEBUG
, "key_setident: No more memory.\n"));
5119 KMALLOC(sah
->identd
, struct sadb_ident
*, iddstlen
);
5120 if (sah
->identd
== NULL
) {
5123 ipseclog((LOG_DEBUG
, "key_setident: No more memory.\n"));
5126 bcopy(idsrc
, sah
->idents
, idsrclen
);
5127 bcopy(iddst
, sah
->identd
, iddstlen
);
5133 * m will not be freed on return.
5134 * it is caller's responsibility to free the result.
5136 static struct mbuf
*
5137 key_getmsgbuf_x1(struct mbuf
*m
, const struct sadb_msghdr
*mhp
)
5142 if (m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
5143 panic("key_getmsgbuf_x1: NULL pointer is passed.");
5145 /* create new sadb_msg to reply. */
5146 n
= key_gather_mbuf(m
, mhp
, 1, 9, SADB_EXT_RESERVED
,
5147 SADB_EXT_SA
, SADB_X_EXT_SA2
,
5148 SADB_EXT_ADDRESS_SRC
, SADB_EXT_ADDRESS_DST
,
5149 SADB_EXT_LIFETIME_HARD
, SADB_EXT_LIFETIME_SOFT
,
5150 SADB_EXT_IDENTITY_SRC
, SADB_EXT_IDENTITY_DST
);
5154 if (n
->m_len
< sizeof(struct sadb_msg
)) {
5155 n
= m_pullup(n
, sizeof(struct sadb_msg
));
5159 mtod(n
, struct sadb_msg
*)->sadb_msg_errno
= 0;
5160 mtod(n
, struct sadb_msg
*)->sadb_msg_len
=
5161 PFKEY_UNIT64(n
->m_pkthdr
.len
);
5166 static int key_delete_all (struct socket
*, struct mbuf
*,
5167 const struct sadb_msghdr
*, u_int16_t
);
5170 * SADB_DELETE processing
5172 * <base, SA(*), address(SD)>
5173 * from the ikmpd, and set SADB_SASTATE_DEAD,
5175 * <base, SA(*), address(SD)>
5178 * m will always be freed.
5181 key_delete(struct socket
*so
, struct mbuf
*m
,
5182 const struct sadb_msghdr
*mhp
)
5184 struct sadb_sa
*sa0
;
5185 struct sadb_address
*src0
, *dst0
;
5186 struct secasindex saidx
;
5187 struct secashead
*sah
;
5188 struct secasvar
*sav
= NULL
;
5192 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
5193 panic("key_delete: NULL pointer is passed.");
5195 /* map satype to proto */
5196 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
5197 ipseclog((LOG_DEBUG
, "key_delete: invalid satype is passed.\n"));
5198 return key_senderror(so
, m
, EINVAL
);
5201 if (mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
5202 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
) {
5203 ipseclog((LOG_DEBUG
, "key_delete: invalid message is passed.\n"));
5204 return key_senderror(so
, m
, EINVAL
);
5207 if (mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
5208 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
5209 ipseclog((LOG_DEBUG
, "key_delete: invalid message is passed.\n"));
5210 return key_senderror(so
, m
, EINVAL
);
5213 if (mhp
->ext
[SADB_EXT_SA
] == NULL
) {
5215 * Caller wants us to delete all non-LARVAL SAs
5216 * that match the src/dst. This is used during
5217 * IKE INITIAL-CONTACT.
5219 ipseclog((LOG_DEBUG
, "key_delete: doing delete all.\n"));
5220 return key_delete_all(so
, m
, mhp
, proto
);
5221 } else if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(struct sadb_sa
)) {
5222 ipseclog((LOG_DEBUG
, "key_delete: invalid message is passed.\n"));
5223 return key_senderror(so
, m
, EINVAL
);
5226 sa0
= (struct sadb_sa
*)mhp
->ext
[SADB_EXT_SA
];
5227 src0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_SRC
]);
5228 dst0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_DST
]);
5230 /* XXX boundary check against sa_len */
5231 KEY_SETSECASIDX(proto
, IPSEC_MODE_ANY
, 0, src0
+ 1, dst0
+ 1, &saidx
);
5233 /* get a SA header */
5234 LIST_FOREACH(sah
, &sahtree
, chain
) {
5235 if (sah
->state
== SADB_SASTATE_DEAD
)
5237 if (key_cmpsaidx(&sah
->saidx
, &saidx
, CMP_HEAD
) == 0)
5240 /* get a SA with SPI. */
5241 sav
= key_getsavbyspi(sah
, sa0
->sadb_sa_spi
);
5246 ipseclog((LOG_DEBUG
, "key_delete: no SA found.\n"));
5247 return key_senderror(so
, m
, ENOENT
);
5250 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
5256 struct sadb_msg
*newmsg
;
5258 /* create new sadb_msg to reply. */
5259 n
= key_gather_mbuf(m
, mhp
, 1, 4, SADB_EXT_RESERVED
,
5260 SADB_EXT_SA
, SADB_EXT_ADDRESS_SRC
, SADB_EXT_ADDRESS_DST
);
5262 return key_senderror(so
, m
, ENOBUFS
);
5264 if (n
->m_len
< sizeof(struct sadb_msg
)) {
5265 n
= m_pullup(n
, sizeof(struct sadb_msg
));
5267 return key_senderror(so
, m
, ENOBUFS
);
5269 newmsg
= mtod(n
, struct sadb_msg
*);
5270 newmsg
->sadb_msg_errno
= 0;
5271 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
5274 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
5279 * delete all SAs for src/dst. Called from key_delete().
5282 key_delete_all(struct socket
*so
, struct mbuf
*m
,
5283 const struct sadb_msghdr
*mhp
, u_int16_t proto
)
5285 struct sadb_address
*src0
, *dst0
;
5286 struct secasindex saidx
;
5287 struct secashead
*sah
;
5288 struct secasvar
*sav
, *nextsav
;
5289 u_int stateidx
, state
;
5291 src0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_SRC
]);
5292 dst0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_DST
]);
5294 /* XXX boundary check against sa_len */
5295 KEY_SETSECASIDX(proto
, IPSEC_MODE_ANY
, 0, src0
+ 1, dst0
+ 1, &saidx
);
5297 LIST_FOREACH(sah
, &sahtree
, chain
) {
5298 if (sah
->state
== SADB_SASTATE_DEAD
)
5300 if (key_cmpsaidx(&sah
->saidx
, &saidx
, CMP_HEAD
) == 0)
5303 /* Delete all non-LARVAL SAs. */
5305 stateidx
< NELEM(saorder_state_alive
);
5307 state
= saorder_state_alive
[stateidx
];
5308 if (state
== SADB_SASTATE_LARVAL
)
5310 for (sav
= LIST_FIRST(&sah
->savtree
[state
]);
5311 sav
!= NULL
; sav
= nextsav
) {
5312 nextsav
= LIST_NEXT(sav
, chain
);
5314 if (sav
->state
!= state
) {
5315 ipseclog((LOG_DEBUG
, "key_delete_all: "
5316 "invalid sav->state "
5317 "(queue: %d SA: %d)\n",
5318 state
, sav
->state
));
5322 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
5329 struct sadb_msg
*newmsg
;
5331 /* create new sadb_msg to reply. */
5332 n
= key_gather_mbuf(m
, mhp
, 1, 3, SADB_EXT_RESERVED
,
5333 SADB_EXT_ADDRESS_SRC
, SADB_EXT_ADDRESS_DST
);
5335 return key_senderror(so
, m
, ENOBUFS
);
5337 if (n
->m_len
< sizeof(struct sadb_msg
)) {
5338 n
= m_pullup(n
, sizeof(struct sadb_msg
));
5340 return key_senderror(so
, m
, ENOBUFS
);
5342 newmsg
= mtod(n
, struct sadb_msg
*);
5343 newmsg
->sadb_msg_errno
= 0;
5344 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
5347 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
5352 * SADB_GET processing
5354 * <base, SA(*), address(SD)>
5355 * from the ikmpd, and get a SP and a SA to respond,
5357 * <base, SA, (lifetime(HSC),) address(SD), (address(P),) key(AE),
5358 * (identity(SD),) (sensitivity)>
5361 * m will always be freed.
5364 key_get(struct socket
*so
, struct mbuf
*m
, const struct sadb_msghdr
*mhp
)
5366 struct sadb_sa
*sa0
;
5367 struct sadb_address
*src0
, *dst0
;
5368 struct secasindex saidx
;
5369 struct secashead
*sah
;
5370 struct secasvar
*sav
= NULL
;
5374 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
5375 panic("key_get: NULL pointer is passed.");
5377 /* map satype to proto */
5378 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
5379 ipseclog((LOG_DEBUG
, "key_get: invalid satype is passed.\n"));
5380 return key_senderror(so
, m
, EINVAL
);
5383 if (mhp
->ext
[SADB_EXT_SA
] == NULL
||
5384 mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
5385 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
) {
5386 ipseclog((LOG_DEBUG
, "key_get: invalid message is passed.\n"));
5387 return key_senderror(so
, m
, EINVAL
);
5389 if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(struct sadb_sa
) ||
5390 mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
5391 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
5392 ipseclog((LOG_DEBUG
, "key_get: invalid message is passed.\n"));
5393 return key_senderror(so
, m
, EINVAL
);
5396 sa0
= (struct sadb_sa
*)mhp
->ext
[SADB_EXT_SA
];
5397 src0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_SRC
];
5398 dst0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_DST
];
5400 /* XXX boundary check against sa_len */
5401 KEY_SETSECASIDX(proto
, IPSEC_MODE_ANY
, 0, src0
+ 1, dst0
+ 1, &saidx
);
5403 /* get a SA header */
5404 LIST_FOREACH(sah
, &sahtree
, chain
) {
5405 if (sah
->state
== SADB_SASTATE_DEAD
)
5407 if (key_cmpsaidx(&sah
->saidx
, &saidx
, CMP_HEAD
) == 0)
5410 /* get a SA with SPI. */
5411 sav
= key_getsavbyspi(sah
, sa0
->sadb_sa_spi
);
5416 ipseclog((LOG_DEBUG
, "key_get: no SA found.\n"));
5417 return key_senderror(so
, m
, ENOENT
);
5424 /* map proto to satype */
5425 if ((satype
= key_proto2satype(sah
->saidx
.proto
)) == 0) {
5426 ipseclog((LOG_DEBUG
, "key_get: there was invalid proto in SAD.\n"));
5427 return key_senderror(so
, m
, EINVAL
);
5430 /* create new sadb_msg to reply. */
5431 n
= key_setdumpsa(sav
, SADB_GET
, satype
, mhp
->msg
->sadb_msg_seq
,
5432 mhp
->msg
->sadb_msg_pid
);
5434 return key_senderror(so
, m
, ENOBUFS
);
5437 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ONE
);
5441 /* XXX make it sysctl-configurable? */
5443 key_getcomb_setlifetime(struct sadb_comb
*comb
)
5446 comb
->sadb_comb_soft_allocations
= 1;
5447 comb
->sadb_comb_hard_allocations
= 1;
5448 comb
->sadb_comb_soft_bytes
= 0;
5449 comb
->sadb_comb_hard_bytes
= 0;
5450 comb
->sadb_comb_hard_addtime
= 86400; /* 1 day */
5451 comb
->sadb_comb_soft_addtime
= comb
->sadb_comb_soft_addtime
* 80 / 100;
5452 comb
->sadb_comb_soft_usetime
= 28800; /* 8 hours */
5453 comb
->sadb_comb_hard_usetime
= comb
->sadb_comb_hard_usetime
* 80 / 100;
5458 * XXX reorder combinations by preference
5459 * XXX no idea if the user wants ESP authentication or not
5461 static struct mbuf
*
5462 key_getcomb_esp(void)
5464 struct sadb_comb
*comb
;
5465 const struct esp_algorithm
*algo
;
5466 struct mbuf
*result
= NULL
, *m
, *n
;
5470 const int l
= PFKEY_ALIGN8(sizeof(struct sadb_comb
));
5473 for (i
= 1; i
<= SADB_EALG_MAX
; i
++) {
5474 algo
= esp_algorithm_lookup(i
);
5478 if (algo
->keymax
< ipsec_esp_keymin
)
5480 if (algo
->keymin
< ipsec_esp_keymin
)
5481 encmin
= ipsec_esp_keymin
;
5483 encmin
= algo
->keymin
;
5486 m
= key_getcomb_ah();
5490 panic("assumption failed in key_getcomb_esp");
5492 MGET(m
, M_NOWAIT
, MT_DATA
);
5497 bzero(mtod(m
, caddr_t
), m
->m_len
);
5504 for (n
= m
; n
; n
= n
->m_next
)
5508 panic("assumption failed in key_getcomb_esp");
5511 for (off
= 0; off
< totlen
; off
+= l
) {
5512 n
= m_pulldown(m
, off
, l
, &o
);
5514 /* m is already freed */
5517 comb
= (struct sadb_comb
*)(mtod(n
, caddr_t
) + o
);
5518 bzero(comb
, sizeof(*comb
));
5519 key_getcomb_setlifetime(comb
);
5520 comb
->sadb_comb_encrypt
= i
;
5521 comb
->sadb_comb_encrypt_minbits
= encmin
;
5522 comb
->sadb_comb_encrypt_maxbits
= algo
->keymax
;
5541 * XXX reorder combinations by preference
5543 static struct mbuf
*
5544 key_getcomb_ah(void)
5546 struct sadb_comb
*comb
;
5547 const struct ah_algorithm
*algo
;
5551 const int l
= PFKEY_ALIGN8(sizeof(struct sadb_comb
));
5554 for (i
= 1; i
<= SADB_AALG_MAX
; i
++) {
5556 /* we prefer HMAC algorithms, not old algorithms */
5557 if (i
!= SADB_AALG_SHA1HMAC
&& i
!= SADB_AALG_MD5HMAC
)
5560 algo
= ah_algorithm_lookup(i
);
5564 if (algo
->keymax
< ipsec_ah_keymin
)
5566 if (algo
->keymin
< ipsec_ah_keymin
)
5567 min
= ipsec_ah_keymin
;
5574 panic("assumption failed in key_getcomb_ah");
5576 MGET(m
, M_NOWAIT
, MT_DATA
);
5583 M_PREPEND(m
, l
, M_NOWAIT
);
5587 comb
= mtod(m
, struct sadb_comb
*);
5588 bzero(comb
, sizeof(*comb
));
5589 key_getcomb_setlifetime(comb
);
5590 comb
->sadb_comb_auth
= i
;
5591 comb
->sadb_comb_auth_minbits
= min
;
5592 comb
->sadb_comb_auth_maxbits
= algo
->keymax
;
5599 * not really an official behavior. discussed in pf_key@inner.net in Sep2000.
5600 * XXX reorder combinations by preference
5602 static struct mbuf
*
5603 key_getcomb_ipcomp(void)
5605 struct sadb_comb
*comb
;
5606 const struct ipcomp_algorithm
*algo
;
5609 const int l
= PFKEY_ALIGN8(sizeof(struct sadb_comb
));
5612 for (i
= 1; i
<= SADB_X_CALG_MAX
; i
++) {
5613 algo
= ipcomp_algorithm_lookup(i
);
5620 panic("assumption failed in key_getcomb_ipcomp");
5622 MGET(m
, M_NOWAIT
, MT_DATA
);
5629 M_PREPEND(m
, l
, M_NOWAIT
);
5633 comb
= mtod(m
, struct sadb_comb
*);
5634 bzero(comb
, sizeof(*comb
));
5635 key_getcomb_setlifetime(comb
);
5636 comb
->sadb_comb_encrypt
= i
;
5637 /* what should we set into sadb_comb_*_{min,max}bits? */
5644 * XXX no way to pass mode (transport/tunnel) to userland
5645 * XXX replay checking?
5646 * XXX sysctl interface to ipsec_{ah,esp}_keymin
5648 static struct mbuf
*
5649 key_getprop(const struct secasindex
*saidx
)
5651 struct sadb_prop
*prop
;
5653 const int l
= PFKEY_ALIGN8(sizeof(struct sadb_prop
));
5656 switch (saidx
->proto
) {
5659 m
= key_getcomb_esp();
5663 m
= key_getcomb_ah();
5665 case IPPROTO_IPCOMP
:
5666 m
= key_getcomb_ipcomp();
5674 M_PREPEND(m
, l
, M_NOWAIT
);
5679 for (n
= m
; n
; n
= n
->m_next
)
5682 prop
= mtod(m
, struct sadb_prop
*);
5683 bzero(prop
, sizeof(*prop
));
5684 prop
->sadb_prop_len
= PFKEY_UNIT64(totlen
);
5685 prop
->sadb_prop_exttype
= SADB_EXT_PROPOSAL
;
5686 prop
->sadb_prop_replay
= 32; /* XXX */
5692 * SADB_ACQUIRE processing called by key_checkrequest() and key_acquire2().
5694 * <base, SA, address(SD), (address(P)), x_policy,
5695 * (identity(SD),) (sensitivity,) proposal>
5696 * to KMD, and expect to receive
5697 * <base> with SADB_ACQUIRE if error occured,
5699 * <base, src address, dst address, (SPI range)> with SADB_GETSPI
5700 * from KMD by PF_KEY.
5702 * XXX x_policy is outside of RFC2367 (KAME extension).
5703 * XXX sensitivity is not supported.
5704 * XXX for ipcomp, RFC2367 does not define how to fill in proposal.
5705 * see comment for key_getcomb_ipcomp().
5709 * others: error number
5712 key_acquire(struct secasindex
*saidx
, struct secpolicy
*sp
)
5714 struct mbuf
*result
= NULL
, *m
;
5715 #ifndef IPSEC_NONBLOCK_ACQUIRE
5716 struct secacq
*newacq
;
5724 panic("key_acquire: NULL pointer is passed.");
5725 if ((satype
= key_proto2satype(saidx
->proto
)) == 0)
5726 panic("key_acquire: invalid proto is passed.");
5728 #ifndef IPSEC_NONBLOCK_ACQUIRE
5730 * We never do anything about acquirng SA. There is anather
5731 * solution that kernel blocks to send SADB_ACQUIRE message until
5732 * getting something message from IKEd. In later case, to be
5733 * managed with ACQUIRING list.
5735 /* get a entry to check whether sending message or not. */
5736 if ((newacq
= key_getacq(saidx
)) != NULL
) {
5737 if (key_blockacq_count
< newacq
->count
) {
5738 /* reset counter and do send message. */
5741 /* increment counter and do nothing. */
5746 /* make new entry for blocking to send SADB_ACQUIRE. */
5747 if ((newacq
= key_newacq(saidx
)) == NULL
)
5750 /* add to acqtree */
5751 LIST_INSERT_HEAD(&acqtree
, newacq
, chain
);
5756 #ifndef IPSEC_NONBLOCK_ACQUIRE
5759 seq
= (acq_seq
= (acq_seq
== ~0 ? 1 : ++acq_seq
));
5761 m
= key_setsadbmsg(SADB_ACQUIRE
, 0, satype
, seq
, 0, 0);
5768 /* set sadb_address for saidx's. */
5769 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
5770 (struct sockaddr
*)&saidx
->src
, FULLMASK
, IPSEC_ULPROTO_ANY
);
5777 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
5778 (struct sockaddr
*)&saidx
->dst
, FULLMASK
, IPSEC_ULPROTO_ANY
);
5785 /* XXX proxy address (optional) */
5787 /* set sadb_x_policy */
5789 m
= key_setsadbxpolicy(sp
->policy
, sp
->spidx
.dir
, sp
->id
);
5797 /* XXX identity (optional) */
5799 if (idexttype
&& fqdn
) {
5800 /* create identity extension (FQDN) */
5801 struct sadb_ident
*id
;
5804 fqdnlen
= strlen(fqdn
) + 1; /* +1 for terminating-NUL */
5805 id
= (struct sadb_ident
*)p
;
5806 bzero(id
, sizeof(*id
) + PFKEY_ALIGN8(fqdnlen
));
5807 id
->sadb_ident_len
= PFKEY_UNIT64(sizeof(*id
) + PFKEY_ALIGN8(fqdnlen
));
5808 id
->sadb_ident_exttype
= idexttype
;
5809 id
->sadb_ident_type
= SADB_IDENTTYPE_FQDN
;
5810 bcopy(fqdn
, id
+ 1, fqdnlen
);
5811 p
+= sizeof(struct sadb_ident
) + PFKEY_ALIGN8(fqdnlen
);
5815 /* create identity extension (USERFQDN) */
5816 struct sadb_ident
*id
;
5820 /* +1 for terminating-NUL */
5821 userfqdnlen
= strlen(userfqdn
) + 1;
5824 id
= (struct sadb_ident
*)p
;
5825 bzero(id
, sizeof(*id
) + PFKEY_ALIGN8(userfqdnlen
));
5826 id
->sadb_ident_len
= PFKEY_UNIT64(sizeof(*id
) + PFKEY_ALIGN8(userfqdnlen
));
5827 id
->sadb_ident_exttype
= idexttype
;
5828 id
->sadb_ident_type
= SADB_IDENTTYPE_USERFQDN
;
5829 /* XXX is it correct? */
5830 if (curproc
&& curproc
->p_cred
)
5831 id
->sadb_ident_id
= curproc
->p_cred
->p_ruid
;
5832 if (userfqdn
&& userfqdnlen
)
5833 bcopy(userfqdn
, id
+ 1, userfqdnlen
);
5834 p
+= sizeof(struct sadb_ident
) + PFKEY_ALIGN8(userfqdnlen
);
5838 /* XXX sensitivity (optional) */
5840 /* create proposal/combination extension */
5841 m
= key_getprop(saidx
);
5844 * spec conformant: always attach proposal/combination extension,
5845 * the problem is that we have no way to attach it for ipcomp,
5846 * due to the way sadb_comb is declared in RFC2367.
5855 * outside of spec; make proposal/combination extension optional.
5861 if ((result
->m_flags
& M_PKTHDR
) == 0) {
5866 if (result
->m_len
< sizeof(struct sadb_msg
)) {
5867 result
= m_pullup(result
, sizeof(struct sadb_msg
));
5868 if (result
== NULL
) {
5874 result
->m_pkthdr
.len
= 0;
5875 for (m
= result
; m
; m
= m
->m_next
)
5876 result
->m_pkthdr
.len
+= m
->m_len
;
5878 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
5879 PFKEY_UNIT64(result
->m_pkthdr
.len
);
5881 return key_sendup_mbuf(NULL
, result
, KEY_SENDUP_REGISTERED
);
5889 #ifndef IPSEC_NONBLOCK_ACQUIRE
5890 static struct secacq
*
5891 key_newacq(struct secasindex
*saidx
)
5893 struct secacq
*newacq
;
5897 KMALLOC(newacq
, struct secacq
*, sizeof(struct secacq
));
5898 if (newacq
== NULL
) {
5899 ipseclog((LOG_DEBUG
, "key_newacq: No more memory.\n"));
5902 bzero(newacq
, sizeof(*newacq
));
5905 bcopy(saidx
, &newacq
->saidx
, sizeof(newacq
->saidx
));
5906 newacq
->seq
= (acq_seq
== ~0 ? 1 : ++acq_seq
);
5908 newacq
->created
= tv
.tv_sec
;
5914 static struct secacq
*
5915 key_getacq(struct secasindex
*saidx
)
5919 LIST_FOREACH(acq
, &acqtree
, chain
) {
5920 if (key_cmpsaidx(saidx
, &acq
->saidx
, CMP_EXACTLY
))
5927 static struct secacq
*
5928 key_getacqbyseq(u_int32_t seq
)
5932 LIST_FOREACH(acq
, &acqtree
, chain
) {
5933 if (acq
->seq
== seq
)
5941 static struct secspacq
*
5942 key_newspacq(struct secpolicyindex
*spidx
)
5944 struct secspacq
*acq
;
5948 KMALLOC(acq
, struct secspacq
*, sizeof(struct secspacq
));
5950 ipseclog((LOG_DEBUG
, "key_newspacq: No more memory.\n"));
5953 bzero(acq
, sizeof(*acq
));
5956 bcopy(spidx
, &acq
->spidx
, sizeof(acq
->spidx
));
5958 acq
->created
= tv
.tv_sec
;
5964 static struct secspacq
*
5965 key_getspacq(struct secpolicyindex
*spidx
)
5967 struct secspacq
*acq
;
5969 LIST_FOREACH(acq
, &spacqtree
, chain
) {
5970 if (key_cmpspidx_exactly(spidx
, &acq
->spidx
))
5978 * SADB_ACQUIRE processing,
5979 * in first situation, is receiving
5981 * from the ikmpd, and clear sequence of its secasvar entry.
5983 * In second situation, is receiving
5984 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
5985 * from a user land process, and return
5986 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
5989 * m will always be freed.
5992 key_acquire2(struct socket
*so
, struct mbuf
*m
,
5993 const struct sadb_msghdr
*mhp
)
5995 struct sadb_address
*src0
, *dst0
;
5996 struct secasindex saidx
;
5997 struct secashead
*sah
;
6002 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
6003 panic("key_acquire2: NULL pointer is passed.");
6006 * Error message from KMd.
6007 * We assume that if error was occured in IKEd, the length of PFKEY
6008 * message is equal to the size of sadb_msg structure.
6009 * We do not raise error even if error occured in this function.
6011 if (mhp
->msg
->sadb_msg_len
== PFKEY_UNIT64(sizeof(struct sadb_msg
))) {
6012 #ifndef IPSEC_NONBLOCK_ACQUIRE
6016 /* check sequence number */
6017 if (mhp
->msg
->sadb_msg_seq
== 0) {
6018 ipseclog((LOG_DEBUG
, "key_acquire2: must specify sequence number.\n"));
6023 if ((acq
= key_getacqbyseq(mhp
->msg
->sadb_msg_seq
)) == NULL
) {
6025 * the specified larval SA is already gone, or we got
6026 * a bogus sequence number. we can silently ignore it.
6032 /* reset acq counter in order to deletion by timehander. */
6034 acq
->created
= tv
.tv_sec
;
6042 * This message is from user land.
6045 /* map satype to proto */
6046 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
6047 ipseclog((LOG_DEBUG
, "key_acquire2: invalid satype is passed.\n"));
6048 return key_senderror(so
, m
, EINVAL
);
6051 if (mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
6052 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
||
6053 mhp
->ext
[SADB_EXT_PROPOSAL
] == NULL
) {
6055 ipseclog((LOG_DEBUG
, "key_acquire2: invalid message is passed.\n"));
6056 return key_senderror(so
, m
, EINVAL
);
6058 if (mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
6059 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
) ||
6060 mhp
->extlen
[SADB_EXT_PROPOSAL
] < sizeof(struct sadb_prop
)) {
6062 ipseclog((LOG_DEBUG
, "key_acquire2: invalid message is passed.\n"));
6063 return key_senderror(so
, m
, EINVAL
);
6066 src0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_SRC
];
6067 dst0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_DST
];
6069 /* XXX boundary check against sa_len */
6070 KEY_SETSECASIDX(proto
, IPSEC_MODE_ANY
, 0, src0
+ 1, dst0
+ 1, &saidx
);
6072 /* get a SA index */
6073 LIST_FOREACH(sah
, &sahtree
, chain
) {
6074 if (sah
->state
== SADB_SASTATE_DEAD
)
6076 if (key_cmpsaidx(&sah
->saidx
, &saidx
, CMP_MODE_REQID
))
6080 ipseclog((LOG_DEBUG
, "key_acquire2: a SA exists already.\n"));
6081 return key_senderror(so
, m
, EEXIST
);
6084 error
= key_acquire(&saidx
, NULL
);
6086 ipseclog((LOG_DEBUG
, "key_acquire2: error %d returned "
6087 "from key_acquire.\n", mhp
->msg
->sadb_msg_errno
));
6088 return key_senderror(so
, m
, error
);
6091 return key_sendup_mbuf(so
, m
, KEY_SENDUP_REGISTERED
);
6095 * SADB_REGISTER processing.
6096 * If SATYPE_UNSPEC has been passed as satype, only return sabd_supported.
6099 * from the ikmpd, and register a socket to send PF_KEY messages,
6103 * If socket is detached, must free from regnode.
6105 * m will always be freed.
6108 key_register(struct socket
*so
, struct mbuf
*m
,
6109 const struct sadb_msghdr
*mhp
)
6111 struct secreg
*reg
, *newreg
= NULL
;
6114 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
6115 panic("key_register: NULL pointer is passed.");
6117 /* check for invalid register message */
6118 if (mhp
->msg
->sadb_msg_satype
>= NELEM(regtree
))
6119 return key_senderror(so
, m
, EINVAL
);
6121 /* When SATYPE_UNSPEC is specified, only return sabd_supported. */
6122 if (mhp
->msg
->sadb_msg_satype
== SADB_SATYPE_UNSPEC
)
6125 /* check whether existing or not */
6126 LIST_FOREACH(reg
, ®tree
[mhp
->msg
->sadb_msg_satype
], chain
) {
6127 if (reg
->so
== so
) {
6128 ipseclog((LOG_DEBUG
, "key_register: socket exists already.\n"));
6129 return key_senderror(so
, m
, EEXIST
);
6133 /* create regnode */
6134 KMALLOC(newreg
, struct secreg
*, sizeof(*newreg
));
6135 if (newreg
== NULL
) {
6136 ipseclog((LOG_DEBUG
, "key_register: No more memory.\n"));
6137 return key_senderror(so
, m
, ENOBUFS
);
6139 bzero((caddr_t
)newreg
, sizeof(*newreg
));
6142 ((struct keycb
*)sotorawcb(so
))->kp_registered
++;
6144 /* add regnode to regtree. */
6145 LIST_INSERT_HEAD(®tree
[mhp
->msg
->sadb_msg_satype
], newreg
, chain
);
6150 struct sadb_msg
*newmsg
;
6151 struct sadb_supported
*sup
;
6152 u_int len
, alen
, elen
;
6155 struct sadb_alg
*alg
;
6157 /* create new sadb_msg to reply. */
6159 for (i
= 1; i
<= SADB_AALG_MAX
; i
++) {
6160 if (ah_algorithm_lookup(i
))
6161 alen
+= sizeof(struct sadb_alg
);
6164 alen
+= sizeof(struct sadb_supported
);
6167 for (i
= 1; i
<= SADB_EALG_MAX
; i
++) {
6168 if (esp_algorithm_lookup(i
))
6169 elen
+= sizeof(struct sadb_alg
);
6172 elen
+= sizeof(struct sadb_supported
);
6175 len
= sizeof(struct sadb_msg
) + alen
+ elen
;
6178 return key_senderror(so
, m
, ENOBUFS
);
6180 MGETHDR(n
, M_NOWAIT
, MT_DATA
);
6182 MCLGET(n
, M_NOWAIT
);
6183 if ((n
->m_flags
& M_EXT
) == 0) {
6189 return key_senderror(so
, m
, ENOBUFS
);
6191 n
->m_pkthdr
.len
= n
->m_len
= len
;
6195 m_copydata(m
, 0, sizeof(struct sadb_msg
), mtod(n
, caddr_t
) + off
);
6196 newmsg
= mtod(n
, struct sadb_msg
*);
6197 newmsg
->sadb_msg_errno
= 0;
6198 newmsg
->sadb_msg_len
= PFKEY_UNIT64(len
);
6199 off
+= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
6201 /* for authentication algorithm */
6203 sup
= (struct sadb_supported
*)(mtod(n
, caddr_t
) + off
);
6204 sup
->sadb_supported_len
= PFKEY_UNIT64(alen
);
6205 sup
->sadb_supported_exttype
= SADB_EXT_SUPPORTED_AUTH
;
6206 off
+= PFKEY_ALIGN8(sizeof(*sup
));
6208 for (i
= 1; i
<= SADB_AALG_MAX
; i
++) {
6209 const struct ah_algorithm
*aalgo
;
6211 aalgo
= ah_algorithm_lookup(i
);
6214 alg
= (struct sadb_alg
*)(mtod(n
, caddr_t
) + off
);
6215 alg
->sadb_alg_id
= i
;
6216 alg
->sadb_alg_ivlen
= 0;
6217 alg
->sadb_alg_minbits
= aalgo
->keymin
;
6218 alg
->sadb_alg_maxbits
= aalgo
->keymax
;
6219 off
+= PFKEY_ALIGN8(sizeof(*alg
));
6224 /* for encryption algorithm */
6226 sup
= (struct sadb_supported
*)(mtod(n
, caddr_t
) + off
);
6227 sup
->sadb_supported_len
= PFKEY_UNIT64(elen
);
6228 sup
->sadb_supported_exttype
= SADB_EXT_SUPPORTED_ENCRYPT
;
6229 off
+= PFKEY_ALIGN8(sizeof(*sup
));
6231 for (i
= 1; i
<= SADB_EALG_MAX
; i
++) {
6232 const struct esp_algorithm
*ealgo
;
6234 ealgo
= esp_algorithm_lookup(i
);
6237 alg
= (struct sadb_alg
*)(mtod(n
, caddr_t
) + off
);
6238 alg
->sadb_alg_id
= i
;
6239 if (ealgo
&& ealgo
->ivlen
) {
6241 * give NULL to get the value preferred by
6242 * algorithm XXX SADB_X_EXT_DERIV ?
6244 alg
->sadb_alg_ivlen
=
6245 (*ealgo
->ivlen
)(ealgo
, NULL
);
6247 alg
->sadb_alg_ivlen
= 0;
6248 alg
->sadb_alg_minbits
= ealgo
->keymin
;
6249 alg
->sadb_alg_maxbits
= ealgo
->keymax
;
6250 off
+= PFKEY_ALIGN8(sizeof(struct sadb_alg
));
6257 panic("length assumption failed in key_register");
6261 return key_sendup_mbuf(so
, n
, KEY_SENDUP_REGISTERED
);
6266 * free secreg entry registered.
6267 * XXX: I want to do free a socket marked done SADB_RESIGER to socket.
6270 key_freereg(struct socket
*so
)
6277 panic("key_freereg: NULL pointer is passed.");
6280 * check whether existing or not.
6281 * check all type of SA, because there is a potential that
6282 * one socket is registered to multiple type of SA.
6284 lwkt_gettoken(&key_token
);
6285 for (i
= 0; i
<= SADB_SATYPE_MAX
; i
++) {
6286 LIST_FOREACH(reg
, ®tree
[i
], chain
) {
6288 && __LIST_CHAINED(reg
)) {
6289 LIST_REMOVE(reg
, chain
);
6295 lwkt_reltoken(&key_token
);
6299 * SADB_EXPIRE processing
6301 * <base, SA, SA2, lifetime(C and one of HS), address(SD)>
6303 * NOTE: We send only soft lifetime extension.
6306 * others : error number
6309 key_expire(struct secasvar
*sav
)
6312 struct mbuf
*result
= NULL
, *m
;
6315 struct sadb_lifetime
*lt
;
6319 panic("key_expire: NULL pointer is passed.");
6320 if (sav
->sah
== NULL
)
6321 panic("key_expire: Why was SA index in SA NULL.");
6322 if ((satype
= key_proto2satype(sav
->sah
->saidx
.proto
)) == 0)
6323 panic("key_expire: invalid proto is passed.");
6325 /* set msg header */
6326 m
= key_setsadbmsg(SADB_EXPIRE
, 0, satype
, sav
->seq
, 0, sav
->refcnt
);
6333 /* create SA extension */
6334 m
= key_setsadbsa(sav
);
6341 /* create SA extension */
6342 m
= key_setsadbxsa2(sav
->sah
->saidx
.mode
,
6343 sav
->replay
? sav
->replay
->count
: 0,
6344 sav
->sah
->saidx
.reqid
);
6351 /* create lifetime extension (current and soft) */
6352 len
= PFKEY_ALIGN8(sizeof(*lt
)) * 2;
6353 m
= key_alloc_mbuf(len
);
6354 if (!m
|| m
->m_next
) { /*XXX*/
6360 bzero(mtod(m
, caddr_t
), len
);
6361 lt
= mtod(m
, struct sadb_lifetime
*);
6362 lt
->sadb_lifetime_len
= PFKEY_UNIT64(sizeof(struct sadb_lifetime
));
6363 lt
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
6364 lt
->sadb_lifetime_allocations
= sav
->lft_c
->sadb_lifetime_allocations
;
6365 lt
->sadb_lifetime_bytes
= sav
->lft_c
->sadb_lifetime_bytes
;
6366 lt
->sadb_lifetime_addtime
= sav
->lft_c
->sadb_lifetime_addtime
;
6367 lt
->sadb_lifetime_usetime
= sav
->lft_c
->sadb_lifetime_usetime
;
6368 lt
= (struct sadb_lifetime
*)(mtod(m
, caddr_t
) + len
/ 2);
6369 bcopy(sav
->lft_s
, lt
, sizeof(*lt
));
6372 /* set sadb_address for source */
6373 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
6374 (struct sockaddr
*)&sav
->sah
->saidx
.src
,
6375 FULLMASK
, IPSEC_ULPROTO_ANY
);
6382 /* set sadb_address for destination */
6383 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
6384 (struct sockaddr
*)&sav
->sah
->saidx
.dst
,
6385 FULLMASK
, IPSEC_ULPROTO_ANY
);
6392 if ((result
->m_flags
& M_PKTHDR
) == 0) {
6397 if (result
->m_len
< sizeof(struct sadb_msg
)) {
6398 result
= m_pullup(result
, sizeof(struct sadb_msg
));
6399 if (result
== NULL
) {
6405 result
->m_pkthdr
.len
= 0;
6406 for (m
= result
; m
; m
= m
->m_next
)
6407 result
->m_pkthdr
.len
+= m
->m_len
;
6409 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
6410 PFKEY_UNIT64(result
->m_pkthdr
.len
);
6412 return key_sendup_mbuf(NULL
, result
, KEY_SENDUP_REGISTERED
);
6421 * SADB_FLUSH processing
6424 * from the ikmpd, and free all entries in secastree.
6428 * NOTE: to do is only marking SADB_SASTATE_DEAD.
6430 * m will always be freed.
6433 key_flush(struct socket
*so
, struct mbuf
*m
,
6434 const struct sadb_msghdr
*mhp
)
6436 struct sadb_msg
*newmsg
;
6437 struct secashead
*sah
, *nextsah
;
6438 struct secasvar
*sav
, *nextsav
;
6444 if (so
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
6445 panic("key_flush: NULL pointer is passed.");
6447 /* map satype to proto */
6448 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
6449 ipseclog((LOG_DEBUG
, "key_flush: invalid satype is passed.\n"));
6450 return key_senderror(so
, m
, EINVAL
);
6453 /* no SATYPE specified, i.e. flushing all SA. */
6454 for (sah
= LIST_FIRST(&sahtree
);
6457 nextsah
= LIST_NEXT(sah
, chain
);
6459 if (mhp
->msg
->sadb_msg_satype
!= SADB_SATYPE_UNSPEC
6460 && proto
!= sah
->saidx
.proto
)
6464 stateidx
< NELEM(saorder_state_alive
);
6466 state
= saorder_state_any
[stateidx
];
6467 for (sav
= LIST_FIRST(&sah
->savtree
[state
]);
6471 nextsav
= LIST_NEXT(sav
, chain
);
6473 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
6478 sah
->state
= SADB_SASTATE_DEAD
;
6481 if (m
->m_len
< sizeof(struct sadb_msg
) ||
6482 sizeof(struct sadb_msg
) > m
->m_len
+ M_TRAILINGSPACE(m
)) {
6483 ipseclog((LOG_DEBUG
, "key_flush: No more memory.\n"));
6484 return key_senderror(so
, m
, ENOBUFS
);
6490 m
->m_pkthdr
.len
= m
->m_len
= sizeof(struct sadb_msg
);
6491 newmsg
= mtod(m
, struct sadb_msg
*);
6492 newmsg
->sadb_msg_errno
= 0;
6493 newmsg
->sadb_msg_len
= PFKEY_UNIT64(m
->m_pkthdr
.len
);
6495 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ALL
);
6499 * SADB_DUMP processing
6500 * dump all entries including status of DEAD in SAD.
6503 * from the ikmpd, and dump all secasvar leaves
6508 * m will always be freed.
6511 key_dump(struct socket
*so
, struct mbuf
*m
,
6512 const struct sadb_msghdr
*mhp
)
6514 struct secashead
*sah
;
6515 struct secasvar
*sav
;
6524 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
6525 panic("key_dump: NULL pointer is passed.");
6527 /* map satype to proto */
6528 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
6529 ipseclog((LOG_DEBUG
, "key_dump: invalid satype is passed.\n"));
6530 return key_senderror(so
, m
, EINVAL
);
6533 /* count sav entries to be sent to the userland. */
6535 LIST_FOREACH(sah
, &sahtree
, chain
) {
6536 if (mhp
->msg
->sadb_msg_satype
!= SADB_SATYPE_UNSPEC
6537 && proto
!= sah
->saidx
.proto
)
6541 stateidx
< NELEM(saorder_state_any
);
6543 state
= saorder_state_any
[stateidx
];
6544 LIST_FOREACH(sav
, &sah
->savtree
[state
], chain
) {
6551 return key_senderror(so
, m
, ENOENT
);
6553 /* send this to the userland, one at a time. */
6554 LIST_FOREACH(sah
, &sahtree
, chain
) {
6555 if (mhp
->msg
->sadb_msg_satype
!= SADB_SATYPE_UNSPEC
6556 && proto
!= sah
->saidx
.proto
)
6559 /* map proto to satype */
6560 if ((satype
= key_proto2satype(sah
->saidx
.proto
)) == 0) {
6561 ipseclog((LOG_DEBUG
, "key_dump: there was invalid proto in SAD.\n"));
6562 return key_senderror(so
, m
, EINVAL
);
6566 stateidx
< NELEM(saorder_state_any
);
6568 state
= saorder_state_any
[stateidx
];
6569 LIST_FOREACH(sav
, &sah
->savtree
[state
], chain
) {
6570 n
= key_setdumpsa(sav
, SADB_DUMP
, satype
,
6571 --cnt
, mhp
->msg
->sadb_msg_pid
);
6573 return key_senderror(so
, m
, ENOBUFS
);
6575 key_sendup_mbuf(so
, n
, KEY_SENDUP_ONE
);
6585 * SADB_X_PROMISC processing
6587 * m will always be freed.
6590 key_promisc(struct socket
*so
, struct mbuf
*m
,
6591 const struct sadb_msghdr
*mhp
)
6596 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
6597 panic("key_promisc: NULL pointer is passed.");
6599 olen
= PFKEY_UNUNIT64(mhp
->msg
->sadb_msg_len
);
6601 if (olen
< sizeof(struct sadb_msg
)) {
6603 return key_senderror(so
, m
, EINVAL
);
6608 } else if (olen
== sizeof(struct sadb_msg
)) {
6609 /* enable/disable promisc mode */
6612 if ((kp
= (struct keycb
*)sotorawcb(so
)) == NULL
)
6613 return key_senderror(so
, m
, EINVAL
);
6614 mhp
->msg
->sadb_msg_errno
= 0;
6615 switch (mhp
->msg
->sadb_msg_satype
) {
6618 kp
->kp_promisc
= mhp
->msg
->sadb_msg_satype
;
6621 return key_senderror(so
, m
, EINVAL
);
6624 /* send the original message back to everyone */
6625 mhp
->msg
->sadb_msg_errno
= 0;
6626 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ALL
);
6628 /* send packet as is */
6630 m_adj(m
, PFKEY_ALIGN8(sizeof(struct sadb_msg
)));
6632 /* TODO: if sadb_msg_seq is specified, send to specific pid */
6633 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ALL
);
6637 static int (*key_typesw
[]) (struct socket
*, struct mbuf
*,
6638 const struct sadb_msghdr
*) = {
6639 NULL
, /* SADB_RESERVED */
6640 key_getspi
, /* SADB_GETSPI */
6641 key_update
, /* SADB_UPDATE */
6642 key_add
, /* SADB_ADD */
6643 key_delete
, /* SADB_DELETE */
6644 key_get
, /* SADB_GET */
6645 key_acquire2
, /* SADB_ACQUIRE */
6646 key_register
, /* SADB_REGISTER */
6647 NULL
, /* SADB_EXPIRE */
6648 key_flush
, /* SADB_FLUSH */
6649 key_dump
, /* SADB_DUMP */
6650 key_promisc
, /* SADB_X_PROMISC */
6651 NULL
, /* SADB_X_PCHANGE */
6652 key_spdadd
, /* SADB_X_SPDUPDATE */
6653 key_spdadd
, /* SADB_X_SPDADD */
6654 key_spddelete
, /* SADB_X_SPDDELETE */
6655 key_spdget
, /* SADB_X_SPDGET */
6656 NULL
, /* SADB_X_SPDACQUIRE */
6657 key_spddump
, /* SADB_X_SPDDUMP */
6658 key_spdflush
, /* SADB_X_SPDFLUSH */
6659 key_spdadd
, /* SADB_X_SPDSETIDX */
6660 NULL
, /* SADB_X_SPDEXPIRE */
6661 key_spddelete2
, /* SADB_X_SPDDELETE2 */
6665 * parse sadb_msg buffer to process PFKEYv2,
6666 * and create a data to response if needed.
6667 * I think to be dealed with mbuf directly.
6669 * msgp : pointer to pointer to a received buffer pulluped.
6670 * This is rewrited to response.
6671 * so : pointer to socket.
6673 * length for buffer to send to user process.
6676 key_parse(struct mbuf
*m
, struct socket
*so
)
6678 struct sadb_msg
*msg
;
6679 struct sadb_msghdr mh
;
6684 if (m
== NULL
|| so
== NULL
)
6685 panic("key_parse: NULL pointer is passed.");
6687 #if 0 /*kdebug_sadb assumes msg in linear buffer*/
6688 KEYDEBUG(KEYDEBUG_KEY_DUMP
,
6689 ipseclog((LOG_DEBUG
, "key_parse: passed sadb_msg\n"));
6693 if (m
->m_len
< sizeof(struct sadb_msg
)) {
6694 m
= m_pullup(m
, sizeof(struct sadb_msg
));
6698 msg
= mtod(m
, struct sadb_msg
*);
6699 target
= KEY_SENDUP_ONE
;
6701 if ((m
->m_flags
& M_PKTHDR
) == 0 ||
6702 m
->m_pkthdr
.len
!= m
->m_pkthdr
.len
) {
6703 ipseclog((LOG_DEBUG
, "key_parse: invalid message length.\n"));
6704 pfkeystat
.out_invlen
++;
6709 if (msg
->sadb_msg_version
!= PF_KEY_V2
) {
6710 ipseclog((LOG_DEBUG
,
6711 "key_parse: PF_KEY version %u is mismatched.\n",
6712 msg
->sadb_msg_version
));
6713 pfkeystat
.out_invver
++;
6718 if (msg
->sadb_msg_type
> SADB_MAX
) {
6719 ipseclog((LOG_DEBUG
, "key_parse: invalid type %u is passed.\n",
6720 msg
->sadb_msg_type
));
6721 pfkeystat
.out_invmsgtype
++;
6726 /* for old-fashioned code - should be nuked */
6727 if (m
->m_pkthdr
.len
> MCLBYTES
) {
6734 MGETHDR(n
, M_NOWAIT
, MT_DATA
);
6735 if (n
&& m
->m_pkthdr
.len
> MHLEN
) {
6736 MCLGET(n
, M_NOWAIT
);
6737 if ((n
->m_flags
& M_EXT
) == 0) {
6746 m_copydata(m
, 0, m
->m_pkthdr
.len
, mtod(n
, caddr_t
));
6747 n
->m_pkthdr
.len
= n
->m_len
= m
->m_pkthdr
.len
;
6753 /* align the mbuf chain so that extensions are in contiguous region. */
6754 error
= key_align(m
, &mh
);
6758 if (m
->m_next
) { /*XXX*/
6766 switch (msg
->sadb_msg_satype
) {
6767 case SADB_SATYPE_UNSPEC
:
6768 switch (msg
->sadb_msg_type
) {
6776 ipseclog((LOG_DEBUG
, "key_parse: must specify satype "
6777 "when msg type=%u.\n", msg
->sadb_msg_type
));
6778 pfkeystat
.out_invsatype
++;
6783 case SADB_SATYPE_AH
:
6784 case SADB_SATYPE_ESP
:
6785 case SADB_X_SATYPE_IPCOMP
:
6786 case SADB_X_SATYPE_TCPSIGNATURE
:
6787 switch (msg
->sadb_msg_type
) {
6789 case SADB_X_SPDDELETE
:
6791 case SADB_X_SPDDUMP
:
6792 case SADB_X_SPDFLUSH
:
6793 case SADB_X_SPDSETIDX
:
6794 case SADB_X_SPDUPDATE
:
6795 case SADB_X_SPDDELETE2
:
6796 ipseclog((LOG_DEBUG
, "key_parse: illegal satype=%u\n",
6797 msg
->sadb_msg_type
));
6798 pfkeystat
.out_invsatype
++;
6803 case SADB_SATYPE_RSVP
:
6804 case SADB_SATYPE_OSPFV2
:
6805 case SADB_SATYPE_RIPV2
:
6806 case SADB_SATYPE_MIP
:
6807 ipseclog((LOG_DEBUG
, "key_parse: type %u isn't supported.\n",
6808 msg
->sadb_msg_satype
));
6809 pfkeystat
.out_invsatype
++;
6812 case 1: /* XXX: What does it do? */
6813 if (msg
->sadb_msg_type
== SADB_X_PROMISC
)
6817 ipseclog((LOG_DEBUG
, "key_parse: invalid type %u is passed.\n",
6818 msg
->sadb_msg_satype
));
6819 pfkeystat
.out_invsatype
++;
6824 /* check field of upper layer protocol and address family */
6825 if (mh
.ext
[SADB_EXT_ADDRESS_SRC
] != NULL
6826 && mh
.ext
[SADB_EXT_ADDRESS_DST
] != NULL
) {
6827 struct sadb_address
*src0
, *dst0
;
6830 src0
= (struct sadb_address
*)(mh
.ext
[SADB_EXT_ADDRESS_SRC
]);
6831 dst0
= (struct sadb_address
*)(mh
.ext
[SADB_EXT_ADDRESS_DST
]);
6833 /* check upper layer protocol */
6834 if (src0
->sadb_address_proto
!= dst0
->sadb_address_proto
) {
6835 ipseclog((LOG_DEBUG
, "key_parse: upper layer protocol mismatched.\n"));
6836 pfkeystat
.out_invaddr
++;
6842 if (PFKEY_ADDR_SADDR(src0
)->sa_family
!=
6843 PFKEY_ADDR_SADDR(dst0
)->sa_family
) {
6844 ipseclog((LOG_DEBUG
, "key_parse: address family mismatched.\n"));
6845 pfkeystat
.out_invaddr
++;
6849 if (PFKEY_ADDR_SADDR(src0
)->sa_len
!=
6850 PFKEY_ADDR_SADDR(dst0
)->sa_len
) {
6851 ipseclog((LOG_DEBUG
,
6852 "key_parse: address struct size mismatched.\n"));
6853 pfkeystat
.out_invaddr
++;
6858 switch (PFKEY_ADDR_SADDR(src0
)->sa_family
) {
6860 if (PFKEY_ADDR_SADDR(src0
)->sa_len
!=
6861 sizeof(struct sockaddr_in
)) {
6862 pfkeystat
.out_invaddr
++;
6868 if (PFKEY_ADDR_SADDR(src0
)->sa_len
!=
6869 sizeof(struct sockaddr_in6
)) {
6870 pfkeystat
.out_invaddr
++;
6876 ipseclog((LOG_DEBUG
,
6877 "key_parse: unsupported address family.\n"));
6878 pfkeystat
.out_invaddr
++;
6879 error
= EAFNOSUPPORT
;
6883 switch (PFKEY_ADDR_SADDR(src0
)->sa_family
) {
6885 plen
= sizeof(struct in_addr
) << 3;
6888 plen
= sizeof(struct in6_addr
) << 3;
6891 plen
= 0; /*fool gcc*/
6895 /* check max prefix length */
6896 if (src0
->sadb_address_prefixlen
> plen
||
6897 dst0
->sadb_address_prefixlen
> plen
) {
6898 ipseclog((LOG_DEBUG
,
6899 "key_parse: illegal prefixlen.\n"));
6900 pfkeystat
.out_invaddr
++;
6906 * prefixlen == 0 is valid because there can be a case when
6907 * all addresses are matched.
6911 if (msg
->sadb_msg_type
>= NELEM(key_typesw
) ||
6912 key_typesw
[msg
->sadb_msg_type
] == NULL
) {
6913 pfkeystat
.out_invmsgtype
++;
6918 lwkt_gettoken(&key_token
);
6919 error
= (*key_typesw
[msg
->sadb_msg_type
])(so
, m
, &mh
);
6920 lwkt_reltoken(&key_token
);
6924 msg
->sadb_msg_errno
= error
;
6925 lwkt_gettoken(&key_token
);
6926 error
= key_sendup_mbuf(so
, m
, target
);
6927 lwkt_reltoken(&key_token
);
6932 key_senderror(struct socket
*so
, struct mbuf
*m
, int code
)
6934 struct sadb_msg
*msg
;
6936 if (m
->m_len
< sizeof(struct sadb_msg
))
6937 panic("invalid mbuf passed to key_senderror");
6939 msg
= mtod(m
, struct sadb_msg
*);
6940 msg
->sadb_msg_errno
= code
;
6941 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ONE
);
6945 * set the pointer to each header into message buffer.
6946 * m will be freed on error.
6947 * XXX larger-than-MCLBYTES extension?
6950 key_align(struct mbuf
*m
, struct sadb_msghdr
*mhp
)
6953 struct sadb_ext
*ext
;
6959 if (m
== NULL
|| mhp
== NULL
)
6960 panic("key_align: NULL pointer is passed.");
6961 if (m
->m_len
< sizeof(struct sadb_msg
))
6962 panic("invalid mbuf passed to key_align");
6965 bzero(mhp
, sizeof(*mhp
));
6967 mhp
->msg
= mtod(m
, struct sadb_msg
*);
6968 mhp
->ext
[0] = (struct sadb_ext
*)mhp
->msg
; /*XXX backward compat */
6970 end
= PFKEY_UNUNIT64(mhp
->msg
->sadb_msg_len
);
6971 extlen
= end
; /*just in case extlen is not updated*/
6972 for (off
= sizeof(struct sadb_msg
); off
< end
; off
+= extlen
) {
6973 n
= m_pulldown(m
, off
, sizeof(struct sadb_ext
), &toff
);
6975 /* m is already freed */
6978 ext
= (struct sadb_ext
*)(mtod(n
, caddr_t
) + toff
);
6981 switch (ext
->sadb_ext_type
) {
6983 case SADB_EXT_ADDRESS_SRC
:
6984 case SADB_EXT_ADDRESS_DST
:
6985 case SADB_EXT_ADDRESS_PROXY
:
6986 case SADB_EXT_LIFETIME_CURRENT
:
6987 case SADB_EXT_LIFETIME_HARD
:
6988 case SADB_EXT_LIFETIME_SOFT
:
6989 case SADB_EXT_KEY_AUTH
:
6990 case SADB_EXT_KEY_ENCRYPT
:
6991 case SADB_EXT_IDENTITY_SRC
:
6992 case SADB_EXT_IDENTITY_DST
:
6993 case SADB_EXT_SENSITIVITY
:
6994 case SADB_EXT_PROPOSAL
:
6995 case SADB_EXT_SUPPORTED_AUTH
:
6996 case SADB_EXT_SUPPORTED_ENCRYPT
:
6997 case SADB_EXT_SPIRANGE
:
6998 case SADB_X_EXT_POLICY
:
6999 case SADB_X_EXT_SA2
:
7000 /* duplicate check */
7002 * XXX Are there duplication payloads of either
7003 * KEY_AUTH or KEY_ENCRYPT ?
7005 if (mhp
->ext
[ext
->sadb_ext_type
] != NULL
) {
7006 ipseclog((LOG_DEBUG
,
7007 "key_align: duplicate ext_type %u "
7008 "is passed.\n", ext
->sadb_ext_type
));
7010 pfkeystat
.out_dupext
++;
7015 ipseclog((LOG_DEBUG
,
7016 "key_align: invalid ext_type %u is passed.\n",
7017 ext
->sadb_ext_type
));
7019 pfkeystat
.out_invexttype
++;
7023 extlen
= PFKEY_UNUNIT64(ext
->sadb_ext_len
);
7025 if (key_validate_ext(ext
, extlen
)) {
7027 pfkeystat
.out_invlen
++;
7031 n
= m_pulldown(m
, off
, extlen
, &toff
);
7033 /* m is already freed */
7036 ext
= (struct sadb_ext
*)(mtod(n
, caddr_t
) + toff
);
7038 mhp
->ext
[ext
->sadb_ext_type
] = ext
;
7039 mhp
->extoff
[ext
->sadb_ext_type
] = off
;
7040 mhp
->extlen
[ext
->sadb_ext_type
] = extlen
;
7045 pfkeystat
.out_invlen
++;
7053 key_validate_ext(const struct sadb_ext
*ext
, int len
)
7055 const struct sockaddr
*sa
;
7056 enum { NONE
, ADDR
} checktype
= NONE
;
7058 const int sal
= offsetof(struct sockaddr
, sa_len
) + sizeof(sa
->sa_len
);
7060 if (len
!= PFKEY_UNUNIT64(ext
->sadb_ext_len
))
7063 /* if it does not match minimum/maximum length, bail */
7064 if (ext
->sadb_ext_type
>= NELEM(minsize
) ||
7065 ext
->sadb_ext_type
>= NELEM(maxsize
))
7067 if (!minsize
[ext
->sadb_ext_type
] || len
< minsize
[ext
->sadb_ext_type
])
7069 if (maxsize
[ext
->sadb_ext_type
] && len
> maxsize
[ext
->sadb_ext_type
])
7072 /* more checks based on sadb_ext_type XXX need more */
7073 switch (ext
->sadb_ext_type
) {
7074 case SADB_EXT_ADDRESS_SRC
:
7075 case SADB_EXT_ADDRESS_DST
:
7076 case SADB_EXT_ADDRESS_PROXY
:
7077 baselen
= PFKEY_ALIGN8(sizeof(struct sadb_address
));
7080 case SADB_EXT_IDENTITY_SRC
:
7081 case SADB_EXT_IDENTITY_DST
:
7082 if (((const struct sadb_ident
*)ext
)->sadb_ident_type
==
7083 SADB_X_IDENTTYPE_ADDR
) {
7084 baselen
= PFKEY_ALIGN8(sizeof(struct sadb_ident
));
7094 switch (checktype
) {
7098 sa
= (const struct sockaddr
*)((c_caddr_t
)ext
+ baselen
);
7099 if (len
< baselen
+ sal
)
7101 if (baselen
+ PFKEY_ALIGN8(sa
->sa_len
) != len
)
7114 bzero((caddr_t
)&key_cb
, sizeof(key_cb
));
7116 for (i
= 0; i
< IPSEC_DIR_MAX
; i
++) {
7117 LIST_INIT(&sptree
[i
]);
7120 LIST_INIT(&sahtree
);
7122 for (i
= 0; i
<= SADB_SATYPE_MAX
; i
++) {
7123 LIST_INIT(®tree
[i
]);
7126 #ifndef IPSEC_NONBLOCK_ACQUIRE
7127 LIST_INIT(&acqtree
);
7129 LIST_INIT(&spacqtree
);
7131 /* system default */
7133 ip4_def_policy
.policy
= IPSEC_POLICY_NONE
;
7134 ip4_def_policy
.refcnt
++; /*never reclaim this*/
7137 ip6_def_policy
.policy
= IPSEC_POLICY_NONE
;
7138 ip6_def_policy
.refcnt
++; /*never reclaim this*/
7141 #ifndef IPSEC_DEBUG2
7142 callout_init(&key_timehandler_ch
);
7143 callout_reset(&key_timehandler_ch
, hz
, key_timehandler
, NULL
);
7144 #endif /*IPSEC_DEBUG2*/
7146 /* initialize key statistics */
7147 keystat
.getspi_count
= 1;
7149 kprintf("IPsec: Initialized Security Association Processing.\n");
7155 * XXX: maybe This function is called after INBOUND IPsec processing.
7157 * Special check for tunnel-mode packets.
7158 * We must make some checks for consistency between inner and outer IP header.
7160 * xxx more checks to be provided
7163 key_checktunnelsanity(struct secasvar
*sav
, u_int family
,
7164 caddr_t src
, caddr_t dst
)
7167 if (sav
->sah
== NULL
)
7168 panic("sav->sah == NULL at key_checktunnelsanity");
7170 /* XXX: check inner IP header */
7176 #define hostnamelen strlen(hostname)
7179 * Get FQDN for the host.
7180 * If the administrator configured hostname (by hostname(1)) without
7181 * domain name, returns nothing.
7188 static char fqdn
[MAXHOSTNAMELEN
+ 1];
7193 /* check if it comes with domain name. */
7195 for (i
= 0; i
< hostnamelen
; i
++) {
7196 if (hostname
[i
] == '.')
7202 /* NOTE: hostname may not be NUL-terminated. */
7203 bzero(fqdn
, sizeof(fqdn
));
7204 bcopy(hostname
, fqdn
, hostnamelen
);
7205 fqdn
[hostnamelen
] = '\0';
7210 * get username@FQDN for the host/user.
7213 key_getuserfqdn(void)
7216 static char userfqdn
[MAXHOSTNAMELEN
+ MAXLOGNAME
+ 2];
7217 struct proc
*p
= curproc
;
7220 if (!p
|| !p
->p_pgrp
|| !p
->p_pgrp
->pg_session
)
7222 if (!(host
= key_getfqdn()))
7225 /* NOTE: s_login may not be-NUL terminated. */
7226 bzero(userfqdn
, sizeof(userfqdn
));
7227 bcopy(p
->p_pgrp
->pg_session
->s_login
, userfqdn
, MAXLOGNAME
);
7228 userfqdn
[MAXLOGNAME
] = '\0'; /* safeguard */
7229 q
= userfqdn
+ strlen(userfqdn
);
7231 bcopy(host
, q
, strlen(host
));
7239 /* record data transfer on SA, and update timestamps */
7241 key_sa_recordxfer(struct secasvar
*sav
, struct mbuf
*m
)
7244 panic("key_sa_recordxfer called with sav == NULL");
7246 panic("key_sa_recordxfer called with m == NULL");
7251 * XXX Currently, there is a difference of bytes size
7252 * between inbound and outbound processing.
7254 sav
->lft_c
->sadb_lifetime_bytes
+= m
->m_pkthdr
.len
;
7255 /* to check bytes lifetime is done in key_timehandler(). */
7258 * We use the number of packets as the unit of
7259 * sadb_lifetime_allocations. We increment the variable
7260 * whenever {esp,ah}_{in,out}put is called.
7262 sav
->lft_c
->sadb_lifetime_allocations
++;
7263 /* XXX check for expires? */
7266 * NOTE: We record CURRENT sadb_lifetime_usetime by using wall clock,
7267 * in seconds. HARD and SOFT lifetime are measured by the time
7268 * difference (again in seconds) from sadb_lifetime_usetime.
7272 * -----+-----+--------+---> t
7273 * <--------------> HARD
7279 sav
->lft_c
->sadb_lifetime_usetime
= tv
.tv_sec
;
7280 /* XXX check for expires? */
7288 key_sa_routechange(struct sockaddr
*dst
)
7290 struct secashead
*sah
;
7293 lwkt_gettoken(&key_token
);
7294 LIST_FOREACH(sah
, &sahtree
, chain
) {
7295 ro
= &sah
->sa_route
;
7296 if (ro
->ro_rt
&& dst
->sa_len
== ro
->ro_dst
.sa_len
7297 && bcmp(dst
, &ro
->ro_dst
, dst
->sa_len
) == 0) {
7302 lwkt_reltoken(&key_token
);
7306 key_sa_chgstate(struct secasvar
*sav
, u_int8_t state
)
7309 panic("key_sa_chgstate called with sav == NULL");
7311 if (sav
->state
== state
)
7314 if (__LIST_CHAINED(sav
))
7315 LIST_REMOVE(sav
, chain
);
7318 LIST_INSERT_HEAD(&sav
->sah
->savtree
[state
], sav
, chain
);
7322 key_sa_stir_iv(struct secasvar
*sav
)
7325 panic("key_sa_stir_iv called with sav == NULL");
7326 key_randomfill(sav
->iv
, sav
->ivlen
);
7330 static struct mbuf
*
7331 key_alloc_mbuf(int l
)
7333 struct mbuf
*m
= NULL
, *n
;
7338 MGET(n
, M_NOWAIT
, MT_DATA
);
7339 if (n
&& len
> MLEN
)
7340 MCLGET(n
, M_NOWAIT
);
7348 n
->m_len
= M_TRAILINGSPACE(n
);
7349 /* use the bottom of mbuf, hoping we can prepend afterwards */
7350 if (n
->m_len
> len
) {
7351 t
= (n
->m_len
- len
) & ~(sizeof(long) - 1);