Properly set HMAC length for incoming packets.
[oweals/tinc.git] / src / protocol_subnet.c
1 /*
2     protocol_subnet.c -- handle the meta-protocol, subnets
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
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$
21 */
22
23 #include "system.h"
24
25 #include "conf.h"
26 #include "connection.h"
27 #include "logger.h"
28 #include "net.h"
29 #include "netutl.h"
30 #include "node.h"
31 #include "protocol.h"
32 #include "subnet.h"
33 #include "utils.h"
34 #include "xalloc.h"
35
36 bool send_add_subnet(connection_t *c, const subnet_t *subnet)
37 {
38         char netstr[MAXNETSTR];
39
40         cp();
41
42         if(!net2str(netstr, sizeof netstr, subnet))
43                 return false;
44
45         return send_request(c, "%d %lx %s %s", ADD_SUBNET, random(), subnet->owner->name, netstr);
46 }
47
48 bool add_subnet_h(connection_t *c)
49 {
50         char subnetstr[MAX_STRING_SIZE];
51         char name[MAX_STRING_SIZE];
52         node_t *owner;
53         subnet_t s = {0}, *new;
54
55         cp();
56
57         if(sscanf(c->buffer, "%*d %*x " MAX_STRING " " MAX_STRING, name, subnetstr) != 2) {
58                 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "ADD_SUBNET", c->name,
59                            c->hostname);
60                 return false;
61         }
62
63         /* Check if owner name is valid */
64
65         if(!check_id(name)) {
66                 logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_SUBNET", c->name,
67                            c->hostname, _("invalid name"));
68                 return false;
69         }
70
71         /* Check if subnet string is valid */
72
73         if(!str2net(&s, subnetstr)) {
74                 logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_SUBNET", c->name,
75                            c->hostname, _("invalid subnet string"));
76                 return false;
77         }
78
79         if(seen_request(c->buffer))
80                 return true;
81
82         /* Check if the owner of the new subnet is in the connection list */
83
84         owner = lookup_node(name);
85
86         if(tunnelserver && owner != myself && owner != c->node) {
87                 /* in case of tunnelserver, ignore indirect subnet registrations */
88                 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Ignoring indirect %s from %s (%s) for %s"),
89                                    "ADD_SUBNET", c->name, c->hostname, subnetstr);
90                 return true;
91         }
92
93         if(!owner) {
94                 owner = new_node();
95                 owner->name = xstrdup(name);
96                 node_add(owner);
97         }
98
99         /* Check if we already know this subnet */
100
101         if(lookup_subnet(owner, &s))
102                 return true;
103
104         /* If we don't know this subnet, but we are the owner, retaliate with a DEL_SUBNET */
105
106         if(owner == myself) {
107                 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for ourself"),
108                                    "ADD_SUBNET", c->name, c->hostname);
109                 s.owner = myself;
110                 send_del_subnet(c, &s);
111                 return true;
112         }
113
114         /* In tunnel server mode, check if the subnet matches one in the config file of this node */
115
116         if(tunnelserver) {
117                 config_t *cfg;
118                 subnet_t *allowed;
119
120                 for(cfg = lookup_config(c->config_tree, "Subnet"); cfg; cfg = lookup_config_next(c->config_tree, cfg)) {
121                         if(!get_config_subnet(cfg, &allowed))
122                                 return false;
123
124                         if(!subnet_compare(&s, allowed))
125                                 break;
126
127                         free_subnet(allowed);
128                 }
129
130                 if(!cfg) {
131                         logger(LOG_WARNING, _("Unauthorized %s from %s (%s) for %s"),
132                                 "ADD_SUBNET", c->name, c->hostname, subnetstr);
133                         return false;
134                 }
135
136                 free_subnet(allowed);
137         }
138
139         /* If everything is correct, add the subnet to the list of the owner */
140
141         *(new = new_subnet()) = s;
142         subnet_add(owner, new);
143
144         if(owner->status.reachable)
145                 subnet_update(owner, new, true);
146
147         /* Tell the rest */
148
149         if(!tunnelserver)
150                 forward_request(c);
151
152         return true;
153 }
154
155 bool send_del_subnet(connection_t *c, const subnet_t *s)
156 {
157         char netstr[MAXNETSTR];
158
159         cp();
160
161         if(!net2str(netstr, sizeof netstr, s))
162                 return false;
163
164         return send_request(c, "%d %lx %s %s", DEL_SUBNET, random(), s->owner->name, netstr);
165 }
166
167 bool del_subnet_h(connection_t *c)
168 {
169         char subnetstr[MAX_STRING_SIZE];
170         char name[MAX_STRING_SIZE];
171         node_t *owner;
172         subnet_t s = {0}, *find;
173
174         cp();
175
176         if(sscanf(c->buffer, "%*d %*x " MAX_STRING " " MAX_STRING, name, subnetstr) != 2) {
177                 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "DEL_SUBNET", c->name,
178                            c->hostname);
179                 return false;
180         }
181
182         /* Check if owner name is valid */
183
184         if(!check_id(name)) {
185                 logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_SUBNET", c->name,
186                            c->hostname, _("invalid name"));
187                 return false;
188         }
189
190         /* Check if subnet string is valid */
191
192         if(!str2net(&s, subnetstr)) {
193                 logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_SUBNET", c->name,
194                            c->hostname, _("invalid subnet string"));
195                 return false;
196         }
197
198         if(seen_request(c->buffer))
199                 return true;
200
201         /* Check if the owner of the subnet being deleted is in the connection list */
202
203         owner = lookup_node(name);
204
205         if(tunnelserver && owner != myself && owner != c->node) {
206                 /* in case of tunnelserver, ignore indirect subnet deletion */
207                 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Ignoring indirect %s from %s (%s) for %s"),
208                                   "DEL_SUBNET", c->name, c->hostname, subnetstr);
209                 return true;
210         }
211
212         if(!owner) {
213                 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for %s which is not in our node tree"),
214                                    "DEL_SUBNET", c->name, c->hostname, name);
215                 return true;
216         }
217
218         /* If everything is correct, delete the subnet from the list of the owner */
219
220         s.owner = owner;
221
222         find = lookup_subnet(owner, &s);
223
224         if(!find) {
225                 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for %s which does not appear in his subnet tree"),
226                                    "DEL_SUBNET", c->name, c->hostname, name);
227                 return true;
228         }
229
230         /* If we are the owner of this subnet, retaliate with an ADD_SUBNET */
231
232         if(owner == myself) {
233                 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for ourself"),
234                                    "DEL_SUBNET", c->name, c->hostname);
235                 send_add_subnet(c, find);
236                 return true;
237         }
238
239         /* Tell the rest */
240
241         if(!tunnelserver)
242                 forward_request(c);
243
244         /* Finally, delete it. */
245
246         if(owner->status.reachable)
247                 subnet_update(owner, find, false);
248
249         subnet_del(owner, find);
250
251         return true;
252 }