driver_test.c 70 KB

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