Reflow all Markdown files.
[tinc] / src / subnet_parse.c
1 /*
2     subnet_parse.c -- handle subnet parsing
3     Copyright (C) 2000-2021 Guus Sliepen <guus@tinc-vpn.org>,
4                   2000-2005 Ivo Timmermans
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License along
17     with this program; if not, write to the Free Software Foundation, Inc.,
18     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "system.h"
22
23 #include "logger.h"
24 #include "net.h"
25 #include "subnet.h"
26
27 /* Changing this default will affect ADD_SUBNET messages - beware of inconsistencies between versions */
28 static const int DEFAULT_WEIGHT = 10;
29
30 /* Subnet mask handling */
31
32 int maskcmp(const void *va, const void *vb, size_t masklen) {
33         size_t i, m;
34         const uint8_t *a = va;
35         const uint8_t *b = vb;
36
37         for(m = masklen, i = 0; m >= 8; m -= 8, i++) {
38                 int result = a[i] - b[i];
39
40                 if(result) {
41                         return result;
42                 }
43         }
44
45         if(m)
46                 return (a[i] & (0x100 - (1 << (8 - m)))) -
47                        (b[i] & (0x100 - (1 << (8 - m))));
48
49         return 0;
50 }
51
52 void mask(void *va, size_t masklen, size_t len) {
53         size_t i;
54         uint8_t *a = va;
55
56         i = masklen / 8;
57         masklen %= 8;
58
59         if(masklen) {
60                 a[i++] &= (0x100 - (1 << (8 - masklen)));
61         }
62
63         for(; i < len; i++) {
64                 a[i] = 0;
65         }
66 }
67
68 void maskcpy(void *va, const void *vb, size_t masklen, size_t len) {
69         size_t i, m;
70         uint8_t *a = va;
71         const uint8_t *b = vb;
72
73         for(m = masklen, i = 0; m >= 8; m -= 8, i++) {
74                 a[i] = b[i];
75         }
76
77         if(m) {
78                 a[i] = b[i] & (0x100 - (1 << (8 - m)));
79                 i++;
80         }
81
82         for(; i < len; i++) {
83                 a[i] = 0;
84         }
85 }
86
87 bool subnetcheck(const subnet_t subnet) {
88         if(((subnet.type == SUBNET_IPV4)
89                         && !maskcheck(&subnet.net.ipv4.address, subnet.net.ipv4.prefixlength, sizeof(subnet.net.ipv4.address)))
90                         || ((subnet.type == SUBNET_IPV6)
91                             && !maskcheck(&subnet.net.ipv6.address, subnet.net.ipv6.prefixlength, sizeof(subnet.net.ipv6.address)))) {
92                 return false;
93         }
94
95         return true;
96 }
97
98 bool maskcheck(const void *va, size_t masklen, size_t len) {
99         size_t i;
100         const uint8_t *a = va;
101
102         i = masklen / 8;
103         masklen %= 8;
104
105         if(masklen && a[i++] & (0xff >> masklen)) {
106                 return false;
107         }
108
109         for(; i < len; i++)
110                 if(a[i] != 0) {
111                         return false;
112                 }
113
114         return true;
115 }
116
117 /* Subnet comparison */
118
119 static int subnet_compare_mac(const subnet_t *a, const subnet_t *b) {
120         int result;
121
122         result = memcmp(&a->net.mac.address, &b->net.mac.address, sizeof(a->net.mac.address));
123
124         if(result) {
125                 return result;
126         }
127
128         result = a->weight - b->weight;
129
130         if(result || !a->owner || !b->owner) {
131                 return result;
132         }
133
134         return strcmp(a->owner->name, b->owner->name);
135 }
136
137 static int subnet_compare_ipv4(const subnet_t *a, const subnet_t *b) {
138         int result;
139
140         result = b->net.ipv4.prefixlength - a->net.ipv4.prefixlength;
141
142         if(result) {
143                 return result;
144         }
145
146         result = memcmp(&a->net.ipv4.address, &b->net.ipv4.address, sizeof(ipv4_t));
147
148         if(result) {
149                 return result;
150         }
151
152         result = a->weight - b->weight;
153
154         if(result || !a->owner || !b->owner) {
155                 return result;
156         }
157
158         return strcmp(a->owner->name, b->owner->name);
159 }
160
161 static int subnet_compare_ipv6(const subnet_t *a, const subnet_t *b) {
162         int result;
163
164         result = b->net.ipv6.prefixlength - a->net.ipv6.prefixlength;
165
166         if(result) {
167                 return result;
168         }
169
170         result = memcmp(&a->net.ipv6.address, &b->net.ipv6.address, sizeof(ipv6_t));
171
172         if(result) {
173                 return result;
174         }
175
176         result = a->weight - b->weight;
177
178         if(result || !a->owner || !b->owner) {
179                 return result;
180         }
181
182         return strcmp(a->owner->name, b->owner->name);
183 }
184
185 int subnet_compare(const subnet_t *a, const subnet_t *b) {
186         int result = (int)a->type - (int)b->type;
187
188         if(result) {
189                 return result;
190         }
191
192         switch(a->type) {
193         case SUBNET_MAC:
194                 return subnet_compare_mac(a, b);
195
196         case SUBNET_IPV4:
197                 return subnet_compare_ipv4(a, b);
198
199         case SUBNET_IPV6:
200                 return subnet_compare_ipv6(a, b);
201
202         default:
203                 logger(DEBUG_ALWAYS, LOG_ERR, "subnet_compare() was called with unknown subnet type %d, exitting!", a->type);
204                 exit(1);
205         }
206
207         return 0;
208 }
209
210 /* Ascii representation of subnets */
211
212 bool str2net(subnet_t *subnet, const char *subnetstr) {
213         char str[64];
214         strncpy(str, subnetstr, sizeof(str));
215         str[sizeof(str) - 1] = 0;
216         int consumed;
217
218         int weight = DEFAULT_WEIGHT;
219         char *weight_separator = strchr(str, '#');
220
221         if(weight_separator) {
222                 char *weight_str = weight_separator + 1;
223
224                 if(sscanf(weight_str, "%d%n", &weight, &consumed) < 1) {
225                         return false;
226                 }
227
228                 if(weight_str[consumed]) {
229                         return false;
230                 }
231
232                 *weight_separator = 0;
233         }
234
235         int prefixlength = -1;
236         char *prefixlength_separator = strchr(str, '/');
237
238         if(prefixlength_separator) {
239                 char *prefixlength_str = prefixlength_separator + 1;
240
241                 if(sscanf(prefixlength_str, "%d%n", &prefixlength, &consumed) < 1) {
242                         return false;
243                 }
244
245                 if(prefixlength_str[consumed]) {
246                         return false;
247                 }
248
249                 *prefixlength_separator = 0;
250
251                 if(prefixlength < 0) {
252                         return false;
253                 }
254         }
255
256         uint16_t x[8];
257
258         if(sscanf(str, "%hx:%hx:%hx:%hx:%hx:%hx%n", &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &consumed) >= 6 && !str[consumed]) {
259                 /*
260                    Normally we should check that each part has two digits to prevent ambiguities.
261                    However, in old tinc versions net2str() will aggressively return MAC addresses with one-digit parts,
262                    so we have to accept them otherwise we would be unable to parse ADD_SUBNET messages.
263                 */
264                 if(prefixlength >= 0) {
265                         return false;
266                 }
267
268                 subnet->type = SUBNET_MAC;
269                 subnet->weight = weight;
270
271                 for(int i = 0; i < 6; i++) {
272                         subnet->net.mac.address.x[i] = x[i];
273                 }
274
275                 return true;
276         }
277
278         if(inet_pton(AF_INET, str, &subnet->net.ipv4.address)) {
279                 if(prefixlength == -1) {
280                         prefixlength = 32;
281                 }
282
283                 if(prefixlength > 32) {
284                         return false;
285                 }
286
287                 subnet->type = SUBNET_IPV4;
288                 subnet->net.ipv4.prefixlength = prefixlength;
289                 subnet->weight = weight;
290
291                 return true;
292         }
293
294         if(inet_pton(AF_INET6, str, &subnet->net.ipv6.address)) {
295                 if(prefixlength == -1) {
296                         prefixlength = 128;
297                 }
298
299                 if(prefixlength > 128) {
300                         return false;
301                 }
302
303                 subnet->type = SUBNET_IPV6;
304                 subnet->net.ipv6.prefixlength = prefixlength;
305                 subnet->weight = weight;
306
307                 return true;
308         }
309
310         return false;
311 }
312
313 bool net2str(char *netstr, size_t len, const subnet_t *subnet) {
314         if(!netstr || !subnet) {
315                 logger(DEBUG_ALWAYS, LOG_ERR, "net2str() was called with netstr=%p, subnet=%p!", (void *)netstr, (void *)subnet);
316                 return false;
317         }
318
319         int result;
320         int prefixlength = -1;
321
322         switch(subnet->type) {
323         case SUBNET_MAC:
324                 snprintf(netstr, len, "%02x:%02x:%02x:%02x:%02x:%02x",
325                          subnet->net.mac.address.x[0],
326                          subnet->net.mac.address.x[1],
327                          subnet->net.mac.address.x[2],
328                          subnet->net.mac.address.x[3],
329                          subnet->net.mac.address.x[4],
330                          subnet->net.mac.address.x[5]);
331                 break;
332
333         case SUBNET_IPV4:
334                 inet_ntop(AF_INET, &subnet->net.ipv4.address, netstr, len);
335                 prefixlength = subnet->net.ipv4.prefixlength;
336
337                 if(prefixlength == 32) {
338                         prefixlength = -1;
339                 }
340
341                 break;
342
343         case SUBNET_IPV6: {
344                 inet_ntop(AF_INET6, &subnet->net.ipv6.address, netstr, len);
345                 prefixlength = subnet->net.ipv6.prefixlength;
346
347                 if(prefixlength == 128) {
348                         prefixlength = -1;
349                 }
350
351                 break;
352         }
353
354         default:
355                 logger(DEBUG_ALWAYS, LOG_ERR, "net2str() was called with unknown subnet type %d, exiting!", subnet->type);
356                 exit(1);
357         }
358
359         size_t used = strlen(netstr);
360         netstr += used;
361         len -= used;
362
363         if(prefixlength >= 0) {
364                 result = snprintf(netstr, len, "/%d", prefixlength);
365                 netstr += result;
366                 len -= result;
367         }
368
369         if(subnet->weight != DEFAULT_WEIGHT) {
370                 snprintf(netstr, len, "#%d", subnet->weight);
371         }
372
373         return true;
374 }