hotspot.c (12825B)
1 /* 2 * 3 * Wireless daemon for Linux 4 * 5 * Copyright (C) 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 #define _GNU_SOURCE 28 #include <dirent.h> 29 #include <errno.h> 30 #include <stdio.h> 31 32 #include <ell/ell.h> 33 34 #include "src/iwd.h" 35 #include "src/module.h" 36 #include "src/common.h" 37 #include "src/network.h" 38 #include "src/util.h" 39 #include "src/ie.h" 40 #include "src/knownnetworks.h" 41 #include "src/storage.h" 42 #include "src/scan.h" 43 44 static struct l_dir_watch *hs20_dir_watch; 45 static struct l_queue *hs20_settings; 46 47 struct hs20_config { 48 struct network_info super; 49 char *filename; 50 uint8_t hessid[6]; 51 char **nai_realms; 52 uint8_t *rc; /* roaming consortium */ 53 size_t rc_len; 54 char *object_path; 55 char *name; 56 }; 57 58 static bool match_filename(const void *a, const void *b) 59 { 60 const struct hs20_config *config = a; 61 const char *filename = b; 62 63 if (!strcmp(config->filename, filename)) 64 return true; 65 66 return false; 67 } 68 69 static void hs20_config_free(void *user_data) 70 { 71 struct hs20_config *config = user_data; 72 73 l_queue_remove(hs20_settings, config); 74 75 l_strv_free(config->nai_realms); 76 l_free(config->rc); 77 l_free(config->object_path); 78 l_free(config->filename); 79 l_free(config->name); 80 l_free(config); 81 } 82 83 static int hotspot_network_touch(struct network_info *info) 84 { 85 struct hs20_config *config = l_container_of(info, struct hs20_config, 86 super); 87 88 return l_path_touch(config->filename); 89 } 90 91 static struct l_settings *hotspot_network_open(struct network_info *info) 92 { 93 struct l_settings *settings; 94 struct hs20_config *config = l_container_of(info, struct hs20_config, 95 super); 96 97 settings = l_settings_new(); 98 99 if (!l_settings_load_from_file(settings, config->filename)) 100 goto error; 101 102 if (!storage_decrypt(settings, config->filename, config->name)) 103 goto error; 104 105 return settings; 106 107 error: 108 l_settings_free(settings); 109 return NULL; 110 } 111 112 static void hotspot_network_sync(struct network_info *info, 113 struct l_settings *settings) 114 { 115 char *data; 116 size_t length = 0; 117 struct hs20_config *config = l_container_of(info, struct hs20_config, 118 super); 119 120 data = __storage_encrypt(settings, config->name, &length); 121 if (!data) { 122 l_error("Unable to sync profile %s", config->filename); 123 return; 124 } 125 126 write_file(data, length, true, "%s", config->filename); 127 l_free(data); 128 } 129 130 static void hotspot_network_remove(struct network_info *info) 131 { 132 struct hs20_config *config = l_container_of(info, struct hs20_config, 133 super); 134 135 unlink(config->filename); 136 } 137 138 static void hotspot_network_free(struct network_info *info) 139 { 140 struct hs20_config *config = l_container_of(info, struct hs20_config, 141 super); 142 143 hs20_config_free(config); 144 } 145 146 static const char *hotspot_network_get_path(const struct network_info *info) 147 { 148 char *digest; 149 struct l_checksum *sha; 150 char **realms; 151 struct hs20_config *config = l_container_of(info, struct hs20_config, 152 super); 153 154 if (config->object_path) 155 return config->object_path; 156 157 sha = l_checksum_new(L_CHECKSUM_SHA256); 158 159 if (config->nai_realms) { 160 realms = config->nai_realms; 161 162 while (*realms) { 163 l_checksum_update(sha, *realms, strlen(*realms)); 164 realms++; 165 } 166 } 167 168 if (config->rc) 169 l_checksum_update(sha, config->rc, config->rc_len); 170 171 if (!l_memeqzero(config->hessid, 6)) 172 l_checksum_update(sha, config->hessid, 6); 173 174 digest = l_checksum_get_string(sha); 175 l_checksum_free(sha); 176 config->object_path = l_strdup_printf("/%.8s_hotspot", digest); 177 l_free(digest); 178 179 return config->object_path; 180 } 181 182 static const char *hotspot_network_get_name(const struct network_info *info) 183 { 184 struct hs20_config *config = l_container_of(info, struct hs20_config, 185 super); 186 187 return config->name; 188 } 189 190 static const char *hotspot_network_get_type(const struct network_info *info) 191 { 192 return "hotspot"; 193 } 194 195 static bool hotspot_match_hessid(const struct network_info *info, 196 const uint8_t *hessid) 197 { 198 struct hs20_config *config = l_container_of(info, struct hs20_config, 199 super); 200 201 if (l_memeqzero(config->hessid, 6) || !hessid) 202 return false; 203 204 return !memcmp(config->hessid, hessid, 6); 205 } 206 207 static const uint8_t *hotspot_match_roaming_consortium( 208 const struct network_info *info, 209 const uint8_t *rc_ie, 210 size_t rc_len, 211 size_t *rc_len_out) 212 { 213 const uint8_t *rc1, *rc2, *rc3; 214 size_t rc1_len, rc2_len, rc3_len; 215 struct hs20_config *config = l_container_of(info, struct hs20_config, 216 super); 217 218 if (!config->rc || !rc_ie) 219 return NULL; 220 221 if (ie_parse_roaming_consortium_from_data(rc_ie, rc_ie[1] + 2, NULL, 222 &rc1, &rc1_len, &rc2, &rc2_len, 223 &rc3, &rc3_len) < 0) 224 return NULL; 225 226 /* rc1 is guaranteed to be set if the above returns success */ 227 if (rc1_len == config->rc_len && !memcmp(rc1, config->rc, rc1_len)) { 228 if (rc_len_out) 229 *rc_len_out = rc1_len; 230 return rc1; 231 } 232 233 if (rc2 && rc2_len == config->rc_len && 234 !memcmp(rc2, config->rc, rc2_len)) { 235 if (rc_len_out) 236 *rc_len_out = rc2_len; 237 return rc2; 238 } 239 240 if (rc3 && rc1_len == config->rc_len && 241 !memcmp(rc3, config->rc, rc3_len)) { 242 if (rc_len_out) 243 *rc_len_out = rc3_len; 244 return rc3; 245 } 246 247 return NULL; 248 } 249 250 static bool hotspot_match_nai_realms(const struct network_info *info, 251 const char **nai_realms) 252 { 253 const char **realms = nai_realms; 254 struct hs20_config *config = l_container_of(info, struct hs20_config, 255 super); 256 257 if (!config->nai_realms || !nai_realms) 258 return false; 259 260 while (*realms) { 261 if (l_strv_contains(config->nai_realms, *realms)) 262 return true; 263 264 realms++; 265 } 266 267 return false; 268 } 269 270 static const struct iovec *hotspot_network_get_ies( 271 const struct network_info *info, 272 struct scan_bss *bss, 273 size_t *num_elems) 274 { 275 static struct iovec iov[2]; 276 static uint8_t hs20_ie[7]; 277 static uint8_t rc_buf[11]; 278 const uint8_t *rc; 279 size_t rc_len; 280 size_t iov_elems = 0; 281 282 ie_build_hs20_indication(bss->hs20_version, hs20_ie); 283 284 iov[iov_elems].iov_base = hs20_ie; 285 iov[iov_elems].iov_len = hs20_ie[1] + 2; 286 iov_elems++; 287 288 rc = hotspot_match_roaming_consortium(info, bss->rc_ie, 289 bss->rc_ie[1] + 2, &rc_len); 290 if (rc) { 291 ie_build_roaming_consortium(rc, rc_len, rc_buf); 292 293 iov[iov_elems].iov_base = rc_buf; 294 iov[iov_elems].iov_len = rc_buf[1] + 2; 295 iov_elems++; 296 } 297 298 *num_elems = iov_elems; 299 300 return iov; 301 } 302 303 static char *hotspot_network_get_file_path(const struct network_info *info) 304 { 305 struct hs20_config *config = l_container_of(info, struct hs20_config, 306 super); 307 return l_strdup(config->filename); 308 } 309 310 static struct network_info_ops hotspot_ops = { 311 .open = hotspot_network_open, 312 .touch = hotspot_network_touch, 313 .sync = hotspot_network_sync, 314 .remove = hotspot_network_remove, 315 .free = hotspot_network_free, 316 .get_path = hotspot_network_get_path, 317 .get_name = hotspot_network_get_name, 318 .get_type = hotspot_network_get_type, 319 .get_extra_ies = hotspot_network_get_ies, 320 .get_file_path = hotspot_network_get_file_path, 321 322 .match_hessid = hotspot_match_hessid, 323 .match_roaming_consortium = hotspot_match_roaming_consortium, 324 .match_nai_realms = hotspot_match_nai_realms, 325 }; 326 327 static struct hs20_config *hs20_config_new(struct l_settings *settings, 328 char *filename, 329 struct network_config *c) 330 { 331 struct hs20_config *config; 332 char *hessid_str; 333 char **nai_realms = NULL; 334 size_t rc_len; 335 uint8_t *rc; 336 char *name; 337 338 /* One of HESSID, NAI realms, or Roaming Consortium must be included */ 339 hessid_str = l_settings_get_string(settings, "Hotspot", "HESSID"); 340 nai_realms = l_settings_get_string_list(settings, "Hotspot", 341 "NAIRealmNames", ','); 342 rc = l_settings_get_bytes(settings, "Hotspot", "RoamingConsortium", 343 &rc_len); 344 345 name = l_settings_get_string(settings, "Hotspot", "Name"); 346 347 if ((!hessid_str && !nai_realms && !rc) || !name) { 348 l_error("Could not parse hotspot config %s", filename); 349 goto free_values; 350 } 351 352 if (!storage_decrypt(settings, filename, name)) { 353 l_error("Could not open hotspot profile %s", filename); 354 goto free_values; 355 } 356 357 config = l_new(struct hs20_config, 1); 358 359 if (hessid_str) { 360 if (!util_string_to_address(hessid_str, config->hessid)) { 361 l_error("Invalid HESSID in settings"); 362 l_free(config); 363 goto free_values; 364 } 365 366 l_free(hessid_str); 367 } 368 369 if (nai_realms) 370 config->nai_realms = nai_realms; 371 372 if (rc) { 373 /* 374 * WiFi Alliance Hotspot 2.0 Spec - Section 3.1.4 375 * 376 * "The Consortium OI field is 3 or 5-octet field set to a value 377 * of a roaming consortium OI" 378 */ 379 if (rc && rc_len != 3 && rc_len != 5) { 380 l_warn("invalid RoamingConsortium length %zu", rc_len); 381 l_free(rc); 382 rc = NULL; 383 } 384 385 config->rc = rc; 386 config->rc_len = rc_len; 387 } 388 389 __network_info_init(&config->super, NULL, SECURITY_8021X, c); 390 config->super.is_hotspot = true; 391 config->name = name; 392 config->filename = l_strdup(filename); 393 config->super.ops = &hotspot_ops; 394 395 known_networks_add(&config->super); 396 397 return config; 398 399 free_values: 400 l_strv_free(nai_realms); 401 l_free(hessid_str); 402 l_free(name); 403 l_free(rc); 404 405 return NULL; 406 } 407 408 static void hs20_dir_watch_cb(const char *filename, 409 enum l_dir_watch_event event, 410 void *user_data) 411 { 412 struct l_settings *new; 413 uint64_t connected_time; 414 struct hs20_config *hs20; 415 struct network_config config; 416 417 L_AUTO_FREE_VAR(char *, full_path) = NULL; 418 419 /* 420 * Ignore notifications for the actual directory, we can't do 421 * anything about some of them anyway. Only react to 422 * notifications for files in the storage directory. 423 */ 424 if (!filename) 425 return; 426 427 full_path = storage_get_hotspot_path("%s", filename); 428 429 switch (event) { 430 case L_DIR_WATCH_EVENT_CREATED: 431 new = l_settings_new(); 432 433 if (!l_settings_load_from_file(new, full_path)) { 434 l_settings_free(new); 435 return; 436 } 437 438 __network_config_parse(new, full_path, &config); 439 440 hs20 = hs20_config_new(new, full_path, &config); 441 l_settings_free(new); 442 443 if (!hs20) 444 return; 445 446 l_queue_push_head(hs20_settings, hs20); 447 break; 448 case L_DIR_WATCH_EVENT_REMOVED: 449 hs20 = l_queue_remove_if(hs20_settings, match_filename, 450 full_path); 451 if (!hs20) 452 return; 453 454 known_networks_remove(&hs20->super); 455 break; 456 case L_DIR_WATCH_EVENT_MODIFIED: 457 hs20 = l_queue_find(hs20_settings, match_filename, full_path); 458 if (!hs20) 459 return; 460 461 new = l_settings_new(); 462 463 if (!l_settings_load_from_file(new, full_path)) { 464 l_settings_free(new); 465 return; 466 } 467 468 __network_config_parse(new, full_path, &config); 469 known_network_update(&hs20->super, &config); 470 471 /* TODO: Update hotspot specific settings */ 472 473 l_settings_free(new); 474 475 break; 476 case L_DIR_WATCH_EVENT_ACCESSED: 477 break; 478 case L_DIR_WATCH_EVENT_ATTRIB: 479 hs20 = l_queue_find(hs20_settings, match_filename, full_path); 480 if (!hs20) 481 return; 482 483 connected_time = l_path_get_mtime(full_path); 484 known_network_set_connected_time(&hs20->super, 485 connected_time); 486 break; 487 } 488 } 489 490 static void hs20_dir_watch_destroy(void *user_data) 491 { 492 hs20_dir_watch = NULL; 493 } 494 495 static int hotspot_init(void) 496 { 497 DIR *dir; 498 struct dirent *dirent; 499 500 L_AUTO_FREE_VAR(char *, hs20_dir) = storage_get_hotspot_path(NULL); 501 502 dir = opendir(hs20_dir); 503 if (!dir) 504 return -ENOENT; 505 506 hs20_settings = l_queue_new(); 507 508 while ((dirent = readdir(dir))) { 509 struct hs20_config *hs20; 510 struct l_settings *s; 511 char *filename; 512 struct network_config config; 513 514 filename = l_strdup_printf("%s/%s", hs20_dir, dirent->d_name); 515 s = l_settings_new(); 516 517 if (dirent->d_type == DT_UNKNOWN) { 518 if (!storage_is_file(filename)) 519 goto next; 520 } else if (dirent->d_type != DT_REG && 521 dirent->d_type != DT_LNK) { 522 goto next; 523 } 524 525 if (!l_settings_load_from_file(s, filename)) 526 goto next; 527 528 __network_config_parse(s, filename, &config); 529 530 hs20 = hs20_config_new(s, filename, &config); 531 if (hs20) 532 l_queue_push_head(hs20_settings, hs20); 533 534 next: 535 l_free(filename); 536 l_settings_free(s); 537 } 538 539 closedir(dir); 540 541 hs20_dir_watch = l_dir_watch_new(hs20_dir, hs20_dir_watch_cb, NULL, 542 hs20_dir_watch_destroy); 543 544 return 0; 545 } 546 547 static void hotspot_exit(void) 548 { 549 l_dir_watch_destroy(hs20_dir_watch); 550 551 l_queue_destroy(hs20_settings, NULL); 552 hs20_settings = NULL; 553 } 554 555 IWD_MODULE(hotspot, hotspot_init, hotspot_exit) 556 IWD_MODULE_DEPENDS(hotspot, known_networks)