Fix all UBSAN warnings triggered by tests.
[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 = (int)a->type - (int)b->type;
190
191         if(result) {
192                 return result;
193         }
194
195         switch(a->type) {
196         case SUBNET_MAC:
197                 return subnet_compare_mac(a, b);
198
199         case SUBNET_IPV4:
200                 return subnet_compare_ipv4(a, b);
201
202         case SUBNET_IPV6:
203                 return subnet_compare_ipv6(a, b);
204
205         default:
206                 logger(DEBUG_ALWAYS, LOG_ERR, "subnet_compare() was called with unknown subnet type %d, exitting!", a->type);
207                 exit(1);
208         }
209
210         return 0;
211 }
212
213 /* Ascii representation of subnets */
214
215 bool str2net(subnet_t *subnet, const char *subnetstr) {
216         char str[1024];
217         strncpy(str, subnetstr, sizeof(str));
218         str[sizeof(str) - 1] = 0;
219         int consumed;
220
221         int weight = DEFAULT_WEIGHT;
222         char *weight_separator = strchr(str, '#');
223
224         if(weight_separator) {
225                 char *weight_str = weight_separator + 1;
226
227                 if(sscanf(weight_str, "%d%n", &weight, &consumed) < 1) {
228                         return false;
229                 }
230
231                 if(weight_str[consumed]) {
232                         return false;
233                 }
234
235                 *weight_separator = 0;
236         }
237
238         int prefixlength = -1;
239         char *prefixlength_separator = strchr(str, '/');
240
241         if(prefixlength_separator) {
242                 char *prefixlength_str = prefixlength_separator + 1;
243
244                 if(sscanf(prefixlength_str, "%d%n", &prefixlength, &consumed) < 1) {
245                         return false;
246                 }
247
248                 if(prefixlength_str[consumed]) {
249                         return false;
250                 }
251
252                 *prefixlength_separator = 0;
253
254                 if(prefixlength < 0) {
255                         return false;
256                 }
257         }
258
259         uint16_t x[8];
260
261         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]) {
262                 /*
263                    Normally we should check that each part has two digits to prevent ambiguities.
264                    However, in old tinc versions net2str() will aggressively return MAC addresses with one-digit parts,
265                    so we have to accept them otherwise we would be unable to parse ADD_SUBNET messages.
266                 */
267                 if(prefixlength >= 0) {
268                         return false;
269                 }
270
271                 subnet->type = SUBNET_MAC;
272                 subnet->weight = weight;
273
274                 for(int i = 0; i < 6; i++) {
275                         subnet->net.mac.address.x[i] = x[i];
276                 }
277
278                 return true;
279         }
280
281         if(sscanf(str, "%hu.%hu.%hu.%hu%n", &x[0], &x[1], &x[2], &x[3], &consumed) >= 4 && !str[consumed]) {
282                 if(prefixlength == -1) {
283                         prefixlength = 32;
284                 }
285
286                 if(prefixlength > 32) {
287                         return false;
288                 }
289
290                 subnet->type = SUBNET_IPV4;
291                 subnet->net.ipv4.prefixlength = prefixlength;
292                 subnet->weight = weight;
293
294                 for(int i = 0; i < 4; i++) {
295                         if(x[i] > 255) {
296                                 return false;
297                         }
298
299                         subnet->net.ipv4.address.x[i] = x[i];
300                 }
301
302                 return true;
303         }
304
305         /* IPv6 */
306
307         char *last_colon = strrchr(str, ':');
308
309         if(last_colon && sscanf(last_colon, ":%hu.%hu.%hu.%hu%n", &x[0], &x[1], &x[2], &x[3], &consumed) >= 4 && !last_colon[consumed]) {
310                 /* Dotted quad suffix notation, convert to standard IPv6 notation */
311                 for(int i = 0; i < 4; i++)
312                         if(x[i] > 255) {
313                                 return false;
314                         }
315
316                 snprintf(last_colon, sizeof(str) - (last_colon - str), ":%02x%02x:%02x%02x", x[0], x[1], x[2], x[3]);
317         }
318
319         char *double_colon = strstr(str, "::");
320
321         if(double_colon) {
322                 /* Figure out how many zero groups we need to expand */
323                 int zero_group_count = 8;
324
325                 for(const char *cur = str; *cur; cur++)
326                         if(*cur != ':') {
327                                 zero_group_count--;
328
329                                 while(cur[1] && cur[1] != ':') {
330                                         cur++;
331                                 }
332                         }
333
334                 if(zero_group_count < 1) {
335                         return false;
336                 }
337
338                 /* Split the double colon in the middle to make room for zero groups */
339                 double_colon++;
340                 memmove(double_colon + (zero_group_count * 2 - 1), double_colon, strlen(double_colon) + 1);
341
342                 /* Write zero groups in the resulting gap, overwriting the second colon */
343                 for(int i = 0; i < zero_group_count; i++) {
344                         memcpy(&double_colon[i * 2], "0:", 2);
345                 }
346
347                 /* Remove any leading or trailing colons */
348                 if(str[0] == ':') {
349                         memmove(&str[0], &str[1], strlen(&str[1]) + 1);
350                 }
351
352                 if(str[strlen(str) - 1] == ':') {
353                         str[strlen(str) - 1] = 0;
354                 }
355         }
356
357         if(sscanf(str, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx%n",
358                         &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7], &consumed) >= 8 && !str[consumed]) {
359                 if(prefixlength == -1) {
360                         prefixlength = 128;
361                 }
362
363                 if(prefixlength > 128) {
364                         return false;
365                 }
366
367                 subnet->type = SUBNET_IPV6;
368                 subnet->net.ipv6.prefixlength = prefixlength;
369                 subnet->weight = weight;
370
371                 for(int i = 0; i < 8; i++) {
372                         subnet->net.ipv6.address.x[i] = htons(x[i]);
373                 }
374
375                 return true;
376         }
377
378         return false;
379 }
380
381 bool net2str(char *netstr, int len, const subnet_t *subnet) {
382         if(!netstr || !subnet) {
383                 logger(DEBUG_ALWAYS, LOG_ERR, "net2str() was called with netstr=%p, subnet=%p!", (void *)netstr, (void *)subnet);
384                 return false;
385         }
386
387         int result;
388         int prefixlength = -1;
389
390         switch(subnet->type) {
391         case SUBNET_MAC:
392                 result = snprintf(netstr, len, "%02x:%02x:%02x:%02x:%02x:%02x",
393                                   subnet->net.mac.address.x[0],
394                                   subnet->net.mac.address.x[1],
395                                   subnet->net.mac.address.x[2],
396                                   subnet->net.mac.address.x[3],
397                                   subnet->net.mac.address.x[4],
398                                   subnet->net.mac.address.x[5]);
399                 netstr += result;
400                 len -= result;
401                 break;
402
403         case SUBNET_IPV4:
404                 result = snprintf(netstr, len, "%u.%u.%u.%u",
405                                   subnet->net.ipv4.address.x[0],
406                                   subnet->net.ipv4.address.x[1],
407                                   subnet->net.ipv4.address.x[2],
408                                   subnet->net.ipv4.address.x[3]);
409                 netstr += result;
410                 len -= result;
411                 prefixlength = subnet->net.ipv4.prefixlength;
412
413                 if(prefixlength == 32) {
414                         prefixlength = -1;
415                 }
416
417                 break;
418
419         case SUBNET_IPV6: {
420                 /* Find the longest sequence of consecutive zeroes */
421                 int max_zero_length = 0;
422                 int max_zero_length_index = 0;
423                 int current_zero_length = 0;
424                 int current_zero_length_index = 0;
425
426                 for(int i = 0; i < 8; i++) {
427                         if(subnet->net.ipv6.address.x[i] != 0) {
428                                 current_zero_length = 0;
429                         } else {
430                                 if(current_zero_length == 0) {
431                                         current_zero_length_index = i;
432                                 }
433
434                                 current_zero_length++;
435
436                                 if(current_zero_length > max_zero_length) {
437                                         max_zero_length = current_zero_length;
438                                         max_zero_length_index = current_zero_length_index;
439                                 }
440                         }
441                 }
442
443                 /* Print the address */
444                 for(int i = 0; i < 8;) {
445                         if(max_zero_length > 1 && max_zero_length_index == i) {
446                                 /* Shorten the representation as per RFC 5952 */
447                                 const char *const FORMATS[] = { "%.1s", "%.2s", "%.3s" };
448                                 const char *const *format = &FORMATS[0];
449
450                                 if(i == 0) {
451                                         format++;
452                                 }
453
454                                 if(i + max_zero_length == 8) {
455                                         format++;
456                                 }
457
458                                 result = snprintf(netstr, len, *format, ":::");
459                                 i += max_zero_length;
460                         } else {
461                                 result = snprintf(netstr, len, "%x:", ntohs(subnet->net.ipv6.address.x[i]));
462                                 i++;
463                         }
464
465                         netstr += result;
466                         len -= result;
467                 }
468
469                 /* Remove the trailing colon */
470                 netstr--;
471                 len++;
472                 *netstr = 0;
473
474                 prefixlength = subnet->net.ipv6.prefixlength;
475
476                 if(prefixlength == 128) {
477                         prefixlength = -1;
478                 }
479
480                 break;
481         }
482
483         default:
484                 logger(DEBUG_ALWAYS, LOG_ERR, "net2str() was called with unknown subnet type %d, exiting!", subnet->type);
485                 exit(1);
486         }
487
488         if(prefixlength >= 0) {
489                 result = snprintf(netstr, len, "/%d", prefixlength);
490                 netstr += result;
491                 len -= result;
492         }
493
494         if(subnet->weight != DEFAULT_WEIGHT) {
495                 snprintf(netstr, len, "#%d", subnet->weight);
496         }
497
498         return true;
499 }