proxy.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <stdarg.h>
  4. #include <memory.h>
  5. #include <assert.h>
  6. #include <errno.h>
  7. #include <poll.h>
  8. #include <unistd.h>
  9. #include <sys/types.h>
  10. #include <arpa/inet.h>
  11. #include <netinet/in.h>
  12. #include <linux/ip.h>
  13. #include <linux/icmp.h>
  14. #define CMD_DEFINE
  15. #include "proxy.h"
  16. #define MTU 1500
  17. struct ip_net {
  18. in_addr_t ip;
  19. in_addr_t mask;
  20. };
  21. struct route_entry {
  22. struct ip_net dst;
  23. struct sockaddr_in next_hop;
  24. };
  25. typedef struct icmp_pkt {
  26. struct iphdr iph;
  27. struct icmphdr icmph;
  28. /* dest unreachable must include IP hdr 8 bytes of upper layer proto
  29. * of the original packet. */
  30. char data[sizeof(struct iphdr) + MAX_IPOPTLEN + 8];
  31. } __attribute__ ((aligned (4))) icmp_pkt;
  32. /* we calc hdr checksums using 32bit uints that can alias other types */
  33. typedef uint32_t __attribute__((__may_alias__)) aliasing_uint32_t;
  34. struct route_entry *routes;
  35. size_t routes_alloc;
  36. size_t routes_cnt;
  37. in_addr_t tun_addr;
  38. int log_enabled;
  39. int exit_flag;
  40. static inline in_addr_t netmask(int prefix_len) {
  41. return htonl(~((uint32_t)0) << (32 - prefix_len));
  42. }
  43. static inline int contains(struct ip_net net, in_addr_t ip) {
  44. return net.ip == (ip & net.mask);
  45. }
  46. static void log_error(const char *fmt, ...) {
  47. va_list ap;
  48. if( log_enabled ) {
  49. va_start(ap, fmt);
  50. vfprintf(stderr, fmt, ap);
  51. va_end(ap);
  52. }
  53. }
  54. /* fast version -- only works with mults of 4 bytes */
  55. uint16_t cksum(aliasing_uint32_t *buf, int len) {
  56. uint32_t sum = 0;
  57. uint16_t t1, t2;
  58. for( ; len > 0; len-- ) {
  59. uint32_t s = *buf++;
  60. sum += s;
  61. if( sum < s )
  62. sum++;
  63. }
  64. /* Fold down to 16 bits */
  65. t1 = sum;
  66. t2 = sum >> 16;
  67. t1 += t2;
  68. if( t1 < t2 )
  69. t1++;
  70. return ~t1;
  71. }
  72. static void send_net_unreachable(int tun, char *offender) {
  73. icmp_pkt pkt;
  74. int off_iph_len;
  75. struct iphdr *off_iph = (struct iphdr *)offender;
  76. size_t pktlen, nsent;
  77. off_iph_len = off_iph->ihl * 4;
  78. if( off_iph_len >= sizeof(struct iphdr) + MAX_IPOPTLEN ) {
  79. log_error("not sending net unreachable: mulformed ip pkt: iph=%d\n", (int)off_iph_len);
  80. return; /* ip pkt mulformed */
  81. }
  82. if( off_iph->protocol == IPPROTO_ICMP ) {
  83. /* To avoid infinite loops, RFC 792 instructs not to send ICMPs
  84. * about ICMPs */
  85. return;
  86. }
  87. /* Lower 3 bits (in network order) of frag_off is actually flags */
  88. if( (off_iph->frag_off & htons(0x1FFF)) != 0 ) {
  89. /* ICMP messages are only sent for first fragemnt */
  90. return;
  91. }
  92. pktlen = sizeof(struct iphdr) + sizeof(struct icmphdr) + off_iph_len + 8;
  93. memset(&pkt, 0, sizeof(pkt));
  94. /* Fill in the IP header */
  95. pkt.iph.ihl = sizeof(struct iphdr) / 4;
  96. pkt.iph.version = IPVERSION;
  97. pkt.iph.tot_len = htons(pktlen);
  98. pkt.iph.ttl = 8;
  99. pkt.iph.protocol = IPPROTO_ICMP;
  100. pkt.iph.saddr = tun_addr;
  101. pkt.iph.daddr = off_iph->saddr;
  102. pkt.iph.check = cksum((aliasing_uint32_t*) &pkt.iph, sizeof(struct iphdr) / sizeof(aliasing_uint32_t));
  103. /* Fill in the ICMP header */
  104. pkt.icmph.type = ICMP_DEST_UNREACH;
  105. pkt.icmph.code = ICMP_NET_UNREACH;
  106. /* Copy the offenders IP hdr + first 8 bytes of IP payload */
  107. memcpy(pkt.data, offender, off_iph_len + 8);
  108. /* Compute the checksum over the ICMP header and data */
  109. pkt.icmph.checksum = cksum((aliasing_uint32_t*) &pkt.icmph,
  110. (sizeof(struct icmphdr) + off_iph_len + 8) / sizeof(aliasing_uint32_t));
  111. /* Kick it back */
  112. nsent = write(tun, &pkt, pktlen);
  113. if( nsent < 0 ) {
  114. log_error("failed to send ICMP net unreachable: %s\n", strerror(errno));
  115. } else if( nsent != pktlen ) {
  116. log_error("failed to send ICMP net unreachable: only %d out of %d byte sent\n", (int)nsent, (int)pktlen);
  117. }
  118. }
  119. static int set_route(struct ip_net dst, struct sockaddr_in *next_hop) {
  120. size_t i;
  121. for( i = 0; i < routes_cnt; i++ ) {
  122. if( dst.ip == routes[i].dst.ip && dst.mask == routes[i].dst.mask ) {
  123. routes[i].next_hop = *next_hop;
  124. return 0;
  125. }
  126. }
  127. if( routes_alloc == routes_cnt ) {
  128. int new_alloc = (routes_alloc ? 2*routes_alloc : 8);
  129. struct route_entry *new_routes = (struct route_entry *) realloc(routes, new_alloc);
  130. if( !new_routes )
  131. return ENOMEM;
  132. routes = new_routes;
  133. routes_alloc = new_alloc;
  134. }
  135. routes[routes_cnt].dst = dst;
  136. routes[routes_cnt].next_hop = *next_hop;
  137. routes_cnt++;
  138. return 0;
  139. }
  140. static int del_route(struct ip_net dst) {
  141. size_t i;
  142. for( i = 0; i < routes_cnt; i++ ) {
  143. if( dst.ip == routes[i].dst.ip && dst.mask == routes[i].dst.mask ) {
  144. routes[i] = routes[routes_cnt-1];
  145. routes_cnt--;
  146. return 0;
  147. }
  148. }
  149. return ENOENT;
  150. }
  151. static struct sockaddr_in *find_route(in_addr_t dst) {
  152. size_t i;
  153. for( i = 0; i < routes_cnt; i++ ) {
  154. if( contains(routes[i].dst, dst) ) {
  155. // packets for same dest tend to come in bursts. swap to front make it faster for subsequent ones
  156. if( i != 0 ) {
  157. struct route_entry tmp = routes[i];
  158. routes[i] = routes[0];
  159. routes[0] = tmp;
  160. }
  161. return &routes[0].next_hop;
  162. }
  163. }
  164. return NULL;
  165. }
  166. static char *inaddr_str(in_addr_t a, char *buf, size_t len) {
  167. struct in_addr addr;
  168. addr.s_addr = a;
  169. strncpy(buf, inet_ntoa(addr), len);
  170. buf[len-1] = '\0';
  171. return buf;
  172. }
  173. static ssize_t tun_recv_packet(int tun, char *buf, size_t buflen) {
  174. ssize_t nread = read(tun, buf, buflen);
  175. if( nread < sizeof(struct iphdr) ) {
  176. if( nread < 0 ) {
  177. log_error("TUN recv failed: %s\n", strerror(errno));
  178. } else {
  179. log_error("TUN recv packet too small: %d bytes\n", (int)nread);
  180. }
  181. return -1;
  182. }
  183. return nread;
  184. }
  185. static ssize_t sock_recv_packet(int sock, char *buf, size_t buflen) {
  186. ssize_t nread = recv(sock, buf, buflen, 0);
  187. if( nread < sizeof(struct iphdr) ) {
  188. if( nread < 0 ) {
  189. log_error("UDP recv failed: %s\n", strerror(errno));
  190. } else {
  191. log_error("UDP recv packet too small: %d bytes\n", (int)nread);
  192. }
  193. return -1;
  194. }
  195. return nread;
  196. }
  197. static void sock_send_packet(int sock, char *pkt, size_t pktlen, struct sockaddr_in *dst) {
  198. ssize_t nsent = sendto(sock, pkt, pktlen, 0, (struct sockaddr *)dst, sizeof(struct sockaddr_in));
  199. if( nsent != pktlen ) {
  200. if( nsent < 0 ) {
  201. log_error("UDP send to %s:%hu failed: %s\n",
  202. inet_ntoa(dst->sin_addr), ntohs(dst->sin_port), strerror(errno));
  203. } else {
  204. log_error("Was only able to send %d out of %d bytes to %s:%hu\n",
  205. (int)nsent, (int)pktlen, inet_ntoa(dst->sin_addr), ntohs(dst->sin_port));
  206. }
  207. }
  208. }
  209. static void tun_send_packet(int tun, char *pkt, size_t pktlen) {
  210. ssize_t nsent = write(tun, pkt, pktlen);
  211. if( nsent != pktlen ) {
  212. if( nsent < 0 ) {
  213. log_error("TUN send failed: %s\n", strerror(errno));
  214. } else {
  215. log_error("Was only able to send %d out of %d bytes to TUN\n", (int)nsent, (int)pktlen);
  216. }
  217. }
  218. }
  219. inline static int decrement_ttl(struct iphdr *iph) {
  220. if( --(iph->ttl) == 0 ) {
  221. char saddr[32], daddr[32];
  222. log_error("Discarding IP fragment %s -> %s due to zero TTL\n",
  223. inaddr_str(iph->saddr, saddr, sizeof(saddr)),
  224. inaddr_str(iph->daddr, daddr, sizeof(daddr)));
  225. return 0;
  226. }
  227. /* patch up IP checksum (see RFC 1624) */
  228. if( iph->check >= htons(0xFFFFu - 0x100) ) {
  229. iph->check += htons(0x100) + 1;
  230. } else {
  231. iph->check += htons(0x100);
  232. }
  233. return 1;
  234. }
  235. static void tun_to_udp(int tun, int sock) {
  236. char buf[MTU] __attribute__ ((aligned (4)));
  237. struct iphdr *iph;
  238. struct sockaddr_in *next_hop;
  239. ssize_t pktlen = tun_recv_packet(tun, buf, MTU);
  240. if( pktlen < 0 )
  241. return;
  242. iph = (struct iphdr *)buf;
  243. next_hop = find_route((in_addr_t) iph->daddr);
  244. if( !next_hop ) {
  245. send_net_unreachable(tun, buf);
  246. return;
  247. }
  248. if( !decrement_ttl(iph) ) {
  249. /* TTL went to 0, discard.
  250. * TODO: send back ICMP Time Exceeded
  251. */
  252. return;
  253. }
  254. sock_send_packet(sock, buf, pktlen, next_hop);
  255. }
  256. static void udp_to_tun(int sock, int tun) {
  257. char buf[MTU] __attribute__ ((aligned (4)));
  258. struct iphdr *iph;
  259. ssize_t pktlen = recv(sock, buf, MTU, 0);
  260. if( pktlen < 0 ) {
  261. return;
  262. }
  263. iph = (struct iphdr *)buf;
  264. if( !decrement_ttl(iph) ) {
  265. /* TTL went to 0, discard.
  266. * TODO: send back ICMP Time Exceeded
  267. */
  268. return;
  269. }
  270. tun_send_packet(tun, buf, pktlen);
  271. }
  272. static void process_cmd(int ctl) {
  273. struct command cmd;
  274. struct ip_net ipn;
  275. struct sockaddr_in sa = {
  276. .sin_family = AF_INET
  277. };
  278. ssize_t nrecv = recv(ctl, (char *) &cmd, sizeof(cmd), 0);
  279. if( nrecv < 0 ) {
  280. log_error("CTL recv failed: %s\n", strerror(errno));
  281. return;
  282. }
  283. if( cmd.cmd == CMD_SET_ROUTE ) {
  284. ipn.mask = netmask(cmd.dest_net_len);
  285. ipn.ip = cmd.dest_net & ipn.mask;
  286. sa.sin_addr.s_addr = cmd.next_hop_ip;
  287. sa.sin_port = htons(cmd.next_hop_port);
  288. set_route(ipn, &sa);
  289. } else if( cmd.cmd == CMD_DEL_ROUTE ) {
  290. ipn.mask = netmask(cmd.dest_net_len);
  291. ipn.ip = cmd.dest_net & ipn.mask;
  292. del_route(ipn);
  293. } else if( cmd.cmd == CMD_STOP ) {
  294. exit_flag = 1;
  295. }
  296. }
  297. void run_proxy(int tun, int sock, int ctl, in_addr_t tun_ip, int log_errors) {
  298. struct pollfd fds[3] = {
  299. {
  300. .fd = tun,
  301. .events = POLLIN
  302. },
  303. {
  304. .fd = sock,
  305. .events = POLLIN
  306. },
  307. {
  308. .fd = ctl,
  309. .events = POLLIN
  310. },
  311. };
  312. exit_flag = 0;
  313. tun_addr = tun_ip;
  314. log_enabled = log_errors;
  315. while( !exit_flag ) {
  316. int nfds = poll(fds, 3, -1);
  317. if( nfds < 0 ) {
  318. log_error("Poll failed: %s\n", strerror(errno));
  319. exit(1);
  320. }
  321. if( fds[0].revents & POLLIN )
  322. tun_to_udp(tun, sock);
  323. if( fds[1].revents & POLLIN )
  324. udp_to_tun(sock, tun);
  325. if( fds[2].revents & POLLIN )
  326. process_cmd(ctl);
  327. }
  328. }