implement tcp6 pseudo header cksum.

This commit is contained in:
itojun 2000-08-01 17:24:40 +00:00
parent a8cd48f9e6
commit 1174b9a1a9
1 changed files with 62 additions and 2 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: print-tcp.c,v 1.16 1999/12/10 05:45:08 itojun Exp $ */
/* $NetBSD: print-tcp.c,v 1.17 2000/08/01 17:24:40 itojun Exp $ */
/*
* Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
@ -27,7 +27,7 @@
static const char rcsid[] =
"@(#) Header: print-tcp.c,v 1.55 97/06/15 13:20:28 leres Exp (LBL)";
#else
__RCSID("$NetBSD: print-tcp.c,v 1.16 1999/12/10 05:45:08 itojun Exp $");
__RCSID("$NetBSD: print-tcp.c,v 1.17 2000/08/01 17:24:40 itojun Exp $");
#endif
#endif
@ -159,6 +159,54 @@ static int tcp_cksum(register const struct ip *ip,
return (sum);
}
#ifdef INET6
static int tcp6_cksum(const struct ip6_hdr *ip6, const struct tcphdr *tp,
int len)
{
int i, tlen;
register const u_int16_t *sp;
u_int32_t sum;
union {
struct {
struct in6_addr ph_src;
struct in6_addr ph_dst;
u_int32_t ph_len;
u_int8_t ph_zero[3];
u_int8_t ph_nxt;
} ph;
u_int16_t pa[20];
} phu;
tlen = ntohs(ip6->ip6_plen) + sizeof(struct ip6_hdr) -
((const char *)tp - (const char*)ip6);
/* pseudo-header */
memset(&phu, 0, sizeof(phu));
phu.ph.ph_src = ip6->ip6_src;
phu.ph.ph_dst = ip6->ip6_dst;
phu.ph.ph_len = htonl(tlen);
phu.ph.ph_nxt = IPPROTO_TCP;
sum = 0;
for (i = 0; i < sizeof(phu.pa) / sizeof(phu.pa[0]); i++)
sum += phu.pa[i];
sp = (const u_int16_t *)tp;
for (i = 0; i < (tlen & ~1); i += 2)
sum += *sp++;
if (tlen & 1)
sum += htons((*(const char *)sp) << 8);
while (sum > 0xffff)
sum = (sum & 0xffff) + (sum >> 16);
sum = ~sum & 0xffff;
return (sum);
}
#endif
void
tcp_print(register const u_char *bp, register u_int length,
@ -379,6 +427,18 @@ tcp_print(register const u_char *bp, register u_int length,
(void)printf(" [tcp sum ok]");
}
}
#ifdef INET6
if (ip->ip_v == 6 && ip6->ip6_plen && vflag) {
int sum;
if (TTEST2(tp->th_sport, length)) {
sum = tcp6_cksum(ip6, tp, length);
if (sum != 0)
(void)printf(" [bad tcp cksum %x!]", sum);
else
(void)printf(" [tcp sum ok]");
}
}
#endif
length -= hlen;
if (length > 0 || flags & (TH_SYN | TH_FIN | TH_RST))