ap.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  1. /*
  2. * WPA Supplicant - Basic AP mode support routines
  3. * Copyright (c) 2003-2009, Jouni Malinen <j@w1.fi>
  4. * Copyright (c) 2009, Atheros Communications
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Alternatively, this software may be distributed under the terms of BSD
  11. * license.
  12. *
  13. * See README and COPYING for more details.
  14. */
  15. #include "utils/includes.h"
  16. #include "utils/common.h"
  17. #include "utils/eloop.h"
  18. #include "utils/uuid.h"
  19. #include "common/ieee802_11_defs.h"
  20. #include "common/wpa_ctrl.h"
  21. #include "ap/hostapd.h"
  22. #include "ap/ap_config.h"
  23. #include "ap/ap_drv_ops.h"
  24. #ifdef NEED_AP_MLME
  25. #include "ap/ieee802_11.h"
  26. #endif /* NEED_AP_MLME */
  27. #include "ap/beacon.h"
  28. #include "ap/ieee802_1x.h"
  29. #include "ap/wps_hostapd.h"
  30. #include "ap/ctrl_iface_ap.h"
  31. #include "wps/wps.h"
  32. #include "common/ieee802_11_defs.h"
  33. #include "config_ssid.h"
  34. #include "config.h"
  35. #include "wpa_supplicant_i.h"
  36. #include "driver_i.h"
  37. #include "p2p_supplicant.h"
  38. #include "ap.h"
  39. #include "ap/sta_info.h"
  40. #include "notify.h"
  41. #ifdef CONFIG_WPS
  42. static void wpas_wps_ap_pin_timeout(void *eloop_data, void *user_ctx);
  43. #endif /* CONFIG_WPS */
  44. static int wpa_supplicant_conf_ap(struct wpa_supplicant *wpa_s,
  45. struct wpa_ssid *ssid,
  46. struct hostapd_config *conf)
  47. {
  48. struct hostapd_bss_config *bss = &conf->bss[0];
  49. int pairwise;
  50. conf->driver = wpa_s->driver;
  51. os_strlcpy(bss->iface, wpa_s->ifname, sizeof(bss->iface));
  52. if (ssid->frequency == 0) {
  53. /* default channel 11 */
  54. conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
  55. conf->channel = 11;
  56. } else if (ssid->frequency >= 2412 && ssid->frequency <= 2472) {
  57. conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
  58. conf->channel = (ssid->frequency - 2407) / 5;
  59. } else if ((ssid->frequency >= 5180 && ssid->frequency <= 5240) ||
  60. (ssid->frequency >= 5745 && ssid->frequency <= 5825)) {
  61. conf->hw_mode = HOSTAPD_MODE_IEEE80211A;
  62. conf->channel = (ssid->frequency - 5000) / 5;
  63. } else {
  64. wpa_printf(MSG_ERROR, "Unsupported AP mode frequency: %d MHz",
  65. ssid->frequency);
  66. return -1;
  67. }
  68. /* TODO: enable HT40 if driver supports it;
  69. * drop to 11b if driver does not support 11g */
  70. #ifdef CONFIG_IEEE80211N
  71. /*
  72. * Enable HT20 if the driver supports it, by setting conf->ieee80211n.
  73. * Using default config settings for: conf->ht_op_mode_fixed,
  74. * conf->ht_capab, conf->secondary_channel, conf->require_ht
  75. */
  76. if (wpa_s->hw.modes) {
  77. struct hostapd_hw_modes *mode = NULL;
  78. int i;
  79. for (i = 0; i < wpa_s->hw.num_modes; i++) {
  80. if (wpa_s->hw.modes[i].mode == conf->hw_mode) {
  81. mode = &wpa_s->hw.modes[i];
  82. break;
  83. }
  84. }
  85. if (mode && mode->ht_capab)
  86. conf->ieee80211n = 1;
  87. }
  88. #endif /* CONFIG_IEEE80211N */
  89. #ifdef CONFIG_P2P
  90. if (conf->hw_mode == HOSTAPD_MODE_IEEE80211G) {
  91. /* Remove 802.11b rates from supported and basic rate sets */
  92. int *list = os_malloc(4 * sizeof(int));
  93. if (list) {
  94. list[0] = 60;
  95. list[1] = 120;
  96. list[2] = 240;
  97. list[3] = -1;
  98. }
  99. conf->basic_rates = list;
  100. list = os_malloc(9 * sizeof(int));
  101. if (list) {
  102. list[0] = 60;
  103. list[1] = 90;
  104. list[2] = 120;
  105. list[3] = 180;
  106. list[4] = 240;
  107. list[5] = 360;
  108. list[6] = 480;
  109. list[7] = 540;
  110. list[8] = -1;
  111. }
  112. conf->supported_rates = list;
  113. }
  114. bss->isolate = !wpa_s->conf->p2p_intra_bss;
  115. #endif /* CONFIG_P2P */
  116. if (ssid->ssid_len == 0) {
  117. wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
  118. return -1;
  119. }
  120. os_memcpy(bss->ssid.ssid, ssid->ssid, ssid->ssid_len);
  121. bss->ssid.ssid[ssid->ssid_len] = '\0';
  122. bss->ssid.ssid_len = ssid->ssid_len;
  123. bss->ssid.ssid_set = 1;
  124. if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt))
  125. bss->wpa = ssid->proto;
  126. bss->wpa_key_mgmt = ssid->key_mgmt;
  127. bss->wpa_pairwise = ssid->pairwise_cipher;
  128. if (ssid->passphrase) {
  129. bss->ssid.wpa_passphrase = os_strdup(ssid->passphrase);
  130. } else if (ssid->psk_set) {
  131. os_free(bss->ssid.wpa_psk);
  132. bss->ssid.wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
  133. if (bss->ssid.wpa_psk == NULL)
  134. return -1;
  135. os_memcpy(bss->ssid.wpa_psk->psk, ssid->psk, PMK_LEN);
  136. bss->ssid.wpa_psk->group = 1;
  137. } else if (ssid->wep_key_len[0] || ssid->wep_key_len[1] ||
  138. ssid->wep_key_len[2] || ssid->wep_key_len[3]) {
  139. struct hostapd_wep_keys *wep = &bss->ssid.wep;
  140. int i;
  141. for (i = 0; i < NUM_WEP_KEYS; i++) {
  142. if (ssid->wep_key_len[i] == 0)
  143. continue;
  144. wep->key[i] = os_malloc(ssid->wep_key_len[i]);
  145. if (wep->key[i] == NULL)
  146. return -1;
  147. os_memcpy(wep->key[i], ssid->wep_key[i],
  148. ssid->wep_key_len[i]);
  149. wep->len[i] = ssid->wep_key_len[i];
  150. }
  151. wep->idx = ssid->wep_tx_keyidx;
  152. wep->keys_set = 1;
  153. }
  154. /* Select group cipher based on the enabled pairwise cipher suites */
  155. pairwise = 0;
  156. if (bss->wpa & 1)
  157. pairwise |= bss->wpa_pairwise;
  158. if (bss->wpa & 2) {
  159. if (bss->rsn_pairwise == 0)
  160. bss->rsn_pairwise = bss->wpa_pairwise;
  161. pairwise |= bss->rsn_pairwise;
  162. }
  163. if (pairwise & WPA_CIPHER_TKIP)
  164. bss->wpa_group = WPA_CIPHER_TKIP;
  165. else
  166. bss->wpa_group = WPA_CIPHER_CCMP;
  167. if (bss->wpa && bss->ieee802_1x)
  168. bss->ssid.security_policy = SECURITY_WPA;
  169. else if (bss->wpa)
  170. bss->ssid.security_policy = SECURITY_WPA_PSK;
  171. else if (bss->ieee802_1x) {
  172. int cipher = WPA_CIPHER_NONE;
  173. bss->ssid.security_policy = SECURITY_IEEE_802_1X;
  174. bss->ssid.wep.default_len = bss->default_wep_key_len;
  175. if (bss->default_wep_key_len)
  176. cipher = bss->default_wep_key_len >= 13 ?
  177. WPA_CIPHER_WEP104 : WPA_CIPHER_WEP40;
  178. bss->wpa_group = cipher;
  179. bss->wpa_pairwise = cipher;
  180. bss->rsn_pairwise = cipher;
  181. } else if (bss->ssid.wep.keys_set) {
  182. int cipher = WPA_CIPHER_WEP40;
  183. if (bss->ssid.wep.len[0] >= 13)
  184. cipher = WPA_CIPHER_WEP104;
  185. bss->ssid.security_policy = SECURITY_STATIC_WEP;
  186. bss->wpa_group = cipher;
  187. bss->wpa_pairwise = cipher;
  188. bss->rsn_pairwise = cipher;
  189. } else {
  190. bss->ssid.security_policy = SECURITY_PLAINTEXT;
  191. bss->wpa_group = WPA_CIPHER_NONE;
  192. bss->wpa_pairwise = WPA_CIPHER_NONE;
  193. bss->rsn_pairwise = WPA_CIPHER_NONE;
  194. }
  195. #ifdef CONFIG_WPS
  196. /*
  197. * Enable WPS by default for open and WPA/WPA2-Personal network, but
  198. * require user interaction to actually use it. Only the internal
  199. * Registrar is supported.
  200. */
  201. if (bss->ssid.security_policy != SECURITY_WPA_PSK &&
  202. bss->ssid.security_policy != SECURITY_PLAINTEXT)
  203. goto no_wps;
  204. bss->eap_server = 1;
  205. bss->wps_state = 2;
  206. bss->ap_setup_locked = 2;
  207. if (wpa_s->conf->config_methods)
  208. bss->config_methods = os_strdup(wpa_s->conf->config_methods);
  209. os_memcpy(bss->device_type, wpa_s->conf->device_type,
  210. WPS_DEV_TYPE_LEN);
  211. if (wpa_s->conf->device_name) {
  212. bss->device_name = os_strdup(wpa_s->conf->device_name);
  213. bss->friendly_name = os_strdup(wpa_s->conf->device_name);
  214. }
  215. if (wpa_s->conf->manufacturer)
  216. bss->manufacturer = os_strdup(wpa_s->conf->manufacturer);
  217. if (wpa_s->conf->model_name)
  218. bss->model_name = os_strdup(wpa_s->conf->model_name);
  219. if (wpa_s->conf->model_number)
  220. bss->model_number = os_strdup(wpa_s->conf->model_number);
  221. if (wpa_s->conf->serial_number)
  222. bss->serial_number = os_strdup(wpa_s->conf->serial_number);
  223. if (is_nil_uuid(wpa_s->conf->uuid))
  224. os_memcpy(bss->uuid, wpa_s->wps->uuid, WPS_UUID_LEN);
  225. else
  226. os_memcpy(bss->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
  227. os_memcpy(bss->os_version, wpa_s->conf->os_version, 4);
  228. no_wps:
  229. #endif /* CONFIG_WPS */
  230. if (wpa_s->max_stations &&
  231. wpa_s->max_stations < wpa_s->conf->max_num_sta)
  232. bss->max_num_sta = wpa_s->max_stations;
  233. else
  234. bss->max_num_sta = wpa_s->conf->max_num_sta;
  235. bss->disassoc_low_ack = wpa_s->conf->disassoc_low_ack;
  236. return 0;
  237. }
  238. static void ap_public_action_rx(void *ctx, const u8 *buf, size_t len, int freq)
  239. {
  240. #ifdef CONFIG_P2P
  241. struct wpa_supplicant *wpa_s = ctx;
  242. const struct ieee80211_mgmt *mgmt;
  243. size_t hdr_len;
  244. mgmt = (const struct ieee80211_mgmt *) buf;
  245. hdr_len = (const u8 *) &mgmt->u.action.u.vs_public_action.action - buf;
  246. if (hdr_len > len)
  247. return;
  248. wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
  249. mgmt->u.action.category,
  250. &mgmt->u.action.u.vs_public_action.action,
  251. len - hdr_len, freq);
  252. #endif /* CONFIG_P2P */
  253. }
  254. static void ap_wps_event_cb(void *ctx, enum wps_event event,
  255. union wps_event_data *data)
  256. {
  257. #ifdef CONFIG_P2P
  258. struct wpa_supplicant *wpa_s = ctx;
  259. if (event == WPS_EV_FAIL) {
  260. struct wps_event_fail *fail = &data->fail;
  261. if (wpa_s->parent && wpa_s->parent != wpa_s &&
  262. wpa_s == wpa_s->global->p2p_group_formation) {
  263. /*
  264. * src/ap/wps_hostapd.c has already sent this on the
  265. * main interface, so only send on the parent interface
  266. * here if needed.
  267. */
  268. wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_FAIL
  269. "msg=%d config_error=%d",
  270. fail->msg, fail->config_error);
  271. }
  272. wpas_p2p_wps_failed(wpa_s, fail);
  273. }
  274. #endif /* CONFIG_P2P */
  275. }
  276. static void ap_sta_authorized_cb(void *ctx, const u8 *mac_addr,
  277. int authorized)
  278. {
  279. wpas_notify_sta_authorized(ctx, mac_addr, authorized);
  280. }
  281. static int ap_vendor_action_rx(void *ctx, const u8 *buf, size_t len, int freq)
  282. {
  283. #ifdef CONFIG_P2P
  284. struct wpa_supplicant *wpa_s = ctx;
  285. const struct ieee80211_mgmt *mgmt;
  286. size_t hdr_len;
  287. mgmt = (const struct ieee80211_mgmt *) buf;
  288. hdr_len = (const u8 *) &mgmt->u.action.u.vs_public_action.action - buf;
  289. if (hdr_len > len)
  290. return -1;
  291. wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
  292. mgmt->u.action.category,
  293. &mgmt->u.action.u.vs_public_action.action,
  294. len - hdr_len, freq);
  295. #endif /* CONFIG_P2P */
  296. return 0;
  297. }
  298. static int ap_probe_req_rx(void *ctx, const u8 *sa, const u8 *da,
  299. const u8 *bssid, const u8 *ie, size_t ie_len)
  300. {
  301. #ifdef CONFIG_P2P
  302. struct wpa_supplicant *wpa_s = ctx;
  303. return wpas_p2p_probe_req_rx(wpa_s, sa, da, bssid, ie, ie_len);
  304. #else /* CONFIG_P2P */
  305. return 0;
  306. #endif /* CONFIG_P2P */
  307. }
  308. static void ap_wps_reg_success_cb(void *ctx, const u8 *mac_addr,
  309. const u8 *uuid_e)
  310. {
  311. #ifdef CONFIG_P2P
  312. struct wpa_supplicant *wpa_s = ctx;
  313. wpas_p2p_wps_success(wpa_s, mac_addr, 1);
  314. #endif /* CONFIG_P2P */
  315. }
  316. static void wpas_ap_configured_cb(void *ctx)
  317. {
  318. struct wpa_supplicant *wpa_s = ctx;
  319. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  320. if (wpa_s->ap_configured_cb)
  321. wpa_s->ap_configured_cb(wpa_s->ap_configured_cb_ctx,
  322. wpa_s->ap_configured_cb_data);
  323. }
  324. int wpa_supplicant_create_ap(struct wpa_supplicant *wpa_s,
  325. struct wpa_ssid *ssid)
  326. {
  327. struct wpa_driver_associate_params params;
  328. struct hostapd_iface *hapd_iface;
  329. struct hostapd_config *conf;
  330. size_t i;
  331. if (ssid->ssid == NULL || ssid->ssid_len == 0) {
  332. wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
  333. return -1;
  334. }
  335. wpa_supplicant_ap_deinit(wpa_s);
  336. wpa_printf(MSG_DEBUG, "Setting up AP (SSID='%s')",
  337. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  338. os_memset(&params, 0, sizeof(params));
  339. params.ssid = ssid->ssid;
  340. params.ssid_len = ssid->ssid_len;
  341. switch (ssid->mode) {
  342. case WPAS_MODE_INFRA:
  343. params.mode = IEEE80211_MODE_INFRA;
  344. break;
  345. case WPAS_MODE_IBSS:
  346. params.mode = IEEE80211_MODE_IBSS;
  347. break;
  348. case WPAS_MODE_AP:
  349. case WPAS_MODE_P2P_GO:
  350. case WPAS_MODE_P2P_GROUP_FORMATION:
  351. params.mode = IEEE80211_MODE_AP;
  352. break;
  353. }
  354. params.freq = ssid->frequency;
  355. params.wpa_proto = ssid->proto;
  356. if (ssid->key_mgmt & WPA_KEY_MGMT_PSK)
  357. wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
  358. else
  359. wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
  360. params.key_mgmt_suite = key_mgmt2driver(wpa_s->key_mgmt);
  361. if (ssid->pairwise_cipher & WPA_CIPHER_CCMP)
  362. wpa_s->pairwise_cipher = WPA_CIPHER_CCMP;
  363. else if (ssid->pairwise_cipher & WPA_CIPHER_TKIP)
  364. wpa_s->pairwise_cipher = WPA_CIPHER_TKIP;
  365. else if (ssid->pairwise_cipher & WPA_CIPHER_NONE)
  366. wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
  367. else {
  368. wpa_printf(MSG_WARNING, "WPA: Failed to select pairwise "
  369. "cipher.");
  370. return -1;
  371. }
  372. params.pairwise_suite = cipher_suite2driver(wpa_s->pairwise_cipher);
  373. params.group_suite = params.pairwise_suite;
  374. #ifdef CONFIG_P2P
  375. if (ssid->mode == WPAS_MODE_P2P_GO ||
  376. ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
  377. params.p2p = 1;
  378. #endif /* CONFIG_P2P */
  379. if (wpa_s->parent->set_ap_uapsd)
  380. params.uapsd = wpa_s->parent->ap_uapsd;
  381. else
  382. params.uapsd = -1;
  383. if (wpa_drv_associate(wpa_s, &params) < 0) {
  384. wpa_msg(wpa_s, MSG_INFO, "Failed to start AP functionality");
  385. return -1;
  386. }
  387. wpa_s->ap_iface = hapd_iface = os_zalloc(sizeof(*wpa_s->ap_iface));
  388. if (hapd_iface == NULL)
  389. return -1;
  390. hapd_iface->owner = wpa_s;
  391. hapd_iface->drv_flags = wpa_s->drv_flags;
  392. wpa_s->ap_iface->conf = conf = hostapd_config_defaults();
  393. if (conf == NULL) {
  394. wpa_supplicant_ap_deinit(wpa_s);
  395. return -1;
  396. }
  397. if (params.uapsd > 0) {
  398. conf->bss->wmm_enabled = 1;
  399. conf->bss->wmm_uapsd = 1;
  400. }
  401. if (wpa_supplicant_conf_ap(wpa_s, ssid, conf)) {
  402. wpa_printf(MSG_ERROR, "Failed to create AP configuration");
  403. wpa_supplicant_ap_deinit(wpa_s);
  404. return -1;
  405. }
  406. #ifdef CONFIG_P2P
  407. if (ssid->mode == WPAS_MODE_P2P_GO)
  408. conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER;
  409. else if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
  410. conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER |
  411. P2P_GROUP_FORMATION;
  412. #endif /* CONFIG_P2P */
  413. hapd_iface->num_bss = conf->num_bss;
  414. hapd_iface->bss = os_zalloc(conf->num_bss *
  415. sizeof(struct hostapd_data *));
  416. if (hapd_iface->bss == NULL) {
  417. wpa_supplicant_ap_deinit(wpa_s);
  418. return -1;
  419. }
  420. for (i = 0; i < conf->num_bss; i++) {
  421. hapd_iface->bss[i] =
  422. hostapd_alloc_bss_data(hapd_iface, conf,
  423. &conf->bss[i]);
  424. if (hapd_iface->bss[i] == NULL) {
  425. wpa_supplicant_ap_deinit(wpa_s);
  426. return -1;
  427. }
  428. hapd_iface->bss[i]->msg_ctx = wpa_s;
  429. hapd_iface->bss[i]->public_action_cb = ap_public_action_rx;
  430. hapd_iface->bss[i]->public_action_cb_ctx = wpa_s;
  431. hapd_iface->bss[i]->vendor_action_cb = ap_vendor_action_rx;
  432. hapd_iface->bss[i]->vendor_action_cb_ctx = wpa_s;
  433. hostapd_register_probereq_cb(hapd_iface->bss[i],
  434. ap_probe_req_rx, wpa_s);
  435. hapd_iface->bss[i]->wps_reg_success_cb = ap_wps_reg_success_cb;
  436. hapd_iface->bss[i]->wps_reg_success_cb_ctx = wpa_s;
  437. hapd_iface->bss[i]->wps_event_cb = ap_wps_event_cb;
  438. hapd_iface->bss[i]->wps_event_cb_ctx = wpa_s;
  439. hapd_iface->bss[i]->sta_authorized_cb = ap_sta_authorized_cb;
  440. hapd_iface->bss[i]->sta_authorized_cb_ctx = wpa_s;
  441. #ifdef CONFIG_P2P
  442. hapd_iface->bss[i]->p2p = wpa_s->global->p2p;
  443. hapd_iface->bss[i]->p2p_group = wpas_p2p_group_init(
  444. wpa_s, ssid->p2p_persistent_group,
  445. ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION);
  446. #endif /* CONFIG_P2P */
  447. hapd_iface->bss[i]->setup_complete_cb = wpas_ap_configured_cb;
  448. hapd_iface->bss[i]->setup_complete_cb_ctx = wpa_s;
  449. }
  450. os_memcpy(hapd_iface->bss[0]->own_addr, wpa_s->own_addr, ETH_ALEN);
  451. hapd_iface->bss[0]->driver = wpa_s->driver;
  452. hapd_iface->bss[0]->drv_priv = wpa_s->drv_priv;
  453. wpa_s->current_ssid = ssid;
  454. os_memcpy(wpa_s->bssid, wpa_s->own_addr, ETH_ALEN);
  455. wpa_s->assoc_freq = ssid->frequency;
  456. if (hostapd_setup_interface(wpa_s->ap_iface)) {
  457. wpa_printf(MSG_ERROR, "Failed to initialize AP interface");
  458. wpa_supplicant_ap_deinit(wpa_s);
  459. return -1;
  460. }
  461. return 0;
  462. }
  463. void wpa_supplicant_ap_deinit(struct wpa_supplicant *wpa_s)
  464. {
  465. #ifdef CONFIG_WPS
  466. eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
  467. #endif /* CONFIG_WPS */
  468. if (wpa_s->ap_iface == NULL)
  469. return;
  470. wpa_s->current_ssid = NULL;
  471. wpa_s->assoc_freq = 0;
  472. wpa_s->reassociated_connection = 0;
  473. #ifdef CONFIG_P2P
  474. if (wpa_s->ap_iface->bss)
  475. wpa_s->ap_iface->bss[0]->p2p_group = NULL;
  476. wpas_p2p_group_deinit(wpa_s);
  477. #endif /* CONFIG_P2P */
  478. hostapd_interface_deinit(wpa_s->ap_iface);
  479. hostapd_interface_free(wpa_s->ap_iface);
  480. wpa_s->ap_iface = NULL;
  481. wpa_drv_deinit_ap(wpa_s);
  482. }
  483. void ap_tx_status(void *ctx, const u8 *addr,
  484. const u8 *buf, size_t len, int ack)
  485. {
  486. #ifdef NEED_AP_MLME
  487. struct wpa_supplicant *wpa_s = ctx;
  488. hostapd_tx_status(wpa_s->ap_iface->bss[0], addr, buf, len, ack);
  489. #endif /* NEED_AP_MLME */
  490. }
  491. void ap_client_poll_ok(void *ctx, const u8 *addr)
  492. {
  493. #ifdef NEED_AP_MLME
  494. struct wpa_supplicant *wpa_s = ctx;
  495. if (wpa_s->ap_iface)
  496. hostapd_client_poll_ok(wpa_s->ap_iface->bss[0], addr);
  497. #endif /* NEED_AP_MLME */
  498. }
  499. void ap_rx_from_unknown_sta(void *ctx, const u8 *addr, int wds)
  500. {
  501. #ifdef NEED_AP_MLME
  502. struct wpa_supplicant *wpa_s = ctx;
  503. ieee802_11_rx_from_unknown(wpa_s->ap_iface->bss[0], addr, wds);
  504. #endif /* NEED_AP_MLME */
  505. }
  506. void ap_mgmt_rx(void *ctx, struct rx_mgmt *rx_mgmt)
  507. {
  508. #ifdef NEED_AP_MLME
  509. struct wpa_supplicant *wpa_s = ctx;
  510. struct hostapd_frame_info fi;
  511. os_memset(&fi, 0, sizeof(fi));
  512. fi.datarate = rx_mgmt->datarate;
  513. fi.ssi_signal = rx_mgmt->ssi_signal;
  514. ieee802_11_mgmt(wpa_s->ap_iface->bss[0], rx_mgmt->frame,
  515. rx_mgmt->frame_len, &fi);
  516. #endif /* NEED_AP_MLME */
  517. }
  518. void ap_mgmt_tx_cb(void *ctx, const u8 *buf, size_t len, u16 stype, int ok)
  519. {
  520. #ifdef NEED_AP_MLME
  521. struct wpa_supplicant *wpa_s = ctx;
  522. ieee802_11_mgmt_cb(wpa_s->ap_iface->bss[0], buf, len, stype, ok);
  523. #endif /* NEED_AP_MLME */
  524. }
  525. void wpa_supplicant_ap_rx_eapol(struct wpa_supplicant *wpa_s,
  526. const u8 *src_addr, const u8 *buf, size_t len)
  527. {
  528. ieee802_1x_receive(wpa_s->ap_iface->bss[0], src_addr, buf, len);
  529. }
  530. #ifdef CONFIG_WPS
  531. int wpa_supplicant_ap_wps_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid,
  532. const u8 *p2p_dev_addr)
  533. {
  534. if (!wpa_s->ap_iface)
  535. return -1;
  536. return hostapd_wps_button_pushed(wpa_s->ap_iface->bss[0],
  537. p2p_dev_addr);
  538. }
  539. static int wpa_supplicant_ap_wps_sta_cancel(struct hostapd_data *hapd,
  540. struct sta_info *sta, void *ctx)
  541. {
  542. if (sta && (sta->flags & WLAN_STA_WPS)) {
  543. ap_sta_deauthenticate(hapd, sta,
  544. WLAN_REASON_PREV_AUTH_NOT_VALID);
  545. wpa_printf(MSG_DEBUG, "WPS: %s: Deauth sta=" MACSTR,
  546. __func__, MAC2STR(sta->addr));
  547. return 1;
  548. }
  549. return 0;
  550. }
  551. int wpa_supplicant_ap_wps_cancel(struct wpa_supplicant *wpa_s)
  552. {
  553. struct wps_registrar *reg;
  554. int reg_sel = 0, wps_sta = 0;
  555. if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0]->wps)
  556. return -1;
  557. reg = wpa_s->ap_iface->bss[0]->wps->registrar;
  558. reg_sel = wps_registrar_wps_cancel(reg);
  559. wps_sta = ap_for_each_sta(wpa_s->ap_iface->bss[0],
  560. wpa_supplicant_ap_wps_sta_cancel, NULL);
  561. if (!reg_sel && !wps_sta) {
  562. wpa_printf(MSG_DEBUG, "No WPS operation in progress at this "
  563. "time");
  564. return -1;
  565. }
  566. /*
  567. * There are 2 cases to return wps cancel as success:
  568. * 1. When wps cancel was initiated but no connection has been
  569. * established with client yet.
  570. * 2. Client is in the middle of exchanging WPS messages.
  571. */
  572. return 0;
  573. }
  574. int wpa_supplicant_ap_wps_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
  575. const char *pin, char *buf, size_t buflen)
  576. {
  577. int ret, ret_len = 0;
  578. if (!wpa_s->ap_iface)
  579. return -1;
  580. if (pin == NULL) {
  581. unsigned int rpin = wps_generate_pin();
  582. ret_len = os_snprintf(buf, buflen, "%d", rpin);
  583. pin = buf;
  584. } else
  585. ret_len = os_snprintf(buf, buflen, "%s", pin);
  586. ret = hostapd_wps_add_pin(wpa_s->ap_iface->bss[0], bssid, "any", pin,
  587. 0);
  588. if (ret)
  589. return -1;
  590. return ret_len;
  591. }
  592. static void wpas_wps_ap_pin_timeout(void *eloop_data, void *user_ctx)
  593. {
  594. struct wpa_supplicant *wpa_s = eloop_data;
  595. wpa_printf(MSG_DEBUG, "WPS: AP PIN timed out");
  596. wpas_wps_ap_pin_disable(wpa_s);
  597. }
  598. static void wpas_wps_ap_pin_enable(struct wpa_supplicant *wpa_s, int timeout)
  599. {
  600. struct hostapd_data *hapd;
  601. if (wpa_s->ap_iface == NULL)
  602. return;
  603. hapd = wpa_s->ap_iface->bss[0];
  604. wpa_printf(MSG_DEBUG, "WPS: Enabling AP PIN (timeout=%d)", timeout);
  605. hapd->ap_pin_failures = 0;
  606. eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
  607. if (timeout > 0)
  608. eloop_register_timeout(timeout, 0,
  609. wpas_wps_ap_pin_timeout, wpa_s, NULL);
  610. }
  611. void wpas_wps_ap_pin_disable(struct wpa_supplicant *wpa_s)
  612. {
  613. struct hostapd_data *hapd;
  614. if (wpa_s->ap_iface == NULL)
  615. return;
  616. wpa_printf(MSG_DEBUG, "WPS: Disabling AP PIN");
  617. hapd = wpa_s->ap_iface->bss[0];
  618. os_free(hapd->conf->ap_pin);
  619. hapd->conf->ap_pin = NULL;
  620. eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
  621. }
  622. const char * wpas_wps_ap_pin_random(struct wpa_supplicant *wpa_s, int timeout)
  623. {
  624. struct hostapd_data *hapd;
  625. unsigned int pin;
  626. char pin_txt[9];
  627. if (wpa_s->ap_iface == NULL)
  628. return NULL;
  629. hapd = wpa_s->ap_iface->bss[0];
  630. pin = wps_generate_pin();
  631. os_snprintf(pin_txt, sizeof(pin_txt), "%u", pin);
  632. os_free(hapd->conf->ap_pin);
  633. hapd->conf->ap_pin = os_strdup(pin_txt);
  634. if (hapd->conf->ap_pin == NULL)
  635. return NULL;
  636. wpas_wps_ap_pin_enable(wpa_s, timeout);
  637. return hapd->conf->ap_pin;
  638. }
  639. const char * wpas_wps_ap_pin_get(struct wpa_supplicant *wpa_s)
  640. {
  641. struct hostapd_data *hapd;
  642. if (wpa_s->ap_iface == NULL)
  643. return NULL;
  644. hapd = wpa_s->ap_iface->bss[0];
  645. return hapd->conf->ap_pin;
  646. }
  647. int wpas_wps_ap_pin_set(struct wpa_supplicant *wpa_s, const char *pin,
  648. int timeout)
  649. {
  650. struct hostapd_data *hapd;
  651. char pin_txt[9];
  652. int ret;
  653. if (wpa_s->ap_iface == NULL)
  654. return -1;
  655. hapd = wpa_s->ap_iface->bss[0];
  656. ret = os_snprintf(pin_txt, sizeof(pin_txt), "%s", pin);
  657. if (ret < 0 || ret >= (int) sizeof(pin_txt))
  658. return -1;
  659. os_free(hapd->conf->ap_pin);
  660. hapd->conf->ap_pin = os_strdup(pin_txt);
  661. if (hapd->conf->ap_pin == NULL)
  662. return -1;
  663. wpas_wps_ap_pin_enable(wpa_s, timeout);
  664. return 0;
  665. }
  666. void wpa_supplicant_ap_pwd_auth_fail(struct wpa_supplicant *wpa_s)
  667. {
  668. struct hostapd_data *hapd;
  669. if (wpa_s->ap_iface == NULL)
  670. return;
  671. hapd = wpa_s->ap_iface->bss[0];
  672. /*
  673. * Registrar failed to prove its knowledge of the AP PIN. Disable AP
  674. * PIN if this happens multiple times to slow down brute force attacks.
  675. */
  676. hapd->ap_pin_failures++;
  677. wpa_printf(MSG_DEBUG, "WPS: AP PIN authentication failure number %u",
  678. hapd->ap_pin_failures);
  679. if (hapd->ap_pin_failures < 3)
  680. return;
  681. wpa_printf(MSG_DEBUG, "WPS: Disable AP PIN");
  682. hapd->ap_pin_failures = 0;
  683. os_free(hapd->conf->ap_pin);
  684. hapd->conf->ap_pin = NULL;
  685. }
  686. #endif /* CONFIG_WPS */
  687. #ifdef CONFIG_CTRL_IFACE
  688. int ap_ctrl_iface_sta_first(struct wpa_supplicant *wpa_s,
  689. char *buf, size_t buflen)
  690. {
  691. if (wpa_s->ap_iface == NULL)
  692. return -1;
  693. return hostapd_ctrl_iface_sta_first(wpa_s->ap_iface->bss[0],
  694. buf, buflen);
  695. }
  696. int ap_ctrl_iface_sta(struct wpa_supplicant *wpa_s, const char *txtaddr,
  697. char *buf, size_t buflen)
  698. {
  699. if (wpa_s->ap_iface == NULL)
  700. return -1;
  701. return hostapd_ctrl_iface_sta(wpa_s->ap_iface->bss[0], txtaddr,
  702. buf, buflen);
  703. }
  704. int ap_ctrl_iface_sta_next(struct wpa_supplicant *wpa_s, const char *txtaddr,
  705. char *buf, size_t buflen)
  706. {
  707. if (wpa_s->ap_iface == NULL)
  708. return -1;
  709. return hostapd_ctrl_iface_sta_next(wpa_s->ap_iface->bss[0], txtaddr,
  710. buf, buflen);
  711. }
  712. int ap_ctrl_iface_wpa_get_status(struct wpa_supplicant *wpa_s, char *buf,
  713. size_t buflen, int verbose)
  714. {
  715. char *pos = buf, *end = buf + buflen;
  716. int ret;
  717. struct hostapd_bss_config *conf;
  718. if (wpa_s->ap_iface == NULL)
  719. return -1;
  720. conf = wpa_s->ap_iface->bss[0]->conf;
  721. if (conf->wpa == 0)
  722. return 0;
  723. ret = os_snprintf(pos, end - pos,
  724. "pairwise_cipher=%s\n"
  725. "group_cipher=%s\n"
  726. "key_mgmt=%s\n",
  727. wpa_cipher_txt(conf->rsn_pairwise),
  728. wpa_cipher_txt(conf->wpa_group),
  729. wpa_key_mgmt_txt(conf->wpa_key_mgmt,
  730. conf->wpa));
  731. if (ret < 0 || ret >= end - pos)
  732. return pos - buf;
  733. pos += ret;
  734. return pos - buf;
  735. }
  736. #endif /* CONFIG_CTRL_IFACE */
  737. int wpa_supplicant_ap_update_beacon(struct wpa_supplicant *wpa_s)
  738. {
  739. struct hostapd_iface *iface = wpa_s->ap_iface;
  740. struct wpa_ssid *ssid = wpa_s->current_ssid;
  741. struct hostapd_data *hapd;
  742. if (ssid == NULL || wpa_s->ap_iface == NULL)
  743. return -1;
  744. #ifdef CONFIG_P2P
  745. if (ssid->mode == WPAS_MODE_P2P_GO)
  746. iface->conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER;
  747. else if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
  748. iface->conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER |
  749. P2P_GROUP_FORMATION;
  750. #endif /* CONFIG_P2P */
  751. ieee802_11_set_beacons(iface);
  752. hapd = iface->bss[0];
  753. hostapd_set_ap_wps_ie(hapd);
  754. return 0;
  755. }
  756. int wpa_supplicant_ap_mac_addr_filter(struct wpa_supplicant *wpa_s,
  757. const u8 *addr)
  758. {
  759. struct hostapd_data *hapd;
  760. struct hostapd_bss_config *conf;
  761. if (!wpa_s->ap_iface)
  762. return -1;
  763. if (addr)
  764. wpa_printf(MSG_DEBUG, "AP: Set MAC address filter: " MACSTR,
  765. MAC2STR(addr));
  766. else
  767. wpa_printf(MSG_DEBUG, "AP: Clear MAC address filter");
  768. hapd = wpa_s->ap_iface->bss[0];
  769. conf = hapd->conf;
  770. os_free(conf->accept_mac);
  771. conf->accept_mac = NULL;
  772. conf->num_accept_mac = 0;
  773. os_free(conf->deny_mac);
  774. conf->deny_mac = NULL;
  775. conf->num_deny_mac = 0;
  776. if (addr == NULL) {
  777. conf->macaddr_acl = ACCEPT_UNLESS_DENIED;
  778. return 0;
  779. }
  780. conf->macaddr_acl = DENY_UNLESS_ACCEPTED;
  781. conf->accept_mac = os_zalloc(sizeof(struct mac_acl_entry));
  782. if (conf->accept_mac == NULL)
  783. return -1;
  784. os_memcpy(conf->accept_mac[0].addr, addr, ETH_ALEN);
  785. conf->num_accept_mac = 1;
  786. return 0;
  787. }