libnetfilter_queue  1.0.3
checksum.c
1 /*
2  * (C) 2012 by Pablo Neira Ayuso <pablo@netfilter.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This code has been sponsored by Vyatta Inc. <http://www.vyatta.com>
10  */
11 
12 #include <stdio.h>
13 #include <stdbool.h>
14 #include <arpa/inet.h>
15 #include <netinet/ip.h>
16 #include <netinet/ip6.h>
17 #include <netinet/tcp.h>
18 
19 #include <libnetfilter_queue/libnetfilter_queue.h>
20 
21 #include "internal.h"
22 
23 uint16_t nfq_checksum(uint32_t sum, uint16_t *buf, int size)
24 {
25  while (size > 1) {
26  sum += *buf++;
27  size -= sizeof(uint16_t);
28  }
29  if (size)
30  sum += *(uint8_t *)buf;
31 
32  sum = (sum >> 16) + (sum & 0xffff);
33  sum += (sum >>16);
34 
35  return (uint16_t)(~sum);
36 }
37 
38 uint16_t nfq_checksum_tcpudp_ipv4(struct iphdr *iph)
39 {
40  uint32_t sum = 0;
41  uint32_t iph_len = iph->ihl*4;
42  uint32_t len = ntohs(iph->tot_len) - iph_len;
43  uint8_t *payload = (uint8_t *)iph + iph_len;
44 
45  sum += (iph->saddr >> 16) & 0xFFFF;
46  sum += (iph->saddr) & 0xFFFF;
47  sum += (iph->daddr >> 16) & 0xFFFF;
48  sum += (iph->daddr) & 0xFFFF;
49  sum += htons(IPPROTO_TCP);
50  sum += htons(len);
51 
52  return nfq_checksum(sum, (uint16_t *)payload, len);
53 }
54 
55 uint16_t nfq_checksum_tcpudp_ipv6(struct ip6_hdr *ip6h, void *transport_hdr)
56 {
57  uint32_t sum = 0;
58  uint32_t hdr_len = (uint32_t *)transport_hdr - (uint32_t *)ip6h;
59  uint32_t len = ip6h->ip6_plen - hdr_len;
60  uint8_t *payload = (uint8_t *)ip6h + hdr_len;
61  int i;
62 
63  for (i=0; i<8; i++) {
64  sum += (ip6h->ip6_src.s6_addr16[i] >> 16) & 0xFFFF;
65  sum += (ip6h->ip6_src.s6_addr16[i]) & 0xFFFF;
66  }
67  for (i=0; i<8; i++) {
68  sum += (ip6h->ip6_dst.s6_addr16[i] >> 16) & 0xFFFF;
69  sum += (ip6h->ip6_dst.s6_addr16[i]) & 0xFFFF;
70  }
71  sum += htons(IPPROTO_TCP);
72  sum += htons(ip6h->ip6_plen);
73 
74  return nfq_checksum(sum, (uint16_t *)payload, len);
75 }
76