1 /* -*- c-basic-offset: 4 -*-
2 *
3 * in_cksum.c -- Internet checksum
4 * parts borrowed, with bug fixes, from one of the BSDs
5 */
6
7 #include <click/config.h>
8 #include <clicknet/ip.h>
9 #if CLICK_LINUXMODULE
10 # include <linux/string.h>
11 #elif CLICK_BSDMODULE
12 # include <sys/param.h>
13 # include <sys/proc.h>
14 # include <sys/systm.h>
15 #else
16 # include <string.h>
17 #endif
18
19 #if !CLICK_LINUXMODULE
20 uint16_t
click_in_cksum(const unsigned char * addr,int len)21 click_in_cksum(const unsigned char *addr, int len)
22 {
23 int nleft = len;
24 const uint16_t *w = (const uint16_t *)addr;
25 uint32_t sum = 0;
26 uint16_t answer = 0;
27
28 /*
29 * Our algorithm is simple, using a 32 bit accumulator (sum), we add
30 * sequential 16 bit words to it, and at the end, fold back all the
31 * carry bits from the top 16 bits into the lower 16 bits.
32 */
33 while (nleft > 1) {
34 sum += *w++;
35 nleft -= 2;
36 }
37
38 /* mop up an odd byte, if necessary */
39 if (nleft == 1) {
40 *(unsigned char *)(&answer) = *(const unsigned char *)w ;
41 sum += answer;
42 }
43
44 /* add back carry outs from top 16 bits to low 16 bits */
45 sum = (sum & 0xffff) + (sum >> 16);
46 sum += (sum >> 16);
47 /* guaranteed now that the lower 16 bits of sum are correct */
48
49 answer = ~sum; /* truncate to 16 bits */
50 return answer;
51 }
52
53 uint16_t
click_in_cksum_pseudohdr_raw(uint32_t csum,uint32_t src,uint32_t dst,int proto,int packet_len)54 click_in_cksum_pseudohdr_raw(uint32_t csum, uint32_t src, uint32_t dst, int proto, int packet_len)
55 {
56 assert(csum <= 0xFFFF);
57 csum = ~csum & 0xFFFF;
58 # ifdef __i386__
59 // borrowed from Linux
60 __asm__("\n\
61 addl %1, %0\n\
62 adcl %2, %0\n\
63 adcl %3, %0\n\
64 adcl $0, %0\n"
65 : "=r" (csum)
66 : "g" (src), "g" (dst), "g" ((htons(packet_len) << 16) + (proto << 8)), "0" (csum));
67 __asm__("\n\
68 addl %1, %0\n\
69 adcl $0xffff, %0\n"
70 : "=r" (csum)
71 : "r" (csum << 16), "0" (csum & 0xFFFF0000));
72 return (~csum) >> 16;
73 # else
74 csum += (src & 0xffff) + (src >> 16);
75 csum += (dst & 0xffff) + (dst >> 16);
76 csum += htons(packet_len) + htons(proto);
77 csum = (csum & 0xffff) + (csum >> 16);
78 return ~(csum + (csum >> 16)) & 0xFFFF;
79 # endif
80 }
81 #endif
82
83 uint16_t
click_in_cksum_pseudohdr_hard(uint32_t csum,const struct click_ip * iph,int packet_len)84 click_in_cksum_pseudohdr_hard(uint32_t csum, const struct click_ip *iph, int packet_len)
85 {
86 const uint8_t *opt = (const uint8_t *)(iph + 1);
87 const uint8_t *end_opt = ((const uint8_t *)iph) + (iph->ip_hl << 2);
88 while (opt < end_opt) {
89 /* check one-byte options */
90 if (*opt == IPOPT_NOP) {
91 opt++;
92 continue;
93 } else if (*opt == IPOPT_EOL)
94 break;
95
96 /* check option length */
97 if (opt + 1 >= end_opt || opt[1] < 2 || opt + opt[1] > end_opt)
98 break;
99
100 /* grab correct final destination from source routing option */
101 if ((*opt == IPOPT_SSRR || *opt == IPOPT_LSRR) && opt[1] >= 7) {
102 uint32_t daddr;
103 memcpy(&daddr, opt + opt[1] - 4, 4);
104 return click_in_cksum_pseudohdr_raw(csum, iph->ip_src.s_addr, daddr, iph->ip_p, packet_len);
105 }
106
107 opt += opt[1];
108 }
109
110 return click_in_cksum_pseudohdr_raw(csum, iph->ip_src.s_addr, iph->ip_dst.s_addr, iph->ip_p, packet_len);
111 }
112
113 void
click_update_zero_in_cksum_hard(uint16_t * csum,const unsigned char * x,int len)114 click_update_zero_in_cksum_hard(uint16_t *csum, const unsigned char *x, int len)
115 {
116 for (; len > 0; --len, ++x)
117 if (*x)
118 return;
119 // if we get here, all bytes were zero, so the checksum is ~0
120 *csum = ~0;
121 }
122