ap.c 36 KB

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