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