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.
27 /* Changing this default will affect ADD_SUBNET messages - beware of inconsistencies between versions */
28 static const int DEFAULT_WEIGHT = 10;
30 /* Subnet mask handling */
32 int maskcmp(const void *va, const void *vb, size_t masklen) {
34 const uint8_t *a = va;
35 const uint8_t *b = vb;
37 for(m = masklen, i = 0; m >= 8; m -= 8, i++) {
38 int result = a[i] - b[i];
46 return (a[i] & (0x100 - (1 << (8 - m)))) -
47 (b[i] & (0x100 - (1 << (8 - m))));
52 void mask(void *va, size_t masklen, size_t len) {
60 a[i++] &= (0x100 - (1 << (8 - masklen)));
68 void maskcpy(void *va, const void *vb, size_t masklen, size_t len) {
71 const uint8_t *b = vb;
73 for(m = masklen, i = 0; m >= 8; m -= 8, i++) {
78 a[i] = b[i] & (0x100 - (1 << (8 - m)));
87 bool subnetcheck(const subnet_t subnet) {
88 if(((subnet.type == SUBNET_IPV4)
89 && !maskcheck(&subnet.net.ipv4.address, subnet.net.ipv4.prefixlength, sizeof(subnet.net.ipv4.address)))
90 || ((subnet.type == SUBNET_IPV6)
91 && !maskcheck(&subnet.net.ipv6.address, subnet.net.ipv6.prefixlength, sizeof(subnet.net.ipv6.address)))) {
98 bool maskcheck(const void *va, size_t masklen, size_t len) {
100 const uint8_t *a = va;
105 if(masklen && a[i++] & (0xff >> masklen)) {
117 /* Subnet comparison */
119 static int subnet_compare_mac(const subnet_t *a, const subnet_t *b) {
122 result = memcmp(&a->net.mac.address, &b->net.mac.address, sizeof(a->net.mac.address));
128 result = a->weight - b->weight;
130 if(result || !a->owner || !b->owner) {
134 return strcmp(a->owner->name, b->owner->name);
137 static int subnet_compare_ipv4(const subnet_t *a, const subnet_t *b) {
140 result = b->net.ipv4.prefixlength - a->net.ipv4.prefixlength;
146 result = memcmp(&a->net.ipv4.address, &b->net.ipv4.address, sizeof(ipv4_t));
152 result = a->weight - b->weight;
154 if(result || !a->owner || !b->owner) {
158 return strcmp(a->owner->name, b->owner->name);
161 static int subnet_compare_ipv6(const subnet_t *a, const subnet_t *b) {
164 result = b->net.ipv6.prefixlength - a->net.ipv6.prefixlength;
170 result = memcmp(&a->net.ipv6.address, &b->net.ipv6.address, sizeof(ipv6_t));
176 result = a->weight - b->weight;
178 if(result || !a->owner || !b->owner) {
182 return strcmp(a->owner->name, b->owner->name);
185 int subnet_compare(const subnet_t *a, const subnet_t *b) {
186 int result = (int)a->type - (int)b->type;
194 return subnet_compare_mac(a, b);
197 return subnet_compare_ipv4(a, b);
200 return subnet_compare_ipv6(a, b);
203 logger(DEBUG_ALWAYS, LOG_ERR, "subnet_compare() was called with unknown subnet type %d, exitting!", a->type);
210 /* Ascii representation of subnets */
212 bool str2net(subnet_t *subnet, const char *subnetstr) {
214 strncpy(str, subnetstr, sizeof(str));
215 str[sizeof(str) - 1] = 0;
218 int weight = DEFAULT_WEIGHT;
219 char *weight_separator = strchr(str, '#');
221 if(weight_separator) {
222 char *weight_str = weight_separator + 1;
224 if(sscanf(weight_str, "%d%n", &weight, &consumed) < 1) {
228 if(weight_str[consumed]) {
232 *weight_separator = 0;
235 int prefixlength = -1;
236 char *prefixlength_separator = strchr(str, '/');
238 if(prefixlength_separator) {
239 char *prefixlength_str = prefixlength_separator + 1;
241 if(sscanf(prefixlength_str, "%d%n", &prefixlength, &consumed) < 1) {
245 if(prefixlength_str[consumed]) {
249 *prefixlength_separator = 0;
251 if(prefixlength < 0) {
258 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]) {
260 Normally we should check that each part has two digits to prevent ambiguities.
261 However, in old tinc versions net2str() will aggressively return MAC addresses with one-digit parts,
262 so we have to accept them otherwise we would be unable to parse ADD_SUBNET messages.
264 if(prefixlength >= 0) {
268 subnet->type = SUBNET_MAC;
269 subnet->weight = weight;
271 for(int i = 0; i < 6; i++) {
272 subnet->net.mac.address.x[i] = x[i];
278 if(inet_pton(AF_INET, str, &subnet->net.ipv4.address)) {
279 if(prefixlength == -1) {
283 if(prefixlength > 32) {
287 subnet->type = SUBNET_IPV4;
288 subnet->net.ipv4.prefixlength = prefixlength;
289 subnet->weight = weight;
294 if(inet_pton(AF_INET6, str, &subnet->net.ipv6.address)) {
295 if(prefixlength == -1) {
299 if(prefixlength > 128) {
303 subnet->type = SUBNET_IPV6;
304 subnet->net.ipv6.prefixlength = prefixlength;
305 subnet->weight = weight;
313 bool net2str(char *netstr, size_t len, const subnet_t *subnet) {
314 if(!netstr || !subnet) {
315 logger(DEBUG_ALWAYS, LOG_ERR, "net2str() was called with netstr=%p, subnet=%p!", (void *)netstr, (void *)subnet);
320 int prefixlength = -1;
322 switch(subnet->type) {
324 snprintf(netstr, len, "%02x:%02x:%02x:%02x:%02x:%02x",
325 subnet->net.mac.address.x[0],
326 subnet->net.mac.address.x[1],
327 subnet->net.mac.address.x[2],
328 subnet->net.mac.address.x[3],
329 subnet->net.mac.address.x[4],
330 subnet->net.mac.address.x[5]);
334 inet_ntop(AF_INET, &subnet->net.ipv4.address, netstr, len);
335 prefixlength = subnet->net.ipv4.prefixlength;
337 if(prefixlength == 32) {
344 inet_ntop(AF_INET6, &subnet->net.ipv6.address, netstr, len);
345 prefixlength = subnet->net.ipv6.prefixlength;
347 if(prefixlength == 128) {
355 logger(DEBUG_ALWAYS, LOG_ERR, "net2str() was called with unknown subnet type %d, exiting!", subnet->type);
359 size_t used = strlen(netstr);
363 if(prefixlength >= 0) {
364 result = snprintf(netstr, len, "/%d", prefixlength);
369 if(subnet->weight != DEFAULT_WEIGHT) {
370 snprintf(netstr, len, "#%d", subnet->weight);