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