driver_bsd.c 19 KB

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