wctl.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 int msglevel =MSG_LEVEL_INFO;
  43. /*--------------------- Static Functions --------------------------*/
  44. /*--------------------- Export Variables --------------------------*/
  45. /*
  46. * Description:
  47. * Scan Rx cache. Return true if packet is duplicate, else
  48. * inserts in receive cache and returns false.
  49. *
  50. * Parameters:
  51. * In:
  52. * pCache - Receive packets history
  53. * pMACHeader - 802.11 MAC Header of received packet
  54. * Out:
  55. * none
  56. *
  57. * Return Value: true if packet duplicate; otherwise false
  58. *
  59. */
  60. bool WCTLbIsDuplicate (PSCache pCache, PS802_11Header pMACHeader)
  61. {
  62. unsigned int uIndex;
  63. unsigned int ii;
  64. PSCacheEntry pCacheEntry;
  65. if (IS_FC_RETRY(pMACHeader)) {
  66. uIndex = pCache->uInPtr;
  67. for (ii = 0; ii < DUPLICATE_RX_CACHE_LENGTH; ii++) {
  68. pCacheEntry = &(pCache->asCacheEntry[uIndex]);
  69. if ((pCacheEntry->wFmSequence == pMACHeader->wSeqCtl) &&
  70. (!compare_ether_addr(&(pCacheEntry->abyAddr2[0]), &(pMACHeader->abyAddr2[0])))
  71. ) {
  72. /* Duplicate match */
  73. return true;
  74. }
  75. ADD_ONE_WITH_WRAP_AROUND(uIndex, DUPLICATE_RX_CACHE_LENGTH);
  76. }
  77. }
  78. /* Not fount in cache - insert */
  79. pCacheEntry = &pCache->asCacheEntry[pCache->uInPtr];
  80. pCacheEntry->wFmSequence = pMACHeader->wSeqCtl;
  81. memcpy(&(pCacheEntry->abyAddr2[0]), &(pMACHeader->abyAddr2[0]), ETH_ALEN);
  82. ADD_ONE_WITH_WRAP_AROUND(pCache->uInPtr, DUPLICATE_RX_CACHE_LENGTH);
  83. return false;
  84. }
  85. /*
  86. * Description:
  87. * Found if sequence number of received fragment packet in Defragment Database
  88. *
  89. * Parameters:
  90. * In:
  91. * pDevice - Pointer to adapter
  92. * pMACHeader - 802.11 MAC Header of received packet
  93. * Out:
  94. * none
  95. *
  96. * Return Value: index number in Defragment Database
  97. *
  98. */
  99. unsigned int WCTLuSearchDFCB (PSDevice pDevice, PS802_11Header pMACHeader)
  100. {
  101. unsigned int ii;
  102. for(ii=0;ii<pDevice->cbDFCB;ii++) {
  103. if ((pDevice->sRxDFCB[ii].bInUse == true) &&
  104. (!compare_ether_addr(&(pDevice->sRxDFCB[ii].abyAddr2[0]), &(pMACHeader->abyAddr2[0])))
  105. ) {
  106. //
  107. return(ii);
  108. }
  109. }
  110. return(pDevice->cbDFCB);
  111. }
  112. /*
  113. * Description:
  114. * Insert received fragment packet in Defragment Database
  115. *
  116. * Parameters:
  117. * In:
  118. * pDevice - Pointer to adapter
  119. * pMACHeader - 802.11 MAC Header of received packet
  120. * Out:
  121. * none
  122. *
  123. * Return Value: index number in Defragment Database
  124. *
  125. */
  126. unsigned int WCTLuInsertDFCB (PSDevice pDevice, PS802_11Header pMACHeader)
  127. {
  128. unsigned int ii;
  129. if (pDevice->cbFreeDFCB == 0)
  130. return(pDevice->cbDFCB);
  131. for(ii=0;ii<pDevice->cbDFCB;ii++) {
  132. if (pDevice->sRxDFCB[ii].bInUse == false) {
  133. pDevice->cbFreeDFCB--;
  134. pDevice->sRxDFCB[ii].uLifetime = pDevice->dwMaxReceiveLifetime;
  135. pDevice->sRxDFCB[ii].bInUse = true;
  136. pDevice->sRxDFCB[ii].wSequence = (pMACHeader->wSeqCtl >> 4);
  137. pDevice->sRxDFCB[ii].wFragNum = (pMACHeader->wSeqCtl & 0x000F);
  138. memcpy(&(pDevice->sRxDFCB[ii].abyAddr2[0]), &(pMACHeader->abyAddr2[0]), ETH_ALEN);
  139. return(ii);
  140. }
  141. }
  142. return(pDevice->cbDFCB);
  143. }
  144. /*
  145. * Description:
  146. * Handle received fragment packet
  147. *
  148. * Parameters:
  149. * In:
  150. * pDevice - Pointer to adapter
  151. * pMACHeader - 802.11 MAC Header of received packet
  152. * cbFrameLength - Frame length
  153. * bWEP - is WEP packet
  154. * Out:
  155. * none
  156. *
  157. * Return Value: true if it is valid fragment packet and we have resource to defragment; otherwise false
  158. *
  159. */
  160. bool WCTLbHandleFragment (PSDevice pDevice, PS802_11Header pMACHeader, unsigned int cbFrameLength, bool bWEP, bool bExtIV)
  161. {
  162. unsigned int uHeaderSize;
  163. if (bWEP == true) {
  164. uHeaderSize = 28;
  165. if (bExtIV)
  166. // ExtIV
  167. uHeaderSize +=4;
  168. }
  169. else {
  170. uHeaderSize = 24;
  171. }
  172. if (IS_FIRST_FRAGMENT_PKT(pMACHeader)) {
  173. pDevice->uCurrentDFCBIdx = WCTLuSearchDFCB(pDevice, pMACHeader);
  174. if (pDevice->uCurrentDFCBIdx < pDevice->cbDFCB) {
  175. // duplicate, we must flush previous DCB
  176. pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].uLifetime = pDevice->dwMaxReceiveLifetime;
  177. pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].wSequence = (pMACHeader->wSeqCtl >> 4);
  178. pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].wFragNum = (pMACHeader->wSeqCtl & 0x000F);
  179. }
  180. else {
  181. pDevice->uCurrentDFCBIdx = WCTLuInsertDFCB(pDevice, pMACHeader);
  182. if (pDevice->uCurrentDFCBIdx == pDevice->cbDFCB) {
  183. return(false);
  184. }
  185. }
  186. // reserve 4 byte to match MAC RX Buffer
  187. pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].pbyRxBuffer = (unsigned char *) (pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].skb->data + 4);
  188. memcpy(pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].pbyRxBuffer, pMACHeader, cbFrameLength);
  189. pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].cbFrameLength = cbFrameLength;
  190. pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].pbyRxBuffer += cbFrameLength;
  191. pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].wFragNum++;
  192. //DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "First pDevice->uCurrentDFCBIdx= %d\n", pDevice->uCurrentDFCBIdx);
  193. return(false);
  194. }
  195. else {
  196. pDevice->uCurrentDFCBIdx = WCTLuSearchDFCB(pDevice, pMACHeader);
  197. if (pDevice->uCurrentDFCBIdx != pDevice->cbDFCB) {
  198. if ((pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].wSequence == (pMACHeader->wSeqCtl >> 4)) &&
  199. (pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].wFragNum == (pMACHeader->wSeqCtl & 0x000F)) &&
  200. ((pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].cbFrameLength + cbFrameLength - uHeaderSize) < 2346)) {
  201. memcpy(pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].pbyRxBuffer, ((unsigned char *) (pMACHeader) + uHeaderSize), (cbFrameLength - uHeaderSize));
  202. pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].cbFrameLength += (cbFrameLength - uHeaderSize);
  203. pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].pbyRxBuffer += (cbFrameLength - uHeaderSize);
  204. pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].wFragNum++;
  205. //DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Second pDevice->uCurrentDFCBIdx= %d\n", pDevice->uCurrentDFCBIdx);
  206. }
  207. else {
  208. // seq error or frag # error flush DFCB
  209. pDevice->cbFreeDFCB++;
  210. pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].bInUse = false;
  211. return(false);
  212. }
  213. }
  214. else {
  215. return(false);
  216. }
  217. if (IS_LAST_FRAGMENT_PKT(pMACHeader)) {
  218. //enq defragcontrolblock
  219. pDevice->cbFreeDFCB++;
  220. pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].bInUse = false;
  221. //DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Last pDevice->uCurrentDFCBIdx= %d\n", pDevice->uCurrentDFCBIdx);
  222. return(true);
  223. }
  224. return(false);
  225. }
  226. }