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