driver_nl80211.c 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048
  1. /*
  2. * hostapd / Kernel driver communication via nl80211
  3. * Copyright (c) 2002-2007, Jouni Malinen <j@w1.fi>
  4. * Copyright (c) 2003-2004, Instant802 Networks, Inc.
  5. * Copyright (c) 2005-2006, Devicescape Software, Inc.
  6. * Copyright (c) 2007, Johannes Berg <johannes@sipsolutions.net>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * Alternatively, this software may be distributed under the terms of BSD
  13. * license.
  14. *
  15. * See README and COPYING for more details.
  16. */
  17. #include "includes.h"
  18. #include <sys/ioctl.h>
  19. #include <netlink/genl/genl.h>
  20. #include <netlink/genl/family.h>
  21. #include <netlink/genl/ctrl.h>
  22. #include <netlink/msg.h>
  23. #include <netlink/attr.h>
  24. #include "nl80211_copy.h"
  25. #include <net/if.h>
  26. #include <netpacket/packet.h>
  27. #include "wireless_copy.h"
  28. #include <linux/filter.h>
  29. #include <net/if_arp.h>
  30. #include "hostapd.h"
  31. #include "config.h"
  32. #include "driver.h"
  33. #include "eloop.h"
  34. #include "hw_features.h"
  35. #include "mlme.h"
  36. #include "radiotap.h"
  37. #include "radiotap_iter.h"
  38. #include "ieee802_11_defs.h"
  39. #include "ieee802_11_common.h"
  40. #ifdef CONFIG_LIBNL20
  41. /* libnl 2.0 compatibility code */
  42. #define nl_handle_alloc_cb nl_socket_alloc_cb
  43. #define nl_handle_destroy nl_socket_free
  44. #endif /* CONFIG_LIBNL20 */
  45. static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
  46. enum ieee80211_msg_type {
  47. ieee80211_msg_normal = 0,
  48. ieee80211_msg_tx_callback_ack = 1,
  49. ieee80211_msg_tx_callback_fail = 2,
  50. };
  51. struct i802_bss {
  52. struct i802_bss *next;
  53. char iface[IFNAMSIZ + 1];
  54. unsigned int beacon_set:1;
  55. };
  56. struct i802_driver_data {
  57. struct hostapd_data *hapd;
  58. char iface[IFNAMSIZ + 1];
  59. int bridge;
  60. int ioctl_sock; /* socket for ioctl() use */
  61. int wext_sock; /* socket for wireless events */
  62. int eapol_sock; /* socket for EAPOL frames */
  63. int monitor_sock; /* socket for monitor */
  64. int monitor_ifidx;
  65. int default_if_indices[16];
  66. int *if_indices;
  67. int num_if_indices;
  68. int we_version;
  69. struct nl_handle *nl_handle;
  70. struct nl_cache *nl_cache;
  71. struct nl_cb *nl_cb;
  72. struct genl_family *nl80211;
  73. int beacon_int;
  74. struct i802_bss bss;
  75. unsigned int ieee802_1x_active:1;
  76. unsigned int ht_40mhz_scan:1;
  77. int last_freq;
  78. int last_freq_ht;
  79. struct hostapd_neighbor_bss *neighbors;
  80. size_t num_neighbors;
  81. };
  82. static int i802_sta_deauth(void *priv, const u8 *addr, int reason);
  83. static int i802_sta_disassoc(void *priv, const u8 *addr, int reason);
  84. static struct i802_bss * get_bss(struct i802_driver_data *drv,
  85. const char *iface)
  86. {
  87. struct i802_bss *bss = &drv->bss;
  88. while (bss) {
  89. if (os_strncmp(iface, bss->iface, IFNAMSIZ) == 0)
  90. return bss;
  91. bss = bss->next;
  92. }
  93. wpa_printf(MSG_DEBUG, "nl80211: get_bss(%s) failed", iface);
  94. return NULL;
  95. }
  96. static void add_ifidx(struct i802_driver_data *drv, int ifidx)
  97. {
  98. int i;
  99. int *old;
  100. for (i = 0; i < drv->num_if_indices; i++) {
  101. if (drv->if_indices[i] == 0) {
  102. drv->if_indices[i] = ifidx;
  103. return;
  104. }
  105. }
  106. if (drv->if_indices != drv->default_if_indices)
  107. old = drv->if_indices;
  108. else
  109. old = NULL;
  110. drv->if_indices = realloc(old,
  111. sizeof(int) * (drv->num_if_indices + 1));
  112. if (!drv->if_indices) {
  113. if (!old)
  114. drv->if_indices = drv->default_if_indices;
  115. else
  116. drv->if_indices = old;
  117. wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
  118. "interfaces");
  119. wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
  120. return;
  121. }
  122. drv->if_indices[drv->num_if_indices] = ifidx;
  123. drv->num_if_indices++;
  124. }
  125. static void del_ifidx(struct i802_driver_data *drv, int ifidx)
  126. {
  127. int i;
  128. for (i = 0; i < drv->num_if_indices; i++) {
  129. if (drv->if_indices[i] == ifidx) {
  130. drv->if_indices[i] = 0;
  131. break;
  132. }
  133. }
  134. }
  135. static int have_ifidx(struct i802_driver_data *drv, int ifidx)
  136. {
  137. int i;
  138. if (ifidx == drv->bridge)
  139. return 1;
  140. for (i = 0; i < drv->num_if_indices; i++)
  141. if (drv->if_indices[i] == ifidx)
  142. return 1;
  143. return 0;
  144. }
  145. /* nl80211 code */
  146. static int ack_handler(struct nl_msg *msg, void *arg)
  147. {
  148. int *err = arg;
  149. *err = 0;
  150. return NL_STOP;
  151. }
  152. static int finish_handler(struct nl_msg *msg, void *arg)
  153. {
  154. int *ret = arg;
  155. *ret = 0;
  156. return NL_SKIP;
  157. }
  158. static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
  159. void *arg)
  160. {
  161. int *ret = arg;
  162. *ret = err->error;
  163. return NL_SKIP;
  164. }
  165. static int send_and_recv_msgs(struct i802_driver_data *drv,
  166. struct nl_msg *msg,
  167. int (*valid_handler)(struct nl_msg *, void *),
  168. void *valid_data)
  169. {
  170. struct nl_cb *cb;
  171. int err = -ENOMEM;
  172. cb = nl_cb_clone(drv->nl_cb);
  173. if (!cb)
  174. goto out;
  175. err = nl_send_auto_complete(drv->nl_handle, msg);
  176. if (err < 0)
  177. goto out;
  178. err = 1;
  179. nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
  180. nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
  181. nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
  182. if (valid_handler)
  183. nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
  184. valid_handler, valid_data);
  185. while (err > 0)
  186. nl_recvmsgs(drv->nl_handle, cb);
  187. out:
  188. nl_cb_put(cb);
  189. nlmsg_free(msg);
  190. return err;
  191. }
  192. static int hostapd_set_iface_flags(struct i802_driver_data *drv,
  193. const char *ifname, int dev_up)
  194. {
  195. struct ifreq ifr;
  196. if (drv->ioctl_sock < 0)
  197. return -1;
  198. memset(&ifr, 0, sizeof(ifr));
  199. os_strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
  200. if (ioctl(drv->ioctl_sock, SIOCGIFFLAGS, &ifr) != 0) {
  201. perror("ioctl[SIOCGIFFLAGS]");
  202. wpa_printf(MSG_DEBUG, "Could not read interface flags (%s)",
  203. drv->iface);
  204. return -1;
  205. }
  206. if (dev_up)
  207. ifr.ifr_flags |= IFF_UP;
  208. else
  209. ifr.ifr_flags &= ~IFF_UP;
  210. if (ioctl(drv->ioctl_sock, SIOCSIFFLAGS, &ifr) != 0) {
  211. perror("ioctl[SIOCSIFFLAGS]");
  212. return -1;
  213. }
  214. return 0;
  215. }
  216. static int nl_set_encr(int ifindex, struct i802_driver_data *drv,
  217. wpa_alg alg, const u8 *addr, int idx, const u8 *key,
  218. size_t key_len, int txkey)
  219. {
  220. struct nl_msg *msg;
  221. int ret;
  222. msg = nlmsg_alloc();
  223. if (!msg)
  224. return -ENOMEM;
  225. if (alg == WPA_ALG_NONE) {
  226. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  227. 0, NL80211_CMD_DEL_KEY, 0);
  228. } else {
  229. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  230. 0, NL80211_CMD_NEW_KEY, 0);
  231. NLA_PUT(msg, NL80211_ATTR_KEY_DATA, key_len, key);
  232. switch (alg) {
  233. case WPA_ALG_WEP:
  234. if (key_len == 5)
  235. NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
  236. 0x000FAC01);
  237. else
  238. NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
  239. 0x000FAC05);
  240. break;
  241. case WPA_ALG_TKIP:
  242. NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, 0x000FAC02);
  243. break;
  244. case WPA_ALG_CCMP:
  245. NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, 0x000FAC04);
  246. break;
  247. case WPA_ALG_IGTK:
  248. NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, 0x000FAC06);
  249. break;
  250. default:
  251. wpa_printf(MSG_ERROR, "%s: Unsupported encryption "
  252. "algorithm %d", __func__, alg);
  253. nlmsg_free(msg);
  254. return -1;
  255. }
  256. }
  257. if (addr)
  258. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
  259. NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
  260. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
  261. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  262. if (ret == -ENOENT)
  263. ret = 0;
  264. /*
  265. * If we failed or don't need to set the default TX key (below),
  266. * we're done here.
  267. */
  268. if (ret || !txkey || addr)
  269. return ret;
  270. msg = nlmsg_alloc();
  271. if (!msg)
  272. return -ENOMEM;
  273. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  274. 0, NL80211_CMD_SET_KEY, 0);
  275. NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
  276. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
  277. if (alg == WPA_ALG_IGTK)
  278. NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT_MGMT);
  279. else
  280. NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
  281. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  282. if (ret == -ENOENT)
  283. ret = 0;
  284. return ret;
  285. nla_put_failure:
  286. return -ENOBUFS;
  287. }
  288. static int i802_set_key(const char *iface, void *priv, wpa_alg alg,
  289. const u8 *addr, int key_idx, int set_tx, const u8 *seq,
  290. size_t seq_len, const u8 *key, size_t key_len)
  291. {
  292. struct i802_driver_data *drv = priv;
  293. int ret;
  294. ret = nl_set_encr(if_nametoindex(iface), drv, alg, addr, key_idx, key,
  295. key_len, set_tx);
  296. if (ret < 0)
  297. return ret;
  298. return ret;
  299. }
  300. static inline int min_int(int a, int b)
  301. {
  302. if (a < b)
  303. return a;
  304. return b;
  305. }
  306. static int get_key_handler(struct nl_msg *msg, void *arg)
  307. {
  308. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  309. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  310. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  311. genlmsg_attrlen(gnlh, 0), NULL);
  312. /*
  313. * TODO: validate the key index and mac address!
  314. * Otherwise, there's a race condition as soon as
  315. * the kernel starts sending key notifications.
  316. */
  317. if (tb[NL80211_ATTR_KEY_SEQ])
  318. memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
  319. min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
  320. return NL_SKIP;
  321. }
  322. static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
  323. int idx, u8 *seq)
  324. {
  325. struct i802_driver_data *drv = priv;
  326. struct nl_msg *msg;
  327. msg = nlmsg_alloc();
  328. if (!msg)
  329. return -ENOMEM;
  330. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  331. 0, NL80211_CMD_GET_KEY, 0);
  332. if (addr)
  333. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
  334. NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
  335. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
  336. memset(seq, 0, 6);
  337. return send_and_recv_msgs(drv, msg, get_key_handler, seq);
  338. nla_put_failure:
  339. return -ENOBUFS;
  340. }
  341. static int i802_set_rate_sets(void *priv, int *supp_rates, int *basic_rates,
  342. int mode)
  343. {
  344. struct i802_driver_data *drv = priv;
  345. struct nl_msg *msg;
  346. u8 rates[NL80211_MAX_SUPP_RATES];
  347. u8 rates_len = 0;
  348. int i;
  349. msg = nlmsg_alloc();
  350. if (!msg)
  351. return -ENOMEM;
  352. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  353. NL80211_CMD_SET_BSS, 0);
  354. for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0; i++)
  355. rates[rates_len++] = basic_rates[i] / 5;
  356. NLA_PUT(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
  357. /* TODO: multi-BSS support */
  358. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
  359. return send_and_recv_msgs(drv, msg, NULL, NULL);
  360. nla_put_failure:
  361. return -ENOBUFS;
  362. }
  363. static int i802_send_frame(void *priv, const void *data, size_t len,
  364. int encrypt, int flags)
  365. {
  366. __u8 rtap_hdr[] = {
  367. 0x00, 0x00, /* radiotap version */
  368. 0x0e, 0x00, /* radiotap length */
  369. 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
  370. IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */
  371. 0x00, /* padding */
  372. 0x00, 0x00, /* RX and TX flags to indicate that */
  373. 0x00, 0x00, /* this is the injected frame directly */
  374. };
  375. struct i802_driver_data *drv = priv;
  376. struct iovec iov[2] = {
  377. {
  378. .iov_base = &rtap_hdr,
  379. .iov_len = sizeof(rtap_hdr),
  380. },
  381. {
  382. .iov_base = (void*)data,
  383. .iov_len = len,
  384. }
  385. };
  386. struct msghdr msg = {
  387. .msg_name = NULL,
  388. .msg_namelen = 0,
  389. .msg_iov = iov,
  390. .msg_iovlen = 2,
  391. .msg_control = NULL,
  392. .msg_controllen = 0,
  393. .msg_flags = 0,
  394. };
  395. if (encrypt)
  396. rtap_hdr[8] |= IEEE80211_RADIOTAP_F_WEP;
  397. return sendmsg(drv->monitor_sock, &msg, flags);
  398. }
  399. static int i802_send_mgmt_frame(void *priv, const void *data, size_t len,
  400. int flags)
  401. {
  402. struct ieee80211_mgmt *mgmt;
  403. int do_not_encrypt = 0;
  404. u16 fc;
  405. mgmt = (struct ieee80211_mgmt *) data;
  406. fc = le_to_host16(mgmt->frame_control);
  407. if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
  408. WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
  409. /*
  410. * Only one of the authentication frame types is encrypted.
  411. * In order for static WEP encryption to work properly (i.e.,
  412. * to not encrypt the frame), we need to tell mac80211 about
  413. * the frames that must not be encrypted.
  414. */
  415. u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
  416. u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
  417. if (auth_alg == WLAN_AUTH_OPEN ||
  418. (auth_alg == WLAN_AUTH_SHARED_KEY && auth_trans != 3))
  419. do_not_encrypt = 1;
  420. }
  421. return i802_send_frame(priv, data, len, !do_not_encrypt, flags);
  422. }
  423. /* Set kernel driver on given frequency (MHz) */
  424. static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
  425. {
  426. struct i802_driver_data *drv = priv;
  427. struct nl_msg *msg;
  428. msg = nlmsg_alloc();
  429. if (!msg)
  430. return -1;
  431. drv->last_freq = freq->freq;
  432. drv->last_freq_ht = freq->ht_enabled;
  433. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  434. NL80211_CMD_SET_WIPHY, 0);
  435. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
  436. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq);
  437. if (freq->ht_enabled) {
  438. switch (freq->sec_channel_offset) {
  439. case -1:
  440. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
  441. NL80211_CHAN_HT40MINUS);
  442. break;
  443. case 1:
  444. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
  445. NL80211_CHAN_HT40PLUS);
  446. break;
  447. default:
  448. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
  449. NL80211_CHAN_HT20);
  450. break;
  451. }
  452. }
  453. if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
  454. return 0;
  455. nla_put_failure:
  456. return -1;
  457. }
  458. static int i802_set_rts(void *priv, int rts)
  459. {
  460. struct i802_driver_data *drv = priv;
  461. struct iwreq iwr;
  462. memset(&iwr, 0, sizeof(iwr));
  463. os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
  464. iwr.u.rts.value = rts;
  465. iwr.u.rts.fixed = 1;
  466. if (ioctl(drv->ioctl_sock, SIOCSIWRTS, &iwr) < 0) {
  467. perror("ioctl[SIOCSIWRTS]");
  468. return -1;
  469. }
  470. return 0;
  471. }
  472. static int i802_set_frag(void *priv, int frag)
  473. {
  474. struct i802_driver_data *drv = priv;
  475. struct iwreq iwr;
  476. memset(&iwr, 0, sizeof(iwr));
  477. os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
  478. iwr.u.frag.value = frag;
  479. iwr.u.frag.fixed = 1;
  480. if (ioctl(drv->ioctl_sock, SIOCSIWFRAG, &iwr) < 0) {
  481. perror("ioctl[SIOCSIWFRAG]");
  482. return -1;
  483. }
  484. return 0;
  485. }
  486. static int i802_set_retry(void *priv, int short_retry, int long_retry)
  487. {
  488. struct i802_driver_data *drv = priv;
  489. struct iwreq iwr;
  490. memset(&iwr, 0, sizeof(iwr));
  491. os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
  492. iwr.u.retry.value = short_retry;
  493. iwr.u.retry.flags = IW_RETRY_LIMIT | IW_RETRY_MIN;
  494. if (ioctl(drv->ioctl_sock, SIOCSIWRETRY, &iwr) < 0) {
  495. perror("ioctl[SIOCSIWRETRY(short)]");
  496. return -1;
  497. }
  498. iwr.u.retry.value = long_retry;
  499. iwr.u.retry.flags = IW_RETRY_LIMIT | IW_RETRY_MAX;
  500. if (ioctl(drv->ioctl_sock, SIOCSIWRETRY, &iwr) < 0) {
  501. perror("ioctl[SIOCSIWRETRY(long)]");
  502. return -1;
  503. }
  504. return 0;
  505. }
  506. static int i802_flush(void *priv)
  507. {
  508. struct i802_driver_data *drv = priv;
  509. struct nl_msg *msg;
  510. msg = nlmsg_alloc();
  511. if (!msg)
  512. return -1;
  513. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  514. 0, NL80211_CMD_DEL_STATION, 0);
  515. /*
  516. * XXX: FIX! this needs to flush all VLANs too
  517. */
  518. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
  519. if_nametoindex(drv->iface));
  520. return send_and_recv_msgs(drv, msg, NULL, NULL);
  521. nla_put_failure:
  522. return -ENOBUFS;
  523. }
  524. static int get_sta_handler(struct nl_msg *msg, void *arg)
  525. {
  526. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  527. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  528. struct hostap_sta_driver_data *data = arg;
  529. struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
  530. static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
  531. [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
  532. [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
  533. [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
  534. [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
  535. [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
  536. };
  537. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  538. genlmsg_attrlen(gnlh, 0), NULL);
  539. /*
  540. * TODO: validate the interface and mac address!
  541. * Otherwise, there's a race condition as soon as
  542. * the kernel starts sending station notifications.
  543. */
  544. if (!tb[NL80211_ATTR_STA_INFO]) {
  545. wpa_printf(MSG_DEBUG, "sta stats missing!");
  546. return NL_SKIP;
  547. }
  548. if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
  549. tb[NL80211_ATTR_STA_INFO],
  550. stats_policy)) {
  551. wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
  552. return NL_SKIP;
  553. }
  554. if (stats[NL80211_STA_INFO_INACTIVE_TIME])
  555. data->inactive_msec =
  556. nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
  557. if (stats[NL80211_STA_INFO_RX_BYTES])
  558. data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
  559. if (stats[NL80211_STA_INFO_TX_BYTES])
  560. data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
  561. if (stats[NL80211_STA_INFO_RX_PACKETS])
  562. data->rx_packets =
  563. nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
  564. if (stats[NL80211_STA_INFO_TX_PACKETS])
  565. data->tx_packets =
  566. nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
  567. return NL_SKIP;
  568. }
  569. static int i802_read_sta_data(void *priv, struct hostap_sta_driver_data *data,
  570. const u8 *addr)
  571. {
  572. struct i802_driver_data *drv = priv;
  573. struct nl_msg *msg;
  574. os_memset(data, 0, sizeof(*data));
  575. msg = nlmsg_alloc();
  576. if (!msg)
  577. return -ENOMEM;
  578. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  579. 0, NL80211_CMD_GET_STATION, 0);
  580. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
  581. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
  582. return send_and_recv_msgs(drv, msg, get_sta_handler, data);
  583. nla_put_failure:
  584. return -ENOBUFS;
  585. }
  586. static int i802_send_eapol(void *priv, const u8 *addr, const u8 *data,
  587. size_t data_len, int encrypt, const u8 *own_addr)
  588. {
  589. struct i802_driver_data *drv = priv;
  590. struct ieee80211_hdr *hdr;
  591. size_t len;
  592. u8 *pos;
  593. int res;
  594. #if 0 /* FIX */
  595. int qos = sta->flags & WLAN_STA_WME;
  596. #else
  597. int qos = 0;
  598. #endif
  599. len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
  600. data_len;
  601. hdr = os_zalloc(len);
  602. if (hdr == NULL) {
  603. printf("malloc() failed for i802_send_data(len=%lu)\n",
  604. (unsigned long) len);
  605. return -1;
  606. }
  607. hdr->frame_control =
  608. IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
  609. hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
  610. if (encrypt)
  611. hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
  612. #if 0 /* To be enabled if qos determination is added above */
  613. if (qos) {
  614. hdr->frame_control |=
  615. host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
  616. }
  617. #endif
  618. memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
  619. memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
  620. memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
  621. pos = (u8 *) (hdr + 1);
  622. #if 0 /* To be enabled if qos determination is added above */
  623. if (qos) {
  624. /* add an empty QoS header if needed */
  625. pos[0] = 0;
  626. pos[1] = 0;
  627. pos += 2;
  628. }
  629. #endif
  630. memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
  631. pos += sizeof(rfc1042_header);
  632. WPA_PUT_BE16(pos, ETH_P_PAE);
  633. pos += 2;
  634. memcpy(pos, data, data_len);
  635. res = i802_send_frame(drv, (u8 *) hdr, len, encrypt, 0);
  636. free(hdr);
  637. if (res < 0) {
  638. perror("i802_send_eapol: send");
  639. printf("i802_send_eapol - packet len: %lu - failed\n",
  640. (unsigned long) len);
  641. }
  642. return res;
  643. }
  644. static int i802_sta_add(const char *ifname, void *priv,
  645. struct hostapd_sta_add_params *params)
  646. {
  647. struct i802_driver_data *drv = priv;
  648. struct nl_msg *msg;
  649. int ret = -ENOBUFS;
  650. msg = nlmsg_alloc();
  651. if (!msg)
  652. return -ENOMEM;
  653. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  654. 0, NL80211_CMD_NEW_STATION, 0);
  655. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
  656. if_nametoindex(drv->iface));
  657. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr);
  658. NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, params->aid);
  659. NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, params->supp_rates_len,
  660. params->supp_rates);
  661. NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
  662. params->listen_interval);
  663. #ifdef CONFIG_IEEE80211N
  664. if (params->ht_capabilities) {
  665. NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY,
  666. params->ht_capabilities->length,
  667. &params->ht_capabilities->data);
  668. }
  669. #endif /* CONFIG_IEEE80211N */
  670. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  671. if (ret)
  672. wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_NEW_STATION "
  673. "result: %d (%s)", ret, strerror(-ret));
  674. if (ret == -EEXIST)
  675. ret = 0;
  676. nla_put_failure:
  677. return ret;
  678. }
  679. static int i802_sta_remove(void *priv, const u8 *addr)
  680. {
  681. struct i802_driver_data *drv = priv;
  682. struct nl_msg *msg;
  683. int ret;
  684. msg = nlmsg_alloc();
  685. if (!msg)
  686. return -ENOMEM;
  687. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  688. 0, NL80211_CMD_DEL_STATION, 0);
  689. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
  690. if_nametoindex(drv->iface));
  691. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
  692. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  693. if (ret == -ENOENT)
  694. return 0;
  695. return ret;
  696. nla_put_failure:
  697. return -ENOBUFS;
  698. }
  699. static int i802_sta_set_flags(void *priv, const u8 *addr,
  700. int total_flags, int flags_or, int flags_and)
  701. {
  702. struct i802_driver_data *drv = priv;
  703. struct nl_msg *msg, *flags = NULL;
  704. msg = nlmsg_alloc();
  705. if (!msg)
  706. return -ENOMEM;
  707. flags = nlmsg_alloc();
  708. if (!flags) {
  709. nlmsg_free(msg);
  710. return -ENOMEM;
  711. }
  712. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  713. 0, NL80211_CMD_SET_STATION, 0);
  714. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
  715. if_nametoindex(drv->iface));
  716. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
  717. if (total_flags & WLAN_STA_AUTHORIZED || !drv->ieee802_1x_active)
  718. NLA_PUT_FLAG(flags, NL80211_STA_FLAG_AUTHORIZED);
  719. if (total_flags & WLAN_STA_WMM)
  720. NLA_PUT_FLAG(flags, NL80211_STA_FLAG_WME);
  721. if (total_flags & WLAN_STA_SHORT_PREAMBLE)
  722. NLA_PUT_FLAG(flags, NL80211_STA_FLAG_SHORT_PREAMBLE);
  723. if (total_flags & WLAN_STA_MFP)
  724. NLA_PUT_FLAG(flags, NL80211_STA_FLAG_MFP);
  725. if (nla_put_nested(msg, NL80211_ATTR_STA_FLAGS, flags))
  726. goto nla_put_failure;
  727. nlmsg_free(flags);
  728. return send_and_recv_msgs(drv, msg, NULL, NULL);
  729. nla_put_failure:
  730. nlmsg_free(flags);
  731. return -ENOBUFS;
  732. }
  733. static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
  734. int cw_min, int cw_max, int burst_time)
  735. {
  736. struct i802_driver_data *drv = priv;
  737. struct nl_msg *msg;
  738. struct nlattr *txq, *params;
  739. msg = nlmsg_alloc();
  740. if (!msg)
  741. return -1;
  742. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  743. 0, NL80211_CMD_SET_WIPHY, 0);
  744. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
  745. txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
  746. if (!txq)
  747. goto nla_put_failure;
  748. /* We are only sending parameters for a single TXQ at a time */
  749. params = nla_nest_start(msg, 1);
  750. if (!params)
  751. goto nla_put_failure;
  752. NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, queue);
  753. /* Burst time is configured in units of 0.1 msec and TXOP parameter in
  754. * 32 usec, so need to convert the value here. */
  755. NLA_PUT_U16(msg, NL80211_TXQ_ATTR_TXOP, (burst_time * 100 + 16) / 32);
  756. NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min);
  757. NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max);
  758. NLA_PUT_U8(msg, NL80211_TXQ_ATTR_AIFS, aifs);
  759. nla_nest_end(msg, params);
  760. nla_nest_end(msg, txq);
  761. if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
  762. return 0;
  763. nla_put_failure:
  764. return -1;
  765. }
  766. static void nl80211_remove_iface(struct i802_driver_data *drv, int ifidx)
  767. {
  768. struct nl_msg *msg;
  769. /* stop listening for EAPOL on this interface */
  770. del_ifidx(drv, ifidx);
  771. msg = nlmsg_alloc();
  772. if (!msg)
  773. goto nla_put_failure;
  774. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  775. 0, NL80211_CMD_DEL_INTERFACE, 0);
  776. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx);
  777. if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
  778. return;
  779. nla_put_failure:
  780. printf("Failed to remove interface.\n");
  781. }
  782. static int nl80211_create_iface(struct i802_driver_data *drv,
  783. const char *ifname,
  784. enum nl80211_iftype iftype,
  785. const u8 *addr)
  786. {
  787. struct nl_msg *msg, *flags = NULL;
  788. int ifidx;
  789. struct ifreq ifreq;
  790. struct iwreq iwr;
  791. int ret = -ENOBUFS;
  792. msg = nlmsg_alloc();
  793. if (!msg)
  794. return -1;
  795. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  796. 0, NL80211_CMD_NEW_INTERFACE, 0);
  797. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
  798. NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname);
  799. NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
  800. if (iftype == NL80211_IFTYPE_MONITOR) {
  801. int err;
  802. flags = nlmsg_alloc();
  803. if (!flags)
  804. goto nla_put_failure;
  805. NLA_PUT_FLAG(flags, NL80211_MNTR_FLAG_COOK_FRAMES);
  806. err = nla_put_nested(msg, NL80211_ATTR_MNTR_FLAGS, flags);
  807. nlmsg_free(flags);
  808. if (err)
  809. goto nla_put_failure;
  810. }
  811. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  812. if (ret) {
  813. nla_put_failure:
  814. printf("Failed to create interface %s.\n", ifname);
  815. return ret;
  816. }
  817. ifidx = if_nametoindex(ifname);
  818. if (ifidx <= 0)
  819. return -1;
  820. /* start listening for EAPOL on this interface */
  821. add_ifidx(drv, ifidx);
  822. if (addr) {
  823. switch (iftype) {
  824. case NL80211_IFTYPE_AP:
  825. os_strlcpy(ifreq.ifr_name, ifname, IFNAMSIZ);
  826. memcpy(ifreq.ifr_hwaddr.sa_data, addr, ETH_ALEN);
  827. ifreq.ifr_hwaddr.sa_family = ARPHRD_ETHER;
  828. if (ioctl(drv->ioctl_sock, SIOCSIFHWADDR, &ifreq)) {
  829. nl80211_remove_iface(drv, ifidx);
  830. return -1;
  831. }
  832. break;
  833. case NL80211_IFTYPE_WDS:
  834. memset(&iwr, 0, sizeof(iwr));
  835. os_strlcpy(iwr.ifr_name, ifname, IFNAMSIZ);
  836. iwr.u.addr.sa_family = ARPHRD_ETHER;
  837. memcpy(iwr.u.addr.sa_data, addr, ETH_ALEN);
  838. if (ioctl(drv->ioctl_sock, SIOCSIWAP, &iwr))
  839. return -1;
  840. break;
  841. default:
  842. /* nothing */
  843. break;
  844. }
  845. }
  846. return ifidx;
  847. }
  848. static int i802_bss_add(void *priv, const char *ifname, const u8 *bssid)
  849. {
  850. struct i802_driver_data *drv = priv;
  851. int ifidx;
  852. struct i802_bss *bss;
  853. bss = os_zalloc(sizeof(*bss));
  854. if (bss == NULL)
  855. return -1;
  856. os_strlcpy(bss->iface, ifname, IFNAMSIZ);
  857. ifidx = nl80211_create_iface(priv, ifname, NL80211_IFTYPE_AP, bssid);
  858. if (ifidx < 0) {
  859. os_free(bss);
  860. return -1;
  861. }
  862. if (hostapd_set_iface_flags(priv, ifname, 1)) {
  863. nl80211_remove_iface(priv, ifidx);
  864. os_free(bss);
  865. return -1;
  866. }
  867. bss->next = drv->bss.next;
  868. drv->bss.next = bss;
  869. return 0;
  870. }
  871. static int i802_bss_remove(void *priv, const char *ifname)
  872. {
  873. struct i802_driver_data *drv = priv;
  874. struct i802_bss *bss, *prev;
  875. nl80211_remove_iface(priv, if_nametoindex(ifname));
  876. prev = &drv->bss;
  877. bss = drv->bss.next;
  878. while (bss) {
  879. if (os_strncmp(ifname, bss->iface, IFNAMSIZ) == 0) {
  880. prev->next = bss->next;
  881. os_free(bss);
  882. break;
  883. }
  884. prev = bss;
  885. bss = bss->next;
  886. }
  887. return 0;
  888. }
  889. static int i802_set_beacon(const char *iface, void *priv,
  890. const u8 *head, size_t head_len,
  891. const u8 *tail, size_t tail_len, int dtim_period)
  892. {
  893. struct i802_driver_data *drv = priv;
  894. struct nl_msg *msg;
  895. u8 cmd = NL80211_CMD_NEW_BEACON;
  896. int ret;
  897. struct i802_bss *bss;
  898. bss = get_bss(drv, iface);
  899. if (bss == NULL)
  900. return -ENOENT;
  901. msg = nlmsg_alloc();
  902. if (!msg)
  903. return -ENOMEM;
  904. wpa_printf(MSG_DEBUG, "nl80211: Set beacon (iface=%s beacon_set=%d)",
  905. iface, bss->beacon_set);
  906. if (bss->beacon_set)
  907. cmd = NL80211_CMD_SET_BEACON;
  908. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  909. 0, cmd, 0);
  910. NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, head_len, head);
  911. NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL, tail_len, tail);
  912. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
  913. NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, drv->beacon_int);
  914. NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, dtim_period);
  915. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  916. if (!ret)
  917. bss->beacon_set = 1;
  918. return ret;
  919. nla_put_failure:
  920. return -ENOBUFS;
  921. }
  922. static int i802_del_beacon(struct i802_driver_data *drv)
  923. {
  924. struct nl_msg *msg;
  925. msg = nlmsg_alloc();
  926. if (!msg)
  927. return -ENOMEM;
  928. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  929. 0, NL80211_CMD_DEL_BEACON, 0);
  930. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
  931. return send_and_recv_msgs(drv, msg, NULL, NULL);
  932. nla_put_failure:
  933. return -ENOBUFS;
  934. }
  935. static int i802_set_ieee8021x(const char *ifname, void *priv, int enabled)
  936. {
  937. struct i802_driver_data *drv = priv;
  938. /*
  939. * FIXME: This needs to be per interface (BSS)
  940. */
  941. drv->ieee802_1x_active = enabled;
  942. return 0;
  943. }
  944. static int i802_set_privacy(const char *ifname, void *priv, int enabled)
  945. {
  946. struct i802_driver_data *drv = priv;
  947. struct iwreq iwr;
  948. memset(&iwr, 0, sizeof(iwr));
  949. os_strlcpy(iwr.ifr_name, ifname, IFNAMSIZ);
  950. iwr.u.param.flags = IW_AUTH_PRIVACY_INVOKED;
  951. iwr.u.param.value = enabled;
  952. ioctl(drv->ioctl_sock, SIOCSIWAUTH, &iwr);
  953. /* ignore errors, the kernel/driver might not care */
  954. return 0;
  955. }
  956. static int i802_set_internal_bridge(void *priv, int value)
  957. {
  958. return -1;
  959. }
  960. static int i802_set_beacon_int(void *priv, int value)
  961. {
  962. struct i802_driver_data *drv = priv;
  963. struct nl_msg *msg;
  964. drv->beacon_int = value;
  965. if (!drv->bss.beacon_set)
  966. return 0;
  967. msg = nlmsg_alloc();
  968. if (!msg)
  969. return -ENOMEM;
  970. wpa_printf(MSG_DEBUG, "nl80211: Set beacon interval %d "
  971. "(beacon_set=%d)", value, drv->bss.beacon_set);
  972. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  973. 0, NL80211_CMD_SET_BEACON, 0);
  974. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
  975. NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, value);
  976. return send_and_recv_msgs(drv, msg, NULL, NULL);
  977. nla_put_failure:
  978. return -ENOBUFS;
  979. }
  980. static int i802_set_bss(void *priv, int cts, int preamble, int slot)
  981. {
  982. struct i802_driver_data *drv = priv;
  983. struct nl_msg *msg;
  984. msg = nlmsg_alloc();
  985. if (!msg)
  986. return -ENOMEM;
  987. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  988. NL80211_CMD_SET_BSS, 0);
  989. if (cts >= 0)
  990. NLA_PUT_U8(msg, NL80211_ATTR_BSS_CTS_PROT, cts);
  991. if (preamble >= 0)
  992. NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble);
  993. if (slot >= 0)
  994. NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot);
  995. /* TODO: multi-BSS support */
  996. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
  997. return send_and_recv_msgs(drv, msg, NULL, NULL);
  998. nla_put_failure:
  999. return -ENOBUFS;
  1000. }
  1001. static int i802_set_cts_protect(void *priv, int value)
  1002. {
  1003. return i802_set_bss(priv, value, -1, -1);
  1004. }
  1005. static int i802_set_preamble(void *priv, int value)
  1006. {
  1007. return i802_set_bss(priv, -1, value, -1);
  1008. }
  1009. static int i802_set_short_slot_time(void *priv, int value)
  1010. {
  1011. return i802_set_bss(priv, -1, -1, value);
  1012. }
  1013. static enum nl80211_iftype i802_if_type(enum hostapd_driver_if_type type)
  1014. {
  1015. switch (type) {
  1016. case HOSTAPD_IF_VLAN:
  1017. return NL80211_IFTYPE_AP_VLAN;
  1018. case HOSTAPD_IF_WDS:
  1019. return NL80211_IFTYPE_WDS;
  1020. }
  1021. return -1;
  1022. }
  1023. static int i802_if_add(const char *iface, void *priv,
  1024. enum hostapd_driver_if_type type, char *ifname,
  1025. const u8 *addr)
  1026. {
  1027. if (nl80211_create_iface(priv, ifname, i802_if_type(type), addr) < 0)
  1028. return -1;
  1029. return 0;
  1030. }
  1031. static int i802_if_update(void *priv, enum hostapd_driver_if_type type,
  1032. char *ifname, const u8 *addr)
  1033. {
  1034. /* unused at the moment */
  1035. return -1;
  1036. }
  1037. static int i802_if_remove(void *priv, enum hostapd_driver_if_type type,
  1038. const char *ifname, const u8 *addr)
  1039. {
  1040. nl80211_remove_iface(priv, if_nametoindex(ifname));
  1041. return 0;
  1042. }
  1043. struct phy_info_arg {
  1044. u16 *num_modes;
  1045. struct hostapd_hw_modes *modes;
  1046. };
  1047. static int phy_info_handler(struct nl_msg *msg, void *arg)
  1048. {
  1049. struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
  1050. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  1051. struct phy_info_arg *phy_info = arg;
  1052. struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
  1053. struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
  1054. static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
  1055. [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
  1056. [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
  1057. [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
  1058. [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG },
  1059. [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
  1060. [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
  1061. };
  1062. struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
  1063. static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
  1064. [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
  1065. [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] = { .type = NLA_FLAG },
  1066. };
  1067. struct nlattr *nl_band;
  1068. struct nlattr *nl_freq;
  1069. struct nlattr *nl_rate;
  1070. int rem_band, rem_freq, rem_rate;
  1071. struct hostapd_hw_modes *mode;
  1072. int idx, mode_is_set;
  1073. nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  1074. genlmsg_attrlen(gnlh, 0), NULL);
  1075. if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
  1076. return NL_SKIP;
  1077. nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band) {
  1078. mode = realloc(phy_info->modes, (*phy_info->num_modes + 1) * sizeof(*mode));
  1079. if (!mode)
  1080. return NL_SKIP;
  1081. phy_info->modes = mode;
  1082. mode_is_set = 0;
  1083. mode = &phy_info->modes[*(phy_info->num_modes)];
  1084. memset(mode, 0, sizeof(*mode));
  1085. *(phy_info->num_modes) += 1;
  1086. nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
  1087. nla_len(nl_band), NULL);
  1088. if (tb_band[NL80211_BAND_ATTR_HT_CAPA]) {
  1089. mode->ht_capab = nla_get_u16(
  1090. tb_band[NL80211_BAND_ATTR_HT_CAPA]);
  1091. }
  1092. nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
  1093. nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
  1094. nla_len(nl_freq), freq_policy);
  1095. if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
  1096. continue;
  1097. mode->num_channels++;
  1098. }
  1099. mode->channels = calloc(mode->num_channels, sizeof(struct hostapd_channel_data));
  1100. if (!mode->channels)
  1101. return NL_SKIP;
  1102. idx = 0;
  1103. nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
  1104. nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
  1105. nla_len(nl_freq), freq_policy);
  1106. if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
  1107. continue;
  1108. mode->channels[idx].freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
  1109. mode->channels[idx].flag = 0;
  1110. if (!mode_is_set) {
  1111. /* crude heuristic */
  1112. if (mode->channels[idx].freq < 4000)
  1113. mode->mode = HOSTAPD_MODE_IEEE80211B;
  1114. else
  1115. mode->mode = HOSTAPD_MODE_IEEE80211A;
  1116. mode_is_set = 1;
  1117. }
  1118. /* crude heuristic */
  1119. if (mode->channels[idx].freq < 4000)
  1120. if (mode->channels[idx].freq == 2848)
  1121. mode->channels[idx].chan = 14;
  1122. else
  1123. mode->channels[idx].chan = (mode->channels[idx].freq - 2407) / 5;
  1124. else
  1125. mode->channels[idx].chan = mode->channels[idx].freq/5 - 1000;
  1126. if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
  1127. mode->channels[idx].flag |=
  1128. HOSTAPD_CHAN_DISABLED;
  1129. if (tb_freq[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN])
  1130. mode->channels[idx].flag |=
  1131. HOSTAPD_CHAN_PASSIVE_SCAN;
  1132. if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IBSS])
  1133. mode->channels[idx].flag |=
  1134. HOSTAPD_CHAN_NO_IBSS;
  1135. if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR])
  1136. mode->channels[idx].flag |=
  1137. HOSTAPD_CHAN_RADAR;
  1138. if (tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER] &&
  1139. !tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
  1140. mode->channels[idx].max_tx_power =
  1141. nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER]) / 100;
  1142. idx++;
  1143. }
  1144. nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
  1145. nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
  1146. nla_len(nl_rate), rate_policy);
  1147. if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
  1148. continue;
  1149. mode->num_rates++;
  1150. }
  1151. mode->rates = calloc(mode->num_rates, sizeof(struct hostapd_rate_data));
  1152. if (!mode->rates)
  1153. return NL_SKIP;
  1154. idx = 0;
  1155. nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
  1156. nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
  1157. nla_len(nl_rate), rate_policy);
  1158. if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
  1159. continue;
  1160. mode->rates[idx].rate = nla_get_u32(tb_rate[NL80211_BITRATE_ATTR_RATE]);
  1161. /* crude heuristic */
  1162. if (mode->mode == HOSTAPD_MODE_IEEE80211B &&
  1163. mode->rates[idx].rate > 200)
  1164. mode->mode = HOSTAPD_MODE_IEEE80211G;
  1165. if (tb_rate[NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE])
  1166. mode->rates[idx].flags |= HOSTAPD_RATE_PREAMBLE2;
  1167. idx++;
  1168. }
  1169. }
  1170. return NL_SKIP;
  1171. }
  1172. static struct hostapd_hw_modes *i802_add_11b(struct hostapd_hw_modes *modes,
  1173. u16 *num_modes)
  1174. {
  1175. u16 m;
  1176. struct hostapd_hw_modes *mode11g = NULL, *nmodes, *mode;
  1177. int i, mode11g_idx = -1;
  1178. /* If only 802.11g mode is included, use it to construct matching
  1179. * 802.11b mode data. */
  1180. for (m = 0; m < *num_modes; m++) {
  1181. if (modes[m].mode == HOSTAPD_MODE_IEEE80211B)
  1182. return modes; /* 802.11b already included */
  1183. if (modes[m].mode == HOSTAPD_MODE_IEEE80211G)
  1184. mode11g_idx = m;
  1185. }
  1186. if (mode11g_idx < 0)
  1187. return modes; /* 2.4 GHz band not supported at all */
  1188. nmodes = os_realloc(modes, (*num_modes + 1) * sizeof(*nmodes));
  1189. if (nmodes == NULL)
  1190. return modes; /* Could not add 802.11b mode */
  1191. mode = &nmodes[*num_modes];
  1192. os_memset(mode, 0, sizeof(*mode));
  1193. (*num_modes)++;
  1194. modes = nmodes;
  1195. mode->mode = HOSTAPD_MODE_IEEE80211B;
  1196. mode11g = &modes[mode11g_idx];
  1197. mode->num_channels = mode11g->num_channels;
  1198. mode->channels = os_malloc(mode11g->num_channels *
  1199. sizeof(struct hostapd_channel_data));
  1200. if (mode->channels == NULL) {
  1201. (*num_modes)--;
  1202. return modes; /* Could not add 802.11b mode */
  1203. }
  1204. os_memcpy(mode->channels, mode11g->channels,
  1205. mode11g->num_channels * sizeof(struct hostapd_channel_data));
  1206. mode->num_rates = 0;
  1207. mode->rates = os_malloc(4 * sizeof(struct hostapd_rate_data));
  1208. if (mode->rates == NULL) {
  1209. os_free(mode->channels);
  1210. (*num_modes)--;
  1211. return modes; /* Could not add 802.11b mode */
  1212. }
  1213. for (i = 0; i < mode11g->num_rates; i++) {
  1214. if (mode11g->rates[i].rate > 110 ||
  1215. mode11g->rates[i].flags &
  1216. (HOSTAPD_RATE_ERP | HOSTAPD_RATE_OFDM))
  1217. continue;
  1218. mode->rates[mode->num_rates] = mode11g->rates[i];
  1219. mode->num_rates++;
  1220. if (mode->num_rates == 4)
  1221. break;
  1222. }
  1223. if (mode->num_rates == 0) {
  1224. os_free(mode->channels);
  1225. os_free(mode->rates);
  1226. (*num_modes)--;
  1227. return modes; /* No 802.11b rates */
  1228. }
  1229. wpa_printf(MSG_DEBUG, "nl80211: Added 802.11b mode based on 802.11g "
  1230. "information");
  1231. return modes;
  1232. }
  1233. static struct hostapd_hw_modes *i802_get_hw_feature_data(void *priv,
  1234. u16 *num_modes,
  1235. u16 *flags)
  1236. {
  1237. struct i802_driver_data *drv = priv;
  1238. struct nl_msg *msg;
  1239. struct phy_info_arg result = {
  1240. .num_modes = num_modes,
  1241. .modes = NULL,
  1242. };
  1243. *num_modes = 0;
  1244. *flags = 0;
  1245. msg = nlmsg_alloc();
  1246. if (!msg)
  1247. return NULL;
  1248. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  1249. 0, NL80211_CMD_GET_WIPHY, 0);
  1250. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
  1251. if (send_and_recv_msgs(drv, msg, phy_info_handler, &result) == 0)
  1252. return i802_add_11b(result.modes, num_modes);
  1253. nla_put_failure:
  1254. return NULL;
  1255. }
  1256. static int i802_set_sta_vlan(void *priv, const u8 *addr,
  1257. const char *ifname, int vlan_id)
  1258. {
  1259. struct i802_driver_data *drv = priv;
  1260. struct nl_msg *msg;
  1261. msg = nlmsg_alloc();
  1262. if (!msg)
  1263. return -ENOMEM;
  1264. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  1265. 0, NL80211_CMD_SET_STATION, 0);
  1266. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
  1267. if_nametoindex(drv->iface));
  1268. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
  1269. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
  1270. if_nametoindex(ifname));
  1271. return send_and_recv_msgs(drv, msg, NULL, NULL);
  1272. nla_put_failure:
  1273. return -ENOBUFS;
  1274. }
  1275. static int i802_set_country(void *priv, const char *country)
  1276. {
  1277. struct i802_driver_data *drv = priv;
  1278. struct nl_msg *msg;
  1279. char alpha2[3];
  1280. msg = nlmsg_alloc();
  1281. if (!msg)
  1282. return -ENOMEM;
  1283. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  1284. 0, NL80211_CMD_REQ_SET_REG, 0);
  1285. alpha2[0] = country[0];
  1286. alpha2[1] = country[1];
  1287. alpha2[2] = '\0';
  1288. NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2);
  1289. return send_and_recv_msgs(drv, msg, NULL, NULL);
  1290. nla_put_failure:
  1291. return -ENOBUFS;
  1292. }
  1293. static void handle_tx_callback(struct hostapd_data *hapd, u8 *buf, size_t len,
  1294. int ok)
  1295. {
  1296. struct ieee80211_hdr *hdr;
  1297. u16 fc, type, stype;
  1298. hdr = (struct ieee80211_hdr *) buf;
  1299. fc = le_to_host16(hdr->frame_control);
  1300. type = WLAN_FC_GET_TYPE(fc);
  1301. stype = WLAN_FC_GET_STYPE(fc);
  1302. switch (type) {
  1303. case WLAN_FC_TYPE_MGMT:
  1304. wpa_printf(MSG_DEBUG, "MGMT (TX callback) %s",
  1305. ok ? "ACK" : "fail");
  1306. hostapd_mgmt_tx_cb(hapd, buf, len, stype, ok);
  1307. break;
  1308. case WLAN_FC_TYPE_CTRL:
  1309. wpa_printf(MSG_DEBUG, "CTRL (TX callback) %s",
  1310. ok ? "ACK" : "fail");
  1311. break;
  1312. case WLAN_FC_TYPE_DATA:
  1313. hostapd_tx_status(hapd, hdr->addr1, buf, len, ok);
  1314. break;
  1315. default:
  1316. printf("unknown TX callback frame type %d\n", type);
  1317. break;
  1318. }
  1319. }
  1320. static void handle_frame(struct i802_driver_data *drv,
  1321. struct hostapd_iface *iface, u8 *buf, size_t len,
  1322. struct hostapd_frame_info *hfi,
  1323. enum ieee80211_msg_type msg_type)
  1324. {
  1325. struct ieee80211_hdr *hdr;
  1326. u16 fc, type, stype;
  1327. size_t data_len = len;
  1328. struct hostapd_data *hapd = NULL;
  1329. int broadcast_bssid = 0;
  1330. size_t i;
  1331. u8 *bssid;
  1332. /*
  1333. * PS-Poll frames are 16 bytes. All other frames are
  1334. * 24 bytes or longer.
  1335. */
  1336. if (len < 16)
  1337. return;
  1338. hdr = (struct ieee80211_hdr *) buf;
  1339. fc = le_to_host16(hdr->frame_control);
  1340. type = WLAN_FC_GET_TYPE(fc);
  1341. stype = WLAN_FC_GET_STYPE(fc);
  1342. switch (type) {
  1343. case WLAN_FC_TYPE_DATA:
  1344. if (len < 24)
  1345. return;
  1346. switch (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) {
  1347. case WLAN_FC_TODS:
  1348. bssid = hdr->addr1;
  1349. break;
  1350. case WLAN_FC_FROMDS:
  1351. bssid = hdr->addr2;
  1352. break;
  1353. default:
  1354. /* discard */
  1355. return;
  1356. }
  1357. break;
  1358. case WLAN_FC_TYPE_CTRL:
  1359. /* discard non-ps-poll frames */
  1360. if (stype != WLAN_FC_STYPE_PSPOLL)
  1361. return;
  1362. bssid = hdr->addr1;
  1363. break;
  1364. case WLAN_FC_TYPE_MGMT:
  1365. bssid = hdr->addr3;
  1366. break;
  1367. default:
  1368. /* discard */
  1369. return;
  1370. }
  1371. /* find interface frame belongs to */
  1372. for (i = 0; i < iface->num_bss; i++) {
  1373. if (memcmp(bssid, iface->bss[i]->own_addr, ETH_ALEN) == 0) {
  1374. hapd = iface->bss[i];
  1375. break;
  1376. }
  1377. }
  1378. if (hapd == NULL) {
  1379. hapd = iface->bss[0];
  1380. if (bssid[0] != 0xff || bssid[1] != 0xff ||
  1381. bssid[2] != 0xff || bssid[3] != 0xff ||
  1382. bssid[4] != 0xff || bssid[5] != 0xff) {
  1383. /*
  1384. * Unknown BSSID - drop frame if this is not from
  1385. * passive scanning or a beacon (at least ProbeReq
  1386. * frames to other APs may be allowed through RX
  1387. * filtering in the wlan hw/driver)
  1388. */
  1389. if ((type != WLAN_FC_TYPE_MGMT ||
  1390. stype != WLAN_FC_STYPE_BEACON))
  1391. return;
  1392. } else
  1393. broadcast_bssid = 1;
  1394. }
  1395. switch (msg_type) {
  1396. case ieee80211_msg_normal:
  1397. /* continue processing */
  1398. break;
  1399. case ieee80211_msg_tx_callback_ack:
  1400. handle_tx_callback(hapd, buf, data_len, 1);
  1401. return;
  1402. case ieee80211_msg_tx_callback_fail:
  1403. handle_tx_callback(hapd, buf, data_len, 0);
  1404. return;
  1405. }
  1406. switch (type) {
  1407. case WLAN_FC_TYPE_MGMT:
  1408. if (stype != WLAN_FC_STYPE_BEACON &&
  1409. stype != WLAN_FC_STYPE_PROBE_REQ)
  1410. wpa_printf(MSG_MSGDUMP, "MGMT");
  1411. if (broadcast_bssid) {
  1412. for (i = 0; i < iface->num_bss; i++)
  1413. hostapd_mgmt_rx(iface->bss[i], buf, data_len,
  1414. stype, hfi);
  1415. } else
  1416. hostapd_mgmt_rx(hapd, buf, data_len, stype, hfi);
  1417. break;
  1418. case WLAN_FC_TYPE_CTRL:
  1419. /* can only get here with PS-Poll frames */
  1420. wpa_printf(MSG_DEBUG, "CTRL");
  1421. hostapd_rx_from_unknown_sta(drv->hapd, hdr->addr2);
  1422. break;
  1423. case WLAN_FC_TYPE_DATA:
  1424. hostapd_rx_from_unknown_sta(drv->hapd, hdr->addr2);
  1425. break;
  1426. }
  1427. }
  1428. static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
  1429. {
  1430. struct i802_driver_data *drv = eloop_ctx;
  1431. struct hostapd_data *hapd = drv->hapd;
  1432. struct sockaddr_ll lladdr;
  1433. unsigned char buf[3000];
  1434. int len;
  1435. socklen_t fromlen = sizeof(lladdr);
  1436. len = recvfrom(sock, buf, sizeof(buf), 0,
  1437. (struct sockaddr *)&lladdr, &fromlen);
  1438. if (len < 0) {
  1439. perror("recv");
  1440. return;
  1441. }
  1442. if (have_ifidx(drv, lladdr.sll_ifindex))
  1443. hostapd_eapol_receive(hapd, lladdr.sll_addr, buf, len);
  1444. }
  1445. static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
  1446. {
  1447. struct i802_driver_data *drv = eloop_ctx;
  1448. int len;
  1449. unsigned char buf[3000];
  1450. struct hostapd_data *hapd = drv->hapd;
  1451. struct ieee80211_radiotap_iterator iter;
  1452. int ret;
  1453. struct hostapd_frame_info hfi;
  1454. int injected = 0, failed = 0, msg_type, rxflags = 0;
  1455. len = recv(sock, buf, sizeof(buf), 0);
  1456. if (len < 0) {
  1457. perror("recv");
  1458. return;
  1459. }
  1460. if (ieee80211_radiotap_iterator_init(&iter, (void*)buf, len)) {
  1461. printf("received invalid radiotap frame\n");
  1462. return;
  1463. }
  1464. memset(&hfi, 0, sizeof(hfi));
  1465. while (1) {
  1466. ret = ieee80211_radiotap_iterator_next(&iter);
  1467. if (ret == -ENOENT)
  1468. break;
  1469. if (ret) {
  1470. printf("received invalid radiotap frame (%d)\n", ret);
  1471. return;
  1472. }
  1473. switch (iter.this_arg_index) {
  1474. case IEEE80211_RADIOTAP_FLAGS:
  1475. if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
  1476. len -= 4;
  1477. break;
  1478. case IEEE80211_RADIOTAP_RX_FLAGS:
  1479. rxflags = 1;
  1480. break;
  1481. case IEEE80211_RADIOTAP_TX_FLAGS:
  1482. injected = 1;
  1483. failed = le_to_host16((*(uint16_t *) iter.this_arg)) &
  1484. IEEE80211_RADIOTAP_F_TX_FAIL;
  1485. break;
  1486. case IEEE80211_RADIOTAP_DATA_RETRIES:
  1487. break;
  1488. case IEEE80211_RADIOTAP_CHANNEL:
  1489. /* TODO convert from freq/flags to channel number
  1490. hfi.channel = XXX;
  1491. hfi.phytype = XXX;
  1492. */
  1493. break;
  1494. case IEEE80211_RADIOTAP_RATE:
  1495. hfi.datarate = *iter.this_arg * 5;
  1496. break;
  1497. case IEEE80211_RADIOTAP_DB_ANTSIGNAL:
  1498. hfi.ssi_signal = *iter.this_arg;
  1499. break;
  1500. }
  1501. }
  1502. if (rxflags && injected)
  1503. return;
  1504. if (!injected)
  1505. msg_type = ieee80211_msg_normal;
  1506. else if (failed)
  1507. msg_type = ieee80211_msg_tx_callback_fail;
  1508. else
  1509. msg_type = ieee80211_msg_tx_callback_ack;
  1510. handle_frame(drv, hapd->iface, buf + iter.max_length,
  1511. len - iter.max_length, &hfi, msg_type);
  1512. }
  1513. /*
  1514. * we post-process the filter code later and rewrite
  1515. * this to the offset to the last instruction
  1516. */
  1517. #define PASS 0xFF
  1518. #define FAIL 0xFE
  1519. static struct sock_filter msock_filter_insns[] = {
  1520. /*
  1521. * do a little-endian load of the radiotap length field
  1522. */
  1523. /* load lower byte into A */
  1524. BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 2),
  1525. /* put it into X (== index register) */
  1526. BPF_STMT(BPF_MISC| BPF_TAX, 0),
  1527. /* load upper byte into A */
  1528. BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 3),
  1529. /* left-shift it by 8 */
  1530. BPF_STMT(BPF_ALU | BPF_LSH | BPF_K, 8),
  1531. /* or with X */
  1532. BPF_STMT(BPF_ALU | BPF_OR | BPF_X, 0),
  1533. /* put result into X */
  1534. BPF_STMT(BPF_MISC| BPF_TAX, 0),
  1535. /*
  1536. * Allow management frames through, this also gives us those
  1537. * management frames that we sent ourselves with status
  1538. */
  1539. /* load the lower byte of the IEEE 802.11 frame control field */
  1540. BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
  1541. /* mask off frame type and version */
  1542. BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xF),
  1543. /* accept frame if it's both 0, fall through otherwise */
  1544. BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, PASS, 0),
  1545. /*
  1546. * TODO: add a bit to radiotap RX flags that indicates
  1547. * that the sending station is not associated, then
  1548. * add a filter here that filters on our DA and that flag
  1549. * to allow us to deauth frames to that bad station.
  1550. *
  1551. * Not a regression -- we didn't do it before either.
  1552. */
  1553. #if 0
  1554. /*
  1555. * drop non-data frames, WDS frames
  1556. */
  1557. /* load the lower byte of the frame control field */
  1558. BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
  1559. /* mask off QoS bit */
  1560. BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0c),
  1561. /* drop non-data frames */
  1562. BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 8, 0, FAIL),
  1563. /* load the upper byte of the frame control field */
  1564. BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
  1565. /* mask off toDS/fromDS */
  1566. BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x03),
  1567. /* drop WDS frames */
  1568. BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 3, FAIL, 0),
  1569. #endif
  1570. /*
  1571. * add header length to index
  1572. */
  1573. /* load the lower byte of the frame control field */
  1574. BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
  1575. /* mask off QoS bit */
  1576. BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x80),
  1577. /* right shift it by 6 to give 0 or 2 */
  1578. BPF_STMT(BPF_ALU | BPF_RSH | BPF_K, 6),
  1579. /* add data frame header length */
  1580. BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 24),
  1581. /* add index, was start of 802.11 header */
  1582. BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
  1583. /* move to index, now start of LL header */
  1584. BPF_STMT(BPF_MISC | BPF_TAX, 0),
  1585. /*
  1586. * Accept empty data frames, we use those for
  1587. * polling activity.
  1588. */
  1589. BPF_STMT(BPF_LD | BPF_W | BPF_LEN, 0),
  1590. BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_X, 0, PASS, 0),
  1591. /*
  1592. * Accept EAPOL frames
  1593. */
  1594. BPF_STMT(BPF_LD | BPF_W | BPF_IND, 0),
  1595. BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xAAAA0300, 0, FAIL),
  1596. BPF_STMT(BPF_LD | BPF_W | BPF_IND, 4),
  1597. BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0000888E, PASS, FAIL),
  1598. /* keep these last two statements or change the code below */
  1599. /* return 0 == "DROP" */
  1600. BPF_STMT(BPF_RET | BPF_K, 0),
  1601. /* return ~0 == "keep all" */
  1602. BPF_STMT(BPF_RET | BPF_K, ~0),
  1603. };
  1604. static struct sock_fprog msock_filter = {
  1605. .len = sizeof(msock_filter_insns)/sizeof(msock_filter_insns[0]),
  1606. .filter = msock_filter_insns,
  1607. };
  1608. static int add_monitor_filter(int s)
  1609. {
  1610. int idx;
  1611. /* rewrite all PASS/FAIL jump offsets */
  1612. for (idx = 0; idx < msock_filter.len; idx++) {
  1613. struct sock_filter *insn = &msock_filter_insns[idx];
  1614. if (BPF_CLASS(insn->code) == BPF_JMP) {
  1615. if (insn->code == (BPF_JMP|BPF_JA)) {
  1616. if (insn->k == PASS)
  1617. insn->k = msock_filter.len - idx - 2;
  1618. else if (insn->k == FAIL)
  1619. insn->k = msock_filter.len - idx - 3;
  1620. }
  1621. if (insn->jt == PASS)
  1622. insn->jt = msock_filter.len - idx - 2;
  1623. else if (insn->jt == FAIL)
  1624. insn->jt = msock_filter.len - idx - 3;
  1625. if (insn->jf == PASS)
  1626. insn->jf = msock_filter.len - idx - 2;
  1627. else if (insn->jf == FAIL)
  1628. insn->jf = msock_filter.len - idx - 3;
  1629. }
  1630. }
  1631. if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER,
  1632. &msock_filter, sizeof(msock_filter))) {
  1633. perror("SO_ATTACH_FILTER");
  1634. return -1;
  1635. }
  1636. return 0;
  1637. }
  1638. static int nl80211_create_monitor_interface(struct i802_driver_data *drv)
  1639. {
  1640. char buf[IFNAMSIZ];
  1641. struct sockaddr_ll ll;
  1642. int optval;
  1643. socklen_t optlen;
  1644. snprintf(buf, IFNAMSIZ, "mon.%s", drv->iface);
  1645. buf[IFNAMSIZ - 1] = '\0';
  1646. drv->monitor_ifidx =
  1647. nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL);
  1648. if (drv->monitor_ifidx < 0)
  1649. return -1;
  1650. if (hostapd_set_iface_flags(drv, buf, 1))
  1651. goto error;
  1652. memset(&ll, 0, sizeof(ll));
  1653. ll.sll_family = AF_PACKET;
  1654. ll.sll_ifindex = drv->monitor_ifidx;
  1655. drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
  1656. if (drv->monitor_sock < 0) {
  1657. perror("socket[PF_PACKET,SOCK_RAW]");
  1658. goto error;
  1659. }
  1660. if (add_monitor_filter(drv->monitor_sock)) {
  1661. wpa_printf(MSG_INFO, "Failed to set socket filter for monitor "
  1662. "interface; do filtering in user space");
  1663. /* This works, but will cost in performance. */
  1664. }
  1665. if (bind(drv->monitor_sock, (struct sockaddr *) &ll,
  1666. sizeof(ll)) < 0) {
  1667. perror("monitor socket bind");
  1668. goto error;
  1669. }
  1670. optlen = sizeof(optval);
  1671. optval = 20;
  1672. if (setsockopt
  1673. (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
  1674. perror("Failed to set socket priority");
  1675. goto error;
  1676. }
  1677. if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
  1678. drv, NULL)) {
  1679. printf("Could not register monitor read socket\n");
  1680. goto error;
  1681. }
  1682. return 0;
  1683. error:
  1684. nl80211_remove_iface(drv, drv->monitor_ifidx);
  1685. return -1;
  1686. }
  1687. static int nl80211_set_mode(struct i802_driver_data *drv, const char *ifname,
  1688. int mode)
  1689. {
  1690. struct nl_msg *msg;
  1691. int ret = -ENOBUFS;
  1692. msg = nlmsg_alloc();
  1693. if (!msg)
  1694. return -ENOMEM;
  1695. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  1696. 0, NL80211_CMD_SET_INTERFACE, 0);
  1697. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
  1698. if_nametoindex(ifname));
  1699. NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, mode);
  1700. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  1701. if (!ret)
  1702. return 0;
  1703. nla_put_failure:
  1704. wpa_printf(MSG_ERROR, "Failed to set interface %s to master "
  1705. "mode.", ifname);
  1706. return ret;
  1707. }
  1708. #ifdef CONFIG_IEEE80211N
  1709. static void i802_add_neighbor(struct i802_driver_data *drv, u8 *bssid,
  1710. int freq, u8 *ie, size_t ie_len)
  1711. {
  1712. struct ieee802_11_elems elems;
  1713. int ht, pri_chan = 0, sec_chan = 0;
  1714. struct ieee80211_ht_operation *oper;
  1715. struct hostapd_neighbor_bss *nnei;
  1716. ieee802_11_parse_elems(ie, ie_len, &elems, 0);
  1717. ht = elems.ht_capabilities || elems.ht_operation;
  1718. if (elems.ht_operation && elems.ht_operation_len >= sizeof(*oper)) {
  1719. oper = (struct ieee80211_ht_operation *) elems.ht_operation;
  1720. pri_chan = oper->control_chan;
  1721. if (oper->ht_param & HT_INFO_HT_PARAM_REC_TRANS_CHNL_WIDTH) {
  1722. if (oper->ht_param &
  1723. HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE)
  1724. sec_chan = pri_chan + 4;
  1725. else if (oper->ht_param &
  1726. HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW)
  1727. sec_chan = pri_chan - 4;
  1728. }
  1729. }
  1730. wpa_printf(MSG_DEBUG, "nl80211: Neighboring BSS - bssid=" MACSTR
  1731. " freq=%d MHz HT=%d pri_chan=%d sec_chan=%d",
  1732. MAC2STR(bssid), freq, ht, pri_chan, sec_chan);
  1733. nnei = os_realloc(drv->neighbors, (drv->num_neighbors + 1) *
  1734. sizeof(struct hostapd_neighbor_bss));
  1735. if (nnei == NULL)
  1736. return;
  1737. drv->neighbors = nnei;
  1738. nnei = &nnei[drv->num_neighbors];
  1739. os_memcpy(nnei->bssid, bssid, ETH_ALEN);
  1740. nnei->freq = freq;
  1741. nnei->ht = !!ht;
  1742. nnei->pri_chan = pri_chan;
  1743. nnei->sec_chan = sec_chan;
  1744. drv->num_neighbors++;
  1745. }
  1746. static int i802_get_scan_freq(struct iw_event *iwe, int *freq)
  1747. {
  1748. int divi = 1000000, i;
  1749. if (iwe->u.freq.e == 0) {
  1750. /*
  1751. * Some drivers do not report frequency, but a channel.
  1752. * Try to map this to frequency by assuming they are using
  1753. * IEEE 802.11b/g. But don't overwrite a previously parsed
  1754. * frequency if the driver sends both frequency and channel,
  1755. * since the driver may be sending an A-band channel that we
  1756. * don't handle here.
  1757. */
  1758. if (*freq)
  1759. return 0;
  1760. if (iwe->u.freq.m >= 1 && iwe->u.freq.m <= 13) {
  1761. *freq = 2407 + 5 * iwe->u.freq.m;
  1762. return 0;
  1763. } else if (iwe->u.freq.m == 14) {
  1764. *freq = 2484;
  1765. return 0;
  1766. }
  1767. }
  1768. if (iwe->u.freq.e > 6) {
  1769. wpa_printf(MSG_DEBUG, "Invalid freq in scan results: "
  1770. "m=%d e=%d", iwe->u.freq.m, iwe->u.freq.e);
  1771. return -1;
  1772. }
  1773. for (i = 0; i < iwe->u.freq.e; i++)
  1774. divi /= 10;
  1775. *freq = iwe->u.freq.m / divi;
  1776. return 0;
  1777. }
  1778. static int i802_parse_scan(struct i802_driver_data *drv, u8 *res_buf,
  1779. size_t len)
  1780. {
  1781. size_t ap_num = 0;
  1782. int first;
  1783. struct iw_event iwe_buf, *iwe = &iwe_buf;
  1784. char *pos, *end, *custom;
  1785. u8 bssid[ETH_ALEN];
  1786. int freq = 0;
  1787. u8 *ie = NULL;
  1788. size_t ie_len = 0;
  1789. ap_num = 0;
  1790. first = 1;
  1791. pos = (char *) res_buf;
  1792. end = (char *) res_buf + len;
  1793. while (pos + IW_EV_LCP_LEN <= end) {
  1794. /* Event data may be unaligned, so make a local, aligned copy
  1795. * before processing. */
  1796. os_memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
  1797. if (iwe->len <= IW_EV_LCP_LEN)
  1798. break;
  1799. custom = pos + IW_EV_POINT_LEN;
  1800. if (iwe->cmd == IWEVGENIE) {
  1801. /* WE-19 removed the pointer from struct iw_point */
  1802. char *dpos = (char *) &iwe_buf.u.data.length;
  1803. int dlen = dpos - (char *) &iwe_buf;
  1804. os_memcpy(dpos, pos + IW_EV_LCP_LEN,
  1805. sizeof(struct iw_event) - dlen);
  1806. } else {
  1807. os_memcpy(&iwe_buf, pos, sizeof(struct iw_event));
  1808. custom += IW_EV_POINT_OFF;
  1809. }
  1810. switch (iwe->cmd) {
  1811. case SIOCGIWAP:
  1812. if (!first)
  1813. i802_add_neighbor(drv, bssid, freq, ie,
  1814. ie_len);
  1815. first = 0;
  1816. os_memcpy(bssid, iwe->u.ap_addr.sa_data, ETH_ALEN);
  1817. freq = 0;
  1818. ie = NULL;
  1819. ie_len = 0;
  1820. break;
  1821. case SIOCGIWFREQ:
  1822. i802_get_scan_freq(iwe, &freq);
  1823. break;
  1824. case IWEVGENIE:
  1825. if (custom + iwe->u.data.length > end) {
  1826. wpa_printf(MSG_ERROR, "IWEVGENIE overflow");
  1827. return -1;
  1828. }
  1829. ie = (u8 *) custom;
  1830. ie_len = iwe->u.data.length;
  1831. break;
  1832. }
  1833. pos += iwe->len;
  1834. }
  1835. if (!first)
  1836. i802_add_neighbor(drv, bssid, freq, ie, ie_len);
  1837. return 0;
  1838. }
  1839. static int i802_get_ht_scan_res(struct i802_driver_data *drv)
  1840. {
  1841. struct iwreq iwr;
  1842. u8 *res_buf;
  1843. size_t res_buf_len;
  1844. int res;
  1845. res_buf_len = IW_SCAN_MAX_DATA;
  1846. for (;;) {
  1847. res_buf = os_malloc(res_buf_len);
  1848. if (res_buf == NULL)
  1849. return -1;
  1850. os_memset(&iwr, 0, sizeof(iwr));
  1851. os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
  1852. iwr.u.data.pointer = res_buf;
  1853. iwr.u.data.length = res_buf_len;
  1854. if (ioctl(drv->ioctl_sock, SIOCGIWSCAN, &iwr) == 0)
  1855. break;
  1856. if (errno == E2BIG && res_buf_len < 65535) {
  1857. os_free(res_buf);
  1858. res_buf = NULL;
  1859. res_buf_len *= 2;
  1860. if (res_buf_len > 65535)
  1861. res_buf_len = 65535; /* 16-bit length field */
  1862. wpa_printf(MSG_DEBUG, "Scan results did not fit - "
  1863. "trying larger buffer (%lu bytes)",
  1864. (unsigned long) res_buf_len);
  1865. } else {
  1866. perror("ioctl[SIOCGIWSCAN]");
  1867. os_free(res_buf);
  1868. return -1;
  1869. }
  1870. }
  1871. if (iwr.u.data.length > res_buf_len) {
  1872. os_free(res_buf);
  1873. return -1;
  1874. }
  1875. res = i802_parse_scan(drv, res_buf, iwr.u.data.length);
  1876. os_free(res_buf);
  1877. return res;
  1878. }
  1879. static int i802_is_event_wireless_scan_complete(char *data, int len)
  1880. {
  1881. struct iw_event iwe_buf, *iwe = &iwe_buf;
  1882. char *pos, *end;
  1883. pos = data;
  1884. end = data + len;
  1885. while (pos + IW_EV_LCP_LEN <= end) {
  1886. /* Event data may be unaligned, so make a local, aligned copy
  1887. * before processing. */
  1888. os_memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
  1889. if (iwe->cmd == SIOCGIWSCAN)
  1890. return 1;
  1891. pos += iwe->len;
  1892. }
  1893. return 0;
  1894. }
  1895. static int i802_is_rtm_scan_complete(int ifindex, struct nlmsghdr *h, int len)
  1896. {
  1897. struct ifinfomsg *ifi;
  1898. int attrlen, _nlmsg_len, rta_len;
  1899. struct rtattr *attr;
  1900. if (len < (int) sizeof(*ifi))
  1901. return 0;
  1902. ifi = NLMSG_DATA(h);
  1903. if (ifindex != ifi->ifi_index)
  1904. return 0; /* event for foreign ifindex */
  1905. _nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg));
  1906. attrlen = h->nlmsg_len - _nlmsg_len;
  1907. if (attrlen < 0)
  1908. return 0;
  1909. attr = (struct rtattr *) (((char *) ifi) + _nlmsg_len);
  1910. rta_len = RTA_ALIGN(sizeof(struct rtattr));
  1911. while (RTA_OK(attr, attrlen)) {
  1912. if (attr->rta_type == IFLA_WIRELESS &&
  1913. i802_is_event_wireless_scan_complete(
  1914. ((char *) attr) + rta_len,
  1915. attr->rta_len - rta_len))
  1916. return 1;
  1917. attr = RTA_NEXT(attr, attrlen);
  1918. }
  1919. return 0;
  1920. }
  1921. static int i802_is_scan_complete(int s, int ifindex)
  1922. {
  1923. char buf[1024];
  1924. int left;
  1925. struct nlmsghdr *h;
  1926. left = recv(s, buf, sizeof(buf), MSG_DONTWAIT);
  1927. if (left < 0) {
  1928. perror("recv(netlink)");
  1929. return 0;
  1930. }
  1931. h = (struct nlmsghdr *) buf;
  1932. while (left >= (int) sizeof(*h)) {
  1933. int len, plen;
  1934. len = h->nlmsg_len;
  1935. plen = len - sizeof(*h);
  1936. if (len > left || plen < 0) {
  1937. wpa_printf(MSG_DEBUG, "Malformed netlink message: "
  1938. "len=%d left=%d plen=%d",
  1939. len, left, plen);
  1940. break;
  1941. }
  1942. switch (h->nlmsg_type) {
  1943. case RTM_NEWLINK:
  1944. if (i802_is_rtm_scan_complete(ifindex, h, plen))
  1945. return 1;
  1946. break;
  1947. }
  1948. len = NLMSG_ALIGN(len);
  1949. left -= len;
  1950. h = (struct nlmsghdr *) ((char *) h + len);
  1951. }
  1952. return 0;
  1953. }
  1954. static int i802_ht_scan(struct i802_driver_data *drv)
  1955. {
  1956. struct iwreq iwr;
  1957. int s, res, ifindex;
  1958. struct sockaddr_nl local;
  1959. time_t now, end;
  1960. fd_set rfds;
  1961. struct timeval tv;
  1962. wpa_printf(MSG_DEBUG, "nl80211: Scanning overlapping BSSes before "
  1963. "starting HT 20/40 MHz BSS");
  1964. /* Request a new scan */
  1965. /* TODO: would be enough to scan the selected band */
  1966. os_memset(&iwr, 0, sizeof(iwr));
  1967. os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
  1968. if (ioctl(drv->ioctl_sock, SIOCSIWSCAN, &iwr) < 0) {
  1969. perror("ioctl[SIOCSIWSCAN]");
  1970. return -1;
  1971. }
  1972. ifindex = if_nametoindex(drv->iface);
  1973. /* Wait for scan completion event or timeout */
  1974. s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  1975. if (s < 0) {
  1976. perror("socket(PF_NETLINK,SOCK_RAW,NETLINK_ROUTE)");
  1977. return -1;
  1978. }
  1979. os_memset(&local, 0, sizeof(local));
  1980. local.nl_family = AF_NETLINK;
  1981. local.nl_groups = RTMGRP_LINK;
  1982. if (bind(s, (struct sockaddr *) &local, sizeof(local)) < 0) {
  1983. perror("bind(netlink)");
  1984. close(s);
  1985. return -1;
  1986. }
  1987. time(&end);
  1988. end += 30; /* Wait at most 30 seconds for scan results */
  1989. for (;;) {
  1990. time(&now);
  1991. tv.tv_sec = end > now ? end - now : 0;
  1992. tv.tv_usec = 0;
  1993. FD_ZERO(&rfds);
  1994. FD_SET(s, &rfds);
  1995. res = select(s + 1, &rfds, NULL, NULL, &tv);
  1996. if (res < 0) {
  1997. perror("select");
  1998. /* Assume results are ready after 10 seconds wait */
  1999. os_sleep(10, 0);
  2000. break;
  2001. } else if (res) {
  2002. if (i802_is_scan_complete(s, ifindex)) {
  2003. wpa_printf(MSG_DEBUG, "nl80211: Scan "
  2004. "completed");
  2005. break;
  2006. }
  2007. } else {
  2008. wpa_printf(MSG_DEBUG, "nl80211: Scan timeout");
  2009. /* Assume results are ready to be read now */
  2010. break;
  2011. }
  2012. }
  2013. close(s);
  2014. return i802_get_ht_scan_res(drv);
  2015. }
  2016. #endif /* CONFIG_IEEE80211N */
  2017. static int i802_init_sockets(struct i802_driver_data *drv, const u8 *bssid)
  2018. {
  2019. struct ifreq ifr;
  2020. struct sockaddr_ll addr;
  2021. drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
  2022. if (drv->ioctl_sock < 0) {
  2023. perror("socket[PF_INET,SOCK_DGRAM]");
  2024. return -1;
  2025. }
  2026. /* start listening for EAPOL on the default AP interface */
  2027. add_ifidx(drv, if_nametoindex(drv->iface));
  2028. if (hostapd_set_iface_flags(drv, drv->iface, 0))
  2029. return -1;
  2030. if (bssid) {
  2031. os_strlcpy(ifr.ifr_name, drv->iface, IFNAMSIZ);
  2032. memcpy(ifr.ifr_hwaddr.sa_data, bssid, ETH_ALEN);
  2033. ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
  2034. if (ioctl(drv->ioctl_sock, SIOCSIFHWADDR, &ifr)) {
  2035. perror("ioctl(SIOCSIFHWADDR)");
  2036. return -1;
  2037. }
  2038. }
  2039. /*
  2040. * initialise generic netlink and nl80211
  2041. */
  2042. drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
  2043. if (!drv->nl_cb) {
  2044. printf("Failed to allocate netlink callbacks.\n");
  2045. return -1;
  2046. }
  2047. drv->nl_handle = nl_handle_alloc_cb(drv->nl_cb);
  2048. if (!drv->nl_handle) {
  2049. printf("Failed to allocate netlink handle.\n");
  2050. return -1;
  2051. }
  2052. if (genl_connect(drv->nl_handle)) {
  2053. printf("Failed to connect to generic netlink.\n");
  2054. return -1;
  2055. }
  2056. #ifdef CONFIG_LIBNL20
  2057. if (genl_ctrl_alloc_cache(drv->nl_handle, &drv->nl_cache) < 0) {
  2058. printf("Failed to allocate generic netlink cache.\n");
  2059. return -1;
  2060. }
  2061. #else /* CONFIG_LIBNL20 */
  2062. drv->nl_cache = genl_ctrl_alloc_cache(drv->nl_handle);
  2063. if (!drv->nl_cache) {
  2064. printf("Failed to allocate generic netlink cache.\n");
  2065. return -1;
  2066. }
  2067. #endif /* CONFIG_LIBNL20 */
  2068. drv->nl80211 = genl_ctrl_search_by_name(drv->nl_cache, "nl80211");
  2069. if (!drv->nl80211) {
  2070. printf("nl80211 not found.\n");
  2071. return -1;
  2072. }
  2073. #ifdef CONFIG_IEEE80211N
  2074. if (drv->ht_40mhz_scan) {
  2075. if (nl80211_set_mode(drv, drv->iface, NL80211_IFTYPE_STATION)
  2076. || hostapd_set_iface_flags(drv, drv->iface, 1) ||
  2077. i802_ht_scan(drv) ||
  2078. hostapd_set_iface_flags(drv, drv->iface, 0)) {
  2079. wpa_printf(MSG_ERROR, "Failed to scan channels for "
  2080. "HT 40 MHz operations");
  2081. return -1;
  2082. }
  2083. }
  2084. #endif /* CONFIG_IEEE80211N */
  2085. /* Initialise a monitor interface */
  2086. if (nl80211_create_monitor_interface(drv))
  2087. return -1;
  2088. if (nl80211_set_mode(drv, drv->iface, NL80211_IFTYPE_AP))
  2089. goto fail1;
  2090. if (hostapd_set_iface_flags(drv, drv->iface, 1))
  2091. goto fail1;
  2092. memset(&addr, 0, sizeof(addr));
  2093. addr.sll_family = AF_PACKET;
  2094. addr.sll_ifindex = ifr.ifr_ifindex;
  2095. wpa_printf(MSG_DEBUG, "Opening raw packet socket for ifindex %d",
  2096. addr.sll_ifindex);
  2097. drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
  2098. if (drv->eapol_sock < 0) {
  2099. perror("socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE)");
  2100. goto fail1;
  2101. }
  2102. if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
  2103. {
  2104. printf("Could not register read socket for eapol\n");
  2105. return -1;
  2106. }
  2107. memset(&ifr, 0, sizeof(ifr));
  2108. os_strlcpy(ifr.ifr_name, drv->iface, sizeof(ifr.ifr_name));
  2109. if (ioctl(drv->ioctl_sock, SIOCGIFHWADDR, &ifr) != 0) {
  2110. perror("ioctl(SIOCGIFHWADDR)");
  2111. goto fail1;
  2112. }
  2113. if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
  2114. printf("Invalid HW-addr family 0x%04x\n",
  2115. ifr.ifr_hwaddr.sa_family);
  2116. goto fail1;
  2117. }
  2118. memcpy(drv->hapd->own_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
  2119. return 0;
  2120. fail1:
  2121. nl80211_remove_iface(drv, drv->monitor_ifidx);
  2122. return -1;
  2123. }
  2124. static int i802_get_inact_sec(void *priv, const u8 *addr)
  2125. {
  2126. struct hostap_sta_driver_data data;
  2127. int ret;
  2128. data.inactive_msec = (unsigned long) -1;
  2129. ret = i802_read_sta_data(priv, &data, addr);
  2130. if (ret || data.inactive_msec == (unsigned long) -1)
  2131. return -1;
  2132. return data.inactive_msec / 1000;
  2133. }
  2134. static int i802_sta_clear_stats(void *priv, const u8 *addr)
  2135. {
  2136. #if 0
  2137. /* TODO */
  2138. #endif
  2139. return 0;
  2140. }
  2141. static void
  2142. hostapd_wireless_event_wireless_custom(struct i802_driver_data *drv,
  2143. char *custom)
  2144. {
  2145. wpa_printf(MSG_DEBUG, "Custom wireless event: '%s'", custom);
  2146. if (strncmp(custom, "MLME-MICHAELMICFAILURE.indication", 33) == 0) {
  2147. char *pos;
  2148. u8 addr[ETH_ALEN];
  2149. pos = strstr(custom, "addr=");
  2150. if (pos == NULL) {
  2151. wpa_printf(MSG_DEBUG,
  2152. "MLME-MICHAELMICFAILURE.indication "
  2153. "without sender address ignored");
  2154. return;
  2155. }
  2156. pos += 5;
  2157. if (hwaddr_aton(pos, addr) == 0) {
  2158. hostapd_michael_mic_failure(drv->hapd, addr);
  2159. } else {
  2160. wpa_printf(MSG_DEBUG,
  2161. "MLME-MICHAELMICFAILURE.indication "
  2162. "with invalid MAC address");
  2163. }
  2164. }
  2165. }
  2166. static void hostapd_wireless_event_wireless(struct i802_driver_data *drv,
  2167. char *data, int len)
  2168. {
  2169. struct iw_event iwe_buf, *iwe = &iwe_buf;
  2170. char *pos, *end, *custom, *buf;
  2171. pos = data;
  2172. end = data + len;
  2173. while (pos + IW_EV_LCP_LEN <= end) {
  2174. /* Event data may be unaligned, so make a local, aligned copy
  2175. * before processing. */
  2176. memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
  2177. wpa_printf(MSG_DEBUG, "Wireless event: cmd=0x%x len=%d",
  2178. iwe->cmd, iwe->len);
  2179. if (iwe->len <= IW_EV_LCP_LEN)
  2180. return;
  2181. custom = pos + IW_EV_POINT_LEN;
  2182. if (drv->we_version > 18 &&
  2183. (iwe->cmd == IWEVMICHAELMICFAILURE ||
  2184. iwe->cmd == IWEVCUSTOM)) {
  2185. /* WE-19 removed the pointer from struct iw_point */
  2186. char *dpos = (char *) &iwe_buf.u.data.length;
  2187. int dlen = dpos - (char *) &iwe_buf;
  2188. memcpy(dpos, pos + IW_EV_LCP_LEN,
  2189. sizeof(struct iw_event) - dlen);
  2190. } else {
  2191. memcpy(&iwe_buf, pos, sizeof(struct iw_event));
  2192. custom += IW_EV_POINT_OFF;
  2193. }
  2194. switch (iwe->cmd) {
  2195. case IWEVCUSTOM:
  2196. if (custom + iwe->u.data.length > end)
  2197. return;
  2198. buf = malloc(iwe->u.data.length + 1);
  2199. if (buf == NULL)
  2200. return;
  2201. memcpy(buf, custom, iwe->u.data.length);
  2202. buf[iwe->u.data.length] = '\0';
  2203. hostapd_wireless_event_wireless_custom(drv, buf);
  2204. free(buf);
  2205. break;
  2206. }
  2207. pos += iwe->len;
  2208. }
  2209. }
  2210. static void hostapd_wireless_event_rtm_newlink(struct i802_driver_data *drv,
  2211. struct nlmsghdr *h, int len)
  2212. {
  2213. struct ifinfomsg *ifi;
  2214. int attrlen, _nlmsg_len, rta_len;
  2215. struct rtattr *attr;
  2216. if (len < (int) sizeof(*ifi))
  2217. return;
  2218. ifi = NLMSG_DATA(h);
  2219. /* TODO: use ifi->ifi_index to filter out wireless events from other
  2220. * interfaces */
  2221. _nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg));
  2222. attrlen = h->nlmsg_len - _nlmsg_len;
  2223. if (attrlen < 0)
  2224. return;
  2225. attr = (struct rtattr *) (((char *) ifi) + _nlmsg_len);
  2226. rta_len = RTA_ALIGN(sizeof(struct rtattr));
  2227. while (RTA_OK(attr, attrlen)) {
  2228. if (attr->rta_type == IFLA_WIRELESS) {
  2229. hostapd_wireless_event_wireless(
  2230. drv, ((char *) attr) + rta_len,
  2231. attr->rta_len - rta_len);
  2232. }
  2233. attr = RTA_NEXT(attr, attrlen);
  2234. }
  2235. }
  2236. static void hostapd_wireless_event_receive(int sock, void *eloop_ctx,
  2237. void *sock_ctx)
  2238. {
  2239. char buf[256];
  2240. int left;
  2241. struct sockaddr_nl from;
  2242. socklen_t fromlen;
  2243. struct nlmsghdr *h;
  2244. struct i802_driver_data *drv = eloop_ctx;
  2245. fromlen = sizeof(from);
  2246. left = recvfrom(sock, buf, sizeof(buf), MSG_DONTWAIT,
  2247. (struct sockaddr *) &from, &fromlen);
  2248. if (left < 0) {
  2249. if (errno != EINTR && errno != EAGAIN)
  2250. perror("recvfrom(netlink)");
  2251. return;
  2252. }
  2253. h = (struct nlmsghdr *) buf;
  2254. while (left >= (int) sizeof(*h)) {
  2255. int len, plen;
  2256. len = h->nlmsg_len;
  2257. plen = len - sizeof(*h);
  2258. if (len > left || plen < 0) {
  2259. printf("Malformed netlink message: "
  2260. "len=%d left=%d plen=%d\n",
  2261. len, left, plen);
  2262. break;
  2263. }
  2264. switch (h->nlmsg_type) {
  2265. case RTM_NEWLINK:
  2266. hostapd_wireless_event_rtm_newlink(drv, h, plen);
  2267. break;
  2268. }
  2269. len = NLMSG_ALIGN(len);
  2270. left -= len;
  2271. h = (struct nlmsghdr *) ((char *) h + len);
  2272. }
  2273. if (left > 0) {
  2274. printf("%d extra bytes in the end of netlink message\n", left);
  2275. }
  2276. }
  2277. static int hostap_get_we_version(struct i802_driver_data *drv)
  2278. {
  2279. struct iw_range *range;
  2280. struct iwreq iwr;
  2281. int minlen;
  2282. size_t buflen;
  2283. drv->we_version = 0;
  2284. /*
  2285. * Use larger buffer than struct iw_range in order to allow the
  2286. * structure to grow in the future.
  2287. */
  2288. buflen = sizeof(struct iw_range) + 500;
  2289. range = os_zalloc(buflen);
  2290. if (range == NULL)
  2291. return -1;
  2292. memset(&iwr, 0, sizeof(iwr));
  2293. os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
  2294. iwr.u.data.pointer = (caddr_t) range;
  2295. iwr.u.data.length = buflen;
  2296. minlen = ((char *) &range->enc_capa) - (char *) range +
  2297. sizeof(range->enc_capa);
  2298. if (ioctl(drv->ioctl_sock, SIOCGIWRANGE, &iwr) < 0) {
  2299. perror("ioctl[SIOCGIWRANGE]");
  2300. free(range);
  2301. return -1;
  2302. } else if (iwr.u.data.length >= minlen &&
  2303. range->we_version_compiled >= 18) {
  2304. wpa_printf(MSG_DEBUG, "SIOCGIWRANGE: WE(compiled)=%d "
  2305. "WE(source)=%d enc_capa=0x%x",
  2306. range->we_version_compiled,
  2307. range->we_version_source,
  2308. range->enc_capa);
  2309. drv->we_version = range->we_version_compiled;
  2310. }
  2311. free(range);
  2312. return 0;
  2313. }
  2314. static int i802_wireless_event_init(struct i802_driver_data *drv)
  2315. {
  2316. int s;
  2317. struct sockaddr_nl local;
  2318. hostap_get_we_version(drv);
  2319. drv->wext_sock = -1;
  2320. s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  2321. if (s < 0) {
  2322. perror("socket(PF_NETLINK,SOCK_RAW,NETLINK_ROUTE)");
  2323. return -1;
  2324. }
  2325. memset(&local, 0, sizeof(local));
  2326. local.nl_family = AF_NETLINK;
  2327. local.nl_groups = RTMGRP_LINK;
  2328. if (bind(s, (struct sockaddr *) &local, sizeof(local)) < 0) {
  2329. perror("bind(netlink)");
  2330. close(s);
  2331. return -1;
  2332. }
  2333. eloop_register_read_sock(s, hostapd_wireless_event_receive, drv,
  2334. NULL);
  2335. drv->wext_sock = s;
  2336. return 0;
  2337. }
  2338. static void i802_wireless_event_deinit(struct i802_driver_data *drv)
  2339. {
  2340. if (drv->wext_sock < 0)
  2341. return;
  2342. eloop_unregister_read_sock(drv->wext_sock);
  2343. close(drv->wext_sock);
  2344. }
  2345. static int i802_sta_deauth(void *priv, const u8 *addr, int reason)
  2346. {
  2347. struct i802_driver_data *drv = priv;
  2348. struct ieee80211_mgmt mgmt;
  2349. memset(&mgmt, 0, sizeof(mgmt));
  2350. mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  2351. WLAN_FC_STYPE_DEAUTH);
  2352. memcpy(mgmt.da, addr, ETH_ALEN);
  2353. memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
  2354. memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
  2355. mgmt.u.deauth.reason_code = host_to_le16(reason);
  2356. return i802_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
  2357. sizeof(mgmt.u.deauth), 0);
  2358. }
  2359. static int i802_sta_disassoc(void *priv, const u8 *addr, int reason)
  2360. {
  2361. struct i802_driver_data *drv = priv;
  2362. struct ieee80211_mgmt mgmt;
  2363. memset(&mgmt, 0, sizeof(mgmt));
  2364. mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  2365. WLAN_FC_STYPE_DISASSOC);
  2366. memcpy(mgmt.da, addr, ETH_ALEN);
  2367. memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
  2368. memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
  2369. mgmt.u.disassoc.reason_code = host_to_le16(reason);
  2370. return i802_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
  2371. sizeof(mgmt.u.disassoc), 0);
  2372. }
  2373. static const struct hostapd_neighbor_bss *
  2374. i802_get_neighbor_bss(void *priv, size_t *num)
  2375. {
  2376. struct i802_driver_data *drv = priv;
  2377. *num = drv->num_neighbors;
  2378. return drv->neighbors;
  2379. }
  2380. static void *i802_init_bssid(struct hostapd_data *hapd, const u8 *bssid)
  2381. {
  2382. struct i802_driver_data *drv;
  2383. drv = os_zalloc(sizeof(struct i802_driver_data));
  2384. if (drv == NULL) {
  2385. printf("Could not allocate memory for i802 driver data\n");
  2386. return NULL;
  2387. }
  2388. drv->hapd = hapd;
  2389. memcpy(drv->iface, hapd->conf->iface, sizeof(drv->iface));
  2390. memcpy(drv->bss.iface, hapd->conf->iface, sizeof(drv->iface));
  2391. drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
  2392. drv->if_indices = drv->default_if_indices;
  2393. drv->bridge = if_nametoindex(hapd->conf->bridge);
  2394. drv->ht_40mhz_scan = hapd->iconf->secondary_channel != 0;
  2395. if (i802_init_sockets(drv, bssid))
  2396. goto failed;
  2397. if (i802_wireless_event_init(drv))
  2398. goto failed;
  2399. return drv;
  2400. failed:
  2401. free(drv);
  2402. return NULL;
  2403. }
  2404. static void *i802_init(struct hostapd_data *hapd)
  2405. {
  2406. return i802_init_bssid(hapd, NULL);
  2407. }
  2408. static void i802_deinit(void *priv)
  2409. {
  2410. struct i802_driver_data *drv = priv;
  2411. struct i802_bss *bss, *prev;
  2412. i802_wireless_event_deinit(drv);
  2413. if (drv->last_freq_ht) {
  2414. /* Clear HT flags from the driver */
  2415. struct hostapd_freq_params freq;
  2416. os_memset(&freq, 0, sizeof(freq));
  2417. freq.freq = drv->last_freq;
  2418. i802_set_freq(priv, &freq);
  2419. }
  2420. i802_del_beacon(drv);
  2421. /* remove monitor interface */
  2422. nl80211_remove_iface(drv, drv->monitor_ifidx);
  2423. (void) hostapd_set_iface_flags(drv, drv->iface, 0);
  2424. if (drv->monitor_sock >= 0) {
  2425. eloop_unregister_read_sock(drv->monitor_sock);
  2426. close(drv->monitor_sock);
  2427. }
  2428. if (drv->ioctl_sock >= 0)
  2429. close(drv->ioctl_sock);
  2430. if (drv->eapol_sock >= 0) {
  2431. eloop_unregister_read_sock(drv->eapol_sock);
  2432. close(drv->eapol_sock);
  2433. }
  2434. genl_family_put(drv->nl80211);
  2435. nl_cache_free(drv->nl_cache);
  2436. nl_handle_destroy(drv->nl_handle);
  2437. nl_cb_put(drv->nl_cb);
  2438. if (drv->if_indices != drv->default_if_indices)
  2439. free(drv->if_indices);
  2440. os_free(drv->neighbors);
  2441. bss = drv->bss.next;
  2442. while (bss) {
  2443. prev = bss;
  2444. bss = bss->next;
  2445. os_free(bss);
  2446. }
  2447. free(drv);
  2448. }
  2449. const struct hapd_driver_ops wpa_driver_nl80211_ops = {
  2450. .name = "nl80211",
  2451. .init = i802_init,
  2452. .init_bssid = i802_init_bssid,
  2453. .deinit = i802_deinit,
  2454. .set_ieee8021x = i802_set_ieee8021x,
  2455. .set_privacy = i802_set_privacy,
  2456. .set_key = i802_set_key,
  2457. .get_seqnum = i802_get_seqnum,
  2458. .flush = i802_flush,
  2459. .read_sta_data = i802_read_sta_data,
  2460. .send_eapol = i802_send_eapol,
  2461. .sta_set_flags = i802_sta_set_flags,
  2462. .sta_deauth = i802_sta_deauth,
  2463. .sta_disassoc = i802_sta_disassoc,
  2464. .sta_remove = i802_sta_remove,
  2465. .send_mgmt_frame = i802_send_mgmt_frame,
  2466. .sta_add = i802_sta_add,
  2467. .get_inact_sec = i802_get_inact_sec,
  2468. .sta_clear_stats = i802_sta_clear_stats,
  2469. .set_freq = i802_set_freq,
  2470. .set_rts = i802_set_rts,
  2471. .set_frag = i802_set_frag,
  2472. .set_retry = i802_set_retry,
  2473. .set_rate_sets = i802_set_rate_sets,
  2474. .set_beacon = i802_set_beacon,
  2475. .set_internal_bridge = i802_set_internal_bridge,
  2476. .set_beacon_int = i802_set_beacon_int,
  2477. .set_cts_protect = i802_set_cts_protect,
  2478. .set_preamble = i802_set_preamble,
  2479. .set_short_slot_time = i802_set_short_slot_time,
  2480. .set_tx_queue_params = i802_set_tx_queue_params,
  2481. .bss_add = i802_bss_add,
  2482. .bss_remove = i802_bss_remove,
  2483. .if_add = i802_if_add,
  2484. .if_update = i802_if_update,
  2485. .if_remove = i802_if_remove,
  2486. .get_hw_feature_data = i802_get_hw_feature_data,
  2487. .set_sta_vlan = i802_set_sta_vlan,
  2488. .set_country = i802_set_country,
  2489. .get_neighbor_bss = i802_get_neighbor_bss,
  2490. };