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