Use free_known_addresses() to free memory allocated by get_known_addresses().
[tinc] / src / meta.c
1 /*
2     meta.c -- handle the meta communication
3     Copyright (C) 2000-2014 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 "cipher.h"
25 #include "connection.h"
26 #include "logger.h"
27 #include "meta.h"
28 #include "net.h"
29 #include "protocol.h"
30 #include "utils.h"
31 #include "xalloc.h"
32
33 #ifndef MIN
34 #define MIN(x, y) (((x)<(y))?(x):(y))
35 #endif
36
37 bool send_meta_sptps(void *handle, uint8_t type, const void *buffer, size_t length) {
38         connection_t *c = handle;
39
40         if(!c) {
41                 logger(DEBUG_ALWAYS, LOG_ERR, "send_meta_sptps() called with NULL pointer!");
42                 abort();
43         }
44
45         buffer_add(&c->outbuf, buffer, length);
46         io_set(&c->io, IO_READ | IO_WRITE);
47
48         return true;
49 }
50
51 bool send_meta(connection_t *c, const char *buffer, int length) {
52         if(!c) {
53                 logger(DEBUG_ALWAYS, LOG_ERR, "send_meta() called with NULL pointer!");
54                 abort();
55         }
56
57         logger(DEBUG_META, LOG_DEBUG, "Sending %d bytes of metadata to %s (%s)", length,
58                            c->name, c->hostname);
59
60         if(c->protocol_minor >= 2)
61                 return sptps_send_record(&c->sptps, 0, buffer, length);
62
63         /* Add our data to buffer */
64         if(c->status.encryptout) {
65 #ifdef DISABLE_LEGACY
66                 return false;
67 #else
68                 if(length > c->outbudget) {
69                         logger(DEBUG_META, LOG_ERR, "Byte limit exceeded for encryption to %s (%s)", c->name, c->hostname);
70                         return false;
71                 } else {
72                         c->outbudget -= length;
73                 }
74
75                 size_t outlen = length;
76
77                 if(!cipher_encrypt(c->outcipher, buffer, length, buffer_prepare(&c->outbuf, length), &outlen, false) || outlen != length) {
78                         logger(DEBUG_ALWAYS, LOG_ERR, "Error while encrypting metadata to %s (%s)",
79                                         c->name, c->hostname);
80                         return false;
81                 }
82 #endif
83         } else {
84                 buffer_add(&c->outbuf, buffer, length);
85         }
86
87         io_set(&c->io, IO_READ | IO_WRITE);
88
89         return true;
90 }
91
92 void send_meta_raw(connection_t *c, const char *buffer, int length) {
93         if(!c) {
94                 logger(DEBUG_ALWAYS, LOG_ERR, "send_meta() called with NULL pointer!");
95                 abort();
96         }
97
98         logger(DEBUG_META, LOG_DEBUG, "Sending %d bytes of raw metadata to %s (%s)", length,
99                            c->name, c->hostname);
100
101         buffer_add(&c->outbuf, buffer, length);
102
103         io_set(&c->io, IO_READ | IO_WRITE);
104 }
105
106 void broadcast_meta(connection_t *from, const char *buffer, int length) {
107         for list_each(connection_t, c, connection_list)
108                 if(c != from && c->edge)
109                         send_meta(c, buffer, length);
110 }
111
112 bool receive_meta_sptps(void *handle, uint8_t type, const void *vdata, uint16_t length) {
113         const char *data = vdata;
114         connection_t *c = handle;
115
116         if(!c) {
117                 logger(DEBUG_ALWAYS, LOG_ERR, "receive_meta_sptps() called with NULL pointer!");
118                 abort();
119         }
120
121         if(type == SPTPS_HANDSHAKE) {
122                 if(c->allow_request == ACK)
123                         return send_ack(c);
124                 else
125                         return true;
126         }
127
128         if(!data)
129                 return true;
130
131         /* Are we receiving a TCPpacket? */
132
133         if(c->tcplen) {
134                 if(length != c->tcplen)
135                         return false;
136                 receive_tcppacket(c, data, length);
137                 c->tcplen = 0;
138                 return true;
139         }
140
141         /* Change newline to null byte, just like non-SPTPS requests */
142
143         if(data[length - 1] == '\n')
144                 ((char *)data)[length - 1] = 0;
145
146         /* Otherwise we are waiting for a request */
147
148         return receive_request(c, data);
149 }
150
151 bool receive_meta(connection_t *c) {
152         int inlen;
153         char inbuf[MAXBUFSIZE];
154         char *bufp = inbuf, *endp;
155
156         /* Strategy:
157            - Read as much as possible from the TCP socket in one go.
158            - Decrypt it.
159            - Check if a full request is in the input buffer.
160            - If yes, process request and remove it from the buffer,
161            then check again.
162            - If not, keep stuff in buffer and exit.
163          */
164
165         buffer_compact(&c->inbuf, MAXBUFSIZE);
166
167         if(sizeof inbuf <= c->inbuf.len) {
168                 logger(DEBUG_ALWAYS, LOG_ERR, "Input buffer full for %s (%s)", c->name, c->hostname);
169                 return false;
170         }
171
172         inlen = recv(c->socket, inbuf, sizeof inbuf - c->inbuf.len, 0);
173
174         if(inlen <= 0) {
175                 if(!inlen || !sockerrno) {
176                         logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Connection closed by %s (%s)",
177                                            c->name, c->hostname);
178                 } else if(sockwouldblock(sockerrno))
179                         return true;
180                 else
181                         logger(DEBUG_ALWAYS, LOG_ERR, "Metadata socket read error for %s (%s): %s",
182                                    c->name, c->hostname, sockstrerror(sockerrno));
183                 return false;
184         }
185
186         do {
187                 /* Are we receiving a SPTPS packet? */
188
189                 if(c->sptpslen) {
190                         int len = MIN(inlen, c->sptpslen - c->inbuf.len);
191                         buffer_add(&c->inbuf, bufp, len);
192
193                         char *sptpspacket = buffer_read(&c->inbuf, c->sptpslen);
194                         if(!sptpspacket)
195                                 return true;
196
197                         if(!receive_tcppacket_sptps(c, sptpspacket, c->sptpslen))
198                                 return false;
199                         c->sptpslen = 0;
200
201                         bufp += len;
202                         inlen -= len;
203                         continue;
204                 }
205
206                 if(c->protocol_minor >= 2) {
207                         int len = sptps_receive_data(&c->sptps, bufp, inlen);
208                         if(!len)
209                                 return false;
210                         bufp += len;
211                         inlen -= len;
212                         continue;
213                 }
214
215                 if(!c->status.decryptin) {
216                         endp = memchr(bufp, '\n', inlen);
217                         if(endp)
218                                 endp++;
219                         else
220                                 endp = bufp + inlen;
221
222                         buffer_add(&c->inbuf, bufp, endp - bufp);
223
224                         inlen -= endp - bufp;
225                         bufp = endp;
226                 } else {
227 #ifdef DISABLE_LEGACY
228                         return false;
229 #else
230                         if(inlen > c->inbudget) {
231                                 logger(DEBUG_META, LOG_ERR, "yte limit exceeded for decryption from %s (%s)", c->name, c->hostname);
232                                 return false;
233                         } else {
234                                 c->inbudget -= inlen;
235                         }
236
237                         size_t outlen = inlen;
238
239                         if(!cipher_decrypt(c->incipher, bufp, inlen, buffer_prepare(&c->inbuf, inlen), &outlen, false) || inlen != outlen) {
240                                 logger(DEBUG_ALWAYS, LOG_ERR, "Error while decrypting metadata from %s (%s)",
241                                            c->name, c->hostname);
242                                 return false;
243                         }
244
245                         inlen = 0;
246 #endif
247                 }
248
249                 while(c->inbuf.len) {
250                         /* Are we receiving a TCPpacket? */
251
252                         if(c->tcplen) {
253                                 char *tcpbuffer = buffer_read(&c->inbuf, c->tcplen);
254                                 if(!tcpbuffer)
255                                         break;
256
257                                 if(!c->node) {
258                                         if(c->outgoing && proxytype == PROXY_SOCKS4 && c->allow_request == ID) {
259                                                 if(tcpbuffer[0] == 0 && tcpbuffer[1] == 0x5a) {
260                                                         logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Proxy request granted");
261                                                 } else {
262                                                         logger(DEBUG_CONNECTIONS, LOG_ERR, "Proxy request rejected");
263                                                         return false;
264                                                 }
265                                         } else if(c->outgoing && proxytype == PROXY_SOCKS5 && c->allow_request == ID) {
266                                                 if(tcpbuffer[0] != 5) {
267                                                         logger(DEBUG_CONNECTIONS, LOG_ERR, "Invalid response from proxy server");
268                                                         return false;
269                                                 }
270                                                 if(tcpbuffer[1] == (char)0xff) {
271                                                         logger(DEBUG_CONNECTIONS, LOG_ERR, "Proxy request rejected: unsuitable authentication method");
272                                                         return false;
273                                                 }
274                                                 if(tcpbuffer[2] != 5) {
275                                                         logger(DEBUG_CONNECTIONS, LOG_ERR, "Invalid response from proxy server");
276                                                         return false;
277                                                 }
278                                                 if(tcpbuffer[3] == 0) {
279                                                         logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Proxy request granted");
280                                                 } else {
281                                                         logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Proxy request rejected");
282                                                         return false;
283                                                 }
284                                         } else {
285                                                 logger(DEBUG_CONNECTIONS, LOG_ERR, "c->tcplen set but c->node is NULL!");
286                                                 abort();
287                                         }
288                                 } else {
289                                         if(c->allow_request == ALL) {
290                                                 receive_tcppacket(c, tcpbuffer, c->tcplen);
291                                         } else {
292                                                 logger(DEBUG_CONNECTIONS, LOG_ERR, "Got unauthorized TCP packet from %s (%s)", c->name, c->hostname);
293                                                 return false;
294                                         }
295                                 }
296
297                                 c->tcplen = 0;
298                         }
299
300                         /* Otherwise we are waiting for a request */
301
302                         char *request = buffer_readline(&c->inbuf);
303                         if(request) {
304                                 bool result = receive_request(c, request);
305                                 if(!result)
306                                         return false;
307                                 continue;
308                         } else {
309                                 break;
310                         }
311                 }
312         } while(inlen);
313
314         return true;
315 }