driver_test.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225
  1. /*
  2. * hostapd / Driver interface for development testing
  3. * Copyright (c) 2004-2008, 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/un.h>
  16. #include <dirent.h>
  17. #include "hostapd.h"
  18. #include "driver.h"
  19. #include "sha1.h"
  20. #include "eloop.h"
  21. #include "wpa.h"
  22. #include "l2_packet/l2_packet.h"
  23. #include "hw_features.h"
  24. #include "wps_hostapd.h"
  25. struct test_client_socket {
  26. struct test_client_socket *next;
  27. u8 addr[ETH_ALEN];
  28. struct sockaddr_un un;
  29. socklen_t unlen;
  30. struct test_driver_bss *bss;
  31. };
  32. struct test_driver_bss {
  33. struct test_driver_bss *next;
  34. char ifname[IFNAMSIZ + 1];
  35. u8 bssid[ETH_ALEN];
  36. u8 *ie;
  37. size_t ielen;
  38. u8 *wps_beacon_ie;
  39. size_t wps_beacon_ie_len;
  40. u8 *wps_probe_resp_ie;
  41. size_t wps_probe_resp_ie_len;
  42. u8 ssid[32];
  43. size_t ssid_len;
  44. int privacy;
  45. };
  46. struct test_driver_data {
  47. struct hostapd_data *hapd;
  48. struct test_client_socket *cli;
  49. int test_socket;
  50. struct test_driver_bss *bss;
  51. char *socket_dir;
  52. char *own_socket_path;
  53. int udp_port;
  54. };
  55. static void test_driver_free_bss(struct test_driver_bss *bss)
  56. {
  57. free(bss->ie);
  58. free(bss->wps_beacon_ie);
  59. free(bss->wps_probe_resp_ie);
  60. free(bss);
  61. }
  62. static void test_driver_free_priv(struct test_driver_data *drv)
  63. {
  64. struct test_driver_bss *bss, *prev;
  65. if (drv == NULL)
  66. return;
  67. bss = drv->bss;
  68. while (bss) {
  69. prev = bss;
  70. bss = bss->next;
  71. test_driver_free_bss(prev);
  72. }
  73. free(drv->own_socket_path);
  74. free(drv->socket_dir);
  75. free(drv);
  76. }
  77. static struct test_client_socket *
  78. test_driver_get_cli(struct test_driver_data *drv, struct sockaddr_un *from,
  79. socklen_t fromlen)
  80. {
  81. struct test_client_socket *cli = drv->cli;
  82. while (cli) {
  83. if (cli->unlen == fromlen &&
  84. strncmp(cli->un.sun_path, from->sun_path,
  85. fromlen - sizeof(cli->un.sun_family)) == 0)
  86. return cli;
  87. cli = cli->next;
  88. }
  89. return NULL;
  90. }
  91. static int test_driver_send_eapol(void *priv, const u8 *addr, const u8 *data,
  92. size_t data_len, int encrypt,
  93. const u8 *own_addr)
  94. {
  95. struct test_driver_data *drv = priv;
  96. struct test_client_socket *cli;
  97. struct msghdr msg;
  98. struct iovec io[3];
  99. struct l2_ethhdr eth;
  100. if (drv->test_socket < 0)
  101. return -1;
  102. cli = drv->cli;
  103. while (cli) {
  104. if (memcmp(cli->addr, addr, ETH_ALEN) == 0)
  105. break;
  106. cli = cli->next;
  107. }
  108. if (!cli) {
  109. wpa_printf(MSG_DEBUG, "%s: no destination client entry",
  110. __func__);
  111. return -1;
  112. }
  113. memcpy(eth.h_dest, addr, ETH_ALEN);
  114. memcpy(eth.h_source, own_addr, ETH_ALEN);
  115. eth.h_proto = host_to_be16(ETH_P_EAPOL);
  116. io[0].iov_base = "EAPOL ";
  117. io[0].iov_len = 6;
  118. io[1].iov_base = &eth;
  119. io[1].iov_len = sizeof(eth);
  120. io[2].iov_base = (u8 *) data;
  121. io[2].iov_len = data_len;
  122. memset(&msg, 0, sizeof(msg));
  123. msg.msg_iov = io;
  124. msg.msg_iovlen = 3;
  125. msg.msg_name = &cli->un;
  126. msg.msg_namelen = cli->unlen;
  127. return sendmsg(drv->test_socket, &msg, 0);
  128. }
  129. static int test_driver_send_ether(void *priv, const u8 *dst, const u8 *src,
  130. u16 proto, const u8 *data, size_t data_len)
  131. {
  132. struct test_driver_data *drv = priv;
  133. struct msghdr msg;
  134. struct iovec io[3];
  135. struct l2_ethhdr eth;
  136. char desttxt[30];
  137. struct sockaddr_un addr;
  138. struct dirent *dent;
  139. DIR *dir;
  140. int ret = 0, broadcast = 0, count = 0;
  141. if (drv->test_socket < 0 || drv->socket_dir == NULL) {
  142. wpa_printf(MSG_DEBUG, "%s: invalid parameters (sock=%d "
  143. "socket_dir=%p)",
  144. __func__, drv->test_socket, drv->socket_dir);
  145. return -1;
  146. }
  147. broadcast = memcmp(dst, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) == 0;
  148. snprintf(desttxt, sizeof(desttxt), MACSTR, MAC2STR(dst));
  149. memcpy(eth.h_dest, dst, ETH_ALEN);
  150. memcpy(eth.h_source, src, ETH_ALEN);
  151. eth.h_proto = host_to_be16(proto);
  152. io[0].iov_base = "ETHER ";
  153. io[0].iov_len = 6;
  154. io[1].iov_base = &eth;
  155. io[1].iov_len = sizeof(eth);
  156. io[2].iov_base = (u8 *) data;
  157. io[2].iov_len = data_len;
  158. memset(&msg, 0, sizeof(msg));
  159. msg.msg_iov = io;
  160. msg.msg_iovlen = 3;
  161. dir = opendir(drv->socket_dir);
  162. if (dir == NULL) {
  163. perror("test_driver: opendir");
  164. return -1;
  165. }
  166. while ((dent = readdir(dir))) {
  167. #ifdef _DIRENT_HAVE_D_TYPE
  168. /* Skip the file if it is not a socket. Also accept
  169. * DT_UNKNOWN (0) in case the C library or underlying file
  170. * system does not support d_type. */
  171. if (dent->d_type != DT_SOCK && dent->d_type != DT_UNKNOWN)
  172. continue;
  173. #endif /* _DIRENT_HAVE_D_TYPE */
  174. if (strcmp(dent->d_name, ".") == 0 ||
  175. strcmp(dent->d_name, "..") == 0)
  176. continue;
  177. memset(&addr, 0, sizeof(addr));
  178. addr.sun_family = AF_UNIX;
  179. snprintf(addr.sun_path, sizeof(addr.sun_path), "%s/%s",
  180. drv->socket_dir, dent->d_name);
  181. if (strcmp(addr.sun_path, drv->own_socket_path) == 0)
  182. continue;
  183. if (!broadcast && strstr(dent->d_name, desttxt) == NULL)
  184. continue;
  185. wpa_printf(MSG_DEBUG, "%s: Send ether frame to %s",
  186. __func__, dent->d_name);
  187. msg.msg_name = &addr;
  188. msg.msg_namelen = sizeof(addr);
  189. ret = sendmsg(drv->test_socket, &msg, 0);
  190. if (ret < 0)
  191. perror("driver_test: sendmsg");
  192. count++;
  193. }
  194. closedir(dir);
  195. if (!broadcast && count == 0) {
  196. wpa_printf(MSG_DEBUG, "%s: Destination " MACSTR " not found",
  197. __func__, MAC2STR(dst));
  198. return -1;
  199. }
  200. return ret;
  201. }
  202. static int test_driver_send_mgmt_frame(void *priv, const void *buf,
  203. size_t len, int flags)
  204. {
  205. struct test_driver_data *drv = priv;
  206. struct msghdr msg;
  207. struct iovec io[2];
  208. const u8 *dest;
  209. int ret = 0, broadcast = 0;
  210. char desttxt[30];
  211. struct sockaddr_un addr;
  212. struct dirent *dent;
  213. DIR *dir;
  214. struct ieee80211_hdr *hdr;
  215. u16 fc;
  216. if (drv->test_socket < 0 || len < 10 || drv->socket_dir == NULL) {
  217. wpa_printf(MSG_DEBUG, "%s: invalid parameters (sock=%d len=%lu"
  218. " socket_dir=%p)",
  219. __func__, drv->test_socket, (unsigned long) len,
  220. drv->socket_dir);
  221. return -1;
  222. }
  223. dest = buf;
  224. dest += 4;
  225. broadcast = memcmp(dest, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) == 0;
  226. snprintf(desttxt, sizeof(desttxt), MACSTR, MAC2STR(dest));
  227. io[0].iov_base = "MLME ";
  228. io[0].iov_len = 5;
  229. io[1].iov_base = (void *) buf;
  230. io[1].iov_len = len;
  231. memset(&msg, 0, sizeof(msg));
  232. msg.msg_iov = io;
  233. msg.msg_iovlen = 2;
  234. dir = opendir(drv->socket_dir);
  235. if (dir == NULL) {
  236. perror("test_driver: opendir");
  237. return -1;
  238. }
  239. while ((dent = readdir(dir))) {
  240. #ifdef _DIRENT_HAVE_D_TYPE
  241. /* Skip the file if it is not a socket. Also accept
  242. * DT_UNKNOWN (0) in case the C library or underlying file
  243. * system does not support d_type. */
  244. if (dent->d_type != DT_SOCK && dent->d_type != DT_UNKNOWN)
  245. continue;
  246. #endif /* _DIRENT_HAVE_D_TYPE */
  247. if (strcmp(dent->d_name, ".") == 0 ||
  248. strcmp(dent->d_name, "..") == 0)
  249. continue;
  250. memset(&addr, 0, sizeof(addr));
  251. addr.sun_family = AF_UNIX;
  252. snprintf(addr.sun_path, sizeof(addr.sun_path), "%s/%s",
  253. drv->socket_dir, dent->d_name);
  254. if (strcmp(addr.sun_path, drv->own_socket_path) == 0)
  255. continue;
  256. if (!broadcast && strstr(dent->d_name, desttxt) == NULL)
  257. continue;
  258. wpa_printf(MSG_DEBUG, "%s: Send management frame to %s",
  259. __func__, dent->d_name);
  260. msg.msg_name = &addr;
  261. msg.msg_namelen = sizeof(addr);
  262. ret = sendmsg(drv->test_socket, &msg, 0);
  263. if (ret < 0)
  264. perror("driver_test: sendmsg");
  265. }
  266. closedir(dir);
  267. hdr = (struct ieee80211_hdr *) buf;
  268. fc = le_to_host16(hdr->frame_control);
  269. hostapd_mgmt_tx_cb(drv->hapd, (u8 *) buf, len, WLAN_FC_GET_STYPE(fc),
  270. ret >= 0);
  271. return ret;
  272. }
  273. static void test_driver_scan(struct test_driver_data *drv,
  274. struct sockaddr_un *from, socklen_t fromlen,
  275. char *data)
  276. {
  277. char buf[512], *pos, *end;
  278. int ret;
  279. struct test_driver_bss *bss;
  280. u8 sa[ETH_ALEN];
  281. u8 ie[512];
  282. size_t ielen;
  283. /* data: optional [ ' ' | STA-addr | ' ' | IEs(hex) ] */
  284. wpa_printf(MSG_DEBUG, "test_driver: SCAN");
  285. if (*data) {
  286. if (*data != ' ' ||
  287. hwaddr_aton(data + 1, sa)) {
  288. wpa_printf(MSG_DEBUG, "test_driver: Unexpected SCAN "
  289. "command format");
  290. return;
  291. }
  292. data += 18;
  293. while (*data == ' ')
  294. data++;
  295. ielen = os_strlen(data) / 2;
  296. if (ielen > sizeof(ie))
  297. ielen = sizeof(ie);
  298. if (hexstr2bin(data, ie, ielen) < 0)
  299. ielen = 0;
  300. wpa_printf(MSG_DEBUG, "test_driver: Scan from " MACSTR,
  301. MAC2STR(sa));
  302. wpa_hexdump(MSG_MSGDUMP, "test_driver: scan IEs", ie, ielen);
  303. hostapd_wps_probe_req_rx(drv->hapd, sa, ie, ielen);
  304. }
  305. for (bss = drv->bss; bss; bss = bss->next) {
  306. pos = buf;
  307. end = buf + sizeof(buf);
  308. /* reply: SCANRESP BSSID SSID IEs */
  309. ret = snprintf(pos, end - pos, "SCANRESP " MACSTR " ",
  310. MAC2STR(bss->bssid));
  311. if (ret < 0 || ret >= end - pos)
  312. return;
  313. pos += ret;
  314. pos += wpa_snprintf_hex(pos, end - pos,
  315. bss->ssid, bss->ssid_len);
  316. ret = snprintf(pos, end - pos, " ");
  317. if (ret < 0 || ret >= end - pos)
  318. return;
  319. pos += ret;
  320. pos += wpa_snprintf_hex(pos, end - pos, bss->ie, bss->ielen);
  321. pos += wpa_snprintf_hex(pos, end - pos, bss->wps_probe_resp_ie,
  322. bss->wps_probe_resp_ie_len);
  323. if (bss->privacy) {
  324. ret = snprintf(pos, end - pos, " PRIVACY");
  325. if (ret < 0 || ret >= end - pos)
  326. return;
  327. pos += ret;
  328. }
  329. sendto(drv->test_socket, buf, pos - buf, 0,
  330. (struct sockaddr *) from, fromlen);
  331. }
  332. }
  333. static struct hostapd_data * test_driver_get_hapd(struct test_driver_data *drv,
  334. struct test_driver_bss *bss)
  335. {
  336. struct hostapd_iface *iface = drv->hapd->iface;
  337. struct hostapd_data *hapd = NULL;
  338. size_t i;
  339. if (bss == NULL) {
  340. wpa_printf(MSG_DEBUG, "%s: bss == NULL", __func__);
  341. return NULL;
  342. }
  343. for (i = 0; i < iface->num_bss; i++) {
  344. hapd = iface->bss[i];
  345. if (memcmp(hapd->own_addr, bss->bssid, ETH_ALEN) == 0)
  346. break;
  347. }
  348. if (i == iface->num_bss) {
  349. wpa_printf(MSG_DEBUG, "%s: no matching interface entry found "
  350. "for BSSID " MACSTR, __func__, MAC2STR(bss->bssid));
  351. return NULL;
  352. }
  353. return hapd;
  354. }
  355. static int test_driver_new_sta(struct test_driver_data *drv,
  356. struct test_driver_bss *bss, const u8 *addr,
  357. const u8 *ie, size_t ielen)
  358. {
  359. struct hostapd_data *hapd;
  360. hapd = test_driver_get_hapd(drv, bss);
  361. if (hapd == NULL)
  362. return -1;
  363. return hostapd_notif_assoc(hapd, addr, ie, ielen);
  364. }
  365. static void test_driver_assoc(struct test_driver_data *drv,
  366. struct sockaddr_un *from, socklen_t fromlen,
  367. char *data)
  368. {
  369. struct test_client_socket *cli;
  370. u8 ie[256], ssid[32];
  371. size_t ielen, ssid_len = 0;
  372. char *pos, *pos2, cmd[50];
  373. struct test_driver_bss *bss;
  374. /* data: STA-addr SSID(hex) IEs(hex) */
  375. cli = os_zalloc(sizeof(*cli));
  376. if (cli == NULL)
  377. return;
  378. if (hwaddr_aton(data, cli->addr)) {
  379. printf("test_socket: Invalid MAC address '%s' in ASSOC\n",
  380. data);
  381. free(cli);
  382. return;
  383. }
  384. pos = data + 17;
  385. while (*pos == ' ')
  386. pos++;
  387. pos2 = strchr(pos, ' ');
  388. ielen = 0;
  389. if (pos2) {
  390. ssid_len = (pos2 - pos) / 2;
  391. if (hexstr2bin(pos, ssid, ssid_len) < 0) {
  392. wpa_printf(MSG_DEBUG, "%s: Invalid SSID", __func__);
  393. free(cli);
  394. return;
  395. }
  396. wpa_hexdump_ascii(MSG_DEBUG, "test_driver_assoc: SSID",
  397. ssid, ssid_len);
  398. pos = pos2 + 1;
  399. ielen = strlen(pos) / 2;
  400. if (ielen > sizeof(ie))
  401. ielen = sizeof(ie);
  402. if (hexstr2bin(pos, ie, ielen) < 0)
  403. ielen = 0;
  404. }
  405. for (bss = drv->bss; bss; bss = bss->next) {
  406. if (bss->ssid_len == ssid_len &&
  407. memcmp(bss->ssid, ssid, ssid_len) == 0)
  408. break;
  409. }
  410. if (bss == NULL) {
  411. wpa_printf(MSG_DEBUG, "%s: No matching SSID found from "
  412. "configured BSSes", __func__);
  413. free(cli);
  414. return;
  415. }
  416. cli->bss = bss;
  417. memcpy(&cli->un, from, sizeof(cli->un));
  418. cli->unlen = fromlen;
  419. cli->next = drv->cli;
  420. drv->cli = cli;
  421. wpa_hexdump_ascii(MSG_DEBUG, "test_socket: ASSOC sun_path",
  422. (const u8 *) cli->un.sun_path,
  423. cli->unlen - sizeof(cli->un.sun_family));
  424. snprintf(cmd, sizeof(cmd), "ASSOCRESP " MACSTR " 0",
  425. MAC2STR(bss->bssid));
  426. sendto(drv->test_socket, cmd, strlen(cmd), 0,
  427. (struct sockaddr *) from, fromlen);
  428. if (test_driver_new_sta(drv, bss, cli->addr, ie, ielen) < 0) {
  429. wpa_printf(MSG_DEBUG, "test_driver: failed to add new STA");
  430. }
  431. }
  432. static void test_driver_disassoc(struct test_driver_data *drv,
  433. struct sockaddr_un *from, socklen_t fromlen)
  434. {
  435. struct test_client_socket *cli;
  436. cli = test_driver_get_cli(drv, from, fromlen);
  437. if (!cli)
  438. return;
  439. hostapd_notif_disassoc(drv->hapd, cli->addr);
  440. }
  441. static void test_driver_eapol(struct test_driver_data *drv,
  442. struct sockaddr_un *from, socklen_t fromlen,
  443. u8 *data, size_t datalen)
  444. {
  445. struct test_client_socket *cli;
  446. if (datalen > 14) {
  447. /* Skip Ethernet header */
  448. wpa_printf(MSG_DEBUG, "test_driver: dst=" MACSTR " src="
  449. MACSTR " proto=%04x",
  450. MAC2STR(data), MAC2STR(data + ETH_ALEN),
  451. WPA_GET_BE16(data + 2 * ETH_ALEN));
  452. data += 14;
  453. datalen -= 14;
  454. }
  455. cli = test_driver_get_cli(drv, from, fromlen);
  456. if (cli) {
  457. struct hostapd_data *hapd;
  458. hapd = test_driver_get_hapd(drv, cli->bss);
  459. if (hapd == NULL)
  460. return;
  461. hostapd_eapol_receive(hapd, cli->addr, data, datalen);
  462. } else {
  463. wpa_printf(MSG_DEBUG, "test_socket: EAPOL from unknown "
  464. "client");
  465. }
  466. }
  467. static void test_driver_ether(struct test_driver_data *drv,
  468. struct sockaddr_un *from, socklen_t fromlen,
  469. u8 *data, size_t datalen)
  470. {
  471. struct l2_ethhdr *eth;
  472. if (datalen < sizeof(*eth))
  473. return;
  474. eth = (struct l2_ethhdr *) data;
  475. wpa_printf(MSG_DEBUG, "test_driver: RX ETHER dst=" MACSTR " src="
  476. MACSTR " proto=%04x",
  477. MAC2STR(eth->h_dest), MAC2STR(eth->h_source),
  478. be_to_host16(eth->h_proto));
  479. #ifdef CONFIG_IEEE80211R
  480. if (be_to_host16(eth->h_proto) == ETH_P_RRB) {
  481. wpa_ft_rrb_rx(drv->hapd->wpa_auth, eth->h_source,
  482. data + sizeof(*eth), datalen - sizeof(*eth));
  483. }
  484. #endif /* CONFIG_IEEE80211R */
  485. }
  486. static void test_driver_mlme(struct test_driver_data *drv,
  487. struct sockaddr_un *from, socklen_t fromlen,
  488. u8 *data, size_t datalen)
  489. {
  490. struct ieee80211_hdr *hdr;
  491. u16 fc;
  492. hdr = (struct ieee80211_hdr *) data;
  493. if (test_driver_get_cli(drv, from, fromlen) == NULL && datalen >= 16) {
  494. struct test_client_socket *cli;
  495. cli = os_zalloc(sizeof(*cli));
  496. if (cli == NULL)
  497. return;
  498. wpa_printf(MSG_DEBUG, "Adding client entry for " MACSTR,
  499. MAC2STR(hdr->addr2));
  500. memcpy(cli->addr, hdr->addr2, ETH_ALEN);
  501. memcpy(&cli->un, from, sizeof(cli->un));
  502. cli->unlen = fromlen;
  503. cli->next = drv->cli;
  504. drv->cli = cli;
  505. }
  506. wpa_hexdump(MSG_MSGDUMP, "test_driver_mlme: received frame",
  507. data, datalen);
  508. fc = le_to_host16(hdr->frame_control);
  509. if (WLAN_FC_GET_TYPE(fc) != WLAN_FC_TYPE_MGMT) {
  510. wpa_printf(MSG_ERROR, "%s: received non-mgmt frame",
  511. __func__);
  512. return;
  513. }
  514. hostapd_mgmt_rx(drv->hapd, data, datalen, WLAN_FC_GET_STYPE(fc), NULL);
  515. }
  516. static void test_driver_receive_unix(int sock, void *eloop_ctx, void *sock_ctx)
  517. {
  518. struct test_driver_data *drv = eloop_ctx;
  519. char buf[2000];
  520. int res;
  521. struct sockaddr_un from;
  522. socklen_t fromlen = sizeof(from);
  523. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  524. (struct sockaddr *) &from, &fromlen);
  525. if (res < 0) {
  526. perror("recvfrom(test_socket)");
  527. return;
  528. }
  529. buf[res] = '\0';
  530. wpa_printf(MSG_DEBUG, "test_driver: received %u bytes", res);
  531. if (strncmp(buf, "SCAN", 4) == 0) {
  532. test_driver_scan(drv, &from, fromlen, buf + 4);
  533. } else if (strncmp(buf, "ASSOC ", 6) == 0) {
  534. test_driver_assoc(drv, &from, fromlen, buf + 6);
  535. } else if (strcmp(buf, "DISASSOC") == 0) {
  536. test_driver_disassoc(drv, &from, fromlen);
  537. } else if (strncmp(buf, "EAPOL ", 6) == 0) {
  538. test_driver_eapol(drv, &from, fromlen, (u8 *) buf + 6,
  539. res - 6);
  540. } else if (strncmp(buf, "ETHER ", 6) == 0) {
  541. test_driver_ether(drv, &from, fromlen, (u8 *) buf + 6,
  542. res - 6);
  543. } else if (strncmp(buf, "MLME ", 5) == 0) {
  544. test_driver_mlme(drv, &from, fromlen, (u8 *) buf + 5, res - 5);
  545. } else {
  546. wpa_hexdump_ascii(MSG_DEBUG, "Unknown test_socket command",
  547. (u8 *) buf, res);
  548. }
  549. }
  550. static struct test_driver_bss *
  551. test_driver_get_bss(struct test_driver_data *drv, const char *ifname)
  552. {
  553. struct test_driver_bss *bss;
  554. for (bss = drv->bss; bss; bss = bss->next) {
  555. if (strcmp(bss->ifname, ifname) == 0)
  556. return bss;
  557. }
  558. return NULL;
  559. }
  560. static int test_driver_set_generic_elem(const char *ifname, void *priv,
  561. const u8 *elem, size_t elem_len)
  562. {
  563. struct test_driver_data *drv = priv;
  564. struct test_driver_bss *bss;
  565. bss = test_driver_get_bss(drv, ifname);
  566. if (bss == NULL)
  567. return -1;
  568. free(bss->ie);
  569. if (elem == NULL) {
  570. bss->ie = NULL;
  571. bss->ielen = 0;
  572. return 0;
  573. }
  574. bss->ie = malloc(elem_len);
  575. if (bss->ie == NULL) {
  576. bss->ielen = 0;
  577. return -1;
  578. }
  579. memcpy(bss->ie, elem, elem_len);
  580. bss->ielen = elem_len;
  581. return 0;
  582. }
  583. static int test_driver_set_wps_beacon_ie(const char *ifname, void *priv,
  584. const u8 *ie, size_t len)
  585. {
  586. struct test_driver_data *drv = priv;
  587. struct test_driver_bss *bss;
  588. wpa_hexdump(MSG_DEBUG, "test_driver: Beacon WPS IE", ie, len);
  589. bss = test_driver_get_bss(drv, ifname);
  590. if (bss == NULL)
  591. return -1;
  592. free(bss->wps_beacon_ie);
  593. if (ie == NULL) {
  594. bss->wps_beacon_ie = NULL;
  595. bss->wps_beacon_ie_len = 0;
  596. return 0;
  597. }
  598. bss->wps_beacon_ie = malloc(len);
  599. if (bss->wps_beacon_ie == NULL) {
  600. bss->wps_beacon_ie_len = 0;
  601. return -1;
  602. }
  603. memcpy(bss->wps_beacon_ie, ie, len);
  604. bss->wps_beacon_ie_len = len;
  605. return 0;
  606. }
  607. static int test_driver_set_wps_probe_resp_ie(const char *ifname, void *priv,
  608. const u8 *ie, size_t len)
  609. {
  610. struct test_driver_data *drv = priv;
  611. struct test_driver_bss *bss;
  612. wpa_hexdump(MSG_DEBUG, "test_driver: ProbeResp WPS IE", ie, len);
  613. bss = test_driver_get_bss(drv, ifname);
  614. if (bss == NULL)
  615. return -1;
  616. free(bss->wps_probe_resp_ie);
  617. if (ie == NULL) {
  618. bss->wps_probe_resp_ie = NULL;
  619. bss->wps_probe_resp_ie_len = 0;
  620. return 0;
  621. }
  622. bss->wps_probe_resp_ie = malloc(len);
  623. if (bss->wps_probe_resp_ie == NULL) {
  624. bss->wps_probe_resp_ie_len = 0;
  625. return -1;
  626. }
  627. memcpy(bss->wps_probe_resp_ie, ie, len);
  628. bss->wps_probe_resp_ie_len = len;
  629. return 0;
  630. }
  631. static int test_driver_sta_deauth(void *priv, const u8 *addr, int reason)
  632. {
  633. struct test_driver_data *drv = priv;
  634. struct test_client_socket *cli;
  635. if (drv->test_socket < 0)
  636. return -1;
  637. cli = drv->cli;
  638. while (cli) {
  639. if (memcmp(cli->addr, addr, ETH_ALEN) == 0)
  640. break;
  641. cli = cli->next;
  642. }
  643. if (!cli)
  644. return -1;
  645. return sendto(drv->test_socket, "DEAUTH", 6, 0,
  646. (struct sockaddr *) &cli->un, cli->unlen);
  647. }
  648. static int test_driver_sta_disassoc(void *priv, const u8 *addr, int reason)
  649. {
  650. struct test_driver_data *drv = priv;
  651. struct test_client_socket *cli;
  652. if (drv->test_socket < 0)
  653. return -1;
  654. cli = drv->cli;
  655. while (cli) {
  656. if (memcmp(cli->addr, addr, ETH_ALEN) == 0)
  657. break;
  658. cli = cli->next;
  659. }
  660. if (!cli)
  661. return -1;
  662. return sendto(drv->test_socket, "DISASSOC", 8, 0,
  663. (struct sockaddr *) &cli->un, cli->unlen);
  664. }
  665. static struct hostapd_hw_modes *
  666. test_driver_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags)
  667. {
  668. struct hostapd_hw_modes *modes;
  669. *num_modes = 3;
  670. *flags = 0;
  671. modes = os_zalloc(*num_modes * sizeof(struct hostapd_hw_modes));
  672. if (modes == NULL)
  673. return NULL;
  674. modes[0].mode = HOSTAPD_MODE_IEEE80211G;
  675. modes[0].num_channels = 1;
  676. modes[0].num_rates = 1;
  677. modes[0].channels = os_zalloc(sizeof(struct hostapd_channel_data));
  678. modes[0].rates = os_zalloc(sizeof(struct hostapd_rate_data));
  679. if (modes[0].channels == NULL || modes[0].rates == NULL) {
  680. hostapd_free_hw_features(modes, *num_modes);
  681. return NULL;
  682. }
  683. modes[0].channels[0].chan = 1;
  684. modes[0].channels[0].freq = 2412;
  685. modes[0].channels[0].flag = 0;
  686. modes[0].rates[0].rate = 10;
  687. modes[0].rates[0].flags = HOSTAPD_RATE_BASIC | HOSTAPD_RATE_SUPPORTED |
  688. HOSTAPD_RATE_CCK | HOSTAPD_RATE_MANDATORY;
  689. modes[1].mode = HOSTAPD_MODE_IEEE80211B;
  690. modes[1].num_channels = 1;
  691. modes[1].num_rates = 1;
  692. modes[1].channels = os_zalloc(sizeof(struct hostapd_channel_data));
  693. modes[1].rates = os_zalloc(sizeof(struct hostapd_rate_data));
  694. if (modes[1].channels == NULL || modes[1].rates == NULL) {
  695. hostapd_free_hw_features(modes, *num_modes);
  696. return NULL;
  697. }
  698. modes[1].channels[0].chan = 1;
  699. modes[1].channels[0].freq = 2412;
  700. modes[1].channels[0].flag = 0;
  701. modes[1].rates[0].rate = 10;
  702. modes[1].rates[0].flags = HOSTAPD_RATE_BASIC | HOSTAPD_RATE_SUPPORTED |
  703. HOSTAPD_RATE_CCK | HOSTAPD_RATE_MANDATORY;
  704. modes[2].mode = HOSTAPD_MODE_IEEE80211A;
  705. modes[2].num_channels = 1;
  706. modes[2].num_rates = 1;
  707. modes[2].channels = os_zalloc(sizeof(struct hostapd_channel_data));
  708. modes[2].rates = os_zalloc(sizeof(struct hostapd_rate_data));
  709. if (modes[2].channels == NULL || modes[2].rates == NULL) {
  710. hostapd_free_hw_features(modes, *num_modes);
  711. return NULL;
  712. }
  713. modes[2].channels[0].chan = 60;
  714. modes[2].channels[0].freq = 5300;
  715. modes[2].channels[0].flag = 0;
  716. modes[2].rates[0].rate = 60;
  717. modes[2].rates[0].flags = HOSTAPD_RATE_BASIC | HOSTAPD_RATE_SUPPORTED |
  718. HOSTAPD_RATE_MANDATORY;
  719. return modes;
  720. }
  721. static int test_driver_bss_add(void *priv, const char *ifname, const u8 *bssid)
  722. {
  723. struct test_driver_data *drv = priv;
  724. struct test_driver_bss *bss;
  725. wpa_printf(MSG_DEBUG, "%s(ifname=%s bssid=" MACSTR ")",
  726. __func__, ifname, MAC2STR(bssid));
  727. bss = os_zalloc(sizeof(*bss));
  728. if (bss == NULL)
  729. return -1;
  730. os_strlcpy(bss->ifname, ifname, IFNAMSIZ);
  731. memcpy(bss->bssid, bssid, ETH_ALEN);
  732. bss->next = drv->bss;
  733. drv->bss = bss;
  734. return 0;
  735. }
  736. static int test_driver_bss_remove(void *priv, const char *ifname)
  737. {
  738. struct test_driver_data *drv = priv;
  739. struct test_driver_bss *bss, *prev;
  740. struct test_client_socket *cli, *prev_c;
  741. wpa_printf(MSG_DEBUG, "%s(ifname=%s)", __func__, ifname);
  742. for (prev = NULL, bss = drv->bss; bss; prev = bss, bss = bss->next) {
  743. if (strcmp(bss->ifname, ifname) != 0)
  744. continue;
  745. if (prev)
  746. prev->next = bss->next;
  747. else
  748. drv->bss = bss->next;
  749. for (prev_c = NULL, cli = drv->cli; cli;
  750. prev_c = cli, cli = cli->next) {
  751. if (cli->bss != bss)
  752. continue;
  753. if (prev_c)
  754. prev_c->next = cli->next;
  755. else
  756. drv->cli = cli->next;
  757. free(cli);
  758. break;
  759. }
  760. test_driver_free_bss(bss);
  761. return 0;
  762. }
  763. return -1;
  764. }
  765. static int test_driver_if_add(const char *iface, void *priv,
  766. enum hostapd_driver_if_type type, char *ifname,
  767. const u8 *addr)
  768. {
  769. wpa_printf(MSG_DEBUG, "%s(iface=%s type=%d ifname=%s)",
  770. __func__, iface, type, ifname);
  771. return 0;
  772. }
  773. static int test_driver_if_update(void *priv, enum hostapd_driver_if_type type,
  774. char *ifname, const u8 *addr)
  775. {
  776. wpa_printf(MSG_DEBUG, "%s(type=%d ifname=%s)", __func__, type, ifname);
  777. return 0;
  778. }
  779. static int test_driver_if_remove(void *priv, enum hostapd_driver_if_type type,
  780. const char *ifname, const u8 *addr)
  781. {
  782. wpa_printf(MSG_DEBUG, "%s(type=%d ifname=%s)", __func__, type, ifname);
  783. return 0;
  784. }
  785. static int test_driver_valid_bss_mask(void *priv, const u8 *addr,
  786. const u8 *mask)
  787. {
  788. return 0;
  789. }
  790. static int test_driver_set_ssid(const char *ifname, void *priv, const u8 *buf,
  791. int len)
  792. {
  793. struct test_driver_data *drv = priv;
  794. struct test_driver_bss *bss;
  795. wpa_printf(MSG_DEBUG, "%s(ifname=%s)", __func__, ifname);
  796. wpa_hexdump_ascii(MSG_DEBUG, "test_driver_set_ssid: SSID", buf, len);
  797. for (bss = drv->bss; bss; bss = bss->next) {
  798. if (strcmp(bss->ifname, ifname) != 0)
  799. continue;
  800. if (len < 0 || (size_t) len > sizeof(bss->ssid))
  801. return -1;
  802. memcpy(bss->ssid, buf, len);
  803. bss->ssid_len = len;
  804. return 0;
  805. }
  806. return -1;
  807. }
  808. static int test_driver_set_privacy(const char *ifname, void *priv, int enabled)
  809. {
  810. struct test_driver_data *drv = priv;
  811. struct test_driver_bss *bss;
  812. wpa_printf(MSG_DEBUG, "%s(ifname=%s enabled=%d)",
  813. __func__, ifname, enabled);
  814. for (bss = drv->bss; bss; bss = bss->next) {
  815. if (strcmp(bss->ifname, ifname) != 0)
  816. continue;
  817. bss->privacy = enabled;
  818. return 0;
  819. }
  820. return -1;
  821. }
  822. static int test_driver_set_encryption(const char *iface, void *priv,
  823. const char *alg, const u8 *addr, int idx,
  824. const u8 *key, size_t key_len, int txkey)
  825. {
  826. wpa_printf(MSG_DEBUG, "%s(iface=%s alg=%s idx=%d txkey=%d)",
  827. __func__, iface, alg, idx, txkey);
  828. if (addr)
  829. wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
  830. if (key)
  831. wpa_hexdump_key(MSG_DEBUG, " key", key, key_len);
  832. return 0;
  833. }
  834. static int test_driver_set_sta_vlan(void *priv, const u8 *addr,
  835. const char *ifname, int vlan_id)
  836. {
  837. wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " ifname=%s vlan_id=%d)",
  838. __func__, MAC2STR(addr), ifname, vlan_id);
  839. return 0;
  840. }
  841. static int test_driver_sta_add(const char *ifname, void *priv, const u8 *addr,
  842. u16 aid, u16 capability, u8 *supp_rates,
  843. size_t supp_rates_len, int flags,
  844. u16 listen_interval)
  845. {
  846. struct test_driver_data *drv = priv;
  847. struct test_client_socket *cli;
  848. struct test_driver_bss *bss;
  849. wpa_printf(MSG_DEBUG, "%s(ifname=%s addr=" MACSTR " aid=%d "
  850. "capability=0x%x flags=0x%x listen_interval=%d)",
  851. __func__, ifname, MAC2STR(addr), aid, capability, flags,
  852. listen_interval);
  853. wpa_hexdump(MSG_DEBUG, "test_driver_sta_add - supp_rates",
  854. supp_rates, supp_rates_len);
  855. cli = drv->cli;
  856. while (cli) {
  857. if (memcmp(cli->addr, addr, ETH_ALEN) == 0)
  858. break;
  859. cli = cli->next;
  860. }
  861. if (!cli) {
  862. wpa_printf(MSG_DEBUG, "%s: no matching client entry",
  863. __func__);
  864. return -1;
  865. }
  866. for (bss = drv->bss; bss; bss = bss->next) {
  867. if (strcmp(ifname, bss->ifname) == 0)
  868. break;
  869. }
  870. if (bss == NULL) {
  871. wpa_printf(MSG_DEBUG, "%s: No matching interface found from "
  872. "configured BSSes", __func__);
  873. return -1;
  874. }
  875. cli->bss = bss;
  876. return 0;
  877. }
  878. static void * test_driver_init(struct hostapd_data *hapd)
  879. {
  880. struct test_driver_data *drv;
  881. struct sockaddr_un addr_un;
  882. struct sockaddr_in addr_in;
  883. struct sockaddr *addr;
  884. socklen_t alen;
  885. drv = os_zalloc(sizeof(struct test_driver_data));
  886. if (drv == NULL) {
  887. printf("Could not allocate memory for test driver data\n");
  888. return NULL;
  889. }
  890. drv->bss = os_zalloc(sizeof(*drv->bss));
  891. if (drv->bss == NULL) {
  892. printf("Could not allocate memory for test driver BSS data\n");
  893. free(drv);
  894. return NULL;
  895. }
  896. drv->hapd = hapd;
  897. /* Generate a MAC address to help testing with multiple APs */
  898. hapd->own_addr[0] = 0x02; /* locally administered */
  899. sha1_prf((const u8 *) hapd->conf->iface, strlen(hapd->conf->iface),
  900. "hostapd test bssid generation",
  901. (const u8 *) hapd->conf->ssid.ssid, hapd->conf->ssid.ssid_len,
  902. hapd->own_addr + 1, ETH_ALEN - 1);
  903. os_strlcpy(drv->bss->ifname, hapd->conf->iface, IFNAMSIZ);
  904. memcpy(drv->bss->bssid, hapd->own_addr, ETH_ALEN);
  905. if (hapd->conf->test_socket) {
  906. if (strlen(hapd->conf->test_socket) >=
  907. sizeof(addr_un.sun_path)) {
  908. printf("Too long test_socket path\n");
  909. test_driver_free_priv(drv);
  910. return NULL;
  911. }
  912. if (strncmp(hapd->conf->test_socket, "DIR:", 4) == 0) {
  913. size_t len = strlen(hapd->conf->test_socket) + 30;
  914. drv->socket_dir = strdup(hapd->conf->test_socket + 4);
  915. drv->own_socket_path = malloc(len);
  916. if (drv->own_socket_path) {
  917. snprintf(drv->own_socket_path, len,
  918. "%s/AP-" MACSTR,
  919. hapd->conf->test_socket + 4,
  920. MAC2STR(hapd->own_addr));
  921. }
  922. } else if (strncmp(hapd->conf->test_socket, "UDP:", 4) == 0) {
  923. drv->udp_port = atoi(hapd->conf->test_socket + 4);
  924. } else {
  925. drv->own_socket_path = strdup(hapd->conf->test_socket);
  926. }
  927. if (drv->own_socket_path == NULL && drv->udp_port == 0) {
  928. test_driver_free_priv(drv);
  929. return NULL;
  930. }
  931. drv->test_socket = socket(drv->udp_port ? PF_INET : PF_UNIX,
  932. SOCK_DGRAM, 0);
  933. if (drv->test_socket < 0) {
  934. perror("socket");
  935. test_driver_free_priv(drv);
  936. return NULL;
  937. }
  938. if (drv->udp_port) {
  939. os_memset(&addr_in, 0, sizeof(addr_in));
  940. addr_in.sin_family = AF_INET;
  941. addr_in.sin_port = htons(drv->udp_port);
  942. addr = (struct sockaddr *) &addr_in;
  943. alen = sizeof(addr_in);
  944. } else {
  945. os_memset(&addr_un, 0, sizeof(addr_un));
  946. addr_un.sun_family = AF_UNIX;
  947. os_strlcpy(addr_un.sun_path, drv->own_socket_path,
  948. sizeof(addr_un.sun_path));
  949. addr = (struct sockaddr *) &addr_un;
  950. alen = sizeof(addr_un);
  951. }
  952. if (bind(drv->test_socket, addr, alen) < 0) {
  953. perror("bind(PF_UNIX)");
  954. close(drv->test_socket);
  955. if (drv->own_socket_path)
  956. unlink(drv->own_socket_path);
  957. test_driver_free_priv(drv);
  958. return NULL;
  959. }
  960. eloop_register_read_sock(drv->test_socket,
  961. test_driver_receive_unix, drv, NULL);
  962. } else
  963. drv->test_socket = -1;
  964. return drv;
  965. }
  966. static void test_driver_deinit(void *priv)
  967. {
  968. struct test_driver_data *drv = priv;
  969. struct test_client_socket *cli, *prev;
  970. cli = drv->cli;
  971. while (cli) {
  972. prev = cli;
  973. cli = cli->next;
  974. free(prev);
  975. }
  976. if (drv->test_socket >= 0) {
  977. eloop_unregister_read_sock(drv->test_socket);
  978. close(drv->test_socket);
  979. if (drv->own_socket_path)
  980. unlink(drv->own_socket_path);
  981. }
  982. /* There should be only one BSS remaining at this point. */
  983. if (drv->bss == NULL)
  984. wpa_printf(MSG_ERROR, "%s: drv->bss == NULL", __func__);
  985. else if (drv->bss->next)
  986. wpa_printf(MSG_ERROR, "%s: drv->bss->next != NULL", __func__);
  987. test_driver_free_priv(drv);
  988. }
  989. const struct wpa_driver_ops wpa_driver_test_ops = {
  990. .name = "test",
  991. .init = test_driver_init,
  992. .deinit = test_driver_deinit,
  993. .send_eapol = test_driver_send_eapol,
  994. .send_mgmt_frame = test_driver_send_mgmt_frame,
  995. .set_generic_elem = test_driver_set_generic_elem,
  996. .sta_deauth = test_driver_sta_deauth,
  997. .sta_disassoc = test_driver_sta_disassoc,
  998. .get_hw_feature_data = test_driver_get_hw_feature_data,
  999. .bss_add = test_driver_bss_add,
  1000. .bss_remove = test_driver_bss_remove,
  1001. .if_add = test_driver_if_add,
  1002. .if_update = test_driver_if_update,
  1003. .if_remove = test_driver_if_remove,
  1004. .valid_bss_mask = test_driver_valid_bss_mask,
  1005. .set_ssid = test_driver_set_ssid,
  1006. .set_privacy = test_driver_set_privacy,
  1007. .set_encryption = test_driver_set_encryption,
  1008. .set_sta_vlan = test_driver_set_sta_vlan,
  1009. .sta_add = test_driver_sta_add,
  1010. .send_ether = test_driver_send_ether,
  1011. .set_wps_beacon_ie = test_driver_set_wps_beacon_ie,
  1012. .set_wps_probe_resp_ie = test_driver_set_wps_probe_resp_ie,
  1013. };