tx.c 54 KB

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