rx.c 70 KB

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