You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1410 lines
41 KiB

6 years ago
11 years ago
11 years ago
11 years ago
11 years ago
9 years ago
11 years ago
11 years ago
11 years ago
9 years ago
6 years ago
11 years ago
8 years ago
8 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
6 years ago
8 years ago
8 years ago
8 years ago
8 years ago
11 years ago
11 years ago
9 years ago
11 years ago
11 years ago
8 years ago
8 years ago
9 years ago
8 years ago
8 years ago
11 years ago
10 years ago
11 years ago
11 years ago
9 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
9 years ago
8 years ago
10 years ago
11 years ago
11 years ago
11 years ago
9 years ago
10 years ago
11 years ago
11 years ago
10 years ago
10 years ago
11 years ago
10 years ago
11 years ago
11 years ago
8 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
11 years ago
11 years ago
8 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
6 years ago
6 years ago
11 years ago
6 years ago
6 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
6 years ago
6 years ago
10 years ago
6 years ago
6 years ago
11 years ago
11 years ago
8 years ago
11 years ago
11 years ago
11 years ago
11 years ago
9 years ago
11 years ago
10 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
9 years ago
9 years ago
11 years ago
11 years ago
11 years ago
9 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
8 years ago
11 years ago
9 years ago
7 years ago
11 years ago
10 years ago
11 years ago
11 years ago
8 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
8 years ago
11 years ago
11 years ago
11 years ago
8 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
8 years ago
11 years ago
10 years ago
11 years ago
8 years ago
11 years ago
11 years ago
10 years ago
11 years ago
9 years ago
9 years ago
11 years ago
8 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
11 years ago
9 years ago
11 years ago
11 years ago
9 years ago
8 years ago
9 years ago
11 years ago
9 years ago
9 years ago
9 years ago
11 years ago
9 years ago
11 years ago
10 years ago
10 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
8 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
8 years ago
11 years ago
11 years ago
11 years ago
11 years ago
8 years ago
11 years ago
9 years ago
6 years ago
9 years ago
9 years ago
6 years ago
9 years ago
10 years ago
11 years ago
11 years ago
9 years ago
8 years ago
11 years ago
11 years ago
8 years ago
11 years ago
11 years ago
11 years ago
8 years ago
8 years ago
10 years ago
11 years ago
9 years ago
10 years ago
11 years ago
11 years ago
11 years ago
8 years ago
11 years ago
10 years ago
8 years ago
11 years ago
10 years ago
11 years ago
8 years ago
8 years ago
6 years ago
11 years ago
11 years ago
8 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
7 years ago
11 years ago
11 years ago
11 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
11 years ago
8 years ago
10 years ago
11 years ago
11 years ago
11 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
11 years ago
11 years ago
11 years ago
10 years ago
9 years ago
10 years ago
10 years ago
11 years ago
10 years ago
10 years ago
11 years ago
11 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
8 years ago
10 years ago
11 years ago
11 years ago
6 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
8 years ago
8 years ago
11 years ago
10 years ago
8 years ago
11 years ago
9 years ago
8 years ago
6 years ago
8 years ago
9 years ago
9 years ago
8 years ago
8 years ago
11 years ago
9 years ago
11 years ago
10 years ago
10 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
11 years ago
11 years ago
11 years ago
11 years ago
8 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
8 years ago
8 years ago
8 years ago
11 years ago
11 years ago
11 years ago
8 years ago
8 years ago
8 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
6 years ago
11 years ago
11 years ago
6 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
9 years ago
7 years ago
11 years ago
8 years ago
9 years ago
  1. /*
  2. * udprelay.c - Setup UDP relay for both client and server
  3. *
  4. * Copyright (C) 2013 - 2018, Max Lv <max.c.lv@gmail.com>
  5. *
  6. * This file is part of the shadowsocks-libev.
  7. *
  8. * shadowsocks-libev is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * shadowsocks-libev is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with shadowsocks-libev; see the file COPYING. If not, see
  20. * <http://www.gnu.org/licenses/>.
  21. */
  22. #include <sys/stat.h>
  23. #include <sys/types.h>
  24. #include <fcntl.h>
  25. #include <locale.h>
  26. #include <signal.h>
  27. #include <string.h>
  28. #include <strings.h>
  29. #include <time.h>
  30. #include <unistd.h>
  31. #ifndef __MINGW32__
  32. #include <arpa/inet.h>
  33. #include <errno.h>
  34. #include <netdb.h>
  35. #include <netinet/in.h>
  36. #include <pthread.h>
  37. #endif
  38. #ifdef HAVE_CONFIG_H
  39. #include "config.h"
  40. #endif
  41. #if defined(HAVE_SYS_IOCTL_H) && defined(HAVE_NET_IF_H) && defined(__linux__)
  42. #include <net/if.h>
  43. #include <sys/ioctl.h>
  44. #define SET_INTERFACE
  45. #endif
  46. #include <libcork/core.h>
  47. #include "utils.h"
  48. #include "netutils.h"
  49. #include "cache.h"
  50. #include "udprelay.h"
  51. #include "winsock.h"
  52. #ifdef MODULE_REMOTE
  53. #define MAX_UDP_CONN_NUM 512
  54. #else
  55. #define MAX_UDP_CONN_NUM 256
  56. #endif
  57. #ifdef MODULE_REMOTE
  58. #ifdef MODULE_
  59. #error "MODULE_REMOTE and MODULE_LOCAL should not be both defined"
  60. #endif
  61. #endif
  62. #ifndef EAGAIN
  63. #define EAGAIN EWOULDBLOCK
  64. #endif
  65. #ifndef EWOULDBLOCK
  66. #define EWOULDBLOCK EAGAIN
  67. #endif
  68. static void server_recv_cb(EV_P_ ev_io *w, int revents);
  69. static void remote_recv_cb(EV_P_ ev_io *w, int revents);
  70. static void remote_timeout_cb(EV_P_ ev_timer *watcher, int revents);
  71. static char *hash_key(const int af, const struct sockaddr_storage *addr);
  72. #ifdef MODULE_REMOTE
  73. static void resolv_free_cb(void *data);
  74. static void resolv_cb(struct sockaddr *addr, void *data);
  75. #endif
  76. static void close_and_free_remote(EV_P_ remote_ctx_t *ctx);
  77. static remote_ctx_t *new_remote(int fd, server_ctx_t *server_ctx);
  78. #ifdef __ANDROID__
  79. extern uint64_t tx;
  80. extern uint64_t rx;
  81. extern int vpn;
  82. extern void stat_update_cb();
  83. #endif
  84. extern int verbose;
  85. extern int reuse_port;
  86. #ifdef MODULE_REMOTE
  87. extern uint64_t tx;
  88. extern uint64_t rx;
  89. extern char *local_addr;
  90. #endif
  91. static int packet_size = DEFAULT_PACKET_SIZE;
  92. static int buf_size = DEFAULT_PACKET_SIZE * 2;
  93. static int server_num = 0;
  94. static server_ctx_t *server_ctx_list[MAX_REMOTE_NUM] = { NULL };
  95. const char* s_port = NULL;
  96. #ifndef __MINGW32__
  97. static int
  98. setnonblocking(int fd)
  99. {
  100. int flags;
  101. if (-1 == (flags = fcntl(fd, F_GETFL, 0))) {
  102. flags = 0;
  103. }
  104. return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
  105. }
  106. #endif
  107. #if defined(MODULE_REMOTE) && defined(SO_BROADCAST)
  108. static int
  109. set_broadcast(int socket_fd)
  110. {
  111. int opt = 1;
  112. return setsockopt(socket_fd, SOL_SOCKET, SO_BROADCAST, &opt, sizeof(opt));
  113. }
  114. #endif
  115. #ifdef SO_NOSIGPIPE
  116. static int
  117. set_nosigpipe(int socket_fd)
  118. {
  119. int opt = 1;
  120. return setsockopt(socket_fd, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof(opt));
  121. }
  122. #endif
  123. #ifdef MODULE_REDIR
  124. #ifndef IP_TRANSPARENT
  125. #define IP_TRANSPARENT 19
  126. #endif
  127. #ifndef IP_RECVORIGDSTADDR
  128. #ifdef IP_ORIGDSTADDR
  129. #define IP_RECVORIGDSTADDR IP_ORIGDSTADDR
  130. #else
  131. #define IP_RECVORIGDSTADDR 20
  132. #endif
  133. #endif
  134. #ifndef IPV6_RECVORIGDSTADDR
  135. #ifdef IPV6_ORIGDSTADDR
  136. #define IPV6_RECVORIGDSTADDR IPV6_ORIGDSTADDR
  137. #else
  138. #define IPV6_RECVORIGDSTADDR 74
  139. #endif
  140. #endif
  141. static int
  142. get_dstaddr(struct msghdr *msg, struct sockaddr_storage *dstaddr)
  143. {
  144. struct cmsghdr *cmsg;
  145. for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
  146. if (cmsg->cmsg_level == SOL_IP && cmsg->cmsg_type == IP_RECVORIGDSTADDR) {
  147. memcpy(dstaddr, CMSG_DATA(cmsg), sizeof(struct sockaddr_in));
  148. dstaddr->ss_family = AF_INET;
  149. return 0;
  150. } else if (cmsg->cmsg_level == SOL_IPV6 && cmsg->cmsg_type == IPV6_RECVORIGDSTADDR) {
  151. memcpy(dstaddr, CMSG_DATA(cmsg), sizeof(struct sockaddr_in6));
  152. dstaddr->ss_family = AF_INET6;
  153. return 0;
  154. }
  155. }
  156. return 1;
  157. }
  158. #endif
  159. #define HASH_KEY_LEN sizeof(struct sockaddr_storage) + sizeof(int)
  160. static char *
  161. hash_key(const int af, const struct sockaddr_storage *addr)
  162. {
  163. size_t addr_len = sizeof(struct sockaddr_storage);
  164. static char key[HASH_KEY_LEN];
  165. memset(key, 0, HASH_KEY_LEN);
  166. memcpy(key, &af, sizeof(int));
  167. memcpy(key + sizeof(int), (const uint8_t *)addr, addr_len);
  168. return key;
  169. }
  170. #if defined(MODULE_REDIR) || defined(MODULE_REMOTE)
  171. static int
  172. construct_udprelay_header(const struct sockaddr_storage *in_addr,
  173. char *addr_header)
  174. {
  175. int addr_header_len = 0;
  176. if (in_addr->ss_family == AF_INET) {
  177. struct sockaddr_in *addr = (struct sockaddr_in *)in_addr;
  178. size_t addr_len = sizeof(struct in_addr);
  179. addr_header[addr_header_len++] = 1;
  180. memcpy(addr_header + addr_header_len, &addr->sin_addr, addr_len);
  181. addr_header_len += addr_len;
  182. memcpy(addr_header + addr_header_len, &addr->sin_port, 2);
  183. addr_header_len += 2;
  184. } else if (in_addr->ss_family == AF_INET6) {
  185. struct sockaddr_in6 *addr = (struct sockaddr_in6 *)in_addr;
  186. size_t addr_len = sizeof(struct in6_addr);
  187. addr_header[addr_header_len++] = 4;
  188. memcpy(addr_header + addr_header_len, &addr->sin6_addr, addr_len);
  189. addr_header_len += addr_len;
  190. memcpy(addr_header + addr_header_len, &addr->sin6_port, 2);
  191. addr_header_len += 2;
  192. } else {
  193. return 0;
  194. }
  195. return addr_header_len;
  196. }
  197. #endif
  198. static int
  199. parse_udprelay_header(const char *buf, const size_t buf_len,
  200. char *host, char *port, struct sockaddr_storage *storage)
  201. {
  202. const uint8_t atyp = *(uint8_t *)buf;
  203. int offset = 1;
  204. // get remote addr and port
  205. if ((atyp & ADDRTYPE_MASK) == 1) {
  206. // IP V4
  207. size_t in_addr_len = sizeof(struct in_addr);
  208. if (buf_len >= in_addr_len + 3) {
  209. if (storage != NULL) {
  210. struct sockaddr_in *addr = (struct sockaddr_in *)storage;
  211. addr->sin_family = AF_INET;
  212. addr->sin_addr = *(struct in_addr *)(buf + offset);
  213. addr->sin_port = *(uint16_t *)(buf + offset + in_addr_len);
  214. }
  215. if (host != NULL) {
  216. inet_ntop(AF_INET, (const void *)(buf + offset),
  217. host, INET_ADDRSTRLEN);
  218. }
  219. offset += in_addr_len;
  220. }
  221. } else if ((atyp & ADDRTYPE_MASK) == 3) {
  222. // Domain name
  223. uint8_t name_len = *(uint8_t *)(buf + offset);
  224. if (name_len + 4 <= buf_len) {
  225. if (storage != NULL) {
  226. char tmp[257] = { 0 };
  227. struct cork_ip ip;
  228. memcpy(tmp, buf + offset + 1, name_len);
  229. if (cork_ip_init(&ip, tmp) != -1) {
  230. if (ip.version == 4) {
  231. struct sockaddr_in *addr = (struct sockaddr_in *)storage;
  232. inet_pton(AF_INET, tmp, &(addr->sin_addr));
  233. addr->sin_port = *(uint16_t *)(buf + offset + 1 + name_len);
  234. addr->sin_family = AF_INET;
  235. } else if (ip.version == 6) {
  236. struct sockaddr_in6 *addr = (struct sockaddr_in6 *)storage;
  237. inet_pton(AF_INET, tmp, &(addr->sin6_addr));
  238. addr->sin6_port = *(uint16_t *)(buf + offset + 1 + name_len);
  239. addr->sin6_family = AF_INET6;
  240. }
  241. }
  242. }
  243. if (host != NULL) {
  244. memcpy(host, buf + offset + 1, name_len);
  245. }
  246. offset += 1 + name_len;
  247. }
  248. } else if ((atyp & ADDRTYPE_MASK) == 4) {
  249. // IP V6
  250. size_t in6_addr_len = sizeof(struct in6_addr);
  251. if (buf_len >= in6_addr_len + 3) {
  252. if (storage != NULL) {
  253. struct sockaddr_in6 *addr = (struct sockaddr_in6 *)storage;
  254. addr->sin6_family = AF_INET6;
  255. addr->sin6_addr = *(struct in6_addr *)(buf + offset);
  256. addr->sin6_port = *(uint16_t *)(buf + offset + in6_addr_len);
  257. }
  258. if (host != NULL) {
  259. inet_ntop(AF_INET6, (const void *)(buf + offset),
  260. host, INET6_ADDRSTRLEN);
  261. }
  262. offset += in6_addr_len;
  263. }
  264. }
  265. if (offset == 1) {
  266. LOGE("[udp] invalid header with addr type %d", atyp);
  267. return 0;
  268. }
  269. if (port != NULL) {
  270. sprintf(port, "%d", ntohs(*(uint16_t *)(buf + offset)));
  271. }
  272. offset += 2;
  273. return offset;
  274. }
  275. static char *
  276. get_addr_str(const struct sockaddr *sa)
  277. {
  278. static char s[SS_ADDRSTRLEN];
  279. memset(s, 0, SS_ADDRSTRLEN);
  280. char addr[INET6_ADDRSTRLEN] = { 0 };
  281. char port[PORTSTRLEN] = { 0 };
  282. uint16_t p;
  283. switch (sa->sa_family) {
  284. case AF_INET:
  285. inet_ntop(AF_INET, &(((struct sockaddr_in *)sa)->sin_addr),
  286. addr, INET_ADDRSTRLEN);
  287. p = ntohs(((struct sockaddr_in *)sa)->sin_port);
  288. sprintf(port, "%d", p);
  289. break;
  290. case AF_INET6:
  291. inet_ntop(AF_INET6, &(((struct sockaddr_in6 *)sa)->sin6_addr),
  292. addr, INET6_ADDRSTRLEN);
  293. p = ntohs(((struct sockaddr_in *)sa)->sin_port);
  294. sprintf(port, "%d", p);
  295. break;
  296. default:
  297. strncpy(s, "Unknown AF", SS_ADDRSTRLEN);
  298. }
  299. int addr_len = strlen(addr);
  300. int port_len = strlen(port);
  301. memcpy(s, addr, addr_len);
  302. memcpy(s + addr_len + 1, port, port_len);
  303. s[addr_len] = ':';
  304. return s;
  305. }
  306. int
  307. create_remote_socket(int ipv6)
  308. {
  309. int remote_sock;
  310. if (ipv6) {
  311. // Try to bind IPv6 first
  312. struct sockaddr_in6 addr;
  313. memset(&addr, 0, sizeof(struct sockaddr_in6));
  314. addr.sin6_family = AF_INET6;
  315. addr.sin6_addr = in6addr_any;
  316. addr.sin6_port = 0;
  317. remote_sock = socket(AF_INET6, SOCK_DGRAM, 0);
  318. if (remote_sock == -1) {
  319. ERROR("[udp] cannot create socket");
  320. return -1;
  321. }
  322. #ifdef MODULE_REMOTE
  323. if (local_addr != NULL) {
  324. if (bind_to_address(remote_sock, local_addr) == -1) {
  325. ERROR("bind_to_address");
  326. FATAL("[udp] cannot bind remote");
  327. return -1;
  328. }
  329. } else {
  330. #endif
  331. if (bind(remote_sock, (struct sockaddr *)&addr, sizeof(addr)) != 0) {
  332. FATAL("[udp] cannot bind remote");
  333. return -1;
  334. }
  335. #ifdef MODULE_REMOTE
  336. }
  337. #endif
  338. } else {
  339. // Or else bind to IPv4
  340. struct sockaddr_in addr;
  341. memset(&addr, 0, sizeof(struct sockaddr_in));
  342. addr.sin_family = AF_INET;
  343. addr.sin_addr.s_addr = INADDR_ANY;
  344. addr.sin_port = 0;
  345. remote_sock = socket(AF_INET, SOCK_DGRAM, 0);
  346. if (remote_sock == -1) {
  347. ERROR("[udp] cannot create socket");
  348. return -1;
  349. }
  350. #ifdef MODULE_REMOTE
  351. if (local_addr != NULL) {
  352. if (bind_to_address(remote_sock, local_addr) == -1) {
  353. ERROR("bind_to_address");
  354. FATAL("[udp] cannot bind remote");
  355. return -1;
  356. }
  357. } else {
  358. #endif
  359. if (bind(remote_sock, (struct sockaddr *)&addr, sizeof(addr)) != 0) {
  360. FATAL("[udp] cannot bind remote");
  361. return -1;
  362. }
  363. #ifdef MODULE_REMOTE
  364. }
  365. #endif
  366. }
  367. return remote_sock;
  368. }
  369. int
  370. create_server_socket(const char *host, const char *port)
  371. {
  372. struct addrinfo hints;
  373. struct addrinfo *result, *rp, *ipv4v6bindall;
  374. int s, server_sock;
  375. memset(&hints, 0, sizeof(struct addrinfo));
  376. hints.ai_family = AF_UNSPEC; /* Return IPv4 and IPv6 choices */
  377. hints.ai_socktype = SOCK_DGRAM; /* We want a UDP socket */
  378. hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG; /* For wildcard IP address */
  379. hints.ai_protocol = IPPROTO_UDP;
  380. s = getaddrinfo(host, port, &hints, &result);
  381. if (s != 0) {
  382. LOGE("[udp] getaddrinfo: %s", gai_strerror(s));
  383. return -1;
  384. }
  385. if (result == NULL) {
  386. LOGE("[udp] cannot bind");
  387. return -1;
  388. }
  389. rp = result;
  390. /*
  391. * On Linux, with net.ipv6.bindv6only = 0 (the default), getaddrinfo(NULL) with
  392. * AI_PASSIVE returns 0.0.0.0 and :: (in this order). AI_PASSIVE was meant to
  393. * return a list of addresses to listen on, but it is impossible to listen on
  394. * 0.0.0.0 and :: at the same time, if :: implies dualstack mode.
  395. */
  396. if (!host) {
  397. ipv4v6bindall = result;
  398. /* Loop over all address infos found until a IPV6 address is found. */
  399. while (ipv4v6bindall) {
  400. if (ipv4v6bindall->ai_family == AF_INET6) {
  401. rp = ipv4v6bindall; /* Take first IPV6 address available */
  402. break;
  403. }
  404. ipv4v6bindall = ipv4v6bindall->ai_next; /* Get next address info, if any */
  405. }
  406. }
  407. for (/*rp = result*/; rp != NULL; rp = rp->ai_next) {
  408. server_sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
  409. if (server_sock == -1) {
  410. continue;
  411. }
  412. if (rp->ai_family == AF_INET6) {
  413. int ipv6only = host ? 1 : 0;
  414. setsockopt(server_sock, IPPROTO_IPV6, IPV6_V6ONLY, &ipv6only, sizeof(ipv6only));
  415. }
  416. int opt = 1;
  417. setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
  418. #ifdef SO_NOSIGPIPE
  419. set_nosigpipe(server_sock);
  420. #endif
  421. if (reuse_port) {
  422. int err = set_reuseport(server_sock);
  423. if (err == 0) {
  424. LOGI("udp port reuse enabled");
  425. }
  426. }
  427. #ifdef IP_TOS
  428. // Set QoS flag
  429. int tos = 46;
  430. setsockopt(server_sock, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
  431. #endif
  432. #ifdef MODULE_REDIR
  433. if (setsockopt(server_sock, SOL_IP, IP_TRANSPARENT, &opt, sizeof(opt))) {
  434. ERROR("[udp] setsockopt IP_TRANSPARENT");
  435. exit(EXIT_FAILURE);
  436. }
  437. if (rp->ai_family == AF_INET) {
  438. if (setsockopt(server_sock, SOL_IP, IP_RECVORIGDSTADDR, &opt, sizeof(opt))) {
  439. FATAL("[udp] setsockopt IP_RECVORIGDSTADDR");
  440. }
  441. } else if (rp->ai_family == AF_INET6) {
  442. if (setsockopt(server_sock, SOL_IPV6, IPV6_RECVORIGDSTADDR, &opt, sizeof(opt))) {
  443. FATAL("[udp] setsockopt IPV6_RECVORIGDSTADDR");
  444. }
  445. }
  446. #endif
  447. s = bind(server_sock, rp->ai_addr, rp->ai_addrlen);
  448. if (s == 0) {
  449. /* We managed to bind successfully! */
  450. break;
  451. } else {
  452. ERROR("[udp] bind");
  453. }
  454. close(server_sock);
  455. server_sock = -1;
  456. }
  457. freeaddrinfo(result);
  458. return server_sock;
  459. }
  460. remote_ctx_t *
  461. new_remote(int fd, server_ctx_t *server_ctx)
  462. {
  463. remote_ctx_t *ctx = ss_malloc(sizeof(remote_ctx_t));
  464. memset(ctx, 0, sizeof(remote_ctx_t));
  465. ctx->fd = fd;
  466. ctx->server_ctx = server_ctx;
  467. ctx->af = AF_UNSPEC;
  468. ev_io_init(&ctx->io, remote_recv_cb, fd, EV_READ);
  469. ev_timer_init(&ctx->watcher, remote_timeout_cb, server_ctx->timeout,
  470. server_ctx->timeout);
  471. return ctx;
  472. }
  473. server_ctx_t *
  474. new_server_ctx(int fd)
  475. {
  476. server_ctx_t *ctx = ss_malloc(sizeof(server_ctx_t));
  477. memset(ctx, 0, sizeof(server_ctx_t));
  478. ctx->fd = fd;
  479. ev_io_init(&ctx->io, server_recv_cb, fd, EV_READ);
  480. return ctx;
  481. }
  482. #ifdef MODULE_REMOTE
  483. struct query_ctx *
  484. new_query_ctx(char *buf, size_t len)
  485. {
  486. struct query_ctx *ctx = ss_malloc(sizeof(struct query_ctx));
  487. memset(ctx, 0, sizeof(struct query_ctx));
  488. ctx->buf = ss_malloc(sizeof(buffer_t));
  489. balloc(ctx->buf, len);
  490. memcpy(ctx->buf->data, buf, len);
  491. ctx->buf->len = len;
  492. return ctx;
  493. }
  494. void
  495. close_and_free_query(EV_P_ struct query_ctx *ctx)
  496. {
  497. if (ctx != NULL) {
  498. if (ctx->buf != NULL) {
  499. bfree(ctx->buf);
  500. ss_free(ctx->buf);
  501. }
  502. ss_free(ctx);
  503. }
  504. }
  505. #endif
  506. void
  507. close_and_free_remote(EV_P_ remote_ctx_t *ctx)
  508. {
  509. if (ctx != NULL) {
  510. ev_timer_stop(EV_A_ & ctx->watcher);
  511. ev_io_stop(EV_A_ & ctx->io);
  512. close(ctx->fd);
  513. ss_free(ctx);
  514. }
  515. }
  516. static void
  517. remote_timeout_cb(EV_P_ ev_timer *watcher, int revents)
  518. {
  519. remote_ctx_t *remote_ctx
  520. = cork_container_of(watcher, remote_ctx_t, watcher);
  521. if (verbose) {
  522. LOGI("[udp] connection timeout");
  523. }
  524. char *key = hash_key(remote_ctx->af, &remote_ctx->src_addr);
  525. cache_remove(remote_ctx->server_ctx->conn_cache, key, HASH_KEY_LEN);
  526. }
  527. #ifdef MODULE_REMOTE
  528. static void
  529. resolv_free_cb(void *data)
  530. {
  531. struct query_ctx *ctx = (struct query_ctx *)data;
  532. if (ctx->buf != NULL) {
  533. bfree(ctx->buf);
  534. ss_free(ctx->buf);
  535. }
  536. ss_free(ctx);
  537. }
  538. static void
  539. resolv_cb(struct sockaddr *addr, void *data)
  540. {
  541. struct query_ctx *query_ctx = (struct query_ctx *)data;
  542. struct ev_loop *loop = query_ctx->server_ctx->loop;
  543. if (addr == NULL) {
  544. LOGE("[udp] unable to resolve");
  545. } else {
  546. remote_ctx_t *remote_ctx = query_ctx->remote_ctx;
  547. int cache_hit = 0;
  548. // Lookup in the conn cache
  549. if (remote_ctx == NULL) {
  550. char *key = hash_key(AF_UNSPEC, &query_ctx->src_addr);
  551. cache_lookup(query_ctx->server_ctx->conn_cache, key, HASH_KEY_LEN, (void *)&remote_ctx);
  552. }
  553. if (remote_ctx == NULL) {
  554. int remotefd = create_remote_socket(addr->sa_family == AF_INET6);
  555. if (remotefd != -1) {
  556. setnonblocking(remotefd);
  557. #ifdef SO_BROADCAST
  558. set_broadcast(remotefd);
  559. #endif
  560. #ifdef SO_NOSIGPIPE
  561. set_nosigpipe(remotefd);
  562. #endif
  563. #ifdef IP_TOS
  564. // Set QoS flag
  565. int tos = 46;
  566. setsockopt(remotefd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
  567. #endif
  568. #ifdef SET_INTERFACE
  569. if (query_ctx->server_ctx->iface) {
  570. if (setinterface(remotefd, query_ctx->server_ctx->iface) == -1)
  571. ERROR("setinterface");
  572. }
  573. #endif
  574. remote_ctx = new_remote(remotefd, query_ctx->server_ctx);
  575. remote_ctx->src_addr = query_ctx->src_addr;
  576. remote_ctx->server_ctx = query_ctx->server_ctx;
  577. remote_ctx->addr_header_len = query_ctx->addr_header_len;
  578. memcpy(remote_ctx->addr_header, query_ctx->addr_header,
  579. query_ctx->addr_header_len);
  580. } else {
  581. ERROR("[udp] bind() error");
  582. }
  583. } else {
  584. cache_hit = 1;
  585. }
  586. if (remote_ctx != NULL) {
  587. if (addr->sa_family == AF_INET)
  588. memcpy(&remote_ctx->dst_addr, addr, sizeof(struct sockaddr_in));
  589. else
  590. memcpy(&remote_ctx->dst_addr, addr, sizeof(struct sockaddr_in6));
  591. size_t addr_len = get_sockaddr_len(addr);
  592. int s = sendto(remote_ctx->fd, query_ctx->buf->data, query_ctx->buf->len,
  593. 0, addr, addr_len);
  594. if (s == -1) {
  595. ERROR("[udp] sendto_remote");
  596. if (!cache_hit) {
  597. close_and_free_remote(EV_A_ remote_ctx);
  598. }
  599. } else {
  600. if (!cache_hit) {
  601. // Add to conn cache
  602. char *key = hash_key(AF_UNSPEC, &remote_ctx->src_addr);
  603. cache_insert(query_ctx->server_ctx->conn_cache, key, HASH_KEY_LEN, (void *)remote_ctx);
  604. ev_io_start(EV_A_ & remote_ctx->io);
  605. ev_timer_start(EV_A_ & remote_ctx->watcher);
  606. }
  607. }
  608. }
  609. }
  610. }
  611. #endif
  612. static void
  613. remote_recv_cb(EV_P_ ev_io *w, int revents)
  614. {
  615. ssize_t r;
  616. remote_ctx_t *remote_ctx = (remote_ctx_t *)w;
  617. server_ctx_t *server_ctx = remote_ctx->server_ctx;
  618. // server has been closed
  619. if (server_ctx == NULL) {
  620. LOGE("[udp] invalid server");
  621. close_and_free_remote(EV_A_ remote_ctx);
  622. return;
  623. }
  624. if (verbose) {
  625. LOGI("[udp] remote receive a packet");
  626. }
  627. struct sockaddr_storage src_addr;
  628. socklen_t src_addr_len = sizeof(struct sockaddr_storage);
  629. memset(&src_addr, 0, src_addr_len);
  630. buffer_t *buf = ss_malloc(sizeof(buffer_t));
  631. balloc(buf, buf_size);
  632. // recv
  633. r = recvfrom(remote_ctx->fd, buf->data, buf_size, 0, (struct sockaddr *)&src_addr, &src_addr_len);
  634. if (r == -1) {
  635. // error on recv
  636. // simply drop that packet
  637. ERROR("[udp] remote_recv_recvfrom");
  638. goto CLEAN_UP;
  639. } else if (r > packet_size) {
  640. if (verbose) {
  641. LOGI("[udp] remote_recv_recvfrom fragmentation, MTU at least be: " SSIZE_FMT, r + PACKET_HEADER_SIZE);
  642. }
  643. }
  644. buf->len = r;
  645. #ifdef MODULE_LOCAL
  646. int err = server_ctx->crypto->decrypt_all(buf, server_ctx->crypto->cipher, buf_size);
  647. if (err) {
  648. // drop the packet silently
  649. goto CLEAN_UP;
  650. }
  651. #ifdef MODULE_REDIR
  652. struct sockaddr_storage dst_addr;
  653. memset(&dst_addr, 0, sizeof(struct sockaddr_storage));
  654. int len = parse_udprelay_header(buf->data, buf->len, NULL, NULL, &dst_addr);
  655. if (dst_addr.ss_family != AF_INET && dst_addr.ss_family != AF_INET6) {
  656. LOGI("[udp] ss-redir does not support domain name");
  657. goto CLEAN_UP;
  658. }
  659. #else
  660. int len = parse_udprelay_header(buf->data, buf->len, NULL, NULL, NULL);
  661. #endif
  662. if (len == 0) {
  663. // error when parsing header
  664. LOGE("[udp] error in parse header");
  665. goto CLEAN_UP;
  666. }
  667. #if defined(MODULE_TUNNEL) || defined(MODULE_REDIR)
  668. // Construct packet
  669. buf->len -= len;
  670. memmove(buf->data, buf->data + len, buf->len);
  671. #else
  672. #ifdef __ANDROID__
  673. rx += buf->len;
  674. stat_update_cb();
  675. #endif
  676. // Construct packet
  677. brealloc(buf, buf->len + 3, buf_size);
  678. memmove(buf->data + 3, buf->data, buf->len);
  679. memset(buf->data, 0, 3);
  680. buf->len += 3;
  681. #endif
  682. #endif
  683. #ifdef MODULE_REMOTE
  684. rx += buf->len;
  685. // Reconstruct UDP response header
  686. char addr_header[512];
  687. int addr_header_len = construct_udprelay_header(&src_addr, addr_header);
  688. // Construct packet
  689. brealloc(buf, buf->len + addr_header_len, buf_size);
  690. memmove(buf->data + addr_header_len, buf->data, buf->len);
  691. memcpy(buf->data, addr_header, addr_header_len);
  692. buf->len += addr_header_len;
  693. int err = server_ctx->crypto->encrypt_all(buf, server_ctx->crypto->cipher, buf_size);
  694. if (err) {
  695. // drop the packet silently
  696. goto CLEAN_UP;
  697. }
  698. #endif
  699. if (buf->len > packet_size) {
  700. if (verbose) {
  701. LOGI("[udp] remote_recv_sendto fragmentation, MTU at least be: " SSIZE_FMT, buf->len + PACKET_HEADER_SIZE);
  702. }
  703. }
  704. size_t remote_src_addr_len = get_sockaddr_len((struct sockaddr *)&remote_ctx->src_addr);
  705. #ifdef MODULE_REDIR
  706. size_t remote_dst_addr_len = get_sockaddr_len((struct sockaddr *)&dst_addr);
  707. int src_fd = socket(remote_ctx->src_addr.ss_family, SOCK_DGRAM, 0);
  708. if (src_fd < 0) {
  709. ERROR("[udp] remote_recv_socket");
  710. goto CLEAN_UP;
  711. }
  712. int opt = 1;
  713. if (setsockopt(src_fd, SOL_IP, IP_TRANSPARENT, &opt, sizeof(opt))) {
  714. ERROR("[udp] remote_recv_setsockopt");
  715. close(src_fd);
  716. goto CLEAN_UP;
  717. }
  718. if (setsockopt(src_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt))) {
  719. ERROR("[udp] remote_recv_setsockopt");
  720. close(src_fd);
  721. goto CLEAN_UP;
  722. }
  723. #ifdef IP_TOS
  724. // Set QoS flag
  725. int tos = 46;
  726. setsockopt(src_fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
  727. #endif
  728. if (bind(src_fd, (struct sockaddr *)&dst_addr, remote_dst_addr_len) != 0) {
  729. ERROR("[udp] remote_recv_bind");
  730. close(src_fd);
  731. goto CLEAN_UP;
  732. }
  733. int s = sendto(src_fd, buf->data, buf->len, 0,
  734. (struct sockaddr *)&remote_ctx->src_addr, remote_src_addr_len);
  735. if (s == -1) {
  736. ERROR("[udp] remote_recv_sendto");
  737. close(src_fd);
  738. goto CLEAN_UP;
  739. }
  740. close(src_fd);
  741. #else
  742. int s = sendto(server_ctx->fd, buf->data, buf->len, 0,
  743. (struct sockaddr *)&remote_ctx->src_addr, remote_src_addr_len);
  744. if (s == -1) {
  745. ERROR("[udp] remote_recv_sendto");
  746. goto CLEAN_UP;
  747. }
  748. #endif
  749. // handle the UDP packet successfully,
  750. // triger the timer
  751. ev_timer_again(EV_A_ & remote_ctx->watcher);
  752. CLEAN_UP:
  753. bfree(buf);
  754. ss_free(buf);
  755. }
  756. static void
  757. server_recv_cb(EV_P_ ev_io *w, int revents)
  758. {
  759. server_ctx_t *server_ctx = (server_ctx_t *)w;
  760. struct sockaddr_storage src_addr;
  761. memset(&src_addr, 0, sizeof(struct sockaddr_storage));
  762. buffer_t *buf = ss_malloc(sizeof(buffer_t));
  763. balloc(buf, buf_size);
  764. socklen_t src_addr_len = sizeof(struct sockaddr_storage);
  765. unsigned int offset = 0;
  766. #ifdef MODULE_REDIR
  767. char control_buffer[64] = { 0 };
  768. struct msghdr msg;
  769. memset(&msg, 0, sizeof(struct msghdr));
  770. struct iovec iov[1];
  771. struct sockaddr_storage dst_addr;
  772. memset(&dst_addr, 0, sizeof(struct sockaddr_storage));
  773. msg.msg_name = &src_addr;
  774. msg.msg_namelen = src_addr_len;
  775. msg.msg_control = control_buffer;
  776. msg.msg_controllen = sizeof(control_buffer);
  777. iov[0].iov_base = buf->data;
  778. iov[0].iov_len = buf_size;
  779. msg.msg_iov = iov;
  780. msg.msg_iovlen = 1;
  781. buf->len = recvmsg(server_ctx->fd, &msg, 0);
  782. if (buf->len == -1) {
  783. ERROR("[udp] server_recvmsg");
  784. goto CLEAN_UP;
  785. } else if (buf->len > packet_size) {
  786. if (verbose) {
  787. LOGI("[udp] UDP server_recv_recvmsg fragmentation, MTU at least be: " SSIZE_FMT,
  788. buf->len + PACKET_HEADER_SIZE);
  789. }
  790. }
  791. if (get_dstaddr(&msg, &dst_addr)) {
  792. LOGE("[udp] unable to get dest addr");
  793. goto CLEAN_UP;
  794. }
  795. src_addr_len = msg.msg_namelen;
  796. #else
  797. ssize_t r;
  798. r = recvfrom(server_ctx->fd, buf->data, buf_size,
  799. 0, (struct sockaddr *)&src_addr, &src_addr_len);
  800. if (r == -1) {
  801. // error on recv
  802. // simply drop that packet
  803. ERROR("[udp] server_recv_recvfrom");
  804. goto CLEAN_UP;
  805. } else if (r > packet_size) {
  806. if (verbose) {
  807. LOGI("[udp] server_recv_recvfrom fragmentation, MTU at least be: " SSIZE_FMT, r + PACKET_HEADER_SIZE);
  808. }
  809. }
  810. buf->len = r;
  811. #endif
  812. if (verbose) {
  813. LOGI("[udp] server receive a packet");
  814. }
  815. #ifdef MODULE_REMOTE
  816. tx += buf->len;
  817. int err = server_ctx->crypto->decrypt_all(buf, server_ctx->crypto->cipher, buf_size);
  818. if (err) {
  819. // drop the packet silently
  820. goto CLEAN_UP;
  821. }
  822. #endif
  823. #ifdef MODULE_LOCAL
  824. #if !defined(MODULE_TUNNEL) && !defined(MODULE_REDIR)
  825. #ifdef __ANDROID__
  826. tx += buf->len;
  827. #endif
  828. uint8_t frag = *(uint8_t *)(buf->data + 2);
  829. offset += 3;
  830. #endif
  831. #endif
  832. /*
  833. *
  834. * SOCKS5 UDP Request
  835. * +----+------+------+----------+----------+----------+
  836. * |RSV | FRAG | ATYP | DST.ADDR | DST.PORT | DATA |
  837. * +----+------+------+----------+----------+----------+
  838. * | 2 | 1 | 1 | Variable | 2 | Variable |
  839. * +----+------+------+----------+----------+----------+
  840. *
  841. * SOCKS5 UDP Response
  842. * +----+------+------+----------+----------+----------+
  843. * |RSV | FRAG | ATYP | DST.ADDR | DST.PORT | DATA |
  844. * +----+------+------+----------+----------+----------+
  845. * | 2 | 1 | 1 | Variable | 2 | Variable |
  846. * +----+------+------+----------+----------+----------+
  847. *
  848. * shadowsocks UDP Request (before encrypted)
  849. * +------+----------+----------+----------+
  850. * | ATYP | DST.ADDR | DST.PORT | DATA |
  851. * +------+----------+----------+----------+
  852. * | 1 | Variable | 2 | Variable |
  853. * +------+----------+----------+----------+
  854. *
  855. * shadowsocks UDP Response (before encrypted)
  856. * +------+----------+----------+----------+
  857. * | ATYP | DST.ADDR | DST.PORT | DATA |
  858. * +------+----------+----------+----------+
  859. * | 1 | Variable | 2 | Variable |
  860. * +------+----------+----------+----------+
  861. *
  862. * shadowsocks UDP Request and Response (after encrypted)
  863. * +-------+--------------+
  864. * | IV | PAYLOAD |
  865. * +-------+--------------+
  866. * | Fixed | Variable |
  867. * +-------+--------------+
  868. *
  869. */
  870. #ifdef MODULE_REDIR
  871. char addr_header[512] = { 0 };
  872. int addr_header_len = construct_udprelay_header(&dst_addr, addr_header);
  873. if (addr_header_len == 0) {
  874. LOGE("[udp] failed to parse tproxy addr");
  875. goto CLEAN_UP;
  876. }
  877. // reconstruct the buffer
  878. brealloc(buf, buf->len + addr_header_len, buf_size);
  879. memmove(buf->data + addr_header_len, buf->data, buf->len);
  880. memcpy(buf->data, addr_header, addr_header_len);
  881. buf->len += addr_header_len;
  882. #elif MODULE_TUNNEL
  883. char addr_header[512] = { 0 };
  884. char *host = server_ctx->tunnel_addr.host;
  885. char *port = server_ctx->tunnel_addr.port;
  886. uint16_t port_num = (uint16_t)atoi(port);
  887. uint16_t port_net_num = htons(port_num);
  888. int addr_header_len = 0;
  889. struct cork_ip ip;
  890. if (cork_ip_init(&ip, host) != -1) {
  891. if (ip.version == 4) {
  892. // send as IPv4
  893. struct in_addr host_addr;
  894. memset(&host_addr, 0, sizeof(struct in_addr));
  895. int host_len = sizeof(struct in_addr);
  896. if (inet_pton(AF_INET, host, &host_addr) == -1) {
  897. FATAL("IP parser error");
  898. }
  899. addr_header[addr_header_len++] = 1;
  900. memcpy(addr_header + addr_header_len, &host_addr, host_len);
  901. addr_header_len += host_len;
  902. } else if (ip.version == 6) {
  903. // send as IPv6
  904. struct in6_addr host_addr;
  905. memset(&host_addr, 0, sizeof(struct in6_addr));
  906. int host_len = sizeof(struct in6_addr);
  907. if (inet_pton(AF_INET6, host, &host_addr) == -1) {
  908. FATAL("IP parser error");
  909. }
  910. addr_header[addr_header_len++] = 4;
  911. memcpy(addr_header + addr_header_len, &host_addr, host_len);
  912. addr_header_len += host_len;
  913. } else {
  914. FATAL("IP parser error");
  915. }
  916. } else {
  917. // send as domain
  918. int host_len = strlen(host);
  919. addr_header[addr_header_len++] = 3;
  920. addr_header[addr_header_len++] = host_len;
  921. memcpy(addr_header + addr_header_len, host, host_len);
  922. addr_header_len += host_len;
  923. }
  924. memcpy(addr_header + addr_header_len, &port_net_num, 2);
  925. addr_header_len += 2;
  926. // reconstruct the buffer
  927. brealloc(buf, buf->len + addr_header_len, buf_size);
  928. memmove(buf->data + addr_header_len, buf->data, buf->len);
  929. memcpy(buf->data, addr_header, addr_header_len);
  930. buf->len += addr_header_len;
  931. #else
  932. char host[257] = { 0 };
  933. char port[64] = { 0 };
  934. struct sockaddr_storage dst_addr;
  935. memset(&dst_addr, 0, sizeof(struct sockaddr_storage));
  936. int addr_header_len = parse_udprelay_header(buf->data + offset, buf->len - offset,
  937. host, port, &dst_addr);
  938. if (addr_header_len == 0) {
  939. // error in parse header
  940. goto CLEAN_UP;
  941. }
  942. char *addr_header = buf->data + offset;
  943. #endif
  944. #ifdef MODULE_LOCAL
  945. char *key = hash_key(server_ctx->remote_addr->sa_family, &src_addr);
  946. #else
  947. char *key = hash_key(dst_addr.ss_family, &src_addr);
  948. #endif
  949. struct cache *conn_cache = server_ctx->conn_cache;
  950. remote_ctx_t *remote_ctx = NULL;
  951. cache_lookup(conn_cache, key, HASH_KEY_LEN, (void *)&remote_ctx);
  952. if (remote_ctx != NULL) {
  953. if (sockaddr_cmp(&src_addr, &remote_ctx->src_addr, sizeof(src_addr))) {
  954. remote_ctx = NULL;
  955. }
  956. }
  957. // reset the timer
  958. if (remote_ctx != NULL) {
  959. ev_timer_again(EV_A_ & remote_ctx->watcher);
  960. }
  961. if (remote_ctx == NULL) {
  962. if (verbose) {
  963. #ifdef MODULE_REDIR
  964. char src[SS_ADDRSTRLEN];
  965. char dst[SS_ADDRSTRLEN];
  966. strcpy(src, get_addr_str((struct sockaddr *)&src_addr));
  967. strcpy(dst, get_addr_str((struct sockaddr *)&dst_addr));
  968. LOGI("[%s] [udp] cache miss: %s <-> %s", s_port, dst, src);
  969. #else
  970. LOGI("[%s] [udp] cache miss: %s:%s <-> %s", s_port, host, port,
  971. get_addr_str((struct sockaddr *)&src_addr));
  972. #endif
  973. }
  974. } else {
  975. if (verbose) {
  976. #ifdef MODULE_REDIR
  977. char src[SS_ADDRSTRLEN];
  978. char dst[SS_ADDRSTRLEN];
  979. strcpy(src, get_addr_str((struct sockaddr *)&src_addr));
  980. strcpy(dst, get_addr_str((struct sockaddr *)&dst_addr));
  981. LOGI("[%s] [udp] cache hit: %s <-> %s", s_port, dst, src);
  982. #else
  983. LOGI("[%s] [udp] cache hit: %s:%s <-> %s", s_port, host, port,
  984. get_addr_str((struct sockaddr *)&src_addr));
  985. #endif
  986. }
  987. }
  988. #ifdef MODULE_LOCAL
  989. #if !defined(MODULE_TUNNEL) && !defined(MODULE_REDIR)
  990. if (frag) {
  991. LOGE("[udp] drop a message since frag is not 0, but %d", frag);
  992. goto CLEAN_UP;
  993. }
  994. #endif
  995. const struct sockaddr *remote_addr = server_ctx->remote_addr;
  996. const int remote_addr_len = server_ctx->remote_addr_len;
  997. if (remote_ctx == NULL) {
  998. // Bind to any port
  999. int remotefd = create_remote_socket(remote_addr->sa_family == AF_INET6);
  1000. if (remotefd < 0) {
  1001. ERROR("[udp] udprelay bind() error");
  1002. goto CLEAN_UP;
  1003. }
  1004. setnonblocking(remotefd);
  1005. #ifdef SO_NOSIGPIPE
  1006. set_nosigpipe(remotefd);
  1007. #endif
  1008. #ifdef IP_TOS
  1009. // Set QoS flag
  1010. int tos = 46;
  1011. setsockopt(remotefd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
  1012. #endif
  1013. #ifdef SET_INTERFACE
  1014. if (server_ctx->iface) {
  1015. if (setinterface(remotefd, server_ctx->iface) == -1)
  1016. ERROR("setinterface");
  1017. }
  1018. #endif
  1019. #ifdef __ANDROID__
  1020. if (vpn) {
  1021. if (protect_socket(remotefd) == -1) {
  1022. ERROR("protect_socket");
  1023. close(remotefd);
  1024. goto CLEAN_UP;
  1025. }
  1026. }
  1027. #endif
  1028. // Init remote_ctx
  1029. remote_ctx = new_remote(remotefd, server_ctx);
  1030. remote_ctx->src_addr = src_addr;
  1031. remote_ctx->af = remote_addr->sa_family;
  1032. // Add to conn cache
  1033. cache_insert(conn_cache, key, HASH_KEY_LEN, (void *)remote_ctx);
  1034. // Start remote io
  1035. ev_io_start(EV_A_ & remote_ctx->io);
  1036. ev_timer_start(EV_A_ & remote_ctx->watcher);
  1037. }
  1038. remote_ctx->addr_header_len = addr_header_len;
  1039. memcpy(remote_ctx->addr_header, addr_header, addr_header_len);
  1040. if (offset > 0) {
  1041. buf->len -= offset;
  1042. memmove(buf->data, buf->data + offset, buf->len);
  1043. }
  1044. int err = server_ctx->crypto->encrypt_all(buf, server_ctx->crypto->cipher, buf_size);
  1045. if (err) {
  1046. // drop the packet silently
  1047. goto CLEAN_UP;
  1048. }
  1049. if (buf->len > packet_size) {
  1050. if (verbose) {
  1051. LOGI("[udp] server_recv_sendto fragmentation, MTU at least be: " SSIZE_FMT, buf->len + PACKET_HEADER_SIZE);
  1052. }
  1053. }
  1054. int s = sendto(remote_ctx->fd, buf->data, buf->len, 0, remote_addr, remote_addr_len);
  1055. if (s == -1) {
  1056. ERROR("[udp] server_recv_sendto");
  1057. }
  1058. #else
  1059. int cache_hit = 0;
  1060. int need_query = 0;
  1061. if (buf->len - addr_header_len > packet_size) {
  1062. if (verbose) {
  1063. LOGI("[udp] server_recv_sendto fragmentation, MTU at least be: " SSIZE_FMT,
  1064. buf->len - addr_header_len + PACKET_HEADER_SIZE);
  1065. }
  1066. }
  1067. if (remote_ctx != NULL) {
  1068. cache_hit = 1;
  1069. if (dst_addr.ss_family != AF_INET && dst_addr.ss_family != AF_INET6) {
  1070. need_query = 1;
  1071. }
  1072. } else {
  1073. if (dst_addr.ss_family == AF_INET || dst_addr.ss_family == AF_INET6) {
  1074. int remotefd = create_remote_socket(dst_addr.ss_family == AF_INET6);
  1075. if (remotefd != -1) {
  1076. setnonblocking(remotefd);
  1077. #ifdef SO_BROADCAST
  1078. set_broadcast(remotefd);
  1079. #endif
  1080. #ifdef SO_NOSIGPIPE
  1081. set_nosigpipe(remotefd);
  1082. #endif
  1083. #ifdef IP_TOS
  1084. // Set QoS flag
  1085. int tos = 46;
  1086. setsockopt(remotefd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
  1087. #endif
  1088. #ifdef SET_INTERFACE
  1089. if (server_ctx->iface) {
  1090. if (setinterface(remotefd, server_ctx->iface) == -1)
  1091. ERROR("setinterface");
  1092. }
  1093. #endif
  1094. remote_ctx = new_remote(remotefd, server_ctx);
  1095. remote_ctx->src_addr = src_addr;
  1096. remote_ctx->server_ctx = server_ctx;
  1097. remote_ctx->addr_header_len = addr_header_len;
  1098. memcpy(remote_ctx->addr_header, addr_header, addr_header_len);
  1099. memcpy(&remote_ctx->dst_addr, &dst_addr, sizeof(struct sockaddr_storage));
  1100. } else {
  1101. ERROR("[udp] bind() error");
  1102. goto CLEAN_UP;
  1103. }
  1104. }
  1105. }
  1106. if (remote_ctx != NULL && !need_query) {
  1107. size_t addr_len = get_sockaddr_len((struct sockaddr *)&dst_addr);
  1108. int s = sendto(remote_ctx->fd, buf->data + addr_header_len,
  1109. buf->len - addr_header_len, 0,
  1110. (struct sockaddr *)&dst_addr, addr_len);
  1111. if (s == -1) {
  1112. ERROR("[udp] sendto_remote");
  1113. if (!cache_hit) {
  1114. close_and_free_remote(EV_A_ remote_ctx);
  1115. }
  1116. } else {
  1117. if (!cache_hit) {
  1118. // Add to conn cache
  1119. remote_ctx->af = dst_addr.ss_family;
  1120. char *key = hash_key(remote_ctx->af, &remote_ctx->src_addr);
  1121. cache_insert(server_ctx->conn_cache, key, HASH_KEY_LEN, (void *)remote_ctx);
  1122. ev_io_start(EV_A_ & remote_ctx->io);
  1123. ev_timer_start(EV_A_ & remote_ctx->watcher);
  1124. }
  1125. }
  1126. } else {
  1127. struct addrinfo hints;
  1128. memset(&hints, 0, sizeof(struct addrinfo));
  1129. hints.ai_family = AF_UNSPEC;
  1130. hints.ai_socktype = SOCK_DGRAM;
  1131. hints.ai_protocol = IPPROTO_UDP;
  1132. struct query_ctx *query_ctx = new_query_ctx(buf->data + addr_header_len,
  1133. buf->len - addr_header_len);
  1134. query_ctx->server_ctx = server_ctx;
  1135. query_ctx->addr_header_len = addr_header_len;
  1136. query_ctx->src_addr = src_addr;
  1137. memcpy(query_ctx->addr_header, addr_header, addr_header_len);
  1138. if (need_query) {
  1139. query_ctx->remote_ctx = remote_ctx;
  1140. }
  1141. resolv_start(host, htons(atoi(port)), resolv_cb, resolv_free_cb, query_ctx);
  1142. }
  1143. #endif
  1144. CLEAN_UP:
  1145. bfree(buf);
  1146. ss_free(buf);
  1147. }
  1148. void
  1149. free_cb(void *key, void *element)
  1150. {
  1151. remote_ctx_t *remote_ctx = (remote_ctx_t *)element;
  1152. if (verbose) {
  1153. LOGI("[udp] one connection freed");
  1154. }
  1155. close_and_free_remote(EV_DEFAULT, remote_ctx);
  1156. }
  1157. int
  1158. init_udprelay(const char *server_host, const char *server_port,
  1159. #ifdef MODULE_LOCAL
  1160. const struct sockaddr *remote_addr, const int remote_addr_len,
  1161. #ifdef MODULE_TUNNEL
  1162. const ss_addr_t tunnel_addr,
  1163. #endif
  1164. #endif
  1165. int mtu, crypto_t *crypto, int timeout, const char *iface)
  1166. {
  1167. s_port = server_port;
  1168. // Initialize ev loop
  1169. struct ev_loop *loop = EV_DEFAULT;
  1170. // Initialize MTU
  1171. if (mtu > 0) {
  1172. packet_size = mtu - PACKET_HEADER_SIZE;
  1173. buf_size = packet_size * 2;
  1174. }
  1175. // ////////////////////////////////////////////////
  1176. // Setup server context
  1177. // Bind to port
  1178. int serverfd = create_server_socket(server_host, server_port);
  1179. if (serverfd < 0) {
  1180. return -1;
  1181. }
  1182. setnonblocking(serverfd);
  1183. // Initialize cache
  1184. struct cache *conn_cache;
  1185. cache_create(&conn_cache, MAX_UDP_CONN_NUM, free_cb);
  1186. server_ctx_t *server_ctx = new_server_ctx(serverfd);
  1187. #ifdef MODULE_REMOTE
  1188. server_ctx->loop = loop;
  1189. #endif
  1190. server_ctx->timeout = max(timeout, MIN_UDP_TIMEOUT);
  1191. server_ctx->crypto = crypto;
  1192. server_ctx->iface = iface;
  1193. server_ctx->conn_cache = conn_cache;
  1194. #ifdef MODULE_LOCAL
  1195. server_ctx->remote_addr = remote_addr;
  1196. server_ctx->remote_addr_len = remote_addr_len;
  1197. #ifdef MODULE_TUNNEL
  1198. server_ctx->tunnel_addr = tunnel_addr;
  1199. #endif
  1200. #endif
  1201. ev_io_start(loop, &server_ctx->io);
  1202. server_ctx_list[server_num++] = server_ctx;
  1203. return serverfd;
  1204. }
  1205. void
  1206. free_udprelay()
  1207. {
  1208. struct ev_loop *loop = EV_DEFAULT;
  1209. while (server_num > 0) {
  1210. server_ctx_t *server_ctx = server_ctx_list[--server_num];
  1211. ev_io_stop(loop, &server_ctx->io);
  1212. close(server_ctx->fd);
  1213. cache_delete(server_ctx->conn_cache, 0);
  1214. ss_free(server_ctx);
  1215. server_ctx_list[server_num] = NULL;
  1216. }
  1217. }