Use the ipchecksum function in the ipv6 stack.
Change-Id: I88aae124757232d4d51112a6e1a85f5b67b125a6
diff --git a/src/net/ipv6/inet6.c b/src/net/ipv6/inet6.c
index 3c931ba..069cb63 100644
--- a/src/net/ipv6/inet6.c
+++ b/src/net/ipv6/inet6.c
@@ -8,6 +8,7 @@
#include <string.h>
#include "base/xalloc.h"
+#include "base/ipchecksum.h"
#include "net/ipv6/inet6.h"
#include "net/ipv6/ipv6.h"
@@ -166,22 +167,6 @@
return -1;
}
-static uint16_t checksum(const void *_data, size_t len, uint16_t _sum)
-{
- uint32_t sum = _sum;
- const uint16_t *data = _data;
-
- while (len > 1) {
- sum += *data++;
- len -= 2;
- }
- if (len)
- sum += (*data & 0xFF);
- while (sum > 0xFFFF)
- sum = (sum & 0xFFFF) + (sum >> 16);
- return sum;
-}
-
typedef struct __attribute__((packed)) {
uint32_t ver_tc_flow;
uint16_t length;
@@ -213,15 +198,10 @@
static unsigned ipv6_checksum(Ipv6Hdr *ip, unsigned type, size_t length)
{
- uint16_t sum;
-
// Length and protocol field for pseudo-header.
- sum = checksum(&ip->length, 2, htonw(type));
+ uint16_t sum = ipchecksum(&ip->length, 2, ~htonw(type));
// src/dst for pseudo-header + payload.
- sum = checksum(ip->src, 32 + length, sum);
-
- // 0 is illegal, so 0xffff remains 0xffff
- return sum == 0xffff ? sum : ~sum;
+ return ipchecksum(ip->src, 32 + length, sum);
}
static int ipv6_setup(Ipv6Pkt *p, NetDevice *dev, const Ipv6Address *daddr,
@@ -350,7 +330,6 @@
static void _udpv6_recv(Ipv6Hdr *ip, void *_data, size_t len)
{
UdpHdr *udp = _data;
- uint16_t sum, n;
if (len < Udp_HdrLen)
printf("error: Bogus Header Len\n");
@@ -359,12 +338,12 @@
if (udp->checksum == 0xFFFF)
udp->checksum = 0;
- sum = checksum(&ip->length, 2, htonw(HdrUdp));
- sum = checksum(ip->src, 32 + len, sum);
- if (sum != 0xFFFF)
+ uint16_t sum = ipchecksum(&ip->length, 2, ~htonw(HdrUdp));
+ sum = ipchecksum(ip->src, 32 + len, sum);
+ if (sum != 0)
printf("error: Checksum Incorrect\n");
- n = ntohw(udp->length);
+ uint16_t n = ntohw(udp->length);
if (n < Udp_HdrLen)
printf("error: Bogus Header Len\n");
if (n > len)
@@ -401,9 +380,9 @@
if (icmp->checksum == 0xFFFF)
icmp->checksum = 0;
- uint16_t sum = checksum(&ip->length, 2, htonw(HdrIcmpv6));
- sum = checksum(ip->src, 32 + len, sum);
- if (sum != 0xFFFF)
+ uint16_t sum = ipchecksum(&ip->length, 2, ~htonw(HdrIcmpv6));
+ sum = ipchecksum(ip->src, 32 + len, sum);
+ if (sum != 0)
printf("error: Checksum Incorrect\n");
if (icmp->type == Icmpv6_NdpNSolicit) {