driver_test.c 28 KB

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