File: visit_connection.c

package info (click to toggle)
libreswan 5.2-2.3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 81,644 kB
  • sloc: ansic: 129,988; sh: 32,018; xml: 20,646; python: 10,303; makefile: 3,022; javascript: 1,506; sed: 574; yacc: 511; perl: 264; awk: 52
file content (630 lines) | stat: -rw-r--r-- 18,878 bytes parent folder | download | duplicates (2)
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
/* whack communicating routines, for libreswan
 *
 * Copyright (C) 1997 Angelos D. Keromytis.
 * Copyright (C) 1998-2001,2013-2016 D. Hugh Redelmeier <hugh@mimosa.com>
 * Copyright (C) 2003-2008 Michael Richardson <mcr@xelerance.com>
 * Copyright (C) 2003-2010 Paul Wouters <paul@xelerance.com>
 * Copyright (C) 2009 Avesh Agarwal <avagarwa@redhat.com>
 * Copyright (C) 2010 David McCullough <david_mccullough@securecomputing.com>
 * Copyright (C) 2011 Mika Ilmaranta <ilmis@foobar.fi>
 * Copyright (C) 2012-2013 Paul Wouters <paul@libreswan.org>
 * Copyright (C) 2014-2020 Paul Wouters <pwouters@redhat.com>
 * Copyright (C) 2014-2017 Antony Antony <antony@phenome.org>
 * Copyright (C) 2019 Andrew Cagney <cagney@gnu.org>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <https://www.gnu.org/licenses/gpl2.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include "visit_connection.h"

#include "show.h"
#include "connections.h"
#include "log.h"
#include "ikev1.h"		/* for send_n_log_v1_delete() */
#include "ikev2_delete.h"

typedef unsigned (whack_connections_visitor_cb)
(struct connection *c,
 const struct whack_message *m,
 struct show *s,
 whack_connection_visitor_cb *visit_connection);

static whack_connections_visitor_cb visit_connections_bottom_up;
static whack_connections_visitor_cb visit_connections_root;

/*
 * When there's no name, whack all connections.
 *
 * How to decorate this with a header / footer?
 */

void whack_all_connections_sorted(const struct whack_message *m, struct show *s,
				  whack_connection_visitor_cb *visit_connection)
{
	struct connection **connections = sort_connections();
	if (connections == NULL) {
		return;
	}

	for (struct connection **cp = connections; *cp != NULL; cp++) {
		visit_connection(m, s, (*cp));
	}
	pfree(connections);
}

/*
 * Try by name.
 *
 * Search OLD2NEW so that a template connection matching name is found
 * before any of its instantiated instances (which have the same name,
 * ugh).  WHACK_CONNECTIONS() will then vist it any any instances.
 */

static bool whack_connections_by_name(const struct whack_message *m,
				      struct show *s,
				      whack_connections_visitor_cb *visit_connections,
				      whack_connection_visitor_cb *visit_connection,
				      const struct each *each UNUSED)
{
	struct connection_filter by_name = {
		.name = m->name,
		.search = {
			.order = OLD2NEW,
			.verbose.logger = show_logger(s),
			.where = HERE,
		},
	};
	if (next_connection(&by_name)) {
		visit_connections(by_name.c, m, s,
				  visit_connection);
		return true; /* found something, stop */
	}
	return false; /* keep looking */
}

/*
 * Try by alias.
 *
 * A connection like:
 *
 *   conn foo
 *     subnets=...
 *
 * will expand into alias=FOO name=FOO/1x1 et.al.
 *
 * If FOO is a template, then that will be further expanded into alias-instances.
 *
 * The visit_connections() call-back is is passed the root of this
 * connection tree, it will in turn call visit_connection().
 */

