rx.c 80 KB

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