]> git.meshlink.io Git - meshlink/blob - src/protocol.c
Merge branch 'master' of git://tinc-vpn.org/tinc into 1.1
[meshlink] / src / protocol.c
1 /*
2     protocol.c -- handle the meta-protocol, basic functions
3     Copyright (C) 1999-2005 Ivo Timmermans,
4                   2000-2009 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 "logger.h"
26 #include "meta.h"
27 #include "protocol.h"
28 #include "utils.h"
29 #include "xalloc.h"
30
31 bool tunnelserver = false;
32 bool strictsubnets = false;
33 bool experimental = false;
34
35 /* Jumptable for the request handlers */
36
37 static bool (*request_handlers[])(connection_t *, char *) = {
38                 id_h, metakey_h, challenge_h, chal_reply_h, ack_h,
39                 status_h, error_h, termreq_h,
40                 ping_h, pong_h,
41                 add_subnet_h, del_subnet_h,
42                 add_edge_h, del_edge_h,
43                 key_changed_h, req_key_h, ans_key_h, tcppacket_h, control_h,
44 };
45
46 /* Request names */
47
48 static char (*request_name[]) = {
49                 "ID", "METAKEY", "CHALLENGE", "CHAL_REPLY", "ACK",
50                 "STATUS", "ERROR", "TERMREQ",
51                 "PING", "PONG",
52                 "ADD_SUBNET", "DEL_SUBNET",
53                 "ADD_EDGE", "DEL_EDGE", "KEY_CHANGED", "REQ_KEY", "ANS_KEY", "PACKET", "CONTROL",
54 };
55
56 static splay_tree_t *past_request_tree;
57
58 bool check_id(const char *id) {
59         for(; *id; id++)
60                 if(!isalnum(*id) && *id != '_')
61                         return false;
62
63         return true;
64 }
65
66 /* Generic request routines - takes care of logging and error
67    detection as well */
68
69 bool send_request(connection_t *c, const char *format, ...) {
70         va_list args;
71         char request[MAXBUFSIZE];
72         int len;
73
74         /* Use vsnprintf instead of vxasprintf: faster, no memory
75            fragmentation, cleanup is automatic, and there is a limit on the
76            input buffer anyway */
77
78         va_start(args, format);
79         len = vsnprintf(request, MAXBUFSIZE, format, args);
80         va_end(args);
81
82         if(len < 0 || len > MAXBUFSIZE - 1) {
83                 logger(LOG_ERR, "Output buffer overflow while sending request to %s (%s)",
84                            c->name, c->hostname);
85                 return false;
86         }
87
88         ifdebug(PROTOCOL) {
89                 ifdebug(META)
90                         logger(LOG_DEBUG, "Sending %s to %s (%s): %s",
91                                    request_name[atoi(request)], c->name, c->hostname, request);
92                 else
93                         logger(LOG_DEBUG, "Sending %s to %s (%s)", request_name[atoi(request)],
94                                    c->name, c->hostname);
95         }
96
97         request[len++] = '\n';
98
99         if(c == everyone) {
100                 broadcast_meta(NULL, request, len);
101                 return true;
102         } else
103                 return send_meta(c, request, len);
104 }
105
106 void forward_request(connection_t *from, char *request) {
107         /* Note: request is not zero terminated anymore after a call to this function! */
108         ifdebug(PROTOCOL) {
109                 ifdebug(META)
110                         logger(LOG_DEBUG, "Forwarding %s from %s (%s): %s",
111                                    request_name[atoi(request)], from->name, from->hostname, request);
112                 else
113                         logger(LOG_DEBUG, "Forwarding %s from %s (%s)",
114                                    request_name[atoi(request)], from->name, from->hostname);
115         }
116
117         int len = strlen(request);
118         request[len++] = '\n';
119         broadcast_meta(from, request, len);
120 }
121
122 bool receive_request(connection_t *c, char *request) {
123         int reqno = atoi(request);
124
125         if(reqno || *request == '0') {
126                 if((reqno < 0) || (reqno >= LAST) || !request_handlers[reqno]) {
127                         ifdebug(META)
128                                 logger(LOG_DEBUG, "Unknown request from %s (%s): %s",
129                                            c->name, c->hostname, request);
130                         else
131                                 logger(LOG_ERR, "Unknown request from %s (%s)",
132                                            c->name, c->hostname);
133
134                         return false;
135                 } else {
136                         ifdebug(PROTOCOL) {
137                                 ifdebug(META)
138                                         logger(LOG_DEBUG, "Got %s from %s (%s): %s",
139                                                    request_name[reqno], c->name, c->hostname, request);
140                                 else
141                                         logger(LOG_DEBUG, "Got %s from %s (%s)",
142                                                    request_name[reqno], c->name, c->hostname);
143                         }
144                 }
145
146                 if((c->allow_request != ALL) && (c->allow_request != reqno)) {
147                         logger(LOG_ERR, "Unauthorized request from %s (%s)", c->name,
148                                    c->hostname);
149                         return false;
150                 }
151
152                 if(!request_handlers[reqno](c, request)) {
153                         /* Something went wrong. Probably scriptkiddies. Terminate. */
154
155                         logger(LOG_ERR, "Error while processing %s from %s (%s)",
156                                    request_name[reqno], c->name, c->hostname);
157                         return false;
158                 }
159         } else {
160                 logger(LOG_ERR, "Bogus data received from %s (%s)",
161                            c->name, c->hostname);
162                 return false;
163         }
164
165         return true;
166 }
167
168 static int past_request_compare(const past_request_t *a, const past_request_t *b) {
169         return strcmp(a->request, b->request);
170 }
171
172 static void free_past_request(past_request_t *r) {
173         if(r->request)
174                 free(r->request);
175
176         free(r);
177 }
178
179 static struct event past_request_event;
180
181 bool seen_request(char *request) {
182         past_request_t *new, p = {NULL};
183
184         p.request = request;
185
186         if(splay_search(past_request_tree, &p)) {
187                 ifdebug(SCARY_THINGS) logger(LOG_DEBUG, "Already seen request");
188                 return true;
189         } else {
190                 new = xmalloc(sizeof *new);
191                 new->request = xstrdup(request);
192                 new->firstseen = time(NULL);
193                 splay_insert(past_request_tree, new);
194                 event_add(&past_request_event, &(struct timeval){10, 0});
195                 return false;
196         }
197 }
198
199 static void age_past_requests(int fd, short events, void *data) {
200         splay_node_t *node, *next;
201         past_request_t *p;
202         int left = 0, deleted = 0;
203         time_t now = time(NULL);
204
205         for(node = past_request_tree->head; node; node = next) {
206                 next = node->next;
207                 p = node->data;
208
209                 if(p->firstseen + pinginterval <= now)
210                         splay_delete_node(past_request_tree, node), deleted++;
211                 else
212                         left++;
213         }
214
215         if(left || deleted)
216                 ifdebug(SCARY_THINGS) logger(LOG_DEBUG, "Aging past requests: deleted %d, left %d",
217                            deleted, left);
218
219         if(left)
220                 event_add(&past_request_event, &(struct timeval){10, 0});
221 }
222
223 void init_requests(void) {
224         past_request_tree = splay_alloc_tree((splay_compare_t) past_request_compare, (splay_action_t) free_past_request);
225
226         timeout_set(&past_request_event, age_past_requests, NULL);
227 }
228
229 void exit_requests(void) {
230         splay_delete_tree(past_request_tree);
231
232         if(timeout_initialized(&past_request_event))
233                 event_del(&past_request_event);
234 }