ieee80211_tx.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  1. /******************************************************************************
  2. Copyright(c) 2003 - 2004 Intel Corporation. All rights reserved.
  3. This program is free software; you can redistribute it and/or modify it
  4. under the terms of version 2 of the GNU General Public License as
  5. published by the Free Software Foundation.
  6. This program is distributed in the hope that it will be useful, but WITHOUT
  7. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  8. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  9. more details.
  10. You should have received a copy of the GNU General Public License along with
  11. this program; if not, write to the Free Software Foundation, Inc., 59
  12. Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  13. The full GNU General Public License is included in this distribution in the
  14. file called LICENSE.
  15. Contact Information:
  16. James P. Ketrenos <ipw2100-admin@linux.intel.com>
  17. Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  18. ******************************************************************************
  19. Few modifications for Realtek's Wi-Fi drivers by
  20. Andrea Merello <andrea.merello@gmail.com>
  21. A special thanks goes to Realtek for their support !
  22. ******************************************************************************/
  23. #include <linux/compiler.h>
  24. #include <linux/errno.h>
  25. #include <linux/if_arp.h>
  26. #include <linux/in6.h>
  27. #include <linux/in.h>
  28. #include <linux/ip.h>
  29. #include <linux/kernel.h>
  30. #include <linux/module.h>
  31. #include <linux/netdevice.h>
  32. #include <linux/pci.h>
  33. #include <linux/proc_fs.h>
  34. #include <linux/skbuff.h>
  35. #include <linux/slab.h>
  36. #include <linux/tcp.h>
  37. #include <linux/types.h>
  38. #include <linux/wireless.h>
  39. #include <linux/etherdevice.h>
  40. #include <linux/uaccess.h>
  41. #include <linux/if_vlan.h>
  42. #include "ieee80211.h"
  43. /*
  44. 802.11 Data Frame
  45. 802.11 frame_contorl for data frames - 2 bytes
  46. ,-----------------------------------------------------------------------------------------.
  47. bits | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | a | b | c | d | e |
  48. |----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|------|
  49. val | 0 | 0 | 0 | 1 | x | 0 | 0 | 0 | 1 | 0 | x | x | x | x | x |
  50. |----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|------|
  51. desc | ^-ver-^ | ^type-^ | ^-----subtype-----^ | to |from |more |retry| pwr |more |wep |
  52. | | | x=0 data,x=1 data+ack | DS | DS |frag | | mgm |data | |
  53. '-----------------------------------------------------------------------------------------'
  54. /\
  55. |
  56. 802.11 Data Frame |
  57. ,--------- 'ctrl' expands to >-----------'
  58. |
  59. ,--'---,-------------------------------------------------------------.
  60. Bytes | 2 | 2 | 6 | 6 | 6 | 2 | 0..2312 | 4 |
  61. |------|------|---------|---------|---------|------|---------|------|
  62. Desc. | ctrl | dura | DA/RA | TA | SA | Sequ | Frame | fcs |
  63. | | tion | (BSSID) | | | ence | data | |
  64. `--------------------------------------------------| |------'
  65. Total: 28 non-data bytes `----.----'
  66. |
  67. .- 'Frame data' expands to <---------------------------'
  68. |
  69. V
  70. ,---------------------------------------------------.
  71. Bytes | 1 | 1 | 1 | 3 | 2 | 0-2304 |
  72. |------|------|---------|----------|------|---------|
  73. Desc. | SNAP | SNAP | Control |Eth Tunnel| Type | IP |
  74. | DSAP | SSAP | | | | Packet |
  75. | 0xAA | 0xAA |0x03 (UI)|0x00-00-F8| | |
  76. `-----------------------------------------| |
  77. Total: 8 non-data bytes `----.----'
  78. |
  79. .- 'IP Packet' expands, if WEP enabled, to <--'
  80. |
  81. V
  82. ,-----------------------.
  83. Bytes | 4 | 0-2296 | 4 |
  84. |-----|-----------|-----|
  85. Desc. | IV | Encrypted | ICV |
  86. | | IP Packet | |
  87. `-----------------------'
  88. Total: 8 non-data bytes
  89. 802.3 Ethernet Data Frame
  90. ,-----------------------------------------.
  91. Bytes | 6 | 6 | 2 | Variable | 4 |
  92. |-------|-------|------|-----------|------|
  93. Desc. | Dest. | Source| Type | IP Packet | fcs |
  94. | MAC | MAC | | | |
  95. `-----------------------------------------'
  96. Total: 18 non-data bytes
  97. In the event that fragmentation is required, the incoming payload is split into
  98. N parts of size ieee->fts. The first fragment contains the SNAP header and the
  99. remaining packets are just data.
  100. If encryption is enabled, each fragment payload size is reduced by enough space
  101. to add the prefix and postfix (IV and ICV totalling 8 bytes in the case of WEP)
  102. So if you have 1500 bytes of payload with ieee->fts set to 500 without
  103. encryption it will take 3 frames. With WEP it will take 4 frames as the
  104. payload of each frame is reduced to 492 bytes.
  105. * SKB visualization
  106. *
  107. * ,- skb->data
  108. * |
  109. * | ETHERNET HEADER ,-<-- PAYLOAD
  110. * | | 14 bytes from skb->data
  111. * | 2 bytes for Type --> ,T. | (sizeof ethhdr)
  112. * | | | |
  113. * |,-Dest.--. ,--Src.---. | | |
  114. * | 6 bytes| | 6 bytes | | | |
  115. * v | | | | | |
  116. * 0 | v 1 | v | v 2
  117. * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
  118. * ^ | ^ | ^ |
  119. * | | | | | |
  120. * | | | | `T' <---- 2 bytes for Type
  121. * | | | |
  122. * | | '---SNAP--' <-------- 6 bytes for SNAP
  123. * | |
  124. * `-IV--' <-------------------- 4 bytes for IV (WEP)
  125. *
  126. * SNAP HEADER
  127. *
  128. */
  129. static u8 P802_1H_OUI[P80211_OUI_LEN] = { 0x00, 0x00, 0xf8 };
  130. static u8 RFC1042_OUI[P80211_OUI_LEN] = { 0x00, 0x00, 0x00 };
  131. static inline int ieee80211_put_snap(u8 *data, u16 h_proto)
  132. {
  133. struct ieee80211_snap_hdr *snap;
  134. u8 *oui;
  135. snap = (struct ieee80211_snap_hdr *)data;
  136. snap->dsap = 0xaa;
  137. snap->ssap = 0xaa;
  138. snap->ctrl = 0x03;
  139. if (h_proto == 0x8137 || h_proto == 0x80f3)
  140. oui = P802_1H_OUI;
  141. else
  142. oui = RFC1042_OUI;
  143. snap->oui[0] = oui[0];
  144. snap->oui[1] = oui[1];
  145. snap->oui[2] = oui[2];
  146. *(u16 *)(data + SNAP_SIZE) = htons(h_proto);
  147. return SNAP_SIZE + sizeof(u16);
  148. }
  149. int ieee80211_encrypt_fragment(
  150. struct ieee80211_device *ieee,
  151. struct sk_buff *frag,
  152. int hdr_len)
  153. {
  154. struct ieee80211_crypt_data *crypt = ieee->crypt[ieee->tx_keyidx];
  155. int res;
  156. if (!(crypt && crypt->ops))
  157. {
  158. printk("=========>%s(), crypt is null\n", __func__);
  159. return -1;
  160. }
  161. if (ieee->tkip_countermeasures &&
  162. crypt && crypt->ops && strcmp(crypt->ops->name, "TKIP") == 0) {
  163. if (net_ratelimit()) {
  164. struct rtl_80211_hdr_3addrqos *header;
  165. header = (struct rtl_80211_hdr_3addrqos *)frag->data;
  166. printk(KERN_DEBUG "%s: TKIP countermeasures: dropped "
  167. "TX packet to %pM\n",
  168. ieee->dev->name, header->addr1);
  169. }
  170. return -1;
  171. }
  172. /* To encrypt, frame format is:
  173. * IV (4 bytes), clear payload (including SNAP), ICV (4 bytes) */
  174. // PR: FIXME: Copied from hostap. Check fragmentation/MSDU/MPDU encryption.
  175. /* Host-based IEEE 802.11 fragmentation for TX is not yet supported, so
  176. * call both MSDU and MPDU encryption functions from here. */
  177. atomic_inc(&crypt->refcnt);
  178. res = 0;
  179. if (crypt->ops->encrypt_msdu)
  180. res = crypt->ops->encrypt_msdu(frag, hdr_len, crypt->priv);
  181. if (res == 0 && crypt->ops->encrypt_mpdu)
  182. res = crypt->ops->encrypt_mpdu(frag, hdr_len, crypt->priv);
  183. atomic_dec(&crypt->refcnt);
  184. if (res < 0) {
  185. printk(KERN_INFO "%s: Encryption failed: len=%d.\n",
  186. ieee->dev->name, frag->len);
  187. ieee->ieee_stats.tx_discards++;
  188. return -1;
  189. }
  190. return 0;
  191. }
  192. void ieee80211_txb_free(struct ieee80211_txb *txb) {
  193. //int i;
  194. if (unlikely(!txb))
  195. return;
  196. kfree(txb);
  197. }
  198. EXPORT_SYMBOL(ieee80211_txb_free);
  199. static struct ieee80211_txb *ieee80211_alloc_txb(int nr_frags, int txb_size,
  200. gfp_t gfp_mask)
  201. {
  202. struct ieee80211_txb *txb;
  203. int i;
  204. txb = kmalloc(
  205. sizeof(struct ieee80211_txb) + (sizeof(u8 *) * nr_frags),
  206. gfp_mask);
  207. if (!txb)
  208. return NULL;
  209. memset(txb, 0, sizeof(struct ieee80211_txb));
  210. txb->nr_frags = nr_frags;
  211. txb->frag_size = txb_size;
  212. for (i = 0; i < nr_frags; i++) {
  213. txb->fragments[i] = dev_alloc_skb(txb_size);
  214. if (unlikely(!txb->fragments[i])) {
  215. i--;
  216. break;
  217. }
  218. memset(txb->fragments[i]->cb, 0, sizeof(txb->fragments[i]->cb));
  219. }
  220. if (unlikely(i != nr_frags)) {
  221. while (i >= 0)
  222. dev_kfree_skb_any(txb->fragments[i--]);
  223. kfree(txb);
  224. return NULL;
  225. }
  226. return txb;
  227. }
  228. // Classify the to-be send data packet
  229. // Need to acquire the sent queue index.
  230. static int
  231. ieee80211_classify(struct sk_buff *skb, struct ieee80211_network *network)
  232. {
  233. struct ethhdr *eth;
  234. struct iphdr *ip;
  235. eth = (struct ethhdr *)skb->data;
  236. if (eth->h_proto != htons(ETH_P_IP))
  237. return 0;
  238. // IEEE80211_DEBUG_DATA(IEEE80211_DL_DATA, skb->data, skb->len);
  239. ip = ip_hdr(skb);
  240. switch (ip->tos & 0xfc) {
  241. case 0x20:
  242. return 2;
  243. case 0x40:
  244. return 1;
  245. case 0x60:
  246. return 3;
  247. case 0x80:
  248. return 4;
  249. case 0xa0:
  250. return 5;
  251. case 0xc0:
  252. return 6;
  253. case 0xe0:
  254. return 7;
  255. default:
  256. return 0;
  257. }
  258. }
  259. #define SN_LESS(a, b) (((a-b)&0x800)!=0)
  260. static void ieee80211_tx_query_agg_cap(struct ieee80211_device *ieee,
  261. struct sk_buff *skb, cb_desc *tcb_desc)
  262. {
  263. PRT_HIGH_THROUGHPUT pHTInfo = ieee->pHTInfo;
  264. PTX_TS_RECORD pTxTs = NULL;
  265. struct rtl_80211_hdr_1addr *hdr = (struct rtl_80211_hdr_1addr *)skb->data;
  266. if (!pHTInfo->bCurrentHTSupport||!pHTInfo->bEnableHT)
  267. return;
  268. if (!IsQoSDataFrame(skb->data))
  269. return;
  270. if (is_multicast_ether_addr(hdr->addr1))
  271. return;
  272. //check packet and mode later
  273. #ifdef TO_DO_LIST
  274. if(pTcb->PacketLength >= 4096)
  275. return;
  276. // For RTL819X, if pairwisekey = wep/tkip, we don't aggrregation.
  277. if(!Adapter->HalFunc.GetNmodeSupportBySecCfgHandler(Adapter))
  278. return;
  279. #endif
  280. if(!ieee->GetNmodeSupportBySecCfg(ieee->dev))
  281. {
  282. return;
  283. }
  284. if(pHTInfo->bCurrentAMPDUEnable)
  285. {
  286. if (!GetTs(ieee, (PTS_COMMON_INFO *)(&pTxTs), hdr->addr1, skb->priority, TX_DIR, true))
  287. {
  288. printk("===>can't get TS\n");
  289. return;
  290. }
  291. if (!pTxTs->TxAdmittedBARecord.bValid)
  292. {
  293. TsStartAddBaProcess(ieee, pTxTs);
  294. goto FORCED_AGG_SETTING;
  295. }
  296. else if (!pTxTs->bUsingBa)
  297. {
  298. if (SN_LESS(pTxTs->TxAdmittedBARecord.BaStartSeqCtrl.field.SeqNum, (pTxTs->TxCurSeq+1)%4096))
  299. pTxTs->bUsingBa = true;
  300. else
  301. goto FORCED_AGG_SETTING;
  302. }
  303. if (ieee->iw_mode == IW_MODE_INFRA)
  304. {
  305. tcb_desc->bAMPDUEnable = true;
  306. tcb_desc->ampdu_factor = pHTInfo->CurrentAMPDUFactor;
  307. tcb_desc->ampdu_density = pHTInfo->CurrentMPDUDensity;
  308. }
  309. }
  310. FORCED_AGG_SETTING:
  311. switch (pHTInfo->ForcedAMPDUMode )
  312. {
  313. case HT_AGG_AUTO:
  314. break;
  315. case HT_AGG_FORCE_ENABLE:
  316. tcb_desc->bAMPDUEnable = true;
  317. tcb_desc->ampdu_density = pHTInfo->ForcedMPDUDensity;
  318. tcb_desc->ampdu_factor = pHTInfo->ForcedAMPDUFactor;
  319. break;
  320. case HT_AGG_FORCE_DISABLE:
  321. tcb_desc->bAMPDUEnable = false;
  322. tcb_desc->ampdu_density = 0;
  323. tcb_desc->ampdu_factor = 0;
  324. break;
  325. }
  326. return;
  327. }
  328. static void ieee80211_qurey_ShortPreambleMode(struct ieee80211_device *ieee,
  329. cb_desc *tcb_desc)
  330. {
  331. tcb_desc->bUseShortPreamble = false;
  332. if (tcb_desc->data_rate == 2)
  333. {//// 1M can only use Long Preamble. 11B spec
  334. return;
  335. }
  336. else if (ieee->current_network.capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
  337. {
  338. tcb_desc->bUseShortPreamble = true;
  339. }
  340. return;
  341. }
  342. static void
  343. ieee80211_query_HTCapShortGI(struct ieee80211_device *ieee, cb_desc *tcb_desc)
  344. {
  345. PRT_HIGH_THROUGHPUT pHTInfo = ieee->pHTInfo;
  346. tcb_desc->bUseShortGI = false;
  347. if(!pHTInfo->bCurrentHTSupport||!pHTInfo->bEnableHT)
  348. return;
  349. if(pHTInfo->bForcedShortGI)
  350. {
  351. tcb_desc->bUseShortGI = true;
  352. return;
  353. }
  354. if((pHTInfo->bCurBW40MHz==true) && pHTInfo->bCurShortGI40MHz)
  355. tcb_desc->bUseShortGI = true;
  356. else if((pHTInfo->bCurBW40MHz==false) && pHTInfo->bCurShortGI20MHz)
  357. tcb_desc->bUseShortGI = true;
  358. }
  359. static void ieee80211_query_BandwidthMode(struct ieee80211_device *ieee,
  360. cb_desc *tcb_desc)
  361. {
  362. PRT_HIGH_THROUGHPUT pHTInfo = ieee->pHTInfo;
  363. tcb_desc->bPacketBW = false;
  364. if(!pHTInfo->bCurrentHTSupport||!pHTInfo->bEnableHT)
  365. return;
  366. if(tcb_desc->bMulticast || tcb_desc->bBroadcast)
  367. return;
  368. if((tcb_desc->data_rate & 0x80)==0) // If using legacy rate, it shall use 20MHz channel.
  369. return;
  370. //BandWidthAutoSwitch is for auto switch to 20 or 40 in long distance
  371. if(pHTInfo->bCurBW40MHz && pHTInfo->bCurTxBW40MHz && !ieee->bandwidth_auto_switch.bforced_tx20Mhz)
  372. tcb_desc->bPacketBW = true;
  373. return;
  374. }
  375. static void ieee80211_query_protectionmode(struct ieee80211_device *ieee,
  376. cb_desc *tcb_desc,
  377. struct sk_buff *skb)
  378. {
  379. // Common Settings
  380. tcb_desc->bRTSSTBC = false;
  381. tcb_desc->bRTSUseShortGI = false; // Since protection frames are always sent by legacy rate, ShortGI will never be used.
  382. tcb_desc->bCTSEnable = false; // Most of protection using RTS/CTS
  383. tcb_desc->RTSSC = 0; // 20MHz: Don't care; 40MHz: Duplicate.
  384. tcb_desc->bRTSBW = false; // RTS frame bandwidth is always 20MHz
  385. if(tcb_desc->bBroadcast || tcb_desc->bMulticast)//only unicast frame will use rts/cts
  386. return;
  387. if (is_broadcast_ether_addr(skb->data+16)) //check addr3 as infrastructure add3 is DA.
  388. return;
  389. if (ieee->mode < IEEE_N_24G) //b, g mode
  390. {
  391. // (1) RTS_Threshold is compared to the MPDU, not MSDU.
  392. // (2) If there are more than one frag in this MSDU, only the first frag uses protection frame.
  393. // Other fragments are protected by previous fragment.
  394. // So we only need to check the length of first fragment.
  395. if (skb->len > ieee->rts)
  396. {
  397. tcb_desc->bRTSEnable = true;
  398. tcb_desc->rts_rate = MGN_24M;
  399. }
  400. else if (ieee->current_network.buseprotection)
  401. {
  402. // Use CTS-to-SELF in protection mode.
  403. tcb_desc->bRTSEnable = true;
  404. tcb_desc->bCTSEnable = true;
  405. tcb_desc->rts_rate = MGN_24M;
  406. }
  407. //otherwise return;
  408. return;
  409. }
  410. else
  411. {// 11n High throughput case.
  412. PRT_HIGH_THROUGHPUT pHTInfo = ieee->pHTInfo;
  413. while (true)
  414. {
  415. //check ERP protection
  416. if (ieee->current_network.buseprotection)
  417. {// CTS-to-SELF
  418. tcb_desc->bRTSEnable = true;
  419. tcb_desc->bCTSEnable = true;
  420. tcb_desc->rts_rate = MGN_24M;
  421. break;
  422. }
  423. //check HT op mode
  424. if(pHTInfo->bCurrentHTSupport && pHTInfo->bEnableHT)
  425. {
  426. u8 HTOpMode = pHTInfo->CurrentOpMode;
  427. if((pHTInfo->bCurBW40MHz && (HTOpMode == 2 || HTOpMode == 3)) ||
  428. (!pHTInfo->bCurBW40MHz && HTOpMode == 3) )
  429. {
  430. tcb_desc->rts_rate = MGN_24M; // Rate is 24Mbps.
  431. tcb_desc->bRTSEnable = true;
  432. break;
  433. }
  434. }
  435. //check rts
  436. if (skb->len > ieee->rts)
  437. {
  438. tcb_desc->rts_rate = MGN_24M; // Rate is 24Mbps.
  439. tcb_desc->bRTSEnable = true;
  440. break;
  441. }
  442. //to do list: check MIMO power save condition.
  443. //check AMPDU aggregation for TXOP
  444. if(tcb_desc->bAMPDUEnable)
  445. {
  446. tcb_desc->rts_rate = MGN_24M; // Rate is 24Mbps.
  447. // According to 8190 design, firmware sends CF-End only if RTS/CTS is enabled. However, it degrads
  448. // throughput around 10M, so we disable of this mechanism. 2007.08.03 by Emily
  449. tcb_desc->bRTSEnable = false;
  450. break;
  451. }
  452. //check IOT action
  453. if(pHTInfo->IOTAction & HT_IOT_ACT_FORCED_CTS2SELF)
  454. {
  455. tcb_desc->bCTSEnable = true;
  456. tcb_desc->rts_rate = MGN_24M;
  457. tcb_desc->bRTSEnable = true;
  458. break;
  459. }
  460. // Totally no protection case!!
  461. goto NO_PROTECTION;
  462. }
  463. }
  464. // For test , CTS replace with RTS
  465. if (0) {
  466. tcb_desc->bCTSEnable = true;
  467. tcb_desc->rts_rate = MGN_24M;
  468. tcb_desc->bRTSEnable = true;
  469. }
  470. if (ieee->current_network.capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
  471. tcb_desc->bUseShortPreamble = true;
  472. if (ieee->mode == IW_MODE_MASTER)
  473. goto NO_PROTECTION;
  474. return;
  475. NO_PROTECTION:
  476. tcb_desc->bRTSEnable = false;
  477. tcb_desc->bCTSEnable = false;
  478. tcb_desc->rts_rate = 0;
  479. tcb_desc->RTSSC = 0;
  480. tcb_desc->bRTSBW = false;
  481. }
  482. static void ieee80211_txrate_selectmode(struct ieee80211_device *ieee,
  483. cb_desc *tcb_desc)
  484. {
  485. #ifdef TO_DO_LIST
  486. if(!IsDataFrame(pFrame))
  487. {
  488. pTcb->bTxDisableRateFallBack = true;
  489. pTcb->bTxUseDriverAssingedRate = true;
  490. pTcb->RATRIndex = 7;
  491. return;
  492. }
  493. if(pMgntInfo->ForcedDataRate!= 0)
  494. {
  495. pTcb->bTxDisableRateFallBack = true;
  496. pTcb->bTxUseDriverAssingedRate = true;
  497. return;
  498. }
  499. #endif
  500. if(ieee->bTxDisableRateFallBack)
  501. tcb_desc->bTxDisableRateFallBack = true;
  502. if(ieee->bTxUseDriverAssingedRate)
  503. tcb_desc->bTxUseDriverAssingedRate = true;
  504. if(!tcb_desc->bTxDisableRateFallBack || !tcb_desc->bTxUseDriverAssingedRate)
  505. {
  506. if (ieee->iw_mode == IW_MODE_INFRA || ieee->iw_mode == IW_MODE_ADHOC)
  507. tcb_desc->RATRIndex = 0;
  508. }
  509. }
  510. static void ieee80211_query_seqnum(struct ieee80211_device *ieee,
  511. struct sk_buff *skb, u8 *dst)
  512. {
  513. if (is_multicast_ether_addr(dst))
  514. return;
  515. if (IsQoSDataFrame(skb->data)) //we deal qos data only
  516. {
  517. PTX_TS_RECORD pTS = NULL;
  518. if (!GetTs(ieee, (PTS_COMMON_INFO *)(&pTS), dst, skb->priority, TX_DIR, true))
  519. {
  520. return;
  521. }
  522. pTS->TxCurSeq = (pTS->TxCurSeq+1)%4096;
  523. }
  524. }
  525. int ieee80211_xmit(struct sk_buff *skb, struct net_device *dev)
  526. {
  527. struct ieee80211_device *ieee = netdev_priv(dev);
  528. struct ieee80211_txb *txb = NULL;
  529. struct rtl_80211_hdr_3addrqos *frag_hdr;
  530. int i, bytes_per_frag, nr_frags, bytes_last_frag, frag_size;
  531. unsigned long flags;
  532. struct net_device_stats *stats = &ieee->stats;
  533. int ether_type = 0, encrypt;
  534. int bytes, fc, qos_ctl = 0, hdr_len;
  535. struct sk_buff *skb_frag;
  536. struct rtl_80211_hdr_3addrqos header = { /* Ensure zero initialized */
  537. .duration_id = 0,
  538. .seq_ctl = 0,
  539. .qos_ctl = 0
  540. };
  541. u8 dest[ETH_ALEN], src[ETH_ALEN];
  542. int qos_actived = ieee->current_network.qos_data.active;
  543. struct ieee80211_crypt_data *crypt;
  544. cb_desc *tcb_desc;
  545. spin_lock_irqsave(&ieee->lock, flags);
  546. /* If there is no driver handler to take the TXB, dont' bother
  547. * creating it... */
  548. if ((!ieee->hard_start_xmit && !(ieee->softmac_features & IEEE_SOFTMAC_TX_QUEUE))||
  549. ((!ieee->softmac_data_hard_start_xmit && (ieee->softmac_features & IEEE_SOFTMAC_TX_QUEUE)))) {
  550. printk(KERN_WARNING "%s: No xmit handler.\n",
  551. ieee->dev->name);
  552. goto success;
  553. }
  554. if(likely(ieee->raw_tx == 0)){
  555. if (unlikely(skb->len < SNAP_SIZE + sizeof(u16))) {
  556. printk(KERN_WARNING "%s: skb too small (%d).\n",
  557. ieee->dev->name, skb->len);
  558. goto success;
  559. }
  560. memset(skb->cb, 0, sizeof(skb->cb));
  561. ether_type = ntohs(((struct ethhdr *)skb->data)->h_proto);
  562. crypt = ieee->crypt[ieee->tx_keyidx];
  563. encrypt = !(ether_type == ETH_P_PAE && ieee->ieee802_1x) &&
  564. ieee->host_encrypt && crypt && crypt->ops;
  565. if (!encrypt && ieee->ieee802_1x &&
  566. ieee->drop_unencrypted && ether_type != ETH_P_PAE) {
  567. stats->tx_dropped++;
  568. goto success;
  569. }
  570. #ifdef CONFIG_IEEE80211_DEBUG
  571. if (crypt && !encrypt && ether_type == ETH_P_PAE) {
  572. struct eapol *eap = (struct eapol *)(skb->data +
  573. sizeof(struct ethhdr) - SNAP_SIZE - sizeof(u16));
  574. IEEE80211_DEBUG_EAP("TX: IEEE 802.11 EAPOL frame: %s\n",
  575. eap_get_type(eap->type));
  576. }
  577. #endif
  578. /* Save source and destination addresses */
  579. memcpy(&dest, skb->data, ETH_ALEN);
  580. memcpy(&src, skb->data+ETH_ALEN, ETH_ALEN);
  581. /* Advance the SKB to the start of the payload */
  582. skb_pull(skb, sizeof(struct ethhdr));
  583. /* Determine total amount of storage required for TXB packets */
  584. bytes = skb->len + SNAP_SIZE + sizeof(u16);
  585. if (encrypt)
  586. fc = IEEE80211_FTYPE_DATA | IEEE80211_FCTL_WEP;
  587. else
  588. fc = IEEE80211_FTYPE_DATA;
  589. //if(ieee->current_network.QoS_Enable)
  590. if(qos_actived)
  591. fc |= IEEE80211_STYPE_QOS_DATA;
  592. else
  593. fc |= IEEE80211_STYPE_DATA;
  594. if (ieee->iw_mode == IW_MODE_INFRA) {
  595. fc |= IEEE80211_FCTL_TODS;
  596. /* To DS: Addr1 = BSSID, Addr2 = SA,
  597. Addr3 = DA */
  598. memcpy(&header.addr1, ieee->current_network.bssid, ETH_ALEN);
  599. memcpy(&header.addr2, &src, ETH_ALEN);
  600. memcpy(&header.addr3, &dest, ETH_ALEN);
  601. } else if (ieee->iw_mode == IW_MODE_ADHOC) {
  602. /* not From/To DS: Addr1 = DA, Addr2 = SA,
  603. Addr3 = BSSID */
  604. memcpy(&header.addr1, dest, ETH_ALEN);
  605. memcpy(&header.addr2, src, ETH_ALEN);
  606. memcpy(&header.addr3, ieee->current_network.bssid, ETH_ALEN);
  607. }
  608. header.frame_ctl = cpu_to_le16(fc);
  609. /* Determine fragmentation size based on destination (multicast
  610. * and broadcast are not fragmented) */
  611. if (is_multicast_ether_addr(header.addr1)) {
  612. frag_size = MAX_FRAG_THRESHOLD;
  613. qos_ctl |= QOS_CTL_NOTCONTAIN_ACK;
  614. }
  615. else {
  616. frag_size = ieee->fts;//default:392
  617. qos_ctl = 0;
  618. }
  619. //if (ieee->current_network.QoS_Enable)
  620. if(qos_actived)
  621. {
  622. hdr_len = IEEE80211_3ADDR_LEN + 2;
  623. skb->priority = ieee80211_classify(skb, &ieee->current_network);
  624. qos_ctl |= skb->priority; //set in the ieee80211_classify
  625. header.qos_ctl = cpu_to_le16(qos_ctl & IEEE80211_QOS_TID);
  626. } else {
  627. hdr_len = IEEE80211_3ADDR_LEN;
  628. }
  629. /* Determine amount of payload per fragment. Regardless of if
  630. * this stack is providing the full 802.11 header, one will
  631. * eventually be affixed to this fragment -- so we must account for
  632. * it when determining the amount of payload space. */
  633. bytes_per_frag = frag_size - hdr_len;
  634. if (ieee->config &
  635. (CFG_IEEE80211_COMPUTE_FCS | CFG_IEEE80211_RESERVE_FCS))
  636. bytes_per_frag -= IEEE80211_FCS_LEN;
  637. /* Each fragment may need to have room for encryption pre/postfix */
  638. if (encrypt)
  639. bytes_per_frag -= crypt->ops->extra_prefix_len +
  640. crypt->ops->extra_postfix_len;
  641. /* Number of fragments is the total bytes_per_frag /
  642. * payload_per_fragment */
  643. nr_frags = bytes / bytes_per_frag;
  644. bytes_last_frag = bytes % bytes_per_frag;
  645. if (bytes_last_frag)
  646. nr_frags++;
  647. else
  648. bytes_last_frag = bytes_per_frag;
  649. /* When we allocate the TXB we allocate enough space for the reserve
  650. * and full fragment bytes (bytes_per_frag doesn't include prefix,
  651. * postfix, header, FCS, etc.) */
  652. txb = ieee80211_alloc_txb(nr_frags, frag_size + ieee->tx_headroom, GFP_ATOMIC);
  653. if (unlikely(!txb)) {
  654. printk(KERN_WARNING "%s: Could not allocate TXB\n",
  655. ieee->dev->name);
  656. goto failed;
  657. }
  658. txb->encrypted = encrypt;
  659. txb->payload_size = bytes;
  660. //if (ieee->current_network.QoS_Enable)
  661. if(qos_actived)
  662. {
  663. txb->queue_index = UP2AC(skb->priority);
  664. } else {
  665. txb->queue_index = WME_AC_BK;
  666. }
  667. for (i = 0; i < nr_frags; i++) {
  668. skb_frag = txb->fragments[i];
  669. tcb_desc = (cb_desc *)(skb_frag->cb + MAX_DEV_ADDR_SIZE);
  670. if(qos_actived){
  671. skb_frag->priority = skb->priority;//UP2AC(skb->priority);
  672. tcb_desc->queue_index = UP2AC(skb->priority);
  673. } else {
  674. skb_frag->priority = WME_AC_BK;
  675. tcb_desc->queue_index = WME_AC_BK;
  676. }
  677. skb_reserve(skb_frag, ieee->tx_headroom);
  678. if (encrypt){
  679. if (ieee->hwsec_active)
  680. tcb_desc->bHwSec = 1;
  681. else
  682. tcb_desc->bHwSec = 0;
  683. skb_reserve(skb_frag, crypt->ops->extra_prefix_len);
  684. }
  685. else
  686. {
  687. tcb_desc->bHwSec = 0;
  688. }
  689. frag_hdr = (struct rtl_80211_hdr_3addrqos *)skb_put(skb_frag, hdr_len);
  690. memcpy(frag_hdr, &header, hdr_len);
  691. /* If this is not the last fragment, then add the MOREFRAGS
  692. * bit to the frame control */
  693. if (i != nr_frags - 1) {
  694. frag_hdr->frame_ctl = cpu_to_le16(
  695. fc | IEEE80211_FCTL_MOREFRAGS);
  696. bytes = bytes_per_frag;
  697. } else {
  698. /* The last fragment takes the remaining length */
  699. bytes = bytes_last_frag;
  700. }
  701. //if(ieee->current_network.QoS_Enable)
  702. if(qos_actived)
  703. {
  704. // add 1 only indicate to corresponding seq number control 2006/7/12
  705. frag_hdr->seq_ctl = cpu_to_le16(ieee->seq_ctrl[UP2AC(skb->priority)+1]<<4 | i);
  706. } else {
  707. frag_hdr->seq_ctl = cpu_to_le16(ieee->seq_ctrl[0]<<4 | i);
  708. }
  709. /* Put a SNAP header on the first fragment */
  710. if (i == 0) {
  711. ieee80211_put_snap(
  712. skb_put(skb_frag, SNAP_SIZE + sizeof(u16)),
  713. ether_type);
  714. bytes -= SNAP_SIZE + sizeof(u16);
  715. }
  716. memcpy(skb_put(skb_frag, bytes), skb->data, bytes);
  717. /* Advance the SKB... */
  718. skb_pull(skb, bytes);
  719. /* Encryption routine will move the header forward in order
  720. * to insert the IV between the header and the payload */
  721. if (encrypt)
  722. ieee80211_encrypt_fragment(ieee, skb_frag, hdr_len);
  723. if (ieee->config &
  724. (CFG_IEEE80211_COMPUTE_FCS | CFG_IEEE80211_RESERVE_FCS))
  725. skb_put(skb_frag, 4);
  726. }
  727. if(qos_actived)
  728. {
  729. if (ieee->seq_ctrl[UP2AC(skb->priority) + 1] == 0xFFF)
  730. ieee->seq_ctrl[UP2AC(skb->priority) + 1] = 0;
  731. else
  732. ieee->seq_ctrl[UP2AC(skb->priority) + 1]++;
  733. } else {
  734. if (ieee->seq_ctrl[0] == 0xFFF)
  735. ieee->seq_ctrl[0] = 0;
  736. else
  737. ieee->seq_ctrl[0]++;
  738. }
  739. }else{
  740. if (unlikely(skb->len < sizeof(struct rtl_80211_hdr_3addr))) {
  741. printk(KERN_WARNING "%s: skb too small (%d).\n",
  742. ieee->dev->name, skb->len);
  743. goto success;
  744. }
  745. txb = ieee80211_alloc_txb(1, skb->len, GFP_ATOMIC);
  746. if(!txb){
  747. printk(KERN_WARNING "%s: Could not allocate TXB\n",
  748. ieee->dev->name);
  749. goto failed;
  750. }
  751. txb->encrypted = 0;
  752. txb->payload_size = skb->len;
  753. memcpy(skb_put(txb->fragments[0],skb->len), skb->data, skb->len);
  754. }
  755. success:
  756. //WB add to fill data tcb_desc here. only first fragment is considered, need to change, and you may remove to other place.
  757. if (txb)
  758. {
  759. cb_desc *tcb_desc = (cb_desc *)(txb->fragments[0]->cb + MAX_DEV_ADDR_SIZE);
  760. tcb_desc->bTxEnableFwCalcDur = 1;
  761. if (is_multicast_ether_addr(header.addr1))
  762. tcb_desc->bMulticast = 1;
  763. if (is_broadcast_ether_addr(header.addr1))
  764. tcb_desc->bBroadcast = 1;
  765. ieee80211_txrate_selectmode(ieee, tcb_desc);
  766. if (tcb_desc->bMulticast || tcb_desc->bBroadcast)
  767. tcb_desc->data_rate = ieee->basic_rate;
  768. else
  769. //tcb_desc->data_rate = CURRENT_RATE(ieee->current_network.mode, ieee->rate, ieee->HTCurrentOperaRate);
  770. tcb_desc->data_rate = CURRENT_RATE(ieee->mode, ieee->rate, ieee->HTCurrentOperaRate);
  771. ieee80211_qurey_ShortPreambleMode(ieee, tcb_desc);
  772. ieee80211_tx_query_agg_cap(ieee, txb->fragments[0], tcb_desc);
  773. ieee80211_query_HTCapShortGI(ieee, tcb_desc);
  774. ieee80211_query_BandwidthMode(ieee, tcb_desc);
  775. ieee80211_query_protectionmode(ieee, tcb_desc, txb->fragments[0]);
  776. ieee80211_query_seqnum(ieee, txb->fragments[0], header.addr1);
  777. // IEEE80211_DEBUG_DATA(IEEE80211_DL_DATA, txb->fragments[0]->data, txb->fragments[0]->len);
  778. //IEEE80211_DEBUG_DATA(IEEE80211_DL_DATA, tcb_desc, sizeof(cb_desc));
  779. }
  780. spin_unlock_irqrestore(&ieee->lock, flags);
  781. dev_kfree_skb_any(skb);
  782. if (txb) {
  783. if (ieee->softmac_features & IEEE_SOFTMAC_TX_QUEUE){
  784. ieee80211_softmac_xmit(txb, ieee);
  785. }else{
  786. if ((*ieee->hard_start_xmit)(txb, dev) == 0) {
  787. stats->tx_packets++;
  788. stats->tx_bytes += txb->payload_size;
  789. return 0;
  790. }
  791. ieee80211_txb_free(txb);
  792. }
  793. }
  794. return 0;
  795. failed:
  796. spin_unlock_irqrestore(&ieee->lock, flags);
  797. netif_stop_queue(dev);
  798. stats->tx_errors++;
  799. return 1;
  800. }