tx.c 75 KB

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