in4_cksum.c revision 1.15 1 /* $NetBSD: in4_cksum.c,v 1.15 2008/01/25 21:12:15 joerg Exp $ */
2
3 /*
4 * Copyright (C) 1999 WIDE Project.
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 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 1988, 1992, 1993
34 * The Regents of the University of California. All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 * @(#)in_cksum.c 8.1 (Berkeley) 6/10/93
61 */
62
63 #include <sys/cdefs.h>
64 __KERNEL_RCSID(0, "$NetBSD: in4_cksum.c,v 1.15 2008/01/25 21:12:15 joerg Exp $");
65
66 #include <sys/param.h>
67 #include <sys/mbuf.h>
68 #include <sys/systm.h>
69 #include <sys/socket.h>
70 #include <net/route.h>
71 #include <netinet/in.h>
72 #include <netinet/in_systm.h>
73 #include <netinet/ip.h>
74 #include <netinet/ip_var.h>
75
76 /*
77 * Checksum routine for Internet Protocol family headers (Portable Version).
78 * This is only for IPv4 pseudo header checksum.
79 * No need to clear non-pseudo-header fields in IPv4 header.
80 * len is for actual payload size, and does not include IPv4 header and
81 * skipped header chain (off + len should be equal to the whole packet).
82 */
83
84 int
85 in4_cksum(struct mbuf *m, u_int8_t nxt, int off, int len)
86 {
87 uint32_t sum;
88 union {
89 struct ipovly ipov;
90 uint16_t w[10];
91 } u;
92 uint16_t *w;
93
94 sum = 0;
95
96 if (nxt == 0)
97 goto skip_phdr;
98
99 /* pseudo header */
100 if (off < sizeof(struct ipovly))
101 panic("in4_cksum: offset too short");
102 if (m->m_len < sizeof(struct ip))
103 panic("in4_cksum: bad mbuf chain");
104 bzero(&u.ipov, sizeof(u.ipov));
105 u.ipov.ih_len = htons(len);
106 u.ipov.ih_pr = nxt;
107 u.ipov.ih_src = mtod(m, struct ip *)->ip_src;
108 u.ipov.ih_dst = mtod(m, struct ip *)->ip_dst;
109 w = u.w;
110 /* assumes sizeof(ipov) == 20 */
111 sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3]; sum += w[4];
112 sum += w[5]; sum += w[6]; sum += w[7]; sum += w[8]; sum += w[9];
113
114 skip_phdr:
115 return cpu_in_cksum(m, len, off, sum);
116 }
117