ap.c 38 KB

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