driver_nl80211.c 116 KB

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