ioops: Constify `name' parameter to tun_open_or_die()
[netsniff-ng.git] / csum.h
blobcf4599a6a922807484fe157ad2f1161a90d3e2dc
1 #ifndef CSUM_H
2 #define CSUM_H
4 #include <netinet/in.h>
5 #include <netinet/ip.h>
7 #include "built_in.h"
9 static inline unsigned short csum(unsigned short *buf, int nwords)
11 unsigned long sum;
13 for (sum = 0; nwords > 0; nwords--)
14 sum += *buf++;
15 sum = (sum >> 16) + (sum & 0xffff);
16 sum += (sum >> 16);
18 return ~sum;
21 static inline uint16_t calc_csum(void *addr, size_t len,
22 int ccsum __maybe_unused)
24 return csum(addr, len >> 1);
27 static inline uint16_t csum_expected(uint16_t sum, uint16_t computed_sum)
29 uint32_t shouldbe;
31 shouldbe = sum;
32 shouldbe += ntohs(computed_sum);
33 shouldbe = (shouldbe & 0xFFFF) + (shouldbe >> 16);
34 shouldbe = (shouldbe & 0xFFFF) + (shouldbe >> 16);
36 return shouldbe;
39 /* Taken and modified from tcpdump, Copyright belongs to them! */
41 struct cksum_vec {
42 const u8 *ptr;
43 int len;
46 #define ADDCARRY(x) \
47 do { if ((x) > 65535) \
48 (x) -= 65535; \
49 } while (0)
51 #define REDUCE \
52 do { \
53 l_util.l = sum; \
54 sum = l_util.s[0] + l_util.s[1]; \
55 ADDCARRY(sum); \
56 } while (0)
58 static inline u16 __in_cksum(const struct cksum_vec *vec, int veclen)
60 const u16 *w;
61 int sum = 0, mlen = 0;
62 int byte_swapped = 0;
63 union {
64 u8 c[2];
65 u16 s;
66 } s_util;
67 union {
68 u16 s[2];
69 u32 l;
70 } l_util;
72 for (; veclen != 0; vec++, veclen--) {
73 if (vec->len == 0)
74 continue;
76 w = (const u16 *) (void *) vec->ptr;
78 if (mlen == -1) {
79 s_util.c[1] = *(const u8 *) w;
80 sum += s_util.s;
81 w = (const u16 *) (void *) ((const u8 *) w + 1);
82 mlen = vec->len - 1;
83 } else
84 mlen = vec->len;
86 if ((1 & (unsigned long) w) && (mlen > 0)) {
87 REDUCE;
88 sum <<= 8;
89 s_util.c[0] = *(const u8 *) w;
90 w = (const u16 *) (void *) ((const u8 *) w + 1);
91 mlen--;
92 byte_swapped = 1;
95 while ((mlen -= 32) >= 0) {
96 sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
97 sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7];
98 sum += w[8]; sum += w[9]; sum += w[10]; sum += w[11];
99 sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15];
100 w += 16;
103 mlen += 32;
105 while ((mlen -= 8) >= 0) {
106 sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
107 w += 4;
110 mlen += 8;
112 if (mlen == 0 && byte_swapped == 0)
113 continue;
115 REDUCE;
117 while ((mlen -= 2) >= 0) {
118 sum += *w++;
121 if (byte_swapped) {
122 REDUCE;
123 sum <<= 8;
124 byte_swapped = 0;
126 if (mlen == -1) {
127 s_util.c[1] = *(const u8 *) w;
128 sum += s_util.s;
129 mlen = 0;
130 } else
131 mlen = -1;
132 } else if (mlen == -1)
133 s_util.c[0] = *(const u8 *) w;
136 if (mlen == -1) {
137 s_util.c[1] = 0;
138 sum += s_util.s;
141 REDUCE;
143 return (~sum & 0xffff);
146 static inline u16 p4_csum(const struct ip *ip, const u8 *data, u16 len,
147 u8 next_proto)
149 struct cksum_vec vec[2];
150 struct pseudo_hdr {
151 u32 src;
152 u32 dst;
153 u8 mbz;
154 u8 proto;
155 u16 len;
156 } ph;
158 memset(&ph, 0, sizeof(ph));
159 ph.len = htons(len);
160 ph.mbz = 0;
161 ph.proto = next_proto;
162 ph.src = ip->ip_src.s_addr;
163 ph.dst = ip->ip_dst.s_addr;
165 vec[0].ptr = (const u8 *) (void *) &ph;
166 vec[0].len = sizeof(ph);
168 vec[1].ptr = data;
169 vec[1].len = len;
171 return __in_cksum(vec, 2);
174 #endif /* CSUM_H */