Skip to content

Instantly share code, notes, and snippets.

@cmouse
Created July 1, 2013 20:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmouse/5904571 to your computer and use it in GitHub Desktop.
Save cmouse/5904571 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <arpa/inet.h>
uint16_t icmp6_checksum(const unsigned char *src_addr, const unsigned char *dest_addr, unsigned char *buff, size_t dlen, uint16_t *target) {
uint16_t word16;
uint32_t sum=0;
size_t i;
uint16_t pad;
pad=dlen&1;
for (i=0;i<16;i=i+2){
word16 =((src_addr[i]<<8)&0xFF00)+(src_addr[i+1]&0xFF);
sum += (uint32_t)word16;
}
for (i=0;i<16;i=i+2){
word16 =((dest_addr[i]<<8)&0xFF00)+(dest_addr[i+1]&0xFF);
sum += (uint32_t)word16;
}
sum += ntohl(dlen);
sum += 0x003A; // ICMPv6
for (i=0;i<dlen+pad;i=i+2){
word16 =((buff[i]<<8)&0xff00)+(buff[i+1]&0xff);
sum += (uint32_t)word16;
}
sum = (sum & 0xFFFF)+(sum >> 16);
sum += (sum >> 16);
(*target) = htons((uint16_t)~sum);
return *target;
}
int main(void) {
unsigned char packet[] = {0x88,0x43,0xe1,0xde,0x22,0xc0,0xd0,0xd0,0xfd,0x09,0x34,0x2d,
0x81,0x00,0xe0,0xce,0x86,0xdd,0x6e,0x00,0x00,0x00,0x00,0x20,
0x3a,0xff,0xff,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x01,0xff,0x01,0x34,0x2d,0xfe,0x80,0x00,0x00,0x00,0x00,
0x00,0x00,0x8a,0x43,0xe1,0xff,0xfe,0xde,0x22,0xc0,0x88,0x00,
0x48,0x2f,0x00,0x00,0x00,0x40,0x20,0x01,0x06,0xe8,0x02,0x80,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x34,0x2d,0x01,0x01,
0x88,0x43,0xe1,0xde,0x22,0xc0};
*(uint16_t*)(packet + 14 + 4 + 40 + 2) = 0;
icmp6_checksum(packet + 14 + 4 + 8, packet + 14 + 4 + 24, packet + 14 + 4 + 40, 32, (uint16_t*)(packet + 14 + 4 + 42));
printf("%02x\n", *(uint16_t*)(packet + 14 + 4 + 40 + 2));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment