driver_nl80211.c 71 KB

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