File: kernel_ops.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 (533 lines) | stat: -rw-r--r-- 14,975 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
/* kernel op wrappers, for libreswan
 *
 * Copyright (C) 2021-2023 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 "ip_encap.h"

#include "kernel.h"
#include "kernel_ops.h"
#include "log.h"
#include "ipsec_interface.h"
#include "ip_info.h"
#include "kernel_iface.h"
#include "kernel_policy.h"

/*
 * Setup an IPsec route entry.
 *
 * There's lots of redundancy here, see debug log lines below.
 */

bool kernel_ops_policy_add(enum kernel_policy_op op,
			   enum direction dir,
			   const ip_selector *src_client,
			   const ip_selector *dst_client,
			   const struct kernel_policy *policy,
			   deltatime_t use_lifetime,
			   struct logger *logger, where_t where, const char *story)
{
	const struct ip_protocol *client_proto = selector_protocol(*src_client);
	pexpect(client_proto == selector_protocol(*dst_client));

	if (DBGP(DBG_ROUTING)) {
		LLOG_JAMBUF(DEBUG_STREAM|ADD_PREFIX, logger, buf) {
			jam(buf, "routing:  %s()", __func__);

			jam_string(buf, " ");
			jam_enum_short(buf, &kernel_policy_op_names, op);
			jam_string(buf, "+");
			jam_enum_short(buf, &direction_names, dir);

			jam(buf, " ");
			jam_string(buf, story);
			jam_string(buf, " ");
			jam_where(buf, where);
		}

		LLOG_JAMBUF(DEBUG_STREAM|ADD_PREFIX, logger, buf) {
			jam(buf, "routing:  ");

			jam(buf, " client=");
			jam_selector(buf, src_client);
			jam_string(buf, "=>"); /* directional */
			jam_selector(buf, dst_client);

			jam(buf, " lifetime=");
			jam_deltatime(buf, use_lifetime);
			jam(buf, "s");
		}

		if (policy->sa_marks != NULL ||
		    policy->ipsec_interface != NULL) {
			LLOG_JAMBUF(DEBUG_STREAM|ADD_PREFIX, logger, buf) {
				jam(buf, "routing:  ");

				if (policy->ipsec_interface != NULL) {
					jam_string(buf, " ");
					jam_ipsec_interface(buf, policy->ipsec_interface);
				}

				if (policy->sa_marks != NULL) {
					jam_string(buf, " sa_marks=");
					const char *dir = "out:";
					FOR_EACH_THING(mark,
						       &policy->sa_marks->out,
						       &policy->sa_marks->in) {
						jam(buf, "%s"PRI_SA_MARK,
						    dir, pri_sa_mark(*mark));
						dir = ",in:";
					}
				}
			}

		}

		LLOG_JAMBUF(DEBUG_STREAM|ADD_PREFIX, logger, buf) {
			jam(buf, "routing:  ");

			jam_string(buf, " policy=");
			jam_address(buf, &policy->src.host);
			jam(buf, "=>");
			jam_address(buf, &policy->dst.host);
			jam_string(buf, ",");
			jam_enum_short(buf, &shunt_kind_names, policy->kind);
			jam_string(buf, "=");
			jam_enum_short(buf, &shunt_policy_names, policy->shunt);
			jam_string(buf, ",");
			jam(buf, "priority=%"PRI_SPD_PRIORITY,
			    pri_spd_priority(policy->priority));
			/*
			 * Print outer-to-inner and use paren to show
			 * how each wraps the next.
			 *
			 * XXX: how to also print the encap mode - TCP
			 * or UDP?
			 */
			jam_string(buf, ",");
			jam_enum_short(buf, &kernel_mode_names, policy->mode);
			jam_string(buf, "[");
			for (unsigned i = policy->nr_rules; i > 0; i--) {
				const struct kernel_policy_rule *rule = &policy->rule[i-1];
				const struct ip_protocol *rule_proto = protocol_from_ipproto(rule->proto);
				jam(buf, "%s@"PRI_REQID"(", rule_proto->name, rule->reqid);
			}
			if (policy->nr_rules > 0) {
				/* XXX: should use stuff from selector */
				jam_string(buf, client_proto->name);
			}
			for (unsigned i = policy->nr_rules; i > 0; i--) {
				jam_string(buf, ")");
			}
			jam_string(buf, "]");
		}

		if (policy->sec_label.len > 0) {
			LLOG_JAMBUF(DEBUG_STREAM|ADD_PREFIX, logger, buf) {
				jam(buf, "routing:  ");
				jam_string(buf, " sec_label=");
				jam_sanitized_hunk(buf, policy->sec_label);
			}
		}
	}