static bool whack_connections_by_alias(const struct whack_message *m,
				       struct show *s,
				       whack_connections_visitor_cb *visit_connections,
				       whack_connection_visitor_cb *visit_connection,
				       enum chrono alias_order,
				       const struct each *each)
{
	struct logger *logger = show_logger(s);
	struct connection_filter by_alias_root = {
		.alias_root = m->name,
		.search = {
			.order = alias_order,
			.verbose.logger = logger,
			.where = HERE,
		},
	};

	/*
	 * Danger:
	 *
	 * When deleting connections, ALIAS_ORDER should be NEW2OLD so
	 * that when the alias root is a template all instances are
	 * deleted before the template (instances are always newer
	 * than their templates).
	 *
	 * This way deleting an alias connection tree can't corrupt
	 * the search list.
	 */
	if (all_connections(&by_alias_root)) {
		/* header */
		if (each->future_tense != NULL) {
			/*
			 * The config option is connalias= but, given
			 * we want this to go away, better to not
			 * tell any one and instead use something
			 * closer to connectionstatus which logs
			 * "aliases: ...".
			 */
			llog(RC_LOG, logger, "%s all connections with alias \"%s\"",
			     each->future_tense, m->name);
		}
		unsigned nr = 0;
		do {
			nr += visit_connections(by_alias_root.c, m, s, visit_connection);
		} while (all_connections(&by_alias_root));
		/* footer */
		if (each->past_tense != NULL) {
			if (nr == 1) {
				llog(RC_LOG, logger, "%s %u connection",
				     each->past_tense, nr);
			} else {
				llog(RC_LOG, logger, "%s %u connections",
				     each->past_tense, nr);
			}
		}
		return true; /* found something, stop */
	}
	return false; /* keep looking */
}

/*
 * If NAME is of the form "$N" or "#N", use that to find and whack a
 * connection.
 *
 * Return true if the search for a connection should stop; not that
 * the search was successful.
 */

static bool whack_connection_by_serialno(const struct whack_message *m,
					 struct show *s,
					 whack_connections_visitor_cb *visit_connections,
					 whack_connection_visitor_cb *visit_connection,
					 const struct each *each UNUSED)
{
	struct logger *logger = show_logger(s);
	if (m->name[0] == '$' ||
	    m->name[0] == '#') {
		ldbg(logger, "looking up '%s' by serialno", m->name);
		uintmax_t serialno = 0;
		err_t e = shunk_to_uintmax(shunk1(m->name + 1), NULL, /*base*/0, &serialno);
		if (e != NULL) {
			llog(RC_LOG, logger, "invalid serial number '%s': %s",
			     m->name, e);
			return true; /* found something, stop */
		}
		if (serialno >= INT_MAX) {/* arbitrary limit */
			llog(RC_LOG, logger, "serial number '%s' is huge", m->name);
			return true; /* found something, stop */
		}
		switch (m->name[0]) {
		case '$':
		{
			struct connection *c = connection_by_serialno(serialno);
			if (c != NULL) {
				visit_connections(c, m, s, visit_connection);
				return true; /* found something, stop */
			}
			break;
		}
		case '#':
		{
			struct state *st = state_by_serialno(serialno);
			if (st != NULL) {
				struct connection *c = st->st_connection;
				visit_connections(c, m, s, visit_connection);
				return true; /* found something, stop */
			}
			break;
		}
		}
		llog(RC_LOG, logger, "serialno '%s' not found", m->name);
		return true; /* found something, stop (YES!) */
	}
	return false; /* keep looking */
}

static unsigned visit_connections_root(struct connection *c,
				       const struct whack_message *m,
				       struct show *s,
				       whack_connection_visitor_cb *visit_connection)
{
	return visit_connection(m, s, c);
}

unsigned whack_connection_instance_new2old(const struct whack_message *m,
					   struct show *s,
					   struct connection *c,
					   whack_connection_visitor_cb *visit_connection)
{
	PEXPECT(c->logger, (is_template(c) ||
			    is_labeled_template(c) ||
			    is_labeled_parent(c)));

	unsigned nr = 0;
	struct connection_filter instances = {
		.clonedfrom = c,
		.ike_version = c->config->ike_version, /*redundant but meh*/
		.search = {
			.order = NEW2OLD,
			.verbose.logger = show_logger(s),
			.where = HERE,
		},
	};
	while (next_connection(&instances)) {

		connection_buf cqb;
		ldbg(c->logger, "visiting instance "PRI_CONNECTION,
		     pri_connection(instances.c, &cqb));
		PEXPECT(c->logger, ((is_template(c) && is_instance(instances.c)) ||
				    (is_labeled_template(c) && is_labeled_parent(instances.c)) ||
				    (is_labeled_parent(c) && is_labeled_child(instances.c))));

		/* abuse bool */
		connection_addref(instances.c, c->logger);
		nr += visit_connection(m, s, instances.c);
		connection_delref(&instances.c, c->logger);
	}

	return nr;
}

