ap.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504
  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. bss->ssid.wpa_psk_set = 1;
  271. } else if (ssid->passphrase) {
  272. bss->ssid.wpa_passphrase = os_strdup(ssid->passphrase);
  273. } else if (ssid->wep_key_len[0] || ssid->wep_key_len[1] ||
  274. ssid->wep_key_len[2] || ssid->wep_key_len[3]) {
  275. struct hostapd_wep_keys *wep = &bss->ssid.wep;
  276. int i;
  277. for (i = 0; i < NUM_WEP_KEYS; i++) {
  278. if (ssid->wep_key_len[i] == 0)
  279. continue;
  280. wep->key[i] = os_malloc(ssid->wep_key_len[i]);
  281. if (wep->key[i] == NULL)
  282. return -1;
  283. os_memcpy(wep->key[i], ssid->wep_key[i],
  284. ssid->wep_key_len[i]);
  285. wep->len[i] = ssid->wep_key_len[i];
  286. }
  287. wep->idx = ssid->wep_tx_keyidx;
  288. wep->keys_set = 1;
  289. }
  290. if (ssid->ap_max_inactivity)
  291. bss->ap_max_inactivity = ssid->ap_max_inactivity;
  292. if (ssid->dtim_period)
  293. bss->dtim_period = ssid->dtim_period;
  294. else if (wpa_s->conf->dtim_period)
  295. bss->dtim_period = wpa_s->conf->dtim_period;
  296. if (ssid->beacon_int)
  297. conf->beacon_int = ssid->beacon_int;
  298. else if (wpa_s->conf->beacon_int)
  299. conf->beacon_int = wpa_s->conf->beacon_int;
  300. #ifdef CONFIG_P2P
  301. if (ssid->mode == WPAS_MODE_P2P_GO ||
  302. ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
  303. if (wpa_s->conf->p2p_go_ctwindow > conf->beacon_int) {
  304. wpa_printf(MSG_INFO,
  305. "CTWindow (%d) is bigger than beacon interval (%d) - avoid configuring it",
  306. wpa_s->conf->p2p_go_ctwindow,
  307. conf->beacon_int);
  308. conf->p2p_go_ctwindow = 0;
  309. } else {
  310. conf->p2p_go_ctwindow = wpa_s->conf->p2p_go_ctwindow;
  311. }
  312. }
  313. #endif /* CONFIG_P2P */
  314. if ((bss->wpa & 2) && bss->rsn_pairwise == 0)
  315. bss->rsn_pairwise = bss->wpa_pairwise;
  316. bss->wpa_group = wpa_select_ap_group_cipher(bss->wpa, bss->wpa_pairwise,
  317. bss->rsn_pairwise);
  318. if (bss->wpa && bss->ieee802_1x)
  319. bss->ssid.security_policy = SECURITY_WPA;
  320. else if (bss->wpa)
  321. bss->ssid.security_policy = SECURITY_WPA_PSK;
  322. else if (bss->ieee802_1x) {
  323. int cipher = WPA_CIPHER_NONE;
  324. bss->ssid.security_policy = SECURITY_IEEE_802_1X;
  325. bss->ssid.wep.default_len = bss->default_wep_key_len;
  326. if (bss->default_wep_key_len)
  327. cipher = bss->default_wep_key_len >= 13 ?
  328. WPA_CIPHER_WEP104 : WPA_CIPHER_WEP40;
  329. bss->wpa_group = cipher;
  330. bss->wpa_pairwise = cipher;
  331. bss->rsn_pairwise = cipher;
  332. } else if (bss->ssid.wep.keys_set) {
  333. int cipher = WPA_CIPHER_WEP40;
  334. if (bss->ssid.wep.len[0] >= 13)
  335. cipher = WPA_CIPHER_WEP104;
  336. bss->ssid.security_policy = SECURITY_STATIC_WEP;
  337. bss->wpa_group = cipher;
  338. bss->wpa_pairwise = cipher;
  339. bss->rsn_pairwise = cipher;
  340. } else {
  341. bss->ssid.security_policy = SECURITY_PLAINTEXT;
  342. bss->wpa_group = WPA_CIPHER_NONE;
  343. bss->wpa_pairwise = WPA_CIPHER_NONE;
  344. bss->rsn_pairwise = WPA_CIPHER_NONE;
  345. }
  346. if (bss->wpa_group_rekey < 86400 && (bss->wpa & 2) &&
  347. (bss->wpa_group == WPA_CIPHER_CCMP ||
  348. bss->wpa_group == WPA_CIPHER_GCMP ||
  349. bss->wpa_group == WPA_CIPHER_CCMP_256 ||
  350. bss->wpa_group == WPA_CIPHER_GCMP_256)) {
  351. /*
  352. * Strong ciphers do not need frequent rekeying, so increase
  353. * the default GTK rekeying period to 24 hours.
  354. */
  355. bss->wpa_group_rekey = 86400;
  356. }
  357. #ifdef CONFIG_IEEE80211W
  358. if (ssid->ieee80211w != MGMT_FRAME_PROTECTION_DEFAULT)
  359. bss->ieee80211w = ssid->ieee80211w;
  360. #endif /* CONFIG_IEEE80211W */
  361. #ifdef CONFIG_WPS
  362. /*
  363. * Enable WPS by default for open and WPA/WPA2-Personal network, but
  364. * require user interaction to actually use it. Only the internal
  365. * Registrar is supported.
  366. */
  367. if (bss->ssid.security_policy != SECURITY_WPA_PSK &&
  368. bss->ssid.security_policy != SECURITY_PLAINTEXT)
  369. goto no_wps;
  370. if (bss->ssid.security_policy == SECURITY_WPA_PSK &&
  371. (!(bss->rsn_pairwise & (WPA_CIPHER_CCMP | WPA_CIPHER_GCMP)) ||
  372. !(bss->wpa & 2)))
  373. goto no_wps; /* WPS2 does not allow WPA/TKIP-only
  374. * configuration */
  375. if (ssid->wps_disabled)
  376. goto no_wps;
  377. bss->eap_server = 1;
  378. if (!ssid->ignore_broadcast_ssid)
  379. bss->wps_state = 2;
  380. bss->ap_setup_locked = 2;
  381. if (wpa_s->conf->config_methods)
  382. bss->config_methods = os_strdup(wpa_s->conf->config_methods);
  383. os_memcpy(bss->device_type, wpa_s->conf->device_type,
  384. WPS_DEV_TYPE_LEN);
  385. if (wpa_s->conf->device_name) {
  386. bss->device_name = os_strdup(wpa_s->conf->device_name);
  387. bss->friendly_name = os_strdup(wpa_s->conf->device_name);
  388. }
  389. if (wpa_s->conf->manufacturer)
  390. bss->manufacturer = os_strdup(wpa_s->conf->manufacturer);
  391. if (wpa_s->conf->model_name)
  392. bss->model_name = os_strdup(wpa_s->conf->model_name);
  393. if (wpa_s->conf->model_number)
  394. bss->model_number = os_strdup(wpa_s->conf->model_number);
  395. if (wpa_s->conf->serial_number)
  396. bss->serial_number = os_strdup(wpa_s->conf->serial_number);
  397. if (is_nil_uuid(wpa_s->conf->uuid))
  398. os_memcpy(bss->uuid, wpa_s->wps->uuid, WPS_UUID_LEN);
  399. else
  400. os_memcpy(bss->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
  401. os_memcpy(bss->os_version, wpa_s->conf->os_version, 4);
  402. bss->pbc_in_m1 = wpa_s->conf->pbc_in_m1;
  403. no_wps:
  404. #endif /* CONFIG_WPS */
  405. if (wpa_s->max_stations &&
  406. wpa_s->max_stations < wpa_s->conf->max_num_sta)
  407. bss->max_num_sta = wpa_s->max_stations;
  408. else
  409. bss->max_num_sta = wpa_s->conf->max_num_sta;
  410. bss->disassoc_low_ack = wpa_s->conf->disassoc_low_ack;
  411. if (wpa_s->conf->ap_vendor_elements) {
  412. bss->vendor_elements =
  413. wpabuf_dup(wpa_s->conf->ap_vendor_elements);
  414. }
  415. return 0;
  416. }
  417. static void ap_public_action_rx(void *ctx, const u8 *buf, size_t len, int freq)
  418. {
  419. #ifdef CONFIG_P2P
  420. struct wpa_supplicant *wpa_s = ctx;
  421. const struct ieee80211_mgmt *mgmt;
  422. mgmt = (const struct ieee80211_mgmt *) buf;
  423. if (len < IEEE80211_HDRLEN + 1)
  424. return;
  425. if (mgmt->u.action.category != WLAN_ACTION_PUBLIC)
  426. return;
  427. wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
  428. mgmt->u.action.category,
  429. buf + IEEE80211_HDRLEN + 1,
  430. len - IEEE80211_HDRLEN - 1, freq);
  431. #endif /* CONFIG_P2P */
  432. }
  433. static void ap_wps_event_cb(void *ctx, enum wps_event event,
  434. union wps_event_data *data)
  435. {
  436. #ifdef CONFIG_P2P
  437. struct wpa_supplicant *wpa_s = ctx;
  438. if (event == WPS_EV_FAIL) {
  439. struct wps_event_fail *fail = &data->fail;
  440. if (wpa_s->p2pdev && wpa_s->p2pdev != wpa_s &&
  441. wpa_s == wpa_s->global->p2p_group_formation) {
  442. /*
  443. * src/ap/wps_hostapd.c has already sent this on the
  444. * main interface, so only send on the parent interface
  445. * here if needed.
  446. */
  447. wpa_msg(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_FAIL
  448. "msg=%d config_error=%d",
  449. fail->msg, fail->config_error);
  450. }
  451. wpas_p2p_wps_failed(wpa_s, fail);
  452. }
  453. #endif /* CONFIG_P2P */
  454. }
  455. static void ap_sta_authorized_cb(void *ctx, const u8 *mac_addr,
  456. int authorized, const u8 *p2p_dev_addr)
  457. {
  458. wpas_notify_sta_authorized(ctx, mac_addr, authorized, p2p_dev_addr);
  459. }
  460. #ifdef CONFIG_P2P
  461. static void ap_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *p2p_dev_addr,
  462. const u8 *psk, size_t psk_len)
  463. {
  464. struct wpa_supplicant *wpa_s = ctx;
  465. if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL)
  466. return;
  467. wpas_p2p_new_psk_cb(wpa_s, mac_addr, p2p_dev_addr, psk, psk_len);
  468. }
  469. #endif /* CONFIG_P2P */
  470. static int ap_vendor_action_rx(void *ctx, const u8 *buf, size_t len, int freq)
  471. {
  472. #ifdef CONFIG_P2P
  473. struct wpa_supplicant *wpa_s = ctx;
  474. const struct ieee80211_mgmt *mgmt;
  475. mgmt = (const struct ieee80211_mgmt *) buf;
  476. if (len < IEEE80211_HDRLEN + 1)
  477. return -1;
  478. wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
  479. mgmt->u.action.category,
  480. buf + IEEE80211_HDRLEN + 1,
  481. len - IEEE80211_HDRLEN - 1, freq);
  482. #endif /* CONFIG_P2P */
  483. return 0;
  484. }
  485. static int ap_probe_req_rx(void *ctx, const u8 *sa, const u8 *da,
  486. const u8 *bssid, const u8 *ie, size_t ie_len,
  487. int ssi_signal)
  488. {
  489. struct wpa_supplicant *wpa_s = ctx;
  490. unsigned int freq = 0;
  491. if (wpa_s->ap_iface)
  492. freq = wpa_s->ap_iface->freq;
  493. return wpas_p2p_probe_req_rx(wpa_s, sa, da, bssid, ie, ie_len,
  494. freq, ssi_signal);
  495. }
  496. static void ap_wps_reg_success_cb(void *ctx, const u8 *mac_addr,
  497. const u8 *uuid_e)
  498. {
  499. struct wpa_supplicant *wpa_s = ctx;
  500. wpas_p2p_wps_success(wpa_s, mac_addr, 1);
  501. }
  502. static void wpas_ap_configured_cb(void *ctx)
  503. {
  504. struct wpa_supplicant *wpa_s = ctx;
  505. #ifdef CONFIG_ACS
  506. if (wpa_s->current_ssid && wpa_s->current_ssid->acs)
  507. wpa_s->assoc_freq = wpa_s->ap_iface->freq;
  508. #endif /* CONFIG_ACS */
  509. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  510. if (wpa_s->ap_configured_cb)
  511. wpa_s->ap_configured_cb(wpa_s->ap_configured_cb_ctx,
  512. wpa_s->ap_configured_cb_data);
  513. }
  514. int wpa_supplicant_create_ap(struct wpa_supplicant *wpa_s,
  515. struct wpa_ssid *ssid)
  516. {
  517. struct wpa_driver_associate_params params;
  518. struct hostapd_iface *hapd_iface;
  519. struct hostapd_config *conf;
  520. size_t i;
  521. if (ssid->ssid == NULL || ssid->ssid_len == 0) {
  522. wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
  523. return -1;
  524. }
  525. wpa_supplicant_ap_deinit(wpa_s);
  526. wpa_printf(MSG_DEBUG, "Setting up AP (SSID='%s')",
  527. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  528. os_memset(&params, 0, sizeof(params));
  529. params.ssid = ssid->ssid;
  530. params.ssid_len = ssid->ssid_len;
  531. switch (ssid->mode) {
  532. case WPAS_MODE_AP:
  533. case WPAS_MODE_P2P_GO:
  534. case WPAS_MODE_P2P_GROUP_FORMATION:
  535. params.mode = IEEE80211_MODE_AP;
  536. break;
  537. default:
  538. return -1;
  539. }
  540. if (ssid->frequency == 0)
  541. ssid->frequency = 2462; /* default channel 11 */
  542. params.freq.freq = ssid->frequency;
  543. params.wpa_proto = ssid->proto;
  544. if (ssid->key_mgmt & WPA_KEY_MGMT_PSK)
  545. wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
  546. else
  547. wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
  548. params.key_mgmt_suite = wpa_s->key_mgmt;
  549. wpa_s->pairwise_cipher = wpa_pick_pairwise_cipher(ssid->pairwise_cipher,
  550. 1);
  551. if (wpa_s->pairwise_cipher < 0) {
  552. wpa_printf(MSG_WARNING, "WPA: Failed to select pairwise "
  553. "cipher.");
  554. return -1;
  555. }
  556. params.pairwise_suite = wpa_s->pairwise_cipher;
  557. params.group_suite = params.pairwise_suite;
  558. #ifdef CONFIG_P2P
  559. if (ssid->mode == WPAS_MODE_P2P_GO ||
  560. ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
  561. params.p2p = 1;
  562. #endif /* CONFIG_P2P */
  563. if (wpa_s->p2pdev->set_ap_uapsd)
  564. params.uapsd = wpa_s->p2pdev->ap_uapsd;
  565. else if (params.p2p && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_UAPSD))
  566. params.uapsd = 1; /* mandatory for P2P GO */
  567. else
  568. params.uapsd = -1;
  569. if (ieee80211_is_dfs(params.freq.freq))
  570. params.freq.freq = 0; /* set channel after CAC */
  571. if (params.p2p)
  572. wpa_drv_get_ext_capa(wpa_s, WPA_IF_P2P_GO);
  573. else
  574. wpa_drv_get_ext_capa(wpa_s, WPA_IF_AP_BSS);
  575. if (wpa_drv_associate(wpa_s, &params) < 0) {
  576. wpa_msg(wpa_s, MSG_INFO, "Failed to start AP functionality");
  577. return -1;
  578. }
  579. wpa_s->ap_iface = hapd_iface = os_zalloc(sizeof(*wpa_s->ap_iface));
  580. if (hapd_iface == NULL)
  581. return -1;
  582. hapd_iface->owner = wpa_s;
  583. hapd_iface->drv_flags = wpa_s->drv_flags;
  584. hapd_iface->smps_modes = wpa_s->drv_smps_modes;
  585. hapd_iface->probe_resp_offloads = wpa_s->probe_resp_offloads;
  586. hapd_iface->extended_capa = wpa_s->extended_capa;
  587. hapd_iface->extended_capa_mask = wpa_s->extended_capa_mask;
  588. hapd_iface->extended_capa_len = wpa_s->extended_capa_len;
  589. wpa_s->ap_iface->conf = conf = hostapd_config_defaults();
  590. if (conf == NULL) {
  591. wpa_supplicant_ap_deinit(wpa_s);
  592. return -1;
  593. }
  594. /* Use the maximum oper channel width if it's given. */
  595. if (ssid->max_oper_chwidth)
  596. conf->vht_oper_chwidth = ssid->max_oper_chwidth;
  597. ieee80211_freq_to_chan(ssid->vht_center_freq2,
  598. &conf->vht_oper_centr_freq_seg1_idx);
  599. os_memcpy(wpa_s->ap_iface->conf->wmm_ac_params,
  600. wpa_s->conf->wmm_ac_params,
  601. sizeof(wpa_s->conf->wmm_ac_params));
  602. if (params.uapsd > 0) {
  603. conf->bss[0]->wmm_enabled = 1;
  604. conf->bss[0]->wmm_uapsd = 1;
  605. }
  606. if (wpa_supplicant_conf_ap(wpa_s, ssid, conf)) {
  607. wpa_printf(MSG_ERROR, "Failed to create AP configuration");
  608. wpa_supplicant_ap_deinit(wpa_s);
  609. return -1;
  610. }
  611. #ifdef CONFIG_P2P
  612. if (ssid->mode == WPAS_MODE_P2P_GO)
  613. conf->bss[0]->p2p = P2P_ENABLED | P2P_GROUP_OWNER;
  614. else if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
  615. conf->bss[0]->p2p = P2P_ENABLED | P2P_GROUP_OWNER |
  616. P2P_GROUP_FORMATION;
  617. #endif /* CONFIG_P2P */
  618. hapd_iface->num_bss = conf->num_bss;
  619. hapd_iface->bss = os_calloc(conf->num_bss,
  620. sizeof(struct hostapd_data *));
  621. if (hapd_iface->bss == NULL) {
  622. wpa_supplicant_ap_deinit(wpa_s);
  623. return -1;
  624. }
  625. for (i = 0; i < conf->num_bss; i++) {
  626. hapd_iface->bss[i] =
  627. hostapd_alloc_bss_data(hapd_iface, conf,
  628. conf->bss[i]);
  629. if (hapd_iface->bss[i] == NULL) {
  630. wpa_supplicant_ap_deinit(wpa_s);
  631. return -1;
  632. }
  633. hapd_iface->bss[i]->msg_ctx = wpa_s;
  634. hapd_iface->bss[i]->msg_ctx_parent = wpa_s->p2pdev;
  635. hapd_iface->bss[i]->public_action_cb = ap_public_action_rx;
  636. hapd_iface->bss[i]->public_action_cb_ctx = wpa_s;
  637. hapd_iface->bss[i]->vendor_action_cb = ap_vendor_action_rx;
  638. hapd_iface->bss[i]->vendor_action_cb_ctx = wpa_s;
  639. hostapd_register_probereq_cb(hapd_iface->bss[i],
  640. ap_probe_req_rx, wpa_s);
  641. hapd_iface->bss[i]->wps_reg_success_cb = ap_wps_reg_success_cb;
  642. hapd_iface->bss[i]->wps_reg_success_cb_ctx = wpa_s;
  643. hapd_iface->bss[i]->wps_event_cb = ap_wps_event_cb;
  644. hapd_iface->bss[i]->wps_event_cb_ctx = wpa_s;
  645. hapd_iface->bss[i]->sta_authorized_cb = ap_sta_authorized_cb;
  646. hapd_iface->bss[i]->sta_authorized_cb_ctx = wpa_s;
  647. #ifdef CONFIG_P2P
  648. hapd_iface->bss[i]->new_psk_cb = ap_new_psk_cb;
  649. hapd_iface->bss[i]->new_psk_cb_ctx = wpa_s;
  650. hapd_iface->bss[i]->p2p = wpa_s->global->p2p;
  651. hapd_iface->bss[i]->p2p_group = wpas_p2p_group_init(wpa_s,
  652. ssid);
  653. #endif /* CONFIG_P2P */
  654. hapd_iface->bss[i]->setup_complete_cb = wpas_ap_configured_cb;
  655. hapd_iface->bss[i]->setup_complete_cb_ctx = wpa_s;
  656. #ifdef CONFIG_TESTING_OPTIONS
  657. hapd_iface->bss[i]->ext_eapol_frame_io =
  658. wpa_s->ext_eapol_frame_io;
  659. #endif /* CONFIG_TESTING_OPTIONS */
  660. }
  661. os_memcpy(hapd_iface->bss[0]->own_addr, wpa_s->own_addr, ETH_ALEN);
  662. hapd_iface->bss[0]->driver = wpa_s->driver;
  663. hapd_iface->bss[0]->drv_priv = wpa_s->drv_priv;
  664. wpa_s->current_ssid = ssid;
  665. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  666. os_memcpy(wpa_s->bssid, wpa_s->own_addr, ETH_ALEN);
  667. wpa_s->assoc_freq = ssid->frequency;
  668. if (hostapd_setup_interface(wpa_s->ap_iface)) {
  669. wpa_printf(MSG_ERROR, "Failed to initialize AP interface");
  670. wpa_supplicant_ap_deinit(wpa_s);
  671. return -1;
  672. }
  673. return 0;
  674. }
  675. void wpa_supplicant_ap_deinit(struct wpa_supplicant *wpa_s)
  676. {
  677. #ifdef CONFIG_WPS
  678. eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
  679. #endif /* CONFIG_WPS */
  680. if (wpa_s->ap_iface == NULL)
  681. return;
  682. wpa_s->current_ssid = NULL;
  683. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  684. wpa_s->assoc_freq = 0;
  685. wpas_p2p_ap_deinit(wpa_s);
  686. wpa_s->ap_iface->driver_ap_teardown =
  687. !!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
  688. hostapd_interface_deinit(wpa_s->ap_iface);
  689. hostapd_interface_free(wpa_s->ap_iface);
  690. wpa_s->ap_iface = NULL;
  691. wpa_drv_deinit_ap(wpa_s);
  692. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
  693. " reason=%d locally_generated=1",
  694. MAC2STR(wpa_s->own_addr), WLAN_REASON_DEAUTH_LEAVING);
  695. }
  696. void ap_tx_status(void *ctx, const u8 *addr,
  697. const u8 *buf, size_t len, int ack)
  698. {
  699. #ifdef NEED_AP_MLME
  700. struct wpa_supplicant *wpa_s = ctx;
  701. hostapd_tx_status(wpa_s->ap_iface->bss[0], addr, buf, len, ack);
  702. #endif /* NEED_AP_MLME */
  703. }
  704. void ap_eapol_tx_status(void *ctx, const u8 *dst,
  705. const u8 *data, size_t len, int ack)
  706. {
  707. #ifdef NEED_AP_MLME
  708. struct wpa_supplicant *wpa_s = ctx;
  709. if (!wpa_s->ap_iface)
  710. return;
  711. hostapd_tx_status(wpa_s->ap_iface->bss[0], dst, data, len, ack);
  712. #endif /* NEED_AP_MLME */
  713. }
  714. void ap_client_poll_ok(void *ctx, const u8 *addr)
  715. {
  716. #ifdef NEED_AP_MLME
  717. struct wpa_supplicant *wpa_s = ctx;
  718. if (wpa_s->ap_iface)
  719. hostapd_client_poll_ok(wpa_s->ap_iface->bss[0], addr);
  720. #endif /* NEED_AP_MLME */
  721. }
  722. void ap_rx_from_unknown_sta(void *ctx, const u8 *addr, int wds)
  723. {
  724. #ifdef NEED_AP_MLME
  725. struct wpa_supplicant *wpa_s = ctx;
  726. ieee802_11_rx_from_unknown(wpa_s->ap_iface->bss[0], addr, wds);
  727. #endif /* NEED_AP_MLME */
  728. }
  729. void ap_mgmt_rx(void *ctx, struct rx_mgmt *rx_mgmt)
  730. {
  731. #ifdef NEED_AP_MLME
  732. struct wpa_supplicant *wpa_s = ctx;
  733. struct hostapd_frame_info fi;
  734. os_memset(&fi, 0, sizeof(fi));
  735. fi.datarate = rx_mgmt->datarate;
  736. fi.ssi_signal = rx_mgmt->ssi_signal;
  737. ieee802_11_mgmt(wpa_s->ap_iface->bss[0], rx_mgmt->frame,
  738. rx_mgmt->frame_len, &fi);
  739. #endif /* NEED_AP_MLME */
  740. }
  741. void ap_mgmt_tx_cb(void *ctx, const u8 *buf, size_t len, u16 stype, int ok)
  742. {
  743. #ifdef NEED_AP_MLME
  744. struct wpa_supplicant *wpa_s = ctx;
  745. ieee802_11_mgmt_cb(wpa_s->ap_iface->bss[0], buf, len, stype, ok);
  746. #endif /* NEED_AP_MLME */
  747. }
  748. void wpa_supplicant_ap_rx_eapol(struct wpa_supplicant *wpa_s,
  749. const u8 *src_addr, const u8 *buf, size_t len)
  750. {
  751. ieee802_1x_receive(wpa_s->ap_iface->bss[0], src_addr, buf, len);
  752. }
  753. #ifdef CONFIG_WPS
  754. int wpa_supplicant_ap_wps_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid,
  755. const u8 *p2p_dev_addr)
  756. {
  757. if (!wpa_s->ap_iface)
  758. return -1;
  759. return hostapd_wps_button_pushed(wpa_s->ap_iface->bss[0],
  760. p2p_dev_addr);
  761. }
  762. int wpa_supplicant_ap_wps_cancel(struct wpa_supplicant *wpa_s)
  763. {
  764. struct wps_registrar *reg;
  765. int reg_sel = 0, wps_sta = 0;
  766. if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0]->wps)
  767. return -1;
  768. reg = wpa_s->ap_iface->bss[0]->wps->registrar;
  769. reg_sel = wps_registrar_wps_cancel(reg);
  770. wps_sta = ap_for_each_sta(wpa_s->ap_iface->bss[0],
  771. ap_sta_wps_cancel, NULL);
  772. if (!reg_sel && !wps_sta) {
  773. wpa_printf(MSG_DEBUG, "No WPS operation in progress at this "
  774. "time");
  775. return -1;
  776. }
  777. /*
  778. * There are 2 cases to return wps cancel as success:
  779. * 1. When wps cancel was initiated but no connection has been
  780. * established with client yet.
  781. * 2. Client is in the middle of exchanging WPS messages.
  782. */
  783. return 0;
  784. }
  785. int wpa_supplicant_ap_wps_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
  786. const char *pin, char *buf, size_t buflen,
  787. int timeout)
  788. {
  789. int ret, ret_len = 0;
  790. if (!wpa_s->ap_iface)
  791. return -1;
  792. if (pin == NULL) {
  793. unsigned int rpin;
  794. if (wps_generate_pin(&rpin) < 0)
  795. return -1;
  796. ret_len = os_snprintf(buf, buflen, "%08d", rpin);
  797. if (os_snprintf_error(buflen, ret_len))
  798. return -1;
  799. pin = buf;
  800. } else if (buf) {
  801. ret_len = os_snprintf(buf, buflen, "%s", pin);
  802. if (os_snprintf_error(buflen, ret_len))
  803. return -1;
  804. }
  805. ret = hostapd_wps_add_pin(wpa_s->ap_iface->bss[0], bssid, "any", pin,
  806. timeout);
  807. if (ret)
  808. return -1;
  809. return ret_len;
  810. }
  811. static void wpas_wps_ap_pin_timeout(void *eloop_data, void *user_ctx)
  812. {
  813. struct wpa_supplicant *wpa_s = eloop_data;
  814. wpa_printf(MSG_DEBUG, "WPS: AP PIN timed out");
  815. wpas_wps_ap_pin_disable(wpa_s);
  816. }
  817. static void wpas_wps_ap_pin_enable(struct wpa_supplicant *wpa_s, int timeout)
  818. {
  819. struct hostapd_data *hapd;
  820. if (wpa_s->ap_iface == NULL)
  821. return;
  822. hapd = wpa_s->ap_iface->bss[0];
  823. wpa_printf(MSG_DEBUG, "WPS: Enabling AP PIN (timeout=%d)", timeout);
  824. hapd->ap_pin_failures = 0;
  825. eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
  826. if (timeout > 0)
  827. eloop_register_timeout(timeout, 0,
  828. wpas_wps_ap_pin_timeout, wpa_s, NULL);
  829. }
  830. void wpas_wps_ap_pin_disable(struct wpa_supplicant *wpa_s)
  831. {
  832. struct hostapd_data *hapd;
  833. if (wpa_s->ap_iface == NULL)
  834. return;
  835. wpa_printf(MSG_DEBUG, "WPS: Disabling AP PIN");
  836. hapd = wpa_s->ap_iface->bss[0];
  837. os_free(hapd->conf->ap_pin);
  838. hapd->conf->ap_pin = NULL;
  839. eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
  840. }
  841. const char * wpas_wps_ap_pin_random(struct wpa_supplicant *wpa_s, int timeout)
  842. {
  843. struct hostapd_data *hapd;
  844. unsigned int pin;
  845. char pin_txt[9];
  846. if (wpa_s->ap_iface == NULL)
  847. return NULL;
  848. hapd = wpa_s->ap_iface->bss[0];
  849. if (wps_generate_pin(&pin) < 0)
  850. return NULL;
  851. os_snprintf(pin_txt, sizeof(pin_txt), "%08u", pin);
  852. os_free(hapd->conf->ap_pin);
  853. hapd->conf->ap_pin = os_strdup(pin_txt);
  854. if (hapd->conf->ap_pin == NULL)
  855. return NULL;
  856. wpas_wps_ap_pin_enable(wpa_s, timeout);
  857. return hapd->conf->ap_pin;
  858. }
  859. const char * wpas_wps_ap_pin_get(struct wpa_supplicant *wpa_s)
  860. {
  861. struct hostapd_data *hapd;
  862. if (wpa_s->ap_iface == NULL)
  863. return NULL;
  864. hapd = wpa_s->ap_iface->bss[0];
  865. return hapd->conf->ap_pin;
  866. }
  867. int wpas_wps_ap_pin_set(struct wpa_supplicant *wpa_s, const char *pin,
  868. int timeout)
  869. {
  870. struct hostapd_data *hapd;
  871. char pin_txt[9];
  872. int ret;
  873. if (wpa_s->ap_iface == NULL)
  874. return -1;
  875. hapd = wpa_s->ap_iface->bss[0];
  876. ret = os_snprintf(pin_txt, sizeof(pin_txt), "%s", pin);
  877. if (os_snprintf_error(sizeof(pin_txt), ret))
  878. return -1;
  879. os_free(hapd->conf->ap_pin);
  880. hapd->conf->ap_pin = os_strdup(pin_txt);
  881. if (hapd->conf->ap_pin == NULL)
  882. return -1;
  883. wpas_wps_ap_pin_enable(wpa_s, timeout);
  884. return 0;
  885. }
  886. void wpa_supplicant_ap_pwd_auth_fail(struct wpa_supplicant *wpa_s)
  887. {
  888. struct hostapd_data *hapd;
  889. if (wpa_s->ap_iface == NULL)
  890. return;
  891. hapd = wpa_s->ap_iface->bss[0];
  892. /*
  893. * Registrar failed to prove its knowledge of the AP PIN. Disable AP
  894. * PIN if this happens multiple times to slow down brute force attacks.
  895. */
  896. hapd->ap_pin_failures++;
  897. wpa_printf(MSG_DEBUG, "WPS: AP PIN authentication failure number %u",
  898. hapd->ap_pin_failures);
  899. if (hapd->ap_pin_failures < 3)
  900. return;
  901. wpa_printf(MSG_DEBUG, "WPS: Disable AP PIN");
  902. hapd->ap_pin_failures = 0;
  903. os_free(hapd->conf->ap_pin);
  904. hapd->conf->ap_pin = NULL;
  905. }
  906. #ifdef CONFIG_WPS_NFC
  907. struct wpabuf * wpas_ap_wps_nfc_config_token(struct wpa_supplicant *wpa_s,
  908. int ndef)
  909. {
  910. struct hostapd_data *hapd;
  911. if (wpa_s->ap_iface == NULL)
  912. return NULL;
  913. hapd = wpa_s->ap_iface->bss[0];
  914. return hostapd_wps_nfc_config_token(hapd, ndef);
  915. }
  916. struct wpabuf * wpas_ap_wps_nfc_handover_sel(struct wpa_supplicant *wpa_s,
  917. int ndef)
  918. {
  919. struct hostapd_data *hapd;
  920. if (wpa_s->ap_iface == NULL)
  921. return NULL;
  922. hapd = wpa_s->ap_iface->bss[0];
  923. return hostapd_wps_nfc_hs_cr(hapd, ndef);
  924. }
  925. int wpas_ap_wps_nfc_report_handover(struct wpa_supplicant *wpa_s,
  926. const struct wpabuf *req,
  927. const struct wpabuf *sel)
  928. {
  929. struct hostapd_data *hapd;
  930. if (wpa_s->ap_iface == NULL)
  931. return -1;
  932. hapd = wpa_s->ap_iface->bss[0];
  933. return hostapd_wps_nfc_report_handover(hapd, req, sel);
  934. }
  935. #endif /* CONFIG_WPS_NFC */
  936. #endif /* CONFIG_WPS */
  937. #ifdef CONFIG_CTRL_IFACE
  938. int ap_ctrl_iface_sta_first(struct wpa_supplicant *wpa_s,
  939. char *buf, size_t buflen)
  940. {
  941. struct hostapd_data *hapd;
  942. if (wpa_s->ap_iface)
  943. hapd = wpa_s->ap_iface->bss[0];
  944. else if (wpa_s->ifmsh)
  945. hapd = wpa_s->ifmsh->bss[0];
  946. else
  947. return -1;
  948. return hostapd_ctrl_iface_sta_first(hapd, buf, buflen);
  949. }
  950. int ap_ctrl_iface_sta(struct wpa_supplicant *wpa_s, const char *txtaddr,
  951. char *buf, size_t buflen)
  952. {
  953. struct hostapd_data *hapd;
  954. if (wpa_s->ap_iface)
  955. hapd = wpa_s->ap_iface->bss[0];
  956. else if (wpa_s->ifmsh)
  957. hapd = wpa_s->ifmsh->bss[0];
  958. else
  959. return -1;
  960. return hostapd_ctrl_iface_sta(hapd, txtaddr, buf, buflen);
  961. }
  962. int ap_ctrl_iface_sta_next(struct wpa_supplicant *wpa_s, const char *txtaddr,
  963. char *buf, size_t buflen)
  964. {
  965. struct hostapd_data *hapd;
  966. if (wpa_s->ap_iface)
  967. hapd = wpa_s->ap_iface->bss[0];
  968. else if (wpa_s->ifmsh)
  969. hapd = wpa_s->ifmsh->bss[0];
  970. else
  971. return -1;
  972. return hostapd_ctrl_iface_sta_next(hapd, txtaddr, buf, buflen);
  973. }
  974. int ap_ctrl_iface_sta_disassociate(struct wpa_supplicant *wpa_s,
  975. const char *txtaddr)
  976. {
  977. if (wpa_s->ap_iface == NULL)
  978. return -1;
  979. return hostapd_ctrl_iface_disassociate(wpa_s->ap_iface->bss[0],
  980. txtaddr);
  981. }
  982. int ap_ctrl_iface_sta_deauthenticate(struct wpa_supplicant *wpa_s,
  983. const char *txtaddr)
  984. {
  985. if (wpa_s->ap_iface == NULL)
  986. return -1;
  987. return hostapd_ctrl_iface_deauthenticate(wpa_s->ap_iface->bss[0],
  988. txtaddr);
  989. }
  990. int ap_ctrl_iface_wpa_get_status(struct wpa_supplicant *wpa_s, char *buf,
  991. size_t buflen, int verbose)
  992. {
  993. char *pos = buf, *end = buf + buflen;
  994. int ret;
  995. struct hostapd_bss_config *conf;
  996. if (wpa_s->ap_iface == NULL)
  997. return -1;
  998. conf = wpa_s->ap_iface->bss[0]->conf;
  999. if (conf->wpa == 0)
  1000. return 0;
  1001. ret = os_snprintf(pos, end - pos,
  1002. "pairwise_cipher=%s\n"
  1003. "group_cipher=%s\n"
  1004. "key_mgmt=%s\n",
  1005. wpa_cipher_txt(conf->rsn_pairwise),
  1006. wpa_cipher_txt(conf->wpa_group),
  1007. wpa_key_mgmt_txt(conf->wpa_key_mgmt,
  1008. conf->wpa));
  1009. if (os_snprintf_error(end - pos, ret))
  1010. return pos - buf;
  1011. pos += ret;
  1012. return pos - buf;
  1013. }
  1014. #endif /* CONFIG_CTRL_IFACE */
  1015. int wpa_supplicant_ap_update_beacon(struct wpa_supplicant *wpa_s)
  1016. {
  1017. struct hostapd_iface *iface = wpa_s->ap_iface;
  1018. struct wpa_ssid *ssid = wpa_s->current_ssid;
  1019. struct hostapd_data *hapd;
  1020. if (ssid == NULL || wpa_s->ap_iface == NULL ||
  1021. ssid->mode == WPAS_MODE_INFRA ||
  1022. ssid->mode == WPAS_MODE_IBSS)
  1023. return -1;
  1024. #ifdef CONFIG_P2P
  1025. if (ssid->mode == WPAS_MODE_P2P_GO)
  1026. iface->conf->bss[0]->p2p = P2P_ENABLED | P2P_GROUP_OWNER;
  1027. else if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
  1028. iface->conf->bss[0]->p2p = P2P_ENABLED | P2P_GROUP_OWNER |
  1029. P2P_GROUP_FORMATION;
  1030. #endif /* CONFIG_P2P */
  1031. hapd = iface->bss[0];
  1032. if (hapd->drv_priv == NULL)
  1033. return -1;
  1034. ieee802_11_set_beacons(iface);
  1035. hostapd_set_ap_wps_ie(hapd);
  1036. return 0;
  1037. }
  1038. int ap_switch_channel(struct wpa_supplicant *wpa_s,
  1039. struct csa_settings *settings)
  1040. {
  1041. #ifdef NEED_AP_MLME
  1042. if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0])
  1043. return -1;
  1044. return hostapd_switch_channel(wpa_s->ap_iface->bss[0], settings);
  1045. #else /* NEED_AP_MLME */
  1046. return -1;
  1047. #endif /* NEED_AP_MLME */
  1048. }
  1049. #ifdef CONFIG_CTRL_IFACE
  1050. int ap_ctrl_iface_chanswitch(struct wpa_supplicant *wpa_s, const char *pos)
  1051. {
  1052. struct csa_settings settings;
  1053. int ret = hostapd_parse_csa_settings(pos, &settings);
  1054. if (ret)
  1055. return ret;
  1056. return ap_switch_channel(wpa_s, &settings);
  1057. }
  1058. #endif /* CONFIG_CTRL_IFACE */
  1059. void wpas_ap_ch_switch(struct wpa_supplicant *wpa_s, int freq, int ht,
  1060. int offset, int width, int cf1, int cf2)
  1061. {
  1062. if (!wpa_s->ap_iface)
  1063. return;
  1064. wpa_s->assoc_freq = freq;
  1065. if (wpa_s->current_ssid)
  1066. wpa_s->current_ssid->frequency = freq;
  1067. hostapd_event_ch_switch(wpa_s->ap_iface->bss[0], freq, ht,
  1068. offset, width, cf1, cf2);
  1069. }
  1070. int wpa_supplicant_ap_mac_addr_filter(struct wpa_supplicant *wpa_s,
  1071. const u8 *addr)
  1072. {
  1073. struct hostapd_data *hapd;
  1074. struct hostapd_bss_config *conf;
  1075. if (!wpa_s->ap_iface)
  1076. return -1;
  1077. if (addr)
  1078. wpa_printf(MSG_DEBUG, "AP: Set MAC address filter: " MACSTR,
  1079. MAC2STR(addr));
  1080. else
  1081. wpa_printf(MSG_DEBUG, "AP: Clear MAC address filter");
  1082. hapd = wpa_s->ap_iface->bss[0];
  1083. conf = hapd->conf;
  1084. os_free(conf->accept_mac);
  1085. conf->accept_mac = NULL;
  1086. conf->num_accept_mac = 0;
  1087. os_free(conf->deny_mac);
  1088. conf->deny_mac = NULL;
  1089. conf->num_deny_mac = 0;
  1090. if (addr == NULL) {
  1091. conf->macaddr_acl = ACCEPT_UNLESS_DENIED;
  1092. return 0;
  1093. }
  1094. conf->macaddr_acl = DENY_UNLESS_ACCEPTED;
  1095. conf->accept_mac = os_zalloc(sizeof(struct mac_acl_entry));
  1096. if (conf->accept_mac == NULL)
  1097. return -1;
  1098. os_memcpy(conf->accept_mac[0].addr, addr, ETH_ALEN);
  1099. conf->num_accept_mac = 1;
  1100. return 0;
  1101. }
  1102. #ifdef CONFIG_WPS_NFC
  1103. int wpas_ap_wps_add_nfc_pw(struct wpa_supplicant *wpa_s, u16 pw_id,
  1104. const struct wpabuf *pw, const u8 *pubkey_hash)
  1105. {
  1106. struct hostapd_data *hapd;
  1107. struct wps_context *wps;
  1108. if (!wpa_s->ap_iface)
  1109. return -1;
  1110. hapd = wpa_s->ap_iface->bss[0];
  1111. wps = hapd->wps;
  1112. if (wpa_s->p2pdev->conf->wps_nfc_dh_pubkey == NULL ||
  1113. wpa_s->p2pdev->conf->wps_nfc_dh_privkey == NULL) {
  1114. wpa_printf(MSG_DEBUG, "P2P: No NFC DH key known");
  1115. return -1;
  1116. }
  1117. dh5_free(wps->dh_ctx);
  1118. wpabuf_free(wps->dh_pubkey);
  1119. wpabuf_free(wps->dh_privkey);
  1120. wps->dh_privkey = wpabuf_dup(
  1121. wpa_s->p2pdev->conf->wps_nfc_dh_privkey);
  1122. wps->dh_pubkey = wpabuf_dup(
  1123. wpa_s->p2pdev->conf->wps_nfc_dh_pubkey);
  1124. if (wps->dh_privkey == NULL || wps->dh_pubkey == NULL) {
  1125. wps->dh_ctx = NULL;
  1126. wpabuf_free(wps->dh_pubkey);
  1127. wps->dh_pubkey = NULL;
  1128. wpabuf_free(wps->dh_privkey);
  1129. wps->dh_privkey = NULL;
  1130. return -1;
  1131. }
  1132. wps->dh_ctx = dh5_init_fixed(wps->dh_privkey, wps->dh_pubkey);
  1133. if (wps->dh_ctx == NULL)
  1134. return -1;
  1135. return wps_registrar_add_nfc_pw_token(hapd->wps->registrar, pubkey_hash,
  1136. pw_id,
  1137. pw ? wpabuf_head(pw) : NULL,
  1138. pw ? wpabuf_len(pw) : 0, 1);
  1139. }
  1140. #endif /* CONFIG_WPS_NFC */
  1141. #ifdef CONFIG_CTRL_IFACE
  1142. int wpas_ap_stop_ap(struct wpa_supplicant *wpa_s)
  1143. {
  1144. struct hostapd_data *hapd;
  1145. if (!wpa_s->ap_iface)
  1146. return -1;
  1147. hapd = wpa_s->ap_iface->bss[0];
  1148. return hostapd_ctrl_iface_stop_ap(hapd);
  1149. }
  1150. int wpas_ap_pmksa_cache_list(struct wpa_supplicant *wpa_s, char *buf,
  1151. size_t len)
  1152. {
  1153. size_t reply_len = 0, i;
  1154. char ap_delimiter[] = "---- AP ----\n";
  1155. char mesh_delimiter[] = "---- mesh ----\n";
  1156. size_t dlen;
  1157. if (wpa_s->ap_iface) {
  1158. dlen = os_strlen(ap_delimiter);
  1159. if (dlen > len - reply_len)
  1160. return reply_len;
  1161. os_memcpy(&buf[reply_len], ap_delimiter, dlen);
  1162. reply_len += dlen;
  1163. for (i = 0; i < wpa_s->ap_iface->num_bss; i++) {
  1164. reply_len += hostapd_ctrl_iface_pmksa_list(
  1165. wpa_s->ap_iface->bss[i],
  1166. &buf[reply_len], len - reply_len);
  1167. }
  1168. }
  1169. if (wpa_s->ifmsh) {
  1170. dlen = os_strlen(mesh_delimiter);
  1171. if (dlen > len - reply_len)
  1172. return reply_len;
  1173. os_memcpy(&buf[reply_len], mesh_delimiter, dlen);
  1174. reply_len += dlen;
  1175. reply_len += hostapd_ctrl_iface_pmksa_list(
  1176. wpa_s->ifmsh->bss[0], &buf[reply_len],
  1177. len - reply_len);
  1178. }
  1179. return reply_len;
  1180. }
  1181. void wpas_ap_pmksa_cache_flush(struct wpa_supplicant *wpa_s)
  1182. {
  1183. size_t i;
  1184. if (wpa_s->ap_iface) {
  1185. for (i = 0; i < wpa_s->ap_iface->num_bss; i++)
  1186. hostapd_ctrl_iface_pmksa_flush(wpa_s->ap_iface->bss[i]);
  1187. }
  1188. if (wpa_s->ifmsh)
  1189. hostapd_ctrl_iface_pmksa_flush(wpa_s->ifmsh->bss[0]);
  1190. }
  1191. #endif /* CONFIG_CTRL_IFACE */
  1192. #ifdef NEED_AP_MLME
  1193. void wpas_event_dfs_radar_detected(struct wpa_supplicant *wpa_s,
  1194. struct dfs_event *radar)
  1195. {
  1196. if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0])
  1197. return;
  1198. wpa_printf(MSG_DEBUG, "DFS radar detected on %d MHz", radar->freq);
  1199. hostapd_dfs_radar_detected(wpa_s->ap_iface, radar->freq,
  1200. radar->ht_enabled, radar->chan_offset,
  1201. radar->chan_width,
  1202. radar->cf1, radar->cf2);
  1203. }
  1204. void wpas_event_dfs_cac_started(struct wpa_supplicant *wpa_s,
  1205. struct dfs_event *radar)
  1206. {
  1207. if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0])
  1208. return;
  1209. wpa_printf(MSG_DEBUG, "DFS CAC started on %d MHz", radar->freq);
  1210. hostapd_dfs_start_cac(wpa_s->ap_iface, radar->freq,
  1211. radar->ht_enabled, radar->chan_offset,
  1212. radar->chan_width, radar->cf1, radar->cf2);
  1213. }
  1214. void wpas_event_dfs_cac_finished(struct wpa_supplicant *wpa_s,
  1215. struct dfs_event *radar)
  1216. {
  1217. if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0])
  1218. return;
  1219. wpa_printf(MSG_DEBUG, "DFS CAC finished on %d MHz", radar->freq);
  1220. hostapd_dfs_complete_cac(wpa_s->ap_iface, 1, radar->freq,
  1221. radar->ht_enabled, radar->chan_offset,
  1222. radar->chan_width, radar->cf1, radar->cf2);
  1223. }
  1224. void wpas_event_dfs_cac_aborted(struct wpa_supplicant *wpa_s,
  1225. struct dfs_event *radar)
  1226. {
  1227. if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0])
  1228. return;
  1229. wpa_printf(MSG_DEBUG, "DFS CAC aborted on %d MHz", radar->freq);
  1230. hostapd_dfs_complete_cac(wpa_s->ap_iface, 0, radar->freq,
  1231. radar->ht_enabled, radar->chan_offset,
  1232. radar->chan_width, radar->cf1, radar->cf2);
  1233. }
  1234. void wpas_event_dfs_cac_nop_finished(struct wpa_supplicant *wpa_s,
  1235. struct dfs_event *radar)
  1236. {
  1237. if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0])
  1238. return;
  1239. wpa_printf(MSG_DEBUG, "DFS NOP finished on %d MHz", radar->freq);
  1240. hostapd_dfs_nop_finished(wpa_s->ap_iface, radar->freq,
  1241. radar->ht_enabled, radar->chan_offset,
  1242. radar->chan_width, radar->cf1, radar->cf2);
  1243. }
  1244. #endif /* NEED_AP_MLME */
  1245. void ap_periodic(struct wpa_supplicant *wpa_s)
  1246. {
  1247. if (wpa_s->ap_iface)
  1248. hostapd_periodic_iface(wpa_s->ap_iface);
  1249. }