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_storage
*remote
; /* remote address to negosiate ph1 */
129 struct sockaddr_storage
*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 */
182 vchar_t
*id
; /* ID minus gen header */
183 vchar_t
*id_p
; /* partner's ID minus general header */
184 /* i.e. struct ipsecdoi_id_b*. */
185 struct isakmp_ivm
*ivm
; /* IVs */
187 vchar_t
*sa
; /* whole SA payload to send/to be sent*/
188 /* to calculate HASH */
189 /* NOT INCLUDING general header. */
191 vchar_t
*sa_ret
; /* SA payload to reply/to be replyed */
192 /* NOT INCLUDING general header. */
193 /* NOTE: Should be release after use. */
196 void *gssapi_state
; /* GSS-API specific state. */
197 /* Allocated when needed */
198 vchar_t
*gi_i
; /* optional initiator GSS id */
199 vchar_t
*gi_r
; /* optional responder GSS id */
202 struct isakmp_pl_hash
*pl_hash
; /* pointer to hash payload */
204 time_t created
; /* timestamp for establish */
206 struct timeval start
;
211 int dpd_support
; /* Does remote supports DPD ? */
212 time_t dpd_lastack
; /* Last ack received */
213 u_int16_t dpd_seq
; /* DPD seq number to receive */
214 u_int8_t dpd_fails
; /* number of failures */
215 u_int8_t peer_sent_ike
;
216 struct sched
*dpd_r_u
;
219 #ifdef ENABLE_VPNCONTROL_PORT
220 struct sched
*ping_sched
; /* for sending pings to keep FW open */
223 u_int32_t msgid2
; /* msgid counter for Phase 2 */
224 int ph2cnt
; /* the number which is negotiated by this phase 1 */
225 LIST_HEAD(_ph2ofph1_
, ph2handle
) ph2tree
;
227 LIST_ENTRY(ph1handle
) chain
;
229 struct isakmp_cfg_state
*mode_cfg
; /* ISAKMP mode config state */
230 u_int8_t pended_xauth_id
; /* saved id for reply from vpn control socket */
231 u_int8_t xauth_awaiting_userinput
; /* indicates we are waiting for user input */
232 vchar_t
*xauth_awaiting_userinput_msg
; /* tracks the last packet that triggered XAUTH */
236 ike_session_t
*parent_session
;
237 LIST_ENTRY(ph1handle
) ph1ofsession_chain
;
240 /* Phase 2 handler */
241 /* allocated per a SA or SA bundles of a pair of peer's IP addresses. */
243 * initiator responder
245 * 1 start start (1st msg received)
246 * 2 acquire msg get 1st valid msg received
247 * 3 getspi request sent getspi request sent
248 * 4 getspi done getspi done
249 * 5 1st msg sent 1st msg sent
250 * 6 1st valid msg received 2nd valid msg received
251 * 7 (commit bit) (commit bit)
252 * 8 SAs added SAs added
253 * 9 SAs established SAs established
254 * 10 SAs expired SAs expired
256 #define PHASE2ST_SPAWN 0
257 #define PHASE2ST_START 1
258 #define PHASE2ST_STATUS2 2
259 #define PHASE2ST_GETSPISENT 3
260 #define PHASE2ST_GETSPIDONE 4
261 #define PHASE2ST_MSG1SENT 5
262 #define PHASE2ST_STATUS6 6
263 #define PHASE2ST_COMMIT 7
264 #define PHASE2ST_ADDSA 8
265 #define PHASE2ST_ESTABLISHED 9
266 #define PHASE2ST_EXPIRED 10
267 #define PHASE2ST_MAX 11
270 struct sockaddr_storage
*src
; /* my address of SA. */
271 struct sockaddr_storage
*dst
; /* peer's address of SA. */
274 * copy ip address from ID payloads when ID type is ip address.
275 * In other case, they must be null.
277 struct sockaddr_storage
*src_id
;
278 struct sockaddr_storage
*dst_id
;
280 u_int32_t spid
; /* policy id by kernel */
282 int status
; /* ipsec sa status */
283 u_int8_t side
; /* INITIATOR or RESPONDER */
285 struct sched
*sce
; /* schedule for expire */
286 struct sched
*scr
; /* schedule for resend */
287 int retry_counter
; /* for resend. */
288 vchar_t
*sendbuf
; /* buffer for re-sending */
289 vchar_t
*msg1
; /* buffer for re-sending */
290 /* used for responder's first message */
292 int retry_checkph1
; /* counter to wait phase 1 finished. */
293 /* NOTE: actually it's timer. */
295 u_int32_t seq
; /* sequence number used by PF_KEY */
297 * NOTE: In responder side, we can't identify each SAs
298 * with same destination address for example, when
299 * socket based SA is required. So we set a identifier
300 * number to "seq", and sent kernel by pfkey.
302 u_int8_t satype
; /* satype in PF_KEY */
304 * saved satype in the original PF_KEY request from
305 * the kernel in order to reply a error.
308 u_int8_t flags
; /* Flags for phase 2 */
309 u_int32_t msgid
; /* msgid for phase 2 */
311 struct sainfo
*sainfo
; /* place holder of sainfo */
312 struct saprop
*proposal
; /* SA(s) proposal. */
313 struct saprop
*approval
; /* SA(s) approved. */
314 struct policyindex
* spidx_gen
; /* policy from peer's proposal */
317 SecDHContext dhC
; /* Context for Security Framework Diffie-Hellman calculations */
318 size_t publicKeySize
;
320 struct dhgroup
*pfsgrp
; /* DH; prime number */
321 vchar_t
*dhpriv
; /* DH; private value */
322 vchar_t
*dhpub
; /* DH; public value */
323 vchar_t
*dhpub_p
; /* DH; partner's public value */
324 vchar_t
*dhgxy
; /* DH; shared secret */
325 vchar_t
*id
; /* ID minus gen header */
326 vchar_t
*id_p
; /* peer's ID minus general header */
327 vchar_t
*nonce
; /* nonce value in phase 2 */
328 vchar_t
*nonce_p
; /* partner's nonce value in phase 2 */
330 vchar_t
*sa
; /* whole SA payload to send/to be sent*/
331 /* to calculate HASH */
332 /* NOT INCLUDING general header. */
334 vchar_t
*sa_ret
; /* SA payload to reply/to be replyed */
335 /* NOT INCLUDING general header. */
336 /* NOTE: Should be release after use. */
338 struct isakmp_ivm
*ivm
; /* IVs */
340 int generated_spidx
; /* mark handlers whith generated policy */
343 struct timeval start
;
346 struct ph1handle
*ph1
; /* back pointer to isakmp status */
349 ike_session_t
*parent_session
;
350 LIST_ENTRY(ph2handle
) ph2ofsession_chain
;
352 vchar_t
*ext_nat_id_p
;
354 LIST_ENTRY(ph2handle
) chain
;
355 LIST_ENTRY(ph2handle
) ph1bind
; /* chain to ph1handle */
359 * for handling initial contact.
362 struct sockaddr_storage
*remote
; /* remote address to negotiate ph1 */
363 LIST_ENTRY(contacted
) chain
;
367 * for checking if a packet is retransmited.
370 struct sockaddr_storage
*remote
; /* the remote address */
371 struct sockaddr_storage
*local
; /* the local address */
372 vchar_t
*hash
; /* hash of the received packet */
373 vchar_t
*sendbuf
; /* buffer for the response */
374 int retry_counter
; /* how many times to send */
375 time_t time_send
; /* timestamp to send a packet */
376 time_t created
; /* timestamp to create a queue */
377 time_t retry_interval
;
379 u_int32_t frag_flags
; /* IKE phase 1 fragmentation */
382 struct sched
*scr
; /* schedule for resend, may not used */
384 LIST_ENTRY(recvdpkt
) chain
;
387 /* for parsing ISAKMP header. */
388 struct isakmp_parse_t
{
389 u_char type
; /* payload type of mine */
390 int len
; /* ntohs(ptr->len) */
391 struct isakmp_gen
*ptr
;
398 * initiator responder
399 * ------------------------- --------------------------
400 * initialize iv(A), ive(A). initialize iv(A), ive(A).
402 * save to iv(B). ---[packet(B)]--> save to ive(B).
404 * packet consistency.
405 * sync iv(B) with ive(B).
406 * check auth, integrity.
408 * save to ive(C). <--[packet(C)]--- save to iv(C).
412 * - In the case that a error is found while cipher processing,
413 * initiator responder
414 * ------------------------- --------------------------
415 * initialize iv(A), ive(A). initialize iv(A), ive(A).
417 * save to iv(B). ---[packet(B)]--> save to ive(B).
419 * packet consistency.
420 * sync iv(B) with ive(B).
421 * check auth, integrity.
424 * get ive2(X) from iv(B).
426 * get iv2(X) from iv(B). <--[packet(Y)]--- save to iv2(Y).
431 * The reason why the responder synchronizes iv with ive after checking the
432 * packet consistency is that it is required to leave the IV for decoding
433 * packet. Because there is a potential of error while checking the packet
434 * consistency. Also the reason why that is before authentication and
435 * integirty check is that the IV for informational exchange has to be made
436 * by the IV which is after packet decoded and checking the packet consistency.
437 * Otherwise IV mismatched happens between the intitiator and the responder.
440 vchar_t
*iv
; /* for decoding packet */
441 /* if phase 1, it's for computing phase2 iv */
442 vchar_t
*ive
; /* for encoding packet */
450 struct sockaddr_storage remote
;
451 struct sockaddr_storage local
;
458 struct sockaddr_storage
;
463 extern struct ph1handle
*getph1byindex
__P((isakmp_index
*));
464 extern struct ph1handle
*getph1byindex0
__P((isakmp_index
*));
465 extern struct ph1handle
*getph1byaddr
__P((struct sockaddr_storage
*,
466 struct sockaddr_storage
*));
467 extern struct ph1handle
*getph1byaddrwop
__P((struct sockaddr_storage
*,
468 struct sockaddr_storage
*));
469 extern struct ph1handle
*getph1bydstaddrwop
__P((struct sockaddr_storage
*));
470 extern int islast_ph1
__P((struct ph1handle
*));
471 struct ph1handle
*ph1
;
473 struct ph1handle
*getph1bylogin
__P((char *));
474 int purgeph1bylogin
__P((char *));
476 extern int purgephXbydstaddrwop
__P((struct sockaddr_storage
*));
477 extern void purgephXbyspid
__P((u_int32_t
, int));
479 extern vchar_t
*dumpph1
__P((void));
480 extern struct ph1handle
*newph1
__P((void));
481 extern void delph1
__P((struct ph1handle
*));
482 extern int insph1
__P((struct ph1handle
*));
483 extern void remph1
__P((struct ph1handle
*));
484 extern void flushph1
__P((int));
485 extern void initph1tree
__P((void));
487 extern struct ph2handle
*getph2byspidx
__P((struct policyindex
*));
488 extern struct ph2handle
*getph2byspid
__P((u_int32_t
));
489 extern struct ph2handle
*getph2byseq
__P((u_int32_t
));
490 extern struct ph2handle
*getph2bysaddr
__P((struct sockaddr_storage
*,
491 struct sockaddr_storage
*));
492 extern struct ph2handle
*getph2bymsgid
__P((struct ph1handle
*, u_int32_t
));
493 extern struct ph2handle
*getph2byid
__P((struct sockaddr_storage
*,
494 struct sockaddr_storage
*, u_int32_t
));
495 extern struct ph2handle
*getph2bysaidx
__P((struct sockaddr_storage
*,
496 struct sockaddr_storage
*, u_int
, u_int32_t
));
497 extern struct ph2handle
*newph2
__P((void));
498 extern void initph2
__P((struct ph2handle
*));
499 extern void delph2
__P((struct ph2handle
*));
500 extern int insph2
__P((struct ph2handle
*));
501 extern void remph2
__P((struct ph2handle
*));
502 extern void flushph2
__P((int));
503 extern void deleteallph2
__P((struct sockaddr_storage
*, struct sockaddr_storage
*, u_int
));
504 extern void deleteallph1
__P((struct sockaddr_storage
*, struct sockaddr_storage
*));
505 extern void initph2tree
__P((void));
507 extern void bindph12
__P((struct ph1handle
*, struct ph2handle
*));
508 extern void unbindph12
__P((struct ph2handle
*));
509 extern void rebindph12
__P((struct ph1handle
*, struct ph2handle
*));
511 extern struct contacted
*getcontacted
__P((struct sockaddr_storage
*));
512 extern int inscontacted
__P((struct sockaddr_storage
*));
513 extern void clear_contacted
__P((void));
514 extern void initctdtree
__P((void));
516 extern time_t get_exp_retx_interval
__P((int num_retries
, int fixed_retry_interval
));
518 extern int check_recvdpkt
__P((struct sockaddr_storage
*,
519 struct sockaddr_storage
*, vchar_t
*));
520 extern int add_recvdpkt
__P((struct sockaddr_storage
*, struct sockaddr_storage
*,
521 vchar_t
*, vchar_t
*, size_t, u_int32_t
));
522 extern void clear_recvdpkt
__P((void));
523 extern void init_recvdpkt
__P((void));
526 extern int exclude_cfg_addr
__P((const struct sockaddr_storage
*));
530 extern int ph1_force_dpd
__P((struct sockaddr_storage
*));
532 extern void sweep_sleepwake
__P((void));
534 #endif /* _HANDLER_H */