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(void)
3800 thread_funnel_set(network_flock
, TRUE
);
3802 s
= splsoftnet(); /*called from softclock()*/
3804 s
= splnet(); /*called from softclock()*/
3809 struct secpolicy
*sp
, *nextsp
;
3811 for (dir
= 0; dir
< IPSEC_DIR_MAX
; dir
++) {
3812 for (sp
= LIST_FIRST(&sptree
[dir
]);
3816 nextsp
= LIST_NEXT(sp
, chain
);
3818 if (sp
->state
== IPSEC_SPSTATE_DEAD
)
3826 struct secashead
*sah
, *nextsah
;
3827 struct secasvar
*sav
, *nextsav
;
3829 for (sah
= LIST_FIRST(&sahtree
);
3833 nextsah
= LIST_NEXT(sah
, chain
);
3835 /* if sah has been dead, then delete it and process next sah. */
3836 if (sah
->state
== SADB_SASTATE_DEAD
) {
3841 /* if LARVAL entry doesn't become MATURE, delete it. */
3842 for (sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_LARVAL
]);
3846 nextsav
= LIST_NEXT(sav
, chain
);
3850 if (key_larval_lifetime
< sav
->tick
) {
3856 * check MATURE entry to start to send expire message
3859 for (sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_MATURE
]);
3863 nextsav
= LIST_NEXT(sav
, chain
);
3867 /* we don't need to check. */
3868 if (sav
->lft_s
== NULL
)
3872 if (sav
->lft_c
== NULL
) {
3874 printf("key_timehandler: "
3875 "There is no CURRENT time, why?\n");
3880 /* compare SOFT lifetime and tick */
3881 if (sav
->lft_s
->sadb_lifetime_addtime
!= 0
3882 && sav
->lft_s
->sadb_lifetime_addtime
< sav
->tick
) {
3884 * check SA to be used whether or not.
3885 * when SA hasn't been used, delete it.
3887 if (sav
->lft_c
->sadb_lifetime_usetime
== 0) {
3888 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
3892 key_sa_chgstate(sav
, SADB_SASTATE_DYING
);
3894 * XXX If we keep to send expire
3895 * message in the status of
3896 * DYING. Do remove below code.
3901 /* check SOFT lifetime by bytes */
3903 * XXX I don't know the way to delete this SA
3904 * when new SA is installed. Caution when it's
3905 * installed too big lifetime by time.
3907 else if (sav
->lft_s
->sadb_lifetime_bytes
!= 0
3908 && sav
->lft_s
->sadb_lifetime_bytes
< sav
->lft_c
->sadb_lifetime_bytes
) {
3910 key_sa_chgstate(sav
, SADB_SASTATE_DYING
);
3912 * XXX If we keep to send expire
3913 * message in the status of
3914 * DYING. Do remove below code.
3920 /* check DYING entry to change status to DEAD. */
3921 for (sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_DYING
]);
3925 nextsav
= LIST_NEXT(sav
, chain
);
3929 /* we don't need to check. */
3930 if (sav
->lft_h
== NULL
)
3934 if (sav
->lft_c
== NULL
) {
3936 printf("key_timehandler: "
3937 "There is no CURRENT time, why?\n");
3942 /* compare HARD lifetime and tick */
3943 if (sav
->lft_h
->sadb_lifetime_addtime
!= 0
3944 && sav
->lft_h
->sadb_lifetime_addtime
< sav
->tick
) {
3945 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
3949 #if 0 /* XXX Should we keep to send expire message until HARD lifetime ? */
3950 else if (sav
->lft_s
!= NULL
3951 && sav
->lft_s
->sadb_lifetime_addtime
!= 0
3952 && sav
->lft_s
->sadb_lifetime_addtime
< sav
->tick
) {
3954 * XXX: should be checked to be
3955 * installed the valid SA.
3959 * If there is no SA then sending
3965 /* check HARD lifetime by bytes */
3966 else if (sav
->lft_h
->sadb_lifetime_bytes
!= 0
3967 && sav
->lft_h
->sadb_lifetime_bytes
< sav
->lft_c
->sadb_lifetime_bytes
) {
3968 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
3974 /* delete entry in DEAD */
3975 for (sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_DEAD
]);
3979 nextsav
= LIST_NEXT(sav
, chain
);
3982 if (sav
->state
!= SADB_SASTATE_DEAD
) {
3984 printf("key_timehandler: "
3985 "invalid sav->state "
3986 "(queue: %d SA: %d): "
3988 SADB_SASTATE_DEAD
, sav
->state
);
3993 * do not call key_freesav() here.
3994 * sav should already be freed, and sav->refcnt
3995 * shows other references to sav
3996 * (such as from SPD).
4002 #ifndef IPSEC_NONBLOCK_ACQUIRE
4005 struct secacq
*acq
, *nextacq
;
4007 for (acq
= LIST_FIRST(&acqtree
);
4011 nextacq
= LIST_NEXT(acq
, chain
);
4015 if (key_blockacq_lifetime
< acq
->tick
&& __LIST_CHAINED(acq
)) {
4016 LIST_REMOVE(acq
, chain
);
4025 struct secspacq
*acq
, *nextacq
;
4027 for (acq
= LIST_FIRST(&spacqtree
);
4031 nextacq
= LIST_NEXT(acq
, chain
);
4035 if (key_blockacq_lifetime
< acq
->tick
&& __LIST_CHAINED(acq
)) {
4036 LIST_REMOVE(acq
, chain
);
4042 /* initialize random seed */
4043 if (key_tick_init_random
++ > key_int_random
) {
4044 key_tick_init_random
= 0;
4048 #ifndef IPSEC_DEBUG2
4049 /* do exchange to tick time !! */
4050 (void)timeout((void *)key_timehandler
, (void *)0, 100);
4051 #endif /* IPSEC_DEBUG2 */
4054 thread_funnel_set(network_funnel
, FALSE
);
4059 * to initialize a seed for random()
4066 extern long randseed
; /* it's defined at i386/i386/random.s */
4067 #endif /* __bsdi__ */
4071 #if defined(__FreeBSD__)
4072 srandom(tv
.tv_usec
);
4073 #endif /* __FreeBSD__ */
4074 #if defined(__APPLE__)
4078 randseed
= tv
.tv_usec
;
4079 #endif /* __bsdi__ */
4085 * map SADB_SATYPE_* to IPPROTO_*.
4086 * if satype == SADB_SATYPE then satype is mapped to ~0.
4088 * 0: invalid satype.
4091 key_satype2proto(satype
)
4095 case SADB_SATYPE_UNSPEC
:
4096 return IPSEC_PROTO_ANY
;
4097 case SADB_SATYPE_AH
:
4099 case SADB_SATYPE_ESP
:
4101 #if 1 /*nonstandard*/
4102 case SADB_X_SATYPE_IPCOMP
:
4103 return IPPROTO_IPCOMP
;
4113 * map IPPROTO_* to SADB_SATYPE_*
4115 * 0: invalid protocol type.
4118 key_proto2satype(proto
)
4123 return SADB_SATYPE_AH
;
4125 return SADB_SATYPE_ESP
;
4126 #if 1 /*nonstandard*/
4127 case IPPROTO_IPCOMP
:
4128 return SADB_X_SATYPE_IPCOMP
;
4139 * SADB_GETSPI processing is to receive
4140 * <base, src address, dst address, (SPI range)>
4141 * from the IKMPd, to assign a unique spi value, to hang on the INBOUND
4142 * tree with the status of LARVAL, and send
4143 * <base, SA(*), address(SD)>
4146 * IN: mhp: pointer to the pointer to each header.
4147 * OUT: NULL if fail.
4148 * other if success, return pointer to the message to send.
4150 static struct sadb_msg
*
4154 struct sadb_msg
*msg0
;
4155 struct sadb_address
*src0
, *dst0
;
4156 struct secasindex saidx
;
4157 struct secashead
*newsah
;
4158 struct secasvar
*newsav
;
4163 if (mhp
== NULL
|| mhp
[0] == NULL
)
4164 panic("key_getspi: NULL pointer is passed.\n");
4166 msg0
= (struct sadb_msg
*)mhp
[0];
4168 if (mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
4169 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
) {
4171 printf("key_getspi: invalid message is passed.\n");
4173 msg0
->sadb_msg_errno
= EINVAL
;
4177 src0
= (struct sadb_address
*)(mhp
[SADB_EXT_ADDRESS_SRC
]);
4178 dst0
= (struct sadb_address
*)(mhp
[SADB_EXT_ADDRESS_DST
]);
4180 /* map satype to proto */
4181 if ((proto
= key_satype2proto(msg0
->sadb_msg_satype
)) == 0) {
4183 printf("key_getspi: invalid satype is passed.\n");
4185 msg0
->sadb_msg_errno
= EINVAL
;
4189 KEY_SETSECASIDX(proto
, msg0
, src0
+1, dst0
+1, &saidx
);
4191 /* SPI allocation */
4192 spi
= key_do_getnewspi((struct sadb_spirange
*)mhp
[SADB_EXT_SPIRANGE
],
4195 msg0
->sadb_msg_errno
= EEXIST
;
4199 /* get a SA index */
4200 if ((newsah
= key_getsah(&saidx
)) == NULL
) {
4202 /* create a new SA index */
4203 if ((newsah
= key_newsah(&saidx
)) == NULL
) {
4205 printf("key_getspi: No more memory.\n");
4207 msg0
->sadb_msg_errno
= ENOBUFS
;
4213 if ((newsav
= key_newsav(mhp
, newsah
)) == NULL
) {
4214 msg0
->sadb_msg_errno
= ENOBUFS
;
4215 /* XXX don't free new SA index allocated in above. */
4220 newsav
->spi
= htonl(spi
);
4222 #ifndef IPSEC_NONBLOCK_ACQUIRE
4223 /* delete the entry in acqtree */
4224 if (msg0
->sadb_msg_seq
!= 0) {
4226 if ((acq
= key_getacqbyseq(msg0
->sadb_msg_seq
)) != NULL
) {
4227 /* reset counter in order to deletion by timehander. */
4228 acq
->tick
= key_blockacq_lifetime
;
4235 struct sadb_msg
*newmsg
;
4239 /* create new sadb_msg to reply. */
4240 len
= sizeof(struct sadb_msg
)
4241 + sizeof(struct sadb_sa
)
4242 + PFKEY_EXTLEN(mhp
[SADB_EXT_ADDRESS_SRC
])
4243 + PFKEY_EXTLEN(mhp
[SADB_EXT_ADDRESS_DST
]);
4245 KMALLOC(newmsg
, struct sadb_msg
*, len
);
4246 if (newmsg
== NULL
) {
4248 printf("key_getspi: No more memory.\n");
4250 msg0
->sadb_msg_errno
= ENOBUFS
;
4253 bzero((caddr_t
)newmsg
, len
);
4255 bcopy((caddr_t
)mhp
[0], (caddr_t
)newmsg
, sizeof(*msg0
));
4256 newmsg
->sadb_msg_seq
= newsav
->seq
;
4257 newmsg
->sadb_msg_errno
= 0;
4258 newmsg
->sadb_msg_len
= PFKEY_UNIT64(len
);
4259 p
= (caddr_t
)newmsg
+ sizeof(*msg0
);
4262 struct sadb_sa
*m_sa
;
4263 m_sa
= (struct sadb_sa
*)p
;
4264 m_sa
->sadb_sa_len
= PFKEY_UNIT64(sizeof(struct sadb_sa
));
4265 m_sa
->sadb_sa_exttype
= SADB_EXT_SA
;
4266 m_sa
->sadb_sa_spi
= htonl(spi
);
4267 p
+= sizeof(struct sadb_sa
);
4270 p
= key_setsadbext(p
, mhp
[SADB_EXT_ADDRESS_SRC
]);
4271 p
= key_setsadbext(p
, mhp
[SADB_EXT_ADDRESS_DST
]);
4278 * allocating new SPI
4279 * called by key_getspi().
4285 key_do_getnewspi(spirange
, saidx
)
4286 struct sadb_spirange
*spirange
;
4287 struct secasindex
*saidx
;
4291 int count
= key_spi_trycnt
;
4293 /* set spi range to allocate */
4294 if (spirange
!= NULL
) {
4295 min
= spirange
->sadb_spirange_min
;
4296 max
= spirange
->sadb_spirange_max
;
4298 min
= key_spi_minval
;
4299 max
= key_spi_maxval
;
4301 /* IPCOMP needs 2-byte SPI */
4302 if (saidx
->proto
== IPPROTO_IPCOMP
) {
4309 t
= min
; min
= max
; max
= t
;
4314 if (key_checkspidup(saidx
, min
) != NULL
) {
4316 printf("key_do_getnewspi: SPI %u exists already.\n", min
);
4321 count
--; /* taking one cost. */
4329 /* when requesting to allocate spi ranged */
4331 /* generate pseudo-random SPI value ranged. */
4332 newspi
= min
+ (random() % ( max
- min
+ 1 ));
4334 if (key_checkspidup(saidx
, newspi
) == NULL
)
4338 if (count
== 0 || newspi
== 0) {
4340 printf("key_do_getnewspi: to allocate spi is failed.\n");
4347 keystat
.getspi_count
=
4348 (keystat
.getspi_count
+ key_spi_trycnt
- count
) / 2;
4354 * SADB_UPDATE processing
4356 * <base, SA, (lifetime(HSC),) address(SD), (address(P),)
4357 * key(AE), (identity(SD),) (sensitivity)>
4358 * from the ikmpd, and update a secasvar entry whose status is SADB_SASTATE_LARVAL.
4360 * <base, SA, (lifetime(HSC),) address(SD), (address(P),)
4361 * (identity(SD),) (sensitivity)>
4364 * IN: mhp: pointer to the pointer to each header.
4365 * OUT: NULL if fail.
4366 * other if success, return pointer to the message to send.
4368 static struct sadb_msg
*
4372 struct sadb_msg
*msg0
;
4373 struct sadb_sa
*sa0
;
4374 struct sadb_address
*src0
, *dst0
;
4375 struct secasindex saidx
;
4376 struct secashead
*sah
;
4377 struct secasvar
*sav
;
4381 if (mhp
== NULL
|| mhp
[0] == NULL
)
4382 panic("key_update: NULL pointer is passed.\n");
4384 msg0
= (struct sadb_msg
*)mhp
[0];
4386 /* map satype to proto */
4387 if ((proto
= key_satype2proto(msg0
->sadb_msg_satype
)) == 0) {
4389 printf("key_update: invalid satype is passed.\n");
4391 msg0
->sadb_msg_errno
= EINVAL
;
4395 if (mhp
[SADB_EXT_SA
] == NULL
4396 || mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
4397 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
4398 || (msg0
->sadb_msg_satype
== SADB_SATYPE_ESP
4399 && mhp
[SADB_EXT_KEY_ENCRYPT
] == NULL
)
4400 || (msg0
->sadb_msg_satype
== SADB_SATYPE_AH
4401 && mhp
[SADB_EXT_KEY_AUTH
] == NULL
)
4402 || (mhp
[SADB_EXT_LIFETIME_HARD
] != NULL
4403 && mhp
[SADB_EXT_LIFETIME_SOFT
] == NULL
)
4404 || (mhp
[SADB_EXT_LIFETIME_HARD
] == NULL
4405 && mhp
[SADB_EXT_LIFETIME_SOFT
] != NULL
)) {
4407 printf("key_update: invalid message is passed.\n");
4409 msg0
->sadb_msg_errno
= EINVAL
;
4413 sa0
= (struct sadb_sa
*)mhp
[SADB_EXT_SA
];
4414 src0
= (struct sadb_address
*)(mhp
[SADB_EXT_ADDRESS_SRC
]);
4415 dst0
= (struct sadb_address
*)(mhp
[SADB_EXT_ADDRESS_DST
]);
4417 KEY_SETSECASIDX(proto
, msg0
, src0
+1, dst0
+1, &saidx
);
4419 /* get a SA header */
4420 if ((sah
= key_getsah(&saidx
)) == NULL
) {
4422 printf("key_update: no SA index found.\n");
4424 msg0
->sadb_msg_errno
= ENOENT
;
4428 /* set spidx if there */
4429 if (key_setident(sah
, mhp
) < 0)
4432 /* find a SA with sequence number. */
4433 #if IPSEC_DOSEQCHECK
4434 if (msg0
->sadb_msg_seq
!= 0
4435 && (sav
= key_getsavbyseq(sah
, msg0
->sadb_msg_seq
)) == NULL
) {
4437 printf("key_update: no larval SA with sequence %u exists.\n",
4438 msg0
->sadb_msg_seq
);
4440 msg0
->sadb_msg_errno
= ENOENT
;
4444 if ((sav
= key_getsavbyspi(sah
, sa0
->sadb_sa_spi
)) == NULL
) {
4446 printf("key_update: no such a SA found (spi:%u)\n",
4447 (u_int32_t
)ntohl(sa0
->sadb_sa_spi
));
4449 msg0
->sadb_msg_errno
= EINVAL
;
4454 /* validity check */
4455 if (sav
->sah
->saidx
.proto
!= proto
) {
4457 printf("key_update: protocol mismatched (DB=%u param=%u)\n",
4458 sav
->sah
->saidx
.proto
, proto
);
4460 msg0
->sadb_msg_errno
= EINVAL
;
4463 #if IPSEC_DOSEQCHECK
4464 if (sav
->spi
!= sa0
->sadb_sa_spi
) {
4466 printf("key_update: SPI mismatched (DB:%u param:%u)\n",
4467 (u_int32_t
)ntohl(sav
->spi
),
4468 (u_int32_t
)ntohl(sa0
->sadb_sa_spi
));
4470 msg0
->sadb_msg_errno
= EINVAL
;
4474 if (sav
->pid
!= msg0
->sadb_msg_pid
) {
4476 printf("key_update: pid mismatched (DB:%u param:%u)\n",
4477 sav
->pid
, msg0
->sadb_msg_pid
);
4479 msg0
->sadb_msg_errno
= EINVAL
;
4483 /* copy sav values */
4484 if (key_setsaval(sav
, mhp
)) {
4489 /* check SA values to be mature. */
4490 if ((msg0
->sadb_msg_errno
= key_mature(sav
)) != 0) {
4496 struct sadb_msg
*newmsg
;
4498 /* set msg buf from mhp */
4499 if ((newmsg
= key_getmsgbuf_x1(mhp
)) == NULL
) {
4501 printf("key_update: No more memory.\n");
4503 msg0
->sadb_msg_errno
= ENOBUFS
;
4511 * search SAD with sequence for a SA which state is SADB_SASTATE_LARVAL.
4512 * only called by key_update().
4515 * others : found, pointer to a SA.
4517 #if IPSEC_DOSEQCHECK
4518 static struct secasvar
*
4519 key_getsavbyseq(sah
, seq
)
4520 struct secashead
*sah
;
4523 struct secasvar
*sav
;
4526 state
= SADB_SASTATE_LARVAL
;
4528 /* search SAD with sequence number ? */
4529 LIST_FOREACH(sav
, &sah
->savtree
[state
], chain
) {
4531 KEY_CHKSASTATE(state
, sav
->state
, "key_getsabyseq");
4533 if (sav
->seq
== seq
) {
4535 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
4536 printf("DP key_getsavbyseq cause "
4537 "refcnt++:%d SA:%p\n",
4548 * SADB_ADD processing
4549 * add a entry to SA database, when received
4550 * <base, SA, (lifetime(HSC),) address(SD), (address(P),)
4551 * key(AE), (identity(SD),) (sensitivity)>
4554 * <base, SA, (lifetime(HSC),) address(SD), (address(P),)
4555 * (identity(SD),) (sensitivity)>
4558 * IGNORE identity and sensitivity messages.
4560 * IN: mhp: pointer to the pointer to each header.
4561 * OUT: NULL if fail.
4562 * other if success, return pointer to the message to send.
4564 static struct sadb_msg
*
4568 struct sadb_msg
*msg0
;
4569 struct sadb_sa
*sa0
;
4570 struct sadb_address
*src0
, *dst0
;
4571 struct secasindex saidx
;
4572 struct secashead
*newsah
;
4573 struct secasvar
*newsav
;
4577 if (mhp
== NULL
|| mhp
[0] == NULL
)
4578 panic("key_add: NULL pointer is passed.\n");
4580 msg0
= (struct sadb_msg
*)mhp
[0];
4582 /* map satype to proto */
4583 if ((proto
= key_satype2proto(msg0
->sadb_msg_satype
)) == 0) {
4585 printf("key_add: invalid satype is passed.\n");
4587 msg0
->sadb_msg_errno
= EINVAL
;
4591 if (mhp
[SADB_EXT_SA
] == NULL
4592 || mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
4593 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
4594 || (msg0
->sadb_msg_satype
== SADB_SATYPE_ESP
4595 && mhp
[SADB_EXT_KEY_ENCRYPT
] == NULL
)
4596 || (msg0
->sadb_msg_satype
== SADB_SATYPE_AH
4597 && mhp
[SADB_EXT_KEY_AUTH
] == NULL
)
4598 || (mhp
[SADB_EXT_LIFETIME_HARD
] != NULL
4599 && mhp
[SADB_EXT_LIFETIME_SOFT
] == NULL
)
4600 || (mhp
[SADB_EXT_LIFETIME_HARD
] == NULL
4601 && mhp
[SADB_EXT_LIFETIME_SOFT
] != NULL
)) {
4603 printf("key_add: invalid message is passed.\n");
4605 msg0
->sadb_msg_errno
= EINVAL
;
4609 sa0
= (struct sadb_sa
*)mhp
[SADB_EXT_SA
];
4610 src0
= (struct sadb_address
*)(mhp
[SADB_EXT_ADDRESS_SRC
]);
4611 dst0
= (struct sadb_address
*)(mhp
[SADB_EXT_ADDRESS_DST
]);
4613 KEY_SETSECASIDX(proto
, msg0
, src0
+1, dst0
+1, &saidx
);
4615 /* get a SA header */
4616 if ((newsah
= key_getsah(&saidx
)) == NULL
) {
4618 /* create a new SA header */
4619 if ((newsah
= key_newsah(&saidx
)) == NULL
) {
4621 printf("key_add: No more memory.\n");
4623 msg0
->sadb_msg_errno
= ENOBUFS
;
4628 /* set spidx if there */
4629 if (key_setident(newsah
, mhp
) < 0)
4632 /* create new SA entry. */
4633 /* We can create new SA only if SPI is differenct. */
4634 if (key_getsavbyspi(newsah
, sa0
->sadb_sa_spi
)) {
4636 printf("key_add: SA already exists.\n");
4638 msg0
->sadb_msg_errno
= EEXIST
;
4641 if ((newsav
= key_newsav(mhp
, newsah
)) == NULL
)
4644 /* check SA values to be mature. */
4645 if ((msg0
->sadb_msg_errno
= key_mature(newsav
)) != NULL
) {
4646 key_freesav(newsav
);
4651 * don't call key_freesav() here, as we would like to keep the SA
4652 * in the database on success.
4656 struct sadb_msg
*newmsg
;
4658 /* set msg buf from mhp */
4659 if ((newmsg
= key_getmsgbuf_x1(mhp
)) == NULL
) {
4661 printf("key_add: No more memory.\n");
4663 msg0
->sadb_msg_errno
= ENOBUFS
;
4672 key_setident(sah
, mhp
)
4673 struct secashead
*sah
;
4676 struct sadb_msg
*msg0
;
4677 struct sadb_ident
*idsrc
, *iddst
;
4678 int idsrclen
, iddstlen
;
4681 if (sah
== NULL
|| mhp
== NULL
|| mhp
[0] == NULL
)
4682 panic("key_setident: NULL pointer is passed.\n");
4684 msg0
= (struct sadb_msg
*)mhp
[0];
4686 /* don't make buffer if not there */
4687 if (mhp
[SADB_EXT_IDENTITY_SRC
] == NULL
4688 && mhp
[SADB_EXT_IDENTITY_DST
] == NULL
) {
4694 if (mhp
[SADB_EXT_IDENTITY_SRC
] == NULL
4695 || mhp
[SADB_EXT_IDENTITY_DST
] == NULL
) {
4697 printf("key_setident: invalid identity.\n");
4699 msg0
->sadb_msg_errno
= EINVAL
;
4703 idsrc
= (struct sadb_ident
*)mhp
[SADB_EXT_IDENTITY_SRC
];
4704 iddst
= (struct sadb_ident
*)mhp
[SADB_EXT_IDENTITY_DST
];
4705 idsrclen
= PFKEY_UNUNIT64(idsrc
->sadb_ident_len
);
4706 iddstlen
= PFKEY_UNUNIT64(idsrc
->sadb_ident_len
);
4708 /* validity check */
4709 if (idsrc
->sadb_ident_type
!= iddst
->sadb_ident_type
) {
4711 printf("key_setident: ident type mismatch.\n");
4713 msg0
->sadb_msg_errno
= EINVAL
;
4717 switch (idsrc
->sadb_ident_type
) {
4718 case SADB_X_IDENTTYPE_ADDR
:
4720 sizeof(*idsrc
) + ((struct sockaddr
*)(idsrc
+ 1))->sa_len
4722 sizeof(*iddst
) + ((struct sockaddr
*)(iddst
+ 1))->sa_len
) {
4724 printf("key_setident: invalid length is passed.\n");
4726 msg0
->sadb_msg_errno
= EINVAL
;
4729 if (((struct sockaddr
*)(idsrc
+ 1))->sa_len
>
4730 sizeof(struct sockaddr_storage
)
4731 || ((struct sockaddr
*)(iddst
+ 1))->sa_len
>
4732 sizeof(struct sockaddr_storage
)) {
4734 printf("key_setident: invalid sa_len is passed.\n");
4736 msg0
->sadb_msg_errno
= EINVAL
;
4739 #define __IDENTXID(a) ((union sadb_x_ident_id *)&(a)->sadb_ident_id)
4740 if (__IDENTXID(idsrc
)->sadb_x_ident_id_addr
.ul_proto
4741 != __IDENTXID(iddst
)->sadb_x_ident_id_addr
.ul_proto
) {
4743 printf("key_setident: ul_proto mismatch.\n");
4745 msg0
->sadb_msg_errno
= EINVAL
;
4748 #undef __IDENTXID(a)
4750 case SADB_IDENTTYPE_PREFIX
:
4751 case SADB_IDENTTYPE_FQDN
:
4752 case SADB_IDENTTYPE_USERFQDN
:
4754 /* XXX do nothing */
4760 /* make structure */
4761 KMALLOC(sah
->idents
, struct sadb_ident
*, idsrclen
);
4762 if (sah
->idents
== NULL
) {
4764 printf("key_setident: No more memory.\n");
4766 msg0
->sadb_msg_errno
= ENOBUFS
;
4769 KMALLOC(sah
->identd
, struct sadb_ident
*, iddstlen
);
4770 if (sah
->identd
== NULL
) {
4773 printf("key_setident: No more memory.\n");
4775 msg0
->sadb_msg_errno
= ENOBUFS
;
4778 bcopy(idsrc
, sah
->idents
, idsrclen
);
4779 bcopy(iddst
, sah
->identd
, iddstlen
);
4784 static struct sadb_msg
*
4785 key_getmsgbuf_x1(mhp
)
4788 struct sadb_msg
*msg0
;
4789 struct sadb_msg
*newmsg
;
4794 if (mhp
== NULL
|| mhp
[0] == NULL
)
4795 panic("key_getmsgbuf_x1: NULL pointer is passed.\n");
4797 msg0
= (struct sadb_msg
*)mhp
[0];
4799 /* create new sadb_msg to reply. */
4800 len
= sizeof(struct sadb_msg
)
4801 + sizeof(struct sadb_sa
)
4802 + PFKEY_EXTLEN(mhp
[SADB_EXT_ADDRESS_SRC
])
4803 + PFKEY_EXTLEN(mhp
[SADB_EXT_ADDRESS_DST
])
4804 + (mhp
[SADB_EXT_LIFETIME_HARD
] == NULL
4805 ? 0 : sizeof(struct sadb_lifetime
))
4806 + (mhp
[SADB_EXT_LIFETIME_SOFT
] == NULL
4807 ? 0 : sizeof(struct sadb_lifetime
))
4808 + (mhp
[SADB_EXT_IDENTITY_SRC
] == NULL
4809 ? 0 : PFKEY_EXTLEN(mhp
[SADB_EXT_IDENTITY_SRC
]))
4810 + (mhp
[SADB_EXT_IDENTITY_DST
] == NULL
4811 ? 0 : PFKEY_EXTLEN(mhp
[SADB_EXT_IDENTITY_DST
]));
4813 KMALLOC(newmsg
, struct sadb_msg
*, len
);
4816 bzero((caddr_t
)newmsg
, len
);
4818 bcopy((caddr_t
)mhp
[0], (caddr_t
)newmsg
, sizeof(*msg0
));
4819 newmsg
->sadb_msg_errno
= 0;
4820 newmsg
->sadb_msg_len
= PFKEY_UNIT64(len
);
4821 p
= (caddr_t
)newmsg
+ sizeof(*msg0
);
4823 p
= key_setsadbext(p
, mhp
[SADB_EXT_SA
]);
4824 p
= key_setsadbext(p
, mhp
[SADB_EXT_ADDRESS_SRC
]);
4825 p
= key_setsadbext(p
, mhp
[SADB_EXT_ADDRESS_DST
]);
4827 if (mhp
[SADB_EXT_LIFETIME_HARD
] != NULL
)
4828 p
= key_setsadbext(p
, mhp
[SADB_EXT_LIFETIME_HARD
]);
4830 if (mhp
[SADB_EXT_LIFETIME_SOFT
] != NULL
)
4831 p
= key_setsadbext(p
, mhp
[SADB_EXT_LIFETIME_SOFT
]);
4833 if (mhp
[SADB_EXT_IDENTITY_SRC
] != NULL
)
4834 p
= key_setsadbext(p
, mhp
[SADB_EXT_IDENTITY_SRC
]);
4835 if (mhp
[SADB_EXT_IDENTITY_DST
] != NULL
)
4836 p
= key_setsadbext(p
, mhp
[SADB_EXT_IDENTITY_DST
]);
4842 * SADB_DELETE processing
4844 * <base, SA(*), address(SD)>
4845 * from the ikmpd, and set SADB_SASTATE_DEAD,
4847 * <base, SA(*), address(SD)>
4850 * IN: mhp: pointer to the pointer to each header.
4851 * OUT: NULL if fail.
4852 * other if success, return pointer to the message to send.
4854 static struct sadb_msg
*
4858 struct sadb_msg
*msg0
;
4859 struct sadb_sa
*sa0
;
4860 struct sadb_address
*src0
, *dst0
;
4861 struct secasindex saidx
;
4862 struct secashead
*sah
;
4863 struct secasvar
*sav
;
4867 if (mhp
== NULL
|| mhp
[0] == NULL
)
4868 panic("key_delete: NULL pointer is passed.\n");
4870 msg0
= (struct sadb_msg
*)mhp
[0];
4872 /* map satype to proto */
4873 if ((proto
= key_satype2proto(msg0
->sadb_msg_satype
)) == 0) {
4875 printf("key_delete: invalid satype is passed.\n");
4877 msg0
->sadb_msg_errno
= EINVAL
;
4881 if (mhp
[SADB_EXT_SA
] == NULL
4882 || mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
4883 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
) {
4885 printf("key_delete: invalid message is passed.\n");
4887 msg0
->sadb_msg_errno
= EINVAL
;
4890 sa0
= (struct sadb_sa
*)mhp
[SADB_EXT_SA
];
4891 src0
= (struct sadb_address
*)(mhp
[SADB_EXT_ADDRESS_SRC
]);
4892 dst0
= (struct sadb_address
*)(mhp
[SADB_EXT_ADDRESS_DST
]);
4894 KEY_SETSECASIDX(proto
, msg0
, src0
+1, dst0
+1, &saidx
);
4896 /* get a SA header */
4897 if ((sah
= key_getsah(&saidx
)) == NULL
) {
4899 printf("key_delete: no SA found.\n");
4901 msg0
->sadb_msg_errno
= ENOENT
;
4905 /* get a SA with SPI. */
4906 sav
= key_getsavbyspi(sah
, sa0
->sadb_sa_spi
);
4909 printf("key_delete: no alive SA found.\n");
4911 msg0
->sadb_msg_errno
= ENOENT
;
4915 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
4920 struct sadb_msg
*newmsg
;
4924 /* create new sadb_msg to reply. */
4925 len
= sizeof(struct sadb_msg
)
4926 + sizeof(struct sadb_sa
)
4927 + PFKEY_EXTLEN(mhp
[SADB_EXT_ADDRESS_SRC
])
4928 + PFKEY_EXTLEN(mhp
[SADB_EXT_ADDRESS_DST
]);
4930 KMALLOC(newmsg
, struct sadb_msg
*, len
);
4931 if (newmsg
== NULL
) {
4933 printf("key_delete: No more memory.\n");
4935 msg0
->sadb_msg_errno
= ENOBUFS
;
4938 bzero((caddr_t
)newmsg
, len
);
4940 bcopy((caddr_t
)mhp
[0], (caddr_t
)newmsg
, sizeof(*msg0
));
4941 newmsg
->sadb_msg_errno
= 0;
4942 newmsg
->sadb_msg_len
= PFKEY_UNIT64(len
);
4943 p
= (caddr_t
)newmsg
+ sizeof(*msg0
);
4945 p
= key_setsadbext(p
, mhp
[SADB_EXT_SA
]);
4946 p
= key_setsadbext(p
, mhp
[SADB_EXT_ADDRESS_SRC
]);
4947 p
= key_setsadbext(p
, mhp
[SADB_EXT_ADDRESS_DST
]);
4954 * SADB_GET processing
4956 * <base, SA(*), address(SD)>
4957 * from the ikmpd, and get a SP and a SA to respond,
4959 * <base, SA, (lifetime(HSC),) address(SD), (address(P),) key(AE),
4960 * (identity(SD),) (sensitivity)>
4963 * IN: mhp: pointer to the pointer to each header.
4964 * OUT: NULL if fail.
4965 * other if success, return pointer to the message to send.
4967 static struct sadb_msg
*
4971 struct sadb_msg
*msg0
;
4972 struct sadb_sa
*sa0
;
4973 struct sadb_address
*src0
, *dst0
;
4974 struct secasindex saidx
;
4975 struct secashead
*sah
;
4976 struct secasvar
*sav
;
4980 if (mhp
== NULL
|| mhp
[0] == NULL
)
4981 panic("key_get: NULL pointer is passed.\n");
4983 msg0
= (struct sadb_msg
*)mhp
[0];
4985 /* map satype to proto */
4986 if ((proto
= key_satype2proto(msg0
->sadb_msg_satype
)) == 0) {
4988 printf("key_get: invalid satype is passed.\n");
4990 msg0
->sadb_msg_errno
= EINVAL
;
4994 if (mhp
[SADB_EXT_SA
] == NULL
4995 || mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
4996 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
) {
4998 printf("key_get: invalid message is passed.\n");
5000 msg0
->sadb_msg_errno
= EINVAL
;
5003 sa0
= (struct sadb_sa
*)mhp
[SADB_EXT_SA
];
5004 src0
= (struct sadb_address
*)(mhp
[SADB_EXT_ADDRESS_SRC
]);
5005 dst0
= (struct sadb_address
*)(mhp
[SADB_EXT_ADDRESS_DST
]);
5007 KEY_SETSECASIDX(proto
, msg0
, src0
+1, dst0
+1, &saidx
);
5009 /* get a SA header */
5010 if ((sah
= key_getsah(&saidx
)) == NULL
) {
5012 printf("key_get: no SA found.\n");
5014 msg0
->sadb_msg_errno
= ENOENT
;
5018 /* get a SA with SPI. */
5019 sav
= key_getsavbyspi(sah
, sa0
->sadb_sa_spi
);
5022 printf("key_get: no SA with state of mature found.\n");
5024 msg0
->sadb_msg_errno
= ENOENT
;
5029 struct sadb_msg
*newmsg
;
5033 /* map proto to satype */
5034 if ((satype
= key_proto2satype(sah
->saidx
.proto
)) == 0) {
5036 printf("key_get: there was invalid proto in SAD.\n");
5038 msg0
->sadb_msg_errno
= EINVAL
;
5042 /* calculate a length of message buffer */
5043 len
= key_getmsglen(sav
);
5045 KMALLOC(newmsg
, struct sadb_msg
*, len
);
5046 if (newmsg
== NULL
) {
5048 printf("key_get: No more memory.\n");
5050 msg0
->sadb_msg_errno
= ENOBUFS
;
5054 /* create new sadb_msg to reply. */
5055 (void)key_setdumpsa(newmsg
, sav
, SADB_GET
,
5056 satype
, msg0
->sadb_msg_seq
, msg0
->sadb_msg_pid
);
5063 * SADB_ACQUIRE processing called by key_checkrequest() and key_acquire2().
5065 * <base, SA, address(SD), (address(P)),
5066 * (identity(SD),) (sensitivity,) proposal>
5067 * to KMD, and expect to receive
5068 * <base> with SADB_ACQUIRE if error occured,
5070 * <base, src address, dst address, (SPI range)> with SADB_GETSPI
5071 * from KMD by PF_KEY.
5073 * sensitivity is not supported.
5077 * others: error number
5080 key_acquire(saidx
, sp
)
5081 struct secasindex
*saidx
;
5082 struct secpolicy
*sp
;
5084 #ifndef IPSEC_NONBLOCK_ACQUIRE
5085 struct secacq
*newacq
;
5087 struct secpolicyindex
*spidx
= NULL
;
5092 if (saidx
== NULL
|| sp
== NULL
)
5093 panic("key_acquire: NULL pointer is passed.\n");
5094 if ((satype
= key_proto2satype(saidx
->proto
)) == 0)
5095 panic("key_acquire: invalid proto is passed.\n");
5099 #ifndef IPSEC_NONBLOCK_ACQUIRE
5101 * We never do anything about acquirng SA. There is anather
5102 * solution that kernel blocks to send SADB_ACQUIRE message until
5103 * getting something message from IKEd. In later case, to be
5104 * managed with ACQUIRING list.
5106 /* get a entry to check whether sending message or not. */
5107 if ((newacq
= key_getacq(saidx
)) != NULL
) {
5108 if (key_blockacq_count
< newacq
->count
) {
5109 /* reset counter and do send message. */
5112 /* increment counter and do nothing. */
5117 /* make new entry for blocking to send SADB_ACQUIRE. */
5118 if ((newacq
= key_newacq(saidx
)) == NULL
)
5121 /* add to acqtree */
5122 LIST_INSERT_HEAD(&acqtree
, newacq
, chain
);
5127 struct sadb_msg
*newmsg
= NULL
;
5128 union sadb_x_ident_id id
;
5132 /* create new sadb_msg to reply. */
5133 len
= sizeof(struct sadb_msg
)
5134 + sizeof(struct sadb_address
)
5135 + PFKEY_ALIGN8(saidx
->src
.ss_len
)
5136 + sizeof(struct sadb_address
)
5137 + PFKEY_ALIGN8(saidx
->dst
.ss_len
)
5138 + sizeof(struct sadb_x_policy
)
5139 + sizeof(struct sadb_ident
)
5140 + PFKEY_ALIGN8(spidx
->src
.ss_len
)
5141 + sizeof(struct sadb_ident
)
5142 + PFKEY_ALIGN8(spidx
->dst
.ss_len
)
5143 + sizeof(struct sadb_prop
)
5144 + sizeof(struct sadb_comb
); /* XXX to be multiple */
5146 KMALLOC(newmsg
, struct sadb_msg
*, len
);
5149 printf("key_acquire: No more memory.\n");
5153 bzero((caddr_t
)newmsg
, len
);
5155 newmsg
->sadb_msg_version
= PF_KEY_V2
;
5156 newmsg
->sadb_msg_type
= SADB_ACQUIRE
;
5157 newmsg
->sadb_msg_errno
= 0;
5158 newmsg
->sadb_msg_satype
= satype
;
5159 newmsg
->sadb_msg_len
= PFKEY_UNIT64(len
);
5160 newmsg
->sadb_msg_mode
= saidx
->mode
;
5161 newmsg
->sadb_msg_reqid
= saidx
->reqid
;
5163 #ifndef IPSEC_NONBLOCK_ACQUIRE
5164 newmsg
->sadb_msg_seq
= newacq
->seq
;
5166 newmsg
->sadb_msg_seq
= (acq_seq
= (acq_seq
== ~0 ? 1 : ++acq_seq
));
5169 newmsg
->sadb_msg_pid
= 0;
5171 p
= (caddr_t
)newmsg
+ sizeof(struct sadb_msg
);
5173 /* set sadb_address for saidx's. */
5174 p
= key_setsadbaddr(p
,
5175 SADB_EXT_ADDRESS_SRC
,
5176 (struct sockaddr
*)&saidx
->src
,
5177 _INALENBYAF(saidx
->src
.ss_family
) << 3,
5179 p
= key_setsadbaddr(p
,
5180 SADB_EXT_ADDRESS_DST
,
5181 (struct sockaddr
*)&saidx
->dst
,
5182 _INALENBYAF(saidx
->dst
.ss_family
) << 3,
5185 /* set sadb_x_policy */
5186 p
= key_setsadbxpolicy(p
, sp
->policy
, sp
->spidx
.dir
, sp
->id
);
5188 /* set sadb_address for spidx's. */
5189 bzero(&id
, sizeof(id
));
5190 id
.sadb_x_ident_id_addr
.prefix
= spidx
->prefs
;
5191 id
.sadb_x_ident_id_addr
.ul_proto
= spidx
->ul_proto
;
5192 p
= key_setsadbident(p
,
5193 SADB_EXT_IDENTITY_SRC
,
5194 SADB_X_IDENTTYPE_ADDR
,
5195 (caddr_t
)&spidx
->src
,
5199 bzero(&id
, sizeof(id
));
5200 id
.sadb_x_ident_id_addr
.prefix
= spidx
->prefd
;
5201 id
.sadb_x_ident_id_addr
.ul_proto
= spidx
->ul_proto
;
5202 p
= key_setsadbident(p
,
5203 SADB_EXT_IDENTITY_DST
,
5204 SADB_X_IDENTTYPE_ADDR
,
5205 (caddr_t
)&spidx
->dst
,
5209 /* create proposal extension */
5210 /* set combination extension */
5211 /* XXX: to be defined by proposal database */
5213 struct sadb_prop
*prop
;
5214 struct sadb_comb
*comb
;
5216 prop
= (struct sadb_prop
*)p
;
5217 prop
->sadb_prop_len
= PFKEY_UNIT64(sizeof(*prop
) + sizeof(*comb
));
5218 /* XXX to be multiple */
5219 prop
->sadb_prop_exttype
= SADB_EXT_PROPOSAL
;
5220 prop
->sadb_prop_replay
= 32; /* XXX be variable ? */
5221 p
+= sizeof(struct sadb_prop
);
5223 comb
= (struct sadb_comb
*)p
;
5224 comb
->sadb_comb_auth
= SADB_AALG_SHA1HMAC
; /* XXX ??? */
5225 comb
->sadb_comb_encrypt
= SADB_EALG_DESCBC
; /* XXX ??? */
5226 comb
->sadb_comb_flags
= 0;
5227 comb
->sadb_comb_auth_minbits
= 8; /* XXX */
5228 comb
->sadb_comb_auth_maxbits
= 1024; /* XXX */
5229 comb
->sadb_comb_encrypt_minbits
= 64; /* XXX */
5230 comb
->sadb_comb_encrypt_maxbits
= 64; /* XXX */
5231 comb
->sadb_comb_soft_allocations
= 0;
5232 comb
->sadb_comb_hard_allocations
= 0;
5233 comb
->sadb_comb_soft_bytes
= 0;
5234 comb
->sadb_comb_hard_bytes
= 0;
5235 comb
->sadb_comb_soft_addtime
= 0;
5236 comb
->sadb_comb_hard_addtime
= 0;
5237 comb
->sadb_comb_soft_usetime
= 0;
5238 comb
->sadb_comb_hard_usetime
= 0;
5243 #if 0 /* XXX Do it ?*/
5244 if (idexttype
&& fqdn
) {
5245 /* create identity extension (FQDN) */
5246 struct sadb_ident
*id
;
5249 fqdnlen
= strlen(fqdn
) + 1; /* +1 for terminating-NUL */
5250 id
= (struct sadb_ident
*)p
;
5251 bzero(id
, sizeof(*id
) + PFKEY_ALIGN8(fqdnlen
));
5252 id
->sadb_ident_len
= PFKEY_UNIT64(sizeof(*id
) + PFKEY_ALIGN8(fqdnlen
));
5253 id
->sadb_ident_exttype
= idexttype
;
5254 id
->sadb_ident_type
= SADB_IDENTTYPE_FQDN
;
5255 bcopy(fqdn
, id
+ 1, fqdnlen
);
5256 p
+= sizeof(struct sadb_ident
) + PFKEY_ALIGN8(fqdnlen
);
5260 /* create identity extension (USERFQDN) */
5261 struct sadb_ident
*id
;
5265 /* +1 for terminating-NUL */
5266 userfqdnlen
= strlen(userfqdn
) + 1;
5269 id
= (struct sadb_ident
*)p
;
5270 bzero(id
, sizeof(*id
) + PFKEY_ALIGN8(userfqdnlen
));
5271 id
->sadb_ident_len
= PFKEY_UNIT64(sizeof(*id
) + PFKEY_ALIGN8(userfqdnlen
));
5272 id
->sadb_ident_exttype
= idexttype
;
5273 id
->sadb_ident_type
= SADB_IDENTTYPE_USERFQDN
;
5274 /* XXX is it correct? */
5275 if (curproc
&& curproc
->p_cred
)
5276 id
->sadb_ident_id
= curproc
->p_cred
->p_ruid
;
5277 if (userfqdn
&& userfqdnlen
)
5278 bcopy(userfqdn
, id
+ 1, userfqdnlen
);
5279 p
+= sizeof(struct sadb_ident
) + PFKEY_ALIGN8(userfqdnlen
);
5283 error
= key_sendall(newmsg
, len
);
5286 printf("key_acquire: key_sendall returned %d\n", error
);
5294 #ifndef IPSEC_NONBLOCK_ACQUIRE
5295 static struct secacq
*
5297 struct secasindex
*saidx
;
5299 struct secacq
*newacq
;
5302 KMALLOC(newacq
, struct secacq
*, sizeof(struct secacq
));
5303 if (newacq
== NULL
) {
5305 printf("key_newacq: No more memory.\n");
5309 bzero(newacq
, sizeof(*newacq
));
5312 bcopy(saidx
, &newacq
->saidx
, sizeof(newacq
->saidx
));
5313 newacq
->seq
= (acq_seq
== ~0 ? 1 : ++acq_seq
);
5320 static struct secacq
*
5322 struct secasindex
*saidx
;
5326 LIST_FOREACH(acq
, &acqtree
, chain
) {
5327 if (key_cmpsaidx_exactly(saidx
, &acq
->saidx
))
5334 static struct secacq
*
5335 key_getacqbyseq(seq
)
5340 LIST_FOREACH(acq
, &acqtree
, chain
) {
5341 if (acq
->seq
== seq
)
5349 static struct secspacq
*
5351 struct secpolicyindex
*spidx
;
5353 struct secspacq
*acq
;
5356 KMALLOC(acq
, struct secspacq
*, sizeof(struct secspacq
));
5359 printf("key_newspacq: No more memory.\n");
5363 bzero(acq
, sizeof(*acq
));
5366 bcopy(spidx
, &acq
->spidx
, sizeof(acq
->spidx
));
5373 static struct secspacq
*
5375 struct secpolicyindex
*spidx
;
5377 struct secspacq
*acq
;
5379 LIST_FOREACH(acq
, &spacqtree
, chain
) {
5380 if (key_cmpspidx_exactly(spidx
, &acq
->spidx
))
5388 * SADB_ACQUIRE processing,
5389 * in first situation, is receiving
5391 * from the ikmpd, and clear sequence of its secasvar entry.
5393 * In second situation, is receiving
5394 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
5395 * from a user land process, and return
5396 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
5399 * IN: mhp: pointer to the pointer to each header.
5400 * OUT: NULL if fail.
5401 * other if success, return pointer to the message to send.
5403 static struct sadb_msg
*
5407 struct sadb_msg
*msg0
;
5408 struct sadb_address
*src0
, *dst0
;
5409 struct secasindex saidx
;
5410 struct secashead
*sah
;
5414 if (mhp
== NULL
|| mhp
[0] == NULL
)
5415 panic("key_acquire2: NULL pointer is passed.\n");
5417 msg0
= (struct sadb_msg
*)mhp
[0];
5420 * Error message from KMd.
5421 * We assume that if error was occured in IKEd, the length of PFKEY
5422 * message is equal to the size of sadb_msg structure.
5423 * We return ~0 even if error occured in this function.
5425 if (msg0
->sadb_msg_len
== PFKEY_UNIT64(sizeof(struct sadb_msg
))) {
5427 #ifndef IPSEC_NONBLOCK_ACQUIRE
5430 /* check sequence number */
5431 if (msg0
->sadb_msg_seq
== 0) {
5433 printf("key_acquire2: must specify sequence number.\n");
5435 return (struct sadb_msg
*)~0;
5438 if ((acq
= key_getacqbyseq(msg0
->sadb_msg_seq
)) == NULL
) {
5440 printf("key_acquire2: "
5441 "invalid sequence number is passed.\n");
5443 return (struct sadb_msg
*)~0;
5446 /* reset acq counter in order to deletion by timehander. */
5447 acq
->tick
= key_blockacq_lifetime
;
5450 return (struct sadb_msg
*)~0;
5455 * This message is from user land.
5458 /* map satype to proto */
5459 if ((proto
= key_satype2proto(msg0
->sadb_msg_satype
)) == 0) {
5461 printf("key_acquire2: invalid satype is passed.\n");
5463 msg0
->sadb_msg_errno
= EINVAL
;
5467 if (mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
5468 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
5469 || mhp
[SADB_EXT_PROPOSAL
] == NULL
) {
5472 printf("key_acquire2: invalid message is passed.\n");
5474 msg0
->sadb_msg_errno
= EINVAL
;
5477 src0
= (struct sadb_address
*)(mhp
[SADB_EXT_ADDRESS_SRC
]);
5478 dst0
= (struct sadb_address
*)(mhp
[SADB_EXT_ADDRESS_DST
]);
5480 KEY_SETSECASIDX(proto
, msg0
, src0
+1, dst0
+1, &saidx
);
5482 /* get a SA index */
5483 if ((sah
= key_getsah(&saidx
)) != NULL
) {
5485 printf("key_acquire2: a SA exists already.\n");
5487 msg0
->sadb_msg_errno
= EEXIST
;
5491 msg0
->sadb_msg_errno
= key_acquire(&saidx
, NULL
);
5492 if (msg0
->sadb_msg_errno
!= 0) {
5494 printf("key_acquire2: error %d returned "
5495 "from key_acquire.\n", msg0
->sadb_msg_errno
);
5501 struct sadb_msg
*newmsg
;
5504 /* create new sadb_msg to reply. */
5505 len
= PFKEY_UNUNIT64(msg0
->sadb_msg_len
);
5507 KMALLOC(newmsg
, struct sadb_msg
*, len
);
5508 if (newmsg
== NULL
) {
5510 printf("key_acquire2: No more memory.\n");
5512 msg0
->sadb_msg_errno
= ENOBUFS
;
5515 bzero((caddr_t
)newmsg
, len
);
5517 bcopy(mhp
[0], (caddr_t
)newmsg
, len
);
5524 * SADB_REGISTER processing.
5525 * If SATYPE_UNSPEC has been passed as satype, only return sabd_supported.
5528 * from the ikmpd, and register a socket to send PF_KEY messages,
5532 * If socket is detached, must free from regnode.
5535 * others: error number
5537 static struct sadb_msg
*
5538 key_register(mhp
, so
)
5542 struct sadb_msg
*msg0
;
5543 struct secreg
*reg
, *newreg
= 0;
5546 if (mhp
== NULL
|| so
== NULL
|| mhp
[0] == NULL
)
5547 panic("key_register: NULL pointer is passed.\n");
5549 msg0
= (struct sadb_msg
*)mhp
[0];
5551 /* check for invalid register message */
5552 if (msg0
->sadb_msg_satype
>= sizeof(regtree
)/sizeof(regtree
[0])) {
5553 msg0
->sadb_msg_errno
= EINVAL
;
5557 /* When SATYPE_UNSPEC is specified, only return sabd_supported. */
5558 if (msg0
->sadb_msg_satype
== SADB_SATYPE_UNSPEC
)
5561 /* check whether existing or not */
5562 LIST_FOREACH(reg
, ®tree
[msg0
->sadb_msg_satype
], chain
) {
5563 if (reg
->so
== so
) {
5565 printf("key_register: socket exists already.\n");
5567 msg0
->sadb_msg_errno
= EEXIST
;
5572 /* create regnode */
5573 KMALLOC(newreg
, struct secreg
*, sizeof(struct secreg
));
5574 if (newreg
== NULL
) {
5576 printf("key_register: No more memory.\n");
5578 msg0
->sadb_msg_errno
= ENOBUFS
;
5581 bzero((caddr_t
)newreg
, sizeof(struct secreg
));
5584 ((struct keycb
*)sotorawcb(so
))->kp_registered
++;
5586 /* add regnode to regtree. */
5587 LIST_INSERT_HEAD(®tree
[msg0
->sadb_msg_satype
], newreg
, chain
);
5591 struct sadb_msg
*newmsg
;
5592 struct sadb_supported
*sup
;
5593 u_int len
, alen
, elen
;
5596 /* create new sadb_msg to reply. */
5597 alen
= sizeof(struct sadb_supported
)
5598 + ((SADB_AALG_MAX
- 1) * sizeof(struct sadb_alg
));
5601 elen
= sizeof(struct sadb_supported
)
5602 + ((SADB_EALG_MAX
- 1) * sizeof(struct sadb_alg
));
5607 len
= sizeof(struct sadb_msg
)
5611 KMALLOC(newmsg
, struct sadb_msg
*, len
);
5612 if (newmsg
== NULL
) {
5614 printf("key_register: No more memory.\n");
5616 msg0
->sadb_msg_errno
= ENOBUFS
;
5619 bzero((caddr_t
)newmsg
, len
);
5621 bcopy((caddr_t
)mhp
[0], (caddr_t
)newmsg
, sizeof(*msg0
));
5622 newmsg
->sadb_msg_errno
= 0;
5623 newmsg
->sadb_msg_len
= PFKEY_UNIT64(len
);
5624 p
= (caddr_t
)newmsg
+ sizeof(*msg0
);
5626 /* for authentication algorithm */
5627 sup
= (struct sadb_supported
*)p
;
5628 sup
->sadb_supported_len
= PFKEY_UNIT64(alen
);
5629 sup
->sadb_supported_exttype
= SADB_EXT_SUPPORTED_AUTH
;
5634 struct sadb_alg
*alg
;
5635 struct ah_algorithm
*algo
;
5637 for (i
= 1; i
< SADB_AALG_MAX
; i
++) {
5638 algo
= &ah_algorithms
[i
];
5639 alg
= (struct sadb_alg
*)p
;
5640 alg
->sadb_alg_id
= i
;
5641 alg
->sadb_alg_ivlen
= 0;
5642 alg
->sadb_alg_minbits
= algo
->keymin
;
5643 alg
->sadb_alg_maxbits
= algo
->keymax
;
5644 p
+= sizeof(struct sadb_alg
);
5649 /* for encryption algorithm */
5650 sup
= (struct sadb_supported
*)p
;
5651 sup
->sadb_supported_len
= PFKEY_UNIT64(elen
);
5652 sup
->sadb_supported_exttype
= SADB_EXT_SUPPORTED_ENCRYPT
;
5657 struct sadb_alg
*alg
;
5658 struct esp_algorithm
*algo
;
5660 for (i
= 1; i
< SADB_EALG_MAX
; i
++) {
5661 algo
= &esp_algorithms
[i
];
5663 alg
= (struct sadb_alg
*)p
;
5664 alg
->sadb_alg_id
= i
;
5665 if (algo
&& algo
->ivlen
) {
5667 * give NULL to get the value preferred by algorithm
5668 * XXX SADB_X_EXT_DERIV ?
5670 alg
->sadb_alg_ivlen
= (*algo
->ivlen
)(NULL
);
5672 alg
->sadb_alg_ivlen
= 0;
5673 alg
->sadb_alg_minbits
= algo
->keymin
;
5674 alg
->sadb_alg_maxbits
= algo
->keymax
;
5675 p
+= sizeof(struct sadb_alg
);
5685 * free secreg entry registered.
5686 * XXX: I want to do free a socket marked done SADB_RESIGER to socket.
5697 panic("key_freereg: NULL pointer is passed.\n");
5700 * check whether existing or not.
5701 * check all type of SA, because there is a potential that
5702 * one socket is registered to multiple type of SA.
5704 for (i
= 0; i
<= SADB_SATYPE_MAX
; i
++) {
5705 LIST_FOREACH(reg
, ®tree
[i
], chain
) {
5707 && __LIST_CHAINED(reg
)) {
5708 LIST_REMOVE(reg
, chain
);
5719 * SADB_EXPIRE processing
5721 * <base, SA, lifetime(C and one of HS), address(SD)>
5723 * NOTE: We send only soft lifetime extension.
5726 * others : error number
5730 struct secasvar
*sav
;
5735 /* XXX: Why do we lock ? */
5737 s
= splsoftnet(); /*called from softclock()*/
5739 s
= splnet(); /*called from softclock()*/
5744 panic("key_expire: NULL pointer is passed.\n");
5745 if (sav
->sah
== NULL
)
5746 panic("key_expire: Why was SA index in SA NULL.\n");
5747 if ((satype
= key_proto2satype(sav
->sah
->saidx
.proto
)) == 0)
5748 panic("key_expire: invalid proto is passed.\n");
5751 struct sadb_msg
*newmsg
= NULL
;
5756 /* create new sadb_msg to reply. */
5757 len
= sizeof(struct sadb_msg
)
5758 + sizeof(struct sadb_sa
)
5759 + sizeof(struct sadb_lifetime
)
5760 + sizeof(struct sadb_lifetime
)
5761 + sizeof(struct sadb_address
)
5762 + PFKEY_ALIGN8(sav
->sah
->saidx
.src
.ss_len
)
5763 + sizeof(struct sadb_address
)
5764 + PFKEY_ALIGN8(sav
->sah
->saidx
.dst
.ss_len
);
5766 KMALLOC(newmsg
, struct sadb_msg
*, len
);
5767 if (newmsg
== NULL
) {
5769 printf("key_expire: No more memory.\n");
5774 bzero((caddr_t
)newmsg
, len
);
5776 /* set msg header */
5777 p
= key_setsadbmsg((caddr_t
)newmsg
, SADB_EXPIRE
, len
,
5778 satype
, sav
->seq
, 0,
5779 sav
->sah
->saidx
.mode
, sav
->sah
->saidx
.reqid
,
5782 /* create SA extension */
5783 p
= key_setsadbsa(p
, sav
);
5785 /* create lifetime extension */
5787 struct sadb_lifetime
*m_lt
= (struct sadb_lifetime
*)p
;
5789 m_lt
->sadb_lifetime_len
= PFKEY_UNIT64(sizeof(struct sadb_lifetime
));
5790 m_lt
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
5791 m_lt
->sadb_lifetime_allocations
= sav
->lft_c
->sadb_lifetime_allocations
;
5792 m_lt
->sadb_lifetime_bytes
= sav
->lft_c
->sadb_lifetime_bytes
;
5793 m_lt
->sadb_lifetime_addtime
= sav
->lft_c
->sadb_lifetime_addtime
;
5794 m_lt
->sadb_lifetime_usetime
= sav
->lft_c
->sadb_lifetime_usetime
;
5795 p
+= sizeof(struct sadb_lifetime
);
5797 /* copy SOFT lifetime extension. */
5798 bcopy(sav
->lft_s
, p
, sizeof(struct sadb_lifetime
));
5799 p
+= sizeof(struct sadb_lifetime
);
5802 /* set sadb_address for source */
5803 p
= key_setsadbaddr(p
,
5804 SADB_EXT_ADDRESS_SRC
,
5805 (struct sockaddr
*)&sav
->sah
->saidx
.src
,
5806 _INALENBYAF(sav
->sah
->saidx
.src
.ss_family
) << 3,
5809 /* set sadb_address for destination */
5810 p
= key_setsadbaddr(p
,
5811 SADB_EXT_ADDRESS_DST
,
5812 (struct sockaddr
*)&sav
->sah
->saidx
.dst
,
5813 _INALENBYAF(sav
->sah
->saidx
.dst
.ss_family
) << 3,
5816 error
= key_sendall(newmsg
, len
);
5823 * SADB_FLUSH processing
5826 * from the ikmpd, and free all entries in secastree.
5830 * NOTE: to do is only marking SADB_SASTATE_DEAD.
5832 * IN: mhp: pointer to the pointer to each header.
5833 * OUT: NULL if fail.
5834 * other if success, return pointer to the message to send.
5836 static struct sadb_msg
*
5840 struct sadb_msg
*msg0
;
5841 struct secashead
*sah
, *nextsah
;
5842 struct secasvar
*sav
, *nextsav
;
5848 if (mhp
== NULL
|| mhp
[0] == NULL
)
5849 panic("key_flush: NULL pointer is passed.\n");
5851 msg0
= (struct sadb_msg
*)mhp
[0];
5853 /* map satype to proto */
5854 if ((proto
= key_satype2proto(msg0
->sadb_msg_satype
)) == 0) {
5856 printf("key_flush: invalid satype is passed.\n");
5858 msg0
->sadb_msg_errno
= EINVAL
;
5862 /* no SATYPE specified, i.e. flushing all SA. */
5863 for (sah
= LIST_FIRST(&sahtree
);
5867 nextsah
= LIST_NEXT(sah
, chain
);
5869 if (msg0
->sadb_msg_satype
!= SADB_SATYPE_UNSPEC
5870 && proto
!= sah
->saidx
.proto
)
5874 stateidx
< _ARRAYLEN(saorder_state_alive
);
5877 state
= saorder_state_any
[stateidx
];
5878 for (sav
= LIST_FIRST(&sah
->savtree
[state
]);
5882 nextsav
= LIST_NEXT(sav
, chain
);
5884 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
5889 sah
->state
= SADB_SASTATE_DEAD
;
5893 struct sadb_msg
*newmsg
;
5896 /* create new sadb_msg to reply. */
5897 len
= sizeof(struct sadb_msg
);
5899 KMALLOC(newmsg
, struct sadb_msg
*, len
);
5900 if (newmsg
== NULL
) {
5902 printf("key_flush: No more memory.\n");
5904 msg0
->sadb_msg_errno
= ENOBUFS
;
5907 bzero((caddr_t
)newmsg
, len
);
5909 bcopy((caddr_t
)mhp
[0], (caddr_t
)newmsg
, sizeof(*msg0
));
5910 newmsg
->sadb_msg_errno
= 0;
5911 newmsg
->sadb_msg_len
= PFKEY_UNIT64(len
);
5918 * SADB_DUMP processing
5919 * dump all entries including status of DEAD in SAD.
5922 * from the ikmpd, and dump all secasvar leaves
5927 * IN: mhp: pointer to the pointer to each header.
5928 * OUT: error code. 0 on success.
5931 key_dump(mhp
, so
, target
)
5936 struct sadb_msg
*msg0
;
5937 struct secashead
*sah
;
5938 struct secasvar
*sav
;
5944 struct sadb_msg
*newmsg
;
5947 if (mhp
== NULL
|| mhp
[0] == NULL
)
5948 panic("key_dump: NULL pointer is passed.\n");
5950 msg0
= (struct sadb_msg
*)mhp
[0];
5952 /* map satype to proto */
5953 if ((proto
= key_satype2proto(msg0
->sadb_msg_satype
)) == 0) {
5955 printf("key_dump: invalid satype is passed.\n");
5957 msg0
->sadb_msg_errno
= EINVAL
;
5961 /* count sav entries to be sent to the userland. */
5963 LIST_FOREACH(sah
, &sahtree
, chain
) {
5965 if (msg0
->sadb_msg_satype
!= SADB_SATYPE_UNSPEC
5966 && proto
!= sah
->saidx
.proto
)
5970 stateidx
< _ARRAYLEN(saorder_state_any
);
5973 state
= saorder_state_any
[stateidx
];
5974 LIST_FOREACH(sav
, &sah
->savtree
[state
], chain
) {
5983 /* send this to the userland, one at a time. */
5985 LIST_FOREACH(sah
, &sahtree
, chain
) {
5987 if (msg0
->sadb_msg_satype
!= SADB_SATYPE_UNSPEC
5988 && proto
!= sah
->saidx
.proto
)
5991 /* map proto to satype */
5992 if ((satype
= key_proto2satype(sah
->saidx
.proto
)) == 0) {
5994 printf("key_dump: there was invalid proto in SAD.\n");
5996 msg0
->sadb_msg_errno
= EINVAL
;
6001 stateidx
< _ARRAYLEN(saorder_state_any
);
6004 state
= saorder_state_any
[stateidx
];
6005 LIST_FOREACH(sav
, &sah
->savtree
[state
], chain
) {
6007 len
= key_getmsglen(sav
);
6008 KMALLOC(newmsg
, struct sadb_msg
*, len
);
6009 if (newmsg
== NULL
) {
6011 printf("key_dump: No more memory.\n");
6015 bzero((caddr_t
)newmsg
, len
);
6018 (void)key_setdumpsa(newmsg
, sav
, SADB_DUMP
,
6019 satype
, cnt
, msg0
->sadb_msg_pid
);
6021 key_sendup(so
, newmsg
, len
, target
);
6032 * SADB_X_PROMISC processing
6035 key_promisc(mhp
, so
)
6039 struct sadb_msg
*msg0
;
6043 if (mhp
== NULL
|| mhp
[0] == NULL
)
6044 panic("key_promisc: NULL pointer is passed.\n");
6046 msg0
= (struct sadb_msg
*)mhp
[0];
6047 olen
= PFKEY_UNUNIT64(msg0
->sadb_msg_len
);
6049 if (olen
< sizeof(struct sadb_msg
)) {
6051 } else if (olen
== sizeof(struct sadb_msg
)) {
6052 /* enable/disable promisc mode */
6056 target
= KEY_SENDUP_ONE
;
6061 if ((kp
= (struct keycb
*)sotorawcb(so
)) == NULL
) {
6062 msg0
->sadb_msg_errno
= EINVAL
;
6065 msg0
->sadb_msg_errno
= 0;
6066 if (msg0
->sadb_msg_satype
== 1 || msg0
->sadb_msg_satype
== 0) {
6067 kp
->kp_promisc
= msg0
->sadb_msg_satype
;
6069 msg0
->sadb_msg_errno
= EINVAL
;
6073 /* send the original message back to everyone */
6074 msg0
->sadb_msg_errno
= 0;
6075 target
= KEY_SENDUP_ALL
;
6077 key_sendup(so
, msg0
, PFKEY_UNUNIT64(msg0
->sadb_msg_len
), target
);
6079 /* send packet as is */
6080 struct sadb_msg
*msg
;
6083 len
= olen
- sizeof(struct sadb_msg
);
6084 KMALLOC(msg
, struct sadb_msg
*, len
);
6086 msg0
->sadb_msg_errno
= ENOBUFS
;
6087 key_sendup(so
, msg0
, PFKEY_UNUNIT64(msg0
->sadb_msg_len
),
6088 KEY_SENDUP_ONE
); /*XXX*/
6091 /* XXX if sadb_msg_seq is specified, send to specific pid */
6092 key_sendup(so
, msg
, len
, KEY_SENDUP_ALL
);
6098 * send message to the socket.
6104 key_sendall(msg
, len
)
6105 struct sadb_msg
*msg
;
6113 panic("key_sendall: NULL pointer is passed.\n");
6115 /* search table registerd socket to send a message. */
6116 LIST_FOREACH(reg
, ®tree
[msg
->sadb_msg_satype
], chain
) {
6117 error
= key_sendup(reg
->so
, msg
, len
, KEY_SENDUP_ONE
);
6120 if (error
== ENOBUFS
)
6121 printf("key_sendall: No more memory.\n");
6123 printf("key_sendall: key_sendup returned %d\n",
6137 * parse sadb_msg buffer to process PFKEYv2,
6138 * and create a data to response if needed.
6139 * I think to be dealed with mbuf directly.
6141 * msgp : pointer to pointer to a received buffer pulluped.
6142 * This is rewrited to response.
6143 * so : pointer to socket.
6145 * length for buffer to send to user process.
6148 key_parse(msgp
, so
, targetp
)
6149 struct sadb_msg
**msgp
;
6153 struct sadb_msg
*msg
= *msgp
, *newmsg
= NULL
;
6154 caddr_t mhp
[SADB_EXT_MAX
+ 1];
6159 if (msg
== NULL
|| so
== NULL
)
6160 panic("key_parse: NULL pointer is passed.\n");
6162 KEYDEBUG(KEYDEBUG_KEY_DUMP
,
6163 printf("key_parse: passed sadb_msg\n");
6166 orglen
= PFKEY_UNUNIT64(msg
->sadb_msg_len
);
6169 *targetp
= KEY_SENDUP_ONE
;
6172 if (msg
->sadb_msg_version
!= PF_KEY_V2
) {
6174 printf("key_parse: PF_KEY version %u is mismatched.\n",
6175 msg
->sadb_msg_version
);
6177 pfkeystat
.out_invver
++;
6178 msg
->sadb_msg_errno
= EINVAL
;
6183 if (msg
->sadb_msg_type
> SADB_MAX
) {
6185 printf("key_parse: invalid type %u is passed.\n",
6186 msg
->sadb_msg_type
);
6188 msg
->sadb_msg_errno
= EINVAL
;
6189 pfkeystat
.out_invmsgtype
++;
6193 /* align message. */
6194 if (key_align(msg
, mhp
) != 0) {
6195 msg
->sadb_msg_errno
= EINVAL
;
6200 switch (msg
->sadb_msg_satype
) {
6201 case SADB_SATYPE_UNSPEC
:
6202 switch (msg
->sadb_msg_type
) {
6211 printf("key_parse: must specify satype "
6212 "when msg type=%u.\n",
6213 msg
->sadb_msg_type
);
6215 msg
->sadb_msg_errno
= EINVAL
;
6216 pfkeystat
.out_invsatype
++;
6220 case SADB_SATYPE_AH
:
6221 case SADB_SATYPE_ESP
:
6222 #if 1 /*nonstandard*/
6223 case SADB_X_SATYPE_IPCOMP
:
6225 switch (msg
->sadb_msg_type
) {
6227 case SADB_X_SPDDELETE
:
6229 case SADB_X_SPDDUMP
:
6230 case SADB_X_SPDFLUSH
:
6231 case SADB_X_SPDSETIDX
:
6232 case SADB_X_SPDUPDATE
:
6233 case SADB_X_SPDDELETE2
:
6235 printf("key_parse: illegal satype=%u\n",
6236 msg
->sadb_msg_type
);
6238 msg
->sadb_msg_errno
= EINVAL
;
6239 pfkeystat
.out_invsatype
++;
6243 case SADB_SATYPE_RSVP
:
6244 case SADB_SATYPE_OSPFV2
:
6245 case SADB_SATYPE_RIPV2
:
6246 case SADB_SATYPE_MIP
:
6248 printf("key_parse: type %u isn't supported.\n",
6249 msg
->sadb_msg_satype
);
6251 msg
->sadb_msg_errno
= EOPNOTSUPP
;
6252 pfkeystat
.out_invsatype
++;
6254 case 1: /* XXX: What does it do ? */
6255 if (msg
->sadb_msg_type
== SADB_X_PROMISC
)
6260 printf("key_parse: invalid type %u is passed.\n",
6261 msg
->sadb_msg_satype
);
6263 msg
->sadb_msg_errno
= EINVAL
;
6264 pfkeystat
.out_invsatype
++;
6268 /* check field of upper layer protocol and address family */
6269 if (mhp
[SADB_EXT_ADDRESS_SRC
] != NULL
6270 && mhp
[SADB_EXT_ADDRESS_DST
] != NULL
) {
6271 struct sadb_address
*src0
, *dst0
;
6274 src0
= (struct sadb_address
*)(mhp
[SADB_EXT_ADDRESS_SRC
]);
6275 dst0
= (struct sadb_address
*)(mhp
[SADB_EXT_ADDRESS_DST
]);
6277 /* check upper layer protocol */
6278 if (src0
->sadb_address_proto
!= dst0
->sadb_address_proto
) {
6280 printf("key_parse: upper layer protocol mismatched.\n");
6282 msg
->sadb_msg_errno
= EINVAL
;
6283 pfkeystat
.out_invaddr
++;
6288 if (PFKEY_ADDR_SADDR(src0
)->sa_family
6289 != PFKEY_ADDR_SADDR(dst0
)->sa_family
) {
6291 printf("key_parse: address family mismatched.\n");
6293 msg
->sadb_msg_errno
= EINVAL
;
6294 pfkeystat
.out_invaddr
++;
6298 prefix
= _INALENBYAF(PFKEY_ADDR_SADDR(src0
)->sa_family
) << 3;
6300 /* check max prefixlen */
6301 if (prefix
< src0
->sadb_address_prefixlen
6302 || prefix
< dst0
->sadb_address_prefixlen
) {
6304 printf("key_parse: illegal prefixlen.\n");
6306 msg
->sadb_msg_errno
= EINVAL
;
6307 pfkeystat
.out_invaddr
++;
6311 switch (PFKEY_ADDR_SADDR(src0
)->sa_family
) {
6317 printf("key_parse: invalid address family.\n");
6319 msg
->sadb_msg_errno
= EINVAL
;
6320 pfkeystat
.out_invaddr
++;
6325 * prefixlen == 0 is valid because there can be a case when
6326 * all addresses are matched.
6330 switch (msg
->sadb_msg_type
) {
6332 if ((newmsg
= key_getspi(mhp
)) == NULL
)
6335 *targetp
= KEY_SENDUP_ALL
;
6339 if ((newmsg
= key_update(mhp
)) == NULL
)
6342 *targetp
= KEY_SENDUP_ALL
;
6346 if ((newmsg
= key_add(mhp
)) == NULL
)
6349 *targetp
= KEY_SENDUP_ALL
;
6353 if ((newmsg
= key_delete(mhp
)) == NULL
)
6356 *targetp
= KEY_SENDUP_ALL
;
6360 if ((newmsg
= key_get(mhp
)) == NULL
)
6365 if ((newmsg
= key_acquire2(mhp
)) == NULL
)
6368 if (newmsg
== (struct sadb_msg
*)~0) {
6370 * It's not need to reply because of the message
6371 * that was reporting an error occured from the KMd.
6379 if ((newmsg
= key_register(mhp
, so
)) == NULL
)
6383 *targetp
= KEY_SENDUP_REGISTERED
;
6385 /* send result to all registered sockets */
6387 key_sendall(newmsg
, PFKEY_UNUNIT64(newmsg
->sadb_msg_len
));
6394 printf("key_parse: why is SADB_EXPIRE received ?\n");
6396 msg
->sadb_msg_errno
= EINVAL
;
6398 *targetp
= KEY_SENDUP_ALL
;
6399 pfkeystat
.out_invmsgtype
++;
6403 if ((newmsg
= key_flush(mhp
)) == NULL
)
6406 *targetp
= KEY_SENDUP_ALL
;
6410 /* key_dump will call key_sendup() on her own */
6411 error
= key_dump(mhp
, so
, KEY_SENDUP_ONE
);
6413 msg
->sadb_msg_errno
= error
;
6421 case SADB_X_PROMISC
:
6422 /* everything is handled in key_promisc() */
6423 key_promisc(mhp
, so
);
6425 return 0; /*nothing to reply*/
6427 case SADB_X_PCHANGE
:
6429 printf("key_parse: SADB_X_PCHANGE isn't supported.\n");
6431 msg
->sadb_msg_errno
= EINVAL
;
6432 pfkeystat
.out_invmsgtype
++;
6436 *targetp
= KEY_SENDUP_REGISTERED
;
6440 case SADB_X_SPDSETIDX
:
6441 case SADB_X_SPDUPDATE
:
6442 if ((newmsg
= key_spdadd(mhp
)) == NULL
)
6445 *targetp
= KEY_SENDUP_ALL
;
6448 case SADB_X_SPDDELETE
:
6449 if ((newmsg
= key_spddelete(mhp
)) == NULL
)
6452 *targetp
= KEY_SENDUP_ALL
;
6455 case SADB_X_SPDDELETE2
:
6456 if ((newmsg
= key_spddelete2(mhp
)) == NULL
)
6459 *targetp
= KEY_SENDUP_ALL
;
6463 /* key_spdget will call key_sendup() on her own */
6464 error
= key_spdget(mhp
, so
, KEY_SENDUP_ONE
);
6466 msg
->sadb_msg_errno
= error
;
6474 case SADB_X_SPDDUMP
:
6475 /* key_spddump will call key_sendup() on her own */
6476 error
= key_spddump(mhp
, so
, KEY_SENDUP_ONE
);
6478 msg
->sadb_msg_errno
= error
;
6486 case SADB_X_SPDFLUSH
:
6487 if ((newmsg
= key_spdflush(mhp
)) == NULL
)
6490 *targetp
= KEY_SENDUP_ALL
;
6494 msg
->sadb_msg_errno
= EOPNOTSUPP
;
6498 /* switch from old sadb_msg to new one if success. */
6502 return PFKEY_UNUNIT64((*msgp
)->sadb_msg_len
);
6506 * set the pointer to each header into message buffer.
6507 * IN: msg: pointer to message buffer.
6508 * mhp: pointer to the buffer allocated like below:
6509 * caddr_t mhp[SADB_EXT_MAX + 1];
6515 struct sadb_msg
*msg
;
6518 struct sadb_ext
*ext
;
6523 if (msg
== NULL
|| mhp
== NULL
)
6524 panic("key_align: NULL pointer is passed.\n");
6527 for (i
= 0; i
< SADB_EXT_MAX
+ 1; i
++)
6530 mhp
[0] = (caddr_t
)msg
;
6532 tlen
= PFKEY_UNUNIT64(msg
->sadb_msg_len
) - sizeof(struct sadb_msg
);
6533 ext
= (struct sadb_ext
*)((caddr_t
)msg
+ sizeof(struct sadb_msg
));
6537 switch (ext
->sadb_ext_type
) {
6539 case SADB_EXT_LIFETIME_CURRENT
:
6540 case SADB_EXT_LIFETIME_HARD
:
6541 case SADB_EXT_LIFETIME_SOFT
:
6542 case SADB_EXT_ADDRESS_SRC
:
6543 case SADB_EXT_ADDRESS_DST
:
6544 case SADB_EXT_ADDRESS_PROXY
:
6545 case SADB_EXT_KEY_AUTH
:
6546 case SADB_EXT_KEY_ENCRYPT
:
6547 case SADB_EXT_IDENTITY_SRC
:
6548 case SADB_EXT_IDENTITY_DST
:
6549 case SADB_EXT_SENSITIVITY
:
6550 case SADB_EXT_PROPOSAL
:
6551 case SADB_EXT_SUPPORTED_AUTH
:
6552 case SADB_EXT_SUPPORTED_ENCRYPT
:
6553 case SADB_EXT_SPIRANGE
:
6554 case SADB_X_EXT_POLICY
:
6555 /* duplicate check */
6557 * XXX Are there duplication payloads of either
6558 * KEY_AUTH or KEY_ENCRYPT ?
6560 if (mhp
[ext
->sadb_ext_type
] != NULL
) {
6562 printf("key_align: duplicate ext_type %u "
6564 ext
->sadb_ext_type
);
6566 pfkeystat
.out_dupext
++;
6569 mhp
[ext
->sadb_ext_type
] = (caddr_t
)ext
;
6573 printf("key_align: invalid ext_type %u is passed.\n",
6574 ext
->sadb_ext_type
);
6576 pfkeystat
.out_invexttype
++;
6580 extlen
= PFKEY_UNUNIT64(ext
->sadb_ext_len
);
6582 ext
= (struct sadb_ext
*)((caddr_t
)ext
+ extlen
);
6593 bzero((caddr_t
)&key_cb
, sizeof(key_cb
));
6595 for (i
= 0; i
< IPSEC_DIR_MAX
; i
++) {
6596 LIST_INIT(&sptree
[i
]);
6599 LIST_INIT(&sahtree
);
6601 for (i
= 0; i
<= SADB_SATYPE_MAX
; i
++) {
6602 LIST_INIT(®tree
[i
]);
6605 #ifndef IPSEC_NONBLOCK_ACQUIRE
6606 LIST_INIT(&acqtree
);
6608 LIST_INIT(&spacqtree
);
6610 /* system default */
6611 ip4_def_policy
.policy
= IPSEC_POLICY_NONE
;
6612 ip4_def_policy
.refcnt
++; /*never reclaim this*/
6614 ip6_def_policy
.policy
= IPSEC_POLICY_NONE
;
6615 ip6_def_policy
.refcnt
++; /*never reclaim this*/
6618 #ifndef IPSEC_DEBUG2
6619 timeout((void *)key_timehandler
, (void *)0, hz
);
6620 #endif /*IPSEC_DEBUG2*/
6622 /* initialize key statistics */
6623 keystat
.getspi_count
= 1;
6625 printf("IPsec: Initialized Security Association Processing.\n");
6631 * XXX: maybe This function is called after INBOUND IPsec processing.
6633 * Special check for tunnel-mode packets.
6634 * We must make some checks for consistency between inner and outer IP header.
6636 * xxx more checks to be provided
6639 key_checktunnelsanity(sav
, family
, src
, dst
)
6640 struct secasvar
*sav
;
6646 if (sav
->sah
== NULL
)
6647 panic("sav->sah == NULL at key_checktunnelsanity");
6649 /* XXX: check inner IP header */
6656 #define hostnamelen strlen(hostname)
6660 * Get FQDN for the host.
6661 * If the administrator configured hostname (by hostname(1)) without
6662 * domain name, returns nothing.
6669 static char fqdn
[MAXHOSTNAMELEN
+ 1];
6674 /* check if it comes with domain name. */
6676 for (i
= 0; i
< hostnamelen
; i
++) {
6677 if (hostname
[i
] == '.')
6683 /* NOTE: hostname may not be NUL-terminated. */
6684 bzero(fqdn
, sizeof(fqdn
));
6685 bcopy(hostname
, fqdn
, hostnamelen
);
6686 fqdn
[hostnamelen
] = '\0';
6691 * get username@FQDN for the host/user.
6697 static char userfqdn
[MAXHOSTNAMELEN
+ MAXLOGNAME
+ 2];
6698 struct proc
*p
= curproc
;
6701 if (!p
|| !p
->p_pgrp
|| !p
->p_pgrp
->pg_session
)
6703 if (!(host
= key_getfqdn()))
6706 /* NOTE: s_login may not be-NUL terminated. */
6707 bzero(userfqdn
, sizeof(userfqdn
));
6708 bcopy(p
->p_pgrp
->pg_session
->s_login
, userfqdn
, MAXLOGNAME
);
6709 userfqdn
[MAXLOGNAME
] = '\0'; /* safeguard */
6710 q
= userfqdn
+ strlen(userfqdn
);
6712 bcopy(host
, q
, strlen(host
));
6720 /* record data transfer on SA, and update timestamps */
6722 key_sa_recordxfer(sav
, m
)
6723 struct secasvar
*sav
;
6727 panic("key_sa_recordxfer called with sav == NULL");
6729 panic("key_sa_recordxfer called with m == NULL");
6734 * XXX Currently, there is a difference of bytes size
6735 * between inbound and outbound processing.
6737 sav
->lft_c
->sadb_lifetime_bytes
+= m
->m_pkthdr
.len
;
6738 /* to check bytes lifetime is done in key_timehandler(). */
6741 * We use the number of packets as the unit of
6742 * sadb_lifetime_allocations. We increment the variable
6743 * whenever {esp,ah}_{in,out}put is called.
6745 sav
->lft_c
->sadb_lifetime_allocations
++;
6746 /* XXX check for expires? */
6749 * NOTE: We record CURRENT sadb_lifetime_usetime by using wall clock,
6750 * in seconds. HARD and SOFT lifetime are measured by the time
6751 * difference (again in seconds) from sadb_lifetime_usetime.
6755 * -----+-----+--------+---> t
6756 * <--------------> HARD
6762 sav
->lft_c
->sadb_lifetime_usetime
= tv
.tv_sec
;
6763 /* XXX check for expires? */
6771 key_sa_routechange(dst
)
6772 struct sockaddr
*dst
;
6774 struct secashead
*sah
;
6777 LIST_FOREACH(sah
, &sahtree
, chain
) {
6778 ro
= &sah
->sa_route
;
6779 if (ro
->ro_rt
&& dst
->sa_len
== ro
->ro_dst
.sa_len
6780 && bcmp(dst
, &ro
->ro_dst
, dst
->sa_len
) == 0) {
6782 ro
->ro_rt
= (struct rtentry
*)NULL
;
6790 key_sa_chgstate(sav
, state
)
6791 struct secasvar
*sav
;
6795 panic("key_sa_chgstate called with sav == NULL");
6797 if (sav
->state
== state
)
6800 if (__LIST_CHAINED(sav
))
6801 LIST_REMOVE(sav
, chain
);
6804 LIST_INSERT_HEAD(&sav
->sah
->savtree
[state
], sav
, chain
);
6807 /* returns NULL on error, m0 will be left unchanged */
6809 key_appendmbuf(m0
, len
)
6817 if (!m0
|| (m0
->m_flags
& M_PKTHDR
) == 0)
6818 return NULL
; /*EINVAL*/
6820 return NULL
; /*EINVAL*/
6822 for (m
= m0
; m
&& m
->m_next
; m
= m
->m_next
)
6824 if (len
<= M_TRAILINGSPACE(m
)) {
6825 p
= mtod(m
, caddr_t
) + m
->m_len
;
6827 m0
->m_pkthdr
.len
+= len
;
6831 MGET(n
, M_DONTWAIT
, m
->m_type
);
6833 MCLGET(n
, M_DONTWAIT
);
6834 if ((n
->m_flags
& M_EXT
) == 0) {
6840 return NULL
; /*ENOBUFS*/
6844 m0
->m_pkthdr
.len
+= len
;
6846 return mtod(n
, caddr_t
);
6850 #include <sys/user.h>
6851 #include <sys/sysctl.h>
6853 int *key_sysvars
[] = KEYCTL_VARS
;
6856 key_sysctl(name
, namelen
, oldp
, oldlenp
, newp
, newlen
)
6864 if (name
[0] >= KEYCTL_MAXID
)
6868 return sysctl_int_arr(key_sysvars
, name
, namelen
,
6869 oldp
, oldlenp
, newp
, newlen
);
6876 #include <sys/sysctl.h>
6878 static int *key_sysvars
[] = KEYCTL_VARS
;
6881 key_sysctl(name
, namelen
, oldp
, oldlenp
, newp
, newlen
)
6889 if (name
[0] >= KEYCTL_MAXID
)
6891 if (!key_sysvars
[name
[0]])
6895 return sysctl_int(oldp
, oldlenp
, newp
, newlen
,
6896 key_sysvars
[name
[0]]);
6899 #endif /*__NetBSD__*/