skcsum.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  1. /******************************************************************************
  2. *
  3. * Name: skcsum.c
  4. * Project: GEnesis, PCI Gigabit Ethernet Adapter
  5. * Version: $Revision: 1.10 $
  6. * Date: $Date: 2002/04/11 10:02:04 $
  7. * Purpose: Store/verify Internet checksum in send/receive packets.
  8. *
  9. ******************************************************************************/
  10. /******************************************************************************
  11. *
  12. * (C)Copyright 1998-2001 SysKonnect GmbH.
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * The information in this file is provided "AS IS" without warranty.
  20. *
  21. ******************************************************************************/
  22. /******************************************************************************
  23. *
  24. * History:
  25. *
  26. * $Log: skcsum.c,v $
  27. * Revision 1.10 2002/04/11 10:02:04 rwahl
  28. * Fix in SkCsGetSendInfo():
  29. * - function did not return ProtocolFlags in every case.
  30. * - pseudo header csum calculated wrong for big endian.
  31. *
  32. * Revision 1.9 2001/06/13 07:42:08 gklug
  33. * fix: NetNumber was wrong in CLEAR_STAT event
  34. * add: check for good NetNumber in Clear STAT
  35. *
  36. * Revision 1.8 2001/02/06 11:15:36 rassmann
  37. * Supporting two nets on dual-port adapters.
  38. *
  39. * Revision 1.7 2000/06/29 13:17:05 rassmann
  40. * Corrected reception of a packet with UDP checksum == 0 (which means there
  41. * is no UDP checksum).
  42. *
  43. * Revision 1.6 2000/02/21 12:35:10 cgoos
  44. * Fixed license header comment.
  45. *
  46. * Revision 1.5 2000/02/21 11:05:19 cgoos
  47. * Merged changes back to common source.
  48. * Fixed rx path for BIG ENDIAN architecture.
  49. *
  50. * Revision 1.1 1999/07/26 15:28:12 mkarl
  51. * added return SKCS_STATUS_IP_CSUM_ERROR_UDP and
  52. * SKCS_STATUS_IP_CSUM_ERROR_TCP to pass the NidsTester
  53. * changed from common source to windows specific source
  54. * therefore restarting with v1.0
  55. *
  56. * Revision 1.3 1999/05/10 08:39:33 mkarl
  57. * prevent overflows in SKCS_HTON16
  58. * fixed a bug in pseudo header checksum calculation
  59. * added some comments
  60. *
  61. * Revision 1.2 1998/10/22 11:53:28 swolf
  62. * Now using SK_DBG_MSG.
  63. *
  64. * Revision 1.1 1998/09/01 15:35:41 swolf
  65. * initial revision
  66. *
  67. * 13-May-1998 sw Created.
  68. *
  69. ******************************************************************************/
  70. #include <config.h>
  71. #ifdef SK_USE_CSUM /* Check if CSUM is to be used. */
  72. #ifndef lint
  73. static const char SysKonnectFileId[] = "@(#)"
  74. "$Id: skcsum.c,v 1.10 2002/04/11 10:02:04 rwahl Exp $"
  75. " (C) SysKonnect.";
  76. #endif /* !lint */
  77. /******************************************************************************
  78. *
  79. * Description:
  80. *
  81. * This is the "GEnesis" common module "CSUM".
  82. *
  83. * This module contains the code necessary to calculate, store, and verify the
  84. * Internet Checksum of IP, TCP, and UDP frames.
  85. *
  86. * "GEnesis" is an abbreviation of "Gigabit Ethernet Network System in Silicon"
  87. * and is the code name of this SysKonnect project.
  88. *
  89. * Compilation Options:
  90. *
  91. * SK_USE_CSUM - Define if CSUM is to be used. Otherwise, CSUM will be an
  92. * empty module.
  93. *
  94. * SKCS_OVERWRITE_PROTO - Define to overwrite the default protocol id
  95. * definitions. In this case, all SKCS_PROTO_xxx definitions must be made
  96. * external.
  97. *
  98. * SKCS_OVERWRITE_STATUS - Define to overwrite the default return status
  99. * definitions. In this case, all SKCS_STATUS_xxx definitions must be made
  100. * external.
  101. *
  102. * Include File Hierarchy:
  103. *
  104. * "h/skdrv1st.h"
  105. * "h/skcsum.h"
  106. * "h/sktypes.h"
  107. * "h/skqueue.h"
  108. * "h/skdrv2nd.h"
  109. *
  110. ******************************************************************************/
  111. #include "h/skdrv1st.h"
  112. #include "h/skcsum.h"
  113. #include "h/skdrv2nd.h"
  114. /* defines ********************************************************************/
  115. /* The size of an Ethernet MAC header. */
  116. #define SKCS_ETHERNET_MAC_HEADER_SIZE (6+6+2)
  117. /* The size of the used topology's MAC header. */
  118. #define SKCS_MAC_HEADER_SIZE SKCS_ETHERNET_MAC_HEADER_SIZE
  119. /* The size of the IP header without any option fields. */
  120. #define SKCS_IP_HEADER_SIZE 20
  121. /*
  122. * Field offsets within the IP header.
  123. */
  124. /* "Internet Header Version" and "Length". */
  125. #define SKCS_OFS_IP_HEADER_VERSION_AND_LENGTH 0
  126. /* "Total Length". */
  127. #define SKCS_OFS_IP_TOTAL_LENGTH 2
  128. /* "Flags" "Fragment Offset". */
  129. #define SKCS_OFS_IP_FLAGS_AND_FRAGMENT_OFFSET 6
  130. /* "Next Level Protocol" identifier. */
  131. #define SKCS_OFS_IP_NEXT_LEVEL_PROTOCOL 9
  132. /* Source IP address. */
  133. #define SKCS_OFS_IP_SOURCE_ADDRESS 12
  134. /* Destination IP address. */
  135. #define SKCS_OFS_IP_DESTINATION_ADDRESS 16
  136. /*
  137. * Field offsets within the UDP header.
  138. */
  139. /* UDP checksum. */
  140. #define SKCS_OFS_UDP_CHECKSUM 6
  141. /* IP "Next Level Protocol" identifiers (see RFC 790). */
  142. #define SKCS_PROTO_ID_TCP 6 /* Transport Control Protocol */
  143. #define SKCS_PROTO_ID_UDP 17 /* User Datagram Protocol */
  144. /* IP "Don't Fragment" bit. */
  145. #define SKCS_IP_DONT_FRAGMENT SKCS_HTON16(0x4000)
  146. /* Add a byte offset to a pointer. */
  147. #define SKCS_IDX(pPtr, Ofs) ((void *) ((char *) (pPtr) + (Ofs)))
  148. /*
  149. * Macros that convert host to network representation and vice versa, i.e.
  150. * little/big endian conversion on little endian machines only.
  151. */
  152. #ifdef SK_LITTLE_ENDIAN
  153. #define SKCS_HTON16(Val16) (((unsigned) (Val16) >> 8) | (((Val16) & 0xFF) << 8))
  154. #endif /* SK_LITTLE_ENDIAN */
  155. #ifdef SK_BIG_ENDIAN
  156. #define SKCS_HTON16(Val16) (Val16)
  157. #endif /* SK_BIG_ENDIAN */
  158. #define SKCS_NTOH16(Val16) SKCS_HTON16(Val16)
  159. /* typedefs *******************************************************************/
  160. /* function prototypes ********************************************************/
  161. /******************************************************************************
  162. *
  163. * SkCsGetSendInfo - get checksum information for a send packet
  164. *
  165. * Description:
  166. * Get all checksum information necessary to send a TCP or UDP packet. The
  167. * function checks the IP header passed to it. If the high-level protocol
  168. * is either TCP or UDP the pseudo header checksum is calculated and
  169. * returned.
  170. *
  171. * The function returns the total length of the IP header (including any
  172. * IP option fields), which is the same as the start offset of the IP data
  173. * which in turn is the start offset of the TCP or UDP header.
  174. *
  175. * The function also returns the TCP or UDP pseudo header checksum, which
  176. * should be used as the start value for the hardware checksum calculation.
  177. * (Note that any actual pseudo header checksum can never calculate to
  178. * zero.)
  179. *
  180. * Note:
  181. * There is a bug in the ASIC which may lead to wrong checksums.
  182. *
  183. * Arguments:
  184. * pAc - A pointer to the adapter context struct.
  185. *
  186. * pIpHeader - Pointer to IP header. Must be at least the IP header *not*
  187. * including any option fields, i.e. at least 20 bytes.
  188. *
  189. * Note: This pointer will be used to address 8-, 16-, and 32-bit
  190. * variables with the respective alignment offsets relative to the pointer.
  191. * Thus, the pointer should point to a 32-bit aligned address. If the
  192. * target system cannot address 32-bit variables on non 32-bit aligned
  193. * addresses, then the pointer *must* point to a 32-bit aligned address.
  194. *
  195. * pPacketInfo - A pointer to the packet information structure for this
  196. * packet. Before calling this SkCsGetSendInfo(), the following field must
  197. * be initialized:
  198. *
  199. * ProtocolFlags - Initialize with any combination of
  200. * SKCS_PROTO_XXX bit flags. SkCsGetSendInfo() will only work on
  201. * the protocols specified here. Any protocol(s) not specified
  202. * here will be ignored.
  203. *
  204. * Note: Only one checksum can be calculated in hardware. Thus, if
  205. * SKCS_PROTO_IP is specified in the 'ProtocolFlags',
  206. * SkCsGetSendInfo() must calculate the IP header checksum in
  207. * software. It might be a better idea to have the calling
  208. * protocol stack calculate the IP header checksum.
  209. *
  210. * Returns: N/A
  211. * On return, the following fields in 'pPacketInfo' may or may not have
  212. * been filled with information, depending on the protocol(s) found in the
  213. * packet:
  214. *
  215. * ProtocolFlags - Returns the SKCS_PROTO_XXX bit flags of the protocol(s)
  216. * that were both requested by the caller and actually found in the packet.
  217. * Protocol(s) not specified by the caller and/or not found in the packet
  218. * will have their respective SKCS_PROTO_XXX bit flags reset.
  219. *
  220. * Note: For IP fragments, TCP and UDP packet information is ignored.
  221. *
  222. * IpHeaderLength - The total length in bytes of the complete IP header
  223. * including any option fields is returned here. This is the start offset
  224. * of the IP data, i.e. the TCP or UDP header if present.
  225. *
  226. * IpHeaderChecksum - If IP has been specified in the 'ProtocolFlags', the
  227. * 16-bit Internet Checksum of the IP header is returned here. This value
  228. * is to be stored into the packet's 'IP Header Checksum' field.
  229. *
  230. * PseudoHeaderChecksum - If this is a TCP or UDP packet and if TCP or UDP
  231. * has been specified in the 'ProtocolFlags', the 16-bit Internet Checksum
  232. * of the TCP or UDP pseudo header is returned here.
  233. */
  234. #if 0
  235. void SkCsGetSendInfo(
  236. SK_AC *pAc, /* Adapter context struct. */
  237. void *pIpHeader, /* IP header. */
  238. SKCS_PACKET_INFO *pPacketInfo, /* Packet information struct. */
  239. int NetNumber) /* Net number */
  240. {
  241. /* Internet Header Version found in IP header. */
  242. unsigned InternetHeaderVersion;
  243. /* Length of the IP header as found in IP header. */
  244. unsigned IpHeaderLength;
  245. /* Bit field specifiying the desired/found protocols. */
  246. unsigned ProtocolFlags;
  247. /* Next level protocol identifier found in IP header. */
  248. unsigned NextLevelProtocol;
  249. /* Length of IP data portion. */
  250. unsigned IpDataLength;
  251. /* TCP/UDP pseudo header checksum. */
  252. unsigned long PseudoHeaderChecksum;
  253. /* Pointer to next level protocol statistics structure. */
  254. SKCS_PROTO_STATS *NextLevelProtoStats;
  255. /* Temporary variable. */
  256. unsigned Tmp;
  257. Tmp = *(SK_U8 *)
  258. SKCS_IDX(pIpHeader, SKCS_OFS_IP_HEADER_VERSION_AND_LENGTH);
  259. /* Get the Internet Header Version (IHV). */
  260. /* Note: The IHV is stored in the upper four bits. */
  261. InternetHeaderVersion = Tmp >> 4;
  262. /* Check the Internet Header Version. */
  263. /* Note: We currently only support IP version 4. */
  264. if (InternetHeaderVersion != 4) { /* IPv4? */
  265. SK_DBG_MSG(pAc, SK_DBGMOD_CSUM, SK_DBGCAT_ERR | SK_DBGCAT_TX,
  266. ("Tx: Unknown Internet Header Version %u.\n",
  267. InternetHeaderVersion));
  268. pPacketInfo->ProtocolFlags = 0;
  269. pAc->Csum.ProtoStats[NetNumber][SKCS_PROTO_STATS_IP].TxUnableCts++;
  270. return;
  271. }
  272. /* Get the IP header length (IHL). */
  273. /*
  274. * Note: The IHL is stored in the lower four bits as the number of
  275. * 4-byte words.
  276. */
  277. IpHeaderLength = (Tmp & 0xf) * 4;
  278. pPacketInfo->IpHeaderLength = IpHeaderLength;
  279. /* Check the IP header length. */
  280. /* 04-Aug-1998 sw - Really check the IHL? Necessary? */
  281. if (IpHeaderLength < 5*4) {
  282. SK_DBG_MSG(pAc, SK_DBGMOD_CSUM, SK_DBGCAT_ERR | SK_DBGCAT_TX,
  283. ("Tx: Invalid IP Header Length %u.\n", IpHeaderLength));
  284. pPacketInfo->ProtocolFlags = 0;
  285. pAc->Csum.ProtoStats[NetNumber][SKCS_PROTO_STATS_IP].TxUnableCts++;
  286. return;
  287. }
  288. /* This is an IPv4 frame with a header of valid length. */
  289. pAc->Csum.ProtoStats[NetNumber][SKCS_PROTO_STATS_IP].TxOkCts++;
  290. /* Check if we should calculate the IP header checksum. */
  291. ProtocolFlags = pPacketInfo->ProtocolFlags;
  292. if (ProtocolFlags & SKCS_PROTO_IP) {
  293. pPacketInfo->IpHeaderChecksum =
  294. SkCsCalculateChecksum(pIpHeader, IpHeaderLength);
  295. }
  296. /* Get the next level protocol identifier. */
  297. NextLevelProtocol =
  298. *(SK_U8 *) SKCS_IDX(pIpHeader, SKCS_OFS_IP_NEXT_LEVEL_PROTOCOL);
  299. /*
  300. * Check if this is a TCP or UDP frame and if we should calculate the
  301. * TCP/UDP pseudo header checksum.
  302. *
  303. * Also clear all protocol bit flags of protocols not present in the
  304. * frame.
  305. */
  306. if ((ProtocolFlags & SKCS_PROTO_TCP) != 0 &&
  307. NextLevelProtocol == SKCS_PROTO_ID_TCP) {
  308. /* TCP/IP frame. */
  309. ProtocolFlags &= SKCS_PROTO_TCP | SKCS_PROTO_IP;
  310. NextLevelProtoStats =
  311. &pAc->Csum.ProtoStats[NetNumber][SKCS_PROTO_STATS_TCP];
  312. }
  313. else if ((ProtocolFlags & SKCS_PROTO_UDP) != 0 &&
  314. NextLevelProtocol == SKCS_PROTO_ID_UDP) {
  315. /* UDP/IP frame. */
  316. ProtocolFlags &= SKCS_PROTO_UDP | SKCS_PROTO_IP;
  317. NextLevelProtoStats =
  318. &pAc->Csum.ProtoStats[NetNumber][SKCS_PROTO_STATS_UDP];
  319. }
  320. else {
  321. /*
  322. * Either not a TCP or UDP frame and/or TCP/UDP processing not
  323. * specified.
  324. */
  325. pPacketInfo->ProtocolFlags = ProtocolFlags & SKCS_PROTO_IP;
  326. return;
  327. }
  328. /* Check if this is an IP fragment. */
  329. /*
  330. * Note: An IP fragment has a non-zero "Fragment Offset" field and/or
  331. * the "More Fragments" bit set. Thus, if both the "Fragment Offset"
  332. * and the "More Fragments" are zero, it is *not* a fragment. We can
  333. * easily check both at the same time since they are in the same 16-bit
  334. * word.
  335. */
  336. if ((*(SK_U16 *)
  337. SKCS_IDX(pIpHeader, SKCS_OFS_IP_FLAGS_AND_FRAGMENT_OFFSET) &
  338. ~SKCS_IP_DONT_FRAGMENT) != 0) {
  339. /* IP fragment; ignore all other protocols. */
  340. pPacketInfo->ProtocolFlags = ProtocolFlags & SKCS_PROTO_IP;
  341. NextLevelProtoStats->TxUnableCts++;
  342. return;
  343. }
  344. /*
  345. * Calculate the TCP/UDP pseudo header checksum.
  346. */
  347. /* Get total length of IP header and data. */
  348. IpDataLength =
  349. *(SK_U16 *) SKCS_IDX(pIpHeader, SKCS_OFS_IP_TOTAL_LENGTH);
  350. /* Get length of IP data portion. */
  351. IpDataLength = SKCS_NTOH16(IpDataLength) - IpHeaderLength;
  352. /* Calculate the sum of all pseudo header fields (16-bit). */
  353. PseudoHeaderChecksum =
  354. (unsigned long) *(SK_U16 *) SKCS_IDX(pIpHeader,
  355. SKCS_OFS_IP_SOURCE_ADDRESS + 0) +
  356. (unsigned long) *(SK_U16 *) SKCS_IDX(pIpHeader,
  357. SKCS_OFS_IP_SOURCE_ADDRESS + 2) +
  358. (unsigned long) *(SK_U16 *) SKCS_IDX(pIpHeader,
  359. SKCS_OFS_IP_DESTINATION_ADDRESS + 0) +
  360. (unsigned long) *(SK_U16 *) SKCS_IDX(pIpHeader,
  361. SKCS_OFS_IP_DESTINATION_ADDRESS + 2) +
  362. (unsigned long) SKCS_HTON16(NextLevelProtocol) +
  363. (unsigned long) SKCS_HTON16(IpDataLength);
  364. /* Add-in any carries. */
  365. SKCS_OC_ADD(PseudoHeaderChecksum, PseudoHeaderChecksum, 0);
  366. /* Add-in any new carry. */
  367. SKCS_OC_ADD(pPacketInfo->PseudoHeaderChecksum, PseudoHeaderChecksum, 0);
  368. pPacketInfo->ProtocolFlags = ProtocolFlags;
  369. NextLevelProtoStats->TxOkCts++; /* Success. */
  370. } /* SkCsGetSendInfo */
  371. /******************************************************************************
  372. *
  373. * SkCsGetReceiveInfo - verify checksum information for a received packet
  374. *
  375. * Description:
  376. * Verify a received frame's checksum. The function returns a status code
  377. * reflecting the result of the verification.
  378. *
  379. * Note:
  380. * Before calling this function you have to verify that the frame is
  381. * not padded and Checksum1 and Checksum2 are bigger than 1.
  382. *
  383. * Arguments:
  384. * pAc - Pointer to adapter context struct.
  385. *
  386. * pIpHeader - Pointer to IP header. Must be at least the length in bytes
  387. * of the received IP header including any option fields. For UDP packets,
  388. * 8 additional bytes are needed to access the UDP checksum.
  389. *
  390. * Note: The actual length of the IP header is stored in the lower four
  391. * bits of the first octet of the IP header as the number of 4-byte words,
  392. * so it must be multiplied by four to get the length in bytes. Thus, the
  393. * maximum IP header length is 15 * 4 = 60 bytes.
  394. *
  395. * Checksum1 - The first 16-bit Internet Checksum calculated by the
  396. * hardware starting at the offset returned by SkCsSetReceiveFlags().
  397. *
  398. * Checksum2 - The second 16-bit Internet Checksum calculated by the
  399. * hardware starting at the offset returned by SkCsSetReceiveFlags().
  400. *
  401. * Returns:
  402. * SKCS_STATUS_UNKNOWN_IP_VERSION - Not an IP v4 frame.
  403. * SKCS_STATUS_IP_CSUM_ERROR - IP checksum error.
  404. * SKCS_STATUS_IP_CSUM_ERROR_TCP - IP checksum error in TCP frame.
  405. * SKCS_STATUS_IP_CSUM_ERROR_UDP - IP checksum error in UDP frame
  406. * SKCS_STATUS_IP_FRAGMENT - IP fragment (IP checksum ok).
  407. * SKCS_STATUS_IP_CSUM_OK - IP checksum ok (not a TCP or UDP frame).
  408. * SKCS_STATUS_TCP_CSUM_ERROR - TCP checksum error (IP checksum ok).
  409. * SKCS_STATUS_UDP_CSUM_ERROR - UDP checksum error (IP checksum ok).
  410. * SKCS_STATUS_TCP_CSUM_OK - IP and TCP checksum ok.
  411. * SKCS_STATUS_UDP_CSUM_OK - IP and UDP checksum ok.
  412. * SKCS_STATUS_IP_CSUM_OK_NO_UDP - IP checksum OK and no UDP checksum.
  413. *
  414. * Note: If SKCS_OVERWRITE_STATUS is defined, the SKCS_STATUS_XXX values
  415. * returned here can be defined in some header file by the module using CSUM.
  416. * In this way, the calling module can assign return values for its own needs,
  417. * e.g. by assigning bit flags to the individual protocols.
  418. */
  419. SKCS_STATUS SkCsGetReceiveInfo(
  420. SK_AC *pAc, /* Adapter context struct. */
  421. void *pIpHeader, /* IP header. */
  422. unsigned Checksum1, /* Hardware checksum 1. */
  423. unsigned Checksum2, /* Hardware checksum 2. */
  424. int NetNumber) /* Net number */
  425. {
  426. /* Internet Header Version found in IP header. */
  427. unsigned InternetHeaderVersion;
  428. /* Length of the IP header as found in IP header. */
  429. unsigned IpHeaderLength;
  430. /* Length of IP data portion. */
  431. unsigned IpDataLength;
  432. /* IP header checksum. */
  433. unsigned IpHeaderChecksum;
  434. /* IP header options checksum, if any. */
  435. unsigned IpOptionsChecksum;
  436. /* IP data checksum, i.e. TCP/UDP checksum. */
  437. unsigned IpDataChecksum;
  438. /* Next level protocol identifier found in IP header. */
  439. unsigned NextLevelProtocol;
  440. /* The checksum of the "next level protocol", i.e. TCP or UDP. */
  441. unsigned long NextLevelProtocolChecksum;
  442. /* Pointer to next level protocol statistics structure. */
  443. SKCS_PROTO_STATS *NextLevelProtoStats;
  444. /* Temporary variable. */
  445. unsigned Tmp;
  446. Tmp = *(SK_U8 *)
  447. SKCS_IDX(pIpHeader, SKCS_OFS_IP_HEADER_VERSION_AND_LENGTH);
  448. /* Get the Internet Header Version (IHV). */
  449. /* Note: The IHV is stored in the upper four bits. */
  450. InternetHeaderVersion = Tmp >> 4;
  451. /* Check the Internet Header Version. */
  452. /* Note: We currently only support IP version 4. */
  453. if (InternetHeaderVersion != 4) { /* IPv4? */
  454. SK_DBG_MSG(pAc, SK_DBGMOD_CSUM, SK_DBGCAT_ERR | SK_DBGCAT_RX,
  455. ("Rx: Unknown Internet Header Version %u.\n",
  456. InternetHeaderVersion));
  457. pAc->Csum.ProtoStats[NetNumber][SKCS_PROTO_STATS_IP].RxUnableCts++;
  458. return (SKCS_STATUS_UNKNOWN_IP_VERSION);
  459. }
  460. /* Get the IP header length (IHL). */
  461. /*
  462. * Note: The IHL is stored in the lower four bits as the number of
  463. * 4-byte words.
  464. */
  465. IpHeaderLength = (Tmp & 0xf) * 4;
  466. /* Check the IP header length. */
  467. /* 04-Aug-1998 sw - Really check the IHL? Necessary? */
  468. if (IpHeaderLength < 5*4) {
  469. SK_DBG_MSG(pAc, SK_DBGMOD_CSUM, SK_DBGCAT_ERR | SK_DBGCAT_RX,
  470. ("Rx: Invalid IP Header Length %u.\n", IpHeaderLength));
  471. pAc->Csum.ProtoStats[NetNumber][SKCS_PROTO_STATS_IP].RxErrCts++;
  472. return (SKCS_STATUS_IP_CSUM_ERROR);
  473. }
  474. /* This is an IPv4 frame with a header of valid length. */
  475. /* Get the IP header and data checksum. */
  476. IpDataChecksum = Checksum2;
  477. /*
  478. * The IP header checksum is calculated as follows:
  479. *
  480. * IpHeaderChecksum = Checksum1 - Checksum2
  481. */
  482. SKCS_OC_SUB(IpHeaderChecksum, Checksum1, Checksum2);
  483. /* Check if any IP header options. */
  484. if (IpHeaderLength > SKCS_IP_HEADER_SIZE) {
  485. /* Get the IP options checksum. */
  486. IpOptionsChecksum = SkCsCalculateChecksum(
  487. SKCS_IDX(pIpHeader, SKCS_IP_HEADER_SIZE),
  488. IpHeaderLength - SKCS_IP_HEADER_SIZE);
  489. /* Adjust the IP header and IP data checksums. */
  490. SKCS_OC_ADD(IpHeaderChecksum, IpHeaderChecksum, IpOptionsChecksum);
  491. SKCS_OC_SUB(IpDataChecksum, IpDataChecksum, IpOptionsChecksum);
  492. }
  493. /*
  494. * Check if the IP header checksum is ok.
  495. *
  496. * NOTE: We must check the IP header checksum even if the caller just wants
  497. * us to check upper-layer checksums, because we cannot do any further
  498. * processing of the packet without a valid IP checksum.
  499. */
  500. /* Get the next level protocol identifier. */
  501. NextLevelProtocol = *(SK_U8 *)
  502. SKCS_IDX(pIpHeader, SKCS_OFS_IP_NEXT_LEVEL_PROTOCOL);
  503. if (IpHeaderChecksum != 0xFFFF) {
  504. pAc->Csum.ProtoStats[NetNumber][SKCS_PROTO_STATS_IP].RxErrCts++;
  505. /* the NDIS tester wants to know the upper level protocol too */
  506. if (NextLevelProtocol == SKCS_PROTO_ID_TCP) {
  507. return(SKCS_STATUS_IP_CSUM_ERROR_TCP);
  508. }
  509. else if (NextLevelProtocol == SKCS_PROTO_ID_UDP) {
  510. return(SKCS_STATUS_IP_CSUM_ERROR_UDP);
  511. }
  512. return (SKCS_STATUS_IP_CSUM_ERROR);
  513. }
  514. /*
  515. * Check if this is a TCP or UDP frame and if we should calculate the
  516. * TCP/UDP pseudo header checksum.
  517. *
  518. * Also clear all protocol bit flags of protocols not present in the
  519. * frame.
  520. */
  521. if ((pAc->Csum.ReceiveFlags[NetNumber] & SKCS_PROTO_TCP) != 0 &&
  522. NextLevelProtocol == SKCS_PROTO_ID_TCP) {
  523. /* TCP/IP frame. */
  524. NextLevelProtoStats =
  525. &pAc->Csum.ProtoStats[NetNumber][SKCS_PROTO_STATS_TCP];
  526. }
  527. else if ((pAc->Csum.ReceiveFlags[NetNumber] & SKCS_PROTO_UDP) != 0 &&
  528. NextLevelProtocol == SKCS_PROTO_ID_UDP) {
  529. /* UDP/IP frame. */
  530. NextLevelProtoStats =
  531. &pAc->Csum.ProtoStats[NetNumber][SKCS_PROTO_STATS_UDP];
  532. }
  533. else {
  534. /*
  535. * Either not a TCP or UDP frame and/or TCP/UDP processing not
  536. * specified.
  537. */
  538. return (SKCS_STATUS_IP_CSUM_OK);
  539. }
  540. /* Check if this is an IP fragment. */
  541. /*
  542. * Note: An IP fragment has a non-zero "Fragment Offset" field and/or
  543. * the "More Fragments" bit set. Thus, if both the "Fragment Offset"
  544. * and the "More Fragments" are zero, it is *not* a fragment. We can
  545. * easily check both at the same time since they are in the same 16-bit
  546. * word.
  547. */
  548. if ((*(SK_U16 *)
  549. SKCS_IDX(pIpHeader, SKCS_OFS_IP_FLAGS_AND_FRAGMENT_OFFSET) &
  550. ~SKCS_IP_DONT_FRAGMENT) != 0) {
  551. /* IP fragment; ignore all other protocols. */
  552. NextLevelProtoStats->RxUnableCts++;
  553. return (SKCS_STATUS_IP_FRAGMENT);
  554. }
  555. /*
  556. * 08-May-2000 ra
  557. *
  558. * From RFC 768 (UDP)
  559. * If the computed checksum is zero, it is transmitted as all ones (the
  560. * equivalent in one's complement arithmetic). An all zero transmitted
  561. * checksum value means that the transmitter generated no checksum (for
  562. * debugging or for higher level protocols that don't care).
  563. */
  564. if (NextLevelProtocol == SKCS_PROTO_ID_UDP &&
  565. *(SK_U16*)SKCS_IDX(pIpHeader, IpHeaderLength + 6) == 0x0000) {
  566. NextLevelProtoStats->RxOkCts++;
  567. return (SKCS_STATUS_IP_CSUM_OK_NO_UDP);
  568. }
  569. /*
  570. * Calculate the TCP/UDP checksum.
  571. */
  572. /* Get total length of IP header and data. */
  573. IpDataLength =
  574. *(SK_U16 *) SKCS_IDX(pIpHeader, SKCS_OFS_IP_TOTAL_LENGTH);
  575. /* Get length of IP data portion. */
  576. IpDataLength = SKCS_NTOH16(IpDataLength) - IpHeaderLength;
  577. NextLevelProtocolChecksum =
  578. /* Calculate the pseudo header checksum. */
  579. (unsigned long) *(SK_U16 *) SKCS_IDX(pIpHeader,
  580. SKCS_OFS_IP_SOURCE_ADDRESS + 0) +
  581. (unsigned long) *(SK_U16 *) SKCS_IDX(pIpHeader,
  582. SKCS_OFS_IP_SOURCE_ADDRESS + 2) +
  583. (unsigned long) *(SK_U16 *) SKCS_IDX(pIpHeader,
  584. SKCS_OFS_IP_DESTINATION_ADDRESS + 0) +
  585. (unsigned long) *(SK_U16 *) SKCS_IDX(pIpHeader,
  586. SKCS_OFS_IP_DESTINATION_ADDRESS + 2) +
  587. (unsigned long) SKCS_HTON16(NextLevelProtocol) +
  588. (unsigned long) SKCS_HTON16(IpDataLength) +
  589. /* Add the TCP/UDP header checksum. */
  590. (unsigned long) IpDataChecksum;
  591. /* Add-in any carries. */
  592. SKCS_OC_ADD(NextLevelProtocolChecksum, NextLevelProtocolChecksum, 0);
  593. /* Add-in any new carry. */
  594. SKCS_OC_ADD(NextLevelProtocolChecksum, NextLevelProtocolChecksum, 0);
  595. /* Check if the TCP/UDP checksum is ok. */
  596. if ((unsigned) NextLevelProtocolChecksum == 0xFFFF) {
  597. /* TCP/UDP checksum ok. */
  598. NextLevelProtoStats->RxOkCts++;
  599. return (NextLevelProtocol == SKCS_PROTO_ID_TCP ?
  600. SKCS_STATUS_TCP_CSUM_OK : SKCS_STATUS_UDP_CSUM_OK);
  601. }
  602. /* TCP/UDP checksum error. */
  603. NextLevelProtoStats->RxErrCts++;
  604. return (NextLevelProtocol == SKCS_PROTO_ID_TCP ?
  605. SKCS_STATUS_TCP_CSUM_ERROR : SKCS_STATUS_UDP_CSUM_ERROR);
  606. } /* SkCsGetReceiveInfo */
  607. #endif
  608. /******************************************************************************
  609. *
  610. * SkCsSetReceiveFlags - set checksum receive flags
  611. *
  612. * Description:
  613. * Use this function to set the various receive flags. According to the
  614. * protocol flags set by the caller, the start offsets within received
  615. * packets of the two hardware checksums are returned. These offsets must
  616. * be stored in all receive descriptors.
  617. *
  618. * Arguments:
  619. * pAc - Pointer to adapter context struct.
  620. *
  621. * ReceiveFlags - Any combination of SK_PROTO_XXX flags of the protocols
  622. * for which the caller wants checksum information on received frames.
  623. *
  624. * pChecksum1Offset - The start offset of the first receive descriptor
  625. * hardware checksum to be calculated for received frames is returned
  626. * here.
  627. *
  628. * pChecksum2Offset - The start offset of the second receive descriptor
  629. * hardware checksum to be calculated for received frames is returned
  630. * here.
  631. *
  632. * Returns: N/A
  633. * Returns the two hardware checksum start offsets.
  634. */
  635. void SkCsSetReceiveFlags(
  636. SK_AC *pAc, /* Adapter context struct. */
  637. unsigned ReceiveFlags, /* New receive flags. */
  638. unsigned *pChecksum1Offset, /* Offset for hardware checksum 1. */
  639. unsigned *pChecksum2Offset, /* Offset for hardware checksum 2. */
  640. int NetNumber)
  641. {
  642. /* Save the receive flags. */
  643. pAc->Csum.ReceiveFlags[NetNumber] = ReceiveFlags;
  644. /* First checksum start offset is the IP header. */
  645. *pChecksum1Offset = SKCS_MAC_HEADER_SIZE;
  646. /*
  647. * Second checksum start offset is the IP data. Note that this may vary
  648. * if there are any IP header options in the actual packet.
  649. */
  650. *pChecksum2Offset = SKCS_MAC_HEADER_SIZE + SKCS_IP_HEADER_SIZE;
  651. } /* SkCsSetReceiveFlags */
  652. #ifndef SkCsCalculateChecksum
  653. /******************************************************************************
  654. *
  655. * SkCsCalculateChecksum - calculate checksum for specified data
  656. *
  657. * Description:
  658. * Calculate and return the 16-bit Internet Checksum for the specified
  659. * data.
  660. *
  661. * Arguments:
  662. * pData - Pointer to data for which the checksum shall be calculated.
  663. * Note: The pointer should be aligned on a 16-bit boundary.
  664. *
  665. * Length - Length in bytes of data to checksum.
  666. *
  667. * Returns:
  668. * The 16-bit Internet Checksum for the specified data.
  669. *
  670. * Note: The checksum is calculated in the machine's natural byte order,
  671. * i.e. little vs. big endian. Thus, the resulting checksum is different
  672. * for the same input data on little and big endian machines.
  673. *
  674. * However, when written back to the network packet, the byte order is
  675. * always in correct network order.
  676. */
  677. unsigned SkCsCalculateChecksum(
  678. void *pData, /* Data to checksum. */
  679. unsigned Length) /* Length of data. */
  680. {
  681. SK_U16 *pU16; /* Pointer to the data as 16-bit words. */
  682. unsigned long Checksum; /* Checksum; must be at least 32 bits. */
  683. /* Sum up all 16-bit words. */
  684. pU16 = (SK_U16 *) pData;
  685. for (Checksum = 0; Length > 1; Length -= 2) {
  686. Checksum += *pU16++;
  687. }
  688. /* If this is an odd number of bytes, add-in the last byte. */
  689. if (Length > 0) {
  690. #ifdef SK_BIG_ENDIAN
  691. /* Add the last byte as the high byte. */
  692. Checksum += ((unsigned) *(SK_U8 *) pU16) << 8;
  693. #else /* !SK_BIG_ENDIAN */
  694. /* Add the last byte as the low byte. */
  695. Checksum += *(SK_U8 *) pU16;
  696. #endif /* !SK_BIG_ENDIAN */
  697. }
  698. /* Add-in any carries. */
  699. SKCS_OC_ADD(Checksum, Checksum, 0);
  700. /* Add-in any new carry. */
  701. SKCS_OC_ADD(Checksum, Checksum, 0);
  702. /* Note: All bits beyond the 16-bit limit are now zero. */
  703. return ((unsigned) Checksum);
  704. } /* SkCsCalculateChecksum */
  705. #endif /* SkCsCalculateChecksum */
  706. /******************************************************************************
  707. *
  708. * SkCsEvent - the CSUM event dispatcher
  709. *
  710. * Description:
  711. * This is the event handler for the CSUM module.
  712. *
  713. * Arguments:
  714. * pAc - Pointer to adapter context.
  715. *
  716. * Ioc - I/O context.
  717. *
  718. * Event - Event id.
  719. *
  720. * Param - Event dependent parameter.
  721. *
  722. * Returns:
  723. * The 16-bit Internet Checksum for the specified data.
  724. *
  725. * Note: The checksum is calculated in the machine's natural byte order,
  726. * i.e. little vs. big endian. Thus, the resulting checksum is different
  727. * for the same input data on little and big endian machines.
  728. *
  729. * However, when written back to the network packet, the byte order is
  730. * always in correct network order.
  731. */
  732. int SkCsEvent(
  733. SK_AC *pAc, /* Pointer to adapter context. */
  734. SK_IOC Ioc, /* I/O context. */
  735. SK_U32 Event, /* Event id. */
  736. SK_EVPARA Param) /* Event dependent parameter. */
  737. {
  738. int ProtoIndex;
  739. int NetNumber;
  740. switch (Event) {
  741. /*
  742. * Clear protocol statistics.
  743. *
  744. * Param - Protocol index, or -1 for all protocols.
  745. * - Net number.
  746. */
  747. case SK_CSUM_EVENT_CLEAR_PROTO_STATS:
  748. ProtoIndex = (int)Param.Para32[1];
  749. NetNumber = (int)Param.Para32[0];
  750. if (ProtoIndex < 0) { /* Clear for all protocols. */
  751. if (NetNumber >= 0) {
  752. memset(&pAc->Csum.ProtoStats[NetNumber][0], 0,
  753. sizeof(pAc->Csum.ProtoStats[NetNumber]));
  754. }
  755. }
  756. else { /* Clear for individual protocol. */
  757. memset(&pAc->Csum.ProtoStats[NetNumber][ProtoIndex], 0,
  758. sizeof(pAc->Csum.ProtoStats[NetNumber][ProtoIndex]));
  759. }
  760. break;
  761. default:
  762. break;
  763. }
  764. return (0); /* Success. */
  765. } /* SkCsEvent */
  766. #endif /* SK_USE_CSUM */