ap.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  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 program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Alternatively, this software may be distributed under the terms of BSD
  11. * license.
  12. *
  13. * See README and COPYING for more details.
  14. */
  15. #include "includes.h"
  16. #include "common.h"
  17. #include "../hostapd/hostapd.h"
  18. #include "../hostapd/config.h"
  19. #ifdef NEED_AP_MLME
  20. #include "../hostapd/ieee802_11.h"
  21. #endif /* NEED_AP_MLME */
  22. #include "../hostapd/wps_hostapd.h"
  23. #include "../hostapd/ctrl_iface_ap.h"
  24. #include "eap_common/eap_defs.h"
  25. #include "eap_server/eap_methods.h"
  26. #include "eap_common/eap_wsc_common.h"
  27. #include "wps/wps.h"
  28. #include "config_ssid.h"
  29. #include "wpa_supplicant_i.h"
  30. #include "driver_i.h"
  31. #include "ap.h"
  32. int hostapd_for_each_interface(int (*cb)(struct hostapd_iface *iface,
  33. void *ctx), void *ctx)
  34. {
  35. /* TODO */
  36. return 0;
  37. }
  38. int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
  39. {
  40. return 0;
  41. }
  42. void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
  43. {
  44. }
  45. struct ap_driver_data {
  46. struct hostapd_data *hapd;
  47. };
  48. static void * ap_driver_init(struct hostapd_data *hapd,
  49. struct wpa_init_params *params)
  50. {
  51. struct ap_driver_data *drv;
  52. struct wpa_supplicant *wpa_s = hapd->iface->owner;
  53. drv = os_zalloc(sizeof(struct ap_driver_data));
  54. if (drv == NULL) {
  55. wpa_printf(MSG_ERROR, "Could not allocate memory for AP "
  56. "driver data");
  57. return NULL;
  58. }
  59. drv->hapd = hapd;
  60. os_memcpy(hapd->own_addr, wpa_s->own_addr, ETH_ALEN);
  61. return drv;
  62. }
  63. static void ap_driver_deinit(void *priv)
  64. {
  65. struct ap_driver_data *drv = priv;
  66. os_free(drv);
  67. }
  68. static int ap_driver_send_ether(void *priv, const u8 *dst, const u8 *src,
  69. u16 proto, const u8 *data, size_t data_len)
  70. {
  71. wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
  72. return -1;
  73. }
  74. static int ap_driver_set_key(const char *iface, void *priv, wpa_alg alg,
  75. const u8 *addr, int key_idx, int set_tx,
  76. const u8 *seq, size_t seq_len, const u8 *key,
  77. size_t key_len)
  78. {
  79. struct ap_driver_data *drv = priv;
  80. struct wpa_supplicant *wpa_s = drv->hapd->iface->owner;
  81. return wpa_drv_set_key(wpa_s, alg, addr, key_idx, set_tx, seq, seq_len,
  82. key, key_len);
  83. }
  84. static int ap_driver_get_seqnum(const char *iface, void *priv, const u8 *addr,
  85. int idx, u8 *seq)
  86. {
  87. wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
  88. return -1;
  89. }
  90. static int ap_driver_flush(void *priv)
  91. {
  92. wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
  93. return -1;
  94. }
  95. static int ap_driver_read_sta_data(void *priv,
  96. struct hostap_sta_driver_data *data,
  97. const u8 *addr)
  98. {
  99. wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
  100. return -1;
  101. }
  102. static int ap_driver_sta_set_flags(void *priv, const u8 *addr, int total_flags,
  103. int flags_or, int flags_and)
  104. {
  105. struct ap_driver_data *drv = priv;
  106. struct wpa_supplicant *wpa_s = drv->hapd->iface->owner;
  107. return wpa_drv_sta_set_flags(wpa_s, addr, total_flags, flags_or,
  108. flags_and);
  109. }
  110. static int ap_driver_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
  111. int reason)
  112. {
  113. wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
  114. return -1;
  115. }
  116. static int ap_driver_sta_disassoc(void *priv, const u8 *own_addr,
  117. const u8 *addr, int reason)
  118. {
  119. wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
  120. return -1;
  121. }
  122. static int ap_driver_sta_remove(void *priv, const u8 *addr)
  123. {
  124. struct ap_driver_data *drv = priv;
  125. struct wpa_supplicant *wpa_s = drv->hapd->iface->owner;
  126. return wpa_drv_sta_remove(wpa_s, addr);
  127. }
  128. static int ap_driver_send_mlme(void *priv, const u8 *data, size_t len)
  129. {
  130. struct ap_driver_data *drv = priv;
  131. struct wpa_supplicant *wpa_s = drv->hapd->iface->owner;
  132. return wpa_drv_send_mlme(wpa_s, data, len);
  133. }
  134. static int ap_driver_sta_add(const char *ifname, void *priv,
  135. struct hostapd_sta_add_params *params)
  136. {
  137. struct ap_driver_data *drv = priv;
  138. struct wpa_supplicant *wpa_s = drv->hapd->iface->owner;
  139. return wpa_drv_sta_add(wpa_s, params);
  140. }
  141. static int ap_driver_get_inact_sec(void *priv, const u8 *addr)
  142. {
  143. wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
  144. return -1;
  145. }
  146. static int ap_driver_set_freq(void *priv, struct hostapd_freq_params *freq)
  147. {
  148. wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
  149. return 0;
  150. }
  151. static int ap_driver_set_beacon(const char *iface, void *priv,
  152. const u8 *head, size_t head_len,
  153. const u8 *tail, size_t tail_len,
  154. int dtim_period, int beacon_int)
  155. {
  156. struct ap_driver_data *drv = priv;
  157. struct wpa_supplicant *wpa_s = drv->hapd->iface->owner;
  158. return wpa_drv_set_beacon(wpa_s, head, head_len, tail, tail_len,
  159. dtim_period, beacon_int);
  160. }
  161. static int ap_driver_set_cts_protect(void *priv, int value)
  162. {
  163. wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
  164. return -1;
  165. }
  166. static int ap_driver_set_preamble(void *priv, int value)
  167. {
  168. wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
  169. return -1;
  170. }
  171. static int ap_driver_set_short_slot_time(void *priv, int value)
  172. {
  173. wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
  174. return -1;
  175. }
  176. static int ap_driver_set_tx_queue_params(void *priv, int queue, int aifs,
  177. int cw_min, int cw_max,
  178. int burst_time)
  179. {
  180. wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
  181. return -1;
  182. }
  183. static struct hostapd_hw_modes *ap_driver_get_hw_feature_data(void *priv,
  184. u16 *num_modes,
  185. u16 *flags)
  186. {
  187. struct ap_driver_data *drv = priv;
  188. struct wpa_supplicant *wpa_s = drv->hapd->iface->owner;
  189. return wpa_drv_get_hw_feature_data(wpa_s, num_modes, flags);
  190. }
  191. static int ap_driver_hapd_send_eapol(void *priv, const u8 *addr,
  192. const u8 *data, size_t data_len,
  193. int encrypt, const u8 *own_addr)
  194. {
  195. struct ap_driver_data *drv = priv;
  196. struct wpa_supplicant *wpa_s = drv->hapd->iface->owner;
  197. return wpa_drv_hapd_send_eapol(wpa_s, addr, data, data_len, encrypt,
  198. own_addr);
  199. }
  200. struct wpa_driver_ops ap_driver_ops =
  201. {
  202. .name = "wpa_supplicant",
  203. .hapd_init = ap_driver_init,
  204. .hapd_deinit = ap_driver_deinit,
  205. .send_ether = ap_driver_send_ether,
  206. .set_key = ap_driver_set_key,
  207. .get_seqnum = ap_driver_get_seqnum,
  208. .flush = ap_driver_flush,
  209. .read_sta_data = ap_driver_read_sta_data,
  210. .sta_set_flags = ap_driver_sta_set_flags,
  211. .sta_deauth = ap_driver_sta_deauth,
  212. .sta_disassoc = ap_driver_sta_disassoc,
  213. .sta_remove = ap_driver_sta_remove,
  214. .send_mlme = ap_driver_send_mlme,
  215. .sta_add = ap_driver_sta_add,
  216. .get_inact_sec = ap_driver_get_inact_sec,
  217. .set_freq = ap_driver_set_freq,
  218. .set_beacon = ap_driver_set_beacon,
  219. .set_cts_protect = ap_driver_set_cts_protect,
  220. .set_preamble = ap_driver_set_preamble,
  221. .set_short_slot_time = ap_driver_set_short_slot_time,
  222. .set_tx_queue_params = ap_driver_set_tx_queue_params,
  223. .get_hw_feature_data = ap_driver_get_hw_feature_data,
  224. .hapd_send_eapol = ap_driver_hapd_send_eapol,
  225. };
  226. extern struct wpa_driver_ops *wpa_drivers[];
  227. static int wpa_supplicant_conf_ap(struct wpa_supplicant *wpa_s,
  228. struct wpa_ssid *ssid,
  229. struct hostapd_config *conf)
  230. {
  231. struct hostapd_bss_config *bss = &conf->bss[0];
  232. int j, pairwise;
  233. for (j = 0; wpa_drivers[j]; j++) {
  234. if (os_strcmp("wpa_supplicant", wpa_drivers[j]->name) == 0) {
  235. conf->driver = wpa_drivers[j];
  236. break;
  237. }
  238. }
  239. if (conf->driver == NULL) {
  240. wpa_printf(MSG_ERROR, "No AP driver ops found");
  241. return -1;
  242. }
  243. os_strlcpy(bss->iface, wpa_s->ifname, sizeof(bss->iface));
  244. if (ssid->frequency == 0) {
  245. /* default channel 11 */
  246. conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
  247. conf->channel = 11;
  248. } else if (ssid->frequency >= 2412 && ssid->frequency <= 2472) {
  249. conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
  250. conf->channel = (ssid->frequency - 2407) / 5;
  251. } else if ((ssid->frequency >= 5180 && ssid->frequency <= 5240) ||
  252. (ssid->frequency >= 5745 && ssid->frequency <= 5825)) {
  253. conf->hw_mode = HOSTAPD_MODE_IEEE80211A;
  254. conf->channel = (ssid->frequency - 5000) / 5;
  255. } else {
  256. wpa_printf(MSG_ERROR, "Unsupported AP mode frequency: %d MHz",
  257. ssid->frequency);
  258. return -1;
  259. }
  260. /* TODO: enable HT if driver supports it;
  261. * drop to 11b if driver does not support 11g */
  262. if (ssid->ssid_len == 0) {
  263. wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
  264. return -1;
  265. }
  266. os_memcpy(bss->ssid.ssid, ssid->ssid, ssid->ssid_len);
  267. bss->ssid.ssid[ssid->ssid_len] = '\0';
  268. bss->ssid.ssid_len = ssid->ssid_len;
  269. bss->ssid.ssid_set = 1;
  270. if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt))
  271. bss->wpa = ssid->proto;
  272. bss->wpa_key_mgmt = ssid->key_mgmt;
  273. bss->wpa_pairwise = ssid->pairwise_cipher;
  274. if (ssid->passphrase) {
  275. bss->ssid.wpa_passphrase = os_strdup(ssid->passphrase);
  276. } else if (ssid->psk_set) {
  277. os_free(bss->ssid.wpa_psk);
  278. bss->ssid.wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
  279. if (bss->ssid.wpa_psk == NULL)
  280. return -1;
  281. os_memcpy(bss->ssid.wpa_psk->psk, ssid->psk, PMK_LEN);
  282. bss->ssid.wpa_psk->group = 1;
  283. }
  284. /* Select group cipher based on the enabled pairwise cipher suites */
  285. pairwise = 0;
  286. if (bss->wpa & 1)
  287. pairwise |= bss->wpa_pairwise;
  288. if (bss->wpa & 2) {
  289. if (bss->rsn_pairwise == 0)
  290. bss->rsn_pairwise = bss->wpa_pairwise;
  291. pairwise |= bss->rsn_pairwise;
  292. }
  293. if (pairwise & WPA_CIPHER_TKIP)
  294. bss->wpa_group = WPA_CIPHER_TKIP;
  295. else
  296. bss->wpa_group = WPA_CIPHER_CCMP;
  297. if (bss->wpa && bss->ieee802_1x)
  298. bss->ssid.security_policy = SECURITY_WPA;
  299. else if (bss->wpa)
  300. bss->ssid.security_policy = SECURITY_WPA_PSK;
  301. else if (bss->ieee802_1x) {
  302. bss->ssid.security_policy = SECURITY_IEEE_802_1X;
  303. bss->ssid.wep.default_len = bss->default_wep_key_len;
  304. } else if (bss->ssid.wep.keys_set)
  305. bss->ssid.security_policy = SECURITY_STATIC_WEP;
  306. else
  307. bss->ssid.security_policy = SECURITY_PLAINTEXT;
  308. #ifdef CONFIG_WPS
  309. /*
  310. * Enable WPS by default, but require user interaction to actually use
  311. * it. Only the internal Registrar is supported.
  312. */
  313. bss->eap_server = 1;
  314. bss->wps_state = 2;
  315. bss->ap_setup_locked = 1;
  316. bss->config_methods = os_strdup("display push_button");
  317. #endif /* CONFIG_WPS */
  318. return 0;
  319. }
  320. int wpa_supplicant_create_ap(struct wpa_supplicant *wpa_s,
  321. struct wpa_ssid *ssid)
  322. {
  323. struct wpa_driver_associate_params params;
  324. struct hostapd_iface *hapd_iface;
  325. struct hostapd_config *conf;
  326. size_t i;
  327. if (ssid->ssid == NULL || ssid->ssid_len == 0) {
  328. wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
  329. return -1;
  330. }
  331. wpa_supplicant_ap_deinit(wpa_s);
  332. wpa_printf(MSG_DEBUG, "Setting up AP (SSID='%s')",
  333. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  334. os_memset(&params, 0, sizeof(params));
  335. params.ssid = ssid->ssid;
  336. params.ssid_len = ssid->ssid_len;
  337. params.mode = ssid->mode;
  338. params.freq = ssid->frequency;
  339. if (wpa_drv_associate(wpa_s, &params) < 0) {
  340. wpa_msg(wpa_s, MSG_INFO, "Failed to start AP functionality");
  341. return -1;
  342. }
  343. wpa_s->ap_iface = hapd_iface = os_zalloc(sizeof(*wpa_s->ap_iface));
  344. if (hapd_iface == NULL)
  345. return -1;
  346. hapd_iface->owner = wpa_s;
  347. wpa_s->ap_iface->conf = conf = hostapd_config_defaults();
  348. if (conf == NULL) {
  349. wpa_supplicant_ap_deinit(wpa_s);
  350. return -1;
  351. }
  352. if (wpa_supplicant_conf_ap(wpa_s, ssid, conf)) {
  353. wpa_printf(MSG_ERROR, "Failed to create AP configuration");
  354. wpa_supplicant_ap_deinit(wpa_s);
  355. return -1;
  356. }
  357. hapd_iface->num_bss = conf->num_bss;
  358. hapd_iface->bss = os_zalloc(conf->num_bss *
  359. sizeof(struct hostapd_data *));
  360. if (hapd_iface->bss == NULL) {
  361. wpa_supplicant_ap_deinit(wpa_s);
  362. return -1;
  363. }
  364. for (i = 0; i < conf->num_bss; i++) {
  365. hapd_iface->bss[i] =
  366. hostapd_alloc_bss_data(hapd_iface, conf,
  367. &conf->bss[i]);
  368. if (hapd_iface->bss[i] == NULL) {
  369. wpa_supplicant_ap_deinit(wpa_s);
  370. return -1;
  371. }
  372. hapd_iface->bss[i]->msg_ctx = wpa_s;
  373. }
  374. if (hostapd_setup_interface(wpa_s->ap_iface)) {
  375. wpa_printf(MSG_ERROR, "Failed to initialize AP interface");
  376. wpa_supplicant_ap_deinit(wpa_s);
  377. return -1;
  378. }
  379. wpa_s->current_ssid = ssid;
  380. os_memcpy(wpa_s->bssid, wpa_s->own_addr, ETH_ALEN);
  381. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  382. return 0;
  383. }
  384. void wpa_supplicant_ap_deinit(struct wpa_supplicant *wpa_s)
  385. {
  386. if (wpa_s->ap_iface == NULL)
  387. return;
  388. hostapd_interface_deinit(wpa_s->ap_iface);
  389. wpa_s->ap_iface = NULL;
  390. }
  391. void ap_tx_status(void *ctx, const u8 *addr,
  392. const u8 *buf, size_t len, int ack)
  393. {
  394. struct wpa_supplicant *wpa_s = ctx;
  395. hostapd_tx_status(wpa_s->ap_iface->bss[0], addr, buf, len, ack);
  396. }
  397. void ap_rx_from_unknown_sta(void *ctx, struct ieee80211_hdr *hdr, size_t len)
  398. {
  399. struct wpa_supplicant *wpa_s = ctx;
  400. hostapd_rx_from_unknown_sta(wpa_s->ap_iface->bss[0], hdr, len);
  401. }
  402. #ifdef NEED_AP_MLME
  403. void ap_mgmt_rx(void *ctx, u8 *buf, size_t len, u16 stype,
  404. struct hostapd_frame_info *fi)
  405. {
  406. struct wpa_supplicant *wpa_s = ctx;
  407. ieee802_11_mgmt(wpa_s->ap_iface->bss[0], buf, len, stype, fi);
  408. }
  409. void ap_mgmt_tx_cb(void *ctx, u8 *buf, size_t len, u16 stype, int ok)
  410. {
  411. struct wpa_supplicant *wpa_s = ctx;
  412. ieee802_11_mgmt_cb(wpa_s->ap_iface->bss[0], buf, len, stype, ok);
  413. }
  414. #endif /* NEED_AP_MLME */
  415. void wpa_supplicant_ap_rx_eapol(struct wpa_supplicant *wpa_s,
  416. const u8 *src_addr, const u8 *buf, size_t len)
  417. {
  418. hostapd_eapol_receive(wpa_s->ap_iface->bss[0], src_addr, buf, len);
  419. }
  420. #ifdef CONFIG_WPS
  421. int wpa_supplicant_ap_wps_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid)
  422. {
  423. return hostapd_wps_button_pushed(wpa_s->ap_iface->bss[0]);
  424. }
  425. int wpa_supplicant_ap_wps_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
  426. const char *pin, char *buf, size_t buflen)
  427. {
  428. int ret, ret_len = 0;
  429. if (pin == NULL) {
  430. unsigned int rpin = wps_generate_pin();
  431. ret_len = os_snprintf(buf, buflen, "%d", rpin);
  432. pin = buf;
  433. }
  434. ret = hostapd_wps_add_pin(wpa_s->ap_iface->bss[0], "any", pin, 0);
  435. if (ret)
  436. return -1;
  437. return ret_len;
  438. }
  439. #endif /* CONFIG_WPS */
  440. #ifdef CONFIG_CTRL_IFACE
  441. int ap_ctrl_iface_sta_first(struct wpa_supplicant *wpa_s,
  442. char *buf, size_t buflen)
  443. {
  444. if (wpa_s->ap_iface == NULL)
  445. return -1;
  446. return hostapd_ctrl_iface_sta_first(wpa_s->ap_iface->bss[0],
  447. buf, buflen);
  448. }
  449. int ap_ctrl_iface_sta(struct wpa_supplicant *wpa_s, const char *txtaddr,
  450. char *buf, size_t buflen)
  451. {
  452. if (wpa_s->ap_iface == NULL)
  453. return -1;
  454. return hostapd_ctrl_iface_sta(wpa_s->ap_iface->bss[0], txtaddr,
  455. buf, buflen);
  456. }
  457. int ap_ctrl_iface_sta_next(struct wpa_supplicant *wpa_s, const char *txtaddr,
  458. char *buf, size_t buflen)
  459. {
  460. if (wpa_s->ap_iface == NULL)
  461. return -1;
  462. return hostapd_ctrl_iface_sta_next(wpa_s->ap_iface->bss[0], txtaddr,
  463. buf, buflen);
  464. }
  465. int ap_ctrl_iface_wpa_get_status(struct wpa_supplicant *wpa_s, char *buf,
  466. size_t buflen, int verbose)
  467. {
  468. char *pos = buf, *end = buf + buflen;
  469. int ret;
  470. struct hostapd_bss_config *conf;
  471. if (wpa_s->ap_iface == NULL)
  472. return -1;
  473. conf = wpa_s->ap_iface->bss[0]->conf;
  474. if (conf->wpa == 0)
  475. return 0;
  476. ret = os_snprintf(pos, end - pos,
  477. "pairwise_cipher=%s\n"
  478. "group_cipher=%s\n"
  479. "key_mgmt=%s\n",
  480. wpa_cipher_txt(conf->rsn_pairwise),
  481. wpa_cipher_txt(conf->wpa_group),
  482. wpa_key_mgmt_txt(conf->wpa_key_mgmt,
  483. conf->wpa));
  484. if (ret < 0 || ret >= end - pos)
  485. return pos - buf;
  486. pos += ret;
  487. return pos - buf;
  488. }
  489. #endif /* CONFIG_CTRL_IFACE */