ap.c 24 KB

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