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
);
206 static const struct blobmsg_policy notify_policy
[__NOTIFY_MAX
] = {
207 [NOTIFY_RESPONSE
] = { "notify_response", BLOBMSG_TYPE_INT32
},
211 hostapd_notify_response(struct ubus_context
*ctx
, struct ubus_object
*obj
,
212 struct ubus_request_data
*req
, const char *method
,
213 struct blob_attr
*msg
)
215 struct blob_attr
*tb
[__NOTIFY_MAX
];
216 struct hostapd_data
*hapd
= get_hapd_from_object(obj
);
217 struct wpabuf
*elems
;
221 blobmsg_parse(notify_policy
, __NOTIFY_MAX
, tb
,
222 blob_data(msg
), blob_len(msg
));
224 if (!tb
[NOTIFY_RESPONSE
])
225 return UBUS_STATUS_INVALID_ARGUMENT
;
227 hapd
->ubus
.notify_response
= blobmsg_get_u32(tb
[NOTIFY_RESPONSE
]);
229 return UBUS_STATUS_OK
;
240 static const struct blobmsg_policy del_policy
[__DEL_CLIENT_MAX
] = {
241 [DEL_CLIENT_ADDR
] = { "addr", BLOBMSG_TYPE_STRING
},
242 [DEL_CLIENT_REASON
] = { "reason", BLOBMSG_TYPE_INT32
},
243 [DEL_CLIENT_DEAUTH
] = { "deauth", BLOBMSG_TYPE_INT8
},
244 [DEL_CLIENT_BAN_TIME
] = { "ban_time", BLOBMSG_TYPE_INT32
},
248 hostapd_bss_del_client(struct ubus_context
*ctx
, struct ubus_object
*obj
,
249 struct ubus_request_data
*req
, const char *method
,
250 struct blob_attr
*msg
)
252 struct blob_attr
*tb
[__DEL_CLIENT_MAX
];
253 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
254 struct sta_info
*sta
;
259 blobmsg_parse(del_policy
, __DEL_CLIENT_MAX
, tb
, blob_data(msg
), blob_len(msg
));
261 if (!tb
[DEL_CLIENT_ADDR
])
262 return UBUS_STATUS_INVALID_ARGUMENT
;
264 if (hwaddr_aton(blobmsg_data(tb
[DEL_CLIENT_ADDR
]), addr
))
265 return UBUS_STATUS_INVALID_ARGUMENT
;
267 if (tb
[DEL_CLIENT_REASON
])
268 reason
= blobmsg_get_u32(tb
[DEL_CLIENT_REASON
]);
270 if (tb
[DEL_CLIENT_DEAUTH
])
271 deauth
= blobmsg_get_bool(tb
[DEL_CLIENT_DEAUTH
]);
273 sta
= ap_get_sta(hapd
, addr
);
276 hostapd_drv_sta_deauth(hapd
, addr
, reason
);
277 ap_sta_deauthenticate(hapd
, sta
, reason
);
279 hostapd_drv_sta_disassoc(hapd
, addr
, reason
);
280 ap_sta_disassociate(hapd
, sta
, reason
);
284 if (tb
[DEL_CLIENT_BAN_TIME
])
285 hostapd_bss_ban_client(hapd
, addr
, blobmsg_get_u32(tb
[DEL_CLIENT_BAN_TIME
]));
291 blobmsg_add_macaddr(struct blob_buf
*buf
, const char *name
, const u8
*addr
)
295 s
= blobmsg_alloc_string_buffer(buf
, name
, 20);
296 sprintf(s
, MACSTR
, MAC2STR(addr
));
297 blobmsg_add_string_buffer(buf
);
301 hostapd_bss_list_bans(struct ubus_context
*ctx
, struct ubus_object
*obj
,
302 struct ubus_request_data
*req
, const char *method
,
303 struct blob_attr
*msg
)
305 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
306 struct ubus_banned_client
*ban
;
309 blob_buf_init(&b
, 0);
310 c
= blobmsg_open_array(&b
, "clients");
311 avl_for_each_element(&hapd
->ubus
.banned
, ban
, avl
)
312 blobmsg_add_macaddr(&b
, NULL
, ban
->addr
);
313 blobmsg_close_array(&b
, c
);
314 ubus_send_reply(ctx
, req
, b
.head
);
320 hostapd_bss_wps_start(struct ubus_context
*ctx
, struct ubus_object
*obj
,
321 struct ubus_request_data
*req
, const char *method
,
322 struct blob_attr
*msg
)
325 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
327 rc
= hostapd_wps_button_pushed(hapd
, NULL
);
330 return UBUS_STATUS_NOT_SUPPORTED
;
336 hostapd_bss_wps_cancel(struct ubus_context
*ctx
, struct ubus_object
*obj
,
337 struct ubus_request_data
*req
, const char *method
,
338 struct blob_attr
*msg
)
341 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
343 rc
= hostapd_wps_cancel(hapd
);
346 return UBUS_STATUS_NOT_SUPPORTED
;
352 hostapd_bss_update_beacon(struct ubus_context
*ctx
, struct ubus_object
*obj
,
353 struct ubus_request_data
*req
, const char *method
,
354 struct blob_attr
*msg
)
357 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
359 rc
= ieee802_11_set_beacon(hapd
);
362 return UBUS_STATUS_NOT_SUPPORTED
;
373 static const struct blobmsg_policy csa_policy
[__CSA_MAX
] = {
375 * for now, frequency and beacon count are enough, add more
376 * parameters on demand
378 [CSA_FREQ
] = { "freq", BLOBMSG_TYPE_INT32
},
379 [CSA_BCN_COUNT
] = { "bcn_count", BLOBMSG_TYPE_INT32
},
384 hostapd_switch_chan(struct ubus_context
*ctx
, struct ubus_object
*obj
,
385 struct ubus_request_data
*req
, const char *method
,
386 struct blob_attr
*msg
)
388 struct blob_attr
*tb
[__CSA_MAX
];
389 struct hostapd_data
*hapd
= get_hapd_from_object(obj
);
390 struct csa_settings css
;
392 blobmsg_parse(csa_policy
, __CSA_MAX
, tb
, blob_data(msg
), blob_len(msg
));
395 return UBUS_STATUS_INVALID_ARGUMENT
;
397 memset(&css
, 0, sizeof(css
));
398 css
.freq_params
.freq
= blobmsg_get_u32(tb
[CSA_FREQ
]);
399 if (tb
[CSA_BCN_COUNT
])
400 css
.cs_count
= blobmsg_get_u32(tb
[CSA_BCN_COUNT
]);
402 if (hostapd_switch_channel(hapd
, &css
) != 0)
403 return UBUS_STATUS_NOT_SUPPORTED
;
404 return UBUS_STATUS_OK
;
410 __VENDOR_ELEMENTS_MAX
413 static const struct blobmsg_policy ve_policy
[__VENDOR_ELEMENTS_MAX
] = {
414 /* vendor elements are provided as hex-string */
415 [VENDOR_ELEMENTS
] = { "vendor_elements", BLOBMSG_TYPE_STRING
},
419 hostapd_vendor_elements(struct ubus_context
*ctx
, struct ubus_object
*obj
,
420 struct ubus_request_data
*req
, const char *method
,
421 struct blob_attr
*msg
)
423 struct blob_attr
*tb
[__VENDOR_ELEMENTS_MAX
];
424 struct hostapd_data
*hapd
= get_hapd_from_object(obj
);
425 struct hostapd_bss_config
*bss
= hapd
->conf
;
426 struct wpabuf
*elems
;
430 blobmsg_parse(ve_policy
, __VENDOR_ELEMENTS_MAX
, tb
,
431 blob_data(msg
), blob_len(msg
));
433 if (!tb
[VENDOR_ELEMENTS
])
434 return UBUS_STATUS_INVALID_ARGUMENT
;
436 pos
= blobmsg_data(tb
[VENDOR_ELEMENTS
]);
437 len
= os_strlen(pos
);
439 return UBUS_STATUS_INVALID_ARGUMENT
;
443 wpabuf_free(bss
->vendor_elements
);
444 bss
->vendor_elements
= NULL
;
448 elems
= wpabuf_alloc(len
);
452 if (hexstr2bin(pos
, wpabuf_put(elems
, len
), len
)) {
454 return UBUS_STATUS_INVALID_ARGUMENT
;
457 wpabuf_free(bss
->vendor_elements
);
458 bss
->vendor_elements
= elems
;
460 /* update beacons if vendor elements were set successfully */
461 if (ieee802_11_update_beacons(hapd
->iface
) != 0)
462 return UBUS_STATUS_NOT_SUPPORTED
;
463 return UBUS_STATUS_OK
;
467 hostapd_rrm_print_nr(struct hostapd_neighbor_entry
*nr
)
473 blobmsg_printf(&b
, "", MACSTR
, MAC2STR(nr
->bssid
));
475 str
= blobmsg_alloc_string_buffer(&b
, "", nr
->ssid
.ssid_len
+ 1);
476 memcpy(str
, nr
->ssid
.ssid
, nr
->ssid
.ssid_len
);
477 str
[nr
->ssid
.ssid_len
] = 0;
478 blobmsg_add_string_buffer(&b
);
480 len
= wpabuf_len(nr
->nr
);
481 str
= blobmsg_alloc_string_buffer(&b
, "", 2 * len
+ 1);
482 wpa_snprintf_hex(str
, 2 * len
+ 1, wpabuf_head_u8(nr
->nr
), len
);
483 blobmsg_add_string_buffer(&b
);
487 BSS_MGMT_EN_NEIGHBOR
,
490 BSS_MGMT_EN_BSS_TRANSITION
,
496 __hostapd_bss_mgmt_enable_f(struct hostapd_data
*hapd
, int flag
)
498 struct hostapd_bss_config
*bss
= hapd
->conf
;
502 case BSS_MGMT_EN_NEIGHBOR
:
503 if (bss
->radio_measurements
[0] &
504 WLAN_RRM_CAPS_NEIGHBOR_REPORT
)
507 bss
->radio_measurements
[0] |=
508 WLAN_RRM_CAPS_NEIGHBOR_REPORT
;
509 hostapd_set_own_neighbor_report(hapd
);
511 case BSS_MGMT_EN_BEACON
:
512 flags
= WLAN_RRM_CAPS_BEACON_REPORT_PASSIVE
|
513 WLAN_RRM_CAPS_BEACON_REPORT_ACTIVE
|
514 WLAN_RRM_CAPS_BEACON_REPORT_TABLE
;
516 if (bss
->radio_measurements
[0] & flags
== flags
)
519 bss
->radio_measurements
[0] |= (u8
) flags
;
522 case BSS_MGMT_EN_BSS_TRANSITION
:
523 if (bss
->bss_transition
)
526 bss
->bss_transition
= 1;
533 __hostapd_bss_mgmt_enable(struct hostapd_data
*hapd
, uint32_t flags
)
538 for (i
= 0; i
< __BSS_MGMT_EN_MAX
; i
++) {
539 if (!(flags
& (1 << i
)))
542 update
|= __hostapd_bss_mgmt_enable_f(hapd
, i
);
546 ieee802_11_update_beacons(hapd
->iface
);
550 static const struct blobmsg_policy bss_mgmt_enable_policy
[__BSS_MGMT_EN_MAX
] = {
551 [BSS_MGMT_EN_NEIGHBOR
] = { "neighbor_report", BLOBMSG_TYPE_BOOL
},
552 [BSS_MGMT_EN_BEACON
] = { "beacon_report", BLOBMSG_TYPE_BOOL
},
554 [BSS_MGMT_EN_BSS_TRANSITION
] = { "bss_transition", BLOBMSG_TYPE_BOOL
},
559 hostapd_bss_mgmt_enable(struct ubus_context
*ctx
, struct ubus_object
*obj
,
560 struct ubus_request_data
*req
, const char *method
,
561 struct blob_attr
*msg
)
564 struct hostapd_data
*hapd
= get_hapd_from_object(obj
);
565 struct blob_attr
*tb
[__BSS_MGMT_EN_MAX
];
566 struct blob_attr
*cur
;
569 bool neigh
= false, beacon
= false;
571 blobmsg_parse(bss_mgmt_enable_policy
, __BSS_MGMT_EN_MAX
, tb
, blob_data(msg
), blob_len(msg
));
573 for (i
= 0; i
< ARRAY_SIZE(tb
); i
++) {
574 if (!tb
[i
] || !blobmsg_get_bool(tb
[i
]))
580 __hostapd_bss_mgmt_enable(hapd
, flags
);
585 hostapd_rrm_nr_enable(struct hostapd_data
*hapd
)
587 __hostapd_bss_mgmt_enable(hapd
, 1 << BSS_MGMT_EN_NEIGHBOR
);
591 hostapd_rrm_nr_get_own(struct ubus_context
*ctx
, struct ubus_object
*obj
,
592 struct ubus_request_data
*req
, const char *method
,
593 struct blob_attr
*msg
)
595 struct hostapd_data
*hapd
= get_hapd_from_object(obj
);
596 struct hostapd_neighbor_entry
*nr
;
599 hostapd_rrm_nr_enable(hapd
);
601 nr
= hostapd_neighbor_get(hapd
, hapd
->own_addr
, NULL
);
603 return UBUS_STATUS_NOT_FOUND
;
605 blob_buf_init(&b
, 0);
607 c
= blobmsg_open_array(&b
, "value");
608 hostapd_rrm_print_nr(nr
);
609 blobmsg_close_array(&b
, c
);
611 ubus_send_reply(ctx
, req
, b
.head
);
617 hostapd_rrm_nr_list(struct ubus_context
*ctx
, struct ubus_object
*obj
,
618 struct ubus_request_data
*req
, const char *method
,
619 struct blob_attr
*msg
)
621 struct hostapd_data
*hapd
= get_hapd_from_object(obj
);
622 struct hostapd_neighbor_entry
*nr
;
625 hostapd_rrm_nr_enable(hapd
);
626 blob_buf_init(&b
, 0);
628 c
= blobmsg_open_array(&b
, "list");
629 dl_list_for_each(nr
, &hapd
->nr_db
, struct hostapd_neighbor_entry
, list
) {
632 if (!memcmp(nr
->bssid
, hapd
->own_addr
, ETH_ALEN
))
635 cur
= blobmsg_open_array(&b
, NULL
);
636 hostapd_rrm_print_nr(nr
);
637 blobmsg_close_array(&b
, cur
);
639 blobmsg_close_array(&b
, c
);
641 ubus_send_reply(ctx
, req
, b
.head
);
651 static const struct blobmsg_policy nr_set_policy
[__NR_SET_LIST_MAX
] = {
652 [NR_SET_LIST
] = { "list", BLOBMSG_TYPE_ARRAY
},
657 hostapd_rrm_nr_clear(struct hostapd_data
*hapd
)
659 struct hostapd_neighbor_entry
*nr
;
662 dl_list_for_each(nr
, &hapd
->nr_db
, struct hostapd_neighbor_entry
, list
) {
663 if (!memcmp(nr
->bssid
, hapd
->own_addr
, ETH_ALEN
))
666 hostapd_neighbor_remove(hapd
, nr
->bssid
, &nr
->ssid
);
672 hostapd_rrm_nr_set(struct ubus_context
*ctx
, struct ubus_object
*obj
,
673 struct ubus_request_data
*req
, const char *method
,
674 struct blob_attr
*msg
)
676 static const struct blobmsg_policy nr_e_policy
[] = {
677 { .type
= BLOBMSG_TYPE_STRING
},
678 { .type
= BLOBMSG_TYPE_STRING
},
679 { .type
= BLOBMSG_TYPE_STRING
},
681 struct hostapd_data
*hapd
= get_hapd_from_object(obj
);
682 struct blob_attr
*tb_l
[__NR_SET_LIST_MAX
];
683 struct blob_attr
*tb
[ARRAY_SIZE(nr_e_policy
)];
684 struct blob_attr
*cur
;
688 hostapd_rrm_nr_enable(hapd
);
690 blobmsg_parse(nr_set_policy
, __NR_SET_LIST_MAX
, tb_l
, blob_data(msg
), blob_len(msg
));
691 if (!tb_l
[NR_SET_LIST
])
692 return UBUS_STATUS_INVALID_ARGUMENT
;
694 hostapd_rrm_nr_clear(hapd
);
695 blobmsg_for_each_attr(cur
, tb_l
[NR_SET_LIST
], rem
) {
696 struct wpa_ssid_value ssid
;
701 blobmsg_parse_array(nr_e_policy
, ARRAY_SIZE(nr_e_policy
), tb
, blobmsg_data(cur
), blobmsg_data_len(cur
));
702 if (!tb
[0] || !tb
[1] || !tb
[2])
705 s
= blobmsg_get_string(tb
[0]);
706 if (hwaddr_aton(s
, bssid
))
709 s
= blobmsg_get_string(tb
[1]);
710 ssid
.ssid_len
= strlen(s
);
711 if (ssid
.ssid_len
> sizeof(ssid
.ssid
))
714 memcpy(&ssid
, s
, ssid
.ssid_len
);
715 data
= wpabuf_parse_bin(blobmsg_get_string(tb
[2]));
719 hostapd_neighbor_set(hapd
, bssid
, &ssid
, data
, NULL
, NULL
, 0);
724 ret
= UBUS_STATUS_INVALID_ARGUMENT
;
741 static const struct blobmsg_policy beacon_req_policy
[__BEACON_REQ_MAX
] = {
742 [BEACON_REQ_ADDR
] = { "addr", BLOBMSG_TYPE_STRING
},
743 [BEACON_REQ_OP_CLASS
] { "op_class", BLOBMSG_TYPE_INT32
},
744 [BEACON_REQ_CHANNEL
] { "channel", BLOBMSG_TYPE_INT32
},
745 [BEACON_REQ_DURATION
] { "duration", BLOBMSG_TYPE_INT32
},
746 [BEACON_REQ_MODE
] { "mode", BLOBMSG_TYPE_INT32
},
747 [BEACON_REQ_BSSID
] { "bssid", BLOBMSG_TYPE_STRING
},
748 [BEACON_REQ_SSID
] { "ssid", BLOBMSG_TYPE_STRING
},
752 hostapd_rrm_beacon_req(struct ubus_context
*ctx
, struct ubus_object
*obj
,
753 struct ubus_request_data
*ureq
, const char *method
,
754 struct blob_attr
*msg
)
756 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
757 struct blob_attr
*tb
[__BEACON_REQ_MAX
];
758 struct blob_attr
*cur
;
760 u8 bssid
[ETH_ALEN
] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
765 blobmsg_parse(beacon_req_policy
, __BEACON_REQ_MAX
, tb
, blob_data(msg
), blob_len(msg
));
767 if (!tb
[BEACON_REQ_ADDR
] || !tb
[BEACON_REQ_MODE
] || !tb
[BEACON_REQ_DURATION
] ||
768 !tb
[BEACON_REQ_OP_CLASS
] || !tb
[BEACON_REQ_CHANNEL
])
769 return UBUS_STATUS_INVALID_ARGUMENT
;
771 if (tb
[BEACON_REQ_SSID
])
772 buf_len
+= blobmsg_data_len(tb
[BEACON_REQ_SSID
]) + 2 - 1;
774 mode
= blobmsg_get_u32(tb
[BEACON_REQ_MODE
]);
775 if (hwaddr_aton(blobmsg_data(tb
[BEACON_REQ_ADDR
]), addr
))
776 return UBUS_STATUS_INVALID_ARGUMENT
;
778 if (tb
[BEACON_REQ_BSSID
] &&
779 hwaddr_aton(blobmsg_data(tb
[BEACON_REQ_BSSID
]), bssid
))
780 return UBUS_STATUS_INVALID_ARGUMENT
;
782 req
= wpabuf_alloc(buf_len
);
784 return UBUS_STATUS_UNKNOWN_ERROR
;
786 /* 1: regulatory class */
787 wpabuf_put_u8(req
, blobmsg_get_u32(tb
[BEACON_REQ_OP_CLASS
]));
789 /* 2: channel number */
790 wpabuf_put_u8(req
, blobmsg_get_u32(tb
[BEACON_REQ_CHANNEL
]));
792 /* 3-4: randomization interval */
793 wpabuf_put_le16(req
, 0);
796 wpabuf_put_le16(req
, blobmsg_get_u32(tb
[BEACON_REQ_DURATION
]));
799 wpabuf_put_u8(req
, blobmsg_get_u32(tb
[BEACON_REQ_MODE
]));
802 wpabuf_put_data(req
, bssid
, ETH_ALEN
);
804 if ((cur
= tb
[BEACON_REQ_SSID
]) != NULL
) {
805 wpabuf_put_u8(req
, WLAN_EID_SSID
);
806 wpabuf_put_u8(req
, blobmsg_data_len(cur
) - 1);
807 wpabuf_put_data(req
, blobmsg_data(cur
), blobmsg_data_len(cur
) - 1);
810 ret
= hostapd_send_beacon_req(hapd
, addr
, 0, req
);
821 WNM_DISASSOC_DURATION
,
822 WNM_DISASSOC_NEIGHBORS
,
826 static const struct blobmsg_policy wnm_disassoc_policy
[__WNM_DISASSOC_MAX
] = {
827 [WNM_DISASSOC_ADDR
] = { "addr", BLOBMSG_TYPE_STRING
},
828 [WNM_DISASSOC_DURATION
] { "duration", BLOBMSG_TYPE_INT32
},
829 [WNM_DISASSOC_NEIGHBORS
] { "neighbors", BLOBMSG_TYPE_ARRAY
},
833 hostapd_wnm_disassoc_imminent(struct ubus_context
*ctx
, struct ubus_object
*obj
,
834 struct ubus_request_data
*ureq
, const char *method
,
835 struct blob_attr
*msg
)
837 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
838 struct blob_attr
*tb
[__WNM_DISASSOC_MAX
];
839 struct blob_attr
*cur
;
840 struct sta_info
*sta
;
845 u8 req_mode
= WNM_BSS_TM_REQ_DISASSOC_IMMINENT
;
848 blobmsg_parse(wnm_disassoc_policy
, __WNM_DISASSOC_MAX
, tb
, blob_data(msg
), blob_len(msg
));
850 if (!tb
[WNM_DISASSOC_ADDR
])
851 return UBUS_STATUS_INVALID_ARGUMENT
;
853 if (hwaddr_aton(blobmsg_data(tb
[WNM_DISASSOC_ADDR
]), addr
))
854 return UBUS_STATUS_INVALID_ARGUMENT
;
856 if ((cur
= tb
[WNM_DISASSOC_DURATION
]) != NULL
)
857 duration
= blobmsg_get_u32(cur
);
859 sta
= ap_get_sta(hapd
, addr
);
861 return UBUS_STATUS_NOT_FOUND
;
863 if (tb
[WNM_DISASSOC_NEIGHBORS
]) {
866 if (blobmsg_check_array(tb
[WNM_DISASSOC_NEIGHBORS
],
867 BLOBMSG_TYPE_STRING
) < 0)
868 return UBUS_STATUS_INVALID_ARGUMENT
;
870 blobmsg_for_each_attr(cur
, tb
[WNM_DISASSOC_NEIGHBORS
], rem
) {
871 int len
= strlen(blobmsg_get_string(cur
));
874 return UBUS_STATUS_INVALID_ARGUMENT
;
876 nr_len
+= (len
/ 2) + 2;
880 nr
= os_zalloc(nr_len
);
882 return UBUS_STATUS_UNKNOWN_ERROR
;
886 blobmsg_for_each_attr(cur
, tb
[WNM_DISASSOC_NEIGHBORS
], rem
) {
887 int len
= strlen(blobmsg_get_string(cur
)) / 2;
889 *nr_cur
++ = WLAN_EID_NEIGHBOR_REPORT
;
890 *nr_cur
++ = (u8
) len
;
891 if (hexstr2bin(blobmsg_data(cur
), nr_cur
, len
)) {
893 return UBUS_STATUS_INVALID_ARGUMENT
;
901 req_mode
|= WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED
;
903 if (wnm_send_bss_tm_req(hapd
, sta
, req_mode
, duration
, 0, NULL
,
904 NULL
, nr
, nr_len
, NULL
, 0))
905 return UBUS_STATUS_UNKNOWN_ERROR
;
911 static const struct ubus_method bss_methods
[] = {
912 UBUS_METHOD_NOARG("get_clients", hostapd_bss_get_clients
),
913 UBUS_METHOD("del_client", hostapd_bss_del_client
, del_policy
),
914 UBUS_METHOD_NOARG("list_bans", hostapd_bss_list_bans
),
915 UBUS_METHOD_NOARG("wps_start", hostapd_bss_wps_start
),
916 UBUS_METHOD_NOARG("wps_cancel", hostapd_bss_wps_cancel
),
917 UBUS_METHOD_NOARG("update_beacon", hostapd_bss_update_beacon
),
919 UBUS_METHOD("switch_chan", hostapd_switch_chan
, csa_policy
),
921 UBUS_METHOD("set_vendor_elements", hostapd_vendor_elements
, ve_policy
),
922 UBUS_METHOD("notify_response", hostapd_notify_response
, notify_policy
),
923 UBUS_METHOD("bss_mgmt_enable", hostapd_bss_mgmt_enable
, bss_mgmt_enable_policy
),
924 UBUS_METHOD_NOARG("rrm_nr_get_own", hostapd_rrm_nr_get_own
),
925 UBUS_METHOD_NOARG("rrm_nr_list", hostapd_rrm_nr_list
),
926 UBUS_METHOD("rrm_nr_set", hostapd_rrm_nr_set
, nr_set_policy
),
927 UBUS_METHOD("rrm_beacon_req", hostapd_rrm_beacon_req
, beacon_req_policy
),
929 UBUS_METHOD("wnm_disassoc_imminent", hostapd_wnm_disassoc_imminent
, wnm_disassoc_policy
),
933 static struct ubus_object_type bss_object_type
=
934 UBUS_OBJECT_TYPE("hostapd_bss", bss_methods
);
936 static int avl_compare_macaddr(const void *k1
, const void *k2
, void *ptr
)
938 return memcmp(k1
, k2
, ETH_ALEN
);
941 void hostapd_ubus_add_bss(struct hostapd_data
*hapd
)
943 struct ubus_object
*obj
= &hapd
->ubus
.obj
;
948 if (hapd
->conf
->mesh
& MESH_ENABLED
)
952 if (!hostapd_ubus_init())
955 if (asprintf(&name
, "hostapd.%s", hapd
->conf
->iface
) < 0)
958 avl_init(&hapd
->ubus
.banned
, avl_compare_macaddr
, false, NULL
);
960 obj
->type
= &bss_object_type
;
961 obj
->methods
= bss_object_type
.methods
;
962 obj
->n_methods
= bss_object_type
.n_methods
;
963 ret
= ubus_add_object(ctx
, obj
);
964 hostapd_ubus_ref_inc();
967 void hostapd_ubus_free_bss(struct hostapd_data
*hapd
)
969 struct ubus_object
*obj
= &hapd
->ubus
.obj
;
970 char *name
= (char *) obj
->name
;
976 ubus_remove_object(ctx
, obj
);
977 hostapd_ubus_ref_dec();
983 struct ubus_event_req
{
984 struct ubus_notify_request nreq
;
989 ubus_event_cb(struct ubus_notify_request
*req
, int idx
, int ret
)
991 struct ubus_event_req
*ureq
= container_of(req
, struct ubus_event_req
, nreq
);
996 int hostapd_ubus_handle_event(struct hostapd_data
*hapd
, struct hostapd_ubus_request
*req
)
998 struct ubus_banned_client
*ban
;
999 const char *types
[HOSTAPD_UBUS_TYPE_MAX
] = {
1000 [HOSTAPD_UBUS_PROBE_REQ
] = "probe",
1001 [HOSTAPD_UBUS_AUTH_REQ
] = "auth",
1002 [HOSTAPD_UBUS_ASSOC_REQ
] = "assoc",
1004 const char *type
= "mgmt";
1005 struct ubus_event_req ureq
= {};
1008 if (req
->mgmt_frame
)
1009 addr
= req
->mgmt_frame
->sa
;
1013 ban
= avl_find_element(&hapd
->ubus
.banned
, addr
, ban
, avl
);
1015 return WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA
;
1017 if (!hapd
->ubus
.obj
.has_subscribers
)
1018 return WLAN_STATUS_SUCCESS
;
1020 if (req
->type
< ARRAY_SIZE(types
))
1021 type
= types
[req
->type
];
1023 blob_buf_init(&b
, 0);
1024 blobmsg_add_macaddr(&b
, "address", addr
);
1025 if (req
->mgmt_frame
)
1026 blobmsg_add_macaddr(&b
, "target", req
->mgmt_frame
->da
);
1027 if (req
->frame_info
)
1028 blobmsg_add_u32(&b
, "signal", req
->frame_info
->ssi_signal
);
1029 blobmsg_add_u32(&b
, "freq", hapd
->iface
->freq
);
1031 if (!hapd
->ubus
.notify_response
) {
1032 ubus_notify(ctx
, &hapd
->ubus
.obj
, type
, b
.head
, -1);
1033 return WLAN_STATUS_SUCCESS
;
1036 if (ubus_notify_async(ctx
, &hapd
->ubus
.obj
, type
, b
.head
, &ureq
.nreq
))
1037 return WLAN_STATUS_SUCCESS
;
1039 ureq
.nreq
.status_cb
= ubus_event_cb
;
1040 ubus_complete_request(ctx
, &ureq
.nreq
.req
, 100);
1045 return WLAN_STATUS_SUCCESS
;
1048 void hostapd_ubus_notify(struct hostapd_data
*hapd
, const char *type
, const u8
*addr
)
1050 if (!hapd
->ubus
.obj
.has_subscribers
)
1056 blob_buf_init(&b
, 0);
1057 blobmsg_add_macaddr(&b
, "address", addr
);
1059 ubus_notify(ctx
, &hapd
->ubus
.obj
, type
, b
.head
, -1);