ap.c 41 KB

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