eef7e37430f9d8d5e8f2c8c10f688d19ceedd737
[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                   2009      Michael Tokarev <mjt@tls.msk.ru>
6
7     This program is free software; you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation; either version 2 of the License, or
10     (at your option) any later version.
11
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16
17     You should have received a copy of the GNU General Public License
18     along with this program; if not, write to the Free Software
19     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21     $Id$
22 */
23
24 #include "system.h"
25
26 #include "conf.h"
27 #include "connection.h"
28 #include "logger.h"
29 #include "net.h"
30 #include "netutl.h"
31 #include "node.h"
32 #include "protocol.h"
33 #include "subnet.h"
34 #include "utils.h"
35 #include "xalloc.h"
36
37 bool send_add_subnet(connection_t *c, const subnet_t *subnet)
38 {
39         char netstr[MAXNETSTR];
40
41         cp();
42
43         if(!net2str(netstr, sizeof netstr, subnet))
44                 return false;
45
46         return send_request(c, "%d %x %s %s", ADD_SUBNET, rand(), subnet->owner->name, netstr);
47 }
48
49 bool add_subnet_h(connection_t *c)
50 {
51         char subnetstr[MAX_STRING_SIZE];
52         char name[MAX_STRING_SIZE];
53         node_t *owner;
54         subnet_t s = {0}, *new;
55
56         cp();
57
58         if(sscanf(c->buffer, "%*d %*x " MAX_STRING " " MAX_STRING, name, subnetstr) != 2) {
59                 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "ADD_SUBNET", c->name,
60                            c->hostname);
61                 return false;
62         }
63
64         /* Check if owner name is valid */
65
66         if(!check_id(name)) {
67                 logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_SUBNET", c->name,
68                            c->hostname, _("invalid name"));
69                 return false;
70         }
71
72         /* Check if subnet string is valid */
73
74         if(!str2net(&s, subnetstr)) {
75                 logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_SUBNET", c->name,
76                            c->hostname, _("invalid subnet string"));
77                 return false;
78         }
79
80         if(seen_request(c->buffer))
81                 return true;
82
83         /* Check if the owner of the new subnet is in the connection list */
84
85         owner = lookup_node(name);
86
87         if(tunnelserver && owner != myself && owner != c->node) {
88                 /* in case of tunnelserver, ignore indirect subnet registrations */
89                 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Ignoring indirect %s from %s (%s) for %s"),
90                                    "ADD_SUBNET", c->name, c->hostname, subnetstr);
91                 return true;
92         }
93
94         if(!owner) {
95                 owner = new_node();
96                 owner->name = xstrdup(name);
97                 node_add(owner);
98         }
99
100         /* Check if we already know this subnet */
101
102         if(lookup_subnet(owner, &s))
103                 return true;
104
105         /* If we don't know this subnet, but we are the owner, retaliate with a DEL_SUBNET */
106
107         if(owner == myself) {
108                 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for ourself"),
109                                    "ADD_SUBNET", c->name, c->hostname);
110                 s.owner = myself;
111                 send_del_subnet(c, &s);
112                 return true;
113         }
114
115         /* In tunnel server mode, check if the subnet matches one in the config file of this node */
116
117         if(tunnelserver) {
118                 config_t *cfg;
119                 subnet_t *allowed;
120
121                 for(cfg = lookup_config(c->config_tree, "Subnet"); cfg; cfg = lookup_config_next(c->config_tree, cfg)) {
122                         if(!get_config_subnet(cfg, &allowed))
123                                 return false;
124
125                         if(!subnet_compare(&s, allowed))
126                                 break;
127
128                         free_subnet(allowed);
129                 }
130
131                 if(!cfg) {
132                         logger(LOG_WARNING, _("Unauthorized %s from %s (%s) for %s"),
133                                 "ADD_SUBNET", c->name, c->hostname, subnetstr);
134                         return false;
135                 }
136
137                 free_subnet(allowed);
138         }
139
140         /* If everything is correct, add the subnet to the list of the owner */
141
142         *(new = new_subnet()) = s;
143         subnet_add(owner, new);
144
145         if(owner->status.reachable)
146                 subnet_update(owner, new, true);
147
148         /* Tell the rest */
149
150         if(!tunnelserver)
151                 forward_request(c);
152
153         return true;
154 }
155
156 bool send_del_subnet(connection_t *c, const subnet_t *s)
157 {
158         char netstr[MAXNETSTR];
159
160         cp();
161
162         if(!net2str(netstr, sizeof netstr, s))
163                 return false;
164
165         return send_request(c, "%d %x %s %s", DEL_SUBNET, rand(), s->owner->name, netstr);
166 }
167
168 bool del_subnet_h(connection_t *c)
169 {
170         char subnetstr[MAX_STRING_SIZE];
171         char name[MAX_STRING_SIZE];
172         node_t *owner;
173         subnet_t s = {0}, *find;
174
175         cp();
176
177         if(sscanf(c->buffer, "%*d %*x " MAX_STRING " " MAX_STRING, name, subnetstr) != 2) {
178                 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "DEL_SUBNET", c->name,
179                            c->hostname);
180                 return false;
181         }
182
183         /* Check if owner name is valid */
184
185         if(!check_id(name)) {
186                 logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_SUBNET", c->name,
187                            c->hostname, _("invalid name"));
188                 return false;
189         }
190
191         /* Check if subnet string is valid */
192
193         if(!str2net(&s, subnetstr)) {
194                 logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_SUBNET", c->name,
195                            c->hostname, _("invalid subnet string"));
196                 return false;
197         }
198
199         if(seen_request(c->buffer))
200                 return true;
201
202         /* Check if the owner of the subnet being deleted is in the connection list */
203
204         owner = lookup_node(name);
205
206         if(tunnelserver && owner != myself && owner != c->node) {
207                 /* in case of tunnelserver, ignore indirect subnet deletion */
208                 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Ignoring indirect %s from %s (%s) for %s"),
209                                   "DEL_SUBNET", c->name, c->hostname, subnetstr);
210                 return true;
211         }
212
213         if(!owner) {
214                 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for %s which is not in our node tree"),
215                                    "DEL_SUBNET", c->name, c->hostname, name);
216                 return true;
217         }
218
219         /* If everything is correct, delete the subnet from the list of the owner */
220
221         s.owner = owner;
222
223         find = lookup_subnet(owner, &s);
224
225         if(!find) {
226                 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for %s which does not appear in his subnet tree"),
227                                    "DEL_SUBNET", c->name, c->hostname, name);
228                 return true;
229         }
230
231         /* If we are the owner of this subnet, retaliate with an ADD_SUBNET */
232
233         if(owner == myself) {
234                 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for ourself"),
235                                    "DEL_SUBNET", c->name, c->hostname);
236                 send_add_subnet(c, find);
237                 return true;
238         }
239
240         /* Tell the rest */
241
242         if(!tunnelserver)
243                 forward_request(c);
244
245         /* Finally, delete it. */
246
247         if(owner->status.reachable)
248                 subnet_update(owner, find, false);
249
250         subnet_del(owner, find);
251
252         return true;
253 }