Improvements for PMTU discovery and IPv4 packet fragmentation.
[tinc] / src / protocol.c
1 /*
2     protocol.c -- handle the meta-protocol, basic functions
3     Copyright (C) 1999-2001 Ivo Timmermans <ivo@o2w.nl>,
4                   2000,2001 Guus Sliepen <guus@sliepen.eu.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
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20     $Id: protocol.c,v 1.28.4.148 2003/11/17 15:30:17 guus Exp $
21 */
22
23 #include "system.h"
24
25 #include "conf.h"
26 #include "connection.h"
27 #include "logger.h"
28 #include "meta.h"
29 #include "protocol.h"
30 #include "utils.h"
31 #include "xalloc.h"
32
33 bool tunnelserver = false;
34
35 /* Jumptable for the request handlers */
36
37 static bool (*request_handlers[])(connection_t *) = {
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,
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",
54 };
55
56 static avl_tree_t *past_request_tree;
57
58 bool check_id(const char *id)
59 {
60         for(; *id; id++)
61                 if(!isalnum(*id) && *id != '_')
62                         return false;
63
64         return true;
65 }
66
67 /* Generic request routines - takes care of logging and error
68    detection as well */
69
70 bool send_request(connection_t *c, const char *format, ...)
71 {
72         va_list args;
73         char buffer[MAXBUFSIZE];
74         int len, request;
75
76         cp();
77
78         /* Use vsnprintf instead of vasprintf: faster, no memory
79            fragmentation, cleanup is automatic, and there is a limit on the
80            input buffer anyway */
81
82         va_start(args, format);
83         len = vsnprintf(buffer, MAXBUFSIZE, format, args);
84         va_end(args);
85
86         if(len < 0 || len > MAXBUFSIZE - 1) {
87                 logger(LOG_ERR, _("Output buffer overflow while sending request to %s (%s)"),
88                            c->name, c->hostname);
89                 return false;
90         }
91
92         ifdebug(PROTOCOL) {
93                 sscanf(buffer, "%d", &request);
94                 ifdebug(META)
95                         logger(LOG_DEBUG, _("Sending %s to %s (%s): %s"),
96                                    request_name[request], c->name, c->hostname, buffer);
97                 else
98                         logger(LOG_DEBUG, _("Sending %s to %s (%s)"), request_name[request],
99                                    c->name, c->hostname);
100         }
101
102         buffer[len++] = '\n';
103
104         if(c == broadcast) {
105                 broadcast_meta(NULL, buffer, len);
106                 return true;
107         } else
108                 return send_meta(c, buffer, len);
109 }
110
111 void forward_request(connection_t *from)
112 {
113         int request;
114
115         cp();
116
117         ifdebug(PROTOCOL) {
118                 sscanf(from->buffer, "%d", &request);
119                 ifdebug(META)
120                         logger(LOG_DEBUG, _("Forwarding %s from %s (%s): %s"),
121                                    request_name[request], from->name, from->hostname,
122                                    from->buffer);
123                 else
124                         logger(LOG_DEBUG, _("Forwarding %s from %s (%s)"),
125                                    request_name[request], from->name, from->hostname);
126         }
127
128         from->buffer[from->reqlen - 1] = '\n';
129
130         broadcast_meta(from, from->buffer, from->reqlen);
131 }
132
133 bool receive_request(connection_t *c)
134 {
135         int request;
136
137         cp();
138
139         if(sscanf(c->buffer, "%d", &request) == 1) {
140                 if((request < 0) || (request >= LAST) || !request_handlers[request]) {
141                         ifdebug(META)
142                                 logger(LOG_DEBUG, _("Unknown request from %s (%s): %s"),
143                                            c->name, c->hostname, c->buffer);
144                         else
145                                 logger(LOG_ERR, _("Unknown request from %s (%s)"),
146                                            c->name, c->hostname);
147
148                         return false;
149                 } else {
150                         ifdebug(PROTOCOL) {
151                                 ifdebug(META)
152                                         logger(LOG_DEBUG, _("Got %s from %s (%s): %s"),
153                                                    request_name[request], c->name, c->hostname,
154                                                    c->buffer);
155                                 else
156                                         logger(LOG_DEBUG, _("Got %s from %s (%s)"),
157                                                    request_name[request], c->name, c->hostname);
158                         }
159                 }
160
161                 if((c->allow_request != ALL) && (c->allow_request != request)) {
162                         logger(LOG_ERR, _("Unauthorized request from %s (%s)"), c->name,
163                                    c->hostname);
164                         return false;
165                 }
166
167                 if(!request_handlers[request](c)) {
168                         /* Something went wrong. Probably scriptkiddies. Terminate. */
169
170                         logger(LOG_ERR, _("Error while processing %s from %s (%s)"),
171                                    request_name[request], c->name, c->hostname);
172                         return false;
173                 }
174         } else {
175                 logger(LOG_ERR, _("Bogus data received from %s (%s)"),
176                            c->name, c->hostname);
177                 return false;
178         }
179
180         return true;
181 }
182
183 static int past_request_compare(const past_request_t *a, const past_request_t *b)
184 {
185         return strcmp(a->request, b->request);
186 }
187
188 static void free_past_request(past_request_t *r)
189 {
190         cp();
191
192         if(r->request)
193                 free(r->request);
194
195         free(r);
196 }
197
198 void init_requests(void)
199 {
200         cp();
201
202         past_request_tree = avl_alloc_tree((avl_compare_t) past_request_compare, (avl_action_t) free_past_request);
203 }
204
205 void exit_requests(void)
206 {
207         cp();
208
209         avl_delete_tree(past_request_tree);
210 }
211
212 bool seen_request(char *request)
213 {
214         past_request_t *new, p = {0};
215
216         cp();
217
218         p.request = request;
219
220         if(avl_search(past_request_tree, &p)) {
221                 ifdebug(SCARY_THINGS) logger(LOG_DEBUG, _("Already seen request"));
222                 return true;
223         } else {
224                 new = xmalloc(sizeof(*new));
225                 new->request = xstrdup(request);
226                 new->firstseen = now;
227                 avl_insert(past_request_tree, new);
228                 return false;
229         }
230 }
231
232 void age_past_requests(void)
233 {
234         avl_node_t *node, *next;
235         past_request_t *p;
236         int left = 0, deleted = 0;
237
238         cp();
239
240         for(node = past_request_tree->head; node; node = next) {
241                 next = node->next;
242                 p = node->data;
243
244                 if(p->firstseen + pingtimeout < now)
245                         avl_delete_node(past_request_tree, node), deleted++;
246                 else
247                         left++;
248         }
249
250         if(left || deleted)
251                 ifdebug(SCARY_THINGS) logger(LOG_DEBUG, _("Aging past requests: deleted %d, left %d"),
252                            deleted, left);
253 }