File: state_db.c

package info (click to toggle)
libreswan 4.3-1%2Bdeb11u4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 62,688 kB
  • sloc: ansic: 108,293; sh: 25,973; xml: 11,756; python: 10,230; makefile: 1,580; javascript: 1,353; yacc: 825; sed: 647; perl: 584; lex: 159; awk: 156
file content (473 lines) | stat: -rw-r--r-- 13,333 bytes parent folder | download
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
/* State table indexed by serialno, for libreswan
 *
 * Copyright (C) 2015-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 "defs.h"
#include "log.h"
#include "state_db.h"
#include "state.h"
#include "connections.h"
#include "hash_table.h"

static struct hash_table state_hash_tables[];

static void jam_state(struct jambuf *buf, const void *data)
{
	if (data == NULL) {
		jam(buf, "#0");
	} else {
		const struct state *st = data;
		jam(buf, "#%lu", st->st_serialno);
	}
}

static bool state_plausable(struct state *st,
			    enum ike_version ike_version,
			    const so_serial_t *clonedfrom,
			    const msgid_t *v1_msgid
#ifndef USE_IKEv1
			    UNUSED
#endif
			    , const enum sa_role *role)
{
	if (ike_version != st->st_connection->ike_version) {
		return false;
	}
#ifdef USE_IKEv1
	if (v1_msgid != NULL && st->st_v1_msgid.id != *v1_msgid) {
		return false;
	}
#endif
	if (role != NULL && st->st_sa_role != *role) {
		return false;
	}
	if (clonedfrom != NULL && st->st_clonedfrom != *clonedfrom) {
		return false;
	}
	return true;
}

/*
 * A table ordered by serialno.
 */

static const struct list_info state_serialno_list_info = {
	.name = "serialno list",
	.jam = jam_state,
};

struct list_head state_serialno_list_head = INIT_LIST_HEAD(&state_serialno_list_head,
							   &state_serialno_list_info);

/*
 * A table hashed by serialno.
 */

static hash_t serialno_hasher(const so_serial_t *serialno)
{
	return hash_table_hasher(shunk2(serialno, sizeof(*serialno)), zero_hash);
}

static hash_t state_serialno_hasher(const void *data)
{
	const struct state *st = data;
	return serialno_hasher(&st->st_serialno);
}

static struct list_entry *state_serialno_entry(void *data)
{
	struct state *st = data;
	return &st->st_hash_table_entries[STATE_SERIALNO_HASH_TABLE];
}

struct state *state_by_serialno(so_serial_t serialno)
{
	/*
	 * Note that since SOS_NOBODY is never hashed, a lookup of
	 * SOS_NOBODY always returns NULL.
	 */
	struct state *st;
	hash_t hash = serialno_hasher(&serialno);
	struct list_head *bucket = hash_table_bucket(&state_hash_tables[STATE_SERIALNO_HASH_TABLE], hash);
	FOR_EACH_LIST_ENTRY_NEW2OLD(bucket, st) {
		if (st->st_serialno == serialno) {
			return st;
		}
	}
	return NULL;
}

struct ike_sa *ike_sa_by_serialno(so_serial_t serialno)
{
	return pexpect_ike_sa(state_by_serialno(serialno));
}

struct child_sa *child_sa_by_serialno(so_serial_t serialno)
{
	return pexpect_child_sa(state_by_serialno(serialno));
}

/*
 * A table hashed by the connection's address.
 */

static hash_t connection_hasher(struct connection *const *connection)
{
	return hash_table_hasher(shunk2(connection, sizeof(*connection)), zero_hash);
}

static hash_t state_connection_hasher(const void *data)
{
	const struct state *st = data;
	return connection_hasher(&st->st_connection);
}

static struct list_entry *state_connection_entry(void *data)
{
	struct state *st = data;
	return &st->st_hash_table_entries[STATE_CONNECTION_HASH_TABLE];
}

