tx.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919
  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 "ieee80211_led.h"
  27. #include "wep.h"
  28. #include "wpa.h"
  29. #include "wme.h"
  30. #include "ieee80211_rate.h"
  31. #define IEEE80211_TX_OK 0
  32. #define IEEE80211_TX_AGAIN 1
  33. #define IEEE80211_TX_FRAG_AGAIN 2
  34. /* misc utils */
  35. static inline void ieee80211_include_sequence(struct ieee80211_sub_if_data *sdata,
  36. struct ieee80211_hdr *hdr)
  37. {
  38. /* Set the sequence number for this frame. */
  39. hdr->seq_ctrl = cpu_to_le16(sdata->sequence);
  40. /* Increase the sequence number. */
  41. sdata->sequence = (sdata->sequence + 0x10) & IEEE80211_SCTL_SEQ;
  42. }
  43. #ifdef CONFIG_MAC80211_LOWTX_FRAME_DUMP
  44. static void ieee80211_dump_frame(const char *ifname, const char *title,
  45. const struct sk_buff *skb)
  46. {
  47. const struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  48. u16 fc;
  49. int hdrlen;
  50. printk(KERN_DEBUG "%s: %s (len=%d)", ifname, title, skb->len);
  51. if (skb->len < 4) {
  52. printk("\n");
  53. return;
  54. }
  55. fc = le16_to_cpu(hdr->frame_control);
  56. hdrlen = ieee80211_get_hdrlen(fc);
  57. if (hdrlen > skb->len)
  58. hdrlen = skb->len;
  59. if (hdrlen >= 4)
  60. printk(" FC=0x%04x DUR=0x%04x",
  61. fc, le16_to_cpu(hdr->duration_id));
  62. if (hdrlen >= 10)
  63. printk(" A1=" MAC_FMT, MAC_ARG(hdr->addr1));
  64. if (hdrlen >= 16)
  65. printk(" A2=" MAC_FMT, MAC_ARG(hdr->addr2));
  66. if (hdrlen >= 24)
  67. printk(" A3=" MAC_FMT, MAC_ARG(hdr->addr3));
  68. if (hdrlen >= 30)
  69. printk(" A4=" MAC_FMT, MAC_ARG(hdr->addr4));
  70. printk("\n");
  71. }
  72. #else /* CONFIG_MAC80211_LOWTX_FRAME_DUMP */
  73. static inline void ieee80211_dump_frame(const char *ifname, const char *title,
  74. struct sk_buff *skb)
  75. {
  76. }
  77. #endif /* CONFIG_MAC80211_LOWTX_FRAME_DUMP */
  78. static u16 ieee80211_duration(struct ieee80211_txrx_data *tx, int group_addr,
  79. int next_frag_len)
  80. {
  81. int rate, mrate, erp, dur, i;
  82. struct ieee80211_rate *txrate = tx->u.tx.rate;
  83. struct ieee80211_local *local = tx->local;
  84. struct ieee80211_hw_mode *mode = tx->u.tx.mode;
  85. erp = txrate->flags & IEEE80211_RATE_ERP;
  86. /*
  87. * data and mgmt (except PS Poll):
  88. * - during CFP: 32768
  89. * - during contention period:
  90. * if addr1 is group address: 0
  91. * if more fragments = 0 and addr1 is individual address: time to
  92. * transmit one ACK plus SIFS
  93. * if more fragments = 1 and addr1 is individual address: time to
  94. * transmit next fragment plus 2 x ACK plus 3 x SIFS
  95. *
  96. * IEEE 802.11, 9.6:
  97. * - control response frame (CTS or ACK) shall be transmitted using the
  98. * same rate as the immediately previous frame in the frame exchange
  99. * sequence, if this rate belongs to the PHY mandatory rates, or else
  100. * at the highest possible rate belonging to the PHY rates in the
  101. * BSSBasicRateSet
  102. */
  103. if ((tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL) {
  104. /* TODO: These control frames are not currently sent by
  105. * 80211.o, but should they be implemented, this function
  106. * needs to be updated to support duration field calculation.
  107. *
  108. * RTS: time needed to transmit pending data/mgmt frame plus
  109. * one CTS frame plus one ACK frame plus 3 x SIFS
  110. * CTS: duration of immediately previous RTS minus time
  111. * required to transmit CTS and its SIFS
  112. * ACK: 0 if immediately previous directed data/mgmt had
  113. * more=0, with more=1 duration in ACK frame is duration
  114. * from previous frame minus time needed to transmit ACK
  115. * and its SIFS
  116. * PS Poll: BIT(15) | BIT(14) | aid
  117. */
  118. return 0;
  119. }
  120. /* data/mgmt */
  121. if (0 /* FIX: data/mgmt during CFP */)
  122. return 32768;
  123. if (group_addr) /* Group address as the destination - no ACK */
  124. return 0;
  125. /* Individual destination address:
  126. * IEEE 802.11, Ch. 9.6 (after IEEE 802.11g changes)
  127. * CTS and ACK frames shall be transmitted using the highest rate in
  128. * basic rate set that is less than or equal to the rate of the
  129. * immediately previous frame and that is using the same modulation
  130. * (CCK or OFDM). If no basic rate set matches with these requirements,
  131. * the highest mandatory rate of the PHY that is less than or equal to
  132. * the rate of the previous frame is used.
  133. * Mandatory rates for IEEE 802.11g PHY: 1, 2, 5.5, 11, 6, 12, 24 Mbps
  134. */
  135. rate = -1;
  136. mrate = 10; /* use 1 Mbps if everything fails */
  137. for (i = 0; i < mode->num_rates; i++) {
  138. struct ieee80211_rate *r = &mode->rates[i];
  139. if (r->rate > txrate->rate)
  140. break;
  141. if (IEEE80211_RATE_MODULATION(txrate->flags) !=
  142. IEEE80211_RATE_MODULATION(r->flags))
  143. continue;
  144. if (r->flags & IEEE80211_RATE_BASIC)
  145. rate = r->rate;
  146. else if (r->flags & IEEE80211_RATE_MANDATORY)
  147. mrate = r->rate;
  148. }
  149. if (rate == -1) {
  150. /* No matching basic rate found; use highest suitable mandatory
  151. * PHY rate */
  152. rate = mrate;
  153. }
  154. /* Time needed to transmit ACK
  155. * (10 bytes + 4-byte FCS = 112 bits) plus SIFS; rounded up
  156. * to closest integer */
  157. dur = ieee80211_frame_duration(local, 10, rate, erp,
  158. tx->sdata->flags & IEEE80211_SDATA_SHORT_PREAMBLE);
  159. if (next_frag_len) {
  160. /* Frame is fragmented: duration increases with time needed to
  161. * transmit next fragment plus ACK and 2 x SIFS. */
  162. dur *= 2; /* ACK + SIFS */
  163. /* next fragment */
  164. dur += ieee80211_frame_duration(local, next_frag_len,
  165. txrate->rate, erp,
  166. tx->sdata->flags &
  167. IEEE80211_SDATA_SHORT_PREAMBLE);
  168. }
  169. return dur;
  170. }
  171. static inline int __ieee80211_queue_stopped(const struct ieee80211_local *local,
  172. int queue)
  173. {
  174. return test_bit(IEEE80211_LINK_STATE_XOFF, &local->state[queue]);
  175. }
  176. static inline int __ieee80211_queue_pending(const struct ieee80211_local *local,
  177. int queue)
  178. {
  179. return test_bit(IEEE80211_LINK_STATE_PENDING, &local->state[queue]);
  180. }
  181. static int inline is_ieee80211_device(struct net_device *dev,
  182. struct net_device *master)
  183. {
  184. return (wdev_priv(dev->ieee80211_ptr) ==
  185. wdev_priv(master->ieee80211_ptr));
  186. }
  187. /* tx handlers */
  188. static ieee80211_txrx_result
  189. ieee80211_tx_h_check_assoc(struct ieee80211_txrx_data *tx)
  190. {
  191. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  192. struct sk_buff *skb = tx->skb;
  193. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  194. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  195. u32 sta_flags;
  196. if (unlikely(tx->local->sta_scanning != 0) &&
  197. ((tx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
  198. (tx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_PROBE_REQ))
  199. return TXRX_DROP;
  200. if (tx->flags & IEEE80211_TXRXD_TXPS_BUFFERED)
  201. return TXRX_CONTINUE;
  202. sta_flags = tx->sta ? tx->sta->flags : 0;
  203. if (likely(tx->flags & IEEE80211_TXRXD_TXUNICAST)) {
  204. if (unlikely(!(sta_flags & WLAN_STA_ASSOC) &&
  205. tx->sdata->type != IEEE80211_IF_TYPE_IBSS &&
  206. (tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)) {
  207. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  208. printk(KERN_DEBUG "%s: dropped data frame to not "
  209. "associated station " MAC_FMT "\n",
  210. tx->dev->name, MAC_ARG(hdr->addr1));
  211. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  212. I802_DEBUG_INC(tx->local->tx_handlers_drop_not_assoc);
  213. return TXRX_DROP;
  214. }
  215. } else {
  216. if (unlikely((tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
  217. tx->local->num_sta == 0 &&
  218. tx->sdata->type != IEEE80211_IF_TYPE_IBSS)) {
  219. /*
  220. * No associated STAs - no need to send multicast
  221. * frames.
  222. */
  223. return TXRX_DROP;
  224. }
  225. return TXRX_CONTINUE;
  226. }
  227. if (unlikely(!tx->u.tx.mgmt_interface && tx->sdata->ieee802_1x &&
  228. !(sta_flags & WLAN_STA_AUTHORIZED))) {
  229. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  230. printk(KERN_DEBUG "%s: dropped frame to " MAC_FMT
  231. " (unauthorized port)\n", tx->dev->name,
  232. MAC_ARG(hdr->addr1));
  233. #endif
  234. I802_DEBUG_INC(tx->local->tx_handlers_drop_unauth_port);
  235. return TXRX_DROP;
  236. }
  237. return TXRX_CONTINUE;
  238. }
  239. static ieee80211_txrx_result
  240. ieee80211_tx_h_sequence(struct ieee80211_txrx_data *tx)
  241. {
  242. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
  243. if (ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_control)) >= 24)
  244. ieee80211_include_sequence(tx->sdata, hdr);
  245. return TXRX_CONTINUE;
  246. }
  247. /* This function is called whenever the AP is about to exceed the maximum limit
  248. * of buffered frames for power saving STAs. This situation should not really
  249. * happen often during normal operation, so dropping the oldest buffered packet
  250. * from each queue should be OK to make some room for new frames. */
  251. static void purge_old_ps_buffers(struct ieee80211_local *local)
  252. {
  253. int total = 0, purged = 0;
  254. struct sk_buff *skb;
  255. struct ieee80211_sub_if_data *sdata;
  256. struct sta_info *sta;
  257. read_lock(&local->sub_if_lock);
  258. list_for_each_entry(sdata, &local->sub_if_list, list) {
  259. struct ieee80211_if_ap *ap;
  260. if (sdata->dev == local->mdev ||
  261. sdata->type != IEEE80211_IF_TYPE_AP)
  262. continue;
  263. ap = &sdata->u.ap;
  264. skb = skb_dequeue(&ap->ps_bc_buf);
  265. if (skb) {
  266. purged++;
  267. dev_kfree_skb(skb);
  268. }
  269. total += skb_queue_len(&ap->ps_bc_buf);
  270. }
  271. read_unlock(&local->sub_if_lock);
  272. read_lock_bh(&local->sta_lock);
  273. list_for_each_entry(sta, &local->sta_list, list) {
  274. skb = skb_dequeue(&sta->ps_tx_buf);
  275. if (skb) {
  276. purged++;
  277. dev_kfree_skb(skb);
  278. }
  279. total += skb_queue_len(&sta->ps_tx_buf);
  280. }
  281. read_unlock_bh(&local->sta_lock);
  282. local->total_ps_buffered = total;
  283. printk(KERN_DEBUG "%s: PS buffers full - purged %d frames\n",
  284. local->mdev->name, purged);
  285. }
  286. static inline ieee80211_txrx_result
  287. ieee80211_tx_h_multicast_ps_buf(struct ieee80211_txrx_data *tx)
  288. {
  289. /* broadcast/multicast frame */
  290. /* If any of the associated stations is in power save mode,
  291. * the frame is buffered to be sent after DTIM beacon frame */
  292. if ((tx->local->hw.flags & IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING) &&
  293. tx->sdata->type != IEEE80211_IF_TYPE_WDS &&
  294. tx->sdata->bss && atomic_read(&tx->sdata->bss->num_sta_ps) &&
  295. !(tx->fc & IEEE80211_FCTL_ORDER)) {
  296. if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
  297. purge_old_ps_buffers(tx->local);
  298. if (skb_queue_len(&tx->sdata->bss->ps_bc_buf) >=
  299. AP_MAX_BC_BUFFER) {
  300. if (net_ratelimit()) {
  301. printk(KERN_DEBUG "%s: BC TX buffer full - "
  302. "dropping the oldest frame\n",
  303. tx->dev->name);
  304. }
  305. dev_kfree_skb(skb_dequeue(&tx->sdata->bss->ps_bc_buf));
  306. } else
  307. tx->local->total_ps_buffered++;
  308. skb_queue_tail(&tx->sdata->bss->ps_bc_buf, tx->skb);
  309. return TXRX_QUEUED;
  310. }
  311. return TXRX_CONTINUE;
  312. }
  313. static inline ieee80211_txrx_result
  314. ieee80211_tx_h_unicast_ps_buf(struct ieee80211_txrx_data *tx)
  315. {
  316. struct sta_info *sta = tx->sta;
  317. if (unlikely(!sta ||
  318. ((tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT &&
  319. (tx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_RESP)))
  320. return TXRX_CONTINUE;
  321. if (unlikely((sta->flags & WLAN_STA_PS) && !sta->pspoll)) {
  322. struct ieee80211_tx_packet_data *pkt_data;
  323. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  324. printk(KERN_DEBUG "STA " MAC_FMT " aid %d: PS buffer (entries "
  325. "before %d)\n",
  326. MAC_ARG(sta->addr), sta->aid,
  327. skb_queue_len(&sta->ps_tx_buf));
  328. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  329. sta->flags |= WLAN_STA_TIM;
  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. if (net_ratelimit()) {
  335. printk(KERN_DEBUG "%s: STA " MAC_FMT " TX "
  336. "buffer full - dropping oldest frame\n",
  337. tx->dev->name, MAC_ARG(sta->addr));
  338. }
  339. dev_kfree_skb(old);
  340. } else
  341. tx->local->total_ps_buffered++;
  342. /* Queue frame to be sent after STA sends an PS Poll frame */
  343. if (skb_queue_empty(&sta->ps_tx_buf)) {
  344. if (tx->local->ops->set_tim)
  345. tx->local->ops->set_tim(local_to_hw(tx->local),
  346. sta->aid, 1);
  347. if (tx->sdata->bss)
  348. bss_tim_set(tx->local, tx->sdata->bss, sta->aid);
  349. }
  350. pkt_data = (struct ieee80211_tx_packet_data *)tx->skb->cb;
  351. pkt_data->jiffies = jiffies;
  352. skb_queue_tail(&sta->ps_tx_buf, tx->skb);
  353. return TXRX_QUEUED;
  354. }
  355. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  356. else if (unlikely(sta->flags & WLAN_STA_PS)) {
  357. printk(KERN_DEBUG "%s: STA " MAC_FMT " in PS mode, but pspoll "
  358. "set -> send frame\n", tx->dev->name,
  359. MAC_ARG(sta->addr));
  360. }
  361. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  362. sta->pspoll = 0;
  363. return TXRX_CONTINUE;
  364. }
  365. static ieee80211_txrx_result
  366. ieee80211_tx_h_ps_buf(struct ieee80211_txrx_data *tx)
  367. {
  368. if (unlikely(tx->flags & IEEE80211_TXRXD_TXPS_BUFFERED))
  369. return TXRX_CONTINUE;
  370. if (tx->flags & IEEE80211_TXRXD_TXUNICAST)
  371. return ieee80211_tx_h_unicast_ps_buf(tx);
  372. else
  373. return ieee80211_tx_h_multicast_ps_buf(tx);
  374. }
  375. static ieee80211_txrx_result
  376. ieee80211_tx_h_select_key(struct ieee80211_txrx_data *tx)
  377. {
  378. struct ieee80211_key *key;
  379. tx->u.tx.control->key_idx = HW_KEY_IDX_INVALID;
  380. if (unlikely(tx->u.tx.control->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT))
  381. tx->key = NULL;
  382. else if (tx->sta && (key = rcu_dereference(tx->sta->key)))
  383. tx->key = key;
  384. else if ((key = rcu_dereference(tx->sdata->default_key)))
  385. tx->key = key;
  386. else if (tx->sdata->drop_unencrypted &&
  387. !(tx->sdata->eapol && ieee80211_is_eapol(tx->skb))) {
  388. I802_DEBUG_INC(tx->local->tx_handlers_drop_unencrypted);
  389. return TXRX_DROP;
  390. } else
  391. tx->key = NULL;
  392. if (tx->key) {
  393. tx->key->tx_rx_count++;
  394. if (unlikely(tx->local->key_tx_rx_threshold &&
  395. tx->key->tx_rx_count >
  396. tx->local->key_tx_rx_threshold)) {
  397. ieee80211_key_threshold_notify(tx->dev, tx->key,
  398. tx->sta);
  399. }
  400. }
  401. return TXRX_CONTINUE;
  402. }
  403. static ieee80211_txrx_result
  404. ieee80211_tx_h_fragment(struct ieee80211_txrx_data *tx)
  405. {
  406. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
  407. size_t hdrlen, per_fragm, num_fragm, payload_len, left;
  408. struct sk_buff **frags, *first, *frag;
  409. int i;
  410. u16 seq;
  411. u8 *pos;
  412. int frag_threshold = tx->local->fragmentation_threshold;
  413. if (!(tx->flags & IEEE80211_TXRXD_FRAGMENTED))
  414. return TXRX_CONTINUE;
  415. first = tx->skb;
  416. hdrlen = ieee80211_get_hdrlen(tx->fc);
  417. payload_len = first->len - hdrlen;
  418. per_fragm = frag_threshold - hdrlen - FCS_LEN;
  419. num_fragm = DIV_ROUND_UP(payload_len, per_fragm);
  420. frags = kzalloc(num_fragm * sizeof(struct sk_buff *), GFP_ATOMIC);
  421. if (!frags)
  422. goto fail;
  423. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREFRAGS);
  424. seq = le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_SEQ;
  425. pos = first->data + hdrlen + per_fragm;
  426. left = payload_len - per_fragm;
  427. for (i = 0; i < num_fragm - 1; i++) {
  428. struct ieee80211_hdr *fhdr;
  429. size_t copylen;
  430. if (left <= 0)
  431. goto fail;
  432. /* reserve enough extra head and tail room for possible
  433. * encryption */
  434. frag = frags[i] =
  435. dev_alloc_skb(tx->local->tx_headroom +
  436. frag_threshold +
  437. IEEE80211_ENCRYPT_HEADROOM +
  438. IEEE80211_ENCRYPT_TAILROOM);
  439. if (!frag)
  440. goto fail;
  441. /* Make sure that all fragments use the same priority so
  442. * that they end up using the same TX queue */
  443. frag->priority = first->priority;
  444. skb_reserve(frag, tx->local->tx_headroom +
  445. IEEE80211_ENCRYPT_HEADROOM);
  446. fhdr = (struct ieee80211_hdr *) skb_put(frag, hdrlen);
  447. memcpy(fhdr, first->data, hdrlen);
  448. if (i == num_fragm - 2)
  449. fhdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREFRAGS);
  450. fhdr->seq_ctrl = cpu_to_le16(seq | ((i + 1) & IEEE80211_SCTL_FRAG));
  451. copylen = left > per_fragm ? per_fragm : left;
  452. memcpy(skb_put(frag, copylen), pos, copylen);
  453. pos += copylen;
  454. left -= copylen;
  455. }
  456. skb_trim(first, hdrlen + per_fragm);
  457. tx->u.tx.num_extra_frag = num_fragm - 1;
  458. tx->u.tx.extra_frag = frags;
  459. return TXRX_CONTINUE;
  460. fail:
  461. printk(KERN_DEBUG "%s: failed to fragment frame\n", tx->dev->name);
  462. if (frags) {
  463. for (i = 0; i < num_fragm - 1; i++)
  464. if (frags[i])
  465. dev_kfree_skb(frags[i]);
  466. kfree(frags);
  467. }
  468. I802_DEBUG_INC(tx->local->tx_handlers_drop_fragment);
  469. return TXRX_DROP;
  470. }
  471. static int wep_encrypt_skb(struct ieee80211_txrx_data *tx, struct sk_buff *skb)
  472. {
  473. if (!(tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)) {
  474. if (ieee80211_wep_encrypt(tx->local, skb, tx->key))
  475. return -1;
  476. } else {
  477. tx->u.tx.control->key_idx = tx->key->conf.hw_key_idx;
  478. if (tx->key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV) {
  479. if (!ieee80211_wep_add_iv(tx->local, skb, tx->key))
  480. return -1;
  481. }
  482. }
  483. return 0;
  484. }
  485. static ieee80211_txrx_result
  486. ieee80211_tx_h_wep_encrypt(struct ieee80211_txrx_data *tx)
  487. {
  488. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
  489. u16 fc;
  490. fc = le16_to_cpu(hdr->frame_control);
  491. if (!tx->key || tx->key->conf.alg != ALG_WEP ||
  492. ((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA &&
  493. ((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
  494. (fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_AUTH)))
  495. return TXRX_CONTINUE;
  496. tx->u.tx.control->iv_len = WEP_IV_LEN;
  497. tx->u.tx.control->icv_len = WEP_ICV_LEN;
  498. ieee80211_tx_set_iswep(tx);
  499. if (wep_encrypt_skb(tx, tx->skb) < 0) {
  500. I802_DEBUG_INC(tx->local->tx_handlers_drop_wep);
  501. return TXRX_DROP;
  502. }
  503. if (tx->u.tx.extra_frag) {
  504. int i;
  505. for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
  506. if (wep_encrypt_skb(tx, tx->u.tx.extra_frag[i]) < 0) {
  507. I802_DEBUG_INC(tx->local->
  508. tx_handlers_drop_wep);
  509. return TXRX_DROP;
  510. }
  511. }
  512. }
  513. return TXRX_CONTINUE;
  514. }
  515. static ieee80211_txrx_result
  516. ieee80211_tx_h_rate_ctrl(struct ieee80211_txrx_data *tx)
  517. {
  518. struct rate_control_extra extra;
  519. memset(&extra, 0, sizeof(extra));
  520. extra.mode = tx->u.tx.mode;
  521. extra.mgmt_data = tx->sdata &&
  522. tx->sdata->type == IEEE80211_IF_TYPE_MGMT;
  523. extra.ethertype = tx->ethertype;
  524. tx->u.tx.rate = rate_control_get_rate(tx->local, tx->dev, tx->skb,
  525. &extra);
  526. if (unlikely(extra.probe != NULL)) {
  527. tx->u.tx.control->flags |= IEEE80211_TXCTL_RATE_CTRL_PROBE;
  528. tx->flags |= IEEE80211_TXRXD_TXPROBE_LAST_FRAG;
  529. tx->u.tx.control->alt_retry_rate = tx->u.tx.rate->val;
  530. tx->u.tx.rate = extra.probe;
  531. } else {
  532. tx->u.tx.control->alt_retry_rate = -1;
  533. }
  534. if (!tx->u.tx.rate)
  535. return TXRX_DROP;
  536. if (tx->u.tx.mode->mode == MODE_IEEE80211G &&
  537. (tx->sdata->flags & IEEE80211_SDATA_USE_PROTECTION) &&
  538. (tx->flags & IEEE80211_TXRXD_FRAGMENTED) && extra.nonerp) {
  539. tx->u.tx.last_frag_rate = tx->u.tx.rate;
  540. if (extra.probe)
  541. tx->flags &= ~IEEE80211_TXRXD_TXPROBE_LAST_FRAG;
  542. else
  543. tx->flags |= IEEE80211_TXRXD_TXPROBE_LAST_FRAG;
  544. tx->u.tx.rate = extra.nonerp;
  545. tx->u.tx.control->rate = extra.nonerp;
  546. tx->u.tx.control->flags &= ~IEEE80211_TXCTL_RATE_CTRL_PROBE;
  547. } else {
  548. tx->u.tx.last_frag_rate = tx->u.tx.rate;
  549. tx->u.tx.control->rate = tx->u.tx.rate;
  550. }
  551. tx->u.tx.control->tx_rate = tx->u.tx.rate->val;
  552. return TXRX_CONTINUE;
  553. }
  554. static ieee80211_txrx_result
  555. ieee80211_tx_h_misc(struct ieee80211_txrx_data *tx)
  556. {
  557. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
  558. u16 fc = le16_to_cpu(hdr->frame_control);
  559. u16 dur;
  560. struct ieee80211_tx_control *control = tx->u.tx.control;
  561. struct ieee80211_hw_mode *mode = tx->u.tx.mode;
  562. if (!is_multicast_ether_addr(hdr->addr1)) {
  563. if (tx->skb->len + FCS_LEN > tx->local->rts_threshold &&
  564. tx->local->rts_threshold < IEEE80211_MAX_RTS_THRESHOLD) {
  565. control->flags |= IEEE80211_TXCTL_USE_RTS_CTS;
  566. control->flags |= IEEE80211_TXCTL_LONG_RETRY_LIMIT;
  567. control->retry_limit =
  568. tx->local->long_retry_limit;
  569. } else {
  570. control->retry_limit =
  571. tx->local->short_retry_limit;
  572. }
  573. } else {
  574. control->retry_limit = 1;
  575. }
  576. if (tx->flags & IEEE80211_TXRXD_FRAGMENTED) {
  577. /* Do not use multiple retry rates when sending fragmented
  578. * frames.
  579. * TODO: The last fragment could still use multiple retry
  580. * rates. */
  581. control->alt_retry_rate = -1;
  582. }
  583. /* Use CTS protection for unicast frames sent using extended rates if
  584. * there are associated non-ERP stations and RTS/CTS is not configured
  585. * for the frame. */
  586. if (mode->mode == MODE_IEEE80211G &&
  587. (tx->u.tx.rate->flags & IEEE80211_RATE_ERP) &&
  588. (tx->flags & IEEE80211_TXRXD_TXUNICAST) &&
  589. (tx->sdata->flags & IEEE80211_SDATA_USE_PROTECTION) &&
  590. !(control->flags & IEEE80211_TXCTL_USE_RTS_CTS))
  591. control->flags |= IEEE80211_TXCTL_USE_CTS_PROTECT;
  592. /* Transmit data frames using short preambles if the driver supports
  593. * short preambles at the selected rate and short preambles are
  594. * available on the network at the current point in time. */
  595. if (((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
  596. (tx->u.tx.rate->flags & IEEE80211_RATE_PREAMBLE2) &&
  597. (tx->sdata->flags & IEEE80211_SDATA_SHORT_PREAMBLE) &&
  598. (!tx->sta || (tx->sta->flags & WLAN_STA_SHORT_PREAMBLE))) {
  599. tx->u.tx.control->tx_rate = tx->u.tx.rate->val2;
  600. }
  601. /* Setup duration field for the first fragment of the frame. Duration
  602. * for remaining fragments will be updated when they are being sent
  603. * to low-level driver in ieee80211_tx(). */
  604. dur = ieee80211_duration(tx, is_multicast_ether_addr(hdr->addr1),
  605. (tx->flags & IEEE80211_TXRXD_FRAGMENTED) ?
  606. tx->u.tx.extra_frag[0]->len : 0);
  607. hdr->duration_id = cpu_to_le16(dur);
  608. if ((control->flags & IEEE80211_TXCTL_USE_RTS_CTS) ||
  609. (control->flags & IEEE80211_TXCTL_USE_CTS_PROTECT)) {
  610. struct ieee80211_rate *rate;
  611. /* Do not use multiple retry rates when using RTS/CTS */
  612. control->alt_retry_rate = -1;
  613. /* Use min(data rate, max base rate) as CTS/RTS rate */
  614. rate = tx->u.tx.rate;
  615. while (rate > mode->rates &&
  616. !(rate->flags & IEEE80211_RATE_BASIC))
  617. rate--;
  618. control->rts_cts_rate = rate->val;
  619. control->rts_rate = rate;
  620. }
  621. if (tx->sta) {
  622. tx->sta->tx_packets++;
  623. tx->sta->tx_fragments++;
  624. tx->sta->tx_bytes += tx->skb->len;
  625. if (tx->u.tx.extra_frag) {
  626. int i;
  627. tx->sta->tx_fragments += tx->u.tx.num_extra_frag;
  628. for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
  629. tx->sta->tx_bytes +=
  630. tx->u.tx.extra_frag[i]->len;
  631. }
  632. }
  633. }
  634. return TXRX_CONTINUE;
  635. }
  636. static ieee80211_txrx_result
  637. ieee80211_tx_h_load_stats(struct ieee80211_txrx_data *tx)
  638. {
  639. struct ieee80211_local *local = tx->local;
  640. struct ieee80211_hw_mode *mode = tx->u.tx.mode;
  641. struct sk_buff *skb = tx->skb;
  642. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  643. u32 load = 0, hdrtime;
  644. /* TODO: this could be part of tx_status handling, so that the number
  645. * of retries would be known; TX rate should in that case be stored
  646. * somewhere with the packet */
  647. /* Estimate total channel use caused by this frame */
  648. /* 1 bit at 1 Mbit/s takes 1 usec; in channel_use values,
  649. * 1 usec = 1/8 * (1080 / 10) = 13.5 */
  650. if (mode->mode == MODE_IEEE80211A ||
  651. (mode->mode == MODE_IEEE80211G &&
  652. tx->u.tx.rate->flags & IEEE80211_RATE_ERP))
  653. hdrtime = CHAN_UTIL_HDR_SHORT;
  654. else
  655. hdrtime = CHAN_UTIL_HDR_LONG;
  656. load = hdrtime;
  657. if (!is_multicast_ether_addr(hdr->addr1))
  658. load += hdrtime;
  659. if (tx->u.tx.control->flags & IEEE80211_TXCTL_USE_RTS_CTS)
  660. load += 2 * hdrtime;
  661. else if (tx->u.tx.control->flags & IEEE80211_TXCTL_USE_CTS_PROTECT)
  662. load += hdrtime;
  663. load += skb->len * tx->u.tx.rate->rate_inv;
  664. if (tx->u.tx.extra_frag) {
  665. int i;
  666. for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
  667. load += 2 * hdrtime;
  668. load += tx->u.tx.extra_frag[i]->len *
  669. tx->u.tx.rate->rate;
  670. }
  671. }
  672. /* Divide channel_use by 8 to avoid wrapping around the counter */
  673. load >>= CHAN_UTIL_SHIFT;
  674. local->channel_use_raw += load;
  675. if (tx->sta)
  676. tx->sta->channel_use_raw += load;
  677. tx->sdata->channel_use_raw += load;
  678. return TXRX_CONTINUE;
  679. }
  680. /* TODO: implement register/unregister functions for adding TX/RX handlers
  681. * into ordered list */
  682. ieee80211_tx_handler ieee80211_tx_handlers[] =
  683. {
  684. ieee80211_tx_h_check_assoc,
  685. ieee80211_tx_h_sequence,
  686. ieee80211_tx_h_ps_buf,
  687. ieee80211_tx_h_select_key,
  688. ieee80211_tx_h_michael_mic_add,
  689. ieee80211_tx_h_fragment,
  690. ieee80211_tx_h_tkip_encrypt,
  691. ieee80211_tx_h_ccmp_encrypt,
  692. ieee80211_tx_h_wep_encrypt,
  693. ieee80211_tx_h_rate_ctrl,
  694. ieee80211_tx_h_misc,
  695. ieee80211_tx_h_load_stats,
  696. NULL
  697. };
  698. /* actual transmit path */
  699. /*
  700. * deal with packet injection down monitor interface
  701. * with Radiotap Header -- only called for monitor mode interface
  702. */
  703. static ieee80211_txrx_result
  704. __ieee80211_parse_tx_radiotap(
  705. struct ieee80211_txrx_data *tx,
  706. struct sk_buff *skb, struct ieee80211_tx_control *control)
  707. {
  708. /*
  709. * this is the moment to interpret and discard the radiotap header that
  710. * must be at the start of the packet injected in Monitor mode
  711. *
  712. * Need to take some care with endian-ness since radiotap
  713. * args are little-endian
  714. */
  715. struct ieee80211_radiotap_iterator iterator;
  716. struct ieee80211_radiotap_header *rthdr =
  717. (struct ieee80211_radiotap_header *) skb->data;
  718. struct ieee80211_hw_mode *mode = tx->local->hw.conf.mode;
  719. int ret = ieee80211_radiotap_iterator_init(&iterator, rthdr, skb->len);
  720. /*
  721. * default control situation for all injected packets
  722. * FIXME: this does not suit all usage cases, expand to allow control
  723. */
  724. control->retry_limit = 1; /* no retry */
  725. control->key_idx = HW_KEY_IDX_INVALID;
  726. control->flags &= ~(IEEE80211_TXCTL_USE_RTS_CTS |
  727. IEEE80211_TXCTL_USE_CTS_PROTECT);
  728. control->flags |= IEEE80211_TXCTL_DO_NOT_ENCRYPT |
  729. IEEE80211_TXCTL_NO_ACK;
  730. control->antenna_sel_tx = 0; /* default to default antenna */
  731. /*
  732. * for every radiotap entry that is present
  733. * (ieee80211_radiotap_iterator_next returns -ENOENT when no more
  734. * entries present, or -EINVAL on error)
  735. */
  736. while (!ret) {
  737. int i, target_rate;
  738. ret = ieee80211_radiotap_iterator_next(&iterator);
  739. if (ret)
  740. continue;
  741. /* see if this argument is something we can use */
  742. switch (iterator.this_arg_index) {
  743. /*
  744. * You must take care when dereferencing iterator.this_arg
  745. * for multibyte types... the pointer is not aligned. Use
  746. * get_unaligned((type *)iterator.this_arg) to dereference
  747. * iterator.this_arg for type "type" safely on all arches.
  748. */
  749. case IEEE80211_RADIOTAP_RATE:
  750. /*
  751. * radiotap rate u8 is in 500kbps units eg, 0x02=1Mbps
  752. * ieee80211 rate int is in 100kbps units eg, 0x0a=1Mbps
  753. */
  754. target_rate = (*iterator.this_arg) * 5;
  755. for (i = 0; i < mode->num_rates; i++) {
  756. struct ieee80211_rate *r = &mode->rates[i];
  757. if (r->rate > target_rate)
  758. continue;
  759. control->rate = r;
  760. if (r->flags & IEEE80211_RATE_PREAMBLE2)
  761. control->tx_rate = r->val2;
  762. else
  763. control->tx_rate = r->val;
  764. /* end on exact match */
  765. if (r->rate == target_rate)
  766. i = mode->num_rates;
  767. }
  768. break;
  769. case IEEE80211_RADIOTAP_ANTENNA:
  770. /*
  771. * radiotap uses 0 for 1st ant, mac80211 is 1 for
  772. * 1st ant
  773. */
  774. control->antenna_sel_tx = (*iterator.this_arg) + 1;
  775. break;
  776. case IEEE80211_RADIOTAP_DBM_TX_POWER:
  777. control->power_level = *iterator.this_arg;
  778. break;
  779. case IEEE80211_RADIOTAP_FLAGS:
  780. if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FCS) {
  781. /*
  782. * this indicates that the skb we have been
  783. * handed has the 32-bit FCS CRC at the end...
  784. * we should react to that by snipping it off
  785. * because it will be recomputed and added
  786. * on transmission
  787. */
  788. if (skb->len < (iterator.max_length + FCS_LEN))
  789. return TXRX_DROP;
  790. skb_trim(skb, skb->len - FCS_LEN);
  791. }
  792. break;
  793. default:
  794. break;
  795. }
  796. }
  797. if (ret != -ENOENT) /* ie, if we didn't simply run out of fields */
  798. return TXRX_DROP;
  799. /*
  800. * remove the radiotap header
  801. * iterator->max_length was sanity-checked against
  802. * skb->len by iterator init
  803. */
  804. skb_pull(skb, iterator.max_length);
  805. return TXRX_CONTINUE;
  806. }
  807. static ieee80211_txrx_result inline
  808. __ieee80211_tx_prepare(struct ieee80211_txrx_data *tx,
  809. struct sk_buff *skb,
  810. struct net_device *dev,
  811. struct ieee80211_tx_control *control)
  812. {
  813. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  814. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  815. struct ieee80211_sub_if_data *sdata;
  816. ieee80211_txrx_result res = TXRX_CONTINUE;
  817. int hdrlen;
  818. memset(tx, 0, sizeof(*tx));
  819. tx->skb = skb;
  820. tx->dev = dev; /* use original interface */
  821. tx->local = local;
  822. tx->sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  823. tx->sta = sta_info_get(local, hdr->addr1);
  824. tx->fc = le16_to_cpu(hdr->frame_control);
  825. /*
  826. * set defaults for things that can be set by
  827. * injected radiotap headers
  828. */
  829. control->power_level = local->hw.conf.power_level;
  830. control->antenna_sel_tx = local->hw.conf.antenna_sel_tx;
  831. /* process and remove the injection radiotap header */
  832. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  833. if (unlikely(sdata->type == IEEE80211_IF_TYPE_MNTR)) {
  834. if (__ieee80211_parse_tx_radiotap(tx, skb, control) ==
  835. TXRX_DROP) {
  836. return TXRX_DROP;
  837. }
  838. /*
  839. * we removed the radiotap header after this point,
  840. * we filled control with what we could use
  841. * set to the actual ieee header now
  842. */
  843. hdr = (struct ieee80211_hdr *) skb->data;
  844. res = TXRX_QUEUED; /* indication it was monitor packet */
  845. }
  846. tx->u.tx.control = control;
  847. if (is_multicast_ether_addr(hdr->addr1)) {
  848. tx->flags &= ~IEEE80211_TXRXD_TXUNICAST;
  849. control->flags |= IEEE80211_TXCTL_NO_ACK;
  850. } else {
  851. tx->flags |= IEEE80211_TXRXD_TXUNICAST;
  852. control->flags &= ~IEEE80211_TXCTL_NO_ACK;
  853. }
  854. if (local->fragmentation_threshold < IEEE80211_MAX_FRAG_THRESHOLD &&
  855. (tx->flags & IEEE80211_TXRXD_TXUNICAST) &&
  856. skb->len + FCS_LEN > local->fragmentation_threshold &&
  857. !local->ops->set_frag_threshold)
  858. tx->flags |= IEEE80211_TXRXD_FRAGMENTED;
  859. else
  860. tx->flags &= ~IEEE80211_TXRXD_FRAGMENTED;
  861. if (!tx->sta)
  862. control->flags |= IEEE80211_TXCTL_CLEAR_DST_MASK;
  863. else if (tx->sta->clear_dst_mask) {
  864. control->flags |= IEEE80211_TXCTL_CLEAR_DST_MASK;
  865. tx->sta->clear_dst_mask = 0;
  866. }
  867. hdrlen = ieee80211_get_hdrlen(tx->fc);
  868. if (skb->len > hdrlen + sizeof(rfc1042_header) + 2) {
  869. u8 *pos = &skb->data[hdrlen + sizeof(rfc1042_header)];
  870. tx->ethertype = (pos[0] << 8) | pos[1];
  871. }
  872. control->flags |= IEEE80211_TXCTL_FIRST_FRAGMENT;
  873. return res;
  874. }
  875. /* Device in tx->dev has a reference added; use dev_put(tx->dev) when
  876. * finished with it. */
  877. static int inline ieee80211_tx_prepare(struct ieee80211_txrx_data *tx,
  878. struct sk_buff *skb,
  879. struct net_device *mdev,
  880. struct ieee80211_tx_control *control)
  881. {
  882. struct ieee80211_tx_packet_data *pkt_data;
  883. struct net_device *dev;
  884. pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
  885. dev = dev_get_by_index(&init_net, pkt_data->ifindex);
  886. if (unlikely(dev && !is_ieee80211_device(dev, mdev))) {
  887. dev_put(dev);
  888. dev = NULL;
  889. }
  890. if (unlikely(!dev))
  891. return -ENODEV;
  892. __ieee80211_tx_prepare(tx, skb, dev, control);
  893. return 0;
  894. }
  895. static int __ieee80211_tx(struct ieee80211_local *local, struct sk_buff *skb,
  896. struct ieee80211_txrx_data *tx)
  897. {
  898. struct ieee80211_tx_control *control = tx->u.tx.control;
  899. int ret, i;
  900. if (!ieee80211_qdisc_installed(local->mdev) &&
  901. __ieee80211_queue_stopped(local, 0)) {
  902. netif_stop_queue(local->mdev);
  903. return IEEE80211_TX_AGAIN;
  904. }
  905. if (skb) {
  906. ieee80211_dump_frame(local->mdev->name, "TX to low-level driver", skb);
  907. ret = local->ops->tx(local_to_hw(local), skb, control);
  908. if (ret)
  909. return IEEE80211_TX_AGAIN;
  910. local->mdev->trans_start = jiffies;
  911. ieee80211_led_tx(local, 1);
  912. }
  913. if (tx->u.tx.extra_frag) {
  914. control->flags &= ~(IEEE80211_TXCTL_USE_RTS_CTS |
  915. IEEE80211_TXCTL_USE_CTS_PROTECT |
  916. IEEE80211_TXCTL_CLEAR_DST_MASK |
  917. IEEE80211_TXCTL_FIRST_FRAGMENT);
  918. for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
  919. if (!tx->u.tx.extra_frag[i])
  920. continue;
  921. if (__ieee80211_queue_stopped(local, control->queue))
  922. return IEEE80211_TX_FRAG_AGAIN;
  923. if (i == tx->u.tx.num_extra_frag) {
  924. control->tx_rate = tx->u.tx.last_frag_hwrate;
  925. control->rate = tx->u.tx.last_frag_rate;
  926. if (tx->flags & IEEE80211_TXRXD_TXPROBE_LAST_FRAG)
  927. control->flags |=
  928. IEEE80211_TXCTL_RATE_CTRL_PROBE;
  929. else
  930. control->flags &=
  931. ~IEEE80211_TXCTL_RATE_CTRL_PROBE;
  932. }
  933. ieee80211_dump_frame(local->mdev->name,
  934. "TX to low-level driver",
  935. tx->u.tx.extra_frag[i]);
  936. ret = local->ops->tx(local_to_hw(local),
  937. tx->u.tx.extra_frag[i],
  938. control);
  939. if (ret)
  940. return IEEE80211_TX_FRAG_AGAIN;
  941. local->mdev->trans_start = jiffies;
  942. ieee80211_led_tx(local, 1);
  943. tx->u.tx.extra_frag[i] = NULL;
  944. }
  945. kfree(tx->u.tx.extra_frag);
  946. tx->u.tx.extra_frag = NULL;
  947. }
  948. return IEEE80211_TX_OK;
  949. }
  950. static int ieee80211_tx(struct net_device *dev, struct sk_buff *skb,
  951. struct ieee80211_tx_control *control, int mgmt)
  952. {
  953. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  954. struct sta_info *sta;
  955. ieee80211_tx_handler *handler;
  956. struct ieee80211_txrx_data tx;
  957. ieee80211_txrx_result res = TXRX_DROP, res_prepare;
  958. int ret, i;
  959. WARN_ON(__ieee80211_queue_pending(local, control->queue));
  960. if (unlikely(skb->len < 10)) {
  961. dev_kfree_skb(skb);
  962. return 0;
  963. }
  964. res_prepare = __ieee80211_tx_prepare(&tx, skb, dev, control);
  965. if (res_prepare == TXRX_DROP) {
  966. dev_kfree_skb(skb);
  967. return 0;
  968. }
  969. /*
  970. * key references are protected using RCU and this requires that
  971. * we are in a read-site RCU section during receive processing
  972. */
  973. rcu_read_lock();
  974. sta = tx.sta;
  975. tx.u.tx.mgmt_interface = mgmt;
  976. tx.u.tx.mode = local->hw.conf.mode;
  977. if (res_prepare == TXRX_QUEUED) { /* if it was an injected packet */
  978. res = TXRX_CONTINUE;
  979. } else {
  980. for (handler = local->tx_handlers; *handler != NULL;
  981. handler++) {
  982. res = (*handler)(&tx);
  983. if (res != TXRX_CONTINUE)
  984. break;
  985. }
  986. }
  987. skb = tx.skb; /* handlers are allowed to change skb */
  988. if (sta)
  989. sta_info_put(sta);
  990. if (unlikely(res == TXRX_DROP)) {
  991. I802_DEBUG_INC(local->tx_handlers_drop);
  992. goto drop;
  993. }
  994. if (unlikely(res == TXRX_QUEUED)) {
  995. I802_DEBUG_INC(local->tx_handlers_queued);
  996. rcu_read_unlock();
  997. return 0;
  998. }
  999. if (tx.u.tx.extra_frag) {
  1000. for (i = 0; i < tx.u.tx.num_extra_frag; i++) {
  1001. int next_len, dur;
  1002. struct ieee80211_hdr *hdr =
  1003. (struct ieee80211_hdr *)
  1004. tx.u.tx.extra_frag[i]->data;
  1005. if (i + 1 < tx.u.tx.num_extra_frag) {
  1006. next_len = tx.u.tx.extra_frag[i + 1]->len;
  1007. } else {
  1008. next_len = 0;
  1009. tx.u.tx.rate = tx.u.tx.last_frag_rate;
  1010. tx.u.tx.last_frag_hwrate = tx.u.tx.rate->val;
  1011. }
  1012. dur = ieee80211_duration(&tx, 0, next_len);
  1013. hdr->duration_id = cpu_to_le16(dur);
  1014. }
  1015. }
  1016. retry:
  1017. ret = __ieee80211_tx(local, skb, &tx);
  1018. if (ret) {
  1019. struct ieee80211_tx_stored_packet *store =
  1020. &local->pending_packet[control->queue];
  1021. if (ret == IEEE80211_TX_FRAG_AGAIN)
  1022. skb = NULL;
  1023. set_bit(IEEE80211_LINK_STATE_PENDING,
  1024. &local->state[control->queue]);
  1025. smp_mb();
  1026. /* When the driver gets out of buffers during sending of
  1027. * fragments and calls ieee80211_stop_queue, there is
  1028. * a small window between IEEE80211_LINK_STATE_XOFF and
  1029. * IEEE80211_LINK_STATE_PENDING flags are set. If a buffer
  1030. * gets available in that window (i.e. driver calls
  1031. * ieee80211_wake_queue), we would end up with ieee80211_tx
  1032. * called with IEEE80211_LINK_STATE_PENDING. Prevent this by
  1033. * continuing transmitting here when that situation is
  1034. * possible to have happened. */
  1035. if (!__ieee80211_queue_stopped(local, control->queue)) {
  1036. clear_bit(IEEE80211_LINK_STATE_PENDING,
  1037. &local->state[control->queue]);
  1038. goto retry;
  1039. }
  1040. memcpy(&store->control, control,
  1041. sizeof(struct ieee80211_tx_control));
  1042. store->skb = skb;
  1043. store->extra_frag = tx.u.tx.extra_frag;
  1044. store->num_extra_frag = tx.u.tx.num_extra_frag;
  1045. store->last_frag_hwrate = tx.u.tx.last_frag_hwrate;
  1046. store->last_frag_rate = tx.u.tx.last_frag_rate;
  1047. store->last_frag_rate_ctrl_probe =
  1048. !!(tx.flags & IEEE80211_TXRXD_TXPROBE_LAST_FRAG);
  1049. }
  1050. rcu_read_unlock();
  1051. return 0;
  1052. drop:
  1053. if (skb)
  1054. dev_kfree_skb(skb);
  1055. for (i = 0; i < tx.u.tx.num_extra_frag; i++)
  1056. if (tx.u.tx.extra_frag[i])
  1057. dev_kfree_skb(tx.u.tx.extra_frag[i]);
  1058. kfree(tx.u.tx.extra_frag);
  1059. rcu_read_unlock();
  1060. return 0;
  1061. }
  1062. /* device xmit handlers */
  1063. int ieee80211_master_start_xmit(struct sk_buff *skb,
  1064. struct net_device *dev)
  1065. {
  1066. struct ieee80211_tx_control control;
  1067. struct ieee80211_tx_packet_data *pkt_data;
  1068. struct net_device *odev = NULL;
  1069. struct ieee80211_sub_if_data *osdata;
  1070. int headroom;
  1071. int ret;
  1072. /*
  1073. * copy control out of the skb so other people can use skb->cb
  1074. */
  1075. pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
  1076. memset(&control, 0, sizeof(struct ieee80211_tx_control));
  1077. if (pkt_data->ifindex)
  1078. odev = dev_get_by_index(&init_net, pkt_data->ifindex);
  1079. if (unlikely(odev && !is_ieee80211_device(odev, dev))) {
  1080. dev_put(odev);
  1081. odev = NULL;
  1082. }
  1083. if (unlikely(!odev)) {
  1084. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  1085. printk(KERN_DEBUG "%s: Discarded packet with nonexistent "
  1086. "originating device\n", dev->name);
  1087. #endif
  1088. dev_kfree_skb(skb);
  1089. return 0;
  1090. }
  1091. osdata = IEEE80211_DEV_TO_SUB_IF(odev);
  1092. headroom = osdata->local->tx_headroom + IEEE80211_ENCRYPT_HEADROOM;
  1093. if (skb_headroom(skb) < headroom) {
  1094. if (pskb_expand_head(skb, headroom, 0, GFP_ATOMIC)) {
  1095. dev_kfree_skb(skb);
  1096. dev_put(odev);
  1097. return 0;
  1098. }
  1099. }
  1100. control.ifindex = odev->ifindex;
  1101. control.type = osdata->type;
  1102. if (pkt_data->flags & IEEE80211_TXPD_REQ_TX_STATUS)
  1103. control.flags |= IEEE80211_TXCTL_REQ_TX_STATUS;
  1104. if (pkt_data->flags & IEEE80211_TXPD_DO_NOT_ENCRYPT)
  1105. control.flags |= IEEE80211_TXCTL_DO_NOT_ENCRYPT;
  1106. if (pkt_data->flags & IEEE80211_TXPD_REQUEUE)
  1107. control.flags |= IEEE80211_TXCTL_REQUEUE;
  1108. control.queue = pkt_data->queue;
  1109. ret = ieee80211_tx(odev, skb, &control,
  1110. control.type == IEEE80211_IF_TYPE_MGMT);
  1111. dev_put(odev);
  1112. return ret;
  1113. }
  1114. int ieee80211_monitor_start_xmit(struct sk_buff *skb,
  1115. struct net_device *dev)
  1116. {
  1117. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  1118. struct ieee80211_tx_packet_data *pkt_data;
  1119. struct ieee80211_radiotap_header *prthdr =
  1120. (struct ieee80211_radiotap_header *)skb->data;
  1121. u16 len_rthdr;
  1122. /* check for not even having the fixed radiotap header part */
  1123. if (unlikely(skb->len < sizeof(struct ieee80211_radiotap_header)))
  1124. goto fail; /* too short to be possibly valid */
  1125. /* is it a header version we can trust to find length from? */
  1126. if (unlikely(prthdr->it_version))
  1127. goto fail; /* only version 0 is supported */
  1128. /* then there must be a radiotap header with a length we can use */
  1129. len_rthdr = ieee80211_get_radiotap_len(skb->data);
  1130. /* does the skb contain enough to deliver on the alleged length? */
  1131. if (unlikely(skb->len < len_rthdr))
  1132. goto fail; /* skb too short for claimed rt header extent */
  1133. skb->dev = local->mdev;
  1134. pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
  1135. memset(pkt_data, 0, sizeof(*pkt_data));
  1136. /* needed because we set skb device to master */
  1137. pkt_data->ifindex = dev->ifindex;
  1138. pkt_data->flags |= IEEE80211_TXPD_DO_NOT_ENCRYPT;
  1139. /*
  1140. * fix up the pointers accounting for the radiotap
  1141. * header still being in there. We are being given
  1142. * a precooked IEEE80211 header so no need for
  1143. * normal processing
  1144. */
  1145. skb_set_mac_header(skb, len_rthdr);
  1146. /*
  1147. * these are just fixed to the end of the rt area since we
  1148. * don't have any better information and at this point, nobody cares
  1149. */
  1150. skb_set_network_header(skb, len_rthdr);
  1151. skb_set_transport_header(skb, len_rthdr);
  1152. /* pass the radiotap header up to the next stage intact */
  1153. dev_queue_xmit(skb);
  1154. return NETDEV_TX_OK;
  1155. fail:
  1156. dev_kfree_skb(skb);
  1157. return NETDEV_TX_OK; /* meaning, we dealt with the skb */
  1158. }
  1159. /**
  1160. * ieee80211_subif_start_xmit - netif start_xmit function for Ethernet-type
  1161. * subinterfaces (wlan#, WDS, and VLAN interfaces)
  1162. * @skb: packet to be sent
  1163. * @dev: incoming interface
  1164. *
  1165. * Returns: 0 on success (and frees skb in this case) or 1 on failure (skb will
  1166. * not be freed, and caller is responsible for either retrying later or freeing
  1167. * skb).
  1168. *
  1169. * This function takes in an Ethernet header and encapsulates it with suitable
  1170. * IEEE 802.11 header based on which interface the packet is coming in. The
  1171. * encapsulated packet will then be passed to master interface, wlan#.11, for
  1172. * transmission (through low-level driver).
  1173. */
  1174. int ieee80211_subif_start_xmit(struct sk_buff *skb,
  1175. struct net_device *dev)
  1176. {
  1177. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  1178. struct ieee80211_tx_packet_data *pkt_data;
  1179. struct ieee80211_sub_if_data *sdata;
  1180. int ret = 1, head_need;
  1181. u16 ethertype, hdrlen, fc;
  1182. struct ieee80211_hdr hdr;
  1183. const u8 *encaps_data;
  1184. int encaps_len, skip_header_bytes;
  1185. int nh_pos, h_pos;
  1186. struct sta_info *sta;
  1187. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  1188. if (unlikely(skb->len < ETH_HLEN)) {
  1189. printk(KERN_DEBUG "%s: short skb (len=%d)\n",
  1190. dev->name, skb->len);
  1191. ret = 0;
  1192. goto fail;
  1193. }
  1194. nh_pos = skb_network_header(skb) - skb->data;
  1195. h_pos = skb_transport_header(skb) - skb->data;
  1196. /* convert Ethernet header to proper 802.11 header (based on
  1197. * operation mode) */
  1198. ethertype = (skb->data[12] << 8) | skb->data[13];
  1199. /* TODO: handling for 802.1x authorized/unauthorized port */
  1200. fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA;
  1201. switch (sdata->type) {
  1202. case IEEE80211_IF_TYPE_AP:
  1203. case IEEE80211_IF_TYPE_VLAN:
  1204. fc |= IEEE80211_FCTL_FROMDS;
  1205. /* DA BSSID SA */
  1206. memcpy(hdr.addr1, skb->data, ETH_ALEN);
  1207. memcpy(hdr.addr2, dev->dev_addr, ETH_ALEN);
  1208. memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
  1209. hdrlen = 24;
  1210. break;
  1211. case IEEE80211_IF_TYPE_WDS:
  1212. fc |= IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS;
  1213. /* RA TA DA SA */
  1214. memcpy(hdr.addr1, sdata->u.wds.remote_addr, ETH_ALEN);
  1215. memcpy(hdr.addr2, dev->dev_addr, ETH_ALEN);
  1216. memcpy(hdr.addr3, skb->data, ETH_ALEN);
  1217. memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
  1218. hdrlen = 30;
  1219. break;
  1220. case IEEE80211_IF_TYPE_STA:
  1221. fc |= IEEE80211_FCTL_TODS;
  1222. /* BSSID SA DA */
  1223. memcpy(hdr.addr1, sdata->u.sta.bssid, ETH_ALEN);
  1224. memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
  1225. memcpy(hdr.addr3, skb->data, ETH_ALEN);
  1226. hdrlen = 24;
  1227. break;
  1228. case IEEE80211_IF_TYPE_IBSS:
  1229. /* DA SA BSSID */
  1230. memcpy(hdr.addr1, skb->data, ETH_ALEN);
  1231. memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
  1232. memcpy(hdr.addr3, sdata->u.sta.bssid, ETH_ALEN);
  1233. hdrlen = 24;
  1234. break;
  1235. default:
  1236. ret = 0;
  1237. goto fail;
  1238. }
  1239. /* receiver is QoS enabled, use a QoS type frame */
  1240. sta = sta_info_get(local, hdr.addr1);
  1241. if (sta) {
  1242. if (sta->flags & WLAN_STA_WME) {
  1243. fc |= IEEE80211_STYPE_QOS_DATA;
  1244. hdrlen += 2;
  1245. }
  1246. sta_info_put(sta);
  1247. }
  1248. hdr.frame_control = cpu_to_le16(fc);
  1249. hdr.duration_id = 0;
  1250. hdr.seq_ctrl = 0;
  1251. skip_header_bytes = ETH_HLEN;
  1252. if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
  1253. encaps_data = bridge_tunnel_header;
  1254. encaps_len = sizeof(bridge_tunnel_header);
  1255. skip_header_bytes -= 2;
  1256. } else if (ethertype >= 0x600) {
  1257. encaps_data = rfc1042_header;
  1258. encaps_len = sizeof(rfc1042_header);
  1259. skip_header_bytes -= 2;
  1260. } else {
  1261. encaps_data = NULL;
  1262. encaps_len = 0;
  1263. }
  1264. skb_pull(skb, skip_header_bytes);
  1265. nh_pos -= skip_header_bytes;
  1266. h_pos -= skip_header_bytes;
  1267. /* TODO: implement support for fragments so that there is no need to
  1268. * reallocate and copy payload; it might be enough to support one
  1269. * extra fragment that would be copied in the beginning of the frame
  1270. * data.. anyway, it would be nice to include this into skb structure
  1271. * somehow
  1272. *
  1273. * There are few options for this:
  1274. * use skb->cb as an extra space for 802.11 header
  1275. * allocate new buffer if not enough headroom
  1276. * make sure that there is enough headroom in every skb by increasing
  1277. * build in headroom in __dev_alloc_skb() (linux/skbuff.h) and
  1278. * alloc_skb() (net/core/skbuff.c)
  1279. */
  1280. head_need = hdrlen + encaps_len + local->tx_headroom;
  1281. head_need -= skb_headroom(skb);
  1282. /* We are going to modify skb data, so make a copy of it if happens to
  1283. * be cloned. This could happen, e.g., with Linux bridge code passing
  1284. * us broadcast frames. */
  1285. if (head_need > 0 || skb_cloned(skb)) {
  1286. #if 0
  1287. printk(KERN_DEBUG "%s: need to reallocate buffer for %d bytes "
  1288. "of headroom\n", dev->name, head_need);
  1289. #endif
  1290. if (skb_cloned(skb))
  1291. I802_DEBUG_INC(local->tx_expand_skb_head_cloned);
  1292. else
  1293. I802_DEBUG_INC(local->tx_expand_skb_head);
  1294. /* Since we have to reallocate the buffer, make sure that there
  1295. * is enough room for possible WEP IV/ICV and TKIP (8 bytes
  1296. * before payload and 12 after). */
  1297. if (pskb_expand_head(skb, (head_need > 0 ? head_need + 8 : 8),
  1298. 12, GFP_ATOMIC)) {
  1299. printk(KERN_DEBUG "%s: failed to reallocate TX buffer"
  1300. "\n", dev->name);
  1301. goto fail;
  1302. }
  1303. }
  1304. if (encaps_data) {
  1305. memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
  1306. nh_pos += encaps_len;
  1307. h_pos += encaps_len;
  1308. }
  1309. if (fc & IEEE80211_STYPE_QOS_DATA) {
  1310. __le16 *qos_control;
  1311. qos_control = (__le16*) skb_push(skb, 2);
  1312. memcpy(skb_push(skb, hdrlen - 2), &hdr, hdrlen - 2);
  1313. /*
  1314. * Maybe we could actually set some fields here, for now just
  1315. * initialise to zero to indicate no special operation.
  1316. */
  1317. *qos_control = 0;
  1318. } else
  1319. memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
  1320. nh_pos += hdrlen;
  1321. h_pos += hdrlen;
  1322. pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
  1323. memset(pkt_data, 0, sizeof(struct ieee80211_tx_packet_data));
  1324. pkt_data->ifindex = dev->ifindex;
  1325. if (sdata->type == IEEE80211_IF_TYPE_MGMT)
  1326. pkt_data->flags |= IEEE80211_TXPD_MGMT_IFACE;
  1327. skb->dev = local->mdev;
  1328. sdata->stats.tx_packets++;
  1329. sdata->stats.tx_bytes += skb->len;
  1330. /* Update skb pointers to various headers since this modified frame
  1331. * is going to go through Linux networking code that may potentially
  1332. * need things like pointer to IP header. */
  1333. skb_set_mac_header(skb, 0);
  1334. skb_set_network_header(skb, nh_pos);
  1335. skb_set_transport_header(skb, h_pos);
  1336. dev->trans_start = jiffies;
  1337. dev_queue_xmit(skb);
  1338. return 0;
  1339. fail:
  1340. if (!ret)
  1341. dev_kfree_skb(skb);
  1342. return ret;
  1343. }
  1344. /*
  1345. * This is the transmit routine for the 802.11 type interfaces
  1346. * called by upper layers of the linux networking
  1347. * stack when it has a frame to transmit
  1348. */
  1349. int ieee80211_mgmt_start_xmit(struct sk_buff *skb, struct net_device *dev)
  1350. {
  1351. struct ieee80211_sub_if_data *sdata;
  1352. struct ieee80211_tx_packet_data *pkt_data;
  1353. struct ieee80211_hdr *hdr;
  1354. u16 fc;
  1355. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  1356. if (skb->len < 10) {
  1357. dev_kfree_skb(skb);
  1358. return 0;
  1359. }
  1360. if (skb_headroom(skb) < sdata->local->tx_headroom) {
  1361. if (pskb_expand_head(skb, sdata->local->tx_headroom,
  1362. 0, GFP_ATOMIC)) {
  1363. dev_kfree_skb(skb);
  1364. return 0;
  1365. }
  1366. }
  1367. hdr = (struct ieee80211_hdr *) skb->data;
  1368. fc = le16_to_cpu(hdr->frame_control);
  1369. pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
  1370. memset(pkt_data, 0, sizeof(struct ieee80211_tx_packet_data));
  1371. pkt_data->ifindex = sdata->dev->ifindex;
  1372. if (sdata->type == IEEE80211_IF_TYPE_MGMT)
  1373. pkt_data->flags |= IEEE80211_TXPD_MGMT_IFACE;
  1374. skb->priority = 20; /* use hardcoded priority for mgmt TX queue */
  1375. skb->dev = sdata->local->mdev;
  1376. /*
  1377. * We're using the protocol field of the the frame control header
  1378. * to request TX callback for hostapd. BIT(1) is checked.
  1379. */
  1380. if ((fc & BIT(1)) == BIT(1)) {
  1381. pkt_data->flags |= IEEE80211_TXPD_REQ_TX_STATUS;
  1382. fc &= ~BIT(1);
  1383. hdr->frame_control = cpu_to_le16(fc);
  1384. }
  1385. if (!(fc & IEEE80211_FCTL_PROTECTED))
  1386. pkt_data->flags |= IEEE80211_TXPD_DO_NOT_ENCRYPT;
  1387. sdata->stats.tx_packets++;
  1388. sdata->stats.tx_bytes += skb->len;
  1389. dev_queue_xmit(skb);
  1390. return 0;
  1391. }
  1392. /* helper functions for pending packets for when queues are stopped */
  1393. void ieee80211_clear_tx_pending(struct ieee80211_local *local)
  1394. {
  1395. int i, j;
  1396. struct ieee80211_tx_stored_packet *store;
  1397. for (i = 0; i < local->hw.queues; i++) {
  1398. if (!__ieee80211_queue_pending(local, i))
  1399. continue;
  1400. store = &local->pending_packet[i];
  1401. kfree_skb(store->skb);
  1402. for (j = 0; j < store->num_extra_frag; j++)
  1403. kfree_skb(store->extra_frag[j]);
  1404. kfree(store->extra_frag);
  1405. clear_bit(IEEE80211_LINK_STATE_PENDING, &local->state[i]);
  1406. }
  1407. }
  1408. void ieee80211_tx_pending(unsigned long data)
  1409. {
  1410. struct ieee80211_local *local = (struct ieee80211_local *)data;
  1411. struct net_device *dev = local->mdev;
  1412. struct ieee80211_tx_stored_packet *store;
  1413. struct ieee80211_txrx_data tx;
  1414. int i, ret, reschedule = 0;
  1415. netif_tx_lock_bh(dev);
  1416. for (i = 0; i < local->hw.queues; i++) {
  1417. if (__ieee80211_queue_stopped(local, i))
  1418. continue;
  1419. if (!__ieee80211_queue_pending(local, i)) {
  1420. reschedule = 1;
  1421. continue;
  1422. }
  1423. store = &local->pending_packet[i];
  1424. tx.u.tx.control = &store->control;
  1425. tx.u.tx.extra_frag = store->extra_frag;
  1426. tx.u.tx.num_extra_frag = store->num_extra_frag;
  1427. tx.u.tx.last_frag_hwrate = store->last_frag_hwrate;
  1428. tx.u.tx.last_frag_rate = store->last_frag_rate;
  1429. tx.flags = 0;
  1430. if (store->last_frag_rate_ctrl_probe)
  1431. tx.flags |= IEEE80211_TXRXD_TXPROBE_LAST_FRAG;
  1432. ret = __ieee80211_tx(local, store->skb, &tx);
  1433. if (ret) {
  1434. if (ret == IEEE80211_TX_FRAG_AGAIN)
  1435. store->skb = NULL;
  1436. } else {
  1437. clear_bit(IEEE80211_LINK_STATE_PENDING,
  1438. &local->state[i]);
  1439. reschedule = 1;
  1440. }
  1441. }
  1442. netif_tx_unlock_bh(dev);
  1443. if (reschedule) {
  1444. if (!ieee80211_qdisc_installed(dev)) {
  1445. if (!__ieee80211_queue_stopped(local, 0))
  1446. netif_wake_queue(dev);
  1447. } else
  1448. netif_schedule(dev);
  1449. }
  1450. }
  1451. /* functions for drivers to get certain frames */
  1452. static void ieee80211_beacon_add_tim(struct ieee80211_local *local,
  1453. struct ieee80211_if_ap *bss,
  1454. struct sk_buff *skb)
  1455. {
  1456. u8 *pos, *tim;
  1457. int aid0 = 0;
  1458. int i, have_bits = 0, n1, n2;
  1459. /* Generate bitmap for TIM only if there are any STAs in power save
  1460. * mode. */
  1461. read_lock_bh(&local->sta_lock);
  1462. if (atomic_read(&bss->num_sta_ps) > 0)
  1463. /* in the hope that this is faster than
  1464. * checking byte-for-byte */
  1465. have_bits = !bitmap_empty((unsigned long*)bss->tim,
  1466. IEEE80211_MAX_AID+1);
  1467. if (bss->dtim_count == 0)
  1468. bss->dtim_count = bss->dtim_period - 1;
  1469. else
  1470. bss->dtim_count--;
  1471. tim = pos = (u8 *) skb_put(skb, 6);
  1472. *pos++ = WLAN_EID_TIM;
  1473. *pos++ = 4;
  1474. *pos++ = bss->dtim_count;
  1475. *pos++ = bss->dtim_period;
  1476. if (bss->dtim_count == 0 && !skb_queue_empty(&bss->ps_bc_buf))
  1477. aid0 = 1;
  1478. if (have_bits) {
  1479. /* Find largest even number N1 so that bits numbered 1 through
  1480. * (N1 x 8) - 1 in the bitmap are 0 and number N2 so that bits
  1481. * (N2 + 1) x 8 through 2007 are 0. */
  1482. n1 = 0;
  1483. for (i = 0; i < IEEE80211_MAX_TIM_LEN; i++) {
  1484. if (bss->tim[i]) {
  1485. n1 = i & 0xfe;
  1486. break;
  1487. }
  1488. }
  1489. n2 = n1;
  1490. for (i = IEEE80211_MAX_TIM_LEN - 1; i >= n1; i--) {
  1491. if (bss->tim[i]) {
  1492. n2 = i;
  1493. break;
  1494. }
  1495. }
  1496. /* Bitmap control */
  1497. *pos++ = n1 | aid0;
  1498. /* Part Virt Bitmap */
  1499. memcpy(pos, bss->tim + n1, n2 - n1 + 1);
  1500. tim[1] = n2 - n1 + 4;
  1501. skb_put(skb, n2 - n1);
  1502. } else {
  1503. *pos++ = aid0; /* Bitmap control */
  1504. *pos++ = 0; /* Part Virt Bitmap */
  1505. }
  1506. read_unlock_bh(&local->sta_lock);
  1507. }
  1508. struct sk_buff *ieee80211_beacon_get(struct ieee80211_hw *hw, int if_id,
  1509. struct ieee80211_tx_control *control)
  1510. {
  1511. struct ieee80211_local *local = hw_to_local(hw);
  1512. struct sk_buff *skb;
  1513. struct net_device *bdev;
  1514. struct ieee80211_sub_if_data *sdata = NULL;
  1515. struct ieee80211_if_ap *ap = NULL;
  1516. struct ieee80211_rate *rate;
  1517. struct rate_control_extra extra;
  1518. u8 *b_head, *b_tail;
  1519. int bh_len, bt_len;
  1520. bdev = dev_get_by_index(&init_net, if_id);
  1521. if (bdev) {
  1522. sdata = IEEE80211_DEV_TO_SUB_IF(bdev);
  1523. ap = &sdata->u.ap;
  1524. dev_put(bdev);
  1525. }
  1526. if (!ap || sdata->type != IEEE80211_IF_TYPE_AP ||
  1527. !ap->beacon_head) {
  1528. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  1529. if (net_ratelimit())
  1530. printk(KERN_DEBUG "no beacon data avail for idx=%d "
  1531. "(%s)\n", if_id, bdev ? bdev->name : "N/A");
  1532. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  1533. return NULL;
  1534. }
  1535. /* Assume we are generating the normal beacon locally */
  1536. b_head = ap->beacon_head;
  1537. b_tail = ap->beacon_tail;
  1538. bh_len = ap->beacon_head_len;
  1539. bt_len = ap->beacon_tail_len;
  1540. skb = dev_alloc_skb(local->tx_headroom +
  1541. bh_len + bt_len + 256 /* maximum TIM len */);
  1542. if (!skb)
  1543. return NULL;
  1544. skb_reserve(skb, local->tx_headroom);
  1545. memcpy(skb_put(skb, bh_len), b_head, bh_len);
  1546. ieee80211_include_sequence(sdata, (struct ieee80211_hdr *)skb->data);
  1547. ieee80211_beacon_add_tim(local, ap, skb);
  1548. if (b_tail) {
  1549. memcpy(skb_put(skb, bt_len), b_tail, bt_len);
  1550. }
  1551. if (control) {
  1552. memset(&extra, 0, sizeof(extra));
  1553. extra.mode = local->oper_hw_mode;
  1554. rate = rate_control_get_rate(local, local->mdev, skb, &extra);
  1555. if (!rate) {
  1556. if (net_ratelimit()) {
  1557. printk(KERN_DEBUG "%s: ieee80211_beacon_get: no rate "
  1558. "found\n", local->mdev->name);
  1559. }
  1560. dev_kfree_skb(skb);
  1561. return NULL;
  1562. }
  1563. control->tx_rate =
  1564. ((sdata->flags & IEEE80211_SDATA_SHORT_PREAMBLE) &&
  1565. (rate->flags & IEEE80211_RATE_PREAMBLE2)) ?
  1566. rate->val2 : rate->val;
  1567. control->antenna_sel_tx = local->hw.conf.antenna_sel_tx;
  1568. control->power_level = local->hw.conf.power_level;
  1569. control->flags |= IEEE80211_TXCTL_NO_ACK;
  1570. control->retry_limit = 1;
  1571. control->flags |= IEEE80211_TXCTL_CLEAR_DST_MASK;
  1572. }
  1573. ap->num_beacons++;
  1574. return skb;
  1575. }
  1576. EXPORT_SYMBOL(ieee80211_beacon_get);
  1577. void ieee80211_rts_get(struct ieee80211_hw *hw, int if_id,
  1578. const void *frame, size_t frame_len,
  1579. const struct ieee80211_tx_control *frame_txctl,
  1580. struct ieee80211_rts *rts)
  1581. {
  1582. const struct ieee80211_hdr *hdr = frame;
  1583. u16 fctl;
  1584. fctl = IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS;
  1585. rts->frame_control = cpu_to_le16(fctl);
  1586. rts->duration = ieee80211_rts_duration(hw, if_id, frame_len, frame_txctl);
  1587. memcpy(rts->ra, hdr->addr1, sizeof(rts->ra));
  1588. memcpy(rts->ta, hdr->addr2, sizeof(rts->ta));
  1589. }
  1590. EXPORT_SYMBOL(ieee80211_rts_get);
  1591. void ieee80211_ctstoself_get(struct ieee80211_hw *hw, int if_id,
  1592. const void *frame, size_t frame_len,
  1593. const struct ieee80211_tx_control *frame_txctl,
  1594. struct ieee80211_cts *cts)
  1595. {
  1596. const struct ieee80211_hdr *hdr = frame;
  1597. u16 fctl;
  1598. fctl = IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS;
  1599. cts->frame_control = cpu_to_le16(fctl);
  1600. cts->duration = ieee80211_ctstoself_duration(hw, if_id, frame_len, frame_txctl);
  1601. memcpy(cts->ra, hdr->addr1, sizeof(cts->ra));
  1602. }
  1603. EXPORT_SYMBOL(ieee80211_ctstoself_get);
  1604. struct sk_buff *
  1605. ieee80211_get_buffered_bc(struct ieee80211_hw *hw, int if_id,
  1606. struct ieee80211_tx_control *control)
  1607. {
  1608. struct ieee80211_local *local = hw_to_local(hw);
  1609. struct sk_buff *skb;
  1610. struct sta_info *sta;
  1611. ieee80211_tx_handler *handler;
  1612. struct ieee80211_txrx_data tx;
  1613. ieee80211_txrx_result res = TXRX_DROP;
  1614. struct net_device *bdev;
  1615. struct ieee80211_sub_if_data *sdata;
  1616. struct ieee80211_if_ap *bss = NULL;
  1617. bdev = dev_get_by_index(&init_net, if_id);
  1618. if (bdev) {
  1619. sdata = IEEE80211_DEV_TO_SUB_IF(bdev);
  1620. bss = &sdata->u.ap;
  1621. dev_put(bdev);
  1622. }
  1623. if (!bss || sdata->type != IEEE80211_IF_TYPE_AP || !bss->beacon_head)
  1624. return NULL;
  1625. if (bss->dtim_count != 0)
  1626. return NULL; /* send buffered bc/mc only after DTIM beacon */
  1627. memset(control, 0, sizeof(*control));
  1628. while (1) {
  1629. skb = skb_dequeue(&bss->ps_bc_buf);
  1630. if (!skb)
  1631. return NULL;
  1632. local->total_ps_buffered--;
  1633. if (!skb_queue_empty(&bss->ps_bc_buf) && skb->len >= 2) {
  1634. struct ieee80211_hdr *hdr =
  1635. (struct ieee80211_hdr *) skb->data;
  1636. /* more buffered multicast/broadcast frames ==> set
  1637. * MoreData flag in IEEE 802.11 header to inform PS
  1638. * STAs */
  1639. hdr->frame_control |=
  1640. cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  1641. }
  1642. if (ieee80211_tx_prepare(&tx, skb, local->mdev, control) == 0)
  1643. break;
  1644. dev_kfree_skb_any(skb);
  1645. }
  1646. sta = tx.sta;
  1647. tx.flags |= IEEE80211_TXRXD_TXPS_BUFFERED;
  1648. for (handler = local->tx_handlers; *handler != NULL; handler++) {
  1649. res = (*handler)(&tx);
  1650. if (res == TXRX_DROP || res == TXRX_QUEUED)
  1651. break;
  1652. }
  1653. dev_put(tx.dev);
  1654. skb = tx.skb; /* handlers are allowed to change skb */
  1655. if (res == TXRX_DROP) {
  1656. I802_DEBUG_INC(local->tx_handlers_drop);
  1657. dev_kfree_skb(skb);
  1658. skb = NULL;
  1659. } else if (res == TXRX_QUEUED) {
  1660. I802_DEBUG_INC(local->tx_handlers_queued);
  1661. skb = NULL;
  1662. }
  1663. if (sta)
  1664. sta_info_put(sta);
  1665. return skb;
  1666. }
  1667. EXPORT_SYMBOL(ieee80211_get_buffered_bc);