044d6e729564ce5c6b7cdc962131bbfd7af4a0bd
[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         /* Check that the last colon is not further than possible in an IPv6 address */
310         if(last_colon >= str + 5 * 8) {
311                 return false;
312         }
313
314         if(last_colon && sscanf(last_colon, ":%hu.%hu.%hu.%hu%n", &x[0], &x[1], &x[2], &x[3], &consumed) >= 4 && !last_colon[consumed]) {
315                 /* Dotted quad suffix notation, convert to standard IPv6 notation */
316                 for(int i = 0; i < 4; i++)
317                         if(x[i] > 255) {
318                                 return false;
319                         }
320
321                 snprintf(last_colon, sizeof(str) - (last_colon - str), ":%02x%02x:%02x%02x", x[0], x[1], x[2], x[3]);
322         }
323
324         char *double_colon = strstr(str, "::");
325
326         if(double_colon) {
327                 /* Figure out how many zero groups we need to expand */
328                 int zero_group_count = 8;
329
330                 for(const char *cur = str; *cur; cur++)
331                         if(*cur != ':') {
332                                 zero_group_count--;
333
334                                 while(cur[1] && cur[1] != ':') {
335                                         cur++;
336                                 }
337                         }
338
339                 if(zero_group_count < 1) {
340                         return false;
341                 }
342
343                 /* Split the double colon in the middle to make room for zero groups */
344                 double_colon++;
345                 memmove(double_colon + (zero_group_count * 2 - 1), double_colon, strlen(double_colon) + 1);
346
347                 /* Write zero groups in the resulting gap, overwriting the second colon */
348                 for(int i = 0; i < zero_group_count; i++) {
349                         memcpy(&double_colon[i * 2], "0:", 2);
350                 }
351
352                 /* Remove any leading or trailing colons */
353                 if(str[0] == ':') {
354                         memmove(&str[0], &str[1], strlen(&str[1]) + 1);
355                 }
356
357                 if(str[strlen(str) - 1] == ':') {
358                         str[strlen(str) - 1] = 0;
359                 }
360         }
361
362         if(sscanf(str, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx%n",
363                         &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7], &consumed) >= 8 && !str[consumed]) {
364                 if(prefixlength == -1) {
365                         prefixlength = 128;
366                 }
367
368                 if(prefixlength > 128) {
369                         return false;
370                 }
371
372                 subnet->type = SUBNET_IPV6;
373                 subnet->net.ipv6.prefixlength = prefixlength;
374                 subnet->weight = weight;
375
376                 for(int i = 0; i < 8; i++) {
377                         subnet->net.ipv6.address.x[i] = htons(x[i]);
378                 }
379
380                 return true;
381         }
382
383         return false;
384 }
385
386 bool net2str(char *netstr, int len, const subnet_t *subnet) {
387         if(!netstr || !subnet) {
388                 logger(DEBUG_ALWAYS, LOG_ERR, "net2str() was called with netstr=%p, subnet=%p!", (void *)netstr, (void *)subnet);
389                 return false;
390         }
391
392         int result;
393         int prefixlength = -1;
394
395         switch(subnet->type) {
396         case SUBNET_MAC:
397                 result = snprintf(netstr, len, "%02x:%02x:%02x:%02x:%02x:%02x",
398                                   subnet->net.mac.address.x[0],
399                                   subnet->net.mac.address.x[1],
400                                   subnet->net.mac.address.x[2],
401                                   subnet->net.mac.address.x[3],
402                                   subnet->net.mac.address.x[4],
403                                   subnet->net.mac.address.x[5]);
404                 netstr += result;
405                 len -= result;
406                 break;
407
408         case SUBNET_IPV4:
409                 result = snprintf(netstr, len, "%u.%u.%u.%u",
410                                   subnet->net.ipv4.address.x[0],
411                                   subnet->net.ipv4.address.x[1],
412                                   subnet->net.ipv4.address.x[2],
413                                   subnet->net.ipv4.address.x[3]);
414                 netstr += result;
415                 len -= result;
416                 prefixlength = subnet->net.ipv4.prefixlength;
417
418                 if(prefixlength == 32) {
419                         prefixlength = -1;
420                 }
421
422                 break;
423
424         case SUBNET_IPV6: {
425                 /* Find the longest sequence of consecutive zeroes */
426                 int max_zero_length = 0;
427                 int max_zero_length_index = 0;
428                 int current_zero_length = 0;
429                 int current_zero_length_index = 0;
430
431                 for(int i = 0; i < 8; i++) {
432                         if(subnet->net.ipv6.address.x[i] != 0) {
433                                 current_zero_length = 0;
434                         } else {
435                                 if(current_zero_length == 0) {
436                                         current_zero_length_index = i;
437                                 }
438
439                                 current_zero_length++;
440
441                                 if(current_zero_length > max_zero_length) {
442                                         max_zero_length = current_zero_length;
443                                         max_zero_length_index = current_zero_length_index;
444                                 }
445                         }
446                 }
447
448                 /* Print the address */
449                 for(int i = 0; i < 8;) {
450                         if(max_zero_length > 1 && max_zero_length_index == i) {
451                                 /* Shorten the representation as per RFC 5952 */
452                                 const char *const FORMATS[] = { "%.1s", "%.2s", "%.3s" };
453                                 const char *const *format = &FORMATS[0];
454
455                                 if(i == 0) {
456                                         format++;
457                                 }
458
459                                 if(i + max_zero_length == 8) {
460                                         format++;
461                                 }
462
463                                 result = snprintf(netstr, len, *format, ":::");
464                                 i += max_zero_length;
465                         } else {
466                                 result = snprintf(netstr, len, "%x:", ntohs(subnet->net.ipv6.address.x[i]));
467                                 i++;
468                         }
469
470                         netstr += result;
471                         len -= result;
472                 }
473
474                 /* Remove the trailing colon */
475                 netstr--;
476                 len++;
477                 *netstr = 0;
478
479                 prefixlength = subnet->net.ipv6.prefixlength;
480
481                 if(prefixlength == 128) {
482                         prefixlength = -1;
483                 }
484
485                 break;
486         }
487
488         default:
489                 logger(DEBUG_ALWAYS, LOG_ERR, "net2str() was called with unknown subnet type %d, exiting!", subnet->type);
490                 exit(1);
491         }
492
493         if(prefixlength >= 0) {
494                 result = snprintf(netstr, len, "/%d", prefixlength);
495                 netstr += result;
496                 len -= result;
497         }
498
499         if(subnet->weight != DEFAULT_WEIGHT) {
500                 snprintf(netstr, len, "#%d", subnet->weight);
501         }
502
503         return true;
504 }