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