static unsigned visit_connections_bottom_up(struct connection *c,
					    const struct whack_message *m,
					    struct show *s,
					    whack_connection_visitor_cb *visit_connection)
{
	struct logger *logger = show_logger(s);
	connection_addref(c, logger); /* must delref */

	unsigned nr = 0;
	struct connection_filter instances = {
		.clonedfrom = c,
		.ike_version = c->config->ike_version, /*redundant but meh*/
		.search = {
			.order = NEW2OLD,
			.verbose.logger = logger,
			.where = HERE,
		},
	};
	while (next_connection(&instances)) {
		/* abuse bool */
		nr += visit_connections_bottom_up(instances.c, m, s, visit_connection);
	}
	/* abuse bool */
	nr += visit_connection(m, s, c);

	/* kill addref() and caller's pointer */
	connection_delref(&c, logger);
	return nr;
}

static void whack_connections(const struct whack_message *m,
			      struct show *s,
			      whack_connections_visitor_cb *visit_connections,
			      whack_connection_visitor_cb *visit_connection,
			      enum chrono alias_order,
			      const struct each *each)
{
	struct logger *logger = show_logger(s);

	/*
	 * Try by name, alias, then serial no.
	 */

	if (whack_connections_by_name(m, s,
				      visit_connections,
				      visit_connection,
				      each)) {
		return;
	}
 
	/*
	 * Danger:
	 *
	 * When deleting connections, ALIAS_ORDER should be NEW2OLD so
	 * that when the alias root is a template all instances are
	 * deleted before the template (instances are always newer
	 * than their templates).
	 *
	 * This way deleting an alias connection tree can't corrupt
	 * the search list.
	 */
	if (whack_connections_by_alias(m, s,
				       visit_connections,
				       visit_connection,
				       alias_order, each)) {
		return;
	}

	if (whack_connection_by_serialno(m, s,
					 visit_connections,
					 visit_connection,
					 each)) {
		return;
	}

	/*
	 * Danger:
	 *
	 * Logging with RC_UNKNOWN_NAME is "fatal" - when whack sees
	 * it it, it detaches immediately.  For instance, adding a
	 * connection is performed in two steps: DELETE+ADD; KEYS.
	 * When there's no connection to delete that should not be
	 * logged as it A. is confusing and B. would cause whack to
	 * detach stopping the KEYS from being added.
	 */
	if (each->log_unknown_name) {
#define MESSAGE "no connection or alias named \"%s\"'", m->name
		/* what means leave more breadcrumbs */
		if (each->past_tense != NULL) {
			llog(RC_UNKNOWN_NAME, logger, MESSAGE);
		} else {
			whack_log(RC_UNKNOWN_NAME, s, MESSAGE);
		}
	}
#undef MESSAGE
}

void whack_connection(const struct whack_message *m,
		      struct show *s,
		      whack_connection_visitor_cb *visit_connection,
		      enum chrono alias_order,
		      struct each each)
{
	/*
	 * Danger:
	 *
	 * When deleting connections, ALIAS_ORDER should be NEW2OLD so
	 * that when the alias root is a template all instances are
	 * deleted before the template (instances are always newer
	 * than their templates).
	 *
	 * This way deleting an alias connection tree can't corrupt
	 * the search list.
	 */
	whack_connections(m, s, visit_connections_root,
			  visit_connection, alias_order, &each);
}

void whack_connections_bottom_up(const struct whack_message *m,
				 struct show *s,
				 whack_connection_visitor_cb *visit_connection,
				 struct each each)
{
	/*
	 * Danger:
	 *
	 * When deleting connections, ALIAS_ORDER should be NEW2OLD so
	 * that when the alias root is a template all instances are
	 * deleted before the template (instances are always newer
	 * than their templates).
	 *
	 * This way deleting an alias connection tree can't corrupt
	 * the search list.
	 */
	whack_connections(m, s, visit_connections_bottom_up,
			  visit_connection,
			  /*alias_order*/NEW2OLD,
			  &each);
}

