driver_bsd.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  1. /*
  2. * hostapd / Driver interaction with BSD net80211 layer
  3. * Copyright (c) 2004, Sam Leffler <sam@errno.com>
  4. * Copyright (c) 2004, 2Wire, Inc
  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 <sys/ioctl.h>
  17. #include <net/if.h>
  18. #include <net80211/ieee80211.h>
  19. #include <net80211/ieee80211_crypto.h>
  20. #include <net80211/ieee80211_ioctl.h>
  21. /*
  22. * Avoid conflicts with hostapd definitions by undefining couple of defines
  23. * from net80211 header files.
  24. */
  25. #undef RSN_VERSION
  26. #undef WPA_VERSION
  27. #undef WPA_OUI_TYPE
  28. #include "hostapd.h"
  29. #include "config.h"
  30. #include "driver.h"
  31. #include "eloop.h"
  32. #include "l2_packet/l2_packet.h"
  33. #include "eapol_sm.h"
  34. #include "common.h"
  35. struct bsd_driver_data {
  36. struct hostapd_data *hapd; /* back pointer */
  37. char iface[IFNAMSIZ + 1];
  38. struct l2_packet_data *sock_xmit; /* raw packet xmit socket */
  39. int ioctl_sock; /* socket for ioctl() use */
  40. int wext_sock; /* socket for wireless events */
  41. };
  42. static int bsd_sta_deauth(void *priv, const u8 *addr, int reason_code);
  43. static int
  44. set80211var(struct bsd_driver_data *drv, int op, const void *arg, int arg_len)
  45. {
  46. struct ieee80211req ireq;
  47. memset(&ireq, 0, sizeof(ireq));
  48. os_strlcpy(ireq.i_name, drv->iface, IFNAMSIZ);
  49. ireq.i_type = op;
  50. ireq.i_len = arg_len;
  51. ireq.i_data = (void *) arg;
  52. if (ioctl(drv->ioctl_sock, SIOCS80211, &ireq) < 0) {
  53. perror("ioctl[SIOCS80211]");
  54. return -1;
  55. }
  56. return 0;
  57. }
  58. static int
  59. get80211var(struct bsd_driver_data *drv, int op, void *arg, int arg_len)
  60. {
  61. struct ieee80211req ireq;
  62. memset(&ireq, 0, sizeof(ireq));
  63. os_strlcpy(ireq.i_name, drv->iface, IFNAMSIZ);
  64. ireq.i_type = op;
  65. ireq.i_len = arg_len;
  66. ireq.i_data = arg;
  67. if (ioctl(drv->ioctl_sock, SIOCG80211, &ireq) < 0) {
  68. perror("ioctl[SIOCG80211]");
  69. return -1;
  70. }
  71. return ireq.i_len;
  72. }
  73. static int
  74. set80211param(struct bsd_driver_data *drv, int op, int arg)
  75. {
  76. struct ieee80211req ireq;
  77. memset(&ireq, 0, sizeof(ireq));
  78. os_strlcpy(ireq.i_name, drv->iface, IFNAMSIZ);
  79. ireq.i_type = op;
  80. ireq.i_val = arg;
  81. if (ioctl(drv->ioctl_sock, SIOCS80211, &ireq) < 0) {
  82. perror("ioctl[SIOCS80211]");
  83. return -1;
  84. }
  85. return 0;
  86. }
  87. static const char *
  88. ether_sprintf(const u8 *addr)
  89. {
  90. static char buf[sizeof(MACSTR)];
  91. if (addr != NULL)
  92. snprintf(buf, sizeof(buf), MACSTR, MAC2STR(addr));
  93. else
  94. snprintf(buf, sizeof(buf), MACSTR, 0,0,0,0,0,0);
  95. return buf;
  96. }
  97. /*
  98. * Configure WPA parameters.
  99. */
  100. static int
  101. bsd_configure_wpa(struct bsd_driver_data *drv)
  102. {
  103. static const char *ciphernames[] =
  104. { "WEP", "TKIP", "AES-OCB", "AES-CCM", "CKIP", "NONE" };
  105. struct hostapd_data *hapd = drv->hapd;
  106. struct hostapd_bss_config *conf = hapd->conf;
  107. int v;
  108. switch (conf->wpa_group) {
  109. case WPA_CIPHER_CCMP:
  110. v = IEEE80211_CIPHER_AES_CCM;
  111. break;
  112. case WPA_CIPHER_TKIP:
  113. v = IEEE80211_CIPHER_TKIP;
  114. break;
  115. case WPA_CIPHER_WEP104:
  116. v = IEEE80211_CIPHER_WEP;
  117. break;
  118. case WPA_CIPHER_WEP40:
  119. v = IEEE80211_CIPHER_WEP;
  120. break;
  121. case WPA_CIPHER_NONE:
  122. v = IEEE80211_CIPHER_NONE;
  123. break;
  124. default:
  125. printf("Unknown group key cipher %u\n",
  126. conf->wpa_group);
  127. return -1;
  128. }
  129. wpa_printf(MSG_DEBUG, "%s: group key cipher=%s (%u)",
  130. __func__, ciphernames[v], v);
  131. if (set80211param(drv, IEEE80211_IOC_MCASTCIPHER, v)) {
  132. printf("Unable to set group key cipher to %u (%s)\n",
  133. v, ciphernames[v]);
  134. return -1;
  135. }
  136. if (v == IEEE80211_CIPHER_WEP) {
  137. /* key length is done only for specific ciphers */
  138. v = (conf->wpa_group == WPA_CIPHER_WEP104 ? 13 : 5);
  139. if (set80211param(drv, IEEE80211_IOC_MCASTKEYLEN, v)) {
  140. printf("Unable to set group key length to %u\n", v);
  141. return -1;
  142. }
  143. }
  144. v = 0;
  145. if (conf->wpa_pairwise & WPA_CIPHER_CCMP)
  146. v |= 1<<IEEE80211_CIPHER_AES_CCM;
  147. if (conf->wpa_pairwise & WPA_CIPHER_TKIP)
  148. v |= 1<<IEEE80211_CIPHER_TKIP;
  149. if (conf->wpa_pairwise & WPA_CIPHER_NONE)
  150. v |= 1<<IEEE80211_CIPHER_NONE;
  151. wpa_printf(MSG_DEBUG, "%s: pairwise key ciphers=0x%x", __func__, v);
  152. if (set80211param(drv, IEEE80211_IOC_UCASTCIPHERS, v)) {
  153. printf("Unable to set pairwise key ciphers to 0x%x\n", v);
  154. return -1;
  155. }
  156. wpa_printf(MSG_DEBUG, "%s: key management algorithms=0x%x",
  157. __func__, conf->wpa_key_mgmt);
  158. if (set80211param(drv, IEEE80211_IOC_KEYMGTALGS, conf->wpa_key_mgmt)) {
  159. printf("Unable to set key management algorithms to 0x%x\n",
  160. conf->wpa_key_mgmt);
  161. return -1;
  162. }
  163. v = 0;
  164. if (conf->rsn_preauth)
  165. v |= BIT(0);
  166. wpa_printf(MSG_DEBUG, "%s: rsn capabilities=0x%x",
  167. __func__, conf->rsn_preauth);
  168. if (set80211param(drv, IEEE80211_IOC_RSNCAPS, v)) {
  169. printf("Unable to set RSN capabilities to 0x%x\n", v);
  170. return -1;
  171. }
  172. wpa_printf(MSG_DEBUG, "%s: enable WPA= 0x%x", __func__, conf->wpa);
  173. if (set80211param(drv, IEEE80211_IOC_WPA, conf->wpa)) {
  174. printf("Unable to set WPA to %u\n", conf->wpa);
  175. return -1;
  176. }
  177. return 0;
  178. }
  179. static int
  180. bsd_set_iface_flags(void *priv, int dev_up)
  181. {
  182. struct bsd_driver_data *drv = priv;
  183. struct ifreq ifr;
  184. wpa_printf(MSG_DEBUG, "%s: dev_up=%d", __func__, dev_up);
  185. if (drv->ioctl_sock < 0)
  186. return -1;
  187. memset(&ifr, 0, sizeof(ifr));
  188. os_strlcpy(ifr.ifr_name, drv->iface, IFNAMSIZ);
  189. if (ioctl(drv->ioctl_sock, SIOCGIFFLAGS, &ifr) != 0) {
  190. perror("ioctl[SIOCGIFFLAGS]");
  191. return -1;
  192. }
  193. if (dev_up)
  194. ifr.ifr_flags |= IFF_UP;
  195. else
  196. ifr.ifr_flags &= ~IFF_UP;
  197. if (ioctl(drv->ioctl_sock, SIOCSIFFLAGS, &ifr) != 0) {
  198. perror("ioctl[SIOCSIFFLAGS]");
  199. return -1;
  200. }
  201. if (dev_up) {
  202. memset(&ifr, 0, sizeof(ifr));
  203. os_strlcpy(ifr.ifr_name, drv->iface, IFNAMSIZ);
  204. ifr.ifr_mtu = HOSTAPD_MTU;
  205. if (ioctl(drv->ioctl_sock, SIOCSIFMTU, &ifr) != 0) {
  206. perror("ioctl[SIOCSIFMTU]");
  207. printf("Setting MTU failed - trying to survive with "
  208. "current value\n");
  209. }
  210. }
  211. return 0;
  212. }
  213. static int
  214. bsd_set_ieee8021x(const char *ifname, void *priv, int enabled)
  215. {
  216. struct bsd_driver_data *drv = priv;
  217. struct hostapd_data *hapd = drv->hapd;
  218. struct hostapd_bss_config *conf = hapd->conf;
  219. wpa_printf(MSG_DEBUG, "%s: enabled=%d", __func__, enabled);
  220. if (!enabled) {
  221. /* XXX restore state */
  222. return set80211param(priv, IEEE80211_IOC_AUTHMODE,
  223. IEEE80211_AUTH_AUTO);
  224. }
  225. if (!conf->wpa && !conf->ieee802_1x) {
  226. hostapd_logger(hapd, NULL, HOSTAPD_MODULE_DRIVER,
  227. HOSTAPD_LEVEL_WARNING, "No 802.1X or WPA enabled!");
  228. return -1;
  229. }
  230. if (conf->wpa && bsd_configure_wpa(drv) != 0) {
  231. hostapd_logger(hapd, NULL, HOSTAPD_MODULE_DRIVER,
  232. HOSTAPD_LEVEL_WARNING, "Error configuring WPA state!");
  233. return -1;
  234. }
  235. if (set80211param(priv, IEEE80211_IOC_AUTHMODE,
  236. (conf->wpa ? IEEE80211_AUTH_WPA : IEEE80211_AUTH_8021X))) {
  237. hostapd_logger(hapd, NULL, HOSTAPD_MODULE_DRIVER,
  238. HOSTAPD_LEVEL_WARNING, "Error enabling WPA/802.1X!");
  239. return -1;
  240. }
  241. return bsd_set_iface_flags(priv, 1);
  242. }
  243. static int
  244. bsd_set_privacy(const char *ifname, void *priv, int enabled)
  245. {
  246. struct bsd_driver_data *drv = priv;
  247. wpa_printf(MSG_DEBUG, "%s: enabled=%d", __func__, enabled);
  248. return set80211param(drv, IEEE80211_IOC_PRIVACY, enabled);
  249. }
  250. static int
  251. bsd_set_sta_authorized(void *priv, const u8 *addr, int authorized)
  252. {
  253. struct bsd_driver_data *drv = priv;
  254. struct ieee80211req_mlme mlme;
  255. wpa_printf(MSG_DEBUG, "%s: addr=%s authorized=%d",
  256. __func__, ether_sprintf(addr), authorized);
  257. if (authorized)
  258. mlme.im_op = IEEE80211_MLME_AUTHORIZE;
  259. else
  260. mlme.im_op = IEEE80211_MLME_UNAUTHORIZE;
  261. mlme.im_reason = 0;
  262. memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
  263. return set80211var(drv, IEEE80211_IOC_MLME, &mlme, sizeof(mlme));
  264. }
  265. static int
  266. bsd_sta_set_flags(void *priv, const u8 *addr, int total_flags, int flags_or,
  267. int flags_and)
  268. {
  269. /* For now, only support setting Authorized flag */
  270. if (flags_or & WLAN_STA_AUTHORIZED)
  271. return bsd_set_sta_authorized(priv, addr, 1);
  272. if (!(flags_and & WLAN_STA_AUTHORIZED))
  273. return bsd_set_sta_authorized(priv, addr, 0);
  274. return 0;
  275. }
  276. static int
  277. bsd_del_key(void *priv, const u8 *addr, int key_idx)
  278. {
  279. struct bsd_driver_data *drv = priv;
  280. struct ieee80211req_del_key wk;
  281. wpa_printf(MSG_DEBUG, "%s: addr=%s key_idx=%d",
  282. __func__, ether_sprintf(addr), key_idx);
  283. memset(&wk, 0, sizeof(wk));
  284. if (addr != NULL) {
  285. memcpy(wk.idk_macaddr, addr, IEEE80211_ADDR_LEN);
  286. wk.idk_keyix = (u_int8_t) IEEE80211_KEYIX_NONE; /* XXX */
  287. } else {
  288. wk.idk_keyix = key_idx;
  289. }
  290. return set80211var(drv, IEEE80211_IOC_DELKEY, &wk, sizeof(wk));
  291. }
  292. static int
  293. bsd_set_key(const char *ifname, void *priv, wpa_alg alg,
  294. const u8 *addr, int key_idx, int set_tx, const u8 *seq,
  295. size_t seq_len, const u8 *key, size_t key_len)
  296. {
  297. struct bsd_driver_data *drv = priv;
  298. struct ieee80211req_key wk;
  299. u_int8_t cipher;
  300. if (alg == WPA_ALG_NONE)
  301. return bsd_del_key(drv, addr, key_idx);
  302. wpa_printf(MSG_DEBUG, "%s: alg=%d addr=%s key_idx=%d",
  303. __func__, alg, ether_sprintf(addr), key_idx);
  304. if (alg == WPA_ALG_WEP)
  305. cipher = IEEE80211_CIPHER_WEP;
  306. else if (alg == WPA_ALG_TKIP)
  307. cipher = IEEE80211_CIPHER_TKIP;
  308. else if (alg == WPA_ALG_CCMP)
  309. cipher = IEEE80211_CIPHER_AES_CCM;
  310. else {
  311. printf("%s: unknown/unsupported algorithm %d\n",
  312. __func__, alg);
  313. return -1;
  314. }
  315. if (key_len > sizeof(wk.ik_keydata)) {
  316. printf("%s: key length %d too big\n", __func__, key_len);
  317. return -3;
  318. }
  319. memset(&wk, 0, sizeof(wk));
  320. wk.ik_type = cipher;
  321. wk.ik_flags = IEEE80211_KEY_RECV | IEEE80211_KEY_XMIT;
  322. if (addr == NULL) {
  323. memset(wk.ik_macaddr, 0xff, IEEE80211_ADDR_LEN);
  324. wk.ik_keyix = key_idx;
  325. wk.ik_flags |= IEEE80211_KEY_DEFAULT;
  326. } else {
  327. memcpy(wk.ik_macaddr, addr, IEEE80211_ADDR_LEN);
  328. wk.ik_keyix = IEEE80211_KEYIX_NONE;
  329. }
  330. wk.ik_keylen = key_len;
  331. memcpy(wk.ik_keydata, key, key_len);
  332. return set80211var(drv, IEEE80211_IOC_WPAKEY, &wk, sizeof(wk));
  333. }
  334. static int
  335. bsd_get_seqnum(const char *ifname, void *priv, const u8 *addr, int idx,
  336. u8 *seq)
  337. {
  338. struct bsd_driver_data *drv = priv;
  339. struct ieee80211req_key wk;
  340. wpa_printf(MSG_DEBUG, "%s: addr=%s idx=%d",
  341. __func__, ether_sprintf(addr), idx);
  342. memset(&wk, 0, sizeof(wk));
  343. if (addr == NULL)
  344. memset(wk.ik_macaddr, 0xff, IEEE80211_ADDR_LEN);
  345. else
  346. memcpy(wk.ik_macaddr, addr, IEEE80211_ADDR_LEN);
  347. wk.ik_keyix = idx;
  348. if (get80211var(drv, IEEE80211_IOC_WPAKEY, &wk, sizeof(wk)) < 0) {
  349. printf("Failed to get encryption.\n");
  350. return -1;
  351. }
  352. #ifdef WORDS_BIGENDIAN
  353. {
  354. /*
  355. * wk.ik_keytsc is in host byte order (big endian), need to
  356. * swap it to match with the byte order used in WPA.
  357. */
  358. int i;
  359. u8 tmp[WPA_KEY_RSC_LEN];
  360. memcpy(tmp, &wk.ik_keytsc, sizeof(wk.ik_keytsc));
  361. for (i = 0; i < WPA_KEY_RSC_LEN; i++) {
  362. seq[i] = tmp[WPA_KEY_RSC_LEN - i - 1];
  363. }
  364. }
  365. #else /* WORDS_BIGENDIAN */
  366. memcpy(seq, &wk.ik_keytsc, sizeof(wk.ik_keytsc));
  367. #endif /* WORDS_BIGENDIAN */
  368. return 0;
  369. }
  370. static int
  371. bsd_flush(void *priv)
  372. {
  373. u8 allsta[IEEE80211_ADDR_LEN];
  374. memset(allsta, 0xff, IEEE80211_ADDR_LEN);
  375. return bsd_sta_deauth(priv, allsta, IEEE80211_REASON_AUTH_LEAVE);
  376. }
  377. static int
  378. bsd_read_sta_driver_data(void *priv, struct hostap_sta_driver_data *data,
  379. const u8 *addr)
  380. {
  381. struct bsd_driver_data *drv = priv;
  382. struct ieee80211req_sta_stats stats;
  383. memcpy(stats.is_u.macaddr, addr, IEEE80211_ADDR_LEN);
  384. if (get80211var(drv, IEEE80211_IOC_STA_STATS, &stats, sizeof(stats)) > 0) {
  385. /* XXX? do packets counts include non-data frames? */
  386. data->rx_packets = stats.is_stats.ns_rx_data;
  387. data->rx_bytes = stats.is_stats.ns_rx_bytes;
  388. data->tx_packets = stats.is_stats.ns_tx_data;
  389. data->tx_bytes = stats.is_stats.ns_tx_bytes;
  390. }
  391. return 0;
  392. }
  393. static int
  394. bsd_set_opt_ie(const char *ifname, void *priv, const u8 *ie, size_t ie_len)
  395. {
  396. /*
  397. * Do nothing; we setup parameters at startup that define the
  398. * contents of the beacon information element.
  399. */
  400. return 0;
  401. }
  402. static int
  403. bsd_sta_deauth(void *priv, const u8 *addr, int reason_code)
  404. {
  405. struct bsd_driver_data *drv = priv;
  406. struct ieee80211req_mlme mlme;
  407. wpa_printf(MSG_DEBUG, "%s: addr=%s reason_code=%d",
  408. __func__, ether_sprintf(addr), reason_code);
  409. mlme.im_op = IEEE80211_MLME_DEAUTH;
  410. mlme.im_reason = reason_code;
  411. memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
  412. return set80211var(drv, IEEE80211_IOC_MLME, &mlme, sizeof(mlme));
  413. }
  414. static int
  415. bsd_sta_disassoc(void *priv, const u8 *addr, int reason_code)
  416. {
  417. struct bsd_driver_data *drv = priv;
  418. struct ieee80211req_mlme mlme;
  419. wpa_printf(MSG_DEBUG, "%s: addr=%s reason_code=%d",
  420. __func__, ether_sprintf(addr), reason_code);
  421. mlme.im_op = IEEE80211_MLME_DISASSOC;
  422. mlme.im_reason = reason_code;
  423. memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
  424. return set80211var(drv, IEEE80211_IOC_MLME, &mlme, sizeof(mlme));
  425. }
  426. static int
  427. bsd_new_sta(struct bsd_driver_data *drv, u8 addr[IEEE80211_ADDR_LEN])
  428. {
  429. struct hostapd_data *hapd = drv->hapd;
  430. struct ieee80211req_wpaie ie;
  431. int ielen = 0;
  432. u8 *iebuf = NULL;
  433. /*
  434. * Fetch and validate any negotiated WPA/RSN parameters.
  435. */
  436. memset(&ie, 0, sizeof(ie));
  437. memcpy(ie.wpa_macaddr, addr, IEEE80211_ADDR_LEN);
  438. if (get80211var(drv, IEEE80211_IOC_WPAIE, &ie, sizeof(ie)) < 0) {
  439. printf("Failed to get WPA/RSN information element.\n");
  440. goto no_ie;
  441. }
  442. iebuf = ie.wpa_ie;
  443. ielen = ie.wpa_ie[1];
  444. if (ielen == 0)
  445. iebuf = NULL;
  446. else
  447. ielen += 2;
  448. no_ie:
  449. return hostapd_notif_assoc(hapd, addr, iebuf, ielen);
  450. }
  451. #include <net/route.h>
  452. #include <net80211/ieee80211_freebsd.h>
  453. static void
  454. bsd_wireless_event_receive(int sock, void *ctx, void *sock_ctx)
  455. {
  456. struct bsd_driver_data *drv = ctx;
  457. struct hostapd_data *hapd = drv->hapd;
  458. char buf[2048];
  459. struct if_announcemsghdr *ifan;
  460. struct rt_msghdr *rtm;
  461. struct ieee80211_michael_event *mic;
  462. struct ieee80211_join_event *join;
  463. struct ieee80211_leave_event *leave;
  464. int n;
  465. n = read(sock, buf, sizeof(buf));
  466. if (n < 0) {
  467. if (errno != EINTR && errno != EAGAIN)
  468. perror("read(PF_ROUTE)");
  469. return;
  470. }
  471. rtm = (struct rt_msghdr *) buf;
  472. if (rtm->rtm_version != RTM_VERSION) {
  473. wpa_printf(MSG_DEBUG, "Routing message version %d not "
  474. "understood\n", rtm->rtm_version);
  475. return;
  476. }
  477. ifan = (struct if_announcemsghdr *) rtm;
  478. switch (rtm->rtm_type) {
  479. case RTM_IEEE80211:
  480. switch (ifan->ifan_what) {
  481. case RTM_IEEE80211_ASSOC:
  482. case RTM_IEEE80211_REASSOC:
  483. case RTM_IEEE80211_DISASSOC:
  484. case RTM_IEEE80211_SCAN:
  485. break;
  486. case RTM_IEEE80211_LEAVE:
  487. leave = (struct ieee80211_leave_event *) &ifan[1];
  488. hostapd_notif_disassoc(drv->hapd, leave->iev_addr);
  489. break;
  490. case RTM_IEEE80211_JOIN:
  491. #ifdef RTM_IEEE80211_REJOIN
  492. case RTM_IEEE80211_REJOIN:
  493. #endif
  494. join = (struct ieee80211_join_event *) &ifan[1];
  495. bsd_new_sta(drv, join->iev_addr);
  496. break;
  497. case RTM_IEEE80211_REPLAY:
  498. /* ignore */
  499. break;
  500. case RTM_IEEE80211_MICHAEL:
  501. mic = (struct ieee80211_michael_event *) &ifan[1];
  502. wpa_printf(MSG_DEBUG,
  503. "Michael MIC failure wireless event: "
  504. "keyix=%u src_addr=" MACSTR, mic->iev_keyix,
  505. MAC2STR(mic->iev_src));
  506. hostapd_michael_mic_failure(hapd, mic->iev_src);
  507. break;
  508. }
  509. break;
  510. }
  511. }
  512. static int
  513. bsd_wireless_event_init(void *priv)
  514. {
  515. struct bsd_driver_data *drv = priv;
  516. int s;
  517. drv->wext_sock = -1;
  518. s = socket(PF_ROUTE, SOCK_RAW, 0);
  519. if (s < 0) {
  520. perror("socket(PF_ROUTE,SOCK_RAW)");
  521. return -1;
  522. }
  523. eloop_register_read_sock(s, bsd_wireless_event_receive, drv, NULL);
  524. drv->wext_sock = s;
  525. return 0;
  526. }
  527. static void
  528. bsd_wireless_event_deinit(void *priv)
  529. {
  530. struct bsd_driver_data *drv = priv;
  531. if (drv != NULL) {
  532. if (drv->wext_sock < 0)
  533. return;
  534. eloop_unregister_read_sock(drv->wext_sock);
  535. close(drv->wext_sock);
  536. }
  537. }
  538. static int
  539. bsd_send_eapol(void *priv, const u8 *addr, const u8 *data, size_t data_len,
  540. int encrypt, const u8 *own_addr)
  541. {
  542. struct bsd_driver_data *drv = priv;
  543. unsigned char buf[3000];
  544. unsigned char *bp = buf;
  545. struct l2_ethhdr *eth;
  546. size_t len;
  547. int status;
  548. /*
  549. * Prepend the Etherent header. If the caller left us
  550. * space at the front we could just insert it but since
  551. * we don't know we copy to a local buffer. Given the frequency
  552. * and size of frames this probably doesn't matter.
  553. */
  554. len = data_len + sizeof(struct l2_ethhdr);
  555. if (len > sizeof(buf)) {
  556. bp = malloc(len);
  557. if (bp == NULL) {
  558. printf("EAPOL frame discarded, cannot malloc temp "
  559. "buffer of size %u!\n", len);
  560. return -1;
  561. }
  562. }
  563. eth = (struct l2_ethhdr *) bp;
  564. memcpy(eth->h_dest, addr, ETH_ALEN);
  565. memcpy(eth->h_source, own_addr, ETH_ALEN);
  566. eth->h_proto = htons(ETH_P_EAPOL);
  567. memcpy(eth+1, data, data_len);
  568. wpa_hexdump(MSG_MSGDUMP, "TX EAPOL", bp, len);
  569. status = l2_packet_send(drv->sock_xmit, addr, ETH_P_EAPOL, bp, len);
  570. if (bp != buf)
  571. free(bp);
  572. return status;
  573. }
  574. static void
  575. handle_read(void *ctx, const u8 *src_addr, const u8 *buf, size_t len)
  576. {
  577. struct bsd_driver_data *drv = ctx;
  578. hostapd_eapol_receive(drv->hapd, src_addr,
  579. buf + sizeof(struct l2_ethhdr),
  580. len - sizeof(struct l2_ethhdr));
  581. }
  582. static int
  583. bsd_get_ssid(const char *ifname, void *priv, u8 *buf, int len)
  584. {
  585. struct bsd_driver_data *drv = priv;
  586. int ssid_len = get80211var(drv, IEEE80211_IOC_SSID, buf, len);
  587. wpa_printf(MSG_DEBUG, "%s: ssid=\"%.*s\"", __func__, ssid_len, buf);
  588. return ssid_len;
  589. }
  590. static int
  591. bsd_set_ssid(const char *ifname, void *priv, const u8 *buf, int len)
  592. {
  593. struct bsd_driver_data *drv = priv;
  594. wpa_printf(MSG_DEBUG, "%s: ssid=\"%.*s\"", __func__, len, buf);
  595. return set80211var(drv, IEEE80211_IOC_SSID, buf, len);
  596. }
  597. static void *
  598. bsd_init(struct hostapd_data *hapd)
  599. {
  600. struct bsd_driver_data *drv;
  601. drv = os_zalloc(sizeof(struct bsd_driver_data));
  602. if (drv == NULL) {
  603. printf("Could not allocate memory for bsd driver data\n");
  604. goto bad;
  605. }
  606. drv->hapd = hapd;
  607. drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
  608. if (drv->ioctl_sock < 0) {
  609. perror("socket[PF_INET,SOCK_DGRAM]");
  610. goto bad;
  611. }
  612. memcpy(drv->iface, hapd->conf->iface, sizeof(drv->iface));
  613. drv->sock_xmit = l2_packet_init(drv->iface, NULL, ETH_P_EAPOL,
  614. handle_read, drv, 1);
  615. if (drv->sock_xmit == NULL)
  616. goto bad;
  617. if (l2_packet_get_own_addr(drv->sock_xmit, hapd->own_addr))
  618. goto bad;
  619. bsd_set_iface_flags(drv, 0); /* mark down during setup */
  620. return drv;
  621. bad:
  622. if (drv->sock_xmit != NULL)
  623. l2_packet_deinit(drv->sock_xmit);
  624. if (drv->ioctl_sock >= 0)
  625. close(drv->ioctl_sock);
  626. if (drv != NULL)
  627. free(drv);
  628. return NULL;
  629. }
  630. static void
  631. bsd_deinit(void *priv)
  632. {
  633. struct bsd_driver_data *drv = priv;
  634. (void) bsd_set_iface_flags(drv, 0);
  635. if (drv->ioctl_sock >= 0)
  636. close(drv->ioctl_sock);
  637. if (drv->sock_xmit != NULL)
  638. l2_packet_deinit(drv->sock_xmit);
  639. free(drv);
  640. }
  641. const struct hapd_driver_ops wpa_driver_bsd_ops = {
  642. .name = "bsd",
  643. .init = bsd_init,
  644. .deinit = bsd_deinit,
  645. .set_ieee8021x = bsd_set_ieee8021x,
  646. .set_privacy = bsd_set_privacy,
  647. .set_key = bsd_set_key,
  648. .get_seqnum = bsd_get_seqnum,
  649. .flush = bsd_flush,
  650. .set_generic_elem = bsd_set_opt_ie,
  651. .wireless_event_init = bsd_wireless_event_init,
  652. .wireless_event_deinit = bsd_wireless_event_deinit,
  653. .sta_set_flags = bsd_sta_set_flags,
  654. .read_sta_data = bsd_read_sta_driver_data,
  655. .send_eapol = bsd_send_eapol,
  656. .sta_disassoc = bsd_sta_disassoc,
  657. .sta_deauth = bsd_sta_deauth,
  658. .set_ssid = bsd_set_ssid,
  659. .get_ssid = bsd_get_ssid,
  660. };