ap.c 27 KB

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