ap.c 40 KB

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