-
Notifications
You must be signed in to change notification settings - Fork 0
/
inline.c
187 lines (142 loc) · 4.69 KB
/
inline.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
#include "inline.h"
#include "log.h"
#include <errno.h>
#include <string.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <net/ethernet.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <linux/if_packet.h>
int inline_run(const char *in_ifname, const char *out_ifname, const uint8_t *ether_dst, const rule_t *rules) {
int rfd, wfd, retval = 0;
rfd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_IP));
if (rfd < 0) {
log_fatal("input socket: %s\n", strerror(errno));
return rfd;
}
wfd = socket(AF_PACKET, SOCK_RAW, IPPROTO_RAW);
if (wfd < 0) {
log_fatal("output socket: %s\n", strerror(errno));
close(rfd);
return wfd;
}
uint8_t rbuf[0xffff + sizeof(struct ether_header)], wbuf[0xffff + sizeof(struct ether_header)];
// bind to interface to scope the sniffing.
int inindex = ifname2index(in_ifname);
if (inindex < 0) {
log_fatal("invalid input interface: %s\n", in_ifname);
retval = inindex;
goto end;
}
struct sockaddr_ll addr;
memset(&addr, 0, sizeof(struct sockaddr_ll));
addr.sll_family = AF_PACKET;
addr.sll_ifindex = inindex;
addr.sll_protocol = htons(ETH_P_IP);
if (bind(rfd, (struct sockaddr *) &addr, sizeof(struct sockaddr_ll)) < 0) {
log_fatal("bind input socket: %s\n", strerror(errno));
retval = -1;
goto end;
}
// pre-fill the outgoing sockaddr_ll.
int outindex = ifname2index(out_ifname);
if (outindex < 0) {
log_fatal("invalid output interface: %s\n", in_ifname);
retval = outindex;
goto end;
}
struct sockaddr_ll dst_addr;
memset(&dst_addr, 0, sizeof(struct sockaddr_ll));
dst_addr.sll_ifindex = outindex;
dst_addr.sll_halen = ETH_ALEN;
memcpy(dst_addr.sll_addr, ether_dst, ETH_ALEN);
// pre-fill the outgoing ether header.
struct ether_header *oethhdr = (struct ether_header *) wbuf;
memcpy(oethhdr->ether_dhost, ether_dst, ETH_ALEN);
get_eth_addr(outindex, oethhdr->ether_shost);
oethhdr->ether_type = htons(ETH_P_IP);
uint8_t *wptr = wbuf + sizeof(struct ether_header);
// enable promiscuous mode on input interface.
struct packet_mreq mr;
memset(&mr, 0, sizeof(struct packet_mreq));
mr.mr_ifindex = inindex;
mr.mr_type = PACKET_MR_PROMISC;
if (setsockopt(rfd, SOL_PACKET, PACKET_ADD_MEMBERSHIP, &mr, sizeof(mr)) < 0) {
log_fatal("setsockopt(): %s\n", strerror(errno));
retval = -1;
goto end;
}
// get scatter/gather ready.
struct sockaddr_ll src_addr;
struct iovec iov[1];
iov[0].iov_base = rbuf;
iov[0].iov_len = sizeof(rbuf);
struct msghdr message;
message.msg_name = &src_addr;
message.msg_namelen = sizeof(src_addr);
message.msg_iov = iov;
message.msg_iovlen = 1;
message.msg_control = 0;
message.msg_controllen = 0;
log_debug("listening on '%s', sending on '%s'...\n", in_ifname, out_ifname);
while (1) {
ssize_t len = recvmsg(rfd, &message, 0);
if (len < 0) {
log_fatal("recvmsg(): %s\n", strerror(errno));
retval = -1;
goto end;
}
struct ether_header *iethhdr = (struct ether_header *) rbuf;
if (iethhdr->ether_type != htons(ETH_P_IP)) {
continue;
}
uint8_t *rptr = (uint8_t *) (rbuf + sizeof(struct ether_header));
len = build_reply(rules, rptr, (size_t) len, wptr, 0xffff);
if (len <= 0) {
continue;
}
len = sendto(wfd, wbuf, (size_t) len + sizeof(struct ether_header), 0, (struct sockaddr *) &dst_addr, sizeof(dst_addr));
if (len < 0) {
log_error("error sending reply: %s\n", strerror(errno));
}
}
end:
close(wfd);
close(rfd);
return retval;
}
int ifname2index(const char *ifname) {
struct ifreq ifr;
int fd;
fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0) {
log_fatal("socket: %s\n", strerror(errno));
return fd;
}
memset(&ifr, 0, sizeof(struct ifreq));
strncpy(ifr.ifr_name, ifname, IFNAMSIZ - 1);
if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0) {
log_fatal("ioctl: %s\n", strerror(errno));
return -1;
}
close(fd);
return ifr.ifr_ifindex;
}
int get_eth_addr(int ifindex, uint8_t *addr) {
struct ifreq ifr;
int fd;
fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0) {
log_fatal("socket: %s\n", strerror(errno));
return fd;
}
ifr.ifr_ifindex = ifindex;
if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {
log_fatal("ioctl: %s\n", strerror(errno));
return -1;
}
close(fd);
memcpy(addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
return 0;
}