rx.c 81 KB

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