xref: /freebsd/sys/netinet/libalias/alias_util.c (revision 4f29da19bd44f0e99f021510460a81bf754c21d2)
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 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
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 #ifdef _KERNEL
47 #include <sys/param.h>
48 #else
49 #include <sys/types.h>
50 #include <stdio.h>
51 #endif
52 
53 #include <netinet/in_systm.h>
54 #include <netinet/in.h>
55 #include <netinet/ip.h>
56 #include <netinet/tcp.h>
57 
58 #ifdef _KERNEL
59 #include <netinet/libalias/alias.h>
60 #include <netinet/libalias/alias_local.h>
61 #else
62 #include "alias.h"
63 #include "alias_local.h"
64 #endif
65 
66 /*
67  * Note: the checksum routines assume that the actual checksum word has
68  * been zeroed out.  If the checksum word is filled with the proper value,
69  * then these routines will give a result of zero (useful for testing
70  * purposes);
71  */
72 u_short
73 LibAliasInternetChecksum(struct libalias *la __unused, u_short * ptr,
74 	int nbytes)
75 {
76 	int sum, oddbyte;
77 
78 	sum = 0;
79 	while (nbytes > 1) {
80 		sum += *ptr++;
81 		nbytes -= 2;
82 	}
83 	if (nbytes == 1) {
84 		oddbyte = 0;
85 		((u_char *) & oddbyte)[0] = *(u_char *) ptr;
86 		((u_char *) & oddbyte)[1] = 0;
87 		sum += oddbyte;
88 	}
89 	sum = (sum >> 16) + (sum & 0xffff);
90 	sum += (sum >> 16);
91 	return (~sum);
92 }
93 
94 #ifndef	_KERNEL
95 u_short
96 IpChecksum(struct ip *pip)
97 {
98 	return (LibAliasInternetChecksum(NULL, (u_short *) pip,
99 	    (pip->ip_hl << 2)));
100 
101 }
102 
103 u_short
104 TcpChecksum(struct ip *pip)
105 {
106 	u_short *ptr;
107 	struct tcphdr *tc;
108 	int nhdr, ntcp, nbytes;
109 	int sum, oddbyte;
110 
111 	nhdr = pip->ip_hl << 2;
112 	ntcp = ntohs(pip->ip_len) - nhdr;
113 
114 	tc = (struct tcphdr *)ip_next(pip);
115 	ptr = (u_short *) tc;
116 
117 /* Add up TCP header and data */
118 	nbytes = ntcp;
119 	sum = 0;
120 	while (nbytes > 1) {
121 		sum += *ptr++;
122 		nbytes -= 2;
123 	}
124 	if (nbytes == 1) {
125 		oddbyte = 0;
126 		((u_char *) & oddbyte)[0] = *(u_char *) ptr;
127 		((u_char *) & oddbyte)[1] = 0;
128 		sum += oddbyte;
129 	}
130 /* "Pseudo-header" data */
131 	ptr = (u_short *) & (pip->ip_dst);
132 	sum += *ptr++;
133 	sum += *ptr;
134 	ptr = (u_short *) & (pip->ip_src);
135 	sum += *ptr++;
136 	sum += *ptr;
137 	sum += htons((u_short) ntcp);
138 	sum += htons((u_short) pip->ip_p);
139 
140 /* Roll over carry bits */
141 	sum = (sum >> 16) + (sum & 0xffff);
142 	sum += (sum >> 16);
143 
144 /* Return checksum */
145 	return ((u_short) ~ sum);
146 }
147 #endif	/* not _KERNEL */
148 
149 void
150 DifferentialChecksum(u_short * cksum, void *newp, void *oldp, int n)
151 {
152 	int i;
153 	int accumulate;
154 	u_short *new = newp;
155 	u_short *old = oldp;
156 
157 	accumulate = *cksum;
158 	for (i = 0; i < n; i++) {
159 		accumulate -= *new++;
160 		accumulate += *old++;
161 	}
162 
163 	if (accumulate < 0) {
164 		accumulate = -accumulate;
165 		accumulate = (accumulate >> 16) + (accumulate & 0xffff);
166 		accumulate += accumulate >> 16;
167 		*cksum = (u_short) ~ accumulate;
168 	} else {
169 		accumulate = (accumulate >> 16) + (accumulate & 0xffff);
170 		accumulate += accumulate >> 16;
171 		*cksum = (u_short) accumulate;
172 	}
173 }
174