ap.c 36 KB

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