tx.c 83 KB

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