-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvdelxcd.c
311 lines (278 loc) · 6.73 KB
/
vdelxcd.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
/*
* vdelxcd: vde daemon for linux containers
* Copyright (C) 2023 Renzo Davoli, Virtualsquare University of Bologna
*
* vxdlxcd 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.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <getopt.h>
#include <limits.h>
#include <poll.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <sys/wait.h>
#include <libvdeplug.h>
#include <fdprintf.h>
#include <log.h>
#include <passfd.h>
#include <vdeauth.h>
#define ETH_ALEN 6
#define ETH_HDRLEN (ETH_ALEN+ETH_ALEN+2)
static int fdcwd = -1;
static pid_t mypid;
struct sockaddr_un socket_sa = {AF_UNIX, ""};
static mode_t mode = 0;
void panic(char *msg) {
printlog(LOG_CRIT, "%s: %s", msg, strerror(errno));
exit(1);
}
void vdethread(int connfd) {
char vnl[PATH_MAX];
int tapfd = -1;
int n = read_fd(connfd, vnl, PATH_MAX, &tapfd);
if (n < 0 || tapfd < 0)
goto err;
char *safevnl = vdeauth_check(vnl);
if (safevnl == NULL) {
printlog(LOG_WARNING, "open interface permission denied: %s", vnl);
errno = EACCES;
goto err2;
}
printlog(LOG_INFO, "open interface: %s", safevnl);
VDECONN *vdeconn = vde_open(safevnl, "vdelxcd", NULL);
if (vdeconn == NULL)
goto err2;
int datafd = vde_datafd(vdeconn);
fdprintf(connfd, "%ld", 0);
close(connfd);
struct pollfd pfd[] = {{tapfd, POLLIN, 0}, {datafd, POLLIN, 0}};
for(;;) {
char *buf[VDE_MAXMTU];
int n = poll(pfd, 2, -1);
if (n < 0)
break;
if (pfd[0].revents & POLLERR || pfd[0].revents & POLLERR)
break;
if (pfd[0].revents & POLLIN) {
ssize_t len = read(tapfd, buf, VDE_MAXMTU);
if (len <= 0)
break;
vde_send(vdeconn, buf, len, 0);
}
if (pfd[1].revents & POLLIN) {
ssize_t len = vde_recv(vdeconn, buf, VDE_MAXMTU, 0);
if (len <= 0)
break;
if (len >= ETH_HDRLEN) {
if (write(tapfd, buf, len) != len)
break;
}
}
}
printlog(LOG_INFO, "close interface: %s", safevnl);
vde_close(vdeconn);
close(tapfd);
return;
err2:
close(tapfd);
err:
fdprintf(connfd, "%ld", errno);
close(connfd);
return;
}
static void ck_unlinkold(void) {
struct stat st;
int rv = stat(socket_sa.sun_path, &st);
if (rv == -1) {
if (errno == ENOENT)
return;
else
panic("can't create socket");
}
if (S_ISSOCK(st.st_mode)) {
unlink(socket_sa.sun_path);
} else {
errno = EEXIST;
panic("can't create socket");
}
}
int vdelxcd(void) {
int fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (fd < 0)
panic("socket");
ck_unlinkold();
if (bind(fd, (void *) &socket_sa, sizeof(socket_sa)) < 0)
panic("bind");
if (mode)
chmod(socket_sa.sun_path, mode);
if (listen(fd, 10) < 0)
panic("listen");
for (;;) {
int connfd = accept(fd, NULL, NULL);
switch (fork()) {
case 0:
close(fd);
vdethread(connfd);
exit(0);
default:
close(connfd);
break;
case -1:
panic("fork");
}
}
return 0;
}
/* signal handling */
static void terminate(int signum) {
pid_t pid = getpid();
if (pid == mypid) {
printlog(LOG_INFO, "(%d) leaving on signal %d", pid, signum);
unlink(socket_sa.sun_path);
}
exit(0);
}
static void cont_handler(int signum) {
switch (signum) {
case SIGCHLD: wait(NULL); break;
}
}
static void setsignals(void) {
struct sigaction action = {
.sa_handler = terminate
};
sigaction(SIGINT, &action, NULL);
sigaction(SIGTERM, &action, NULL);
struct sigaction cont = {
.sa_handler = cont_handler,
.sa_flags = SA_RESTART
};
sigaction(SIGHUP, &cont, NULL);
sigaction(SIGCHLD, &cont, NULL);
}
void save_pidfile(char *pidfile, int fdcwd)
{
int fd = openat(fdcwd, pidfile,
O_WRONLY | O_CREAT | O_EXCL,
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (fd < 0)
panic("save pid file");
fdprintf(fd, "%ld\n", (long int)mypid);
close(fd);
}
/* Main and command line args management */
void usage(char *progname)
{
fprintf(stderr,"Usage: %s [ OPTIONS ] SOCKET [ VNL ] \n"
"\t-f, --rcfile <conffile> configuration file\n"
"\t-d, --daemon daemon mone\n"
"\t-p, --pidfile <pidfile> save daemon's pid\n"
"\t-v, --verbose enable verbose mode\n"
"\t-m, --mode <MODE> set socket permission bits to MODE\n"
"\t-h, --help\n",
progname);
exit(1);
}
static char short_options[] = "hdvf:p:m:";
static struct option long_options[] = {
{"help", 0, 0, 'h'},
{"rcfile", 1, 0, 'f'},
{"daemon", 0, 0, 'd'},
{"pidfile", 1, 0, 'p'},
{"verbose", 0, 0, 'v'},
{"mode", 1, 0, 'm'},
{0,0,0,0}
};
static char arg_tags[] = "dvpm";
static union {
struct {
char *daemon;
char *verbose;
char *pidfile;
char *mode;
};
char *argv[sizeof(arg_tags)];
} args;
#ifndef _GNU_SOURCE
static inline char *strchrnul(const char *s, int c) {
while (*s && *s != c)
s++;
return (char *) s;
}
#endif
static inline int argindex(char tag) {
return strchrnul(arg_tags, tag) - arg_tags;
}
int main(int argc, char *argv[]) {
char *progname = basename(argv[0]);
int option_index;
char *default_vnl = NULL;
char *conffile = NULL;
while(1) {
int c;
if ((c = getopt_long (argc, argv, short_options,
long_options, &option_index)) < 0)
break;
switch (c) {
case 'f':
conffile = optarg;
break;
case -1:
case '?':
case 'h': usage(progname); break;
default: {
int index = argindex(c);
if (args.argv[index] == NULL)
args.argv[index] = optarg ? optarg : "";
}
break;
}
}
if (args.mode) {
mode = strtol(args.mode, NULL, 8);
if (mode == 0)
usage(progname);
}
if (argc == optind)
usage(progname);
snprintf(socket_sa.sun_path, sizeof(socket_sa.sun_path), "%s", argv[optind++]);
if (argc != optind)
default_vnl = argv[optind++];
if (argc != optind)
usage(progname);
if (conffile && vdeauth_parsercfile(conffile) < 0)
panic("load rc file");
if (default_vnl)
vdeauth_setdefault(default_vnl);
/* saves current path in cwd, because otherwise with daemon() we
* forget it */
if((fdcwd = open(".", O_PATH)) < 0)
panic("getcwd");
setsignals();
if (args.daemon && daemon(0, 0))
panic("daemon");
mypid = getpid();
if (args.pidfile)
save_pidfile(args.pidfile, fdcwd);
startlog(progname, args.daemon != NULL, args.verbose != NULL);
return vdelxcd();
}