summaryrefslogtreecommitdiff
path: root/sbin/isakmpd/connection.c
blob: deebcc8c5f137a9d1360bb629341aed1b3b522c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/*	$OpenBSD: connection.c,v 1.12 2001/01/26 12:12:51 niklas Exp $	*/
/*	$EOM: connection.c,v 1.28 2000/11/23 12:21:18 niklas Exp $	*/

/*
 * Copyright (c) 1999, 2000 Niklas Hallqvist.  All rights reserved.
 * Copyright (c) 1999 Hakan Olsson.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by Ericsson Radio Systems.
 * 4. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * This code was written under funding by Ericsson Radio Systems.
 */

#include <sys/queue.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <string.h>

#include "sysdep.h"

#include "conf.h"
#include "connection.h"
#include "doi.h"
#include "ipsec.h"

/* XXX isakmp.h only required for compare_ids().  */
#include "isakmp.h"

#include "log.h"
#include "timer.h"
#include "util.h"

/* How often should we check that connections we require to be up, are up?  */
#define CHECK_INTERVAL 60

struct connection
{
  TAILQ_ENTRY (connection) link;
  char *name;
  struct event *ev;
};

struct connection_passive
{
  TAILQ_ENTRY (connection_passive) link;
  char *name;
  u_int8_t *local_id, *remote_id;
  size_t local_sz, remote_sz;

#if 0
  /* XXX Potential additions to 'connection_passive'.  */
  char *isakmp_peer;
  struct sa *sa;                /* XXX "Soft" ref to active sa?  */ 
  struct timeval sa_expiration; /* XXX *sa may expire.  */
#endif
};

TAILQ_HEAD (connection_head, connection) connections;
TAILQ_HEAD (passive_head, connection_passive) connections_passive;

/*
 * This is where we setup all the connections we want there right from the
 * start.
 */
void
connection_init ()
{
  struct conf_list *conns, *attrs;
  struct conf_list_node *conn, *attr;

  /*
   * Passive connections normally include: all "active" connections that
   * are not flagged "Active-Only", plus all connections listed in
   * the 'Passive-Connections' list.
   */

  TAILQ_INIT (&connections);
  TAILQ_INIT (&connections_passive);

  conns = conf_get_list ("Phase 2", "Connections");
  if (conns)
    {
      for (conn = TAILQ_FIRST (&conns->fields); conn;
	   conn = TAILQ_NEXT (conn, link))
	{
	  if (connection_setup (conn->field))
	    log_print ("connection_init: could not setup \"%s\"", conn->field);

	  /* XXX Break/abort here if connection_setup failed?  */

	  /*
	   * XXX This code (i.e. the attribute lookup) seems like a
	   * likely candidate for factoring out into a function of its
	   * own.
	   */
	  attrs = conf_get_list (conn->field, "Flags");
	  if (attrs)
	      for (attr = TAILQ_FIRST (&attrs->fields); attr;
		   attr = TAILQ_NEXT (attr, link))
		if (strcasecmp ("active-only", attr->field) == 0)
		  break;
	  if (!attrs || (attrs && !attr))
	    if (connection_record_passive (conn->field))
	      log_print ("connection_init: could not record "
			 "connection \"%s\"", conn->field);
	  if (attrs)
	    conf_free_list (attrs);

	}
      conf_free_list (conns);
    }

  conns = conf_get_list ("Phase 2", "Passive-Connections");
  if (conns)
    {
      for (conn = TAILQ_FIRST (&conns->fields); conn;
	   conn = TAILQ_NEXT (conn, link))
	if (connection_record_passive (conn->field))
	  log_print ("connection_init: could not record passive "
		     "connection \"%s\"", conn->field);
      conf_free_list (conns);
    }
}

/* Check the connection in VCONN and schedule another check later.  */
static void
connection_checker (void *vconn)
{
  struct timeval now;
  struct connection *conn = vconn;

  gettimeofday (&now, 0);
  now.tv_sec += conf_get_num ("General", "check-interval", CHECK_INTERVAL);
  conn->ev
    = timer_add_event ("connection_checker", connection_checker, conn, &now);
  if (!conn->ev)
    log_print ("connection_checker: could not add timer event");
  sysdep_connection_check (conn->name);
}

