ap.c 37 KB

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