rx.c 70 KB

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