rx.c 59 KB

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