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$"); 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{ 78 u_int32_t sum = 0; 79 u_char *w; 80 u_char oldtc; 81 int mlen, words; 82 struct ipx *ipx; 83 union { 84 u_char b[2]; 85 u_short w; 86 } buf; 87 88 ipx = mtod(m, struct ipx*); 89 oldtc = ipx->ipx_tc; 90 ipx->ipx_tc = 0; 91 w = (u_char *)&ipx->ipx_len; 92 len -= 2; 93 mlen = 2; 94 95 for(;;) { 96 mlen = imin(m->m_len - mlen, len); 97 words = mlen / 2; 98 len -= mlen & ~1; 99 while (words >= 16) { 100 SUMADV; SUMADV; SUMADV; SUMADV; 101 SUMADV; SUMADV; SUMADV; SUMADV; 102 SUMADV; SUMADV; SUMADV; SUMADV; 103 SUMADV; SUMADV; SUMADV; SUMADV; 104 words -= 16; 105 } 106 while (words--) 107 SUMADV; 108 if (len == 0) 109 break; 110 mlen &= 1; 111 if (mlen) { 112 buf.b[0] = *w; 113 if (--len == 0) { 114 buf.b[1] = 0; 115 sum += buf.w; 116 break; 117 } 118 } 119 m = m->m_next; 120 if (m == NULL) 121 break; 122 w = mtod(m, u_char *); 123 if (mlen) { 124 buf.b[1] = *w; 125 sum += buf.w; 126 w++; 127 if (--len == 0) 128 break; 129 } 130 } 131 132 ipx->ipx_tc = oldtc; 133 134 sum = (sum & 0xffff) + (sum >> 16); 135 if (sum >= 0x10000) 136 sum++; 137 if (sum) 138 sum = ~sum; 139 return (sum); 140} 141