ap.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594
  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. bss->disassoc_low_ack = wpa_s->conf->disassoc_low_ack;
  451. if (wpa_s->conf->ap_vendor_elements) {
  452. bss->vendor_elements =
  453. wpabuf_dup(wpa_s->conf->ap_vendor_elements);
  454. }
  455. bss->ftm_responder = wpa_s->conf->ftm_responder;
  456. bss->ftm_initiator = wpa_s->conf->ftm_initiator;
  457. return 0;
  458. }
  459. static void ap_public_action_rx(void *ctx, const u8 *buf, size_t len, int freq)
  460. {
  461. #ifdef CONFIG_P2P
  462. struct wpa_supplicant *wpa_s = ctx;
  463. const struct ieee80211_mgmt *mgmt;
  464. mgmt = (const struct ieee80211_mgmt *) buf;
  465. if (len < IEEE80211_HDRLEN + 1)
  466. return;
  467. if (mgmt->u.action.category != WLAN_ACTION_PUBLIC)
  468. return;
  469. wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
  470. mgmt->u.action.category,
  471. buf + IEEE80211_HDRLEN + 1,
  472. len - IEEE80211_HDRLEN - 1, freq);
  473. #endif /* CONFIG_P2P */
  474. }
  475. static void ap_wps_event_cb(void *ctx, enum wps_event event,
  476. union wps_event_data *data)
  477. {
  478. #ifdef CONFIG_P2P
  479. struct wpa_supplicant *wpa_s = ctx;
  480. if (event == WPS_EV_FAIL) {
  481. struct wps_event_fail *fail = &data->fail;
  482. if (wpa_s->p2pdev && wpa_s->p2pdev != wpa_s &&
  483. wpa_s == wpa_s->global->p2p_group_formation) {
  484. /*
  485. * src/ap/wps_hostapd.c has already sent this on the
  486. * main interface, so only send on the parent interface
  487. * here if needed.
  488. */
  489. wpa_msg(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_FAIL
  490. "msg=%d config_error=%d",
  491. fail->msg, fail->config_error);
  492. }
  493. wpas_p2p_wps_failed(wpa_s, fail);
  494. }
  495. #endif /* CONFIG_P2P */
  496. }
  497. static void ap_sta_authorized_cb(void *ctx, const u8 *mac_addr,
  498. int authorized, const u8 *p2p_dev_addr)
  499. {
  500. wpas_notify_sta_authorized(ctx, mac_addr, authorized, p2p_dev_addr);
  501. }
  502. #ifdef CONFIG_P2P
  503. static void ap_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *p2p_dev_addr,
  504. const u8 *psk, size_t psk_len)
  505. {
  506. struct wpa_supplicant *wpa_s = ctx;
  507. if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL)
  508. return;
  509. wpas_p2p_new_psk_cb(wpa_s, mac_addr, p2p_dev_addr, psk, psk_len);
  510. }
  511. #endif /* CONFIG_P2P */
  512. static int ap_vendor_action_rx(void *ctx, const u8 *buf, size_t len, int freq)
  513. {
  514. #ifdef CONFIG_P2P
  515. struct wpa_supplicant *wpa_s = ctx;
  516. const struct ieee80211_mgmt *mgmt;
  517. mgmt = (const struct ieee80211_mgmt *) buf;
  518. if (len < IEEE80211_HDRLEN + 1)
  519. return -1;
  520. wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
  521. mgmt->u.action.category,
  522. buf + IEEE80211_HDRLEN + 1,
  523. len - IEEE80211_HDRLEN - 1, freq);
  524. #endif /* CONFIG_P2P */
  525. return 0;
  526. }
  527. static int ap_probe_req_rx(void *ctx, const u8 *sa, const u8 *da,
  528. const u8 *bssid, const u8 *ie, size_t ie_len,
  529. int ssi_signal)
  530. {
  531. struct wpa_supplicant *wpa_s = ctx;
  532. unsigned int freq = 0;
  533. if (wpa_s->ap_iface)
  534. freq = wpa_s->ap_iface->freq;
  535. return wpas_p2p_probe_req_rx(wpa_s, sa, da, bssid, ie, ie_len,
  536. freq, ssi_signal);
  537. }
  538. static void ap_wps_reg_success_cb(void *ctx, const u8 *mac_addr,
  539. const u8 *uuid_e)
  540. {
  541. struct wpa_supplicant *wpa_s = ctx;
  542. wpas_p2p_wps_success(wpa_s, mac_addr, 1);
  543. }
  544. static void wpas_ap_configured_cb(void *ctx)
  545. {
  546. struct wpa_supplicant *wpa_s = ctx;
  547. #ifdef CONFIG_ACS
  548. if (wpa_s->current_ssid && wpa_s->current_ssid->acs) {
  549. wpa_s->assoc_freq = wpa_s->ap_iface->freq;
  550. wpa_s->current_ssid->frequency = wpa_s->ap_iface->freq;
  551. }
  552. #endif /* CONFIG_ACS */
  553. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  554. if (wpa_s->ap_configured_cb)
  555. wpa_s->ap_configured_cb(wpa_s->ap_configured_cb_ctx,
  556. wpa_s->ap_configured_cb_data);
  557. }
  558. int wpa_supplicant_create_ap(struct wpa_supplicant *wpa_s,
  559. struct wpa_ssid *ssid)
  560. {
  561. struct wpa_driver_associate_params params;
  562. struct hostapd_iface *hapd_iface;
  563. struct hostapd_config *conf;
  564. size_t i;
  565. if (ssid->ssid == NULL || ssid->ssid_len == 0) {
  566. wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
  567. return -1;
  568. }
  569. wpa_supplicant_ap_deinit(wpa_s);
  570. wpa_printf(MSG_DEBUG, "Setting up AP (SSID='%s')",
  571. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  572. os_memset(&params, 0, sizeof(params));
  573. params.ssid = ssid->ssid;
  574. params.ssid_len = ssid->ssid_len;
  575. switch (ssid->mode) {
  576. case WPAS_MODE_AP:
  577. case WPAS_MODE_P2P_GO:
  578. case WPAS_MODE_P2P_GROUP_FORMATION:
  579. params.mode = IEEE80211_MODE_AP;
  580. break;
  581. default:
  582. return -1;
  583. }
  584. if (ssid->frequency == 0)
  585. ssid->frequency = 2462; /* default channel 11 */
  586. params.freq.freq = ssid->frequency;
  587. params.wpa_proto = ssid->proto;
  588. if (ssid->key_mgmt & WPA_KEY_MGMT_PSK)
  589. wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
  590. else
  591. wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
  592. params.key_mgmt_suite = wpa_s->key_mgmt;
  593. wpa_s->pairwise_cipher = wpa_pick_pairwise_cipher(ssid->pairwise_cipher,
  594. 1);
  595. if (wpa_s->pairwise_cipher < 0) {
  596. wpa_printf(MSG_WARNING, "WPA: Failed to select pairwise "
  597. "cipher.");
  598. return -1;
  599. }
  600. params.pairwise_suite = wpa_s->pairwise_cipher;
  601. params.group_suite = params.pairwise_suite;
  602. #ifdef CONFIG_P2P
  603. if (ssid->mode == WPAS_MODE_P2P_GO ||
  604. ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
  605. params.p2p = 1;
  606. #endif /* CONFIG_P2P */
  607. if (wpa_s->p2pdev->set_ap_uapsd)
  608. params.uapsd = wpa_s->p2pdev->ap_uapsd;
  609. else if (params.p2p && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_UAPSD))
  610. params.uapsd = 1; /* mandatory for P2P GO */
  611. else
  612. params.uapsd = -1;
  613. if (ieee80211_is_dfs(params.freq.freq))
  614. params.freq.freq = 0; /* set channel after CAC */
  615. if (params.p2p)
  616. wpa_drv_get_ext_capa(wpa_s, WPA_IF_P2P_GO);
  617. else
  618. wpa_drv_get_ext_capa(wpa_s, WPA_IF_AP_BSS);
  619. if (wpa_drv_associate(wpa_s, &params) < 0) {
  620. wpa_msg(wpa_s, MSG_INFO, "Failed to start AP functionality");
  621. return -1;
  622. }
  623. wpa_s->ap_iface = hapd_iface = hostapd_alloc_iface();
  624. if (hapd_iface == NULL)
  625. return -1;
  626. hapd_iface->owner = wpa_s;
  627. hapd_iface->drv_flags = wpa_s->drv_flags;
  628. hapd_iface->smps_modes = wpa_s->drv_smps_modes;
  629. hapd_iface->probe_resp_offloads = wpa_s->probe_resp_offloads;
  630. hapd_iface->extended_capa = wpa_s->extended_capa;
  631. hapd_iface->extended_capa_mask = wpa_s->extended_capa_mask;
  632. hapd_iface->extended_capa_len = wpa_s->extended_capa_len;
  633. wpa_s->ap_iface->conf = conf = hostapd_config_defaults();
  634. if (conf == NULL) {
  635. wpa_supplicant_ap_deinit(wpa_s);
  636. return -1;
  637. }
  638. os_memcpy(wpa_s->ap_iface->conf->wmm_ac_params,
  639. wpa_s->conf->wmm_ac_params,
  640. sizeof(wpa_s->conf->wmm_ac_params));
  641. if (params.uapsd > 0) {
  642. conf->bss[0]->wmm_enabled = 1;
  643. conf->bss[0]->wmm_uapsd = 1;
  644. }
  645. if (wpa_supplicant_conf_ap(wpa_s, ssid, conf)) {
  646. wpa_printf(MSG_ERROR, "Failed to create AP configuration");
  647. wpa_supplicant_ap_deinit(wpa_s);
  648. return -1;
  649. }
  650. #ifdef CONFIG_P2P
  651. if (ssid->mode == WPAS_MODE_P2P_GO)
  652. conf->bss[0]->p2p = P2P_ENABLED | P2P_GROUP_OWNER;
  653. else if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
  654. conf->bss[0]->p2p = P2P_ENABLED | P2P_GROUP_OWNER |
  655. P2P_GROUP_FORMATION;
  656. #endif /* CONFIG_P2P */
  657. hapd_iface->num_bss = conf->num_bss;
  658. hapd_iface->bss = os_calloc(conf->num_bss,
  659. sizeof(struct hostapd_data *));
  660. if (hapd_iface->bss == NULL) {
  661. wpa_supplicant_ap_deinit(wpa_s);
  662. return -1;
  663. }
  664. for (i = 0; i < conf->num_bss; i++) {
  665. hapd_iface->bss[i] =
  666. hostapd_alloc_bss_data(hapd_iface, conf,
  667. conf->bss[i]);
  668. if (hapd_iface->bss[i] == NULL) {
  669. wpa_supplicant_ap_deinit(wpa_s);
  670. return -1;
  671. }
  672. hapd_iface->bss[i]->msg_ctx = wpa_s;
  673. hapd_iface->bss[i]->msg_ctx_parent = wpa_s->p2pdev;
  674. hapd_iface->bss[i]->public_action_cb = ap_public_action_rx;
  675. hapd_iface->bss[i]->public_action_cb_ctx = wpa_s;
  676. hapd_iface->bss[i]->vendor_action_cb = ap_vendor_action_rx;
  677. hapd_iface->bss[i]->vendor_action_cb_ctx = wpa_s;
  678. hostapd_register_probereq_cb(hapd_iface->bss[i],
  679. ap_probe_req_rx, wpa_s);
  680. hapd_iface->bss[i]->wps_reg_success_cb = ap_wps_reg_success_cb;
  681. hapd_iface->bss[i]->wps_reg_success_cb_ctx = wpa_s;
  682. hapd_iface->bss[i]->wps_event_cb = ap_wps_event_cb;
  683. hapd_iface->bss[i]->wps_event_cb_ctx = wpa_s;
  684. hapd_iface->bss[i]->sta_authorized_cb = ap_sta_authorized_cb;
  685. hapd_iface->bss[i]->sta_authorized_cb_ctx = wpa_s;
  686. #ifdef CONFIG_P2P
  687. hapd_iface->bss[i]->new_psk_cb = ap_new_psk_cb;
  688. hapd_iface->bss[i]->new_psk_cb_ctx = wpa_s;
  689. hapd_iface->bss[i]->p2p = wpa_s->global->p2p;
  690. hapd_iface->bss[i]->p2p_group = wpas_p2p_group_init(wpa_s,
  691. ssid);
  692. #endif /* CONFIG_P2P */
  693. hapd_iface->bss[i]->setup_complete_cb = wpas_ap_configured_cb;
  694. hapd_iface->bss[i]->setup_complete_cb_ctx = wpa_s;
  695. #ifdef CONFIG_TESTING_OPTIONS
  696. hapd_iface->bss[i]->ext_eapol_frame_io =
  697. wpa_s->ext_eapol_frame_io;
  698. #endif /* CONFIG_TESTING_OPTIONS */
  699. }
  700. os_memcpy(hapd_iface->bss[0]->own_addr, wpa_s->own_addr, ETH_ALEN);
  701. hapd_iface->bss[0]->driver = wpa_s->driver;
  702. hapd_iface->bss[0]->drv_priv = wpa_s->drv_priv;
  703. wpa_s->current_ssid = ssid;
  704. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  705. os_memcpy(wpa_s->bssid, wpa_s->own_addr, ETH_ALEN);
  706. wpa_s->assoc_freq = ssid->frequency;
  707. #if defined(CONFIG_P2P) && defined(CONFIG_ACS)
  708. if (wpa_s->p2p_go_do_acs) {
  709. wpa_s->ap_iface->conf->channel = 0;
  710. wpa_s->ap_iface->conf->hw_mode = wpa_s->p2p_go_acs_band;
  711. ssid->acs = 1;
  712. }
  713. #endif /* CONFIG_P2P && CONFIG_ACS */
  714. if (hostapd_setup_interface(wpa_s->ap_iface)) {
  715. wpa_printf(MSG_ERROR, "Failed to initialize AP interface");
  716. wpa_supplicant_ap_deinit(wpa_s);
  717. return -1;
  718. }
  719. return 0;
  720. }
  721. void wpa_supplicant_ap_deinit(struct wpa_supplicant *wpa_s)
  722. {
  723. #ifdef CONFIG_WPS
  724. eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
  725. #endif /* CONFIG_WPS */
  726. if (wpa_s->ap_iface == NULL)
  727. return;
  728. wpa_s->current_ssid = NULL;
  729. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  730. wpa_s->assoc_freq = 0;
  731. wpas_p2p_ap_deinit(wpa_s);
  732. wpa_s->ap_iface->driver_ap_teardown =
  733. !!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
  734. hostapd_interface_deinit(wpa_s->ap_iface);
  735. hostapd_interface_free(wpa_s->ap_iface);
  736. wpa_s->ap_iface = NULL;
  737. wpa_drv_deinit_ap(wpa_s);
  738. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
  739. " reason=%d locally_generated=1",
  740. MAC2STR(wpa_s->own_addr), WLAN_REASON_DEAUTH_LEAVING);
  741. }
  742. void ap_tx_status(void *ctx, const u8 *addr,
  743. const u8 *buf, size_t len, int ack)
  744. {
  745. #ifdef NEED_AP_MLME
  746. struct wpa_supplicant *wpa_s = ctx;
  747. hostapd_tx_status(wpa_s->ap_iface->bss[0], addr, buf, len, ack);
  748. #endif /* NEED_AP_MLME */
  749. }
  750. void ap_eapol_tx_status(void *ctx, const u8 *dst,
  751. const u8 *data, size_t len, int ack)
  752. {
  753. #ifdef NEED_AP_MLME
  754. struct wpa_supplicant *wpa_s = ctx;
  755. if (!wpa_s->ap_iface)
  756. return;
  757. hostapd_tx_status(wpa_s->ap_iface->bss[0], dst, data, len, ack);
  758. #endif /* NEED_AP_MLME */
  759. }
  760. void ap_client_poll_ok(void *ctx, const u8 *addr)
  761. {
  762. #ifdef NEED_AP_MLME
  763. struct wpa_supplicant *wpa_s = ctx;
  764. if (wpa_s->ap_iface)
  765. hostapd_client_poll_ok(wpa_s->ap_iface->bss[0], addr);
  766. #endif /* NEED_AP_MLME */
  767. }
  768. void ap_rx_from_unknown_sta(void *ctx, const u8 *addr, int wds)
  769. {
  770. #ifdef NEED_AP_MLME
  771. struct wpa_supplicant *wpa_s = ctx;
  772. ieee802_11_rx_from_unknown(wpa_s->ap_iface->bss[0], addr, wds);
  773. #endif /* NEED_AP_MLME */
  774. }
  775. void ap_mgmt_rx(void *ctx, struct rx_mgmt *rx_mgmt)
  776. {
  777. #ifdef NEED_AP_MLME
  778. struct wpa_supplicant *wpa_s = ctx;
  779. struct hostapd_frame_info fi;
  780. os_memset(&fi, 0, sizeof(fi));
  781. fi.datarate = rx_mgmt->datarate;
  782. fi.ssi_signal = rx_mgmt->ssi_signal;
  783. ieee802_11_mgmt(wpa_s->ap_iface->bss[0], rx_mgmt->frame,
  784. rx_mgmt->frame_len, &fi);
  785. #endif /* NEED_AP_MLME */
  786. }
  787. void ap_mgmt_tx_cb(void *ctx, const u8 *buf, size_t len, u16 stype, int ok)
  788. {
  789. #ifdef NEED_AP_MLME
  790. struct wpa_supplicant *wpa_s = ctx;
  791. ieee802_11_mgmt_cb(wpa_s->ap_iface->bss[0], buf, len, stype, ok);
  792. #endif /* NEED_AP_MLME */
  793. }
  794. void wpa_supplicant_ap_rx_eapol(struct wpa_supplicant *wpa_s,
  795. const u8 *src_addr, const u8 *buf, size_t len)
  796. {
  797. ieee802_1x_receive(wpa_s->ap_iface->bss[0], src_addr, buf, len);
  798. }
  799. #ifdef CONFIG_WPS
  800. int wpa_supplicant_ap_wps_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid,
  801. const u8 *p2p_dev_addr)
  802. {
  803. if (!wpa_s->ap_iface)
  804. return -1;
  805. return hostapd_wps_button_pushed(wpa_s->ap_iface->bss[0],
  806. p2p_dev_addr);
  807. }
  808. int wpa_supplicant_ap_wps_cancel(struct wpa_supplicant *wpa_s)
  809. {
  810. struct wps_registrar *reg;
  811. int reg_sel = 0, wps_sta = 0;
  812. if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0]->wps)
  813. return -1;
  814. reg = wpa_s->ap_iface->bss[0]->wps->registrar;
  815. reg_sel = wps_registrar_wps_cancel(reg);
  816. wps_sta = ap_for_each_sta(wpa_s->ap_iface->bss[0],
  817. ap_sta_wps_cancel, NULL);
  818. if (!reg_sel && !wps_sta) {
  819. wpa_printf(MSG_DEBUG, "No WPS operation in progress at this "
  820. "time");
  821. return -1;
  822. }
  823. /*
  824. * There are 2 cases to return wps cancel as success:
  825. * 1. When wps cancel was initiated but no connection has been
  826. * established with client yet.
  827. * 2. Client is in the middle of exchanging WPS messages.
  828. */
  829. return 0;
  830. }
  831. int wpa_supplicant_ap_wps_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
  832. const char *pin, char *buf, size_t buflen,
  833. int timeout)
  834. {
  835. int ret, ret_len = 0;
  836. if (!wpa_s->ap_iface)
  837. return -1;
  838. if (pin == NULL) {
  839. unsigned int rpin;
  840. if (wps_generate_pin(&rpin) < 0)
  841. return -1;
  842. ret_len = os_snprintf(buf, buflen, "%08d", rpin);
  843. if (os_snprintf_error(buflen, ret_len))
  844. return -1;
  845. pin = buf;
  846. } else if (buf) {
  847. ret_len = os_snprintf(buf, buflen, "%s", pin);
  848. if (os_snprintf_error(buflen, ret_len))
  849. return -1;
  850. }
  851. ret = hostapd_wps_add_pin(wpa_s->ap_iface->bss[0], bssid, "any", pin,
  852. timeout);
  853. if (ret)
  854. return -1;
  855. return ret_len;
  856. }
  857. static void wpas_wps_ap_pin_timeout(void *eloop_data, void *user_ctx)
  858. {
  859. struct wpa_supplicant *wpa_s = eloop_data;
  860. wpa_printf(MSG_DEBUG, "WPS: AP PIN timed out");
  861. wpas_wps_ap_pin_disable(wpa_s);
  862. }
  863. static void wpas_wps_ap_pin_enable(struct wpa_supplicant *wpa_s, int timeout)
  864. {
  865. struct hostapd_data *hapd;
  866. if (wpa_s->ap_iface == NULL)
  867. return;
  868. hapd = wpa_s->ap_iface->bss[0];
  869. wpa_printf(MSG_DEBUG, "WPS: Enabling AP PIN (timeout=%d)", timeout);
  870. hapd->ap_pin_failures = 0;
  871. eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
  872. if (timeout > 0)
  873. eloop_register_timeout(timeout, 0,
  874. wpas_wps_ap_pin_timeout, wpa_s, NULL);
  875. }
  876. void wpas_wps_ap_pin_disable(struct wpa_supplicant *wpa_s)
  877. {
  878. struct hostapd_data *hapd;
  879. if (wpa_s->ap_iface == NULL)
  880. return;
  881. wpa_printf(MSG_DEBUG, "WPS: Disabling AP PIN");
  882. hapd = wpa_s->ap_iface->bss[0];
  883. os_free(hapd->conf->ap_pin);
  884. hapd->conf->ap_pin = NULL;
  885. eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
  886. }
  887. const char * wpas_wps_ap_pin_random(struct wpa_supplicant *wpa_s, int timeout)
  888. {
  889. struct hostapd_data *hapd;
  890. unsigned int pin;
  891. char pin_txt[9];
  892. if (wpa_s->ap_iface == NULL)
  893. return NULL;
  894. hapd = wpa_s->ap_iface->bss[0];
  895. if (wps_generate_pin(&pin) < 0)
  896. return NULL;
  897. os_snprintf(pin_txt, sizeof(pin_txt), "%08u", pin);
  898. os_free(hapd->conf->ap_pin);
  899. hapd->conf->ap_pin = os_strdup(pin_txt);
  900. if (hapd->conf->ap_pin == NULL)
  901. return NULL;
  902. wpas_wps_ap_pin_enable(wpa_s, timeout);
  903. return hapd->conf->ap_pin;
  904. }
  905. const char * wpas_wps_ap_pin_get(struct wpa_supplicant *wpa_s)
  906. {
  907. struct hostapd_data *hapd;
  908. if (wpa_s->ap_iface == NULL)
  909. return NULL;
  910. hapd = wpa_s->ap_iface->bss[0];
  911. return hapd->conf->ap_pin;
  912. }
  913. int wpas_wps_ap_pin_set(struct wpa_supplicant *wpa_s, const char *pin,
  914. int timeout)
  915. {
  916. struct hostapd_data *hapd;
  917. char pin_txt[9];
  918. int ret;
  919. if (wpa_s->ap_iface == NULL)
  920. return -1;
  921. hapd = wpa_s->ap_iface->bss[0];
  922. ret = os_snprintf(pin_txt, sizeof(pin_txt), "%s", pin);
  923. if (os_snprintf_error(sizeof(pin_txt), ret))
  924. return -1;
  925. os_free(hapd->conf->ap_pin);
  926. hapd->conf->ap_pin = os_strdup(pin_txt);
  927. if (hapd->conf->ap_pin == NULL)
  928. return -1;
  929. wpas_wps_ap_pin_enable(wpa_s, timeout);
  930. return 0;
  931. }
  932. void wpa_supplicant_ap_pwd_auth_fail(struct wpa_supplicant *wpa_s)
  933. {
  934. struct hostapd_data *hapd;
  935. if (wpa_s->ap_iface == NULL)
  936. return;
  937. hapd = wpa_s->ap_iface->bss[0];
  938. /*
  939. * Registrar failed to prove its knowledge of the AP PIN. Disable AP
  940. * PIN if this happens multiple times to slow down brute force attacks.
  941. */
  942. hapd->ap_pin_failures++;
  943. wpa_printf(MSG_DEBUG, "WPS: AP PIN authentication failure number %u",
  944. hapd->ap_pin_failures);
  945. if (hapd->ap_pin_failures < 3)
  946. return;
  947. wpa_printf(MSG_DEBUG, "WPS: Disable AP PIN");
  948. hapd->ap_pin_failures = 0;
  949. os_free(hapd->conf->ap_pin);
  950. hapd->conf->ap_pin = NULL;
  951. }
  952. #ifdef CONFIG_WPS_NFC
  953. struct wpabuf * wpas_ap_wps_nfc_config_token(struct wpa_supplicant *wpa_s,
  954. int ndef)
  955. {
  956. struct hostapd_data *hapd;
  957. if (wpa_s->ap_iface == NULL)
  958. return NULL;
  959. hapd = wpa_s->ap_iface->bss[0];
  960. return hostapd_wps_nfc_config_token(hapd, ndef);
  961. }
  962. struct wpabuf * wpas_ap_wps_nfc_handover_sel(struct wpa_supplicant *wpa_s,
  963. int ndef)
  964. {
  965. struct hostapd_data *hapd;
  966. if (wpa_s->ap_iface == NULL)
  967. return NULL;
  968. hapd = wpa_s->ap_iface->bss[0];
  969. return hostapd_wps_nfc_hs_cr(hapd, ndef);
  970. }
  971. int wpas_ap_wps_nfc_report_handover(struct wpa_supplicant *wpa_s,
  972. const struct wpabuf *req,
  973. const struct wpabuf *sel)
  974. {
  975. struct hostapd_data *hapd;
  976. if (wpa_s->ap_iface == NULL)
  977. return -1;
  978. hapd = wpa_s->ap_iface->bss[0];
  979. return hostapd_wps_nfc_report_handover(hapd, req, sel);
  980. }
  981. #endif /* CONFIG_WPS_NFC */
  982. #endif /* CONFIG_WPS */
  983. #ifdef CONFIG_CTRL_IFACE
  984. int ap_ctrl_iface_sta_first(struct wpa_supplicant *wpa_s,
  985. char *buf, size_t buflen)
  986. {
  987. struct hostapd_data *hapd;
  988. if (wpa_s->ap_iface)
  989. hapd = wpa_s->ap_iface->bss[0];
  990. else if (wpa_s->ifmsh)
  991. hapd = wpa_s->ifmsh->bss[0];
  992. else
  993. return -1;
  994. return hostapd_ctrl_iface_sta_first(hapd, buf, buflen);
  995. }
  996. int ap_ctrl_iface_sta(struct wpa_supplicant *wpa_s, const char *txtaddr,
  997. char *buf, size_t buflen)
  998. {
  999. struct hostapd_data *hapd;
  1000. if (wpa_s->ap_iface)
  1001. hapd = wpa_s->ap_iface->bss[0];
  1002. else if (wpa_s->ifmsh)
  1003. hapd = wpa_s->ifmsh->bss[0];
  1004. else
  1005. return -1;
  1006. return hostapd_ctrl_iface_sta(hapd, txtaddr, buf, buflen);
  1007. }
  1008. int ap_ctrl_iface_sta_next(struct wpa_supplicant *wpa_s, const char *txtaddr,
  1009. char *buf, size_t buflen)
  1010. {
  1011. struct hostapd_data *hapd;
  1012. if (wpa_s->ap_iface)
  1013. hapd = wpa_s->ap_iface->bss[0];
  1014. else if (wpa_s->ifmsh)
  1015. hapd = wpa_s->ifmsh->bss[0];
  1016. else
  1017. return -1;
  1018. return hostapd_ctrl_iface_sta_next(hapd, txtaddr, buf, buflen);
  1019. }
  1020. int ap_ctrl_iface_sta_disassociate(struct wpa_supplicant *wpa_s,
  1021. const char *txtaddr)
  1022. {
  1023. if (wpa_s->ap_iface == NULL)
  1024. return -1;
  1025. return hostapd_ctrl_iface_disassociate(wpa_s->ap_iface->bss[0],
  1026. txtaddr);
  1027. }
  1028. int ap_ctrl_iface_sta_deauthenticate(struct wpa_supplicant *wpa_s,
  1029. const char *txtaddr)
  1030. {
  1031. if (wpa_s->ap_iface == NULL)
  1032. return -1;
  1033. return hostapd_ctrl_iface_deauthenticate(wpa_s->ap_iface->bss[0],
  1034. txtaddr);
  1035. }
  1036. int ap_ctrl_iface_wpa_get_status(struct wpa_supplicant *wpa_s, char *buf,
  1037. size_t buflen, int verbose)
  1038. {
  1039. char *pos = buf, *end = buf + buflen;
  1040. int ret;
  1041. struct hostapd_bss_config *conf;
  1042. if (wpa_s->ap_iface == NULL)
  1043. return -1;
  1044. conf = wpa_s->ap_iface->bss[0]->conf;
  1045. if (conf->wpa == 0)
  1046. return 0;
  1047. ret = os_snprintf(pos, end - pos,
  1048. "pairwise_cipher=%s\n"
  1049. "group_cipher=%s\n"
  1050. "key_mgmt=%s\n",
  1051. wpa_cipher_txt(conf->rsn_pairwise),
  1052. wpa_cipher_txt(conf->wpa_group),
  1053. wpa_key_mgmt_txt(conf->wpa_key_mgmt,
  1054. conf->wpa));
  1055. if (os_snprintf_error(end - pos, ret))
  1056. return pos - buf;
  1057. pos += ret;
  1058. return pos - buf;
  1059. }
  1060. #endif /* CONFIG_CTRL_IFACE */
  1061. int wpa_supplicant_ap_update_beacon(struct wpa_supplicant *wpa_s)
  1062. {
  1063. struct hostapd_iface *iface = wpa_s->ap_iface;
  1064. struct wpa_ssid *ssid = wpa_s->current_ssid;
  1065. struct hostapd_data *hapd;
  1066. if (ssid == NULL || wpa_s->ap_iface == NULL ||
  1067. ssid->mode == WPAS_MODE_INFRA ||
  1068. ssid->mode == WPAS_MODE_IBSS)
  1069. return -1;
  1070. #ifdef CONFIG_P2P
  1071. if (ssid->mode == WPAS_MODE_P2P_GO)
  1072. iface->conf->bss[0]->p2p = P2P_ENABLED | P2P_GROUP_OWNER;
  1073. else if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
  1074. iface->conf->bss[0]->p2p = P2P_ENABLED | P2P_GROUP_OWNER |
  1075. P2P_GROUP_FORMATION;
  1076. #endif /* CONFIG_P2P */
  1077. hapd = iface->bss[0];
  1078. if (hapd->drv_priv == NULL)
  1079. return -1;
  1080. ieee802_11_set_beacons(iface);
  1081. hostapd_set_ap_wps_ie(hapd);
  1082. return 0;
  1083. }
  1084. int ap_switch_channel(struct wpa_supplicant *wpa_s,
  1085. struct csa_settings *settings)
  1086. {
  1087. #ifdef NEED_AP_MLME
  1088. if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0])
  1089. return -1;
  1090. return hostapd_switch_channel(wpa_s->ap_iface->bss[0], settings);
  1091. #else /* NEED_AP_MLME */
  1092. return -1;
  1093. #endif /* NEED_AP_MLME */
  1094. }
  1095. #ifdef CONFIG_CTRL_IFACE
  1096. int ap_ctrl_iface_chanswitch(struct wpa_supplicant *wpa_s, const char *pos)
  1097. {
  1098. struct csa_settings settings;
  1099. int ret = hostapd_parse_csa_settings(pos, &settings);
  1100. if (ret)
  1101. return ret;
  1102. return ap_switch_channel(wpa_s, &settings);
  1103. }
  1104. #endif /* CONFIG_CTRL_IFACE */
  1105. void wpas_ap_ch_switch(struct wpa_supplicant *wpa_s, int freq, int ht,
  1106. int offset, int width, int cf1, int cf2)
  1107. {
  1108. if (!wpa_s->ap_iface)
  1109. return;
  1110. wpa_s->assoc_freq = freq;
  1111. if (wpa_s->current_ssid)
  1112. wpa_s->current_ssid->frequency = freq;
  1113. hostapd_event_ch_switch(wpa_s->ap_iface->bss[0], freq, ht,
  1114. offset, width, cf1, cf2);
  1115. }
  1116. int wpa_supplicant_ap_mac_addr_filter(struct wpa_supplicant *wpa_s,
  1117. const u8 *addr)
  1118. {
  1119. struct hostapd_data *hapd;
  1120. struct hostapd_bss_config *conf;
  1121. if (!wpa_s->ap_iface)
  1122. return -1;
  1123. if (addr)
  1124. wpa_printf(MSG_DEBUG, "AP: Set MAC address filter: " MACSTR,
  1125. MAC2STR(addr));
  1126. else
  1127. wpa_printf(MSG_DEBUG, "AP: Clear MAC address filter");
  1128. hapd = wpa_s->ap_iface->bss[0];
  1129. conf = hapd->conf;
  1130. os_free(conf->accept_mac);
  1131. conf->accept_mac = NULL;
  1132. conf->num_accept_mac = 0;
  1133. os_free(conf->deny_mac);
  1134. conf->deny_mac = NULL;
  1135. conf->num_deny_mac = 0;
  1136. if (addr == NULL) {
  1137. conf->macaddr_acl = ACCEPT_UNLESS_DENIED;
  1138. return 0;
  1139. }
  1140. conf->macaddr_acl = DENY_UNLESS_ACCEPTED;
  1141. conf->accept_mac = os_zalloc(sizeof(struct mac_acl_entry));
  1142. if (conf->accept_mac == NULL)
  1143. return -1;
  1144. os_memcpy(conf->accept_mac[0].addr, addr, ETH_ALEN);
  1145. conf->num_accept_mac = 1;
  1146. return 0;
  1147. }
  1148. #ifdef CONFIG_WPS_NFC
  1149. int wpas_ap_wps_add_nfc_pw(struct wpa_supplicant *wpa_s, u16 pw_id,
  1150. const struct wpabuf *pw, const u8 *pubkey_hash)
  1151. {
  1152. struct hostapd_data *hapd;
  1153. struct wps_context *wps;
  1154. if (!wpa_s->ap_iface)
  1155. return -1;
  1156. hapd = wpa_s->ap_iface->bss[0];
  1157. wps = hapd->wps;
  1158. if (wpa_s->p2pdev->conf->wps_nfc_dh_pubkey == NULL ||
  1159. wpa_s->p2pdev->conf->wps_nfc_dh_privkey == NULL) {
  1160. wpa_printf(MSG_DEBUG, "P2P: No NFC DH key known");
  1161. return -1;
  1162. }
  1163. dh5_free(wps->dh_ctx);
  1164. wpabuf_free(wps->dh_pubkey);
  1165. wpabuf_free(wps->dh_privkey);
  1166. wps->dh_privkey = wpabuf_dup(
  1167. wpa_s->p2pdev->conf->wps_nfc_dh_privkey);
  1168. wps->dh_pubkey = wpabuf_dup(
  1169. wpa_s->p2pdev->conf->wps_nfc_dh_pubkey);
  1170. if (wps->dh_privkey == NULL || wps->dh_pubkey == NULL) {
  1171. wps->dh_ctx = NULL;
  1172. wpabuf_free(wps->dh_pubkey);
  1173. wps->dh_pubkey = NULL;
  1174. wpabuf_free(wps->dh_privkey);
  1175. wps->dh_privkey = NULL;
  1176. return -1;
  1177. }
  1178. wps->dh_ctx = dh5_init_fixed(wps->dh_privkey, wps->dh_pubkey);
  1179. if (wps->dh_ctx == NULL)
  1180. return -1;
  1181. return wps_registrar_add_nfc_pw_token(hapd->wps->registrar, pubkey_hash,
  1182. pw_id,
  1183. pw ? wpabuf_head(pw) : NULL,
  1184. pw ? wpabuf_len(pw) : 0, 1);
  1185. }
  1186. #endif /* CONFIG_WPS_NFC */
  1187. #ifdef CONFIG_CTRL_IFACE
  1188. int wpas_ap_stop_ap(struct wpa_supplicant *wpa_s)
  1189. {
  1190. struct hostapd_data *hapd;
  1191. if (!wpa_s->ap_iface)
  1192. return -1;
  1193. hapd = wpa_s->ap_iface->bss[0];
  1194. return hostapd_ctrl_iface_stop_ap(hapd);
  1195. }
  1196. int wpas_ap_pmksa_cache_list(struct wpa_supplicant *wpa_s, char *buf,
  1197. size_t len)
  1198. {
  1199. size_t reply_len = 0, i;
  1200. char ap_delimiter[] = "---- AP ----\n";
  1201. char mesh_delimiter[] = "---- mesh ----\n";
  1202. size_t dlen;
  1203. if (wpa_s->ap_iface) {
  1204. dlen = os_strlen(ap_delimiter);
  1205. if (dlen > len - reply_len)
  1206. return reply_len;
  1207. os_memcpy(&buf[reply_len], ap_delimiter, dlen);
  1208. reply_len += dlen;
  1209. for (i = 0; i < wpa_s->ap_iface->num_bss; i++) {
  1210. reply_len += hostapd_ctrl_iface_pmksa_list(
  1211. wpa_s->ap_iface->bss[i],
  1212. &buf[reply_len], len - reply_len);
  1213. }
  1214. }
  1215. if (wpa_s->ifmsh) {
  1216. dlen = os_strlen(mesh_delimiter);
  1217. if (dlen > len - reply_len)
  1218. return reply_len;
  1219. os_memcpy(&buf[reply_len], mesh_delimiter, dlen);
  1220. reply_len += dlen;
  1221. reply_len += hostapd_ctrl_iface_pmksa_list(
  1222. wpa_s->ifmsh->bss[0], &buf[reply_len],
  1223. len - reply_len);
  1224. }
  1225. return reply_len;
  1226. }
  1227. void wpas_ap_pmksa_cache_flush(struct wpa_supplicant *wpa_s)
  1228. {
  1229. size_t i;
  1230. if (wpa_s->ap_iface) {
  1231. for (i = 0; i < wpa_s->ap_iface->num_bss; i++)
  1232. hostapd_ctrl_iface_pmksa_flush(wpa_s->ap_iface->bss[i]);
  1233. }
  1234. if (wpa_s->ifmsh)
  1235. hostapd_ctrl_iface_pmksa_flush(wpa_s->ifmsh->bss[0]);
  1236. }
  1237. #ifdef CONFIG_PMKSA_CACHE_EXTERNAL
  1238. #ifdef CONFIG_MESH
  1239. int wpas_ap_pmksa_cache_list_mesh(struct wpa_supplicant *wpa_s, const u8 *addr,
  1240. char *buf, size_t len)
  1241. {
  1242. return hostapd_ctrl_iface_pmksa_list_mesh(wpa_s->ifmsh->bss[0], addr,
  1243. &buf[0], len);
  1244. }
  1245. int wpas_ap_pmksa_cache_add_external(struct wpa_supplicant *wpa_s, char *cmd)
  1246. {
  1247. struct external_pmksa_cache *entry;
  1248. void *pmksa_cache;
  1249. pmksa_cache = hostapd_ctrl_iface_pmksa_create_entry(wpa_s->own_addr,
  1250. cmd);
  1251. if (!pmksa_cache)
  1252. return -1;
  1253. entry = os_zalloc(sizeof(struct external_pmksa_cache));
  1254. if (!entry)
  1255. return -1;
  1256. entry->pmksa_cache = pmksa_cache;
  1257. dl_list_add(&wpa_s->mesh_external_pmksa_cache, &entry->list);
  1258. return 0;
  1259. }
  1260. #endif /* CONFIG_MESH */
  1261. #endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
  1262. #endif /* CONFIG_CTRL_IFACE */
  1263. #ifdef NEED_AP_MLME
  1264. void wpas_event_dfs_radar_detected(struct wpa_supplicant *wpa_s,
  1265. struct dfs_event *radar)
  1266. {
  1267. if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0])
  1268. return;
  1269. wpa_printf(MSG_DEBUG, "DFS radar detected on %d MHz", radar->freq);
  1270. hostapd_dfs_radar_detected(wpa_s->ap_iface, radar->freq,
  1271. radar->ht_enabled, radar->chan_offset,
  1272. radar->chan_width,
  1273. radar->cf1, radar->cf2);
  1274. }
  1275. void wpas_event_dfs_cac_started(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 CAC started on %d MHz", radar->freq);
  1281. hostapd_dfs_start_cac(wpa_s->ap_iface, radar->freq,
  1282. radar->ht_enabled, radar->chan_offset,
  1283. radar->chan_width, radar->cf1, radar->cf2);
  1284. }
  1285. void wpas_event_dfs_cac_finished(struct wpa_supplicant *wpa_s,
  1286. struct dfs_event *radar)
  1287. {
  1288. if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0])
  1289. return;
  1290. wpa_printf(MSG_DEBUG, "DFS CAC finished on %d MHz", radar->freq);
  1291. hostapd_dfs_complete_cac(wpa_s->ap_iface, 1, radar->freq,
  1292. radar->ht_enabled, radar->chan_offset,
  1293. radar->chan_width, radar->cf1, radar->cf2);
  1294. }
  1295. void wpas_event_dfs_cac_aborted(struct wpa_supplicant *wpa_s,
  1296. struct dfs_event *radar)
  1297. {
  1298. if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0])
  1299. return;
  1300. wpa_printf(MSG_DEBUG, "DFS CAC aborted on %d MHz", radar->freq);
  1301. hostapd_dfs_complete_cac(wpa_s->ap_iface, 0, radar->freq,
  1302. radar->ht_enabled, radar->chan_offset,
  1303. radar->chan_width, radar->cf1, radar->cf2);
  1304. }
  1305. void wpas_event_dfs_cac_nop_finished(struct wpa_supplicant *wpa_s,
  1306. struct dfs_event *radar)
  1307. {
  1308. if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0])
  1309. return;
  1310. wpa_printf(MSG_DEBUG, "DFS NOP finished on %d MHz", radar->freq);
  1311. hostapd_dfs_nop_finished(wpa_s->ap_iface, radar->freq,
  1312. radar->ht_enabled, radar->chan_offset,
  1313. radar->chan_width, radar->cf1, radar->cf2);
  1314. }
  1315. #endif /* NEED_AP_MLME */
  1316. void ap_periodic(struct wpa_supplicant *wpa_s)
  1317. {
  1318. if (wpa_s->ap_iface)
  1319. hostapd_periodic_iface(wpa_s->ap_iface);
  1320. }