2 subnet_parse.c -- handle subnet parsing
3 Copyright (C) 2000-2021 Guus Sliepen <guus@tinc-vpn.org>,
4 2000-2005 Ivo Timmermans
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.
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.
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.
30 /* Changing this default will affect ADD_SUBNET messages - beware of inconsistencies between versions */
31 static const int DEFAULT_WEIGHT = 10;
33 /* Subnet mask handling */
35 int maskcmp(const void *va, const void *vb, int masklen) {
40 for(m = masklen, i = 0; m >= 8; m -= 8, i++) {
49 return (a[i] & (0x100 - (1 << (8 - m)))) -
50 (b[i] & (0x100 - (1 << (8 - m))));
55 void mask(void *va, int masklen, int len) {
63 a[i++] &= (0x100 - (1 << (8 - masklen)));
71 void maskcpy(void *va, const void *vb, int masklen, int len) {
76 for(m = masklen, i = 0; m >= 8; m -= 8, i++) {
81 a[i] = b[i] & (0x100 - (1 << (8 - m)));
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)))) {
101 bool maskcheck(const void *va, int masklen, int len) {
108 if(masklen && a[i++] & (0xff >> masklen)) {
120 /* Subnet comparison */
122 static int subnet_compare_mac(const subnet_t *a, const subnet_t *b) {
125 result = memcmp(&a->net.mac.address, &b->net.mac.address, sizeof(a->net.mac.address));
131 result = a->weight - b->weight;
133 if(result || !a->owner || !b->owner) {
137 return strcmp(a->owner->name, b->owner->name);
140 static int subnet_compare_ipv4(const subnet_t *a, const subnet_t *b) {
143 result = b->net.ipv4.prefixlength - a->net.ipv4.prefixlength;
149 result = memcmp(&a->net.ipv4.address, &b->net.ipv4.address, sizeof(ipv4_t));
155 result = a->weight - b->weight;
157 if(result || !a->owner || !b->owner) {
161 return strcmp(a->owner->name, b->owner->name);
164 static int subnet_compare_ipv6(const subnet_t *a, const subnet_t *b) {
167 result = b->net.ipv6.prefixlength - a->net.ipv6.prefixlength;
173 result = memcmp(&a->net.ipv6.address, &b->net.ipv6.address, sizeof(ipv6_t));
179 result = a->weight - b->weight;
181 if(result || !a->owner || !b->owner) {
185 return strcmp(a->owner->name, b->owner->name);
188 int subnet_compare(const subnet_t *a, const subnet_t *b) {
189 int result = (int)a->type - (int)b->type;
197 return subnet_compare_mac(a, b);
200 return subnet_compare_ipv4(a, b);
203 return subnet_compare_ipv6(a, b);
206 logger(DEBUG_ALWAYS, LOG_ERR, "subnet_compare() was called with unknown subnet type %d, exitting!", a->type);
213 /* Ascii representation of subnets */
215 bool str2net(subnet_t *subnet, const char *subnetstr) {
217 strncpy(str, subnetstr, sizeof(str));
218 str[sizeof(str) - 1] = 0;
221 int weight = DEFAULT_WEIGHT;
222 char *weight_separator = strchr(str, '#');
224 if(weight_separator) {
225 char *weight_str = weight_separator + 1;
227 if(sscanf(weight_str, "%d%n", &weight, &consumed) < 1) {
231 if(weight_str[consumed]) {
235 *weight_separator = 0;
238 int prefixlength = -1;
239 char *prefixlength_separator = strchr(str, '/');
241 if(prefixlength_separator) {
242 char *prefixlength_str = prefixlength_separator + 1;
244 if(sscanf(prefixlength_str, "%d%n", &prefixlength, &consumed) < 1) {
248 if(prefixlength_str[consumed]) {
252 *prefixlength_separator = 0;
254 if(prefixlength < 0) {
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]) {
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.
267 if(prefixlength >= 0) {
271 subnet->type = SUBNET_MAC;
272 subnet->weight = weight;
274 for(int i = 0; i < 6; i++) {
275 subnet->net.mac.address.x[i] = x[i];
281 if(inet_pton(AF_INET, str, &subnet->net.ipv4.address)) {
282 if(prefixlength == -1) {
286 if(prefixlength > 32) {
290 subnet->type = SUBNET_IPV4;
291 subnet->net.ipv4.prefixlength = prefixlength;
292 subnet->weight = weight;
297 if(inet_pton(AF_INET6, str, &subnet->net.ipv6.address)) {
298 if(prefixlength == -1) {
302 if(prefixlength > 128) {
306 subnet->type = SUBNET_IPV6;
307 subnet->net.ipv6.prefixlength = prefixlength;
308 subnet->weight = weight;
316 bool net2str(char *netstr, int len, const subnet_t *subnet) {
317 if(!netstr || !subnet) {
318 logger(DEBUG_ALWAYS, LOG_ERR, "net2str() was called with netstr=%p, subnet=%p!", (void *)netstr, (void *)subnet);
323 int prefixlength = -1;
325 switch(subnet->type) {
327 snprintf(netstr, len, "%02x:%02x:%02x:%02x:%02x:%02x",
328 subnet->net.mac.address.x[0],
329 subnet->net.mac.address.x[1],
330 subnet->net.mac.address.x[2],
331 subnet->net.mac.address.x[3],
332 subnet->net.mac.address.x[4],
333 subnet->net.mac.address.x[5]);
337 inet_ntop(AF_INET, &subnet->net.ipv4.address, netstr, len);
338 prefixlength = subnet->net.ipv4.prefixlength;
340 if(prefixlength == 32) {
347 inet_ntop(AF_INET6, &subnet->net.ipv6.address, netstr, len);
348 prefixlength = subnet->net.ipv6.prefixlength;
350 if(prefixlength == 128) {
358 logger(DEBUG_ALWAYS, LOG_ERR, "net2str() was called with unknown subnet type %d, exiting!", subnet->type);
362 size_t used = strlen(netstr);
366 if(prefixlength >= 0) {
367 result = snprintf(netstr, len, "/%d", prefixlength);
372 if(subnet->weight != DEFAULT_WEIGHT) {
373 snprintf(netstr, len, "#%d", subnet->weight);