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