ap.c 27 KB

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