Fix all warnings when compiling with -Wall -W -pedantic.
[tinc] / src / protocol_subnet.c
1 /*
2     protocol_subnet.c -- handle the meta-protocol, subnets
3     Copyright (C) 1999-2005 Ivo Timmermans,
4                   2000-2012 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         if(!net2str(netstr, sizeof(netstr), subnet)) {
39                 return false;
40         }
41
42         return send_request(c, "%d %x %s %s", ADD_SUBNET, rand(), subnet->owner->name, netstr);
43 }
44
45 bool add_subnet_h(connection_t *c, const char *request) {
46         char subnetstr[MAX_STRING_SIZE];
47         char name[MAX_STRING_SIZE];
48         node_t *owner;
49         subnet_t s = {0}, *new, *old;
50
51         if(sscanf(request, "%*d %*x " MAX_STRING " " MAX_STRING, name, subnetstr) != 2) {
52                 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "ADD_SUBNET", c->name,
53                        c->hostname);
54                 return false;
55         }
56
57         /* Check if owner name is valid */
58
59         if(!check_id(name)) {
60                 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "ADD_SUBNET", c->name,
61                        c->hostname, "invalid name");
62                 return false;
63         }
64
65         /* Check if subnet string is valid */
66
67         if(!str2net(&s, subnetstr)) {
68                 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "ADD_SUBNET", c->name,
69                        c->hostname, "invalid subnet string");
70                 return false;
71         }
72
73         if(seen_request(request)) {
74                 return true;
75         }
76
77         /* Check if the owner of the new subnet is in the connection list */
78
79         owner = lookup_node(name);
80
81         if(tunnelserver && owner != myself && owner != c->node) {
82                 /* in case of tunnelserver, ignore indirect subnet registrations */
83                 logger(DEBUG_PROTOCOL, LOG_WARNING, "Ignoring indirect %s from %s (%s) for %s",
84                        "ADD_SUBNET", c->name, c->hostname, subnetstr);
85                 return true;
86         }
87
88         if(!owner) {
89                 owner = new_node();
90                 owner->name = xstrdup(name);
91                 node_add(owner);
92         }
93
94         /* Check if we already know this subnet */
95
96         if(lookup_subnet(owner, &s)) {
97                 return true;
98         }
99
100         /* If we don't know this subnet, but we are the owner, retaliate with a DEL_SUBNET */
101
102         if(owner == myself) {
103                 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) for ourself",
104                        "ADD_SUBNET", c->name, c->hostname);
105                 s.owner = myself;
106                 send_del_subnet(c, &s);
107                 return true;
108         }
109
110         /* In tunnel server mode, we should already know all allowed subnets */
111
112         if(tunnelserver) {
113                 logger(DEBUG_ALWAYS, LOG_WARNING, "Ignoring unauthorized %s from %s (%s): %s",
114                        "ADD_SUBNET", c->name, c->hostname, subnetstr);
115                 return true;
116         }
117
118         /* Ignore if strictsubnets is true, but forward it to others */
119
120         if(strictsubnets) {
121                 logger(DEBUG_ALWAYS, LOG_WARNING, "Ignoring unauthorized %s from %s (%s): %s",
122                        "ADD_SUBNET", c->name, c->hostname, subnetstr);
123                 forward_request(c, request);
124                 return true;
125         }
126
127         /* If everything is correct, add the subnet to the list of the owner */
128
129         *(new = new_subnet()) = s;
130         subnet_add(owner, new);
131
132         if(owner->status.reachable) {
133                 subnet_update(owner, new, true);
134         }
135
136         /* Tell the rest */
137
138         if(!tunnelserver) {
139                 forward_request(c, request);
140         }
141
142         /* Fast handoff of roaming MAC addresses */
143
144         if(s.type == SUBNET_MAC && owner != myself && (old = lookup_subnet(myself, &s)) && old->expires) {
145                 old->expires = 1;
146         }
147
148         return true;
149 }
150
151 bool send_del_subnet(connection_t *c, const subnet_t *s) {
152         char netstr[MAXNETSTR];
153
154         if(!net2str(netstr, sizeof(netstr), s)) {
155                 return false;
156         }
157
158         return send_request(c, "%d %x %s %s", DEL_SUBNET, rand(), s->owner->name, netstr);
159 }
160
161 bool del_subnet_h(connection_t *c, const char *request) {
162         char subnetstr[MAX_STRING_SIZE];
163         char name[MAX_STRING_SIZE];
164         node_t *owner;
165         subnet_t s = {0}, *find;
166
167         if(sscanf(request, "%*d %*x " MAX_STRING " " MAX_STRING, name, subnetstr) != 2) {
168                 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "DEL_SUBNET", c->name,
169                        c->hostname);
170                 return false;
171         }
172
173         /* Check if owner name is valid */
174
175         if(!check_id(name)) {
176                 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "DEL_SUBNET", c->name,
177                        c->hostname, "invalid name");
178                 return false;
179         }
180
181         /* Check if subnet string is valid */
182
183         if(!str2net(&s, subnetstr)) {
184                 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "DEL_SUBNET", c->name,
185                        c->hostname, "invalid subnet string");
186                 return false;
187         }
188
189         if(seen_request(request)) {
190                 return true;
191         }
192
193         /* Check if the owner of the subnet being deleted is in the connection list */
194
195         owner = lookup_node(name);
196
197         if(tunnelserver && owner != myself && owner != c->node) {
198                 /* in case of tunnelserver, ignore indirect subnet deletion */
199                 logger(DEBUG_PROTOCOL, LOG_WARNING, "Ignoring indirect %s from %s (%s) for %s",
200                        "DEL_SUBNET", c->name, c->hostname, subnetstr);
201                 return true;
202         }
203
204         if(!owner) {
205                 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) for %s which is not in our node tree",
206                        "DEL_SUBNET", c->name, c->hostname, name);
207                 return true;
208         }
209
210         /* If everything is correct, delete the subnet from the list of the owner */
211
212         s.owner = owner;
213
214         find = lookup_subnet(owner, &s);
215
216         if(!find) {
217                 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) for %s which does not appear in his subnet tree",
218                        "DEL_SUBNET", c->name, c->hostname, name);
219
220                 if(strictsubnets) {
221                         forward_request(c, request);
222                 }
223
224                 return true;
225         }
226
227         /* If we are the owner of this subnet, retaliate with an ADD_SUBNET */
228
229         if(owner == myself) {
230                 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) for ourself",
231                        "DEL_SUBNET", c->name, c->hostname);
232                 send_add_subnet(c, find);
233                 return true;
234         }
235
236         if(tunnelserver) {
237                 return true;
238         }
239
240         /* Tell the rest */
241
242         if(!tunnelserver) {
243                 forward_request(c, request);
244         }
245
246         if(strictsubnets) {
247                 return true;
248         }
249
250         /* Finally, delete it. */
251
252         if(owner->status.reachable) {
253                 subnet_update(owner, find, false);
254         }
255
256         subnet_del(owner, find);
257
258         return true;
259 }