void visit_connection_states(struct connection *c,
			     void (visit_connection_state)(struct connection *c,
							   struct ike_sa **ike,
							   struct child_sa **child,
							   enum connection_visit_kind visit_kind,
							   struct visit_connection_state_context *context),
			     struct visit_connection_state_context *context,
			     where_t where)
{
	connection_buf cb;
	VERBOSE_DBGP(DBG_BASE, c->logger,
		     PRI_CONNECTION" .routing_ike_sa "PRI_SO" .negotiating_ike_sa "PRI_SO" .established_ike_sa "PRI_SO" .negotiating_child_sa "PRI_SO" .established_child_sa "PRI_SO,
		     pri_connection(c, &cb),
		     pri_so(c->routing_sa),
		     pri_so(c->negotiating_ike_sa),
		     pri_so(c->established_ike_sa),
		     pri_so(c->negotiating_child_sa),
		     pri_so(c->established_child_sa));

	/*
	 * Start by visiting the the connection's IKE SA, when there
	 * is one.
	 *
	 * Cases when there isn't include IKEv1 where the ISAKMP was
	 * deleted, and IKEv2 when the connection is a cuckoo (i.e.,
	 * the Child SA is using another connection's IKE SA).
	 */

	struct ike_sa *ike = ike_sa_by_serialno(c->established_ike_sa); /* could be NULL */
	if (ike != NULL) {
		vdbg("dispatching START to "PRI_SO, pri_so(ike->sa.st_serialno));
		visit_connection_state(c, &ike, NULL, CONNECTION_IKE_PREP, context);
		/*
		 * START_IKE does _not_ delete the IKE SA, thus
		 * ensuring that .establised_ike_sa is still valid.
		 * IKEv1 children need the IKE SA to send their delete
		 * notifications, and IKEv2 children are never
		 * orphaned.
		 *
		 * Additionally, code below uses .established_ike_sa
		 * to detect a Child SA using another connection's IKE
		 * SA (a.k.a. cuckoo).
		 */
		PEXPECT(c->logger, ike != NULL);
	} else if (c->established_ike_sa != SOS_NOBODY) {
		llog_pexpect(c->logger, HERE,
			     "connection's established IKE SA "PRI_SO" is missing",
			     pri_so(c->established_ike_sa));
		/* try to patch up mess!?! */
		c->established_ike_sa = SOS_NOBODY;
	} else {
		vdbg("skipping START, no IKE");
	}

	/*
	 * Notify the connection's Child SA before, notifying any
	 * other children.
	 *
	 * This is to ensure that the connection's Child SA is the
	 * first with an opportunity to put the connection on the
	 * revival queue.  Without this, one of the siblings and their
	 * connection ends up going first and this results in each
	 * revival using a different connection (very confusing).
	 *
	 * Only need to visit the connection with the IKE SA (ignoring
	 * start/stop) when the connection wasn't visited with the
	 * Child SA.
	 */

	enum connection_visit_kind visited_child;
	struct child_sa *child =
		child_sa_by_serialno(c->negotiating_child_sa);
	if (child == NULL) {
		vdbg("skipping Child SA, as no "PRI_SO, pri_so(c->negotiating_child_sa));
		visited_child = 0;
	} else if (c->established_ike_sa == child->sa.st_clonedfrom) {
		vdbg("dispatch Child SA "PRI_SO,
		     pri_so(child->sa.st_serialno));
		visited_child = CONNECTION_IKE_CHILD;
		visit_connection_state(c, &ike, &child, visited_child, context);
	} else if (c->established_ike_sa != SOS_NOBODY) {
		/*
		 * i.e., the connection's Child SA is not using the
		 * connection's IKE SA (.established_ike_sa is for a
		 * different IKE SA), or the connection has no IKE SA
		 * (.established_ike_sa == SOS_NOBODY).
		 */
		vdbg("dispatch cuckoo Child SA "PRI_SO, pri_so(child->sa.st_serialno));
		visited_child = CONNECTION_CUCKOO_CHILD;
		visit_connection_state(c, &ike, &child, visited_child, context);
	} else {
		vdbg("dispatch orphaned Child SA "PRI_SO,
		     pri_so(child->sa.st_serialno));
		visited_child = CONNECTION_ORPHAN_CHILD;
		visit_connection_state(c, NULL, &child, visited_child, context);
	}

	/* debug-log when callback zapps IKE SA */
	if (c->established_ike_sa != SOS_NOBODY && ike == NULL) {
		vdbg("IKE SA "PRI_SO" wiped when visiting child",
		     pri_so(c->established_ike_sa));
	}

	/*
	 * Weed out any larval or lingering SAs.
	 *
	 * These are SAs that are using the connection yet are not the
	 * owner (newest IKE SA or Child SA).  For instance:
	 *
	 * + an IKE SA that failed to establish
	 *
	 * + an IKE SA that was replaced but hasn't yet expired
	 *
	 * + children that are part way through an IKE_AUTH or
	 *   CREATE_CHILD_SA exchange and don't yet own their
	 *   connection's route.
	 *
	 * Typically these states can be deleted outright.
	 */

	vdbg("weeding out larval and lingering SAs");

	struct state_filter weed = {
		.connection_serialno = c->serialno,
		.search = {
			.order = NEW2OLD,
			.verbose = verbose,
			.where = where,
		},
	};
	unsigned nr_parents = 0;
	unsigned nr_children = 0;
	while (next_state(&weed)) {
		struct verbose verbose = weed.search.verbose;

		if (weed.st->st_serialno == c->established_ike_sa) {
			vdbg("skipping "PRI_SO" as newest IKE SA",
			     pri_so(weed.st->st_serialno));
			continue;
		}

		if (weed.st->st_serialno == c->established_child_sa) {
			vdbg("skipping "PRI_SO" as newest Child SA",
			      pri_so(weed.st->st_serialno));
			continue;
		}

		if (weed.st->st_serialno == c->negotiating_child_sa) {
			vdbg("skipping "PRI_SO" as newest routing SA",
			      pri_so(weed.st->st_serialno));
			continue;
		}

		if (IS_PARENT_SA(weed.st)) {
			vdbg("dispatch lurking IKE SA to "PRI_SO,
			     pri_so(weed.st->st_serialno));
			struct ike_sa *lingering_ike = pexpect_ike_sa(weed.st);
			visit_connection_state(c, &lingering_ike, NULL, CONNECTION_LURKING_IKE, context);
			nr_parents++;
			continue;
		}

		vdbg("dispatch lurking Child SA to "PRI_SO,
		     pri_so(weed.st->st_serialno));
		struct child_sa *lingering_child = pexpect_child_sa(weed.st);
		/* may not have IKE as parent? */
		nr_children++;
		visit_connection_state(c, NULL, &lingering_child, CONNECTION_LURKING_CHILD, context);
	}

	vdbg("weeded %u parents and %u children", nr_parents, nr_children);

	/*
	 * Now go through any remaining children.
	 *
	 * This could include children of the first IKE SA that are
	 * been replaced.
	 */

	if (ike != NULL) {
		vdbg("poking siblings");
		struct state_filter child_filter = {
			.clonedfrom = ike->sa.st_serialno,
			.search = {
				.order = NEW2OLD,
				.verbose = verbose,
				.where = where,
			},
		};
		unsigned nr = 0;
		while (next_state(&child_filter)) {
			struct verbose verbose = child_filter.search.verbose;

			struct child_sa *child = pexpect_child_sa(child_filter.st);
			state_buf sb;
			vdbg("dispatching to sibling Child SA "PRI_STATE,
			     pri_state(&child->sa, &sb));
			visit_connection_state(c, &ike, &child, CONNECTION_CHILD_SIBLING, context);
			nr++;
		}
		vdbg("poked %u siblings", nr);
	}

	/*
	 * With everything cleaned up decide what to do with the IKE
	 * SA.
	 */

	if (ike != NULL && visited_child == 0) {
		vdbg("dispatch to IKE SA "PRI_SO" as child skipped",
		     pri_so(ike->sa.st_serialno));
		visit_connection_state(c, &ike, NULL, CONNECTION_CHILDLESS_IKE, context);
	}

	if (ike != NULL) {
		vdbg("dispatch STOP as reached end");
		visit_connection_state(c, &ike, NULL, CONNECTION_IKE_POST, context);
	} else {
		vdbg("skipping STOP, no IKE");
	}
}