tx.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024
  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 "led.h"
  27. #include "mesh.h"
  28. #include "wep.h"
  29. #include "wpa.h"
  30. #include "wme.h"
  31. #include "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 ? get_sta_flags(tx->sta) : 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. list_for_each_entry_rcu(sta, &local->sta_list, list) {
  289. skb = skb_dequeue(&sta->ps_tx_buf);
  290. if (skb) {
  291. purged++;
  292. dev_kfree_skb(skb);
  293. }
  294. total += skb_queue_len(&sta->ps_tx_buf);
  295. }
  296. rcu_read_unlock();
  297. local->total_ps_buffered = total;
  298. printk(KERN_DEBUG "%s: PS buffers full - purged %d frames\n",
  299. wiphy_name(local->hw.wiphy), purged);
  300. }
  301. static ieee80211_tx_result
  302. ieee80211_tx_h_multicast_ps_buf(struct ieee80211_tx_data *tx)
  303. {
  304. /*
  305. * broadcast/multicast frame
  306. *
  307. * If any of the associated stations is in power save mode,
  308. * the frame is buffered to be sent after DTIM beacon frame.
  309. * This is done either by the hardware or us.
  310. */
  311. /* not AP/IBSS or ordered frame */
  312. if (!tx->sdata->bss || (tx->fc & IEEE80211_FCTL_ORDER))
  313. return TX_CONTINUE;
  314. /* no stations in PS mode */
  315. if (!atomic_read(&tx->sdata->bss->num_sta_ps))
  316. return TX_CONTINUE;
  317. /* buffered in mac80211 */
  318. if (tx->local->hw.flags & IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING) {
  319. if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
  320. purge_old_ps_buffers(tx->local);
  321. if (skb_queue_len(&tx->sdata->bss->ps_bc_buf) >=
  322. AP_MAX_BC_BUFFER) {
  323. if (net_ratelimit()) {
  324. printk(KERN_DEBUG "%s: BC TX buffer full - "
  325. "dropping the oldest frame\n",
  326. tx->dev->name);
  327. }
  328. dev_kfree_skb(skb_dequeue(&tx->sdata->bss->ps_bc_buf));
  329. } else
  330. tx->local->total_ps_buffered++;
  331. skb_queue_tail(&tx->sdata->bss->ps_bc_buf, tx->skb);
  332. return TX_QUEUED;
  333. }
  334. /* buffered in hardware */
  335. tx->control->flags |= IEEE80211_TXCTL_SEND_AFTER_DTIM;
  336. return TX_CONTINUE;
  337. }
  338. static ieee80211_tx_result
  339. ieee80211_tx_h_unicast_ps_buf(struct ieee80211_tx_data *tx)
  340. {
  341. struct sta_info *sta = tx->sta;
  342. u32 staflags;
  343. DECLARE_MAC_BUF(mac);
  344. if (unlikely(!sta ||
  345. ((tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT &&
  346. (tx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_RESP)))
  347. return TX_CONTINUE;
  348. staflags = get_sta_flags(sta);
  349. if (unlikely((staflags & WLAN_STA_PS) &&
  350. !(staflags & 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(test_sta_flags(sta, 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. clear_sta_flags(sta, 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 || test_sta_flags(tx->sta, 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. control->aid = tx->sta->aid;
  646. tx->sta->tx_packets++;
  647. tx->sta->tx_fragments++;
  648. tx->sta->tx_bytes += tx->skb->len;
  649. if (tx->extra_frag) {
  650. int i;
  651. tx->sta->tx_fragments += tx->num_extra_frag;
  652. for (i = 0; i < tx->num_extra_frag; i++) {
  653. tx->sta->tx_bytes +=
  654. tx->extra_frag[i]->len;
  655. }
  656. }
  657. }
  658. return TX_CONTINUE;
  659. }
  660. static ieee80211_tx_result
  661. ieee80211_tx_h_load_stats(struct ieee80211_tx_data *tx)
  662. {
  663. struct ieee80211_local *local = tx->local;
  664. struct sk_buff *skb = tx->skb;
  665. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  666. u32 load = 0, hdrtime;
  667. struct ieee80211_rate *rate = tx->rate;
  668. /* TODO: this could be part of tx_status handling, so that the number
  669. * of retries would be known; TX rate should in that case be stored
  670. * somewhere with the packet */
  671. /* Estimate total channel use caused by this frame */
  672. /* 1 bit at 1 Mbit/s takes 1 usec; in channel_use values,
  673. * 1 usec = 1/8 * (1080 / 10) = 13.5 */
  674. if (tx->channel->band == IEEE80211_BAND_5GHZ ||
  675. (tx->channel->band == IEEE80211_BAND_2GHZ &&
  676. rate->flags & IEEE80211_RATE_ERP_G))
  677. hdrtime = CHAN_UTIL_HDR_SHORT;
  678. else
  679. hdrtime = CHAN_UTIL_HDR_LONG;
  680. load = hdrtime;
  681. if (!is_multicast_ether_addr(hdr->addr1))
  682. load += hdrtime;
  683. if (tx->control->flags & IEEE80211_TXCTL_USE_RTS_CTS)
  684. load += 2 * hdrtime;
  685. else if (tx->control->flags & IEEE80211_TXCTL_USE_CTS_PROTECT)
  686. load += hdrtime;
  687. /* TODO: optimise again */
  688. load += skb->len * CHAN_UTIL_RATE_LCM / rate->bitrate;
  689. if (tx->extra_frag) {
  690. int i;
  691. for (i = 0; i < tx->num_extra_frag; i++) {
  692. load += 2 * hdrtime;
  693. load += tx->extra_frag[i]->len *
  694. tx->rate->bitrate;
  695. }
  696. }
  697. /* Divide channel_use by 8 to avoid wrapping around the counter */
  698. load >>= CHAN_UTIL_SHIFT;
  699. local->channel_use_raw += load;
  700. if (tx->sta)
  701. tx->sta->channel_use_raw += load;
  702. tx->sdata->channel_use_raw += load;
  703. return TX_CONTINUE;
  704. }
  705. typedef ieee80211_tx_result (*ieee80211_tx_handler)(struct ieee80211_tx_data *);
  706. static ieee80211_tx_handler ieee80211_tx_handlers[] =
  707. {
  708. ieee80211_tx_h_check_assoc,
  709. ieee80211_tx_h_sequence,
  710. ieee80211_tx_h_ps_buf,
  711. ieee80211_tx_h_select_key,
  712. ieee80211_tx_h_michael_mic_add,
  713. ieee80211_tx_h_fragment,
  714. ieee80211_tx_h_encrypt,
  715. ieee80211_tx_h_rate_ctrl,
  716. ieee80211_tx_h_misc,
  717. ieee80211_tx_h_load_stats,
  718. NULL
  719. };
  720. /* actual transmit path */
  721. /*
  722. * deal with packet injection down monitor interface
  723. * with Radiotap Header -- only called for monitor mode interface
  724. */
  725. static ieee80211_tx_result
  726. __ieee80211_parse_tx_radiotap(struct ieee80211_tx_data *tx,
  727. struct sk_buff *skb)
  728. {
  729. /*
  730. * this is the moment to interpret and discard the radiotap header that
  731. * must be at the start of the packet injected in Monitor mode
  732. *
  733. * Need to take some care with endian-ness since radiotap
  734. * args are little-endian
  735. */
  736. struct ieee80211_radiotap_iterator iterator;
  737. struct ieee80211_radiotap_header *rthdr =
  738. (struct ieee80211_radiotap_header *) skb->data;
  739. struct ieee80211_supported_band *sband;
  740. int ret = ieee80211_radiotap_iterator_init(&iterator, rthdr, skb->len);
  741. struct ieee80211_tx_control *control = tx->control;
  742. sband = tx->local->hw.wiphy->bands[tx->local->hw.conf.channel->band];
  743. control->flags |= IEEE80211_TXCTL_DO_NOT_ENCRYPT;
  744. tx->flags |= IEEE80211_TX_INJECTED;
  745. tx->flags &= ~IEEE80211_TX_FRAGMENTED;
  746. /*
  747. * for every radiotap entry that is present
  748. * (ieee80211_radiotap_iterator_next returns -ENOENT when no more
  749. * entries present, or -EINVAL on error)
  750. */
  751. while (!ret) {
  752. int i, target_rate;
  753. ret = ieee80211_radiotap_iterator_next(&iterator);
  754. if (ret)
  755. continue;
  756. /* see if this argument is something we can use */
  757. switch (iterator.this_arg_index) {
  758. /*
  759. * You must take care when dereferencing iterator.this_arg
  760. * for multibyte types... the pointer is not aligned. Use
  761. * get_unaligned((type *)iterator.this_arg) to dereference
  762. * iterator.this_arg for type "type" safely on all arches.
  763. */
  764. case IEEE80211_RADIOTAP_RATE:
  765. /*
  766. * radiotap rate u8 is in 500kbps units eg, 0x02=1Mbps
  767. * ieee80211 rate int is in 100kbps units eg, 0x0a=1Mbps
  768. */
  769. target_rate = (*iterator.this_arg) * 5;
  770. for (i = 0; i < sband->n_bitrates; i++) {
  771. struct ieee80211_rate *r;
  772. r = &sband->bitrates[i];
  773. if (r->bitrate == target_rate) {
  774. tx->rate = r;
  775. break;
  776. }
  777. }
  778. break;
  779. case IEEE80211_RADIOTAP_ANTENNA:
  780. /*
  781. * radiotap uses 0 for 1st ant, mac80211 is 1 for
  782. * 1st ant
  783. */
  784. control->antenna_sel_tx = (*iterator.this_arg) + 1;
  785. break;
  786. #if 0
  787. case IEEE80211_RADIOTAP_DBM_TX_POWER:
  788. control->power_level = *iterator.this_arg;
  789. break;
  790. #endif
  791. case IEEE80211_RADIOTAP_FLAGS:
  792. if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FCS) {
  793. /*
  794. * this indicates that the skb we have been
  795. * handed has the 32-bit FCS CRC at the end...
  796. * we should react to that by snipping it off
  797. * because it will be recomputed and added
  798. * on transmission
  799. */
  800. if (skb->len < (iterator.max_length + FCS_LEN))
  801. return TX_DROP;
  802. skb_trim(skb, skb->len - FCS_LEN);
  803. }
  804. if (*iterator.this_arg & IEEE80211_RADIOTAP_F_WEP)
  805. control->flags &=
  806. ~IEEE80211_TXCTL_DO_NOT_ENCRYPT;
  807. if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FRAG)
  808. tx->flags |= IEEE80211_TX_FRAGMENTED;
  809. break;
  810. /*
  811. * Please update the file
  812. * Documentation/networking/mac80211-injection.txt
  813. * when parsing new fields here.
  814. */
  815. default:
  816. break;
  817. }
  818. }
  819. if (ret != -ENOENT) /* ie, if we didn't simply run out of fields */
  820. return TX_DROP;
  821. /*
  822. * remove the radiotap header
  823. * iterator->max_length was sanity-checked against
  824. * skb->len by iterator init
  825. */
  826. skb_pull(skb, iterator.max_length);
  827. return TX_CONTINUE;
  828. }
  829. /*
  830. * initialises @tx
  831. */
  832. static ieee80211_tx_result
  833. __ieee80211_tx_prepare(struct ieee80211_tx_data *tx,
  834. struct sk_buff *skb,
  835. struct net_device *dev,
  836. struct ieee80211_tx_control *control)
  837. {
  838. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  839. struct ieee80211_hdr *hdr;
  840. struct ieee80211_sub_if_data *sdata;
  841. int hdrlen;
  842. memset(tx, 0, sizeof(*tx));
  843. tx->skb = skb;
  844. tx->dev = dev; /* use original interface */
  845. tx->local = local;
  846. tx->sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  847. tx->control = control;
  848. /*
  849. * Set this flag (used below to indicate "automatic fragmentation"),
  850. * it will be cleared/left by radiotap as desired.
  851. */
  852. tx->flags |= IEEE80211_TX_FRAGMENTED;
  853. /* process and remove the injection radiotap header */
  854. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  855. if (unlikely(sdata->vif.type == IEEE80211_IF_TYPE_MNTR)) {
  856. if (__ieee80211_parse_tx_radiotap(tx, skb) == TX_DROP)
  857. return TX_DROP;
  858. /*
  859. * __ieee80211_parse_tx_radiotap has now removed
  860. * the radiotap header that was present and pre-filled
  861. * 'tx' with tx control information.
  862. */
  863. }
  864. hdr = (struct ieee80211_hdr *) skb->data;
  865. tx->sta = sta_info_get(local, hdr->addr1);
  866. tx->fc = le16_to_cpu(hdr->frame_control);
  867. if (is_multicast_ether_addr(hdr->addr1)) {
  868. tx->flags &= ~IEEE80211_TX_UNICAST;
  869. control->flags |= IEEE80211_TXCTL_NO_ACK;
  870. } else {
  871. tx->flags |= IEEE80211_TX_UNICAST;
  872. control->flags &= ~IEEE80211_TXCTL_NO_ACK;
  873. }
  874. if (tx->flags & IEEE80211_TX_FRAGMENTED) {
  875. if ((tx->flags & IEEE80211_TX_UNICAST) &&
  876. skb->len + FCS_LEN > local->fragmentation_threshold &&
  877. !local->ops->set_frag_threshold)
  878. tx->flags |= IEEE80211_TX_FRAGMENTED;
  879. else
  880. tx->flags &= ~IEEE80211_TX_FRAGMENTED;
  881. }
  882. if (!tx->sta)
  883. control->flags |= IEEE80211_TXCTL_CLEAR_PS_FILT;
  884. else if (test_and_clear_sta_flags(tx->sta, WLAN_STA_CLEAR_PS_FILT))
  885. control->flags |= IEEE80211_TXCTL_CLEAR_PS_FILT;
  886. hdrlen = ieee80211_get_hdrlen(tx->fc);
  887. if (skb->len > hdrlen + sizeof(rfc1042_header) + 2) {
  888. u8 *pos = &skb->data[hdrlen + sizeof(rfc1042_header)];
  889. tx->ethertype = (pos[0] << 8) | pos[1];
  890. }
  891. control->flags |= IEEE80211_TXCTL_FIRST_FRAGMENT;
  892. return TX_CONTINUE;
  893. }
  894. /*
  895. * NB: @tx is uninitialised when passed in here
  896. */
  897. static int ieee80211_tx_prepare(struct ieee80211_tx_data *tx,
  898. struct sk_buff *skb,
  899. struct net_device *mdev,
  900. struct ieee80211_tx_control *control)
  901. {
  902. struct ieee80211_tx_packet_data *pkt_data;
  903. struct net_device *dev;
  904. pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
  905. dev = dev_get_by_index(&init_net, pkt_data->ifindex);
  906. if (unlikely(dev && !is_ieee80211_device(dev, mdev))) {
  907. dev_put(dev);
  908. dev = NULL;
  909. }
  910. if (unlikely(!dev))
  911. return -ENODEV;
  912. /* initialises tx with control */
  913. __ieee80211_tx_prepare(tx, skb, dev, control);
  914. dev_put(dev);
  915. return 0;
  916. }
  917. static int __ieee80211_tx(struct ieee80211_local *local, struct sk_buff *skb,
  918. struct ieee80211_tx_data *tx)
  919. {
  920. struct ieee80211_tx_control *control = tx->control;
  921. int ret, i;
  922. if (!ieee80211_qdisc_installed(local->mdev) &&
  923. __ieee80211_queue_stopped(local, 0)) {
  924. netif_stop_queue(local->mdev);
  925. return IEEE80211_TX_AGAIN;
  926. }
  927. if (skb) {
  928. ieee80211_dump_frame(wiphy_name(local->hw.wiphy),
  929. "TX to low-level driver", skb);
  930. ret = local->ops->tx(local_to_hw(local), skb, control);
  931. if (ret)
  932. return IEEE80211_TX_AGAIN;
  933. local->mdev->trans_start = jiffies;
  934. ieee80211_led_tx(local, 1);
  935. }
  936. if (tx->extra_frag) {
  937. control->flags &= ~(IEEE80211_TXCTL_USE_RTS_CTS |
  938. IEEE80211_TXCTL_USE_CTS_PROTECT |
  939. IEEE80211_TXCTL_CLEAR_PS_FILT |
  940. IEEE80211_TXCTL_FIRST_FRAGMENT);
  941. for (i = 0; i < tx->num_extra_frag; i++) {
  942. if (!tx->extra_frag[i])
  943. continue;
  944. if (__ieee80211_queue_stopped(local, control->queue))
  945. return IEEE80211_TX_FRAG_AGAIN;
  946. if (i == tx->num_extra_frag) {
  947. control->tx_rate = tx->last_frag_rate;
  948. if (tx->flags & IEEE80211_TX_PROBE_LAST_FRAG)
  949. control->flags |=
  950. IEEE80211_TXCTL_RATE_CTRL_PROBE;
  951. else
  952. control->flags &=
  953. ~IEEE80211_TXCTL_RATE_CTRL_PROBE;
  954. }
  955. ieee80211_dump_frame(wiphy_name(local->hw.wiphy),
  956. "TX to low-level driver",
  957. tx->extra_frag[i]);
  958. ret = local->ops->tx(local_to_hw(local),
  959. tx->extra_frag[i],
  960. control);
  961. if (ret)
  962. return IEEE80211_TX_FRAG_AGAIN;
  963. local->mdev->trans_start = jiffies;
  964. ieee80211_led_tx(local, 1);
  965. tx->extra_frag[i] = NULL;
  966. }
  967. kfree(tx->extra_frag);
  968. tx->extra_frag = NULL;
  969. }
  970. return IEEE80211_TX_OK;
  971. }
  972. static int ieee80211_tx(struct net_device *dev, struct sk_buff *skb,
  973. struct ieee80211_tx_control *control)
  974. {
  975. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  976. struct sta_info *sta;
  977. ieee80211_tx_handler *handler;
  978. struct ieee80211_tx_data tx;
  979. ieee80211_tx_result res = TX_DROP, res_prepare;
  980. int ret, i;
  981. WARN_ON(__ieee80211_queue_pending(local, control->queue));
  982. if (unlikely(skb->len < 10)) {
  983. dev_kfree_skb(skb);
  984. return 0;
  985. }
  986. rcu_read_lock();
  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. rcu_read_unlock();
  992. return 0;
  993. }
  994. sta = tx.sta;
  995. tx.channel = local->hw.conf.channel;
  996. for (handler = ieee80211_tx_handlers; *handler != NULL;
  997. handler++) {
  998. res = (*handler)(&tx);
  999. if (res != TX_CONTINUE)
  1000. break;
  1001. }
  1002. skb = tx.skb; /* handlers are allowed to change skb */
  1003. if (unlikely(res == TX_DROP)) {
  1004. I802_DEBUG_INC(local->tx_handlers_drop);
  1005. goto drop;
  1006. }
  1007. if (unlikely(res == TX_QUEUED)) {
  1008. I802_DEBUG_INC(local->tx_handlers_queued);
  1009. rcu_read_unlock();
  1010. return 0;
  1011. }
  1012. if (tx.extra_frag) {
  1013. for (i = 0; i < tx.num_extra_frag; i++) {
  1014. int next_len, dur;
  1015. struct ieee80211_hdr *hdr =
  1016. (struct ieee80211_hdr *)
  1017. tx.extra_frag[i]->data;
  1018. if (i + 1 < tx.num_extra_frag) {
  1019. next_len = tx.extra_frag[i + 1]->len;
  1020. } else {
  1021. next_len = 0;
  1022. tx.rate = tx.last_frag_rate;
  1023. }
  1024. dur = ieee80211_duration(&tx, 0, next_len);
  1025. hdr->duration_id = cpu_to_le16(dur);
  1026. }
  1027. }
  1028. retry:
  1029. ret = __ieee80211_tx(local, skb, &tx);
  1030. if (ret) {
  1031. struct ieee80211_tx_stored_packet *store =
  1032. &local->pending_packet[control->queue];
  1033. if (ret == IEEE80211_TX_FRAG_AGAIN)
  1034. skb = NULL;
  1035. set_bit(IEEE80211_LINK_STATE_PENDING,
  1036. &local->state[control->queue]);
  1037. smp_mb();
  1038. /* When the driver gets out of buffers during sending of
  1039. * fragments and calls ieee80211_stop_queue, there is
  1040. * a small window between IEEE80211_LINK_STATE_XOFF and
  1041. * IEEE80211_LINK_STATE_PENDING flags are set. If a buffer
  1042. * gets available in that window (i.e. driver calls
  1043. * ieee80211_wake_queue), we would end up with ieee80211_tx
  1044. * called with IEEE80211_LINK_STATE_PENDING. Prevent this by
  1045. * continuing transmitting here when that situation is
  1046. * possible to have happened. */
  1047. if (!__ieee80211_queue_stopped(local, control->queue)) {
  1048. clear_bit(IEEE80211_LINK_STATE_PENDING,
  1049. &local->state[control->queue]);
  1050. goto retry;
  1051. }
  1052. memcpy(&store->control, control,
  1053. sizeof(struct ieee80211_tx_control));
  1054. store->skb = skb;
  1055. store->extra_frag = tx.extra_frag;
  1056. store->num_extra_frag = tx.num_extra_frag;
  1057. store->last_frag_rate = tx.last_frag_rate;
  1058. store->last_frag_rate_ctrl_probe =
  1059. !!(tx.flags & IEEE80211_TX_PROBE_LAST_FRAG);
  1060. }
  1061. rcu_read_unlock();
  1062. return 0;
  1063. drop:
  1064. if (skb)
  1065. dev_kfree_skb(skb);
  1066. for (i = 0; i < tx.num_extra_frag; i++)
  1067. if (tx.extra_frag[i])
  1068. dev_kfree_skb(tx.extra_frag[i]);
  1069. kfree(tx.extra_frag);
  1070. rcu_read_unlock();
  1071. return 0;
  1072. }
  1073. /* device xmit handlers */
  1074. int ieee80211_master_start_xmit(struct sk_buff *skb,
  1075. struct net_device *dev)
  1076. {
  1077. struct ieee80211_tx_control control;
  1078. struct ieee80211_tx_packet_data *pkt_data;
  1079. struct net_device *odev = NULL;
  1080. struct ieee80211_sub_if_data *osdata;
  1081. int headroom;
  1082. int ret;
  1083. /*
  1084. * copy control out of the skb so other people can use skb->cb
  1085. */
  1086. pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
  1087. memset(&control, 0, sizeof(struct ieee80211_tx_control));
  1088. if (pkt_data->ifindex)
  1089. odev = dev_get_by_index(&init_net, pkt_data->ifindex);
  1090. if (unlikely(odev && !is_ieee80211_device(odev, dev))) {
  1091. dev_put(odev);
  1092. odev = NULL;
  1093. }
  1094. if (unlikely(!odev)) {
  1095. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  1096. printk(KERN_DEBUG "%s: Discarded packet with nonexistent "
  1097. "originating device\n", dev->name);
  1098. #endif
  1099. dev_kfree_skb(skb);
  1100. return 0;
  1101. }
  1102. osdata = IEEE80211_DEV_TO_SUB_IF(odev);
  1103. headroom = osdata->local->tx_headroom + IEEE80211_ENCRYPT_HEADROOM;
  1104. if (skb_headroom(skb) < headroom) {
  1105. if (pskb_expand_head(skb, headroom, 0, GFP_ATOMIC)) {
  1106. dev_kfree_skb(skb);
  1107. dev_put(odev);
  1108. return 0;
  1109. }
  1110. }
  1111. control.vif = &osdata->vif;
  1112. control.type = osdata->vif.type;
  1113. if (pkt_data->flags & IEEE80211_TXPD_REQ_TX_STATUS)
  1114. control.flags |= IEEE80211_TXCTL_REQ_TX_STATUS;
  1115. if (pkt_data->flags & IEEE80211_TXPD_DO_NOT_ENCRYPT)
  1116. control.flags |= IEEE80211_TXCTL_DO_NOT_ENCRYPT;
  1117. if (pkt_data->flags & IEEE80211_TXPD_REQUEUE)
  1118. control.flags |= IEEE80211_TXCTL_REQUEUE;
  1119. if (pkt_data->flags & IEEE80211_TXPD_EAPOL_FRAME)
  1120. control.flags |= IEEE80211_TXCTL_EAPOL_FRAME;
  1121. if (pkt_data->flags & IEEE80211_TXPD_AMPDU)
  1122. control.flags |= IEEE80211_TXCTL_AMPDU;
  1123. control.queue = pkt_data->queue;
  1124. ret = ieee80211_tx(odev, skb, &control);
  1125. dev_put(odev);
  1126. return ret;
  1127. }
  1128. int ieee80211_monitor_start_xmit(struct sk_buff *skb,
  1129. struct net_device *dev)
  1130. {
  1131. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  1132. struct ieee80211_tx_packet_data *pkt_data;
  1133. struct ieee80211_radiotap_header *prthdr =
  1134. (struct ieee80211_radiotap_header *)skb->data;
  1135. u16 len_rthdr;
  1136. /* check for not even having the fixed radiotap header part */
  1137. if (unlikely(skb->len < sizeof(struct ieee80211_radiotap_header)))
  1138. goto fail; /* too short to be possibly valid */
  1139. /* is it a header version we can trust to find length from? */
  1140. if (unlikely(prthdr->it_version))
  1141. goto fail; /* only version 0 is supported */
  1142. /* then there must be a radiotap header with a length we can use */
  1143. len_rthdr = ieee80211_get_radiotap_len(skb->data);
  1144. /* does the skb contain enough to deliver on the alleged length? */
  1145. if (unlikely(skb->len < len_rthdr))
  1146. goto fail; /* skb too short for claimed rt header extent */
  1147. skb->dev = local->mdev;
  1148. pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
  1149. memset(pkt_data, 0, sizeof(*pkt_data));
  1150. /* needed because we set skb device to master */
  1151. pkt_data->ifindex = dev->ifindex;
  1152. pkt_data->flags |= IEEE80211_TXPD_DO_NOT_ENCRYPT;
  1153. /*
  1154. * fix up the pointers accounting for the radiotap
  1155. * header still being in there. We are being given
  1156. * a precooked IEEE80211 header so no need for
  1157. * normal processing
  1158. */
  1159. skb_set_mac_header(skb, len_rthdr);
  1160. /*
  1161. * these are just fixed to the end of the rt area since we
  1162. * don't have any better information and at this point, nobody cares
  1163. */
  1164. skb_set_network_header(skb, len_rthdr);
  1165. skb_set_transport_header(skb, len_rthdr);
  1166. /* pass the radiotap header up to the next stage intact */
  1167. dev_queue_xmit(skb);
  1168. return NETDEV_TX_OK;
  1169. fail:
  1170. dev_kfree_skb(skb);
  1171. return NETDEV_TX_OK; /* meaning, we dealt with the skb */
  1172. }
  1173. /**
  1174. * ieee80211_subif_start_xmit - netif start_xmit function for Ethernet-type
  1175. * subinterfaces (wlan#, WDS, and VLAN interfaces)
  1176. * @skb: packet to be sent
  1177. * @dev: incoming interface
  1178. *
  1179. * Returns: 0 on success (and frees skb in this case) or 1 on failure (skb will
  1180. * not be freed, and caller is responsible for either retrying later or freeing
  1181. * skb).
  1182. *
  1183. * This function takes in an Ethernet header and encapsulates it with suitable
  1184. * IEEE 802.11 header based on which interface the packet is coming in. The
  1185. * encapsulated packet will then be passed to master interface, wlan#.11, for
  1186. * transmission (through low-level driver).
  1187. */
  1188. int ieee80211_subif_start_xmit(struct sk_buff *skb,
  1189. struct net_device *dev)
  1190. {
  1191. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  1192. struct ieee80211_tx_packet_data *pkt_data;
  1193. struct ieee80211_sub_if_data *sdata;
  1194. int ret = 1, head_need;
  1195. u16 ethertype, hdrlen, meshhdrlen = 0, fc;
  1196. struct ieee80211_hdr hdr;
  1197. struct ieee80211s_hdr mesh_hdr;
  1198. const u8 *encaps_data;
  1199. int encaps_len, skip_header_bytes;
  1200. int nh_pos, h_pos;
  1201. struct sta_info *sta;
  1202. u32 sta_flags = 0;
  1203. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  1204. if (unlikely(skb->len < ETH_HLEN)) {
  1205. printk(KERN_DEBUG "%s: short skb (len=%d)\n",
  1206. dev->name, skb->len);
  1207. ret = 0;
  1208. goto fail;
  1209. }
  1210. nh_pos = skb_network_header(skb) - skb->data;
  1211. h_pos = skb_transport_header(skb) - skb->data;
  1212. /* convert Ethernet header to proper 802.11 header (based on
  1213. * operation mode) */
  1214. ethertype = (skb->data[12] << 8) | skb->data[13];
  1215. fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA;
  1216. switch (sdata->vif.type) {
  1217. case IEEE80211_IF_TYPE_AP:
  1218. case IEEE80211_IF_TYPE_VLAN:
  1219. fc |= IEEE80211_FCTL_FROMDS;
  1220. /* DA BSSID SA */
  1221. memcpy(hdr.addr1, skb->data, ETH_ALEN);
  1222. memcpy(hdr.addr2, dev->dev_addr, ETH_ALEN);
  1223. memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
  1224. hdrlen = 24;
  1225. break;
  1226. case IEEE80211_IF_TYPE_WDS:
  1227. fc |= IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS;
  1228. /* RA TA DA SA */
  1229. memcpy(hdr.addr1, sdata->u.wds.remote_addr, ETH_ALEN);
  1230. memcpy(hdr.addr2, dev->dev_addr, ETH_ALEN);
  1231. memcpy(hdr.addr3, skb->data, ETH_ALEN);
  1232. memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
  1233. hdrlen = 30;
  1234. break;
  1235. #ifdef CONFIG_MAC80211_MESH
  1236. case IEEE80211_IF_TYPE_MESH_POINT:
  1237. fc |= IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS;
  1238. /* RA TA DA SA */
  1239. if (is_multicast_ether_addr(skb->data))
  1240. memcpy(hdr.addr1, skb->data, ETH_ALEN);
  1241. else if (mesh_nexthop_lookup(hdr.addr1, skb, dev))
  1242. return 0;
  1243. memcpy(hdr.addr2, dev->dev_addr, ETH_ALEN);
  1244. memcpy(hdr.addr3, skb->data, ETH_ALEN);
  1245. memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
  1246. if (skb->pkt_type == PACKET_OTHERHOST) {
  1247. /* Forwarded frame, keep mesh ttl and seqnum */
  1248. struct ieee80211s_hdr *prev_meshhdr;
  1249. prev_meshhdr = ((struct ieee80211s_hdr *)skb->cb);
  1250. meshhdrlen = ieee80211_get_mesh_hdrlen(prev_meshhdr);
  1251. memcpy(&mesh_hdr, prev_meshhdr, meshhdrlen);
  1252. sdata->u.sta.mshstats.fwded_frames++;
  1253. } else {
  1254. if (!sdata->u.sta.mshcfg.dot11MeshTTL) {
  1255. /* Do not send frames with mesh_ttl == 0 */
  1256. sdata->u.sta.mshstats.dropped_frames_ttl++;
  1257. ret = 0;
  1258. goto fail;
  1259. }
  1260. meshhdrlen = ieee80211_new_mesh_header(&mesh_hdr,
  1261. sdata);
  1262. }
  1263. hdrlen = 30;
  1264. break;
  1265. #endif
  1266. case IEEE80211_IF_TYPE_STA:
  1267. fc |= IEEE80211_FCTL_TODS;
  1268. /* BSSID SA DA */
  1269. memcpy(hdr.addr1, sdata->u.sta.bssid, ETH_ALEN);
  1270. memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
  1271. memcpy(hdr.addr3, skb->data, ETH_ALEN);
  1272. hdrlen = 24;
  1273. break;
  1274. case IEEE80211_IF_TYPE_IBSS:
  1275. /* DA SA BSSID */
  1276. memcpy(hdr.addr1, skb->data, ETH_ALEN);
  1277. memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
  1278. memcpy(hdr.addr3, sdata->u.sta.bssid, ETH_ALEN);
  1279. hdrlen = 24;
  1280. break;
  1281. default:
  1282. ret = 0;
  1283. goto fail;
  1284. }
  1285. /*
  1286. * There's no need to try to look up the destination
  1287. * if it is a multicast address (which can only happen
  1288. * in AP mode)
  1289. */
  1290. if (!is_multicast_ether_addr(hdr.addr1)) {
  1291. rcu_read_lock();
  1292. sta = sta_info_get(local, hdr.addr1);
  1293. if (sta)
  1294. sta_flags = get_sta_flags(sta);
  1295. rcu_read_unlock();
  1296. }
  1297. /* receiver and we are QoS enabled, use a QoS type frame */
  1298. if (sta_flags & WLAN_STA_WME && local->hw.queues >= 4) {
  1299. fc |= IEEE80211_STYPE_QOS_DATA;
  1300. hdrlen += 2;
  1301. }
  1302. /*
  1303. * Drop unicast frames to unauthorised stations unless they are
  1304. * EAPOL frames from the local station.
  1305. */
  1306. if (unlikely(!is_multicast_ether_addr(hdr.addr1) &&
  1307. !(sta_flags & WLAN_STA_AUTHORIZED) &&
  1308. !(ethertype == ETH_P_PAE &&
  1309. compare_ether_addr(dev->dev_addr,
  1310. skb->data + ETH_ALEN) == 0))) {
  1311. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  1312. DECLARE_MAC_BUF(mac);
  1313. if (net_ratelimit())
  1314. printk(KERN_DEBUG "%s: dropped frame to %s"
  1315. " (unauthorized port)\n", dev->name,
  1316. print_mac(mac, hdr.addr1));
  1317. #endif
  1318. I802_DEBUG_INC(local->tx_handlers_drop_unauth_port);
  1319. ret = 0;
  1320. goto fail;
  1321. }
  1322. hdr.frame_control = cpu_to_le16(fc);
  1323. hdr.duration_id = 0;
  1324. hdr.seq_ctrl = 0;
  1325. skip_header_bytes = ETH_HLEN;
  1326. if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
  1327. encaps_data = bridge_tunnel_header;
  1328. encaps_len = sizeof(bridge_tunnel_header);
  1329. skip_header_bytes -= 2;
  1330. } else if (ethertype >= 0x600) {
  1331. encaps_data = rfc1042_header;
  1332. encaps_len = sizeof(rfc1042_header);
  1333. skip_header_bytes -= 2;
  1334. } else {
  1335. encaps_data = NULL;
  1336. encaps_len = 0;
  1337. }
  1338. skb_pull(skb, skip_header_bytes);
  1339. nh_pos -= skip_header_bytes;
  1340. h_pos -= skip_header_bytes;
  1341. /* TODO: implement support for fragments so that there is no need to
  1342. * reallocate and copy payload; it might be enough to support one
  1343. * extra fragment that would be copied in the beginning of the frame
  1344. * data.. anyway, it would be nice to include this into skb structure
  1345. * somehow
  1346. *
  1347. * There are few options for this:
  1348. * use skb->cb as an extra space for 802.11 header
  1349. * allocate new buffer if not enough headroom
  1350. * make sure that there is enough headroom in every skb by increasing
  1351. * build in headroom in __dev_alloc_skb() (linux/skbuff.h) and
  1352. * alloc_skb() (net/core/skbuff.c)
  1353. */
  1354. head_need = hdrlen + encaps_len + meshhdrlen + local->tx_headroom;
  1355. head_need -= skb_headroom(skb);
  1356. /* We are going to modify skb data, so make a copy of it if happens to
  1357. * be cloned. This could happen, e.g., with Linux bridge code passing
  1358. * us broadcast frames. */
  1359. if (head_need > 0 || skb_cloned(skb)) {
  1360. #if 0
  1361. printk(KERN_DEBUG "%s: need to reallocate buffer for %d bytes "
  1362. "of headroom\n", dev->name, head_need);
  1363. #endif
  1364. if (skb_cloned(skb))
  1365. I802_DEBUG_INC(local->tx_expand_skb_head_cloned);
  1366. else
  1367. I802_DEBUG_INC(local->tx_expand_skb_head);
  1368. /* Since we have to reallocate the buffer, make sure that there
  1369. * is enough room for possible WEP IV/ICV and TKIP (8 bytes
  1370. * before payload and 12 after). */
  1371. if (pskb_expand_head(skb, (head_need > 0 ? head_need + 8 : 8),
  1372. 12, GFP_ATOMIC)) {
  1373. printk(KERN_DEBUG "%s: failed to reallocate TX buffer"
  1374. "\n", dev->name);
  1375. goto fail;
  1376. }
  1377. }
  1378. if (encaps_data) {
  1379. memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
  1380. nh_pos += encaps_len;
  1381. h_pos += encaps_len;
  1382. }
  1383. if (meshhdrlen > 0) {
  1384. memcpy(skb_push(skb, meshhdrlen), &mesh_hdr, meshhdrlen);
  1385. nh_pos += meshhdrlen;
  1386. h_pos += meshhdrlen;
  1387. }
  1388. if (fc & IEEE80211_STYPE_QOS_DATA) {
  1389. __le16 *qos_control;
  1390. qos_control = (__le16*) skb_push(skb, 2);
  1391. memcpy(skb_push(skb, hdrlen - 2), &hdr, hdrlen - 2);
  1392. /*
  1393. * Maybe we could actually set some fields here, for now just
  1394. * initialise to zero to indicate no special operation.
  1395. */
  1396. *qos_control = 0;
  1397. } else
  1398. memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
  1399. nh_pos += hdrlen;
  1400. h_pos += hdrlen;
  1401. pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
  1402. memset(pkt_data, 0, sizeof(struct ieee80211_tx_packet_data));
  1403. pkt_data->ifindex = dev->ifindex;
  1404. if (ethertype == ETH_P_PAE)
  1405. pkt_data->flags |= IEEE80211_TXPD_EAPOL_FRAME;
  1406. skb->dev = local->mdev;
  1407. dev->stats.tx_packets++;
  1408. dev->stats.tx_bytes += skb->len;
  1409. /* Update skb pointers to various headers since this modified frame
  1410. * is going to go through Linux networking code that may potentially
  1411. * need things like pointer to IP header. */
  1412. skb_set_mac_header(skb, 0);
  1413. skb_set_network_header(skb, nh_pos);
  1414. skb_set_transport_header(skb, h_pos);
  1415. dev->trans_start = jiffies;
  1416. dev_queue_xmit(skb);
  1417. return 0;
  1418. fail:
  1419. if (!ret)
  1420. dev_kfree_skb(skb);
  1421. return ret;
  1422. }
  1423. /* helper functions for pending packets for when queues are stopped */
  1424. void ieee80211_clear_tx_pending(struct ieee80211_local *local)
  1425. {
  1426. int i, j;
  1427. struct ieee80211_tx_stored_packet *store;
  1428. for (i = 0; i < local->hw.queues; i++) {
  1429. if (!__ieee80211_queue_pending(local, i))
  1430. continue;
  1431. store = &local->pending_packet[i];
  1432. kfree_skb(store->skb);
  1433. for (j = 0; j < store->num_extra_frag; j++)
  1434. kfree_skb(store->extra_frag[j]);
  1435. kfree(store->extra_frag);
  1436. clear_bit(IEEE80211_LINK_STATE_PENDING, &local->state[i]);
  1437. }
  1438. }
  1439. void ieee80211_tx_pending(unsigned long data)
  1440. {
  1441. struct ieee80211_local *local = (struct ieee80211_local *)data;
  1442. struct net_device *dev = local->mdev;
  1443. struct ieee80211_tx_stored_packet *store;
  1444. struct ieee80211_tx_data tx;
  1445. int i, ret, reschedule = 0;
  1446. netif_tx_lock_bh(dev);
  1447. for (i = 0; i < local->hw.queues; i++) {
  1448. if (__ieee80211_queue_stopped(local, i))
  1449. continue;
  1450. if (!__ieee80211_queue_pending(local, i)) {
  1451. reschedule = 1;
  1452. continue;
  1453. }
  1454. store = &local->pending_packet[i];
  1455. tx.control = &store->control;
  1456. tx.extra_frag = store->extra_frag;
  1457. tx.num_extra_frag = store->num_extra_frag;
  1458. tx.last_frag_rate = store->last_frag_rate;
  1459. tx.flags = 0;
  1460. if (store->last_frag_rate_ctrl_probe)
  1461. tx.flags |= IEEE80211_TX_PROBE_LAST_FRAG;
  1462. ret = __ieee80211_tx(local, store->skb, &tx);
  1463. if (ret) {
  1464. if (ret == IEEE80211_TX_FRAG_AGAIN)
  1465. store->skb = NULL;
  1466. } else {
  1467. clear_bit(IEEE80211_LINK_STATE_PENDING,
  1468. &local->state[i]);
  1469. reschedule = 1;
  1470. }
  1471. }
  1472. netif_tx_unlock_bh(dev);
  1473. if (reschedule) {
  1474. if (!ieee80211_qdisc_installed(dev)) {
  1475. if (!__ieee80211_queue_stopped(local, 0))
  1476. netif_wake_queue(dev);
  1477. } else
  1478. netif_schedule(dev);
  1479. }
  1480. }
  1481. /* functions for drivers to get certain frames */
  1482. static void ieee80211_beacon_add_tim(struct ieee80211_local *local,
  1483. struct ieee80211_if_ap *bss,
  1484. struct sk_buff *skb,
  1485. struct beacon_data *beacon)
  1486. {
  1487. u8 *pos, *tim;
  1488. int aid0 = 0;
  1489. int i, have_bits = 0, n1, n2;
  1490. /* Generate bitmap for TIM only if there are any STAs in power save
  1491. * mode. */
  1492. if (atomic_read(&bss->num_sta_ps) > 0)
  1493. /* in the hope that this is faster than
  1494. * checking byte-for-byte */
  1495. have_bits = !bitmap_empty((unsigned long*)bss->tim,
  1496. IEEE80211_MAX_AID+1);
  1497. if (bss->dtim_count == 0)
  1498. bss->dtim_count = beacon->dtim_period - 1;
  1499. else
  1500. bss->dtim_count--;
  1501. tim = pos = (u8 *) skb_put(skb, 6);
  1502. *pos++ = WLAN_EID_TIM;
  1503. *pos++ = 4;
  1504. *pos++ = bss->dtim_count;
  1505. *pos++ = beacon->dtim_period;
  1506. if (bss->dtim_count == 0 && !skb_queue_empty(&bss->ps_bc_buf))
  1507. aid0 = 1;
  1508. if (have_bits) {
  1509. /* Find largest even number N1 so that bits numbered 1 through
  1510. * (N1 x 8) - 1 in the bitmap are 0 and number N2 so that bits
  1511. * (N2 + 1) x 8 through 2007 are 0. */
  1512. n1 = 0;
  1513. for (i = 0; i < IEEE80211_MAX_TIM_LEN; i++) {
  1514. if (bss->tim[i]) {
  1515. n1 = i & 0xfe;
  1516. break;
  1517. }
  1518. }
  1519. n2 = n1;
  1520. for (i = IEEE80211_MAX_TIM_LEN - 1; i >= n1; i--) {
  1521. if (bss->tim[i]) {
  1522. n2 = i;
  1523. break;
  1524. }
  1525. }
  1526. /* Bitmap control */
  1527. *pos++ = n1 | aid0;
  1528. /* Part Virt Bitmap */
  1529. memcpy(pos, bss->tim + n1, n2 - n1 + 1);
  1530. tim[1] = n2 - n1 + 4;
  1531. skb_put(skb, n2 - n1);
  1532. } else {
  1533. *pos++ = aid0; /* Bitmap control */
  1534. *pos++ = 0; /* Part Virt Bitmap */
  1535. }
  1536. }
  1537. struct sk_buff *ieee80211_beacon_get(struct ieee80211_hw *hw,
  1538. struct ieee80211_vif *vif,
  1539. struct ieee80211_tx_control *control)
  1540. {
  1541. struct ieee80211_local *local = hw_to_local(hw);
  1542. struct sk_buff *skb;
  1543. struct net_device *bdev;
  1544. struct ieee80211_sub_if_data *sdata = NULL;
  1545. struct ieee80211_if_ap *ap = NULL;
  1546. struct rate_selection rsel;
  1547. struct beacon_data *beacon;
  1548. struct ieee80211_supported_band *sband;
  1549. struct ieee80211_mgmt *mgmt;
  1550. int *num_beacons;
  1551. bool err = true;
  1552. u8 *pos;
  1553. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  1554. rcu_read_lock();
  1555. sdata = vif_to_sdata(vif);
  1556. bdev = sdata->dev;
  1557. if (sdata->vif.type == IEEE80211_IF_TYPE_AP) {
  1558. ap = &sdata->u.ap;
  1559. beacon = rcu_dereference(ap->beacon);
  1560. if (ap && beacon) {
  1561. /*
  1562. * headroom, head length,
  1563. * tail length and maximum TIM length
  1564. */
  1565. skb = dev_alloc_skb(local->tx_headroom +
  1566. beacon->head_len +
  1567. beacon->tail_len + 256);
  1568. if (!skb)
  1569. goto out;
  1570. skb_reserve(skb, local->tx_headroom);
  1571. memcpy(skb_put(skb, beacon->head_len), beacon->head,
  1572. beacon->head_len);
  1573. ieee80211_include_sequence(sdata,
  1574. (struct ieee80211_hdr *)skb->data);
  1575. /*
  1576. * Not very nice, but we want to allow the driver to call
  1577. * ieee80211_beacon_get() as a response to the set_tim()
  1578. * callback. That, however, is already invoked under the
  1579. * sta_lock to guarantee consistent and race-free update
  1580. * of the tim bitmap in mac80211 and the driver.
  1581. */
  1582. if (local->tim_in_locked_section) {
  1583. ieee80211_beacon_add_tim(local, ap, skb, beacon);
  1584. } else {
  1585. unsigned long flags;
  1586. spin_lock_irqsave(&local->sta_lock, flags);
  1587. ieee80211_beacon_add_tim(local, ap, skb, beacon);
  1588. spin_unlock_irqrestore(&local->sta_lock, flags);
  1589. }
  1590. if (beacon->tail)
  1591. memcpy(skb_put(skb, beacon->tail_len),
  1592. beacon->tail, beacon->tail_len);
  1593. num_beacons = &ap->num_beacons;
  1594. err = false;
  1595. }
  1596. } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
  1597. /* headroom, head length, tail length and maximum TIM length */
  1598. skb = dev_alloc_skb(local->tx_headroom + 400);
  1599. if (!skb)
  1600. goto out;
  1601. skb_reserve(skb, local->hw.extra_tx_headroom);
  1602. mgmt = (struct ieee80211_mgmt *)
  1603. skb_put(skb, 24 + sizeof(mgmt->u.beacon));
  1604. memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
  1605. mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
  1606. IEEE80211_STYPE_BEACON);
  1607. memset(mgmt->da, 0xff, ETH_ALEN);
  1608. memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
  1609. /* BSSID is left zeroed, wildcard value */
  1610. mgmt->u.beacon.beacon_int =
  1611. cpu_to_le16(local->hw.conf.beacon_int);
  1612. mgmt->u.beacon.capab_info = 0x0; /* 0x0 for MPs */
  1613. pos = skb_put(skb, 2);
  1614. *pos++ = WLAN_EID_SSID;
  1615. *pos++ = 0x0;
  1616. mesh_mgmt_ies_add(skb, sdata->dev);
  1617. num_beacons = &sdata->u.sta.num_beacons;
  1618. err = false;
  1619. }
  1620. if (err) {
  1621. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  1622. if (net_ratelimit())
  1623. printk(KERN_DEBUG "no beacon data avail for %s\n",
  1624. bdev->name);
  1625. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  1626. skb = NULL;
  1627. goto out;
  1628. }
  1629. if (control) {
  1630. rate_control_get_rate(local->mdev, sband, skb, &rsel);
  1631. if (!rsel.rate) {
  1632. if (net_ratelimit()) {
  1633. printk(KERN_DEBUG "%s: ieee80211_beacon_get: "
  1634. "no rate found\n",
  1635. wiphy_name(local->hw.wiphy));
  1636. }
  1637. dev_kfree_skb(skb);
  1638. skb = NULL;
  1639. goto out;
  1640. }
  1641. control->vif = vif;
  1642. control->tx_rate = rsel.rate;
  1643. if (sdata->bss_conf.use_short_preamble &&
  1644. rsel.rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
  1645. control->flags |= IEEE80211_TXCTL_SHORT_PREAMBLE;
  1646. control->antenna_sel_tx = local->hw.conf.antenna_sel_tx;
  1647. control->flags |= IEEE80211_TXCTL_NO_ACK;
  1648. control->retry_limit = 1;
  1649. control->flags |= IEEE80211_TXCTL_CLEAR_PS_FILT;
  1650. }
  1651. (*num_beacons)++;
  1652. out:
  1653. rcu_read_unlock();
  1654. return skb;
  1655. }
  1656. EXPORT_SYMBOL(ieee80211_beacon_get);
  1657. void ieee80211_rts_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  1658. const void *frame, size_t frame_len,
  1659. const struct ieee80211_tx_control *frame_txctl,
  1660. struct ieee80211_rts *rts)
  1661. {
  1662. const struct ieee80211_hdr *hdr = frame;
  1663. u16 fctl;
  1664. fctl = IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS;
  1665. rts->frame_control = cpu_to_le16(fctl);
  1666. rts->duration = ieee80211_rts_duration(hw, vif, frame_len,
  1667. frame_txctl);
  1668. memcpy(rts->ra, hdr->addr1, sizeof(rts->ra));
  1669. memcpy(rts->ta, hdr->addr2, sizeof(rts->ta));
  1670. }
  1671. EXPORT_SYMBOL(ieee80211_rts_get);
  1672. void ieee80211_ctstoself_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  1673. const void *frame, size_t frame_len,
  1674. const struct ieee80211_tx_control *frame_txctl,
  1675. struct ieee80211_cts *cts)
  1676. {
  1677. const struct ieee80211_hdr *hdr = frame;
  1678. u16 fctl;
  1679. fctl = IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS;
  1680. cts->frame_control = cpu_to_le16(fctl);
  1681. cts->duration = ieee80211_ctstoself_duration(hw, vif,
  1682. frame_len, frame_txctl);
  1683. memcpy(cts->ra, hdr->addr1, sizeof(cts->ra));
  1684. }
  1685. EXPORT_SYMBOL(ieee80211_ctstoself_get);
  1686. struct sk_buff *
  1687. ieee80211_get_buffered_bc(struct ieee80211_hw *hw,
  1688. struct ieee80211_vif *vif,
  1689. struct ieee80211_tx_control *control)
  1690. {
  1691. struct ieee80211_local *local = hw_to_local(hw);
  1692. struct sk_buff *skb;
  1693. struct sta_info *sta;
  1694. ieee80211_tx_handler *handler;
  1695. struct ieee80211_tx_data tx;
  1696. ieee80211_tx_result res = TX_DROP;
  1697. struct net_device *bdev;
  1698. struct ieee80211_sub_if_data *sdata;
  1699. struct ieee80211_if_ap *bss = NULL;
  1700. struct beacon_data *beacon;
  1701. sdata = vif_to_sdata(vif);
  1702. bdev = sdata->dev;
  1703. if (!bss)
  1704. return NULL;
  1705. rcu_read_lock();
  1706. beacon = rcu_dereference(bss->beacon);
  1707. if (sdata->vif.type != IEEE80211_IF_TYPE_AP || !beacon ||
  1708. !beacon->head) {
  1709. rcu_read_unlock();
  1710. return NULL;
  1711. }
  1712. if (bss->dtim_count != 0)
  1713. return NULL; /* send buffered bc/mc only after DTIM beacon */
  1714. memset(control, 0, sizeof(*control));
  1715. while (1) {
  1716. skb = skb_dequeue(&bss->ps_bc_buf);
  1717. if (!skb)
  1718. return NULL;
  1719. local->total_ps_buffered--;
  1720. if (!skb_queue_empty(&bss->ps_bc_buf) && skb->len >= 2) {
  1721. struct ieee80211_hdr *hdr =
  1722. (struct ieee80211_hdr *) skb->data;
  1723. /* more buffered multicast/broadcast frames ==> set
  1724. * MoreData flag in IEEE 802.11 header to inform PS
  1725. * STAs */
  1726. hdr->frame_control |=
  1727. cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  1728. }
  1729. if (!ieee80211_tx_prepare(&tx, skb, local->mdev, control))
  1730. break;
  1731. dev_kfree_skb_any(skb);
  1732. }
  1733. sta = tx.sta;
  1734. tx.flags |= IEEE80211_TX_PS_BUFFERED;
  1735. tx.channel = local->hw.conf.channel;
  1736. for (handler = ieee80211_tx_handlers; *handler != NULL; handler++) {
  1737. res = (*handler)(&tx);
  1738. if (res == TX_DROP || res == TX_QUEUED)
  1739. break;
  1740. }
  1741. skb = tx.skb; /* handlers are allowed to change skb */
  1742. if (res == TX_DROP) {
  1743. I802_DEBUG_INC(local->tx_handlers_drop);
  1744. dev_kfree_skb(skb);
  1745. skb = NULL;
  1746. } else if (res == TX_QUEUED) {
  1747. I802_DEBUG_INC(local->tx_handlers_queued);
  1748. skb = NULL;
  1749. }
  1750. rcu_read_unlock();
  1751. return skb;
  1752. }
  1753. EXPORT_SYMBOL(ieee80211_get_buffered_bc);