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.
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.
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.
15 * Copyright (C) 2020 embedd.ch
16 * Copyright (C) 2020 Felix Fietkau <nbd@nbd.name>
17 * Copyright (C) 2020 John Crispin <john@phrozen.org>
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <net/ethernet.h>
24 #include <netinet/ether.h>
29 #include <libubox/avl-cmp.h>
30 #include <libubox/blobmsg_json.h>
34 AVL_TREE(local_nodes
, avl_strcmp
, false, NULL
);
35 static struct blob_buf b
;
36 static char *node_up_script
;
39 usteer_local_node_state_reset(struct usteer_local_node
*ln
)
41 if (ln
->req_state
== REQ_IDLE
)
44 ubus_abort_request(ubus_ctx
, &ln
->req
);
45 uloop_timeout_cancel(&ln
->req_timer
);
46 ln
->req_state
= REQ_IDLE
;
50 usteer_local_node_pending_bss_tm_free(struct usteer_local_node
*ln
)
52 struct usteer_bss_tm_query
*query
, *tmp
;
54 list_for_each_entry_safe(query
, tmp
, &ln
->bss_tm_queries
, list
) {
55 list_del(&query
->list
);
61 usteer_free_node(struct ubus_context
*ctx
, struct usteer_local_node
*ln
)
63 struct usteer_node_handler
*h
;
65 list_for_each_entry(h
, &node_handlers
, list
) {
68 h
->free_node(&ln
->node
);
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
);
83 struct usteer_local_node
*usteer_local_node_by_bssid(uint8_t *bssid
) {
84 struct usteer_local_node
*ln
;
85 struct usteer_node
*n
;
87 for_each_local_node(n
) {
88 ln
= container_of(n
, struct usteer_local_node
, node
);
89 if (!memcmp(n
->bssid
, bssid
, 6))
97 usteer_handle_remove(struct ubus_context
*ctx
, struct ubus_subscriber
*s
,
100 struct usteer_local_node
*ln
= container_of(s
, struct usteer_local_node
, ev
);
102 usteer_free_node(ctx
, ln
);
106 usteer_handle_bss_tm_query(struct usteer_local_node
*ln
, struct blob_attr
*msg
)
109 BSS_TM_QUERY_ADDRESS
,
110 BSS_TM_QUERY_DIALOG_TOKEN
,
111 BSS_TM_QUERY_CANDIDATE_LIST
,
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
},
119 struct blob_attr
*tb
[__BSS_TM_QUERY_MAX
];
120 struct usteer_bss_tm_query
*query
;
123 blobmsg_parse(policy
, __BSS_TM_QUERY_MAX
, tb
, blob_data(msg
), blob_len(msg
));
125 if (!tb
[BSS_TM_QUERY_ADDRESS
] || !tb
[BSS_TM_QUERY_DIALOG_TOKEN
])
128 query
= calloc(1, sizeof(*query
));
132 query
->dialog_token
= blobmsg_get_u8(tb
[BSS_TM_QUERY_DIALOG_TOKEN
]);
134 sta_addr
= (uint8_t *) ether_aton(blobmsg_get_string(tb
[BSS_TM_QUERY_ADDRESS
]));
138 memcpy(query
->sta_addr
, sta_addr
, 6);
140 list_add(&query
->list
, &ln
->bss_tm_queries
);
141 uloop_timeout_set(&ln
->bss_tm_queries_timeout
, 1);
147 usteer_handle_bss_tm_response(struct usteer_local_node
*ln
, struct blob_attr
*msg
)
150 BSS_TM_RESPONSE_ADDRESS
,
151 BSS_TM_RESPONSE_STATUS_CODE
,
152 __BSS_TM_RESPONSE_MAX
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
},
158 struct blob_attr
*tb
[__BSS_TM_RESPONSE_MAX
];
163 blobmsg_parse(policy
, __BSS_TM_RESPONSE_MAX
, tb
, blob_data(msg
), blob_len(msg
));
165 if (!tb
[BSS_TM_RESPONSE_ADDRESS
] || !tb
[BSS_TM_RESPONSE_STATUS_CODE
])
168 sta_addr
= (uint8_t *) ether_aton(blobmsg_get_string(tb
[BSS_TM_RESPONSE_ADDRESS
]));
172 sta
= usteer_sta_get(sta_addr
, false);
176 si
= usteer_sta_info_get(sta
, &ln
->node
, false);
180 si
->bss_transition_response
.status_code
= blobmsg_get_u8(tb
[BSS_TM_RESPONSE_STATUS_CODE
]);
181 si
->bss_transition_response
.timestamp
= current_time
;
183 if (si
->bss_transition_response
.status_code
) {
184 /* Cancel imminent kick in case BSS transition was rejected */
192 usteer_local_node_handle_beacon_report(struct usteer_local_node
*ln
, struct blob_attr
*msg
)
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
},
207 struct blob_attr
*tb
[__BR_MAX
];
208 struct usteer_node
*node
;
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
])
216 addr
= (uint8_t *) ether_aton(blobmsg_get_string(tb
[BR_ADDRESS
]));
220 sta
= usteer_sta_get(addr
, false);
224 addr
= (uint8_t *) ether_aton(blobmsg_get_string(tb
[BR_BSSID
]));
228 node
= usteer_node_by_bssid(addr
);
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
]),
240 usteer_local_node_handle_link_measurement_report(struct usteer_local_node
*ln
, struct blob_attr
*msg
)
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
},
253 struct blob_attr
*tb
[__LMR_MAX
];
257 blobmsg_parse(policy
, __LMR_MAX
, tb
, blob_data(msg
), blob_len(msg
));
258 if (!tb
[LMR_ADDRESS
] || !tb
[LMR_RCPI
] || !tb
[LMR_RSNI
])
261 addr
= (uint8_t *) ether_aton(blobmsg_get_string(tb
[LMR_ADDRESS
]));
265 sta
= usteer_sta_get(addr
, false);
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
]),
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
)
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
},
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
;
300 const char *addr_str
;
305 usteer_update_time();
307 ln
= container_of(obj
, struct usteer_local_node
, ev
.obj
);
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
);
319 for (i
= 0; i
< ARRAY_SIZE(event_types
); i
++) {
320 if (strcmp(method
, event_types
[i
]) != 0)
327 ln
= container_of(obj
, struct usteer_local_node
, ev
.obj
);
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
;
333 if (tb
[EVENT_SIGNAL
])
334 signal
= (int32_t) blobmsg_get_u32(tb
[EVENT_SIGNAL
]);
337 freq
= blobmsg_get_u32(tb
[EVENT_FREQ
]);
339 addr_str
= blobmsg_data(tb
[EVENT_ADDR
]);
340 addr
= (uint8_t *) ether_aton(addr_str
);
342 return UBUS_STATUS_INVALID_ARGUMENT
;
344 ret
= usteer_handle_sta_event(node
, addr
, ev_type
, freq
, signal
);
346 MSG(DEBUG
, "received %s event from %s, signal=%d, freq=%d, handled:%s\n",
347 method
, addr_str
, signal
, freq
, ret
? "true" : "false");
349 return ret
? 0 : 17 /* WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA */;
353 usteer_local_node_assoc_update(struct sta_info
*si
, struct blob_attr
*data
)
359 static struct blobmsg_policy policy
[__MSG_MAX
] = {
360 [MSG_ASSOC
] = { "assoc", BLOBMSG_TYPE_BOOL
},
362 struct blob_attr
*tb
[__MSG_MAX
];
363 struct usteer_remote_node
*rn
;
364 struct sta_info
*remote_si
;
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
);
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.
384 si
->connected
= STA_CONNECTED
;
389 usteer_local_node_update_sta_rrm_wnm(struct sta_info
*si
, struct blob_attr
*client_attr
)
391 static const struct blobmsg_policy mbo_policy
= {
393 .type
= BLOBMSG_TYPE_BOOL
,
395 static const struct blobmsg_policy rrm_policy
= {
397 .type
= BLOBMSG_TYPE_ARRAY
,
399 static const struct blobmsg_policy ext_capa_policy
= {
400 .name
= "extended_capabilities",
401 .type
= BLOBMSG_TYPE_ARRAY
,
403 struct blob_attr
*mbo_blob
= NULL
, *rrm_blob
= NULL
, *wnm_blob
= NULL
, *cur
;
408 blobmsg_parse(&mbo_policy
, 1, &mbo_blob
, blobmsg_data(client_attr
), blobmsg_data_len(client_attr
));
411 si
->mbo
= blobmsg_get_u8(mbo_blob
);
416 blobmsg_parse(&rrm_policy
, 1, &rrm_blob
, blobmsg_data(client_attr
), blobmsg_data_len(client_attr
));
420 si
->rrm
= blobmsg_get_u32(blobmsg_data(rrm_blob
));
422 /* Extended Capabilities / WNM */
423 blobmsg_parse(&ext_capa_policy
, 1, &wnm_blob
, blobmsg_data(client_attr
), blobmsg_data_len(client_attr
));
427 blobmsg_for_each_attr(cur
, wnm_blob
, rem
) {
428 if (blobmsg_type(cur
) != BLOBMSG_TYPE_INT32
)
432 if (blobmsg_get_u32(cur
) & (1 << 3))
433 si
->bss_transition
= true;
441 usteer_local_node_set_assoc(struct usteer_local_node
*ln
, struct blob_attr
*cl
)
443 struct usteer_node
*node
= &ln
->node
;
444 struct usteer_node_handler
*h
;
445 struct blob_attr
*cur
;
451 usteer_update_time();
453 list_for_each_entry(si
, &node
->sta_info
, node_list
) {
455 si
->connected
= STA_DISCONNECTED
;
458 blobmsg_for_each_attr(cur
, cl
, rem
) {
459 uint8_t *addr
= (uint8_t *) ether_aton(blobmsg_name(cur
));
465 sta
= usteer_sta_get(addr
, true);
466 si
= usteer_sta_info_get(sta
, node
, &create
);
467 list_for_each_entry(h
, &node_handlers
, list
) {
471 h
->update_sta(node
, si
);
473 usteer_local_node_assoc_update(si
, cur
);
474 if (si
->connected
== STA_CONNECTED
) {
475 si
->last_connected
= current_time
;
479 /* Read RRM information */
480 usteer_local_node_update_sta_rrm_wnm(si
, cur
);
483 node
->n_assoc
= n_assoc
;
485 list_for_each_entry(si
, &node
->sta_info
, node_list
) {
486 if (si
->connected
!= STA_DISCONNECTED
)
489 usteer_sta_disconnected(si
);
490 MSG(VERBOSE
, "station "MAC_ADDR_FMT
" disconnected from node %s\n",
491 MAC_ADDR_DATA(si
->sta
->addr
), usteer_node_name(node
));
496 usteer_local_node_list_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
503 static struct blobmsg_policy policy
[__MSG_MAX
] = {
504 [MSG_FREQ
] = { "freq", BLOBMSG_TYPE_INT32
},
505 [MSG_CLIENTS
] = { "clients", BLOBMSG_TYPE_TABLE
},
507 struct blob_attr
*tb
[__MSG_MAX
];
508 struct usteer_local_node
*ln
;
509 struct usteer_node
*node
;
511 ln
= container_of(req
, struct usteer_local_node
, req
);
514 blobmsg_parse(policy
, __MSG_MAX
, tb
, blob_data(msg
), blob_len(msg
));
515 if (!tb
[MSG_FREQ
] || !tb
[MSG_CLIENTS
])
518 node
->freq
= blobmsg_get_u32(tb
[MSG_FREQ
]);
519 usteer_local_node_set_assoc(ln
, tb
[MSG_CLIENTS
]);
523 usteer_local_node_status_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
532 static struct blobmsg_policy policy
[__MSG_MAX
] = {
533 [MSG_FREQ
] = { "freq", BLOBMSG_TYPE_INT32
},
534 [MSG_CHANNEL
] = { "channel", BLOBMSG_TYPE_INT32
},
535 [MSG_OP_CLASS
] = { "op_class", BLOBMSG_TYPE_INT32
},
536 [MSG_BEACON_INTERVAL
] = { "beacon_interval", BLOBMSG_TYPE_INT32
},
538 struct blob_attr
*tb
[__MSG_MAX
];
539 struct usteer_local_node
*ln
;
540 struct usteer_node
*node
;
542 ln
= container_of(req
, struct usteer_local_node
, req
);
545 blobmsg_parse(policy
, __MSG_MAX
, tb
, blob_data(msg
), blob_len(msg
));
547 node
->freq
= blobmsg_get_u32(tb
[MSG_FREQ
]);
549 node
->channel
= blobmsg_get_u32(tb
[MSG_CHANNEL
]);
550 if (tb
[MSG_OP_CLASS
])
551 node
->op_class
= blobmsg_get_u32(tb
[MSG_OP_CLASS
]);
554 if (tb
[MSG_BEACON_INTERVAL
])
555 ln
->beacon_interval
= blobmsg_get_u32(tb
[MSG_BEACON_INTERVAL
]);
559 usteer_local_node_rrm_nr_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
561 static const struct blobmsg_policy policy
= {
562 "value", BLOBMSG_TYPE_ARRAY
564 struct usteer_local_node
*ln
;
565 struct blob_attr
*tb
;
567 ln
= container_of(req
, struct usteer_local_node
, req
);
569 blobmsg_parse(&policy
, 1, &tb
, blob_data(msg
), blob_len(msg
));
573 usteer_node_set_blob(&ln
->node
.rrm_nr
, tb
);
577 usteer_local_node_req_cb(struct ubus_request
*req
, int ret
)
579 struct usteer_local_node
*ln
;
581 ln
= container_of(req
, struct usteer_local_node
, req
);
582 uloop_timeout_set(&ln
->req_timer
, 1);
586 usteer_add_rrm_data(struct usteer_local_node
*ln
, struct usteer_node
*node
)
588 if (node
== &ln
->node
)
594 /* Remote node only adds same SSID. Required for local-node. */
595 if (strcmp(ln
->node
.ssid
, node
->ssid
) != 0)
598 blobmsg_add_field(&b
, BLOBMSG_TYPE_ARRAY
, "",
599 blobmsg_data(node
->rrm_nr
),
600 blobmsg_data_len(node
->rrm_nr
));
606 usteer_local_node_prepare_rrm_set(struct usteer_local_node
*ln
)
608 struct usteer_node
*node
, *last_remote_neighbor
= NULL
;
612 c
= blobmsg_open_array(&b
, "list");
613 for_each_local_node(node
) {
614 if (i
>= config
.max_neighbor_reports
)
616 if (usteer_add_rrm_data(ln
, node
))
620 while (i
< config
.max_neighbor_reports
) {
621 node
= usteer_node_get_next_neighbor(&ln
->node
, last_remote_neighbor
);
623 /* No more nodes available */
627 last_remote_neighbor
= node
;
628 if (usteer_add_rrm_data(ln
, node
))
632 blobmsg_close_array(&b
, c
);
636 usteer_local_node_state_next(struct uloop_timeout
*timeout
)
638 struct usteer_local_node
*ln
;
640 ln
= container_of(timeout
, struct usteer_local_node
, req_timer
);
643 if (ln
->req_state
>= __REQ_MAX
) {
644 ln
->req_state
= REQ_IDLE
;
648 blob_buf_init(&b
, 0);
649 switch (ln
->req_state
) {
651 ubus_invoke_async(ubus_ctx
, ln
->obj_id
, "get_clients", b
.head
, &ln
->req
);
652 ln
->req
.data_cb
= usteer_local_node_list_cb
;
655 ubus_invoke_async(ubus_ctx
, ln
->obj_id
, "get_status", b
.head
, &ln
->req
);
656 ln
->req
.data_cb
= usteer_local_node_status_cb
;
658 case REQ_RRM_SET_LIST
:
659 usteer_local_node_prepare_rrm_set(ln
);
660 ubus_invoke_async(ubus_ctx
, ln
->obj_id
, "rrm_nr_set", b
.head
, &ln
->req
);
661 ln
->req
.data_cb
= NULL
;
663 case REQ_RRM_GET_OWN
:
664 ubus_invoke_async(ubus_ctx
, ln
->obj_id
, "rrm_nr_get_own", b
.head
, &ln
->req
);
665 ln
->req
.data_cb
= usteer_local_node_rrm_nr_cb
;
670 ln
->req
.complete_cb
= usteer_local_node_req_cb
;
671 ubus_complete_request_async(ubus_ctx
, &ln
->req
);
675 usteer_local_node_request_link_measurement(struct usteer_local_node
*ln
)
677 unsigned int min_count
= DIV_ROUND_UP(config
.link_measurement_interval
, config
.local_sta_update
);
678 struct usteer_node
*node
;
683 if (ln
->link_measurement_tries
< min_count
) {
684 ln
->link_measurement_tries
++;
688 ln
->link_measurement_tries
= 0;
690 if (!config
.link_measurement_interval
)
693 list_for_each_entry(si
, &node
->sta_info
, node_list
) {
694 if (si
->connected
!= STA_CONNECTED
)
697 usteer_ubus_trigger_link_measurement(si
);
702 usteer_local_node_update(struct uloop_timeout
*timeout
)
704 struct usteer_local_node
*ln
;
705 struct usteer_node_handler
*h
;
706 struct usteer_node
*node
;
708 ln
= container_of(timeout
, struct usteer_local_node
, update
);
711 list_for_each_entry(h
, &node_handlers
, list
) {
715 h
->update_node(node
);
718 usteer_local_node_state_reset(ln
);
719 uloop_timeout_set(&ln
->req_timer
, 1);
720 usteer_local_node_kick(ln
);
721 usteer_band_steering_perform_steer(ln
);
722 usteer_local_node_request_link_measurement(ln
);
724 uloop_timeout_set(timeout
, config
.local_sta_update
);
728 usteer_local_node_process_bss_tm_queries(struct uloop_timeout
*timeout
)
730 struct usteer_bss_tm_query
*query
, *tmp
;
731 struct usteer_local_node
*ln
;
732 struct usteer_node
*node
;
735 uint8_t validity_period
;
737 ln
= container_of(timeout
, struct usteer_local_node
, bss_tm_queries_timeout
);
740 validity_period
= 10000 / usteer_local_node_get_beacon_interval(ln
); /* ~ 10 seconds */
742 list_for_each_entry_safe(query
, tmp
, &ln
->bss_tm_queries
, list
) {
743 sta
= usteer_sta_get(query
->sta_addr
, false);
747 si
= usteer_sta_info_get(sta
, node
, false);
751 usteer_ubus_bss_transition_request(si
, query
->dialog_token
, false, false, validity_period
, NULL
);
754 /* Free pending queries we can not handle */
755 usteer_local_node_pending_bss_tm_free(ln
);
758 static struct usteer_local_node
*
759 usteer_get_node(struct ubus_context
*ctx
, const char *name
)
761 struct usteer_local_node
*ln
;
762 struct usteer_node
*node
;
765 ln
= avl_find_element(&local_nodes
, name
, ln
, node
.avl
);
769 ln
= calloc_a(sizeof(*ln
), &str
, strlen(name
) + 1);
771 node
->type
= NODE_TYPE_LOCAL
;
772 node
->created
= current_time
;
773 node
->avl
.key
= strcpy(str
, name
);
774 ln
->ev
.remove_cb
= usteer_handle_remove
;
775 ln
->ev
.cb
= usteer_handle_event
;
776 ln
->update
.cb
= usteer_local_node_update
;
777 ln
->req_timer
.cb
= usteer_local_node_state_next
;
778 ubus_register_subscriber(ctx
, &ln
->ev
);
779 avl_insert(&local_nodes
, &node
->avl
);
780 kvlist_init(&ln
->node_info
, kvlist_blob_len
);
781 INIT_LIST_HEAD(&node
->sta_info
);
782 INIT_LIST_HEAD(&node
->measurements
);
784 ln
->bss_tm_queries_timeout
.cb
= usteer_local_node_process_bss_tm_queries
;
785 INIT_LIST_HEAD(&ln
->bss_tm_queries
);
790 usteer_node_run_update_script(struct usteer_node
*node
)
792 struct usteer_local_node
*ln
= container_of(node
, struct usteer_local_node
, node
);
798 val
= alloca(strlen(node_up_script
) + strlen(ln
->iface
) + 8);
799 sprintf(val
, "%s '%s'", node_up_script
, ln
->iface
);
801 MSG(INFO
, "failed to execute %s\n", val
);
805 usteer_check_node_enabled(struct usteer_local_node
*ln
)
807 bool ssid_disabled
= config
.ssid_list
;
808 struct blob_attr
*cur
;
811 blobmsg_for_each_attr(cur
, config
.ssid_list
, rem
) {
812 if (strcmp(blobmsg_get_string(cur
), ln
->node
.ssid
) != 0)
815 ssid_disabled
= false;
819 if (ln
->node
.disabled
== ssid_disabled
)
822 ln
->node
.disabled
= ssid_disabled
;
825 MSG(INFO
, "Disconnecting from local node %s\n", usteer_node_name(&ln
->node
));
826 usteer_local_node_state_reset(ln
);
827 usteer_sta_node_cleanup(&ln
->node
);
828 usteer_measurement_report_node_cleanup(&ln
->node
);
829 uloop_timeout_cancel(&ln
->update
);
830 ubus_unsubscribe(ubus_ctx
, &ln
->ev
, ln
->obj_id
);
834 MSG(INFO
, "Connecting to local node %s\n", usteer_node_name(&ln
->node
));
835 ubus_subscribe(ubus_ctx
, &ln
->ev
, ln
->obj_id
);
836 uloop_timeout_set(&ln
->update
, 1);
837 usteer_node_run_update_script(&ln
->node
);
841 usteer_register_node(struct ubus_context
*ctx
, const char *name
, uint32_t id
)
843 struct usteer_local_node
*ln
;
844 struct usteer_node_handler
*h
;
846 int offset
= sizeof("hostapd.") - 1;
848 iface
= name
+ offset
;
849 if (strncmp(name
, "hostapd.", iface
- name
) != 0)
852 MSG(INFO
, "Creating local node %s\n", name
);
853 ln
= usteer_get_node(ctx
, name
);
855 ln
->iface
= usteer_node_name(&ln
->node
) + offset
;
856 ln
->ifindex
= if_nametoindex(ln
->iface
);
858 blob_buf_init(&b
, 0);
859 blobmsg_add_u32(&b
, "notify_response", 1);
860 ubus_invoke(ctx
, id
, "notify_response", b
.head
, NULL
, NULL
, 1000);
862 blob_buf_init(&b
, 0);
863 blobmsg_add_u8(&b
, "neighbor_report", 1);
864 blobmsg_add_u8(&b
, "link_measurement", 1);
865 blobmsg_add_u8(&b
, "beacon_report", 1);
866 blobmsg_add_u8(&b
, "bss_transition", 1);
867 ubus_invoke(ctx
, id
, "bss_mgmt_enable", b
.head
, NULL
, NULL
, 1000);
869 list_for_each_entry(h
, &node_handlers
, list
) {
873 h
->init_node(&ln
->node
);
876 ln
->node
.disabled
= true;
877 usteer_check_node_enabled(ln
);
881 usteer_event_handler(struct ubus_context
*ctx
, struct ubus_event_handler
*ev
,
882 const char *type
, struct blob_attr
*msg
)
884 static const struct blobmsg_policy policy
[2] = {
885 { .name
= "id", .type
= BLOBMSG_TYPE_INT32
},
886 { .name
= "path", .type
= BLOBMSG_TYPE_STRING
},
888 struct blob_attr
*tb
[2];
891 blobmsg_parse(policy
, 2, tb
, blob_data(msg
), blob_len(msg
));
893 if (!tb
[0] || !tb
[1])
896 path
= blobmsg_data(tb
[1]);
897 usteer_register_node(ctx
, path
, blobmsg_get_u32(tb
[0]));
901 usteer_register_events(struct ubus_context
*ctx
)
903 static struct ubus_event_handler handler
= {
904 .cb
= usteer_event_handler
907 ubus_register_event_handler(ctx
, &handler
, "ubus.object.add");
911 node_list_cb(struct ubus_context
*ctx
, struct ubus_object_data
*obj
, void *priv
)
913 usteer_register_node(ctx
, obj
->path
, obj
->id
);
917 usteer_local_node_get_beacon_interval(struct usteer_local_node
*ln
)
919 /* Check if beacon-interval is not available (pre-21.02+) */
920 if (ln
->beacon_interval
< 1)
923 return ln
->beacon_interval
;
926 void config_set_node_up_script(struct blob_attr
*data
)
929 struct usteer_node
*node
;
934 val
= blobmsg_get_string(data
);
935 if (node_up_script
&& !strcmp(val
, node_up_script
))
938 free(node_up_script
);
941 node_up_script
= NULL
;
945 node_up_script
= strdup(val
);
947 for_each_local_node(node
)
948 usteer_node_run_update_script(node
);
951 void config_get_node_up_script(struct blob_buf
*buf
)
956 blobmsg_add_string(buf
, "node_up_script", node_up_script
);
959 void config_set_ssid_list(struct blob_attr
*data
)
961 struct usteer_local_node
*ln
;
963 free(config
.ssid_list
);
965 if (data
&& blobmsg_len(data
))
966 config
.ssid_list
= blob_memdup(data
);
968 config
.ssid_list
= NULL
;
970 avl_for_each_element(&local_nodes
, ln
, node
.avl
)
971 usteer_check_node_enabled(ln
);
974 void config_get_ssid_list(struct blob_buf
*buf
)
976 if (config
.ssid_list
)
977 blobmsg_add_blob(buf
, config
.ssid_list
);
981 usteer_local_nodes_init(struct ubus_context
*ctx
)
983 usteer_register_events(ctx
);
984 ubus_lookup(ctx
, "hostapd.*", node_list_cb
, NULL
);