public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
c6c595a47f7cd23127227449b95fae8f68a720f9 blob 12828 bytes (raw)

  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
 
// SPDX-License-Identifier: GPL-2.0-or-later

/* PESTO - Programmable Extensible Socket Translation Orchestrator
 *  front-end for passt(1) and pasta(1) forwarding configuration
 *
 * pesto.c - Main program (it's not actually extensible)
 *
 * Copyright (c) 2026 Red Hat GmbH
 * Author: Stefano Brivio <sbrivio@redhat.com>
 */

#include <arpa/inet.h>
#include <sys/prctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <errno.h>
#include <getopt.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <unistd.h>

#include <linux/audit.h>
#include <linux/capability.h>
#include <linux/filter.h>
#include <linux/seccomp.h>

#include "common.h"
#include "seccomp_pesto.h"
#include "serialise.h"
#include "fwd_rule.h"
#include "pesto.h"
#include "log.h"

bool debug_flag = false;

static char stdout_buf[BUFSIZ];

/**
 * usage() - Print usage, exit with given status code
 * @name:	Executable name
 * @f:		Stream to print usage info to
 * @status:	Status code for exit(2)
 *
 * #syscalls:pesto exit_group fstat write
 */
static void usage(const char *name, FILE *f, int status)
{
	FPRINTF(f, "Usage: %s [OPTION]... PATH\n", name);
	FPRINTF(f,
		"\n"
		"  -t, --tcp-ports SPEC	TCP inbound port forwarding\n"
		"    can be specified multiple times\n"
		"    SPEC can be:\n"
		"      'none': don't forward any ports\n"
		"      [ADDR[%%IFACE]/]PORTS: forward specific ports\n"
		"        PORTS is either 'all' (forward all unbound, non-ephemeral\n"
		"        ports), or a comma-separated list of ports, optionally\n"
		"        ranged with '-' and optional target ports after ':'.\n"
		"        Ranges can be reduced by excluding ports or ranges\n"
		"        prefixed by '~'.\n"
		"        The 'auto' keyword may be given to only forward\n"
		"        ports which are bound in the target namespace\n"
		"        Examples:\n"
		"        -t all         Forward all ports\n"
		"        -t 127.0.0.1/all Forward all ports from local address\n"
		"                         127.0.0.1\n"
		"        -t 22		Forward local port 22 to 22\n"
		"        -t 22:23	Forward local port 22 to 23\n"
		"        -t 22,25	Forward ports 22, 25 to ports 22, 25\n"
		"        -t 22-80  	Forward ports 22 to 80\n"
		"        -t 22-80:32-90	Forward ports 22 to 80 to\n"
		"			corresponding port numbers plus 10\n"
		"        -t 192.0.2.1/5	Bind port 5 of 192.0.2.1\n"
		"        -t 5-25,~10-20	Forward ports 5 to 9, and 21 to 25\n"
		"        -t ~25		Forward all ports except for 25\n"
		"        -t auto	Forward all ports bound in namespace\n"
		"        -t 192.0.2.2/auto Forward ports from 192.0.2.2 if\n"
		"                          they are bound in the namespace\n"
		"        -t 8000-8010,auto Forward ports 8000-8010 if they\n"
		"                          are bound in the namespace\n"
		"  -u, --udp-ports SPEC	UDP inbound port forwarding\n"
		"    SPEC is as described for TCP above\n"
		"  -T, --tcp-ns SPEC	TCP outbound port forwarding\n"
		"    SPEC is as described above\n"
		"  -U, --udp-ns SPEC	UDP outbound port forwarding\n"
		"    SPEC is as described above\n"
		"  -s, --show		Show configuration before and after\n"
		"  -d, --debug		Print debugging messages\n"
		"  -h, --help		Display this help message and exit\n"
		"  --version		Show version and exit\n");
	exit(status);
}

/* Maximum number of pifs with rule tables */
#define MAX_PIFS	3

struct pif_configuration {
	uint8_t pif;
	char name[PIF_NAME_SIZE];
	struct fwd_table fwd;
};

struct configuration {
	uint32_t npifs;
	struct pif_configuration pif[MAX_PIFS];
};

/**
 * pif_conf_by_num() - Find a pif's configuration by pif id
 * @conf:	Configuration description
 * @pif:	pif id
 *
 * Return: pointer to the pif_configuration for @pif, or NULL if not found
 */
