tx.c 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217
  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. *
  12. * Transmit and frame generation functions.
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/slab.h>
  16. #include <linux/skbuff.h>
  17. #include <linux/etherdevice.h>
  18. #include <linux/bitmap.h>
  19. #include <linux/rcupdate.h>
  20. #include <net/net_namespace.h>
  21. #include <net/ieee80211_radiotap.h>
  22. #include <net/cfg80211.h>
  23. #include <net/mac80211.h>
  24. #include <asm/unaligned.h>
  25. #include "ieee80211_i.h"
  26. #include "driver-ops.h"
  27. #include "led.h"
  28. #include "mesh.h"
  29. #include "wep.h"
  30. #include "wpa.h"
  31. #include "wme.h"
  32. #include "rate.h"
  33. #define IEEE80211_TX_OK 0
  34. #define IEEE80211_TX_AGAIN 1
  35. #define IEEE80211_TX_PENDING 2
  36. /* misc utils */
  37. static __le16 ieee80211_duration(struct ieee80211_tx_data *tx, int group_addr,
  38. int next_frag_len)
  39. {
  40. int rate, mrate, erp, dur, i;
  41. struct ieee80211_rate *txrate;
  42. struct ieee80211_local *local = tx->local;
  43. struct ieee80211_supported_band *sband;
  44. struct ieee80211_hdr *hdr;
  45. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
  46. /* assume HW handles this */
  47. if (info->control.rates[0].flags & IEEE80211_TX_RC_MCS)
  48. return 0;
  49. /* uh huh? */
  50. if (WARN_ON_ONCE(info->control.rates[0].idx < 0))
  51. return 0;
  52. sband = local->hw.wiphy->bands[tx->channel->band];
  53. txrate = &sband->bitrates[info->control.rates[0].idx];
  54. erp = txrate->flags & IEEE80211_RATE_ERP_G;
  55. /*
  56. * data and mgmt (except PS Poll):
  57. * - during CFP: 32768
  58. * - during contention period:
  59. * if addr1 is group address: 0
  60. * if more fragments = 0 and addr1 is individual address: time to
  61. * transmit one ACK plus SIFS
  62. * if more fragments = 1 and addr1 is individual address: time to
  63. * transmit next fragment plus 2 x ACK plus 3 x SIFS
  64. *
  65. * IEEE 802.11, 9.6:
  66. * - control response frame (CTS or ACK) shall be transmitted using the
  67. * same rate as the immediately previous frame in the frame exchange
  68. * sequence, if this rate belongs to the PHY mandatory rates, or else
  69. * at the highest possible rate belonging to the PHY rates in the
  70. * BSSBasicRateSet
  71. */
  72. hdr = (struct ieee80211_hdr *)tx->skb->data;
  73. if (ieee80211_is_ctl(hdr->frame_control)) {
  74. /* TODO: These control frames are not currently sent by
  75. * mac80211, but should they be implemented, this function
  76. * needs to be updated to support duration field calculation.
  77. *
  78. * RTS: time needed to transmit pending data/mgmt frame plus
  79. * one CTS frame plus one ACK frame plus 3 x SIFS
  80. * CTS: duration of immediately previous RTS minus time
  81. * required to transmit CTS and its SIFS
  82. * ACK: 0 if immediately previous directed data/mgmt had
  83. * more=0, with more=1 duration in ACK frame is duration
  84. * from previous frame minus time needed to transmit ACK
  85. * and its SIFS
  86. * PS Poll: BIT(15) | BIT(14) | aid
  87. */
  88. return 0;
  89. }
  90. /* data/mgmt */
  91. if (0 /* FIX: data/mgmt during CFP */)
  92. return cpu_to_le16(32768);
  93. if (group_addr) /* Group address as the destination - no ACK */
  94. return 0;
  95. /* Individual destination address:
  96. * IEEE 802.11, Ch. 9.6 (after IEEE 802.11g changes)
  97. * CTS and ACK frames shall be transmitted using the highest rate in
  98. * basic rate set that is less than or equal to the rate of the
  99. * immediately previous frame and that is using the same modulation
  100. * (CCK or OFDM). If no basic rate set matches with these requirements,
  101. * the highest mandatory rate of the PHY that is less than or equal to
  102. * the rate of the previous frame is used.
  103. * Mandatory rates for IEEE 802.11g PHY: 1, 2, 5.5, 11, 6, 12, 24 Mbps
  104. */
  105. rate = -1;
  106. /* use lowest available if everything fails */
  107. mrate = sband->bitrates[0].bitrate;
  108. for (i = 0; i < sband->n_bitrates; i++) {
  109. struct ieee80211_rate *r = &sband->bitrates[i];
  110. if (r->bitrate > txrate->bitrate)
  111. break;
  112. if (tx->sdata->vif.bss_conf.basic_rates & BIT(i))
  113. rate = r->bitrate;
  114. switch (sband->band) {
  115. case IEEE80211_BAND_2GHZ: {
  116. u32 flag;
  117. if (tx->sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
  118. flag = IEEE80211_RATE_MANDATORY_G;
  119. else
  120. flag = IEEE80211_RATE_MANDATORY_B;
  121. if (r->flags & flag)
  122. mrate = r->bitrate;
  123. break;
  124. }
  125. case IEEE80211_BAND_5GHZ:
  126. if (r->flags & IEEE80211_RATE_MANDATORY_A)
  127. mrate = r->bitrate;
  128. break;
  129. case IEEE80211_NUM_BANDS:
  130. WARN_ON(1);
  131. break;
  132. }
  133. }
  134. if (rate == -1) {
  135. /* No matching basic rate found; use highest suitable mandatory
  136. * PHY rate */
  137. rate = mrate;
  138. }
  139. /* Time needed to transmit ACK
  140. * (10 bytes + 4-byte FCS = 112 bits) plus SIFS; rounded up
  141. * to closest integer */
  142. dur = ieee80211_frame_duration(local, 10, rate, erp,
  143. tx->sdata->vif.bss_conf.use_short_preamble);
  144. if (next_frag_len) {
  145. /* Frame is fragmented: duration increases with time needed to
  146. * transmit next fragment plus ACK and 2 x SIFS. */
  147. dur *= 2; /* ACK + SIFS */
  148. /* next fragment */
  149. dur += ieee80211_frame_duration(local, next_frag_len,
  150. txrate->bitrate, erp,
  151. tx->sdata->vif.bss_conf.use_short_preamble);
  152. }
  153. return cpu_to_le16(dur);
  154. }
  155. static int inline is_ieee80211_device(struct ieee80211_local *local,
  156. struct net_device *dev)
  157. {
  158. return local == wdev_priv(dev->ieee80211_ptr);
  159. }
  160. /* tx handlers */
  161. static ieee80211_tx_result debug_noinline
  162. ieee80211_tx_h_check_assoc(struct ieee80211_tx_data *tx)
  163. {
  164. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
  165. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
  166. u32 sta_flags;
  167. if (unlikely(info->flags & IEEE80211_TX_CTL_INJECTED))
  168. return TX_CONTINUE;
  169. if (unlikely(test_bit(SCAN_OFF_CHANNEL, &tx->local->scanning)) &&
  170. !ieee80211_is_probe_req(hdr->frame_control) &&
  171. !ieee80211_is_nullfunc(hdr->frame_control))
  172. /*
  173. * When software scanning only nullfunc frames (to notify
  174. * the sleep state to the AP) and probe requests (for the
  175. * active scan) are allowed, all other frames should not be
  176. * sent and we should not get here, but if we do
  177. * nonetheless, drop them to avoid sending them
  178. * off-channel. See the link below and
  179. * ieee80211_start_scan() for more.
  180. *
  181. * http://article.gmane.org/gmane.linux.kernel.wireless.general/30089
  182. */
  183. return TX_DROP;
  184. if (tx->sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
  185. return TX_CONTINUE;
  186. if (tx->flags & IEEE80211_TX_PS_BUFFERED)
  187. return TX_CONTINUE;
  188. sta_flags = tx->sta ? get_sta_flags(tx->sta) : 0;
  189. if (likely(tx->flags & IEEE80211_TX_UNICAST)) {
  190. if (unlikely(!(sta_flags & WLAN_STA_ASSOC) &&
  191. tx->sdata->vif.type != NL80211_IFTYPE_ADHOC &&
  192. ieee80211_is_data(hdr->frame_control))) {
  193. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  194. printk(KERN_DEBUG "%s: dropped data frame to not "
  195. "associated station %pM\n",
  196. tx->dev->name, hdr->addr1);
  197. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  198. I802_DEBUG_INC(tx->local->tx_handlers_drop_not_assoc);
  199. return TX_DROP;
  200. }
  201. } else {
  202. if (unlikely(ieee80211_is_data(hdr->frame_control) &&
  203. tx->local->num_sta == 0 &&
  204. tx->sdata->vif.type != NL80211_IFTYPE_ADHOC)) {
  205. /*
  206. * No associated STAs - no need to send multicast
  207. * frames.
  208. */
  209. return TX_DROP;
  210. }
  211. return TX_CONTINUE;
  212. }
  213. return TX_CONTINUE;
  214. }
  215. /* This function is called whenever the AP is about to exceed the maximum limit
  216. * of buffered frames for power saving STAs. This situation should not really
  217. * happen often during normal operation, so dropping the oldest buffered packet
  218. * from each queue should be OK to make some room for new frames. */
  219. static void purge_old_ps_buffers(struct ieee80211_local *local)
  220. {
  221. int total = 0, purged = 0;
  222. struct sk_buff *skb;
  223. struct ieee80211_sub_if_data *sdata;
  224. struct sta_info *sta;
  225. /*
  226. * virtual interfaces are protected by RCU
  227. */
  228. rcu_read_lock();
  229. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  230. struct ieee80211_if_ap *ap;
  231. if (sdata->vif.type != NL80211_IFTYPE_AP)
  232. continue;
  233. ap = &sdata->u.ap;
  234. skb = skb_dequeue(&ap->ps_bc_buf);
  235. if (skb) {
  236. purged++;
  237. dev_kfree_skb(skb);
  238. }
  239. total += skb_queue_len(&ap->ps_bc_buf);
  240. }
  241. list_for_each_entry_rcu(sta, &local->sta_list, list) {
  242. skb = skb_dequeue(&sta->ps_tx_buf);
  243. if (skb) {
  244. purged++;
  245. dev_kfree_skb(skb);
  246. }
  247. total += skb_queue_len(&sta->ps_tx_buf);
  248. }
  249. rcu_read_unlock();
  250. local->total_ps_buffered = total;
  251. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  252. printk(KERN_DEBUG "%s: PS buffers full - purged %d frames\n",
  253. wiphy_name(local->hw.wiphy), purged);
  254. #endif
  255. }
  256. static ieee80211_tx_result
  257. ieee80211_tx_h_multicast_ps_buf(struct ieee80211_tx_data *tx)
  258. {
  259. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
  260. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
  261. /*
  262. * broadcast/multicast frame
  263. *
  264. * If any of the associated stations is in power save mode,
  265. * the frame is buffered to be sent after DTIM beacon frame.
  266. * This is done either by the hardware or us.
  267. */
  268. /* powersaving STAs only in AP/VLAN mode */
  269. if (!tx->sdata->bss)
  270. return TX_CONTINUE;
  271. /* no buffering for ordered frames */
  272. if (ieee80211_has_order(hdr->frame_control))
  273. return TX_CONTINUE;
  274. /* no stations in PS mode */
  275. if (!atomic_read(&tx->sdata->bss->num_sta_ps))
  276. return TX_CONTINUE;
  277. /* buffered in mac80211 */
  278. if (tx->local->hw.flags & IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING) {
  279. if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
  280. purge_old_ps_buffers(tx->local);
  281. if (skb_queue_len(&tx->sdata->bss->ps_bc_buf) >=
  282. AP_MAX_BC_BUFFER) {
  283. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  284. if (net_ratelimit()) {
  285. printk(KERN_DEBUG "%s: BC TX buffer full - "
  286. "dropping the oldest frame\n",
  287. tx->dev->name);
  288. }
  289. #endif
  290. dev_kfree_skb(skb_dequeue(&tx->sdata->bss->ps_bc_buf));
  291. } else
  292. tx->local->total_ps_buffered++;
  293. skb_queue_tail(&tx->sdata->bss->ps_bc_buf, tx->skb);
  294. return TX_QUEUED;
  295. }
  296. /* buffered in hardware */
  297. info->flags |= IEEE80211_TX_CTL_SEND_AFTER_DTIM;
  298. return TX_CONTINUE;
  299. }
  300. static int ieee80211_use_mfp(__le16 fc, struct sta_info *sta,
  301. struct sk_buff *skb)
  302. {
  303. if (!ieee80211_is_mgmt(fc))
  304. return 0;
  305. if (sta == NULL || !test_sta_flags(sta, WLAN_STA_MFP))
  306. return 0;
  307. if (!ieee80211_is_robust_mgmt_frame((struct ieee80211_hdr *)
  308. skb->data))
  309. return 0;
  310. return 1;
  311. }
  312. static ieee80211_tx_result
  313. ieee80211_tx_h_unicast_ps_buf(struct ieee80211_tx_data *tx)
  314. {
  315. struct sta_info *sta = tx->sta;
  316. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
  317. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
  318. u32 staflags;
  319. if (unlikely(!sta || ieee80211_is_probe_resp(hdr->frame_control)))
  320. return TX_CONTINUE;
  321. staflags = get_sta_flags(sta);
  322. if (unlikely((staflags & WLAN_STA_PS) &&
  323. !(info->flags & IEEE80211_TX_CTL_PSPOLL_RESPONSE))) {
  324. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  325. printk(KERN_DEBUG "STA %pM aid %d: PS buffer (entries "
  326. "before %d)\n",
  327. sta->sta.addr, sta->sta.aid,
  328. skb_queue_len(&sta->ps_tx_buf));
  329. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  330. if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
  331. purge_old_ps_buffers(tx->local);
  332. if (skb_queue_len(&sta->ps_tx_buf) >= STA_MAX_TX_BUFFER) {
  333. struct sk_buff *old = skb_dequeue(&sta->ps_tx_buf);
  334. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  335. if (net_ratelimit()) {
  336. printk(KERN_DEBUG "%s: STA %pM TX "
  337. "buffer full - dropping oldest frame\n",
  338. tx->dev->name, sta->sta.addr);
  339. }
  340. #endif
  341. dev_kfree_skb(old);
  342. } else
  343. tx->local->total_ps_buffered++;
  344. /* Queue frame to be sent after STA sends an PS Poll frame */
  345. if (skb_queue_empty(&sta->ps_tx_buf))
  346. sta_info_set_tim_bit(sta);
  347. info->control.jiffies = jiffies;
  348. info->control.vif = &tx->sdata->vif;
  349. info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
  350. skb_queue_tail(&sta->ps_tx_buf, tx->skb);
  351. return TX_QUEUED;
  352. }
  353. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  354. else if (unlikely(test_sta_flags(sta, WLAN_STA_PS))) {
  355. printk(KERN_DEBUG "%s: STA %pM in PS mode, but pspoll "
  356. "set -> send frame\n", tx->dev->name,
  357. sta->sta.addr);
  358. }
  359. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  360. return TX_CONTINUE;
  361. }
  362. static ieee80211_tx_result debug_noinline
  363. ieee80211_tx_h_ps_buf(struct ieee80211_tx_data *tx)
  364. {
  365. if (unlikely(tx->flags & IEEE80211_TX_PS_BUFFERED))
  366. return TX_CONTINUE;
  367. if (tx->flags & IEEE80211_TX_UNICAST)
  368. return ieee80211_tx_h_unicast_ps_buf(tx);
  369. else
  370. return ieee80211_tx_h_multicast_ps_buf(tx);
  371. }
  372. static ieee80211_tx_result debug_noinline
  373. ieee80211_tx_h_select_key(struct ieee80211_tx_data *tx)
  374. {
  375. struct ieee80211_key *key = NULL;
  376. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
  377. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
  378. if (unlikely(info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT))
  379. tx->key = NULL;
  380. else if (tx->sta && (key = rcu_dereference(tx->sta->key)))
  381. tx->key = key;
  382. else if (ieee80211_is_mgmt(hdr->frame_control) &&
  383. (key = rcu_dereference(tx->sdata->default_mgmt_key)))
  384. tx->key = key;
  385. else if ((key = rcu_dereference(tx->sdata->default_key)))
  386. tx->key = key;
  387. else if (tx->sdata->drop_unencrypted &&
  388. (tx->skb->protocol != cpu_to_be16(ETH_P_PAE)) &&
  389. !(info->flags & IEEE80211_TX_CTL_INJECTED) &&
  390. (!ieee80211_is_robust_mgmt_frame(hdr) ||
  391. (ieee80211_is_action(hdr->frame_control) &&
  392. tx->sta && test_sta_flags(tx->sta, WLAN_STA_MFP)))) {
  393. I802_DEBUG_INC(tx->local->tx_handlers_drop_unencrypted);
  394. return TX_DROP;
  395. } else
  396. tx->key = NULL;
  397. if (tx->key) {
  398. tx->key->tx_rx_count++;
  399. /* TODO: add threshold stuff again */
  400. switch (tx->key->conf.alg) {
  401. case ALG_WEP:
  402. if (ieee80211_is_auth(hdr->frame_control))
  403. break;
  404. case ALG_TKIP:
  405. if (!ieee80211_is_data_present(hdr->frame_control))
  406. tx->key = NULL;
  407. break;
  408. case ALG_CCMP:
  409. if (!ieee80211_is_data_present(hdr->frame_control) &&
  410. !ieee80211_use_mfp(hdr->frame_control, tx->sta,
  411. tx->skb))
  412. tx->key = NULL;
  413. break;
  414. case ALG_AES_CMAC:
  415. if (!ieee80211_is_mgmt(hdr->frame_control))
  416. tx->key = NULL;
  417. break;
  418. }
  419. }
  420. if (!tx->key || !(tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
  421. info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  422. return TX_CONTINUE;
  423. }
  424. static ieee80211_tx_result debug_noinline
  425. ieee80211_tx_h_rate_ctrl(struct ieee80211_tx_data *tx)
  426. {
  427. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
  428. struct ieee80211_hdr *hdr = (void *)tx->skb->data;
  429. struct ieee80211_supported_band *sband;
  430. struct ieee80211_rate *rate;
  431. int i, len;
  432. bool inval = false, rts = false, short_preamble = false;
  433. struct ieee80211_tx_rate_control txrc;
  434. u32 sta_flags;
  435. memset(&txrc, 0, sizeof(txrc));
  436. sband = tx->local->hw.wiphy->bands[tx->channel->band];
  437. len = min_t(int, tx->skb->len + FCS_LEN,
  438. tx->local->hw.wiphy->frag_threshold);
  439. /* set up the tx rate control struct we give the RC algo */
  440. txrc.hw = local_to_hw(tx->local);
  441. txrc.sband = sband;
  442. txrc.bss_conf = &tx->sdata->vif.bss_conf;
  443. txrc.skb = tx->skb;
  444. txrc.reported_rate.idx = -1;
  445. txrc.max_rate_idx = tx->sdata->max_ratectrl_rateidx;
  446. /* set up RTS protection if desired */
  447. if (len > tx->local->hw.wiphy->rts_threshold) {
  448. txrc.rts = rts = true;
  449. }
  450. /*
  451. * Use short preamble if the BSS can handle it, but not for
  452. * management frames unless we know the receiver can handle
  453. * that -- the management frame might be to a station that
  454. * just wants a probe response.
  455. */
  456. if (tx->sdata->vif.bss_conf.use_short_preamble &&
  457. (ieee80211_is_data(hdr->frame_control) ||
  458. (tx->sta && test_sta_flags(tx->sta, WLAN_STA_SHORT_PREAMBLE))))
  459. txrc.short_preamble = short_preamble = true;
  460. sta_flags = tx->sta ? get_sta_flags(tx->sta) : 0;
  461. /*
  462. * Lets not bother rate control if we're associated and cannot
  463. * talk to the sta. This should not happen.
  464. */
  465. if (WARN(test_bit(SCAN_SW_SCANNING, &tx->local->scanning) &&
  466. (sta_flags & WLAN_STA_ASSOC) &&
  467. !rate_usable_index_exists(sband, &tx->sta->sta),
  468. "%s: Dropped data frame as no usable bitrate found while "
  469. "scanning and associated. Target station: "
  470. "%pM on %d GHz band\n",
  471. tx->dev->name, hdr->addr1,
  472. tx->channel->band ? 5 : 2))
  473. return TX_DROP;
  474. /*
  475. * If we're associated with the sta at this point we know we can at
  476. * least send the frame at the lowest bit rate.
  477. */
  478. rate_control_get_rate(tx->sdata, tx->sta, &txrc);
  479. if (unlikely(info->control.rates[0].idx < 0))
  480. return TX_DROP;
  481. if (txrc.reported_rate.idx < 0)
  482. txrc.reported_rate = info->control.rates[0];
  483. if (tx->sta)
  484. tx->sta->last_tx_rate = txrc.reported_rate;
  485. if (unlikely(!info->control.rates[0].count))
  486. info->control.rates[0].count = 1;
  487. if (WARN_ON_ONCE((info->control.rates[0].count > 1) &&
  488. (info->flags & IEEE80211_TX_CTL_NO_ACK)))
  489. info->control.rates[0].count = 1;
  490. if (is_multicast_ether_addr(hdr->addr1)) {
  491. /*
  492. * XXX: verify the rate is in the basic rateset
  493. */
  494. return TX_CONTINUE;
  495. }
  496. /*
  497. * set up the RTS/CTS rate as the fastest basic rate
  498. * that is not faster than the data rate
  499. *
  500. * XXX: Should this check all retry rates?
  501. */
  502. if (!(info->control.rates[0].flags & IEEE80211_TX_RC_MCS)) {
  503. s8 baserate = 0;
  504. rate = &sband->bitrates[info->control.rates[0].idx];
  505. for (i = 0; i < sband->n_bitrates; i++) {
  506. /* must be a basic rate */
  507. if (!(tx->sdata->vif.bss_conf.basic_rates & BIT(i)))
  508. continue;
  509. /* must not be faster than the data rate */
  510. if (sband->bitrates[i].bitrate > rate->bitrate)
  511. continue;
  512. /* maximum */
  513. if (sband->bitrates[baserate].bitrate <
  514. sband->bitrates[i].bitrate)
  515. baserate = i;
  516. }
  517. info->control.rts_cts_rate_idx = baserate;
  518. }
  519. for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
  520. /*
  521. * make sure there's no valid rate following
  522. * an invalid one, just in case drivers don't
  523. * take the API seriously to stop at -1.
  524. */
  525. if (inval) {
  526. info->control.rates[i].idx = -1;
  527. continue;
  528. }
  529. if (info->control.rates[i].idx < 0) {
  530. inval = true;
  531. continue;
  532. }
  533. /*
  534. * For now assume MCS is already set up correctly, this
  535. * needs to be fixed.
  536. */
  537. if (info->control.rates[i].flags & IEEE80211_TX_RC_MCS) {
  538. WARN_ON(info->control.rates[i].idx > 76);
  539. continue;
  540. }
  541. /* set up RTS protection if desired */
  542. if (rts)
  543. info->control.rates[i].flags |=
  544. IEEE80211_TX_RC_USE_RTS_CTS;
  545. /* RC is busted */
  546. if (WARN_ON_ONCE(info->control.rates[i].idx >=
  547. sband->n_bitrates)) {
  548. info->control.rates[i].idx = -1;
  549. continue;
  550. }
  551. rate = &sband->bitrates[info->control.rates[i].idx];
  552. /* set up short preamble */
  553. if (short_preamble &&
  554. rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
  555. info->control.rates[i].flags |=
  556. IEEE80211_TX_RC_USE_SHORT_PREAMBLE;
  557. /* set up G protection */
  558. if (!rts && tx->sdata->vif.bss_conf.use_cts_prot &&
  559. rate->flags & IEEE80211_RATE_ERP_G)
  560. info->control.rates[i].flags |=
  561. IEEE80211_TX_RC_USE_CTS_PROTECT;
  562. }
  563. return TX_CONTINUE;
  564. }
  565. static ieee80211_tx_result debug_noinline
  566. ieee80211_tx_h_misc(struct ieee80211_tx_data *tx)
  567. {
  568. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
  569. if (tx->sta)
  570. info->control.sta = &tx->sta->sta;
  571. return TX_CONTINUE;
  572. }
  573. static ieee80211_tx_result debug_noinline
  574. ieee80211_tx_h_sequence(struct ieee80211_tx_data *tx)
  575. {
  576. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
  577. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
  578. u16 *seq;
  579. u8 *qc;
  580. int tid;
  581. /*
  582. * Packet injection may want to control the sequence
  583. * number, if we have no matching interface then we
  584. * neither assign one ourselves nor ask the driver to.
  585. */
  586. if (unlikely(info->control.vif->type == NL80211_IFTYPE_MONITOR))
  587. return TX_CONTINUE;
  588. if (unlikely(ieee80211_is_ctl(hdr->frame_control)))
  589. return TX_CONTINUE;
  590. if (ieee80211_hdrlen(hdr->frame_control) < 24)
  591. return TX_CONTINUE;
  592. /*
  593. * Anything but QoS data that has a sequence number field
  594. * (is long enough) gets a sequence number from the global
  595. * counter.
  596. */
  597. if (!ieee80211_is_data_qos(hdr->frame_control)) {
  598. /* driver should assign sequence number */
  599. info->flags |= IEEE80211_TX_CTL_ASSIGN_SEQ;
  600. /* for pure STA mode without beacons, we can do it */
  601. hdr->seq_ctrl = cpu_to_le16(tx->sdata->sequence_number);
  602. tx->sdata->sequence_number += 0x10;
  603. tx->sdata->sequence_number &= IEEE80211_SCTL_SEQ;
  604. return TX_CONTINUE;
  605. }
  606. /*
  607. * This should be true for injected/management frames only, for
  608. * management frames we have set the IEEE80211_TX_CTL_ASSIGN_SEQ
  609. * above since they are not QoS-data frames.
  610. */
  611. if (!tx->sta)
  612. return TX_CONTINUE;
  613. /* include per-STA, per-TID sequence counter */
  614. qc = ieee80211_get_qos_ctl(hdr);
  615. tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
  616. seq = &tx->sta->tid_seq[tid];
  617. hdr->seq_ctrl = cpu_to_le16(*seq);
  618. /* Increase the sequence number. */
  619. *seq = (*seq + 0x10) & IEEE80211_SCTL_SEQ;
  620. return TX_CONTINUE;
  621. }
  622. static int ieee80211_fragment(struct ieee80211_local *local,
  623. struct sk_buff *skb, int hdrlen,
  624. int frag_threshold)
  625. {
  626. struct sk_buff *tail = skb, *tmp;
  627. int per_fragm = frag_threshold - hdrlen - FCS_LEN;
  628. int pos = hdrlen + per_fragm;
  629. int rem = skb->len - hdrlen - per_fragm;
  630. if (WARN_ON(rem < 0))
  631. return -EINVAL;
  632. while (rem) {
  633. int fraglen = per_fragm;
  634. if (fraglen > rem)
  635. fraglen = rem;
  636. rem -= fraglen;
  637. tmp = dev_alloc_skb(local->tx_headroom +
  638. frag_threshold +
  639. IEEE80211_ENCRYPT_HEADROOM +
  640. IEEE80211_ENCRYPT_TAILROOM);
  641. if (!tmp)
  642. return -ENOMEM;
  643. tail->next = tmp;
  644. tail = tmp;
  645. skb_reserve(tmp, local->tx_headroom +
  646. IEEE80211_ENCRYPT_HEADROOM);
  647. /* copy control information */
  648. memcpy(tmp->cb, skb->cb, sizeof(tmp->cb));
  649. skb_copy_queue_mapping(tmp, skb);
  650. tmp->priority = skb->priority;
  651. tmp->dev = skb->dev;
  652. /* copy header and data */
  653. memcpy(skb_put(tmp, hdrlen), skb->data, hdrlen);
  654. memcpy(skb_put(tmp, fraglen), skb->data + pos, fraglen);
  655. pos += fraglen;
  656. }
  657. skb->len = hdrlen + per_fragm;
  658. return 0;
  659. }
  660. static ieee80211_tx_result debug_noinline
  661. ieee80211_tx_h_fragment(struct ieee80211_tx_data *tx)
  662. {
  663. struct sk_buff *skb = tx->skb;
  664. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  665. struct ieee80211_hdr *hdr = (void *)skb->data;
  666. int frag_threshold = tx->local->hw.wiphy->frag_threshold;
  667. int hdrlen;
  668. int fragnum;
  669. if (!(tx->flags & IEEE80211_TX_FRAGMENTED))
  670. return TX_CONTINUE;
  671. /*
  672. * Warn when submitting a fragmented A-MPDU frame and drop it.
  673. * This scenario is handled in ieee80211_tx_prepare but extra
  674. * caution taken here as fragmented ampdu may cause Tx stop.
  675. */
  676. if (WARN_ON(info->flags & IEEE80211_TX_CTL_AMPDU))
  677. return TX_DROP;
  678. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  679. /* internal error, why is TX_FRAGMENTED set? */
  680. if (WARN_ON(skb->len + FCS_LEN <= frag_threshold))
  681. return TX_DROP;
  682. /*
  683. * Now fragment the frame. This will allocate all the fragments and
  684. * chain them (using skb as the first fragment) to skb->next.
  685. * During transmission, we will remove the successfully transmitted
  686. * fragments from this list. When the low-level driver rejects one
  687. * of the fragments then we will simply pretend to accept the skb
  688. * but store it away as pending.
  689. */
  690. if (ieee80211_fragment(tx->local, skb, hdrlen, frag_threshold))
  691. return TX_DROP;
  692. /* update duration/seq/flags of fragments */
  693. fragnum = 0;
  694. do {
  695. int next_len;
  696. const __le16 morefrags = cpu_to_le16(IEEE80211_FCTL_MOREFRAGS);
  697. hdr = (void *)skb->data;
  698. info = IEEE80211_SKB_CB(skb);
  699. if (skb->next) {
  700. hdr->frame_control |= morefrags;
  701. next_len = skb->next->len;
  702. /*
  703. * No multi-rate retries for fragmented frames, that
  704. * would completely throw off the NAV at other STAs.
  705. */
  706. info->control.rates[1].idx = -1;
  707. info->control.rates[2].idx = -1;
  708. info->control.rates[3].idx = -1;
  709. info->control.rates[4].idx = -1;
  710. BUILD_BUG_ON(IEEE80211_TX_MAX_RATES != 5);
  711. info->flags &= ~IEEE80211_TX_CTL_RATE_CTRL_PROBE;
  712. } else {
  713. hdr->frame_control &= ~morefrags;
  714. next_len = 0;
  715. }
  716. hdr->duration_id = ieee80211_duration(tx, 0, next_len);
  717. hdr->seq_ctrl |= cpu_to_le16(fragnum & IEEE80211_SCTL_FRAG);
  718. fragnum++;
  719. } while ((skb = skb->next));
  720. return TX_CONTINUE;
  721. }
  722. static ieee80211_tx_result debug_noinline
  723. ieee80211_tx_h_encrypt(struct ieee80211_tx_data *tx)
  724. {
  725. if (!tx->key)
  726. return TX_CONTINUE;
  727. switch (tx->key->conf.alg) {
  728. case ALG_WEP:
  729. return ieee80211_crypto_wep_encrypt(tx);
  730. case ALG_TKIP:
  731. return ieee80211_crypto_tkip_encrypt(tx);
  732. case ALG_CCMP:
  733. return ieee80211_crypto_ccmp_encrypt(tx);
  734. case ALG_AES_CMAC:
  735. return ieee80211_crypto_aes_cmac_encrypt(tx);
  736. }
  737. /* not reached */
  738. WARN_ON(1);
  739. return TX_DROP;
  740. }
  741. static ieee80211_tx_result debug_noinline
  742. ieee80211_tx_h_calculate_duration(struct ieee80211_tx_data *tx)
  743. {
  744. struct sk_buff *skb = tx->skb;
  745. struct ieee80211_hdr *hdr;
  746. int next_len;
  747. bool group_addr;
  748. do {
  749. hdr = (void *) skb->data;
  750. if (unlikely(ieee80211_is_pspoll(hdr->frame_control)))
  751. break; /* must not overwrite AID */
  752. next_len = skb->next ? skb->next->len : 0;
  753. group_addr = is_multicast_ether_addr(hdr->addr1);
  754. hdr->duration_id =
  755. ieee80211_duration(tx, group_addr, next_len);
  756. } while ((skb = skb->next));
  757. return TX_CONTINUE;
  758. }
  759. static ieee80211_tx_result debug_noinline
  760. ieee80211_tx_h_stats(struct ieee80211_tx_data *tx)
  761. {
  762. struct sk_buff *skb = tx->skb;
  763. if (!tx->sta)
  764. return TX_CONTINUE;
  765. tx->sta->tx_packets++;
  766. do {
  767. tx->sta->tx_fragments++;
  768. tx->sta->tx_bytes += skb->len;
  769. } while ((skb = skb->next));
  770. return TX_CONTINUE;
  771. }
  772. /* actual transmit path */
  773. /*
  774. * deal with packet injection down monitor interface
  775. * with Radiotap Header -- only called for monitor mode interface
  776. */
  777. static bool __ieee80211_parse_tx_radiotap(struct ieee80211_tx_data *tx,
  778. struct sk_buff *skb)
  779. {
  780. /*
  781. * this is the moment to interpret and discard the radiotap header that
  782. * must be at the start of the packet injected in Monitor mode
  783. *
  784. * Need to take some care with endian-ness since radiotap
  785. * args are little-endian
  786. */
  787. struct ieee80211_radiotap_iterator iterator;
  788. struct ieee80211_radiotap_header *rthdr =
  789. (struct ieee80211_radiotap_header *) skb->data;
  790. struct ieee80211_supported_band *sband;
  791. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  792. int ret = ieee80211_radiotap_iterator_init(&iterator, rthdr, skb->len);
  793. sband = tx->local->hw.wiphy->bands[tx->channel->band];
  794. info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  795. tx->flags &= ~IEEE80211_TX_FRAGMENTED;
  796. /*
  797. * for every radiotap entry that is present
  798. * (ieee80211_radiotap_iterator_next returns -ENOENT when no more
  799. * entries present, or -EINVAL on error)
  800. */
  801. while (!ret) {
  802. ret = ieee80211_radiotap_iterator_next(&iterator);
  803. if (ret)
  804. continue;
  805. /* see if this argument is something we can use */
  806. switch (iterator.this_arg_index) {
  807. /*
  808. * You must take care when dereferencing iterator.this_arg
  809. * for multibyte types... the pointer is not aligned. Use
  810. * get_unaligned((type *)iterator.this_arg) to dereference
  811. * iterator.this_arg for type "type" safely on all arches.
  812. */
  813. case IEEE80211_RADIOTAP_FLAGS:
  814. if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FCS) {
  815. /*
  816. * this indicates that the skb we have been
  817. * handed has the 32-bit FCS CRC at the end...
  818. * we should react to that by snipping it off
  819. * because it will be recomputed and added
  820. * on transmission
  821. */
  822. if (skb->len < (iterator.max_length + FCS_LEN))
  823. return false;
  824. skb_trim(skb, skb->len - FCS_LEN);
  825. }
  826. if (*iterator.this_arg & IEEE80211_RADIOTAP_F_WEP)
  827. info->flags &= ~IEEE80211_TX_INTFL_DONT_ENCRYPT;
  828. if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FRAG)
  829. tx->flags |= IEEE80211_TX_FRAGMENTED;
  830. break;
  831. /*
  832. * Please update the file
  833. * Documentation/networking/mac80211-injection.txt
  834. * when parsing new fields here.
  835. */
  836. default:
  837. break;
  838. }
  839. }
  840. if (ret != -ENOENT) /* ie, if we didn't simply run out of fields */
  841. return false;
  842. /*
  843. * remove the radiotap header
  844. * iterator->max_length was sanity-checked against
  845. * skb->len by iterator init
  846. */
  847. skb_pull(skb, iterator.max_length);
  848. return true;
  849. }
  850. /*
  851. * initialises @tx
  852. */
  853. static ieee80211_tx_result
  854. ieee80211_tx_prepare(struct ieee80211_sub_if_data *sdata,
  855. struct ieee80211_tx_data *tx,
  856. struct sk_buff *skb)
  857. {
  858. struct ieee80211_local *local = sdata->local;
  859. struct ieee80211_hdr *hdr;
  860. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  861. int hdrlen, tid;
  862. u8 *qc, *state;
  863. bool queued = false;
  864. memset(tx, 0, sizeof(*tx));
  865. tx->skb = skb;
  866. tx->dev = sdata->dev; /* use original interface */
  867. tx->local = local;
  868. tx->sdata = sdata;
  869. tx->channel = local->hw.conf.channel;
  870. /*
  871. * Set this flag (used below to indicate "automatic fragmentation"),
  872. * it will be cleared/left by radiotap as desired.
  873. */
  874. tx->flags |= IEEE80211_TX_FRAGMENTED;
  875. /* process and remove the injection radiotap header */
  876. if (unlikely(info->flags & IEEE80211_TX_CTL_INJECTED)) {
  877. if (!__ieee80211_parse_tx_radiotap(tx, skb))
  878. return TX_DROP;
  879. /*
  880. * __ieee80211_parse_tx_radiotap has now removed
  881. * the radiotap header that was present and pre-filled
  882. * 'tx' with tx control information.
  883. */
  884. }
  885. /*
  886. * If this flag is set to true anywhere, and we get here,
  887. * we are doing the needed processing, so remove the flag
  888. * now.
  889. */
  890. info->flags &= ~IEEE80211_TX_INTFL_NEED_TXPROCESSING;
  891. hdr = (struct ieee80211_hdr *) skb->data;
  892. tx->sta = sta_info_get(local, hdr->addr1);
  893. if (tx->sta && ieee80211_is_data_qos(hdr->frame_control) &&
  894. (local->hw.flags & IEEE80211_HW_AMPDU_AGGREGATION)) {
  895. unsigned long flags;
  896. struct tid_ampdu_tx *tid_tx;
  897. qc = ieee80211_get_qos_ctl(hdr);
  898. tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
  899. spin_lock_irqsave(&tx->sta->lock, flags);
  900. /*
  901. * XXX: This spinlock could be fairly expensive, but see the
  902. * comment in agg-tx.c:ieee80211_agg_tx_operational().
  903. * One way to solve this would be to do something RCU-like
  904. * for managing the tid_tx struct and using atomic bitops
  905. * for the actual state -- by introducing an actual
  906. * 'operational' bit that would be possible. It would
  907. * require changing ieee80211_agg_tx_operational() to
  908. * set that bit, and changing the way tid_tx is managed
  909. * everywhere, including races between that bit and
  910. * tid_tx going away (tid_tx being added can be easily
  911. * committed to memory before the 'operational' bit).
  912. */
  913. tid_tx = tx->sta->ampdu_mlme.tid_tx[tid];
  914. state = &tx->sta->ampdu_mlme.tid_state_tx[tid];
  915. if (*state == HT_AGG_STATE_OPERATIONAL) {
  916. info->flags |= IEEE80211_TX_CTL_AMPDU;
  917. } else if (*state != HT_AGG_STATE_IDLE) {
  918. /* in progress */
  919. queued = true;
  920. info->control.vif = &sdata->vif;
  921. info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
  922. __skb_queue_tail(&tid_tx->pending, skb);
  923. }
  924. spin_unlock_irqrestore(&tx->sta->lock, flags);
  925. if (unlikely(queued))
  926. return TX_QUEUED;
  927. }
  928. if (is_multicast_ether_addr(hdr->addr1)) {
  929. tx->flags &= ~IEEE80211_TX_UNICAST;
  930. info->flags |= IEEE80211_TX_CTL_NO_ACK;
  931. } else {
  932. tx->flags |= IEEE80211_TX_UNICAST;
  933. if (unlikely(local->wifi_wme_noack_test))
  934. info->flags |= IEEE80211_TX_CTL_NO_ACK;
  935. else
  936. info->flags &= ~IEEE80211_TX_CTL_NO_ACK;
  937. }
  938. if (tx->flags & IEEE80211_TX_FRAGMENTED) {
  939. if ((tx->flags & IEEE80211_TX_UNICAST) &&
  940. skb->len + FCS_LEN > local->hw.wiphy->frag_threshold &&
  941. !(info->flags & IEEE80211_TX_CTL_AMPDU))
  942. tx->flags |= IEEE80211_TX_FRAGMENTED;
  943. else
  944. tx->flags &= ~IEEE80211_TX_FRAGMENTED;
  945. }
  946. if (!tx->sta)
  947. info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
  948. else if (test_and_clear_sta_flags(tx->sta, WLAN_STA_CLEAR_PS_FILT))
  949. info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
  950. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  951. if (skb->len > hdrlen + sizeof(rfc1042_header) + 2) {
  952. u8 *pos = &skb->data[hdrlen + sizeof(rfc1042_header)];
  953. tx->ethertype = (pos[0] << 8) | pos[1];
  954. }
  955. info->flags |= IEEE80211_TX_CTL_FIRST_FRAGMENT;
  956. return TX_CONTINUE;
  957. }
  958. static int __ieee80211_tx(struct ieee80211_local *local,
  959. struct sk_buff **skbp,
  960. struct sta_info *sta,
  961. bool txpending)
  962. {
  963. struct sk_buff *skb = *skbp, *next;
  964. struct ieee80211_tx_info *info;
  965. struct ieee80211_sub_if_data *sdata;
  966. unsigned long flags;
  967. int ret, len;
  968. bool fragm = false;
  969. while (skb) {
  970. int q = skb_get_queue_mapping(skb);
  971. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  972. ret = IEEE80211_TX_OK;
  973. if (local->queue_stop_reasons[q] ||
  974. (!txpending && !skb_queue_empty(&local->pending[q])))
  975. ret = IEEE80211_TX_PENDING;
  976. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  977. if (ret != IEEE80211_TX_OK)
  978. return ret;
  979. info = IEEE80211_SKB_CB(skb);
  980. if (fragm)
  981. info->flags &= ~(IEEE80211_TX_CTL_CLEAR_PS_FILT |
  982. IEEE80211_TX_CTL_FIRST_FRAGMENT);
  983. next = skb->next;
  984. len = skb->len;
  985. sdata = vif_to_sdata(info->control.vif);
  986. switch (sdata->vif.type) {
  987. case NL80211_IFTYPE_MONITOR:
  988. info->control.vif = NULL;
  989. break;
  990. case NL80211_IFTYPE_AP_VLAN:
  991. info->control.vif = &container_of(sdata->bss,
  992. struct ieee80211_sub_if_data, u.ap)->vif;
  993. break;
  994. default:
  995. /* keep */
  996. break;
  997. }
  998. ret = drv_tx(local, skb);
  999. info->control.vif = &sdata->vif;
  1000. if (WARN_ON(ret != NETDEV_TX_OK && skb->len != len)) {
  1001. dev_kfree_skb(skb);
  1002. ret = NETDEV_TX_OK;
  1003. }
  1004. if (ret != NETDEV_TX_OK)
  1005. return IEEE80211_TX_AGAIN;
  1006. *skbp = skb = next;
  1007. ieee80211_led_tx(local, 1);
  1008. fragm = true;
  1009. }
  1010. return IEEE80211_TX_OK;
  1011. }
  1012. /*
  1013. * Invoke TX handlers, return 0 on success and non-zero if the
  1014. * frame was dropped or queued.
  1015. */
  1016. static int invoke_tx_handlers(struct ieee80211_tx_data *tx)
  1017. {
  1018. struct sk_buff *skb = tx->skb;
  1019. ieee80211_tx_result res = TX_DROP;
  1020. #define CALL_TXH(txh) \
  1021. res = txh(tx); \
  1022. if (res != TX_CONTINUE) \
  1023. goto txh_done;
  1024. CALL_TXH(ieee80211_tx_h_check_assoc)
  1025. CALL_TXH(ieee80211_tx_h_ps_buf)
  1026. CALL_TXH(ieee80211_tx_h_select_key)
  1027. CALL_TXH(ieee80211_tx_h_michael_mic_add)
  1028. CALL_TXH(ieee80211_tx_h_rate_ctrl)
  1029. CALL_TXH(ieee80211_tx_h_misc)
  1030. CALL_TXH(ieee80211_tx_h_sequence)
  1031. CALL_TXH(ieee80211_tx_h_fragment)
  1032. /* handlers after fragment must be aware of tx info fragmentation! */
  1033. CALL_TXH(ieee80211_tx_h_encrypt)
  1034. CALL_TXH(ieee80211_tx_h_calculate_duration)
  1035. CALL_TXH(ieee80211_tx_h_stats)
  1036. #undef CALL_TXH
  1037. txh_done:
  1038. if (unlikely(res == TX_DROP)) {
  1039. I802_DEBUG_INC(tx->local->tx_handlers_drop);
  1040. while (skb) {
  1041. struct sk_buff *next;
  1042. next = skb->next;
  1043. dev_kfree_skb(skb);
  1044. skb = next;
  1045. }
  1046. return -1;
  1047. } else if (unlikely(res == TX_QUEUED)) {
  1048. I802_DEBUG_INC(tx->local->tx_handlers_queued);
  1049. return -1;
  1050. }
  1051. return 0;
  1052. }
  1053. static void ieee80211_tx(struct ieee80211_sub_if_data *sdata,
  1054. struct sk_buff *skb, bool txpending)
  1055. {
  1056. struct ieee80211_local *local = sdata->local;
  1057. struct ieee80211_tx_data tx;
  1058. ieee80211_tx_result res_prepare;
  1059. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1060. struct sk_buff *next;
  1061. unsigned long flags;
  1062. int ret, retries;
  1063. u16 queue;
  1064. queue = skb_get_queue_mapping(skb);
  1065. if (unlikely(skb->len < 10)) {
  1066. dev_kfree_skb(skb);
  1067. return;
  1068. }
  1069. rcu_read_lock();
  1070. /* initialises tx */
  1071. res_prepare = ieee80211_tx_prepare(sdata, &tx, skb);
  1072. if (unlikely(res_prepare == TX_DROP)) {
  1073. dev_kfree_skb(skb);
  1074. rcu_read_unlock();
  1075. return;
  1076. } else if (unlikely(res_prepare == TX_QUEUED)) {
  1077. rcu_read_unlock();
  1078. return;
  1079. }
  1080. tx.channel = local->hw.conf.channel;
  1081. info->band = tx.channel->band;
  1082. if (invoke_tx_handlers(&tx))
  1083. goto out;
  1084. retries = 0;
  1085. retry:
  1086. ret = __ieee80211_tx(local, &tx.skb, tx.sta, txpending);
  1087. switch (ret) {
  1088. case IEEE80211_TX_OK:
  1089. break;
  1090. case IEEE80211_TX_AGAIN:
  1091. /*
  1092. * Since there are no fragmented frames on A-MPDU
  1093. * queues, there's no reason for a driver to reject
  1094. * a frame there, warn and drop it.
  1095. */
  1096. if (WARN_ON(info->flags & IEEE80211_TX_CTL_AMPDU))
  1097. goto drop;
  1098. /* fall through */
  1099. case IEEE80211_TX_PENDING:
  1100. skb = tx.skb;
  1101. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  1102. if (local->queue_stop_reasons[queue] ||
  1103. !skb_queue_empty(&local->pending[queue])) {
  1104. /*
  1105. * if queue is stopped, queue up frames for later
  1106. * transmission from the tasklet
  1107. */
  1108. do {
  1109. next = skb->next;
  1110. skb->next = NULL;
  1111. if (unlikely(txpending))
  1112. __skb_queue_head(&local->pending[queue],
  1113. skb);
  1114. else
  1115. __skb_queue_tail(&local->pending[queue],
  1116. skb);
  1117. } while ((skb = next));
  1118. spin_unlock_irqrestore(&local->queue_stop_reason_lock,
  1119. flags);
  1120. } else {
  1121. /*
  1122. * otherwise retry, but this is a race condition or
  1123. * a driver bug (which we warn about if it persists)
  1124. */
  1125. spin_unlock_irqrestore(&local->queue_stop_reason_lock,
  1126. flags);
  1127. retries++;
  1128. if (WARN(retries > 10, "tx refused but queue active\n"))
  1129. goto drop;
  1130. goto retry;
  1131. }
  1132. }
  1133. out:
  1134. rcu_read_unlock();
  1135. return;
  1136. drop:
  1137. rcu_read_unlock();
  1138. skb = tx.skb;
  1139. while (skb) {
  1140. next = skb->next;
  1141. dev_kfree_skb(skb);
  1142. skb = next;
  1143. }
  1144. }
  1145. /* device xmit handlers */
  1146. static int ieee80211_skb_resize(struct ieee80211_local *local,
  1147. struct sk_buff *skb,
  1148. int head_need, bool may_encrypt)
  1149. {
  1150. int tail_need = 0;
  1151. /*
  1152. * This could be optimised, devices that do full hardware
  1153. * crypto (including TKIP MMIC) need no tailroom... But we
  1154. * have no drivers for such devices currently.
  1155. */
  1156. if (may_encrypt) {
  1157. tail_need = IEEE80211_ENCRYPT_TAILROOM;
  1158. tail_need -= skb_tailroom(skb);
  1159. tail_need = max_t(int, tail_need, 0);
  1160. }
  1161. if (head_need || tail_need) {
  1162. /* Sorry. Can't account for this any more */
  1163. skb_orphan(skb);
  1164. }
  1165. if (skb_header_cloned(skb))
  1166. I802_DEBUG_INC(local->tx_expand_skb_head_cloned);
  1167. else
  1168. I802_DEBUG_INC(local->tx_expand_skb_head);
  1169. if (pskb_expand_head(skb, head_need, tail_need, GFP_ATOMIC)) {
  1170. printk(KERN_DEBUG "%s: failed to reallocate TX buffer\n",
  1171. wiphy_name(local->hw.wiphy));
  1172. return -ENOMEM;
  1173. }
  1174. /* update truesize too */
  1175. skb->truesize += head_need + tail_need;
  1176. return 0;
  1177. }
  1178. static void ieee80211_xmit(struct ieee80211_sub_if_data *sdata,
  1179. struct sk_buff *skb)
  1180. {
  1181. struct ieee80211_local *local = sdata->local;
  1182. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1183. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  1184. struct ieee80211_sub_if_data *tmp_sdata;
  1185. int headroom;
  1186. bool may_encrypt;
  1187. dev_hold(sdata->dev);
  1188. if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
  1189. local->hw.conf.dynamic_ps_timeout > 0 &&
  1190. !(local->scanning) && local->ps_sdata) {
  1191. if (local->hw.conf.flags & IEEE80211_CONF_PS) {
  1192. ieee80211_stop_queues_by_reason(&local->hw,
  1193. IEEE80211_QUEUE_STOP_REASON_PS);
  1194. queue_work(local->hw.workqueue,
  1195. &local->dynamic_ps_disable_work);
  1196. }
  1197. mod_timer(&local->dynamic_ps_timer, jiffies +
  1198. msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
  1199. }
  1200. info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
  1201. if (ieee80211_vif_is_mesh(&sdata->vif) &&
  1202. ieee80211_is_data(hdr->frame_control)) {
  1203. if (is_multicast_ether_addr(hdr->addr3))
  1204. memcpy(hdr->addr1, hdr->addr3, ETH_ALEN);
  1205. else
  1206. if (mesh_nexthop_lookup(skb, sdata)) {
  1207. dev_put(sdata->dev);
  1208. return;
  1209. }
  1210. } else if (unlikely(sdata->vif.type == NL80211_IFTYPE_MONITOR)) {
  1211. int hdrlen;
  1212. u16 len_rthdr;
  1213. info->flags |= IEEE80211_TX_CTL_INJECTED;
  1214. len_rthdr = ieee80211_get_radiotap_len(skb->data);
  1215. hdr = (struct ieee80211_hdr *)(skb->data + len_rthdr);
  1216. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  1217. /* check the header is complete in the frame */
  1218. if (likely(skb->len >= len_rthdr + hdrlen)) {
  1219. /*
  1220. * We process outgoing injected frames that have a
  1221. * local address we handle as though they are our
  1222. * own frames.
  1223. * This code here isn't entirely correct, the local
  1224. * MAC address is not necessarily enough to find
  1225. * the interface to use; for that proper VLAN/WDS
  1226. * support we will need a different mechanism.
  1227. */
  1228. rcu_read_lock();
  1229. list_for_each_entry_rcu(tmp_sdata, &local->interfaces,
  1230. list) {
  1231. if (!netif_running(tmp_sdata->dev))
  1232. continue;
  1233. if (tmp_sdata->vif.type != NL80211_IFTYPE_AP)
  1234. continue;
  1235. if (compare_ether_addr(tmp_sdata->dev->dev_addr,
  1236. hdr->addr2)) {
  1237. dev_hold(tmp_sdata->dev);
  1238. dev_put(sdata->dev);
  1239. sdata = tmp_sdata;
  1240. break;
  1241. }
  1242. }
  1243. rcu_read_unlock();
  1244. }
  1245. }
  1246. may_encrypt = !(info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT);
  1247. headroom = local->tx_headroom;
  1248. if (may_encrypt)
  1249. headroom += IEEE80211_ENCRYPT_HEADROOM;
  1250. headroom -= skb_headroom(skb);
  1251. headroom = max_t(int, 0, headroom);
  1252. if (ieee80211_skb_resize(local, skb, headroom, may_encrypt)) {
  1253. dev_kfree_skb(skb);
  1254. dev_put(sdata->dev);
  1255. return;
  1256. }
  1257. info->control.vif = &sdata->vif;
  1258. ieee80211_select_queue(local, skb);
  1259. ieee80211_tx(sdata, skb, false);
  1260. dev_put(sdata->dev);
  1261. }
  1262. int ieee80211_monitor_start_xmit(struct sk_buff *skb,
  1263. struct net_device *dev)
  1264. {
  1265. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  1266. struct ieee80211_channel *chan = local->hw.conf.channel;
  1267. struct ieee80211_radiotap_header *prthdr =
  1268. (struct ieee80211_radiotap_header *)skb->data;
  1269. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1270. u16 len_rthdr;
  1271. /*
  1272. * Frame injection is not allowed if beaconing is not allowed
  1273. * or if we need radar detection. Beaconing is usually not allowed when
  1274. * the mode or operation (Adhoc, AP, Mesh) does not support DFS.
  1275. * Passive scan is also used in world regulatory domains where
  1276. * your country is not known and as such it should be treated as
  1277. * NO TX unless the channel is explicitly allowed in which case
  1278. * your current regulatory domain would not have the passive scan
  1279. * flag.
  1280. *
  1281. * Since AP mode uses monitor interfaces to inject/TX management
  1282. * frames we can make AP mode the exception to this rule once it
  1283. * supports radar detection as its implementation can deal with
  1284. * radar detection by itself. We can do that later by adding a
  1285. * monitor flag interfaces used for AP support.
  1286. */
  1287. if ((chan->flags & (IEEE80211_CHAN_NO_IBSS | IEEE80211_CHAN_RADAR |
  1288. IEEE80211_CHAN_PASSIVE_SCAN)))
  1289. goto fail;
  1290. /* check for not even having the fixed radiotap header part */
  1291. if (unlikely(skb->len < sizeof(struct ieee80211_radiotap_header)))
  1292. goto fail; /* too short to be possibly valid */
  1293. /* is it a header version we can trust to find length from? */
  1294. if (unlikely(prthdr->it_version))
  1295. goto fail; /* only version 0 is supported */
  1296. /* then there must be a radiotap header with a length we can use */
  1297. len_rthdr = ieee80211_get_radiotap_len(skb->data);
  1298. /* does the skb contain enough to deliver on the alleged length? */
  1299. if (unlikely(skb->len < len_rthdr))
  1300. goto fail; /* skb too short for claimed rt header extent */
  1301. /*
  1302. * fix up the pointers accounting for the radiotap
  1303. * header still being in there. We are being given
  1304. * a precooked IEEE80211 header so no need for
  1305. * normal processing
  1306. */
  1307. skb_set_mac_header(skb, len_rthdr);
  1308. /*
  1309. * these are just fixed to the end of the rt area since we
  1310. * don't have any better information and at this point, nobody cares
  1311. */
  1312. skb_set_network_header(skb, len_rthdr);
  1313. skb_set_transport_header(skb, len_rthdr);
  1314. memset(info, 0, sizeof(*info));
  1315. /* pass the radiotap header up to xmit */
  1316. ieee80211_xmit(IEEE80211_DEV_TO_SUB_IF(dev), skb);
  1317. return NETDEV_TX_OK;
  1318. fail:
  1319. dev_kfree_skb(skb);
  1320. return NETDEV_TX_OK; /* meaning, we dealt with the skb */
  1321. }
  1322. /**
  1323. * ieee80211_subif_start_xmit - netif start_xmit function for Ethernet-type
  1324. * subinterfaces (wlan#, WDS, and VLAN interfaces)
  1325. * @skb: packet to be sent
  1326. * @dev: incoming interface
  1327. *
  1328. * Returns: 0 on success (and frees skb in this case) or 1 on failure (skb will
  1329. * not be freed, and caller is responsible for either retrying later or freeing
  1330. * skb).
  1331. *
  1332. * This function takes in an Ethernet header and encapsulates it with suitable
  1333. * IEEE 802.11 header based on which interface the packet is coming in. The
  1334. * encapsulated packet will then be passed to master interface, wlan#.11, for
  1335. * transmission (through low-level driver).
  1336. */
  1337. int ieee80211_subif_start_xmit(struct sk_buff *skb,
  1338. struct net_device *dev)
  1339. {
  1340. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  1341. struct ieee80211_local *local = sdata->local;
  1342. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1343. int ret = NETDEV_TX_BUSY, head_need;
  1344. u16 ethertype, hdrlen, meshhdrlen = 0;
  1345. __le16 fc;
  1346. struct ieee80211_hdr hdr;
  1347. struct ieee80211s_hdr mesh_hdr;
  1348. const u8 *encaps_data;
  1349. int encaps_len, skip_header_bytes;
  1350. int nh_pos, h_pos;
  1351. struct sta_info *sta;
  1352. u32 sta_flags = 0;
  1353. if (unlikely(skb->len < ETH_HLEN)) {
  1354. ret = NETDEV_TX_OK;
  1355. goto fail;
  1356. }
  1357. nh_pos = skb_network_header(skb) - skb->data;
  1358. h_pos = skb_transport_header(skb) - skb->data;
  1359. /* convert Ethernet header to proper 802.11 header (based on
  1360. * operation mode) */
  1361. ethertype = (skb->data[12] << 8) | skb->data[13];
  1362. fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
  1363. switch (sdata->vif.type) {
  1364. case NL80211_IFTYPE_AP:
  1365. case NL80211_IFTYPE_AP_VLAN:
  1366. fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
  1367. /* DA BSSID SA */
  1368. memcpy(hdr.addr1, skb->data, ETH_ALEN);
  1369. memcpy(hdr.addr2, dev->dev_addr, ETH_ALEN);
  1370. memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
  1371. hdrlen = 24;
  1372. break;
  1373. case NL80211_IFTYPE_WDS:
  1374. fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
  1375. /* RA TA DA SA */
  1376. memcpy(hdr.addr1, sdata->u.wds.remote_addr, ETH_ALEN);
  1377. memcpy(hdr.addr2, dev->dev_addr, ETH_ALEN);
  1378. memcpy(hdr.addr3, skb->data, ETH_ALEN);
  1379. memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
  1380. hdrlen = 30;
  1381. break;
  1382. #ifdef CONFIG_MAC80211_MESH
  1383. case NL80211_IFTYPE_MESH_POINT:
  1384. fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
  1385. if (!sdata->u.mesh.mshcfg.dot11MeshTTL) {
  1386. /* Do not send frames with mesh_ttl == 0 */
  1387. sdata->u.mesh.mshstats.dropped_frames_ttl++;
  1388. ret = NETDEV_TX_OK;
  1389. goto fail;
  1390. }
  1391. memset(&mesh_hdr, 0, sizeof(mesh_hdr));
  1392. if (compare_ether_addr(dev->dev_addr,
  1393. skb->data + ETH_ALEN) == 0) {
  1394. /* RA TA DA SA */
  1395. memset(hdr.addr1, 0, ETH_ALEN);
  1396. memcpy(hdr.addr2, dev->dev_addr, ETH_ALEN);
  1397. memcpy(hdr.addr3, skb->data, ETH_ALEN);
  1398. memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
  1399. meshhdrlen = ieee80211_new_mesh_header(&mesh_hdr, sdata);
  1400. } else {
  1401. /* packet from other interface */
  1402. struct mesh_path *mppath;
  1403. memset(hdr.addr1, 0, ETH_ALEN);
  1404. memcpy(hdr.addr2, dev->dev_addr, ETH_ALEN);
  1405. memcpy(hdr.addr4, dev->dev_addr, ETH_ALEN);
  1406. if (is_multicast_ether_addr(skb->data))
  1407. memcpy(hdr.addr3, skb->data, ETH_ALEN);
  1408. else {
  1409. rcu_read_lock();
  1410. mppath = mpp_path_lookup(skb->data, sdata);
  1411. if (mppath)
  1412. memcpy(hdr.addr3, mppath->mpp, ETH_ALEN);
  1413. else
  1414. memset(hdr.addr3, 0xff, ETH_ALEN);
  1415. rcu_read_unlock();
  1416. }
  1417. mesh_hdr.flags |= MESH_FLAGS_AE_A5_A6;
  1418. mesh_hdr.ttl = sdata->u.mesh.mshcfg.dot11MeshTTL;
  1419. put_unaligned(cpu_to_le32(sdata->u.mesh.mesh_seqnum), &mesh_hdr.seqnum);
  1420. memcpy(mesh_hdr.eaddr1, skb->data, ETH_ALEN);
  1421. memcpy(mesh_hdr.eaddr2, skb->data + ETH_ALEN, ETH_ALEN);
  1422. sdata->u.mesh.mesh_seqnum++;
  1423. meshhdrlen = 18;
  1424. }
  1425. hdrlen = 30;
  1426. break;
  1427. #endif
  1428. case NL80211_IFTYPE_STATION:
  1429. fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
  1430. /* BSSID SA DA */
  1431. memcpy(hdr.addr1, sdata->u.mgd.bssid, ETH_ALEN);
  1432. memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
  1433. memcpy(hdr.addr3, skb->data, ETH_ALEN);
  1434. hdrlen = 24;
  1435. break;
  1436. case NL80211_IFTYPE_ADHOC:
  1437. /* DA SA BSSID */
  1438. memcpy(hdr.addr1, skb->data, ETH_ALEN);
  1439. memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
  1440. memcpy(hdr.addr3, sdata->u.ibss.bssid, ETH_ALEN);
  1441. hdrlen = 24;
  1442. break;
  1443. default:
  1444. ret = NETDEV_TX_OK;
  1445. goto fail;
  1446. }
  1447. /*
  1448. * There's no need to try to look up the destination
  1449. * if it is a multicast address (which can only happen
  1450. * in AP mode)
  1451. */
  1452. if (!is_multicast_ether_addr(hdr.addr1)) {
  1453. rcu_read_lock();
  1454. sta = sta_info_get(local, hdr.addr1);
  1455. if (sta)
  1456. sta_flags = get_sta_flags(sta);
  1457. rcu_read_unlock();
  1458. }
  1459. /* receiver and we are QoS enabled, use a QoS type frame */
  1460. if ((sta_flags & WLAN_STA_WME) && local->hw.queues >= 4) {
  1461. fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
  1462. hdrlen += 2;
  1463. }
  1464. /*
  1465. * Drop unicast frames to unauthorised stations unless they are
  1466. * EAPOL frames from the local station.
  1467. */
  1468. if (!ieee80211_vif_is_mesh(&sdata->vif) &&
  1469. unlikely(!is_multicast_ether_addr(hdr.addr1) &&
  1470. !(sta_flags & WLAN_STA_AUTHORIZED) &&
  1471. !(ethertype == ETH_P_PAE &&
  1472. compare_ether_addr(dev->dev_addr,
  1473. skb->data + ETH_ALEN) == 0))) {
  1474. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  1475. if (net_ratelimit())
  1476. printk(KERN_DEBUG "%s: dropped frame to %pM"
  1477. " (unauthorized port)\n", dev->name,
  1478. hdr.addr1);
  1479. #endif
  1480. I802_DEBUG_INC(local->tx_handlers_drop_unauth_port);
  1481. ret = NETDEV_TX_OK;
  1482. goto fail;
  1483. }
  1484. hdr.frame_control = fc;
  1485. hdr.duration_id = 0;
  1486. hdr.seq_ctrl = 0;
  1487. skip_header_bytes = ETH_HLEN;
  1488. if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
  1489. encaps_data = bridge_tunnel_header;
  1490. encaps_len = sizeof(bridge_tunnel_header);
  1491. skip_header_bytes -= 2;
  1492. } else if (ethertype >= 0x600) {
  1493. encaps_data = rfc1042_header;
  1494. encaps_len = sizeof(rfc1042_header);
  1495. skip_header_bytes -= 2;
  1496. } else {
  1497. encaps_data = NULL;
  1498. encaps_len = 0;
  1499. }
  1500. skb_pull(skb, skip_header_bytes);
  1501. nh_pos -= skip_header_bytes;
  1502. h_pos -= skip_header_bytes;
  1503. head_need = hdrlen + encaps_len + meshhdrlen - skb_headroom(skb);
  1504. /*
  1505. * So we need to modify the skb header and hence need a copy of
  1506. * that. The head_need variable above doesn't, so far, include
  1507. * the needed header space that we don't need right away. If we
  1508. * can, then we don't reallocate right now but only after the
  1509. * frame arrives at the master device (if it does...)
  1510. *
  1511. * If we cannot, however, then we will reallocate to include all
  1512. * the ever needed space. Also, if we need to reallocate it anyway,
  1513. * make it big enough for everything we may ever need.
  1514. */
  1515. if (head_need > 0 || skb_cloned(skb)) {
  1516. head_need += IEEE80211_ENCRYPT_HEADROOM;
  1517. head_need += local->tx_headroom;
  1518. head_need = max_t(int, 0, head_need);
  1519. if (ieee80211_skb_resize(local, skb, head_need, true))
  1520. goto fail;
  1521. }
  1522. if (encaps_data) {
  1523. memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
  1524. nh_pos += encaps_len;
  1525. h_pos += encaps_len;
  1526. }
  1527. if (meshhdrlen > 0) {
  1528. memcpy(skb_push(skb, meshhdrlen), &mesh_hdr, meshhdrlen);
  1529. nh_pos += meshhdrlen;
  1530. h_pos += meshhdrlen;
  1531. }
  1532. if (ieee80211_is_data_qos(fc)) {
  1533. __le16 *qos_control;
  1534. qos_control = (__le16*) skb_push(skb, 2);
  1535. memcpy(skb_push(skb, hdrlen - 2), &hdr, hdrlen - 2);
  1536. /*
  1537. * Maybe we could actually set some fields here, for now just
  1538. * initialise to zero to indicate no special operation.
  1539. */
  1540. *qos_control = 0;
  1541. } else
  1542. memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
  1543. nh_pos += hdrlen;
  1544. h_pos += hdrlen;
  1545. dev->stats.tx_packets++;
  1546. dev->stats.tx_bytes += skb->len;
  1547. /* Update skb pointers to various headers since this modified frame
  1548. * is going to go through Linux networking code that may potentially
  1549. * need things like pointer to IP header. */
  1550. skb_set_mac_header(skb, 0);
  1551. skb_set_network_header(skb, nh_pos);
  1552. skb_set_transport_header(skb, h_pos);
  1553. memset(info, 0, sizeof(*info));
  1554. dev->trans_start = jiffies;
  1555. ieee80211_xmit(sdata, skb);
  1556. return NETDEV_TX_OK;
  1557. fail:
  1558. if (ret == NETDEV_TX_OK)
  1559. dev_kfree_skb(skb);
  1560. return ret;
  1561. }
  1562. /*
  1563. * ieee80211_clear_tx_pending may not be called in a context where
  1564. * it is possible that it packets could come in again.
  1565. */
  1566. void ieee80211_clear_tx_pending(struct ieee80211_local *local)
  1567. {
  1568. int i;
  1569. for (i = 0; i < local->hw.queues; i++)
  1570. skb_queue_purge(&local->pending[i]);
  1571. }
  1572. static bool ieee80211_tx_pending_skb(struct ieee80211_local *local,
  1573. struct sk_buff *skb)
  1574. {
  1575. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1576. struct ieee80211_sub_if_data *sdata;
  1577. struct sta_info *sta;
  1578. struct ieee80211_hdr *hdr;
  1579. int ret;
  1580. bool result = true;
  1581. sdata = vif_to_sdata(info->control.vif);
  1582. if (info->flags & IEEE80211_TX_INTFL_NEED_TXPROCESSING) {
  1583. ieee80211_tx(sdata, skb, true);
  1584. } else {
  1585. hdr = (struct ieee80211_hdr *)skb->data;
  1586. sta = sta_info_get(local, hdr->addr1);
  1587. ret = __ieee80211_tx(local, &skb, sta, true);
  1588. if (ret != IEEE80211_TX_OK)
  1589. result = false;
  1590. }
  1591. return result;
  1592. }
  1593. /*
  1594. * Transmit all pending packets. Called from tasklet.
  1595. */
  1596. void ieee80211_tx_pending(unsigned long data)
  1597. {
  1598. struct ieee80211_local *local = (struct ieee80211_local *)data;
  1599. unsigned long flags;
  1600. int i;
  1601. bool txok;
  1602. rcu_read_lock();
  1603. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  1604. for (i = 0; i < local->hw.queues; i++) {
  1605. /*
  1606. * If queue is stopped by something other than due to pending
  1607. * frames, or we have no pending frames, proceed to next queue.
  1608. */
  1609. if (local->queue_stop_reasons[i] ||
  1610. skb_queue_empty(&local->pending[i]))
  1611. continue;
  1612. while (!skb_queue_empty(&local->pending[i])) {
  1613. struct sk_buff *skb = __skb_dequeue(&local->pending[i]);
  1614. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1615. struct ieee80211_sub_if_data *sdata;
  1616. sdata = vif_to_sdata(info->control.vif);
  1617. dev_hold(sdata->dev);
  1618. spin_unlock_irqrestore(&local->queue_stop_reason_lock,
  1619. flags);
  1620. txok = ieee80211_tx_pending_skb(local, skb);
  1621. dev_put(sdata->dev);
  1622. if (!txok)
  1623. __skb_queue_head(&local->pending[i], skb);
  1624. spin_lock_irqsave(&local->queue_stop_reason_lock,
  1625. flags);
  1626. if (!txok)
  1627. break;
  1628. }
  1629. }
  1630. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  1631. rcu_read_unlock();
  1632. }
  1633. /* functions for drivers to get certain frames */
  1634. static void ieee80211_beacon_add_tim(struct ieee80211_if_ap *bss,
  1635. struct sk_buff *skb,
  1636. struct beacon_data *beacon)
  1637. {
  1638. u8 *pos, *tim;
  1639. int aid0 = 0;
  1640. int i, have_bits = 0, n1, n2;
  1641. /* Generate bitmap for TIM only if there are any STAs in power save
  1642. * mode. */
  1643. if (atomic_read(&bss->num_sta_ps) > 0)
  1644. /* in the hope that this is faster than
  1645. * checking byte-for-byte */
  1646. have_bits = !bitmap_empty((unsigned long*)bss->tim,
  1647. IEEE80211_MAX_AID+1);
  1648. if (bss->dtim_count == 0)
  1649. bss->dtim_count = beacon->dtim_period - 1;
  1650. else
  1651. bss->dtim_count--;
  1652. tim = pos = (u8 *) skb_put(skb, 6);
  1653. *pos++ = WLAN_EID_TIM;
  1654. *pos++ = 4;
  1655. *pos++ = bss->dtim_count;
  1656. *pos++ = beacon->dtim_period;
  1657. if (bss->dtim_count == 0 && !skb_queue_empty(&bss->ps_bc_buf))
  1658. aid0 = 1;
  1659. if (have_bits) {
  1660. /* Find largest even number N1 so that bits numbered 1 through
  1661. * (N1 x 8) - 1 in the bitmap are 0 and number N2 so that bits
  1662. * (N2 + 1) x 8 through 2007 are 0. */
  1663. n1 = 0;
  1664. for (i = 0; i < IEEE80211_MAX_TIM_LEN; i++) {
  1665. if (bss->tim[i]) {
  1666. n1 = i & 0xfe;
  1667. break;
  1668. }
  1669. }
  1670. n2 = n1;
  1671. for (i = IEEE80211_MAX_TIM_LEN - 1; i >= n1; i--) {
  1672. if (bss->tim[i]) {
  1673. n2 = i;
  1674. break;
  1675. }
  1676. }
  1677. /* Bitmap control */
  1678. *pos++ = n1 | aid0;
  1679. /* Part Virt Bitmap */
  1680. memcpy(pos, bss->tim + n1, n2 - n1 + 1);
  1681. tim[1] = n2 - n1 + 4;
  1682. skb_put(skb, n2 - n1);
  1683. } else {
  1684. *pos++ = aid0; /* Bitmap control */
  1685. *pos++ = 0; /* Part Virt Bitmap */
  1686. }
  1687. }
  1688. struct sk_buff *ieee80211_beacon_get(struct ieee80211_hw *hw,
  1689. struct ieee80211_vif *vif)
  1690. {
  1691. struct ieee80211_local *local = hw_to_local(hw);
  1692. struct sk_buff *skb = NULL;
  1693. struct ieee80211_tx_info *info;
  1694. struct ieee80211_sub_if_data *sdata = NULL;
  1695. struct ieee80211_if_ap *ap = NULL;
  1696. struct beacon_data *beacon;
  1697. struct ieee80211_supported_band *sband;
  1698. enum ieee80211_band band = local->hw.conf.channel->band;
  1699. sband = local->hw.wiphy->bands[band];
  1700. rcu_read_lock();
  1701. sdata = vif_to_sdata(vif);
  1702. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  1703. ap = &sdata->u.ap;
  1704. beacon = rcu_dereference(ap->beacon);
  1705. if (ap && beacon) {
  1706. /*
  1707. * headroom, head length,
  1708. * tail length and maximum TIM length
  1709. */
  1710. skb = dev_alloc_skb(local->tx_headroom +
  1711. beacon->head_len +
  1712. beacon->tail_len + 256);
  1713. if (!skb)
  1714. goto out;
  1715. skb_reserve(skb, local->tx_headroom);
  1716. memcpy(skb_put(skb, beacon->head_len), beacon->head,
  1717. beacon->head_len);
  1718. /*
  1719. * Not very nice, but we want to allow the driver to call
  1720. * ieee80211_beacon_get() as a response to the set_tim()
  1721. * callback. That, however, is already invoked under the
  1722. * sta_lock to guarantee consistent and race-free update
  1723. * of the tim bitmap in mac80211 and the driver.
  1724. */
  1725. if (local->tim_in_locked_section) {
  1726. ieee80211_beacon_add_tim(ap, skb, beacon);
  1727. } else {
  1728. unsigned long flags;
  1729. spin_lock_irqsave(&local->sta_lock, flags);
  1730. ieee80211_beacon_add_tim(ap, skb, beacon);
  1731. spin_unlock_irqrestore(&local->sta_lock, flags);
  1732. }
  1733. if (beacon->tail)
  1734. memcpy(skb_put(skb, beacon->tail_len),
  1735. beacon->tail, beacon->tail_len);
  1736. } else
  1737. goto out;
  1738. } else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
  1739. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  1740. struct ieee80211_hdr *hdr;
  1741. struct sk_buff *presp = rcu_dereference(ifibss->presp);
  1742. if (!presp)
  1743. goto out;
  1744. skb = skb_copy(presp, GFP_ATOMIC);
  1745. if (!skb)
  1746. goto out;
  1747. hdr = (struct ieee80211_hdr *) skb->data;
  1748. hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  1749. IEEE80211_STYPE_BEACON);
  1750. } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
  1751. struct ieee80211_mgmt *mgmt;
  1752. u8 *pos;
  1753. /* headroom, head length, tail length and maximum TIM length */
  1754. skb = dev_alloc_skb(local->tx_headroom + 400);
  1755. if (!skb)
  1756. goto out;
  1757. skb_reserve(skb, local->hw.extra_tx_headroom);
  1758. mgmt = (struct ieee80211_mgmt *)
  1759. skb_put(skb, 24 + sizeof(mgmt->u.beacon));
  1760. memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
  1761. mgmt->frame_control =
  1762. cpu_to_le16(IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_BEACON);
  1763. memset(mgmt->da, 0xff, ETH_ALEN);
  1764. memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
  1765. /* BSSID is left zeroed, wildcard value */
  1766. mgmt->u.beacon.beacon_int =
  1767. cpu_to_le16(sdata->vif.bss_conf.beacon_int);
  1768. mgmt->u.beacon.capab_info = 0x0; /* 0x0 for MPs */
  1769. pos = skb_put(skb, 2);
  1770. *pos++ = WLAN_EID_SSID;
  1771. *pos++ = 0x0;
  1772. mesh_mgmt_ies_add(skb, sdata);
  1773. } else {
  1774. WARN_ON(1);
  1775. goto out;
  1776. }
  1777. info = IEEE80211_SKB_CB(skb);
  1778. info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  1779. info->band = band;
  1780. /*
  1781. * XXX: For now, always use the lowest rate
  1782. */
  1783. info->control.rates[0].idx = 0;
  1784. info->control.rates[0].count = 1;
  1785. info->control.rates[1].idx = -1;
  1786. info->control.rates[2].idx = -1;
  1787. info->control.rates[3].idx = -1;
  1788. info->control.rates[4].idx = -1;
  1789. BUILD_BUG_ON(IEEE80211_TX_MAX_RATES != 5);
  1790. info->control.vif = vif;
  1791. info->flags |= IEEE80211_TX_CTL_NO_ACK;
  1792. info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
  1793. info->flags |= IEEE80211_TX_CTL_ASSIGN_SEQ;
  1794. out:
  1795. rcu_read_unlock();
  1796. return skb;
  1797. }
  1798. EXPORT_SYMBOL(ieee80211_beacon_get);
  1799. void ieee80211_rts_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  1800. const void *frame, size_t frame_len,
  1801. const struct ieee80211_tx_info *frame_txctl,
  1802. struct ieee80211_rts *rts)
  1803. {
  1804. const struct ieee80211_hdr *hdr = frame;
  1805. rts->frame_control =
  1806. cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS);
  1807. rts->duration = ieee80211_rts_duration(hw, vif, frame_len,
  1808. frame_txctl);
  1809. memcpy(rts->ra, hdr->addr1, sizeof(rts->ra));
  1810. memcpy(rts->ta, hdr->addr2, sizeof(rts->ta));
  1811. }
  1812. EXPORT_SYMBOL(ieee80211_rts_get);
  1813. void ieee80211_ctstoself_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  1814. const void *frame, size_t frame_len,
  1815. const struct ieee80211_tx_info *frame_txctl,
  1816. struct ieee80211_cts *cts)
  1817. {
  1818. const struct ieee80211_hdr *hdr = frame;
  1819. cts->frame_control =
  1820. cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS);
  1821. cts->duration = ieee80211_ctstoself_duration(hw, vif,
  1822. frame_len, frame_txctl);
  1823. memcpy(cts->ra, hdr->addr1, sizeof(cts->ra));
  1824. }
  1825. EXPORT_SYMBOL(ieee80211_ctstoself_get);
  1826. struct sk_buff *
  1827. ieee80211_get_buffered_bc(struct ieee80211_hw *hw,
  1828. struct ieee80211_vif *vif)
  1829. {
  1830. struct ieee80211_local *local = hw_to_local(hw);
  1831. struct sk_buff *skb = NULL;
  1832. struct sta_info *sta;
  1833. struct ieee80211_tx_data tx;
  1834. struct ieee80211_sub_if_data *sdata;
  1835. struct ieee80211_if_ap *bss = NULL;
  1836. struct beacon_data *beacon;
  1837. struct ieee80211_tx_info *info;
  1838. sdata = vif_to_sdata(vif);
  1839. bss = &sdata->u.ap;
  1840. rcu_read_lock();
  1841. beacon = rcu_dereference(bss->beacon);
  1842. if (sdata->vif.type != NL80211_IFTYPE_AP || !beacon || !beacon->head)
  1843. goto out;
  1844. if (bss->dtim_count != 0)
  1845. goto out; /* send buffered bc/mc only after DTIM beacon */
  1846. while (1) {
  1847. skb = skb_dequeue(&bss->ps_bc_buf);
  1848. if (!skb)
  1849. goto out;
  1850. local->total_ps_buffered--;
  1851. if (!skb_queue_empty(&bss->ps_bc_buf) && skb->len >= 2) {
  1852. struct ieee80211_hdr *hdr =
  1853. (struct ieee80211_hdr *) skb->data;
  1854. /* more buffered multicast/broadcast frames ==> set
  1855. * MoreData flag in IEEE 802.11 header to inform PS
  1856. * STAs */
  1857. hdr->frame_control |=
  1858. cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  1859. }
  1860. if (!ieee80211_tx_prepare(sdata, &tx, skb))
  1861. break;
  1862. dev_kfree_skb_any(skb);
  1863. }
  1864. info = IEEE80211_SKB_CB(skb);
  1865. sta = tx.sta;
  1866. tx.flags |= IEEE80211_TX_PS_BUFFERED;
  1867. tx.channel = local->hw.conf.channel;
  1868. info->band = tx.channel->band;
  1869. if (invoke_tx_handlers(&tx))
  1870. skb = NULL;
  1871. out:
  1872. rcu_read_unlock();
  1873. return skb;
  1874. }
  1875. EXPORT_SYMBOL(ieee80211_get_buffered_bc);
  1876. void ieee80211_tx_skb(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb,
  1877. int encrypt)
  1878. {
  1879. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1880. skb_set_mac_header(skb, 0);
  1881. skb_set_network_header(skb, 0);
  1882. skb_set_transport_header(skb, 0);
  1883. if (!encrypt)
  1884. info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  1885. /*
  1886. * The other path calling ieee80211_xmit is from the tasklet,
  1887. * and while we can handle concurrent transmissions locking
  1888. * requirements are that we do not come into tx with bhs on.
  1889. */
  1890. local_bh_disable();
  1891. ieee80211_xmit(sdata, skb);
  1892. local_bh_enable();
  1893. }