rx.c 70 KB

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