aab5c84db98f59813dc37696cf20bab4db4690e6
[strongswan.git] / src / pluto / connections.h
1 /* information about connections between hosts and clients
2 * Copyright (C) 1998-2001 D. Hugh Redelmeier
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * for more details.
13 *
14 * RCSID $Id: connections.h,v 1.18 2006/04/22 21:59:20 as Exp $
15 */
16
17 #ifndef _CONNECTIONS_H
18 #define _CONNECTIONS_H
19
20 #include <sys/queue.h>
21
22 #include "id.h"
23 #include "certs.h"
24 #include "ac.h"
25 #include "smartcard.h"
26 #include "whack.h"
27
28 /* There are two kinds of connections:
29 * - ISAKMP connections, between hosts (for IKE communication)
30 * - IPsec connections, between clients (for secure IP communication)
31 *
32 * An ISAKMP connection looks like:
33 * host<--->host
34 *
35 * An IPsec connection looks like:
36 * client-subnet<-->host<->nexthop<--->nexthop<->host<-->client-subnet
37 *
38 * For the connection to be relevant to this instance of Pluto,
39 * exactly one of the hosts must be a public interface of our machine
40 * known to this instance.
41 *
42 * The client subnet might simply be the host -- this is a
43 * representation of "host mode".
44 *
45 * Each nexthop defaults to the neighbouring host's IP address.
46 * The nexthop is a property of the pair of hosts, not each
47 * individually. It is only needed for IPsec because of the
48 * way IPsec is mixed into the kernel routing logic. Furthermore,
49 * only this end's nexthop is actually used. Eventually, nexthop
50 * will be unnecessary.
51 *
52 * Other information represented:
53 * - each connection has a name: a chunk of uninterpreted text
54 * that is unique for each connection.
55 * - security requirements (currently just the "policy" flags from
56 * the whack command to initiate the connection, but eventually
57 * much more. Different for ISAKMP and IPsec connections.
58 * - rekeying parameters:
59 * + time an SA may live
60 * + time before SA death that a rekeying should be attempted
61 * (only by the initiator)
62 * + number of times to attempt rekeying
63 * - With the current KLIPS, we must route packets for a client
64 * subnet through the ipsec interface (ipsec0). Only one
65 * gateway can get traffic for a specific (client) subnet.
66 * Furthermore, if the routing isn't in place, packets will
67 * be sent in the clear.
68 * "routing" indicates whether the routing has been done for
69 * this connection. Note that several connections may claim
70 * the same routing, as long as they agree about where the
71 * packets are to be sent.
72 * - With the current KLIPS, only one outbound IPsec SA bundle can be
73 * used for a particular client. This is due to a limitation
74 * of using only routing for selection. So only one IPsec state (SA)
75 * may "own" the eroute. "eroute_owner" is the serial number of
76 * this state, SOS_NOBODY if there is none. "routing" indicates
77 * what kind of erouting has been done for this connection, if any.
78 *
79 * Details on routing is in constants.h
80 *
81 * Operations on Connections:
82 *
83 * - add a new connection (with all details) [whack command]
84 * - delete a connection (by name) [whack command]
85 * - initiate a connection (by name) [whack command]
86 * - find a connection (by IP addresses of hosts)
87 * [response to peer request; finding ISAKMP connection for IPsec connection]
88 *
89 * Some connections are templates, missing the address of the peer
90 * (represented by INADDR_ANY). These are always arranged so that the
91 * missing end is "that" (there can only be one missing end). These can
92 * be instantiated (turned into real connections) by Pluto in one of two
93 * different ways: Road Warrior Instantiation or Opportunistic
94 * Instantiation. A template connection is marked for Opportunistic
95 * Instantiation by specifying the peer client as 0.0.0.0/32 (or the IPV6
96 * equivalent). Otherwise, it is suitable for Road Warrior Instantiation.
97 *
98 * Instantiation creates a new temporary connection, with the missing
99 * details filled in. The resulting template lasts only as long as there
100 * is a state that uses it.
101 */
102
103 /* connection policy priority: how important this policy is
104 * - used to implement eroute-like precedence (augmented by a small
105 * bonus for a routed connection).
106 * - a whole number
107 * - larger is more important
108 * - three subcomponents. In order of decreasing significance:
109 * + length of source subnet mask (8 bits)
110 * + length of destination subnet mask (8 bits)
111 * + bias (8 bit)
112 * - a bias of 1 is added to allow prio BOTTOM_PRIO to be less than all
113 * normal priorities
114 * - other bias values are created on the fly to give mild preference
115 * to certaion conditions (eg. routedness)
116 * - priority is inherited -- an instance of a policy has the same priority
117 * as the original policy, even though its subnets might be smaller.
118 * - display format: n,m
119 */
120 typedef unsigned long policy_prio_t;
121 #define BOTTOM_PRIO ((policy_prio_t)0) /* smaller than any real prio */
122 #define set_policy_prio(c) { (c)->prio = \
123 ((policy_prio_t)(c)->spd.this.client.maskbits << 16) \
124 | ((policy_prio_t)(c)->spd.that.client.maskbits << 8) \
125 | (policy_prio_t)1; }
126 #define POLICY_PRIO_BUF (3+1+3+1)
127 extern void fmt_policy_prio(policy_prio_t pp, char buf[POLICY_PRIO_BUF]);
128
129 struct virtual_t;
130
131 struct end {
132 struct id id;
133 ip_address
134 host_addr,
135 host_nexthop,
136 host_srcip;
137 ip_subnet client;
138
139 bool key_from_DNS_on_demand;
140 bool has_client;
141 bool has_client_wildcard;
142 bool has_port_wildcard;
143 bool has_id_wildcards;
144 char *updown;
145 u_int16_t host_port; /* host order */
146 u_int16_t port; /* host order */
147 u_int8_t protocol;
148 cert_t cert; /* end certificate */
149 chunk_t ca; /* CA distinguished name */
150 struct ietfAttrList *groups;/* access control groups */
151 smartcard_t *sc; /* smartcard reader and key info */
152 struct virtual_t *virt;
153 bool modecfg; /* this end: request local address from server */
154 /* that end: give local addresses to clients */
155 bool hostaccess; /* allow access to host via iptables INPUT/OUTPUT */
156 /* rules if client behind host is a subnet */
157 certpolicy_t sendcert; /* whether or not to send the certificate */
158 };
159
160 struct spd_route {
161 struct spd_route *next;
162 struct end this;
163 struct end that;
164 so_serial_t eroute_owner;
165 enum routing_t routing; /* level of routing in place */
166 uint32_t reqid;
167 };
168
169 struct connection {
170 char *name;
171 bool ikev1;
172
173 lset_t policy;
174 time_t sa_ike_life_seconds;
175 time_t sa_ipsec_life_seconds;
176 time_t sa_rekey_margin;
177 unsigned long sa_rekey_fuzz;
178 unsigned long sa_keying_tries;
179
180 /* RFC 3706 DPD */
181 time_t dpd_delay;
182 time_t dpd_timeout;
183 dpd_action_t dpd_action;
184
185 char *log_file_name; /* name of log file */
186 FILE *log_file; /* possibly open FILE */
187 CIRCLEQ_ENTRY(connection) log_link; /* linked list of open conns */
188 bool log_file_err; /* only bitch once */
189
190 struct spd_route spd;
191
192 /* internal fields: */
193
194 unsigned long instance_serial;
195 policy_prio_t prio;
196 bool instance_initiation_ok; /* this is an instance of a policy that mandates initiate */
197 enum connection_kind kind;
198 const struct iface *interface; /* filled in iff oriented */
199
200 so_serial_t /* state object serial number */
201 newest_isakmp_sa,
202 newest_ipsec_sa;
203
204
205 #ifdef DEBUG
206 lset_t extra_debugging;
207 #endif
208
209 /* note: if the client is the gateway, the following must be equal */
210 sa_family_t addr_family; /* between gateways */
211 sa_family_t tunnel_addr_family; /* between clients */
212
213 struct connection *policy_next; /* if multiple policies,
214 next one to apply */
215
216 struct gw_info *gw_info;
217 struct alg_info_esp *alg_info_esp;
218 struct alg_info_ike *alg_info_ike;
219
220 struct host_pair *host_pair;
221 struct connection *hp_next; /* host pair list link */
222
223 struct connection *ac_next; /* all connections list link */
224
225 generalName_t *requested_ca; /* collected certificate requests */
226 bool got_certrequest;
227 };
228
229 #define oriented(c) ((c).interface != NULL)
230 extern bool orient(struct connection *c);
231
232 extern bool same_peer_ids(const struct connection *c
233 , const struct connection *d, const struct id *his_id);
234
235 /* Format the topology of a connection end, leaving out defaults.
236 * Largest left end looks like: client === host : port [ host_id ] --- hop
237 * Note: if that==NULL, skip nexthop
238 */
239 #define END_BUF (SUBNETTOT_BUF + ADDRTOT_BUF + IDTOA_BUF + ADDRTOT_BUF + 10)
240 extern size_t format_end(char *buf, size_t buf_len
241 , const struct end *this, const struct end *that
242 , bool is_left, lset_t policy);
243
244 extern void add_connection(const whack_message_t *wm);
245 extern void initiate_connection(const char *name, int whackfd);
246 extern void initiate_opportunistic(const ip_address *our_client
247 , const ip_address *peer_client, int transport_proto, bool held, int whackfd);
248 extern void terminate_connection(const char *nm);
249 extern void release_connection(struct connection *c, bool relations);
250 extern void delete_connection(struct connection *c, bool relations);
251 extern void delete_connections_by_name(const char *name, bool strict);
252 extern void delete_every_connection(void);
253 extern char *add_group_instance(struct connection *group, const ip_subnet *target);
254 extern void remove_group_instance(const struct connection *group, const char *name);
255 extern void release_dead_interfaces(void);
256 extern void check_orientations(void);
257 extern struct connection *route_owner(struct connection *c
258 , struct spd_route **srp
259 , struct connection **erop
260 , struct spd_route **esrp);
261 extern struct connection *shunt_owner(const ip_subnet *ours
262 , const ip_subnet *his);
263
264 extern bool uniqueIDs; /* --uniqueids? */
265 extern void ISAKMP_SA_established(struct connection *c, so_serial_t serial);
266
267 #define his_id_was_instantiated(c) ((c)->kind == CK_INSTANCE \
268 && (id_is_ipaddr(&(c)->spd.that.id)? \
269 sameaddr(&(c)->spd.that.id.ip_addr, &(c)->spd.that.host_addr) : TRUE))
270
271 struct state; /* forward declaration of tag (defined in state.h) */
272 extern struct connection
273 *con_by_name(const char *nm, bool strict),
274 *find_host_connection(const ip_address *me, u_int16_t my_port
275 , const ip_address *him, u_int16_t his_port, lset_t policy),
276 *refine_host_connection(const struct state *st, const struct id *id
277 , chunk_t peer_ca),
278 *find_client_connection(struct connection *c
279 , const ip_subnet *our_net
280 , const ip_subnet *peer_net
281 , const u_int8_t our_protocol
282 , const u_int16_t out_port
283 , const u_int8_t peer_protocol
284 , const u_int16_t peer_port),
285 *find_connection_by_reqid(uint32_t reqid);
286
287 extern struct connection *
288 find_connection_for_clients(struct spd_route **srp
289 , const ip_address *our_client
290 , const ip_address *peer_client
291 , int transport_proto);
292
293 extern chunk_t get_peer_ca_and_groups(struct connection *c
294 , const ietfAttrList_t **peer_list);
295
296 /* instantiating routines
297 * Note: connection_discard() is in state.h because all its work
298 * is looking through state objects.
299 */
300 struct gw_info; /* forward declaration of tag (defined in dnskey.h) */
301 struct alg_info; /* forward declaration of tag (defined in alg_info.h) */
302 extern struct connection *rw_instantiate(struct connection *c
303 , const ip_address *him
304 , u_int16_t his_port
305 , const ip_subnet *his_net
306 , const struct id *his_id);
307
308 extern struct connection *oppo_instantiate(struct connection *c
309 , const ip_address *him
310 , const struct id *his_id
311 , struct gw_info *gw
312 , const ip_address *our_client
313 , const ip_address *peer_client);
314
315 extern struct connection
316 *build_outgoing_opportunistic_connection(struct gw_info *gw
317 , const ip_address *our_client
318 , const ip_address *peer_client);
319
320 /* worst case: "[" serial "] " myclient "=== ..." peer "===" hisclient '\0' */
321 #define CONN_INST_BUF \
322 (2 + 10 + 1 + SUBNETTOT_BUF + 7 + ADDRTOT_BUF + 3 + SUBNETTOT_BUF + 1)
323
324 extern void fmt_conn_instance(const struct connection *c
325 , char buf[CONN_INST_BUF]);
326
327 /* operations on "pending", the structure representing Quick Mode
328 * negotiations delayed until a Keying Channel has been negotiated.
329 */
330
331 struct pending; /* forward declaration (opaque outside connections.c) */
332
333 extern void add_pending(int whack_sock
334 , struct state *isakmp_sa
335 , struct connection *c
336 , lset_t policy
337 , unsigned long try
338 , so_serial_t replacing);
339
340 extern void release_pending_whacks(struct state *st, err_t story);
341 extern void unpend(struct state *st);
342 extern void update_pending(struct state *os, struct state *ns);
343 extern void flush_pending_by_state(struct state *st);
344 extern void show_pending_phase2(const struct host_pair *hp, const struct state *st);
345
346 extern void connection_discard(struct connection *c);
347
348 /* A template connection's eroute can be eclipsed by
349 * either a %hold or an eroute for an instance iff
350 * the template is a /32 -> /32. This requires some special casing.
351 */
352 #define eclipsable(sr) (subnetishost(&(sr)->this.client) && subnetishost(&(sr)->that.client))
353 extern long eclipse_count;
354 extern struct connection *eclipsed(struct connection *c, struct spd_route **);
355
356
357 /* print connection status */
358
359 extern void show_connections_status(bool all, const char *name);
360 extern int connection_compare(const struct connection *ca
361 , const struct connection *cb);
362 extern void update_host_pair(const char *why, struct connection *c
363 , const ip_address *myaddr, u_int16_t myport
364 , const ip_address *hisaddr, u_int16_t hisport);
365
366 #endif /* _CONNECTIONS_H */