rx.c 82 KB

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