skcsum.c 28 KB

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