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