ap.c 41 KB

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