1 /* 2 * This program is free software; you can redistribute it and/or modify 3 * it under the terms of the GNU General Public License as published by 4 * the Free Software Foundation; either version 2 of the License. 5 * 6 * This program is distributed in the hope that it will be useful, 7 * but WITHOUT ANY WARRANTY; without even the implied warranty of 8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 9 * GNU General Public License for more details. 10 * 11 * You should have received a copy of the GNU General Public License 12 * along with this program; if not, write to the Free Software 13 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. 14 * 15 * Copyright (C) 2020 embedd.ch 16 * Copyright (C) 2020 Felix Fietkau <nbd@nbd.name> 17 * Copyright (C) 2020 John Crispin <john@phrozen.org> 18 */ 19 20 #include <sys/types.h> 21 #include <sys/socket.h> 22 #include <net/ethernet.h> 23 #ifdef linux 24 #include <netinet/ether.h> 25 #endif 26 #include <net/if.h> 27 #include <stdlib.h> 28 29 #include <libubox/avl-cmp.h> 30 #include <libubox/blobmsg_json.h> 31 #include "usteer.h" 32 #include "node.h" 33 34 AVL_TREE(local_nodes, avl_strcmp, false, NULL); 35 static struct blob_buf b; 36 static char *node_up_script; 37 38 static void 39 usteer_local_node_state_reset(struct usteer_local_node *ln) 40 { 41 if (ln->req_state == REQ_IDLE) 42 return; 43 44 ubus_abort_request(ubus_ctx, &ln->req); 45 uloop_timeout_cancel(&ln->req_timer); 46 ln->req_state = REQ_IDLE; 47 } 48 49 static void 50 usteer_local_node_pending_bss_tm_free(struct usteer_local_node *ln) 51 { 52 struct usteer_bss_tm_query *query, *tmp; 53 54 list_for_each_entry_safe(query, tmp, &ln->bss_tm_queries, list) { 55 list_del(&query->list); 56 free(query); 57 } 58 } 59 60 static void 61 usteer_free_node(struct ubus_context *ctx, struct usteer_local_node *ln) 62 { 63 struct usteer_node_handler *h; 64 65 list_for_each_entry(h, &node_handlers, list) { 66 if (!h->free_node) 67 continue; 68 h->free_node(&ln->node); 69 } 70 71 usteer_local_node_pending_bss_tm_free(ln); 72 usteer_local_node_state_reset(ln); 73 usteer_sta_node_cleanup(&ln->node); 74 usteer_measurement_report_node_cleanup(&ln->node); 75 uloop_timeout_cancel(&ln->update); 76 uloop_timeout_cancel(&ln->bss_tm_queries_timeout); 77 avl_delete(&local_nodes, &ln->node.avl); 78 ubus_unregister_subscriber(ctx, &ln->ev); 79 kvlist_free(&ln->node_info); 80 free(ln); 81 } 82 83 struct usteer_local_node *usteer_local_node_by_bssid(uint8_t *bssid) { 84 struct usteer_local_node *ln; 85 struct usteer_node *n; 86 87 for_each_local_node(n) { 88 ln = container_of(n, struct usteer_local_node, node); 89 if (!memcmp(n->bssid, bssid, 6)) 90 return ln; 91 } 92 93 return NULL; 94 } 95 96 static void 97 usteer_handle_remove(struct ubus_context *ctx, struct ubus_subscriber *s, 98 uint32_t id) 99 { 100 struct usteer_local_node *ln = container_of(s, struct usteer_local_node, ev); 101 102 usteer_free_node(ctx, ln); 103 } 104 105 static int 106 usteer_handle_bss_tm_query(struct usteer_local_node *ln, struct blob_attr *msg) 107 { 108 enum { 109 BSS_TM_QUERY_ADDRESS, 110 BSS_TM_QUERY_DIALOG_TOKEN, 111 BSS_TM_QUERY_CANDIDATE_LIST, 112 __BSS_TM_QUERY_MAX 113 }; 114 struct blobmsg_policy policy[__BSS_TM_QUERY_MAX] = { 115 [BSS_TM_QUERY_ADDRESS] = { .name = "address", .type = BLOBMSG_TYPE_STRING }, 116 [BSS_TM_QUERY_DIALOG_TOKEN] = { .name = "dialog-token", .type = BLOBMSG_TYPE_INT8 }, 117 [BSS_TM_QUERY_CANDIDATE_LIST] = { .name = "candidate-list", .type = BLOBMSG_TYPE_STRING }, 118 }; 119 struct blob_attr *tb[__BSS_TM_QUERY_MAX]; 120 struct usteer_bss_tm_query *query; 121 uint8_t *sta_addr; 122 123 blobmsg_parse(policy, __BSS_TM_QUERY_MAX, tb, blob_data(msg), blob_len(msg)); 124 125 if (!tb[BSS_TM_QUERY_ADDRESS] || !tb[BSS_TM_QUERY_DIALOG_TOKEN]) 126 return 0; 127 128 query = calloc(1, sizeof(*query)); 129 if (!query) 130 return 0; 131 132 query->dialog_token = blobmsg_get_u8(tb[BSS_TM_QUERY_DIALOG_TOKEN]); 133 134 sta_addr = (uint8_t *) ether_aton(blobmsg_get_string(tb[BSS_TM_QUERY_ADDRESS])); 135 if (!sta_addr) 136 return 0; 137 138 memcpy(query->sta_addr, sta_addr, 6); 139 140 list_add(&query->list, &ln->bss_tm_queries); 141 uloop_timeout_set(&ln->bss_tm_queries_timeout, 1); 142 143 return 1; 144 } 145 146 static int 147 usteer_handle_bss_tm_response(struct usteer_local_node *ln, struct blob_attr *msg) 148 { 149 enum { 150 BSS_TM_RESPONSE_ADDRESS, 151 BSS_TM_RESPONSE_STATUS_CODE, 152 __BSS_TM_RESPONSE_MAX 153 }; 154 struct blobmsg_policy policy[__BSS_TM_RESPONSE_MAX] = { 155 [BSS_TM_RESPONSE_ADDRESS] = { .name = "address", .type = BLOBMSG_TYPE_STRING }, 156 [BSS_TM_RESPONSE_STATUS_CODE] = { .name = "status-code", .type = BLOBMSG_TYPE_INT8 }, 157 }; 158 struct blob_attr *tb[__BSS_TM_RESPONSE_MAX]; 159 struct sta_info *si; 160 struct sta *sta; 161 uint8_t *sta_addr; 162 163 blobmsg_parse(policy, __BSS_TM_RESPONSE_MAX, tb, blob_data(msg), blob_len(msg)); 164 165 if (!tb[BSS_TM_RESPONSE_ADDRESS] || !tb[BSS_TM_RESPONSE_STATUS_CODE]) 166 return 0; 167 168 sta_addr = (uint8_t *) ether_aton(blobmsg_get_string(tb[BSS_TM_RESPONSE_ADDRESS])); 169 if (!sta_addr) 170 return 0; 171 172 sta = usteer_sta_get(sta_addr, false); 173 if (!sta) 174 return 0; 175 176 si = usteer_sta_info_get(sta, &ln->node, false); 177 if (!si) 178 return 0; 179 180 si->bss_transition_response.status_code = blobmsg_get_u8(tb[BSS_TM_RESPONSE_STATUS_CODE]); 181 si->bss_transition_response.timestamp = current_time; 182 183 if (si->bss_transition_response.status_code) { 184 /* Cancel imminent kick in case BSS transition was rejected */ 185 si->kick_time = 0; 186 } 187 188 return 0; 189 } 190 191 static int 192 usteer_local_node_handle_beacon_report(struct usteer_local_node *ln, struct blob_attr *msg) 193 { 194 enum { 195 BR_ADDRESS, 196 BR_BSSID, 197 BR_RCPI, 198 BR_RSNI, 199 __BR_MAX 200 }; 201 struct blobmsg_policy policy[__BR_MAX] = { 202 [BR_ADDRESS] = { .name = "address", .type = BLOBMSG_TYPE_STRING }, 203 [BR_BSSID] = { .name = "bssid", .type = BLOBMSG_TYPE_STRING }, 204 [BR_RCPI] = { .name = "rcpi", .type = BLOBMSG_TYPE_INT16 }, 205 [BR_RSNI] = { .name = "rsni", .type = BLOBMSG_TYPE_INT16 }, 206 }; 207 struct blob_attr *tb[__BR_MAX]; 208 struct usteer_node *node; 209 uint8_t *addr; 210 struct sta *sta; 211 212 blobmsg_parse(policy, __BR_MAX, tb, blob_data(msg), blob_len(msg)); 213 if (!tb[BR_ADDRESS] || !tb[BR_BSSID] || !tb[BR_RCPI] || !tb[BR_RSNI]) 214 return 0; 215 216 addr = (uint8_t *) ether_aton(blobmsg_get_string(tb[BR_ADDRESS])); 217 if (!addr) 218 return 0; 219 220 sta = usteer_sta_get(addr, false); 221 if (!sta) 222 return 0; 223 224 addr = (uint8_t *) ether_aton(blobmsg_get_string(tb[BR_BSSID])); 225 if (!addr) 226 return 0; 227 228 node = usteer_node_by_bssid(addr); 229 if (!node) 230 return 0; 231 232 usteer_measurement_report_add(sta, node, 233 (uint8_t)blobmsg_get_u16(tb[BR_RCPI]), 234 (uint8_t)blobmsg_get_u16(tb[BR_RSNI]), 235 current_time); 236 return 0; 237 } 238 239 static int 240 usteer_local_node_handle_link_measurement_report(struct usteer_local_node *ln, struct blob_attr *msg) 241 { 242 enum { 243 LMR_ADDRESS, 244 LMR_RCPI, 245 LMR_RSNI, 246 __LMR_MAX 247 }; 248 struct blobmsg_policy policy[__LMR_MAX] = { 249 [LMR_ADDRESS] = { .name = "address", .type = BLOBMSG_TYPE_STRING }, 250 [LMR_RCPI] = { .name = "rcpi", .type = BLOBMSG_TYPE_INT16 }, 251 [LMR_RSNI] = { .name = "rsni", .type = BLOBMSG_TYPE_INT16 }, 252 }; 253 struct blob_attr *tb[__LMR_MAX]; 254 uint8_t *addr; 255 struct sta *sta; 256 257 blobmsg_parse(policy, __LMR_MAX, tb, blob_data(msg), blob_len(msg)); 258 if (!tb[LMR_ADDRESS] || !tb[LMR_RCPI] || !tb[LMR_RSNI]) 259 return 0; 260 261 addr = (uint8_t *) ether_aton(blobmsg_get_string(tb[LMR_ADDRESS])); 262 if (!addr) 263 return 0; 264 265 sta = usteer_sta_get(addr, false); 266 if (!sta) 267 return 0; 268 269 usteer_measurement_report_add(sta, &ln->node, 270 (uint8_t)blobmsg_get_u16(tb[LMR_RCPI]), 271 (uint8_t)blobmsg_get_u16(tb[LMR_RSNI]), 272 current_time); 273 return 0; 274 } 275 276 static int 277 usteer_handle_event(struct ubus_context *ctx, struct ubus_object *obj, 278 struct ubus_request_data *req, const char *method, 279 struct blob_attr *msg) 280 { 281 enum { 282 EVENT_ADDR, 283 EVENT_SIGNAL, 284 EVENT_TARGET, 285 EVENT_FREQ, 286 __EVENT_MAX 287 }; 288 struct blobmsg_policy policy[__EVENT_MAX] = { 289 [EVENT_ADDR] = { .name = "address", .type = BLOBMSG_TYPE_STRING }, 290 [EVENT_SIGNAL] = { .name = "signal", .type = BLOBMSG_TYPE_INT32 }, 291 [EVENT_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING }, 292 [EVENT_FREQ] = { .name = "freq", .type = BLOBMSG_TYPE_INT32 }, 293 }; 294 enum usteer_event_type ev_type = __EVENT_TYPE_MAX; 295 struct blob_attr *tb[__EVENT_MAX]; 296 struct usteer_local_node *ln; 297 struct usteer_node *node; 298 int signal = NO_SIGNAL; 299 int freq = 0; 300 const char *addr_str; 301 const uint8_t *addr; 302 int i; 303 bool ret; 304 305 usteer_update_time(); 306 307 ln = container_of(obj, struct usteer_local_node, ev.obj); 308 309 if(!strcmp(method, "bss-transition-query")) { 310 return usteer_handle_bss_tm_query(ln, msg); 311 } else if(!strcmp(method, "bss-transition-response")) { 312 return usteer_handle_bss_tm_response(ln, msg); 313 } else if(!strcmp(method, "beacon-report")) { 314 return usteer_local_node_handle_beacon_report(ln, msg); 315 } else if(!strcmp(method, "link-measurement-report")) { 316 return usteer_local_node_handle_link_measurement_report(ln, msg); 317 } 318 319 for (i = 0; i < ARRAY_SIZE(event_types); i++) { 320 if (strcmp(method, event_types[i]) != 0) 321 continue; 322 323 ev_type = i; 324 break; 325 } 326 327 ln = container_of(obj, struct usteer_local_node, ev.obj); 328 node = &ln->node; 329 blobmsg_parse(policy, __EVENT_MAX, tb, blob_data(msg), blob_len(msg)); 330 if (!tb[EVENT_ADDR] || !tb[EVENT_FREQ]) 331 return UBUS_STATUS_INVALID_ARGUMENT; 332 333 if (tb[EVENT_SIGNAL]) 334 signal = (int32_t) blobmsg_get_u32(tb[EVENT_SIGNAL]); 335 336 if (tb[EVENT_FREQ]) 337 freq = blobmsg_get_u32(tb[EVENT_FREQ]); 338 339 addr_str = blobmsg_data(tb[EVENT_ADDR]); 340 addr = (uint8_t *) ether_aton(addr_str); 341 if (!addr) 342 return UBUS_STATUS_INVALID_ARGUMENT; 343 344 ret = usteer_handle_sta_event(node, addr, ev_type, freq, signal); 345 346 MSG(DEBUG, "received %s event from %s, signal=%d, freq=%d, handled:%s\n", 347 method, addr_str, signal, freq, ret ? "true" : "false"); 348 349 return ret ? 0 : 17 /* WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA */; 350 } 351 352 static void 353 usteer_local_node_assoc_update(struct sta_info *si, struct blob_attr *data) 354 { 355 enum { 356 MSG_ASSOC, 357 __MSG_MAX, 358 }; 359 static struct blobmsg_policy policy[__MSG_MAX] = { 360 [MSG_ASSOC] = { "assoc", BLOBMSG_TYPE_BOOL }, 361 }; 362 struct blob_attr *tb[__MSG_MAX]; 363 struct usteer_remote_node *rn; 364 struct sta_info *remote_si; 365 366 blobmsg_parse(policy, __MSG_MAX, tb, blobmsg_data(data), blobmsg_data_len(data)); 367 if (tb[MSG_ASSOC] && blobmsg_get_u8(tb[MSG_ASSOC])) { 368 if (si->connected == STA_NOT_CONNECTED) { 369 /* New connection. Check if STA roamed. */ 370 for_each_remote_node(rn) { 371 remote_si = usteer_sta_info_get(si->sta, &rn->node, NULL); 372 if (!remote_si) 373 continue; 374 375 if (current_time - remote_si->last_connected < config.roam_process_timeout) { 376 rn->node.roam_events.source++; 377 /* Don't abort looking for roam sources here. 378 * The client might have roamed via another node 379 * within the roam-timeout. 380 */ 381 } 382 } 383 } 384 si->connected = STA_CONNECTED; 385 } 386 } 387 388 static void 389 usteer_local_node_update_sta_rrm_wnm(struct sta_info *si, struct blob_attr *client_attr) 390 { 391 static const struct blobmsg_policy rrm_policy = { 392 .name = "rrm", 393 .type = BLOBMSG_TYPE_ARRAY, 394 }; 395 static const struct blobmsg_policy ext_capa_policy = { 396 .name = "extended_capabilities", 397 .type = BLOBMSG_TYPE_ARRAY, 398 }; 399 struct blob_attr *rrm_blob = NULL, *wnm_blob = NULL, *cur; 400 int rem; 401 int i = 0; 402 403 /* RRM */ 404 blobmsg_parse(&rrm_policy, 1, &rrm_blob, blobmsg_data(client_attr), blobmsg_data_len(client_attr)); 405 if (!rrm_blob) 406 return; 407 408 si->rrm = blobmsg_get_u32(blobmsg_data(rrm_blob)); 409 410 /* Extended Capabilities / WNM */ 411 blobmsg_parse(&ext_capa_policy, 1, &wnm_blob, blobmsg_data(client_attr), blobmsg_data_len(client_attr)); 412 if (!wnm_blob) 413 return; 414 415 blobmsg_for_each_attr(cur, wnm_blob, rem) { 416 if (blobmsg_type(cur) != BLOBMSG_TYPE_INT32) 417 return; 418 419 if (i == 2) { 420 if (blobmsg_get_u32(cur) & (1 << 3)) 421 si->bss_transition = true; 422 } 423 424 i++; 425 } 426 } 427 428 static void 429 usteer_local_node_set_assoc(struct usteer_local_node *ln, struct blob_attr *cl) 430 { 431 struct usteer_node *node = &ln->node; 432 struct usteer_node_handler *h; 433 struct blob_attr *cur; 434 struct sta_info *si; 435 struct sta *sta; 436 int n_assoc = 0; 437 int rem; 438 439 usteer_update_time(); 440 441 list_for_each_entry(si, &node->sta_info, node_list) { 442 if (si->connected) 443 si->connected = STA_DISCONNECTED; 444 } 445 446 blobmsg_for_each_attr(cur, cl, rem) { 447 uint8_t *addr = (uint8_t *) ether_aton(blobmsg_name(cur)); 448 bool create; 449 450 if (!addr) 451 continue; 452 453 sta = usteer_sta_get(addr, true); 454 si = usteer_sta_info_get(sta, node, &create); 455 list_for_each_entry(h, &node_handlers, list) { 456 if (!h->update_sta) 457 continue; 458 459 h->update_sta(node, si); 460 } 461 usteer_local_node_assoc_update(si, cur); 462 if (si->connected == STA_CONNECTED) { 463 si->last_connected = current_time; 464 n_assoc++; 465 } 466 467 /* Read RRM information */ 468 usteer_local_node_update_sta_rrm_wnm(si, cur); 469 } 470 471 node->n_assoc = n_assoc; 472 473 list_for_each_entry(si, &node->sta_info, node_list) { 474 if (si->connected != STA_DISCONNECTED) 475 continue; 476 477 usteer_sta_disconnected(si); 478 MSG(VERBOSE, "station "MAC_ADDR_FMT" disconnected from node %s\n", 479 MAC_ADDR_DATA(si->sta->addr), usteer_node_name(node)); 480 } 481 } 482 483 static void 484 usteer_local_node_list_cb(struct ubus_request *req, int type, struct blob_attr *msg) 485 { 486 enum { 487 MSG_FREQ, 488 MSG_CLIENTS, 489 __MSG_MAX, 490 }; 491 static struct blobmsg_policy policy[__MSG_MAX] = { 492 [MSG_FREQ] = { "freq", BLOBMSG_TYPE_INT32 }, 493 [MSG_CLIENTS] = { "clients", BLOBMSG_TYPE_TABLE }, 494 }; 495 struct blob_attr *tb[__MSG_MAX]; 496 struct usteer_local_node *ln; 497 struct usteer_node *node; 498 499 ln = container_of(req, struct usteer_local_node, req); 500 node = &ln->node; 501 502 blobmsg_parse(policy, __MSG_MAX, tb, blob_data(msg), blob_len(msg)); 503 if (!tb[MSG_FREQ] || !tb[MSG_CLIENTS]) 504 return; 505 506 node->freq = blobmsg_get_u32(tb[MSG_FREQ]); 507 usteer_local_node_set_assoc(ln, tb[MSG_CLIENTS]); 508 } 509 510 static void 511 usteer_local_node_status_cb(struct ubus_request *req, int type, struct blob_attr *msg) 512 { 513 enum { 514 MSG_FREQ, 515 MSG_CHANNEL, 516 MSG_OP_CLASS, 517 MSG_BEACON_INTERVAL, 518 __MSG_MAX, 519 }; 520 static struct blobmsg_policy policy[__MSG_MAX] = { 521 [MSG_FREQ] = { "freq", BLOBMSG_TYPE_INT32 }, 522 [MSG_CHANNEL] = { "channel", BLOBMSG_TYPE_INT32 }, 523 [MSG_OP_CLASS] = { "op_class", BLOBMSG_TYPE_INT32 }, 524 [MSG_BEACON_INTERVAL] = { "beacon_interval", BLOBMSG_TYPE_INT32 }, 525 }; 526 struct blob_attr *tb[__MSG_MAX]; 527 struct usteer_local_node *ln; 528 struct usteer_node *node; 529 530 ln = container_of(req, struct usteer_local_node, req); 531 node = &ln->node; 532 533 blobmsg_parse(policy, __MSG_MAX, tb, blob_data(msg), blob_len(msg)); 534 if (tb[MSG_FREQ]) 535 node->freq = blobmsg_get_u32(tb[MSG_FREQ]); 536 if (tb[MSG_CHANNEL]) 537 node->channel = blobmsg_get_u32(tb[MSG_CHANNEL]); 538 if (tb[MSG_OP_CLASS]) 539 node->op_class = blobmsg_get_u32(tb[MSG_OP_CLASS]); 540 541 /* Local-Node */ 542 if (tb[MSG_BEACON_INTERVAL]) 543 ln->beacon_interval = blobmsg_get_u32(tb[MSG_BEACON_INTERVAL]); 544 } 545 546 static void 547 usteer_local_node_rrm_nr_cb(struct ubus_request *req, int type, struct blob_attr *msg) 548 { 549 static const struct blobmsg_policy policy = { 550 "value", BLOBMSG_TYPE_ARRAY 551 }; 552 struct usteer_local_node *ln; 553 struct blob_attr *tb; 554 555 ln = container_of(req, struct usteer_local_node, req); 556 557 blobmsg_parse(&policy, 1, &tb, blob_data(msg), blob_len(msg)); 558 if (!tb) 559 return; 560 561 usteer_node_set_blob(&ln->node.rrm_nr, tb); 562 } 563 564 static void 565 usteer_local_node_req_cb(struct ubus_request *req, int ret) 566 { 567 struct usteer_local_node *ln; 568 569 ln = container_of(req, struct usteer_local_node, req); 570 uloop_timeout_set(&ln->req_timer, 1); 571 } 572 573 static bool 574 usteer_add_rrm_data(struct usteer_local_node *ln, struct usteer_node *node) 575 { 576 if (node == &ln->node) 577 return false; 578 579 if (!node->rrm_nr) 580 return false; 581 582 /* Remote node only adds same SSID. Required for local-node. */ 583 if (strcmp(ln->node.ssid, node->ssid) != 0) 584 return false; 585 586 blobmsg_add_field(&b, BLOBMSG_TYPE_ARRAY, "", 587 blobmsg_data(node->rrm_nr), 588 blobmsg_data_len(node->rrm_nr)); 589 590 return true; 591 } 592 593 static void 594 usteer_local_node_prepare_rrm_set(struct usteer_local_node *ln) 595 { 596 struct usteer_node *node, *last_remote_neighbor = NULL; 597 int i = 0; 598 void *c; 599 600 c = blobmsg_open_array(&b, "list"); 601 for_each_local_node(node) { 602 if (i >= config.max_neighbor_reports) 603 break; 604 if (usteer_add_rrm_data(ln, node)) 605 i++; 606 } 607 608 while (i < config.max_neighbor_reports) { 609 node = usteer_node_get_next_neighbor(&ln->node, last_remote_neighbor); 610 if (!node) { 611 /* No more nodes available */ 612 break; 613 } 614 615 last_remote_neighbor = node; 616 if (usteer_add_rrm_data(ln, node)) 617 i++; 618 } 619 620 blobmsg_close_array(&b, c); 621 } 622 623 static void 624 usteer_local_node_state_next(struct uloop_timeout *timeout) 625 { 626 struct usteer_local_node *ln; 627 628 ln = container_of(timeout, struct usteer_local_node, req_timer); 629 630 ln->req_state++; 631 if (ln->req_state >= __REQ_MAX) { 632 ln->req_state = REQ_IDLE; 633 return; 634 } 635 636 blob_buf_init(&b, 0); 637 switch (ln->req_state) { 638 case REQ_CLIENTS: 639 ubus_invoke_async(ubus_ctx, ln->obj_id, "get_clients", b.head, &ln->req); 640 ln->req.data_cb = usteer_local_node_list_cb; 641 break; 642 case REQ_STATUS: 643 ubus_invoke_async(ubus_ctx, ln->obj_id, "get_status", b.head, &ln->req); 644 ln->req.data_cb = usteer_local_node_status_cb; 645 break; 646 case REQ_RRM_SET_LIST: 647 usteer_local_node_prepare_rrm_set(ln); 648 ubus_invoke_async(ubus_ctx, ln->obj_id, "rrm_nr_set", b.head, &ln->req); 649 ln->req.data_cb = NULL; 650 break; 651 case REQ_RRM_GET_OWN: 652 ubus_invoke_async(ubus_ctx, ln->obj_id, "rrm_nr_get_own", b.head, &ln->req); 653 ln->req.data_cb = usteer_local_node_rrm_nr_cb; 654 break; 655 default: 656 break; 657 } 658 ln->req.complete_cb = usteer_local_node_req_cb; 659 ubus_complete_request_async(ubus_ctx, &ln->req); 660 } 661 662 static void 663 usteer_local_node_request_link_measurement(struct usteer_local_node *ln) 664 { 665 unsigned int min_count = DIV_ROUND_UP(config.link_measurement_interval, config.local_sta_update); 666 struct usteer_node *node; 667 struct sta_info *si; 668 669 node = &ln->node; 670 671 if (ln->link_measurement_tries < min_count) { 672 ln->link_measurement_tries++; 673 return; 674 } 675 676 ln->link_measurement_tries = 0; 677 678 if (!config.link_measurement_interval) 679 return; 680 681 list_for_each_entry(si, &node->sta_info, node_list) { 682 if (si->connected != STA_CONNECTED) 683 continue; 684 685 usteer_ubus_trigger_link_measurement(si); 686 } 687 } 688 689 static void 690 usteer_local_node_update(struct uloop_timeout *timeout) 691 { 692 struct usteer_local_node *ln; 693 struct usteer_node_handler *h; 694 struct usteer_node *node; 695 696 ln = container_of(timeout, struct usteer_local_node, update); 697 node = &ln->node; 698 699 list_for_each_entry(h, &node_handlers, list) { 700 if (!h->update_node) 701 continue; 702 703 h->update_node(node); 704 } 705 706 usteer_local_node_state_reset(ln); 707 uloop_timeout_set(&ln->req_timer, 1); 708 usteer_local_node_kick(ln); 709 usteer_band_steering_perform_steer(ln); 710 usteer_local_node_request_link_measurement(ln); 711 712 uloop_timeout_set(timeout, config.local_sta_update); 713 } 714 715 static void 716 usteer_local_node_process_bss_tm_queries(struct uloop_timeout *timeout) 717 { 718 struct usteer_bss_tm_query *query, *tmp; 719 struct usteer_local_node *ln; 720 struct usteer_node *node; 721 struct sta_info *si; 722 struct sta *sta; 723 uint8_t validity_period; 724 725 ln = container_of(timeout, struct usteer_local_node, bss_tm_queries_timeout); 726 node = &ln->node; 727 728 validity_period = 10000 / usteer_local_node_get_beacon_interval(ln); /* ~ 10 seconds */ 729 730 list_for_each_entry_safe(query, tmp, &ln->bss_tm_queries, list) { 731 sta = usteer_sta_get(query->sta_addr, false); 732 if (!sta) 733 continue; 734 735 si = usteer_sta_info_get(sta, node, false); 736 if (!si) 737 continue; 738 739 usteer_ubus_bss_transition_request(si, query->dialog_token, false, false, validity_period, NULL); 740 } 741 742 /* Free pending queries we can not handle */ 743 usteer_local_node_pending_bss_tm_free(ln); 744 } 745 746 static struct usteer_local_node * 747 usteer_get_node(struct ubus_context *ctx, const char *name) 748 { 749 struct usteer_local_node *ln; 750 struct usteer_node *node; 751 char *str; 752 753 ln = avl_find_element(&local_nodes, name, ln, node.avl); 754 if (ln) 755 return ln; 756 757 ln = calloc_a(sizeof(*ln), &str, strlen(name) + 1); 758 node = &ln->node; 759 node->type = NODE_TYPE_LOCAL; 760 node->created = current_time; 761 node->avl.key = strcpy(str, name); 762 ln->ev.remove_cb = usteer_handle_remove; 763 ln->ev.cb = usteer_handle_event; 764 ln->update.cb = usteer_local_node_update; 765 ln->req_timer.cb = usteer_local_node_state_next; 766 ubus_register_subscriber(ctx, &ln->ev); 767 avl_insert(&local_nodes, &node->avl); 768 kvlist_init(&ln->node_info, kvlist_blob_len); 769 INIT_LIST_HEAD(&node->sta_info); 770 INIT_LIST_HEAD(&node->measurements); 771 772 ln->bss_tm_queries_timeout.cb = usteer_local_node_process_bss_tm_queries; 773 INIT_LIST_HEAD(&ln->bss_tm_queries); 774 return ln; 775 } 776 777 static void 778 usteer_node_run_update_script(struct usteer_node *node) 779 { 780 struct usteer_local_node *ln = container_of(node, struct usteer_local_node, node); 781 char *val; 782 783 if (!node_up_script) 784 return; 785 786 val = alloca(strlen(node_up_script) + strlen(ln->iface) + 8); 787 sprintf(val, "%s '%s'", node_up_script, ln->iface); 788 if (system(val)) 789 MSG(INFO, "failed to execute %s\n", val); 790 } 791 792 static void 793 usteer_check_node_enabled(struct usteer_local_node *ln) 794 { 795 bool ssid_disabled = config.ssid_list; 796 struct blob_attr *cur; 797 int rem; 798 799 blobmsg_for_each_attr(cur, config.ssid_list, rem) { 800 if (strcmp(blobmsg_get_string(cur), ln->node.ssid) != 0) 801 continue; 802 803 ssid_disabled = false; 804 break; 805 } 806 807 if (ln->node.disabled == ssid_disabled) 808 return; 809 810 ln->node.disabled = ssid_disabled; 811 812 if (ssid_disabled) { 813 MSG(INFO, "Disconnecting from local node %s\n", usteer_node_name(&ln->node)); 814 usteer_local_node_state_reset(ln); 815 usteer_sta_node_cleanup(&ln->node); 816 usteer_measurement_report_node_cleanup(&ln->node); 817 uloop_timeout_cancel(&ln->update); 818 ubus_unsubscribe(ubus_ctx, &ln->ev, ln->obj_id); 819 return; 820 } 821 822 MSG(INFO, "Connecting to local node %s\n", usteer_node_name(&ln->node)); 823 ubus_subscribe(ubus_ctx, &ln->ev, ln->obj_id); 824 uloop_timeout_set(&ln->update, 1); 825 usteer_node_run_update_script(&ln->node); 826 } 827 828 static void 829 usteer_register_node(struct ubus_context *ctx, const char *name, uint32_t id) 830 { 831 struct usteer_local_node *ln; 832 struct usteer_node_handler *h; 833 const char *iface; 834 int offset = sizeof("hostapd.") - 1; 835 836 iface = name + offset; 837 if (strncmp(name, "hostapd.", iface - name) != 0) 838 return; 839 840 MSG(INFO, "Creating local node %s\n", name); 841 ln = usteer_get_node(ctx, name); 842 ln->obj_id = id; 843 ln->iface = usteer_node_name(&ln->node) + offset; 844 ln->ifindex = if_nametoindex(ln->iface); 845 846 blob_buf_init(&b, 0); 847 blobmsg_add_u32(&b, "notify_response", 1); 848 ubus_invoke(ctx, id, "notify_response", b.head, NULL, NULL, 1000); 849 850 blob_buf_init(&b, 0); 851 blobmsg_add_u8(&b, "neighbor_report", 1); 852 blobmsg_add_u8(&b, "link_measurement", 1); 853 blobmsg_add_u8(&b, "beacon_report", 1); 854 blobmsg_add_u8(&b, "bss_transition", 1); 855 ubus_invoke(ctx, id, "bss_mgmt_enable", b.head, NULL, NULL, 1000); 856 857 list_for_each_entry(h, &node_handlers, list) { 858 if (!h->init_node) 859 continue; 860 861 h->init_node(&ln->node); 862 } 863 864 ln->node.disabled = true; 865 usteer_check_node_enabled(ln); 866 } 867 868 static void 869 usteer_event_handler(struct ubus_context *ctx, struct ubus_event_handler *ev, 870 const char *type, struct blob_attr *msg) 871 { 872 static const struct blobmsg_policy policy[2] = { 873 { .name = "id", .type = BLOBMSG_TYPE_INT32 }, 874 { .name = "path", .type = BLOBMSG_TYPE_STRING }, 875 }; 876 struct blob_attr *tb[2]; 877 const char *path; 878 879 blobmsg_parse(policy, 2, tb, blob_data(msg), blob_len(msg)); 880 881 if (!tb[0] || !tb[1]) 882 return; 883 884 path = blobmsg_data(tb[1]); 885 usteer_register_node(ctx, path, blobmsg_get_u32(tb[0])); 886 } 887 888 static void 889 usteer_register_events(struct ubus_context *ctx) 890 { 891 static struct ubus_event_handler handler = { 892 .cb = usteer_event_handler 893 }; 894 895 ubus_register_event_handler(ctx, &handler, "ubus.object.add"); 896 } 897 898 static void 899 node_list_cb(struct ubus_context *ctx, struct ubus_object_data *obj, void *priv) 900 { 901 usteer_register_node(ctx, obj->path, obj->id); 902 } 903 904 int 905 usteer_local_node_get_beacon_interval(struct usteer_local_node *ln) 906 { 907 /* Check if beacon-interval is not available (pre-21.02+) */ 908 if (ln->beacon_interval < 1) 909 return 100; 910 911 return ln->beacon_interval; 912 } 913 914 void config_set_node_up_script(struct blob_attr *data) 915 { 916 const char *val; 917 struct usteer_node *node; 918 919 if (!data) 920 return; 921 922 val = blobmsg_get_string(data); 923 if (node_up_script && !strcmp(val, node_up_script)) 924 return; 925 926 free(node_up_script); 927 928 if (!strlen(val)) { 929 node_up_script = NULL; 930 return; 931 } 932 933 node_up_script = strdup(val); 934 935 for_each_local_node(node) 936 usteer_node_run_update_script(node); 937 } 938 939 void config_get_node_up_script(struct blob_buf *buf) 940 { 941 if (!node_up_script) 942 return; 943 944 blobmsg_add_string(buf, "node_up_script", node_up_script); 945 } 946 947 void config_set_ssid_list(struct blob_attr *data) 948 { 949 struct usteer_local_node *ln; 950 951 free(config.ssid_list); 952 953 if (data && blobmsg_len(data)) 954 config.ssid_list = blob_memdup(data); 955 else 956 config.ssid_list = NULL; 957 958 avl_for_each_element(&local_nodes, ln, node.avl) 959 usteer_check_node_enabled(ln); 960 } 961 962 void config_get_ssid_list(struct blob_buf *buf) 963 { 964 if (config.ssid_list) 965 blobmsg_add_blob(buf, config.ssid_list); 966 } 967 968 void 969 usteer_local_nodes_init(struct ubus_context *ctx) 970 { 971 usteer_register_events(ctx); 972 ubus_lookup(ctx, "hostapd.*", node_list_cb, NULL); 973 } 974
This page was automatically generated by LXR 0.3.1. • OpenWrt