32b7e0a09a4de64c1af90dbcc713144fbed75578
[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 "netutl.h"
26 #include "subnet.h"
27 #include "utils.h"
28 #include "xalloc.h"
29
30 /* Changing this default will affect ADD_SUBNET messages - beware of inconsistencies between versions */
31 static const int DEFAULT_WEIGHT = 10;
32
33 /* Subnet mask handling */
34
35 int maskcmp(const void *va, const void *vb, int masklen) {
36         int i, m, result;
37         const char *a = va;
38         const char *b = vb;
39
40         for(m = masklen, i = 0; m >= 8; m -= 8, i++) {
41                 result = a[i] - b[i];
42
43                 if(result) {
44                         return result;
45                 }
46         }
47
48         if(m)
49                 return (a[i] & (0x100 - (1 << (8 - m)))) -
50                        (b[i] & (0x100 - (1 << (8 - m))));
51
52         return 0;
53 }
54
55 void mask(void *va, int masklen, int len) {
56         int i;
57         char *a = va;
58
59         i = masklen / 8;
60         masklen %= 8;
61
62         if(masklen) {
63                 a[i++] &= (0x100 - (1 << (8 - masklen)));
64         }
65
66         for(; i < len; i++) {
67                 a[i] = 0;
68         }
69 }
70
71 void maskcpy(void *va, const void *vb, int masklen, int len) {
72         int i, m;
73         char *a = va;
74         const char *b = vb;
75
76         for(m = masklen, i = 0; m >= 8; m -= 8, i++) {
77                 a[i] = b[i];
78         }
79
80         if(m) {
81                 a[i] = b[i] & (0x100 - (1 << (8 - m)));
82                 i++;
83         }
84
85         for(; i < len; i++) {
86                 a[i] = 0;
87         }
88 }
89
90 bool subnetcheck(const subnet_t subnet) {
91         if(((subnet.type == SUBNET_IPV4)
92                         && !maskcheck(&subnet.net.ipv4.address, subnet.net.ipv4.prefixlength, sizeof(subnet.net.ipv4.address)))
93                         || ((subnet.type == SUBNET_IPV6)
94                             && !maskcheck(&subnet.net.ipv6.address, subnet.net.ipv6.prefixlength, sizeof(subnet.net.ipv6.address)))) {
95                 return false;
96         }
97
98         return true;
99 }
100
101 bool maskcheck(const void *va, int masklen, int len) {
102         int i;
103         const char *a = va;
104
105         i = masklen / 8;
106         masklen %= 8;
107
108         if(masklen && a[i++] & (0xff >> masklen)) {
109                 return false;
110         }
111
112         for(; i < len; i++)
113                 if(a[i] != 0) {
114                         return false;
115                 }
116
117         return true;
118 }
119
120 /* Subnet comparison */
121
122 static int subnet_compare_mac(const subnet_t *a, const subnet_t *b) {
123         int result;
124
125         result = memcmp(&a->net.mac.address, &b->net.mac.address, sizeof(a->net.mac.address));
126
127         if(result) {
128                 return result;
129         }
130
131         result = a->weight - b->weight;
132
133         if(result || !a->owner || !b->owner) {
134                 return result;
135         }
136
137         return strcmp(a->owner->name, b->owner->name);
138 }
139
140 static int subnet_compare_ipv4(const subnet_t *a, const subnet_t *b) {
141         int result;
142
143         result = b->net.ipv4.prefixlength - a->net.ipv4.prefixlength;
144
145         if(result) {
146                 return result;
147         }
148
149         result = memcmp(&a->net.ipv4.address, &b->net.ipv4.address, sizeof(ipv4_t));
150
151         if(result) {
152                 return result;
153         }
154
155         result = a->weight - b->weight;
156
157         if(result || !a->owner || !b->owner) {
158                 return result;
159         }
160
161         return strcmp(a->owner->name, b->owner->name);
162 }
163
164 static int subnet_compare_ipv6(const subnet_t *a, const subnet_t *b) {
165         int result;
166
167         result = b->net.ipv6.prefixlength - a->net.ipv6.prefixlength;
168
169         if(result) {
170                 return result;
171         }
172
173         result = memcmp(&a->net.ipv6.address, &b->net.ipv6.address, sizeof(ipv6_t));
174
175         if(result) {
176                 return result;
177         }
178
179         result = a->weight - b->weight;
180
181         if(result || !a->owner || !b->owner) {
182                 return result;
183         }
184
185         return strcmp(a->owner->name, b->owner->name);
186 }
187
188 int subnet_compare(const subnet_t *a, const subnet_t *b) {
189         int result;
190
191         result = a->type - b->type;
192
193         if(result) {
194                 return result;
195         }
196
197         switch(a->type) {
198         case SUBNET_MAC:
199                 return subnet_compare_mac(a, b);
200
201         case SUBNET_IPV4:
202                 return subnet_compare_ipv4(a, b);
203
204         case SUBNET_IPV6:
205                 return subnet_compare_ipv6(a, b);
206
207         default:
208                 logger(DEBUG_ALWAYS, LOG_ERR, "subnet_compare() was called with unknown subnet type %d, exitting!", a->type);
209                 exit(1);
210         }
211
212         return 0;
213 }
214
215 /* Ascii representation of subnets */
216
217 bool str2net(subnet_t *subnet, const char *subnetstr) {
218         char str[1024];
219         strncpy(str, subnetstr, sizeof(str));
220         str[sizeof(str) - 1] = 0;
221         int consumed;
222
223         int weight = DEFAULT_WEIGHT;
224         char *weight_separator = strchr(str, '#');
225
226         if(weight_separator) {
227                 char *weight_str = weight_separator + 1;
228
229                 if(sscanf(weight_str, "%d%n", &weight, &consumed) < 1) {
230                         return false;
231                 }
232
233                 if(weight_str[consumed]) {
234                         return false;
235                 }
236
237                 *weight_separator = 0;
238         }
239
240         int prefixlength = -1;
241         char *prefixlength_separator = strchr(str, '/');
242
243         if(prefixlength_separator) {
244                 char *prefixlength_str = prefixlength_separator + 1;
245
246                 if(sscanf(prefixlength_str, "%d%n", &prefixlength, &consumed) < 1) {
247                         return false;
248                 }
249
250                 if(prefixlength_str[consumed]) {
251                         return false;
252                 }
253
254                 *prefixlength_separator = 0;
255
256                 if(prefixlength < 0) {
257                         return false;
258                 }
259         }
260
261         uint16_t x[8];
262
263         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]) {
264                 /*
265                    Normally we should check that each part has two digits to prevent ambiguities.
266                    However, in old tinc versions net2str() will aggressively return MAC addresses with one-digit parts,
267                    so we have to accept them otherwise we would be unable to parse ADD_SUBNET messages.
268                 */
269                 if(prefixlength >= 0) {
270                         return false;
271                 }
272
273                 subnet->type = SUBNET_MAC;
274                 subnet->weight = weight;
275
276                 for(int i = 0; i < 6; i++) {
277                         subnet->net.mac.address.x[i] = x[i];
278                 }
279
280                 return true;
281         }
282
283         if(sscanf(str, "%hu.%hu.%hu.%hu%n", &x[0], &x[1], &x[2], &x[3], &consumed) >= 4 && !str[consumed]) {
284                 if(prefixlength == -1) {
285                         prefixlength = 32;
286                 }
287
288                 if(prefixlength > 32) {
289                         return false;
290                 }
291
292                 subnet->type = SUBNET_IPV4;
293                 subnet->net.ipv4.prefixlength = prefixlength;
294                 subnet->weight = weight;
295
296                 for(int i = 0; i < 4; i++) {
297                         if(x[i] > 255) {
298                                 return false;
299                         }
300
301                         subnet->net.ipv4.address.x[i] = x[i];
302                 }
303
304                 return true;
305         }
306
307         /* IPv6 */
308
309         char *last_colon = strrchr(str, ':');
310
311         if(last_colon && sscanf(last_colon, ":%hu.%hu.%hu.%hu%n", &x[0], &x[1], &x[2], &x[3], &consumed) >= 4 && !last_colon[consumed]) {
312                 /* Dotted quad suffix notation, convert to standard IPv6 notation */
313                 for(int i = 0; i < 4; i++)
314                         if(x[i] > 255) {
315                                 return false;
316                         }
317
318                 snprintf(last_colon, sizeof(str) - (last_colon - str), ":%02x%02x:%02x%02x", x[0], x[1], x[2], x[3]);
319         }
320
321         char *double_colon = strstr(str, "::");
322
323         if(double_colon) {
324                 /* Figure out how many zero groups we need to expand */
325                 int zero_group_count = 8;
326
327                 for(const char *cur = str; *cur; cur++)
328                         if(*cur != ':') {
329                                 zero_group_count--;
330
331                                 while(cur[1] && cur[1] != ':') {
332                                         cur++;
333                                 }
334                         }
335
336                 if(zero_group_count < 1) {
337                         return false;
338                 }
339
340                 /* Split the double colon in the middle to make room for zero groups */
341                 double_colon++;
342                 memmove(double_colon + (zero_group_count * 2 - 1), double_colon, strlen(double_colon) + 1);
343
344                 /* Write zero groups in the resulting gap, overwriting the second colon */
345                 for(int i = 0; i < zero_group_count; i++) {
346                         memcpy(&double_colon[i * 2], "0:", 2);
347                 }
348
349                 /* Remove any leading or trailing colons */
350                 if(str[0] == ':') {
351                         memmove(&str[0], &str[1], strlen(&str[1]) + 1);
352                 }
353
354                 if(str[strlen(str) - 1] == ':') {
355                         str[strlen(str) - 1] = 0;
356                 }
357         }
358
359         if(sscanf(str, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx%n",
360                         &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7], &consumed) >= 8 && !str[consumed]) {
361                 if(prefixlength == -1) {
362                         prefixlength = 128;
363                 }
364
365                 if(prefixlength > 128) {
366                         return false;
367                 }
368
369                 subnet->type = SUBNET_IPV6;
370                 subnet->net.ipv6.prefixlength = prefixlength;
371                 subnet->weight = weight;
372
373                 for(int i = 0; i < 8; i++) {
374                         subnet->net.ipv6.address.x[i] = htons(x[i]);
375                 }
376
377                 return true;
378         }
379
380         return false;
381 }
382
383 bool net2str(char *netstr, int len, const subnet_t *subnet) {
384         if(!netstr || !subnet) {
385                 logger(DEBUG_ALWAYS, LOG_ERR, "net2str() was called with netstr=%p, subnet=%p!", (void *)netstr, (void *)subnet);
386                 return false;
387         }
388
389         int result;
390         int prefixlength = -1;
391
392         switch(subnet->type) {
393         case SUBNET_MAC:
394                 result = snprintf(netstr, len, "%02x:%02x:%02x:%02x:%02x:%02x",
395                                   subnet->net.mac.address.x[0],
396                                   subnet->net.mac.address.x[1],
397                                   subnet->net.mac.address.x[2],
398                                   subnet->net.mac.address.x[3],
399                                   subnet->net.mac.address.x[4],
400                                   subnet->net.mac.address.x[5]);
401                 netstr += result;
402                 len -= result;
403                 break;
404
405         case SUBNET_IPV4:
406                 result = snprintf(netstr, len, "%u.%u.%u.%u",
407                                   subnet->net.ipv4.address.x[0],
408                                   subnet->net.ipv4.address.x[1],
409                                   subnet->net.ipv4.address.x[2],
410                                   subnet->net.ipv4.address.x[3]);
411                 netstr += result;
412                 len -= result;
413                 prefixlength = subnet->net.ipv4.prefixlength;
414
415                 if(prefixlength == 32) {
416                         prefixlength = -1;
417                 }
418
419                 break;
420
421         case SUBNET_IPV6: {
422                 /* Find the longest sequence of consecutive zeroes */
423                 int max_zero_length = 0;
424                 int max_zero_length_index = 0;
425                 int current_zero_length = 0;
426                 int current_zero_length_index = 0;
427
428                 for(int i = 0; i < 8; i++) {
429                         if(subnet->net.ipv6.address.x[i] != 0) {
430                                 current_zero_length = 0;
431                         } else {
432                                 if(current_zero_length == 0) {
433                                         current_zero_length_index = i;
434                                 }
435
436                                 current_zero_length++;
437
438                                 if(current_zero_length > max_zero_length) {
439                                         max_zero_length = current_zero_length;
440                                         max_zero_length_index = current_zero_length_index;
441                                 }
442                         }
443                 }
444
445                 /* Print the address */
446                 for(int i = 0; i < 8;) {
447                         if(max_zero_length > 1 && max_zero_length_index == i) {
448                                 /* Shorten the representation as per RFC 5952 */
449                                 const char *const FORMATS[] = { "%.1s", "%.2s", "%.3s" };
450                                 const char *const *format = &FORMATS[0];
451
452                                 if(i == 0) {
453                                         format++;
454                                 }
455
456                                 if(i + max_zero_length == 8) {
457                                         format++;
458                                 }
459
460                                 result = snprintf(netstr, len, *format, ":::");
461                                 i += max_zero_length;
462                         } else {
463                                 result = snprintf(netstr, len, "%x:", ntohs(subnet->net.ipv6.address.x[i]));
464                                 i++;
465                         }
466
467                         netstr += result;
468                         len -= result;
469                 }
470
471                 /* Remove the trailing colon */
472                 netstr--;
473                 len++;
474                 *netstr = 0;
475
476                 prefixlength = subnet->net.ipv6.prefixlength;
477
478                 if(prefixlength == 128) {
479                         prefixlength = -1;
480                 }
481
482                 break;
483         }
484
485         default:
486                 logger(DEBUG_ALWAYS, LOG_ERR, "net2str() was called with unknown subnet type %d, exiting!", subnet->type);
487                 exit(1);
488         }
489
490         if(prefixlength >= 0) {
491                 result = snprintf(netstr, len, "/%d", prefixlength);
492                 netstr += result;
493                 len -= result;
494         }
495
496         if(subnet->weight != DEFAULT_WEIGHT) {
497                 snprintf(netstr, len, "#%d", subnet->weight);
498         }
499
500         return true;
501 }