struct state *state_by_connection(struct connection *connection,
				  state_by_predicate *predicate /*optional*/,
				  void *predicate_context,
				  const char *reason)
{
	/*
	 * Note that since SOS_NOBODY is never hashed, a lookup of
	 * SOS_NOBODY always returns NULL.
	 */
	struct state *st;
	hash_t hash = connection_hasher(&connection);
	struct list_head *bucket = hash_table_bucket(&state_hash_tables[STATE_CONNECTION_HASH_TABLE], hash);
	FOR_EACH_LIST_ENTRY_NEW2OLD(bucket, st) {
		if (st->st_connection != connection) {
			continue;
		}
		if (predicate != NULL &&
		    !predicate(st, predicate_context)) {
			continue;
		}
		dbg("State DB: found state #%lu in %s (%s)",
		    st->st_serialno, st->st_state->short_name, reason);
		return st;
	}
	dbg("State DB: state not found (%s)", reason);
	return NULL;
}

void rehash_state_connection(struct state *st)
{
	rehash_table_entry(&state_hash_tables[STATE_CONNECTION_HASH_TABLE], st);
}

/*
 * A table hashed by reqid.
 */

static hash_t reqid_hasher(const reqid_t *reqid)
{
	return hash_table_hasher(shunk2(reqid, sizeof(*reqid)), zero_hash);
}

static hash_t state_reqid_hasher(const void *data)
{
	const struct state *st = data;
	return reqid_hasher(&st->st_reqid);
}

static struct list_entry *state_reqid_entry(void *data)
{
	struct state *st = data;
	return &st->st_hash_table_entries[STATE_REQID_HASH_TABLE];
}

struct state *state_by_reqid(reqid_t reqid,
			     state_by_predicate *predicate /*optional*/,
			     void *predicate_context,
			     const char *reason)
{
	/*
	 * Note that since SOS_NOBODY is never hashed, a lookup of
	 * SOS_NOBODY always returns NULL.
	 */
	struct state *st;
	hash_t hash = reqid_hasher(&reqid);
	struct list_head *bucket = hash_table_bucket(&state_hash_tables[STATE_REQID_HASH_TABLE], hash);
	FOR_EACH_LIST_ENTRY_NEW2OLD(bucket, st) {
		if (st->st_reqid != reqid) {
			continue;
		}
		if (predicate != NULL &&
		    !predicate(st, predicate_context)) {
			continue;
		}
		dbg("State DB: found state #%lu in %s (%s)",
		    st->st_serialno, st->st_state->short_name, reason);
		return st;
	}
	dbg("State DB: state not found (%s)", reason);
	return NULL;
}

void rehash_state_reqid(struct state *st)
{
	rehash_table_entry(&state_hash_tables[STATE_REQID_HASH_TABLE], st);
}

/*
 * Hash table indexed by just the IKE SPIi.
 *
 * The response to an IKE_SA_INIT contains an as yet unknown SPIr
 * value.  Hence, when looking for the initiator of an IKE_SA_INIT
 * response, only the SPIi key is used.
 *
 * When a CHILD SA is emancipated creating a new IKE SA its IKE SPIs
 * are replaced, hence a rehash is required.
 */

static hash_t ike_initiator_spi_hasher(const ike_spi_t *ike_initiator_spi)
{
	return hash_table_hasher(shunk2(ike_initiator_spi, sizeof(*ike_initiator_spi)), zero_hash);
}

static hash_t state_ike_initiator_spi_hasher(const void *data)
{
	const struct state *st = data;
	return ike_initiator_spi_hasher(&st->st_ike_spis.initiator);
}

static struct list_entry *state_ike_initiator_spi_entry(void *data)
{
	struct state *st = data;
	return &st->st_hash_table_entries[STATE_IKE_INITIATOR_SPI_HASH_TABLE];
}

static void jam_state_ike_initiator_spi(struct jambuf *buf, const void *data)
{
	const struct state *st = data;
	jam_state(buf, st);
	jam(buf, ": ");
	jam_dump_bytes(buf, st->st_ike_spis.initiator.bytes,
		       sizeof(st->st_ike_spis.initiator.bytes));
}

