blob: 162b67df0e80ed22acfc43e70680e885f6f98b4e [file] [log] [blame]
wdenk7152b1d2003-09-05 23:19:14 +00001/******************************************************************************
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 *
13 * (C)Copyright 1998-2001 SysKonnect GmbH.
14 *
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.
19 *
20 * The information in this file is provided "AS IS" without warranty.
21 *
22 ******************************************************************************/
23
24/******************************************************************************
25 *
26 * History:
27 *
28 * $Log: skcsum.c,v $
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.
wdenk42d1f032003-10-15 23:53:47 +000033 *
wdenk7152b1d2003-09-05 23:19:14 +000034 * 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
wdenk42d1f032003-10-15 23:53:47 +000037 *
wdenk7152b1d2003-09-05 23:19:14 +000038 * Revision 1.8 2001/02/06 11:15:36 rassmann
39 * Supporting two nets on dual-port adapters.
wdenk42d1f032003-10-15 23:53:47 +000040 *
wdenk7152b1d2003-09-05 23:19:14 +000041 * 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).
wdenk42d1f032003-10-15 23:53:47 +000044 *
wdenk7152b1d2003-09-05 23:19:14 +000045 * Revision 1.6 2000/02/21 12:35:10 cgoos
46 * Fixed license header comment.
wdenk42d1f032003-10-15 23:53:47 +000047 *
wdenk7152b1d2003-09-05 23:19:14 +000048 * 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.
wdenk42d1f032003-10-15 23:53:47 +000051 *
wdenk7152b1d2003-09-05 23:19:14 +000052 * 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
wdenk42d1f032003-10-15 23:53:47 +000057 *
wdenk7152b1d2003-09-05 23:19:14 +000058 * 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
61 * added some comments
wdenk42d1f032003-10-15 23:53:47 +000062 *
wdenk7152b1d2003-09-05 23:19:14 +000063 * Revision 1.2 1998/10/22 11:53:28 swolf
64 * Now using SK_DBG_MSG.
wdenk42d1f032003-10-15 23:53:47 +000065 *
wdenk7152b1d2003-09-05 23:19:14 +000066 * Revision 1.1 1998/09/01 15:35:41 swolf
67 * initial revision
68 *
69 * 13-May-1998 sw Created.
70 *
71 ******************************************************************************/
72
wdenk149dded2003-09-10 18:20:28 +000073#include <config.h>
74
wdenk7152b1d2003-09-05 23:19:14 +000075#ifdef SK_USE_CSUM /* Check if CSUM is to be used. */
76
77#ifndef lint
78static const char SysKonnectFileId[] = "@(#)"
79 "$Id: skcsum.c,v 1.10 2002/04/11 10:02:04 rwahl Exp $"
80 " (C) SysKonnect.";
81#endif /* !lint */
82
83/******************************************************************************
84 *
85 * Description:
86 *
87 * This is the "GEnesis" common module "CSUM".
88 *
89 * This module contains the code necessary to calculate, store, and verify the
90 * Internet Checksum of IP, TCP, and UDP frames.
91 *
92 * "GEnesis" is an abbreviation of "Gigabit Ethernet Network System in Silicon"
93 * and is the code name of this SysKonnect project.
94 *
95 * Compilation Options:
96 *
97 * SK_USE_CSUM - Define if CSUM is to be used. Otherwise, CSUM will be an
98 * empty module.
99 *
100 * SKCS_OVERWRITE_PROTO - Define to overwrite the default protocol id
101 * definitions. In this case, all SKCS_PROTO_xxx definitions must be made
102 * external.
103 *
104 * SKCS_OVERWRITE_STATUS - Define to overwrite the default return status
105 * definitions. In this case, all SKCS_STATUS_xxx definitions must be made
106 * external.
107 *
108 * Include File Hierarchy:
109 *
110 * "h/skdrv1st.h"
111 * "h/skcsum.h"
112 * "h/sktypes.h"
113 * "h/skqueue.h"
114 * "h/skdrv2nd.h"
115 *
116 ******************************************************************************/
117
118#include "h/skdrv1st.h"
119#include "h/skcsum.h"
120#include "h/skdrv2nd.h"
121
122/* defines ********************************************************************/
123
124/* The size of an Ethernet MAC header. */
125#define SKCS_ETHERNET_MAC_HEADER_SIZE (6+6+2)
126
127/* The size of the used topology's MAC header. */
128#define SKCS_MAC_HEADER_SIZE SKCS_ETHERNET_MAC_HEADER_SIZE
129
130/* The size of the IP header without any option fields. */
131#define SKCS_IP_HEADER_SIZE 20
132
133/*
134 * Field offsets within the IP header.
135 */
136
137/* "Internet Header Version" and "Length". */
138#define SKCS_OFS_IP_HEADER_VERSION_AND_LENGTH 0
139
140/* "Total Length". */
141#define SKCS_OFS_IP_TOTAL_LENGTH 2
142
143/* "Flags" "Fragment Offset". */
144#define SKCS_OFS_IP_FLAGS_AND_FRAGMENT_OFFSET 6
145
146/* "Next Level Protocol" identifier. */
147#define SKCS_OFS_IP_NEXT_LEVEL_PROTOCOL 9
148
149/* Source IP address. */
150#define SKCS_OFS_IP_SOURCE_ADDRESS 12
151
152/* Destination IP address. */
153#define SKCS_OFS_IP_DESTINATION_ADDRESS 16
154
155
156/*
157 * Field offsets within the UDP header.
158 */
159
160/* UDP checksum. */
161#define SKCS_OFS_UDP_CHECKSUM 6
162
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 */
166
167/* IP "Don't Fragment" bit. */
168#define SKCS_IP_DONT_FRAGMENT SKCS_HTON16(0x4000)
169
170/* Add a byte offset to a pointer. */
171#define SKCS_IDX(pPtr, Ofs) ((void *) ((char *) (pPtr) + (Ofs)))
172
173/*
174 * Macros that convert host to network representation and vice versa, i.e.
175 * little/big endian conversion on little endian machines only.
176 */
177#ifdef SK_LITTLE_ENDIAN
178#define SKCS_HTON16(Val16) (((unsigned) (Val16) >> 8) | (((Val16) & 0xFF) << 8))
179#endif /* SK_LITTLE_ENDIAN */
180#ifdef SK_BIG_ENDIAN
181#define SKCS_HTON16(Val16) (Val16)
182#endif /* SK_BIG_ENDIAN */
183#define SKCS_NTOH16(Val16) SKCS_HTON16(Val16)
184
185/* typedefs *******************************************************************/
186
187/* function prototypes ********************************************************/
188
189/******************************************************************************
190 *
191 * SkCsGetSendInfo - get checksum information for a send packet
192 *
193 * Description:
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
197 * returned.
198 *
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.
202 *
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
206 * zero.)
207 *
208 * Note:
209 * There is a bug in the ASIC which may lead to wrong checksums.
210 *
211 * Arguments:
212 * pAc - A pointer to the adapter context struct.
213 *
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.
216 *
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.
222 *
223 * pPacketInfo - A pointer to the packet information structure for this
224 * packet. Before calling this SkCsGetSendInfo(), the following field must
225 * be initialized:
226 *
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.
231 *
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.
237 *
238 * Returns: N/A
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
241 * packet:
242 *
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.
247 *
248 * Note: For IP fragments, TCP and UDP packet information is ignored.
249 *
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.
253 *
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.
257 *
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.
261 */
262#if 0
263void SkCsGetSendInfo(
264SK_AC *pAc, /* Adapter context struct. */
265void *pIpHeader, /* IP header. */
266SKCS_PACKET_INFO *pPacketInfo, /* Packet information struct. */
267int NetNumber) /* Net number */
268{
269 /* Internet Header Version found in IP header. */
270 unsigned InternetHeaderVersion;
271
272 /* Length of the IP header as found in IP header. */
273 unsigned IpHeaderLength;
274
275 /* Bit field specifiying the desired/found protocols. */
276 unsigned ProtocolFlags;
277
278 /* Next level protocol identifier found in IP header. */
279 unsigned NextLevelProtocol;
280
281 /* Length of IP data portion. */
282 unsigned IpDataLength;
283
284 /* TCP/UDP pseudo header checksum. */
285 unsigned long PseudoHeaderChecksum;
286
287 /* Pointer to next level protocol statistics structure. */
288 SKCS_PROTO_STATS *NextLevelProtoStats;
289
290 /* Temporary variable. */
291 unsigned Tmp;
292
293 Tmp = *(SK_U8 *)
294 SKCS_IDX(pIpHeader, SKCS_OFS_IP_HEADER_VERSION_AND_LENGTH);
295
296 /* Get the Internet Header Version (IHV). */
297 /* Note: The IHV is stored in the upper four bits. */
298
299 InternetHeaderVersion = Tmp >> 4;
300
301 /* Check the Internet Header Version. */
302 /* Note: We currently only support IP version 4. */
303
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++;
310 return;
311 }
312
313 /* Get the IP header length (IHL). */
314 /*
315 * Note: The IHL is stored in the lower four bits as the number of
316 * 4-byte words.
317 */
318
319 IpHeaderLength = (Tmp & 0xf) * 4;
320 pPacketInfo->IpHeaderLength = IpHeaderLength;
321
322 /* Check the IP header length. */
323
324 /* 04-Aug-1998 sw - Really check the IHL? Necessary? */
325
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++;
331 return;
332 }
333
334 /* This is an IPv4 frame with a header of valid length. */
335
336 pAc->Csum.ProtoStats[NetNumber][SKCS_PROTO_STATS_IP].TxOkCts++;
337
338 /* Check if we should calculate the IP header checksum. */
339
340 ProtocolFlags = pPacketInfo->ProtocolFlags;
341
342 if (ProtocolFlags & SKCS_PROTO_IP) {
343 pPacketInfo->IpHeaderChecksum =
344 SkCsCalculateChecksum(pIpHeader, IpHeaderLength);
345 }
346
347 /* Get the next level protocol identifier. */
348
349 NextLevelProtocol =
350 *(SK_U8 *) SKCS_IDX(pIpHeader, SKCS_OFS_IP_NEXT_LEVEL_PROTOCOL);
351
352 /*
353 * Check if this is a TCP or UDP frame and if we should calculate the
354 * TCP/UDP pseudo header checksum.
355 *
356 * Also clear all protocol bit flags of protocols not present in the
357 * frame.
358 */
359
360 if ((ProtocolFlags & SKCS_PROTO_TCP) != 0 &&
361 NextLevelProtocol == SKCS_PROTO_ID_TCP) {
362 /* TCP/IP frame. */
363 ProtocolFlags &= SKCS_PROTO_TCP | SKCS_PROTO_IP;
364 NextLevelProtoStats =
365 &pAc->Csum.ProtoStats[NetNumber][SKCS_PROTO_STATS_TCP];
366 }
367 else if ((ProtocolFlags & SKCS_PROTO_UDP) != 0 &&
368 NextLevelProtocol == SKCS_PROTO_ID_UDP) {
369 /* UDP/IP frame. */
370 ProtocolFlags &= SKCS_PROTO_UDP | SKCS_PROTO_IP;
371 NextLevelProtoStats =
372 &pAc->Csum.ProtoStats[NetNumber][SKCS_PROTO_STATS_UDP];
373 }
374 else {
375 /*
376 * Either not a TCP or UDP frame and/or TCP/UDP processing not
377 * specified.
378 */
379 pPacketInfo->ProtocolFlags = ProtocolFlags & SKCS_PROTO_IP;
380 return;
381 }
382
383 /* Check if this is an IP fragment. */
384
385 /*
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
390 * word.
391 */
392
393 if ((*(SK_U16 *)
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++;
399 return;
400 }
401
402 /*
403 * Calculate the TCP/UDP pseudo header checksum.
404 */
405
406 /* Get total length of IP header and data. */
407
408 IpDataLength =
409 *(SK_U16 *) SKCS_IDX(pIpHeader, SKCS_OFS_IP_TOTAL_LENGTH);
410
411 /* Get length of IP data portion. */
412
413 IpDataLength = SKCS_NTOH16(IpDataLength) - IpHeaderLength;
414
415 /* Calculate the sum of all pseudo header fields (16-bit). */
416
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);
wdenk42d1f032003-10-15 23:53:47 +0000428
wdenk7152b1d2003-09-05 23:19:14 +0000429 /* Add-in any carries. */
430
431 SKCS_OC_ADD(PseudoHeaderChecksum, PseudoHeaderChecksum, 0);
432
433 /* Add-in any new carry. */
434
435 SKCS_OC_ADD(pPacketInfo->PseudoHeaderChecksum, PseudoHeaderChecksum, 0);
436
437 pPacketInfo->ProtocolFlags = ProtocolFlags;
438 NextLevelProtoStats->TxOkCts++; /* Success. */
439} /* SkCsGetSendInfo */
440
441
442/******************************************************************************
443 *
444 * SkCsGetReceiveInfo - verify checksum information for a received packet
445 *
446 * Description:
447 * Verify a received frame's checksum. The function returns a status code
448 * reflecting the result of the verification.
449 *
450 * Note:
451 * Before calling this function you have to verify that the frame is
452 * not padded and Checksum1 and Checksum2 are bigger than 1.
453 *
454 * Arguments:
455 * pAc - Pointer to adapter context struct.
456 *
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.
460 *
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.
465 *
466 * Checksum1 - The first 16-bit Internet Checksum calculated by the
467 * hardware starting at the offset returned by SkCsSetReceiveFlags().
468 *
469 * Checksum2 - The second 16-bit Internet Checksum calculated by the
470 * hardware starting at the offset returned by SkCsSetReceiveFlags().
471 *
472 * Returns:
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.
484 *
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.
489 */
490SKCS_STATUS SkCsGetReceiveInfo(
491SK_AC *pAc, /* Adapter context struct. */
492void *pIpHeader, /* IP header. */
493unsigned Checksum1, /* Hardware checksum 1. */
494unsigned Checksum2, /* Hardware checksum 2. */
495int NetNumber) /* Net number */
496{
497 /* Internet Header Version found in IP header. */
498 unsigned InternetHeaderVersion;
499
500 /* Length of the IP header as found in IP header. */
501 unsigned IpHeaderLength;
502
503 /* Length of IP data portion. */
504 unsigned IpDataLength;
505
506 /* IP header checksum. */
507 unsigned IpHeaderChecksum;
508
509 /* IP header options checksum, if any. */
510 unsigned IpOptionsChecksum;
511
512 /* IP data checksum, i.e. TCP/UDP checksum. */
513 unsigned IpDataChecksum;
514
515 /* Next level protocol identifier found in IP header. */
516 unsigned NextLevelProtocol;
517
518 /* The checksum of the "next level protocol", i.e. TCP or UDP. */
519 unsigned long NextLevelProtocolChecksum;
520
521 /* Pointer to next level protocol statistics structure. */
522 SKCS_PROTO_STATS *NextLevelProtoStats;
523
524 /* Temporary variable. */
525 unsigned Tmp;
526
527 Tmp = *(SK_U8 *)
528 SKCS_IDX(pIpHeader, SKCS_OFS_IP_HEADER_VERSION_AND_LENGTH);
529
530 /* Get the Internet Header Version (IHV). */
531 /* Note: The IHV is stored in the upper four bits. */
532
533 InternetHeaderVersion = Tmp >> 4;
534
535 /* Check the Internet Header Version. */
536 /* Note: We currently only support IP version 4. */
537
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);
544 }
545
546 /* Get the IP header length (IHL). */
547 /*
548 * Note: The IHL is stored in the lower four bits as the number of
549 * 4-byte words.
550 */
551
552 IpHeaderLength = (Tmp & 0xf) * 4;
553
554 /* Check the IP header length. */
555
556 /* 04-Aug-1998 sw - Really check the IHL? Necessary? */
557
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);
563 }
564
565 /* This is an IPv4 frame with a header of valid length. */
566
567 /* Get the IP header and data checksum. */
568
569 IpDataChecksum = Checksum2;
570
571 /*
572 * The IP header checksum is calculated as follows:
573 *
574 * IpHeaderChecksum = Checksum1 - Checksum2
575 */
576
577 SKCS_OC_SUB(IpHeaderChecksum, Checksum1, Checksum2);
578
579 /* Check if any IP header options. */
580
581 if (IpHeaderLength > SKCS_IP_HEADER_SIZE) {
582
583 /* Get the IP options checksum. */
584
585 IpOptionsChecksum = SkCsCalculateChecksum(
586 SKCS_IDX(pIpHeader, SKCS_IP_HEADER_SIZE),
587 IpHeaderLength - SKCS_IP_HEADER_SIZE);
588
589 /* Adjust the IP header and IP data checksums. */
590
591 SKCS_OC_ADD(IpHeaderChecksum, IpHeaderChecksum, IpOptionsChecksum);
592
593 SKCS_OC_SUB(IpDataChecksum, IpDataChecksum, IpOptionsChecksum);
594 }
595
596 /*
597 * Check if the IP header checksum is ok.
598 *
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.
602 */
wdenk42d1f032003-10-15 23:53:47 +0000603
wdenk7152b1d2003-09-05 23:19:14 +0000604 /* Get the next level protocol identifier. */
wdenk42d1f032003-10-15 23:53:47 +0000605
wdenk7152b1d2003-09-05 23:19:14 +0000606 NextLevelProtocol = *(SK_U8 *)
607 SKCS_IDX(pIpHeader, SKCS_OFS_IP_NEXT_LEVEL_PROTOCOL);
608
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);
614 }
615 else if (NextLevelProtocol == SKCS_PROTO_ID_UDP) {
616 return(SKCS_STATUS_IP_CSUM_ERROR_UDP);
617 }
618 return (SKCS_STATUS_IP_CSUM_ERROR);
619 }
620
621 /*
622 * Check if this is a TCP or UDP frame and if we should calculate the
623 * TCP/UDP pseudo header checksum.
624 *
625 * Also clear all protocol bit flags of protocols not present in the
626 * frame.
627 */
628
629 if ((pAc->Csum.ReceiveFlags[NetNumber] & SKCS_PROTO_TCP) != 0 &&
630 NextLevelProtocol == SKCS_PROTO_ID_TCP) {
631 /* TCP/IP frame. */
632 NextLevelProtoStats =
633 &pAc->Csum.ProtoStats[NetNumber][SKCS_PROTO_STATS_TCP];
634 }
635 else if ((pAc->Csum.ReceiveFlags[NetNumber] & SKCS_PROTO_UDP) != 0 &&
636 NextLevelProtocol == SKCS_PROTO_ID_UDP) {
637 /* UDP/IP frame. */
638 NextLevelProtoStats =
639 &pAc->Csum.ProtoStats[NetNumber][SKCS_PROTO_STATS_UDP];
640 }
641 else {
642 /*
643 * Either not a TCP or UDP frame and/or TCP/UDP processing not
644 * specified.
645 */
646 return (SKCS_STATUS_IP_CSUM_OK);
647 }
648
649 /* Check if this is an IP fragment. */
650
651 /*
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
656 * word.
657 */
658
659 if ((*(SK_U16 *)
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);
665 }
666
667 /*
668 * 08-May-2000 ra
669 *
670 * From RFC 768 (UDP)
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).
675 */
676
677 if (NextLevelProtocol == SKCS_PROTO_ID_UDP &&
678 *(SK_U16*)SKCS_IDX(pIpHeader, IpHeaderLength + 6) == 0x0000) {
679
680 NextLevelProtoStats->RxOkCts++;
wdenk42d1f032003-10-15 23:53:47 +0000681
wdenk7152b1d2003-09-05 23:19:14 +0000682 return (SKCS_STATUS_IP_CSUM_OK_NO_UDP);
683 }
684
685 /*
686 * Calculate the TCP/UDP checksum.
687 */
688
689 /* Get total length of IP header and data. */
690
691 IpDataLength =
692 *(SK_U16 *) SKCS_IDX(pIpHeader, SKCS_OFS_IP_TOTAL_LENGTH);
693
694 /* Get length of IP data portion. */
695
696 IpDataLength = SKCS_NTOH16(IpDataLength) - IpHeaderLength;
697
698 NextLevelProtocolChecksum =
699
700 /* Calculate the pseudo header checksum. */
701
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) +
712
713 /* Add the TCP/UDP header checksum. */
714
715 (unsigned long) IpDataChecksum;
716
717 /* Add-in any carries. */
718
719 SKCS_OC_ADD(NextLevelProtocolChecksum, NextLevelProtocolChecksum, 0);
720
721 /* Add-in any new carry. */
722
723 SKCS_OC_ADD(NextLevelProtocolChecksum, NextLevelProtocolChecksum, 0);
724
725 /* Check if the TCP/UDP checksum is ok. */
726
727 if ((unsigned) NextLevelProtocolChecksum == 0xFFFF) {
728
729 /* TCP/UDP checksum ok. */
730
731 NextLevelProtoStats->RxOkCts++;
732
733 return (NextLevelProtocol == SKCS_PROTO_ID_TCP ?
734 SKCS_STATUS_TCP_CSUM_OK : SKCS_STATUS_UDP_CSUM_OK);
735 }
wdenk42d1f032003-10-15 23:53:47 +0000736
wdenk7152b1d2003-09-05 23:19:14 +0000737 /* TCP/UDP checksum error. */
738
739 NextLevelProtoStats->RxErrCts++;
740
741 return (NextLevelProtocol == SKCS_PROTO_ID_TCP ?
742 SKCS_STATUS_TCP_CSUM_ERROR : SKCS_STATUS_UDP_CSUM_ERROR);
743} /* SkCsGetReceiveInfo */
744#endif
745
746
747/******************************************************************************
748 *
749 * SkCsSetReceiveFlags - set checksum receive flags
750 *
751 * Description:
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.
756 *
757 * Arguments:
758 * pAc - Pointer to adapter context struct.
759 *
760 * ReceiveFlags - Any combination of SK_PROTO_XXX flags of the protocols
761 * for which the caller wants checksum information on received frames.
762 *
763 * pChecksum1Offset - The start offset of the first receive descriptor
764 * hardware checksum to be calculated for received frames is returned
765 * here.
766 *
767 * pChecksum2Offset - The start offset of the second receive descriptor
768 * hardware checksum to be calculated for received frames is returned
769 * here.
770 *
771 * Returns: N/A
772 * Returns the two hardware checksum start offsets.
773 */
774void SkCsSetReceiveFlags(
775SK_AC *pAc, /* Adapter context struct. */
776unsigned ReceiveFlags, /* New receive flags. */
777unsigned *pChecksum1Offset, /* Offset for hardware checksum 1. */
778unsigned *pChecksum2Offset, /* Offset for hardware checksum 2. */
779int NetNumber)
780{
781 /* Save the receive flags. */
782
783 pAc->Csum.ReceiveFlags[NetNumber] = ReceiveFlags;
784
785 /* First checksum start offset is the IP header. */
786 *pChecksum1Offset = SKCS_MAC_HEADER_SIZE;
787
788 /*
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.
791 */
792 *pChecksum2Offset = SKCS_MAC_HEADER_SIZE + SKCS_IP_HEADER_SIZE;
793} /* SkCsSetReceiveFlags */
794
795#ifndef SkCsCalculateChecksum
796
797/******************************************************************************
798 *
799 * SkCsCalculateChecksum - calculate checksum for specified data
800 *
801 * Description:
802 * Calculate and return the 16-bit Internet Checksum for the specified
803 * data.
804 *
805 * Arguments:
806 * pData - Pointer to data for which the checksum shall be calculated.
807 * Note: The pointer should be aligned on a 16-bit boundary.
808 *
809 * Length - Length in bytes of data to checksum.
810 *
811 * Returns:
812 * The 16-bit Internet Checksum for the specified data.
813 *
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.
817 *
818 * However, when written back to the network packet, the byte order is
819 * always in correct network order.
820 */
821unsigned SkCsCalculateChecksum(
822void *pData, /* Data to checksum. */
823unsigned Length) /* Length of data. */
824{
825 SK_U16 *pU16; /* Pointer to the data as 16-bit words. */
826 unsigned long Checksum; /* Checksum; must be at least 32 bits. */
827
828 /* Sum up all 16-bit words. */
829
830 pU16 = (SK_U16 *) pData;
831 for (Checksum = 0; Length > 1; Length -= 2) {
832 Checksum += *pU16++;
833 }
834
835 /* If this is an odd number of bytes, add-in the last byte. */
836
837 if (Length > 0) {
838#ifdef SK_BIG_ENDIAN
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 */
845 }
846
847 /* Add-in any carries. */
848
849 SKCS_OC_ADD(Checksum, Checksum, 0);
850
851 /* Add-in any new carry. */
852
853 SKCS_OC_ADD(Checksum, Checksum, 0);
854
855 /* Note: All bits beyond the 16-bit limit are now zero. */
856
857 return ((unsigned) Checksum);
858} /* SkCsCalculateChecksum */
859
860#endif /* SkCsCalculateChecksum */
861
862/******************************************************************************
863 *
864 * SkCsEvent - the CSUM event dispatcher
865 *
866 * Description:
867 * This is the event handler for the CSUM module.
868 *
869 * Arguments:
870 * pAc - Pointer to adapter context.
871 *
872 * Ioc - I/O context.
873 *
874 * Event - Event id.
875 *
876 * Param - Event dependent parameter.
877 *
878 * Returns:
879 * The 16-bit Internet Checksum for the specified data.
880 *
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.
884 *
885 * However, when written back to the network packet, the byte order is
886 * always in correct network order.
887 */
888int SkCsEvent(
889SK_AC *pAc, /* Pointer to adapter context. */
890SK_IOC Ioc, /* I/O context. */
891SK_U32 Event, /* Event id. */
892SK_EVPARA Param) /* Event dependent parameter. */
893{
894 int ProtoIndex;
895 int NetNumber;
896
897 switch (Event) {
898 /*
899 * Clear protocol statistics.
900 *
901 * Param - Protocol index, or -1 for all protocols.
902 * - Net number.
903 */
904 case SK_CSUM_EVENT_CLEAR_PROTO_STATS:
905
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]));
912 }
913 }
914 else { /* Clear for individual protocol. */
915 memset(&pAc->Csum.ProtoStats[NetNumber][ProtoIndex], 0,
916 sizeof(pAc->Csum.ProtoStats[NetNumber][ProtoIndex]));
917 }
918 break;
919 default:
920 break;
921 }
922 return (0); /* Success. */
923} /* SkCsEvent */
924
925#endif /* SK_USE_CSUM */