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