ap.c 37 KB

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