ap.c 27 KB

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