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