driver_nl80211.c 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001
  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. drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
  1995. if (drv->ioctl_sock < 0) {
  1996. perror("socket[PF_INET,SOCK_DGRAM]");
  1997. return -1;
  1998. }
  1999. /* start listening for EAPOL on the default AP interface */
  2000. add_ifidx(drv, if_nametoindex(drv->iface));
  2001. if (hostapd_set_iface_flags(drv, drv->iface, 0))
  2002. return -1;
  2003. if (bssid) {
  2004. os_strlcpy(ifr.ifr_name, drv->iface, IFNAMSIZ);
  2005. memcpy(ifr.ifr_hwaddr.sa_data, bssid, ETH_ALEN);
  2006. ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
  2007. if (ioctl(drv->ioctl_sock, SIOCSIFHWADDR, &ifr)) {
  2008. perror("ioctl(SIOCSIFHWADDR)");
  2009. return -1;
  2010. }
  2011. }
  2012. /*
  2013. * initialise generic netlink and nl80211
  2014. */
  2015. drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
  2016. if (!drv->nl_cb) {
  2017. printf("Failed to allocate netlink callbacks.\n");
  2018. return -1;
  2019. }
  2020. drv->nl_handle = nl_handle_alloc_cb(drv->nl_cb);
  2021. if (!drv->nl_handle) {
  2022. printf("Failed to allocate netlink handle.\n");
  2023. return -1;
  2024. }
  2025. if (genl_connect(drv->nl_handle)) {
  2026. printf("Failed to connect to generic netlink.\n");
  2027. return -1;
  2028. }
  2029. #ifdef CONFIG_LIBNL20
  2030. if (genl_ctrl_alloc_cache(drv->nl_handle, &drv->nl_cache) < 0) {
  2031. printf("Failed to allocate generic netlink cache.\n");
  2032. return -1;
  2033. }
  2034. #else /* CONFIG_LIBNL20 */
  2035. drv->nl_cache = genl_ctrl_alloc_cache(drv->nl_handle);
  2036. if (!drv->nl_cache) {
  2037. printf("Failed to allocate generic netlink cache.\n");
  2038. return -1;
  2039. }
  2040. #endif /* CONFIG_LIBNL20 */
  2041. drv->nl80211 = genl_ctrl_search_by_name(drv->nl_cache, "nl80211");
  2042. if (!drv->nl80211) {
  2043. printf("nl80211 not found.\n");
  2044. return -1;
  2045. }
  2046. #ifdef CONFIG_IEEE80211N
  2047. if (drv->ht_40mhz_scan) {
  2048. if (nl80211_set_mode(drv, drv->iface, NL80211_IFTYPE_STATION)
  2049. || hostapd_set_iface_flags(drv, drv->iface, 1) ||
  2050. i802_ht_scan(drv) ||
  2051. hostapd_set_iface_flags(drv, drv->iface, 0)) {
  2052. wpa_printf(MSG_ERROR, "Failed to scan channels for "
  2053. "HT 40 MHz operations");
  2054. return -1;
  2055. }
  2056. }
  2057. #endif /* CONFIG_IEEE80211N */
  2058. /* Initialise a monitor interface */
  2059. if (nl80211_create_monitor_interface(drv))
  2060. return -1;
  2061. if (nl80211_set_mode(drv, drv->iface, NL80211_IFTYPE_AP))
  2062. goto fail1;
  2063. if (hostapd_set_iface_flags(drv, drv->iface, 1))
  2064. goto fail1;
  2065. drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
  2066. if (drv->eapol_sock < 0) {
  2067. perror("socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE)");
  2068. goto fail1;
  2069. }
  2070. if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
  2071. {
  2072. printf("Could not register read socket for eapol\n");
  2073. return -1;
  2074. }
  2075. memset(&ifr, 0, sizeof(ifr));
  2076. os_strlcpy(ifr.ifr_name, drv->iface, sizeof(ifr.ifr_name));
  2077. if (ioctl(drv->ioctl_sock, SIOCGIFHWADDR, &ifr) != 0) {
  2078. perror("ioctl(SIOCGIFHWADDR)");
  2079. goto fail1;
  2080. }
  2081. if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
  2082. printf("Invalid HW-addr family 0x%04x\n",
  2083. ifr.ifr_hwaddr.sa_family);
  2084. goto fail1;
  2085. }
  2086. memcpy(drv->hapd->own_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
  2087. return 0;
  2088. fail1:
  2089. nl80211_remove_iface(drv, drv->monitor_ifidx);
  2090. return -1;
  2091. }
  2092. static int i802_get_inact_sec(void *priv, const u8 *addr)
  2093. {
  2094. struct hostap_sta_driver_data data;
  2095. int ret;
  2096. data.inactive_msec = (unsigned long) -1;
  2097. ret = i802_read_sta_data(priv, &data, addr);
  2098. if (ret || data.inactive_msec == (unsigned long) -1)
  2099. return -1;
  2100. return data.inactive_msec / 1000;
  2101. }
  2102. static int i802_sta_clear_stats(void *priv, const u8 *addr)
  2103. {
  2104. #if 0
  2105. /* TODO */
  2106. #endif
  2107. return 0;
  2108. }
  2109. static void
  2110. hostapd_wireless_event_wireless_custom(struct i802_driver_data *drv,
  2111. char *custom)
  2112. {
  2113. wpa_printf(MSG_DEBUG, "Custom wireless event: '%s'", custom);
  2114. if (strncmp(custom, "MLME-MICHAELMICFAILURE.indication", 33) == 0) {
  2115. char *pos;
  2116. u8 addr[ETH_ALEN];
  2117. pos = strstr(custom, "addr=");
  2118. if (pos == NULL) {
  2119. wpa_printf(MSG_DEBUG,
  2120. "MLME-MICHAELMICFAILURE.indication "
  2121. "without sender address ignored");
  2122. return;
  2123. }
  2124. pos += 5;
  2125. if (hwaddr_aton(pos, addr) == 0) {
  2126. hostapd_michael_mic_failure(drv->hapd, addr);
  2127. } else {
  2128. wpa_printf(MSG_DEBUG,
  2129. "MLME-MICHAELMICFAILURE.indication "
  2130. "with invalid MAC address");
  2131. }
  2132. }
  2133. }
  2134. static void hostapd_wireless_event_wireless(struct i802_driver_data *drv,
  2135. char *data, int len)
  2136. {
  2137. struct iw_event iwe_buf, *iwe = &iwe_buf;
  2138. char *pos, *end, *custom, *buf;
  2139. pos = data;
  2140. end = data + len;
  2141. while (pos + IW_EV_LCP_LEN <= end) {
  2142. /* Event data may be unaligned, so make a local, aligned copy
  2143. * before processing. */
  2144. memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
  2145. wpa_printf(MSG_DEBUG, "Wireless event: cmd=0x%x len=%d",
  2146. iwe->cmd, iwe->len);
  2147. if (iwe->len <= IW_EV_LCP_LEN)
  2148. return;
  2149. custom = pos + IW_EV_POINT_LEN;
  2150. if (drv->we_version > 18 &&
  2151. (iwe->cmd == IWEVMICHAELMICFAILURE ||
  2152. iwe->cmd == IWEVCUSTOM)) {
  2153. /* WE-19 removed the pointer from struct iw_point */
  2154. char *dpos = (char *) &iwe_buf.u.data.length;
  2155. int dlen = dpos - (char *) &iwe_buf;
  2156. memcpy(dpos, pos + IW_EV_LCP_LEN,
  2157. sizeof(struct iw_event) - dlen);
  2158. } else {
  2159. memcpy(&iwe_buf, pos, sizeof(struct iw_event));
  2160. custom += IW_EV_POINT_OFF;
  2161. }
  2162. switch (iwe->cmd) {
  2163. case IWEVCUSTOM:
  2164. if (custom + iwe->u.data.length > end)
  2165. return;
  2166. buf = malloc(iwe->u.data.length + 1);
  2167. if (buf == NULL)
  2168. return;
  2169. memcpy(buf, custom, iwe->u.data.length);
  2170. buf[iwe->u.data.length] = '\0';
  2171. hostapd_wireless_event_wireless_custom(drv, buf);
  2172. free(buf);
  2173. break;
  2174. }
  2175. pos += iwe->len;
  2176. }
  2177. }
  2178. static void hostapd_wireless_event_rtm_newlink(struct i802_driver_data *drv,
  2179. struct nlmsghdr *h, int len)
  2180. {
  2181. struct ifinfomsg *ifi;
  2182. int attrlen, _nlmsg_len, rta_len;
  2183. struct rtattr *attr;
  2184. if (len < (int) sizeof(*ifi))
  2185. return;
  2186. ifi = NLMSG_DATA(h);
  2187. /* TODO: use ifi->ifi_index to filter out wireless events from other
  2188. * interfaces */
  2189. _nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg));
  2190. attrlen = h->nlmsg_len - _nlmsg_len;
  2191. if (attrlen < 0)
  2192. return;
  2193. attr = (struct rtattr *) (((char *) ifi) + _nlmsg_len);
  2194. rta_len = RTA_ALIGN(sizeof(struct rtattr));
  2195. while (RTA_OK(attr, attrlen)) {
  2196. if (attr->rta_type == IFLA_WIRELESS) {
  2197. hostapd_wireless_event_wireless(
  2198. drv, ((char *) attr) + rta_len,
  2199. attr->rta_len - rta_len);
  2200. }
  2201. attr = RTA_NEXT(attr, attrlen);
  2202. }
  2203. }
  2204. static void hostapd_wireless_event_receive(int sock, void *eloop_ctx,
  2205. void *sock_ctx)
  2206. {
  2207. char buf[256];
  2208. int left;
  2209. struct sockaddr_nl from;
  2210. socklen_t fromlen;
  2211. struct nlmsghdr *h;
  2212. struct i802_driver_data *drv = eloop_ctx;
  2213. fromlen = sizeof(from);
  2214. left = recvfrom(sock, buf, sizeof(buf), MSG_DONTWAIT,
  2215. (struct sockaddr *) &from, &fromlen);
  2216. if (left < 0) {
  2217. if (errno != EINTR && errno != EAGAIN)
  2218. perror("recvfrom(netlink)");
  2219. return;
  2220. }
  2221. h = (struct nlmsghdr *) buf;
  2222. while (left >= (int) sizeof(*h)) {
  2223. int len, plen;
  2224. len = h->nlmsg_len;
  2225. plen = len - sizeof(*h);
  2226. if (len > left || plen < 0) {
  2227. printf("Malformed netlink message: "
  2228. "len=%d left=%d plen=%d\n",
  2229. len, left, plen);
  2230. break;
  2231. }
  2232. switch (h->nlmsg_type) {
  2233. case RTM_NEWLINK:
  2234. hostapd_wireless_event_rtm_newlink(drv, h, plen);
  2235. break;
  2236. }
  2237. len = NLMSG_ALIGN(len);
  2238. left -= len;
  2239. h = (struct nlmsghdr *) ((char *) h + len);
  2240. }
  2241. if (left > 0) {
  2242. printf("%d extra bytes in the end of netlink message\n", left);
  2243. }
  2244. }
  2245. static int hostap_get_we_version(struct i802_driver_data *drv)
  2246. {
  2247. struct iw_range *range;
  2248. struct iwreq iwr;
  2249. int minlen;
  2250. size_t buflen;
  2251. drv->we_version = 0;
  2252. /*
  2253. * Use larger buffer than struct iw_range in order to allow the
  2254. * structure to grow in the future.
  2255. */
  2256. buflen = sizeof(struct iw_range) + 500;
  2257. range = os_zalloc(buflen);
  2258. if (range == NULL)
  2259. return -1;
  2260. memset(&iwr, 0, sizeof(iwr));
  2261. os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
  2262. iwr.u.data.pointer = (caddr_t) range;
  2263. iwr.u.data.length = buflen;
  2264. minlen = ((char *) &range->enc_capa) - (char *) range +
  2265. sizeof(range->enc_capa);
  2266. if (ioctl(drv->ioctl_sock, SIOCGIWRANGE, &iwr) < 0) {
  2267. perror("ioctl[SIOCGIWRANGE]");
  2268. free(range);
  2269. return -1;
  2270. } else if (iwr.u.data.length >= minlen &&
  2271. range->we_version_compiled >= 18) {
  2272. wpa_printf(MSG_DEBUG, "SIOCGIWRANGE: WE(compiled)=%d "
  2273. "WE(source)=%d enc_capa=0x%x",
  2274. range->we_version_compiled,
  2275. range->we_version_source,
  2276. range->enc_capa);
  2277. drv->we_version = range->we_version_compiled;
  2278. }
  2279. free(range);
  2280. return 0;
  2281. }
  2282. static int i802_wireless_event_init(struct i802_driver_data *drv)
  2283. {
  2284. int s;
  2285. struct sockaddr_nl local;
  2286. hostap_get_we_version(drv);
  2287. drv->wext_sock = -1;
  2288. s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  2289. if (s < 0) {
  2290. perror("socket(PF_NETLINK,SOCK_RAW,NETLINK_ROUTE)");
  2291. return -1;
  2292. }
  2293. memset(&local, 0, sizeof(local));
  2294. local.nl_family = AF_NETLINK;
  2295. local.nl_groups = RTMGRP_LINK;
  2296. if (bind(s, (struct sockaddr *) &local, sizeof(local)) < 0) {
  2297. perror("bind(netlink)");
  2298. close(s);
  2299. return -1;
  2300. }
  2301. eloop_register_read_sock(s, hostapd_wireless_event_receive, drv,
  2302. NULL);
  2303. drv->wext_sock = s;
  2304. return 0;
  2305. }
  2306. static void i802_wireless_event_deinit(struct i802_driver_data *drv)
  2307. {
  2308. if (drv->wext_sock < 0)
  2309. return;
  2310. eloop_unregister_read_sock(drv->wext_sock);
  2311. close(drv->wext_sock);
  2312. }
  2313. static int i802_sta_deauth(void *priv, const u8 *addr, int reason)
  2314. {
  2315. struct i802_driver_data *drv = priv;
  2316. struct ieee80211_mgmt mgmt;
  2317. memset(&mgmt, 0, sizeof(mgmt));
  2318. mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  2319. WLAN_FC_STYPE_DEAUTH);
  2320. memcpy(mgmt.da, addr, ETH_ALEN);
  2321. memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
  2322. memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
  2323. mgmt.u.deauth.reason_code = host_to_le16(reason);
  2324. return i802_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
  2325. sizeof(mgmt.u.deauth), 0);
  2326. }
  2327. static int i802_sta_disassoc(void *priv, const u8 *addr, int reason)
  2328. {
  2329. struct i802_driver_data *drv = priv;
  2330. struct ieee80211_mgmt mgmt;
  2331. memset(&mgmt, 0, sizeof(mgmt));
  2332. mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  2333. WLAN_FC_STYPE_DISASSOC);
  2334. memcpy(mgmt.da, addr, ETH_ALEN);
  2335. memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
  2336. memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
  2337. mgmt.u.disassoc.reason_code = host_to_le16(reason);
  2338. return i802_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
  2339. sizeof(mgmt.u.disassoc), 0);
  2340. }
  2341. static const struct hostapd_neighbor_bss *
  2342. i802_get_neighbor_bss(void *priv, size_t *num)
  2343. {
  2344. struct i802_driver_data *drv = priv;
  2345. *num = drv->num_neighbors;
  2346. return drv->neighbors;
  2347. }
  2348. static void *i802_init_bssid(struct hostapd_data *hapd, const u8 *bssid)
  2349. {
  2350. struct i802_driver_data *drv;
  2351. drv = os_zalloc(sizeof(struct i802_driver_data));
  2352. if (drv == NULL) {
  2353. printf("Could not allocate memory for i802 driver data\n");
  2354. return NULL;
  2355. }
  2356. drv->hapd = hapd;
  2357. memcpy(drv->iface, hapd->conf->iface, sizeof(drv->iface));
  2358. memcpy(drv->bss.iface, hapd->conf->iface, sizeof(drv->iface));
  2359. drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
  2360. drv->if_indices = drv->default_if_indices;
  2361. drv->bridge = if_nametoindex(hapd->conf->bridge);
  2362. drv->ht_40mhz_scan = hapd->iconf->secondary_channel != 0;
  2363. if (i802_init_sockets(drv, bssid))
  2364. goto failed;
  2365. if (i802_wireless_event_init(drv))
  2366. goto failed;
  2367. return drv;
  2368. failed:
  2369. free(drv);
  2370. return NULL;
  2371. }
  2372. static void *i802_init(struct hostapd_data *hapd)
  2373. {
  2374. return i802_init_bssid(hapd, NULL);
  2375. }
  2376. static void i802_deinit(void *priv)
  2377. {
  2378. struct i802_driver_data *drv = priv;
  2379. struct i802_bss *bss, *prev;
  2380. i802_wireless_event_deinit(drv);
  2381. if (drv->last_freq_ht) {
  2382. /* Clear HT flags from the driver */
  2383. struct hostapd_freq_params freq;
  2384. os_memset(&freq, 0, sizeof(freq));
  2385. freq.freq = drv->last_freq;
  2386. i802_set_freq(priv, &freq);
  2387. }
  2388. i802_del_beacon(drv);
  2389. /* remove monitor interface */
  2390. nl80211_remove_iface(drv, drv->monitor_ifidx);
  2391. (void) hostapd_set_iface_flags(drv, drv->iface, 0);
  2392. if (drv->monitor_sock >= 0) {
  2393. eloop_unregister_read_sock(drv->monitor_sock);
  2394. close(drv->monitor_sock);
  2395. }
  2396. if (drv->ioctl_sock >= 0)
  2397. close(drv->ioctl_sock);
  2398. if (drv->eapol_sock >= 0) {
  2399. eloop_unregister_read_sock(drv->eapol_sock);
  2400. close(drv->eapol_sock);
  2401. }
  2402. genl_family_put(drv->nl80211);
  2403. nl_cache_free(drv->nl_cache);
  2404. nl_handle_destroy(drv->nl_handle);
  2405. nl_cb_put(drv->nl_cb);
  2406. if (drv->if_indices != drv->default_if_indices)
  2407. free(drv->if_indices);
  2408. os_free(drv->neighbors);
  2409. bss = drv->bss.next;
  2410. while (bss) {
  2411. prev = bss;
  2412. bss = bss->next;
  2413. os_free(bss);
  2414. }
  2415. free(drv);
  2416. }
  2417. const struct hapd_driver_ops wpa_driver_nl80211_ops = {
  2418. .name = "nl80211",
  2419. .init = i802_init,
  2420. .init_bssid = i802_init_bssid,
  2421. .deinit = i802_deinit,
  2422. .set_key = i802_set_key,
  2423. .get_seqnum = i802_get_seqnum,
  2424. .flush = i802_flush,
  2425. .read_sta_data = i802_read_sta_data,
  2426. .send_eapol = i802_send_eapol,
  2427. .sta_set_flags = i802_sta_set_flags,
  2428. .sta_deauth = i802_sta_deauth,
  2429. .sta_disassoc = i802_sta_disassoc,
  2430. .sta_remove = i802_sta_remove,
  2431. .send_mgmt_frame = i802_send_mgmt_frame,
  2432. .sta_add = i802_sta_add,
  2433. .get_inact_sec = i802_get_inact_sec,
  2434. .sta_clear_stats = i802_sta_clear_stats,
  2435. .set_freq = i802_set_freq,
  2436. .set_rts = i802_set_rts,
  2437. .set_frag = i802_set_frag,
  2438. .set_retry = i802_set_retry,
  2439. .set_rate_sets = i802_set_rate_sets,
  2440. .set_beacon = i802_set_beacon,
  2441. .set_beacon_int = i802_set_beacon_int,
  2442. .set_cts_protect = i802_set_cts_protect,
  2443. .set_preamble = i802_set_preamble,
  2444. .set_short_slot_time = i802_set_short_slot_time,
  2445. .set_tx_queue_params = i802_set_tx_queue_params,
  2446. .bss_add = i802_bss_add,
  2447. .bss_remove = i802_bss_remove,
  2448. .if_add = i802_if_add,
  2449. .if_update = i802_if_update,
  2450. .if_remove = i802_if_remove,
  2451. .get_hw_feature_data = i802_get_hw_feature_data,
  2452. .set_sta_vlan = i802_set_sta_vlan,
  2453. .set_country = i802_set_country,
  2454. .get_neighbor_bss = i802_get_neighbor_bss,
  2455. };