static struct pif_configuration *pif_conf_by_num(struct configuration *conf,
						 uint8_t pif)
{
	unsigned i;

	for (i = 0; i < conf->npifs; i++) {
		if (conf->pif[i].pif == pif)
			return &conf->pif[i];
	}

	return NULL;
}

/**
 * pif_conf_by_name() - Find a pif's configuration by name
 * @conf:	Configuration description
 * @name:	Interface name
 *
 * Return: pif_configuration for pif named @name, or NULL if not found
 */
static struct pif_configuration *pif_conf_by_name(struct configuration *conf,
						  const char *name)
{
	unsigned i;

	for (i = 0; i < conf->npifs; i++) {
		if (strcmp(conf->pif[i].name, name) == 0)
			return &conf->pif[i];
	}

	return NULL;
}

/**
 * pesto_read_rules() - Read rulestate from passt/pasta
 * @fd:		Control socket
 * @conf:	Configuration description to update
 */
static bool read_pif_conf(int fd, struct configuration *conf)
{
	struct pif_configuration *pc;
	struct pesto_pif_info info;
	uint8_t pif;
	unsigned i;

	if (read_u8(fd, &pif) < 0)
		die("Error reading from control socket");

	if (pif == PIF_NONE)
		return false;

	debug("Receiving config for PIF %"PRIu8, pif);

	if (conf->npifs >= ARRAY_SIZE(conf->pif)) {
		die("passt has more pifs than pesto can manage (max %d)",
		    ARRAY_SIZE(conf->pif));
	}

	pc = &conf->pif[conf->npifs];
	pc->pif = pif;

	if (read_all_buf(fd, &info, sizeof(info)) < 0)
		die("Error reading from control socket");

	if (info.name[sizeof(info.name)-1])
		die("Interface name was not NULL terminated");

	static_assert(sizeof(info.name) == sizeof(pc->name),
		      "Mismatching pif name lengths");
	memcpy(pc->name, info.name, sizeof(pc->name));
	pc->fwd.caps = ntohl(info.caps);
	pc->fwd.count = ntohl(info.count);

	debug("PIF %"PRIu8": %s, %"PRIu32" rules, capabilities 0x%"PRIx32
	      ":%s%s%s%s%s%s", pc->pif, pc->name, pc->fwd.count, pc->fwd.caps,
	      pc->fwd.caps & FWD_CAP_IPV4 ? " IPv4" : "",
	      pc->fwd.caps & FWD_CAP_IPV6 ? " IPv6" : "",
	      pc->fwd.caps & FWD_CAP_TCP ? " TCP" : "",
	      pc->fwd.caps & FWD_CAP_UDP ? " UDP" : "",
	      pc->fwd.caps & FWD_CAP_SCAN ? " scan" : "",
	      pc->fwd.caps & FWD_CAP_IFNAME ? " ifname" : "");

	/* O(n^2), but n is bounded by MAX_PIFS */
	if (pif_conf_by_num(conf, pc->pif))
		die("Received duplicate interface identifier");

	/* O(n^2), but n is bounded by MAX_PIFS */
	if (pif_conf_by_name(conf, pc->name))
		die("Received duplicate interface name");

	/* NOTE: We read the fwd rules directly into fwd.rules, rather than
	 * using fwd_rule_add().  This means we can read and display rules even
	 * if something has gone wrong (in pesto or passt) and we get rules that
	 * fwd_rule_add() would reject.  It does have the side effect that we
	 * never assign socket space for the fwd rules, but we don't need that
	 * within pesto.
	 */
	for (i = 0; i < pc->fwd.count; i++) {
		if (fwd_rule_read(fd, &pc->fwd.rules[i]) < 0)
			die("Error reading from control socket");
	}

	conf->npifs++;
	return true;
}

/**
 * send_conf() - Send updated configuration to passt/pasta
 * @fd:		Control socket
 * @conf:	Updated configuration
 */
