driver_hostap.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253
  1. /*
  2. * hostapd / Kernel driver communication with Linux Host AP driver
  3. * Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "includes.h"
  15. #include <sys/ioctl.h>
  16. #ifdef USE_KERNEL_HEADERS
  17. /* compat-wireless does not include linux/compiler.h to define __user, so
  18. * define it here */
  19. #ifndef __user
  20. #define __user
  21. #endif /* __user */
  22. #include <asm/types.h>
  23. #include <linux/if_packet.h>
  24. #include <linux/if_ether.h> /* The L2 protocols */
  25. #include <linux/if_arp.h>
  26. #include <linux/wireless.h>
  27. #else /* USE_KERNEL_HEADERS */
  28. #include <net/if_arp.h>
  29. #include <netpacket/packet.h>
  30. #include "wireless_copy.h"
  31. #endif /* USE_KERNEL_HEADERS */
  32. #include "hostapd.h"
  33. #include "driver.h"
  34. #include "eloop.h"
  35. #include "priv_netlink.h"
  36. #include "hostap_common.h"
  37. #include "hw_features.h"
  38. static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
  39. struct hostap_driver_data {
  40. struct hostapd_data *hapd;
  41. char iface[IFNAMSIZ + 1];
  42. int sock; /* raw packet socket for driver access */
  43. int ioctl_sock; /* socket for ioctl() use */
  44. int wext_sock; /* socket for wireless events */
  45. int we_version;
  46. u8 *generic_ie;
  47. size_t generic_ie_len;
  48. u8 *wps_ie;
  49. size_t wps_ie_len;
  50. };
  51. static int hostapd_ioctl(void *priv, struct prism2_hostapd_param *param,
  52. int len);
  53. static int hostap_set_iface_flags(void *priv, int dev_up);
  54. static int hostap_sta_disassoc(void *priv, const u8 *addr, int reason);
  55. static int hostap_sta_deauth(void *priv, const u8 *addr, int reason);
  56. static void handle_data(struct hostap_driver_data *drv, u8 *buf, size_t len,
  57. u16 stype)
  58. {
  59. struct ieee80211_hdr *hdr;
  60. u16 fc, ethertype;
  61. u8 *pos, *sa;
  62. size_t left;
  63. if (len < sizeof(struct ieee80211_hdr))
  64. return;
  65. hdr = (struct ieee80211_hdr *) buf;
  66. fc = le_to_host16(hdr->frame_control);
  67. if ((fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) != WLAN_FC_TODS) {
  68. printf("Not ToDS data frame (fc=0x%04x)\n", fc);
  69. return;
  70. }
  71. sa = hdr->addr2;
  72. hostapd_rx_from_unknown_sta(drv->hapd, sa);
  73. pos = (u8 *) (hdr + 1);
  74. left = len - sizeof(*hdr);
  75. if (left < sizeof(rfc1042_header)) {
  76. printf("Too short data frame\n");
  77. return;
  78. }
  79. if (memcmp(pos, rfc1042_header, sizeof(rfc1042_header)) != 0) {
  80. printf("Data frame with no RFC1042 header\n");
  81. return;
  82. }
  83. pos += sizeof(rfc1042_header);
  84. left -= sizeof(rfc1042_header);
  85. if (left < 2) {
  86. printf("No ethertype in data frame\n");
  87. return;
  88. }
  89. ethertype = WPA_GET_BE16(pos);
  90. pos += 2;
  91. left -= 2;
  92. switch (ethertype) {
  93. case ETH_P_PAE:
  94. hostapd_eapol_receive(drv->hapd, sa, pos, left);
  95. break;
  96. default:
  97. printf("Unknown ethertype 0x%04x in data frame\n", ethertype);
  98. break;
  99. }
  100. }
  101. static void handle_tx_callback(struct hostap_driver_data *drv, u8 *buf,
  102. size_t len, int ok)
  103. {
  104. struct ieee80211_hdr *hdr;
  105. u16 fc, type, stype;
  106. hdr = (struct ieee80211_hdr *) buf;
  107. fc = le_to_host16(hdr->frame_control);
  108. type = WLAN_FC_GET_TYPE(fc);
  109. stype = WLAN_FC_GET_STYPE(fc);
  110. switch (type) {
  111. case WLAN_FC_TYPE_MGMT:
  112. wpa_printf(MSG_DEBUG, "MGMT (TX callback) %s",
  113. ok ? "ACK" : "fail");
  114. hostapd_mgmt_tx_cb(drv->hapd, buf, len, stype, ok);
  115. break;
  116. case WLAN_FC_TYPE_CTRL:
  117. wpa_printf(MSG_DEBUG, "CTRL (TX callback) %s",
  118. ok ? "ACK" : "fail");
  119. break;
  120. case WLAN_FC_TYPE_DATA:
  121. wpa_printf(MSG_DEBUG, "DATA (TX callback) %s",
  122. ok ? "ACK" : "fail");
  123. hostapd_tx_status(drv->hapd, hdr->addr1, buf, len, ok);
  124. break;
  125. default:
  126. printf("unknown TX callback frame type %d\n", type);
  127. break;
  128. }
  129. }
  130. static void handle_frame(struct hostap_driver_data *drv, u8 *buf, size_t len)
  131. {
  132. struct ieee80211_hdr *hdr;
  133. u16 fc, extra_len, type, stype;
  134. unsigned char *extra = NULL;
  135. size_t data_len = len;
  136. int ver;
  137. /* PSPOLL is only 16 bytes, but driver does not (at least yet) pass
  138. * these to user space */
  139. if (len < 24) {
  140. wpa_printf(MSG_MSGDUMP, "handle_frame: too short (%lu)",
  141. (unsigned long) len);
  142. return;
  143. }
  144. hdr = (struct ieee80211_hdr *) buf;
  145. fc = le_to_host16(hdr->frame_control);
  146. type = WLAN_FC_GET_TYPE(fc);
  147. stype = WLAN_FC_GET_STYPE(fc);
  148. if (type != WLAN_FC_TYPE_MGMT || stype != WLAN_FC_STYPE_BEACON) {
  149. wpa_hexdump(MSG_MSGDUMP, "Received management frame",
  150. buf, len);
  151. }
  152. ver = fc & WLAN_FC_PVER;
  153. /* protocol version 3 is reserved for indicating extra data after the
  154. * payload, version 2 for indicating ACKed frame (TX callbacks), and
  155. * version 1 for indicating failed frame (no ACK, TX callbacks) */
  156. if (ver == 3) {
  157. u8 *pos = buf + len - 2;
  158. extra_len = WPA_GET_LE16(pos);
  159. printf("extra data in frame (elen=%d)\n", extra_len);
  160. if ((size_t) extra_len + 2 > len) {
  161. printf(" extra data overflow\n");
  162. return;
  163. }
  164. len -= extra_len + 2;
  165. extra = buf + len;
  166. } else if (ver == 1 || ver == 2) {
  167. handle_tx_callback(drv, buf, data_len, ver == 2 ? 1 : 0);
  168. return;
  169. } else if (ver != 0) {
  170. printf("unknown protocol version %d\n", ver);
  171. return;
  172. }
  173. switch (type) {
  174. case WLAN_FC_TYPE_MGMT:
  175. if (stype != WLAN_FC_STYPE_BEACON)
  176. wpa_printf(MSG_MSGDUMP, "MGMT");
  177. hostapd_mgmt_rx(drv->hapd, buf, data_len, stype, NULL);
  178. break;
  179. case WLAN_FC_TYPE_CTRL:
  180. wpa_printf(MSG_DEBUG, "CTRL");
  181. break;
  182. case WLAN_FC_TYPE_DATA:
  183. wpa_printf(MSG_DEBUG, "DATA");
  184. handle_data(drv, buf, data_len, stype);
  185. break;
  186. default:
  187. wpa_printf(MSG_DEBUG, "unknown frame type %d", type);
  188. break;
  189. }
  190. }
  191. static void handle_read(int sock, void *eloop_ctx, void *sock_ctx)
  192. {
  193. struct hostap_driver_data *drv = eloop_ctx;
  194. int len;
  195. unsigned char buf[3000];
  196. len = recv(sock, buf, sizeof(buf), 0);
  197. if (len < 0) {
  198. perror("recv");
  199. return;
  200. }
  201. handle_frame(drv, buf, len);
  202. }
  203. static int hostap_init_sockets(struct hostap_driver_data *drv)
  204. {
  205. struct ifreq ifr;
  206. struct sockaddr_ll addr;
  207. drv->sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
  208. if (drv->sock < 0) {
  209. perror("socket[PF_PACKET,SOCK_RAW]");
  210. return -1;
  211. }
  212. if (eloop_register_read_sock(drv->sock, handle_read, drv, NULL)) {
  213. printf("Could not register read socket\n");
  214. return -1;
  215. }
  216. memset(&ifr, 0, sizeof(ifr));
  217. snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%sap", drv->iface);
  218. if (ioctl(drv->sock, SIOCGIFINDEX, &ifr) != 0) {
  219. perror("ioctl(SIOCGIFINDEX)");
  220. return -1;
  221. }
  222. if (hostap_set_iface_flags(drv, 1)) {
  223. return -1;
  224. }
  225. memset(&addr, 0, sizeof(addr));
  226. addr.sll_family = AF_PACKET;
  227. addr.sll_ifindex = ifr.ifr_ifindex;
  228. wpa_printf(MSG_DEBUG, "Opening raw packet socket for ifindex %d",
  229. addr.sll_ifindex);
  230. if (bind(drv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  231. perror("bind");
  232. return -1;
  233. }
  234. memset(&ifr, 0, sizeof(ifr));
  235. os_strlcpy(ifr.ifr_name, drv->iface, sizeof(ifr.ifr_name));
  236. if (ioctl(drv->sock, SIOCGIFHWADDR, &ifr) != 0) {
  237. perror("ioctl(SIOCGIFHWADDR)");
  238. return -1;
  239. }
  240. if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
  241. printf("Invalid HW-addr family 0x%04x\n",
  242. ifr.ifr_hwaddr.sa_family);
  243. return -1;
  244. }
  245. memcpy(drv->hapd->own_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
  246. return 0;
  247. }
  248. static int hostap_send_mgmt_frame(void *priv, const void *msg, size_t len,
  249. int flags)
  250. {
  251. struct hostap_driver_data *drv = priv;
  252. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) msg;
  253. int res;
  254. /* Request TX callback */
  255. hdr->frame_control |= host_to_le16(BIT(1));
  256. res = send(drv->sock, msg, len, flags);
  257. hdr->frame_control &= ~host_to_le16(BIT(1));
  258. return res;
  259. }
  260. static int hostap_send_eapol(void *priv, const u8 *addr, const u8 *data,
  261. size_t data_len, int encrypt, const u8 *own_addr)
  262. {
  263. struct hostap_driver_data *drv = priv;
  264. struct ieee80211_hdr *hdr;
  265. size_t len;
  266. u8 *pos;
  267. int res;
  268. len = sizeof(*hdr) + sizeof(rfc1042_header) + 2 + data_len;
  269. hdr = os_zalloc(len);
  270. if (hdr == NULL) {
  271. printf("malloc() failed for hostapd_send_data(len=%lu)\n",
  272. (unsigned long) len);
  273. return -1;
  274. }
  275. hdr->frame_control =
  276. IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
  277. hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
  278. if (encrypt)
  279. hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
  280. memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
  281. memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
  282. memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
  283. pos = (u8 *) (hdr + 1);
  284. memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
  285. pos += sizeof(rfc1042_header);
  286. *((u16 *) pos) = htons(ETH_P_PAE);
  287. pos += 2;
  288. memcpy(pos, data, data_len);
  289. res = hostap_send_mgmt_frame(drv, (u8 *) hdr, len, 0);
  290. free(hdr);
  291. if (res < 0) {
  292. perror("hostapd_send_eapol: send");
  293. printf("hostapd_send_eapol - packet len: %lu - failed\n",
  294. (unsigned long) len);
  295. }
  296. return res;
  297. }
  298. static int hostap_sta_set_flags(void *priv, const u8 *addr,
  299. int total_flags, int flags_or, int flags_and)
  300. {
  301. struct hostap_driver_data *drv = priv;
  302. struct prism2_hostapd_param param;
  303. memset(&param, 0, sizeof(param));
  304. param.cmd = PRISM2_HOSTAPD_SET_FLAGS_STA;
  305. memcpy(param.sta_addr, addr, ETH_ALEN);
  306. param.u.set_flags_sta.flags_or = flags_or;
  307. param.u.set_flags_sta.flags_and = flags_and;
  308. return hostapd_ioctl(drv, &param, sizeof(param));
  309. }
  310. static int hostap_set_iface_flags(void *priv, int dev_up)
  311. {
  312. struct hostap_driver_data *drv = priv;
  313. struct ifreq ifr;
  314. if (drv->ioctl_sock < 0)
  315. return -1;
  316. memset(&ifr, 0, sizeof(ifr));
  317. snprintf(ifr.ifr_name, IFNAMSIZ, "%sap", drv->iface);
  318. if (ioctl(drv->ioctl_sock, SIOCGIFFLAGS, &ifr) != 0) {
  319. perror("ioctl[SIOCGIFFLAGS]");
  320. return -1;
  321. }
  322. if (dev_up)
  323. ifr.ifr_flags |= IFF_UP;
  324. else
  325. ifr.ifr_flags &= ~IFF_UP;
  326. if (ioctl(drv->ioctl_sock, SIOCSIFFLAGS, &ifr) != 0) {
  327. perror("ioctl[SIOCSIFFLAGS]");
  328. return -1;
  329. }
  330. if (dev_up) {
  331. memset(&ifr, 0, sizeof(ifr));
  332. snprintf(ifr.ifr_name, IFNAMSIZ, "%sap", drv->iface);
  333. ifr.ifr_mtu = HOSTAPD_MTU;
  334. if (ioctl(drv->ioctl_sock, SIOCSIFMTU, &ifr) != 0) {
  335. perror("ioctl[SIOCSIFMTU]");
  336. printf("Setting MTU failed - trying to survive with "
  337. "current value\n");
  338. }
  339. }
  340. return 0;
  341. }
  342. static int hostapd_ioctl(void *priv, struct prism2_hostapd_param *param,
  343. int len)
  344. {
  345. struct hostap_driver_data *drv = priv;
  346. struct iwreq iwr;
  347. memset(&iwr, 0, sizeof(iwr));
  348. os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
  349. iwr.u.data.pointer = (caddr_t) param;
  350. iwr.u.data.length = len;
  351. if (ioctl(drv->ioctl_sock, PRISM2_IOCTL_HOSTAPD, &iwr) < 0) {
  352. perror("ioctl[PRISM2_IOCTL_HOSTAPD]");
  353. return -1;
  354. }
  355. return 0;
  356. }
  357. static int hostap_set_encryption(const char *ifname, void *priv,
  358. const char *alg, const u8 *addr,
  359. int idx, const u8 *key, size_t key_len,
  360. int txkey)
  361. {
  362. struct hostap_driver_data *drv = priv;
  363. struct prism2_hostapd_param *param;
  364. u8 *buf;
  365. size_t blen;
  366. int ret = 0;
  367. blen = sizeof(*param) + key_len;
  368. buf = os_zalloc(blen);
  369. if (buf == NULL)
  370. return -1;
  371. param = (struct prism2_hostapd_param *) buf;
  372. param->cmd = PRISM2_SET_ENCRYPTION;
  373. if (addr == NULL)
  374. memset(param->sta_addr, 0xff, ETH_ALEN);
  375. else
  376. memcpy(param->sta_addr, addr, ETH_ALEN);
  377. os_strlcpy((char *) param->u.crypt.alg, alg,
  378. HOSTAP_CRYPT_ALG_NAME_LEN);
  379. param->u.crypt.flags = txkey ? HOSTAP_CRYPT_FLAG_SET_TX_KEY : 0;
  380. param->u.crypt.idx = idx;
  381. param->u.crypt.key_len = key_len;
  382. memcpy((u8 *) (param + 1), key, key_len);
  383. if (hostapd_ioctl(drv, param, blen)) {
  384. printf("Failed to set encryption.\n");
  385. ret = -1;
  386. }
  387. free(buf);
  388. return ret;
  389. }
  390. static int hostap_get_seqnum(const char *ifname, void *priv, const u8 *addr,
  391. int idx, u8 *seq)
  392. {
  393. struct hostap_driver_data *drv = priv;
  394. struct prism2_hostapd_param *param;
  395. u8 *buf;
  396. size_t blen;
  397. int ret = 0;
  398. blen = sizeof(*param) + 32;
  399. buf = os_zalloc(blen);
  400. if (buf == NULL)
  401. return -1;
  402. param = (struct prism2_hostapd_param *) buf;
  403. param->cmd = PRISM2_GET_ENCRYPTION;
  404. if (addr == NULL)
  405. memset(param->sta_addr, 0xff, ETH_ALEN);
  406. else
  407. memcpy(param->sta_addr, addr, ETH_ALEN);
  408. param->u.crypt.idx = idx;
  409. if (hostapd_ioctl(drv, param, blen)) {
  410. printf("Failed to get encryption.\n");
  411. ret = -1;
  412. } else {
  413. memcpy(seq, param->u.crypt.seq, 8);
  414. }
  415. free(buf);
  416. return ret;
  417. }
  418. static int hostap_ioctl_prism2param(void *priv, int param, int value)
  419. {
  420. struct hostap_driver_data *drv = priv;
  421. struct iwreq iwr;
  422. int *i;
  423. memset(&iwr, 0, sizeof(iwr));
  424. os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
  425. i = (int *) iwr.u.name;
  426. *i++ = param;
  427. *i++ = value;
  428. if (ioctl(drv->ioctl_sock, PRISM2_IOCTL_PRISM2_PARAM, &iwr) < 0) {
  429. perror("ioctl[PRISM2_IOCTL_PRISM2_PARAM]");
  430. return -1;
  431. }
  432. return 0;
  433. }
  434. static int hostap_set_ieee8021x(const char *ifname, void *priv, int enabled)
  435. {
  436. struct hostap_driver_data *drv = priv;
  437. /* enable kernel driver support for IEEE 802.1X */
  438. if (hostap_ioctl_prism2param(drv, PRISM2_PARAM_IEEE_802_1X, enabled)) {
  439. printf("Could not setup IEEE 802.1X support in kernel driver."
  440. "\n");
  441. return -1;
  442. }
  443. if (!enabled)
  444. return 0;
  445. /* use host driver implementation of encryption to allow
  446. * individual keys and passing plaintext EAPOL frames */
  447. if (hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOST_DECRYPT, 1) ||
  448. hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOST_ENCRYPT, 1)) {
  449. printf("Could not setup host-based encryption in kernel "
  450. "driver.\n");
  451. return -1;
  452. }
  453. return 0;
  454. }
  455. static int hostap_set_privacy(const char *ifname, void *priv, int enabled)
  456. {
  457. struct hostap_drvier_data *drv = priv;
  458. return hostap_ioctl_prism2param(drv, PRISM2_PARAM_PRIVACY_INVOKED,
  459. enabled);
  460. }
  461. static int hostap_set_ssid(const char *ifname, void *priv, const u8 *buf,
  462. int len)
  463. {
  464. struct hostap_driver_data *drv = priv;
  465. struct iwreq iwr;
  466. memset(&iwr, 0, sizeof(iwr));
  467. os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
  468. iwr.u.essid.flags = 1; /* SSID active */
  469. iwr.u.essid.pointer = (caddr_t) buf;
  470. iwr.u.essid.length = len + 1;
  471. if (ioctl(drv->ioctl_sock, SIOCSIWESSID, &iwr) < 0) {
  472. perror("ioctl[SIOCSIWESSID]");
  473. printf("len=%d\n", len);
  474. return -1;
  475. }
  476. return 0;
  477. }
  478. static int hostap_flush(void *priv)
  479. {
  480. struct hostap_driver_data *drv = priv;
  481. struct prism2_hostapd_param param;
  482. memset(&param, 0, sizeof(param));
  483. param.cmd = PRISM2_HOSTAPD_FLUSH;
  484. return hostapd_ioctl(drv, &param, sizeof(param));
  485. }
  486. static int hostap_read_sta_data(void *priv,
  487. struct hostap_sta_driver_data *data,
  488. const u8 *addr)
  489. {
  490. struct hostap_driver_data *drv = priv;
  491. char buf[1024], line[128], *pos;
  492. FILE *f;
  493. unsigned long val;
  494. memset(data, 0, sizeof(*data));
  495. snprintf(buf, sizeof(buf), "/proc/net/hostap/%s/" MACSTR,
  496. drv->iface, MAC2STR(addr));
  497. f = fopen(buf, "r");
  498. if (!f)
  499. return -1;
  500. /* Need to read proc file with in one piece, so use large enough
  501. * buffer. */
  502. setbuffer(f, buf, sizeof(buf));
  503. while (fgets(line, sizeof(line), f)) {
  504. pos = strchr(line, '=');
  505. if (!pos)
  506. continue;
  507. *pos++ = '\0';
  508. val = strtoul(pos, NULL, 10);
  509. if (strcmp(line, "rx_packets") == 0)
  510. data->rx_packets = val;
  511. else if (strcmp(line, "tx_packets") == 0)
  512. data->tx_packets = val;
  513. else if (strcmp(line, "rx_bytes") == 0)
  514. data->rx_bytes = val;
  515. else if (strcmp(line, "tx_bytes") == 0)
  516. data->tx_bytes = val;
  517. }
  518. fclose(f);
  519. return 0;
  520. }
  521. static int hostap_sta_add(const char *ifname, void *priv, const u8 *addr,
  522. u16 aid, u16 capability, u8 *supp_rates,
  523. size_t supp_rates_len, int flags,
  524. u16 listen_interval)
  525. {
  526. struct hostap_driver_data *drv = priv;
  527. struct prism2_hostapd_param param;
  528. int tx_supp_rates = 0;
  529. size_t i;
  530. #define WLAN_RATE_1M BIT(0)
  531. #define WLAN_RATE_2M BIT(1)
  532. #define WLAN_RATE_5M5 BIT(2)
  533. #define WLAN_RATE_11M BIT(3)
  534. for (i = 0; i < supp_rates_len; i++) {
  535. if ((supp_rates[i] & 0x7f) == 2)
  536. tx_supp_rates |= WLAN_RATE_1M;
  537. if ((supp_rates[i] & 0x7f) == 4)
  538. tx_supp_rates |= WLAN_RATE_2M;
  539. if ((supp_rates[i] & 0x7f) == 11)
  540. tx_supp_rates |= WLAN_RATE_5M5;
  541. if ((supp_rates[i] & 0x7f) == 22)
  542. tx_supp_rates |= WLAN_RATE_11M;
  543. }
  544. memset(&param, 0, sizeof(param));
  545. param.cmd = PRISM2_HOSTAPD_ADD_STA;
  546. memcpy(param.sta_addr, addr, ETH_ALEN);
  547. param.u.add_sta.aid = aid;
  548. param.u.add_sta.capability = capability;
  549. param.u.add_sta.tx_supp_rates = tx_supp_rates;
  550. return hostapd_ioctl(drv, &param, sizeof(param));
  551. }
  552. static int hostap_sta_remove(void *priv, const u8 *addr)
  553. {
  554. struct hostap_driver_data *drv = priv;
  555. struct prism2_hostapd_param param;
  556. hostap_sta_set_flags(drv, addr, 0, 0, ~WLAN_STA_AUTHORIZED);
  557. memset(&param, 0, sizeof(param));
  558. param.cmd = PRISM2_HOSTAPD_REMOVE_STA;
  559. memcpy(param.sta_addr, addr, ETH_ALEN);
  560. if (hostapd_ioctl(drv, &param, sizeof(param))) {
  561. printf("Could not remove station from kernel driver.\n");
  562. return -1;
  563. }
  564. return 0;
  565. }
  566. static int hostap_get_inact_sec(void *priv, const u8 *addr)
  567. {
  568. struct hostap_driver_data *drv = priv;
  569. struct prism2_hostapd_param param;
  570. memset(&param, 0, sizeof(param));
  571. param.cmd = PRISM2_HOSTAPD_GET_INFO_STA;
  572. memcpy(param.sta_addr, addr, ETH_ALEN);
  573. if (hostapd_ioctl(drv, &param, sizeof(param))) {
  574. return -1;
  575. }
  576. return param.u.get_info_sta.inactive_sec;
  577. }
  578. static int hostap_sta_clear_stats(void *priv, const u8 *addr)
  579. {
  580. struct hostap_driver_data *drv = priv;
  581. struct prism2_hostapd_param param;
  582. memset(&param, 0, sizeof(param));
  583. param.cmd = PRISM2_HOSTAPD_STA_CLEAR_STATS;
  584. memcpy(param.sta_addr, addr, ETH_ALEN);
  585. if (hostapd_ioctl(drv, &param, sizeof(param))) {
  586. return -1;
  587. }
  588. return 0;
  589. }
  590. static int hostap_set_assoc_ap(void *priv, const u8 *addr)
  591. {
  592. struct hostap_driver_data *drv = priv;
  593. struct prism2_hostapd_param param;
  594. memset(&param, 0, sizeof(param));
  595. param.cmd = PRISM2_HOSTAPD_SET_ASSOC_AP_ADDR;
  596. memcpy(param.sta_addr, addr, ETH_ALEN);
  597. if (hostapd_ioctl(drv, &param, sizeof(param)))
  598. return -1;
  599. return 0;
  600. }
  601. static int hostapd_ioctl_set_generic_elem(struct hostap_driver_data *drv)
  602. {
  603. struct prism2_hostapd_param *param;
  604. int res;
  605. size_t blen, elem_len;
  606. elem_len = drv->generic_ie_len + drv->wps_ie_len;
  607. blen = PRISM2_HOSTAPD_GENERIC_ELEMENT_HDR_LEN + elem_len;
  608. if (blen < sizeof(*param))
  609. blen = sizeof(*param);
  610. param = os_zalloc(blen);
  611. if (param == NULL)
  612. return -1;
  613. param->cmd = PRISM2_HOSTAPD_SET_GENERIC_ELEMENT;
  614. param->u.generic_elem.len = elem_len;
  615. if (drv->generic_ie) {
  616. os_memcpy(param->u.generic_elem.data, drv->generic_ie,
  617. drv->generic_ie_len);
  618. }
  619. if (drv->wps_ie) {
  620. os_memcpy(&param->u.generic_elem.data[drv->generic_ie_len],
  621. drv->wps_ie, drv->wps_ie_len);
  622. }
  623. wpa_hexdump(MSG_DEBUG, "hostap: Set generic IE",
  624. param->u.generic_elem.data, elem_len);
  625. res = hostapd_ioctl(drv, param, blen);
  626. os_free(param);
  627. return res;
  628. }
  629. static int hostap_set_generic_elem(const char *ifname, void *priv,
  630. const u8 *elem, size_t elem_len)
  631. {
  632. struct hostap_driver_data *drv = priv;
  633. os_free(drv->generic_ie);
  634. drv->generic_ie = NULL;
  635. drv->generic_ie_len = 0;
  636. if (elem) {
  637. drv->generic_ie = os_malloc(elem_len);
  638. if (drv->generic_ie == NULL)
  639. return -1;
  640. os_memcpy(drv->generic_ie, elem, elem_len);
  641. drv->generic_ie_len = elem_len;
  642. }
  643. return hostapd_ioctl_set_generic_elem(drv);
  644. }
  645. static int hostap_set_wps_beacon_ie(const char *ifname, void *priv,
  646. const u8 *ie, size_t len)
  647. {
  648. /* Host AP driver supports only one set of extra IEs, so we need to
  649. * use the ProbeResp IEs also for Beacon frames since they include more
  650. * information. */
  651. return 0;
  652. }
  653. static int hostap_set_wps_probe_resp_ie(const char *ifname, void *priv,
  654. const u8 *ie, size_t len)
  655. {
  656. struct hostap_driver_data *drv = priv;
  657. os_free(drv->wps_ie);
  658. drv->wps_ie = NULL;
  659. drv->wps_ie_len = 0;
  660. if (ie) {
  661. drv->wps_ie = os_malloc(len);
  662. if (drv->wps_ie == NULL)
  663. return -1;
  664. os_memcpy(drv->wps_ie, ie, len);
  665. drv->wps_ie_len = len;
  666. }
  667. return hostapd_ioctl_set_generic_elem(drv);
  668. }
  669. static void
  670. hostapd_wireless_event_wireless_custom(struct hostap_driver_data *drv,
  671. char *custom)
  672. {
  673. wpa_printf(MSG_DEBUG, "Custom wireless event: '%s'", custom);
  674. if (strncmp(custom, "MLME-MICHAELMICFAILURE.indication", 33) == 0) {
  675. char *pos;
  676. u8 addr[ETH_ALEN];
  677. pos = strstr(custom, "addr=");
  678. if (pos == NULL) {
  679. wpa_printf(MSG_DEBUG,
  680. "MLME-MICHAELMICFAILURE.indication "
  681. "without sender address ignored");
  682. return;
  683. }
  684. pos += 5;
  685. if (hwaddr_aton(pos, addr) == 0) {
  686. hostapd_michael_mic_failure(drv->hapd, addr);
  687. } else {
  688. wpa_printf(MSG_DEBUG,
  689. "MLME-MICHAELMICFAILURE.indication "
  690. "with invalid MAC address");
  691. }
  692. }
  693. }
  694. static void hostapd_wireless_event_wireless(struct hostap_driver_data *drv,
  695. char *data, int len)
  696. {
  697. struct iw_event iwe_buf, *iwe = &iwe_buf;
  698. char *pos, *end, *custom, *buf;
  699. pos = data;
  700. end = data + len;
  701. while (pos + IW_EV_LCP_LEN <= end) {
  702. /* Event data may be unaligned, so make a local, aligned copy
  703. * before processing. */
  704. memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
  705. wpa_printf(MSG_DEBUG, "Wireless event: cmd=0x%x len=%d",
  706. iwe->cmd, iwe->len);
  707. if (iwe->len <= IW_EV_LCP_LEN)
  708. return;
  709. custom = pos + IW_EV_POINT_LEN;
  710. if (drv->we_version > 18 &&
  711. (iwe->cmd == IWEVMICHAELMICFAILURE ||
  712. iwe->cmd == IWEVCUSTOM)) {
  713. /* WE-19 removed the pointer from struct iw_point */
  714. char *dpos = (char *) &iwe_buf.u.data.length;
  715. int dlen = dpos - (char *) &iwe_buf;
  716. memcpy(dpos, pos + IW_EV_LCP_LEN,
  717. sizeof(struct iw_event) - dlen);
  718. } else {
  719. memcpy(&iwe_buf, pos, sizeof(struct iw_event));
  720. custom += IW_EV_POINT_OFF;
  721. }
  722. switch (iwe->cmd) {
  723. case IWEVCUSTOM:
  724. if (custom + iwe->u.data.length > end)
  725. return;
  726. buf = malloc(iwe->u.data.length + 1);
  727. if (buf == NULL)
  728. return;
  729. memcpy(buf, custom, iwe->u.data.length);
  730. buf[iwe->u.data.length] = '\0';
  731. hostapd_wireless_event_wireless_custom(drv, buf);
  732. free(buf);
  733. break;
  734. }
  735. pos += iwe->len;
  736. }
  737. }
  738. static void hostapd_wireless_event_rtm_newlink(struct hostap_driver_data *drv,
  739. struct nlmsghdr *h, int len)
  740. {
  741. struct ifinfomsg *ifi;
  742. int attrlen, nlmsg_len, rta_len;
  743. struct rtattr * attr;
  744. if (len < (int) sizeof(*ifi))
  745. return;
  746. ifi = NLMSG_DATA(h);
  747. /* TODO: use ifi->ifi_index to filter out wireless events from other
  748. * interfaces */
  749. nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg));
  750. attrlen = h->nlmsg_len - nlmsg_len;
  751. if (attrlen < 0)
  752. return;
  753. attr = (struct rtattr *) (((char *) ifi) + nlmsg_len);
  754. rta_len = RTA_ALIGN(sizeof(struct rtattr));
  755. while (RTA_OK(attr, attrlen)) {
  756. if (attr->rta_type == IFLA_WIRELESS) {
  757. hostapd_wireless_event_wireless(
  758. drv, ((char *) attr) + rta_len,
  759. attr->rta_len - rta_len);
  760. }
  761. attr = RTA_NEXT(attr, attrlen);
  762. }
  763. }
  764. static void hostapd_wireless_event_receive(int sock, void *eloop_ctx,
  765. void *sock_ctx)
  766. {
  767. char buf[256];
  768. int left;
  769. struct sockaddr_nl from;
  770. socklen_t fromlen;
  771. struct nlmsghdr *h;
  772. struct hostap_driver_data *drv = eloop_ctx;
  773. fromlen = sizeof(from);
  774. left = recvfrom(sock, buf, sizeof(buf), MSG_DONTWAIT,
  775. (struct sockaddr *) &from, &fromlen);
  776. if (left < 0) {
  777. if (errno != EINTR && errno != EAGAIN)
  778. perror("recvfrom(netlink)");
  779. return;
  780. }
  781. h = (struct nlmsghdr *) buf;
  782. while (left >= (int) sizeof(*h)) {
  783. int len, plen;
  784. len = h->nlmsg_len;
  785. plen = len - sizeof(*h);
  786. if (len > left || plen < 0) {
  787. printf("Malformed netlink message: "
  788. "len=%d left=%d plen=%d\n",
  789. len, left, plen);
  790. break;
  791. }
  792. switch (h->nlmsg_type) {
  793. case RTM_NEWLINK:
  794. hostapd_wireless_event_rtm_newlink(drv, h, plen);
  795. break;
  796. }
  797. len = NLMSG_ALIGN(len);
  798. left -= len;
  799. h = (struct nlmsghdr *) ((char *) h + len);
  800. }
  801. if (left > 0) {
  802. printf("%d extra bytes in the end of netlink message\n", left);
  803. }
  804. }
  805. static int hostap_get_we_version(struct hostap_driver_data *drv)
  806. {
  807. struct iw_range *range;
  808. struct iwreq iwr;
  809. int minlen;
  810. size_t buflen;
  811. drv->we_version = 0;
  812. /*
  813. * Use larger buffer than struct iw_range in order to allow the
  814. * structure to grow in the future.
  815. */
  816. buflen = sizeof(struct iw_range) + 500;
  817. range = os_zalloc(buflen);
  818. if (range == NULL)
  819. return -1;
  820. memset(&iwr, 0, sizeof(iwr));
  821. os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
  822. iwr.u.data.pointer = (caddr_t) range;
  823. iwr.u.data.length = buflen;
  824. minlen = ((char *) &range->enc_capa) - (char *) range +
  825. sizeof(range->enc_capa);
  826. if (ioctl(drv->ioctl_sock, SIOCGIWRANGE, &iwr) < 0) {
  827. perror("ioctl[SIOCGIWRANGE]");
  828. free(range);
  829. return -1;
  830. } else if (iwr.u.data.length >= minlen &&
  831. range->we_version_compiled >= 18) {
  832. wpa_printf(MSG_DEBUG, "SIOCGIWRANGE: WE(compiled)=%d "
  833. "WE(source)=%d enc_capa=0x%x",
  834. range->we_version_compiled,
  835. range->we_version_source,
  836. range->enc_capa);
  837. drv->we_version = range->we_version_compiled;
  838. }
  839. free(range);
  840. return 0;
  841. }
  842. static int hostap_wireless_event_init(void *priv)
  843. {
  844. struct hostap_driver_data *drv = priv;
  845. int s;
  846. struct sockaddr_nl local;
  847. hostap_get_we_version(drv);
  848. drv->wext_sock = -1;
  849. s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  850. if (s < 0) {
  851. perror("socket(PF_NETLINK,SOCK_RAW,NETLINK_ROUTE)");
  852. return -1;
  853. }
  854. memset(&local, 0, sizeof(local));
  855. local.nl_family = AF_NETLINK;
  856. local.nl_groups = RTMGRP_LINK;
  857. if (bind(s, (struct sockaddr *) &local, sizeof(local)) < 0) {
  858. perror("bind(netlink)");
  859. close(s);
  860. return -1;
  861. }
  862. eloop_register_read_sock(s, hostapd_wireless_event_receive, drv,
  863. NULL);
  864. drv->wext_sock = s;
  865. return 0;
  866. }
  867. static void hostap_wireless_event_deinit(void *priv)
  868. {
  869. struct hostap_driver_data *drv = priv;
  870. if (drv->wext_sock < 0)
  871. return;
  872. eloop_unregister_read_sock(drv->wext_sock);
  873. close(drv->wext_sock);
  874. }
  875. static void * hostap_init(struct hostapd_data *hapd)
  876. {
  877. struct hostap_driver_data *drv;
  878. drv = os_zalloc(sizeof(struct hostap_driver_data));
  879. if (drv == NULL) {
  880. printf("Could not allocate memory for hostapd driver data\n");
  881. return NULL;
  882. }
  883. drv->hapd = hapd;
  884. drv->ioctl_sock = drv->sock = -1;
  885. memcpy(drv->iface, hapd->conf->iface, sizeof(drv->iface));
  886. drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
  887. if (drv->ioctl_sock < 0) {
  888. perror("socket[PF_INET,SOCK_DGRAM]");
  889. free(drv);
  890. return NULL;
  891. }
  892. if (hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOSTAPD, 1)) {
  893. printf("Could not enable hostapd mode for interface %s\n",
  894. drv->iface);
  895. close(drv->ioctl_sock);
  896. free(drv);
  897. return NULL;
  898. }
  899. if (hostap_init_sockets(drv)) {
  900. close(drv->ioctl_sock);
  901. free(drv);
  902. return NULL;
  903. }
  904. return drv;
  905. }
  906. static void hostap_driver_deinit(void *priv)
  907. {
  908. struct hostap_driver_data *drv = priv;
  909. (void) hostap_set_iface_flags(drv, 0);
  910. (void) hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOSTAPD, 0);
  911. (void) hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOSTAPD_STA, 0);
  912. if (drv->ioctl_sock >= 0)
  913. close(drv->ioctl_sock);
  914. if (drv->sock >= 0)
  915. close(drv->sock);
  916. os_free(drv->generic_ie);
  917. os_free(drv->wps_ie);
  918. free(drv);
  919. }
  920. static int hostap_sta_deauth(void *priv, const u8 *addr, int reason)
  921. {
  922. struct hostap_driver_data *drv = priv;
  923. struct ieee80211_mgmt mgmt;
  924. memset(&mgmt, 0, sizeof(mgmt));
  925. mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  926. WLAN_FC_STYPE_DEAUTH);
  927. memcpy(mgmt.da, addr, ETH_ALEN);
  928. memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
  929. memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
  930. mgmt.u.deauth.reason_code = host_to_le16(reason);
  931. return hostap_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
  932. sizeof(mgmt.u.deauth), 0);
  933. }
  934. static int hostap_sta_disassoc(void *priv, const u8 *addr, int reason)
  935. {
  936. struct hostap_driver_data *drv = priv;
  937. struct ieee80211_mgmt mgmt;
  938. memset(&mgmt, 0, sizeof(mgmt));
  939. mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  940. WLAN_FC_STYPE_DISASSOC);
  941. memcpy(mgmt.da, addr, ETH_ALEN);
  942. memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
  943. memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
  944. mgmt.u.disassoc.reason_code = host_to_le16(reason);
  945. return hostap_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
  946. sizeof(mgmt.u.disassoc), 0);
  947. }
  948. static struct hostapd_hw_modes * hostap_get_hw_feature_data(void *priv,
  949. u16 *num_modes,
  950. u16 *flags)
  951. {
  952. struct hostapd_hw_modes *mode;
  953. int i, clen, rlen;
  954. const short chan2freq[14] = {
  955. 2412, 2417, 2422, 2427, 2432, 2437, 2442,
  956. 2447, 2452, 2457, 2462, 2467, 2472, 2484
  957. };
  958. mode = os_zalloc(sizeof(struct hostapd_hw_modes));
  959. if (mode == NULL)
  960. return NULL;
  961. *num_modes = 1;
  962. *flags = 0;
  963. mode->mode = HOSTAPD_MODE_IEEE80211B;
  964. mode->num_channels = 14;
  965. mode->num_rates = 4;
  966. clen = mode->num_channels * sizeof(struct hostapd_channel_data);
  967. rlen = mode->num_rates * sizeof(struct hostapd_rate_data);
  968. mode->channels = os_zalloc(clen);
  969. mode->rates = os_zalloc(rlen);
  970. if (mode->channels == NULL || mode->rates == NULL) {
  971. hostapd_free_hw_features(mode, *num_modes);
  972. return NULL;
  973. }
  974. for (i = 0; i < 14; i++) {
  975. mode->channels[i].chan = i + 1;
  976. mode->channels[i].freq = chan2freq[i];
  977. /* TODO: Get allowed channel list from the driver */
  978. if (i >= 11)
  979. mode->channels[i].flag = HOSTAPD_CHAN_DISABLED;
  980. }
  981. mode->rates[0].rate = 10;
  982. mode->rates[0].flags = HOSTAPD_RATE_CCK;
  983. mode->rates[1].rate = 20;
  984. mode->rates[1].flags = HOSTAPD_RATE_CCK;
  985. mode->rates[2].rate = 55;
  986. mode->rates[2].flags = HOSTAPD_RATE_CCK;
  987. mode->rates[3].rate = 110;
  988. mode->rates[3].flags = HOSTAPD_RATE_CCK;
  989. return mode;
  990. }
  991. const struct wpa_driver_ops wpa_driver_hostap_ops = {
  992. .name = "hostap",
  993. .init = hostap_init,
  994. .deinit = hostap_driver_deinit,
  995. .wireless_event_init = hostap_wireless_event_init,
  996. .wireless_event_deinit = hostap_wireless_event_deinit,
  997. .set_ieee8021x = hostap_set_ieee8021x,
  998. .set_privacy = hostap_set_privacy,
  999. .set_encryption = hostap_set_encryption,
  1000. .get_seqnum = hostap_get_seqnum,
  1001. .flush = hostap_flush,
  1002. .set_generic_elem = hostap_set_generic_elem,
  1003. .read_sta_data = hostap_read_sta_data,
  1004. .send_eapol = hostap_send_eapol,
  1005. .sta_set_flags = hostap_sta_set_flags,
  1006. .sta_deauth = hostap_sta_deauth,
  1007. .sta_disassoc = hostap_sta_disassoc,
  1008. .sta_remove = hostap_sta_remove,
  1009. .set_ssid = hostap_set_ssid,
  1010. .send_mgmt_frame = hostap_send_mgmt_frame,
  1011. .set_assoc_ap = hostap_set_assoc_ap,
  1012. .sta_add = hostap_sta_add,
  1013. .get_inact_sec = hostap_get_inact_sec,
  1014. .sta_clear_stats = hostap_sta_clear_stats,
  1015. .get_hw_feature_data = hostap_get_hw_feature_data,
  1016. .set_wps_beacon_ie = hostap_set_wps_beacon_ie,
  1017. .set_wps_probe_resp_ie = hostap_set_wps_probe_resp_ie,
  1018. };