skcsum.c 27 KB

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