rx.c 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003
  1. /*
  2. * Copyright 2002-2005, Instant802 Networks, Inc.
  3. * Copyright 2005-2006, Devicescape Software, Inc.
  4. * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
  5. * Copyright 2007-2010 Johannes Berg <johannes@sipsolutions.net>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/jiffies.h>
  12. #include <linux/slab.h>
  13. #include <linux/kernel.h>
  14. #include <linux/skbuff.h>
  15. #include <linux/netdevice.h>
  16. #include <linux/etherdevice.h>
  17. #include <linux/rcupdate.h>
  18. #include <net/mac80211.h>
  19. #include <net/ieee80211_radiotap.h>
  20. #include "ieee80211_i.h"
  21. #include "driver-ops.h"
  22. #include "led.h"
  23. #include "mesh.h"
  24. #include "wep.h"
  25. #include "wpa.h"
  26. #include "tkip.h"
  27. #include "wme.h"
  28. /*
  29. * monitor mode reception
  30. *
  31. * This function cleans up the SKB, i.e. it removes all the stuff
  32. * only useful for monitoring.
  33. */
  34. static struct sk_buff *remove_monitor_info(struct ieee80211_local *local,
  35. struct sk_buff *skb)
  36. {
  37. if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS) {
  38. if (likely(skb->len > FCS_LEN))
  39. __pskb_trim(skb, skb->len - FCS_LEN);
  40. else {
  41. /* driver bug */
  42. WARN_ON(1);
  43. dev_kfree_skb(skb);
  44. skb = NULL;
  45. }
  46. }
  47. return skb;
  48. }
  49. static inline int should_drop_frame(struct sk_buff *skb,
  50. int present_fcs_len)
  51. {
  52. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  53. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  54. if (status->flag & (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_FAILED_PLCP_CRC))
  55. return 1;
  56. if (unlikely(skb->len < 16 + present_fcs_len))
  57. return 1;
  58. if (ieee80211_is_ctl(hdr->frame_control) &&
  59. !ieee80211_is_pspoll(hdr->frame_control) &&
  60. !ieee80211_is_back_req(hdr->frame_control))
  61. return 1;
  62. return 0;
  63. }
  64. static int
  65. ieee80211_rx_radiotap_len(struct ieee80211_local *local,
  66. struct ieee80211_rx_status *status)
  67. {
  68. int len;
  69. /* always present fields */
  70. len = sizeof(struct ieee80211_radiotap_header) + 9;
  71. if (status->flag & RX_FLAG_MACTIME_MPDU)
  72. len += 8;
  73. if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM)
  74. len += 1;
  75. if (len & 1) /* padding for RX_FLAGS if necessary */
  76. len++;
  77. if (status->flag & RX_FLAG_HT) /* HT info */
  78. len += 3;
  79. return len;
  80. }
  81. /*
  82. * ieee80211_add_rx_radiotap_header - add radiotap header
  83. *
  84. * add a radiotap header containing all the fields which the hardware provided.
  85. */
  86. static void
  87. ieee80211_add_rx_radiotap_header(struct ieee80211_local *local,
  88. struct sk_buff *skb,
  89. struct ieee80211_rate *rate,
  90. int rtap_len)
  91. {
  92. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  93. struct ieee80211_radiotap_header *rthdr;
  94. unsigned char *pos;
  95. u16 rx_flags = 0;
  96. rthdr = (struct ieee80211_radiotap_header *)skb_push(skb, rtap_len);
  97. memset(rthdr, 0, rtap_len);
  98. /* radiotap header, set always present flags */
  99. rthdr->it_present =
  100. cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
  101. (1 << IEEE80211_RADIOTAP_CHANNEL) |
  102. (1 << IEEE80211_RADIOTAP_ANTENNA) |
  103. (1 << IEEE80211_RADIOTAP_RX_FLAGS));
  104. rthdr->it_len = cpu_to_le16(rtap_len);
  105. pos = (unsigned char *)(rthdr+1);
  106. /* the order of the following fields is important */
  107. /* IEEE80211_RADIOTAP_TSFT */
  108. if (status->flag & RX_FLAG_MACTIME_MPDU) {
  109. put_unaligned_le64(status->mactime, pos);
  110. rthdr->it_present |=
  111. cpu_to_le32(1 << IEEE80211_RADIOTAP_TSFT);
  112. pos += 8;
  113. }
  114. /* IEEE80211_RADIOTAP_FLAGS */
  115. if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS)
  116. *pos |= IEEE80211_RADIOTAP_F_FCS;
  117. if (status->flag & (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_FAILED_PLCP_CRC))
  118. *pos |= IEEE80211_RADIOTAP_F_BADFCS;
  119. if (status->flag & RX_FLAG_SHORTPRE)
  120. *pos |= IEEE80211_RADIOTAP_F_SHORTPRE;
  121. pos++;
  122. /* IEEE80211_RADIOTAP_RATE */
  123. if (status->flag & RX_FLAG_HT) {
  124. /*
  125. * MCS information is a separate field in radiotap,
  126. * added below. The byte here is needed as padding
  127. * for the channel though, so initialise it to 0.
  128. */
  129. *pos = 0;
  130. } else {
  131. rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_RATE);
  132. *pos = rate->bitrate / 5;
  133. }
  134. pos++;
  135. /* IEEE80211_RADIOTAP_CHANNEL */
  136. put_unaligned_le16(status->freq, pos);
  137. pos += 2;
  138. if (status->band == IEEE80211_BAND_5GHZ)
  139. put_unaligned_le16(IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ,
  140. pos);
  141. else if (status->flag & RX_FLAG_HT)
  142. put_unaligned_le16(IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ,
  143. pos);
  144. else if (rate->flags & IEEE80211_RATE_ERP_G)
  145. put_unaligned_le16(IEEE80211_CHAN_OFDM | IEEE80211_CHAN_2GHZ,
  146. pos);
  147. else
  148. put_unaligned_le16(IEEE80211_CHAN_CCK | IEEE80211_CHAN_2GHZ,
  149. pos);
  150. pos += 2;
  151. /* IEEE80211_RADIOTAP_DBM_ANTSIGNAL */
  152. if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM) {
  153. *pos = status->signal;
  154. rthdr->it_present |=
  155. cpu_to_le32(1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL);
  156. pos++;
  157. }
  158. /* IEEE80211_RADIOTAP_LOCK_QUALITY is missing */
  159. /* IEEE80211_RADIOTAP_ANTENNA */
  160. *pos = status->antenna;
  161. pos++;
  162. /* IEEE80211_RADIOTAP_DB_ANTNOISE is not used */
  163. /* IEEE80211_RADIOTAP_RX_FLAGS */
  164. /* ensure 2 byte alignment for the 2 byte field as required */
  165. if ((pos - (u8 *)rthdr) & 1)
  166. pos++;
  167. if (status->flag & RX_FLAG_FAILED_PLCP_CRC)
  168. rx_flags |= IEEE80211_RADIOTAP_F_RX_BADPLCP;
  169. put_unaligned_le16(rx_flags, pos);
  170. pos += 2;
  171. if (status->flag & RX_FLAG_HT) {
  172. rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_MCS);
  173. *pos++ = IEEE80211_RADIOTAP_MCS_HAVE_MCS |
  174. IEEE80211_RADIOTAP_MCS_HAVE_GI |
  175. IEEE80211_RADIOTAP_MCS_HAVE_BW;
  176. *pos = 0;
  177. if (status->flag & RX_FLAG_SHORT_GI)
  178. *pos |= IEEE80211_RADIOTAP_MCS_SGI;
  179. if (status->flag & RX_FLAG_40MHZ)
  180. *pos |= IEEE80211_RADIOTAP_MCS_BW_40;
  181. pos++;
  182. *pos++ = status->rate_idx;
  183. }
  184. }
  185. /*
  186. * This function copies a received frame to all monitor interfaces and
  187. * returns a cleaned-up SKB that no longer includes the FCS nor the
  188. * radiotap header the driver might have added.
  189. */
  190. static struct sk_buff *
  191. ieee80211_rx_monitor(struct ieee80211_local *local, struct sk_buff *origskb,
  192. struct ieee80211_rate *rate)
  193. {
  194. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(origskb);
  195. struct ieee80211_sub_if_data *sdata;
  196. int needed_headroom = 0;
  197. struct sk_buff *skb, *skb2;
  198. struct net_device *prev_dev = NULL;
  199. int present_fcs_len = 0;
  200. /*
  201. * First, we may need to make a copy of the skb because
  202. * (1) we need to modify it for radiotap (if not present), and
  203. * (2) the other RX handlers will modify the skb we got.
  204. *
  205. * We don't need to, of course, if we aren't going to return
  206. * the SKB because it has a bad FCS/PLCP checksum.
  207. */
  208. /* room for the radiotap header based on driver features */
  209. needed_headroom = ieee80211_rx_radiotap_len(local, status);
  210. if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS)
  211. present_fcs_len = FCS_LEN;
  212. /* make sure hdr->frame_control is on the linear part */
  213. if (!pskb_may_pull(origskb, 2)) {
  214. dev_kfree_skb(origskb);
  215. return NULL;
  216. }
  217. if (!local->monitors) {
  218. if (should_drop_frame(origskb, present_fcs_len)) {
  219. dev_kfree_skb(origskb);
  220. return NULL;
  221. }
  222. return remove_monitor_info(local, origskb);
  223. }
  224. if (should_drop_frame(origskb, present_fcs_len)) {
  225. /* only need to expand headroom if necessary */
  226. skb = origskb;
  227. origskb = NULL;
  228. /*
  229. * This shouldn't trigger often because most devices have an
  230. * RX header they pull before we get here, and that should
  231. * be big enough for our radiotap information. We should
  232. * probably export the length to drivers so that we can have
  233. * them allocate enough headroom to start with.
  234. */
  235. if (skb_headroom(skb) < needed_headroom &&
  236. pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC)) {
  237. dev_kfree_skb(skb);
  238. return NULL;
  239. }
  240. } else {
  241. /*
  242. * Need to make a copy and possibly remove radiotap header
  243. * and FCS from the original.
  244. */
  245. skb = skb_copy_expand(origskb, needed_headroom, 0, GFP_ATOMIC);
  246. origskb = remove_monitor_info(local, origskb);
  247. if (!skb)
  248. return origskb;
  249. }
  250. /* prepend radiotap information */
  251. ieee80211_add_rx_radiotap_header(local, skb, rate, needed_headroom);
  252. skb_reset_mac_header(skb);
  253. skb->ip_summed = CHECKSUM_UNNECESSARY;
  254. skb->pkt_type = PACKET_OTHERHOST;
  255. skb->protocol = htons(ETH_P_802_2);
  256. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  257. if (sdata->vif.type != NL80211_IFTYPE_MONITOR)
  258. continue;
  259. if (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES)
  260. continue;
  261. if (!ieee80211_sdata_running(sdata))
  262. continue;
  263. if (prev_dev) {
  264. skb2 = skb_clone(skb, GFP_ATOMIC);
  265. if (skb2) {
  266. skb2->dev = prev_dev;
  267. netif_receive_skb(skb2);
  268. }
  269. }
  270. prev_dev = sdata->dev;
  271. sdata->dev->stats.rx_packets++;
  272. sdata->dev->stats.rx_bytes += skb->len;
  273. }
  274. if (prev_dev) {
  275. skb->dev = prev_dev;
  276. netif_receive_skb(skb);
  277. } else
  278. dev_kfree_skb(skb);
  279. return origskb;
  280. }
  281. static void ieee80211_parse_qos(struct ieee80211_rx_data *rx)
  282. {
  283. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  284. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  285. int tid, seqno_idx, security_idx;
  286. /* does the frame have a qos control field? */
  287. if (ieee80211_is_data_qos(hdr->frame_control)) {
  288. u8 *qc = ieee80211_get_qos_ctl(hdr);
  289. /* frame has qos control */
  290. tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
  291. if (*qc & IEEE80211_QOS_CTL_A_MSDU_PRESENT)
  292. status->rx_flags |= IEEE80211_RX_AMSDU;
  293. seqno_idx = tid;
  294. security_idx = tid;
  295. } else {
  296. /*
  297. * IEEE 802.11-2007, 7.1.3.4.1 ("Sequence Number field"):
  298. *
  299. * Sequence numbers for management frames, QoS data
  300. * frames with a broadcast/multicast address in the
  301. * Address 1 field, and all non-QoS data frames sent
  302. * by QoS STAs are assigned using an additional single
  303. * modulo-4096 counter, [...]
  304. *
  305. * We also use that counter for non-QoS STAs.
  306. */
  307. seqno_idx = NUM_RX_DATA_QUEUES;
  308. security_idx = 0;
  309. if (ieee80211_is_mgmt(hdr->frame_control))
  310. security_idx = NUM_RX_DATA_QUEUES;
  311. tid = 0;
  312. }
  313. rx->seqno_idx = seqno_idx;
  314. rx->security_idx = security_idx;
  315. /* Set skb->priority to 1d tag if highest order bit of TID is not set.
  316. * For now, set skb->priority to 0 for other cases. */
  317. rx->skb->priority = (tid > 7) ? 0 : tid;
  318. }
  319. /**
  320. * DOC: Packet alignment
  321. *
  322. * Drivers always need to pass packets that are aligned to two-byte boundaries
  323. * to the stack.
  324. *
  325. * Additionally, should, if possible, align the payload data in a way that
  326. * guarantees that the contained IP header is aligned to a four-byte
  327. * boundary. In the case of regular frames, this simply means aligning the
  328. * payload to a four-byte boundary (because either the IP header is directly
  329. * contained, or IV/RFC1042 headers that have a length divisible by four are
  330. * in front of it). If the payload data is not properly aligned and the
  331. * architecture doesn't support efficient unaligned operations, mac80211
  332. * will align the data.
  333. *
  334. * With A-MSDU frames, however, the payload data address must yield two modulo
  335. * four because there are 14-byte 802.3 headers within the A-MSDU frames that
  336. * push the IP header further back to a multiple of four again. Thankfully, the
  337. * specs were sane enough this time around to require padding each A-MSDU
  338. * subframe to a length that is a multiple of four.
  339. *
  340. * Padding like Atheros hardware adds which is between the 802.11 header and
  341. * the payload is not supported, the driver is required to move the 802.11
  342. * header to be directly in front of the payload in that case.
  343. */
  344. static void ieee80211_verify_alignment(struct ieee80211_rx_data *rx)
  345. {
  346. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  347. WARN_ONCE((unsigned long)rx->skb->data & 1,
  348. "unaligned packet at 0x%p\n", rx->skb->data);
  349. #endif
  350. }
  351. /* rx handlers */
  352. static ieee80211_rx_result debug_noinline
  353. ieee80211_rx_h_passive_scan(struct ieee80211_rx_data *rx)
  354. {
  355. struct ieee80211_local *local = rx->local;
  356. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  357. struct sk_buff *skb = rx->skb;
  358. if (likely(!(status->rx_flags & IEEE80211_RX_IN_SCAN) &&
  359. !local->sched_scanning))
  360. return RX_CONTINUE;
  361. if (test_bit(SCAN_HW_SCANNING, &local->scanning) ||
  362. test_bit(SCAN_SW_SCANNING, &local->scanning) ||
  363. local->sched_scanning)
  364. return ieee80211_scan_rx(rx->sdata, skb);
  365. /* scanning finished during invoking of handlers */
  366. I802_DEBUG_INC(local->rx_handlers_drop_passive_scan);
  367. return RX_DROP_UNUSABLE;
  368. }
  369. static int ieee80211_is_unicast_robust_mgmt_frame(struct sk_buff *skb)
  370. {
  371. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  372. if (skb->len < 24 || is_multicast_ether_addr(hdr->addr1))
  373. return 0;
  374. return ieee80211_is_robust_mgmt_frame(hdr);
  375. }
  376. static int ieee80211_is_multicast_robust_mgmt_frame(struct sk_buff *skb)
  377. {
  378. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  379. if (skb->len < 24 || !is_multicast_ether_addr(hdr->addr1))
  380. return 0;
  381. return ieee80211_is_robust_mgmt_frame(hdr);
  382. }
  383. /* Get the BIP key index from MMIE; return -1 if this is not a BIP frame */
  384. static int ieee80211_get_mmie_keyidx(struct sk_buff *skb)
  385. {
  386. struct ieee80211_mgmt *hdr = (struct ieee80211_mgmt *) skb->data;
  387. struct ieee80211_mmie *mmie;
  388. if (skb->len < 24 + sizeof(*mmie) ||
  389. !is_multicast_ether_addr(hdr->da))
  390. return -1;
  391. if (!ieee80211_is_robust_mgmt_frame((struct ieee80211_hdr *) hdr))
  392. return -1; /* not a robust management frame */
  393. mmie = (struct ieee80211_mmie *)
  394. (skb->data + skb->len - sizeof(*mmie));
  395. if (mmie->element_id != WLAN_EID_MMIE ||
  396. mmie->length != sizeof(*mmie) - 2)
  397. return -1;
  398. return le16_to_cpu(mmie->key_id);
  399. }
  400. static ieee80211_rx_result
  401. ieee80211_rx_mesh_check(struct ieee80211_rx_data *rx)
  402. {
  403. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  404. char *dev_addr = rx->sdata->vif.addr;
  405. if (ieee80211_is_data(hdr->frame_control)) {
  406. if (is_multicast_ether_addr(hdr->addr1)) {
  407. if (ieee80211_has_tods(hdr->frame_control) ||
  408. !ieee80211_has_fromds(hdr->frame_control))
  409. return RX_DROP_MONITOR;
  410. if (memcmp(hdr->addr3, dev_addr, ETH_ALEN) == 0)
  411. return RX_DROP_MONITOR;
  412. } else {
  413. if (!ieee80211_has_a4(hdr->frame_control))
  414. return RX_DROP_MONITOR;
  415. if (memcmp(hdr->addr4, dev_addr, ETH_ALEN) == 0)
  416. return RX_DROP_MONITOR;
  417. }
  418. }
  419. /* If there is not an established peer link and this is not a peer link
  420. * establisment frame, beacon or probe, drop the frame.
  421. */
  422. if (!rx->sta || sta_plink_state(rx->sta) != NL80211_PLINK_ESTAB) {
  423. struct ieee80211_mgmt *mgmt;
  424. if (!ieee80211_is_mgmt(hdr->frame_control))
  425. return RX_DROP_MONITOR;
  426. if (ieee80211_is_action(hdr->frame_control)) {
  427. u8 category;
  428. mgmt = (struct ieee80211_mgmt *)hdr;
  429. category = mgmt->u.action.category;
  430. if (category != WLAN_CATEGORY_MESH_ACTION &&
  431. category != WLAN_CATEGORY_SELF_PROTECTED)
  432. return RX_DROP_MONITOR;
  433. return RX_CONTINUE;
  434. }
  435. if (ieee80211_is_probe_req(hdr->frame_control) ||
  436. ieee80211_is_probe_resp(hdr->frame_control) ||
  437. ieee80211_is_beacon(hdr->frame_control) ||
  438. ieee80211_is_auth(hdr->frame_control))
  439. return RX_CONTINUE;
  440. return RX_DROP_MONITOR;
  441. }
  442. return RX_CONTINUE;
  443. }
  444. #define SEQ_MODULO 0x1000
  445. #define SEQ_MASK 0xfff
  446. static inline int seq_less(u16 sq1, u16 sq2)
  447. {
  448. return ((sq1 - sq2) & SEQ_MASK) > (SEQ_MODULO >> 1);
  449. }
  450. static inline u16 seq_inc(u16 sq)
  451. {
  452. return (sq + 1) & SEQ_MASK;
  453. }
  454. static inline u16 seq_sub(u16 sq1, u16 sq2)
  455. {
  456. return (sq1 - sq2) & SEQ_MASK;
  457. }
  458. static void ieee80211_release_reorder_frame(struct ieee80211_hw *hw,
  459. struct tid_ampdu_rx *tid_agg_rx,
  460. int index)
  461. {
  462. struct ieee80211_local *local = hw_to_local(hw);
  463. struct sk_buff *skb = tid_agg_rx->reorder_buf[index];
  464. struct ieee80211_rx_status *status;
  465. lockdep_assert_held(&tid_agg_rx->reorder_lock);
  466. if (!skb)
  467. goto no_frame;
  468. /* release the frame from the reorder ring buffer */
  469. tid_agg_rx->stored_mpdu_num--;
  470. tid_agg_rx->reorder_buf[index] = NULL;
  471. status = IEEE80211_SKB_RXCB(skb);
  472. status->rx_flags |= IEEE80211_RX_DEFERRED_RELEASE;
  473. skb_queue_tail(&local->rx_skb_queue, skb);
  474. no_frame:
  475. tid_agg_rx->head_seq_num = seq_inc(tid_agg_rx->head_seq_num);
  476. }
  477. static void ieee80211_release_reorder_frames(struct ieee80211_hw *hw,
  478. struct tid_ampdu_rx *tid_agg_rx,
  479. u16 head_seq_num)
  480. {
  481. int index;
  482. lockdep_assert_held(&tid_agg_rx->reorder_lock);
  483. while (seq_less(tid_agg_rx->head_seq_num, head_seq_num)) {
  484. index = seq_sub(tid_agg_rx->head_seq_num, tid_agg_rx->ssn) %
  485. tid_agg_rx->buf_size;
  486. ieee80211_release_reorder_frame(hw, tid_agg_rx, index);
  487. }
  488. }
  489. /*
  490. * Timeout (in jiffies) for skb's that are waiting in the RX reorder buffer. If
  491. * the skb was added to the buffer longer than this time ago, the earlier
  492. * frames that have not yet been received are assumed to be lost and the skb
  493. * can be released for processing. This may also release other skb's from the
  494. * reorder buffer if there are no additional gaps between the frames.
  495. *
  496. * Callers must hold tid_agg_rx->reorder_lock.
  497. */
  498. #define HT_RX_REORDER_BUF_TIMEOUT (HZ / 10)
  499. static void ieee80211_sta_reorder_release(struct ieee80211_hw *hw,
  500. struct tid_ampdu_rx *tid_agg_rx)
  501. {
  502. int index, j;
  503. lockdep_assert_held(&tid_agg_rx->reorder_lock);
  504. /* release the buffer until next missing frame */
  505. index = seq_sub(tid_agg_rx->head_seq_num, tid_agg_rx->ssn) %
  506. tid_agg_rx->buf_size;
  507. if (!tid_agg_rx->reorder_buf[index] &&
  508. tid_agg_rx->stored_mpdu_num > 1) {
  509. /*
  510. * No buffers ready to be released, but check whether any
  511. * frames in the reorder buffer have timed out.
  512. */
  513. int skipped = 1;
  514. for (j = (index + 1) % tid_agg_rx->buf_size; j != index;
  515. j = (j + 1) % tid_agg_rx->buf_size) {
  516. if (!tid_agg_rx->reorder_buf[j]) {
  517. skipped++;
  518. continue;
  519. }
  520. if (skipped &&
  521. !time_after(jiffies, tid_agg_rx->reorder_time[j] +
  522. HT_RX_REORDER_BUF_TIMEOUT))
  523. goto set_release_timer;
  524. #ifdef CONFIG_MAC80211_HT_DEBUG
  525. if (net_ratelimit())
  526. wiphy_debug(hw->wiphy,
  527. "release an RX reorder frame due to timeout on earlier frames\n");
  528. #endif
  529. ieee80211_release_reorder_frame(hw, tid_agg_rx, j);
  530. /*
  531. * Increment the head seq# also for the skipped slots.
  532. */
  533. tid_agg_rx->head_seq_num =
  534. (tid_agg_rx->head_seq_num + skipped) & SEQ_MASK;
  535. skipped = 0;
  536. }
  537. } else while (tid_agg_rx->reorder_buf[index]) {
  538. ieee80211_release_reorder_frame(hw, tid_agg_rx, index);
  539. index = seq_sub(tid_agg_rx->head_seq_num, tid_agg_rx->ssn) %
  540. tid_agg_rx->buf_size;
  541. }
  542. if (tid_agg_rx->stored_mpdu_num) {
  543. j = index = seq_sub(tid_agg_rx->head_seq_num,
  544. tid_agg_rx->ssn) % tid_agg_rx->buf_size;
  545. for (; j != (index - 1) % tid_agg_rx->buf_size;
  546. j = (j + 1) % tid_agg_rx->buf_size) {
  547. if (tid_agg_rx->reorder_buf[j])
  548. break;
  549. }
  550. set_release_timer:
  551. mod_timer(&tid_agg_rx->reorder_timer,
  552. tid_agg_rx->reorder_time[j] + 1 +
  553. HT_RX_REORDER_BUF_TIMEOUT);
  554. } else {
  555. del_timer(&tid_agg_rx->reorder_timer);
  556. }
  557. }
  558. /*
  559. * As this function belongs to the RX path it must be under
  560. * rcu_read_lock protection. It returns false if the frame
  561. * can be processed immediately, true if it was consumed.
  562. */
  563. static bool ieee80211_sta_manage_reorder_buf(struct ieee80211_hw *hw,
  564. struct tid_ampdu_rx *tid_agg_rx,
  565. struct sk_buff *skb)
  566. {
  567. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  568. u16 sc = le16_to_cpu(hdr->seq_ctrl);
  569. u16 mpdu_seq_num = (sc & IEEE80211_SCTL_SEQ) >> 4;
  570. u16 head_seq_num, buf_size;
  571. int index;
  572. bool ret = true;
  573. spin_lock(&tid_agg_rx->reorder_lock);
  574. buf_size = tid_agg_rx->buf_size;
  575. head_seq_num = tid_agg_rx->head_seq_num;
  576. /* frame with out of date sequence number */
  577. if (seq_less(mpdu_seq_num, head_seq_num)) {
  578. dev_kfree_skb(skb);
  579. goto out;
  580. }
  581. /*
  582. * If frame the sequence number exceeds our buffering window
  583. * size release some previous frames to make room for this one.
  584. */
  585. if (!seq_less(mpdu_seq_num, head_seq_num + buf_size)) {
  586. head_seq_num = seq_inc(seq_sub(mpdu_seq_num, buf_size));
  587. /* release stored frames up to new head to stack */
  588. ieee80211_release_reorder_frames(hw, tid_agg_rx, head_seq_num);
  589. }
  590. /* Now the new frame is always in the range of the reordering buffer */
  591. index = seq_sub(mpdu_seq_num, tid_agg_rx->ssn) % tid_agg_rx->buf_size;
  592. /* check if we already stored this frame */
  593. if (tid_agg_rx->reorder_buf[index]) {
  594. dev_kfree_skb(skb);
  595. goto out;
  596. }
  597. /*
  598. * If the current MPDU is in the right order and nothing else
  599. * is stored we can process it directly, no need to buffer it.
  600. * If it is first but there's something stored, we may be able
  601. * to release frames after this one.
  602. */
  603. if (mpdu_seq_num == tid_agg_rx->head_seq_num &&
  604. tid_agg_rx->stored_mpdu_num == 0) {
  605. tid_agg_rx->head_seq_num = seq_inc(tid_agg_rx->head_seq_num);
  606. ret = false;
  607. goto out;
  608. }
  609. /* put the frame in the reordering buffer */
  610. tid_agg_rx->reorder_buf[index] = skb;
  611. tid_agg_rx->reorder_time[index] = jiffies;
  612. tid_agg_rx->stored_mpdu_num++;
  613. ieee80211_sta_reorder_release(hw, tid_agg_rx);
  614. out:
  615. spin_unlock(&tid_agg_rx->reorder_lock);
  616. return ret;
  617. }
  618. /*
  619. * Reorder MPDUs from A-MPDUs, keeping them on a buffer. Returns
  620. * true if the MPDU was buffered, false if it should be processed.
  621. */
  622. static void ieee80211_rx_reorder_ampdu(struct ieee80211_rx_data *rx)
  623. {
  624. struct sk_buff *skb = rx->skb;
  625. struct ieee80211_local *local = rx->local;
  626. struct ieee80211_hw *hw = &local->hw;
  627. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  628. struct sta_info *sta = rx->sta;
  629. struct tid_ampdu_rx *tid_agg_rx;
  630. u16 sc;
  631. int tid;
  632. if (!ieee80211_is_data_qos(hdr->frame_control))
  633. goto dont_reorder;
  634. /*
  635. * filter the QoS data rx stream according to
  636. * STA/TID and check if this STA/TID is on aggregation
  637. */
  638. if (!sta)
  639. goto dont_reorder;
  640. tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
  641. tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
  642. if (!tid_agg_rx)
  643. goto dont_reorder;
  644. /* qos null data frames are excluded */
  645. if (unlikely(hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_NULLFUNC)))
  646. goto dont_reorder;
  647. /* new, potentially un-ordered, ampdu frame - process it */
  648. /* reset session timer */
  649. if (tid_agg_rx->timeout)
  650. mod_timer(&tid_agg_rx->session_timer,
  651. TU_TO_EXP_TIME(tid_agg_rx->timeout));
  652. /* if this mpdu is fragmented - terminate rx aggregation session */
  653. sc = le16_to_cpu(hdr->seq_ctrl);
  654. if (sc & IEEE80211_SCTL_FRAG) {
  655. skb->pkt_type = IEEE80211_SDATA_QUEUE_TYPE_FRAME;
  656. skb_queue_tail(&rx->sdata->skb_queue, skb);
  657. ieee80211_queue_work(&local->hw, &rx->sdata->work);
  658. return;
  659. }
  660. /*
  661. * No locking needed -- we will only ever process one
  662. * RX packet at a time, and thus own tid_agg_rx. All
  663. * other code manipulating it needs to (and does) make
  664. * sure that we cannot get to it any more before doing
  665. * anything with it.
  666. */
  667. if (ieee80211_sta_manage_reorder_buf(hw, tid_agg_rx, skb))
  668. return;
  669. dont_reorder:
  670. skb_queue_tail(&local->rx_skb_queue, skb);
  671. }
  672. static ieee80211_rx_result debug_noinline
  673. ieee80211_rx_h_check(struct ieee80211_rx_data *rx)
  674. {
  675. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  676. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  677. /* Drop duplicate 802.11 retransmissions (IEEE 802.11 Chap. 9.2.9) */
  678. if (rx->sta && !is_multicast_ether_addr(hdr->addr1)) {
  679. if (unlikely(ieee80211_has_retry(hdr->frame_control) &&
  680. rx->sta->last_seq_ctrl[rx->seqno_idx] ==
  681. hdr->seq_ctrl)) {
  682. if (status->rx_flags & IEEE80211_RX_RA_MATCH) {
  683. rx->local->dot11FrameDuplicateCount++;
  684. rx->sta->num_duplicates++;
  685. }
  686. return RX_DROP_UNUSABLE;
  687. } else
  688. rx->sta->last_seq_ctrl[rx->seqno_idx] = hdr->seq_ctrl;
  689. }
  690. if (unlikely(rx->skb->len < 16)) {
  691. I802_DEBUG_INC(rx->local->rx_handlers_drop_short);
  692. return RX_DROP_MONITOR;
  693. }
  694. /* Drop disallowed frame classes based on STA auth/assoc state;
  695. * IEEE 802.11, Chap 5.5.
  696. *
  697. * mac80211 filters only based on association state, i.e. it drops
  698. * Class 3 frames from not associated stations. hostapd sends
  699. * deauth/disassoc frames when needed. In addition, hostapd is
  700. * responsible for filtering on both auth and assoc states.
  701. */
  702. if (ieee80211_vif_is_mesh(&rx->sdata->vif))
  703. return ieee80211_rx_mesh_check(rx);
  704. if (unlikely((ieee80211_is_data(hdr->frame_control) ||
  705. ieee80211_is_pspoll(hdr->frame_control)) &&
  706. rx->sdata->vif.type != NL80211_IFTYPE_ADHOC &&
  707. rx->sdata->vif.type != NL80211_IFTYPE_WDS &&
  708. (!rx->sta || !test_sta_flags(rx->sta, WLAN_STA_ASSOC)))) {
  709. if (rx->sta && rx->sta->dummy &&
  710. ieee80211_is_data_present(hdr->frame_control)) {
  711. u16 ethertype;
  712. u8 *payload;
  713. payload = rx->skb->data +
  714. ieee80211_hdrlen(hdr->frame_control);
  715. ethertype = (payload[6] << 8) | payload[7];
  716. if (cpu_to_be16(ethertype) ==
  717. rx->sdata->control_port_protocol)
  718. return RX_CONTINUE;
  719. }
  720. return RX_DROP_MONITOR;
  721. }
  722. return RX_CONTINUE;
  723. }
  724. static ieee80211_rx_result debug_noinline
  725. ieee80211_rx_h_decrypt(struct ieee80211_rx_data *rx)
  726. {
  727. struct sk_buff *skb = rx->skb;
  728. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  729. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  730. int keyidx;
  731. int hdrlen;
  732. ieee80211_rx_result result = RX_DROP_UNUSABLE;
  733. struct ieee80211_key *sta_ptk = NULL;
  734. int mmie_keyidx = -1;
  735. __le16 fc;
  736. /*
  737. * Key selection 101
  738. *
  739. * There are four types of keys:
  740. * - GTK (group keys)
  741. * - IGTK (group keys for management frames)
  742. * - PTK (pairwise keys)
  743. * - STK (station-to-station pairwise keys)
  744. *
  745. * When selecting a key, we have to distinguish between multicast
  746. * (including broadcast) and unicast frames, the latter can only
  747. * use PTKs and STKs while the former always use GTKs and IGTKs.
  748. * Unless, of course, actual WEP keys ("pre-RSNA") are used, then
  749. * unicast frames can also use key indices like GTKs. Hence, if we
  750. * don't have a PTK/STK we check the key index for a WEP key.
  751. *
  752. * Note that in a regular BSS, multicast frames are sent by the
  753. * AP only, associated stations unicast the frame to the AP first
  754. * which then multicasts it on their behalf.
  755. *
  756. * There is also a slight problem in IBSS mode: GTKs are negotiated
  757. * with each station, that is something we don't currently handle.
  758. * The spec seems to expect that one negotiates the same key with
  759. * every station but there's no such requirement; VLANs could be
  760. * possible.
  761. */
  762. /*
  763. * No point in finding a key and decrypting if the frame is neither
  764. * addressed to us nor a multicast frame.
  765. */
  766. if (!(status->rx_flags & IEEE80211_RX_RA_MATCH))
  767. return RX_CONTINUE;
  768. /* start without a key */
  769. rx->key = NULL;
  770. if (rx->sta)
  771. sta_ptk = rcu_dereference(rx->sta->ptk);
  772. fc = hdr->frame_control;
  773. if (!ieee80211_has_protected(fc))
  774. mmie_keyidx = ieee80211_get_mmie_keyidx(rx->skb);
  775. if (!is_multicast_ether_addr(hdr->addr1) && sta_ptk) {
  776. rx->key = sta_ptk;
  777. if ((status->flag & RX_FLAG_DECRYPTED) &&
  778. (status->flag & RX_FLAG_IV_STRIPPED))
  779. return RX_CONTINUE;
  780. /* Skip decryption if the frame is not protected. */
  781. if (!ieee80211_has_protected(fc))
  782. return RX_CONTINUE;
  783. } else if (mmie_keyidx >= 0) {
  784. /* Broadcast/multicast robust management frame / BIP */
  785. if ((status->flag & RX_FLAG_DECRYPTED) &&
  786. (status->flag & RX_FLAG_IV_STRIPPED))
  787. return RX_CONTINUE;
  788. if (mmie_keyidx < NUM_DEFAULT_KEYS ||
  789. mmie_keyidx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
  790. return RX_DROP_MONITOR; /* unexpected BIP keyidx */
  791. if (rx->sta)
  792. rx->key = rcu_dereference(rx->sta->gtk[mmie_keyidx]);
  793. if (!rx->key)
  794. rx->key = rcu_dereference(rx->sdata->keys[mmie_keyidx]);
  795. } else if (!ieee80211_has_protected(fc)) {
  796. /*
  797. * The frame was not protected, so skip decryption. However, we
  798. * need to set rx->key if there is a key that could have been
  799. * used so that the frame may be dropped if encryption would
  800. * have been expected.
  801. */
  802. struct ieee80211_key *key = NULL;
  803. struct ieee80211_sub_if_data *sdata = rx->sdata;
  804. int i;
  805. if (ieee80211_is_mgmt(fc) &&
  806. is_multicast_ether_addr(hdr->addr1) &&
  807. (key = rcu_dereference(rx->sdata->default_mgmt_key)))
  808. rx->key = key;
  809. else {
  810. if (rx->sta) {
  811. for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
  812. key = rcu_dereference(rx->sta->gtk[i]);
  813. if (key)
  814. break;
  815. }
  816. }
  817. if (!key) {
  818. for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
  819. key = rcu_dereference(sdata->keys[i]);
  820. if (key)
  821. break;
  822. }
  823. }
  824. if (key)
  825. rx->key = key;
  826. }
  827. return RX_CONTINUE;
  828. } else {
  829. u8 keyid;
  830. /*
  831. * The device doesn't give us the IV so we won't be
  832. * able to look up the key. That's ok though, we
  833. * don't need to decrypt the frame, we just won't
  834. * be able to keep statistics accurate.
  835. * Except for key threshold notifications, should
  836. * we somehow allow the driver to tell us which key
  837. * the hardware used if this flag is set?
  838. */
  839. if ((status->flag & RX_FLAG_DECRYPTED) &&
  840. (status->flag & RX_FLAG_IV_STRIPPED))
  841. return RX_CONTINUE;
  842. hdrlen = ieee80211_hdrlen(fc);
  843. if (rx->skb->len < 8 + hdrlen)
  844. return RX_DROP_UNUSABLE; /* TODO: count this? */
  845. /*
  846. * no need to call ieee80211_wep_get_keyidx,
  847. * it verifies a bunch of things we've done already
  848. */
  849. skb_copy_bits(rx->skb, hdrlen + 3, &keyid, 1);
  850. keyidx = keyid >> 6;
  851. /* check per-station GTK first, if multicast packet */
  852. if (is_multicast_ether_addr(hdr->addr1) && rx->sta)
  853. rx->key = rcu_dereference(rx->sta->gtk[keyidx]);
  854. /* if not found, try default key */
  855. if (!rx->key) {
  856. rx->key = rcu_dereference(rx->sdata->keys[keyidx]);
  857. /*
  858. * RSNA-protected unicast frames should always be
  859. * sent with pairwise or station-to-station keys,
  860. * but for WEP we allow using a key index as well.
  861. */
  862. if (rx->key &&
  863. rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP40 &&
  864. rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP104 &&
  865. !is_multicast_ether_addr(hdr->addr1))
  866. rx->key = NULL;
  867. }
  868. }
  869. if (rx->key) {
  870. if (unlikely(rx->key->flags & KEY_FLAG_TAINTED))
  871. return RX_DROP_MONITOR;
  872. rx->key->tx_rx_count++;
  873. /* TODO: add threshold stuff again */
  874. } else {
  875. return RX_DROP_MONITOR;
  876. }
  877. if (skb_linearize(rx->skb))
  878. return RX_DROP_UNUSABLE;
  879. /* the hdr variable is invalid now! */
  880. switch (rx->key->conf.cipher) {
  881. case WLAN_CIPHER_SUITE_WEP40:
  882. case WLAN_CIPHER_SUITE_WEP104:
  883. /* Check for weak IVs if possible */
  884. if (rx->sta && ieee80211_is_data(fc) &&
  885. (!(status->flag & RX_FLAG_IV_STRIPPED) ||
  886. !(status->flag & RX_FLAG_DECRYPTED)) &&
  887. ieee80211_wep_is_weak_iv(rx->skb, rx->key))
  888. rx->sta->wep_weak_iv_count++;
  889. result = ieee80211_crypto_wep_decrypt(rx);
  890. break;
  891. case WLAN_CIPHER_SUITE_TKIP:
  892. result = ieee80211_crypto_tkip_decrypt(rx);
  893. break;
  894. case WLAN_CIPHER_SUITE_CCMP:
  895. result = ieee80211_crypto_ccmp_decrypt(rx);
  896. break;
  897. case WLAN_CIPHER_SUITE_AES_CMAC:
  898. result = ieee80211_crypto_aes_cmac_decrypt(rx);
  899. break;
  900. default:
  901. /*
  902. * We can reach here only with HW-only algorithms
  903. * but why didn't it decrypt the frame?!
  904. */
  905. return RX_DROP_UNUSABLE;
  906. }
  907. /* either the frame has been decrypted or will be dropped */
  908. status->flag |= RX_FLAG_DECRYPTED;
  909. return result;
  910. }
  911. static ieee80211_rx_result debug_noinline
  912. ieee80211_rx_h_check_more_data(struct ieee80211_rx_data *rx)
  913. {
  914. struct ieee80211_local *local;
  915. struct ieee80211_hdr *hdr;
  916. struct sk_buff *skb;
  917. local = rx->local;
  918. skb = rx->skb;
  919. hdr = (struct ieee80211_hdr *) skb->data;
  920. if (!local->pspolling)
  921. return RX_CONTINUE;
  922. if (!ieee80211_has_fromds(hdr->frame_control))
  923. /* this is not from AP */
  924. return RX_CONTINUE;
  925. if (!ieee80211_is_data(hdr->frame_control))
  926. return RX_CONTINUE;
  927. if (!ieee80211_has_moredata(hdr->frame_control)) {
  928. /* AP has no more frames buffered for us */
  929. local->pspolling = false;
  930. return RX_CONTINUE;
  931. }
  932. /* more data bit is set, let's request a new frame from the AP */
  933. ieee80211_send_pspoll(local, rx->sdata);
  934. return RX_CONTINUE;
  935. }
  936. static void ap_sta_ps_start(struct sta_info *sta)
  937. {
  938. struct ieee80211_sub_if_data *sdata = sta->sdata;
  939. struct ieee80211_local *local = sdata->local;
  940. atomic_inc(&sdata->bss->num_sta_ps);
  941. set_sta_flags(sta, WLAN_STA_PS_STA);
  942. if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS))
  943. drv_sta_notify(local, sdata, STA_NOTIFY_SLEEP, &sta->sta);
  944. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  945. printk(KERN_DEBUG "%s: STA %pM aid %d enters power save mode\n",
  946. sdata->name, sta->sta.addr, sta->sta.aid);
  947. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  948. }
  949. static void ap_sta_ps_end(struct sta_info *sta)
  950. {
  951. struct ieee80211_sub_if_data *sdata = sta->sdata;
  952. atomic_dec(&sdata->bss->num_sta_ps);
  953. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  954. printk(KERN_DEBUG "%s: STA %pM aid %d exits power save mode\n",
  955. sdata->name, sta->sta.addr, sta->sta.aid);
  956. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  957. if (test_sta_flags(sta, WLAN_STA_PS_DRIVER)) {
  958. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  959. printk(KERN_DEBUG "%s: STA %pM aid %d driver-ps-blocked\n",
  960. sdata->name, sta->sta.addr, sta->sta.aid);
  961. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  962. return;
  963. }
  964. ieee80211_sta_ps_deliver_wakeup(sta);
  965. }
  966. int ieee80211_sta_ps_transition(struct ieee80211_sta *sta, bool start)
  967. {
  968. struct sta_info *sta_inf = container_of(sta, struct sta_info, sta);
  969. bool in_ps;
  970. WARN_ON(!(sta_inf->local->hw.flags & IEEE80211_HW_AP_LINK_PS));
  971. /* Don't let the same PS state be set twice */
  972. in_ps = test_sta_flags(sta_inf, WLAN_STA_PS_STA);
  973. if ((start && in_ps) || (!start && !in_ps))
  974. return -EINVAL;
  975. if (start)
  976. ap_sta_ps_start(sta_inf);
  977. else
  978. ap_sta_ps_end(sta_inf);
  979. return 0;
  980. }
  981. EXPORT_SYMBOL(ieee80211_sta_ps_transition);
  982. static ieee80211_rx_result debug_noinline
  983. ieee80211_rx_h_sta_process(struct ieee80211_rx_data *rx)
  984. {
  985. struct sta_info *sta = rx->sta;
  986. struct sk_buff *skb = rx->skb;
  987. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  988. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  989. if (!sta)
  990. return RX_CONTINUE;
  991. /*
  992. * Update last_rx only for IBSS packets which are for the current
  993. * BSSID to avoid keeping the current IBSS network alive in cases
  994. * where other STAs start using different BSSID.
  995. */
  996. if (rx->sdata->vif.type == NL80211_IFTYPE_ADHOC) {
  997. u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len,
  998. NL80211_IFTYPE_ADHOC);
  999. if (compare_ether_addr(bssid, rx->sdata->u.ibss.bssid) == 0) {
  1000. sta->last_rx = jiffies;
  1001. if (ieee80211_is_data(hdr->frame_control)) {
  1002. sta->last_rx_rate_idx = status->rate_idx;
  1003. sta->last_rx_rate_flag = status->flag;
  1004. }
  1005. }
  1006. } else if (!is_multicast_ether_addr(hdr->addr1)) {
  1007. /*
  1008. * Mesh beacons will update last_rx when if they are found to
  1009. * match the current local configuration when processed.
  1010. */
  1011. sta->last_rx = jiffies;
  1012. if (ieee80211_is_data(hdr->frame_control)) {
  1013. sta->last_rx_rate_idx = status->rate_idx;
  1014. sta->last_rx_rate_flag = status->flag;
  1015. }
  1016. }
  1017. if (!(status->rx_flags & IEEE80211_RX_RA_MATCH))
  1018. return RX_CONTINUE;
  1019. if (rx->sdata->vif.type == NL80211_IFTYPE_STATION)
  1020. ieee80211_sta_rx_notify(rx->sdata, hdr);
  1021. sta->rx_fragments++;
  1022. sta->rx_bytes += rx->skb->len;
  1023. sta->last_signal = status->signal;
  1024. ewma_add(&sta->avg_signal, -status->signal);
  1025. /*
  1026. * Change STA power saving mode only at the end of a frame
  1027. * exchange sequence.
  1028. */
  1029. if (!(sta->local->hw.flags & IEEE80211_HW_AP_LINK_PS) &&
  1030. !ieee80211_has_morefrags(hdr->frame_control) &&
  1031. !(status->rx_flags & IEEE80211_RX_DEFERRED_RELEASE) &&
  1032. (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
  1033. rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)) {
  1034. if (test_sta_flags(sta, WLAN_STA_PS_STA)) {
  1035. /*
  1036. * Ignore doze->wake transitions that are
  1037. * indicated by non-data frames, the standard
  1038. * is unclear here, but for example going to
  1039. * PS mode and then scanning would cause a
  1040. * doze->wake transition for the probe request,
  1041. * and that is clearly undesirable.
  1042. */
  1043. if (ieee80211_is_data(hdr->frame_control) &&
  1044. !ieee80211_has_pm(hdr->frame_control))
  1045. ap_sta_ps_end(sta);
  1046. } else {
  1047. if (ieee80211_has_pm(hdr->frame_control))
  1048. ap_sta_ps_start(sta);
  1049. }
  1050. }
  1051. /*
  1052. * Drop (qos-)data::nullfunc frames silently, since they
  1053. * are used only to control station power saving mode.
  1054. */
  1055. if (ieee80211_is_nullfunc(hdr->frame_control) ||
  1056. ieee80211_is_qos_nullfunc(hdr->frame_control)) {
  1057. I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
  1058. /*
  1059. * If we receive a 4-addr nullfunc frame from a STA
  1060. * that was not moved to a 4-addr STA vlan yet, drop
  1061. * the frame to the monitor interface, to make sure
  1062. * that hostapd sees it
  1063. */
  1064. if (ieee80211_has_a4(hdr->frame_control) &&
  1065. (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
  1066. (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
  1067. !rx->sdata->u.vlan.sta)))
  1068. return RX_DROP_MONITOR;
  1069. /*
  1070. * Update counter and free packet here to avoid
  1071. * counting this as a dropped packed.
  1072. */
  1073. sta->rx_packets++;
  1074. dev_kfree_skb(rx->skb);
  1075. return RX_QUEUED;
  1076. }
  1077. return RX_CONTINUE;
  1078. } /* ieee80211_rx_h_sta_process */
  1079. static inline struct ieee80211_fragment_entry *
  1080. ieee80211_reassemble_add(struct ieee80211_sub_if_data *sdata,
  1081. unsigned int frag, unsigned int seq, int rx_queue,
  1082. struct sk_buff **skb)
  1083. {
  1084. struct ieee80211_fragment_entry *entry;
  1085. int idx;
  1086. idx = sdata->fragment_next;
  1087. entry = &sdata->fragments[sdata->fragment_next++];
  1088. if (sdata->fragment_next >= IEEE80211_FRAGMENT_MAX)
  1089. sdata->fragment_next = 0;
  1090. if (!skb_queue_empty(&entry->skb_list)) {
  1091. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  1092. struct ieee80211_hdr *hdr =
  1093. (struct ieee80211_hdr *) entry->skb_list.next->data;
  1094. printk(KERN_DEBUG "%s: RX reassembly removed oldest "
  1095. "fragment entry (idx=%d age=%lu seq=%d last_frag=%d "
  1096. "addr1=%pM addr2=%pM\n",
  1097. sdata->name, idx,
  1098. jiffies - entry->first_frag_time, entry->seq,
  1099. entry->last_frag, hdr->addr1, hdr->addr2);
  1100. #endif
  1101. __skb_queue_purge(&entry->skb_list);
  1102. }
  1103. __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
  1104. *skb = NULL;
  1105. entry->first_frag_time = jiffies;
  1106. entry->seq = seq;
  1107. entry->rx_queue = rx_queue;
  1108. entry->last_frag = frag;
  1109. entry->ccmp = 0;
  1110. entry->extra_len = 0;
  1111. return entry;
  1112. }
  1113. static inline struct ieee80211_fragment_entry *
  1114. ieee80211_reassemble_find(struct ieee80211_sub_if_data *sdata,
  1115. unsigned int frag, unsigned int seq,
  1116. int rx_queue, struct ieee80211_hdr *hdr)
  1117. {
  1118. struct ieee80211_fragment_entry *entry;
  1119. int i, idx;
  1120. idx = sdata->fragment_next;
  1121. for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
  1122. struct ieee80211_hdr *f_hdr;
  1123. idx--;
  1124. if (idx < 0)
  1125. idx = IEEE80211_FRAGMENT_MAX - 1;
  1126. entry = &sdata->fragments[idx];
  1127. if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
  1128. entry->rx_queue != rx_queue ||
  1129. entry->last_frag + 1 != frag)
  1130. continue;
  1131. f_hdr = (struct ieee80211_hdr *)entry->skb_list.next->data;
  1132. /*
  1133. * Check ftype and addresses are equal, else check next fragment
  1134. */
  1135. if (((hdr->frame_control ^ f_hdr->frame_control) &
  1136. cpu_to_le16(IEEE80211_FCTL_FTYPE)) ||
  1137. compare_ether_addr(hdr->addr1, f_hdr->addr1) != 0 ||
  1138. compare_ether_addr(hdr->addr2, f_hdr->addr2) != 0)
  1139. continue;
  1140. if (time_after(jiffies, entry->first_frag_time + 2 * HZ)) {
  1141. __skb_queue_purge(&entry->skb_list);
  1142. continue;
  1143. }
  1144. return entry;
  1145. }
  1146. return NULL;
  1147. }
  1148. static ieee80211_rx_result debug_noinline
  1149. ieee80211_rx_h_defragment(struct ieee80211_rx_data *rx)
  1150. {
  1151. struct ieee80211_hdr *hdr;
  1152. u16 sc;
  1153. __le16 fc;
  1154. unsigned int frag, seq;
  1155. struct ieee80211_fragment_entry *entry;
  1156. struct sk_buff *skb;
  1157. struct ieee80211_rx_status *status;
  1158. hdr = (struct ieee80211_hdr *)rx->skb->data;
  1159. fc = hdr->frame_control;
  1160. sc = le16_to_cpu(hdr->seq_ctrl);
  1161. frag = sc & IEEE80211_SCTL_FRAG;
  1162. if (likely((!ieee80211_has_morefrags(fc) && frag == 0) ||
  1163. (rx->skb)->len < 24 ||
  1164. is_multicast_ether_addr(hdr->addr1))) {
  1165. /* not fragmented */
  1166. goto out;
  1167. }
  1168. I802_DEBUG_INC(rx->local->rx_handlers_fragments);
  1169. if (skb_linearize(rx->skb))
  1170. return RX_DROP_UNUSABLE;
  1171. /*
  1172. * skb_linearize() might change the skb->data and
  1173. * previously cached variables (in this case, hdr) need to
  1174. * be refreshed with the new data.
  1175. */
  1176. hdr = (struct ieee80211_hdr *)rx->skb->data;
  1177. seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
  1178. if (frag == 0) {
  1179. /* This is the first fragment of a new frame. */
  1180. entry = ieee80211_reassemble_add(rx->sdata, frag, seq,
  1181. rx->seqno_idx, &(rx->skb));
  1182. if (rx->key && rx->key->conf.cipher == WLAN_CIPHER_SUITE_CCMP &&
  1183. ieee80211_has_protected(fc)) {
  1184. int queue = rx->security_idx;
  1185. /* Store CCMP PN so that we can verify that the next
  1186. * fragment has a sequential PN value. */
  1187. entry->ccmp = 1;
  1188. memcpy(entry->last_pn,
  1189. rx->key->u.ccmp.rx_pn[queue],
  1190. CCMP_PN_LEN);
  1191. }
  1192. return RX_QUEUED;
  1193. }
  1194. /* This is a fragment for a frame that should already be pending in
  1195. * fragment cache. Add this fragment to the end of the pending entry.
  1196. */
  1197. entry = ieee80211_reassemble_find(rx->sdata, frag, seq,
  1198. rx->seqno_idx, hdr);
  1199. if (!entry) {
  1200. I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
  1201. return RX_DROP_MONITOR;
  1202. }
  1203. /* Verify that MPDUs within one MSDU have sequential PN values.
  1204. * (IEEE 802.11i, 8.3.3.4.5) */
  1205. if (entry->ccmp) {
  1206. int i;
  1207. u8 pn[CCMP_PN_LEN], *rpn;
  1208. int queue;
  1209. if (!rx->key || rx->key->conf.cipher != WLAN_CIPHER_SUITE_CCMP)
  1210. return RX_DROP_UNUSABLE;
  1211. memcpy(pn, entry->last_pn, CCMP_PN_LEN);
  1212. for (i = CCMP_PN_LEN - 1; i >= 0; i--) {
  1213. pn[i]++;
  1214. if (pn[i])
  1215. break;
  1216. }
  1217. queue = rx->security_idx;
  1218. rpn = rx->key->u.ccmp.rx_pn[queue];
  1219. if (memcmp(pn, rpn, CCMP_PN_LEN))
  1220. return RX_DROP_UNUSABLE;
  1221. memcpy(entry->last_pn, pn, CCMP_PN_LEN);
  1222. }
  1223. skb_pull(rx->skb, ieee80211_hdrlen(fc));
  1224. __skb_queue_tail(&entry->skb_list, rx->skb);
  1225. entry->last_frag = frag;
  1226. entry->extra_len += rx->skb->len;
  1227. if (ieee80211_has_morefrags(fc)) {
  1228. rx->skb = NULL;
  1229. return RX_QUEUED;
  1230. }
  1231. rx->skb = __skb_dequeue(&entry->skb_list);
  1232. if (skb_tailroom(rx->skb) < entry->extra_len) {
  1233. I802_DEBUG_INC(rx->local->rx_expand_skb_head2);
  1234. if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
  1235. GFP_ATOMIC))) {
  1236. I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
  1237. __skb_queue_purge(&entry->skb_list);
  1238. return RX_DROP_UNUSABLE;
  1239. }
  1240. }
  1241. while ((skb = __skb_dequeue(&entry->skb_list))) {
  1242. memcpy(skb_put(rx->skb, skb->len), skb->data, skb->len);
  1243. dev_kfree_skb(skb);
  1244. }
  1245. /* Complete frame has been reassembled - process it now */
  1246. status = IEEE80211_SKB_RXCB(rx->skb);
  1247. status->rx_flags |= IEEE80211_RX_FRAGMENTED;
  1248. out:
  1249. if (rx->sta)
  1250. rx->sta->rx_packets++;
  1251. if (is_multicast_ether_addr(hdr->addr1))
  1252. rx->local->dot11MulticastReceivedFrameCount++;
  1253. else
  1254. ieee80211_led_rx(rx->local);
  1255. return RX_CONTINUE;
  1256. }
  1257. static ieee80211_rx_result debug_noinline
  1258. ieee80211_rx_h_ps_poll(struct ieee80211_rx_data *rx)
  1259. {
  1260. struct ieee80211_sub_if_data *sdata = rx->sdata;
  1261. __le16 fc = ((struct ieee80211_hdr *)rx->skb->data)->frame_control;
  1262. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  1263. if (likely(!rx->sta || !ieee80211_is_pspoll(fc) ||
  1264. !(status->rx_flags & IEEE80211_RX_RA_MATCH)))
  1265. return RX_CONTINUE;
  1266. if ((sdata->vif.type != NL80211_IFTYPE_AP) &&
  1267. (sdata->vif.type != NL80211_IFTYPE_AP_VLAN))
  1268. return RX_DROP_UNUSABLE;
  1269. if (!test_sta_flags(rx->sta, WLAN_STA_PS_DRIVER))
  1270. ieee80211_sta_ps_deliver_poll_response(rx->sta);
  1271. else
  1272. set_sta_flags(rx->sta, WLAN_STA_PSPOLL);
  1273. /* Free PS Poll skb here instead of returning RX_DROP that would
  1274. * count as an dropped frame. */
  1275. dev_kfree_skb(rx->skb);
  1276. return RX_QUEUED;
  1277. }
  1278. static ieee80211_rx_result debug_noinline
  1279. ieee80211_rx_h_remove_qos_control(struct ieee80211_rx_data *rx)
  1280. {
  1281. u8 *data = rx->skb->data;
  1282. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)data;
  1283. if (!ieee80211_is_data_qos(hdr->frame_control))
  1284. return RX_CONTINUE;
  1285. /* remove the qos control field, update frame type and meta-data */
  1286. memmove(data + IEEE80211_QOS_CTL_LEN, data,
  1287. ieee80211_hdrlen(hdr->frame_control) - IEEE80211_QOS_CTL_LEN);
  1288. hdr = (struct ieee80211_hdr *)skb_pull(rx->skb, IEEE80211_QOS_CTL_LEN);
  1289. /* change frame type to non QOS */
  1290. hdr->frame_control &= ~cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
  1291. return RX_CONTINUE;
  1292. }
  1293. static int
  1294. ieee80211_802_1x_port_control(struct ieee80211_rx_data *rx)
  1295. {
  1296. if (unlikely(!rx->sta ||
  1297. !test_sta_flags(rx->sta, WLAN_STA_AUTHORIZED)))
  1298. return -EACCES;
  1299. return 0;
  1300. }
  1301. static int
  1302. ieee80211_drop_unencrypted(struct ieee80211_rx_data *rx, __le16 fc)
  1303. {
  1304. struct sk_buff *skb = rx->skb;
  1305. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  1306. /*
  1307. * Pass through unencrypted frames if the hardware has
  1308. * decrypted them already.
  1309. */
  1310. if (status->flag & RX_FLAG_DECRYPTED)
  1311. return 0;
  1312. /* Drop unencrypted frames if key is set. */
  1313. if (unlikely(!ieee80211_has_protected(fc) &&
  1314. !ieee80211_is_nullfunc(fc) &&
  1315. ieee80211_is_data(fc) &&
  1316. (rx->key || rx->sdata->drop_unencrypted)))
  1317. return -EACCES;
  1318. return 0;
  1319. }
  1320. static int
  1321. ieee80211_drop_unencrypted_mgmt(struct ieee80211_rx_data *rx)
  1322. {
  1323. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  1324. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  1325. __le16 fc = hdr->frame_control;
  1326. /*
  1327. * Pass through unencrypted frames if the hardware has
  1328. * decrypted them already.
  1329. */
  1330. if (status->flag & RX_FLAG_DECRYPTED)
  1331. return 0;
  1332. if (rx->sta && test_sta_flags(rx->sta, WLAN_STA_MFP)) {
  1333. if (unlikely(!ieee80211_has_protected(fc) &&
  1334. ieee80211_is_unicast_robust_mgmt_frame(rx->skb) &&
  1335. rx->key)) {
  1336. if (ieee80211_is_deauth(fc))
  1337. cfg80211_send_unprot_deauth(rx->sdata->dev,
  1338. rx->skb->data,
  1339. rx->skb->len);
  1340. else if (ieee80211_is_disassoc(fc))
  1341. cfg80211_send_unprot_disassoc(rx->sdata->dev,
  1342. rx->skb->data,
  1343. rx->skb->len);
  1344. return -EACCES;
  1345. }
  1346. /* BIP does not use Protected field, so need to check MMIE */
  1347. if (unlikely(ieee80211_is_multicast_robust_mgmt_frame(rx->skb) &&
  1348. ieee80211_get_mmie_keyidx(rx->skb) < 0)) {
  1349. if (ieee80211_is_deauth(fc))
  1350. cfg80211_send_unprot_deauth(rx->sdata->dev,
  1351. rx->skb->data,
  1352. rx->skb->len);
  1353. else if (ieee80211_is_disassoc(fc))
  1354. cfg80211_send_unprot_disassoc(rx->sdata->dev,
  1355. rx->skb->data,
  1356. rx->skb->len);
  1357. return -EACCES;
  1358. }
  1359. /*
  1360. * When using MFP, Action frames are not allowed prior to
  1361. * having configured keys.
  1362. */
  1363. if (unlikely(ieee80211_is_action(fc) && !rx->key &&
  1364. ieee80211_is_robust_mgmt_frame(
  1365. (struct ieee80211_hdr *) rx->skb->data)))
  1366. return -EACCES;
  1367. }
  1368. return 0;
  1369. }
  1370. static int
  1371. __ieee80211_data_to_8023(struct ieee80211_rx_data *rx, bool *port_control)
  1372. {
  1373. struct ieee80211_sub_if_data *sdata = rx->sdata;
  1374. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  1375. bool check_port_control = false;
  1376. struct ethhdr *ehdr;
  1377. int ret;
  1378. *port_control = false;
  1379. if (ieee80211_has_a4(hdr->frame_control) &&
  1380. sdata->vif.type == NL80211_IFTYPE_AP_VLAN && !sdata->u.vlan.sta)
  1381. return -1;
  1382. if (sdata->vif.type == NL80211_IFTYPE_STATION &&
  1383. !!sdata->u.mgd.use_4addr != !!ieee80211_has_a4(hdr->frame_control)) {
  1384. if (!sdata->u.mgd.use_4addr)
  1385. return -1;
  1386. else
  1387. check_port_control = true;
  1388. }
  1389. if (is_multicast_ether_addr(hdr->addr1) &&
  1390. sdata->vif.type == NL80211_IFTYPE_AP_VLAN && sdata->u.vlan.sta)
  1391. return -1;
  1392. ret = ieee80211_data_to_8023(rx->skb, sdata->vif.addr, sdata->vif.type);
  1393. if (ret < 0)
  1394. return ret;
  1395. ehdr = (struct ethhdr *) rx->skb->data;
  1396. if (ehdr->h_proto == rx->sdata->control_port_protocol)
  1397. *port_control = true;
  1398. else if (check_port_control)
  1399. return -1;
  1400. return 0;
  1401. }
  1402. /*
  1403. * requires that rx->skb is a frame with ethernet header
  1404. */
  1405. static bool ieee80211_frame_allowed(struct ieee80211_rx_data *rx, __le16 fc)
  1406. {
  1407. static const u8 pae_group_addr[ETH_ALEN] __aligned(2)
  1408. = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x03 };
  1409. struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
  1410. /*
  1411. * Allow EAPOL frames to us/the PAE group address regardless
  1412. * of whether the frame was encrypted or not.
  1413. */
  1414. if (ehdr->h_proto == rx->sdata->control_port_protocol &&
  1415. (compare_ether_addr(ehdr->h_dest, rx->sdata->vif.addr) == 0 ||
  1416. compare_ether_addr(ehdr->h_dest, pae_group_addr) == 0))
  1417. return true;
  1418. if (ieee80211_802_1x_port_control(rx) ||
  1419. ieee80211_drop_unencrypted(rx, fc))
  1420. return false;
  1421. return true;
  1422. }
  1423. /*
  1424. * requires that rx->skb is a frame with ethernet header
  1425. */
  1426. static void
  1427. ieee80211_deliver_skb(struct ieee80211_rx_data *rx)
  1428. {
  1429. struct ieee80211_sub_if_data *sdata = rx->sdata;
  1430. struct net_device *dev = sdata->dev;
  1431. struct sk_buff *skb, *xmit_skb;
  1432. struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
  1433. struct sta_info *dsta;
  1434. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  1435. skb = rx->skb;
  1436. xmit_skb = NULL;
  1437. if ((sdata->vif.type == NL80211_IFTYPE_AP ||
  1438. sdata->vif.type == NL80211_IFTYPE_AP_VLAN) &&
  1439. !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
  1440. (status->rx_flags & IEEE80211_RX_RA_MATCH) &&
  1441. (sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->u.vlan.sta)) {
  1442. if (is_multicast_ether_addr(ehdr->h_dest)) {
  1443. /*
  1444. * send multicast frames both to higher layers in
  1445. * local net stack and back to the wireless medium
  1446. */
  1447. xmit_skb = skb_copy(skb, GFP_ATOMIC);
  1448. if (!xmit_skb && net_ratelimit())
  1449. printk(KERN_DEBUG "%s: failed to clone "
  1450. "multicast frame\n", dev->name);
  1451. } else {
  1452. dsta = sta_info_get(sdata, skb->data);
  1453. if (dsta) {
  1454. /*
  1455. * The destination station is associated to
  1456. * this AP (in this VLAN), so send the frame
  1457. * directly to it and do not pass it to local
  1458. * net stack.
  1459. */
  1460. xmit_skb = skb;
  1461. skb = NULL;
  1462. }
  1463. }
  1464. }
  1465. if (skb) {
  1466. int align __maybe_unused;
  1467. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  1468. /*
  1469. * 'align' will only take the values 0 or 2 here
  1470. * since all frames are required to be aligned
  1471. * to 2-byte boundaries when being passed to
  1472. * mac80211. That also explains the __skb_push()
  1473. * below.
  1474. */
  1475. align = ((unsigned long)(skb->data + sizeof(struct ethhdr))) & 3;
  1476. if (align) {
  1477. if (WARN_ON(skb_headroom(skb) < 3)) {
  1478. dev_kfree_skb(skb);
  1479. skb = NULL;
  1480. } else {
  1481. u8 *data = skb->data;
  1482. size_t len = skb_headlen(skb);
  1483. skb->data -= align;
  1484. memmove(skb->data, data, len);
  1485. skb_set_tail_pointer(skb, len);
  1486. }
  1487. }
  1488. #endif
  1489. if (skb) {
  1490. /* deliver to local stack */
  1491. skb->protocol = eth_type_trans(skb, dev);
  1492. memset(skb->cb, 0, sizeof(skb->cb));
  1493. netif_receive_skb(skb);
  1494. }
  1495. }
  1496. if (xmit_skb) {
  1497. /* send to wireless media */
  1498. xmit_skb->protocol = htons(ETH_P_802_3);
  1499. skb_reset_network_header(xmit_skb);
  1500. skb_reset_mac_header(xmit_skb);
  1501. dev_queue_xmit(xmit_skb);
  1502. }
  1503. }
  1504. static ieee80211_rx_result debug_noinline
  1505. ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx)
  1506. {
  1507. struct net_device *dev = rx->sdata->dev;
  1508. struct sk_buff *skb = rx->skb;
  1509. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1510. __le16 fc = hdr->frame_control;
  1511. struct sk_buff_head frame_list;
  1512. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  1513. if (unlikely(!ieee80211_is_data(fc)))
  1514. return RX_CONTINUE;
  1515. if (unlikely(!ieee80211_is_data_present(fc)))
  1516. return RX_DROP_MONITOR;
  1517. if (!(status->rx_flags & IEEE80211_RX_AMSDU))
  1518. return RX_CONTINUE;
  1519. if (ieee80211_has_a4(hdr->frame_control) &&
  1520. rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
  1521. !rx->sdata->u.vlan.sta)
  1522. return RX_DROP_UNUSABLE;
  1523. if (is_multicast_ether_addr(hdr->addr1) &&
  1524. ((rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
  1525. rx->sdata->u.vlan.sta) ||
  1526. (rx->sdata->vif.type == NL80211_IFTYPE_STATION &&
  1527. rx->sdata->u.mgd.use_4addr)))
  1528. return RX_DROP_UNUSABLE;
  1529. skb->dev = dev;
  1530. __skb_queue_head_init(&frame_list);
  1531. if (skb_linearize(skb))
  1532. return RX_DROP_UNUSABLE;
  1533. ieee80211_amsdu_to_8023s(skb, &frame_list, dev->dev_addr,
  1534. rx->sdata->vif.type,
  1535. rx->local->hw.extra_tx_headroom, true);
  1536. while (!skb_queue_empty(&frame_list)) {
  1537. rx->skb = __skb_dequeue(&frame_list);
  1538. if (!ieee80211_frame_allowed(rx, fc)) {
  1539. dev_kfree_skb(rx->skb);
  1540. continue;
  1541. }
  1542. dev->stats.rx_packets++;
  1543. dev->stats.rx_bytes += rx->skb->len;
  1544. ieee80211_deliver_skb(rx);
  1545. }
  1546. return RX_QUEUED;
  1547. }
  1548. #ifdef CONFIG_MAC80211_MESH
  1549. static ieee80211_rx_result
  1550. ieee80211_rx_h_mesh_fwding(struct ieee80211_rx_data *rx)
  1551. {
  1552. struct ieee80211_hdr *hdr;
  1553. struct ieee80211s_hdr *mesh_hdr;
  1554. unsigned int hdrlen;
  1555. struct sk_buff *skb = rx->skb, *fwd_skb;
  1556. struct ieee80211_local *local = rx->local;
  1557. struct ieee80211_sub_if_data *sdata = rx->sdata;
  1558. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  1559. hdr = (struct ieee80211_hdr *) skb->data;
  1560. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  1561. mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
  1562. /* frame is in RMC, don't forward */
  1563. if (ieee80211_is_data(hdr->frame_control) &&
  1564. is_multicast_ether_addr(hdr->addr1) &&
  1565. mesh_rmc_check(hdr->addr3, mesh_hdr, rx->sdata))
  1566. return RX_DROP_MONITOR;
  1567. if (!ieee80211_is_data(hdr->frame_control))
  1568. return RX_CONTINUE;
  1569. if (!mesh_hdr->ttl)
  1570. /* illegal frame */
  1571. return RX_DROP_MONITOR;
  1572. if (mesh_hdr->flags & MESH_FLAGS_AE) {
  1573. struct mesh_path *mppath;
  1574. char *proxied_addr;
  1575. char *mpp_addr;
  1576. if (is_multicast_ether_addr(hdr->addr1)) {
  1577. mpp_addr = hdr->addr3;
  1578. proxied_addr = mesh_hdr->eaddr1;
  1579. } else {
  1580. mpp_addr = hdr->addr4;
  1581. proxied_addr = mesh_hdr->eaddr2;
  1582. }
  1583. rcu_read_lock();
  1584. mppath = mpp_path_lookup(proxied_addr, sdata);
  1585. if (!mppath) {
  1586. mpp_path_add(proxied_addr, mpp_addr, sdata);
  1587. } else {
  1588. spin_lock_bh(&mppath->state_lock);
  1589. if (compare_ether_addr(mppath->mpp, mpp_addr) != 0)
  1590. memcpy(mppath->mpp, mpp_addr, ETH_ALEN);
  1591. spin_unlock_bh(&mppath->state_lock);
  1592. }
  1593. rcu_read_unlock();
  1594. }
  1595. /* Frame has reached destination. Don't forward */
  1596. if (!is_multicast_ether_addr(hdr->addr1) &&
  1597. compare_ether_addr(sdata->vif.addr, hdr->addr3) == 0)
  1598. return RX_CONTINUE;
  1599. mesh_hdr->ttl--;
  1600. if (status->rx_flags & IEEE80211_RX_RA_MATCH) {
  1601. if (!mesh_hdr->ttl)
  1602. IEEE80211_IFSTA_MESH_CTR_INC(&rx->sdata->u.mesh,
  1603. dropped_frames_ttl);
  1604. else {
  1605. struct ieee80211_hdr *fwd_hdr;
  1606. struct ieee80211_tx_info *info;
  1607. fwd_skb = skb_copy(skb, GFP_ATOMIC);
  1608. if (!fwd_skb && net_ratelimit())
  1609. printk(KERN_DEBUG "%s: failed to clone mesh frame\n",
  1610. sdata->name);
  1611. if (!fwd_skb)
  1612. goto out;
  1613. fwd_hdr = (struct ieee80211_hdr *) fwd_skb->data;
  1614. memcpy(fwd_hdr->addr2, sdata->vif.addr, ETH_ALEN);
  1615. info = IEEE80211_SKB_CB(fwd_skb);
  1616. memset(info, 0, sizeof(*info));
  1617. info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
  1618. info->control.vif = &rx->sdata->vif;
  1619. skb_set_queue_mapping(skb,
  1620. ieee80211_select_queue(rx->sdata, fwd_skb));
  1621. ieee80211_set_qos_hdr(local, skb);
  1622. if (is_multicast_ether_addr(fwd_hdr->addr1))
  1623. IEEE80211_IFSTA_MESH_CTR_INC(&sdata->u.mesh,
  1624. fwded_mcast);
  1625. else {
  1626. int err;
  1627. /*
  1628. * Save TA to addr1 to send TA a path error if a
  1629. * suitable next hop is not found
  1630. */
  1631. memcpy(fwd_hdr->addr1, fwd_hdr->addr2,
  1632. ETH_ALEN);
  1633. err = mesh_nexthop_lookup(fwd_skb, sdata);
  1634. /* Failed to immediately resolve next hop:
  1635. * fwded frame was dropped or will be added
  1636. * later to the pending skb queue. */
  1637. if (err)
  1638. return RX_DROP_MONITOR;
  1639. IEEE80211_IFSTA_MESH_CTR_INC(&sdata->u.mesh,
  1640. fwded_unicast);
  1641. }
  1642. IEEE80211_IFSTA_MESH_CTR_INC(&sdata->u.mesh,
  1643. fwded_frames);
  1644. ieee80211_add_pending_skb(local, fwd_skb);
  1645. }
  1646. }
  1647. out:
  1648. if (is_multicast_ether_addr(hdr->addr1) ||
  1649. sdata->dev->flags & IFF_PROMISC)
  1650. return RX_CONTINUE;
  1651. else
  1652. return RX_DROP_MONITOR;
  1653. }
  1654. #endif
  1655. static ieee80211_rx_result debug_noinline
  1656. ieee80211_rx_h_data(struct ieee80211_rx_data *rx)
  1657. {
  1658. struct ieee80211_sub_if_data *sdata = rx->sdata;
  1659. struct ieee80211_local *local = rx->local;
  1660. struct net_device *dev = sdata->dev;
  1661. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  1662. __le16 fc = hdr->frame_control;
  1663. bool port_control;
  1664. int err;
  1665. if (unlikely(!ieee80211_is_data(hdr->frame_control)))
  1666. return RX_CONTINUE;
  1667. if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
  1668. return RX_DROP_MONITOR;
  1669. /*
  1670. * Allow the cooked monitor interface of an AP to see 4-addr frames so
  1671. * that a 4-addr station can be detected and moved into a separate VLAN
  1672. */
  1673. if (ieee80211_has_a4(hdr->frame_control) &&
  1674. sdata->vif.type == NL80211_IFTYPE_AP)
  1675. return RX_DROP_MONITOR;
  1676. err = __ieee80211_data_to_8023(rx, &port_control);
  1677. if (unlikely(err))
  1678. return RX_DROP_UNUSABLE;
  1679. if (!ieee80211_frame_allowed(rx, fc))
  1680. return RX_DROP_MONITOR;
  1681. if (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
  1682. unlikely(port_control) && sdata->bss) {
  1683. sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
  1684. u.ap);
  1685. dev = sdata->dev;
  1686. rx->sdata = sdata;
  1687. }
  1688. rx->skb->dev = dev;
  1689. dev->stats.rx_packets++;
  1690. dev->stats.rx_bytes += rx->skb->len;
  1691. if (local->ps_sdata && local->hw.conf.dynamic_ps_timeout > 0 &&
  1692. !is_multicast_ether_addr(
  1693. ((struct ethhdr *)rx->skb->data)->h_dest) &&
  1694. (!local->scanning &&
  1695. !test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state))) {
  1696. mod_timer(&local->dynamic_ps_timer, jiffies +
  1697. msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
  1698. }
  1699. ieee80211_deliver_skb(rx);
  1700. return RX_QUEUED;
  1701. }
  1702. static ieee80211_rx_result debug_noinline
  1703. ieee80211_rx_h_ctrl(struct ieee80211_rx_data *rx)
  1704. {
  1705. struct ieee80211_local *local = rx->local;
  1706. struct ieee80211_hw *hw = &local->hw;
  1707. struct sk_buff *skb = rx->skb;
  1708. struct ieee80211_bar *bar = (struct ieee80211_bar *)skb->data;
  1709. struct tid_ampdu_rx *tid_agg_rx;
  1710. u16 start_seq_num;
  1711. u16 tid;
  1712. if (likely(!ieee80211_is_ctl(bar->frame_control)))
  1713. return RX_CONTINUE;
  1714. if (ieee80211_is_back_req(bar->frame_control)) {
  1715. struct {
  1716. __le16 control, start_seq_num;
  1717. } __packed bar_data;
  1718. if (!rx->sta)
  1719. return RX_DROP_MONITOR;
  1720. if (skb_copy_bits(skb, offsetof(struct ieee80211_bar, control),
  1721. &bar_data, sizeof(bar_data)))
  1722. return RX_DROP_MONITOR;
  1723. tid = le16_to_cpu(bar_data.control) >> 12;
  1724. tid_agg_rx = rcu_dereference(rx->sta->ampdu_mlme.tid_rx[tid]);
  1725. if (!tid_agg_rx)
  1726. return RX_DROP_MONITOR;
  1727. start_seq_num = le16_to_cpu(bar_data.start_seq_num) >> 4;
  1728. /* reset session timer */
  1729. if (tid_agg_rx->timeout)
  1730. mod_timer(&tid_agg_rx->session_timer,
  1731. TU_TO_EXP_TIME(tid_agg_rx->timeout));
  1732. spin_lock(&tid_agg_rx->reorder_lock);
  1733. /* release stored frames up to start of BAR */
  1734. ieee80211_release_reorder_frames(hw, tid_agg_rx, start_seq_num);
  1735. spin_unlock(&tid_agg_rx->reorder_lock);
  1736. kfree_skb(skb);
  1737. return RX_QUEUED;
  1738. }
  1739. /*
  1740. * After this point, we only want management frames,
  1741. * so we can drop all remaining control frames to
  1742. * cooked monitor interfaces.
  1743. */
  1744. return RX_DROP_MONITOR;
  1745. }
  1746. static void ieee80211_process_sa_query_req(struct ieee80211_sub_if_data *sdata,
  1747. struct ieee80211_mgmt *mgmt,
  1748. size_t len)
  1749. {
  1750. struct ieee80211_local *local = sdata->local;
  1751. struct sk_buff *skb;
  1752. struct ieee80211_mgmt *resp;
  1753. if (compare_ether_addr(mgmt->da, sdata->vif.addr) != 0) {
  1754. /* Not to own unicast address */
  1755. return;
  1756. }
  1757. if (compare_ether_addr(mgmt->sa, sdata->u.mgd.bssid) != 0 ||
  1758. compare_ether_addr(mgmt->bssid, sdata->u.mgd.bssid) != 0) {
  1759. /* Not from the current AP or not associated yet. */
  1760. return;
  1761. }
  1762. if (len < 24 + 1 + sizeof(resp->u.action.u.sa_query)) {
  1763. /* Too short SA Query request frame */
  1764. return;
  1765. }
  1766. skb = dev_alloc_skb(sizeof(*resp) + local->hw.extra_tx_headroom);
  1767. if (skb == NULL)
  1768. return;
  1769. skb_reserve(skb, local->hw.extra_tx_headroom);
  1770. resp = (struct ieee80211_mgmt *) skb_put(skb, 24);
  1771. memset(resp, 0, 24);
  1772. memcpy(resp->da, mgmt->sa, ETH_ALEN);
  1773. memcpy(resp->sa, sdata->vif.addr, ETH_ALEN);
  1774. memcpy(resp->bssid, sdata->u.mgd.bssid, ETH_ALEN);
  1775. resp->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  1776. IEEE80211_STYPE_ACTION);
  1777. skb_put(skb, 1 + sizeof(resp->u.action.u.sa_query));
  1778. resp->u.action.category = WLAN_CATEGORY_SA_QUERY;
  1779. resp->u.action.u.sa_query.action = WLAN_ACTION_SA_QUERY_RESPONSE;
  1780. memcpy(resp->u.action.u.sa_query.trans_id,
  1781. mgmt->u.action.u.sa_query.trans_id,
  1782. WLAN_SA_QUERY_TR_ID_LEN);
  1783. ieee80211_tx_skb(sdata, skb);
  1784. }
  1785. static ieee80211_rx_result debug_noinline
  1786. ieee80211_rx_h_mgmt_check(struct ieee80211_rx_data *rx)
  1787. {
  1788. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
  1789. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  1790. /*
  1791. * From here on, look only at management frames.
  1792. * Data and control frames are already handled,
  1793. * and unknown (reserved) frames are useless.
  1794. */
  1795. if (rx->skb->len < 24)
  1796. return RX_DROP_MONITOR;
  1797. if (!ieee80211_is_mgmt(mgmt->frame_control))
  1798. return RX_DROP_MONITOR;
  1799. if (!(status->rx_flags & IEEE80211_RX_RA_MATCH))
  1800. return RX_DROP_MONITOR;
  1801. if (ieee80211_drop_unencrypted_mgmt(rx))
  1802. return RX_DROP_UNUSABLE;
  1803. return RX_CONTINUE;
  1804. }
  1805. static ieee80211_rx_result debug_noinline
  1806. ieee80211_rx_h_action(struct ieee80211_rx_data *rx)
  1807. {
  1808. struct ieee80211_local *local = rx->local;
  1809. struct ieee80211_sub_if_data *sdata = rx->sdata;
  1810. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
  1811. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  1812. int len = rx->skb->len;
  1813. if (!ieee80211_is_action(mgmt->frame_control))
  1814. return RX_CONTINUE;
  1815. /* drop too small frames */
  1816. if (len < IEEE80211_MIN_ACTION_SIZE)
  1817. return RX_DROP_UNUSABLE;
  1818. if (!rx->sta && mgmt->u.action.category != WLAN_CATEGORY_PUBLIC)
  1819. return RX_DROP_UNUSABLE;
  1820. if (!(status->rx_flags & IEEE80211_RX_RA_MATCH))
  1821. return RX_DROP_UNUSABLE;
  1822. switch (mgmt->u.action.category) {
  1823. case WLAN_CATEGORY_BACK:
  1824. /*
  1825. * The aggregation code is not prepared to handle
  1826. * anything but STA/AP due to the BSSID handling;
  1827. * IBSS could work in the code but isn't supported
  1828. * by drivers or the standard.
  1829. */
  1830. if (sdata->vif.type != NL80211_IFTYPE_STATION &&
  1831. sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
  1832. sdata->vif.type != NL80211_IFTYPE_AP)
  1833. break;
  1834. /* verify action_code is present */
  1835. if (len < IEEE80211_MIN_ACTION_SIZE + 1)
  1836. break;
  1837. switch (mgmt->u.action.u.addba_req.action_code) {
  1838. case WLAN_ACTION_ADDBA_REQ:
  1839. if (len < (IEEE80211_MIN_ACTION_SIZE +
  1840. sizeof(mgmt->u.action.u.addba_req)))
  1841. goto invalid;
  1842. break;
  1843. case WLAN_ACTION_ADDBA_RESP:
  1844. if (len < (IEEE80211_MIN_ACTION_SIZE +
  1845. sizeof(mgmt->u.action.u.addba_resp)))
  1846. goto invalid;
  1847. break;
  1848. case WLAN_ACTION_DELBA:
  1849. if (len < (IEEE80211_MIN_ACTION_SIZE +
  1850. sizeof(mgmt->u.action.u.delba)))
  1851. goto invalid;
  1852. break;
  1853. default:
  1854. goto invalid;
  1855. }
  1856. goto queue;
  1857. case WLAN_CATEGORY_SPECTRUM_MGMT:
  1858. if (local->hw.conf.channel->band != IEEE80211_BAND_5GHZ)
  1859. break;
  1860. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  1861. break;
  1862. /* verify action_code is present */
  1863. if (len < IEEE80211_MIN_ACTION_SIZE + 1)
  1864. break;
  1865. switch (mgmt->u.action.u.measurement.action_code) {
  1866. case WLAN_ACTION_SPCT_MSR_REQ:
  1867. if (len < (IEEE80211_MIN_ACTION_SIZE +
  1868. sizeof(mgmt->u.action.u.measurement)))
  1869. break;
  1870. ieee80211_process_measurement_req(sdata, mgmt, len);
  1871. goto handled;
  1872. case WLAN_ACTION_SPCT_CHL_SWITCH:
  1873. if (len < (IEEE80211_MIN_ACTION_SIZE +
  1874. sizeof(mgmt->u.action.u.chan_switch)))
  1875. break;
  1876. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  1877. break;
  1878. if (memcmp(mgmt->bssid, sdata->u.mgd.bssid, ETH_ALEN))
  1879. break;
  1880. goto queue;
  1881. }
  1882. break;
  1883. case WLAN_CATEGORY_SA_QUERY:
  1884. if (len < (IEEE80211_MIN_ACTION_SIZE +
  1885. sizeof(mgmt->u.action.u.sa_query)))
  1886. break;
  1887. switch (mgmt->u.action.u.sa_query.action) {
  1888. case WLAN_ACTION_SA_QUERY_REQUEST:
  1889. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  1890. break;
  1891. ieee80211_process_sa_query_req(sdata, mgmt, len);
  1892. goto handled;
  1893. }
  1894. break;
  1895. case WLAN_CATEGORY_SELF_PROTECTED:
  1896. switch (mgmt->u.action.u.self_prot.action_code) {
  1897. case WLAN_SP_MESH_PEERING_OPEN:
  1898. case WLAN_SP_MESH_PEERING_CLOSE:
  1899. case WLAN_SP_MESH_PEERING_CONFIRM:
  1900. if (!ieee80211_vif_is_mesh(&sdata->vif))
  1901. goto invalid;
  1902. if (sdata->u.mesh.security != IEEE80211_MESH_SEC_NONE)
  1903. /* userspace handles this frame */
  1904. break;
  1905. goto queue;
  1906. case WLAN_SP_MGK_INFORM:
  1907. case WLAN_SP_MGK_ACK:
  1908. if (!ieee80211_vif_is_mesh(&sdata->vif))
  1909. goto invalid;
  1910. break;
  1911. }
  1912. break;
  1913. case WLAN_CATEGORY_MESH_ACTION:
  1914. if (!ieee80211_vif_is_mesh(&sdata->vif))
  1915. break;
  1916. if (mesh_action_is_path_sel(mgmt) &&
  1917. (!mesh_path_sel_is_hwmp(sdata)))
  1918. break;
  1919. goto queue;
  1920. }
  1921. return RX_CONTINUE;
  1922. invalid:
  1923. status->rx_flags |= IEEE80211_RX_MALFORMED_ACTION_FRM;
  1924. /* will return in the next handlers */
  1925. return RX_CONTINUE;
  1926. handled:
  1927. if (rx->sta)
  1928. rx->sta->rx_packets++;
  1929. dev_kfree_skb(rx->skb);
  1930. return RX_QUEUED;
  1931. queue:
  1932. rx->skb->pkt_type = IEEE80211_SDATA_QUEUE_TYPE_FRAME;
  1933. skb_queue_tail(&sdata->skb_queue, rx->skb);
  1934. ieee80211_queue_work(&local->hw, &sdata->work);
  1935. if (rx->sta)
  1936. rx->sta->rx_packets++;
  1937. return RX_QUEUED;
  1938. }
  1939. static ieee80211_rx_result debug_noinline
  1940. ieee80211_rx_h_userspace_mgmt(struct ieee80211_rx_data *rx)
  1941. {
  1942. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  1943. /* skip known-bad action frames and return them in the next handler */
  1944. if (status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM)
  1945. return RX_CONTINUE;
  1946. /*
  1947. * Getting here means the kernel doesn't know how to handle
  1948. * it, but maybe userspace does ... include returned frames
  1949. * so userspace can register for those to know whether ones
  1950. * it transmitted were processed or returned.
  1951. */
  1952. if (cfg80211_rx_mgmt(rx->sdata->dev, status->freq,
  1953. rx->skb->data, rx->skb->len,
  1954. GFP_ATOMIC)) {
  1955. if (rx->sta)
  1956. rx->sta->rx_packets++;
  1957. dev_kfree_skb(rx->skb);
  1958. return RX_QUEUED;
  1959. }
  1960. return RX_CONTINUE;
  1961. }
  1962. static ieee80211_rx_result debug_noinline
  1963. ieee80211_rx_h_action_return(struct ieee80211_rx_data *rx)
  1964. {
  1965. struct ieee80211_local *local = rx->local;
  1966. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
  1967. struct sk_buff *nskb;
  1968. struct ieee80211_sub_if_data *sdata = rx->sdata;
  1969. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  1970. if (!ieee80211_is_action(mgmt->frame_control))
  1971. return RX_CONTINUE;
  1972. /*
  1973. * For AP mode, hostapd is responsible for handling any action
  1974. * frames that we didn't handle, including returning unknown
  1975. * ones. For all other modes we will return them to the sender,
  1976. * setting the 0x80 bit in the action category, as required by
  1977. * 802.11-2007 7.3.1.11.
  1978. * Newer versions of hostapd shall also use the management frame
  1979. * registration mechanisms, but older ones still use cooked
  1980. * monitor interfaces so push all frames there.
  1981. */
  1982. if (!(status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM) &&
  1983. (sdata->vif.type == NL80211_IFTYPE_AP ||
  1984. sdata->vif.type == NL80211_IFTYPE_AP_VLAN))
  1985. return RX_DROP_MONITOR;
  1986. /* do not return rejected action frames */
  1987. if (mgmt->u.action.category & 0x80)
  1988. return RX_DROP_UNUSABLE;
  1989. nskb = skb_copy_expand(rx->skb, local->hw.extra_tx_headroom, 0,
  1990. GFP_ATOMIC);
  1991. if (nskb) {
  1992. struct ieee80211_mgmt *nmgmt = (void *)nskb->data;
  1993. nmgmt->u.action.category |= 0x80;
  1994. memcpy(nmgmt->da, nmgmt->sa, ETH_ALEN);
  1995. memcpy(nmgmt->sa, rx->sdata->vif.addr, ETH_ALEN);
  1996. memset(nskb->cb, 0, sizeof(nskb->cb));
  1997. ieee80211_tx_skb(rx->sdata, nskb);
  1998. }
  1999. dev_kfree_skb(rx->skb);
  2000. return RX_QUEUED;
  2001. }
  2002. static ieee80211_rx_result debug_noinline
  2003. ieee80211_rx_h_mgmt(struct ieee80211_rx_data *rx)
  2004. {
  2005. struct ieee80211_sub_if_data *sdata = rx->sdata;
  2006. ieee80211_rx_result rxs;
  2007. struct ieee80211_mgmt *mgmt = (void *)rx->skb->data;
  2008. __le16 stype;
  2009. rxs = ieee80211_work_rx_mgmt(rx->sdata, rx->skb);
  2010. if (rxs != RX_CONTINUE)
  2011. return rxs;
  2012. stype = mgmt->frame_control & cpu_to_le16(IEEE80211_FCTL_STYPE);
  2013. if (!ieee80211_vif_is_mesh(&sdata->vif) &&
  2014. sdata->vif.type != NL80211_IFTYPE_ADHOC &&
  2015. sdata->vif.type != NL80211_IFTYPE_STATION)
  2016. return RX_DROP_MONITOR;
  2017. switch (stype) {
  2018. case cpu_to_le16(IEEE80211_STYPE_BEACON):
  2019. case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
  2020. /* process for all: mesh, mlme, ibss */
  2021. break;
  2022. case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
  2023. case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
  2024. if (is_multicast_ether_addr(mgmt->da) &&
  2025. !is_broadcast_ether_addr(mgmt->da))
  2026. return RX_DROP_MONITOR;
  2027. /* process only for station */
  2028. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  2029. return RX_DROP_MONITOR;
  2030. break;
  2031. case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
  2032. case cpu_to_le16(IEEE80211_STYPE_AUTH):
  2033. /* process only for ibss */
  2034. if (sdata->vif.type != NL80211_IFTYPE_ADHOC)
  2035. return RX_DROP_MONITOR;
  2036. break;
  2037. default:
  2038. return RX_DROP_MONITOR;
  2039. }
  2040. /* queue up frame and kick off work to process it */
  2041. rx->skb->pkt_type = IEEE80211_SDATA_QUEUE_TYPE_FRAME;
  2042. skb_queue_tail(&sdata->skb_queue, rx->skb);
  2043. ieee80211_queue_work(&rx->local->hw, &sdata->work);
  2044. if (rx->sta)
  2045. rx->sta->rx_packets++;
  2046. return RX_QUEUED;
  2047. }
  2048. /* TODO: use IEEE80211_RX_FRAGMENTED */
  2049. static void ieee80211_rx_cooked_monitor(struct ieee80211_rx_data *rx,
  2050. struct ieee80211_rate *rate)
  2051. {
  2052. struct ieee80211_sub_if_data *sdata;
  2053. struct ieee80211_local *local = rx->local;
  2054. struct ieee80211_rtap_hdr {
  2055. struct ieee80211_radiotap_header hdr;
  2056. u8 flags;
  2057. u8 rate_or_pad;
  2058. __le16 chan_freq;
  2059. __le16 chan_flags;
  2060. } __packed *rthdr;
  2061. struct sk_buff *skb = rx->skb, *skb2;
  2062. struct net_device *prev_dev = NULL;
  2063. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  2064. /*
  2065. * If cooked monitor has been processed already, then
  2066. * don't do it again. If not, set the flag.
  2067. */
  2068. if (rx->flags & IEEE80211_RX_CMNTR)
  2069. goto out_free_skb;
  2070. rx->flags |= IEEE80211_RX_CMNTR;
  2071. if (skb_headroom(skb) < sizeof(*rthdr) &&
  2072. pskb_expand_head(skb, sizeof(*rthdr), 0, GFP_ATOMIC))
  2073. goto out_free_skb;
  2074. rthdr = (void *)skb_push(skb, sizeof(*rthdr));
  2075. memset(rthdr, 0, sizeof(*rthdr));
  2076. rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
  2077. rthdr->hdr.it_present =
  2078. cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
  2079. (1 << IEEE80211_RADIOTAP_CHANNEL));
  2080. if (rate) {
  2081. rthdr->rate_or_pad = rate->bitrate / 5;
  2082. rthdr->hdr.it_present |=
  2083. cpu_to_le32(1 << IEEE80211_RADIOTAP_RATE);
  2084. }
  2085. rthdr->chan_freq = cpu_to_le16(status->freq);
  2086. if (status->band == IEEE80211_BAND_5GHZ)
  2087. rthdr->chan_flags = cpu_to_le16(IEEE80211_CHAN_OFDM |
  2088. IEEE80211_CHAN_5GHZ);
  2089. else
  2090. rthdr->chan_flags = cpu_to_le16(IEEE80211_CHAN_DYN |
  2091. IEEE80211_CHAN_2GHZ);
  2092. skb_set_mac_header(skb, 0);
  2093. skb->ip_summed = CHECKSUM_UNNECESSARY;
  2094. skb->pkt_type = PACKET_OTHERHOST;
  2095. skb->protocol = htons(ETH_P_802_2);
  2096. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  2097. if (!ieee80211_sdata_running(sdata))
  2098. continue;
  2099. if (sdata->vif.type != NL80211_IFTYPE_MONITOR ||
  2100. !(sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES))
  2101. continue;
  2102. if (prev_dev) {
  2103. skb2 = skb_clone(skb, GFP_ATOMIC);
  2104. if (skb2) {
  2105. skb2->dev = prev_dev;
  2106. netif_receive_skb(skb2);
  2107. }
  2108. }
  2109. prev_dev = sdata->dev;
  2110. sdata->dev->stats.rx_packets++;
  2111. sdata->dev->stats.rx_bytes += skb->len;
  2112. }
  2113. if (prev_dev) {
  2114. skb->dev = prev_dev;
  2115. netif_receive_skb(skb);
  2116. return;
  2117. }
  2118. out_free_skb:
  2119. dev_kfree_skb(skb);
  2120. }
  2121. static void ieee80211_rx_handlers_result(struct ieee80211_rx_data *rx,
  2122. ieee80211_rx_result res)
  2123. {
  2124. switch (res) {
  2125. case RX_DROP_MONITOR:
  2126. I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
  2127. if (rx->sta)
  2128. rx->sta->rx_dropped++;
  2129. /* fall through */
  2130. case RX_CONTINUE: {
  2131. struct ieee80211_rate *rate = NULL;
  2132. struct ieee80211_supported_band *sband;
  2133. struct ieee80211_rx_status *status;
  2134. status = IEEE80211_SKB_RXCB((rx->skb));
  2135. sband = rx->local->hw.wiphy->bands[status->band];
  2136. if (!(status->flag & RX_FLAG_HT))
  2137. rate = &sband->bitrates[status->rate_idx];
  2138. ieee80211_rx_cooked_monitor(rx, rate);
  2139. break;
  2140. }
  2141. case RX_DROP_UNUSABLE:
  2142. I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
  2143. if (rx->sta)
  2144. rx->sta->rx_dropped++;
  2145. dev_kfree_skb(rx->skb);
  2146. break;
  2147. case RX_QUEUED:
  2148. I802_DEBUG_INC(rx->sdata->local->rx_handlers_queued);
  2149. break;
  2150. }
  2151. }
  2152. static void ieee80211_rx_handlers(struct ieee80211_rx_data *rx)
  2153. {
  2154. ieee80211_rx_result res = RX_DROP_MONITOR;
  2155. struct sk_buff *skb;
  2156. #define CALL_RXH(rxh) \
  2157. do { \
  2158. res = rxh(rx); \
  2159. if (res != RX_CONTINUE) \
  2160. goto rxh_next; \
  2161. } while (0);
  2162. spin_lock(&rx->local->rx_skb_queue.lock);
  2163. if (rx->local->running_rx_handler)
  2164. goto unlock;
  2165. rx->local->running_rx_handler = true;
  2166. while ((skb = __skb_dequeue(&rx->local->rx_skb_queue))) {
  2167. spin_unlock(&rx->local->rx_skb_queue.lock);
  2168. /*
  2169. * all the other fields are valid across frames
  2170. * that belong to an aMPDU since they are on the
  2171. * same TID from the same station
  2172. */
  2173. rx->skb = skb;
  2174. CALL_RXH(ieee80211_rx_h_decrypt)
  2175. CALL_RXH(ieee80211_rx_h_check_more_data)
  2176. CALL_RXH(ieee80211_rx_h_sta_process)
  2177. CALL_RXH(ieee80211_rx_h_defragment)
  2178. CALL_RXH(ieee80211_rx_h_ps_poll)
  2179. CALL_RXH(ieee80211_rx_h_michael_mic_verify)
  2180. /* must be after MMIC verify so header is counted in MPDU mic */
  2181. CALL_RXH(ieee80211_rx_h_remove_qos_control)
  2182. CALL_RXH(ieee80211_rx_h_amsdu)
  2183. #ifdef CONFIG_MAC80211_MESH
  2184. if (ieee80211_vif_is_mesh(&rx->sdata->vif))
  2185. CALL_RXH(ieee80211_rx_h_mesh_fwding);
  2186. #endif
  2187. CALL_RXH(ieee80211_rx_h_data)
  2188. CALL_RXH(ieee80211_rx_h_ctrl);
  2189. CALL_RXH(ieee80211_rx_h_mgmt_check)
  2190. CALL_RXH(ieee80211_rx_h_action)
  2191. CALL_RXH(ieee80211_rx_h_userspace_mgmt)
  2192. CALL_RXH(ieee80211_rx_h_action_return)
  2193. CALL_RXH(ieee80211_rx_h_mgmt)
  2194. rxh_next:
  2195. ieee80211_rx_handlers_result(rx, res);
  2196. spin_lock(&rx->local->rx_skb_queue.lock);
  2197. #undef CALL_RXH
  2198. }
  2199. rx->local->running_rx_handler = false;
  2200. unlock:
  2201. spin_unlock(&rx->local->rx_skb_queue.lock);
  2202. }
  2203. static void ieee80211_invoke_rx_handlers(struct ieee80211_rx_data *rx)
  2204. {
  2205. ieee80211_rx_result res = RX_DROP_MONITOR;
  2206. #define CALL_RXH(rxh) \
  2207. do { \
  2208. res = rxh(rx); \
  2209. if (res != RX_CONTINUE) \
  2210. goto rxh_next; \
  2211. } while (0);
  2212. CALL_RXH(ieee80211_rx_h_passive_scan)
  2213. CALL_RXH(ieee80211_rx_h_check)
  2214. ieee80211_rx_reorder_ampdu(rx);
  2215. ieee80211_rx_handlers(rx);
  2216. return;
  2217. rxh_next:
  2218. ieee80211_rx_handlers_result(rx, res);
  2219. #undef CALL_RXH
  2220. }
  2221. /*
  2222. * This function makes calls into the RX path, therefore
  2223. * it has to be invoked under RCU read lock.
  2224. */
  2225. void ieee80211_release_reorder_timeout(struct sta_info *sta, int tid)
  2226. {
  2227. struct ieee80211_rx_data rx = {
  2228. .sta = sta,
  2229. .sdata = sta->sdata,
  2230. .local = sta->local,
  2231. /* This is OK -- must be QoS data frame */
  2232. .security_idx = tid,
  2233. .seqno_idx = tid,
  2234. .flags = 0,
  2235. };
  2236. struct tid_ampdu_rx *tid_agg_rx;
  2237. tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
  2238. if (!tid_agg_rx)
  2239. return;
  2240. spin_lock(&tid_agg_rx->reorder_lock);
  2241. ieee80211_sta_reorder_release(&sta->local->hw, tid_agg_rx);
  2242. spin_unlock(&tid_agg_rx->reorder_lock);
  2243. ieee80211_rx_handlers(&rx);
  2244. }
  2245. /* main receive path */
  2246. static int prepare_for_handlers(struct ieee80211_rx_data *rx,
  2247. struct ieee80211_hdr *hdr)
  2248. {
  2249. struct ieee80211_sub_if_data *sdata = rx->sdata;
  2250. struct sk_buff *skb = rx->skb;
  2251. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  2252. u8 *bssid = ieee80211_get_bssid(hdr, skb->len, sdata->vif.type);
  2253. int multicast = is_multicast_ether_addr(hdr->addr1);
  2254. switch (sdata->vif.type) {
  2255. case NL80211_IFTYPE_STATION:
  2256. if (!bssid && !sdata->u.mgd.use_4addr)
  2257. return 0;
  2258. if (!multicast &&
  2259. compare_ether_addr(sdata->vif.addr, hdr->addr1) != 0) {
  2260. if (!(sdata->dev->flags & IFF_PROMISC) ||
  2261. sdata->u.mgd.use_4addr)
  2262. return 0;
  2263. status->rx_flags &= ~IEEE80211_RX_RA_MATCH;
  2264. }
  2265. break;
  2266. case NL80211_IFTYPE_ADHOC:
  2267. if (!bssid)
  2268. return 0;
  2269. if (ieee80211_is_beacon(hdr->frame_control)) {
  2270. return 1;
  2271. }
  2272. else if (!ieee80211_bssid_match(bssid, sdata->u.ibss.bssid)) {
  2273. if (!(status->rx_flags & IEEE80211_RX_IN_SCAN))
  2274. return 0;
  2275. status->rx_flags &= ~IEEE80211_RX_RA_MATCH;
  2276. } else if (!multicast &&
  2277. compare_ether_addr(sdata->vif.addr,
  2278. hdr->addr1) != 0) {
  2279. if (!(sdata->dev->flags & IFF_PROMISC))
  2280. return 0;
  2281. status->rx_flags &= ~IEEE80211_RX_RA_MATCH;
  2282. } else if (!rx->sta) {
  2283. int rate_idx;
  2284. if (status->flag & RX_FLAG_HT)
  2285. rate_idx = 0; /* TODO: HT rates */
  2286. else
  2287. rate_idx = status->rate_idx;
  2288. rx->sta = ieee80211_ibss_add_sta(sdata, bssid,
  2289. hdr->addr2, BIT(rate_idx), GFP_ATOMIC);
  2290. }
  2291. break;
  2292. case NL80211_IFTYPE_MESH_POINT:
  2293. if (!multicast &&
  2294. compare_ether_addr(sdata->vif.addr,
  2295. hdr->addr1) != 0) {
  2296. if (!(sdata->dev->flags & IFF_PROMISC))
  2297. return 0;
  2298. status->rx_flags &= ~IEEE80211_RX_RA_MATCH;
  2299. }
  2300. break;
  2301. case NL80211_IFTYPE_AP_VLAN:
  2302. case NL80211_IFTYPE_AP:
  2303. if (!bssid) {
  2304. if (compare_ether_addr(sdata->vif.addr,
  2305. hdr->addr1))
  2306. return 0;
  2307. } else if (!ieee80211_bssid_match(bssid,
  2308. sdata->vif.addr)) {
  2309. if (!(status->rx_flags & IEEE80211_RX_IN_SCAN) &&
  2310. !ieee80211_is_beacon(hdr->frame_control) &&
  2311. !(ieee80211_is_action(hdr->frame_control) &&
  2312. sdata->vif.p2p))
  2313. return 0;
  2314. status->rx_flags &= ~IEEE80211_RX_RA_MATCH;
  2315. }
  2316. break;
  2317. case NL80211_IFTYPE_WDS:
  2318. if (bssid || !ieee80211_is_data(hdr->frame_control))
  2319. return 0;
  2320. if (compare_ether_addr(sdata->u.wds.remote_addr, hdr->addr2))
  2321. return 0;
  2322. break;
  2323. default:
  2324. /* should never get here */
  2325. WARN_ON(1);
  2326. break;
  2327. }
  2328. return 1;
  2329. }
  2330. /*
  2331. * This function returns whether or not the SKB
  2332. * was destined for RX processing or not, which,
  2333. * if consume is true, is equivalent to whether
  2334. * or not the skb was consumed.
  2335. */
  2336. static bool ieee80211_prepare_and_rx_handle(struct ieee80211_rx_data *rx,
  2337. struct sk_buff *skb, bool consume)
  2338. {
  2339. struct ieee80211_local *local = rx->local;
  2340. struct ieee80211_sub_if_data *sdata = rx->sdata;
  2341. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  2342. struct ieee80211_hdr *hdr = (void *)skb->data;
  2343. int prepares;
  2344. rx->skb = skb;
  2345. status->rx_flags |= IEEE80211_RX_RA_MATCH;
  2346. prepares = prepare_for_handlers(rx, hdr);
  2347. if (!prepares)
  2348. return false;
  2349. if (!consume) {
  2350. skb = skb_copy(skb, GFP_ATOMIC);
  2351. if (!skb) {
  2352. if (net_ratelimit())
  2353. wiphy_debug(local->hw.wiphy,
  2354. "failed to copy skb for %s\n",
  2355. sdata->name);
  2356. return true;
  2357. }
  2358. rx->skb = skb;
  2359. }
  2360. ieee80211_invoke_rx_handlers(rx);
  2361. return true;
  2362. }
  2363. /*
  2364. * This is the actual Rx frames handler. as it blongs to Rx path it must
  2365. * be called with rcu_read_lock protection.
  2366. */
  2367. static void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw,
  2368. struct sk_buff *skb)
  2369. {
  2370. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  2371. struct ieee80211_local *local = hw_to_local(hw);
  2372. struct ieee80211_sub_if_data *sdata;
  2373. struct ieee80211_hdr *hdr;
  2374. __le16 fc;
  2375. struct ieee80211_rx_data rx;
  2376. struct ieee80211_sub_if_data *prev;
  2377. struct sta_info *sta, *tmp, *prev_sta;
  2378. int err = 0;
  2379. fc = ((struct ieee80211_hdr *)skb->data)->frame_control;
  2380. memset(&rx, 0, sizeof(rx));
  2381. rx.skb = skb;
  2382. rx.local = local;
  2383. if (ieee80211_is_data(fc) || ieee80211_is_mgmt(fc))
  2384. local->dot11ReceivedFragmentCount++;
  2385. if (unlikely(test_bit(SCAN_HW_SCANNING, &local->scanning) ||
  2386. test_bit(SCAN_SW_SCANNING, &local->scanning)))
  2387. status->rx_flags |= IEEE80211_RX_IN_SCAN;
  2388. if (ieee80211_is_mgmt(fc))
  2389. err = skb_linearize(skb);
  2390. else
  2391. err = !pskb_may_pull(skb, ieee80211_hdrlen(fc));
  2392. if (err) {
  2393. dev_kfree_skb(skb);
  2394. return;
  2395. }
  2396. hdr = (struct ieee80211_hdr *)skb->data;
  2397. ieee80211_parse_qos(&rx);
  2398. ieee80211_verify_alignment(&rx);
  2399. if (ieee80211_is_data(fc)) {
  2400. prev_sta = NULL;
  2401. for_each_sta_info_rx(local, hdr->addr2, sta, tmp) {
  2402. if (!prev_sta) {
  2403. prev_sta = sta;
  2404. continue;
  2405. }
  2406. rx.sta = prev_sta;
  2407. rx.sdata = prev_sta->sdata;
  2408. ieee80211_prepare_and_rx_handle(&rx, skb, false);
  2409. prev_sta = sta;
  2410. }
  2411. if (prev_sta) {
  2412. rx.sta = prev_sta;
  2413. rx.sdata = prev_sta->sdata;
  2414. if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
  2415. return;
  2416. goto out;
  2417. }
  2418. }
  2419. prev = NULL;
  2420. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  2421. if (!ieee80211_sdata_running(sdata))
  2422. continue;
  2423. if (sdata->vif.type == NL80211_IFTYPE_MONITOR ||
  2424. sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  2425. continue;
  2426. /*
  2427. * frame is destined for this interface, but if it's
  2428. * not also for the previous one we handle that after
  2429. * the loop to avoid copying the SKB once too much
  2430. */
  2431. if (!prev) {
  2432. prev = sdata;
  2433. continue;
  2434. }
  2435. rx.sta = sta_info_get_bss_rx(prev, hdr->addr2);
  2436. rx.sdata = prev;
  2437. ieee80211_prepare_and_rx_handle(&rx, skb, false);
  2438. prev = sdata;
  2439. }
  2440. if (prev) {
  2441. rx.sta = sta_info_get_bss_rx(prev, hdr->addr2);
  2442. rx.sdata = prev;
  2443. if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
  2444. return;
  2445. }
  2446. out:
  2447. dev_kfree_skb(skb);
  2448. }
  2449. /*
  2450. * This is the receive path handler. It is called by a low level driver when an
  2451. * 802.11 MPDU is received from the hardware.
  2452. */
  2453. void ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb)
  2454. {
  2455. struct ieee80211_local *local = hw_to_local(hw);
  2456. struct ieee80211_rate *rate = NULL;
  2457. struct ieee80211_supported_band *sband;
  2458. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  2459. WARN_ON_ONCE(softirq_count() == 0);
  2460. if (WARN_ON(status->band < 0 ||
  2461. status->band >= IEEE80211_NUM_BANDS))
  2462. goto drop;
  2463. sband = local->hw.wiphy->bands[status->band];
  2464. if (WARN_ON(!sband))
  2465. goto drop;
  2466. /*
  2467. * If we're suspending, it is possible although not too likely
  2468. * that we'd be receiving frames after having already partially
  2469. * quiesced the stack. We can't process such frames then since
  2470. * that might, for example, cause stations to be added or other
  2471. * driver callbacks be invoked.
  2472. */
  2473. if (unlikely(local->quiescing || local->suspended))
  2474. goto drop;
  2475. /*
  2476. * The same happens when we're not even started,
  2477. * but that's worth a warning.
  2478. */
  2479. if (WARN_ON(!local->started))
  2480. goto drop;
  2481. if (likely(!(status->flag & RX_FLAG_FAILED_PLCP_CRC))) {
  2482. /*
  2483. * Validate the rate, unless a PLCP error means that
  2484. * we probably can't have a valid rate here anyway.
  2485. */
  2486. if (status->flag & RX_FLAG_HT) {
  2487. /*
  2488. * rate_idx is MCS index, which can be [0-76]
  2489. * as documented on:
  2490. *
  2491. * http://wireless.kernel.org/en/developers/Documentation/ieee80211/802.11n
  2492. *
  2493. * Anything else would be some sort of driver or
  2494. * hardware error. The driver should catch hardware
  2495. * errors.
  2496. */
  2497. if (WARN((status->rate_idx < 0 ||
  2498. status->rate_idx > 76),
  2499. "Rate marked as an HT rate but passed "
  2500. "status->rate_idx is not "
  2501. "an MCS index [0-76]: %d (0x%02x)\n",
  2502. status->rate_idx,
  2503. status->rate_idx))
  2504. goto drop;
  2505. } else {
  2506. if (WARN_ON(status->rate_idx < 0 ||
  2507. status->rate_idx >= sband->n_bitrates))
  2508. goto drop;
  2509. rate = &sband->bitrates[status->rate_idx];
  2510. }
  2511. }
  2512. status->rx_flags = 0;
  2513. /*
  2514. * key references and virtual interfaces are protected using RCU
  2515. * and this requires that we are in a read-side RCU section during
  2516. * receive processing
  2517. */
  2518. rcu_read_lock();
  2519. /*
  2520. * Frames with failed FCS/PLCP checksum are not returned,
  2521. * all other frames are returned without radiotap header
  2522. * if it was previously present.
  2523. * Also, frames with less than 16 bytes are dropped.
  2524. */
  2525. skb = ieee80211_rx_monitor(local, skb, rate);
  2526. if (!skb) {
  2527. rcu_read_unlock();
  2528. return;
  2529. }
  2530. ieee80211_tpt_led_trig_rx(local,
  2531. ((struct ieee80211_hdr *)skb->data)->frame_control,
  2532. skb->len);
  2533. __ieee80211_rx_handle_packet(hw, skb);
  2534. rcu_read_unlock();
  2535. return;
  2536. drop:
  2537. kfree_skb(skb);
  2538. }
  2539. EXPORT_SYMBOL(ieee80211_rx);
  2540. /* This is a version of the rx handler that can be called from hard irq
  2541. * context. Post the skb on the queue and schedule the tasklet */
  2542. void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb)
  2543. {
  2544. struct ieee80211_local *local = hw_to_local(hw);
  2545. BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
  2546. skb->pkt_type = IEEE80211_RX_MSG;
  2547. skb_queue_tail(&local->skb_queue, skb);
  2548. tasklet_schedule(&local->tasklet);
  2549. }
  2550. EXPORT_SYMBOL(ieee80211_rx_irqsafe);