	PASSERT(logger, policy != NULL);

	switch(policy->shunt) {
	case SHUNT_IPSEC:
		/*
		 * For an IPsec tunnel to be useful it needs both
		 * inbound and outbound policy.
		 */
		PASSERT(logger, (dir == DIRECTION_OUTBOUND ||
				 dir == DIRECTION_INBOUND));
		PASSERT(logger, policy->nr_rules > 0);
		PASSERT(logger, (policy->kind == SHUNT_KIND_IPSEC));
		break;
	case SHUNT_PASS:
		/*
		 * For instance, in ikev2-33-clearport-01 both inbound
		 * and outbound pass policy is installed.
		 */
		PASSERT(logger, (dir == DIRECTION_OUTBOUND ||
				 dir == DIRECTION_INBOUND));
		PASSERT(logger, policy->nr_rules == 0);
		PASSERT(logger, (policy->kind == SHUNT_KIND_NEVER_NEGOTIATE ||
				 policy->kind == SHUNT_KIND_NEGOTIATION ||
				 /* via FAILURE=NONE */
				 policy->kind == SHUNT_KIND_FAILURE));
		break;
	case SHUNT_DROP:
		/*
		 * For instance, in basic-pluto-19-seedbits, a
		 * never-negotiate drop connection is added.
		 */
		PASSERT(logger, (dir == DIRECTION_OUTBOUND ||
				 dir == DIRECTION_INBOUND));
		PASSERT(logger, policy->nr_rules > 0);
		PASSERT(logger, (policy->kind == SHUNT_KIND_FAILURE ||
				 policy->kind == SHUNT_KIND_NEVER_NEGOTIATE ||
				 policy->kind == SHUNT_KIND_BLOCK));
		break;
	case SHUNT_REJECT:
		/*
		 * For instance, in certoe-10-symmetric-cert-whack, a
		 * block (reject) kernel policy is installed.
		 */
		PASSERT(logger, (dir == DIRECTION_OUTBOUND ||
				 dir == DIRECTION_INBOUND));
		PASSERT(logger, policy->nr_rules > 0);
		PASSERT(logger, (policy->kind == SHUNT_KIND_NEVER_NEGOTIATE));
		break;
	case SHUNT_TRAP:
		PASSERT(logger, (dir == DIRECTION_OUTBOUND));
		PASSERT(logger, policy->nr_rules > 0);
		PASSERT(logger, (policy->kind == SHUNT_KIND_ONDEMAND));
		break;
	case SHUNT_HOLD:
		PASSERT(logger, (dir == DIRECTION_OUTBOUND));
		PASSERT(logger, policy->nr_rules > 0);
		PASSERT(logger, policy->kind == SHUNT_KIND_NEGOTIATION);
		break;
	case SHUNT_NONE:
		/*
		 * FAILURE=NONE should have been turned into
		 * NEGOTIATION=...
		 */
		PASSERT(logger, (policy->kind == SHUNT_KIND_FAILURE));
		PASSERT(logger, policy->nr_rules > 0);
		bad_enum(logger, &shunt_policy_names, policy->shunt);
	case SHUNT_UNSET:
		bad_enum(logger, &shunt_policy_names, policy->shunt);
	}

