meta.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. meta.c -- handle the meta communication
  3. Copyright (C) 2000-2017 Guus Sliepen <guus@tinc-vpn.org>,
  4. 2000-2005 Ivo Timmermans
  5. 2006 Scott Lamb <slamb@slamb.org>
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License along
  15. with this program; if not, write to the Free Software Foundation, Inc.,
  16. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18. #include "system.h"
  19. #include <openssl/err.h>
  20. #include <openssl/evp.h>
  21. #include "avl_tree.h"
  22. #include "connection.h"
  23. #include "logger.h"
  24. #include "meta.h"
  25. #include "net.h"
  26. #include "protocol.h"
  27. #include "proxy.h"
  28. #include "utils.h"
  29. #include "xalloc.h"
  30. bool send_meta(connection_t *c, const char *buffer, int length) {
  31. int outlen;
  32. int result;
  33. ifdebug(META) logger(LOG_DEBUG, "Sending %d bytes of metadata to %s (%s)", length,
  34. c->name, c->hostname);
  35. if(!c->outbuflen) {
  36. c->last_flushed_time = now;
  37. }
  38. /* Find room in connection's buffer */
  39. if(length + c->outbuflen > c->outbufsize) {
  40. c->outbufsize = length + c->outbuflen;
  41. c->outbuf = xrealloc(c->outbuf, c->outbufsize);
  42. }
  43. if(length + c->outbuflen + c->outbufstart > c->outbufsize) {
  44. memmove(c->outbuf, c->outbuf + c->outbufstart, c->outbuflen);
  45. c->outbufstart = 0;
  46. }
  47. /* Add our data to buffer */
  48. if(c->status.encryptout) {
  49. /* Check encryption limits */
  50. if((uint64_t)length > c->outbudget) {
  51. ifdebug(META) logger(LOG_ERR, "Byte limit exceeded for encryption to %s (%s)", c->name, c->hostname);
  52. return false;
  53. } else {
  54. c->outbudget -= length;
  55. }
  56. result = EVP_EncryptUpdate(c->outctx, (unsigned char *)c->outbuf + c->outbufstart + c->outbuflen,
  57. &outlen, (unsigned char *)buffer, length);
  58. if(!result || outlen < length) {
  59. logger(LOG_ERR, "Error while encrypting metadata to %s (%s): %s",
  60. c->name, c->hostname, ERR_error_string(ERR_get_error(), NULL));
  61. return false;
  62. } else if(outlen > length) {
  63. logger(LOG_EMERG, "Encrypted data too long! Heap corrupted!");
  64. abort();
  65. }
  66. c->outbuflen += outlen;
  67. } else {
  68. memcpy(c->outbuf + c->outbufstart + c->outbuflen, buffer, length);
  69. c->outbuflen += length;
  70. }
  71. return true;
  72. }
  73. bool flush_meta(connection_t *c) {
  74. int result;
  75. ifdebug(META) logger(LOG_DEBUG, "Flushing %d bytes to %s (%s)",
  76. c->outbuflen, c->name, c->hostname);
  77. while(c->outbuflen) {
  78. result = send(c->socket, c->outbuf + c->outbufstart, c->outbuflen, 0);
  79. if(result <= 0) {
  80. if(!errno || errno == EPIPE) {
  81. ifdebug(CONNECTIONS) logger(LOG_NOTICE, "Connection closed by %s (%s)",
  82. c->name, c->hostname);
  83. } else if(errno == EINTR) {
  84. continue;
  85. } else if(sockwouldblock(sockerrno)) {
  86. ifdebug(META) logger(LOG_DEBUG, "Flushing %d bytes to %s (%s) would block",
  87. c->outbuflen, c->name, c->hostname);
  88. return true;
  89. } else {
  90. logger(LOG_ERR, "Flushing meta data to %s (%s) failed: %s", c->name,
  91. c->hostname, sockstrerror(sockerrno));
  92. }
  93. return false;
  94. }
  95. c->outbufstart += result;
  96. c->outbuflen -= result;
  97. }
  98. c->outbufstart = 0; /* avoid unnecessary memmoves */
  99. return true;
  100. }
  101. void broadcast_meta(connection_t *from, const char *buffer, int length) {
  102. avl_node_t *node;
  103. connection_t *c;
  104. for(node = connection_tree->head; node; node = node->next) {
  105. c = node->data;
  106. if(c != from && c->status.active) {
  107. send_meta(c, buffer, length);
  108. }
  109. }
  110. }
  111. bool receive_meta(connection_t *c) {
  112. int oldlen, i, result;
  113. int lenin, lenout, reqlen;
  114. bool decrypted = false;
  115. char inbuf[MAXBUFSIZE];
  116. /* Strategy:
  117. - Read as much as possible from the TCP socket in one go.
  118. - Decrypt it.
  119. - Check if a full request is in the input buffer.
  120. - If yes, process request and remove it from the buffer,
  121. then check again.
  122. - If not, keep stuff in buffer and exit.
  123. */
  124. lenin = recv(c->socket, c->buffer + c->buflen, MAXBUFSIZE - c->buflen, 0);
  125. if(lenin <= 0) {
  126. if(!lenin || !errno) {
  127. ifdebug(CONNECTIONS) logger(LOG_NOTICE, "Connection closed by %s (%s)",
  128. c->name, c->hostname);
  129. } else if(sockwouldblock(sockerrno)) {
  130. return true;
  131. } else
  132. logger(LOG_ERR, "Metadata socket read error for %s (%s): %s",
  133. c->name, c->hostname, sockstrerror(sockerrno));
  134. return false;
  135. }
  136. oldlen = c->buflen;
  137. c->buflen += lenin;
  138. while(lenin > 0) {
  139. reqlen = 0;
  140. /* Is it proxy metadata? */
  141. if(c->allow_request == PROXY) {
  142. reqlen = receive_proxy_meta(c);
  143. if(reqlen < 0) {
  144. return false;
  145. }
  146. goto consume;
  147. }
  148. /* Decrypt */
  149. if(c->status.decryptin && !decrypted) {
  150. /* Check decryption limits */
  151. if((uint64_t)lenin > c->inbudget) {
  152. ifdebug(META) logger(LOG_ERR, "Byte limit exceeded for decryption from %s (%s)", c->name, c->hostname);
  153. return false;
  154. } else {
  155. c->inbudget -= lenin;
  156. }
  157. result = EVP_DecryptUpdate(c->inctx, (unsigned char *)inbuf, &lenout, (unsigned char *)c->buffer + oldlen, lenin);
  158. if(!result || lenout != lenin) {
  159. logger(LOG_ERR, "Error while decrypting metadata from %s (%s): %s",
  160. c->name, c->hostname, ERR_error_string(ERR_get_error(), NULL));
  161. return false;
  162. }
  163. memcpy(c->buffer + oldlen, inbuf, lenin);
  164. decrypted = true;
  165. }
  166. /* Are we receiving a TCPpacket? */
  167. if(c->tcplen) {
  168. if(c->tcplen <= c->buflen) {
  169. if(c->allow_request != ALL) {
  170. logger(LOG_ERR, "Got unauthorized TCP packet from %s (%s)", c->name, c->hostname);
  171. return false;
  172. }
  173. receive_tcppacket(c, c->buffer, c->tcplen);
  174. reqlen = c->tcplen;
  175. c->tcplen = 0;
  176. }
  177. } else {
  178. /* Otherwise we are waiting for a request */
  179. for(i = oldlen; i < c->buflen; i++) {
  180. if(c->buffer[i] == '\n') {
  181. c->buffer[i] = '\0'; /* replace end-of-line by end-of-string so we can use sscanf */
  182. c->reqlen = reqlen = i + 1;
  183. break;
  184. }
  185. }
  186. if(reqlen && !receive_request(c)) {
  187. return false;
  188. }
  189. }
  190. consume:
  191. if(reqlen) {
  192. c->buflen -= reqlen;
  193. lenin -= reqlen - oldlen;
  194. memmove(c->buffer, c->buffer + reqlen, c->buflen);
  195. oldlen = 0;
  196. continue;
  197. } else {
  198. break;
  199. }
  200. }
  201. if(c->buflen >= MAXBUFSIZE) {
  202. logger(LOG_ERR, "Metadata read buffer overflow for %s (%s)",
  203. c->name, c->hostname);
  204. return false;
  205. }
  206. return true;
  207. }