/* Find the connection named NAME.  */
static struct connection *
connection_lookup (char *name)
{
  struct connection *conn;

  for (conn = TAILQ_FIRST (&connections); conn; conn = TAILQ_NEXT (conn, link))
    if (strcasecmp (conn->name, name) == 0)
      return conn;
  return 0;
}

/* Does the connection named NAME exist?  */
int
connection_exist (char *name)
{
  return (connection_lookup (name) != NULL);
}

/* Find the passive connection named NAME.  */
static struct connection_passive *
connection_passive_lookup_by_name (char *name)
{
  struct connection_passive *conn;

  for (conn = TAILQ_FIRST (&connections_passive); conn;
       conn = TAILQ_NEXT (conn, link))
    if (strcasecmp (conn->name, name) == 0)
      return conn;
  return 0;
}

/*
 * IDs of different types cannot be the same.  
 * XXX Rename to ipsec_compare_id, and move to ipsec.c ?
 */
static int 
compare_ids (u_int8_t *id1, u_int8_t *id2, size_t idlen)
{
  int id1_type, id2_type;

  id1_type = GET_ISAKMP_ID_TYPE (id1);
  id2_type = GET_ISAKMP_ID_TYPE (id2);
  
  return id1_type == id2_type
    ? memcmp (id1 + ISAKMP_ID_DATA_OFF, id2 + ISAKMP_ID_DATA_OFF,
	      idlen - ISAKMP_ID_DATA_OFF) : -1;
}

/* Find the connection named with matching IDs.  */
char *
connection_passive_lookup_by_ids (u_int8_t *id1, u_int8_t *id2)
{
  struct connection_passive *conn;
  
  for (conn = TAILQ_FIRST (&connections_passive); conn;
       conn = TAILQ_NEXT (conn, link))
    {
      if (conn->remote_id == NULL)
	continue;

      /*
       * If both IDs match what we have saved, return the name.  Don't bother
       * in which order they are.
       */
      if ((compare_ids (id1, conn->local_id, conn->local_sz) == 0
	   && compare_ids (id2, conn->remote_id, conn->remote_sz) == 0)
	  || (compare_ids (id1, conn->remote_id, conn->remote_sz) == 0
	      && compare_ids (id2, conn->local_id, conn->local_sz) == 0))
	{
	  LOG_DBG ((LOG_MISC, 60,
		    "connection_passive_lookup_by_ids: returned \"%s\"",
		    conn->name));
	  return conn->name;
	}
    }

  /* In the road warrior case, we do not know the remote ID. In that
   * case we will just match against the local ID.
   */
  for (conn = TAILQ_FIRST (&connections_passive); conn;
       conn = TAILQ_NEXT (conn, link))
    {
      if (conn->remote_id != NULL)
	continue;
      
      if (compare_ids (id1, conn->local_id, conn->local_sz) == 0
	  || compare_ids (id2, conn->local_id, conn->local_sz) == 0)
	{
	  LOG_DBG ((LOG_MISC, 60,
		    "connection passive_lookup_by_ids: returned \"%s\""
		    " only matched local id", conn->name));
	  return conn->name;
	}
    }
  LOG_DBG ((LOG_MISC, 60,
	    "connection_passive_lookup_by_ids: no match"));
  return 0;
}

/*
 * Setup NAME to be a connection that should be up "always", i.e. if it dies,
 * for whatever reason, it should be tried to be brought up, over and over
 * again.
 */
int
connection_setup (char *name)
{
  struct connection *conn = 0;
  struct timeval now;

  /* Check for trials to add duplicate connections.  */
  if (connection_lookup (name))
    {
      LOG_DBG ((LOG_MISC, 10, "connection_setup: cannot add \"%s\" twice",
		name));
      return 0;
    }

  conn = calloc (1, sizeof *conn);
  if (!conn)
    {
      log_error ("connection_setup: calloc (1, %d) failed", sizeof *conn);
      goto fail;
    }

  conn->name = strdup (name);
  if (!conn->name)
    {
      log_error ("connection_setup: strdup (\"%s\") failed", name);
      goto fail;
    }

  gettimeofday (&now, 0);
  conn->ev
    = timer_add_event ("connection_checker", connection_checker, conn, &now);
  if (!conn->ev)
    {
      log_print ("connection_setup: could not add timer event");
      goto fail;
    }

  TAILQ_INSERT_TAIL (&connections, conn, link);
  return 0;

 fail:
  if (conn)
    {
      if (conn->name)
	free (conn->name);
      free (conn);
    }
  return -1;
}