	bool ok = kernel_ops->policy_add(op, dir,
					 src_client, dst_client,
					 policy,
					 use_lifetime,
					 logger, __func__);

	if (DBGP(DBG_ROUTING)) {
		LLOG_JAMBUF(DEBUG_STREAM|ADD_PREFIX, logger, buf) {
			jam(buf, "routing:   ... %s", bool_str(ok));
		}
	}

	return ok;
}

bool kernel_ops_policy_del(enum direction dir,
			   enum expect_kernel_policy expect_kernel_policy,
			   const ip_selector *src_client,
			   const ip_selector *dst_client,
			   const struct sa_marks *sa_marks,
			   const struct ipsec_interface *xfrmi,
			   enum kernel_policy_id id,
			   const shunk_t sec_label, /*needed*/
			   struct logger *logger, where_t where, const char *story)
{
	const struct ip_protocol *client_proto = selector_protocol(*src_client);
	pexpect(client_proto == selector_protocol(*dst_client));

	if (DBGP(DBG_ROUTING)) {

		LLOG_JAMBUF(DEBUG_STREAM|ADD_PREFIX, logger, buf) {
			jam(buf, "routing:  %s()", __func__);

			jam_string(buf, " ");
			jam_enum_short(buf, &direction_names, dir);

			jam_string(buf, " ");
			jam_string(buf, expect_kernel_policy_name(expect_kernel_policy));

			jam(buf, " ");
			jam_string(buf, story);
			jam_string(buf, " ");
			jam_where(buf, where);

		}

		LLOG_JAMBUF(DEBUG_STREAM|ADD_PREFIX, logger, buf) {
			jam(buf, "routing:  ");

			jam(buf, " client=");
			jam_selector(buf, src_client);
			jam_string(buf, "=>"); /* directional */
			jam_selector(buf, dst_client);
		}

		if (sa_marks != NULL || xfrmi != NULL) {
			LLOG_JAMBUF(DEBUG_STREAM|ADD_PREFIX, logger, buf) {
				jam(buf, "routing:  ");

				if (sa_marks != NULL) {
					jam(buf, " sa_marks=");
					const char *dir = "out:";
					FOR_EACH_THING(mark, &sa_marks->out, &sa_marks->in) {
						jam(buf, "%s"PRI_SA_MARK,
						    dir, pri_sa_mark(*mark));
						dir = ",in:";
					}
				}

				if (xfrmi != NULL) {
					jam(buf, " xfrm_if_id=%d", (int)xfrmi->if_id);
				}
			}

		}

		if (sec_label.len > 0) {
			LLOG_JAMBUF(DEBUG_STREAM|ADD_PREFIX, logger, buf) {
				jam(buf, "routing:  ");
				jam_string(buf, " sec_label=");
				jam_sanitized_hunk(buf, sec_label);
			}
		}
	}

	bool ok = kernel_ops->policy_del(dir, expect_kernel_policy,
					 src_client, dst_client,
					 sa_marks, xfrmi, id, sec_label,
					 logger, __func__);

	if (DBGP(DBG_ROUTING)) {
		LLOG_JAMBUF(DEBUG_STREAM|ADD_PREFIX, logger, buf) {
			jam(buf, "routing:   ... %s", bool_str(ok));
		}
	}

	return ok;
}

