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_free_node(struct ubus_context
*ctx
, struct usteer_local_node
*ln
)
52 struct usteer_node_handler
*h
;
54 list_for_each_entry(h
, &node_handlers
, list
) {
57 h
->free_node(&ln
->node
);
60 usteer_local_node_state_reset(ln
);
61 usteer_sta_node_cleanup(&ln
->node
);
62 uloop_timeout_cancel(&ln
->update
);
63 avl_delete(&local_nodes
, &ln
->node
.avl
);
64 ubus_unregister_subscriber(ctx
, &ln
->ev
);
65 kvlist_free(&ln
->node_info
);
70 usteer_handle_remove(struct ubus_context
*ctx
, struct ubus_subscriber
*s
,
73 struct usteer_local_node
*ln
= container_of(s
, struct usteer_local_node
, ev
);
75 usteer_free_node(ctx
, ln
);
79 usteer_handle_event(struct ubus_context
*ctx
, struct ubus_object
*obj
,
80 struct ubus_request_data
*req
, const char *method
,
81 struct blob_attr
*msg
)
90 struct blobmsg_policy policy
[__EVENT_MAX
] = {
91 [EVENT_ADDR
] = { .name
= "address", .type
= BLOBMSG_TYPE_STRING
},
92 [EVENT_SIGNAL
] = { .name
= "signal", .type
= BLOBMSG_TYPE_INT32
},
93 [EVENT_TARGET
] = { .name
= "target", .type
= BLOBMSG_TYPE_STRING
},
94 [EVENT_FREQ
] = { .name
= "freq", .type
= BLOBMSG_TYPE_INT32
},
96 enum usteer_event_type ev_type
= __EVENT_TYPE_MAX
;
97 struct blob_attr
*tb
[__EVENT_MAX
];
98 struct usteer_local_node
*ln
;
99 struct usteer_node
*node
;
100 int signal
= NO_SIGNAL
;
102 const char *addr_str
;
107 usteer_update_time();
109 for (i
= 0; i
< ARRAY_SIZE(event_types
); i
++) {
110 if (strcmp(method
, event_types
[i
]) != 0)
117 ln
= container_of(obj
, struct usteer_local_node
, ev
.obj
);
119 blobmsg_parse(policy
, __EVENT_MAX
, tb
, blob_data(msg
), blob_len(msg
));
120 if (!tb
[EVENT_ADDR
] || !tb
[EVENT_FREQ
])
121 return UBUS_STATUS_INVALID_ARGUMENT
;
123 if (tb
[EVENT_SIGNAL
])
124 signal
= (int32_t) blobmsg_get_u32(tb
[EVENT_SIGNAL
]);
127 freq
= blobmsg_get_u32(tb
[EVENT_FREQ
]);
129 addr_str
= blobmsg_data(tb
[EVENT_ADDR
]);
130 addr
= (uint8_t *) ether_aton(addr_str
);
132 return UBUS_STATUS_INVALID_ARGUMENT
;
134 ret
= usteer_handle_sta_event(node
, addr
, ev_type
, freq
, signal
);
136 MSG(DEBUG
, "received %s event from %s, signal=%d, freq=%d, handled:%s\n",
137 method
, addr_str
, signal
, freq
, ret
? "true" : "false");
139 return ret
? 0 : 17 /* WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA */;
143 usteer_local_node_assoc_update(struct sta_info
*si
, struct blob_attr
*data
)
149 static struct blobmsg_policy policy
[__MSG_MAX
] = {
150 [MSG_ASSOC
] = { "assoc", BLOBMSG_TYPE_BOOL
},
152 struct blob_attr
*tb
[__MSG_MAX
];
153 struct usteer_remote_node
*rn
;
154 struct sta_info
*remote_si
;
156 blobmsg_parse(policy
, __MSG_MAX
, tb
, blobmsg_data(data
), blobmsg_data_len(data
));
157 if (tb
[MSG_ASSOC
] && blobmsg_get_u8(tb
[MSG_ASSOC
])) {
158 if (si
->connected
== STA_NOT_CONNECTED
) {
159 /* New connection. Check if STA roamed. */
160 for_each_remote_node(rn
) {
161 remote_si
= usteer_sta_info_get(si
->sta
, &rn
->node
, NULL
);
165 if (current_time
- remote_si
->last_connected
< config
.roam_process_timeout
) {
166 rn
->node
.roam_source
++;
167 /* Don't abort looking for roam sources here.
168 * The client might have roamed via another node
169 * within the roam-timeout.
174 si
->connected
= STA_CONNECTED
;
177 if (si
->node
->freq
< 4000)
178 si
->sta
->seen_2ghz
= 1;
180 si
->sta
->seen_5ghz
= 1;
184 usteer_local_node_update_sta_rrm(const uint8_t *addr
, struct blob_attr
*client_attr
)
186 static const struct blobmsg_policy rrm_policy
= {
188 .type
= BLOBMSG_TYPE_ARRAY
,
190 struct blob_attr
*sta_blob
= NULL
;
196 /* Don't create the STA */
197 sta
= usteer_sta_get(addr
, false);
201 blobmsg_parse(&rrm_policy
, 1, &sta_blob
, blobmsg_data(client_attr
), blobmsg_data_len(client_attr
));
205 sta
->rrm
= blobmsg_get_u32(blobmsg_data(sta_blob
));
209 usteer_local_node_set_assoc(struct usteer_local_node
*ln
, struct blob_attr
*cl
)
211 struct usteer_node
*node
= &ln
->node
;
212 struct usteer_node_handler
*h
;
213 struct blob_attr
*cur
;
219 usteer_update_time();
221 list_for_each_entry(si
, &node
->sta_info
, node_list
) {
223 si
->connected
= STA_DISCONNECTED
;
226 blobmsg_for_each_attr(cur
, cl
, rem
) {
227 uint8_t *addr
= (uint8_t *) ether_aton(blobmsg_name(cur
));
233 sta
= usteer_sta_get(addr
, true);
234 si
= usteer_sta_info_get(sta
, node
, &create
);
235 list_for_each_entry(h
, &node_handlers
, list
) {
239 h
->update_sta(node
, si
);
241 usteer_local_node_assoc_update(si
, cur
);
242 if (si
->connected
== STA_CONNECTED
) {
243 si
->last_connected
= current_time
;
247 /* Read RRM information */
248 usteer_local_node_update_sta_rrm(addr
, cur
);
251 node
->n_assoc
= n_assoc
;
253 list_for_each_entry(si
, &node
->sta_info
, node_list
) {
254 if (si
->connected
!= STA_DISCONNECTED
)
257 si
->connected
= STA_NOT_CONNECTED
;
258 usteer_sta_info_update_timeout(si
, config
.local_sta_timeout
);
259 MSG(VERBOSE
, "station "MAC_ADDR_FMT
" disconnected from node %s\n",
260 MAC_ADDR_DATA(si
->sta
->addr
), usteer_node_name(node
));
265 usteer_local_node_list_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
272 static struct blobmsg_policy policy
[__MSG_MAX
] = {
273 [MSG_FREQ
] = { "freq", BLOBMSG_TYPE_INT32
},
274 [MSG_CLIENTS
] = { "clients", BLOBMSG_TYPE_TABLE
},
276 struct blob_attr
*tb
[__MSG_MAX
];
277 struct usteer_local_node
*ln
;
278 struct usteer_node
*node
;
280 ln
= container_of(req
, struct usteer_local_node
, req
);
283 blobmsg_parse(policy
, __MSG_MAX
, tb
, blob_data(msg
), blob_len(msg
));
284 if (!tb
[MSG_FREQ
] || !tb
[MSG_CLIENTS
])
287 node
->freq
= blobmsg_get_u32(tb
[MSG_FREQ
]);
288 usteer_local_node_set_assoc(ln
, tb
[MSG_CLIENTS
]);
292 usteer_local_node_rrm_nr_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
294 static const struct blobmsg_policy policy
= {
295 "value", BLOBMSG_TYPE_ARRAY
297 struct usteer_local_node
*ln
;
298 struct blob_attr
*tb
;
300 ln
= container_of(req
, struct usteer_local_node
, req
);
302 blobmsg_parse(&policy
, 1, &tb
, blob_data(msg
), blob_len(msg
));
306 usteer_node_set_blob(&ln
->node
.rrm_nr
, tb
);
310 usteer_local_node_req_cb(struct ubus_request
*req
, int ret
)
312 struct usteer_local_node
*ln
;
314 ln
= container_of(req
, struct usteer_local_node
, req
);
315 uloop_timeout_set(&ln
->req_timer
, 1);
319 usteer_add_rrm_data(struct usteer_local_node
*ln
, struct usteer_node
*node
)
321 if (node
== &ln
->node
)
327 /* Remote node only adds same SSID. Required for local-node. */
328 if (strcmp(ln
->node
.ssid
, node
->ssid
) != 0)
331 blobmsg_add_field(&b
, BLOBMSG_TYPE_ARRAY
, "",
332 blobmsg_data(node
->rrm_nr
),
333 blobmsg_data_len(node
->rrm_nr
));
339 usteer_local_node_prepare_rrm_set(struct usteer_local_node
*ln
)
341 struct usteer_node
*node
, *last_remote_neighbor
= NULL
;
345 c
= blobmsg_open_array(&b
, "list");
346 for_each_local_node(node
) {
347 if (i
>= config
.max_neighbor_reports
)
349 if (usteer_add_rrm_data(ln
, node
))
353 while (i
< config
.max_neighbor_reports
) {
354 node
= usteer_node_get_next_neighbor(&ln
->node
, last_remote_neighbor
);
356 /* No more nodes available */
360 last_remote_neighbor
= node
;
361 if (usteer_add_rrm_data(ln
, node
))
365 blobmsg_close_array(&b
, c
);
369 usteer_local_node_state_next(struct uloop_timeout
*timeout
)
371 struct usteer_local_node
*ln
;
373 ln
= container_of(timeout
, struct usteer_local_node
, req_timer
);
376 if (ln
->req_state
>= __REQ_MAX
) {
377 ln
->req_state
= REQ_IDLE
;
381 blob_buf_init(&b
, 0);
382 switch (ln
->req_state
) {
384 ubus_invoke_async(ubus_ctx
, ln
->obj_id
, "get_clients", b
.head
, &ln
->req
);
385 ln
->req
.data_cb
= usteer_local_node_list_cb
;
387 case REQ_RRM_SET_LIST
:
388 usteer_local_node_prepare_rrm_set(ln
);
389 ubus_invoke_async(ubus_ctx
, ln
->obj_id
, "rrm_nr_set", b
.head
, &ln
->req
);
390 ln
->req
.data_cb
= NULL
;
392 case REQ_RRM_GET_OWN
:
393 ubus_invoke_async(ubus_ctx
, ln
->obj_id
, "rrm_nr_get_own", b
.head
, &ln
->req
);
394 ln
->req
.data_cb
= usteer_local_node_rrm_nr_cb
;
399 ln
->req
.complete_cb
= usteer_local_node_req_cb
;
400 ubus_complete_request_async(ubus_ctx
, &ln
->req
);
404 usteer_local_node_update(struct uloop_timeout
*timeout
)
406 struct usteer_local_node
*ln
;
407 struct usteer_node_handler
*h
;
408 struct usteer_node
*node
;
410 ln
= container_of(timeout
, struct usteer_local_node
, update
);
413 list_for_each_entry(h
, &node_handlers
, list
) {
417 h
->update_node(node
);
420 usteer_local_node_state_reset(ln
);
421 uloop_timeout_set(&ln
->req_timer
, 1);
422 usteer_local_node_kick(ln
);
423 uloop_timeout_set(timeout
, config
.local_sta_update
);
426 static struct usteer_local_node
*
427 usteer_get_node(struct ubus_context
*ctx
, const char *name
)
429 struct usteer_local_node
*ln
;
430 struct usteer_node
*node
;
433 ln
= avl_find_element(&local_nodes
, name
, ln
, node
.avl
);
437 ln
= calloc_a(sizeof(*ln
), &str
, strlen(name
) + 1);
439 node
->type
= NODE_TYPE_LOCAL
;
440 node
->created
= current_time
;
441 node
->avl
.key
= strcpy(str
, name
);
442 ln
->ev
.remove_cb
= usteer_handle_remove
;
443 ln
->ev
.cb
= usteer_handle_event
;
444 ln
->update
.cb
= usteer_local_node_update
;
445 ln
->req_timer
.cb
= usteer_local_node_state_next
;
446 ubus_register_subscriber(ctx
, &ln
->ev
);
447 avl_insert(&local_nodes
, &node
->avl
);
448 kvlist_init(&ln
->node_info
, kvlist_blob_len
);
449 INIT_LIST_HEAD(&node
->sta_info
);
455 usteer_node_run_update_script(struct usteer_node
*node
)
457 struct usteer_local_node
*ln
= container_of(node
, struct usteer_local_node
, node
);
463 val
= alloca(strlen(node_up_script
) + strlen(ln
->iface
) + 8);
464 sprintf(val
, "%s '%s'", node_up_script
, ln
->iface
);
466 MSG(INFO
, "failed to execute %s\n", val
);
470 usteer_check_node_enabled(struct usteer_local_node
*ln
)
472 bool ssid_disabled
= config
.ssid_list
;
473 struct blob_attr
*cur
;
476 blobmsg_for_each_attr(cur
, config
.ssid_list
, rem
) {
477 if (strcmp(blobmsg_get_string(cur
), ln
->node
.ssid
) != 0)
480 ssid_disabled
= false;
484 if (ln
->node
.disabled
== ssid_disabled
)
487 ln
->node
.disabled
= ssid_disabled
;
490 MSG(INFO
, "Disconnecting from local node %s\n", usteer_node_name(&ln
->node
));
491 usteer_local_node_state_reset(ln
);
492 usteer_sta_node_cleanup(&ln
->node
);
493 uloop_timeout_cancel(&ln
->update
);
494 ubus_unsubscribe(ubus_ctx
, &ln
->ev
, ln
->obj_id
);
498 MSG(INFO
, "Connecting to local node %s\n", usteer_node_name(&ln
->node
));
499 ubus_subscribe(ubus_ctx
, &ln
->ev
, ln
->obj_id
);
500 uloop_timeout_set(&ln
->update
, 1);
501 usteer_node_run_update_script(&ln
->node
);
505 usteer_register_node(struct ubus_context
*ctx
, const char *name
, uint32_t id
)
507 struct usteer_local_node
*ln
;
508 struct usteer_node_handler
*h
;
510 int offset
= sizeof("hostapd.") - 1;
512 iface
= name
+ offset
;
513 if (strncmp(name
, "hostapd.", iface
- name
) != 0)
516 MSG(INFO
, "Creating local node %s\n", name
);
517 ln
= usteer_get_node(ctx
, name
);
519 ln
->iface
= usteer_node_name(&ln
->node
) + offset
;
520 ln
->ifindex
= if_nametoindex(ln
->iface
);
522 blob_buf_init(&b
, 0);
523 blobmsg_add_u32(&b
, "notify_response", 1);
524 ubus_invoke(ctx
, id
, "notify_response", b
.head
, NULL
, NULL
, 1000);
526 blob_buf_init(&b
, 0);
527 blobmsg_add_u8(&b
, "neighbor_report", 1);
528 blobmsg_add_u8(&b
, "beacon_report", 1);
529 blobmsg_add_u8(&b
, "bss_transition", 1);
530 ubus_invoke(ctx
, id
, "bss_mgmt_enable", b
.head
, NULL
, NULL
, 1000);
532 list_for_each_entry(h
, &node_handlers
, list
) {
536 h
->init_node(&ln
->node
);
539 ln
->node
.disabled
= true;
540 usteer_check_node_enabled(ln
);
544 usteer_event_handler(struct ubus_context
*ctx
, struct ubus_event_handler
*ev
,
545 const char *type
, struct blob_attr
*msg
)
547 static const struct blobmsg_policy policy
[2] = {
548 { .name
= "id", .type
= BLOBMSG_TYPE_INT32
},
549 { .name
= "path", .type
= BLOBMSG_TYPE_STRING
},
551 struct blob_attr
*tb
[2];
554 blobmsg_parse(policy
, 2, tb
, blob_data(msg
), blob_len(msg
));
556 if (!tb
[0] || !tb
[1])
559 path
= blobmsg_data(tb
[1]);
560 usteer_register_node(ctx
, path
, blobmsg_get_u32(tb
[0]));
564 usteer_register_events(struct ubus_context
*ctx
)
566 static struct ubus_event_handler handler
= {
567 .cb
= usteer_event_handler
570 ubus_register_event_handler(ctx
, &handler
, "ubus.object.add");
574 node_list_cb(struct ubus_context
*ctx
, struct ubus_object_data
*obj
, void *priv
)
576 usteer_register_node(ctx
, obj
->path
, obj
->id
);
579 void config_set_node_up_script(struct blob_attr
*data
)
582 struct usteer_node
*node
;
587 val
= blobmsg_get_string(data
);
588 if (node_up_script
&& !strcmp(val
, node_up_script
))
591 free(node_up_script
);
594 node_up_script
= NULL
;
598 node_up_script
= strdup(val
);
600 for_each_local_node(node
)
601 usteer_node_run_update_script(node
);
604 void config_get_node_up_script(struct blob_buf
*buf
)
609 blobmsg_add_string(buf
, "node_up_script", node_up_script
);
612 void config_set_ssid_list(struct blob_attr
*data
)
614 struct usteer_local_node
*ln
;
616 free(config
.ssid_list
);
618 if (data
&& blobmsg_len(data
))
619 config
.ssid_list
= blob_memdup(data
);
621 config
.ssid_list
= NULL
;
623 avl_for_each_element(&local_nodes
, ln
, node
.avl
)
624 usteer_check_node_enabled(ln
);
627 void config_get_ssid_list(struct blob_buf
*buf
)
629 if (config
.ssid_list
)
630 blobmsg_add_blob(buf
, config
.ssid_list
);
634 usteer_local_nodes_init(struct ubus_context
*ctx
)
636 usteer_register_events(ctx
);
637 ubus_lookup(ctx
, "hostapd.*", node_list_cb
, NULL
);