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
| | // 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 "pesto.h"
#define die(...) \
do { \
FPRINTF(stderr, __VA_ARGS__); \
FPRINTF(stderr, "\n"); \
exit(EXIT_FAILURE); \
} while (0)
#define debug(...) \
do { \
if (verbosity > 1) { \
FPRINTF(stderr, __VA_ARGS__); \
FPRINTF(stderr, "\n"); \
} \
} while (0)
/**
* usage() - Print usage, exit with given status code
* @name: Executable name
* @f: Stream to print usage info to
* @status: Status code for exit(2)
*/
static void usage(const char *name, FILE *f, int status)
{
FPRINTF(f, "Usage: %s [OPTION]... PATH\n", name);
FPRINTF(f,
"\n"
" -v, --verbose Be more verbose\n"
" -q, --quiet Be less verbose\n"
" -h, --help Display this help message and exit\n"
" --version Show version and exit\n");
exit(status);
}
/**
* main() - Entry point and whole program with loop
* @argc: Argument count
* @argv: Arguments: socket path, operation, port specifiers
*
* Return: 0 on success, won't return on failure
*
* #syscalls:pesto connect write close exit_group fstat brk
* #syscalls:pesto socket s390x:socketcall i686:socketcall
* #syscalls:pesto recvfrom recvmsg arm:recv ppc64le:recv
* #syscalls:pesto sendto sendmsg arm:send ppc64le:send
*/
int main(int argc, char **argv)
{
const struct option options[] = {
{"quiet", no_argument, NULL, 'q' },
{"verbose", no_argument, NULL, 'v' },
{"help", no_argument, NULL, 'h' },
{"version", no_argument, NULL, 1 },
{ 0 },
};
struct sockaddr_un a = { AF_UNIX, "" };
const char *optstring = "vh";
struct pesto_hello hello;
struct sock_fprog prog;
int optname, ret, s;
uint32_t s_version;
int verbosity = 1;
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");
do {
optname = getopt_long(argc, argv, optstring, options, NULL);
switch (optname) {
case -1:
case 0:
break;
case 'h':
usage(argv[0], stdout, EXIT_SUCCESS);
break;
case 'q':
verbosity--;
break;
case 'v':
verbosity++;
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);
if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
die("Failed to create AF_UNIX socket: %s", strerror(errno));
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[optind]);
ret = connect(s, (struct sockaddr *)&a, sizeof(a));
if (ret < 0) {
die("Failed to connect to %s: %s",
a.sun_path, strerror(errno));
}
debug("Connected to passt/pasta control socket");
ret = seread_var(s, &hello);
if (ret < 0)
die("Couldn't read server greeting: %s", strerror(errno));
if (memcmp(hello.magic, PESTO_SERVER_MAGIC, sizeof(hello.magic)))
die("Bad magic number from server");
s_version = ntohl(hello.version);
debug("Server protocol version: %"PRIu32, s_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");
}
exit(0);
}
|