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 return hostapd_reload_config(hapd
->iface
, 1);
159 hostapd_bss_get_clients(struct ubus_context
*ctx
, struct ubus_object
*obj
,
160 struct ubus_request_data
*req
, const char *method
,
161 struct blob_attr
*msg
)
163 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
164 struct sta_info
*sta
;
167 static const struct {
171 { "auth", WLAN_STA_AUTH
},
172 { "assoc", WLAN_STA_ASSOC
},
173 { "authorized", WLAN_STA_AUTHORIZED
},
174 { "preauth", WLAN_STA_PREAUTH
},
175 { "wds", WLAN_STA_WDS
},
176 { "wmm", WLAN_STA_WMM
},
177 { "ht", WLAN_STA_HT
},
178 { "vht", WLAN_STA_VHT
},
179 { "wps", WLAN_STA_WPS
},
180 { "mfp", WLAN_STA_MFP
},
183 blob_buf_init(&b
, 0);
184 blobmsg_add_u32(&b
, "freq", hapd
->iface
->freq
);
185 list
= blobmsg_open_table(&b
, "clients");
186 for (sta
= hapd
->sta_list
; sta
; sta
= sta
->next
) {
190 sprintf(mac_buf
, MACSTR
, MAC2STR(sta
->addr
));
191 c
= blobmsg_open_table(&b
, mac_buf
);
192 for (i
= 0; i
< ARRAY_SIZE(sta_flags
); i
++)
193 blobmsg_add_u8(&b
, sta_flags
[i
].name
,
194 !!(sta
->flags
& sta_flags
[i
].flag
));
196 r
= blobmsg_open_array(&b
, "rrm");
197 for (i
= 0; i
< ARRAY_SIZE(sta
->rrm_enabled_capa
); i
++)
198 blobmsg_add_u32(&b
, "", sta
->rrm_enabled_capa
[i
]);
199 blobmsg_close_array(&b
, r
);
200 blobmsg_add_u32(&b
, "aid", sta
->aid
);
201 #ifdef CONFIG_TAXONOMY
202 r
= blobmsg_alloc_string_buffer(&b
, "signature", 1024);
203 if (retrieve_sta_taxonomy(hapd
, sta
, r
, 1024) > 0)
204 blobmsg_add_string_buffer(&b
);
206 blobmsg_close_table(&b
, c
);
208 blobmsg_close_array(&b
, list
);
209 ubus_send_reply(ctx
, req
, b
.head
);
215 hostapd_bss_get_features(struct ubus_context
*ctx
, struct ubus_object
*obj
,
216 struct ubus_request_data
*req
, const char *method
,
217 struct blob_attr
*msg
)
219 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
221 blob_buf_init(&b
, 0);
222 blobmsg_add_u8(&b
, "ht_supported", ht_supported(hapd
->iface
->hw_features
));
223 blobmsg_add_u8(&b
, "vht_supported", vht_supported(hapd
->iface
->hw_features
));
224 ubus_send_reply(ctx
, req
, b
.head
);
234 static const struct blobmsg_policy notify_policy
[__NOTIFY_MAX
] = {
235 [NOTIFY_RESPONSE
] = { "notify_response", BLOBMSG_TYPE_INT32
},
239 hostapd_notify_response(struct ubus_context
*ctx
, struct ubus_object
*obj
,
240 struct ubus_request_data
*req
, const char *method
,
241 struct blob_attr
*msg
)
243 struct blob_attr
*tb
[__NOTIFY_MAX
];
244 struct hostapd_data
*hapd
= get_hapd_from_object(obj
);
245 struct wpabuf
*elems
;
249 blobmsg_parse(notify_policy
, __NOTIFY_MAX
, tb
,
250 blob_data(msg
), blob_len(msg
));
252 if (!tb
[NOTIFY_RESPONSE
])
253 return UBUS_STATUS_INVALID_ARGUMENT
;
255 hapd
->ubus
.notify_response
= blobmsg_get_u32(tb
[NOTIFY_RESPONSE
]);
257 return UBUS_STATUS_OK
;
268 static const struct blobmsg_policy del_policy
[__DEL_CLIENT_MAX
] = {
269 [DEL_CLIENT_ADDR
] = { "addr", BLOBMSG_TYPE_STRING
},
270 [DEL_CLIENT_REASON
] = { "reason", BLOBMSG_TYPE_INT32
},
271 [DEL_CLIENT_DEAUTH
] = { "deauth", BLOBMSG_TYPE_INT8
},
272 [DEL_CLIENT_BAN_TIME
] = { "ban_time", BLOBMSG_TYPE_INT32
},
276 hostapd_bss_del_client(struct ubus_context
*ctx
, struct ubus_object
*obj
,
277 struct ubus_request_data
*req
, const char *method
,
278 struct blob_attr
*msg
)
280 struct blob_attr
*tb
[__DEL_CLIENT_MAX
];
281 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
282 struct sta_info
*sta
;
287 blobmsg_parse(del_policy
, __DEL_CLIENT_MAX
, tb
, blob_data(msg
), blob_len(msg
));
289 if (!tb
[DEL_CLIENT_ADDR
])
290 return UBUS_STATUS_INVALID_ARGUMENT
;
292 if (hwaddr_aton(blobmsg_data(tb
[DEL_CLIENT_ADDR
]), addr
))
293 return UBUS_STATUS_INVALID_ARGUMENT
;
295 if (tb
[DEL_CLIENT_REASON
])
296 reason
= blobmsg_get_u32(tb
[DEL_CLIENT_REASON
]);
298 if (tb
[DEL_CLIENT_DEAUTH
])
299 deauth
= blobmsg_get_bool(tb
[DEL_CLIENT_DEAUTH
]);
301 sta
= ap_get_sta(hapd
, addr
);
304 hostapd_drv_sta_deauth(hapd
, addr
, reason
);
305 ap_sta_deauthenticate(hapd
, sta
, reason
);
307 hostapd_drv_sta_disassoc(hapd
, addr
, reason
);
308 ap_sta_disassociate(hapd
, sta
, reason
);
312 if (tb
[DEL_CLIENT_BAN_TIME
])
313 hostapd_bss_ban_client(hapd
, addr
, blobmsg_get_u32(tb
[DEL_CLIENT_BAN_TIME
]));
319 blobmsg_add_macaddr(struct blob_buf
*buf
, const char *name
, const u8
*addr
)
323 s
= blobmsg_alloc_string_buffer(buf
, name
, 20);
324 sprintf(s
, MACSTR
, MAC2STR(addr
));
325 blobmsg_add_string_buffer(buf
);
329 hostapd_bss_list_bans(struct ubus_context
*ctx
, struct ubus_object
*obj
,
330 struct ubus_request_data
*req
, const char *method
,
331 struct blob_attr
*msg
)
333 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
334 struct ubus_banned_client
*ban
;
337 blob_buf_init(&b
, 0);
338 c
= blobmsg_open_array(&b
, "clients");
339 avl_for_each_element(&hapd
->ubus
.banned
, ban
, avl
)
340 blobmsg_add_macaddr(&b
, NULL
, ban
->addr
);
341 blobmsg_close_array(&b
, c
);
342 ubus_send_reply(ctx
, req
, b
.head
);
348 hostapd_bss_wps_start(struct ubus_context
*ctx
, struct ubus_object
*obj
,
349 struct ubus_request_data
*req
, const char *method
,
350 struct blob_attr
*msg
)
353 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
355 rc
= hostapd_wps_button_pushed(hapd
, NULL
);
358 return UBUS_STATUS_NOT_SUPPORTED
;
364 hostapd_bss_wps_cancel(struct ubus_context
*ctx
, struct ubus_object
*obj
,
365 struct ubus_request_data
*req
, const char *method
,
366 struct blob_attr
*msg
)
369 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
371 rc
= hostapd_wps_cancel(hapd
);
374 return UBUS_STATUS_NOT_SUPPORTED
;
380 hostapd_bss_update_beacon(struct ubus_context
*ctx
, struct ubus_object
*obj
,
381 struct ubus_request_data
*req
, const char *method
,
382 struct blob_attr
*msg
)
385 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
387 rc
= ieee802_11_set_beacon(hapd
);
390 return UBUS_STATUS_NOT_SUPPORTED
;
401 static const struct blobmsg_policy config_add_policy
[__CONFIG_MAX
] = {
402 [CONFIG_IFACE
] = { "iface", BLOBMSG_TYPE_STRING
},
403 [CONFIG_FILE
] = { "config", BLOBMSG_TYPE_STRING
},
407 hostapd_config_add(struct ubus_context
*ctx
, struct ubus_object
*obj
,
408 struct ubus_request_data
*req
, const char *method
,
409 struct blob_attr
*msg
)
411 struct blob_attr
*tb
[__CONFIG_MAX
];
412 struct hapd_interfaces
*interfaces
= get_hapd_interfaces_from_object(obj
);
415 blobmsg_parse(config_add_policy
, __CONFIG_MAX
, tb
, blob_data(msg
), blob_len(msg
));
417 if (!tb
[CONFIG_FILE
] || !tb
[CONFIG_IFACE
])
418 return UBUS_STATUS_INVALID_ARGUMENT
;
420 snprintf(buf
, sizeof(buf
), "bss_config=%s:%s",
421 blobmsg_get_string(tb
[CONFIG_IFACE
]),
422 blobmsg_get_string(tb
[CONFIG_FILE
]));
424 if (hostapd_add_iface(interfaces
, buf
))
425 return UBUS_STATUS_INVALID_ARGUMENT
;
427 return UBUS_STATUS_OK
;
435 static const struct blobmsg_policy config_remove_policy
[__CONFIG_REM_MAX
] = {
436 [CONFIG_REM_IFACE
] = { "iface", BLOBMSG_TYPE_STRING
},
440 hostapd_config_remove(struct ubus_context
*ctx
, struct ubus_object
*obj
,
441 struct ubus_request_data
*req
, const char *method
,
442 struct blob_attr
*msg
)
444 struct blob_attr
*tb
[__CONFIG_REM_MAX
];
445 struct hapd_interfaces
*interfaces
= get_hapd_interfaces_from_object(obj
);
448 blobmsg_parse(config_remove_policy
, __CONFIG_REM_MAX
, tb
, blob_data(msg
), blob_len(msg
));
450 if (!tb
[CONFIG_REM_IFACE
])
451 return UBUS_STATUS_INVALID_ARGUMENT
;
453 if (hostapd_remove_iface(interfaces
, blobmsg_get_string(tb
[CONFIG_REM_IFACE
])))
454 return UBUS_STATUS_INVALID_ARGUMENT
;
456 return UBUS_STATUS_OK
;
465 CSA_SEC_CHANNEL_OFFSET
,
472 static const struct blobmsg_policy csa_policy
[__CSA_MAX
] = {
473 [CSA_FREQ
] = { "freq", BLOBMSG_TYPE_INT32
},
474 [CSA_BCN_COUNT
] = { "bcn_count", BLOBMSG_TYPE_INT32
},
475 [CSA_CENTER_FREQ1
] = { "center_freq1", BLOBMSG_TYPE_INT32
},
476 [CSA_CENTER_FREQ2
] = { "center_freq2", BLOBMSG_TYPE_INT32
},
477 [CSA_BANDWIDTH
] = { "bandwidth", BLOBMSG_TYPE_INT32
},
478 [CSA_SEC_CHANNEL_OFFSET
] = { "sec_channel_offset", BLOBMSG_TYPE_INT32
},
479 [CSA_HT
] = { "ht", BLOBMSG_TYPE_BOOL
},
480 [CSA_VHT
] = { "vht", BLOBMSG_TYPE_BOOL
},
481 [CSA_BLOCK_TX
] = { "block_tx", BLOBMSG_TYPE_BOOL
},
486 hostapd_switch_chan(struct ubus_context
*ctx
, struct ubus_object
*obj
,
487 struct ubus_request_data
*req
, const char *method
,
488 struct blob_attr
*msg
)
490 struct blob_attr
*tb
[__CSA_MAX
];
491 struct hostapd_data
*hapd
= get_hapd_from_object(obj
);
492 struct csa_settings css
;
494 blobmsg_parse(csa_policy
, __CSA_MAX
, tb
, blob_data(msg
), blob_len(msg
));
497 return UBUS_STATUS_INVALID_ARGUMENT
;
499 memset(&css
, 0, sizeof(css
));
500 css
.freq_params
.freq
= blobmsg_get_u32(tb
[CSA_FREQ
]);
502 #define SET_CSA_SETTING(name, field, type) \
505 css.field = blobmsg_get_ ## type(tb[name]); \
508 SET_CSA_SETTING(CSA_BCN_COUNT
, cs_count
, u32
);
509 SET_CSA_SETTING(CSA_CENTER_FREQ1
, freq_params
.center_freq1
, u32
);
510 SET_CSA_SETTING(CSA_CENTER_FREQ2
, freq_params
.center_freq2
, u32
);
511 SET_CSA_SETTING(CSA_BANDWIDTH
, freq_params
.bandwidth
, u32
);
512 SET_CSA_SETTING(CSA_SEC_CHANNEL_OFFSET
, freq_params
.sec_channel_offset
, u32
);
513 SET_CSA_SETTING(CSA_HT
, freq_params
.ht_enabled
, bool);
514 SET_CSA_SETTING(CSA_VHT
, freq_params
.vht_enabled
, bool);
515 SET_CSA_SETTING(CSA_BLOCK_TX
, block_tx
, bool);
518 if (hostapd_switch_channel(hapd
, &css
) != 0)
519 return UBUS_STATUS_NOT_SUPPORTED
;
520 return UBUS_STATUS_OK
;
521 #undef SET_CSA_SETTING
527 __VENDOR_ELEMENTS_MAX
530 static const struct blobmsg_policy ve_policy
[__VENDOR_ELEMENTS_MAX
] = {
531 /* vendor elements are provided as hex-string */
532 [VENDOR_ELEMENTS
] = { "vendor_elements", BLOBMSG_TYPE_STRING
},
536 hostapd_vendor_elements(struct ubus_context
*ctx
, struct ubus_object
*obj
,
537 struct ubus_request_data
*req
, const char *method
,
538 struct blob_attr
*msg
)
540 struct blob_attr
*tb
[__VENDOR_ELEMENTS_MAX
];
541 struct hostapd_data
*hapd
= get_hapd_from_object(obj
);
542 struct hostapd_bss_config
*bss
= hapd
->conf
;
543 struct wpabuf
*elems
;
547 blobmsg_parse(ve_policy
, __VENDOR_ELEMENTS_MAX
, tb
,
548 blob_data(msg
), blob_len(msg
));
550 if (!tb
[VENDOR_ELEMENTS
])
551 return UBUS_STATUS_INVALID_ARGUMENT
;
553 pos
= blobmsg_data(tb
[VENDOR_ELEMENTS
]);
554 len
= os_strlen(pos
);
556 return UBUS_STATUS_INVALID_ARGUMENT
;
560 wpabuf_free(bss
->vendor_elements
);
561 bss
->vendor_elements
= NULL
;
565 elems
= wpabuf_alloc(len
);
569 if (hexstr2bin(pos
, wpabuf_put(elems
, len
), len
)) {
571 return UBUS_STATUS_INVALID_ARGUMENT
;
574 wpabuf_free(bss
->vendor_elements
);
575 bss
->vendor_elements
= elems
;
577 /* update beacons if vendor elements were set successfully */
578 if (ieee802_11_update_beacons(hapd
->iface
) != 0)
579 return UBUS_STATUS_NOT_SUPPORTED
;
580 return UBUS_STATUS_OK
;
584 hostapd_rrm_print_nr(struct hostapd_neighbor_entry
*nr
)
590 blobmsg_printf(&b
, "", MACSTR
, MAC2STR(nr
->bssid
));
592 str
= blobmsg_alloc_string_buffer(&b
, "", nr
->ssid
.ssid_len
+ 1);
593 memcpy(str
, nr
->ssid
.ssid
, nr
->ssid
.ssid_len
);
594 str
[nr
->ssid
.ssid_len
] = 0;
595 blobmsg_add_string_buffer(&b
);
597 len
= wpabuf_len(nr
->nr
);
598 str
= blobmsg_alloc_string_buffer(&b
, "", 2 * len
+ 1);
599 wpa_snprintf_hex(str
, 2 * len
+ 1, wpabuf_head_u8(nr
->nr
), len
);
600 blobmsg_add_string_buffer(&b
);
604 BSS_MGMT_EN_NEIGHBOR
,
607 BSS_MGMT_EN_BSS_TRANSITION
,
613 __hostapd_bss_mgmt_enable_f(struct hostapd_data
*hapd
, int flag
)
615 struct hostapd_bss_config
*bss
= hapd
->conf
;
619 case BSS_MGMT_EN_NEIGHBOR
:
620 if (bss
->radio_measurements
[0] &
621 WLAN_RRM_CAPS_NEIGHBOR_REPORT
)
624 bss
->radio_measurements
[0] |=
625 WLAN_RRM_CAPS_NEIGHBOR_REPORT
;
626 hostapd_neighbor_set_own_report(hapd
);
628 case BSS_MGMT_EN_BEACON
:
629 flags
= WLAN_RRM_CAPS_BEACON_REPORT_PASSIVE
|
630 WLAN_RRM_CAPS_BEACON_REPORT_ACTIVE
|
631 WLAN_RRM_CAPS_BEACON_REPORT_TABLE
;
633 if (bss
->radio_measurements
[0] & flags
== flags
)
636 bss
->radio_measurements
[0] |= (u8
) flags
;
639 case BSS_MGMT_EN_BSS_TRANSITION
:
640 if (bss
->bss_transition
)
643 bss
->bss_transition
= 1;
650 __hostapd_bss_mgmt_enable(struct hostapd_data
*hapd
, uint32_t flags
)
655 for (i
= 0; i
< __BSS_MGMT_EN_MAX
; i
++) {
656 if (!(flags
& (1 << i
)))
659 update
|= __hostapd_bss_mgmt_enable_f(hapd
, i
);
663 ieee802_11_update_beacons(hapd
->iface
);
667 static const struct blobmsg_policy bss_mgmt_enable_policy
[__BSS_MGMT_EN_MAX
] = {
668 [BSS_MGMT_EN_NEIGHBOR
] = { "neighbor_report", BLOBMSG_TYPE_BOOL
},
669 [BSS_MGMT_EN_BEACON
] = { "beacon_report", BLOBMSG_TYPE_BOOL
},
671 [BSS_MGMT_EN_BSS_TRANSITION
] = { "bss_transition", BLOBMSG_TYPE_BOOL
},
676 hostapd_bss_mgmt_enable(struct ubus_context
*ctx
, struct ubus_object
*obj
,
677 struct ubus_request_data
*req
, const char *method
,
678 struct blob_attr
*msg
)
681 struct hostapd_data
*hapd
= get_hapd_from_object(obj
);
682 struct blob_attr
*tb
[__BSS_MGMT_EN_MAX
];
683 struct blob_attr
*cur
;
686 bool neigh
= false, beacon
= false;
688 blobmsg_parse(bss_mgmt_enable_policy
, __BSS_MGMT_EN_MAX
, tb
, blob_data(msg
), blob_len(msg
));
690 for (i
= 0; i
< ARRAY_SIZE(tb
); i
++) {
691 if (!tb
[i
] || !blobmsg_get_bool(tb
[i
]))
697 __hostapd_bss_mgmt_enable(hapd
, flags
);
702 hostapd_rrm_nr_enable(struct hostapd_data
*hapd
)
704 __hostapd_bss_mgmt_enable(hapd
, 1 << BSS_MGMT_EN_NEIGHBOR
);
708 hostapd_rrm_nr_get_own(struct ubus_context
*ctx
, struct ubus_object
*obj
,
709 struct ubus_request_data
*req
, const char *method
,
710 struct blob_attr
*msg
)
712 struct hostapd_data
*hapd
= get_hapd_from_object(obj
);
713 struct hostapd_neighbor_entry
*nr
;
716 hostapd_rrm_nr_enable(hapd
);
718 nr
= hostapd_neighbor_get(hapd
, hapd
->own_addr
, NULL
);
720 return UBUS_STATUS_NOT_FOUND
;
722 blob_buf_init(&b
, 0);
724 c
= blobmsg_open_array(&b
, "value");
725 hostapd_rrm_print_nr(nr
);
726 blobmsg_close_array(&b
, c
);
728 ubus_send_reply(ctx
, req
, b
.head
);
734 hostapd_rrm_nr_list(struct ubus_context
*ctx
, struct ubus_object
*obj
,
735 struct ubus_request_data
*req
, const char *method
,
736 struct blob_attr
*msg
)
738 struct hostapd_data
*hapd
= get_hapd_from_object(obj
);
739 struct hostapd_neighbor_entry
*nr
;
742 hostapd_rrm_nr_enable(hapd
);
743 blob_buf_init(&b
, 0);
745 c
= blobmsg_open_array(&b
, "list");
746 dl_list_for_each(nr
, &hapd
->nr_db
, struct hostapd_neighbor_entry
, list
) {
749 if (!memcmp(nr
->bssid
, hapd
->own_addr
, ETH_ALEN
))
752 cur
= blobmsg_open_array(&b
, NULL
);
753 hostapd_rrm_print_nr(nr
);
754 blobmsg_close_array(&b
, cur
);
756 blobmsg_close_array(&b
, c
);
758 ubus_send_reply(ctx
, req
, b
.head
);
768 static const struct blobmsg_policy nr_set_policy
[__NR_SET_LIST_MAX
] = {
769 [NR_SET_LIST
] = { "list", BLOBMSG_TYPE_ARRAY
},
774 hostapd_rrm_nr_clear(struct hostapd_data
*hapd
)
776 struct hostapd_neighbor_entry
*nr
;
779 dl_list_for_each(nr
, &hapd
->nr_db
, struct hostapd_neighbor_entry
, list
) {
780 if (!memcmp(nr
->bssid
, hapd
->own_addr
, ETH_ALEN
))
783 hostapd_neighbor_remove(hapd
, nr
->bssid
, &nr
->ssid
);
789 hostapd_rrm_nr_set(struct ubus_context
*ctx
, struct ubus_object
*obj
,
790 struct ubus_request_data
*req
, const char *method
,
791 struct blob_attr
*msg
)
793 static const struct blobmsg_policy nr_e_policy
[] = {
794 { .type
= BLOBMSG_TYPE_STRING
},
795 { .type
= BLOBMSG_TYPE_STRING
},
796 { .type
= BLOBMSG_TYPE_STRING
},
798 struct hostapd_data
*hapd
= get_hapd_from_object(obj
);
799 struct blob_attr
*tb_l
[__NR_SET_LIST_MAX
];
800 struct blob_attr
*tb
[ARRAY_SIZE(nr_e_policy
)];
801 struct blob_attr
*cur
;
805 hostapd_rrm_nr_enable(hapd
);
807 blobmsg_parse(nr_set_policy
, __NR_SET_LIST_MAX
, tb_l
, blob_data(msg
), blob_len(msg
));
808 if (!tb_l
[NR_SET_LIST
])
809 return UBUS_STATUS_INVALID_ARGUMENT
;
811 hostapd_rrm_nr_clear(hapd
);
812 blobmsg_for_each_attr(cur
, tb_l
[NR_SET_LIST
], rem
) {
813 struct wpa_ssid_value ssid
;
818 blobmsg_parse_array(nr_e_policy
, ARRAY_SIZE(nr_e_policy
), tb
, blobmsg_data(cur
), blobmsg_data_len(cur
));
819 if (!tb
[0] || !tb
[1] || !tb
[2])
822 s
= blobmsg_get_string(tb
[0]);
823 if (hwaddr_aton(s
, bssid
))
826 s
= blobmsg_get_string(tb
[1]);
827 ssid
.ssid_len
= strlen(s
);
828 if (ssid
.ssid_len
> sizeof(ssid
.ssid
))
831 memcpy(&ssid
, s
, ssid
.ssid_len
);
832 data
= wpabuf_parse_bin(blobmsg_get_string(tb
[2]));
836 hostapd_neighbor_set(hapd
, bssid
, &ssid
, data
, NULL
, NULL
, 0);
841 ret
= UBUS_STATUS_INVALID_ARGUMENT
;
858 static const struct blobmsg_policy beacon_req_policy
[__BEACON_REQ_MAX
] = {
859 [BEACON_REQ_ADDR
] = { "addr", BLOBMSG_TYPE_STRING
},
860 [BEACON_REQ_OP_CLASS
] { "op_class", BLOBMSG_TYPE_INT32
},
861 [BEACON_REQ_CHANNEL
] { "channel", BLOBMSG_TYPE_INT32
},
862 [BEACON_REQ_DURATION
] { "duration", BLOBMSG_TYPE_INT32
},
863 [BEACON_REQ_MODE
] { "mode", BLOBMSG_TYPE_INT32
},
864 [BEACON_REQ_BSSID
] { "bssid", BLOBMSG_TYPE_STRING
},
865 [BEACON_REQ_SSID
] { "ssid", BLOBMSG_TYPE_STRING
},
869 hostapd_rrm_beacon_req(struct ubus_context
*ctx
, struct ubus_object
*obj
,
870 struct ubus_request_data
*ureq
, const char *method
,
871 struct blob_attr
*msg
)
873 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
874 struct blob_attr
*tb
[__BEACON_REQ_MAX
];
875 struct blob_attr
*cur
;
877 u8 bssid
[ETH_ALEN
] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
882 blobmsg_parse(beacon_req_policy
, __BEACON_REQ_MAX
, tb
, blob_data(msg
), blob_len(msg
));
884 if (!tb
[BEACON_REQ_ADDR
] || !tb
[BEACON_REQ_MODE
] || !tb
[BEACON_REQ_DURATION
] ||
885 !tb
[BEACON_REQ_OP_CLASS
] || !tb
[BEACON_REQ_CHANNEL
])
886 return UBUS_STATUS_INVALID_ARGUMENT
;
888 if (tb
[BEACON_REQ_SSID
])
889 buf_len
+= blobmsg_data_len(tb
[BEACON_REQ_SSID
]) + 2 - 1;
891 mode
= blobmsg_get_u32(tb
[BEACON_REQ_MODE
]);
892 if (hwaddr_aton(blobmsg_data(tb
[BEACON_REQ_ADDR
]), addr
))
893 return UBUS_STATUS_INVALID_ARGUMENT
;
895 if (tb
[BEACON_REQ_BSSID
] &&
896 hwaddr_aton(blobmsg_data(tb
[BEACON_REQ_BSSID
]), bssid
))
897 return UBUS_STATUS_INVALID_ARGUMENT
;
899 req
= wpabuf_alloc(buf_len
);
901 return UBUS_STATUS_UNKNOWN_ERROR
;
903 /* 1: regulatory class */
904 wpabuf_put_u8(req
, blobmsg_get_u32(tb
[BEACON_REQ_OP_CLASS
]));
906 /* 2: channel number */
907 wpabuf_put_u8(req
, blobmsg_get_u32(tb
[BEACON_REQ_CHANNEL
]));
909 /* 3-4: randomization interval */
910 wpabuf_put_le16(req
, 0);
913 wpabuf_put_le16(req
, blobmsg_get_u32(tb
[BEACON_REQ_DURATION
]));
916 wpabuf_put_u8(req
, blobmsg_get_u32(tb
[BEACON_REQ_MODE
]));
919 wpabuf_put_data(req
, bssid
, ETH_ALEN
);
921 if ((cur
= tb
[BEACON_REQ_SSID
]) != NULL
) {
922 wpabuf_put_u8(req
, WLAN_EID_SSID
);
923 wpabuf_put_u8(req
, blobmsg_data_len(cur
) - 1);
924 wpabuf_put_data(req
, blobmsg_data(cur
), blobmsg_data_len(cur
) - 1);
927 ret
= hostapd_send_beacon_req(hapd
, addr
, 0, req
);
938 WNM_DISASSOC_DURATION
,
939 WNM_DISASSOC_NEIGHBORS
,
943 static const struct blobmsg_policy wnm_disassoc_policy
[__WNM_DISASSOC_MAX
] = {
944 [WNM_DISASSOC_ADDR
] = { "addr", BLOBMSG_TYPE_STRING
},
945 [WNM_DISASSOC_DURATION
] { "duration", BLOBMSG_TYPE_INT32
},
946 [WNM_DISASSOC_NEIGHBORS
] { "neighbors", BLOBMSG_TYPE_ARRAY
},
950 hostapd_wnm_disassoc_imminent(struct ubus_context
*ctx
, struct ubus_object
*obj
,
951 struct ubus_request_data
*ureq
, const char *method
,
952 struct blob_attr
*msg
)
954 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
955 struct blob_attr
*tb
[__WNM_DISASSOC_MAX
];
956 struct blob_attr
*cur
;
957 struct sta_info
*sta
;
962 u8 req_mode
= WNM_BSS_TM_REQ_DISASSOC_IMMINENT
;
965 blobmsg_parse(wnm_disassoc_policy
, __WNM_DISASSOC_MAX
, tb
, blob_data(msg
), blob_len(msg
));
967 if (!tb
[WNM_DISASSOC_ADDR
])
968 return UBUS_STATUS_INVALID_ARGUMENT
;
970 if (hwaddr_aton(blobmsg_data(tb
[WNM_DISASSOC_ADDR
]), addr
))
971 return UBUS_STATUS_INVALID_ARGUMENT
;
973 if ((cur
= tb
[WNM_DISASSOC_DURATION
]) != NULL
)
974 duration
= blobmsg_get_u32(cur
);
976 sta
= ap_get_sta(hapd
, addr
);
978 return UBUS_STATUS_NOT_FOUND
;
980 if (tb
[WNM_DISASSOC_NEIGHBORS
]) {
983 if (blobmsg_check_array(tb
[WNM_DISASSOC_NEIGHBORS
],
984 BLOBMSG_TYPE_STRING
) < 0)
985 return UBUS_STATUS_INVALID_ARGUMENT
;
987 blobmsg_for_each_attr(cur
, tb
[WNM_DISASSOC_NEIGHBORS
], rem
) {
988 int len
= strlen(blobmsg_get_string(cur
));
991 return UBUS_STATUS_INVALID_ARGUMENT
;
993 nr_len
+= (len
/ 2) + 2;
997 nr
= os_zalloc(nr_len
);
999 return UBUS_STATUS_UNKNOWN_ERROR
;
1003 blobmsg_for_each_attr(cur
, tb
[WNM_DISASSOC_NEIGHBORS
], rem
) {
1004 int len
= strlen(blobmsg_get_string(cur
)) / 2;
1006 *nr_cur
++ = WLAN_EID_NEIGHBOR_REPORT
;
1007 *nr_cur
++ = (u8
) len
;
1008 if (hexstr2bin(blobmsg_data(cur
), nr_cur
, len
)) {
1010 return UBUS_STATUS_INVALID_ARGUMENT
;
1018 req_mode
|= WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED
;
1020 if (wnm_send_bss_tm_req(hapd
, sta
, req_mode
, duration
, 0, NULL
,
1021 NULL
, nr
, nr_len
, NULL
, 0))
1022 return UBUS_STATUS_UNKNOWN_ERROR
;
1028 static const struct ubus_method bss_methods
[] = {
1029 UBUS_METHOD_NOARG("reload", hostapd_bss_reload
),
1030 UBUS_METHOD_NOARG("get_clients", hostapd_bss_get_clients
),
1031 UBUS_METHOD("del_client", hostapd_bss_del_client
, del_policy
),
1032 UBUS_METHOD_NOARG("list_bans", hostapd_bss_list_bans
),
1033 UBUS_METHOD_NOARG("wps_start", hostapd_bss_wps_start
),
1034 UBUS_METHOD_NOARG("wps_cancel", hostapd_bss_wps_cancel
),
1035 UBUS_METHOD_NOARG("update_beacon", hostapd_bss_update_beacon
),
1036 UBUS_METHOD_NOARG("get_features", hostapd_bss_get_features
),
1038 UBUS_METHOD("switch_chan", hostapd_switch_chan
, csa_policy
),
1040 UBUS_METHOD("set_vendor_elements", hostapd_vendor_elements
, ve_policy
),
1041 UBUS_METHOD("notify_response", hostapd_notify_response
, notify_policy
),
1042 UBUS_METHOD("bss_mgmt_enable", hostapd_bss_mgmt_enable
, bss_mgmt_enable_policy
),
1043 UBUS_METHOD_NOARG("rrm_nr_get_own", hostapd_rrm_nr_get_own
),
1044 UBUS_METHOD_NOARG("rrm_nr_list", hostapd_rrm_nr_list
),
1045 UBUS_METHOD("rrm_nr_set", hostapd_rrm_nr_set
, nr_set_policy
),
1046 UBUS_METHOD("rrm_beacon_req", hostapd_rrm_beacon_req
, beacon_req_policy
),
1047 #ifdef CONFIG_WNM_AP
1048 UBUS_METHOD("wnm_disassoc_imminent", hostapd_wnm_disassoc_imminent
, wnm_disassoc_policy
),
1052 static struct ubus_object_type bss_object_type
=
1053 UBUS_OBJECT_TYPE("hostapd_bss", bss_methods
);
1055 static int avl_compare_macaddr(const void *k1
, const void *k2
, void *ptr
)
1057 return memcmp(k1
, k2
, ETH_ALEN
);
1060 void hostapd_ubus_add_bss(struct hostapd_data
*hapd
)
1062 struct ubus_object
*obj
= &hapd
->ubus
.obj
;
1067 if (hapd
->conf
->mesh
& MESH_ENABLED
)
1071 if (!hostapd_ubus_init())
1074 if (asprintf(&name
, "hostapd.%s", hapd
->conf
->iface
) < 0)
1077 avl_init(&hapd
->ubus
.banned
, avl_compare_macaddr
, false, NULL
);
1079 obj
->type
= &bss_object_type
;
1080 obj
->methods
= bss_object_type
.methods
;
1081 obj
->n_methods
= bss_object_type
.n_methods
;
1082 ret
= ubus_add_object(ctx
, obj
);
1083 hostapd_ubus_ref_inc();
1086 void hostapd_ubus_free_bss(struct hostapd_data
*hapd
)
1088 struct ubus_object
*obj
= &hapd
->ubus
.obj
;
1089 char *name
= (char *) obj
->name
;
1095 ubus_remove_object(ctx
, obj
);
1096 hostapd_ubus_ref_dec();
1102 static const struct ubus_method daemon_methods
[] = {
1103 UBUS_METHOD("config_add", hostapd_config_add
, config_add_policy
),
1104 UBUS_METHOD("config_remove", hostapd_config_remove
, config_remove_policy
),
1107 static struct ubus_object_type daemon_object_type
=
1108 UBUS_OBJECT_TYPE("hostapd", daemon_methods
);
1110 void hostapd_ubus_add(struct hapd_interfaces
*interfaces
)
1112 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;
1124 name
= malloc(name_len
);
1125 strcpy(name
, "hostapd");
1126 if (interfaces
->name
) {
1128 strcat(name
, interfaces
->name
);
1132 obj
->type
= &daemon_object_type
;
1133 obj
->methods
= daemon_object_type
.methods
;
1134 obj
->n_methods
= daemon_object_type
.n_methods
;
1135 ret
= ubus_add_object(ctx
, obj
);
1136 hostapd_ubus_ref_inc();
1139 void hostapd_ubus_free(struct hapd_interfaces
*interfaces
)
1141 struct ubus_object
*obj
= &interfaces
->ubus
;
1142 char *name
= (char *) obj
->name
;
1148 ubus_remove_object(ctx
, obj
);
1149 hostapd_ubus_ref_dec();
1155 struct ubus_event_req
{
1156 struct ubus_notify_request nreq
;
1161 ubus_event_cb(struct ubus_notify_request
*req
, int idx
, int ret
)
1163 struct ubus_event_req
*ureq
= container_of(req
, struct ubus_event_req
, nreq
);
1168 int hostapd_ubus_handle_event(struct hostapd_data
*hapd
, struct hostapd_ubus_request
*req
)
1170 struct ubus_banned_client
*ban
;
1171 const char *types
[HOSTAPD_UBUS_TYPE_MAX
] = {
1172 [HOSTAPD_UBUS_PROBE_REQ
] = "probe",
1173 [HOSTAPD_UBUS_AUTH_REQ
] = "auth",
1174 [HOSTAPD_UBUS_ASSOC_REQ
] = "assoc",
1176 const char *type
= "mgmt";
1177 struct ubus_event_req ureq
= {};
1180 if (req
->mgmt_frame
)
1181 addr
= req
->mgmt_frame
->sa
;
1185 ban
= avl_find_element(&hapd
->ubus
.banned
, addr
, ban
, avl
);
1187 return WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA
;
1189 if (!hapd
->ubus
.obj
.has_subscribers
)
1190 return WLAN_STATUS_SUCCESS
;
1192 if (req
->type
< ARRAY_SIZE(types
))
1193 type
= types
[req
->type
];
1195 blob_buf_init(&b
, 0);
1196 blobmsg_add_macaddr(&b
, "address", addr
);
1197 if (req
->mgmt_frame
)
1198 blobmsg_add_macaddr(&b
, "target", req
->mgmt_frame
->da
);
1199 if (req
->ssi_signal
)
1200 blobmsg_add_u32(&b
, "signal", req
->ssi_signal
);
1201 blobmsg_add_u32(&b
, "freq", hapd
->iface
->freq
);
1204 if(req
->elems
->ht_capabilities
)
1206 struct ieee80211_ht_capabilities
*ht_capabilities
;
1207 void *ht_cap
, *ht_cap_mcs_set
, *mcs_set
;
1210 ht_capabilities
= (struct ieee80211_ht_capabilities
*) req
->elems
->ht_capabilities
;
1211 ht_cap
= blobmsg_open_table(&b
, "ht_capabilities");
1212 blobmsg_add_u16(&b
, "ht_capabilities_info", ht_capabilities
->ht_capabilities_info
);
1213 ht_cap_mcs_set
= blobmsg_open_table(&b
, "supported_mcs_set");
1214 blobmsg_add_u16(&b
, "a_mpdu_params", ht_capabilities
->a_mpdu_params
);
1215 blobmsg_add_u16(&b
, "ht_extended_capabilities", ht_capabilities
->ht_extended_capabilities
);
1216 blobmsg_add_u32(&b
, "tx_bf_capability_info", ht_capabilities
->tx_bf_capability_info
);
1217 blobmsg_add_u16(&b
, "asel_capabilities", ht_capabilities
->asel_capabilities
);
1218 mcs_set
= blobmsg_open_array(&b
, "supported_mcs_set");
1219 for (int i
= 0; i
< 16; i
++) {
1220 blobmsg_add_u16(&b
, NULL
, (u16
) ht_capabilities
->supported_mcs_set
[i
]);
1222 blobmsg_close_array(&b
, mcs_set
);
1223 blobmsg_close_table(&b
, ht_cap_mcs_set
);
1224 blobmsg_close_table(&b
, ht_cap
);
1226 if(req
->elems
->vht_capabilities
)
1228 struct ieee80211_vht_capabilities
*vht_capabilities
;
1229 void *vht_cap
, *vht_cap_mcs_set
;
1231 vht_capabilities
= (struct ieee80211_vht_capabilities
*) req
->elems
->vht_capabilities
;
1232 vht_cap
= blobmsg_open_table(&b
, "vht_capabilities");
1233 blobmsg_add_u32(&b
, "vht_capabilities_info", vht_capabilities
->vht_capabilities_info
);
1234 vht_cap_mcs_set
= blobmsg_open_table(&b
, "vht_supported_mcs_set");
1235 blobmsg_add_u16(&b
, "rx_map", vht_capabilities
->vht_supported_mcs_set
.rx_map
);
1236 blobmsg_add_u16(&b
, "rx_highest", vht_capabilities
->vht_supported_mcs_set
.rx_highest
);
1237 blobmsg_add_u16(&b
, "tx_map", vht_capabilities
->vht_supported_mcs_set
.tx_map
);
1238 blobmsg_add_u16(&b
, "tx_highest", vht_capabilities
->vht_supported_mcs_set
.tx_highest
);
1239 blobmsg_close_table(&b
, vht_cap_mcs_set
);
1240 blobmsg_close_table(&b
, vht_cap
);
1244 if (!hapd
->ubus
.notify_response
) {
1245 ubus_notify(ctx
, &hapd
->ubus
.obj
, type
, b
.head
, -1);
1246 return WLAN_STATUS_SUCCESS
;
1249 if (ubus_notify_async(ctx
, &hapd
->ubus
.obj
, type
, b
.head
, &ureq
.nreq
))
1250 return WLAN_STATUS_SUCCESS
;
1252 ureq
.nreq
.status_cb
= ubus_event_cb
;
1253 ubus_complete_request(ctx
, &ureq
.nreq
.req
, 100);
1258 return WLAN_STATUS_SUCCESS
;
1261 void hostapd_ubus_notify(struct hostapd_data
*hapd
, const char *type
, const u8
*addr
)
1263 if (!hapd
->ubus
.obj
.has_subscribers
)
1269 blob_buf_init(&b
, 0);
1270 blobmsg_add_macaddr(&b
, "address", addr
);
1272 ubus_notify(ctx
, &hapd
->ubus
.obj
, type
, b
.head
, -1);
1275 void hostapd_ubus_notify_beacon_report(
1276 struct hostapd_data
*hapd
, const u8
*addr
, u8 token
, u8 rep_mode
,
1277 struct rrm_measurement_beacon_report
*rep
, size_t len
)
1279 if (!hapd
->ubus
.obj
.has_subscribers
)
1285 blob_buf_init(&b
, 0);
1286 blobmsg_add_macaddr(&b
, "address", addr
);
1287 blobmsg_add_u16(&b
, "op-class", rep
->op_class
);
1288 blobmsg_add_u16(&b
, "channel", rep
->channel
);
1289 blobmsg_add_u64(&b
, "start-time", rep
->start_time
);
1290 blobmsg_add_u16(&b
, "duration", rep
->duration
);
1291 blobmsg_add_u16(&b
, "report-info", rep
->report_info
);
1292 blobmsg_add_u16(&b
, "rcpi", rep
->rcpi
);
1293 blobmsg_add_u16(&b
, "rsni", rep
->rsni
);
1294 blobmsg_add_macaddr(&b
, "bssid", rep
->bssid
);
1295 blobmsg_add_u16(&b
, "antenna-id", rep
->antenna_id
);
1296 blobmsg_add_u16(&b
, "parent-tsf", rep
->parent_tsf
);
1298 ubus_notify(ctx
, &hapd
->ubus
.obj
, "beacon-report", b
.head
, -1);