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 "common/ieee802_11_defs.h"
14 #include "wps_hostapd.h"
17 #include "ap_drv_ops.h"
20 static struct ubus_context
*ctx
;
21 static struct blob_buf b
;
24 static inline struct hostapd_data
*get_hapd_from_object(struct ubus_object
*obj
)
26 return container_of(obj
, struct hostapd_data
, ubus
.obj
);
30 struct ubus_banned_client
{
35 static void ubus_receive(int sock
, void *eloop_ctx
, void *sock_ctx
)
37 struct ubus_context
*ctx
= eloop_ctx
;
38 ubus_handle_event(ctx
);
41 static void ubus_reconnect_timeout(void *eloop_data
, void *user_ctx
)
43 if (ubus_reconnect(ctx
, NULL
)) {
44 eloop_register_timeout(1, 0, ubus_reconnect_timeout
, ctx
, NULL
);
48 eloop_register_read_sock(ctx
->sock
.fd
, ubus_receive
, ctx
, NULL
);
51 static void hostapd_ubus_connection_lost(struct ubus_context
*ctx
)
53 eloop_unregister_read_sock(ctx
->sock
.fd
);
54 eloop_register_timeout(1, 0, ubus_reconnect_timeout
, ctx
, NULL
);
57 static bool hostapd_ubus_init(void)
62 ctx
= ubus_connect(NULL
);
66 ctx
->connection_lost
= hostapd_ubus_connection_lost
;
67 eloop_register_read_sock(ctx
->sock
.fd
, ubus_receive
, ctx
, NULL
);
71 static void hostapd_ubus_ref_inc(void)
76 static void hostapd_ubus_ref_dec(void)
85 eloop_unregister_read_sock(ctx
->sock
.fd
);
90 void hostapd_ubus_add_iface(struct hostapd_iface
*iface
)
92 if (!hostapd_ubus_init())
96 void hostapd_ubus_free_iface(struct hostapd_iface
*iface
)
103 hostapd_bss_del_ban(void *eloop_data
, void *user_ctx
)
105 struct ubus_banned_client
*ban
= eloop_data
;
106 struct hostapd_data
*hapd
= user_ctx
;
108 avl_delete(&hapd
->ubus
.banned
, &ban
->avl
);
113 hostapd_bss_ban_client(struct hostapd_data
*hapd
, u8
*addr
, int time
)
115 struct ubus_banned_client
*ban
;
120 ban
= avl_find_element(&hapd
->ubus
.banned
, addr
, ban
, avl
);
125 ban
= os_zalloc(sizeof(*ban
));
126 memcpy(ban
->addr
, addr
, sizeof(ban
->addr
));
127 ban
->avl
.key
= ban
->addr
;
128 avl_insert(&hapd
->ubus
.banned
, &ban
->avl
);
130 eloop_cancel_timeout(hostapd_bss_del_ban
, ban
, hapd
);
132 hostapd_bss_del_ban(ban
, hapd
);
137 eloop_register_timeout(0, time
* 1000, hostapd_bss_del_ban
, ban
, hapd
);
141 hostapd_bss_get_clients(struct ubus_context
*ctx
, struct ubus_object
*obj
,
142 struct ubus_request_data
*req
, const char *method
,
143 struct blob_attr
*msg
)
145 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
146 struct sta_info
*sta
;
149 static const struct {
153 { "auth", WLAN_STA_AUTH
},
154 { "assoc", WLAN_STA_ASSOC
},
155 { "authorized", WLAN_STA_AUTHORIZED
},
156 { "preauth", WLAN_STA_PREAUTH
},
157 { "wds", WLAN_STA_WDS
},
158 { "wmm", WLAN_STA_WMM
},
159 { "ht", WLAN_STA_HT
},
160 { "vht", WLAN_STA_VHT
},
161 { "wps", WLAN_STA_WPS
},
162 { "mfp", WLAN_STA_MFP
},
165 blob_buf_init(&b
, 0);
166 blobmsg_add_u32(&b
, "freq", hapd
->iface
->freq
);
167 list
= blobmsg_open_table(&b
, "clients");
168 for (sta
= hapd
->sta_list
; sta
; sta
= sta
->next
) {
171 sprintf(mac_buf
, MACSTR
, MAC2STR(sta
->addr
));
172 c
= blobmsg_open_table(&b
, mac_buf
);
173 for (i
= 0; i
< ARRAY_SIZE(sta_flags
); i
++)
174 blobmsg_add_u8(&b
, sta_flags
[i
].name
,
175 !!(sta
->flags
& sta_flags
[i
].flag
));
176 blobmsg_add_u32(&b
, "aid", sta
->aid
);
177 blobmsg_close_table(&b
, c
);
179 blobmsg_close_array(&b
, list
);
180 ubus_send_reply(ctx
, req
, b
.head
);
193 static const struct blobmsg_policy del_policy
[__DEL_CLIENT_MAX
] = {
194 [DEL_CLIENT_ADDR
] = { "addr", BLOBMSG_TYPE_STRING
},
195 [DEL_CLIENT_REASON
] = { "reason", BLOBMSG_TYPE_INT32
},
196 [DEL_CLIENT_DEAUTH
] = { "deauth", BLOBMSG_TYPE_INT8
},
197 [DEL_CLIENT_BAN_TIME
] = { "ban_time", BLOBMSG_TYPE_INT32
},
201 hostapd_bss_del_client(struct ubus_context
*ctx
, struct ubus_object
*obj
,
202 struct ubus_request_data
*req
, const char *method
,
203 struct blob_attr
*msg
)
205 struct blob_attr
*tb
[__DEL_CLIENT_MAX
];
206 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
207 struct sta_info
*sta
;
212 blobmsg_parse(del_policy
, __DEL_CLIENT_MAX
, tb
, blob_data(msg
), blob_len(msg
));
214 if (!tb
[DEL_CLIENT_ADDR
])
215 return UBUS_STATUS_INVALID_ARGUMENT
;
217 if (hwaddr_aton(blobmsg_data(tb
[DEL_CLIENT_ADDR
]), addr
))
218 return UBUS_STATUS_INVALID_ARGUMENT
;
220 if (tb
[DEL_CLIENT_REASON
])
221 reason
= blobmsg_get_u32(tb
[DEL_CLIENT_REASON
]);
223 if (tb
[DEL_CLIENT_DEAUTH
])
224 deauth
= blobmsg_get_bool(tb
[DEL_CLIENT_DEAUTH
]);
226 sta
= ap_get_sta(hapd
, addr
);
229 hostapd_drv_sta_deauth(hapd
, addr
, reason
);
230 ap_sta_deauthenticate(hapd
, sta
, reason
);
232 hostapd_drv_sta_disassoc(hapd
, addr
, reason
);
233 ap_sta_disassociate(hapd
, sta
, reason
);
237 if (tb
[DEL_CLIENT_BAN_TIME
])
238 hostapd_bss_ban_client(hapd
, addr
, blobmsg_get_u32(tb
[DEL_CLIENT_BAN_TIME
]));
244 blobmsg_add_macaddr(struct blob_buf
*buf
, const char *name
, const u8
*addr
)
248 s
= blobmsg_alloc_string_buffer(buf
, name
, 20);
249 sprintf(s
, MACSTR
, MAC2STR(addr
));
250 blobmsg_add_string_buffer(buf
);
254 hostapd_bss_list_bans(struct ubus_context
*ctx
, struct ubus_object
*obj
,
255 struct ubus_request_data
*req
, const char *method
,
256 struct blob_attr
*msg
)
258 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
259 struct ubus_banned_client
*ban
;
262 blob_buf_init(&b
, 0);
263 c
= blobmsg_open_array(&b
, "clients");
264 avl_for_each_element(&hapd
->ubus
.banned
, ban
, avl
)
265 blobmsg_add_macaddr(&b
, NULL
, ban
->addr
);
266 blobmsg_close_array(&b
, c
);
267 ubus_send_reply(ctx
, req
, b
.head
);
273 hostapd_bss_wps_start(struct ubus_context
*ctx
, struct ubus_object
*obj
,
274 struct ubus_request_data
*req
, const char *method
,
275 struct blob_attr
*msg
)
278 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
280 rc
= hostapd_wps_button_pushed(hapd
, NULL
);
283 return UBUS_STATUS_NOT_SUPPORTED
;
289 hostapd_bss_wps_cancel(struct ubus_context
*ctx
, struct ubus_object
*obj
,
290 struct ubus_request_data
*req
, const char *method
,
291 struct blob_attr
*msg
)
294 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
296 rc
= hostapd_wps_cancel(hapd
);
299 return UBUS_STATUS_NOT_SUPPORTED
;
305 hostapd_bss_update_beacon(struct ubus_context
*ctx
, struct ubus_object
*obj
,
306 struct ubus_request_data
*req
, const char *method
,
307 struct blob_attr
*msg
)
310 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
312 rc
= ieee802_11_set_beacon(hapd
);
315 return UBUS_STATUS_NOT_SUPPORTED
;
326 static const struct blobmsg_policy csa_policy
[__CSA_MAX
] = {
328 * for now, frequency and beacon count are enough, add more
329 * parameters on demand
331 [CSA_FREQ
] = { "freq", BLOBMSG_TYPE_INT32
},
332 [CSA_BCN_COUNT
] = { "bcn_count", BLOBMSG_TYPE_INT32
},
337 hostapd_switch_chan(struct ubus_context
*ctx
, struct ubus_object
*obj
,
338 struct ubus_request_data
*req
, const char *method
,
339 struct blob_attr
*msg
)
341 struct blob_attr
*tb
[__CSA_MAX
];
342 struct hostapd_data
*hapd
= get_hapd_from_object(obj
);
343 struct csa_settings css
;
345 blobmsg_parse(csa_policy
, __CSA_MAX
, tb
, blob_data(msg
), blob_len(msg
));
348 return UBUS_STATUS_INVALID_ARGUMENT
;
350 memset(&css
, 0, sizeof(css
));
351 css
.freq_params
.freq
= blobmsg_get_u32(tb
[CSA_FREQ
]);
352 if (tb
[CSA_BCN_COUNT
])
353 css
.cs_count
= blobmsg_get_u32(tb
[CSA_BCN_COUNT
]);
355 if (hostapd_switch_channel(hapd
, &css
) != 0)
356 return UBUS_STATUS_NOT_SUPPORTED
;
357 return UBUS_STATUS_OK
;
363 __VENDOR_ELEMENTS_MAX
366 static const struct blobmsg_policy ve_policy
[__VENDOR_ELEMENTS_MAX
] = {
367 /* vendor elements are provided as hex-string */
368 [VENDOR_ELEMENTS
] = { "vendor_elements", BLOBMSG_TYPE_STRING
},
372 hostapd_vendor_elements(struct ubus_context
*ctx
, struct ubus_object
*obj
,
373 struct ubus_request_data
*req
, const char *method
,
374 struct blob_attr
*msg
)
376 struct blob_attr
*tb
[__VENDOR_ELEMENTS_MAX
];
377 struct hostapd_data
*hapd
= get_hapd_from_object(obj
);
378 struct hostapd_bss_config
*bss
= hapd
->conf
;
379 struct wpabuf
*elems
;
383 blobmsg_parse(ve_policy
, __VENDOR_ELEMENTS_MAX
, tb
,
384 blob_data(msg
), blob_len(msg
));
386 if (!tb
[VENDOR_ELEMENTS
])
387 return UBUS_STATUS_INVALID_ARGUMENT
;
389 pos
= blobmsg_data(tb
[VENDOR_ELEMENTS
]);
390 len
= os_strlen(pos
);
392 return UBUS_STATUS_INVALID_ARGUMENT
;
396 wpabuf_free(bss
->vendor_elements
);
397 bss
->vendor_elements
= NULL
;
401 elems
= wpabuf_alloc(len
);
405 if (hexstr2bin(pos
, wpabuf_put(elems
, len
), len
)) {
407 return UBUS_STATUS_INVALID_ARGUMENT
;
410 wpabuf_free(bss
->vendor_elements
);
411 bss
->vendor_elements
= elems
;
413 /* update beacons if vendor elements were set successfully */
414 if (ieee802_11_update_beacons(hapd
->iface
) != 0)
415 return UBUS_STATUS_NOT_SUPPORTED
;
416 return UBUS_STATUS_OK
;
419 static const struct ubus_method bss_methods
[] = {
420 UBUS_METHOD_NOARG("get_clients", hostapd_bss_get_clients
),
421 UBUS_METHOD("del_client", hostapd_bss_del_client
, del_policy
),
422 UBUS_METHOD_NOARG("list_bans", hostapd_bss_list_bans
),
423 UBUS_METHOD_NOARG("wps_start", hostapd_bss_wps_start
),
424 UBUS_METHOD_NOARG("wps_cancel", hostapd_bss_wps_cancel
),
425 UBUS_METHOD_NOARG("update_beacon", hostapd_bss_update_beacon
),
427 UBUS_METHOD("switch_chan", hostapd_switch_chan
, csa_policy
),
429 UBUS_METHOD("set_vendor_elements", hostapd_vendor_elements
, ve_policy
),
432 static struct ubus_object_type bss_object_type
=
433 UBUS_OBJECT_TYPE("hostapd_bss", bss_methods
);
435 static int avl_compare_macaddr(const void *k1
, const void *k2
, void *ptr
)
437 return memcmp(k1
, k2
, ETH_ALEN
);
440 void hostapd_ubus_add_bss(struct hostapd_data
*hapd
)
442 struct ubus_object
*obj
= &hapd
->ubus
.obj
;
446 if (!hostapd_ubus_init())
449 if (asprintf(&name
, "hostapd.%s", hapd
->conf
->iface
) < 0)
452 avl_init(&hapd
->ubus
.banned
, avl_compare_macaddr
, false, NULL
);
454 obj
->type
= &bss_object_type
;
455 obj
->methods
= bss_object_type
.methods
;
456 obj
->n_methods
= bss_object_type
.n_methods
;
457 ret
= ubus_add_object(ctx
, obj
);
458 hostapd_ubus_ref_inc();
461 void hostapd_ubus_free_bss(struct hostapd_data
*hapd
)
463 struct ubus_object
*obj
= &hapd
->ubus
.obj
;
464 char *name
= (char *) obj
->name
;
470 ubus_remove_object(ctx
, obj
);
471 hostapd_ubus_ref_dec();
477 struct ubus_event_req
{
478 struct ubus_notify_request nreq
;
483 ubus_event_cb(struct ubus_notify_request
*req
, int idx
, int ret
)
485 struct ubus_event_req
*ureq
= container_of(req
, struct ubus_event_req
, nreq
);
491 int hostapd_ubus_handle_event(struct hostapd_data
*hapd
, struct hostapd_ubus_request
*req
)
493 struct ubus_banned_client
*ban
;
494 const char *types
[HOSTAPD_UBUS_TYPE_MAX
] = {
495 [HOSTAPD_UBUS_PROBE_REQ
] = "probe",
496 [HOSTAPD_UBUS_AUTH_REQ
] = "auth",
497 [HOSTAPD_UBUS_ASSOC_REQ
] = "assoc",
499 const char *type
= "mgmt";
500 struct ubus_event_req ureq
= {};
504 addr
= req
->mgmt_frame
->sa
;
508 ban
= avl_find_element(&hapd
->ubus
.banned
, addr
, ban
, avl
);
512 if (!hapd
->ubus
.obj
.has_subscribers
)
515 if (req
->type
< ARRAY_SIZE(types
))
516 type
= types
[req
->type
];
518 blob_buf_init(&b
, 0);
519 blobmsg_add_macaddr(&b
, "address", addr
);
521 blobmsg_add_macaddr(&b
, "target", req
->mgmt_frame
->da
);
523 blobmsg_add_u32(&b
, "signal", req
->frame_info
->ssi_signal
);
524 blobmsg_add_u32(&b
, "freq", hapd
->iface
->freq
);
526 if (ubus_notify_async(ctx
, &hapd
->ubus
.obj
, type
, b
.head
, &ureq
.nreq
))
529 ureq
.nreq
.status_cb
= ubus_event_cb
;
530 ubus_complete_request(ctx
, &ureq
.nreq
.req
, 100);