]> git.saurik.com Git - apple/network_cmds.git/blob - racoon.tproj/handler.h
network_cmds-245.8.tar.gz
[apple/network_cmds.git] / racoon.tproj / handler.h
1 /* $KAME: handler.h,v 1.44 2002/07/10 23:22:03 itojun Exp $ */
2
3 /*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
18 *
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
29 * SUCH DAMAGE.
30 */
31
32 #ifndef __HANDLER_H__
33 #define __HANDLER_H__
34
35 /* Phase 1 handler */
36 /*
37 * main mode:
38 * initiator responder
39 * 0 (---) (---)
40 * 1 start start (1st msg received)
41 * 2 (---) 1st valid msg received
42 * 3 1st msg sent 1st msg sent
43 * 4 1st valid msg received 2st valid msg received
44 * 5 2nd msg sent 2nd msg sent
45 * 6 2nd valid msg received 3rd valid msg received
46 * 7 3rd msg sent 3rd msg sent
47 * 8 3rd valid msg received (---)
48 * 9 SA established SA established
49 *
50 * aggressive mode:
51 * initiator responder
52 * 0 (---) (---)
53 * 1 start start (1st msg received)
54 * 2 (---) 1st valid msg received
55 * 3 1st msg sent 1st msg sent
56 * 4 1st valid msg received 2st valid msg received
57 * 5 (---) (---)
58 * 6 (---) (---)
59 * 7 (---) (---)
60 * 8 (---) (---)
61 * 9 SA established SA established
62 *
63 * base mode:
64 * initiator responder
65 * 0 (---) (---)
66 * 1 start start (1st msg received)
67 * 2 (---) 1st valid msg received
68 * 3 1st msg sent 1st msg sent
69 * 4 1st valid msg received 2st valid msg received
70 * 5 2nd msg sent (---)
71 * 6 (---) (---)
72 * 7 (---) (---)
73 * 8 (---) (---)
74 * 9 SA established SA established
75 */
76 #define PHASE1ST_SPAWN 0
77 #define PHASE1ST_START 1
78 #define PHASE1ST_MSG1RECEIVED 2
79 #define PHASE1ST_MSG1SENT 3
80 #define PHASE1ST_MSG2RECEIVED 4
81 #define PHASE1ST_MSG2SENT 5
82 #define PHASE1ST_MSG3RECEIVED 6
83 #define PHASE1ST_MSG3SENT 7
84 #define PHASE1ST_MSG4RECEIVED 8
85 #define PHASE1ST_ESTABLISHED 9
86 #define PHASE1ST_EXPIRED 10
87 #define PHASE1ST_MAX 11
88
89 typedef enum {
90 natt_natd_received = 0x0001,
91 natt_no_remote_nat = 0x0010,
92 natt_no_local_nat = 0x0020,
93 natt_type_rfc = 0x0100,
94 natt_type_apple = 0x0200,
95 natt_type_02 = 0x0400,
96 natt_type_02N = 0x0800
97 } natt_flags_t;
98
99 #define NATT_TYPE_MASK 0x0F00
100
101 /* About address semantics in each case.
102 * initiator(addr=I) responder(addr=R)
103 * src dst src dst
104 * (local) (remote) (local) (remote)
105 * phase 1 handler I R R I
106 * phase 2 handler I R R I
107 * getspi msg R I I R
108 * acquire msg I R
109 * ID payload I R I R
110 */
111 struct ph1handle {
112 isakmp_index index;
113
114 int status; /* status of this SA */
115 int side; /* INITIATOR or RESPONDER */
116
117 struct sockaddr *remote; /* remote address to negosiate ph1 */
118 struct sockaddr *local; /* local address to negosiate ph1 */
119 /* XXX copy from rmconf due to anonymous configuration.
120 * If anonymous will be forbidden, we do delete them. */
121
122 struct remoteconf *rmconf; /* pointer to remote configuration */
123
124 struct isakmpsa *approval; /* pointer to SA(s) approved. */
125 vchar_t *authstr; /* place holder of string for auth. */
126 /* for example pre-shared key */
127
128 u_int8_t version; /* ISAKMP version */
129 u_int8_t etype; /* Exchange type actually for use */
130 u_int8_t flags; /* Flags */
131 u_int32_t msgid; /* message id */
132
133 struct sched *sce; /* schedule for expire */
134
135 struct sched *scr; /* schedule for resend */
136 int retry_counter; /* for resend. */
137 vchar_t *sendbuf; /* buffer for re-sending */
138
139 vchar_t *dhpriv; /* DH; private value */
140 vchar_t *dhpub; /* DH; public value */
141 vchar_t *dhpub_p; /* DH; partner's public value */
142 vchar_t *dhgxy; /* DH; shared secret */
143 vchar_t *nonce; /* nonce value */
144 vchar_t *nonce_p; /* partner's nonce value */
145 vchar_t *skeyid; /* SKEYID */
146 vchar_t *skeyid_d; /* SKEYID_d */
147 vchar_t *skeyid_a; /* SKEYID_a, i.e. hash */
148 vchar_t *skeyid_e; /* SKEYID_e, i.e. encryption */
149 vchar_t *key; /* cipher key */
150 vchar_t *hash; /* HASH minus general header */
151 vchar_t *sig; /* SIG minus general header */
152 vchar_t *sig_p; /* peer's SIG minus general header */
153 cert_t *cert; /* CERT minus general header */
154 cert_t *cert_p; /* peer's CERT minus general header */
155 cert_t *crl_p; /* peer's CRL minus general header */
156 cert_t *cr_p; /* peer's CR not including general */
157 vchar_t *id; /* ID minus gen header */
158 vchar_t *id_p; /* partner's ID minus general header */
159 /* i.e. strut ipsecdoi_id_b*. */
160 struct isakmp_ivm *ivm; /* IVs */
161
162 vchar_t *sa; /* whole SA payload to send/to be sent*/
163 /* to calculate HASH */
164 /* NOT INCLUDING general header. */
165
166 vchar_t *sa_ret; /* SA payload to reply/to be replyed */
167 /* NOT INCLUDING general header. */
168 /* NOTE: Should be release after use. */
169
170 #ifdef HAVE_GSSAPI
171 void *gssapi_state; /* GSS-API specific state. */
172 /* Allocated when needed */
173 vchar_t *gi_i; /* optional initiator GSS id */
174 vchar_t *gi_r; /* optional responder GSS id */
175 #endif
176
177 struct isakmp_pl_hash *pl_hash; /* pointer to hash payload */
178
179 time_t created; /* timestamp for establish */
180 #ifdef ENABLE_STATS
181 struct timeval start;
182 struct timeval end;
183 #endif
184
185 #ifdef IKE_NAT_T
186 natt_flags_t natt_flags;
187 int natd_payload_type;
188 vchar_t *local_natd;
189 vchar_t *remote_natd;
190 #endif
191
192 u_int32_t msgid2; /* msgid counter for Phase 2 */
193 int ph2cnt; /* the number which is negotiated by this phase 1 */
194 LIST_HEAD(_ph2ofph1_, ph2handle) ph2tree;
195
196 LIST_ENTRY(ph1handle) chain;
197 };
198
199 /* Phase 2 handler */
200 /* allocated per a SA or SA bundles of a pair of peer's IP addresses. */
201 /*
202 * initiator responder
203 * 0 (---) (---)
204 * 1 start start (1st msg received)
205 * 2 acquire msg get 1st valid msg received
206 * 3 getspi request sent getspi request sent
207 * 4 getspi done getspi done
208 * 5 1st msg sent 1st msg sent
209 * 6 1st valid msg received 2nd valid msg received
210 * 7 (commit bit) (commit bit)
211 * 8 SAs added SAs added
212 * 9 SAs established SAs established
213 * 10 SAs expired SAs expired
214 */
215 #define PHASE2ST_SPAWN 0
216 #define PHASE2ST_START 1
217 #define PHASE2ST_STATUS2 2
218 #define PHASE2ST_GETSPISENT 3
219 #define PHASE2ST_GETSPIDONE 4
220 #define PHASE2ST_MSG1SENT 5
221 #define PHASE2ST_STATUS6 6
222 #define PHASE2ST_COMMIT 7
223 #define PHASE2ST_ADDSA 8
224 #define PHASE2ST_ESTABLISHED 9
225 #define PHASE2ST_EXPIRED 10
226 #define PHASE2ST_MAX 11
227
228 struct ph2handle {
229 struct sockaddr *src; /* my address of SA. */
230 struct sockaddr *dst; /* peer's address of SA. */
231
232 /*
233 * copy ip address from ID payloads when ID type is ip address.
234 * In other case, they must be null.
235 */
236 struct sockaddr *src_id;
237 struct sockaddr *dst_id;
238
239 u_int32_t spid; /* policy id by kernel */
240
241 int status; /* ipsec sa status */
242 u_int8_t side; /* INITIATOR or RESPONDER */
243
244 struct sched *sce; /* schedule for expire */
245 struct sched *scr; /* schedule for resend */
246 int retry_counter; /* for resend. */
247 vchar_t *sendbuf; /* buffer for re-sending */
248 vchar_t *msg1; /* buffer for re-sending */
249 /* used for responder's first message */
250
251 int retry_checkph1; /* counter to wait phase 1 finished. */
252 /* NOTE: actually it's timer. */
253
254 u_int32_t seq; /* sequence number used by PF_KEY */
255 /*
256 * NOTE: In responder side, we can't identify each SAs
257 * with same destination address for example, when
258 * socket based SA is required. So we set a identifier
259 * number to "seq", and sent kernel by pfkey.
260 */
261 u_int8_t satype; /* satype in PF_KEY */
262 /*
263 * saved satype in the original PF_KEY request from
264 * the kernel in order to reply a error.
265 */
266
267 u_int8_t flags; /* Flags for phase 2 */
268 u_int32_t msgid; /* msgid for phase 2 */
269
270 struct sainfo *sainfo; /* place holder of sainfo */
271 struct saprop *proposal; /* SA(s) proposal. */
272 struct saprop *approval; /* SA(s) approved. */
273 caddr_t spidx_gen; /* policy from peer's proposal */
274
275 struct dhgroup *pfsgrp; /* DH; prime number */
276 vchar_t *dhpriv; /* DH; private value */
277 vchar_t *dhpub; /* DH; public value */
278 vchar_t *dhpub_p; /* DH; partner's public value */
279 vchar_t *dhgxy; /* DH; shared secret */
280 vchar_t *id; /* ID minus gen header */
281 vchar_t *id_p; /* peer's ID minus general header */
282 vchar_t *nonce; /* nonce value in phase 2 */
283 vchar_t *nonce_p; /* partner's nonce value in phase 2 */
284
285 vchar_t *sa; /* whole SA payload to send/to be sent*/
286 /* to calculate HASH */
287 /* NOT INCLUDING general header. */
288
289 vchar_t *sa_ret; /* SA payload to reply/to be replyed */
290 /* NOT INCLUDING general header. */
291 /* NOTE: Should be release after use. */
292
293
294 struct isakmp_ivm *ivm; /* IVs */
295
296 #ifdef ENABLE_STATS
297 struct timeval start;
298 struct timeval end;
299 #endif
300 struct ph1handle *ph1; /* back pointer to isakmp status */
301
302 LIST_ENTRY(ph2handle) chain;
303 LIST_ENTRY(ph2handle) ph1bind; /* chain to ph1handle */
304 };
305
306 /*
307 * for handling initial contact.
308 */
309 struct contacted {
310 struct sockaddr *remote; /* remote address to negosiate ph1 */
311 LIST_ENTRY(contacted) chain;
312 };
313
314 /*
315 * for checking a packet retransmited.
316 */
317 struct recvdpkt {
318 struct sockaddr *remote; /* the remote address */
319 struct sockaddr *local; /* the local address */
320 vchar_t *hash; /* hash of the received packet */
321 vchar_t *sendbuf; /* buffer for the response */
322 int retry_counter; /* how many times to send */
323 time_t time_send; /* timestamp to send a packet */
324 time_t created; /* timestamp to create a queue */
325
326 struct sched *scr; /* schedule for resend, may not used */
327
328 LIST_ENTRY(recvdpkt) chain;
329 };
330
331 /* for parsing ISAKMP header. */
332 struct isakmp_parse_t {
333 u_char type; /* payload type of mine */
334 int len; /* ntohs(ptr->len) */
335 struct isakmp_gen *ptr;
336 };
337
338 /*
339 * for IV management.
340 *
341 * - normal case
342 * initiator responder
343 * ------------------------- --------------------------
344 * initialize iv(A), ive(A). initialize iv(A), ive(A).
345 * encode by ive(A).
346 * save to iv(B). ---[packet(B)]--> save to ive(B).
347 * decode by iv(A).
348 * packet consistency.
349 * sync iv(B) with ive(B).
350 * check auth, integrity.
351 * encode by ive(B).
352 * save to ive(C). <--[packet(C)]--- save to iv(C).
353 * decoded by iv(B).
354 * :
355 *
356 * - In the case that a error is found while cipher processing,
357 * initiator responder
358 * ------------------------- --------------------------
359 * initialize iv(A), ive(A). initialize iv(A), ive(A).
360 * encode by ive(A).
361 * save to iv(B). ---[packet(B)]--> save to ive(B).
362 * decode by iv(A).
363 * packet consistency.
364 * sync iv(B) with ive(B).
365 * check auth, integrity.
366 * error found.
367 * create notify.
368 * get ive2(X) from iv(B).
369 * encode by ive2(X).
370 * get iv2(X) from iv(B). <--[packet(Y)]--- save to iv2(Y).
371 * save to ive2(Y).
372 * decoded by iv2(X).
373 * :
374 *
375 * The reason why the responder synchronizes iv with ive after checking the
376 * packet consistency is that it is required to leave the IV for decoding
377 * packet. Because there is a potential of error while checking the packet
378 * consistency. Also the reason why that is before authentication and
379 * integirty check is that the IV for informational exchange has to be made
380 * by the IV which is after packet decoded and checking the packet consistency.
381 * Otherwise IV mismatched happens between the intitiator and the responder.
382 */
383 struct isakmp_ivm {
384 vchar_t *iv; /* for decoding packet */
385 /* if phase 1, it's for computing phase2 iv */
386 vchar_t *ive; /* for encoding packet */
387 };
388
389 /* for dumping */
390 struct ph1dump {
391 isakmp_index index;
392 int status;
393 int side;
394 struct sockaddr_storage remote;
395 struct sockaddr_storage local;
396 u_int8_t version;
397 u_int8_t etype;
398 time_t created;
399 int ph2cnt;
400 };
401
402 struct sockaddr;
403 struct ph1handle;
404 struct ph2handle;
405 struct policyindex;
406
407 extern struct ph1handle *getph1byindex __P((isakmp_index *));
408 extern struct ph1handle *getph1byindex0 __P((isakmp_index *));
409 extern struct ph1handle *getph1byaddr __P((struct sockaddr *,
410 struct sockaddr *));
411 extern vchar_t *dumpph1 __P((void));
412 extern struct ph1handle *newph1 __P((void));
413 extern void delph1 __P((struct ph1handle *));
414 extern int insph1 __P((struct ph1handle *));
415 extern void remph1 __P((struct ph1handle *));
416 extern void flushph1 __P((void));
417 extern void initph1tree __P((void));
418
419 extern struct ph2handle *getph2byspidx __P((struct policyindex *));
420 extern struct ph2handle *getph2byspid __P((u_int32_t));
421 extern struct ph2handle *getph2byseq __P((u_int32_t));
422 extern struct ph2handle *getph2bymsgid __P((struct ph1handle *, u_int32_t));
423 extern struct ph2handle *getph2bysaidx __P((struct sockaddr *,
424 struct sockaddr *, u_int, u_int32_t));
425 extern struct ph2handle *newph2 __P((void));
426 extern void initph2 __P((struct ph2handle *));
427 extern void delph2 __P((struct ph2handle *));
428 extern int insph2 __P((struct ph2handle *));
429 extern void remph2 __P((struct ph2handle *));
430 extern void flushph2 __P((void));
431 extern void deleteallph2 __P((struct sockaddr *, struct sockaddr *, u_int));
432 extern void initph2tree __P((void));
433
434 extern void bindph12 __P((struct ph1handle *, struct ph2handle *));
435 extern void unbindph12 __P((struct ph2handle *));
436
437 extern struct contacted *getcontacted __P((struct sockaddr *));
438 extern int inscontacted __P((struct sockaddr *));
439 extern void initctdtree __P((void));
440
441 extern int check_recvdpkt __P((struct sockaddr *,
442 struct sockaddr *, vchar_t *));
443 extern int add_recvdpkt __P((struct sockaddr *, struct sockaddr *,
444 vchar_t *, vchar_t *));
445 extern void init_recvdpkt __P((void));
446
447 #endif /* __HANDLER_H__ */
448