600-ubus_support.patch 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. --- a/hostapd/Makefile
  2. +++ b/hostapd/Makefile
  3. @@ -157,6 +157,11 @@ OBJS += ../src/common/hw_features_common
  4. OBJS += ../src/eapol_auth/eapol_auth_sm.o
  5. +ifdef CONFIG_UBUS
  6. +CFLAGS += -DUBUS_SUPPORT
  7. +OBJS += ../src/ap/ubus.o
  8. +LIBS += -lubox -lubus
  9. +endif
  10. ifdef CONFIG_CODE_COVERAGE
  11. CFLAGS += -O0 -fprofile-arcs -ftest-coverage
  12. --- a/src/ap/hostapd.h
  13. +++ b/src/ap/hostapd.h
  14. @@ -13,6 +13,7 @@
  15. #include "utils/list.h"
  16. #include "ap_config.h"
  17. #include "drivers/driver.h"
  18. +#include "ubus.h"
  19. struct wpa_ctrl_dst;
  20. struct radius_server_data;
  21. @@ -118,6 +119,7 @@ struct hostapd_data {
  22. struct hostapd_iface *iface;
  23. struct hostapd_config *iconf;
  24. struct hostapd_bss_config *conf;
  25. + struct hostapd_ubus_bss ubus;
  26. int interface_added; /* virtual interface added for this BSS */
  27. unsigned int started:1;
  28. unsigned int disabled:1;
  29. @@ -323,6 +325,8 @@ struct hostapd_iface {
  30. struct hostapd_config *conf;
  31. char phy[16]; /* Name of the PHY (radio) */
  32. + struct hostapd_ubus_iface ubus;
  33. +
  34. enum hostapd_iface_state {
  35. HAPD_IFACE_UNINITIALIZED,
  36. HAPD_IFACE_DISABLED,
  37. --- /dev/null
  38. +++ b/src/ap/ubus.c
  39. @@ -0,0 +1,536 @@
  40. +/*
  41. + * hostapd / ubus support
  42. + * Copyright (c) 2013, Felix Fietkau <nbd@nbd.name>
  43. + *
  44. + * This software may be distributed under the terms of the BSD license.
  45. + * See README for more details.
  46. + */
  47. +
  48. +#include "utils/includes.h"
  49. +#include "utils/common.h"
  50. +#include "utils/eloop.h"
  51. +#include "common/ieee802_11_defs.h"
  52. +#include "hostapd.h"
  53. +#include "wps_hostapd.h"
  54. +#include "sta_info.h"
  55. +#include "ubus.h"
  56. +#include "ap_drv_ops.h"
  57. +#include "beacon.h"
  58. +
  59. +static struct ubus_context *ctx;
  60. +static struct blob_buf b;
  61. +static int ctx_ref;
  62. +
  63. +static inline struct hostapd_data *get_hapd_from_object(struct ubus_object *obj)
  64. +{
  65. + return container_of(obj, struct hostapd_data, ubus.obj);
  66. +}
  67. +
  68. +
  69. +struct ubus_banned_client {
  70. + struct avl_node avl;
  71. + u8 addr[ETH_ALEN];
  72. +};
  73. +
  74. +static void ubus_receive(int sock, void *eloop_ctx, void *sock_ctx)
  75. +{
  76. + struct ubus_context *ctx = eloop_ctx;
  77. + ubus_handle_event(ctx);
  78. +}
  79. +
  80. +static void ubus_reconnect_timeout(void *eloop_data, void *user_ctx)
  81. +{
  82. + if (ubus_reconnect(ctx, NULL)) {
  83. + eloop_register_timeout(1, 0, ubus_reconnect_timeout, ctx, NULL);
  84. + return;
  85. + }
  86. +
  87. + eloop_register_read_sock(ctx->sock.fd, ubus_receive, ctx, NULL);
  88. +}
  89. +
  90. +static void hostapd_ubus_connection_lost(struct ubus_context *ctx)
  91. +{
  92. + eloop_unregister_read_sock(ctx->sock.fd);
  93. + eloop_register_timeout(1, 0, ubus_reconnect_timeout, ctx, NULL);
  94. +}
  95. +
  96. +static bool hostapd_ubus_init(void)
  97. +{
  98. + if (ctx)
  99. + return true;
  100. +
  101. + ctx = ubus_connect(NULL);
  102. + if (!ctx)
  103. + return false;
  104. +
  105. + ctx->connection_lost = hostapd_ubus_connection_lost;
  106. + eloop_register_read_sock(ctx->sock.fd, ubus_receive, ctx, NULL);
  107. + return true;
  108. +}
  109. +
  110. +static void hostapd_ubus_ref_inc(void)
  111. +{
  112. + ctx_ref++;
  113. +}
  114. +
  115. +static void hostapd_ubus_ref_dec(void)
  116. +{
  117. + ctx_ref--;
  118. + if (!ctx)
  119. + return;
  120. +
  121. + if (ctx_ref)
  122. + return;
  123. +
  124. + eloop_unregister_read_sock(ctx->sock.fd);
  125. + ubus_free(ctx);
  126. + ctx = NULL;
  127. +}
  128. +
  129. +void hostapd_ubus_add_iface(struct hostapd_iface *iface)
  130. +{
  131. + if (!hostapd_ubus_init())
  132. + return;
  133. +}
  134. +
  135. +void hostapd_ubus_free_iface(struct hostapd_iface *iface)
  136. +{
  137. + if (!ctx)
  138. + return;
  139. +}
  140. +
  141. +static void
  142. +hostapd_bss_del_ban(void *eloop_data, void *user_ctx)
  143. +{
  144. + struct ubus_banned_client *ban = eloop_data;
  145. + struct hostapd_data *hapd = user_ctx;
  146. +
  147. + avl_delete(&hapd->ubus.banned, &ban->avl);
  148. + free(ban);
  149. +}
  150. +
  151. +static void
  152. +hostapd_bss_ban_client(struct hostapd_data *hapd, u8 *addr, int time)
  153. +{
  154. + struct ubus_banned_client *ban;
  155. +
  156. + if (time < 0)
  157. + time = 0;
  158. +
  159. + ban = avl_find_element(&hapd->ubus.banned, addr, ban, avl);
  160. + if (!ban) {
  161. + if (!time)
  162. + return;
  163. +
  164. + ban = os_zalloc(sizeof(*ban));
  165. + memcpy(ban->addr, addr, sizeof(ban->addr));
  166. + ban->avl.key = ban->addr;
  167. + avl_insert(&hapd->ubus.banned, &ban->avl);
  168. + } else {
  169. + eloop_cancel_timeout(hostapd_bss_del_ban, ban, hapd);
  170. + if (!time) {
  171. + hostapd_bss_del_ban(ban, hapd);
  172. + return;
  173. + }
  174. + }
  175. +
  176. + eloop_register_timeout(0, time * 1000, hostapd_bss_del_ban, ban, hapd);
  177. +}
  178. +
  179. +static int
  180. +hostapd_bss_get_clients(struct ubus_context *ctx, struct ubus_object *obj,
  181. + struct ubus_request_data *req, const char *method,
  182. + struct blob_attr *msg)
  183. +{
  184. + struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
  185. + struct sta_info *sta;
  186. + void *list, *c;
  187. + char mac_buf[20];
  188. + static const struct {
  189. + const char *name;
  190. + uint32_t flag;
  191. + } sta_flags[] = {
  192. + { "auth", WLAN_STA_AUTH },
  193. + { "assoc", WLAN_STA_ASSOC },
  194. + { "authorized", WLAN_STA_AUTHORIZED },
  195. + { "preauth", WLAN_STA_PREAUTH },
  196. + { "wds", WLAN_STA_WDS },
  197. + { "wmm", WLAN_STA_WMM },
  198. + { "ht", WLAN_STA_HT },
  199. + { "vht", WLAN_STA_VHT },
  200. + { "wps", WLAN_STA_WPS },
  201. + { "mfp", WLAN_STA_MFP },
  202. + };
  203. +
  204. + blob_buf_init(&b, 0);
  205. + blobmsg_add_u32(&b, "freq", hapd->iface->freq);
  206. + list = blobmsg_open_table(&b, "clients");
  207. + for (sta = hapd->sta_list; sta; sta = sta->next) {
  208. + int i;
  209. +
  210. + sprintf(mac_buf, MACSTR, MAC2STR(sta->addr));
  211. + c = blobmsg_open_table(&b, mac_buf);
  212. + for (i = 0; i < ARRAY_SIZE(sta_flags); i++)
  213. + blobmsg_add_u8(&b, sta_flags[i].name,
  214. + !!(sta->flags & sta_flags[i].flag));
  215. + blobmsg_add_u32(&b, "aid", sta->aid);
  216. + blobmsg_close_table(&b, c);
  217. + }
  218. + blobmsg_close_array(&b, list);
  219. + ubus_send_reply(ctx, req, b.head);
  220. +
  221. + return 0;
  222. +}
  223. +
  224. +enum {
  225. + DEL_CLIENT_ADDR,
  226. + DEL_CLIENT_REASON,
  227. + DEL_CLIENT_DEAUTH,
  228. + DEL_CLIENT_BAN_TIME,
  229. + __DEL_CLIENT_MAX
  230. +};
  231. +
  232. +static const struct blobmsg_policy del_policy[__DEL_CLIENT_MAX] = {
  233. + [DEL_CLIENT_ADDR] = { "addr", BLOBMSG_TYPE_STRING },
  234. + [DEL_CLIENT_REASON] = { "reason", BLOBMSG_TYPE_INT32 },
  235. + [DEL_CLIENT_DEAUTH] = { "deauth", BLOBMSG_TYPE_INT8 },
  236. + [DEL_CLIENT_BAN_TIME] = { "ban_time", BLOBMSG_TYPE_INT32 },
  237. +};
  238. +
  239. +static int
  240. +hostapd_bss_del_client(struct ubus_context *ctx, struct ubus_object *obj,
  241. + struct ubus_request_data *req, const char *method,
  242. + struct blob_attr *msg)
  243. +{
  244. + struct blob_attr *tb[__DEL_CLIENT_MAX];
  245. + struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
  246. + struct sta_info *sta;
  247. + bool deauth = false;
  248. + int reason;
  249. + u8 addr[ETH_ALEN];
  250. +
  251. + blobmsg_parse(del_policy, __DEL_CLIENT_MAX, tb, blob_data(msg), blob_len(msg));
  252. +
  253. + if (!tb[DEL_CLIENT_ADDR])
  254. + return UBUS_STATUS_INVALID_ARGUMENT;
  255. +
  256. + if (hwaddr_aton(blobmsg_data(tb[DEL_CLIENT_ADDR]), addr))
  257. + return UBUS_STATUS_INVALID_ARGUMENT;
  258. +
  259. + if (tb[DEL_CLIENT_REASON])
  260. + reason = blobmsg_get_u32(tb[DEL_CLIENT_REASON]);
  261. +
  262. + if (tb[DEL_CLIENT_DEAUTH])
  263. + deauth = blobmsg_get_bool(tb[DEL_CLIENT_DEAUTH]);
  264. +
  265. + sta = ap_get_sta(hapd, addr);
  266. + if (sta) {
  267. + if (deauth) {
  268. + hostapd_drv_sta_deauth(hapd, addr, reason);
  269. + ap_sta_deauthenticate(hapd, sta, reason);
  270. + } else {
  271. + hostapd_drv_sta_disassoc(hapd, addr, reason);
  272. + ap_sta_disassociate(hapd, sta, reason);
  273. + }
  274. + }
  275. +
  276. + if (tb[DEL_CLIENT_BAN_TIME])
  277. + hostapd_bss_ban_client(hapd, addr, blobmsg_get_u32(tb[DEL_CLIENT_BAN_TIME]));
  278. +
  279. + return 0;
  280. +}
  281. +
  282. +static void
  283. +blobmsg_add_macaddr(struct blob_buf *buf, const char *name, const u8 *addr)
  284. +{
  285. + char *s;
  286. +
  287. + s = blobmsg_alloc_string_buffer(buf, name, 20);
  288. + sprintf(s, MACSTR, MAC2STR(addr));
  289. + blobmsg_add_string_buffer(buf);
  290. +}
  291. +
  292. +static int
  293. +hostapd_bss_list_bans(struct ubus_context *ctx, struct ubus_object *obj,
  294. + struct ubus_request_data *req, const char *method,
  295. + struct blob_attr *msg)
  296. +{
  297. + struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
  298. + struct ubus_banned_client *ban;
  299. + void *c;
  300. +
  301. + blob_buf_init(&b, 0);
  302. + c = blobmsg_open_array(&b, "clients");
  303. + avl_for_each_element(&hapd->ubus.banned, ban, avl)
  304. + blobmsg_add_macaddr(&b, NULL, ban->addr);
  305. + blobmsg_close_array(&b, c);
  306. + ubus_send_reply(ctx, req, b.head);
  307. +
  308. + return 0;
  309. +}
  310. +
  311. +static int
  312. +hostapd_bss_wps_start(struct ubus_context *ctx, struct ubus_object *obj,
  313. + struct ubus_request_data *req, const char *method,
  314. + struct blob_attr *msg)
  315. +{
  316. + int rc;
  317. + struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
  318. +
  319. + rc = hostapd_wps_button_pushed(hapd, NULL);
  320. +
  321. + if (rc != 0)
  322. + return UBUS_STATUS_NOT_SUPPORTED;
  323. +
  324. + return 0;
  325. +}
  326. +
  327. +static int
  328. +hostapd_bss_wps_cancel(struct ubus_context *ctx, struct ubus_object *obj,
  329. + struct ubus_request_data *req, const char *method,
  330. + struct blob_attr *msg)
  331. +{
  332. + int rc;
  333. + struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
  334. +
  335. + rc = hostapd_wps_cancel(hapd);
  336. +
  337. + if (rc != 0)
  338. + return UBUS_STATUS_NOT_SUPPORTED;
  339. +
  340. + return 0;
  341. +}
  342. +
  343. +static int
  344. +hostapd_bss_update_beacon(struct ubus_context *ctx, struct ubus_object *obj,
  345. + struct ubus_request_data *req, const char *method,
  346. + struct blob_attr *msg)
  347. +{
  348. + int rc;
  349. + struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
  350. +
  351. + rc = ieee802_11_set_beacon(hapd);
  352. +
  353. + if (rc != 0)
  354. + return UBUS_STATUS_NOT_SUPPORTED;
  355. +
  356. + return 0;
  357. +}
  358. +
  359. +enum {
  360. + CSA_FREQ,
  361. + CSA_BCN_COUNT,
  362. + __CSA_MAX
  363. +};
  364. +
  365. +static const struct blobmsg_policy csa_policy[__CSA_MAX] = {
  366. + /*
  367. + * for now, frequency and beacon count are enough, add more
  368. + * parameters on demand
  369. + */
  370. + [CSA_FREQ] = { "freq", BLOBMSG_TYPE_INT32 },
  371. + [CSA_BCN_COUNT] = { "bcn_count", BLOBMSG_TYPE_INT32 },
  372. +};
  373. +
  374. +#ifdef NEED_AP_MLME
  375. +static int
  376. +hostapd_switch_chan(struct ubus_context *ctx, struct ubus_object *obj,
  377. + struct ubus_request_data *req, const char *method,
  378. + struct blob_attr *msg)
  379. +{
  380. + struct blob_attr *tb[__CSA_MAX];
  381. + struct hostapd_data *hapd = get_hapd_from_object(obj);
  382. + struct csa_settings css;
  383. +
  384. + blobmsg_parse(csa_policy, __CSA_MAX, tb, blob_data(msg), blob_len(msg));
  385. +
  386. + if (!tb[CSA_FREQ])
  387. + return UBUS_STATUS_INVALID_ARGUMENT;
  388. +
  389. + memset(&css, 0, sizeof(css));
  390. + css.freq_params.freq = blobmsg_get_u32(tb[CSA_FREQ]);
  391. + if (tb[CSA_BCN_COUNT])
  392. + css.cs_count = blobmsg_get_u32(tb[CSA_BCN_COUNT]);
  393. +
  394. + if (hostapd_switch_channel(hapd, &css) != 0)
  395. + return UBUS_STATUS_NOT_SUPPORTED;
  396. + return UBUS_STATUS_OK;
  397. +}
  398. +#endif
  399. +
  400. +enum {
  401. + VENDOR_ELEMENTS,
  402. + __VENDOR_ELEMENTS_MAX
  403. +};
  404. +
  405. +static const struct blobmsg_policy ve_policy[__VENDOR_ELEMENTS_MAX] = {
  406. + /* vendor elements are provided as hex-string */
  407. + [VENDOR_ELEMENTS] = { "vendor_elements", BLOBMSG_TYPE_STRING },
  408. +};
  409. +
  410. +static int
  411. +hostapd_vendor_elements(struct ubus_context *ctx, struct ubus_object *obj,
  412. + struct ubus_request_data *req, const char *method,
  413. + struct blob_attr *msg)
  414. +{
  415. + struct blob_attr *tb[__VENDOR_ELEMENTS_MAX];
  416. + struct hostapd_data *hapd = get_hapd_from_object(obj);
  417. + struct hostapd_bss_config *bss = hapd->conf;
  418. + struct wpabuf *elems;
  419. + const char *pos;
  420. + size_t len;
  421. +
  422. + blobmsg_parse(ve_policy, __VENDOR_ELEMENTS_MAX, tb,
  423. + blob_data(msg), blob_len(msg));
  424. +
  425. + if (!tb[VENDOR_ELEMENTS])
  426. + return UBUS_STATUS_INVALID_ARGUMENT;
  427. +
  428. + pos = blobmsg_data(tb[VENDOR_ELEMENTS]);
  429. + len = os_strlen(pos);
  430. + if (len & 0x01)
  431. + return UBUS_STATUS_INVALID_ARGUMENT;
  432. +
  433. + len /= 2;
  434. + if (len == 0) {
  435. + wpabuf_free(bss->vendor_elements);
  436. + bss->vendor_elements = NULL;
  437. + return 0;
  438. + }
  439. +
  440. + elems = wpabuf_alloc(len);
  441. + if (elems == NULL)
  442. + return 1;
  443. +
  444. + if (hexstr2bin(pos, wpabuf_put(elems, len), len)) {
  445. + wpabuf_free(elems);
  446. + return UBUS_STATUS_INVALID_ARGUMENT;
  447. + }
  448. +
  449. + wpabuf_free(bss->vendor_elements);
  450. + bss->vendor_elements = elems;
  451. +
  452. + /* update beacons if vendor elements were set successfully */
  453. + if (ieee802_11_update_beacons(hapd->iface) != 0)
  454. + return UBUS_STATUS_NOT_SUPPORTED;
  455. + return UBUS_STATUS_OK;
  456. +}
  457. +
  458. +static const struct ubus_method bss_methods[] = {
  459. + UBUS_METHOD_NOARG("get_clients", hostapd_bss_get_clients),
  460. + UBUS_METHOD("del_client", hostapd_bss_del_client, del_policy),
  461. + UBUS_METHOD_NOARG("list_bans", hostapd_bss_list_bans),
  462. + UBUS_METHOD_NOARG("wps_start", hostapd_bss_wps_start),
  463. + UBUS_METHOD_NOARG("wps_cancel", hostapd_bss_wps_cancel),
  464. + UBUS_METHOD_NOARG("update_beacon", hostapd_bss_update_beacon),
  465. +#ifdef NEED_AP_MLME
  466. + UBUS_METHOD("switch_chan", hostapd_switch_chan, csa_policy),
  467. +#endif
  468. + UBUS_METHOD("set_vendor_elements", hostapd_vendor_elements, ve_policy),
  469. +};
  470. +
  471. +static struct ubus_object_type bss_object_type =
  472. + UBUS_OBJECT_TYPE("hostapd_bss", bss_methods);
  473. +
  474. +static int avl_compare_macaddr(const void *k1, const void *k2, void *ptr)
  475. +{
  476. + return memcmp(k1, k2, ETH_ALEN);
  477. +}
  478. +
  479. +void hostapd_ubus_add_bss(struct hostapd_data *hapd)
  480. +{
  481. + struct ubus_object *obj = &hapd->ubus.obj;
  482. + char *name;
  483. + int ret;
  484. +
  485. + if (!hostapd_ubus_init())
  486. + return;
  487. +
  488. + if (asprintf(&name, "hostapd.%s", hapd->conf->iface) < 0)
  489. + return;
  490. +
  491. + avl_init(&hapd->ubus.banned, avl_compare_macaddr, false, NULL);
  492. + obj->name = name;
  493. + obj->type = &bss_object_type;
  494. + obj->methods = bss_object_type.methods;
  495. + obj->n_methods = bss_object_type.n_methods;
  496. + ret = ubus_add_object(ctx, obj);
  497. + hostapd_ubus_ref_inc();
  498. +}
  499. +
  500. +void hostapd_ubus_free_bss(struct hostapd_data *hapd)
  501. +{
  502. + struct ubus_object *obj = &hapd->ubus.obj;
  503. + char *name = (char *) obj->name;
  504. +
  505. + if (!ctx)
  506. + return;
  507. +
  508. + if (obj->id) {
  509. + ubus_remove_object(ctx, obj);
  510. + hostapd_ubus_ref_dec();
  511. + }
  512. +
  513. + free(name);
  514. +}
  515. +
  516. +struct ubus_event_req {
  517. + struct ubus_notify_request nreq;
  518. + bool deny;
  519. +};
  520. +
  521. +static void
  522. +ubus_event_cb(struct ubus_notify_request *req, int idx, int ret)
  523. +{
  524. + struct ubus_event_req *ureq = container_of(req, struct ubus_event_req, nreq);
  525. +
  526. + if (ret)
  527. + ureq->deny = true;
  528. +}
  529. +
  530. +int hostapd_ubus_handle_event(struct hostapd_data *hapd, struct hostapd_ubus_request *req)
  531. +{
  532. + struct ubus_banned_client *ban;
  533. + const char *types[HOSTAPD_UBUS_TYPE_MAX] = {
  534. + [HOSTAPD_UBUS_PROBE_REQ] = "probe",
  535. + [HOSTAPD_UBUS_AUTH_REQ] = "auth",
  536. + [HOSTAPD_UBUS_ASSOC_REQ] = "assoc",
  537. + };
  538. + const char *type = "mgmt";
  539. + struct ubus_event_req ureq = {};
  540. + const u8 *addr;
  541. +
  542. + if (req->mgmt_frame)
  543. + addr = req->mgmt_frame->sa;
  544. + else
  545. + addr = req->addr;
  546. +
  547. + ban = avl_find_element(&hapd->ubus.banned, addr, ban, avl);
  548. + if (ban)
  549. + return -2;
  550. +
  551. + if (!hapd->ubus.obj.has_subscribers)
  552. + return 0;
  553. +
  554. + if (req->type < ARRAY_SIZE(types))
  555. + type = types[req->type];
  556. +
  557. + blob_buf_init(&b, 0);
  558. + blobmsg_add_macaddr(&b, "address", addr);
  559. + if (req->mgmt_frame)
  560. + blobmsg_add_macaddr(&b, "target", req->mgmt_frame->da);
  561. + if (req->frame_info)
  562. + blobmsg_add_u32(&b, "signal", req->frame_info->ssi_signal);
  563. + blobmsg_add_u32(&b, "freq", hapd->iface->freq);
  564. +
  565. + if (ubus_notify_async(ctx, &hapd->ubus.obj, type, b.head, &ureq.nreq))
  566. + return 0;
  567. +
  568. + ureq.nreq.status_cb = ubus_event_cb;
  569. + ubus_complete_request(ctx, &ureq.nreq.req, 100);
  570. +
  571. + if (ureq.deny)
  572. + return -1;
  573. +
  574. + return 0;
  575. +}
  576. --- /dev/null
  577. +++ b/src/ap/ubus.h
  578. @@ -0,0 +1,78 @@
  579. +/*
  580. + * hostapd / ubus support
  581. + * Copyright (c) 2013, Felix Fietkau <nbd@nbd.name>
  582. + *
  583. + * This software may be distributed under the terms of the BSD license.
  584. + * See README for more details.
  585. + */
  586. +#ifndef __HOSTAPD_UBUS_H
  587. +#define __HOSTAPD_UBUS_H
  588. +
  589. +enum hostapd_ubus_event_type {
  590. + HOSTAPD_UBUS_PROBE_REQ,
  591. + HOSTAPD_UBUS_AUTH_REQ,
  592. + HOSTAPD_UBUS_ASSOC_REQ,
  593. + HOSTAPD_UBUS_TYPE_MAX
  594. +};
  595. +
  596. +struct hostapd_ubus_request {
  597. + enum hostapd_ubus_event_type type;
  598. + const struct ieee80211_mgmt *mgmt_frame;
  599. + const struct hostapd_frame_info *frame_info;
  600. + const u8 *addr;
  601. +};
  602. +
  603. +struct hostapd_iface;
  604. +struct hostapd_data;
  605. +
  606. +#ifdef UBUS_SUPPORT
  607. +
  608. +#include <libubox/avl.h>
  609. +#include <libubus.h>
  610. +
  611. +struct hostapd_ubus_iface {
  612. + struct ubus_object obj;
  613. +};
  614. +
  615. +struct hostapd_ubus_bss {
  616. + struct ubus_object obj;
  617. + struct avl_tree banned;
  618. +};
  619. +
  620. +void hostapd_ubus_add_iface(struct hostapd_iface *iface);
  621. +void hostapd_ubus_free_iface(struct hostapd_iface *iface);
  622. +void hostapd_ubus_add_bss(struct hostapd_data *hapd);
  623. +void hostapd_ubus_free_bss(struct hostapd_data *hapd);
  624. +
  625. +int hostapd_ubus_handle_event(struct hostapd_data *hapd, struct hostapd_ubus_request *req);
  626. +
  627. +#else
  628. +
  629. +struct hostapd_ubus_iface {};
  630. +
  631. +struct hostapd_ubus_bss {};
  632. +
  633. +static inline void hostapd_ubus_add_iface(struct hostapd_iface *iface)
  634. +{
  635. +}
  636. +
  637. +static inline void hostapd_ubus_free_iface(struct hostapd_iface *iface)
  638. +{
  639. +}
  640. +
  641. +static inline void hostapd_ubus_add_bss(struct hostapd_data *hapd)
  642. +{
  643. +}
  644. +
  645. +static inline void hostapd_ubus_free_bss(struct hostapd_data *hapd)
  646. +{
  647. +}
  648. +
  649. +static inline int hostapd_ubus_handle_event(struct hostapd_data *hapd, struct hostapd_ubus_request *req)
  650. +{
  651. + return 0;
  652. +}
  653. +
  654. +#endif
  655. +
  656. +#endif
  657. --- a/src/ap/hostapd.c
  658. +++ b/src/ap/hostapd.c
  659. @@ -284,6 +284,7 @@ static void hostapd_free_hapd_data(struc
  660. hapd->started = 0;
  661. wpa_printf(MSG_DEBUG, "%s(%s)", __func__, hapd->conf->iface);
  662. + hostapd_ubus_free_bss(hapd);
  663. iapp_deinit(hapd->iapp);
  664. hapd->iapp = NULL;
  665. accounting_deinit(hapd);
  666. @@ -1139,6 +1140,8 @@ static int hostapd_setup_bss(struct host
  667. if (hapd->driver && hapd->driver->set_operstate)
  668. hapd->driver->set_operstate(hapd->drv_priv, 1);
  669. + hostapd_ubus_add_bss(hapd);
  670. +
  671. return 0;
  672. }
  673. @@ -1664,6 +1667,7 @@ static int hostapd_setup_interface_compl
  674. if (err)
  675. goto fail;
  676. + hostapd_ubus_add_iface(iface);
  677. wpa_printf(MSG_DEBUG, "Completing interface initialization");
  678. if (iface->conf->channel) {
  679. #ifdef NEED_AP_MLME
  680. @@ -1844,6 +1848,7 @@ dfs_offload:
  681. fail:
  682. wpa_printf(MSG_ERROR, "Interface initialization failed");
  683. + hostapd_ubus_free_iface(iface);
  684. hostapd_set_state(iface, HAPD_IFACE_DISABLED);
  685. wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
  686. #ifdef CONFIG_FST
  687. @@ -2277,6 +2282,7 @@ void hostapd_interface_deinit_free(struc
  688. (unsigned int) iface->conf->num_bss);
  689. driver = iface->bss[0]->driver;
  690. drv_priv = iface->bss[0]->drv_priv;
  691. + hostapd_ubus_free_iface(iface);
  692. hostapd_interface_deinit(iface);
  693. wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
  694. __func__, driver, drv_priv);
  695. --- a/src/ap/ieee802_11.c
  696. +++ b/src/ap/ieee802_11.c
  697. @@ -980,7 +980,8 @@ int auth_sae_init_committed(struct hosta
  698. static void handle_auth(struct hostapd_data *hapd,
  699. - const struct ieee80211_mgmt *mgmt, size_t len)
  700. + const struct ieee80211_mgmt *mgmt, size_t len,
  701. + struct hostapd_frame_info *fi)
  702. {
  703. u16 auth_alg, auth_transaction, status_code;
  704. u16 resp = WLAN_STATUS_SUCCESS;
  705. @@ -996,6 +997,11 @@ static void handle_auth(struct hostapd_d
  706. char *identity = NULL;
  707. char *radius_cui = NULL;
  708. u16 seq_ctrl;
  709. + struct hostapd_ubus_request req = {
  710. + .type = HOSTAPD_UBUS_AUTH_REQ,
  711. + .mgmt_frame = mgmt,
  712. + .frame_info = fi,
  713. + };
  714. os_memset(&vlan_id, 0, sizeof(vlan_id));
  715. @@ -1149,6 +1155,14 @@ static void handle_auth(struct hostapd_d
  716. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  717. goto fail;
  718. }
  719. +
  720. + if (hostapd_ubus_handle_event(hapd, &req)) {
  721. + wpa_printf(MSG_DEBUG, "Station " MACSTR " rejected by ubus handler.\n",
  722. + MAC2STR(mgmt->sa));
  723. + resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  724. + goto fail;
  725. + }
  726. +
  727. if (res == HOSTAPD_ACL_PENDING) {
  728. wpa_printf(MSG_DEBUG, "Authentication frame from " MACSTR
  729. " waiting for an external authentication",
  730. @@ -2033,13 +2047,18 @@ static u16 send_assoc_resp(struct hostap
  731. static void handle_assoc(struct hostapd_data *hapd,
  732. const struct ieee80211_mgmt *mgmt, size_t len,
  733. - int reassoc)
  734. + int reassoc, struct hostapd_frame_info *fi)
  735. {
  736. u16 capab_info, listen_interval, seq_ctrl, fc;
  737. u16 resp = WLAN_STATUS_SUCCESS, reply_res;
  738. const u8 *pos;
  739. int left, i;
  740. struct sta_info *sta;
  741. + struct hostapd_ubus_request req = {
  742. + .type = HOSTAPD_UBUS_ASSOC_REQ,
  743. + .mgmt_frame = mgmt,
  744. + .frame_info = fi,
  745. + };
  746. if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_req) :
  747. sizeof(mgmt->u.assoc_req))) {
  748. @@ -2159,6 +2178,13 @@ static void handle_assoc(struct hostapd_
  749. }
  750. #endif /* CONFIG_MBO */
  751. + if (hostapd_ubus_handle_event(hapd, &req)) {
  752. + wpa_printf(MSG_DEBUG, "Station " MACSTR " assoc rejected by ubus handler.\n",
  753. + MAC2STR(mgmt->sa));
  754. + resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  755. + goto fail;
  756. + }
  757. +
  758. /*
  759. * sta->capability is used in check_assoc_ies() for RRM enabled
  760. * capability element.
  761. @@ -2639,7 +2665,7 @@ int ieee802_11_mgmt(struct hostapd_data
  762. if (stype == WLAN_FC_STYPE_PROBE_REQ) {
  763. - handle_probe_req(hapd, mgmt, len, fi->ssi_signal);
  764. + handle_probe_req(hapd, mgmt, len, fi);
  765. return 1;
  766. }
  767. @@ -2657,17 +2683,17 @@ int ieee802_11_mgmt(struct hostapd_data
  768. switch (stype) {
  769. case WLAN_FC_STYPE_AUTH:
  770. wpa_printf(MSG_DEBUG, "mgmt::auth");
  771. - handle_auth(hapd, mgmt, len);
  772. + handle_auth(hapd, mgmt, len, fi);
  773. ret = 1;
  774. break;
  775. case WLAN_FC_STYPE_ASSOC_REQ:
  776. wpa_printf(MSG_DEBUG, "mgmt::assoc_req");
  777. - handle_assoc(hapd, mgmt, len, 0);
  778. + handle_assoc(hapd, mgmt, len, 0, fi);
  779. ret = 1;
  780. break;
  781. case WLAN_FC_STYPE_REASSOC_REQ:
  782. wpa_printf(MSG_DEBUG, "mgmt::reassoc_req");
  783. - handle_assoc(hapd, mgmt, len, 1);
  784. + handle_assoc(hapd, mgmt, len, 1, fi);
  785. ret = 1;
  786. break;
  787. case WLAN_FC_STYPE_DISASSOC:
  788. --- a/src/ap/beacon.c
  789. +++ b/src/ap/beacon.c
  790. @@ -675,7 +675,7 @@ sta_track_seen_on(struct hostapd_iface *
  791. void handle_probe_req(struct hostapd_data *hapd,
  792. const struct ieee80211_mgmt *mgmt, size_t len,
  793. - int ssi_signal)
  794. + struct hostapd_frame_info *fi)
  795. {
  796. u8 *resp;
  797. struct ieee802_11_elems elems;
  798. @@ -684,9 +684,15 @@ void handle_probe_req(struct hostapd_dat
  799. size_t i, resp_len;
  800. int noack;
  801. enum ssid_match_result res;
  802. + int ssi_signal = fi->ssi_signal;
  803. int ret;
  804. u16 csa_offs[2];
  805. size_t csa_offs_len;
  806. + struct hostapd_ubus_request req = {
  807. + .type = HOSTAPD_UBUS_PROBE_REQ,
  808. + .mgmt_frame = mgmt,
  809. + .frame_info = fi,
  810. + };
  811. if (len < IEEE80211_HDRLEN)
  812. return;
  813. @@ -838,6 +844,12 @@ void handle_probe_req(struct hostapd_dat
  814. }
  815. #endif /* CONFIG_P2P */
  816. + if (hostapd_ubus_handle_event(hapd, &req)) {
  817. + wpa_printf(MSG_DEBUG, "Probe request for " MACSTR " rejected by ubus handler.\n",
  818. + MAC2STR(mgmt->sa));
  819. + return;
  820. + }
  821. +
  822. /* TODO: verify that supp_rates contains at least one matching rate
  823. * with AP configuration */
  824. --- a/src/ap/beacon.h
  825. +++ b/src/ap/beacon.h
  826. @@ -14,7 +14,7 @@ struct ieee80211_mgmt;
  827. void handle_probe_req(struct hostapd_data *hapd,
  828. const struct ieee80211_mgmt *mgmt, size_t len,
  829. - int ssi_signal);
  830. + struct hostapd_frame_info *fi);
  831. int ieee802_11_set_beacon(struct hostapd_data *hapd);
  832. int ieee802_11_set_beacons(struct hostapd_iface *iface);
  833. int ieee802_11_update_beacons(struct hostapd_iface *iface);
  834. --- a/src/ap/drv_callbacks.c
  835. +++ b/src/ap/drv_callbacks.c
  836. @@ -52,6 +52,10 @@ int hostapd_notif_assoc(struct hostapd_d
  837. u16 reason = WLAN_REASON_UNSPECIFIED;
  838. u16 status = WLAN_STATUS_SUCCESS;
  839. const u8 *p2p_dev_addr = NULL;
  840. + struct hostapd_ubus_request req = {
  841. + .type = HOSTAPD_UBUS_ASSOC_REQ,
  842. + .addr = addr,
  843. + };
  844. if (addr == NULL) {
  845. /*
  846. @@ -124,6 +128,12 @@ int hostapd_notif_assoc(struct hostapd_d
  847. goto fail;
  848. }
  849. + if (hostapd_ubus_handle_event(hapd, &req)) {
  850. + wpa_printf(MSG_DEBUG, "Station " MACSTR " assoc rejected by ubus handler.\n",
  851. + MAC2STR(req.addr));
  852. + goto fail;
  853. + }
  854. +
  855. #ifdef CONFIG_P2P
  856. if (elems.p2p) {
  857. wpabuf_free(sta->p2p_ie);