bool kernel_ops_add_sa(const struct kernel_state *sa, bool replace, struct logger *logger)
{
	if (DBGP(DBG_ROUTING)) {
		LLOG_JAMBUF(DEBUG_STREAM|ADD_PREFIX, logger, buf) {
			jam(buf, "routing:  %s()", __func__);

			jam(buf, " level=%d", sa->level);
			jam_string(buf, " ");
			jam_enum_short(buf, &direction_names, sa->direction);
			jam_string(buf, " ");
			jam_enum_short(buf, &kernel_mode_names, sa->mode);
		}

		LLOG_JAMBUF(DEBUG_STREAM|ADD_PREFIX, logger, buf) {
			jam(buf, "routing:  ");
			jam(buf, " (src) ");
			jam_selector(buf, &sa->src.route);
			jam_string(buf, " -> ");
			jam_address(buf, &sa->src.address);
			jam(buf, "["PRI_IPSEC_SPI"]", pri_ipsec_spi(sa->spi));
			if (sa->encap_type != NULL) {
				jam(buf, "=%s", sa->encap_type->name);
			}
			jam(buf, " ==> ");
			jam_address(buf, &sa->dst.address);
			jam_string(buf, " -> ");
			jam_selector(buf, &sa->dst.route);
			jam(buf, " (dst)");
		}

		LLOG_JAMBUF(DEBUG_STREAM|ADD_PREFIX, logger, buf) {
			jam(buf, "routing:  ");

			if (sa->ipcomp != NULL) {
				jam(buf, " %s", sa->ipcomp->common.fqn);
			}
			if (sa->integ != NULL) {
				jam(buf, " %s:%zu", sa->integ->common.fqn, sa->integ_key.len);
			}
			if (sa->encrypt != NULL) {
				jam(buf, " %s:%zu", sa->encrypt->common.fqn, sa->encrypt_key.len);
			}

			jam(buf, " replay_window=%d", sa->replay_window);
			jam(buf, " esn=%s", bool_str(sa->esn));

			if (sa->ipsec_interface != NULL) {
				jam_string(buf, " ");
				jam_ipsec_interface(buf, sa->ipsec_interface);
			}

			/* manual mark-out? */
			jam_string(buf, " sa_mark_out=");
			if (sa->sa_mark_out == NULL) {
				jam_string(buf, "auto");
			} else {
				jam(buf, PRI_SA_MARK, pri_sa_mark((*sa->sa_mark_out)));
			}

			/* other stuff */
			if (sa->decap_dscp) jam_string(buf, " +decap_dscp");
			if (!sa->encap_dscp) jam_string(buf, " +dont_encap_dscp");
			if (sa->nopmtudisc) jam_string(buf, " +nopmtudisc");

			jam_string(buf, " ...");
		}
	}

	switch (sa->mode) {
	case KERNEL_MODE_TRANSPORT:
	{
		/*
		 * XXX: since this is for transport mode what is
		 * allowed to change?
		 *
		 * Suspect this code is handling the scenario:
		 *
		 *   l.client <-> l.host <-> r.host <-> r.host+client
		 *
		 * where src.client and dst.host+client are called the
		 * ROUTE.
		 */
		const struct ip_info *afi = address_info(sa->src.address);
		PEXPECT(logger, selector_info(sa->src.route) == afi);
		PEXPECT(logger, selector_info(sa->dst.route) == afi);
		if (DBGP(DBG_BASE)) {
			/* XXX: no test triggers these!?! */
			PEXPECT(logger, selector_prefix_len(sa->src.route) == (int)afi->mask_cnt);
			PEXPECT(logger, selector_prefix_len(sa->dst.route) == (int)afi->mask_cnt);
			/* don't know which of .D/.S is .L/.R */
			PEXPECT(logger, address_eq_address(sa->src.address, selector_prefix(sa->src.route)));
			PEXPECT(logger, address_eq_address(sa->dst.address, selector_prefix(sa->dst.route)));
		}
		break;
	}
	case KERNEL_MODE_TUNNEL:
	case KERNEL_MODE_IPTFS:
		break;
	}

	bool ok = kernel_ops->add_sa(sa, replace, logger);

	if (DBGP(DBG_ROUTING)) {
		LLOG_JAMBUF(DEBUG_STREAM|ADD_PREFIX, logger, buf) {
			jam(buf, "routing:   ... %s", bool_str(ok));
		}
	}

	return ok;
}

