1 1.2 christos /* $NetBSD: ip_cksum.c,v 1.2 2006/01/25 13:46:09 christos Exp $ */ 2 1.1 christos 3 1.1 christos /* 4 1.1 christos * Copyright (c) 1992 Regents of the University of California. 5 1.1 christos * All rights reserved. 6 1.1 christos * 7 1.1 christos * This software was developed by the Computer Systems Engineering group 8 1.1 christos * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 9 1.1 christos * contributed to Berkeley. 10 1.1 christos * 11 1.1 christos * Redistribution and use in source and binary forms, with or without 12 1.1 christos * modification, are permitted provided that the following conditions 13 1.1 christos * are met: 14 1.1 christos * 1. Redistributions of source code must retain the above copyright 15 1.1 christos * notice, this list of conditions and the following disclaimer. 16 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright 17 1.1 christos * notice, this list of conditions and the following disclaimer in the 18 1.1 christos * documentation and/or other materials provided with the distribution. 19 1.1 christos * 3. All advertising materials mentioning features or use of this software 20 1.1 christos * must display the following acknowledgement: 21 1.1 christos * This product includes software developed by the University of 22 1.1 christos * California, Lawrence Berkeley Laboratory and its contributors. 23 1.1 christos * 4. Neither the name of the University nor the names of its contributors 24 1.1 christos * may be used to endorse or promote products derived from this software 25 1.1 christos * without specific prior written permission. 26 1.1 christos * 27 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 28 1.1 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 29 1.1 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 30 1.1 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 31 1.1 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 32 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 33 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 34 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 35 1.1 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 36 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 37 1.1 christos * SUCH DAMAGE. 38 1.1 christos * 39 1.1 christos * @(#) Header: in_cksum.c,v 1.1 92/09/11 01:15:55 leres Exp (LBL) 40 1.1 christos */ 41 1.1 christos 42 1.1 christos #include <sys/types.h> 43 1.1 christos #include <sys/socket.h> 44 1.1 christos 45 1.1 christos #include <net/if.h> 46 1.1 christos #include <net/if_ether.h> 47 1.1 christos 48 1.1 christos #include <netinet/in.h> 49 1.1 christos #include <netinet/in_systm.h> 50 1.1 christos #include <netinet/ip.h> 51 1.1 christos #include <netinet/ip_var.h> 52 1.1 christos #include <netinet/udp.h> 53 1.1 christos #include <netinet/udp_var.h> 54 1.1 christos 55 1.1 christos #include <machine/endian.h> 56 1.1 christos 57 1.1 christos #include "stand.h" 58 1.1 christos #include "net.h" 59 1.1 christos 60 1.1 christos /* 61 1.1 christos * Checksum routine for Internet Protocol family headers. 62 1.1 christos * This routine is very heavily used in the network 63 1.1 christos * code and should be modified for each CPU to be as fast as possible. 64 1.1 christos * In particular, it should not be this one. 65 1.1 christos */ 66 1.1 christos int 67 1.2 christos ip_cksum(const void *p, size_t llen) 68 1.1 christos { 69 1.2 christos int sum = 0, oddbyte = 0, v = 0, len = (int)llen; 70 1.1 christos const u_char *cp = p; 71 1.1 christos 72 1.1 christos /* we assume < 2^16 bytes being summed */ 73 1.1 christos while (len > 0) { 74 1.1 christos if (oddbyte) { 75 1.1 christos sum += v + *cp++; 76 1.1 christos len--; 77 1.1 christos } 78 1.1 christos if (((long)cp & 1) == 0) { 79 1.1 christos while ((len -= 2) >= 0) { 80 1.1 christos sum += *(const u_short *)cp; 81 1.1 christos cp += 2; 82 1.1 christos } 83 1.1 christos } else { 84 1.1 christos while ((len -= 2) >= 0) { 85 1.1 christos #if BYTE_ORDER == BIG_ENDIAN 86 1.1 christos sum += *cp++ << 8; 87 1.1 christos sum += *cp++; 88 1.1 christos #else 89 1.1 christos sum += *cp++; 90 1.1 christos sum += *cp++ << 8; 91 1.1 christos #endif 92 1.1 christos } 93 1.1 christos } 94 1.1 christos if ((oddbyte = len & 1) != 0) 95 1.1 christos #if BYTE_ORDER == BIG_ENDIAN 96 1.1 christos v = *cp << 8; 97 1.1 christos #else 98 1.1 christos v = *cp; 99 1.1 christos #endif 100 1.1 christos } 101 1.1 christos if (oddbyte) 102 1.1 christos sum += v; 103 1.1 christos sum = (sum >> 16) + (sum & 0xffff); /* add in accumulated carries */ 104 1.1 christos sum += sum >> 16; /* add potential last carry */ 105 1.1 christos return (0xffff & ~sum); 106 1.1 christos } 107