tx.c 54 KB

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