ap.c 24 KB

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