libnetfilter_queue: fix checksum computation
[oweals/openwrt.git] / package / libs / libnetfilter-queue / patches / 100-checksum_computation.patch
1 --- a/src/extra/checksum.c
2 +++ b/src/extra/checksum.c
3 @@ -11,6 +11,7 @@
4  
5  #include <stdio.h>
6  #include <stdbool.h>
7 +#include <endian.h>
8  #include <arpa/inet.h>
9  #include <netinet/ip.h>
10  #include <netinet/ip6.h>
11 @@ -26,8 +27,13 @@ uint16_t checksum(uint32_t sum, uint16_t
12                 sum += *buf++;
13                 size -= sizeof(uint16_t);
14         }
15 -       if (size)
16 -               sum += *(uint8_t *)buf;
17 +       if (size) {
18 +#if __BYTE_ORDER == __BIG_ENDIAN
19 +               sum += (uint16_t)*(uint8_t *)buf << 8;
20 +#else
21 +               sum += (uint16_t)*(uint8_t *)buf;
22 +#endif
23 +       }
24  
25         sum = (sum >> 16) + (sum & 0xffff);
26         sum += (sum >>16);
27 @@ -35,7 +41,7 @@ uint16_t checksum(uint32_t sum, uint16_t
28         return (uint16_t)(~sum);
29  }
30  
31 -uint16_t checksum_tcpudp_ipv4(struct iphdr *iph)
32 +uint16_t checksum_tcpudp_ipv4(struct iphdr *iph, uint16_t protocol_id)
33  {
34         uint32_t sum = 0;
35         uint32_t iph_len = iph->ihl*4;
36 @@ -46,13 +52,13 @@ uint16_t checksum_tcpudp_ipv4(struct iph
37         sum += (iph->saddr) & 0xFFFF;
38         sum += (iph->daddr >> 16) & 0xFFFF;
39         sum += (iph->daddr) & 0xFFFF;
40 -       sum += htons(IPPROTO_TCP);
41 +       sum += htons(protocol_id);
42         sum += htons(len);
43  
44         return checksum(sum, (uint16_t *)payload, len);
45  }
46  
47 -uint16_t checksum_tcpudp_ipv6(struct ip6_hdr *ip6h, void *transport_hdr)
48 +uint16_t checksum_tcpudp_ipv6(struct ip6_hdr *ip6h, void *transport_hdr, uint16_t protocol_id)
49  {
50         uint32_t sum = 0;
51         uint32_t hdr_len = (uint32_t *)transport_hdr - (uint32_t *)ip6h;
52 @@ -68,7 +74,7 @@ uint16_t checksum_tcpudp_ipv6(struct ip6
53                 sum += (ip6h->ip6_dst.s6_addr16[i] >> 16) & 0xFFFF;
54                 sum += (ip6h->ip6_dst.s6_addr16[i]) & 0xFFFF;
55         }
56 -       sum += htons(IPPROTO_TCP);
57 +       sum += htons(protocol_id);
58         sum += htons(ip6h->ip6_plen);
59  
60         return checksum(sum, (uint16_t *)payload, len);
61 --- a/src/extra/tcp.c
62 +++ b/src/extra/tcp.c
63 @@ -91,7 +91,7 @@ nfq_tcp_compute_checksum_ipv4(struct tcp
64  {
65         /* checksum field in header needs to be zero for calculation. */
66         tcph->check = 0;
67 -       tcph->check = checksum_tcpudp_ipv4(iph);
68 +       tcph->check = checksum_tcpudp_ipv4(iph, IPPROTO_TCP);
69  }
70  EXPORT_SYMBOL(nfq_tcp_compute_checksum_ipv4);
71  
72 @@ -105,7 +105,7 @@ nfq_tcp_compute_checksum_ipv6(struct tcp
73  {
74         /* checksum field in header needs to be zero for calculation. */
75         tcph->check = 0;
76 -       tcph->check = checksum_tcpudp_ipv6(ip6h, tcph);
77 +       tcph->check = checksum_tcpudp_ipv6(ip6h, tcph, IPPROTO_TCP);
78  }
79  EXPORT_SYMBOL(nfq_tcp_compute_checksum_ipv6);
80  
81 --- a/src/extra/udp.c
82 +++ b/src/extra/udp.c
83 @@ -91,7 +91,7 @@ nfq_udp_compute_checksum_ipv4(struct udp
84  {
85         /* checksum field in header needs to be zero for calculation. */
86         udph->check = 0;
87 -       udph->check = checksum_tcpudp_ipv4(iph);
88 +       udph->check = checksum_tcpudp_ipv4(iph, IPPROTO_UDP);
89  }
90  EXPORT_SYMBOL(nfq_udp_compute_checksum_ipv4);
91  
92 @@ -110,7 +110,7 @@ nfq_udp_compute_checksum_ipv6(struct udp
93  {
94         /* checksum field in header needs to be zero for calculation. */
95         udph->check = 0;
96 -       udph->check = checksum_tcpudp_ipv6(ip6h, udph);
97 +       udph->check = checksum_tcpudp_ipv6(ip6h, udph, IPPROTO_UDP);
98  }
99  EXPORT_SYMBOL(nfq_udp_compute_checksum_ipv6);
100  
101 --- a/src/internal.h
102 +++ b/src/internal.h
103 @@ -13,8 +13,8 @@ struct iphdr;
104  struct ip6_hdr;
105  
106  uint16_t checksum(uint32_t sum, uint16_t *buf, int size);
107 -uint16_t checksum_tcpudp_ipv4(struct iphdr *iph);
108 -uint16_t checksum_tcpudp_ipv6(struct ip6_hdr *ip6h, void *transport_hdr);
109 +uint16_t checksum_tcpudp_ipv4(struct iphdr *iph, uint16_t protocol_id);
110 +uint16_t checksum_tcpudp_ipv6(struct ip6_hdr *ip6h, void *transport_hdr, uint16_t protocol_id);
111  
112  struct pkt_buff {
113         uint8_t *mac_header;