ap.c 24 KB

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