1/* $NetBSD: in4_cksum.c,v 1.20 2014/11/30 18:15:41 christos Exp $ */
2
3/*-
4 * Copyright (c) 2008 Joerg Sonnenberger <joerg@NetBSD.org>.
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 *
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
16 * distribution.
17 *
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
29 * SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
33__KERNEL_RCSID(0, "$NetBSD: in4_cksum.c,v 1.20 2014/11/30 18:15:41 christos Exp $");
34
35#include <sys/param.h>
36#include <sys/mbuf.h>
37#include <netinet/in.h>
38#include <netinet/in_systm.h>
39#include <netinet/ip.h>
40
41/*
42 * Checksum of the IPv4 pseudo header.
43 *
44 * off is supposed to be the skipped IPv4 header, len is the payload size.
45 */
46#ifdef DIAGNOSTIC
47#define PANIC(a,...) panic(a, __VA_ARGS__)
48#else
49#define PANIC(a,...) do { \
50 printf(a, __VA_ARGS__); \
51 return -1; \
52} while (/*CONSTCOND*/0)
53#endif
54
55int
56in4_cksum(struct mbuf *m, u_int8_t nxt, int off, int len)
57{
58 uint32_t sum;
59 uint16_t *w;
60
61 if (__predict_false(m->m_len < sizeof(struct ip)))
62 PANIC("%s: mbuf %d too short for IP header %zu", __func__,
63 m->m_len, sizeof(struct ip));
64
65 if (nxt == 0)
66 return cpu_in_cksum(m, len, off, 0);
67
68 if (__predict_false(off < sizeof(struct ip)))
69 PANIC("%s: offset %d too short for IP header %zu", __func__,
70 off, sizeof(struct ip));
71
72 /*
73 * Compute the equivalent of:
74 * struct ipovly ip;
75 *
76 * bzero(sizeof(*ip));
77 * ip.ih_pr = nxt;
78 * ip.ip_len = htons(len);
79 * ip.ih_src = mtod(m, struct ip *)->ip_src;
80 * ip.ih_dst = mtod(m, struct ip *)->ip_dst;
81 * sum = one_add(&ip);
82 */
83
84#if BYTE_ORDER == LITTLE_ENDIAN
85 sum = ((len & 0xffff) + nxt) << 8;
86#else
87 sum = (len & 0xffff) + nxt;
88#endif
89 w = (uint16_t *)(mtod(m, char *) + offsetof(struct ip, ip_src));
90 if (__predict_true((uintptr_t)w % 2 == 0)) {
91 sum += w[0];
92 sum += w[1];
93 sum += w[2];
94 sum += w[3];
95 } else {
96 uint32_t partial;
97 w = (void *)((uintptr_t)w - 1);
98#if BYTE_ORDER == LITTLE_ENDIAN
99 partial = w[0] & 0xff00;
100#else
101 partial = w[0] & 0x00ff;
102#endif
103 partial += w[1];
104 partial += w[2];
105 partial += w[3];
106#if BYTE_ORDER == LITTLE_ENDIAN
107 partial += w[4] & 0x00ff;
108#else
109 partial += w[4] & 0xff00;
110#endif
111 sum += partial << 8;
112 }
113
114 return cpu_in_cksum(m, len, off, sum);
115}
116