Add the Forwarding option.
[oweals/tinc.git] / 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         if(!net2str(netstr, sizeof netstr, subnet))
39                 return false;
40
41         return send_request(c, "%d %x %s %s", ADD_SUBNET, rand(), subnet->owner->name, netstr);
42 }
43
44 bool add_subnet_h(connection_t *c) {
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(c->buffer, "%*d %*x " MAX_STRING " " MAX_STRING, name, subnetstr) != 2) {
51                 logger(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(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(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(c->buffer))
73                 return true;
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                 ifdebug(PROTOCOL) logger(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         /* If we don't know this subnet, but we are the owner, retaliate with a DEL_SUBNET */
98
99         if(owner == myself) {
100                 ifdebug(PROTOCOL) logger(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(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                 forward_request(c);
119                 return true;
120         }
121
122         /* If everything is correct, add the subnet to the list of the owner */
123
124         *(new = new_subnet()) = s;
125         subnet_add(owner, new);
126
127         if(owner->status.reachable)
128                 subnet_update(owner, new, true);
129
130         /* Tell the rest */
131
132         forward_request(c);
133
134         /* Fast handoff of roaming MAC addresses */
135
136         if(s.type == SUBNET_MAC && owner != myself && (old = lookup_subnet(myself, &s)) && old->expires)
137                 old->expires = now;
138
139         return true;
140 }
141
142 bool send_del_subnet(connection_t *c, const subnet_t *s) {
143         char netstr[MAXNETSTR];
144
145         if(!net2str(netstr, sizeof netstr, s))
146                 return false;
147
148         return send_request(c, "%d %x %s %s", DEL_SUBNET, rand(), s->owner->name, netstr);
149 }
150
151 bool del_subnet_h(connection_t *c) {
152         char subnetstr[MAX_STRING_SIZE];
153         char name[MAX_STRING_SIZE];
154         node_t *owner;
155         subnet_t s = {0}, *find;
156
157         if(sscanf(c->buffer, "%*d %*x " MAX_STRING " " MAX_STRING, name, subnetstr) != 2) {
158                 logger(LOG_ERR, "Got bad %s from %s (%s)", "DEL_SUBNET", c->name,
159                            c->hostname);
160                 return false;
161         }
162
163         /* Check if owner name is valid */
164
165         if(!check_id(name)) {
166                 logger(LOG_ERR, "Got bad %s from %s (%s): %s", "DEL_SUBNET", c->name,
167                            c->hostname, "invalid name");
168                 return false;
169         }
170
171         /* Check if subnet string is valid */
172
173         if(!str2net(&s, subnetstr)) {
174                 logger(LOG_ERR, "Got bad %s from %s (%s): %s", "DEL_SUBNET", c->name,
175                            c->hostname, "invalid subnet string");
176                 return false;
177         }
178
179         if(seen_request(c->buffer))
180                 return true;
181
182         /* Check if the owner of the subnet being deleted is in the connection list */
183
184         owner = lookup_node(name);
185
186         if(tunnelserver && owner != myself && owner != c->node) {
187                 /* in case of tunnelserver, ignore indirect subnet deletion */
188                 ifdebug(PROTOCOL) logger(LOG_WARNING, "Ignoring indirect %s from %s (%s) for %s",
189                                   "DEL_SUBNET", c->name, c->hostname, subnetstr);
190                 return true;
191         }
192
193         if(!owner) {
194                 ifdebug(PROTOCOL) logger(LOG_WARNING, "Got %s from %s (%s) for %s which is not in our node tree",
195                                    "DEL_SUBNET", c->name, c->hostname, name);
196                 return true;
197         }
198
199         /* If everything is correct, delete the subnet from the list of the owner */
200
201         s.owner = owner;
202
203         find = lookup_subnet(owner, &s);
204
205         if(!find) {
206                 ifdebug(PROTOCOL) logger(LOG_WARNING, "Got %s from %s (%s) for %s which does not appear in his subnet tree",
207                                    "DEL_SUBNET", c->name, c->hostname, name);
208                 if(strictsubnets)
209                         forward_request(c);
210                 return true;
211         }
212
213         /* If we are the owner of this subnet, retaliate with an ADD_SUBNET */
214
215         if(owner == myself) {
216                 ifdebug(PROTOCOL) logger(LOG_WARNING, "Got %s from %s (%s) for ourself",
217                                    "DEL_SUBNET", c->name, c->hostname);
218                 send_add_subnet(c, find);
219                 return true;
220         }
221
222         if(tunnelserver)
223                 return true;
224
225         /* Tell the rest */
226
227         forward_request(c);
228         if(strictsubnets)
229                 return true;
230
231         /* Finally, delete it. */
232
233         if(owner->status.reachable)
234                 subnet_update(owner, find, false);
235
236         subnet_del(owner, find);
237
238         return true;
239 }