Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/sys/netpfil/pf/in4_cksum.c
39481 views
1
/* $OpenBSD: in4_cksum.c,v 1.7 2003/06/02 23:28:13 millert Exp $ */
2
/* $KAME: in4_cksum.c,v 1.10 2001/11/30 10:06:15 itojun Exp $ */
3
/* $NetBSD: in_cksum.c,v 1.13 1996/10/13 02:03:03 christos Exp $ */
4
5
/*-
6
* SPDX-License-Identifier: BSD-3-Clause
7
*
8
* Copyright (C) 1999 WIDE Project.
9
* All rights reserved.
10
*
11
* Redistribution and use in source and binary forms, with or without
12
* modification, are permitted provided that the following conditions
13
* are met:
14
* 1. Redistributions of source code must retain the above copyright
15
* notice, this list of conditions and the following disclaimer.
16
* 2. Redistributions in binary form must reproduce the above copyright
17
* notice, this list of conditions and the following disclaimer in the
18
* documentation and/or other materials provided with the distribution.
19
* 3. Neither the name of the project nor the names of its contributors
20
* may be used to endorse or promote products derived from this software
21
* without specific prior written permission.
22
*
23
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
24
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26
* ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
27
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33
* SUCH DAMAGE.
34
*/
35
36
/*
37
* Copyright (c) 1988, 1992, 1993
38
* The Regents of the University of California. All rights reserved.
39
*
40
* Redistribution and use in source and binary forms, with or without
41
* modification, are permitted provided that the following conditions
42
* are met:
43
* 1. Redistributions of source code must retain the above copyright
44
* notice, this list of conditions and the following disclaimer.
45
* 2. Redistributions in binary form must reproduce the above copyright
46
* notice, this list of conditions and the following disclaimer in the
47
* documentation and/or other materials provided with the distribution.
48
* 3. Neither the name of the University nor the names of its contributors
49
* may be used to endorse or promote products derived from this software
50
* without specific prior written permission.
51
*
52
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62
* SUCH DAMAGE.
63
*/
64
65
#include <sys/param.h>
66
#include <sys/systm.h>
67
#include <sys/mbuf.h>
68
69
#include <netinet/in.h>
70
#include <netinet/in_systm.h>
71
#include <netinet/ip.h>
72
#include <netinet/ip_var.h>
73
74
#include <machine/in_cksum.h>
75
76
#define ADDCARRY(x) (x > 65535 ? x -= 65535 : x)
77
#define REDUCE {l_util.l = sum; sum = l_util.s[0] + l_util.s[1]; (void)ADDCARRY(sum);}
78
79
int in4_cksum(struct mbuf *, u_int8_t, int, int);
80
81
int
82
in4_cksum(struct mbuf *m, u_int8_t nxt, int off, int len)
83
{
84
union {
85
struct ipovly ipov;
86
u_int16_t w[10];
87
} u;
88
union {
89
u_int16_t s[2];
90
u_int32_t l;
91
} l_util;
92
93
u_int16_t *w;
94
int psum;
95
int sum = 0;
96
97
if (nxt != 0) {
98
/* pseudo header */
99
if (off < sizeof(struct ipovly))
100
panic("in4_cksum: offset too short");
101
if (m->m_len < sizeof(struct ip))
102
panic("in4_cksum: bad mbuf chain");
103
bzero(&u.ipov, sizeof(u.ipov));
104
u.ipov.ih_len = htons(len);
105
u.ipov.ih_pr = nxt;
106
u.ipov.ih_src = mtod(m, struct ip *)->ip_src;
107
u.ipov.ih_dst = mtod(m, struct ip *)->ip_dst;
108
w = u.w;
109
/* assumes sizeof(ipov) == 20 */
110
sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3]; sum += w[4];
111
sum += w[5]; sum += w[6]; sum += w[7]; sum += w[8]; sum += w[9];
112
}
113
114
psum = in_cksum_skip(m, len + off, off);
115
psum = ~psum & 0xffff;
116
sum += psum;
117
REDUCE;
118
return (~sum & 0xffff);
119
}
120
121