ap.c 32 KB

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