rx.c 71 KB

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