ipx_cksum.c revision 165899
1/*- 2 * Copyright (c) 1982, 1992, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * Copyright (c) 1995, Mike Mitchell 30 * 31 * Redistribution and use in source and binary forms, with or without 32 * modification, are permitted provided that the following conditions 33 * are met: 34 * 1. Redistributions of source code must retain the above copyright 35 * notice, this list of conditions and the following disclaimer. 36 * 2. Redistributions in binary form must reproduce the above copyright 37 * notice, this list of conditions and the following disclaimer in the 38 * documentation and/or other materials provided with the distribution. 39 * 3. All advertising materials mentioning features or use of this software 40 * must display the following acknowledgement: 41 * This product includes software developed by the University of 42 * California, Berkeley and its contributors. 43 * 4. Neither the name of the University nor the names of its contributors 44 * may be used to endorse or promote products derived from this software 45 * without specific prior written permission. 46 * 47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 57 * SUCH DAMAGE. 58 * 59 * @(#)ipx_cksum.c 60 */ 61 62#include <sys/cdefs.h> 63__FBSDID("$FreeBSD: head/sys/netipx/ipx_cksum.c 165899 2007-01-08 22:14:00Z rwatson $"); 64 65#include <sys/param.h> 66#include <sys/mbuf.h> 67#include <sys/libkern.h> 68 69#include <netipx/ipx.h> 70#include <netipx/ipx_var.h> 71 72 73#define SUMADV sum += *w++ 74 75u_short 76ipx_cksum(struct mbuf *m, int len) { 77 u_int32_t sum = 0; 78 u_char *w; 79 u_char oldtc; 80 int mlen, words; 81 struct ipx *ipx; 82 union { 83 u_char b[2]; 84 u_short w; 85 } buf; 86 87 ipx = mtod(m, struct ipx*); 88 oldtc = ipx->ipx_tc; 89 ipx->ipx_tc = 0; 90 w = (u_char *)&ipx->ipx_len; 91 len -= 2; 92 mlen = 2; 93 94 for(;;) { 95 mlen = imin(m->m_len - mlen, len); 96 words = mlen / 2; 97 len -= mlen & ~1; 98 while (words >= 16) { 99 SUMADV; SUMADV; SUMADV; SUMADV; 100 SUMADV; SUMADV; SUMADV; SUMADV; 101 SUMADV; SUMADV; SUMADV; SUMADV; 102 SUMADV; SUMADV; SUMADV; SUMADV; 103 words -= 16; 104 } 105 while (words--) 106 SUMADV; 107 if (len == 0) 108 break; 109 mlen &= 1; 110 if (mlen) { 111 buf.b[0] = *w; 112 if (--len == 0) { 113 buf.b[1] = 0; 114 sum += buf.w; 115 break; 116 } 117 } 118 m = m->m_next; 119 if (m == NULL) 120 break; 121 w = mtod(m, u_char *); 122 if (mlen) { 123 buf.b[1] = *w; 124 sum += buf.w; 125 w++; 126 if (--len == 0) 127 break; 128 } 129 } 130 131 ipx->ipx_tc = oldtc; 132 133 sum = (sum & 0xffff) + (sum >> 16); 134 if (sum >= 0x10000) 135 sum++; 136 if (sum) 137 sum = ~sum; 138 return (sum); 139} 140