tx.c 55 KB

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