1 /* $NetBSD: in4_cksum.c,v 1.19 2013/03/12 21:54:36 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.19 2013/03/12 21:54:36 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 55 int 56 in4_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(off < sizeof(struct ip))) 62 PANIC("%s: offset %d too short for IP header %zu", __func__, 63 off, sizeof(struct ip)); 64 if (__predict_false(m->m_len < sizeof(struct ip))) 65 PANIC("%s: mbuf %d too short for IP header %zu", __func__, 66 m->m_len, sizeof(struct ip)); 67 68 if (nxt == 0) 69 return cpu_in_cksum(m, len, off, 0); 70 71 /* 72 * Compute the equivalent of: 73 * struct ipovly ip; 74 * 75 * bzero(sizeof(*ip)); 76 * ip.ih_pr = nxt; 77 * ip.ip_len = htons(len); 78 * ip.ih_src = mtod(m, struct ip *)->ip_src; 79 * ip.ih_dst = mtod(m, struct ip *)->ip_dst; 80 * sum = one_add(&ip); 81 */ 82 83 #if BYTE_ORDER == LITTLE_ENDIAN 84 sum = ((len & 0xffff) + nxt) << 8; 85 #else 86 sum = (len & 0xffff) + nxt; 87 #endif 88 w = (uint16_t *)(mtod(m, char *) + offsetof(struct ip, ip_src)); 89 if (__predict_true((uintptr_t)w % 2 == 0)) { 90 sum += w[0]; 91 sum += w[1]; 92 sum += w[2]; 93 sum += w[3]; 94 } else { 95 uint32_t partial; 96 w = (void *)((uintptr_t)w - 1); 97 #if BYTE_ORDER == LITTLE_ENDIAN 98 partial = w[0] & 0xff00; 99 #else 100 partial = w[0] & 0x00ff; 101 #endif 102 partial += w[1]; 103 partial += w[2]; 104 partial += w[3]; 105 #if BYTE_ORDER == LITTLE_ENDIAN 106 partial += w[4] & 0x00ff; 107 #else 108 partial += w[4] & 0xff00; 109 #endif 110 sum += partial << 8; 111 } 112 113 return cpu_in_cksum(m, len, off, sum); 114 } 115