tether.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright (c) 2003 VIA Networking, Inc. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. *
  18. *
  19. * File: tether.c
  20. *
  21. * Purpose:
  22. *
  23. * Author: Tevin Chen
  24. *
  25. * Date: May 21, 1996
  26. *
  27. * Functions:
  28. * ETHbyGetHashIndexByCrc32 - Caculate multicast hash value by CRC32
  29. * ETHbIsBufferCrc32Ok - Check CRC value of the buffer if Ok or not
  30. *
  31. * Revision History:
  32. *
  33. */
  34. #include "device.h"
  35. #include "tmacro.h"
  36. #include "tcrc.h"
  37. #include "tether.h"
  38. /*--------------------- Static Definitions -------------------------*/
  39. /*--------------------- Static Classes ----------------------------*/
  40. /*--------------------- Static Variables --------------------------*/
  41. /*--------------------- Static Functions --------------------------*/
  42. /*--------------------- Export Variables --------------------------*/
  43. /*
  44. * Description: Caculate multicast hash value by CRC32
  45. *
  46. * Parameters:
  47. * In:
  48. * pbyMultiAddr - Multicast Address
  49. * Out:
  50. * none
  51. *
  52. * Return Value: Hash value
  53. *
  54. */
  55. unsigned char ETHbyGetHashIndexByCrc32 (unsigned char *pbyMultiAddr)
  56. {
  57. int ii;
  58. unsigned char byTmpHash;
  59. unsigned char byHash = 0;
  60. // get the least 6-bits from CRC generator
  61. byTmpHash = (unsigned char)(CRCdwCrc32(pbyMultiAddr, ETH_ALEN,
  62. 0xFFFFFFFFL) & 0x3F);
  63. // reverse most bit to least bit
  64. for (ii = 0; ii < (sizeof(byTmpHash) * 8); ii++) {
  65. byHash <<= 1;
  66. if (byTmpHash & 0x01)
  67. byHash |= 1;
  68. byTmpHash >>= 1;
  69. }
  70. // adjust 6-bits to the right most
  71. return (byHash >> 2);
  72. }
  73. /*
  74. * Description: Check CRC value of the buffer if Ok or not
  75. *
  76. * Parameters:
  77. * In:
  78. * pbyBuffer - pointer of buffer (normally is rx buffer)
  79. * cbFrameLength - length of buffer, including CRC portion
  80. * Out:
  81. * none
  82. *
  83. * Return Value: true if ok; false if error.
  84. *
  85. */
  86. bool ETHbIsBufferCrc32Ok (unsigned char *pbyBuffer, unsigned int cbFrameLength)
  87. {
  88. unsigned long dwCRC;
  89. dwCRC = CRCdwGetCrc32(pbyBuffer, cbFrameLength - 4);
  90. if (cpu_to_le32(*((unsigned long *)(pbyBuffer + cbFrameLength - 4))) != dwCRC) {
  91. return false;
  92. }
  93. return true;
  94. }