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"
24 static struct ubus_context
*ctx
;
25 static struct blob_buf b
;
28 static inline struct hostapd_data
*get_hapd_from_object(struct ubus_object
*obj
)
30 return container_of(obj
, struct hostapd_data
, ubus
.obj
);
34 struct ubus_banned_client
{
39 static void ubus_receive(int sock
, void *eloop_ctx
, void *sock_ctx
)
41 struct ubus_context
*ctx
= eloop_ctx
;
42 ubus_handle_event(ctx
);
45 static void ubus_reconnect_timeout(void *eloop_data
, void *user_ctx
)
47 if (ubus_reconnect(ctx
, NULL
)) {
48 eloop_register_timeout(1, 0, ubus_reconnect_timeout
, ctx
, NULL
);
52 eloop_register_read_sock(ctx
->sock
.fd
, ubus_receive
, ctx
, NULL
);
55 static void hostapd_ubus_connection_lost(struct ubus_context
*ctx
)
57 eloop_unregister_read_sock(ctx
->sock
.fd
);
58 eloop_register_timeout(1, 0, ubus_reconnect_timeout
, ctx
, NULL
);
61 static bool hostapd_ubus_init(void)
66 ctx
= ubus_connect(NULL
);
70 ctx
->connection_lost
= hostapd_ubus_connection_lost
;
71 eloop_register_read_sock(ctx
->sock
.fd
, ubus_receive
, ctx
, NULL
);
75 static void hostapd_ubus_ref_inc(void)
80 static void hostapd_ubus_ref_dec(void)
89 eloop_unregister_read_sock(ctx
->sock
.fd
);
94 void hostapd_ubus_add_iface(struct hostapd_iface
*iface
)
96 if (!hostapd_ubus_init())
100 void hostapd_ubus_free_iface(struct hostapd_iface
*iface
)
107 hostapd_bss_del_ban(void *eloop_data
, void *user_ctx
)
109 struct ubus_banned_client
*ban
= eloop_data
;
110 struct hostapd_data
*hapd
= user_ctx
;
112 avl_delete(&hapd
->ubus
.banned
, &ban
->avl
);
117 hostapd_bss_ban_client(struct hostapd_data
*hapd
, u8
*addr
, int time
)
119 struct ubus_banned_client
*ban
;
124 ban
= avl_find_element(&hapd
->ubus
.banned
, addr
, ban
, avl
);
129 ban
= os_zalloc(sizeof(*ban
));
130 memcpy(ban
->addr
, addr
, sizeof(ban
->addr
));
131 ban
->avl
.key
= ban
->addr
;
132 avl_insert(&hapd
->ubus
.banned
, &ban
->avl
);
134 eloop_cancel_timeout(hostapd_bss_del_ban
, ban
, hapd
);
136 hostapd_bss_del_ban(ban
, hapd
);
141 eloop_register_timeout(0, time
* 1000, hostapd_bss_del_ban
, ban
, hapd
);
145 hostapd_bss_get_clients(struct ubus_context
*ctx
, struct ubus_object
*obj
,
146 struct ubus_request_data
*req
, const char *method
,
147 struct blob_attr
*msg
)
149 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
150 struct sta_info
*sta
;
153 static const struct {
157 { "auth", WLAN_STA_AUTH
},
158 { "assoc", WLAN_STA_ASSOC
},
159 { "authorized", WLAN_STA_AUTHORIZED
},
160 { "preauth", WLAN_STA_PREAUTH
},
161 { "wds", WLAN_STA_WDS
},
162 { "wmm", WLAN_STA_WMM
},
163 { "ht", WLAN_STA_HT
},
164 { "vht", WLAN_STA_VHT
},
165 { "wps", WLAN_STA_WPS
},
166 { "mfp", WLAN_STA_MFP
},
169 blob_buf_init(&b
, 0);
170 blobmsg_add_u32(&b
, "freq", hapd
->iface
->freq
);
171 list
= blobmsg_open_table(&b
, "clients");
172 for (sta
= hapd
->sta_list
; sta
; sta
= sta
->next
) {
176 sprintf(mac_buf
, MACSTR
, MAC2STR(sta
->addr
));
177 c
= blobmsg_open_table(&b
, mac_buf
);
178 for (i
= 0; i
< ARRAY_SIZE(sta_flags
); i
++)
179 blobmsg_add_u8(&b
, sta_flags
[i
].name
,
180 !!(sta
->flags
& sta_flags
[i
].flag
));
182 r
= blobmsg_open_array(&b
, "rrm");
183 for (i
= 0; i
< ARRAY_SIZE(sta
->rrm_enabled_capa
); i
++)
184 blobmsg_add_u32(&b
, "", sta
->rrm_enabled_capa
[i
]);
185 blobmsg_close_array(&b
, r
);
186 blobmsg_add_u32(&b
, "aid", sta
->aid
);
187 blobmsg_close_table(&b
, c
);
189 blobmsg_close_array(&b
, list
);
190 ubus_send_reply(ctx
, req
, b
.head
);
200 static const struct blobmsg_policy notify_policy
[__NOTIFY_MAX
] = {
201 [NOTIFY_RESPONSE
] = { "notify_response", BLOBMSG_TYPE_INT32
},
205 hostapd_notify_response(struct ubus_context
*ctx
, struct ubus_object
*obj
,
206 struct ubus_request_data
*req
, const char *method
,
207 struct blob_attr
*msg
)
209 struct blob_attr
*tb
[__NOTIFY_MAX
];
210 struct hostapd_data
*hapd
= get_hapd_from_object(obj
);
211 struct wpabuf
*elems
;
215 blobmsg_parse(notify_policy
, __NOTIFY_MAX
, tb
,
216 blob_data(msg
), blob_len(msg
));
218 if (!tb
[NOTIFY_RESPONSE
])
219 return UBUS_STATUS_INVALID_ARGUMENT
;
221 hapd
->ubus
.notify_response
= blobmsg_get_u32(tb
[NOTIFY_RESPONSE
]);
223 return UBUS_STATUS_OK
;
234 static const struct blobmsg_policy del_policy
[__DEL_CLIENT_MAX
] = {
235 [DEL_CLIENT_ADDR
] = { "addr", BLOBMSG_TYPE_STRING
},
236 [DEL_CLIENT_REASON
] = { "reason", BLOBMSG_TYPE_INT32
},
237 [DEL_CLIENT_DEAUTH
] = { "deauth", BLOBMSG_TYPE_INT8
},
238 [DEL_CLIENT_BAN_TIME
] = { "ban_time", BLOBMSG_TYPE_INT32
},
242 hostapd_bss_del_client(struct ubus_context
*ctx
, struct ubus_object
*obj
,
243 struct ubus_request_data
*req
, const char *method
,
244 struct blob_attr
*msg
)
246 struct blob_attr
*tb
[__DEL_CLIENT_MAX
];
247 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
248 struct sta_info
*sta
;
253 blobmsg_parse(del_policy
, __DEL_CLIENT_MAX
, tb
, blob_data(msg
), blob_len(msg
));
255 if (!tb
[DEL_CLIENT_ADDR
])
256 return UBUS_STATUS_INVALID_ARGUMENT
;
258 if (hwaddr_aton(blobmsg_data(tb
[DEL_CLIENT_ADDR
]), addr
))
259 return UBUS_STATUS_INVALID_ARGUMENT
;
261 if (tb
[DEL_CLIENT_REASON
])
262 reason
= blobmsg_get_u32(tb
[DEL_CLIENT_REASON
]);
264 if (tb
[DEL_CLIENT_DEAUTH
])
265 deauth
= blobmsg_get_bool(tb
[DEL_CLIENT_DEAUTH
]);
267 sta
= ap_get_sta(hapd
, addr
);
270 hostapd_drv_sta_deauth(hapd
, addr
, reason
);
271 ap_sta_deauthenticate(hapd
, sta
, reason
);
273 hostapd_drv_sta_disassoc(hapd
, addr
, reason
);
274 ap_sta_disassociate(hapd
, sta
, reason
);
278 if (tb
[DEL_CLIENT_BAN_TIME
])
279 hostapd_bss_ban_client(hapd
, addr
, blobmsg_get_u32(tb
[DEL_CLIENT_BAN_TIME
]));
285 blobmsg_add_macaddr(struct blob_buf
*buf
, const char *name
, const u8
*addr
)
289 s
= blobmsg_alloc_string_buffer(buf
, name
, 20);
290 sprintf(s
, MACSTR
, MAC2STR(addr
));
291 blobmsg_add_string_buffer(buf
);
295 hostapd_bss_list_bans(struct ubus_context
*ctx
, struct ubus_object
*obj
,
296 struct ubus_request_data
*req
, const char *method
,
297 struct blob_attr
*msg
)
299 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
300 struct ubus_banned_client
*ban
;
303 blob_buf_init(&b
, 0);
304 c
= blobmsg_open_array(&b
, "clients");
305 avl_for_each_element(&hapd
->ubus
.banned
, ban
, avl
)
306 blobmsg_add_macaddr(&b
, NULL
, ban
->addr
);
307 blobmsg_close_array(&b
, c
);
308 ubus_send_reply(ctx
, req
, b
.head
);
314 hostapd_bss_wps_start(struct ubus_context
*ctx
, struct ubus_object
*obj
,
315 struct ubus_request_data
*req
, const char *method
,
316 struct blob_attr
*msg
)
319 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
321 rc
= hostapd_wps_button_pushed(hapd
, NULL
);
324 return UBUS_STATUS_NOT_SUPPORTED
;
330 hostapd_bss_wps_cancel(struct ubus_context
*ctx
, struct ubus_object
*obj
,
331 struct ubus_request_data
*req
, const char *method
,
332 struct blob_attr
*msg
)
335 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
337 rc
= hostapd_wps_cancel(hapd
);
340 return UBUS_STATUS_NOT_SUPPORTED
;
346 hostapd_bss_update_beacon(struct ubus_context
*ctx
, struct ubus_object
*obj
,
347 struct ubus_request_data
*req
, const char *method
,
348 struct blob_attr
*msg
)
351 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
353 rc
= ieee802_11_set_beacon(hapd
);
356 return UBUS_STATUS_NOT_SUPPORTED
;
367 static const struct blobmsg_policy csa_policy
[__CSA_MAX
] = {
369 * for now, frequency and beacon count are enough, add more
370 * parameters on demand
372 [CSA_FREQ
] = { "freq", BLOBMSG_TYPE_INT32
},
373 [CSA_BCN_COUNT
] = { "bcn_count", BLOBMSG_TYPE_INT32
},
378 hostapd_switch_chan(struct ubus_context
*ctx
, struct ubus_object
*obj
,
379 struct ubus_request_data
*req
, const char *method
,
380 struct blob_attr
*msg
)
382 struct blob_attr
*tb
[__CSA_MAX
];
383 struct hostapd_data
*hapd
= get_hapd_from_object(obj
);
384 struct csa_settings css
;
386 blobmsg_parse(csa_policy
, __CSA_MAX
, tb
, blob_data(msg
), blob_len(msg
));
389 return UBUS_STATUS_INVALID_ARGUMENT
;
391 memset(&css
, 0, sizeof(css
));
392 css
.freq_params
.freq
= blobmsg_get_u32(tb
[CSA_FREQ
]);
393 if (tb
[CSA_BCN_COUNT
])
394 css
.cs_count
= blobmsg_get_u32(tb
[CSA_BCN_COUNT
]);
396 if (hostapd_switch_channel(hapd
, &css
) != 0)
397 return UBUS_STATUS_NOT_SUPPORTED
;
398 return UBUS_STATUS_OK
;
404 __VENDOR_ELEMENTS_MAX
407 static const struct blobmsg_policy ve_policy
[__VENDOR_ELEMENTS_MAX
] = {
408 /* vendor elements are provided as hex-string */
409 [VENDOR_ELEMENTS
] = { "vendor_elements", BLOBMSG_TYPE_STRING
},
413 hostapd_vendor_elements(struct ubus_context
*ctx
, struct ubus_object
*obj
,
414 struct ubus_request_data
*req
, const char *method
,
415 struct blob_attr
*msg
)
417 struct blob_attr
*tb
[__VENDOR_ELEMENTS_MAX
];
418 struct hostapd_data
*hapd
= get_hapd_from_object(obj
);
419 struct hostapd_bss_config
*bss
= hapd
->conf
;
420 struct wpabuf
*elems
;
424 blobmsg_parse(ve_policy
, __VENDOR_ELEMENTS_MAX
, tb
,
425 blob_data(msg
), blob_len(msg
));
427 if (!tb
[VENDOR_ELEMENTS
])
428 return UBUS_STATUS_INVALID_ARGUMENT
;
430 pos
= blobmsg_data(tb
[VENDOR_ELEMENTS
]);
431 len
= os_strlen(pos
);
433 return UBUS_STATUS_INVALID_ARGUMENT
;
437 wpabuf_free(bss
->vendor_elements
);
438 bss
->vendor_elements
= NULL
;
442 elems
= wpabuf_alloc(len
);
446 if (hexstr2bin(pos
, wpabuf_put(elems
, len
), len
)) {
448 return UBUS_STATUS_INVALID_ARGUMENT
;
451 wpabuf_free(bss
->vendor_elements
);
452 bss
->vendor_elements
= elems
;
454 /* update beacons if vendor elements were set successfully */
455 if (ieee802_11_update_beacons(hapd
->iface
) != 0)
456 return UBUS_STATUS_NOT_SUPPORTED
;
457 return UBUS_STATUS_OK
;
461 hostapd_rrm_print_nr(struct hostapd_neighbor_entry
*nr
)
467 blobmsg_printf(&b
, "", MACSTR
, MAC2STR(nr
->bssid
));
469 str
= blobmsg_alloc_string_buffer(&b
, "", nr
->ssid
.ssid_len
+ 1);
470 memcpy(str
, nr
->ssid
.ssid
, nr
->ssid
.ssid_len
);
471 str
[nr
->ssid
.ssid_len
] = 0;
472 blobmsg_add_string_buffer(&b
);
474 len
= wpabuf_len(nr
->nr
);
475 str
= blobmsg_alloc_string_buffer(&b
, "", 2 * len
+ 1);
476 wpa_snprintf_hex(str
, 2 * len
+ 1, wpabuf_head_u8(nr
->nr
), len
);
477 blobmsg_add_string_buffer(&b
);
481 BSS_MGMT_EN_NEIGHBOR
,
484 BSS_MGMT_EN_BSS_TRANSITION
,
490 __hostapd_bss_mgmt_enable_f(struct hostapd_data
*hapd
, int flag
)
492 struct hostapd_bss_config
*bss
= hapd
->conf
;
496 case BSS_MGMT_EN_NEIGHBOR
:
497 if (bss
->radio_measurements
[0] &
498 WLAN_RRM_CAPS_NEIGHBOR_REPORT
)
501 bss
->radio_measurements
[0] |=
502 WLAN_RRM_CAPS_NEIGHBOR_REPORT
;
503 hostapd_set_own_neighbor_report(hapd
);
505 case BSS_MGMT_EN_BEACON
:
506 flags
= WLAN_RRM_CAPS_BEACON_REPORT_PASSIVE
|
507 WLAN_RRM_CAPS_BEACON_REPORT_ACTIVE
|
508 WLAN_RRM_CAPS_BEACON_REPORT_TABLE
;
510 if (bss
->radio_measurements
[0] & flags
== flags
)
513 bss
->radio_measurements
[0] |= (u8
) flags
;
516 case BSS_MGMT_EN_BSS_TRANSITION
:
517 if (bss
->bss_transition
)
520 bss
->bss_transition
= 1;
527 __hostapd_bss_mgmt_enable(struct hostapd_data
*hapd
, uint32_t flags
)
532 for (i
= 0; i
< __BSS_MGMT_EN_MAX
; i
++) {
533 if (!(flags
& (1 << i
)))
536 update
|= __hostapd_bss_mgmt_enable_f(hapd
, i
);
540 ieee802_11_update_beacons(hapd
->iface
);
544 static const struct blobmsg_policy bss_mgmt_enable_policy
[__BSS_MGMT_EN_MAX
] = {
545 [BSS_MGMT_EN_NEIGHBOR
] = { "neighbor_report", BLOBMSG_TYPE_BOOL
},
546 [BSS_MGMT_EN_BEACON
] = { "beacon_report", BLOBMSG_TYPE_BOOL
},
548 [BSS_MGMT_EN_BSS_TRANSITION
] = { "bss_transition", BLOBMSG_TYPE_BOOL
},
553 hostapd_bss_mgmt_enable(struct ubus_context
*ctx
, struct ubus_object
*obj
,
554 struct ubus_request_data
*req
, const char *method
,
555 struct blob_attr
*msg
)
558 struct hostapd_data
*hapd
= get_hapd_from_object(obj
);
559 struct blob_attr
*tb
[__BSS_MGMT_EN_MAX
];
560 struct blob_attr
*cur
;
563 bool neigh
= false, beacon
= false;
565 blobmsg_parse(bss_mgmt_enable_policy
, __BSS_MGMT_EN_MAX
, tb
, blob_data(msg
), blob_len(msg
));
567 for (i
= 0; i
< ARRAY_SIZE(tb
); i
++) {
568 if (!tb
[i
] || !blobmsg_get_bool(tb
[i
]))
574 __hostapd_bss_mgmt_enable(hapd
, flags
);
579 hostapd_rrm_nr_enable(struct hostapd_data
*hapd
)
581 __hostapd_bss_mgmt_enable(hapd
, 1 << BSS_MGMT_EN_NEIGHBOR
);
585 hostapd_rrm_nr_get_own(struct ubus_context
*ctx
, struct ubus_object
*obj
,
586 struct ubus_request_data
*req
, const char *method
,
587 struct blob_attr
*msg
)
589 struct hostapd_data
*hapd
= get_hapd_from_object(obj
);
590 struct hostapd_neighbor_entry
*nr
;
593 hostapd_rrm_nr_enable(hapd
);
595 nr
= hostapd_neighbor_get(hapd
, hapd
->own_addr
, NULL
);
597 return UBUS_STATUS_NOT_FOUND
;
599 blob_buf_init(&b
, 0);
601 c
= blobmsg_open_array(&b
, "value");
602 hostapd_rrm_print_nr(nr
);
603 blobmsg_close_array(&b
, c
);
605 ubus_send_reply(ctx
, req
, b
.head
);
611 hostapd_rrm_nr_list(struct ubus_context
*ctx
, struct ubus_object
*obj
,
612 struct ubus_request_data
*req
, const char *method
,
613 struct blob_attr
*msg
)
615 struct hostapd_data
*hapd
= get_hapd_from_object(obj
);
616 struct hostapd_neighbor_entry
*nr
;
619 hostapd_rrm_nr_enable(hapd
);
620 blob_buf_init(&b
, 0);
622 c
= blobmsg_open_array(&b
, "list");
623 dl_list_for_each(nr
, &hapd
->nr_db
, struct hostapd_neighbor_entry
, list
) {
626 if (!memcmp(nr
->bssid
, hapd
->own_addr
, ETH_ALEN
))
629 cur
= blobmsg_open_array(&b
, NULL
);
630 hostapd_rrm_print_nr(nr
);
631 blobmsg_close_array(&b
, cur
);
633 blobmsg_close_array(&b
, c
);
635 ubus_send_reply(ctx
, req
, b
.head
);
645 static const struct blobmsg_policy nr_set_policy
[__NR_SET_LIST_MAX
] = {
646 [NR_SET_LIST
] = { "list", BLOBMSG_TYPE_ARRAY
},
651 hostapd_rrm_nr_clear(struct hostapd_data
*hapd
)
653 struct hostapd_neighbor_entry
*nr
;
656 dl_list_for_each(nr
, &hapd
->nr_db
, struct hostapd_neighbor_entry
, list
) {
657 if (!memcmp(nr
->bssid
, hapd
->own_addr
, ETH_ALEN
))
660 hostapd_neighbor_remove(hapd
, nr
->bssid
, &nr
->ssid
);
666 hostapd_rrm_nr_set(struct ubus_context
*ctx
, struct ubus_object
*obj
,
667 struct ubus_request_data
*req
, const char *method
,
668 struct blob_attr
*msg
)
670 static const struct blobmsg_policy nr_e_policy
[] = {
671 { .type
= BLOBMSG_TYPE_STRING
},
672 { .type
= BLOBMSG_TYPE_STRING
},
673 { .type
= BLOBMSG_TYPE_STRING
},
675 struct hostapd_data
*hapd
= get_hapd_from_object(obj
);
676 struct blob_attr
*tb_l
[__NR_SET_LIST_MAX
];
677 struct blob_attr
*tb
[ARRAY_SIZE(nr_e_policy
)];
678 struct blob_attr
*cur
;
682 hostapd_rrm_nr_enable(hapd
);
684 blobmsg_parse(nr_set_policy
, __NR_SET_LIST_MAX
, tb_l
, blob_data(msg
), blob_len(msg
));
685 if (!tb_l
[NR_SET_LIST
])
686 return UBUS_STATUS_INVALID_ARGUMENT
;
688 hostapd_rrm_nr_clear(hapd
);
689 blobmsg_for_each_attr(cur
, tb_l
[NR_SET_LIST
], rem
) {
690 struct wpa_ssid_value ssid
;
695 blobmsg_parse_array(nr_e_policy
, ARRAY_SIZE(nr_e_policy
), tb
, blobmsg_data(cur
), blobmsg_data_len(cur
));
696 if (!tb
[0] || !tb
[1] || !tb
[2])
699 s
= blobmsg_get_string(tb
[0]);
700 if (hwaddr_aton(s
, bssid
))
703 s
= blobmsg_get_string(tb
[1]);
704 ssid
.ssid_len
= strlen(s
);
705 if (ssid
.ssid_len
> sizeof(ssid
.ssid
))
708 memcpy(&ssid
, s
, ssid
.ssid_len
);
709 data
= wpabuf_parse_bin(blobmsg_get_string(tb
[2]));
713 hostapd_neighbor_set(hapd
, bssid
, &ssid
, data
, NULL
, NULL
, 0);
718 ret
= UBUS_STATUS_INVALID_ARGUMENT
;
735 static const struct blobmsg_policy beacon_req_policy
[__BEACON_REQ_MAX
] = {
736 [BEACON_REQ_ADDR
] = { "addr", BLOBMSG_TYPE_STRING
},
737 [BEACON_REQ_OP_CLASS
] { "op_class", BLOBMSG_TYPE_INT32
},
738 [BEACON_REQ_CHANNEL
] { "channel", BLOBMSG_TYPE_INT32
},
739 [BEACON_REQ_DURATION
] { "duration", BLOBMSG_TYPE_INT32
},
740 [BEACON_REQ_MODE
] { "mode", BLOBMSG_TYPE_INT32
},
741 [BEACON_REQ_BSSID
] { "bssid", BLOBMSG_TYPE_STRING
},
742 [BEACON_REQ_SSID
] { "ssid", BLOBMSG_TYPE_STRING
},
746 hostapd_rrm_beacon_req(struct ubus_context
*ctx
, struct ubus_object
*obj
,
747 struct ubus_request_data
*ureq
, const char *method
,
748 struct blob_attr
*msg
)
750 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
751 struct blob_attr
*tb
[__BEACON_REQ_MAX
];
752 struct blob_attr
*cur
;
754 u8 bssid
[ETH_ALEN
] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
759 blobmsg_parse(beacon_req_policy
, __BEACON_REQ_MAX
, tb
, blob_data(msg
), blob_len(msg
));
761 if (!tb
[BEACON_REQ_ADDR
] || !tb
[BEACON_REQ_MODE
] || !tb
[BEACON_REQ_DURATION
] ||
762 !tb
[BEACON_REQ_OP_CLASS
] || !tb
[BEACON_REQ_CHANNEL
])
763 return UBUS_STATUS_INVALID_ARGUMENT
;
765 if (tb
[BEACON_REQ_SSID
])
766 buf_len
+= blobmsg_data_len(tb
[BEACON_REQ_SSID
]) + 2 - 1;
768 mode
= blobmsg_get_u32(tb
[BEACON_REQ_MODE
]);
769 if (hwaddr_aton(blobmsg_data(tb
[BEACON_REQ_ADDR
]), addr
))
770 return UBUS_STATUS_INVALID_ARGUMENT
;
772 if (tb
[BEACON_REQ_BSSID
] &&
773 hwaddr_aton(blobmsg_data(tb
[BEACON_REQ_BSSID
]), bssid
))
774 return UBUS_STATUS_INVALID_ARGUMENT
;
776 req
= wpabuf_alloc(buf_len
);
778 return UBUS_STATUS_UNKNOWN_ERROR
;
780 /* 1: regulatory class */
781 wpabuf_put_u8(req
, blobmsg_get_u32(tb
[BEACON_REQ_OP_CLASS
]));
783 /* 2: channel number */
784 wpabuf_put_u8(req
, blobmsg_get_u32(tb
[BEACON_REQ_CHANNEL
]));
786 /* 3-4: randomization interval */
787 wpabuf_put_le16(req
, 0);
790 wpabuf_put_le16(req
, blobmsg_get_u32(tb
[BEACON_REQ_DURATION
]));
793 wpabuf_put_u8(req
, blobmsg_get_u32(tb
[BEACON_REQ_MODE
]));
796 wpabuf_put_data(req
, bssid
, ETH_ALEN
);
798 if ((cur
= tb
[BEACON_REQ_SSID
]) != NULL
) {
799 wpabuf_put_u8(req
, WLAN_EID_SSID
);
800 wpabuf_put_u8(req
, blobmsg_data_len(cur
) - 1);
801 wpabuf_put_data(req
, blobmsg_data(cur
), blobmsg_data_len(cur
) - 1);
804 ret
= hostapd_send_beacon_req(hapd
, addr
, 0, req
);
815 WNM_DISASSOC_DURATION
,
816 WNM_DISASSOC_NEIGHBORS
,
820 static const struct blobmsg_policy wnm_disassoc_policy
[__WNM_DISASSOC_MAX
] = {
821 [WNM_DISASSOC_ADDR
] = { "addr", BLOBMSG_TYPE_STRING
},
822 [WNM_DISASSOC_DURATION
] { "duration", BLOBMSG_TYPE_INT32
},
823 [WNM_DISASSOC_NEIGHBORS
] { "neighbors", BLOBMSG_TYPE_ARRAY
},
827 hostapd_wnm_disassoc_imminent(struct ubus_context
*ctx
, struct ubus_object
*obj
,
828 struct ubus_request_data
*ureq
, const char *method
,
829 struct blob_attr
*msg
)
831 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
832 struct blob_attr
*tb
[__WNM_DISASSOC_MAX
];
833 struct blob_attr
*cur
;
834 struct sta_info
*sta
;
839 u8 req_mode
= WNM_BSS_TM_REQ_DISASSOC_IMMINENT
;
842 blobmsg_parse(wnm_disassoc_policy
, __WNM_DISASSOC_MAX
, tb
, blob_data(msg
), blob_len(msg
));
844 if (!tb
[WNM_DISASSOC_ADDR
])
845 return UBUS_STATUS_INVALID_ARGUMENT
;
847 if (hwaddr_aton(blobmsg_data(tb
[WNM_DISASSOC_ADDR
]), addr
))
848 return UBUS_STATUS_INVALID_ARGUMENT
;
850 if ((cur
= tb
[WNM_DISASSOC_DURATION
]) != NULL
)
851 duration
= blobmsg_get_u32(cur
);
853 sta
= ap_get_sta(hapd
, addr
);
855 return UBUS_STATUS_NOT_FOUND
;
857 if (tb
[WNM_DISASSOC_NEIGHBORS
]) {
860 if (blobmsg_check_array(tb
[WNM_DISASSOC_NEIGHBORS
],
861 BLOBMSG_TYPE_STRING
) < 0)
862 return UBUS_STATUS_INVALID_ARGUMENT
;
864 blobmsg_for_each_attr(cur
, tb
[WNM_DISASSOC_NEIGHBORS
], rem
) {
865 int len
= strlen(blobmsg_get_string(cur
));
868 return UBUS_STATUS_INVALID_ARGUMENT
;
870 nr_len
+= (len
/ 2) + 2;
874 nr
= os_zalloc(nr_len
);
876 return UBUS_STATUS_UNKNOWN_ERROR
;
880 blobmsg_for_each_attr(cur
, tb
[WNM_DISASSOC_NEIGHBORS
], rem
) {
881 int len
= strlen(blobmsg_get_string(cur
)) / 2;
883 *nr_cur
++ = WLAN_EID_NEIGHBOR_REPORT
;
884 *nr_cur
++ = (u8
) len
;
885 if (hexstr2bin(blobmsg_data(cur
), nr_cur
, len
)) {
887 return UBUS_STATUS_INVALID_ARGUMENT
;
895 req_mode
|= WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED
;
897 if (wnm_send_bss_tm_req(hapd
, sta
, req_mode
, duration
, 0, NULL
,
898 NULL
, nr
, nr_len
, NULL
, 0))
899 return UBUS_STATUS_UNKNOWN_ERROR
;
905 static const struct ubus_method bss_methods
[] = {
906 UBUS_METHOD_NOARG("get_clients", hostapd_bss_get_clients
),
907 UBUS_METHOD("del_client", hostapd_bss_del_client
, del_policy
),
908 UBUS_METHOD_NOARG("list_bans", hostapd_bss_list_bans
),
909 UBUS_METHOD_NOARG("wps_start", hostapd_bss_wps_start
),
910 UBUS_METHOD_NOARG("wps_cancel", hostapd_bss_wps_cancel
),
911 UBUS_METHOD_NOARG("update_beacon", hostapd_bss_update_beacon
),
913 UBUS_METHOD("switch_chan", hostapd_switch_chan
, csa_policy
),
915 UBUS_METHOD("set_vendor_elements", hostapd_vendor_elements
, ve_policy
),
916 UBUS_METHOD("notify_response", hostapd_notify_response
, notify_policy
),
917 UBUS_METHOD("bss_mgmt_enable", hostapd_bss_mgmt_enable
, bss_mgmt_enable_policy
),
918 UBUS_METHOD_NOARG("rrm_nr_get_own", hostapd_rrm_nr_get_own
),
919 UBUS_METHOD_NOARG("rrm_nr_list", hostapd_rrm_nr_list
),
920 UBUS_METHOD("rrm_nr_set", hostapd_rrm_nr_set
, nr_set_policy
),
921 UBUS_METHOD("rrm_beacon_req", hostapd_rrm_beacon_req
, beacon_req_policy
),
923 UBUS_METHOD("wnm_disassoc_imminent", hostapd_wnm_disassoc_imminent
, wnm_disassoc_policy
),
927 static struct ubus_object_type bss_object_type
=
928 UBUS_OBJECT_TYPE("hostapd_bss", bss_methods
);
930 static int avl_compare_macaddr(const void *k1
, const void *k2
, void *ptr
)
932 return memcmp(k1
, k2
, ETH_ALEN
);
935 void hostapd_ubus_add_bss(struct hostapd_data
*hapd
)
937 struct ubus_object
*obj
= &hapd
->ubus
.obj
;
941 if (!hostapd_ubus_init())
944 if (asprintf(&name
, "hostapd.%s", hapd
->conf
->iface
) < 0)
947 avl_init(&hapd
->ubus
.banned
, avl_compare_macaddr
, false, NULL
);
949 obj
->type
= &bss_object_type
;
950 obj
->methods
= bss_object_type
.methods
;
951 obj
->n_methods
= bss_object_type
.n_methods
;
952 ret
= ubus_add_object(ctx
, obj
);
953 hostapd_ubus_ref_inc();
956 void hostapd_ubus_free_bss(struct hostapd_data
*hapd
)
958 struct ubus_object
*obj
= &hapd
->ubus
.obj
;
959 char *name
= (char *) obj
->name
;
965 ubus_remove_object(ctx
, obj
);
966 hostapd_ubus_ref_dec();
972 struct ubus_event_req
{
973 struct ubus_notify_request nreq
;
978 ubus_event_cb(struct ubus_notify_request
*req
, int idx
, int ret
)
980 struct ubus_event_req
*ureq
= container_of(req
, struct ubus_event_req
, nreq
);
985 int hostapd_ubus_handle_event(struct hostapd_data
*hapd
, struct hostapd_ubus_request
*req
)
987 struct ubus_banned_client
*ban
;
988 const char *types
[HOSTAPD_UBUS_TYPE_MAX
] = {
989 [HOSTAPD_UBUS_PROBE_REQ
] = "probe",
990 [HOSTAPD_UBUS_AUTH_REQ
] = "auth",
991 [HOSTAPD_UBUS_ASSOC_REQ
] = "assoc",
993 const char *type
= "mgmt";
994 struct ubus_event_req ureq
= {};
998 addr
= req
->mgmt_frame
->sa
;
1002 ban
= avl_find_element(&hapd
->ubus
.banned
, addr
, ban
, avl
);
1004 return WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA
;
1006 if (!hapd
->ubus
.obj
.has_subscribers
)
1007 return WLAN_STATUS_SUCCESS
;
1009 if (req
->type
< ARRAY_SIZE(types
))
1010 type
= types
[req
->type
];
1012 blob_buf_init(&b
, 0);
1013 blobmsg_add_macaddr(&b
, "address", addr
);
1014 if (req
->mgmt_frame
)
1015 blobmsg_add_macaddr(&b
, "target", req
->mgmt_frame
->da
);
1016 if (req
->frame_info
)
1017 blobmsg_add_u32(&b
, "signal", req
->frame_info
->ssi_signal
);
1018 blobmsg_add_u32(&b
, "freq", hapd
->iface
->freq
);
1020 if (!hapd
->ubus
.notify_response
) {
1021 ubus_notify(ctx
, &hapd
->ubus
.obj
, type
, b
.head
, -1);
1022 return WLAN_STATUS_SUCCESS
;
1025 if (ubus_notify_async(ctx
, &hapd
->ubus
.obj
, type
, b
.head
, &ureq
.nreq
))
1026 return WLAN_STATUS_SUCCESS
;
1028 ureq
.nreq
.status_cb
= ubus_event_cb
;
1029 ubus_complete_request(ctx
, &ureq
.nreq
.req
, 100);
1034 return WLAN_STATUS_SUCCESS
;
1037 void hostapd_ubus_notify(struct hostapd_data
*hapd
, const char *type
, const u8
*addr
)
1039 if (!hapd
->ubus
.obj
.has_subscribers
)
1045 blob_buf_init(&b
, 0);
1046 blobmsg_add_macaddr(&b
, "address", addr
);
1048 ubus_notify(ctx
, &hapd
->ubus
.obj
, type
, b
.head
, -1);