util.c (15136B)
1 /* 2 * 3 * Wireless daemon for Linux 4 * 5 * Copyright (C) 2014-2019 Intel Corporation. All rights reserved. 6 * 7 * This library is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU Lesser General Public 9 * License as published by the Free Software Foundation; either 10 * version 2.1 of the License, or (at your option) any later version. 11 * 12 * This library is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * Lesser General Public License for more details. 16 * 17 * You should have received a copy of the GNU Lesser General Public 18 * License along with this library; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 20 * 21 */ 22 23 #ifdef HAVE_CONFIG_H 24 #include <config.h> 25 #endif 26 27 #include <string.h> 28 #include <stdio.h> 29 #include <sys/uio.h> 30 #include <sys/time.h> 31 #include <netinet/in.h> 32 #include <arpa/inet.h> 33 34 #include <ell/ell.h> 35 36 #include "ell/useful.h" 37 #include "src/defs.h" 38 #include "src/util.h" 39 #include "src/band.h" 40 41 const char *util_ssid_to_utf8(size_t len, const uint8_t *ssid) 42 { 43 static char buf[3 * 32 + 1]; 44 size_t i = 0, pos = 0; 45 const uint8_t *start = ssid, *end; 46 47 memset(buf, 0, sizeof(buf)); 48 49 if (len > SSID_MAX_SIZE) 50 goto no_ssid; 51 52 while (i < len && !ssid[i]) 53 i++; 54 55 if (i == len) 56 goto no_ssid; 57 58 i = len; 59 60 while (i && (!l_utf8_validate((const char *)start, i, 61 (const char **)&end))) { 62 const char replacement[] = { 0xEF, 0xBF, 0xBD }; 63 int bytes = end - start; 64 65 memcpy(&buf[pos], start, bytes); 66 pos += bytes; 67 68 memcpy(&buf[pos], replacement, sizeof(replacement)); 69 pos += sizeof(replacement); 70 71 start = end + 1; 72 i -= (bytes + 1); 73 } 74 75 if (i) { 76 memcpy(&buf[pos], start, i); 77 pos += i; 78 } 79 80 no_ssid: 81 buf[pos] = '\0'; 82 83 return buf; 84 } 85 86 bool util_ssid_is_utf8(size_t len, const uint8_t *ssid) 87 { 88 if (len > SSID_MAX_SIZE) 89 return false; 90 91 return l_utf8_validate((const char *)ssid, len, NULL); 92 } 93 94 /* 95 * Checks whether this is a hidden SSID. Two conditions are checked: 96 * 1. If the SSID is length 0 97 * 2. If the SSID length > 0 and all bytes are 0 98 * 99 * The length is not sanitized so the caller must have sanitized the arguments 100 * beforehand. 101 */ 102 bool util_ssid_is_hidden(size_t len, const uint8_t *ssid) 103 { 104 if (!len) 105 return true; 106 107 return l_memeqzero(ssid, len); 108 } 109 110 const char *util_address_to_string(const uint8_t *addr) 111 { 112 static char str[18]; 113 114 sprintf(str, "%02x:%02x:%02x:%02x:%02x:%02x", 115 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]); 116 117 return str; 118 } 119 120 bool util_string_to_address(const char *str, uint8_t *out_addr) 121 { 122 unsigned int i; 123 uint8_t addr[6]; 124 125 if (!str) 126 return false; 127 128 if (strlen(str) != 17) 129 return false; 130 131 for (i = 0; i < 15; i += 3) { 132 if (!l_ascii_isxdigit(str[i])) 133 return false; 134 135 if (!l_ascii_isxdigit(str[i + 1])) 136 return false; 137 138 if (str[i + 2] != ':') 139 return false; 140 } 141 142 if (!l_ascii_isxdigit(str[i])) 143 return false; 144 145 if (!l_ascii_isxdigit(str[i + 1])) 146 return false; 147 148 if (sscanf(str, "%2hhx:%2hhx:%2hhx:%2hhx:%2hhx:%2hhx", 149 &addr[0], &addr[1], &addr[2], 150 &addr[3], &addr[4], &addr[5]) != 6) 151 return false; 152 153 memcpy(out_addr, addr, sizeof(addr)); 154 155 return true; 156 } 157 158 bool util_is_group_address(const uint8_t *addr) 159 { 160 /* 802.11-2016 section 9.2.2 */ 161 return test_bit(addr, 0); 162 } 163 164 bool util_is_broadcast_address(const uint8_t *addr) 165 { 166 /* 802.11-2016 section 9.2.4.3 */ 167 static const uint8_t bcast_addr[6] = { 168 0xff, 0xff, 0xff, 0xff, 0xff, 0xff 169 }; 170 171 return !memcmp(addr, bcast_addr, 6); 172 } 173 174 bool util_is_valid_sta_address(const uint8_t *addr) 175 { 176 return !util_is_broadcast_address(addr) && !util_is_group_address(addr); 177 } 178 179 /* This function assumes that identity is not bigger than 253 bytes */ 180 const char *util_get_domain(const char *identity) 181 { 182 static char domain[256]; 183 const char *c; 184 185 memset(domain, 0, sizeof(domain)); 186 187 for (c = identity; *c; c++) { 188 switch (*c) { 189 case '\\': 190 memcpy(domain, identity, c - identity); 191 return domain; 192 case '@': 193 l_strlcpy(domain, c + 1, sizeof(domain)); 194 return domain; 195 default: 196 continue; 197 } 198 } 199 200 return identity; 201 } 202 203 /* This function assumes that identity is not bigger than 253 bytes */ 204 const char *util_get_username(const char *identity) 205 { 206 static char username[256]; 207 const char *c; 208 209 memset(username, 0, sizeof(username)); 210 211 for (c = identity; *c; c++) { 212 switch (*c) { 213 case '\\': 214 l_strlcpy(username, c + 1, sizeof(username)); 215 return username; 216 case '@': 217 memcpy(username, identity, c - identity); 218 return username; 219 default: 220 continue; 221 } 222 } 223 224 return identity; 225 } 226 227 static bool is_prefix_valid(uint32_t ip, unsigned int prefix) 228 { 229 int i; 230 231 for (i = 31 - prefix; i >= 0; i--) { 232 if (ip & (1 << i)) 233 return false; 234 } 235 236 return true; 237 } 238 239 /* 240 * Parse a prefix notation IP string (e.g. A.B.C.D/E) into an IP range and 241 * netmask. All returned IP addresses/mask will be in host order. The start/end 242 * IP will only include the usable IP range where the last octet is not zero or 243 * 255. 244 */ 245 bool util_ip_prefix_tohl(const char *ip, uint8_t *prefix_out, 246 uint32_t *start_out, uint32_t *end_out, 247 uint32_t *mask_out) 248 { 249 struct in_addr ia; 250 int i; 251 unsigned int prefix = 0; 252 char no_prefix[INET_ADDRSTRLEN]; 253 char *endp; 254 uint32_t start_ip; 255 uint32_t end_ip; 256 uint32_t netmask = 0xffffffff; 257 258 /* 259 * Only iterate over the max length of an IP in case of invalid long 260 * inputs. 261 */ 262 for (i = 0; i < INET_ADDRSTRLEN && ip[i] != '\0'; i++) { 263 /* Found '/', check the next byte exists and parse prefix */ 264 if (ip[i] == '/' && ip[i + 1] != '\0') { 265 prefix = strtoul(ip + i + 1, &endp, 10); 266 if (*endp != '\0') 267 return false; 268 269 break; 270 } 271 } 272 273 if (prefix < 1 || prefix > 31) 274 return false; 275 276 /* 'i' will be at most INET_ADDRSTRLEN - 1 */ 277 l_strlcpy(no_prefix, ip, i + 1); 278 279 /* Check if IP preceding prefix is valid */ 280 if (inet_pton(AF_INET, no_prefix, &ia) != 1 || ia.s_addr == 0) 281 return false; 282 283 start_ip = ntohl(ia.s_addr); 284 285 if (!is_prefix_valid(start_ip, prefix)) 286 return false; 287 288 /* Usable range is start + 1 .. end - 1 */ 289 start_ip += 1; 290 291 /* Calculate end IP and netmask */ 292 end_ip = start_ip; 293 for (i = 31 - prefix; i >= 0; i--) { 294 end_ip |= (1 << i); 295 netmask &= ~(1 << i); 296 } 297 298 end_ip -= 1; 299 300 if (prefix_out) 301 *prefix_out = prefix; 302 303 if (start_out) 304 *start_out = start_ip; 305 306 if (end_out) 307 *end_out = end_ip; 308 309 if (mask_out) 310 *mask_out = netmask; 311 312 return true; 313 } 314 315 /* 316 * Linearly maps @value (expected to be within range @a_start and @a_end) to 317 * a new value between @b_start and @b_end. 318 * 319 * Returns: false if 320 * @value is not between @a_start and @a_end 321 * @a_start/@a_end or @b_start/@b_end are equal. 322 */ 323 bool util_linear_map(double value, double a_start, double a_end, 324 double b_start, double b_end, double *mapped_value) 325 { 326 /* Check value is within a's range */ 327 if (a_start < a_end) { 328 if (value < a_start || value > a_end) 329 return false; 330 } else if (a_start > a_end) { 331 if (value > a_start || value < a_end) 332 return false; 333 } else 334 return false; 335 336 if (b_start == b_end) 337 return false; 338 339 *mapped_value = b_start + (((b_end - b_start) / (a_end - a_start)) * 340 (value - a_start)); 341 342 return true; 343 } 344 345 struct scan_freq_set { 346 uint16_t channels_2ghz; 347 struct l_uintset *channels_5ghz; 348 struct l_uintset *channels_6ghz; 349 }; 350 351 struct scan_freq_set *scan_freq_set_new(void) 352 { 353 struct scan_freq_set *ret = l_new(struct scan_freq_set, 1); 354 355 /* 802.11-2012, 8.4.2.10 hints that 200 is the largest channel number */ 356 ret->channels_5ghz = l_uintset_new_from_range(1, 200); 357 ret->channels_6ghz = l_uintset_new_from_range(1, 233); 358 359 return ret; 360 } 361 362 void scan_freq_set_free(struct scan_freq_set *freqs) 363 { 364 if (!freqs) 365 return; 366 367 l_uintset_free(freqs->channels_5ghz); 368 l_uintset_free(freqs->channels_6ghz); 369 l_free(freqs); 370 } 371 372 bool scan_freq_set_add(struct scan_freq_set *freqs, uint32_t freq) 373 { 374 enum band_freq band; 375 uint8_t channel; 376 377 channel = band_freq_to_channel(freq, &band); 378 if (!channel) 379 return false; 380 381 switch (band) { 382 case BAND_FREQ_2_4_GHZ: 383 freqs->channels_2ghz |= 1 << (channel - 1); 384 return true; 385 case BAND_FREQ_5_GHZ: 386 return l_uintset_put(freqs->channels_5ghz, channel); 387 case BAND_FREQ_6_GHZ: 388 return l_uintset_put(freqs->channels_6ghz, channel); 389 } 390 391 return false; 392 } 393 394 bool scan_freq_set_remove(struct scan_freq_set *freqs, uint32_t freq) 395 { 396 enum band_freq band; 397 uint8_t channel; 398 399 channel = band_freq_to_channel(freq, &band); 400 if (!channel) 401 return false; 402 403 switch (band) { 404 case BAND_FREQ_2_4_GHZ: 405 freqs->channels_2ghz &= ~(1 << (channel - 1)); 406 return true; 407 case BAND_FREQ_5_GHZ: 408 return l_uintset_take(freqs->channels_5ghz, channel); 409 case BAND_FREQ_6_GHZ: 410 return l_uintset_take(freqs->channels_6ghz, channel); 411 } 412 413 return false; 414 } 415 416 bool scan_freq_set_contains(const struct scan_freq_set *freqs, uint32_t freq) 417 { 418 enum band_freq band; 419 uint8_t channel; 420 421 channel = band_freq_to_channel(freq, &band); 422 if (!channel) 423 return false; 424 425 switch (band) { 426 case BAND_FREQ_2_4_GHZ: 427 return freqs->channels_2ghz & (1 << (channel - 1)); 428 case BAND_FREQ_5_GHZ: 429 return l_uintset_contains(freqs->channels_5ghz, channel); 430 case BAND_FREQ_6_GHZ: 431 return l_uintset_contains(freqs->channels_6ghz, channel); 432 } 433 434 return false; 435 } 436 437 uint32_t scan_freq_set_get_bands(const struct scan_freq_set *freqs) 438 { 439 uint32_t bands = 0; 440 uint32_t max; 441 442 if (freqs->channels_2ghz) 443 bands |= BAND_FREQ_2_4_GHZ; 444 445 max = l_uintset_get_max(freqs->channels_5ghz); 446 447 if (l_uintset_find_min(freqs->channels_5ghz) <= max) 448 bands |= BAND_FREQ_5_GHZ; 449 450 max = l_uintset_get_max(freqs->channels_6ghz); 451 452 if (l_uintset_find_min(freqs->channels_6ghz) <= max) 453 bands |= BAND_FREQ_6_GHZ; 454 455 return bands; 456 } 457 458 static void scan_channels_add(uint32_t channel, void *user_data) 459 { 460 struct l_uintset *to = user_data; 461 462 l_uintset_put(to, channel); 463 } 464 465 void scan_freq_set_merge(struct scan_freq_set *to, 466 const struct scan_freq_set *from) 467 { 468 to->channels_2ghz |= from->channels_2ghz; 469 470 l_uintset_foreach(from->channels_5ghz, scan_channels_add, 471 to->channels_5ghz); 472 l_uintset_foreach(from->channels_6ghz, scan_channels_add, 473 to->channels_6ghz); 474 } 475 476 bool scan_freq_set_isempty(const struct scan_freq_set *set) 477 { 478 if (set->channels_2ghz == 0 && l_uintset_isempty(set->channels_5ghz) && 479 l_uintset_isempty(set->channels_6ghz)) 480 return true; 481 482 return false; 483 } 484 485 struct channels_foreach_data { 486 scan_freq_set_func_t func; 487 enum band_freq band; 488 void *user_data; 489 }; 490 491 static void scan_channels_foreach(uint32_t channel, void *user_data) 492 { 493 const struct channels_foreach_data *channels_data = user_data; 494 uint32_t freq; 495 496 freq = band_channel_to_freq(channel, channels_data->band); 497 if (!freq) { 498 l_warn("invalid channel %u for band %u", channel, 499 channels_data->band); 500 return; 501 } 502 503 channels_data->func(freq, channels_data->user_data); 504 } 505 506 void scan_freq_set_foreach(const struct scan_freq_set *freqs, 507 scan_freq_set_func_t func, void *user_data) 508 { 509 struct channels_foreach_data data = { }; 510 uint8_t channel; 511 uint32_t freq; 512 513 if (unlikely(!freqs || !func)) 514 return; 515 516 data.func = func; 517 data.band = BAND_FREQ_5_GHZ; 518 data.user_data = user_data; 519 520 l_uintset_foreach(freqs->channels_5ghz, scan_channels_foreach, &data); 521 522 data.band = BAND_FREQ_6_GHZ; 523 524 l_uintset_foreach(freqs->channels_6ghz, scan_channels_foreach, &data); 525 526 if (!freqs->channels_2ghz) 527 return; 528 529 for (channel = 1; channel <= 14; channel++) { 530 if (freqs->channels_2ghz & (1 << (channel - 1))) { 531 freq = band_channel_to_freq(channel, BAND_FREQ_2_4_GHZ); 532 533 func(freq, user_data); 534 } 535 } 536 } 537 538 void scan_freq_set_constrain(struct scan_freq_set *set, 539 const struct scan_freq_set *constraint) 540 { 541 struct l_uintset *intersection; 542 543 intersection = l_uintset_intersect(constraint->channels_5ghz, 544 set->channels_5ghz); 545 if (!intersection) 546 /* This shouldn't ever be the case. */ 547 return; 548 549 l_uintset_free(set->channels_5ghz); 550 set->channels_5ghz = intersection; 551 552 intersection = l_uintset_intersect(constraint->channels_6ghz, 553 set->channels_6ghz); 554 if (!intersection) 555 return; 556 557 l_uintset_free(set->channels_6ghz); 558 set->channels_6ghz = intersection; 559 560 set->channels_2ghz &= constraint->channels_2ghz; 561 } 562 563 void scan_freq_set_subtract(struct scan_freq_set *set, 564 const struct scan_freq_set *subtract) 565 { 566 struct l_uintset *sub; 567 568 sub = l_uintset_subtract(set->channels_6ghz, subtract->channels_6ghz); 569 if (L_WARN_ON(!sub)) 570 return; 571 572 l_uintset_free(set->channels_6ghz); 573 set->channels_6ghz = sub; 574 575 sub = l_uintset_subtract(set->channels_5ghz, subtract->channels_5ghz); 576 if (L_WARN_ON(!sub)) 577 return; 578 579 l_uintset_free(set->channels_5ghz); 580 set->channels_5ghz = sub; 581 582 set->channels_2ghz &= ~subtract->channels_2ghz; 583 } 584 585 static void add_foreach(uint32_t freq, void *user_data) 586 { 587 uint32_t **list = user_data; 588 589 **list = freq; 590 591 *list = *list + 1; 592 } 593 594 uint32_t *scan_freq_set_to_fixed_array(const struct scan_freq_set *set, 595 size_t *len_out) 596 { 597 uint8_t count = 0; 598 uint32_t *freqs; 599 600 count = __builtin_popcount(set->channels_2ghz) + 601 l_uintset_size(set->channels_5ghz) + 602 l_uintset_size(set->channels_6ghz); 603 604 if (!count) 605 return NULL; 606 607 freqs = l_new(uint32_t, count); 608 609 scan_freq_set_foreach(set, add_foreach, &freqs); 610 611 /* Move pointer back to start of list */ 612 freqs -= count; 613 614 *len_out = count; 615 616 return freqs; 617 } 618 619 struct scan_freq_set *scan_freq_set_clone(const struct scan_freq_set *set, 620 uint32_t band_mask) 621 { 622 struct scan_freq_set *new = l_new(struct scan_freq_set, 1); 623 624 if (band_mask & BAND_FREQ_2_4_GHZ) 625 new->channels_2ghz = set->channels_2ghz; 626 627 if (band_mask & BAND_FREQ_5_GHZ) 628 new->channels_5ghz = l_uintset_clone(set->channels_5ghz); 629 else 630 new->channels_5ghz = l_uintset_new_from_range(1, 200); 631 632 if (band_mask & BAND_FREQ_6_GHZ) 633 new->channels_6ghz = l_uintset_clone(set->channels_6ghz); 634 else 635 new->channels_6ghz = l_uintset_new_from_range(1, 233); 636 637 return new; 638 } 639 640 /* First 64 entries calculated by 1 / pow(n, 0.3) for n >= 1 */ 641 static const double rankmod_table[] = { 642 1.0000000000, 0.8122523964, 0.7192230933, 0.6597539554, 643 0.6170338627, 0.5841906811, 0.5577898253, 0.5358867313, 644 0.5172818580, 0.5011872336, 0.4870596972, 0.4745102806, 645 0.4632516708, 0.4530661223, 0.4437850034, 0.4352752816, 646 0.4274303178, 0.4201634287, 0.4134032816, 0.4070905315, 647 0.4011753236, 0.3956154062, 0.3903746872, 0.3854221125, 648 0.3807307877, 0.3762772797, 0.3720410580, 0.3680040435, 649 0.3641502401, 0.3604654325, 0.3569369365, 0.3535533906, 650 0.3503045821, 0.3471812999, 0.3441752105, 0.3412787518, 651 0.3384850430, 0.3357878061, 0.3331812996, 0.3306602598, 652 0.3282198502, 0.3258556179, 0.3235634544, 0.3213395618, 653 0.3191804229, 0.3170827751, 0.3150435863, 0.3130600345, 654 0.3111294892, 0.3092494947, 0.3074177553, 0.3056321221, 655 0.3038905808, 0.3021912409, 0.3005323264, 0.2989121662, 656 0.2973291870, 0.2957819051, 0.2942689208, 0.2927889114, 657 0.2913406263, 0.2899228820, 0.2885345572, 0.2871745887, 658 }; 659 660 double util_exponential_decay(unsigned int n) 661 { 662 if (n >= L_ARRAY_SIZE(rankmod_table)) 663 return rankmod_table[L_ARRAY_SIZE(rankmod_table) - 1]; 664 665 return rankmod_table[n]; 666 }