Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2001-2004 Swedish Institute of Computer Science. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without modification, |
| 6 | * are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT |
| 19 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 20 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
| 21 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
| 24 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY |
| 25 | * OF SUCH DAMAGE. |
| 26 | * |
| 27 | * This file is part of the lwIP TCP/IP stack. |
| 28 | * |
| 29 | * Author: Adam Dunkels <adam@sics.se> |
| 30 | * |
| 31 | */ |
| 32 | |
| 33 | |
| 34 | /* inet6.c |
| 35 | * |
| 36 | * Functions common to all TCP/IP modules, such as the Internet checksum and the |
| 37 | * byte order functions. |
| 38 | * |
| 39 | */ |
| 40 | |
| 41 | |
| 42 | #include "lwip/opt.h" |
| 43 | |
| 44 | #include "lwip/def.h" |
| 45 | #include "lwip/inet.h" |
| 46 | |
| 47 | |
| 48 | |
| 49 | /* chksum: |
| 50 | * |
| 51 | * Sums up all 16 bit words in a memory portion. Also includes any odd byte. |
| 52 | * This function is used by the other checksum functions. |
| 53 | * |
| 54 | * For now, this is not optimized. Must be optimized for the particular processor |
| 55 | * arcitecture on which it is to run. Preferebly coded in assembler. |
| 56 | */ |
| 57 | |
| 58 | static u32_t |
| 59 | chksum(void *dataptr, u16_t len) |
| 60 | { |
| 61 | u16_t *sdataptr = dataptr; |
| 62 | u32_t acc; |
| 63 | |
| 64 | |
| 65 | for(acc = 0; len > 1; len -= 2) { |
| 66 | acc += *sdataptr++; |
| 67 | } |
| 68 | |
| 69 | /* add up any odd byte */ |
| 70 | if (len == 1) { |
| 71 | acc += htons((u16_t)(*(u8_t *)dataptr) << 8); |
| 72 | } |
| 73 | |
| 74 | return acc; |
| 75 | |
| 76 | } |
| 77 | |
| 78 | /* inet_chksum_pseudo: |
| 79 | * |
| 80 | * Calculates the pseudo Internet checksum used by TCP and UDP for a pbuf chain. |
| 81 | */ |
| 82 | |
| 83 | u16_t |
| 84 | inet_chksum_pseudo(struct pbuf *p, |
| 85 | struct ip_addr *src, struct ip_addr *dest, |
| 86 | u8_t proto, u32_t proto_len) |
| 87 | { |
| 88 | u32_t acc; |
| 89 | struct pbuf *q; |
| 90 | u8_t swapped, i; |
| 91 | |
| 92 | acc = 0; |
| 93 | swapped = 0; |
| 94 | for(q = p; q != NULL; q = q->next) { |
| 95 | acc += chksum(q->payload, q->len); |
| 96 | while (acc >> 16) { |
| 97 | acc = (acc & 0xffff) + (acc >> 16); |
| 98 | } |
| 99 | if (q->len % 2 != 0) { |
| 100 | swapped = 1 - swapped; |
| 101 | acc = ((acc & 0xff) << 8) | ((acc & 0xff00) >> 8); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | if (swapped) { |
| 106 | acc = ((acc & 0xff) << 8) | ((acc & 0xff00) >> 8); |
| 107 | } |
| 108 | |
| 109 | for(i = 0; i < 8; i++) { |
| 110 | acc += ((u16_t *)src->addr)[i] & 0xffff; |
| 111 | acc += ((u16_t *)dest->addr)[i] & 0xffff; |
| 112 | while (acc >> 16) { |
| 113 | acc = (acc & 0xffff) + (acc >> 16); |
| 114 | } |
| 115 | } |
| 116 | acc += (u16_t)htons((u16_t)proto); |
| 117 | acc += ((u16_t *)&proto_len)[0] & 0xffff; |
| 118 | acc += ((u16_t *)&proto_len)[1] & 0xffff; |
| 119 | |
| 120 | while (acc >> 16) { |
| 121 | acc = (acc & 0xffff) + (acc >> 16); |
| 122 | } |
| 123 | return ~(acc & 0xffff); |
| 124 | } |
| 125 | |
| 126 | /* inet_chksum: |
| 127 | * |
| 128 | * Calculates the Internet checksum over a portion of memory. Used primarely for IP |
| 129 | * and ICMP. |
| 130 | */ |
| 131 | |
| 132 | u16_t |
| 133 | inet_chksum(void *dataptr, u16_t len) |
| 134 | { |
| 135 | u32_t acc, sum; |
| 136 | |
| 137 | acc = chksum(dataptr, len); |
| 138 | sum = (acc & 0xffff) + (acc >> 16); |
| 139 | sum += (sum >> 16); |
| 140 | return ~(sum & 0xffff); |
| 141 | } |
| 142 | |
| 143 | u16_t |
| 144 | inet_chksum_pbuf(struct pbuf *p) |
| 145 | { |
| 146 | u32_t acc; |
| 147 | struct pbuf *q; |
| 148 | u8_t swapped; |
| 149 | |
| 150 | acc = 0; |
| 151 | swapped = 0; |
| 152 | for(q = p; q != NULL; q = q->next) { |
| 153 | acc += chksum(q->payload, q->len); |
| 154 | while (acc >> 16) { |
| 155 | acc = (acc & 0xffff) + (acc >> 16); |
| 156 | } |
| 157 | if (q->len % 2 != 0) { |
| 158 | swapped = 1 - swapped; |
| 159 | acc = (acc & 0xff << 8) | (acc & 0xff00 >> 8); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | if (swapped) { |
| 164 | acc = ((acc & 0xff) << 8) | ((acc & 0xff00) >> 8); |
| 165 | } |
| 166 | return ~(acc & 0xffff); |
| 167 | } |
| 168 | |