tx.c 56 KB

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