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