rx.c 59 KB

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