Skip to content

Commit 57bfcb4

Browse files
committed
Add a routine to do the "checksum with pseudo-header" stuff for IPv4.
Clean up some other stuff while we're at it.
1 parent d18bb23 commit 57bfcb4

File tree

6 files changed

+43
-76
lines changed

6 files changed

+43
-76
lines changed

ip.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,4 @@ struct ip_timestamp {
161161
#define IP_MSS 576 /* default maximum segment size */
162162

163163
/* in print-ip.c */
164-
extern u_int32_t ip_finddst(const struct ip *);
164+
extern int nextproto4_cksum(const struct ip *, const u_int8_t *, u_int, u_int);

netdissect.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ extern void igmp_print(netdissect_options *,
352352
register const u_char *, u_int);
353353
extern void igrp_print(netdissect_options *,const u_char *, u_int,
354354
const u_char *);
355+
extern int nextproto4_cksum(const struct ip *, const u_int8_t *, u_int, u_int);
355356
extern void ipN_print(netdissect_options *,const u_char *, u_int);
356357
extern void ipx_print(netdissect_options *,const u_char *, u_int);
357358
extern void isoclns_print(netdissect_options *,const u_char *,

print-dccp.c

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ static const char *dccp_feature_nums[] = {
6060
"check data checksum",
6161
};
6262

63-
static inline int dccp_csum_coverage(const struct dccp_hdr* dh, u_int len)
63+
static inline u_int dccp_csum_coverage(const struct dccp_hdr* dh, u_int len)
6464
{
6565
u_int cov;
6666

@@ -73,31 +73,8 @@ static inline int dccp_csum_coverage(const struct dccp_hdr* dh, u_int len)
7373
static int dccp_cksum(const struct ip *ip,
7474
const struct dccp_hdr *dh, u_int len)
7575
{
76-
int cov = dccp_csum_coverage(dh, len);
77-
struct phdr {
78-
u_int32_t src;
79-
u_int32_t dst;
80-
u_char mbz;
81-
u_char proto;
82-
u_int16_t len;
83-
} ph;
84-
struct cksum_vec vec[2];
85-
86-
/* pseudo-header.. */
87-
ph.mbz = 0;
88-
ph.len = htons(len);
89-
ph.proto = IPPROTO_DCCP;
90-
memcpy(&ph.src, &ip->ip_src.s_addr, sizeof(u_int32_t));
91-
if (IP_HL(ip) == 5)
92-
memcpy(&ph.dst, &ip->ip_dst.s_addr, sizeof(u_int32_t));
93-
else
94-
ph.dst = ip_finddst(ip);
95-
96-
vec[0].ptr = (const u_int8_t *)(void *)&ph;
97-
vec[0].len = sizeof(ph);
98-
vec[1].ptr = (const u_int8_t *)(void *)dh;
99-
vec[1].len = cov;
100-
return in_cksum(vec, 2);
76+
return nextproto4_cksum(ip, (const u_int8_t *)(void *)dh,
77+
dccp_csum_coverage(dh, len), IPPROTO_DCCP);
10178
}
10279

10380
#ifdef INET6

print-ip.c

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ ip_printroute(register const u_char *cp, u_int length)
8787
* This is used for UDP and TCP pseudo-header in the checksum
8888
* calculation.
8989
*/
90-
u_int32_t
90+
static u_int32_t
9191
ip_finddst(const struct ip *ip)
9292
{
9393
int length;
@@ -129,6 +129,39 @@ ip_finddst(const struct ip *ip)
129129
return retval;
130130
}
131131

132+
/*
133+
* Compute a V4-style checksum by building a pseudoheader.
134+
*/
135+
int
136+
nextproto4_cksum(const struct ip *ip, const u_int8_t *data,
137+
u_int len, u_int next_proto)
138+
{
139+
struct phdr {
140+
u_int32_t src;
141+
u_int32_t dst;
142+
u_char mbz;
143+
u_char proto;
144+
u_int16_t len;
145+
} ph;
146+
struct cksum_vec vec[2];
147+
148+
/* pseudo-header.. */
149+
ph.len = htons((u_int16_t)len);
150+
ph.mbz = 0;
151+
ph.proto = next_proto;
152+
memcpy(&ph.src, &ip->ip_src.s_addr, sizeof(u_int32_t));
153+
if (IP_HL(ip) == 5)
154+
memcpy(&ph.dst, &ip->ip_dst.s_addr, sizeof(u_int32_t));
155+
else
156+
ph.dst = ip_finddst(ip);
157+
158+
vec[0].ptr = (const u_int8_t *)(void *)&ph;
159+
vec[0].len = sizeof(ph);
160+
vec[1].ptr = data;
161+
vec[1].len = len;
162+
return (in_cksum(vec, 2));
163+
}
164+
132165
static void
133166
ip_printts(register const u_char *cp, u_int length)
134167
{

print-tcp.c

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -129,30 +129,8 @@ static int tcp_cksum(register const struct ip *ip,
129129
register const struct tcphdr *tp,
130130
register u_int len)
131131
{
132-
struct phdr {
133-
u_int32_t src;
134-
u_int32_t dst;
135-
u_char mbz;
136-
u_char proto;
137-
u_int16_t len;
138-
} ph;
139-
struct cksum_vec vec[2];
140-
141-
/* pseudo-header.. */
142-
ph.len = htons((u_int16_t)len);
143-
ph.mbz = 0;
144-
ph.proto = IPPROTO_TCP;
145-
memcpy(&ph.src, &ip->ip_src.s_addr, sizeof(u_int32_t));
146-
if (IP_HL(ip) == 5)
147-
memcpy(&ph.dst, &ip->ip_dst.s_addr, sizeof(u_int32_t));
148-
else
149-
ph.dst = ip_finddst(ip);
150-
151-
vec[0].ptr = (const u_int8_t *)(void *)&ph;
152-
vec[0].len = sizeof(ph);
153-
vec[1].ptr = (const u_int8_t *)tp;
154-
vec[1].len = len;
155-
return in_cksum(vec, 2);
132+
return (nextproto4_cksum(ip, (const u_int8_t *)tp, len,
133+
IPPROTO_TCP));
156134
}
157135

158136
void

print-udp.c

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -286,30 +286,8 @@ static int udp_cksum(register const struct ip *ip,
286286
register const struct udphdr *up,
287287
register u_int len)
288288
{
289-
struct phdr {
290-
u_int32_t src;
291-
u_int32_t dst;
292-
u_char mbz;
293-
u_char proto;
294-
u_int16_t len;
295-
} ph;
296-
struct cksum_vec vec[2];
297-
298-
/* pseudo-header.. */
299-
ph.len = htons((u_int16_t)len);
300-
ph.mbz = 0;
301-
ph.proto = IPPROTO_UDP;
302-
memcpy(&ph.src, &ip->ip_src.s_addr, sizeof(u_int32_t));
303-
if (IP_HL(ip) == 5)
304-
memcpy(&ph.dst, &ip->ip_dst.s_addr, sizeof(u_int32_t));
305-
else
306-
ph.dst = ip_finddst(ip);
307-
308-
vec[0].ptr = (const u_int8_t *)(void *)&ph;
309-
vec[0].len = sizeof(ph);
310-
vec[1].ptr = (const u_int8_t *)(void *)up;
311-
vec[1].len = len;
312-
return (in_cksum(vec, 2));
289+
return (nextproto4_cksum(ip, (const u_int8_t *)(void *)up, len,
290+
IPPROTO_UDP));
313291
}
314292

315293
#ifdef INET6

0 commit comments

Comments
 (0)