ap.c 40 KB

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