02dbf5bf6aa8c44bc6f8ac4faefe0a10525b90b5
[tinc] / src / protocol.c
1 /*
2     protocol.c -- handle the meta-protocol, basic functions
3     Copyright (C) 1999-2005 Ivo Timmermans,
4                   2000-2022 Guus Sliepen <guus@tinc-vpn.org>
5
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
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
16     You should have received a copy of the GNU General Public License along
17     with this program; if not, write to the Free Software Foundation, Inc.,
18     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "system.h"
22
23 #include "conf.h"
24 #include "connection.h"
25 #include "crypto.h"
26 #include "logger.h"
27 #include "meta.h"
28 #include "protocol.h"
29 #include "utils.h"
30 #include "xalloc.h"
31
32 bool tunnelserver = false;
33 bool strictsubnets = false;
34 bool experimental = true;
35
36 /* Jumptable for the request handlers */
37
38 static bool (*request_handlers[])(connection_t *, const char *) = {
39         id_h, metakey_h, challenge_h, chal_reply_h, ack_h,
40         NULL, NULL, termreq_h,
41         ping_h, pong_h,
42         add_subnet_h, del_subnet_h,
43         add_edge_h, del_edge_h,
44         key_changed_h, req_key_h, ans_key_h, tcppacket_h, control_h,
45         NULL, NULL, /* Not "real" requests (yet) */
46         sptps_tcppacket_h,
47         udp_info_h, mtu_info_h,
48 };
49
50 /* Request names */
51
52 static const char (*request_name[]) = {
53         "ID", "METAKEY", "CHALLENGE", "CHAL_REPLY", "ACK",
54         "STATUS", "ERROR", "TERMREQ",
55         "PING", "PONG",
56         "ADD_SUBNET", "DEL_SUBNET",
57         "ADD_EDGE", "DEL_EDGE", "KEY_CHANGED", "REQ_KEY", "ANS_KEY", "PACKET", "CONTROL",
58         "REQ_PUBKEY", "ANS_PUBKEY", "SPTPS_PACKET", "UDP_INFO", "MTU_INFO",
59 };
60
61 static int past_request_compare(const past_request_t *a, const past_request_t *b) {
62         return strcmp(a->request, b->request);
63 }
64
65 static void free_past_request(past_request_t *r) {
66         free((char *)r->request);
67         free(r);
68 }
69
70 static splay_tree_t past_request_tree = {
71         .compare = (splay_compare_t) past_request_compare,
72         .delete = (splay_action_t) free_past_request,
73 };
74
75 /* Generic request routines - takes care of logging and error
76    detection as well */
77
78 bool send_request(connection_t *c, const char *format, ...) {
79         va_list args;
80         char request[MAXBUFSIZE];
81         int len;
82
83         /* Use vsnprintf instead of vxasprintf: faster, no memory
84            fragmentation, cleanup is automatic, and there is a limit on the
85            input buffer anyway */
86
87         va_start(args, format);
88         len = vsnprintf(request, sizeof(request), format, args);
89         request[sizeof(request) - 1] = 0;
90         va_end(args);
91
92         if(len < 0 || (size_t)len > sizeof(request) - 1) {
93                 logger(DEBUG_ALWAYS, LOG_ERR, "Output buffer overflow while sending request to %s (%s)",
94                        c->name, c->hostname);
95                 return false;
96         }
97
98         int id = atoi(request);
99         logger(DEBUG_META, LOG_DEBUG, "Sending %s to %s (%s): %s", request_name[id], c->name, c->hostname, request);
100
101         request[len++] = '\n';
102
103         if(c == everyone) {
104                 broadcast_meta(NULL, request, len);
105                 return true;
106         } else {
107                 if(id) {
108                         return send_meta(c, request, len);
109                 } else {
110                         send_meta_raw(c, request, len);
111                         return true;
112                 }
113         }
114 }
115
116 void forward_request(connection_t *from, const char *request) {
117         logger(DEBUG_META, LOG_DEBUG, "Forwarding %s from %s (%s): %s", request_name[atoi(request)], from->name, from->hostname, request);
118
119         // Create a temporary newline-terminated copy of the request
120         size_t len = strlen(request);
121         const size_t tmplen = len + 1;
122         char *tmp = alloca(tmplen);
123         memcpy(tmp, request, len);
124         tmp[len] = '\n';
125         broadcast_meta(from, tmp, tmplen);
126 }
127
128 bool receive_request(connection_t *c, const char *request) {
129         if(c->outgoing && proxytype == PROXY_HTTP && c->allow_request == ID) {
130                 if(!request[0] || request[0] == '\r') {
131                         return true;
132                 }
133
134                 if(!strncasecmp(request, "HTTP/1.1 ", 9)) {
135                         if(!strncmp(request + 9, "200", 3)) {
136                                 logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Proxy request granted");
137                                 return true;
138                         } else {
139                                 logger(DEBUG_ALWAYS, LOG_DEBUG, "Proxy request rejected: %s", request + 9);
140                                 return false;
141                         }
142                 }
143         }
144
145         int reqno = atoi(request);
146
147         if(reqno || *request == '0') {
148                 if((reqno < 0) || (reqno >= LAST) || !request_handlers[reqno]) {
149                         logger(DEBUG_META, LOG_DEBUG, "Unknown request from %s (%s): %s", c->name, c->hostname, request);
150                         return false;
151                 } else {
152                         logger(DEBUG_META, LOG_DEBUG, "Got %s from %s (%s): %s", request_name[reqno], c->name, c->hostname, request);
153                 }
154
155                 if((c->allow_request != ALL) && (c->allow_request != reqno)) {
156                         logger(DEBUG_ALWAYS, LOG_ERR, "Unauthorized request from %s (%s)", c->name, c->hostname);
157                         return false;
158                 }
159
160                 if(!request_handlers[reqno](c, request)) {
161                         /* Something went wrong. Probably scriptkiddies. Terminate. */
162
163                         if(reqno != TERMREQ) {
164                                 logger(DEBUG_ALWAYS, LOG_ERR, "Error while processing %s from %s (%s)", request_name[reqno], c->name, c->hostname);
165                         }
166
167                         return false;
168                 }
169         } else {
170                 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus data received from %s (%s)", c->name, c->hostname);
171                 return false;
172         }
173
174         return true;
175 }
176
177 static timeout_t past_request_timeout;
178
179 static void age_past_requests(void *data) {
180         (void)data;
181         int left = 0, deleted = 0;
182
183         for splay_each(past_request_t, p, &past_request_tree) {
184                 if(p->firstseen + pinginterval <= now.tv_sec) {
185                         splay_delete_node(&past_request_tree, node), deleted++;
186                 } else {
187                         left++;
188                 }
189         }
190
191         if(left || deleted) {
192                 logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Aging past requests: deleted %d, left %d", deleted, left);
193         }
194
195         if(left)
196                 timeout_set(&past_request_timeout, &(struct timeval) {
197                 10, jitter()
198         });
199 }
200
201 bool seen_request(const char *request) {
202         past_request_t *new, p = {0};
203
204         p.request = request;
205
206         if(splay_search(&past_request_tree, &p)) {
207                 logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Already seen request");
208                 return true;
209         } else {
210                 new = xmalloc(sizeof(*new));
211                 new->request = xstrdup(request);
212                 new->firstseen = now.tv_sec;
213                 splay_insert(&past_request_tree, new);
214                 timeout_add(&past_request_timeout, age_past_requests, NULL, &(struct timeval) {
215                         10, jitter()
216                 });
217                 return false;
218         }
219 }
220
221 void exit_requests(void) {
222         splay_empty_tree(&past_request_tree);
223
224         timeout_del(&past_request_timeout);
225 }