1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2001 Charles Mott <cm@linktel.net>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD$");
31
32/*
33    Alias_util.c contains general utilities used by other functions
34    in the packet aliasing module.  At the moment, there are functions
35    for computing IP header and TCP packet checksums.
36
37    The checksum routines are based upon example code in a Unix networking
38    text written by Stevens (sorry, I can't remember the title -- but
39    at least this is a good author).
40
41    Initial Version:  August, 1996  (cjm)
42
43    Version 1.7:  January 9, 1997
44	 Added differential checksum update function.
45*/
46
47#ifdef _KERNEL
48#include <sys/param.h>
49#include <sys/proc.h>
50#else
51#include <sys/types.h>
52#include <stdio.h>
53#endif
54
55#include <netinet/in_systm.h>
56#include <netinet/in.h>
57#include <netinet/ip.h>
58#include <netinet/tcp.h>
59
60#ifdef _KERNEL
61#include <netinet/libalias/alias.h>
62#include <netinet/libalias/alias_local.h>
63#else
64#include "alias.h"
65#include "alias_local.h"
66#endif
67
68/*
69 * Note: the checksum routines assume that the actual checksum word has
70 * been zeroed out.  If the checksum word is filled with the proper value,
71 * then these routines will give a result of zero (useful for testing
72 * purposes);
73 */
74u_short
75LibAliasInternetChecksum(struct libalias *la __unused, u_short *ptr,
76    int nbytes)
77{
78	int sum, oddbyte;
79
80	LIBALIAS_LOCK(la);
81	sum = 0;
82	while (nbytes > 1) {
83		sum += *ptr++;
84		nbytes -= 2;
85	}
86	if (nbytes == 1) {
87		oddbyte = 0;
88		((u_char *)&oddbyte)[0] = *(u_char *)ptr;
89		((u_char *)&oddbyte)[1] = 0;
90		sum += oddbyte;
91	}
92	sum = (sum >> 16) + (sum & 0xffff);
93	sum += (sum >> 16);
94	LIBALIAS_UNLOCK(la);
95	return (~sum);
96}
97
98#ifndef _KERNEL
99u_short
100IpChecksum(struct ip *pip)
101{
102	return (LibAliasInternetChecksum(NULL, (u_short *)pip,
103	    (pip->ip_hl << 2)));
104
105}
106
107u_short
108TcpChecksum(struct ip *pip)
109{
110	u_short *ptr;
111	struct tcphdr *tc;
112	int nhdr, ntcp, nbytes;
113	int sum, oddbyte;
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	ptr = (void *)&pip->ip_dst;
136	sum += *ptr++;
137	sum += *ptr;
138	ptr = (void *)&pip->ip_src;
139	sum += *ptr++;
140	sum += *ptr;
141	sum += htons((u_short)ntcp);
142	sum += htons((u_short)pip->ip_p);
143
144/* Roll over carry bits */
145	sum = (sum >> 16) + (sum & 0xffff);
146	sum += (sum >> 16);
147
148/* Return checksum */
149	return ((u_short)~sum);
150}
151#endif	/* not _KERNEL */
152
153void
154DifferentialChecksum(u_short *cksum, void *newp, void *oldp, int n)
155{
156	int i;
157	int accumulate;
158	u_short *new = newp;
159	u_short *old = oldp;
160
161	accumulate = *cksum;
162	for (i = 0; i < n; i++) {
163		accumulate -= *new++;
164		accumulate += *old++;
165	}
166
167	if (accumulate < 0) {
168		accumulate = -accumulate;
169		accumulate = (accumulate >> 16) + (accumulate & 0xffff);
170		accumulate += accumulate >> 16;
171		*cksum = (u_short)~accumulate;
172	} else {
173		accumulate = (accumulate >> 16) + (accumulate & 0xffff);
174		accumulate += accumulate >> 16;
175		*cksum = (u_short)accumulate;
176	}
177}
178