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>
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.
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.
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.
24 #include "connection.h"
32 bool tunnelserver = false;
33 bool strictsubnets = false;
34 bool experimental = true;
36 static inline bool is_valid_request(request_t req) {
37 return req > ALL && req < LAST;
40 /* Request handlers */
41 const request_entry_t *get_request_entry(request_t req) {
42 if(!is_valid_request(req)) {
43 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid request %d", req);
47 // Prevent user from accessing the table directly to always have bound checks
48 static const request_entry_t request_entries[] = {
50 [METAKEY] = {metakey_h, "METAKEY"},
51 [CHALLENGE] = {challenge_h, "CHALLENGE"},
52 [CHAL_REPLY] = {chal_reply_h, "CHAL_REPLY"},
53 [ACK] = {ack_h, "ACK"},
54 [STATUS] = {NULL, "STATUS"},
55 [ERROR] = {NULL, "ERROR"},
56 [TERMREQ] = {termreq_h, "TERMREQ"},
57 [PING] = {ping_h, "PING"},
58 [PONG] = {pong_h, "PONG"},
59 [ADD_SUBNET] = {add_subnet_h, "ADD_SUBNET"},
60 [DEL_SUBNET] = {del_subnet_h, "DEL_SUBNET"},
61 [ADD_EDGE] = {add_edge_h, "ADD_EDGE"},
62 [DEL_EDGE] = {del_edge_h, "DEL_EDGE"},
63 [KEY_CHANGED] = {key_changed_h, "KEY_CHANGED"},
64 [REQ_KEY] = {req_key_h, "REQ_KEY"},
65 [ANS_KEY] = {ans_key_h, "ANS_KEY"},
66 [PACKET] = {tcppacket_h, "PACKET"},
67 [CONTROL] = {control_h, "CONTROL"},
68 /* Not "real" requests yet */
69 [REQ_PUBKEY] = {NULL, "REQ_PUBKEY"},
70 [ANS_PUBKEY] = {NULL, "ANS_PUBKEY"},
71 [SPTPS_PACKET] = {sptps_tcppacket_h, "SPTPS_PACKET"},
72 [UDP_INFO] = {udp_info_h, "UDP_INFO"},
73 [MTU_INFO] = {mtu_info_h, "MTU_INFO"},
75 return &request_entries[req];
78 static int past_request_compare(const past_request_t *a, const past_request_t *b) {
79 return strcmp(a->request, b->request);
82 static void free_past_request(past_request_t *r) {
84 free((char *)r->request);
89 static splay_tree_t past_request_tree = {
90 .compare = (splay_compare_t) past_request_compare,
91 .delete = (splay_action_t) free_past_request,
94 /* Generic request routines - takes care of logging and error
97 bool send_request(connection_t *c, const char *format, ...) {
99 char request[MAXBUFSIZE];
102 /* Use vsnprintf instead of vxasprintf: faster, no memory
103 fragmentation, cleanup is automatic, and there is a limit on the
104 input buffer anyway */
106 va_start(args, format);
107 len = vsnprintf(request, sizeof(request), format, args);
108 request[sizeof(request) - 1] = 0;
111 if(len < 0 || (size_t)len > sizeof(request) - 1) {
112 logger(DEBUG_ALWAYS, LOG_ERR, "Output buffer overflow while sending request to %s (%s)",
113 c->name, c->hostname);
117 int id = atoi(request);
118 logger(DEBUG_META, LOG_DEBUG, "Sending %s to %s (%s): %s", get_request_entry(id)->name, c->name, c->hostname, request);
120 request[len++] = '\n';
123 broadcast_meta(NULL, request, len);
127 return send_meta(c, request, len);
129 send_meta_raw(c, request, len);
135 void forward_request(connection_t *from, const char *request) {
136 logger(DEBUG_META, LOG_DEBUG, "Forwarding %s from %s (%s): %s", get_request_entry(atoi(request))->name, from->name, from->hostname, request);
138 // Create a temporary newline-terminated copy of the request
139 size_t len = strlen(request);
140 const size_t tmplen = len + 1;
141 char *tmp = alloca(tmplen);
142 memcpy(tmp, request, len);
144 broadcast_meta(from, tmp, tmplen);
147 bool receive_request(connection_t *c, const char *request) {
148 if(c->outgoing && proxytype == PROXY_HTTP && c->allow_request == ID) {
149 if(!request[0] || request[0] == '\r') {
153 if(!strncasecmp(request, "HTTP/1.1 ", 9)) {
154 if(!strncmp(request + 9, "200", 3)) {
155 logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Proxy request granted");
158 logger(DEBUG_ALWAYS, LOG_DEBUG, "Proxy request rejected: %s", request + 9);
164 int reqno = atoi(request);
166 if(reqno || *request == '0') {
167 if(!is_valid_request(reqno) || !get_request_entry(reqno)->handler) {
168 logger(DEBUG_META, LOG_DEBUG, "Unknown request from %s (%s): %s", c->name, c->hostname, request);
172 const request_entry_t *entry = get_request_entry(reqno);
173 logger(DEBUG_META, LOG_DEBUG, "Got %s from %s (%s): %s", entry->name, c->name, c->hostname, request);
175 if((c->allow_request != ALL) && (c->allow_request != reqno)) {
176 logger(DEBUG_ALWAYS, LOG_ERR, "Unauthorized request from %s (%s)", c->name, c->hostname);
180 if(!entry->handler(c, request)) {
181 /* Something went wrong. Probably scriptkiddies. Terminate. */
183 if(reqno != TERMREQ) {
184 logger(DEBUG_ALWAYS, LOG_ERR, "Error while processing %s from %s (%s)", entry->name, c->name, c->hostname);
190 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus data received from %s (%s)", c->name, c->hostname);
197 static timeout_t past_request_timeout;
199 static void age_past_requests(void *data) {
201 int left = 0, deleted = 0;
203 for splay_each(past_request_t, p, &past_request_tree) {
204 if(p->firstseen + pinginterval <= now.tv_sec) {
205 splay_delete_node(&past_request_tree, node), deleted++;
211 if(left || deleted) {
212 logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Aging past requests: deleted %d, left %d", deleted, left);
216 timeout_set(&past_request_timeout, &(struct timeval) {
221 bool seen_request(const char *request) {
222 past_request_t *new, p = {0};
226 if(splay_search(&past_request_tree, &p)) {
227 logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Already seen request");
230 new = xmalloc(sizeof(*new));
231 new->request = xstrdup(request);
232 new->firstseen = now.tv_sec;
233 splay_insert(&past_request_tree, new);
234 timeout_add(&past_request_timeout, age_past_requests, NULL, &(struct timeval) {
241 void exit_requests(void) {
242 splay_empty_tree(&past_request_tree);
244 timeout_del(&past_request_timeout);