rx.c 82 KB

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