tcp/udp: rename checksum functions for better meaning

The TCP/UDP checksum functions no longer just calculate
the checksum, they can validate as well as calculate so
use a more generic name.
pull/2624/head
Jason Ish 9 years ago committed by Victor Julien
parent f56428d996
commit b79a18ea15

@ -633,12 +633,12 @@ static int Unified2PrintStreamSegmentCallback(const Packet *p, void *data, const
if (PKT_IS_IPV6(p)) {
FakeIPv6Hdr *fakehdr = (FakeIPv6Hdr *)aun->iphdr;
fakehdr->tcph.th_sum = TCPV6CalculateChecksum(fakehdr->ip6h.s_ip6_addrs,
fakehdr->tcph.th_sum = TCPV6Checksum(fakehdr->ip6h.s_ip6_addrs,
(uint16_t *)&fakehdr->tcph, buflen + sizeof(TCPHdr), 0);
} else {
FakeIPv4Hdr *fakehdr = (FakeIPv4Hdr *)aun->iphdr;
fakehdr->tcph.th_sum = TCPCalculateChecksum(fakehdr->ip4h.s_ip_addrs,
fakehdr->tcph.th_sum = TCPChecksum(fakehdr->ip4h.s_ip_addrs,
(uint16_t *)&fakehdr->tcph, buflen + sizeof(TCPHdr), 0);
fakehdr->ip4h.ip_csum = IPV4CalculateChecksum((uint16_t *)&fakehdr->ip4h,
IPV4_GET_RAW_HLEN(&fakehdr->ip4h));

@ -236,7 +236,7 @@ static int TCPCalculateValidChecksumtest01(void)
csum = *( ((uint16_t *)raw_tcp) + 8);
FAIL_IF(TCPCalculateChecksum((uint16_t *)raw_ipshdr,
FAIL_IF(TCPChecksum((uint16_t *)raw_ipshdr,
(uint16_t *)raw_tcp, sizeof(raw_tcp), csum) != 0);
PASS;
}
@ -257,7 +257,7 @@ static int TCPCalculateInvalidChecksumtest02(void)
csum = *( ((uint16_t *)raw_tcp) + 8);
FAIL_IF(TCPCalculateChecksum((uint16_t *) raw_ipshdr,
FAIL_IF(TCPChecksum((uint16_t *) raw_ipshdr,
(uint16_t *)raw_tcp, sizeof(raw_tcp), csum) == 0);
PASS;
}
@ -281,7 +281,7 @@ static int TCPV6CalculateValidChecksumtest03(void)
csum = *( ((uint16_t *)(raw_ipv6 + 70)));
FAIL_IF(TCPV6CalculateChecksum((uint16_t *)(raw_ipv6 + 14 + 8),
FAIL_IF(TCPV6Checksum((uint16_t *)(raw_ipv6 + 14 + 8),
(uint16_t *)(raw_ipv6 + 54), 32, csum) != 0);
PASS;
}
@ -305,7 +305,7 @@ static int TCPV6CalculateInvalidChecksumtest04(void)
csum = *( ((uint16_t *)(raw_ipv6 + 70)));
FAIL_IF(TCPV6CalculateChecksum((uint16_t *)(raw_ipv6 + 14 + 8),
FAIL_IF(TCPV6Checksum((uint16_t *)(raw_ipv6 + 14 + 8),
(uint16_t *)(raw_ipv6 + 54), 32, csum) == 0);
PASS;
}

@ -164,10 +164,8 @@ typedef struct TCPVars_
void DecodeTCPRegisterTests(void);
/** -------- Inline functions ------- */
static inline uint16_t TCPCalculateChecksum(uint16_t *, uint16_t *, uint16_t,
uint16_t);
static inline uint16_t TCPV6CalculateChecksum(uint16_t *, uint16_t *, uint16_t,
uint16_t);
static inline uint16_t TCPChecksum(uint16_t *, uint16_t *, uint16_t, uint16_t);
static inline uint16_t TCPV6Checksum(uint16_t *, uint16_t *, uint16_t, uint16_t);
/**
* \brief Calculate or validate the checksum for the TCP packet
@ -181,8 +179,8 @@ static inline uint16_t TCPV6CalculateChecksum(uint16_t *, uint16_t *, uint16_t,
* \retval csum For validation 0 will be returned for success, for calculation
* this will be the checksum.
*/
static inline uint16_t TCPCalculateChecksum(uint16_t *shdr, uint16_t *pkt,
uint16_t tlen, uint16_t init)
static inline uint16_t TCPChecksum(uint16_t *shdr, uint16_t *pkt,
uint16_t tlen, uint16_t init)
{
uint16_t pad = 0;
uint32_t csum = init;
@ -246,8 +244,8 @@ static inline uint16_t TCPCalculateChecksum(uint16_t *shdr, uint16_t *pkt,
* \retval csum For validation 0 will be returned for success, for calculation
* this will be the checksum.
*/
static inline uint16_t TCPV6CalculateChecksum(uint16_t *shdr, uint16_t *pkt,
uint16_t tlen, uint16_t init)
static inline uint16_t TCPV6Checksum(uint16_t *shdr, uint16_t *pkt,
uint16_t tlen, uint16_t init)
{
uint16_t pad = 0;
uint32_t csum = init;

@ -117,7 +117,7 @@ static int UDPV4CalculateValidChecksumtest01(void)
csum = *( ((uint16_t *)raw_udp) + 3);
FAIL_IF(UDPV4CalculateChecksum((uint16_t *) raw_ipshdr,
FAIL_IF(UDPV4Checksum((uint16_t *) raw_ipshdr,
(uint16_t *)raw_udp, sizeof(raw_udp), csum) != 0);
PASS;
}
@ -144,7 +144,7 @@ static int UDPV4CalculateInvalidChecksumtest02(void)
csum = *( ((uint16_t *)raw_udp) + 3);
FAIL_IF(UDPV4CalculateChecksum((uint16_t *) raw_ipshdr,
FAIL_IF(UDPV4Checksum((uint16_t *) raw_ipshdr,
(uint16_t *)raw_udp, sizeof(raw_udp), csum) == 0);
PASS;
}
@ -167,7 +167,7 @@ static int UDPV6CalculateValidChecksumtest03(void)
csum = *( ((uint16_t *)(raw_ipv6 + 60)));
FAIL_IF(UDPV6CalculateChecksum((uint16_t *)(raw_ipv6 + 14 + 8),
FAIL_IF(UDPV6Checksum((uint16_t *)(raw_ipv6 + 14 + 8),
(uint16_t *)(raw_ipv6 + 54), 20, csum) != 0);
PASS;
}
@ -190,7 +190,7 @@ static int UDPV6CalculateInvalidChecksumtest04(void)
csum = *( ((uint16_t *)(raw_ipv6 + 60)));
FAIL_IF(UDPV6CalculateChecksum((uint16_t *)(raw_ipv6 + 14 + 8),
FAIL_IF(UDPV6Checksum((uint16_t *)(raw_ipv6 + 14 + 8),
(uint16_t *)(raw_ipv6 + 54), 20, csum) == 0);
PASS;
}

@ -54,10 +54,8 @@ typedef struct UDPHdr_
void DecodeUDPV4RegisterTests(void);
/** ------ Inline function ------ */
static inline uint16_t UDPV4CalculateChecksum(uint16_t *, uint16_t *, uint16_t,
uint16_t);
static inline uint16_t UDPV6CalculateChecksum(uint16_t *, uint16_t *, uint16_t,
uint16_t);
static inline uint16_t UDPV4Checksum(uint16_t *, uint16_t *, uint16_t, uint16_t);
static inline uint16_t UDPV6Checksum(uint16_t *, uint16_t *, uint16_t, uint16_t);
/**
* \brief Calculate or valid the checksum for the UDP packet
@ -72,8 +70,8 @@ static inline uint16_t UDPV6CalculateChecksum(uint16_t *, uint16_t *, uint16_t,
* \retval csum For validation 0 will be returned for success, for calculation
* this will be the checksum.
*/
static inline uint16_t UDPV4CalculateChecksum(uint16_t *shdr, uint16_t *pkt,
uint16_t tlen, uint16_t init)
static inline uint16_t UDPV4Checksum(uint16_t *shdr, uint16_t *pkt,
uint16_t tlen, uint16_t init)
{
uint16_t pad = 0;
uint32_t csum = init;
@ -139,8 +137,8 @@ static inline uint16_t UDPV4CalculateChecksum(uint16_t *shdr, uint16_t *pkt,
* \retval csum For validation 0 will be returned for success, for calculation
* this will be the checksum.
*/
static inline uint16_t UDPV6CalculateChecksum(uint16_t *shdr, uint16_t *pkt,
uint16_t tlen, uint16_t init)
static inline uint16_t UDPV6Checksum(uint16_t *shdr, uint16_t *pkt,
uint16_t tlen, uint16_t init)
{
uint16_t pad = 0;
uint32_t csum = init;

@ -336,11 +336,11 @@ static int DetectTCPV4CsumMatch(ThreadVars *t, DetectEngineThreadCtx *det_ctx,
}
if (p->level4_comp_csum == -1)
p->level4_comp_csum = TCPCalculateChecksum(p->ip4h->s_ip_addrs,
(uint16_t *)p->tcph,
(p->payload_len +
TCP_GET_HLEN(p)),
p->tcph->th_sum);
p->level4_comp_csum = TCPChecksum(p->ip4h->s_ip_addrs,
(uint16_t *)p->tcph,
(p->payload_len +
TCP_GET_HLEN(p)),
p->tcph->th_sum);
if (p->level4_comp_csum == 0 && cd->valid == 1)
return 1;
@ -433,11 +433,11 @@ static int DetectTCPV6CsumMatch(ThreadVars *t, DetectEngineThreadCtx *det_ctx,
}
if (p->level4_comp_csum == -1)
p->level4_comp_csum = TCPV6CalculateChecksum(p->ip6h->s_ip6_addrs,
(uint16_t *)p->tcph,
(p->payload_len +
TCP_GET_HLEN(p)),
p->tcph->th_sum);
p->level4_comp_csum = TCPV6Checksum(p->ip6h->s_ip6_addrs,
(uint16_t *)p->tcph,
(p->payload_len +
TCP_GET_HLEN(p)),
p->tcph->th_sum);
if (p->level4_comp_csum == 0 && cd->valid == 1)
return 1;
@ -530,11 +530,11 @@ static int DetectUDPV4CsumMatch(ThreadVars *t, DetectEngineThreadCtx *det_ctx,
}
if (p->level4_comp_csum == -1)
p->level4_comp_csum = UDPV4CalculateChecksum(p->ip4h->s_ip_addrs,
(uint16_t *)p->udph,
(p->payload_len +
UDP_HEADER_LEN),
p->udph->uh_sum);
p->level4_comp_csum = UDPV4Checksum(p->ip4h->s_ip_addrs,
(uint16_t *)p->udph,
(p->payload_len +
UDP_HEADER_LEN),
p->udph->uh_sum);
if (p->level4_comp_csum == 0 && cd->valid == 1)
return 1;
@ -627,11 +627,11 @@ static int DetectUDPV6CsumMatch(ThreadVars *t, DetectEngineThreadCtx *det_ctx,
}
if (p->level4_comp_csum == -1)
p->level4_comp_csum = UDPV6CalculateChecksum(p->ip6h->s_ip6_addrs,
(uint16_t *)p->udph,
(p->payload_len +
UDP_HEADER_LEN),
p->udph->uh_sum);
p->level4_comp_csum = UDPV6Checksum(p->ip6h->s_ip6_addrs,
(uint16_t *)p->udph,
(p->payload_len +
UDP_HEADER_LEN),
p->udph->uh_sum);
if (p->level4_comp_csum == 0 && cd->valid == 1)
return 1;

@ -239,14 +239,14 @@ static inline Packet *FlowForceReassemblyPseudoPacketSetup(Packet *p,
}
if (FLOW_IS_IPV4(f)) {
p->tcph->th_sum = TCPCalculateChecksum(p->ip4h->s_ip_addrs,
p->tcph->th_sum = TCPChecksum(p->ip4h->s_ip_addrs,
(uint16_t *)p->tcph, 20, 0);
/* calc ipv4 csum as we may log it and barnyard might reject
* a wrong checksum */
p->ip4h->ip_csum = IPV4CalculateChecksum((uint16_t *)p->ip4h,
IPV4_GET_RAW_HLEN(p->ip4h));
} else if (FLOW_IS_IPV6(f)) {
p->tcph->th_sum = TCPCalculateChecksum(p->ip6h->s_ip6_addrs,
p->tcph->th_sum = TCPChecksum(p->ip6h->s_ip6_addrs,
(uint16_t *)p->tcph, 20, 0);
}

@ -4707,17 +4707,17 @@ static inline int StreamTcpValidateChecksum(Packet *p)
if (p->level4_comp_csum == -1) {
if (PKT_IS_IPV4(p)) {
p->level4_comp_csum = TCPCalculateChecksum(p->ip4h->s_ip_addrs,
(uint16_t *)p->tcph,
(p->payload_len +
TCP_GET_HLEN(p)),
p->tcph->th_sum);
p->level4_comp_csum = TCPChecksum(p->ip4h->s_ip_addrs,
(uint16_t *)p->tcph,
(p->payload_len +
TCP_GET_HLEN(p)),
p->tcph->th_sum);
} else if (PKT_IS_IPV6(p)) {
p->level4_comp_csum = TCPV6CalculateChecksum(p->ip6h->s_ip6_addrs,
(uint16_t *)p->tcph,
(p->payload_len +
TCP_GET_HLEN(p)),
p->tcph->th_sum);
p->level4_comp_csum = TCPV6Checksum(p->ip6h->s_ip6_addrs,
(uint16_t *)p->tcph,
(p->payload_len +
TCP_GET_HLEN(p)),
p->tcph->th_sum);
}
}

@ -33,11 +33,11 @@ int ReCalculateChecksum(Packet *p)
if (PKT_IS_TCP(p)) {
/* TCP */
p->tcph->th_sum = 0;
p->tcph->th_sum = TCPCalculateChecksum(p->ip4h->s_ip_addrs,
p->tcph->th_sum = TCPChecksum(p->ip4h->s_ip_addrs,
(uint16_t *)p->tcph, (p->payload_len + TCP_GET_HLEN(p)), 0);
} else if (PKT_IS_UDP(p)) {
p->udph->uh_sum = 0;
p->udph->uh_sum = UDPV4CalculateChecksum(p->ip4h->s_ip_addrs,
p->udph->uh_sum = UDPV4Checksum(p->ip4h->s_ip_addrs,
(uint16_t *)p->udph, (p->payload_len + UDP_HEADER_LEN), 0);
}
/* IPV4 */
@ -48,11 +48,11 @@ int ReCalculateChecksum(Packet *p)
/* just TCP for IPV6 */
if (PKT_IS_TCP(p)) {
p->tcph->th_sum = 0;
p->tcph->th_sum = TCPV6CalculateChecksum(p->ip6h->s_ip6_addrs,
p->tcph->th_sum = TCPV6Checksum(p->ip6h->s_ip6_addrs,
(uint16_t *)p->tcph, (p->payload_len + TCP_GET_HLEN(p)), 0);
} else if (PKT_IS_UDP(p)) {
p->udph->uh_sum = 0;
p->udph->uh_sum = UDPV6CalculateChecksum(p->ip6h->s_ip6_addrs,
p->udph->uh_sum = UDPV6Checksum(p->ip6h->s_ip6_addrs,
(uint16_t *)p->udph, (p->payload_len + UDP_HEADER_LEN), 0);
}
}

Loading…
Cancel
Save