]> git.saurik.com Git - apple/ipsec.git/blob - ipsec-tools/racoon/handler.h
2f4dd3127de98ed27d2d7070f0c79d915837f27c
[apple/ipsec.git] / ipsec-tools / racoon / handler.h
1 /* $NetBSD: handler.h,v 1.9 2006/09/09 16:22:09 manu Exp $ */
2
3 /* Id: handler.h,v 1.19 2006/02/25 08:25:12 manubsd Exp */
4
5 /*
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
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.
20 *
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
31 * SUCH DAMAGE.
32 */
33
34 #ifndef _HANDLER_H
35 #define _HANDLER_H
36
37 #include "config.h"
38 #include "racoon_types.h"
39
40 #include <sys/queue.h>
41 #ifdef HAVE_OPENSSL
42 #include <openssl/rsa.h>
43 #endif
44
45 #include <sys/time.h>
46
47 #include "isakmp_var.h"
48 #include "oakley.h"
49 #ifndef HAVE_OPENSSL
50 #include <Security/SecDH.h>
51 #endif
52 #include <sys/socket.h>
53
54 #include <schedule.h>
55
56
57 /* About address semantics in each case.
58 * initiator(addr=I) responder(addr=R)
59 * src dst src dst
60 * (local) (remote) (local) (remote)
61 * phase 1 handler I R R I
62 * phase 2 handler I R R I
63 * getspi msg R I I R
64 * acquire msg I R
65 * ID payload I R I R
66 */
67 #ifdef ENABLE_HYBRID
68 struct isakmp_cfg_state;
69 #endif
70
71 #define INVALID_MSGID 0xFFFFFFFF
72
73 //=======================================================================
74 // PHASE 1
75 //=======================================================================
76
77 struct phase1handle {
78 isakmp_index index;
79
80 int status; /* status of this SA */
81 int side; /* INITIATOR or RESPONDER */
82 int started_by_api; /* connection started by VPNControl API */
83
84 struct sockaddr_storage *remote; /* remote address to negosiate ph1 */
85 struct sockaddr_storage *local; /* local address to negosiate ph1 */
86 /* XXX copy from rmconf due to anonymous configuration.
87 * If anonymous will be forbidden, we do delete them. */
88
89 struct remoteconf *rmconf; /* pointer to remote configuration */
90
91 struct isakmpsa *approval; /* pointer to SA(s) approved. */
92 /* for example pre-shared key */
93
94 u_int8_t version; /* ISAKMP version */
95 u_int8_t etype; /* Exchange type actually for use */
96 u_int8_t flags; /* Flags */
97 u_int32_t msgid; /* message id */
98
99 #ifdef ENABLE_NATT
100 struct ph1natt_options *natt_options; /* Selected NAT-T IKE version */
101 u_int32_t natt_flags; /* NAT-T related flags */
102 #endif
103 #ifdef ENABLE_FRAG
104 int frag; /* IKE phase 1 fragmentation */
105 struct isakmp_frag_item *frag_chain; /* Received fragments */
106 #endif
107
108 schedule_ref sce; /* schedule for expire */
109 schedule_ref sce_rekey; /* schedule for rekey */
110
111 schedule_ref scr; /* schedule for resend */
112 int retry_counter; /* for resend. */
113 vchar_t *sendbuf; /* buffer for re-sending */
114
115 #ifndef HAVE_OPENSSL
116 SecDHContext dhC; /* Context for Security Framework Diffie-Hellman calculations */
117 size_t publicKeySize;
118 #endif
119 vchar_t *dhpriv; /* DH; private value */
120 vchar_t *dhpub; /* DH; public value */
121 vchar_t *dhpub_p; /* DH; partner's public value */
122 vchar_t *dhgxy; /* DH; shared secret */
123 vchar_t *nonce; /* nonce value */
124 vchar_t *nonce_p; /* partner's nonce value */
125 vchar_t *skeyid; /* SKEYID */
126 vchar_t *skeyid_d; /* SKEYID_d */
127 vchar_t *skeyid_a; /* SKEYID_a, i.e. integrity protection */
128 vchar_t *skeyid_a_p; /* SKEYID_a_p, i.e. integrity protection */
129 vchar_t *skeyid_e; /* SKEYID_e, i.e. encryption */
130 vchar_t *skeyid_e_p; /* peer's SKEYID_e, i.e. encryption */
131 vchar_t *skeyid_p; /* SKEYID_p, i.e. for IKEv2 */
132 vchar_t *skeyid_p_p; /* peer's SKEYID_p, i.e. for IKEv2 */
133 vchar_t *key; /* cipher key */
134 vchar_t *key_p; /* peer's cipher key */
135 vchar_t *hash; /* HASH minus general header */
136 vchar_t *sig; /* SIG minus general header */
137 vchar_t *sig_p; /* peer's SIG minus general header */
138 cert_t *cert; /* CERT minus general header */
139 cert_t *cert_p; /* peer's CERT minus general header */
140 cert_t *crl_p; /* peer's CRL minus general header */
141 cert_t *cr_p; /* peer's CR not including general */
142 vchar_t *id; /* ID minus gen header */
143 vchar_t *id_p; /* partner's ID minus general header */
144 /* i.e. struct ipsecdoi_id_b*. */
145 struct isakmp_ivm *ivm; /* IVs */
146
147 vchar_t *sa; /* whole SA payload to send/to be sent*/
148 /* to calculate HASH */
149 /* NOT INCLUDING general header. */
150
151 vchar_t *sa_ret; /* SA payload to reply/to be replyed */
152 /* NOT INCLUDING general header. */
153 /* NOTE: Should be release after use. */
154
155 struct isakmp_pl_hash *pl_hash; /* pointer to hash payload */
156
157 time_t created; /* timestamp for establish */
158 #ifdef ENABLE_STATS
159 struct timeval start;
160 struct timeval end;
161 #endif
162
163 #ifdef ENABLE_DPD
164 int dpd_support; /* Does remote supports DPD ? */
165 time_t dpd_lastack; /* Last ack received */
166 u_int16_t dpd_seq; /* DPD seq number to receive */
167 u_int8_t dpd_fails; /* number of failures */
168 u_int8_t peer_sent_ike;
169 schedule_ref dpd_r_u;
170 #endif
171
172 #ifdef ENABLE_VPNCONTROL_PORT
173 schedule_ref ping_sched; /* for sending pings to keep FW open */
174 #endif
175
176 u_int32_t msgid2; /* msgid counter for Phase 2 */
177 int ph2cnt; /* the number which is negotiated by this phase 1 */
178 #ifdef ENABLE_HYBRID
179 struct isakmp_cfg_state *mode_cfg; /* ISAKMP mode config state */
180 u_int8_t pended_xauth_id; /* saved id for reply from vpn control socket */
181 u_int8_t xauth_awaiting_userinput; /* indicates we are waiting for user input */
182 vchar_t *xauth_awaiting_userinput_msg; /* tracks the last packet that triggered XAUTH */
183 #endif
184 int is_rekey:1;
185 int is_dying:1;
186 ike_session_t *parent_session;
187 LIST_HEAD(_ph2ofph1_, phase2handle) bound_ph2tree;
188 LIST_ENTRY(phase1handle) ph1ofsession_chain;
189
190 };
191
192
193 #define PHASE2_TYPE_SA 0
194 #define PHASE2_TYPE_INFO 1
195 #define PHASE2_TYPE_CFG 2
196
197 //=======================================================================
198 // PHASE 2
199 //=======================================================================
200 struct phase2handle {
201 struct sockaddr_storage *src; /* my address of SA. */
202 struct sockaddr_storage *dst; /* peer's address of SA. */
203
204 /*
205 * copy ip address from ID payloads when ID type is ip address.
206 * In other case, they must be null.
207 */
208 struct sockaddr_storage *src_id;
209 struct sockaddr_storage *dst_id;
210
211 int phase2_type; /* what this phase2 struct is for - see defines for PHASE2_TYPE... */
212 u_int32_t spid; /* policy id by kernel */
213
214 int status; /* ipsec sa status */
215 u_int8_t side; /* INITIATOR or RESPONDER */
216 u_int8_t version; /* ISAKMP version */
217
218 schedule_ref sce; /* schedule for expire */
219 schedule_ref scr; /* schedule for resend */
220 int retry_counter; /* for resend. */
221 vchar_t *sendbuf; /* buffer for re-sending */
222 vchar_t *msg1; /* buffer for re-sending */
223 /* used for responder's first message */
224
225 int retry_checkph1; /* counter to wait phase 1 finished. */
226 /* NOTE: actually it's timer. */
227
228 u_int32_t seq; /* sequence number used by PF_KEY */
229 /*
230 * NOTE: In responder side, we can't identify each SAs
231 * with same destination address for example, when
232 * socket based SA is required. So we set a identifier
233 * number to "seq", and sent kernel by pfkey.
234 */
235 u_int8_t satype; /* satype in PF_KEY */
236 /*
237 * saved satype in the original PF_KEY request from
238 * the kernel in order to reply a error.
239 */
240
241 u_int8_t flags; /* Flags for phase 2 */
242 u_int32_t msgid; /* msgid for phase 2 */
243
244 struct sainfo *sainfo; /* place holder of sainfo */
245 struct saprop *proposal; /* SA(s) proposal. */
246 struct saprop *approval; /* SA(s) approved. */
247 struct policyindex * spidx_gen; /* policy from peer's proposal */
248
249 #ifndef HAVE_OPENSSL
250 SecDHContext dhC; /* Context for Security Framework Diffie-Hellman calculations */
251 size_t publicKeySize;
252 #endif
253 struct dhgroup *pfsgrp; /* DH; prime number */
254 vchar_t *dhpriv; /* DH; private value */
255 vchar_t *dhpub; /* DH; public value */
256 vchar_t *dhpub_p; /* DH; partner's public value */
257 vchar_t *dhgxy; /* DH; shared secret */
258 vchar_t *id; /* ID minus gen header */
259 vchar_t *id_p; /* peer's ID minus general header */
260 vchar_t *nonce; /* nonce value in phase 2 */
261 vchar_t *nonce_p; /* partner's nonce value in phase 2 */
262
263 vchar_t *sa; /* whole SA payload to send/to be sent*/
264 /* to calculate HASH */
265 /* NOT INCLUDING general header. */
266
267 vchar_t *sa_ret; /* SA payload to reply/to be replyed */
268 /* NOT INCLUDING general header. */
269 /* NOTE: Should be release after use. */
270
271 struct isakmp_ivm *ivm; /* IVs */
272
273 int generated_spidx; /* mark handlers whith generated policy */
274
275 #ifdef ENABLE_STATS
276 struct timeval start;
277 struct timeval end;
278 #endif
279 struct phase1handle *ph1; /* back pointer to isakmp status */
280 int is_rekey:1;
281 int is_dying:1;
282 ike_session_t *parent_session;
283 vchar_t *ext_nat_id;
284 vchar_t *ext_nat_id_p;
285 LIST_ENTRY(phase2handle) ph2ofsession_chain;
286 LIST_ENTRY(phase2handle) ph1bind_chain; /* chain to ph1handle */
287
288 };
289
290 /*
291 * for handling initial contact.
292 */
293 struct contacted {
294 struct sockaddr_storage *remote; /* remote address to negotiate ph1 */
295 LIST_ENTRY(contacted) chain;
296 };
297
298 /*
299 * for checking if a packet is retransmited.
300 */
301 struct recvdpkt {
302 struct sockaddr_storage *remote; /* the remote address */
303 struct sockaddr_storage *local; /* the local address */
304 vchar_t *hash; /* hash of the received packet */
305 vchar_t *sendbuf; /* buffer for the response */
306 int retry_counter; /* how many times to send */
307 time_t time_send; /* timestamp to send a packet */
308 time_t created; /* timestamp to create a queue */
309 time_t retry_interval;
310 #ifdef ENABLE_FRAG
311 u_int32_t frag_flags; /* IKE phase 1 fragmentation */
312 #endif
313
314 schedule_ref scr; /* schedule for resend, may not used */
315
316 LIST_ENTRY(recvdpkt) chain;
317 };
318
319 /* for parsing ISAKMP header. */
320 struct isakmp_parse_t {
321 u_char type; /* payload type of mine */
322 int len; /* ntohs(ptr->len) */
323 struct isakmp_gen *ptr;
324 };
325
326 /*
327 * for IV management.
328 *
329 * - normal case
330 * initiator responder
331 * ------------------------- --------------------------
332 * initialize iv(A), ive(A). initialize iv(A), ive(A).
333 * encode by ive(A).
334 * save to iv(B). ---[packet(B)]--> save to ive(B).
335 * decode by iv(A).
336 * packet consistency.
337 * sync iv(B) with ive(B).
338 * check auth, integrity.
339 * encode by ive(B).
340 * save to ive(C). <--[packet(C)]--- save to iv(C).
341 * decoded by iv(B).
342 * :
343 *
344 * - In the case that a error is found while cipher processing,
345 * initiator responder
346 * ------------------------- --------------------------
347 * initialize iv(A), ive(A). initialize iv(A), ive(A).
348 * encode by ive(A).
349 * save to iv(B). ---[packet(B)]--> save to ive(B).
350 * decode by iv(A).
351 * packet consistency.
352 * sync iv(B) with ive(B).
353 * check auth, integrity.
354 * error found.
355 * create notify.
356 * get ive2(X) from iv(B).
357 * encode by ive2(X).
358 * get iv2(X) from iv(B). <--[packet(Y)]--- save to iv2(Y).
359 * save to ive2(Y).
360 * decoded by iv2(X).
361 * :
362 *
363 * The reason why the responder synchronizes iv with ive after checking the
364 * packet consistency is that it is required to leave the IV for decoding
365 * packet. Because there is a potential of error while checking the packet
366 * consistency. Also the reason why that is before authentication and
367 * integirty check is that the IV for informational exchange has to be made
368 * by the IV which is after packet decoded and checking the packet consistency.
369 * Otherwise IV mismatched happens between the intitiator and the responder.
370 */
371 struct isakmp_ivm {
372 vchar_t *iv; /* for decoding packet */
373 /* if phase 1, it's for computing phase2 iv */
374 vchar_t *ive; /* for encoding packet */
375 };
376
377 /* for dumping */
378 struct ph1dump {
379 isakmp_index index;
380 int status;
381 int side;
382 struct sockaddr_storage remote;
383 struct sockaddr_storage local;
384 u_int8_t version;
385 u_int8_t etype;
386 time_t created;
387 int ph2cnt;
388 };
389
390 struct sockaddr_storage;
391 struct policyindex;
392
393 extern int ike_session_check_recvdpkt (struct sockaddr_storage *, struct sockaddr_storage *, vchar_t *);
394
395 extern void ike_session_flush_all_phase1_for_session(ike_session_t *, int);
396 extern void ike_session_flush_all_phase1 (int);
397
398 extern phase1_handle_t *ike_session_getph1byindex (ike_session_t *, isakmp_index *);
399 extern phase1_handle_t *ike_session_getph1byindex0 (ike_session_t *, isakmp_index *);
400 extern phase1_handle_t *ike_session_getph1byaddr (ike_session_t *, struct sockaddr_storage *,
401 struct sockaddr_storage *);
402 extern phase1_handle_t *ike_session_getph1byaddrwop (ike_session_t *, struct sockaddr_storage *,
403 struct sockaddr_storage *);
404 extern phase1_handle_t *ike_session_getph1bydstaddrwop (ike_session_t *, struct sockaddr_storage *);
405 extern int ike_session_islast_ph1 (phase1_handle_t *);
406
407 extern int ike_session_expire_session(ike_session_t *session);
408 extern int ike_session_purgephXbydstaddrwop (struct sockaddr_storage *);
409 extern void ike_session_purgephXbyspid (u_int32_t, int);
410
411 extern phase1_handle_t *ike_session_newph1 (unsigned int);
412 extern void ike_session_delph1 (phase1_handle_t *);
413
414 extern phase2_handle_t *ike_session_getph2byspidx (ike_session_t *, struct policyindex *);
415 extern phase2_handle_t *ike_session_getph2byspid (u_int32_t);
416 extern phase2_handle_t *ike_session_getph2byseq (u_int32_t);
417 //extern phase2_handle_t *ike_session_getph2bysaddr (struct sockaddr_storage *, struct sockaddr_storage *);
418 extern phase2_handle_t *ike_session_getph2bymsgid (phase1_handle_t *, u_int32_t);
419 extern phase2_handle_t *ike_session_getonlyph2(phase1_handle_t *iph1);
420 extern phase2_handle_t *ike_session_getph2byid (struct sockaddr_storage *, struct sockaddr_storage *, u_int32_t);
421 extern phase2_handle_t *ike_session_getph2bysaidx (struct sockaddr_storage *, struct sockaddr_storage *, u_int, u_int32_t);
422 extern phase2_handle_t *ike_session_getph2bysaidx2(struct sockaddr_storage *src, struct sockaddr_storage *dst, u_int proto_id, u_int32_t spi, u_int32_t *opposite_spi);
423 extern phase2_handle_t *ike_session_newph2 (unsigned int, int);
424 extern void ike_session_initph2 (phase2_handle_t *);
425 extern void ike_session_delph2 (phase2_handle_t *);
426 extern void ike_session_flush_all_phase2_for_session(ike_session_t *, int);
427 extern void ike_session_flush_all_phase2 (int);
428 extern void ike_session_deleteallph2 (struct sockaddr_storage *, struct sockaddr_storage *, u_int);
429 extern void ike_session_deleteallph1 (struct sockaddr_storage *, struct sockaddr_storage *);
430
431 #ifdef ENABLE_DPD
432 extern int ike_session_ph1_force_dpd (struct sockaddr_storage *);
433 #endif
434
435 //%%%%%%%%%%% don't know where the following will go yet - all these below could change
436 extern struct contacted *ike_session_getcontacted (struct sockaddr_storage *);
437 extern int ike_session_inscontacted (struct sockaddr_storage *);
438 extern void ike_session_clear_contacted (void);
439 extern void ike_session_initctdtree (void);
440
441 extern time_t ike_session_get_exp_retx_interval (int num_retries, int fixed_retry_interval);
442
443 extern int ike_session_add_recvdpkt (struct sockaddr_storage *, struct sockaddr_storage *,
444 vchar_t *, vchar_t *, size_t, u_int32_t);
445 extern void ike_session_clear_recvdpkt (void);
446 extern void ike_session_init_recvdpkt (void);
447
448 #ifdef ENABLE_HYBRID
449 //extern int ike_session_exclude_cfg_addr (const struct sockaddr_storage *);
450 #endif
451
452 extern void sweep_sleepwake (void);
453
454 #endif /* _HANDLER_H */