driver_prism54.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  1. /*
  2. * hostapd / Driver interaction with Prism54 PIMFOR interface
  3. * Copyright (c) 2004, Bell Kin <bell_kin@pek.com.tw>
  4. * based on hostap driver.c, ieee802_11.c
  5. * Copyright (c) 2002-2007, Jouni Malinen <j@w1.fi>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * Alternatively, this software may be distributed under the terms of BSD
  12. * license.
  13. *
  14. * See README and COPYING for more details.
  15. */
  16. #include "includes.h"
  17. #include <sys/ioctl.h>
  18. #include <sys/select.h>
  19. #ifdef USE_KERNEL_HEADERS
  20. /* compat-wireless does not include linux/compiler.h to define __user, so
  21. * define it here */
  22. #ifndef __user
  23. #define __user
  24. #endif /* __user */
  25. #include <asm/types.h>
  26. #include <linux/if_packet.h>
  27. #include <linux/if_ether.h> /* The L2 protocols */
  28. #include <linux/if_arp.h>
  29. #include <linux/wireless.h>
  30. #else /* USE_KERNEL_HEADERS */
  31. #include <net/if_arp.h>
  32. #include <netpacket/packet.h>
  33. #include "wireless_copy.h"
  34. #endif /* USE_KERNEL_HEADERS */
  35. #include "hostapd.h"
  36. #include "config.h"
  37. #include "driver.h"
  38. #include "ieee802_1x.h"
  39. #include "eloop.h"
  40. #include "ieee802_11.h"
  41. #include "prism54.h"
  42. #include "wpa.h"
  43. #include "radius/radius.h"
  44. #include "sta_info.h"
  45. #include "accounting.h"
  46. const int PIM_BUF_SIZE = 4096;
  47. struct prism54_driver_data {
  48. struct hostapd_data *hapd;
  49. char iface[IFNAMSIZ + 1];
  50. int sock; /* raw packet socket for 802.3 access */
  51. int pim_sock; /* socket for pimfor packet */
  52. char macs[2007][6];
  53. };
  54. static int mac_id_refresh(struct prism54_driver_data *data, int id, char *mac)
  55. {
  56. if (id < 0 || id > 2006) {
  57. return -1;
  58. }
  59. memcpy(&data->macs[id][0], mac, ETH_ALEN);
  60. return 0;
  61. }
  62. static char * mac_id_get(struct prism54_driver_data *data, int id)
  63. {
  64. if (id < 0 || id > 2006) {
  65. return NULL;
  66. }
  67. return &data->macs[id][0];
  68. }
  69. /* wait for a specific pimfor, timeout in 10ms resolution */
  70. /* pim_sock must be non-block to prevent dead lock from no response */
  71. /* or same response type in series */
  72. static int prism54_waitpim(void *priv, unsigned long oid, void *buf, int len,
  73. int timeout)
  74. {
  75. struct prism54_driver_data *drv = priv;
  76. struct timeval tv, stv, ctv;
  77. fd_set pfd;
  78. int rlen;
  79. pimdev_hdr *pkt;
  80. pkt = malloc(8192);
  81. if (pkt == NULL)
  82. return -1;
  83. FD_ZERO(&pfd);
  84. gettimeofday(&stv, NULL);
  85. do {
  86. FD_SET(drv->pim_sock, &pfd);
  87. tv.tv_sec = 0;
  88. tv.tv_usec = 10000;
  89. if (select(drv->pim_sock + 1, &pfd, NULL, NULL, &tv)) {
  90. rlen = recv(drv->pim_sock, pkt, 8192, 0);
  91. if (rlen > 0) {
  92. if (pkt->oid == htonl(oid)) {
  93. if (rlen <= len) {
  94. if (buf != NULL) {
  95. memcpy(buf, pkt, rlen);
  96. }
  97. free(pkt);
  98. return rlen;
  99. } else {
  100. printf("buffer too small\n");
  101. free(pkt);
  102. return -1;
  103. }
  104. } else {
  105. gettimeofday(&ctv, NULL);
  106. continue;
  107. }
  108. }
  109. }
  110. gettimeofday(&ctv, NULL);
  111. } while (((ctv.tv_sec - stv.tv_sec) * 100 +
  112. (ctv.tv_usec - stv.tv_usec) / 10000) > timeout);
  113. free(pkt);
  114. return 0;
  115. }
  116. /* send an eapol packet */
  117. static int prism54_send_eapol(void *priv, const u8 *addr,
  118. const u8 *data, size_t data_len, int encrypt,
  119. const u8 *own_addr)
  120. {
  121. struct prism54_driver_data *drv = priv;
  122. ieee802_3_hdr *hdr;
  123. size_t len;
  124. u8 *pos;
  125. int res;
  126. len = sizeof(*hdr) + data_len;
  127. hdr = os_zalloc(len);
  128. if (hdr == NULL) {
  129. printf("malloc() failed for prism54_send_data(len=%lu)\n",
  130. (unsigned long) len);
  131. return -1;
  132. }
  133. memcpy(&hdr->da[0], addr, ETH_ALEN);
  134. memcpy(&hdr->sa[0], own_addr, ETH_ALEN);
  135. hdr->type = htons(ETH_P_PAE);
  136. pos = (u8 *) (hdr + 1);
  137. memcpy(pos, data, data_len);
  138. res = send(drv->sock, hdr, len, 0);
  139. free(hdr);
  140. if (res < 0) {
  141. perror("hostapd_send_eapol: send");
  142. printf("hostapd_send_eapol - packet len: %lu - failed\n",
  143. (unsigned long) len);
  144. }
  145. return res;
  146. }
  147. /* open data channel(auth-1) or eapol only(unauth-0) */
  148. static int prism54_set_sta_authorized(void *priv, const u8 *addr,
  149. int authorized)
  150. {
  151. struct prism54_driver_data *drv = priv;
  152. pimdev_hdr *hdr;
  153. char *pos;
  154. hdr = malloc(sizeof(*hdr) + ETH_ALEN);
  155. if (hdr == NULL)
  156. return -1;
  157. hdr->op = htonl(PIMOP_SET);
  158. if (authorized) {
  159. hdr->oid = htonl(DOT11_OID_EAPAUTHSTA);
  160. } else {
  161. hdr->oid = htonl(DOT11_OID_EAPUNAUTHSTA);
  162. }
  163. pos = (char *) (hdr + 1);
  164. memcpy(pos, addr, ETH_ALEN);
  165. send(drv->pim_sock, hdr, sizeof(*hdr) + ETH_ALEN, 0);
  166. prism54_waitpim(priv, hdr->oid, hdr, sizeof(*hdr) + ETH_ALEN, 10);
  167. free(hdr);
  168. return 0;
  169. }
  170. static int
  171. prism54_sta_set_flags(void *priv, const u8 *addr, int total_flags,
  172. int flags_or, int flags_and)
  173. {
  174. /* For now, only support setting Authorized flag */
  175. if (flags_or & WLAN_STA_AUTHORIZED)
  176. return prism54_set_sta_authorized(priv, addr, 1);
  177. if (flags_and & WLAN_STA_AUTHORIZED)
  178. return prism54_set_sta_authorized(priv, addr, 0);
  179. return 0;
  180. }
  181. /* set per station key */
  182. static int prism54_set_encryption(const char *ifname, void *priv,
  183. const char *alg, const u8 *addr,
  184. int idx, const u8 *key, size_t key_len,
  185. int txkey)
  186. {
  187. struct prism54_driver_data *drv = priv;
  188. pimdev_hdr *hdr;
  189. struct obj_stakey *keys;
  190. u8 *buf;
  191. size_t blen;
  192. int ret = 0;
  193. blen = sizeof(struct obj_stakey) + sizeof(pimdev_hdr);
  194. hdr = malloc(blen);
  195. if (hdr == NULL) {
  196. printf("memory low\n");
  197. return -1;
  198. }
  199. keys = (struct obj_stakey *) &hdr[1];
  200. if (!addr) {
  201. memset(&keys->address[0], 0xff, ETH_ALEN);
  202. } else {
  203. memcpy(&keys->address[0], addr, ETH_ALEN);
  204. }
  205. if (!strcmp(alg, "WEP")) {
  206. keys->type = DOT11_PRIV_WEP;
  207. } else if (!strcmp(alg, "TKIP")) {
  208. keys->type = DOT11_PRIV_TKIP;
  209. } else if (!strcmp(alg, "none")) {
  210. /* the only way to clear the key is to deauth it */
  211. /* and prism54 is capable to receive unencrypted packet */
  212. /* so we do nothing here */
  213. free(hdr);
  214. return 0;
  215. } else {
  216. printf("bad auth type: %s\n", alg);
  217. }
  218. buf = (u8 *) &keys->key[0];
  219. keys->length = key_len;
  220. keys->keyid = idx;
  221. keys->options = htons(DOT11_STAKEY_OPTION_DEFAULTKEY);
  222. keys->reserved = 0;
  223. hdr->op = htonl(PIMOP_SET);
  224. hdr->oid = htonl(DOT11_OID_STAKEY);
  225. memcpy(buf, key, key_len);
  226. ret = send(drv->pim_sock, hdr, blen, 0);
  227. if (ret < 0) {
  228. free(hdr);
  229. return ret;
  230. }
  231. prism54_waitpim(priv, hdr->oid, hdr, blen, 10);
  232. free(hdr);
  233. return 0;
  234. }
  235. /* get TKIP station sequence counter, prism54 is only 6 bytes */
  236. static int prism54_get_seqnum(const char *ifname, void *priv, const u8 *addr,
  237. int idx, u8 *seq)
  238. {
  239. struct prism54_driver_data *drv = priv;
  240. struct obj_stasc *stasc;
  241. pimdev_hdr *hdr;
  242. size_t blen;
  243. int ret = 0;
  244. blen = sizeof(*stasc) + sizeof(*hdr);
  245. hdr = malloc(blen);
  246. if (hdr == NULL)
  247. return -1;
  248. stasc = (struct obj_stasc *) &hdr[1];
  249. if (addr == NULL)
  250. memset(&stasc->address[0], 0xff, ETH_ALEN);
  251. else
  252. memcpy(&stasc->address[0], addr, ETH_ALEN);
  253. hdr->oid = htonl(DOT11_OID_STASC);
  254. hdr->op = htonl(PIMOP_GET);
  255. stasc->keyid = idx;
  256. if (send(drv->pim_sock,hdr,blen,0) <= 0) {
  257. free(hdr);
  258. return -1;
  259. }
  260. if (prism54_waitpim(priv, DOT11_OID_STASC, hdr, blen, 10) <= 0) {
  261. ret = -1;
  262. } else {
  263. if (hdr->op == (int) htonl(PIMOP_RESPONSE)) {
  264. memcpy(seq + 2, &stasc->sc_high, ETH_ALEN);
  265. memset(seq, 0, 2);
  266. } else {
  267. ret = -1;
  268. }
  269. }
  270. free(hdr);
  271. return ret;
  272. }
  273. /* include unencrypted, set mlme autolevel to extended */
  274. static int prism54_init_1x(void *priv)
  275. {
  276. struct prism54_driver_data *drv = priv;
  277. pimdev_hdr *hdr;
  278. unsigned long *ul;
  279. int blen = sizeof(*hdr) + sizeof(*ul);
  280. hdr = malloc(blen);
  281. if (hdr == NULL)
  282. return -1;
  283. ul = (unsigned long *) &hdr[1];
  284. hdr->op = htonl(PIMOP_SET);
  285. hdr->oid = htonl(DOT11_OID_EXUNENCRYPTED);
  286. *ul = htonl(DOT11_BOOL_TRUE); /* not accept */
  287. send(drv->pim_sock, hdr, blen, 0);
  288. prism54_waitpim(priv, DOT11_OID_EXUNENCRYPTED, hdr, blen, 10);
  289. hdr->op = htonl(PIMOP_SET);
  290. hdr->oid = htonl(DOT11_OID_MLMEAUTOLEVEL);
  291. *ul = htonl(DOT11_MLME_EXTENDED);
  292. send(drv->pim_sock, hdr, blen, 0);
  293. prism54_waitpim(priv, DOT11_OID_MLMEAUTOLEVEL, hdr, blen, 10);
  294. hdr->op = htonl(PIMOP_SET);
  295. hdr->oid = htonl(DOT11_OID_DOT1XENABLE);
  296. *ul = htonl(DOT11_BOOL_TRUE);
  297. send(drv->pim_sock, hdr, blen, 0);
  298. prism54_waitpim(priv, DOT11_OID_DOT1XENABLE, hdr, blen, 10);
  299. hdr->op = htonl(PIMOP_SET);
  300. hdr->oid = htonl(DOT11_OID_AUTHENABLE);
  301. *ul = htonl(DOT11_AUTH_OS); /* OS */
  302. send(drv->pim_sock, hdr, blen, 0);
  303. prism54_waitpim(priv, DOT11_OID_AUTHENABLE, hdr, blen, 10);
  304. free(hdr);
  305. return 0;
  306. }
  307. static int prism54_set_privacy_invoked(const char *ifname, void *priv,
  308. int flag)
  309. {
  310. struct prism54_driver_data *drv = priv;
  311. pimdev_hdr *hdr;
  312. unsigned long *ul;
  313. int ret;
  314. int blen = sizeof(*hdr) + sizeof(*ul);
  315. hdr = malloc(blen);
  316. if (hdr == NULL)
  317. return -1;
  318. ul = (unsigned long *) &hdr[1];
  319. hdr->op = htonl(PIMOP_SET);
  320. hdr->oid = htonl(DOT11_OID_PRIVACYINVOKED);
  321. if (flag) {
  322. *ul = htonl(DOT11_BOOL_TRUE); /* has privacy */
  323. } else {
  324. *ul = 0;
  325. }
  326. ret = send(drv->pim_sock, hdr, blen, 0);
  327. if (ret >= 0) {
  328. ret = prism54_waitpim(priv, DOT11_OID_PRIVACYINVOKED, hdr,
  329. blen, 10);
  330. }
  331. free(hdr);
  332. return ret;
  333. }
  334. static int prism54_ioctl_setiwessid(const char *ifname, void *priv,
  335. const u8 *buf, int len)
  336. {
  337. #if 0
  338. struct prism54_driver_data *drv = priv;
  339. struct iwreq iwr;
  340. memset(&iwr, 0, sizeof(iwr));
  341. os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
  342. iwr.u.essid.flags = 1; /* SSID active */
  343. iwr.u.essid.pointer = (caddr_t) buf;
  344. iwr.u.essid.length = len + 1;
  345. if (ioctl(drv->pim_sock, SIOCSIWESSID, &iwr) < 0) {
  346. perror("ioctl[SIOCSIWESSID]");
  347. printf("len=%d\n", len);
  348. return -1;
  349. }
  350. #endif
  351. return 0;
  352. }
  353. /* kick all stations */
  354. /* does not work during init, but at least it won't crash firmware */
  355. static int prism54_flush(void *priv)
  356. {
  357. struct prism54_driver_data *drv = priv;
  358. struct obj_mlmeex *mlme;
  359. pimdev_hdr *hdr;
  360. int ret;
  361. unsigned int i;
  362. long *nsta;
  363. int blen = sizeof(*hdr) + sizeof(*mlme);
  364. char *mac_id;
  365. hdr = os_zalloc(blen);
  366. if (hdr == NULL)
  367. return -1;
  368. mlme = (struct obj_mlmeex *) &hdr[1];
  369. nsta = (long *) &hdr[1];
  370. hdr->op = htonl(PIMOP_GET);
  371. hdr->oid = htonl(DOT11_OID_CLIENTS);
  372. ret = send(drv->pim_sock, hdr, sizeof(*hdr) + sizeof(long), 0);
  373. ret = prism54_waitpim(priv, DOT11_OID_CLIENTS, hdr, blen, 10);
  374. if ((ret < 0) || (hdr->op != (int) htonl(PIMOP_RESPONSE)) ||
  375. (le_to_host32(*nsta) > 2007)) {
  376. free(hdr);
  377. return 0;
  378. }
  379. for (i = 0; i < le_to_host32(*nsta); i++) {
  380. mlme->id = -1;
  381. mac_id = mac_id_get(drv, i);
  382. if (mac_id)
  383. memcpy(&mlme->address[0], mac_id, ETH_ALEN);
  384. mlme->code = host_to_le16(WLAN_REASON_UNSPECIFIED);
  385. mlme->state = htons(DOT11_STATE_NONE);
  386. mlme->size = 0;
  387. hdr->op = htonl(PIMOP_SET);
  388. hdr->oid = htonl(DOT11_OID_DISASSOCIATEEX);
  389. ret = send(drv->pim_sock, hdr, blen, 0);
  390. prism54_waitpim(priv, DOT11_OID_DISASSOCIATEEX, hdr, blen,
  391. 100);
  392. }
  393. for (i = 0; i < le_to_host32(*nsta); i++) {
  394. mlme->id = -1;
  395. mac_id = mac_id_get(drv, i);
  396. if (mac_id)
  397. memcpy(&mlme->address[0], mac_id, ETH_ALEN);
  398. mlme->code = host_to_le16(WLAN_REASON_UNSPECIFIED);
  399. mlme->state = htons(DOT11_STATE_NONE);
  400. mlme->size = 0;
  401. hdr->op = htonl(PIMOP_SET);
  402. hdr->oid = htonl(DOT11_OID_DEAUTHENTICATEEX);
  403. ret = send(drv->pim_sock, hdr, blen, 0);
  404. prism54_waitpim(priv, DOT11_OID_DEAUTHENTICATEEX, hdr, blen,
  405. 100);
  406. }
  407. free(hdr);
  408. return 0;
  409. }
  410. static int prism54_sta_deauth(void *priv, const u8 *addr, int reason)
  411. {
  412. struct prism54_driver_data *drv = priv;
  413. pimdev_hdr *hdr;
  414. struct obj_mlmeex *mlme;
  415. int ret;
  416. int blen = sizeof(*hdr) + sizeof(*mlme);
  417. hdr = malloc(blen);
  418. if (hdr == NULL)
  419. return -1;
  420. mlme = (struct obj_mlmeex *) &hdr[1];
  421. hdr->op = htonl(PIMOP_SET);
  422. hdr->oid = htonl(DOT11_OID_DEAUTHENTICATEEX);
  423. memcpy(&mlme->address[0], addr, ETH_ALEN);
  424. mlme->id = -1;
  425. mlme->state = htons(DOT11_STATE_NONE);
  426. mlme->code = host_to_le16(reason);
  427. mlme->size = 0;
  428. ret = send(drv->pim_sock, hdr, blen, 0);
  429. prism54_waitpim(priv, DOT11_OID_DEAUTHENTICATEEX, hdr, blen, 10);
  430. free(hdr);
  431. return ret;
  432. }
  433. static int prism54_sta_disassoc(void *priv, const u8 *addr, int reason)
  434. {
  435. struct prism54_driver_data *drv = priv;
  436. pimdev_hdr *hdr;
  437. struct obj_mlmeex *mlme;
  438. int ret;
  439. int blen = sizeof(*hdr) + sizeof(*mlme);
  440. hdr = malloc(blen);
  441. if (hdr == NULL)
  442. return -1;
  443. mlme = (struct obj_mlmeex *) &hdr[1];
  444. hdr->op = htonl(PIMOP_SET);
  445. hdr->oid = htonl(DOT11_OID_DISASSOCIATEEX);
  446. memcpy(&mlme->address[0], addr, ETH_ALEN);
  447. mlme->id = -1;
  448. mlme->state = htons(DOT11_STATE_NONE);
  449. mlme->code = host_to_le16(reason);
  450. mlme->size = 0;
  451. ret = send(drv->pim_sock, hdr, blen, 0);
  452. prism54_waitpim(priv, DOT11_OID_DISASSOCIATEEX, hdr, blen, 10);
  453. free(hdr);
  454. return ret;
  455. }
  456. static int prism54_get_inact_sec(void *priv, const u8 *addr)
  457. {
  458. struct prism54_driver_data *drv = priv;
  459. pimdev_hdr *hdr;
  460. struct obj_sta *sta;
  461. int blen = sizeof(*hdr) + sizeof(*sta);
  462. int ret;
  463. hdr = malloc(blen);
  464. if (hdr == NULL)
  465. return -1;
  466. hdr->op = htonl(PIMOP_GET);
  467. hdr->oid = htonl(DOT11_OID_CLIENTFIND);
  468. sta = (struct obj_sta *) &hdr[1];
  469. memcpy(&sta->address[0], addr, ETH_ALEN);
  470. ret = send(drv->pim_sock, hdr, blen, 0);
  471. ret = prism54_waitpim(priv, DOT11_OID_CLIENTFIND, hdr, blen, 10);
  472. if (ret != blen) {
  473. printf("get_inact_sec: bad return %d\n", ret);
  474. free(hdr);
  475. return -1;
  476. }
  477. if (hdr->op != (int) htonl(PIMOP_RESPONSE)) {
  478. printf("get_inact_sec: bad resp\n");
  479. free(hdr);
  480. return -1;
  481. }
  482. free(hdr);
  483. return le_to_host16(sta->age);
  484. }
  485. /* set attachments */
  486. static int prism54_set_generic_elem(const char *ifname, void *priv,
  487. const u8 *elem, size_t elem_len)
  488. {
  489. struct prism54_driver_data *drv = priv;
  490. pimdev_hdr *hdr;
  491. char *pos;
  492. struct obj_attachment_hdr *attach;
  493. size_t blen = sizeof(*hdr) + sizeof(*attach) + elem_len;
  494. hdr = os_zalloc(blen);
  495. if (hdr == NULL) {
  496. printf("%s: memory low\n", __func__);
  497. return -1;
  498. }
  499. hdr->op = htonl(PIMOP_SET);
  500. hdr->oid = htonl(DOT11_OID_ATTACHMENT);
  501. attach = (struct obj_attachment_hdr *)&hdr[1];
  502. attach->type = DOT11_PKT_BEACON;
  503. attach->id = -1;
  504. attach->size = host_to_le16((short)elem_len);
  505. pos = ((char*) attach) + sizeof(*attach);
  506. if (elem)
  507. memcpy(pos, elem, elem_len);
  508. send(drv->pim_sock, hdr, blen, 0);
  509. attach->type = DOT11_PKT_PROBE_RESP;
  510. send(drv->pim_sock, hdr, blen, 0);
  511. free(hdr);
  512. return 0;
  513. }
  514. /* tell the card to auth the sta */
  515. static void prism54_handle_probe(struct prism54_driver_data *drv,
  516. void *buf, size_t len)
  517. {
  518. struct obj_mlmeex *mlme;
  519. pimdev_hdr *hdr;
  520. struct sta_info *sta;
  521. hdr = (pimdev_hdr *)buf;
  522. mlme = (struct obj_mlmeex *) &hdr[1];
  523. sta = ap_get_sta(drv->hapd, (u8 *) &mlme->address[0]);
  524. if (sta != NULL) {
  525. if (sta->flags & (WLAN_STA_AUTH | WLAN_STA_ASSOC))
  526. return;
  527. }
  528. if (len < sizeof(*mlme)) {
  529. printf("bad probe packet\n");
  530. return;
  531. }
  532. mlme->state = htons(DOT11_STATE_AUTHING);
  533. mlme->code = 0;
  534. hdr->op = htonl(PIMOP_SET);
  535. hdr->oid = htonl(DOT11_OID_AUTHENTICATEEX);
  536. mlme->size = 0;
  537. send(drv->pim_sock, hdr, sizeof(*hdr)+sizeof(*mlme), 0);
  538. }
  539. static void prism54_handle_deauth(struct prism54_driver_data *drv,
  540. void *buf, size_t len)
  541. {
  542. struct obj_mlme *mlme;
  543. pimdev_hdr *hdr;
  544. struct sta_info *sta;
  545. char *mac_id;
  546. hdr = (pimdev_hdr *) buf;
  547. mlme = (struct obj_mlme *) &hdr[1];
  548. sta = ap_get_sta(drv->hapd, (u8 *) &mlme->address[0]);
  549. mac_id = mac_id_get(drv, mlme->id);
  550. if (sta == NULL || mac_id == NULL)
  551. return;
  552. memcpy(&mlme->address[0], mac_id, ETH_ALEN);
  553. sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
  554. wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
  555. sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
  556. ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
  557. ap_free_sta(drv->hapd, sta);
  558. }
  559. static void prism54_handle_disassoc(struct prism54_driver_data *drv,
  560. void *buf, size_t len)
  561. {
  562. struct obj_mlme *mlme;
  563. pimdev_hdr *hdr;
  564. struct sta_info *sta;
  565. char *mac_id;
  566. hdr = (pimdev_hdr *) buf;
  567. mlme = (struct obj_mlme *) &hdr[1];
  568. mac_id = mac_id_get(drv, mlme->id);
  569. if (mac_id == NULL)
  570. return;
  571. memcpy(&mlme->address[0], mac_id, ETH_ALEN);
  572. sta = ap_get_sta(drv->hapd, (u8 *) &mlme->address[0]);
  573. if (sta == NULL) {
  574. return;
  575. }
  576. sta->flags &= ~WLAN_STA_ASSOC;
  577. wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
  578. sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
  579. ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
  580. accounting_sta_stop(drv->hapd, sta);
  581. ieee802_1x_free_station(sta);
  582. }
  583. /* to auth it, just allow it now, later for os/sk */
  584. static void prism54_handle_auth(struct prism54_driver_data *drv,
  585. void *buf, size_t len)
  586. {
  587. struct obj_mlmeex *mlme;
  588. pimdev_hdr *hdr;
  589. struct sta_info *sta;
  590. int resp;
  591. hdr = (pimdev_hdr *) buf;
  592. mlme = (struct obj_mlmeex *) &hdr[1];
  593. if (len < sizeof(*mlme)) {
  594. printf("bad auth packet\n");
  595. return;
  596. }
  597. if (mlme->state == htons(DOT11_STATE_AUTHING)) {
  598. sta = ap_sta_add(drv->hapd, (u8 *) &mlme->address[0]);
  599. if (drv->hapd->tkip_countermeasures) {
  600. resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
  601. goto fail;
  602. }
  603. mac_id_refresh(drv, mlme->id, &mlme->address[0]);
  604. if (!sta) {
  605. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  606. goto fail;
  607. }
  608. sta->flags &= ~WLAN_STA_PREAUTH;
  609. ieee802_1x_notify_pre_auth(sta->eapol_sm, 0);
  610. sta->flags |= WLAN_STA_AUTH;
  611. wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
  612. mlme->code = 0;
  613. mlme->state=htons(DOT11_STATE_AUTH);
  614. hdr->op = htonl(PIMOP_SET);
  615. hdr->oid = htonl(DOT11_OID_AUTHENTICATEEX);
  616. mlme->size = 0;
  617. sta->timeout_next = STA_NULLFUNC;
  618. send(drv->pim_sock, hdr, sizeof(*hdr) + sizeof(*mlme), 0);
  619. }
  620. return;
  621. fail:
  622. printf("auth fail: %x\n", resp);
  623. mlme->code = host_to_le16(resp);
  624. mlme->size = 0;
  625. if (sta)
  626. sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
  627. hdr->oid = htonl(DOT11_OID_DEAUTHENTICATEEX);
  628. hdr->op = htonl(PIMOP_SET);
  629. send(drv->pim_sock, hdr, sizeof(*hdr)+sizeof(*mlme), 0);
  630. }
  631. /* do the wpa thing */
  632. static void prism54_handle_assoc(struct prism54_driver_data *drv,
  633. void *buf, size_t len)
  634. {
  635. pimdev_hdr *hdr;
  636. struct obj_mlmeex *mlme;
  637. struct ieee802_11_elems elems;
  638. struct sta_info *sta;
  639. u8 *wpa_ie;
  640. u8 *cb;
  641. int ieofs = 0;
  642. size_t wpa_ie_len;
  643. int resp, new_assoc;
  644. char *mac_id;
  645. resp = 0;
  646. hdr = (pimdev_hdr *) buf;
  647. mlme = (struct obj_mlmeex *) &hdr[1];
  648. switch (ntohl(hdr->oid)) {
  649. case DOT11_OID_ASSOCIATE:
  650. case DOT11_OID_REASSOCIATE:
  651. mlme->size = 0;
  652. default:
  653. break;
  654. }
  655. if ((mlme->state == (int) htonl(DOT11_STATE_ASSOCING)) ||
  656. (mlme->state == (int) htonl(DOT11_STATE_REASSOCING))) {
  657. if (len < sizeof(pimdev_hdr) + sizeof(struct obj_mlme)) {
  658. printf("bad assoc packet\n");
  659. return;
  660. }
  661. mac_id = mac_id_get(drv, mlme->id);
  662. if (mac_id == NULL)
  663. return;
  664. memcpy(&mlme->address[0], mac_id, ETH_ALEN);
  665. sta = ap_get_sta(drv->hapd, (u8 *) &mlme->address[0]);
  666. if (sta == NULL) {
  667. printf("cannot get sta\n");
  668. return;
  669. }
  670. cb = (u8 *) &mlme->data[0];
  671. if (hdr->oid == htonl(DOT11_OID_ASSOCIATEEX)) {
  672. ieofs = 4;
  673. } else if (hdr->oid == htonl(DOT11_OID_REASSOCIATEEX)) {
  674. ieofs = 10;
  675. }
  676. if (le_to_host16(mlme->size) <= ieofs) {
  677. printf("attach too small\n");
  678. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  679. goto fail;
  680. }
  681. if (ieee802_11_parse_elems(cb + ieofs,
  682. le_to_host16(mlme->size) - ieofs,
  683. &elems, 1) == ParseFailed) {
  684. printf("STA " MACSTR " sent invalid association "
  685. "request\n", MAC2STR(sta->addr));
  686. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  687. goto fail;
  688. }
  689. if ((drv->hapd->conf->wpa & WPA_PROTO_RSN) &&
  690. elems.rsn_ie) {
  691. wpa_ie = elems.rsn_ie;
  692. wpa_ie_len = elems.rsn_ie_len;
  693. } else if ((drv->hapd->conf->wpa & WPA_PROTO_WPA) &&
  694. elems.wpa_ie) {
  695. wpa_ie = elems.wpa_ie;
  696. wpa_ie_len = elems.wpa_ie_len;
  697. } else {
  698. wpa_ie = NULL;
  699. wpa_ie_len = 0;
  700. }
  701. if (drv->hapd->conf->wpa && wpa_ie == NULL) {
  702. printf("STA " MACSTR ": No WPA/RSN IE in association "
  703. "request\n", MAC2STR(sta->addr));
  704. resp = WLAN_STATUS_INVALID_IE;
  705. goto fail;
  706. }
  707. if (drv->hapd->conf->wpa) {
  708. int res;
  709. wpa_ie -= 2;
  710. wpa_ie_len += 2;
  711. if (sta->wpa_sm == NULL)
  712. sta->wpa_sm = wpa_auth_sta_init(
  713. drv->hapd->wpa_auth, sta->addr);
  714. if (sta->wpa_sm == NULL) {
  715. printf("Failed to initialize WPA state "
  716. "machine\n");
  717. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  718. goto fail;
  719. }
  720. res = wpa_validate_wpa_ie(drv->hapd->wpa_auth,
  721. sta->wpa_sm,
  722. wpa_ie, wpa_ie_len,
  723. NULL, 0);
  724. if (res == WPA_INVALID_GROUP)
  725. resp = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
  726. else if (res == WPA_INVALID_PAIRWISE)
  727. resp = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
  728. else if (res == WPA_INVALID_AKMP)
  729. resp = WLAN_STATUS_AKMP_NOT_VALID;
  730. else if (res == WPA_ALLOC_FAIL)
  731. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  732. else if (res != WPA_IE_OK)
  733. resp = WLAN_STATUS_INVALID_IE;
  734. if (resp != WLAN_STATUS_SUCCESS)
  735. goto fail;
  736. }
  737. hdr->oid = (hdr->oid == htonl(DOT11_OID_ASSOCIATEEX)) ?
  738. htonl(DOT11_OID_ASSOCIATEEX) :
  739. htonl(DOT11_OID_REASSOCIATEEX);
  740. hdr->op = htonl(PIMOP_SET);
  741. mlme->code = 0;
  742. mlme->state = htons(DOT11_STATE_ASSOC);
  743. mlme->size = 0;
  744. send(drv->pim_sock, hdr, sizeof(*hdr) + sizeof(*mlme), 0);
  745. return;
  746. } else if (mlme->state==htons(DOT11_STATE_ASSOC)) {
  747. if (len < sizeof(pimdev_hdr) + sizeof(struct obj_mlme)) {
  748. printf("bad assoc packet\n");
  749. return;
  750. }
  751. mac_id = mac_id_get(drv, mlme->id);
  752. if (mac_id == NULL)
  753. return;
  754. memcpy(&mlme->address[0], mac_id, ETH_ALEN);
  755. sta = ap_get_sta(drv->hapd, (u8 *) &mlme->address[0]);
  756. if (sta == NULL) {
  757. printf("cannot get sta\n");
  758. return;
  759. }
  760. new_assoc = (sta->flags & WLAN_STA_ASSOC) == 0;
  761. sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
  762. wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
  763. hostapd_new_assoc_sta(drv->hapd, sta, !new_assoc);
  764. ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
  765. sta->timeout_next = STA_NULLFUNC;
  766. return;
  767. }
  768. return;
  769. fail:
  770. printf("Prism54: assoc fail: %x\n", resp);
  771. mlme->code = host_to_le16(resp);
  772. mlme->size = 0;
  773. mlme->state = htons(DOT11_STATE_ASSOCING);
  774. hdr->oid = htonl(DOT11_OID_DISASSOCIATEEX);
  775. hdr->op = htonl(PIMOP_SET);
  776. sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
  777. send(drv->pim_sock, hdr, sizeof(*hdr) + sizeof(*mlme), 0);
  778. }
  779. static void handle_pim(int sock, void *eloop_ctx, void *sock_ctx)
  780. {
  781. struct prism54_driver_data *drv = eloop_ctx;
  782. int len;
  783. pimdev_hdr *hdr;
  784. hdr = malloc(PIM_BUF_SIZE);
  785. if (hdr == NULL)
  786. return;
  787. len = recv(sock, hdr, PIM_BUF_SIZE, 0);
  788. if (len < 0) {
  789. perror("recv");
  790. free(hdr);
  791. return;
  792. }
  793. if (len < 8) {
  794. printf("handle_pim: too short (%d)\n", len);
  795. free(hdr);
  796. return;
  797. }
  798. if (hdr->op != (int) htonl(PIMOP_TRAP)) {
  799. free(hdr);
  800. return;
  801. }
  802. switch (ntohl(hdr->oid)) {
  803. case DOT11_OID_PROBE:
  804. prism54_handle_probe(drv, hdr, len);
  805. break;
  806. case DOT11_OID_DEAUTHENTICATEEX:
  807. case DOT11_OID_DEAUTHENTICATE:
  808. prism54_handle_deauth(drv, hdr, len);
  809. break;
  810. case DOT11_OID_DISASSOCIATEEX:
  811. case DOT11_OID_DISASSOCIATE:
  812. prism54_handle_disassoc(drv, hdr, len);
  813. break;
  814. case DOT11_OID_AUTHENTICATEEX:
  815. case DOT11_OID_AUTHENTICATE:
  816. prism54_handle_auth(drv, hdr, len);
  817. break;
  818. case DOT11_OID_ASSOCIATEEX:
  819. case DOT11_OID_REASSOCIATEEX:
  820. case DOT11_OID_ASSOCIATE:
  821. case DOT11_OID_REASSOCIATE:
  822. prism54_handle_assoc(drv, hdr, len);
  823. default:
  824. break;
  825. }
  826. free(hdr);
  827. }
  828. static void handle_802_3(int sock, void *eloop_ctx, void *sock_ctx)
  829. {
  830. struct hostapd_data *hapd = (struct hostapd_data *) eloop_ctx;
  831. int len;
  832. ieee802_3_hdr *hdr;
  833. hdr = malloc(PIM_BUF_SIZE);
  834. if (hdr == NULL)
  835. return;
  836. len = recv(sock, hdr, PIM_BUF_SIZE, 0);
  837. if (len < 0) {
  838. perror("recv");
  839. free(hdr);
  840. return;
  841. }
  842. if (len < 14) {
  843. wpa_printf(MSG_MSGDUMP, "handle_802_3: too short (%d)", len);
  844. free(hdr);
  845. return;
  846. }
  847. if (hdr->type == htons(ETH_P_PAE)) {
  848. hostapd_eapol_receive(hapd, (u8 *) &hdr->sa[0], (u8 *) &hdr[1],
  849. len - sizeof(*hdr));
  850. }
  851. free(hdr);
  852. }
  853. static int prism54_init_sockets(struct prism54_driver_data *drv)
  854. {
  855. struct hostapd_data *hapd = drv->hapd;
  856. struct ifreq ifr;
  857. struct sockaddr_ll addr;
  858. drv->sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_PAE));
  859. if (drv->sock < 0) {
  860. perror("socket[PF_PACKET,SOCK_RAW]");
  861. return -1;
  862. }
  863. if (eloop_register_read_sock(drv->sock, handle_802_3, drv->hapd, NULL))
  864. {
  865. printf("Could not register read socket\n");
  866. return -1;
  867. }
  868. memset(&ifr, 0, sizeof(ifr));
  869. if (hapd->conf->bridge[0] != '\0') {
  870. printf("opening bridge: %s\n", hapd->conf->bridge);
  871. os_strlcpy(ifr.ifr_name, hapd->conf->bridge,
  872. sizeof(ifr.ifr_name));
  873. } else {
  874. os_strlcpy(ifr.ifr_name, drv->iface, sizeof(ifr.ifr_name));
  875. }
  876. if (ioctl(drv->sock, SIOCGIFINDEX, &ifr) != 0) {
  877. perror("ioctl(SIOCGIFINDEX)");
  878. return -1;
  879. }
  880. memset(&addr, 0, sizeof(addr));
  881. addr.sll_family = AF_PACKET;
  882. addr.sll_ifindex = ifr.ifr_ifindex;
  883. addr.sll_protocol = htons(ETH_P_PAE);
  884. wpa_printf(MSG_DEBUG, "Opening raw packet socket for ifindex %d",
  885. addr.sll_ifindex);
  886. if (bind(drv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  887. perror("bind");
  888. return -1;
  889. }
  890. memset(&ifr, 0, sizeof(ifr));
  891. os_strlcpy(ifr.ifr_name, drv->iface, sizeof(ifr.ifr_name));
  892. if (ioctl(drv->sock, SIOCGIFHWADDR, &ifr) != 0) {
  893. perror("ioctl(SIOCGIFHWADDR)");
  894. return -1;
  895. }
  896. if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
  897. printf("Invalid HW-addr family 0x%04x\n",
  898. ifr.ifr_hwaddr.sa_family);
  899. return -1;
  900. }
  901. memcpy(drv->hapd->own_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
  902. drv->pim_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
  903. if (drv->pim_sock < 0) {
  904. perror("socket[PF_PACKET,SOCK_RAW]");
  905. return -1;
  906. }
  907. if (eloop_register_read_sock(drv->pim_sock, handle_pim, drv, NULL)) {
  908. printf("Could not register read socket\n");
  909. return -1;
  910. }
  911. memset(&ifr, 0, sizeof(ifr));
  912. snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%sap", drv->iface);
  913. if (ioctl(drv->pim_sock, SIOCGIFINDEX, &ifr) != 0) {
  914. perror("ioctl(SIOCGIFINDEX)");
  915. return -1;
  916. }
  917. memset(&addr, 0, sizeof(addr));
  918. addr.sll_family = AF_PACKET;
  919. addr.sll_ifindex = ifr.ifr_ifindex;
  920. addr.sll_protocol = htons(ETH_P_ALL);
  921. wpa_printf(MSG_DEBUG, "Opening raw packet socket for ifindex %d",
  922. addr.sll_ifindex);
  923. if (bind(drv->pim_sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  924. perror("bind");
  925. return -1;
  926. }
  927. return 0;
  928. }
  929. static void * prism54_driver_init(struct hostapd_data *hapd)
  930. {
  931. struct prism54_driver_data *drv;
  932. drv = os_zalloc(sizeof(struct prism54_driver_data));
  933. if (drv == NULL) {
  934. printf("Could not allocate memory for hostapd Prism54 driver "
  935. "data\n");
  936. return NULL;
  937. }
  938. drv->hapd = hapd;
  939. drv->pim_sock = drv->sock = -1;
  940. memcpy(drv->iface, hapd->conf->iface, sizeof(drv->iface));
  941. if (prism54_init_sockets(drv)) {
  942. free(drv);
  943. return NULL;
  944. }
  945. prism54_init_1x(drv);
  946. /* must clean previous elems */
  947. prism54_set_generic_elem(drv->iface, drv, NULL, 0);
  948. return drv;
  949. }
  950. static void prism54_driver_deinit(void *priv)
  951. {
  952. struct prism54_driver_data *drv = priv;
  953. if (drv->pim_sock >= 0)
  954. close(drv->pim_sock);
  955. if (drv->sock >= 0)
  956. close(drv->sock);
  957. free(drv);
  958. }
  959. const struct wpa_driver_ops wpa_driver_prism54_ops = {
  960. .name = "prism54",
  961. .init = prism54_driver_init,
  962. .deinit = prism54_driver_deinit,
  963. /* .set_ieee8021x = prism54_init_1x, */
  964. .set_privacy = prism54_set_privacy_invoked,
  965. .set_encryption = prism54_set_encryption,
  966. .get_seqnum = prism54_get_seqnum,
  967. .flush = prism54_flush,
  968. .set_generic_elem = prism54_set_generic_elem,
  969. .send_eapol = prism54_send_eapol,
  970. .sta_set_flags = prism54_sta_set_flags,
  971. .sta_deauth = prism54_sta_deauth,
  972. .sta_disassoc = prism54_sta_disassoc,
  973. .set_ssid = prism54_ioctl_setiwessid,
  974. .get_inact_sec = prism54_get_inact_sec,
  975. };