libnetfilter_queue 1.0.5
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 <endian.h>
15#include <arpa/inet.h>
16#include <netinet/ip.h>
17#include <netinet/ip6.h>
18#include <netinet/tcp.h>
19
20#include <libnetfilter_queue/libnetfilter_queue.h>
21
22#include "internal.h"
23
24uint16_t nfq_checksum(uint32_t sum, uint16_t *buf, int size)
25{
26 while (size > 1) {
27 sum += *buf++;
28 size -= sizeof(uint16_t);
29 }
30 if (size) {
31#if __BYTE_ORDER == __BIG_ENDIAN
32 sum += (uint16_t)*(uint8_t *)buf << 8;
33#else
34 sum += (uint16_t)*(uint8_t *)buf;
35#endif
36 }
37
38 sum = (sum >> 16) + (sum & 0xffff);
39 sum += (sum >>16);
40
41 return (uint16_t)(~sum);
42}
43
44uint16_t nfq_checksum_tcpudp_ipv4(struct iphdr *iph, uint16_t protonum)
45{
46 uint32_t sum = 0;
47 uint32_t iph_len = iph->ihl*4;
48 uint32_t len = ntohs(iph->tot_len) - iph_len;
49 uint8_t *payload = (uint8_t *)iph + iph_len;
50
51 sum += (iph->saddr >> 16) & 0xFFFF;
52 sum += (iph->saddr) & 0xFFFF;
53 sum += (iph->daddr >> 16) & 0xFFFF;
54 sum += (iph->daddr) & 0xFFFF;
55 sum += htons(protonum);
56 sum += htons(len);
57
58 return nfq_checksum(sum, (uint16_t *)payload, len);
59}
60
61uint16_t nfq_checksum_tcpudp_ipv6(struct ip6_hdr *ip6h, void *transport_hdr,
62 uint16_t protonum)
63{
64 uint32_t sum = 0;
65 uint32_t hdr_len = (uint8_t *)transport_hdr - (uint8_t *)ip6h;
66 /* Allow for extra headers before the UDP header */
67 /* TODO: Deal with routing headers */
68 uint32_t len = ntohs(ip6h->ip6_plen) - (hdr_len - sizeof *ip6h);
69 uint8_t *payload = (uint8_t *)ip6h + hdr_len;
70 int i;
71
72 for (i=0; i<8; i++) {
73 sum += (ip6h->ip6_src.s6_addr16[i]);
74 }
75 for (i=0; i<8; i++) {
76 sum += (ip6h->ip6_dst.s6_addr16[i]);
77 }
78 sum += htons(protonum);
79 sum += htons(len);
80
81 return nfq_checksum(sum, (uint16_t *)payload, len);
82}
83