7 #include <netinet/in.h>
10 #include <linux/if_tun.h>
21 uint8_t reserved0_hi : 4;
25 uint8_t has_checksum : 1;
28 uint8_t reserved0_lo: 5;
30 uint16_t protocol_type;
33 int tun_open(const char *name) {
36 int fd = open("/dev/net/tun", O_RDWR);
38 perror("/dev/net/tun");
42 memset(&ifr, 0, sizeof(ifr));
43 ifr.ifr_flags = IFF_TUN;
44 strncpy(ifr.ifr_name, name, IFNAMSIZ);
46 int err = ioctl(fd, TUNSETIFF, &ifr);
48 perror("ioctl(TUNSETIFF)");
55 in6_addr get_addr(const char *str) {
57 if (inet_pton(AF_INET6, str, &ret) != 1) {
58 fprintf(stderr, "Could not parse %s\n", str);
69 bool operator> (const GREPacket &other) const {
70 return seq > other.seq;
76 class GREProtocol : public Protocol {
78 GREProtocol(const in6_addr &myaddr, const in6_addr &dst);
79 virtual void send_packet(uint16_t proto, const string &data);
80 virtual int fd() const;
81 void read_packet(Reorderer* sender);
89 class TUNProtocol : public Protocol {
91 TUNProtocol(const char *devname);
92 virtual void send_packet(uint16_t proto, const string &data);
93 virtual int fd() const;
94 void read_packet(Protocol* sender);
102 Reorderer(Protocol* sender);
103 void handle_packet(uint16_t proto, const string& data, int seq);
106 void send_packet(uint16_t proto, const string &data, bool silence);
111 priority_queue<GREPacket, vector<GREPacket>, greater<GREPacket>> packet_buffer;
115 GREProtocol::GREProtocol(const in6_addr &src, const in6_addr &dst)
118 memset(&dstaddr, 0, sizeof(dstaddr));
119 dstaddr.sin6_family = AF_INET6;
120 dstaddr.sin6_addr = dst;
122 sock = socket(AF_INET6, SOCK_RAW, IPPROTO_GRE);
128 sockaddr_in6 my_addr;
129 memset(&my_addr, 0, sizeof(my_addr));
130 my_addr.sin6_family = AF_INET6;
131 my_addr.sin6_addr = src;
132 if (bind(sock, (sockaddr *)&my_addr, sizeof(my_addr)) == -1) {
138 void GREProtocol::send_packet(uint16_t proto, const string &data)
141 gre_header *gre = (gre_header *)buf;
143 memset(gre, 0, sizeof(*gre));
146 gre->protocol_type = htons(proto);
148 char *ptr = buf + sizeof(*gre);
149 int seq_be = htonl(seq++);
150 memcpy(ptr, &seq_be, sizeof(seq_be));
151 ptr += sizeof(seq_be);
153 memcpy(ptr, data.data(), data.size());
155 if (sendto(sock, buf, data.size() + sizeof(seq_be) + sizeof(*gre), 0, (sockaddr *)&dstaddr, sizeof(dstaddr)) == -1) {
161 int GREProtocol::fd() const
166 TUNProtocol::TUNProtocol(const char *devname)
167 : tunfd(tun_open(devname)) {
170 void TUNProtocol::send_packet(uint16_t proto, const string &data)
176 memcpy(ptr, &flags, sizeof(flags));
177 ptr += sizeof(flags);
179 proto = htons(proto);
180 memcpy(ptr, &proto, sizeof(proto));
181 ptr += sizeof(proto);
183 memcpy(ptr, data.data(), data.size());
185 int len = sizeof(flags) + sizeof(proto) + data.size();
186 if (write(tunfd, buf, len) != len) {
192 int TUNProtocol::fd() const
197 Reorderer::Reorderer(Protocol* sender)
198 : sender(sender), last_seq(-1)
202 #define PACKET_BUFFER_SIZE 100
204 void Reorderer::handle_packet(uint16_t proto, const string& data, int seq)
206 bool silence = false;
207 if (packet_buffer.size() >= PACKET_BUFFER_SIZE) {
208 printf("Gave up waiting for packets [%d,%d>\n",
209 last_seq + 1, packet_buffer.top().seq);
211 last_seq = packet_buffer.top().seq - 1;
216 packet.proto = proto;
218 packet_buffer.push(packet);
220 while (!packet_buffer.empty() &&
221 (last_seq == -1 || packet_buffer.top().seq <= last_seq + 1)) {
222 int front_seq = packet_buffer.top().seq;
223 if (front_seq < last_seq + 1) {
224 printf("Duplicate packet or way out-of-order: seq=%d front_seq=%d\n",
225 front_seq, last_seq + 1);
229 //if (packet_buffer.size() > 1) {
230 // printf("seq=%d (REORDER %d)\n", front_seq, int(packet_buffer.size()));
232 // printf("seq=%d\n", front_seq);
234 const string &data = packet_buffer.top().data;
235 send_packet(packet_buffer.top().proto, data, silence);
237 last_seq = front_seq;
238 if (!silence && !packet_buffer.empty()) {
239 printf("Reordering with packet buffer size %d: seq=%d new_front_seq=%d\n", int(packet_buffer.size()), front_seq, packet_buffer.top().seq);
245 void Reorderer::send_packet(uint16_t proto, const string &data, bool silence)
247 if (data.size() == 1344) {
248 for (int i = 0; i < 7; ++i) {
249 const char *pkt = &data[i * 188 + 28];
250 int pid = (ntohl(*(uint32_t *)(pkt)) & 0x1fff00) >> 8;
255 int has_payload = pkt[3] & 0x10;
256 int cc = pkt[3] & 0xf;
258 int last_cc = ccs[pid];
259 if (!silence && cc != ((last_cc + 1) & 0xf)) {
260 printf("Pid %d discontinuity (expected %d, got %d)\n", pid, (last_cc + 1) & 0xf, cc);
266 sender->send_packet(proto, data);
269 void GREProtocol::read_packet(Reorderer *sender)
271 struct sockaddr_storage addr;
272 socklen_t addrlen = sizeof(addr);
274 int ret = recvfrom(sock, buf, sizeof(buf), 0, (struct sockaddr *)&addr, &addrlen);
279 if (addr.ss_family != AF_INET6) {
282 struct in6_addr *addr6 = &((struct sockaddr_in6 *)&addr)->sin6_addr;
283 if (memcmp(addr6, &dstaddr.sin6_addr, sizeof(*addr6)) != 0) {
287 gre_header* gre = (gre_header *)buf;
289 char* ptr = buf + sizeof(gre_header);
290 if (gre->has_checksum) {
298 seq = ntohl(*(uint32_t *)ptr);
302 //printf("gre packet: proto=%x\n", ntohs(gre->protocol_type));
304 sender->handle_packet(ntohs(gre->protocol_type), string(ptr, buf + ret), seq);
307 void TUNProtocol::read_packet(Protocol *sender)
310 int ret = read(tunfd, buf, sizeof(buf));
316 fprintf(stderr, "tunfd EOF\n");
321 uint16_t flags = *(uint16_t *)ptr;
323 uint16_t proto = ntohs(*(uint16_t *)ptr);
325 //fprintf(stderr, "tun packet: flags=%x proto=%x len=%d\n",
326 // flags, proto, ret - 4);
327 sender->send_packet(proto, string(ptr, buf + ret));
330 int main(int argc, char **argv)
332 in6_addr myaddr = get_addr(argv[1]);
333 in6_addr remoteaddr = get_addr(argv[2]);
334 GREProtocol gre(myaddr, remoteaddr);
335 TUNProtocol tun("tungre");
337 Reorderer tun_reorderer(&tun);
342 FD_SET(gre.fd(), &fds);
343 FD_SET(tun.fd(), &fds);
344 int ret = select(1024, &fds, NULL, NULL, NULL);
350 if (FD_ISSET(gre.fd(), &fds)) {
351 gre.read_packet(&tun_reorderer);
353 if (FD_ISSET(tun.fd(), &fds)) {
354 tun.read_packet(&gre);