driver_test.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169
  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. u16 listen_interval)
  821. {
  822. struct test_driver_data *drv = priv;
  823. struct test_client_socket *cli;
  824. struct test_driver_bss *bss;
  825. wpa_printf(MSG_DEBUG, "%s(ifname=%s addr=" MACSTR " aid=%d "
  826. "capability=0x%x flags=0x%x listen_interval=%d)",
  827. __func__, ifname, MAC2STR(addr), aid, capability, flags,
  828. listen_interval);
  829. wpa_hexdump(MSG_DEBUG, "test_driver_sta_add - supp_rates",
  830. supp_rates, supp_rates_len);
  831. cli = drv->cli;
  832. while (cli) {
  833. if (memcmp(cli->addr, addr, ETH_ALEN) == 0)
  834. break;
  835. cli = cli->next;
  836. }
  837. if (!cli) {
  838. wpa_printf(MSG_DEBUG, "%s: no matching client entry",
  839. __func__);
  840. return -1;
  841. }
  842. for (bss = drv->bss; bss; bss = bss->next) {
  843. if (strcmp(ifname, bss->ifname) == 0)
  844. break;
  845. }
  846. if (bss == NULL) {
  847. wpa_printf(MSG_DEBUG, "%s: No matching interface found from "
  848. "configured BSSes", __func__);
  849. return -1;
  850. }
  851. cli->bss = bss;
  852. return 0;
  853. }
  854. static void * test_driver_init(struct hostapd_data *hapd)
  855. {
  856. struct test_driver_data *drv;
  857. struct sockaddr_un addr;
  858. drv = os_zalloc(sizeof(struct test_driver_data));
  859. if (drv == NULL) {
  860. printf("Could not allocate memory for test driver data\n");
  861. return NULL;
  862. }
  863. drv->bss = os_zalloc(sizeof(*drv->bss));
  864. if (drv->bss == NULL) {
  865. printf("Could not allocate memory for test driver BSS data\n");
  866. free(drv);
  867. return NULL;
  868. }
  869. drv->hapd = hapd;
  870. /* Generate a MAC address to help testing with multiple APs */
  871. hapd->own_addr[0] = 0x02; /* locally administered */
  872. sha1_prf((const u8 *) hapd->conf->iface, strlen(hapd->conf->iface),
  873. "hostapd test bssid generation",
  874. (const u8 *) hapd->conf->ssid.ssid, hapd->conf->ssid.ssid_len,
  875. hapd->own_addr + 1, ETH_ALEN - 1);
  876. os_strlcpy(drv->bss->ifname, hapd->conf->iface, IFNAMSIZ);
  877. memcpy(drv->bss->bssid, hapd->own_addr, ETH_ALEN);
  878. if (hapd->conf->test_socket) {
  879. if (strlen(hapd->conf->test_socket) >= sizeof(addr.sun_path)) {
  880. printf("Too long test_socket path\n");
  881. test_driver_free_priv(drv);
  882. return NULL;
  883. }
  884. if (strncmp(hapd->conf->test_socket, "DIR:", 4) == 0) {
  885. size_t len = strlen(hapd->conf->test_socket) + 30;
  886. drv->socket_dir = strdup(hapd->conf->test_socket + 4);
  887. drv->own_socket_path = malloc(len);
  888. if (drv->own_socket_path) {
  889. snprintf(drv->own_socket_path, len,
  890. "%s/AP-" MACSTR,
  891. hapd->conf->test_socket + 4,
  892. MAC2STR(hapd->own_addr));
  893. }
  894. } else {
  895. drv->own_socket_path = strdup(hapd->conf->test_socket);
  896. }
  897. if (drv->own_socket_path == NULL) {
  898. test_driver_free_priv(drv);
  899. return NULL;
  900. }
  901. drv->test_socket = socket(PF_UNIX, SOCK_DGRAM, 0);
  902. if (drv->test_socket < 0) {
  903. perror("socket(PF_UNIX)");
  904. test_driver_free_priv(drv);
  905. return NULL;
  906. }
  907. memset(&addr, 0, sizeof(addr));
  908. addr.sun_family = AF_UNIX;
  909. os_strlcpy(addr.sun_path, drv->own_socket_path,
  910. sizeof(addr.sun_path));
  911. if (bind(drv->test_socket, (struct sockaddr *) &addr,
  912. sizeof(addr)) < 0) {
  913. perror("bind(PF_UNIX)");
  914. close(drv->test_socket);
  915. unlink(drv->own_socket_path);
  916. test_driver_free_priv(drv);
  917. return NULL;
  918. }
  919. eloop_register_read_sock(drv->test_socket,
  920. test_driver_receive_unix, drv, NULL);
  921. } else
  922. drv->test_socket = -1;
  923. return drv;
  924. }
  925. static void test_driver_deinit(void *priv)
  926. {
  927. struct test_driver_data *drv = priv;
  928. struct test_client_socket *cli, *prev;
  929. cli = drv->cli;
  930. while (cli) {
  931. prev = cli;
  932. cli = cli->next;
  933. free(prev);
  934. }
  935. if (drv->test_socket >= 0) {
  936. eloop_unregister_read_sock(drv->test_socket);
  937. close(drv->test_socket);
  938. unlink(drv->own_socket_path);
  939. }
  940. /* There should be only one BSS remaining at this point. */
  941. if (drv->bss == NULL)
  942. wpa_printf(MSG_ERROR, "%s: drv->bss == NULL", __func__);
  943. else if (drv->bss->next)
  944. wpa_printf(MSG_ERROR, "%s: drv->bss->next != NULL", __func__);
  945. test_driver_free_priv(drv);
  946. }
  947. const struct wpa_driver_ops wpa_driver_test_ops = {
  948. .name = "test",
  949. .init = test_driver_init,
  950. .deinit = test_driver_deinit,
  951. .send_eapol = test_driver_send_eapol,
  952. .send_mgmt_frame = test_driver_send_mgmt_frame,
  953. .set_generic_elem = test_driver_set_generic_elem,
  954. .sta_deauth = test_driver_sta_deauth,
  955. .sta_disassoc = test_driver_sta_disassoc,
  956. .get_hw_feature_data = test_driver_get_hw_feature_data,
  957. .bss_add = test_driver_bss_add,
  958. .bss_remove = test_driver_bss_remove,
  959. .if_add = test_driver_if_add,
  960. .if_update = test_driver_if_update,
  961. .if_remove = test_driver_if_remove,
  962. .valid_bss_mask = test_driver_valid_bss_mask,
  963. .set_ssid = test_driver_set_ssid,
  964. .set_privacy = test_driver_set_privacy,
  965. .set_encryption = test_driver_set_encryption,
  966. .set_sta_vlan = test_driver_set_sta_vlan,
  967. .sta_add = test_driver_sta_add,
  968. .send_ether = test_driver_send_ether,
  969. };