-
Notifications
You must be signed in to change notification settings - Fork 3
/
tap.c
131 lines (101 loc) · 3.34 KB
/
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
#include <stdlib.h>
#include <malloc.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <linux/if_tun.h>
#define NETSTACK_LOG_UNIT "TAP"
#include <netstack/log.h>
#include <netstack/intf/tap.h>
#include <netstack/eth/ether.h>
#include <netstack/api/socket.h>
int tap_new(struct intf *interface) {
if (interface == NULL)
return EINVAL;
int fd;
if ((fd = open("/dev/net/tun", O_RDWR)) < 0) {
return fd;
}
struct ifreq req = {0};
// IFF_TUN or IFF_TAP, plus maybe IFF_NO_PI
req.ifr_flags = IFF_TAP | IFF_NO_PI;
// TODO: Allow TUN/TAP name configuration
char *devname = "netstack";
strncpy(req.ifr_name, devname, IFNAMSIZ);
if (sys_ioctl(fd, TUNSETIFF, &req)) {
LOGERR("ioctl TUNSETIFF");
close(fd);
return errno;
}
memset(&req, 0, sizeof(struct ifreq));
strncpy(req.ifr_name, devname, IFNAMSIZ);
if (sys_ioctl(fd, SIOCGIFMTU, &req)) {
LOGERR("ioctl SIOCGIFMTU");
// close(fd);
// return errno;
}
// TODO: Fix TUN/TAP SIOCGIFMTU: EINVAL
// int mtu = req.ifr_mtu;
int mtu = 1500;
LOG(LINFO, "Using interface (#%d) %s, mtu %d",
req.ifr_ifindex, devname, mtu);
if (sys_ioctl(fd, SIOCGIFHWADDR, &req) < 0) {
LOGERR("ioctl SIOCGIFHWADDR");
return errno;
}
uint8_t *hwaddr = malloc(ETH_ADDR_LEN);
for (int i = 0; i < ETH_ADDR_LEN; ++i)
hwaddr[i] = (uint8_t) req.ifr_hwaddr.sa_data[i];
int *ll = malloc(sizeof(int));
*ll = fd;
interface->ll = ll;
interface->ll_addr = hwaddr;
interface->mtu = (size_t) mtu;
// Zero then copy interface name
memset(interface->name, 0, IFNAMSIZ);
strncpy(interface->name, devname, IFNAMSIZ);
interface->type = INTF_TAP;
interface->proto = PROTO_ETHER;
interface->free = tap_free;
interface->recv_frame = tap_recv_frame;
interface->send_frame = tap_send_frame;
interface->new_buffer = intf_malloc_buffer;
interface->free_buffer = intf_free_buffer;
return 0;
}
void tap_free(struct intf *intf) {
}
long tap_recv_frame(struct frame *frame) {
struct intf *interface = frame->intf;
// Count is raw eth packet size (inc eth + ip + trans:port)
ssize_t count = 0;
int sock = *((int *) interface->ll);
// TODO: Check for IFF_PI and allocate space for it
size_t size = interface->mtu + sizeof(struct eth_hdr_vlan) + 4;
frame_init_buf(frame, malloc(size), size);
frame->data = frame->buffer;
// Allow cancellation around peek() as this is the main blocking call
pthread_cleanup_push((void (*)(void *)) frame_decref, frame) ;
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
if ((count = sys_read(sock, frame->buffer, frame->buf_sz)) == -1)
return (int) count;
// Don't allow cancellation from here onwards
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
pthread_cleanup_pop(false);
return count;
}
long tap_send_frame(struct frame *frame) {
struct intf *interface = frame->intf;
int sock = *((int *) interface->ll);
ssize_t count = 0;
size_t len = frame_pkt_len(frame);
if ((count = sys_write(sock, frame->head, len)) == -1) {
LOGERR("write");
return errno;
}
if (count != len)
LOG(LWARN, "write() -> count != len");
return 0;
}