struct state *state_by_ike_initiator_spi(enum ike_version ike_version,
					 const so_serial_t *clonedfrom, /*optional*/
					 const msgid_t *v1_msgid, /*optional*/
					 const enum sa_role *role, /*optional*/
					 const ike_spi_t *ike_initiator_spi,
					 const char *name)
{
	hash_t hash = ike_initiator_spi_hasher(ike_initiator_spi);
	struct list_head *bucket = hash_table_bucket(&state_hash_tables[STATE_IKE_INITIATOR_SPI_HASH_TABLE], hash);
	struct state *st = NULL;
	FOR_EACH_LIST_ENTRY_NEW2OLD(bucket, st) {
		if (!state_plausable(st, ike_version, clonedfrom, v1_msgid, role)) {
			continue;
		}
		if (!ike_spi_eq(&st->st_ike_spis.initiator, ike_initiator_spi)) {
			continue;
		}
		dbg("State DB: found %s state #%lu in %s (%s)",
		    enum_name(&ike_version_names, ike_version),
		    st->st_serialno, st->st_state->short_name, name);
		return st;
	}
	dbg("State DB: %s state not found (%s)",
	    enum_name(&ike_version_names, ike_version), name);
	return NULL;
}

/*
 * Hash table indexed by both both IKE SPIi+SPIr.
 *
 * Note that these values change over time and when this happens a
 * rehash is required:
 *
 * - initially SPIr=0, but then when the first response is received
 *   SPIr changes to the value in that response
 *
 * - when a CHILD SA is emancipated creating a new IKE SA, the IKE
 *   SPIs of the child change to those from the CREATE_CHILD_SA
 *   exchange
 */

static hash_t ike_spis_hasher(const ike_spis_t *ike_spis)
{
	return hash_table_hasher(shunk2(ike_spis, sizeof(*ike_spis)), zero_hash);
}

static hash_t state_ike_spis_hasher(const void *data)
{
	const struct state *st = data;
	return ike_spis_hasher(&st->st_ike_spis);
}

static struct list_entry *state_ike_spis_entry(void *data)
{
	struct state *st = data;
	return &st->st_hash_table_entries[STATE_IKE_SPIS_HASH_TABLE];
}

static void jam_state_ike_spis(struct jambuf *buf, const void *data)
{
	const struct state *st = data;
	jam_state(buf, st);
	jam(buf, ": ");
	jam_dump_bytes(buf, st->st_ike_spis.initiator.bytes,
		       sizeof(st->st_ike_spis.initiator.bytes));
	jam(buf, "  ");
	jam_dump_bytes(buf, st->st_ike_spis.responder.bytes,
		       sizeof(st->st_ike_spis.responder.bytes));
}

struct state *state_by_ike_spis(enum ike_version ike_version,
				const so_serial_t *clonedfrom,
				const msgid_t *v1_msgid, /* optional */
				const enum sa_role *sa_role, /* optional */
				const ike_spis_t *ike_spis,
				state_by_predicate *predicate,
				void *predicate_context,
				const char *name)
{
	hash_t hash = ike_spis_hasher(ike_spis);
	struct list_head *bucket = hash_table_bucket(&state_hash_tables[STATE_IKE_SPIS_HASH_TABLE], hash);
	struct state *st = NULL;
	FOR_EACH_LIST_ENTRY_NEW2OLD(bucket, st) {
		if (!state_plausable(st, ike_version, clonedfrom, v1_msgid, sa_role)) {
			continue;
		}
		if (!ike_spis_eq(&st->st_ike_spis, ike_spis)) {
			continue;
		}
		if (predicate != NULL) {
			if (!predicate(st, predicate_context)) {
				continue;
			}
		}
		dbg("State DB: found %s state #%lu in %s (%s)",
		    enum_name(&ike_version_names, ike_version),
		    st->st_serialno, st->st_state->short_name, name);
		return st;
	}
	dbg("State DB: %s state not found (%s)",
	    enum_name(&ike_version_names, ike_version), name);
	return NULL;
}

/*
 * Maintain the contents of the hash tables.
 *
 * Unlike serialno, the IKE SPI[ir] keys can change over time.
 */

static struct list_head hash_slots[STATE_HASH_TABLES_ROOF][STATE_TABLE_SIZE];

