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