1 /* $NetBSD: handler.h,v 1.9 2006/09/09 16:22:09 manu Exp $ */
3 /* Id: handler.h,v 1.19 2006/02/25 08:25:12 manubsd Exp */
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #include <sys/queue.h>
41 #include <openssl/rsa.h>
46 #include "isakmp_var.h"
49 #include <Security/SecDH.h>
52 typedef struct ike_session ike_session_t
;
59 * 1 start start (1st msg received)
60 * 2 (---) 1st valid msg received
61 * 3 1st msg sent 1st msg sent
62 * 4 1st valid msg received 2st valid msg received
63 * 5 2nd msg sent 2nd msg sent
64 * 6 2nd valid msg received 3rd valid msg received
65 * 7 3rd msg sent 3rd msg sent
66 * 8 3rd valid msg received (---)
67 * 9 SA established SA established
72 * 1 start start (1st msg received)
73 * 2 (---) 1st valid msg received
74 * 3 1st msg sent 1st msg sent
75 * 4 1st valid msg received 2st valid msg received
80 * 9 SA established SA established
85 * 1 start start (1st msg received)
86 * 2 (---) 1st valid msg received
87 * 3 1st msg sent 1st msg sent
88 * 4 1st valid msg received 2st valid msg received
89 * 5 2nd msg sent (---)
93 * 9 SA established SA established
95 #define PHASE1ST_SPAWN 0
96 #define PHASE1ST_START 1
97 #define PHASE1ST_MSG1RECEIVED 2
98 #define PHASE1ST_MSG1SENT 3
99 #define PHASE1ST_MSG2RECEIVED 4
100 #define PHASE1ST_MSG2SENT 5
101 #define PHASE1ST_MSG3RECEIVED 6
102 #define PHASE1ST_MSG3SENT 7
103 #define PHASE1ST_MSG4RECEIVED 8
104 #define PHASE1ST_ESTABLISHED 9
105 #define PHASE1ST_EXPIRED 10
106 #define PHASE1ST_MAX 11
108 /* About address semantics in each case.
109 * initiator(addr=I) responder(addr=R)
111 * (local) (remote) (local) (remote)
112 * phase 1 handler I R R I
113 * phase 2 handler I R R I
119 struct isakmp_cfg_state
;
124 int status
; /* status of this SA */
125 int side
; /* INITIATOR or RESPONDER */
126 int started_by_api
; /* connection started by VPNControl API */
128 struct sockaddr
*remote
; /* remote address to negosiate ph1 */
129 struct sockaddr
*local
; /* local address to negosiate ph1 */
130 /* XXX copy from rmconf due to anonymous configuration.
131 * If anonymous will be forbidden, we do delete them. */
133 struct remoteconf
*rmconf
; /* pointer to remote configuration */
135 struct isakmpsa
*approval
; /* pointer to SA(s) approved. */
136 vchar_t
*authstr
; /* place holder of string for auth. */
137 /* for example pre-shared key */
139 u_int8_t version
; /* ISAKMP version */
140 u_int8_t etype
; /* Exchange type actually for use */
141 u_int8_t flags
; /* Flags */
142 u_int32_t msgid
; /* message id */
145 struct ph1natt_options
*natt_options
; /* Selected NAT-T IKE version */
146 u_int32_t natt_flags
; /* NAT-T related flags */
149 int frag
; /* IKE phase 1 fragmentation */
150 struct isakmp_frag_item
*frag_chain
; /* Received fragments */
153 struct sched
*sce
; /* schedule for expire */
154 struct sched
*sce_rekey
; /* schedule for rekey */
156 struct sched
*scr
; /* schedule for resend */
157 int retry_counter
; /* for resend. */
158 vchar_t
*sendbuf
; /* buffer for re-sending */
161 SecDHContext dhC
; /* Context for Security Framework Diffie-Hellman calculations */
162 size_t publicKeySize
;
164 vchar_t
*dhpriv
; /* DH; private value */
165 vchar_t
*dhpub
; /* DH; public value */
166 vchar_t
*dhpub_p
; /* DH; partner's public value */
167 vchar_t
*dhgxy
; /* DH; shared secret */
168 vchar_t
*nonce
; /* nonce value */
169 vchar_t
*nonce_p
; /* partner's nonce value */
170 vchar_t
*skeyid
; /* SKEYID */
171 vchar_t
*skeyid_d
; /* SKEYID_d */
172 vchar_t
*skeyid_a
; /* SKEYID_a, i.e. hash */
173 vchar_t
*skeyid_e
; /* SKEYID_e, i.e. encryption */
174 vchar_t
*key
; /* cipher key */
175 vchar_t
*hash
; /* HASH minus general header */
176 vchar_t
*sig
; /* SIG minus general header */
177 vchar_t
*sig_p
; /* peer's SIG minus general header */
178 cert_t
*cert
; /* CERT minus general header */
179 cert_t
*cert_p
; /* peer's CERT minus general header */
180 cert_t
*crl_p
; /* peer's CRL minus general header */
181 cert_t
*cr_p
; /* peer's CR not including general */
183 RSA
*rsa
; /* my RSA key */
184 RSA
*rsa_p
; /* peer's RSA key */
185 struct genlist
*rsa_candidates
; /* possible candidates for peer's RSA key */
187 vchar_t
*id
; /* ID minus gen header */
188 vchar_t
*id_p
; /* partner's ID minus general header */
189 /* i.e. struct ipsecdoi_id_b*. */
190 struct isakmp_ivm
*ivm
; /* IVs */
192 vchar_t
*sa
; /* whole SA payload to send/to be sent*/
193 /* to calculate HASH */
194 /* NOT INCLUDING general header. */
196 vchar_t
*sa_ret
; /* SA payload to reply/to be replyed */
197 /* NOT INCLUDING general header. */
198 /* NOTE: Should be release after use. */
201 void *gssapi_state
; /* GSS-API specific state. */
202 /* Allocated when needed */
203 vchar_t
*gi_i
; /* optional initiator GSS id */
204 vchar_t
*gi_r
; /* optional responder GSS id */
207 struct isakmp_pl_hash
*pl_hash
; /* pointer to hash payload */
209 time_t created
; /* timestamp for establish */
211 struct timeval start
;
216 int dpd_support
; /* Does remote supports DPD ? */
217 time_t dpd_lastack
; /* Last ack received */
218 u_int16_t dpd_seq
; /* DPD seq number to receive */
219 u_int8_t dpd_fails
; /* number of failures */
220 u_int8_t peer_sent_ike
;
221 struct sched
*dpd_r_u
;
224 #ifdef ENABLE_VPNCONTROL_PORT
225 struct sched
*ping_sched
; /* for sending pings to keep FW open */
228 u_int32_t msgid2
; /* msgid counter for Phase 2 */
229 int ph2cnt
; /* the number which is negotiated by this phase 1 */
230 LIST_HEAD(_ph2ofph1_
, ph2handle
) ph2tree
;
232 LIST_ENTRY(ph1handle
) chain
;
234 struct isakmp_cfg_state
*mode_cfg
; /* ISAKMP mode config state */
235 u_int8_t pended_xauth_id
; /* saved id for reply from vpn control socket */
236 u_int8_t xauth_awaiting_userinput
; /* indicates we are waiting for user input */
237 vchar_t
*xauth_awaiting_userinput_msg
; /* tracks the last packet that triggered XAUTH */
241 ike_session_t
*parent_session
;
242 LIST_ENTRY(ph1handle
) ph1ofsession_chain
;
245 /* Phase 2 handler */
246 /* allocated per a SA or SA bundles of a pair of peer's IP addresses. */
248 * initiator responder
250 * 1 start start (1st msg received)
251 * 2 acquire msg get 1st valid msg received
252 * 3 getspi request sent getspi request sent
253 * 4 getspi done getspi done
254 * 5 1st msg sent 1st msg sent
255 * 6 1st valid msg received 2nd valid msg received
256 * 7 (commit bit) (commit bit)
257 * 8 SAs added SAs added
258 * 9 SAs established SAs established
259 * 10 SAs expired SAs expired
261 #define PHASE2ST_SPAWN 0
262 #define PHASE2ST_START 1
263 #define PHASE2ST_STATUS2 2
264 #define PHASE2ST_GETSPISENT 3
265 #define PHASE2ST_GETSPIDONE 4
266 #define PHASE2ST_MSG1SENT 5
267 #define PHASE2ST_STATUS6 6
268 #define PHASE2ST_COMMIT 7
269 #define PHASE2ST_ADDSA 8
270 #define PHASE2ST_ESTABLISHED 9
271 #define PHASE2ST_EXPIRED 10
272 #define PHASE2ST_MAX 11
275 struct sockaddr
*src
; /* my address of SA. */
276 struct sockaddr
*dst
; /* peer's address of SA. */
279 * copy ip address from ID payloads when ID type is ip address.
280 * In other case, they must be null.
282 struct sockaddr
*src_id
;
283 struct sockaddr
*dst_id
;
285 u_int32_t spid
; /* policy id by kernel */
287 int status
; /* ipsec sa status */
288 u_int8_t side
; /* INITIATOR or RESPONDER */
290 struct sched
*sce
; /* schedule for expire */
291 struct sched
*scr
; /* schedule for resend */
292 int retry_counter
; /* for resend. */
293 vchar_t
*sendbuf
; /* buffer for re-sending */
294 vchar_t
*msg1
; /* buffer for re-sending */
295 /* used for responder's first message */
297 int retry_checkph1
; /* counter to wait phase 1 finished. */
298 /* NOTE: actually it's timer. */
300 u_int32_t seq
; /* sequence number used by PF_KEY */
302 * NOTE: In responder side, we can't identify each SAs
303 * with same destination address for example, when
304 * socket based SA is required. So we set a identifier
305 * number to "seq", and sent kernel by pfkey.
307 u_int8_t satype
; /* satype in PF_KEY */
309 * saved satype in the original PF_KEY request from
310 * the kernel in order to reply a error.
313 u_int8_t flags
; /* Flags for phase 2 */
314 u_int32_t msgid
; /* msgid for phase 2 */
316 struct sainfo
*sainfo
; /* place holder of sainfo */
317 struct saprop
*proposal
; /* SA(s) proposal. */
318 struct saprop
*approval
; /* SA(s) approved. */
319 caddr_t spidx_gen
; /* policy from peer's proposal */
322 SecDHContext dhC
; /* Context for Security Framework Diffie-Hellman calculations */
323 size_t publicKeySize
;
325 struct dhgroup
*pfsgrp
; /* DH; prime number */
326 vchar_t
*dhpriv
; /* DH; private value */
327 vchar_t
*dhpub
; /* DH; public value */
328 vchar_t
*dhpub_p
; /* DH; partner's public value */
329 vchar_t
*dhgxy
; /* DH; shared secret */
330 vchar_t
*id
; /* ID minus gen header */
331 vchar_t
*id_p
; /* peer's ID minus general header */
332 vchar_t
*nonce
; /* nonce value in phase 2 */
333 vchar_t
*nonce_p
; /* partner's nonce value in phase 2 */
335 vchar_t
*sa
; /* whole SA payload to send/to be sent*/
336 /* to calculate HASH */
337 /* NOT INCLUDING general header. */
339 vchar_t
*sa_ret
; /* SA payload to reply/to be replyed */
340 /* NOT INCLUDING general header. */
341 /* NOTE: Should be release after use. */
343 struct isakmp_ivm
*ivm
; /* IVs */
345 int generated_spidx
; /* mark handlers whith generated policy */
348 struct timeval start
;
351 struct ph1handle
*ph1
; /* back pointer to isakmp status */
354 ike_session_t
*parent_session
;
355 LIST_ENTRY(ph2handle
) ph2ofsession_chain
;
357 vchar_t
*ext_nat_id_p
;
359 LIST_ENTRY(ph2handle
) chain
;
360 LIST_ENTRY(ph2handle
) ph1bind
; /* chain to ph1handle */
364 * for handling initial contact.
367 struct sockaddr
*remote
; /* remote address to negotiate ph1 */
368 LIST_ENTRY(contacted
) chain
;
372 * for checking if a packet is retransmited.
375 struct sockaddr
*remote
; /* the remote address */
376 struct sockaddr
*local
; /* the local address */
377 vchar_t
*hash
; /* hash of the received packet */
378 vchar_t
*sendbuf
; /* buffer for the response */
379 int retry_counter
; /* how many times to send */
380 time_t time_send
; /* timestamp to send a packet */
381 time_t created
; /* timestamp to create a queue */
382 time_t retry_interval
;
384 u_int32_t frag_flags
; /* IKE phase 1 fragmentation */
387 struct sched
*scr
; /* schedule for resend, may not used */
389 LIST_ENTRY(recvdpkt
) chain
;
392 /* for parsing ISAKMP header. */
393 struct isakmp_parse_t
{
394 u_char type
; /* payload type of mine */
395 int len
; /* ntohs(ptr->len) */
396 struct isakmp_gen
*ptr
;
403 * initiator responder
404 * ------------------------- --------------------------
405 * initialize iv(A), ive(A). initialize iv(A), ive(A).
407 * save to iv(B). ---[packet(B)]--> save to ive(B).
409 * packet consistency.
410 * sync iv(B) with ive(B).
411 * check auth, integrity.
413 * save to ive(C). <--[packet(C)]--- save to iv(C).
417 * - In the case that a error is found while cipher processing,
418 * initiator responder
419 * ------------------------- --------------------------
420 * initialize iv(A), ive(A). initialize iv(A), ive(A).
422 * save to iv(B). ---[packet(B)]--> save to ive(B).
424 * packet consistency.
425 * sync iv(B) with ive(B).
426 * check auth, integrity.
429 * get ive2(X) from iv(B).
431 * get iv2(X) from iv(B). <--[packet(Y)]--- save to iv2(Y).
436 * The reason why the responder synchronizes iv with ive after checking the
437 * packet consistency is that it is required to leave the IV for decoding
438 * packet. Because there is a potential of error while checking the packet
439 * consistency. Also the reason why that is before authentication and
440 * integirty check is that the IV for informational exchange has to be made
441 * by the IV which is after packet decoded and checking the packet consistency.
442 * Otherwise IV mismatched happens between the intitiator and the responder.
445 vchar_t
*iv
; /* for decoding packet */
446 /* if phase 1, it's for computing phase2 iv */
447 vchar_t
*ive
; /* for encoding packet */
455 struct sockaddr_storage remote
;
456 struct sockaddr_storage local
;
468 extern struct ph1handle
*getph1byindex
__P((isakmp_index
*));
469 extern struct ph1handle
*getph1byindex0
__P((isakmp_index
*));
470 extern struct ph1handle
*getph1byaddr
__P((struct sockaddr
*,
472 extern struct ph1handle
*getph1byaddrwop
__P((struct sockaddr
*,
474 extern struct ph1handle
*getph1bydstaddrwop
__P((struct sockaddr
*));
475 extern int islast_ph1
__P((struct ph1handle
*));
476 struct ph1handle
*ph1
;
478 struct ph1handle
*getph1bylogin
__P((char *));
479 int purgeph1bylogin
__P((char *));
481 extern int purgephXbydstaddrwop
__P((struct sockaddr
*));
482 extern void purgephXbyspid
__P((u_int32_t
, int));
484 extern vchar_t
*dumpph1
__P((void));
485 extern struct ph1handle
*newph1
__P((void));
486 extern void delph1
__P((struct ph1handle
*));
487 extern int insph1
__P((struct ph1handle
*));
488 extern void remph1
__P((struct ph1handle
*));
489 extern void flushph1
__P((int));
490 extern void initph1tree
__P((void));
492 extern struct ph2handle
*getph2byspidx
__P((struct policyindex
*));
493 extern struct ph2handle
*getph2byspid
__P((u_int32_t
));
494 extern struct ph2handle
*getph2byseq
__P((u_int32_t
));
495 extern struct ph2handle
*getph2bysaddr
__P((struct sockaddr
*,
497 extern struct ph2handle
*getph2bymsgid
__P((struct ph1handle
*, u_int32_t
));
498 extern struct ph2handle
*getph2byid
__P((struct sockaddr
*,
499 struct sockaddr
*, u_int32_t
));
500 extern struct ph2handle
*getph2bysaidx
__P((struct sockaddr
*,
501 struct sockaddr
*, u_int
, u_int32_t
));
502 extern struct ph2handle
*newph2
__P((void));
503 extern void initph2
__P((struct ph2handle
*));
504 extern void delph2
__P((struct ph2handle
*));
505 extern int insph2
__P((struct ph2handle
*));
506 extern void remph2
__P((struct ph2handle
*));
507 extern void flushph2
__P((int));
508 extern void deleteallph2
__P((struct sockaddr
*, struct sockaddr
*, u_int
));
509 extern void deleteallph1
__P((struct sockaddr
*, struct sockaddr
*));
510 extern void initph2tree
__P((void));
512 extern void bindph12
__P((struct ph1handle
*, struct ph2handle
*));
513 extern void unbindph12
__P((struct ph2handle
*));
514 extern void rebindph12
__P((struct ph1handle
*, struct ph2handle
*));
516 extern struct contacted
*getcontacted
__P((struct sockaddr
*));
517 extern int inscontacted
__P((struct sockaddr
*));
518 extern void clear_contacted
__P((void));
519 extern void initctdtree
__P((void));
521 extern time_t get_exp_retx_interval
__P((int num_retries
, int fixed_retry_interval
));
523 extern int check_recvdpkt
__P((struct sockaddr
*,
524 struct sockaddr
*, vchar_t
*));
525 extern int add_recvdpkt
__P((struct sockaddr
*, struct sockaddr
*,
526 vchar_t
*, vchar_t
*, size_t, u_int32_t
));
527 extern void clear_recvdpkt
__P((void));
528 extern void init_recvdpkt
__P((void));
531 extern int exclude_cfg_addr
__P((const struct sockaddr
*));
535 extern int ph1_force_dpd
__P((struct sockaddr
*));
537 extern void sweep_sleepwake
__P((void));
539 #endif /* _HANDLER_H */