ap.c 24 KB

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