-
Notifications
You must be signed in to change notification settings - Fork 155
Expand file tree
/
Copy pathspt_module_net.c
More file actions
154 lines (138 loc) · 4.73 KB
/
spt_module_net.c
File metadata and controls
154 lines (138 loc) · 4.73 KB
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
/*
* Copyright (c) 2015-2019 Contributors as noted in the AUTHORS file
*
* This file is part of Solo5, a sandboxed execution environment.
*
* Permission to use, copy, modify, and/or distribute this software
* for any purpose with or without fee is hereby granted, provided
* that the above copyright notice and this permission notice appear
* in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
* WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
* AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* spt_module_net.c: Network device module.
*/
#include <assert.h>
#include <err.h>
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <seccomp.h>
#include <sys/epoll.h>
#include "../common/tap_attach.h"
#include "spt.h"
static bool module_in_use;
static int handle_cmdarg(char *cmdarg, struct mft *mft)
{
enum {
opt_net,
opt_net_mac
} which;
if (strncmp("--net:", cmdarg, 6) == 0)
which = opt_net;
else if (strncmp("--net-mac:", cmdarg, 10) == 0)
which = opt_net_mac;
else
return -1;
char name[MFT_NAME_SIZE];
char iface[20]; /* XXX should be IFNAMSIZ, needs extra header here */
int rc;
if (which == opt_net) {
rc = sscanf(cmdarg,
"--net:%" XSTR(MFT_NAME_MAX) "[A-Za-z0-9]="
"%19s", name, iface);
if (rc != 2)
return -1;
struct mft_entry *e = mft_get_by_name(mft, name, MFT_DEV_NET_BASIC,
NULL);
if (e == NULL) {
warnx("Resource not declared in manifest: '%s'", name);
return -1;
}
int fd = tap_attach(iface);
if (fd < 0) {
warnx("Could not attach interface: %s", iface);
return -1;
}
/* e->u.net_basic.mac[] is set either by option or generated later by
* setup().
*/
e->u.net_basic.mtu = 1500; /* TODO */
e->b.hostfd = fd;
e->attached = true;
module_in_use = true;
}
else if (which == opt_net_mac) {
uint8_t mac[6];
rc = sscanf(cmdarg,
"--net-mac:%" XSTR(MFT_NAME_MAX) "[A-Za-z0-9]="
"%02"SCNx8":%02"SCNx8":%02"SCNx8":"
"%02"SCNx8":%02"SCNx8":%02"SCNx8,
name,
&mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]);
if (rc != 7)
return -1;
struct mft_entry *e = mft_get_by_name(mft, name, MFT_DEV_NET_BASIC,
NULL);
if (e == NULL) {
warnx("Resource not declared in manifest: '%s'", name);
return -1;
}
memcpy(e->u.net_basic.mac, mac, sizeof mac);
}
return 0;
}
static int setup(struct spt *spt, struct mft *mft)
{
if (!module_in_use)
return 0;
for (unsigned i = 0; i != mft->entries; i++) {
if (mft->e[i].type != MFT_DEV_NET_BASIC || !mft->e[i].attached)
continue;
char no_mac[6] = { 0 };
if (memcmp(mft->e[i].u.net_basic.mac, no_mac, sizeof no_mac) == 0)
tap_attach_genmac(mft->e[i].u.net_basic.mac);
int rc;
struct epoll_event ev;
ev.events = EPOLLIN;
/*
* i is the manifest index, i.e. the solo5_handle_t and will be returned
* by epoll() as part of any received event.
*/
ev.data.u64 = i;
rc = epoll_ctl(spt->epollfd, EPOLL_CTL_ADD, mft->e[i].b.hostfd, &ev);
if (rc == -1)
err(1, "epoll_ctl(EPOLL_CTL_ADD, hostfd=%d) failed",
mft->e[i].b.hostfd);
rc = seccomp_rule_add(spt->sc_ctx, SCMP_ACT_ALLOW, SCMP_SYS(read), 1,
SCMP_A0(SCMP_CMP_EQ, mft->e[i].b.hostfd));
if (rc != 0)
errx(1, "seccomp_rule_add(read, fd=%d) failed: %s",
mft->e[i].b.hostfd, strerror(-rc));
rc = seccomp_rule_add(spt->sc_ctx, SCMP_ACT_ALLOW, SCMP_SYS(write), 1,
SCMP_A0(SCMP_CMP_EQ, mft->e[i].b.hostfd));
if (rc != 0)
errx(1, "seccomp_rule_add(write, fd=%d) failed: %s",
mft->e[i].b.hostfd, strerror(-rc));
}
return 0;
}
static char *usage(void)
{
return "--net:NAME=IFACE | @NN (attach tap at IFACE or at fd @NN as network NAME)\n"
" [ --net-mac:NAME=HWADDR ] (set HWADDR for network NAME)";
}
DECLARE_MODULE(net,
.setup = setup,
.handle_cmdarg = handle_cmdarg,
.usage = usage
)