-
Notifications
You must be signed in to change notification settings - Fork 0
/
runnsctl.c
365 lines (330 loc) · 11 KB
/
runnsctl.c
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
/*
* vim:et:sw=2:
*
* Copyright (c) 2019-2024 Nikita (sh1r4s3) Ermakov <sh1r4s3@mail.si-head.nl>
* SPDX-License-Identifier: MIT
*/
#include "runns.h"
#include <arpa/inet.h>
#include <limits.h>
#include <strings.h>
#define CLIENT_NAME "runnsctl"
void cleanup();
/*
* Macros for logging
*/
#define ERR(format, ...) \
do { \
fprintf(stderr, CLIENT_NAME ":%d / errno=%d / " format "\n", __LINE__, errno, ##__VA_ARGS__); \
cleanup(); \
exit(0); \
} while (0)
#define WARN(format, ...) \
do { \
fprintf(stderr, CLIENT_NAME ":%d / warning / " format "\n", __LINE__, ##__VA_ARGS__); \
} while (0)
#define INFO(format, ...) \
do { \
printf(CLIENT_NAME ":%d / info / " format "\n", __LINE__, ##__VA_ARGS__); \
} while (0)
// This macro could be set from the configure script
#ifdef ENABLE_DEBUG
# define DEBUG(format, ...) \
do { \
printf(CLIENT_NAME ":%d / debug / " format "\n", __LINE__, ##__VA_ARGS__); \
} while (0)
#else
# define DEBUG(...)
#endif
enum wide_opts {OPT_SET_NETNS = 0xFF01, OPT_SOCKET = 0xFF02};
int netns_size = 0;
struct netns_list *ns_head = NULL;
int sockfd = 0;
struct runns_header hdr = {0};
const char *prog = 0, *netns = 0;
struct option opts[] = {
{ .name = "help", .has_arg = 0, .flag = 0, .val = 'h' },
{ .name = "program", .has_arg = 1, .flag = 0, .val = 'p' },
{ .name = "verbose", .has_arg = 0, .flag = 0, .val = 'v' },
{ .name = "stop", .has_arg = 0, .flag = 0, .val = 's' },
{ .name = "list", .has_arg = 0, .flag = 0, .val = 'l' },
{ .name = "create-ptms", .has_arg = 0, .flag = 0, .val = 't' },
{ .name = "forward-port", .has_arg = 1, .flag = 0, .val = 'f' },
{ .name = "set-netns", .has_arg = 1, .flag = 0, .val = OPT_SET_NETNS },
{ .name = "socket", .has_arg = 1, .flag = 0, .val = OPT_SOCKET },
{ 0, 0, 0, 0 }
};
extern char **environ;
void help_me() {
const char *hstr = \
"client [options] \n" \
"Options: \n" \
"-h|--help help\n" \
"-s|--stop stop daemon (only root)\n" \
"-l|--list list childs\n" \
"-p|--program <path> program to run in desired netns\n" \
"-t|--create-ptms create control terminal\n" \
"-f|--forward-port <ip>:<port>:<netns path>:<proto><ip family>\n" \
" <ip family> could be 4 or 6\n" \
" <netns path> path to the netns fd\n" \
"--set-netns <path> network namespace to switch\n" \
"--socket <path> path to the runns socket\n" \
"-v|--verbose be verbose\n";
puts(hstr);
exit(EXIT_SUCCESS);
}
void parse_l4_proto(char *l4_proto_str, L4_PROTOCOLS *l4_proto, sa_family_t *family) {
// Basic checks and calc the size of the str
if (!l4_proto_str || !l4_proto || !family) {
ERR("NULL slipped in 0x%x 0x%x 0x%x", l4_proto_str, l4_proto, family);
}
*l4_proto_str++ = '\0';
*family = AF_INET;
*l4_proto = L4_PROTOCOL_UNK;
size_t l4_proto_str_sz = strlen(l4_proto_str);
if (l4_proto_str_sz != 4) {
WARN("L4 protocol has wrong size %d != 4. Skip", l4_proto_str_sz);
return;
}
// Get IP family
switch (l4_proto_str[3]) {
case '4':
*family = AF_INET;
break;
case '6':
*family = AF_INET6;
break;
default:
WARN("L4 protocol has wrong IP family %c (only 4 and 6 are allowed). Fallback to 4", l4_proto_str[3]);
}
// Get proto
if (strncasecmp(l4_proto_str, "tcp", 3) == 0) {
*l4_proto = L4_PROTOCOL_TCP;
} else if (strncasecmp(l4_proto_str, "udp", 3) == 0) {
*l4_proto = L4_PROTOCOL_UDP;
} else {
WARN("L4 protocol %s is not correct", l4_proto_str);
}
DEBUG("l4_proto_str=%s l4_proto=%d family=%d", l4_proto_str, l4_proto, family);
}
void add_netns(char *ip) {
if (!ip) {
ERR("forward string is empty");
}
char *port = NULL, *netns_path = NULL;
size_t ip_len = strlen(ip);
port = strchr(ip, ENV_SEPARATOR);
if (port && (size_t)port < ((size_t)ip + ip_len))
netns_path = strchr(port + 1, ENV_SEPARATOR);
if (!port || !netns_path) { // Mandatory fields
WARN("skipping %s -- port and netns_path are mandatory fields", ip);
return;
}
*port++ = '\0';
*netns_path++ = '\0';
// An optional field for level 4 protocols according to the OSI model
char *l4_proto_str = strchr(netns_path + 1, ENV_SEPARATOR);
L4_PROTOCOLS l4_proto = L4_PROTOCOL_UNK;
sa_family_t family = AF_UNSPEC;
parse_l4_proto(l4_proto_str, &l4_proto, &family);
// Open a netns file to get a fd
int netns_fd = open(netns_path, 0);
if (netns_fd < 0) {
ERR("Can't open netns %s errno: %s", netns_path, strerror(errno));
}
// Create a new netns
struct netns_list *ns = (struct netns_list *)malloc(sizeof(struct netns_list));
inet_pton(family, ip, ns->node.ip);
ns->node.port = atoi(port);
ns->node.fd = netns_fd;
ns->node.family = family;
ns->node.proto = l4_proto;
ns->pnext = NULL;
DEBUG("adding port=%d ip=%s(0x%x), netns=%s, proto=%d", ns->node.port, ip, *((int *)ns->node.ip), ns->node.netns, ns->node.proto);
++netns_size;
// Insert into the list of netns
if (ns_head) {
struct netns_list *p;
for (p = ns_head; p != NULL; p = p->pnext);
p->pnext = ns;
} else {
ns_head = ns;
}
}
void cleanup() {
for (struct netns_list *p = ns_head; p != NULL;) {
struct netns_list *ptmp = p->pnext;
free(p);
p = ptmp;
}
if (sockfd)
close(sockfd);
}
void send_netns(int argc, char **argv) {
// TODO: either transer prog + netns or a list of netns
// this should depend on the current operation mode
// OP_MODE_FWD_PORT -- forward ports (a list of netns)
// OP_MODE_NETNS -- the "classic" mode to run prog in netns
if (write(sockfd, (void *)prog, hdr.prog_sz) == -1 ||
write(sockfd, (void *)netns, hdr.netns_sz) == -1) {
ERR("Can't send program name or network namespace name to the daemon");
}
// Transfer argv
if (hdr.args_sz > 0) {
for (int i = optind; i < argc; i++) {
size_t sz = strlen(argv[i]) + 1; // strlen + \0
if (write(sockfd, (void *)&sz, sizeof(size_t)) == -1 ||
write(sockfd, (void *)argv[i], sz) == -1) {
ERR("Can't send argv to the daemon");
}
}
}
// Transfer environment variables
for (int i = 0; i < hdr.env_sz; i++) {
size_t sz = strlen(environ[i]) + 1; // strlen + \0
if (write(sockfd, (void *)&sz, sizeof(size_t)) == -1 ||
write(sockfd, (void *)environ[i], sz) == -1) {
ERR("Can't send envs to the daemon");
}
}
int eof = 0;
if (write(sockfd, &eof, sizeof(int)) == -1)
ERR("Can't send EOF to the daemon");
}
// Don't define main() for unit tests
#ifndef TAU_TEST
int main(int argc, char **argv) {
struct sockaddr_un addr = {.sun_family = AF_UNIX, .sun_path = DEFAULT_RUNNS_SOCKET};
const char *optstring = "hp:vsltf:";
int opt, len;
char verbose = 0;
int ret = EXIT_SUCCESS;
// Parse command line options
if (argc <= 1)
ERR("For the help message try: runnsctl --help");
while ((opt = getopt_long(argc, argv, optstring, opts, 0)) != -1) {
switch (opt) {
case 'h':
help_me();
break;
case 's':
hdr.flag |= RUNNS_STOP;
break;
case 'l':
hdr.flag |= RUNNS_LIST;
break;
case 'p':
prog = optarg;
hdr.prog_sz = strlen(prog) + 1;
break;
case 't':
hdr.flag |= RUNNS_NPTMS;
break;
case 'f':
if (hdr.op_mode == OP_MODE_NETNS) {
ERR("--forward-port and --set-netns mutually exclusive");
}
hdr.op_mode = OP_MODE_FWD_PORT;
add_netns(optarg);
break;
case 'v':
verbose = 1;
break;
case OPT_SET_NETNS:
if (hdr.op_mode == OP_MODE_FWD_PORT) {
ERR("--forward-port and --set-netns mutually exclusive");
}
hdr.op_mode = OP_MODE_NETNS;
netns = optarg;
hdr.netns_sz = strlen(netns) + 1;
break;
case OPT_SOCKET:
len = strlen(optarg);
if (len >= RUNNS_MAXLEN)
ERR("Socket file name is too long > " STR_TOKEN(RUNNS_MAXLEN) "\n");
memcpy(addr.sun_path, optarg, len + 1);
break;
default:
ERR("Wrong option: %c", (char)opt);
}
}
// TODO: remove this debugging output
#if 0
if (hdr.op_mode == OP_MODE_FWD_PORT) {
for (struct netns *p = ns_head; p != NULL; p = p->pnext) {
INFO("0x%x %s %s %d %d", *((int *)p->ip), p->netns, p->family == AF_INET ? "AF_INET" : "AF_INET6", p->proto, p->port);
}
exit(0);
}
#endif
if (hdr.op_mode == OP_MODE_FWD_PORT && !ns_head) {
ERR("Nothing to forward");
}
if (hdr.op_mode == OP_MODE_NETNS && !hdr.flag && (!netns || !prog)) {
ERR("Please check that you set network namespace and program");
}
// Output parameters in the case of verbose option
if (netns && verbose) {
if (hdr.flag & (RUNNS_STOP | RUNNS_LIST)) { // flags related to runns
// daemon
char *str = NULL;
if (hdr.flag & RUNNS_STOP) str = "RUNNS_STOP";
if (hdr.flag & RUNNS_LIST) str = "RUNNS_LIST";
if (str)
printf("Command to runns daemon: %s\n", str);
} else {
printf("network namespace to switch is: %s\n"
"program to run: %s\n",
netns, prog);
}
}
// Count number of environment variables
for (hdr.env_sz = 0; environ[hdr.env_sz] != 0; ++hdr.env_sz);
// Up socket
sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
if (sockfd == -1) {
ERR("Something gone very wrong, socket = %d", sockfd);
cleanup();
return ret;
}
if (connect(sockfd, (const struct sockaddr *)&addr, sizeof(addr)) == -1) {
ERR("Can't connect to runns daemon");
cleanup();
return ret;
}
// Calculate number of non-options
hdr.args_sz = argc - optind;
// Get termios
tcgetattr(STDIN_FILENO, &hdr.tmode);
if (write(sockfd, (void *)&hdr, sizeof(hdr)) == -1)
ERR("Can't send header to the daemon");
// Stop daemon
if (hdr.flag & RUNNS_STOP) {
cleanup();
return ret;
}
// Print list of children and exit
if (hdr.flag & RUNNS_LIST) {
unsigned int childs_run;
struct runns_child child;
if (read(sockfd, (void *)&childs_run, sizeof(childs_run)) == -1)
ERR("Can't read number of childs from the daemon");
for (int i = 0; i < childs_run; i++) {
if (read(sockfd, (void *)&child, sizeof(child)) == -1)
ERR("Can't read child info from the daemon");
printf("%d\n", child.pid);
}
cleanup();
return ret;
}
switch (hdr.op_mode) {
case OP_MODE_NETNS:
send_netns(argc, argv);
break;
default:
ERR("Unknown OP_MODE: %d", hdr.op_mode);
}
cleanup();
return ret;
}
#endif