driver_test.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167
  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. accounting_sta_get_id(hapd, sta);
  346. if (hapd->conf->wpa) {
  347. if (ie == NULL || ielen == 0) {
  348. printf("test_driver: no IE from STA\n");
  349. return -1;
  350. }
  351. if (sta->wpa_sm == NULL)
  352. sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
  353. sta->addr);
  354. if (sta->wpa_sm == NULL) {
  355. printf("test_driver: Failed to initialize WPA state "
  356. "machine\n");
  357. return -1;
  358. }
  359. res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
  360. ie, ielen, NULL, 0);
  361. if (res != WPA_IE_OK) {
  362. printf("WPA/RSN information element rejected? "
  363. "(res %u)\n", res);
  364. return -1;
  365. }
  366. }
  367. new_assoc = (sta->flags & WLAN_STA_ASSOC) == 0;
  368. sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
  369. wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
  370. hostapd_new_assoc_sta(hapd, sta, !new_assoc);
  371. ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
  372. return 0;
  373. }
  374. static void test_driver_assoc(struct test_driver_data *drv,
  375. struct sockaddr_un *from, socklen_t fromlen,
  376. char *data)
  377. {
  378. struct test_client_socket *cli;
  379. u8 ie[256], ssid[32];
  380. size_t ielen, ssid_len = 0;
  381. char *pos, *pos2, cmd[50];
  382. struct test_driver_bss *bss;
  383. /* data: STA-addr SSID(hex) IEs(hex) */
  384. cli = os_zalloc(sizeof(*cli));
  385. if (cli == NULL)
  386. return;
  387. if (hwaddr_aton(data, cli->addr)) {
  388. printf("test_socket: Invalid MAC address '%s' in ASSOC\n",
  389. data);
  390. free(cli);
  391. return;
  392. }
  393. pos = data + 17;
  394. while (*pos == ' ')
  395. pos++;
  396. pos2 = strchr(pos, ' ');
  397. ielen = 0;
  398. if (pos2) {
  399. ssid_len = (pos2 - pos) / 2;
  400. if (hexstr2bin(pos, ssid, ssid_len) < 0) {
  401. wpa_printf(MSG_DEBUG, "%s: Invalid SSID", __func__);
  402. free(cli);
  403. return;
  404. }
  405. wpa_hexdump_ascii(MSG_DEBUG, "test_driver_assoc: SSID",
  406. ssid, ssid_len);
  407. pos = pos2 + 1;
  408. ielen = strlen(pos) / 2;
  409. if (ielen > sizeof(ie))
  410. ielen = sizeof(ie);
  411. if (hexstr2bin(pos, ie, ielen) < 0)
  412. ielen = 0;
  413. }
  414. for (bss = drv->bss; bss; bss = bss->next) {
  415. if (bss->ssid_len == ssid_len &&
  416. memcmp(bss->ssid, ssid, ssid_len) == 0)
  417. break;
  418. }
  419. if (bss == NULL) {
  420. wpa_printf(MSG_DEBUG, "%s: No matching SSID found from "
  421. "configured BSSes", __func__);
  422. free(cli);
  423. return;
  424. }
  425. cli->bss = bss;
  426. memcpy(&cli->un, from, sizeof(cli->un));
  427. cli->unlen = fromlen;
  428. cli->next = drv->cli;
  429. drv->cli = cli;
  430. wpa_hexdump_ascii(MSG_DEBUG, "test_socket: ASSOC sun_path",
  431. (const u8 *) cli->un.sun_path,
  432. cli->unlen - sizeof(cli->un.sun_family));
  433. snprintf(cmd, sizeof(cmd), "ASSOCRESP " MACSTR " 0",
  434. MAC2STR(bss->bssid));
  435. sendto(drv->test_socket, cmd, strlen(cmd), 0,
  436. (struct sockaddr *) from, fromlen);
  437. if (test_driver_new_sta(drv, bss, cli->addr, ie, ielen) < 0) {
  438. wpa_printf(MSG_DEBUG, "test_driver: failed to add new STA");
  439. }
  440. }
  441. static void test_driver_disassoc(struct test_driver_data *drv,
  442. struct sockaddr_un *from, socklen_t fromlen)
  443. {
  444. struct test_client_socket *cli;
  445. struct sta_info *sta;
  446. cli = test_driver_get_cli(drv, from, fromlen);
  447. if (!cli)
  448. return;
  449. hostapd_logger(drv->hapd, cli->addr, HOSTAPD_MODULE_IEEE80211,
  450. HOSTAPD_LEVEL_INFO, "disassociated");
  451. sta = ap_get_sta(drv->hapd, cli->addr);
  452. if (sta != NULL) {
  453. sta->flags &= ~WLAN_STA_ASSOC;
  454. wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
  455. sta->acct_terminate_cause =
  456. RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
  457. ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
  458. ap_free_sta(drv->hapd, sta);
  459. }
  460. }
  461. static void test_driver_eapol(struct test_driver_data *drv,
  462. struct sockaddr_un *from, socklen_t fromlen,
  463. u8 *data, size_t datalen)
  464. {
  465. struct test_client_socket *cli;
  466. if (datalen > 14) {
  467. u8 *proto = data + 2 * ETH_ALEN;
  468. /* Skip Ethernet header */
  469. wpa_printf(MSG_DEBUG, "test_driver: dst=" MACSTR " src="
  470. MACSTR " proto=%04x",
  471. MAC2STR(data), MAC2STR(data + ETH_ALEN),
  472. WPA_GET_BE16(proto));
  473. data += 14;
  474. datalen -= 14;
  475. }
  476. cli = test_driver_get_cli(drv, from, fromlen);
  477. if (cli) {
  478. struct hostapd_data *hapd;
  479. hapd = test_driver_get_hapd(drv, cli->bss);
  480. if (hapd == NULL)
  481. return;
  482. ieee802_1x_receive(hapd, cli->addr, data, datalen);
  483. } else {
  484. wpa_printf(MSG_DEBUG, "test_socket: EAPOL from unknown "
  485. "client");
  486. }
  487. }
  488. static void test_driver_ether(struct test_driver_data *drv,
  489. struct sockaddr_un *from, socklen_t fromlen,
  490. u8 *data, size_t datalen)
  491. {
  492. struct l2_ethhdr *eth;
  493. if (datalen < sizeof(*eth))
  494. return;
  495. eth = (struct l2_ethhdr *) data;
  496. wpa_printf(MSG_DEBUG, "test_driver: RX ETHER dst=" MACSTR " src="
  497. MACSTR " proto=%04x",
  498. MAC2STR(eth->h_dest), MAC2STR(eth->h_source),
  499. be_to_host16(eth->h_proto));
  500. #ifdef CONFIG_IEEE80211R
  501. if (be_to_host16(eth->h_proto) == ETH_P_RRB) {
  502. wpa_ft_rrb_rx(drv->hapd->wpa_auth, eth->h_source,
  503. data + sizeof(*eth), datalen - sizeof(*eth));
  504. }
  505. #endif /* CONFIG_IEEE80211R */
  506. }
  507. static void test_driver_mlme(struct test_driver_data *drv,
  508. struct sockaddr_un *from, socklen_t fromlen,
  509. u8 *data, size_t datalen)
  510. {
  511. struct ieee80211_hdr *hdr;
  512. u16 fc;
  513. hdr = (struct ieee80211_hdr *) data;
  514. if (test_driver_get_cli(drv, from, fromlen) == NULL && datalen >= 16) {
  515. struct test_client_socket *cli;
  516. cli = os_zalloc(sizeof(*cli));
  517. if (cli == NULL)
  518. return;
  519. wpa_printf(MSG_DEBUG, "Adding client entry for " MACSTR,
  520. MAC2STR(hdr->addr2));
  521. memcpy(cli->addr, hdr->addr2, ETH_ALEN);
  522. memcpy(&cli->un, from, sizeof(cli->un));
  523. cli->unlen = fromlen;
  524. cli->next = drv->cli;
  525. drv->cli = cli;
  526. }
  527. wpa_hexdump(MSG_MSGDUMP, "test_driver_mlme: received frame",
  528. data, datalen);
  529. fc = le_to_host16(hdr->frame_control);
  530. if (WLAN_FC_GET_TYPE(fc) != WLAN_FC_TYPE_MGMT) {
  531. wpa_printf(MSG_ERROR, "%s: received non-mgmt frame",
  532. __func__);
  533. return;
  534. }
  535. ieee802_11_mgmt(drv->hapd, data, datalen, WLAN_FC_GET_STYPE(fc), NULL);
  536. }
  537. static void test_driver_receive_unix(int sock, void *eloop_ctx, void *sock_ctx)
  538. {
  539. struct test_driver_data *drv = eloop_ctx;
  540. char buf[2000];
  541. int res;
  542. struct sockaddr_un from;
  543. socklen_t fromlen = sizeof(from);
  544. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  545. (struct sockaddr *) &from, &fromlen);
  546. if (res < 0) {
  547. perror("recvfrom(test_socket)");
  548. return;
  549. }
  550. buf[res] = '\0';
  551. wpa_printf(MSG_DEBUG, "test_driver: received %u bytes", res);
  552. if (strcmp(buf, "SCAN") == 0) {
  553. test_driver_scan(drv, &from, fromlen);
  554. } else if (strncmp(buf, "ASSOC ", 6) == 0) {
  555. test_driver_assoc(drv, &from, fromlen, buf + 6);
  556. } else if (strcmp(buf, "DISASSOC") == 0) {
  557. test_driver_disassoc(drv, &from, fromlen);
  558. } else if (strncmp(buf, "EAPOL ", 6) == 0) {
  559. test_driver_eapol(drv, &from, fromlen, (u8 *) buf + 6,
  560. res - 6);
  561. } else if (strncmp(buf, "ETHER ", 6) == 0) {
  562. test_driver_ether(drv, &from, fromlen, (u8 *) buf + 6,
  563. res - 6);
  564. } else if (strncmp(buf, "MLME ", 5) == 0) {
  565. test_driver_mlme(drv, &from, fromlen, (u8 *) buf + 5, res - 5);
  566. } else {
  567. wpa_hexdump_ascii(MSG_DEBUG, "Unknown test_socket command",
  568. (u8 *) buf, res);
  569. }
  570. }
  571. static struct test_driver_bss *
  572. test_driver_get_bss(struct test_driver_data *drv, const char *ifname)
  573. {
  574. struct test_driver_bss *bss;
  575. for (bss = drv->bss; bss; bss = bss->next) {
  576. if (strcmp(bss->ifname, ifname) == 0)
  577. return bss;
  578. }
  579. return NULL;
  580. }
  581. static int test_driver_set_generic_elem(const char *ifname, void *priv,
  582. const u8 *elem, size_t elem_len)
  583. {
  584. struct test_driver_data *drv = priv;
  585. struct test_driver_bss *bss;
  586. bss = test_driver_get_bss(drv, ifname);
  587. if (bss == NULL)
  588. return -1;
  589. free(bss->ie);
  590. if (elem == NULL) {
  591. bss->ie = NULL;
  592. bss->ielen = 0;
  593. return 0;
  594. }
  595. bss->ie = malloc(elem_len);
  596. if (bss->ie == NULL) {
  597. bss->ielen = 0;
  598. return -1;
  599. }
  600. memcpy(bss->ie, elem, elem_len);
  601. bss->ielen = elem_len;
  602. return 0;
  603. }
  604. static int test_driver_sta_deauth(void *priv, const u8 *addr, int reason)
  605. {
  606. struct test_driver_data *drv = priv;
  607. struct test_client_socket *cli;
  608. if (drv->test_socket < 0)
  609. return -1;
  610. cli = drv->cli;
  611. while (cli) {
  612. if (memcmp(cli->addr, addr, ETH_ALEN) == 0)
  613. break;
  614. cli = cli->next;
  615. }
  616. if (!cli)
  617. return -1;
  618. return sendto(drv->test_socket, "DEAUTH", 6, 0,
  619. (struct sockaddr *) &cli->un, cli->unlen);
  620. }
  621. static int test_driver_sta_disassoc(void *priv, const u8 *addr, int reason)
  622. {
  623. struct test_driver_data *drv = priv;
  624. struct test_client_socket *cli;
  625. if (drv->test_socket < 0)
  626. return -1;
  627. cli = drv->cli;
  628. while (cli) {
  629. if (memcmp(cli->addr, addr, ETH_ALEN) == 0)
  630. break;
  631. cli = cli->next;
  632. }
  633. if (!cli)
  634. return -1;
  635. return sendto(drv->test_socket, "DISASSOC", 8, 0,
  636. (struct sockaddr *) &cli->un, cli->unlen);
  637. }
  638. static struct hostapd_hw_modes *
  639. test_driver_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags)
  640. {
  641. struct hostapd_hw_modes *modes;
  642. *num_modes = 3;
  643. *flags = 0;
  644. modes = os_zalloc(*num_modes * sizeof(struct hostapd_hw_modes));
  645. if (modes == NULL)
  646. return NULL;
  647. modes[0].mode = HOSTAPD_MODE_IEEE80211G;
  648. modes[0].num_channels = 1;
  649. modes[0].num_rates = 1;
  650. modes[0].channels = os_zalloc(sizeof(struct hostapd_channel_data));
  651. modes[0].rates = os_zalloc(sizeof(struct hostapd_rate_data));
  652. if (modes[0].channels == NULL || modes[0].rates == NULL) {
  653. hostapd_free_hw_features(modes, *num_modes);
  654. return NULL;
  655. }
  656. modes[0].channels[0].chan = 1;
  657. modes[0].channels[0].freq = 2412;
  658. modes[0].channels[0].flag = HOSTAPD_CHAN_W_SCAN |
  659. HOSTAPD_CHAN_W_ACTIVE_SCAN;
  660. modes[0].rates[0].rate = 10;
  661. modes[0].rates[0].flags = HOSTAPD_RATE_BASIC | HOSTAPD_RATE_SUPPORTED |
  662. HOSTAPD_RATE_CCK | HOSTAPD_RATE_MANDATORY;
  663. modes[1].mode = HOSTAPD_MODE_IEEE80211B;
  664. modes[1].num_channels = 1;
  665. modes[1].num_rates = 1;
  666. modes[1].channels = os_zalloc(sizeof(struct hostapd_channel_data));
  667. modes[1].rates = os_zalloc(sizeof(struct hostapd_rate_data));
  668. if (modes[1].channels == NULL || modes[1].rates == NULL) {
  669. hostapd_free_hw_features(modes, *num_modes);
  670. return NULL;
  671. }
  672. modes[1].channels[0].chan = 1;
  673. modes[1].channels[0].freq = 2412;
  674. modes[1].channels[0].flag = HOSTAPD_CHAN_W_SCAN |
  675. HOSTAPD_CHAN_W_ACTIVE_SCAN;
  676. modes[1].rates[0].rate = 10;
  677. modes[1].rates[0].flags = HOSTAPD_RATE_BASIC | HOSTAPD_RATE_SUPPORTED |
  678. HOSTAPD_RATE_CCK | HOSTAPD_RATE_MANDATORY;
  679. modes[2].mode = HOSTAPD_MODE_IEEE80211A;
  680. modes[2].num_channels = 1;
  681. modes[2].num_rates = 1;
  682. modes[2].channels = os_zalloc(sizeof(struct hostapd_channel_data));
  683. modes[2].rates = os_zalloc(sizeof(struct hostapd_rate_data));
  684. if (modes[2].channels == NULL || modes[2].rates == NULL) {
  685. hostapd_free_hw_features(modes, *num_modes);
  686. return NULL;
  687. }
  688. modes[2].channels[0].chan = 60;
  689. modes[2].channels[0].freq = 5300;
  690. modes[2].channels[0].flag = HOSTAPD_CHAN_W_SCAN |
  691. HOSTAPD_CHAN_W_ACTIVE_SCAN;
  692. modes[2].rates[0].rate = 60;
  693. modes[2].rates[0].flags = HOSTAPD_RATE_BASIC | HOSTAPD_RATE_SUPPORTED |
  694. HOSTAPD_RATE_MANDATORY;
  695. return modes;
  696. }
  697. static int test_driver_bss_add(void *priv, const char *ifname, const u8 *bssid)
  698. {
  699. struct test_driver_data *drv = priv;
  700. struct test_driver_bss *bss;
  701. wpa_printf(MSG_DEBUG, "%s(ifname=%s bssid=" MACSTR ")",
  702. __func__, ifname, MAC2STR(bssid));
  703. bss = os_zalloc(sizeof(*bss));
  704. if (bss == NULL)
  705. return -1;
  706. os_strlcpy(bss->ifname, ifname, IFNAMSIZ);
  707. memcpy(bss->bssid, bssid, ETH_ALEN);
  708. bss->next = drv->bss;
  709. drv->bss = bss;
  710. return 0;
  711. }
  712. static int test_driver_bss_remove(void *priv, const char *ifname)
  713. {
  714. struct test_driver_data *drv = priv;
  715. struct test_driver_bss *bss, *prev;
  716. struct test_client_socket *cli, *prev_c;
  717. wpa_printf(MSG_DEBUG, "%s(ifname=%s)", __func__, ifname);
  718. for (prev = NULL, bss = drv->bss; bss; prev = bss, bss = bss->next) {
  719. if (strcmp(bss->ifname, ifname) != 0)
  720. continue;
  721. if (prev)
  722. prev->next = bss->next;
  723. else
  724. drv->bss = bss->next;
  725. for (prev_c = NULL, cli = drv->cli; cli;
  726. prev_c = cli, cli = cli->next) {
  727. if (cli->bss != bss)
  728. continue;
  729. if (prev_c)
  730. prev_c->next = cli->next;
  731. else
  732. drv->cli = cli->next;
  733. free(cli);
  734. break;
  735. }
  736. test_driver_free_bss(bss);
  737. return 0;
  738. }
  739. return -1;
  740. }
  741. static int test_driver_if_add(const char *iface, void *priv,
  742. enum hostapd_driver_if_type type, char *ifname,
  743. const u8 *addr)
  744. {
  745. wpa_printf(MSG_DEBUG, "%s(iface=%s type=%d ifname=%s)",
  746. __func__, iface, type, ifname);
  747. return 0;
  748. }
  749. static int test_driver_if_update(void *priv, enum hostapd_driver_if_type type,
  750. char *ifname, const u8 *addr)
  751. {
  752. wpa_printf(MSG_DEBUG, "%s(type=%d ifname=%s)", __func__, type, ifname);
  753. return 0;
  754. }
  755. static int test_driver_if_remove(void *priv, enum hostapd_driver_if_type type,
  756. const char *ifname, const u8 *addr)
  757. {
  758. wpa_printf(MSG_DEBUG, "%s(type=%d ifname=%s)", __func__, type, ifname);
  759. return 0;
  760. }
  761. static int test_driver_valid_bss_mask(void *priv, const u8 *addr,
  762. const u8 *mask)
  763. {
  764. return 0;
  765. }
  766. static int test_driver_set_ssid(const char *ifname, void *priv, const u8 *buf,
  767. int len)
  768. {
  769. struct test_driver_data *drv = priv;
  770. struct test_driver_bss *bss;
  771. wpa_printf(MSG_DEBUG, "%s(ifname=%s)", __func__, ifname);
  772. wpa_hexdump_ascii(MSG_DEBUG, "test_driver_set_ssid: SSID", buf, len);
  773. for (bss = drv->bss; bss; bss = bss->next) {
  774. if (strcmp(bss->ifname, ifname) != 0)
  775. continue;
  776. if (len < 0 || (size_t) len > sizeof(bss->ssid))
  777. return -1;
  778. memcpy(bss->ssid, buf, len);
  779. bss->ssid_len = len;
  780. return 0;
  781. }
  782. return -1;
  783. }
  784. static int test_driver_set_privacy(const char *ifname, void *priv, int enabled)
  785. {
  786. struct test_driver_data *drv = priv;
  787. struct test_driver_bss *bss;
  788. wpa_printf(MSG_DEBUG, "%s(ifname=%s enabled=%d)",
  789. __func__, ifname, enabled);
  790. for (bss = drv->bss; bss; bss = bss->next) {
  791. if (strcmp(bss->ifname, ifname) != 0)
  792. continue;
  793. bss->privacy = enabled;
  794. return 0;
  795. }
  796. return -1;
  797. }
  798. static int test_driver_set_encryption(const char *iface, void *priv,
  799. const char *alg, const u8 *addr, int idx,
  800. const u8 *key, size_t key_len, int txkey)
  801. {
  802. wpa_printf(MSG_DEBUG, "%s(iface=%s alg=%s idx=%d txkey=%d)",
  803. __func__, iface, alg, idx, txkey);
  804. if (addr)
  805. wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
  806. if (key)
  807. wpa_hexdump_key(MSG_DEBUG, " key", key, key_len);
  808. return 0;
  809. }
  810. static int test_driver_set_sta_vlan(void *priv, const u8 *addr,
  811. const char *ifname, int vlan_id)
  812. {
  813. wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " ifname=%s vlan_id=%d)",
  814. __func__, MAC2STR(addr), ifname, vlan_id);
  815. return 0;
  816. }
  817. static int test_driver_sta_add(const char *ifname, void *priv, const u8 *addr,
  818. u16 aid, u16 capability, u8 *supp_rates,
  819. size_t supp_rates_len, int flags)
  820. {
  821. struct test_driver_data *drv = priv;
  822. struct test_client_socket *cli;
  823. struct test_driver_bss *bss;
  824. wpa_printf(MSG_DEBUG, "%s(ifname=%s addr=" MACSTR " aid=%d "
  825. "capability=0x%x flags=0x%x",
  826. __func__, ifname, MAC2STR(addr), aid, capability, flags);
  827. wpa_hexdump(MSG_DEBUG, "test_driver_sta_add - supp_rates",
  828. supp_rates, supp_rates_len);
  829. cli = drv->cli;
  830. while (cli) {
  831. if (memcmp(cli->addr, addr, ETH_ALEN) == 0)
  832. break;
  833. cli = cli->next;
  834. }
  835. if (!cli) {
  836. wpa_printf(MSG_DEBUG, "%s: no matching client entry",
  837. __func__);
  838. return -1;
  839. }
  840. for (bss = drv->bss; bss; bss = bss->next) {
  841. if (strcmp(ifname, bss->ifname) == 0)
  842. break;
  843. }
  844. if (bss == NULL) {
  845. wpa_printf(MSG_DEBUG, "%s: No matching interface found from "
  846. "configured BSSes", __func__);
  847. return -1;
  848. }
  849. cli->bss = bss;
  850. return 0;
  851. }
  852. static void * test_driver_init(struct hostapd_data *hapd)
  853. {
  854. struct test_driver_data *drv;
  855. struct sockaddr_un addr;
  856. drv = os_zalloc(sizeof(struct test_driver_data));
  857. if (drv == NULL) {
  858. printf("Could not allocate memory for test driver data\n");
  859. return NULL;
  860. }
  861. drv->bss = os_zalloc(sizeof(*drv->bss));
  862. if (drv->bss == NULL) {
  863. printf("Could not allocate memory for test driver BSS data\n");
  864. free(drv);
  865. return NULL;
  866. }
  867. drv->hapd = hapd;
  868. /* Generate a MAC address to help testing with multiple APs */
  869. hapd->own_addr[0] = 0x02; /* locally administered */
  870. sha1_prf((const u8 *) hapd->conf->iface, strlen(hapd->conf->iface),
  871. "hostapd test bssid generation",
  872. (const u8 *) hapd->conf->ssid.ssid, hapd->conf->ssid.ssid_len,
  873. hapd->own_addr + 1, ETH_ALEN - 1);
  874. os_strlcpy(drv->bss->ifname, hapd->conf->iface, IFNAMSIZ);
  875. memcpy(drv->bss->bssid, hapd->own_addr, ETH_ALEN);
  876. if (hapd->conf->test_socket) {
  877. if (strlen(hapd->conf->test_socket) >= sizeof(addr.sun_path)) {
  878. printf("Too long test_socket path\n");
  879. test_driver_free_priv(drv);
  880. return NULL;
  881. }
  882. if (strncmp(hapd->conf->test_socket, "DIR:", 4) == 0) {
  883. size_t len = strlen(hapd->conf->test_socket) + 30;
  884. drv->socket_dir = strdup(hapd->conf->test_socket + 4);
  885. drv->own_socket_path = malloc(len);
  886. if (drv->own_socket_path) {
  887. snprintf(drv->own_socket_path, len,
  888. "%s/AP-" MACSTR,
  889. hapd->conf->test_socket + 4,
  890. MAC2STR(hapd->own_addr));
  891. }
  892. } else {
  893. drv->own_socket_path = strdup(hapd->conf->test_socket);
  894. }
  895. if (drv->own_socket_path == NULL) {
  896. test_driver_free_priv(drv);
  897. return NULL;
  898. }
  899. drv->test_socket = socket(PF_UNIX, SOCK_DGRAM, 0);
  900. if (drv->test_socket < 0) {
  901. perror("socket(PF_UNIX)");
  902. test_driver_free_priv(drv);
  903. return NULL;
  904. }
  905. memset(&addr, 0, sizeof(addr));
  906. addr.sun_family = AF_UNIX;
  907. os_strlcpy(addr.sun_path, drv->own_socket_path,
  908. sizeof(addr.sun_path));
  909. if (bind(drv->test_socket, (struct sockaddr *) &addr,
  910. sizeof(addr)) < 0) {
  911. perror("bind(PF_UNIX)");
  912. close(drv->test_socket);
  913. unlink(drv->own_socket_path);
  914. test_driver_free_priv(drv);
  915. return NULL;
  916. }
  917. eloop_register_read_sock(drv->test_socket,
  918. test_driver_receive_unix, drv, NULL);
  919. } else
  920. drv->test_socket = -1;
  921. return drv;
  922. }
  923. static void test_driver_deinit(void *priv)
  924. {
  925. struct test_driver_data *drv = priv;
  926. struct test_client_socket *cli, *prev;
  927. cli = drv->cli;
  928. while (cli) {
  929. prev = cli;
  930. cli = cli->next;
  931. free(prev);
  932. }
  933. if (drv->test_socket >= 0) {
  934. eloop_unregister_read_sock(drv->test_socket);
  935. close(drv->test_socket);
  936. unlink(drv->own_socket_path);
  937. }
  938. /* There should be only one BSS remaining at this point. */
  939. if (drv->bss == NULL)
  940. wpa_printf(MSG_ERROR, "%s: drv->bss == NULL", __func__);
  941. else if (drv->bss->next)
  942. wpa_printf(MSG_ERROR, "%s: drv->bss->next != NULL", __func__);
  943. test_driver_free_priv(drv);
  944. }
  945. const struct wpa_driver_ops wpa_driver_test_ops = {
  946. .name = "test",
  947. .init = test_driver_init,
  948. .deinit = test_driver_deinit,
  949. .send_eapol = test_driver_send_eapol,
  950. .send_mgmt_frame = test_driver_send_mgmt_frame,
  951. .set_generic_elem = test_driver_set_generic_elem,
  952. .sta_deauth = test_driver_sta_deauth,
  953. .sta_disassoc = test_driver_sta_disassoc,
  954. .get_hw_feature_data = test_driver_get_hw_feature_data,
  955. .bss_add = test_driver_bss_add,
  956. .bss_remove = test_driver_bss_remove,
  957. .if_add = test_driver_if_add,
  958. .if_update = test_driver_if_update,
  959. .if_remove = test_driver_if_remove,
  960. .valid_bss_mask = test_driver_valid_bss_mask,
  961. .set_ssid = test_driver_set_ssid,
  962. .set_privacy = test_driver_set_privacy,
  963. .set_encryption = test_driver_set_encryption,
  964. .set_sta_vlan = test_driver_set_sta_vlan,
  965. .sta_add = test_driver_sta_add,
  966. .send_ether = test_driver_send_ether,
  967. };