driver_test.c 30 KB

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