net_packet.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  1. /*
  2. net_packet.c -- Handles in- and outgoing VPN packets
  3. Copyright (C) 1998-2005 Ivo Timmermans,
  4. 2000-2016 Guus Sliepen <guus@tinc-vpn.org>
  5. 2010 Timothy Redaelli <timothy@redaelli.eu>
  6. 2010 Brandon Black <blblack@gmail.com>
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License along
  16. with this program; if not, write to the Free Software Foundation, Inc.,
  17. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. */
  19. #include "system.h"
  20. #include <openssl/rand.h>
  21. #include <openssl/err.h>
  22. #include <openssl/evp.h>
  23. #include <openssl/pem.h>
  24. #include <openssl/hmac.h>
  25. #ifdef HAVE_ZLIB
  26. #include <zlib.h>
  27. #endif
  28. #ifdef HAVE_LZO
  29. #include LZO1X_H
  30. #endif
  31. #include "avl_tree.h"
  32. #include "conf.h"
  33. #include "connection.h"
  34. #include "device.h"
  35. #include "ethernet.h"
  36. #include "event.h"
  37. #include "graph.h"
  38. #include "logger.h"
  39. #include "net.h"
  40. #include "netutl.h"
  41. #include "protocol.h"
  42. #include "process.h"
  43. #include "route.h"
  44. #include "utils.h"
  45. #include "xalloc.h"
  46. int keylifetime = 0;
  47. int keyexpires = 0;
  48. #ifdef HAVE_LZO
  49. static char lzo_wrkmem[LZO1X_999_MEM_COMPRESS > LZO1X_1_MEM_COMPRESS ? LZO1X_999_MEM_COMPRESS : LZO1X_1_MEM_COMPRESS];
  50. #endif
  51. static void send_udppacket(node_t *, vpn_packet_t *);
  52. unsigned replaywin = 16;
  53. bool localdiscovery = false;
  54. #define MAX_SEQNO 1073741824
  55. /* mtuprobes == 1..30: initial discovery, send bursts with 1 second interval
  56. mtuprobes == 31: sleep pinginterval seconds
  57. mtuprobes == 32: send 1 burst, sleep pingtimeout second
  58. mtuprobes == 33: no response from other side, restart PMTU discovery process
  59. Probes are sent in batches of at least three, with random sizes between the
  60. lower and upper boundaries for the MTU thus far discovered.
  61. After the initial discovery, a fourth packet is added to each batch with a
  62. size larger than the currently known PMTU, to test if the PMTU has increased.
  63. In case local discovery is enabled, another packet is added to each batch,
  64. which will be broadcast to the local network.
  65. */
  66. void send_mtu_probe(node_t *n) {
  67. vpn_packet_t packet;
  68. int len, i;
  69. int timeout = 1;
  70. n->mtuprobes++;
  71. n->mtuevent = NULL;
  72. if(!n->status.reachable || !n->status.validkey) {
  73. ifdebug(TRAFFIC) logger(LOG_INFO, "Trying to send MTU probe to unreachable or rekeying node %s (%s)", n->name, n->hostname);
  74. n->mtuprobes = 0;
  75. return;
  76. }
  77. if(n->mtuprobes > 32) {
  78. if(!n->minmtu) {
  79. n->mtuprobes = 31;
  80. timeout = pinginterval;
  81. goto end;
  82. }
  83. ifdebug(TRAFFIC) logger(LOG_INFO, "%s (%s) did not respond to UDP ping, restarting PMTU discovery", n->name, n->hostname);
  84. n->mtuprobes = 1;
  85. n->minmtu = 0;
  86. n->maxmtu = MTU;
  87. }
  88. if(n->mtuprobes >= 10 && n->mtuprobes < 32 && !n->minmtu) {
  89. ifdebug(TRAFFIC) logger(LOG_INFO, "No response to MTU probes from %s (%s)", n->name, n->hostname);
  90. n->mtuprobes = 31;
  91. }
  92. if(n->mtuprobes == 30 || (n->mtuprobes < 30 && n->minmtu >= n->maxmtu)) {
  93. if(n->minmtu > n->maxmtu) {
  94. n->minmtu = n->maxmtu;
  95. } else {
  96. n->maxmtu = n->minmtu;
  97. }
  98. n->mtu = n->minmtu;
  99. ifdebug(TRAFFIC) logger(LOG_INFO, "Fixing MTU of %s (%s) to %d after %d probes", n->name, n->hostname, n->mtu, n->mtuprobes);
  100. n->mtuprobes = 31;
  101. }
  102. if(n->mtuprobes == 31) {
  103. timeout = pinginterval;
  104. goto end;
  105. } else if(n->mtuprobes == 32) {
  106. timeout = pingtimeout;
  107. }
  108. for(i = 0; i < 4 + localdiscovery; i++) {
  109. if(i == 0) {
  110. if(n->mtuprobes < 30 || n->maxmtu + 8 >= MTU) {
  111. continue;
  112. }
  113. len = n->maxmtu + 8;
  114. } else if(n->maxmtu <= n->minmtu) {
  115. len = n->maxmtu;
  116. } else {
  117. len = n->minmtu + 1 + rand() % (n->maxmtu - n->minmtu);
  118. }
  119. if(len < 64) {
  120. len = 64;
  121. }
  122. memset(packet.data, 0, 14);
  123. RAND_bytes(packet.data + 14, len - 14);
  124. packet.len = len;
  125. if(i >= 4 && n->mtuprobes <= 10) {
  126. packet.priority = -1;
  127. } else {
  128. packet.priority = 0;
  129. }
  130. ifdebug(TRAFFIC) logger(LOG_INFO, "Sending MTU probe length %d to %s (%s)", len, n->name, n->hostname);
  131. send_udppacket(n, &packet);
  132. }
  133. end:
  134. n->mtuevent = new_event();
  135. n->mtuevent->handler = (event_handler_t)send_mtu_probe;
  136. n->mtuevent->data = n;
  137. n->mtuevent->time = now + timeout;
  138. event_add(n->mtuevent);
  139. }
  140. void mtu_probe_h(node_t *n, vpn_packet_t *packet, length_t len) {
  141. ifdebug(TRAFFIC) logger(LOG_INFO, "Got MTU probe length %d from %s (%s)", packet->len, n->name, n->hostname);
  142. if(!packet->data[0]) {
  143. packet->data[0] = 1;
  144. send_udppacket(n, packet);
  145. } else {
  146. if(n->mtuprobes > 30) {
  147. if(len == n->maxmtu + 8) {
  148. ifdebug(TRAFFIC) logger(LOG_INFO, "Increase in PMTU to %s (%s) detected, restarting PMTU discovery", n->name, n->hostname);
  149. n->maxmtu = MTU;
  150. n->mtuprobes = 10;
  151. return;
  152. }
  153. if(n->minmtu) {
  154. n->mtuprobes = 30;
  155. } else {
  156. n->mtuprobes = 1;
  157. }
  158. }
  159. if(len > n->maxmtu) {
  160. len = n->maxmtu;
  161. }
  162. if(n->minmtu < len) {
  163. n->minmtu = len;
  164. }
  165. }
  166. }
  167. static length_t compress_packet(uint8_t *dest, const uint8_t *source, length_t len, int level) {
  168. if(level == 0) {
  169. memcpy(dest, source, len);
  170. return len;
  171. } else if(level == 10) {
  172. #ifdef HAVE_LZO
  173. lzo_uint lzolen = MAXSIZE;
  174. lzo1x_1_compress(source, len, dest, &lzolen, lzo_wrkmem);
  175. return lzolen;
  176. #else
  177. return 0;
  178. #endif
  179. } else if(level < 10) {
  180. #ifdef HAVE_ZLIB
  181. unsigned long destlen = MAXSIZE;
  182. if(compress2(dest, &destlen, source, len, level) == Z_OK) {
  183. return destlen;
  184. } else
  185. #endif
  186. return 0;
  187. } else {
  188. #ifdef HAVE_LZO
  189. lzo_uint lzolen = MAXSIZE;
  190. lzo1x_999_compress(source, len, dest, &lzolen, lzo_wrkmem);
  191. return lzolen;
  192. #else
  193. return 0;
  194. #endif
  195. }
  196. return 0;
  197. }
  198. static length_t uncompress_packet(uint8_t *dest, const uint8_t *source, length_t len, int level) {
  199. if(level == 0) {
  200. memcpy(dest, source, len);
  201. return len;
  202. } else if(level > 9) {
  203. #ifdef HAVE_LZO
  204. lzo_uint lzolen = MAXSIZE;
  205. if(lzo1x_decompress_safe(source, len, dest, &lzolen, NULL) == LZO_E_OK) {
  206. return lzolen;
  207. } else
  208. #endif
  209. return 0;
  210. }
  211. #ifdef HAVE_ZLIB
  212. else {
  213. unsigned long destlen = MAXSIZE;
  214. if(uncompress(dest, &destlen, source, len) == Z_OK) {
  215. return destlen;
  216. } else {
  217. return 0;
  218. }
  219. }
  220. #endif
  221. return -1;
  222. }
  223. /* VPN packet I/O */
  224. static void receive_packet(node_t *n, vpn_packet_t *packet) {
  225. ifdebug(TRAFFIC) logger(LOG_DEBUG, "Received packet of %d bytes from %s (%s)",
  226. packet->len, n->name, n->hostname);
  227. route(n, packet);
  228. }
  229. static bool try_mac(const node_t *n, const vpn_packet_t *inpkt) {
  230. unsigned char hmac[EVP_MAX_MD_SIZE];
  231. if(!n->indigest || !n->inmaclength || !n->inkey || inpkt->len < sizeof(inpkt->seqno) + n->inmaclength) {
  232. return false;
  233. }
  234. HMAC(n->indigest, n->inkey, n->inkeylength, (unsigned char *) &inpkt->seqno, inpkt->len - n->inmaclength, (unsigned char *)hmac, NULL);
  235. return !memcmp_constant_time(hmac, (char *) &inpkt->seqno + inpkt->len - n->inmaclength, n->inmaclength);
  236. }
  237. static void receive_udppacket(node_t *n, vpn_packet_t *inpkt) {
  238. vpn_packet_t pkt1, pkt2;
  239. vpn_packet_t *pkt[] = { &pkt1, &pkt2, &pkt1, &pkt2 };
  240. int nextpkt = 0;
  241. vpn_packet_t *outpkt;
  242. int outlen, outpad;
  243. unsigned char hmac[EVP_MAX_MD_SIZE];
  244. if(!n->inkey) {
  245. ifdebug(TRAFFIC) logger(LOG_DEBUG, "Got packet from %s (%s) but he hasn't got our key yet",
  246. n->name, n->hostname);
  247. return;
  248. }
  249. /* Check packet length */
  250. if(inpkt->len < sizeof(inpkt->seqno) + n->inmaclength) {
  251. ifdebug(TRAFFIC) logger(LOG_DEBUG, "Got too short packet from %s (%s)",
  252. n->name, n->hostname);
  253. return;
  254. }
  255. /* Check the message authentication code */
  256. if(n->indigest && n->inmaclength) {
  257. inpkt->len -= n->inmaclength;
  258. HMAC(n->indigest, n->inkey, n->inkeylength,
  259. (unsigned char *) &inpkt->seqno, inpkt->len, (unsigned char *)hmac, NULL);
  260. if(memcmp_constant_time(hmac, (char *) &inpkt->seqno + inpkt->len, n->inmaclength)) {
  261. ifdebug(TRAFFIC) logger(LOG_DEBUG, "Got unauthenticated packet from %s (%s)",
  262. n->name, n->hostname);
  263. return;
  264. }
  265. }
  266. /* Decrypt the packet */
  267. if(n->incipher) {
  268. outpkt = pkt[nextpkt++];
  269. if(!EVP_DecryptInit_ex(n->inctx, NULL, NULL, NULL, NULL)
  270. || !EVP_DecryptUpdate(n->inctx, (unsigned char *) &outpkt->seqno, &outlen,
  271. (unsigned char *) &inpkt->seqno, inpkt->len)
  272. || !EVP_DecryptFinal_ex(n->inctx, (unsigned char *) &outpkt->seqno + outlen, &outpad)) {
  273. ifdebug(TRAFFIC) logger(LOG_DEBUG, "Error decrypting packet from %s (%s): %s",
  274. n->name, n->hostname, ERR_error_string(ERR_get_error(), NULL));
  275. return;
  276. }
  277. outpkt->len = outlen + outpad;
  278. inpkt = outpkt;
  279. }
  280. /* Check the sequence number */
  281. inpkt->len -= sizeof(inpkt->seqno);
  282. inpkt->seqno = ntohl(inpkt->seqno);
  283. if(replaywin) {
  284. if(inpkt->seqno != n->received_seqno + 1) {
  285. if(inpkt->seqno >= n->received_seqno + replaywin * 8) {
  286. if(n->farfuture++ < replaywin >> 2) {
  287. ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet from %s (%s) is %d seqs in the future, dropped (%u)",
  288. n->name, n->hostname, inpkt->seqno - n->received_seqno - 1, n->farfuture);
  289. return;
  290. }
  291. ifdebug(TRAFFIC) logger(LOG_WARNING, "Lost %d packets from %s (%s)",
  292. inpkt->seqno - n->received_seqno - 1, n->name, n->hostname);
  293. memset(n->late, 0, replaywin);
  294. } else if(inpkt->seqno <= n->received_seqno) {
  295. if((n->received_seqno >= replaywin * 8 && inpkt->seqno <= n->received_seqno - replaywin * 8) || !(n->late[(inpkt->seqno / 8) % replaywin] & (1 << inpkt->seqno % 8))) {
  296. ifdebug(TRAFFIC) logger(LOG_WARNING, "Got late or replayed packet from %s (%s), seqno %d, last received %d",
  297. n->name, n->hostname, inpkt->seqno, n->received_seqno);
  298. return;
  299. }
  300. } else {
  301. for(uint32_t i = n->received_seqno + 1; i < inpkt->seqno; i++) {
  302. n->late[(i / 8) % replaywin] |= 1 << i % 8;
  303. }
  304. }
  305. }
  306. n->farfuture = 0;
  307. n->late[(inpkt->seqno / 8) % replaywin] &= ~(1 << inpkt->seqno % 8);
  308. }
  309. if(inpkt->seqno > n->received_seqno) {
  310. n->received_seqno = inpkt->seqno;
  311. }
  312. if(n->received_seqno > MAX_SEQNO) {
  313. keyexpires = 0;
  314. }
  315. /* Decompress the packet */
  316. length_t origlen = inpkt->len;
  317. if(n->incompression) {
  318. outpkt = pkt[nextpkt++];
  319. if(!(outpkt->len = uncompress_packet(outpkt->data, inpkt->data, inpkt->len, n->incompression))) {
  320. ifdebug(TRAFFIC) logger(LOG_ERR, "Error while uncompressing packet from %s (%s)",
  321. n->name, n->hostname);
  322. return;
  323. }
  324. inpkt = outpkt;
  325. origlen -= MTU / 64 + 20;
  326. }
  327. inpkt->priority = 0;
  328. if(!inpkt->data[12] && !inpkt->data[13]) {
  329. mtu_probe_h(n, inpkt, origlen);
  330. } else {
  331. receive_packet(n, inpkt);
  332. }
  333. }
  334. void receive_tcppacket(connection_t *c, const char *buffer, length_t len) {
  335. vpn_packet_t outpkt;
  336. if(len > sizeof(outpkt.data)) {
  337. return;
  338. }
  339. outpkt.len = len;
  340. if(c->options & OPTION_TCPONLY) {
  341. outpkt.priority = 0;
  342. } else {
  343. outpkt.priority = -1;
  344. }
  345. memcpy(outpkt.data, buffer, len);
  346. receive_packet(c->node, &outpkt);
  347. }
  348. static void send_udppacket(node_t *n, vpn_packet_t *origpkt) {
  349. vpn_packet_t pkt1, pkt2;
  350. vpn_packet_t *pkt[] = { &pkt1, &pkt2, &pkt1, &pkt2 };
  351. vpn_packet_t *inpkt = origpkt;
  352. int nextpkt = 0;
  353. vpn_packet_t *outpkt;
  354. int origlen;
  355. int outlen, outpad;
  356. int origpriority;
  357. if(!n->status.reachable) {
  358. ifdebug(TRAFFIC) logger(LOG_INFO, "Trying to send UDP packet to unreachable node %s (%s)", n->name, n->hostname);
  359. return;
  360. }
  361. /* Make sure we have a valid key */
  362. if(!n->status.validkey) {
  363. ifdebug(TRAFFIC) logger(LOG_INFO,
  364. "No valid key known yet for %s (%s), forwarding via TCP",
  365. n->name, n->hostname);
  366. if(n->last_req_key + 10 <= now) {
  367. send_req_key(n);
  368. n->last_req_key = now;
  369. }
  370. send_tcppacket(n->nexthop->connection, origpkt);
  371. return;
  372. }
  373. if(n->options & OPTION_PMTU_DISCOVERY && inpkt->len > n->minmtu && (inpkt->data[12] | inpkt->data[13])) {
  374. ifdebug(TRAFFIC) logger(LOG_INFO,
  375. "Packet for %s (%s) larger than minimum MTU, forwarding via %s",
  376. n->name, n->hostname, n != n->nexthop ? n->nexthop->name : "TCP");
  377. if(n != n->nexthop) {
  378. send_packet(n->nexthop, origpkt);
  379. } else {
  380. send_tcppacket(n->nexthop->connection, origpkt);
  381. }
  382. return;
  383. }
  384. origlen = inpkt->len;
  385. origpriority = inpkt->priority;
  386. /* Compress the packet */
  387. if(n->outcompression) {
  388. outpkt = pkt[nextpkt++];
  389. if(!(outpkt->len = compress_packet(outpkt->data, inpkt->data, inpkt->len, n->outcompression))) {
  390. ifdebug(TRAFFIC) logger(LOG_ERR, "Error while compressing packet to %s (%s)",
  391. n->name, n->hostname);
  392. return;
  393. }
  394. inpkt = outpkt;
  395. }
  396. /* Add sequence number */
  397. inpkt->seqno = htonl(++(n->sent_seqno));
  398. inpkt->len += sizeof(inpkt->seqno);
  399. /* Encrypt the packet */
  400. if(n->outcipher) {
  401. outpkt = pkt[nextpkt++];
  402. if(!EVP_EncryptInit_ex(n->outctx, NULL, NULL, NULL, NULL)
  403. || !EVP_EncryptUpdate(n->outctx, (unsigned char *) &outpkt->seqno, &outlen,
  404. (unsigned char *) &inpkt->seqno, inpkt->len)
  405. || !EVP_EncryptFinal_ex(n->outctx, (unsigned char *) &outpkt->seqno + outlen, &outpad)) {
  406. ifdebug(TRAFFIC) logger(LOG_ERR, "Error while encrypting packet to %s (%s): %s",
  407. n->name, n->hostname, ERR_error_string(ERR_get_error(), NULL));
  408. goto end;
  409. }
  410. outpkt->len = outlen + outpad;
  411. inpkt = outpkt;
  412. }
  413. /* Add the message authentication code */
  414. if(n->outdigest && n->outmaclength) {
  415. HMAC(n->outdigest, n->outkey, n->outkeylength, (unsigned char *) &inpkt->seqno,
  416. inpkt->len, (unsigned char *) &inpkt->seqno + inpkt->len, NULL);
  417. inpkt->len += n->outmaclength;
  418. }
  419. /* Determine which socket we have to use */
  420. if(n->address.sa.sa_family != listen_socket[n->sock].sa.sa.sa_family) {
  421. for(int sock = 0; sock < listen_sockets; sock++) {
  422. if(n->address.sa.sa_family == listen_socket[sock].sa.sa.sa_family) {
  423. n->sock = sock;
  424. break;
  425. }
  426. }
  427. }
  428. /* Send the packet */
  429. struct sockaddr *sa;
  430. socklen_t sl;
  431. int sock;
  432. sockaddr_t broadcast;
  433. /* Overloaded use of priority field: -1 means local broadcast */
  434. if(origpriority == -1 && n->prevedge) {
  435. sock = rand() % listen_sockets;
  436. memset(&broadcast, 0, sizeof(broadcast));
  437. if(listen_socket[sock].sa.sa.sa_family == AF_INET6) {
  438. broadcast.in6.sin6_family = AF_INET6;
  439. broadcast.in6.sin6_addr.s6_addr[0x0] = 0xff;
  440. broadcast.in6.sin6_addr.s6_addr[0x1] = 0x02;
  441. broadcast.in6.sin6_addr.s6_addr[0xf] = 0x01;
  442. broadcast.in6.sin6_port = n->prevedge->address.in.sin_port;
  443. broadcast.in6.sin6_scope_id = listen_socket[sock].sa.in6.sin6_scope_id;
  444. } else {
  445. broadcast.in.sin_family = AF_INET;
  446. broadcast.in.sin_addr.s_addr = -1;
  447. broadcast.in.sin_port = n->prevedge->address.in.sin_port;
  448. }
  449. sa = &broadcast.sa;
  450. sl = SALEN(broadcast.sa);
  451. } else {
  452. if(origpriority == -1) {
  453. origpriority = 0;
  454. }
  455. sa = &(n->address.sa);
  456. sl = SALEN(n->address.sa);
  457. sock = n->sock;
  458. }
  459. if(priorityinheritance && origpriority != listen_socket[n->sock].priority) {
  460. listen_socket[n->sock].priority = origpriority;
  461. switch(listen_socket[n->sock].sa.sa.sa_family) {
  462. #if defined(IP_TOS)
  463. case AF_INET:
  464. ifdebug(TRAFFIC) logger(LOG_DEBUG, "Setting IPv4 outgoing packet priority to %d", origpriority);
  465. if(setsockopt(listen_socket[n->sock].udp, IPPROTO_IP, IP_TOS, (void *)&origpriority, sizeof(origpriority))) { /* SO_PRIORITY doesn't seem to work */
  466. logger(LOG_ERR, "System call `%s' failed: %s", "setsockopt", strerror(errno));
  467. }
  468. break;
  469. #endif
  470. #if defined(IPV6_TCLASS)
  471. case AF_INET6:
  472. ifdebug(TRAFFIC) logger(LOG_DEBUG, "Setting IPv6 outgoing packet priority to %d", origpriority);
  473. if(setsockopt(listen_socket[n->sock].udp, IPPROTO_IPV6, IPV6_TCLASS, (void *)&origpriority, sizeof(origpriority))) {
  474. logger(LOG_ERR, "System call `%s' failed: %s", "setsockopt", strerror(errno));
  475. }
  476. break;
  477. #endif
  478. default:
  479. break;
  480. }
  481. }
  482. if(sendto(listen_socket[sock].udp, (char *) &inpkt->seqno, inpkt->len, 0, sa, sl) < 0 && !sockwouldblock(sockerrno)) {
  483. if(sockmsgsize(sockerrno)) {
  484. if(n->maxmtu >= origlen) {
  485. n->maxmtu = origlen - 1;
  486. }
  487. if(n->mtu >= origlen) {
  488. n->mtu = origlen - 1;
  489. }
  490. } else {
  491. ifdebug(TRAFFIC) logger(LOG_WARNING, "Error sending packet to %s (%s): %s", n->name, n->hostname, sockstrerror(sockerrno));
  492. }
  493. }
  494. end:
  495. origpkt->len = origlen;
  496. }
  497. /*
  498. send a packet to the given vpn ip.
  499. */
  500. void send_packet(const node_t *n, vpn_packet_t *packet) {
  501. node_t *via;
  502. if(n == myself) {
  503. if(overwrite_mac) {
  504. memcpy(packet->data, mymac.x, ETH_ALEN);
  505. }
  506. devops.write(packet);
  507. return;
  508. }
  509. ifdebug(TRAFFIC) logger(LOG_ERR, "Sending packet of %d bytes to %s (%s)",
  510. packet->len, n->name, n->hostname);
  511. if(!n->status.reachable) {
  512. ifdebug(TRAFFIC) logger(LOG_INFO, "Node %s (%s) is not reachable",
  513. n->name, n->hostname);
  514. return;
  515. }
  516. via = (packet->priority == -1 || n->via == myself) ? n->nexthop : n->via;
  517. if(via != n)
  518. ifdebug(TRAFFIC) logger(LOG_INFO, "Sending packet to %s via %s (%s)",
  519. n->name, via->name, n->via->hostname);
  520. if(packet->priority == -1 || ((myself->options | via->options) & OPTION_TCPONLY)) {
  521. if(!send_tcppacket(via->connection, packet)) {
  522. terminate_connection(via->connection, true);
  523. }
  524. } else {
  525. send_udppacket(via, packet);
  526. }
  527. }
  528. /* Broadcast a packet using the minimum spanning tree */
  529. void broadcast_packet(const node_t *from, vpn_packet_t *packet) {
  530. avl_node_t *node;
  531. connection_t *c;
  532. node_t *n;
  533. // Always give ourself a copy of the packet.
  534. if(from != myself) {
  535. send_packet(myself, packet);
  536. }
  537. // In TunnelServer mode, do not forward broadcast packets.
  538. // The MST might not be valid and create loops.
  539. if(tunnelserver || broadcast_mode == BMODE_NONE) {
  540. return;
  541. }
  542. ifdebug(TRAFFIC) logger(LOG_INFO, "Broadcasting packet of %d bytes from %s (%s)",
  543. packet->len, from->name, from->hostname);
  544. switch(broadcast_mode) {
  545. // In MST mode, broadcast packets travel via the Minimum Spanning Tree.
  546. // This guarantees all nodes receive the broadcast packet, and
  547. // usually distributes the sending of broadcast packets over all nodes.
  548. case BMODE_MST:
  549. for(node = connection_tree->head; node; node = node->next) {
  550. c = node->data;
  551. if(c->status.active && c->status.mst && c != from->nexthop->connection) {
  552. send_packet(c->node, packet);
  553. }
  554. }
  555. break;
  556. // In direct mode, we send copies to each node we know of.
  557. // However, this only reaches nodes that can be reached in a single hop.
  558. // We don't have enough information to forward broadcast packets in this case.
  559. case BMODE_DIRECT:
  560. if(from != myself) {
  561. break;
  562. }
  563. for(node = node_udp_tree->head; node; node = node->next) {
  564. n = node->data;
  565. if(n->status.reachable && n != myself && ((n->via == myself && n->nexthop == n) || n->via == n)) {
  566. send_packet(n, packet);
  567. }
  568. }
  569. break;
  570. default:
  571. break;
  572. }
  573. }
  574. static node_t *try_harder(const sockaddr_t *from, const vpn_packet_t *pkt) {
  575. avl_node_t *node;
  576. edge_t *e;
  577. node_t *n = NULL;
  578. static time_t last_hard_try = 0;
  579. for(node = edge_weight_tree->head; node; node = node->next) {
  580. e = node->data;
  581. if(e->to == myself) {
  582. continue;
  583. }
  584. if(last_hard_try == now && sockaddrcmp_noport(from, &e->address)) {
  585. continue;
  586. }
  587. if(!try_mac(e->to, pkt)) {
  588. continue;
  589. }
  590. n = e->to;
  591. break;
  592. }
  593. last_hard_try = now;
  594. return n;
  595. }
  596. void handle_incoming_vpn_data(int sock) {
  597. vpn_packet_t pkt;
  598. char *hostname;
  599. sockaddr_t from;
  600. socklen_t fromlen = sizeof(from);
  601. node_t *n;
  602. ssize_t len = recvfrom(listen_socket[sock].udp, (char *) &pkt.seqno, MAXSIZE, 0, &from.sa, &fromlen);
  603. if(len <= 0 || len > UINT16_MAX) {
  604. if(len >= 0) {
  605. logger(LOG_ERR, "Receiving packet with invalid size");
  606. } else if(!sockwouldblock(sockerrno)) {
  607. logger(LOG_ERR, "Receiving packet failed: %s", sockstrerror(sockerrno));
  608. }
  609. return;
  610. }
  611. pkt.len = len;
  612. sockaddrunmap(&from); /* Some braindead IPv6 implementations do stupid things. */
  613. n = lookup_node_udp(&from);
  614. if(!n) {
  615. n = try_harder(&from, &pkt);
  616. if(n) {
  617. update_node_udp(n, &from);
  618. } else ifdebug(PROTOCOL) {
  619. hostname = sockaddr2hostname(&from);
  620. logger(LOG_WARNING, "Received UDP packet from unknown source %s", hostname);
  621. free(hostname);
  622. return;
  623. } else {
  624. return;
  625. }
  626. }
  627. n->sock = sock;
  628. receive_udppacket(n, &pkt);
  629. }