driver_atheros.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298
  1. /*
  2. * hostapd / Driver interaction with Atheros driver
  3. * Copyright (c) 2004, Sam Leffler <sam@errno.com>
  4. * Copyright (c) 2004, Video54 Technologies
  5. * Copyright (c) 2005-2007, Jouni Malinen <j@w1.fi>
  6. * Copyright (c) 2009, Atheros Communications
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * Alternatively, this software may be distributed under the terms of BSD
  13. * license.
  14. *
  15. * See README and COPYING for more details.
  16. */
  17. #include "includes.h"
  18. #include <net/if.h>
  19. #include <sys/ioctl.h>
  20. #include "common.h"
  21. #ifndef _BYTE_ORDER
  22. #ifdef WORDS_BIGENDIAN
  23. #define _BYTE_ORDER _BIG_ENDIAN
  24. #else
  25. #define _BYTE_ORDER _LITTLE_ENDIAN
  26. #endif
  27. #endif /* _BYTE_ORDER */
  28. /*
  29. * Note, the ATH_WPS_IE setting must match with the driver build.. If the
  30. * driver does not include this, the IEEE80211_IOCTL_GETWPAIE ioctl will fail.
  31. */
  32. #define ATH_WPS_IE
  33. #include "os/linux/include/ieee80211_external.h"
  34. #ifdef CONFIG_WPS
  35. #include <netpacket/packet.h>
  36. #ifndef ETH_P_80211_RAW
  37. #define ETH_P_80211_RAW 0x0019
  38. #endif
  39. #endif /* CONFIG_WPS */
  40. #include "wireless_copy.h"
  41. #include "driver.h"
  42. #include "eloop.h"
  43. #include "priv_netlink.h"
  44. #include "l2_packet/l2_packet.h"
  45. #include "common/ieee802_11_defs.h"
  46. #include "netlink.h"
  47. #include "linux_ioctl.h"
  48. struct madwifi_driver_data {
  49. struct hostapd_data *hapd; /* back pointer */
  50. char iface[IFNAMSIZ + 1];
  51. int ifindex;
  52. struct l2_packet_data *sock_xmit; /* raw packet xmit socket */
  53. struct l2_packet_data *sock_recv; /* raw packet recv socket */
  54. int ioctl_sock; /* socket for ioctl() use */
  55. struct netlink_data *netlink;
  56. int we_version;
  57. u8 acct_mac[ETH_ALEN];
  58. struct hostap_sta_driver_data acct_data;
  59. struct l2_packet_data *sock_raw; /* raw 802.11 management frames */
  60. };
  61. static int madwifi_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
  62. int reason_code);
  63. static int madwifi_set_privacy(void *priv, int enabled);
  64. static const char * athr_get_ioctl_name(int op)
  65. {
  66. switch (op) {
  67. case IEEE80211_IOCTL_SETPARAM:
  68. return "SETPARAM";
  69. case IEEE80211_IOCTL_GETPARAM:
  70. return "GETPARAM";
  71. case IEEE80211_IOCTL_SETKEY:
  72. return "SETKEY";
  73. case IEEE80211_IOCTL_SETWMMPARAMS:
  74. return "SETWMMPARAMS";
  75. case IEEE80211_IOCTL_DELKEY:
  76. return "DELKEY";
  77. case IEEE80211_IOCTL_GETWMMPARAMS:
  78. return "GETWMMPARAMS";
  79. case IEEE80211_IOCTL_SETMLME:
  80. return "SETMLME";
  81. case IEEE80211_IOCTL_GETCHANINFO:
  82. return "GETCHANINFO";
  83. case IEEE80211_IOCTL_SETOPTIE:
  84. return "SETOPTIE";
  85. case IEEE80211_IOCTL_GETOPTIE:
  86. return "GETOPTIE";
  87. case IEEE80211_IOCTL_ADDMAC:
  88. return "ADDMAC";
  89. case IEEE80211_IOCTL_DELMAC:
  90. return "DELMAC";
  91. case IEEE80211_IOCTL_GETCHANLIST:
  92. return "GETCHANLIST";
  93. case IEEE80211_IOCTL_SETCHANLIST:
  94. return "SETCHANLIST";
  95. case IEEE80211_IOCTL_KICKMAC:
  96. return "KICKMAC";
  97. case IEEE80211_IOCTL_CHANSWITCH:
  98. return "CHANSWITCH";
  99. case IEEE80211_IOCTL_GETMODE:
  100. return "GETMODE";
  101. case IEEE80211_IOCTL_SETMODE:
  102. return "SETMODE";
  103. case IEEE80211_IOCTL_GET_APPIEBUF:
  104. return "GET_APPIEBUF";
  105. case IEEE80211_IOCTL_SET_APPIEBUF:
  106. return "SET_APPIEBUF";
  107. case IEEE80211_IOCTL_SET_ACPARAMS:
  108. return "SET_ACPARAMS";
  109. case IEEE80211_IOCTL_FILTERFRAME:
  110. return "FILTERFRAME";
  111. case IEEE80211_IOCTL_SET_RTPARAMS:
  112. return "SET_RTPARAMS";
  113. case IEEE80211_IOCTL_SENDADDBA:
  114. return "SENDADDBA";
  115. case IEEE80211_IOCTL_GETADDBASTATUS:
  116. return "GETADDBASTATUS";
  117. case IEEE80211_IOCTL_SENDDELBA:
  118. return "SENDDELBA";
  119. case IEEE80211_IOCTL_SET_MEDENYENTRY:
  120. return "SET_MEDENYENTRY";
  121. case IEEE80211_IOCTL_SET_ADDBARESP:
  122. return "SET_ADDBARESP";
  123. case IEEE80211_IOCTL_GET_MACADDR:
  124. return "GET_MACADDR";
  125. case IEEE80211_IOCTL_SET_HBRPARAMS:
  126. return "SET_HBRPARAMS";
  127. case IEEE80211_IOCTL_SET_RXTIMEOUT:
  128. return "SET_RXTIMEOUT";
  129. case IEEE80211_IOCTL_STA_STATS:
  130. return "STA_STATS";
  131. case IEEE80211_IOCTL_GETWPAIE:
  132. return "GETWPAIE";
  133. default:
  134. return "??";
  135. }
  136. }
  137. static const char * athr_get_param_name(int op)
  138. {
  139. switch (op) {
  140. case IEEE80211_IOC_MCASTCIPHER:
  141. return "MCASTCIPHER";
  142. case IEEE80211_PARAM_MCASTKEYLEN:
  143. return "MCASTKEYLEN";
  144. case IEEE80211_PARAM_UCASTCIPHERS:
  145. return "UCASTCIPHERS";
  146. case IEEE80211_PARAM_KEYMGTALGS:
  147. return "KEYMGTALGS";
  148. case IEEE80211_PARAM_RSNCAPS:
  149. return "RSNCAPS";
  150. case IEEE80211_PARAM_WPA:
  151. return "WPA";
  152. case IEEE80211_PARAM_AUTHMODE:
  153. return "AUTHMODE";
  154. case IEEE80211_PARAM_PRIVACY:
  155. return "PRIVACY";
  156. case IEEE80211_PARAM_COUNTERMEASURES:
  157. return "COUNTERMEASURES";
  158. default:
  159. return "??";
  160. }
  161. }
  162. static int
  163. set80211priv(struct madwifi_driver_data *drv, int op, void *data, int len)
  164. {
  165. struct iwreq iwr;
  166. int do_inline = len < IFNAMSIZ;
  167. /* Certain ioctls must use the non-inlined method */
  168. if (op == IEEE80211_IOCTL_SET_APPIEBUF ||
  169. op == IEEE80211_IOCTL_FILTERFRAME)
  170. do_inline = 0;
  171. memset(&iwr, 0, sizeof(iwr));
  172. os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
  173. if (do_inline) {
  174. /*
  175. * Argument data fits inline; put it there.
  176. */
  177. memcpy(iwr.u.name, data, len);
  178. } else {
  179. /*
  180. * Argument data too big for inline transfer; setup a
  181. * parameter block instead; the kernel will transfer
  182. * the data for the driver.
  183. */
  184. iwr.u.data.pointer = data;
  185. iwr.u.data.length = len;
  186. }
  187. if (ioctl(drv->ioctl_sock, op, &iwr) < 0) {
  188. wpa_printf(MSG_DEBUG, "atheros: %s: %s: ioctl op=0x%x "
  189. "(%s) len=%d failed: %d (%s)",
  190. __func__, drv->iface, op,
  191. athr_get_ioctl_name(op),
  192. len, errno, strerror(errno));
  193. return -1;
  194. }
  195. return 0;
  196. }
  197. static int
  198. set80211param(struct madwifi_driver_data *drv, int op, int arg)
  199. {
  200. struct iwreq iwr;
  201. memset(&iwr, 0, sizeof(iwr));
  202. os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
  203. iwr.u.mode = op;
  204. memcpy(iwr.u.name+sizeof(__u32), &arg, sizeof(arg));
  205. if (ioctl(drv->ioctl_sock, IEEE80211_IOCTL_SETPARAM, &iwr) < 0) {
  206. perror("ioctl[IEEE80211_IOCTL_SETPARAM]");
  207. wpa_printf(MSG_DEBUG, "%s: %s: Failed to set parameter (op %d "
  208. "(%s) arg %d)", __func__, drv->iface, op,
  209. athr_get_param_name(op), arg);
  210. return -1;
  211. }
  212. return 0;
  213. }
  214. #ifndef CONFIG_NO_STDOUT_DEBUG
  215. static const char *
  216. ether_sprintf(const u8 *addr)
  217. {
  218. static char buf[sizeof(MACSTR)];
  219. if (addr != NULL)
  220. snprintf(buf, sizeof(buf), MACSTR, MAC2STR(addr));
  221. else
  222. snprintf(buf, sizeof(buf), MACSTR, 0,0,0,0,0,0);
  223. return buf;
  224. }
  225. #endif /* CONFIG_NO_STDOUT_DEBUG */
  226. /*
  227. * Configure WPA parameters.
  228. */
  229. static int
  230. madwifi_configure_wpa(struct madwifi_driver_data *drv,
  231. struct wpa_bss_params *params)
  232. {
  233. int v;
  234. switch (params->wpa_group) {
  235. case WPA_CIPHER_CCMP:
  236. v = IEEE80211_CIPHER_AES_CCM;
  237. break;
  238. case WPA_CIPHER_TKIP:
  239. v = IEEE80211_CIPHER_TKIP;
  240. break;
  241. case WPA_CIPHER_WEP104:
  242. v = IEEE80211_CIPHER_WEP;
  243. break;
  244. case WPA_CIPHER_WEP40:
  245. v = IEEE80211_CIPHER_WEP;
  246. break;
  247. case WPA_CIPHER_NONE:
  248. v = IEEE80211_CIPHER_NONE;
  249. break;
  250. default:
  251. wpa_printf(MSG_ERROR, "Unknown group key cipher %u",
  252. params->wpa_group);
  253. return -1;
  254. }
  255. wpa_printf(MSG_DEBUG, "%s: group key cipher=%d", __func__, v);
  256. if (set80211param(drv, IEEE80211_PARAM_MCASTCIPHER, v)) {
  257. printf("Unable to set group key cipher to %u\n", v);
  258. return -1;
  259. }
  260. if (v == IEEE80211_CIPHER_WEP) {
  261. /* key length is done only for specific ciphers */
  262. v = (params->wpa_group == WPA_CIPHER_WEP104 ? 13 : 5);
  263. if (set80211param(drv, IEEE80211_PARAM_MCASTKEYLEN, v)) {
  264. printf("Unable to set group key length to %u\n", v);
  265. return -1;
  266. }
  267. }
  268. v = 0;
  269. if (params->wpa_pairwise & WPA_CIPHER_CCMP)
  270. v |= 1<<IEEE80211_CIPHER_AES_CCM;
  271. if (params->wpa_pairwise & WPA_CIPHER_TKIP)
  272. v |= 1<<IEEE80211_CIPHER_TKIP;
  273. if (params->wpa_pairwise & WPA_CIPHER_NONE)
  274. v |= 1<<IEEE80211_CIPHER_NONE;
  275. wpa_printf(MSG_DEBUG, "%s: pairwise key ciphers=0x%x", __func__, v);
  276. if (set80211param(drv, IEEE80211_PARAM_UCASTCIPHERS, v)) {
  277. printf("Unable to set pairwise key ciphers to 0x%x\n", v);
  278. return -1;
  279. }
  280. wpa_printf(MSG_DEBUG, "%s: key management algorithms=0x%x",
  281. __func__, params->wpa_key_mgmt);
  282. if (set80211param(drv, IEEE80211_PARAM_KEYMGTALGS,
  283. params->wpa_key_mgmt)) {
  284. printf("Unable to set key management algorithms to 0x%x\n",
  285. params->wpa_key_mgmt);
  286. return -1;
  287. }
  288. v = 0;
  289. if (params->rsn_preauth)
  290. v |= BIT(0);
  291. wpa_printf(MSG_DEBUG, "%s: rsn capabilities=0x%x",
  292. __func__, params->rsn_preauth);
  293. if (set80211param(drv, IEEE80211_PARAM_RSNCAPS, v)) {
  294. printf("Unable to set RSN capabilities to 0x%x\n", v);
  295. return -1;
  296. }
  297. wpa_printf(MSG_DEBUG, "%s: enable WPA=0x%x", __func__, params->wpa);
  298. if (set80211param(drv, IEEE80211_PARAM_WPA, params->wpa)) {
  299. printf("Unable to set WPA to %u\n", params->wpa);
  300. return -1;
  301. }
  302. return 0;
  303. }
  304. static int
  305. madwifi_set_ieee8021x(void *priv, struct wpa_bss_params *params)
  306. {
  307. struct madwifi_driver_data *drv = priv;
  308. wpa_printf(MSG_DEBUG, "%s: enabled=%d", __func__, params->enabled);
  309. if (!params->enabled) {
  310. /* XXX restore state */
  311. if (set80211param(priv, IEEE80211_PARAM_AUTHMODE,
  312. IEEE80211_AUTH_AUTO) < 0)
  313. return -1;
  314. /* IEEE80211_AUTH_AUTO ends up enabling Privacy; clear that */
  315. return madwifi_set_privacy(drv, 0);
  316. }
  317. if (!params->wpa && !params->ieee802_1x) {
  318. hostapd_logger(drv->hapd, NULL, HOSTAPD_MODULE_DRIVER,
  319. HOSTAPD_LEVEL_WARNING, "No 802.1X or WPA enabled!");
  320. return -1;
  321. }
  322. if (params->wpa && madwifi_configure_wpa(drv, params) != 0) {
  323. hostapd_logger(drv->hapd, NULL, HOSTAPD_MODULE_DRIVER,
  324. HOSTAPD_LEVEL_WARNING, "Error configuring WPA state!");
  325. return -1;
  326. }
  327. if (set80211param(priv, IEEE80211_PARAM_AUTHMODE,
  328. (params->wpa ? IEEE80211_AUTH_WPA : IEEE80211_AUTH_8021X))) {
  329. hostapd_logger(drv->hapd, NULL, HOSTAPD_MODULE_DRIVER,
  330. HOSTAPD_LEVEL_WARNING, "Error enabling WPA/802.1X!");
  331. return -1;
  332. }
  333. return 0;
  334. }
  335. static int
  336. madwifi_set_privacy(void *priv, int enabled)
  337. {
  338. struct madwifi_driver_data *drv = priv;
  339. wpa_printf(MSG_DEBUG, "%s: enabled=%d", __func__, enabled);
  340. return set80211param(drv, IEEE80211_PARAM_PRIVACY, enabled);
  341. }
  342. static int
  343. madwifi_set_sta_authorized(void *priv, const u8 *addr, int authorized)
  344. {
  345. struct madwifi_driver_data *drv = priv;
  346. struct ieee80211req_mlme mlme;
  347. int ret;
  348. wpa_printf(MSG_DEBUG, "%s: addr=%s authorized=%d",
  349. __func__, ether_sprintf(addr), authorized);
  350. if (authorized)
  351. mlme.im_op = IEEE80211_MLME_AUTHORIZE;
  352. else
  353. mlme.im_op = IEEE80211_MLME_UNAUTHORIZE;
  354. mlme.im_reason = 0;
  355. memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
  356. ret = set80211priv(drv, IEEE80211_IOCTL_SETMLME, &mlme, sizeof(mlme));
  357. if (ret < 0) {
  358. wpa_printf(MSG_DEBUG, "%s: Failed to %sauthorize STA " MACSTR,
  359. __func__, authorized ? "" : "un", MAC2STR(addr));
  360. }
  361. return ret;
  362. }
  363. static int
  364. madwifi_sta_set_flags(void *priv, const u8 *addr,
  365. int total_flags, int flags_or, int flags_and)
  366. {
  367. /* For now, only support setting Authorized flag */
  368. if (flags_or & WPA_STA_AUTHORIZED)
  369. return madwifi_set_sta_authorized(priv, addr, 1);
  370. if (!(flags_and & WPA_STA_AUTHORIZED))
  371. return madwifi_set_sta_authorized(priv, addr, 0);
  372. return 0;
  373. }
  374. static int
  375. madwifi_del_key(void *priv, const u8 *addr, int key_idx)
  376. {
  377. struct madwifi_driver_data *drv = priv;
  378. struct ieee80211req_del_key wk;
  379. int ret;
  380. wpa_printf(MSG_DEBUG, "%s: addr=%s key_idx=%d",
  381. __func__, ether_sprintf(addr), key_idx);
  382. memset(&wk, 0, sizeof(wk));
  383. if (addr != NULL) {
  384. memcpy(wk.idk_macaddr, addr, IEEE80211_ADDR_LEN);
  385. wk.idk_keyix = (u8) IEEE80211_KEYIX_NONE;
  386. } else {
  387. wk.idk_keyix = key_idx;
  388. }
  389. ret = set80211priv(drv, IEEE80211_IOCTL_DELKEY, &wk, sizeof(wk));
  390. if (ret < 0) {
  391. wpa_printf(MSG_DEBUG, "%s: Failed to delete key (addr %s"
  392. " key_idx %d)", __func__, ether_sprintf(addr),
  393. key_idx);
  394. }
  395. return ret;
  396. }
  397. static int
  398. madwifi_set_key(const char *ifname, void *priv, enum wpa_alg alg,
  399. const u8 *addr, int key_idx, int set_tx, const u8 *seq,
  400. size_t seq_len, const u8 *key, size_t key_len)
  401. {
  402. struct madwifi_driver_data *drv = priv;
  403. struct ieee80211req_key wk;
  404. u_int8_t cipher;
  405. int ret;
  406. if (alg == WPA_ALG_NONE)
  407. return madwifi_del_key(drv, addr, key_idx);
  408. wpa_printf(MSG_DEBUG, "%s: alg=%d addr=%s key_idx=%d",
  409. __func__, alg, ether_sprintf(addr), key_idx);
  410. switch (alg) {
  411. case WPA_ALG_WEP:
  412. cipher = IEEE80211_CIPHER_WEP;
  413. break;
  414. case WPA_ALG_TKIP:
  415. cipher = IEEE80211_CIPHER_TKIP;
  416. break;
  417. case WPA_ALG_CCMP:
  418. cipher = IEEE80211_CIPHER_AES_CCM;
  419. break;
  420. default:
  421. printf("%s: unknown/unsupported algorithm %d\n",
  422. __func__, alg);
  423. return -1;
  424. }
  425. if (key_len > sizeof(wk.ik_keydata)) {
  426. printf("%s: key length %lu too big\n", __func__,
  427. (unsigned long) key_len);
  428. return -3;
  429. }
  430. memset(&wk, 0, sizeof(wk));
  431. wk.ik_type = cipher;
  432. wk.ik_flags = IEEE80211_KEY_RECV | IEEE80211_KEY_XMIT;
  433. if (addr == NULL) {
  434. memset(wk.ik_macaddr, 0xff, IEEE80211_ADDR_LEN);
  435. wk.ik_keyix = key_idx;
  436. wk.ik_flags |= IEEE80211_KEY_DEFAULT;
  437. } else {
  438. memcpy(wk.ik_macaddr, addr, IEEE80211_ADDR_LEN);
  439. wk.ik_keyix = IEEE80211_KEYIX_NONE;
  440. }
  441. wk.ik_keylen = key_len;
  442. memcpy(wk.ik_keydata, key, key_len);
  443. ret = set80211priv(drv, IEEE80211_IOCTL_SETKEY, &wk, sizeof(wk));
  444. if (ret < 0) {
  445. wpa_printf(MSG_DEBUG, "%s: Failed to set key (addr %s"
  446. " key_idx %d alg %d key_len %lu set_tx %d)",
  447. __func__, ether_sprintf(wk.ik_macaddr), key_idx,
  448. alg, (unsigned long) key_len, set_tx);
  449. }
  450. return ret;
  451. }
  452. static int
  453. madwifi_get_seqnum(const char *ifname, void *priv, const u8 *addr, int idx,
  454. u8 *seq)
  455. {
  456. struct madwifi_driver_data *drv = priv;
  457. struct ieee80211req_key wk;
  458. wpa_printf(MSG_DEBUG, "%s: addr=%s idx=%d",
  459. __func__, ether_sprintf(addr), idx);
  460. memset(&wk, 0, sizeof(wk));
  461. if (addr == NULL)
  462. memset(wk.ik_macaddr, 0xff, IEEE80211_ADDR_LEN);
  463. else
  464. memcpy(wk.ik_macaddr, addr, IEEE80211_ADDR_LEN);
  465. wk.ik_keyix = idx;
  466. if (set80211priv(drv, IEEE80211_IOCTL_GETKEY, &wk, sizeof(wk))) {
  467. wpa_printf(MSG_DEBUG, "%s: Failed to get encryption data "
  468. "(addr " MACSTR " key_idx %d)",
  469. __func__, MAC2STR(wk.ik_macaddr), idx);
  470. return -1;
  471. }
  472. #ifdef WORDS_BIGENDIAN
  473. {
  474. /*
  475. * wk.ik_keytsc is in host byte order (big endian), need to
  476. * swap it to match with the byte order used in WPA.
  477. */
  478. int i;
  479. #ifndef WPA_KEY_RSC_LEN
  480. #define WPA_KEY_RSC_LEN 8
  481. #endif
  482. u8 tmp[WPA_KEY_RSC_LEN];
  483. memcpy(tmp, &wk.ik_keytsc, sizeof(wk.ik_keytsc));
  484. for (i = 0; i < WPA_KEY_RSC_LEN; i++) {
  485. seq[i] = tmp[WPA_KEY_RSC_LEN - i - 1];
  486. }
  487. }
  488. #else /* WORDS_BIGENDIAN */
  489. memcpy(seq, &wk.ik_keytsc, sizeof(wk.ik_keytsc));
  490. #endif /* WORDS_BIGENDIAN */
  491. return 0;
  492. }
  493. static int
  494. madwifi_flush(void *priv)
  495. {
  496. u8 allsta[IEEE80211_ADDR_LEN];
  497. memset(allsta, 0xff, IEEE80211_ADDR_LEN);
  498. return madwifi_sta_deauth(priv, NULL, allsta,
  499. IEEE80211_REASON_AUTH_LEAVE);
  500. }
  501. static int
  502. madwifi_read_sta_driver_data(void *priv, struct hostap_sta_driver_data *data,
  503. const u8 *addr)
  504. {
  505. struct madwifi_driver_data *drv = priv;
  506. struct ieee80211req_sta_stats stats;
  507. memset(data, 0, sizeof(*data));
  508. /*
  509. * Fetch statistics for station from the system.
  510. */
  511. memset(&stats, 0, sizeof(stats));
  512. memcpy(stats.is_u.macaddr, addr, IEEE80211_ADDR_LEN);
  513. if (set80211priv(drv, IEEE80211_IOCTL_STA_STATS,
  514. &stats, sizeof(stats))) {
  515. wpa_printf(MSG_DEBUG, "%s: Failed to fetch STA stats (addr "
  516. MACSTR ")", __func__, MAC2STR(addr));
  517. if (memcmp(addr, drv->acct_mac, ETH_ALEN) == 0) {
  518. memcpy(data, &drv->acct_data, sizeof(*data));
  519. return 0;
  520. }
  521. printf("Failed to get station stats information element.\n");
  522. return -1;
  523. }
  524. data->rx_packets = stats.is_stats.ns_rx_data;
  525. data->rx_bytes = stats.is_stats.ns_rx_bytes;
  526. data->tx_packets = stats.is_stats.ns_tx_data;
  527. data->tx_bytes = stats.is_stats.ns_tx_bytes;
  528. return 0;
  529. }
  530. static int
  531. madwifi_sta_clear_stats(void *priv, const u8 *addr)
  532. {
  533. struct madwifi_driver_data *drv = priv;
  534. struct ieee80211req_mlme mlme;
  535. int ret;
  536. wpa_printf(MSG_DEBUG, "%s: addr=%s", __func__, ether_sprintf(addr));
  537. mlme.im_op = IEEE80211_MLME_CLEAR_STATS;
  538. memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
  539. ret = set80211priv(drv, IEEE80211_IOCTL_SETMLME, &mlme,
  540. sizeof(mlme));
  541. if (ret < 0) {
  542. wpa_printf(MSG_DEBUG, "%s: Failed to clear STA stats (addr "
  543. MACSTR ")", __func__, MAC2STR(addr));
  544. }
  545. return ret;
  546. }
  547. static int
  548. madwifi_set_opt_ie(void *priv, const u8 *ie, size_t ie_len)
  549. {
  550. /*
  551. * Do nothing; we setup parameters at startup that define the
  552. * contents of the beacon information element.
  553. */
  554. return 0;
  555. }
  556. static int
  557. madwifi_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
  558. int reason_code)
  559. {
  560. struct madwifi_driver_data *drv = priv;
  561. struct ieee80211req_mlme mlme;
  562. int ret;
  563. wpa_printf(MSG_DEBUG, "%s: addr=%s reason_code=%d",
  564. __func__, ether_sprintf(addr), reason_code);
  565. mlme.im_op = IEEE80211_MLME_DEAUTH;
  566. mlme.im_reason = reason_code;
  567. memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
  568. ret = set80211priv(drv, IEEE80211_IOCTL_SETMLME, &mlme, sizeof(mlme));
  569. if (ret < 0) {
  570. wpa_printf(MSG_DEBUG, "%s: Failed to deauth STA (addr " MACSTR
  571. " reason %d)",
  572. __func__, MAC2STR(addr), reason_code);
  573. }
  574. return ret;
  575. }
  576. static int
  577. madwifi_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
  578. int reason_code)
  579. {
  580. struct madwifi_driver_data *drv = priv;
  581. struct ieee80211req_mlme mlme;
  582. int ret;
  583. wpa_printf(MSG_DEBUG, "%s: addr=%s reason_code=%d",
  584. __func__, ether_sprintf(addr), reason_code);
  585. mlme.im_op = IEEE80211_MLME_DISASSOC;
  586. mlme.im_reason = reason_code;
  587. memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
  588. ret = set80211priv(drv, IEEE80211_IOCTL_SETMLME, &mlme, sizeof(mlme));
  589. if (ret < 0) {
  590. wpa_printf(MSG_DEBUG, "%s: Failed to disassoc STA (addr "
  591. MACSTR " reason %d)",
  592. __func__, MAC2STR(addr), reason_code);
  593. }
  594. return ret;
  595. }
  596. #ifdef CONFIG_WPS
  597. static void madwifi_raw_receive(void *ctx, const u8 *src_addr, const u8 *buf,
  598. size_t len)
  599. {
  600. struct madwifi_driver_data *drv = ctx;
  601. const struct ieee80211_mgmt *mgmt;
  602. u16 fc;
  603. union wpa_event_data event;
  604. /* Send Probe Request information to WPS processing */
  605. if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.probe_req))
  606. return;
  607. mgmt = (const struct ieee80211_mgmt *) buf;
  608. fc = le_to_host16(mgmt->frame_control);
  609. if (WLAN_FC_GET_TYPE(fc) != WLAN_FC_TYPE_MGMT ||
  610. WLAN_FC_GET_STYPE(fc) != WLAN_FC_STYPE_PROBE_REQ)
  611. return;
  612. os_memset(&event, 0, sizeof(event));
  613. event.rx_probe_req.sa = mgmt->sa;
  614. event.rx_probe_req.ie = mgmt->u.probe_req.variable;
  615. event.rx_probe_req.ie_len =
  616. len - (IEEE80211_HDRLEN + sizeof(mgmt->u.probe_req));
  617. wpa_supplicant_event(drv->hapd, EVENT_RX_PROBE_REQ, &event);
  618. }
  619. #endif /* CONFIG_WPS */
  620. static int madwifi_receive_probe_req(struct madwifi_driver_data *drv)
  621. {
  622. int ret = 0;
  623. #ifdef CONFIG_WPS
  624. struct ieee80211req_set_filter filt;
  625. wpa_printf(MSG_DEBUG, "%s Enter", __func__);
  626. filt.app_filterype = IEEE80211_FILTER_TYPE_PROBE_REQ;
  627. ret = set80211priv(drv, IEEE80211_IOCTL_FILTERFRAME, &filt,
  628. sizeof(struct ieee80211req_set_filter));
  629. if (ret)
  630. return ret;
  631. drv->sock_raw = l2_packet_init(drv->iface, NULL, ETH_P_80211_RAW,
  632. madwifi_raw_receive, drv, 1);
  633. if (drv->sock_raw == NULL)
  634. return -1;
  635. #endif /* CONFIG_WPS */
  636. return ret;
  637. }
  638. #ifdef CONFIG_WPS
  639. static int
  640. madwifi_set_wps_ie(void *priv, const u8 *ie, size_t len, u32 frametype)
  641. {
  642. struct madwifi_driver_data *drv = priv;
  643. u8 buf[256];
  644. struct ieee80211req_getset_appiebuf *beac_ie;
  645. wpa_printf(MSG_DEBUG, "%s buflen = %lu", __func__,
  646. (unsigned long) len);
  647. beac_ie = (struct ieee80211req_getset_appiebuf *) buf;
  648. beac_ie->app_frmtype = frametype;
  649. beac_ie->app_buflen = len;
  650. memcpy(&(beac_ie->app_buf[0]), ie, len);
  651. return set80211priv(drv, IEEE80211_IOCTL_SET_APPIEBUF, beac_ie,
  652. sizeof(struct ieee80211req_getset_appiebuf) + len);
  653. }
  654. static int
  655. madwifi_set_ap_wps_ie(void *priv, const struct wpabuf *beacon,
  656. const struct wpabuf *proberesp)
  657. {
  658. if (madwifi_set_wps_ie(priv, beacon ? wpabuf_head(beacon) : NULL,
  659. beacon ? wpabuf_len(beacon) : 0,
  660. IEEE80211_APPIE_FRAME_BEACON))
  661. return -1;
  662. return madwifi_set_wps_ie(priv,
  663. proberesp ? wpabuf_head(proberesp) : NULL,
  664. proberesp ? wpabuf_len(proberesp): 0,
  665. IEEE80211_APPIE_FRAME_PROBE_RESP);
  666. }
  667. #else /* CONFIG_WPS */
  668. #define madwifi_set_ap_wps_ie NULL
  669. #endif /* CONFIG_WPS */
  670. static void
  671. madwifi_new_sta(struct madwifi_driver_data *drv, u8 addr[IEEE80211_ADDR_LEN])
  672. {
  673. struct hostapd_data *hapd = drv->hapd;
  674. struct ieee80211req_wpaie ie;
  675. int ielen = 0;
  676. u8 *iebuf = NULL;
  677. /*
  678. * Fetch negotiated WPA/RSN parameters from the system.
  679. */
  680. memset(&ie, 0, sizeof(ie));
  681. memcpy(ie.wpa_macaddr, addr, IEEE80211_ADDR_LEN);
  682. if (set80211priv(drv, IEEE80211_IOCTL_GETWPAIE, &ie, sizeof(ie))) {
  683. /*
  684. * See ATH_WPS_IE comment in the beginning of the file for a
  685. * possible cause for the failure..
  686. */
  687. wpa_printf(MSG_DEBUG, "%s: Failed to get WPA/RSN IE: %s",
  688. __func__, strerror(errno));
  689. goto no_ie;
  690. }
  691. wpa_hexdump(MSG_MSGDUMP, "madwifi req WPA IE",
  692. ie.wpa_ie, IEEE80211_MAX_OPT_IE);
  693. wpa_hexdump(MSG_MSGDUMP, "madwifi req RSN IE",
  694. ie.rsn_ie, IEEE80211_MAX_OPT_IE);
  695. iebuf = ie.wpa_ie;
  696. /* madwifi seems to return some random data if WPA/RSN IE is not set.
  697. * Assume the IE was not included if the IE type is unknown. */
  698. if (iebuf[0] != WLAN_EID_VENDOR_SPECIFIC)
  699. iebuf[1] = 0;
  700. if (iebuf[1] == 0 && ie.rsn_ie[1] > 0) {
  701. /* madwifi-ng svn #1453 added rsn_ie. Use it, if wpa_ie was not
  702. * set. This is needed for WPA2. */
  703. iebuf = ie.rsn_ie;
  704. if (iebuf[0] != WLAN_EID_RSN)
  705. iebuf[1] = 0;
  706. }
  707. ielen = iebuf[1];
  708. if (ielen == 0)
  709. iebuf = NULL;
  710. else
  711. ielen += 2;
  712. no_ie:
  713. drv_event_assoc(hapd, addr, iebuf, ielen);
  714. if (memcmp(addr, drv->acct_mac, ETH_ALEN) == 0) {
  715. /* Cached accounting data is not valid anymore. */
  716. memset(drv->acct_mac, 0, ETH_ALEN);
  717. memset(&drv->acct_data, 0, sizeof(drv->acct_data));
  718. }
  719. }
  720. static void
  721. madwifi_wireless_event_wireless_custom(struct madwifi_driver_data *drv,
  722. char *custom, char *end)
  723. {
  724. wpa_printf(MSG_DEBUG, "Custom wireless event: '%s'", custom);
  725. if (strncmp(custom, "MLME-MICHAELMICFAILURE.indication", 33) == 0) {
  726. char *pos;
  727. u8 addr[ETH_ALEN];
  728. pos = strstr(custom, "addr=");
  729. if (pos == NULL) {
  730. wpa_printf(MSG_DEBUG,
  731. "MLME-MICHAELMICFAILURE.indication "
  732. "without sender address ignored");
  733. return;
  734. }
  735. pos += 5;
  736. if (hwaddr_aton(pos, addr) == 0) {
  737. union wpa_event_data data;
  738. os_memset(&data, 0, sizeof(data));
  739. data.michael_mic_failure.unicast = 1;
  740. data.michael_mic_failure.src = addr;
  741. wpa_supplicant_event(drv->hapd,
  742. EVENT_MICHAEL_MIC_FAILURE, &data);
  743. } else {
  744. wpa_printf(MSG_DEBUG,
  745. "MLME-MICHAELMICFAILURE.indication "
  746. "with invalid MAC address");
  747. }
  748. } else if (strncmp(custom, "STA-TRAFFIC-STAT", 16) == 0) {
  749. char *key, *value;
  750. u32 val;
  751. key = custom;
  752. while ((key = strchr(key, '\n')) != NULL) {
  753. key++;
  754. value = strchr(key, '=');
  755. if (value == NULL)
  756. continue;
  757. *value++ = '\0';
  758. val = strtoul(value, NULL, 10);
  759. if (strcmp(key, "mac") == 0)
  760. hwaddr_aton(value, drv->acct_mac);
  761. else if (strcmp(key, "rx_packets") == 0)
  762. drv->acct_data.rx_packets = val;
  763. else if (strcmp(key, "tx_packets") == 0)
  764. drv->acct_data.tx_packets = val;
  765. else if (strcmp(key, "rx_bytes") == 0)
  766. drv->acct_data.rx_bytes = val;
  767. else if (strcmp(key, "tx_bytes") == 0)
  768. drv->acct_data.tx_bytes = val;
  769. key = value;
  770. }
  771. #ifdef CONFIG_WPS
  772. } else if (strncmp(custom, "PUSH-BUTTON.indication", 22) == 0) {
  773. /* Some atheros kernels send push button as a wireless event */
  774. /* PROBLEM! this event is received for ALL BSSs ...
  775. * so all are enabled for WPS... ugh.
  776. */
  777. wpa_supplicant_event(drv->hapd, EVENT_WPS_BUTTON_PUSHED, NULL);
  778. } else if (strncmp(custom, "Manage.prob_req ", 16) == 0) {
  779. /*
  780. * Atheros driver uses a hack to pass Probe Request frames as a
  781. * binary data in the custom wireless event. The old way (using
  782. * packet sniffing) didn't work when bridging.
  783. * Format: "Manage.prob_req <frame len>" | zero padding | frame
  784. */
  785. #define WPS_FRAM_TAG_SIZE 30 /* hardcoded in driver */
  786. int len = atoi(custom + 16);
  787. if (len < 0 || custom + WPS_FRAM_TAG_SIZE + len > end) {
  788. wpa_printf(MSG_DEBUG, "Invalid Manage.prob_req event "
  789. "length %d", len);
  790. return;
  791. }
  792. madwifi_raw_receive(drv, NULL,
  793. (u8 *) custom + WPS_FRAM_TAG_SIZE, len);
  794. #endif /* CONFIG_WPS */
  795. }
  796. }
  797. static void
  798. madwifi_wireless_event_wireless(struct madwifi_driver_data *drv,
  799. char *data, int len)
  800. {
  801. struct iw_event iwe_buf, *iwe = &iwe_buf;
  802. char *pos, *end, *custom, *buf;
  803. pos = data;
  804. end = data + len;
  805. while (pos + IW_EV_LCP_LEN <= end) {
  806. /* Event data may be unaligned, so make a local, aligned copy
  807. * before processing. */
  808. memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
  809. wpa_printf(MSG_MSGDUMP, "Wireless event: cmd=0x%x len=%d",
  810. iwe->cmd, iwe->len);
  811. if (iwe->len <= IW_EV_LCP_LEN)
  812. return;
  813. custom = pos + IW_EV_POINT_LEN;
  814. if (drv->we_version > 18 &&
  815. (iwe->cmd == IWEVMICHAELMICFAILURE ||
  816. iwe->cmd == IWEVASSOCREQIE ||
  817. iwe->cmd == IWEVCUSTOM)) {
  818. /* WE-19 removed the pointer from struct iw_point */
  819. char *dpos = (char *) &iwe_buf.u.data.length;
  820. int dlen = dpos - (char *) &iwe_buf;
  821. memcpy(dpos, pos + IW_EV_LCP_LEN,
  822. sizeof(struct iw_event) - dlen);
  823. } else {
  824. memcpy(&iwe_buf, pos, sizeof(struct iw_event));
  825. custom += IW_EV_POINT_OFF;
  826. }
  827. switch (iwe->cmd) {
  828. case IWEVEXPIRED:
  829. drv_event_disassoc(drv->hapd,
  830. (u8 *) iwe->u.addr.sa_data);
  831. break;
  832. case IWEVREGISTERED:
  833. madwifi_new_sta(drv, (u8 *) iwe->u.addr.sa_data);
  834. break;
  835. case IWEVASSOCREQIE:
  836. /* Driver hack.. Use IWEVASSOCREQIE to bypass
  837. * IWEVCUSTOM size limitations. Need to handle this
  838. * just like IWEVCUSTOM.
  839. */
  840. case IWEVCUSTOM:
  841. if (custom + iwe->u.data.length > end)
  842. return;
  843. buf = malloc(iwe->u.data.length + 1);
  844. if (buf == NULL)
  845. return; /* XXX */
  846. memcpy(buf, custom, iwe->u.data.length);
  847. buf[iwe->u.data.length] = '\0';
  848. madwifi_wireless_event_wireless_custom(
  849. drv, buf, buf + iwe->u.data.length);
  850. free(buf);
  851. break;
  852. }
  853. pos += iwe->len;
  854. }
  855. }
  856. static void
  857. madwifi_wireless_event_rtm_newlink(void *ctx,
  858. struct ifinfomsg *ifi, u8 *buf, size_t len)
  859. {
  860. struct madwifi_driver_data *drv = ctx;
  861. int attrlen, rta_len;
  862. struct rtattr *attr;
  863. if (ifi->ifi_index != drv->ifindex)
  864. return;
  865. attrlen = len;
  866. attr = (struct rtattr *) buf;
  867. rta_len = RTA_ALIGN(sizeof(struct rtattr));
  868. while (RTA_OK(attr, attrlen)) {
  869. if (attr->rta_type == IFLA_WIRELESS) {
  870. madwifi_wireless_event_wireless(
  871. drv, ((char *) attr) + rta_len,
  872. attr->rta_len - rta_len);
  873. }
  874. attr = RTA_NEXT(attr, attrlen);
  875. }
  876. }
  877. static int
  878. madwifi_get_we_version(struct madwifi_driver_data *drv)
  879. {
  880. struct iw_range *range;
  881. struct iwreq iwr;
  882. int minlen;
  883. size_t buflen;
  884. drv->we_version = 0;
  885. /*
  886. * Use larger buffer than struct iw_range in order to allow the
  887. * structure to grow in the future.
  888. */
  889. buflen = sizeof(struct iw_range) + 500;
  890. range = os_zalloc(buflen);
  891. if (range == NULL)
  892. return -1;
  893. memset(&iwr, 0, sizeof(iwr));
  894. os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
  895. iwr.u.data.pointer = (caddr_t) range;
  896. iwr.u.data.length = buflen;
  897. minlen = ((char *) &range->enc_capa) - (char *) range +
  898. sizeof(range->enc_capa);
  899. if (ioctl(drv->ioctl_sock, SIOCGIWRANGE, &iwr) < 0) {
  900. perror("ioctl[SIOCGIWRANGE]");
  901. free(range);
  902. return -1;
  903. } else if (iwr.u.data.length >= minlen &&
  904. range->we_version_compiled >= 18) {
  905. wpa_printf(MSG_DEBUG, "SIOCGIWRANGE: WE(compiled)=%d "
  906. "WE(source)=%d enc_capa=0x%x",
  907. range->we_version_compiled,
  908. range->we_version_source,
  909. range->enc_capa);
  910. drv->we_version = range->we_version_compiled;
  911. }
  912. free(range);
  913. return 0;
  914. }
  915. static int
  916. madwifi_wireless_event_init(struct madwifi_driver_data *drv)
  917. {
  918. struct netlink_config *cfg;
  919. madwifi_get_we_version(drv);
  920. cfg = os_zalloc(sizeof(*cfg));
  921. if (cfg == NULL)
  922. return -1;
  923. cfg->ctx = drv;
  924. cfg->newlink_cb = madwifi_wireless_event_rtm_newlink;
  925. drv->netlink = netlink_init(cfg);
  926. if (drv->netlink == NULL) {
  927. os_free(cfg);
  928. return -1;
  929. }
  930. return 0;
  931. }
  932. static int
  933. madwifi_send_eapol(void *priv, const u8 *addr, const u8 *data, size_t data_len,
  934. int encrypt, const u8 *own_addr)
  935. {
  936. struct madwifi_driver_data *drv = priv;
  937. unsigned char buf[3000];
  938. unsigned char *bp = buf;
  939. struct l2_ethhdr *eth;
  940. size_t len;
  941. int status;
  942. /*
  943. * Prepend the Ethernet header. If the caller left us
  944. * space at the front we could just insert it but since
  945. * we don't know we copy to a local buffer. Given the frequency
  946. * and size of frames this probably doesn't matter.
  947. */
  948. len = data_len + sizeof(struct l2_ethhdr);
  949. if (len > sizeof(buf)) {
  950. bp = malloc(len);
  951. if (bp == NULL) {
  952. printf("EAPOL frame discarded, cannot malloc temp "
  953. "buffer of size %lu!\n", (unsigned long) len);
  954. return -1;
  955. }
  956. }
  957. eth = (struct l2_ethhdr *) bp;
  958. memcpy(eth->h_dest, addr, ETH_ALEN);
  959. memcpy(eth->h_source, own_addr, ETH_ALEN);
  960. eth->h_proto = host_to_be16(ETH_P_EAPOL);
  961. memcpy(eth+1, data, data_len);
  962. wpa_hexdump(MSG_MSGDUMP, "TX EAPOL", bp, len);
  963. status = l2_packet_send(drv->sock_xmit, addr, ETH_P_EAPOL, bp, len);
  964. if (bp != buf)
  965. free(bp);
  966. return status;
  967. }
  968. static void
  969. handle_read(void *ctx, const u8 *src_addr, const u8 *buf, size_t len)
  970. {
  971. struct madwifi_driver_data *drv = ctx;
  972. drv_event_eapol_rx(drv->hapd, src_addr, buf + sizeof(struct l2_ethhdr),
  973. len - sizeof(struct l2_ethhdr));
  974. }
  975. static void *
  976. madwifi_init(struct hostapd_data *hapd, struct wpa_init_params *params)
  977. {
  978. struct madwifi_driver_data *drv;
  979. struct ifreq ifr;
  980. struct iwreq iwr;
  981. char brname[IFNAMSIZ];
  982. drv = os_zalloc(sizeof(struct madwifi_driver_data));
  983. if (drv == NULL) {
  984. printf("Could not allocate memory for madwifi driver data\n");
  985. return NULL;
  986. }
  987. drv->hapd = hapd;
  988. drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
  989. if (drv->ioctl_sock < 0) {
  990. perror("socket[PF_INET,SOCK_DGRAM]");
  991. goto bad;
  992. }
  993. memcpy(drv->iface, params->ifname, sizeof(drv->iface));
  994. memset(&ifr, 0, sizeof(ifr));
  995. os_strlcpy(ifr.ifr_name, drv->iface, sizeof(ifr.ifr_name));
  996. if (ioctl(drv->ioctl_sock, SIOCGIFINDEX, &ifr) != 0) {
  997. perror("ioctl(SIOCGIFINDEX)");
  998. goto bad;
  999. }
  1000. drv->ifindex = ifr.ifr_ifindex;
  1001. drv->sock_xmit = l2_packet_init(drv->iface, NULL, ETH_P_EAPOL,
  1002. handle_read, drv, 1);
  1003. if (drv->sock_xmit == NULL)
  1004. goto bad;
  1005. if (l2_packet_get_own_addr(drv->sock_xmit, params->own_addr))
  1006. goto bad;
  1007. if (params->bridge[0]) {
  1008. wpa_printf(MSG_DEBUG, "Configure bridge %s for EAPOL traffic.",
  1009. params->bridge[0]);
  1010. drv->sock_recv = l2_packet_init(params->bridge[0], NULL,
  1011. ETH_P_EAPOL, handle_read, drv,
  1012. 1);
  1013. if (drv->sock_recv == NULL)
  1014. goto bad;
  1015. } else if (linux_br_get(brname, drv->iface) == 0) {
  1016. wpa_printf(MSG_DEBUG, "Interface in bridge %s; configure for "
  1017. "EAPOL receive", brname);
  1018. drv->sock_recv = l2_packet_init(brname, NULL, ETH_P_EAPOL,
  1019. handle_read, drv, 1);
  1020. if (drv->sock_recv == NULL)
  1021. goto bad;
  1022. } else
  1023. drv->sock_recv = drv->sock_xmit;
  1024. memset(&iwr, 0, sizeof(iwr));
  1025. os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
  1026. iwr.u.mode = IW_MODE_MASTER;
  1027. if (ioctl(drv->ioctl_sock, SIOCSIWMODE, &iwr) < 0) {
  1028. perror("ioctl[SIOCSIWMODE]");
  1029. printf("Could not set interface to master mode!\n");
  1030. goto bad;
  1031. }
  1032. /* mark down during setup */
  1033. linux_set_iface_flags(drv->ioctl_sock, drv->iface, 0);
  1034. madwifi_set_privacy(drv, 0); /* default to no privacy */
  1035. madwifi_receive_probe_req(drv);
  1036. if (madwifi_wireless_event_init(drv))
  1037. goto bad;
  1038. return drv;
  1039. bad:
  1040. if (drv->sock_recv != NULL && drv->sock_recv != drv->sock_xmit)
  1041. l2_packet_deinit(drv->sock_recv);
  1042. if (drv->sock_xmit != NULL)
  1043. l2_packet_deinit(drv->sock_xmit);
  1044. if (drv->ioctl_sock >= 0)
  1045. close(drv->ioctl_sock);
  1046. if (drv != NULL)
  1047. free(drv);
  1048. return NULL;
  1049. }
  1050. static void
  1051. madwifi_deinit(void *priv)
  1052. {
  1053. struct madwifi_driver_data *drv = priv;
  1054. netlink_deinit(drv->netlink);
  1055. (void) linux_set_iface_flags(drv->ioctl_sock, drv->iface, 0);
  1056. if (drv->ioctl_sock >= 0)
  1057. close(drv->ioctl_sock);
  1058. if (drv->sock_recv != NULL && drv->sock_recv != drv->sock_xmit)
  1059. l2_packet_deinit(drv->sock_recv);
  1060. if (drv->sock_xmit != NULL)
  1061. l2_packet_deinit(drv->sock_xmit);
  1062. if (drv->sock_raw)
  1063. l2_packet_deinit(drv->sock_raw);
  1064. free(drv);
  1065. }
  1066. static int
  1067. madwifi_set_ssid(void *priv, const u8 *buf, int len)
  1068. {
  1069. struct madwifi_driver_data *drv = priv;
  1070. struct iwreq iwr;
  1071. memset(&iwr, 0, sizeof(iwr));
  1072. os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
  1073. iwr.u.essid.flags = 1; /* SSID active */
  1074. iwr.u.essid.pointer = (caddr_t) buf;
  1075. iwr.u.essid.length = len + 1;
  1076. if (ioctl(drv->ioctl_sock, SIOCSIWESSID, &iwr) < 0) {
  1077. perror("ioctl[SIOCSIWESSID]");
  1078. printf("len=%d\n", len);
  1079. return -1;
  1080. }
  1081. return 0;
  1082. }
  1083. static int
  1084. madwifi_get_ssid(void *priv, u8 *buf, int len)
  1085. {
  1086. struct madwifi_driver_data *drv = priv;
  1087. struct iwreq iwr;
  1088. int ret = 0;
  1089. memset(&iwr, 0, sizeof(iwr));
  1090. os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
  1091. iwr.u.essid.pointer = (caddr_t) buf;
  1092. iwr.u.essid.length = len;
  1093. if (ioctl(drv->ioctl_sock, SIOCGIWESSID, &iwr) < 0) {
  1094. perror("ioctl[SIOCGIWESSID]");
  1095. ret = -1;
  1096. } else
  1097. ret = iwr.u.essid.length;
  1098. return ret;
  1099. }
  1100. static int
  1101. madwifi_set_countermeasures(void *priv, int enabled)
  1102. {
  1103. struct madwifi_driver_data *drv = priv;
  1104. wpa_printf(MSG_DEBUG, "%s: enabled=%d", __FUNCTION__, enabled);
  1105. return set80211param(drv, IEEE80211_PARAM_COUNTERMEASURES, enabled);
  1106. }
  1107. static int
  1108. madwifi_commit(void *priv)
  1109. {
  1110. struct madwifi_driver_data *drv = priv;
  1111. return linux_set_iface_flags(drv->ioctl_sock, drv->iface, 1);
  1112. }
  1113. const struct wpa_driver_ops wpa_driver_atheros_ops = {
  1114. .name = "atheros",
  1115. .hapd_init = madwifi_init,
  1116. .hapd_deinit = madwifi_deinit,
  1117. .set_ieee8021x = madwifi_set_ieee8021x,
  1118. .set_privacy = madwifi_set_privacy,
  1119. .set_key = madwifi_set_key,
  1120. .get_seqnum = madwifi_get_seqnum,
  1121. .flush = madwifi_flush,
  1122. .set_generic_elem = madwifi_set_opt_ie,
  1123. .sta_set_flags = madwifi_sta_set_flags,
  1124. .read_sta_data = madwifi_read_sta_driver_data,
  1125. .hapd_send_eapol = madwifi_send_eapol,
  1126. .sta_disassoc = madwifi_sta_disassoc,
  1127. .sta_deauth = madwifi_sta_deauth,
  1128. .hapd_set_ssid = madwifi_set_ssid,
  1129. .hapd_get_ssid = madwifi_get_ssid,
  1130. .set_countermeasures = madwifi_set_countermeasures,
  1131. .sta_clear_stats = madwifi_sta_clear_stats,
  1132. .commit = madwifi_commit,
  1133. .set_ap_wps_ie = madwifi_set_ap_wps_ie,
  1134. };