ap.c 40 KB

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