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