Skip to content

Commit d18bb23

Browse files
committed
Use nextproto6_cksum() for XXX-over-IPv6 checksums.
Get rid of duplicated checksums with IPv6 pseudo-headers.
1 parent 7074f77 commit d18bb23

File tree

7 files changed

+12
-87
lines changed

7 files changed

+12
-87
lines changed

ip6.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,6 @@ struct ip6_frag {
187187
#define IP6F_MORE_FRAG 0x0001 /* more-fragments flag */
188188

189189
/* in print-ip6.c */
190-
extern int nextproto6_cksum(const struct ip6_hdr *, const u_short *, u_int, u_int);
190+
extern int nextproto6_cksum(const struct ip6_hdr *, const u_int8_t *, u_int, u_int);
191191

192192
#endif /* not _NETINET_IP6_H_ */

netdissect.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ extern u_int ieee802_15_4_if_print(netdissect_options *,const struct pcap_pkthdr
461461
extern void ip6_print(netdissect_options *,const u_char *, u_int);
462462
#if 0
463463
extern void ip6_opt_print(netdissect_options *,const u_char *, int);
464-
extern int nextproto6_cksum(const struct ip6_hdr *, const u_short *, u_int, u_int);
464+
extern int nextproto6_cksum(const struct ip6_hdr *, const u_int8_t *, u_int, u_int);
465465
extern int hbhopt_print(netdissect_options *,const u_char *);
466466
extern int dstopt_print(netdissect_options *,const u_char *);
467467
extern int frag6_print(netdissect_options *,const u_char *,

print-dccp.c

+2-22
Original file line numberDiff line numberDiff line change
@@ -103,28 +103,8 @@ static int dccp_cksum(const struct ip *ip,
103103
#ifdef INET6
104104
static int dccp6_cksum(const struct ip6_hdr *ip6, const struct dccp_hdr *dh, u_int len)
105105
{
106-
int cov = dccp_csum_coverage(dh, len);
107-
struct {
108-
struct in6_addr ph_src;
109-
struct in6_addr ph_dst;
110-
u_int32_t ph_len;
111-
u_int8_t ph_zero[3];
112-
u_int8_t ph_nxt;
113-
} ph;
114-
struct cksum_vec vec[2];
115-
116-
/* pseudo-header */
117-
memset(&ph, 0, sizeof(ph));
118-
ph.ph_src = ip6->ip6_src;
119-
ph.ph_dst = ip6->ip6_dst;
120-
ph.ph_len = htonl(len);
121-
ph.ph_nxt = IPPROTO_DCCP;
122-
123-
vec[0].ptr = (const u_int8_t *)(void *)&ph;
124-
vec[0].len = sizeof(ph);
125-
vec[1].ptr = (const u_int8_t *)(void *)dh;
126-
vec[1].len = cov;
127-
return in_cksum(vec, 2);
106+
return nextproto6_cksum(ip6, (const u_int8_t *)(void *)dh,
107+
dccp_csum_coverage(dh, len), IPPROTO_DCCP);
128108
}
129109
#endif
130110

print-icmp6.c

+2-38
Original file line numberDiff line numberDiff line change
@@ -197,44 +197,8 @@ print_lladdr(const u_int8_t *p, size_t l)
197197
static int icmp6_cksum(const struct ip6_hdr *ip6, const struct icmp6_hdr *icp,
198198
u_int len)
199199
{
200-
size_t i;
201-
register const u_int16_t *sp;
202-
u_int32_t sum;
203-
union {
204-
struct {
205-
struct in6_addr ph_src;
206-
struct in6_addr ph_dst;
207-
u_int32_t ph_len;
208-
u_int8_t ph_zero[3];
209-
u_int8_t ph_nxt;
210-
} ph;
211-
u_int16_t pa[20];
212-
} phu;
213-
214-
/* pseudo-header */
215-
memset(&phu, 0, sizeof(phu));
216-
phu.ph.ph_src = ip6->ip6_src;
217-
phu.ph.ph_dst = ip6->ip6_dst;
218-
phu.ph.ph_len = htonl(len);
219-
phu.ph.ph_nxt = IPPROTO_ICMPV6;
220-
221-
sum = 0;
222-
for (i = 0; i < sizeof(phu.pa) / sizeof(phu.pa[0]); i++)
223-
sum += phu.pa[i];
224-
225-
sp = (const u_int16_t *)icp;
226-
227-
for (i = 0; i < (len & ~1); i += 2)
228-
sum += *sp++;
229-
230-
if (len & 1)
231-
sum += htons((*(const u_int8_t *)sp) << 8);
232-
233-
while (sum > 0xffff)
234-
sum = (sum & 0xffff) + (sum >> 16);
235-
sum = ~sum & 0xffff;
236-
237-
return (sum);
200+
return (nextproto6_cksum(ip6, (const u_int8_t *)(void *)icp, len,
201+
IPPROTO_ICMPV6));
238202
}
239203

240204
enum ND_RPL_CODE {

print-ip6.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ static const char rcsid[] _U_ =
4848
* Compute a V6-style checksum by building a pseudoheader.
4949
*/
5050
int
51-
nextproto6_cksum(const struct ip6_hdr *ip6, const u_short *data,
51+
nextproto6_cksum(const struct ip6_hdr *ip6, const u_int8_t *data,
5252
u_int len, u_int next_proto)
5353
{
5454
struct {
@@ -69,7 +69,7 @@ nextproto6_cksum(const struct ip6_hdr *ip6, const u_short *data,
6969

7070
vec[0].ptr = (const u_int8_t *)(void *)&ph;
7171
vec[0].len = sizeof(ph);
72-
vec[1].ptr = (const u_int8_t *)(void *)data;
72+
vec[1].ptr = data;
7373
vec[1].len = len;
7474

7575
return in_cksum(vec, 2);
@@ -225,7 +225,7 @@ ip6_print(netdissect_options *ndo, const u_char *bp, u_int length)
225225
}
226226

227227
case IPPROTO_PIM:
228-
pim_print(cp, len, nextproto6_cksum(ip6, (u_short *)cp, len,
228+
pim_print(cp, len, nextproto6_cksum(ip6, cp, len,
229229
IPPROTO_PIM));
230230
return;
231231

print-tcp.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ tcp_print(register const u_char *bp, register u_int length,
432432
#ifdef INET6
433433
else if (IP_V(ip) == 6 && ip6->ip6_plen) {
434434
if (TTEST2(tp->th_sport, length)) {
435-
sum = nextproto6_cksum(ip6, (u_short *)tp, length, IPPROTO_TCP);
435+
sum = nextproto6_cksum(ip6, (const u_int8_t *)tp, length, IPPROTO_TCP);
436436
tcp_sum = EXTRACT_16BITS(&tp->th_sum);
437437

438438
(void)printf(", cksum 0x%04x", tcp_sum);

print-udp.c

+2-21
Original file line numberDiff line numberDiff line change
@@ -316,27 +316,8 @@ static int udp_cksum(register const struct ip *ip,
316316
static int udp6_cksum(const struct ip6_hdr *ip6, const struct udphdr *up,
317317
u_int len)
318318
{
319-
struct {
320-
struct in6_addr ph_src;
321-
struct in6_addr ph_dst;
322-
u_int32_t ph_len;
323-
u_int8_t ph_zero[3];
324-
u_int8_t ph_nxt;
325-
} ph;
326-
struct cksum_vec vec[2];
327-
328-
/* pseudo-header */
329-
memset(&ph, 0, sizeof(ph));
330-
ph.ph_src = ip6->ip6_src;
331-
ph.ph_dst = ip6->ip6_dst;
332-
ph.ph_len = htonl(len);
333-
ph.ph_nxt = IPPROTO_UDP;
334-
335-
vec[0].ptr = (const u_int8_t *)(void *)&ph;
336-
vec[0].len = sizeof(ph);
337-
vec[1].ptr = (const u_int8_t *)(void *)up;
338-
vec[1].len = len;
339-
return (in_cksum(vec, 2));
319+
return (nextproto6_cksum(ip6, (const u_int8_t *)(void *)up, len,
320+
IPPROTO_UDP));
340321
}
341322
#endif
342323

0 commit comments

Comments
 (0)