tx.c 83 KB

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