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