1 /*- 2 * Copyright (c) 2001 Charles Mott <cmott@scientech.com> 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$ 27 */ 28 29 /* 30 Alias_util.c contains general utilities used by other functions 31 in the packet aliasing module. At the moment, there are functions 32 for computing IP header and TCP packet checksums. 33 34 The checksum routines are based upon example code in a Unix networking 35 text written by Stevens (sorry, I can't remember the title -- but 36 at least this is a good author). 37 38 Initial Version: August, 1996 (cjm) 39 40 Version 1.7: January 9, 1997 41 Added differential checksum update function. 42 */ 43 44 /* 45 Note: the checksum routines assume that the actual checksum word has 46 been zeroed out. If the checksum word is filled with the proper value, 47 then these routines will give a result of zero (useful for testing 48 purposes); 49 */ 50 51 #include <sys/types.h> 52 #include <netinet/in_systm.h> 53 #include <netinet/in.h> 54 #include <netinet/ip.h> 55 #include <netinet/tcp.h> 56 57 #include "alias.h" 58 #include "alias_local.h" 59 60 u_short 61 PacketAliasInternetChecksum(u_short *ptr, int nbytes) 62 { 63 int sum, oddbyte; 64 65 sum = 0; 66 while (nbytes > 1) 67 { 68 sum += *ptr++; 69 nbytes -= 2; 70 } 71 if (nbytes == 1) 72 { 73 oddbyte = 0; 74 ((u_char *) &oddbyte)[0] = *(u_char *) ptr; 75 ((u_char *) &oddbyte)[1] = 0; 76 sum += oddbyte; 77 } 78 sum = (sum >> 16) + (sum & 0xffff); 79 sum += (sum >> 16); 80 return(~sum); 81 } 82 83 u_short 84 IpChecksum(struct ip *pip) 85 { 86 return( PacketAliasInternetChecksum((u_short *) pip, 87 (pip->ip_hl << 2)) ); 88 89 } 90 91 u_short 92 TcpChecksum(struct ip *pip) 93 { 94 u_short *ptr; 95 struct tcphdr *tc; 96 int nhdr, ntcp, nbytes; 97 int sum, oddbyte; 98 99 nhdr = pip->ip_hl << 2; 100 ntcp = ntohs(pip->ip_len) - nhdr; 101 102 tc = (struct tcphdr *) ((char *) pip + nhdr); 103 ptr = (u_short *) tc; 104 105 /* Add up TCP header and data */ 106 nbytes = ntcp; 107 sum = 0; 108 while (nbytes > 1) 109 { 110 sum += *ptr++; 111 nbytes -= 2; 112 } 113 if (nbytes == 1) 114 { 115 oddbyte = 0; 116 ((u_char *) &oddbyte)[0] = *(u_char *) ptr; 117 ((u_char *) &oddbyte)[1] = 0; 118 sum += oddbyte; 119 } 120 121 /* "Pseudo-header" data */ 122 ptr = (u_short *) &(pip->ip_dst); 123 sum += *ptr++; 124 sum += *ptr; 125 ptr = (u_short *) &(pip->ip_src); 126 sum += *ptr++; 127 sum += *ptr; 128 sum += htons((u_short) ntcp); 129 sum += htons((u_short) pip->ip_p); 130 131 /* Roll over carry bits */ 132 sum = (sum >> 16) + (sum & 0xffff); 133 sum += (sum >> 16); 134 135 /* Return checksum */ 136 return((u_short) ~sum); 137 } 138 139 140 void 141 DifferentialChecksum(u_short *cksum, u_short *new, u_short *old, int n) 142 { 143 int i; 144 int accumulate; 145 146 accumulate = *cksum; 147 for (i=0; i<n; i++) 148 { 149 accumulate -= *new++; 150 accumulate += *old++; 151 } 152 153 if (accumulate < 0) 154 { 155 accumulate = -accumulate; 156 accumulate = (accumulate >> 16) + (accumulate & 0xffff); 157 accumulate += accumulate >> 16; 158 *cksum = (u_short) ~accumulate; 159 } 160 else 161 { 162 accumulate = (accumulate >> 16) + (accumulate & 0xffff); 163 accumulate += accumulate >> 16; 164 *cksum = (u_short) accumulate; 165 } 166 } 167 168