1 /* $NetBSD: in4_cksum.c,v 1.16 2008/02/07 22:45:20 joerg Exp $ */
4 * Copyright (c) 2008 Joerg Sonnenberger <joerg@NetBSD.org>.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
28 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: in4_cksum.c,v 1.16 2008/02/07 22:45:20 joerg Exp $");
35 #include <sys/param.h>
37 #include <netinet/in.h>
38 #include <netinet/in_systm.h>
39 #include <netinet/ip.h>
42 * Checksum of the IPv4 pseudo header.
44 * off is supposed to be the skipped IPv4 header, len is the payload size.
48 in4_cksum(struct mbuf
*m
, u_int8_t nxt
, int off
, int len
)
53 if (__predict_false(off
< sizeof(struct ip
)))
54 panic("in4_cksum: offset too short for IP header");
55 if (__predict_false(m
->m_len
< sizeof(struct ip
)))
56 panic("in4_cksum: mbuf too short for IP header");
59 return cpu_in_cksum(m
, len
, off
, 0);
62 * Compute the equivalent of:
67 * ip.ip_len = htons(len);
68 * ip.ih_src = mtod(m, struct ip *)->ip_src;
69 * ip.ih_dst = mtod(m, struct ip *)->ip_dst;
73 #if BYTE_ORDER == LITTLE_ENDIAN
74 sum
= ((len
& 0xffff) + nxt
) << 8;
76 sum
= (len
& 0xffff) + nxt
;
78 w
= (uint16_t *)(mtod(m
, char *) + offsetof(struct ip
, ip_src
));
79 if (__predict_true((uintptr_t)w
% 2 == 0)) {
86 w
= (void *)((uintptr_t)w
- 1);
87 #if BYTE_ORDER == LITTLE_ENDIAN
88 partial
= w
[0] & 0xff00;
90 partial
= w
[0] & 0x00ff;
95 #if BYTE_ORDER == LITTLE_ENDIAN
96 partial
+= w
[4] & 0x00ff;
98 partial
+= w
[4] & 0xff00;
103 return cpu_in_cksum(m
, len
, off
, sum
);