static void send_conf(int fd, const struct configuration *conf)
{
	unsigned i;

	for (i = 0; i < conf->npifs; i++) {
		const struct pif_configuration *pc = &conf->pif[i];
		unsigned j;

		if (write_u8(fd, pc->pif) < 0)
			goto fail;

		if (write_u32(fd, pc->fwd.count) < 0)
			goto fail;

		for (j = 0; j < pc->fwd.count; j++) {
			if (fwd_rule_write(fd, &pc->fwd.rules[j]) < 0)
				goto fail;
		}
	}

	if (write_u8(fd, PIF_NONE) < 0)
		goto fail;
	return;

fail:
	die_perror("Error writing to control socket");
}

/**
 * show_conf() - Show current configuration obtained from passt/pasta
 * @conf:	Configuration description
 */
static void show_conf(const struct configuration *conf)
{
	unsigned i;

	for (i = 0; i < conf->npifs; i++) {
		const struct pif_configuration *pc = &conf->pif[i];
		printf("  %s\n", pc->name);
		fwd_rules_dump(printf, pc->fwd.rules, pc->fwd.count,
			       "    ", "\n");
	}
	/* Flush stdout, so this doesn't get misordered with later debug()s */
	(void)fflush(stdout);
}

/**
 * main() - Dynamic reconfiguration client main program
 * @argc:	Argument count
 * @argv:	Arguments: socket path, operation, port specifiers
 *
 * Return: 0 on success, won't return on failure
 *
 * #syscalls:pesto socket s390x:socketcall i686:socketcall
 * #syscalls:pesto connect shutdown close
 * #syscalls:pesto exit_group fstat read write openat
 */
int main(int argc, char **argv)
{
	const struct option options[] = {
		{"debug",	no_argument,		NULL,		'd' },
		{"help",	no_argument,		NULL,		'h' },
		{"version",	no_argument,		NULL,		1 },
		{"tcp-ports",	required_argument,	NULL,		't' },
		{"udp-ports",	required_argument,	NULL,		'u' },
		{"tcp-ns",	required_argument,	NULL,		'T' },
		{"udp-ns",	required_argument,	NULL,		'U' },
		{"show",	no_argument,		NULL,		's' },
		{ 0 },
	};
	struct pif_configuration *inbound, *outbound;
	struct sockaddr_un a = { AF_UNIX, "" };
	const char *optstring = "dht:u:T:U:s";
	struct configuration conf = { 0 };
	bool update = false, show = false;
	struct pesto_hello hello;
	struct sock_fprog prog;
	int optname, ret, s;
	uint32_t s_version;

	prctl(PR_SET_DUMPABLE, 0);

	prog.len = (unsigned short)sizeof(filter_pesto) /
				   sizeof(filter_pesto[0]);
	prog.filter = filter_pesto;
	if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) ||
	    prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog))
		die("Failed to apply seccomp filter");

	/* Explicitly set stdout buffer, otherwise printf() might allocate,
	 * breaking our seccomp profile.
	 */
	if (setvbuf(stdout, stdout_buf, _IOFBF, sizeof(stdout_buf)))
		die_perror("Failed to set stdout buffer");

	fwd_probe_ephemeral();

	do {
		optname = getopt_long(argc, argv, optstring, options, NULL);

		switch (optname) {
		case -1:
		case 0:
			break;
		case 't':
		case 'u':
		case 'T':
		case 'U':
			/* Parse these options after we've read state from passt/pasta */
			update = true;
			break;
		case 's':
			show = true;
			break;
		case 'h':
			usage(argv[0], stdout, EXIT_SUCCESS);
			break;
		case 'd':
			debug_flag = true;
			break;
		case 1:
			FPRINTF(stdout, "pesto ");
			FPRINTF(stdout, VERSION_BLOB);
			exit(EXIT_SUCCESS);
		default:
			usage(argv[0], stderr, EXIT_FAILURE);
		}
	} while (optname != -1);

	if (argc - optind != 1)
		usage(argv[0], stderr, EXIT_FAILURE);

	debug("debug_flag=%d, path=\"%s\"", debug_flag, argv[optind]);

	if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
		die_perror("Failed to create AF_UNIX socket");

	ret = snprintf(a.sun_path, sizeof(a.sun_path), "%s", argv[optind]);
	if (ret <= 0 || ret >= (int)sizeof(a.sun_path))
		die("Invalid socket path \"%s\"", argv[1]);

	ret = connect(s, (struct sockaddr *)&a, sizeof(a));
	if (ret < 0) {
		die_perror("Failed to connect to %s", a.sun_path);
	}

	debug("Connected to passt/pasta control socket");

	ret = read_all_buf(s, &hello, sizeof(hello));
	if (ret < 0)
		die_perror("Couldn't read server greeting");

	if (memcmp(hello.magic, PESTO_SERVER_MAGIC, sizeof(hello.magic)))
		die("Bad magic number from server");

	s_version = ntohl(hello.version);

	if (s_version > PESTO_PROTOCOL_VERSION) {
		die("Unknown server protocol version %"PRIu32" > %"PRIu32"\n",
		    s_version, PESTO_PROTOCOL_VERSION);
	}

	/* cppcheck-suppress knownConditionTrueFalse */
	if (!s_version) {
		if (PESTO_PROTOCOL_VERSION)
			die("Unsupported experimental server protocol");
		FPRINTF(stderr,
"Warning: Using experimental protocol version, client and server must match\n");
	}

	if (ntohl(hello.pif_name_size) != PIF_NAME_SIZE) {
		die("Server has unexpected pif name size (%"
		    PRIu32" not %"PRIu32"\n",
		    ntohl(hello.pif_name_size), PIF_NAME_SIZE);
	}

	if (ntohl(hello.ifnamsiz) != IFNAMSIZ) {
		die("Server has unexpected IFNAMSIZ (%"
		    PRIu32" not %"PRIu32"\n",
		    ntohl(hello.ifnamsiz), IFNAMSIZ);
	}
	
	while (read_pif_conf(s, &conf))
		;

	if (!update) {
		printf("passt/pasta configuration (%s)\n", a.sun_path);
		show_conf(&conf);
		goto noupdate;
	}

	if (show) {
		printf("Previous configuration (%s)\n", a.sun_path);
		show_conf(&conf);
	}

	inbound = pif_conf_by_name(&conf, "HOST");
	outbound = pif_conf_by_name(&conf, "SPLICE");

	optind = 0;
	do {
		optname = getopt_long(argc, argv, optstring, options, NULL);

		switch (optname) {
		case 't':
		case 'u':
			if (!inbound) {
				die("Can't use -%c, no inbound interface",
				    optname);
			}
			fwd_rule_parse(optname, optarg, &inbound->fwd);
			break;
		case 'T':
		case 'U':
			if (!outbound) {
				die("Can't use -%c, no outbound interface",
				    optname);
			}
			fwd_rule_parse(optname, optarg, &outbound->fwd);
			break;
		default:
			continue;
		}
	} while (optname != -1);

	if (show) {
		printf("Updated configuration (%s)\n", a.sun_path);
		show_conf(&conf);
	}

	send_conf(s, &conf);