int
connection_record_passive (char *name)
{
  struct connection_passive *conn;
  char *local_id, *remote_id;

  if (connection_passive_lookup_by_name (name))
    {
      LOG_DBG ((LOG_MISC, 10, 
		"connection_record_passive: cannot add \"%s\" twice",
		name));
      return 0;
    }
  
  local_id = conf_get_str (name, "Local-ID");
  if (!local_id)
    {
      log_print ("connection_record_passive: "
		 "\"Local-ID\" is missing from section [%s]",
		 name);
      return -1;
    }

  /* If the remote id lookup fails we defer it to later */
  remote_id = conf_get_str (name, "Remote-ID");

  conn = calloc (1, sizeof *conn);
  if (!conn)
    {
      log_error ("connection_record_passive: calloc (1, %d) failed",
		 sizeof *conn);
      return -1;
    }
  
  conn->name = strdup (name);
  if (!conn->name)
    {
      log_error ("connection_record_passive: strdup (\"%s\") failed", name);
      goto fail;
    }

  /* XXX IPSec DOI-specific.  */
  conn->local_id = ipsec_build_id (local_id, &conn->local_sz);
  if (!conn->local_id)
    goto fail;

  if (remote_id) 
    {
      conn->remote_id = ipsec_build_id (remote_id, &conn->remote_sz);
      if (!conn->remote_id)
	goto fail;
    }
  else
    conn->remote_id = NULL;

  TAILQ_INSERT_TAIL (&connections_passive, conn, link);
  
  LOG_DBG ((LOG_MISC, 60,
	    "connection_record_passive: passive connection \"%s\" "
	    "added", conn->name));
  return 0;

 fail:
  if (conn->local_id)
    free (conn->local_id);
  if (conn->name)
    free (conn->name);
  free (conn);
  return -1;
}

/* Remove the connection named NAME.  */
void
connection_teardown (char *name)
{
  struct connection *conn;

  conn = connection_lookup (name);
  if (!conn)
    return;

  TAILQ_REMOVE (&connections, conn, link);
  timer_remove_event (conn->ev);
  free (conn->name);
  free (conn);
}

/* Remove the passive connection named NAME.  */
void
connection_passive_teardown (char *name)
{
  struct connection_passive *conn;

  conn = connection_passive_lookup_by_name (name);
  if (!conn) 
    return;
  
  TAILQ_REMOVE (&connections_passive, conn, link);
  free (conn->name);
  free (conn->local_id);
  free (conn->remote_id);
  free (conn);
}

void
connection_report (void)
{
  struct connection *conn;
  struct timeval now;
#ifdef USE_DEBUG
  struct connection_passive *pconn;
  struct doi *doi = doi_lookup (ISAKMP_DOI_ISAKMP);
#endif

  gettimeofday (&now, 0);
  for (conn = TAILQ_FIRST (&connections); conn; conn = TAILQ_NEXT (conn, link))
    LOG_DBG ((LOG_REPORT, 0, 
	      "connection_report: connection %s next check %ld seconds",
	      (conn->name ? conn->name : "<unnamed>"),
	      conn->ev->expiration.tv_sec - now.tv_sec));
#ifdef USE_DEBUG
  for (pconn = TAILQ_FIRST (&connections_passive); pconn; 
       pconn = TAILQ_NEXT (pconn, link))
    LOG_DBG ((LOG_REPORT, 0,
	      "connection_report: passive connection %s %s", pconn->name, 
	      doi->decode_ids ("local_id: %s, remote_id: %s",
				pconn->local_id, pconn->local_sz,
				pconn->remote_id, pconn->remote_sz, 1)));
#endif
}

/* Reinit all connections (SIGHUP handling).  */
void
connection_reinit (void)
{
  struct connection *conn;
  struct connection_passive *pconn;

  LOG_DBG ((LOG_MISC, 30, 
	    "connection_reinit: reinitializing connection list"));

  /* Remove all present connections.  */

  for (conn = TAILQ_FIRST (&connections); conn; conn = TAILQ_NEXT (conn, link))
    connection_teardown (conn->name);

  for (pconn = TAILQ_FIRST (&connections_passive); pconn; 
       pconn = TAILQ_NEXT (pconn, link))
    connection_passive_teardown (pconn->name);

  /* Setup new connections, as the (new) config directs.  */
  connection_init ();
}