tx.c 76 KB

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