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