static struct hash_table state_hash_tables[] = {
	[STATE_SERIALNO_HASH_TABLE] = {
		.info = {
			.name = "st_serialno table",
			.jam = jam_state,
		},
		.hasher = state_serialno_hasher,
		.entry = state_serialno_entry,
		.nr_slots = elemsof(hash_slots[STATE_SERIALNO_HASH_TABLE]),
		.slots = hash_slots[STATE_SERIALNO_HASH_TABLE],
	},
	[STATE_CONNECTION_HASH_TABLE] = {
		.info = {
			.name = "st_connection table",
			.jam = jam_state,
		},
		.hasher = state_connection_hasher,
		.entry = state_connection_entry,
		.nr_slots = elemsof(hash_slots[STATE_CONNECTION_HASH_TABLE]),
		.slots = hash_slots[STATE_CONNECTION_HASH_TABLE],
	},
	[STATE_REQID_HASH_TABLE] = {
		.info = {
			.name = "st_reqid table",
			.jam = jam_state,
		},
		.hasher = state_reqid_hasher,
		.entry = state_reqid_entry,
		.nr_slots = elemsof(hash_slots[STATE_REQID_HASH_TABLE]),
		.slots = hash_slots[STATE_REQID_HASH_TABLE],
	},
	[STATE_IKE_INITIATOR_SPI_HASH_TABLE] = {
		.info = {
			.name = "IKE SPIi table",
			.jam = jam_state_ike_initiator_spi,
		},
		.hasher = state_ike_initiator_spi_hasher,
		.entry = state_ike_initiator_spi_entry,
		.nr_slots = elemsof(hash_slots[STATE_IKE_INITIATOR_SPI_HASH_TABLE]),
		.slots = hash_slots[STATE_IKE_INITIATOR_SPI_HASH_TABLE],
	},
	[STATE_IKE_SPIS_HASH_TABLE] = {
		.info = {
			.name = "IKE SPI[ir] table",
			.jam = jam_state_ike_spis,
		},
		.hasher = state_ike_spis_hasher,
		.entry = state_ike_spis_entry,
		.nr_slots = elemsof(hash_slots[STATE_IKE_SPIS_HASH_TABLE]),
		.slots = hash_slots[STATE_IKE_SPIS_HASH_TABLE],
	},
};

void add_state_to_db(struct state *st)
{
	dbg("State DB: adding %s state #%lu in %s",
	    enum_name(&ike_version_names, st->st_connection->ike_version),
	    st->st_serialno, st->st_state->short_name);
	passert(st->st_serialno != SOS_NOBODY);

	/* serial NR list, entries are only added */
	st->st_serialno_list_entry = list_entry(&state_serialno_list_info, st);
	insert_list_entry(&state_serialno_list_head,
			  &st->st_serialno_list_entry);

	for (unsigned h = 0; h < elemsof(state_hash_tables); h++) {
		add_hash_table_entry(&state_hash_tables[h], st);
	}
}

void rehash_state_cookies_in_db(struct state *st)
{
	dbg("State DB: re-hashing %s state #%lu IKE SPIi and SPI[ir]",
	    enum_name(&ike_version_names, st->st_connection->ike_version),
	    st->st_serialno);
	rehash_table_entry(&state_hash_tables[STATE_IKE_SPIS_HASH_TABLE], st);
	rehash_table_entry(&state_hash_tables[STATE_IKE_INITIATOR_SPI_HASH_TABLE], st);
}

void del_state_from_db(struct state *st)
{
	dbg("State DB: deleting %s state #%lu in %s",
	    enum_name(&ike_version_names, st->st_connection->ike_version),
	    st->st_serialno, st->st_state->short_name);
	remove_list_entry(&st->st_serialno_list_entry);
	for (unsigned h = 0; h < elemsof(state_hash_tables); h++) {
		del_hash_table_entry(&state_hash_tables[h], st);
	}
}

void init_state_db(void)
{
	for (unsigned h = 0; h < elemsof(state_hash_tables); h++) {
		init_hash_table(&state_hash_tables[h]);
	}
}