ap.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496
  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 "eapol_supp/eapol_supp_sm.h"
  16. #include "crypto/dh_group5.h"
  17. #include "ap/hostapd.h"
  18. #include "ap/ap_config.h"
  19. #include "ap/ap_drv_ops.h"
  20. #ifdef NEED_AP_MLME
  21. #include "ap/ieee802_11.h"
  22. #endif /* NEED_AP_MLME */
  23. #include "ap/beacon.h"
  24. #include "ap/ieee802_1x.h"
  25. #include "ap/wps_hostapd.h"
  26. #include "ap/ctrl_iface_ap.h"
  27. #include "ap/dfs.h"
  28. #include "wps/wps.h"
  29. #include "common/ieee802_11_defs.h"
  30. #include "config_ssid.h"
  31. #include "config.h"
  32. #include "wpa_supplicant_i.h"
  33. #include "driver_i.h"
  34. #include "p2p_supplicant.h"
  35. #include "ap.h"
  36. #include "ap/sta_info.h"
  37. #include "notify.h"
  38. #ifdef CONFIG_WPS
  39. static void wpas_wps_ap_pin_timeout(void *eloop_data, void *user_ctx);
  40. #endif /* CONFIG_WPS */
  41. #ifdef CONFIG_IEEE80211N
  42. static void wpas_conf_ap_vht(struct wpa_supplicant *wpa_s,
  43. struct hostapd_config *conf,
  44. struct hostapd_hw_modes *mode)
  45. {
  46. #ifdef CONFIG_P2P
  47. u8 center_chan = 0;
  48. u8 channel = conf->channel;
  49. if (!conf->secondary_channel)
  50. goto no_vht;
  51. switch (conf->vht_oper_chwidth) {
  52. case VHT_CHANWIDTH_80MHZ:
  53. case VHT_CHANWIDTH_80P80MHZ:
  54. center_chan = wpas_p2p_get_vht80_center(wpa_s, mode, channel);
  55. break;
  56. case VHT_CHANWIDTH_160MHZ:
  57. center_chan = wpas_p2p_get_vht160_center(wpa_s, mode, channel);
  58. break;
  59. default:
  60. /*
  61. * conf->vht_oper_chwidth might not be set for non-P2P GO cases,
  62. * try oper_cwidth 160 MHz first then VHT 80 MHz, if 160 MHz is
  63. * not supported.
  64. */
  65. conf->vht_oper_chwidth = VHT_CHANWIDTH_160MHZ;
  66. center_chan = wpas_p2p_get_vht160_center(wpa_s, mode, channel);
  67. if (!center_chan) {
  68. conf->vht_oper_chwidth = VHT_CHANWIDTH_80MHZ;
  69. center_chan = wpas_p2p_get_vht80_center(wpa_s, mode,
  70. channel);
  71. }
  72. break;
  73. }
  74. if (!center_chan)
  75. goto no_vht;
  76. conf->vht_oper_centr_freq_seg0_idx = center_chan;
  77. return;
  78. no_vht:
  79. conf->vht_oper_centr_freq_seg0_idx =
  80. channel + conf->secondary_channel * 2;
  81. #else /* CONFIG_P2P */
  82. conf->vht_oper_centr_freq_seg0_idx =
  83. conf->channel + conf->secondary_channel * 2;
  84. #endif /* CONFIG_P2P */
  85. conf->vht_oper_chwidth = VHT_CHANWIDTH_USE_HT;
  86. }
  87. #endif /* CONFIG_IEEE80211N */
  88. int wpa_supplicant_conf_ap_ht(struct wpa_supplicant *wpa_s,
  89. struct wpa_ssid *ssid,
  90. struct hostapd_config *conf)
  91. {
  92. conf->hw_mode = ieee80211_freq_to_chan(ssid->frequency,
  93. &conf->channel);
  94. if (conf->hw_mode == NUM_HOSTAPD_MODES) {
  95. wpa_printf(MSG_ERROR, "Unsupported AP mode frequency: %d MHz",
  96. ssid->frequency);
  97. return -1;
  98. }
  99. /* TODO: enable HT40 if driver supports it;
  100. * drop to 11b if driver does not support 11g */
  101. #ifdef CONFIG_IEEE80211N
  102. /*
  103. * Enable HT20 if the driver supports it, by setting conf->ieee80211n
  104. * and a mask of allowed capabilities within conf->ht_capab.
  105. * Using default config settings for: conf->ht_op_mode_fixed,
  106. * conf->secondary_channel, conf->require_ht
  107. */
  108. if (wpa_s->hw.modes) {
  109. struct hostapd_hw_modes *mode = NULL;
  110. int i, no_ht = 0;
  111. for (i = 0; i < wpa_s->hw.num_modes; i++) {
  112. if (wpa_s->hw.modes[i].mode == conf->hw_mode) {
  113. mode = &wpa_s->hw.modes[i];
  114. break;
  115. }
  116. }
  117. #ifdef CONFIG_HT_OVERRIDES
  118. if (ssid->disable_ht) {
  119. conf->ieee80211n = 0;
  120. conf->ht_capab = 0;
  121. no_ht = 1;
  122. }
  123. #endif /* CONFIG_HT_OVERRIDES */
  124. if (!no_ht && mode && mode->ht_capab) {
  125. conf->ieee80211n = 1;
  126. #ifdef CONFIG_P2P
  127. if (conf->hw_mode == HOSTAPD_MODE_IEEE80211A &&
  128. (mode->ht_capab &
  129. HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) &&
  130. ssid->ht40)
  131. conf->secondary_channel =
  132. wpas_p2p_get_ht40_mode(wpa_s, mode,
  133. conf->channel);
  134. if (conf->secondary_channel)
  135. conf->ht_capab |=
  136. HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
  137. #endif /* CONFIG_P2P */
  138. /*
  139. * white-list capabilities that won't cause issues
  140. * to connecting stations, while leaving the current
  141. * capabilities intact (currently disabled SMPS).
  142. */
  143. conf->ht_capab |= mode->ht_capab &
  144. (HT_CAP_INFO_GREEN_FIELD |
  145. HT_CAP_INFO_SHORT_GI20MHZ |
  146. HT_CAP_INFO_SHORT_GI40MHZ |
  147. HT_CAP_INFO_RX_STBC_MASK |
  148. HT_CAP_INFO_TX_STBC |
  149. HT_CAP_INFO_MAX_AMSDU_SIZE);
  150. if (mode->vht_capab && ssid->vht) {
  151. conf->ieee80211ac = 1;
  152. wpas_conf_ap_vht(wpa_s, conf, mode);
  153. }
  154. }
  155. }
  156. if (conf->secondary_channel) {
  157. struct wpa_supplicant *iface;
  158. for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
  159. {
  160. if (iface == wpa_s ||
  161. iface->wpa_state < WPA_AUTHENTICATING ||
  162. (int) iface->assoc_freq != ssid->frequency)
  163. continue;
  164. /*
  165. * Do not allow 40 MHz co-ex PRI/SEC switch to force us
  166. * to change our PRI channel since we have an existing,
  167. * concurrent connection on that channel and doing
  168. * multi-channel concurrency is likely to cause more
  169. * harm than using different PRI/SEC selection in
  170. * environment with multiple BSSes on these two channels
  171. * with mixed 20 MHz or PRI channel selection.
  172. */
  173. conf->no_pri_sec_switch = 1;
  174. }
  175. }
  176. #endif /* CONFIG_IEEE80211N */
  177. return 0;
  178. }
  179. static int wpa_supplicant_conf_ap(struct wpa_supplicant *wpa_s,
  180. struct wpa_ssid *ssid,
  181. struct hostapd_config *conf)
  182. {
  183. struct hostapd_bss_config *bss = conf->bss[0];
  184. conf->driver = wpa_s->driver;
  185. os_strlcpy(bss->iface, wpa_s->ifname, sizeof(bss->iface));
  186. if (wpa_supplicant_conf_ap_ht(wpa_s, ssid, conf))
  187. return -1;
  188. if (ssid->pbss > 1) {
  189. wpa_printf(MSG_ERROR, "Invalid pbss value(%d) for AP mode",
  190. ssid->pbss);
  191. return -1;
  192. }
  193. bss->pbss = ssid->pbss;
  194. #ifdef CONFIG_ACS
  195. if (ssid->acs) {
  196. /* Setting channel to 0 in order to enable ACS */
  197. conf->channel = 0;
  198. wpa_printf(MSG_DEBUG, "Use automatic channel selection");
  199. }
  200. #endif /* CONFIG_ACS */
  201. if (ieee80211_is_dfs(ssid->frequency) && wpa_s->conf->country[0]) {
  202. conf->ieee80211h = 1;
  203. conf->ieee80211d = 1;
  204. conf->country[0] = wpa_s->conf->country[0];
  205. conf->country[1] = wpa_s->conf->country[1];
  206. }
  207. #ifdef CONFIG_P2P
  208. if (conf->hw_mode == HOSTAPD_MODE_IEEE80211G &&
  209. (ssid->mode == WPAS_MODE_P2P_GO ||
  210. ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)) {
  211. /* Remove 802.11b rates from supported and basic rate sets */
  212. int *list = os_malloc(4 * sizeof(int));
  213. if (list) {
  214. list[0] = 60;
  215. list[1] = 120;
  216. list[2] = 240;
  217. list[3] = -1;
  218. }
  219. conf->basic_rates = list;
  220. list = os_malloc(9 * sizeof(int));
  221. if (list) {
  222. list[0] = 60;
  223. list[1] = 90;
  224. list[2] = 120;
  225. list[3] = 180;
  226. list[4] = 240;
  227. list[5] = 360;
  228. list[6] = 480;
  229. list[7] = 540;
  230. list[8] = -1;
  231. }
  232. conf->supported_rates = list;
  233. }
  234. bss->isolate = !wpa_s->conf->p2p_intra_bss;
  235. bss->force_per_enrollee_psk = wpa_s->global->p2p_per_sta_psk;
  236. if (ssid->p2p_group) {
  237. os_memcpy(bss->ip_addr_go, wpa_s->p2pdev->conf->ip_addr_go, 4);
  238. os_memcpy(bss->ip_addr_mask, wpa_s->p2pdev->conf->ip_addr_mask,
  239. 4);
  240. os_memcpy(bss->ip_addr_start,
  241. wpa_s->p2pdev->conf->ip_addr_start, 4);
  242. os_memcpy(bss->ip_addr_end, wpa_s->p2pdev->conf->ip_addr_end,
  243. 4);
  244. }
  245. #endif /* CONFIG_P2P */
  246. if (ssid->ssid_len == 0) {
  247. wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
  248. return -1;
  249. }
  250. os_memcpy(bss->ssid.ssid, ssid->ssid, ssid->ssid_len);
  251. bss->ssid.ssid_len = ssid->ssid_len;
  252. bss->ssid.ssid_set = 1;
  253. bss->ignore_broadcast_ssid = ssid->ignore_broadcast_ssid;
  254. if (ssid->auth_alg)
  255. bss->auth_algs = ssid->auth_alg;
  256. if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt))
  257. bss->wpa = ssid->proto;
  258. if (ssid->key_mgmt == DEFAULT_KEY_MGMT)
  259. bss->wpa_key_mgmt = WPA_KEY_MGMT_PSK;
  260. else
  261. bss->wpa_key_mgmt = ssid->key_mgmt;
  262. bss->wpa_pairwise = ssid->pairwise_cipher;
  263. if (ssid->psk_set) {
  264. bin_clear_free(bss->ssid.wpa_psk, sizeof(*bss->ssid.wpa_psk));
  265. bss->ssid.wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
  266. if (bss->ssid.wpa_psk == NULL)
  267. return -1;
  268. os_memcpy(bss->ssid.wpa_psk->psk, ssid->psk, PMK_LEN);
  269. bss->ssid.wpa_psk->group = 1;
  270. } else if (ssid->passphrase) {
  271. bss->ssid.wpa_passphrase = os_strdup(ssid->passphrase);
  272. } else if (ssid->wep_key_len[0] || ssid->wep_key_len[1] ||
  273. ssid->wep_key_len[2] || ssid->wep_key_len[3]) {
  274. struct hostapd_wep_keys *wep = &bss->ssid.wep;
  275. int i;
  276. for (i = 0; i < NUM_WEP_KEYS; i++) {
  277. if (ssid->wep_key_len[i] == 0)
  278. continue;
  279. wep->key[i] = os_malloc(ssid->wep_key_len[i]);
  280. if (wep->key[i] == NULL)
  281. return -1;
  282. os_memcpy(wep->key[i], ssid->wep_key[i],
  283. ssid->wep_key_len[i]);
  284. wep->len[i] = ssid->wep_key_len[i];
  285. }
  286. wep->idx = ssid->wep_tx_keyidx;
  287. wep->keys_set = 1;
  288. }
  289. if (ssid->ap_max_inactivity)
  290. bss->ap_max_inactivity = ssid->ap_max_inactivity;
  291. if (ssid->dtim_period)
  292. bss->dtim_period = ssid->dtim_period;
  293. else if (wpa_s->conf->dtim_period)
  294. bss->dtim_period = wpa_s->conf->dtim_period;
  295. if (ssid->beacon_int)
  296. conf->beacon_int = ssid->beacon_int;
  297. else if (wpa_s->conf->beacon_int)
  298. conf->beacon_int = wpa_s->conf->beacon_int;
  299. #ifdef CONFIG_P2P
  300. if (ssid->mode == WPAS_MODE_P2P_GO ||
  301. ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
  302. if (wpa_s->conf->p2p_go_ctwindow > conf->beacon_int) {
  303. wpa_printf(MSG_INFO,
  304. "CTWindow (%d) is bigger than beacon interval (%d) - avoid configuring it",
  305. wpa_s->conf->p2p_go_ctwindow,
  306. conf->beacon_int);
  307. conf->p2p_go_ctwindow = 0;
  308. } else {
  309. conf->p2p_go_ctwindow = wpa_s->conf->p2p_go_ctwindow;
  310. }
  311. }
  312. #endif /* CONFIG_P2P */
  313. if ((bss->wpa & 2) && bss->rsn_pairwise == 0)
  314. bss->rsn_pairwise = bss->wpa_pairwise;
  315. bss->wpa_group = wpa_select_ap_group_cipher(bss->wpa, bss->wpa_pairwise,
  316. bss->rsn_pairwise);
  317. if (bss->wpa && bss->ieee802_1x)
  318. bss->ssid.security_policy = SECURITY_WPA;
  319. else if (bss->wpa)
  320. bss->ssid.security_policy = SECURITY_WPA_PSK;
  321. else if (bss->ieee802_1x) {
  322. int cipher = WPA_CIPHER_NONE;
  323. bss->ssid.security_policy = SECURITY_IEEE_802_1X;
  324. bss->ssid.wep.default_len = bss->default_wep_key_len;
  325. if (bss->default_wep_key_len)
  326. cipher = bss->default_wep_key_len >= 13 ?
  327. WPA_CIPHER_WEP104 : WPA_CIPHER_WEP40;
  328. bss->wpa_group = cipher;
  329. bss->wpa_pairwise = cipher;
  330. bss->rsn_pairwise = cipher;
  331. } else if (bss->ssid.wep.keys_set) {
  332. int cipher = WPA_CIPHER_WEP40;
  333. if (bss->ssid.wep.len[0] >= 13)
  334. cipher = WPA_CIPHER_WEP104;
  335. bss->ssid.security_policy = SECURITY_STATIC_WEP;
  336. bss->wpa_group = cipher;
  337. bss->wpa_pairwise = cipher;
  338. bss->rsn_pairwise = cipher;
  339. } else {
  340. bss->ssid.security_policy = SECURITY_PLAINTEXT;
  341. bss->wpa_group = WPA_CIPHER_NONE;
  342. bss->wpa_pairwise = WPA_CIPHER_NONE;
  343. bss->rsn_pairwise = WPA_CIPHER_NONE;
  344. }
  345. if (bss->wpa_group_rekey < 86400 && (bss->wpa & 2) &&
  346. (bss->wpa_group == WPA_CIPHER_CCMP ||
  347. bss->wpa_group == WPA_CIPHER_GCMP ||
  348. bss->wpa_group == WPA_CIPHER_CCMP_256 ||
  349. bss->wpa_group == WPA_CIPHER_GCMP_256)) {
  350. /*
  351. * Strong ciphers do not need frequent rekeying, so increase
  352. * the default GTK rekeying period to 24 hours.
  353. */
  354. bss->wpa_group_rekey = 86400;
  355. }
  356. #ifdef CONFIG_IEEE80211W
  357. if (ssid->ieee80211w != MGMT_FRAME_PROTECTION_DEFAULT)
  358. bss->ieee80211w = ssid->ieee80211w;
  359. #endif /* CONFIG_IEEE80211W */
  360. #ifdef CONFIG_WPS
  361. /*
  362. * Enable WPS by default for open and WPA/WPA2-Personal network, but
  363. * require user interaction to actually use it. Only the internal
  364. * Registrar is supported.
  365. */
  366. if (bss->ssid.security_policy != SECURITY_WPA_PSK &&
  367. bss->ssid.security_policy != SECURITY_PLAINTEXT)
  368. goto no_wps;
  369. if (bss->ssid.security_policy == SECURITY_WPA_PSK &&
  370. (!(bss->rsn_pairwise & (WPA_CIPHER_CCMP | WPA_CIPHER_GCMP)) ||
  371. !(bss->wpa & 2)))
  372. goto no_wps; /* WPS2 does not allow WPA/TKIP-only
  373. * configuration */
  374. bss->eap_server = 1;
  375. if (!ssid->ignore_broadcast_ssid)
  376. bss->wps_state = 2;
  377. bss->ap_setup_locked = 2;
  378. if (wpa_s->conf->config_methods)
  379. bss->config_methods = os_strdup(wpa_s->conf->config_methods);
  380. os_memcpy(bss->device_type, wpa_s->conf->device_type,
  381. WPS_DEV_TYPE_LEN);
  382. if (wpa_s->conf->device_name) {
  383. bss->device_name = os_strdup(wpa_s->conf->device_name);
  384. bss->friendly_name = os_strdup(wpa_s->conf->device_name);
  385. }
  386. if (wpa_s->conf->manufacturer)
  387. bss->manufacturer = os_strdup(wpa_s->conf->manufacturer);
  388. if (wpa_s->conf->model_name)
  389. bss->model_name = os_strdup(wpa_s->conf->model_name);
  390. if (wpa_s->conf->model_number)
  391. bss->model_number = os_strdup(wpa_s->conf->model_number);
  392. if (wpa_s->conf->serial_number)
  393. bss->serial_number = os_strdup(wpa_s->conf->serial_number);
  394. if (is_nil_uuid(wpa_s->conf->uuid))
  395. os_memcpy(bss->uuid, wpa_s->wps->uuid, WPS_UUID_LEN);
  396. else
  397. os_memcpy(bss->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
  398. os_memcpy(bss->os_version, wpa_s->conf->os_version, 4);
  399. bss->pbc_in_m1 = wpa_s->conf->pbc_in_m1;
  400. no_wps:
  401. #endif /* CONFIG_WPS */
  402. if (wpa_s->max_stations &&
  403. wpa_s->max_stations < wpa_s->conf->max_num_sta)
  404. bss->max_num_sta = wpa_s->max_stations;
  405. else
  406. bss->max_num_sta = wpa_s->conf->max_num_sta;
  407. bss->disassoc_low_ack = wpa_s->conf->disassoc_low_ack;
  408. if (wpa_s->conf->ap_vendor_elements) {
  409. bss->vendor_elements =
  410. wpabuf_dup(wpa_s->conf->ap_vendor_elements);
  411. }
  412. return 0;
  413. }
  414. static void ap_public_action_rx(void *ctx, const u8 *buf, size_t len, int freq)
  415. {
  416. #ifdef CONFIG_P2P
  417. struct wpa_supplicant *wpa_s = ctx;
  418. const struct ieee80211_mgmt *mgmt;
  419. mgmt = (const struct ieee80211_mgmt *) buf;
  420. if (len < IEEE80211_HDRLEN + 1)
  421. return;
  422. if (mgmt->u.action.category != WLAN_ACTION_PUBLIC)
  423. return;
  424. wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
  425. mgmt->u.action.category,
  426. buf + IEEE80211_HDRLEN + 1,
  427. len - IEEE80211_HDRLEN - 1, freq);
  428. #endif /* CONFIG_P2P */
  429. }
  430. static void ap_wps_event_cb(void *ctx, enum wps_event event,
  431. union wps_event_data *data)
  432. {
  433. #ifdef CONFIG_P2P
  434. struct wpa_supplicant *wpa_s = ctx;
  435. if (event == WPS_EV_FAIL) {
  436. struct wps_event_fail *fail = &data->fail;
  437. if (wpa_s->p2pdev && wpa_s->p2pdev != wpa_s &&
  438. wpa_s == wpa_s->global->p2p_group_formation) {
  439. /*
  440. * src/ap/wps_hostapd.c has already sent this on the
  441. * main interface, so only send on the parent interface
  442. * here if needed.
  443. */
  444. wpa_msg(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_FAIL
  445. "msg=%d config_error=%d",
  446. fail->msg, fail->config_error);
  447. }
  448. wpas_p2p_wps_failed(wpa_s, fail);
  449. }
  450. #endif /* CONFIG_P2P */
  451. }
  452. static void ap_sta_authorized_cb(void *ctx, const u8 *mac_addr,
  453. int authorized, const u8 *p2p_dev_addr)
  454. {
  455. wpas_notify_sta_authorized(ctx, mac_addr, authorized, p2p_dev_addr);
  456. }
  457. #ifdef CONFIG_P2P
  458. static void ap_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *p2p_dev_addr,
  459. const u8 *psk, size_t psk_len)
  460. {
  461. struct wpa_supplicant *wpa_s = ctx;
  462. if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL)
  463. return;
  464. wpas_p2p_new_psk_cb(wpa_s, mac_addr, p2p_dev_addr, psk, psk_len);
  465. }
  466. #endif /* CONFIG_P2P */
  467. static int ap_vendor_action_rx(void *ctx, const u8 *buf, size_t len, int freq)
  468. {
  469. #ifdef CONFIG_P2P
  470. struct wpa_supplicant *wpa_s = ctx;
  471. const struct ieee80211_mgmt *mgmt;
  472. mgmt = (const struct ieee80211_mgmt *) buf;
  473. if (len < IEEE80211_HDRLEN + 1)
  474. return -1;
  475. wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
  476. mgmt->u.action.category,
  477. buf + IEEE80211_HDRLEN + 1,
  478. len - IEEE80211_HDRLEN - 1, freq);
  479. #endif /* CONFIG_P2P */
  480. return 0;
  481. }
  482. static int ap_probe_req_rx(void *ctx, const u8 *sa, const u8 *da,
  483. const u8 *bssid, const u8 *ie, size_t ie_len,
  484. int ssi_signal)
  485. {
  486. struct wpa_supplicant *wpa_s = ctx;
  487. unsigned int freq = 0;
  488. if (wpa_s->ap_iface)
  489. freq = wpa_s->ap_iface->freq;
  490. return wpas_p2p_probe_req_rx(wpa_s, sa, da, bssid, ie, ie_len,
  491. freq, ssi_signal);
  492. }
  493. static void ap_wps_reg_success_cb(void *ctx, const u8 *mac_addr,
  494. const u8 *uuid_e)
  495. {
  496. struct wpa_supplicant *wpa_s = ctx;
  497. wpas_p2p_wps_success(wpa_s, mac_addr, 1);
  498. }
  499. static void wpas_ap_configured_cb(void *ctx)
  500. {
  501. struct wpa_supplicant *wpa_s = ctx;
  502. #ifdef CONFIG_ACS
  503. if (wpa_s->current_ssid && wpa_s->current_ssid->acs)
  504. wpa_s->assoc_freq = wpa_s->ap_iface->freq;
  505. #endif /* CONFIG_ACS */
  506. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  507. if (wpa_s->ap_configured_cb)
  508. wpa_s->ap_configured_cb(wpa_s->ap_configured_cb_ctx,
  509. wpa_s->ap_configured_cb_data);
  510. }
  511. int wpa_supplicant_create_ap(struct wpa_supplicant *wpa_s,
  512. struct wpa_ssid *ssid)
  513. {
  514. struct wpa_driver_associate_params params;
  515. struct hostapd_iface *hapd_iface;
  516. struct hostapd_config *conf;
  517. size_t i;
  518. if (ssid->ssid == NULL || ssid->ssid_len == 0) {
  519. wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
  520. return -1;
  521. }
  522. wpa_supplicant_ap_deinit(wpa_s);
  523. wpa_printf(MSG_DEBUG, "Setting up AP (SSID='%s')",
  524. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  525. os_memset(&params, 0, sizeof(params));
  526. params.ssid = ssid->ssid;
  527. params.ssid_len = ssid->ssid_len;
  528. switch (ssid->mode) {
  529. case WPAS_MODE_AP:
  530. case WPAS_MODE_P2P_GO:
  531. case WPAS_MODE_P2P_GROUP_FORMATION:
  532. params.mode = IEEE80211_MODE_AP;
  533. break;
  534. default:
  535. return -1;
  536. }
  537. if (ssid->frequency == 0)
  538. ssid->frequency = 2462; /* default channel 11 */
  539. params.freq.freq = ssid->frequency;
  540. params.wpa_proto = ssid->proto;
  541. if (ssid->key_mgmt & WPA_KEY_MGMT_PSK)
  542. wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
  543. else
  544. wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
  545. params.key_mgmt_suite = wpa_s->key_mgmt;
  546. wpa_s->pairwise_cipher = wpa_pick_pairwise_cipher(ssid->pairwise_cipher,
  547. 1);
  548. if (wpa_s->pairwise_cipher < 0) {
  549. wpa_printf(MSG_WARNING, "WPA: Failed to select pairwise "
  550. "cipher.");
  551. return -1;
  552. }
  553. params.pairwise_suite = wpa_s->pairwise_cipher;
  554. params.group_suite = params.pairwise_suite;
  555. #ifdef CONFIG_P2P
  556. if (ssid->mode == WPAS_MODE_P2P_GO ||
  557. ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
  558. params.p2p = 1;
  559. #endif /* CONFIG_P2P */
  560. if (wpa_s->p2pdev->set_ap_uapsd)
  561. params.uapsd = wpa_s->p2pdev->ap_uapsd;
  562. else if (params.p2p && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_UAPSD))
  563. params.uapsd = 1; /* mandatory for P2P GO */
  564. else
  565. params.uapsd = -1;
  566. if (ieee80211_is_dfs(params.freq.freq))
  567. params.freq.freq = 0; /* set channel after CAC */
  568. if (wpa_drv_associate(wpa_s, &params) < 0) {
  569. wpa_msg(wpa_s, MSG_INFO, "Failed to start AP functionality");
  570. return -1;
  571. }
  572. wpa_s->ap_iface = hapd_iface = os_zalloc(sizeof(*wpa_s->ap_iface));
  573. if (hapd_iface == NULL)
  574. return -1;
  575. hapd_iface->owner = wpa_s;
  576. hapd_iface->drv_flags = wpa_s->drv_flags;
  577. hapd_iface->smps_modes = wpa_s->drv_smps_modes;
  578. hapd_iface->probe_resp_offloads = wpa_s->probe_resp_offloads;
  579. hapd_iface->extended_capa = wpa_s->extended_capa;
  580. hapd_iface->extended_capa_mask = wpa_s->extended_capa_mask;
  581. hapd_iface->extended_capa_len = wpa_s->extended_capa_len;
  582. wpa_s->ap_iface->conf = conf = hostapd_config_defaults();
  583. if (conf == NULL) {
  584. wpa_supplicant_ap_deinit(wpa_s);
  585. return -1;
  586. }
  587. /* Use the maximum oper channel width if it's given. */
  588. if (ssid->max_oper_chwidth)
  589. conf->vht_oper_chwidth = ssid->max_oper_chwidth;
  590. ieee80211_freq_to_chan(ssid->vht_center_freq2,
  591. &conf->vht_oper_centr_freq_seg1_idx);
  592. os_memcpy(wpa_s->ap_iface->conf->wmm_ac_params,
  593. wpa_s->conf->wmm_ac_params,
  594. sizeof(wpa_s->conf->wmm_ac_params));
  595. if (params.uapsd > 0) {
  596. conf->bss[0]->wmm_enabled = 1;
  597. conf->bss[0]->wmm_uapsd = 1;
  598. }
  599. if (wpa_supplicant_conf_ap(wpa_s, ssid, conf)) {
  600. wpa_printf(MSG_ERROR, "Failed to create AP configuration");
  601. wpa_supplicant_ap_deinit(wpa_s);
  602. return -1;
  603. }
  604. #ifdef CONFIG_P2P
  605. if (ssid->mode == WPAS_MODE_P2P_GO)
  606. conf->bss[0]->p2p = P2P_ENABLED | P2P_GROUP_OWNER;
  607. else if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
  608. conf->bss[0]->p2p = P2P_ENABLED | P2P_GROUP_OWNER |
  609. P2P_GROUP_FORMATION;
  610. #endif /* CONFIG_P2P */
  611. hapd_iface->num_bss = conf->num_bss;
  612. hapd_iface->bss = os_calloc(conf->num_bss,
  613. sizeof(struct hostapd_data *));
  614. if (hapd_iface->bss == NULL) {
  615. wpa_supplicant_ap_deinit(wpa_s);
  616. return -1;
  617. }
  618. for (i = 0; i < conf->num_bss; i++) {
  619. hapd_iface->bss[i] =
  620. hostapd_alloc_bss_data(hapd_iface, conf,
  621. conf->bss[i]);
  622. if (hapd_iface->bss[i] == NULL) {
  623. wpa_supplicant_ap_deinit(wpa_s);
  624. return -1;
  625. }
  626. hapd_iface->bss[i]->msg_ctx = wpa_s;
  627. hapd_iface->bss[i]->msg_ctx_parent = wpa_s->p2pdev;
  628. hapd_iface->bss[i]->public_action_cb = ap_public_action_rx;
  629. hapd_iface->bss[i]->public_action_cb_ctx = wpa_s;
  630. hapd_iface->bss[i]->vendor_action_cb = ap_vendor_action_rx;
  631. hapd_iface->bss[i]->vendor_action_cb_ctx = wpa_s;
  632. hostapd_register_probereq_cb(hapd_iface->bss[i],
  633. ap_probe_req_rx, wpa_s);
  634. hapd_iface->bss[i]->wps_reg_success_cb = ap_wps_reg_success_cb;
  635. hapd_iface->bss[i]->wps_reg_success_cb_ctx = wpa_s;
  636. hapd_iface->bss[i]->wps_event_cb = ap_wps_event_cb;
  637. hapd_iface->bss[i]->wps_event_cb_ctx = wpa_s;
  638. hapd_iface->bss[i]->sta_authorized_cb = ap_sta_authorized_cb;
  639. hapd_iface->bss[i]->sta_authorized_cb_ctx = wpa_s;
  640. #ifdef CONFIG_P2P
  641. hapd_iface->bss[i]->new_psk_cb = ap_new_psk_cb;
  642. hapd_iface->bss[i]->new_psk_cb_ctx = wpa_s;
  643. hapd_iface->bss[i]->p2p = wpa_s->global->p2p;
  644. hapd_iface->bss[i]->p2p_group = wpas_p2p_group_init(wpa_s,
  645. ssid);
  646. #endif /* CONFIG_P2P */
  647. hapd_iface->bss[i]->setup_complete_cb = wpas_ap_configured_cb;
  648. hapd_iface->bss[i]->setup_complete_cb_ctx = wpa_s;
  649. #ifdef CONFIG_TESTING_OPTIONS
  650. hapd_iface->bss[i]->ext_eapol_frame_io =
  651. wpa_s->ext_eapol_frame_io;
  652. #endif /* CONFIG_TESTING_OPTIONS */
  653. }
  654. os_memcpy(hapd_iface->bss[0]->own_addr, wpa_s->own_addr, ETH_ALEN);
  655. hapd_iface->bss[0]->driver = wpa_s->driver;
  656. hapd_iface->bss[0]->drv_priv = wpa_s->drv_priv;
  657. wpa_s->current_ssid = ssid;
  658. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  659. os_memcpy(wpa_s->bssid, wpa_s->own_addr, ETH_ALEN);
  660. wpa_s->assoc_freq = ssid->frequency;
  661. if (hostapd_setup_interface(wpa_s->ap_iface)) {
  662. wpa_printf(MSG_ERROR, "Failed to initialize AP interface");
  663. wpa_supplicant_ap_deinit(wpa_s);
  664. return -1;
  665. }
  666. return 0;
  667. }
  668. void wpa_supplicant_ap_deinit(struct wpa_supplicant *wpa_s)
  669. {
  670. #ifdef CONFIG_WPS
  671. eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
  672. #endif /* CONFIG_WPS */
  673. if (wpa_s->ap_iface == NULL)
  674. return;
  675. wpa_s->current_ssid = NULL;
  676. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  677. wpa_s->assoc_freq = 0;
  678. wpas_p2p_ap_deinit(wpa_s);
  679. wpa_s->ap_iface->driver_ap_teardown =
  680. !!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
  681. hostapd_interface_deinit(wpa_s->ap_iface);
  682. hostapd_interface_free(wpa_s->ap_iface);
  683. wpa_s->ap_iface = NULL;
  684. wpa_drv_deinit_ap(wpa_s);
  685. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
  686. " reason=%d locally_generated=1",
  687. MAC2STR(wpa_s->own_addr), WLAN_REASON_DEAUTH_LEAVING);
  688. }
  689. void ap_tx_status(void *ctx, const u8 *addr,
  690. const u8 *buf, size_t len, int ack)
  691. {
  692. #ifdef NEED_AP_MLME
  693. struct wpa_supplicant *wpa_s = ctx;
  694. hostapd_tx_status(wpa_s->ap_iface->bss[0], addr, buf, len, ack);
  695. #endif /* NEED_AP_MLME */
  696. }
  697. void ap_eapol_tx_status(void *ctx, const u8 *dst,
  698. const u8 *data, size_t len, int ack)
  699. {
  700. #ifdef NEED_AP_MLME
  701. struct wpa_supplicant *wpa_s = ctx;
  702. if (!wpa_s->ap_iface)
  703. return;
  704. hostapd_tx_status(wpa_s->ap_iface->bss[0], dst, data, len, ack);
  705. #endif /* NEED_AP_MLME */
  706. }
  707. void ap_client_poll_ok(void *ctx, const u8 *addr)
  708. {
  709. #ifdef NEED_AP_MLME
  710. struct wpa_supplicant *wpa_s = ctx;
  711. if (wpa_s->ap_iface)
  712. hostapd_client_poll_ok(wpa_s->ap_iface->bss[0], addr);
  713. #endif /* NEED_AP_MLME */
  714. }
  715. void ap_rx_from_unknown_sta(void *ctx, const u8 *addr, int wds)
  716. {
  717. #ifdef NEED_AP_MLME
  718. struct wpa_supplicant *wpa_s = ctx;
  719. ieee802_11_rx_from_unknown(wpa_s->ap_iface->bss[0], addr, wds);
  720. #endif /* NEED_AP_MLME */
  721. }
  722. void ap_mgmt_rx(void *ctx, struct rx_mgmt *rx_mgmt)
  723. {
  724. #ifdef NEED_AP_MLME
  725. struct wpa_supplicant *wpa_s = ctx;
  726. struct hostapd_frame_info fi;
  727. os_memset(&fi, 0, sizeof(fi));
  728. fi.datarate = rx_mgmt->datarate;
  729. fi.ssi_signal = rx_mgmt->ssi_signal;
  730. ieee802_11_mgmt(wpa_s->ap_iface->bss[0], rx_mgmt->frame,
  731. rx_mgmt->frame_len, &fi);
  732. #endif /* NEED_AP_MLME */
  733. }
  734. void ap_mgmt_tx_cb(void *ctx, const u8 *buf, size_t len, u16 stype, int ok)
  735. {
  736. #ifdef NEED_AP_MLME
  737. struct wpa_supplicant *wpa_s = ctx;
  738. ieee802_11_mgmt_cb(wpa_s->ap_iface->bss[0], buf, len, stype, ok);
  739. #endif /* NEED_AP_MLME */
  740. }
  741. void wpa_supplicant_ap_rx_eapol(struct wpa_supplicant *wpa_s,
  742. const u8 *src_addr, const u8 *buf, size_t len)
  743. {
  744. ieee802_1x_receive(wpa_s->ap_iface->bss[0], src_addr, buf, len);
  745. }
  746. #ifdef CONFIG_WPS
  747. int wpa_supplicant_ap_wps_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid,
  748. const u8 *p2p_dev_addr)
  749. {
  750. if (!wpa_s->ap_iface)
  751. return -1;
  752. return hostapd_wps_button_pushed(wpa_s->ap_iface->bss[0],
  753. p2p_dev_addr);
  754. }
  755. int wpa_supplicant_ap_wps_cancel(struct wpa_supplicant *wpa_s)
  756. {
  757. struct wps_registrar *reg;
  758. int reg_sel = 0, wps_sta = 0;
  759. if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0]->wps)
  760. return -1;
  761. reg = wpa_s->ap_iface->bss[0]->wps->registrar;
  762. reg_sel = wps_registrar_wps_cancel(reg);
  763. wps_sta = ap_for_each_sta(wpa_s->ap_iface->bss[0],
  764. ap_sta_wps_cancel, NULL);
  765. if (!reg_sel && !wps_sta) {
  766. wpa_printf(MSG_DEBUG, "No WPS operation in progress at this "
  767. "time");
  768. return -1;
  769. }
  770. /*
  771. * There are 2 cases to return wps cancel as success:
  772. * 1. When wps cancel was initiated but no connection has been
  773. * established with client yet.
  774. * 2. Client is in the middle of exchanging WPS messages.
  775. */
  776. return 0;
  777. }
  778. int wpa_supplicant_ap_wps_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
  779. const char *pin, char *buf, size_t buflen,
  780. int timeout)
  781. {
  782. int ret, ret_len = 0;
  783. if (!wpa_s->ap_iface)
  784. return -1;
  785. if (pin == NULL) {
  786. unsigned int rpin;
  787. if (wps_generate_pin(&rpin) < 0)
  788. return -1;
  789. ret_len = os_snprintf(buf, buflen, "%08d", rpin);
  790. if (os_snprintf_error(buflen, ret_len))
  791. return -1;
  792. pin = buf;
  793. } else if (buf) {
  794. ret_len = os_snprintf(buf, buflen, "%s", pin);
  795. if (os_snprintf_error(buflen, ret_len))
  796. return -1;
  797. }
  798. ret = hostapd_wps_add_pin(wpa_s->ap_iface->bss[0], bssid, "any", pin,
  799. timeout);
  800. if (ret)
  801. return -1;
  802. return ret_len;
  803. }
  804. static void wpas_wps_ap_pin_timeout(void *eloop_data, void *user_ctx)
  805. {
  806. struct wpa_supplicant *wpa_s = eloop_data;
  807. wpa_printf(MSG_DEBUG, "WPS: AP PIN timed out");
  808. wpas_wps_ap_pin_disable(wpa_s);
  809. }
  810. static void wpas_wps_ap_pin_enable(struct wpa_supplicant *wpa_s, int timeout)
  811. {
  812. struct hostapd_data *hapd;
  813. if (wpa_s->ap_iface == NULL)
  814. return;
  815. hapd = wpa_s->ap_iface->bss[0];
  816. wpa_printf(MSG_DEBUG, "WPS: Enabling AP PIN (timeout=%d)", timeout);
  817. hapd->ap_pin_failures = 0;
  818. eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
  819. if (timeout > 0)
  820. eloop_register_timeout(timeout, 0,
  821. wpas_wps_ap_pin_timeout, wpa_s, NULL);
  822. }
  823. void wpas_wps_ap_pin_disable(struct wpa_supplicant *wpa_s)
  824. {
  825. struct hostapd_data *hapd;
  826. if (wpa_s->ap_iface == NULL)
  827. return;
  828. wpa_printf(MSG_DEBUG, "WPS: Disabling AP PIN");
  829. hapd = wpa_s->ap_iface->bss[0];
  830. os_free(hapd->conf->ap_pin);
  831. hapd->conf->ap_pin = NULL;
  832. eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
  833. }
  834. const char * wpas_wps_ap_pin_random(struct wpa_supplicant *wpa_s, int timeout)
  835. {
  836. struct hostapd_data *hapd;
  837. unsigned int pin;
  838. char pin_txt[9];
  839. if (wpa_s->ap_iface == NULL)
  840. return NULL;
  841. hapd = wpa_s->ap_iface->bss[0];
  842. if (wps_generate_pin(&pin) < 0)
  843. return NULL;
  844. os_snprintf(pin_txt, sizeof(pin_txt), "%08u", pin);
  845. os_free(hapd->conf->ap_pin);
  846. hapd->conf->ap_pin = os_strdup(pin_txt);
  847. if (hapd->conf->ap_pin == NULL)
  848. return NULL;
  849. wpas_wps_ap_pin_enable(wpa_s, timeout);
  850. return hapd->conf->ap_pin;
  851. }
  852. const char * wpas_wps_ap_pin_get(struct wpa_supplicant *wpa_s)
  853. {
  854. struct hostapd_data *hapd;
  855. if (wpa_s->ap_iface == NULL)
  856. return NULL;
  857. hapd = wpa_s->ap_iface->bss[0];
  858. return hapd->conf->ap_pin;
  859. }
  860. int wpas_wps_ap_pin_set(struct wpa_supplicant *wpa_s, const char *pin,
  861. int timeout)
  862. {
  863. struct hostapd_data *hapd;
  864. char pin_txt[9];
  865. int ret;
  866. if (wpa_s->ap_iface == NULL)
  867. return -1;
  868. hapd = wpa_s->ap_iface->bss[0];
  869. ret = os_snprintf(pin_txt, sizeof(pin_txt), "%s", pin);
  870. if (os_snprintf_error(sizeof(pin_txt), ret))
  871. return -1;
  872. os_free(hapd->conf->ap_pin);
  873. hapd->conf->ap_pin = os_strdup(pin_txt);
  874. if (hapd->conf->ap_pin == NULL)
  875. return -1;
  876. wpas_wps_ap_pin_enable(wpa_s, timeout);
  877. return 0;
  878. }
  879. void wpa_supplicant_ap_pwd_auth_fail(struct wpa_supplicant *wpa_s)
  880. {
  881. struct hostapd_data *hapd;
  882. if (wpa_s->ap_iface == NULL)
  883. return;
  884. hapd = wpa_s->ap_iface->bss[0];
  885. /*
  886. * Registrar failed to prove its knowledge of the AP PIN. Disable AP
  887. * PIN if this happens multiple times to slow down brute force attacks.
  888. */
  889. hapd->ap_pin_failures++;
  890. wpa_printf(MSG_DEBUG, "WPS: AP PIN authentication failure number %u",
  891. hapd->ap_pin_failures);
  892. if (hapd->ap_pin_failures < 3)
  893. return;
  894. wpa_printf(MSG_DEBUG, "WPS: Disable AP PIN");
  895. hapd->ap_pin_failures = 0;
  896. os_free(hapd->conf->ap_pin);
  897. hapd->conf->ap_pin = NULL;
  898. }
  899. #ifdef CONFIG_WPS_NFC
  900. struct wpabuf * wpas_ap_wps_nfc_config_token(struct wpa_supplicant *wpa_s,
  901. int ndef)
  902. {
  903. struct hostapd_data *hapd;
  904. if (wpa_s->ap_iface == NULL)
  905. return NULL;
  906. hapd = wpa_s->ap_iface->bss[0];
  907. return hostapd_wps_nfc_config_token(hapd, ndef);
  908. }
  909. struct wpabuf * wpas_ap_wps_nfc_handover_sel(struct wpa_supplicant *wpa_s,
  910. int ndef)
  911. {
  912. struct hostapd_data *hapd;
  913. if (wpa_s->ap_iface == NULL)
  914. return NULL;
  915. hapd = wpa_s->ap_iface->bss[0];
  916. return hostapd_wps_nfc_hs_cr(hapd, ndef);
  917. }
  918. int wpas_ap_wps_nfc_report_handover(struct wpa_supplicant *wpa_s,
  919. const struct wpabuf *req,
  920. const struct wpabuf *sel)
  921. {
  922. struct hostapd_data *hapd;
  923. if (wpa_s->ap_iface == NULL)
  924. return -1;
  925. hapd = wpa_s->ap_iface->bss[0];
  926. return hostapd_wps_nfc_report_handover(hapd, req, sel);
  927. }
  928. #endif /* CONFIG_WPS_NFC */
  929. #endif /* CONFIG_WPS */
  930. #ifdef CONFIG_CTRL_IFACE
  931. int ap_ctrl_iface_sta_first(struct wpa_supplicant *wpa_s,
  932. char *buf, size_t buflen)
  933. {
  934. struct hostapd_data *hapd;
  935. if (wpa_s->ap_iface)
  936. hapd = wpa_s->ap_iface->bss[0];
  937. else if (wpa_s->ifmsh)
  938. hapd = wpa_s->ifmsh->bss[0];
  939. else
  940. return -1;
  941. return hostapd_ctrl_iface_sta_first(hapd, buf, buflen);
  942. }
  943. int ap_ctrl_iface_sta(struct wpa_supplicant *wpa_s, const char *txtaddr,
  944. char *buf, size_t buflen)
  945. {
  946. struct hostapd_data *hapd;
  947. if (wpa_s->ap_iface)
  948. hapd = wpa_s->ap_iface->bss[0];
  949. else if (wpa_s->ifmsh)
  950. hapd = wpa_s->ifmsh->bss[0];
  951. else
  952. return -1;
  953. return hostapd_ctrl_iface_sta(hapd, txtaddr, buf, buflen);
  954. }
  955. int ap_ctrl_iface_sta_next(struct wpa_supplicant *wpa_s, const char *txtaddr,
  956. char *buf, size_t buflen)
  957. {
  958. struct hostapd_data *hapd;
  959. if (wpa_s->ap_iface)
  960. hapd = wpa_s->ap_iface->bss[0];
  961. else if (wpa_s->ifmsh)
  962. hapd = wpa_s->ifmsh->bss[0];
  963. else
  964. return -1;
  965. return hostapd_ctrl_iface_sta_next(hapd, txtaddr, buf, buflen);
  966. }
  967. int ap_ctrl_iface_sta_disassociate(struct wpa_supplicant *wpa_s,
  968. const char *txtaddr)
  969. {
  970. if (wpa_s->ap_iface == NULL)
  971. return -1;
  972. return hostapd_ctrl_iface_disassociate(wpa_s->ap_iface->bss[0],
  973. txtaddr);
  974. }
  975. int ap_ctrl_iface_sta_deauthenticate(struct wpa_supplicant *wpa_s,
  976. const char *txtaddr)
  977. {
  978. if (wpa_s->ap_iface == NULL)
  979. return -1;
  980. return hostapd_ctrl_iface_deauthenticate(wpa_s->ap_iface->bss[0],
  981. txtaddr);
  982. }
  983. int ap_ctrl_iface_wpa_get_status(struct wpa_supplicant *wpa_s, char *buf,
  984. size_t buflen, int verbose)
  985. {
  986. char *pos = buf, *end = buf + buflen;
  987. int ret;
  988. struct hostapd_bss_config *conf;
  989. if (wpa_s->ap_iface == NULL)
  990. return -1;
  991. conf = wpa_s->ap_iface->bss[0]->conf;
  992. if (conf->wpa == 0)
  993. return 0;
  994. ret = os_snprintf(pos, end - pos,
  995. "pairwise_cipher=%s\n"
  996. "group_cipher=%s\n"
  997. "key_mgmt=%s\n",
  998. wpa_cipher_txt(conf->rsn_pairwise),
  999. wpa_cipher_txt(conf->wpa_group),
  1000. wpa_key_mgmt_txt(conf->wpa_key_mgmt,
  1001. conf->wpa));
  1002. if (os_snprintf_error(end - pos, ret))
  1003. return pos - buf;
  1004. pos += ret;
  1005. return pos - buf;
  1006. }
  1007. #endif /* CONFIG_CTRL_IFACE */
  1008. int wpa_supplicant_ap_update_beacon(struct wpa_supplicant *wpa_s)
  1009. {
  1010. struct hostapd_iface *iface = wpa_s->ap_iface;
  1011. struct wpa_ssid *ssid = wpa_s->current_ssid;
  1012. struct hostapd_data *hapd;
  1013. if (ssid == NULL || wpa_s->ap_iface == NULL ||
  1014. ssid->mode == WPAS_MODE_INFRA ||
  1015. ssid->mode == WPAS_MODE_IBSS)
  1016. return -1;
  1017. #ifdef CONFIG_P2P
  1018. if (ssid->mode == WPAS_MODE_P2P_GO)
  1019. iface->conf->bss[0]->p2p = P2P_ENABLED | P2P_GROUP_OWNER;
  1020. else if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
  1021. iface->conf->bss[0]->p2p = P2P_ENABLED | P2P_GROUP_OWNER |
  1022. P2P_GROUP_FORMATION;
  1023. #endif /* CONFIG_P2P */
  1024. hapd = iface->bss[0];
  1025. if (hapd->drv_priv == NULL)
  1026. return -1;
  1027. ieee802_11_set_beacons(iface);
  1028. hostapd_set_ap_wps_ie(hapd);
  1029. return 0;
  1030. }
  1031. int ap_switch_channel(struct wpa_supplicant *wpa_s,
  1032. struct csa_settings *settings)
  1033. {
  1034. #ifdef NEED_AP_MLME
  1035. if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0])
  1036. return -1;
  1037. return hostapd_switch_channel(wpa_s->ap_iface->bss[0], settings);
  1038. #else /* NEED_AP_MLME */
  1039. return -1;
  1040. #endif /* NEED_AP_MLME */
  1041. }
  1042. #ifdef CONFIG_CTRL_IFACE
  1043. int ap_ctrl_iface_chanswitch(struct wpa_supplicant *wpa_s, const char *pos)
  1044. {
  1045. struct csa_settings settings;
  1046. int ret = hostapd_parse_csa_settings(pos, &settings);
  1047. if (ret)
  1048. return ret;
  1049. return ap_switch_channel(wpa_s, &settings);
  1050. }
  1051. #endif /* CONFIG_CTRL_IFACE */
  1052. void wpas_ap_ch_switch(struct wpa_supplicant *wpa_s, int freq, int ht,
  1053. int offset, int width, int cf1, int cf2)
  1054. {
  1055. if (!wpa_s->ap_iface)
  1056. return;
  1057. wpa_s->assoc_freq = freq;
  1058. if (wpa_s->current_ssid)
  1059. wpa_s->current_ssid->frequency = freq;
  1060. hostapd_event_ch_switch(wpa_s->ap_iface->bss[0], freq, ht,
  1061. offset, width, cf1, cf2);
  1062. }
  1063. int wpa_supplicant_ap_mac_addr_filter(struct wpa_supplicant *wpa_s,
  1064. const u8 *addr)
  1065. {
  1066. struct hostapd_data *hapd;
  1067. struct hostapd_bss_config *conf;
  1068. if (!wpa_s->ap_iface)
  1069. return -1;
  1070. if (addr)
  1071. wpa_printf(MSG_DEBUG, "AP: Set MAC address filter: " MACSTR,
  1072. MAC2STR(addr));
  1073. else
  1074. wpa_printf(MSG_DEBUG, "AP: Clear MAC address filter");
  1075. hapd = wpa_s->ap_iface->bss[0];
  1076. conf = hapd->conf;
  1077. os_free(conf->accept_mac);
  1078. conf->accept_mac = NULL;
  1079. conf->num_accept_mac = 0;
  1080. os_free(conf->deny_mac);
  1081. conf->deny_mac = NULL;
  1082. conf->num_deny_mac = 0;
  1083. if (addr == NULL) {
  1084. conf->macaddr_acl = ACCEPT_UNLESS_DENIED;
  1085. return 0;
  1086. }
  1087. conf->macaddr_acl = DENY_UNLESS_ACCEPTED;
  1088. conf->accept_mac = os_zalloc(sizeof(struct mac_acl_entry));
  1089. if (conf->accept_mac == NULL)
  1090. return -1;
  1091. os_memcpy(conf->accept_mac[0].addr, addr, ETH_ALEN);
  1092. conf->num_accept_mac = 1;
  1093. return 0;
  1094. }
  1095. #ifdef CONFIG_WPS_NFC
  1096. int wpas_ap_wps_add_nfc_pw(struct wpa_supplicant *wpa_s, u16 pw_id,
  1097. const struct wpabuf *pw, const u8 *pubkey_hash)
  1098. {
  1099. struct hostapd_data *hapd;
  1100. struct wps_context *wps;
  1101. if (!wpa_s->ap_iface)
  1102. return -1;
  1103. hapd = wpa_s->ap_iface->bss[0];
  1104. wps = hapd->wps;
  1105. if (wpa_s->p2pdev->conf->wps_nfc_dh_pubkey == NULL ||
  1106. wpa_s->p2pdev->conf->wps_nfc_dh_privkey == NULL) {
  1107. wpa_printf(MSG_DEBUG, "P2P: No NFC DH key known");
  1108. return -1;
  1109. }
  1110. dh5_free(wps->dh_ctx);
  1111. wpabuf_free(wps->dh_pubkey);
  1112. wpabuf_free(wps->dh_privkey);
  1113. wps->dh_privkey = wpabuf_dup(
  1114. wpa_s->p2pdev->conf->wps_nfc_dh_privkey);
  1115. wps->dh_pubkey = wpabuf_dup(
  1116. wpa_s->p2pdev->conf->wps_nfc_dh_pubkey);
  1117. if (wps->dh_privkey == NULL || wps->dh_pubkey == NULL) {
  1118. wps->dh_ctx = NULL;
  1119. wpabuf_free(wps->dh_pubkey);
  1120. wps->dh_pubkey = NULL;
  1121. wpabuf_free(wps->dh_privkey);
  1122. wps->dh_privkey = NULL;
  1123. return -1;
  1124. }
  1125. wps->dh_ctx = dh5_init_fixed(wps->dh_privkey, wps->dh_pubkey);
  1126. if (wps->dh_ctx == NULL)
  1127. return -1;
  1128. return wps_registrar_add_nfc_pw_token(hapd->wps->registrar, pubkey_hash,
  1129. pw_id,
  1130. pw ? wpabuf_head(pw) : NULL,
  1131. pw ? wpabuf_len(pw) : 0, 1);
  1132. }
  1133. #endif /* CONFIG_WPS_NFC */
  1134. #ifdef CONFIG_CTRL_IFACE
  1135. int wpas_ap_stop_ap(struct wpa_supplicant *wpa_s)
  1136. {
  1137. struct hostapd_data *hapd;
  1138. if (!wpa_s->ap_iface)
  1139. return -1;
  1140. hapd = wpa_s->ap_iface->bss[0];
  1141. return hostapd_ctrl_iface_stop_ap(hapd);
  1142. }
  1143. int wpas_ap_pmksa_cache_list(struct wpa_supplicant *wpa_s, char *buf,
  1144. size_t len)
  1145. {
  1146. size_t reply_len = 0, i;
  1147. char ap_delimiter[] = "---- AP ----\n";
  1148. char mesh_delimiter[] = "---- mesh ----\n";
  1149. size_t dlen;
  1150. if (wpa_s->ap_iface) {
  1151. dlen = os_strlen(ap_delimiter);
  1152. if (dlen > len - reply_len)
  1153. return reply_len;
  1154. os_memcpy(&buf[reply_len], ap_delimiter, dlen);
  1155. reply_len += dlen;
  1156. for (i = 0; i < wpa_s->ap_iface->num_bss; i++) {
  1157. reply_len += hostapd_ctrl_iface_pmksa_list(
  1158. wpa_s->ap_iface->bss[i],
  1159. &buf[reply_len], len - reply_len);
  1160. }
  1161. }
  1162. if (wpa_s->ifmsh) {
  1163. dlen = os_strlen(mesh_delimiter);
  1164. if (dlen > len - reply_len)
  1165. return reply_len;
  1166. os_memcpy(&buf[reply_len], mesh_delimiter, dlen);
  1167. reply_len += dlen;
  1168. reply_len += hostapd_ctrl_iface_pmksa_list(
  1169. wpa_s->ifmsh->bss[0], &buf[reply_len],
  1170. len - reply_len);
  1171. }
  1172. return reply_len;
  1173. }
  1174. void wpas_ap_pmksa_cache_flush(struct wpa_supplicant *wpa_s)
  1175. {
  1176. size_t i;
  1177. if (wpa_s->ap_iface) {
  1178. for (i = 0; i < wpa_s->ap_iface->num_bss; i++)
  1179. hostapd_ctrl_iface_pmksa_flush(wpa_s->ap_iface->bss[i]);
  1180. }
  1181. if (wpa_s->ifmsh)
  1182. hostapd_ctrl_iface_pmksa_flush(wpa_s->ifmsh->bss[0]);
  1183. }
  1184. #endif /* CONFIG_CTRL_IFACE */
  1185. #ifdef NEED_AP_MLME
  1186. void wpas_event_dfs_radar_detected(struct wpa_supplicant *wpa_s,
  1187. struct dfs_event *radar)
  1188. {
  1189. if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0])
  1190. return;
  1191. wpa_printf(MSG_DEBUG, "DFS radar detected on %d MHz", radar->freq);
  1192. hostapd_dfs_radar_detected(wpa_s->ap_iface, radar->freq,
  1193. radar->ht_enabled, radar->chan_offset,
  1194. radar->chan_width,
  1195. radar->cf1, radar->cf2);
  1196. }
  1197. void wpas_event_dfs_cac_started(struct wpa_supplicant *wpa_s,
  1198. struct dfs_event *radar)
  1199. {
  1200. if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0])
  1201. return;
  1202. wpa_printf(MSG_DEBUG, "DFS CAC started on %d MHz", radar->freq);
  1203. hostapd_dfs_start_cac(wpa_s->ap_iface, radar->freq,
  1204. radar->ht_enabled, radar->chan_offset,
  1205. radar->chan_width, radar->cf1, radar->cf2);
  1206. }
  1207. void wpas_event_dfs_cac_finished(struct wpa_supplicant *wpa_s,
  1208. struct dfs_event *radar)
  1209. {
  1210. if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0])
  1211. return;
  1212. wpa_printf(MSG_DEBUG, "DFS CAC finished on %d MHz", radar->freq);
  1213. hostapd_dfs_complete_cac(wpa_s->ap_iface, 1, radar->freq,
  1214. radar->ht_enabled, radar->chan_offset,
  1215. radar->chan_width, radar->cf1, radar->cf2);
  1216. }
  1217. void wpas_event_dfs_cac_aborted(struct wpa_supplicant *wpa_s,
  1218. struct dfs_event *radar)
  1219. {
  1220. if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0])
  1221. return;
  1222. wpa_printf(MSG_DEBUG, "DFS CAC aborted on %d MHz", radar->freq);
  1223. hostapd_dfs_complete_cac(wpa_s->ap_iface, 0, radar->freq,
  1224. radar->ht_enabled, radar->chan_offset,
  1225. radar->chan_width, radar->cf1, radar->cf2);
  1226. }
  1227. void wpas_event_dfs_cac_nop_finished(struct wpa_supplicant *wpa_s,
  1228. struct dfs_event *radar)
  1229. {
  1230. if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0])
  1231. return;
  1232. wpa_printf(MSG_DEBUG, "DFS NOP finished on %d MHz", radar->freq);
  1233. hostapd_dfs_nop_finished(wpa_s->ap_iface, radar->freq,
  1234. radar->ht_enabled, radar->chan_offset,
  1235. radar->chan_width, radar->cf1, radar->cf2);
  1236. }
  1237. #endif /* NEED_AP_MLME */
  1238. void ap_periodic(struct wpa_supplicant *wpa_s)
  1239. {
  1240. if (wpa_s->ap_iface)
  1241. hostapd_periodic_iface(wpa_s->ap_iface);
  1242. }