bool kernel_ops_migrate_ipsec_sa(struct child_sa *child)
{
	struct logger *logger = child->sa.logger;
	if (kernel_ops->migrate_ipsec_sa == NULL) {
		ldbg(logger, "%s() unsupported kernel stack in migrate_ipsec_sa", __func__);
		return false;
	}

	ldbg(logger, "%s() migrating "PRI_SO" ...", __func__, pri_so(child->sa.st_serialno));

	bool ok = kernel_ops->migrate_ipsec_sa(child);

	ldbg(logger, "%s() ... %s", __func__, bool_str(ok));
	return ok;
}

ipsec_spi_t kernel_ops_get_ipsec_spi(ipsec_spi_t avoid,
				     const ip_address *src,
				     const ip_address *dst,
				     const struct ip_protocol *proto,
				     reqid_t reqid,
				     uintmax_t min, uintmax_t max,
				     const char *story,	/* often SAID string */
				     struct logger *logger)
{
	if (DBGP(DBG_ROUTING)) {
		LLOG_JAMBUF(DEBUG_STREAM|ADD_PREFIX, logger, buf) {
			jam(buf, "routing:  %s()", __func__);

			jam_string(buf, " ");
			jam_address(buf, src);
			jam_string(buf, "-");
			jam(buf, "%s", proto->name);
			jam_string(buf, "->");
			jam_address(buf, dst);
			jam(buf, " reqid=%x", reqid);
			jam(buf, " [%jx,%jx]", min, max);
			jam(buf, " for %s ...", story);
		}
	}

	passert(kernel_ops->get_ipsec_spi != NULL);
	ipsec_spi_t spi = kernel_ops->get_ipsec_spi(avoid, src, dst, proto,
						    reqid, min, max, story, logger);

	if (DBGP(DBG_ROUTING)) {
		LLOG_JAMBUF(DEBUG_STREAM|ADD_PREFIX, logger, buf) {
			jam(buf, "routing:   ... allocated "PRI_IPSEC_SPI" for %s",
			    pri_ipsec_spi(spi), story);
		}
	}

	return spi;
}

bool kernel_ops_del_ipsec_spi(ipsec_spi_t spi, const struct ip_protocol *proto,
			      const ip_address *src, const ip_address *dst,
			      struct logger *logger)
{
	ip_said said = said_from_address_protocol_spi(*dst, proto, spi);
	said_buf sbuf;
	const char *said_story = str_said(&said, &sbuf);

	if (DBGP(DBG_ROUTING)) {
		LLOG_JAMBUF(DEBUG_STREAM|ADD_PREFIX, logger, buf) {
			address_buf sb, db;
			jam(buf, "routing:  %s() deleting sa %s-%s["PRI_IPSEC_SPI"]->%s for %s ...",
			    __func__,
			    str_address(src, &sb),
			    proto == NULL ? "<NULL>" : proto->name,
			    pri_ipsec_spi(spi),
			    str_address(dst, &db),
			    said_story);
		}
	}

	passert(kernel_ops->del_ipsec_spi != NULL);

	bool ok = kernel_ops->del_ipsec_spi(spi, proto, src, dst, said_story, logger);

	if (DBGP(DBG_ROUTING)) {
		LLOG_JAMBUF(DEBUG_STREAM|ADD_PREFIX, logger, buf) {
			jam(buf, "routing:   ... %s", bool_str(ok));
		}
	}

	return ok;
}

bool kernel_ops_detect_nic_offload(const char *name, struct logger *logger)
{
	static bool no_offload;
	if (no_offload) {
		ldbg(logger, "%s() no offload already detected", __func__);
		return false;
	}

	if (kernel_ops->detect_nic_offload == NULL) {
		ldbg(logger, "%s() %s kernel interface does not support offload",
		     __func__, kernel_ops->interface_name);
		no_offload = true;
		return false;
	}

	ldbg(logger, "%s() %s ...", __func__, name);
	bool ok = kernel_ops->detect_nic_offload(name, logger);
	ldbg(logger, "%s() ... %s", __func__, bool_str(ok));

	return ok;
}