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