2 * hostapd / ubus support
3 * Copyright (c) 2013, Felix Fietkau <nbd@nbd.name>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
10 #include "utils/common.h"
11 #include "utils/eloop.h"
12 #include "utils/wpabuf.h"
13 #include "common/ieee802_11_defs.h"
15 #include "neighbor_db.h"
16 #include "wps_hostapd.h"
19 #include "ap_drv_ops.h"
25 static struct ubus_context
*ctx
;
26 static struct blob_buf b
;
29 static inline struct hapd_interfaces
*get_hapd_interfaces_from_object(struct ubus_object
*obj
)
31 return container_of(obj
, struct hapd_interfaces
, ubus
);
34 static inline struct hostapd_data
*get_hapd_from_object(struct ubus_object
*obj
)
36 return container_of(obj
, struct hostapd_data
, ubus
.obj
);
39 struct ubus_banned_client
{
44 static void ubus_receive(int sock
, void *eloop_ctx
, void *sock_ctx
)
46 struct ubus_context
*ctx
= eloop_ctx
;
47 ubus_handle_event(ctx
);
50 static void ubus_reconnect_timeout(void *eloop_data
, void *user_ctx
)
52 if (ubus_reconnect(ctx
, NULL
)) {
53 eloop_register_timeout(1, 0, ubus_reconnect_timeout
, ctx
, NULL
);
57 eloop_register_read_sock(ctx
->sock
.fd
, ubus_receive
, ctx
, NULL
);
60 static void hostapd_ubus_connection_lost(struct ubus_context
*ctx
)
62 eloop_unregister_read_sock(ctx
->sock
.fd
);
63 eloop_register_timeout(1, 0, ubus_reconnect_timeout
, ctx
, NULL
);
66 static bool hostapd_ubus_init(void)
71 ctx
= ubus_connect(NULL
);
75 ctx
->connection_lost
= hostapd_ubus_connection_lost
;
76 eloop_register_read_sock(ctx
->sock
.fd
, ubus_receive
, ctx
, NULL
);
80 static void hostapd_ubus_ref_inc(void)
85 static void hostapd_ubus_ref_dec(void)
94 eloop_unregister_read_sock(ctx
->sock
.fd
);
99 void hostapd_ubus_add_iface(struct hostapd_iface
*iface
)
101 if (!hostapd_ubus_init())
105 void hostapd_ubus_free_iface(struct hostapd_iface
*iface
)
112 hostapd_bss_del_ban(void *eloop_data
, void *user_ctx
)
114 struct ubus_banned_client
*ban
= eloop_data
;
115 struct hostapd_data
*hapd
= user_ctx
;
117 avl_delete(&hapd
->ubus
.banned
, &ban
->avl
);
122 hostapd_bss_ban_client(struct hostapd_data
*hapd
, u8
*addr
, int time
)
124 struct ubus_banned_client
*ban
;
129 ban
= avl_find_element(&hapd
->ubus
.banned
, addr
, ban
, avl
);
134 ban
= os_zalloc(sizeof(*ban
));
135 memcpy(ban
->addr
, addr
, sizeof(ban
->addr
));
136 ban
->avl
.key
= ban
->addr
;
137 avl_insert(&hapd
->ubus
.banned
, &ban
->avl
);
139 eloop_cancel_timeout(hostapd_bss_del_ban
, ban
, hapd
);
141 hostapd_bss_del_ban(ban
, hapd
);
146 eloop_register_timeout(0, time
* 1000, hostapd_bss_del_ban
, ban
, hapd
);
150 hostapd_bss_reload(struct ubus_context
*ctx
, struct ubus_object
*obj
,
151 struct ubus_request_data
*req
, const char *method
,
152 struct blob_attr
*msg
)
154 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
155 hostapd_reload_config(hapd
->iface
);
156 hostapd_reload_iface(hapd
->iface
);
160 hostapd_bss_get_clients(struct ubus_context
*ctx
, struct ubus_object
*obj
,
161 struct ubus_request_data
*req
, const char *method
,
162 struct blob_attr
*msg
)
164 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
165 struct sta_info
*sta
;
168 static const struct {
172 { "auth", WLAN_STA_AUTH
},
173 { "assoc", WLAN_STA_ASSOC
},
174 { "authorized", WLAN_STA_AUTHORIZED
},
175 { "preauth", WLAN_STA_PREAUTH
},
176 { "wds", WLAN_STA_WDS
},
177 { "wmm", WLAN_STA_WMM
},
178 { "ht", WLAN_STA_HT
},
179 { "vht", WLAN_STA_VHT
},
180 { "wps", WLAN_STA_WPS
},
181 { "mfp", WLAN_STA_MFP
},
184 blob_buf_init(&b
, 0);
185 blobmsg_add_u32(&b
, "freq", hapd
->iface
->freq
);
186 list
= blobmsg_open_table(&b
, "clients");
187 for (sta
= hapd
->sta_list
; sta
; sta
= sta
->next
) {
191 sprintf(mac_buf
, MACSTR
, MAC2STR(sta
->addr
));
192 c
= blobmsg_open_table(&b
, mac_buf
);
193 for (i
= 0; i
< ARRAY_SIZE(sta_flags
); i
++)
194 blobmsg_add_u8(&b
, sta_flags
[i
].name
,
195 !!(sta
->flags
& sta_flags
[i
].flag
));
197 r
= blobmsg_open_array(&b
, "rrm");
198 for (i
= 0; i
< ARRAY_SIZE(sta
->rrm_enabled_capa
); i
++)
199 blobmsg_add_u32(&b
, "", sta
->rrm_enabled_capa
[i
]);
200 blobmsg_close_array(&b
, r
);
201 blobmsg_add_u32(&b
, "aid", sta
->aid
);
202 #ifdef CONFIG_TAXONOMY
203 r
= blobmsg_alloc_string_buffer(&b
, "signature", 1024);
204 if (retrieve_sta_taxonomy(hapd
, sta
, r
, 1024) > 0)
205 blobmsg_add_string_buffer(&b
);
207 blobmsg_close_table(&b
, c
);
209 blobmsg_close_array(&b
, list
);
210 ubus_send_reply(ctx
, req
, b
.head
);
216 hostapd_bss_get_features(struct ubus_context
*ctx
, struct ubus_object
*obj
,
217 struct ubus_request_data
*req
, const char *method
,
218 struct blob_attr
*msg
)
220 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
222 blob_buf_init(&b
, 0);
223 blobmsg_add_u8(&b
, "ht_supported", ht_supported(hapd
->iface
->hw_features
));
224 blobmsg_add_u8(&b
, "vht_supported", vht_supported(hapd
->iface
->hw_features
));
225 ubus_send_reply(ctx
, req
, b
.head
);
235 static const struct blobmsg_policy notify_policy
[__NOTIFY_MAX
] = {
236 [NOTIFY_RESPONSE
] = { "notify_response", BLOBMSG_TYPE_INT32
},
240 hostapd_notify_response(struct ubus_context
*ctx
, struct ubus_object
*obj
,
241 struct ubus_request_data
*req
, const char *method
,
242 struct blob_attr
*msg
)
244 struct blob_attr
*tb
[__NOTIFY_MAX
];
245 struct hostapd_data
*hapd
= get_hapd_from_object(obj
);
246 struct wpabuf
*elems
;
250 blobmsg_parse(notify_policy
, __NOTIFY_MAX
, tb
,
251 blob_data(msg
), blob_len(msg
));
253 if (!tb
[NOTIFY_RESPONSE
])
254 return UBUS_STATUS_INVALID_ARGUMENT
;
256 hapd
->ubus
.notify_response
= blobmsg_get_u32(tb
[NOTIFY_RESPONSE
]);
258 return UBUS_STATUS_OK
;
269 static const struct blobmsg_policy del_policy
[__DEL_CLIENT_MAX
] = {
270 [DEL_CLIENT_ADDR
] = { "addr", BLOBMSG_TYPE_STRING
},
271 [DEL_CLIENT_REASON
] = { "reason", BLOBMSG_TYPE_INT32
},
272 [DEL_CLIENT_DEAUTH
] = { "deauth", BLOBMSG_TYPE_INT8
},
273 [DEL_CLIENT_BAN_TIME
] = { "ban_time", BLOBMSG_TYPE_INT32
},
277 hostapd_bss_del_client(struct ubus_context
*ctx
, struct ubus_object
*obj
,
278 struct ubus_request_data
*req
, const char *method
,
279 struct blob_attr
*msg
)
281 struct blob_attr
*tb
[__DEL_CLIENT_MAX
];
282 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
283 struct sta_info
*sta
;
288 blobmsg_parse(del_policy
, __DEL_CLIENT_MAX
, tb
, blob_data(msg
), blob_len(msg
));
290 if (!tb
[DEL_CLIENT_ADDR
])
291 return UBUS_STATUS_INVALID_ARGUMENT
;
293 if (hwaddr_aton(blobmsg_data(tb
[DEL_CLIENT_ADDR
]), addr
))
294 return UBUS_STATUS_INVALID_ARGUMENT
;
296 if (tb
[DEL_CLIENT_REASON
])
297 reason
= blobmsg_get_u32(tb
[DEL_CLIENT_REASON
]);
299 if (tb
[DEL_CLIENT_DEAUTH
])
300 deauth
= blobmsg_get_bool(tb
[DEL_CLIENT_DEAUTH
]);
302 sta
= ap_get_sta(hapd
, addr
);
305 hostapd_drv_sta_deauth(hapd
, addr
, reason
);
306 ap_sta_deauthenticate(hapd
, sta
, reason
);
308 hostapd_drv_sta_disassoc(hapd
, addr
, reason
);
309 ap_sta_disassociate(hapd
, sta
, reason
);
313 if (tb
[DEL_CLIENT_BAN_TIME
])
314 hostapd_bss_ban_client(hapd
, addr
, blobmsg_get_u32(tb
[DEL_CLIENT_BAN_TIME
]));
320 blobmsg_add_macaddr(struct blob_buf
*buf
, const char *name
, const u8
*addr
)
324 s
= blobmsg_alloc_string_buffer(buf
, name
, 20);
325 sprintf(s
, MACSTR
, MAC2STR(addr
));
326 blobmsg_add_string_buffer(buf
);
330 hostapd_bss_list_bans(struct ubus_context
*ctx
, struct ubus_object
*obj
,
331 struct ubus_request_data
*req
, const char *method
,
332 struct blob_attr
*msg
)
334 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
335 struct ubus_banned_client
*ban
;
338 blob_buf_init(&b
, 0);
339 c
= blobmsg_open_array(&b
, "clients");
340 avl_for_each_element(&hapd
->ubus
.banned
, ban
, avl
)
341 blobmsg_add_macaddr(&b
, NULL
, ban
->addr
);
342 blobmsg_close_array(&b
, c
);
343 ubus_send_reply(ctx
, req
, b
.head
);
349 hostapd_bss_wps_start(struct ubus_context
*ctx
, struct ubus_object
*obj
,
350 struct ubus_request_data
*req
, const char *method
,
351 struct blob_attr
*msg
)
354 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
356 rc
= hostapd_wps_button_pushed(hapd
, NULL
);
359 return UBUS_STATUS_NOT_SUPPORTED
;
365 hostapd_bss_wps_cancel(struct ubus_context
*ctx
, struct ubus_object
*obj
,
366 struct ubus_request_data
*req
, const char *method
,
367 struct blob_attr
*msg
)
370 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
372 rc
= hostapd_wps_cancel(hapd
);
375 return UBUS_STATUS_NOT_SUPPORTED
;
381 hostapd_bss_update_beacon(struct ubus_context
*ctx
, struct ubus_object
*obj
,
382 struct ubus_request_data
*req
, const char *method
,
383 struct blob_attr
*msg
)
386 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
388 rc
= ieee802_11_set_beacon(hapd
);
391 return UBUS_STATUS_NOT_SUPPORTED
;
402 static const struct blobmsg_policy config_add_policy
[__CONFIG_MAX
] = {
403 [CONFIG_IFACE
] = { "iface", BLOBMSG_TYPE_STRING
},
404 [CONFIG_FILE
] = { "config", BLOBMSG_TYPE_STRING
},
408 hostapd_config_add(struct ubus_context
*ctx
, struct ubus_object
*obj
,
409 struct ubus_request_data
*req
, const char *method
,
410 struct blob_attr
*msg
)
412 struct blob_attr
*tb
[__CONFIG_MAX
];
413 struct hapd_interfaces
*interfaces
= get_hapd_interfaces_from_object(obj
);
416 blobmsg_parse(config_add_policy
, __CONFIG_MAX
, tb
, blob_data(msg
), blob_len(msg
));
418 if (!tb
[CONFIG_FILE
] || !tb
[CONFIG_IFACE
])
419 return UBUS_STATUS_INVALID_ARGUMENT
;
421 snprintf(buf
, sizeof(buf
), "bss_config=%s:%s",
422 blobmsg_get_string(tb
[CONFIG_IFACE
]),
423 blobmsg_get_string(tb
[CONFIG_FILE
]));
425 if (hostapd_add_iface(interfaces
, buf
))
426 return UBUS_STATUS_INVALID_ARGUMENT
;
428 return UBUS_STATUS_OK
;
436 static const struct blobmsg_policy config_remove_policy
[__CONFIG_REM_MAX
] = {
437 [CONFIG_REM_IFACE
] = { "iface", BLOBMSG_TYPE_STRING
},
441 hostapd_config_remove(struct ubus_context
*ctx
, struct ubus_object
*obj
,
442 struct ubus_request_data
*req
, const char *method
,
443 struct blob_attr
*msg
)
445 struct blob_attr
*tb
[__CONFIG_REM_MAX
];
446 struct hapd_interfaces
*interfaces
= get_hapd_interfaces_from_object(obj
);
449 blobmsg_parse(config_remove_policy
, __CONFIG_REM_MAX
, tb
, blob_data(msg
), blob_len(msg
));
451 if (!tb
[CONFIG_REM_IFACE
])
452 return UBUS_STATUS_INVALID_ARGUMENT
;
454 if (hostapd_remove_iface(interfaces
, blobmsg_get_string(tb
[CONFIG_REM_IFACE
])))
455 return UBUS_STATUS_INVALID_ARGUMENT
;
457 return UBUS_STATUS_OK
;
466 CSA_SEC_CHANNEL_OFFSET
,
473 static const struct blobmsg_policy csa_policy
[__CSA_MAX
] = {
474 [CSA_FREQ
] = { "freq", BLOBMSG_TYPE_INT32
},
475 [CSA_BCN_COUNT
] = { "bcn_count", BLOBMSG_TYPE_INT32
},
476 [CSA_CENTER_FREQ1
] = { "center_freq1", BLOBMSG_TYPE_INT32
},
477 [CSA_CENTER_FREQ2
] = { "center_freq2", BLOBMSG_TYPE_INT32
},
478 [CSA_BANDWIDTH
] = { "bandwidth", BLOBMSG_TYPE_INT32
},
479 [CSA_SEC_CHANNEL_OFFSET
] = { "sec_channel_offset", BLOBMSG_TYPE_INT32
},
480 [CSA_HT
] = { "ht", BLOBMSG_TYPE_BOOL
},
481 [CSA_VHT
] = { "vht", BLOBMSG_TYPE_BOOL
},
482 [CSA_BLOCK_TX
] = { "block_tx", BLOBMSG_TYPE_BOOL
},
487 hostapd_switch_chan(struct ubus_context
*ctx
, struct ubus_object
*obj
,
488 struct ubus_request_data
*req
, const char *method
,
489 struct blob_attr
*msg
)
491 struct blob_attr
*tb
[__CSA_MAX
];
492 struct hostapd_data
*hapd
= get_hapd_from_object(obj
);
493 struct csa_settings css
;
495 blobmsg_parse(csa_policy
, __CSA_MAX
, tb
, blob_data(msg
), blob_len(msg
));
498 return UBUS_STATUS_INVALID_ARGUMENT
;
500 memset(&css
, 0, sizeof(css
));
501 css
.freq_params
.freq
= blobmsg_get_u32(tb
[CSA_FREQ
]);
503 #define SET_CSA_SETTING(name, field, type) \
506 css.field = blobmsg_get_ ## type(tb[name]); \
509 SET_CSA_SETTING(CSA_BCN_COUNT
, cs_count
, u32
);
510 SET_CSA_SETTING(CSA_CENTER_FREQ1
, freq_params
.center_freq1
, u32
);
511 SET_CSA_SETTING(CSA_CENTER_FREQ2
, freq_params
.center_freq2
, u32
);
512 SET_CSA_SETTING(CSA_BANDWIDTH
, freq_params
.bandwidth
, u32
);
513 SET_CSA_SETTING(CSA_SEC_CHANNEL_OFFSET
, freq_params
.sec_channel_offset
, u32
);
514 SET_CSA_SETTING(CSA_HT
, freq_params
.ht_enabled
, bool);
515 SET_CSA_SETTING(CSA_VHT
, freq_params
.vht_enabled
, bool);
516 SET_CSA_SETTING(CSA_BLOCK_TX
, block_tx
, bool);
519 if (hostapd_switch_channel(hapd
, &css
) != 0)
520 return UBUS_STATUS_NOT_SUPPORTED
;
521 return UBUS_STATUS_OK
;
522 #undef SET_CSA_SETTING
528 __VENDOR_ELEMENTS_MAX
531 static const struct blobmsg_policy ve_policy
[__VENDOR_ELEMENTS_MAX
] = {
532 /* vendor elements are provided as hex-string */
533 [VENDOR_ELEMENTS
] = { "vendor_elements", BLOBMSG_TYPE_STRING
},
537 hostapd_vendor_elements(struct ubus_context
*ctx
, struct ubus_object
*obj
,
538 struct ubus_request_data
*req
, const char *method
,
539 struct blob_attr
*msg
)
541 struct blob_attr
*tb
[__VENDOR_ELEMENTS_MAX
];
542 struct hostapd_data
*hapd
= get_hapd_from_object(obj
);
543 struct hostapd_bss_config
*bss
= hapd
->conf
;
544 struct wpabuf
*elems
;
548 blobmsg_parse(ve_policy
, __VENDOR_ELEMENTS_MAX
, tb
,
549 blob_data(msg
), blob_len(msg
));
551 if (!tb
[VENDOR_ELEMENTS
])
552 return UBUS_STATUS_INVALID_ARGUMENT
;
554 pos
= blobmsg_data(tb
[VENDOR_ELEMENTS
]);
555 len
= os_strlen(pos
);
557 return UBUS_STATUS_INVALID_ARGUMENT
;
561 wpabuf_free(bss
->vendor_elements
);
562 bss
->vendor_elements
= NULL
;
566 elems
= wpabuf_alloc(len
);
570 if (hexstr2bin(pos
, wpabuf_put(elems
, len
), len
)) {
572 return UBUS_STATUS_INVALID_ARGUMENT
;
575 wpabuf_free(bss
->vendor_elements
);
576 bss
->vendor_elements
= elems
;
578 /* update beacons if vendor elements were set successfully */
579 if (ieee802_11_update_beacons(hapd
->iface
) != 0)
580 return UBUS_STATUS_NOT_SUPPORTED
;
581 return UBUS_STATUS_OK
;
585 hostapd_rrm_print_nr(struct hostapd_neighbor_entry
*nr
)
591 blobmsg_printf(&b
, "", MACSTR
, MAC2STR(nr
->bssid
));
593 str
= blobmsg_alloc_string_buffer(&b
, "", nr
->ssid
.ssid_len
+ 1);
594 memcpy(str
, nr
->ssid
.ssid
, nr
->ssid
.ssid_len
);
595 str
[nr
->ssid
.ssid_len
] = 0;
596 blobmsg_add_string_buffer(&b
);
598 len
= wpabuf_len(nr
->nr
);
599 str
= blobmsg_alloc_string_buffer(&b
, "", 2 * len
+ 1);
600 wpa_snprintf_hex(str
, 2 * len
+ 1, wpabuf_head_u8(nr
->nr
), len
);
601 blobmsg_add_string_buffer(&b
);
605 BSS_MGMT_EN_NEIGHBOR
,
608 BSS_MGMT_EN_BSS_TRANSITION
,
614 __hostapd_bss_mgmt_enable_f(struct hostapd_data
*hapd
, int flag
)
616 struct hostapd_bss_config
*bss
= hapd
->conf
;
620 case BSS_MGMT_EN_NEIGHBOR
:
621 if (bss
->radio_measurements
[0] &
622 WLAN_RRM_CAPS_NEIGHBOR_REPORT
)
625 bss
->radio_measurements
[0] |=
626 WLAN_RRM_CAPS_NEIGHBOR_REPORT
;
627 hostapd_neighbor_set_own_report(hapd
);
629 case BSS_MGMT_EN_BEACON
:
630 flags
= WLAN_RRM_CAPS_BEACON_REPORT_PASSIVE
|
631 WLAN_RRM_CAPS_BEACON_REPORT_ACTIVE
|
632 WLAN_RRM_CAPS_BEACON_REPORT_TABLE
;
634 if (bss
->radio_measurements
[0] & flags
== flags
)
637 bss
->radio_measurements
[0] |= (u8
) flags
;
640 case BSS_MGMT_EN_BSS_TRANSITION
:
641 if (bss
->bss_transition
)
644 bss
->bss_transition
= 1;
651 __hostapd_bss_mgmt_enable(struct hostapd_data
*hapd
, uint32_t flags
)
656 for (i
= 0; i
< __BSS_MGMT_EN_MAX
; i
++) {
657 if (!(flags
& (1 << i
)))
660 update
|= __hostapd_bss_mgmt_enable_f(hapd
, i
);
664 ieee802_11_update_beacons(hapd
->iface
);
668 static const struct blobmsg_policy bss_mgmt_enable_policy
[__BSS_MGMT_EN_MAX
] = {
669 [BSS_MGMT_EN_NEIGHBOR
] = { "neighbor_report", BLOBMSG_TYPE_BOOL
},
670 [BSS_MGMT_EN_BEACON
] = { "beacon_report", BLOBMSG_TYPE_BOOL
},
672 [BSS_MGMT_EN_BSS_TRANSITION
] = { "bss_transition", BLOBMSG_TYPE_BOOL
},
677 hostapd_bss_mgmt_enable(struct ubus_context
*ctx
, struct ubus_object
*obj
,
678 struct ubus_request_data
*req
, const char *method
,
679 struct blob_attr
*msg
)
682 struct hostapd_data
*hapd
= get_hapd_from_object(obj
);
683 struct blob_attr
*tb
[__BSS_MGMT_EN_MAX
];
684 struct blob_attr
*cur
;
687 bool neigh
= false, beacon
= false;
689 blobmsg_parse(bss_mgmt_enable_policy
, __BSS_MGMT_EN_MAX
, tb
, blob_data(msg
), blob_len(msg
));
691 for (i
= 0; i
< ARRAY_SIZE(tb
); i
++) {
692 if (!tb
[i
] || !blobmsg_get_bool(tb
[i
]))
698 __hostapd_bss_mgmt_enable(hapd
, flags
);
703 hostapd_rrm_nr_enable(struct hostapd_data
*hapd
)
705 __hostapd_bss_mgmt_enable(hapd
, 1 << BSS_MGMT_EN_NEIGHBOR
);
709 hostapd_rrm_nr_get_own(struct ubus_context
*ctx
, struct ubus_object
*obj
,
710 struct ubus_request_data
*req
, const char *method
,
711 struct blob_attr
*msg
)
713 struct hostapd_data
*hapd
= get_hapd_from_object(obj
);
714 struct hostapd_neighbor_entry
*nr
;
717 hostapd_rrm_nr_enable(hapd
);
719 nr
= hostapd_neighbor_get(hapd
, hapd
->own_addr
, NULL
);
721 return UBUS_STATUS_NOT_FOUND
;
723 blob_buf_init(&b
, 0);
725 c
= blobmsg_open_array(&b
, "value");
726 hostapd_rrm_print_nr(nr
);
727 blobmsg_close_array(&b
, c
);
729 ubus_send_reply(ctx
, req
, b
.head
);
735 hostapd_rrm_nr_list(struct ubus_context
*ctx
, struct ubus_object
*obj
,
736 struct ubus_request_data
*req
, const char *method
,
737 struct blob_attr
*msg
)
739 struct hostapd_data
*hapd
= get_hapd_from_object(obj
);
740 struct hostapd_neighbor_entry
*nr
;
743 hostapd_rrm_nr_enable(hapd
);
744 blob_buf_init(&b
, 0);
746 c
= blobmsg_open_array(&b
, "list");
747 dl_list_for_each(nr
, &hapd
->nr_db
, struct hostapd_neighbor_entry
, list
) {
750 if (!memcmp(nr
->bssid
, hapd
->own_addr
, ETH_ALEN
))
753 cur
= blobmsg_open_array(&b
, NULL
);
754 hostapd_rrm_print_nr(nr
);
755 blobmsg_close_array(&b
, cur
);
757 blobmsg_close_array(&b
, c
);
759 ubus_send_reply(ctx
, req
, b
.head
);
769 static const struct blobmsg_policy nr_set_policy
[__NR_SET_LIST_MAX
] = {
770 [NR_SET_LIST
] = { "list", BLOBMSG_TYPE_ARRAY
},
775 hostapd_rrm_nr_clear(struct hostapd_data
*hapd
)
777 struct hostapd_neighbor_entry
*nr
;
780 dl_list_for_each(nr
, &hapd
->nr_db
, struct hostapd_neighbor_entry
, list
) {
781 if (!memcmp(nr
->bssid
, hapd
->own_addr
, ETH_ALEN
))
784 hostapd_neighbor_remove(hapd
, nr
->bssid
, &nr
->ssid
);
790 hostapd_rrm_nr_set(struct ubus_context
*ctx
, struct ubus_object
*obj
,
791 struct ubus_request_data
*req
, const char *method
,
792 struct blob_attr
*msg
)
794 static const struct blobmsg_policy nr_e_policy
[] = {
795 { .type
= BLOBMSG_TYPE_STRING
},
796 { .type
= BLOBMSG_TYPE_STRING
},
797 { .type
= BLOBMSG_TYPE_STRING
},
799 struct hostapd_data
*hapd
= get_hapd_from_object(obj
);
800 struct blob_attr
*tb_l
[__NR_SET_LIST_MAX
];
801 struct blob_attr
*tb
[ARRAY_SIZE(nr_e_policy
)];
802 struct blob_attr
*cur
;
806 hostapd_rrm_nr_enable(hapd
);
808 blobmsg_parse(nr_set_policy
, __NR_SET_LIST_MAX
, tb_l
, blob_data(msg
), blob_len(msg
));
809 if (!tb_l
[NR_SET_LIST
])
810 return UBUS_STATUS_INVALID_ARGUMENT
;
812 hostapd_rrm_nr_clear(hapd
);
813 blobmsg_for_each_attr(cur
, tb_l
[NR_SET_LIST
], rem
) {
814 struct wpa_ssid_value ssid
;
819 blobmsg_parse_array(nr_e_policy
, ARRAY_SIZE(nr_e_policy
), tb
, blobmsg_data(cur
), blobmsg_data_len(cur
));
820 if (!tb
[0] || !tb
[1] || !tb
[2])
823 s
= blobmsg_get_string(tb
[0]);
824 if (hwaddr_aton(s
, bssid
))
827 s
= blobmsg_get_string(tb
[1]);
828 ssid
.ssid_len
= strlen(s
);
829 if (ssid
.ssid_len
> sizeof(ssid
.ssid
))
832 memcpy(&ssid
, s
, ssid
.ssid_len
);
833 data
= wpabuf_parse_bin(blobmsg_get_string(tb
[2]));
837 hostapd_neighbor_set(hapd
, bssid
, &ssid
, data
, NULL
, NULL
, 0);
842 ret
= UBUS_STATUS_INVALID_ARGUMENT
;
859 static const struct blobmsg_policy beacon_req_policy
[__BEACON_REQ_MAX
] = {
860 [BEACON_REQ_ADDR
] = { "addr", BLOBMSG_TYPE_STRING
},
861 [BEACON_REQ_OP_CLASS
] { "op_class", BLOBMSG_TYPE_INT32
},
862 [BEACON_REQ_CHANNEL
] { "channel", BLOBMSG_TYPE_INT32
},
863 [BEACON_REQ_DURATION
] { "duration", BLOBMSG_TYPE_INT32
},
864 [BEACON_REQ_MODE
] { "mode", BLOBMSG_TYPE_INT32
},
865 [BEACON_REQ_BSSID
] { "bssid", BLOBMSG_TYPE_STRING
},
866 [BEACON_REQ_SSID
] { "ssid", BLOBMSG_TYPE_STRING
},
870 hostapd_rrm_beacon_req(struct ubus_context
*ctx
, struct ubus_object
*obj
,
871 struct ubus_request_data
*ureq
, const char *method
,
872 struct blob_attr
*msg
)
874 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
875 struct blob_attr
*tb
[__BEACON_REQ_MAX
];
876 struct blob_attr
*cur
;
878 u8 bssid
[ETH_ALEN
] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
883 blobmsg_parse(beacon_req_policy
, __BEACON_REQ_MAX
, tb
, blob_data(msg
), blob_len(msg
));
885 if (!tb
[BEACON_REQ_ADDR
] || !tb
[BEACON_REQ_MODE
] || !tb
[BEACON_REQ_DURATION
] ||
886 !tb
[BEACON_REQ_OP_CLASS
] || !tb
[BEACON_REQ_CHANNEL
])
887 return UBUS_STATUS_INVALID_ARGUMENT
;
889 if (tb
[BEACON_REQ_SSID
])
890 buf_len
+= blobmsg_data_len(tb
[BEACON_REQ_SSID
]) + 2 - 1;
892 mode
= blobmsg_get_u32(tb
[BEACON_REQ_MODE
]);
893 if (hwaddr_aton(blobmsg_data(tb
[BEACON_REQ_ADDR
]), addr
))
894 return UBUS_STATUS_INVALID_ARGUMENT
;
896 if (tb
[BEACON_REQ_BSSID
] &&
897 hwaddr_aton(blobmsg_data(tb
[BEACON_REQ_BSSID
]), bssid
))
898 return UBUS_STATUS_INVALID_ARGUMENT
;
900 req
= wpabuf_alloc(buf_len
);
902 return UBUS_STATUS_UNKNOWN_ERROR
;
904 /* 1: regulatory class */
905 wpabuf_put_u8(req
, blobmsg_get_u32(tb
[BEACON_REQ_OP_CLASS
]));
907 /* 2: channel number */
908 wpabuf_put_u8(req
, blobmsg_get_u32(tb
[BEACON_REQ_CHANNEL
]));
910 /* 3-4: randomization interval */
911 wpabuf_put_le16(req
, 0);
914 wpabuf_put_le16(req
, blobmsg_get_u32(tb
[BEACON_REQ_DURATION
]));
917 wpabuf_put_u8(req
, blobmsg_get_u32(tb
[BEACON_REQ_MODE
]));
920 wpabuf_put_data(req
, bssid
, ETH_ALEN
);
922 if ((cur
= tb
[BEACON_REQ_SSID
]) != NULL
) {
923 wpabuf_put_u8(req
, WLAN_EID_SSID
);
924 wpabuf_put_u8(req
, blobmsg_data_len(cur
) - 1);
925 wpabuf_put_data(req
, blobmsg_data(cur
), blobmsg_data_len(cur
) - 1);
928 ret
= hostapd_send_beacon_req(hapd
, addr
, 0, req
);
939 WNM_DISASSOC_DURATION
,
940 WNM_DISASSOC_NEIGHBORS
,
944 static const struct blobmsg_policy wnm_disassoc_policy
[__WNM_DISASSOC_MAX
] = {
945 [WNM_DISASSOC_ADDR
] = { "addr", BLOBMSG_TYPE_STRING
},
946 [WNM_DISASSOC_DURATION
] { "duration", BLOBMSG_TYPE_INT32
},
947 [WNM_DISASSOC_NEIGHBORS
] { "neighbors", BLOBMSG_TYPE_ARRAY
},
951 hostapd_wnm_disassoc_imminent(struct ubus_context
*ctx
, struct ubus_object
*obj
,
952 struct ubus_request_data
*ureq
, const char *method
,
953 struct blob_attr
*msg
)
955 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
956 struct blob_attr
*tb
[__WNM_DISASSOC_MAX
];
957 struct blob_attr
*cur
;
958 struct sta_info
*sta
;
963 u8 req_mode
= WNM_BSS_TM_REQ_DISASSOC_IMMINENT
;
966 blobmsg_parse(wnm_disassoc_policy
, __WNM_DISASSOC_MAX
, tb
, blob_data(msg
), blob_len(msg
));
968 if (!tb
[WNM_DISASSOC_ADDR
])
969 return UBUS_STATUS_INVALID_ARGUMENT
;
971 if (hwaddr_aton(blobmsg_data(tb
[WNM_DISASSOC_ADDR
]), addr
))
972 return UBUS_STATUS_INVALID_ARGUMENT
;
974 if ((cur
= tb
[WNM_DISASSOC_DURATION
]) != NULL
)
975 duration
= blobmsg_get_u32(cur
);
977 sta
= ap_get_sta(hapd
, addr
);
979 return UBUS_STATUS_NOT_FOUND
;
981 if (tb
[WNM_DISASSOC_NEIGHBORS
]) {
984 if (blobmsg_check_array(tb
[WNM_DISASSOC_NEIGHBORS
],
985 BLOBMSG_TYPE_STRING
) < 0)
986 return UBUS_STATUS_INVALID_ARGUMENT
;
988 blobmsg_for_each_attr(cur
, tb
[WNM_DISASSOC_NEIGHBORS
], rem
) {
989 int len
= strlen(blobmsg_get_string(cur
));
992 return UBUS_STATUS_INVALID_ARGUMENT
;
994 nr_len
+= (len
/ 2) + 2;
998 nr
= os_zalloc(nr_len
);
1000 return UBUS_STATUS_UNKNOWN_ERROR
;
1004 blobmsg_for_each_attr(cur
, tb
[WNM_DISASSOC_NEIGHBORS
], rem
) {
1005 int len
= strlen(blobmsg_get_string(cur
)) / 2;
1007 *nr_cur
++ = WLAN_EID_NEIGHBOR_REPORT
;
1008 *nr_cur
++ = (u8
) len
;
1009 if (hexstr2bin(blobmsg_data(cur
), nr_cur
, len
)) {
1011 return UBUS_STATUS_INVALID_ARGUMENT
;
1019 req_mode
|= WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED
;
1021 if (wnm_send_bss_tm_req(hapd
, sta
, req_mode
, duration
, 0, NULL
,
1022 NULL
, nr
, nr_len
, NULL
, 0))
1023 return UBUS_STATUS_UNKNOWN_ERROR
;
1029 static const struct ubus_method bss_methods
[] = {
1030 UBUS_METHOD_NOARG("reload", hostapd_bss_reload
),
1031 UBUS_METHOD_NOARG("get_clients", hostapd_bss_get_clients
),
1032 UBUS_METHOD("del_client", hostapd_bss_del_client
, del_policy
),
1033 UBUS_METHOD_NOARG("list_bans", hostapd_bss_list_bans
),
1034 UBUS_METHOD_NOARG("wps_start", hostapd_bss_wps_start
),
1035 UBUS_METHOD_NOARG("wps_cancel", hostapd_bss_wps_cancel
),
1036 UBUS_METHOD_NOARG("update_beacon", hostapd_bss_update_beacon
),
1037 UBUS_METHOD_NOARG("get_features", hostapd_bss_get_features
),
1039 UBUS_METHOD("switch_chan", hostapd_switch_chan
, csa_policy
),
1041 UBUS_METHOD("set_vendor_elements", hostapd_vendor_elements
, ve_policy
),
1042 UBUS_METHOD("notify_response", hostapd_notify_response
, notify_policy
),
1043 UBUS_METHOD("bss_mgmt_enable", hostapd_bss_mgmt_enable
, bss_mgmt_enable_policy
),
1044 UBUS_METHOD_NOARG("rrm_nr_get_own", hostapd_rrm_nr_get_own
),
1045 UBUS_METHOD_NOARG("rrm_nr_list", hostapd_rrm_nr_list
),
1046 UBUS_METHOD("rrm_nr_set", hostapd_rrm_nr_set
, nr_set_policy
),
1047 UBUS_METHOD("rrm_beacon_req", hostapd_rrm_beacon_req
, beacon_req_policy
),
1048 #ifdef CONFIG_WNM_AP
1049 UBUS_METHOD("wnm_disassoc_imminent", hostapd_wnm_disassoc_imminent
, wnm_disassoc_policy
),
1053 static struct ubus_object_type bss_object_type
=
1054 UBUS_OBJECT_TYPE("hostapd_bss", bss_methods
);
1056 static int avl_compare_macaddr(const void *k1
, const void *k2
, void *ptr
)
1058 return memcmp(k1
, k2
, ETH_ALEN
);
1061 void hostapd_ubus_add_bss(struct hostapd_data
*hapd
)
1063 struct ubus_object
*obj
= &hapd
->ubus
.obj
;
1068 if (hapd
->conf
->mesh
& MESH_ENABLED
)
1072 if (!hostapd_ubus_init())
1075 if (asprintf(&name
, "hostapd.%s", hapd
->conf
->iface
) < 0)
1078 avl_init(&hapd
->ubus
.banned
, avl_compare_macaddr
, false, NULL
);
1080 obj
->type
= &bss_object_type
;
1081 obj
->methods
= bss_object_type
.methods
;
1082 obj
->n_methods
= bss_object_type
.n_methods
;
1083 ret
= ubus_add_object(ctx
, obj
);
1084 hostapd_ubus_ref_inc();
1087 void hostapd_ubus_free_bss(struct hostapd_data
*hapd
)
1089 struct ubus_object
*obj
= &hapd
->ubus
.obj
;
1090 char *name
= (char *) obj
->name
;
1096 ubus_remove_object(ctx
, obj
);
1097 hostapd_ubus_ref_dec();
1103 static const struct ubus_method daemon_methods
[] = {
1104 UBUS_METHOD("config_add", hostapd_config_add
, config_add_policy
),
1105 UBUS_METHOD("config_remove", hostapd_config_remove
, config_remove_policy
),
1108 static struct ubus_object_type daemon_object_type
=
1109 UBUS_OBJECT_TYPE("hostapd", daemon_methods
);
1111 void hostapd_ubus_add(struct hapd_interfaces
*interfaces
)
1113 struct ubus_object
*obj
= &interfaces
->ubus
;
1117 if (!hostapd_ubus_init())
1120 name_len
= strlen("hostapd") + 1;
1121 if (interfaces
->name
)
1122 name_len
+= strlen(interfaces
->name
) + 1;
1123 obj
->name
= malloc(name_len
);
1124 strcpy(obj
->name
, "hostapd");
1125 if (interfaces
->name
) {
1126 strcat(obj
->name
, ".");
1127 strcat(obj
->name
, interfaces
->name
);
1130 obj
->type
= &daemon_object_type
;
1131 obj
->methods
= daemon_object_type
.methods
;
1132 obj
->n_methods
= daemon_object_type
.n_methods
;
1133 ret
= ubus_add_object(ctx
, obj
);
1134 hostapd_ubus_ref_inc();
1137 void hostapd_ubus_free(struct hapd_interfaces
*interfaces
)
1139 struct ubus_object
*obj
= &interfaces
->ubus
;
1140 char *name
= (char *) obj
->name
;
1146 ubus_remove_object(ctx
, obj
);
1147 hostapd_ubus_ref_dec();
1153 struct ubus_event_req
{
1154 struct ubus_notify_request nreq
;
1159 ubus_event_cb(struct ubus_notify_request
*req
, int idx
, int ret
)
1161 struct ubus_event_req
*ureq
= container_of(req
, struct ubus_event_req
, nreq
);
1166 int hostapd_ubus_handle_event(struct hostapd_data
*hapd
, struct hostapd_ubus_request
*req
)
1168 struct ubus_banned_client
*ban
;
1169 const char *types
[HOSTAPD_UBUS_TYPE_MAX
] = {
1170 [HOSTAPD_UBUS_PROBE_REQ
] = "probe",
1171 [HOSTAPD_UBUS_AUTH_REQ
] = "auth",
1172 [HOSTAPD_UBUS_ASSOC_REQ
] = "assoc",
1174 const char *type
= "mgmt";
1175 struct ubus_event_req ureq
= {};
1178 if (req
->mgmt_frame
)
1179 addr
= req
->mgmt_frame
->sa
;
1183 ban
= avl_find_element(&hapd
->ubus
.banned
, addr
, ban
, avl
);
1185 return WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA
;
1187 if (!hapd
->ubus
.obj
.has_subscribers
)
1188 return WLAN_STATUS_SUCCESS
;
1190 if (req
->type
< ARRAY_SIZE(types
))
1191 type
= types
[req
->type
];
1193 blob_buf_init(&b
, 0);
1194 blobmsg_add_macaddr(&b
, "address", addr
);
1195 if (req
->mgmt_frame
)
1196 blobmsg_add_macaddr(&b
, "target", req
->mgmt_frame
->da
);
1197 if (req
->ssi_signal
)
1198 blobmsg_add_u32(&b
, "signal", req
->ssi_signal
);
1199 blobmsg_add_u32(&b
, "freq", hapd
->iface
->freq
);
1202 if(req
->elems
->ht_capabilities
)
1204 struct ieee80211_ht_capabilities
*ht_capabilities
;
1205 void *ht_cap
, *ht_cap_mcs_set
, *mcs_set
;
1208 ht_capabilities
= (struct ieee80211_ht_capabilities
*) req
->elems
->ht_capabilities
;
1209 ht_cap
= blobmsg_open_table(&b
, "ht_capabilities");
1210 blobmsg_add_u16(&b
, "ht_capabilities_info", ht_capabilities
->ht_capabilities_info
);
1211 ht_cap_mcs_set
= blobmsg_open_table(&b
, "supported_mcs_set");
1212 blobmsg_add_u16(&b
, "a_mpdu_params", ht_capabilities
->a_mpdu_params
);
1213 blobmsg_add_u16(&b
, "ht_extended_capabilities", ht_capabilities
->ht_extended_capabilities
);
1214 blobmsg_add_u32(&b
, "tx_bf_capability_info", ht_capabilities
->tx_bf_capability_info
);
1215 blobmsg_add_u16(&b
, "asel_capabilities", ht_capabilities
->asel_capabilities
);
1216 mcs_set
= blobmsg_open_array(&b
, "supported_mcs_set");
1217 for (int i
= 0; i
< 16; i
++) {
1218 blobmsg_add_u16(&b
, NULL
, (u16
) ht_capabilities
->supported_mcs_set
[i
]);
1220 blobmsg_close_array(&b
, mcs_set
);
1221 blobmsg_close_table(&b
, ht_cap_mcs_set
);
1222 blobmsg_close_table(&b
, ht_cap
);
1224 if(req
->elems
->vht_capabilities
)
1226 struct ieee80211_vht_capabilities
*vht_capabilities
;
1227 void *vht_cap
, *vht_cap_mcs_set
;
1229 vht_capabilities
= (struct ieee80211_vht_capabilities
*) req
->elems
->vht_capabilities
;
1230 vht_cap
= blobmsg_open_table(&b
, "vht_capabilities");
1231 blobmsg_add_u32(&b
, "vht_capabilities_info", vht_capabilities
->vht_capabilities_info
);
1232 vht_cap_mcs_set
= blobmsg_open_table(&b
, "vht_supported_mcs_set");
1233 blobmsg_add_u16(&b
, "rx_map", vht_capabilities
->vht_supported_mcs_set
.rx_map
);
1234 blobmsg_add_u16(&b
, "rx_highest", vht_capabilities
->vht_supported_mcs_set
.rx_highest
);
1235 blobmsg_add_u16(&b
, "tx_map", vht_capabilities
->vht_supported_mcs_set
.tx_map
);
1236 blobmsg_add_u16(&b
, "tx_highest", vht_capabilities
->vht_supported_mcs_set
.tx_highest
);
1237 blobmsg_close_table(&b
, vht_cap_mcs_set
);
1238 blobmsg_close_table(&b
, vht_cap
);
1242 if (!hapd
->ubus
.notify_response
) {
1243 ubus_notify(ctx
, &hapd
->ubus
.obj
, type
, b
.head
, -1);
1244 return WLAN_STATUS_SUCCESS
;
1247 if (ubus_notify_async(ctx
, &hapd
->ubus
.obj
, type
, b
.head
, &ureq
.nreq
))
1248 return WLAN_STATUS_SUCCESS
;
1250 ureq
.nreq
.status_cb
= ubus_event_cb
;
1251 ubus_complete_request(ctx
, &ureq
.nreq
.req
, 100);
1256 return WLAN_STATUS_SUCCESS
;
1259 void hostapd_ubus_notify(struct hostapd_data
*hapd
, const char *type
, const u8
*addr
)
1261 if (!hapd
->ubus
.obj
.has_subscribers
)
1267 blob_buf_init(&b
, 0);
1268 blobmsg_add_macaddr(&b
, "address", addr
);
1270 ubus_notify(ctx
, &hapd
->ubus
.obj
, type
, b
.head
, -1);