driver_test.c 30 KB

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