rx.c 69 KB

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