driver_bsd.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  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 "ieee802_1x.h"
  31. #include "eloop.h"
  32. #include "l2_packet/l2_packet.h"
  33. #include "eapol_sm.h"
  34. #include "ieee802_11.h"
  35. #include "common.h"
  36. struct bsd_driver_data {
  37. struct hostapd_data *hapd; /* back pointer */
  38. char iface[IFNAMSIZ + 1];
  39. struct l2_packet_data *sock_xmit; /* raw packet xmit socket */
  40. int ioctl_sock; /* socket for ioctl() use */
  41. int wext_sock; /* socket for wireless events */
  42. };
  43. static int bsd_sta_deauth(void *priv, const u8 *addr, int reason_code);
  44. static int
  45. set80211var(struct bsd_driver_data *drv, int op, const void *arg, int arg_len)
  46. {
  47. struct ieee80211req ireq;
  48. memset(&ireq, 0, sizeof(ireq));
  49. os_strlcpy(ireq.i_name, drv->iface, IFNAMSIZ);
  50. ireq.i_type = op;
  51. ireq.i_len = arg_len;
  52. ireq.i_data = (void *) arg;
  53. if (ioctl(drv->ioctl_sock, SIOCS80211, &ireq) < 0) {
  54. perror("ioctl[SIOCS80211]");
  55. return -1;
  56. }
  57. return 0;
  58. }
  59. static int
  60. get80211var(struct bsd_driver_data *drv, int op, void *arg, int arg_len)
  61. {
  62. struct ieee80211req ireq;
  63. memset(&ireq, 0, sizeof(ireq));
  64. os_strlcpy(ireq.i_name, drv->iface, IFNAMSIZ);
  65. ireq.i_type = op;
  66. ireq.i_len = arg_len;
  67. ireq.i_data = arg;
  68. if (ioctl(drv->ioctl_sock, SIOCG80211, &ireq) < 0) {
  69. perror("ioctl[SIOCG80211]");
  70. return -1;
  71. }
  72. return ireq.i_len;
  73. }
  74. static int
  75. set80211param(struct bsd_driver_data *drv, int op, int arg)
  76. {
  77. struct ieee80211req ireq;
  78. memset(&ireq, 0, sizeof(ireq));
  79. os_strlcpy(ireq.i_name, drv->iface, IFNAMSIZ);
  80. ireq.i_type = op;
  81. ireq.i_val = arg;
  82. if (ioctl(drv->ioctl_sock, SIOCS80211, &ireq) < 0) {
  83. perror("ioctl[SIOCS80211]");
  84. return -1;
  85. }
  86. return 0;
  87. }
  88. static const char *
  89. ether_sprintf(const u8 *addr)
  90. {
  91. static char buf[sizeof(MACSTR)];
  92. if (addr != NULL)
  93. snprintf(buf, sizeof(buf), MACSTR, MAC2STR(addr));
  94. else
  95. snprintf(buf, sizeof(buf), MACSTR, 0,0,0,0,0,0);
  96. return buf;
  97. }
  98. /*
  99. * Configure WPA parameters.
  100. */
  101. static int
  102. bsd_configure_wpa(struct bsd_driver_data *drv)
  103. {
  104. static const char *ciphernames[] =
  105. { "WEP", "TKIP", "AES-OCB", "AES-CCM", "CKIP", "NONE" };
  106. struct hostapd_data *hapd = drv->hapd;
  107. struct hostapd_bss_config *conf = hapd->conf;
  108. int v;
  109. switch (conf->wpa_group) {
  110. case WPA_CIPHER_CCMP:
  111. v = IEEE80211_CIPHER_AES_CCM;
  112. break;
  113. case WPA_CIPHER_TKIP:
  114. v = IEEE80211_CIPHER_TKIP;
  115. break;
  116. case WPA_CIPHER_WEP104:
  117. v = IEEE80211_CIPHER_WEP;
  118. break;
  119. case WPA_CIPHER_WEP40:
  120. v = IEEE80211_CIPHER_WEP;
  121. break;
  122. case WPA_CIPHER_NONE:
  123. v = IEEE80211_CIPHER_NONE;
  124. break;
  125. default:
  126. printf("Unknown group key cipher %u\n",
  127. conf->wpa_group);
  128. return -1;
  129. }
  130. wpa_printf(MSG_DEBUG, "%s: group key cipher=%s (%u)",
  131. __func__, ciphernames[v], v);
  132. if (set80211param(drv, IEEE80211_IOC_MCASTCIPHER, v)) {
  133. printf("Unable to set group key cipher to %u (%s)\n",
  134. v, ciphernames[v]);
  135. return -1;
  136. }
  137. if (v == IEEE80211_CIPHER_WEP) {
  138. /* key length is done only for specific ciphers */
  139. v = (conf->wpa_group == WPA_CIPHER_WEP104 ? 13 : 5);
  140. if (set80211param(drv, IEEE80211_IOC_MCASTKEYLEN, v)) {
  141. printf("Unable to set group key length to %u\n", v);
  142. return -1;
  143. }
  144. }
  145. v = 0;
  146. if (conf->wpa_pairwise & WPA_CIPHER_CCMP)
  147. v |= 1<<IEEE80211_CIPHER_AES_CCM;
  148. if (conf->wpa_pairwise & WPA_CIPHER_TKIP)
  149. v |= 1<<IEEE80211_CIPHER_TKIP;
  150. if (conf->wpa_pairwise & WPA_CIPHER_NONE)
  151. v |= 1<<IEEE80211_CIPHER_NONE;
  152. wpa_printf(MSG_DEBUG, "%s: pairwise key ciphers=0x%x", __func__, v);
  153. if (set80211param(drv, IEEE80211_IOC_UCASTCIPHERS, v)) {
  154. printf("Unable to set pairwise key ciphers to 0x%x\n", v);
  155. return -1;
  156. }
  157. wpa_printf(MSG_DEBUG, "%s: key management algorithms=0x%x",
  158. __func__, conf->wpa_key_mgmt);
  159. if (set80211param(drv, IEEE80211_IOC_KEYMGTALGS, conf->wpa_key_mgmt)) {
  160. printf("Unable to set key management algorithms to 0x%x\n",
  161. conf->wpa_key_mgmt);
  162. return -1;
  163. }
  164. v = 0;
  165. if (conf->rsn_preauth)
  166. v |= BIT(0);
  167. wpa_printf(MSG_DEBUG, "%s: rsn capabilities=0x%x",
  168. __func__, conf->rsn_preauth);
  169. if (set80211param(drv, IEEE80211_IOC_RSNCAPS, v)) {
  170. printf("Unable to set RSN capabilities to 0x%x\n", v);
  171. return -1;
  172. }
  173. wpa_printf(MSG_DEBUG, "%s: enable WPA= 0x%x", __func__, conf->wpa);
  174. if (set80211param(drv, IEEE80211_IOC_WPA, conf->wpa)) {
  175. printf("Unable to set WPA to %u\n", conf->wpa);
  176. return -1;
  177. }
  178. return 0;
  179. }
  180. static int
  181. bsd_set_iface_flags(void *priv, int dev_up)
  182. {
  183. struct bsd_driver_data *drv = priv;
  184. struct ifreq ifr;
  185. wpa_printf(MSG_DEBUG, "%s: dev_up=%d", __func__, dev_up);
  186. if (drv->ioctl_sock < 0)
  187. return -1;
  188. memset(&ifr, 0, sizeof(ifr));
  189. os_strlcpy(ifr.ifr_name, drv->iface, IFNAMSIZ);
  190. if (ioctl(drv->ioctl_sock, SIOCGIFFLAGS, &ifr) != 0) {
  191. perror("ioctl[SIOCGIFFLAGS]");
  192. return -1;
  193. }
  194. if (dev_up)
  195. ifr.ifr_flags |= IFF_UP;
  196. else
  197. ifr.ifr_flags &= ~IFF_UP;
  198. if (ioctl(drv->ioctl_sock, SIOCSIFFLAGS, &ifr) != 0) {
  199. perror("ioctl[SIOCSIFFLAGS]");
  200. return -1;
  201. }
  202. if (dev_up) {
  203. memset(&ifr, 0, sizeof(ifr));
  204. os_strlcpy(ifr.ifr_name, drv->iface, IFNAMSIZ);
  205. ifr.ifr_mtu = HOSTAPD_MTU;
  206. if (ioctl(drv->ioctl_sock, SIOCSIFMTU, &ifr) != 0) {
  207. perror("ioctl[SIOCSIFMTU]");
  208. printf("Setting MTU failed - trying to survive with "
  209. "current value\n");
  210. }
  211. }
  212. return 0;
  213. }
  214. static int
  215. bsd_set_ieee8021x(const char *ifname, void *priv, int enabled)
  216. {
  217. struct bsd_driver_data *drv = priv;
  218. struct hostapd_data *hapd = drv->hapd;
  219. struct hostapd_bss_config *conf = hapd->conf;
  220. wpa_printf(MSG_DEBUG, "%s: enabled=%d", __func__, enabled);
  221. if (!enabled) {
  222. /* XXX restore state */
  223. return set80211param(priv, IEEE80211_IOC_AUTHMODE,
  224. IEEE80211_AUTH_AUTO);
  225. }
  226. if (!conf->wpa && !conf->ieee802_1x) {
  227. hostapd_logger(hapd, NULL, HOSTAPD_MODULE_DRIVER,
  228. HOSTAPD_LEVEL_WARNING, "No 802.1X or WPA enabled!");
  229. return -1;
  230. }
  231. if (conf->wpa && bsd_configure_wpa(drv) != 0) {
  232. hostapd_logger(hapd, NULL, HOSTAPD_MODULE_DRIVER,
  233. HOSTAPD_LEVEL_WARNING, "Error configuring WPA state!");
  234. return -1;
  235. }
  236. if (set80211param(priv, IEEE80211_IOC_AUTHMODE,
  237. (conf->wpa ? IEEE80211_AUTH_WPA : IEEE80211_AUTH_8021X))) {
  238. hostapd_logger(hapd, NULL, HOSTAPD_MODULE_DRIVER,
  239. HOSTAPD_LEVEL_WARNING, "Error enabling WPA/802.1X!");
  240. return -1;
  241. }
  242. return bsd_set_iface_flags(priv, 1);
  243. }
  244. static int
  245. bsd_set_privacy(const char *ifname, void *priv, int enabled)
  246. {
  247. struct bsd_driver_data *drv = priv;
  248. wpa_printf(MSG_DEBUG, "%s: enabled=%d", __func__, enabled);
  249. return set80211param(drv, IEEE80211_IOC_PRIVACY, enabled);
  250. }
  251. static int
  252. bsd_set_sta_authorized(void *priv, const u8 *addr, int authorized)
  253. {
  254. struct bsd_driver_data *drv = priv;
  255. struct ieee80211req_mlme mlme;
  256. wpa_printf(MSG_DEBUG, "%s: addr=%s authorized=%d",
  257. __func__, ether_sprintf(addr), authorized);
  258. if (authorized)
  259. mlme.im_op = IEEE80211_MLME_AUTHORIZE;
  260. else
  261. mlme.im_op = IEEE80211_MLME_UNAUTHORIZE;
  262. mlme.im_reason = 0;
  263. memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
  264. return set80211var(drv, IEEE80211_IOC_MLME, &mlme, sizeof(mlme));
  265. }
  266. static int
  267. bsd_sta_set_flags(void *priv, const u8 *addr, int total_flags, int flags_or,
  268. int flags_and)
  269. {
  270. /* For now, only support setting Authorized flag */
  271. if (flags_or & WLAN_STA_AUTHORIZED)
  272. return bsd_set_sta_authorized(priv, addr, 1);
  273. if (!(flags_and & WLAN_STA_AUTHORIZED))
  274. return bsd_set_sta_authorized(priv, addr, 0);
  275. return 0;
  276. }
  277. static int
  278. bsd_del_key(void *priv, const u8 *addr, int key_idx)
  279. {
  280. struct bsd_driver_data *drv = priv;
  281. struct ieee80211req_del_key wk;
  282. wpa_printf(MSG_DEBUG, "%s: addr=%s key_idx=%d",
  283. __func__, ether_sprintf(addr), key_idx);
  284. memset(&wk, 0, sizeof(wk));
  285. if (addr != NULL) {
  286. memcpy(wk.idk_macaddr, addr, IEEE80211_ADDR_LEN);
  287. wk.idk_keyix = (u_int8_t) IEEE80211_KEYIX_NONE; /* XXX */
  288. } else {
  289. wk.idk_keyix = key_idx;
  290. }
  291. return set80211var(drv, IEEE80211_IOC_DELKEY, &wk, sizeof(wk));
  292. }
  293. static int
  294. bsd_set_key(const char *ifname, void *priv, const char *alg,
  295. const u8 *addr, int key_idx,
  296. const u8 *key, size_t key_len, int txkey)
  297. {
  298. struct bsd_driver_data *drv = priv;
  299. struct ieee80211req_key wk;
  300. u_int8_t cipher;
  301. if (strcmp(alg, "none") == 0)
  302. return bsd_del_key(drv, addr, key_idx);
  303. wpa_printf(MSG_DEBUG, "%s: alg=%s addr=%s key_idx=%d",
  304. __func__, alg, ether_sprintf(addr), key_idx);
  305. if (strcmp(alg, "WEP") == 0)
  306. cipher = IEEE80211_CIPHER_WEP;
  307. else if (strcmp(alg, "TKIP") == 0)
  308. cipher = IEEE80211_CIPHER_TKIP;
  309. else if (strcmp(alg, "CCMP") == 0)
  310. cipher = IEEE80211_CIPHER_AES_CCM;
  311. else {
  312. printf("%s: unknown/unsupported algorithm %s\n",
  313. __func__, alg);
  314. return -1;
  315. }
  316. if (key_len > sizeof(wk.ik_keydata)) {
  317. printf("%s: key length %d too big\n", __func__, key_len);
  318. return -3;
  319. }
  320. memset(&wk, 0, sizeof(wk));
  321. wk.ik_type = cipher;
  322. wk.ik_flags = IEEE80211_KEY_RECV | IEEE80211_KEY_XMIT;
  323. if (addr == NULL) {
  324. memset(wk.ik_macaddr, 0xff, IEEE80211_ADDR_LEN);
  325. wk.ik_keyix = key_idx;
  326. wk.ik_flags |= IEEE80211_KEY_DEFAULT;
  327. } else {
  328. memcpy(wk.ik_macaddr, addr, IEEE80211_ADDR_LEN);
  329. wk.ik_keyix = IEEE80211_KEYIX_NONE;
  330. }
  331. wk.ik_keylen = key_len;
  332. memcpy(wk.ik_keydata, key, key_len);
  333. return set80211var(drv, IEEE80211_IOC_WPAKEY, &wk, sizeof(wk));
  334. }
  335. static int
  336. bsd_get_seqnum(const char *ifname, void *priv, const u8 *addr, int idx,
  337. u8 *seq)
  338. {
  339. struct bsd_driver_data *drv = priv;
  340. struct ieee80211req_key wk;
  341. wpa_printf(MSG_DEBUG, "%s: addr=%s idx=%d",
  342. __func__, ether_sprintf(addr), idx);
  343. memset(&wk, 0, sizeof(wk));
  344. if (addr == NULL)
  345. memset(wk.ik_macaddr, 0xff, IEEE80211_ADDR_LEN);
  346. else
  347. memcpy(wk.ik_macaddr, addr, IEEE80211_ADDR_LEN);
  348. wk.ik_keyix = idx;
  349. if (get80211var(drv, IEEE80211_IOC_WPAKEY, &wk, sizeof(wk)) < 0) {
  350. printf("Failed to get encryption.\n");
  351. return -1;
  352. }
  353. #ifdef WORDS_BIGENDIAN
  354. {
  355. /*
  356. * wk.ik_keytsc is in host byte order (big endian), need to
  357. * swap it to match with the byte order used in WPA.
  358. */
  359. int i;
  360. u8 tmp[WPA_KEY_RSC_LEN];
  361. memcpy(tmp, &wk.ik_keytsc, sizeof(wk.ik_keytsc));
  362. for (i = 0; i < WPA_KEY_RSC_LEN; i++) {
  363. seq[i] = tmp[WPA_KEY_RSC_LEN - i - 1];
  364. }
  365. }
  366. #else /* WORDS_BIGENDIAN */
  367. memcpy(seq, &wk.ik_keytsc, sizeof(wk.ik_keytsc));
  368. #endif /* WORDS_BIGENDIAN */
  369. return 0;
  370. }
  371. static int
  372. bsd_flush(void *priv)
  373. {
  374. u8 allsta[IEEE80211_ADDR_LEN];
  375. memset(allsta, 0xff, IEEE80211_ADDR_LEN);
  376. return bsd_sta_deauth(priv, allsta, IEEE80211_REASON_AUTH_LEAVE);
  377. }
  378. static int
  379. bsd_read_sta_driver_data(void *priv, struct hostap_sta_driver_data *data,
  380. const u8 *addr)
  381. {
  382. struct bsd_driver_data *drv = priv;
  383. struct ieee80211req_sta_stats stats;
  384. memcpy(stats.is_u.macaddr, addr, IEEE80211_ADDR_LEN);
  385. if (get80211var(drv, IEEE80211_IOC_STA_STATS, &stats, sizeof(stats)) > 0) {
  386. /* XXX? do packets counts include non-data frames? */
  387. data->rx_packets = stats.is_stats.ns_rx_data;
  388. data->rx_bytes = stats.is_stats.ns_rx_bytes;
  389. data->tx_packets = stats.is_stats.ns_tx_data;
  390. data->tx_bytes = stats.is_stats.ns_tx_bytes;
  391. }
  392. return 0;
  393. }
  394. static int
  395. bsd_set_opt_ie(const char *ifname, void *priv, const u8 *ie, size_t ie_len)
  396. {
  397. /*
  398. * Do nothing; we setup parameters at startup that define the
  399. * contents of the beacon information element.
  400. */
  401. return 0;
  402. }
  403. static int
  404. bsd_sta_deauth(void *priv, const u8 *addr, int reason_code)
  405. {
  406. struct bsd_driver_data *drv = priv;
  407. struct ieee80211req_mlme mlme;
  408. wpa_printf(MSG_DEBUG, "%s: addr=%s reason_code=%d",
  409. __func__, ether_sprintf(addr), reason_code);
  410. mlme.im_op = IEEE80211_MLME_DEAUTH;
  411. mlme.im_reason = reason_code;
  412. memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
  413. return set80211var(drv, IEEE80211_IOC_MLME, &mlme, sizeof(mlme));
  414. }
  415. static int
  416. bsd_sta_disassoc(void *priv, const u8 *addr, int reason_code)
  417. {
  418. struct bsd_driver_data *drv = priv;
  419. struct ieee80211req_mlme mlme;
  420. wpa_printf(MSG_DEBUG, "%s: addr=%s reason_code=%d",
  421. __func__, ether_sprintf(addr), reason_code);
  422. mlme.im_op = IEEE80211_MLME_DISASSOC;
  423. mlme.im_reason = reason_code;
  424. memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
  425. return set80211var(drv, IEEE80211_IOC_MLME, &mlme, sizeof(mlme));
  426. }
  427. static int
  428. bsd_new_sta(struct bsd_driver_data *drv, u8 addr[IEEE80211_ADDR_LEN])
  429. {
  430. struct hostapd_data *hapd = drv->hapd;
  431. struct ieee80211req_wpaie ie;
  432. int new_assoc, ielen = 0, res;
  433. u8 *iebuf = NULL;
  434. /*
  435. * Fetch and validate any negotiated WPA/RSN parameters.
  436. */
  437. memset(&ie, 0, sizeof(ie));
  438. memcpy(ie.wpa_macaddr, addr, IEEE80211_ADDR_LEN);
  439. if (get80211var(drv, IEEE80211_IOC_WPAIE, &ie, sizeof(ie)) < 0) {
  440. printf("Failed to get WPA/RSN information element.\n");
  441. goto no_ie;
  442. }
  443. iebuf = ie.wpa_ie;
  444. ielen = ie.wpa_ie[1];
  445. if (ielen == 0)
  446. iebuf = NULL;
  447. else
  448. ielen += 2;
  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, 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. ieee80211_michael_mic_failure(hapd, mic->iev_src, 1);
  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. struct hostapd_data *hapd = drv->hapd;
  579. ieee802_1x_receive(hapd, src_addr, 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 wpa_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_encryption = 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. };