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"
23 static struct ubus_context
*ctx
;
24 static struct blob_buf b
;
27 static inline struct hostapd_data
*get_hapd_from_object(struct ubus_object
*obj
)
29 return container_of(obj
, struct hostapd_data
, ubus
.obj
);
33 struct ubus_banned_client
{
38 static void ubus_receive(int sock
, void *eloop_ctx
, void *sock_ctx
)
40 struct ubus_context
*ctx
= eloop_ctx
;
41 ubus_handle_event(ctx
);
44 static void ubus_reconnect_timeout(void *eloop_data
, void *user_ctx
)
46 if (ubus_reconnect(ctx
, NULL
)) {
47 eloop_register_timeout(1, 0, ubus_reconnect_timeout
, ctx
, NULL
);
51 eloop_register_read_sock(ctx
->sock
.fd
, ubus_receive
, ctx
, NULL
);
54 static void hostapd_ubus_connection_lost(struct ubus_context
*ctx
)
56 eloop_unregister_read_sock(ctx
->sock
.fd
);
57 eloop_register_timeout(1, 0, ubus_reconnect_timeout
, ctx
, NULL
);
60 static bool hostapd_ubus_init(void)
65 ctx
= ubus_connect(NULL
);
69 ctx
->connection_lost
= hostapd_ubus_connection_lost
;
70 eloop_register_read_sock(ctx
->sock
.fd
, ubus_receive
, ctx
, NULL
);
74 static void hostapd_ubus_ref_inc(void)
79 static void hostapd_ubus_ref_dec(void)
88 eloop_unregister_read_sock(ctx
->sock
.fd
);
93 void hostapd_ubus_add_iface(struct hostapd_iface
*iface
)
95 if (!hostapd_ubus_init())
99 void hostapd_ubus_free_iface(struct hostapd_iface
*iface
)
106 hostapd_bss_del_ban(void *eloop_data
, void *user_ctx
)
108 struct ubus_banned_client
*ban
= eloop_data
;
109 struct hostapd_data
*hapd
= user_ctx
;
111 avl_delete(&hapd
->ubus
.banned
, &ban
->avl
);
116 hostapd_bss_ban_client(struct hostapd_data
*hapd
, u8
*addr
, int time
)
118 struct ubus_banned_client
*ban
;
123 ban
= avl_find_element(&hapd
->ubus
.banned
, addr
, ban
, avl
);
128 ban
= os_zalloc(sizeof(*ban
));
129 memcpy(ban
->addr
, addr
, sizeof(ban
->addr
));
130 ban
->avl
.key
= ban
->addr
;
131 avl_insert(&hapd
->ubus
.banned
, &ban
->avl
);
133 eloop_cancel_timeout(hostapd_bss_del_ban
, ban
, hapd
);
135 hostapd_bss_del_ban(ban
, hapd
);
140 eloop_register_timeout(0, time
* 1000, hostapd_bss_del_ban
, ban
, hapd
);
144 hostapd_bss_get_clients(struct ubus_context
*ctx
, struct ubus_object
*obj
,
145 struct ubus_request_data
*req
, const char *method
,
146 struct blob_attr
*msg
)
148 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
149 struct sta_info
*sta
;
152 static const struct {
156 { "auth", WLAN_STA_AUTH
},
157 { "assoc", WLAN_STA_ASSOC
},
158 { "authorized", WLAN_STA_AUTHORIZED
},
159 { "preauth", WLAN_STA_PREAUTH
},
160 { "wds", WLAN_STA_WDS
},
161 { "wmm", WLAN_STA_WMM
},
162 { "ht", WLAN_STA_HT
},
163 { "vht", WLAN_STA_VHT
},
164 { "wps", WLAN_STA_WPS
},
165 { "mfp", WLAN_STA_MFP
},
168 blob_buf_init(&b
, 0);
169 blobmsg_add_u32(&b
, "freq", hapd
->iface
->freq
);
170 list
= blobmsg_open_table(&b
, "clients");
171 for (sta
= hapd
->sta_list
; sta
; sta
= sta
->next
) {
175 sprintf(mac_buf
, MACSTR
, MAC2STR(sta
->addr
));
176 c
= blobmsg_open_table(&b
, mac_buf
);
177 for (i
= 0; i
< ARRAY_SIZE(sta_flags
); i
++)
178 blobmsg_add_u8(&b
, sta_flags
[i
].name
,
179 !!(sta
->flags
& sta_flags
[i
].flag
));
181 r
= blobmsg_open_array(&b
, "rrm");
182 for (i
= 0; i
< ARRAY_SIZE(sta
->rrm_enabled_capa
); i
++)
183 blobmsg_add_u32(&b
, "", sta
->rrm_enabled_capa
[i
]);
184 blobmsg_close_array(&b
, r
);
185 blobmsg_add_u32(&b
, "aid", sta
->aid
);
186 blobmsg_close_table(&b
, c
);
188 blobmsg_close_array(&b
, list
);
189 ubus_send_reply(ctx
, req
, b
.head
);
199 static const struct blobmsg_policy notify_policy
[__NOTIFY_MAX
] = {
200 [NOTIFY_RESPONSE
] = { "notify_response", BLOBMSG_TYPE_INT32
},
204 hostapd_notify_response(struct ubus_context
*ctx
, struct ubus_object
*obj
,
205 struct ubus_request_data
*req
, const char *method
,
206 struct blob_attr
*msg
)
208 struct blob_attr
*tb
[__NOTIFY_MAX
];
209 struct hostapd_data
*hapd
= get_hapd_from_object(obj
);
210 struct wpabuf
*elems
;
214 blobmsg_parse(notify_policy
, __NOTIFY_MAX
, tb
,
215 blob_data(msg
), blob_len(msg
));
217 if (!tb
[NOTIFY_RESPONSE
])
218 return UBUS_STATUS_INVALID_ARGUMENT
;
220 hapd
->ubus
.notify_response
= blobmsg_get_u32(tb
[NOTIFY_RESPONSE
]);
222 return UBUS_STATUS_OK
;
233 static const struct blobmsg_policy del_policy
[__DEL_CLIENT_MAX
] = {
234 [DEL_CLIENT_ADDR
] = { "addr", BLOBMSG_TYPE_STRING
},
235 [DEL_CLIENT_REASON
] = { "reason", BLOBMSG_TYPE_INT32
},
236 [DEL_CLIENT_DEAUTH
] = { "deauth", BLOBMSG_TYPE_INT8
},
237 [DEL_CLIENT_BAN_TIME
] = { "ban_time", BLOBMSG_TYPE_INT32
},
241 hostapd_bss_del_client(struct ubus_context
*ctx
, struct ubus_object
*obj
,
242 struct ubus_request_data
*req
, const char *method
,
243 struct blob_attr
*msg
)
245 struct blob_attr
*tb
[__DEL_CLIENT_MAX
];
246 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
247 struct sta_info
*sta
;
252 blobmsg_parse(del_policy
, __DEL_CLIENT_MAX
, tb
, blob_data(msg
), blob_len(msg
));
254 if (!tb
[DEL_CLIENT_ADDR
])
255 return UBUS_STATUS_INVALID_ARGUMENT
;
257 if (hwaddr_aton(blobmsg_data(tb
[DEL_CLIENT_ADDR
]), addr
))
258 return UBUS_STATUS_INVALID_ARGUMENT
;
260 if (tb
[DEL_CLIENT_REASON
])
261 reason
= blobmsg_get_u32(tb
[DEL_CLIENT_REASON
]);
263 if (tb
[DEL_CLIENT_DEAUTH
])
264 deauth
= blobmsg_get_bool(tb
[DEL_CLIENT_DEAUTH
]);
266 sta
= ap_get_sta(hapd
, addr
);
269 hostapd_drv_sta_deauth(hapd
, addr
, reason
);
270 ap_sta_deauthenticate(hapd
, sta
, reason
);
272 hostapd_drv_sta_disassoc(hapd
, addr
, reason
);
273 ap_sta_disassociate(hapd
, sta
, reason
);
277 if (tb
[DEL_CLIENT_BAN_TIME
])
278 hostapd_bss_ban_client(hapd
, addr
, blobmsg_get_u32(tb
[DEL_CLIENT_BAN_TIME
]));
284 blobmsg_add_macaddr(struct blob_buf
*buf
, const char *name
, const u8
*addr
)
288 s
= blobmsg_alloc_string_buffer(buf
, name
, 20);
289 sprintf(s
, MACSTR
, MAC2STR(addr
));
290 blobmsg_add_string_buffer(buf
);
294 hostapd_bss_list_bans(struct ubus_context
*ctx
, struct ubus_object
*obj
,
295 struct ubus_request_data
*req
, const char *method
,
296 struct blob_attr
*msg
)
298 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
299 struct ubus_banned_client
*ban
;
302 blob_buf_init(&b
, 0);
303 c
= blobmsg_open_array(&b
, "clients");
304 avl_for_each_element(&hapd
->ubus
.banned
, ban
, avl
)
305 blobmsg_add_macaddr(&b
, NULL
, ban
->addr
);
306 blobmsg_close_array(&b
, c
);
307 ubus_send_reply(ctx
, req
, b
.head
);
313 hostapd_bss_wps_start(struct ubus_context
*ctx
, struct ubus_object
*obj
,
314 struct ubus_request_data
*req
, const char *method
,
315 struct blob_attr
*msg
)
318 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
320 rc
= hostapd_wps_button_pushed(hapd
, NULL
);
323 return UBUS_STATUS_NOT_SUPPORTED
;
329 hostapd_bss_wps_cancel(struct ubus_context
*ctx
, struct ubus_object
*obj
,
330 struct ubus_request_data
*req
, const char *method
,
331 struct blob_attr
*msg
)
334 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
336 rc
= hostapd_wps_cancel(hapd
);
339 return UBUS_STATUS_NOT_SUPPORTED
;
345 hostapd_bss_update_beacon(struct ubus_context
*ctx
, struct ubus_object
*obj
,
346 struct ubus_request_data
*req
, const char *method
,
347 struct blob_attr
*msg
)
350 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
352 rc
= ieee802_11_set_beacon(hapd
);
355 return UBUS_STATUS_NOT_SUPPORTED
;
366 static const struct blobmsg_policy csa_policy
[__CSA_MAX
] = {
368 * for now, frequency and beacon count are enough, add more
369 * parameters on demand
371 [CSA_FREQ
] = { "freq", BLOBMSG_TYPE_INT32
},
372 [CSA_BCN_COUNT
] = { "bcn_count", BLOBMSG_TYPE_INT32
},
377 hostapd_switch_chan(struct ubus_context
*ctx
, struct ubus_object
*obj
,
378 struct ubus_request_data
*req
, const char *method
,
379 struct blob_attr
*msg
)
381 struct blob_attr
*tb
[__CSA_MAX
];
382 struct hostapd_data
*hapd
= get_hapd_from_object(obj
);
383 struct csa_settings css
;
385 blobmsg_parse(csa_policy
, __CSA_MAX
, tb
, blob_data(msg
), blob_len(msg
));
388 return UBUS_STATUS_INVALID_ARGUMENT
;
390 memset(&css
, 0, sizeof(css
));
391 css
.freq_params
.freq
= blobmsg_get_u32(tb
[CSA_FREQ
]);
392 if (tb
[CSA_BCN_COUNT
])
393 css
.cs_count
= blobmsg_get_u32(tb
[CSA_BCN_COUNT
]);
395 if (hostapd_switch_channel(hapd
, &css
) != 0)
396 return UBUS_STATUS_NOT_SUPPORTED
;
397 return UBUS_STATUS_OK
;
403 __VENDOR_ELEMENTS_MAX
406 static const struct blobmsg_policy ve_policy
[__VENDOR_ELEMENTS_MAX
] = {
407 /* vendor elements are provided as hex-string */
408 [VENDOR_ELEMENTS
] = { "vendor_elements", BLOBMSG_TYPE_STRING
},
412 hostapd_vendor_elements(struct ubus_context
*ctx
, struct ubus_object
*obj
,
413 struct ubus_request_data
*req
, const char *method
,
414 struct blob_attr
*msg
)
416 struct blob_attr
*tb
[__VENDOR_ELEMENTS_MAX
];
417 struct hostapd_data
*hapd
= get_hapd_from_object(obj
);
418 struct hostapd_bss_config
*bss
= hapd
->conf
;
419 struct wpabuf
*elems
;
423 blobmsg_parse(ve_policy
, __VENDOR_ELEMENTS_MAX
, tb
,
424 blob_data(msg
), blob_len(msg
));
426 if (!tb
[VENDOR_ELEMENTS
])
427 return UBUS_STATUS_INVALID_ARGUMENT
;
429 pos
= blobmsg_data(tb
[VENDOR_ELEMENTS
]);
430 len
= os_strlen(pos
);
432 return UBUS_STATUS_INVALID_ARGUMENT
;
436 wpabuf_free(bss
->vendor_elements
);
437 bss
->vendor_elements
= NULL
;
441 elems
= wpabuf_alloc(len
);
445 if (hexstr2bin(pos
, wpabuf_put(elems
, len
), len
)) {
447 return UBUS_STATUS_INVALID_ARGUMENT
;
450 wpabuf_free(bss
->vendor_elements
);
451 bss
->vendor_elements
= elems
;
453 /* update beacons if vendor elements were set successfully */
454 if (ieee802_11_update_beacons(hapd
->iface
) != 0)
455 return UBUS_STATUS_NOT_SUPPORTED
;
456 return UBUS_STATUS_OK
;
460 hostapd_rrm_print_nr(struct hostapd_neighbor_entry
*nr
)
466 blobmsg_printf(&b
, "", MACSTR
, MAC2STR(nr
->bssid
));
468 str
= blobmsg_alloc_string_buffer(&b
, "", nr
->ssid
.ssid_len
+ 1);
469 memcpy(str
, nr
->ssid
.ssid
, nr
->ssid
.ssid_len
);
470 str
[nr
->ssid
.ssid_len
] = 0;
471 blobmsg_add_string_buffer(&b
);
473 len
= wpabuf_len(nr
->nr
);
474 str
= blobmsg_alloc_string_buffer(&b
, "", 2 * len
+ 1);
475 wpa_snprintf_hex(str
, 2 * len
+ 1, wpabuf_head_u8(nr
->nr
), len
);
476 blobmsg_add_string_buffer(&b
);
480 BSS_MGMT_EN_NEIGHBOR
,
483 BSS_MGMT_EN_BSS_TRANSITION
,
489 __hostapd_bss_mgmt_enable_f(struct hostapd_data
*hapd
, int flag
)
491 struct hostapd_bss_config
*bss
= hapd
->conf
;
495 case BSS_MGMT_EN_NEIGHBOR
:
496 if (bss
->radio_measurements
[0] &
497 WLAN_RRM_CAPS_NEIGHBOR_REPORT
)
500 bss
->radio_measurements
[0] |=
501 WLAN_RRM_CAPS_NEIGHBOR_REPORT
;
502 hostapd_set_own_neighbor_report(hapd
);
504 case BSS_MGMT_EN_BEACON
:
505 flags
= WLAN_RRM_CAPS_BEACON_REPORT_PASSIVE
|
506 WLAN_RRM_CAPS_BEACON_REPORT_ACTIVE
|
507 WLAN_RRM_CAPS_BEACON_REPORT_TABLE
;
509 if (bss
->radio_measurements
[0] & flags
== flags
)
512 bss
->radio_measurements
[0] |= (u8
) flags
;
515 case BSS_MGMT_EN_BSS_TRANSITION
:
516 if (bss
->bss_transition
)
519 bss
->bss_transition
= 1;
526 __hostapd_bss_mgmt_enable(struct hostapd_data
*hapd
, uint32_t flags
)
531 for (i
= 0; i
< __BSS_MGMT_EN_MAX
; i
++) {
532 if (!(flags
& (1 << i
)))
535 update
|= __hostapd_bss_mgmt_enable_f(hapd
, i
);
539 ieee802_11_update_beacons(hapd
->iface
);
543 static const struct blobmsg_policy bss_mgmt_enable_policy
[__BSS_MGMT_EN_MAX
] = {
544 [BSS_MGMT_EN_NEIGHBOR
] = { "neighbor_report", BLOBMSG_TYPE_BOOL
},
545 [BSS_MGMT_EN_BEACON
] = { "beacon_report", BLOBMSG_TYPE_BOOL
},
547 [BSS_MGMT_EN_BSS_TRANSITION
] = { "bss_transition", BLOBMSG_TYPE_BOOL
},
552 hostapd_bss_mgmt_enable(struct ubus_context
*ctx
, struct ubus_object
*obj
,
553 struct ubus_request_data
*req
, const char *method
,
554 struct blob_attr
*msg
)
557 struct hostapd_data
*hapd
= get_hapd_from_object(obj
);
558 struct blob_attr
*tb
[__BSS_MGMT_EN_MAX
];
559 struct blob_attr
*cur
;
562 bool neigh
= false, beacon
= false;
564 blobmsg_parse(bss_mgmt_enable_policy
, __BSS_MGMT_EN_MAX
, tb
, blob_data(msg
), blob_len(msg
));
566 for (i
= 0; i
< ARRAY_SIZE(tb
); i
++) {
567 if (!tb
[i
] || !blobmsg_get_bool(tb
[i
]))
573 __hostapd_bss_mgmt_enable(hapd
, flags
);
578 hostapd_rrm_nr_enable(struct hostapd_data
*hapd
)
580 __hostapd_bss_mgmt_enable(hapd
, 1 << BSS_MGMT_EN_NEIGHBOR
);
584 hostapd_rrm_nr_get_own(struct ubus_context
*ctx
, struct ubus_object
*obj
,
585 struct ubus_request_data
*req
, const char *method
,
586 struct blob_attr
*msg
)
588 struct hostapd_data
*hapd
= get_hapd_from_object(obj
);
589 struct hostapd_neighbor_entry
*nr
;
592 hostapd_rrm_nr_enable(hapd
);
594 nr
= hostapd_neighbor_get(hapd
, hapd
->own_addr
, NULL
);
596 return UBUS_STATUS_NOT_FOUND
;
598 blob_buf_init(&b
, 0);
600 c
= blobmsg_open_array(&b
, "value");
601 hostapd_rrm_print_nr(nr
);
602 blobmsg_close_array(&b
, c
);
604 ubus_send_reply(ctx
, req
, b
.head
);
610 hostapd_rrm_nr_list(struct ubus_context
*ctx
, struct ubus_object
*obj
,
611 struct ubus_request_data
*req
, const char *method
,
612 struct blob_attr
*msg
)
614 struct hostapd_data
*hapd
= get_hapd_from_object(obj
);
615 struct hostapd_neighbor_entry
*nr
;
618 hostapd_rrm_nr_enable(hapd
);
619 blob_buf_init(&b
, 0);
621 c
= blobmsg_open_array(&b
, "list");
622 dl_list_for_each(nr
, &hapd
->nr_db
, struct hostapd_neighbor_entry
, list
) {
625 if (!memcmp(nr
->bssid
, hapd
->own_addr
, ETH_ALEN
))
628 cur
= blobmsg_open_array(&b
, NULL
);
629 hostapd_rrm_print_nr(nr
);
630 blobmsg_close_array(&b
, cur
);
632 blobmsg_close_array(&b
, c
);
634 ubus_send_reply(ctx
, req
, b
.head
);
644 static const struct blobmsg_policy nr_set_policy
[__NR_SET_LIST_MAX
] = {
645 [NR_SET_LIST
] = { "list", BLOBMSG_TYPE_ARRAY
},
650 hostapd_rrm_nr_clear(struct hostapd_data
*hapd
)
652 struct hostapd_neighbor_entry
*nr
;
655 dl_list_for_each(nr
, &hapd
->nr_db
, struct hostapd_neighbor_entry
, list
) {
656 if (!memcmp(nr
->bssid
, hapd
->own_addr
, ETH_ALEN
))
659 hostapd_neighbor_remove(hapd
, nr
->bssid
, &nr
->ssid
);
665 hostapd_rrm_nr_set(struct ubus_context
*ctx
, struct ubus_object
*obj
,
666 struct ubus_request_data
*req
, const char *method
,
667 struct blob_attr
*msg
)
669 static const struct blobmsg_policy nr_e_policy
[] = {
670 { .type
= BLOBMSG_TYPE_STRING
},
671 { .type
= BLOBMSG_TYPE_STRING
},
672 { .type
= BLOBMSG_TYPE_STRING
},
674 struct hostapd_data
*hapd
= get_hapd_from_object(obj
);
675 struct blob_attr
*tb_l
[__NR_SET_LIST_MAX
];
676 struct blob_attr
*tb
[ARRAY_SIZE(nr_e_policy
)];
677 struct blob_attr
*cur
;
681 hostapd_rrm_nr_enable(hapd
);
683 blobmsg_parse(nr_set_policy
, __NR_SET_LIST_MAX
, tb_l
, blob_data(msg
), blob_len(msg
));
684 if (!tb_l
[NR_SET_LIST
])
685 return UBUS_STATUS_INVALID_ARGUMENT
;
687 hostapd_rrm_nr_clear(hapd
);
688 blobmsg_for_each_attr(cur
, tb_l
[NR_SET_LIST
], rem
) {
689 struct wpa_ssid_value ssid
;
694 blobmsg_parse_array(nr_e_policy
, ARRAY_SIZE(nr_e_policy
), tb
, blobmsg_data(cur
), blobmsg_data_len(cur
));
695 if (!tb
[0] || !tb
[1] || !tb
[2])
698 s
= blobmsg_get_string(tb
[0]);
699 if (hwaddr_aton(s
, bssid
))
702 s
= blobmsg_get_string(tb
[1]);
703 ssid
.ssid_len
= strlen(s
);
704 if (ssid
.ssid_len
> sizeof(ssid
.ssid
))
707 memcpy(&ssid
, s
, ssid
.ssid_len
);
708 data
= wpabuf_parse_bin(blobmsg_get_string(tb
[2]));
712 hostapd_neighbor_set(hapd
, bssid
, &ssid
, data
, NULL
, NULL
, 0);
717 ret
= UBUS_STATUS_INVALID_ARGUMENT
;
734 static const struct blobmsg_policy beacon_req_policy
[__BEACON_REQ_MAX
] = {
735 [BEACON_REQ_ADDR
] = { "addr", BLOBMSG_TYPE_STRING
},
736 [BEACON_REQ_OP_CLASS
] { "op_class", BLOBMSG_TYPE_INT32
},
737 [BEACON_REQ_CHANNEL
] { "channel", BLOBMSG_TYPE_INT32
},
738 [BEACON_REQ_DURATION
] { "duration", BLOBMSG_TYPE_INT32
},
739 [BEACON_REQ_MODE
] { "mode", BLOBMSG_TYPE_INT32
},
740 [BEACON_REQ_BSSID
] { "bssid", BLOBMSG_TYPE_STRING
},
741 [BEACON_REQ_SSID
] { "ssid", BLOBMSG_TYPE_STRING
},
745 hostapd_rrm_beacon_req(struct ubus_context
*ctx
, struct ubus_object
*obj
,
746 struct ubus_request_data
*ureq
, const char *method
,
747 struct blob_attr
*msg
)
749 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
750 struct blob_attr
*tb
[__BEACON_REQ_MAX
];
751 struct blob_attr
*cur
;
753 u8 bssid
[ETH_ALEN
] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
758 blobmsg_parse(beacon_req_policy
, __BEACON_REQ_MAX
, tb
, blob_data(msg
), blob_len(msg
));
760 if (!tb
[BEACON_REQ_ADDR
] || !tb
[BEACON_REQ_MODE
] || !tb
[BEACON_REQ_DURATION
] ||
761 !tb
[BEACON_REQ_OP_CLASS
] || !tb
[BEACON_REQ_CHANNEL
])
762 return UBUS_STATUS_INVALID_ARGUMENT
;
764 if (tb
[BEACON_REQ_SSID
])
765 buf_len
+= blobmsg_data_len(tb
[BEACON_REQ_SSID
]) + 2 - 1;
767 mode
= blobmsg_get_u32(tb
[BEACON_REQ_MODE
]);
768 if (hwaddr_aton(blobmsg_data(tb
[BEACON_REQ_ADDR
]), addr
))
769 return UBUS_STATUS_INVALID_ARGUMENT
;
771 if (tb
[BEACON_REQ_BSSID
] &&
772 hwaddr_aton(blobmsg_data(tb
[BEACON_REQ_BSSID
]), bssid
))
773 return UBUS_STATUS_INVALID_ARGUMENT
;
775 req
= wpabuf_alloc(buf_len
);
777 return UBUS_STATUS_UNKNOWN_ERROR
;
779 /* 1: regulatory class */
780 wpabuf_put_u8(req
, blobmsg_get_u32(tb
[BEACON_REQ_OP_CLASS
]));
782 /* 2: channel number */
783 wpabuf_put_u8(req
, blobmsg_get_u32(tb
[BEACON_REQ_CHANNEL
]));
785 /* 3-4: randomization interval */
786 wpabuf_put_le16(req
, 0);
789 wpabuf_put_le16(req
, blobmsg_get_u32(tb
[BEACON_REQ_DURATION
]));
792 wpabuf_put_u8(req
, blobmsg_get_u32(tb
[BEACON_REQ_MODE
]));
795 wpabuf_put_data(req
, bssid
, ETH_ALEN
);
797 if ((cur
= tb
[BEACON_REQ_SSID
]) != NULL
) {
798 wpabuf_put_u8(req
, WLAN_EID_SSID
);
799 wpabuf_put_u8(req
, blobmsg_data_len(cur
) - 1);
800 wpabuf_put_data(req
, blobmsg_data(cur
), blobmsg_data_len(cur
) - 1);
803 ret
= hostapd_send_beacon_req(hapd
, addr
, 0, req
);
811 static const struct ubus_method bss_methods
[] = {
812 UBUS_METHOD_NOARG("get_clients", hostapd_bss_get_clients
),
813 UBUS_METHOD("del_client", hostapd_bss_del_client
, del_policy
),
814 UBUS_METHOD_NOARG("list_bans", hostapd_bss_list_bans
),
815 UBUS_METHOD_NOARG("wps_start", hostapd_bss_wps_start
),
816 UBUS_METHOD_NOARG("wps_cancel", hostapd_bss_wps_cancel
),
817 UBUS_METHOD_NOARG("update_beacon", hostapd_bss_update_beacon
),
819 UBUS_METHOD("switch_chan", hostapd_switch_chan
, csa_policy
),
821 UBUS_METHOD("set_vendor_elements", hostapd_vendor_elements
, ve_policy
),
822 UBUS_METHOD("notify_response", hostapd_notify_response
, notify_policy
),
823 UBUS_METHOD("bss_mgmt_enable", hostapd_bss_mgmt_enable
, bss_mgmt_enable_policy
),
824 UBUS_METHOD_NOARG("rrm_nr_get_own", hostapd_rrm_nr_get_own
),
825 UBUS_METHOD_NOARG("rrm_nr_list", hostapd_rrm_nr_list
),
826 UBUS_METHOD("rrm_nr_set", hostapd_rrm_nr_set
, nr_set_policy
),
827 UBUS_METHOD("rrm_beacon_req", hostapd_rrm_beacon_req
, beacon_req_policy
),
830 static struct ubus_object_type bss_object_type
=
831 UBUS_OBJECT_TYPE("hostapd_bss", bss_methods
);
833 static int avl_compare_macaddr(const void *k1
, const void *k2
, void *ptr
)
835 return memcmp(k1
, k2
, ETH_ALEN
);
838 void hostapd_ubus_add_bss(struct hostapd_data
*hapd
)
840 struct ubus_object
*obj
= &hapd
->ubus
.obj
;
844 if (!hostapd_ubus_init())
847 if (asprintf(&name
, "hostapd.%s", hapd
->conf
->iface
) < 0)
850 avl_init(&hapd
->ubus
.banned
, avl_compare_macaddr
, false, NULL
);
852 obj
->type
= &bss_object_type
;
853 obj
->methods
= bss_object_type
.methods
;
854 obj
->n_methods
= bss_object_type
.n_methods
;
855 ret
= ubus_add_object(ctx
, obj
);
856 hostapd_ubus_ref_inc();
859 void hostapd_ubus_free_bss(struct hostapd_data
*hapd
)
861 struct ubus_object
*obj
= &hapd
->ubus
.obj
;
862 char *name
= (char *) obj
->name
;
868 ubus_remove_object(ctx
, obj
);
869 hostapd_ubus_ref_dec();
875 struct ubus_event_req
{
876 struct ubus_notify_request nreq
;
881 ubus_event_cb(struct ubus_notify_request
*req
, int idx
, int ret
)
883 struct ubus_event_req
*ureq
= container_of(req
, struct ubus_event_req
, nreq
);
888 int hostapd_ubus_handle_event(struct hostapd_data
*hapd
, struct hostapd_ubus_request
*req
)
890 struct ubus_banned_client
*ban
;
891 const char *types
[HOSTAPD_UBUS_TYPE_MAX
] = {
892 [HOSTAPD_UBUS_PROBE_REQ
] = "probe",
893 [HOSTAPD_UBUS_AUTH_REQ
] = "auth",
894 [HOSTAPD_UBUS_ASSOC_REQ
] = "assoc",
896 const char *type
= "mgmt";
897 struct ubus_event_req ureq
= {};
901 addr
= req
->mgmt_frame
->sa
;
905 ban
= avl_find_element(&hapd
->ubus
.banned
, addr
, ban
, avl
);
907 return WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA
;
909 if (!hapd
->ubus
.obj
.has_subscribers
)
910 return WLAN_STATUS_SUCCESS
;
912 if (req
->type
< ARRAY_SIZE(types
))
913 type
= types
[req
->type
];
915 blob_buf_init(&b
, 0);
916 blobmsg_add_macaddr(&b
, "address", addr
);
918 blobmsg_add_macaddr(&b
, "target", req
->mgmt_frame
->da
);
920 blobmsg_add_u32(&b
, "signal", req
->frame_info
->ssi_signal
);
921 blobmsg_add_u32(&b
, "freq", hapd
->iface
->freq
);
923 if (!hapd
->ubus
.notify_response
) {
924 ubus_notify(ctx
, &hapd
->ubus
.obj
, type
, b
.head
, -1);
925 return WLAN_STATUS_SUCCESS
;
928 if (ubus_notify_async(ctx
, &hapd
->ubus
.obj
, type
, b
.head
, &ureq
.nreq
))
929 return WLAN_STATUS_SUCCESS
;
931 ureq
.nreq
.status_cb
= ubus_event_cb
;
932 ubus_complete_request(ctx
, &ureq
.nreq
.req
, 100);
937 return WLAN_STATUS_SUCCESS
;
940 void hostapd_ubus_notify(struct hostapd_data
*hapd
, const char *type
, const u8
*addr
)
942 if (!hapd
->ubus
.obj
.has_subscribers
)
948 blob_buf_init(&b
, 0);
949 blobmsg_add_macaddr(&b
, "address", addr
);
951 ubus_notify(ctx
, &hapd
->ubus
.obj
, type
, b
.head
, -1);