rx.c 80 KB

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