1c6948c7138117c20f4b1612698aed0e8962f042
[strongswan.git] / src / pluto / state.h
1 /* state and event objects
2 * Copyright (C) 1997 Angelos D. Keromytis.
3 * Copyright (C) 1998-2001 D. Hugh Redelmeier.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include <sys/types.h>
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <time.h>
20 #include <gmp.h> /* GNU MP library */
21
22 #include "connections.h"
23
24 /* Message ID mechanism.
25 *
26 * A Message ID is contained in each IKE message header.
27 * For Phase 1 exchanges (Main and Aggressive), it will be zero.
28 * For other exchanges, which must be under the protection of an
29 * ISAKMP SA, the Message ID must be unique within that ISAKMP SA.
30 * Effectively, this labels the message as belonging to a particular
31 * exchange.
32 *
33 * RFC2408 "ISAKMP" 3.1 "ISAKMP Header Format" (near end) states that
34 * the Message ID must be unique. We interpret this to be "unique within
35 * one ISAKMP SA".
36 *
37 * BTW, we feel this uniqueness allows rekeying to be somewhat simpler
38 * than specified by draft-jenkins-ipsec-rekeying-06.txt.
39 */
40
41 typedef u_int32_t msgid_t; /* Network order! */
42 #define MAINMODE_MSGID ((msgid_t) 0)
43
44 struct state; /* forward declaration of tag */
45 extern bool reserve_msgid(struct state *isakmp_sa, msgid_t msgid);
46 extern msgid_t generate_msgid(struct state *isakmp_sa);
47
48
49 /* Oakley (Phase 1 / Main Mode) transform and attributes
50 * This is a flattened/decoded version of what is represented
51 * in the Transaction Payload.
52 * Names are chosen to match corresponding names in state.
53 */
54 struct oakley_trans_attrs {
55 u_int16_t encrypt; /* Encryption algorithm */
56 u_int16_t enckeylen; /* encryption key len (bits) */
57 const struct encrypt_desc *encrypter; /* package of encryption routines */
58 u_int16_t hash; /* Hash algorithm */
59 const struct hash_desc *hasher; /* package of hashing routines */
60 u_int16_t auth; /* Authentication method */
61 const struct oakley_group_desc *group; /* Oakley group */
62 time_t life_seconds; /* When this SA expires (seconds) */
63 u_int32_t life_kilobytes; /* When this SA is exhausted (kilobytes) */
64 #if 0 /* not yet */
65 u_int16_t prf; /* Pseudo Random Function */
66 #endif
67 };
68
69 /* IPsec (Phase 2 / Quick Mode) transform and attributes
70 * This is a flattened/decoded version of what is represented
71 * by a Transaction Payload. There may be one for AH, one
72 * for ESP, and a funny one for IPCOMP.
73 */
74 struct ipsec_trans_attrs {
75 u_int8_t transid; /* transform id */
76 ipsec_spi_t spi; /* his SPI */
77 time_t life_seconds; /* When this SA expires */
78 u_int32_t life_kilobytes; /* When this SA expires */
79 u_int16_t encapsulation;
80 u_int16_t auth;
81 u_int16_t key_len;
82 u_int16_t key_rounds;
83 #if 0 /* not implemented yet */
84 u_int16_t cmprs_dict_sz;
85 u_int32_t cmprs_alg;
86 #endif
87 };
88
89 /* IPsec per protocol state information */
90 struct ipsec_proto_info {
91 bool present; /* was this transform specified? */
92 struct ipsec_trans_attrs attrs;
93 ipsec_spi_t our_spi;
94 u_int16_t keymat_len; /* same for both */
95 u_char *our_keymat;
96 u_char *peer_keymat;
97 };
98
99 /* state object: record the state of a (possibly nascent) SA
100 *
101 * Invariants (violated only during short transitions):
102 * - each state object will be in statetable exactly once.
103 * - each state object will always have a pending event.
104 * This prevents leaks.
105 */
106 struct state
107 {
108 so_serial_t st_serialno; /* serial number (for seniority) */
109 so_serial_t st_clonedfrom; /* serial number of parent */
110
111 struct connection *st_connection; /* connection for this SA */
112
113 int st_whack_sock; /* fd for our Whack TCP socket.
114 * Single copy: close when freeing struct.
115 */
116
117 struct msg_digest *st_suspended_md; /* suspended state-transition */
118
119 struct oakley_trans_attrs st_oakley;
120
121 struct ipsec_proto_info st_ah;
122 struct ipsec_proto_info st_esp;
123 struct ipsec_proto_info st_ipcomp;
124 #ifdef KLIPS
125 ipsec_spi_t st_tunnel_in_spi; /* KLUDGE */
126 ipsec_spi_t st_tunnel_out_spi; /* KLUDGE */
127 #endif
128
129 const struct oakley_group_desc *st_pfs_group; /* group for Phase 2 PFS */
130
131 u_int32_t st_doi; /* Domain of Interpretation */
132 u_int32_t st_situation;
133
134 lset_t st_policy; /* policy for IPsec SA */
135
136 msgid_t st_msgid; /* MSG-ID from header. Network Order! */
137
138 /* only for a state representing an ISAKMP SA */
139 struct msgid_list *st_used_msgids; /* used-up msgids */
140
141 /* symmetric stuff */
142
143 /* initiator stuff */
144 chunk_t st_gi; /* Initiator public value */
145 u_int8_t st_icookie[COOKIE_SIZE];/* Initiator Cookie */
146 chunk_t st_ni; /* Ni nonce */
147
148 /* responder stuff */
149 chunk_t st_gr; /* Responder public value */
150 u_int8_t st_rcookie[COOKIE_SIZE];/* Responder Cookie */
151 chunk_t st_nr; /* Nr nonce */
152
153
154 /* my stuff */
155
156 chunk_t st_tpacket; /* Transmitted packet */
157
158 /* Phase 2 ID payload info about my user */
159 u_int8_t st_myuserprotoid; /* IDcx.protoid */
160 u_int16_t st_myuserport;
161
162 /* his stuff */
163
164 chunk_t st_rpacket; /* Received packet */
165
166 /* Phase 2 ID payload info about peer's user */
167 u_int8_t st_peeruserprotoid; /* IDcx.protoid */
168 u_int16_t st_peeruserport;
169
170 /* end of symmetric stuff */
171
172 u_int8_t st_sec_in_use; /* bool: does st_sec hold a value */
173 MP_INT st_sec; /* Our local secret value */
174
175 chunk_t st_shared; /* Derived shared secret
176 * Note: during Quick Mode,
177 * presence indicates PFS
178 * selected.
179 */
180
181 /* In a Phase 1 state, preserve peer's public key after authentication */
182 struct pubkey *st_peer_pubkey;
183
184 enum state_kind st_state; /* State of exchange */
185 u_int8_t st_retransmit; /* Number of retransmits */
186 unsigned long st_try; /* number of times rekeying attempted */
187 /* 0 means the only time */
188 time_t st_margin; /* life after EVENT_SA_REPLACE */
189 unsigned long st_outbound_count; /* traffic through eroute */
190 time_t st_outbound_time; /* time of last change to st_outbound_count */
191 chunk_t st_p1isa; /* Phase 1 initiator SA (Payload) for HASH */
192 chunk_t st_skeyid; /* Key material */
193 chunk_t st_skeyid_d; /* KM for non-ISAKMP key derivation */
194 chunk_t st_skeyid_a; /* KM for ISAKMP authentication */
195 chunk_t st_skeyid_e; /* KM for ISAKMP encryption */
196 u_char st_iv[MAX_DIGEST_LEN]; /* IV for encryption */
197 u_char st_new_iv[MAX_DIGEST_LEN];
198 u_char st_ph1_iv[MAX_DIGEST_LEN]; /* IV at end if phase 1 */
199 unsigned int st_iv_len;
200 unsigned int st_new_iv_len;
201 unsigned int st_ph1_iv_len;
202
203 chunk_t st_enc_key; /* Oakley Encryption key */
204
205 struct event *st_event; /* backpointer for certain events */
206 struct state *st_hashchain_next; /* Next in list */
207 struct state *st_hashchain_prev; /* Previous in list */
208
209 struct {
210 bool vars_set;
211 bool started;
212 } st_modecfg;
213
214 struct {
215 int attempt;
216 bool started;
217 bool status;
218 } st_xauth;
219
220 u_int32_t nat_traversal;
221 ip_address nat_oa;
222
223 /* RFC 3706 Dead Peer Detection */
224 bool st_dpd; /* Peer supports DPD */
225 time_t st_last_dpd; /* Time of last DPD transmit */
226 u_int32_t st_dpd_seqno; /* Next R_U_THERE to send */
227 u_int32_t st_dpd_expectseqno; /* Next R_U_THERE_ACK to receive */
228 u_int32_t st_dpd_peerseqno; /* global variables */
229 struct event *st_dpd_event; /* backpointer for DPD events */
230
231 u_int32_t st_seen_vendorid; /* Bit field about recognized Vendor ID */
232 };
233
234 /* global variables */
235
236 extern u_int16_t pluto_port; /* Pluto's port */
237
238 extern bool states_use_connection(struct connection *c);
239
240 /* state functions */
241
242 extern struct state *new_state(void);
243 extern void init_states(void);
244 extern void insert_state(struct state *st);
245 extern void unhash_state(struct state *st);
246 extern void release_whack(struct state *st);
247 extern void state_eroute_usage(ip_subnet *ours, ip_subnet *his
248 , unsigned long count, time_t nw);
249 extern void delete_state(struct state *st);
250 extern void delete_states_by_connection(struct connection *c, bool relations);
251
252 extern struct state
253 *duplicate_state(struct state *st),
254 *find_state(const u_char *icookie
255 , const u_char *rcookie
256 , const ip_address *peer
257 , msgid_t msgid),
258 *state_with_serialno(so_serial_t sn),
259 *find_phase2_state_to_delete(const struct state *p1st, u_int8_t protoid
260 , ipsec_spi_t spi, bool *bogus),
261 *find_phase1_state(const struct connection *c, lset_t ok_states),
262 *find_sender(size_t packet_len, u_char *packet);
263
264 extern void show_states_status(bool all, const char *name);
265 extern void for_each_state(void *(f)(struct state *, void *data), void *data);
266 extern void find_my_cpi_gap(cpi_t *latest_cpi, cpi_t *first_busy_cpi);
267 extern ipsec_spi_t uniquify_his_cpi(ipsec_spi_t cpi, struct state *st);
268 extern void fmt_state(bool all, struct state *st, time_t n
269 , char *state_buf, size_t state_buf_len
270 , char *state_buf2, size_t state_buf_len2);
271 extern void delete_states_by_peer(ip_address *peer);