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 hostapd_data
*get_hapd_from_object(struct ubus_object
*obj
)
31 return container_of(obj
, struct hostapd_data
, ubus
.obj
);
35 struct ubus_banned_client
{
40 static void ubus_receive(int sock
, void *eloop_ctx
, void *sock_ctx
)
42 struct ubus_context
*ctx
= eloop_ctx
;
43 ubus_handle_event(ctx
);
46 static void ubus_reconnect_timeout(void *eloop_data
, void *user_ctx
)
48 if (ubus_reconnect(ctx
, NULL
)) {
49 eloop_register_timeout(1, 0, ubus_reconnect_timeout
, ctx
, NULL
);
53 eloop_register_read_sock(ctx
->sock
.fd
, ubus_receive
, ctx
, NULL
);
56 static void hostapd_ubus_connection_lost(struct ubus_context
*ctx
)
58 eloop_unregister_read_sock(ctx
->sock
.fd
);
59 eloop_register_timeout(1, 0, ubus_reconnect_timeout
, ctx
, NULL
);
62 static bool hostapd_ubus_init(void)
67 ctx
= ubus_connect(NULL
);
71 ctx
->connection_lost
= hostapd_ubus_connection_lost
;
72 eloop_register_read_sock(ctx
->sock
.fd
, ubus_receive
, ctx
, NULL
);
76 static void hostapd_ubus_ref_inc(void)
81 static void hostapd_ubus_ref_dec(void)
90 eloop_unregister_read_sock(ctx
->sock
.fd
);
95 void hostapd_ubus_add_iface(struct hostapd_iface
*iface
)
97 if (!hostapd_ubus_init())
101 void hostapd_ubus_free_iface(struct hostapd_iface
*iface
)
108 hostapd_bss_del_ban(void *eloop_data
, void *user_ctx
)
110 struct ubus_banned_client
*ban
= eloop_data
;
111 struct hostapd_data
*hapd
= user_ctx
;
113 avl_delete(&hapd
->ubus
.banned
, &ban
->avl
);
118 hostapd_bss_ban_client(struct hostapd_data
*hapd
, u8
*addr
, int time
)
120 struct ubus_banned_client
*ban
;
125 ban
= avl_find_element(&hapd
->ubus
.banned
, addr
, ban
, avl
);
130 ban
= os_zalloc(sizeof(*ban
));
131 memcpy(ban
->addr
, addr
, sizeof(ban
->addr
));
132 ban
->avl
.key
= ban
->addr
;
133 avl_insert(&hapd
->ubus
.banned
, &ban
->avl
);
135 eloop_cancel_timeout(hostapd_bss_del_ban
, ban
, hapd
);
137 hostapd_bss_del_ban(ban
, hapd
);
142 eloop_register_timeout(0, time
* 1000, hostapd_bss_del_ban
, ban
, hapd
);
146 hostapd_bss_get_clients(struct ubus_context
*ctx
, struct ubus_object
*obj
,
147 struct ubus_request_data
*req
, const char *method
,
148 struct blob_attr
*msg
)
150 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
151 struct sta_info
*sta
;
154 static const struct {
158 { "auth", WLAN_STA_AUTH
},
159 { "assoc", WLAN_STA_ASSOC
},
160 { "authorized", WLAN_STA_AUTHORIZED
},
161 { "preauth", WLAN_STA_PREAUTH
},
162 { "wds", WLAN_STA_WDS
},
163 { "wmm", WLAN_STA_WMM
},
164 { "ht", WLAN_STA_HT
},
165 { "vht", WLAN_STA_VHT
},
166 { "wps", WLAN_STA_WPS
},
167 { "mfp", WLAN_STA_MFP
},
170 blob_buf_init(&b
, 0);
171 blobmsg_add_u32(&b
, "freq", hapd
->iface
->freq
);
172 list
= blobmsg_open_table(&b
, "clients");
173 for (sta
= hapd
->sta_list
; sta
; sta
= sta
->next
) {
177 sprintf(mac_buf
, MACSTR
, MAC2STR(sta
->addr
));
178 c
= blobmsg_open_table(&b
, mac_buf
);
179 for (i
= 0; i
< ARRAY_SIZE(sta_flags
); i
++)
180 blobmsg_add_u8(&b
, sta_flags
[i
].name
,
181 !!(sta
->flags
& sta_flags
[i
].flag
));
183 r
= blobmsg_open_array(&b
, "rrm");
184 for (i
= 0; i
< ARRAY_SIZE(sta
->rrm_enabled_capa
); i
++)
185 blobmsg_add_u32(&b
, "", sta
->rrm_enabled_capa
[i
]);
186 blobmsg_close_array(&b
, r
);
187 blobmsg_add_u32(&b
, "aid", sta
->aid
);
188 #ifdef CONFIG_TAXONOMY
189 r
= blobmsg_alloc_string_buffer(&b
, "signature", 1024);
190 if (retrieve_sta_taxonomy(hapd
, sta
, r
, 1024) > 0)
191 blobmsg_add_string_buffer(&b
);
193 blobmsg_close_table(&b
, c
);
195 blobmsg_close_array(&b
, list
);
196 ubus_send_reply(ctx
, req
, b
.head
);
202 hostapd_bss_get_features(struct ubus_context
*ctx
, struct ubus_object
*obj
,
203 struct ubus_request_data
*req
, const char *method
,
204 struct blob_attr
*msg
)
206 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
208 blob_buf_init(&b
, 0);
209 blobmsg_add_u8(&b
, "ht_supported", ht_supported(hapd
->iface
->hw_features
));
210 blobmsg_add_u8(&b
, "vht_supported", vht_supported(hapd
->iface
->hw_features
));
211 ubus_send_reply(ctx
, req
, b
.head
);
221 static const struct blobmsg_policy notify_policy
[__NOTIFY_MAX
] = {
222 [NOTIFY_RESPONSE
] = { "notify_response", BLOBMSG_TYPE_INT32
},
226 hostapd_notify_response(struct ubus_context
*ctx
, struct ubus_object
*obj
,
227 struct ubus_request_data
*req
, const char *method
,
228 struct blob_attr
*msg
)
230 struct blob_attr
*tb
[__NOTIFY_MAX
];
231 struct hostapd_data
*hapd
= get_hapd_from_object(obj
);
232 struct wpabuf
*elems
;
236 blobmsg_parse(notify_policy
, __NOTIFY_MAX
, tb
,
237 blob_data(msg
), blob_len(msg
));
239 if (!tb
[NOTIFY_RESPONSE
])
240 return UBUS_STATUS_INVALID_ARGUMENT
;
242 hapd
->ubus
.notify_response
= blobmsg_get_u32(tb
[NOTIFY_RESPONSE
]);
244 return UBUS_STATUS_OK
;
255 static const struct blobmsg_policy del_policy
[__DEL_CLIENT_MAX
] = {
256 [DEL_CLIENT_ADDR
] = { "addr", BLOBMSG_TYPE_STRING
},
257 [DEL_CLIENT_REASON
] = { "reason", BLOBMSG_TYPE_INT32
},
258 [DEL_CLIENT_DEAUTH
] = { "deauth", BLOBMSG_TYPE_INT8
},
259 [DEL_CLIENT_BAN_TIME
] = { "ban_time", BLOBMSG_TYPE_INT32
},
263 hostapd_bss_del_client(struct ubus_context
*ctx
, struct ubus_object
*obj
,
264 struct ubus_request_data
*req
, const char *method
,
265 struct blob_attr
*msg
)
267 struct blob_attr
*tb
[__DEL_CLIENT_MAX
];
268 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
269 struct sta_info
*sta
;
274 blobmsg_parse(del_policy
, __DEL_CLIENT_MAX
, tb
, blob_data(msg
), blob_len(msg
));
276 if (!tb
[DEL_CLIENT_ADDR
])
277 return UBUS_STATUS_INVALID_ARGUMENT
;
279 if (hwaddr_aton(blobmsg_data(tb
[DEL_CLIENT_ADDR
]), addr
))
280 return UBUS_STATUS_INVALID_ARGUMENT
;
282 if (tb
[DEL_CLIENT_REASON
])
283 reason
= blobmsg_get_u32(tb
[DEL_CLIENT_REASON
]);
285 if (tb
[DEL_CLIENT_DEAUTH
])
286 deauth
= blobmsg_get_bool(tb
[DEL_CLIENT_DEAUTH
]);
288 sta
= ap_get_sta(hapd
, addr
);
291 hostapd_drv_sta_deauth(hapd
, addr
, reason
);
292 ap_sta_deauthenticate(hapd
, sta
, reason
);
294 hostapd_drv_sta_disassoc(hapd
, addr
, reason
);
295 ap_sta_disassociate(hapd
, sta
, reason
);
299 if (tb
[DEL_CLIENT_BAN_TIME
])
300 hostapd_bss_ban_client(hapd
, addr
, blobmsg_get_u32(tb
[DEL_CLIENT_BAN_TIME
]));
306 blobmsg_add_macaddr(struct blob_buf
*buf
, const char *name
, const u8
*addr
)
310 s
= blobmsg_alloc_string_buffer(buf
, name
, 20);
311 sprintf(s
, MACSTR
, MAC2STR(addr
));
312 blobmsg_add_string_buffer(buf
);
316 hostapd_bss_list_bans(struct ubus_context
*ctx
, struct ubus_object
*obj
,
317 struct ubus_request_data
*req
, const char *method
,
318 struct blob_attr
*msg
)
320 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
321 struct ubus_banned_client
*ban
;
324 blob_buf_init(&b
, 0);
325 c
= blobmsg_open_array(&b
, "clients");
326 avl_for_each_element(&hapd
->ubus
.banned
, ban
, avl
)
327 blobmsg_add_macaddr(&b
, NULL
, ban
->addr
);
328 blobmsg_close_array(&b
, c
);
329 ubus_send_reply(ctx
, req
, b
.head
);
335 hostapd_bss_wps_start(struct ubus_context
*ctx
, struct ubus_object
*obj
,
336 struct ubus_request_data
*req
, const char *method
,
337 struct blob_attr
*msg
)
340 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
342 rc
= hostapd_wps_button_pushed(hapd
, NULL
);
345 return UBUS_STATUS_NOT_SUPPORTED
;
351 hostapd_bss_wps_cancel(struct ubus_context
*ctx
, struct ubus_object
*obj
,
352 struct ubus_request_data
*req
, const char *method
,
353 struct blob_attr
*msg
)
356 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
358 rc
= hostapd_wps_cancel(hapd
);
361 return UBUS_STATUS_NOT_SUPPORTED
;
367 hostapd_bss_update_beacon(struct ubus_context
*ctx
, struct ubus_object
*obj
,
368 struct ubus_request_data
*req
, const char *method
,
369 struct blob_attr
*msg
)
372 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
374 rc
= ieee802_11_set_beacon(hapd
);
377 return UBUS_STATUS_NOT_SUPPORTED
;
388 static const struct blobmsg_policy csa_policy
[__CSA_MAX
] = {
390 * for now, frequency and beacon count are enough, add more
391 * parameters on demand
393 [CSA_FREQ
] = { "freq", BLOBMSG_TYPE_INT32
},
394 [CSA_BCN_COUNT
] = { "bcn_count", BLOBMSG_TYPE_INT32
},
399 hostapd_switch_chan(struct ubus_context
*ctx
, struct ubus_object
*obj
,
400 struct ubus_request_data
*req
, const char *method
,
401 struct blob_attr
*msg
)
403 struct blob_attr
*tb
[__CSA_MAX
];
404 struct hostapd_data
*hapd
= get_hapd_from_object(obj
);
405 struct csa_settings css
;
407 blobmsg_parse(csa_policy
, __CSA_MAX
, tb
, blob_data(msg
), blob_len(msg
));
410 return UBUS_STATUS_INVALID_ARGUMENT
;
412 memset(&css
, 0, sizeof(css
));
413 css
.freq_params
.freq
= blobmsg_get_u32(tb
[CSA_FREQ
]);
414 if (tb
[CSA_BCN_COUNT
])
415 css
.cs_count
= blobmsg_get_u32(tb
[CSA_BCN_COUNT
]);
417 if (hostapd_switch_channel(hapd
, &css
) != 0)
418 return UBUS_STATUS_NOT_SUPPORTED
;
419 return UBUS_STATUS_OK
;
425 __VENDOR_ELEMENTS_MAX
428 static const struct blobmsg_policy ve_policy
[__VENDOR_ELEMENTS_MAX
] = {
429 /* vendor elements are provided as hex-string */
430 [VENDOR_ELEMENTS
] = { "vendor_elements", BLOBMSG_TYPE_STRING
},
434 hostapd_vendor_elements(struct ubus_context
*ctx
, struct ubus_object
*obj
,
435 struct ubus_request_data
*req
, const char *method
,
436 struct blob_attr
*msg
)
438 struct blob_attr
*tb
[__VENDOR_ELEMENTS_MAX
];
439 struct hostapd_data
*hapd
= get_hapd_from_object(obj
);
440 struct hostapd_bss_config
*bss
= hapd
->conf
;
441 struct wpabuf
*elems
;
445 blobmsg_parse(ve_policy
, __VENDOR_ELEMENTS_MAX
, tb
,
446 blob_data(msg
), blob_len(msg
));
448 if (!tb
[VENDOR_ELEMENTS
])
449 return UBUS_STATUS_INVALID_ARGUMENT
;
451 pos
= blobmsg_data(tb
[VENDOR_ELEMENTS
]);
452 len
= os_strlen(pos
);
454 return UBUS_STATUS_INVALID_ARGUMENT
;
458 wpabuf_free(bss
->vendor_elements
);
459 bss
->vendor_elements
= NULL
;
463 elems
= wpabuf_alloc(len
);
467 if (hexstr2bin(pos
, wpabuf_put(elems
, len
), len
)) {
469 return UBUS_STATUS_INVALID_ARGUMENT
;
472 wpabuf_free(bss
->vendor_elements
);
473 bss
->vendor_elements
= elems
;
475 /* update beacons if vendor elements were set successfully */
476 if (ieee802_11_update_beacons(hapd
->iface
) != 0)
477 return UBUS_STATUS_NOT_SUPPORTED
;
478 return UBUS_STATUS_OK
;
482 hostapd_rrm_print_nr(struct hostapd_neighbor_entry
*nr
)
488 blobmsg_printf(&b
, "", MACSTR
, MAC2STR(nr
->bssid
));
490 str
= blobmsg_alloc_string_buffer(&b
, "", nr
->ssid
.ssid_len
+ 1);
491 memcpy(str
, nr
->ssid
.ssid
, nr
->ssid
.ssid_len
);
492 str
[nr
->ssid
.ssid_len
] = 0;
493 blobmsg_add_string_buffer(&b
);
495 len
= wpabuf_len(nr
->nr
);
496 str
= blobmsg_alloc_string_buffer(&b
, "", 2 * len
+ 1);
497 wpa_snprintf_hex(str
, 2 * len
+ 1, wpabuf_head_u8(nr
->nr
), len
);
498 blobmsg_add_string_buffer(&b
);
502 BSS_MGMT_EN_NEIGHBOR
,
505 BSS_MGMT_EN_BSS_TRANSITION
,
511 __hostapd_bss_mgmt_enable_f(struct hostapd_data
*hapd
, int flag
)
513 struct hostapd_bss_config
*bss
= hapd
->conf
;
517 case BSS_MGMT_EN_NEIGHBOR
:
518 if (bss
->radio_measurements
[0] &
519 WLAN_RRM_CAPS_NEIGHBOR_REPORT
)
522 bss
->radio_measurements
[0] |=
523 WLAN_RRM_CAPS_NEIGHBOR_REPORT
;
524 hostapd_set_own_neighbor_report(hapd
);
526 case BSS_MGMT_EN_BEACON
:
527 flags
= WLAN_RRM_CAPS_BEACON_REPORT_PASSIVE
|
528 WLAN_RRM_CAPS_BEACON_REPORT_ACTIVE
|
529 WLAN_RRM_CAPS_BEACON_REPORT_TABLE
;
531 if (bss
->radio_measurements
[0] & flags
== flags
)
534 bss
->radio_measurements
[0] |= (u8
) flags
;
537 case BSS_MGMT_EN_BSS_TRANSITION
:
538 if (bss
->bss_transition
)
541 bss
->bss_transition
= 1;
548 __hostapd_bss_mgmt_enable(struct hostapd_data
*hapd
, uint32_t flags
)
553 for (i
= 0; i
< __BSS_MGMT_EN_MAX
; i
++) {
554 if (!(flags
& (1 << i
)))
557 update
|= __hostapd_bss_mgmt_enable_f(hapd
, i
);
561 ieee802_11_update_beacons(hapd
->iface
);
565 static const struct blobmsg_policy bss_mgmt_enable_policy
[__BSS_MGMT_EN_MAX
] = {
566 [BSS_MGMT_EN_NEIGHBOR
] = { "neighbor_report", BLOBMSG_TYPE_BOOL
},
567 [BSS_MGMT_EN_BEACON
] = { "beacon_report", BLOBMSG_TYPE_BOOL
},
569 [BSS_MGMT_EN_BSS_TRANSITION
] = { "bss_transition", BLOBMSG_TYPE_BOOL
},
574 hostapd_bss_mgmt_enable(struct ubus_context
*ctx
, struct ubus_object
*obj
,
575 struct ubus_request_data
*req
, const char *method
,
576 struct blob_attr
*msg
)
579 struct hostapd_data
*hapd
= get_hapd_from_object(obj
);
580 struct blob_attr
*tb
[__BSS_MGMT_EN_MAX
];
581 struct blob_attr
*cur
;
584 bool neigh
= false, beacon
= false;
586 blobmsg_parse(bss_mgmt_enable_policy
, __BSS_MGMT_EN_MAX
, tb
, blob_data(msg
), blob_len(msg
));
588 for (i
= 0; i
< ARRAY_SIZE(tb
); i
++) {
589 if (!tb
[i
] || !blobmsg_get_bool(tb
[i
]))
595 __hostapd_bss_mgmt_enable(hapd
, flags
);
600 hostapd_rrm_nr_enable(struct hostapd_data
*hapd
)
602 __hostapd_bss_mgmt_enable(hapd
, 1 << BSS_MGMT_EN_NEIGHBOR
);
606 hostapd_rrm_nr_get_own(struct ubus_context
*ctx
, struct ubus_object
*obj
,
607 struct ubus_request_data
*req
, const char *method
,
608 struct blob_attr
*msg
)
610 struct hostapd_data
*hapd
= get_hapd_from_object(obj
);
611 struct hostapd_neighbor_entry
*nr
;
614 hostapd_rrm_nr_enable(hapd
);
616 nr
= hostapd_neighbor_get(hapd
, hapd
->own_addr
, NULL
);
618 return UBUS_STATUS_NOT_FOUND
;
620 blob_buf_init(&b
, 0);
622 c
= blobmsg_open_array(&b
, "value");
623 hostapd_rrm_print_nr(nr
);
624 blobmsg_close_array(&b
, c
);
626 ubus_send_reply(ctx
, req
, b
.head
);
632 hostapd_rrm_nr_list(struct ubus_context
*ctx
, struct ubus_object
*obj
,
633 struct ubus_request_data
*req
, const char *method
,
634 struct blob_attr
*msg
)
636 struct hostapd_data
*hapd
= get_hapd_from_object(obj
);
637 struct hostapd_neighbor_entry
*nr
;
640 hostapd_rrm_nr_enable(hapd
);
641 blob_buf_init(&b
, 0);
643 c
= blobmsg_open_array(&b
, "list");
644 dl_list_for_each(nr
, &hapd
->nr_db
, struct hostapd_neighbor_entry
, list
) {
647 if (!memcmp(nr
->bssid
, hapd
->own_addr
, ETH_ALEN
))
650 cur
= blobmsg_open_array(&b
, NULL
);
651 hostapd_rrm_print_nr(nr
);
652 blobmsg_close_array(&b
, cur
);
654 blobmsg_close_array(&b
, c
);
656 ubus_send_reply(ctx
, req
, b
.head
);
666 static const struct blobmsg_policy nr_set_policy
[__NR_SET_LIST_MAX
] = {
667 [NR_SET_LIST
] = { "list", BLOBMSG_TYPE_ARRAY
},
672 hostapd_rrm_nr_clear(struct hostapd_data
*hapd
)
674 struct hostapd_neighbor_entry
*nr
;
677 dl_list_for_each(nr
, &hapd
->nr_db
, struct hostapd_neighbor_entry
, list
) {
678 if (!memcmp(nr
->bssid
, hapd
->own_addr
, ETH_ALEN
))
681 hostapd_neighbor_remove(hapd
, nr
->bssid
, &nr
->ssid
);
687 hostapd_rrm_nr_set(struct ubus_context
*ctx
, struct ubus_object
*obj
,
688 struct ubus_request_data
*req
, const char *method
,
689 struct blob_attr
*msg
)
691 static const struct blobmsg_policy nr_e_policy
[] = {
692 { .type
= BLOBMSG_TYPE_STRING
},
693 { .type
= BLOBMSG_TYPE_STRING
},
694 { .type
= BLOBMSG_TYPE_STRING
},
696 struct hostapd_data
*hapd
= get_hapd_from_object(obj
);
697 struct blob_attr
*tb_l
[__NR_SET_LIST_MAX
];
698 struct blob_attr
*tb
[ARRAY_SIZE(nr_e_policy
)];
699 struct blob_attr
*cur
;
703 hostapd_rrm_nr_enable(hapd
);
705 blobmsg_parse(nr_set_policy
, __NR_SET_LIST_MAX
, tb_l
, blob_data(msg
), blob_len(msg
));
706 if (!tb_l
[NR_SET_LIST
])
707 return UBUS_STATUS_INVALID_ARGUMENT
;
709 hostapd_rrm_nr_clear(hapd
);
710 blobmsg_for_each_attr(cur
, tb_l
[NR_SET_LIST
], rem
) {
711 struct wpa_ssid_value ssid
;
716 blobmsg_parse_array(nr_e_policy
, ARRAY_SIZE(nr_e_policy
), tb
, blobmsg_data(cur
), blobmsg_data_len(cur
));
717 if (!tb
[0] || !tb
[1] || !tb
[2])
720 s
= blobmsg_get_string(tb
[0]);
721 if (hwaddr_aton(s
, bssid
))
724 s
= blobmsg_get_string(tb
[1]);
725 ssid
.ssid_len
= strlen(s
);
726 if (ssid
.ssid_len
> sizeof(ssid
.ssid
))
729 memcpy(&ssid
, s
, ssid
.ssid_len
);
730 data
= wpabuf_parse_bin(blobmsg_get_string(tb
[2]));
734 hostapd_neighbor_set(hapd
, bssid
, &ssid
, data
, NULL
, NULL
, 0);
739 ret
= UBUS_STATUS_INVALID_ARGUMENT
;
756 static const struct blobmsg_policy beacon_req_policy
[__BEACON_REQ_MAX
] = {
757 [BEACON_REQ_ADDR
] = { "addr", BLOBMSG_TYPE_STRING
},
758 [BEACON_REQ_OP_CLASS
] { "op_class", BLOBMSG_TYPE_INT32
},
759 [BEACON_REQ_CHANNEL
] { "channel", BLOBMSG_TYPE_INT32
},
760 [BEACON_REQ_DURATION
] { "duration", BLOBMSG_TYPE_INT32
},
761 [BEACON_REQ_MODE
] { "mode", BLOBMSG_TYPE_INT32
},
762 [BEACON_REQ_BSSID
] { "bssid", BLOBMSG_TYPE_STRING
},
763 [BEACON_REQ_SSID
] { "ssid", BLOBMSG_TYPE_STRING
},
767 hostapd_rrm_beacon_req(struct ubus_context
*ctx
, struct ubus_object
*obj
,
768 struct ubus_request_data
*ureq
, const char *method
,
769 struct blob_attr
*msg
)
771 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
772 struct blob_attr
*tb
[__BEACON_REQ_MAX
];
773 struct blob_attr
*cur
;
775 u8 bssid
[ETH_ALEN
] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
780 blobmsg_parse(beacon_req_policy
, __BEACON_REQ_MAX
, tb
, blob_data(msg
), blob_len(msg
));
782 if (!tb
[BEACON_REQ_ADDR
] || !tb
[BEACON_REQ_MODE
] || !tb
[BEACON_REQ_DURATION
] ||
783 !tb
[BEACON_REQ_OP_CLASS
] || !tb
[BEACON_REQ_CHANNEL
])
784 return UBUS_STATUS_INVALID_ARGUMENT
;
786 if (tb
[BEACON_REQ_SSID
])
787 buf_len
+= blobmsg_data_len(tb
[BEACON_REQ_SSID
]) + 2 - 1;
789 mode
= blobmsg_get_u32(tb
[BEACON_REQ_MODE
]);
790 if (hwaddr_aton(blobmsg_data(tb
[BEACON_REQ_ADDR
]), addr
))
791 return UBUS_STATUS_INVALID_ARGUMENT
;
793 if (tb
[BEACON_REQ_BSSID
] &&
794 hwaddr_aton(blobmsg_data(tb
[BEACON_REQ_BSSID
]), bssid
))
795 return UBUS_STATUS_INVALID_ARGUMENT
;
797 req
= wpabuf_alloc(buf_len
);
799 return UBUS_STATUS_UNKNOWN_ERROR
;
801 /* 1: regulatory class */
802 wpabuf_put_u8(req
, blobmsg_get_u32(tb
[BEACON_REQ_OP_CLASS
]));
804 /* 2: channel number */
805 wpabuf_put_u8(req
, blobmsg_get_u32(tb
[BEACON_REQ_CHANNEL
]));
807 /* 3-4: randomization interval */
808 wpabuf_put_le16(req
, 0);
811 wpabuf_put_le16(req
, blobmsg_get_u32(tb
[BEACON_REQ_DURATION
]));
814 wpabuf_put_u8(req
, blobmsg_get_u32(tb
[BEACON_REQ_MODE
]));
817 wpabuf_put_data(req
, bssid
, ETH_ALEN
);
819 if ((cur
= tb
[BEACON_REQ_SSID
]) != NULL
) {
820 wpabuf_put_u8(req
, WLAN_EID_SSID
);
821 wpabuf_put_u8(req
, blobmsg_data_len(cur
) - 1);
822 wpabuf_put_data(req
, blobmsg_data(cur
), blobmsg_data_len(cur
) - 1);
825 ret
= hostapd_send_beacon_req(hapd
, addr
, 0, req
);
836 WNM_DISASSOC_DURATION
,
837 WNM_DISASSOC_NEIGHBORS
,
841 static const struct blobmsg_policy wnm_disassoc_policy
[__WNM_DISASSOC_MAX
] = {
842 [WNM_DISASSOC_ADDR
] = { "addr", BLOBMSG_TYPE_STRING
},
843 [WNM_DISASSOC_DURATION
] { "duration", BLOBMSG_TYPE_INT32
},
844 [WNM_DISASSOC_NEIGHBORS
] { "neighbors", BLOBMSG_TYPE_ARRAY
},
848 hostapd_wnm_disassoc_imminent(struct ubus_context
*ctx
, struct ubus_object
*obj
,
849 struct ubus_request_data
*ureq
, const char *method
,
850 struct blob_attr
*msg
)
852 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
853 struct blob_attr
*tb
[__WNM_DISASSOC_MAX
];
854 struct blob_attr
*cur
;
855 struct sta_info
*sta
;
860 u8 req_mode
= WNM_BSS_TM_REQ_DISASSOC_IMMINENT
;
863 blobmsg_parse(wnm_disassoc_policy
, __WNM_DISASSOC_MAX
, tb
, blob_data(msg
), blob_len(msg
));
865 if (!tb
[WNM_DISASSOC_ADDR
])
866 return UBUS_STATUS_INVALID_ARGUMENT
;
868 if (hwaddr_aton(blobmsg_data(tb
[WNM_DISASSOC_ADDR
]), addr
))
869 return UBUS_STATUS_INVALID_ARGUMENT
;
871 if ((cur
= tb
[WNM_DISASSOC_DURATION
]) != NULL
)
872 duration
= blobmsg_get_u32(cur
);
874 sta
= ap_get_sta(hapd
, addr
);
876 return UBUS_STATUS_NOT_FOUND
;
878 if (tb
[WNM_DISASSOC_NEIGHBORS
]) {
881 if (blobmsg_check_array(tb
[WNM_DISASSOC_NEIGHBORS
],
882 BLOBMSG_TYPE_STRING
) < 0)
883 return UBUS_STATUS_INVALID_ARGUMENT
;
885 blobmsg_for_each_attr(cur
, tb
[WNM_DISASSOC_NEIGHBORS
], rem
) {
886 int len
= strlen(blobmsg_get_string(cur
));
889 return UBUS_STATUS_INVALID_ARGUMENT
;
891 nr_len
+= (len
/ 2) + 2;
895 nr
= os_zalloc(nr_len
);
897 return UBUS_STATUS_UNKNOWN_ERROR
;
901 blobmsg_for_each_attr(cur
, tb
[WNM_DISASSOC_NEIGHBORS
], rem
) {
902 int len
= strlen(blobmsg_get_string(cur
)) / 2;
904 *nr_cur
++ = WLAN_EID_NEIGHBOR_REPORT
;
905 *nr_cur
++ = (u8
) len
;
906 if (hexstr2bin(blobmsg_data(cur
), nr_cur
, len
)) {
908 return UBUS_STATUS_INVALID_ARGUMENT
;
916 req_mode
|= WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED
;
918 if (wnm_send_bss_tm_req(hapd
, sta
, req_mode
, duration
, 0, NULL
,
919 NULL
, nr
, nr_len
, NULL
, 0))
920 return UBUS_STATUS_UNKNOWN_ERROR
;
926 static const struct ubus_method bss_methods
[] = {
927 UBUS_METHOD_NOARG("get_clients", hostapd_bss_get_clients
),
928 UBUS_METHOD("del_client", hostapd_bss_del_client
, del_policy
),
929 UBUS_METHOD_NOARG("list_bans", hostapd_bss_list_bans
),
930 UBUS_METHOD_NOARG("wps_start", hostapd_bss_wps_start
),
931 UBUS_METHOD_NOARG("wps_cancel", hostapd_bss_wps_cancel
),
932 UBUS_METHOD_NOARG("update_beacon", hostapd_bss_update_beacon
),
933 UBUS_METHOD_NOARG("get_features", hostapd_bss_get_features
),
935 UBUS_METHOD("switch_chan", hostapd_switch_chan
, csa_policy
),
937 UBUS_METHOD("set_vendor_elements", hostapd_vendor_elements
, ve_policy
),
938 UBUS_METHOD("notify_response", hostapd_notify_response
, notify_policy
),
939 UBUS_METHOD("bss_mgmt_enable", hostapd_bss_mgmt_enable
, bss_mgmt_enable_policy
),
940 UBUS_METHOD_NOARG("rrm_nr_get_own", hostapd_rrm_nr_get_own
),
941 UBUS_METHOD_NOARG("rrm_nr_list", hostapd_rrm_nr_list
),
942 UBUS_METHOD("rrm_nr_set", hostapd_rrm_nr_set
, nr_set_policy
),
943 UBUS_METHOD("rrm_beacon_req", hostapd_rrm_beacon_req
, beacon_req_policy
),
945 UBUS_METHOD("wnm_disassoc_imminent", hostapd_wnm_disassoc_imminent
, wnm_disassoc_policy
),
949 static struct ubus_object_type bss_object_type
=
950 UBUS_OBJECT_TYPE("hostapd_bss", bss_methods
);
952 static int avl_compare_macaddr(const void *k1
, const void *k2
, void *ptr
)
954 return memcmp(k1
, k2
, ETH_ALEN
);
957 void hostapd_ubus_add_bss(struct hostapd_data
*hapd
)
959 struct ubus_object
*obj
= &hapd
->ubus
.obj
;
964 if (hapd
->conf
->mesh
& MESH_ENABLED
)
968 if (!hostapd_ubus_init())
971 if (asprintf(&name
, "hostapd.%s", hapd
->conf
->iface
) < 0)
974 avl_init(&hapd
->ubus
.banned
, avl_compare_macaddr
, false, NULL
);
976 obj
->type
= &bss_object_type
;
977 obj
->methods
= bss_object_type
.methods
;
978 obj
->n_methods
= bss_object_type
.n_methods
;
979 ret
= ubus_add_object(ctx
, obj
);
980 hostapd_ubus_ref_inc();
983 void hostapd_ubus_free_bss(struct hostapd_data
*hapd
)
985 struct ubus_object
*obj
= &hapd
->ubus
.obj
;
986 char *name
= (char *) obj
->name
;
992 ubus_remove_object(ctx
, obj
);
993 hostapd_ubus_ref_dec();
999 struct ubus_event_req
{
1000 struct ubus_notify_request nreq
;
1005 ubus_event_cb(struct ubus_notify_request
*req
, int idx
, int ret
)
1007 struct ubus_event_req
*ureq
= container_of(req
, struct ubus_event_req
, nreq
);
1012 int hostapd_ubus_handle_event(struct hostapd_data
*hapd
, struct hostapd_ubus_request
*req
)
1014 struct ubus_banned_client
*ban
;
1015 const char *types
[HOSTAPD_UBUS_TYPE_MAX
] = {
1016 [HOSTAPD_UBUS_PROBE_REQ
] = "probe",
1017 [HOSTAPD_UBUS_AUTH_REQ
] = "auth",
1018 [HOSTAPD_UBUS_ASSOC_REQ
] = "assoc",
1020 const char *type
= "mgmt";
1021 struct ubus_event_req ureq
= {};
1024 if (req
->mgmt_frame
)
1025 addr
= req
->mgmt_frame
->sa
;
1029 ban
= avl_find_element(&hapd
->ubus
.banned
, addr
, ban
, avl
);
1031 return WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA
;
1033 if (!hapd
->ubus
.obj
.has_subscribers
)
1034 return WLAN_STATUS_SUCCESS
;
1036 if (req
->type
< ARRAY_SIZE(types
))
1037 type
= types
[req
->type
];
1039 blob_buf_init(&b
, 0);
1040 blobmsg_add_macaddr(&b
, "address", addr
);
1041 if (req
->mgmt_frame
)
1042 blobmsg_add_macaddr(&b
, "target", req
->mgmt_frame
->da
);
1043 if (req
->frame_info
)
1044 blobmsg_add_u32(&b
, "signal", req
->frame_info
->ssi_signal
);
1045 blobmsg_add_u32(&b
, "freq", hapd
->iface
->freq
);
1047 if (!hapd
->ubus
.notify_response
) {
1048 ubus_notify(ctx
, &hapd
->ubus
.obj
, type
, b
.head
, -1);
1049 return WLAN_STATUS_SUCCESS
;
1052 if (ubus_notify_async(ctx
, &hapd
->ubus
.obj
, type
, b
.head
, &ureq
.nreq
))
1053 return WLAN_STATUS_SUCCESS
;
1055 ureq
.nreq
.status_cb
= ubus_event_cb
;
1056 ubus_complete_request(ctx
, &ureq
.nreq
.req
, 100);
1061 return WLAN_STATUS_SUCCESS
;
1064 void hostapd_ubus_notify(struct hostapd_data
*hapd
, const char *type
, const u8
*addr
)
1066 if (!hapd
->ubus
.obj
.has_subscribers
)
1072 blob_buf_init(&b
, 0);
1073 blobmsg_add_macaddr(&b
, "address", addr
);
1075 ubus_notify(ctx
, &hapd
->ubus
.obj
, type
, b
.head
, -1);