1 /* $KAME: key.c,v 1.76 2000/03/27 05:11:04 sumikawa Exp $ */
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * This code is referd to RFC 2367
36 #if (defined(__FreeBSD__) && __FreeBSD__ >= 3) || defined(__NetBSD__)
39 #include "opt_ipsec.h"
43 /* this is for backward compatibility. we should not touch those. */
44 #define ss_len __ss_len
45 #define ss_family __ss_family
47 #include <sys/types.h>
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
52 #include <sys/domain.h>
53 #include <sys/protosw.h>
54 #include <sys/malloc.h>
55 #include <sys/socket.h>
56 #include <sys/socketvar.h>
57 #if defined(__FreeBSD__) || defined (__APPLE__)
58 #include <sys/sysctl.h>
60 #include <sys/errno.h>
62 #include <sys/queue.h>
65 #include <net/route.h>
66 #include <net/raw_cb.h>
68 #include <netinet/in.h>
69 #include <netinet/in_systm.h>
70 #include <netinet/ip.h>
71 #include <netinet/in_var.h>
74 #include <netinet/ip6.h>
75 #include <netinet6/in6_var.h>
76 #include <netinet6/ip6_var.h>
80 #include <netinet/in_pcb.h>
83 #if !(defined(__bsdi__) && _BSDI_VERSION >= 199802)
84 #include <netinet6/in6_pcb.h>
88 #include <net/pfkeyv2.h>
89 #include <netkey/keydb.h>
90 #include <netkey/key.h>
91 #include <netkey/keysock.h>
92 #include <netkey/key_debug.h>
94 #include <netinet6/ipsec.h>
95 #include <netinet6/ah.h>
97 #include <netinet6/esp.h>
99 #include <netinet6/ipcomp.h>
101 #include <net/net_osdep.h>
104 * Note on SA reference counting:
105 * - SAs that are not in DEAD state will have (total external reference + 1)
106 * following value in reference count field. they cannot be freed and are
107 * referenced from SA header.
108 * - SAs that are in DEAD state will have (total external reference)
109 * in reference count field. they are ready to be freed. reference from
110 * SA header will be removed in key_delsav(), when the reference count
111 * field hits 0 (= no external reference other than from SA header.
114 u_int32_t key_debug_level
= 0; //### our sysctl is not dynamic
115 static u_int key_spi_trycnt
= 1000;
116 static u_int32_t key_spi_minval
= 0x100;
117 static u_int32_t key_spi_maxval
= 0x0fffffff; /* XXX */
118 static u_int32_t policy_id
= 0;
119 static u_int key_int_random
= 60; /*interval to initialize randseed,1(m)*/
120 static u_int key_larval_lifetime
= 30; /* interval to expire acquiring, 30(s)*/
121 static int key_blockacq_count
= 10; /* counter for blocking SADB_ACQUIRE.*/
122 static int key_blockacq_lifetime
= 20; /* lifetime for blocking SADB_ACQUIRE.*/
124 static u_int32_t acq_seq
= 0;
125 static int key_tick_init_random
= 0;
127 static LIST_HEAD(_sptree
, secpolicy
) sptree
[IPSEC_DIR_MAX
]; /* SPD */
128 static LIST_HEAD(_sahtree
, secashead
) sahtree
; /* SAD */
129 static LIST_HEAD(_regtree
, secreg
) regtree
[SADB_SATYPE_MAX
+ 1];
131 #ifndef IPSEC_NONBLOCK_ACQUIRE
132 static LIST_HEAD(_acqtree
, secacq
) acqtree
; /* acquiring list */
134 static LIST_HEAD(_spacqtree
, secspacq
) spacqtree
; /* SP acquiring list */
136 struct key_cb key_cb
;
138 /* search order for SAs */
139 static u_int saorder_state_valid
[] = {
140 SADB_SASTATE_DYING
, SADB_SASTATE_MATURE
,
142 * This order is important because we must select a oldest SA
143 * for outbound processing. For inbound, This is not important.
146 static u_int saorder_state_alive
[] = {
148 SADB_SASTATE_MATURE
, SADB_SASTATE_DYING
, SADB_SASTATE_LARVAL
150 static u_int saorder_state_any
[] = {
151 SADB_SASTATE_MATURE
, SADB_SASTATE_DYING
,
152 SADB_SASTATE_LARVAL
, SADB_SASTATE_DEAD
155 #if defined(__FreeBSD__) || defined (__APPLE__)
156 SYSCTL_DECL(_net_key
);
157 //#if defined(IPSEC_DEBUG)
158 SYSCTL_INT(_net_key
, KEYCTL_DEBUG_LEVEL
, debug
, CTLFLAG_RW
, \
159 &key_debug_level
, 0, "");
160 //#endif /* defined(IPSEC_DEBUG) */
162 /* max count of trial for the decision of spi value */
163 SYSCTL_INT(_net_key
, KEYCTL_SPI_TRY
, spi_trycnt
, CTLFLAG_RW
, \
164 &key_spi_trycnt
, 0, "");
166 /* minimum spi value to allocate automatically. */
167 SYSCTL_INT(_net_key
, KEYCTL_SPI_MIN_VALUE
, spi_minval
, CTLFLAG_RW
, \
168 &key_spi_minval
, 0, "");
170 /* maximun spi value to allocate automatically. */
171 SYSCTL_INT(_net_key
, KEYCTL_SPI_MAX_VALUE
, spi_maxval
, CTLFLAG_RW
, \
172 &key_spi_maxval
, 0, "");
174 /* interval to initialize randseed */
175 SYSCTL_INT(_net_key
, KEYCTL_RANDOM_INT
, int_random
, CTLFLAG_RW
, \
176 &key_int_random
, 0, "");
178 /* lifetime for larval SA */
179 SYSCTL_INT(_net_key
, KEYCTL_LARVAL_LIFETIME
, larval_lifetime
, CTLFLAG_RW
, \
180 &key_larval_lifetime
, 0, "");
182 /* counter for blocking to send SADB_ACQUIRE to IKEd */
183 SYSCTL_INT(_net_key
, KEYCTL_BLOCKACQ_COUNT
, blockacq_count
, CTLFLAG_RW
, \
184 &key_blockacq_count
, 0, "");
186 /* lifetime for blocking to send SADB_ACQUIRE to IKEd */
187 SYSCTL_INT(_net_key
, KEYCTL_BLOCKACQ_LIFETIME
, blockacq_lifetime
, CTLFLAG_RW
, \
188 &key_blockacq_lifetime
, 0, "");
190 #endif /* __FreeBSD__ */
193 #define LIST_FOREACH(elm, head, field) \
194 for (elm = LIST_FIRST(head); elm; elm = LIST_NEXT(elm, field))
196 #define __LIST_CHAINED(elm) \
197 (!((elm)->chain.le_next == NULL && (elm)->chain.le_prev == NULL))
198 #define LIST_INSERT_TAIL(head, elm, type, field) \
200 struct type *curelm = LIST_FIRST(head); \
201 if (curelm == NULL) {\
202 LIST_INSERT_HEAD(head, elm, field); \
204 while (LIST_NEXT(curelm, field)) \
205 curelm = LIST_NEXT(curelm, field);\
206 LIST_INSERT_AFTER(curelm, elm, field);\
210 #define KEY_CHKSASTATE(head, sav, name) \
212 if ((head) != (sav)) { \
213 printf("%s: state mismatched (TREE=%d SA=%d)\n", \
214 (name), (head), (sav)); \
219 #define KEY_CHKSPDIR(head, sp, name) \
221 if ((head) != (sp)) { \
222 printf("%s: direction mismatched (TREE=%d SP=%d), " \
223 "anyway continue.\n", \
224 (name), (head), (sp)); \
229 #define KMALLOC(p, t, n) \
230 ((p) = (t) _MALLOC((unsigned long)(n), M_SECA, M_NOWAIT))
232 _FREE((caddr_t)(p), M_SECA);
234 #define KMALLOC(p, t, n) \
236 ((p) = (t)_MALLOC((unsigned long)(n), M_SECA, M_NOWAIT)); \
237 printf("%s %d: %p <- KMALLOC(%s, %d)\n", \
238 __FILE__, __LINE__, (p), #t, n); \
243 printf("%s %d: %p -> KFREE()\n", __FILE__, __LINE__, (p)); \
244 _FREE((caddr_t)(p), M_SECA); \
249 * set parameters into secpolicyindex buffer.
250 * Must allocate secpolicyindex buffer passed to this function.
252 #define KEY_SETSECSPIDX(_dir, s, d, ps, pd, ulp, idx) \
254 bzero((idx), sizeof(struct secpolicyindex)); \
255 (idx)->dir = (_dir); \
256 (idx)->prefs = (ps); \
257 (idx)->prefd = (pd); \
258 (idx)->ul_proto = (ulp); \
259 bcopy((s), &(idx)->src, ((struct sockaddr *)(s))->sa_len); \
260 bcopy((d), &(idx)->dst, ((struct sockaddr *)(d))->sa_len); \
264 * set parameters into secasindex buffer.
265 * Must allocate secasindex buffer before calling this function.
267 #define KEY_SETSECASIDX(p, m, s, d, idx) \
269 bzero((idx), sizeof(struct secasindex)); \
270 (idx)->proto = (p); \
271 (idx)->mode = (m)->sadb_msg_mode; \
272 (idx)->reqid = (m)->sadb_msg_reqid; \
273 bcopy((s), &(idx)->src, ((struct sockaddr *)(s))->sa_len); \
274 bcopy((d), &(idx)->dst, ((struct sockaddr *)(d))->sa_len); \
279 u_long getspi_count
; /* the avarage of count to try to get new SPI */
282 static struct secasvar
*key_allocsa_policy
__P((struct secasindex
*saidx
));
283 static void key_freesp_so
__P((struct secpolicy
**sp
));
284 static struct secasvar
*key_do_allocsa_policy
__P((struct secashead
*sah
,
286 static void key_delsp
__P((struct secpolicy
*sp
));
287 static struct secpolicy
*key_getsp
__P((struct secpolicyindex
*spidx
));
288 static struct secpolicy
*key_getspbyid
__P((u_int32_t id
));
289 static u_int32_t key_newreqid
__P((void));
290 static struct sadb_msg
*key_spdadd
__P((caddr_t
*mhp
));
291 static u_int32_t key_getnewspid
__P((void));
292 static struct sadb_msg
*key_spddelete
__P((caddr_t
*mhp
));
293 static struct sadb_msg
*key_spddelete2
__P((caddr_t
*mhp
));
294 static int key_spdget
__P((caddr_t
*mhp
, struct socket
*so
, int target
));
295 static struct sadb_msg
*key_spdflush
__P((caddr_t
*mhp
));
296 static int key_spddump
__P((caddr_t
*mhp
, struct socket
*so
, int target
));
297 static struct mbuf
*key_setdumpsp
__P((struct secpolicy
*sp
,
298 u_int8_t type
, u_int32_t seq
, u_int32_t pid
));
299 static u_int key_getspmsglen
__P((struct secpolicy
*sp
));
300 static u_int key_getspreqmsglen
__P((struct secpolicy
*sp
));
301 static struct secashead
*key_newsah
__P((struct secasindex
*saidx
));
302 static void key_delsah
__P((struct secashead
*sah
));
303 static struct secasvar
*key_newsav
__P((caddr_t
*mhp
, struct secashead
*sah
));
304 static void key_delsav
__P((struct secasvar
*sav
));
305 static struct secashead
*key_getsah
__P((struct secasindex
*saidx
));
306 static struct secasvar
*key_checkspidup
__P((struct secasindex
*saidx
,
308 static struct secasvar
*key_getsavbyspi
__P((struct secashead
*sah
,
310 static int key_setsaval
__P((struct secasvar
*sav
, caddr_t
*mhp
));
311 static u_int key_getmsglen
__P((struct secasvar
*sav
));
312 static int key_mature
__P((struct secasvar
*sav
));
313 static u_int key_setdumpsa
__P((struct sadb_msg
*newmsg
, struct secasvar
*sav
,
314 u_int8_t type
, u_int8_t satype
,
315 u_int32_t seq
, u_int32_t pid
));
317 static int key_setsadbmsg_m
__P((struct mbuf
*, u_int8_t type
, int tlen
,
318 u_int8_t satype
, u_int32_t seq
, pid_t pid
,
319 u_int8_t mode
, u_int32_t reqid
,
320 u_int8_t reserved1
, u_int32_t reserved2
));
322 static caddr_t key_setsadbmsg
__P((caddr_t buf
, u_int8_t type
, int tlen
,
323 u_int8_t satype
, u_int32_t seq
, pid_t pid
,
324 u_int8_t mode
, u_int32_t reqid
,
325 u_int8_t reserved1
, u_int32_t reserved2
));
326 static caddr_t key_setsadbsa
__P((caddr_t buf
, struct secasvar
*sav
));
328 static int key_setsadbaddr_m
__P((struct mbuf
*m
, u_int16_t exttype
,
329 struct sockaddr
*saddr
, u_int8_t prefixlen
, u_int16_t ul_proto
));
331 static caddr_t key_setsadbaddr
__P((caddr_t buf
, u_int16_t exttype
,
332 struct sockaddr
*saddr
, u_int8_t prefixlen
, u_int16_t ul_proto
));
333 static caddr_t key_setsadbident
334 __P((caddr_t buf
, u_int16_t exttype
, u_int16_t idtype
,
335 caddr_t string
, int stringlen
, u_int64_t id
));
336 static caddr_t key_setsadbxpolicy
337 __P((caddr_t buf
, u_int16_t type
, u_int8_t dir
, u_int32_t id
));
338 static caddr_t key_setsadbext
__P((caddr_t p
, caddr_t ext
));
339 static void *key_newbuf
__P((void *src
, u_int len
));
341 static int key_ismyaddr6
__P((caddr_t addr
));
344 static int key_isloopback
__P((u_int family
, caddr_t addr
));
346 static int key_cmpsaidx_exactly
347 __P((struct secasindex
*saidx0
, struct secasindex
*saidx1
));
348 static int key_cmpsaidx_withmode
349 __P((struct secasindex
*saidx0
, struct secasindex
*saidx1
));
350 static int key_cmpspidx_exactly
351 __P((struct secpolicyindex
*spidx0
, struct secpolicyindex
*spidx1
));
352 static int key_cmpspidx_withmask
353 __P((struct secpolicyindex
*spidx0
, struct secpolicyindex
*spidx1
));
354 static int key_bbcmp
__P((caddr_t p1
, caddr_t p2
, u_int bits
));
355 static u_int16_t key_satype2proto
__P((u_int8_t satype
));
356 static u_int8_t key_proto2satype
__P((u_int16_t proto
));
358 static struct sadb_msg
*key_getspi
__P((caddr_t
*mhp
));
359 static u_int32_t key_do_getnewspi
__P((struct sadb_spirange
*spirange
,
360 struct secasindex
*saidx
));
361 static struct sadb_msg
*key_update
__P((caddr_t
*mhp
));
362 #ifdef IPSEC_DOSEQCHECK
363 static struct secasvar
*key_getsavbyseq
__P((struct secashead
*sah
,
366 static struct sadb_msg
*key_add
__P((caddr_t
*mhp
));
367 static int key_setident
__P((struct secashead
*sah
, caddr_t
*mhp
));
368 static struct sadb_msg
*key_getmsgbuf_x1
__P((caddr_t
*mhp
));
369 static struct sadb_msg
*key_delete
__P((caddr_t
*mhp
));
370 static struct sadb_msg
*key_get
__P((caddr_t
*mhp
));
371 static int key_acquire
__P((struct secasindex
*, struct secpolicy
*));
372 static struct secacq
*key_newacq
__P((struct secasindex
*saidx
));
373 static struct secacq
*key_getacq
__P((struct secasindex
*saidx
));
374 static struct secacq
*key_getacqbyseq
__P((u_int32_t seq
));
375 static struct secspacq
*key_newspacq
__P((struct secpolicyindex
*spidx
));
376 static struct secspacq
*key_getspacq
__P((struct secpolicyindex
*spidx
));
377 static struct sadb_msg
*key_acquire2
__P((caddr_t
*mhp
));
378 static struct sadb_msg
*key_register
__P((caddr_t
*mhp
, struct socket
*so
));
379 static int key_expire
__P((struct secasvar
*sav
));
380 static struct sadb_msg
*key_flush
__P((caddr_t
*mhp
));
381 static int key_dump
__P((caddr_t
*mhp
, struct socket
*so
, int target
));
382 static void key_promisc
__P((caddr_t
*mhp
, struct socket
*so
));
383 static int key_sendall
__P((struct sadb_msg
*msg
, u_int len
));
384 static int key_align
__P((struct sadb_msg
*msg
, caddr_t
*mhp
));
386 static const char *key_getfqdn
__P((void));
387 static const char *key_getuserfqdn
__P((void));
389 static void key_sa_chgstate
__P((struct secasvar
*sav
, u_int8_t state
));
390 static caddr_t key_appendmbuf
__P((struct mbuf
*, int));
392 /* %%% IPsec policy management */
394 * allocating a SP for OUTBOUND or INBOUND packet.
395 * Must call key_freesp() later.
396 * OUT: NULL: not found
397 * others: found and return the pointer.
400 key_allocsp(spidx
, dir
)
401 struct secpolicyindex
*spidx
;
404 struct secpolicy
*sp
;
409 panic("key_allocsp: NULL pointer is passed.\n");
411 /* check direction */
413 case IPSEC_DIR_INBOUND
:
414 case IPSEC_DIR_OUTBOUND
:
417 panic("key_allocsp: Invalid direction is passed.\n");
422 s
= splsoftnet(); /*called from softclock()*/
424 s
= splnet(); /*called from softclock()*/
426 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
427 printf("*** objects\n");
428 kdebug_secpolicyindex(spidx
));
430 LIST_FOREACH(sp
, &sptree
[dir
], chain
) {
431 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
432 printf("*** in SPD\n");
433 kdebug_secpolicyindex(&sp
->spidx
));
435 if (sp
->state
== IPSEC_SPSTATE_DEAD
)
437 if (key_cmpspidx_withmask(&sp
->spidx
, spidx
))
446 KEY_CHKSPDIR(sp
->spidx
.dir
, dir
, "key_allocsp");
448 /* found a SPD entry */
451 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
452 printf("DP key_allocsp cause refcnt++:%d SP:%p\n",
459 * allocating a SA entry for a *OUTBOUND* packet.
460 * checking each request entries in SP, and acquire SA if need.
461 * OUT: 0: there are valid requests.
462 * ENOENT: policy may be valid, but SA with REQUIRE is on acquiring.
465 key_checkrequest(isr
, saidx
)
466 struct ipsecrequest
*isr
;
467 struct secasindex
*saidx
;
473 if (isr
== NULL
|| saidx
== NULL
)
474 panic("key_checkrequest: NULL pointer is passed.\n");
477 switch (saidx
->mode
) {
478 case IPSEC_MODE_TRANSPORT
:
479 case IPSEC_MODE_TUNNEL
:
483 panic("key_checkrequest: Invalid policy defined.\n");
486 /* get current level */
487 level
= ipsec_get_reqlevel(isr
);
491 * We do allocate new SA only if the state of SA in the holder is
492 * SADB_SASTATE_DEAD. The SA for outbound must be the oldest.
494 if (isr
->sav
!= NULL
) {
495 if (isr
->sav
->sah
== NULL
)
496 panic("key_checkrequest: sah is null.\n");
497 if (isr
->sav
== (struct secasvar
*)LIST_FIRST(
498 &isr
->sav
->sah
->savtree
[SADB_SASTATE_DEAD
])) {
499 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
500 printf("DP checkrequest calls free SA:%p\n",
502 key_freesav(isr
->sav
);
508 * we free any SA stashed in the IPsec request because a different
509 * SA may be involved each time this request is checked, either
510 * because new SAs are being configured, or this request is
511 * associated with an unconnected datagram socket, or this request
512 * is associated with a system default policy.
514 * The operation may have negative impact to performance. We may
515 * want to check cached SA carefully, rather than picking new SA
518 if (isr
->sav
!= NULL
) {
519 key_freesav(isr
->sav
);
525 * new SA allocation if no SA found.
526 * key_allocsa_policy should allocate the oldest SA available.
527 * See key_do_allocsa_policy(), and draft-jenkins-ipsec-rekeying-03.txt.
529 if (isr
->sav
== NULL
)
530 isr
->sav
= key_allocsa_policy(saidx
);
532 /* When there is SA. */
533 if (isr
->sav
!= NULL
)
537 if ((error
= key_acquire(saidx
, isr
->sp
)) != 0) {
538 /* XXX What I do ? */
540 printf("key_checkrequest: error %d returned "
541 "from key_acquire.\n", error
);
546 return level
== IPSEC_LEVEL_REQUIRE
? ENOENT
: 0;
550 * allocating a SA for policy entry from SAD.
551 * NOTE: searching SAD of aliving state.
552 * OUT: NULL: not found.
553 * others: found and return the pointer.
555 static struct secasvar
*
556 key_allocsa_policy(saidx
)
557 struct secasindex
*saidx
;
559 struct secashead
*sah
;
560 struct secasvar
*sav
;
561 u_int stateidx
, state
;
563 LIST_FOREACH(sah
, &sahtree
, chain
) {
564 if (sah
->state
== SADB_SASTATE_DEAD
)
566 if (key_cmpsaidx_withmode(&sah
->saidx
, saidx
))
574 /* search valid state */
576 stateidx
< _ARRAYLEN(saorder_state_valid
);
579 state
= saorder_state_valid
[stateidx
];
581 sav
= key_do_allocsa_policy(sah
, state
);
590 * searching SAD with direction, protocol, mode and state.
591 * called by key_allocsa_policy().
594 * others : found, pointer to a SA.
596 static struct secasvar
*
597 key_do_allocsa_policy(sah
, state
)
598 struct secashead
*sah
;
601 struct secasvar
*sav
, *candidate
;
606 LIST_FOREACH(sav
, &sah
->savtree
[state
], chain
) {
609 KEY_CHKSASTATE(sav
->state
, state
, "key_do_allocsa_policy");
612 if (candidate
== NULL
) {
617 /* Which SA is the better ? */
620 if (candidate
->lft_c
== NULL
|| sav
->lft_c
== NULL
)
621 panic("key_do_allocsa_policy: "
622 "lifetime_current is NULL.\n");
624 /* XXX What the best method is to compare ? */
625 if (candidate
->lft_c
->sadb_lifetime_addtime
>
626 sav
->lft_c
->sadb_lifetime_addtime
) {
634 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
635 printf("DP allocsa_policy cause "
636 "refcnt++:%d SA:%p\n",
637 candidate
->refcnt
, candidate
));
643 * allocating a SA entry for a *INBOUND* packet.
644 * Must call key_freesav() later.
645 * OUT: positive: pointer to a sav.
646 * NULL: not found, or error occured.
648 * In the comparison, source address will be ignored for RFC2401 conformance.
649 * To quote, from section 4.1:
650 * A security association is uniquely identified by a triple consisting
651 * of a Security Parameter Index (SPI), an IP Destination Address, and a
652 * security protocol (AH or ESP) identifier.
653 * Note that, however, we do need to keep source address in IPsec SA.
654 * IPsec SA. IKE specification and PF_KEY specification do assume that we
655 * keep source address in IPsec SA. We see a tricky situation here.
658 key_allocsa(family
, src
, dst
, proto
, spi
)
663 struct secashead
*sah
;
664 struct secasvar
*sav
;
665 u_int stateidx
, state
;
669 if (src
== NULL
|| dst
== NULL
)
670 panic("key_allocsa: NULL pointer is passed.\n");
674 * XXX: to be checked internal IP header somewhere. Also when
675 * IPsec tunnel packet is received. But ESP tunnel mode is
676 * encrypted so we can't check internal IP header.
679 s
= splsoftnet(); /*called from softclock()*/
681 s
= splnet(); /*called from softclock()*/
683 LIST_FOREACH(sah
, &sahtree
, chain
) {
685 /* search valid state */
687 stateidx
< _ARRAYLEN(saorder_state_valid
);
690 state
= saorder_state_valid
[stateidx
];
691 LIST_FOREACH(sav
, &sah
->savtree
[state
], chain
) {
694 KEY_CHKSASTATE(sav
->state
, state
, "key_allocsav");
695 if (proto
!= sav
->sah
->saidx
.proto
)
700 #if 0 /* don't check src */
702 _INADDRBYSA(&sav
->sah
->saidx
.src
),
703 _INALENBYAF(sav
->sah
->saidx
.src
.ss_family
) << 3))
707 _INADDRBYSA(&sav
->sah
->saidx
.dst
),
708 _INALENBYAF(sav
->sah
->saidx
.dst
.ss_family
) << 3))
723 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
724 printf("DP allocsa cause refcnt++:%d SA:%p\n",
730 * Must be called after calling key_allocsp().
731 * For both the packet without socket and key_freeso().
735 struct secpolicy
*sp
;
739 panic("key_freesp: NULL pointer is passed.\n");
742 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
743 printf("DP freesp cause refcnt--:%d SP:%p\n",
753 * Must be called after calling key_allocsp().
754 * For the packet with socket.
762 panic("key_freeso: NULL pointer is passed.\n");
764 switch (so
->so_proto
->pr_domain
->dom_family
) {
768 struct inpcb
*pcb
= sotoinpcb(so
);
770 /* Does it have a PCB ? */
773 key_freesp_so(&pcb
->inp_sp
->sp_in
);
774 key_freesp_so(&pcb
->inp_sp
->sp_out
);
782 struct inpcb
*pcb
= sotoinpcb(so
);
784 /* Does it have a PCB ? */
787 key_freesp_so(&pcb
->inp_sp
->sp_in
);
788 key_freesp_so(&pcb
->inp_sp
->sp_out
);
790 struct in6pcb
*pcb
= sotoin6pcb(so
);
792 /* Does it have a PCB ? */
795 key_freesp_so(&pcb
->in6p_sp
->sp_in
);
796 key_freesp_so(&pcb
->in6p_sp
->sp_out
);
803 printf("key_freeso: unknown address family=%d.\n",
804 so
->so_proto
->pr_domain
->dom_family
);
814 struct secpolicy
**sp
;
817 if (sp
== NULL
|| *sp
== NULL
)
818 panic("key_freesp_so: sp == NULL\n");
820 switch ((*sp
)->policy
) {
821 case IPSEC_POLICY_IPSEC
:
822 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
823 printf("DP freeso calls free SP:%p\n", *sp
));
827 case IPSEC_POLICY_ENTRUST
:
828 case IPSEC_POLICY_BYPASS
:
831 panic("key_freesp_so: Invalid policy found %d", (*sp
)->policy
);
838 * Must be called after calling key_allocsa().
839 * This function is called by key_freesp() to free some SA allocated
844 struct secasvar
*sav
;
848 panic("key_freesav: NULL pointer is passed.\n");
851 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
852 printf("DP freesav cause refcnt--:%d SA:%p SPI %d\n",
853 sav
->refcnt
, sav
, (u_int32_t
)ntohl(sav
->spi
)));
855 if (sav
->refcnt
== 0)
861 /* %%% SPD management */
863 * free security policy entry.
867 struct secpolicy
*sp
;
873 panic("key_delsp: NULL pointer is passed.\n");
875 sp
->state
= IPSEC_SPSTATE_DEAD
;
878 return; /* can't free */
881 s
= splsoftnet(); /*called from softclock()*/
883 s
= splnet(); /*called from softclock()*/
885 /* remove from SP index */
886 if (__LIST_CHAINED(sp
))
887 LIST_REMOVE(sp
, chain
);
890 struct ipsecrequest
*isr
= sp
->req
, *nextisr
;
892 while (isr
!= NULL
) {
893 if (isr
->sav
!= NULL
) {
894 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
895 printf("DP delsp calls free SA:%p\n",
897 key_freesav(isr
->sav
);
907 keydb_delsecpolicy(sp
);
916 * OUT: NULL : not found
917 * others : found, pointer to a SP.
919 static struct secpolicy
*
921 struct secpolicyindex
*spidx
;
923 struct secpolicy
*sp
;
927 panic("key_getsp: NULL pointer is passed.\n");
929 LIST_FOREACH(sp
, &sptree
[spidx
->dir
], chain
) {
930 if (sp
->state
== IPSEC_SPSTATE_DEAD
)
932 if (key_cmpspidx_exactly(spidx
, &sp
->spidx
)) {
943 * OUT: NULL : not found
944 * others : found, pointer to a SP.
946 static struct secpolicy
*
950 struct secpolicy
*sp
;
952 LIST_FOREACH(sp
, &sptree
[IPSEC_DIR_INBOUND
], chain
) {
953 if (sp
->state
== IPSEC_SPSTATE_DEAD
)
961 LIST_FOREACH(sp
, &sptree
[IPSEC_DIR_OUTBOUND
], chain
) {
962 if (sp
->state
== IPSEC_SPSTATE_DEAD
)
976 struct secpolicy
*newsp
= NULL
;
978 newsp
= keydb_newsecpolicy();
989 * create secpolicy structure from sadb_x_policy structure.
990 * NOTE: `state', `secpolicyindex' in secpolicy structure are not set,
991 * so must be set properly later.
994 key_msg2sp(xpl0
, len
, error
)
995 struct sadb_x_policy
*xpl0
;
999 struct secpolicy
*newsp
;
1003 panic("key_msg2sp: NULL pointer was passed.\n");
1004 if (len
< sizeof(*xpl0
))
1005 panic("key_msg2sp: invalid length.\n");
1006 if (len
!= PFKEY_EXTLEN(xpl0
)) {
1008 printf("key_msg2sp: Invalid msg length.\n");
1014 if ((newsp
= key_newsp()) == NULL
) {
1019 newsp
->spidx
.dir
= xpl0
->sadb_x_policy_dir
;
1020 newsp
->policy
= xpl0
->sadb_x_policy_type
;
1023 switch (xpl0
->sadb_x_policy_type
) {
1024 case IPSEC_POLICY_DISCARD
:
1025 case IPSEC_POLICY_NONE
:
1026 case IPSEC_POLICY_ENTRUST
:
1027 case IPSEC_POLICY_BYPASS
:
1031 case IPSEC_POLICY_IPSEC
:
1034 struct sadb_x_ipsecrequest
*xisr
;
1035 struct ipsecrequest
**p_isr
= &newsp
->req
;
1037 /* validity check */
1038 if (PFKEY_EXTLEN(xpl0
) < sizeof(*xpl0
)) {
1040 printf("key_msg2sp: Invalid msg length.\n");
1047 tlen
= PFKEY_EXTLEN(xpl0
) - sizeof(*xpl0
);
1048 xisr
= (struct sadb_x_ipsecrequest
*)(xpl0
+ 1);
1053 if (xisr
->sadb_x_ipsecrequest_len
< sizeof(*xisr
)) {
1055 printf("key_msg2sp: "
1056 "invalid ipsecrequest length.\n");
1063 /* allocate request buffer */
1064 KMALLOC(*p_isr
, struct ipsecrequest
*, sizeof(**p_isr
));
1065 if ((*p_isr
) == NULL
) {
1067 printf("key_msg2sp: No more memory.\n");
1073 bzero(*p_isr
, sizeof(**p_isr
));
1076 (*p_isr
)->next
= NULL
;
1078 switch (xisr
->sadb_x_ipsecrequest_proto
) {
1081 #if 1 /*nonstandard*/
1082 case IPPROTO_IPCOMP
:
1087 printf("key_msg2sp: invalid proto type=%u\n",
1088 xisr
->sadb_x_ipsecrequest_proto
);
1091 *error
= EPROTONOSUPPORT
;
1094 (*p_isr
)->saidx
.proto
= xisr
->sadb_x_ipsecrequest_proto
;
1096 switch (xisr
->sadb_x_ipsecrequest_mode
) {
1097 case IPSEC_MODE_TRANSPORT
:
1098 case IPSEC_MODE_TUNNEL
:
1100 case IPSEC_MODE_ANY
:
1103 printf("key_msg2sp: invalid mode=%u\n",
1104 xisr
->sadb_x_ipsecrequest_mode
);
1110 (*p_isr
)->saidx
.mode
= xisr
->sadb_x_ipsecrequest_mode
;
1112 switch (xisr
->sadb_x_ipsecrequest_level
) {
1113 case IPSEC_LEVEL_DEFAULT
:
1114 case IPSEC_LEVEL_USE
:
1115 case IPSEC_LEVEL_REQUIRE
:
1117 case IPSEC_LEVEL_UNIQUE
:
1118 /* validity check */
1120 * If range violation of reqid, kernel will
1121 * update it, don't refuse it.
1123 if (xisr
->sadb_x_ipsecrequest_reqid
1124 > IPSEC_MANUAL_REQID_MAX
) {
1126 printf("key_msg2sp: reqid=%d "
1128 "updated by kernel.\n",
1129 xisr
->sadb_x_ipsecrequest_reqid
);
1131 xisr
->sadb_x_ipsecrequest_reqid
= 0;
1134 /* allocate new reqid id if reqid is zero. */
1135 if (xisr
->sadb_x_ipsecrequest_reqid
== 0) {
1137 if ((reqid
= key_newreqid()) == 0) {
1142 (*p_isr
)->saidx
.reqid
= reqid
;
1143 xisr
->sadb_x_ipsecrequest_reqid
= reqid
;
1145 /* set it for manual keying. */
1146 (*p_isr
)->saidx
.reqid
=
1147 xisr
->sadb_x_ipsecrequest_reqid
;
1153 printf("key_msg2sp: invalid level=%u\n",
1154 xisr
->sadb_x_ipsecrequest_level
);
1160 (*p_isr
)->level
= xisr
->sadb_x_ipsecrequest_level
;
1162 /* set IP addresses if there */
1163 if (xisr
->sadb_x_ipsecrequest_len
> sizeof(*xisr
)) {
1164 struct sockaddr
*paddr
;
1166 paddr
= (struct sockaddr
*)(xisr
+ 1);
1168 /* validity check */
1170 > sizeof((*p_isr
)->saidx
.src
)) {
1172 printf("key_msg2sp: invalid request "
1173 "address length.\n");
1179 bcopy(paddr
, &(*p_isr
)->saidx
.src
,
1182 paddr
= (struct sockaddr
*)((caddr_t
)paddr
1185 /* validity check */
1187 > sizeof((*p_isr
)->saidx
.dst
)) {
1189 printf("key_msg2sp: invalid request "
1190 "address length.\n");
1196 bcopy(paddr
, &(*p_isr
)->saidx
.dst
,
1200 (*p_isr
)->sav
= NULL
;
1201 (*p_isr
)->sp
= newsp
;
1203 /* initialization for the next. */
1204 p_isr
= &(*p_isr
)->next
;
1205 tlen
-= xisr
->sadb_x_ipsecrequest_len
;
1207 /* validity check */
1210 printf("key_msg2sp: becoming tlen < 0.\n");
1217 xisr
= (struct sadb_x_ipsecrequest
*)((caddr_t
)xisr
1218 + xisr
->sadb_x_ipsecrequest_len
);
1224 printf("key_msg2sp: invalid policy type.\n");
1238 static u_int32_t auto_reqid
= IPSEC_MANUAL_REQID_MAX
+ 1;
1240 auto_reqid
= (auto_reqid
== ~0
1241 ? IPSEC_MANUAL_REQID_MAX
+ 1 : auto_reqid
+ 1);
1243 /* XXX should be unique check */
1249 * copy secpolicy struct to sadb_x_policy structure indicated.
1253 struct secpolicy
*sp
;
1255 struct sadb_x_policy
*xpl
;
1262 panic("key_sp2msg: NULL pointer was passed.\n");
1264 tlen
= key_getspreqmsglen(sp
);
1266 MGET(m
, M_DONTWAIT
, MT_DATA
);
1267 if (m
&& MLEN
< tlen
) {
1268 MCLGET(m
, M_DONTWAIT
);
1269 if ((m
->m_flags
& M_EXT
) == 0) {
1275 if (!m
|| M_TRAILINGSPACE(m
) < tlen
) {
1277 printf("key_sp2msg: No more memory.\n");
1286 xpl
= mtod(m
, struct sadb_x_policy
*);
1289 xpl
->sadb_x_policy_len
= PFKEY_UNIT64(tlen
);
1290 xpl
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
1291 xpl
->sadb_x_policy_type
= sp
->policy
;
1292 xpl
->sadb_x_policy_dir
= sp
->spidx
.dir
;
1293 xpl
->sadb_x_policy_id
= sp
->id
;
1294 p
= (caddr_t
)xpl
+ sizeof(*xpl
);
1296 /* if is the policy for ipsec ? */
1297 if (sp
->policy
== IPSEC_POLICY_IPSEC
) {
1298 struct sadb_x_ipsecrequest
*xisr
;
1299 struct ipsecrequest
*isr
;
1301 for (isr
= sp
->req
; isr
!= NULL
; isr
= isr
->next
) {
1303 xisr
= (struct sadb_x_ipsecrequest
*)p
;
1305 xisr
->sadb_x_ipsecrequest_proto
= isr
->saidx
.proto
;
1306 xisr
->sadb_x_ipsecrequest_mode
= isr
->saidx
.mode
;
1307 xisr
->sadb_x_ipsecrequest_level
= isr
->level
;
1308 xisr
->sadb_x_ipsecrequest_reqid
= isr
->saidx
.reqid
;
1311 bcopy(&isr
->saidx
.src
, p
, isr
->saidx
.src
.ss_len
);
1312 p
+= isr
->saidx
.src
.ss_len
;
1313 bcopy(&isr
->saidx
.dst
, p
, isr
->saidx
.dst
.ss_len
);
1314 p
+= isr
->saidx
.src
.ss_len
;
1316 xisr
->sadb_x_ipsecrequest_len
=
1317 PFKEY_ALIGN8(sizeof(*xisr
)
1318 + isr
->saidx
.src
.ss_len
1319 + isr
->saidx
.dst
.ss_len
);
1327 * SADB_X_SPDADD, SADB_X_SPDSETIDX or SADB_X_SPDUPDATE processing
1328 * add a entry to SP database, when received
1329 * <base, address(SD), policy>
1331 * Adding to SP database,
1333 * <base, address(SD), policy>
1334 * to the socket which was send.
1336 * SPDADD set a unique policy entry.
1337 * SPDSETIDX like SPDADD without a part of policy requests.
1338 * SPDUPDATE replace a unique policy entry.
1340 * IN: mhp: pointer to the pointer to each header.
1341 * OUT: NULL if fail.
1342 * other if success, return pointer to the message to send.
1345 static struct sadb_msg
*
1349 struct sadb_msg
*msg0
;
1350 struct sadb_address
*src0
, *dst0
;
1351 struct sadb_x_policy
*xpl0
;
1352 struct secpolicyindex spidx
;
1353 struct secpolicy
*newsp
;
1357 if (mhp
== NULL
|| mhp
[0] == NULL
)
1358 panic("key_spdadd: NULL pointer is passed.\n");
1360 msg0
= (struct sadb_msg
*)mhp
[0];
1362 if (mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
1363 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
1364 || mhp
[SADB_X_EXT_POLICY
] == NULL
) {
1366 printf("key_spdadd: invalid message is passed.\n");
1368 msg0
->sadb_msg_errno
= EINVAL
;
1372 src0
= (struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_SRC
];
1373 dst0
= (struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_DST
];
1374 xpl0
= (struct sadb_x_policy
*)mhp
[SADB_X_EXT_POLICY
];
1377 KEY_SETSECSPIDX(xpl0
->sadb_x_policy_dir
,
1380 src0
->sadb_address_prefixlen
,
1381 dst0
->sadb_address_prefixlen
,
1382 src0
->sadb_address_proto
,
1385 /* checking the direciton. */
1386 switch (xpl0
->sadb_x_policy_dir
) {
1387 case IPSEC_DIR_INBOUND
:
1388 case IPSEC_DIR_OUTBOUND
:
1392 printf("key_spdadd: Invalid SP direction.\n");
1394 msg0
->sadb_msg_errno
= EINVAL
;
1399 /* key_spdadd() accepts DISCARD, NONE and IPSEC. */
1400 if (xpl0
->sadb_x_policy_type
== IPSEC_POLICY_ENTRUST
1401 || xpl0
->sadb_x_policy_type
== IPSEC_POLICY_BYPASS
) {
1403 printf("key_spdadd: Invalid policy type.\n");
1405 msg0
->sadb_msg_errno
= EINVAL
;
1409 /* policy requests are mandatory when action is ipsec. */
1410 if (msg0
->sadb_msg_type
!= SADB_X_SPDSETIDX
1411 && xpl0
->sadb_x_policy_type
== IPSEC_POLICY_IPSEC
1412 && PFKEY_EXTLEN(xpl0
) <= sizeof(*xpl0
)) {
1414 printf("key_spdadd: some policy requests part required.\n");
1416 msg0
->sadb_msg_errno
= EINVAL
;
1421 * checking there is SP already or not.
1422 * If type is SPDUPDATE and no SP found, then error.
1423 * If type is either SPDADD or SPDSETIDX and SP found, then error.
1425 newsp
= key_getsp(&spidx
);
1426 if (msg0
->sadb_msg_type
== SADB_X_SPDUPDATE
) {
1427 if (newsp
== NULL
) {
1429 printf("key_spdadd: no SP found.\n");
1431 msg0
->sadb_msg_errno
= ENOENT
;
1435 newsp
->state
= IPSEC_SPSTATE_DEAD
;
1438 if (newsp
!= NULL
) {
1441 printf("key_spdadd: a SP entry exists already.\n");
1443 msg0
->sadb_msg_errno
= EEXIST
;
1448 /* allocation new SP entry */
1449 if ((newsp
= key_msg2sp(xpl0
, PFKEY_EXTLEN(xpl0
), &error
)) == NULL
) {
1450 msg0
->sadb_msg_errno
= error
;
1454 if ((newsp
->id
= key_getnewspid()) == 0) {
1455 msg0
->sadb_msg_errno
= ENOBUFS
;
1456 keydb_delsecpolicy(newsp
);
1460 KEY_SETSECSPIDX(xpl0
->sadb_x_policy_dir
,
1463 src0
->sadb_address_prefixlen
,
1464 dst0
->sadb_address_prefixlen
,
1465 src0
->sadb_address_proto
,
1468 /* sanity check on addr pair */
1469 if (((struct sockaddr
*)(src0
+ 1))->sa_family
!=
1470 ((struct sockaddr
*)(dst0
+ 1))->sa_family
) {
1471 msg0
->sadb_msg_errno
= EINVAL
;
1472 keydb_delsecpolicy(newsp
);
1476 if (newsp
->req
&& newsp
->req
->saidx
.src
.ss_family
) {
1477 struct sockaddr
*sa
;
1478 sa
= (struct sockaddr
*)(src0
+ 1);
1479 if (sa
->sa_family
!= newsp
->req
->saidx
.src
.ss_family
) {
1480 msg0
->sadb_msg_errno
= EINVAL
;
1481 keydb_delsecpolicy(newsp
);
1485 if (newsp
->req
&& newsp
->req
->saidx
.dst
.ss_family
) {
1486 struct sockaddr
*sa
;
1487 sa
= (struct sockaddr
*)(dst0
+ 1);
1488 if (sa
->sa_family
!= newsp
->req
->saidx
.dst
.ss_family
) {
1489 msg0
->sadb_msg_errno
= EINVAL
;
1490 keydb_delsecpolicy(newsp
);
1496 newsp
->refcnt
= 1; /* do not reclaim until I say I do */
1497 newsp
->state
= IPSEC_SPSTATE_ALIVE
;
1498 LIST_INSERT_HEAD(&sptree
[newsp
->spidx
.dir
], newsp
, chain
);
1500 /* delete the entry in spacqtree */
1501 if (msg0
->sadb_msg_type
== SADB_X_SPDUPDATE
) {
1502 struct secspacq
*spacq
;
1503 if ((spacq
= key_getspacq(&spidx
)) != NULL
) {
1504 /* reset counter in order to deletion by timehander. */
1505 spacq
->tick
= key_blockacq_lifetime
;
1511 struct sadb_msg
*newmsg
;
1515 /* create new sadb_msg to reply. */
1516 len
= sizeof(struct sadb_msg
)
1517 + PFKEY_EXTLEN(mhp
[SADB_X_EXT_POLICY
])
1518 + PFKEY_EXTLEN(mhp
[SADB_EXT_ADDRESS_SRC
])
1519 + PFKEY_EXTLEN(mhp
[SADB_EXT_ADDRESS_DST
]);
1521 KMALLOC(newmsg
, struct sadb_msg
*, len
);
1522 if (newmsg
== NULL
) {
1524 printf("key_spdadd: No more memory.\n");
1526 /* newsp persists in the kernel */
1527 msg0
->sadb_msg_errno
= ENOBUFS
;
1530 bzero((caddr_t
)newmsg
, len
);
1532 bcopy((caddr_t
)msg0
, (caddr_t
)newmsg
, sizeof(*msg0
));
1533 newmsg
->sadb_msg_errno
= 0;
1534 newmsg
->sadb_msg_len
= PFKEY_UNIT64(len
);
1535 p
= (caddr_t
)newmsg
+ sizeof(*msg0
);
1538 * reqid may had been updated at key_msg2sp() if reqid's
1541 ((struct sadb_x_policy
*)mhp
[SADB_X_EXT_POLICY
])->sadb_x_policy_id
= newsp
->id
;
1542 p
= key_setsadbext(p
, mhp
[SADB_X_EXT_POLICY
]);
1544 p
= key_setsadbext(p
, mhp
[SADB_EXT_ADDRESS_SRC
]);
1545 p
= key_setsadbext(p
, mhp
[SADB_EXT_ADDRESS_DST
]);
1552 * get new policy id.
1560 u_int32_t newid
= 0;
1561 int count
= key_spi_trycnt
; /* XXX */
1562 struct secpolicy
*sp
;
1564 /* when requesting to allocate spi ranged */
1566 newid
= (policy_id
= (policy_id
== ~0 ? 1 : ++policy_id
));
1568 if ((sp
= key_getspbyid(newid
)) == NULL
)
1574 if (count
== 0 || newid
== 0) {
1576 printf("key_getnewspid: to allocate policy id is failed.\n");
1585 * SADB_SPDDELETE processing
1587 * <base, address(SD), policy(*)>
1588 * from the user(?), and set SADB_SASTATE_DEAD,
1590 * <base, address(SD), policy(*)>
1592 * policy(*) including direction of policy.
1594 * IN: mhp: pointer to the pointer to each header.
1595 * OUT: other if success, return pointer to the message to send.
1598 static struct sadb_msg
*
1602 struct sadb_msg
*msg0
;
1603 struct sadb_address
*src0
, *dst0
;
1604 struct sadb_x_policy
*xpl0
;
1605 struct secpolicyindex spidx
;
1606 struct secpolicy
*sp
;
1609 if (mhp
== NULL
|| mhp
[0] == NULL
)
1610 panic("key_spddelete: NULL pointer is passed.\n");
1612 msg0
= (struct sadb_msg
*)mhp
[0];
1614 if (mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
1615 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
1616 || mhp
[SADB_X_EXT_POLICY
] == NULL
) {
1618 printf("key_spddelete: invalid message is passed.\n");
1620 msg0
->sadb_msg_errno
= EINVAL
;
1624 src0
= (struct sadb_address
*)(mhp
[SADB_EXT_ADDRESS_SRC
]);
1625 dst0
= (struct sadb_address
*)(mhp
[SADB_EXT_ADDRESS_DST
]);
1626 xpl0
= (struct sadb_x_policy
*)mhp
[SADB_X_EXT_POLICY
];
1629 KEY_SETSECSPIDX(xpl0
->sadb_x_policy_dir
,
1632 src0
->sadb_address_prefixlen
,
1633 dst0
->sadb_address_prefixlen
,
1634 src0
->sadb_address_proto
,
1637 /* checking the direciton. */
1638 switch (xpl0
->sadb_x_policy_dir
) {
1639 case IPSEC_DIR_INBOUND
:
1640 case IPSEC_DIR_OUTBOUND
:
1644 printf("key_spddelete: Invalid SP direction.\n");
1646 msg0
->sadb_msg_errno
= EINVAL
;
1650 /* Is there SP in SPD ? */
1651 if ((sp
= key_getsp(&spidx
)) == NULL
) {
1653 printf("key_spddelete: no SP found.\n");
1655 msg0
->sadb_msg_errno
= ENOENT
;
1659 /* save policy id to buffer to be returned. */
1660 xpl0
->sadb_x_policy_id
= sp
->id
;
1662 sp
->state
= IPSEC_SPSTATE_DEAD
;
1666 struct sadb_msg
*newmsg
;
1670 /* create new sadb_msg to reply. */
1671 len
= sizeof(struct sadb_msg
)
1672 + PFKEY_EXTLEN(mhp
[SADB_X_EXT_POLICY
])
1673 + PFKEY_EXTLEN(mhp
[SADB_EXT_ADDRESS_SRC
])
1674 + PFKEY_EXTLEN(mhp
[SADB_EXT_ADDRESS_DST
]);
1676 KMALLOC(newmsg
, struct sadb_msg
*, len
);
1677 if (newmsg
== NULL
) {
1679 printf("key_spddelete: No more memory.\n");
1681 msg0
->sadb_msg_errno
= ENOBUFS
;
1684 bzero((caddr_t
)newmsg
, len
);
1686 bcopy((caddr_t
)mhp
[0], (caddr_t
)newmsg
, sizeof(*msg0
));
1687 newmsg
->sadb_msg_errno
= 0;
1688 newmsg
->sadb_msg_len
= PFKEY_UNIT64(len
);
1689 p
= (caddr_t
)newmsg
+ sizeof(*msg0
);
1691 p
= key_setsadbext(p
, mhp
[SADB_X_EXT_POLICY
]);
1692 p
= key_setsadbext(p
, mhp
[SADB_EXT_ADDRESS_SRC
]);
1693 p
= key_setsadbext(p
, mhp
[SADB_EXT_ADDRESS_DST
]);
1700 * SADB_SPDDELETE2 processing
1703 * from the user(?), and set SADB_SASTATE_DEAD,
1707 * policy(*) including direction of policy.
1709 * IN: mhp: pointer to the pointer to each header.
1710 * OUT: other if success, return pointer to the message to send.
1713 static struct sadb_msg
*
1717 struct sadb_msg
*msg0
;
1719 struct secpolicy
*sp
;
1722 if (mhp
== NULL
|| mhp
[0] == NULL
)
1723 panic("key_spddelete2: NULL pointer is passed.\n");
1725 msg0
= (struct sadb_msg
*)mhp
[0];
1727 if (mhp
[SADB_X_EXT_POLICY
] == NULL
) {
1729 printf("key_spddelete2: invalid message is passed.\n");
1731 msg0
->sadb_msg_errno
= EINVAL
;
1735 id
= ((struct sadb_x_policy
*)mhp
[SADB_X_EXT_POLICY
])->sadb_x_policy_id
;
1737 /* Is there SP in SPD ? */
1738 if ((sp
= key_getspbyid(id
)) == NULL
) {
1740 printf("key_spddelete2: no SP found id:%u.\n", id
);
1742 msg0
->sadb_msg_errno
= ENOENT
;
1746 sp
->state
= IPSEC_SPSTATE_DEAD
;
1750 struct sadb_msg
*newmsg
;
1754 /* create new sadb_msg to reply. */
1755 len
= sizeof(struct sadb_msg
)
1756 + PFKEY_EXTLEN(mhp
[SADB_X_EXT_POLICY
]);
1758 KMALLOC(newmsg
, struct sadb_msg
*, len
);
1759 if (newmsg
== NULL
) {
1761 printf("key_spddelete2: No more memory.\n");
1763 msg0
->sadb_msg_errno
= ENOBUFS
;
1766 bzero((caddr_t
)newmsg
, len
);
1768 bcopy((caddr_t
)mhp
[0], (caddr_t
)newmsg
, sizeof(*msg0
));
1769 newmsg
->sadb_msg_errno
= 0;
1770 newmsg
->sadb_msg_len
= PFKEY_UNIT64(len
);
1771 p
= (caddr_t
)newmsg
+ sizeof(*msg0
);
1773 p
= key_setsadbext(p
, mhp
[SADB_X_EXT_POLICY
]);
1780 * SADB_X_GET processing
1785 * <base, address(SD), policy>
1787 * policy(*) including direction of policy.
1789 * IN: mhp: pointer to the pointer to each header.
1790 * OUT: other if success, return pointer to the message to send.
1794 key_spdget(mhp
, so
, target
)
1799 struct sadb_msg
*msg0
;
1801 struct secpolicy
*sp
;
1805 if (mhp
== NULL
|| mhp
[0] == NULL
)
1806 panic("key_spdget: NULL pointer is passed.\n");
1808 msg0
= (struct sadb_msg
*)mhp
[0];
1810 if (mhp
[SADB_X_EXT_POLICY
] == NULL
) {
1812 printf("key_spdget: invalid message is passed.\n");
1817 id
= ((struct sadb_x_policy
*)mhp
[SADB_X_EXT_POLICY
])->sadb_x_policy_id
;
1819 /* Is there SP in SPD ? */
1820 if ((sp
= key_getspbyid(id
)) == NULL
) {
1822 printf("key_spdget: no SP found id:%u.\n", id
);
1827 m
= key_setdumpsp(sp
, SADB_X_SPDGET
, 0, msg0
->sadb_msg_pid
);
1829 key_sendup_mbuf(so
, m
, target
);
1835 * SADB_X_SPDACQUIRE processing.
1836 * Acquire policy and SA(s) for a *OUTBOUND* packet.
1839 * to KMD, and expect to receive
1840 * <base> with SADB_X_SPDACQUIRE if error occured,
1843 * with SADB_X_SPDUPDATE from KMD by PF_KEY.
1844 * policy(*) is without policy requests.
1847 * others: error number
1851 struct secpolicy
*sp
;
1853 struct secspacq
*newspacq
;
1858 panic("key_spdacquire: NULL pointer is passed.\n");
1859 if (sp
->req
!= NULL
)
1860 panic("key_spdacquire: called but there is request.\n");
1861 if (sp
->policy
!= IPSEC_POLICY_IPSEC
)
1862 panic("key_spdacquire: policy mismathed. IPsec is expected.\n");
1864 /* get a entry to check whether sent message or not. */
1865 if ((newspacq
= key_getspacq(&sp
->spidx
)) != NULL
) {
1866 if (key_blockacq_count
< newspacq
->count
) {
1867 /* reset counter and do send message. */
1868 newspacq
->count
= 0;
1870 /* increment counter and do nothing. */
1875 /* make new entry for blocking to send SADB_ACQUIRE. */
1876 if ((newspacq
= key_newspacq(&sp
->spidx
)) == NULL
)
1879 /* add to acqtree */
1880 LIST_INSERT_HEAD(&spacqtree
, newspacq
, chain
);
1884 struct sadb_msg
*newmsg
= NULL
;
1885 union sadb_x_ident_id id
;
1889 /* create new sadb_msg to reply. */
1890 len
= sizeof(struct sadb_msg
)
1891 + sizeof(struct sadb_ident
)
1892 + PFKEY_ALIGN8(sp
->spidx
.src
.ss_len
)
1893 + sizeof(struct sadb_ident
)
1894 + PFKEY_ALIGN8(sp
->spidx
.dst
.ss_len
);
1896 KMALLOC(newmsg
, struct sadb_msg
*, len
);
1899 printf("key_spdacquire: No more memory.\n");
1903 bzero((caddr_t
)newmsg
, len
);
1905 newmsg
->sadb_msg_version
= PF_KEY_V2
;
1906 newmsg
->sadb_msg_type
= SADB_X_SPDACQUIRE
;
1907 newmsg
->sadb_msg_errno
= 0;
1908 newmsg
->sadb_msg_satype
= 0;
1909 newmsg
->sadb_msg_len
= PFKEY_UNIT64(len
);
1910 newmsg
->sadb_msg_mode
= 0;
1911 newmsg
->sadb_msg_reqid
= 0;
1912 newmsg
->sadb_msg_seq
= 0;
1913 newmsg
->sadb_msg_pid
= 0;
1914 p
= (caddr_t
)newmsg
+ sizeof(struct sadb_msg
);
1916 /* set sadb_address for spidx's. */
1917 bzero(&id
, sizeof(id
));
1918 id
.sadb_x_ident_id_addr
.prefix
= sp
->spidx
.prefs
;
1919 id
.sadb_x_ident_id_addr
.ul_proto
= sp
->spidx
.ul_proto
;
1920 p
= key_setsadbident(p
,
1921 SADB_EXT_IDENTITY_SRC
,
1922 SADB_X_IDENTTYPE_ADDR
,
1923 (caddr_t
)&sp
->spidx
.src
,
1924 sp
->spidx
.src
.ss_len
,
1927 bzero(&id
, sizeof(id
));
1928 id
.sadb_x_ident_id_addr
.prefix
= sp
->spidx
.prefd
;
1929 id
.sadb_x_ident_id_addr
.ul_proto
= sp
->spidx
.ul_proto
;
1930 p
= key_setsadbident(p
,
1931 SADB_EXT_IDENTITY_DST
,
1932 SADB_X_IDENTTYPE_ADDR
,
1933 (caddr_t
)&sp
->spidx
.dst
,
1934 sp
->spidx
.dst
.ss_len
,
1937 error
= key_sendall(newmsg
, len
);
1940 printf("key_spdacquire: key_sendall returned %d\n", error
);
1949 * SADB_SPDFLUSH processing
1952 * from the user, and free all entries in secpctree.
1956 * NOTE: what to do is only marking SADB_SASTATE_DEAD.
1958 * IN: mhp: pointer to the pointer to each header.
1959 * OUT: other if success, return pointer to the message to send.
1962 static struct sadb_msg
*
1966 struct sadb_msg
*msg0
;
1967 struct secpolicy
*sp
;
1971 if (mhp
== NULL
|| mhp
[0] == NULL
)
1972 panic("key_spdflush: NULL pointer is passed.\n");
1974 msg0
= (struct sadb_msg
*)mhp
[0];
1976 for (dir
= 0; dir
< IPSEC_DIR_MAX
; dir
++) {
1977 LIST_FOREACH(sp
, &sptree
[dir
], chain
) {
1978 sp
->state
= IPSEC_SPSTATE_DEAD
;
1983 struct sadb_msg
*newmsg
;
1986 /* create new sadb_msg to reply. */
1987 len
= sizeof(struct sadb_msg
);
1989 KMALLOC(newmsg
, struct sadb_msg
*, len
);
1990 if (newmsg
== NULL
) {
1992 printf("key_spdflush: No more memory.\n");
1994 msg0
->sadb_msg_errno
= ENOBUFS
;
1997 bzero((caddr_t
)newmsg
, len
);
1999 bcopy((caddr_t
)mhp
[0], (caddr_t
)newmsg
, sizeof(*msg0
));
2000 newmsg
->sadb_msg_errno
= 0;
2001 newmsg
->sadb_msg_len
= PFKEY_UNIT64(len
);
2008 * SADB_SPDDUMP processing
2011 * from the user, and dump all SP leaves
2016 * IN: mhp: pointer to the pointer to each header.
2017 * OUT: other if success, return pointer to the message to send.
2021 key_spddump(mhp
, so
, target
)
2026 struct sadb_msg
*msg0
;
2027 struct secpolicy
*sp
;
2033 if (mhp
== NULL
|| mhp
[0] == NULL
)
2034 panic("key_spddump: NULL pointer is passed.\n");
2036 msg0
= (struct sadb_msg
*)mhp
[0];
2038 /* search SPD entry and get buffer size. */
2040 for (dir
= 0; dir
< IPSEC_DIR_MAX
; dir
++) {
2041 LIST_FOREACH(sp
, &sptree
[dir
], chain
) {
2049 for (dir
= 0; dir
< IPSEC_DIR_MAX
; dir
++) {
2050 LIST_FOREACH(sp
, &sptree
[dir
], chain
) {
2052 m
= key_setdumpsp(sp
, SADB_X_SPDDUMP
,
2053 cnt
, msg0
->sadb_msg_pid
);
2056 key_sendup_mbuf(so
, m
, target
);
2063 static struct mbuf
*
2064 key_setdumpsp(sp
, type
, seq
, pid
)
2065 struct secpolicy
*sp
;
2072 /* XXX it would be better to avoid pre-computing length */
2073 tlen
= key_getspmsglen(sp
);
2075 /* XXX maybe it's a wrong idea to insist on cluster? */
2076 MGETHDR(m
, M_DONTWAIT
, MT_DATA
);
2078 MCLGET(m
, M_DONTWAIT
);
2079 if ((m
->m_flags
& M_EXT
) == 0) {
2085 return NULL
; /*ENOBUFS*/
2087 m
->m_pkthdr
.len
= m
->m_len
= 0;
2090 /* sadb_msg->sadb_msg_len must be filled afterwards */
2091 if (key_setsadbmsg_m(m
, type
, 0, SADB_SATYPE_UNSPEC
, seq
, pid
,
2092 IPSEC_MODE_ANY
, 0, 0, sp
->refcnt
) != 0) {
2097 if (key_setsadbaddr_m(m
, SADB_EXT_ADDRESS_SRC
,
2098 (struct sockaddr
*)&sp
->spidx
.src
, sp
->spidx
.prefs
,
2099 sp
->spidx
.ul_proto
) != 0) {
2104 if (key_setsadbaddr_m(m
, SADB_EXT_ADDRESS_DST
,
2105 (struct sockaddr
*)&sp
->spidx
.dst
, sp
->spidx
.prefd
,
2106 sp
->spidx
.ul_proto
) != 0) {
2113 struct sadb_x_policy
*tmp
;
2116 if (!n
|| n
->m_len
< sizeof(*tmp
)) {
2118 printf("key_setdumpsp: No more memory.\n");
2126 tmp
= mtod(n
, struct sadb_x_policy
*);
2128 /* validity check */
2129 if (key_getspreqmsglen(sp
) != PFKEY_UNUNIT64(tmp
->sadb_x_policy_len
)
2130 || n
->m_len
!= PFKEY_UNUNIT64(tmp
->sadb_x_policy_len
))
2131 panic("key_setdumpsp: length mismatch."
2133 key_getspreqmsglen(sp
),
2134 PFKEY_UNUNIT64(tmp
->sadb_x_policy_len
));
2137 m
->m_pkthdr
.len
+= n
->m_len
;
2140 if (m
->m_len
< sizeof(struct sadb_msg
)) {
2141 m
= m_pullup(m
, sizeof(struct sadb_msg
));
2145 mtod(m
, struct sadb_msg
*)->sadb_msg_len
=
2146 PFKEY_UNIT64(m
->m_pkthdr
.len
);
2151 /* get sadb message length for a SP. */
2154 struct secpolicy
*sp
;
2160 panic("key_getspmsglen: NULL pointer is passed.\n");
2162 tlen
= (sizeof(struct sadb_msg
)
2163 + sizeof(struct sadb_address
)
2164 + PFKEY_ALIGN8(_SALENBYAF(sp
->spidx
.src
.ss_family
))
2165 + sizeof(struct sadb_address
)
2166 + PFKEY_ALIGN8(_SALENBYAF(sp
->spidx
.dst
.ss_family
)));
2168 tlen
+= key_getspreqmsglen(sp
);
2174 * get PFKEY message length for security policy and request.
2177 key_getspreqmsglen(sp
)
2178 struct secpolicy
*sp
;
2182 tlen
= sizeof(struct sadb_x_policy
);
2184 /* if is the policy for ipsec ? */
2185 if (sp
->policy
!= IPSEC_POLICY_IPSEC
)
2188 /* get length of ipsec requests */
2190 struct ipsecrequest
*isr
;
2193 for (isr
= sp
->req
; isr
!= NULL
; isr
= isr
->next
) {
2194 len
= sizeof(struct sadb_x_ipsecrequest
)
2195 + isr
->saidx
.src
.ss_len
2196 + isr
->saidx
.dst
.ss_len
;
2198 tlen
+= PFKEY_ALIGN8(len
);
2205 /* %%% SAD management */
2207 * allocating a memory for new SA head, and copy from the values of mhp.
2208 * OUT: NULL : failure due to the lack of memory.
2209 * others : pointer to new SA head.
2211 static struct secashead
*
2213 struct secasindex
*saidx
;
2215 struct secashead
*newsah
;
2219 panic("key_newsaidx: NULL pointer is passed.\n");
2221 newsah
= keydb_newsecashead();
2225 bcopy(saidx
, &newsah
->saidx
, sizeof(newsah
->saidx
));
2227 /* add to saidxtree */
2228 newsah
->state
= SADB_SASTATE_MATURE
;
2229 LIST_INSERT_HEAD(&sahtree
, newsah
, chain
);
2235 * delete SA index and all SA registerd.
2239 struct secashead
*sah
;
2241 struct secasvar
*sav
, *nextsav
;
2242 u_int stateidx
, state
;
2248 panic("key_delsah: NULL pointer is passed.\n");
2251 s
= splsoftnet(); /*called from softclock()*/
2253 s
= splnet(); /*called from softclock()*/
2256 /* searching all SA registerd in the secindex. */
2258 stateidx
< _ARRAYLEN(saorder_state_any
);
2261 state
= saorder_state_any
[stateidx
];
2262 for (sav
= (struct secasvar
*)LIST_FIRST(&sah
->savtree
[state
]);
2266 nextsav
= LIST_NEXT(sav
, chain
);
2268 if (sav
->refcnt
> 0) {
2269 /* give up to delete this sa */
2275 KEY_CHKSASTATE(state
, sav
->state
, "key_delsah");
2279 /* remove back pointer */
2285 /* don't delete sah only if there are savs. */
2291 if (sah
->sa_route
.ro_rt
) {
2292 RTFREE(sah
->sa_route
.ro_rt
);
2293 sah
->sa_route
.ro_rt
= (struct rtentry
*)NULL
;
2296 /* remove from tree of SA index */
2297 if (__LIST_CHAINED(sah
))
2298 LIST_REMOVE(sah
, chain
);
2307 * allocating a new SA with LARVAL state. key_add() and key_getspi() call,
2308 * and copy the values of mhp into new buffer.
2309 * When SAD message type is GETSPI:
2310 * to set sequence number from acq_seq++,
2311 * to set zero to SPI.
2312 * not to call key_setsava().
2314 * others : pointer to new secasvar.
2316 static struct secasvar
*
2317 key_newsav(mhp
, sah
)
2319 struct secashead
*sah
;
2321 struct secasvar
*newsav
;
2322 struct sadb_msg
*msg0
;
2325 if (mhp
== NULL
|| mhp
[0] == NULL
|| sah
== NULL
)
2326 panic("key_newsa: NULL pointer is passed.\n");
2328 msg0
= (struct sadb_msg
*)mhp
[0];
2330 KMALLOC(newsav
, struct secasvar
*, sizeof(struct secasvar
));
2331 if (newsav
== NULL
) {
2333 printf("key_newsa: No more memory.\n");
2335 msg0
->sadb_msg_errno
= ENOBUFS
;
2338 bzero((caddr_t
)newsav
, sizeof(struct secasvar
));
2340 switch (msg0
->sadb_msg_type
) {
2344 #if IPSEC_DOSEQCHECK
2345 /* sync sequence number */
2346 if (msg0
->sadb_msg_seq
== 0)
2348 (acq_seq
= (acq_seq
== ~0 ? 1 : ++acq_seq
));
2351 newsav
->seq
= msg0
->sadb_msg_seq
;
2356 if (mhp
[SADB_EXT_SA
] == NULL
) {
2359 printf("key_newsa: invalid message is passed.\n");
2361 msg0
->sadb_msg_errno
= EINVAL
;
2364 newsav
->spi
= ((struct sadb_sa
*)mhp
[SADB_EXT_SA
])->sadb_sa_spi
;
2365 newsav
->seq
= msg0
->sadb_msg_seq
;
2369 msg0
->sadb_msg_errno
= EINVAL
;
2373 /* copy sav values */
2374 if (msg0
->sadb_msg_type
!= SADB_GETSPI
&& key_setsaval(newsav
, mhp
)) {
2376 /* msg0->sadb_msg_errno is set at key_setsaval. */
2383 newsav
->pid
= msg0
->sadb_msg_pid
;
2388 newsav
->state
= SADB_SASTATE_LARVAL
;
2389 LIST_INSERT_TAIL(&sah
->savtree
[SADB_SASTATE_LARVAL
], newsav
,
2396 * free() SA variable entry.
2400 struct secasvar
*sav
;
2404 panic("key_delsav: NULL pointer is passed.\n");
2406 if (sav
->refcnt
> 0)
2407 return; /* can't free */
2409 /* remove from SA header */
2410 if (__LIST_CHAINED(sav
))
2411 LIST_REMOVE(sav
, chain
);
2413 if (sav
->key_auth
!= NULL
)
2414 KFREE(sav
->key_auth
);
2415 if (sav
->key_enc
!= NULL
)
2416 KFREE(sav
->key_enc
);
2417 if (sav
->replay
!= NULL
)
2418 keydb_delsecreplay(sav
->replay
);
2419 if (sav
->lft_c
!= NULL
)
2421 if (sav
->lft_h
!= NULL
)
2423 if (sav
->lft_s
!= NULL
)
2425 if (sav
->iv
!= NULL
)
2428 if (sav
->misc1
!= NULL
)
2430 if (sav
->misc2
!= NULL
)
2432 if (sav
->misc3
!= NULL
)
2445 * others : found, pointer to a SA.
2447 static struct secashead
*
2449 struct secasindex
*saidx
;
2451 struct secashead
*sah
;
2453 LIST_FOREACH(sah
, &sahtree
, chain
) {
2454 if (sah
->state
== SADB_SASTATE_DEAD
)
2456 if (key_cmpsaidx_exactly(&sah
->saidx
, saidx
))
2464 * check not to be duplicated SPI.
2465 * NOTE: this function is too slow due to searching all SAD.
2468 * others : found, pointer to a SA.
2470 static struct secasvar
*
2471 key_checkspidup(saidx
, spi
)
2472 struct secasindex
*saidx
;
2475 struct secashead
*sah
;
2476 struct secasvar
*sav
;
2478 /* check address family */
2479 if (saidx
->src
.ss_family
!= saidx
->dst
.ss_family
) {
2481 printf("key_checkspidup: address family mismatched.\n");
2487 LIST_FOREACH(sah
, &sahtree
, chain
) {
2488 if (!key_ismyaddr(sah
->saidx
.dst
.ss_family
,
2489 _INADDRBYSA(&sah
->saidx
.dst
)))
2491 sav
= key_getsavbyspi(sah
, spi
);
2500 * search SAD litmited alive SA, protocol, SPI.
2503 * others : found, pointer to a SA.
2505 static struct secasvar
*
2506 key_getsavbyspi(sah
, spi
)
2507 struct secashead
*sah
;
2510 struct secasvar
*sav
;
2511 u_int stateidx
, state
;
2513 /* search all status */
2515 stateidx
< _ARRAYLEN(saorder_state_alive
);
2518 state
= saorder_state_alive
[stateidx
];
2519 LIST_FOREACH(sav
, &sah
->savtree
[state
], chain
) {
2522 if (sav
->state
!= state
) {
2524 printf("key_getsavbyspi: "
2525 "invalid sav->state "
2526 "(queue: %d SA: %d)\n",
2532 if (sav
->spi
== spi
)
2541 * copy SA values from PF_KEY message except *SPI, SEQ, PID, STATE and TYPE*.
2542 * You must update these if need.
2544 * 1: failure. set errno to (mhp[0])->sadb_msg_errno.
2547 key_setsaval(sav
, mhp
)
2548 struct secasvar
*sav
;
2551 struct sadb_msg
*msg0
;
2555 if (mhp
== NULL
|| mhp
[0] == NULL
)
2556 panic("key_setsaval: NULL pointer is passed.\n");
2558 msg0
= (struct sadb_msg
*)mhp
[0];
2560 /* initialization */
2562 sav
->key_auth
= NULL
;
2563 sav
->key_enc
= NULL
;
2575 if (mhp
[SADB_EXT_SA
] != NULL
) {
2576 struct sadb_sa
*sa0
= (struct sadb_sa
*)mhp
[SADB_EXT_SA
];
2578 sav
->alg_auth
= sa0
->sadb_sa_auth
;
2579 sav
->alg_enc
= sa0
->sadb_sa_encrypt
;
2580 sav
->flags
= sa0
->sadb_sa_flags
;
2583 if ((sa0
->sadb_sa_flags
& SADB_X_EXT_OLD
) == 0) {
2584 sav
->replay
= keydb_newsecreplay(sa0
->sadb_sa_replay
);
2585 if (sav
->replay
== NULL
) {
2587 printf("key_setsaval: No more memory.\n");
2595 /* Authentication keys */
2596 if (mhp
[SADB_EXT_KEY_AUTH
] != NULL
) {
2597 struct sadb_key
*key0
;
2600 key0
= (struct sadb_key
*)mhp
[SADB_EXT_KEY_AUTH
];
2601 len
= PFKEY_UNUNIT64(key0
->sadb_key_len
);
2604 if (len
< sizeof(struct sadb_key
))
2606 switch (msg0
->sadb_msg_satype
) {
2607 case SADB_SATYPE_AH
:
2608 case SADB_SATYPE_ESP
:
2609 if (len
== sizeof(struct sadb_key
)
2610 && sav
->alg_auth
!= SADB_AALG_NULL
) {
2614 case SADB_X_SATYPE_IPCOMP
:
2623 printf("key_setsaval: invalid key_auth values.\n");
2628 sav
->key_auth
= (struct sadb_key
*)key_newbuf(key0
, len
);
2629 if (sav
->key_auth
== NULL
) {
2631 printf("key_setsaval: No more memory.\n");
2637 /* make length shift up for kernel*/
2638 sav
->key_auth
->sadb_key_len
= len
;
2641 /* Encryption key */
2642 if (mhp
[SADB_EXT_KEY_ENCRYPT
] != NULL
) {
2643 struct sadb_key
*key0
;
2646 key0
= (struct sadb_key
*)mhp
[SADB_EXT_KEY_ENCRYPT
];
2647 len
= PFKEY_UNUNIT64(key0
->sadb_key_len
);
2650 if (len
< sizeof(struct sadb_key
))
2652 switch (msg0
->sadb_msg_satype
) {
2653 case SADB_SATYPE_ESP
:
2654 if (len
== sizeof(struct sadb_key
)
2655 && sav
->alg_enc
!= SADB_EALG_NULL
) {
2659 case SADB_SATYPE_AH
:
2662 case SADB_X_SATYPE_IPCOMP
:
2670 printf("key_setsatval: invalid key_enc value.\n");
2675 sav
->key_enc
= (struct sadb_key
*)key_newbuf(key0
, len
);
2676 if (sav
->key_enc
== NULL
) {
2678 printf("key_setsaval: No more memory.\n");
2684 /* make length shift up for kernel*/
2685 sav
->key_enc
->sadb_key_len
= len
;
2691 switch (msg0
->sadb_msg_satype
) {
2692 case SADB_SATYPE_ESP
:
2695 struct esp_algorithm
*algo
;
2697 algo
= &esp_algorithms
[sav
->alg_enc
];
2698 if (algo
&& algo
->ivlen
)
2699 sav
->ivlen
= (*algo
->ivlen
)(sav
);
2700 KMALLOC(sav
->iv
, caddr_t
, sav
->ivlen
);
2703 printf("key_setsaval: No more memory.\n");
2714 case SADB_SATYPE_AH
:
2715 #if 1 /*nonstandard*/
2716 case SADB_X_SATYPE_IPCOMP
:
2721 printf("key_setsaval: invalid SA type.\n");
2730 /* make lifetime for CURRENT */
2734 KMALLOC(sav
->lft_c
, struct sadb_lifetime
*,
2735 sizeof(struct sadb_lifetime
));
2736 if (sav
->lft_c
== NULL
) {
2738 printf("key_setsaval: No more memory.\n");
2746 sav
->lft_c
->sadb_lifetime_len
=
2747 PFKEY_UNIT64(sizeof(struct sadb_lifetime
));
2748 sav
->lft_c
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
2749 sav
->lft_c
->sadb_lifetime_allocations
= 0;
2750 sav
->lft_c
->sadb_lifetime_bytes
= 0;
2751 sav
->lft_c
->sadb_lifetime_addtime
= tv
.tv_sec
;
2752 sav
->lft_c
->sadb_lifetime_usetime
= 0;
2755 /* lifetimes for HARD and SOFT */
2757 struct sadb_lifetime
*lft0
;
2759 lft0
= (struct sadb_lifetime
*)mhp
[SADB_EXT_LIFETIME_HARD
];
2761 sav
->lft_h
= (struct sadb_lifetime
*)key_newbuf(lft0
,
2763 if (sav
->lft_h
== NULL
) {
2765 printf("key_setsaval: No more memory.\n");
2770 /* to be initialize ? */
2773 lft0
= (struct sadb_lifetime
*)mhp
[SADB_EXT_LIFETIME_SOFT
];
2775 sav
->lft_s
= (struct sadb_lifetime
*)key_newbuf(lft0
,
2777 if (sav
->lft_s
== NULL
) {
2779 printf("key_setsaval: No more memory.\n");
2784 /* to be initialize ? */
2789 /* pre-processing for DES */
2790 switch (sav
->alg_enc
) {
2791 case SADB_EALG_DESCBC
:
2792 if (des_key_sched((C_Block
*)_KEYBUF(sav
->key_enc
),
2793 (des_key_schedule
)sav
->misc1
) != 0) {
2795 printf("key_setsaval: error des_key_sched.\n");
2801 case SADB_EALG_3DESCBC
:
2802 if (des_key_sched((C_Block
*)_KEYBUF(sav
->key_enc
),
2803 (des_key_schedule
)sav
->misc1
) != 0
2804 || des_key_sched((C_Block
*)(_KEYBUF(sav
->key_enc
) + 8),
2805 (des_key_schedule
)sav
->misc2
) != 0
2806 || des_key_sched((C_Block
*)(_KEYBUF(sav
->key_enc
) + 16),
2807 (des_key_schedule
)sav
->misc3
) != 0) {
2809 printf("key_setsaval: error des_key_sched.\n");
2819 msg0
->sadb_msg_errno
= 0;
2823 /* initialization */
2824 if (sav
->replay
!= NULL
)
2825 keydb_delsecreplay(sav
->replay
);
2826 if (sav
->key_auth
!= NULL
)
2827 KFREE(sav
->key_auth
);
2828 if (sav
->key_enc
!= NULL
)
2829 KFREE(sav
->key_enc
);
2830 if (sav
->iv
!= NULL
)
2832 if (sav
->lft_c
!= NULL
)
2834 if (sav
->lft_h
!= NULL
)
2836 if (sav
->lft_s
!= NULL
)
2839 if (sav
->misc1
!= NULL
)
2841 if (sav
->misc2
!= NULL
)
2843 if (sav
->misc3
!= NULL
)
2847 msg0
->sadb_msg_errno
= error
;
2852 * get message buffer length.
2856 struct secasvar
*sav
;
2858 int len
= sizeof(struct sadb_msg
);
2860 len
+= sizeof(struct sadb_sa
);
2861 len
+= (sizeof(struct sadb_address
)
2862 + PFKEY_ALIGN8(_SALENBYAF(sav
->sah
->saidx
.src
.ss_family
)));
2863 len
+= (sizeof(struct sadb_address
)
2864 + PFKEY_ALIGN8(_SALENBYAF(sav
->sah
->saidx
.dst
.ss_family
)));
2866 if (sav
->key_auth
!= NULL
)
2867 len
+= sav
->key_auth
->sadb_key_len
;
2868 if (sav
->key_enc
!= NULL
)
2869 len
+= sav
->key_enc
->sadb_key_len
;
2871 if (sav
->lft_c
!= NULL
)
2872 len
+= sizeof(struct sadb_lifetime
);
2873 if (sav
->lft_h
!= NULL
)
2874 len
+= sizeof(struct sadb_lifetime
);
2875 if (sav
->lft_s
!= NULL
)
2876 len
+= sizeof(struct sadb_lifetime
);
2882 * validation with a secasvar entry, and set SADB_SATYPE_MATURE.
2888 struct secasvar
*sav
;
2891 int checkmask
= 0; /* 2^0: ealg 2^1: aalg 2^2: calg */
2892 int mustmask
= 0; /* 2^0: ealg 2^1: aalg 2^2: calg */
2896 /* check SPI value */
2897 if (ntohl(sav
->spi
) >= 0 && ntohl(sav
->spi
) <= 255) {
2899 printf("key_mature: illegal range of SPI %d.\n", sav
->spi
);
2905 switch (sav
->sah
->saidx
.proto
) {
2908 if ((sav
->flags
& SADB_X_EXT_OLD
)
2909 && (sav
->flags
& SADB_X_EXT_DERIV
)) {
2911 printf("key_mature: "
2912 "invalid flag (derived) given to old-esp.\n");
2921 if (sav
->flags
& SADB_X_EXT_DERIV
) {
2923 printf("key_mature: "
2924 "invalid flag (derived) given to AH SA.\n");
2928 if (sav
->alg_enc
!= SADB_EALG_NONE
) {
2930 printf("key_mature: "
2931 "protocol and algorithm mismated.\n");
2938 #if 1 /*nonstandard*/
2939 case IPPROTO_IPCOMP
:
2940 if (sav
->alg_auth
!= SADB_AALG_NONE
) {
2942 printf("key_mature: "
2943 "protocol and algorithm mismated.\n");
2947 if ((sav
->flags
& SADB_X_EXT_RAWCPI
) == 0
2948 && ntohl(sav
->spi
) >= 0x10000) {
2950 printf("key_mature: invalid cpi for IPComp.\n");
2960 printf("key_mature: Invalid satype.\n");
2962 return EPROTONOSUPPORT
;
2965 /* check authentication algorithm */
2966 if ((checkmask
& 2) != 0) {
2967 struct ah_algorithm
*algo
;
2970 /* XXX: should use algorithm map to check. */
2971 switch (sav
->alg_auth
) {
2972 case SADB_AALG_NONE
:
2973 case SADB_AALG_MD5HMAC
:
2974 case SADB_AALG_SHA1HMAC
:
2977 case SADB_AALG_NULL
:
2981 printf("key_mature: "
2982 "unknown authentication algorithm.\n");
2987 /* algorithm-dependent check */
2988 algo
= &ah_algorithms
[sav
->alg_auth
];
2991 keylen
= sav
->key_auth
->sadb_key_bits
;
2994 if (keylen
< algo
->keymin
|| algo
->keymax
< keylen
) {
2996 printf("key_mature: invalid AH key length %d "
2997 "(%d-%d allowed)\n", keylen
,
2998 algo
->keymin
, algo
->keymax
);
3004 if ((*algo
->mature
)(sav
)) {
3005 /* message generated in per-algorithm function*/
3008 mature
= SADB_SATYPE_AH
;
3011 if ((mustmask
& 2) != 0 && mature
!= SADB_SATYPE_AH
) {
3013 printf("key_mature: no satisfy algorithm for AH\n");
3019 /* check encryption algorithm */
3020 if ((checkmask
& 1) != 0) {
3022 struct esp_algorithm
*algo
;
3025 switch (sav
->alg_enc
) {
3026 case SADB_EALG_NONE
:
3027 case SADB_EALG_DESCBC
:
3028 case SADB_EALG_3DESCBC
:
3029 case SADB_EALG_NULL
:
3030 case SADB_EALG_BLOWFISHCBC
:
3031 case SADB_EALG_CAST128CBC
:
3032 case SADB_EALG_RC5CBC
:
3036 printf("key_mature: unknown encryption algorithm.\n");
3041 /* algorithm-dependent check */
3042 algo
= &esp_algorithms
[sav
->alg_enc
];
3045 keylen
= sav
->key_enc
->sadb_key_bits
;
3048 if (keylen
< algo
->keymin
|| algo
->keymax
< keylen
) {
3050 printf("key_mature: invalid ESP key length %d "
3051 "(%d-%d allowed)\n", keylen
,
3052 algo
->keymin
, algo
->keymax
);
3058 if ((*algo
->mature
)(sav
)) {
3059 /* message generated in per-algorithm function*/
3062 mature
= SADB_SATYPE_ESP
;
3065 if ((mustmask
& 1) != 0 && mature
!= SADB_SATYPE_ESP
) {
3067 printf("key_mature: no satisfy algorithm for ESP\n");
3073 printf("key_mature: ESP not supported in this configuration\n");
3079 /* check compression algorithm */
3080 if ((checkmask
& 4) != 0) {
3081 struct ipcomp_algorithm
*algo
;
3083 switch (sav
->alg_enc
) {
3084 case SADB_X_CALG_NONE
:
3085 case SADB_X_CALG_OUI
:
3086 case SADB_X_CALG_DEFLATE
:
3087 case SADB_X_CALG_LZS
:
3091 printf("key_mature: unknown compression algorithm.\n");
3096 /* algorithm-dependent check */
3097 algo
= &ipcomp_algorithms
[sav
->alg_enc
];
3099 if (!(algo
->compress
&& algo
->decompress
)) {
3101 printf("key_mature: "
3102 "unsupported compression algorithm.\n");
3108 key_sa_chgstate(sav
, SADB_SASTATE_MATURE
);
3114 * subroutine for SADB_GET and SADB_DUMP.
3115 * the buf must be allocated sufficent space.
3118 key_setdumpsa(newmsg
, sav
, type
, satype
, seq
, pid
)
3119 struct sadb_msg
*newmsg
;
3120 struct secasvar
*sav
;
3121 u_int8_t type
, satype
;
3128 tlen
= key_getmsglen(sav
);
3130 p
= key_setsadbmsg((caddr_t
)newmsg
, type
, tlen
,
3132 sav
->sah
->saidx
.mode
, sav
->sah
->saidx
.reqid
,
3135 for (i
= 1; i
<= SADB_EXT_MAX
; i
++) {
3138 p
= key_setsadbsa(p
, sav
);
3141 case SADB_EXT_ADDRESS_SRC
:
3142 p
= key_setsadbaddr(p
,
3143 SADB_EXT_ADDRESS_SRC
,
3144 (struct sockaddr
*)&sav
->sah
->saidx
.src
,
3145 _INALENBYAF(sav
->sah
->saidx
.src
.ss_family
) << 3,
3149 case SADB_EXT_ADDRESS_DST
:
3150 p
= key_setsadbaddr(p
,
3151 SADB_EXT_ADDRESS_DST
,
3152 (struct sockaddr
*)&sav
->sah
->saidx
.dst
,
3153 _INALENBYAF(sav
->sah
->saidx
.dst
.ss_family
) << 3,
3157 case SADB_EXT_KEY_AUTH
:
3160 if (sav
->key_auth
== NULL
) break;
3161 len
= sav
->key_auth
->sadb_key_len
; /* real length */
3162 bcopy((caddr_t
)sav
->key_auth
, p
, len
);
3163 ((struct sadb_ext
*)p
)->sadb_ext_len
= PFKEY_UNIT64(len
);
3168 case SADB_EXT_KEY_ENCRYPT
:
3171 if (sav
->key_enc
== NULL
) break;
3172 len
= sav
->key_enc
->sadb_key_len
; /* real length */
3173 bcopy((caddr_t
)sav
->key_enc
, p
, len
);
3174 ((struct sadb_ext
*)p
)->sadb_ext_len
= PFKEY_UNIT64(len
);
3179 case SADB_EXT_LIFETIME_CURRENT
:
3180 if (sav
->lft_c
== NULL
) break;
3181 p
= key_setsadbext(p
, (caddr_t
)sav
->lft_c
);
3184 case SADB_EXT_LIFETIME_HARD
:
3185 if (sav
->lft_h
== NULL
) break;
3186 p
= key_setsadbext(p
, (caddr_t
)sav
->lft_h
);
3189 case SADB_EXT_LIFETIME_SOFT
:
3190 if (sav
->lft_s
== NULL
) break;
3191 p
= key_setsadbext(p
, (caddr_t
)sav
->lft_s
);
3194 case SADB_EXT_IDENTITY_SRC
:
3195 case SADB_EXT_IDENTITY_DST
:
3196 /* XXX: should we brought from SPD ? */
3197 case SADB_EXT_SENSITIVITY
:
3208 key_setsadbmsg_m(m
, type
, tlen
, satype
, seq
, pid
, mode
, reqid
,
3209 reserved1
, reserved2
)
3211 u_int8_t type
, satype
;
3218 u_int32_t reserved2
;
3221 const size_t len
= sizeof(struct sadb_msg
);
3223 p
= key_appendmbuf(m
, len
);
3227 if (key_setsadbmsg(p
, type
, tlen
, satype
, seq
, pid
, mode
, reqid
,
3228 reserved1
, reserved2
))
3236 * set data into sadb_msg.
3237 * `buf' must has been allocated sufficiently.
3240 key_setsadbmsg(buf
, type
, tlen
, satype
, seq
, pid
, mode
, reqid
,
3241 reserved1
, reserved2
)
3243 u_int8_t type
, satype
;
3250 u_int32_t reserved2
;
3255 p
= (struct sadb_msg
*)buf
;
3256 len
= sizeof(struct sadb_msg
);
3259 p
->sadb_msg_version
= PF_KEY_V2
;
3260 p
->sadb_msg_type
= type
;
3261 p
->sadb_msg_errno
= 0;
3262 p
->sadb_msg_satype
= satype
;
3263 p
->sadb_msg_len
= PFKEY_UNIT64(tlen
);
3264 p
->sadb_msg_mode
= mode
;
3265 p
->sadb_msg_reserved1
= reserved1
;
3266 p
->sadb_msg_seq
= seq
;
3267 p
->sadb_msg_pid
= (u_int32_t
)pid
;
3268 p
->sadb_msg_reqid
= reqid
;
3269 p
->sadb_msg_reserved2
= reserved2
;
3275 * copy secasvar data into sadb_address.
3276 * `buf' must has been allocated sufficiently.
3279 key_setsadbsa(buf
, sav
)
3281 struct secasvar
*sav
;
3286 p
= (struct sadb_sa
*)buf
;
3287 len
= sizeof(struct sadb_sa
);
3290 p
->sadb_sa_len
= PFKEY_UNIT64(len
);
3291 p
->sadb_sa_exttype
= SADB_EXT_SA
;
3292 p
->sadb_sa_spi
= sav
->spi
;
3293 p
->sadb_sa_replay
= (sav
->replay
!= NULL
? sav
->replay
->wsize
: 0);
3294 p
->sadb_sa_state
= sav
->state
;
3295 p
->sadb_sa_auth
= sav
->alg_auth
;
3296 p
->sadb_sa_encrypt
= sav
->alg_enc
;
3297 p
->sadb_sa_flags
= sav
->flags
;
3304 key_setsadbaddr_m(m
, exttype
, saddr
, prefixlen
, ul_proto
)
3307 struct sockaddr
*saddr
;
3313 sizeof(struct sadb_address
) + PFKEY_ALIGN8(saddr
->sa_len
);
3315 p
= key_appendmbuf(m
, len
);
3319 if (key_setsadbaddr(p
, exttype
, saddr
, prefixlen
, ul_proto
))
3327 * set data into sadb_address.
3328 * `buf' must has been allocated sufficiently.
3331 key_setsadbaddr(buf
, exttype
, saddr
, prefixlen
, ul_proto
)
3334 struct sockaddr
*saddr
;
3338 struct sadb_address
*p
;
3341 p
= (struct sadb_address
*)buf
;
3342 len
= sizeof(struct sadb_address
) + PFKEY_ALIGN8(saddr
->sa_len
);
3345 p
->sadb_address_len
= PFKEY_UNIT64(len
);
3346 p
->sadb_address_exttype
= exttype
;
3347 p
->sadb_address_proto
= ul_proto
;
3348 p
->sadb_address_prefixlen
= prefixlen
;
3349 p
->sadb_address_reserved
= 0;
3351 bcopy(saddr
, p
+ 1, saddr
->sa_len
);
3357 * set data into sadb_ident.
3358 * `buf' must has been allocated sufficiently.
3361 key_setsadbident(buf
, exttype
, idtype
, string
, stringlen
, id
)
3363 u_int16_t exttype
, idtype
;
3368 struct sadb_ident
*p
;
3371 p
= (struct sadb_ident
*)buf
;
3372 len
= sizeof(struct sadb_ident
) + PFKEY_ALIGN8(stringlen
);
3375 p
->sadb_ident_len
= PFKEY_UNIT64(len
);
3376 p
->sadb_ident_exttype
= exttype
;
3377 p
->sadb_ident_type
= idtype
;
3378 p
->sadb_ident_reserved
= 0;
3379 p
->sadb_ident_id
= id
;
3381 bcopy(string
, p
+ 1, stringlen
);
3387 * set data into sadb_x_policy
3388 * `buf' must has been allocated sufficiently.
3391 key_setsadbxpolicy(buf
, type
, dir
, id
)
3397 struct sadb_x_policy
*p
;
3400 p
= (struct sadb_x_policy
*)buf
;
3401 len
= sizeof(struct sadb_x_policy
);
3404 p
->sadb_x_policy_len
= PFKEY_UNIT64(len
);
3405 p
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
3406 p
->sadb_x_policy_type
= type
;
3407 p
->sadb_x_policy_dir
= dir
;
3408 p
->sadb_x_policy_id
= id
;
3414 * copy buffer of any sadb extension type into sadb_ext.
3415 * assume that sadb_ext_len shifted down >> 3.
3416 * i.e. shift length up when setting length of extension.
3419 key_setsadbext(p
, ext
)
3424 len
= PFKEY_UNUNIT64(((struct sadb_ext
*)ext
)->sadb_ext_len
);
3433 * copy a buffer into the new buffer allocated.
3436 key_newbuf(src
, len
)
3442 KMALLOC(new, caddr_t
, len
);
3445 printf("key_newbuf: No more memory.\n");
3449 bcopy((caddr_t
)src
, new, len
);
3454 /* compare my own address
3455 * OUT: 1: true, i.e. my address.
3459 key_ismyaddr(family
, addr
)
3465 panic("key_ismyaddr: NULL pointer is passed.\n");
3470 struct in_ifaddr
*ia
;
3473 for (ia
= in_ifaddr
.tqh_first
; ia
; ia
= ia
->ia_list
.tqe_next
)
3474 #elif defined(__FreeBSD__) && __FreeBSD__ >= 3 || defined (__APPLE__)
3475 for (ia
= in_ifaddrhead
.tqh_first
; ia
;
3476 ia
= ia
->ia_link
.tqe_next
)
3478 for (ia
= in_ifaddr
; ia
; ia
= ia
->ia_next
)
3481 (caddr_t
)&ia
->ia_addr
.sin_addr
,
3482 _INALENBYAF(family
)) == 0)
3488 return key_ismyaddr6(addr
);
3497 * compare my own address for IPv6.
3500 * NOTE: derived ip6_input() in KAME. This is necessary to modify more.
3502 #include <netinet6/in6_var.h>
3508 struct in6_addr
*a
= (struct in6_addr
*)addr
;
3509 struct in6_ifaddr
*ia
;
3511 for (ia
= in6_ifaddr
; ia
; ia
= ia
->ia_next
) {
3512 if (bcmp(addr
, (caddr_t
)&ia
->ia_addr
.sin6_addr
,
3513 _INALENBYAF(AF_INET6
)) == 0) {
3519 struct in6_multi
*in6m
= 0;
3521 #if defined(__FreeBSD__) && __FreeBSD__ >= 3 || defined (__APPLE__)
3522 IN6_LOOKUP_MULTI(*(struct in6_addr
*)addr
, ia
->ia_ifp
, in6m
);
3524 for ((in6m
) = ia
->ia6_multiaddrs
.lh_first
;
3526 !IN6_ARE_ADDR_EQUAL(&(in6m
)->in6m_addr
, a
);
3527 (in6m
) = in6m
->in6m_entry
.le_next
)
3535 /* loopback, just for safety */
3536 if (IN6_IS_ADDR_LOOPBACK(a
))
3541 if (ip6_keepfaith
&&
3542 (a
->s6_addr32
[0] == ip6_faith_prefix
.s6_addr32
[0] &&
3543 a
->s6_addr32
[1] == ip6_faith_prefix
.s6_addr32
[1] &&
3544 a
->s6_addr32
[2] == ip6_faith_prefix
.s6_addr32
[2]))
3555 /* checking address is whether loopback or not.
3560 key_isloopback(family
, addr
)
3566 if (((caddr_t
)addr
)[0] == IN_LOOPBACKNET
)
3571 if (IN6_IS_ADDR_LOOPBACK((struct in6_addr
*)addr
))
3577 printf("key_isloopback: unknown address family=%d.\n", family
);
3587 * compare two secasindex structure exactly.
3589 * saidx0: source, it can be in SAD.
3596 key_cmpsaidx_exactly(saidx0
, saidx1
)
3597 struct secasindex
*saidx0
, *saidx1
;
3600 if (saidx0
== NULL
&& saidx1
== NULL
)
3603 if (saidx0
== NULL
|| saidx1
== NULL
)
3606 if (saidx0
->proto
!= saidx1
->proto
3607 || saidx0
->mode
!= saidx1
->mode
3608 || saidx0
->reqid
!= saidx1
->reqid
)
3611 if (bcmp(&saidx0
->src
, &saidx1
->src
, saidx0
->src
.ss_len
) != 0
3612 || bcmp(&saidx0
->dst
, &saidx1
->dst
, saidx0
->dst
.ss_len
) != 0)
3619 * compare two secasindex structure with consideration mode.
3620 * don't compare port.
3622 * saidx0: source, it is often in SAD.
3623 * saidx1: object, it is often from SPD.
3629 key_cmpsaidx_withmode(saidx0
, saidx1
)
3630 struct secasindex
*saidx0
, *saidx1
;
3633 if (saidx0
== NULL
&& saidx1
== NULL
)
3636 if (saidx0
== NULL
|| saidx1
== NULL
)
3639 if (saidx0
->proto
!= saidx1
->proto
3640 || saidx0
->src
.ss_family
!= saidx1
->src
.ss_family
3641 || saidx0
->dst
.ss_family
!= saidx1
->dst
.ss_family
)
3645 * If reqid of SPD is non-zero, unique SA is required.
3646 * The result must be of same reqid in this case.
3648 if (saidx1
->reqid
!= 0
3649 && saidx0
->reqid
!= saidx1
->reqid
)
3652 if (saidx0
->mode
!= IPSEC_MODE_ANY
3653 && saidx0
->mode
!= saidx1
->mode
)
3657 int sa_len
= _INALENBYAF(saidx0
->src
.ss_family
);
3659 if (bcmp(_INADDRBYSA(&saidx0
->src
), _INADDRBYSA(&saidx1
->src
), sa_len
)
3660 || bcmp(_INADDRBYSA(&saidx0
->dst
), _INADDRBYSA(&saidx1
->dst
), sa_len
))
3668 * compare two secindex structure exactly.
3670 * spidx0: source, it is often in SPD.
3671 * spidx1: object, it is often from PFKEY message.
3677 key_cmpspidx_exactly(spidx0
, spidx1
)
3678 struct secpolicyindex
*spidx0
, *spidx1
;
3681 if (spidx0
== NULL
&& spidx1
== NULL
)
3684 if (spidx0
== NULL
|| spidx1
== NULL
)
3687 if (spidx0
->prefs
!= spidx1
->prefs
3688 || spidx0
->prefd
!= spidx1
->prefd
3689 || spidx0
->ul_proto
!= spidx1
->ul_proto
)
3692 if (bcmp(&spidx0
->src
, &spidx1
->src
, spidx0
->src
.ss_len
) != 0
3693 || bcmp(&spidx0
->dst
, &spidx1
->dst
, spidx0
->dst
.ss_len
) != 0)
3700 * compare two secindex structure with mask.
3702 * spidx0: source, it is often in SPD.
3703 * spidx1: object, it is often from IP header.
3709 key_cmpspidx_withmask(spidx0
, spidx1
)
3710 struct secpolicyindex
*spidx0
, *spidx1
;
3713 if (spidx0
== NULL
&& spidx1
== NULL
)
3716 if (spidx0
== NULL
|| spidx1
== NULL
)
3719 if (spidx0
->src
.ss_family
!= spidx1
->src
.ss_family
3720 || spidx0
->dst
.ss_family
!= spidx1
->dst
.ss_family
)
3723 /* if spidx.ul_proto == IPSEC_ULPROTO_ANY, ignore. */
3724 if (spidx0
->ul_proto
!= (u_int16_t
)IPSEC_ULPROTO_ANY
3725 && spidx0
->ul_proto
!= spidx1
->ul_proto
)
3728 if (_INPORTBYSA(&spidx0
->src
) != IPSEC_PORT_ANY
3729 && _INPORTBYSA(&spidx0
->src
) != _INPORTBYSA(&spidx1
->src
))
3732 if (_INPORTBYSA(&spidx0
->dst
) != IPSEC_PORT_ANY
3733 && _INPORTBYSA(&spidx0
->dst
) != _INPORTBYSA(&spidx1
->dst
))
3736 if (!key_bbcmp(_INADDRBYSA(&spidx0
->src
),
3737 _INADDRBYSA(&spidx1
->src
),
3741 if (!key_bbcmp(_INADDRBYSA(&spidx0
->dst
),
3742 _INADDRBYSA(&spidx1
->dst
),
3746 /* XXX Do we check other field ? e.g. flowinfo, scope_id. */
3752 * compare two buffers with mask.
3756 * bits: Number of bits to compare
3762 key_bbcmp(p1
, p2
, bits
)
3763 register caddr_t p1
, p2
;
3764 register u_int bits
;
3768 /* XXX: This could be considerably faster if we compare a word
3769 * at a time, but it is complicated on LSB Endian machines */
3771 /* Handle null pointers */
3772 if (p1
== NULL
|| p2
== NULL
)
3782 mask
= ~((1<<(8-bits
))-1);
3783 if ((*p1
& mask
) != (*p2
& mask
))
3786 return 1; /* Match! */
3791 * scanning SPD and SAD to check status for each entries,
3792 * and do to remove or to expire.
3795 key_timehandler_funneled(void)
3797 boolean_t funnel_state
;
3798 funnel_state
= thread_funnel_set(network_flock
, TRUE
);
3800 (void) thread_funnel_set(network_flock
, FALSE
);
3804 key_timehandler(void)
3809 boolean_t funnel_state
;
3810 funnel_state
= thread_funnel_set(network_flock
, TRUE
);
3812 s
= splsoftnet(); /*called from softclock()*/
3814 s
= splnet(); /*called from softclock()*/
3819 struct secpolicy
*sp
, *nextsp
;
3821 for (dir
= 0; dir
< IPSEC_DIR_MAX
; dir
++) {
3822 for (sp
= LIST_FIRST(&sptree
[dir
]);
3826 nextsp
= LIST_NEXT(sp
, chain
);
3828 if (sp
->state
== IPSEC_SPSTATE_DEAD
)
3836 struct secashead
*sah
, *nextsah
;
3837 struct secasvar
*sav
, *nextsav
;
3839 for (sah
= LIST_FIRST(&sahtree
);
3843 nextsah
= LIST_NEXT(sah
, chain
);
3845 /* if sah has been dead, then delete it and process next sah. */
3846 if (sah
->state
== SADB_SASTATE_DEAD
) {
3851 /* if LARVAL entry doesn't become MATURE, delete it. */
3852 for (sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_LARVAL
]);
3856 nextsav
= LIST_NEXT(sav
, chain
);
3860 if (key_larval_lifetime
< sav
->tick
) {
3866 * check MATURE entry to start to send expire message
3869 for (sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_MATURE
]);
3873 nextsav
= LIST_NEXT(sav
, chain
);
3877 /* we don't need to check. */
3878 if (sav
->lft_s
== NULL
)
3882 if (sav
->lft_c
== NULL
) {
3884 printf("key_timehandler: "
3885 "There is no CURRENT time, why?\n");
3890 /* compare SOFT lifetime and tick */
3891 if (sav
->lft_s
->sadb_lifetime_addtime
!= 0
3892 && sav
->lft_s
->sadb_lifetime_addtime
< sav
->tick
) {
3894 * check SA to be used whether or not.
3895 * when SA hasn't been used, delete it.
3897 if (sav
->lft_c
->sadb_lifetime_usetime
== 0) {
3898 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
3902 key_sa_chgstate(sav
, SADB_SASTATE_DYING
);
3904 * XXX If we keep to send expire
3905 * message in the status of
3906 * DYING. Do remove below code.
3911 /* check SOFT lifetime by bytes */
3913 * XXX I don't know the way to delete this SA
3914 * when new SA is installed. Caution when it's
3915 * installed too big lifetime by time.
3917 else if (sav
->lft_s
->sadb_lifetime_bytes
!= 0
3918 && sav
->lft_s
->sadb_lifetime_bytes
< sav
->lft_c
->sadb_lifetime_bytes
) {
3920 key_sa_chgstate(sav
, SADB_SASTATE_DYING
);
3922 * XXX If we keep to send expire
3923 * message in the status of
3924 * DYING. Do remove below code.
3930 /* check DYING entry to change status to DEAD. */
3931 for (sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_DYING
]);
3935 nextsav
= LIST_NEXT(sav
, chain
);
3939 /* we don't need to check. */
3940 if (sav
->lft_h
== NULL
)
3944 if (sav
->lft_c
== NULL
) {
3946 printf("key_timehandler: "
3947 "There is no CURRENT time, why?\n");
3952 /* compare HARD lifetime and tick */
3953 if (sav
->lft_h
->sadb_lifetime_addtime
!= 0
3954 && sav
->lft_h
->sadb_lifetime_addtime
< sav
->tick
) {
3955 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
3959 #if 0 /* XXX Should we keep to send expire message until HARD lifetime ? */
3960 else if (sav
->lft_s
!= NULL
3961 && sav
->lft_s
->sadb_lifetime_addtime
!= 0
3962 && sav
->lft_s
->sadb_lifetime_addtime
< sav
->tick
) {
3964 * XXX: should be checked to be
3965 * installed the valid SA.
3969 * If there is no SA then sending
3975 /* check HARD lifetime by bytes */
3976 else if (sav
->lft_h
->sadb_lifetime_bytes
!= 0
3977 && sav
->lft_h
->sadb_lifetime_bytes
< sav
->lft_c
->sadb_lifetime_bytes
) {
3978 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
3984 /* delete entry in DEAD */
3985 for (sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_DEAD
]);
3989 nextsav
= LIST_NEXT(sav
, chain
);
3992 if (sav
->state
!= SADB_SASTATE_DEAD
) {
3994 printf("key_timehandler: "
3995 "invalid sav->state "
3996 "(queue: %d SA: %d): "
3998 SADB_SASTATE_DEAD
, sav
->state
);
4003 * do not call key_freesav() here.
4004 * sav should already be freed, and sav->refcnt
4005 * shows other references to sav
4006 * (such as from SPD).
4012 #ifndef IPSEC_NONBLOCK_ACQUIRE
4015 struct secacq
*acq
, *nextacq
;
4017 for (acq
= LIST_FIRST(&acqtree
);
4021 nextacq
= LIST_NEXT(acq
, chain
);
4025 if (key_blockacq_lifetime
< acq
->tick
&& __LIST_CHAINED(acq
)) {
4026 LIST_REMOVE(acq
, chain
);
4035 struct secspacq
*acq
, *nextacq
;
4037 for (acq
= LIST_FIRST(&spacqtree
);
4041 nextacq
= LIST_NEXT(acq
, chain
);
4045 if (key_blockacq_lifetime
< acq
->tick
&& __LIST_CHAINED(acq
)) {
4046 LIST_REMOVE(acq
, chain
);
4052 /* initialize random seed */
4053 if (key_tick_init_random
++ > key_int_random
) {
4054 key_tick_init_random
= 0;
4058 #ifndef IPSEC_DEBUG2
4059 /* do exchange to tick time !! */
4060 (void)timeout((void *)key_timehandler_funneled
, (void *)0, 100);
4061 #endif /* IPSEC_DEBUG2 */
4068 * to initialize a seed for random()
4075 extern long randseed
; /* it's defined at i386/i386/random.s */
4076 #endif /* __bsdi__ */
4080 #if defined(__FreeBSD__)
4081 srandom(tv
.tv_usec
);
4082 #endif /* __FreeBSD__ */
4083 #if defined(__APPLE__)
4087 randseed
= tv
.tv_usec
;
4088 #endif /* __bsdi__ */
4094 * map SADB_SATYPE_* to IPPROTO_*.
4095 * if satype == SADB_SATYPE then satype is mapped to ~0.
4097 * 0: invalid satype.
4100 key_satype2proto(satype
)
4104 case SADB_SATYPE_UNSPEC
:
4105 return IPSEC_PROTO_ANY
;
4106 case SADB_SATYPE_AH
:
4108 case SADB_SATYPE_ESP
:
4110 #if 1 /*nonstandard*/
4111 case SADB_X_SATYPE_IPCOMP
:
4112 return IPPROTO_IPCOMP
;
4122 * map IPPROTO_* to SADB_SATYPE_*
4124 * 0: invalid protocol type.
4127 key_proto2satype(proto
)
4132 return SADB_SATYPE_AH
;
4134 return SADB_SATYPE_ESP
;
4135 #if 1 /*nonstandard*/
4136 case IPPROTO_IPCOMP
:
4137 return SADB_X_SATYPE_IPCOMP
;
4148 * SADB_GETSPI processing is to receive
4149 * <base, src address, dst address, (SPI range)>
4150 * from the IKMPd, to assign a unique spi value, to hang on the INBOUND
4151 * tree with the status of LARVAL, and send
4152 * <base, SA(*), address(SD)>
4155 * IN: mhp: pointer to the pointer to each header.
4156 * OUT: NULL if fail.
4157 * other if success, return pointer to the message to send.
4159 static struct sadb_msg
*
4163 struct sadb_msg
*msg0
;
4164 struct sadb_address
*src0
, *dst0
;
4165 struct secasindex saidx
;
4166 struct secashead
*newsah
;
4167 struct secasvar
*newsav
;
4172 if (mhp
== NULL
|| mhp
[0] == NULL
)
4173 panic("key_getspi: NULL pointer is passed.\n");
4175 msg0
= (struct sadb_msg
*)mhp
[0];
4177 if (mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
4178 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
) {
4180 printf("key_getspi: invalid message is passed.\n");
4182 msg0
->sadb_msg_errno
= EINVAL
;
4186 src0
= (struct sadb_address
*)(mhp
[SADB_EXT_ADDRESS_SRC
]);
4187 dst0
= (struct sadb_address
*)(mhp
[SADB_EXT_ADDRESS_DST
]);
4189 /* map satype to proto */
4190 if ((proto
= key_satype2proto(msg0
->sadb_msg_satype
)) == 0) {
4192 printf("key_getspi: invalid satype is passed.\n");
4194 msg0
->sadb_msg_errno
= EINVAL
;
4198 KEY_SETSECASIDX(proto
, msg0
, src0
+1, dst0
+1, &saidx
);
4200 /* SPI allocation */
4201 spi
= key_do_getnewspi((struct sadb_spirange
*)mhp
[SADB_EXT_SPIRANGE
],
4204 msg0
->sadb_msg_errno
= EEXIST
;
4208 /* get a SA index */
4209 if ((newsah
= key_getsah(&saidx
)) == NULL
) {
4211 /* create a new SA index */
4212 if ((newsah
= key_newsah(&saidx
)) == NULL
) {
4214 printf("key_getspi: No more memory.\n");
4216 msg0
->sadb_msg_errno
= ENOBUFS
;
4222 if ((newsav
= key_newsav(mhp
, newsah
)) == NULL
) {
4223 msg0
->sadb_msg_errno
= ENOBUFS
;
4224 /* XXX don't free new SA index allocated in above. */
4229 newsav
->spi
= htonl(spi
);
4231 #ifndef IPSEC_NONBLOCK_ACQUIRE
4232 /* delete the entry in acqtree */
4233 if (msg0
->sadb_msg_seq
!= 0) {
4235 if ((acq
= key_getacqbyseq(msg0
->sadb_msg_seq
)) != NULL
) {
4236 /* reset counter in order to deletion by timehander. */
4237 acq
->tick
= key_blockacq_lifetime
;
4244 struct sadb_msg
*newmsg
;
4248 /* create new sadb_msg to reply. */
4249 len
= sizeof(struct sadb_msg
)
4250 + sizeof(struct sadb_sa
)
4251 + PFKEY_EXTLEN(mhp
[SADB_EXT_ADDRESS_SRC
])
4252 + PFKEY_EXTLEN(mhp
[SADB_EXT_ADDRESS_DST
]);
4254 KMALLOC(newmsg
, struct sadb_msg
*, len
);
4255 if (newmsg
== NULL
) {
4257 printf("key_getspi: No more memory.\n");
4259 msg0
->sadb_msg_errno
= ENOBUFS
;
4262 bzero((caddr_t
)newmsg
, len
);
4264 bcopy((caddr_t
)mhp
[0], (caddr_t
)newmsg
, sizeof(*msg0
));
4265 newmsg
->sadb_msg_seq
= newsav
->seq
;
4266 newmsg
->sadb_msg_errno
= 0;
4267 newmsg
->sadb_msg_len
= PFKEY_UNIT64(len
);
4268 p
= (caddr_t
)newmsg
+ sizeof(*msg0
);
4271 struct sadb_sa
*m_sa
;
4272 m_sa
= (struct sadb_sa
*)p
;
4273 m_sa
->sadb_sa_len
= PFKEY_UNIT64(sizeof(struct sadb_sa
));
4274 m_sa
->sadb_sa_exttype
= SADB_EXT_SA
;
4275 m_sa
->sadb_sa_spi
= htonl(spi
);
4276 p
+= sizeof(struct sadb_sa
);
4279 p
= key_setsadbext(p
, mhp
[SADB_EXT_ADDRESS_SRC
]);
4280 p
= key_setsadbext(p
, mhp
[SADB_EXT_ADDRESS_DST
]);
4287 * allocating new SPI
4288 * called by key_getspi().
4294 key_do_getnewspi(spirange
, saidx
)
4295 struct sadb_spirange
*spirange
;
4296 struct secasindex
*saidx
;
4300 int count
= key_spi_trycnt
;
4302 /* set spi range to allocate */
4303 if (spirange
!= NULL
) {
4304 min
= spirange
->sadb_spirange_min
;
4305 max
= spirange
->sadb_spirange_max
;
4307 min
= key_spi_minval
;
4308 max
= key_spi_maxval
;
4310 /* IPCOMP needs 2-byte SPI */
4311 if (saidx
->proto
== IPPROTO_IPCOMP
) {
4318 t
= min
; min
= max
; max
= t
;
4323 if (key_checkspidup(saidx
, min
) != NULL
) {
4325 printf("key_do_getnewspi: SPI %u exists already.\n", min
);
4330 count
--; /* taking one cost. */
4338 /* when requesting to allocate spi ranged */
4340 /* generate pseudo-random SPI value ranged. */
4341 newspi
= min
+ (random() % ( max
- min
+ 1 ));
4343 if (key_checkspidup(saidx
, newspi
) == NULL
)
4347 if (count
== 0 || newspi
== 0) {
4349 printf("key_do_getnewspi: to allocate spi is failed.\n");
4356 keystat
.getspi_count
=
4357 (keystat
.getspi_count
+ key_spi_trycnt
- count
) / 2;
4363 * SADB_UPDATE processing
4365 * <base, SA, (lifetime(HSC),) address(SD), (address(P),)
4366 * key(AE), (identity(SD),) (sensitivity)>
4367 * from the ikmpd, and update a secasvar entry whose status is SADB_SASTATE_LARVAL.
4369 * <base, SA, (lifetime(HSC),) address(SD), (address(P),)
4370 * (identity(SD),) (sensitivity)>
4373 * IN: mhp: pointer to the pointer to each header.
4374 * OUT: NULL if fail.
4375 * other if success, return pointer to the message to send.
4377 static struct sadb_msg
*
4381 struct sadb_msg
*msg0
;
4382 struct sadb_sa
*sa0
;
4383 struct sadb_address
*src0
, *dst0
;
4384 struct secasindex saidx
;
4385 struct secashead
*sah
;
4386 struct secasvar
*sav
;
4390 if (mhp
== NULL
|| mhp
[0] == NULL
)
4391 panic("key_update: NULL pointer is passed.\n");
4393 msg0
= (struct sadb_msg
*)mhp
[0];
4395 /* map satype to proto */
4396 if ((proto
= key_satype2proto(msg0
->sadb_msg_satype
)) == 0) {
4398 printf("key_update: invalid satype is passed.\n");
4400 msg0
->sadb_msg_errno
= EINVAL
;
4404 if (mhp
[SADB_EXT_SA
] == NULL
4405 || mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
4406 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
4407 || (msg0
->sadb_msg_satype
== SADB_SATYPE_ESP
4408 && mhp
[SADB_EXT_KEY_ENCRYPT
] == NULL
)
4409 || (msg0
->sadb_msg_satype
== SADB_SATYPE_AH
4410 && mhp
[SADB_EXT_KEY_AUTH
] == NULL
)
4411 || (mhp
[SADB_EXT_LIFETIME_HARD
] != NULL
4412 && mhp
[SADB_EXT_LIFETIME_SOFT
] == NULL
)
4413 || (mhp
[SADB_EXT_LIFETIME_HARD
] == NULL
4414 && mhp
[SADB_EXT_LIFETIME_SOFT
] != NULL
)) {
4416 printf("key_update: invalid message is passed.\n");
4418 msg0
->sadb_msg_errno
= EINVAL
;
4422 sa0
= (struct sadb_sa
*)mhp
[SADB_EXT_SA
];
4423 src0
= (struct sadb_address
*)(mhp
[SADB_EXT_ADDRESS_SRC
]);
4424 dst0
= (struct sadb_address
*)(mhp
[SADB_EXT_ADDRESS_DST
]);
4426 KEY_SETSECASIDX(proto
, msg0
, src0
+1, dst0
+1, &saidx
);
4428 /* get a SA header */
4429 if ((sah
= key_getsah(&saidx
)) == NULL
) {
4431 printf("key_update: no SA index found.\n");
4433 msg0
->sadb_msg_errno
= ENOENT
;
4437 /* set spidx if there */
4438 if (key_setident(sah
, mhp
) < 0)
4441 /* find a SA with sequence number. */
4442 #if IPSEC_DOSEQCHECK
4443 if (msg0
->sadb_msg_seq
!= 0
4444 && (sav
= key_getsavbyseq(sah
, msg0
->sadb_msg_seq
)) == NULL
) {
4446 printf("key_update: no larval SA with sequence %u exists.\n",
4447 msg0
->sadb_msg_seq
);
4449 msg0
->sadb_msg_errno
= ENOENT
;
4453 if ((sav
= key_getsavbyspi(sah
, sa0
->sadb_sa_spi
)) == NULL
) {
4455 printf("key_update: no such a SA found (spi:%u)\n",
4456 (u_int32_t
)ntohl(sa0
->sadb_sa_spi
));
4458 msg0
->sadb_msg_errno
= EINVAL
;
4463 /* validity check */
4464 if (sav
->sah
->saidx
.proto
!= proto
) {
4466 printf("key_update: protocol mismatched (DB=%u param=%u)\n",
4467 sav
->sah
->saidx
.proto
, proto
);
4469 msg0
->sadb_msg_errno
= EINVAL
;
4472 #if IPSEC_DOSEQCHECK
4473 if (sav
->spi
!= sa0
->sadb_sa_spi
) {
4475 printf("key_update: SPI mismatched (DB:%u param:%u)\n",
4476 (u_int32_t
)ntohl(sav
->spi
),
4477 (u_int32_t
)ntohl(sa0
->sadb_sa_spi
));
4479 msg0
->sadb_msg_errno
= EINVAL
;
4483 if (sav
->pid
!= msg0
->sadb_msg_pid
) {
4485 printf("key_update: pid mismatched (DB:%u param:%u)\n",
4486 sav
->pid
, msg0
->sadb_msg_pid
);
4488 msg0
->sadb_msg_errno
= EINVAL
;
4492 /* copy sav values */
4493 if (key_setsaval(sav
, mhp
)) {
4498 /* check SA values to be mature. */
4499 if ((msg0
->sadb_msg_errno
= key_mature(sav
)) != 0) {
4505 struct sadb_msg
*newmsg
;
4507 /* set msg buf from mhp */
4508 if ((newmsg
= key_getmsgbuf_x1(mhp
)) == NULL
) {
4510 printf("key_update: No more memory.\n");
4512 msg0
->sadb_msg_errno
= ENOBUFS
;
4520 * search SAD with sequence for a SA which state is SADB_SASTATE_LARVAL.
4521 * only called by key_update().
4524 * others : found, pointer to a SA.
4526 #if IPSEC_DOSEQCHECK
4527 static struct secasvar
*
4528 key_getsavbyseq(sah
, seq
)
4529 struct secashead
*sah
;
4532 struct secasvar
*sav
;
4535 state
= SADB_SASTATE_LARVAL
;
4537 /* search SAD with sequence number ? */
4538 LIST_FOREACH(sav
, &sah
->savtree
[state
], chain
) {
4540 KEY_CHKSASTATE(state
, sav
->state
, "key_getsabyseq");
4542 if (sav
->seq
== seq
) {
4544 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
4545 printf("DP key_getsavbyseq cause "
4546 "refcnt++:%d SA:%p\n",
4557 * SADB_ADD processing
4558 * add a entry to SA database, when received
4559 * <base, SA, (lifetime(HSC),) address(SD), (address(P),)
4560 * key(AE), (identity(SD),) (sensitivity)>
4563 * <base, SA, (lifetime(HSC),) address(SD), (address(P),)
4564 * (identity(SD),) (sensitivity)>
4567 * IGNORE identity and sensitivity messages.
4569 * IN: mhp: pointer to the pointer to each header.
4570 * OUT: NULL if fail.
4571 * other if success, return pointer to the message to send.
4573 static struct sadb_msg
*
4577 struct sadb_msg
*msg0
;
4578 struct sadb_sa
*sa0
;
4579 struct sadb_address
*src0
, *dst0
;
4580 struct secasindex saidx
;
4581 struct secashead
*newsah
;
4582 struct secasvar
*newsav
;
4586 if (mhp
== NULL
|| mhp
[0] == NULL
)
4587 panic("key_add: NULL pointer is passed.\n");
4589 msg0
= (struct sadb_msg
*)mhp
[0];
4591 /* map satype to proto */
4592 if ((proto
= key_satype2proto(msg0
->sadb_msg_satype
)) == 0) {
4594 printf("key_add: invalid satype is passed.\n");
4596 msg0
->sadb_msg_errno
= EINVAL
;
4600 if (mhp
[SADB_EXT_SA
] == NULL
4601 || mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
4602 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
4603 || (msg0
->sadb_msg_satype
== SADB_SATYPE_ESP
4604 && mhp
[SADB_EXT_KEY_ENCRYPT
] == NULL
)
4605 || (msg0
->sadb_msg_satype
== SADB_SATYPE_AH
4606 && mhp
[SADB_EXT_KEY_AUTH
] == NULL
)
4607 || (mhp
[SADB_EXT_LIFETIME_HARD
] != NULL
4608 && mhp
[SADB_EXT_LIFETIME_SOFT
] == NULL
)
4609 || (mhp
[SADB_EXT_LIFETIME_HARD
] == NULL
4610 && mhp
[SADB_EXT_LIFETIME_SOFT
] != NULL
)) {
4612 printf("key_add: invalid message is passed.\n");
4614 msg0
->sadb_msg_errno
= EINVAL
;
4618 sa0
= (struct sadb_sa
*)mhp
[SADB_EXT_SA
];
4619 src0
= (struct sadb_address
*)(mhp
[SADB_EXT_ADDRESS_SRC
]);
4620 dst0
= (struct sadb_address
*)(mhp
[SADB_EXT_ADDRESS_DST
]);
4622 KEY_SETSECASIDX(proto
, msg0
, src0
+1, dst0
+1, &saidx
);
4624 /* get a SA header */
4625 if ((newsah
= key_getsah(&saidx
)) == NULL
) {
4627 /* create a new SA header */
4628 if ((newsah
= key_newsah(&saidx
)) == NULL
) {
4630 printf("key_add: No more memory.\n");
4632 msg0
->sadb_msg_errno
= ENOBUFS
;
4637 /* set spidx if there */
4638 if (key_setident(newsah
, mhp
) < 0)
4641 /* create new SA entry. */
4642 /* We can create new SA only if SPI is differenct. */
4643 if (key_getsavbyspi(newsah
, sa0
->sadb_sa_spi
)) {
4645 printf("key_add: SA already exists.\n");
4647 msg0
->sadb_msg_errno
= EEXIST
;
4650 if ((newsav
= key_newsav(mhp
, newsah
)) == NULL
)
4653 /* check SA values to be mature. */
4654 if ((msg0
->sadb_msg_errno
= key_mature(newsav
)) != NULL
) {
4655 key_freesav(newsav
);
4660 * don't call key_freesav() here, as we would like to keep the SA
4661 * in the database on success.
4665 struct sadb_msg
*newmsg
;
4667 /* set msg buf from mhp */
4668 if ((newmsg
= key_getmsgbuf_x1(mhp
)) == NULL
) {
4670 printf("key_add: No more memory.\n");
4672 msg0
->sadb_msg_errno
= ENOBUFS
;
4681 key_setident(sah
, mhp
)
4682 struct secashead
*sah
;
4685 struct sadb_msg
*msg0
;
4686 struct sadb_ident
*idsrc
, *iddst
;
4687 int idsrclen
, iddstlen
;
4690 if (sah
== NULL
|| mhp
== NULL
|| mhp
[0] == NULL
)
4691 panic("key_setident: NULL pointer is passed.\n");
4693 msg0
= (struct sadb_msg
*)mhp
[0];
4695 /* don't make buffer if not there */
4696 if (mhp
[SADB_EXT_IDENTITY_SRC
] == NULL
4697 && mhp
[SADB_EXT_IDENTITY_DST
] == NULL
) {
4703 if (mhp
[SADB_EXT_IDENTITY_SRC
] == NULL
4704 || mhp
[SADB_EXT_IDENTITY_DST
] == NULL
) {
4706 printf("key_setident: invalid identity.\n");
4708 msg0
->sadb_msg_errno
= EINVAL
;
4712 idsrc
= (struct sadb_ident
*)mhp
[SADB_EXT_IDENTITY_SRC
];
4713 iddst
= (struct sadb_ident
*)mhp
[SADB_EXT_IDENTITY_DST
];
4714 idsrclen
= PFKEY_UNUNIT64(idsrc
->sadb_ident_len
);
4715 iddstlen
= PFKEY_UNUNIT64(idsrc
->sadb_ident_len
);
4717 /* validity check */
4718 if (idsrc
->sadb_ident_type
!= iddst
->sadb_ident_type
) {
4720 printf("key_setident: ident type mismatch.\n");
4722 msg0
->sadb_msg_errno
= EINVAL
;
4726 switch (idsrc
->sadb_ident_type
) {
4727 case SADB_X_IDENTTYPE_ADDR
:
4729 sizeof(*idsrc
) + ((struct sockaddr
*)(idsrc
+ 1))->sa_len
4731 sizeof(*iddst
) + ((struct sockaddr
*)(iddst
+ 1))->sa_len
) {
4733 printf("key_setident: invalid length is passed.\n");
4735 msg0
->sadb_msg_errno
= EINVAL
;
4738 if (((struct sockaddr
*)(idsrc
+ 1))->sa_len
>
4739 sizeof(struct sockaddr_storage
)
4740 || ((struct sockaddr
*)(iddst
+ 1))->sa_len
>
4741 sizeof(struct sockaddr_storage
)) {
4743 printf("key_setident: invalid sa_len is passed.\n");
4745 msg0
->sadb_msg_errno
= EINVAL
;
4748 #define __IDENTXID(a) ((union sadb_x_ident_id *)&(a)->sadb_ident_id)
4749 if (__IDENTXID(idsrc
)->sadb_x_ident_id_addr
.ul_proto
4750 != __IDENTXID(iddst
)->sadb_x_ident_id_addr
.ul_proto
) {
4752 printf("key_setident: ul_proto mismatch.\n");
4754 msg0
->sadb_msg_errno
= EINVAL
;
4757 #undef __IDENTXID(a)
4759 case SADB_IDENTTYPE_PREFIX
:
4760 case SADB_IDENTTYPE_FQDN
:
4761 case SADB_IDENTTYPE_USERFQDN
:
4763 /* XXX do nothing */
4769 /* make structure */
4770 KMALLOC(sah
->idents
, struct sadb_ident
*, idsrclen
);
4771 if (sah
->idents
== NULL
) {
4773 printf("key_setident: No more memory.\n");
4775 msg0
->sadb_msg_errno
= ENOBUFS
;
4778 KMALLOC(sah
->identd
, struct sadb_ident
*, iddstlen
);
4779 if (sah
->identd
== NULL
) {
4782 printf("key_setident: No more memory.\n");
4784 msg0
->sadb_msg_errno
= ENOBUFS
;
4787 bcopy(idsrc
, sah
->idents
, idsrclen
);
4788 bcopy(iddst
, sah
->identd
, iddstlen
);
4793 static struct sadb_msg
*
4794 key_getmsgbuf_x1(mhp
)
4797 struct sadb_msg
*msg0
;
4798 struct sadb_msg
*newmsg
;
4803 if (mhp
== NULL
|| mhp
[0] == NULL
)
4804 panic("key_getmsgbuf_x1: NULL pointer is passed.\n");
4806 msg0
= (struct sadb_msg
*)mhp
[0];
4808 /* create new sadb_msg to reply. */
4809 len
= sizeof(struct sadb_msg
)
4810 + sizeof(struct sadb_sa
)
4811 + PFKEY_EXTLEN(mhp
[SADB_EXT_ADDRESS_SRC
])
4812 + PFKEY_EXTLEN(mhp
[SADB_EXT_ADDRESS_DST
])
4813 + (mhp
[SADB_EXT_LIFETIME_HARD
] == NULL
4814 ? 0 : sizeof(struct sadb_lifetime
))
4815 + (mhp
[SADB_EXT_LIFETIME_SOFT
] == NULL
4816 ? 0 : sizeof(struct sadb_lifetime
))
4817 + (mhp
[SADB_EXT_IDENTITY_SRC
] == NULL
4818 ? 0 : PFKEY_EXTLEN(mhp
[SADB_EXT_IDENTITY_SRC
]))
4819 + (mhp
[SADB_EXT_IDENTITY_DST
] == NULL
4820 ? 0 : PFKEY_EXTLEN(mhp
[SADB_EXT_IDENTITY_DST
]));
4822 KMALLOC(newmsg
, struct sadb_msg
*, len
);
4825 bzero((caddr_t
)newmsg
, len
);
4827 bcopy((caddr_t
)mhp
[0], (caddr_t
)newmsg
, sizeof(*msg0
));
4828 newmsg
->sadb_msg_errno
= 0;
4829 newmsg
->sadb_msg_len
= PFKEY_UNIT64(len
);
4830 p
= (caddr_t
)newmsg
+ sizeof(*msg0
);
4832 p
= key_setsadbext(p
, mhp
[SADB_EXT_SA
]);
4833 p
= key_setsadbext(p
, mhp
[SADB_EXT_ADDRESS_SRC
]);
4834 p
= key_setsadbext(p
, mhp
[SADB_EXT_ADDRESS_DST
]);
4836 if (mhp
[SADB_EXT_LIFETIME_HARD
] != NULL
)
4837 p
= key_setsadbext(p
, mhp
[SADB_EXT_LIFETIME_HARD
]);
4839 if (mhp
[SADB_EXT_LIFETIME_SOFT
] != NULL
)
4840 p
= key_setsadbext(p
, mhp
[SADB_EXT_LIFETIME_SOFT
]);
4842 if (mhp
[SADB_EXT_IDENTITY_SRC
] != NULL
)
4843 p
= key_setsadbext(p
, mhp
[SADB_EXT_IDENTITY_SRC
]);
4844 if (mhp
[SADB_EXT_IDENTITY_DST
] != NULL
)
4845 p
= key_setsadbext(p
, mhp
[SADB_EXT_IDENTITY_DST
]);
4851 * SADB_DELETE processing
4853 * <base, SA(*), address(SD)>
4854 * from the ikmpd, and set SADB_SASTATE_DEAD,
4856 * <base, SA(*), address(SD)>
4859 * IN: mhp: pointer to the pointer to each header.
4860 * OUT: NULL if fail.
4861 * other if success, return pointer to the message to send.
4863 static struct sadb_msg
*
4867 struct sadb_msg
*msg0
;
4868 struct sadb_sa
*sa0
;
4869 struct sadb_address
*src0
, *dst0
;
4870 struct secasindex saidx
;
4871 struct secashead
*sah
;
4872 struct secasvar
*sav
;
4876 if (mhp
== NULL
|| mhp
[0] == NULL
)
4877 panic("key_delete: NULL pointer is passed.\n");
4879 msg0
= (struct sadb_msg
*)mhp
[0];
4881 /* map satype to proto */
4882 if ((proto
= key_satype2proto(msg0
->sadb_msg_satype
)) == 0) {
4884 printf("key_delete: invalid satype is passed.\n");
4886 msg0
->sadb_msg_errno
= EINVAL
;
4890 if (mhp
[SADB_EXT_SA
] == NULL
4891 || mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
4892 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
) {
4894 printf("key_delete: invalid message is passed.\n");
4896 msg0
->sadb_msg_errno
= EINVAL
;
4899 sa0
= (struct sadb_sa
*)mhp
[SADB_EXT_SA
];
4900 src0
= (struct sadb_address
*)(mhp
[SADB_EXT_ADDRESS_SRC
]);
4901 dst0
= (struct sadb_address
*)(mhp
[SADB_EXT_ADDRESS_DST
]);
4903 KEY_SETSECASIDX(proto
, msg0
, src0
+1, dst0
+1, &saidx
);
4905 /* get a SA header */
4906 if ((sah
= key_getsah(&saidx
)) == NULL
) {
4908 printf("key_delete: no SA found.\n");
4910 msg0
->sadb_msg_errno
= ENOENT
;
4914 /* get a SA with SPI. */
4915 sav
= key_getsavbyspi(sah
, sa0
->sadb_sa_spi
);
4918 printf("key_delete: no alive SA found.\n");
4920 msg0
->sadb_msg_errno
= ENOENT
;
4924 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
4929 struct sadb_msg
*newmsg
;
4933 /* create new sadb_msg to reply. */
4934 len
= sizeof(struct sadb_msg
)
4935 + sizeof(struct sadb_sa
)
4936 + PFKEY_EXTLEN(mhp
[SADB_EXT_ADDRESS_SRC
])
4937 + PFKEY_EXTLEN(mhp
[SADB_EXT_ADDRESS_DST
]);
4939 KMALLOC(newmsg
, struct sadb_msg
*, len
);
4940 if (newmsg
== NULL
) {
4942 printf("key_delete: No more memory.\n");
4944 msg0
->sadb_msg_errno
= ENOBUFS
;
4947 bzero((caddr_t
)newmsg
, len
);
4949 bcopy((caddr_t
)mhp
[0], (caddr_t
)newmsg
, sizeof(*msg0
));
4950 newmsg
->sadb_msg_errno
= 0;
4951 newmsg
->sadb_msg_len
= PFKEY_UNIT64(len
);
4952 p
= (caddr_t
)newmsg
+ sizeof(*msg0
);
4954 p
= key_setsadbext(p
, mhp
[SADB_EXT_SA
]);
4955 p
= key_setsadbext(p
, mhp
[SADB_EXT_ADDRESS_SRC
]);
4956 p
= key_setsadbext(p
, mhp
[SADB_EXT_ADDRESS_DST
]);
4963 * SADB_GET processing
4965 * <base, SA(*), address(SD)>
4966 * from the ikmpd, and get a SP and a SA to respond,
4968 * <base, SA, (lifetime(HSC),) address(SD), (address(P),) key(AE),
4969 * (identity(SD),) (sensitivity)>
4972 * IN: mhp: pointer to the pointer to each header.
4973 * OUT: NULL if fail.
4974 * other if success, return pointer to the message to send.
4976 static struct sadb_msg
*
4980 struct sadb_msg
*msg0
;
4981 struct sadb_sa
*sa0
;
4982 struct sadb_address
*src0
, *dst0
;
4983 struct secasindex saidx
;
4984 struct secashead
*sah
;
4985 struct secasvar
*sav
;
4989 if (mhp
== NULL
|| mhp
[0] == NULL
)
4990 panic("key_get: NULL pointer is passed.\n");
4992 msg0
= (struct sadb_msg
*)mhp
[0];
4994 /* map satype to proto */
4995 if ((proto
= key_satype2proto(msg0
->sadb_msg_satype
)) == 0) {
4997 printf("key_get: invalid satype is passed.\n");
4999 msg0
->sadb_msg_errno
= EINVAL
;
5003 if (mhp
[SADB_EXT_SA
] == NULL
5004 || mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
5005 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
) {
5007 printf("key_get: invalid message is passed.\n");
5009 msg0
->sadb_msg_errno
= EINVAL
;
5012 sa0
= (struct sadb_sa
*)mhp
[SADB_EXT_SA
];
5013 src0
= (struct sadb_address
*)(mhp
[SADB_EXT_ADDRESS_SRC
]);
5014 dst0
= (struct sadb_address
*)(mhp
[SADB_EXT_ADDRESS_DST
]);
5016 KEY_SETSECASIDX(proto
, msg0
, src0
+1, dst0
+1, &saidx
);
5018 /* get a SA header */
5019 if ((sah
= key_getsah(&saidx
)) == NULL
) {
5021 printf("key_get: no SA found.\n");
5023 msg0
->sadb_msg_errno
= ENOENT
;
5027 /* get a SA with SPI. */
5028 sav
= key_getsavbyspi(sah
, sa0
->sadb_sa_spi
);
5031 printf("key_get: no SA with state of mature found.\n");
5033 msg0
->sadb_msg_errno
= ENOENT
;
5038 struct sadb_msg
*newmsg
;
5042 /* map proto to satype */
5043 if ((satype
= key_proto2satype(sah
->saidx
.proto
)) == 0) {
5045 printf("key_get: there was invalid proto in SAD.\n");
5047 msg0
->sadb_msg_errno
= EINVAL
;
5051 /* calculate a length of message buffer */
5052 len
= key_getmsglen(sav
);
5054 KMALLOC(newmsg
, struct sadb_msg
*, len
);
5055 if (newmsg
== NULL
) {
5057 printf("key_get: No more memory.\n");
5059 msg0
->sadb_msg_errno
= ENOBUFS
;
5063 /* create new sadb_msg to reply. */
5064 (void)key_setdumpsa(newmsg
, sav
, SADB_GET
,
5065 satype
, msg0
->sadb_msg_seq
, msg0
->sadb_msg_pid
);
5072 * SADB_ACQUIRE processing called by key_checkrequest() and key_acquire2().
5074 * <base, SA, address(SD), (address(P)),
5075 * (identity(SD),) (sensitivity,) proposal>
5076 * to KMD, and expect to receive
5077 * <base> with SADB_ACQUIRE if error occured,
5079 * <base, src address, dst address, (SPI range)> with SADB_GETSPI
5080 * from KMD by PF_KEY.
5082 * sensitivity is not supported.
5086 * others: error number
5089 key_acquire(saidx
, sp
)
5090 struct secasindex
*saidx
;
5091 struct secpolicy
*sp
;
5093 #ifndef IPSEC_NONBLOCK_ACQUIRE
5094 struct secacq
*newacq
;
5096 struct secpolicyindex
*spidx
= NULL
;
5101 if (saidx
== NULL
|| sp
== NULL
)
5102 panic("key_acquire: NULL pointer is passed.\n");
5103 if ((satype
= key_proto2satype(saidx
->proto
)) == 0)
5104 panic("key_acquire: invalid proto is passed.\n");
5108 #ifndef IPSEC_NONBLOCK_ACQUIRE
5110 * We never do anything about acquirng SA. There is anather
5111 * solution that kernel blocks to send SADB_ACQUIRE message until
5112 * getting something message from IKEd. In later case, to be
5113 * managed with ACQUIRING list.
5115 /* get a entry to check whether sending message or not. */
5116 if ((newacq
= key_getacq(saidx
)) != NULL
) {
5117 if (key_blockacq_count
< newacq
->count
) {
5118 /* reset counter and do send message. */
5121 /* increment counter and do nothing. */
5126 /* make new entry for blocking to send SADB_ACQUIRE. */
5127 if ((newacq
= key_newacq(saidx
)) == NULL
)
5130 /* add to acqtree */
5131 LIST_INSERT_HEAD(&acqtree
, newacq
, chain
);
5136 struct sadb_msg
*newmsg
= NULL
;
5137 union sadb_x_ident_id id
;
5141 /* create new sadb_msg to reply. */
5142 len
= sizeof(struct sadb_msg
)
5143 + sizeof(struct sadb_address
)
5144 + PFKEY_ALIGN8(saidx
->src
.ss_len
)
5145 + sizeof(struct sadb_address
)
5146 + PFKEY_ALIGN8(saidx
->dst
.ss_len
)
5147 + sizeof(struct sadb_x_policy
)
5148 + sizeof(struct sadb_ident
)
5149 + PFKEY_ALIGN8(spidx
->src
.ss_len
)
5150 + sizeof(struct sadb_ident
)
5151 + PFKEY_ALIGN8(spidx
->dst
.ss_len
)
5152 + sizeof(struct sadb_prop
)
5153 + sizeof(struct sadb_comb
); /* XXX to be multiple */
5155 KMALLOC(newmsg
, struct sadb_msg
*, len
);
5158 printf("key_acquire: No more memory.\n");
5162 bzero((caddr_t
)newmsg
, len
);
5164 newmsg
->sadb_msg_version
= PF_KEY_V2
;
5165 newmsg
->sadb_msg_type
= SADB_ACQUIRE
;
5166 newmsg
->sadb_msg_errno
= 0;
5167 newmsg
->sadb_msg_satype
= satype
;
5168 newmsg
->sadb_msg_len
= PFKEY_UNIT64(len
);
5169 newmsg
->sadb_msg_mode
= saidx
->mode
;
5170 newmsg
->sadb_msg_reqid
= saidx
->reqid
;
5172 #ifndef IPSEC_NONBLOCK_ACQUIRE
5173 newmsg
->sadb_msg_seq
= newacq
->seq
;
5175 newmsg
->sadb_msg_seq
= (acq_seq
= (acq_seq
== ~0 ? 1 : ++acq_seq
));
5178 newmsg
->sadb_msg_pid
= 0;
5180 p
= (caddr_t
)newmsg
+ sizeof(struct sadb_msg
);
5182 /* set sadb_address for saidx's. */
5183 p
= key_setsadbaddr(p
,
5184 SADB_EXT_ADDRESS_SRC
,
5185 (struct sockaddr
*)&saidx
->src
,
5186 _INALENBYAF(saidx
->src
.ss_family
) << 3,
5188 p
= key_setsadbaddr(p
,
5189 SADB_EXT_ADDRESS_DST
,
5190 (struct sockaddr
*)&saidx
->dst
,
5191 _INALENBYAF(saidx
->dst
.ss_family
) << 3,
5194 /* set sadb_x_policy */
5195 p
= key_setsadbxpolicy(p
, sp
->policy
, sp
->spidx
.dir
, sp
->id
);
5197 /* set sadb_address for spidx's. */
5198 bzero(&id
, sizeof(id
));
5199 id
.sadb_x_ident_id_addr
.prefix
= spidx
->prefs
;
5200 id
.sadb_x_ident_id_addr
.ul_proto
= spidx
->ul_proto
;
5201 p
= key_setsadbident(p
,
5202 SADB_EXT_IDENTITY_SRC
,
5203 SADB_X_IDENTTYPE_ADDR
,
5204 (caddr_t
)&spidx
->src
,
5208 bzero(&id
, sizeof(id
));
5209 id
.sadb_x_ident_id_addr
.prefix
= spidx
->prefd
;
5210 id
.sadb_x_ident_id_addr
.ul_proto
= spidx
->ul_proto
;
5211 p
= key_setsadbident(p
,
5212 SADB_EXT_IDENTITY_DST
,
5213 SADB_X_IDENTTYPE_ADDR
,
5214 (caddr_t
)&spidx
->dst
,
5218 /* create proposal extension */
5219 /* set combination extension */
5220 /* XXX: to be defined by proposal database */
5222 struct sadb_prop
*prop
;
5223 struct sadb_comb
*comb
;
5225 prop
= (struct sadb_prop
*)p
;
5226 prop
->sadb_prop_len
= PFKEY_UNIT64(sizeof(*prop
) + sizeof(*comb
));
5227 /* XXX to be multiple */
5228 prop
->sadb_prop_exttype
= SADB_EXT_PROPOSAL
;
5229 prop
->sadb_prop_replay
= 32; /* XXX be variable ? */
5230 p
+= sizeof(struct sadb_prop
);
5232 comb
= (struct sadb_comb
*)p
;
5233 comb
->sadb_comb_auth
= SADB_AALG_SHA1HMAC
; /* XXX ??? */
5234 comb
->sadb_comb_encrypt
= SADB_EALG_DESCBC
; /* XXX ??? */
5235 comb
->sadb_comb_flags
= 0;
5236 comb
->sadb_comb_auth_minbits
= 8; /* XXX */
5237 comb
->sadb_comb_auth_maxbits
= 1024; /* XXX */
5238 comb
->sadb_comb_encrypt_minbits
= 64; /* XXX */
5239 comb
->sadb_comb_encrypt_maxbits
= 64; /* XXX */
5240 comb
->sadb_comb_soft_allocations
= 0;
5241 comb
->sadb_comb_hard_allocations
= 0;
5242 comb
->sadb_comb_soft_bytes
= 0;
5243 comb
->sadb_comb_hard_bytes
= 0;
5244 comb
->sadb_comb_soft_addtime
= 0;
5245 comb
->sadb_comb_hard_addtime
= 0;
5246 comb
->sadb_comb_soft_usetime
= 0;
5247 comb
->sadb_comb_hard_usetime
= 0;
5252 #if 0 /* XXX Do it ?*/
5253 if (idexttype
&& fqdn
) {
5254 /* create identity extension (FQDN) */
5255 struct sadb_ident
*id
;
5258 fqdnlen
= strlen(fqdn
) + 1; /* +1 for terminating-NUL */
5259 id
= (struct sadb_ident
*)p
;
5260 bzero(id
, sizeof(*id
) + PFKEY_ALIGN8(fqdnlen
));
5261 id
->sadb_ident_len
= PFKEY_UNIT64(sizeof(*id
) + PFKEY_ALIGN8(fqdnlen
));
5262 id
->sadb_ident_exttype
= idexttype
;
5263 id
->sadb_ident_type
= SADB_IDENTTYPE_FQDN
;
5264 bcopy(fqdn
, id
+ 1, fqdnlen
);
5265 p
+= sizeof(struct sadb_ident
) + PFKEY_ALIGN8(fqdnlen
);
5269 /* create identity extension (USERFQDN) */
5270 struct sadb_ident
*id
;
5274 /* +1 for terminating-NUL */
5275 userfqdnlen
= strlen(userfqdn
) + 1;
5278 id
= (struct sadb_ident
*)p
;
5279 bzero(id
, sizeof(*id
) + PFKEY_ALIGN8(userfqdnlen
));
5280 id
->sadb_ident_len
= PFKEY_UNIT64(sizeof(*id
) + PFKEY_ALIGN8(userfqdnlen
));
5281 id
->sadb_ident_exttype
= idexttype
;
5282 id
->sadb_ident_type
= SADB_IDENTTYPE_USERFQDN
;
5283 /* XXX is it correct? */
5284 if (curproc
&& curproc
->p_cred
)
5285 id
->sadb_ident_id
= curproc
->p_cred
->p_ruid
;
5286 if (userfqdn
&& userfqdnlen
)
5287 bcopy(userfqdn
, id
+ 1, userfqdnlen
);
5288 p
+= sizeof(struct sadb_ident
) + PFKEY_ALIGN8(userfqdnlen
);
5292 error
= key_sendall(newmsg
, len
);
5295 printf("key_acquire: key_sendall returned %d\n", error
);
5303 #ifndef IPSEC_NONBLOCK_ACQUIRE
5304 static struct secacq
*
5306 struct secasindex
*saidx
;
5308 struct secacq
*newacq
;
5311 KMALLOC(newacq
, struct secacq
*, sizeof(struct secacq
));
5312 if (newacq
== NULL
) {
5314 printf("key_newacq: No more memory.\n");
5318 bzero(newacq
, sizeof(*newacq
));
5321 bcopy(saidx
, &newacq
->saidx
, sizeof(newacq
->saidx
));
5322 newacq
->seq
= (acq_seq
== ~0 ? 1 : ++acq_seq
);
5329 static struct secacq
*
5331 struct secasindex
*saidx
;
5335 LIST_FOREACH(acq
, &acqtree
, chain
) {
5336 if (key_cmpsaidx_exactly(saidx
, &acq
->saidx
))
5343 static struct secacq
*
5344 key_getacqbyseq(seq
)
5349 LIST_FOREACH(acq
, &acqtree
, chain
) {
5350 if (acq
->seq
== seq
)
5358 static struct secspacq
*
5360 struct secpolicyindex
*spidx
;
5362 struct secspacq
*acq
;
5365 KMALLOC(acq
, struct secspacq
*, sizeof(struct secspacq
));
5368 printf("key_newspacq: No more memory.\n");
5372 bzero(acq
, sizeof(*acq
));
5375 bcopy(spidx
, &acq
->spidx
, sizeof(acq
->spidx
));
5382 static struct secspacq
*
5384 struct secpolicyindex
*spidx
;
5386 struct secspacq
*acq
;
5388 LIST_FOREACH(acq
, &spacqtree
, chain
) {
5389 if (key_cmpspidx_exactly(spidx
, &acq
->spidx
))
5397 * SADB_ACQUIRE processing,
5398 * in first situation, is receiving
5400 * from the ikmpd, and clear sequence of its secasvar entry.
5402 * In second situation, is receiving
5403 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
5404 * from a user land process, and return
5405 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
5408 * IN: mhp: pointer to the pointer to each header.
5409 * OUT: NULL if fail.
5410 * other if success, return pointer to the message to send.
5412 static struct sadb_msg
*
5416 struct sadb_msg
*msg0
;
5417 struct sadb_address
*src0
, *dst0
;
5418 struct secasindex saidx
;
5419 struct secashead
*sah
;
5423 if (mhp
== NULL
|| mhp
[0] == NULL
)
5424 panic("key_acquire2: NULL pointer is passed.\n");
5426 msg0
= (struct sadb_msg
*)mhp
[0];
5429 * Error message from KMd.
5430 * We assume that if error was occured in IKEd, the length of PFKEY
5431 * message is equal to the size of sadb_msg structure.
5432 * We return ~0 even if error occured in this function.
5434 if (msg0
->sadb_msg_len
== PFKEY_UNIT64(sizeof(struct sadb_msg
))) {
5436 #ifndef IPSEC_NONBLOCK_ACQUIRE
5439 /* check sequence number */
5440 if (msg0
->sadb_msg_seq
== 0) {
5442 printf("key_acquire2: must specify sequence number.\n");
5444 return (struct sadb_msg
*)~0;
5447 if ((acq
= key_getacqbyseq(msg0
->sadb_msg_seq
)) == NULL
) {
5449 printf("key_acquire2: "
5450 "invalid sequence number is passed.\n");
5452 return (struct sadb_msg
*)~0;
5455 /* reset acq counter in order to deletion by timehander. */
5456 acq
->tick
= key_blockacq_lifetime
;
5459 return (struct sadb_msg
*)~0;
5464 * This message is from user land.
5467 /* map satype to proto */
5468 if ((proto
= key_satype2proto(msg0
->sadb_msg_satype
)) == 0) {
5470 printf("key_acquire2: invalid satype is passed.\n");
5472 msg0
->sadb_msg_errno
= EINVAL
;
5476 if (mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
5477 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
5478 || mhp
[SADB_EXT_PROPOSAL
] == NULL
) {
5481 printf("key_acquire2: invalid message is passed.\n");
5483 msg0
->sadb_msg_errno
= EINVAL
;
5486 src0
= (struct sadb_address
*)(mhp
[SADB_EXT_ADDRESS_SRC
]);
5487 dst0
= (struct sadb_address
*)(mhp
[SADB_EXT_ADDRESS_DST
]);
5489 KEY_SETSECASIDX(proto
, msg0
, src0
+1, dst0
+1, &saidx
);
5491 /* get a SA index */
5492 if ((sah
= key_getsah(&saidx
)) != NULL
) {
5494 printf("key_acquire2: a SA exists already.\n");
5496 msg0
->sadb_msg_errno
= EEXIST
;
5500 msg0
->sadb_msg_errno
= key_acquire(&saidx
, NULL
);
5501 if (msg0
->sadb_msg_errno
!= 0) {
5503 printf("key_acquire2: error %d returned "
5504 "from key_acquire.\n", msg0
->sadb_msg_errno
);
5510 struct sadb_msg
*newmsg
;
5513 /* create new sadb_msg to reply. */
5514 len
= PFKEY_UNUNIT64(msg0
->sadb_msg_len
);
5516 KMALLOC(newmsg
, struct sadb_msg
*, len
);
5517 if (newmsg
== NULL
) {
5519 printf("key_acquire2: No more memory.\n");
5521 msg0
->sadb_msg_errno
= ENOBUFS
;
5524 bzero((caddr_t
)newmsg
, len
);
5526 bcopy(mhp
[0], (caddr_t
)newmsg
, len
);
5533 * SADB_REGISTER processing.
5534 * If SATYPE_UNSPEC has been passed as satype, only return sabd_supported.
5537 * from the ikmpd, and register a socket to send PF_KEY messages,
5541 * If socket is detached, must free from regnode.
5544 * others: error number
5546 static struct sadb_msg
*
5547 key_register(mhp
, so
)
5551 struct sadb_msg
*msg0
;
5552 struct secreg
*reg
, *newreg
= 0;
5555 if (mhp
== NULL
|| so
== NULL
|| mhp
[0] == NULL
)
5556 panic("key_register: NULL pointer is passed.\n");
5558 msg0
= (struct sadb_msg
*)mhp
[0];
5560 /* check for invalid register message */
5561 if (msg0
->sadb_msg_satype
>= sizeof(regtree
)/sizeof(regtree
[0])) {
5562 msg0
->sadb_msg_errno
= EINVAL
;
5566 /* When SATYPE_UNSPEC is specified, only return sabd_supported. */
5567 if (msg0
->sadb_msg_satype
== SADB_SATYPE_UNSPEC
)
5570 /* check whether existing or not */
5571 LIST_FOREACH(reg
, ®tree
[msg0
->sadb_msg_satype
], chain
) {
5572 if (reg
->so
== so
) {
5574 printf("key_register: socket exists already.\n");
5576 msg0
->sadb_msg_errno
= EEXIST
;
5581 /* create regnode */
5582 KMALLOC(newreg
, struct secreg
*, sizeof(struct secreg
));
5583 if (newreg
== NULL
) {
5585 printf("key_register: No more memory.\n");
5587 msg0
->sadb_msg_errno
= ENOBUFS
;
5590 bzero((caddr_t
)newreg
, sizeof(struct secreg
));
5593 ((struct keycb
*)sotorawcb(so
))->kp_registered
++;
5595 /* add regnode to regtree. */
5596 LIST_INSERT_HEAD(®tree
[msg0
->sadb_msg_satype
], newreg
, chain
);
5600 struct sadb_msg
*newmsg
;
5601 struct sadb_supported
*sup
;
5602 u_int len
, alen
, elen
;
5605 /* create new sadb_msg to reply. */
5606 alen
= sizeof(struct sadb_supported
)
5607 + ((SADB_AALG_MAX
- 1) * sizeof(struct sadb_alg
));
5610 elen
= sizeof(struct sadb_supported
)
5611 + ((SADB_EALG_MAX
- 1) * sizeof(struct sadb_alg
));
5616 len
= sizeof(struct sadb_msg
)
5620 KMALLOC(newmsg
, struct sadb_msg
*, len
);
5621 if (newmsg
== NULL
) {
5623 printf("key_register: No more memory.\n");
5625 msg0
->sadb_msg_errno
= ENOBUFS
;
5628 bzero((caddr_t
)newmsg
, len
);
5630 bcopy((caddr_t
)mhp
[0], (caddr_t
)newmsg
, sizeof(*msg0
));
5631 newmsg
->sadb_msg_errno
= 0;
5632 newmsg
->sadb_msg_len
= PFKEY_UNIT64(len
);
5633 p
= (caddr_t
)newmsg
+ sizeof(*msg0
);
5635 /* for authentication algorithm */
5636 sup
= (struct sadb_supported
*)p
;
5637 sup
->sadb_supported_len
= PFKEY_UNIT64(alen
);
5638 sup
->sadb_supported_exttype
= SADB_EXT_SUPPORTED_AUTH
;
5643 struct sadb_alg
*alg
;
5644 struct ah_algorithm
*algo
;
5646 for (i
= 1; i
< SADB_AALG_MAX
; i
++) {
5647 algo
= &ah_algorithms
[i
];
5648 alg
= (struct sadb_alg
*)p
;
5649 alg
->sadb_alg_id
= i
;
5650 alg
->sadb_alg_ivlen
= 0;
5651 alg
->sadb_alg_minbits
= algo
->keymin
;
5652 alg
->sadb_alg_maxbits
= algo
->keymax
;
5653 p
+= sizeof(struct sadb_alg
);
5658 /* for encryption algorithm */
5659 sup
= (struct sadb_supported
*)p
;
5660 sup
->sadb_supported_len
= PFKEY_UNIT64(elen
);
5661 sup
->sadb_supported_exttype
= SADB_EXT_SUPPORTED_ENCRYPT
;
5666 struct sadb_alg
*alg
;
5667 struct esp_algorithm
*algo
;
5669 for (i
= 1; i
< SADB_EALG_MAX
; i
++) {
5670 algo
= &esp_algorithms
[i
];
5672 alg
= (struct sadb_alg
*)p
;
5673 alg
->sadb_alg_id
= i
;
5674 if (algo
&& algo
->ivlen
) {
5676 * give NULL to get the value preferred by algorithm
5677 * XXX SADB_X_EXT_DERIV ?
5679 alg
->sadb_alg_ivlen
= (*algo
->ivlen
)(NULL
);
5681 alg
->sadb_alg_ivlen
= 0;
5682 alg
->sadb_alg_minbits
= algo
->keymin
;
5683 alg
->sadb_alg_maxbits
= algo
->keymax
;
5684 p
+= sizeof(struct sadb_alg
);
5694 * free secreg entry registered.
5695 * XXX: I want to do free a socket marked done SADB_RESIGER to socket.
5706 panic("key_freereg: NULL pointer is passed.\n");
5709 * check whether existing or not.
5710 * check all type of SA, because there is a potential that
5711 * one socket is registered to multiple type of SA.
5713 for (i
= 0; i
<= SADB_SATYPE_MAX
; i
++) {
5714 LIST_FOREACH(reg
, ®tree
[i
], chain
) {
5716 && __LIST_CHAINED(reg
)) {
5717 LIST_REMOVE(reg
, chain
);
5728 * SADB_EXPIRE processing
5730 * <base, SA, lifetime(C and one of HS), address(SD)>
5732 * NOTE: We send only soft lifetime extension.
5735 * others : error number
5739 struct secasvar
*sav
;
5744 /* XXX: Why do we lock ? */
5746 s
= splsoftnet(); /*called from softclock()*/
5748 s
= splnet(); /*called from softclock()*/
5753 panic("key_expire: NULL pointer is passed.\n");
5754 if (sav
->sah
== NULL
)
5755 panic("key_expire: Why was SA index in SA NULL.\n");
5756 if ((satype
= key_proto2satype(sav
->sah
->saidx
.proto
)) == 0)
5757 panic("key_expire: invalid proto is passed.\n");
5760 struct sadb_msg
*newmsg
= NULL
;
5765 /* create new sadb_msg to reply. */
5766 len
= sizeof(struct sadb_msg
)
5767 + sizeof(struct sadb_sa
)
5768 + sizeof(struct sadb_lifetime
)
5769 + sizeof(struct sadb_lifetime
)
5770 + sizeof(struct sadb_address
)
5771 + PFKEY_ALIGN8(sav
->sah
->saidx
.src
.ss_len
)
5772 + sizeof(struct sadb_address
)
5773 + PFKEY_ALIGN8(sav
->sah
->saidx
.dst
.ss_len
);
5775 KMALLOC(newmsg
, struct sadb_msg
*, len
);
5776 if (newmsg
== NULL
) {
5778 printf("key_expire: No more memory.\n");
5783 bzero((caddr_t
)newmsg
, len
);
5785 /* set msg header */
5786 p
= key_setsadbmsg((caddr_t
)newmsg
, SADB_EXPIRE
, len
,
5787 satype
, sav
->seq
, 0,
5788 sav
->sah
->saidx
.mode
, sav
->sah
->saidx
.reqid
,
5791 /* create SA extension */
5792 p
= key_setsadbsa(p
, sav
);
5794 /* create lifetime extension */
5796 struct sadb_lifetime
*m_lt
= (struct sadb_lifetime
*)p
;
5798 m_lt
->sadb_lifetime_len
= PFKEY_UNIT64(sizeof(struct sadb_lifetime
));
5799 m_lt
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
5800 m_lt
->sadb_lifetime_allocations
= sav
->lft_c
->sadb_lifetime_allocations
;
5801 m_lt
->sadb_lifetime_bytes
= sav
->lft_c
->sadb_lifetime_bytes
;
5802 m_lt
->sadb_lifetime_addtime
= sav
->lft_c
->sadb_lifetime_addtime
;
5803 m_lt
->sadb_lifetime_usetime
= sav
->lft_c
->sadb_lifetime_usetime
;
5804 p
+= sizeof(struct sadb_lifetime
);
5806 /* copy SOFT lifetime extension. */
5807 bcopy(sav
->lft_s
, p
, sizeof(struct sadb_lifetime
));
5808 p
+= sizeof(struct sadb_lifetime
);
5811 /* set sadb_address for source */
5812 p
= key_setsadbaddr(p
,
5813 SADB_EXT_ADDRESS_SRC
,
5814 (struct sockaddr
*)&sav
->sah
->saidx
.src
,
5815 _INALENBYAF(sav
->sah
->saidx
.src
.ss_family
) << 3,
5818 /* set sadb_address for destination */
5819 p
= key_setsadbaddr(p
,
5820 SADB_EXT_ADDRESS_DST
,
5821 (struct sockaddr
*)&sav
->sah
->saidx
.dst
,
5822 _INALENBYAF(sav
->sah
->saidx
.dst
.ss_family
) << 3,
5825 error
= key_sendall(newmsg
, len
);
5832 * SADB_FLUSH processing
5835 * from the ikmpd, and free all entries in secastree.
5839 * NOTE: to do is only marking SADB_SASTATE_DEAD.
5841 * IN: mhp: pointer to the pointer to each header.
5842 * OUT: NULL if fail.
5843 * other if success, return pointer to the message to send.
5845 static struct sadb_msg
*
5849 struct sadb_msg
*msg0
;
5850 struct secashead
*sah
, *nextsah
;
5851 struct secasvar
*sav
, *nextsav
;
5857 if (mhp
== NULL
|| mhp
[0] == NULL
)
5858 panic("key_flush: NULL pointer is passed.\n");
5860 msg0
= (struct sadb_msg
*)mhp
[0];
5862 /* map satype to proto */
5863 if ((proto
= key_satype2proto(msg0
->sadb_msg_satype
)) == 0) {
5865 printf("key_flush: invalid satype is passed.\n");
5867 msg0
->sadb_msg_errno
= EINVAL
;
5871 /* no SATYPE specified, i.e. flushing all SA. */
5872 for (sah
= LIST_FIRST(&sahtree
);
5876 nextsah
= LIST_NEXT(sah
, chain
);
5878 if (msg0
->sadb_msg_satype
!= SADB_SATYPE_UNSPEC
5879 && proto
!= sah
->saidx
.proto
)
5883 stateidx
< _ARRAYLEN(saorder_state_alive
);
5886 state
= saorder_state_any
[stateidx
];
5887 for (sav
= LIST_FIRST(&sah
->savtree
[state
]);
5891 nextsav
= LIST_NEXT(sav
, chain
);
5893 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
5898 sah
->state
= SADB_SASTATE_DEAD
;
5902 struct sadb_msg
*newmsg
;
5905 /* create new sadb_msg to reply. */
5906 len
= sizeof(struct sadb_msg
);
5908 KMALLOC(newmsg
, struct sadb_msg
*, len
);
5909 if (newmsg
== NULL
) {
5911 printf("key_flush: No more memory.\n");
5913 msg0
->sadb_msg_errno
= ENOBUFS
;
5916 bzero((caddr_t
)newmsg
, len
);
5918 bcopy((caddr_t
)mhp
[0], (caddr_t
)newmsg
, sizeof(*msg0
));
5919 newmsg
->sadb_msg_errno
= 0;
5920 newmsg
->sadb_msg_len
= PFKEY_UNIT64(len
);
5927 * SADB_DUMP processing
5928 * dump all entries including status of DEAD in SAD.
5931 * from the ikmpd, and dump all secasvar leaves
5936 * IN: mhp: pointer to the pointer to each header.
5937 * OUT: error code. 0 on success.
5940 key_dump(mhp
, so
, target
)
5945 struct sadb_msg
*msg0
;
5946 struct secashead
*sah
;
5947 struct secasvar
*sav
;
5953 struct sadb_msg
*newmsg
;
5956 if (mhp
== NULL
|| mhp
[0] == NULL
)
5957 panic("key_dump: NULL pointer is passed.\n");
5959 msg0
= (struct sadb_msg
*)mhp
[0];
5961 /* map satype to proto */
5962 if ((proto
= key_satype2proto(msg0
->sadb_msg_satype
)) == 0) {
5964 printf("key_dump: invalid satype is passed.\n");
5966 msg0
->sadb_msg_errno
= EINVAL
;
5970 /* count sav entries to be sent to the userland. */
5972 LIST_FOREACH(sah
, &sahtree
, chain
) {
5974 if (msg0
->sadb_msg_satype
!= SADB_SATYPE_UNSPEC
5975 && proto
!= sah
->saidx
.proto
)
5979 stateidx
< _ARRAYLEN(saorder_state_any
);
5982 state
= saorder_state_any
[stateidx
];
5983 LIST_FOREACH(sav
, &sah
->savtree
[state
], chain
) {
5992 /* send this to the userland, one at a time. */
5994 LIST_FOREACH(sah
, &sahtree
, chain
) {
5996 if (msg0
->sadb_msg_satype
!= SADB_SATYPE_UNSPEC
5997 && proto
!= sah
->saidx
.proto
)
6000 /* map proto to satype */
6001 if ((satype
= key_proto2satype(sah
->saidx
.proto
)) == 0) {
6003 printf("key_dump: there was invalid proto in SAD.\n");
6005 msg0
->sadb_msg_errno
= EINVAL
;
6010 stateidx
< _ARRAYLEN(saorder_state_any
);
6013 state
= saorder_state_any
[stateidx
];
6014 LIST_FOREACH(sav
, &sah
->savtree
[state
], chain
) {
6016 len
= key_getmsglen(sav
);
6017 KMALLOC(newmsg
, struct sadb_msg
*, len
);
6018 if (newmsg
== NULL
) {
6020 printf("key_dump: No more memory.\n");
6024 bzero((caddr_t
)newmsg
, len
);
6027 (void)key_setdumpsa(newmsg
, sav
, SADB_DUMP
,
6028 satype
, cnt
, msg0
->sadb_msg_pid
);
6030 key_sendup(so
, newmsg
, len
, target
);
6041 * SADB_X_PROMISC processing
6044 key_promisc(mhp
, so
)
6048 struct sadb_msg
*msg0
;
6052 if (mhp
== NULL
|| mhp
[0] == NULL
)
6053 panic("key_promisc: NULL pointer is passed.\n");
6055 msg0
= (struct sadb_msg
*)mhp
[0];
6056 olen
= PFKEY_UNUNIT64(msg0
->sadb_msg_len
);
6058 if (olen
< sizeof(struct sadb_msg
)) {
6060 } else if (olen
== sizeof(struct sadb_msg
)) {
6061 /* enable/disable promisc mode */
6065 target
= KEY_SENDUP_ONE
;
6070 if ((kp
= (struct keycb
*)sotorawcb(so
)) == NULL
) {
6071 msg0
->sadb_msg_errno
= EINVAL
;
6074 msg0
->sadb_msg_errno
= 0;
6075 if (msg0
->sadb_msg_satype
== 1 || msg0
->sadb_msg_satype
== 0) {
6076 kp
->kp_promisc
= msg0
->sadb_msg_satype
;
6078 msg0
->sadb_msg_errno
= EINVAL
;
6082 /* send the original message back to everyone */
6083 msg0
->sadb_msg_errno
= 0;
6084 target
= KEY_SENDUP_ALL
;
6086 key_sendup(so
, msg0
, PFKEY_UNUNIT64(msg0
->sadb_msg_len
), target
);
6088 /* send packet as is */
6089 struct sadb_msg
*msg
;
6092 len
= olen
- sizeof(struct sadb_msg
);
6093 KMALLOC(msg
, struct sadb_msg
*, len
);
6095 msg0
->sadb_msg_errno
= ENOBUFS
;
6096 key_sendup(so
, msg0
, PFKEY_UNUNIT64(msg0
->sadb_msg_len
),
6097 KEY_SENDUP_ONE
); /*XXX*/
6100 /* XXX if sadb_msg_seq is specified, send to specific pid */
6101 key_sendup(so
, msg
, len
, KEY_SENDUP_ALL
);
6107 * send message to the socket.
6113 key_sendall(msg
, len
)
6114 struct sadb_msg
*msg
;
6122 panic("key_sendall: NULL pointer is passed.\n");
6124 /* search table registerd socket to send a message. */
6125 LIST_FOREACH(reg
, ®tree
[msg
->sadb_msg_satype
], chain
) {
6126 error
= key_sendup(reg
->so
, msg
, len
, KEY_SENDUP_ONE
);
6129 if (error
== ENOBUFS
)
6130 printf("key_sendall: No more memory.\n");
6132 printf("key_sendall: key_sendup returned %d\n",
6146 * parse sadb_msg buffer to process PFKEYv2,
6147 * and create a data to response if needed.
6148 * I think to be dealed with mbuf directly.
6150 * msgp : pointer to pointer to a received buffer pulluped.
6151 * This is rewrited to response.
6152 * so : pointer to socket.
6154 * length for buffer to send to user process.
6157 key_parse(msgp
, so
, targetp
)
6158 struct sadb_msg
**msgp
;
6162 struct sadb_msg
*msg
= *msgp
, *newmsg
= NULL
;
6163 caddr_t mhp
[SADB_EXT_MAX
+ 1];
6168 if (msg
== NULL
|| so
== NULL
)
6169 panic("key_parse: NULL pointer is passed.\n");
6171 KEYDEBUG(KEYDEBUG_KEY_DUMP
,
6172 printf("key_parse: passed sadb_msg\n");
6175 orglen
= PFKEY_UNUNIT64(msg
->sadb_msg_len
);
6178 *targetp
= KEY_SENDUP_ONE
;
6181 if (msg
->sadb_msg_version
!= PF_KEY_V2
) {
6183 printf("key_parse: PF_KEY version %u is mismatched.\n",
6184 msg
->sadb_msg_version
);
6186 pfkeystat
.out_invver
++;
6187 msg
->sadb_msg_errno
= EINVAL
;
6192 if (msg
->sadb_msg_type
> SADB_MAX
) {
6194 printf("key_parse: invalid type %u is passed.\n",
6195 msg
->sadb_msg_type
);
6197 msg
->sadb_msg_errno
= EINVAL
;
6198 pfkeystat
.out_invmsgtype
++;
6202 /* align message. */
6203 if (key_align(msg
, mhp
) != 0) {
6204 msg
->sadb_msg_errno
= EINVAL
;
6209 switch (msg
->sadb_msg_satype
) {
6210 case SADB_SATYPE_UNSPEC
:
6211 switch (msg
->sadb_msg_type
) {
6220 printf("key_parse: must specify satype "
6221 "when msg type=%u.\n",
6222 msg
->sadb_msg_type
);
6224 msg
->sadb_msg_errno
= EINVAL
;
6225 pfkeystat
.out_invsatype
++;
6229 case SADB_SATYPE_AH
:
6230 case SADB_SATYPE_ESP
:
6231 #if 1 /*nonstandard*/
6232 case SADB_X_SATYPE_IPCOMP
:
6234 switch (msg
->sadb_msg_type
) {
6236 case SADB_X_SPDDELETE
:
6238 case SADB_X_SPDDUMP
:
6239 case SADB_X_SPDFLUSH
:
6240 case SADB_X_SPDSETIDX
:
6241 case SADB_X_SPDUPDATE
:
6242 case SADB_X_SPDDELETE2
:
6244 printf("key_parse: illegal satype=%u\n",
6245 msg
->sadb_msg_type
);
6247 msg
->sadb_msg_errno
= EINVAL
;
6248 pfkeystat
.out_invsatype
++;
6252 case SADB_SATYPE_RSVP
:
6253 case SADB_SATYPE_OSPFV2
:
6254 case SADB_SATYPE_RIPV2
:
6255 case SADB_SATYPE_MIP
:
6257 printf("key_parse: type %u isn't supported.\n",
6258 msg
->sadb_msg_satype
);
6260 msg
->sadb_msg_errno
= EOPNOTSUPP
;
6261 pfkeystat
.out_invsatype
++;
6263 case 1: /* XXX: What does it do ? */
6264 if (msg
->sadb_msg_type
== SADB_X_PROMISC
)
6269 printf("key_parse: invalid type %u is passed.\n",
6270 msg
->sadb_msg_satype
);
6272 msg
->sadb_msg_errno
= EINVAL
;
6273 pfkeystat
.out_invsatype
++;
6277 /* check field of upper layer protocol and address family */
6278 if (mhp
[SADB_EXT_ADDRESS_SRC
] != NULL
6279 && mhp
[SADB_EXT_ADDRESS_DST
] != NULL
) {
6280 struct sadb_address
*src0
, *dst0
;
6283 src0
= (struct sadb_address
*)(mhp
[SADB_EXT_ADDRESS_SRC
]);
6284 dst0
= (struct sadb_address
*)(mhp
[SADB_EXT_ADDRESS_DST
]);
6286 /* check upper layer protocol */
6287 if (src0
->sadb_address_proto
!= dst0
->sadb_address_proto
) {
6289 printf("key_parse: upper layer protocol mismatched.\n");
6291 msg
->sadb_msg_errno
= EINVAL
;
6292 pfkeystat
.out_invaddr
++;
6297 if (PFKEY_ADDR_SADDR(src0
)->sa_family
6298 != PFKEY_ADDR_SADDR(dst0
)->sa_family
) {
6300 printf("key_parse: address family mismatched.\n");
6302 msg
->sadb_msg_errno
= EINVAL
;
6303 pfkeystat
.out_invaddr
++;
6307 prefix
= _INALENBYAF(PFKEY_ADDR_SADDR(src0
)->sa_family
) << 3;
6309 /* check max prefixlen */
6310 if (prefix
< src0
->sadb_address_prefixlen
6311 || prefix
< dst0
->sadb_address_prefixlen
) {
6313 printf("key_parse: illegal prefixlen.\n");
6315 msg
->sadb_msg_errno
= EINVAL
;
6316 pfkeystat
.out_invaddr
++;
6320 switch (PFKEY_ADDR_SADDR(src0
)->sa_family
) {
6326 printf("key_parse: invalid address family.\n");
6328 msg
->sadb_msg_errno
= EINVAL
;
6329 pfkeystat
.out_invaddr
++;
6334 * prefixlen == 0 is valid because there can be a case when
6335 * all addresses are matched.
6339 switch (msg
->sadb_msg_type
) {
6341 if ((newmsg
= key_getspi(mhp
)) == NULL
)
6344 *targetp
= KEY_SENDUP_ALL
;
6348 if ((newmsg
= key_update(mhp
)) == NULL
)
6351 *targetp
= KEY_SENDUP_ALL
;
6355 if ((newmsg
= key_add(mhp
)) == NULL
)
6358 *targetp
= KEY_SENDUP_ALL
;
6362 if ((newmsg
= key_delete(mhp
)) == NULL
)
6365 *targetp
= KEY_SENDUP_ALL
;
6369 if ((newmsg
= key_get(mhp
)) == NULL
)
6374 if ((newmsg
= key_acquire2(mhp
)) == NULL
)
6377 if (newmsg
== (struct sadb_msg
*)~0) {
6379 * It's not need to reply because of the message
6380 * that was reporting an error occured from the KMd.
6388 if ((newmsg
= key_register(mhp
, so
)) == NULL
)
6392 *targetp
= KEY_SENDUP_REGISTERED
;
6394 /* send result to all registered sockets */
6396 key_sendall(newmsg
, PFKEY_UNUNIT64(newmsg
->sadb_msg_len
));
6403 printf("key_parse: why is SADB_EXPIRE received ?\n");
6405 msg
->sadb_msg_errno
= EINVAL
;
6407 *targetp
= KEY_SENDUP_ALL
;
6408 pfkeystat
.out_invmsgtype
++;
6412 if ((newmsg
= key_flush(mhp
)) == NULL
)
6415 *targetp
= KEY_SENDUP_ALL
;
6419 /* key_dump will call key_sendup() on her own */
6420 error
= key_dump(mhp
, so
, KEY_SENDUP_ONE
);
6422 msg
->sadb_msg_errno
= error
;
6430 case SADB_X_PROMISC
:
6431 /* everything is handled in key_promisc() */
6432 key_promisc(mhp
, so
);
6434 return 0; /*nothing to reply*/
6436 case SADB_X_PCHANGE
:
6438 printf("key_parse: SADB_X_PCHANGE isn't supported.\n");
6440 msg
->sadb_msg_errno
= EINVAL
;
6441 pfkeystat
.out_invmsgtype
++;
6445 *targetp
= KEY_SENDUP_REGISTERED
;
6449 case SADB_X_SPDSETIDX
:
6450 case SADB_X_SPDUPDATE
:
6451 if ((newmsg
= key_spdadd(mhp
)) == NULL
)
6454 *targetp
= KEY_SENDUP_ALL
;
6457 case SADB_X_SPDDELETE
:
6458 if ((newmsg
= key_spddelete(mhp
)) == NULL
)
6461 *targetp
= KEY_SENDUP_ALL
;
6464 case SADB_X_SPDDELETE2
:
6465 if ((newmsg
= key_spddelete2(mhp
)) == NULL
)
6468 *targetp
= KEY_SENDUP_ALL
;
6472 /* key_spdget will call key_sendup() on her own */
6473 error
= key_spdget(mhp
, so
, KEY_SENDUP_ONE
);
6475 msg
->sadb_msg_errno
= error
;
6483 case SADB_X_SPDDUMP
:
6484 /* key_spddump will call key_sendup() on her own */
6485 error
= key_spddump(mhp
, so
, KEY_SENDUP_ONE
);
6487 msg
->sadb_msg_errno
= error
;
6495 case SADB_X_SPDFLUSH
:
6496 if ((newmsg
= key_spdflush(mhp
)) == NULL
)
6499 *targetp
= KEY_SENDUP_ALL
;
6503 msg
->sadb_msg_errno
= EOPNOTSUPP
;
6507 /* switch from old sadb_msg to new one if success. */
6511 return PFKEY_UNUNIT64((*msgp
)->sadb_msg_len
);
6515 * set the pointer to each header into message buffer.
6516 * IN: msg: pointer to message buffer.
6517 * mhp: pointer to the buffer allocated like below:
6518 * caddr_t mhp[SADB_EXT_MAX + 1];
6524 struct sadb_msg
*msg
;
6527 struct sadb_ext
*ext
;
6532 if (msg
== NULL
|| mhp
== NULL
)
6533 panic("key_align: NULL pointer is passed.\n");
6536 for (i
= 0; i
< SADB_EXT_MAX
+ 1; i
++)
6539 mhp
[0] = (caddr_t
)msg
;
6541 tlen
= PFKEY_UNUNIT64(msg
->sadb_msg_len
) - sizeof(struct sadb_msg
);
6542 ext
= (struct sadb_ext
*)((caddr_t
)msg
+ sizeof(struct sadb_msg
));
6546 switch (ext
->sadb_ext_type
) {
6548 case SADB_EXT_LIFETIME_CURRENT
:
6549 case SADB_EXT_LIFETIME_HARD
:
6550 case SADB_EXT_LIFETIME_SOFT
:
6551 case SADB_EXT_ADDRESS_SRC
:
6552 case SADB_EXT_ADDRESS_DST
:
6553 case SADB_EXT_ADDRESS_PROXY
:
6554 case SADB_EXT_KEY_AUTH
:
6555 case SADB_EXT_KEY_ENCRYPT
:
6556 case SADB_EXT_IDENTITY_SRC
:
6557 case SADB_EXT_IDENTITY_DST
:
6558 case SADB_EXT_SENSITIVITY
:
6559 case SADB_EXT_PROPOSAL
:
6560 case SADB_EXT_SUPPORTED_AUTH
:
6561 case SADB_EXT_SUPPORTED_ENCRYPT
:
6562 case SADB_EXT_SPIRANGE
:
6563 case SADB_X_EXT_POLICY
:
6564 /* duplicate check */
6566 * XXX Are there duplication payloads of either
6567 * KEY_AUTH or KEY_ENCRYPT ?
6569 if (mhp
[ext
->sadb_ext_type
] != NULL
) {
6571 printf("key_align: duplicate ext_type %u "
6573 ext
->sadb_ext_type
);
6575 pfkeystat
.out_dupext
++;
6578 mhp
[ext
->sadb_ext_type
] = (caddr_t
)ext
;
6582 printf("key_align: invalid ext_type %u is passed.\n",
6583 ext
->sadb_ext_type
);
6585 pfkeystat
.out_invexttype
++;
6589 extlen
= PFKEY_UNUNIT64(ext
->sadb_ext_len
);
6591 ext
= (struct sadb_ext
*)((caddr_t
)ext
+ extlen
);
6602 bzero((caddr_t
)&key_cb
, sizeof(key_cb
));
6604 for (i
= 0; i
< IPSEC_DIR_MAX
; i
++) {
6605 LIST_INIT(&sptree
[i
]);
6608 LIST_INIT(&sahtree
);
6610 for (i
= 0; i
<= SADB_SATYPE_MAX
; i
++) {
6611 LIST_INIT(®tree
[i
]);
6614 #ifndef IPSEC_NONBLOCK_ACQUIRE
6615 LIST_INIT(&acqtree
);
6617 LIST_INIT(&spacqtree
);
6619 /* system default */
6620 ip4_def_policy
.policy
= IPSEC_POLICY_NONE
;
6621 ip4_def_policy
.refcnt
++; /*never reclaim this*/
6623 ip6_def_policy
.policy
= IPSEC_POLICY_NONE
;
6624 ip6_def_policy
.refcnt
++; /*never reclaim this*/
6627 #ifndef IPSEC_DEBUG2
6628 timeout((void *)key_timehandler_funneled
, (void *)0, hz
);
6629 #endif /*IPSEC_DEBUG2*/
6631 /* initialize key statistics */
6632 keystat
.getspi_count
= 1;
6634 printf("IPsec: Initialized Security Association Processing.\n");
6640 * XXX: maybe This function is called after INBOUND IPsec processing.
6642 * Special check for tunnel-mode packets.
6643 * We must make some checks for consistency between inner and outer IP header.
6645 * xxx more checks to be provided
6648 key_checktunnelsanity(sav
, family
, src
, dst
)
6649 struct secasvar
*sav
;
6655 if (sav
->sah
== NULL
)
6656 panic("sav->sah == NULL at key_checktunnelsanity");
6658 /* XXX: check inner IP header */
6665 #define hostnamelen strlen(hostname)
6669 * Get FQDN for the host.
6670 * If the administrator configured hostname (by hostname(1)) without
6671 * domain name, returns nothing.
6678 static char fqdn
[MAXHOSTNAMELEN
+ 1];
6683 /* check if it comes with domain name. */
6685 for (i
= 0; i
< hostnamelen
; i
++) {
6686 if (hostname
[i
] == '.')
6692 /* NOTE: hostname may not be NUL-terminated. */
6693 bzero(fqdn
, sizeof(fqdn
));
6694 bcopy(hostname
, fqdn
, hostnamelen
);
6695 fqdn
[hostnamelen
] = '\0';
6700 * get username@FQDN for the host/user.
6706 static char userfqdn
[MAXHOSTNAMELEN
+ MAXLOGNAME
+ 2];
6707 struct proc
*p
= curproc
;
6710 if (!p
|| !p
->p_pgrp
|| !p
->p_pgrp
->pg_session
)
6712 if (!(host
= key_getfqdn()))
6715 /* NOTE: s_login may not be-NUL terminated. */
6716 bzero(userfqdn
, sizeof(userfqdn
));
6717 bcopy(p
->p_pgrp
->pg_session
->s_login
, userfqdn
, MAXLOGNAME
);
6718 userfqdn
[MAXLOGNAME
] = '\0'; /* safeguard */
6719 q
= userfqdn
+ strlen(userfqdn
);
6721 bcopy(host
, q
, strlen(host
));
6729 /* record data transfer on SA, and update timestamps */
6731 key_sa_recordxfer(sav
, m
)
6732 struct secasvar
*sav
;
6736 panic("key_sa_recordxfer called with sav == NULL");
6738 panic("key_sa_recordxfer called with m == NULL");
6743 * XXX Currently, there is a difference of bytes size
6744 * between inbound and outbound processing.
6746 sav
->lft_c
->sadb_lifetime_bytes
+= m
->m_pkthdr
.len
;
6747 /* to check bytes lifetime is done in key_timehandler(). */
6750 * We use the number of packets as the unit of
6751 * sadb_lifetime_allocations. We increment the variable
6752 * whenever {esp,ah}_{in,out}put is called.
6754 sav
->lft_c
->sadb_lifetime_allocations
++;
6755 /* XXX check for expires? */
6758 * NOTE: We record CURRENT sadb_lifetime_usetime by using wall clock,
6759 * in seconds. HARD and SOFT lifetime are measured by the time
6760 * difference (again in seconds) from sadb_lifetime_usetime.
6764 * -----+-----+--------+---> t
6765 * <--------------> HARD
6771 sav
->lft_c
->sadb_lifetime_usetime
= tv
.tv_sec
;
6772 /* XXX check for expires? */
6780 key_sa_routechange(dst
)
6781 struct sockaddr
*dst
;
6783 struct secashead
*sah
;
6786 LIST_FOREACH(sah
, &sahtree
, chain
) {
6787 ro
= &sah
->sa_route
;
6788 if (ro
->ro_rt
&& dst
->sa_len
== ro
->ro_dst
.sa_len
6789 && bcmp(dst
, &ro
->ro_dst
, dst
->sa_len
) == 0) {
6791 ro
->ro_rt
= (struct rtentry
*)NULL
;
6799 key_sa_chgstate(sav
, state
)
6800 struct secasvar
*sav
;
6804 panic("key_sa_chgstate called with sav == NULL");
6806 if (sav
->state
== state
)
6809 if (__LIST_CHAINED(sav
))
6810 LIST_REMOVE(sav
, chain
);
6813 LIST_INSERT_HEAD(&sav
->sah
->savtree
[state
], sav
, chain
);
6816 /* returns NULL on error, m0 will be left unchanged */
6818 key_appendmbuf(m0
, len
)
6826 if (!m0
|| (m0
->m_flags
& M_PKTHDR
) == 0)
6827 return NULL
; /*EINVAL*/
6829 return NULL
; /*EINVAL*/
6831 for (m
= m0
; m
&& m
->m_next
; m
= m
->m_next
)
6833 if (len
<= M_TRAILINGSPACE(m
)) {
6834 p
= mtod(m
, caddr_t
) + m
->m_len
;
6836 m0
->m_pkthdr
.len
+= len
;
6840 MGET(n
, M_DONTWAIT
, m
->m_type
);
6842 MCLGET(n
, M_DONTWAIT
);
6843 if ((n
->m_flags
& M_EXT
) == 0) {
6849 return NULL
; /*ENOBUFS*/
6853 m0
->m_pkthdr
.len
+= len
;
6855 return mtod(n
, caddr_t
);
6859 #include <sys/user.h>
6860 #include <sys/sysctl.h>
6862 int *key_sysvars
[] = KEYCTL_VARS
;
6865 key_sysctl(name
, namelen
, oldp
, oldlenp
, newp
, newlen
)
6873 if (name
[0] >= KEYCTL_MAXID
)
6877 return sysctl_int_arr(key_sysvars
, name
, namelen
,
6878 oldp
, oldlenp
, newp
, newlen
);
6885 #include <sys/sysctl.h>
6887 static int *key_sysvars
[] = KEYCTL_VARS
;
6890 key_sysctl(name
, namelen
, oldp
, oldlenp
, newp
, newlen
)
6898 if (name
[0] >= KEYCTL_MAXID
)
6900 if (!key_sysvars
[name
[0]])
6904 return sysctl_int(oldp
, oldlenp
, newp
, newlen
,
6905 key_sysvars
[name
[0]]);
6908 #endif /*__NetBSD__*/