tx.c 53 KB

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