driver_test.c 30 KB

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