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) {
83 free((char *)r->request);
87 static splay_tree_t past_request_tree = {
88 .compare = (splay_compare_t) past_request_compare,
89 .delete = (splay_action_t) free_past_request,
92 /* Generic request routines - takes care of logging and error
95 bool send_request(connection_t *c, const char *format, ...) {
97 char request[MAXBUFSIZE];
100 /* Use vsnprintf instead of vxasprintf: faster, no memory
101 fragmentation, cleanup is automatic, and there is a limit on the
102 input buffer anyway */
104 va_start(args, format);
105 len = vsnprintf(request, sizeof(request), format, args);
106 request[sizeof(request) - 1] = 0;
109 if(len < 0 || (size_t)len > sizeof(request) - 1) {
110 logger(DEBUG_ALWAYS, LOG_ERR, "Output buffer overflow while sending request to %s (%s)",
111 c->name, c->hostname);
115 int id = atoi(request);
116 logger(DEBUG_META, LOG_DEBUG, "Sending %s to %s (%s): %s", get_request_entry(id)->name, c->name, c->hostname, request);
118 request[len++] = '\n';
121 broadcast_meta(NULL, request, len);
125 return send_meta(c, request, len);
127 send_meta_raw(c, request, len);
133 void forward_request(connection_t *from, const char *request) {
134 logger(DEBUG_META, LOG_DEBUG, "Forwarding %s from %s (%s): %s", get_request_entry(atoi(request))->name, from->name, from->hostname, request);
136 // Create a temporary newline-terminated copy of the request
137 size_t len = strlen(request);
138 const size_t tmplen = len + 1;
139 char *tmp = alloca(tmplen);
140 memcpy(tmp, request, len);
142 broadcast_meta(from, tmp, tmplen);
145 bool receive_request(connection_t *c, const char *request) {
146 if(c->outgoing && proxytype == PROXY_HTTP && c->allow_request == ID) {
147 if(!request[0] || request[0] == '\r') {
151 if(!strncasecmp(request, "HTTP/1.1 ", 9)) {
152 if(!strncmp(request + 9, "200", 3)) {
153 logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Proxy request granted");
156 logger(DEBUG_ALWAYS, LOG_DEBUG, "Proxy request rejected: %s", request + 9);
162 int reqno = atoi(request);
164 if(reqno || *request == '0') {
165 if(!is_valid_request(reqno) || !get_request_entry(reqno)->handler) {
166 logger(DEBUG_META, LOG_DEBUG, "Unknown request from %s (%s): %s", c->name, c->hostname, request);
170 const request_entry_t *entry = get_request_entry(reqno);
171 logger(DEBUG_META, LOG_DEBUG, "Got %s from %s (%s): %s", entry->name, c->name, c->hostname, request);
173 if((c->allow_request != ALL) && (c->allow_request != reqno)) {
174 logger(DEBUG_ALWAYS, LOG_ERR, "Unauthorized request from %s (%s)", c->name, c->hostname);
178 if(!entry->handler(c, request)) {
179 /* Something went wrong. Probably scriptkiddies. Terminate. */
181 if(reqno != TERMREQ) {
182 logger(DEBUG_ALWAYS, LOG_ERR, "Error while processing %s from %s (%s)", entry->name, c->name, c->hostname);
188 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus data received from %s (%s)", c->name, c->hostname);
195 static timeout_t past_request_timeout;
197 static void age_past_requests(void *data) {
199 int left = 0, deleted = 0;
201 for splay_each(past_request_t, p, &past_request_tree) {
202 if(p->firstseen + pinginterval <= now.tv_sec) {
203 splay_delete_node(&past_request_tree, node), deleted++;
209 if(left || deleted) {
210 logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Aging past requests: deleted %d, left %d", deleted, left);
214 timeout_set(&past_request_timeout, &(struct timeval) {
219 bool seen_request(const char *request) {
220 past_request_t *new, p = {0};
224 if(splay_search(&past_request_tree, &p)) {
225 logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Already seen request");
228 new = xmalloc(sizeof(*new));
229 new->request = xstrdup(request);
230 new->firstseen = now.tv_sec;
231 splay_insert(&past_request_tree, new);
232 timeout_add(&past_request_timeout, age_past_requests, NULL, &(struct timeval) {
239 void exit_requests(void) {
240 splay_empty_tree(&past_request_tree);
242 timeout_del(&past_request_timeout);