driver_nl80211.c 110 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367
  1. /*
  2. * Driver interaction with Linux nl80211/cfg80211
  3. * Copyright (c) 2002-2008, 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. * Copyright (c) 2009, Atheros Communications
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * Alternatively, this software may be distributed under the terms of BSD
  14. * license.
  15. *
  16. * See README and COPYING for more details.
  17. */
  18. #include "includes.h"
  19. #include <sys/ioctl.h>
  20. #include <net/if_arp.h>
  21. #include <net/if.h>
  22. #include <netlink/genl/genl.h>
  23. #include <netlink/genl/family.h>
  24. #include <netlink/genl/ctrl.h>
  25. #include <netpacket/packet.h>
  26. #include <linux/filter.h>
  27. #include "nl80211_copy.h"
  28. #include "common.h"
  29. #include "eloop.h"
  30. #include "common/ieee802_11_defs.h"
  31. #include "netlink.h"
  32. #include "radiotap.h"
  33. #include "radiotap_iter.h"
  34. #include "driver.h"
  35. #ifdef CONFIG_LIBNL20
  36. /* libnl 2.0 compatibility code */
  37. #define nl_handle nl_sock
  38. #define nl_handle_alloc_cb nl_socket_alloc_cb
  39. #define nl_handle_destroy nl_socket_free
  40. #endif /* CONFIG_LIBNL20 */
  41. #ifndef IFF_LOWER_UP
  42. #define IFF_LOWER_UP 0x10000 /* driver signals L1 up */
  43. #endif
  44. #ifndef IFF_DORMANT
  45. #define IFF_DORMANT 0x20000 /* driver signals dormant */
  46. #endif
  47. #ifndef IF_OPER_DORMANT
  48. #define IF_OPER_DORMANT 5
  49. #endif
  50. #ifndef IF_OPER_UP
  51. #define IF_OPER_UP 6
  52. #endif
  53. struct i802_bss {
  54. struct i802_bss *next;
  55. int ifindex;
  56. unsigned int beacon_set:1;
  57. };
  58. struct wpa_driver_nl80211_data {
  59. void *ctx;
  60. struct netlink_data *netlink;
  61. int ioctl_sock; /* socket for ioctl() use */
  62. char ifname[IFNAMSIZ + 1];
  63. int ifindex;
  64. int if_removed;
  65. struct wpa_driver_capa capa;
  66. int has_capability;
  67. int operstate;
  68. int scan_complete_events;
  69. struct nl_handle *nl_handle;
  70. struct nl_handle *nl_handle_event;
  71. struct nl_cache *nl_cache;
  72. struct nl_cache *nl_cache_event;
  73. struct nl_cb *nl_cb;
  74. struct genl_family *nl80211;
  75. u8 auth_bssid[ETH_ALEN];
  76. u8 bssid[ETH_ALEN];
  77. int associated;
  78. u8 ssid[32];
  79. size_t ssid_len;
  80. int nlmode;
  81. int ap_scan_as_station;
  82. int monitor_sock;
  83. int monitor_ifidx;
  84. unsigned int beacon_set:1;
  85. #ifdef HOSTAPD
  86. int eapol_sock; /* socket for EAPOL frames */
  87. int default_if_indices[16];
  88. int *if_indices;
  89. int num_if_indices;
  90. struct i802_bss bss;
  91. int last_freq;
  92. int last_freq_ht;
  93. #endif /* HOSTAPD */
  94. };
  95. static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx,
  96. void *timeout_ctx);
  97. static int wpa_driver_nl80211_set_mode(void *priv, int mode);
  98. static int
  99. wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv);
  100. static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
  101. const u8 *addr, int cmd, u16 reason_code);
  102. static void nl80211_remove_monitor_interface(
  103. struct wpa_driver_nl80211_data *drv);
  104. #ifdef HOSTAPD
  105. static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
  106. static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
  107. static struct i802_bss * get_bss(struct wpa_driver_nl80211_data *drv,
  108. int ifindex);
  109. static int i802_set_freq(void *priv, struct hostapd_freq_params *freq);
  110. #endif /* HOSTAPD */
  111. /* nl80211 code */
  112. static int ack_handler(struct nl_msg *msg, void *arg)
  113. {
  114. int *err = arg;
  115. *err = 0;
  116. return NL_STOP;
  117. }
  118. static int finish_handler(struct nl_msg *msg, void *arg)
  119. {
  120. int *ret = arg;
  121. *ret = 0;
  122. return NL_SKIP;
  123. }
  124. static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
  125. void *arg)
  126. {
  127. int *ret = arg;
  128. *ret = err->error;
  129. return NL_SKIP;
  130. }
  131. static int no_seq_check(struct nl_msg *msg, void *arg)
  132. {
  133. return NL_OK;
  134. }
  135. static int send_and_recv_msgs(struct wpa_driver_nl80211_data *drv,
  136. struct nl_msg *msg,
  137. int (*valid_handler)(struct nl_msg *, void *),
  138. void *valid_data)
  139. {
  140. struct nl_cb *cb;
  141. int err = -ENOMEM;
  142. cb = nl_cb_clone(drv->nl_cb);
  143. if (!cb)
  144. goto out;
  145. err = nl_send_auto_complete(drv->nl_handle, msg);
  146. if (err < 0)
  147. goto out;
  148. err = 1;
  149. nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
  150. nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
  151. nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
  152. if (valid_handler)
  153. nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
  154. valid_handler, valid_data);
  155. while (err > 0)
  156. nl_recvmsgs(drv->nl_handle, cb);
  157. out:
  158. nl_cb_put(cb);
  159. nlmsg_free(msg);
  160. return err;
  161. }
  162. struct family_data {
  163. const char *group;
  164. int id;
  165. };
  166. static int family_handler(struct nl_msg *msg, void *arg)
  167. {
  168. struct family_data *res = arg;
  169. struct nlattr *tb[CTRL_ATTR_MAX + 1];
  170. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  171. struct nlattr *mcgrp;
  172. int i;
  173. nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  174. genlmsg_attrlen(gnlh, 0), NULL);
  175. if (!tb[CTRL_ATTR_MCAST_GROUPS])
  176. return NL_SKIP;
  177. nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) {
  178. struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1];
  179. nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp),
  180. nla_len(mcgrp), NULL);
  181. if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] ||
  182. !tb2[CTRL_ATTR_MCAST_GRP_ID] ||
  183. os_strncmp(nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]),
  184. res->group,
  185. nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME])) != 0)
  186. continue;
  187. res->id = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]);
  188. break;
  189. };
  190. return NL_SKIP;
  191. }
  192. static int nl_get_multicast_id(struct wpa_driver_nl80211_data *drv,
  193. const char *family, const char *group)
  194. {
  195. struct nl_msg *msg;
  196. int ret = -1;
  197. struct family_data res = { group, -ENOENT };
  198. msg = nlmsg_alloc();
  199. if (!msg)
  200. return -ENOMEM;
  201. genlmsg_put(msg, 0, 0, genl_ctrl_resolve(drv->nl_handle, "nlctrl"),
  202. 0, 0, CTRL_CMD_GETFAMILY, 0);
  203. NLA_PUT_STRING(msg, CTRL_ATTR_FAMILY_NAME, family);
  204. ret = send_and_recv_msgs(drv, msg, family_handler, &res);
  205. msg = NULL;
  206. if (ret == 0)
  207. ret = res.id;
  208. nla_put_failure:
  209. nlmsg_free(msg);
  210. return ret;
  211. }
  212. #ifdef HOSTAPD
  213. static int get_ifhwaddr(struct wpa_driver_nl80211_data *drv,
  214. const char *ifname, u8 *addr)
  215. {
  216. struct ifreq ifr;
  217. os_memset(&ifr, 0, sizeof(ifr));
  218. os_strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
  219. if (ioctl(drv->ioctl_sock, SIOCGIFHWADDR, &ifr)) {
  220. wpa_printf(MSG_ERROR, "%s: ioctl(SIOCGIFHWADDR): %d (%s)",
  221. ifname, errno, strerror(errno));
  222. return -1;
  223. }
  224. if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
  225. wpa_printf(MSG_ERROR, "%s: Invalid HW-addr family 0x%04x",
  226. ifname, ifr.ifr_hwaddr.sa_family);
  227. return -1;
  228. }
  229. os_memcpy(addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
  230. return 0;
  231. }
  232. static int set_ifhwaddr(struct wpa_driver_nl80211_data *drv,
  233. const char *ifname, const u8 *addr)
  234. {
  235. struct ifreq ifr;
  236. os_memset(&ifr, 0, sizeof(ifr));
  237. os_strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
  238. os_memcpy(ifr.ifr_hwaddr.sa_data, addr, ETH_ALEN);
  239. ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
  240. if (ioctl(drv->ioctl_sock, SIOCSIFHWADDR, &ifr)) {
  241. wpa_printf(MSG_DEBUG, "%s: ioctl(SIOCSIFHWADDR): %d (%s)",
  242. ifname, errno, strerror(errno));
  243. return -1;
  244. }
  245. return 0;
  246. }
  247. #endif /* HOSTAPD */
  248. static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid)
  249. {
  250. struct wpa_driver_nl80211_data *drv = priv;
  251. if (!drv->associated)
  252. return -1;
  253. os_memcpy(bssid, drv->bssid, ETH_ALEN);
  254. return 0;
  255. }
  256. static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid)
  257. {
  258. struct wpa_driver_nl80211_data *drv = priv;
  259. if (!drv->associated)
  260. return -1;
  261. os_memcpy(ssid, drv->ssid, drv->ssid_len);
  262. return drv->ssid_len;
  263. }
  264. static void wpa_driver_nl80211_event_link(struct wpa_driver_nl80211_data *drv,
  265. char *buf, size_t len, int del)
  266. {
  267. union wpa_event_data event;
  268. os_memset(&event, 0, sizeof(event));
  269. if (len > sizeof(event.interface_status.ifname))
  270. len = sizeof(event.interface_status.ifname) - 1;
  271. os_memcpy(event.interface_status.ifname, buf, len);
  272. event.interface_status.ievent = del ? EVENT_INTERFACE_REMOVED :
  273. EVENT_INTERFACE_ADDED;
  274. wpa_printf(MSG_DEBUG, "RTM_%sLINK, IFLA_IFNAME: Interface '%s' %s",
  275. del ? "DEL" : "NEW",
  276. event.interface_status.ifname,
  277. del ? "removed" : "added");
  278. if (os_strcmp(drv->ifname, event.interface_status.ifname) == 0) {
  279. if (del)
  280. drv->if_removed = 1;
  281. else
  282. drv->if_removed = 0;
  283. }
  284. wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
  285. }
  286. static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv,
  287. u8 *buf, size_t len)
  288. {
  289. int attrlen, rta_len;
  290. struct rtattr *attr;
  291. attrlen = len;
  292. attr = (struct rtattr *) buf;
  293. rta_len = RTA_ALIGN(sizeof(struct rtattr));
  294. while (RTA_OK(attr, attrlen)) {
  295. if (attr->rta_type == IFLA_IFNAME) {
  296. if (os_strcmp(((char *) attr) + rta_len, drv->ifname)
  297. == 0)
  298. return 1;
  299. else
  300. break;
  301. }
  302. attr = RTA_NEXT(attr, attrlen);
  303. }
  304. return 0;
  305. }
  306. static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv,
  307. int ifindex, u8 *buf, size_t len)
  308. {
  309. if (drv->ifindex == ifindex)
  310. return 1;
  311. if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) {
  312. drv->ifindex = if_nametoindex(drv->ifname);
  313. wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed "
  314. "interface");
  315. wpa_driver_nl80211_finish_drv_init(drv);
  316. return 1;
  317. }
  318. return 0;
  319. }
  320. static void wpa_driver_nl80211_event_rtm_newlink(void *ctx,
  321. struct ifinfomsg *ifi,
  322. u8 *buf, size_t len)
  323. {
  324. struct wpa_driver_nl80211_data *drv = ctx;
  325. int attrlen, rta_len;
  326. struct rtattr *attr;
  327. if (!wpa_driver_nl80211_own_ifindex(drv, ifi->ifi_index, buf, len)) {
  328. wpa_printf(MSG_DEBUG, "Ignore event for foreign ifindex %d",
  329. ifi->ifi_index);
  330. return;
  331. }
  332. wpa_printf(MSG_DEBUG, "RTM_NEWLINK: operstate=%d ifi_flags=0x%x "
  333. "(%s%s%s%s)",
  334. drv->operstate, ifi->ifi_flags,
  335. (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
  336. (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
  337. (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
  338. (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
  339. /*
  340. * Some drivers send the association event before the operup event--in
  341. * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
  342. * fails. This will hit us when wpa_supplicant does not need to do
  343. * IEEE 802.1X authentication
  344. */
  345. if (drv->operstate == 1 &&
  346. (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
  347. !(ifi->ifi_flags & IFF_RUNNING))
  348. netlink_send_oper_ifla(drv->netlink, drv->ifindex,
  349. -1, IF_OPER_UP);
  350. attrlen = len;
  351. attr = (struct rtattr *) buf;
  352. rta_len = RTA_ALIGN(sizeof(struct rtattr));
  353. while (RTA_OK(attr, attrlen)) {
  354. if (attr->rta_type == IFLA_IFNAME) {
  355. wpa_driver_nl80211_event_link(
  356. drv,
  357. ((char *) attr) + rta_len,
  358. attr->rta_len - rta_len, 0);
  359. }
  360. attr = RTA_NEXT(attr, attrlen);
  361. }
  362. }
  363. static void wpa_driver_nl80211_event_rtm_dellink(void *ctx,
  364. struct ifinfomsg *ifi,
  365. u8 *buf, size_t len)
  366. {
  367. struct wpa_driver_nl80211_data *drv = ctx;
  368. int attrlen, rta_len;
  369. struct rtattr *attr;
  370. attrlen = len;
  371. attr = (struct rtattr *) buf;
  372. rta_len = RTA_ALIGN(sizeof(struct rtattr));
  373. while (RTA_OK(attr, attrlen)) {
  374. if (attr->rta_type == IFLA_IFNAME) {
  375. wpa_driver_nl80211_event_link(
  376. drv,
  377. ((char *) attr) + rta_len,
  378. attr->rta_len - rta_len, 1);
  379. }
  380. attr = RTA_NEXT(attr, attrlen);
  381. }
  382. }
  383. static void mlme_event_auth(struct wpa_driver_nl80211_data *drv,
  384. const u8 *frame, size_t len)
  385. {
  386. const struct ieee80211_mgmt *mgmt;
  387. union wpa_event_data event;
  388. mgmt = (const struct ieee80211_mgmt *) frame;
  389. if (len < 24 + sizeof(mgmt->u.auth)) {
  390. wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
  391. "frame");
  392. return;
  393. }
  394. os_memcpy(drv->auth_bssid, mgmt->sa, ETH_ALEN);
  395. os_memset(&event, 0, sizeof(event));
  396. os_memcpy(event.auth.peer, mgmt->sa, ETH_ALEN);
  397. event.auth.auth_type = le_to_host16(mgmt->u.auth.auth_alg);
  398. event.auth.status_code = le_to_host16(mgmt->u.auth.status_code);
  399. if (len > 24 + sizeof(mgmt->u.auth)) {
  400. event.auth.ies = mgmt->u.auth.variable;
  401. event.auth.ies_len = len - 24 - sizeof(mgmt->u.auth);
  402. }
  403. wpa_supplicant_event(drv->ctx, EVENT_AUTH, &event);
  404. }
  405. static void mlme_event_assoc(struct wpa_driver_nl80211_data *drv,
  406. const u8 *frame, size_t len)
  407. {
  408. const struct ieee80211_mgmt *mgmt;
  409. union wpa_event_data event;
  410. u16 status;
  411. mgmt = (const struct ieee80211_mgmt *) frame;
  412. if (len < 24 + sizeof(mgmt->u.assoc_resp)) {
  413. wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
  414. "frame");
  415. return;
  416. }
  417. status = le_to_host16(mgmt->u.assoc_resp.status_code);
  418. if (status != WLAN_STATUS_SUCCESS) {
  419. os_memset(&event, 0, sizeof(event));
  420. if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
  421. event.assoc_reject.resp_ies =
  422. (u8 *) mgmt->u.assoc_resp.variable;
  423. event.assoc_reject.resp_ies_len =
  424. len - 24 - sizeof(mgmt->u.assoc_resp);
  425. }
  426. event.assoc_reject.status_code = status;
  427. wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
  428. return;
  429. }
  430. drv->associated = 1;
  431. os_memcpy(drv->bssid, mgmt->sa, ETH_ALEN);
  432. os_memset(&event, 0, sizeof(event));
  433. if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
  434. event.assoc_info.resp_ies = (u8 *) mgmt->u.assoc_resp.variable;
  435. event.assoc_info.resp_ies_len =
  436. len - 24 - sizeof(mgmt->u.assoc_resp);
  437. }
  438. wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
  439. }
  440. static void mlme_event_connect(struct wpa_driver_nl80211_data *drv,
  441. enum nl80211_commands cmd, struct nlattr *status,
  442. struct nlattr *addr, struct nlattr *req_ie,
  443. struct nlattr *resp_ie)
  444. {
  445. union wpa_event_data event;
  446. if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
  447. /*
  448. * Avoid reporting two association events that would confuse
  449. * the core code.
  450. */
  451. wpa_printf(MSG_DEBUG, "nl80211: Ignore connect event (cmd=%d) "
  452. "when using userspace SME", cmd);
  453. return;
  454. }
  455. os_memset(&event, 0, sizeof(event));
  456. if (cmd == NL80211_CMD_CONNECT &&
  457. nla_get_u16(status) != WLAN_STATUS_SUCCESS) {
  458. if (resp_ie) {
  459. event.assoc_reject.resp_ies = nla_data(resp_ie);
  460. event.assoc_reject.resp_ies_len = nla_len(resp_ie);
  461. }
  462. event.assoc_reject.status_code = nla_get_u16(status);
  463. wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
  464. return;
  465. }
  466. drv->associated = 1;
  467. if (addr)
  468. os_memcpy(drv->bssid, nla_data(addr), ETH_ALEN);
  469. if (req_ie) {
  470. event.assoc_info.req_ies = nla_data(req_ie);
  471. event.assoc_info.req_ies_len = nla_len(req_ie);
  472. }
  473. if (resp_ie) {
  474. event.assoc_info.resp_ies = nla_data(resp_ie);
  475. event.assoc_info.resp_ies_len = nla_len(resp_ie);
  476. }
  477. wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
  478. }
  479. static void mlme_timeout_event(struct wpa_driver_nl80211_data *drv,
  480. enum nl80211_commands cmd, struct nlattr *addr)
  481. {
  482. union wpa_event_data event;
  483. enum wpa_event_type ev;
  484. if (nla_len(addr) != ETH_ALEN)
  485. return;
  486. wpa_printf(MSG_DEBUG, "nl80211: MLME event %d; timeout with " MACSTR,
  487. cmd, MAC2STR((u8 *) nla_data(addr)));
  488. if (cmd == NL80211_CMD_AUTHENTICATE)
  489. ev = EVENT_AUTH_TIMED_OUT;
  490. else if (cmd == NL80211_CMD_ASSOCIATE)
  491. ev = EVENT_ASSOC_TIMED_OUT;
  492. else
  493. return;
  494. os_memset(&event, 0, sizeof(event));
  495. os_memcpy(event.timeout_event.addr, nla_data(addr), ETH_ALEN);
  496. wpa_supplicant_event(drv->ctx, ev, &event);
  497. }
  498. static void mlme_event(struct wpa_driver_nl80211_data *drv,
  499. enum nl80211_commands cmd, struct nlattr *frame,
  500. struct nlattr *addr, struct nlattr *timed_out)
  501. {
  502. if (timed_out && addr) {
  503. mlme_timeout_event(drv, cmd, addr);
  504. return;
  505. }
  506. if (frame == NULL) {
  507. wpa_printf(MSG_DEBUG, "nl80211: MLME event %d without frame "
  508. "data", cmd);
  509. return;
  510. }
  511. wpa_printf(MSG_DEBUG, "nl80211: MLME event %d", cmd);
  512. wpa_hexdump(MSG_MSGDUMP, "nl80211: MLME event frame",
  513. nla_data(frame), nla_len(frame));
  514. switch (cmd) {
  515. case NL80211_CMD_AUTHENTICATE:
  516. mlme_event_auth(drv, nla_data(frame), nla_len(frame));
  517. break;
  518. case NL80211_CMD_ASSOCIATE:
  519. mlme_event_assoc(drv, nla_data(frame), nla_len(frame));
  520. break;
  521. case NL80211_CMD_DEAUTHENTICATE:
  522. drv->associated = 0;
  523. wpa_supplicant_event(drv->ctx, EVENT_DEAUTH, NULL);
  524. break;
  525. case NL80211_CMD_DISASSOCIATE:
  526. drv->associated = 0;
  527. wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, NULL);
  528. break;
  529. default:
  530. break;
  531. }
  532. }
  533. static void mlme_event_michael_mic_failure(struct wpa_driver_nl80211_data *drv,
  534. struct nlattr *tb[])
  535. {
  536. union wpa_event_data data;
  537. wpa_printf(MSG_DEBUG, "nl80211: MLME event Michael MIC failure");
  538. os_memset(&data, 0, sizeof(data));
  539. if (tb[NL80211_ATTR_MAC]) {
  540. wpa_hexdump(MSG_DEBUG, "nl80211: Source MAC address",
  541. nla_data(tb[NL80211_ATTR_MAC]),
  542. nla_len(tb[NL80211_ATTR_MAC]));
  543. data.michael_mic_failure.src = nla_data(tb[NL80211_ATTR_MAC]);
  544. }
  545. if (tb[NL80211_ATTR_KEY_SEQ]) {
  546. wpa_hexdump(MSG_DEBUG, "nl80211: TSC",
  547. nla_data(tb[NL80211_ATTR_KEY_SEQ]),
  548. nla_len(tb[NL80211_ATTR_KEY_SEQ]));
  549. }
  550. if (tb[NL80211_ATTR_KEY_TYPE]) {
  551. enum nl80211_key_type key_type =
  552. nla_get_u32(tb[NL80211_ATTR_KEY_TYPE]);
  553. wpa_printf(MSG_DEBUG, "nl80211: Key Type %d", key_type);
  554. if (key_type == NL80211_KEYTYPE_PAIRWISE)
  555. data.michael_mic_failure.unicast = 1;
  556. } else
  557. data.michael_mic_failure.unicast = 1;
  558. if (tb[NL80211_ATTR_KEY_IDX]) {
  559. u8 key_id = nla_get_u8(tb[NL80211_ATTR_KEY_IDX]);
  560. wpa_printf(MSG_DEBUG, "nl80211: Key Id %d", key_id);
  561. }
  562. wpa_supplicant_event(drv->ctx, EVENT_MICHAEL_MIC_FAILURE, &data);
  563. }
  564. static void mlme_event_join_ibss(struct wpa_driver_nl80211_data *drv,
  565. struct nlattr *tb[])
  566. {
  567. if (tb[NL80211_ATTR_MAC] == NULL) {
  568. wpa_printf(MSG_DEBUG, "nl80211: No address in IBSS joined "
  569. "event");
  570. return;
  571. }
  572. os_memcpy(drv->bssid, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
  573. drv->associated = 1;
  574. wpa_printf(MSG_DEBUG, "nl80211: IBSS " MACSTR " joined",
  575. MAC2STR(drv->bssid));
  576. wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL);
  577. }
  578. static int process_event(struct nl_msg *msg, void *arg)
  579. {
  580. struct wpa_driver_nl80211_data *drv = arg;
  581. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  582. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  583. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  584. genlmsg_attrlen(gnlh, 0), NULL);
  585. if (tb[NL80211_ATTR_IFINDEX]) {
  586. int ifindex = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
  587. if (ifindex != drv->ifindex) {
  588. wpa_printf(MSG_DEBUG, "nl80211: Ignored event (cmd=%d)"
  589. " for foreign interface (ifindex %d)",
  590. gnlh->cmd, ifindex);
  591. return NL_SKIP;
  592. }
  593. }
  594. if (drv->ap_scan_as_station &&
  595. (gnlh->cmd == NL80211_CMD_NEW_SCAN_RESULTS ||
  596. gnlh->cmd == NL80211_CMD_SCAN_ABORTED)) {
  597. wpa_driver_nl80211_set_mode(drv, IEEE80211_MODE_AP);
  598. drv->ap_scan_as_station = 0;
  599. }
  600. switch (gnlh->cmd) {
  601. case NL80211_CMD_TRIGGER_SCAN:
  602. wpa_printf(MSG_DEBUG, "nl80211: Scan trigger");
  603. break;
  604. case NL80211_CMD_NEW_SCAN_RESULTS:
  605. wpa_printf(MSG_DEBUG, "nl80211: New scan results available");
  606. drv->scan_complete_events = 1;
  607. eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
  608. drv->ctx);
  609. wpa_supplicant_event(drv->ctx, EVENT_SCAN_RESULTS, NULL);
  610. break;
  611. case NL80211_CMD_SCAN_ABORTED:
  612. wpa_printf(MSG_DEBUG, "nl80211: Scan aborted");
  613. /*
  614. * Need to indicate that scan results are available in order
  615. * not to make wpa_supplicant stop its scanning.
  616. */
  617. eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
  618. drv->ctx);
  619. wpa_supplicant_event(drv->ctx, EVENT_SCAN_RESULTS, NULL);
  620. break;
  621. case NL80211_CMD_AUTHENTICATE:
  622. case NL80211_CMD_ASSOCIATE:
  623. case NL80211_CMD_DEAUTHENTICATE:
  624. case NL80211_CMD_DISASSOCIATE:
  625. mlme_event(drv, gnlh->cmd, tb[NL80211_ATTR_FRAME],
  626. tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT]);
  627. break;
  628. case NL80211_CMD_CONNECT:
  629. case NL80211_CMD_ROAM:
  630. mlme_event_connect(drv, gnlh->cmd,
  631. tb[NL80211_ATTR_STATUS_CODE],
  632. tb[NL80211_ATTR_MAC],
  633. tb[NL80211_ATTR_REQ_IE],
  634. tb[NL80211_ATTR_RESP_IE]);
  635. break;
  636. case NL80211_CMD_DISCONNECT:
  637. if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
  638. /*
  639. * Avoid reporting two disassociation events that could
  640. * confuse the core code.
  641. */
  642. wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
  643. "event when using userspace SME");
  644. break;
  645. }
  646. drv->associated = 0;
  647. wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, NULL);
  648. break;
  649. case NL80211_CMD_MICHAEL_MIC_FAILURE:
  650. mlme_event_michael_mic_failure(drv, tb);
  651. break;
  652. case NL80211_CMD_JOIN_IBSS:
  653. mlme_event_join_ibss(drv, tb);
  654. break;
  655. default:
  656. wpa_printf(MSG_DEBUG, "nl80211: Ignored unknown event "
  657. "(cmd=%d)", gnlh->cmd);
  658. break;
  659. }
  660. return NL_SKIP;
  661. }
  662. static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
  663. void *sock_ctx)
  664. {
  665. struct nl_cb *cb;
  666. struct wpa_driver_nl80211_data *drv = eloop_ctx;
  667. wpa_printf(MSG_DEBUG, "nl80211: Event message available");
  668. cb = nl_cb_clone(drv->nl_cb);
  669. if (!cb)
  670. return;
  671. nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
  672. nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, process_event, drv);
  673. nl_recvmsgs(drv->nl_handle_event, cb);
  674. nl_cb_put(cb);
  675. }
  676. static int hostapd_set_iface_flags(struct wpa_driver_nl80211_data *drv,
  677. const char *ifname, int dev_up)
  678. {
  679. struct ifreq ifr;
  680. if (drv->ioctl_sock < 0)
  681. return -1;
  682. os_memset(&ifr, 0, sizeof(ifr));
  683. os_strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
  684. if (ioctl(drv->ioctl_sock, SIOCGIFFLAGS, &ifr) != 0) {
  685. perror("ioctl[SIOCGIFFLAGS]");
  686. wpa_printf(MSG_DEBUG, "Could not read interface flags (%s)",
  687. ifname);
  688. return -1;
  689. }
  690. if (dev_up) {
  691. if (ifr.ifr_flags & IFF_UP)
  692. return 0;
  693. ifr.ifr_flags |= IFF_UP;
  694. } else {
  695. if (!(ifr.ifr_flags & IFF_UP))
  696. return 0;
  697. ifr.ifr_flags &= ~IFF_UP;
  698. }
  699. if (ioctl(drv->ioctl_sock, SIOCSIFFLAGS, &ifr) != 0) {
  700. perror("ioctl[SIOCSIFFLAGS]");
  701. return -1;
  702. }
  703. return 0;
  704. }
  705. /**
  706. * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
  707. * @priv: driver_nl80211 private data
  708. * @alpha2_arg: country to which to switch to
  709. * Returns: 0 on success, -1 on failure
  710. *
  711. * This asks nl80211 to set the regulatory domain for given
  712. * country ISO / IEC alpha2.
  713. */
  714. static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
  715. {
  716. struct wpa_driver_nl80211_data *drv = priv;
  717. char alpha2[3];
  718. struct nl_msg *msg;
  719. msg = nlmsg_alloc();
  720. if (!msg)
  721. return -ENOMEM;
  722. alpha2[0] = alpha2_arg[0];
  723. alpha2[1] = alpha2_arg[1];
  724. alpha2[2] = '\0';
  725. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  726. 0, NL80211_CMD_REQ_SET_REG, 0);
  727. NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2);
  728. if (send_and_recv_msgs(drv, msg, NULL, NULL))
  729. return -EINVAL;
  730. return 0;
  731. nla_put_failure:
  732. return -EINVAL;
  733. }
  734. #ifndef HOSTAPD
  735. struct wiphy_info_data {
  736. int max_scan_ssids;
  737. int ap_supported;
  738. int auth_supported;
  739. int connect_supported;
  740. };
  741. static int wiphy_info_handler(struct nl_msg *msg, void *arg)
  742. {
  743. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  744. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  745. struct wiphy_info_data *info = arg;
  746. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  747. genlmsg_attrlen(gnlh, 0), NULL);
  748. if (tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS])
  749. info->max_scan_ssids =
  750. nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS]);
  751. if (tb[NL80211_ATTR_SUPPORTED_IFTYPES]) {
  752. struct nlattr *nl_mode;
  753. int i;
  754. nla_for_each_nested(nl_mode,
  755. tb[NL80211_ATTR_SUPPORTED_IFTYPES], i) {
  756. if (nl_mode->nla_type == NL80211_IFTYPE_AP) {
  757. info->ap_supported = 1;
  758. break;
  759. }
  760. }
  761. }
  762. if (tb[NL80211_ATTR_SUPPORTED_COMMANDS]) {
  763. struct nlattr *nl_cmd;
  764. int i;
  765. nla_for_each_nested(nl_cmd,
  766. tb[NL80211_ATTR_SUPPORTED_COMMANDS], i) {
  767. u32 cmd = nla_get_u32(nl_cmd);
  768. if (cmd == NL80211_CMD_AUTHENTICATE)
  769. info->auth_supported = 1;
  770. else if (cmd == NL80211_CMD_CONNECT)
  771. info->connect_supported = 1;
  772. }
  773. }
  774. return NL_SKIP;
  775. }
  776. static int wpa_driver_nl80211_get_info(struct wpa_driver_nl80211_data *drv,
  777. struct wiphy_info_data *info)
  778. {
  779. struct nl_msg *msg;
  780. os_memset(info, 0, sizeof(*info));
  781. msg = nlmsg_alloc();
  782. if (!msg)
  783. return -1;
  784. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  785. 0, NL80211_CMD_GET_WIPHY, 0);
  786. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  787. if (send_and_recv_msgs(drv, msg, wiphy_info_handler, info) == 0)
  788. return 0;
  789. msg = NULL;
  790. nla_put_failure:
  791. nlmsg_free(msg);
  792. return -1;
  793. }
  794. static int wpa_driver_nl80211_capa(struct wpa_driver_nl80211_data *drv)
  795. {
  796. struct wiphy_info_data info;
  797. if (wpa_driver_nl80211_get_info(drv, &info))
  798. return -1;
  799. drv->has_capability = 1;
  800. /* For now, assume TKIP, CCMP, WPA, WPA2 are supported */
  801. drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  802. WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
  803. WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
  804. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK;
  805. drv->capa.enc = WPA_DRIVER_CAPA_ENC_WEP40 |
  806. WPA_DRIVER_CAPA_ENC_WEP104 |
  807. WPA_DRIVER_CAPA_ENC_TKIP |
  808. WPA_DRIVER_CAPA_ENC_CCMP;
  809. drv->capa.max_scan_ssids = info.max_scan_ssids;
  810. if (info.ap_supported)
  811. drv->capa.flags |= WPA_DRIVER_FLAGS_AP;
  812. if (info.auth_supported)
  813. drv->capa.flags |= WPA_DRIVER_FLAGS_SME;
  814. else if (!info.connect_supported) {
  815. wpa_printf(MSG_INFO, "nl80211: Driver does not support "
  816. "authentication/association or connect commands");
  817. return -1;
  818. }
  819. drv->capa.flags |= WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE;
  820. return 0;
  821. }
  822. #endif /* HOSTAPD */
  823. static int wpa_driver_nl80211_init_nl(struct wpa_driver_nl80211_data *drv,
  824. void *ctx)
  825. {
  826. int ret;
  827. /* Initialize generic netlink and nl80211 */
  828. drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
  829. if (drv->nl_cb == NULL) {
  830. wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
  831. "callbacks");
  832. goto err1;
  833. }
  834. drv->nl_handle = nl_handle_alloc_cb(drv->nl_cb);
  835. if (drv->nl_handle == NULL) {
  836. wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
  837. "callbacks");
  838. goto err2;
  839. }
  840. drv->nl_handle_event = nl_handle_alloc_cb(drv->nl_cb);
  841. if (drv->nl_handle_event == NULL) {
  842. wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
  843. "callbacks (event)");
  844. goto err2b;
  845. }
  846. if (genl_connect(drv->nl_handle)) {
  847. wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic "
  848. "netlink");
  849. goto err3;
  850. }
  851. if (genl_connect(drv->nl_handle_event)) {
  852. wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic "
  853. "netlink (event)");
  854. goto err3;
  855. }
  856. #ifdef CONFIG_LIBNL20
  857. if (genl_ctrl_alloc_cache(drv->nl_handle, &drv->nl_cache) < 0) {
  858. wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
  859. "netlink cache");
  860. goto err3;
  861. }
  862. if (genl_ctrl_alloc_cache(drv->nl_handle_event, &drv->nl_cache_event) <
  863. 0) {
  864. wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
  865. "netlink cache (event)");
  866. goto err3b;
  867. }
  868. #else /* CONFIG_LIBNL20 */
  869. drv->nl_cache = genl_ctrl_alloc_cache(drv->nl_handle);
  870. if (drv->nl_cache == NULL) {
  871. wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
  872. "netlink cache");
  873. goto err3;
  874. }
  875. drv->nl_cache_event = genl_ctrl_alloc_cache(drv->nl_handle_event);
  876. if (drv->nl_cache_event == NULL) {
  877. wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
  878. "netlink cache (event)");
  879. goto err3b;
  880. }
  881. #endif /* CONFIG_LIBNL20 */
  882. drv->nl80211 = genl_ctrl_search_by_name(drv->nl_cache, "nl80211");
  883. if (drv->nl80211 == NULL) {
  884. wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
  885. "found");
  886. goto err4;
  887. }
  888. ret = nl_get_multicast_id(drv, "nl80211", "scan");
  889. if (ret >= 0)
  890. ret = nl_socket_add_membership(drv->nl_handle_event, ret);
  891. if (ret < 0) {
  892. wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
  893. "membership for scan events: %d (%s)",
  894. ret, strerror(-ret));
  895. goto err4;
  896. }
  897. ret = nl_get_multicast_id(drv, "nl80211", "mlme");
  898. if (ret >= 0)
  899. ret = nl_socket_add_membership(drv->nl_handle_event, ret);
  900. if (ret < 0) {
  901. wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
  902. "membership for mlme events: %d (%s)",
  903. ret, strerror(-ret));
  904. goto err4;
  905. }
  906. eloop_register_read_sock(nl_socket_get_fd(drv->nl_handle_event),
  907. wpa_driver_nl80211_event_receive, drv, ctx);
  908. return 0;
  909. err4:
  910. nl_cache_free(drv->nl_cache_event);
  911. err3b:
  912. nl_cache_free(drv->nl_cache);
  913. err3:
  914. nl_handle_destroy(drv->nl_handle_event);
  915. err2b:
  916. nl_handle_destroy(drv->nl_handle);
  917. err2:
  918. nl_cb_put(drv->nl_cb);
  919. err1:
  920. return -1;
  921. }
  922. /**
  923. * wpa_driver_nl80211_init - Initialize nl80211 driver interface
  924. * @ctx: context to be used when calling wpa_supplicant functions,
  925. * e.g., wpa_supplicant_event()
  926. * @ifname: interface name, e.g., wlan0
  927. * Returns: Pointer to private data, %NULL on failure
  928. */
  929. static void * wpa_driver_nl80211_init(void *ctx, const char *ifname)
  930. {
  931. struct wpa_driver_nl80211_data *drv;
  932. struct netlink_config *cfg;
  933. drv = os_zalloc(sizeof(*drv));
  934. if (drv == NULL)
  935. return NULL;
  936. drv->ctx = ctx;
  937. os_strlcpy(drv->ifname, ifname, sizeof(drv->ifname));
  938. drv->monitor_ifidx = -1;
  939. drv->monitor_sock = -1;
  940. drv->ioctl_sock = -1;
  941. if (wpa_driver_nl80211_init_nl(drv, ctx)) {
  942. os_free(drv);
  943. return NULL;
  944. }
  945. drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
  946. if (drv->ioctl_sock < 0) {
  947. perror("socket(PF_INET,SOCK_DGRAM)");
  948. goto failed;
  949. }
  950. cfg = os_zalloc(sizeof(*cfg));
  951. if (cfg == NULL)
  952. goto failed;
  953. cfg->ctx = drv;
  954. cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
  955. cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
  956. drv->netlink = netlink_init(cfg);
  957. if (drv->netlink == NULL) {
  958. os_free(cfg);
  959. goto failed;
  960. }
  961. if (wpa_driver_nl80211_finish_drv_init(drv))
  962. goto failed;
  963. return drv;
  964. failed:
  965. netlink_deinit(drv->netlink);
  966. if (drv->ioctl_sock >= 0)
  967. close(drv->ioctl_sock);
  968. genl_family_put(drv->nl80211);
  969. nl_cache_free(drv->nl_cache);
  970. nl_handle_destroy(drv->nl_handle);
  971. nl_cb_put(drv->nl_cb);
  972. os_free(drv);
  973. return NULL;
  974. }
  975. static int
  976. wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv)
  977. {
  978. drv->ifindex = if_nametoindex(drv->ifname);
  979. #ifndef HOSTAPD
  980. if (wpa_driver_nl80211_set_mode(drv, IEEE80211_MODE_INFRA) < 0) {
  981. wpa_printf(MSG_DEBUG, "nl80211: Could not configure driver to "
  982. "use managed mode");
  983. }
  984. if (hostapd_set_iface_flags(drv, drv->ifname, 1)) {
  985. wpa_printf(MSG_ERROR, "Could not set interface '%s' "
  986. "UP", drv->ifname);
  987. return -1;
  988. }
  989. if (wpa_driver_nl80211_capa(drv))
  990. return -1;
  991. netlink_send_oper_ifla(drv->netlink, drv->ifindex,
  992. 1, IF_OPER_DORMANT);
  993. #endif /* HOSTAPD */
  994. return 0;
  995. }
  996. #ifdef HOSTAPD
  997. static void wpa_driver_nl80211_free_bss(struct wpa_driver_nl80211_data *drv)
  998. {
  999. struct i802_bss *bss, *prev;
  1000. bss = drv->bss.next;
  1001. while (bss) {
  1002. prev = bss;
  1003. bss = bss->next;
  1004. os_free(bss);
  1005. }
  1006. }
  1007. #endif /* HOSTAPD */
  1008. static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data *drv)
  1009. {
  1010. struct nl_msg *msg;
  1011. msg = nlmsg_alloc();
  1012. if (!msg)
  1013. return -ENOMEM;
  1014. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  1015. 0, NL80211_CMD_DEL_BEACON, 0);
  1016. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  1017. return send_and_recv_msgs(drv, msg, NULL, NULL);
  1018. nla_put_failure:
  1019. return -ENOBUFS;
  1020. }
  1021. /**
  1022. * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
  1023. * @priv: Pointer to private nl80211 data from wpa_driver_nl80211_init()
  1024. *
  1025. * Shut down driver interface and processing of driver events. Free
  1026. * private data buffer if one was allocated in wpa_driver_nl80211_init().
  1027. */
  1028. static void wpa_driver_nl80211_deinit(void *priv)
  1029. {
  1030. struct wpa_driver_nl80211_data *drv = priv;
  1031. nl80211_remove_monitor_interface(drv);
  1032. if (drv->monitor_sock >= 0) {
  1033. eloop_unregister_read_sock(drv->monitor_sock);
  1034. close(drv->monitor_sock);
  1035. }
  1036. if (drv->nlmode == NL80211_IFTYPE_AP)
  1037. wpa_driver_nl80211_del_beacon(drv);
  1038. #ifdef HOSTAPD
  1039. if (drv->last_freq_ht) {
  1040. /* Clear HT flags from the driver */
  1041. struct hostapd_freq_params freq;
  1042. os_memset(&freq, 0, sizeof(freq));
  1043. freq.freq = drv->last_freq;
  1044. i802_set_freq(priv, &freq);
  1045. }
  1046. if (drv->eapol_sock >= 0) {
  1047. eloop_unregister_read_sock(drv->eapol_sock);
  1048. close(drv->eapol_sock);
  1049. }
  1050. if (drv->if_indices != drv->default_if_indices)
  1051. os_free(drv->if_indices);
  1052. wpa_driver_nl80211_free_bss(drv);
  1053. #endif /* HOSTAPD */
  1054. netlink_send_oper_ifla(drv->netlink, drv->ifindex, 0, IF_OPER_UP);
  1055. netlink_deinit(drv->netlink);
  1056. eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
  1057. (void) hostapd_set_iface_flags(drv, drv->ifname, 0);
  1058. wpa_driver_nl80211_set_mode(drv, IEEE80211_MODE_INFRA);
  1059. if (drv->ioctl_sock >= 0)
  1060. close(drv->ioctl_sock);
  1061. eloop_unregister_read_sock(nl_socket_get_fd(drv->nl_handle_event));
  1062. genl_family_put(drv->nl80211);
  1063. nl_cache_free(drv->nl_cache);
  1064. nl_cache_free(drv->nl_cache_event);
  1065. nl_handle_destroy(drv->nl_handle);
  1066. nl_handle_destroy(drv->nl_handle_event);
  1067. nl_cb_put(drv->nl_cb);
  1068. os_free(drv);
  1069. }
  1070. /**
  1071. * wpa_driver_nl80211_scan_timeout - Scan timeout to report scan completion
  1072. * @eloop_ctx: Driver private data
  1073. * @timeout_ctx: ctx argument given to wpa_driver_nl80211_init()
  1074. *
  1075. * This function can be used as registered timeout when starting a scan to
  1076. * generate a scan completed event if the driver does not report this.
  1077. */
  1078. static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx, void *timeout_ctx)
  1079. {
  1080. struct wpa_driver_nl80211_data *drv = eloop_ctx;
  1081. if (drv->ap_scan_as_station) {
  1082. wpa_driver_nl80211_set_mode(drv, IEEE80211_MODE_AP);
  1083. drv->ap_scan_as_station = 0;
  1084. }
  1085. wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
  1086. wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
  1087. }
  1088. /**
  1089. * wpa_driver_nl80211_scan - Request the driver to initiate scan
  1090. * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
  1091. * @params: Scan parameters
  1092. * Returns: 0 on success, -1 on failure
  1093. */
  1094. static int wpa_driver_nl80211_scan(void *priv,
  1095. struct wpa_driver_scan_params *params)
  1096. {
  1097. struct wpa_driver_nl80211_data *drv = priv;
  1098. int ret = 0, timeout;
  1099. struct nl_msg *msg, *ssids, *freqs;
  1100. size_t i;
  1101. msg = nlmsg_alloc();
  1102. ssids = nlmsg_alloc();
  1103. freqs = nlmsg_alloc();
  1104. if (!msg || !ssids || !freqs) {
  1105. nlmsg_free(msg);
  1106. nlmsg_free(ssids);
  1107. nlmsg_free(freqs);
  1108. return -1;
  1109. }
  1110. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  1111. NL80211_CMD_TRIGGER_SCAN, 0);
  1112. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  1113. for (i = 0; i < params->num_ssids; i++) {
  1114. NLA_PUT(ssids, i + 1, params->ssids[i].ssid_len,
  1115. params->ssids[i].ssid);
  1116. }
  1117. if (params->num_ssids)
  1118. nla_put_nested(msg, NL80211_ATTR_SCAN_SSIDS, ssids);
  1119. if (params->extra_ies) {
  1120. NLA_PUT(msg, NL80211_ATTR_IE, params->extra_ies_len,
  1121. params->extra_ies);
  1122. }
  1123. if (params->freqs) {
  1124. for (i = 0; params->freqs[i]; i++)
  1125. NLA_PUT_U32(freqs, i + 1, params->freqs[i]);
  1126. nla_put_nested(msg, NL80211_ATTR_SCAN_FREQUENCIES, freqs);
  1127. }
  1128. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  1129. msg = NULL;
  1130. if (ret) {
  1131. wpa_printf(MSG_DEBUG, "nl80211: Scan trigger failed: ret=%d "
  1132. "(%s)", ret, strerror(-ret));
  1133. #ifdef HOSTAPD
  1134. if (drv->nlmode == NL80211_IFTYPE_AP) {
  1135. /*
  1136. * mac80211 does not allow scan requests in AP mode, so
  1137. * try to do this in station mode.
  1138. */
  1139. if (wpa_driver_nl80211_set_mode(drv,
  1140. IEEE80211_MODE_INFRA))
  1141. goto nla_put_failure;
  1142. if (wpa_driver_nl80211_scan(drv, params)) {
  1143. wpa_driver_nl80211_set_mode(drv,
  1144. IEEE80211_MODE_AP);
  1145. goto nla_put_failure;
  1146. }
  1147. /* Restore AP mode when processing scan results */
  1148. drv->ap_scan_as_station = 1;
  1149. ret = 0;
  1150. } else
  1151. goto nla_put_failure;
  1152. #else /* HOSTAPD */
  1153. goto nla_put_failure;
  1154. #endif /* HOSTAPD */
  1155. }
  1156. /* Not all drivers generate "scan completed" wireless event, so try to
  1157. * read results after a timeout. */
  1158. timeout = 10;
  1159. if (drv->scan_complete_events) {
  1160. /*
  1161. * The driver seems to deliver events to notify when scan is
  1162. * complete, so use longer timeout to avoid race conditions
  1163. * with scanning and following association request.
  1164. */
  1165. timeout = 30;
  1166. }
  1167. wpa_printf(MSG_DEBUG, "Scan requested (ret=%d) - scan timeout %d "
  1168. "seconds", ret, timeout);
  1169. eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
  1170. eloop_register_timeout(timeout, 0, wpa_driver_nl80211_scan_timeout,
  1171. drv, drv->ctx);
  1172. nla_put_failure:
  1173. nlmsg_free(ssids);
  1174. nlmsg_free(msg);
  1175. nlmsg_free(freqs);
  1176. return ret;
  1177. }
  1178. static int bss_info_handler(struct nl_msg *msg, void *arg)
  1179. {
  1180. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  1181. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  1182. struct nlattr *bss[NL80211_BSS_MAX + 1];
  1183. static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
  1184. [NL80211_BSS_BSSID] = { .type = NLA_UNSPEC },
  1185. [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
  1186. [NL80211_BSS_TSF] = { .type = NLA_U64 },
  1187. [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
  1188. [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
  1189. [NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC },
  1190. [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
  1191. [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
  1192. [NL80211_BSS_STATUS] = { .type = NLA_U32 },
  1193. [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 },
  1194. };
  1195. struct wpa_scan_results *res = arg;
  1196. struct wpa_scan_res **tmp;
  1197. struct wpa_scan_res *r;
  1198. const u8 *ie;
  1199. size_t ie_len;
  1200. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  1201. genlmsg_attrlen(gnlh, 0), NULL);
  1202. if (!tb[NL80211_ATTR_BSS])
  1203. return NL_SKIP;
  1204. if (nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
  1205. bss_policy))
  1206. return NL_SKIP;
  1207. if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
  1208. ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
  1209. ie_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
  1210. } else {
  1211. ie = NULL;
  1212. ie_len = 0;
  1213. }
  1214. r = os_zalloc(sizeof(*r) + ie_len);
  1215. if (r == NULL)
  1216. return NL_SKIP;
  1217. if (bss[NL80211_BSS_BSSID])
  1218. os_memcpy(r->bssid, nla_data(bss[NL80211_BSS_BSSID]),
  1219. ETH_ALEN);
  1220. if (bss[NL80211_BSS_FREQUENCY])
  1221. r->freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
  1222. if (bss[NL80211_BSS_BEACON_INTERVAL])
  1223. r->beacon_int = nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL]);
  1224. if (bss[NL80211_BSS_CAPABILITY])
  1225. r->caps = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
  1226. r->flags |= WPA_SCAN_NOISE_INVALID;
  1227. if (bss[NL80211_BSS_SIGNAL_MBM]) {
  1228. r->level = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]);
  1229. r->level /= 100; /* mBm to dBm */
  1230. r->flags |= WPA_SCAN_LEVEL_DBM | WPA_SCAN_QUAL_INVALID;
  1231. } else if (bss[NL80211_BSS_SIGNAL_UNSPEC]) {
  1232. r->level = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]);
  1233. r->flags |= WPA_SCAN_LEVEL_INVALID;
  1234. } else
  1235. r->flags |= WPA_SCAN_LEVEL_INVALID | WPA_SCAN_QUAL_INVALID;
  1236. if (bss[NL80211_BSS_TSF])
  1237. r->tsf = nla_get_u64(bss[NL80211_BSS_TSF]);
  1238. if (bss[NL80211_BSS_SEEN_MS_AGO])
  1239. r->age = nla_get_u32(bss[NL80211_BSS_SEEN_MS_AGO]);
  1240. r->ie_len = ie_len;
  1241. if (ie)
  1242. os_memcpy(r + 1, ie, ie_len);
  1243. if (bss[NL80211_BSS_STATUS]) {
  1244. enum nl80211_bss_status status;
  1245. status = nla_get_u32(bss[NL80211_BSS_STATUS]);
  1246. switch (status) {
  1247. case NL80211_BSS_STATUS_AUTHENTICATED:
  1248. r->flags |= WPA_SCAN_AUTHENTICATED;
  1249. break;
  1250. case NL80211_BSS_STATUS_ASSOCIATED:
  1251. r->flags |= WPA_SCAN_ASSOCIATED;
  1252. break;
  1253. default:
  1254. break;
  1255. }
  1256. }
  1257. tmp = os_realloc(res->res,
  1258. (res->num + 1) * sizeof(struct wpa_scan_res *));
  1259. if (tmp == NULL) {
  1260. os_free(r);
  1261. return NL_SKIP;
  1262. }
  1263. tmp[res->num++] = r;
  1264. res->res = tmp;
  1265. return NL_SKIP;
  1266. }
  1267. static void clear_state_mismatch(struct wpa_driver_nl80211_data *drv,
  1268. const u8 *addr)
  1269. {
  1270. if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
  1271. wpa_printf(MSG_DEBUG, "nl80211: Clear possible state "
  1272. "mismatch");
  1273. wpa_driver_nl80211_mlme(drv, addr,
  1274. NL80211_CMD_DEAUTHENTICATE,
  1275. WLAN_REASON_PREV_AUTH_NOT_VALID);
  1276. }
  1277. }
  1278. static void wpa_driver_nl80211_check_bss_status(
  1279. struct wpa_driver_nl80211_data *drv, struct wpa_scan_results *res)
  1280. {
  1281. size_t i;
  1282. for (i = 0; i < res->num; i++) {
  1283. struct wpa_scan_res *r = res->res[i];
  1284. if (r->flags & WPA_SCAN_AUTHENTICATED) {
  1285. wpa_printf(MSG_DEBUG, "nl80211: Scan results "
  1286. "indicates BSS status with " MACSTR
  1287. " as authenticated",
  1288. MAC2STR(r->bssid));
  1289. if (drv->nlmode == NL80211_IFTYPE_STATION &&
  1290. os_memcmp(r->bssid, drv->bssid, ETH_ALEN) != 0 &&
  1291. os_memcmp(r->bssid, drv->auth_bssid, ETH_ALEN) !=
  1292. 0) {
  1293. wpa_printf(MSG_DEBUG, "nl80211: Unknown BSSID"
  1294. " in local state (auth=" MACSTR
  1295. " assoc=" MACSTR ")",
  1296. MAC2STR(drv->auth_bssid),
  1297. MAC2STR(drv->bssid));
  1298. }
  1299. }
  1300. if (r->flags & WPA_SCAN_ASSOCIATED) {
  1301. wpa_printf(MSG_DEBUG, "nl80211: Scan results "
  1302. "indicate BSS status with " MACSTR
  1303. " as associated",
  1304. MAC2STR(r->bssid));
  1305. if (drv->nlmode == NL80211_IFTYPE_STATION &&
  1306. !drv->associated) {
  1307. wpa_printf(MSG_DEBUG, "nl80211: Local state "
  1308. "(not associated) does not match "
  1309. "with BSS state");
  1310. clear_state_mismatch(drv, r->bssid);
  1311. } else if (drv->nlmode == NL80211_IFTYPE_STATION &&
  1312. os_memcmp(drv->bssid, r->bssid, ETH_ALEN) !=
  1313. 0) {
  1314. wpa_printf(MSG_DEBUG, "nl80211: Local state "
  1315. "(associated with " MACSTR ") does "
  1316. "not match with BSS state",
  1317. MAC2STR(drv->bssid));
  1318. clear_state_mismatch(drv, r->bssid);
  1319. clear_state_mismatch(drv, drv->bssid);
  1320. }
  1321. }
  1322. }
  1323. }
  1324. /**
  1325. * wpa_driver_nl80211_get_scan_results - Fetch the latest scan results
  1326. * @priv: Pointer to private wext data from wpa_driver_nl80211_init()
  1327. * Returns: Scan results on success, -1 on failure
  1328. */
  1329. static struct wpa_scan_results *
  1330. wpa_driver_nl80211_get_scan_results(void *priv)
  1331. {
  1332. struct wpa_driver_nl80211_data *drv = priv;
  1333. struct nl_msg *msg;
  1334. struct wpa_scan_results *res;
  1335. int ret;
  1336. res = os_zalloc(sizeof(*res));
  1337. if (res == NULL)
  1338. return NULL;
  1339. msg = nlmsg_alloc();
  1340. if (!msg)
  1341. goto nla_put_failure;
  1342. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, NLM_F_DUMP,
  1343. NL80211_CMD_GET_SCAN, 0);
  1344. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  1345. ret = send_and_recv_msgs(drv, msg, bss_info_handler, res);
  1346. msg = NULL;
  1347. if (ret == 0) {
  1348. wpa_printf(MSG_DEBUG, "Received scan results (%lu BSSes)",
  1349. (unsigned long) res->num);
  1350. wpa_driver_nl80211_check_bss_status(drv, res);
  1351. return res;
  1352. }
  1353. wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
  1354. "(%s)", ret, strerror(-ret));
  1355. nla_put_failure:
  1356. nlmsg_free(msg);
  1357. wpa_scan_results_free(res);
  1358. return NULL;
  1359. }
  1360. static int wpa_driver_nl80211_set_key(const char *ifname, void *priv,
  1361. wpa_alg alg, const u8 *addr, int key_idx,
  1362. int set_tx,
  1363. const u8 *seq, size_t seq_len,
  1364. const u8 *key, size_t key_len)
  1365. {
  1366. struct wpa_driver_nl80211_data *drv = priv;
  1367. int ifindex = if_nametoindex(ifname);
  1368. struct nl_msg *msg;
  1369. int ret;
  1370. wpa_printf(MSG_DEBUG, "%s: ifindex=%d alg=%d addr=%p key_idx=%d "
  1371. "set_tx=%d seq_len=%lu key_len=%lu",
  1372. __func__, ifindex, alg, addr, key_idx, set_tx,
  1373. (unsigned long) seq_len, (unsigned long) key_len);
  1374. msg = nlmsg_alloc();
  1375. if (!msg)
  1376. return -ENOMEM;
  1377. if (alg == WPA_ALG_NONE) {
  1378. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  1379. 0, NL80211_CMD_DEL_KEY, 0);
  1380. } else {
  1381. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  1382. 0, NL80211_CMD_NEW_KEY, 0);
  1383. NLA_PUT(msg, NL80211_ATTR_KEY_DATA, key_len, key);
  1384. switch (alg) {
  1385. case WPA_ALG_WEP:
  1386. if (key_len == 5)
  1387. NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
  1388. WLAN_CIPHER_SUITE_WEP40);
  1389. else
  1390. NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
  1391. WLAN_CIPHER_SUITE_WEP104);
  1392. break;
  1393. case WPA_ALG_TKIP:
  1394. NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
  1395. WLAN_CIPHER_SUITE_TKIP);
  1396. break;
  1397. case WPA_ALG_CCMP:
  1398. NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
  1399. WLAN_CIPHER_SUITE_CCMP);
  1400. break;
  1401. case WPA_ALG_IGTK:
  1402. NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
  1403. WLAN_CIPHER_SUITE_AES_CMAC);
  1404. break;
  1405. default:
  1406. wpa_printf(MSG_ERROR, "%s: Unsupported encryption "
  1407. "algorithm %d", __func__, alg);
  1408. nlmsg_free(msg);
  1409. return -1;
  1410. }
  1411. }
  1412. if (seq && seq_len)
  1413. NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq);
  1414. if (addr && os_memcmp(addr, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
  1415. {
  1416. wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
  1417. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
  1418. }
  1419. NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
  1420. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
  1421. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  1422. if (ret == -ENOENT && alg == WPA_ALG_NONE)
  1423. ret = 0;
  1424. if (ret)
  1425. wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
  1426. ret, strerror(-ret));
  1427. /*
  1428. * If we failed or don't need to set the default TX key (below),
  1429. * we're done here.
  1430. */
  1431. if (ret || !set_tx || alg == WPA_ALG_NONE)
  1432. return ret;
  1433. #ifdef HOSTAPD /* FIX: is this needed? */
  1434. if (addr)
  1435. return ret;
  1436. #endif /* HOSTAPD */
  1437. msg = nlmsg_alloc();
  1438. if (!msg)
  1439. return -ENOMEM;
  1440. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  1441. 0, NL80211_CMD_SET_KEY, 0);
  1442. NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
  1443. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
  1444. if (alg == WPA_ALG_IGTK)
  1445. NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT_MGMT);
  1446. else
  1447. NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
  1448. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  1449. if (ret == -ENOENT)
  1450. ret = 0;
  1451. if (ret)
  1452. wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
  1453. "err=%d %s)", ret, strerror(-ret));
  1454. return ret;
  1455. nla_put_failure:
  1456. return -ENOBUFS;
  1457. }
  1458. static int nl_add_key(struct nl_msg *msg, wpa_alg alg,
  1459. int key_idx, int defkey,
  1460. const u8 *seq, size_t seq_len,
  1461. const u8 *key, size_t key_len)
  1462. {
  1463. struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
  1464. if (!key_attr)
  1465. return -1;
  1466. if (defkey && alg == WPA_ALG_IGTK)
  1467. NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_MGMT);
  1468. else if (defkey)
  1469. NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
  1470. NLA_PUT_U8(msg, NL80211_KEY_IDX, key_idx);
  1471. switch (alg) {
  1472. case WPA_ALG_WEP:
  1473. if (key_len == 5)
  1474. NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
  1475. WLAN_CIPHER_SUITE_WEP40);
  1476. else
  1477. NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
  1478. WLAN_CIPHER_SUITE_WEP104);
  1479. break;
  1480. case WPA_ALG_TKIP:
  1481. NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_TKIP);
  1482. break;
  1483. case WPA_ALG_CCMP:
  1484. NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_CCMP);
  1485. break;
  1486. case WPA_ALG_IGTK:
  1487. NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
  1488. WLAN_CIPHER_SUITE_AES_CMAC);
  1489. break;
  1490. default:
  1491. wpa_printf(MSG_ERROR, "%s: Unsupported encryption "
  1492. "algorithm %d", __func__, alg);
  1493. return -1;
  1494. }
  1495. if (seq && seq_len)
  1496. NLA_PUT(msg, NL80211_KEY_SEQ, seq_len, seq);
  1497. NLA_PUT(msg, NL80211_KEY_DATA, key_len, key);
  1498. nla_nest_end(msg, key_attr);
  1499. return 0;
  1500. nla_put_failure:
  1501. return -1;
  1502. }
  1503. static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
  1504. struct nl_msg *msg)
  1505. {
  1506. int i, privacy = 0;
  1507. struct nlattr *nl_keys, *nl_key;
  1508. for (i = 0; i < 4; i++) {
  1509. if (!params->wep_key[i])
  1510. continue;
  1511. privacy = 1;
  1512. break;
  1513. }
  1514. if (!privacy)
  1515. return 0;
  1516. NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
  1517. nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
  1518. if (!nl_keys)
  1519. goto nla_put_failure;
  1520. for (i = 0; i < 4; i++) {
  1521. if (!params->wep_key[i])
  1522. continue;
  1523. nl_key = nla_nest_start(msg, i);
  1524. if (!nl_key)
  1525. goto nla_put_failure;
  1526. NLA_PUT(msg, NL80211_KEY_DATA, params->wep_key_len[i],
  1527. params->wep_key[i]);
  1528. if (params->wep_key_len[i] == 5)
  1529. NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
  1530. WLAN_CIPHER_SUITE_WEP40);
  1531. else
  1532. NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
  1533. WLAN_CIPHER_SUITE_WEP104);
  1534. NLA_PUT_U8(msg, NL80211_KEY_IDX, i);
  1535. if (i == params->wep_tx_keyidx)
  1536. NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
  1537. nla_nest_end(msg, nl_key);
  1538. }
  1539. nla_nest_end(msg, nl_keys);
  1540. return 0;
  1541. nla_put_failure:
  1542. return -ENOBUFS;
  1543. }
  1544. static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
  1545. const u8 *addr, int cmd, u16 reason_code)
  1546. {
  1547. int ret = -1;
  1548. struct nl_msg *msg;
  1549. msg = nlmsg_alloc();
  1550. if (!msg)
  1551. return -1;
  1552. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0, cmd, 0);
  1553. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  1554. NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason_code);
  1555. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
  1556. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  1557. msg = NULL;
  1558. if (ret) {
  1559. wpa_printf(MSG_DEBUG, "nl80211: MLME command failed: ret=%d "
  1560. "(%s)", ret, strerror(-ret));
  1561. goto nla_put_failure;
  1562. }
  1563. ret = 0;
  1564. nla_put_failure:
  1565. nlmsg_free(msg);
  1566. return ret;
  1567. }
  1568. static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
  1569. const u8 *addr, int reason_code)
  1570. {
  1571. wpa_printf(MSG_DEBUG, "%s", __func__);
  1572. drv->associated = 0;
  1573. return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DISCONNECT,
  1574. reason_code);
  1575. }
  1576. static int wpa_driver_nl80211_deauthenticate(void *priv, const u8 *addr,
  1577. int reason_code)
  1578. {
  1579. struct wpa_driver_nl80211_data *drv = priv;
  1580. if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
  1581. return wpa_driver_nl80211_disconnect(drv, addr, reason_code);
  1582. wpa_printf(MSG_DEBUG, "%s", __func__);
  1583. drv->associated = 0;
  1584. return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
  1585. reason_code);
  1586. }
  1587. static int wpa_driver_nl80211_disassociate(void *priv, const u8 *addr,
  1588. int reason_code)
  1589. {
  1590. struct wpa_driver_nl80211_data *drv = priv;
  1591. if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
  1592. return wpa_driver_nl80211_disconnect(drv, addr, reason_code);
  1593. wpa_printf(MSG_DEBUG, "%s", __func__);
  1594. drv->associated = 0;
  1595. return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DISASSOCIATE,
  1596. reason_code);
  1597. }
  1598. static int wpa_driver_nl80211_authenticate(
  1599. void *priv, struct wpa_driver_auth_params *params)
  1600. {
  1601. struct wpa_driver_nl80211_data *drv = priv;
  1602. int ret = -1, i;
  1603. struct nl_msg *msg;
  1604. enum nl80211_auth_type type;
  1605. int count = 0;
  1606. drv->associated = 0;
  1607. os_memset(drv->auth_bssid, 0, ETH_ALEN);
  1608. if (wpa_driver_nl80211_set_mode(drv, IEEE80211_MODE_INFRA) < 0)
  1609. return -1;
  1610. retry:
  1611. msg = nlmsg_alloc();
  1612. if (!msg)
  1613. return -1;
  1614. wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
  1615. drv->ifindex);
  1616. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  1617. NL80211_CMD_AUTHENTICATE, 0);
  1618. for (i = 0; i < 4; i++) {
  1619. if (!params->wep_key[i])
  1620. continue;
  1621. wpa_driver_nl80211_set_key(drv->ifname, drv, WPA_ALG_WEP, NULL,
  1622. i,
  1623. i == params->wep_tx_keyidx, NULL, 0,
  1624. params->wep_key[i],
  1625. params->wep_key_len[i]);
  1626. if (params->wep_tx_keyidx != i)
  1627. continue;
  1628. if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
  1629. params->wep_key[i], params->wep_key_len[i])) {
  1630. nlmsg_free(msg);
  1631. return -1;
  1632. }
  1633. }
  1634. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  1635. if (params->bssid) {
  1636. wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
  1637. MAC2STR(params->bssid));
  1638. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
  1639. }
  1640. if (params->freq) {
  1641. wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
  1642. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
  1643. }
  1644. if (params->ssid) {
  1645. wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
  1646. params->ssid, params->ssid_len);
  1647. NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
  1648. params->ssid);
  1649. }
  1650. wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len);
  1651. if (params->ie)
  1652. NLA_PUT(msg, NL80211_ATTR_IE, params->ie_len, params->ie);
  1653. /*
  1654. * TODO: if multiple auth_alg options enabled, try them one by one if
  1655. * the AP rejects authentication due to unknown auth alg
  1656. */
  1657. if (params->auth_alg & AUTH_ALG_OPEN_SYSTEM)
  1658. type = NL80211_AUTHTYPE_OPEN_SYSTEM;
  1659. else if (params->auth_alg & AUTH_ALG_SHARED_KEY)
  1660. type = NL80211_AUTHTYPE_SHARED_KEY;
  1661. else if (params->auth_alg & AUTH_ALG_LEAP)
  1662. type = NL80211_AUTHTYPE_NETWORK_EAP;
  1663. else if (params->auth_alg & AUTH_ALG_FT)
  1664. type = NL80211_AUTHTYPE_FT;
  1665. else
  1666. goto nla_put_failure;
  1667. wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
  1668. NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
  1669. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  1670. msg = NULL;
  1671. if (ret) {
  1672. wpa_printf(MSG_DEBUG, "nl80211: MLME command failed: ret=%d "
  1673. "(%s)", ret, strerror(-ret));
  1674. count++;
  1675. if (ret == -EALREADY && count == 1 && params->bssid) {
  1676. /*
  1677. * mac80211 does not currently accept new
  1678. * authentication if we are already authenticated. As a
  1679. * workaround, force deauthentication and try again.
  1680. */
  1681. wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
  1682. "after forced deauthentication");
  1683. wpa_driver_nl80211_deauthenticate(
  1684. drv, params->bssid,
  1685. WLAN_REASON_PREV_AUTH_NOT_VALID);
  1686. nlmsg_free(msg);
  1687. goto retry;
  1688. }
  1689. goto nla_put_failure;
  1690. }
  1691. ret = 0;
  1692. wpa_printf(MSG_DEBUG, "nl80211: Authentication request send "
  1693. "successfully");
  1694. nla_put_failure:
  1695. nlmsg_free(msg);
  1696. return ret;
  1697. }
  1698. struct phy_info_arg {
  1699. u16 *num_modes;
  1700. struct hostapd_hw_modes *modes;
  1701. };
  1702. static int phy_info_handler(struct nl_msg *msg, void *arg)
  1703. {
  1704. struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
  1705. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  1706. struct phy_info_arg *phy_info = arg;
  1707. struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
  1708. struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
  1709. static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
  1710. [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
  1711. [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
  1712. [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
  1713. [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG },
  1714. [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
  1715. [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
  1716. };
  1717. struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
  1718. static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
  1719. [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
  1720. [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] = { .type = NLA_FLAG },
  1721. };
  1722. struct nlattr *nl_band;
  1723. struct nlattr *nl_freq;
  1724. struct nlattr *nl_rate;
  1725. int rem_band, rem_freq, rem_rate;
  1726. struct hostapd_hw_modes *mode;
  1727. int idx, mode_is_set;
  1728. nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  1729. genlmsg_attrlen(gnlh, 0), NULL);
  1730. if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
  1731. return NL_SKIP;
  1732. nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band) {
  1733. mode = os_realloc(phy_info->modes, (*phy_info->num_modes + 1) * sizeof(*mode));
  1734. if (!mode)
  1735. return NL_SKIP;
  1736. phy_info->modes = mode;
  1737. mode_is_set = 0;
  1738. mode = &phy_info->modes[*(phy_info->num_modes)];
  1739. memset(mode, 0, sizeof(*mode));
  1740. *(phy_info->num_modes) += 1;
  1741. nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
  1742. nla_len(nl_band), NULL);
  1743. if (tb_band[NL80211_BAND_ATTR_HT_CAPA]) {
  1744. mode->ht_capab = nla_get_u16(
  1745. tb_band[NL80211_BAND_ATTR_HT_CAPA]);
  1746. }
  1747. if (tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR]) {
  1748. mode->a_mpdu_params |= nla_get_u8(
  1749. tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR]) &
  1750. 0x03;
  1751. }
  1752. if (tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY]) {
  1753. mode->a_mpdu_params |= nla_get_u8(
  1754. tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY]) <<
  1755. 2;
  1756. }
  1757. if (tb_band[NL80211_BAND_ATTR_HT_MCS_SET] &&
  1758. nla_len(tb_band[NL80211_BAND_ATTR_HT_MCS_SET])) {
  1759. u8 *mcs;
  1760. mcs = nla_data(tb_band[NL80211_BAND_ATTR_HT_MCS_SET]);
  1761. os_memcpy(mode->mcs_set, mcs, 16);
  1762. }
  1763. nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
  1764. nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
  1765. nla_len(nl_freq), freq_policy);
  1766. if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
  1767. continue;
  1768. mode->num_channels++;
  1769. }
  1770. mode->channels = os_zalloc(mode->num_channels * sizeof(struct hostapd_channel_data));
  1771. if (!mode->channels)
  1772. return NL_SKIP;
  1773. idx = 0;
  1774. nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
  1775. nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
  1776. nla_len(nl_freq), freq_policy);
  1777. if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
  1778. continue;
  1779. mode->channels[idx].freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
  1780. mode->channels[idx].flag = 0;
  1781. if (!mode_is_set) {
  1782. /* crude heuristic */
  1783. if (mode->channels[idx].freq < 4000)
  1784. mode->mode = HOSTAPD_MODE_IEEE80211B;
  1785. else
  1786. mode->mode = HOSTAPD_MODE_IEEE80211A;
  1787. mode_is_set = 1;
  1788. }
  1789. /* crude heuristic */
  1790. if (mode->channels[idx].freq < 4000)
  1791. if (mode->channels[idx].freq == 2484)
  1792. mode->channels[idx].chan = 14;
  1793. else
  1794. mode->channels[idx].chan = (mode->channels[idx].freq - 2407) / 5;
  1795. else
  1796. mode->channels[idx].chan = mode->channels[idx].freq/5 - 1000;
  1797. if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
  1798. mode->channels[idx].flag |=
  1799. HOSTAPD_CHAN_DISABLED;
  1800. if (tb_freq[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN])
  1801. mode->channels[idx].flag |=
  1802. HOSTAPD_CHAN_PASSIVE_SCAN;
  1803. if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IBSS])
  1804. mode->channels[idx].flag |=
  1805. HOSTAPD_CHAN_NO_IBSS;
  1806. if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR])
  1807. mode->channels[idx].flag |=
  1808. HOSTAPD_CHAN_RADAR;
  1809. if (tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER] &&
  1810. !tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
  1811. mode->channels[idx].max_tx_power =
  1812. nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER]) / 100;
  1813. idx++;
  1814. }
  1815. nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
  1816. nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
  1817. nla_len(nl_rate), rate_policy);
  1818. if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
  1819. continue;
  1820. mode->num_rates++;
  1821. }
  1822. mode->rates = os_zalloc(mode->num_rates * sizeof(int));
  1823. if (!mode->rates)
  1824. return NL_SKIP;
  1825. idx = 0;
  1826. nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
  1827. nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
  1828. nla_len(nl_rate), rate_policy);
  1829. if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
  1830. continue;
  1831. mode->rates[idx] = nla_get_u32(tb_rate[NL80211_BITRATE_ATTR_RATE]);
  1832. /* crude heuristic */
  1833. if (mode->mode == HOSTAPD_MODE_IEEE80211B &&
  1834. mode->rates[idx] > 200)
  1835. mode->mode = HOSTAPD_MODE_IEEE80211G;
  1836. idx++;
  1837. }
  1838. }
  1839. return NL_SKIP;
  1840. }
  1841. static struct hostapd_hw_modes *
  1842. wpa_driver_nl80211_add_11b(struct hostapd_hw_modes *modes, u16 *num_modes)
  1843. {
  1844. u16 m;
  1845. struct hostapd_hw_modes *mode11g = NULL, *nmodes, *mode;
  1846. int i, mode11g_idx = -1;
  1847. /* If only 802.11g mode is included, use it to construct matching
  1848. * 802.11b mode data. */
  1849. for (m = 0; m < *num_modes; m++) {
  1850. if (modes[m].mode == HOSTAPD_MODE_IEEE80211B)
  1851. return modes; /* 802.11b already included */
  1852. if (modes[m].mode == HOSTAPD_MODE_IEEE80211G)
  1853. mode11g_idx = m;
  1854. }
  1855. if (mode11g_idx < 0)
  1856. return modes; /* 2.4 GHz band not supported at all */
  1857. nmodes = os_realloc(modes, (*num_modes + 1) * sizeof(*nmodes));
  1858. if (nmodes == NULL)
  1859. return modes; /* Could not add 802.11b mode */
  1860. mode = &nmodes[*num_modes];
  1861. os_memset(mode, 0, sizeof(*mode));
  1862. (*num_modes)++;
  1863. modes = nmodes;
  1864. mode->mode = HOSTAPD_MODE_IEEE80211B;
  1865. mode11g = &modes[mode11g_idx];
  1866. mode->num_channels = mode11g->num_channels;
  1867. mode->channels = os_malloc(mode11g->num_channels *
  1868. sizeof(struct hostapd_channel_data));
  1869. if (mode->channels == NULL) {
  1870. (*num_modes)--;
  1871. return modes; /* Could not add 802.11b mode */
  1872. }
  1873. os_memcpy(mode->channels, mode11g->channels,
  1874. mode11g->num_channels * sizeof(struct hostapd_channel_data));
  1875. mode->num_rates = 0;
  1876. mode->rates = os_malloc(4 * sizeof(int));
  1877. if (mode->rates == NULL) {
  1878. os_free(mode->channels);
  1879. (*num_modes)--;
  1880. return modes; /* Could not add 802.11b mode */
  1881. }
  1882. for (i = 0; i < mode11g->num_rates; i++) {
  1883. if (mode11g->rates[i] != 10 && mode11g->rates[i] != 20 &&
  1884. mode11g->rates[i] != 55 && mode11g->rates[i] != 110)
  1885. continue;
  1886. mode->rates[mode->num_rates] = mode11g->rates[i];
  1887. mode->num_rates++;
  1888. if (mode->num_rates == 4)
  1889. break;
  1890. }
  1891. if (mode->num_rates == 0) {
  1892. os_free(mode->channels);
  1893. os_free(mode->rates);
  1894. (*num_modes)--;
  1895. return modes; /* No 802.11b rates */
  1896. }
  1897. wpa_printf(MSG_DEBUG, "nl80211: Added 802.11b mode based on 802.11g "
  1898. "information");
  1899. return modes;
  1900. }
  1901. static struct hostapd_hw_modes *
  1902. wpa_driver_nl80211_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags)
  1903. {
  1904. struct wpa_driver_nl80211_data *drv = priv;
  1905. struct nl_msg *msg;
  1906. struct phy_info_arg result = {
  1907. .num_modes = num_modes,
  1908. .modes = NULL,
  1909. };
  1910. *num_modes = 0;
  1911. *flags = 0;
  1912. msg = nlmsg_alloc();
  1913. if (!msg)
  1914. return NULL;
  1915. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  1916. 0, NL80211_CMD_GET_WIPHY, 0);
  1917. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  1918. if (send_and_recv_msgs(drv, msg, phy_info_handler, &result) == 0)
  1919. return wpa_driver_nl80211_add_11b(result.modes, num_modes);
  1920. nla_put_failure:
  1921. return NULL;
  1922. }
  1923. static int wpa_driver_nl80211_send_frame(struct wpa_driver_nl80211_data *drv,
  1924. const void *data, size_t len,
  1925. int encrypt)
  1926. {
  1927. __u8 rtap_hdr[] = {
  1928. 0x00, 0x00, /* radiotap version */
  1929. 0x0e, 0x00, /* radiotap length */
  1930. 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
  1931. IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */
  1932. 0x00, /* padding */
  1933. 0x00, 0x00, /* RX and TX flags to indicate that */
  1934. 0x00, 0x00, /* this is the injected frame directly */
  1935. };
  1936. struct iovec iov[2] = {
  1937. {
  1938. .iov_base = &rtap_hdr,
  1939. .iov_len = sizeof(rtap_hdr),
  1940. },
  1941. {
  1942. .iov_base = (void *) data,
  1943. .iov_len = len,
  1944. }
  1945. };
  1946. struct msghdr msg = {
  1947. .msg_name = NULL,
  1948. .msg_namelen = 0,
  1949. .msg_iov = iov,
  1950. .msg_iovlen = 2,
  1951. .msg_control = NULL,
  1952. .msg_controllen = 0,
  1953. .msg_flags = 0,
  1954. };
  1955. if (encrypt)
  1956. rtap_hdr[8] |= IEEE80211_RADIOTAP_F_WEP;
  1957. return sendmsg(drv->monitor_sock, &msg, 0);
  1958. }
  1959. static int wpa_driver_nl80211_send_mlme(void *priv, const u8 *data,
  1960. size_t data_len)
  1961. {
  1962. struct wpa_driver_nl80211_data *drv = priv;
  1963. struct ieee80211_mgmt *mgmt;
  1964. int encrypt = 1;
  1965. u16 fc;
  1966. mgmt = (struct ieee80211_mgmt *) data;
  1967. fc = le_to_host16(mgmt->frame_control);
  1968. if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
  1969. WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
  1970. /*
  1971. * Only one of the authentication frame types is encrypted.
  1972. * In order for static WEP encryption to work properly (i.e.,
  1973. * to not encrypt the frame), we need to tell mac80211 about
  1974. * the frames that must not be encrypted.
  1975. */
  1976. u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
  1977. u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
  1978. if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
  1979. encrypt = 0;
  1980. }
  1981. return wpa_driver_nl80211_send_frame(drv, data, data_len, encrypt);
  1982. }
  1983. static int wpa_driver_nl80211_set_beacon(const char *ifname, void *priv,
  1984. const u8 *head, size_t head_len,
  1985. const u8 *tail, size_t tail_len,
  1986. int dtim_period, int beacon_int)
  1987. {
  1988. struct wpa_driver_nl80211_data *drv = priv;
  1989. struct nl_msg *msg;
  1990. u8 cmd = NL80211_CMD_NEW_BEACON;
  1991. int ret;
  1992. int beacon_set;
  1993. int ifindex = if_nametoindex(ifname);
  1994. #ifdef HOSTAPD
  1995. struct i802_bss *bss;
  1996. bss = get_bss(drv, ifindex);
  1997. if (bss == NULL)
  1998. return -ENOENT;
  1999. beacon_set = bss->beacon_set;
  2000. #else /* HOSTAPD */
  2001. beacon_set = drv->beacon_set;
  2002. #endif /* HOSTAPD */
  2003. msg = nlmsg_alloc();
  2004. if (!msg)
  2005. return -ENOMEM;
  2006. wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
  2007. beacon_set);
  2008. if (beacon_set)
  2009. cmd = NL80211_CMD_SET_BEACON;
  2010. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  2011. 0, cmd, 0);
  2012. NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, head_len, head);
  2013. NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL, tail_len, tail);
  2014. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
  2015. NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, beacon_int);
  2016. NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, dtim_period);
  2017. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  2018. if (ret) {
  2019. wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
  2020. ret, strerror(-ret));
  2021. } else {
  2022. #ifdef HOSTAPD
  2023. bss->beacon_set = 1;
  2024. #else /* HOSTAPD */
  2025. drv->beacon_set = 1;
  2026. #endif /* HOSTAPD */
  2027. }
  2028. return ret;
  2029. nla_put_failure:
  2030. return -ENOBUFS;
  2031. }
  2032. static int wpa_driver_nl80211_set_freq(struct wpa_driver_nl80211_data *drv,
  2033. int freq, int ht_enabled,
  2034. int sec_channel_offset)
  2035. {
  2036. struct nl_msg *msg;
  2037. int ret;
  2038. msg = nlmsg_alloc();
  2039. if (!msg)
  2040. return -1;
  2041. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  2042. NL80211_CMD_SET_WIPHY, 0);
  2043. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  2044. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
  2045. if (ht_enabled) {
  2046. switch (sec_channel_offset) {
  2047. case -1:
  2048. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
  2049. NL80211_CHAN_HT40MINUS);
  2050. break;
  2051. case 1:
  2052. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
  2053. NL80211_CHAN_HT40PLUS);
  2054. break;
  2055. default:
  2056. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
  2057. NL80211_CHAN_HT20);
  2058. break;
  2059. }
  2060. }
  2061. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  2062. if (ret == 0)
  2063. return 0;
  2064. wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
  2065. "%d (%s)", freq, ret, strerror(-ret));
  2066. nla_put_failure:
  2067. return -1;
  2068. }
  2069. static int wpa_driver_nl80211_sta_add(const char *ifname, void *priv,
  2070. struct hostapd_sta_add_params *params)
  2071. {
  2072. struct wpa_driver_nl80211_data *drv = priv;
  2073. struct nl_msg *msg;
  2074. int ret = -ENOBUFS;
  2075. msg = nlmsg_alloc();
  2076. if (!msg)
  2077. return -ENOMEM;
  2078. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  2079. 0, NL80211_CMD_NEW_STATION, 0);
  2080. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(ifname));
  2081. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr);
  2082. NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, params->aid);
  2083. NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, params->supp_rates_len,
  2084. params->supp_rates);
  2085. NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
  2086. params->listen_interval);
  2087. if (params->ht_capabilities) {
  2088. NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY,
  2089. sizeof(*params->ht_capabilities),
  2090. params->ht_capabilities);
  2091. }
  2092. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  2093. if (ret)
  2094. wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_NEW_STATION "
  2095. "result: %d (%s)", ret, strerror(-ret));
  2096. if (ret == -EEXIST)
  2097. ret = 0;
  2098. nla_put_failure:
  2099. return ret;
  2100. }
  2101. static int wpa_driver_nl80211_sta_remove(void *priv, const u8 *addr)
  2102. {
  2103. struct wpa_driver_nl80211_data *drv = priv;
  2104. struct nl_msg *msg;
  2105. int ret;
  2106. msg = nlmsg_alloc();
  2107. if (!msg)
  2108. return -ENOMEM;
  2109. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  2110. 0, NL80211_CMD_DEL_STATION, 0);
  2111. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
  2112. if_nametoindex(drv->ifname));
  2113. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
  2114. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  2115. if (ret == -ENOENT)
  2116. return 0;
  2117. return ret;
  2118. nla_put_failure:
  2119. return -ENOBUFS;
  2120. }
  2121. static void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv,
  2122. int ifidx)
  2123. {
  2124. struct nl_msg *msg;
  2125. #ifdef HOSTAPD
  2126. /* stop listening for EAPOL on this interface */
  2127. del_ifidx(drv, ifidx);
  2128. #endif /* HOSTAPD */
  2129. msg = nlmsg_alloc();
  2130. if (!msg)
  2131. goto nla_put_failure;
  2132. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  2133. 0, NL80211_CMD_DEL_INTERFACE, 0);
  2134. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx);
  2135. if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
  2136. return;
  2137. nla_put_failure:
  2138. wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d).\n",
  2139. ifidx);
  2140. }
  2141. static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
  2142. const char *ifname,
  2143. enum nl80211_iftype iftype,
  2144. const u8 *addr)
  2145. {
  2146. struct nl_msg *msg, *flags = NULL;
  2147. int ifidx;
  2148. int ret = -ENOBUFS;
  2149. msg = nlmsg_alloc();
  2150. if (!msg)
  2151. return -1;
  2152. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  2153. 0, NL80211_CMD_NEW_INTERFACE, 0);
  2154. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  2155. NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname);
  2156. NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
  2157. if (iftype == NL80211_IFTYPE_MONITOR) {
  2158. int err;
  2159. flags = nlmsg_alloc();
  2160. if (!flags)
  2161. goto nla_put_failure;
  2162. NLA_PUT_FLAG(flags, NL80211_MNTR_FLAG_COOK_FRAMES);
  2163. err = nla_put_nested(msg, NL80211_ATTR_MNTR_FLAGS, flags);
  2164. nlmsg_free(flags);
  2165. if (err)
  2166. goto nla_put_failure;
  2167. }
  2168. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  2169. if (ret) {
  2170. nla_put_failure:
  2171. wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
  2172. ifname, ret, strerror(-ret));
  2173. return ret;
  2174. }
  2175. ifidx = if_nametoindex(ifname);
  2176. if (ifidx <= 0)
  2177. return -1;
  2178. #ifdef HOSTAPD
  2179. /* start listening for EAPOL on this interface */
  2180. add_ifidx(drv, ifidx);
  2181. if (addr && iftype == NL80211_IFTYPE_AP &&
  2182. set_ifhwaddr(drv, ifname, addr)) {
  2183. nl80211_remove_iface(drv, ifidx);
  2184. return -1;
  2185. }
  2186. #endif /* HOSTAPD */
  2187. return ifidx;
  2188. }
  2189. static int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
  2190. const char *ifname, enum nl80211_iftype iftype,
  2191. const u8 *addr)
  2192. {
  2193. int ret;
  2194. ret = nl80211_create_iface_once(drv, ifname, iftype, addr);
  2195. /* if error occured and interface exists already */
  2196. if (ret == -ENFILE && if_nametoindex(ifname)) {
  2197. wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
  2198. /* Try to remove the interface that was already there. */
  2199. nl80211_remove_iface(drv, if_nametoindex(ifname));
  2200. /* Try to create the interface again */
  2201. ret = nl80211_create_iface_once(drv, ifname, iftype, addr);
  2202. }
  2203. return ret;
  2204. }
  2205. static void handle_tx_callback(void *ctx, u8 *buf, size_t len, int ok)
  2206. {
  2207. struct ieee80211_hdr *hdr;
  2208. u16 fc;
  2209. union wpa_event_data event;
  2210. hdr = (struct ieee80211_hdr *) buf;
  2211. fc = le_to_host16(hdr->frame_control);
  2212. os_memset(&event, 0, sizeof(event));
  2213. event.tx_status.type = WLAN_FC_GET_TYPE(fc);
  2214. event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
  2215. event.tx_status.dst = hdr->addr1;
  2216. event.tx_status.data = buf;
  2217. event.tx_status.data_len = len;
  2218. event.tx_status.ack = ok;
  2219. wpa_supplicant_event(ctx, EVENT_TX_STATUS, &event);
  2220. }
  2221. static void from_unknown_sta(struct wpa_driver_nl80211_data *drv,
  2222. struct ieee80211_hdr *hdr, size_t len)
  2223. {
  2224. union wpa_event_data event;
  2225. os_memset(&event, 0, sizeof(event));
  2226. event.rx_from_unknown.hdr = hdr;
  2227. event.rx_from_unknown.len = len;
  2228. wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
  2229. }
  2230. static void handle_frame(struct wpa_driver_nl80211_data *drv,
  2231. u8 *buf, size_t len,
  2232. struct hostapd_frame_info *hfi)
  2233. {
  2234. struct ieee80211_hdr *hdr;
  2235. u16 fc;
  2236. union wpa_event_data event;
  2237. hdr = (struct ieee80211_hdr *) buf;
  2238. fc = le_to_host16(hdr->frame_control);
  2239. switch (WLAN_FC_GET_TYPE(fc)) {
  2240. case WLAN_FC_TYPE_MGMT:
  2241. os_memset(&event, 0, sizeof(event));
  2242. event.rx_mgmt.frame = buf;
  2243. event.rx_mgmt.frame_len = len;
  2244. event.rx_mgmt.fi = hfi;
  2245. wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
  2246. break;
  2247. case WLAN_FC_TYPE_CTRL:
  2248. /* can only get here with PS-Poll frames */
  2249. wpa_printf(MSG_DEBUG, "CTRL");
  2250. from_unknown_sta(drv, hdr, len);
  2251. break;
  2252. case WLAN_FC_TYPE_DATA:
  2253. from_unknown_sta(drv, hdr, len);
  2254. break;
  2255. }
  2256. }
  2257. static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
  2258. {
  2259. struct wpa_driver_nl80211_data *drv = eloop_ctx;
  2260. int len;
  2261. unsigned char buf[3000];
  2262. struct ieee80211_radiotap_iterator iter;
  2263. int ret;
  2264. struct hostapd_frame_info hfi;
  2265. int injected = 0, failed = 0, rxflags = 0;
  2266. len = recv(sock, buf, sizeof(buf), 0);
  2267. if (len < 0) {
  2268. perror("recv");
  2269. return;
  2270. }
  2271. if (ieee80211_radiotap_iterator_init(&iter, (void*)buf, len)) {
  2272. printf("received invalid radiotap frame\n");
  2273. return;
  2274. }
  2275. memset(&hfi, 0, sizeof(hfi));
  2276. while (1) {
  2277. ret = ieee80211_radiotap_iterator_next(&iter);
  2278. if (ret == -ENOENT)
  2279. break;
  2280. if (ret) {
  2281. printf("received invalid radiotap frame (%d)\n", ret);
  2282. return;
  2283. }
  2284. switch (iter.this_arg_index) {
  2285. case IEEE80211_RADIOTAP_FLAGS:
  2286. if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
  2287. len -= 4;
  2288. break;
  2289. case IEEE80211_RADIOTAP_RX_FLAGS:
  2290. rxflags = 1;
  2291. break;
  2292. case IEEE80211_RADIOTAP_TX_FLAGS:
  2293. injected = 1;
  2294. failed = le_to_host16((*(uint16_t *) iter.this_arg)) &
  2295. IEEE80211_RADIOTAP_F_TX_FAIL;
  2296. break;
  2297. case IEEE80211_RADIOTAP_DATA_RETRIES:
  2298. break;
  2299. case IEEE80211_RADIOTAP_CHANNEL:
  2300. /* TODO convert from freq/flags to channel number
  2301. hfi.channel = XXX;
  2302. */
  2303. break;
  2304. case IEEE80211_RADIOTAP_RATE:
  2305. hfi.datarate = *iter.this_arg * 5;
  2306. break;
  2307. case IEEE80211_RADIOTAP_DB_ANTSIGNAL:
  2308. hfi.ssi_signal = *iter.this_arg;
  2309. break;
  2310. }
  2311. }
  2312. if (rxflags && injected)
  2313. return;
  2314. if (!injected)
  2315. handle_frame(drv, buf + iter.max_length,
  2316. len - iter.max_length, &hfi);
  2317. else
  2318. handle_tx_callback(drv->ctx, buf + iter.max_length,
  2319. len - iter.max_length, !failed);
  2320. }
  2321. /*
  2322. * we post-process the filter code later and rewrite
  2323. * this to the offset to the last instruction
  2324. */
  2325. #define PASS 0xFF
  2326. #define FAIL 0xFE
  2327. static struct sock_filter msock_filter_insns[] = {
  2328. /*
  2329. * do a little-endian load of the radiotap length field
  2330. */
  2331. /* load lower byte into A */
  2332. BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 2),
  2333. /* put it into X (== index register) */
  2334. BPF_STMT(BPF_MISC| BPF_TAX, 0),
  2335. /* load upper byte into A */
  2336. BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 3),
  2337. /* left-shift it by 8 */
  2338. BPF_STMT(BPF_ALU | BPF_LSH | BPF_K, 8),
  2339. /* or with X */
  2340. BPF_STMT(BPF_ALU | BPF_OR | BPF_X, 0),
  2341. /* put result into X */
  2342. BPF_STMT(BPF_MISC| BPF_TAX, 0),
  2343. /*
  2344. * Allow management frames through, this also gives us those
  2345. * management frames that we sent ourselves with status
  2346. */
  2347. /* load the lower byte of the IEEE 802.11 frame control field */
  2348. BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
  2349. /* mask off frame type and version */
  2350. BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xF),
  2351. /* accept frame if it's both 0, fall through otherwise */
  2352. BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, PASS, 0),
  2353. /*
  2354. * TODO: add a bit to radiotap RX flags that indicates
  2355. * that the sending station is not associated, then
  2356. * add a filter here that filters on our DA and that flag
  2357. * to allow us to deauth frames to that bad station.
  2358. *
  2359. * Not a regression -- we didn't do it before either.
  2360. */
  2361. #if 0
  2362. /*
  2363. * drop non-data frames, WDS frames
  2364. */
  2365. /* load the lower byte of the frame control field */
  2366. BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
  2367. /* mask off QoS bit */
  2368. BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0c),
  2369. /* drop non-data frames */
  2370. BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 8, 0, FAIL),
  2371. /* load the upper byte of the frame control field */
  2372. BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
  2373. /* mask off toDS/fromDS */
  2374. BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x03),
  2375. /* drop WDS frames */
  2376. BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 3, FAIL, 0),
  2377. #endif
  2378. /*
  2379. * add header length to index
  2380. */
  2381. /* load the lower byte of the frame control field */
  2382. BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
  2383. /* mask off QoS bit */
  2384. BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x80),
  2385. /* right shift it by 6 to give 0 or 2 */
  2386. BPF_STMT(BPF_ALU | BPF_RSH | BPF_K, 6),
  2387. /* add data frame header length */
  2388. BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 24),
  2389. /* add index, was start of 802.11 header */
  2390. BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
  2391. /* move to index, now start of LL header */
  2392. BPF_STMT(BPF_MISC | BPF_TAX, 0),
  2393. /*
  2394. * Accept empty data frames, we use those for
  2395. * polling activity.
  2396. */
  2397. BPF_STMT(BPF_LD | BPF_W | BPF_LEN, 0),
  2398. BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_X, 0, PASS, 0),
  2399. /*
  2400. * Accept EAPOL frames
  2401. */
  2402. BPF_STMT(BPF_LD | BPF_W | BPF_IND, 0),
  2403. BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xAAAA0300, 0, FAIL),
  2404. BPF_STMT(BPF_LD | BPF_W | BPF_IND, 4),
  2405. BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0000888E, PASS, FAIL),
  2406. /* keep these last two statements or change the code below */
  2407. /* return 0 == "DROP" */
  2408. BPF_STMT(BPF_RET | BPF_K, 0),
  2409. /* return ~0 == "keep all" */
  2410. BPF_STMT(BPF_RET | BPF_K, ~0),
  2411. };
  2412. static struct sock_fprog msock_filter = {
  2413. .len = sizeof(msock_filter_insns)/sizeof(msock_filter_insns[0]),
  2414. .filter = msock_filter_insns,
  2415. };
  2416. static int add_monitor_filter(int s)
  2417. {
  2418. int idx;
  2419. /* rewrite all PASS/FAIL jump offsets */
  2420. for (idx = 0; idx < msock_filter.len; idx++) {
  2421. struct sock_filter *insn = &msock_filter_insns[idx];
  2422. if (BPF_CLASS(insn->code) == BPF_JMP) {
  2423. if (insn->code == (BPF_JMP|BPF_JA)) {
  2424. if (insn->k == PASS)
  2425. insn->k = msock_filter.len - idx - 2;
  2426. else if (insn->k == FAIL)
  2427. insn->k = msock_filter.len - idx - 3;
  2428. }
  2429. if (insn->jt == PASS)
  2430. insn->jt = msock_filter.len - idx - 2;
  2431. else if (insn->jt == FAIL)
  2432. insn->jt = msock_filter.len - idx - 3;
  2433. if (insn->jf == PASS)
  2434. insn->jf = msock_filter.len - idx - 2;
  2435. else if (insn->jf == FAIL)
  2436. insn->jf = msock_filter.len - idx - 3;
  2437. }
  2438. }
  2439. if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER,
  2440. &msock_filter, sizeof(msock_filter))) {
  2441. perror("SO_ATTACH_FILTER");
  2442. return -1;
  2443. }
  2444. return 0;
  2445. }
  2446. static void nl80211_remove_monitor_interface(
  2447. struct wpa_driver_nl80211_data *drv)
  2448. {
  2449. if (drv->monitor_ifidx >= 0) {
  2450. nl80211_remove_iface(drv, drv->monitor_ifidx);
  2451. drv->monitor_ifidx = -1;
  2452. }
  2453. }
  2454. static int
  2455. nl80211_create_monitor_interface(struct wpa_driver_nl80211_data *drv)
  2456. {
  2457. char buf[IFNAMSIZ];
  2458. struct sockaddr_ll ll;
  2459. int optval;
  2460. socklen_t optlen;
  2461. snprintf(buf, IFNAMSIZ, "mon.%s", drv->ifname);
  2462. buf[IFNAMSIZ - 1] = '\0';
  2463. drv->monitor_ifidx =
  2464. nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL);
  2465. if (drv->monitor_ifidx < 0)
  2466. return -1;
  2467. if (hostapd_set_iface_flags(drv, buf, 1))
  2468. goto error;
  2469. memset(&ll, 0, sizeof(ll));
  2470. ll.sll_family = AF_PACKET;
  2471. ll.sll_ifindex = drv->monitor_ifidx;
  2472. drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
  2473. if (drv->monitor_sock < 0) {
  2474. perror("socket[PF_PACKET,SOCK_RAW]");
  2475. goto error;
  2476. }
  2477. if (add_monitor_filter(drv->monitor_sock)) {
  2478. wpa_printf(MSG_INFO, "Failed to set socket filter for monitor "
  2479. "interface; do filtering in user space");
  2480. /* This works, but will cost in performance. */
  2481. }
  2482. if (bind(drv->monitor_sock, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
  2483. perror("monitor socket bind");
  2484. goto error;
  2485. }
  2486. optlen = sizeof(optval);
  2487. optval = 20;
  2488. if (setsockopt
  2489. (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
  2490. perror("Failed to set socket priority");
  2491. goto error;
  2492. }
  2493. if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
  2494. drv, NULL)) {
  2495. printf("Could not register monitor read socket\n");
  2496. goto error;
  2497. }
  2498. return 0;
  2499. error:
  2500. nl80211_remove_monitor_interface(drv);
  2501. return -1;
  2502. }
  2503. static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
  2504. static int wpa_driver_nl80211_hapd_send_eapol(
  2505. void *priv, const u8 *addr, const u8 *data,
  2506. size_t data_len, int encrypt, const u8 *own_addr)
  2507. {
  2508. struct wpa_driver_nl80211_data *drv = priv;
  2509. struct ieee80211_hdr *hdr;
  2510. size_t len;
  2511. u8 *pos;
  2512. int res;
  2513. #if 0 /* FIX */
  2514. int qos = sta->flags & WPA_STA_WMM;
  2515. #else
  2516. int qos = 0;
  2517. #endif
  2518. len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
  2519. data_len;
  2520. hdr = os_zalloc(len);
  2521. if (hdr == NULL) {
  2522. printf("malloc() failed for i802_send_data(len=%lu)\n",
  2523. (unsigned long) len);
  2524. return -1;
  2525. }
  2526. hdr->frame_control =
  2527. IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
  2528. hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
  2529. if (encrypt)
  2530. hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
  2531. #if 0 /* To be enabled if qos determination is added above */
  2532. if (qos) {
  2533. hdr->frame_control |=
  2534. host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
  2535. }
  2536. #endif
  2537. memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
  2538. memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
  2539. memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
  2540. pos = (u8 *) (hdr + 1);
  2541. #if 0 /* To be enabled if qos determination is added above */
  2542. if (qos) {
  2543. /* add an empty QoS header if needed */
  2544. pos[0] = 0;
  2545. pos[1] = 0;
  2546. pos += 2;
  2547. }
  2548. #endif
  2549. memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
  2550. pos += sizeof(rfc1042_header);
  2551. WPA_PUT_BE16(pos, ETH_P_PAE);
  2552. pos += 2;
  2553. memcpy(pos, data, data_len);
  2554. res = wpa_driver_nl80211_send_frame(drv, (u8 *) hdr, len, encrypt);
  2555. if (res < 0) {
  2556. wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
  2557. "failed: %d (%s)",
  2558. (unsigned long) len, errno, strerror(errno));
  2559. }
  2560. os_free(hdr);
  2561. return res;
  2562. }
  2563. static u32 sta_flags_nl80211(int flags)
  2564. {
  2565. u32 f = 0;
  2566. if (flags & WPA_STA_AUTHORIZED)
  2567. f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
  2568. if (flags & WPA_STA_WMM)
  2569. f |= BIT(NL80211_STA_FLAG_WME);
  2570. if (flags & WPA_STA_SHORT_PREAMBLE)
  2571. f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
  2572. if (flags & WPA_STA_MFP)
  2573. f |= BIT(NL80211_STA_FLAG_MFP);
  2574. return f;
  2575. }
  2576. static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
  2577. int total_flags, int flags_or,
  2578. int flags_and)
  2579. {
  2580. struct wpa_driver_nl80211_data *drv = priv;
  2581. struct nl_msg *msg, *flags = NULL;
  2582. struct nl80211_sta_flag_update upd;
  2583. msg = nlmsg_alloc();
  2584. if (!msg)
  2585. return -ENOMEM;
  2586. flags = nlmsg_alloc();
  2587. if (!flags) {
  2588. nlmsg_free(msg);
  2589. return -ENOMEM;
  2590. }
  2591. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  2592. 0, NL80211_CMD_SET_STATION, 0);
  2593. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
  2594. if_nametoindex(drv->ifname));
  2595. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
  2596. /*
  2597. * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
  2598. * can be removed eventually.
  2599. */
  2600. if (total_flags & WPA_STA_AUTHORIZED)
  2601. NLA_PUT_FLAG(flags, NL80211_STA_FLAG_AUTHORIZED);
  2602. if (total_flags & WPA_STA_WMM)
  2603. NLA_PUT_FLAG(flags, NL80211_STA_FLAG_WME);
  2604. if (total_flags & WPA_STA_SHORT_PREAMBLE)
  2605. NLA_PUT_FLAG(flags, NL80211_STA_FLAG_SHORT_PREAMBLE);
  2606. if (total_flags & WPA_STA_MFP)
  2607. NLA_PUT_FLAG(flags, NL80211_STA_FLAG_MFP);
  2608. if (nla_put_nested(msg, NL80211_ATTR_STA_FLAGS, flags))
  2609. goto nla_put_failure;
  2610. os_memset(&upd, 0, sizeof(upd));
  2611. upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
  2612. upd.set = sta_flags_nl80211(flags_or);
  2613. NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
  2614. nlmsg_free(flags);
  2615. return send_and_recv_msgs(drv, msg, NULL, NULL);
  2616. nla_put_failure:
  2617. nlmsg_free(flags);
  2618. return -ENOBUFS;
  2619. }
  2620. static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
  2621. struct wpa_driver_associate_params *params)
  2622. {
  2623. if (wpa_driver_nl80211_set_mode(drv, params->mode) ||
  2624. wpa_driver_nl80211_set_freq(drv, params->freq, 0, 0)) {
  2625. nl80211_remove_monitor_interface(drv);
  2626. return -1;
  2627. }
  2628. /* TODO: setup monitor interface (and add code somewhere to remove this
  2629. * when AP mode is stopped; associate with mode != 2 or drv_deinit) */
  2630. return 0;
  2631. }
  2632. static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv)
  2633. {
  2634. struct nl_msg *msg;
  2635. int ret = -1;
  2636. msg = nlmsg_alloc();
  2637. if (!msg)
  2638. return -1;
  2639. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  2640. NL80211_CMD_LEAVE_IBSS, 0);
  2641. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  2642. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  2643. msg = NULL;
  2644. if (ret) {
  2645. wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
  2646. "(%s)", ret, strerror(-ret));
  2647. goto nla_put_failure;
  2648. }
  2649. ret = 0;
  2650. wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS request sent successfully");
  2651. nla_put_failure:
  2652. nlmsg_free(msg);
  2653. return ret;
  2654. }
  2655. static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
  2656. struct wpa_driver_associate_params *params)
  2657. {
  2658. struct nl_msg *msg;
  2659. int ret = -1;
  2660. int count = 0;
  2661. wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
  2662. if (wpa_driver_nl80211_set_mode(drv, params->mode)) {
  2663. wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
  2664. "IBSS mode");
  2665. return -1;
  2666. }
  2667. retry:
  2668. msg = nlmsg_alloc();
  2669. if (!msg)
  2670. return -1;
  2671. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  2672. NL80211_CMD_JOIN_IBSS, 0);
  2673. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  2674. if (params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
  2675. goto nla_put_failure;
  2676. wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
  2677. params->ssid, params->ssid_len);
  2678. NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
  2679. params->ssid);
  2680. os_memcpy(drv->ssid, params->ssid, params->ssid_len);
  2681. drv->ssid_len = params->ssid_len;
  2682. wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
  2683. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
  2684. ret = nl80211_set_conn_keys(params, msg);
  2685. if (ret)
  2686. goto nla_put_failure;
  2687. if (params->wpa_ie) {
  2688. wpa_hexdump(MSG_DEBUG,
  2689. " * Extra IEs for Beacon/Probe Response frames",
  2690. params->wpa_ie, params->wpa_ie_len);
  2691. NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
  2692. params->wpa_ie);
  2693. }
  2694. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  2695. msg = NULL;
  2696. if (ret) {
  2697. wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
  2698. ret, strerror(-ret));
  2699. count++;
  2700. if (ret == -EALREADY && count == 1) {
  2701. wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
  2702. "forced leave");
  2703. nl80211_leave_ibss(drv);
  2704. nlmsg_free(msg);
  2705. goto retry;
  2706. }
  2707. goto nla_put_failure;
  2708. }
  2709. ret = 0;
  2710. wpa_printf(MSG_DEBUG, "nl80211: Join IBSS request sent successfully");
  2711. nla_put_failure:
  2712. nlmsg_free(msg);
  2713. return ret;
  2714. }
  2715. static int wpa_driver_nl80211_connect(
  2716. struct wpa_driver_nl80211_data *drv,
  2717. struct wpa_driver_associate_params *params)
  2718. {
  2719. struct nl_msg *msg;
  2720. enum nl80211_auth_type type;
  2721. int ret = 0;
  2722. msg = nlmsg_alloc();
  2723. if (!msg)
  2724. return -1;
  2725. wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
  2726. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  2727. NL80211_CMD_CONNECT, 0);
  2728. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  2729. if (params->bssid) {
  2730. wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
  2731. MAC2STR(params->bssid));
  2732. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
  2733. }
  2734. if (params->freq) {
  2735. wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
  2736. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
  2737. }
  2738. if (params->ssid) {
  2739. wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
  2740. params->ssid, params->ssid_len);
  2741. NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
  2742. params->ssid);
  2743. if (params->ssid_len > sizeof(drv->ssid))
  2744. goto nla_put_failure;
  2745. os_memcpy(drv->ssid, params->ssid, params->ssid_len);
  2746. drv->ssid_len = params->ssid_len;
  2747. }
  2748. wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
  2749. if (params->wpa_ie)
  2750. NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
  2751. params->wpa_ie);
  2752. if (params->auth_alg & AUTH_ALG_OPEN_SYSTEM)
  2753. type = NL80211_AUTHTYPE_OPEN_SYSTEM;
  2754. else if (params->auth_alg & AUTH_ALG_SHARED_KEY)
  2755. type = NL80211_AUTHTYPE_SHARED_KEY;
  2756. else if (params->auth_alg & AUTH_ALG_LEAP)
  2757. type = NL80211_AUTHTYPE_NETWORK_EAP;
  2758. else if (params->auth_alg & AUTH_ALG_FT)
  2759. type = NL80211_AUTHTYPE_FT;
  2760. else
  2761. goto nla_put_failure;
  2762. wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
  2763. NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
  2764. if (params->wpa_ie && params->wpa_ie_len) {
  2765. enum nl80211_wpa_versions ver;
  2766. if (params->wpa_ie[0] == WLAN_EID_RSN)
  2767. ver = NL80211_WPA_VERSION_2;
  2768. else
  2769. ver = NL80211_WPA_VERSION_1;
  2770. wpa_printf(MSG_DEBUG, " * WPA Version %d", ver);
  2771. NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
  2772. }
  2773. if (params->pairwise_suite != CIPHER_NONE) {
  2774. int cipher;
  2775. switch (params->pairwise_suite) {
  2776. case CIPHER_WEP40:
  2777. cipher = WLAN_CIPHER_SUITE_WEP40;
  2778. break;
  2779. case CIPHER_WEP104:
  2780. cipher = WLAN_CIPHER_SUITE_WEP104;
  2781. break;
  2782. case CIPHER_CCMP:
  2783. cipher = WLAN_CIPHER_SUITE_CCMP;
  2784. break;
  2785. case CIPHER_TKIP:
  2786. default:
  2787. cipher = WLAN_CIPHER_SUITE_TKIP;
  2788. break;
  2789. }
  2790. NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
  2791. }
  2792. if (params->group_suite != CIPHER_NONE) {
  2793. int cipher;
  2794. switch (params->group_suite) {
  2795. case CIPHER_WEP40:
  2796. cipher = WLAN_CIPHER_SUITE_WEP40;
  2797. break;
  2798. case CIPHER_WEP104:
  2799. cipher = WLAN_CIPHER_SUITE_WEP104;
  2800. break;
  2801. case CIPHER_CCMP:
  2802. cipher = WLAN_CIPHER_SUITE_CCMP;
  2803. break;
  2804. case CIPHER_TKIP:
  2805. default:
  2806. cipher = WLAN_CIPHER_SUITE_TKIP;
  2807. break;
  2808. }
  2809. NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
  2810. }
  2811. if (params->key_mgmt_suite == KEY_MGMT_802_1X ||
  2812. params->key_mgmt_suite == KEY_MGMT_PSK) {
  2813. int mgmt = WLAN_AKM_SUITE_PSK;
  2814. switch (params->key_mgmt_suite) {
  2815. case KEY_MGMT_802_1X:
  2816. mgmt = WLAN_AKM_SUITE_8021X;
  2817. break;
  2818. case KEY_MGMT_PSK:
  2819. default:
  2820. mgmt = WLAN_AKM_SUITE_PSK;
  2821. break;
  2822. }
  2823. NLA_PUT_U32(msg, NL80211_ATTR_AKM_SUITES, mgmt);
  2824. }
  2825. ret = nl80211_set_conn_keys(params, msg);
  2826. if (ret)
  2827. goto nla_put_failure;
  2828. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  2829. msg = NULL;
  2830. if (ret) {
  2831. wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
  2832. "(%s)", ret, strerror(-ret));
  2833. goto nla_put_failure;
  2834. }
  2835. ret = 0;
  2836. wpa_printf(MSG_DEBUG, "nl80211: Connect request send successfully");
  2837. nla_put_failure:
  2838. nlmsg_free(msg);
  2839. return ret;
  2840. }
  2841. static int wpa_driver_nl80211_associate(
  2842. void *priv, struct wpa_driver_associate_params *params)
  2843. {
  2844. struct wpa_driver_nl80211_data *drv = priv;
  2845. int ret = -1;
  2846. struct nl_msg *msg;
  2847. if (params->mode == IEEE80211_MODE_AP)
  2848. return wpa_driver_nl80211_ap(drv, params);
  2849. if (params->mode == IEEE80211_MODE_IBSS)
  2850. return wpa_driver_nl80211_ibss(drv, params);
  2851. if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
  2852. if (wpa_driver_nl80211_set_mode(drv, params->mode) < 0)
  2853. return -1;
  2854. return wpa_driver_nl80211_connect(drv, params);
  2855. }
  2856. drv->associated = 0;
  2857. msg = nlmsg_alloc();
  2858. if (!msg)
  2859. return -1;
  2860. wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
  2861. drv->ifindex);
  2862. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  2863. NL80211_CMD_ASSOCIATE, 0);
  2864. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  2865. if (params->bssid) {
  2866. wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
  2867. MAC2STR(params->bssid));
  2868. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
  2869. }
  2870. if (params->freq) {
  2871. wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
  2872. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
  2873. }
  2874. if (params->ssid) {
  2875. wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
  2876. params->ssid, params->ssid_len);
  2877. NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
  2878. params->ssid);
  2879. if (params->ssid_len > sizeof(drv->ssid))
  2880. goto nla_put_failure;
  2881. os_memcpy(drv->ssid, params->ssid, params->ssid_len);
  2882. drv->ssid_len = params->ssid_len;
  2883. }
  2884. wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
  2885. if (params->wpa_ie)
  2886. NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
  2887. params->wpa_ie);
  2888. #ifdef CONFIG_IEEE80211W
  2889. if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED)
  2890. NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED);
  2891. #endif /* CONFIG_IEEE80211W */
  2892. NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
  2893. if (params->prev_bssid) {
  2894. wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR,
  2895. MAC2STR(params->prev_bssid));
  2896. NLA_PUT(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
  2897. params->prev_bssid);
  2898. }
  2899. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  2900. msg = NULL;
  2901. if (ret) {
  2902. wpa_printf(MSG_DEBUG, "nl80211: MLME command failed: ret=%d "
  2903. "(%s)", ret, strerror(-ret));
  2904. goto nla_put_failure;
  2905. }
  2906. ret = 0;
  2907. wpa_printf(MSG_DEBUG, "nl80211: Association request send "
  2908. "successfully");
  2909. nla_put_failure:
  2910. nlmsg_free(msg);
  2911. return ret;
  2912. }
  2913. static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
  2914. int ifindex, int mode)
  2915. {
  2916. struct nl_msg *msg;
  2917. int ret = -ENOBUFS;
  2918. msg = nlmsg_alloc();
  2919. if (!msg)
  2920. return -ENOMEM;
  2921. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  2922. 0, NL80211_CMD_SET_INTERFACE, 0);
  2923. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
  2924. NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, mode);
  2925. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  2926. if (!ret)
  2927. return 0;
  2928. nla_put_failure:
  2929. wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
  2930. " %d (%s)", ifindex, mode, ret, strerror(-ret));
  2931. return ret;
  2932. }
  2933. static int wpa_driver_nl80211_set_mode(void *priv, int mode)
  2934. {
  2935. struct wpa_driver_nl80211_data *drv = priv;
  2936. int ret = -1;
  2937. int nlmode;
  2938. switch (mode) {
  2939. case 0:
  2940. nlmode = NL80211_IFTYPE_STATION;
  2941. break;
  2942. case 1:
  2943. nlmode = NL80211_IFTYPE_ADHOC;
  2944. break;
  2945. case 2:
  2946. nlmode = NL80211_IFTYPE_AP;
  2947. break;
  2948. default:
  2949. return -1;
  2950. }
  2951. if (nl80211_set_mode(drv, drv->ifindex, nlmode) == 0) {
  2952. drv->nlmode = nlmode;
  2953. ret = 0;
  2954. goto done;
  2955. }
  2956. if (nlmode == drv->nlmode) {
  2957. ret = 0;
  2958. goto done; /* Already in the requested mode */
  2959. }
  2960. /* mac80211 doesn't allow mode changes while the device is up, so
  2961. * take the device down, try to set the mode again, and bring the
  2962. * device back up.
  2963. */
  2964. if (hostapd_set_iface_flags(drv, drv->ifname, 0) == 0) {
  2965. /* Try to set the mode again while the interface is down */
  2966. ret = nl80211_set_mode(drv, drv->ifindex, nlmode);
  2967. if (hostapd_set_iface_flags(drv, drv->ifname, 1))
  2968. ret = -1;
  2969. }
  2970. if (!ret)
  2971. drv->nlmode = nlmode;
  2972. done:
  2973. if (!ret && nlmode == NL80211_IFTYPE_AP) {
  2974. /* Setup additional AP mode functionality if needed */
  2975. if (drv->monitor_ifidx < 0 &&
  2976. nl80211_create_monitor_interface(drv))
  2977. return -1;
  2978. } else if (!ret && nlmode != NL80211_IFTYPE_AP) {
  2979. /* Remove additional AP mode functionality */
  2980. nl80211_remove_monitor_interface(drv);
  2981. }
  2982. return ret;
  2983. }
  2984. static int wpa_driver_nl80211_get_capa(void *priv,
  2985. struct wpa_driver_capa *capa)
  2986. {
  2987. struct wpa_driver_nl80211_data *drv = priv;
  2988. if (!drv->has_capability)
  2989. return -1;
  2990. os_memcpy(capa, &drv->capa, sizeof(*capa));
  2991. return 0;
  2992. }
  2993. static int wpa_driver_nl80211_set_operstate(void *priv, int state)
  2994. {
  2995. struct wpa_driver_nl80211_data *drv = priv;
  2996. wpa_printf(MSG_DEBUG, "%s: operstate %d->%d (%s)",
  2997. __func__, drv->operstate, state, state ? "UP" : "DORMANT");
  2998. drv->operstate = state;
  2999. return netlink_send_oper_ifla(drv->netlink, drv->ifindex, -1,
  3000. state ? IF_OPER_UP : IF_OPER_DORMANT);
  3001. }
  3002. static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
  3003. {
  3004. struct wpa_driver_nl80211_data *drv = priv;
  3005. struct nl_msg *msg;
  3006. struct nl80211_sta_flag_update upd;
  3007. msg = nlmsg_alloc();
  3008. if (!msg)
  3009. return -ENOMEM;
  3010. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  3011. 0, NL80211_CMD_SET_STATION, 0);
  3012. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
  3013. if_nametoindex(drv->ifname));
  3014. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
  3015. os_memset(&upd, 0, sizeof(upd));
  3016. upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
  3017. if (authorized)
  3018. upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
  3019. NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
  3020. return send_and_recv_msgs(drv, msg, NULL, NULL);
  3021. nla_put_failure:
  3022. return -ENOBUFS;
  3023. }
  3024. #ifdef HOSTAPD
  3025. static struct i802_bss * get_bss(struct wpa_driver_nl80211_data *drv,
  3026. int ifindex)
  3027. {
  3028. struct i802_bss *bss = &drv->bss;
  3029. while (bss) {
  3030. if (ifindex == bss->ifindex)
  3031. return bss;
  3032. bss = bss->next;
  3033. }
  3034. wpa_printf(MSG_DEBUG, "nl80211: get_bss(%d) failed", ifindex);
  3035. return NULL;
  3036. }
  3037. static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
  3038. {
  3039. int i;
  3040. int *old;
  3041. wpa_printf(MSG_DEBUG, "nl80211: Add own interface ifindex %d",
  3042. ifidx);
  3043. for (i = 0; i < drv->num_if_indices; i++) {
  3044. if (drv->if_indices[i] == 0) {
  3045. drv->if_indices[i] = ifidx;
  3046. return;
  3047. }
  3048. }
  3049. if (drv->if_indices != drv->default_if_indices)
  3050. old = drv->if_indices;
  3051. else
  3052. old = NULL;
  3053. drv->if_indices = os_realloc(old,
  3054. sizeof(int) * (drv->num_if_indices + 1));
  3055. if (!drv->if_indices) {
  3056. if (!old)
  3057. drv->if_indices = drv->default_if_indices;
  3058. else
  3059. drv->if_indices = old;
  3060. wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
  3061. "interfaces");
  3062. wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
  3063. return;
  3064. }
  3065. drv->if_indices[drv->num_if_indices] = ifidx;
  3066. drv->num_if_indices++;
  3067. }
  3068. static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
  3069. {
  3070. int i;
  3071. for (i = 0; i < drv->num_if_indices; i++) {
  3072. if (drv->if_indices[i] == ifidx) {
  3073. drv->if_indices[i] = 0;
  3074. break;
  3075. }
  3076. }
  3077. }
  3078. static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
  3079. {
  3080. int i;
  3081. for (i = 0; i < drv->num_if_indices; i++)
  3082. if (drv->if_indices[i] == ifidx)
  3083. return 1;
  3084. return 0;
  3085. }
  3086. static inline int min_int(int a, int b)
  3087. {
  3088. if (a < b)
  3089. return a;
  3090. return b;
  3091. }
  3092. static int get_key_handler(struct nl_msg *msg, void *arg)
  3093. {
  3094. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  3095. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  3096. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  3097. genlmsg_attrlen(gnlh, 0), NULL);
  3098. /*
  3099. * TODO: validate the key index and mac address!
  3100. * Otherwise, there's a race condition as soon as
  3101. * the kernel starts sending key notifications.
  3102. */
  3103. if (tb[NL80211_ATTR_KEY_SEQ])
  3104. memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
  3105. min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
  3106. return NL_SKIP;
  3107. }
  3108. static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
  3109. int idx, u8 *seq)
  3110. {
  3111. struct wpa_driver_nl80211_data *drv = priv;
  3112. struct nl_msg *msg;
  3113. msg = nlmsg_alloc();
  3114. if (!msg)
  3115. return -ENOMEM;
  3116. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  3117. 0, NL80211_CMD_GET_KEY, 0);
  3118. if (addr)
  3119. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
  3120. NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
  3121. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
  3122. memset(seq, 0, 6);
  3123. return send_and_recv_msgs(drv, msg, get_key_handler, seq);
  3124. nla_put_failure:
  3125. return -ENOBUFS;
  3126. }
  3127. static int i802_set_rate_sets(void *priv, int *supp_rates, int *basic_rates,
  3128. int mode)
  3129. {
  3130. struct wpa_driver_nl80211_data *drv = priv;
  3131. struct nl_msg *msg;
  3132. u8 rates[NL80211_MAX_SUPP_RATES];
  3133. u8 rates_len = 0;
  3134. int i;
  3135. msg = nlmsg_alloc();
  3136. if (!msg)
  3137. return -ENOMEM;
  3138. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  3139. NL80211_CMD_SET_BSS, 0);
  3140. for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0; i++)
  3141. rates[rates_len++] = basic_rates[i] / 5;
  3142. NLA_PUT(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
  3143. /* TODO: multi-BSS support */
  3144. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->ifname));
  3145. return send_and_recv_msgs(drv, msg, NULL, NULL);
  3146. nla_put_failure:
  3147. return -ENOBUFS;
  3148. }
  3149. /* Set kernel driver on given frequency (MHz) */
  3150. static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
  3151. {
  3152. struct wpa_driver_nl80211_data *drv = priv;
  3153. return wpa_driver_nl80211_set_freq(drv, freq->freq, freq->ht_enabled,
  3154. freq->sec_channel_offset);
  3155. }
  3156. static int i802_set_rts(void *priv, int rts)
  3157. {
  3158. struct wpa_driver_nl80211_data *drv = priv;
  3159. struct nl_msg *msg;
  3160. int ret = -ENOBUFS;
  3161. u32 val;
  3162. msg = nlmsg_alloc();
  3163. if (!msg)
  3164. return -ENOMEM;
  3165. if (rts >= 2347)
  3166. val = (u32) -1;
  3167. else
  3168. val = rts;
  3169. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  3170. 0, NL80211_CMD_SET_WIPHY, 0);
  3171. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  3172. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val);
  3173. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  3174. if (!ret)
  3175. return 0;
  3176. nla_put_failure:
  3177. wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
  3178. "%d (%s)", rts, ret, strerror(-ret));
  3179. return ret;
  3180. }
  3181. static int i802_set_frag(void *priv, int frag)
  3182. {
  3183. struct wpa_driver_nl80211_data *drv = priv;
  3184. struct nl_msg *msg;
  3185. int ret = -ENOBUFS;
  3186. u32 val;
  3187. msg = nlmsg_alloc();
  3188. if (!msg)
  3189. return -ENOMEM;
  3190. if (frag >= 2346)
  3191. val = (u32) -1;
  3192. else
  3193. val = frag;
  3194. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  3195. 0, NL80211_CMD_SET_WIPHY, 0);
  3196. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  3197. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val);
  3198. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  3199. if (!ret)
  3200. return 0;
  3201. nla_put_failure:
  3202. wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
  3203. "%d: %d (%s)", frag, ret, strerror(-ret));
  3204. return ret;
  3205. }
  3206. static int i802_flush(void *priv)
  3207. {
  3208. struct wpa_driver_nl80211_data *drv = priv;
  3209. struct nl_msg *msg;
  3210. msg = nlmsg_alloc();
  3211. if (!msg)
  3212. return -1;
  3213. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  3214. 0, NL80211_CMD_DEL_STATION, 0);
  3215. /*
  3216. * XXX: FIX! this needs to flush all VLANs too
  3217. */
  3218. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
  3219. if_nametoindex(drv->ifname));
  3220. return send_and_recv_msgs(drv, msg, NULL, NULL);
  3221. nla_put_failure:
  3222. return -ENOBUFS;
  3223. }
  3224. static int get_sta_handler(struct nl_msg *msg, void *arg)
  3225. {
  3226. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  3227. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  3228. struct hostap_sta_driver_data *data = arg;
  3229. struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
  3230. static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
  3231. [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
  3232. [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
  3233. [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
  3234. [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
  3235. [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
  3236. };
  3237. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  3238. genlmsg_attrlen(gnlh, 0), NULL);
  3239. /*
  3240. * TODO: validate the interface and mac address!
  3241. * Otherwise, there's a race condition as soon as
  3242. * the kernel starts sending station notifications.
  3243. */
  3244. if (!tb[NL80211_ATTR_STA_INFO]) {
  3245. wpa_printf(MSG_DEBUG, "sta stats missing!");
  3246. return NL_SKIP;
  3247. }
  3248. if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
  3249. tb[NL80211_ATTR_STA_INFO],
  3250. stats_policy)) {
  3251. wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
  3252. return NL_SKIP;
  3253. }
  3254. if (stats[NL80211_STA_INFO_INACTIVE_TIME])
  3255. data->inactive_msec =
  3256. nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
  3257. if (stats[NL80211_STA_INFO_RX_BYTES])
  3258. data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
  3259. if (stats[NL80211_STA_INFO_TX_BYTES])
  3260. data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
  3261. if (stats[NL80211_STA_INFO_RX_PACKETS])
  3262. data->rx_packets =
  3263. nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
  3264. if (stats[NL80211_STA_INFO_TX_PACKETS])
  3265. data->tx_packets =
  3266. nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
  3267. return NL_SKIP;
  3268. }
  3269. static int i802_read_sta_data(void *priv, struct hostap_sta_driver_data *data,
  3270. const u8 *addr)
  3271. {
  3272. struct wpa_driver_nl80211_data *drv = priv;
  3273. struct nl_msg *msg;
  3274. os_memset(data, 0, sizeof(*data));
  3275. msg = nlmsg_alloc();
  3276. if (!msg)
  3277. return -ENOMEM;
  3278. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  3279. 0, NL80211_CMD_GET_STATION, 0);
  3280. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
  3281. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->ifname));
  3282. return send_and_recv_msgs(drv, msg, get_sta_handler, data);
  3283. nla_put_failure:
  3284. return -ENOBUFS;
  3285. }
  3286. static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
  3287. int cw_min, int cw_max, int burst_time)
  3288. {
  3289. struct wpa_driver_nl80211_data *drv = priv;
  3290. struct nl_msg *msg;
  3291. struct nlattr *txq, *params;
  3292. msg = nlmsg_alloc();
  3293. if (!msg)
  3294. return -1;
  3295. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  3296. 0, NL80211_CMD_SET_WIPHY, 0);
  3297. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->ifname));
  3298. txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
  3299. if (!txq)
  3300. goto nla_put_failure;
  3301. /* We are only sending parameters for a single TXQ at a time */
  3302. params = nla_nest_start(msg, 1);
  3303. if (!params)
  3304. goto nla_put_failure;
  3305. NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, queue);
  3306. /* Burst time is configured in units of 0.1 msec and TXOP parameter in
  3307. * 32 usec, so need to convert the value here. */
  3308. NLA_PUT_U16(msg, NL80211_TXQ_ATTR_TXOP, (burst_time * 100 + 16) / 32);
  3309. NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min);
  3310. NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max);
  3311. NLA_PUT_U8(msg, NL80211_TXQ_ATTR_AIFS, aifs);
  3312. nla_nest_end(msg, params);
  3313. nla_nest_end(msg, txq);
  3314. if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
  3315. return 0;
  3316. nla_put_failure:
  3317. return -1;
  3318. }
  3319. static int i802_set_bss(void *priv, int cts, int preamble, int slot)
  3320. {
  3321. struct wpa_driver_nl80211_data *drv = priv;
  3322. struct nl_msg *msg;
  3323. msg = nlmsg_alloc();
  3324. if (!msg)
  3325. return -ENOMEM;
  3326. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  3327. NL80211_CMD_SET_BSS, 0);
  3328. if (cts >= 0)
  3329. NLA_PUT_U8(msg, NL80211_ATTR_BSS_CTS_PROT, cts);
  3330. if (preamble >= 0)
  3331. NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble);
  3332. if (slot >= 0)
  3333. NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot);
  3334. /* TODO: multi-BSS support */
  3335. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->ifname));
  3336. return send_and_recv_msgs(drv, msg, NULL, NULL);
  3337. nla_put_failure:
  3338. return -ENOBUFS;
  3339. }
  3340. static int i802_set_cts_protect(void *priv, int value)
  3341. {
  3342. return i802_set_bss(priv, value, -1, -1);
  3343. }
  3344. static int i802_set_preamble(void *priv, int value)
  3345. {
  3346. return i802_set_bss(priv, -1, value, -1);
  3347. }
  3348. static int i802_set_short_slot_time(void *priv, int value)
  3349. {
  3350. return i802_set_bss(priv, -1, -1, value);
  3351. }
  3352. static int i802_set_sta_vlan(void *priv, const u8 *addr,
  3353. const char *ifname, int vlan_id)
  3354. {
  3355. struct wpa_driver_nl80211_data *drv = priv;
  3356. struct nl_msg *msg;
  3357. msg = nlmsg_alloc();
  3358. if (!msg)
  3359. return -ENOMEM;
  3360. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  3361. 0, NL80211_CMD_SET_STATION, 0);
  3362. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
  3363. if_nametoindex(drv->ifname));
  3364. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
  3365. NLA_PUT_U32(msg, NL80211_ATTR_STA_VLAN,
  3366. if_nametoindex(ifname));
  3367. return send_and_recv_msgs(drv, msg, NULL, NULL);
  3368. nla_put_failure:
  3369. return -ENOBUFS;
  3370. }
  3371. static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
  3372. {
  3373. struct wpa_driver_nl80211_data *drv = eloop_ctx;
  3374. struct sockaddr_ll lladdr;
  3375. unsigned char buf[3000];
  3376. int len;
  3377. socklen_t fromlen = sizeof(lladdr);
  3378. len = recvfrom(sock, buf, sizeof(buf), 0,
  3379. (struct sockaddr *)&lladdr, &fromlen);
  3380. if (len < 0) {
  3381. perror("recv");
  3382. return;
  3383. }
  3384. if (have_ifidx(drv, lladdr.sll_ifindex)) {
  3385. void *ctx;
  3386. ctx = hostapd_sta_get_bss(drv->ctx, lladdr.sll_addr);
  3387. if (!ctx)
  3388. return;
  3389. hostapd_eapol_receive(ctx, lladdr.sll_addr, buf, len);
  3390. }
  3391. }
  3392. static int i802_get_inact_sec(void *priv, const u8 *addr)
  3393. {
  3394. struct hostap_sta_driver_data data;
  3395. int ret;
  3396. data.inactive_msec = (unsigned long) -1;
  3397. ret = i802_read_sta_data(priv, &data, addr);
  3398. if (ret || data.inactive_msec == (unsigned long) -1)
  3399. return -1;
  3400. return data.inactive_msec / 1000;
  3401. }
  3402. static int i802_sta_clear_stats(void *priv, const u8 *addr)
  3403. {
  3404. #if 0
  3405. /* TODO */
  3406. #endif
  3407. return 0;
  3408. }
  3409. static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
  3410. int reason)
  3411. {
  3412. struct wpa_driver_nl80211_data *drv = priv;
  3413. struct ieee80211_mgmt mgmt;
  3414. memset(&mgmt, 0, sizeof(mgmt));
  3415. mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  3416. WLAN_FC_STYPE_DEAUTH);
  3417. memcpy(mgmt.da, addr, ETH_ALEN);
  3418. memcpy(mgmt.sa, own_addr, ETH_ALEN);
  3419. memcpy(mgmt.bssid, own_addr, ETH_ALEN);
  3420. mgmt.u.deauth.reason_code = host_to_le16(reason);
  3421. return wpa_driver_nl80211_send_mlme(drv, (u8 *) &mgmt,
  3422. IEEE80211_HDRLEN +
  3423. sizeof(mgmt.u.deauth));
  3424. }
  3425. static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
  3426. int reason)
  3427. {
  3428. struct wpa_driver_nl80211_data *drv = priv;
  3429. struct ieee80211_mgmt mgmt;
  3430. memset(&mgmt, 0, sizeof(mgmt));
  3431. mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  3432. WLAN_FC_STYPE_DISASSOC);
  3433. memcpy(mgmt.da, addr, ETH_ALEN);
  3434. memcpy(mgmt.sa, own_addr, ETH_ALEN);
  3435. memcpy(mgmt.bssid, own_addr, ETH_ALEN);
  3436. mgmt.u.disassoc.reason_code = host_to_le16(reason);
  3437. return wpa_driver_nl80211_send_mlme(drv, (u8 *) &mgmt,
  3438. IEEE80211_HDRLEN +
  3439. sizeof(mgmt.u.disassoc));
  3440. }
  3441. static void *i802_init(struct hostapd_data *hapd,
  3442. struct wpa_init_params *params)
  3443. {
  3444. struct wpa_driver_nl80211_data *drv;
  3445. size_t i;
  3446. drv = wpa_driver_nl80211_init(hapd, params->ifname);
  3447. if (drv == NULL)
  3448. return NULL;
  3449. drv->bss.ifindex = drv->ifindex;
  3450. drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
  3451. drv->if_indices = drv->default_if_indices;
  3452. for (i = 0; i < params->num_bridge; i++) {
  3453. if (params->bridge[i])
  3454. add_ifidx(drv, if_nametoindex(params->bridge[i]));
  3455. }
  3456. /* start listening for EAPOL on the default AP interface */
  3457. add_ifidx(drv, drv->ifindex);
  3458. if (hostapd_set_iface_flags(drv, drv->ifname, 0))
  3459. goto failed;
  3460. if (params->bssid) {
  3461. if (set_ifhwaddr(drv, drv->ifname, params->bssid))
  3462. goto failed;
  3463. }
  3464. if (wpa_driver_nl80211_set_mode(drv, IEEE80211_MODE_AP)) {
  3465. wpa_printf(MSG_ERROR, "nl80211: Failed to set interface %s "
  3466. "into AP mode", drv->ifname);
  3467. goto failed;
  3468. }
  3469. if (hostapd_set_iface_flags(drv, drv->ifname, 1))
  3470. goto failed;
  3471. drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
  3472. if (drv->eapol_sock < 0) {
  3473. perror("socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE)");
  3474. goto failed;
  3475. }
  3476. if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
  3477. {
  3478. printf("Could not register read socket for eapol\n");
  3479. goto failed;
  3480. }
  3481. if (get_ifhwaddr(drv, drv->ifname, params->own_addr))
  3482. goto failed;
  3483. return drv;
  3484. failed:
  3485. nl80211_remove_monitor_interface(drv);
  3486. if (drv->ioctl_sock >= 0)
  3487. close(drv->ioctl_sock);
  3488. genl_family_put(drv->nl80211);
  3489. nl_cache_free(drv->nl_cache);
  3490. nl_handle_destroy(drv->nl_handle);
  3491. nl_cb_put(drv->nl_cb);
  3492. os_free(drv);
  3493. return NULL;
  3494. }
  3495. static void i802_deinit(void *priv)
  3496. {
  3497. wpa_driver_nl80211_deinit(priv);
  3498. }
  3499. #endif /* HOSTAPD */
  3500. static enum nl80211_iftype wpa_driver_nl80211_if_type(
  3501. enum wpa_driver_if_type type)
  3502. {
  3503. switch (type) {
  3504. case WPA_IF_STATION:
  3505. return NL80211_IFTYPE_STATION;
  3506. case WPA_IF_AP_VLAN:
  3507. return NL80211_IFTYPE_AP_VLAN;
  3508. case WPA_IF_AP_BSS:
  3509. return NL80211_IFTYPE_AP;
  3510. }
  3511. return -1;
  3512. }
  3513. static int wpa_driver_nl80211_if_add(const char *iface, void *priv,
  3514. enum wpa_driver_if_type type,
  3515. const char *ifname, const u8 *addr,
  3516. void *bss_ctx)
  3517. {
  3518. struct wpa_driver_nl80211_data *drv = priv;
  3519. int ifidx;
  3520. #ifdef HOSTAPD
  3521. struct i802_bss *bss = NULL;
  3522. if (type == WPA_IF_AP_BSS) {
  3523. bss = os_zalloc(sizeof(*bss));
  3524. if (bss == NULL)
  3525. return -1;
  3526. }
  3527. #endif /* HOSTAPD */
  3528. ifidx = nl80211_create_iface(drv, ifname,
  3529. wpa_driver_nl80211_if_type(type), addr);
  3530. if (ifidx < 0) {
  3531. #ifdef HOSTAPD
  3532. os_free(bss);
  3533. #endif /* HOSTAPD */
  3534. return -1;
  3535. }
  3536. #ifdef HOSTAPD
  3537. if (type == WPA_IF_AP_BSS) {
  3538. if (hostapd_set_iface_flags(priv, ifname, 1)) {
  3539. nl80211_remove_iface(priv, ifidx);
  3540. os_free(bss);
  3541. return -1;
  3542. }
  3543. bss->ifindex = ifidx;
  3544. bss->next = drv->bss.next;
  3545. drv->bss.next = bss;
  3546. }
  3547. #endif /* HOSTAPD */
  3548. return 0;
  3549. }
  3550. static int wpa_driver_nl80211_if_remove(void *priv,
  3551. enum wpa_driver_if_type type,
  3552. const char *ifname)
  3553. {
  3554. struct wpa_driver_nl80211_data *drv = priv;
  3555. int ifindex = if_nametoindex(ifname);
  3556. nl80211_remove_iface(drv, ifindex);
  3557. #ifdef HOSTAPD
  3558. if (type == WPA_IF_AP_BSS) {
  3559. struct i802_bss *bss, *prev;
  3560. prev = &drv->bss;
  3561. bss = drv->bss.next;
  3562. while (bss) {
  3563. if (ifindex == bss->ifindex) {
  3564. prev->next = bss->next;
  3565. os_free(bss);
  3566. break;
  3567. }
  3568. prev = bss;
  3569. bss = bss->next;
  3570. }
  3571. }
  3572. #endif /* HOSTAPD */
  3573. return 0;
  3574. }
  3575. const struct wpa_driver_ops wpa_driver_nl80211_ops = {
  3576. .name = "nl80211",
  3577. .desc = "Linux nl80211/cfg80211",
  3578. .get_bssid = wpa_driver_nl80211_get_bssid,
  3579. .get_ssid = wpa_driver_nl80211_get_ssid,
  3580. .set_key = wpa_driver_nl80211_set_key,
  3581. .scan2 = wpa_driver_nl80211_scan,
  3582. .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
  3583. .deauthenticate = wpa_driver_nl80211_deauthenticate,
  3584. .disassociate = wpa_driver_nl80211_disassociate,
  3585. .authenticate = wpa_driver_nl80211_authenticate,
  3586. .associate = wpa_driver_nl80211_associate,
  3587. .init = wpa_driver_nl80211_init,
  3588. .deinit = wpa_driver_nl80211_deinit,
  3589. .get_capa = wpa_driver_nl80211_get_capa,
  3590. .set_operstate = wpa_driver_nl80211_set_operstate,
  3591. .set_supp_port = wpa_driver_nl80211_set_supp_port,
  3592. .set_country = wpa_driver_nl80211_set_country,
  3593. .set_beacon = wpa_driver_nl80211_set_beacon,
  3594. .if_add = wpa_driver_nl80211_if_add,
  3595. .if_remove = wpa_driver_nl80211_if_remove,
  3596. .send_mlme = wpa_driver_nl80211_send_mlme,
  3597. .get_hw_feature_data = wpa_driver_nl80211_get_hw_feature_data,
  3598. .sta_add = wpa_driver_nl80211_sta_add,
  3599. .sta_remove = wpa_driver_nl80211_sta_remove,
  3600. .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
  3601. .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
  3602. #ifdef HOSTAPD
  3603. .hapd_init = i802_init,
  3604. .hapd_deinit = i802_deinit,
  3605. .get_seqnum = i802_get_seqnum,
  3606. .flush = i802_flush,
  3607. .read_sta_data = i802_read_sta_data,
  3608. .sta_deauth = i802_sta_deauth,
  3609. .sta_disassoc = i802_sta_disassoc,
  3610. .get_inact_sec = i802_get_inact_sec,
  3611. .sta_clear_stats = i802_sta_clear_stats,
  3612. .set_freq = i802_set_freq,
  3613. .set_rts = i802_set_rts,
  3614. .set_frag = i802_set_frag,
  3615. .set_rate_sets = i802_set_rate_sets,
  3616. .set_cts_protect = i802_set_cts_protect,
  3617. .set_preamble = i802_set_preamble,
  3618. .set_short_slot_time = i802_set_short_slot_time,
  3619. .set_tx_queue_params = i802_set_tx_queue_params,
  3620. .set_sta_vlan = i802_set_sta_vlan,
  3621. #endif /* HOSTAPD */
  3622. };