2 * hostapd / ubus support
3 * Copyright (c) 2013, Felix Fietkau <nbd@nbd.name>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
10 #include "utils/common.h"
11 #include "utils/eloop.h"
12 #include "utils/wpabuf.h"
13 #include "common/ieee802_11_defs.h"
15 #include "neighbor_db.h"
16 #include "wps_hostapd.h"
19 #include "ap_drv_ops.h"
25 static struct ubus_context
*ctx
;
26 static struct blob_buf b
;
29 static inline struct hapd_interfaces
*get_hapd_interfaces_from_object(struct ubus_object
*obj
)
31 return container_of(obj
, struct hapd_interfaces
, ubus
);
34 static inline struct hostapd_data
*get_hapd_from_object(struct ubus_object
*obj
)
36 return container_of(obj
, struct hostapd_data
, ubus
.obj
);
39 struct ubus_banned_client
{
44 static void ubus_receive(int sock
, void *eloop_ctx
, void *sock_ctx
)
46 struct ubus_context
*ctx
= eloop_ctx
;
47 ubus_handle_event(ctx
);
50 static void ubus_reconnect_timeout(void *eloop_data
, void *user_ctx
)
52 if (ubus_reconnect(ctx
, NULL
)) {
53 eloop_register_timeout(1, 0, ubus_reconnect_timeout
, ctx
, NULL
);
57 eloop_register_read_sock(ctx
->sock
.fd
, ubus_receive
, ctx
, NULL
);
60 static void hostapd_ubus_connection_lost(struct ubus_context
*ctx
)
62 eloop_unregister_read_sock(ctx
->sock
.fd
);
63 eloop_register_timeout(1, 0, ubus_reconnect_timeout
, ctx
, NULL
);
66 static bool hostapd_ubus_init(void)
71 ctx
= ubus_connect(NULL
);
75 ctx
->connection_lost
= hostapd_ubus_connection_lost
;
76 eloop_register_read_sock(ctx
->sock
.fd
, ubus_receive
, ctx
, NULL
);
80 static void hostapd_ubus_ref_inc(void)
85 static void hostapd_ubus_ref_dec(void)
94 eloop_unregister_read_sock(ctx
->sock
.fd
);
99 void hostapd_ubus_add_iface(struct hostapd_iface
*iface
)
101 if (!hostapd_ubus_init())
105 void hostapd_ubus_free_iface(struct hostapd_iface
*iface
)
111 static void hostapd_notify_ubus(struct ubus_object
*obj
, char *bssname
, char *event
)
118 if (asprintf(&event_type
, "bss.%s", event
) < 0)
121 blob_buf_init(&b
, 0);
122 blobmsg_add_string(&b
, "name", bssname
);
123 ubus_notify(ctx
, obj
, event_type
, b
.head
, -1);
127 static void hostapd_send_procd_event(char *bssname
, char *event
)
133 if (!ctx
|| ubus_lookup_id(ctx
, "service", &id
))
136 if (asprintf(&name
, "hostapd.%s.%s", bssname
, event
) < 0)
139 blob_buf_init(&b
, 0);
141 s
= blobmsg_alloc_string_buffer(&b
, "type", strlen(name
) + 1);
142 sprintf(s
, "%s", name
);
143 blobmsg_add_string_buffer(&b
);
145 v
= blobmsg_open_table(&b
, "data");
146 blobmsg_close_table(&b
, v
);
148 ubus_invoke(ctx
, id
, "event", b
.head
, NULL
, NULL
, 1000);
153 static void hostapd_send_shared_event(struct ubus_object
*obj
, char *bssname
, char *event
)
155 hostapd_send_procd_event(bssname
, event
);
156 hostapd_notify_ubus(obj
, bssname
, event
);
160 hostapd_bss_del_ban(void *eloop_data
, void *user_ctx
)
162 struct ubus_banned_client
*ban
= eloop_data
;
163 struct hostapd_data
*hapd
= user_ctx
;
165 avl_delete(&hapd
->ubus
.banned
, &ban
->avl
);
170 hostapd_bss_ban_client(struct hostapd_data
*hapd
, u8
*addr
, int time
)
172 struct ubus_banned_client
*ban
;
177 ban
= avl_find_element(&hapd
->ubus
.banned
, addr
, ban
, avl
);
182 ban
= os_zalloc(sizeof(*ban
));
183 memcpy(ban
->addr
, addr
, sizeof(ban
->addr
));
184 ban
->avl
.key
= ban
->addr
;
185 avl_insert(&hapd
->ubus
.banned
, &ban
->avl
);
187 eloop_cancel_timeout(hostapd_bss_del_ban
, ban
, hapd
);
189 hostapd_bss_del_ban(ban
, hapd
);
194 eloop_register_timeout(0, time
* 1000, hostapd_bss_del_ban
, ban
, hapd
);
198 hostapd_bss_reload(struct ubus_context
*ctx
, struct ubus_object
*obj
,
199 struct ubus_request_data
*req
, const char *method
,
200 struct blob_attr
*msg
)
202 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
203 int ret
= hostapd_reload_config(hapd
->iface
, 1);
205 hostapd_send_shared_event(&hapd
->iface
->interfaces
->ubus
, hapd
->conf
->iface
, "reload");
210 hostapd_bss_get_clients(struct ubus_context
*ctx
, struct ubus_object
*obj
,
211 struct ubus_request_data
*req
, const char *method
,
212 struct blob_attr
*msg
)
214 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
215 struct sta_info
*sta
;
218 static const struct {
222 { "auth", WLAN_STA_AUTH
},
223 { "assoc", WLAN_STA_ASSOC
},
224 { "authorized", WLAN_STA_AUTHORIZED
},
225 { "preauth", WLAN_STA_PREAUTH
},
226 { "wds", WLAN_STA_WDS
},
227 { "wmm", WLAN_STA_WMM
},
228 { "ht", WLAN_STA_HT
},
229 { "vht", WLAN_STA_VHT
},
230 { "wps", WLAN_STA_WPS
},
231 { "mfp", WLAN_STA_MFP
},
234 blob_buf_init(&b
, 0);
235 blobmsg_add_u32(&b
, "freq", hapd
->iface
->freq
);
236 list
= blobmsg_open_table(&b
, "clients");
237 for (sta
= hapd
->sta_list
; sta
; sta
= sta
->next
) {
241 sprintf(mac_buf
, MACSTR
, MAC2STR(sta
->addr
));
242 c
= blobmsg_open_table(&b
, mac_buf
);
243 for (i
= 0; i
< ARRAY_SIZE(sta_flags
); i
++)
244 blobmsg_add_u8(&b
, sta_flags
[i
].name
,
245 !!(sta
->flags
& sta_flags
[i
].flag
));
247 r
= blobmsg_open_array(&b
, "rrm");
248 for (i
= 0; i
< ARRAY_SIZE(sta
->rrm_enabled_capa
); i
++)
249 blobmsg_add_u32(&b
, "", sta
->rrm_enabled_capa
[i
]);
250 blobmsg_close_array(&b
, r
);
251 blobmsg_add_u32(&b
, "aid", sta
->aid
);
252 #ifdef CONFIG_TAXONOMY
253 r
= blobmsg_alloc_string_buffer(&b
, "signature", 1024);
254 if (retrieve_sta_taxonomy(hapd
, sta
, r
, 1024) > 0)
255 blobmsg_add_string_buffer(&b
);
257 blobmsg_close_table(&b
, c
);
259 blobmsg_close_array(&b
, list
);
260 ubus_send_reply(ctx
, req
, b
.head
);
266 hostapd_bss_get_features(struct ubus_context
*ctx
, struct ubus_object
*obj
,
267 struct ubus_request_data
*req
, const char *method
,
268 struct blob_attr
*msg
)
270 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
272 blob_buf_init(&b
, 0);
273 blobmsg_add_u8(&b
, "ht_supported", ht_supported(hapd
->iface
->hw_features
));
274 blobmsg_add_u8(&b
, "vht_supported", vht_supported(hapd
->iface
->hw_features
));
275 ubus_send_reply(ctx
, req
, b
.head
);
285 static const struct blobmsg_policy notify_policy
[__NOTIFY_MAX
] = {
286 [NOTIFY_RESPONSE
] = { "notify_response", BLOBMSG_TYPE_INT32
},
290 hostapd_notify_response(struct ubus_context
*ctx
, struct ubus_object
*obj
,
291 struct ubus_request_data
*req
, const char *method
,
292 struct blob_attr
*msg
)
294 struct blob_attr
*tb
[__NOTIFY_MAX
];
295 struct hostapd_data
*hapd
= get_hapd_from_object(obj
);
296 struct wpabuf
*elems
;
300 blobmsg_parse(notify_policy
, __NOTIFY_MAX
, tb
,
301 blob_data(msg
), blob_len(msg
));
303 if (!tb
[NOTIFY_RESPONSE
])
304 return UBUS_STATUS_INVALID_ARGUMENT
;
306 hapd
->ubus
.notify_response
= blobmsg_get_u32(tb
[NOTIFY_RESPONSE
]);
308 return UBUS_STATUS_OK
;
319 static const struct blobmsg_policy del_policy
[__DEL_CLIENT_MAX
] = {
320 [DEL_CLIENT_ADDR
] = { "addr", BLOBMSG_TYPE_STRING
},
321 [DEL_CLIENT_REASON
] = { "reason", BLOBMSG_TYPE_INT32
},
322 [DEL_CLIENT_DEAUTH
] = { "deauth", BLOBMSG_TYPE_INT8
},
323 [DEL_CLIENT_BAN_TIME
] = { "ban_time", BLOBMSG_TYPE_INT32
},
327 hostapd_bss_del_client(struct ubus_context
*ctx
, struct ubus_object
*obj
,
328 struct ubus_request_data
*req
, const char *method
,
329 struct blob_attr
*msg
)
331 struct blob_attr
*tb
[__DEL_CLIENT_MAX
];
332 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
333 struct sta_info
*sta
;
338 blobmsg_parse(del_policy
, __DEL_CLIENT_MAX
, tb
, blob_data(msg
), blob_len(msg
));
340 if (!tb
[DEL_CLIENT_ADDR
])
341 return UBUS_STATUS_INVALID_ARGUMENT
;
343 if (hwaddr_aton(blobmsg_data(tb
[DEL_CLIENT_ADDR
]), addr
))
344 return UBUS_STATUS_INVALID_ARGUMENT
;
346 if (tb
[DEL_CLIENT_REASON
])
347 reason
= blobmsg_get_u32(tb
[DEL_CLIENT_REASON
]);
349 if (tb
[DEL_CLIENT_DEAUTH
])
350 deauth
= blobmsg_get_bool(tb
[DEL_CLIENT_DEAUTH
]);
352 sta
= ap_get_sta(hapd
, addr
);
355 hostapd_drv_sta_deauth(hapd
, addr
, reason
);
356 ap_sta_deauthenticate(hapd
, sta
, reason
);
358 hostapd_drv_sta_disassoc(hapd
, addr
, reason
);
359 ap_sta_disassociate(hapd
, sta
, reason
);
363 if (tb
[DEL_CLIENT_BAN_TIME
])
364 hostapd_bss_ban_client(hapd
, addr
, blobmsg_get_u32(tb
[DEL_CLIENT_BAN_TIME
]));
370 blobmsg_add_macaddr(struct blob_buf
*buf
, const char *name
, const u8
*addr
)
374 s
= blobmsg_alloc_string_buffer(buf
, name
, 20);
375 sprintf(s
, MACSTR
, MAC2STR(addr
));
376 blobmsg_add_string_buffer(buf
);
380 hostapd_bss_list_bans(struct ubus_context
*ctx
, struct ubus_object
*obj
,
381 struct ubus_request_data
*req
, const char *method
,
382 struct blob_attr
*msg
)
384 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
385 struct ubus_banned_client
*ban
;
388 blob_buf_init(&b
, 0);
389 c
= blobmsg_open_array(&b
, "clients");
390 avl_for_each_element(&hapd
->ubus
.banned
, ban
, avl
)
391 blobmsg_add_macaddr(&b
, NULL
, ban
->addr
);
392 blobmsg_close_array(&b
, c
);
393 ubus_send_reply(ctx
, req
, b
.head
);
399 hostapd_bss_wps_start(struct ubus_context
*ctx
, struct ubus_object
*obj
,
400 struct ubus_request_data
*req
, const char *method
,
401 struct blob_attr
*msg
)
404 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
406 rc
= hostapd_wps_button_pushed(hapd
, NULL
);
409 return UBUS_STATUS_NOT_SUPPORTED
;
415 hostapd_bss_wps_cancel(struct ubus_context
*ctx
, struct ubus_object
*obj
,
416 struct ubus_request_data
*req
, const char *method
,
417 struct blob_attr
*msg
)
420 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
422 rc
= hostapd_wps_cancel(hapd
);
425 return UBUS_STATUS_NOT_SUPPORTED
;
431 hostapd_bss_update_beacon(struct ubus_context
*ctx
, struct ubus_object
*obj
,
432 struct ubus_request_data
*req
, const char *method
,
433 struct blob_attr
*msg
)
436 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
438 rc
= ieee802_11_set_beacon(hapd
);
441 return UBUS_STATUS_NOT_SUPPORTED
;
452 static const struct blobmsg_policy config_add_policy
[__CONFIG_MAX
] = {
453 [CONFIG_IFACE
] = { "iface", BLOBMSG_TYPE_STRING
},
454 [CONFIG_FILE
] = { "config", BLOBMSG_TYPE_STRING
},
458 hostapd_config_add(struct ubus_context
*ctx
, struct ubus_object
*obj
,
459 struct ubus_request_data
*req
, const char *method
,
460 struct blob_attr
*msg
)
462 struct blob_attr
*tb
[__CONFIG_MAX
];
463 struct hapd_interfaces
*interfaces
= get_hapd_interfaces_from_object(obj
);
466 blobmsg_parse(config_add_policy
, __CONFIG_MAX
, tb
, blob_data(msg
), blob_len(msg
));
468 if (!tb
[CONFIG_FILE
] || !tb
[CONFIG_IFACE
])
469 return UBUS_STATUS_INVALID_ARGUMENT
;
471 snprintf(buf
, sizeof(buf
), "bss_config=%s:%s",
472 blobmsg_get_string(tb
[CONFIG_IFACE
]),
473 blobmsg_get_string(tb
[CONFIG_FILE
]));
475 if (hostapd_add_iface(interfaces
, buf
))
476 return UBUS_STATUS_INVALID_ARGUMENT
;
478 return UBUS_STATUS_OK
;
486 static const struct blobmsg_policy config_remove_policy
[__CONFIG_REM_MAX
] = {
487 [CONFIG_REM_IFACE
] = { "iface", BLOBMSG_TYPE_STRING
},
491 hostapd_config_remove(struct ubus_context
*ctx
, struct ubus_object
*obj
,
492 struct ubus_request_data
*req
, const char *method
,
493 struct blob_attr
*msg
)
495 struct blob_attr
*tb
[__CONFIG_REM_MAX
];
496 struct hapd_interfaces
*interfaces
= get_hapd_interfaces_from_object(obj
);
499 blobmsg_parse(config_remove_policy
, __CONFIG_REM_MAX
, tb
, blob_data(msg
), blob_len(msg
));
501 if (!tb
[CONFIG_REM_IFACE
])
502 return UBUS_STATUS_INVALID_ARGUMENT
;
504 if (hostapd_remove_iface(interfaces
, blobmsg_get_string(tb
[CONFIG_REM_IFACE
])))
505 return UBUS_STATUS_INVALID_ARGUMENT
;
507 return UBUS_STATUS_OK
;
516 CSA_SEC_CHANNEL_OFFSET
,
523 static const struct blobmsg_policy csa_policy
[__CSA_MAX
] = {
524 [CSA_FREQ
] = { "freq", BLOBMSG_TYPE_INT32
},
525 [CSA_BCN_COUNT
] = { "bcn_count", BLOBMSG_TYPE_INT32
},
526 [CSA_CENTER_FREQ1
] = { "center_freq1", BLOBMSG_TYPE_INT32
},
527 [CSA_CENTER_FREQ2
] = { "center_freq2", BLOBMSG_TYPE_INT32
},
528 [CSA_BANDWIDTH
] = { "bandwidth", BLOBMSG_TYPE_INT32
},
529 [CSA_SEC_CHANNEL_OFFSET
] = { "sec_channel_offset", BLOBMSG_TYPE_INT32
},
530 [CSA_HT
] = { "ht", BLOBMSG_TYPE_BOOL
},
531 [CSA_VHT
] = { "vht", BLOBMSG_TYPE_BOOL
},
532 [CSA_BLOCK_TX
] = { "block_tx", BLOBMSG_TYPE_BOOL
},
537 hostapd_switch_chan(struct ubus_context
*ctx
, struct ubus_object
*obj
,
538 struct ubus_request_data
*req
, const char *method
,
539 struct blob_attr
*msg
)
541 struct blob_attr
*tb
[__CSA_MAX
];
542 struct hostapd_data
*hapd
= get_hapd_from_object(obj
);
543 struct csa_settings css
;
545 blobmsg_parse(csa_policy
, __CSA_MAX
, tb
, blob_data(msg
), blob_len(msg
));
548 return UBUS_STATUS_INVALID_ARGUMENT
;
550 memset(&css
, 0, sizeof(css
));
551 css
.freq_params
.freq
= blobmsg_get_u32(tb
[CSA_FREQ
]);
553 #define SET_CSA_SETTING(name, field, type) \
556 css.field = blobmsg_get_ ## type(tb[name]); \
559 SET_CSA_SETTING(CSA_BCN_COUNT
, cs_count
, u32
);
560 SET_CSA_SETTING(CSA_CENTER_FREQ1
, freq_params
.center_freq1
, u32
);
561 SET_CSA_SETTING(CSA_CENTER_FREQ2
, freq_params
.center_freq2
, u32
);
562 SET_CSA_SETTING(CSA_BANDWIDTH
, freq_params
.bandwidth
, u32
);
563 SET_CSA_SETTING(CSA_SEC_CHANNEL_OFFSET
, freq_params
.sec_channel_offset
, u32
);
564 SET_CSA_SETTING(CSA_HT
, freq_params
.ht_enabled
, bool);
565 SET_CSA_SETTING(CSA_VHT
, freq_params
.vht_enabled
, bool);
566 SET_CSA_SETTING(CSA_BLOCK_TX
, block_tx
, bool);
569 if (hostapd_switch_channel(hapd
, &css
) != 0)
570 return UBUS_STATUS_NOT_SUPPORTED
;
571 return UBUS_STATUS_OK
;
572 #undef SET_CSA_SETTING
578 __VENDOR_ELEMENTS_MAX
581 static const struct blobmsg_policy ve_policy
[__VENDOR_ELEMENTS_MAX
] = {
582 /* vendor elements are provided as hex-string */
583 [VENDOR_ELEMENTS
] = { "vendor_elements", BLOBMSG_TYPE_STRING
},
587 hostapd_vendor_elements(struct ubus_context
*ctx
, struct ubus_object
*obj
,
588 struct ubus_request_data
*req
, const char *method
,
589 struct blob_attr
*msg
)
591 struct blob_attr
*tb
[__VENDOR_ELEMENTS_MAX
];
592 struct hostapd_data
*hapd
= get_hapd_from_object(obj
);
593 struct hostapd_bss_config
*bss
= hapd
->conf
;
594 struct wpabuf
*elems
;
598 blobmsg_parse(ve_policy
, __VENDOR_ELEMENTS_MAX
, tb
,
599 blob_data(msg
), blob_len(msg
));
601 if (!tb
[VENDOR_ELEMENTS
])
602 return UBUS_STATUS_INVALID_ARGUMENT
;
604 pos
= blobmsg_data(tb
[VENDOR_ELEMENTS
]);
605 len
= os_strlen(pos
);
607 return UBUS_STATUS_INVALID_ARGUMENT
;
611 wpabuf_free(bss
->vendor_elements
);
612 bss
->vendor_elements
= NULL
;
616 elems
= wpabuf_alloc(len
);
620 if (hexstr2bin(pos
, wpabuf_put(elems
, len
), len
)) {
622 return UBUS_STATUS_INVALID_ARGUMENT
;
625 wpabuf_free(bss
->vendor_elements
);
626 bss
->vendor_elements
= elems
;
628 /* update beacons if vendor elements were set successfully */
629 if (ieee802_11_update_beacons(hapd
->iface
) != 0)
630 return UBUS_STATUS_NOT_SUPPORTED
;
631 return UBUS_STATUS_OK
;
635 hostapd_rrm_print_nr(struct hostapd_neighbor_entry
*nr
)
641 blobmsg_printf(&b
, "", MACSTR
, MAC2STR(nr
->bssid
));
643 str
= blobmsg_alloc_string_buffer(&b
, "", nr
->ssid
.ssid_len
+ 1);
644 memcpy(str
, nr
->ssid
.ssid
, nr
->ssid
.ssid_len
);
645 str
[nr
->ssid
.ssid_len
] = 0;
646 blobmsg_add_string_buffer(&b
);
648 len
= wpabuf_len(nr
->nr
);
649 str
= blobmsg_alloc_string_buffer(&b
, "", 2 * len
+ 1);
650 wpa_snprintf_hex(str
, 2 * len
+ 1, wpabuf_head_u8(nr
->nr
), len
);
651 blobmsg_add_string_buffer(&b
);
655 BSS_MGMT_EN_NEIGHBOR
,
658 BSS_MGMT_EN_BSS_TRANSITION
,
664 __hostapd_bss_mgmt_enable_f(struct hostapd_data
*hapd
, int flag
)
666 struct hostapd_bss_config
*bss
= hapd
->conf
;
670 case BSS_MGMT_EN_NEIGHBOR
:
671 if (bss
->radio_measurements
[0] &
672 WLAN_RRM_CAPS_NEIGHBOR_REPORT
)
675 bss
->radio_measurements
[0] |=
676 WLAN_RRM_CAPS_NEIGHBOR_REPORT
;
677 hostapd_neighbor_set_own_report(hapd
);
679 case BSS_MGMT_EN_BEACON
:
680 flags
= WLAN_RRM_CAPS_BEACON_REPORT_PASSIVE
|
681 WLAN_RRM_CAPS_BEACON_REPORT_ACTIVE
|
682 WLAN_RRM_CAPS_BEACON_REPORT_TABLE
;
684 if (bss
->radio_measurements
[0] & flags
== flags
)
687 bss
->radio_measurements
[0] |= (u8
) flags
;
690 case BSS_MGMT_EN_BSS_TRANSITION
:
691 if (bss
->bss_transition
)
694 bss
->bss_transition
= 1;
701 __hostapd_bss_mgmt_enable(struct hostapd_data
*hapd
, uint32_t flags
)
706 for (i
= 0; i
< __BSS_MGMT_EN_MAX
; i
++) {
707 if (!(flags
& (1 << i
)))
710 update
|= __hostapd_bss_mgmt_enable_f(hapd
, i
);
714 ieee802_11_update_beacons(hapd
->iface
);
718 static const struct blobmsg_policy bss_mgmt_enable_policy
[__BSS_MGMT_EN_MAX
] = {
719 [BSS_MGMT_EN_NEIGHBOR
] = { "neighbor_report", BLOBMSG_TYPE_BOOL
},
720 [BSS_MGMT_EN_BEACON
] = { "beacon_report", BLOBMSG_TYPE_BOOL
},
722 [BSS_MGMT_EN_BSS_TRANSITION
] = { "bss_transition", BLOBMSG_TYPE_BOOL
},
727 hostapd_bss_mgmt_enable(struct ubus_context
*ctx
, struct ubus_object
*obj
,
728 struct ubus_request_data
*req
, const char *method
,
729 struct blob_attr
*msg
)
732 struct hostapd_data
*hapd
= get_hapd_from_object(obj
);
733 struct blob_attr
*tb
[__BSS_MGMT_EN_MAX
];
734 struct blob_attr
*cur
;
737 bool neigh
= false, beacon
= false;
739 blobmsg_parse(bss_mgmt_enable_policy
, __BSS_MGMT_EN_MAX
, tb
, blob_data(msg
), blob_len(msg
));
741 for (i
= 0; i
< ARRAY_SIZE(tb
); i
++) {
742 if (!tb
[i
] || !blobmsg_get_bool(tb
[i
]))
748 __hostapd_bss_mgmt_enable(hapd
, flags
);
753 hostapd_rrm_nr_enable(struct hostapd_data
*hapd
)
755 __hostapd_bss_mgmt_enable(hapd
, 1 << BSS_MGMT_EN_NEIGHBOR
);
759 hostapd_rrm_nr_get_own(struct ubus_context
*ctx
, struct ubus_object
*obj
,
760 struct ubus_request_data
*req
, const char *method
,
761 struct blob_attr
*msg
)
763 struct hostapd_data
*hapd
= get_hapd_from_object(obj
);
764 struct hostapd_neighbor_entry
*nr
;
767 hostapd_rrm_nr_enable(hapd
);
769 nr
= hostapd_neighbor_get(hapd
, hapd
->own_addr
, NULL
);
771 return UBUS_STATUS_NOT_FOUND
;
773 blob_buf_init(&b
, 0);
775 c
= blobmsg_open_array(&b
, "value");
776 hostapd_rrm_print_nr(nr
);
777 blobmsg_close_array(&b
, c
);
779 ubus_send_reply(ctx
, req
, b
.head
);
785 hostapd_rrm_nr_list(struct ubus_context
*ctx
, struct ubus_object
*obj
,
786 struct ubus_request_data
*req
, const char *method
,
787 struct blob_attr
*msg
)
789 struct hostapd_data
*hapd
= get_hapd_from_object(obj
);
790 struct hostapd_neighbor_entry
*nr
;
793 hostapd_rrm_nr_enable(hapd
);
794 blob_buf_init(&b
, 0);
796 c
= blobmsg_open_array(&b
, "list");
797 dl_list_for_each(nr
, &hapd
->nr_db
, struct hostapd_neighbor_entry
, list
) {
800 if (!memcmp(nr
->bssid
, hapd
->own_addr
, ETH_ALEN
))
803 cur
= blobmsg_open_array(&b
, NULL
);
804 hostapd_rrm_print_nr(nr
);
805 blobmsg_close_array(&b
, cur
);
807 blobmsg_close_array(&b
, c
);
809 ubus_send_reply(ctx
, req
, b
.head
);
819 static const struct blobmsg_policy nr_set_policy
[__NR_SET_LIST_MAX
] = {
820 [NR_SET_LIST
] = { "list", BLOBMSG_TYPE_ARRAY
},
825 hostapd_rrm_nr_clear(struct hostapd_data
*hapd
)
827 struct hostapd_neighbor_entry
*nr
;
830 dl_list_for_each(nr
, &hapd
->nr_db
, struct hostapd_neighbor_entry
, list
) {
831 if (!memcmp(nr
->bssid
, hapd
->own_addr
, ETH_ALEN
))
834 hostapd_neighbor_remove(hapd
, nr
->bssid
, &nr
->ssid
);
840 hostapd_rrm_nr_set(struct ubus_context
*ctx
, struct ubus_object
*obj
,
841 struct ubus_request_data
*req
, const char *method
,
842 struct blob_attr
*msg
)
844 static const struct blobmsg_policy nr_e_policy
[] = {
845 { .type
= BLOBMSG_TYPE_STRING
},
846 { .type
= BLOBMSG_TYPE_STRING
},
847 { .type
= BLOBMSG_TYPE_STRING
},
849 struct hostapd_data
*hapd
= get_hapd_from_object(obj
);
850 struct blob_attr
*tb_l
[__NR_SET_LIST_MAX
];
851 struct blob_attr
*tb
[ARRAY_SIZE(nr_e_policy
)];
852 struct blob_attr
*cur
;
855 hostapd_rrm_nr_enable(hapd
);
857 blobmsg_parse(nr_set_policy
, __NR_SET_LIST_MAX
, tb_l
, blob_data(msg
), blob_len(msg
));
858 if (!tb_l
[NR_SET_LIST
])
859 return UBUS_STATUS_INVALID_ARGUMENT
;
861 hostapd_rrm_nr_clear(hapd
);
862 blobmsg_for_each_attr(cur
, tb_l
[NR_SET_LIST
], rem
) {
863 struct wpa_ssid_value ssid
;
868 blobmsg_parse_array(nr_e_policy
, ARRAY_SIZE(nr_e_policy
), tb
, blobmsg_data(cur
), blobmsg_data_len(cur
));
869 if (!tb
[0] || !tb
[1] || !tb
[2])
872 s
= blobmsg_get_string(tb
[0]);
873 if (hwaddr_aton(s
, bssid
))
876 s
= blobmsg_get_string(tb
[1]);
877 ssid
.ssid_len
= strlen(s
);
878 if (ssid
.ssid_len
> sizeof(ssid
.ssid
))
881 memcpy(&ssid
, s
, ssid
.ssid_len
);
882 data
= wpabuf_parse_bin(blobmsg_get_string(tb
[2]));
886 hostapd_neighbor_set(hapd
, bssid
, &ssid
, data
, NULL
, NULL
, 0);
891 return UBUS_STATUS_INVALID_ARGUMENT
;
908 static const struct blobmsg_policy beacon_req_policy
[__BEACON_REQ_MAX
] = {
909 [BEACON_REQ_ADDR
] = { "addr", BLOBMSG_TYPE_STRING
},
910 [BEACON_REQ_OP_CLASS
] { "op_class", BLOBMSG_TYPE_INT32
},
911 [BEACON_REQ_CHANNEL
] { "channel", BLOBMSG_TYPE_INT32
},
912 [BEACON_REQ_DURATION
] { "duration", BLOBMSG_TYPE_INT32
},
913 [BEACON_REQ_MODE
] { "mode", BLOBMSG_TYPE_INT32
},
914 [BEACON_REQ_BSSID
] { "bssid", BLOBMSG_TYPE_STRING
},
915 [BEACON_REQ_SSID
] { "ssid", BLOBMSG_TYPE_STRING
},
919 hostapd_rrm_beacon_req(struct ubus_context
*ctx
, struct ubus_object
*obj
,
920 struct ubus_request_data
*ureq
, const char *method
,
921 struct blob_attr
*msg
)
923 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
924 struct blob_attr
*tb
[__BEACON_REQ_MAX
];
925 struct blob_attr
*cur
;
927 u8 bssid
[ETH_ALEN
] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
932 blobmsg_parse(beacon_req_policy
, __BEACON_REQ_MAX
, tb
, blob_data(msg
), blob_len(msg
));
934 if (!tb
[BEACON_REQ_ADDR
] || !tb
[BEACON_REQ_MODE
] || !tb
[BEACON_REQ_DURATION
] ||
935 !tb
[BEACON_REQ_OP_CLASS
] || !tb
[BEACON_REQ_CHANNEL
])
936 return UBUS_STATUS_INVALID_ARGUMENT
;
938 if (tb
[BEACON_REQ_SSID
])
939 buf_len
+= blobmsg_data_len(tb
[BEACON_REQ_SSID
]) + 2 - 1;
941 mode
= blobmsg_get_u32(tb
[BEACON_REQ_MODE
]);
942 if (hwaddr_aton(blobmsg_data(tb
[BEACON_REQ_ADDR
]), addr
))
943 return UBUS_STATUS_INVALID_ARGUMENT
;
945 if (tb
[BEACON_REQ_BSSID
] &&
946 hwaddr_aton(blobmsg_data(tb
[BEACON_REQ_BSSID
]), bssid
))
947 return UBUS_STATUS_INVALID_ARGUMENT
;
949 req
= wpabuf_alloc(buf_len
);
951 return UBUS_STATUS_UNKNOWN_ERROR
;
953 /* 1: regulatory class */
954 wpabuf_put_u8(req
, blobmsg_get_u32(tb
[BEACON_REQ_OP_CLASS
]));
956 /* 2: channel number */
957 wpabuf_put_u8(req
, blobmsg_get_u32(tb
[BEACON_REQ_CHANNEL
]));
959 /* 3-4: randomization interval */
960 wpabuf_put_le16(req
, 0);
963 wpabuf_put_le16(req
, blobmsg_get_u32(tb
[BEACON_REQ_DURATION
]));
966 wpabuf_put_u8(req
, blobmsg_get_u32(tb
[BEACON_REQ_MODE
]));
969 wpabuf_put_data(req
, bssid
, ETH_ALEN
);
971 if ((cur
= tb
[BEACON_REQ_SSID
]) != NULL
) {
972 wpabuf_put_u8(req
, WLAN_EID_SSID
);
973 wpabuf_put_u8(req
, blobmsg_data_len(cur
) - 1);
974 wpabuf_put_data(req
, blobmsg_data(cur
), blobmsg_data_len(cur
) - 1);
977 ret
= hostapd_send_beacon_req(hapd
, addr
, 0, req
);
988 WNM_DISASSOC_DURATION
,
989 WNM_DISASSOC_NEIGHBORS
,
990 WNM_DISASSOC_ABRIDGED
,
994 static const struct blobmsg_policy wnm_disassoc_policy
[__WNM_DISASSOC_MAX
] = {
995 [WNM_DISASSOC_ADDR
] = { "addr", BLOBMSG_TYPE_STRING
},
996 [WNM_DISASSOC_DURATION
] { "duration", BLOBMSG_TYPE_INT32
},
997 [WNM_DISASSOC_NEIGHBORS
] { "neighbors", BLOBMSG_TYPE_ARRAY
},
998 [WNM_DISASSOC_ABRIDGED
] { "abridged", BLOBMSG_TYPE_BOOL
},
1002 hostapd_wnm_disassoc_imminent(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1003 struct ubus_request_data
*ureq
, const char *method
,
1004 struct blob_attr
*msg
)
1006 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
1007 struct blob_attr
*tb
[__WNM_DISASSOC_MAX
];
1008 struct blob_attr
*cur
;
1009 struct sta_info
*sta
;
1014 u8 req_mode
= WNM_BSS_TM_REQ_DISASSOC_IMMINENT
;
1017 blobmsg_parse(wnm_disassoc_policy
, __WNM_DISASSOC_MAX
, tb
, blob_data(msg
), blob_len(msg
));
1019 if (!tb
[WNM_DISASSOC_ADDR
])
1020 return UBUS_STATUS_INVALID_ARGUMENT
;
1022 if (hwaddr_aton(blobmsg_data(tb
[WNM_DISASSOC_ADDR
]), addr
))
1023 return UBUS_STATUS_INVALID_ARGUMENT
;
1025 if ((cur
= tb
[WNM_DISASSOC_DURATION
]) != NULL
)
1026 duration
= blobmsg_get_u32(cur
);
1028 sta
= ap_get_sta(hapd
, addr
);
1030 return UBUS_STATUS_NOT_FOUND
;
1032 if (tb
[WNM_DISASSOC_NEIGHBORS
]) {
1035 if (blobmsg_check_array(tb
[WNM_DISASSOC_NEIGHBORS
],
1036 BLOBMSG_TYPE_STRING
) < 0)
1037 return UBUS_STATUS_INVALID_ARGUMENT
;
1039 blobmsg_for_each_attr(cur
, tb
[WNM_DISASSOC_NEIGHBORS
], rem
) {
1040 int len
= strlen(blobmsg_get_string(cur
));
1043 return UBUS_STATUS_INVALID_ARGUMENT
;
1045 nr_len
+= (len
/ 2) + 2;
1049 nr
= os_zalloc(nr_len
);
1051 return UBUS_STATUS_UNKNOWN_ERROR
;
1055 blobmsg_for_each_attr(cur
, tb
[WNM_DISASSOC_NEIGHBORS
], rem
) {
1056 int len
= strlen(blobmsg_get_string(cur
)) / 2;
1058 *nr_cur
++ = WLAN_EID_NEIGHBOR_REPORT
;
1059 *nr_cur
++ = (u8
) len
;
1060 if (hexstr2bin(blobmsg_data(cur
), nr_cur
, len
)) {
1062 return UBUS_STATUS_INVALID_ARGUMENT
;
1070 req_mode
|= WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED
;
1072 if (tb
[WNM_DISASSOC_ABRIDGED
] && blobmsg_get_bool(tb
[WNM_DISASSOC_ABRIDGED
]))
1073 req_mode
|= WNM_BSS_TM_REQ_ABRIDGED
;
1075 if (wnm_send_bss_tm_req(hapd
, sta
, req_mode
, duration
, 0, NULL
,
1076 NULL
, nr
, nr_len
, NULL
, 0))
1077 return UBUS_STATUS_UNKNOWN_ERROR
;
1083 static const struct ubus_method bss_methods
[] = {
1084 UBUS_METHOD_NOARG("reload", hostapd_bss_reload
),
1085 UBUS_METHOD_NOARG("get_clients", hostapd_bss_get_clients
),
1086 UBUS_METHOD("del_client", hostapd_bss_del_client
, del_policy
),
1087 UBUS_METHOD_NOARG("list_bans", hostapd_bss_list_bans
),
1088 UBUS_METHOD_NOARG("wps_start", hostapd_bss_wps_start
),
1089 UBUS_METHOD_NOARG("wps_cancel", hostapd_bss_wps_cancel
),
1090 UBUS_METHOD_NOARG("update_beacon", hostapd_bss_update_beacon
),
1091 UBUS_METHOD_NOARG("get_features", hostapd_bss_get_features
),
1093 UBUS_METHOD("switch_chan", hostapd_switch_chan
, csa_policy
),
1095 UBUS_METHOD("set_vendor_elements", hostapd_vendor_elements
, ve_policy
),
1096 UBUS_METHOD("notify_response", hostapd_notify_response
, notify_policy
),
1097 UBUS_METHOD("bss_mgmt_enable", hostapd_bss_mgmt_enable
, bss_mgmt_enable_policy
),
1098 UBUS_METHOD_NOARG("rrm_nr_get_own", hostapd_rrm_nr_get_own
),
1099 UBUS_METHOD_NOARG("rrm_nr_list", hostapd_rrm_nr_list
),
1100 UBUS_METHOD("rrm_nr_set", hostapd_rrm_nr_set
, nr_set_policy
),
1101 UBUS_METHOD("rrm_beacon_req", hostapd_rrm_beacon_req
, beacon_req_policy
),
1102 #ifdef CONFIG_WNM_AP
1103 UBUS_METHOD("wnm_disassoc_imminent", hostapd_wnm_disassoc_imminent
, wnm_disassoc_policy
),
1107 static struct ubus_object_type bss_object_type
=
1108 UBUS_OBJECT_TYPE("hostapd_bss", bss_methods
);
1110 static int avl_compare_macaddr(const void *k1
, const void *k2
, void *ptr
)
1112 return memcmp(k1
, k2
, ETH_ALEN
);
1115 void hostapd_ubus_add_bss(struct hostapd_data
*hapd
)
1117 struct ubus_object
*obj
= &hapd
->ubus
.obj
;
1122 if (hapd
->conf
->mesh
& MESH_ENABLED
)
1126 if (!hostapd_ubus_init())
1129 if (asprintf(&name
, "hostapd.%s", hapd
->conf
->iface
) < 0)
1132 avl_init(&hapd
->ubus
.banned
, avl_compare_macaddr
, false, NULL
);
1134 obj
->type
= &bss_object_type
;
1135 obj
->methods
= bss_object_type
.methods
;
1136 obj
->n_methods
= bss_object_type
.n_methods
;
1137 ret
= ubus_add_object(ctx
, obj
);
1138 hostapd_ubus_ref_inc();
1140 hostapd_send_shared_event(&hapd
->iface
->interfaces
->ubus
, hapd
->conf
->iface
, "add");
1143 void hostapd_ubus_free_bss(struct hostapd_data
*hapd
)
1145 struct ubus_object
*obj
= &hapd
->ubus
.obj
;
1146 char *name
= (char *) obj
->name
;
1151 hostapd_send_shared_event(&hapd
->iface
->interfaces
->ubus
, hapd
->conf
->iface
, "remove");
1154 ubus_remove_object(ctx
, obj
);
1155 hostapd_ubus_ref_dec();
1161 static const struct ubus_method daemon_methods
[] = {
1162 UBUS_METHOD("config_add", hostapd_config_add
, config_add_policy
),
1163 UBUS_METHOD("config_remove", hostapd_config_remove
, config_remove_policy
),
1166 static struct ubus_object_type daemon_object_type
=
1167 UBUS_OBJECT_TYPE("hostapd", daemon_methods
);
1169 void hostapd_ubus_add(struct hapd_interfaces
*interfaces
)
1171 struct ubus_object
*obj
= &interfaces
->ubus
;
1174 if (!hostapd_ubus_init())
1177 obj
->name
= strdup("hostapd");
1179 obj
->type
= &daemon_object_type
;
1180 obj
->methods
= daemon_object_type
.methods
;
1181 obj
->n_methods
= daemon_object_type
.n_methods
;
1182 ret
= ubus_add_object(ctx
, obj
);
1183 hostapd_ubus_ref_inc();
1186 void hostapd_ubus_free(struct hapd_interfaces
*interfaces
)
1188 struct ubus_object
*obj
= &interfaces
->ubus
;
1189 char *name
= (char *) obj
->name
;
1195 ubus_remove_object(ctx
, obj
);
1196 hostapd_ubus_ref_dec();
1202 struct ubus_event_req
{
1203 struct ubus_notify_request nreq
;
1208 ubus_event_cb(struct ubus_notify_request
*req
, int idx
, int ret
)
1210 struct ubus_event_req
*ureq
= container_of(req
, struct ubus_event_req
, nreq
);
1215 int hostapd_ubus_handle_event(struct hostapd_data
*hapd
, struct hostapd_ubus_request
*req
)
1217 struct ubus_banned_client
*ban
;
1218 const char *types
[HOSTAPD_UBUS_TYPE_MAX
] = {
1219 [HOSTAPD_UBUS_PROBE_REQ
] = "probe",
1220 [HOSTAPD_UBUS_AUTH_REQ
] = "auth",
1221 [HOSTAPD_UBUS_ASSOC_REQ
] = "assoc",
1223 const char *type
= "mgmt";
1224 struct ubus_event_req ureq
= {};
1227 if (req
->mgmt_frame
)
1228 addr
= req
->mgmt_frame
->sa
;
1232 ban
= avl_find_element(&hapd
->ubus
.banned
, addr
, ban
, avl
);
1234 return WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA
;
1236 if (!hapd
->ubus
.obj
.has_subscribers
)
1237 return WLAN_STATUS_SUCCESS
;
1239 if (req
->type
< ARRAY_SIZE(types
))
1240 type
= types
[req
->type
];
1242 blob_buf_init(&b
, 0);
1243 blobmsg_add_macaddr(&b
, "address", addr
);
1244 if (req
->mgmt_frame
)
1245 blobmsg_add_macaddr(&b
, "target", req
->mgmt_frame
->da
);
1246 if (req
->ssi_signal
)
1247 blobmsg_add_u32(&b
, "signal", req
->ssi_signal
);
1248 blobmsg_add_u32(&b
, "freq", hapd
->iface
->freq
);
1251 if(req
->elems
->ht_capabilities
)
1253 struct ieee80211_ht_capabilities
*ht_capabilities
;
1254 void *ht_cap
, *ht_cap_mcs_set
, *mcs_set
;
1257 ht_capabilities
= (struct ieee80211_ht_capabilities
*) req
->elems
->ht_capabilities
;
1258 ht_cap
= blobmsg_open_table(&b
, "ht_capabilities");
1259 blobmsg_add_u16(&b
, "ht_capabilities_info", ht_capabilities
->ht_capabilities_info
);
1260 ht_cap_mcs_set
= blobmsg_open_table(&b
, "supported_mcs_set");
1261 blobmsg_add_u16(&b
, "a_mpdu_params", ht_capabilities
->a_mpdu_params
);
1262 blobmsg_add_u16(&b
, "ht_extended_capabilities", ht_capabilities
->ht_extended_capabilities
);
1263 blobmsg_add_u32(&b
, "tx_bf_capability_info", ht_capabilities
->tx_bf_capability_info
);
1264 blobmsg_add_u16(&b
, "asel_capabilities", ht_capabilities
->asel_capabilities
);
1265 mcs_set
= blobmsg_open_array(&b
, "supported_mcs_set");
1266 for (int i
= 0; i
< 16; i
++) {
1267 blobmsg_add_u16(&b
, NULL
, (u16
) ht_capabilities
->supported_mcs_set
[i
]);
1269 blobmsg_close_array(&b
, mcs_set
);
1270 blobmsg_close_table(&b
, ht_cap_mcs_set
);
1271 blobmsg_close_table(&b
, ht_cap
);
1273 if(req
->elems
->vht_capabilities
)
1275 struct ieee80211_vht_capabilities
*vht_capabilities
;
1276 void *vht_cap
, *vht_cap_mcs_set
;
1278 vht_capabilities
= (struct ieee80211_vht_capabilities
*) req
->elems
->vht_capabilities
;
1279 vht_cap
= blobmsg_open_table(&b
, "vht_capabilities");
1280 blobmsg_add_u32(&b
, "vht_capabilities_info", vht_capabilities
->vht_capabilities_info
);
1281 vht_cap_mcs_set
= blobmsg_open_table(&b
, "vht_supported_mcs_set");
1282 blobmsg_add_u16(&b
, "rx_map", vht_capabilities
->vht_supported_mcs_set
.rx_map
);
1283 blobmsg_add_u16(&b
, "rx_highest", vht_capabilities
->vht_supported_mcs_set
.rx_highest
);
1284 blobmsg_add_u16(&b
, "tx_map", vht_capabilities
->vht_supported_mcs_set
.tx_map
);
1285 blobmsg_add_u16(&b
, "tx_highest", vht_capabilities
->vht_supported_mcs_set
.tx_highest
);
1286 blobmsg_close_table(&b
, vht_cap_mcs_set
);
1287 blobmsg_close_table(&b
, vht_cap
);
1291 if (!hapd
->ubus
.notify_response
) {
1292 ubus_notify(ctx
, &hapd
->ubus
.obj
, type
, b
.head
, -1);
1293 return WLAN_STATUS_SUCCESS
;
1296 if (ubus_notify_async(ctx
, &hapd
->ubus
.obj
, type
, b
.head
, &ureq
.nreq
))
1297 return WLAN_STATUS_SUCCESS
;
1299 ureq
.nreq
.status_cb
= ubus_event_cb
;
1300 ubus_complete_request(ctx
, &ureq
.nreq
.req
, 100);
1305 return WLAN_STATUS_SUCCESS
;
1308 void hostapd_ubus_notify(struct hostapd_data
*hapd
, const char *type
, const u8
*addr
)
1310 if (!hapd
->ubus
.obj
.has_subscribers
)
1316 blob_buf_init(&b
, 0);
1317 blobmsg_add_macaddr(&b
, "address", addr
);
1319 ubus_notify(ctx
, &hapd
->ubus
.obj
, type
, b
.head
, -1);
1322 void hostapd_ubus_notify_beacon_report(
1323 struct hostapd_data
*hapd
, const u8
*addr
, u8 token
, u8 rep_mode
,
1324 struct rrm_measurement_beacon_report
*rep
, size_t len
)
1326 if (!hapd
->ubus
.obj
.has_subscribers
)
1332 blob_buf_init(&b
, 0);
1333 blobmsg_add_macaddr(&b
, "address", addr
);
1334 blobmsg_add_u16(&b
, "op-class", rep
->op_class
);
1335 blobmsg_add_u16(&b
, "channel", rep
->channel
);
1336 blobmsg_add_u64(&b
, "start-time", rep
->start_time
);
1337 blobmsg_add_u16(&b
, "duration", rep
->duration
);
1338 blobmsg_add_u16(&b
, "report-info", rep
->report_info
);
1339 blobmsg_add_u16(&b
, "rcpi", rep
->rcpi
);
1340 blobmsg_add_u16(&b
, "rsni", rep
->rsni
);
1341 blobmsg_add_macaddr(&b
, "bssid", rep
->bssid
);
1342 blobmsg_add_u16(&b
, "antenna-id", rep
->antenna_id
);
1343 blobmsg_add_u16(&b
, "parent-tsf", rep
->parent_tsf
);
1345 ubus_notify(ctx
, &hapd
->ubus
.obj
, "beacon-report", b
.head
, -1);