driver_nl80211.c 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940
  1. /*
  2. * WPA Supplicant - driver interaction with Linux nl80211/cfg80211
  3. * Copyright (c) 2003-2008, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "includes.h"
  15. #include <sys/ioctl.h>
  16. #include <net/if_arp.h>
  17. #include <netlink/genl/genl.h>
  18. #include <netlink/genl/family.h>
  19. #include <netlink/genl/ctrl.h>
  20. #include "nl80211_copy.h"
  21. #ifdef CONFIG_CLIENT_MLME
  22. #include <netpacket/packet.h>
  23. #include <linux/if_ether.h>
  24. #include "radiotap.h"
  25. #include "radiotap_iter.h"
  26. #endif /* CONFIG_CLIENT_MLME */
  27. #include "wireless_copy.h"
  28. #include "common.h"
  29. #include "driver.h"
  30. #include "eloop.h"
  31. #include "ieee802_11_defs.h"
  32. #ifndef IFF_LOWER_UP
  33. #define IFF_LOWER_UP 0x10000 /* driver signals L1 up */
  34. #endif
  35. #ifndef IFF_DORMANT
  36. #define IFF_DORMANT 0x20000 /* driver signals dormant */
  37. #endif
  38. #ifndef IF_OPER_DORMANT
  39. #define IF_OPER_DORMANT 5
  40. #endif
  41. #ifndef IF_OPER_UP
  42. #define IF_OPER_UP 6
  43. #endif
  44. struct wpa_driver_nl80211_data {
  45. void *ctx;
  46. int event_sock;
  47. int ioctl_sock;
  48. char ifname[IFNAMSIZ + 1];
  49. int ifindex;
  50. int if_removed;
  51. u8 *assoc_req_ies;
  52. size_t assoc_req_ies_len;
  53. u8 *assoc_resp_ies;
  54. size_t assoc_resp_ies_len;
  55. struct wpa_driver_capa capa;
  56. int has_capability;
  57. int we_version_compiled;
  58. /* for set_auth_alg fallback */
  59. int use_crypt;
  60. int auth_alg_fallback;
  61. int operstate;
  62. char mlmedev[IFNAMSIZ + 1];
  63. int scan_complete_events;
  64. struct nl_handle *nl_handle;
  65. struct nl_cache *nl_cache;
  66. struct nl_cb *nl_cb;
  67. struct genl_family *nl80211;
  68. #ifdef CONFIG_CLIENT_MLME
  69. int monitor_sock; /* socket for monitor */
  70. int monitor_ifidx;
  71. #endif /* CONFIG_CLIENT_MLME */
  72. };
  73. static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx,
  74. void *timeout_ctx);
  75. static int wpa_driver_nl80211_set_mode(void *priv, int mode);
  76. static int wpa_driver_nl80211_flush_pmkid(void *priv);
  77. static int wpa_driver_nl80211_get_range(void *priv);
  78. static void
  79. wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv);
  80. /* nl80211 code */
  81. static int ack_handler(struct nl_msg *msg, void *arg)
  82. {
  83. int *err = arg;
  84. *err = 0;
  85. return NL_STOP;
  86. }
  87. static int finish_handler(struct nl_msg *msg, void *arg)
  88. {
  89. int *ret = arg;
  90. *ret = 0;
  91. return NL_SKIP;
  92. }
  93. static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
  94. void *arg)
  95. {
  96. int *ret = arg;
  97. *ret = err->error;
  98. return NL_SKIP;
  99. }
  100. static int send_and_recv_msgs(struct wpa_driver_nl80211_data *drv,
  101. struct nl_msg *msg,
  102. int (*valid_handler)(struct nl_msg *, void *),
  103. void *valid_data)
  104. {
  105. struct nl_cb *cb;
  106. int err = -ENOMEM;
  107. cb = nl_cb_clone(drv->nl_cb);
  108. if (!cb)
  109. goto out;
  110. err = nl_send_auto_complete(drv->nl_handle, msg);
  111. if (err < 0)
  112. goto out;
  113. err = 1;
  114. nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
  115. nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
  116. nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
  117. if (valid_handler)
  118. nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
  119. valid_handler, valid_data);
  120. while (err > 0)
  121. nl_recvmsgs(drv->nl_handle, cb);
  122. out:
  123. nl_cb_put(cb);
  124. nlmsg_free(msg);
  125. return err;
  126. }
  127. static int wpa_driver_nl80211_send_oper_ifla(
  128. struct wpa_driver_nl80211_data *drv,
  129. int linkmode, int operstate)
  130. {
  131. struct {
  132. struct nlmsghdr hdr;
  133. struct ifinfomsg ifinfo;
  134. char opts[16];
  135. } req;
  136. struct rtattr *rta;
  137. static int nl_seq;
  138. ssize_t ret;
  139. os_memset(&req, 0, sizeof(req));
  140. req.hdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
  141. req.hdr.nlmsg_type = RTM_SETLINK;
  142. req.hdr.nlmsg_flags = NLM_F_REQUEST;
  143. req.hdr.nlmsg_seq = ++nl_seq;
  144. req.hdr.nlmsg_pid = 0;
  145. req.ifinfo.ifi_family = AF_UNSPEC;
  146. req.ifinfo.ifi_type = 0;
  147. req.ifinfo.ifi_index = drv->ifindex;
  148. req.ifinfo.ifi_flags = 0;
  149. req.ifinfo.ifi_change = 0;
  150. if (linkmode != -1) {
  151. rta = (struct rtattr *)
  152. ((char *) &req + NLMSG_ALIGN(req.hdr.nlmsg_len));
  153. rta->rta_type = IFLA_LINKMODE;
  154. rta->rta_len = RTA_LENGTH(sizeof(char));
  155. *((char *) RTA_DATA(rta)) = linkmode;
  156. req.hdr.nlmsg_len = NLMSG_ALIGN(req.hdr.nlmsg_len) +
  157. RTA_LENGTH(sizeof(char));
  158. }
  159. if (operstate != -1) {
  160. rta = (struct rtattr *)
  161. ((char *) &req + NLMSG_ALIGN(req.hdr.nlmsg_len));
  162. rta->rta_type = IFLA_OPERSTATE;
  163. rta->rta_len = RTA_LENGTH(sizeof(char));
  164. *((char *) RTA_DATA(rta)) = operstate;
  165. req.hdr.nlmsg_len = NLMSG_ALIGN(req.hdr.nlmsg_len) +
  166. RTA_LENGTH(sizeof(char));
  167. }
  168. wpa_printf(MSG_DEBUG, "WEXT: Operstate: linkmode=%d, operstate=%d",
  169. linkmode, operstate);
  170. ret = send(drv->event_sock, &req, req.hdr.nlmsg_len, 0);
  171. if (ret < 0) {
  172. wpa_printf(MSG_DEBUG, "WEXT: Sending operstate IFLA failed: "
  173. "%s (assume operstate is not supported)",
  174. strerror(errno));
  175. }
  176. return ret < 0 ? -1 : 0;
  177. }
  178. static int wpa_driver_nl80211_set_auth_param(
  179. struct wpa_driver_nl80211_data *drv, int idx, u32 value)
  180. {
  181. struct iwreq iwr;
  182. int ret = 0;
  183. os_memset(&iwr, 0, sizeof(iwr));
  184. os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
  185. iwr.u.param.flags = idx & IW_AUTH_INDEX;
  186. iwr.u.param.value = value;
  187. if (ioctl(drv->ioctl_sock, SIOCSIWAUTH, &iwr) < 0) {
  188. if (errno != EOPNOTSUPP) {
  189. wpa_printf(MSG_DEBUG, "WEXT: SIOCSIWAUTH(param %d "
  190. "value 0x%x) failed: %s)",
  191. idx, value, strerror(errno));
  192. }
  193. ret = errno == EOPNOTSUPP ? -2 : -1;
  194. }
  195. return ret;
  196. }
  197. static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid)
  198. {
  199. struct wpa_driver_nl80211_data *drv = priv;
  200. struct iwreq iwr;
  201. int ret = 0;
  202. os_memset(&iwr, 0, sizeof(iwr));
  203. os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
  204. if (ioctl(drv->ioctl_sock, SIOCGIWAP, &iwr) < 0) {
  205. perror("ioctl[SIOCGIWAP]");
  206. ret = -1;
  207. }
  208. os_memcpy(bssid, iwr.u.ap_addr.sa_data, ETH_ALEN);
  209. return ret;
  210. }
  211. static int wpa_driver_nl80211_set_bssid(void *priv, const u8 *bssid)
  212. {
  213. struct wpa_driver_nl80211_data *drv = priv;
  214. struct iwreq iwr;
  215. int ret = 0;
  216. os_memset(&iwr, 0, sizeof(iwr));
  217. os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
  218. iwr.u.ap_addr.sa_family = ARPHRD_ETHER;
  219. if (bssid)
  220. os_memcpy(iwr.u.ap_addr.sa_data, bssid, ETH_ALEN);
  221. else
  222. os_memset(iwr.u.ap_addr.sa_data, 0, ETH_ALEN);
  223. if (ioctl(drv->ioctl_sock, SIOCSIWAP, &iwr) < 0) {
  224. perror("ioctl[SIOCSIWAP]");
  225. ret = -1;
  226. }
  227. return ret;
  228. }
  229. static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid)
  230. {
  231. struct wpa_driver_nl80211_data *drv = priv;
  232. struct iwreq iwr;
  233. int ret = 0;
  234. os_memset(&iwr, 0, sizeof(iwr));
  235. os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
  236. iwr.u.essid.pointer = (caddr_t) ssid;
  237. iwr.u.essid.length = 32;
  238. if (ioctl(drv->ioctl_sock, SIOCGIWESSID, &iwr) < 0) {
  239. perror("ioctl[SIOCGIWESSID]");
  240. ret = -1;
  241. } else {
  242. ret = iwr.u.essid.length;
  243. if (ret > 32)
  244. ret = 32;
  245. /* Some drivers include nul termination in the SSID, so let's
  246. * remove it here before further processing. WE-21 changes this
  247. * to explicitly require the length _not_ to include nul
  248. * termination. */
  249. if (ret > 0 && ssid[ret - 1] == '\0' &&
  250. drv->we_version_compiled < 21)
  251. ret--;
  252. }
  253. return ret;
  254. }
  255. static int wpa_driver_nl80211_set_ssid(void *priv, const u8 *ssid,
  256. size_t ssid_len)
  257. {
  258. struct wpa_driver_nl80211_data *drv = priv;
  259. struct iwreq iwr;
  260. int ret = 0;
  261. char buf[33];
  262. if (ssid_len > 32)
  263. return -1;
  264. os_memset(&iwr, 0, sizeof(iwr));
  265. os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
  266. /* flags: 1 = ESSID is active, 0 = not (promiscuous) */
  267. iwr.u.essid.flags = (ssid_len != 0);
  268. os_memset(buf, 0, sizeof(buf));
  269. os_memcpy(buf, ssid, ssid_len);
  270. iwr.u.essid.pointer = (caddr_t) buf;
  271. if (drv->we_version_compiled < 21) {
  272. /* For historic reasons, set SSID length to include one extra
  273. * character, C string nul termination, even though SSID is
  274. * really an octet string that should not be presented as a C
  275. * string. Some Linux drivers decrement the length by one and
  276. * can thus end up missing the last octet of the SSID if the
  277. * length is not incremented here. WE-21 changes this to
  278. * explicitly require the length _not_ to include nul
  279. * termination. */
  280. if (ssid_len)
  281. ssid_len++;
  282. }
  283. iwr.u.essid.length = ssid_len;
  284. if (ioctl(drv->ioctl_sock, SIOCSIWESSID, &iwr) < 0) {
  285. perror("ioctl[SIOCSIWESSID]");
  286. ret = -1;
  287. }
  288. return ret;
  289. }
  290. static int wpa_driver_nl80211_set_freq(void *priv, int freq)
  291. {
  292. struct wpa_driver_nl80211_data *drv = priv;
  293. struct iwreq iwr;
  294. int ret = 0;
  295. os_memset(&iwr, 0, sizeof(iwr));
  296. os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
  297. iwr.u.freq.m = freq * 100000;
  298. iwr.u.freq.e = 1;
  299. if (ioctl(drv->ioctl_sock, SIOCSIWFREQ, &iwr) < 0) {
  300. perror("ioctl[SIOCSIWFREQ]");
  301. ret = -1;
  302. }
  303. return ret;
  304. }
  305. static void
  306. wpa_driver_nl80211_event_wireless_custom(void *ctx, char *custom)
  307. {
  308. union wpa_event_data data;
  309. wpa_printf(MSG_MSGDUMP, "WEXT: Custom wireless event: '%s'",
  310. custom);
  311. os_memset(&data, 0, sizeof(data));
  312. /* Host AP driver */
  313. if (os_strncmp(custom, "MLME-MICHAELMICFAILURE.indication", 33) == 0) {
  314. data.michael_mic_failure.unicast =
  315. os_strstr(custom, " unicast ") != NULL;
  316. /* TODO: parse parameters(?) */
  317. wpa_supplicant_event(ctx, EVENT_MICHAEL_MIC_FAILURE, &data);
  318. } else if (os_strncmp(custom, "ASSOCINFO(ReqIEs=", 17) == 0) {
  319. char *spos;
  320. int bytes;
  321. spos = custom + 17;
  322. bytes = strspn(spos, "0123456789abcdefABCDEF");
  323. if (!bytes || (bytes & 1))
  324. return;
  325. bytes /= 2;
  326. data.assoc_info.req_ies = os_malloc(bytes);
  327. if (data.assoc_info.req_ies == NULL)
  328. return;
  329. data.assoc_info.req_ies_len = bytes;
  330. hexstr2bin(spos, data.assoc_info.req_ies, bytes);
  331. spos += bytes * 2;
  332. data.assoc_info.resp_ies = NULL;
  333. data.assoc_info.resp_ies_len = 0;
  334. if (os_strncmp(spos, " RespIEs=", 9) == 0) {
  335. spos += 9;
  336. bytes = strspn(spos, "0123456789abcdefABCDEF");
  337. if (!bytes || (bytes & 1))
  338. goto done;
  339. bytes /= 2;
  340. data.assoc_info.resp_ies = os_malloc(bytes);
  341. if (data.assoc_info.resp_ies == NULL)
  342. goto done;
  343. data.assoc_info.resp_ies_len = bytes;
  344. hexstr2bin(spos, data.assoc_info.resp_ies, bytes);
  345. }
  346. wpa_supplicant_event(ctx, EVENT_ASSOCINFO, &data);
  347. done:
  348. os_free(data.assoc_info.resp_ies);
  349. os_free(data.assoc_info.req_ies);
  350. #ifdef CONFIG_PEERKEY
  351. } else if (os_strncmp(custom, "STKSTART.request=", 17) == 0) {
  352. if (hwaddr_aton(custom + 17, data.stkstart.peer)) {
  353. wpa_printf(MSG_DEBUG, "WEXT: unrecognized "
  354. "STKSTART.request '%s'", custom + 17);
  355. return;
  356. }
  357. wpa_supplicant_event(ctx, EVENT_STKSTART, &data);
  358. #endif /* CONFIG_PEERKEY */
  359. }
  360. }
  361. static int wpa_driver_nl80211_event_wireless_michaelmicfailure(
  362. void *ctx, const char *ev, size_t len)
  363. {
  364. const struct iw_michaelmicfailure *mic;
  365. union wpa_event_data data;
  366. if (len < sizeof(*mic))
  367. return -1;
  368. mic = (const struct iw_michaelmicfailure *) ev;
  369. wpa_printf(MSG_DEBUG, "Michael MIC failure wireless event: "
  370. "flags=0x%x src_addr=" MACSTR, mic->flags,
  371. MAC2STR(mic->src_addr.sa_data));
  372. os_memset(&data, 0, sizeof(data));
  373. data.michael_mic_failure.unicast = !(mic->flags & IW_MICFAILURE_GROUP);
  374. wpa_supplicant_event(ctx, EVENT_MICHAEL_MIC_FAILURE, &data);
  375. return 0;
  376. }
  377. static int wpa_driver_nl80211_event_wireless_pmkidcand(
  378. struct wpa_driver_nl80211_data *drv, const char *ev, size_t len)
  379. {
  380. const struct iw_pmkid_cand *cand;
  381. union wpa_event_data data;
  382. const u8 *addr;
  383. if (len < sizeof(*cand))
  384. return -1;
  385. cand = (const struct iw_pmkid_cand *) ev;
  386. addr = (const u8 *) cand->bssid.sa_data;
  387. wpa_printf(MSG_DEBUG, "PMKID candidate wireless event: "
  388. "flags=0x%x index=%d bssid=" MACSTR, cand->flags,
  389. cand->index, MAC2STR(addr));
  390. os_memset(&data, 0, sizeof(data));
  391. os_memcpy(data.pmkid_candidate.bssid, addr, ETH_ALEN);
  392. data.pmkid_candidate.index = cand->index;
  393. data.pmkid_candidate.preauth = cand->flags & IW_PMKID_CAND_PREAUTH;
  394. wpa_supplicant_event(drv->ctx, EVENT_PMKID_CANDIDATE, &data);
  395. return 0;
  396. }
  397. static int wpa_driver_nl80211_event_wireless_assocreqie(
  398. struct wpa_driver_nl80211_data *drv, const char *ev, int len)
  399. {
  400. if (len < 0)
  401. return -1;
  402. wpa_hexdump(MSG_DEBUG, "AssocReq IE wireless event", (const u8 *) ev,
  403. len);
  404. os_free(drv->assoc_req_ies);
  405. drv->assoc_req_ies = os_malloc(len);
  406. if (drv->assoc_req_ies == NULL) {
  407. drv->assoc_req_ies_len = 0;
  408. return -1;
  409. }
  410. os_memcpy(drv->assoc_req_ies, ev, len);
  411. drv->assoc_req_ies_len = len;
  412. return 0;
  413. }
  414. static int wpa_driver_nl80211_event_wireless_assocrespie(
  415. struct wpa_driver_nl80211_data *drv, const char *ev, int len)
  416. {
  417. if (len < 0)
  418. return -1;
  419. wpa_hexdump(MSG_DEBUG, "AssocResp IE wireless event", (const u8 *) ev,
  420. len);
  421. os_free(drv->assoc_resp_ies);
  422. drv->assoc_resp_ies = os_malloc(len);
  423. if (drv->assoc_resp_ies == NULL) {
  424. drv->assoc_resp_ies_len = 0;
  425. return -1;
  426. }
  427. os_memcpy(drv->assoc_resp_ies, ev, len);
  428. drv->assoc_resp_ies_len = len;
  429. return 0;
  430. }
  431. static void wpa_driver_nl80211_event_assoc_ies(struct wpa_driver_nl80211_data *drv)
  432. {
  433. union wpa_event_data data;
  434. if (drv->assoc_req_ies == NULL && drv->assoc_resp_ies == NULL)
  435. return;
  436. os_memset(&data, 0, sizeof(data));
  437. if (drv->assoc_req_ies) {
  438. data.assoc_info.req_ies = drv->assoc_req_ies;
  439. drv->assoc_req_ies = NULL;
  440. data.assoc_info.req_ies_len = drv->assoc_req_ies_len;
  441. }
  442. if (drv->assoc_resp_ies) {
  443. data.assoc_info.resp_ies = drv->assoc_resp_ies;
  444. drv->assoc_resp_ies = NULL;
  445. data.assoc_info.resp_ies_len = drv->assoc_resp_ies_len;
  446. }
  447. wpa_supplicant_event(drv->ctx, EVENT_ASSOCINFO, &data);
  448. os_free(data.assoc_info.req_ies);
  449. os_free(data.assoc_info.resp_ies);
  450. }
  451. static void wpa_driver_nl80211_event_wireless(struct wpa_driver_nl80211_data *drv,
  452. void *ctx, char *data, int len)
  453. {
  454. struct iw_event iwe_buf, *iwe = &iwe_buf;
  455. char *pos, *end, *custom, *buf;
  456. pos = data;
  457. end = data + len;
  458. while (pos + IW_EV_LCP_LEN <= end) {
  459. /* Event data may be unaligned, so make a local, aligned copy
  460. * before processing. */
  461. os_memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
  462. wpa_printf(MSG_DEBUG, "Wireless event: cmd=0x%x len=%d",
  463. iwe->cmd, iwe->len);
  464. if (iwe->len <= IW_EV_LCP_LEN)
  465. return;
  466. custom = pos + IW_EV_POINT_LEN;
  467. if (drv->we_version_compiled > 18 &&
  468. (iwe->cmd == IWEVMICHAELMICFAILURE ||
  469. iwe->cmd == IWEVCUSTOM ||
  470. iwe->cmd == IWEVASSOCREQIE ||
  471. iwe->cmd == IWEVASSOCRESPIE ||
  472. iwe->cmd == IWEVPMKIDCAND)) {
  473. /* WE-19 removed the pointer from struct iw_point */
  474. char *dpos = (char *) &iwe_buf.u.data.length;
  475. int dlen = dpos - (char *) &iwe_buf;
  476. os_memcpy(dpos, pos + IW_EV_LCP_LEN,
  477. sizeof(struct iw_event) - dlen);
  478. } else {
  479. os_memcpy(&iwe_buf, pos, sizeof(struct iw_event));
  480. custom += IW_EV_POINT_OFF;
  481. }
  482. switch (iwe->cmd) {
  483. case SIOCGIWAP:
  484. wpa_printf(MSG_DEBUG, "Wireless event: new AP: "
  485. MACSTR,
  486. MAC2STR((u8 *) iwe->u.ap_addr.sa_data));
  487. if (is_zero_ether_addr(
  488. (const u8 *) iwe->u.ap_addr.sa_data) ||
  489. os_memcmp(iwe->u.ap_addr.sa_data,
  490. "\x44\x44\x44\x44\x44\x44", ETH_ALEN) ==
  491. 0) {
  492. os_free(drv->assoc_req_ies);
  493. drv->assoc_req_ies = NULL;
  494. os_free(drv->assoc_resp_ies);
  495. drv->assoc_resp_ies = NULL;
  496. wpa_supplicant_event(ctx, EVENT_DISASSOC,
  497. NULL);
  498. } else {
  499. wpa_driver_nl80211_event_assoc_ies(drv);
  500. wpa_supplicant_event(ctx, EVENT_ASSOC, NULL);
  501. }
  502. break;
  503. case IWEVMICHAELMICFAILURE:
  504. wpa_driver_nl80211_event_wireless_michaelmicfailure(
  505. ctx, custom, iwe->u.data.length);
  506. break;
  507. case IWEVCUSTOM:
  508. if (custom + iwe->u.data.length > end)
  509. return;
  510. buf = os_malloc(iwe->u.data.length + 1);
  511. if (buf == NULL)
  512. return;
  513. os_memcpy(buf, custom, iwe->u.data.length);
  514. buf[iwe->u.data.length] = '\0';
  515. wpa_driver_nl80211_event_wireless_custom(ctx, buf);
  516. os_free(buf);
  517. break;
  518. case SIOCGIWSCAN:
  519. drv->scan_complete_events = 1;
  520. eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout,
  521. drv, ctx);
  522. wpa_supplicant_event(ctx, EVENT_SCAN_RESULTS, NULL);
  523. break;
  524. case IWEVASSOCREQIE:
  525. wpa_driver_nl80211_event_wireless_assocreqie(
  526. drv, custom, iwe->u.data.length);
  527. break;
  528. case IWEVASSOCRESPIE:
  529. wpa_driver_nl80211_event_wireless_assocrespie(
  530. drv, custom, iwe->u.data.length);
  531. break;
  532. case IWEVPMKIDCAND:
  533. wpa_driver_nl80211_event_wireless_pmkidcand(
  534. drv, custom, iwe->u.data.length);
  535. break;
  536. }
  537. pos += iwe->len;
  538. }
  539. }
  540. static void wpa_driver_nl80211_event_link(struct wpa_driver_nl80211_data *drv,
  541. void *ctx, char *buf, size_t len,
  542. int del)
  543. {
  544. union wpa_event_data event;
  545. os_memset(&event, 0, sizeof(event));
  546. if (len > sizeof(event.interface_status.ifname))
  547. len = sizeof(event.interface_status.ifname) - 1;
  548. os_memcpy(event.interface_status.ifname, buf, len);
  549. event.interface_status.ievent = del ? EVENT_INTERFACE_REMOVED :
  550. EVENT_INTERFACE_ADDED;
  551. wpa_printf(MSG_DEBUG, "RTM_%sLINK, IFLA_IFNAME: Interface '%s' %s",
  552. del ? "DEL" : "NEW",
  553. event.interface_status.ifname,
  554. del ? "removed" : "added");
  555. if (os_strcmp(drv->ifname, event.interface_status.ifname) == 0) {
  556. if (del)
  557. drv->if_removed = 1;
  558. else
  559. drv->if_removed = 0;
  560. }
  561. wpa_supplicant_event(ctx, EVENT_INTERFACE_STATUS, &event);
  562. }
  563. static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv,
  564. struct nlmsghdr *h)
  565. {
  566. struct ifinfomsg *ifi;
  567. int attrlen, _nlmsg_len, rta_len;
  568. struct rtattr *attr;
  569. ifi = NLMSG_DATA(h);
  570. _nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg));
  571. attrlen = h->nlmsg_len - _nlmsg_len;
  572. if (attrlen < 0)
  573. return 0;
  574. attr = (struct rtattr *) (((char *) ifi) + _nlmsg_len);
  575. rta_len = RTA_ALIGN(sizeof(struct rtattr));
  576. while (RTA_OK(attr, attrlen)) {
  577. if (attr->rta_type == IFLA_IFNAME) {
  578. if (os_strcmp(((char *) attr) + rta_len, drv->ifname)
  579. == 0)
  580. return 1;
  581. else
  582. break;
  583. }
  584. attr = RTA_NEXT(attr, attrlen);
  585. }
  586. return 0;
  587. }
  588. static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv,
  589. int ifindex, struct nlmsghdr *h)
  590. {
  591. if (drv->ifindex == ifindex)
  592. return 1;
  593. if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, h)) {
  594. drv->ifindex = if_nametoindex(drv->ifname);
  595. wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed "
  596. "interface");
  597. wpa_driver_nl80211_finish_drv_init(drv);
  598. return 1;
  599. }
  600. return 0;
  601. }
  602. static void wpa_driver_nl80211_event_rtm_newlink(struct wpa_driver_nl80211_data *drv,
  603. void *ctx, struct nlmsghdr *h,
  604. size_t len)
  605. {
  606. struct ifinfomsg *ifi;
  607. int attrlen, _nlmsg_len, rta_len;
  608. struct rtattr * attr;
  609. if (len < sizeof(*ifi))
  610. return;
  611. ifi = NLMSG_DATA(h);
  612. if (!wpa_driver_nl80211_own_ifindex(drv, ifi->ifi_index, h)) {
  613. wpa_printf(MSG_DEBUG, "Ignore event for foreign ifindex %d",
  614. ifi->ifi_index);
  615. return;
  616. }
  617. wpa_printf(MSG_DEBUG, "RTM_NEWLINK: operstate=%d ifi_flags=0x%x "
  618. "(%s%s%s%s)",
  619. drv->operstate, ifi->ifi_flags,
  620. (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
  621. (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
  622. (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
  623. (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
  624. /*
  625. * Some drivers send the association event before the operup event--in
  626. * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
  627. * fails. This will hit us when wpa_supplicant does not need to do
  628. * IEEE 802.1X authentication
  629. */
  630. if (drv->operstate == 1 &&
  631. (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
  632. !(ifi->ifi_flags & IFF_RUNNING))
  633. wpa_driver_nl80211_send_oper_ifla(drv, -1, IF_OPER_UP);
  634. _nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg));
  635. attrlen = h->nlmsg_len - _nlmsg_len;
  636. if (attrlen < 0)
  637. return;
  638. attr = (struct rtattr *) (((char *) ifi) + _nlmsg_len);
  639. rta_len = RTA_ALIGN(sizeof(struct rtattr));
  640. while (RTA_OK(attr, attrlen)) {
  641. if (attr->rta_type == IFLA_WIRELESS) {
  642. wpa_driver_nl80211_event_wireless(
  643. drv, ctx, ((char *) attr) + rta_len,
  644. attr->rta_len - rta_len);
  645. } else if (attr->rta_type == IFLA_IFNAME) {
  646. wpa_driver_nl80211_event_link(
  647. drv, ctx,
  648. ((char *) attr) + rta_len,
  649. attr->rta_len - rta_len, 0);
  650. }
  651. attr = RTA_NEXT(attr, attrlen);
  652. }
  653. }
  654. static void wpa_driver_nl80211_event_rtm_dellink(struct wpa_driver_nl80211_data *drv,
  655. void *ctx, struct nlmsghdr *h,
  656. size_t len)
  657. {
  658. struct ifinfomsg *ifi;
  659. int attrlen, _nlmsg_len, rta_len;
  660. struct rtattr * attr;
  661. if (len < sizeof(*ifi))
  662. return;
  663. ifi = NLMSG_DATA(h);
  664. _nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg));
  665. attrlen = h->nlmsg_len - _nlmsg_len;
  666. if (attrlen < 0)
  667. return;
  668. attr = (struct rtattr *) (((char *) ifi) + _nlmsg_len);
  669. rta_len = RTA_ALIGN(sizeof(struct rtattr));
  670. while (RTA_OK(attr, attrlen)) {
  671. if (attr->rta_type == IFLA_IFNAME) {
  672. wpa_driver_nl80211_event_link(
  673. drv, ctx,
  674. ((char *) attr) + rta_len,
  675. attr->rta_len - rta_len, 1);
  676. }
  677. attr = RTA_NEXT(attr, attrlen);
  678. }
  679. }
  680. static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
  681. void *sock_ctx)
  682. {
  683. char buf[8192];
  684. int left;
  685. struct sockaddr_nl from;
  686. socklen_t fromlen;
  687. struct nlmsghdr *h;
  688. int max_events = 10;
  689. try_again:
  690. fromlen = sizeof(from);
  691. left = recvfrom(sock, buf, sizeof(buf), MSG_DONTWAIT,
  692. (struct sockaddr *) &from, &fromlen);
  693. if (left < 0) {
  694. if (errno != EINTR && errno != EAGAIN)
  695. perror("recvfrom(netlink)");
  696. return;
  697. }
  698. h = (struct nlmsghdr *) buf;
  699. while (left >= (int) sizeof(*h)) {
  700. int len, plen;
  701. len = h->nlmsg_len;
  702. plen = len - sizeof(*h);
  703. if (len > left || plen < 0) {
  704. wpa_printf(MSG_DEBUG, "Malformed netlink message: "
  705. "len=%d left=%d plen=%d",
  706. len, left, plen);
  707. break;
  708. }
  709. switch (h->nlmsg_type) {
  710. case RTM_NEWLINK:
  711. wpa_driver_nl80211_event_rtm_newlink(eloop_ctx, sock_ctx,
  712. h, plen);
  713. break;
  714. case RTM_DELLINK:
  715. wpa_driver_nl80211_event_rtm_dellink(eloop_ctx, sock_ctx,
  716. h, plen);
  717. break;
  718. }
  719. len = NLMSG_ALIGN(len);
  720. left -= len;
  721. h = (struct nlmsghdr *) ((char *) h + len);
  722. }
  723. if (left > 0) {
  724. wpa_printf(MSG_DEBUG, "%d extra bytes in the end of netlink "
  725. "message", left);
  726. }
  727. if (--max_events > 0) {
  728. /*
  729. * Try to receive all events in one eloop call in order to
  730. * limit race condition on cases where AssocInfo event, Assoc
  731. * event, and EAPOL frames are received more or less at the
  732. * same time. We want to process the event messages first
  733. * before starting EAPOL processing.
  734. */
  735. goto try_again;
  736. }
  737. }
  738. static int wpa_driver_nl80211_get_ifflags_ifname(struct wpa_driver_nl80211_data *drv,
  739. const char *ifname, int *flags)
  740. {
  741. struct ifreq ifr;
  742. os_memset(&ifr, 0, sizeof(ifr));
  743. os_strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
  744. if (ioctl(drv->ioctl_sock, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
  745. perror("ioctl[SIOCGIFFLAGS]");
  746. return -1;
  747. }
  748. *flags = ifr.ifr_flags & 0xffff;
  749. return 0;
  750. }
  751. /**
  752. * wpa_driver_nl80211_get_ifflags - Get interface flags (SIOCGIFFLAGS)
  753. * @drv: driver_nl80211 private data
  754. * @flags: Pointer to returned flags value
  755. * Returns: 0 on success, -1 on failure
  756. */
  757. static int wpa_driver_nl80211_get_ifflags(struct wpa_driver_nl80211_data *drv,
  758. int *flags)
  759. {
  760. return wpa_driver_nl80211_get_ifflags_ifname(drv, drv->ifname, flags);
  761. }
  762. static int wpa_driver_nl80211_set_ifflags_ifname(
  763. struct wpa_driver_nl80211_data *drv,
  764. const char *ifname, int flags)
  765. {
  766. struct ifreq ifr;
  767. os_memset(&ifr, 0, sizeof(ifr));
  768. os_strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
  769. ifr.ifr_flags = flags & 0xffff;
  770. if (ioctl(drv->ioctl_sock, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
  771. perror("SIOCSIFFLAGS");
  772. return -1;
  773. }
  774. return 0;
  775. }
  776. /**
  777. * wpa_driver_nl80211_set_ifflags - Set interface flags (SIOCSIFFLAGS)
  778. * @drv: driver_nl80211 private data
  779. * @flags: New value for flags
  780. * Returns: 0 on success, -1 on failure
  781. */
  782. static int wpa_driver_nl80211_set_ifflags(struct wpa_driver_nl80211_data *drv,
  783. int flags)
  784. {
  785. return wpa_driver_nl80211_set_ifflags_ifname(drv, drv->ifname, flags);
  786. }
  787. /**
  788. * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
  789. * @priv: driver_nl80211 private data
  790. * @alpha2_arg: country to which to switch to
  791. * Returns: 0 on success, -1 on failure
  792. *
  793. * This asks nl80211 to set the regulatory domain for given
  794. * country ISO / IEC alpha2.
  795. */
  796. static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
  797. {
  798. struct wpa_driver_nl80211_data *drv = priv;
  799. char alpha2[3];
  800. struct nl_msg *msg;
  801. msg = nlmsg_alloc();
  802. if (!msg)
  803. goto nla_put_failure;
  804. alpha2[0] = alpha2_arg[0];
  805. alpha2[1] = alpha2_arg[1];
  806. alpha2[2] = '\0';
  807. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  808. 0, NL80211_CMD_REQ_SET_REG, 0);
  809. NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2);
  810. if (send_and_recv_msgs(drv, msg, NULL, NULL))
  811. return -EINVAL;
  812. return 0;
  813. nla_put_failure:
  814. return -EINVAL;
  815. }
  816. static int wpa_driver_nl80211_set_probe_req_ie(void *priv, const u8 *ies,
  817. size_t ies_len)
  818. {
  819. struct wpa_driver_nl80211_data *drv = priv;
  820. struct nl_msg *msg;
  821. int ret = -1;
  822. msg = nlmsg_alloc();
  823. if (!msg)
  824. return -ENOMEM;
  825. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  826. NL80211_CMD_SET_MGMT_EXTRA_IE, 0);
  827. NLA_PUT_U8(msg, NL80211_ATTR_MGMT_SUBTYPE, 4 /* ProbeReq */);
  828. if (ies)
  829. NLA_PUT(msg, NL80211_ATTR_IE, ies_len, ies);
  830. ret = 0;
  831. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  832. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  833. return ret;
  834. nla_put_failure:
  835. return -ENOBUFS;
  836. }
  837. #ifdef CONFIG_CLIENT_MLME
  838. static int nl80211_set_vif(struct wpa_driver_nl80211_data *drv,
  839. int drop_unencrypted, int userspace_mlme)
  840. {
  841. #ifdef NL80211_CMD_SET_VIF
  842. struct nl_msg *msg;
  843. int ret = -1;
  844. msg = nlmsg_alloc();
  845. if (!msg)
  846. return -ENOMEM;
  847. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  848. NL80211_CMD_SET_VIF, 0);
  849. if (drop_unencrypted >= 0)
  850. NLA_PUT_U8(msg, NL80211_ATTR_VIF_DROP_UNENCRYPTED,
  851. drop_unencrypted);
  852. if (userspace_mlme >= 0)
  853. NLA_PUT_U8(msg, NL80211_ATTR_VIF_USERSPACE_MLME,
  854. userspace_mlme);
  855. ret = 0;
  856. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  857. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  858. return ret;
  859. nla_put_failure:
  860. return -ENOBUFS;
  861. #else /* NL80211_CMD_SET_VIF */
  862. return -1;
  863. #endif /* NL80211_CMD_SET_VIF */
  864. }
  865. static int wpa_driver_nl80211_set_userspace_mlme(
  866. struct wpa_driver_nl80211_data *drv, int enabled)
  867. {
  868. return nl80211_set_vif(drv, -1, enabled);
  869. }
  870. static void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv,
  871. int ifidx)
  872. {
  873. struct nl_msg *msg;
  874. msg = nlmsg_alloc();
  875. if (!msg)
  876. goto nla_put_failure;
  877. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  878. 0, NL80211_CMD_DEL_INTERFACE, 0);
  879. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx);
  880. if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
  881. return;
  882. nla_put_failure:
  883. wpa_printf(MSG_ERROR, "nl80211: Failed to remove interface.");
  884. }
  885. static int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
  886. const char *ifname, enum nl80211_iftype iftype)
  887. {
  888. struct nl_msg *msg, *flags = NULL;
  889. int ifidx, err;
  890. int ret = -ENOBUFS;
  891. msg = nlmsg_alloc();
  892. if (!msg)
  893. return -1;
  894. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  895. 0, NL80211_CMD_NEW_INTERFACE, 0);
  896. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->ifname));
  897. NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname);
  898. NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
  899. if (iftype == NL80211_IFTYPE_MONITOR) {
  900. flags = nlmsg_alloc();
  901. if (!flags)
  902. goto nla_put_failure;
  903. NLA_PUT_FLAG(flags, NL80211_MNTR_FLAG_COOK_FRAMES);
  904. err = nla_put_nested(msg, NL80211_ATTR_MNTR_FLAGS, flags);
  905. nlmsg_free(flags);
  906. if (err)
  907. goto nla_put_failure;
  908. }
  909. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  910. if (ret) {
  911. nla_put_failure:
  912. wpa_printf(MSG_ERROR, "nl80211: Failed to create interface %d",
  913. ret);
  914. return ret;
  915. }
  916. ifidx = if_nametoindex(ifname);
  917. if (ifidx <= 0)
  918. return -1;
  919. return ifidx;
  920. }
  921. static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
  922. {
  923. struct wpa_driver_nl80211_data *drv = eloop_ctx;
  924. int len;
  925. unsigned char buf[3000];
  926. struct ieee80211_radiotap_iterator iter;
  927. int ret;
  928. int injected = 0, failed = 0, rxflags = 0;
  929. struct ieee80211_rx_status rx_status;
  930. len = recv(sock, buf, sizeof(buf), 0);
  931. if (len < 0) {
  932. perror("recv");
  933. return;
  934. }
  935. if (ieee80211_radiotap_iterator_init(&iter, (void *) buf, len)) {
  936. wpa_printf(MSG_DEBUG, "nl80211: received invalid radiotap "
  937. "frame");
  938. return;
  939. }
  940. os_memset(&rx_status, 0, sizeof(rx_status));
  941. while (1) {
  942. ret = ieee80211_radiotap_iterator_next(&iter);
  943. if (ret == -ENOENT)
  944. break;
  945. if (ret) {
  946. wpa_printf(MSG_DEBUG, "nl80211: received invalid "
  947. "radiotap frame (%d)", ret);
  948. return;
  949. }
  950. switch (iter.this_arg_index) {
  951. case IEEE80211_RADIOTAP_FLAGS:
  952. if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
  953. len -= 4;
  954. break;
  955. case IEEE80211_RADIOTAP_RX_FLAGS:
  956. rxflags = 1;
  957. break;
  958. case IEEE80211_RADIOTAP_TX_FLAGS:
  959. injected = 1;
  960. failed = le_to_host16((*(u16 *) iter.this_arg)) &
  961. IEEE80211_RADIOTAP_F_TX_FAIL;
  962. break;
  963. case IEEE80211_RADIOTAP_DATA_RETRIES:
  964. break;
  965. case IEEE80211_RADIOTAP_CHANNEL:
  966. /* TODO convert from freq/flags to channel number
  967. * rx_status.channel = XXX;
  968. */
  969. break;
  970. case IEEE80211_RADIOTAP_RATE:
  971. break;
  972. case IEEE80211_RADIOTAP_DB_ANTSIGNAL:
  973. rx_status.ssi = *iter.this_arg;
  974. break;
  975. }
  976. }
  977. if (rxflags && injected)
  978. return;
  979. if (!injected) {
  980. wpa_supplicant_sta_rx(drv->ctx, buf + iter.max_length,
  981. len - iter.max_length, &rx_status);
  982. } else if (failed) {
  983. /* TX failure callback */
  984. } else {
  985. /* TX success (ACK) callback */
  986. }
  987. }
  988. static int wpa_driver_nl80211_create_monitor_interface(
  989. struct wpa_driver_nl80211_data *drv)
  990. {
  991. char buf[IFNAMSIZ];
  992. struct sockaddr_ll ll;
  993. int optval, flags;
  994. socklen_t optlen;
  995. os_snprintf(buf, IFNAMSIZ, "mon.%s", drv->ifname);
  996. buf[IFNAMSIZ - 1] = '\0';
  997. drv->monitor_ifidx =
  998. nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR);
  999. if (drv->monitor_ifidx < 0)
  1000. return -1;
  1001. if (wpa_driver_nl80211_get_ifflags_ifname(drv, buf, &flags) != 0 ||
  1002. wpa_driver_nl80211_set_ifflags_ifname(drv, buf, flags | IFF_UP) !=
  1003. 0) {
  1004. wpa_printf(MSG_ERROR, "nl80211: Could not set interface '%s' "
  1005. "UP", buf);
  1006. goto error;
  1007. }
  1008. os_memset(&ll, 0, sizeof(ll));
  1009. ll.sll_family = AF_PACKET;
  1010. ll.sll_ifindex = drv->monitor_ifidx;
  1011. drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
  1012. if (drv->monitor_sock < 0) {
  1013. perror("socket[PF_PACKET,SOCK_RAW]");
  1014. goto error;
  1015. }
  1016. if (bind(drv->monitor_sock, (struct sockaddr *) &ll,
  1017. sizeof(ll)) < 0) {
  1018. perror("monitor socket bind");
  1019. goto error;
  1020. }
  1021. optlen = sizeof(optval);
  1022. optval = 20;
  1023. if (setsockopt
  1024. (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
  1025. perror("Failed to set socket priority");
  1026. goto error;
  1027. }
  1028. if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
  1029. drv, NULL)) {
  1030. wpa_printf(MSG_ERROR, "nl80211: Could not register monitor "
  1031. "read socket");
  1032. goto error;
  1033. }
  1034. return 0;
  1035. error:
  1036. nl80211_remove_iface(drv, drv->monitor_ifidx);
  1037. return -1;
  1038. }
  1039. #endif /* CONFIG_CLIENT_MLME */
  1040. /**
  1041. * wpa_driver_nl80211_init - Initialize nl80211 driver interface
  1042. * @ctx: context to be used when calling wpa_supplicant functions,
  1043. * e.g., wpa_supplicant_event()
  1044. * @ifname: interface name, e.g., wlan0
  1045. * Returns: Pointer to private data, %NULL on failure
  1046. */
  1047. static void * wpa_driver_nl80211_init(void *ctx, const char *ifname)
  1048. {
  1049. int s;
  1050. struct sockaddr_nl local;
  1051. struct wpa_driver_nl80211_data *drv;
  1052. drv = os_zalloc(sizeof(*drv));
  1053. if (drv == NULL)
  1054. return NULL;
  1055. drv->ctx = ctx;
  1056. os_strlcpy(drv->ifname, ifname, sizeof(drv->ifname));
  1057. drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
  1058. if (drv->nl_cb == NULL) {
  1059. wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
  1060. "callbacks");
  1061. goto err1;
  1062. }
  1063. drv->nl_handle = nl_handle_alloc_cb(drv->nl_cb);
  1064. if (drv->nl_handle == NULL) {
  1065. wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
  1066. "callbacks");
  1067. goto err2;
  1068. }
  1069. if (genl_connect(drv->nl_handle)) {
  1070. wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic "
  1071. "netlink");
  1072. goto err3;
  1073. }
  1074. drv->nl_cache = genl_ctrl_alloc_cache(drv->nl_handle);
  1075. if (drv->nl_cache == NULL) {
  1076. wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
  1077. "netlink cache");
  1078. goto err3;
  1079. }
  1080. drv->nl80211 = genl_ctrl_search_by_name(drv->nl_cache, "nl80211");
  1081. if (drv->nl80211 == NULL) {
  1082. wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
  1083. "found");
  1084. goto err4;
  1085. }
  1086. drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
  1087. if (drv->ioctl_sock < 0) {
  1088. perror("socket(PF_INET,SOCK_DGRAM)");
  1089. goto err5;
  1090. }
  1091. s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  1092. if (s < 0) {
  1093. perror("socket(PF_NETLINK,SOCK_RAW,NETLINK_ROUTE)");
  1094. goto err6;
  1095. }
  1096. os_memset(&local, 0, sizeof(local));
  1097. local.nl_family = AF_NETLINK;
  1098. local.nl_groups = RTMGRP_LINK;
  1099. if (bind(s, (struct sockaddr *) &local, sizeof(local)) < 0) {
  1100. perror("bind(netlink)");
  1101. close(s);
  1102. goto err6;
  1103. }
  1104. eloop_register_read_sock(s, wpa_driver_nl80211_event_receive, drv,
  1105. ctx);
  1106. drv->event_sock = s;
  1107. wpa_driver_nl80211_finish_drv_init(drv);
  1108. return drv;
  1109. err6:
  1110. close(drv->ioctl_sock);
  1111. err5:
  1112. genl_family_put(drv->nl80211);
  1113. err4:
  1114. nl_cache_free(drv->nl_cache);
  1115. err3:
  1116. nl_handle_destroy(drv->nl_handle);
  1117. err2:
  1118. nl_cb_put(drv->nl_cb);
  1119. err1:
  1120. os_free(drv);
  1121. return NULL;
  1122. }
  1123. static void
  1124. wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv)
  1125. {
  1126. int flags;
  1127. if (wpa_driver_nl80211_get_ifflags(drv, &flags) != 0)
  1128. printf("Could not get interface '%s' flags\n", drv->ifname);
  1129. else if (!(flags & IFF_UP)) {
  1130. if (wpa_driver_nl80211_set_ifflags(drv, flags | IFF_UP) != 0) {
  1131. printf("Could not set interface '%s' UP\n",
  1132. drv->ifname);
  1133. } else {
  1134. /*
  1135. * Wait some time to allow driver to initialize before
  1136. * starting configuring the driver. This seems to be
  1137. * needed at least some drivers that load firmware etc.
  1138. * when the interface is set up.
  1139. */
  1140. wpa_printf(MSG_DEBUG, "Interface %s set UP - waiting "
  1141. "a second for the driver to complete "
  1142. "initialization", drv->ifname);
  1143. sleep(1);
  1144. }
  1145. }
  1146. /*
  1147. * Make sure that the driver does not have any obsolete PMKID entries.
  1148. */
  1149. wpa_driver_nl80211_flush_pmkid(drv);
  1150. if (wpa_driver_nl80211_set_mode(drv, 0) < 0) {
  1151. printf("Could not configure driver to use managed mode\n");
  1152. }
  1153. wpa_driver_nl80211_get_range(drv);
  1154. drv->ifindex = if_nametoindex(drv->ifname);
  1155. wpa_driver_nl80211_send_oper_ifla(drv, 1, IF_OPER_DORMANT);
  1156. }
  1157. /**
  1158. * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
  1159. * @priv: Pointer to private nl80211 data from wpa_driver_nl80211_init()
  1160. *
  1161. * Shut down driver interface and processing of driver events. Free
  1162. * private data buffer if one was allocated in wpa_driver_nl80211_init().
  1163. */
  1164. static void wpa_driver_nl80211_deinit(void *priv)
  1165. {
  1166. struct wpa_driver_nl80211_data *drv = priv;
  1167. int flags;
  1168. #ifdef CONFIG_CLIENT_MLME
  1169. if (drv->monitor_sock >= 0) {
  1170. eloop_unregister_read_sock(drv->monitor_sock);
  1171. close(drv->monitor_sock);
  1172. }
  1173. if (drv->monitor_ifidx > 0)
  1174. nl80211_remove_iface(drv, drv->monitor_ifidx);
  1175. if (drv->capa.flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
  1176. wpa_driver_nl80211_set_userspace_mlme(drv, 0);
  1177. #endif /* CONFIG_CLIENT_MLME */
  1178. eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
  1179. /*
  1180. * Clear possibly configured driver parameters in order to make it
  1181. * easier to use the driver after wpa_supplicant has been terminated.
  1182. */
  1183. (void) wpa_driver_nl80211_set_bssid(drv,
  1184. (u8 *) "\x00\x00\x00\x00\x00\x00");
  1185. wpa_driver_nl80211_send_oper_ifla(priv, 0, IF_OPER_UP);
  1186. eloop_unregister_read_sock(drv->event_sock);
  1187. if (wpa_driver_nl80211_get_ifflags(drv, &flags) == 0)
  1188. (void) wpa_driver_nl80211_set_ifflags(drv, flags & ~IFF_UP);
  1189. close(drv->event_sock);
  1190. close(drv->ioctl_sock);
  1191. os_free(drv->assoc_req_ies);
  1192. os_free(drv->assoc_resp_ies);
  1193. genl_family_put(drv->nl80211);
  1194. nl_cache_free(drv->nl_cache);
  1195. nl_handle_destroy(drv->nl_handle);
  1196. nl_cb_put(drv->nl_cb);
  1197. os_free(drv);
  1198. }
  1199. /**
  1200. * wpa_driver_nl80211_scan_timeout - Scan timeout to report scan completion
  1201. * @eloop_ctx: Unused
  1202. * @timeout_ctx: ctx argument given to wpa_driver_nl80211_init()
  1203. *
  1204. * This function can be used as registered timeout when starting a scan to
  1205. * generate a scan completed event if the driver does not report this.
  1206. */
  1207. static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx, void *timeout_ctx)
  1208. {
  1209. wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
  1210. wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
  1211. }
  1212. /**
  1213. * wpa_driver_nl80211_scan - Request the driver to initiate scan
  1214. * @priv: Pointer to private wext data from wpa_driver_nl80211_init()
  1215. * @ssid: Specific SSID to scan for (ProbeReq) or %NULL to scan for
  1216. * all SSIDs (either active scan with broadcast SSID or passive
  1217. * scan
  1218. * @ssid_len: Length of the SSID
  1219. * Returns: 0 on success, -1 on failure
  1220. */
  1221. static int wpa_driver_nl80211_scan(void *priv, const u8 *ssid, size_t ssid_len)
  1222. {
  1223. struct wpa_driver_nl80211_data *drv = priv;
  1224. struct iwreq iwr;
  1225. int ret = 0, timeout;
  1226. struct iw_scan_req req;
  1227. if (ssid_len > IW_ESSID_MAX_SIZE) {
  1228. wpa_printf(MSG_DEBUG, "%s: too long SSID (%lu)",
  1229. __FUNCTION__, (unsigned long) ssid_len);
  1230. return -1;
  1231. }
  1232. os_memset(&iwr, 0, sizeof(iwr));
  1233. os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
  1234. if (ssid && ssid_len) {
  1235. os_memset(&req, 0, sizeof(req));
  1236. req.essid_len = ssid_len;
  1237. req.bssid.sa_family = ARPHRD_ETHER;
  1238. os_memset(req.bssid.sa_data, 0xff, ETH_ALEN);
  1239. os_memcpy(req.essid, ssid, ssid_len);
  1240. iwr.u.data.pointer = (caddr_t) &req;
  1241. iwr.u.data.length = sizeof(req);
  1242. iwr.u.data.flags = IW_SCAN_THIS_ESSID;
  1243. }
  1244. if (ioctl(drv->ioctl_sock, SIOCSIWSCAN, &iwr) < 0) {
  1245. perror("ioctl[SIOCSIWSCAN]");
  1246. ret = -1;
  1247. }
  1248. /* Not all drivers generate "scan completed" wireless event, so try to
  1249. * read results after a timeout. */
  1250. timeout = 5;
  1251. if (drv->scan_complete_events) {
  1252. /*
  1253. * The driver seems to deliver SIOCGIWSCAN events to notify
  1254. * when scan is complete, so use longer timeout to avoid race
  1255. * conditions with scanning and following association request.
  1256. */
  1257. timeout = 30;
  1258. }
  1259. wpa_printf(MSG_DEBUG, "Scan requested (ret=%d) - scan timeout %d "
  1260. "seconds", ret, timeout);
  1261. eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
  1262. eloop_register_timeout(timeout, 0, wpa_driver_nl80211_scan_timeout, drv,
  1263. drv->ctx);
  1264. return ret;
  1265. }
  1266. static u8 * wpa_driver_nl80211_giwscan(struct wpa_driver_nl80211_data *drv,
  1267. size_t *len)
  1268. {
  1269. struct iwreq iwr;
  1270. u8 *res_buf;
  1271. size_t res_buf_len;
  1272. res_buf_len = IW_SCAN_MAX_DATA;
  1273. for (;;) {
  1274. res_buf = os_malloc(res_buf_len);
  1275. if (res_buf == NULL)
  1276. return NULL;
  1277. os_memset(&iwr, 0, sizeof(iwr));
  1278. os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
  1279. iwr.u.data.pointer = res_buf;
  1280. iwr.u.data.length = res_buf_len;
  1281. if (ioctl(drv->ioctl_sock, SIOCGIWSCAN, &iwr) == 0)
  1282. break;
  1283. if (errno == E2BIG && res_buf_len < 65535) {
  1284. os_free(res_buf);
  1285. res_buf = NULL;
  1286. res_buf_len *= 2;
  1287. if (res_buf_len > 65535)
  1288. res_buf_len = 65535; /* 16-bit length field */
  1289. wpa_printf(MSG_DEBUG, "Scan results did not fit - "
  1290. "trying larger buffer (%lu bytes)",
  1291. (unsigned long) res_buf_len);
  1292. } else {
  1293. perror("ioctl[SIOCGIWSCAN]");
  1294. os_free(res_buf);
  1295. return NULL;
  1296. }
  1297. }
  1298. if (iwr.u.data.length > res_buf_len) {
  1299. os_free(res_buf);
  1300. return NULL;
  1301. }
  1302. *len = iwr.u.data.length;
  1303. return res_buf;
  1304. }
  1305. /*
  1306. * Data structure for collecting WEXT scan results. This is needed to allow
  1307. * the various methods of reporting IEs to be combined into a single IE buffer.
  1308. */
  1309. struct wext_scan_data {
  1310. struct wpa_scan_res res;
  1311. u8 *ie;
  1312. size_t ie_len;
  1313. u8 ssid[32];
  1314. size_t ssid_len;
  1315. int maxrate;
  1316. };
  1317. static void wext_get_scan_mode(struct iw_event *iwe,
  1318. struct wext_scan_data *res)
  1319. {
  1320. if (iwe->u.mode == IW_MODE_ADHOC)
  1321. res->res.caps |= IEEE80211_CAP_IBSS;
  1322. else if (iwe->u.mode == IW_MODE_MASTER || iwe->u.mode == IW_MODE_INFRA)
  1323. res->res.caps |= IEEE80211_CAP_ESS;
  1324. }
  1325. static void wext_get_scan_ssid(struct iw_event *iwe,
  1326. struct wext_scan_data *res, char *custom,
  1327. char *end)
  1328. {
  1329. int ssid_len = iwe->u.essid.length;
  1330. if (custom + ssid_len > end)
  1331. return;
  1332. if (iwe->u.essid.flags &&
  1333. ssid_len > 0 &&
  1334. ssid_len <= IW_ESSID_MAX_SIZE) {
  1335. os_memcpy(res->ssid, custom, ssid_len);
  1336. res->ssid_len = ssid_len;
  1337. }
  1338. }
  1339. static void wext_get_scan_freq(struct iw_event *iwe,
  1340. struct wext_scan_data *res)
  1341. {
  1342. int divi = 1000000, i;
  1343. if (iwe->u.freq.e == 0) {
  1344. /*
  1345. * Some drivers do not report frequency, but a channel.
  1346. * Try to map this to frequency by assuming they are using
  1347. * IEEE 802.11b/g. But don't overwrite a previously parsed
  1348. * frequency if the driver sends both frequency and channel,
  1349. * since the driver may be sending an A-band channel that we
  1350. * don't handle here.
  1351. */
  1352. if (res->res.freq)
  1353. return;
  1354. if (iwe->u.freq.m >= 1 && iwe->u.freq.m <= 13) {
  1355. res->res.freq = 2407 + 5 * iwe->u.freq.m;
  1356. return;
  1357. } else if (iwe->u.freq.m == 14) {
  1358. res->res.freq = 2484;
  1359. return;
  1360. }
  1361. }
  1362. if (iwe->u.freq.e > 6) {
  1363. wpa_printf(MSG_DEBUG, "Invalid freq in scan results (BSSID="
  1364. MACSTR " m=%d e=%d)",
  1365. MAC2STR(res->res.bssid), iwe->u.freq.m,
  1366. iwe->u.freq.e);
  1367. return;
  1368. }
  1369. for (i = 0; i < iwe->u.freq.e; i++)
  1370. divi /= 10;
  1371. res->res.freq = iwe->u.freq.m / divi;
  1372. }
  1373. static void wext_get_scan_qual(struct iw_event *iwe,
  1374. struct wext_scan_data *res)
  1375. {
  1376. res->res.qual = iwe->u.qual.qual;
  1377. res->res.noise = iwe->u.qual.noise;
  1378. res->res.level = iwe->u.qual.level;
  1379. }
  1380. static void wext_get_scan_encode(struct iw_event *iwe,
  1381. struct wext_scan_data *res)
  1382. {
  1383. if (!(iwe->u.data.flags & IW_ENCODE_DISABLED))
  1384. res->res.caps |= IEEE80211_CAP_PRIVACY;
  1385. }
  1386. static void wext_get_scan_rate(struct iw_event *iwe,
  1387. struct wext_scan_data *res, char *pos,
  1388. char *end)
  1389. {
  1390. int maxrate;
  1391. char *custom = pos + IW_EV_LCP_LEN;
  1392. struct iw_param p;
  1393. size_t clen;
  1394. clen = iwe->len;
  1395. if (custom + clen > end)
  1396. return;
  1397. maxrate = 0;
  1398. while (((ssize_t) clen) >= (ssize_t) sizeof(struct iw_param)) {
  1399. /* Note: may be misaligned, make a local, aligned copy */
  1400. os_memcpy(&p, custom, sizeof(struct iw_param));
  1401. if (p.value > maxrate)
  1402. maxrate = p.value;
  1403. clen -= sizeof(struct iw_param);
  1404. custom += sizeof(struct iw_param);
  1405. }
  1406. /* Convert the maxrate from WE-style (b/s units) to
  1407. * 802.11 rates (500000 b/s units).
  1408. */
  1409. res->maxrate = maxrate / 500000;
  1410. }
  1411. static void wext_get_scan_iwevgenie(struct iw_event *iwe,
  1412. struct wext_scan_data *res, char *custom,
  1413. char *end)
  1414. {
  1415. char *genie, *gpos, *gend;
  1416. u8 *tmp;
  1417. if (iwe->u.data.length == 0)
  1418. return;
  1419. gpos = genie = custom;
  1420. gend = genie + iwe->u.data.length;
  1421. if (gend > end) {
  1422. wpa_printf(MSG_INFO, "IWEVGENIE overflow");
  1423. return;
  1424. }
  1425. tmp = os_realloc(res->ie, res->ie_len + gend - gpos);
  1426. if (tmp == NULL)
  1427. return;
  1428. os_memcpy(tmp + res->ie_len, gpos, gend - gpos);
  1429. res->ie = tmp;
  1430. res->ie_len += gend - gpos;
  1431. }
  1432. static void wext_get_scan_custom(struct iw_event *iwe,
  1433. struct wext_scan_data *res, char *custom,
  1434. char *end)
  1435. {
  1436. size_t clen;
  1437. u8 *tmp;
  1438. clen = iwe->u.data.length;
  1439. if (custom + clen > end)
  1440. return;
  1441. if (clen > 7 && os_strncmp(custom, "wpa_ie=", 7) == 0) {
  1442. char *spos;
  1443. int bytes;
  1444. spos = custom + 7;
  1445. bytes = custom + clen - spos;
  1446. if (bytes & 1 || bytes == 0)
  1447. return;
  1448. bytes /= 2;
  1449. tmp = os_realloc(res->ie, res->ie_len + bytes);
  1450. if (tmp == NULL)
  1451. return;
  1452. hexstr2bin(spos, tmp + res->ie_len, bytes);
  1453. res->ie = tmp;
  1454. res->ie_len += bytes;
  1455. } else if (clen > 7 && os_strncmp(custom, "rsn_ie=", 7) == 0) {
  1456. char *spos;
  1457. int bytes;
  1458. spos = custom + 7;
  1459. bytes = custom + clen - spos;
  1460. if (bytes & 1 || bytes == 0)
  1461. return;
  1462. bytes /= 2;
  1463. tmp = os_realloc(res->ie, res->ie_len + bytes);
  1464. if (tmp == NULL)
  1465. return;
  1466. hexstr2bin(spos, tmp + res->ie_len, bytes);
  1467. res->ie = tmp;
  1468. res->ie_len += bytes;
  1469. } else if (clen > 4 && os_strncmp(custom, "tsf=", 4) == 0) {
  1470. char *spos;
  1471. int bytes;
  1472. u8 bin[8];
  1473. spos = custom + 4;
  1474. bytes = custom + clen - spos;
  1475. if (bytes != 16) {
  1476. wpa_printf(MSG_INFO, "Invalid TSF length (%d)", bytes);
  1477. return;
  1478. }
  1479. bytes /= 2;
  1480. hexstr2bin(spos, bin, bytes);
  1481. res->res.tsf += WPA_GET_BE64(bin);
  1482. }
  1483. }
  1484. static int wext_19_iw_point(struct wpa_driver_nl80211_data *drv, u16 cmd)
  1485. {
  1486. return drv->we_version_compiled > 18 &&
  1487. (cmd == SIOCGIWESSID || cmd == SIOCGIWENCODE ||
  1488. cmd == IWEVGENIE || cmd == IWEVCUSTOM);
  1489. }
  1490. static void wpa_driver_nl80211_add_scan_entry(struct wpa_scan_results *res,
  1491. struct wext_scan_data *data)
  1492. {
  1493. struct wpa_scan_res **tmp;
  1494. struct wpa_scan_res *r;
  1495. size_t extra_len;
  1496. u8 *pos, *end, *ssid_ie = NULL, *rate_ie = NULL;
  1497. /* Figure out whether we need to fake any IEs */
  1498. pos = data->ie;
  1499. end = pos + data->ie_len;
  1500. while (pos && pos + 1 < end) {
  1501. if (pos + 2 + pos[1] > end)
  1502. break;
  1503. if (pos[0] == WLAN_EID_SSID)
  1504. ssid_ie = pos;
  1505. else if (pos[0] == WLAN_EID_SUPP_RATES)
  1506. rate_ie = pos;
  1507. else if (pos[0] == WLAN_EID_EXT_SUPP_RATES)
  1508. rate_ie = pos;
  1509. pos += 2 + pos[1];
  1510. }
  1511. extra_len = 0;
  1512. if (ssid_ie == NULL)
  1513. extra_len += 2 + data->ssid_len;
  1514. if (rate_ie == NULL && data->maxrate)
  1515. extra_len += 3;
  1516. r = os_zalloc(sizeof(*r) + extra_len + data->ie_len);
  1517. if (r == NULL)
  1518. return;
  1519. os_memcpy(r, &data->res, sizeof(*r));
  1520. r->ie_len = extra_len + data->ie_len;
  1521. pos = (u8 *) (r + 1);
  1522. if (ssid_ie == NULL) {
  1523. /*
  1524. * Generate a fake SSID IE since the driver did not report
  1525. * a full IE list.
  1526. */
  1527. *pos++ = WLAN_EID_SSID;
  1528. *pos++ = data->ssid_len;
  1529. os_memcpy(pos, data->ssid, data->ssid_len);
  1530. pos += data->ssid_len;
  1531. }
  1532. if (rate_ie == NULL && data->maxrate) {
  1533. /*
  1534. * Generate a fake Supported Rates IE since the driver did not
  1535. * report a full IE list.
  1536. */
  1537. *pos++ = WLAN_EID_SUPP_RATES;
  1538. *pos++ = 1;
  1539. *pos++ = data->maxrate;
  1540. }
  1541. if (data->ie)
  1542. os_memcpy(pos, data->ie, data->ie_len);
  1543. tmp = os_realloc(res->res,
  1544. (res->num + 1) * sizeof(struct wpa_scan_res *));
  1545. if (tmp == NULL) {
  1546. os_free(r);
  1547. return;
  1548. }
  1549. tmp[res->num++] = r;
  1550. res->res = tmp;
  1551. }
  1552. /**
  1553. * wpa_driver_nl80211_get_scan_results - Fetch the latest scan results
  1554. * @priv: Pointer to private wext data from wpa_driver_nl80211_init()
  1555. * Returns: Scan results on success, -1 on failure
  1556. */
  1557. static struct wpa_scan_results *
  1558. wpa_driver_nl80211_get_scan_results(void *priv)
  1559. {
  1560. struct wpa_driver_nl80211_data *drv = priv;
  1561. size_t ap_num = 0, len;
  1562. int first;
  1563. u8 *res_buf;
  1564. struct iw_event iwe_buf, *iwe = &iwe_buf;
  1565. char *pos, *end, *custom;
  1566. struct wpa_scan_results *res;
  1567. struct wext_scan_data data;
  1568. res_buf = wpa_driver_nl80211_giwscan(drv, &len);
  1569. if (res_buf == NULL)
  1570. return NULL;
  1571. ap_num = 0;
  1572. first = 1;
  1573. res = os_zalloc(sizeof(*res));
  1574. if (res == NULL) {
  1575. os_free(res_buf);
  1576. return NULL;
  1577. }
  1578. pos = (char *) res_buf;
  1579. end = (char *) res_buf + len;
  1580. os_memset(&data, 0, sizeof(data));
  1581. while (pos + IW_EV_LCP_LEN <= end) {
  1582. /* Event data may be unaligned, so make a local, aligned copy
  1583. * before processing. */
  1584. os_memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
  1585. if (iwe->len <= IW_EV_LCP_LEN)
  1586. break;
  1587. custom = pos + IW_EV_POINT_LEN;
  1588. if (wext_19_iw_point(drv, iwe->cmd)) {
  1589. /* WE-19 removed the pointer from struct iw_point */
  1590. char *dpos = (char *) &iwe_buf.u.data.length;
  1591. int dlen = dpos - (char *) &iwe_buf;
  1592. os_memcpy(dpos, pos + IW_EV_LCP_LEN,
  1593. sizeof(struct iw_event) - dlen);
  1594. } else {
  1595. os_memcpy(&iwe_buf, pos, sizeof(struct iw_event));
  1596. custom += IW_EV_POINT_OFF;
  1597. }
  1598. switch (iwe->cmd) {
  1599. case SIOCGIWAP:
  1600. if (!first)
  1601. wpa_driver_nl80211_add_scan_entry(res, &data);
  1602. first = 0;
  1603. os_free(data.ie);
  1604. os_memset(&data, 0, sizeof(data));
  1605. os_memcpy(data.res.bssid,
  1606. iwe->u.ap_addr.sa_data, ETH_ALEN);
  1607. break;
  1608. case SIOCGIWMODE:
  1609. wext_get_scan_mode(iwe, &data);
  1610. break;
  1611. case SIOCGIWESSID:
  1612. wext_get_scan_ssid(iwe, &data, custom, end);
  1613. break;
  1614. case SIOCGIWFREQ:
  1615. wext_get_scan_freq(iwe, &data);
  1616. break;
  1617. case IWEVQUAL:
  1618. wext_get_scan_qual(iwe, &data);
  1619. break;
  1620. case SIOCGIWENCODE:
  1621. wext_get_scan_encode(iwe, &data);
  1622. break;
  1623. case SIOCGIWRATE:
  1624. wext_get_scan_rate(iwe, &data, pos, end);
  1625. break;
  1626. case IWEVGENIE:
  1627. wext_get_scan_iwevgenie(iwe, &data, custom, end);
  1628. break;
  1629. case IWEVCUSTOM:
  1630. wext_get_scan_custom(iwe, &data, custom, end);
  1631. break;
  1632. }
  1633. pos += iwe->len;
  1634. }
  1635. os_free(res_buf);
  1636. res_buf = NULL;
  1637. if (!first)
  1638. wpa_driver_nl80211_add_scan_entry(res, &data);
  1639. os_free(data.ie);
  1640. wpa_printf(MSG_DEBUG, "Received %lu bytes of scan results (%lu BSSes)",
  1641. (unsigned long) len, (unsigned long) res->num);
  1642. return res;
  1643. }
  1644. static int wpa_driver_nl80211_get_range(void *priv)
  1645. {
  1646. struct wpa_driver_nl80211_data *drv = priv;
  1647. struct iw_range *range;
  1648. struct iwreq iwr;
  1649. int minlen;
  1650. size_t buflen;
  1651. /*
  1652. * Use larger buffer than struct iw_range in order to allow the
  1653. * structure to grow in the future.
  1654. */
  1655. buflen = sizeof(struct iw_range) + 500;
  1656. range = os_zalloc(buflen);
  1657. if (range == NULL)
  1658. return -1;
  1659. os_memset(&iwr, 0, sizeof(iwr));
  1660. os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
  1661. iwr.u.data.pointer = (caddr_t) range;
  1662. iwr.u.data.length = buflen;
  1663. minlen = ((char *) &range->enc_capa) - (char *) range +
  1664. sizeof(range->enc_capa);
  1665. if (ioctl(drv->ioctl_sock, SIOCGIWRANGE, &iwr) < 0) {
  1666. perror("ioctl[SIOCGIWRANGE]");
  1667. os_free(range);
  1668. return -1;
  1669. } else if (iwr.u.data.length >= minlen &&
  1670. range->we_version_compiled >= 18) {
  1671. wpa_printf(MSG_DEBUG, "SIOCGIWRANGE: WE(compiled)=%d "
  1672. "WE(source)=%d enc_capa=0x%x",
  1673. range->we_version_compiled,
  1674. range->we_version_source,
  1675. range->enc_capa);
  1676. drv->has_capability = 1;
  1677. drv->we_version_compiled = range->we_version_compiled;
  1678. if (range->enc_capa & IW_ENC_CAPA_WPA) {
  1679. drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  1680. WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK;
  1681. }
  1682. if (range->enc_capa & IW_ENC_CAPA_WPA2) {
  1683. drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
  1684. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK;
  1685. }
  1686. drv->capa.enc |= WPA_DRIVER_CAPA_ENC_WEP40 |
  1687. WPA_DRIVER_CAPA_ENC_WEP104;
  1688. if (range->enc_capa & IW_ENC_CAPA_CIPHER_TKIP)
  1689. drv->capa.enc |= WPA_DRIVER_CAPA_ENC_TKIP;
  1690. if (range->enc_capa & IW_ENC_CAPA_CIPHER_CCMP)
  1691. drv->capa.enc |= WPA_DRIVER_CAPA_ENC_CCMP;
  1692. wpa_printf(MSG_DEBUG, " capabilities: key_mgmt 0x%x enc 0x%x",
  1693. drv->capa.key_mgmt, drv->capa.enc);
  1694. } else {
  1695. wpa_printf(MSG_DEBUG, "SIOCGIWRANGE: too old (short) data - "
  1696. "assuming WPA is not supported");
  1697. }
  1698. os_free(range);
  1699. return 0;
  1700. }
  1701. static int wpa_driver_nl80211_set_wpa(void *priv, int enabled)
  1702. {
  1703. struct wpa_driver_nl80211_data *drv = priv;
  1704. wpa_printf(MSG_DEBUG, "%s", __FUNCTION__);
  1705. return wpa_driver_nl80211_set_auth_param(drv, IW_AUTH_WPA_ENABLED,
  1706. enabled);
  1707. }
  1708. static int wpa_driver_nl80211_set_key(void *priv, wpa_alg alg,
  1709. const u8 *addr, int key_idx,
  1710. int set_tx, const u8 *seq,
  1711. size_t seq_len,
  1712. const u8 *key, size_t key_len)
  1713. {
  1714. struct wpa_driver_nl80211_data *drv = priv;
  1715. int err;
  1716. struct nl_msg *msg;
  1717. wpa_printf(MSG_DEBUG, "%s: alg=%d addr=%p key_idx=%d set_tx=%d "
  1718. "seq_len=%lu key_len=%lu",
  1719. __func__, alg, addr, key_idx, set_tx,
  1720. (unsigned long) seq_len, (unsigned long) key_len);
  1721. msg = nlmsg_alloc();
  1722. if (msg == NULL)
  1723. return -1;
  1724. if (alg == WPA_ALG_NONE) {
  1725. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  1726. NL80211_CMD_DEL_KEY, 0);
  1727. } else {
  1728. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  1729. NL80211_CMD_NEW_KEY, 0);
  1730. NLA_PUT(msg, NL80211_ATTR_KEY_DATA, key_len, key);
  1731. switch (alg) {
  1732. case WPA_ALG_WEP:
  1733. if (key_len == 5)
  1734. NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
  1735. 0x000FAC01);
  1736. else
  1737. NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
  1738. 0x000FAC05);
  1739. break;
  1740. case WPA_ALG_TKIP:
  1741. NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, 0x000FAC02);
  1742. break;
  1743. case WPA_ALG_CCMP:
  1744. NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, 0x000FAC04);
  1745. break;
  1746. default:
  1747. nlmsg_free(msg);
  1748. return -1;
  1749. }
  1750. }
  1751. if (addr && os_memcmp(addr, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
  1752. {
  1753. wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
  1754. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
  1755. }
  1756. NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
  1757. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  1758. err = send_and_recv_msgs(drv, msg, NULL, NULL);
  1759. if (err) {
  1760. wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d", err);
  1761. return -1;
  1762. }
  1763. if (set_tx && alg != WPA_ALG_NONE) {
  1764. msg = nlmsg_alloc();
  1765. if (msg == NULL)
  1766. return -1;
  1767. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  1768. 0, NL80211_CMD_SET_KEY, 0);
  1769. NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
  1770. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  1771. NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
  1772. err = send_and_recv_msgs(drv, msg, NULL, NULL);
  1773. if (err) {
  1774. wpa_printf(MSG_DEBUG, "nl80211: set default key "
  1775. "failed; err=%d", err);
  1776. return -1;
  1777. }
  1778. }
  1779. return 0;
  1780. nla_put_failure:
  1781. return -ENOBUFS;
  1782. }
  1783. static int wpa_driver_nl80211_set_countermeasures(void *priv,
  1784. int enabled)
  1785. {
  1786. struct wpa_driver_nl80211_data *drv = priv;
  1787. wpa_printf(MSG_DEBUG, "%s", __FUNCTION__);
  1788. return wpa_driver_nl80211_set_auth_param(drv,
  1789. IW_AUTH_TKIP_COUNTERMEASURES,
  1790. enabled);
  1791. }
  1792. static int wpa_driver_nl80211_set_drop_unencrypted(void *priv,
  1793. int enabled)
  1794. {
  1795. struct wpa_driver_nl80211_data *drv = priv;
  1796. wpa_printf(MSG_DEBUG, "%s", __FUNCTION__);
  1797. drv->use_crypt = enabled;
  1798. return wpa_driver_nl80211_set_auth_param(drv, IW_AUTH_DROP_UNENCRYPTED,
  1799. enabled);
  1800. }
  1801. static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
  1802. const u8 *addr, int cmd, int reason_code)
  1803. {
  1804. struct iwreq iwr;
  1805. struct iw_mlme mlme;
  1806. int ret = 0;
  1807. os_memset(&iwr, 0, sizeof(iwr));
  1808. os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
  1809. os_memset(&mlme, 0, sizeof(mlme));
  1810. mlme.cmd = cmd;
  1811. mlme.reason_code = reason_code;
  1812. mlme.addr.sa_family = ARPHRD_ETHER;
  1813. os_memcpy(mlme.addr.sa_data, addr, ETH_ALEN);
  1814. iwr.u.data.pointer = (caddr_t) &mlme;
  1815. iwr.u.data.length = sizeof(mlme);
  1816. if (ioctl(drv->ioctl_sock, SIOCSIWMLME, &iwr) < 0) {
  1817. perror("ioctl[SIOCSIWMLME]");
  1818. ret = -1;
  1819. }
  1820. return ret;
  1821. }
  1822. static int wpa_driver_nl80211_deauthenticate(void *priv, const u8 *addr,
  1823. int reason_code)
  1824. {
  1825. struct wpa_driver_nl80211_data *drv = priv;
  1826. wpa_printf(MSG_DEBUG, "%s", __FUNCTION__);
  1827. return wpa_driver_nl80211_mlme(drv, addr, IW_MLME_DEAUTH, reason_code);
  1828. }
  1829. static int wpa_driver_nl80211_disassociate(void *priv, const u8 *addr,
  1830. int reason_code)
  1831. {
  1832. struct wpa_driver_nl80211_data *drv = priv;
  1833. wpa_printf(MSG_DEBUG, "%s", __FUNCTION__);
  1834. return wpa_driver_nl80211_mlme(drv, addr, IW_MLME_DISASSOC,
  1835. reason_code);
  1836. }
  1837. static int wpa_driver_nl80211_set_gen_ie(void *priv, const u8 *ie,
  1838. size_t ie_len)
  1839. {
  1840. struct wpa_driver_nl80211_data *drv = priv;
  1841. struct iwreq iwr;
  1842. int ret = 0;
  1843. os_memset(&iwr, 0, sizeof(iwr));
  1844. os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
  1845. iwr.u.data.pointer = (caddr_t) ie;
  1846. iwr.u.data.length = ie_len;
  1847. if (ioctl(drv->ioctl_sock, SIOCSIWGENIE, &iwr) < 0) {
  1848. perror("ioctl[SIOCSIWGENIE]");
  1849. ret = -1;
  1850. }
  1851. return ret;
  1852. }
  1853. static int wpa_driver_nl80211_cipher2wext(int cipher)
  1854. {
  1855. switch (cipher) {
  1856. case CIPHER_NONE:
  1857. return IW_AUTH_CIPHER_NONE;
  1858. case CIPHER_WEP40:
  1859. return IW_AUTH_CIPHER_WEP40;
  1860. case CIPHER_TKIP:
  1861. return IW_AUTH_CIPHER_TKIP;
  1862. case CIPHER_CCMP:
  1863. return IW_AUTH_CIPHER_CCMP;
  1864. case CIPHER_WEP104:
  1865. return IW_AUTH_CIPHER_WEP104;
  1866. default:
  1867. return 0;
  1868. }
  1869. }
  1870. static int wpa_driver_nl80211_keymgmt2wext(int keymgmt)
  1871. {
  1872. switch (keymgmt) {
  1873. case KEY_MGMT_802_1X:
  1874. case KEY_MGMT_802_1X_NO_WPA:
  1875. return IW_AUTH_KEY_MGMT_802_1X;
  1876. case KEY_MGMT_PSK:
  1877. return IW_AUTH_KEY_MGMT_PSK;
  1878. default:
  1879. return 0;
  1880. }
  1881. }
  1882. static int
  1883. wpa_driver_nl80211_auth_alg_fallback(struct wpa_driver_nl80211_data *drv,
  1884. struct wpa_driver_associate_params *params)
  1885. {
  1886. struct iwreq iwr;
  1887. int ret = 0;
  1888. wpa_printf(MSG_DEBUG, "WEXT: Driver did not support "
  1889. "SIOCSIWAUTH for AUTH_ALG, trying SIOCSIWENCODE");
  1890. os_memset(&iwr, 0, sizeof(iwr));
  1891. os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
  1892. /* Just changing mode, not actual keys */
  1893. iwr.u.encoding.flags = 0;
  1894. iwr.u.encoding.pointer = (caddr_t) NULL;
  1895. iwr.u.encoding.length = 0;
  1896. /*
  1897. * Note: IW_ENCODE_{OPEN,RESTRICTED} can be interpreted to mean two
  1898. * different things. Here they are used to indicate Open System vs.
  1899. * Shared Key authentication algorithm. However, some drivers may use
  1900. * them to select between open/restricted WEP encrypted (open = allow
  1901. * both unencrypted and encrypted frames; restricted = only allow
  1902. * encrypted frames).
  1903. */
  1904. if (!drv->use_crypt) {
  1905. iwr.u.encoding.flags |= IW_ENCODE_DISABLED;
  1906. } else {
  1907. if (params->auth_alg & AUTH_ALG_OPEN_SYSTEM)
  1908. iwr.u.encoding.flags |= IW_ENCODE_OPEN;
  1909. if (params->auth_alg & AUTH_ALG_SHARED_KEY)
  1910. iwr.u.encoding.flags |= IW_ENCODE_RESTRICTED;
  1911. }
  1912. if (ioctl(drv->ioctl_sock, SIOCSIWENCODE, &iwr) < 0) {
  1913. perror("ioctl[SIOCSIWENCODE]");
  1914. ret = -1;
  1915. }
  1916. return ret;
  1917. }
  1918. static int wpa_driver_nl80211_associate(
  1919. void *priv, struct wpa_driver_associate_params *params)
  1920. {
  1921. struct wpa_driver_nl80211_data *drv = priv;
  1922. int ret = 0;
  1923. int allow_unencrypted_eapol;
  1924. int value;
  1925. wpa_printf(MSG_DEBUG, "%s", __FUNCTION__);
  1926. /*
  1927. * If the driver did not support SIOCSIWAUTH, fallback to
  1928. * SIOCSIWENCODE here.
  1929. */
  1930. if (drv->auth_alg_fallback &&
  1931. wpa_driver_nl80211_auth_alg_fallback(drv, params) < 0)
  1932. ret = -1;
  1933. if (!params->bssid &&
  1934. wpa_driver_nl80211_set_bssid(drv, NULL) < 0)
  1935. ret = -1;
  1936. /* TODO: should consider getting wpa version and cipher/key_mgmt suites
  1937. * from configuration, not from here, where only the selected suite is
  1938. * available */
  1939. if (wpa_driver_nl80211_set_gen_ie(drv, params->wpa_ie, params->wpa_ie_len)
  1940. < 0)
  1941. ret = -1;
  1942. if (params->wpa_ie == NULL || params->wpa_ie_len == 0)
  1943. value = IW_AUTH_WPA_VERSION_DISABLED;
  1944. else if (params->wpa_ie[0] == WLAN_EID_RSN)
  1945. value = IW_AUTH_WPA_VERSION_WPA2;
  1946. else
  1947. value = IW_AUTH_WPA_VERSION_WPA;
  1948. if (wpa_driver_nl80211_set_auth_param(drv,
  1949. IW_AUTH_WPA_VERSION, value) < 0)
  1950. ret = -1;
  1951. value = wpa_driver_nl80211_cipher2wext(params->pairwise_suite);
  1952. if (wpa_driver_nl80211_set_auth_param(drv,
  1953. IW_AUTH_CIPHER_PAIRWISE, value) < 0)
  1954. ret = -1;
  1955. value = wpa_driver_nl80211_cipher2wext(params->group_suite);
  1956. if (wpa_driver_nl80211_set_auth_param(drv,
  1957. IW_AUTH_CIPHER_GROUP, value) < 0)
  1958. ret = -1;
  1959. value = wpa_driver_nl80211_keymgmt2wext(params->key_mgmt_suite);
  1960. if (wpa_driver_nl80211_set_auth_param(drv,
  1961. IW_AUTH_KEY_MGMT, value) < 0)
  1962. ret = -1;
  1963. value = params->key_mgmt_suite != KEY_MGMT_NONE ||
  1964. params->pairwise_suite != CIPHER_NONE ||
  1965. params->group_suite != CIPHER_NONE ||
  1966. params->wpa_ie_len;
  1967. if (wpa_driver_nl80211_set_auth_param(drv,
  1968. IW_AUTH_PRIVACY_INVOKED, value) < 0)
  1969. ret = -1;
  1970. /* Allow unencrypted EAPOL messages even if pairwise keys are set when
  1971. * not using WPA. IEEE 802.1X specifies that these frames are not
  1972. * encrypted, but WPA encrypts them when pairwise keys are in use. */
  1973. if (params->key_mgmt_suite == KEY_MGMT_802_1X ||
  1974. params->key_mgmt_suite == KEY_MGMT_PSK)
  1975. allow_unencrypted_eapol = 0;
  1976. else
  1977. allow_unencrypted_eapol = 1;
  1978. if (wpa_driver_nl80211_set_auth_param(drv,
  1979. IW_AUTH_RX_UNENCRYPTED_EAPOL,
  1980. allow_unencrypted_eapol) < 0)
  1981. ret = -1;
  1982. if (params->freq && wpa_driver_nl80211_set_freq(drv, params->freq) < 0)
  1983. ret = -1;
  1984. if (wpa_driver_nl80211_set_ssid(drv, params->ssid, params->ssid_len) < 0)
  1985. ret = -1;
  1986. if (params->bssid &&
  1987. wpa_driver_nl80211_set_bssid(drv, params->bssid) < 0)
  1988. ret = -1;
  1989. return ret;
  1990. }
  1991. static int wpa_driver_nl80211_set_auth_alg(void *priv, int auth_alg)
  1992. {
  1993. struct wpa_driver_nl80211_data *drv = priv;
  1994. int algs = 0, res;
  1995. if (auth_alg & AUTH_ALG_OPEN_SYSTEM)
  1996. algs |= IW_AUTH_ALG_OPEN_SYSTEM;
  1997. if (auth_alg & AUTH_ALG_SHARED_KEY)
  1998. algs |= IW_AUTH_ALG_SHARED_KEY;
  1999. if (auth_alg & AUTH_ALG_LEAP)
  2000. algs |= IW_AUTH_ALG_LEAP;
  2001. if (algs == 0) {
  2002. /* at least one algorithm should be set */
  2003. algs = IW_AUTH_ALG_OPEN_SYSTEM;
  2004. }
  2005. res = wpa_driver_nl80211_set_auth_param(drv, IW_AUTH_80211_AUTH_ALG,
  2006. algs);
  2007. drv->auth_alg_fallback = res == -2;
  2008. return res;
  2009. }
  2010. /**
  2011. * wpa_driver_nl80211_set_mode - Set wireless mode (infra/adhoc), SIOCSIWMODE
  2012. * @priv: Pointer to private wext data from wpa_driver_nl80211_init()
  2013. * @mode: 0 = infra/BSS (associate with an AP), 1 = adhoc/IBSS
  2014. * Returns: 0 on success, -1 on failure
  2015. */
  2016. static int wpa_driver_nl80211_set_mode(void *priv, int mode)
  2017. {
  2018. struct wpa_driver_nl80211_data *drv = priv;
  2019. int ret = -1, flags;
  2020. struct nl_msg *msg;
  2021. msg = nlmsg_alloc();
  2022. if (!msg)
  2023. return -1;
  2024. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  2025. 0, NL80211_CMD_SET_INTERFACE, 0);
  2026. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  2027. NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE,
  2028. mode ? NL80211_IFTYPE_ADHOC : NL80211_IFTYPE_STATION);
  2029. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  2030. if (!ret)
  2031. return 0;
  2032. else
  2033. goto try_again;
  2034. nla_put_failure:
  2035. wpa_printf(MSG_ERROR, "nl80211: Failed to set interface mode");
  2036. return -1;
  2037. try_again:
  2038. /* mac80211 doesn't allow mode changes while the device is up, so
  2039. * take the device down, try to set the mode again, and bring the
  2040. * device back up.
  2041. */
  2042. if (wpa_driver_nl80211_get_ifflags(drv, &flags) == 0) {
  2043. (void) wpa_driver_nl80211_set_ifflags(drv, flags & ~IFF_UP);
  2044. /* Try to set the mode again while the interface is down */
  2045. msg = nlmsg_alloc();
  2046. if (!msg)
  2047. return -1;
  2048. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  2049. 0, NL80211_CMD_SET_INTERFACE, 0);
  2050. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  2051. NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE,
  2052. mode ? NL80211_IFTYPE_ADHOC :
  2053. NL80211_IFTYPE_STATION);
  2054. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  2055. if (ret) {
  2056. wpa_printf(MSG_ERROR, "Failed to set interface %s "
  2057. "mode", drv->ifname);
  2058. }
  2059. /* Ignore return value of get_ifflags to ensure that the device
  2060. * is always up like it was before this function was called.
  2061. */
  2062. (void) wpa_driver_nl80211_get_ifflags(drv, &flags);
  2063. (void) wpa_driver_nl80211_set_ifflags(drv, flags | IFF_UP);
  2064. }
  2065. return ret;
  2066. }
  2067. static int wpa_driver_nl80211_pmksa(struct wpa_driver_nl80211_data *drv,
  2068. u32 cmd, const u8 *bssid, const u8 *pmkid)
  2069. {
  2070. struct iwreq iwr;
  2071. struct iw_pmksa pmksa;
  2072. int ret = 0;
  2073. os_memset(&iwr, 0, sizeof(iwr));
  2074. os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
  2075. os_memset(&pmksa, 0, sizeof(pmksa));
  2076. pmksa.cmd = cmd;
  2077. pmksa.bssid.sa_family = ARPHRD_ETHER;
  2078. if (bssid)
  2079. os_memcpy(pmksa.bssid.sa_data, bssid, ETH_ALEN);
  2080. if (pmkid)
  2081. os_memcpy(pmksa.pmkid, pmkid, IW_PMKID_LEN);
  2082. iwr.u.data.pointer = (caddr_t) &pmksa;
  2083. iwr.u.data.length = sizeof(pmksa);
  2084. if (ioctl(drv->ioctl_sock, SIOCSIWPMKSA, &iwr) < 0) {
  2085. if (errno != EOPNOTSUPP)
  2086. perror("ioctl[SIOCSIWPMKSA]");
  2087. ret = -1;
  2088. }
  2089. return ret;
  2090. }
  2091. static int wpa_driver_nl80211_add_pmkid(void *priv, const u8 *bssid,
  2092. const u8 *pmkid)
  2093. {
  2094. struct wpa_driver_nl80211_data *drv = priv;
  2095. return wpa_driver_nl80211_pmksa(drv, IW_PMKSA_ADD, bssid, pmkid);
  2096. }
  2097. static int wpa_driver_nl80211_remove_pmkid(void *priv, const u8 *bssid,
  2098. const u8 *pmkid)
  2099. {
  2100. struct wpa_driver_nl80211_data *drv = priv;
  2101. return wpa_driver_nl80211_pmksa(drv, IW_PMKSA_REMOVE, bssid, pmkid);
  2102. }
  2103. static int wpa_driver_nl80211_flush_pmkid(void *priv)
  2104. {
  2105. struct wpa_driver_nl80211_data *drv = priv;
  2106. return wpa_driver_nl80211_pmksa(drv, IW_PMKSA_FLUSH, NULL, NULL);
  2107. }
  2108. static int wpa_driver_nl80211_get_capa(void *priv,
  2109. struct wpa_driver_capa *capa)
  2110. {
  2111. struct wpa_driver_nl80211_data *drv = priv;
  2112. if (!drv->has_capability)
  2113. return -1;
  2114. os_memcpy(capa, &drv->capa, sizeof(*capa));
  2115. return 0;
  2116. }
  2117. static int wpa_driver_nl80211_set_operstate(void *priv, int state)
  2118. {
  2119. struct wpa_driver_nl80211_data *drv = priv;
  2120. wpa_printf(MSG_DEBUG, "%s: operstate %d->%d (%s)",
  2121. __func__, drv->operstate, state, state ? "UP" : "DORMANT");
  2122. drv->operstate = state;
  2123. return wpa_driver_nl80211_send_oper_ifla(
  2124. drv, -1, state ? IF_OPER_UP : IF_OPER_DORMANT);
  2125. }
  2126. #ifdef CONFIG_CLIENT_MLME
  2127. static int wpa_driver_nl80211_open_mlme(struct wpa_driver_nl80211_data *drv)
  2128. {
  2129. if (wpa_driver_nl80211_set_userspace_mlme(drv, 1) < 0) {
  2130. wpa_printf(MSG_ERROR, "nl80211: Failed to enable userspace "
  2131. "MLME");
  2132. return -1;
  2133. }
  2134. if (wpa_driver_nl80211_create_monitor_interface(drv)) {
  2135. wpa_printf(MSG_ERROR, "nl80211: Failed to create monitor "
  2136. "interface");
  2137. return -1;
  2138. }
  2139. return 0;
  2140. }
  2141. #endif /* CONFIG_CLIENT_MLME */
  2142. static int wpa_driver_nl80211_set_param(void *priv, const char *param)
  2143. {
  2144. #ifdef CONFIG_CLIENT_MLME
  2145. struct wpa_driver_nl80211_data *drv = priv;
  2146. if (param == NULL)
  2147. return 0;
  2148. wpa_printf(MSG_DEBUG, "%s: param='%s'", __func__, param);
  2149. if (os_strstr(param, "use_mlme=1")) {
  2150. wpa_printf(MSG_DEBUG, "nl80211: Using user space MLME");
  2151. drv->capa.flags |= WPA_DRIVER_FLAGS_USER_SPACE_MLME;
  2152. if (wpa_driver_nl80211_open_mlme(drv))
  2153. return -1;
  2154. }
  2155. #endif /* CONFIG_CLIENT_MLME */
  2156. return 0;
  2157. }
  2158. #ifdef CONFIG_CLIENT_MLME
  2159. struct phy_info_arg {
  2160. u16 *num_modes;
  2161. struct wpa_hw_modes *modes;
  2162. };
  2163. static int phy_info_handler(struct nl_msg *msg, void *arg)
  2164. {
  2165. struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
  2166. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  2167. struct phy_info_arg *phy_info = arg;
  2168. struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
  2169. struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
  2170. static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1]
  2171. = {
  2172. [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
  2173. [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
  2174. [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
  2175. [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG },
  2176. [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
  2177. };
  2178. struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
  2179. static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
  2180. [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
  2181. [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] =
  2182. { .type = NLA_FLAG },
  2183. };
  2184. struct nlattr *nl_band;
  2185. struct nlattr *nl_freq;
  2186. struct nlattr *nl_rate;
  2187. int rem_band, rem_freq, rem_rate;
  2188. struct wpa_hw_modes *mode;
  2189. int idx, mode_is_set;
  2190. nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  2191. genlmsg_attrlen(gnlh, 0), NULL);
  2192. if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
  2193. return NL_SKIP;
  2194. nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS],
  2195. rem_band) {
  2196. mode = os_realloc(phy_info->modes,
  2197. (*phy_info->num_modes + 1) * sizeof(*mode));
  2198. if (!mode)
  2199. return NL_SKIP;
  2200. phy_info->modes = mode;
  2201. mode_is_set = 0;
  2202. mode = &phy_info->modes[*(phy_info->num_modes)];
  2203. os_memset(mode, 0, sizeof(*mode));
  2204. *(phy_info->num_modes) += 1;
  2205. nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
  2206. nla_len(nl_band), NULL);
  2207. nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS],
  2208. rem_freq) {
  2209. nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
  2210. nla_data(nl_freq), nla_len(nl_freq),
  2211. freq_policy);
  2212. if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
  2213. continue;
  2214. mode->num_channels++;
  2215. }
  2216. mode->channels = os_zalloc(mode->num_channels *
  2217. sizeof(struct wpa_channel_data));
  2218. if (!mode->channels)
  2219. return NL_SKIP;
  2220. idx = 0;
  2221. nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS],
  2222. rem_freq) {
  2223. nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
  2224. nla_data(nl_freq), nla_len(nl_freq),
  2225. freq_policy);
  2226. if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
  2227. continue;
  2228. mode->channels[idx].freq = nla_get_u32(
  2229. tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
  2230. mode->channels[idx].flag |= WPA_CHAN_W_SCAN |
  2231. WPA_CHAN_W_ACTIVE_SCAN |
  2232. WPA_CHAN_W_IBSS;
  2233. if (!mode_is_set) {
  2234. /* crude heuristic */
  2235. if (mode->channels[idx].freq < 4000)
  2236. mode->mode = WPA_MODE_IEEE80211B;
  2237. else
  2238. mode->mode = WPA_MODE_IEEE80211A;
  2239. mode_is_set = 1;
  2240. }
  2241. /* crude heuristic */
  2242. if (mode->channels[idx].freq < 4000) {
  2243. if (mode->channels[idx].freq == 2848)
  2244. mode->channels[idx].chan = 14;
  2245. else
  2246. mode->channels[idx].chan =
  2247. (mode->channels[idx].freq -
  2248. 2407) / 5;
  2249. } else
  2250. mode->channels[idx].chan =
  2251. mode->channels[idx].freq / 5 - 1000;
  2252. if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
  2253. mode->channels[idx].flag &= ~WPA_CHAN_W_SCAN;
  2254. if (tb_freq[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN])
  2255. mode->channels[idx].flag &=
  2256. ~WPA_CHAN_W_ACTIVE_SCAN;
  2257. if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IBSS])
  2258. mode->channels[idx].flag &= ~WPA_CHAN_W_IBSS;
  2259. idx++;
  2260. }
  2261. nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES],
  2262. rem_rate) {
  2263. nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
  2264. nla_data(nl_rate), nla_len(nl_rate),
  2265. rate_policy);
  2266. if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
  2267. continue;
  2268. mode->num_rates++;
  2269. }
  2270. mode->rates = os_zalloc(mode->num_rates *
  2271. sizeof(struct wpa_rate_data));
  2272. if (!mode->rates)
  2273. return NL_SKIP;
  2274. idx = 0;
  2275. nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES],
  2276. rem_rate) {
  2277. nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
  2278. nla_data(nl_rate), nla_len(nl_rate),
  2279. rate_policy);
  2280. if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
  2281. continue;
  2282. mode->rates[idx].rate = nla_get_u32(
  2283. tb_rate[NL80211_BITRATE_ATTR_RATE]);
  2284. /* crude heuristic */
  2285. if (mode->mode == WPA_MODE_IEEE80211B &&
  2286. mode->rates[idx].rate > 200)
  2287. mode->mode = WPA_MODE_IEEE80211G;
  2288. if (tb_rate[NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE])
  2289. mode->rates[idx].flags |= WPA_RATE_PREAMBLE2;
  2290. idx++;
  2291. }
  2292. }
  2293. return NL_SKIP;
  2294. }
  2295. static struct wpa_hw_modes *
  2296. wpa_driver_nl80211_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags)
  2297. {
  2298. struct wpa_driver_nl80211_data *drv = priv;
  2299. struct nl_msg *msg;
  2300. struct phy_info_arg result = {
  2301. .num_modes = num_modes,
  2302. .modes = NULL,
  2303. };
  2304. *num_modes = 0;
  2305. *flags = 0;
  2306. msg = nlmsg_alloc();
  2307. if (!msg)
  2308. return NULL;
  2309. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  2310. 0, NL80211_CMD_GET_WIPHY, 0);
  2311. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  2312. if (send_and_recv_msgs(drv, msg, phy_info_handler, &result) == 0)
  2313. return result.modes;
  2314. nla_put_failure:
  2315. return NULL;
  2316. }
  2317. static int wpa_driver_nl80211_set_channel(void *priv, wpa_hw_mode phymode,
  2318. int chan, int freq)
  2319. {
  2320. return wpa_driver_nl80211_set_freq(priv, freq);
  2321. }
  2322. static int wpa_driver_nl80211_send_mlme(void *priv, const u8 *data,
  2323. size_t data_len)
  2324. {
  2325. struct wpa_driver_nl80211_data *drv = priv;
  2326. __u8 rtap_hdr[] = {
  2327. 0x00, 0x00, /* radiotap version */
  2328. 0x0e, 0x00, /* radiotap length */
  2329. 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
  2330. 0x0c, /* F_WEP | F_FRAG (encrypt/fragment if required) */
  2331. 0x00, /* padding */
  2332. 0x00, 0x00, /* RX and TX flags to indicate that */
  2333. 0x00, 0x00, /* this is the injected frame directly */
  2334. };
  2335. struct iovec iov[2] = {
  2336. {
  2337. .iov_base = &rtap_hdr,
  2338. .iov_len = sizeof(rtap_hdr),
  2339. },
  2340. {
  2341. .iov_base = (void *) data,
  2342. .iov_len = data_len,
  2343. }
  2344. };
  2345. struct msghdr msg = {
  2346. .msg_name = NULL,
  2347. .msg_namelen = 0,
  2348. .msg_iov = iov,
  2349. .msg_iovlen = 2,
  2350. .msg_control = NULL,
  2351. .msg_controllen = 0,
  2352. .msg_flags = 0,
  2353. };
  2354. if (sendmsg(drv->monitor_sock, &msg, 0) < 0) {
  2355. perror("send[MLME]");
  2356. return -1;
  2357. }
  2358. return 0;
  2359. }
  2360. static int wpa_driver_nl80211_mlme_add_sta(void *priv, const u8 *addr,
  2361. const u8 *supp_rates,
  2362. size_t supp_rates_len)
  2363. {
  2364. struct wpa_driver_nl80211_data *drv = priv;
  2365. struct nl_msg *msg;
  2366. int ret = -1;
  2367. msg = nlmsg_alloc();
  2368. if (!msg)
  2369. return -ENOMEM;
  2370. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  2371. 0, NL80211_CMD_NEW_STATION, 0);
  2372. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  2373. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
  2374. /* TODO: Get proper Association ID and listen interval */
  2375. NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, 1);
  2376. NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, supp_rates_len,
  2377. supp_rates);
  2378. NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL, 1);
  2379. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  2380. /* ignore EEXIST, this happens if a STA associates while associated */
  2381. if (ret == -EEXIST || ret >= 0)
  2382. ret = 0;
  2383. nla_put_failure:
  2384. return ret;
  2385. }
  2386. static int wpa_driver_nl80211_mlme_remove_sta(void *priv, const u8 *addr)
  2387. {
  2388. struct wpa_driver_nl80211_data *drv = priv;
  2389. struct nl_msg *msg;
  2390. int ret = -1;
  2391. msg = nlmsg_alloc();
  2392. if (!msg)
  2393. return -ENOMEM;
  2394. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  2395. 0, NL80211_CMD_DEL_STATION, 0);
  2396. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  2397. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
  2398. ret = 0;
  2399. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  2400. return ret;
  2401. nla_put_failure:
  2402. return -ENOBUFS;
  2403. }
  2404. #endif /* CONFIG_CLIENT_MLME */
  2405. const struct wpa_driver_ops wpa_driver_nl80211_ops = {
  2406. .name = "nl80211",
  2407. .desc = "Linux nl80211/cfg80211",
  2408. .get_bssid = wpa_driver_nl80211_get_bssid,
  2409. .get_ssid = wpa_driver_nl80211_get_ssid,
  2410. .set_wpa = wpa_driver_nl80211_set_wpa,
  2411. .set_key = wpa_driver_nl80211_set_key,
  2412. .set_countermeasures = wpa_driver_nl80211_set_countermeasures,
  2413. .set_drop_unencrypted = wpa_driver_nl80211_set_drop_unencrypted,
  2414. .scan = wpa_driver_nl80211_scan,
  2415. .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
  2416. .deauthenticate = wpa_driver_nl80211_deauthenticate,
  2417. .disassociate = wpa_driver_nl80211_disassociate,
  2418. .set_mode = wpa_driver_nl80211_set_mode,
  2419. .associate = wpa_driver_nl80211_associate,
  2420. .set_auth_alg = wpa_driver_nl80211_set_auth_alg,
  2421. .init = wpa_driver_nl80211_init,
  2422. .deinit = wpa_driver_nl80211_deinit,
  2423. .set_param = wpa_driver_nl80211_set_param,
  2424. .add_pmkid = wpa_driver_nl80211_add_pmkid,
  2425. .remove_pmkid = wpa_driver_nl80211_remove_pmkid,
  2426. .flush_pmkid = wpa_driver_nl80211_flush_pmkid,
  2427. .get_capa = wpa_driver_nl80211_get_capa,
  2428. .set_operstate = wpa_driver_nl80211_set_operstate,
  2429. .set_country = wpa_driver_nl80211_set_country,
  2430. .set_probe_req_ie = wpa_driver_nl80211_set_probe_req_ie,
  2431. #ifdef CONFIG_CLIENT_MLME
  2432. .get_hw_feature_data = wpa_driver_nl80211_get_hw_feature_data,
  2433. .set_channel = wpa_driver_nl80211_set_channel,
  2434. .set_ssid = wpa_driver_nl80211_set_ssid,
  2435. .set_bssid = wpa_driver_nl80211_set_bssid,
  2436. .send_mlme = wpa_driver_nl80211_send_mlme,
  2437. .mlme_add_sta = wpa_driver_nl80211_mlme_add_sta,
  2438. .mlme_remove_sta = wpa_driver_nl80211_mlme_remove_sta,
  2439. #endif /* CONFIG_CLIENT_MLME */
  2440. };