1 /******************************************************************************
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.
9 ******************************************************************************/
11 /******************************************************************************
13 * (C)Copyright 1998-2001 SysKonnect GmbH.
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * The information in this file is provided "AS IS" without warranty.
22 ******************************************************************************/
24 /******************************************************************************
29 * Revision 1.10 2002/04/11 10:02:04 rwahl
30 * Fix in SkCsGetSendInfo():
31 * - function did not return ProtocolFlags in every case.
32 * - pseudo header csum calculated wrong for big endian.
34 * Revision 1.9 2001/06/13 07:42:08 gklug
35 * fix: NetNumber was wrong in CLEAR_STAT event
36 * add: check for good NetNumber in Clear STAT
38 * Revision 1.8 2001/02/06 11:15:36 rassmann
39 * Supporting two nets on dual-port adapters.
41 * Revision 1.7 2000/06/29 13:17:05 rassmann
42 * Corrected reception of a packet with UDP checksum == 0 (which means there
43 * is no UDP checksum).
45 * Revision 1.6 2000/02/21 12:35:10 cgoos
46 * Fixed license header comment.
48 * Revision 1.5 2000/02/21 11:05:19 cgoos
49 * Merged changes back to common source.
50 * Fixed rx path for BIG ENDIAN architecture.
52 * Revision 1.1 1999/07/26 15:28:12 mkarl
53 * added return SKCS_STATUS_IP_CSUM_ERROR_UDP and
54 * SKCS_STATUS_IP_CSUM_ERROR_TCP to pass the NidsTester
55 * changed from common source to windows specific source
56 * therefore restarting with v1.0
58 * Revision 1.3 1999/05/10 08:39:33 mkarl
59 * prevent overflows in SKCS_HTON16
60 * fixed a bug in pseudo header checksum calculation
63 * Revision 1.2 1998/10/22 11:53:28 swolf
64 * Now using SK_DBG_MSG.
66 * Revision 1.1 1998/09/01 15:35:41 swolf
69 * 13-May-1998 sw Created.
71 ******************************************************************************/
75 #ifdef SK_USE_CSUM /* Check if CSUM is to be used. */
78 static const char SysKonnectFileId[] = "@(#)"
79 "$Id: skcsum.c,v 1.10 2002/04/11 10:02:04 rwahl Exp $"
83 /******************************************************************************
87 * This is the "GEnesis" common module "CSUM".
89 * This module contains the code necessary to calculate, store, and verify the
90 * Internet Checksum of IP, TCP, and UDP frames.
92 * "GEnesis" is an abbreviation of "Gigabit Ethernet Network System in Silicon"
93 * and is the code name of this SysKonnect project.
95 * Compilation Options:
97 * SK_USE_CSUM - Define if CSUM is to be used. Otherwise, CSUM will be an
100 * SKCS_OVERWRITE_PROTO - Define to overwrite the default protocol id
101 * definitions. In this case, all SKCS_PROTO_xxx definitions must be made
104 * SKCS_OVERWRITE_STATUS - Define to overwrite the default return status
105 * definitions. In this case, all SKCS_STATUS_xxx definitions must be made
108 * Include File Hierarchy:
116 ******************************************************************************/
118 #include "h/skdrv1st.h"
119 #include "h/skcsum.h"
120 #include "h/skdrv2nd.h"
122 /* defines ********************************************************************/
124 /* The size of an Ethernet MAC header. */
125 #define SKCS_ETHERNET_MAC_HEADER_SIZE (6+6+2)
127 /* The size of the used topology's MAC header. */
128 #define SKCS_MAC_HEADER_SIZE SKCS_ETHERNET_MAC_HEADER_SIZE
130 /* The size of the IP header without any option fields. */
131 #define SKCS_IP_HEADER_SIZE 20
134 * Field offsets within the IP header.
137 /* "Internet Header Version" and "Length". */
138 #define SKCS_OFS_IP_HEADER_VERSION_AND_LENGTH 0
140 /* "Total Length". */
141 #define SKCS_OFS_IP_TOTAL_LENGTH 2
143 /* "Flags" "Fragment Offset". */
144 #define SKCS_OFS_IP_FLAGS_AND_FRAGMENT_OFFSET 6
146 /* "Next Level Protocol" identifier. */
147 #define SKCS_OFS_IP_NEXT_LEVEL_PROTOCOL 9
149 /* Source IP address. */
150 #define SKCS_OFS_IP_SOURCE_ADDRESS 12
152 /* Destination IP address. */
153 #define SKCS_OFS_IP_DESTINATION_ADDRESS 16
157 * Field offsets within the UDP header.
161 #define SKCS_OFS_UDP_CHECKSUM 6
163 /* IP "Next Level Protocol" identifiers (see RFC 790). */
164 #define SKCS_PROTO_ID_TCP 6 /* Transport Control Protocol */
165 #define SKCS_PROTO_ID_UDP 17 /* User Datagram Protocol */
167 /* IP "Don't Fragment" bit. */
168 #define SKCS_IP_DONT_FRAGMENT SKCS_HTON16(0x4000)
170 /* Add a byte offset to a pointer. */
171 #define SKCS_IDX(pPtr, Ofs) ((void *) ((char *) (pPtr) + (Ofs)))
174 * Macros that convert host to network representation and vice versa, i.e.
175 * little/big endian conversion on little endian machines only.
177 #ifdef SK_LITTLE_ENDIAN
178 #define SKCS_HTON16(Val16) (((unsigned) (Val16) >> 8) | (((Val16) & 0xFF) << 8))
179 #endif /* SK_LITTLE_ENDIAN */
181 #define SKCS_HTON16(Val16) (Val16)
182 #endif /* SK_BIG_ENDIAN */
183 #define SKCS_NTOH16(Val16) SKCS_HTON16(Val16)
185 /* typedefs *******************************************************************/
187 /* function prototypes ********************************************************/
189 /******************************************************************************
191 * SkCsGetSendInfo - get checksum information for a send packet
194 * Get all checksum information necessary to send a TCP or UDP packet. The
195 * function checks the IP header passed to it. If the high-level protocol
196 * is either TCP or UDP the pseudo header checksum is calculated and
199 * The function returns the total length of the IP header (including any
200 * IP option fields), which is the same as the start offset of the IP data
201 * which in turn is the start offset of the TCP or UDP header.
203 * The function also returns the TCP or UDP pseudo header checksum, which
204 * should be used as the start value for the hardware checksum calculation.
205 * (Note that any actual pseudo header checksum can never calculate to
209 * There is a bug in the ASIC which may lead to wrong checksums.
212 * pAc - A pointer to the adapter context struct.
214 * pIpHeader - Pointer to IP header. Must be at least the IP header *not*
215 * including any option fields, i.e. at least 20 bytes.
217 * Note: This pointer will be used to address 8-, 16-, and 32-bit
218 * variables with the respective alignment offsets relative to the pointer.
219 * Thus, the pointer should point to a 32-bit aligned address. If the
220 * target system cannot address 32-bit variables on non 32-bit aligned
221 * addresses, then the pointer *must* point to a 32-bit aligned address.
223 * pPacketInfo - A pointer to the packet information structure for this
224 * packet. Before calling this SkCsGetSendInfo(), the following field must
227 * ProtocolFlags - Initialize with any combination of
228 * SKCS_PROTO_XXX bit flags. SkCsGetSendInfo() will only work on
229 * the protocols specified here. Any protocol(s) not specified
230 * here will be ignored.
232 * Note: Only one checksum can be calculated in hardware. Thus, if
233 * SKCS_PROTO_IP is specified in the 'ProtocolFlags',
234 * SkCsGetSendInfo() must calculate the IP header checksum in
235 * software. It might be a better idea to have the calling
236 * protocol stack calculate the IP header checksum.
239 * On return, the following fields in 'pPacketInfo' may or may not have
240 * been filled with information, depending on the protocol(s) found in the
243 * ProtocolFlags - Returns the SKCS_PROTO_XXX bit flags of the protocol(s)
244 * that were both requested by the caller and actually found in the packet.
245 * Protocol(s) not specified by the caller and/or not found in the packet
246 * will have their respective SKCS_PROTO_XXX bit flags reset.
248 * Note: For IP fragments, TCP and UDP packet information is ignored.
250 * IpHeaderLength - The total length in bytes of the complete IP header
251 * including any option fields is returned here. This is the start offset
252 * of the IP data, i.e. the TCP or UDP header if present.
254 * IpHeaderChecksum - If IP has been specified in the 'ProtocolFlags', the
255 * 16-bit Internet Checksum of the IP header is returned here. This value
256 * is to be stored into the packet's 'IP Header Checksum' field.
258 * PseudoHeaderChecksum - If this is a TCP or UDP packet and if TCP or UDP
259 * has been specified in the 'ProtocolFlags', the 16-bit Internet Checksum
260 * of the TCP or UDP pseudo header is returned here.
263 void SkCsGetSendInfo(
264 SK_AC *pAc, /* Adapter context struct. */
265 void *pIpHeader, /* IP header. */
266 SKCS_PACKET_INFO *pPacketInfo, /* Packet information struct. */
267 int NetNumber) /* Net number */
269 /* Internet Header Version found in IP header. */
270 unsigned InternetHeaderVersion;
272 /* Length of the IP header as found in IP header. */
273 unsigned IpHeaderLength;
275 /* Bit field specifiying the desired/found protocols. */
276 unsigned ProtocolFlags;
278 /* Next level protocol identifier found in IP header. */
279 unsigned NextLevelProtocol;
281 /* Length of IP data portion. */
282 unsigned IpDataLength;
284 /* TCP/UDP pseudo header checksum. */
285 unsigned long PseudoHeaderChecksum;
287 /* Pointer to next level protocol statistics structure. */
288 SKCS_PROTO_STATS *NextLevelProtoStats;
290 /* Temporary variable. */
294 SKCS_IDX(pIpHeader, SKCS_OFS_IP_HEADER_VERSION_AND_LENGTH);
296 /* Get the Internet Header Version (IHV). */
297 /* Note: The IHV is stored in the upper four bits. */
299 InternetHeaderVersion = Tmp >> 4;
301 /* Check the Internet Header Version. */
302 /* Note: We currently only support IP version 4. */
304 if (InternetHeaderVersion != 4) { /* IPv4? */
305 SK_DBG_MSG(pAc, SK_DBGMOD_CSUM, SK_DBGCAT_ERR | SK_DBGCAT_TX,
306 ("Tx: Unknown Internet Header Version %u.\n",
307 InternetHeaderVersion));
308 pPacketInfo->ProtocolFlags = 0;
309 pAc->Csum.ProtoStats[NetNumber][SKCS_PROTO_STATS_IP].TxUnableCts++;
313 /* Get the IP header length (IHL). */
315 * Note: The IHL is stored in the lower four bits as the number of
319 IpHeaderLength = (Tmp & 0xf) * 4;
320 pPacketInfo->IpHeaderLength = IpHeaderLength;
322 /* Check the IP header length. */
324 /* 04-Aug-1998 sw - Really check the IHL? Necessary? */
326 if (IpHeaderLength < 5*4) {
327 SK_DBG_MSG(pAc, SK_DBGMOD_CSUM, SK_DBGCAT_ERR | SK_DBGCAT_TX,
328 ("Tx: Invalid IP Header Length %u.\n", IpHeaderLength));
329 pPacketInfo->ProtocolFlags = 0;
330 pAc->Csum.ProtoStats[NetNumber][SKCS_PROTO_STATS_IP].TxUnableCts++;
334 /* This is an IPv4 frame with a header of valid length. */
336 pAc->Csum.ProtoStats[NetNumber][SKCS_PROTO_STATS_IP].TxOkCts++;
338 /* Check if we should calculate the IP header checksum. */
340 ProtocolFlags = pPacketInfo->ProtocolFlags;
342 if (ProtocolFlags & SKCS_PROTO_IP) {
343 pPacketInfo->IpHeaderChecksum =
344 SkCsCalculateChecksum(pIpHeader, IpHeaderLength);
347 /* Get the next level protocol identifier. */
350 *(SK_U8 *) SKCS_IDX(pIpHeader, SKCS_OFS_IP_NEXT_LEVEL_PROTOCOL);
353 * Check if this is a TCP or UDP frame and if we should calculate the
354 * TCP/UDP pseudo header checksum.
356 * Also clear all protocol bit flags of protocols not present in the
360 if ((ProtocolFlags & SKCS_PROTO_TCP) != 0 &&
361 NextLevelProtocol == SKCS_PROTO_ID_TCP) {
363 ProtocolFlags &= SKCS_PROTO_TCP | SKCS_PROTO_IP;
364 NextLevelProtoStats =
365 &pAc->Csum.ProtoStats[NetNumber][SKCS_PROTO_STATS_TCP];
367 else if ((ProtocolFlags & SKCS_PROTO_UDP) != 0 &&
368 NextLevelProtocol == SKCS_PROTO_ID_UDP) {
370 ProtocolFlags &= SKCS_PROTO_UDP | SKCS_PROTO_IP;
371 NextLevelProtoStats =
372 &pAc->Csum.ProtoStats[NetNumber][SKCS_PROTO_STATS_UDP];
376 * Either not a TCP or UDP frame and/or TCP/UDP processing not
379 pPacketInfo->ProtocolFlags = ProtocolFlags & SKCS_PROTO_IP;
383 /* Check if this is an IP fragment. */
386 * Note: An IP fragment has a non-zero "Fragment Offset" field and/or
387 * the "More Fragments" bit set. Thus, if both the "Fragment Offset"
388 * and the "More Fragments" are zero, it is *not* a fragment. We can
389 * easily check both at the same time since they are in the same 16-bit
394 SKCS_IDX(pIpHeader, SKCS_OFS_IP_FLAGS_AND_FRAGMENT_OFFSET) &
395 ~SKCS_IP_DONT_FRAGMENT) != 0) {
396 /* IP fragment; ignore all other protocols. */
397 pPacketInfo->ProtocolFlags = ProtocolFlags & SKCS_PROTO_IP;
398 NextLevelProtoStats->TxUnableCts++;
403 * Calculate the TCP/UDP pseudo header checksum.
406 /* Get total length of IP header and data. */
409 *(SK_U16 *) SKCS_IDX(pIpHeader, SKCS_OFS_IP_TOTAL_LENGTH);
411 /* Get length of IP data portion. */
413 IpDataLength = SKCS_NTOH16(IpDataLength) - IpHeaderLength;
415 /* Calculate the sum of all pseudo header fields (16-bit). */
417 PseudoHeaderChecksum =
418 (unsigned long) *(SK_U16 *) SKCS_IDX(pIpHeader,
419 SKCS_OFS_IP_SOURCE_ADDRESS + 0) +
420 (unsigned long) *(SK_U16 *) SKCS_IDX(pIpHeader,
421 SKCS_OFS_IP_SOURCE_ADDRESS + 2) +
422 (unsigned long) *(SK_U16 *) SKCS_IDX(pIpHeader,
423 SKCS_OFS_IP_DESTINATION_ADDRESS + 0) +
424 (unsigned long) *(SK_U16 *) SKCS_IDX(pIpHeader,
425 SKCS_OFS_IP_DESTINATION_ADDRESS + 2) +
426 (unsigned long) SKCS_HTON16(NextLevelProtocol) +
427 (unsigned long) SKCS_HTON16(IpDataLength);
429 /* Add-in any carries. */
431 SKCS_OC_ADD(PseudoHeaderChecksum, PseudoHeaderChecksum, 0);
433 /* Add-in any new carry. */
435 SKCS_OC_ADD(pPacketInfo->PseudoHeaderChecksum, PseudoHeaderChecksum, 0);
437 pPacketInfo->ProtocolFlags = ProtocolFlags;
438 NextLevelProtoStats->TxOkCts++; /* Success. */
439 } /* SkCsGetSendInfo */
442 /******************************************************************************
444 * SkCsGetReceiveInfo - verify checksum information for a received packet
447 * Verify a received frame's checksum. The function returns a status code
448 * reflecting the result of the verification.
451 * Before calling this function you have to verify that the frame is
452 * not padded and Checksum1 and Checksum2 are bigger than 1.
455 * pAc - Pointer to adapter context struct.
457 * pIpHeader - Pointer to IP header. Must be at least the length in bytes
458 * of the received IP header including any option fields. For UDP packets,
459 * 8 additional bytes are needed to access the UDP checksum.
461 * Note: The actual length of the IP header is stored in the lower four
462 * bits of the first octet of the IP header as the number of 4-byte words,
463 * so it must be multiplied by four to get the length in bytes. Thus, the
464 * maximum IP header length is 15 * 4 = 60 bytes.
466 * Checksum1 - The first 16-bit Internet Checksum calculated by the
467 * hardware starting at the offset returned by SkCsSetReceiveFlags().
469 * Checksum2 - The second 16-bit Internet Checksum calculated by the
470 * hardware starting at the offset returned by SkCsSetReceiveFlags().
473 * SKCS_STATUS_UNKNOWN_IP_VERSION - Not an IP v4 frame.
474 * SKCS_STATUS_IP_CSUM_ERROR - IP checksum error.
475 * SKCS_STATUS_IP_CSUM_ERROR_TCP - IP checksum error in TCP frame.
476 * SKCS_STATUS_IP_CSUM_ERROR_UDP - IP checksum error in UDP frame
477 * SKCS_STATUS_IP_FRAGMENT - IP fragment (IP checksum ok).
478 * SKCS_STATUS_IP_CSUM_OK - IP checksum ok (not a TCP or UDP frame).
479 * SKCS_STATUS_TCP_CSUM_ERROR - TCP checksum error (IP checksum ok).
480 * SKCS_STATUS_UDP_CSUM_ERROR - UDP checksum error (IP checksum ok).
481 * SKCS_STATUS_TCP_CSUM_OK - IP and TCP checksum ok.
482 * SKCS_STATUS_UDP_CSUM_OK - IP and UDP checksum ok.
483 * SKCS_STATUS_IP_CSUM_OK_NO_UDP - IP checksum OK and no UDP checksum.
485 * Note: If SKCS_OVERWRITE_STATUS is defined, the SKCS_STATUS_XXX values
486 * returned here can be defined in some header file by the module using CSUM.
487 * In this way, the calling module can assign return values for its own needs,
488 * e.g. by assigning bit flags to the individual protocols.
490 SKCS_STATUS SkCsGetReceiveInfo(
491 SK_AC *pAc, /* Adapter context struct. */
492 void *pIpHeader, /* IP header. */
493 unsigned Checksum1, /* Hardware checksum 1. */
494 unsigned Checksum2, /* Hardware checksum 2. */
495 int NetNumber) /* Net number */
497 /* Internet Header Version found in IP header. */
498 unsigned InternetHeaderVersion;
500 /* Length of the IP header as found in IP header. */
501 unsigned IpHeaderLength;
503 /* Length of IP data portion. */
504 unsigned IpDataLength;
506 /* IP header checksum. */
507 unsigned IpHeaderChecksum;
509 /* IP header options checksum, if any. */
510 unsigned IpOptionsChecksum;
512 /* IP data checksum, i.e. TCP/UDP checksum. */
513 unsigned IpDataChecksum;
515 /* Next level protocol identifier found in IP header. */
516 unsigned NextLevelProtocol;
518 /* The checksum of the "next level protocol", i.e. TCP or UDP. */
519 unsigned long NextLevelProtocolChecksum;
521 /* Pointer to next level protocol statistics structure. */
522 SKCS_PROTO_STATS *NextLevelProtoStats;
524 /* Temporary variable. */
528 SKCS_IDX(pIpHeader, SKCS_OFS_IP_HEADER_VERSION_AND_LENGTH);
530 /* Get the Internet Header Version (IHV). */
531 /* Note: The IHV is stored in the upper four bits. */
533 InternetHeaderVersion = Tmp >> 4;
535 /* Check the Internet Header Version. */
536 /* Note: We currently only support IP version 4. */
538 if (InternetHeaderVersion != 4) { /* IPv4? */
539 SK_DBG_MSG(pAc, SK_DBGMOD_CSUM, SK_DBGCAT_ERR | SK_DBGCAT_RX,
540 ("Rx: Unknown Internet Header Version %u.\n",
541 InternetHeaderVersion));
542 pAc->Csum.ProtoStats[NetNumber][SKCS_PROTO_STATS_IP].RxUnableCts++;
543 return (SKCS_STATUS_UNKNOWN_IP_VERSION);
546 /* Get the IP header length (IHL). */
548 * Note: The IHL is stored in the lower four bits as the number of
552 IpHeaderLength = (Tmp & 0xf) * 4;
554 /* Check the IP header length. */
556 /* 04-Aug-1998 sw - Really check the IHL? Necessary? */
558 if (IpHeaderLength < 5*4) {
559 SK_DBG_MSG(pAc, SK_DBGMOD_CSUM, SK_DBGCAT_ERR | SK_DBGCAT_RX,
560 ("Rx: Invalid IP Header Length %u.\n", IpHeaderLength));
561 pAc->Csum.ProtoStats[NetNumber][SKCS_PROTO_STATS_IP].RxErrCts++;
562 return (SKCS_STATUS_IP_CSUM_ERROR);
565 /* This is an IPv4 frame with a header of valid length. */
567 /* Get the IP header and data checksum. */
569 IpDataChecksum = Checksum2;
572 * The IP header checksum is calculated as follows:
574 * IpHeaderChecksum = Checksum1 - Checksum2
577 SKCS_OC_SUB(IpHeaderChecksum, Checksum1, Checksum2);
579 /* Check if any IP header options. */
581 if (IpHeaderLength > SKCS_IP_HEADER_SIZE) {
583 /* Get the IP options checksum. */
585 IpOptionsChecksum = SkCsCalculateChecksum(
586 SKCS_IDX(pIpHeader, SKCS_IP_HEADER_SIZE),
587 IpHeaderLength - SKCS_IP_HEADER_SIZE);
589 /* Adjust the IP header and IP data checksums. */
591 SKCS_OC_ADD(IpHeaderChecksum, IpHeaderChecksum, IpOptionsChecksum);
593 SKCS_OC_SUB(IpDataChecksum, IpDataChecksum, IpOptionsChecksum);
597 * Check if the IP header checksum is ok.
599 * NOTE: We must check the IP header checksum even if the caller just wants
600 * us to check upper-layer checksums, because we cannot do any further
601 * processing of the packet without a valid IP checksum.
604 /* Get the next level protocol identifier. */
606 NextLevelProtocol = *(SK_U8 *)
607 SKCS_IDX(pIpHeader, SKCS_OFS_IP_NEXT_LEVEL_PROTOCOL);
609 if (IpHeaderChecksum != 0xFFFF) {
610 pAc->Csum.ProtoStats[NetNumber][SKCS_PROTO_STATS_IP].RxErrCts++;
611 /* the NDIS tester wants to know the upper level protocol too */
612 if (NextLevelProtocol == SKCS_PROTO_ID_TCP) {
613 return(SKCS_STATUS_IP_CSUM_ERROR_TCP);
615 else if (NextLevelProtocol == SKCS_PROTO_ID_UDP) {
616 return(SKCS_STATUS_IP_CSUM_ERROR_UDP);
618 return (SKCS_STATUS_IP_CSUM_ERROR);
622 * Check if this is a TCP or UDP frame and if we should calculate the
623 * TCP/UDP pseudo header checksum.
625 * Also clear all protocol bit flags of protocols not present in the
629 if ((pAc->Csum.ReceiveFlags[NetNumber] & SKCS_PROTO_TCP) != 0 &&
630 NextLevelProtocol == SKCS_PROTO_ID_TCP) {
632 NextLevelProtoStats =
633 &pAc->Csum.ProtoStats[NetNumber][SKCS_PROTO_STATS_TCP];
635 else if ((pAc->Csum.ReceiveFlags[NetNumber] & SKCS_PROTO_UDP) != 0 &&
636 NextLevelProtocol == SKCS_PROTO_ID_UDP) {
638 NextLevelProtoStats =
639 &pAc->Csum.ProtoStats[NetNumber][SKCS_PROTO_STATS_UDP];
643 * Either not a TCP or UDP frame and/or TCP/UDP processing not
646 return (SKCS_STATUS_IP_CSUM_OK);
649 /* Check if this is an IP fragment. */
652 * Note: An IP fragment has a non-zero "Fragment Offset" field and/or
653 * the "More Fragments" bit set. Thus, if both the "Fragment Offset"
654 * and the "More Fragments" are zero, it is *not* a fragment. We can
655 * easily check both at the same time since they are in the same 16-bit
660 SKCS_IDX(pIpHeader, SKCS_OFS_IP_FLAGS_AND_FRAGMENT_OFFSET) &
661 ~SKCS_IP_DONT_FRAGMENT) != 0) {
662 /* IP fragment; ignore all other protocols. */
663 NextLevelProtoStats->RxUnableCts++;
664 return (SKCS_STATUS_IP_FRAGMENT);
671 * If the computed checksum is zero, it is transmitted as all ones (the
672 * equivalent in one's complement arithmetic). An all zero transmitted
673 * checksum value means that the transmitter generated no checksum (for
674 * debugging or for higher level protocols that don't care).
677 if (NextLevelProtocol == SKCS_PROTO_ID_UDP &&
678 *(SK_U16*)SKCS_IDX(pIpHeader, IpHeaderLength + 6) == 0x0000) {
680 NextLevelProtoStats->RxOkCts++;
682 return (SKCS_STATUS_IP_CSUM_OK_NO_UDP);
686 * Calculate the TCP/UDP checksum.
689 /* Get total length of IP header and data. */
692 *(SK_U16 *) SKCS_IDX(pIpHeader, SKCS_OFS_IP_TOTAL_LENGTH);
694 /* Get length of IP data portion. */
696 IpDataLength = SKCS_NTOH16(IpDataLength) - IpHeaderLength;
698 NextLevelProtocolChecksum =
700 /* Calculate the pseudo header checksum. */
702 (unsigned long) *(SK_U16 *) SKCS_IDX(pIpHeader,
703 SKCS_OFS_IP_SOURCE_ADDRESS + 0) +
704 (unsigned long) *(SK_U16 *) SKCS_IDX(pIpHeader,
705 SKCS_OFS_IP_SOURCE_ADDRESS + 2) +
706 (unsigned long) *(SK_U16 *) SKCS_IDX(pIpHeader,
707 SKCS_OFS_IP_DESTINATION_ADDRESS + 0) +
708 (unsigned long) *(SK_U16 *) SKCS_IDX(pIpHeader,
709 SKCS_OFS_IP_DESTINATION_ADDRESS + 2) +
710 (unsigned long) SKCS_HTON16(NextLevelProtocol) +
711 (unsigned long) SKCS_HTON16(IpDataLength) +
713 /* Add the TCP/UDP header checksum. */
715 (unsigned long) IpDataChecksum;
717 /* Add-in any carries. */
719 SKCS_OC_ADD(NextLevelProtocolChecksum, NextLevelProtocolChecksum, 0);
721 /* Add-in any new carry. */
723 SKCS_OC_ADD(NextLevelProtocolChecksum, NextLevelProtocolChecksum, 0);
725 /* Check if the TCP/UDP checksum is ok. */
727 if ((unsigned) NextLevelProtocolChecksum == 0xFFFF) {
729 /* TCP/UDP checksum ok. */
731 NextLevelProtoStats->RxOkCts++;
733 return (NextLevelProtocol == SKCS_PROTO_ID_TCP ?
734 SKCS_STATUS_TCP_CSUM_OK : SKCS_STATUS_UDP_CSUM_OK);
737 /* TCP/UDP checksum error. */
739 NextLevelProtoStats->RxErrCts++;
741 return (NextLevelProtocol == SKCS_PROTO_ID_TCP ?
742 SKCS_STATUS_TCP_CSUM_ERROR : SKCS_STATUS_UDP_CSUM_ERROR);
743 } /* SkCsGetReceiveInfo */
747 /******************************************************************************
749 * SkCsSetReceiveFlags - set checksum receive flags
752 * Use this function to set the various receive flags. According to the
753 * protocol flags set by the caller, the start offsets within received
754 * packets of the two hardware checksums are returned. These offsets must
755 * be stored in all receive descriptors.
758 * pAc - Pointer to adapter context struct.
760 * ReceiveFlags - Any combination of SK_PROTO_XXX flags of the protocols
761 * for which the caller wants checksum information on received frames.
763 * pChecksum1Offset - The start offset of the first receive descriptor
764 * hardware checksum to be calculated for received frames is returned
767 * pChecksum2Offset - The start offset of the second receive descriptor
768 * hardware checksum to be calculated for received frames is returned
772 * Returns the two hardware checksum start offsets.
774 void SkCsSetReceiveFlags(
775 SK_AC *pAc, /* Adapter context struct. */
776 unsigned ReceiveFlags, /* New receive flags. */
777 unsigned *pChecksum1Offset, /* Offset for hardware checksum 1. */
778 unsigned *pChecksum2Offset, /* Offset for hardware checksum 2. */
781 /* Save the receive flags. */
783 pAc->Csum.ReceiveFlags[NetNumber] = ReceiveFlags;
785 /* First checksum start offset is the IP header. */
786 *pChecksum1Offset = SKCS_MAC_HEADER_SIZE;
789 * Second checksum start offset is the IP data. Note that this may vary
790 * if there are any IP header options in the actual packet.
792 *pChecksum2Offset = SKCS_MAC_HEADER_SIZE + SKCS_IP_HEADER_SIZE;
793 } /* SkCsSetReceiveFlags */
795 #ifndef SkCsCalculateChecksum
797 /******************************************************************************
799 * SkCsCalculateChecksum - calculate checksum for specified data
802 * Calculate and return the 16-bit Internet Checksum for the specified
806 * pData - Pointer to data for which the checksum shall be calculated.
807 * Note: The pointer should be aligned on a 16-bit boundary.
809 * Length - Length in bytes of data to checksum.
812 * The 16-bit Internet Checksum for the specified data.
814 * Note: The checksum is calculated in the machine's natural byte order,
815 * i.e. little vs. big endian. Thus, the resulting checksum is different
816 * for the same input data on little and big endian machines.
818 * However, when written back to the network packet, the byte order is
819 * always in correct network order.
821 unsigned SkCsCalculateChecksum(
822 void *pData, /* Data to checksum. */
823 unsigned Length) /* Length of data. */
825 SK_U16 *pU16; /* Pointer to the data as 16-bit words. */
826 unsigned long Checksum; /* Checksum; must be at least 32 bits. */
828 /* Sum up all 16-bit words. */
830 pU16 = (SK_U16 *) pData;
831 for (Checksum = 0; Length > 1; Length -= 2) {
835 /* If this is an odd number of bytes, add-in the last byte. */
839 /* Add the last byte as the high byte. */
840 Checksum += ((unsigned) *(SK_U8 *) pU16) << 8;
841 #else /* !SK_BIG_ENDIAN */
842 /* Add the last byte as the low byte. */
843 Checksum += *(SK_U8 *) pU16;
844 #endif /* !SK_BIG_ENDIAN */
847 /* Add-in any carries. */
849 SKCS_OC_ADD(Checksum, Checksum, 0);
851 /* Add-in any new carry. */
853 SKCS_OC_ADD(Checksum, Checksum, 0);
855 /* Note: All bits beyond the 16-bit limit are now zero. */
857 return ((unsigned) Checksum);
858 } /* SkCsCalculateChecksum */
860 #endif /* SkCsCalculateChecksum */
862 /******************************************************************************
864 * SkCsEvent - the CSUM event dispatcher
867 * This is the event handler for the CSUM module.
870 * pAc - Pointer to adapter context.
876 * Param - Event dependent parameter.
879 * The 16-bit Internet Checksum for the specified data.
881 * Note: The checksum is calculated in the machine's natural byte order,
882 * i.e. little vs. big endian. Thus, the resulting checksum is different
883 * for the same input data on little and big endian machines.
885 * However, when written back to the network packet, the byte order is
886 * always in correct network order.
889 SK_AC *pAc, /* Pointer to adapter context. */
890 SK_IOC Ioc, /* I/O context. */
891 SK_U32 Event, /* Event id. */
892 SK_EVPARA Param) /* Event dependent parameter. */
899 * Clear protocol statistics.
901 * Param - Protocol index, or -1 for all protocols.
904 case SK_CSUM_EVENT_CLEAR_PROTO_STATS:
906 ProtoIndex = (int)Param.Para32[1];
907 NetNumber = (int)Param.Para32[0];
908 if (ProtoIndex < 0) { /* Clear for all protocols. */
909 if (NetNumber >= 0) {
910 memset(&pAc->Csum.ProtoStats[NetNumber][0], 0,
911 sizeof(pAc->Csum.ProtoStats[NetNumber]));
914 else { /* Clear for individual protocol. */
915 memset(&pAc->Csum.ProtoStats[NetNumber][ProtoIndex], 0,
916 sizeof(pAc->Csum.ProtoStats[NetNumber][ProtoIndex]));
922 return (0); /* Success. */
925 #endif /* SK_USE_CSUM */