noupdate:
	if (shutdown(s, SHUT_RDWR) < 0 || close(s) < 0)
		die_perror("Error shutting down control socket");
			       
	exit(0);
}
debug log:

solving c6c595a4 ...
found c6c595a4 in https://archives.passt.top/passt-dev/20260421062516.2601204-18-david@gibson.dropbear.id.au/ ||
	https://archives.passt.top/passt-dev/20260421044217.2500314-17-david@gibson.dropbear.id.au/
found ebac6bd6 in https://archives.passt.top/passt-dev/20260421062516.2601204-17-david@gibson.dropbear.id.au/ ||
	https://archives.passt.top/passt-dev/20260421044217.2500314-16-david@gibson.dropbear.id.au/
found 35a4d559 in https://archives.passt.top/passt-dev/20260421062516.2601204-16-david@gibson.dropbear.id.au/ ||
	https://archives.passt.top/passt-dev/20260421044217.2500314-15-david@gibson.dropbear.id.au/
found 3e34bbac in https://archives.passt.top/passt-dev/20260421044217.2500314-12-david@gibson.dropbear.id.au/ ||
	https://archives.passt.top/passt-dev/20260421062516.2601204-13-david@gibson.dropbear.id.au/
found 5e191283 in https://archives.passt.top/passt-dev/20260421044217.2500314-11-david@gibson.dropbear.id.au/ ||
	https://archives.passt.top/passt-dev/20260421062516.2601204-12-david@gibson.dropbear.id.au/
found f0916e82 in https://archives.passt.top/passt-dev/20260421062516.2601204-11-david@gibson.dropbear.id.au/ ||
	https://archives.passt.top/passt-dev/20260421044217.2500314-10-david@gibson.dropbear.id.au/
