rx.c 59 KB

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