1 /*- 2 * Copyright (c) 2001 Charles Mott <cm@linktel.net> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD: src/lib/libalias/alias_util.c,v 1.4.2.3 2001/11/03 11:34:33 brian Exp $ 27 * $DragonFly: src/lib/libalias/alias_util.c,v 1.3 2004/08/20 00:08:17 joerg Exp $ 28 */ 29 30 /* 31 Alias_util.c contains general utilities used by other functions 32 in the packet aliasing module. At the moment, there are functions 33 for computing IP header and TCP packet checksums. 34 35 The checksum routines are based upon example code in a Unix networking 36 text written by Stevens (sorry, I can't remember the title -- but 37 at least this is a good author). 38 39 Initial Version: August, 1996 (cjm) 40 41 Version 1.7: January 9, 1997 42 Added differential checksum update function. 43 */ 44 45 /* 46 Note: the checksum routines assume that the actual checksum word has 47 been zeroed out. If the checksum word is filled with the proper value, 48 then these routines will give a result of zero (useful for testing 49 purposes); 50 */ 51 52 #include <sys/param.h> 53 #include <netinet/in_systm.h> 54 #include <netinet/in.h> 55 #include <netinet/ip.h> 56 #include <netinet/tcp.h> 57 58 #include "alias.h" 59 #include "alias_local.h" 60 61 u_short 62 PacketAliasInternetChecksum(u_short *ptr, int nbytes) 63 { 64 int sum, oddbyte; 65 66 sum = 0; 67 while (nbytes > 1) 68 { 69 sum += *ptr++; 70 nbytes -= 2; 71 } 72 if (nbytes == 1) 73 { 74 oddbyte = 0; 75 ((u_char *) &oddbyte)[0] = *(u_char *) ptr; 76 ((u_char *) &oddbyte)[1] = 0; 77 sum += oddbyte; 78 } 79 sum = (sum >> 16) + (sum & 0xffff); 80 sum += (sum >> 16); 81 return(~sum); 82 } 83 84 u_short 85 IpChecksum(struct ip *pip) 86 { 87 return( PacketAliasInternetChecksum((u_short *) pip, 88 (pip->ip_hl << 2)) ); 89 90 } 91 92 u_short 93 TcpChecksum(struct ip *pip) 94 { 95 u_short *ptr; 96 struct tcphdr *tc; 97 int nhdr, ntcp, nbytes; 98 int sum, oddbyte; 99 100 nhdr = pip->ip_hl << 2; 101 ntcp = ntohs(pip->ip_len) - nhdr; 102 103 tc = (struct tcphdr *) ((char *) pip + nhdr); 104 ptr = (u_short *) tc; 105 106 /* Add up TCP header and data */ 107 nbytes = ntcp; 108 sum = 0; 109 while (nbytes > 1) 110 { 111 sum += *ptr++; 112 nbytes -= 2; 113 } 114 if (nbytes == 1) 115 { 116 oddbyte = 0; 117 ((u_char *) &oddbyte)[0] = *(u_char *) ptr; 118 ((u_char *) &oddbyte)[1] = 0; 119 sum += oddbyte; 120 } 121 122 /* "Pseudo-header" data */ 123 ptr = (u_short *) &(pip->ip_dst); 124 sum += *ptr++; 125 sum += *ptr; 126 ptr = (u_short *) &(pip->ip_src); 127 sum += *ptr++; 128 sum += *ptr; 129 sum += htons((u_short) ntcp); 130 sum += htons((u_short) pip->ip_p); 131 132 /* Roll over carry bits */ 133 sum = (sum >> 16) + (sum & 0xffff); 134 sum += (sum >> 16); 135 136 /* Return checksum */ 137 return((u_short) ~sum); 138 } 139 140 141 void 142 DifferentialChecksum(u_short *cksum, u_short *new, u_short *old, int n) 143 { 144 int i; 145 int accumulate; 146 147 accumulate = *cksum; 148 for (i=0; i<n; i++) 149 { 150 accumulate -= *new++; 151 accumulate += *old++; 152 } 153 154 if (accumulate < 0) 155 { 156 accumulate = -accumulate; 157 accumulate = (accumulate >> 16) + (accumulate & 0xffff); 158 accumulate += accumulate >> 16; 159 *cksum = (u_short) ~accumulate; 160 } 161 else 162 { 163 accumulate = (accumulate >> 16) + (accumulate & 0xffff); 164 accumulate += accumulate >> 16; 165 *cksum = (u_short) accumulate; 166 } 167 } 168 169