/drivers/staging/rtl8192u/ieee80211/ieee80211_tx.c

https://bitbucket.org/cyanogenmod/android_kernel_asus_tf300t · C · 911 lines · 623 code · 93 blank · 195 comment · 167 complexity · b664c88dbbb43df79492e3e3596a270d MD5 · raw file

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