driver_test.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520
  1. /*
  2. * WPA Supplicant - testing driver interface
  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. /* Make sure we get winsock2.h for Windows build to get sockaddr_storage */
  15. #include "build_config.h"
  16. #ifdef CONFIG_NATIVE_WINDOWS
  17. #include <winsock2.h>
  18. #endif /* CONFIG_NATIVE_WINDOWS */
  19. #include "includes.h"
  20. #ifndef CONFIG_NATIVE_WINDOWS
  21. #include <sys/un.h>
  22. #include <dirent.h>
  23. #include <sys/stat.h>
  24. #define DRIVER_TEST_UNIX
  25. #endif /* CONFIG_NATIVE_WINDOWS */
  26. #include "common.h"
  27. #include "driver.h"
  28. #include "l2_packet/l2_packet.h"
  29. #include "eloop.h"
  30. #include "crypto/sha1.h"
  31. #include "common/ieee802_11_defs.h"
  32. #include "../../hostapd/hostapd.h"
  33. #include "../../hostapd/wpa.h"
  34. struct test_client_socket {
  35. struct test_client_socket *next;
  36. u8 addr[ETH_ALEN];
  37. struct sockaddr_un un;
  38. socklen_t unlen;
  39. struct test_driver_bss *bss;
  40. };
  41. struct test_driver_bss {
  42. struct test_driver_bss *next;
  43. char ifname[IFNAMSIZ + 1];
  44. u8 bssid[ETH_ALEN];
  45. u8 *ie;
  46. size_t ielen;
  47. u8 *wps_beacon_ie;
  48. size_t wps_beacon_ie_len;
  49. u8 *wps_probe_resp_ie;
  50. size_t wps_probe_resp_ie_len;
  51. u8 ssid[32];
  52. size_t ssid_len;
  53. int privacy;
  54. };
  55. struct wpa_driver_test_global {
  56. int dummy;
  57. };
  58. struct wpa_driver_test_data {
  59. struct wpa_driver_test_global *global;
  60. void *ctx;
  61. u8 own_addr[ETH_ALEN];
  62. int test_socket;
  63. #ifdef DRIVER_TEST_UNIX
  64. struct sockaddr_un hostapd_addr;
  65. #endif /* DRIVER_TEST_UNIX */
  66. int hostapd_addr_set;
  67. struct sockaddr_in hostapd_addr_udp;
  68. int hostapd_addr_udp_set;
  69. char *own_socket_path;
  70. char *test_dir;
  71. u8 bssid[ETH_ALEN];
  72. u8 ssid[32];
  73. size_t ssid_len;
  74. #define MAX_SCAN_RESULTS 30
  75. struct wpa_scan_res *scanres[MAX_SCAN_RESULTS];
  76. size_t num_scanres;
  77. int use_associnfo;
  78. u8 assoc_wpa_ie[80];
  79. size_t assoc_wpa_ie_len;
  80. int use_mlme;
  81. int associated;
  82. u8 *probe_req_ie;
  83. size_t probe_req_ie_len;
  84. int ibss;
  85. int privacy;
  86. int ap;
  87. struct hostapd_data *hapd;
  88. struct test_client_socket *cli;
  89. struct test_driver_bss *bss;
  90. int udp_port;
  91. };
  92. static void wpa_driver_test_deinit(void *priv);
  93. static int wpa_driver_test_attach(struct wpa_driver_test_data *drv,
  94. const char *dir, int ap);
  95. static void wpa_driver_test_close_test_socket(
  96. struct wpa_driver_test_data *drv);
  97. #ifdef CONFIG_AP
  98. void ap_mgmt_rx(void *ctx, u8 *buf, size_t len, u16 stype,
  99. struct hostapd_frame_info *fi);
  100. void ap_mgmt_tx_cb(void *ctx, u8 *buf, size_t len, u16 stype, int ok);
  101. #else /* CONFIG_AP */
  102. #ifndef HOSTAPD
  103. static inline void ap_mgmt_rx(void *ctx, u8 *buf, size_t len, u16 stype,
  104. struct hostapd_frame_info *fi)
  105. {
  106. }
  107. static inline void ap_mgmt_tx_cb(void *ctx, u8 *buf, size_t len, u16 stype,
  108. int ok)
  109. {
  110. }
  111. #endif /* HOSTAPD */
  112. #endif /* CONFIG_AP */
  113. static void test_driver_free_bss(struct test_driver_bss *bss)
  114. {
  115. free(bss->ie);
  116. free(bss->wps_beacon_ie);
  117. free(bss->wps_probe_resp_ie);
  118. free(bss);
  119. }
  120. static void test_driver_free_bsses(struct wpa_driver_test_data *drv)
  121. {
  122. struct test_driver_bss *bss, *prev_bss;
  123. bss = drv->bss;
  124. while (bss) {
  125. prev_bss = bss;
  126. bss = bss->next;
  127. test_driver_free_bss(prev_bss);
  128. }
  129. drv->bss = NULL;
  130. }
  131. static struct test_client_socket *
  132. test_driver_get_cli(struct wpa_driver_test_data *drv, struct sockaddr_un *from,
  133. socklen_t fromlen)
  134. {
  135. struct test_client_socket *cli = drv->cli;
  136. while (cli) {
  137. if (cli->unlen == fromlen &&
  138. strncmp(cli->un.sun_path, from->sun_path,
  139. fromlen - sizeof(cli->un.sun_family)) == 0)
  140. return cli;
  141. cli = cli->next;
  142. }
  143. return NULL;
  144. }
  145. static int test_driver_send_eapol(void *priv, const u8 *addr, const u8 *data,
  146. size_t data_len, int encrypt,
  147. const u8 *own_addr)
  148. {
  149. struct wpa_driver_test_data *drv = priv;
  150. struct test_client_socket *cli;
  151. struct msghdr msg;
  152. struct iovec io[3];
  153. struct l2_ethhdr eth;
  154. if (drv->test_socket < 0)
  155. return -1;
  156. cli = drv->cli;
  157. while (cli) {
  158. if (memcmp(cli->addr, addr, ETH_ALEN) == 0)
  159. break;
  160. cli = cli->next;
  161. }
  162. if (!cli) {
  163. wpa_printf(MSG_DEBUG, "%s: no destination client entry",
  164. __func__);
  165. return -1;
  166. }
  167. memcpy(eth.h_dest, addr, ETH_ALEN);
  168. memcpy(eth.h_source, own_addr, ETH_ALEN);
  169. eth.h_proto = host_to_be16(ETH_P_EAPOL);
  170. io[0].iov_base = "EAPOL ";
  171. io[0].iov_len = 6;
  172. io[1].iov_base = &eth;
  173. io[1].iov_len = sizeof(eth);
  174. io[2].iov_base = (u8 *) data;
  175. io[2].iov_len = data_len;
  176. memset(&msg, 0, sizeof(msg));
  177. msg.msg_iov = io;
  178. msg.msg_iovlen = 3;
  179. msg.msg_name = &cli->un;
  180. msg.msg_namelen = cli->unlen;
  181. return sendmsg(drv->test_socket, &msg, 0);
  182. }
  183. static int test_driver_send_ether(void *priv, const u8 *dst, const u8 *src,
  184. u16 proto, const u8 *data, size_t data_len)
  185. {
  186. struct wpa_driver_test_data *drv = priv;
  187. struct msghdr msg;
  188. struct iovec io[3];
  189. struct l2_ethhdr eth;
  190. char desttxt[30];
  191. struct sockaddr_un addr;
  192. struct dirent *dent;
  193. DIR *dir;
  194. int ret = 0, broadcast = 0, count = 0;
  195. if (drv->test_socket < 0 || drv->test_dir == NULL) {
  196. wpa_printf(MSG_DEBUG, "%s: invalid parameters (sock=%d "
  197. "test_dir=%p)",
  198. __func__, drv->test_socket, drv->test_dir);
  199. return -1;
  200. }
  201. broadcast = memcmp(dst, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) == 0;
  202. snprintf(desttxt, sizeof(desttxt), MACSTR, MAC2STR(dst));
  203. memcpy(eth.h_dest, dst, ETH_ALEN);
  204. memcpy(eth.h_source, src, ETH_ALEN);
  205. eth.h_proto = host_to_be16(proto);
  206. io[0].iov_base = "ETHER ";
  207. io[0].iov_len = 6;
  208. io[1].iov_base = &eth;
  209. io[1].iov_len = sizeof(eth);
  210. io[2].iov_base = (u8 *) data;
  211. io[2].iov_len = data_len;
  212. memset(&msg, 0, sizeof(msg));
  213. msg.msg_iov = io;
  214. msg.msg_iovlen = 3;
  215. dir = opendir(drv->test_dir);
  216. if (dir == NULL) {
  217. perror("test_driver: opendir");
  218. return -1;
  219. }
  220. while ((dent = readdir(dir))) {
  221. #ifdef _DIRENT_HAVE_D_TYPE
  222. /* Skip the file if it is not a socket. Also accept
  223. * DT_UNKNOWN (0) in case the C library or underlying file
  224. * system does not support d_type. */
  225. if (dent->d_type != DT_SOCK && dent->d_type != DT_UNKNOWN)
  226. continue;
  227. #endif /* _DIRENT_HAVE_D_TYPE */
  228. if (strcmp(dent->d_name, ".") == 0 ||
  229. strcmp(dent->d_name, "..") == 0)
  230. continue;
  231. memset(&addr, 0, sizeof(addr));
  232. addr.sun_family = AF_UNIX;
  233. snprintf(addr.sun_path, sizeof(addr.sun_path), "%s/%s",
  234. drv->test_dir, dent->d_name);
  235. if (strcmp(addr.sun_path, drv->own_socket_path) == 0)
  236. continue;
  237. if (!broadcast && strstr(dent->d_name, desttxt) == NULL)
  238. continue;
  239. wpa_printf(MSG_DEBUG, "%s: Send ether frame to %s",
  240. __func__, dent->d_name);
  241. msg.msg_name = &addr;
  242. msg.msg_namelen = sizeof(addr);
  243. ret = sendmsg(drv->test_socket, &msg, 0);
  244. if (ret < 0)
  245. perror("driver_test: sendmsg");
  246. count++;
  247. }
  248. closedir(dir);
  249. if (!broadcast && count == 0) {
  250. wpa_printf(MSG_DEBUG, "%s: Destination " MACSTR " not found",
  251. __func__, MAC2STR(dst));
  252. return -1;
  253. }
  254. return ret;
  255. }
  256. static int wpa_driver_test_send_mlme(void *priv, const u8 *data,
  257. size_t data_len)
  258. {
  259. struct wpa_driver_test_data *drv = priv;
  260. struct msghdr msg;
  261. struct iovec io[2];
  262. const u8 *dest;
  263. struct sockaddr_un addr;
  264. struct dirent *dent;
  265. DIR *dir;
  266. int broadcast;
  267. int ret = 0;
  268. struct ieee80211_hdr *hdr;
  269. u16 fc;
  270. #ifdef HOSTAPD
  271. char desttxt[30];
  272. #endif /* HOSTAPD */
  273. wpa_hexdump(MSG_MSGDUMP, "test_send_mlme", data, data_len);
  274. if (drv->test_socket < 0 || data_len < 10) {
  275. wpa_printf(MSG_DEBUG, "%s: invalid parameters (sock=%d len=%lu"
  276. " test_dir=%p)",
  277. __func__, drv->test_socket,
  278. (unsigned long) data_len,
  279. drv->test_dir);
  280. return -1;
  281. }
  282. dest = data + 4;
  283. broadcast = os_memcmp(dest, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) == 0;
  284. #ifdef HOSTAPD
  285. snprintf(desttxt, sizeof(desttxt), MACSTR, MAC2STR(dest));
  286. #endif /* HOSTAPD */
  287. io[0].iov_base = "MLME ";
  288. io[0].iov_len = 5;
  289. io[1].iov_base = (void *) data;
  290. io[1].iov_len = data_len;
  291. os_memset(&msg, 0, sizeof(msg));
  292. msg.msg_iov = io;
  293. msg.msg_iovlen = 2;
  294. #ifdef HOSTAPD
  295. if (drv->test_dir == NULL) {
  296. wpa_printf(MSG_DEBUG, "%s: test_dir == NULL", __func__);
  297. return -1;
  298. }
  299. dir = opendir(drv->test_dir);
  300. if (dir == NULL) {
  301. perror("test_driver: opendir");
  302. return -1;
  303. }
  304. while ((dent = readdir(dir))) {
  305. #ifdef _DIRENT_HAVE_D_TYPE
  306. /* Skip the file if it is not a socket. Also accept
  307. * DT_UNKNOWN (0) in case the C library or underlying file
  308. * system does not support d_type. */
  309. if (dent->d_type != DT_SOCK && dent->d_type != DT_UNKNOWN)
  310. continue;
  311. #endif /* _DIRENT_HAVE_D_TYPE */
  312. if (os_strcmp(dent->d_name, ".") == 0 ||
  313. os_strcmp(dent->d_name, "..") == 0)
  314. continue;
  315. os_memset(&addr, 0, sizeof(addr));
  316. addr.sun_family = AF_UNIX;
  317. os_snprintf(addr.sun_path, sizeof(addr.sun_path), "%s/%s",
  318. drv->test_dir, dent->d_name);
  319. if (os_strcmp(addr.sun_path, drv->own_socket_path) == 0)
  320. continue;
  321. if (!broadcast && os_strstr(dent->d_name, desttxt) == NULL)
  322. continue;
  323. wpa_printf(MSG_DEBUG, "%s: Send management frame to %s",
  324. __func__, dent->d_name);
  325. msg.msg_name = &addr;
  326. msg.msg_namelen = sizeof(addr);
  327. ret = sendmsg(drv->test_socket, &msg, 0);
  328. if (ret < 0)
  329. perror("driver_test: sendmsg(test_socket)");
  330. }
  331. closedir(dir);
  332. #else /* HOSTAPD */
  333. if (os_memcmp(dest, drv->bssid, ETH_ALEN) == 0 ||
  334. drv->test_dir == NULL) {
  335. if (drv->hostapd_addr_udp_set) {
  336. msg.msg_name = &drv->hostapd_addr_udp;
  337. msg.msg_namelen = sizeof(drv->hostapd_addr_udp);
  338. } else {
  339. #ifdef DRIVER_TEST_UNIX
  340. msg.msg_name = &drv->hostapd_addr;
  341. msg.msg_namelen = sizeof(drv->hostapd_addr);
  342. #endif /* DRIVER_TEST_UNIX */
  343. }
  344. } else if (broadcast) {
  345. dir = opendir(drv->test_dir);
  346. if (dir == NULL)
  347. return -1;
  348. while ((dent = readdir(dir))) {
  349. #ifdef _DIRENT_HAVE_D_TYPE
  350. /* Skip the file if it is not a socket.
  351. * Also accept DT_UNKNOWN (0) in case
  352. * the C library or underlying file
  353. * system does not support d_type. */
  354. if (dent->d_type != DT_SOCK &&
  355. dent->d_type != DT_UNKNOWN)
  356. continue;
  357. #endif /* _DIRENT_HAVE_D_TYPE */
  358. if (os_strcmp(dent->d_name, ".") == 0 ||
  359. os_strcmp(dent->d_name, "..") == 0)
  360. continue;
  361. wpa_printf(MSG_DEBUG, "%s: Send broadcast MLME to %s",
  362. __func__, dent->d_name);
  363. os_memset(&addr, 0, sizeof(addr));
  364. addr.sun_family = AF_UNIX;
  365. os_snprintf(addr.sun_path, sizeof(addr.sun_path),
  366. "%s/%s", drv->test_dir, dent->d_name);
  367. msg.msg_name = &addr;
  368. msg.msg_namelen = sizeof(addr);
  369. ret = sendmsg(drv->test_socket, &msg, 0);
  370. if (ret < 0)
  371. perror("driver_test: sendmsg(test_socket)");
  372. }
  373. closedir(dir);
  374. return ret;
  375. } else {
  376. struct stat st;
  377. os_memset(&addr, 0, sizeof(addr));
  378. addr.sun_family = AF_UNIX;
  379. os_snprintf(addr.sun_path, sizeof(addr.sun_path),
  380. "%s/AP-" MACSTR, drv->test_dir, MAC2STR(dest));
  381. if (stat(addr.sun_path, &st) < 0) {
  382. os_snprintf(addr.sun_path, sizeof(addr.sun_path),
  383. "%s/STA-" MACSTR,
  384. drv->test_dir, MAC2STR(dest));
  385. }
  386. msg.msg_name = &addr;
  387. msg.msg_namelen = sizeof(addr);
  388. }
  389. if (sendmsg(drv->test_socket, &msg, 0) < 0) {
  390. perror("sendmsg(test_socket)");
  391. return -1;
  392. }
  393. #endif /* HOSTAPD */
  394. hdr = (struct ieee80211_hdr *) data;
  395. fc = le_to_host16(hdr->frame_control);
  396. #ifdef HOSTAPD
  397. hostapd_mgmt_tx_cb(drv->hapd, (u8 *) data, data_len,
  398. WLAN_FC_GET_STYPE(fc), ret >= 0);
  399. #else /* HOSTAPD */
  400. if (drv->ap) {
  401. ap_mgmt_tx_cb(drv->ctx, (u8 *) data, data_len,
  402. WLAN_FC_GET_STYPE(fc), ret >= 0);
  403. }
  404. #endif /* HOSTAPD */
  405. return ret;
  406. }
  407. static void test_driver_scan(struct wpa_driver_test_data *drv,
  408. struct sockaddr_un *from, socklen_t fromlen,
  409. char *data)
  410. {
  411. char buf[512], *pos, *end;
  412. int ret;
  413. struct test_driver_bss *bss;
  414. u8 sa[ETH_ALEN];
  415. u8 ie[512];
  416. size_t ielen;
  417. /* data: optional [ ' ' | STA-addr | ' ' | IEs(hex) ] */
  418. wpa_printf(MSG_DEBUG, "test_driver: SCAN");
  419. if (*data) {
  420. if (*data != ' ' ||
  421. hwaddr_aton(data + 1, sa)) {
  422. wpa_printf(MSG_DEBUG, "test_driver: Unexpected SCAN "
  423. "command format");
  424. return;
  425. }
  426. data += 18;
  427. while (*data == ' ')
  428. data++;
  429. ielen = os_strlen(data) / 2;
  430. if (ielen > sizeof(ie))
  431. ielen = sizeof(ie);
  432. if (hexstr2bin(data, ie, ielen) < 0)
  433. ielen = 0;
  434. wpa_printf(MSG_DEBUG, "test_driver: Scan from " MACSTR,
  435. MAC2STR(sa));
  436. wpa_hexdump(MSG_MSGDUMP, "test_driver: scan IEs", ie, ielen);
  437. #ifdef HOSTAPD
  438. hostapd_probe_req_rx(drv->hapd, sa, ie, ielen);
  439. #endif /* HOSTAPD */
  440. }
  441. for (bss = drv->bss; bss; bss = bss->next) {
  442. pos = buf;
  443. end = buf + sizeof(buf);
  444. /* reply: SCANRESP BSSID SSID IEs */
  445. ret = snprintf(pos, end - pos, "SCANRESP " MACSTR " ",
  446. MAC2STR(bss->bssid));
  447. if (ret < 0 || ret >= end - pos)
  448. return;
  449. pos += ret;
  450. pos += wpa_snprintf_hex(pos, end - pos,
  451. bss->ssid, bss->ssid_len);
  452. ret = snprintf(pos, end - pos, " ");
  453. if (ret < 0 || ret >= end - pos)
  454. return;
  455. pos += ret;
  456. pos += wpa_snprintf_hex(pos, end - pos, bss->ie, bss->ielen);
  457. pos += wpa_snprintf_hex(pos, end - pos, bss->wps_probe_resp_ie,
  458. bss->wps_probe_resp_ie_len);
  459. if (bss->privacy) {
  460. ret = snprintf(pos, end - pos, " PRIVACY");
  461. if (ret < 0 || ret >= end - pos)
  462. return;
  463. pos += ret;
  464. }
  465. sendto(drv->test_socket, buf, pos - buf, 0,
  466. (struct sockaddr *) from, fromlen);
  467. }
  468. }
  469. static struct hostapd_data *
  470. test_driver_get_hapd(struct wpa_driver_test_data *drv,
  471. struct test_driver_bss *bss)
  472. {
  473. #ifdef HOSTAPD
  474. struct hostapd_iface *iface = drv->hapd->iface;
  475. struct hostapd_data *hapd = NULL;
  476. size_t i;
  477. if (bss == NULL) {
  478. wpa_printf(MSG_DEBUG, "%s: bss == NULL", __func__);
  479. return NULL;
  480. }
  481. for (i = 0; i < iface->num_bss; i++) {
  482. hapd = iface->bss[i];
  483. if (memcmp(hapd->own_addr, bss->bssid, ETH_ALEN) == 0)
  484. break;
  485. }
  486. if (i == iface->num_bss) {
  487. wpa_printf(MSG_DEBUG, "%s: no matching interface entry found "
  488. "for BSSID " MACSTR, __func__, MAC2STR(bss->bssid));
  489. return NULL;
  490. }
  491. return hapd;
  492. #else /* HOSTAPD */
  493. return NULL;
  494. #endif /* HOSTAPD */
  495. }
  496. static int test_driver_new_sta(struct wpa_driver_test_data *drv,
  497. struct test_driver_bss *bss, const u8 *addr,
  498. const u8 *ie, size_t ielen)
  499. {
  500. struct hostapd_data *hapd;
  501. hapd = test_driver_get_hapd(drv, bss);
  502. if (hapd == NULL)
  503. return -1;
  504. #ifdef HOSTAPD
  505. return hostapd_notif_assoc(hapd, addr, ie, ielen);
  506. #else /* HOSTAPD */
  507. return -1;
  508. #endif /* HOSTAPD */
  509. }
  510. static void test_driver_assoc(struct wpa_driver_test_data *drv,
  511. struct sockaddr_un *from, socklen_t fromlen,
  512. char *data)
  513. {
  514. struct test_client_socket *cli;
  515. u8 ie[256], ssid[32];
  516. size_t ielen, ssid_len = 0;
  517. char *pos, *pos2, cmd[50];
  518. struct test_driver_bss *bss;
  519. /* data: STA-addr SSID(hex) IEs(hex) */
  520. cli = os_zalloc(sizeof(*cli));
  521. if (cli == NULL)
  522. return;
  523. if (hwaddr_aton(data, cli->addr)) {
  524. printf("test_socket: Invalid MAC address '%s' in ASSOC\n",
  525. data);
  526. free(cli);
  527. return;
  528. }
  529. pos = data + 17;
  530. while (*pos == ' ')
  531. pos++;
  532. pos2 = strchr(pos, ' ');
  533. ielen = 0;
  534. if (pos2) {
  535. ssid_len = (pos2 - pos) / 2;
  536. if (hexstr2bin(pos, ssid, ssid_len) < 0) {
  537. wpa_printf(MSG_DEBUG, "%s: Invalid SSID", __func__);
  538. free(cli);
  539. return;
  540. }
  541. wpa_hexdump_ascii(MSG_DEBUG, "test_driver_assoc: SSID",
  542. ssid, ssid_len);
  543. pos = pos2 + 1;
  544. ielen = strlen(pos) / 2;
  545. if (ielen > sizeof(ie))
  546. ielen = sizeof(ie);
  547. if (hexstr2bin(pos, ie, ielen) < 0)
  548. ielen = 0;
  549. }
  550. for (bss = drv->bss; bss; bss = bss->next) {
  551. if (bss->ssid_len == ssid_len &&
  552. memcmp(bss->ssid, ssid, ssid_len) == 0)
  553. break;
  554. }
  555. if (bss == NULL) {
  556. wpa_printf(MSG_DEBUG, "%s: No matching SSID found from "
  557. "configured BSSes", __func__);
  558. free(cli);
  559. return;
  560. }
  561. cli->bss = bss;
  562. memcpy(&cli->un, from, sizeof(cli->un));
  563. cli->unlen = fromlen;
  564. cli->next = drv->cli;
  565. drv->cli = cli;
  566. wpa_hexdump_ascii(MSG_DEBUG, "test_socket: ASSOC sun_path",
  567. (const u8 *) cli->un.sun_path,
  568. cli->unlen - sizeof(cli->un.sun_family));
  569. snprintf(cmd, sizeof(cmd), "ASSOCRESP " MACSTR " 0",
  570. MAC2STR(bss->bssid));
  571. sendto(drv->test_socket, cmd, strlen(cmd), 0,
  572. (struct sockaddr *) from, fromlen);
  573. if (test_driver_new_sta(drv, bss, cli->addr, ie, ielen) < 0) {
  574. wpa_printf(MSG_DEBUG, "test_driver: failed to add new STA");
  575. }
  576. }
  577. static void test_driver_disassoc(struct wpa_driver_test_data *drv,
  578. struct sockaddr_un *from, socklen_t fromlen)
  579. {
  580. struct test_client_socket *cli;
  581. cli = test_driver_get_cli(drv, from, fromlen);
  582. if (!cli)
  583. return;
  584. #ifdef HOSTAPD
  585. hostapd_notif_disassoc(drv->hapd, cli->addr);
  586. #endif /* HOSTAPD */
  587. }
  588. static void test_driver_eapol(struct wpa_driver_test_data *drv,
  589. struct sockaddr_un *from, socklen_t fromlen,
  590. u8 *data, size_t datalen)
  591. {
  592. #ifdef HOSTAPD
  593. struct test_client_socket *cli;
  594. #endif /* HOSTAPD */
  595. const u8 *src = NULL;
  596. if (datalen > 14) {
  597. /* Skip Ethernet header */
  598. src = data + ETH_ALEN;
  599. wpa_printf(MSG_DEBUG, "test_driver: dst=" MACSTR " src="
  600. MACSTR " proto=%04x",
  601. MAC2STR(data), MAC2STR(src),
  602. WPA_GET_BE16(data + 2 * ETH_ALEN));
  603. data += 14;
  604. datalen -= 14;
  605. }
  606. #ifdef HOSTAPD
  607. cli = test_driver_get_cli(drv, from, fromlen);
  608. if (cli) {
  609. struct hostapd_data *hapd;
  610. hapd = test_driver_get_hapd(drv, cli->bss);
  611. if (hapd == NULL)
  612. return;
  613. hostapd_eapol_receive(hapd, cli->addr, data, datalen);
  614. } else {
  615. wpa_printf(MSG_DEBUG, "test_socket: EAPOL from unknown "
  616. "client");
  617. }
  618. #else /* HOSTAPD */
  619. if (src)
  620. wpa_supplicant_rx_eapol(drv->ctx, src, data, datalen);
  621. #endif /* HOSTAPD */
  622. }
  623. static void test_driver_ether(struct wpa_driver_test_data *drv,
  624. struct sockaddr_un *from, socklen_t fromlen,
  625. u8 *data, size_t datalen)
  626. {
  627. struct l2_ethhdr *eth;
  628. if (datalen < sizeof(*eth))
  629. return;
  630. eth = (struct l2_ethhdr *) data;
  631. wpa_printf(MSG_DEBUG, "test_driver: RX ETHER dst=" MACSTR " src="
  632. MACSTR " proto=%04x",
  633. MAC2STR(eth->h_dest), MAC2STR(eth->h_source),
  634. be_to_host16(eth->h_proto));
  635. #ifdef CONFIG_IEEE80211R
  636. if (be_to_host16(eth->h_proto) == ETH_P_RRB) {
  637. #ifdef HOSTAPD
  638. wpa_ft_rrb_rx(drv->hapd->wpa_auth, eth->h_source,
  639. data + sizeof(*eth), datalen - sizeof(*eth));
  640. #endif /* HOSTAPD */
  641. }
  642. #endif /* CONFIG_IEEE80211R */
  643. }
  644. static void test_driver_mlme(struct wpa_driver_test_data *drv,
  645. struct sockaddr_un *from, socklen_t fromlen,
  646. u8 *data, size_t datalen)
  647. {
  648. struct ieee80211_hdr *hdr;
  649. u16 fc;
  650. hdr = (struct ieee80211_hdr *) data;
  651. if (test_driver_get_cli(drv, from, fromlen) == NULL && datalen >= 16) {
  652. struct test_client_socket *cli;
  653. cli = os_zalloc(sizeof(*cli));
  654. if (cli == NULL)
  655. return;
  656. wpa_printf(MSG_DEBUG, "Adding client entry for " MACSTR,
  657. MAC2STR(hdr->addr2));
  658. memcpy(cli->addr, hdr->addr2, ETH_ALEN);
  659. memcpy(&cli->un, from, sizeof(cli->un));
  660. cli->unlen = fromlen;
  661. cli->next = drv->cli;
  662. drv->cli = cli;
  663. }
  664. wpa_hexdump(MSG_MSGDUMP, "test_driver_mlme: received frame",
  665. data, datalen);
  666. fc = le_to_host16(hdr->frame_control);
  667. if (WLAN_FC_GET_TYPE(fc) != WLAN_FC_TYPE_MGMT) {
  668. wpa_printf(MSG_ERROR, "%s: received non-mgmt frame",
  669. __func__);
  670. return;
  671. }
  672. #ifdef HOSTAPD
  673. hostapd_mgmt_rx(drv->hapd, data, datalen, WLAN_FC_GET_STYPE(fc), NULL);
  674. #else /* HOSTAPD */
  675. ap_mgmt_rx(drv->ctx, data, datalen, WLAN_FC_GET_STYPE(fc), NULL);
  676. #endif /* HOSTAPD */
  677. }
  678. static void test_driver_receive_unix(int sock, void *eloop_ctx, void *sock_ctx)
  679. {
  680. struct wpa_driver_test_data *drv = eloop_ctx;
  681. char buf[2000];
  682. int res;
  683. struct sockaddr_un from;
  684. socklen_t fromlen = sizeof(from);
  685. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  686. (struct sockaddr *) &from, &fromlen);
  687. if (res < 0) {
  688. perror("recvfrom(test_socket)");
  689. return;
  690. }
  691. buf[res] = '\0';
  692. wpa_printf(MSG_DEBUG, "test_driver: received %u bytes", res);
  693. if (strncmp(buf, "SCAN", 4) == 0) {
  694. test_driver_scan(drv, &from, fromlen, buf + 4);
  695. } else if (strncmp(buf, "ASSOC ", 6) == 0) {
  696. test_driver_assoc(drv, &from, fromlen, buf + 6);
  697. } else if (strcmp(buf, "DISASSOC") == 0) {
  698. test_driver_disassoc(drv, &from, fromlen);
  699. } else if (strncmp(buf, "EAPOL ", 6) == 0) {
  700. test_driver_eapol(drv, &from, fromlen, (u8 *) buf + 6,
  701. res - 6);
  702. } else if (strncmp(buf, "ETHER ", 6) == 0) {
  703. test_driver_ether(drv, &from, fromlen, (u8 *) buf + 6,
  704. res - 6);
  705. } else if (strncmp(buf, "MLME ", 5) == 0) {
  706. test_driver_mlme(drv, &from, fromlen, (u8 *) buf + 5, res - 5);
  707. } else {
  708. wpa_hexdump_ascii(MSG_DEBUG, "Unknown test_socket command",
  709. (u8 *) buf, res);
  710. }
  711. }
  712. static struct test_driver_bss *
  713. test_driver_get_bss(struct wpa_driver_test_data *drv, const char *ifname)
  714. {
  715. struct test_driver_bss *bss;
  716. for (bss = drv->bss; bss; bss = bss->next) {
  717. if (strcmp(bss->ifname, ifname) == 0)
  718. return bss;
  719. }
  720. return NULL;
  721. }
  722. static int test_driver_set_generic_elem(const char *ifname, void *priv,
  723. const u8 *elem, size_t elem_len)
  724. {
  725. struct wpa_driver_test_data *drv = priv;
  726. struct test_driver_bss *bss;
  727. bss = test_driver_get_bss(drv, ifname);
  728. if (bss == NULL)
  729. return -1;
  730. free(bss->ie);
  731. if (elem == NULL) {
  732. bss->ie = NULL;
  733. bss->ielen = 0;
  734. return 0;
  735. }
  736. bss->ie = malloc(elem_len);
  737. if (bss->ie == NULL) {
  738. bss->ielen = 0;
  739. return -1;
  740. }
  741. memcpy(bss->ie, elem, elem_len);
  742. bss->ielen = elem_len;
  743. return 0;
  744. }
  745. static int test_driver_set_wps_beacon_ie(const char *ifname, void *priv,
  746. const u8 *ie, size_t len)
  747. {
  748. struct wpa_driver_test_data *drv = priv;
  749. struct test_driver_bss *bss;
  750. wpa_hexdump(MSG_DEBUG, "test_driver: Beacon WPS IE", ie, len);
  751. bss = test_driver_get_bss(drv, ifname);
  752. if (bss == NULL)
  753. return -1;
  754. free(bss->wps_beacon_ie);
  755. if (ie == NULL) {
  756. bss->wps_beacon_ie = NULL;
  757. bss->wps_beacon_ie_len = 0;
  758. return 0;
  759. }
  760. bss->wps_beacon_ie = malloc(len);
  761. if (bss->wps_beacon_ie == NULL) {
  762. bss->wps_beacon_ie_len = 0;
  763. return -1;
  764. }
  765. memcpy(bss->wps_beacon_ie, ie, len);
  766. bss->wps_beacon_ie_len = len;
  767. return 0;
  768. }
  769. static int test_driver_set_wps_probe_resp_ie(const char *ifname, void *priv,
  770. const u8 *ie, size_t len)
  771. {
  772. struct wpa_driver_test_data *drv = priv;
  773. struct test_driver_bss *bss;
  774. wpa_hexdump(MSG_DEBUG, "test_driver: ProbeResp WPS IE", ie, len);
  775. bss = test_driver_get_bss(drv, ifname);
  776. if (bss == NULL)
  777. return -1;
  778. free(bss->wps_probe_resp_ie);
  779. if (ie == NULL) {
  780. bss->wps_probe_resp_ie = NULL;
  781. bss->wps_probe_resp_ie_len = 0;
  782. return 0;
  783. }
  784. bss->wps_probe_resp_ie = malloc(len);
  785. if (bss->wps_probe_resp_ie == NULL) {
  786. bss->wps_probe_resp_ie_len = 0;
  787. return -1;
  788. }
  789. memcpy(bss->wps_probe_resp_ie, ie, len);
  790. bss->wps_probe_resp_ie_len = len;
  791. return 0;
  792. }
  793. static int test_driver_sta_deauth(void *priv, const u8 *own_addr,
  794. const u8 *addr, int reason)
  795. {
  796. struct wpa_driver_test_data *drv = priv;
  797. struct test_client_socket *cli;
  798. if (drv->test_socket < 0)
  799. return -1;
  800. cli = drv->cli;
  801. while (cli) {
  802. if (memcmp(cli->addr, addr, ETH_ALEN) == 0)
  803. break;
  804. cli = cli->next;
  805. }
  806. if (!cli)
  807. return -1;
  808. return sendto(drv->test_socket, "DEAUTH", 6, 0,
  809. (struct sockaddr *) &cli->un, cli->unlen);
  810. }
  811. static int test_driver_sta_disassoc(void *priv, const u8 *own_addr,
  812. const u8 *addr, int reason)
  813. {
  814. struct wpa_driver_test_data *drv = priv;
  815. struct test_client_socket *cli;
  816. if (drv->test_socket < 0)
  817. return -1;
  818. cli = drv->cli;
  819. while (cli) {
  820. if (memcmp(cli->addr, addr, ETH_ALEN) == 0)
  821. break;
  822. cli = cli->next;
  823. }
  824. if (!cli)
  825. return -1;
  826. return sendto(drv->test_socket, "DISASSOC", 8, 0,
  827. (struct sockaddr *) &cli->un, cli->unlen);
  828. }
  829. static int test_driver_bss_add(void *priv, const char *ifname, const u8 *bssid)
  830. {
  831. struct wpa_driver_test_data *drv = priv;
  832. struct test_driver_bss *bss;
  833. wpa_printf(MSG_DEBUG, "%s(ifname=%s bssid=" MACSTR ")",
  834. __func__, ifname, MAC2STR(bssid));
  835. bss = os_zalloc(sizeof(*bss));
  836. if (bss == NULL)
  837. return -1;
  838. os_strlcpy(bss->ifname, ifname, IFNAMSIZ);
  839. memcpy(bss->bssid, bssid, ETH_ALEN);
  840. bss->next = drv->bss;
  841. drv->bss = bss;
  842. return 0;
  843. }
  844. static int test_driver_bss_remove(void *priv, const char *ifname)
  845. {
  846. struct wpa_driver_test_data *drv = priv;
  847. struct test_driver_bss *bss, *prev;
  848. struct test_client_socket *cli, *prev_c;
  849. wpa_printf(MSG_DEBUG, "%s(ifname=%s)", __func__, ifname);
  850. for (prev = NULL, bss = drv->bss; bss; prev = bss, bss = bss->next) {
  851. if (strcmp(bss->ifname, ifname) != 0)
  852. continue;
  853. if (prev)
  854. prev->next = bss->next;
  855. else
  856. drv->bss = bss->next;
  857. for (prev_c = NULL, cli = drv->cli; cli;
  858. prev_c = cli, cli = cli->next) {
  859. if (cli->bss != bss)
  860. continue;
  861. if (prev_c)
  862. prev_c->next = cli->next;
  863. else
  864. drv->cli = cli->next;
  865. free(cli);
  866. break;
  867. }
  868. test_driver_free_bss(bss);
  869. return 0;
  870. }
  871. return -1;
  872. }
  873. static int test_driver_if_add(const char *iface, void *priv,
  874. enum hostapd_driver_if_type type, char *ifname,
  875. const u8 *addr)
  876. {
  877. wpa_printf(MSG_DEBUG, "%s(iface=%s type=%d ifname=%s)",
  878. __func__, iface, type, ifname);
  879. return 0;
  880. }
  881. static int test_driver_if_update(void *priv, enum hostapd_driver_if_type type,
  882. char *ifname, const u8 *addr)
  883. {
  884. wpa_printf(MSG_DEBUG, "%s(type=%d ifname=%s)", __func__, type, ifname);
  885. return 0;
  886. }
  887. static int test_driver_if_remove(void *priv, enum hostapd_driver_if_type type,
  888. const char *ifname, const u8 *addr)
  889. {
  890. wpa_printf(MSG_DEBUG, "%s(type=%d ifname=%s)", __func__, type, ifname);
  891. return 0;
  892. }
  893. static int test_driver_valid_bss_mask(void *priv, const u8 *addr,
  894. const u8 *mask)
  895. {
  896. return 0;
  897. }
  898. static int test_driver_set_ssid(const char *ifname, void *priv, const u8 *buf,
  899. int len)
  900. {
  901. struct wpa_driver_test_data *drv = priv;
  902. struct test_driver_bss *bss;
  903. wpa_printf(MSG_DEBUG, "%s(ifname=%s)", __func__, ifname);
  904. wpa_hexdump_ascii(MSG_DEBUG, "test_driver_set_ssid: SSID", buf, len);
  905. for (bss = drv->bss; bss; bss = bss->next) {
  906. if (strcmp(bss->ifname, ifname) != 0)
  907. continue;
  908. if (len < 0 || (size_t) len > sizeof(bss->ssid))
  909. return -1;
  910. memcpy(bss->ssid, buf, len);
  911. bss->ssid_len = len;
  912. return 0;
  913. }
  914. return -1;
  915. }
  916. static int test_driver_set_privacy(const char *ifname, void *priv, int enabled)
  917. {
  918. struct wpa_driver_test_data *drv = priv;
  919. struct test_driver_bss *bss;
  920. wpa_printf(MSG_DEBUG, "%s(ifname=%s enabled=%d)",
  921. __func__, ifname, enabled);
  922. for (bss = drv->bss; bss; bss = bss->next) {
  923. if (strcmp(bss->ifname, ifname) != 0)
  924. continue;
  925. bss->privacy = enabled;
  926. return 0;
  927. }
  928. return -1;
  929. }
  930. static int test_driver_set_sta_vlan(void *priv, const u8 *addr,
  931. const char *ifname, int vlan_id)
  932. {
  933. wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " ifname=%s vlan_id=%d)",
  934. __func__, MAC2STR(addr), ifname, vlan_id);
  935. return 0;
  936. }
  937. static int test_driver_sta_add(const char *ifname, void *priv,
  938. struct hostapd_sta_add_params *params)
  939. {
  940. struct wpa_driver_test_data *drv = priv;
  941. struct test_client_socket *cli;
  942. struct test_driver_bss *bss;
  943. wpa_printf(MSG_DEBUG, "%s(ifname=%s addr=" MACSTR " aid=%d "
  944. "capability=0x%x flags=0x%x listen_interval=%d)",
  945. __func__, ifname, MAC2STR(params->addr), params->aid,
  946. params->capability, params->flags,
  947. params->listen_interval);
  948. wpa_hexdump(MSG_DEBUG, "test_driver_sta_add - supp_rates",
  949. params->supp_rates, params->supp_rates_len);
  950. cli = drv->cli;
  951. while (cli) {
  952. if (os_memcmp(cli->addr, params->addr, ETH_ALEN) == 0)
  953. break;
  954. cli = cli->next;
  955. }
  956. if (!cli) {
  957. wpa_printf(MSG_DEBUG, "%s: no matching client entry",
  958. __func__);
  959. return -1;
  960. }
  961. for (bss = drv->bss; bss; bss = bss->next) {
  962. if (strcmp(ifname, bss->ifname) == 0)
  963. break;
  964. }
  965. if (bss == NULL) {
  966. wpa_printf(MSG_DEBUG, "%s: No matching interface found from "
  967. "configured BSSes", __func__);
  968. return -1;
  969. }
  970. cli->bss = bss;
  971. return 0;
  972. }
  973. static void * test_driver_init(struct hostapd_data *hapd,
  974. struct wpa_init_params *params)
  975. {
  976. struct wpa_driver_test_data *drv;
  977. struct sockaddr_un addr_un;
  978. struct sockaddr_in addr_in;
  979. struct sockaddr *addr;
  980. socklen_t alen;
  981. drv = os_zalloc(sizeof(struct wpa_driver_test_data));
  982. if (drv == NULL) {
  983. printf("Could not allocate memory for test driver data\n");
  984. return NULL;
  985. }
  986. drv->ap = 1;
  987. drv->bss = os_zalloc(sizeof(*drv->bss));
  988. if (drv->bss == NULL) {
  989. printf("Could not allocate memory for test driver BSS data\n");
  990. free(drv);
  991. return NULL;
  992. }
  993. drv->hapd = hapd;
  994. /* Generate a MAC address to help testing with multiple APs */
  995. params->own_addr[0] = 0x02; /* locally administered */
  996. sha1_prf((const u8 *) params->ifname, strlen(params->ifname),
  997. "hostapd test bssid generation",
  998. params->ssid, params->ssid_len,
  999. params->own_addr + 1, ETH_ALEN - 1);
  1000. os_strlcpy(drv->bss->ifname, params->ifname, IFNAMSIZ);
  1001. memcpy(drv->bss->bssid, params->own_addr, ETH_ALEN);
  1002. if (params->test_socket) {
  1003. if (os_strlen(params->test_socket) >=
  1004. sizeof(addr_un.sun_path)) {
  1005. printf("Too long test_socket path\n");
  1006. wpa_driver_test_deinit(drv);
  1007. return NULL;
  1008. }
  1009. if (strncmp(params->test_socket, "DIR:", 4) == 0) {
  1010. size_t len = strlen(params->test_socket) + 30;
  1011. drv->test_dir = strdup(params->test_socket + 4);
  1012. drv->own_socket_path = malloc(len);
  1013. if (drv->own_socket_path) {
  1014. snprintf(drv->own_socket_path, len,
  1015. "%s/AP-" MACSTR,
  1016. params->test_socket + 4,
  1017. MAC2STR(params->own_addr));
  1018. }
  1019. } else if (strncmp(params->test_socket, "UDP:", 4) == 0) {
  1020. drv->udp_port = atoi(params->test_socket + 4);
  1021. } else {
  1022. drv->own_socket_path = strdup(params->test_socket);
  1023. }
  1024. if (drv->own_socket_path == NULL && drv->udp_port == 0) {
  1025. wpa_driver_test_deinit(drv);
  1026. return NULL;
  1027. }
  1028. drv->test_socket = socket(drv->udp_port ? PF_INET : PF_UNIX,
  1029. SOCK_DGRAM, 0);
  1030. if (drv->test_socket < 0) {
  1031. perror("socket");
  1032. wpa_driver_test_deinit(drv);
  1033. return NULL;
  1034. }
  1035. if (drv->udp_port) {
  1036. os_memset(&addr_in, 0, sizeof(addr_in));
  1037. addr_in.sin_family = AF_INET;
  1038. addr_in.sin_port = htons(drv->udp_port);
  1039. addr = (struct sockaddr *) &addr_in;
  1040. alen = sizeof(addr_in);
  1041. } else {
  1042. os_memset(&addr_un, 0, sizeof(addr_un));
  1043. addr_un.sun_family = AF_UNIX;
  1044. os_strlcpy(addr_un.sun_path, drv->own_socket_path,
  1045. sizeof(addr_un.sun_path));
  1046. addr = (struct sockaddr *) &addr_un;
  1047. alen = sizeof(addr_un);
  1048. }
  1049. if (bind(drv->test_socket, addr, alen) < 0) {
  1050. perror("bind(PF_UNIX)");
  1051. close(drv->test_socket);
  1052. if (drv->own_socket_path)
  1053. unlink(drv->own_socket_path);
  1054. wpa_driver_test_deinit(drv);
  1055. return NULL;
  1056. }
  1057. eloop_register_read_sock(drv->test_socket,
  1058. test_driver_receive_unix, drv, NULL);
  1059. } else
  1060. drv->test_socket = -1;
  1061. return drv;
  1062. }
  1063. static void wpa_driver_test_poll(void *eloop_ctx, void *timeout_ctx)
  1064. {
  1065. struct wpa_driver_test_data *drv = eloop_ctx;
  1066. #ifdef DRIVER_TEST_UNIX
  1067. if (drv->associated && drv->hostapd_addr_set) {
  1068. struct stat st;
  1069. if (stat(drv->hostapd_addr.sun_path, &st) < 0) {
  1070. wpa_printf(MSG_DEBUG, "%s: lost connection to AP: %s",
  1071. __func__, strerror(errno));
  1072. drv->associated = 0;
  1073. wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, NULL);
  1074. }
  1075. }
  1076. #endif /* DRIVER_TEST_UNIX */
  1077. eloop_register_timeout(1, 0, wpa_driver_test_poll, drv, NULL);
  1078. }
  1079. static void wpa_driver_test_scan_timeout(void *eloop_ctx, void *timeout_ctx)
  1080. {
  1081. wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
  1082. wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
  1083. }
  1084. #ifdef DRIVER_TEST_UNIX
  1085. static void wpa_driver_scan_dir(struct wpa_driver_test_data *drv,
  1086. const char *path)
  1087. {
  1088. struct dirent *dent;
  1089. DIR *dir;
  1090. struct sockaddr_un addr;
  1091. char cmd[512], *pos, *end;
  1092. int ret;
  1093. dir = opendir(path);
  1094. if (dir == NULL)
  1095. return;
  1096. end = cmd + sizeof(cmd);
  1097. pos = cmd;
  1098. ret = os_snprintf(pos, end - pos, "SCAN " MACSTR,
  1099. MAC2STR(drv->own_addr));
  1100. if (ret >= 0 && ret < end - pos)
  1101. pos += ret;
  1102. if (drv->probe_req_ie) {
  1103. ret = os_snprintf(pos, end - pos, " ");
  1104. if (ret >= 0 && ret < end - pos)
  1105. pos += ret;
  1106. pos += wpa_snprintf_hex(pos, end - pos, drv->probe_req_ie,
  1107. drv->probe_req_ie_len);
  1108. }
  1109. end[-1] = '\0';
  1110. while ((dent = readdir(dir))) {
  1111. if (os_strncmp(dent->d_name, "AP-", 3) != 0 &&
  1112. os_strncmp(dent->d_name, "STA-", 4) != 0)
  1113. continue;
  1114. if (drv->own_socket_path) {
  1115. size_t olen, dlen;
  1116. olen = os_strlen(drv->own_socket_path);
  1117. dlen = os_strlen(dent->d_name);
  1118. if (olen >= dlen &&
  1119. os_strcmp(dent->d_name,
  1120. drv->own_socket_path + olen - dlen) == 0)
  1121. continue;
  1122. }
  1123. wpa_printf(MSG_DEBUG, "%s: SCAN %s", __func__, dent->d_name);
  1124. os_memset(&addr, 0, sizeof(addr));
  1125. addr.sun_family = AF_UNIX;
  1126. os_snprintf(addr.sun_path, sizeof(addr.sun_path), "%s/%s",
  1127. path, dent->d_name);
  1128. if (sendto(drv->test_socket, cmd, os_strlen(cmd), 0,
  1129. (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  1130. perror("sendto(test_socket)");
  1131. }
  1132. }
  1133. closedir(dir);
  1134. }
  1135. #endif /* DRIVER_TEST_UNIX */
  1136. static int wpa_driver_test_scan(void *priv,
  1137. struct wpa_driver_scan_params *params)
  1138. {
  1139. struct wpa_driver_test_data *drv = priv;
  1140. size_t i;
  1141. wpa_printf(MSG_DEBUG, "%s: priv=%p", __func__, priv);
  1142. os_free(drv->probe_req_ie);
  1143. if (params->extra_ies) {
  1144. drv->probe_req_ie = os_malloc(params->extra_ies_len);
  1145. if (drv->probe_req_ie == NULL) {
  1146. drv->probe_req_ie_len = 0;
  1147. return -1;
  1148. }
  1149. os_memcpy(drv->probe_req_ie, params->extra_ies,
  1150. params->extra_ies_len);
  1151. drv->probe_req_ie_len = params->extra_ies_len;
  1152. } else {
  1153. drv->probe_req_ie = NULL;
  1154. drv->probe_req_ie_len = 0;
  1155. }
  1156. for (i = 0; i < params->num_ssids; i++)
  1157. wpa_hexdump(MSG_DEBUG, "Scan SSID",
  1158. params->ssids[i].ssid, params->ssids[i].ssid_len);
  1159. wpa_hexdump(MSG_DEBUG, "Scan extra IE(s)",
  1160. params->extra_ies, params->extra_ies_len);
  1161. drv->num_scanres = 0;
  1162. #ifdef DRIVER_TEST_UNIX
  1163. if (drv->test_socket >= 0 && drv->test_dir)
  1164. wpa_driver_scan_dir(drv, drv->test_dir);
  1165. if (drv->test_socket >= 0 && drv->hostapd_addr_set &&
  1166. sendto(drv->test_socket, "SCAN", 4, 0,
  1167. (struct sockaddr *) &drv->hostapd_addr,
  1168. sizeof(drv->hostapd_addr)) < 0) {
  1169. perror("sendto(test_socket)");
  1170. }
  1171. #endif /* DRIVER_TEST_UNIX */
  1172. if (drv->test_socket >= 0 && drv->hostapd_addr_udp_set &&
  1173. sendto(drv->test_socket, "SCAN", 4, 0,
  1174. (struct sockaddr *) &drv->hostapd_addr_udp,
  1175. sizeof(drv->hostapd_addr_udp)) < 0) {
  1176. perror("sendto(test_socket)");
  1177. }
  1178. eloop_cancel_timeout(wpa_driver_test_scan_timeout, drv, drv->ctx);
  1179. eloop_register_timeout(1, 0, wpa_driver_test_scan_timeout, drv,
  1180. drv->ctx);
  1181. return 0;
  1182. }
  1183. static struct wpa_scan_results * wpa_driver_test_get_scan_results2(void *priv)
  1184. {
  1185. struct wpa_driver_test_data *drv = priv;
  1186. struct wpa_scan_results *res;
  1187. size_t i;
  1188. res = os_zalloc(sizeof(*res));
  1189. if (res == NULL)
  1190. return NULL;
  1191. res->res = os_zalloc(drv->num_scanres * sizeof(struct wpa_scan_res *));
  1192. if (res->res == NULL) {
  1193. os_free(res);
  1194. return NULL;
  1195. }
  1196. for (i = 0; i < drv->num_scanres; i++) {
  1197. struct wpa_scan_res *r;
  1198. if (drv->scanres[i] == NULL)
  1199. continue;
  1200. r = os_malloc(sizeof(*r) + drv->scanres[i]->ie_len);
  1201. if (r == NULL)
  1202. break;
  1203. os_memcpy(r, drv->scanres[i],
  1204. sizeof(*r) + drv->scanres[i]->ie_len);
  1205. res->res[res->num++] = r;
  1206. }
  1207. return res;
  1208. }
  1209. static int wpa_driver_test_set_key(const char *ifname, void *priv, wpa_alg alg,
  1210. const u8 *addr, int key_idx, int set_tx,
  1211. const u8 *seq, size_t seq_len,
  1212. const u8 *key, size_t key_len)
  1213. {
  1214. wpa_printf(MSG_DEBUG, "%s: ifname=%s priv=%p alg=%d key_idx=%d "
  1215. "set_tx=%d",
  1216. __func__, ifname, priv, alg, key_idx, set_tx);
  1217. if (addr)
  1218. wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
  1219. if (seq)
  1220. wpa_hexdump(MSG_DEBUG, " seq", seq, seq_len);
  1221. if (key)
  1222. wpa_hexdump_key(MSG_DEBUG, " key", key, key_len);
  1223. return 0;
  1224. }
  1225. static int wpa_driver_update_mode(struct wpa_driver_test_data *drv, int ap)
  1226. {
  1227. if (ap && !drv->ap) {
  1228. wpa_driver_test_close_test_socket(drv);
  1229. wpa_driver_test_attach(drv, drv->test_dir, 1);
  1230. drv->ap = 1;
  1231. } else if (!ap && drv->ap) {
  1232. wpa_driver_test_close_test_socket(drv);
  1233. wpa_driver_test_attach(drv, drv->test_dir, 0);
  1234. drv->ap = 0;
  1235. }
  1236. return 0;
  1237. }
  1238. static int wpa_driver_test_associate(
  1239. void *priv, struct wpa_driver_associate_params *params)
  1240. {
  1241. struct wpa_driver_test_data *drv = priv;
  1242. wpa_printf(MSG_DEBUG, "%s: priv=%p freq=%d pairwise_suite=%d "
  1243. "group_suite=%d key_mgmt_suite=%d auth_alg=%d mode=%d",
  1244. __func__, priv, params->freq, params->pairwise_suite,
  1245. params->group_suite, params->key_mgmt_suite,
  1246. params->auth_alg, params->mode);
  1247. if (params->bssid) {
  1248. wpa_printf(MSG_DEBUG, " bssid=" MACSTR,
  1249. MAC2STR(params->bssid));
  1250. }
  1251. if (params->ssid) {
  1252. wpa_hexdump_ascii(MSG_DEBUG, " ssid",
  1253. params->ssid, params->ssid_len);
  1254. }
  1255. if (params->wpa_ie) {
  1256. wpa_hexdump(MSG_DEBUG, " wpa_ie",
  1257. params->wpa_ie, params->wpa_ie_len);
  1258. drv->assoc_wpa_ie_len = params->wpa_ie_len;
  1259. if (drv->assoc_wpa_ie_len > sizeof(drv->assoc_wpa_ie))
  1260. drv->assoc_wpa_ie_len = sizeof(drv->assoc_wpa_ie);
  1261. os_memcpy(drv->assoc_wpa_ie, params->wpa_ie,
  1262. drv->assoc_wpa_ie_len);
  1263. } else
  1264. drv->assoc_wpa_ie_len = 0;
  1265. wpa_driver_update_mode(drv, params->mode == IEEE80211_MODE_AP);
  1266. drv->ibss = params->mode == IEEE80211_MODE_IBSS;
  1267. drv->privacy = params->key_mgmt_suite &
  1268. (WPA_KEY_MGMT_IEEE8021X |
  1269. WPA_KEY_MGMT_PSK |
  1270. WPA_KEY_MGMT_WPA_NONE |
  1271. WPA_KEY_MGMT_FT_IEEE8021X |
  1272. WPA_KEY_MGMT_FT_PSK |
  1273. WPA_KEY_MGMT_IEEE8021X_SHA256 |
  1274. WPA_KEY_MGMT_PSK_SHA256);
  1275. if (params->wep_key_len[params->wep_tx_keyidx])
  1276. drv->privacy = 1;
  1277. #ifdef DRIVER_TEST_UNIX
  1278. if (drv->test_dir && params->bssid &&
  1279. params->mode != IEEE80211_MODE_IBSS) {
  1280. os_memset(&drv->hostapd_addr, 0, sizeof(drv->hostapd_addr));
  1281. drv->hostapd_addr.sun_family = AF_UNIX;
  1282. os_snprintf(drv->hostapd_addr.sun_path,
  1283. sizeof(drv->hostapd_addr.sun_path),
  1284. "%s/AP-" MACSTR,
  1285. drv->test_dir, MAC2STR(params->bssid));
  1286. drv->hostapd_addr_set = 1;
  1287. }
  1288. #endif /* DRIVER_TEST_UNIX */
  1289. if (params->mode == IEEE80211_MODE_AP) {
  1290. struct test_driver_bss *bss;
  1291. os_memcpy(drv->ssid, params->ssid, params->ssid_len);
  1292. drv->ssid_len = params->ssid_len;
  1293. test_driver_free_bsses(drv);
  1294. bss = drv->bss = os_zalloc(sizeof(*drv->bss));
  1295. if (bss == NULL)
  1296. return -1;
  1297. os_memcpy(bss->bssid, drv->own_addr, ETH_ALEN);
  1298. os_memcpy(bss->ssid, params->ssid, params->ssid_len);
  1299. bss->ssid_len = params->ssid_len;
  1300. bss->privacy = drv->privacy;
  1301. if (params->wpa_ie && params->wpa_ie_len) {
  1302. bss->ie = os_malloc(params->wpa_ie_len);
  1303. if (bss->ie) {
  1304. os_memcpy(bss->ie, params->wpa_ie,
  1305. params->wpa_ie_len);
  1306. bss->ielen = params->wpa_ie_len;
  1307. }
  1308. }
  1309. } else if (drv->test_socket >= 0 &&
  1310. (drv->hostapd_addr_set || drv->hostapd_addr_udp_set)) {
  1311. char cmd[200], *pos, *end;
  1312. int ret;
  1313. end = cmd + sizeof(cmd);
  1314. pos = cmd;
  1315. ret = os_snprintf(pos, end - pos, "ASSOC " MACSTR " ",
  1316. MAC2STR(drv->own_addr));
  1317. if (ret >= 0 && ret < end - pos)
  1318. pos += ret;
  1319. pos += wpa_snprintf_hex(pos, end - pos, params->ssid,
  1320. params->ssid_len);
  1321. ret = os_snprintf(pos, end - pos, " ");
  1322. if (ret >= 0 && ret < end - pos)
  1323. pos += ret;
  1324. pos += wpa_snprintf_hex(pos, end - pos, params->wpa_ie,
  1325. params->wpa_ie_len);
  1326. end[-1] = '\0';
  1327. #ifdef DRIVER_TEST_UNIX
  1328. if (drv->hostapd_addr_set &&
  1329. sendto(drv->test_socket, cmd, os_strlen(cmd), 0,
  1330. (struct sockaddr *) &drv->hostapd_addr,
  1331. sizeof(drv->hostapd_addr)) < 0) {
  1332. perror("sendto(test_socket)");
  1333. return -1;
  1334. }
  1335. #endif /* DRIVER_TEST_UNIX */
  1336. if (drv->hostapd_addr_udp_set &&
  1337. sendto(drv->test_socket, cmd, os_strlen(cmd), 0,
  1338. (struct sockaddr *) &drv->hostapd_addr_udp,
  1339. sizeof(drv->hostapd_addr_udp)) < 0) {
  1340. perror("sendto(test_socket)");
  1341. return -1;
  1342. }
  1343. os_memcpy(drv->ssid, params->ssid, params->ssid_len);
  1344. drv->ssid_len = params->ssid_len;
  1345. } else {
  1346. drv->associated = 1;
  1347. if (params->mode == IEEE80211_MODE_IBSS) {
  1348. os_memcpy(drv->ssid, params->ssid, params->ssid_len);
  1349. drv->ssid_len = params->ssid_len;
  1350. if (params->bssid)
  1351. os_memcpy(drv->bssid, params->bssid, ETH_ALEN);
  1352. else {
  1353. os_get_random(drv->bssid, ETH_ALEN);
  1354. drv->bssid[0] &= ~0x01;
  1355. drv->bssid[0] |= 0x02;
  1356. }
  1357. }
  1358. wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL);
  1359. }
  1360. return 0;
  1361. }
  1362. static int wpa_driver_test_get_bssid(void *priv, u8 *bssid)
  1363. {
  1364. struct wpa_driver_test_data *drv = priv;
  1365. os_memcpy(bssid, drv->bssid, ETH_ALEN);
  1366. return 0;
  1367. }
  1368. static int wpa_driver_test_get_ssid(void *priv, u8 *ssid)
  1369. {
  1370. struct wpa_driver_test_data *drv = priv;
  1371. os_memcpy(ssid, drv->ssid, 32);
  1372. return drv->ssid_len;
  1373. }
  1374. static int wpa_driver_test_send_disassoc(struct wpa_driver_test_data *drv)
  1375. {
  1376. #ifdef DRIVER_TEST_UNIX
  1377. if (drv->test_socket >= 0 &&
  1378. sendto(drv->test_socket, "DISASSOC", 8, 0,
  1379. (struct sockaddr *) &drv->hostapd_addr,
  1380. sizeof(drv->hostapd_addr)) < 0) {
  1381. perror("sendto(test_socket)");
  1382. return -1;
  1383. }
  1384. #endif /* DRIVER_TEST_UNIX */
  1385. if (drv->test_socket >= 0 && drv->hostapd_addr_udp_set &&
  1386. sendto(drv->test_socket, "DISASSOC", 8, 0,
  1387. (struct sockaddr *) &drv->hostapd_addr_udp,
  1388. sizeof(drv->hostapd_addr_udp)) < 0) {
  1389. perror("sendto(test_socket)");
  1390. return -1;
  1391. }
  1392. return 0;
  1393. }
  1394. static int wpa_driver_test_deauthenticate(void *priv, const u8 *addr,
  1395. int reason_code)
  1396. {
  1397. struct wpa_driver_test_data *drv = priv;
  1398. wpa_printf(MSG_DEBUG, "%s addr=" MACSTR " reason_code=%d",
  1399. __func__, MAC2STR(addr), reason_code);
  1400. os_memset(drv->bssid, 0, ETH_ALEN);
  1401. drv->associated = 0;
  1402. wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, NULL);
  1403. return wpa_driver_test_send_disassoc(drv);
  1404. }
  1405. static int wpa_driver_test_disassociate(void *priv, const u8 *addr,
  1406. int reason_code)
  1407. {
  1408. struct wpa_driver_test_data *drv = priv;
  1409. wpa_printf(MSG_DEBUG, "%s addr=" MACSTR " reason_code=%d",
  1410. __func__, MAC2STR(addr), reason_code);
  1411. os_memset(drv->bssid, 0, ETH_ALEN);
  1412. drv->associated = 0;
  1413. wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, NULL);
  1414. return wpa_driver_test_send_disassoc(drv);
  1415. }
  1416. static void wpa_driver_test_scanresp(struct wpa_driver_test_data *drv,
  1417. struct sockaddr *from,
  1418. socklen_t fromlen,
  1419. const char *data)
  1420. {
  1421. struct wpa_scan_res *res;
  1422. const char *pos, *pos2;
  1423. size_t len;
  1424. u8 *ie_pos, *ie_start, *ie_end;
  1425. #define MAX_IE_LEN 1000
  1426. wpa_printf(MSG_DEBUG, "test_driver: SCANRESP %s", data);
  1427. if (drv->num_scanres >= MAX_SCAN_RESULTS) {
  1428. wpa_printf(MSG_DEBUG, "test_driver: No room for the new scan "
  1429. "result");
  1430. return;
  1431. }
  1432. /* SCANRESP BSSID SSID IEs */
  1433. res = os_zalloc(sizeof(*res) + MAX_IE_LEN);
  1434. if (res == NULL)
  1435. return;
  1436. ie_start = ie_pos = (u8 *) (res + 1);
  1437. ie_end = ie_pos + MAX_IE_LEN;
  1438. if (hwaddr_aton(data, res->bssid)) {
  1439. wpa_printf(MSG_DEBUG, "test_driver: invalid BSSID in scanres");
  1440. os_free(res);
  1441. return;
  1442. }
  1443. pos = data + 17;
  1444. while (*pos == ' ')
  1445. pos++;
  1446. pos2 = os_strchr(pos, ' ');
  1447. if (pos2 == NULL) {
  1448. wpa_printf(MSG_DEBUG, "test_driver: invalid SSID termination "
  1449. "in scanres");
  1450. os_free(res);
  1451. return;
  1452. }
  1453. len = (pos2 - pos) / 2;
  1454. if (len > 32)
  1455. len = 32;
  1456. /*
  1457. * Generate SSID IE from the SSID field since this IE is not included
  1458. * in the main IE field.
  1459. */
  1460. *ie_pos++ = WLAN_EID_SSID;
  1461. *ie_pos++ = len;
  1462. if (hexstr2bin(pos, ie_pos, len) < 0) {
  1463. wpa_printf(MSG_DEBUG, "test_driver: invalid SSID in scanres");
  1464. os_free(res);
  1465. return;
  1466. }
  1467. ie_pos += len;
  1468. pos = pos2 + 1;
  1469. pos2 = os_strchr(pos, ' ');
  1470. if (pos2 == NULL)
  1471. len = os_strlen(pos) / 2;
  1472. else
  1473. len = (pos2 - pos) / 2;
  1474. if ((int) len > ie_end - ie_pos)
  1475. len = ie_end - ie_pos;
  1476. if (hexstr2bin(pos, ie_pos, len) < 0) {
  1477. wpa_printf(MSG_DEBUG, "test_driver: invalid IEs in scanres");
  1478. os_free(res);
  1479. return;
  1480. }
  1481. ie_pos += len;
  1482. res->ie_len = ie_pos - ie_start;
  1483. if (pos2) {
  1484. pos = pos2 + 1;
  1485. while (*pos == ' ')
  1486. pos++;
  1487. if (os_strstr(pos, "PRIVACY"))
  1488. res->caps |= IEEE80211_CAP_PRIVACY;
  1489. if (os_strstr(pos, "IBSS"))
  1490. res->caps |= IEEE80211_CAP_IBSS;
  1491. }
  1492. os_free(drv->scanres[drv->num_scanres]);
  1493. drv->scanres[drv->num_scanres++] = res;
  1494. }
  1495. static void wpa_driver_test_assocresp(struct wpa_driver_test_data *drv,
  1496. struct sockaddr *from,
  1497. socklen_t fromlen,
  1498. const char *data)
  1499. {
  1500. /* ASSOCRESP BSSID <res> */
  1501. if (hwaddr_aton(data, drv->bssid)) {
  1502. wpa_printf(MSG_DEBUG, "test_driver: invalid BSSID in "
  1503. "assocresp");
  1504. }
  1505. if (drv->use_associnfo) {
  1506. union wpa_event_data event;
  1507. os_memset(&event, 0, sizeof(event));
  1508. event.assoc_info.req_ies = drv->assoc_wpa_ie;
  1509. event.assoc_info.req_ies_len = drv->assoc_wpa_ie_len;
  1510. wpa_supplicant_event(drv->ctx, EVENT_ASSOCINFO, &event);
  1511. }
  1512. drv->associated = 1;
  1513. wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL);
  1514. }
  1515. static void wpa_driver_test_disassoc(struct wpa_driver_test_data *drv,
  1516. struct sockaddr *from,
  1517. socklen_t fromlen)
  1518. {
  1519. drv->associated = 0;
  1520. wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, NULL);
  1521. }
  1522. static void wpa_driver_test_eapol(struct wpa_driver_test_data *drv,
  1523. struct sockaddr *from,
  1524. socklen_t fromlen,
  1525. const u8 *data, size_t data_len)
  1526. {
  1527. const u8 *src = drv->bssid;
  1528. if (data_len > 14) {
  1529. /* Skip Ethernet header */
  1530. src = data + ETH_ALEN;
  1531. data += 14;
  1532. data_len -= 14;
  1533. }
  1534. #ifndef HOSTAPD
  1535. wpa_supplicant_rx_eapol(drv->ctx, src, data, data_len);
  1536. #endif /* HOSTAPD */
  1537. }
  1538. static void wpa_driver_test_mlme(struct wpa_driver_test_data *drv,
  1539. struct sockaddr *from,
  1540. socklen_t fromlen,
  1541. const u8 *data, size_t data_len)
  1542. {
  1543. #ifdef CONFIG_CLIENT_MLME
  1544. struct ieee80211_rx_status rx_status;
  1545. os_memset(&rx_status, 0, sizeof(rx_status));
  1546. wpa_supplicant_sta_rx(drv->ctx, data, data_len, &rx_status);
  1547. #endif /* CONFIG_CLIENT_MLME */
  1548. }
  1549. static void wpa_driver_test_scan_cmd(struct wpa_driver_test_data *drv,
  1550. struct sockaddr *from,
  1551. socklen_t fromlen,
  1552. const u8 *data, size_t data_len)
  1553. {
  1554. char buf[512], *pos, *end;
  1555. int ret;
  1556. /* data: optional [ STA-addr | ' ' | IEs(hex) ] */
  1557. if (!drv->ibss)
  1558. return;
  1559. pos = buf;
  1560. end = buf + sizeof(buf);
  1561. /* reply: SCANRESP BSSID SSID IEs */
  1562. ret = snprintf(pos, end - pos, "SCANRESP " MACSTR " ",
  1563. MAC2STR(drv->bssid));
  1564. if (ret < 0 || ret >= end - pos)
  1565. return;
  1566. pos += ret;
  1567. pos += wpa_snprintf_hex(pos, end - pos,
  1568. drv->ssid, drv->ssid_len);
  1569. ret = snprintf(pos, end - pos, " ");
  1570. if (ret < 0 || ret >= end - pos)
  1571. return;
  1572. pos += ret;
  1573. pos += wpa_snprintf_hex(pos, end - pos, drv->assoc_wpa_ie,
  1574. drv->assoc_wpa_ie_len);
  1575. if (drv->privacy) {
  1576. ret = snprintf(pos, end - pos, " PRIVACY");
  1577. if (ret < 0 || ret >= end - pos)
  1578. return;
  1579. pos += ret;
  1580. }
  1581. ret = snprintf(pos, end - pos, " IBSS");
  1582. if (ret < 0 || ret >= end - pos)
  1583. return;
  1584. pos += ret;
  1585. sendto(drv->test_socket, buf, pos - buf, 0,
  1586. (struct sockaddr *) from, fromlen);
  1587. }
  1588. static void wpa_driver_test_receive_unix(int sock, void *eloop_ctx,
  1589. void *sock_ctx)
  1590. {
  1591. struct wpa_driver_test_data *drv = eloop_ctx;
  1592. char *buf;
  1593. int res;
  1594. struct sockaddr_storage from;
  1595. socklen_t fromlen = sizeof(from);
  1596. const size_t buflen = 2000;
  1597. if (drv->ap) {
  1598. test_driver_receive_unix(sock, eloop_ctx, sock_ctx);
  1599. return;
  1600. }
  1601. buf = os_malloc(buflen);
  1602. if (buf == NULL)
  1603. return;
  1604. res = recvfrom(sock, buf, buflen - 1, 0,
  1605. (struct sockaddr *) &from, &fromlen);
  1606. if (res < 0) {
  1607. perror("recvfrom(test_socket)");
  1608. os_free(buf);
  1609. return;
  1610. }
  1611. buf[res] = '\0';
  1612. wpa_printf(MSG_DEBUG, "test_driver: received %u bytes", res);
  1613. if (os_strncmp(buf, "SCANRESP ", 9) == 0) {
  1614. wpa_driver_test_scanresp(drv, (struct sockaddr *) &from,
  1615. fromlen, buf + 9);
  1616. } else if (os_strncmp(buf, "ASSOCRESP ", 10) == 0) {
  1617. wpa_driver_test_assocresp(drv, (struct sockaddr *) &from,
  1618. fromlen, buf + 10);
  1619. } else if (os_strcmp(buf, "DISASSOC") == 0) {
  1620. wpa_driver_test_disassoc(drv, (struct sockaddr *) &from,
  1621. fromlen);
  1622. } else if (os_strcmp(buf, "DEAUTH") == 0) {
  1623. wpa_driver_test_disassoc(drv, (struct sockaddr *) &from,
  1624. fromlen);
  1625. } else if (os_strncmp(buf, "EAPOL ", 6) == 0) {
  1626. wpa_driver_test_eapol(drv, (struct sockaddr *) &from, fromlen,
  1627. (const u8 *) buf + 6, res - 6);
  1628. } else if (os_strncmp(buf, "MLME ", 5) == 0) {
  1629. wpa_driver_test_mlme(drv, (struct sockaddr *) &from, fromlen,
  1630. (const u8 *) buf + 5, res - 5);
  1631. } else if (os_strncmp(buf, "SCAN ", 5) == 0) {
  1632. wpa_driver_test_scan_cmd(drv, (struct sockaddr *) &from,
  1633. fromlen,
  1634. (const u8 *) buf + 5, res - 5);
  1635. } else {
  1636. wpa_hexdump_ascii(MSG_DEBUG, "Unknown test_socket command",
  1637. (u8 *) buf, res);
  1638. }
  1639. os_free(buf);
  1640. }
  1641. static void * wpa_driver_test_init2(void *ctx, const char *ifname,
  1642. void *global_priv)
  1643. {
  1644. struct wpa_driver_test_data *drv;
  1645. drv = os_zalloc(sizeof(*drv));
  1646. if (drv == NULL)
  1647. return NULL;
  1648. drv->global = global_priv;
  1649. drv->ctx = ctx;
  1650. drv->test_socket = -1;
  1651. /* Set dummy BSSID and SSID for testing. */
  1652. drv->bssid[0] = 0x02;
  1653. drv->bssid[1] = 0x00;
  1654. drv->bssid[2] = 0x00;
  1655. drv->bssid[3] = 0x00;
  1656. drv->bssid[4] = 0x00;
  1657. drv->bssid[5] = 0x01;
  1658. os_memcpy(drv->ssid, "test", 5);
  1659. drv->ssid_len = 4;
  1660. /* Generate a MAC address to help testing with multiple STAs */
  1661. drv->own_addr[0] = 0x02; /* locally administered */
  1662. sha1_prf((const u8 *) ifname, os_strlen(ifname),
  1663. "wpa_supplicant test mac addr generation",
  1664. NULL, 0, drv->own_addr + 1, ETH_ALEN - 1);
  1665. eloop_register_timeout(1, 0, wpa_driver_test_poll, drv, NULL);
  1666. return drv;
  1667. }
  1668. static void wpa_driver_test_close_test_socket(struct wpa_driver_test_data *drv)
  1669. {
  1670. if (drv->test_socket >= 0) {
  1671. eloop_unregister_read_sock(drv->test_socket);
  1672. close(drv->test_socket);
  1673. drv->test_socket = -1;
  1674. }
  1675. if (drv->own_socket_path) {
  1676. unlink(drv->own_socket_path);
  1677. os_free(drv->own_socket_path);
  1678. drv->own_socket_path = NULL;
  1679. }
  1680. }
  1681. static void wpa_driver_test_deinit(void *priv)
  1682. {
  1683. struct wpa_driver_test_data *drv = priv;
  1684. struct test_client_socket *cli, *prev;
  1685. int i;
  1686. cli = drv->cli;
  1687. while (cli) {
  1688. prev = cli;
  1689. cli = cli->next;
  1690. os_free(prev);
  1691. }
  1692. #ifdef HOSTAPD
  1693. /* There should be only one BSS remaining at this point. */
  1694. if (drv->bss == NULL)
  1695. wpa_printf(MSG_ERROR, "%s: drv->bss == NULL", __func__);
  1696. else if (drv->bss->next)
  1697. wpa_printf(MSG_ERROR, "%s: drv->bss->next != NULL", __func__);
  1698. #endif /* HOSTAPD */
  1699. test_driver_free_bsses(drv);
  1700. wpa_driver_test_close_test_socket(drv);
  1701. eloop_cancel_timeout(wpa_driver_test_scan_timeout, drv, drv->ctx);
  1702. eloop_cancel_timeout(wpa_driver_test_poll, drv, NULL);
  1703. os_free(drv->test_dir);
  1704. for (i = 0; i < MAX_SCAN_RESULTS; i++)
  1705. os_free(drv->scanres[i]);
  1706. os_free(drv->probe_req_ie);
  1707. os_free(drv);
  1708. }
  1709. static int wpa_driver_test_attach(struct wpa_driver_test_data *drv,
  1710. const char *dir, int ap)
  1711. {
  1712. #ifdef DRIVER_TEST_UNIX
  1713. static unsigned int counter = 0;
  1714. struct sockaddr_un addr;
  1715. size_t len;
  1716. os_free(drv->own_socket_path);
  1717. if (dir) {
  1718. len = os_strlen(dir) + 30;
  1719. drv->own_socket_path = os_malloc(len);
  1720. if (drv->own_socket_path == NULL)
  1721. return -1;
  1722. os_snprintf(drv->own_socket_path, len, "%s/%s-" MACSTR,
  1723. dir, ap ? "AP" : "STA", MAC2STR(drv->own_addr));
  1724. } else {
  1725. drv->own_socket_path = os_malloc(100);
  1726. if (drv->own_socket_path == NULL)
  1727. return -1;
  1728. os_snprintf(drv->own_socket_path, 100,
  1729. "/tmp/wpa_supplicant_test-%d-%d",
  1730. getpid(), counter++);
  1731. }
  1732. drv->test_socket = socket(PF_UNIX, SOCK_DGRAM, 0);
  1733. if (drv->test_socket < 0) {
  1734. perror("socket(PF_UNIX)");
  1735. os_free(drv->own_socket_path);
  1736. drv->own_socket_path = NULL;
  1737. return -1;
  1738. }
  1739. os_memset(&addr, 0, sizeof(addr));
  1740. addr.sun_family = AF_UNIX;
  1741. os_strlcpy(addr.sun_path, drv->own_socket_path, sizeof(addr.sun_path));
  1742. if (bind(drv->test_socket, (struct sockaddr *) &addr,
  1743. sizeof(addr)) < 0) {
  1744. perror("bind(PF_UNIX)");
  1745. close(drv->test_socket);
  1746. unlink(drv->own_socket_path);
  1747. os_free(drv->own_socket_path);
  1748. drv->own_socket_path = NULL;
  1749. return -1;
  1750. }
  1751. eloop_register_read_sock(drv->test_socket,
  1752. wpa_driver_test_receive_unix, drv, NULL);
  1753. return 0;
  1754. #else /* DRIVER_TEST_UNIX */
  1755. return -1;
  1756. #endif /* DRIVER_TEST_UNIX */
  1757. }
  1758. static int wpa_driver_test_attach_udp(struct wpa_driver_test_data *drv,
  1759. char *dst)
  1760. {
  1761. char *pos;
  1762. pos = os_strchr(dst, ':');
  1763. if (pos == NULL)
  1764. return -1;
  1765. *pos++ = '\0';
  1766. wpa_printf(MSG_DEBUG, "%s: addr=%s port=%s", __func__, dst, pos);
  1767. drv->test_socket = socket(PF_INET, SOCK_DGRAM, 0);
  1768. if (drv->test_socket < 0) {
  1769. perror("socket(PF_INET)");
  1770. return -1;
  1771. }
  1772. os_memset(&drv->hostapd_addr_udp, 0, sizeof(drv->hostapd_addr_udp));
  1773. drv->hostapd_addr_udp.sin_family = AF_INET;
  1774. #if defined(CONFIG_NATIVE_WINDOWS) || defined(CONFIG_ANSI_C_EXTRA)
  1775. {
  1776. int a[4];
  1777. u8 *pos;
  1778. sscanf(dst, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]);
  1779. pos = (u8 *) &drv->hostapd_addr_udp.sin_addr;
  1780. *pos++ = a[0];
  1781. *pos++ = a[1];
  1782. *pos++ = a[2];
  1783. *pos++ = a[3];
  1784. }
  1785. #else /* CONFIG_NATIVE_WINDOWS or CONFIG_ANSI_C_EXTRA */
  1786. inet_aton(dst, &drv->hostapd_addr_udp.sin_addr);
  1787. #endif /* CONFIG_NATIVE_WINDOWS or CONFIG_ANSI_C_EXTRA */
  1788. drv->hostapd_addr_udp.sin_port = htons(atoi(pos));
  1789. drv->hostapd_addr_udp_set = 1;
  1790. eloop_register_read_sock(drv->test_socket,
  1791. wpa_driver_test_receive_unix, drv, NULL);
  1792. return 0;
  1793. }
  1794. static int wpa_driver_test_set_param(void *priv, const char *param)
  1795. {
  1796. struct wpa_driver_test_data *drv = priv;
  1797. const char *pos;
  1798. wpa_printf(MSG_DEBUG, "%s: param='%s'", __func__, param);
  1799. if (param == NULL)
  1800. return 0;
  1801. wpa_driver_test_close_test_socket(drv);
  1802. #ifdef DRIVER_TEST_UNIX
  1803. pos = os_strstr(param, "test_socket=");
  1804. if (pos) {
  1805. const char *pos2;
  1806. size_t len;
  1807. pos += 12;
  1808. pos2 = os_strchr(pos, ' ');
  1809. if (pos2)
  1810. len = pos2 - pos;
  1811. else
  1812. len = os_strlen(pos);
  1813. if (len > sizeof(drv->hostapd_addr.sun_path))
  1814. return -1;
  1815. os_memset(&drv->hostapd_addr, 0, sizeof(drv->hostapd_addr));
  1816. drv->hostapd_addr.sun_family = AF_UNIX;
  1817. os_memcpy(drv->hostapd_addr.sun_path, pos, len);
  1818. drv->hostapd_addr_set = 1;
  1819. }
  1820. #endif /* DRIVER_TEST_UNIX */
  1821. pos = os_strstr(param, "test_dir=");
  1822. if (pos) {
  1823. char *end;
  1824. os_free(drv->test_dir);
  1825. drv->test_dir = os_strdup(pos + 9);
  1826. if (drv->test_dir == NULL)
  1827. return -1;
  1828. end = os_strchr(drv->test_dir, ' ');
  1829. if (end)
  1830. *end = '\0';
  1831. if (wpa_driver_test_attach(drv, drv->test_dir, 0))
  1832. return -1;
  1833. } else {
  1834. pos = os_strstr(param, "test_udp=");
  1835. if (pos) {
  1836. char *dst, *epos;
  1837. dst = os_strdup(pos + 9);
  1838. if (dst == NULL)
  1839. return -1;
  1840. epos = os_strchr(dst, ' ');
  1841. if (epos)
  1842. *epos = '\0';
  1843. if (wpa_driver_test_attach_udp(drv, dst))
  1844. return -1;
  1845. os_free(dst);
  1846. } else if (wpa_driver_test_attach(drv, NULL, 0))
  1847. return -1;
  1848. }
  1849. if (os_strstr(param, "use_associnfo=1")) {
  1850. wpa_printf(MSG_DEBUG, "test_driver: Use AssocInfo events");
  1851. drv->use_associnfo = 1;
  1852. }
  1853. #ifdef CONFIG_CLIENT_MLME
  1854. if (os_strstr(param, "use_mlme=1")) {
  1855. wpa_printf(MSG_DEBUG, "test_driver: Use internal MLME");
  1856. drv->use_mlme = 1;
  1857. }
  1858. #endif /* CONFIG_CLIENT_MLME */
  1859. return 0;
  1860. }
  1861. static const u8 * wpa_driver_test_get_mac_addr(void *priv)
  1862. {
  1863. struct wpa_driver_test_data *drv = priv;
  1864. wpa_printf(MSG_DEBUG, "%s", __func__);
  1865. return drv->own_addr;
  1866. }
  1867. static int wpa_driver_test_send_eapol(void *priv, const u8 *dest, u16 proto,
  1868. const u8 *data, size_t data_len)
  1869. {
  1870. struct wpa_driver_test_data *drv = priv;
  1871. char *msg;
  1872. size_t msg_len;
  1873. struct l2_ethhdr eth;
  1874. struct sockaddr *addr;
  1875. socklen_t alen;
  1876. #ifdef DRIVER_TEST_UNIX
  1877. struct sockaddr_un addr_un;
  1878. #endif /* DRIVER_TEST_UNIX */
  1879. wpa_hexdump(MSG_MSGDUMP, "test_send_eapol TX frame", data, data_len);
  1880. os_memset(&eth, 0, sizeof(eth));
  1881. os_memcpy(eth.h_dest, dest, ETH_ALEN);
  1882. os_memcpy(eth.h_source, drv->own_addr, ETH_ALEN);
  1883. eth.h_proto = host_to_be16(proto);
  1884. msg_len = 6 + sizeof(eth) + data_len;
  1885. msg = os_malloc(msg_len);
  1886. if (msg == NULL)
  1887. return -1;
  1888. os_memcpy(msg, "EAPOL ", 6);
  1889. os_memcpy(msg + 6, &eth, sizeof(eth));
  1890. os_memcpy(msg + 6 + sizeof(eth), data, data_len);
  1891. if (os_memcmp(dest, drv->bssid, ETH_ALEN) == 0 ||
  1892. drv->test_dir == NULL) {
  1893. if (drv->hostapd_addr_udp_set) {
  1894. addr = (struct sockaddr *) &drv->hostapd_addr_udp;
  1895. alen = sizeof(drv->hostapd_addr_udp);
  1896. } else {
  1897. #ifdef DRIVER_TEST_UNIX
  1898. addr = (struct sockaddr *) &drv->hostapd_addr;
  1899. alen = sizeof(drv->hostapd_addr);
  1900. #else /* DRIVER_TEST_UNIX */
  1901. os_free(msg);
  1902. return -1;
  1903. #endif /* DRIVER_TEST_UNIX */
  1904. }
  1905. } else {
  1906. #ifdef DRIVER_TEST_UNIX
  1907. struct stat st;
  1908. os_memset(&addr_un, 0, sizeof(addr_un));
  1909. addr_un.sun_family = AF_UNIX;
  1910. os_snprintf(addr_un.sun_path, sizeof(addr_un.sun_path),
  1911. "%s/STA-" MACSTR, drv->test_dir, MAC2STR(dest));
  1912. if (stat(addr_un.sun_path, &st) < 0) {
  1913. os_snprintf(addr_un.sun_path, sizeof(addr_un.sun_path),
  1914. "%s/AP-" MACSTR,
  1915. drv->test_dir, MAC2STR(dest));
  1916. }
  1917. addr = (struct sockaddr *) &addr_un;
  1918. alen = sizeof(addr_un);
  1919. #else /* DRIVER_TEST_UNIX */
  1920. os_free(msg);
  1921. return -1;
  1922. #endif /* DRIVER_TEST_UNIX */
  1923. }
  1924. if (sendto(drv->test_socket, msg, msg_len, 0, addr, alen) < 0) {
  1925. perror("sendmsg(test_socket)");
  1926. os_free(msg);
  1927. return -1;
  1928. }
  1929. os_free(msg);
  1930. return 0;
  1931. }
  1932. static int wpa_driver_test_get_capa(void *priv, struct wpa_driver_capa *capa)
  1933. {
  1934. struct wpa_driver_test_data *drv = priv;
  1935. os_memset(capa, 0, sizeof(*capa));
  1936. capa->key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  1937. WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
  1938. WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
  1939. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK |
  1940. WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE |
  1941. WPA_DRIVER_CAPA_KEY_MGMT_FT |
  1942. WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
  1943. capa->enc = WPA_DRIVER_CAPA_ENC_WEP40 |
  1944. WPA_DRIVER_CAPA_ENC_WEP104 |
  1945. WPA_DRIVER_CAPA_ENC_TKIP |
  1946. WPA_DRIVER_CAPA_ENC_CCMP;
  1947. capa->auth = WPA_DRIVER_AUTH_OPEN |
  1948. WPA_DRIVER_AUTH_SHARED |
  1949. WPA_DRIVER_AUTH_LEAP;
  1950. if (drv->use_mlme)
  1951. capa->flags |= WPA_DRIVER_FLAGS_USER_SPACE_MLME;
  1952. capa->flags |= WPA_DRIVER_FLAGS_AP;
  1953. capa->max_scan_ssids = 2;
  1954. return 0;
  1955. }
  1956. static int wpa_driver_test_mlme_setprotection(void *priv, const u8 *addr,
  1957. int protect_type,
  1958. int key_type)
  1959. {
  1960. wpa_printf(MSG_DEBUG, "%s: protect_type=%d key_type=%d",
  1961. __func__, protect_type, key_type);
  1962. if (addr) {
  1963. wpa_printf(MSG_DEBUG, "%s: addr=" MACSTR,
  1964. __func__, MAC2STR(addr));
  1965. }
  1966. return 0;
  1967. }
  1968. static int wpa_driver_test_set_channel(void *priv, hostapd_hw_mode phymode,
  1969. int chan, int freq)
  1970. {
  1971. wpa_printf(MSG_DEBUG, "%s: phymode=%d chan=%d freq=%d",
  1972. __func__, phymode, chan, freq);
  1973. return 0;
  1974. }
  1975. static int wpa_driver_test_mlme_add_sta(void *priv, const u8 *addr,
  1976. const u8 *supp_rates,
  1977. size_t supp_rates_len)
  1978. {
  1979. wpa_printf(MSG_DEBUG, "%s: addr=" MACSTR, __func__, MAC2STR(addr));
  1980. return 0;
  1981. }
  1982. static int wpa_driver_test_mlme_remove_sta(void *priv, const u8 *addr)
  1983. {
  1984. wpa_printf(MSG_DEBUG, "%s: addr=" MACSTR, __func__, MAC2STR(addr));
  1985. return 0;
  1986. }
  1987. static int wpa_driver_test_set_ssid(void *priv, const u8 *ssid,
  1988. size_t ssid_len)
  1989. {
  1990. wpa_printf(MSG_DEBUG, "%s", __func__);
  1991. return 0;
  1992. }
  1993. static int wpa_driver_test_set_bssid(void *priv, const u8 *bssid)
  1994. {
  1995. wpa_printf(MSG_DEBUG, "%s: bssid=" MACSTR, __func__, MAC2STR(bssid));
  1996. return 0;
  1997. }
  1998. static void * wpa_driver_test_global_init(void)
  1999. {
  2000. struct wpa_driver_test_global *global;
  2001. global = os_zalloc(sizeof(*global));
  2002. return global;
  2003. }
  2004. static void wpa_driver_test_global_deinit(void *priv)
  2005. {
  2006. struct wpa_driver_test_global *global = priv;
  2007. os_free(global);
  2008. }
  2009. static struct wpa_interface_info *
  2010. wpa_driver_test_get_interfaces(void *global_priv)
  2011. {
  2012. /* struct wpa_driver_test_global *global = priv; */
  2013. struct wpa_interface_info *iface;
  2014. iface = os_zalloc(sizeof(*iface));
  2015. if (iface == NULL)
  2016. return iface;
  2017. iface->ifname = os_strdup("sta0");
  2018. iface->desc = os_strdup("test interface 0");
  2019. iface->drv_name = "test";
  2020. iface->next = os_zalloc(sizeof(*iface));
  2021. if (iface->next) {
  2022. iface->next->ifname = os_strdup("sta1");
  2023. iface->next->desc = os_strdup("test interface 1");
  2024. iface->next->drv_name = "test";
  2025. }
  2026. return iface;
  2027. }
  2028. static struct hostapd_hw_modes *
  2029. wpa_driver_test_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags)
  2030. {
  2031. struct hostapd_hw_modes *modes;
  2032. *num_modes = 3;
  2033. *flags = 0;
  2034. modes = os_zalloc(*num_modes * sizeof(struct hostapd_hw_modes));
  2035. if (modes == NULL)
  2036. return NULL;
  2037. modes[0].mode = HOSTAPD_MODE_IEEE80211G;
  2038. modes[0].num_channels = 1;
  2039. modes[0].num_rates = 1;
  2040. modes[0].channels = os_zalloc(sizeof(struct hostapd_channel_data));
  2041. modes[0].rates = os_zalloc(sizeof(struct hostapd_rate_data));
  2042. if (modes[0].channels == NULL || modes[0].rates == NULL)
  2043. goto fail;
  2044. modes[0].channels[0].chan = 1;
  2045. modes[0].channels[0].freq = 2412;
  2046. modes[0].channels[0].flag = 0;
  2047. modes[0].rates[0].rate = 10;
  2048. modes[0].rates[0].flags = HOSTAPD_RATE_BASIC | HOSTAPD_RATE_SUPPORTED |
  2049. HOSTAPD_RATE_CCK | HOSTAPD_RATE_MANDATORY;
  2050. modes[1].mode = HOSTAPD_MODE_IEEE80211B;
  2051. modes[1].num_channels = 1;
  2052. modes[1].num_rates = 1;
  2053. modes[1].channels = os_zalloc(sizeof(struct hostapd_channel_data));
  2054. modes[1].rates = os_zalloc(sizeof(struct hostapd_rate_data));
  2055. if (modes[1].channels == NULL || modes[1].rates == NULL)
  2056. goto fail;
  2057. modes[1].channels[0].chan = 1;
  2058. modes[1].channels[0].freq = 2412;
  2059. modes[1].channels[0].flag = 0;
  2060. modes[1].rates[0].rate = 10;
  2061. modes[1].rates[0].flags = HOSTAPD_RATE_BASIC | HOSTAPD_RATE_SUPPORTED |
  2062. HOSTAPD_RATE_CCK | HOSTAPD_RATE_MANDATORY;
  2063. modes[2].mode = HOSTAPD_MODE_IEEE80211A;
  2064. modes[2].num_channels = 1;
  2065. modes[2].num_rates = 1;
  2066. modes[2].channels = os_zalloc(sizeof(struct hostapd_channel_data));
  2067. modes[2].rates = os_zalloc(sizeof(struct hostapd_rate_data));
  2068. if (modes[2].channels == NULL || modes[2].rates == NULL)
  2069. goto fail;
  2070. modes[2].channels[0].chan = 60;
  2071. modes[2].channels[0].freq = 5300;
  2072. modes[2].channels[0].flag = 0;
  2073. modes[2].rates[0].rate = 60;
  2074. modes[2].rates[0].flags = HOSTAPD_RATE_BASIC | HOSTAPD_RATE_SUPPORTED |
  2075. HOSTAPD_RATE_MANDATORY;
  2076. return modes;
  2077. fail:
  2078. if (modes) {
  2079. size_t i;
  2080. for (i = 0; i < *num_modes; i++) {
  2081. os_free(modes[i].channels);
  2082. os_free(modes[i].rates);
  2083. }
  2084. os_free(modes);
  2085. }
  2086. return NULL;
  2087. }
  2088. const struct wpa_driver_ops wpa_driver_test_ops = {
  2089. "test",
  2090. "wpa_supplicant test driver",
  2091. .hapd_init = test_driver_init,
  2092. .hapd_deinit = wpa_driver_test_deinit,
  2093. .hapd_send_eapol = test_driver_send_eapol,
  2094. .send_mlme = wpa_driver_test_send_mlme,
  2095. .set_generic_elem = test_driver_set_generic_elem,
  2096. .sta_deauth = test_driver_sta_deauth,
  2097. .sta_disassoc = test_driver_sta_disassoc,
  2098. .get_hw_feature_data = wpa_driver_test_get_hw_feature_data,
  2099. .bss_add = test_driver_bss_add,
  2100. .bss_remove = test_driver_bss_remove,
  2101. .if_add = test_driver_if_add,
  2102. .if_update = test_driver_if_update,
  2103. .if_remove = test_driver_if_remove,
  2104. .valid_bss_mask = test_driver_valid_bss_mask,
  2105. .hapd_set_ssid = test_driver_set_ssid,
  2106. .set_privacy = test_driver_set_privacy,
  2107. .set_sta_vlan = test_driver_set_sta_vlan,
  2108. .sta_add = test_driver_sta_add,
  2109. .send_ether = test_driver_send_ether,
  2110. .set_wps_beacon_ie = test_driver_set_wps_beacon_ie,
  2111. .set_wps_probe_resp_ie = test_driver_set_wps_probe_resp_ie,
  2112. .get_bssid = wpa_driver_test_get_bssid,
  2113. .get_ssid = wpa_driver_test_get_ssid,
  2114. .set_key = wpa_driver_test_set_key,
  2115. .deinit = wpa_driver_test_deinit,
  2116. .set_param = wpa_driver_test_set_param,
  2117. .deauthenticate = wpa_driver_test_deauthenticate,
  2118. .disassociate = wpa_driver_test_disassociate,
  2119. .associate = wpa_driver_test_associate,
  2120. .get_capa = wpa_driver_test_get_capa,
  2121. .get_mac_addr = wpa_driver_test_get_mac_addr,
  2122. .send_eapol = wpa_driver_test_send_eapol,
  2123. .mlme_setprotection = wpa_driver_test_mlme_setprotection,
  2124. .set_channel = wpa_driver_test_set_channel,
  2125. .set_ssid = wpa_driver_test_set_ssid,
  2126. .set_bssid = wpa_driver_test_set_bssid,
  2127. .mlme_add_sta = wpa_driver_test_mlme_add_sta,
  2128. .mlme_remove_sta = wpa_driver_test_mlme_remove_sta,
  2129. .get_scan_results2 = wpa_driver_test_get_scan_results2,
  2130. .global_init = wpa_driver_test_global_init,
  2131. .global_deinit = wpa_driver_test_global_deinit,
  2132. .init2 = wpa_driver_test_init2,
  2133. .get_interfaces = wpa_driver_test_get_interfaces,
  2134. .scan2 = wpa_driver_test_scan,
  2135. };