-
Notifications
You must be signed in to change notification settings - Fork 19
/
nuse-vif-tap.c
165 lines (133 loc) · 2.89 KB
/
nuse-vif-tap.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
/*
* tun/tap based virtual network interface feature for NUSE
* Copyright (c) 2015 Ryo Nakamura
*
* Author: Ryo Nakamura <upa@wide.ad.jp>
* Hajime Tazaki <tazaki@sfc.wide.ad.jp>
*/
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <linux/if_tun.h>
#include <linux/if_ether.h>
#include <linux/route.h>
#include <sys/ioctl.h>
#include "nuse-hostcalls.h"
#include "nuse-vif.h"
#include "nuse.h"
static int
tap_alloc(const char *dev)
{
/* create tunnel interface */
int fd;
struct ifreq ifr;
fd = host_open("/dev/net/tun", O_RDWR, 0);
if (fd < 0) {
printf("failed to open /dev/tun\n");
return -1;
}
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
snprintf(ifr.ifr_name, IFNAMSIZ, "%s", dev);
if (host_ioctl(fd, TUNSETIFF, (void *)&ifr) < 0) {
printf("failed to create tap interface \"%s\"\n",
ifr.ifr_name);
return -1;
}
printf("%s: alloc tap interface %s\n", __func__, dev);
return fd;
}
int
tap_up(char *dev)
{
int udp_fd;
struct ifreq ifr;
/* Make Tunnel interface up state */
udp_fd = host_socket(AF_INET, SOCK_DGRAM, 0);
if (udp_fd < 0) {
printf("failt to create control socket of tap interface.\n");
return -1;
}
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = IFF_UP;
strncpy(ifr.ifr_name, dev, IFNAMSIZ);
if (host_ioctl(udp_fd, SIOCSIFFLAGS, (void *)&ifr) < 0) {
printf("failed to make %s up.\n", dev);
host_close(udp_fd);
return -1;
}
host_close(udp_fd);
return 1;
}
void
nuse_vif_tap_read(struct nuse_vif *vif, struct SimDevice *dev)
{
int sock = vif->sock;
char buf[8192];
ssize_t size;
while (1) {
size = host_read(sock, buf, sizeof(buf));
if (size < 0) {
perror("read");
host_close(sock);
return;
} else if (size == 0) {
host_close(sock);
return;
}
nuse_dev_rx(dev, buf, size);
}
}
void
nuse_vif_tap_write(struct nuse_vif *vif, struct SimDevice *dev,
unsigned char *data, int len)
{
int sock = vif->sock;
int ret = host_write(sock, data, len);
if (ret == -1)
perror("write");
}
void *
nuse_vif_tap_create(const char *ifname)
{
int sock;
char dev[IFNAMSIZ];
struct nuse_vif *vif;
snprintf(dev, sizeof(dev), "nuse-%s", ifname);
sock = tap_alloc(dev);
if (sock < 0) {
perror("tap_alloc");
return NULL;
}
if (tap_up(dev) < 0) {
perror("tap_up");
return NULL;
}
vif = malloc(sizeof(struct nuse_vif));
vif->sock = sock;
vif->type = NUSE_VIF_TAP;
return vif;
}
void
nuse_vif_tap_delete(struct nuse_vif *vif)
{
int sock = vif->sock;
free(vif);
host_close(sock);
}
static struct nuse_vif_impl nuse_vif_tap = {
.read = nuse_vif_tap_read,
.write = nuse_vif_tap_write,
.create = nuse_vif_tap_create,
.delete = nuse_vif_tap_delete,
};
extern struct nuse_vif_impl *nuse_vif[NUSE_VIF_MAX];
int __attribute__((constructor))
nuse_vif_tap_init(void)
{
nuse_vif[NUSE_VIF_TAP] = &nuse_vif_tap;
return 0;
}