tx.c 75 KB

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