driver_test.c 30 KB

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