1 /* $FreeBSD: src/sys/netkey/key.c,v 1.16.2.5 2001/07/03 11:01:58 ume Exp $ */
2 /* $KAME: key.c,v 1.187 2001/05/24 07:41:22 itojun 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
37 #include <sys/types.h>
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
42 #include <sys/domain.h>
43 #include <sys/protosw.h>
44 #include <sys/malloc.h>
45 #include <sys/socket.h>
46 #include <sys/socketvar.h>
47 #include <sys/sysctl.h>
48 #include <sys/errno.h>
50 #include <sys/queue.h>
53 #include <net/route.h>
54 #include <net/raw_cb.h>
56 #include <netinet/in.h>
57 #include <netinet/in_systm.h>
58 #include <netinet/ip.h>
59 #include <netinet/in_var.h>
62 #include <netinet/ip6.h>
63 #include <netinet6/in6_var.h>
64 #include <netinet6/ip6_var.h>
68 #include <netinet/in_pcb.h>
71 #include <netinet6/in6_pcb.h>
74 #include <net/pfkeyv2.h>
75 #include <netkey/keydb.h>
76 #include <netkey/key.h>
77 #include <netkey/keysock.h>
78 #include <netkey/key_debug.h>
82 #include <netinet6/ipsec.h>
84 #include <netinet6/ipsec6.h>
86 #include <netinet6/ah.h>
88 #include <netinet6/ah6.h>
91 #include <netinet6/esp.h>
93 #include <netinet6/esp6.h>
96 #include <netinet6/ipcomp.h>
98 #include <netinet6/ipcomp6.h>
103 #include <sys/random.h>
105 #include <net/net_osdep.h>
108 #define satosin(s) ((struct sockaddr_in *)s)
112 * Note on SA reference counting:
113 * - SAs that are not in DEAD state will have (total external reference + 1)
114 * following value in reference count field. they cannot be freed and are
115 * referenced from SA header.
116 * - SAs that are in DEAD state will have (total external reference)
117 * in reference count field. they are ready to be freed. reference from
118 * SA header will be removed in key_delsav(), when the reference count
119 * field hits 0 (= no external reference other than from SA header.
122 u_int32_t key_debug_level
= 0; //### our sysctl is not dynamic
123 static u_int key_spi_trycnt
= 1000;
124 static u_int32_t key_spi_minval
= 0x100;
125 static u_int32_t key_spi_maxval
= 0x0fffffff; /* XXX */
126 static u_int32_t policy_id
= 0;
127 static u_int key_int_random
= 60; /*interval to initialize randseed,1(m)*/
128 static u_int key_larval_lifetime
= 30; /* interval to expire acquiring, 30(s)*/
129 static int key_blockacq_count
= 10; /* counter for blocking SADB_ACQUIRE.*/
130 static int key_blockacq_lifetime
= 20; /* lifetime for blocking SADB_ACQUIRE.*/
132 static u_int32_t acq_seq
= 0;
133 static int key_tick_init_random
= 0;
135 static LIST_HEAD(_sptree
, secpolicy
) sptree
[IPSEC_DIR_MAX
]; /* SPD */
136 static LIST_HEAD(_sahtree
, secashead
) sahtree
; /* SAD */
137 static LIST_HEAD(_regtree
, secreg
) regtree
[SADB_SATYPE_MAX
+ 1];
139 #ifndef IPSEC_NONBLOCK_ACQUIRE
140 static LIST_HEAD(_acqtree
, secacq
) acqtree
; /* acquiring list */
142 static LIST_HEAD(_spacqtree
, secspacq
) spacqtree
; /* SP acquiring list */
144 struct key_cb key_cb
;
146 /* search order for SAs */
147 static u_int saorder_state_valid
[] = {
148 SADB_SASTATE_DYING
, SADB_SASTATE_MATURE
,
150 * This order is important because we must select a oldest SA
151 * for outbound processing. For inbound, This is not important.
154 static u_int saorder_state_alive
[] = {
156 SADB_SASTATE_MATURE
, SADB_SASTATE_DYING
, SADB_SASTATE_LARVAL
158 static u_int saorder_state_any
[] = {
159 SADB_SASTATE_MATURE
, SADB_SASTATE_DYING
,
160 SADB_SASTATE_LARVAL
, SADB_SASTATE_DEAD
163 static const int minsize
[] = {
164 sizeof(struct sadb_msg
), /* SADB_EXT_RESERVED */
165 sizeof(struct sadb_sa
), /* SADB_EXT_SA */
166 sizeof(struct sadb_lifetime
), /* SADB_EXT_LIFETIME_CURRENT */
167 sizeof(struct sadb_lifetime
), /* SADB_EXT_LIFETIME_HARD */
168 sizeof(struct sadb_lifetime
), /* SADB_EXT_LIFETIME_SOFT */
169 sizeof(struct sadb_address
), /* SADB_EXT_ADDRESS_SRC */
170 sizeof(struct sadb_address
), /* SADB_EXT_ADDRESS_DST */
171 sizeof(struct sadb_address
), /* SADB_EXT_ADDRESS_PROXY */
172 sizeof(struct sadb_key
), /* SADB_EXT_KEY_AUTH */
173 sizeof(struct sadb_key
), /* SADB_EXT_KEY_ENCRYPT */
174 sizeof(struct sadb_ident
), /* SADB_EXT_IDENTITY_SRC */
175 sizeof(struct sadb_ident
), /* SADB_EXT_IDENTITY_DST */
176 sizeof(struct sadb_sens
), /* SADB_EXT_SENSITIVITY */
177 sizeof(struct sadb_prop
), /* SADB_EXT_PROPOSAL */
178 sizeof(struct sadb_supported
), /* SADB_EXT_SUPPORTED_AUTH */
179 sizeof(struct sadb_supported
), /* SADB_EXT_SUPPORTED_ENCRYPT */
180 sizeof(struct sadb_spirange
), /* SADB_EXT_SPIRANGE */
181 0, /* SADB_X_EXT_KMPRIVATE */
182 sizeof(struct sadb_x_policy
), /* SADB_X_EXT_POLICY */
183 sizeof(struct sadb_x_sa2
), /* SADB_X_SA2 */
185 static const int maxsize
[] = {
186 sizeof(struct sadb_msg
), /* SADB_EXT_RESERVED */
187 sizeof(struct sadb_sa
), /* SADB_EXT_SA */
188 sizeof(struct sadb_lifetime
), /* SADB_EXT_LIFETIME_CURRENT */
189 sizeof(struct sadb_lifetime
), /* SADB_EXT_LIFETIME_HARD */
190 sizeof(struct sadb_lifetime
), /* SADB_EXT_LIFETIME_SOFT */
191 0, /* SADB_EXT_ADDRESS_SRC */
192 0, /* SADB_EXT_ADDRESS_DST */
193 0, /* SADB_EXT_ADDRESS_PROXY */
194 0, /* SADB_EXT_KEY_AUTH */
195 0, /* SADB_EXT_KEY_ENCRYPT */
196 0, /* SADB_EXT_IDENTITY_SRC */
197 0, /* SADB_EXT_IDENTITY_DST */
198 0, /* SADB_EXT_SENSITIVITY */
199 0, /* SADB_EXT_PROPOSAL */
200 0, /* SADB_EXT_SUPPORTED_AUTH */
201 0, /* SADB_EXT_SUPPORTED_ENCRYPT */
202 sizeof(struct sadb_spirange
), /* SADB_EXT_SPIRANGE */
203 0, /* SADB_X_EXT_KMPRIVATE */
204 0, /* SADB_X_EXT_POLICY */
205 sizeof(struct sadb_x_sa2
), /* SADB_X_SA2 */
208 static int ipsec_esp_keymin
= 256;
209 static int ipsec_esp_auth
= 0;
210 static int ipsec_ah_keymin
= 128;
212 SYSCTL_DECL(_net_key
);
214 SYSCTL_INT(_net_key
, KEYCTL_DEBUG_LEVEL
, debug
, CTLFLAG_RW
, \
215 &key_debug_level
, 0, "");
218 /* max count of trial for the decision of spi value */
219 SYSCTL_INT(_net_key
, KEYCTL_SPI_TRY
, spi_trycnt
, CTLFLAG_RW
, \
220 &key_spi_trycnt
, 0, "");
222 /* minimum spi value to allocate automatically. */
223 SYSCTL_INT(_net_key
, KEYCTL_SPI_MIN_VALUE
, spi_minval
, CTLFLAG_RW
, \
224 &key_spi_minval
, 0, "");
226 /* maximun spi value to allocate automatically. */
227 SYSCTL_INT(_net_key
, KEYCTL_SPI_MAX_VALUE
, spi_maxval
, CTLFLAG_RW
, \
228 &key_spi_maxval
, 0, "");
230 /* interval to initialize randseed */
231 SYSCTL_INT(_net_key
, KEYCTL_RANDOM_INT
, int_random
, CTLFLAG_RW
, \
232 &key_int_random
, 0, "");
234 /* lifetime for larval SA */
235 SYSCTL_INT(_net_key
, KEYCTL_LARVAL_LIFETIME
, larval_lifetime
, CTLFLAG_RW
, \
236 &key_larval_lifetime
, 0, "");
238 /* counter for blocking to send SADB_ACQUIRE to IKEd */
239 SYSCTL_INT(_net_key
, KEYCTL_BLOCKACQ_COUNT
, blockacq_count
, CTLFLAG_RW
, \
240 &key_blockacq_count
, 0, "");
242 /* lifetime for blocking to send SADB_ACQUIRE to IKEd */
243 SYSCTL_INT(_net_key
, KEYCTL_BLOCKACQ_LIFETIME
, blockacq_lifetime
, CTLFLAG_RW
, \
244 &key_blockacq_lifetime
, 0, "");
246 /* minimum ESP key length */
247 SYSCTL_INT(_net_key
, KEYCTL_ESP_KEYMIN
, esp_keymin
, CTLFLAG_RW
, \
248 &ipsec_esp_keymin
, 0, "");
250 /* minimum AH key length */
251 SYSCTL_INT(_net_key
, KEYCTL_AH_KEYMIN
, ah_keymin
, CTLFLAG_RW
, \
252 &ipsec_ah_keymin
, 0, "");
255 #define LIST_FOREACH(elm, head, field) \
256 for (elm = LIST_FIRST(head); elm; elm = LIST_NEXT(elm, field))
258 #define __LIST_CHAINED(elm) \
259 (!((elm)->chain.le_next == NULL && (elm)->chain.le_prev == NULL))
260 #define LIST_INSERT_TAIL(head, elm, type, field) \
262 struct type *curelm = LIST_FIRST(head); \
263 if (curelm == NULL) {\
264 LIST_INSERT_HEAD(head, elm, field); \
266 while (LIST_NEXT(curelm, field)) \
267 curelm = LIST_NEXT(curelm, field);\
268 LIST_INSERT_AFTER(curelm, elm, field);\
272 #define KEY_CHKSASTATE(head, sav, name) \
274 if ((head) != (sav)) { \
275 printf("%s: state mismatched (TREE=%d SA=%d)\n", \
276 (name), (head), (sav)); \
281 #define KEY_CHKSPDIR(head, sp, name) \
283 if ((head) != (sp)) { \
284 printf("%s: direction mismatched (TREE=%d SP=%d), " \
285 "anyway continue.\n", \
286 (name), (head), (sp)); \
291 #define KMALLOC(p, t, n) \
292 ((p) = (t) _MALLOC((unsigned long)(n), M_SECA, M_NOWAIT))
294 _FREE((caddr_t)(p), M_SECA);
296 #define KMALLOC(p, t, n) \
298 ((p) = (t)_MALLOC((unsigned long)(n), M_SECA, M_NOWAIT)); \
299 printf("%s %d: %p <- KMALLOC(%s, %d)\n", \
300 __FILE__, __LINE__, (p), #t, n); \
305 printf("%s %d: %p -> KFREE()\n", __FILE__, __LINE__, (p)); \
306 _FREE((caddr_t)(p), M_SECA); \
311 * set parameters into secpolicyindex buffer.
312 * Must allocate secpolicyindex buffer passed to this function.
314 #define KEY_SETSECSPIDX(_dir, s, d, ps, pd, ulp, idx) \
316 bzero((idx), sizeof(struct secpolicyindex)); \
317 (idx)->dir = (_dir); \
318 (idx)->prefs = (ps); \
319 (idx)->prefd = (pd); \
320 (idx)->ul_proto = (ulp); \
321 bcopy((s), &(idx)->src, ((struct sockaddr *)(s))->sa_len); \
322 bcopy((d), &(idx)->dst, ((struct sockaddr *)(d))->sa_len); \
326 * set parameters into secasindex buffer.
327 * Must allocate secasindex buffer before calling this function.
329 #define KEY_SETSECASIDX(p, m, r, s, d, idx) \
331 bzero((idx), sizeof(struct secasindex)); \
332 (idx)->proto = (p); \
334 (idx)->reqid = (r); \
335 bcopy((s), &(idx)->src, ((struct sockaddr *)(s))->sa_len); \
336 bcopy((d), &(idx)->dst, ((struct sockaddr *)(d))->sa_len); \
341 u_long getspi_count
; /* the avarage of count to try to get new SPI */
345 struct sadb_msg
*msg
;
346 struct sadb_ext
*ext
[SADB_EXT_MAX
+ 1];
347 int extoff
[SADB_EXT_MAX
+ 1];
348 int extlen
[SADB_EXT_MAX
+ 1];
351 static struct secasvar
*key_allocsa_policy
__P((struct secasindex
*));
352 static void key_freesp_so
__P((struct secpolicy
**));
353 static struct secasvar
*key_do_allocsa_policy
__P((struct secashead
*, u_int
));
354 static void key_delsp
__P((struct secpolicy
*));
355 static struct secpolicy
*key_getsp
__P((struct secpolicyindex
*));
356 static struct secpolicy
*key_getspbyid
__P((u_int32_t
));
357 static u_int32_t key_newreqid
__P((void));
358 static struct mbuf
*key_gather_mbuf
__P((struct mbuf
*,
359 const struct sadb_msghdr
*, int, int, int *));
360 static int key_spdadd
__P((struct socket
*, struct mbuf
*,
361 const struct sadb_msghdr
*));
362 static u_int32_t key_getnewspid
__P((void));
363 static int key_spddelete
__P((struct socket
*, struct mbuf
*,
364 const struct sadb_msghdr
*));
365 static int key_spddelete2
__P((struct socket
*, struct mbuf
*,
366 const struct sadb_msghdr
*));
367 static int key_spdget
__P((struct socket
*, struct mbuf
*,
368 const struct sadb_msghdr
*));
369 static int key_spdflush
__P((struct socket
*, struct mbuf
*,
370 const struct sadb_msghdr
*));
371 static int key_spddump
__P((struct socket
*, struct mbuf
*,
372 const struct sadb_msghdr
*));
373 static struct mbuf
*key_setdumpsp
__P((struct secpolicy
*,
374 u_int8_t
, u_int32_t
, u_int32_t
));
375 static u_int key_getspreqmsglen
__P((struct secpolicy
*));
376 static int key_spdexpire
__P((struct secpolicy
*));
377 static struct secashead
*key_newsah
__P((struct secasindex
*));
378 static void key_delsah
__P((struct secashead
*));
379 static struct secasvar
*key_newsav
__P((struct mbuf
*,
380 const struct sadb_msghdr
*, struct secashead
*, int *));
381 static void key_delsav
__P((struct secasvar
*));
382 static struct secashead
*key_getsah
__P((struct secasindex
*));
383 static struct secasvar
*key_checkspidup
__P((struct secasindex
*, u_int32_t
));
384 static struct secasvar
*key_getsavbyspi
__P((struct secashead
*, u_int32_t
));
385 static int key_setsaval
__P((struct secasvar
*, struct mbuf
*,
386 const struct sadb_msghdr
*));
387 static int key_mature
__P((struct secasvar
*));
388 static struct mbuf
*key_setdumpsa
__P((struct secasvar
*, u_int8_t
,
389 u_int8_t
, u_int32_t
, u_int32_t
));
390 static struct mbuf
*key_setsadbmsg
__P((u_int8_t
, u_int16_t
, u_int8_t
,
391 u_int32_t
, pid_t
, u_int16_t
));
392 static struct mbuf
*key_setsadbsa
__P((struct secasvar
*));
393 static struct mbuf
*key_setsadbaddr
__P((u_int16_t
,
394 struct sockaddr
*, u_int8_t
, u_int16_t
));
396 static struct mbuf
*key_setsadbident
__P((u_int16_t
, u_int16_t
, caddr_t
,
399 static struct mbuf
*key_setsadbxsa2(u_int8_t
, u_int32_t
);
400 static struct mbuf
*key_setsadbxpolicy
__P((u_int16_t
, u_int8_t
,
402 static void *key_newbuf
__P((const void *, u_int
));
404 static int key_ismyaddr6
__P((struct sockaddr_in6
*));
406 static int key_cmpsaidx_exactly
407 __P((struct secasindex
*, struct secasindex
*));
408 static int key_cmpsaidx_withmode
409 __P((struct secasindex
*, struct secasindex
*));
410 static int key_cmpsaidx_withoutmode2
411 __P((struct secasindex
*, struct secasindex
*));
412 static int key_cmpsaidx_withoutmode
413 __P((struct secasindex
*, struct secasindex
*));
414 static int key_cmpspidx_exactly
415 __P((struct secpolicyindex
*, struct secpolicyindex
*));
416 static int key_cmpspidx_withmask
417 __P((struct secpolicyindex
*, struct secpolicyindex
*));
418 static int key_sockaddrcmp
__P((struct sockaddr
*, struct sockaddr
*, int));
419 static int key_bbcmp
__P((caddr_t
, caddr_t
, u_int
));
420 static void key_srandom
__P((void));
421 static u_int16_t key_satype2proto
__P((u_int8_t
));
422 static u_int8_t key_proto2satype
__P((u_int16_t
));
424 static int key_getspi
__P((struct socket
*, struct mbuf
*,
425 const struct sadb_msghdr
*));
426 static u_int32_t key_do_getnewspi
__P((struct sadb_spirange
*,
427 struct secasindex
*));
428 static int key_update
__P((struct socket
*, struct mbuf
*,
429 const struct sadb_msghdr
*));
431 static struct secasvar
*key_getsavbyseq
__P((struct secashead
*, u_int32_t
));
433 static int key_add
__P((struct socket
*, struct mbuf
*,
434 const struct sadb_msghdr
*));
435 static int key_setident
__P((struct secashead
*, struct mbuf
*,
436 const struct sadb_msghdr
*));
437 static struct mbuf
*key_getmsgbuf_x1
__P((struct mbuf
*,
438 const struct sadb_msghdr
*));
439 static int key_delete
__P((struct socket
*, struct mbuf
*,
440 const struct sadb_msghdr
*));
441 static int key_get
__P((struct socket
*, struct mbuf
*,
442 const struct sadb_msghdr
*));
444 static void key_getcomb_setlifetime
__P((struct sadb_comb
*));
446 static struct mbuf
*key_getcomb_esp
__P((void));
448 static struct mbuf
*key_getcomb_ah
__P((void));
449 static struct mbuf
*key_getcomb_ipcomp
__P((void));
450 static struct mbuf
*key_getprop
__P((const struct secasindex
*));
452 static int key_acquire
__P((struct secasindex
*, struct secpolicy
*));
453 #ifndef IPSEC_NONBLOCK_ACQUIRE
454 static struct secacq
*key_newacq
__P((struct secasindex
*));
455 static struct secacq
*key_getacq
__P((struct secasindex
*));
456 static struct secacq
*key_getacqbyseq
__P((u_int32_t
));
458 static struct secspacq
*key_newspacq
__P((struct secpolicyindex
*));
459 static struct secspacq
*key_getspacq
__P((struct secpolicyindex
*));
460 static int key_acquire2
__P((struct socket
*, struct mbuf
*,
461 const struct sadb_msghdr
*));
462 static int key_register
__P((struct socket
*, struct mbuf
*,
463 const struct sadb_msghdr
*));
464 static int key_expire
__P((struct secasvar
*));
465 static int key_flush
__P((struct socket
*, struct mbuf
*,
466 const struct sadb_msghdr
*));
467 static int key_dump
__P((struct socket
*, struct mbuf
*,
468 const struct sadb_msghdr
*));
469 static int key_promisc
__P((struct socket
*, struct mbuf
*,
470 const struct sadb_msghdr
*));
471 static int key_senderror
__P((struct socket
*, struct mbuf
*, int));
472 static int key_validate_ext
__P((const struct sadb_ext
*, int));
473 static int key_align
__P((struct mbuf
*, struct sadb_msghdr
*));
475 static const char *key_getfqdn
__P((void));
476 static const char *key_getuserfqdn
__P((void));
478 static void key_sa_chgstate
__P((struct secasvar
*, u_int8_t
));
479 static struct mbuf
*key_alloc_mbuf
__P((int));
481 extern int ipsec_bypass
;
483 /* %%% IPsec policy management */
485 * allocating a SP for OUTBOUND or INBOUND packet.
486 * Must call key_freesp() later.
487 * OUT: NULL: not found
488 * others: found and return the pointer.
491 key_allocsp(spidx
, dir
)
492 struct secpolicyindex
*spidx
;
495 struct secpolicy
*sp
;
501 panic("key_allocsp: NULL pointer is passed.\n");
503 /* check direction */
505 case IPSEC_DIR_INBOUND
:
506 case IPSEC_DIR_OUTBOUND
:
509 panic("key_allocsp: Invalid direction is passed.\n");
513 s
= splnet(); /*called from softclock()*/
514 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
515 printf("*** objects\n");
516 kdebug_secpolicyindex(spidx
));
518 LIST_FOREACH(sp
, &sptree
[dir
], chain
) {
519 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
520 printf("*** in SPD\n");
521 kdebug_secpolicyindex(&sp
->spidx
));
523 if (sp
->state
== IPSEC_SPSTATE_DEAD
)
525 if (key_cmpspidx_withmask(&sp
->spidx
, spidx
))
534 KEY_CHKSPDIR(sp
->spidx
.dir
, dir
, "key_allocsp");
536 /* found a SPD entry */
538 sp
->lastused
= tv
.tv_sec
;
541 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
542 printf("DP key_allocsp cause refcnt++:%d SP:%p\n",
549 * return a policy that matches this particular inbound packet.
553 key_gettunnel(osrc
, odst
, isrc
, idst
)
554 struct sockaddr
*osrc
, *odst
, *isrc
, *idst
;
556 struct secpolicy
*sp
;
557 const int dir
= IPSEC_DIR_INBOUND
;
560 struct ipsecrequest
*r1
, *r2
, *p
;
561 struct sockaddr
*os
, *od
, *is
, *id
;
562 struct secpolicyindex spidx
;
564 s
= splnet(); /*called from softclock()*/
565 LIST_FOREACH(sp
, &sptree
[dir
], chain
) {
566 if (sp
->state
== IPSEC_SPSTATE_DEAD
)
570 for (p
= sp
->req
; p
; p
= p
->next
) {
571 if (p
->saidx
.mode
!= IPSEC_MODE_TUNNEL
)
578 /* here we look at address matches only */
580 if (isrc
->sa_len
> sizeof(spidx
.src
) ||
581 idst
->sa_len
> sizeof(spidx
.dst
))
583 bcopy(isrc
, &spidx
.src
, isrc
->sa_len
);
584 bcopy(idst
, &spidx
.dst
, idst
->sa_len
);
585 if (!key_cmpspidx_withmask(&sp
->spidx
, &spidx
))
588 is
= (struct sockaddr
*)&r1
->saidx
.src
;
589 id
= (struct sockaddr
*)&r1
->saidx
.dst
;
590 if (key_sockaddrcmp(is
, isrc
, 0) ||
591 key_sockaddrcmp(id
, idst
, 0))
595 os
= (struct sockaddr
*)&r2
->saidx
.src
;
596 od
= (struct sockaddr
*)&r2
->saidx
.dst
;
597 if (key_sockaddrcmp(os
, osrc
, 0) ||
598 key_sockaddrcmp(od
, odst
, 0))
609 sp
->lastused
= tv
.tv_sec
;
616 * allocating an SA entry for an *OUTBOUND* packet.
617 * checking each request entries in SP, and acquire an SA if need.
618 * OUT: 0: there are valid requests.
619 * ENOENT: policy may be valid, but SA with REQUIRE is on acquiring.
622 key_checkrequest(isr
, saidx
)
623 struct ipsecrequest
*isr
;
624 struct secasindex
*saidx
;
630 if (isr
== NULL
|| saidx
== NULL
)
631 panic("key_checkrequest: NULL pointer is passed.\n");
634 switch (saidx
->mode
) {
635 case IPSEC_MODE_TRANSPORT
:
636 case IPSEC_MODE_TUNNEL
:
640 panic("key_checkrequest: Invalid policy defined.\n");
643 /* get current level */
644 level
= ipsec_get_reqlevel(isr
);
648 * We do allocate new SA only if the state of SA in the holder is
649 * SADB_SASTATE_DEAD. The SA for outbound must be the oldest.
651 if (isr
->sav
!= NULL
) {
652 if (isr
->sav
->sah
== NULL
)
653 panic("key_checkrequest: sah is null.\n");
654 if (isr
->sav
== (struct secasvar
*)LIST_FIRST(
655 &isr
->sav
->sah
->savtree
[SADB_SASTATE_DEAD
])) {
656 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
657 printf("DP checkrequest calls free SA:%p\n",
659 key_freesav(isr
->sav
);
665 * we free any SA stashed in the IPsec request because a different
666 * SA may be involved each time this request is checked, either
667 * because new SAs are being configured, or this request is
668 * associated with an unconnected datagram socket, or this request
669 * is associated with a system default policy.
671 * The operation may have negative impact to performance. We may
672 * want to check cached SA carefully, rather than picking new SA
675 if (isr
->sav
!= NULL
) {
676 key_freesav(isr
->sav
);
682 * new SA allocation if no SA found.
683 * key_allocsa_policy should allocate the oldest SA available.
684 * See key_do_allocsa_policy(), and draft-jenkins-ipsec-rekeying-03.txt.
686 if (isr
->sav
== NULL
)
687 isr
->sav
= key_allocsa_policy(saidx
);
689 /* When there is SA. */
690 if (isr
->sav
!= NULL
)
694 if ((error
= key_acquire(saidx
, isr
->sp
)) != 0) {
695 /* XXX What I do ? */
697 printf("key_checkrequest: error %d returned "
698 "from key_acquire.\n", error
);
703 return level
== IPSEC_LEVEL_REQUIRE
? ENOENT
: 0;
707 * allocating a SA for policy entry from SAD.
708 * NOTE: searching SAD of aliving state.
709 * OUT: NULL: not found.
710 * others: found and return the pointer.
712 static struct secasvar
*
713 key_allocsa_policy(saidx
)
714 struct secasindex
*saidx
;
716 struct secashead
*sah
;
717 struct secasvar
*sav
;
718 u_int stateidx
, state
;
720 LIST_FOREACH(sah
, &sahtree
, chain
) {
721 if (sah
->state
== SADB_SASTATE_DEAD
)
723 if (key_cmpsaidx_withmode(&sah
->saidx
, saidx
))
731 /* search valid state */
733 stateidx
< _ARRAYLEN(saorder_state_valid
);
736 state
= saorder_state_valid
[stateidx
];
738 sav
= key_do_allocsa_policy(sah
, state
);
747 * searching SAD with direction, protocol, mode and state.
748 * called by key_allocsa_policy().
751 * others : found, pointer to a SA.
753 static struct secasvar
*
754 key_do_allocsa_policy(sah
, state
)
755 struct secashead
*sah
;
758 struct secasvar
*sav
, *candidate
;
763 LIST_FOREACH(sav
, &sah
->savtree
[state
], chain
) {
766 KEY_CHKSASTATE(sav
->state
, state
, "key_do_allocsa_policy");
769 if (candidate
== NULL
) {
774 /* Which SA is the better ? */
777 if (candidate
->lft_c
== NULL
|| sav
->lft_c
== NULL
)
778 panic("key_do_allocsa_policy: "
779 "lifetime_current is NULL.\n");
781 /* XXX What the best method is to compare ? */
782 if (candidate
->lft_c
->sadb_lifetime_addtime
>
783 sav
->lft_c
->sadb_lifetime_addtime
) {
791 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
792 printf("DP allocsa_policy cause "
793 "refcnt++:%d SA:%p\n",
794 candidate
->refcnt
, candidate
));
800 * allocating a SA entry for a *INBOUND* packet.
801 * Must call key_freesav() later.
802 * OUT: positive: pointer to a sav.
803 * NULL: not found, or error occured.
805 * In the comparison, source address will be ignored for RFC2401 conformance.
806 * To quote, from section 4.1:
807 * A security association is uniquely identified by a triple consisting
808 * of a Security Parameter Index (SPI), an IP Destination Address, and a
809 * security protocol (AH or ESP) identifier.
810 * Note that, however, we do need to keep source address in IPsec SA.
811 * IKE specification and PF_KEY specification do assume that we
812 * keep source address in IPsec SA. We see a tricky situation here.
815 key_allocsa(family
, src
, dst
, proto
, spi
)
820 struct secashead
*sah
;
821 struct secasvar
*sav
;
822 u_int stateidx
, state
;
823 struct sockaddr_in sin
;
824 struct sockaddr_in6 sin6
;
828 if (src
== NULL
|| dst
== NULL
)
829 panic("key_allocsa: NULL pointer is passed.\n");
833 * XXX: to be checked internal IP header somewhere. Also when
834 * IPsec tunnel packet is received. But ESP tunnel mode is
835 * encrypted so we can't check internal IP header.
837 s
= splnet(); /*called from softclock()*/
838 LIST_FOREACH(sah
, &sahtree
, chain
) {
839 /* search valid state */
841 stateidx
< _ARRAYLEN(saorder_state_valid
);
843 state
= saorder_state_valid
[stateidx
];
844 LIST_FOREACH(sav
, &sah
->savtree
[state
], chain
) {
846 KEY_CHKSASTATE(sav
->state
, state
, "key_allocsav");
847 if (proto
!= sav
->sah
->saidx
.proto
)
851 if (family
!= sav
->sah
->saidx
.src
.ss_family
||
852 family
!= sav
->sah
->saidx
.dst
.ss_family
)
855 #if 0 /* don't check src */
856 /* check src address */
859 bzero(&sin
, sizeof(sin
));
860 sin
.sin_family
= AF_INET
;
861 sin
.sin_len
= sizeof(sin
);
862 bcopy(src
, &sin
.sin_addr
,
863 sizeof(sin
.sin_addr
));
864 if (key_sockaddrcmp((struct sockaddr
*)&sin
,
865 (struct sockaddr
*)&sav
->sah
->saidx
.src
, 0) != 0)
870 bzero(&sin6
, sizeof(sin6
));
871 sin6
.sin6_family
= AF_INET6
;
872 sin6
.sin6_len
= sizeof(sin6
);
873 bcopy(src
, &sin6
.sin6_addr
,
874 sizeof(sin6
.sin6_addr
));
875 if (IN6_IS_SCOPE_LINKLOCAL(&sin6
.sin6_addr
)) {
876 /* kame fake scopeid */
878 ntohs(sin6
.sin6_addr
.s6_addr16
[1]);
879 sin6
.sin6_addr
.s6_addr16
[1] = 0;
881 if (key_sockaddrcmp((struct sockaddr
*)&sin6
,
882 (struct sockaddr
*)&sav
->sah
->saidx
.src
, 0) != 0)
886 printf("key_allocsa: unknown address family=%d.\n",
892 /* check dst address */
895 bzero(&sin
, sizeof(sin
));
896 sin
.sin_family
= AF_INET
;
897 sin
.sin_len
= sizeof(sin
);
898 bcopy(dst
, &sin
.sin_addr
,
899 sizeof(sin
.sin_addr
));
900 if (key_sockaddrcmp((struct sockaddr
*)&sin
,
901 (struct sockaddr
*)&sav
->sah
->saidx
.dst
, 0) != 0)
906 bzero(&sin6
, sizeof(sin6
));
907 sin6
.sin6_family
= AF_INET6
;
908 sin6
.sin6_len
= sizeof(sin6
);
909 bcopy(dst
, &sin6
.sin6_addr
,
910 sizeof(sin6
.sin6_addr
));
911 if (IN6_IS_SCOPE_LINKLOCAL(&sin6
.sin6_addr
)) {
912 /* kame fake scopeid */
914 ntohs(sin6
.sin6_addr
.s6_addr16
[1]);
915 sin6
.sin6_addr
.s6_addr16
[1] = 0;
917 if (key_sockaddrcmp((struct sockaddr
*)&sin6
,
918 (struct sockaddr
*)&sav
->sah
->saidx
.dst
, 0) != 0)
922 printf("key_allocsa: unknown address family=%d.\n",
939 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
940 printf("DP allocsa cause refcnt++:%d SA:%p\n",
946 * Must be called after calling key_allocsp().
947 * For both the packet without socket and key_freeso().
951 struct secpolicy
*sp
;
955 panic("key_freesp: NULL pointer is passed.\n");
958 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
959 printf("DP freesp cause refcnt--:%d SP:%p\n",
969 * Must be called after calling key_allocsp().
970 * For the packet with socket.
978 panic("key_freeso: NULL pointer is passed.\n");
980 switch (so
->so_proto
->pr_domain
->dom_family
) {
984 struct inpcb
*pcb
= sotoinpcb(so
);
986 /* Does it have a PCB ? */
987 if (pcb
== NULL
|| pcb
->inp_sp
== NULL
)
989 key_freesp_so(&pcb
->inp_sp
->sp_in
);
990 key_freesp_so(&pcb
->inp_sp
->sp_out
);
998 struct inpcb
*pcb
= sotoinpcb(so
);
1000 /* Does it have a PCB ? */
1001 if (pcb
== NULL
|| pcb
->inp_sp
== NULL
)
1003 key_freesp_so(&pcb
->inp_sp
->sp_in
);
1004 key_freesp_so(&pcb
->inp_sp
->sp_out
);
1006 struct in6pcb
*pcb
= sotoin6pcb(so
);
1008 /* Does it have a PCB ? */
1009 if (pcb
== NULL
|| pcb
->in6p_sp
== NULL
)
1011 key_freesp_so(&pcb
->in6p_sp
->sp_in
);
1012 key_freesp_so(&pcb
->in6p_sp
->sp_out
);
1019 printf("key_freeso: unknown address family=%d.\n",
1020 so
->so_proto
->pr_domain
->dom_family
);
1030 struct secpolicy
**sp
;
1033 if (sp
== NULL
|| *sp
== NULL
)
1034 panic("key_freesp_so: sp == NULL\n");
1036 switch ((*sp
)->policy
) {
1037 case IPSEC_POLICY_IPSEC
:
1038 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1039 printf("DP freeso calls free SP:%p\n", *sp
));
1043 case IPSEC_POLICY_ENTRUST
:
1044 case IPSEC_POLICY_BYPASS
:
1047 panic("key_freesp_so: Invalid policy found %d", (*sp
)->policy
);
1054 * Must be called after calling key_allocsa().
1055 * This function is called by key_freesp() to free some SA allocated
1060 struct secasvar
*sav
;
1064 panic("key_freesav: NULL pointer is passed.\n");
1067 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1068 printf("DP freesav cause refcnt--:%d SA:%p SPI %u\n",
1069 sav
->refcnt
, sav
, (u_int32_t
)ntohl(sav
->spi
)));
1071 if (sav
->refcnt
== 0)
1077 /* %%% SPD management */
1079 * free security policy entry.
1083 struct secpolicy
*sp
;
1089 panic("key_delsp: NULL pointer is passed.\n");
1091 sp
->state
= IPSEC_SPSTATE_DEAD
;
1094 return; /* can't free */
1096 s
= splnet(); /*called from softclock()*/
1097 /* remove from SP index */
1098 if (__LIST_CHAINED(sp
))
1099 LIST_REMOVE(sp
, chain
);
1102 struct ipsecrequest
*isr
= sp
->req
, *nextisr
;
1104 while (isr
!= NULL
) {
1105 if (isr
->sav
!= NULL
) {
1106 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1107 printf("DP delsp calls free SA:%p\n",
1109 key_freesav(isr
->sav
);
1113 nextisr
= isr
->next
;
1119 keydb_delsecpolicy(sp
);
1128 * OUT: NULL : not found
1129 * others : found, pointer to a SP.
1131 static struct secpolicy
*
1133 struct secpolicyindex
*spidx
;
1135 struct secpolicy
*sp
;
1139 panic("key_getsp: NULL pointer is passed.\n");
1141 LIST_FOREACH(sp
, &sptree
[spidx
->dir
], chain
) {
1142 if (sp
->state
== IPSEC_SPSTATE_DEAD
)
1144 if (key_cmpspidx_exactly(spidx
, &sp
->spidx
)) {
1155 * OUT: NULL : not found
1156 * others : found, pointer to a SP.
1158 static struct secpolicy
*
1162 struct secpolicy
*sp
;
1164 LIST_FOREACH(sp
, &sptree
[IPSEC_DIR_INBOUND
], chain
) {
1165 if (sp
->state
== IPSEC_SPSTATE_DEAD
)
1173 LIST_FOREACH(sp
, &sptree
[IPSEC_DIR_OUTBOUND
], chain
) {
1174 if (sp
->state
== IPSEC_SPSTATE_DEAD
)
1188 struct secpolicy
*newsp
= NULL
;
1190 newsp
= keydb_newsecpolicy();
1201 * create secpolicy structure from sadb_x_policy structure.
1202 * NOTE: `state', `secpolicyindex' in secpolicy structure are not set,
1203 * so must be set properly later.
1206 key_msg2sp(xpl0
, len
, error
)
1207 struct sadb_x_policy
*xpl0
;
1211 struct secpolicy
*newsp
;
1215 panic("key_msg2sp: NULL pointer was passed.\n");
1216 if (len
< sizeof(*xpl0
))
1217 panic("key_msg2sp: invalid length.\n");
1218 if (len
!= PFKEY_EXTLEN(xpl0
)) {
1220 printf("key_msg2sp: Invalid msg length.\n");
1226 if ((newsp
= key_newsp()) == NULL
) {
1231 newsp
->spidx
.dir
= xpl0
->sadb_x_policy_dir
;
1232 newsp
->policy
= xpl0
->sadb_x_policy_type
;
1235 switch (xpl0
->sadb_x_policy_type
) {
1236 case IPSEC_POLICY_DISCARD
:
1237 case IPSEC_POLICY_NONE
:
1238 case IPSEC_POLICY_ENTRUST
:
1239 case IPSEC_POLICY_BYPASS
:
1243 case IPSEC_POLICY_IPSEC
:
1246 struct sadb_x_ipsecrequest
*xisr
;
1247 struct ipsecrequest
**p_isr
= &newsp
->req
;
1249 /* validity check */
1250 if (PFKEY_EXTLEN(xpl0
) < sizeof(*xpl0
)) {
1252 printf("key_msg2sp: Invalid msg length.\n");
1259 tlen
= PFKEY_EXTLEN(xpl0
) - sizeof(*xpl0
);
1260 xisr
= (struct sadb_x_ipsecrequest
*)(xpl0
+ 1);
1265 if (xisr
->sadb_x_ipsecrequest_len
< sizeof(*xisr
)) {
1267 printf("key_msg2sp: "
1268 "invalid ipsecrequest length.\n");
1275 /* allocate request buffer */
1276 KMALLOC(*p_isr
, struct ipsecrequest
*, sizeof(**p_isr
));
1277 if ((*p_isr
) == NULL
) {
1279 printf("key_msg2sp: No more memory.\n");
1285 bzero(*p_isr
, sizeof(**p_isr
));
1288 (*p_isr
)->next
= NULL
;
1290 switch (xisr
->sadb_x_ipsecrequest_proto
) {
1293 case IPPROTO_IPCOMP
:
1297 printf("key_msg2sp: invalid proto type=%u\n",
1298 xisr
->sadb_x_ipsecrequest_proto
);
1301 *error
= EPROTONOSUPPORT
;
1304 (*p_isr
)->saidx
.proto
= xisr
->sadb_x_ipsecrequest_proto
;
1306 switch (xisr
->sadb_x_ipsecrequest_mode
) {
1307 case IPSEC_MODE_TRANSPORT
:
1308 case IPSEC_MODE_TUNNEL
:
1310 case IPSEC_MODE_ANY
:
1313 printf("key_msg2sp: invalid mode=%u\n",
1314 xisr
->sadb_x_ipsecrequest_mode
);
1320 (*p_isr
)->saidx
.mode
= xisr
->sadb_x_ipsecrequest_mode
;
1322 switch (xisr
->sadb_x_ipsecrequest_level
) {
1323 case IPSEC_LEVEL_DEFAULT
:
1324 case IPSEC_LEVEL_USE
:
1325 case IPSEC_LEVEL_REQUIRE
:
1327 case IPSEC_LEVEL_UNIQUE
:
1328 /* validity check */
1330 * If range violation of reqid, kernel will
1331 * update it, don't refuse it.
1333 if (xisr
->sadb_x_ipsecrequest_reqid
1334 > IPSEC_MANUAL_REQID_MAX
) {
1336 printf("key_msg2sp: reqid=%d "
1338 "updated by kernel.\n",
1339 xisr
->sadb_x_ipsecrequest_reqid
);
1341 xisr
->sadb_x_ipsecrequest_reqid
= 0;
1344 /* allocate new reqid id if reqid is zero. */
1345 if (xisr
->sadb_x_ipsecrequest_reqid
== 0) {
1347 if ((reqid
= key_newreqid()) == 0) {
1352 (*p_isr
)->saidx
.reqid
= reqid
;
1353 xisr
->sadb_x_ipsecrequest_reqid
= reqid
;
1355 /* set it for manual keying. */
1356 (*p_isr
)->saidx
.reqid
=
1357 xisr
->sadb_x_ipsecrequest_reqid
;
1363 printf("key_msg2sp: invalid level=%u\n",
1364 xisr
->sadb_x_ipsecrequest_level
);
1370 (*p_isr
)->level
= xisr
->sadb_x_ipsecrequest_level
;
1372 /* set IP addresses if there */
1373 if (xisr
->sadb_x_ipsecrequest_len
> sizeof(*xisr
)) {
1374 struct sockaddr
*paddr
;
1376 paddr
= (struct sockaddr
*)(xisr
+ 1);
1378 /* validity check */
1380 > sizeof((*p_isr
)->saidx
.src
)) {
1382 printf("key_msg2sp: invalid request "
1383 "address length.\n");
1389 bcopy(paddr
, &(*p_isr
)->saidx
.src
,
1392 paddr
= (struct sockaddr
*)((caddr_t
)paddr
1395 /* validity check */
1397 > sizeof((*p_isr
)->saidx
.dst
)) {
1399 printf("key_msg2sp: invalid request "
1400 "address length.\n");
1406 bcopy(paddr
, &(*p_isr
)->saidx
.dst
,
1410 (*p_isr
)->sav
= NULL
;
1411 (*p_isr
)->sp
= newsp
;
1413 /* initialization for the next. */
1414 p_isr
= &(*p_isr
)->next
;
1415 tlen
-= xisr
->sadb_x_ipsecrequest_len
;
1417 /* validity check */
1420 printf("key_msg2sp: becoming tlen < 0.\n");
1427 xisr
= (struct sadb_x_ipsecrequest
*)((caddr_t
)xisr
1428 + xisr
->sadb_x_ipsecrequest_len
);
1434 printf("key_msg2sp: invalid policy type.\n");
1448 static u_int32_t auto_reqid
= IPSEC_MANUAL_REQID_MAX
+ 1;
1450 auto_reqid
= (auto_reqid
== ~0
1451 ? IPSEC_MANUAL_REQID_MAX
+ 1 : auto_reqid
+ 1);
1453 /* XXX should be unique check */
1459 * copy secpolicy struct to sadb_x_policy structure indicated.
1463 struct secpolicy
*sp
;
1465 struct sadb_x_policy
*xpl
;
1472 panic("key_sp2msg: NULL pointer was passed.\n");
1474 tlen
= key_getspreqmsglen(sp
);
1476 m
= key_alloc_mbuf(tlen
);
1477 if (!m
|| m
->m_next
) { /*XXX*/
1485 xpl
= mtod(m
, struct sadb_x_policy
*);
1488 xpl
->sadb_x_policy_len
= PFKEY_UNIT64(tlen
);
1489 xpl
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
1490 xpl
->sadb_x_policy_type
= sp
->policy
;
1491 xpl
->sadb_x_policy_dir
= sp
->spidx
.dir
;
1492 xpl
->sadb_x_policy_id
= sp
->id
;
1493 p
= (caddr_t
)xpl
+ sizeof(*xpl
);
1495 /* if is the policy for ipsec ? */
1496 if (sp
->policy
== IPSEC_POLICY_IPSEC
) {
1497 struct sadb_x_ipsecrequest
*xisr
;
1498 struct ipsecrequest
*isr
;
1500 for (isr
= sp
->req
; isr
!= NULL
; isr
= isr
->next
) {
1502 xisr
= (struct sadb_x_ipsecrequest
*)p
;
1504 xisr
->sadb_x_ipsecrequest_proto
= isr
->saidx
.proto
;
1505 xisr
->sadb_x_ipsecrequest_mode
= isr
->saidx
.mode
;
1506 xisr
->sadb_x_ipsecrequest_level
= isr
->level
;
1507 xisr
->sadb_x_ipsecrequest_reqid
= isr
->saidx
.reqid
;
1510 bcopy(&isr
->saidx
.src
, p
, isr
->saidx
.src
.ss_len
);
1511 p
+= isr
->saidx
.src
.ss_len
;
1512 bcopy(&isr
->saidx
.dst
, p
, isr
->saidx
.dst
.ss_len
);
1513 p
+= isr
->saidx
.src
.ss_len
;
1515 xisr
->sadb_x_ipsecrequest_len
=
1516 PFKEY_ALIGN8(sizeof(*xisr
)
1517 + isr
->saidx
.src
.ss_len
1518 + isr
->saidx
.dst
.ss_len
);
1525 /* m will not be freed nor modified */
1526 static struct mbuf
*
1527 key_gather_mbuf(struct mbuf
*m
, const struct sadb_msghdr
*mhp
,
1528 int ndeep
, int nitem
, int *items
)
1532 struct mbuf
*result
= NULL
, *n
;
1535 if (m
== NULL
|| mhp
== NULL
)
1536 panic("null pointer passed to key_gather");
1538 for (i
= 0; i
< nitem
; i
++) {
1540 if (idx
< 0 || idx
> SADB_EXT_MAX
)
1542 /* don't attempt to pull empty extension */
1543 if (idx
== SADB_EXT_RESERVED
&& mhp
->msg
== NULL
)
1545 if (idx
!= SADB_EXT_RESERVED
&&
1546 (mhp
->ext
[idx
] == NULL
|| mhp
->extlen
[idx
] == 0))
1549 if (idx
== SADB_EXT_RESERVED
) {
1550 len
= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
1553 panic("assumption failed");
1555 MGETHDR(n
, M_DONTWAIT
, MT_DATA
);
1560 m_copydata(m
, 0, sizeof(struct sadb_msg
),
1562 } else if (i
< ndeep
) {
1563 len
= mhp
->extlen
[idx
];
1564 n
= key_alloc_mbuf(len
);
1565 if (!n
|| n
->m_next
) { /*XXX*/
1570 m_copydata(m
, mhp
->extoff
[idx
], mhp
->extlen
[idx
],
1573 n
= m_copym(m
, mhp
->extoff
[idx
], mhp
->extlen
[idx
],
1585 if ((result
->m_flags
& M_PKTHDR
) != 0) {
1586 result
->m_pkthdr
.len
= 0;
1587 for (n
= result
; n
; n
= n
->m_next
)
1588 result
->m_pkthdr
.len
+= n
->m_len
;
1599 * SADB_X_SPDADD, SADB_X_SPDSETIDX or SADB_X_SPDUPDATE processing
1600 * add a entry to SP database, when received
1601 * <base, address(SD), (lifetime(H),) policy>
1603 * Adding to SP database,
1605 * <base, address(SD), (lifetime(H),) policy>
1606 * to the socket which was send.
1608 * SPDADD set a unique policy entry.
1609 * SPDSETIDX like SPDADD without a part of policy requests.
1610 * SPDUPDATE replace a unique policy entry.
1612 * m will always be freed.
1615 key_spdadd(so
, m
, mhp
)
1618 const struct sadb_msghdr
*mhp
;
1620 struct sadb_address
*src0
, *dst0
;
1621 struct sadb_x_policy
*xpl0
, *xpl
;
1622 struct sadb_lifetime
*lft
= NULL
;
1623 struct secpolicyindex spidx
;
1624 struct secpolicy
*newsp
;
1629 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
1630 panic("key_spdadd: NULL pointer is passed.\n");
1632 if (mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
1633 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
||
1634 mhp
->ext
[SADB_X_EXT_POLICY
] == NULL
) {
1636 printf("key_spdadd: invalid message is passed.\n");
1638 return key_senderror(so
, m
, EINVAL
);
1640 if (mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
1641 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
) ||
1642 mhp
->extlen
[SADB_X_EXT_POLICY
] < sizeof(struct sadb_x_policy
)) {
1644 printf("key_spdadd: invalid message is passed.\n");
1646 return key_senderror(so
, m
, EINVAL
);
1648 if (mhp
->ext
[SADB_EXT_LIFETIME_HARD
] != NULL
) {
1649 if (mhp
->extlen
[SADB_EXT_LIFETIME_HARD
]
1650 < sizeof(struct sadb_lifetime
)) {
1652 printf("key_spdadd: invalid message is passed.\n");
1654 return key_senderror(so
, m
, EINVAL
);
1656 lft
= (struct sadb_lifetime
*)mhp
->ext
[SADB_EXT_LIFETIME_HARD
];
1659 src0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_SRC
];
1660 dst0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_DST
];
1661 xpl0
= (struct sadb_x_policy
*)mhp
->ext
[SADB_X_EXT_POLICY
];
1664 /* XXX boundary check against sa_len */
1665 KEY_SETSECSPIDX(xpl0
->sadb_x_policy_dir
,
1668 src0
->sadb_address_prefixlen
,
1669 dst0
->sadb_address_prefixlen
,
1670 src0
->sadb_address_proto
,
1673 /* checking the direciton. */
1674 switch (xpl0
->sadb_x_policy_dir
) {
1675 case IPSEC_DIR_INBOUND
:
1676 case IPSEC_DIR_OUTBOUND
:
1680 printf("key_spdadd: Invalid SP direction.\n");
1682 mhp
->msg
->sadb_msg_errno
= EINVAL
;
1687 /* key_spdadd() accepts DISCARD, NONE and IPSEC. */
1688 if (xpl0
->sadb_x_policy_type
== IPSEC_POLICY_ENTRUST
1689 || xpl0
->sadb_x_policy_type
== IPSEC_POLICY_BYPASS
) {
1691 printf("key_spdadd: Invalid policy type.\n");
1693 return key_senderror(so
, m
, EINVAL
);
1696 /* policy requests are mandatory when action is ipsec. */
1697 if (mhp
->msg
->sadb_msg_type
!= SADB_X_SPDSETIDX
1698 && xpl0
->sadb_x_policy_type
== IPSEC_POLICY_IPSEC
1699 && mhp
->extlen
[SADB_X_EXT_POLICY
] <= sizeof(*xpl0
)) {
1701 printf("key_spdadd: some policy requests part required.\n");
1703 return key_senderror(so
, m
, EINVAL
);
1707 * checking there is SP already or not.
1708 * If type is SPDUPDATE and no SP found, then error.
1709 * If type is either SPDADD or SPDSETIDX and SP found, then error.
1711 newsp
= key_getsp(&spidx
);
1712 if (mhp
->msg
->sadb_msg_type
== SADB_X_SPDUPDATE
) {
1713 if (newsp
== NULL
) {
1715 printf("key_spdadd: no SP found.\n");
1717 return key_senderror(so
, m
, ENOENT
);
1720 newsp
->state
= IPSEC_SPSTATE_DEAD
;
1723 if (newsp
!= NULL
) {
1726 printf("key_spdadd: a SP entry exists already.\n");
1728 return key_senderror(so
, m
, EEXIST
);
1732 /* allocation new SP entry */
1733 if ((newsp
= key_msg2sp(xpl0
, PFKEY_EXTLEN(xpl0
), &error
)) == NULL
) {
1734 return key_senderror(so
, m
, error
);
1737 if ((newsp
->id
= key_getnewspid()) == 0) {
1738 keydb_delsecpolicy(newsp
);
1739 return key_senderror(so
, m
, ENOBUFS
);
1742 /* XXX boundary check against sa_len */
1743 KEY_SETSECSPIDX(xpl0
->sadb_x_policy_dir
,
1746 src0
->sadb_address_prefixlen
,
1747 dst0
->sadb_address_prefixlen
,
1748 src0
->sadb_address_proto
,
1751 /* sanity check on addr pair */
1752 if (((struct sockaddr
*)(src0
+ 1))->sa_family
!=
1753 ((struct sockaddr
*)(dst0
+ 1))->sa_family
) {
1754 keydb_delsecpolicy(newsp
);
1755 return key_senderror(so
, m
, EINVAL
);
1757 if (((struct sockaddr
*)(src0
+ 1))->sa_len
!=
1758 ((struct sockaddr
*)(dst0
+ 1))->sa_len
) {
1759 keydb_delsecpolicy(newsp
);
1760 return key_senderror(so
, m
, EINVAL
);
1763 if (newsp
->req
&& newsp
->req
->saidx
.src
.ss_family
) {
1764 struct sockaddr
*sa
;
1765 sa
= (struct sockaddr
*)(src0
+ 1);
1766 if (sa
->sa_family
!= newsp
->req
->saidx
.src
.ss_family
) {
1767 keydb_delsecpolicy(newsp
);
1768 return key_senderror(so
, m
, EINVAL
);
1771 if (newsp
->req
&& newsp
->req
->saidx
.dst
.ss_family
) {
1772 struct sockaddr
*sa
;
1773 sa
= (struct sockaddr
*)(dst0
+ 1);
1774 if (sa
->sa_family
!= newsp
->req
->saidx
.dst
.ss_family
) {
1775 keydb_delsecpolicy(newsp
);
1776 return key_senderror(so
, m
, EINVAL
);
1782 newsp
->created
= tv
.tv_sec
;
1783 newsp
->lastused
= tv
.tv_sec
;
1784 newsp
->lifetime
= lft
? lft
->sadb_lifetime_addtime
: 0;
1785 newsp
->validtime
= lft
? lft
->sadb_lifetime_usetime
: 0;
1787 newsp
->refcnt
= 1; /* do not reclaim until I say I do */
1788 newsp
->state
= IPSEC_SPSTATE_ALIVE
;
1789 LIST_INSERT_TAIL(&sptree
[newsp
->spidx
.dir
], newsp
, secpolicy
, chain
);
1791 /* Turn off the ipsec bypass */
1792 if (ipsec_bypass
!= 0)
1795 /* delete the entry in spacqtree */
1796 if (mhp
->msg
->sadb_msg_type
== SADB_X_SPDUPDATE
) {
1797 struct secspacq
*spacq
;
1798 if ((spacq
= key_getspacq(&spidx
)) != NULL
) {
1799 /* reset counter in order to deletion by timehandler. */
1801 spacq
->created
= tv
.tv_sec
;
1807 struct mbuf
*n
, *mpolicy
;
1808 struct sadb_msg
*newmsg
;
1811 /* create new sadb_msg to reply. */
1813 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_X_EXT_POLICY
,
1814 SADB_EXT_LIFETIME_HARD
, SADB_EXT_ADDRESS_SRC
,
1815 SADB_EXT_ADDRESS_DST
};
1816 n
= key_gather_mbuf(m
, mhp
, 2, sizeof(mbufItems
)/sizeof(int), mbufItems
);
1818 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_X_EXT_POLICY
,
1819 SADB_EXT_ADDRESS_SRC
, SADB_EXT_ADDRESS_DST
};
1820 n
= key_gather_mbuf(m
, mhp
, 2, sizeof(mbufItems
)/sizeof(int), mbufItems
);
1823 return key_senderror(so
, m
, ENOBUFS
);
1825 if (n
->m_len
< sizeof(*newmsg
)) {
1826 n
= m_pullup(n
, sizeof(*newmsg
));
1828 return key_senderror(so
, m
, ENOBUFS
);
1830 newmsg
= mtod(n
, struct sadb_msg
*);
1831 newmsg
->sadb_msg_errno
= 0;
1832 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
1835 mpolicy
= m_pulldown(n
, PFKEY_ALIGN8(sizeof(struct sadb_msg
)),
1836 sizeof(*xpl
), &off
);
1837 if (mpolicy
== NULL
) {
1838 /* n is already freed */
1839 return key_senderror(so
, m
, ENOBUFS
);
1841 xpl
= (struct sadb_x_policy
*)(mtod(mpolicy
, caddr_t
) + off
);
1842 if (xpl
->sadb_x_policy_exttype
!= SADB_X_EXT_POLICY
) {
1844 return key_senderror(so
, m
, EINVAL
);
1846 xpl
->sadb_x_policy_id
= newsp
->id
;
1849 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
1854 * get new policy id.
1862 u_int32_t newid
= 0;
1863 int count
= key_spi_trycnt
; /* XXX */
1864 struct secpolicy
*sp
;
1866 /* when requesting to allocate spi ranged */
1868 newid
= (policy_id
= (policy_id
== ~0 ? 1 : ++policy_id
));
1870 if ((sp
= key_getspbyid(newid
)) == NULL
)
1876 if (count
== 0 || newid
== 0) {
1878 printf("key_getnewspid: to allocate policy id is failed.\n");
1887 * SADB_SPDDELETE processing
1889 * <base, address(SD), policy(*)>
1890 * from the user(?), and set SADB_SASTATE_DEAD,
1892 * <base, address(SD), policy(*)>
1894 * policy(*) including direction of policy.
1896 * m will always be freed.
1899 key_spddelete(so
, m
, mhp
)
1902 const struct sadb_msghdr
*mhp
;
1904 struct sadb_address
*src0
, *dst0
;
1905 struct sadb_x_policy
*xpl0
;
1906 struct secpolicyindex spidx
;
1907 struct secpolicy
*sp
;
1910 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
1911 panic("key_spddelete: NULL pointer is passed.\n");
1913 if (mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
1914 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
||
1915 mhp
->ext
[SADB_X_EXT_POLICY
] == NULL
) {
1917 printf("key_spddelete: invalid message is passed.\n");
1919 return key_senderror(so
, m
, EINVAL
);
1921 if (mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
1922 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
) ||
1923 mhp
->extlen
[SADB_X_EXT_POLICY
] < sizeof(struct sadb_x_policy
)) {
1925 printf("key_spddelete: invalid message is passed.\n");
1927 return key_senderror(so
, m
, EINVAL
);
1930 src0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_SRC
];
1931 dst0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_DST
];
1932 xpl0
= (struct sadb_x_policy
*)mhp
->ext
[SADB_X_EXT_POLICY
];
1935 /* XXX boundary check against sa_len */
1936 KEY_SETSECSPIDX(xpl0
->sadb_x_policy_dir
,
1939 src0
->sadb_address_prefixlen
,
1940 dst0
->sadb_address_prefixlen
,
1941 src0
->sadb_address_proto
,
1944 /* checking the direciton. */
1945 switch (xpl0
->sadb_x_policy_dir
) {
1946 case IPSEC_DIR_INBOUND
:
1947 case IPSEC_DIR_OUTBOUND
:
1951 printf("key_spddelete: Invalid SP direction.\n");
1953 return key_senderror(so
, m
, EINVAL
);
1956 /* Is there SP in SPD ? */
1957 if ((sp
= key_getsp(&spidx
)) == NULL
) {
1959 printf("key_spddelete: no SP found.\n");
1961 return key_senderror(so
, m
, EINVAL
);
1964 /* save policy id to buffer to be returned. */
1965 xpl0
->sadb_x_policy_id
= sp
->id
;
1967 sp
->state
= IPSEC_SPSTATE_DEAD
;
1972 struct sadb_msg
*newmsg
;
1973 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_X_EXT_POLICY
,
1974 SADB_EXT_ADDRESS_SRC
, SADB_EXT_ADDRESS_DST
};
1976 /* create new sadb_msg to reply. */
1977 n
= key_gather_mbuf(m
, mhp
, 1, sizeof(mbufItems
)/sizeof(int), mbufItems
);
1979 return key_senderror(so
, m
, ENOBUFS
);
1981 newmsg
= mtod(n
, struct sadb_msg
*);
1982 newmsg
->sadb_msg_errno
= 0;
1983 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
1986 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
1991 * SADB_SPDDELETE2 processing
1994 * from the user(?), and set SADB_SASTATE_DEAD,
1998 * policy(*) including direction of policy.
2000 * m will always be freed.
2003 key_spddelete2(so
, m
, mhp
)
2006 const struct sadb_msghdr
*mhp
;
2009 struct secpolicy
*sp
;
2012 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
2013 panic("key_spddelete2: NULL pointer is passed.\n");
2015 if (mhp
->ext
[SADB_X_EXT_POLICY
] == NULL
||
2016 mhp
->extlen
[SADB_X_EXT_POLICY
] < sizeof(struct sadb_x_policy
)) {
2018 printf("key_spddelete2: invalid message is passed.\n");
2020 key_senderror(so
, m
, EINVAL
);
2024 id
= ((struct sadb_x_policy
*)mhp
->ext
[SADB_X_EXT_POLICY
])->sadb_x_policy_id
;
2026 /* Is there SP in SPD ? */
2027 if ((sp
= key_getspbyid(id
)) == NULL
) {
2029 printf("key_spddelete2: no SP found id:%u.\n", id
);
2031 key_senderror(so
, m
, EINVAL
);
2034 sp
->state
= IPSEC_SPSTATE_DEAD
;
2038 struct mbuf
*n
, *nn
;
2039 struct sadb_msg
*newmsg
;
2042 /* create new sadb_msg to reply. */
2043 len
= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
2046 return key_senderror(so
, m
, ENOBUFS
);
2047 MGETHDR(n
, M_DONTWAIT
, MT_DATA
);
2048 if (n
&& len
> MHLEN
) {
2049 MCLGET(n
, M_DONTWAIT
);
2050 if ((n
->m_flags
& M_EXT
) == 0) {
2056 return key_senderror(so
, m
, ENOBUFS
);
2062 m_copydata(m
, 0, sizeof(struct sadb_msg
), mtod(n
, caddr_t
) + off
);
2063 off
+= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
2067 panic("length inconsistency in key_spddelete2");
2070 n
->m_next
= m_copym(m
, mhp
->extoff
[SADB_X_EXT_POLICY
],
2071 mhp
->extlen
[SADB_X_EXT_POLICY
], M_DONTWAIT
);
2074 return key_senderror(so
, m
, ENOBUFS
);
2077 n
->m_pkthdr
.len
= 0;
2078 for (nn
= n
; nn
; nn
= nn
->m_next
)
2079 n
->m_pkthdr
.len
+= nn
->m_len
;
2081 newmsg
= mtod(n
, struct sadb_msg
*);
2082 newmsg
->sadb_msg_errno
= 0;
2083 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
2086 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
2091 * SADB_X_GET processing
2096 * <base, address(SD), policy>
2098 * policy(*) including direction of policy.
2100 * m will always be freed.
2103 key_spdget(so
, m
, mhp
)
2106 const struct sadb_msghdr
*mhp
;
2109 struct secpolicy
*sp
;
2113 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
2114 panic("key_spdget: NULL pointer is passed.\n");
2116 if (mhp
->ext
[SADB_X_EXT_POLICY
] == NULL
||
2117 mhp
->extlen
[SADB_X_EXT_POLICY
] < sizeof(struct sadb_x_policy
)) {
2119 printf("key_spdget: invalid message is passed.\n");
2121 return key_senderror(so
, m
, EINVAL
);
2124 id
= ((struct sadb_x_policy
*)mhp
->ext
[SADB_X_EXT_POLICY
])->sadb_x_policy_id
;
2126 /* Is there SP in SPD ? */
2127 if ((sp
= key_getspbyid(id
)) == NULL
) {
2129 printf("key_spdget: no SP found id:%u.\n", id
);
2131 return key_senderror(so
, m
, ENOENT
);
2134 n
= key_setdumpsp(sp
, SADB_X_SPDGET
, 0, mhp
->msg
->sadb_msg_pid
);
2137 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ONE
);
2139 return key_senderror(so
, m
, ENOBUFS
);
2143 * SADB_X_SPDACQUIRE processing.
2144 * Acquire policy and SA(s) for a *OUTBOUND* packet.
2147 * to KMD, and expect to receive
2148 * <base> with SADB_X_SPDACQUIRE if error occured,
2151 * with SADB_X_SPDUPDATE from KMD by PF_KEY.
2152 * policy(*) is without policy requests.
2155 * others: error number
2159 struct secpolicy
*sp
;
2161 struct mbuf
*result
= NULL
, *m
;
2162 struct secspacq
*newspacq
;
2167 panic("key_spdacquire: NULL pointer is passed.\n");
2168 if (sp
->req
!= NULL
)
2169 panic("key_spdacquire: called but there is request.\n");
2170 if (sp
->policy
!= IPSEC_POLICY_IPSEC
)
2171 panic("key_spdacquire: policy mismathed. IPsec is expected.\n");
2173 /* get a entry to check whether sent message or not. */
2174 if ((newspacq
= key_getspacq(&sp
->spidx
)) != NULL
) {
2175 if (key_blockacq_count
< newspacq
->count
) {
2176 /* reset counter and do send message. */
2177 newspacq
->count
= 0;
2179 /* increment counter and do nothing. */
2184 /* make new entry for blocking to send SADB_ACQUIRE. */
2185 if ((newspacq
= key_newspacq(&sp
->spidx
)) == NULL
)
2188 /* add to acqtree */
2189 LIST_INSERT_HEAD(&spacqtree
, newspacq
, chain
);
2192 /* create new sadb_msg to reply. */
2193 m
= key_setsadbmsg(SADB_X_SPDACQUIRE
, 0, 0, 0, 0, 0);
2200 result
->m_pkthdr
.len
= 0;
2201 for (m
= result
; m
; m
= m
->m_next
)
2202 result
->m_pkthdr
.len
+= m
->m_len
;
2204 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
2205 PFKEY_UNIT64(result
->m_pkthdr
.len
);
2207 return key_sendup_mbuf(NULL
, m
, KEY_SENDUP_REGISTERED
);
2216 * SADB_SPDFLUSH processing
2219 * from the user, and free all entries in secpctree.
2223 * NOTE: what to do is only marking SADB_SASTATE_DEAD.
2225 * m will always be freed.
2228 key_spdflush(so
, m
, mhp
)
2231 const struct sadb_msghdr
*mhp
;
2233 struct sadb_msg
*newmsg
;
2234 struct secpolicy
*sp
;
2238 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
2239 panic("key_spdflush: NULL pointer is passed.\n");
2241 if (m
->m_len
!= PFKEY_ALIGN8(sizeof(struct sadb_msg
)))
2242 return key_senderror(so
, m
, EINVAL
);
2244 for (dir
= 0; dir
< IPSEC_DIR_MAX
; dir
++) {
2245 LIST_FOREACH(sp
, &sptree
[dir
], chain
) {
2246 sp
->state
= IPSEC_SPSTATE_DEAD
;
2250 if (sizeof(struct sadb_msg
) > m
->m_len
+ M_TRAILINGSPACE(m
)) {
2252 printf("key_spdflush: No more memory.\n");
2254 return key_senderror(so
, m
, ENOBUFS
);
2260 m
->m_pkthdr
.len
= m
->m_len
= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
2261 newmsg
= mtod(m
, struct sadb_msg
*);
2262 newmsg
->sadb_msg_errno
= 0;
2263 newmsg
->sadb_msg_len
= PFKEY_UNIT64(m
->m_pkthdr
.len
);
2265 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ALL
);
2269 * SADB_SPDDUMP processing
2272 * from the user, and dump all SP leaves
2277 * m will always be freed.
2280 key_spddump(so
, m
, mhp
)
2283 const struct sadb_msghdr
*mhp
;
2285 struct secpolicy
*sp
;
2291 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
2292 panic("key_spddump: NULL pointer is passed.\n");
2294 /* search SPD entry and get buffer size. */
2296 for (dir
= 0; dir
< IPSEC_DIR_MAX
; dir
++) {
2297 LIST_FOREACH(sp
, &sptree
[dir
], chain
) {
2303 return key_senderror(so
, m
, ENOENT
);
2305 for (dir
= 0; dir
< IPSEC_DIR_MAX
; dir
++) {
2306 LIST_FOREACH(sp
, &sptree
[dir
], chain
) {
2308 n
= key_setdumpsp(sp
, SADB_X_SPDDUMP
, cnt
,
2309 mhp
->msg
->sadb_msg_pid
);
2312 key_sendup_mbuf(so
, n
, KEY_SENDUP_ONE
);
2320 static struct mbuf
*
2321 key_setdumpsp(sp
, type
, seq
, pid
)
2322 struct secpolicy
*sp
;
2326 struct mbuf
*result
= NULL
, *m
;
2328 m
= key_setsadbmsg(type
, 0, SADB_SATYPE_UNSPEC
, seq
, pid
, sp
->refcnt
);
2333 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
2334 (struct sockaddr
*)&sp
->spidx
.src
, sp
->spidx
.prefs
,
2335 sp
->spidx
.ul_proto
);
2340 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
2341 (struct sockaddr
*)&sp
->spidx
.dst
, sp
->spidx
.prefd
,
2342 sp
->spidx
.ul_proto
);
2352 if ((result
->m_flags
& M_PKTHDR
) == 0)
2355 if (result
->m_len
< sizeof(struct sadb_msg
)) {
2356 result
= m_pullup(result
, sizeof(struct sadb_msg
));
2361 result
->m_pkthdr
.len
= 0;
2362 for (m
= result
; m
; m
= m
->m_next
)
2363 result
->m_pkthdr
.len
+= m
->m_len
;
2365 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
2366 PFKEY_UNIT64(result
->m_pkthdr
.len
);
2376 * get PFKEY message length for security policy and request.
2379 key_getspreqmsglen(sp
)
2380 struct secpolicy
*sp
;
2384 tlen
= sizeof(struct sadb_x_policy
);
2386 /* if is the policy for ipsec ? */
2387 if (sp
->policy
!= IPSEC_POLICY_IPSEC
)
2390 /* get length of ipsec requests */
2392 struct ipsecrequest
*isr
;
2395 for (isr
= sp
->req
; isr
!= NULL
; isr
= isr
->next
) {
2396 len
= sizeof(struct sadb_x_ipsecrequest
)
2397 + isr
->saidx
.src
.ss_len
2398 + isr
->saidx
.dst
.ss_len
;
2400 tlen
+= PFKEY_ALIGN8(len
);
2408 * SADB_SPDEXPIRE processing
2410 * <base, address(SD), lifetime(CH), policy>
2414 * others : error number
2418 struct secpolicy
*sp
;
2421 struct mbuf
*result
= NULL
, *m
;
2424 struct sadb_lifetime
*lt
;
2426 /* XXX: Why do we lock ? */
2427 s
= splnet(); /*called from softclock()*/
2431 panic("key_spdexpire: NULL pointer is passed.\n");
2433 /* set msg header */
2434 m
= key_setsadbmsg(SADB_X_SPDEXPIRE
, 0, 0, 0, 0, 0);
2441 /* create lifetime extension (current and hard) */
2442 len
= PFKEY_ALIGN8(sizeof(*lt
)) * 2;
2443 m
= key_alloc_mbuf(len
);
2444 if (!m
|| m
->m_next
) { /*XXX*/
2450 bzero(mtod(m
, caddr_t
), len
);
2451 lt
= mtod(m
, struct sadb_lifetime
*);
2452 lt
->sadb_lifetime_len
= PFKEY_UNIT64(sizeof(struct sadb_lifetime
));
2453 lt
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
2454 lt
->sadb_lifetime_allocations
= 0;
2455 lt
->sadb_lifetime_bytes
= 0;
2456 lt
->sadb_lifetime_addtime
= sp
->created
;
2457 lt
->sadb_lifetime_usetime
= sp
->lastused
;
2458 lt
= (struct sadb_lifetime
*)(mtod(m
, caddr_t
) + len
/ 2);
2459 lt
->sadb_lifetime_len
= PFKEY_UNIT64(sizeof(struct sadb_lifetime
));
2460 lt
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_HARD
;
2461 lt
->sadb_lifetime_allocations
= 0;
2462 lt
->sadb_lifetime_bytes
= 0;
2463 lt
->sadb_lifetime_addtime
= sp
->lifetime
;
2464 lt
->sadb_lifetime_usetime
= sp
->validtime
;
2467 /* set sadb_address for source */
2468 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
2469 (struct sockaddr
*)&sp
->spidx
.src
,
2470 sp
->spidx
.prefs
, sp
->spidx
.ul_proto
);
2477 /* set sadb_address for destination */
2478 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
2479 (struct sockaddr
*)&sp
->spidx
.dst
,
2480 sp
->spidx
.prefd
, sp
->spidx
.ul_proto
);
2495 if ((result
->m_flags
& M_PKTHDR
) == 0) {
2500 if (result
->m_len
< sizeof(struct sadb_msg
)) {
2501 result
= m_pullup(result
, sizeof(struct sadb_msg
));
2502 if (result
== NULL
) {
2508 result
->m_pkthdr
.len
= 0;
2509 for (m
= result
; m
; m
= m
->m_next
)
2510 result
->m_pkthdr
.len
+= m
->m_len
;
2512 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
2513 PFKEY_UNIT64(result
->m_pkthdr
.len
);
2515 return key_sendup_mbuf(NULL
, result
, KEY_SENDUP_REGISTERED
);
2524 /* %%% SAD management */
2526 * allocating a memory for new SA head, and copy from the values of mhp.
2527 * OUT: NULL : failure due to the lack of memory.
2528 * others : pointer to new SA head.
2530 static struct secashead
*
2532 struct secasindex
*saidx
;
2534 struct secashead
*newsah
;
2538 panic("key_newsaidx: NULL pointer is passed.\n");
2540 newsah
= keydb_newsecashead();
2544 bcopy(saidx
, &newsah
->saidx
, sizeof(newsah
->saidx
));
2546 /* add to saidxtree */
2547 newsah
->state
= SADB_SASTATE_MATURE
;
2548 LIST_INSERT_HEAD(&sahtree
, newsah
, chain
);
2554 * delete SA index and all SA registerd.
2558 struct secashead
*sah
;
2560 struct secasvar
*sav
, *nextsav
;
2561 u_int stateidx
, state
;
2567 panic("key_delsah: NULL pointer is passed.\n");
2569 s
= splnet(); /*called from softclock()*/
2571 /* searching all SA registerd in the secindex. */
2573 stateidx
< _ARRAYLEN(saorder_state_any
);
2576 state
= saorder_state_any
[stateidx
];
2577 for (sav
= (struct secasvar
*)LIST_FIRST(&sah
->savtree
[state
]);
2581 nextsav
= LIST_NEXT(sav
, chain
);
2583 if (sav
->refcnt
> 0) {
2584 /* give up to delete this sa */
2590 KEY_CHKSASTATE(state
, sav
->state
, "key_delsah");
2594 /* remove back pointer */
2600 /* don't delete sah only if there are savs. */
2606 if (sah
->sa_route
.ro_rt
) {
2607 rtfree(sah
->sa_route
.ro_rt
);
2608 sah
->sa_route
.ro_rt
= (struct rtentry
*)NULL
;
2611 /* remove from tree of SA index */
2612 if (__LIST_CHAINED(sah
))
2613 LIST_REMOVE(sah
, chain
);
2622 * allocating a new SA with LARVAL state. key_add() and key_getspi() call,
2623 * and copy the values of mhp into new buffer.
2624 * When SAD message type is GETSPI:
2625 * to set sequence number from acq_seq++,
2626 * to set zero to SPI.
2627 * not to call key_setsava().
2629 * others : pointer to new secasvar.
2631 * does not modify mbuf. does not free mbuf on error.
2633 static struct secasvar
*
2634 key_newsav(m
, mhp
, sah
, errp
)
2636 const struct sadb_msghdr
*mhp
;
2637 struct secashead
*sah
;
2640 struct secasvar
*newsav
;
2641 const struct sadb_sa
*xsa
;
2644 if (m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
|| sah
== NULL
)
2645 panic("key_newsa: NULL pointer is passed.\n");
2647 KMALLOC(newsav
, struct secasvar
*, sizeof(struct secasvar
));
2648 if (newsav
== NULL
) {
2650 printf("key_newsa: No more memory.\n");
2655 bzero((caddr_t
)newsav
, sizeof(struct secasvar
));
2657 switch (mhp
->msg
->sadb_msg_type
) {
2661 #if IPSEC_DOSEQCHECK
2662 /* sync sequence number */
2663 if (mhp
->msg
->sadb_msg_seq
== 0)
2665 (acq_seq
= (acq_seq
== ~0 ? 1 : ++acq_seq
));
2668 newsav
->seq
= mhp
->msg
->sadb_msg_seq
;
2673 if (mhp
->ext
[SADB_EXT_SA
] == NULL
) {
2676 printf("key_newsa: invalid message is passed.\n");
2681 xsa
= (const struct sadb_sa
*)mhp
->ext
[SADB_EXT_SA
];
2682 newsav
->spi
= xsa
->sadb_sa_spi
;
2683 newsav
->seq
= mhp
->msg
->sadb_msg_seq
;
2691 /* copy sav values */
2692 if (mhp
->msg
->sadb_msg_type
!= SADB_GETSPI
) {
2693 *errp
= key_setsaval(newsav
, m
, mhp
);
2704 newsav
->created
= tv
.tv_sec
;
2707 newsav
->pid
= mhp
->msg
->sadb_msg_pid
;
2712 newsav
->state
= SADB_SASTATE_LARVAL
;
2713 LIST_INSERT_TAIL(&sah
->savtree
[SADB_SASTATE_LARVAL
], newsav
,
2720 * free() SA variable entry.
2724 struct secasvar
*sav
;
2728 panic("key_delsav: NULL pointer is passed.\n");
2730 if (sav
->refcnt
> 0)
2731 return; /* can't free */
2733 /* remove from SA header */
2734 if (__LIST_CHAINED(sav
))
2735 LIST_REMOVE(sav
, chain
);
2737 if (sav
->key_auth
!= NULL
) {
2738 bzero(_KEYBUF(sav
->key_auth
), _KEYLEN(sav
->key_auth
));
2739 KFREE(sav
->key_auth
);
2740 sav
->key_auth
= NULL
;
2742 if (sav
->key_enc
!= NULL
) {
2743 bzero(_KEYBUF(sav
->key_enc
), _KEYLEN(sav
->key_enc
));
2744 KFREE(sav
->key_enc
);
2745 sav
->key_enc
= NULL
;
2748 bzero(sav
->sched
, sav
->schedlen
);
2752 if (sav
->replay
!= NULL
) {
2753 keydb_delsecreplay(sav
->replay
);
2756 if (sav
->lft_c
!= NULL
) {
2760 if (sav
->lft_h
!= NULL
) {
2764 if (sav
->lft_s
!= NULL
) {
2768 if (sav
->iv
!= NULL
) {
2782 * others : found, pointer to a SA.
2784 static struct secashead
*
2786 struct secasindex
*saidx
;
2788 struct secashead
*sah
;
2790 LIST_FOREACH(sah
, &sahtree
, chain
) {
2791 if (sah
->state
== SADB_SASTATE_DEAD
)
2793 if (key_cmpsaidx_withoutmode2(&sah
->saidx
, saidx
))
2801 * check not to be duplicated SPI.
2802 * NOTE: this function is too slow due to searching all SAD.
2805 * others : found, pointer to a SA.
2807 static struct secasvar
*
2808 key_checkspidup(saidx
, spi
)
2809 struct secasindex
*saidx
;
2812 struct secashead
*sah
;
2813 struct secasvar
*sav
;
2815 /* check address family */
2816 if (saidx
->src
.ss_family
!= saidx
->dst
.ss_family
) {
2818 printf("key_checkspidup: address family mismatched.\n");
2824 LIST_FOREACH(sah
, &sahtree
, chain
) {
2825 if (!key_ismyaddr((struct sockaddr
*)&sah
->saidx
.dst
))
2827 sav
= key_getsavbyspi(sah
, spi
);
2836 * search SAD litmited alive SA, protocol, SPI.
2839 * others : found, pointer to a SA.
2841 static struct secasvar
*
2842 key_getsavbyspi(sah
, spi
)
2843 struct secashead
*sah
;
2846 struct secasvar
*sav
;
2847 u_int stateidx
, state
;
2849 /* search all status */
2851 stateidx
< _ARRAYLEN(saorder_state_alive
);
2854 state
= saorder_state_alive
[stateidx
];
2855 LIST_FOREACH(sav
, &sah
->savtree
[state
], chain
) {
2858 if (sav
->state
!= state
) {
2860 printf("key_getsavbyspi: "
2861 "invalid sav->state "
2862 "(queue: %d SA: %d)\n",
2868 if (sav
->spi
== spi
)
2877 * copy SA values from PF_KEY message except *SPI, SEQ, PID, STATE and TYPE*.
2878 * You must update these if need.
2882 * does not modify mbuf. does not free mbuf on error.
2885 key_setsaval(sav
, m
, mhp
)
2886 struct secasvar
*sav
;
2888 const struct sadb_msghdr
*mhp
;
2891 const struct esp_algorithm
*algo
;
2897 if (m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
2898 panic("key_setsaval: NULL pointer is passed.\n");
2900 /* initialization */
2902 sav
->key_auth
= NULL
;
2903 sav
->key_enc
= NULL
;
2912 if (mhp
->ext
[SADB_EXT_SA
] != NULL
) {
2913 const struct sadb_sa
*sa0
;
2915 sa0
= (const struct sadb_sa
*)mhp
->ext
[SADB_EXT_SA
];
2916 if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(*sa0
)) {
2921 sav
->alg_auth
= sa0
->sadb_sa_auth
;
2922 sav
->alg_enc
= sa0
->sadb_sa_encrypt
;
2923 sav
->flags
= sa0
->sadb_sa_flags
;
2926 if ((sa0
->sadb_sa_flags
& SADB_X_EXT_OLD
) == 0) {
2927 sav
->replay
= keydb_newsecreplay(sa0
->sadb_sa_replay
);
2928 if (sav
->replay
== NULL
) {
2930 printf("key_setsaval: No more memory.\n");
2938 /* Authentication keys */
2939 if (mhp
->ext
[SADB_EXT_KEY_AUTH
] != NULL
) {
2940 const struct sadb_key
*key0
;
2943 key0
= (const struct sadb_key
*)mhp
->ext
[SADB_EXT_KEY_AUTH
];
2944 len
= mhp
->extlen
[SADB_EXT_KEY_AUTH
];
2947 if (len
< sizeof(*key0
)) {
2951 switch (mhp
->msg
->sadb_msg_satype
) {
2952 case SADB_SATYPE_AH
:
2953 case SADB_SATYPE_ESP
:
2954 if (len
== PFKEY_ALIGN8(sizeof(struct sadb_key
)) &&
2955 sav
->alg_auth
!= SADB_X_AALG_NULL
)
2958 case SADB_X_SATYPE_IPCOMP
:
2965 printf("key_setsaval: invalid key_auth values.\n");
2970 sav
->key_auth
= (struct sadb_key
*)key_newbuf(key0
, len
);
2971 if (sav
->key_auth
== NULL
) {
2973 printf("key_setsaval: No more memory.\n");
2980 /* Encryption key */
2981 if (mhp
->ext
[SADB_EXT_KEY_ENCRYPT
] != NULL
) {
2982 const struct sadb_key
*key0
;
2985 key0
= (const struct sadb_key
*)mhp
->ext
[SADB_EXT_KEY_ENCRYPT
];
2986 len
= mhp
->extlen
[SADB_EXT_KEY_ENCRYPT
];
2989 if (len
< sizeof(*key0
)) {
2993 switch (mhp
->msg
->sadb_msg_satype
) {
2994 case SADB_SATYPE_ESP
:
2995 if (len
== PFKEY_ALIGN8(sizeof(struct sadb_key
)) &&
2996 sav
->alg_enc
!= SADB_EALG_NULL
) {
3000 sav
->key_enc
= (struct sadb_key
*)key_newbuf(key0
, len
);
3001 if (sav
->key_enc
== NULL
) {
3003 printf("key_setsaval: No more memory.\n");
3009 case SADB_X_SATYPE_IPCOMP
:
3010 if (len
!= PFKEY_ALIGN8(sizeof(struct sadb_key
)))
3012 sav
->key_enc
= NULL
; /*just in case*/
3014 case SADB_SATYPE_AH
:
3021 printf("key_setsatval: invalid key_enc value.\n");
3030 switch (mhp
->msg
->sadb_msg_satype
) {
3031 case SADB_SATYPE_ESP
:
3033 algo
= esp_algorithm_lookup(sav
->alg_enc
);
3034 if (algo
&& algo
->ivlen
)
3035 sav
->ivlen
= (*algo
->ivlen
)(algo
, sav
);
3036 if (sav
->ivlen
== 0)
3038 KMALLOC(sav
->iv
, caddr_t
, sav
->ivlen
);
3041 printf("key_setsaval: No more memory.\n");
3048 key_randomfill(sav
->iv
, sav
->ivlen
);
3051 case SADB_SATYPE_AH
:
3052 case SADB_X_SATYPE_IPCOMP
:
3056 printf("key_setsaval: invalid SA type.\n");
3064 sav
->created
= tv
.tv_sec
;
3066 /* make lifetime for CURRENT */
3067 KMALLOC(sav
->lft_c
, struct sadb_lifetime
*,
3068 sizeof(struct sadb_lifetime
));
3069 if (sav
->lft_c
== NULL
) {
3071 printf("key_setsaval: No more memory.\n");
3079 sav
->lft_c
->sadb_lifetime_len
=
3080 PFKEY_UNIT64(sizeof(struct sadb_lifetime
));
3081 sav
->lft_c
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
3082 sav
->lft_c
->sadb_lifetime_allocations
= 0;
3083 sav
->lft_c
->sadb_lifetime_bytes
= 0;
3084 sav
->lft_c
->sadb_lifetime_addtime
= tv
.tv_sec
;
3085 sav
->lft_c
->sadb_lifetime_usetime
= 0;
3087 /* lifetimes for HARD and SOFT */
3089 const struct sadb_lifetime
*lft0
;
3091 lft0
= (struct sadb_lifetime
*)mhp
->ext
[SADB_EXT_LIFETIME_HARD
];
3093 if (mhp
->extlen
[SADB_EXT_LIFETIME_HARD
] < sizeof(*lft0
)) {
3097 sav
->lft_h
= (struct sadb_lifetime
*)key_newbuf(lft0
,
3099 if (sav
->lft_h
== NULL
) {
3101 printf("key_setsaval: No more memory.\n");
3106 /* to be initialize ? */
3109 lft0
= (struct sadb_lifetime
*)mhp
->ext
[SADB_EXT_LIFETIME_SOFT
];
3111 if (mhp
->extlen
[SADB_EXT_LIFETIME_SOFT
] < sizeof(*lft0
)) {
3115 sav
->lft_s
= (struct sadb_lifetime
*)key_newbuf(lft0
,
3117 if (sav
->lft_s
== NULL
) {
3119 printf("key_setsaval: No more memory.\n");
3124 /* to be initialize ? */
3131 /* initialization */
3132 if (sav
->replay
!= NULL
) {
3133 keydb_delsecreplay(sav
->replay
);
3136 if (sav
->key_auth
!= NULL
) {
3137 KFREE(sav
->key_auth
);
3138 sav
->key_auth
= NULL
;
3140 if (sav
->key_enc
!= NULL
) {
3141 KFREE(sav
->key_enc
);
3142 sav
->key_enc
= NULL
;
3148 if (sav
->iv
!= NULL
) {
3152 if (sav
->lft_c
!= NULL
) {
3156 if (sav
->lft_h
!= NULL
) {
3160 if (sav
->lft_s
!= NULL
) {
3169 * validation with a secasvar entry, and set SADB_SATYPE_MATURE.
3175 struct secasvar
*sav
;
3178 int checkmask
= 0; /* 2^0: ealg 2^1: aalg 2^2: calg */
3179 int mustmask
= 0; /* 2^0: ealg 2^1: aalg 2^2: calg */
3183 /* check SPI value */
3184 switch (sav
->sah
->saidx
.proto
) {
3187 if (ntohl(sav
->spi
) >= 0 && ntohl(sav
->spi
) <= 255) {
3189 printf("key_mature: illegal range of SPI %u.\n",
3190 (u_int32_t
)ntohl(sav
->spi
));
3198 switch (sav
->sah
->saidx
.proto
) {
3201 if ((sav
->flags
& SADB_X_EXT_OLD
)
3202 && (sav
->flags
& SADB_X_EXT_DERIV
)) {
3204 printf("key_mature: "
3205 "invalid flag (derived) given to old-esp.\n");
3209 if (sav
->alg_auth
== SADB_AALG_NONE
)
3217 if (sav
->flags
& SADB_X_EXT_DERIV
) {
3219 printf("key_mature: "
3220 "invalid flag (derived) given to AH SA.\n");
3224 if (sav
->alg_enc
!= SADB_EALG_NONE
) {
3226 printf("key_mature: "
3227 "protocol and algorithm mismated.\n");
3234 case IPPROTO_IPCOMP
:
3235 if (sav
->alg_auth
!= SADB_AALG_NONE
) {
3237 printf("key_mature: "
3238 "protocol and algorithm mismated.\n");
3242 if ((sav
->flags
& SADB_X_EXT_RAWCPI
) == 0
3243 && ntohl(sav
->spi
) >= 0x10000) {
3245 printf("key_mature: invalid cpi for IPComp.\n");
3254 printf("key_mature: Invalid satype.\n");
3256 return EPROTONOSUPPORT
;
3259 /* check authentication algorithm */
3260 if ((checkmask
& 2) != 0) {
3261 const struct ah_algorithm
*algo
;
3264 algo
= ah_algorithm_lookup(sav
->alg_auth
);
3267 printf("key_mature: "
3268 "unknown authentication algorithm.\n");
3273 /* algorithm-dependent check */
3275 keylen
= sav
->key_auth
->sadb_key_bits
;
3278 if (keylen
< algo
->keymin
|| algo
->keymax
< keylen
) {
3280 printf("key_mature: invalid AH key length %d "
3281 "(%d-%d allowed)\n", keylen
,
3282 algo
->keymin
, algo
->keymax
);
3288 if ((*algo
->mature
)(sav
)) {
3289 /* message generated in per-algorithm function*/
3292 mature
= SADB_SATYPE_AH
;
3295 if ((mustmask
& 2) != 0 && mature
!= SADB_SATYPE_AH
) {
3297 printf("key_mature: no satisfy algorithm for AH\n");
3303 /* check encryption algorithm */
3304 if ((checkmask
& 1) != 0) {
3306 const struct esp_algorithm
*algo
;
3309 algo
= esp_algorithm_lookup(sav
->alg_enc
);
3312 printf("key_mature: unknown encryption algorithm.\n");
3317 /* algorithm-dependent check */
3319 keylen
= sav
->key_enc
->sadb_key_bits
;
3322 if (keylen
< algo
->keymin
|| algo
->keymax
< keylen
) {
3324 printf("key_mature: invalid ESP key length %d "
3325 "(%d-%d allowed)\n", keylen
,
3326 algo
->keymin
, algo
->keymax
);
3332 if ((*algo
->mature
)(sav
)) {
3333 /* message generated in per-algorithm function*/
3336 mature
= SADB_SATYPE_ESP
;
3339 if ((mustmask
& 1) != 0 && mature
!= SADB_SATYPE_ESP
) {
3341 printf("key_mature: no satisfy algorithm for ESP\n");
3347 printf("key_mature: ESP not supported in this configuration\n");
3353 /* check compression algorithm */
3354 if ((checkmask
& 4) != 0) {
3355 const struct ipcomp_algorithm
*algo
;
3357 /* algorithm-dependent check */
3358 algo
= ipcomp_algorithm_lookup(sav
->alg_enc
);
3361 printf("key_mature: unknown compression algorithm.\n");
3367 key_sa_chgstate(sav
, SADB_SASTATE_MATURE
);
3373 * subroutine for SADB_GET and SADB_DUMP.
3375 static struct mbuf
*
3376 key_setdumpsa(sav
, type
, satype
, seq
, pid
)
3377 struct secasvar
*sav
;
3378 u_int8_t type
, satype
;
3381 struct mbuf
*result
= NULL
, *tres
= NULL
, *m
;
3386 SADB_EXT_SA
, SADB_X_EXT_SA2
,
3387 SADB_EXT_LIFETIME_HARD
, SADB_EXT_LIFETIME_SOFT
,
3388 SADB_EXT_LIFETIME_CURRENT
, SADB_EXT_ADDRESS_SRC
,
3389 SADB_EXT_ADDRESS_DST
, SADB_EXT_ADDRESS_PROXY
, SADB_EXT_KEY_AUTH
,
3390 SADB_EXT_KEY_ENCRYPT
, SADB_EXT_IDENTITY_SRC
,
3391 SADB_EXT_IDENTITY_DST
, SADB_EXT_SENSITIVITY
,
3394 m
= key_setsadbmsg(type
, 0, satype
, seq
, pid
, sav
->refcnt
);
3399 for (i
= sizeof(dumporder
)/sizeof(dumporder
[0]) - 1; i
>= 0; i
--) {
3402 switch (dumporder
[i
]) {
3404 m
= key_setsadbsa(sav
);
3409 case SADB_X_EXT_SA2
:
3410 m
= key_setsadbxsa2(sav
->sah
->saidx
.mode
,
3411 sav
->sah
->saidx
.reqid
);
3416 case SADB_EXT_ADDRESS_SRC
:
3417 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
3418 (struct sockaddr
*)&sav
->sah
->saidx
.src
,
3419 sav
->sah
->saidx
.src
.ss_len
<< 3, IPSEC_ULPROTO_ANY
);
3424 case SADB_EXT_ADDRESS_DST
:
3425 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
3426 (struct sockaddr
*)&sav
->sah
->saidx
.dst
,
3427 sav
->sah
->saidx
.dst
.ss_len
<< 3, IPSEC_ULPROTO_ANY
);
3432 case SADB_EXT_KEY_AUTH
:
3435 l
= PFKEY_UNUNIT64(sav
->key_auth
->sadb_key_len
);
3439 case SADB_EXT_KEY_ENCRYPT
:
3442 l
= PFKEY_UNUNIT64(sav
->key_enc
->sadb_key_len
);
3446 case SADB_EXT_LIFETIME_CURRENT
:
3449 l
= PFKEY_UNUNIT64(((struct sadb_ext
*)sav
->lft_c
)->sadb_ext_len
);
3453 case SADB_EXT_LIFETIME_HARD
:
3456 l
= PFKEY_UNUNIT64(((struct sadb_ext
*)sav
->lft_h
)->sadb_ext_len
);
3460 case SADB_EXT_LIFETIME_SOFT
:
3463 l
= PFKEY_UNUNIT64(((struct sadb_ext
*)sav
->lft_s
)->sadb_ext_len
);
3467 case SADB_EXT_ADDRESS_PROXY
:
3468 case SADB_EXT_IDENTITY_SRC
:
3469 case SADB_EXT_IDENTITY_DST
:
3470 /* XXX: should we brought from SPD ? */
3471 case SADB_EXT_SENSITIVITY
:
3476 if ((!m
&& !p
) || (m
&& p
))
3479 M_PREPEND(tres
, l
, M_DONTWAIT
);
3482 bcopy(p
, mtod(tres
, caddr_t
), l
);
3486 m
= key_alloc_mbuf(l
);
3489 m_copyback(m
, 0, l
, p
);
3497 m_cat(result
, tres
);
3499 if (result
->m_len
< sizeof(struct sadb_msg
)) {
3500 result
= m_pullup(result
, sizeof(struct sadb_msg
));
3505 result
->m_pkthdr
.len
= 0;
3506 for (m
= result
; m
; m
= m
->m_next
)
3507 result
->m_pkthdr
.len
+= m
->m_len
;
3509 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
3510 PFKEY_UNIT64(result
->m_pkthdr
.len
);
3521 * set data into sadb_msg.
3523 static struct mbuf
*
3524 key_setsadbmsg(type
, tlen
, satype
, seq
, pid
, reserved
)
3525 u_int8_t type
, satype
;
3535 len
= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
3538 MGETHDR(m
, M_DONTWAIT
, MT_DATA
);
3539 if (m
&& len
> MHLEN
) {
3540 MCLGET(m
, M_DONTWAIT
);
3541 if ((m
->m_flags
& M_EXT
) == 0) {
3548 m
->m_pkthdr
.len
= m
->m_len
= len
;
3551 p
= mtod(m
, struct sadb_msg
*);
3554 p
->sadb_msg_version
= PF_KEY_V2
;
3555 p
->sadb_msg_type
= type
;
3556 p
->sadb_msg_errno
= 0;
3557 p
->sadb_msg_satype
= satype
;
3558 p
->sadb_msg_len
= PFKEY_UNIT64(tlen
);
3559 p
->sadb_msg_reserved
= reserved
;
3560 p
->sadb_msg_seq
= seq
;
3561 p
->sadb_msg_pid
= (u_int32_t
)pid
;
3567 * copy secasvar data into sadb_address.
3569 static struct mbuf
*
3571 struct secasvar
*sav
;
3577 len
= PFKEY_ALIGN8(sizeof(struct sadb_sa
));
3578 m
= key_alloc_mbuf(len
);
3579 if (!m
|| m
->m_next
) { /*XXX*/
3585 p
= mtod(m
, struct sadb_sa
*);
3588 p
->sadb_sa_len
= PFKEY_UNIT64(len
);
3589 p
->sadb_sa_exttype
= SADB_EXT_SA
;
3590 p
->sadb_sa_spi
= sav
->spi
;
3591 p
->sadb_sa_replay
= (sav
->replay
!= NULL
? sav
->replay
->wsize
: 0);
3592 p
->sadb_sa_state
= sav
->state
;
3593 p
->sadb_sa_auth
= sav
->alg_auth
;
3594 p
->sadb_sa_encrypt
= sav
->alg_enc
;
3595 p
->sadb_sa_flags
= sav
->flags
;
3601 * set data into sadb_address.
3603 static struct mbuf
*
3604 key_setsadbaddr(exttype
, saddr
, prefixlen
, ul_proto
)
3606 struct sockaddr
*saddr
;
3611 struct sadb_address
*p
;
3614 len
= PFKEY_ALIGN8(sizeof(struct sadb_address
)) +
3615 PFKEY_ALIGN8(saddr
->sa_len
);
3616 m
= key_alloc_mbuf(len
);
3617 if (!m
|| m
->m_next
) { /*XXX*/
3623 p
= mtod(m
, struct sadb_address
*);
3626 p
->sadb_address_len
= PFKEY_UNIT64(len
);
3627 p
->sadb_address_exttype
= exttype
;
3628 p
->sadb_address_proto
= ul_proto
;
3629 p
->sadb_address_prefixlen
= prefixlen
;
3630 p
->sadb_address_reserved
= 0;
3633 mtod(m
, caddr_t
) + PFKEY_ALIGN8(sizeof(struct sadb_address
)),
3641 * set data into sadb_ident.
3643 static struct mbuf
*
3644 key_setsadbident(exttype
, idtype
, string
, stringlen
, id
)
3645 u_int16_t exttype
, idtype
;
3651 struct sadb_ident
*p
;
3654 len
= PFKEY_ALIGN8(sizeof(struct sadb_ident
)) + PFKEY_ALIGN8(stringlen
);
3655 m
= key_alloc_mbuf(len
);
3656 if (!m
|| m
->m_next
) { /*XXX*/
3662 p
= mtod(m
, struct sadb_ident
*);
3665 p
->sadb_ident_len
= PFKEY_UNIT64(len
);
3666 p
->sadb_ident_exttype
= exttype
;
3667 p
->sadb_ident_type
= idtype
;
3668 p
->sadb_ident_reserved
= 0;
3669 p
->sadb_ident_id
= id
;
3672 mtod(m
, caddr_t
) + PFKEY_ALIGN8(sizeof(struct sadb_ident
)),
3680 * set data into sadb_x_sa2.
3682 static struct mbuf
*
3683 key_setsadbxsa2(mode
, reqid
)
3688 struct sadb_x_sa2
*p
;
3691 len
= PFKEY_ALIGN8(sizeof(struct sadb_x_sa2
));
3692 m
= key_alloc_mbuf(len
);
3693 if (!m
|| m
->m_next
) { /*XXX*/
3699 p
= mtod(m
, struct sadb_x_sa2
*);
3702 p
->sadb_x_sa2_len
= PFKEY_UNIT64(len
);
3703 p
->sadb_x_sa2_exttype
= SADB_X_EXT_SA2
;
3704 p
->sadb_x_sa2_mode
= mode
;
3705 p
->sadb_x_sa2_reserved1
= 0;
3706 p
->sadb_x_sa2_reserved2
= 0;
3707 p
->sadb_x_sa2_reserved3
= 0;
3708 p
->sadb_x_sa2_reqid
= reqid
;
3714 * set data into sadb_x_policy
3716 static struct mbuf
*
3717 key_setsadbxpolicy(type
, dir
, id
)
3723 struct sadb_x_policy
*p
;
3726 len
= PFKEY_ALIGN8(sizeof(struct sadb_x_policy
));
3727 m
= key_alloc_mbuf(len
);
3728 if (!m
|| m
->m_next
) { /*XXX*/
3734 p
= mtod(m
, struct sadb_x_policy
*);
3737 p
->sadb_x_policy_len
= PFKEY_UNIT64(len
);
3738 p
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
3739 p
->sadb_x_policy_type
= type
;
3740 p
->sadb_x_policy_dir
= dir
;
3741 p
->sadb_x_policy_id
= id
;
3748 * copy a buffer into the new buffer allocated.
3751 key_newbuf(src
, len
)
3757 KMALLOC(new, caddr_t
, len
);
3760 printf("key_newbuf: No more memory.\n");
3764 bcopy(src
, new, len
);
3769 /* compare my own address
3770 * OUT: 1: true, i.e. my address.
3775 struct sockaddr
*sa
;
3778 struct sockaddr_in
*sin
;
3779 struct in_ifaddr
*ia
;
3784 panic("key_ismyaddr: NULL pointer is passed.\n");
3786 switch (sa
->sa_family
) {
3789 sin
= (struct sockaddr_in
*)sa
;
3790 for (ia
= in_ifaddrhead
.tqh_first
; ia
;
3791 ia
= ia
->ia_link
.tqe_next
)
3793 if (sin
->sin_family
== ia
->ia_addr
.sin_family
&&
3794 sin
->sin_len
== ia
->ia_addr
.sin_len
&&
3795 sin
->sin_addr
.s_addr
== ia
->ia_addr
.sin_addr
.s_addr
)
3804 return key_ismyaddr6((struct sockaddr_in6
*)sa
);
3813 * compare my own address for IPv6.
3816 * NOTE: derived ip6_input() in KAME. This is necessary to modify more.
3818 #include <netinet6/in6_var.h>
3822 struct sockaddr_in6
*sin6
;
3824 struct in6_ifaddr
*ia
;
3825 struct in6_multi
*in6m
;
3827 for (ia
= in6_ifaddr
; ia
; ia
= ia
->ia_next
) {
3828 if (key_sockaddrcmp((struct sockaddr
*)&sin6
,
3829 (struct sockaddr
*)&ia
->ia_addr
, 0) == 0)
3834 * XXX why do we care about multlicast here while we don't care
3835 * about IPv4 multicast??
3839 IN6_LOOKUP_MULTI(sin6
->sin6_addr
, ia
->ia_ifp
, in6m
);
3844 /* loopback, just for safety */
3845 if (IN6_IS_ADDR_LOOPBACK(&sin6
->sin6_addr
))
3853 * compare two secasindex structure exactly.
3855 * saidx0: source, it can be in SAD.
3862 key_cmpsaidx_exactly(saidx0
, saidx1
)
3863 struct secasindex
*saidx0
, *saidx1
;
3866 if (saidx0
== NULL
&& saidx1
== NULL
)
3869 if (saidx0
== NULL
|| saidx1
== NULL
)
3872 if (saidx0
->proto
!= saidx1
->proto
3873 || saidx0
->mode
!= saidx1
->mode
3874 || saidx0
->reqid
!= saidx1
->reqid
)
3877 if (bcmp(&saidx0
->src
, &saidx1
->src
, saidx0
->src
.ss_len
) != 0 ||
3878 bcmp(&saidx0
->dst
, &saidx1
->dst
, saidx0
->dst
.ss_len
) != 0)
3885 * compare two secasindex structure with consideration mode.
3886 * don't compare port.
3888 * saidx0: source, it is often in SAD.
3889 * saidx1: object, it is often from SPD.
3895 key_cmpsaidx_withmode(saidx0
, saidx1
)
3896 struct secasindex
*saidx0
, *saidx1
;
3899 if (saidx0
== NULL
&& saidx1
== NULL
)
3902 if (saidx0
== NULL
|| saidx1
== NULL
)
3905 if (saidx0
->proto
!= saidx1
->proto
)
3909 * If reqid of SPD is non-zero, unique SA is required.
3910 * The result must be of same reqid in this case.
3912 if (saidx1
->reqid
!= 0 && saidx0
->reqid
!= saidx1
->reqid
)
3915 if (saidx0
->mode
!= IPSEC_MODE_ANY
&& saidx0
->mode
!= saidx1
->mode
)
3918 if (key_sockaddrcmp((struct sockaddr
*)&saidx0
->src
,
3919 (struct sockaddr
*)&saidx1
->src
, 0) != 0) {
3922 if (key_sockaddrcmp((struct sockaddr
*)&saidx0
->dst
,
3923 (struct sockaddr
*)&saidx1
->dst
, 0) != 0) {
3931 * compare two secasindex structure without mode, but think reqid.
3932 * don't compare port.
3934 * saidx0: source, it is often in SAD.
3935 * saidx1: object, it is often from user.
3941 key_cmpsaidx_withoutmode2(saidx0
, saidx1
)
3942 struct secasindex
*saidx0
, *saidx1
;
3945 if (saidx0
== NULL
&& saidx1
== NULL
)
3948 if (saidx0
== NULL
|| saidx1
== NULL
)
3951 if (saidx0
->proto
!= saidx1
->proto
)
3955 * If reqid of SPD is non-zero, unique SA is required.
3956 * The result must be of same reqid in this case.
3958 if (saidx1
->reqid
!= 0 && saidx0
->reqid
!= saidx1
->reqid
)
3961 if (key_sockaddrcmp((struct sockaddr
*)&saidx0
->src
,
3962 (struct sockaddr
*)&saidx1
->src
, 0) != 0) {
3965 if (key_sockaddrcmp((struct sockaddr
*)&saidx0
->dst
,
3966 (struct sockaddr
*)&saidx1
->dst
, 0) != 0) {
3974 * compare two secasindex structure without both mode and reqid.
3975 * don't compare port.
3977 * saidx0: source, it is often in SAD.
3978 * saidx1: object, it is often from user.
3984 key_cmpsaidx_withoutmode(saidx0
, saidx1
)
3985 struct secasindex
*saidx0
, *saidx1
;
3988 if (saidx0
== NULL
&& saidx1
== NULL
)
3991 if (saidx0
== NULL
|| saidx1
== NULL
)
3994 if (saidx0
->proto
!= saidx1
->proto
)
3997 if (key_sockaddrcmp((struct sockaddr
*)&saidx0
->src
,
3998 (struct sockaddr
*)&saidx1
->src
, 0) != 0) {
4001 if (key_sockaddrcmp((struct sockaddr
*)&saidx0
->dst
,
4002 (struct sockaddr
*)&saidx1
->dst
, 0) != 0) {
4010 * compare two secindex structure exactly.
4012 * spidx0: source, it is often in SPD.
4013 * spidx1: object, it is often from PFKEY message.
4019 key_cmpspidx_exactly(spidx0
, spidx1
)
4020 struct secpolicyindex
*spidx0
, *spidx1
;
4023 if (spidx0
== NULL
&& spidx1
== NULL
)
4026 if (spidx0
== NULL
|| spidx1
== NULL
)
4029 if (spidx0
->prefs
!= spidx1
->prefs
4030 || spidx0
->prefd
!= spidx1
->prefd
4031 || spidx0
->ul_proto
!= spidx1
->ul_proto
)
4034 if (key_sockaddrcmp((struct sockaddr
*)&spidx0
->src
,
4035 (struct sockaddr
*)&spidx1
->src
, 1) != 0) {
4038 if (key_sockaddrcmp((struct sockaddr
*)&spidx0
->dst
,
4039 (struct sockaddr
*)&spidx1
->dst
, 1) != 0) {
4047 * compare two secindex structure with mask.
4049 * spidx0: source, it is often in SPD.
4050 * spidx1: object, it is often from IP header.
4056 key_cmpspidx_withmask(spidx0
, spidx1
)
4057 struct secpolicyindex
*spidx0
, *spidx1
;
4060 if (spidx0
== NULL
&& spidx1
== NULL
)
4063 if (spidx0
== NULL
|| spidx1
== NULL
)
4066 if (spidx0
->src
.ss_family
!= spidx1
->src
.ss_family
||
4067 spidx0
->dst
.ss_family
!= spidx1
->dst
.ss_family
||
4068 spidx0
->src
.ss_len
!= spidx1
->src
.ss_len
||
4069 spidx0
->dst
.ss_len
!= spidx1
->dst
.ss_len
)
4072 /* if spidx.ul_proto == IPSEC_ULPROTO_ANY, ignore. */
4073 if (spidx0
->ul_proto
!= (u_int16_t
)IPSEC_ULPROTO_ANY
4074 && spidx0
->ul_proto
!= spidx1
->ul_proto
)
4077 switch (spidx0
->src
.ss_family
) {
4079 if (satosin(&spidx0
->src
)->sin_port
!= IPSEC_PORT_ANY
4080 && satosin(&spidx0
->src
)->sin_port
!=
4081 satosin(&spidx1
->src
)->sin_port
)
4083 if (!key_bbcmp((caddr_t
)&satosin(&spidx0
->src
)->sin_addr
,
4084 (caddr_t
)&satosin(&spidx1
->src
)->sin_addr
, spidx0
->prefs
))
4088 if (satosin6(&spidx0
->src
)->sin6_port
!= IPSEC_PORT_ANY
4089 && satosin6(&spidx0
->src
)->sin6_port
!=
4090 satosin6(&spidx1
->src
)->sin6_port
)
4093 * scope_id check. if sin6_scope_id is 0, we regard it
4094 * as a wildcard scope, which matches any scope zone ID.
4096 if (satosin6(&spidx0
->src
)->sin6_scope_id
&&
4097 satosin6(&spidx1
->src
)->sin6_scope_id
&&
4098 satosin6(&spidx0
->src
)->sin6_scope_id
!=
4099 satosin6(&spidx1
->src
)->sin6_scope_id
)
4101 if (!key_bbcmp((caddr_t
)&satosin6(&spidx0
->src
)->sin6_addr
,
4102 (caddr_t
)&satosin6(&spidx1
->src
)->sin6_addr
, spidx0
->prefs
))
4107 if (bcmp(&spidx0
->src
, &spidx1
->src
, spidx0
->src
.ss_len
) != 0)
4112 switch (spidx0
->dst
.ss_family
) {
4114 if (satosin(&spidx0
->dst
)->sin_port
!= IPSEC_PORT_ANY
4115 && satosin(&spidx0
->dst
)->sin_port
!=
4116 satosin(&spidx1
->dst
)->sin_port
)
4118 if (!key_bbcmp((caddr_t
)&satosin(&spidx0
->dst
)->sin_addr
,
4119 (caddr_t
)&satosin(&spidx1
->dst
)->sin_addr
, spidx0
->prefd
))
4123 if (satosin6(&spidx0
->dst
)->sin6_port
!= IPSEC_PORT_ANY
4124 && satosin6(&spidx0
->dst
)->sin6_port
!=
4125 satosin6(&spidx1
->dst
)->sin6_port
)
4128 * scope_id check. if sin6_scope_id is 0, we regard it
4129 * as a wildcard scope, which matches any scope zone ID.
4131 if (satosin6(&spidx0
->src
)->sin6_scope_id
&&
4132 satosin6(&spidx1
->src
)->sin6_scope_id
&&
4133 satosin6(&spidx0
->dst
)->sin6_scope_id
!=
4134 satosin6(&spidx1
->dst
)->sin6_scope_id
)
4136 if (!key_bbcmp((caddr_t
)&satosin6(&spidx0
->dst
)->sin6_addr
,
4137 (caddr_t
)&satosin6(&spidx1
->dst
)->sin6_addr
, spidx0
->prefd
))
4142 if (bcmp(&spidx0
->dst
, &spidx1
->dst
, spidx0
->dst
.ss_len
) != 0)
4147 /* XXX Do we check other field ? e.g. flowinfo */
4152 /* returns 0 on match */
4154 key_sockaddrcmp(sa1
, sa2
, port
)
4155 struct sockaddr
*sa1
;
4156 struct sockaddr
*sa2
;
4159 if (sa1
->sa_family
!= sa2
->sa_family
|| sa1
->sa_len
!= sa2
->sa_len
)
4162 switch (sa1
->sa_family
) {
4164 if (sa1
->sa_len
!= sizeof(struct sockaddr_in
))
4166 if (satosin(sa1
)->sin_addr
.s_addr
!=
4167 satosin(sa2
)->sin_addr
.s_addr
) {
4170 if (port
&& satosin(sa1
)->sin_port
!= satosin(sa2
)->sin_port
)
4174 if (sa1
->sa_len
!= sizeof(struct sockaddr_in6
))
4175 return 1; /*EINVAL*/
4176 if (satosin6(sa1
)->sin6_scope_id
!=
4177 satosin6(sa2
)->sin6_scope_id
) {
4180 if (!IN6_ARE_ADDR_EQUAL(&satosin6(sa1
)->sin6_addr
,
4181 &satosin6(sa2
)->sin6_addr
)) {
4185 satosin6(sa1
)->sin6_port
!= satosin6(sa2
)->sin6_port
) {
4189 if (bcmp(sa1
, sa2
, sa1
->sa_len
) != 0)
4198 * compare two buffers with mask.
4202 * bits: Number of bits to compare
4208 key_bbcmp(p1
, p2
, bits
)
4214 /* XXX: This could be considerably faster if we compare a word
4215 * at a time, but it is complicated on LSB Endian machines */
4217 /* Handle null pointers */
4218 if (p1
== NULL
|| p2
== NULL
)
4228 mask
= ~((1<<(8-bits
))-1);
4229 if ((*p1
& mask
) != (*p2
& mask
))
4232 return 1; /* Match! */
4237 * scanning SPD and SAD to check status for each entries,
4238 * and do to remove or to expire.
4239 * XXX: year 2038 problem may remain.
4242 key_timehandler_funnel(void)
4245 boolean_t funnel_state
;
4246 funnel_state
= thread_funnel_set(network_flock
, TRUE
);
4250 (void) thread_funnel_set(network_flock
, FALSE
);
4255 key_timehandler(void)
4263 s
= splnet(); /*called from softclock()*/
4267 struct secpolicy
*sp
, *nextsp
;
4269 for (dir
= 0; dir
< IPSEC_DIR_MAX
; dir
++) {
4270 for (sp
= LIST_FIRST(&sptree
[dir
]);
4274 nextsp
= LIST_NEXT(sp
, chain
);
4276 if (sp
->state
== IPSEC_SPSTATE_DEAD
) {
4281 if (sp
->lifetime
== 0 && sp
->validtime
== 0)
4284 /* the deletion will occur next time */
4286 && tv
.tv_sec
- sp
->created
> sp
->lifetime
)
4288 && tv
.tv_sec
- sp
->lastused
> sp
->validtime
)) {
4289 sp
->state
= IPSEC_SPSTATE_DEAD
;
4299 struct secashead
*sah
, *nextsah
;
4300 struct secasvar
*sav
, *nextsav
;
4302 for (sah
= LIST_FIRST(&sahtree
);
4306 nextsah
= LIST_NEXT(sah
, chain
);
4308 /* if sah has been dead, then delete it and process next sah. */
4309 if (sah
->state
== SADB_SASTATE_DEAD
) {
4314 /* if LARVAL entry doesn't become MATURE, delete it. */
4315 for (sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_LARVAL
]);
4319 nextsav
= LIST_NEXT(sav
, chain
);
4321 if (tv
.tv_sec
- sav
->created
> key_larval_lifetime
) {
4327 * check MATURE entry to start to send expire message
4330 for (sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_MATURE
]);
4334 nextsav
= LIST_NEXT(sav
, chain
);
4336 /* we don't need to check. */
4337 if (sav
->lft_s
== NULL
)
4341 if (sav
->lft_c
== NULL
) {
4343 printf("key_timehandler: "
4344 "There is no CURRENT time, why?\n");
4349 /* check SOFT lifetime */
4350 if (sav
->lft_s
->sadb_lifetime_addtime
!= 0
4351 && tv
.tv_sec
- sav
->created
> sav
->lft_s
->sadb_lifetime_addtime
) {
4353 * check SA to be used whether or not.
4354 * when SA hasn't been used, delete it.
4356 if (sav
->lft_c
->sadb_lifetime_usetime
== 0) {
4357 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
4361 key_sa_chgstate(sav
, SADB_SASTATE_DYING
);
4363 * XXX If we keep to send expire
4364 * message in the status of
4365 * DYING. Do remove below code.
4370 /* check SOFT lifetime by bytes */
4372 * XXX I don't know the way to delete this SA
4373 * when new SA is installed. Caution when it's
4374 * installed too big lifetime by time.
4376 else if (sav
->lft_s
->sadb_lifetime_bytes
!= 0
4377 && sav
->lft_s
->sadb_lifetime_bytes
< sav
->lft_c
->sadb_lifetime_bytes
) {
4379 key_sa_chgstate(sav
, SADB_SASTATE_DYING
);
4381 * XXX If we keep to send expire
4382 * message in the status of
4383 * DYING. Do remove below code.
4389 /* check DYING entry to change status to DEAD. */
4390 for (sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_DYING
]);
4394 nextsav
= LIST_NEXT(sav
, chain
);
4396 /* we don't need to check. */
4397 if (sav
->lft_h
== NULL
)
4401 if (sav
->lft_c
== NULL
) {
4403 printf("key_timehandler: "
4404 "There is no CURRENT time, why?\n");
4409 if (sav
->lft_h
->sadb_lifetime_addtime
!= 0
4410 && tv
.tv_sec
- sav
->created
> sav
->lft_h
->sadb_lifetime_addtime
) {
4411 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
4415 #if 0 /* XXX Should we keep to send expire message until HARD lifetime ? */
4416 else if (sav
->lft_s
!= NULL
4417 && sav
->lft_s
->sadb_lifetime_addtime
!= 0
4418 && tv
.tv_sec
- sav
->created
> sav
->lft_s
->sadb_lifetime_addtime
) {
4420 * XXX: should be checked to be
4421 * installed the valid SA.
4425 * If there is no SA then sending
4431 /* check HARD lifetime by bytes */
4432 else if (sav
->lft_h
->sadb_lifetime_bytes
!= 0
4433 && sav
->lft_h
->sadb_lifetime_bytes
< sav
->lft_c
->sadb_lifetime_bytes
) {
4434 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
4440 /* delete entry in DEAD */
4441 for (sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_DEAD
]);
4445 nextsav
= LIST_NEXT(sav
, chain
);
4448 if (sav
->state
!= SADB_SASTATE_DEAD
) {
4450 printf("key_timehandler: "
4451 "invalid sav->state "
4452 "(queue: %d SA: %d): "
4454 SADB_SASTATE_DEAD
, sav
->state
);
4459 * do not call key_freesav() here.
4460 * sav should already be freed, and sav->refcnt
4461 * shows other references to sav
4462 * (such as from SPD).
4468 #ifndef IPSEC_NONBLOCK_ACQUIRE
4471 struct secacq
*acq
, *nextacq
;
4473 for (acq
= LIST_FIRST(&acqtree
);
4477 nextacq
= LIST_NEXT(acq
, chain
);
4479 if (tv
.tv_sec
- acq
->created
> key_blockacq_lifetime
4480 && __LIST_CHAINED(acq
)) {
4481 LIST_REMOVE(acq
, chain
);
4490 struct secspacq
*acq
, *nextacq
;
4492 for (acq
= LIST_FIRST(&spacqtree
);
4496 nextacq
= LIST_NEXT(acq
, chain
);
4498 if (tv
.tv_sec
- acq
->created
> key_blockacq_lifetime
4499 && __LIST_CHAINED(acq
)) {
4500 LIST_REMOVE(acq
, chain
);
4506 /* initialize random seed */
4507 if (key_tick_init_random
++ > key_int_random
) {
4508 key_tick_init_random
= 0;
4512 #ifndef IPSEC_DEBUG2
4513 /* do exchange to tick time !! */
4514 (void)timeout((void *)key_timehandler_funnel
, (void *)0, hz
);
4515 #endif /* IPSEC_DEBUG2 */
4522 * to initialize a seed for random()
4528 /* Our PRNG is based on Yarrow and doesn't need to be seeded */
4535 srandom(tv
.tv_usec
);
4546 key_randomfill(&value
, sizeof(value
));
4551 key_randomfill(p
, l
)
4557 static int warn
= 1;
4560 read_random(p
, (u_int
)l
);
4563 n
= (size_t)read_random(p
, (u_int
)l
);
4567 bcopy(&v
, (u_int8_t
*)p
+ n
,
4568 l
- n
< sizeof(v
) ? l
- n
: sizeof(v
));
4572 printf("WARNING: pseudo-random number generator "
4573 "used for IPsec processing\n");
4581 * map SADB_SATYPE_* to IPPROTO_*.
4582 * if satype == SADB_SATYPE then satype is mapped to ~0.
4584 * 0: invalid satype.
4587 key_satype2proto(satype
)
4591 case SADB_SATYPE_UNSPEC
:
4592 return IPSEC_PROTO_ANY
;
4593 case SADB_SATYPE_AH
:
4595 case SADB_SATYPE_ESP
:
4597 case SADB_X_SATYPE_IPCOMP
:
4598 return IPPROTO_IPCOMP
;
4607 * map IPPROTO_* to SADB_SATYPE_*
4609 * 0: invalid protocol type.
4612 key_proto2satype(proto
)
4617 return SADB_SATYPE_AH
;
4619 return SADB_SATYPE_ESP
;
4620 case IPPROTO_IPCOMP
:
4621 return SADB_X_SATYPE_IPCOMP
;
4631 * SADB_GETSPI processing is to receive
4632 * <base, (SA2), src address, dst address, (SPI range)>
4633 * from the IKMPd, to assign a unique spi value, to hang on the INBOUND
4634 * tree with the status of LARVAL, and send
4635 * <base, SA(*), address(SD)>
4638 * IN: mhp: pointer to the pointer to each header.
4639 * OUT: NULL if fail.
4640 * other if success, return pointer to the message to send.
4643 key_getspi(so
, m
, mhp
)
4646 const struct sadb_msghdr
*mhp
;
4648 struct sadb_address
*src0
, *dst0
;
4649 struct secasindex saidx
;
4650 struct secashead
*newsah
;
4651 struct secasvar
*newsav
;
4659 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
4660 panic("key_getspi: NULL pointer is passed.\n");
4662 if (mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
4663 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
) {
4665 printf("key_getspi: invalid message is passed.\n");
4667 return key_senderror(so
, m
, EINVAL
);
4669 if (mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
4670 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
4672 printf("key_getspi: invalid message is passed.\n");
4674 return key_senderror(so
, m
, EINVAL
);
4676 if (mhp
->ext
[SADB_X_EXT_SA2
] != NULL
) {
4677 mode
= ((struct sadb_x_sa2
*)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_mode
;
4678 reqid
= ((struct sadb_x_sa2
*)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_reqid
;
4680 mode
= IPSEC_MODE_ANY
;
4684 src0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_SRC
]);
4685 dst0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_DST
]);
4687 /* map satype to proto */
4688 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
4690 printf("key_getspi: invalid satype is passed.\n");
4692 return key_senderror(so
, m
, EINVAL
);
4695 /* make sure if port number is zero. */
4696 switch (((struct sockaddr
*)(src0
+ 1))->sa_family
) {
4698 if (((struct sockaddr
*)(src0
+ 1))->sa_len
!=
4699 sizeof(struct sockaddr_in
))
4700 return key_senderror(so
, m
, EINVAL
);
4701 ((struct sockaddr_in
*)(src0
+ 1))->sin_port
= 0;
4704 if (((struct sockaddr
*)(src0
+ 1))->sa_len
!=
4705 sizeof(struct sockaddr_in6
))
4706 return key_senderror(so
, m
, EINVAL
);
4707 ((struct sockaddr_in6
*)(src0
+ 1))->sin6_port
= 0;
4712 switch (((struct sockaddr
*)(dst0
+ 1))->sa_family
) {
4714 if (((struct sockaddr
*)(dst0
+ 1))->sa_len
!=
4715 sizeof(struct sockaddr_in
))
4716 return key_senderror(so
, m
, EINVAL
);
4717 ((struct sockaddr_in
*)(dst0
+ 1))->sin_port
= 0;
4720 if (((struct sockaddr
*)(dst0
+ 1))->sa_len
!=
4721 sizeof(struct sockaddr_in6
))
4722 return key_senderror(so
, m
, EINVAL
);
4723 ((struct sockaddr_in6
*)(dst0
+ 1))->sin6_port
= 0;
4729 /* XXX boundary check against sa_len */
4730 KEY_SETSECASIDX(proto
, mode
, reqid
, src0
+ 1, dst0
+ 1, &saidx
);
4732 /* SPI allocation */
4733 spi
= key_do_getnewspi((struct sadb_spirange
*)mhp
->ext
[SADB_EXT_SPIRANGE
],
4736 return key_senderror(so
, m
, EINVAL
);
4738 /* get a SA index */
4739 if ((newsah
= key_getsah(&saidx
)) == NULL
) {
4740 /* create a new SA index */
4741 if ((newsah
= key_newsah(&saidx
)) == NULL
) {
4743 printf("key_getspi: No more memory.\n");
4745 return key_senderror(so
, m
, ENOBUFS
);
4751 newsav
= key_newsav(m
, mhp
, newsah
, &error
);
4752 if (newsav
== NULL
) {
4753 /* XXX don't free new SA index allocated in above. */
4754 return key_senderror(so
, m
, error
);
4758 newsav
->spi
= htonl(spi
);
4760 #ifndef IPSEC_NONBLOCK_ACQUIRE
4761 /* delete the entry in acqtree */
4762 if (mhp
->msg
->sadb_msg_seq
!= 0) {
4764 if ((acq
= key_getacqbyseq(mhp
->msg
->sadb_msg_seq
)) != NULL
) {
4765 /* reset counter in order to deletion by timehandler. */
4768 acq
->created
= tv
.tv_sec
;
4775 struct mbuf
*n
, *nn
;
4776 struct sadb_sa
*m_sa
;
4777 struct sadb_msg
*newmsg
;
4780 /* create new sadb_msg to reply. */
4781 len
= PFKEY_ALIGN8(sizeof(struct sadb_msg
)) +
4782 PFKEY_ALIGN8(sizeof(struct sadb_sa
));
4784 return key_senderror(so
, m
, ENOBUFS
);
4786 MGETHDR(n
, M_DONTWAIT
, MT_DATA
);
4788 MCLGET(n
, M_DONTWAIT
);
4789 if ((n
->m_flags
& M_EXT
) == 0) {
4795 return key_senderror(so
, m
, ENOBUFS
);
4801 m_copydata(m
, 0, sizeof(struct sadb_msg
), mtod(n
, caddr_t
) + off
);
4802 off
+= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
4804 m_sa
= (struct sadb_sa
*)(mtod(n
, caddr_t
) + off
);
4805 m_sa
->sadb_sa_len
= PFKEY_UNIT64(sizeof(struct sadb_sa
));
4806 m_sa
->sadb_sa_exttype
= SADB_EXT_SA
;
4807 m_sa
->sadb_sa_spi
= htonl(spi
);
4808 off
+= PFKEY_ALIGN8(sizeof(struct sadb_sa
));
4812 panic("length inconsistency in key_getspi");
4815 int mbufItems
[] = {SADB_EXT_ADDRESS_SRC
, SADB_EXT_ADDRESS_DST
};
4816 n
->m_next
= key_gather_mbuf(m
, mhp
, 0, sizeof(mbufItems
)/sizeof(int), mbufItems
);
4819 return key_senderror(so
, m
, ENOBUFS
);
4823 if (n
->m_len
< sizeof(struct sadb_msg
)) {
4824 n
= m_pullup(n
, sizeof(struct sadb_msg
));
4826 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ONE
);
4829 n
->m_pkthdr
.len
= 0;
4830 for (nn
= n
; nn
; nn
= nn
->m_next
)
4831 n
->m_pkthdr
.len
+= nn
->m_len
;
4833 newmsg
= mtod(n
, struct sadb_msg
*);
4834 newmsg
->sadb_msg_seq
= newsav
->seq
;
4835 newmsg
->sadb_msg_errno
= 0;
4836 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
4839 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ONE
);
4844 * allocating new SPI
4845 * called by key_getspi().
4851 key_do_getnewspi(spirange
, saidx
)
4852 struct sadb_spirange
*spirange
;
4853 struct secasindex
*saidx
;
4857 int count
= key_spi_trycnt
;
4859 /* set spi range to allocate */
4860 if (spirange
!= NULL
) {
4861 min
= spirange
->sadb_spirange_min
;
4862 max
= spirange
->sadb_spirange_max
;
4864 min
= key_spi_minval
;
4865 max
= key_spi_maxval
;
4867 /* IPCOMP needs 2-byte SPI */
4868 if (saidx
->proto
== IPPROTO_IPCOMP
) {
4875 t
= min
; min
= max
; max
= t
;
4880 if (key_checkspidup(saidx
, min
) != NULL
) {
4882 printf("key_do_getnewspi: SPI %u exists already.\n", min
);
4887 count
--; /* taking one cost. */
4895 /* when requesting to allocate spi ranged */
4897 /* generate pseudo-random SPI value ranged. */
4898 newspi
= min
+ (key_random() % (max
- min
+ 1));
4900 if (key_checkspidup(saidx
, newspi
) == NULL
)
4904 if (count
== 0 || newspi
== 0) {
4906 printf("key_do_getnewspi: to allocate spi is failed.\n");
4913 keystat
.getspi_count
=
4914 (keystat
.getspi_count
+ key_spi_trycnt
- count
) / 2;
4920 * SADB_UPDATE processing
4922 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4923 * key(AE), (identity(SD),) (sensitivity)>
4924 * from the ikmpd, and update a secasvar entry whose status is SADB_SASTATE_LARVAL.
4926 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4927 * (identity(SD),) (sensitivity)>
4930 * m will always be freed.
4933 key_update(so
, m
, mhp
)
4936 const struct sadb_msghdr
*mhp
;
4938 struct sadb_sa
*sa0
;
4939 struct sadb_address
*src0
, *dst0
;
4940 struct secasindex saidx
;
4941 struct secashead
*sah
;
4942 struct secasvar
*sav
;
4949 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
4950 panic("key_update: NULL pointer is passed.\n");
4952 /* map satype to proto */
4953 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
4955 printf("key_update: invalid satype is passed.\n");
4957 return key_senderror(so
, m
, EINVAL
);
4960 if (mhp
->ext
[SADB_EXT_SA
] == NULL
||
4961 mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
4962 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
||
4963 (mhp
->msg
->sadb_msg_satype
== SADB_SATYPE_ESP
&&
4964 mhp
->ext
[SADB_EXT_KEY_ENCRYPT
] == NULL
) ||
4965 (mhp
->msg
->sadb_msg_satype
== SADB_SATYPE_AH
&&
4966 mhp
->ext
[SADB_EXT_KEY_AUTH
] == NULL
) ||
4967 (mhp
->ext
[SADB_EXT_LIFETIME_HARD
] != NULL
&&
4968 mhp
->ext
[SADB_EXT_LIFETIME_SOFT
] == NULL
) ||
4969 (mhp
->ext
[SADB_EXT_LIFETIME_HARD
] == NULL
&&
4970 mhp
->ext
[SADB_EXT_LIFETIME_SOFT
] != NULL
)) {
4972 printf("key_update: invalid message is passed.\n");
4974 return key_senderror(so
, m
, EINVAL
);
4976 if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(struct sadb_sa
) ||
4977 mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
4978 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
4980 printf("key_update: invalid message is passed.\n");
4982 return key_senderror(so
, m
, EINVAL
);
4984 if (mhp
->ext
[SADB_X_EXT_SA2
] != NULL
) {
4985 mode
= ((struct sadb_x_sa2
*)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_mode
;
4986 reqid
= ((struct sadb_x_sa2
*)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_reqid
;
4988 mode
= IPSEC_MODE_ANY
;
4991 /* XXX boundary checking for other extensions */
4993 sa0
= (struct sadb_sa
*)mhp
->ext
[SADB_EXT_SA
];
4994 src0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_SRC
]);
4995 dst0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_DST
]);
4997 /* XXX boundary check against sa_len */
4998 KEY_SETSECASIDX(proto
, mode
, reqid
, src0
+ 1, dst0
+ 1, &saidx
);
5000 /* get a SA header */
5001 if ((sah
= key_getsah(&saidx
)) == NULL
) {
5003 printf("key_update: no SA index found.\n");
5005 return key_senderror(so
, m
, ENOENT
);
5008 /* set spidx if there */
5010 error
= key_setident(sah
, m
, mhp
);
5012 return key_senderror(so
, m
, error
);
5014 /* find a SA with sequence number. */
5015 #if IPSEC_DOSEQCHECK
5016 if (mhp
->msg
->sadb_msg_seq
!= 0
5017 && (sav
= key_getsavbyseq(sah
, mhp
->msg
->sadb_msg_seq
)) == NULL
) {
5019 printf("key_update: no larval SA with sequence %u exists.\n",
5020 mhp
->msg
->sadb_msg_seq
);
5022 return key_senderror(so
, m
, ENOENT
);
5025 if ((sav
= key_getsavbyspi(sah
, sa0
->sadb_sa_spi
)) == NULL
) {
5027 printf("key_update: no such a SA found (spi:%u)\n",
5028 (u_int32_t
)ntohl(sa0
->sadb_sa_spi
));
5030 return key_senderror(so
, m
, EINVAL
);
5034 /* validity check */
5035 if (sav
->sah
->saidx
.proto
!= proto
) {
5037 printf("key_update: protocol mismatched (DB=%u param=%u)\n",
5038 sav
->sah
->saidx
.proto
, proto
);
5040 return key_senderror(so
, m
, EINVAL
);
5042 #if IPSEC_DOSEQCHECK
5043 if (sav
->spi
!= sa0
->sadb_sa_spi
) {
5045 printf("key_update: SPI mismatched (DB:%u param:%u)\n",
5046 (u_int32_t
)ntohl(sav
->spi
),
5047 (u_int32_t
)ntohl(sa0
->sadb_sa_spi
));
5049 return key_senderror(so
, m
, EINVAL
);
5052 if (sav
->pid
!= mhp
->msg
->sadb_msg_pid
) {
5054 printf("key_update: pid mismatched (DB:%u param:%u)\n",
5055 sav
->pid
, mhp
->msg
->sadb_msg_pid
);
5057 return key_senderror(so
, m
, EINVAL
);
5060 /* copy sav values */
5061 error
= key_setsaval(sav
, m
, mhp
);
5064 return key_senderror(so
, m
, error
);
5067 /* check SA values to be mature. */
5068 if ((mhp
->msg
->sadb_msg_errno
= key_mature(sav
)) != 0) {
5070 return key_senderror(so
, m
, 0);
5076 /* set msg buf from mhp */
5077 n
= key_getmsgbuf_x1(m
, mhp
);
5080 printf("key_update: No more memory.\n");
5082 return key_senderror(so
, m
, ENOBUFS
);
5086 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
5091 * search SAD with sequence for a SA which state is SADB_SASTATE_LARVAL.
5092 * only called by key_update().
5095 * others : found, pointer to a SA.
5097 #if IPSEC_DOSEQCHECK
5098 static struct secasvar
*
5099 key_getsavbyseq(sah
, seq
)
5100 struct secashead
*sah
;
5103 struct secasvar
*sav
;
5106 state
= SADB_SASTATE_LARVAL
;
5108 /* search SAD with sequence number ? */
5109 LIST_FOREACH(sav
, &sah
->savtree
[state
], chain
) {
5111 KEY_CHKSASTATE(state
, sav
->state
, "key_getsabyseq");
5113 if (sav
->seq
== seq
) {
5115 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
5116 printf("DP key_getsavbyseq cause "
5117 "refcnt++:%d SA:%p\n",
5128 * SADB_ADD processing
5129 * add a entry to SA database, when received
5130 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
5131 * key(AE), (identity(SD),) (sensitivity)>
5134 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
5135 * (identity(SD),) (sensitivity)>
5138 * IGNORE identity and sensitivity messages.
5140 * m will always be freed.
5146 const struct sadb_msghdr
*mhp
;
5148 struct sadb_sa
*sa0
;
5149 struct sadb_address
*src0
, *dst0
;
5150 struct secasindex saidx
;
5151 struct secashead
*newsah
;
5152 struct secasvar
*newsav
;
5159 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
5160 panic("key_add: NULL pointer is passed.\n");
5162 /* map satype to proto */
5163 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
5165 printf("key_add: invalid satype is passed.\n");
5167 return key_senderror(so
, m
, EINVAL
);
5170 if (mhp
->ext
[SADB_EXT_SA
] == NULL
||
5171 mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
5172 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
||
5173 (mhp
->msg
->sadb_msg_satype
== SADB_SATYPE_ESP
&&
5174 mhp
->ext
[SADB_EXT_KEY_ENCRYPT
] == NULL
) ||
5175 (mhp
->msg
->sadb_msg_satype
== SADB_SATYPE_AH
&&
5176 mhp
->ext
[SADB_EXT_KEY_AUTH
] == NULL
) ||
5177 (mhp
->ext
[SADB_EXT_LIFETIME_HARD
] != NULL
&&
5178 mhp
->ext
[SADB_EXT_LIFETIME_SOFT
] == NULL
) ||
5179 (mhp
->ext
[SADB_EXT_LIFETIME_HARD
] == NULL
&&
5180 mhp
->ext
[SADB_EXT_LIFETIME_SOFT
] != NULL
)) {
5182 printf("key_add: invalid message is passed.\n");
5184 return key_senderror(so
, m
, EINVAL
);
5186 if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(struct sadb_sa
) ||
5187 mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
5188 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
5191 printf("key_add: invalid message is passed.\n");
5193 return key_senderror(so
, m
, EINVAL
);
5195 if (mhp
->ext
[SADB_X_EXT_SA2
] != NULL
) {
5196 mode
= ((struct sadb_x_sa2
*)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_mode
;
5197 reqid
= ((struct sadb_x_sa2
*)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_reqid
;
5199 mode
= IPSEC_MODE_ANY
;
5203 sa0
= (struct sadb_sa
*)mhp
->ext
[SADB_EXT_SA
];
5204 src0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_SRC
];
5205 dst0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_DST
];
5207 /* XXX boundary check against sa_len */
5208 KEY_SETSECASIDX(proto
, mode
, reqid
, src0
+ 1, dst0
+ 1, &saidx
);
5210 /* get a SA header */
5211 if ((newsah
= key_getsah(&saidx
)) == NULL
) {
5212 /* create a new SA header */
5213 if ((newsah
= key_newsah(&saidx
)) == NULL
) {
5215 printf("key_add: No more memory.\n");
5217 return key_senderror(so
, m
, ENOBUFS
);
5221 /* set spidx if there */
5223 error
= key_setident(newsah
, m
, mhp
);
5225 return key_senderror(so
, m
, error
);
5228 /* create new SA entry. */
5229 /* We can create new SA only if SPI is differenct. */
5230 if (key_getsavbyspi(newsah
, sa0
->sadb_sa_spi
)) {
5232 printf("key_add: SA already exists.\n");
5234 return key_senderror(so
, m
, EEXIST
);
5236 newsav
= key_newsav(m
, mhp
, newsah
, &error
);
5237 if (newsav
== NULL
) {
5238 return key_senderror(so
, m
, error
);
5241 /* check SA values to be mature. */
5242 if ((error
= key_mature(newsav
)) != 0) {
5243 key_freesav(newsav
);
5244 return key_senderror(so
, m
, error
);
5248 * don't call key_freesav() here, as we would like to keep the SA
5249 * in the database on success.
5255 /* set msg buf from mhp */
5256 n
= key_getmsgbuf_x1(m
, mhp
);
5259 printf("key_update: No more memory.\n");
5261 return key_senderror(so
, m
, ENOBUFS
);
5265 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
5271 key_setident(sah
, m
, mhp
)
5272 struct secashead
*sah
;
5274 const struct sadb_msghdr
*mhp
;
5276 const struct sadb_ident
*idsrc
, *iddst
;
5277 int idsrclen
, iddstlen
;
5280 if (sah
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
5281 panic("key_setident: NULL pointer is passed.\n");
5283 /* don't make buffer if not there */
5284 if (mhp
->ext
[SADB_EXT_IDENTITY_SRC
] == NULL
&&
5285 mhp
->ext
[SADB_EXT_IDENTITY_DST
] == NULL
) {
5291 if (mhp
->ext
[SADB_EXT_IDENTITY_SRC
] == NULL
||
5292 mhp
->ext
[SADB_EXT_IDENTITY_DST
] == NULL
) {
5294 printf("key_setident: invalid identity.\n");
5299 idsrc
= (const struct sadb_ident
*)mhp
->ext
[SADB_EXT_IDENTITY_SRC
];
5300 iddst
= (const struct sadb_ident
*)mhp
->ext
[SADB_EXT_IDENTITY_DST
];
5301 idsrclen
= mhp
->extlen
[SADB_EXT_IDENTITY_SRC
];
5302 iddstlen
= mhp
->extlen
[SADB_EXT_IDENTITY_DST
];
5304 /* validity check */
5305 if (idsrc
->sadb_ident_type
!= iddst
->sadb_ident_type
) {
5307 printf("key_setident: ident type mismatch.\n");
5312 switch (idsrc
->sadb_ident_type
) {
5313 case SADB_IDENTTYPE_PREFIX
:
5314 case SADB_IDENTTYPE_FQDN
:
5315 case SADB_IDENTTYPE_USERFQDN
:
5317 /* XXX do nothing */
5323 /* make structure */
5324 KMALLOC(sah
->idents
, struct sadb_ident
*, idsrclen
);
5325 if (sah
->idents
== NULL
) {
5327 printf("key_setident: No more memory.\n");
5331 KMALLOC(sah
->identd
, struct sadb_ident
*, iddstlen
);
5332 if (sah
->identd
== NULL
) {
5336 printf("key_setident: No more memory.\n");
5340 bcopy(idsrc
, sah
->idents
, idsrclen
);
5341 bcopy(iddst
, sah
->identd
, iddstlen
);
5347 * m will not be freed on return.
5348 * it is caller's responsibility to free the result.
5350 static struct mbuf
*
5351 key_getmsgbuf_x1(m
, mhp
)
5353 const struct sadb_msghdr
*mhp
;
5356 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_EXT_SA
,
5357 SADB_X_EXT_SA2
, SADB_EXT_ADDRESS_SRC
,
5358 SADB_EXT_ADDRESS_DST
, SADB_EXT_LIFETIME_HARD
,
5359 SADB_EXT_LIFETIME_SOFT
, SADB_EXT_IDENTITY_SRC
,
5360 SADB_EXT_IDENTITY_DST
};
5363 if (m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
5364 panic("key_getmsgbuf_x1: NULL pointer is passed.\n");
5366 /* create new sadb_msg to reply. */
5367 n
= key_gather_mbuf(m
, mhp
, 1, sizeof(mbufItems
)/sizeof(int), mbufItems
);
5371 if (n
->m_len
< sizeof(struct sadb_msg
)) {
5372 n
= m_pullup(n
, sizeof(struct sadb_msg
));
5376 mtod(n
, struct sadb_msg
*)->sadb_msg_errno
= 0;
5377 mtod(n
, struct sadb_msg
*)->sadb_msg_len
=
5378 PFKEY_UNIT64(n
->m_pkthdr
.len
);
5383 static int key_delete_all
__P((struct socket
*, struct mbuf
*,
5384 const struct sadb_msghdr
*, u_int16_t
));
5387 * SADB_DELETE processing
5389 * <base, SA(*), address(SD)>
5390 * from the ikmpd, and set SADB_SASTATE_DEAD,
5392 * <base, SA(*), address(SD)>
5395 * m will always be freed.
5398 key_delete(so
, m
, mhp
)
5401 const struct sadb_msghdr
*mhp
;
5403 struct sadb_sa
*sa0
;
5404 struct sadb_address
*src0
, *dst0
;
5405 struct secasindex saidx
;
5406 struct secashead
*sah
;
5407 struct secasvar
*sav
= NULL
;
5411 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
5412 panic("key_delete: NULL pointer is passed.\n");
5414 /* map satype to proto */
5415 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
5417 printf("key_delete: invalid satype is passed.\n");
5419 return key_senderror(so
, m
, EINVAL
);
5422 if (mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
5423 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
) {
5425 printf("key_delete: invalid message is passed.\n");
5427 return key_senderror(so
, m
, EINVAL
);
5430 if (mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
5431 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
5433 printf("key_delete: invalid message is passed.\n");
5435 return key_senderror(so
, m
, EINVAL
);
5438 if (mhp
->ext
[SADB_EXT_SA
] == NULL
) {
5440 * Caller wants us to delete all non-LARVAL SAs
5441 * that match the src/dst. This is used during
5442 * IKE INITIAL-CONTACT.
5445 printf("key_delete: doing delete all.\n");
5447 return key_delete_all(so
, m
, mhp
, proto
);
5448 } else if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(struct sadb_sa
)) {
5450 printf("key_delete: invalid message is passed.\n");
5452 return key_senderror(so
, m
, EINVAL
);
5455 sa0
= (struct sadb_sa
*)mhp
->ext
[SADB_EXT_SA
];
5456 src0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_SRC
]);
5457 dst0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_DST
]);
5459 /* XXX boundary check against sa_len */
5460 KEY_SETSECASIDX(proto
, IPSEC_MODE_ANY
, 0, src0
+ 1, dst0
+ 1, &saidx
);
5462 /* get a SA header */
5463 LIST_FOREACH(sah
, &sahtree
, chain
) {
5464 if (sah
->state
== SADB_SASTATE_DEAD
)
5466 if (key_cmpsaidx_withoutmode(&sah
->saidx
, &saidx
) == 0)
5469 /* get a SA with SPI. */
5470 sav
= key_getsavbyspi(sah
, sa0
->sadb_sa_spi
);
5476 printf("key_delete: no SA found.\n");
5478 return key_senderror(so
, m
, ENOENT
);
5481 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
5487 struct sadb_msg
*newmsg
;
5488 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_EXT_SA
,
5489 SADB_EXT_ADDRESS_SRC
, SADB_EXT_ADDRESS_DST
};
5491 /* create new sadb_msg to reply. */
5492 n
= key_gather_mbuf(m
, mhp
, 1, sizeof(mbufItems
)/sizeof(int), mbufItems
);
5494 return key_senderror(so
, m
, ENOBUFS
);
5496 if (n
->m_len
< sizeof(struct sadb_msg
)) {
5497 n
= m_pullup(n
, sizeof(struct sadb_msg
));
5499 return key_senderror(so
, m
, ENOBUFS
);
5501 newmsg
= mtod(n
, struct sadb_msg
*);
5502 newmsg
->sadb_msg_errno
= 0;
5503 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
5506 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
5511 * delete all SAs for src/dst. Called from key_delete().
5514 key_delete_all(so
, m
, mhp
, proto
)
5517 const struct sadb_msghdr
*mhp
;
5520 struct sadb_address
*src0
, *dst0
;
5521 struct secasindex saidx
;
5522 struct secashead
*sah
;
5523 struct secasvar
*sav
, *nextsav
;
5524 u_int stateidx
, state
;
5526 src0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_SRC
]);
5527 dst0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_DST
]);
5529 /* XXX boundary check against sa_len */
5530 KEY_SETSECASIDX(proto
, IPSEC_MODE_ANY
, 0, src0
+ 1, dst0
+ 1, &saidx
);
5532 LIST_FOREACH(sah
, &sahtree
, chain
) {
5533 if (sah
->state
== SADB_SASTATE_DEAD
)
5535 if (key_cmpsaidx_withoutmode(&sah
->saidx
, &saidx
) == 0)
5538 /* Delete all non-LARVAL SAs. */
5540 stateidx
< _ARRAYLEN(saorder_state_alive
);
5542 state
= saorder_state_alive
[stateidx
];
5543 if (state
== SADB_SASTATE_LARVAL
)
5545 for (sav
= LIST_FIRST(&sah
->savtree
[state
]);
5546 sav
!= NULL
; sav
= nextsav
) {
5547 nextsav
= LIST_NEXT(sav
, chain
);
5549 if (sav
->state
!= state
) {
5551 printf("key_delete_all: "
5552 "invalid sav->state "
5553 "(queue: %d SA: %d)\n",
5559 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
5566 struct sadb_msg
*newmsg
;
5567 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_EXT_ADDRESS_SRC
,
5568 SADB_EXT_ADDRESS_DST
};
5570 /* create new sadb_msg to reply. */
5571 n
= key_gather_mbuf(m
, mhp
, 1, sizeof(mbufItems
)/sizeof(int), mbufItems
);
5573 return key_senderror(so
, m
, ENOBUFS
);
5575 if (n
->m_len
< sizeof(struct sadb_msg
)) {
5576 n
= m_pullup(n
, sizeof(struct sadb_msg
));
5578 return key_senderror(so
, m
, ENOBUFS
);
5580 newmsg
= mtod(n
, struct sadb_msg
*);
5581 newmsg
->sadb_msg_errno
= 0;
5582 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
5585 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
5590 * SADB_GET processing
5592 * <base, SA(*), address(SD)>
5593 * from the ikmpd, and get a SP and a SA to respond,
5595 * <base, SA, (lifetime(HSC),) address(SD), (address(P),) key(AE),
5596 * (identity(SD),) (sensitivity)>
5599 * m will always be freed.
5605 const struct sadb_msghdr
*mhp
;
5607 struct sadb_sa
*sa0
;
5608 struct sadb_address
*src0
, *dst0
;
5609 struct secasindex saidx
;
5610 struct secashead
*sah
;
5611 struct secasvar
*sav
= NULL
;
5615 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
5616 panic("key_get: NULL pointer is passed.\n");
5618 /* map satype to proto */
5619 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
5621 printf("key_get: invalid satype is passed.\n");
5623 return key_senderror(so
, m
, EINVAL
);
5626 if (mhp
->ext
[SADB_EXT_SA
] == NULL
||
5627 mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
5628 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
) {
5630 printf("key_get: invalid message is passed.\n");
5632 return key_senderror(so
, m
, EINVAL
);
5634 if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(struct sadb_sa
) ||
5635 mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
5636 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
5638 printf("key_get: invalid message is passed.\n");
5640 return key_senderror(so
, m
, EINVAL
);
5643 sa0
= (struct sadb_sa
*)mhp
->ext
[SADB_EXT_SA
];
5644 src0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_SRC
];
5645 dst0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_DST
];
5647 /* XXX boundary check against sa_len */
5648 KEY_SETSECASIDX(proto
, IPSEC_MODE_ANY
, 0, src0
+ 1, dst0
+ 1, &saidx
);
5650 /* get a SA header */
5651 LIST_FOREACH(sah
, &sahtree
, chain
) {
5652 if (sah
->state
== SADB_SASTATE_DEAD
)
5654 if (key_cmpsaidx_withoutmode(&sah
->saidx
, &saidx
) == 0)
5657 /* get a SA with SPI. */
5658 sav
= key_getsavbyspi(sah
, sa0
->sadb_sa_spi
);
5664 printf("key_get: no SA found.\n");
5666 return key_senderror(so
, m
, ENOENT
);
5673 /* map proto to satype */
5674 if ((satype
= key_proto2satype(sah
->saidx
.proto
)) == 0) {
5676 printf("key_get: there was invalid proto in SAD.\n");
5678 return key_senderror(so
, m
, EINVAL
);
5681 /* create new sadb_msg to reply. */
5682 n
= key_setdumpsa(sav
, SADB_GET
, satype
, mhp
->msg
->sadb_msg_seq
,
5683 mhp
->msg
->sadb_msg_pid
);
5685 return key_senderror(so
, m
, ENOBUFS
);
5688 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ONE
);
5692 /* XXX make it sysctl-configurable? */
5694 key_getcomb_setlifetime(comb
)
5695 struct sadb_comb
*comb
;
5698 comb
->sadb_comb_soft_allocations
= 1;
5699 comb
->sadb_comb_hard_allocations
= 1;
5700 comb
->sadb_comb_soft_bytes
= 0;
5701 comb
->sadb_comb_hard_bytes
= 0;
5702 comb
->sadb_comb_hard_addtime
= 86400; /* 1 day */
5703 comb
->sadb_comb_soft_addtime
= comb
->sadb_comb_soft_addtime
* 80 / 100;
5704 comb
->sadb_comb_soft_usetime
= 28800; /* 8 hours */
5705 comb
->sadb_comb_hard_usetime
= comb
->sadb_comb_hard_usetime
* 80 / 100;
5710 * XXX reorder combinations by preference
5711 * XXX no idea if the user wants ESP authentication or not
5713 static struct mbuf
*
5716 struct sadb_comb
*comb
;
5717 const struct esp_algorithm
*algo
;
5718 struct mbuf
*result
= NULL
, *m
, *n
;
5722 const int l
= PFKEY_ALIGN8(sizeof(struct sadb_comb
));
5725 for (i
= 1; i
<= SADB_EALG_MAX
; i
++) {
5726 algo
= esp_algorithm_lookup(i
);
5730 if (algo
->keymax
< ipsec_esp_keymin
)
5732 if (algo
->keymin
< ipsec_esp_keymin
)
5733 encmin
= ipsec_esp_keymin
;
5735 encmin
= algo
->keymin
;
5738 m
= key_getcomb_ah();
5742 panic("assumption failed in key_getcomb_esp");
5744 MGET(m
, M_DONTWAIT
, MT_DATA
);
5749 bzero(mtod(m
, caddr_t
), m
->m_len
);
5756 for (n
= m
; n
; n
= n
->m_next
)
5760 panic("assumption failed in key_getcomb_esp");
5763 for (off
= 0; off
< totlen
; off
+= l
) {
5764 n
= m_pulldown(m
, off
, l
, &o
);
5766 /* m is already freed */
5769 comb
= (struct sadb_comb
*)(mtod(n
, caddr_t
) + o
);
5770 bzero(comb
, sizeof(*comb
));
5771 key_getcomb_setlifetime(comb
);
5772 comb
->sadb_comb_encrypt
= i
;
5773 comb
->sadb_comb_encrypt_minbits
= encmin
;
5774 comb
->sadb_comb_encrypt_maxbits
= algo
->keymax
;
5793 * XXX reorder combinations by preference
5795 static struct mbuf
*
5798 struct sadb_comb
*comb
;
5799 const struct ah_algorithm
*algo
;
5803 const int l
= PFKEY_ALIGN8(sizeof(struct sadb_comb
));
5806 for (i
= 1; i
<= SADB_AALG_MAX
; i
++) {
5808 /* we prefer HMAC algorithms, not old algorithms */
5809 if (i
!= SADB_AALG_SHA1HMAC
&& i
!= SADB_AALG_MD5HMAC
)
5812 algo
= ah_algorithm_lookup(i
);
5816 if (algo
->keymax
< ipsec_ah_keymin
)
5818 if (algo
->keymin
< ipsec_ah_keymin
)
5819 min
= ipsec_ah_keymin
;
5826 panic("assumption failed in key_getcomb_ah");
5828 MGET(m
, M_DONTWAIT
, MT_DATA
);
5835 M_PREPEND(m
, l
, M_DONTWAIT
);
5839 comb
= mtod(m
, struct sadb_comb
*);
5840 bzero(comb
, sizeof(*comb
));
5841 key_getcomb_setlifetime(comb
);
5842 comb
->sadb_comb_auth
= i
;
5843 comb
->sadb_comb_auth_minbits
= min
;
5844 comb
->sadb_comb_auth_maxbits
= algo
->keymax
;
5851 * not really an official behavior. discussed in pf_key@inner.net in Sep2000.
5852 * XXX reorder combinations by preference
5854 static struct mbuf
*
5855 key_getcomb_ipcomp()
5857 struct sadb_comb
*comb
;
5858 const struct ipcomp_algorithm
*algo
;
5861 const int l
= PFKEY_ALIGN8(sizeof(struct sadb_comb
));
5864 for (i
= 1; i
<= SADB_X_CALG_MAX
; i
++) {
5865 algo
= ipcomp_algorithm_lookup(i
);
5872 panic("assumption failed in key_getcomb_ipcomp");
5874 MGET(m
, M_DONTWAIT
, MT_DATA
);
5881 M_PREPEND(m
, l
, M_DONTWAIT
);
5885 comb
= mtod(m
, struct sadb_comb
*);
5886 bzero(comb
, sizeof(*comb
));
5887 key_getcomb_setlifetime(comb
);
5888 comb
->sadb_comb_encrypt
= i
;
5889 /* what should we set into sadb_comb_*_{min,max}bits? */
5896 * XXX no way to pass mode (transport/tunnel) to userland
5897 * XXX replay checking?
5898 * XXX sysctl interface to ipsec_{ah,esp}_keymin
5900 static struct mbuf
*
5902 const struct secasindex
*saidx
;
5904 struct sadb_prop
*prop
;
5906 const int l
= PFKEY_ALIGN8(sizeof(struct sadb_prop
));
5909 switch (saidx
->proto
) {
5912 m
= key_getcomb_esp();
5916 m
= key_getcomb_ah();
5918 case IPPROTO_IPCOMP
:
5919 m
= key_getcomb_ipcomp();
5927 M_PREPEND(m
, l
, M_DONTWAIT
);
5932 for (n
= m
; n
; n
= n
->m_next
)
5935 prop
= mtod(m
, struct sadb_prop
*);
5936 bzero(prop
, sizeof(*prop
));
5937 prop
->sadb_prop_len
= PFKEY_UNIT64(totlen
);
5938 prop
->sadb_prop_exttype
= SADB_EXT_PROPOSAL
;
5939 prop
->sadb_prop_replay
= 32; /* XXX */
5945 * SADB_ACQUIRE processing called by key_checkrequest() and key_acquire2().
5947 * <base, SA, address(SD), (address(P)), x_policy,
5948 * (identity(SD),) (sensitivity,) proposal>
5949 * to KMD, and expect to receive
5950 * <base> with SADB_ACQUIRE if error occured,
5952 * <base, src address, dst address, (SPI range)> with SADB_GETSPI
5953 * from KMD by PF_KEY.
5955 * XXX x_policy is outside of RFC2367 (KAME extension).
5956 * XXX sensitivity is not supported.
5957 * XXX for ipcomp, RFC2367 does not define how to fill in proposal.
5958 * see comment for key_getcomb_ipcomp().
5962 * others: error number
5965 key_acquire(saidx
, sp
)
5966 struct secasindex
*saidx
;
5967 struct secpolicy
*sp
;
5969 struct mbuf
*result
= NULL
, *m
;
5970 #ifndef IPSEC_NONBLOCK_ACQUIRE
5971 struct secacq
*newacq
;
5979 panic("key_acquire: NULL pointer is passed.\n");
5980 if ((satype
= key_proto2satype(saidx
->proto
)) == 0)
5981 panic("key_acquire: invalid proto is passed.\n");
5983 #ifndef IPSEC_NONBLOCK_ACQUIRE
5985 * We never do anything about acquirng SA. There is anather
5986 * solution that kernel blocks to send SADB_ACQUIRE message until
5987 * getting something message from IKEd. In later case, to be
5988 * managed with ACQUIRING list.
5990 /* get a entry to check whether sending message or not. */
5991 if ((newacq
= key_getacq(saidx
)) != NULL
) {
5992 if (key_blockacq_count
< newacq
->count
) {
5993 /* reset counter and do send message. */
5996 /* increment counter and do nothing. */
6001 /* make new entry for blocking to send SADB_ACQUIRE. */
6002 if ((newacq
= key_newacq(saidx
)) == NULL
)
6005 /* add to acqtree */
6006 LIST_INSERT_HEAD(&acqtree
, newacq
, chain
);
6011 #ifndef IPSEC_NONBLOCK_ACQUIRE
6014 seq
= (acq_seq
= (acq_seq
== ~0 ? 1 : ++acq_seq
));
6016 m
= key_setsadbmsg(SADB_ACQUIRE
, 0, satype
, seq
, 0, 0);
6023 /* set sadb_address for saidx's. */
6024 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
6025 (struct sockaddr
*)&saidx
->src
, saidx
->src
.ss_len
<< 3,
6033 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
6034 (struct sockaddr
*)&saidx
->dst
, saidx
->dst
.ss_len
<< 3,
6042 /* XXX proxy address (optional) */
6044 /* set sadb_x_policy */
6046 m
= key_setsadbxpolicy(sp
->policy
, sp
->spidx
.dir
, sp
->id
);
6054 /* XXX identity (optional) */
6056 if (idexttype
&& fqdn
) {
6057 /* create identity extension (FQDN) */
6058 struct sadb_ident
*id
;
6061 fqdnlen
= strlen(fqdn
) + 1; /* +1 for terminating-NUL */
6062 id
= (struct sadb_ident
*)p
;
6063 bzero(id
, sizeof(*id
) + PFKEY_ALIGN8(fqdnlen
));
6064 id
->sadb_ident_len
= PFKEY_UNIT64(sizeof(*id
) + PFKEY_ALIGN8(fqdnlen
));
6065 id
->sadb_ident_exttype
= idexttype
;
6066 id
->sadb_ident_type
= SADB_IDENTTYPE_FQDN
;
6067 bcopy(fqdn
, id
+ 1, fqdnlen
);
6068 p
+= sizeof(struct sadb_ident
) + PFKEY_ALIGN8(fqdnlen
);
6072 /* create identity extension (USERFQDN) */
6073 struct sadb_ident
*id
;
6077 /* +1 for terminating-NUL */
6078 userfqdnlen
= strlen(userfqdn
) + 1;
6081 id
= (struct sadb_ident
*)p
;
6082 bzero(id
, sizeof(*id
) + PFKEY_ALIGN8(userfqdnlen
));
6083 id
->sadb_ident_len
= PFKEY_UNIT64(sizeof(*id
) + PFKEY_ALIGN8(userfqdnlen
));
6084 id
->sadb_ident_exttype
= idexttype
;
6085 id
->sadb_ident_type
= SADB_IDENTTYPE_USERFQDN
;
6086 /* XXX is it correct? */
6087 if (curproc
&& curproc
->p_cred
)
6088 id
->sadb_ident_id
= curproc
->p_cred
->p_ruid
;
6089 if (userfqdn
&& userfqdnlen
)
6090 bcopy(userfqdn
, id
+ 1, userfqdnlen
);
6091 p
+= sizeof(struct sadb_ident
) + PFKEY_ALIGN8(userfqdnlen
);
6095 /* XXX sensitivity (optional) */
6097 /* create proposal/combination extension */
6098 m
= key_getprop(saidx
);
6101 * spec conformant: always attach proposal/combination extension,
6102 * the problem is that we have no way to attach it for ipcomp,
6103 * due to the way sadb_comb is declared in RFC2367.
6112 * outside of spec; make proposal/combination extension optional.
6118 if ((result
->m_flags
& M_PKTHDR
) == 0) {
6123 if (result
->m_len
< sizeof(struct sadb_msg
)) {
6124 result
= m_pullup(result
, sizeof(struct sadb_msg
));
6125 if (result
== NULL
) {
6131 result
->m_pkthdr
.len
= 0;
6132 for (m
= result
; m
; m
= m
->m_next
)
6133 result
->m_pkthdr
.len
+= m
->m_len
;
6135 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
6136 PFKEY_UNIT64(result
->m_pkthdr
.len
);
6138 return key_sendup_mbuf(NULL
, result
, KEY_SENDUP_REGISTERED
);
6146 #ifndef IPSEC_NONBLOCK_ACQUIRE
6147 static struct secacq
*
6149 struct secasindex
*saidx
;
6151 struct secacq
*newacq
;
6155 KMALLOC(newacq
, struct secacq
*, sizeof(struct secacq
));
6156 if (newacq
== NULL
) {
6158 printf("key_newacq: No more memory.\n");
6162 bzero(newacq
, sizeof(*newacq
));
6165 bcopy(saidx
, &newacq
->saidx
, sizeof(newacq
->saidx
));
6166 newacq
->seq
= (acq_seq
== ~0 ? 1 : ++acq_seq
);
6168 newacq
->created
= tv
.tv_sec
;
6174 static struct secacq
*
6176 struct secasindex
*saidx
;
6180 LIST_FOREACH(acq
, &acqtree
, chain
) {
6181 if (key_cmpsaidx_exactly(saidx
, &acq
->saidx
))
6188 static struct secacq
*
6189 key_getacqbyseq(seq
)
6194 LIST_FOREACH(acq
, &acqtree
, chain
) {
6195 if (acq
->seq
== seq
)
6203 static struct secspacq
*
6205 struct secpolicyindex
*spidx
;
6207 struct secspacq
*acq
;
6211 KMALLOC(acq
, struct secspacq
*, sizeof(struct secspacq
));
6214 printf("key_newspacq: No more memory.\n");
6218 bzero(acq
, sizeof(*acq
));
6221 bcopy(spidx
, &acq
->spidx
, sizeof(acq
->spidx
));
6223 acq
->created
= tv
.tv_sec
;
6229 static struct secspacq
*
6231 struct secpolicyindex
*spidx
;
6233 struct secspacq
*acq
;
6235 LIST_FOREACH(acq
, &spacqtree
, chain
) {
6236 if (key_cmpspidx_exactly(spidx
, &acq
->spidx
))
6244 * SADB_ACQUIRE processing,
6245 * in first situation, is receiving
6247 * from the ikmpd, and clear sequence of its secasvar entry.
6249 * In second situation, is receiving
6250 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
6251 * from a user land process, and return
6252 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
6255 * m will always be freed.
6258 key_acquire2(so
, m
, mhp
)
6261 const struct sadb_msghdr
*mhp
;
6263 const struct sadb_address
*src0
, *dst0
;
6264 struct secasindex saidx
;
6265 struct secashead
*sah
;
6270 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
6271 panic("key_acquire2: NULL pointer is passed.\n");
6274 * Error message from KMd.
6275 * We assume that if error was occured in IKEd, the length of PFKEY
6276 * message is equal to the size of sadb_msg structure.
6277 * We do not raise error even if error occured in this function.
6279 if (mhp
->msg
->sadb_msg_len
== PFKEY_UNIT64(sizeof(struct sadb_msg
))) {
6280 #ifndef IPSEC_NONBLOCK_ACQUIRE
6284 /* check sequence number */
6285 if (mhp
->msg
->sadb_msg_seq
== 0) {
6287 printf("key_acquire2: must specify sequence number.\n");
6293 if ((acq
= key_getacqbyseq(mhp
->msg
->sadb_msg_seq
)) == NULL
) {
6295 * the specified larval SA is already gone, or we got
6296 * a bogus sequence number. we can silently ignore it.
6302 /* reset acq counter in order to deletion by timehander. */
6304 acq
->created
= tv
.tv_sec
;
6312 * This message is from user land.
6315 /* map satype to proto */
6316 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
6318 printf("key_acquire2: invalid satype is passed.\n");
6320 return key_senderror(so
, m
, EINVAL
);
6323 if (mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
6324 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
||
6325 mhp
->ext
[SADB_EXT_PROPOSAL
] == NULL
) {
6328 printf("key_acquire2: invalid message is passed.\n");
6330 return key_senderror(so
, m
, EINVAL
);
6332 if (mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
6333 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
) ||
6334 mhp
->extlen
[SADB_EXT_PROPOSAL
] < sizeof(struct sadb_prop
)) {
6337 printf("key_acquire2: invalid message is passed.\n");
6339 return key_senderror(so
, m
, EINVAL
);
6342 src0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_SRC
];
6343 dst0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_DST
];
6345 /* XXX boundary check against sa_len */
6346 KEY_SETSECASIDX(proto
, IPSEC_MODE_ANY
, 0, src0
+ 1, dst0
+ 1, &saidx
);
6348 /* get a SA index */
6349 LIST_FOREACH(sah
, &sahtree
, chain
) {
6350 if (sah
->state
== SADB_SASTATE_DEAD
)
6352 if (key_cmpsaidx_withmode(&sah
->saidx
, &saidx
))
6357 printf("key_acquire2: a SA exists already.\n");
6359 return key_senderror(so
, m
, EEXIST
);
6362 error
= key_acquire(&saidx
, NULL
);
6365 printf("key_acquire2: error %d returned "
6366 "from key_acquire.\n", mhp
->msg
->sadb_msg_errno
);
6368 return key_senderror(so
, m
, error
);
6371 return key_sendup_mbuf(so
, m
, KEY_SENDUP_REGISTERED
);
6375 * SADB_REGISTER processing.
6376 * If SATYPE_UNSPEC has been passed as satype, only return sabd_supported.
6379 * from the ikmpd, and register a socket to send PF_KEY messages,
6383 * If socket is detached, must free from regnode.
6385 * m will always be freed.
6388 key_register(so
, m
, mhp
)
6391 const struct sadb_msghdr
*mhp
;
6393 struct secreg
*reg
, *newreg
= 0;
6396 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
6397 panic("key_register: NULL pointer is passed.\n");
6399 /* check for invalid register message */
6400 if (mhp
->msg
->sadb_msg_satype
>= sizeof(regtree
)/sizeof(regtree
[0]))
6401 return key_senderror(so
, m
, EINVAL
);
6403 /* When SATYPE_UNSPEC is specified, only return sabd_supported. */
6404 if (mhp
->msg
->sadb_msg_satype
== SADB_SATYPE_UNSPEC
)
6407 /* check whether existing or not */
6408 LIST_FOREACH(reg
, ®tree
[mhp
->msg
->sadb_msg_satype
], chain
) {
6409 if (reg
->so
== so
) {
6411 printf("key_register: socket exists already.\n");
6413 return key_senderror(so
, m
, EEXIST
);
6417 /* create regnode */
6418 KMALLOC(newreg
, struct secreg
*, sizeof(*newreg
));
6419 if (newreg
== NULL
) {
6421 printf("key_register: No more memory.\n");
6423 return key_senderror(so
, m
, ENOBUFS
);
6425 bzero((caddr_t
)newreg
, sizeof(*newreg
));
6428 ((struct keycb
*)sotorawcb(so
))->kp_registered
++;
6430 /* add regnode to regtree. */
6431 LIST_INSERT_HEAD(®tree
[mhp
->msg
->sadb_msg_satype
], newreg
, chain
);
6436 struct sadb_msg
*newmsg
;
6437 struct sadb_supported
*sup
;
6438 u_int len
, alen
, elen
;
6441 struct sadb_alg
*alg
;
6443 /* create new sadb_msg to reply. */
6445 for (i
= 1; i
<= SADB_AALG_MAX
; i
++) {
6446 if (ah_algorithm_lookup(i
))
6447 alen
+= sizeof(struct sadb_alg
);
6450 alen
+= sizeof(struct sadb_supported
);
6453 for (i
= 1; i
<= SADB_EALG_MAX
; i
++) {
6454 if (esp_algorithm_lookup(i
))
6455 elen
+= sizeof(struct sadb_alg
);
6458 elen
+= sizeof(struct sadb_supported
);
6461 len
= sizeof(struct sadb_msg
) + alen
+ elen
;
6464 return key_senderror(so
, m
, ENOBUFS
);
6466 MGETHDR(n
, M_DONTWAIT
, MT_DATA
);
6468 MCLGET(n
, M_DONTWAIT
);
6469 if ((n
->m_flags
& M_EXT
) == 0) {
6475 return key_senderror(so
, m
, ENOBUFS
);
6477 n
->m_pkthdr
.len
= n
->m_len
= len
;
6481 m_copydata(m
, 0, sizeof(struct sadb_msg
), mtod(n
, caddr_t
) + off
);
6482 newmsg
= mtod(n
, struct sadb_msg
*);
6483 newmsg
->sadb_msg_errno
= 0;
6484 newmsg
->sadb_msg_len
= PFKEY_UNIT64(len
);
6485 off
+= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
6487 /* for authentication algorithm */
6489 sup
= (struct sadb_supported
*)(mtod(n
, caddr_t
) + off
);
6490 sup
->sadb_supported_len
= PFKEY_UNIT64(alen
);
6491 sup
->sadb_supported_exttype
= SADB_EXT_SUPPORTED_AUTH
;
6492 off
+= PFKEY_ALIGN8(sizeof(*sup
));
6494 for (i
= 1; i
<= SADB_AALG_MAX
; i
++) {
6495 const struct ah_algorithm
*aalgo
;
6497 aalgo
= ah_algorithm_lookup(i
);
6500 alg
= (struct sadb_alg
*)(mtod(n
, caddr_t
) + off
);
6501 alg
->sadb_alg_id
= i
;
6502 alg
->sadb_alg_ivlen
= 0;
6503 alg
->sadb_alg_minbits
= aalgo
->keymin
;
6504 alg
->sadb_alg_maxbits
= aalgo
->keymax
;
6505 off
+= PFKEY_ALIGN8(sizeof(*alg
));
6510 /* for encryption algorithm */
6512 sup
= (struct sadb_supported
*)(mtod(n
, caddr_t
) + off
);
6513 sup
->sadb_supported_len
= PFKEY_UNIT64(elen
);
6514 sup
->sadb_supported_exttype
= SADB_EXT_SUPPORTED_ENCRYPT
;
6515 off
+= PFKEY_ALIGN8(sizeof(*sup
));
6517 for (i
= 1; i
<= SADB_EALG_MAX
; i
++) {
6518 const struct esp_algorithm
*ealgo
;
6520 ealgo
= esp_algorithm_lookup(i
);
6523 alg
= (struct sadb_alg
*)(mtod(n
, caddr_t
) + off
);
6524 alg
->sadb_alg_id
= i
;
6525 if (ealgo
&& ealgo
->ivlen
) {
6527 * give NULL to get the value preferred by
6528 * algorithm XXX SADB_X_EXT_DERIV ?
6530 alg
->sadb_alg_ivlen
=
6531 (*ealgo
->ivlen
)(ealgo
, NULL
);
6533 alg
->sadb_alg_ivlen
= 0;
6534 alg
->sadb_alg_minbits
= ealgo
->keymin
;
6535 alg
->sadb_alg_maxbits
= ealgo
->keymax
;
6536 off
+= PFKEY_ALIGN8(sizeof(struct sadb_alg
));
6543 panic("length assumption failed in key_register");
6547 return key_sendup_mbuf(so
, n
, KEY_SENDUP_REGISTERED
);
6552 * free secreg entry registered.
6553 * XXX: I want to do free a socket marked done SADB_RESIGER to socket.
6564 panic("key_freereg: NULL pointer is passed.\n");
6567 * check whether existing or not.
6568 * check all type of SA, because there is a potential that
6569 * one socket is registered to multiple type of SA.
6571 for (i
= 0; i
<= SADB_SATYPE_MAX
; i
++) {
6572 LIST_FOREACH(reg
, ®tree
[i
], chain
) {
6574 && __LIST_CHAINED(reg
)) {
6575 LIST_REMOVE(reg
, chain
);
6586 * SADB_EXPIRE processing
6588 * <base, SA, SA2, lifetime(C and one of HS), address(SD)>
6590 * NOTE: We send only soft lifetime extension.
6593 * others : error number
6597 struct secasvar
*sav
;
6601 struct mbuf
*result
= NULL
, *m
;
6604 struct sadb_lifetime
*lt
;
6606 /* XXX: Why do we lock ? */
6607 s
= splnet(); /*called from softclock()*/
6611 panic("key_expire: NULL pointer is passed.\n");
6612 if (sav
->sah
== NULL
)
6613 panic("key_expire: Why was SA index in SA NULL.\n");
6614 if ((satype
= key_proto2satype(sav
->sah
->saidx
.proto
)) == 0)
6615 panic("key_expire: invalid proto is passed.\n");
6617 /* set msg header */
6618 m
= key_setsadbmsg(SADB_EXPIRE
, 0, satype
, sav
->seq
, 0, sav
->refcnt
);
6625 /* create SA extension */
6626 m
= key_setsadbsa(sav
);
6633 /* create SA extension */
6634 m
= key_setsadbxsa2(sav
->sah
->saidx
.mode
, sav
->sah
->saidx
.reqid
);
6641 /* create lifetime extension (current and soft) */
6642 len
= PFKEY_ALIGN8(sizeof(*lt
)) * 2;
6643 m
= key_alloc_mbuf(len
);
6644 if (!m
|| m
->m_next
) { /*XXX*/
6650 bzero(mtod(m
, caddr_t
), len
);
6651 lt
= mtod(m
, struct sadb_lifetime
*);
6652 lt
->sadb_lifetime_len
= PFKEY_UNIT64(sizeof(struct sadb_lifetime
));
6653 lt
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
6654 lt
->sadb_lifetime_allocations
= sav
->lft_c
->sadb_lifetime_allocations
;
6655 lt
->sadb_lifetime_bytes
= sav
->lft_c
->sadb_lifetime_bytes
;
6656 lt
->sadb_lifetime_addtime
= sav
->lft_c
->sadb_lifetime_addtime
;
6657 lt
->sadb_lifetime_usetime
= sav
->lft_c
->sadb_lifetime_usetime
;
6658 lt
= (struct sadb_lifetime
*)(mtod(m
, caddr_t
) + len
/ 2);
6659 bcopy(sav
->lft_s
, lt
, sizeof(*lt
));
6662 /* set sadb_address for source */
6663 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
6664 (struct sockaddr
*)&sav
->sah
->saidx
.src
,
6665 sav
->sah
->saidx
.src
.ss_len
<< 3, IPSEC_ULPROTO_ANY
);
6672 /* set sadb_address for destination */
6673 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
6674 (struct sockaddr
*)&sav
->sah
->saidx
.dst
,
6675 sav
->sah
->saidx
.dst
.ss_len
<< 3, IPSEC_ULPROTO_ANY
);
6682 if ((result
->m_flags
& M_PKTHDR
) == 0) {
6687 if (result
->m_len
< sizeof(struct sadb_msg
)) {
6688 result
= m_pullup(result
, sizeof(struct sadb_msg
));
6689 if (result
== NULL
) {
6695 result
->m_pkthdr
.len
= 0;
6696 for (m
= result
; m
; m
= m
->m_next
)
6697 result
->m_pkthdr
.len
+= m
->m_len
;
6699 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
6700 PFKEY_UNIT64(result
->m_pkthdr
.len
);
6702 return key_sendup_mbuf(NULL
, result
, KEY_SENDUP_REGISTERED
);
6712 * SADB_FLUSH processing
6715 * from the ikmpd, and free all entries in secastree.
6719 * NOTE: to do is only marking SADB_SASTATE_DEAD.
6721 * m will always be freed.
6724 key_flush(so
, m
, mhp
)
6727 const struct sadb_msghdr
*mhp
;
6729 struct sadb_msg
*newmsg
;
6730 struct secashead
*sah
, *nextsah
;
6731 struct secasvar
*sav
, *nextsav
;
6737 if (so
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
6738 panic("key_flush: NULL pointer is passed.\n");
6740 /* map satype to proto */
6741 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
6743 printf("key_flush: invalid satype is passed.\n");
6745 return key_senderror(so
, m
, EINVAL
);
6748 /* no SATYPE specified, i.e. flushing all SA. */
6749 for (sah
= LIST_FIRST(&sahtree
);
6752 nextsah
= LIST_NEXT(sah
, chain
);
6754 if (mhp
->msg
->sadb_msg_satype
!= SADB_SATYPE_UNSPEC
6755 && proto
!= sah
->saidx
.proto
)
6759 stateidx
< _ARRAYLEN(saorder_state_alive
);
6761 state
= saorder_state_any
[stateidx
];
6762 for (sav
= LIST_FIRST(&sah
->savtree
[state
]);
6766 nextsav
= LIST_NEXT(sav
, chain
);
6768 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
6773 sah
->state
= SADB_SASTATE_DEAD
;
6776 if (m
->m_len
< sizeof(struct sadb_msg
) ||
6777 sizeof(struct sadb_msg
) > m
->m_len
+ M_TRAILINGSPACE(m
)) {
6779 printf("key_flush: No more memory.\n");
6781 return key_senderror(so
, m
, ENOBUFS
);
6787 m
->m_pkthdr
.len
= m
->m_len
= sizeof(struct sadb_msg
);
6788 newmsg
= mtod(m
, struct sadb_msg
*);
6789 newmsg
->sadb_msg_errno
= 0;
6790 newmsg
->sadb_msg_len
= PFKEY_UNIT64(m
->m_pkthdr
.len
);
6792 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ALL
);
6796 * SADB_DUMP processing
6797 * dump all entries including status of DEAD in SAD.
6800 * from the ikmpd, and dump all secasvar leaves
6805 * m will always be freed.
6808 key_dump(so
, m
, mhp
)
6811 const struct sadb_msghdr
*mhp
;
6813 struct secashead
*sah
;
6814 struct secasvar
*sav
;
6820 struct sadb_msg
*newmsg
;
6824 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
6825 panic("key_dump: NULL pointer is passed.\n");
6827 /* map satype to proto */
6828 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
6830 printf("key_dump: invalid satype is passed.\n");
6832 return key_senderror(so
, m
, EINVAL
);
6835 /* count sav entries to be sent to the userland. */
6837 LIST_FOREACH(sah
, &sahtree
, chain
) {
6838 if (mhp
->msg
->sadb_msg_satype
!= SADB_SATYPE_UNSPEC
6839 && proto
!= sah
->saidx
.proto
)
6843 stateidx
< _ARRAYLEN(saorder_state_any
);
6845 state
= saorder_state_any
[stateidx
];
6846 LIST_FOREACH(sav
, &sah
->savtree
[state
], chain
) {
6853 return key_senderror(so
, m
, ENOENT
);
6855 /* send this to the userland, one at a time. */
6857 LIST_FOREACH(sah
, &sahtree
, chain
) {
6858 if (mhp
->msg
->sadb_msg_satype
!= SADB_SATYPE_UNSPEC
6859 && proto
!= sah
->saidx
.proto
)
6862 /* map proto to satype */
6863 if ((satype
= key_proto2satype(sah
->saidx
.proto
)) == 0) {
6865 printf("key_dump: there was invalid proto in SAD.\n");
6867 return key_senderror(so
, m
, EINVAL
);
6871 stateidx
< _ARRAYLEN(saorder_state_any
);
6873 state
= saorder_state_any
[stateidx
];
6874 LIST_FOREACH(sav
, &sah
->savtree
[state
], chain
) {
6875 n
= key_setdumpsa(sav
, SADB_DUMP
, satype
,
6876 --cnt
, mhp
->msg
->sadb_msg_pid
);
6878 return key_senderror(so
, m
, ENOBUFS
);
6880 key_sendup_mbuf(so
, n
, KEY_SENDUP_ONE
);
6890 * SADB_X_PROMISC processing
6892 * m will always be freed.
6895 key_promisc(so
, m
, mhp
)
6898 const struct sadb_msghdr
*mhp
;
6903 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
6904 panic("key_promisc: NULL pointer is passed.\n");
6906 olen
= PFKEY_UNUNIT64(mhp
->msg
->sadb_msg_len
);
6908 if (olen
< sizeof(struct sadb_msg
)) {
6910 return key_senderror(so
, m
, EINVAL
);
6915 } else if (olen
== sizeof(struct sadb_msg
)) {
6916 /* enable/disable promisc mode */
6919 if ((kp
= (struct keycb
*)sotorawcb(so
)) == NULL
)
6920 return key_senderror(so
, m
, EINVAL
);
6921 mhp
->msg
->sadb_msg_errno
= 0;
6922 switch (mhp
->msg
->sadb_msg_satype
) {
6925 kp
->kp_promisc
= mhp
->msg
->sadb_msg_satype
;
6928 return key_senderror(so
, m
, EINVAL
);
6931 /* send the original message back to everyone */
6932 mhp
->msg
->sadb_msg_errno
= 0;
6933 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ALL
);
6935 /* send packet as is */
6937 m_adj(m
, PFKEY_ALIGN8(sizeof(struct sadb_msg
)));
6939 /* TODO: if sadb_msg_seq is specified, send to specific pid */
6940 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ALL
);
6944 static int (*key_typesw
[]) __P((struct socket
*, struct mbuf
*,
6945 const struct sadb_msghdr
*)) = {
6946 NULL
, /* SADB_RESERVED */
6947 key_getspi
, /* SADB_GETSPI */
6948 key_update
, /* SADB_UPDATE */
6949 key_add
, /* SADB_ADD */
6950 key_delete
, /* SADB_DELETE */
6951 key_get
, /* SADB_GET */
6952 key_acquire2
, /* SADB_ACQUIRE */
6953 key_register
, /* SADB_REGISTER */
6954 NULL
, /* SADB_EXPIRE */
6955 key_flush
, /* SADB_FLUSH */
6956 key_dump
, /* SADB_DUMP */
6957 key_promisc
, /* SADB_X_PROMISC */
6958 NULL
, /* SADB_X_PCHANGE */
6959 key_spdadd
, /* SADB_X_SPDUPDATE */
6960 key_spdadd
, /* SADB_X_SPDADD */
6961 key_spddelete
, /* SADB_X_SPDDELETE */
6962 key_spdget
, /* SADB_X_SPDGET */
6963 NULL
, /* SADB_X_SPDACQUIRE */
6964 key_spddump
, /* SADB_X_SPDDUMP */
6965 key_spdflush
, /* SADB_X_SPDFLUSH */
6966 key_spdadd
, /* SADB_X_SPDSETIDX */
6967 NULL
, /* SADB_X_SPDEXPIRE */
6968 key_spddelete2
, /* SADB_X_SPDDELETE2 */
6972 * parse sadb_msg buffer to process PFKEYv2,
6973 * and create a data to response if needed.
6974 * I think to be dealed with mbuf directly.
6976 * msgp : pointer to pointer to a received buffer pulluped.
6977 * This is rewrited to response.
6978 * so : pointer to socket.
6980 * length for buffer to send to user process.
6987 struct sadb_msg
*msg
;
6988 struct sadb_msghdr mh
;
6994 if (m
== NULL
|| so
== NULL
)
6995 panic("key_parse: NULL pointer is passed.\n");
6997 #if 0 /*kdebug_sadb assumes msg in linear buffer*/
6998 KEYDEBUG(KEYDEBUG_KEY_DUMP
,
6999 printf("key_parse: passed sadb_msg\n");
7003 if (m
->m_len
< sizeof(struct sadb_msg
)) {
7004 m
= m_pullup(m
, sizeof(struct sadb_msg
));
7008 msg
= mtod(m
, struct sadb_msg
*);
7009 orglen
= PFKEY_UNUNIT64(msg
->sadb_msg_len
);
7010 target
= KEY_SENDUP_ONE
;
7012 if ((m
->m_flags
& M_PKTHDR
) == 0 ||
7013 m
->m_pkthdr
.len
!= m
->m_pkthdr
.len
) {
7015 printf("key_parse: invalid message length.\n");
7017 pfkeystat
.out_invlen
++;
7022 if (msg
->sadb_msg_version
!= PF_KEY_V2
) {
7024 printf("key_parse: PF_KEY version %u is mismatched.\n",
7025 msg
->sadb_msg_version
);
7027 pfkeystat
.out_invver
++;
7032 if (msg
->sadb_msg_type
> SADB_MAX
) {
7034 printf("key_parse: invalid type %u is passed.\n",
7035 msg
->sadb_msg_type
);
7037 pfkeystat
.out_invmsgtype
++;
7042 /* for old-fashioned code - should be nuked */
7043 if (m
->m_pkthdr
.len
> MCLBYTES
) {
7050 MGETHDR(n
, M_DONTWAIT
, MT_DATA
);
7051 if (n
&& m
->m_pkthdr
.len
> MHLEN
) {
7052 MCLGET(n
, M_DONTWAIT
);
7053 if ((n
->m_flags
& M_EXT
) == 0) {
7062 m_copydata(m
, 0, m
->m_pkthdr
.len
, mtod(n
, caddr_t
));
7063 n
->m_pkthdr
.len
= n
->m_len
= m
->m_pkthdr
.len
;
7069 /* align the mbuf chain so that extensions are in contiguous region. */
7070 error
= key_align(m
, &mh
);
7074 if (m
->m_next
) { /*XXX*/
7082 switch (msg
->sadb_msg_satype
) {
7083 case SADB_SATYPE_UNSPEC
:
7084 switch (msg
->sadb_msg_type
) {
7093 printf("key_parse: must specify satype "
7094 "when msg type=%u.\n",
7095 msg
->sadb_msg_type
);
7097 pfkeystat
.out_invsatype
++;
7102 case SADB_SATYPE_AH
:
7103 case SADB_SATYPE_ESP
:
7104 case SADB_X_SATYPE_IPCOMP
:
7105 switch (msg
->sadb_msg_type
) {
7107 case SADB_X_SPDDELETE
:
7109 case SADB_X_SPDDUMP
:
7110 case SADB_X_SPDFLUSH
:
7111 case SADB_X_SPDSETIDX
:
7112 case SADB_X_SPDUPDATE
:
7113 case SADB_X_SPDDELETE2
:
7115 printf("key_parse: illegal satype=%u\n",
7116 msg
->sadb_msg_type
);
7118 pfkeystat
.out_invsatype
++;
7123 case SADB_SATYPE_RSVP
:
7124 case SADB_SATYPE_OSPFV2
:
7125 case SADB_SATYPE_RIPV2
:
7126 case SADB_SATYPE_MIP
:
7128 printf("key_parse: type %u isn't supported.\n",
7129 msg
->sadb_msg_satype
);
7131 pfkeystat
.out_invsatype
++;
7134 case 1: /* XXX: What does it do? */
7135 if (msg
->sadb_msg_type
== SADB_X_PROMISC
)
7140 printf("key_parse: invalid type %u is passed.\n",
7141 msg
->sadb_msg_satype
);
7143 pfkeystat
.out_invsatype
++;
7148 /* check field of upper layer protocol and address family */
7149 if (mh
.ext
[SADB_EXT_ADDRESS_SRC
] != NULL
7150 && mh
.ext
[SADB_EXT_ADDRESS_DST
] != NULL
) {
7151 struct sadb_address
*src0
, *dst0
;
7154 src0
= (struct sadb_address
*)(mh
.ext
[SADB_EXT_ADDRESS_SRC
]);
7155 dst0
= (struct sadb_address
*)(mh
.ext
[SADB_EXT_ADDRESS_DST
]);
7157 /* check upper layer protocol */
7158 if (src0
->sadb_address_proto
!= dst0
->sadb_address_proto
) {
7160 printf("key_parse: upper layer protocol mismatched.\n");
7162 pfkeystat
.out_invaddr
++;
7168 if (PFKEY_ADDR_SADDR(src0
)->sa_family
!=
7169 PFKEY_ADDR_SADDR(dst0
)->sa_family
) {
7171 printf("key_parse: address family mismatched.\n");
7173 pfkeystat
.out_invaddr
++;
7177 if (PFKEY_ADDR_SADDR(src0
)->sa_len
!=
7178 PFKEY_ADDR_SADDR(dst0
)->sa_len
) {
7180 printf("key_parse: address struct size mismatched.\n");
7182 pfkeystat
.out_invaddr
++;
7187 switch (PFKEY_ADDR_SADDR(src0
)->sa_family
) {
7189 if (PFKEY_ADDR_SADDR(src0
)->sa_len
!=
7190 sizeof(struct sockaddr_in
)) {
7191 pfkeystat
.out_invaddr
++;
7197 if (PFKEY_ADDR_SADDR(src0
)->sa_len
!=
7198 sizeof(struct sockaddr_in6
)) {
7199 pfkeystat
.out_invaddr
++;
7206 printf("key_parse: unsupported address family.\n");
7208 pfkeystat
.out_invaddr
++;
7209 error
= EAFNOSUPPORT
;
7213 switch (PFKEY_ADDR_SADDR(src0
)->sa_family
) {
7215 plen
= sizeof(struct in_addr
) << 3;
7218 plen
= sizeof(struct in6_addr
) << 3;
7221 plen
= 0; /*fool gcc*/
7225 /* check max prefix length */
7226 if (src0
->sadb_address_prefixlen
> plen
||
7227 dst0
->sadb_address_prefixlen
> plen
) {
7229 printf("key_parse: illegal prefixlen.\n");
7231 pfkeystat
.out_invaddr
++;
7237 * prefixlen == 0 is valid because there can be a case when
7238 * all addresses are matched.
7242 if (msg
->sadb_msg_type
>= sizeof(key_typesw
)/sizeof(key_typesw
[0]) ||
7243 key_typesw
[msg
->sadb_msg_type
] == NULL
) {
7244 pfkeystat
.out_invmsgtype
++;
7249 return (*key_typesw
[msg
->sadb_msg_type
])(so
, m
, &mh
);
7252 msg
->sadb_msg_errno
= error
;
7253 return key_sendup_mbuf(so
, m
, target
);
7257 key_senderror(so
, m
, code
)
7262 struct sadb_msg
*msg
;
7264 if (m
->m_len
< sizeof(struct sadb_msg
))
7265 panic("invalid mbuf passed to key_senderror");
7267 msg
= mtod(m
, struct sadb_msg
*);
7268 msg
->sadb_msg_errno
= code
;
7269 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ONE
);
7273 * set the pointer to each header into message buffer.
7274 * m will be freed on error.
7275 * XXX larger-than-MCLBYTES extension?
7280 struct sadb_msghdr
*mhp
;
7283 struct sadb_ext
*ext
;
7289 if (m
== NULL
|| mhp
== NULL
)
7290 panic("key_align: NULL pointer is passed.\n");
7291 if (m
->m_len
< sizeof(struct sadb_msg
))
7292 panic("invalid mbuf passed to key_align");
7295 bzero(mhp
, sizeof(*mhp
));
7297 mhp
->msg
= mtod(m
, struct sadb_msg
*);
7298 mhp
->ext
[0] = (struct sadb_ext
*)mhp
->msg
; /*XXX backward compat */
7300 end
= PFKEY_UNUNIT64(mhp
->msg
->sadb_msg_len
);
7301 extlen
= end
; /*just in case extlen is not updated*/
7302 for (off
= sizeof(struct sadb_msg
); off
< end
; off
+= extlen
) {
7303 n
= m_pulldown(m
, off
, sizeof(struct sadb_ext
), &toff
);
7305 /* m is already freed */
7308 ext
= (struct sadb_ext
*)(mtod(n
, caddr_t
) + toff
);
7311 switch (ext
->sadb_ext_type
) {
7313 case SADB_EXT_ADDRESS_SRC
:
7314 case SADB_EXT_ADDRESS_DST
:
7315 case SADB_EXT_ADDRESS_PROXY
:
7316 case SADB_EXT_LIFETIME_CURRENT
:
7317 case SADB_EXT_LIFETIME_HARD
:
7318 case SADB_EXT_LIFETIME_SOFT
:
7319 case SADB_EXT_KEY_AUTH
:
7320 case SADB_EXT_KEY_ENCRYPT
:
7321 case SADB_EXT_IDENTITY_SRC
:
7322 case SADB_EXT_IDENTITY_DST
:
7323 case SADB_EXT_SENSITIVITY
:
7324 case SADB_EXT_PROPOSAL
:
7325 case SADB_EXT_SUPPORTED_AUTH
:
7326 case SADB_EXT_SUPPORTED_ENCRYPT
:
7327 case SADB_EXT_SPIRANGE
:
7328 case SADB_X_EXT_POLICY
:
7329 case SADB_X_EXT_SA2
:
7330 /* duplicate check */
7332 * XXX Are there duplication payloads of either
7333 * KEY_AUTH or KEY_ENCRYPT ?
7335 if (mhp
->ext
[ext
->sadb_ext_type
] != NULL
) {
7337 printf("key_align: duplicate ext_type %u "
7339 ext
->sadb_ext_type
);
7342 pfkeystat
.out_dupext
++;
7348 printf("key_align: invalid ext_type %u is passed.\n",
7349 ext
->sadb_ext_type
);
7352 pfkeystat
.out_invexttype
++;
7356 extlen
= PFKEY_UNUNIT64(ext
->sadb_ext_len
);
7358 if (key_validate_ext(ext
, extlen
)) {
7360 pfkeystat
.out_invlen
++;
7364 n
= m_pulldown(m
, off
, extlen
, &toff
);
7366 /* m is already freed */
7369 ext
= (struct sadb_ext
*)(mtod(n
, caddr_t
) + toff
);
7371 mhp
->ext
[ext
->sadb_ext_type
] = ext
;
7372 mhp
->extoff
[ext
->sadb_ext_type
] = off
;
7373 mhp
->extlen
[ext
->sadb_ext_type
] = extlen
;
7378 pfkeystat
.out_invlen
++;
7386 key_validate_ext(ext
, len
)
7387 const struct sadb_ext
*ext
;
7390 struct sockaddr
*sa
;
7391 enum { NONE
, ADDR
} checktype
= NONE
;
7393 const int sal
= offsetof(struct sockaddr
, sa_len
) + sizeof(sa
->sa_len
);
7395 if (len
!= PFKEY_UNUNIT64(ext
->sadb_ext_len
))
7398 /* if it does not match minimum/maximum length, bail */
7399 if (ext
->sadb_ext_type
>= sizeof(minsize
) / sizeof(minsize
[0]) ||
7400 ext
->sadb_ext_type
>= sizeof(maxsize
) / sizeof(maxsize
[0]))
7402 if (!minsize
[ext
->sadb_ext_type
] || len
< minsize
[ext
->sadb_ext_type
])
7404 if (maxsize
[ext
->sadb_ext_type
] && len
> maxsize
[ext
->sadb_ext_type
])
7407 /* more checks based on sadb_ext_type XXX need more */
7408 switch (ext
->sadb_ext_type
) {
7409 case SADB_EXT_ADDRESS_SRC
:
7410 case SADB_EXT_ADDRESS_DST
:
7411 case SADB_EXT_ADDRESS_PROXY
:
7412 baselen
= PFKEY_ALIGN8(sizeof(struct sadb_address
));
7415 case SADB_EXT_IDENTITY_SRC
:
7416 case SADB_EXT_IDENTITY_DST
:
7417 if (((struct sadb_ident
*)ext
)->sadb_ident_type
==
7418 SADB_X_IDENTTYPE_ADDR
) {
7419 baselen
= PFKEY_ALIGN8(sizeof(struct sadb_ident
));
7429 switch (checktype
) {
7433 sa
= (struct sockaddr
*)((caddr_t
)ext
+ baselen
);
7434 if (len
< baselen
+ sal
)
7436 if (baselen
+ PFKEY_ALIGN8(sa
->sa_len
) != len
)
7449 bzero((caddr_t
)&key_cb
, sizeof(key_cb
));
7451 for (i
= 0; i
< IPSEC_DIR_MAX
; i
++) {
7452 LIST_INIT(&sptree
[i
]);
7455 LIST_INIT(&sahtree
);
7457 for (i
= 0; i
<= SADB_SATYPE_MAX
; i
++) {
7458 LIST_INIT(®tree
[i
]);
7461 #ifndef IPSEC_NONBLOCK_ACQUIRE
7462 LIST_INIT(&acqtree
);
7464 LIST_INIT(&spacqtree
);
7466 /* system default */
7468 ip4_def_policy
.policy
= IPSEC_POLICY_NONE
;
7469 ip4_def_policy
.refcnt
++; /*never reclaim this*/
7472 ip6_def_policy
.policy
= IPSEC_POLICY_NONE
;
7473 ip6_def_policy
.refcnt
++; /*never reclaim this*/
7476 #ifndef IPSEC_DEBUG2
7477 timeout((void *)key_timehandler_funnel
, (void *)0, hz
);
7478 #endif /*IPSEC_DEBUG2*/
7480 /* initialize key statistics */
7481 keystat
.getspi_count
= 1;
7484 printf("IPsec: Initialized Security Association Processing.\n");
7491 * XXX: maybe This function is called after INBOUND IPsec processing.
7493 * Special check for tunnel-mode packets.
7494 * We must make some checks for consistency between inner and outer IP header.
7496 * xxx more checks to be provided
7499 key_checktunnelsanity(sav
, family
, src
, dst
)
7500 struct secasvar
*sav
;
7506 if (sav
->sah
== NULL
)
7507 panic("sav->sah == NULL at key_checktunnelsanity");
7509 /* XXX: check inner IP header */
7515 #define hostnamelen strlen(hostname)
7518 * Get FQDN for the host.
7519 * If the administrator configured hostname (by hostname(1)) without
7520 * domain name, returns nothing.
7527 static char fqdn
[MAXHOSTNAMELEN
+ 1];
7532 /* check if it comes with domain name. */
7534 for (i
= 0; i
< hostnamelen
; i
++) {
7535 if (hostname
[i
] == '.')
7541 /* NOTE: hostname may not be NUL-terminated. */
7542 bzero(fqdn
, sizeof(fqdn
));
7543 bcopy(hostname
, fqdn
, hostnamelen
);
7544 fqdn
[hostnamelen
] = '\0';
7549 * get username@FQDN for the host/user.
7555 static char userfqdn
[MAXHOSTNAMELEN
+ MAXLOGNAME
+ 2];
7556 struct proc
*p
= curproc
;
7559 if (!p
|| !p
->p_pgrp
|| !p
->p_pgrp
->pg_session
)
7561 if (!(host
= key_getfqdn()))
7564 /* NOTE: s_login may not be-NUL terminated. */
7565 bzero(userfqdn
, sizeof(userfqdn
));
7566 bcopy(p
->p_pgrp
->pg_session
->s_login
, userfqdn
, MAXLOGNAME
);
7567 userfqdn
[MAXLOGNAME
] = '\0'; /* safeguard */
7568 q
= userfqdn
+ strlen(userfqdn
);
7570 bcopy(host
, q
, strlen(host
));
7578 /* record data transfer on SA, and update timestamps */
7580 key_sa_recordxfer(sav
, m
)
7581 struct secasvar
*sav
;
7585 panic("key_sa_recordxfer called with sav == NULL");
7587 panic("key_sa_recordxfer called with m == NULL");
7592 * XXX Currently, there is a difference of bytes size
7593 * between inbound and outbound processing.
7595 sav
->lft_c
->sadb_lifetime_bytes
+= m
->m_pkthdr
.len
;
7596 /* to check bytes lifetime is done in key_timehandler(). */
7599 * We use the number of packets as the unit of
7600 * sadb_lifetime_allocations. We increment the variable
7601 * whenever {esp,ah}_{in,out}put is called.
7603 sav
->lft_c
->sadb_lifetime_allocations
++;
7604 /* XXX check for expires? */
7607 * NOTE: We record CURRENT sadb_lifetime_usetime by using wall clock,
7608 * in seconds. HARD and SOFT lifetime are measured by the time
7609 * difference (again in seconds) from sadb_lifetime_usetime.
7613 * -----+-----+--------+---> t
7614 * <--------------> HARD
7620 sav
->lft_c
->sadb_lifetime_usetime
= tv
.tv_sec
;
7621 /* XXX check for expires? */
7629 key_sa_routechange(dst
)
7630 struct sockaddr
*dst
;
7632 struct secashead
*sah
;
7635 LIST_FOREACH(sah
, &sahtree
, chain
) {
7636 ro
= &sah
->sa_route
;
7637 if (ro
->ro_rt
&& dst
->sa_len
== ro
->ro_dst
.sa_len
7638 && bcmp(dst
, &ro
->ro_dst
, dst
->sa_len
) == 0) {
7640 ro
->ro_rt
= (struct rtentry
*)NULL
;
7648 key_sa_chgstate(sav
, state
)
7649 struct secasvar
*sav
;
7653 panic("key_sa_chgstate called with sav == NULL");
7655 if (sav
->state
== state
)
7658 if (__LIST_CHAINED(sav
))
7659 LIST_REMOVE(sav
, chain
);
7662 LIST_INSERT_HEAD(&sav
->sah
->savtree
[state
], sav
, chain
);
7667 struct secasvar
*sav
;
7671 panic("key_sa_stir_iv called with sav == NULL");
7672 key_randomfill(sav
->iv
, sav
->ivlen
);
7676 static struct mbuf
*
7680 struct mbuf
*m
= NULL
, *n
;
7685 MGET(n
, M_DONTWAIT
, MT_DATA
);
7686 if (n
&& len
> MLEN
)
7687 MCLGET(n
, M_DONTWAIT
);
7695 n
->m_len
= M_TRAILINGSPACE(n
);
7696 /* use the bottom of mbuf, hoping we can prepend afterwards */
7697 if (n
->m_len
> len
) {
7698 t
= (n
->m_len
- len
) & ~(sizeof(long) - 1);