found 9f2fa5d5 in https://archives.passt.top/passt-dev/20260421062516.2601204-10-david@gibson.dropbear.id.au/ ||
	https://archives.passt.top/passt-dev/20260421044217.2500314-9-david@gibson.dropbear.id.au/

applying [1/14] https://archives.passt.top/passt-dev/20260421062516.2601204-10-david@gibson.dropbear.id.au/
diff --git a/pesto.c b/pesto.c
new file mode 100644
index 00000000..9f2fa5d5

Checking patch pesto.c...
Applied patch pesto.c cleanly.

skipping https://archives.passt.top/passt-dev/20260421044217.2500314-9-david@gibson.dropbear.id.au/ for 9f2fa5d5
index at:
100644 9f2fa5d5687d5604ef6aa43c4a9ccea9fd7bccad	pesto.c

applying [2/14] https://archives.passt.top/passt-dev/20260421062516.2601204-11-david@gibson.dropbear.id.au/
diff --git a/pesto.c b/pesto.c
index 9f2fa5d5..f0916e82 100644

Checking patch pesto.c...
Applied patch pesto.c cleanly.

skipping https://archives.passt.top/passt-dev/20260421044217.2500314-10-david@gibson.dropbear.id.au/ for f0916e82
index at:
100644 f0916e82bd78d93f008882dcff67f1210e5c96eb	pesto.c

applying [3/14] https://archives.passt.top/passt-dev/20260421044217.2500314-11-david@gibson.dropbear.id.au/
diff --git a/pesto.c b/pesto.c
index f0916e82..5e191283 100644

Checking patch pesto.c...
Applied patch pesto.c cleanly.

skipping https://archives.passt.top/passt-dev/20260421062516.2601204-12-david@gibson.dropbear.id.au/ for 5e191283
index at:
100644 5e1912833db6e6a36176399793021e5f43d8dd21	pesto.c

applying [4/14] https://archives.passt.top/passt-dev/20260421044217.2500314-12-david@gibson.dropbear.id.au/
diff --git a/pesto.c b/pesto.c
index 5e191283..3e34bbac 100644

Checking patch pesto.c...
Applied patch pesto.c cleanly.

skipping https://archives.passt.top/passt-dev/20260421062516.2601204-13-david@gibson.dropbear.id.au/ for 3e34bbac
index at:
100644 3e34bbac62a66edbc8cb9c24a6339916e6fd2e98	pesto.c

applying [5/14] https://archives.passt.top/passt-dev/20260421062516.2601204-16-david@gibson.dropbear.id.au/
diff --git a/pesto.c b/pesto.c
index 3e34bbac..35a4d559 100644

5:87: trailing whitespace.
	
Checking patch pesto.c...
Applied patch pesto.c cleanly.
warning: 1 line adds whitespace errors.

skipping https://archives.passt.top/passt-dev/20260421044217.2500314-15-david@gibson.dropbear.id.au/ for 35a4d559
index at:
100644 35a4d559ea9584d66e8f6c184e2d73fad9913c47	pesto.c

applying [6/14] https://archives.passt.top/passt-dev/20260421062516.2601204-17-david@gibson.dropbear.id.au/
diff --git a/pesto.c b/pesto.c
index 35a4d559..ebac6bd6 100644

3:177: trailing whitespace.
			       
Checking patch pesto.c...
Applied patch pesto.c cleanly.
warning: 1 line adds whitespace errors.

skipping https://archives.passt.top/passt-dev/20260421044217.2500314-16-david@gibson.dropbear.id.au/ for ebac6bd6
index at:
100644 ebac6bd620fe9444980bb43b5cd84a71286fe4d5	pesto.c

applying [7/14] https://archives.passt.top/passt-dev/20260421062516.2601204-18-david@gibson.dropbear.id.au/
diff --git a/pesto.c b/pesto.c
index ebac6bd6..c6c595a4 100644

Checking patch pesto.c...
Applied patch pesto.c cleanly.

skipping https://archives.passt.top/passt-dev/20260421044217.2500314-17-david@gibson.dropbear.id.au/ for c6c595a4
index at:
100644 c6c595a47f7cd23127227449b95fae8f68a720f9	pesto.c

Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).