wctl.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. * File: wctl.c
  20. *
  21. * Purpose: handle WMAC duplicate filter & defragment
  22. *
  23. * Author: Jerry Chen
  24. *
  25. * Date: Jun. 27, 2002
  26. *
  27. * Functions:
  28. * WCTLbIsDuplicate - Test if duplicate packet
  29. * WCTLuSearchDFCB - Search DeFragment Control Database
  30. * WCTLuInsertDFCB - Insert DeFragment Control Database
  31. * WCTLbHandleFragment - Handle received fragment packet
  32. *
  33. * Revision History:
  34. *
  35. */
  36. #include "wctl.h"
  37. #include "device.h"
  38. #include "card.h"
  39. /*--------------------- Static Definitions -------------------------*/
  40. /*--------------------- Static Classes ----------------------------*/
  41. /*--------------------- Static Variables --------------------------*/
  42. /*--------------------- Static Functions --------------------------*/
  43. /*--------------------- Export Variables --------------------------*/
  44. /*
  45. * Description:
  46. * Scan Rx cache. Return true if packet is duplicate, else
  47. * inserts in receive cache and returns false.
  48. *
  49. * Parameters:
  50. * In:
  51. * pCache - Receive packets history
  52. * pMACHeader - 802.11 MAC Header of received packet
  53. * Out:
  54. * none
  55. *
  56. * Return Value: true if packet duplicate; otherwise false
  57. *
  58. */
  59. bool WCTLbIsDuplicate(PSCache pCache, PS802_11Header pMACHeader)
  60. {
  61. unsigned int uIndex;
  62. unsigned int ii;
  63. PSCacheEntry pCacheEntry;
  64. if (IS_FC_RETRY(pMACHeader)) {
  65. uIndex = pCache->uInPtr;
  66. for (ii = 0; ii < DUPLICATE_RX_CACHE_LENGTH; ii++) {
  67. pCacheEntry = &(pCache->asCacheEntry[uIndex]);
  68. if ((pCacheEntry->wFmSequence == pMACHeader->wSeqCtl) &&
  69. ether_addr_equal(pCacheEntry->abyAddr2,
  70. pMACHeader->abyAddr2)) {
  71. /* Duplicate match */
  72. return true;
  73. }
  74. ADD_ONE_WITH_WRAP_AROUND(uIndex, DUPLICATE_RX_CACHE_LENGTH);
  75. }
  76. }
  77. /* Not fount in cache - insert */
  78. pCacheEntry = &pCache->asCacheEntry[pCache->uInPtr];
  79. pCacheEntry->wFmSequence = pMACHeader->wSeqCtl;
  80. memcpy(&(pCacheEntry->abyAddr2[0]), &(pMACHeader->abyAddr2[0]), ETH_ALEN);
  81. ADD_ONE_WITH_WRAP_AROUND(pCache->uInPtr, DUPLICATE_RX_CACHE_LENGTH);
  82. return false;
  83. }
  84. /*
  85. * Description:
  86. * Found if sequence number of received fragment packet in Defragment Database
  87. *
  88. * Parameters:
  89. * In:
  90. * pDevice - Pointer to adapter
  91. * pMACHeader - 802.11 MAC Header of received packet
  92. * Out:
  93. * none
  94. *
  95. * Return Value: index number in Defragment Database
  96. *
  97. */
  98. unsigned int WCTLuSearchDFCB(struct vnt_private *pDevice,
  99. PS802_11Header pMACHeader)
  100. {
  101. unsigned int ii;
  102. for (ii = 0; ii < pDevice->cbDFCB; ii++) {
  103. if (pDevice->sRxDFCB[ii].bInUse &&
  104. ether_addr_equal(pDevice->sRxDFCB[ii].abyAddr2,
  105. pMACHeader->abyAddr2)) {
  106. return ii;
  107. }
  108. }
  109. return pDevice->cbDFCB;
  110. }
  111. /*
  112. * Description:
  113. * Insert received fragment packet in Defragment Database
  114. *
  115. * Parameters:
  116. * In:
  117. * pDevice - Pointer to adapter
  118. * pMACHeader - 802.11 MAC Header of received packet
  119. * Out:
  120. * none
  121. *
  122. * Return Value: index number in Defragment Database
  123. *
  124. */
  125. unsigned int WCTLuInsertDFCB(struct vnt_private *pDevice, PS802_11Header pMACHeader)
  126. {
  127. unsigned int ii;
  128. if (pDevice->cbFreeDFCB == 0)
  129. return pDevice->cbDFCB;
  130. for (ii = 0; ii < pDevice->cbDFCB; ii++) {
  131. if (!pDevice->sRxDFCB[ii].bInUse) {
  132. pDevice->cbFreeDFCB--;
  133. pDevice->sRxDFCB[ii].uLifetime = pDevice->dwMaxReceiveLifetime;
  134. pDevice->sRxDFCB[ii].bInUse = true;
  135. pDevice->sRxDFCB[ii].wSequence = (pMACHeader->wSeqCtl >> 4);
  136. pDevice->sRxDFCB[ii].wFragNum = (pMACHeader->wSeqCtl & 0x000F);
  137. memcpy(&(pDevice->sRxDFCB[ii].abyAddr2[0]), &(pMACHeader->abyAddr2[0]), ETH_ALEN);
  138. return ii;
  139. }
  140. }
  141. return pDevice->cbDFCB;
  142. }
  143. /*
  144. * Description:
  145. * Handle received fragment packet
  146. *
  147. * Parameters:
  148. * In:
  149. * pDevice - Pointer to adapter
  150. * pMACHeader - 802.11 MAC Header of received packet
  151. * cbFrameLength - Frame length
  152. * bWEP - is WEP packet
  153. * Out:
  154. * none
  155. *
  156. * Return Value: true if it is valid fragment packet and we have resource to defragment; otherwise false
  157. *
  158. */
  159. bool WCTLbHandleFragment(struct vnt_private *pDevice, PS802_11Header pMACHeader,
  160. unsigned int cbFrameLength, bool bWEP, bool bExtIV)
  161. {
  162. unsigned int uHeaderSize;
  163. if (bWEP) {
  164. uHeaderSize = 28;
  165. if (bExtIV)
  166. // ExtIV
  167. uHeaderSize += 4;
  168. } else {
  169. uHeaderSize = 24;
  170. }
  171. if (IS_FIRST_FRAGMENT_PKT(pMACHeader)) {
  172. pDevice->uCurrentDFCBIdx = WCTLuSearchDFCB(pDevice, pMACHeader);
  173. if (pDevice->uCurrentDFCBIdx < pDevice->cbDFCB) {
  174. // duplicate, we must flush previous DCB
  175. pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].uLifetime = pDevice->dwMaxReceiveLifetime;
  176. pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].wSequence = (pMACHeader->wSeqCtl >> 4);
  177. pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].wFragNum = (pMACHeader->wSeqCtl & 0x000F);
  178. } else {
  179. pDevice->uCurrentDFCBIdx = WCTLuInsertDFCB(pDevice, pMACHeader);
  180. if (pDevice->uCurrentDFCBIdx == pDevice->cbDFCB)
  181. return false;
  182. }
  183. // reserve 4 byte to match MAC RX Buffer
  184. pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].pbyRxBuffer = (unsigned char *)(pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].skb->data + 4);
  185. memcpy(pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].pbyRxBuffer, pMACHeader, cbFrameLength);
  186. pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].cbFrameLength = cbFrameLength;
  187. pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].pbyRxBuffer += cbFrameLength;
  188. pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].wFragNum++;
  189. return false;
  190. } else {
  191. pDevice->uCurrentDFCBIdx = WCTLuSearchDFCB(pDevice, pMACHeader);
  192. if (pDevice->uCurrentDFCBIdx != pDevice->cbDFCB) {
  193. if ((pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].wSequence == (pMACHeader->wSeqCtl >> 4)) &&
  194. (pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].wFragNum == (pMACHeader->wSeqCtl & 0x000F)) &&
  195. ((pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].cbFrameLength + cbFrameLength - uHeaderSize) < 2346)) {
  196. memcpy(pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].pbyRxBuffer, ((unsigned char *)(pMACHeader) + uHeaderSize), (cbFrameLength - uHeaderSize));
  197. pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].cbFrameLength += (cbFrameLength - uHeaderSize);
  198. pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].pbyRxBuffer += (cbFrameLength - uHeaderSize);
  199. pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].wFragNum++;
  200. } else {
  201. // seq error or frag # error flush DFCB
  202. pDevice->cbFreeDFCB++;
  203. pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].bInUse = false;
  204. return false;
  205. }
  206. } else {
  207. return false;
  208. }
  209. if (IS_LAST_FRAGMENT_PKT(pMACHeader)) {
  210. //enq defragcontrolblock
  211. pDevice->cbFreeDFCB++;
  212. pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].bInUse = false;
  213. return true;
  214. }
  215. return false;
  216. }
  217. }