tx.c 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681
  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 "driver-ops.h"
  27. #include "led.h"
  28. #include "mesh.h"
  29. #include "wep.h"
  30. #include "wpa.h"
  31. #include "wme.h"
  32. #include "rate.h"
  33. /* misc utils */
  34. static __le16 ieee80211_duration(struct ieee80211_tx_data *tx,
  35. struct sk_buff *skb, int group_addr,
  36. int next_frag_len)
  37. {
  38. int rate, mrate, erp, dur, i;
  39. struct ieee80211_rate *txrate;
  40. struct ieee80211_local *local = tx->local;
  41. struct ieee80211_supported_band *sband;
  42. struct ieee80211_hdr *hdr;
  43. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  44. /* assume HW handles this */
  45. if (info->control.rates[0].flags & IEEE80211_TX_RC_MCS)
  46. return 0;
  47. /* uh huh? */
  48. if (WARN_ON_ONCE(info->control.rates[0].idx < 0))
  49. return 0;
  50. sband = local->hw.wiphy->bands[tx->channel->band];
  51. txrate = &sband->bitrates[info->control.rates[0].idx];
  52. erp = txrate->flags & IEEE80211_RATE_ERP_G;
  53. /*
  54. * data and mgmt (except PS Poll):
  55. * - during CFP: 32768
  56. * - during contention period:
  57. * if addr1 is group address: 0
  58. * if more fragments = 0 and addr1 is individual address: time to
  59. * transmit one ACK plus SIFS
  60. * if more fragments = 1 and addr1 is individual address: time to
  61. * transmit next fragment plus 2 x ACK plus 3 x SIFS
  62. *
  63. * IEEE 802.11, 9.6:
  64. * - control response frame (CTS or ACK) shall be transmitted using the
  65. * same rate as the immediately previous frame in the frame exchange
  66. * sequence, if this rate belongs to the PHY mandatory rates, or else
  67. * at the highest possible rate belonging to the PHY rates in the
  68. * BSSBasicRateSet
  69. */
  70. hdr = (struct ieee80211_hdr *)skb->data;
  71. if (ieee80211_is_ctl(hdr->frame_control)) {
  72. /* TODO: These control frames are not currently sent by
  73. * mac80211, but should they be implemented, this function
  74. * needs to be updated to support duration field calculation.
  75. *
  76. * RTS: time needed to transmit pending data/mgmt frame plus
  77. * one CTS frame plus one ACK frame plus 3 x SIFS
  78. * CTS: duration of immediately previous RTS minus time
  79. * required to transmit CTS and its SIFS
  80. * ACK: 0 if immediately previous directed data/mgmt had
  81. * more=0, with more=1 duration in ACK frame is duration
  82. * from previous frame minus time needed to transmit ACK
  83. * and its SIFS
  84. * PS Poll: BIT(15) | BIT(14) | aid
  85. */
  86. return 0;
  87. }
  88. /* data/mgmt */
  89. if (0 /* FIX: data/mgmt during CFP */)
  90. return cpu_to_le16(32768);
  91. if (group_addr) /* Group address as the destination - no ACK */
  92. return 0;
  93. /* Individual destination address:
  94. * IEEE 802.11, Ch. 9.6 (after IEEE 802.11g changes)
  95. * CTS and ACK frames shall be transmitted using the highest rate in
  96. * basic rate set that is less than or equal to the rate of the
  97. * immediately previous frame and that is using the same modulation
  98. * (CCK or OFDM). If no basic rate set matches with these requirements,
  99. * the highest mandatory rate of the PHY that is less than or equal to
  100. * the rate of the previous frame is used.
  101. * Mandatory rates for IEEE 802.11g PHY: 1, 2, 5.5, 11, 6, 12, 24 Mbps
  102. */
  103. rate = -1;
  104. /* use lowest available if everything fails */
  105. mrate = sband->bitrates[0].bitrate;
  106. for (i = 0; i < sband->n_bitrates; i++) {
  107. struct ieee80211_rate *r = &sband->bitrates[i];
  108. if (r->bitrate > txrate->bitrate)
  109. break;
  110. if (tx->sdata->vif.bss_conf.basic_rates & BIT(i))
  111. rate = r->bitrate;
  112. switch (sband->band) {
  113. case IEEE80211_BAND_2GHZ: {
  114. u32 flag;
  115. if (tx->sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
  116. flag = IEEE80211_RATE_MANDATORY_G;
  117. else
  118. flag = IEEE80211_RATE_MANDATORY_B;
  119. if (r->flags & flag)
  120. mrate = r->bitrate;
  121. break;
  122. }
  123. case IEEE80211_BAND_5GHZ:
  124. if (r->flags & IEEE80211_RATE_MANDATORY_A)
  125. mrate = r->bitrate;
  126. break;
  127. case IEEE80211_NUM_BANDS:
  128. WARN_ON(1);
  129. break;
  130. }
  131. }
  132. if (rate == -1) {
  133. /* No matching basic rate found; use highest suitable mandatory
  134. * PHY rate */
  135. rate = mrate;
  136. }
  137. /* Time needed to transmit ACK
  138. * (10 bytes + 4-byte FCS = 112 bits) plus SIFS; rounded up
  139. * to closest integer */
  140. dur = ieee80211_frame_duration(local, 10, rate, erp,
  141. tx->sdata->vif.bss_conf.use_short_preamble);
  142. if (next_frag_len) {
  143. /* Frame is fragmented: duration increases with time needed to
  144. * transmit next fragment plus ACK and 2 x SIFS. */
  145. dur *= 2; /* ACK + SIFS */
  146. /* next fragment */
  147. dur += ieee80211_frame_duration(local, next_frag_len,
  148. txrate->bitrate, erp,
  149. tx->sdata->vif.bss_conf.use_short_preamble);
  150. }
  151. return cpu_to_le16(dur);
  152. }
  153. static inline int is_ieee80211_device(struct ieee80211_local *local,
  154. struct net_device *dev)
  155. {
  156. return local == wdev_priv(dev->ieee80211_ptr);
  157. }
  158. /* tx handlers */
  159. static ieee80211_tx_result debug_noinline
  160. ieee80211_tx_h_dynamic_ps(struct ieee80211_tx_data *tx)
  161. {
  162. struct ieee80211_local *local = tx->local;
  163. struct ieee80211_if_managed *ifmgd;
  164. /* driver doesn't support power save */
  165. if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS))
  166. return TX_CONTINUE;
  167. /* hardware does dynamic power save */
  168. if (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)
  169. return TX_CONTINUE;
  170. /* dynamic power save disabled */
  171. if (local->hw.conf.dynamic_ps_timeout <= 0)
  172. return TX_CONTINUE;
  173. /* we are scanning, don't enable power save */
  174. if (local->scanning)
  175. return TX_CONTINUE;
  176. if (!local->ps_sdata)
  177. return TX_CONTINUE;
  178. /* No point if we're going to suspend */
  179. if (local->quiescing)
  180. return TX_CONTINUE;
  181. /* dynamic ps is supported only in managed mode */
  182. if (tx->sdata->vif.type != NL80211_IFTYPE_STATION)
  183. return TX_CONTINUE;
  184. ifmgd = &tx->sdata->u.mgd;
  185. /*
  186. * Don't wakeup from power save if u-apsd is enabled, voip ac has
  187. * u-apsd enabled and the frame is in voip class. This effectively
  188. * means that even if all access categories have u-apsd enabled, in
  189. * practise u-apsd is only used with the voip ac. This is a
  190. * workaround for the case when received voip class packets do not
  191. * have correct qos tag for some reason, due the network or the
  192. * peer application.
  193. *
  194. * Note: local->uapsd_queues access is racy here. If the value is
  195. * changed via debugfs, user needs to reassociate manually to have
  196. * everything in sync.
  197. */
  198. if ((ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
  199. && (local->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
  200. && skb_get_queue_mapping(tx->skb) == 0)
  201. return TX_CONTINUE;
  202. if (local->hw.conf.flags & IEEE80211_CONF_PS) {
  203. ieee80211_stop_queues_by_reason(&local->hw,
  204. IEEE80211_QUEUE_STOP_REASON_PS);
  205. ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
  206. ieee80211_queue_work(&local->hw,
  207. &local->dynamic_ps_disable_work);
  208. }
  209. /* Don't restart the timer if we're not disassociated */
  210. if (!ifmgd->associated)
  211. return TX_CONTINUE;
  212. mod_timer(&local->dynamic_ps_timer, jiffies +
  213. msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
  214. return TX_CONTINUE;
  215. }
  216. static ieee80211_tx_result debug_noinline
  217. ieee80211_tx_h_check_assoc(struct ieee80211_tx_data *tx)
  218. {
  219. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
  220. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
  221. bool assoc = false;
  222. if (unlikely(info->flags & IEEE80211_TX_CTL_INJECTED))
  223. return TX_CONTINUE;
  224. if (unlikely(test_bit(SCAN_SW_SCANNING, &tx->local->scanning)) &&
  225. test_bit(SDATA_STATE_OFFCHANNEL, &tx->sdata->state) &&
  226. !ieee80211_is_probe_req(hdr->frame_control) &&
  227. !ieee80211_is_nullfunc(hdr->frame_control))
  228. /*
  229. * When software scanning only nullfunc frames (to notify
  230. * the sleep state to the AP) and probe requests (for the
  231. * active scan) are allowed, all other frames should not be
  232. * sent and we should not get here, but if we do
  233. * nonetheless, drop them to avoid sending them
  234. * off-channel. See the link below and
  235. * ieee80211_start_scan() for more.
  236. *
  237. * http://article.gmane.org/gmane.linux.kernel.wireless.general/30089
  238. */
  239. return TX_DROP;
  240. if (tx->sdata->vif.type == NL80211_IFTYPE_WDS)
  241. return TX_CONTINUE;
  242. if (tx->sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
  243. return TX_CONTINUE;
  244. if (tx->flags & IEEE80211_TX_PS_BUFFERED)
  245. return TX_CONTINUE;
  246. if (tx->sta)
  247. assoc = test_sta_flag(tx->sta, WLAN_STA_ASSOC);
  248. if (likely(tx->flags & IEEE80211_TX_UNICAST)) {
  249. if (unlikely(!assoc &&
  250. tx->sdata->vif.type != NL80211_IFTYPE_ADHOC &&
  251. ieee80211_is_data(hdr->frame_control))) {
  252. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  253. printk(KERN_DEBUG "%s: dropped data frame to not "
  254. "associated station %pM\n",
  255. tx->sdata->name, hdr->addr1);
  256. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  257. I802_DEBUG_INC(tx->local->tx_handlers_drop_not_assoc);
  258. return TX_DROP;
  259. }
  260. } else {
  261. if (unlikely(ieee80211_is_data(hdr->frame_control) &&
  262. tx->local->num_sta == 0 &&
  263. tx->sdata->vif.type != NL80211_IFTYPE_ADHOC)) {
  264. /*
  265. * No associated STAs - no need to send multicast
  266. * frames.
  267. */
  268. return TX_DROP;
  269. }
  270. return TX_CONTINUE;
  271. }
  272. return TX_CONTINUE;
  273. }
  274. /* This function is called whenever the AP is about to exceed the maximum limit
  275. * of buffered frames for power saving STAs. This situation should not really
  276. * happen often during normal operation, so dropping the oldest buffered packet
  277. * from each queue should be OK to make some room for new frames. */
  278. static void purge_old_ps_buffers(struct ieee80211_local *local)
  279. {
  280. int total = 0, purged = 0;
  281. struct sk_buff *skb;
  282. struct ieee80211_sub_if_data *sdata;
  283. struct sta_info *sta;
  284. /*
  285. * virtual interfaces are protected by RCU
  286. */
  287. rcu_read_lock();
  288. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  289. struct ieee80211_if_ap *ap;
  290. if (sdata->vif.type != NL80211_IFTYPE_AP)
  291. continue;
  292. ap = &sdata->u.ap;
  293. skb = skb_dequeue(&ap->ps_bc_buf);
  294. if (skb) {
  295. purged++;
  296. dev_kfree_skb(skb);
  297. }
  298. total += skb_queue_len(&ap->ps_bc_buf);
  299. }
  300. /*
  301. * Drop one frame from each station from the lowest-priority
  302. * AC that has frames at all.
  303. */
  304. list_for_each_entry_rcu(sta, &local->sta_list, list) {
  305. int ac;
  306. for (ac = IEEE80211_AC_BK; ac >= IEEE80211_AC_VO; ac--) {
  307. skb = skb_dequeue(&sta->ps_tx_buf[ac]);
  308. total += skb_queue_len(&sta->ps_tx_buf[ac]);
  309. if (skb) {
  310. purged++;
  311. dev_kfree_skb(skb);
  312. break;
  313. }
  314. }
  315. }
  316. rcu_read_unlock();
  317. local->total_ps_buffered = total;
  318. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  319. wiphy_debug(local->hw.wiphy, "PS buffers full - purged %d frames\n",
  320. purged);
  321. #endif
  322. }
  323. static ieee80211_tx_result
  324. ieee80211_tx_h_multicast_ps_buf(struct ieee80211_tx_data *tx)
  325. {
  326. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
  327. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
  328. /*
  329. * broadcast/multicast frame
  330. *
  331. * If any of the associated stations is in power save mode,
  332. * the frame is buffered to be sent after DTIM beacon frame.
  333. * This is done either by the hardware or us.
  334. */
  335. /* powersaving STAs only in AP/VLAN mode */
  336. if (!tx->sdata->bss)
  337. return TX_CONTINUE;
  338. /* no buffering for ordered frames */
  339. if (ieee80211_has_order(hdr->frame_control))
  340. return TX_CONTINUE;
  341. /* no stations in PS mode */
  342. if (!atomic_read(&tx->sdata->bss->num_sta_ps))
  343. return TX_CONTINUE;
  344. info->flags |= IEEE80211_TX_CTL_SEND_AFTER_DTIM;
  345. /* device releases frame after DTIM beacon */
  346. if (!(tx->local->hw.flags & IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING))
  347. return TX_CONTINUE;
  348. /* buffered in mac80211 */
  349. if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
  350. purge_old_ps_buffers(tx->local);
  351. if (skb_queue_len(&tx->sdata->bss->ps_bc_buf) >= AP_MAX_BC_BUFFER) {
  352. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  353. if (net_ratelimit())
  354. printk(KERN_DEBUG "%s: BC TX buffer full - dropping the oldest frame\n",
  355. tx->sdata->name);
  356. #endif
  357. dev_kfree_skb(skb_dequeue(&tx->sdata->bss->ps_bc_buf));
  358. } else
  359. tx->local->total_ps_buffered++;
  360. skb_queue_tail(&tx->sdata->bss->ps_bc_buf, tx->skb);
  361. return TX_QUEUED;
  362. }
  363. static int ieee80211_use_mfp(__le16 fc, struct sta_info *sta,
  364. struct sk_buff *skb)
  365. {
  366. if (!ieee80211_is_mgmt(fc))
  367. return 0;
  368. if (sta == NULL || !test_sta_flag(sta, WLAN_STA_MFP))
  369. return 0;
  370. if (!ieee80211_is_robust_mgmt_frame((struct ieee80211_hdr *)
  371. skb->data))
  372. return 0;
  373. return 1;
  374. }
  375. static ieee80211_tx_result
  376. ieee80211_tx_h_unicast_ps_buf(struct ieee80211_tx_data *tx)
  377. {
  378. struct sta_info *sta = tx->sta;
  379. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
  380. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
  381. struct ieee80211_local *local = tx->local;
  382. if (unlikely(!sta ||
  383. ieee80211_is_probe_resp(hdr->frame_control) ||
  384. ieee80211_is_auth(hdr->frame_control) ||
  385. ieee80211_is_assoc_resp(hdr->frame_control) ||
  386. ieee80211_is_reassoc_resp(hdr->frame_control)))
  387. return TX_CONTINUE;
  388. if (unlikely((test_sta_flag(sta, WLAN_STA_PS_STA) ||
  389. test_sta_flag(sta, WLAN_STA_PS_DRIVER)) &&
  390. !(info->flags & IEEE80211_TX_CTL_POLL_RESPONSE))) {
  391. int ac = skb_get_queue_mapping(tx->skb);
  392. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  393. printk(KERN_DEBUG "STA %pM aid %d: PS buffer for AC %d\n",
  394. sta->sta.addr, sta->sta.aid, ac);
  395. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  396. if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
  397. purge_old_ps_buffers(tx->local);
  398. if (skb_queue_len(&sta->ps_tx_buf[ac]) >= STA_MAX_TX_BUFFER) {
  399. struct sk_buff *old = skb_dequeue(&sta->ps_tx_buf[ac]);
  400. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  401. if (net_ratelimit())
  402. printk(KERN_DEBUG "%s: STA %pM TX buffer for "
  403. "AC %d full - dropping oldest frame\n",
  404. tx->sdata->name, sta->sta.addr, ac);
  405. #endif
  406. dev_kfree_skb(old);
  407. } else
  408. tx->local->total_ps_buffered++;
  409. info->control.jiffies = jiffies;
  410. info->control.vif = &tx->sdata->vif;
  411. info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
  412. skb_queue_tail(&sta->ps_tx_buf[ac], tx->skb);
  413. if (!timer_pending(&local->sta_cleanup))
  414. mod_timer(&local->sta_cleanup,
  415. round_jiffies(jiffies +
  416. STA_INFO_CLEANUP_INTERVAL));
  417. /*
  418. * We queued up some frames, so the TIM bit might
  419. * need to be set, recalculate it.
  420. */
  421. sta_info_recalc_tim(sta);
  422. return TX_QUEUED;
  423. }
  424. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  425. else if (unlikely(test_sta_flag(sta, WLAN_STA_PS_STA))) {
  426. printk(KERN_DEBUG
  427. "%s: STA %pM in PS mode, but polling/in SP -> send frame\n",
  428. tx->sdata->name, sta->sta.addr);
  429. }
  430. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  431. return TX_CONTINUE;
  432. }
  433. static ieee80211_tx_result debug_noinline
  434. ieee80211_tx_h_ps_buf(struct ieee80211_tx_data *tx)
  435. {
  436. if (unlikely(tx->flags & IEEE80211_TX_PS_BUFFERED))
  437. return TX_CONTINUE;
  438. if (tx->flags & IEEE80211_TX_UNICAST)
  439. return ieee80211_tx_h_unicast_ps_buf(tx);
  440. else
  441. return ieee80211_tx_h_multicast_ps_buf(tx);
  442. }
  443. static ieee80211_tx_result debug_noinline
  444. ieee80211_tx_h_check_control_port_protocol(struct ieee80211_tx_data *tx)
  445. {
  446. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
  447. if (unlikely(tx->sdata->control_port_protocol == tx->skb->protocol &&
  448. tx->sdata->control_port_no_encrypt))
  449. info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  450. return TX_CONTINUE;
  451. }
  452. static ieee80211_tx_result debug_noinline
  453. ieee80211_tx_h_select_key(struct ieee80211_tx_data *tx)
  454. {
  455. struct ieee80211_key *key = NULL;
  456. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
  457. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
  458. if (unlikely(info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT))
  459. tx->key = NULL;
  460. else if (tx->sta && (key = rcu_dereference(tx->sta->ptk)))
  461. tx->key = key;
  462. else if (ieee80211_is_mgmt(hdr->frame_control) &&
  463. is_multicast_ether_addr(hdr->addr1) &&
  464. ieee80211_is_robust_mgmt_frame(hdr) &&
  465. (key = rcu_dereference(tx->sdata->default_mgmt_key)))
  466. tx->key = key;
  467. else if (is_multicast_ether_addr(hdr->addr1) &&
  468. (key = rcu_dereference(tx->sdata->default_multicast_key)))
  469. tx->key = key;
  470. else if (!is_multicast_ether_addr(hdr->addr1) &&
  471. (key = rcu_dereference(tx->sdata->default_unicast_key)))
  472. tx->key = key;
  473. else if (tx->sdata->drop_unencrypted &&
  474. (tx->skb->protocol != tx->sdata->control_port_protocol) &&
  475. !(info->flags & IEEE80211_TX_CTL_INJECTED) &&
  476. (!ieee80211_is_robust_mgmt_frame(hdr) ||
  477. (ieee80211_is_action(hdr->frame_control) &&
  478. tx->sta && test_sta_flag(tx->sta, WLAN_STA_MFP)))) {
  479. I802_DEBUG_INC(tx->local->tx_handlers_drop_unencrypted);
  480. return TX_DROP;
  481. } else
  482. tx->key = NULL;
  483. if (tx->key) {
  484. bool skip_hw = false;
  485. tx->key->tx_rx_count++;
  486. /* TODO: add threshold stuff again */
  487. switch (tx->key->conf.cipher) {
  488. case WLAN_CIPHER_SUITE_WEP40:
  489. case WLAN_CIPHER_SUITE_WEP104:
  490. case WLAN_CIPHER_SUITE_TKIP:
  491. if (!ieee80211_is_data_present(hdr->frame_control))
  492. tx->key = NULL;
  493. break;
  494. case WLAN_CIPHER_SUITE_CCMP:
  495. if (!ieee80211_is_data_present(hdr->frame_control) &&
  496. !ieee80211_use_mfp(hdr->frame_control, tx->sta,
  497. tx->skb))
  498. tx->key = NULL;
  499. else
  500. skip_hw = (tx->key->conf.flags &
  501. IEEE80211_KEY_FLAG_SW_MGMT) &&
  502. ieee80211_is_mgmt(hdr->frame_control);
  503. break;
  504. case WLAN_CIPHER_SUITE_AES_CMAC:
  505. if (!ieee80211_is_mgmt(hdr->frame_control))
  506. tx->key = NULL;
  507. break;
  508. }
  509. if (unlikely(tx->key && tx->key->flags & KEY_FLAG_TAINTED))
  510. return TX_DROP;
  511. if (!skip_hw && tx->key &&
  512. tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)
  513. info->control.hw_key = &tx->key->conf;
  514. }
  515. return TX_CONTINUE;
  516. }
  517. static ieee80211_tx_result debug_noinline
  518. ieee80211_tx_h_rate_ctrl(struct ieee80211_tx_data *tx)
  519. {
  520. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
  521. struct ieee80211_hdr *hdr = (void *)tx->skb->data;
  522. struct ieee80211_supported_band *sband;
  523. struct ieee80211_rate *rate;
  524. int i;
  525. u32 len;
  526. bool inval = false, rts = false, short_preamble = false;
  527. struct ieee80211_tx_rate_control txrc;
  528. bool assoc = false;
  529. memset(&txrc, 0, sizeof(txrc));
  530. sband = tx->local->hw.wiphy->bands[tx->channel->band];
  531. len = min_t(u32, tx->skb->len + FCS_LEN,
  532. tx->local->hw.wiphy->frag_threshold);
  533. /* set up the tx rate control struct we give the RC algo */
  534. txrc.hw = local_to_hw(tx->local);
  535. txrc.sband = sband;
  536. txrc.bss_conf = &tx->sdata->vif.bss_conf;
  537. txrc.skb = tx->skb;
  538. txrc.reported_rate.idx = -1;
  539. txrc.rate_idx_mask = tx->sdata->rc_rateidx_mask[tx->channel->band];
  540. if (txrc.rate_idx_mask == (1 << sband->n_bitrates) - 1)
  541. txrc.max_rate_idx = -1;
  542. else
  543. txrc.max_rate_idx = fls(txrc.rate_idx_mask) - 1;
  544. txrc.bss = (tx->sdata->vif.type == NL80211_IFTYPE_AP ||
  545. tx->sdata->vif.type == NL80211_IFTYPE_ADHOC);
  546. /* set up RTS protection if desired */
  547. if (len > tx->local->hw.wiphy->rts_threshold) {
  548. txrc.rts = rts = true;
  549. }
  550. /*
  551. * Use short preamble if the BSS can handle it, but not for
  552. * management frames unless we know the receiver can handle
  553. * that -- the management frame might be to a station that
  554. * just wants a probe response.
  555. */
  556. if (tx->sdata->vif.bss_conf.use_short_preamble &&
  557. (ieee80211_is_data(hdr->frame_control) ||
  558. (tx->sta && test_sta_flag(tx->sta, WLAN_STA_SHORT_PREAMBLE))))
  559. txrc.short_preamble = short_preamble = true;
  560. if (tx->sta)
  561. assoc = test_sta_flag(tx->sta, WLAN_STA_ASSOC);
  562. /*
  563. * Lets not bother rate control if we're associated and cannot
  564. * talk to the sta. This should not happen.
  565. */
  566. if (WARN(test_bit(SCAN_SW_SCANNING, &tx->local->scanning) && assoc &&
  567. !rate_usable_index_exists(sband, &tx->sta->sta),
  568. "%s: Dropped data frame as no usable bitrate found while "
  569. "scanning and associated. Target station: "
  570. "%pM on %d GHz band\n",
  571. tx->sdata->name, hdr->addr1,
  572. tx->channel->band ? 5 : 2))
  573. return TX_DROP;
  574. /*
  575. * If we're associated with the sta at this point we know we can at
  576. * least send the frame at the lowest bit rate.
  577. */
  578. rate_control_get_rate(tx->sdata, tx->sta, &txrc);
  579. if (unlikely(info->control.rates[0].idx < 0))
  580. return TX_DROP;
  581. if (txrc.reported_rate.idx < 0) {
  582. txrc.reported_rate = info->control.rates[0];
  583. if (tx->sta && ieee80211_is_data(hdr->frame_control))
  584. tx->sta->last_tx_rate = txrc.reported_rate;
  585. } else if (tx->sta)
  586. tx->sta->last_tx_rate = txrc.reported_rate;
  587. if (unlikely(!info->control.rates[0].count))
  588. info->control.rates[0].count = 1;
  589. if (WARN_ON_ONCE((info->control.rates[0].count > 1) &&
  590. (info->flags & IEEE80211_TX_CTL_NO_ACK)))
  591. info->control.rates[0].count = 1;
  592. if (is_multicast_ether_addr(hdr->addr1)) {
  593. /*
  594. * XXX: verify the rate is in the basic rateset
  595. */
  596. return TX_CONTINUE;
  597. }
  598. /*
  599. * set up the RTS/CTS rate as the fastest basic rate
  600. * that is not faster than the data rate
  601. *
  602. * XXX: Should this check all retry rates?
  603. */
  604. if (!(info->control.rates[0].flags & IEEE80211_TX_RC_MCS)) {
  605. s8 baserate = 0;
  606. rate = &sband->bitrates[info->control.rates[0].idx];
  607. for (i = 0; i < sband->n_bitrates; i++) {
  608. /* must be a basic rate */
  609. if (!(tx->sdata->vif.bss_conf.basic_rates & BIT(i)))
  610. continue;
  611. /* must not be faster than the data rate */
  612. if (sband->bitrates[i].bitrate > rate->bitrate)
  613. continue;
  614. /* maximum */
  615. if (sband->bitrates[baserate].bitrate <
  616. sband->bitrates[i].bitrate)
  617. baserate = i;
  618. }
  619. info->control.rts_cts_rate_idx = baserate;
  620. }
  621. for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
  622. /*
  623. * make sure there's no valid rate following
  624. * an invalid one, just in case drivers don't
  625. * take the API seriously to stop at -1.
  626. */
  627. if (inval) {
  628. info->control.rates[i].idx = -1;
  629. continue;
  630. }
  631. if (info->control.rates[i].idx < 0) {
  632. inval = true;
  633. continue;
  634. }
  635. /*
  636. * For now assume MCS is already set up correctly, this
  637. * needs to be fixed.
  638. */
  639. if (info->control.rates[i].flags & IEEE80211_TX_RC_MCS) {
  640. WARN_ON(info->control.rates[i].idx > 76);
  641. continue;
  642. }
  643. /* set up RTS protection if desired */
  644. if (rts)
  645. info->control.rates[i].flags |=
  646. IEEE80211_TX_RC_USE_RTS_CTS;
  647. /* RC is busted */
  648. if (WARN_ON_ONCE(info->control.rates[i].idx >=
  649. sband->n_bitrates)) {
  650. info->control.rates[i].idx = -1;
  651. continue;
  652. }
  653. rate = &sband->bitrates[info->control.rates[i].idx];
  654. /* set up short preamble */
  655. if (short_preamble &&
  656. rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
  657. info->control.rates[i].flags |=
  658. IEEE80211_TX_RC_USE_SHORT_PREAMBLE;
  659. /* set up G protection */
  660. if (!rts && tx->sdata->vif.bss_conf.use_cts_prot &&
  661. rate->flags & IEEE80211_RATE_ERP_G)
  662. info->control.rates[i].flags |=
  663. IEEE80211_TX_RC_USE_CTS_PROTECT;
  664. }
  665. return TX_CONTINUE;
  666. }
  667. static ieee80211_tx_result debug_noinline
  668. ieee80211_tx_h_sequence(struct ieee80211_tx_data *tx)
  669. {
  670. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
  671. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
  672. u16 *seq;
  673. u8 *qc;
  674. int tid;
  675. /*
  676. * Packet injection may want to control the sequence
  677. * number, if we have no matching interface then we
  678. * neither assign one ourselves nor ask the driver to.
  679. */
  680. if (unlikely(info->control.vif->type == NL80211_IFTYPE_MONITOR))
  681. return TX_CONTINUE;
  682. if (unlikely(ieee80211_is_ctl(hdr->frame_control)))
  683. return TX_CONTINUE;
  684. if (ieee80211_hdrlen(hdr->frame_control) < 24)
  685. return TX_CONTINUE;
  686. if (ieee80211_is_qos_nullfunc(hdr->frame_control))
  687. return TX_CONTINUE;
  688. /*
  689. * Anything but QoS data that has a sequence number field
  690. * (is long enough) gets a sequence number from the global
  691. * counter.
  692. */
  693. if (!ieee80211_is_data_qos(hdr->frame_control)) {
  694. /* driver should assign sequence number */
  695. info->flags |= IEEE80211_TX_CTL_ASSIGN_SEQ;
  696. /* for pure STA mode without beacons, we can do it */
  697. hdr->seq_ctrl = cpu_to_le16(tx->sdata->sequence_number);
  698. tx->sdata->sequence_number += 0x10;
  699. return TX_CONTINUE;
  700. }
  701. /*
  702. * This should be true for injected/management frames only, for
  703. * management frames we have set the IEEE80211_TX_CTL_ASSIGN_SEQ
  704. * above since they are not QoS-data frames.
  705. */
  706. if (!tx->sta)
  707. return TX_CONTINUE;
  708. /* include per-STA, per-TID sequence counter */
  709. qc = ieee80211_get_qos_ctl(hdr);
  710. tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
  711. seq = &tx->sta->tid_seq[tid];
  712. hdr->seq_ctrl = cpu_to_le16(*seq);
  713. /* Increase the sequence number. */
  714. *seq = (*seq + 0x10) & IEEE80211_SCTL_SEQ;
  715. return TX_CONTINUE;
  716. }
  717. static int ieee80211_fragment(struct ieee80211_tx_data *tx,
  718. struct sk_buff *skb, int hdrlen,
  719. int frag_threshold)
  720. {
  721. struct ieee80211_local *local = tx->local;
  722. struct ieee80211_tx_info *info;
  723. struct sk_buff *tmp;
  724. int per_fragm = frag_threshold - hdrlen - FCS_LEN;
  725. int pos = hdrlen + per_fragm;
  726. int rem = skb->len - hdrlen - per_fragm;
  727. if (WARN_ON(rem < 0))
  728. return -EINVAL;
  729. /* first fragment was already added to queue by caller */
  730. while (rem) {
  731. int fraglen = per_fragm;
  732. if (fraglen > rem)
  733. fraglen = rem;
  734. rem -= fraglen;
  735. tmp = dev_alloc_skb(local->tx_headroom +
  736. frag_threshold +
  737. IEEE80211_ENCRYPT_HEADROOM +
  738. IEEE80211_ENCRYPT_TAILROOM);
  739. if (!tmp)
  740. return -ENOMEM;
  741. __skb_queue_tail(&tx->skbs, tmp);
  742. skb_reserve(tmp, local->tx_headroom +
  743. IEEE80211_ENCRYPT_HEADROOM);
  744. /* copy control information */
  745. memcpy(tmp->cb, skb->cb, sizeof(tmp->cb));
  746. info = IEEE80211_SKB_CB(tmp);
  747. info->flags &= ~(IEEE80211_TX_CTL_CLEAR_PS_FILT |
  748. IEEE80211_TX_CTL_FIRST_FRAGMENT);
  749. if (rem)
  750. info->flags |= IEEE80211_TX_CTL_MORE_FRAMES;
  751. skb_copy_queue_mapping(tmp, skb);
  752. tmp->priority = skb->priority;
  753. tmp->dev = skb->dev;
  754. /* copy header and data */
  755. memcpy(skb_put(tmp, hdrlen), skb->data, hdrlen);
  756. memcpy(skb_put(tmp, fraglen), skb->data + pos, fraglen);
  757. pos += fraglen;
  758. }
  759. /* adjust first fragment's length */
  760. skb->len = hdrlen + per_fragm;
  761. return 0;
  762. }
  763. static ieee80211_tx_result debug_noinline
  764. ieee80211_tx_h_fragment(struct ieee80211_tx_data *tx)
  765. {
  766. struct sk_buff *skb = tx->skb;
  767. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  768. struct ieee80211_hdr *hdr = (void *)skb->data;
  769. int frag_threshold = tx->local->hw.wiphy->frag_threshold;
  770. int hdrlen;
  771. int fragnum;
  772. /* no matter what happens, tx->skb moves to tx->skbs */
  773. __skb_queue_tail(&tx->skbs, skb);
  774. tx->skb = NULL;
  775. if (info->flags & IEEE80211_TX_CTL_DONTFRAG)
  776. return TX_CONTINUE;
  777. if (tx->local->ops->set_frag_threshold)
  778. return TX_CONTINUE;
  779. /*
  780. * Warn when submitting a fragmented A-MPDU frame and drop it.
  781. * This scenario is handled in ieee80211_tx_prepare but extra
  782. * caution taken here as fragmented ampdu may cause Tx stop.
  783. */
  784. if (WARN_ON(info->flags & IEEE80211_TX_CTL_AMPDU))
  785. return TX_DROP;
  786. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  787. /* internal error, why isn't DONTFRAG set? */
  788. if (WARN_ON(skb->len + FCS_LEN <= frag_threshold))
  789. return TX_DROP;
  790. /*
  791. * Now fragment the frame. This will allocate all the fragments and
  792. * chain them (using skb as the first fragment) to skb->next.
  793. * During transmission, we will remove the successfully transmitted
  794. * fragments from this list. When the low-level driver rejects one
  795. * of the fragments then we will simply pretend to accept the skb
  796. * but store it away as pending.
  797. */
  798. if (ieee80211_fragment(tx, skb, hdrlen, frag_threshold))
  799. return TX_DROP;
  800. /* update duration/seq/flags of fragments */
  801. fragnum = 0;
  802. skb_queue_walk(&tx->skbs, skb) {
  803. int next_len;
  804. const __le16 morefrags = cpu_to_le16(IEEE80211_FCTL_MOREFRAGS);
  805. hdr = (void *)skb->data;
  806. info = IEEE80211_SKB_CB(skb);
  807. if (!skb_queue_is_last(&tx->skbs, skb)) {
  808. hdr->frame_control |= morefrags;
  809. /*
  810. * No multi-rate retries for fragmented frames, that
  811. * would completely throw off the NAV at other STAs.
  812. */
  813. info->control.rates[1].idx = -1;
  814. info->control.rates[2].idx = -1;
  815. info->control.rates[3].idx = -1;
  816. info->control.rates[4].idx = -1;
  817. BUILD_BUG_ON(IEEE80211_TX_MAX_RATES != 5);
  818. info->flags &= ~IEEE80211_TX_CTL_RATE_CTRL_PROBE;
  819. } else {
  820. hdr->frame_control &= ~morefrags;
  821. next_len = 0;
  822. }
  823. hdr->seq_ctrl |= cpu_to_le16(fragnum & IEEE80211_SCTL_FRAG);
  824. fragnum++;
  825. }
  826. return TX_CONTINUE;
  827. }
  828. static ieee80211_tx_result debug_noinline
  829. ieee80211_tx_h_stats(struct ieee80211_tx_data *tx)
  830. {
  831. struct sk_buff *skb;
  832. if (!tx->sta)
  833. return TX_CONTINUE;
  834. tx->sta->tx_packets++;
  835. skb_queue_walk(&tx->skbs, skb) {
  836. tx->sta->tx_fragments++;
  837. tx->sta->tx_bytes += skb->len;
  838. }
  839. return TX_CONTINUE;
  840. }
  841. static ieee80211_tx_result debug_noinline
  842. ieee80211_tx_h_encrypt(struct ieee80211_tx_data *tx)
  843. {
  844. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
  845. if (!tx->key)
  846. return TX_CONTINUE;
  847. switch (tx->key->conf.cipher) {
  848. case WLAN_CIPHER_SUITE_WEP40:
  849. case WLAN_CIPHER_SUITE_WEP104:
  850. return ieee80211_crypto_wep_encrypt(tx);
  851. case WLAN_CIPHER_SUITE_TKIP:
  852. return ieee80211_crypto_tkip_encrypt(tx);
  853. case WLAN_CIPHER_SUITE_CCMP:
  854. return ieee80211_crypto_ccmp_encrypt(tx);
  855. case WLAN_CIPHER_SUITE_AES_CMAC:
  856. return ieee80211_crypto_aes_cmac_encrypt(tx);
  857. default:
  858. /* handle hw-only algorithm */
  859. if (info->control.hw_key) {
  860. ieee80211_tx_set_protected(tx);
  861. return TX_CONTINUE;
  862. }
  863. break;
  864. }
  865. return TX_DROP;
  866. }
  867. static ieee80211_tx_result debug_noinline
  868. ieee80211_tx_h_calculate_duration(struct ieee80211_tx_data *tx)
  869. {
  870. struct sk_buff *skb;
  871. struct ieee80211_hdr *hdr;
  872. int next_len;
  873. bool group_addr;
  874. skb_queue_walk(&tx->skbs, skb) {
  875. hdr = (void *) skb->data;
  876. if (unlikely(ieee80211_is_pspoll(hdr->frame_control)))
  877. break; /* must not overwrite AID */
  878. if (!skb_queue_is_last(&tx->skbs, skb)) {
  879. struct sk_buff *next = skb_queue_next(&tx->skbs, skb);
  880. next_len = next->len;
  881. } else
  882. next_len = 0;
  883. group_addr = is_multicast_ether_addr(hdr->addr1);
  884. hdr->duration_id =
  885. ieee80211_duration(tx, skb, group_addr, next_len);
  886. }
  887. return TX_CONTINUE;
  888. }
  889. /* actual transmit path */
  890. static bool ieee80211_tx_prep_agg(struct ieee80211_tx_data *tx,
  891. struct sk_buff *skb,
  892. struct ieee80211_tx_info *info,
  893. struct tid_ampdu_tx *tid_tx,
  894. int tid)
  895. {
  896. bool queued = false;
  897. if (test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) {
  898. info->flags |= IEEE80211_TX_CTL_AMPDU;
  899. } else if (test_bit(HT_AGG_STATE_WANT_START, &tid_tx->state)) {
  900. /*
  901. * nothing -- this aggregation session is being started
  902. * but that might still fail with the driver
  903. */
  904. } else {
  905. spin_lock(&tx->sta->lock);
  906. /*
  907. * Need to re-check now, because we may get here
  908. *
  909. * 1) in the window during which the setup is actually
  910. * already done, but not marked yet because not all
  911. * packets are spliced over to the driver pending
  912. * queue yet -- if this happened we acquire the lock
  913. * either before or after the splice happens, but
  914. * need to recheck which of these cases happened.
  915. *
  916. * 2) during session teardown, if the OPERATIONAL bit
  917. * was cleared due to the teardown but the pointer
  918. * hasn't been assigned NULL yet (or we loaded it
  919. * before it was assigned) -- in this case it may
  920. * now be NULL which means we should just let the
  921. * packet pass through because splicing the frames
  922. * back is already done.
  923. */
  924. tid_tx = rcu_dereference_protected_tid_tx(tx->sta, tid);
  925. if (!tid_tx) {
  926. /* do nothing, let packet pass through */
  927. } else if (test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) {
  928. info->flags |= IEEE80211_TX_CTL_AMPDU;
  929. } else {
  930. queued = true;
  931. info->control.vif = &tx->sdata->vif;
  932. info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
  933. __skb_queue_tail(&tid_tx->pending, skb);
  934. }
  935. spin_unlock(&tx->sta->lock);
  936. }
  937. return queued;
  938. }
  939. /*
  940. * initialises @tx
  941. */
  942. static ieee80211_tx_result
  943. ieee80211_tx_prepare(struct ieee80211_sub_if_data *sdata,
  944. struct ieee80211_tx_data *tx,
  945. struct sk_buff *skb)
  946. {
  947. struct ieee80211_local *local = sdata->local;
  948. struct ieee80211_hdr *hdr;
  949. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  950. int tid;
  951. u8 *qc;
  952. memset(tx, 0, sizeof(*tx));
  953. tx->skb = skb;
  954. tx->local = local;
  955. tx->sdata = sdata;
  956. tx->channel = local->hw.conf.channel;
  957. __skb_queue_head_init(&tx->skbs);
  958. /*
  959. * If this flag is set to true anywhere, and we get here,
  960. * we are doing the needed processing, so remove the flag
  961. * now.
  962. */
  963. info->flags &= ~IEEE80211_TX_INTFL_NEED_TXPROCESSING;
  964. hdr = (struct ieee80211_hdr *) skb->data;
  965. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
  966. tx->sta = rcu_dereference(sdata->u.vlan.sta);
  967. if (!tx->sta && sdata->dev->ieee80211_ptr->use_4addr)
  968. return TX_DROP;
  969. } else if (info->flags & IEEE80211_TX_CTL_INJECTED) {
  970. tx->sta = sta_info_get_bss(sdata, hdr->addr1);
  971. }
  972. if (!tx->sta)
  973. tx->sta = sta_info_get(sdata, hdr->addr1);
  974. if (tx->sta && ieee80211_is_data_qos(hdr->frame_control) &&
  975. !ieee80211_is_qos_nullfunc(hdr->frame_control) &&
  976. (local->hw.flags & IEEE80211_HW_AMPDU_AGGREGATION) &&
  977. !(local->hw.flags & IEEE80211_HW_TX_AMPDU_SETUP_IN_HW)) {
  978. struct tid_ampdu_tx *tid_tx;
  979. qc = ieee80211_get_qos_ctl(hdr);
  980. tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
  981. tid_tx = rcu_dereference(tx->sta->ampdu_mlme.tid_tx[tid]);
  982. if (tid_tx) {
  983. bool queued;
  984. queued = ieee80211_tx_prep_agg(tx, skb, info,
  985. tid_tx, tid);
  986. if (unlikely(queued))
  987. return TX_QUEUED;
  988. }
  989. }
  990. if (is_multicast_ether_addr(hdr->addr1)) {
  991. tx->flags &= ~IEEE80211_TX_UNICAST;
  992. info->flags |= IEEE80211_TX_CTL_NO_ACK;
  993. } else {
  994. tx->flags |= IEEE80211_TX_UNICAST;
  995. if (unlikely(local->wifi_wme_noack_test))
  996. info->flags |= IEEE80211_TX_CTL_NO_ACK;
  997. /*
  998. * Flags are initialized to 0. Hence, no need to
  999. * explicitly unset IEEE80211_TX_CTL_NO_ACK since
  1000. * it might already be set for injected frames.
  1001. */
  1002. }
  1003. if (!(info->flags & IEEE80211_TX_CTL_DONTFRAG)) {
  1004. if (!(tx->flags & IEEE80211_TX_UNICAST) ||
  1005. skb->len + FCS_LEN <= local->hw.wiphy->frag_threshold ||
  1006. info->flags & IEEE80211_TX_CTL_AMPDU)
  1007. info->flags |= IEEE80211_TX_CTL_DONTFRAG;
  1008. }
  1009. if (!tx->sta)
  1010. info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
  1011. else if (test_and_clear_sta_flag(tx->sta, WLAN_STA_CLEAR_PS_FILT))
  1012. info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
  1013. info->flags |= IEEE80211_TX_CTL_FIRST_FRAGMENT;
  1014. return TX_CONTINUE;
  1015. }
  1016. /*
  1017. * Returns false if the frame couldn't be transmitted but was queued instead.
  1018. */
  1019. static bool __ieee80211_tx(struct ieee80211_local *local,
  1020. struct sk_buff_head *skbs,
  1021. struct sta_info *sta, bool txpending)
  1022. {
  1023. struct sk_buff *skb, *tmp;
  1024. struct ieee80211_tx_info *info;
  1025. struct ieee80211_sub_if_data *sdata;
  1026. unsigned long flags;
  1027. int len;
  1028. skb_queue_walk_safe(skbs, skb, tmp) {
  1029. int q = skb_get_queue_mapping(skb);
  1030. __le16 fc;
  1031. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  1032. if (local->queue_stop_reasons[q] ||
  1033. (!txpending && !skb_queue_empty(&local->pending[q]))) {
  1034. /*
  1035. * Since queue is stopped, queue up frames for later
  1036. * transmission from the tx-pending tasklet when the
  1037. * queue is woken again.
  1038. */
  1039. if (txpending)
  1040. skb_queue_splice(skbs, &local->pending[q]);
  1041. else
  1042. skb_queue_splice_tail(skbs, &local->pending[q]);
  1043. spin_unlock_irqrestore(&local->queue_stop_reason_lock,
  1044. flags);
  1045. return false;
  1046. }
  1047. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  1048. info = IEEE80211_SKB_CB(skb);
  1049. len = skb->len;
  1050. sdata = vif_to_sdata(info->control.vif);
  1051. switch (sdata->vif.type) {
  1052. case NL80211_IFTYPE_MONITOR:
  1053. info->control.vif = NULL;
  1054. break;
  1055. case NL80211_IFTYPE_AP_VLAN:
  1056. info->control.vif = &container_of(sdata->bss,
  1057. struct ieee80211_sub_if_data, u.ap)->vif;
  1058. break;
  1059. default:
  1060. /* keep */
  1061. break;
  1062. }
  1063. if (sta && sta->uploaded)
  1064. info->control.sta = &sta->sta;
  1065. else
  1066. info->control.sta = NULL;
  1067. fc = ((struct ieee80211_hdr *)skb->data)->frame_control;
  1068. __skb_unlink(skb, skbs);
  1069. drv_tx(local, skb);
  1070. ieee80211_tpt_led_trig_tx(local, fc, len);
  1071. ieee80211_led_tx(local, 1);
  1072. }
  1073. WARN_ON(!skb_queue_empty(skbs));
  1074. return true;
  1075. }
  1076. /*
  1077. * Invoke TX handlers, return 0 on success and non-zero if the
  1078. * frame was dropped or queued.
  1079. */
  1080. static int invoke_tx_handlers(struct ieee80211_tx_data *tx)
  1081. {
  1082. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
  1083. ieee80211_tx_result res = TX_DROP;
  1084. #define CALL_TXH(txh) \
  1085. do { \
  1086. res = txh(tx); \
  1087. if (res != TX_CONTINUE) \
  1088. goto txh_done; \
  1089. } while (0)
  1090. CALL_TXH(ieee80211_tx_h_dynamic_ps);
  1091. CALL_TXH(ieee80211_tx_h_check_assoc);
  1092. CALL_TXH(ieee80211_tx_h_ps_buf);
  1093. CALL_TXH(ieee80211_tx_h_check_control_port_protocol);
  1094. CALL_TXH(ieee80211_tx_h_select_key);
  1095. if (!(tx->local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL))
  1096. CALL_TXH(ieee80211_tx_h_rate_ctrl);
  1097. if (unlikely(info->flags & IEEE80211_TX_INTFL_RETRANSMISSION))
  1098. goto txh_done;
  1099. CALL_TXH(ieee80211_tx_h_michael_mic_add);
  1100. CALL_TXH(ieee80211_tx_h_sequence);
  1101. CALL_TXH(ieee80211_tx_h_fragment);
  1102. /* handlers after fragment must be aware of tx info fragmentation! */
  1103. CALL_TXH(ieee80211_tx_h_stats);
  1104. CALL_TXH(ieee80211_tx_h_encrypt);
  1105. if (!(tx->local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL))
  1106. CALL_TXH(ieee80211_tx_h_calculate_duration);
  1107. #undef CALL_TXH
  1108. txh_done:
  1109. if (unlikely(res == TX_DROP)) {
  1110. I802_DEBUG_INC(tx->local->tx_handlers_drop);
  1111. if (tx->skb)
  1112. dev_kfree_skb(tx->skb);
  1113. else
  1114. __skb_queue_purge(&tx->skbs);
  1115. return -1;
  1116. } else if (unlikely(res == TX_QUEUED)) {
  1117. I802_DEBUG_INC(tx->local->tx_handlers_queued);
  1118. return -1;
  1119. }
  1120. return 0;
  1121. }
  1122. /*
  1123. * Returns false if the frame couldn't be transmitted but was queued instead.
  1124. */
  1125. static bool ieee80211_tx(struct ieee80211_sub_if_data *sdata,
  1126. struct sk_buff *skb, bool txpending)
  1127. {
  1128. struct ieee80211_local *local = sdata->local;
  1129. struct ieee80211_tx_data tx;
  1130. ieee80211_tx_result res_prepare;
  1131. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1132. bool result = true;
  1133. if (unlikely(skb->len < 10)) {
  1134. dev_kfree_skb(skb);
  1135. return true;
  1136. }
  1137. rcu_read_lock();
  1138. /* initialises tx */
  1139. res_prepare = ieee80211_tx_prepare(sdata, &tx, skb);
  1140. if (unlikely(res_prepare == TX_DROP)) {
  1141. dev_kfree_skb(skb);
  1142. goto out;
  1143. } else if (unlikely(res_prepare == TX_QUEUED)) {
  1144. goto out;
  1145. }
  1146. tx.channel = local->hw.conf.channel;
  1147. info->band = tx.channel->band;
  1148. if (!invoke_tx_handlers(&tx))
  1149. result = __ieee80211_tx(local, &tx.skbs, tx.sta, txpending);
  1150. out:
  1151. rcu_read_unlock();
  1152. return result;
  1153. }
  1154. /* device xmit handlers */
  1155. static int ieee80211_skb_resize(struct ieee80211_sub_if_data *sdata,
  1156. struct sk_buff *skb,
  1157. int head_need, bool may_encrypt)
  1158. {
  1159. struct ieee80211_local *local = sdata->local;
  1160. int tail_need = 0;
  1161. if (may_encrypt && sdata->crypto_tx_tailroom_needed_cnt) {
  1162. tail_need = IEEE80211_ENCRYPT_TAILROOM;
  1163. tail_need -= skb_tailroom(skb);
  1164. tail_need = max_t(int, tail_need, 0);
  1165. }
  1166. if (skb_cloned(skb))
  1167. I802_DEBUG_INC(local->tx_expand_skb_head_cloned);
  1168. else if (head_need || tail_need)
  1169. I802_DEBUG_INC(local->tx_expand_skb_head);
  1170. else
  1171. return 0;
  1172. if (pskb_expand_head(skb, head_need, tail_need, GFP_ATOMIC)) {
  1173. wiphy_debug(local->hw.wiphy,
  1174. "failed to reallocate TX buffer\n");
  1175. return -ENOMEM;
  1176. }
  1177. return 0;
  1178. }
  1179. void ieee80211_xmit(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb)
  1180. {
  1181. struct ieee80211_local *local = sdata->local;
  1182. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1183. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  1184. int headroom;
  1185. bool may_encrypt;
  1186. rcu_read_lock();
  1187. may_encrypt = !(info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT);
  1188. headroom = local->tx_headroom;
  1189. if (may_encrypt)
  1190. headroom += IEEE80211_ENCRYPT_HEADROOM;
  1191. headroom -= skb_headroom(skb);
  1192. headroom = max_t(int, 0, headroom);
  1193. if (ieee80211_skb_resize(sdata, skb, headroom, may_encrypt)) {
  1194. dev_kfree_skb(skb);
  1195. rcu_read_unlock();
  1196. return;
  1197. }
  1198. hdr = (struct ieee80211_hdr *) skb->data;
  1199. info->control.vif = &sdata->vif;
  1200. if (ieee80211_vif_is_mesh(&sdata->vif) &&
  1201. ieee80211_is_data(hdr->frame_control) &&
  1202. !is_multicast_ether_addr(hdr->addr1))
  1203. if (mesh_nexthop_lookup(skb, sdata)) {
  1204. /* skb queued: don't free */
  1205. rcu_read_unlock();
  1206. return;
  1207. }
  1208. ieee80211_set_qos_hdr(sdata, skb);
  1209. ieee80211_tx(sdata, skb, false);
  1210. rcu_read_unlock();
  1211. }
  1212. static bool ieee80211_parse_tx_radiotap(struct sk_buff *skb)
  1213. {
  1214. struct ieee80211_radiotap_iterator iterator;
  1215. struct ieee80211_radiotap_header *rthdr =
  1216. (struct ieee80211_radiotap_header *) skb->data;
  1217. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1218. int ret = ieee80211_radiotap_iterator_init(&iterator, rthdr, skb->len,
  1219. NULL);
  1220. u16 txflags;
  1221. info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
  1222. IEEE80211_TX_CTL_DONTFRAG;
  1223. /*
  1224. * for every radiotap entry that is present
  1225. * (ieee80211_radiotap_iterator_next returns -ENOENT when no more
  1226. * entries present, or -EINVAL on error)
  1227. */
  1228. while (!ret) {
  1229. ret = ieee80211_radiotap_iterator_next(&iterator);
  1230. if (ret)
  1231. continue;
  1232. /* see if this argument is something we can use */
  1233. switch (iterator.this_arg_index) {
  1234. /*
  1235. * You must take care when dereferencing iterator.this_arg
  1236. * for multibyte types... the pointer is not aligned. Use
  1237. * get_unaligned((type *)iterator.this_arg) to dereference
  1238. * iterator.this_arg for type "type" safely on all arches.
  1239. */
  1240. case IEEE80211_RADIOTAP_FLAGS:
  1241. if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FCS) {
  1242. /*
  1243. * this indicates that the skb we have been
  1244. * handed has the 32-bit FCS CRC at the end...
  1245. * we should react to that by snipping it off
  1246. * because it will be recomputed and added
  1247. * on transmission
  1248. */
  1249. if (skb->len < (iterator._max_length + FCS_LEN))
  1250. return false;
  1251. skb_trim(skb, skb->len - FCS_LEN);
  1252. }
  1253. if (*iterator.this_arg & IEEE80211_RADIOTAP_F_WEP)
  1254. info->flags &= ~IEEE80211_TX_INTFL_DONT_ENCRYPT;
  1255. if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FRAG)
  1256. info->flags &= ~IEEE80211_TX_CTL_DONTFRAG;
  1257. break;
  1258. case IEEE80211_RADIOTAP_TX_FLAGS:
  1259. txflags = get_unaligned_le16(iterator.this_arg);
  1260. if (txflags & IEEE80211_RADIOTAP_F_TX_NOACK)
  1261. info->flags |= IEEE80211_TX_CTL_NO_ACK;
  1262. break;
  1263. /*
  1264. * Please update the file
  1265. * Documentation/networking/mac80211-injection.txt
  1266. * when parsing new fields here.
  1267. */
  1268. default:
  1269. break;
  1270. }
  1271. }
  1272. if (ret != -ENOENT) /* ie, if we didn't simply run out of fields */
  1273. return false;
  1274. /*
  1275. * remove the radiotap header
  1276. * iterator->_max_length was sanity-checked against
  1277. * skb->len by iterator init
  1278. */
  1279. skb_pull(skb, iterator._max_length);
  1280. return true;
  1281. }
  1282. netdev_tx_t ieee80211_monitor_start_xmit(struct sk_buff *skb,
  1283. struct net_device *dev)
  1284. {
  1285. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  1286. struct ieee80211_channel *chan = local->hw.conf.channel;
  1287. struct ieee80211_radiotap_header *prthdr =
  1288. (struct ieee80211_radiotap_header *)skb->data;
  1289. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1290. struct ieee80211_hdr *hdr;
  1291. struct ieee80211_sub_if_data *tmp_sdata, *sdata;
  1292. u16 len_rthdr;
  1293. int hdrlen;
  1294. /*
  1295. * Frame injection is not allowed if beaconing is not allowed
  1296. * or if we need radar detection. Beaconing is usually not allowed when
  1297. * the mode or operation (Adhoc, AP, Mesh) does not support DFS.
  1298. * Passive scan is also used in world regulatory domains where
  1299. * your country is not known and as such it should be treated as
  1300. * NO TX unless the channel is explicitly allowed in which case
  1301. * your current regulatory domain would not have the passive scan
  1302. * flag.
  1303. *
  1304. * Since AP mode uses monitor interfaces to inject/TX management
  1305. * frames we can make AP mode the exception to this rule once it
  1306. * supports radar detection as its implementation can deal with
  1307. * radar detection by itself. We can do that later by adding a
  1308. * monitor flag interfaces used for AP support.
  1309. */
  1310. if ((chan->flags & (IEEE80211_CHAN_NO_IBSS | IEEE80211_CHAN_RADAR |
  1311. IEEE80211_CHAN_PASSIVE_SCAN)))
  1312. goto fail;
  1313. /* check for not even having the fixed radiotap header part */
  1314. if (unlikely(skb->len < sizeof(struct ieee80211_radiotap_header)))
  1315. goto fail; /* too short to be possibly valid */
  1316. /* is it a header version we can trust to find length from? */
  1317. if (unlikely(prthdr->it_version))
  1318. goto fail; /* only version 0 is supported */
  1319. /* then there must be a radiotap header with a length we can use */
  1320. len_rthdr = ieee80211_get_radiotap_len(skb->data);
  1321. /* does the skb contain enough to deliver on the alleged length? */
  1322. if (unlikely(skb->len < len_rthdr))
  1323. goto fail; /* skb too short for claimed rt header extent */
  1324. /*
  1325. * fix up the pointers accounting for the radiotap
  1326. * header still being in there. We are being given
  1327. * a precooked IEEE80211 header so no need for
  1328. * normal processing
  1329. */
  1330. skb_set_mac_header(skb, len_rthdr);
  1331. /*
  1332. * these are just fixed to the end of the rt area since we
  1333. * don't have any better information and at this point, nobody cares
  1334. */
  1335. skb_set_network_header(skb, len_rthdr);
  1336. skb_set_transport_header(skb, len_rthdr);
  1337. if (skb->len < len_rthdr + 2)
  1338. goto fail;
  1339. hdr = (struct ieee80211_hdr *)(skb->data + len_rthdr);
  1340. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  1341. if (skb->len < len_rthdr + hdrlen)
  1342. goto fail;
  1343. /*
  1344. * Initialize skb->protocol if the injected frame is a data frame
  1345. * carrying a rfc1042 header
  1346. */
  1347. if (ieee80211_is_data(hdr->frame_control) &&
  1348. skb->len >= len_rthdr + hdrlen + sizeof(rfc1042_header) + 2) {
  1349. u8 *payload = (u8 *)hdr + hdrlen;
  1350. if (compare_ether_addr(payload, rfc1042_header) == 0)
  1351. skb->protocol = cpu_to_be16((payload[6] << 8) |
  1352. payload[7]);
  1353. }
  1354. memset(info, 0, sizeof(*info));
  1355. info->flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
  1356. IEEE80211_TX_CTL_INJECTED;
  1357. /* process and remove the injection radiotap header */
  1358. if (!ieee80211_parse_tx_radiotap(skb))
  1359. goto fail;
  1360. rcu_read_lock();
  1361. /*
  1362. * We process outgoing injected frames that have a local address
  1363. * we handle as though they are non-injected frames.
  1364. * This code here isn't entirely correct, the local MAC address
  1365. * isn't always enough to find the interface to use; for proper
  1366. * VLAN/WDS support we will need a different mechanism (which
  1367. * likely isn't going to be monitor interfaces).
  1368. */
  1369. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  1370. list_for_each_entry_rcu(tmp_sdata, &local->interfaces, list) {
  1371. if (!ieee80211_sdata_running(tmp_sdata))
  1372. continue;
  1373. if (tmp_sdata->vif.type == NL80211_IFTYPE_MONITOR ||
  1374. tmp_sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
  1375. tmp_sdata->vif.type == NL80211_IFTYPE_WDS)
  1376. continue;
  1377. if (compare_ether_addr(tmp_sdata->vif.addr, hdr->addr2) == 0) {
  1378. sdata = tmp_sdata;
  1379. break;
  1380. }
  1381. }
  1382. ieee80211_xmit(sdata, skb);
  1383. rcu_read_unlock();
  1384. return NETDEV_TX_OK;
  1385. fail:
  1386. dev_kfree_skb(skb);
  1387. return NETDEV_TX_OK; /* meaning, we dealt with the skb */
  1388. }
  1389. /**
  1390. * ieee80211_subif_start_xmit - netif start_xmit function for Ethernet-type
  1391. * subinterfaces (wlan#, WDS, and VLAN interfaces)
  1392. * @skb: packet to be sent
  1393. * @dev: incoming interface
  1394. *
  1395. * Returns: 0 on success (and frees skb in this case) or 1 on failure (skb will
  1396. * not be freed, and caller is responsible for either retrying later or freeing
  1397. * skb).
  1398. *
  1399. * This function takes in an Ethernet header and encapsulates it with suitable
  1400. * IEEE 802.11 header based on which interface the packet is coming in. The
  1401. * encapsulated packet will then be passed to master interface, wlan#.11, for
  1402. * transmission (through low-level driver).
  1403. */
  1404. netdev_tx_t ieee80211_subif_start_xmit(struct sk_buff *skb,
  1405. struct net_device *dev)
  1406. {
  1407. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  1408. struct ieee80211_local *local = sdata->local;
  1409. struct ieee80211_tx_info *info;
  1410. int ret = NETDEV_TX_BUSY, head_need;
  1411. u16 ethertype, hdrlen, meshhdrlen = 0;
  1412. __le16 fc;
  1413. struct ieee80211_hdr hdr;
  1414. struct ieee80211s_hdr mesh_hdr __maybe_unused;
  1415. struct mesh_path __maybe_unused *mppath = NULL;
  1416. const u8 *encaps_data;
  1417. int encaps_len, skip_header_bytes;
  1418. int nh_pos, h_pos;
  1419. struct sta_info *sta = NULL;
  1420. bool wme_sta = false, authorized = false, tdls_auth = false;
  1421. bool tdls_direct = false;
  1422. bool multicast;
  1423. u32 info_flags = 0;
  1424. u16 info_id = 0;
  1425. if (unlikely(skb->len < ETH_HLEN)) {
  1426. ret = NETDEV_TX_OK;
  1427. goto fail;
  1428. }
  1429. /* convert Ethernet header to proper 802.11 header (based on
  1430. * operation mode) */
  1431. ethertype = (skb->data[12] << 8) | skb->data[13];
  1432. fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
  1433. switch (sdata->vif.type) {
  1434. case NL80211_IFTYPE_AP_VLAN:
  1435. rcu_read_lock();
  1436. sta = rcu_dereference(sdata->u.vlan.sta);
  1437. if (sta) {
  1438. fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
  1439. /* RA TA DA SA */
  1440. memcpy(hdr.addr1, sta->sta.addr, ETH_ALEN);
  1441. memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
  1442. memcpy(hdr.addr3, skb->data, ETH_ALEN);
  1443. memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
  1444. hdrlen = 30;
  1445. authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
  1446. wme_sta = test_sta_flag(sta, WLAN_STA_WME);
  1447. }
  1448. rcu_read_unlock();
  1449. if (sta)
  1450. break;
  1451. /* fall through */
  1452. case NL80211_IFTYPE_AP:
  1453. fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
  1454. /* DA BSSID SA */
  1455. memcpy(hdr.addr1, skb->data, ETH_ALEN);
  1456. memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
  1457. memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
  1458. hdrlen = 24;
  1459. break;
  1460. case NL80211_IFTYPE_WDS:
  1461. fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
  1462. /* RA TA DA SA */
  1463. memcpy(hdr.addr1, sdata->u.wds.remote_addr, ETH_ALEN);
  1464. memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
  1465. memcpy(hdr.addr3, skb->data, ETH_ALEN);
  1466. memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
  1467. hdrlen = 30;
  1468. break;
  1469. #ifdef CONFIG_MAC80211_MESH
  1470. case NL80211_IFTYPE_MESH_POINT:
  1471. if (!sdata->u.mesh.mshcfg.dot11MeshTTL) {
  1472. /* Do not send frames with mesh_ttl == 0 */
  1473. sdata->u.mesh.mshstats.dropped_frames_ttl++;
  1474. ret = NETDEV_TX_OK;
  1475. goto fail;
  1476. }
  1477. rcu_read_lock();
  1478. if (!is_multicast_ether_addr(skb->data))
  1479. mppath = mpp_path_lookup(skb->data, sdata);
  1480. /*
  1481. * Use address extension if it is a packet from
  1482. * another interface or if we know the destination
  1483. * is being proxied by a portal (i.e. portal address
  1484. * differs from proxied address)
  1485. */
  1486. if (compare_ether_addr(sdata->vif.addr,
  1487. skb->data + ETH_ALEN) == 0 &&
  1488. !(mppath && compare_ether_addr(mppath->mpp, skb->data))) {
  1489. hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc,
  1490. skb->data, skb->data + ETH_ALEN);
  1491. rcu_read_unlock();
  1492. meshhdrlen = ieee80211_new_mesh_header(&mesh_hdr,
  1493. sdata, NULL, NULL);
  1494. } else {
  1495. int is_mesh_mcast = 1;
  1496. const u8 *mesh_da;
  1497. if (is_multicast_ether_addr(skb->data))
  1498. /* DA TA mSA AE:SA */
  1499. mesh_da = skb->data;
  1500. else {
  1501. static const u8 bcast[ETH_ALEN] =
  1502. { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  1503. if (mppath) {
  1504. /* RA TA mDA mSA AE:DA SA */
  1505. mesh_da = mppath->mpp;
  1506. is_mesh_mcast = 0;
  1507. } else {
  1508. /* DA TA mSA AE:SA */
  1509. mesh_da = bcast;
  1510. }
  1511. }
  1512. hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc,
  1513. mesh_da, sdata->vif.addr);
  1514. rcu_read_unlock();
  1515. if (is_mesh_mcast)
  1516. meshhdrlen =
  1517. ieee80211_new_mesh_header(&mesh_hdr,
  1518. sdata,
  1519. skb->data + ETH_ALEN,
  1520. NULL);
  1521. else
  1522. meshhdrlen =
  1523. ieee80211_new_mesh_header(&mesh_hdr,
  1524. sdata,
  1525. skb->data,
  1526. skb->data + ETH_ALEN);
  1527. }
  1528. break;
  1529. #endif
  1530. case NL80211_IFTYPE_STATION:
  1531. if (sdata->wdev.wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS) {
  1532. bool tdls_peer = false;
  1533. rcu_read_lock();
  1534. sta = sta_info_get(sdata, skb->data);
  1535. if (sta) {
  1536. authorized = test_sta_flag(sta,
  1537. WLAN_STA_AUTHORIZED);
  1538. wme_sta = test_sta_flag(sta, WLAN_STA_WME);
  1539. tdls_peer = test_sta_flag(sta,
  1540. WLAN_STA_TDLS_PEER);
  1541. tdls_auth = test_sta_flag(sta,
  1542. WLAN_STA_TDLS_PEER_AUTH);
  1543. }
  1544. rcu_read_unlock();
  1545. /*
  1546. * If the TDLS link is enabled, send everything
  1547. * directly. Otherwise, allow TDLS setup frames
  1548. * to be transmitted indirectly.
  1549. */
  1550. tdls_direct = tdls_peer && (tdls_auth ||
  1551. !(ethertype == ETH_P_TDLS && skb->len > 14 &&
  1552. skb->data[14] == WLAN_TDLS_SNAP_RFTYPE));
  1553. }
  1554. if (tdls_direct) {
  1555. /* link during setup - throw out frames to peer */
  1556. if (!tdls_auth) {
  1557. ret = NETDEV_TX_OK;
  1558. goto fail;
  1559. }
  1560. /* DA SA BSSID */
  1561. memcpy(hdr.addr1, skb->data, ETH_ALEN);
  1562. memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
  1563. memcpy(hdr.addr3, sdata->u.mgd.bssid, ETH_ALEN);
  1564. hdrlen = 24;
  1565. } else if (sdata->u.mgd.use_4addr &&
  1566. cpu_to_be16(ethertype) != sdata->control_port_protocol) {
  1567. fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
  1568. IEEE80211_FCTL_TODS);
  1569. /* RA TA DA SA */
  1570. memcpy(hdr.addr1, sdata->u.mgd.bssid, ETH_ALEN);
  1571. memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
  1572. memcpy(hdr.addr3, skb->data, ETH_ALEN);
  1573. memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
  1574. hdrlen = 30;
  1575. } else {
  1576. fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
  1577. /* BSSID SA DA */
  1578. memcpy(hdr.addr1, sdata->u.mgd.bssid, ETH_ALEN);
  1579. memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
  1580. memcpy(hdr.addr3, skb->data, ETH_ALEN);
  1581. hdrlen = 24;
  1582. }
  1583. break;
  1584. case NL80211_IFTYPE_ADHOC:
  1585. /* DA SA BSSID */
  1586. memcpy(hdr.addr1, skb->data, ETH_ALEN);
  1587. memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
  1588. memcpy(hdr.addr3, sdata->u.ibss.bssid, ETH_ALEN);
  1589. hdrlen = 24;
  1590. break;
  1591. default:
  1592. ret = NETDEV_TX_OK;
  1593. goto fail;
  1594. }
  1595. /*
  1596. * There's no need to try to look up the destination
  1597. * if it is a multicast address (which can only happen
  1598. * in AP mode)
  1599. */
  1600. multicast = is_multicast_ether_addr(hdr.addr1);
  1601. if (!multicast) {
  1602. rcu_read_lock();
  1603. sta = sta_info_get(sdata, hdr.addr1);
  1604. if (sta) {
  1605. authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
  1606. wme_sta = test_sta_flag(sta, WLAN_STA_WME);
  1607. }
  1608. rcu_read_unlock();
  1609. }
  1610. /* For mesh, the use of the QoS header is mandatory */
  1611. if (ieee80211_vif_is_mesh(&sdata->vif))
  1612. wme_sta = true;
  1613. /* receiver and we are QoS enabled, use a QoS type frame */
  1614. if (wme_sta && local->hw.queues >= 4) {
  1615. fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
  1616. hdrlen += 2;
  1617. }
  1618. /*
  1619. * Drop unicast frames to unauthorised stations unless they are
  1620. * EAPOL frames from the local station.
  1621. */
  1622. if (unlikely(!ieee80211_vif_is_mesh(&sdata->vif) &&
  1623. !is_multicast_ether_addr(hdr.addr1) && !authorized &&
  1624. (cpu_to_be16(ethertype) != sdata->control_port_protocol ||
  1625. compare_ether_addr(sdata->vif.addr, skb->data + ETH_ALEN)))) {
  1626. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  1627. if (net_ratelimit())
  1628. printk(KERN_DEBUG "%s: dropped frame to %pM"
  1629. " (unauthorized port)\n", dev->name,
  1630. hdr.addr1);
  1631. #endif
  1632. I802_DEBUG_INC(local->tx_handlers_drop_unauth_port);
  1633. ret = NETDEV_TX_OK;
  1634. goto fail;
  1635. }
  1636. if (unlikely(!multicast && skb->sk &&
  1637. skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS)) {
  1638. struct sk_buff *orig_skb = skb;
  1639. skb = skb_clone(skb, GFP_ATOMIC);
  1640. if (skb) {
  1641. unsigned long flags;
  1642. int id, r;
  1643. spin_lock_irqsave(&local->ack_status_lock, flags);
  1644. r = idr_get_new_above(&local->ack_status_frames,
  1645. orig_skb, 1, &id);
  1646. if (r == -EAGAIN) {
  1647. idr_pre_get(&local->ack_status_frames,
  1648. GFP_ATOMIC);
  1649. r = idr_get_new_above(&local->ack_status_frames,
  1650. orig_skb, 1, &id);
  1651. }
  1652. if (WARN_ON(!id) || id > 0xffff) {
  1653. idr_remove(&local->ack_status_frames, id);
  1654. r = -ERANGE;
  1655. }
  1656. spin_unlock_irqrestore(&local->ack_status_lock, flags);
  1657. if (!r) {
  1658. info_id = id;
  1659. info_flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
  1660. } else if (skb_shared(skb)) {
  1661. kfree_skb(orig_skb);
  1662. } else {
  1663. kfree_skb(skb);
  1664. skb = orig_skb;
  1665. }
  1666. } else {
  1667. /* couldn't clone -- lose tx status ... */
  1668. skb = orig_skb;
  1669. }
  1670. }
  1671. /*
  1672. * If the skb is shared we need to obtain our own copy.
  1673. */
  1674. if (skb_shared(skb)) {
  1675. struct sk_buff *tmp_skb = skb;
  1676. /* can't happen -- skb is a clone if info_id != 0 */
  1677. WARN_ON(info_id);
  1678. skb = skb_clone(skb, GFP_ATOMIC);
  1679. kfree_skb(tmp_skb);
  1680. if (!skb) {
  1681. ret = NETDEV_TX_OK;
  1682. goto fail;
  1683. }
  1684. }
  1685. hdr.frame_control = fc;
  1686. hdr.duration_id = 0;
  1687. hdr.seq_ctrl = 0;
  1688. skip_header_bytes = ETH_HLEN;
  1689. if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
  1690. encaps_data = bridge_tunnel_header;
  1691. encaps_len = sizeof(bridge_tunnel_header);
  1692. skip_header_bytes -= 2;
  1693. } else if (ethertype >= 0x600) {
  1694. encaps_data = rfc1042_header;
  1695. encaps_len = sizeof(rfc1042_header);
  1696. skip_header_bytes -= 2;
  1697. } else {
  1698. encaps_data = NULL;
  1699. encaps_len = 0;
  1700. }
  1701. nh_pos = skb_network_header(skb) - skb->data;
  1702. h_pos = skb_transport_header(skb) - skb->data;
  1703. skb_pull(skb, skip_header_bytes);
  1704. nh_pos -= skip_header_bytes;
  1705. h_pos -= skip_header_bytes;
  1706. head_need = hdrlen + encaps_len + meshhdrlen - skb_headroom(skb);
  1707. /*
  1708. * So we need to modify the skb header and hence need a copy of
  1709. * that. The head_need variable above doesn't, so far, include
  1710. * the needed header space that we don't need right away. If we
  1711. * can, then we don't reallocate right now but only after the
  1712. * frame arrives at the master device (if it does...)
  1713. *
  1714. * If we cannot, however, then we will reallocate to include all
  1715. * the ever needed space. Also, if we need to reallocate it anyway,
  1716. * make it big enough for everything we may ever need.
  1717. */
  1718. if (head_need > 0 || skb_cloned(skb)) {
  1719. head_need += IEEE80211_ENCRYPT_HEADROOM;
  1720. head_need += local->tx_headroom;
  1721. head_need = max_t(int, 0, head_need);
  1722. if (ieee80211_skb_resize(sdata, skb, head_need, true))
  1723. goto fail;
  1724. }
  1725. if (encaps_data) {
  1726. memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
  1727. nh_pos += encaps_len;
  1728. h_pos += encaps_len;
  1729. }
  1730. #ifdef CONFIG_MAC80211_MESH
  1731. if (meshhdrlen > 0) {
  1732. memcpy(skb_push(skb, meshhdrlen), &mesh_hdr, meshhdrlen);
  1733. nh_pos += meshhdrlen;
  1734. h_pos += meshhdrlen;
  1735. }
  1736. #endif
  1737. if (ieee80211_is_data_qos(fc)) {
  1738. __le16 *qos_control;
  1739. qos_control = (__le16*) skb_push(skb, 2);
  1740. memcpy(skb_push(skb, hdrlen - 2), &hdr, hdrlen - 2);
  1741. /*
  1742. * Maybe we could actually set some fields here, for now just
  1743. * initialise to zero to indicate no special operation.
  1744. */
  1745. *qos_control = 0;
  1746. } else
  1747. memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
  1748. nh_pos += hdrlen;
  1749. h_pos += hdrlen;
  1750. dev->stats.tx_packets++;
  1751. dev->stats.tx_bytes += skb->len;
  1752. /* Update skb pointers to various headers since this modified frame
  1753. * is going to go through Linux networking code that may potentially
  1754. * need things like pointer to IP header. */
  1755. skb_set_mac_header(skb, 0);
  1756. skb_set_network_header(skb, nh_pos);
  1757. skb_set_transport_header(skb, h_pos);
  1758. info = IEEE80211_SKB_CB(skb);
  1759. memset(info, 0, sizeof(*info));
  1760. dev->trans_start = jiffies;
  1761. info->flags = info_flags;
  1762. info->ack_frame_id = info_id;
  1763. ieee80211_xmit(sdata, skb);
  1764. return NETDEV_TX_OK;
  1765. fail:
  1766. if (ret == NETDEV_TX_OK)
  1767. dev_kfree_skb(skb);
  1768. return ret;
  1769. }
  1770. /*
  1771. * ieee80211_clear_tx_pending may not be called in a context where
  1772. * it is possible that it packets could come in again.
  1773. */
  1774. void ieee80211_clear_tx_pending(struct ieee80211_local *local)
  1775. {
  1776. int i;
  1777. for (i = 0; i < local->hw.queues; i++)
  1778. skb_queue_purge(&local->pending[i]);
  1779. }
  1780. /*
  1781. * Returns false if the frame couldn't be transmitted but was queued instead,
  1782. * which in this case means re-queued -- take as an indication to stop sending
  1783. * more pending frames.
  1784. */
  1785. static bool ieee80211_tx_pending_skb(struct ieee80211_local *local,
  1786. struct sk_buff *skb)
  1787. {
  1788. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1789. struct ieee80211_sub_if_data *sdata;
  1790. struct sta_info *sta;
  1791. struct ieee80211_hdr *hdr;
  1792. bool result;
  1793. sdata = vif_to_sdata(info->control.vif);
  1794. if (info->flags & IEEE80211_TX_INTFL_NEED_TXPROCESSING) {
  1795. result = ieee80211_tx(sdata, skb, true);
  1796. } else {
  1797. struct sk_buff_head skbs;
  1798. __skb_queue_head_init(&skbs);
  1799. __skb_queue_tail(&skbs, skb);
  1800. hdr = (struct ieee80211_hdr *)skb->data;
  1801. sta = sta_info_get(sdata, hdr->addr1);
  1802. result = __ieee80211_tx(local, &skbs, sta, true);
  1803. }
  1804. return result;
  1805. }
  1806. /*
  1807. * Transmit all pending packets. Called from tasklet.
  1808. */
  1809. void ieee80211_tx_pending(unsigned long data)
  1810. {
  1811. struct ieee80211_local *local = (struct ieee80211_local *)data;
  1812. struct ieee80211_sub_if_data *sdata;
  1813. unsigned long flags;
  1814. int i;
  1815. bool txok;
  1816. rcu_read_lock();
  1817. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  1818. for (i = 0; i < local->hw.queues; i++) {
  1819. /*
  1820. * If queue is stopped by something other than due to pending
  1821. * frames, or we have no pending frames, proceed to next queue.
  1822. */
  1823. if (local->queue_stop_reasons[i] ||
  1824. skb_queue_empty(&local->pending[i]))
  1825. continue;
  1826. while (!skb_queue_empty(&local->pending[i])) {
  1827. struct sk_buff *skb = __skb_dequeue(&local->pending[i]);
  1828. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1829. if (WARN_ON(!info->control.vif)) {
  1830. kfree_skb(skb);
  1831. continue;
  1832. }
  1833. spin_unlock_irqrestore(&local->queue_stop_reason_lock,
  1834. flags);
  1835. txok = ieee80211_tx_pending_skb(local, skb);
  1836. spin_lock_irqsave(&local->queue_stop_reason_lock,
  1837. flags);
  1838. if (!txok)
  1839. break;
  1840. }
  1841. if (skb_queue_empty(&local->pending[i]))
  1842. list_for_each_entry_rcu(sdata, &local->interfaces, list)
  1843. netif_wake_subqueue(sdata->dev, i);
  1844. }
  1845. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  1846. rcu_read_unlock();
  1847. }
  1848. /* functions for drivers to get certain frames */
  1849. static void ieee80211_beacon_add_tim(struct ieee80211_if_ap *bss,
  1850. struct sk_buff *skb,
  1851. struct beacon_data *beacon)
  1852. {
  1853. u8 *pos, *tim;
  1854. int aid0 = 0;
  1855. int i, have_bits = 0, n1, n2;
  1856. /* Generate bitmap for TIM only if there are any STAs in power save
  1857. * mode. */
  1858. if (atomic_read(&bss->num_sta_ps) > 0)
  1859. /* in the hope that this is faster than
  1860. * checking byte-for-byte */
  1861. have_bits = !bitmap_empty((unsigned long*)bss->tim,
  1862. IEEE80211_MAX_AID+1);
  1863. if (bss->dtim_count == 0)
  1864. bss->dtim_count = beacon->dtim_period - 1;
  1865. else
  1866. bss->dtim_count--;
  1867. tim = pos = (u8 *) skb_put(skb, 6);
  1868. *pos++ = WLAN_EID_TIM;
  1869. *pos++ = 4;
  1870. *pos++ = bss->dtim_count;
  1871. *pos++ = beacon->dtim_period;
  1872. if (bss->dtim_count == 0 && !skb_queue_empty(&bss->ps_bc_buf))
  1873. aid0 = 1;
  1874. bss->dtim_bc_mc = aid0 == 1;
  1875. if (have_bits) {
  1876. /* Find largest even number N1 so that bits numbered 1 through
  1877. * (N1 x 8) - 1 in the bitmap are 0 and number N2 so that bits
  1878. * (N2 + 1) x 8 through 2007 are 0. */
  1879. n1 = 0;
  1880. for (i = 0; i < IEEE80211_MAX_TIM_LEN; i++) {
  1881. if (bss->tim[i]) {
  1882. n1 = i & 0xfe;
  1883. break;
  1884. }
  1885. }
  1886. n2 = n1;
  1887. for (i = IEEE80211_MAX_TIM_LEN - 1; i >= n1; i--) {
  1888. if (bss->tim[i]) {
  1889. n2 = i;
  1890. break;
  1891. }
  1892. }
  1893. /* Bitmap control */
  1894. *pos++ = n1 | aid0;
  1895. /* Part Virt Bitmap */
  1896. memcpy(pos, bss->tim + n1, n2 - n1 + 1);
  1897. tim[1] = n2 - n1 + 4;
  1898. skb_put(skb, n2 - n1);
  1899. } else {
  1900. *pos++ = aid0; /* Bitmap control */
  1901. *pos++ = 0; /* Part Virt Bitmap */
  1902. }
  1903. }
  1904. struct sk_buff *ieee80211_beacon_get_tim(struct ieee80211_hw *hw,
  1905. struct ieee80211_vif *vif,
  1906. u16 *tim_offset, u16 *tim_length)
  1907. {
  1908. struct ieee80211_local *local = hw_to_local(hw);
  1909. struct sk_buff *skb = NULL;
  1910. struct ieee80211_tx_info *info;
  1911. struct ieee80211_sub_if_data *sdata = NULL;
  1912. struct ieee80211_if_ap *ap = NULL;
  1913. struct beacon_data *beacon;
  1914. struct ieee80211_supported_band *sband;
  1915. enum ieee80211_band band = local->hw.conf.channel->band;
  1916. struct ieee80211_tx_rate_control txrc;
  1917. sband = local->hw.wiphy->bands[band];
  1918. rcu_read_lock();
  1919. sdata = vif_to_sdata(vif);
  1920. if (!ieee80211_sdata_running(sdata))
  1921. goto out;
  1922. if (tim_offset)
  1923. *tim_offset = 0;
  1924. if (tim_length)
  1925. *tim_length = 0;
  1926. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  1927. ap = &sdata->u.ap;
  1928. beacon = rcu_dereference(ap->beacon);
  1929. if (beacon) {
  1930. /*
  1931. * headroom, head length,
  1932. * tail length and maximum TIM length
  1933. */
  1934. skb = dev_alloc_skb(local->tx_headroom +
  1935. beacon->head_len +
  1936. beacon->tail_len + 256);
  1937. if (!skb)
  1938. goto out;
  1939. skb_reserve(skb, local->tx_headroom);
  1940. memcpy(skb_put(skb, beacon->head_len), beacon->head,
  1941. beacon->head_len);
  1942. /*
  1943. * Not very nice, but we want to allow the driver to call
  1944. * ieee80211_beacon_get() as a response to the set_tim()
  1945. * callback. That, however, is already invoked under the
  1946. * sta_lock to guarantee consistent and race-free update
  1947. * of the tim bitmap in mac80211 and the driver.
  1948. */
  1949. if (local->tim_in_locked_section) {
  1950. ieee80211_beacon_add_tim(ap, skb, beacon);
  1951. } else {
  1952. unsigned long flags;
  1953. spin_lock_irqsave(&local->sta_lock, flags);
  1954. ieee80211_beacon_add_tim(ap, skb, beacon);
  1955. spin_unlock_irqrestore(&local->sta_lock, flags);
  1956. }
  1957. if (tim_offset)
  1958. *tim_offset = beacon->head_len;
  1959. if (tim_length)
  1960. *tim_length = skb->len - beacon->head_len;
  1961. if (beacon->tail)
  1962. memcpy(skb_put(skb, beacon->tail_len),
  1963. beacon->tail, beacon->tail_len);
  1964. } else
  1965. goto out;
  1966. } else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
  1967. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  1968. struct ieee80211_hdr *hdr;
  1969. struct sk_buff *presp = rcu_dereference(ifibss->presp);
  1970. if (!presp)
  1971. goto out;
  1972. skb = skb_copy(presp, GFP_ATOMIC);
  1973. if (!skb)
  1974. goto out;
  1975. hdr = (struct ieee80211_hdr *) skb->data;
  1976. hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  1977. IEEE80211_STYPE_BEACON);
  1978. } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
  1979. struct ieee80211_mgmt *mgmt;
  1980. u8 *pos;
  1981. int hdr_len = offsetof(struct ieee80211_mgmt, u.beacon) +
  1982. sizeof(mgmt->u.beacon);
  1983. #ifdef CONFIG_MAC80211_MESH
  1984. if (!sdata->u.mesh.mesh_id_len)
  1985. goto out;
  1986. #endif
  1987. skb = dev_alloc_skb(local->tx_headroom +
  1988. hdr_len +
  1989. 2 + /* NULL SSID */
  1990. 2 + 8 + /* supported rates */
  1991. 2 + 3 + /* DS params */
  1992. 2 + (IEEE80211_MAX_SUPP_RATES - 8) +
  1993. 2 + sizeof(struct ieee80211_ht_cap) +
  1994. 2 + sizeof(struct ieee80211_ht_info) +
  1995. 2 + sdata->u.mesh.mesh_id_len +
  1996. 2 + sizeof(struct ieee80211_meshconf_ie) +
  1997. sdata->u.mesh.ie_len);
  1998. if (!skb)
  1999. goto out;
  2000. skb_reserve(skb, local->hw.extra_tx_headroom);
  2001. mgmt = (struct ieee80211_mgmt *) skb_put(skb, hdr_len);
  2002. memset(mgmt, 0, hdr_len);
  2003. mgmt->frame_control =
  2004. cpu_to_le16(IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_BEACON);
  2005. memset(mgmt->da, 0xff, ETH_ALEN);
  2006. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  2007. memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
  2008. mgmt->u.beacon.beacon_int =
  2009. cpu_to_le16(sdata->vif.bss_conf.beacon_int);
  2010. mgmt->u.beacon.capab_info |= cpu_to_le16(
  2011. sdata->u.mesh.security ? WLAN_CAPABILITY_PRIVACY : 0);
  2012. pos = skb_put(skb, 2);
  2013. *pos++ = WLAN_EID_SSID;
  2014. *pos++ = 0x0;
  2015. if (ieee80211_add_srates_ie(&sdata->vif, skb) ||
  2016. mesh_add_ds_params_ie(skb, sdata) ||
  2017. ieee80211_add_ext_srates_ie(&sdata->vif, skb) ||
  2018. mesh_add_rsn_ie(skb, sdata) ||
  2019. mesh_add_ht_cap_ie(skb, sdata) ||
  2020. mesh_add_ht_info_ie(skb, sdata) ||
  2021. mesh_add_meshid_ie(skb, sdata) ||
  2022. mesh_add_meshconf_ie(skb, sdata) ||
  2023. mesh_add_vendor_ies(skb, sdata)) {
  2024. pr_err("o11s: couldn't add ies!\n");
  2025. goto out;
  2026. }
  2027. } else {
  2028. WARN_ON(1);
  2029. goto out;
  2030. }
  2031. info = IEEE80211_SKB_CB(skb);
  2032. info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  2033. info->flags |= IEEE80211_TX_CTL_NO_ACK;
  2034. info->band = band;
  2035. memset(&txrc, 0, sizeof(txrc));
  2036. txrc.hw = hw;
  2037. txrc.sband = sband;
  2038. txrc.bss_conf = &sdata->vif.bss_conf;
  2039. txrc.skb = skb;
  2040. txrc.reported_rate.idx = -1;
  2041. txrc.rate_idx_mask = sdata->rc_rateidx_mask[band];
  2042. if (txrc.rate_idx_mask == (1 << sband->n_bitrates) - 1)
  2043. txrc.max_rate_idx = -1;
  2044. else
  2045. txrc.max_rate_idx = fls(txrc.rate_idx_mask) - 1;
  2046. txrc.bss = true;
  2047. rate_control_get_rate(sdata, NULL, &txrc);
  2048. info->control.vif = vif;
  2049. info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT |
  2050. IEEE80211_TX_CTL_ASSIGN_SEQ |
  2051. IEEE80211_TX_CTL_FIRST_FRAGMENT;
  2052. out:
  2053. rcu_read_unlock();
  2054. return skb;
  2055. }
  2056. EXPORT_SYMBOL(ieee80211_beacon_get_tim);
  2057. struct sk_buff *ieee80211_proberesp_get(struct ieee80211_hw *hw,
  2058. struct ieee80211_vif *vif)
  2059. {
  2060. struct ieee80211_if_ap *ap = NULL;
  2061. struct sk_buff *presp = NULL, *skb = NULL;
  2062. struct ieee80211_hdr *hdr;
  2063. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  2064. if (sdata->vif.type != NL80211_IFTYPE_AP)
  2065. return NULL;
  2066. rcu_read_lock();
  2067. ap = &sdata->u.ap;
  2068. presp = rcu_dereference(ap->probe_resp);
  2069. if (!presp)
  2070. goto out;
  2071. skb = skb_copy(presp, GFP_ATOMIC);
  2072. if (!skb)
  2073. goto out;
  2074. hdr = (struct ieee80211_hdr *) skb->data;
  2075. memset(hdr->addr1, 0, sizeof(hdr->addr1));
  2076. out:
  2077. rcu_read_unlock();
  2078. return skb;
  2079. }
  2080. EXPORT_SYMBOL(ieee80211_proberesp_get);
  2081. struct sk_buff *ieee80211_pspoll_get(struct ieee80211_hw *hw,
  2082. struct ieee80211_vif *vif)
  2083. {
  2084. struct ieee80211_sub_if_data *sdata;
  2085. struct ieee80211_if_managed *ifmgd;
  2086. struct ieee80211_pspoll *pspoll;
  2087. struct ieee80211_local *local;
  2088. struct sk_buff *skb;
  2089. if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
  2090. return NULL;
  2091. sdata = vif_to_sdata(vif);
  2092. ifmgd = &sdata->u.mgd;
  2093. local = sdata->local;
  2094. skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*pspoll));
  2095. if (!skb)
  2096. return NULL;
  2097. skb_reserve(skb, local->hw.extra_tx_headroom);
  2098. pspoll = (struct ieee80211_pspoll *) skb_put(skb, sizeof(*pspoll));
  2099. memset(pspoll, 0, sizeof(*pspoll));
  2100. pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
  2101. IEEE80211_STYPE_PSPOLL);
  2102. pspoll->aid = cpu_to_le16(ifmgd->aid);
  2103. /* aid in PS-Poll has its two MSBs each set to 1 */
  2104. pspoll->aid |= cpu_to_le16(1 << 15 | 1 << 14);
  2105. memcpy(pspoll->bssid, ifmgd->bssid, ETH_ALEN);
  2106. memcpy(pspoll->ta, vif->addr, ETH_ALEN);
  2107. return skb;
  2108. }
  2109. EXPORT_SYMBOL(ieee80211_pspoll_get);
  2110. struct sk_buff *ieee80211_nullfunc_get(struct ieee80211_hw *hw,
  2111. struct ieee80211_vif *vif)
  2112. {
  2113. struct ieee80211_hdr_3addr *nullfunc;
  2114. struct ieee80211_sub_if_data *sdata;
  2115. struct ieee80211_if_managed *ifmgd;
  2116. struct ieee80211_local *local;
  2117. struct sk_buff *skb;
  2118. if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
  2119. return NULL;
  2120. sdata = vif_to_sdata(vif);
  2121. ifmgd = &sdata->u.mgd;
  2122. local = sdata->local;
  2123. skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*nullfunc));
  2124. if (!skb)
  2125. return NULL;
  2126. skb_reserve(skb, local->hw.extra_tx_headroom);
  2127. nullfunc = (struct ieee80211_hdr_3addr *) skb_put(skb,
  2128. sizeof(*nullfunc));
  2129. memset(nullfunc, 0, sizeof(*nullfunc));
  2130. nullfunc->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
  2131. IEEE80211_STYPE_NULLFUNC |
  2132. IEEE80211_FCTL_TODS);
  2133. memcpy(nullfunc->addr1, ifmgd->bssid, ETH_ALEN);
  2134. memcpy(nullfunc->addr2, vif->addr, ETH_ALEN);
  2135. memcpy(nullfunc->addr3, ifmgd->bssid, ETH_ALEN);
  2136. return skb;
  2137. }
  2138. EXPORT_SYMBOL(ieee80211_nullfunc_get);
  2139. struct sk_buff *ieee80211_probereq_get(struct ieee80211_hw *hw,
  2140. struct ieee80211_vif *vif,
  2141. const u8 *ssid, size_t ssid_len,
  2142. const u8 *ie, size_t ie_len)
  2143. {
  2144. struct ieee80211_sub_if_data *sdata;
  2145. struct ieee80211_local *local;
  2146. struct ieee80211_hdr_3addr *hdr;
  2147. struct sk_buff *skb;
  2148. size_t ie_ssid_len;
  2149. u8 *pos;
  2150. sdata = vif_to_sdata(vif);
  2151. local = sdata->local;
  2152. ie_ssid_len = 2 + ssid_len;
  2153. skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*hdr) +
  2154. ie_ssid_len + ie_len);
  2155. if (!skb)
  2156. return NULL;
  2157. skb_reserve(skb, local->hw.extra_tx_headroom);
  2158. hdr = (struct ieee80211_hdr_3addr *) skb_put(skb, sizeof(*hdr));
  2159. memset(hdr, 0, sizeof(*hdr));
  2160. hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  2161. IEEE80211_STYPE_PROBE_REQ);
  2162. memset(hdr->addr1, 0xff, ETH_ALEN);
  2163. memcpy(hdr->addr2, vif->addr, ETH_ALEN);
  2164. memset(hdr->addr3, 0xff, ETH_ALEN);
  2165. pos = skb_put(skb, ie_ssid_len);
  2166. *pos++ = WLAN_EID_SSID;
  2167. *pos++ = ssid_len;
  2168. if (ssid)
  2169. memcpy(pos, ssid, ssid_len);
  2170. pos += ssid_len;
  2171. if (ie) {
  2172. pos = skb_put(skb, ie_len);
  2173. memcpy(pos, ie, ie_len);
  2174. }
  2175. return skb;
  2176. }
  2177. EXPORT_SYMBOL(ieee80211_probereq_get);
  2178. void ieee80211_rts_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  2179. const void *frame, size_t frame_len,
  2180. const struct ieee80211_tx_info *frame_txctl,
  2181. struct ieee80211_rts *rts)
  2182. {
  2183. const struct ieee80211_hdr *hdr = frame;
  2184. rts->frame_control =
  2185. cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS);
  2186. rts->duration = ieee80211_rts_duration(hw, vif, frame_len,
  2187. frame_txctl);
  2188. memcpy(rts->ra, hdr->addr1, sizeof(rts->ra));
  2189. memcpy(rts->ta, hdr->addr2, sizeof(rts->ta));
  2190. }
  2191. EXPORT_SYMBOL(ieee80211_rts_get);
  2192. void ieee80211_ctstoself_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  2193. const void *frame, size_t frame_len,
  2194. const struct ieee80211_tx_info *frame_txctl,
  2195. struct ieee80211_cts *cts)
  2196. {
  2197. const struct ieee80211_hdr *hdr = frame;
  2198. cts->frame_control =
  2199. cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS);
  2200. cts->duration = ieee80211_ctstoself_duration(hw, vif,
  2201. frame_len, frame_txctl);
  2202. memcpy(cts->ra, hdr->addr1, sizeof(cts->ra));
  2203. }
  2204. EXPORT_SYMBOL(ieee80211_ctstoself_get);
  2205. struct sk_buff *
  2206. ieee80211_get_buffered_bc(struct ieee80211_hw *hw,
  2207. struct ieee80211_vif *vif)
  2208. {
  2209. struct ieee80211_local *local = hw_to_local(hw);
  2210. struct sk_buff *skb = NULL;
  2211. struct ieee80211_tx_data tx;
  2212. struct ieee80211_sub_if_data *sdata;
  2213. struct ieee80211_if_ap *bss = NULL;
  2214. struct beacon_data *beacon;
  2215. struct ieee80211_tx_info *info;
  2216. sdata = vif_to_sdata(vif);
  2217. bss = &sdata->u.ap;
  2218. rcu_read_lock();
  2219. beacon = rcu_dereference(bss->beacon);
  2220. if (sdata->vif.type != NL80211_IFTYPE_AP || !beacon || !beacon->head)
  2221. goto out;
  2222. if (bss->dtim_count != 0 || !bss->dtim_bc_mc)
  2223. goto out; /* send buffered bc/mc only after DTIM beacon */
  2224. while (1) {
  2225. skb = skb_dequeue(&bss->ps_bc_buf);
  2226. if (!skb)
  2227. goto out;
  2228. local->total_ps_buffered--;
  2229. if (!skb_queue_empty(&bss->ps_bc_buf) && skb->len >= 2) {
  2230. struct ieee80211_hdr *hdr =
  2231. (struct ieee80211_hdr *) skb->data;
  2232. /* more buffered multicast/broadcast frames ==> set
  2233. * MoreData flag in IEEE 802.11 header to inform PS
  2234. * STAs */
  2235. hdr->frame_control |=
  2236. cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  2237. }
  2238. if (!ieee80211_tx_prepare(sdata, &tx, skb))
  2239. break;
  2240. dev_kfree_skb_any(skb);
  2241. }
  2242. info = IEEE80211_SKB_CB(skb);
  2243. tx.flags |= IEEE80211_TX_PS_BUFFERED;
  2244. tx.channel = local->hw.conf.channel;
  2245. info->band = tx.channel->band;
  2246. if (invoke_tx_handlers(&tx))
  2247. skb = NULL;
  2248. out:
  2249. rcu_read_unlock();
  2250. return skb;
  2251. }
  2252. EXPORT_SYMBOL(ieee80211_get_buffered_bc);
  2253. void ieee80211_tx_skb(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb)
  2254. {
  2255. skb_set_mac_header(skb, 0);
  2256. skb_set_network_header(skb, 0);
  2257. skb_set_transport_header(skb, 0);
  2258. /* Send all internal mgmt frames on VO. Accordingly set TID to 7. */
  2259. skb_set_queue_mapping(skb, IEEE80211_AC_VO);
  2260. skb->priority = 7;
  2261. /*
  2262. * The other path calling ieee80211_xmit is from the tasklet,
  2263. * and while we can handle concurrent transmissions locking
  2264. * requirements are that we do not come into tx with bhs on.
  2265. */
  2266. local_bh_disable();
  2267. ieee80211_xmit(sdata, skb);
  2268. local_bh_enable();
  2269. }