Deleted Added
full compact
print-tcp.c (172684) print-tcp.c (190207)
1/* $NetBSD: print-tcp.c,v 1.9 2007/07/26 18:15:12 plunky Exp $ */
2
1/*
2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Copyright (c) 1999-2004 The tcpdump.org project
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that: (1) source code distributions

--- 9 unchanged lines hidden (view full) ---

18 * written permission.
19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
20 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22 */
23
24#ifndef lint
25static const char rcsid[] _U_ =
3/*
4 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Copyright (c) 1999-2004 The tcpdump.org project
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that: (1) source code distributions

--- 9 unchanged lines hidden (view full) ---

20 * written permission.
21 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
22 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
24 */
25
26#ifndef lint
27static const char rcsid[] _U_ =
26 "@(#) $Header: /tcpdump/master/tcpdump/print-tcp.c,v 1.120.2.3 2005/10/16 06:05:46 guy Exp $ (LBL)";
28"@(#) $Header: /tcpdump/master/tcpdump/print-tcp.c,v 1.130.2.3 2007-12-22 03:08:45 guy Exp $ (LBL)";
29 #else
30__RCSID("$NetBSD: print-tcp.c,v 1.8 2007/07/24 11:53:48 drochner Exp $");
27#endif
28
29#ifdef HAVE_CONFIG_H
30#include "config.h"
31#endif
32
33#include <tcpdump-stdinc.h>
34

--- 20 unchanged lines hidden (view full) ---

55#ifdef HAVE_LIBCRYPTO
56#include <openssl/md5.h>
57
58#define SIGNATURE_VALID 0
59#define SIGNATURE_INVALID 1
60#define CANT_CHECK_SIGNATURE 2
61
62static int tcp_verify_signature(const struct ip *ip, const struct tcphdr *tp,
31#endif
32
33#ifdef HAVE_CONFIG_H
34#include "config.h"
35#endif
36
37#include <tcpdump-stdinc.h>
38

--- 20 unchanged lines hidden (view full) ---

59#ifdef HAVE_LIBCRYPTO
60#include <openssl/md5.h>
61
62#define SIGNATURE_VALID 0
63#define SIGNATURE_INVALID 1
64#define CANT_CHECK_SIGNATURE 2
65
66static int tcp_verify_signature(const struct ip *ip, const struct tcphdr *tp,
63 const u_char *data, int length, const u_char *rcvsig);
67 const u_char *data, int length, const u_char *rcvsig);
64#endif
65
66static void print_tcp_rst_data(register const u_char *sp, u_int length);
67
68#define MAX_RST_DATA_LEN 30
69
70
71struct tha {
72#ifndef INET6
68#endif
69
70static void print_tcp_rst_data(register const u_char *sp, u_int length);
71
72#define MAX_RST_DATA_LEN 30
73
74
75struct tha {
76#ifndef INET6
73 struct in_addr src;
74 struct in_addr dst;
77 struct in_addr src;
78 struct in_addr dst;
75#else
79#else
76 struct in6_addr src;
77 struct in6_addr dst;
80 struct in6_addr src;
81 struct in6_addr dst;
78#endif /*INET6*/
82#endif /*INET6*/
79 u_int port;
83 u_int port;
80};
81
82struct tcp_seq_hash {
84};
85
86struct tcp_seq_hash {
83 struct tcp_seq_hash *nxt;
84 struct tha addr;
85 tcp_seq seq;
86 tcp_seq ack;
87 struct tcp_seq_hash *nxt;
88 struct tha addr;
89 tcp_seq seq;
90 tcp_seq ack;
87};
88
89#define TSEQ_HASHSIZE 919
90
91/* These tcp optinos do not have the size octet */
92#define ZEROLENOPT(o) ((o) == TCPOPT_EOL || (o) == TCPOPT_NOP)
93
94static struct tcp_seq_hash tcp_seq_hash[TSEQ_HASHSIZE];
95
91};
92
93#define TSEQ_HASHSIZE 919
94
95/* These tcp optinos do not have the size octet */
96#define ZEROLENOPT(o) ((o) == TCPOPT_EOL || (o) == TCPOPT_NOP)
97
98static struct tcp_seq_hash tcp_seq_hash[TSEQ_HASHSIZE];
99
100struct tok tcp_flag_values[] = {
101 { TH_FIN, "F" },
102 { TH_SYN, "S" },
103 { TH_RST, "R" },
104 { TH_PUSH, "P" },
105 { TH_ACK, "." },
106 { TH_URG, "U" },
107 { TH_ECNECHO, "E" },
108 { TH_CWR, "W" },
109 { 0, NULL }
110};
96
111
97#ifndef TELNET_PORT
98#define TELNET_PORT 23
99#endif
100#ifndef BGP_PORT
101#define BGP_PORT 179
102#endif
103#define NETBIOS_SSN_PORT 139
104#ifndef PPTP_PORT
105#define PPTP_PORT 1723
106#endif
107#define BEEP_PORT 10288
108#ifndef NFS_PORT
109#define NFS_PORT 2049
110#endif
111#define MSDP_PORT 639
112#define LDP_PORT 646
112struct tok tcp_option_values[] = {
113 { TCPOPT_EOL, "eol" },
114 { TCPOPT_NOP, "nop" },
115 { TCPOPT_MAXSEG, "mss" },
116 { TCPOPT_WSCALE, "wscale" },
117 { TCPOPT_SACKOK, "sackOK" },
118 { TCPOPT_SACK, "sack" },
119 { TCPOPT_ECHO, "echo" },
120 { TCPOPT_ECHOREPLY, "echoreply" },
121 { TCPOPT_TIMESTAMP, "TS" },
122 { TCPOPT_CC, "cc" },
123 { TCPOPT_CCNEW, "ccnew" },
124 { TCPOPT_CCECHO, "" },
125 { TCPOPT_SIGNATURE, "md5" },
126 { TCPOPT_AUTH, "enhanced auth" },
127 { 0, NULL }
128};
113
114static int tcp_cksum(register const struct ip *ip,
115 register const struct tcphdr *tp,
116 register u_int len)
117{
129
130static int tcp_cksum(register const struct ip *ip,
131 register const struct tcphdr *tp,
132 register u_int len)
133{
118 union phu {
119 struct phdr {
120 u_int32_t src;
121 u_int32_t dst;
122 u_char mbz;
123 u_char proto;
124 u_int16_t len;
125 } ph;
126 u_int16_t pa[6];
127 } phu;
128 const u_int16_t *sp;
134 union phu {
135 struct phdr {
136 u_int32_t src;
137 u_int32_t dst;
138 u_char mbz;
139 u_char proto;
140 u_int16_t len;
141 } ph;
142 u_int16_t pa[6];
143 } phu;
144 const u_int16_t *sp;
129
145
130 /* pseudo-header.. */
131 phu.ph.len = htons((u_int16_t)len);
132 phu.ph.mbz = 0;
133 phu.ph.proto = IPPROTO_TCP;
134 memcpy(&phu.ph.src, &ip->ip_src.s_addr, sizeof(u_int32_t));
135 if (IP_HL(ip) == 5)
136 memcpy(&phu.ph.dst, &ip->ip_dst.s_addr, sizeof(u_int32_t));
137 else
138 phu.ph.dst = ip_finddst(ip);
146 /* pseudo-header.. */
147 phu.ph.len = htons((u_int16_t)len);
148 phu.ph.mbz = 0;
149 phu.ph.proto = IPPROTO_TCP;
150 memcpy(&phu.ph.src, &ip->ip_src.s_addr, sizeof(u_int32_t));
151 if (IP_HL(ip) == 5)
152 memcpy(&phu.ph.dst, &ip->ip_dst.s_addr, sizeof(u_int32_t));
153 else
154 phu.ph.dst = ip_finddst(ip);
139
155
140 sp = &phu.pa[0];
141 return in_cksum((u_short *)tp, len,
142 sp[0]+sp[1]+sp[2]+sp[3]+sp[4]+sp[5]);
156 sp = &phu.pa[0];
157 return in_cksum((u_short *)tp, len,
158 sp[0]+sp[1]+sp[2]+sp[3]+sp[4]+sp[5]);
143}
144
145#ifdef INET6
146static int tcp6_cksum(const struct ip6_hdr *ip6, const struct tcphdr *tp,
159}
160
161#ifdef INET6
162static int tcp6_cksum(const struct ip6_hdr *ip6, const struct tcphdr *tp,
147 u_int len)
163 u_int len)
148{
164{
149 size_t i;
150 register const u_int16_t *sp;
151 u_int32_t sum;
152 union {
153 struct {
154 struct in6_addr ph_src;
155 struct in6_addr ph_dst;
156 u_int32_t ph_len;
157 u_int8_t ph_zero[3];
158 u_int8_t ph_nxt;
159 } ph;
160 u_int16_t pa[20];
161 } phu;
165 size_t i;
166 u_int32_t sum = 0;
167 union {
168 struct {
169 struct in6_addr ph_src;
170 struct in6_addr ph_dst;
171 u_int32_t ph_len;
172 u_int8_t ph_zero[3];
173 u_int8_t ph_nxt;
174 } ph;
175 u_int16_t pa[20];
176 } phu;
162
177
163 /* pseudo-header */
164 memset(&phu, 0, sizeof(phu));
165 phu.ph.ph_src = ip6->ip6_src;
166 phu.ph.ph_dst = ip6->ip6_dst;
167 phu.ph.ph_len = htonl(len);
168 phu.ph.ph_nxt = IPPROTO_TCP;
178 /* pseudo-header */
179 memset(&phu, 0, sizeof(phu));
180 phu.ph.ph_src = ip6->ip6_src;
181 phu.ph.ph_dst = ip6->ip6_dst;
182 phu.ph.ph_len = htonl(len);
183 phu.ph.ph_nxt = IPPROTO_TCP;
169
184
170 sum = 0;
171 for (i = 0; i < sizeof(phu.pa) / sizeof(phu.pa[0]); i++)
172 sum += phu.pa[i];
185 for (i = 0; i < sizeof(phu.pa) / sizeof(phu.pa[0]); i++)
186 sum += phu.pa[i];
173
187
174 sp = (const u_int16_t *)tp;
175
176 for (i = 0; i < (len & ~1); i += 2)
177 sum += *sp++;
178
179 if (len & 1)
180 sum += htons((*(const u_int8_t *)sp) << 8);
181
182 while (sum > 0xffff)
183 sum = (sum & 0xffff) + (sum >> 16);
184 sum = ~sum & 0xffff;
185
186 return (sum);
188 return in_cksum((u_short *)tp, len, sum);
187}
188#endif
189
190void
191tcp_print(register const u_char *bp, register u_int length,
192 register const u_char *bp2, int fragmented)
193{
189}
190#endif
191
192void
193tcp_print(register const u_char *bp, register u_int length,
194 register const u_char *bp2, int fragmented)
195{
194 register const struct tcphdr *tp;
195 register const struct ip *ip;
196 register u_char flags;
197 register u_int hlen;
198 register char ch;
199 u_int16_t sport, dport, win, urp;
200 u_int32_t seq, ack, thseq, thack;
201 int threv;
196 register const struct tcphdr *tp;
197 register const struct ip *ip;
198 register u_char flags;
199 register u_int hlen;
200 register char ch;
201 u_int16_t sport, dport, win, urp;
202 u_int32_t seq, ack, thseq, thack;
203 int threv;
202#ifdef INET6
204#ifdef INET6
203 register const struct ip6_hdr *ip6;
205 register const struct ip6_hdr *ip6;
204#endif
205
206#endif
207
206 tp = (struct tcphdr *)bp;
207 ip = (struct ip *)bp2;
208 tp = (struct tcphdr *)bp;
209 ip = (struct ip *)bp2;
208#ifdef INET6
210#ifdef INET6
209 if (IP_V(ip) == 6)
210 ip6 = (struct ip6_hdr *)bp2;
211 else
212 ip6 = NULL;
211 if (IP_V(ip) == 6)
212 ip6 = (struct ip6_hdr *)bp2;
213 else
214 ip6 = NULL;
213#endif /*INET6*/
215#endif /*INET6*/
214 ch = '\0';
215 if (!TTEST(tp->th_dport)) {
216 (void)printf("%s > %s: [|tcp]",
217 ipaddr_string(&ip->ip_src),
218 ipaddr_string(&ip->ip_dst));
219 return;
220 }
216 ch = '\0';
217 if (!TTEST(tp->th_dport)) {
218 (void)printf("%s > %s: [|tcp]",
219 ipaddr_string(&ip->ip_src),
220 ipaddr_string(&ip->ip_dst));
221 return;
222 }
221
223
222 sport = EXTRACT_16BITS(&tp->th_sport);
223 dport = EXTRACT_16BITS(&tp->th_dport);
224 sport = EXTRACT_16BITS(&tp->th_sport);
225 dport = EXTRACT_16BITS(&tp->th_dport);
224
226
225 hlen = TH_OFF(tp) * 4;
227 hlen = TH_OFF(tp) * 4;
226
228
227 /*
229 /*
228 * If data present, header length valid, and NFS port used,
229 * assume NFS.
230 * Pass offset of data plus 4 bytes for RPC TCP msg length
231 * to NFS print routines.
232 */
230 * If data present, header length valid, and NFS port used,
231 * assume NFS.
232 * Pass offset of data plus 4 bytes for RPC TCP msg length
233 * to NFS print routines.
234 */
233 if (!qflag && hlen >= sizeof(*tp) && hlen <= length) {
234 if ((u_char *)tp + 4 + sizeof(struct sunrpc_msg) <= snapend &&
235 dport == NFS_PORT) {
236 nfsreq_print((u_char *)tp + hlen + 4, length - hlen,
237 (u_char *)ip);
238 return;
239 } else if ((u_char *)tp + 4 + sizeof(struct sunrpc_msg)
240 <= snapend &&
241 sport == NFS_PORT) {
242 nfsreply_print((u_char *)tp + hlen + 4, length - hlen,
243 (u_char *)ip);
244 return;
245 }
246 }
235 if (!qflag && hlen >= sizeof(*tp) && hlen <= length &&
236 (length - hlen) >= 4) {
237 u_char *fraglenp;
238 u_int32_t fraglen;
239 register struct sunrpc_msg *rp;
240 enum sunrpc_msg_type direction;
241
242 fraglenp = (u_char *)tp + hlen;
243 if (TTEST2(*fraglenp, 4)) {
244 fraglen = EXTRACT_32BITS(fraglenp) & 0x7FFFFFFF;
245 if (fraglen > (length - hlen) - 4)
246 fraglen = (length - hlen) - 4;
247 rp = (struct sunrpc_msg *)(fraglenp + 4);
248 if (TTEST(rp->rm_direction)) {
249 direction = (enum sunrpc_msg_type)EXTRACT_32BITS(&rp->rm_direction);
250 if (dport == NFS_PORT &&
251 direction == SUNRPC_CALL) {
252 nfsreq_print((u_char *)rp, fraglen,
253 (u_char *)ip);
254 return;
255 }
256 if (sport == NFS_PORT &&
257 direction == SUNRPC_REPLY) {
258 nfsreply_print((u_char *)rp, fraglen,
259 (u_char *)ip);
260 return;
261 }
262 }
263 }
264 }
247#ifdef INET6
265#ifdef INET6
248 if (ip6) {
249 if (ip6->ip6_nxt == IPPROTO_TCP) {
250 (void)printf("%s.%s > %s.%s: ",
251 ip6addr_string(&ip6->ip6_src),
252 tcpport_string(sport),
253 ip6addr_string(&ip6->ip6_dst),
254 tcpport_string(dport));
255 } else {
256 (void)printf("%s > %s: ",
257 tcpport_string(sport), tcpport_string(dport));
258 }
259 } else
266 if (ip6) {
267 if (ip6->ip6_nxt == IPPROTO_TCP) {
268 (void)printf("%s.%s > %s.%s: ",
269 ip6addr_string(&ip6->ip6_src),
270 tcpport_string(sport),
271 ip6addr_string(&ip6->ip6_dst),
272 tcpport_string(dport));
273 } else {
274 (void)printf("%s > %s: ",
275 tcpport_string(sport), tcpport_string(dport));
276 }
277 } else
260#endif /*INET6*/
278#endif /*INET6*/
261 {
262 if (ip->ip_p == IPPROTO_TCP) {
263 (void)printf("%s.%s > %s.%s: ",
264 ipaddr_string(&ip->ip_src),
265 tcpport_string(sport),
266 ipaddr_string(&ip->ip_dst),
267 tcpport_string(dport));
268 } else {
269 (void)printf("%s > %s: ",
270 tcpport_string(sport), tcpport_string(dport));
271 }
272 }
279 {
280 if (ip->ip_p == IPPROTO_TCP) {
281 (void)printf("%s.%s > %s.%s: ",
282 ipaddr_string(&ip->ip_src),
283 tcpport_string(sport),
284 ipaddr_string(&ip->ip_dst),
285 tcpport_string(dport));
286 } else {
287 (void)printf("%s > %s: ",
288 tcpport_string(sport), tcpport_string(dport));
289 }
290 }
273
291
274 if (hlen < sizeof(*tp)) {
275 (void)printf(" tcp %d [bad hdr length %u - too short, < %lu]",
276 length - hlen, hlen, (unsigned long)sizeof(*tp));
277 return;
278 }
292 if (hlen < sizeof(*tp)) {
293 (void)printf(" tcp %d [bad hdr length %u - too short, < %lu]",
294 length - hlen, hlen, (unsigned long)sizeof(*tp));
295 return;
296 }
279
297
280 TCHECK(*tp);
298 TCHECK(*tp);
281
299
282 seq = EXTRACT_32BITS(&tp->th_seq);
283 ack = EXTRACT_32BITS(&tp->th_ack);
284 win = EXTRACT_16BITS(&tp->th_win);
285 urp = EXTRACT_16BITS(&tp->th_urp);
300 seq = EXTRACT_32BITS(&tp->th_seq);
301 ack = EXTRACT_32BITS(&tp->th_ack);
302 win = EXTRACT_16BITS(&tp->th_win);
303 urp = EXTRACT_16BITS(&tp->th_urp);
286
304
287 if (qflag) {
288 (void)printf("tcp %d", length - hlen);
289 if (hlen > length) {
290 (void)printf(" [bad hdr length %u - too long, > %u]",
291 hlen, length);
292 }
293 return;
294 }
295 if ((flags = tp->th_flags) & (TH_SYN|TH_FIN|TH_RST|TH_PUSH|
296 TH_ECNECHO|TH_CWR)) {
297 if (flags & TH_SYN)
298 putchar('S');
299 if (flags & TH_FIN)
300 putchar('F');
301 if (flags & TH_RST)
302 putchar('R');
303 if (flags & TH_PUSH)
304 putchar('P');
305 if (flags & TH_CWR)
306 putchar('W'); /* congestion _W_indow reduced (ECN) */
307 if (flags & TH_ECNECHO)
308 putchar('E'); /* ecn _E_cho sent (ECN) */
309 } else
310 putchar('.');
305 if (qflag) {
306 (void)printf("tcp %d", length - hlen);
307 if (hlen > length) {
308 (void)printf(" [bad hdr length %u - too long, > %u]",
309 hlen, length);
310 }
311 return;
312 }
311
313
312 if (!Sflag && (flags & TH_ACK)) {
313 register struct tcp_seq_hash *th;
314 const void *src, *dst;
315 register int rev;
316 struct tha tha;
317 /*
318 * Find (or record) the initial sequence numbers for
319 * this conversation. (we pick an arbitrary
320 * collating order so there's only one entry for
321 * both directions).
322 */
314 flags = tp->th_flags;
315 printf("Flags [%s]", bittok2str_nosep(tcp_flag_values, "none", flags));
316
317 if (!Sflag && (flags & TH_ACK)) {
318 register struct tcp_seq_hash *th;
319 const void *src, *dst;
320 register int rev;
321 struct tha tha;
322 /*
323 * Find (or record) the initial sequence numbers for
324 * this conversation. (we pick an arbitrary
325 * collating order so there's only one entry for
326 * both directions).
327 */
323#ifdef INET6
328#ifdef INET6
324 memset(&tha, 0, sizeof(tha));
325 rev = 0;
326 if (ip6) {
327 src = &ip6->ip6_src;
328 dst = &ip6->ip6_dst;
329 if (sport > dport)
330 rev = 1;
331 else if (sport == dport) {
332 if (memcmp(src, dst, sizeof ip6->ip6_dst) > 0)
333 rev = 1;
334 }
335 if (rev) {
336 memcpy(&tha.src, dst, sizeof ip6->ip6_dst);
337 memcpy(&tha.dst, src, sizeof ip6->ip6_src);
338 tha.port = dport << 16 | sport;
339 } else {
340 memcpy(&tha.dst, dst, sizeof ip6->ip6_dst);
341 memcpy(&tha.src, src, sizeof ip6->ip6_src);
342 tha.port = sport << 16 | dport;
343 }
344 } else {
345 src = &ip->ip_src;
346 dst = &ip->ip_dst;
347 if (sport > dport)
348 rev = 1;
349 else if (sport == dport) {
350 if (memcmp(src, dst, sizeof ip->ip_dst) > 0)
351 rev = 1;
352 }
353 if (rev) {
354 memcpy(&tha.src, dst, sizeof ip->ip_dst);
355 memcpy(&tha.dst, src, sizeof ip->ip_src);
356 tha.port = dport << 16 | sport;
357 } else {
358 memcpy(&tha.dst, dst, sizeof ip->ip_dst);
359 memcpy(&tha.src, src, sizeof ip->ip_src);
360 tha.port = sport << 16 | dport;
361 }
362 }
329 memset(&tha, 0, sizeof(tha));
330 rev = 0;
331 if (ip6) {
332 src = &ip6->ip6_src;
333 dst = &ip6->ip6_dst;
334 if (sport > dport)
335 rev = 1;
336 else if (sport == dport) {
337 if (memcmp(src, dst, sizeof ip6->ip6_dst) > 0)
338 rev = 1;
339 }
340 if (rev) {
341 memcpy(&tha.src, dst, sizeof ip6->ip6_dst);
342 memcpy(&tha.dst, src, sizeof ip6->ip6_src);
343 tha.port = dport << 16 | sport;
344 } else {
345 memcpy(&tha.dst, dst, sizeof ip6->ip6_dst);
346 memcpy(&tha.src, src, sizeof ip6->ip6_src);
347 tha.port = sport << 16 | dport;
348 }
349 } else {
350 src = &ip->ip_src;
351 dst = &ip->ip_dst;
352 if (sport > dport)
353 rev = 1;
354 else if (sport == dport) {
355 if (memcmp(src, dst, sizeof ip->ip_dst) > 0)
356 rev = 1;
357 }
358 if (rev) {
359 memcpy(&tha.src, dst, sizeof ip->ip_dst);
360 memcpy(&tha.dst, src, sizeof ip->ip_src);
361 tha.port = dport << 16 | sport;
362 } else {
363 memcpy(&tha.dst, dst, sizeof ip->ip_dst);
364 memcpy(&tha.src, src, sizeof ip->ip_src);
365 tha.port = sport << 16 | dport;
366 }
367 }
363#else
368#else
364 rev = 0;
365 src = &ip->ip_src;
366 dst = &ip->ip_dst;
367 if (sport > dport)
368 rev = 1;
369 else if (sport == dport) {
370 if (memcmp(src, dst, sizeof ip->ip_dst) > 0)
371 rev = 1;
372 }
373 if (rev) {
374 memcpy(&tha.src, dst, sizeof ip->ip_dst);
375 memcpy(&tha.dst, src, sizeof ip->ip_src);
376 tha.port = dport << 16 | sport;
377 } else {
378 memcpy(&tha.dst, dst, sizeof ip->ip_dst);
379 memcpy(&tha.src, src, sizeof ip->ip_src);
380 tha.port = sport << 16 | dport;
381 }
369 rev = 0;
370 src = &ip->ip_src;
371 dst = &ip->ip_dst;
372 if (sport > dport)
373 rev = 1;
374 else if (sport == dport) {
375 if (memcmp(src, dst, sizeof ip->ip_dst) > 0)
376 rev = 1;
377 }
378 if (rev) {
379 memcpy(&tha.src, dst, sizeof ip->ip_dst);
380 memcpy(&tha.dst, src, sizeof ip->ip_src);
381 tha.port = dport << 16 | sport;
382 } else {
383 memcpy(&tha.dst, dst, sizeof ip->ip_dst);
384 memcpy(&tha.src, src, sizeof ip->ip_src);
385 tha.port = sport << 16 | dport;
386 }
382#endif
383
387#endif
388
384 threv = rev;
385 for (th = &tcp_seq_hash[tha.port % TSEQ_HASHSIZE];
386 th->nxt; th = th->nxt)
387 if (memcmp((char *)&tha, (char *)&th->addr,
388 sizeof(th->addr)) == 0)
389 break;
389 threv = rev;
390 for (th = &tcp_seq_hash[tha.port % TSEQ_HASHSIZE];
391 th->nxt; th = th->nxt)
392 if (memcmp((char *)&tha, (char *)&th->addr,
393 sizeof(th->addr)) == 0)
394 break;
390
395
391 if (!th->nxt || (flags & TH_SYN)) {
392 /* didn't find it or new conversation */
393 if (th->nxt == NULL) {
394 th->nxt = (struct tcp_seq_hash *)
395 calloc(1, sizeof(*th));
396 if (th->nxt == NULL)
397 error("tcp_print: calloc");
398 }
399 th->addr = tha;
400 if (rev)
401 th->ack = seq, th->seq = ack - 1;
402 else
403 th->seq = seq, th->ack = ack - 1;
404 } else {
405 if (rev)
406 seq -= th->ack, ack -= th->seq;
407 else
408 seq -= th->seq, ack -= th->ack;
409 }
396 if (!th->nxt || (flags & TH_SYN)) {
397 /* didn't find it or new conversation */
398 if (th->nxt == NULL) {
399 th->nxt = (struct tcp_seq_hash *)
400 calloc(1, sizeof(*th));
401 if (th->nxt == NULL)
402 error("tcp_print: calloc");
403 }
404 th->addr = tha;
405 if (rev)
406 th->ack = seq, th->seq = ack - 1;
407 else
408 th->seq = seq, th->ack = ack - 1;
409 } else {
410 if (rev)
411 seq -= th->ack, ack -= th->seq;
412 else
413 seq -= th->seq, ack -= th->ack;
414 }
410
415
411 thseq = th->seq;
412 thack = th->ack;
413 } else {
414 /*fool gcc*/
415 thseq = thack = threv = 0;
416 }
417 if (hlen > length) {
418 (void)printf(" [bad hdr length %u - too long, > %u]",
419 hlen, length);
420 return;
421 }
416 thseq = th->seq;
417 thack = th->ack;
418 } else {
419 /*fool gcc*/
420 thseq = thack = threv = 0;
421 }
422 if (hlen > length) {
423 (void)printf(" [bad hdr length %u - too long, > %u]",
424 hlen, length);
425 return;
426 }
422
427
423 if (IP_V(ip) == 4 && vflag && !fragmented) {
424 u_int16_t sum, tcp_sum;
425 if (TTEST2(tp->th_sport, length)) {
426 sum = tcp_cksum(ip, tp, length);
428 if (IP_V(ip) == 4 && vflag && !Kflag && !fragmented) {
429 u_int16_t sum, tcp_sum;
430 if (TTEST2(tp->th_sport, length)) {
431 sum = tcp_cksum(ip, tp, length);
427
428 (void)printf(", cksum 0x%04x",EXTRACT_16BITS(&tp->th_sum));
432
433 (void)printf(", cksum 0x%04x",EXTRACT_16BITS(&tp->th_sum));
429 if (sum != 0) {
430 tcp_sum = EXTRACT_16BITS(&tp->th_sum);
431 (void)printf(" (incorrect (-> 0x%04x),",in_cksum_shouldbe(tcp_sum, sum));
432 } else
433 (void)printf(" (correct),");
434 }
435 }
434 if (sum != 0) {
435 tcp_sum = EXTRACT_16BITS(&tp->th_sum);
436 (void)printf(" (incorrect -> 0x%04x)",in_cksum_shouldbe(tcp_sum, sum));
437 } else
438 (void)printf(" (correct)");
439 }
440 }
436#ifdef INET6
441#ifdef INET6
437 if (IP_V(ip) == 6 && ip6->ip6_plen && vflag && !fragmented) {
438 u_int16_t sum,tcp_sum;
439 if (TTEST2(tp->th_sport, length)) {
440 sum = tcp6_cksum(ip6, tp, length);
442 if (IP_V(ip) == 6 && ip6->ip6_plen && vflag && !Kflag && !fragmented) {
443 u_int16_t sum,tcp_sum;
444 if (TTEST2(tp->th_sport, length)) {
445 sum = tcp6_cksum(ip6, tp, length);
441 (void)printf(", cksum 0x%04x",EXTRACT_16BITS(&tp->th_sum));
446 (void)printf(", cksum 0x%04x",EXTRACT_16BITS(&tp->th_sum));
442 if (sum != 0) {
443 tcp_sum = EXTRACT_16BITS(&tp->th_sum);
444 (void)printf(" (incorrect (-> 0x%04x),",in_cksum_shouldbe(tcp_sum, sum));
445 } else
446 (void)printf(" (correct),");
447 if (sum != 0) {
448 tcp_sum = EXTRACT_16BITS(&tp->th_sum);
449 (void)printf(" (incorrect -> 0x%04x)",in_cksum_shouldbe(tcp_sum, sum));
450 } else
451 (void)printf(" (correct)");
447
452
448 }
449 }
453 }
454 }
450#endif
451
455#endif
456
452 length -= hlen;
453 if (vflag > 1 || length > 0 || flags & (TH_SYN | TH_FIN | TH_RST))
454 (void)printf(" %u:%u(%u)", seq, seq + length, length);
455 if (flags & TH_ACK)
456 (void)printf(" ack %u", ack);
457 length -= hlen;
458 if (vflag > 1 || flags & (TH_SYN | TH_FIN | TH_RST)) {
459 (void)printf(", seq %u", seq);
457
460
458 (void)printf(" win %d", win);
461 if (length > 0) {
462 (void)printf(":%u", seq + length);
463 }
464 }
459
465
460 if (flags & TH_URG)
461 (void)printf(" urg %d", urp);
462 /*
463 * Handle any options.
464 */
465 if (hlen > sizeof(*tp)) {
466 register const u_char *cp;
467 register u_int i, opt, datalen;
468 register u_int len;
466 if (flags & TH_ACK) {
467 (void)printf(", ack %u", ack);
468 }
469
469
470 hlen -= sizeof(*tp);
471 cp = (const u_char *)tp + sizeof(*tp);
472 putchar(' ');
473 ch = '<';
474 while (hlen > 0) {
475 putchar(ch);
476 TCHECK(*cp);
477 opt = *cp++;
478 if (ZEROLENOPT(opt))
479 len = 1;
480 else {
481 TCHECK(*cp);
482 len = *cp++; /* total including type, len */
483 if (len < 2 || len > hlen)
484 goto bad;
485 --hlen; /* account for length byte */
486 }
487 --hlen; /* account for type byte */
488 datalen = 0;
470 (void)printf(", win %d", win);
489
471
472 if (flags & TH_URG)
473 (void)printf(", urg %d", urp);
474 /*
475 * Handle any options.
476 */
477 if (hlen > sizeof(*tp)) {
478 register const u_char *cp;
479 register u_int i, opt, datalen;
480 register u_int len;
481
482 hlen -= sizeof(*tp);
483 cp = (const u_char *)tp + sizeof(*tp);
484 printf(", options [");
485 while (hlen > 0) {
486 if (ch != '\0')
487 putchar(ch);
488 TCHECK(*cp);
489 opt = *cp++;
490 if (ZEROLENOPT(opt))
491 len = 1;
492 else {
493 TCHECK(*cp);
494 len = *cp++; /* total including type, len */
495 if (len < 2 || len > hlen)
496 goto bad;
497 --hlen; /* account for length byte */
498 }
499 --hlen; /* account for type byte */
500 datalen = 0;
501
490/* Bail if "l" bytes of data are not left or were not captured */
491#define LENCHECK(l) { if ((l) > hlen) goto bad; TCHECK2(*cp, l); }
492
502/* Bail if "l" bytes of data are not left or were not captured */
503#define LENCHECK(l) { if ((l) > hlen) goto bad; TCHECK2(*cp, l); }
504
493 switch (opt) {
494
505
495 case TCPOPT_MAXSEG:
496 (void)printf("mss");
497 datalen = 2;
498 LENCHECK(datalen);
499 (void)printf(" %u", EXTRACT_16BITS(cp));
506 printf("%s", tok2str(tcp_option_values, "Unknown Option %u", opt));
500
507
501 break;
508 switch (opt) {
502
509
503 case TCPOPT_EOL:
504 (void)printf("eol");
505 break;
510 case TCPOPT_MAXSEG:
511 datalen = 2;
512 LENCHECK(datalen);
513 (void)printf(" %u", EXTRACT_16BITS(cp));
514 break;
506
515
507 case TCPOPT_NOP:
508 (void)printf("nop");
509 break;
516 case TCPOPT_WSCALE:
517 datalen = 1;
518 LENCHECK(datalen);
519 (void)printf(" %u", *cp);
520 break;
510
521
511 case TCPOPT_WSCALE:
512 (void)printf("wscale");
513 datalen = 1;
514 LENCHECK(datalen);
515 (void)printf(" %u", *cp);
516 break;
522 case TCPOPT_SACK:
523 datalen = len - 2;
524 if (datalen % 8 != 0) {
525 (void)printf("malformed sack");
526 } else {
527 u_int32_t s, e;
517
528
518 case TCPOPT_SACKOK:
519 (void)printf("sackOK");
520 break;
529 (void)printf(" %d ", datalen / 8);
530 for (i = 0; i < datalen; i += 8) {
531 LENCHECK(i + 4);
532 s = EXTRACT_32BITS(cp + i);
533 LENCHECK(i + 8);
534 e = EXTRACT_32BITS(cp + i + 4);
535 if (threv) {
536 s -= thseq;
537 e -= thseq;
538 } else {
539 s -= thack;
540 e -= thack;
541 }
542 (void)printf("{%u:%u}", s, e);
543 }
544 }
545 break;
521
546
522 case TCPOPT_SACK:
523 datalen = len - 2;
524 if (datalen % 8 != 0) {
525 (void)printf("malformed sack");
526 } else {
527 u_int32_t s, e;
547 case TCPOPT_CC:
548 case TCPOPT_CCNEW:
549 case TCPOPT_CCECHO:
550 case TCPOPT_ECHO:
551 case TCPOPT_ECHOREPLY:
528
552
529 (void)printf("sack %d ", datalen / 8);
530 for (i = 0; i < datalen; i += 8) {
531 LENCHECK(i + 4);
532 s = EXTRACT_32BITS(cp + i);
533 LENCHECK(i + 8);
534 e = EXTRACT_32BITS(cp + i + 4);
535 if (threv) {
536 s -= thseq;
537 e -= thseq;
538 } else {
539 s -= thack;
540 e -= thack;
541 }
542 (void)printf("{%u:%u}", s, e);
543 }
544 }
545 break;
553 /*
554 * those options share their semantics.
555 * fall through
556 */
557 datalen = 4;
558 LENCHECK(datalen);
559 (void)printf(" %u", EXTRACT_32BITS(cp));
560 break;
546
561
547 case TCPOPT_ECHO:
548 (void)printf("echo");
549 datalen = 4;
550 LENCHECK(datalen);
551 (void)printf(" %u", EXTRACT_32BITS(cp));
552 break;
562 case TCPOPT_TIMESTAMP:
563 datalen = 8;
564 LENCHECK(datalen);
565 (void)printf(" val %u ecr %u",
566 EXTRACT_32BITS(cp),
567 EXTRACT_32BITS(cp + 4));
568 break;
553
569
554 case TCPOPT_ECHOREPLY:
555 (void)printf("echoreply");
556 datalen = 4;
557 LENCHECK(datalen);
558 (void)printf(" %u", EXTRACT_32BITS(cp));
559 break;
570 case TCPOPT_SIGNATURE:
571 datalen = TCP_SIGLEN;
572 LENCHECK(datalen);
573#ifdef HAVE_LIBCRYPTO
574 switch (tcp_verify_signature(ip, tp,
575 bp + TH_OFF(tp) * 4, length, cp)) {
560
576
561 case TCPOPT_TIMESTAMP:
562 (void)printf("timestamp");
563 datalen = 8;
564 LENCHECK(4);
565 (void)printf(" %u", EXTRACT_32BITS(cp));
566 LENCHECK(datalen);
567 (void)printf(" %u", EXTRACT_32BITS(cp + 4));
568 break;
577 case SIGNATURE_VALID:
578 (void)printf("valid");
579 break;
569
580
570 case TCPOPT_CC:
571 (void)printf("cc");
572 datalen = 4;
573 LENCHECK(datalen);
574 (void)printf(" %u", EXTRACT_32BITS(cp));
575 break;
581 case SIGNATURE_INVALID:
582 (void)printf("invalid");
583 break;
576
584
577 case TCPOPT_CCNEW:
578 (void)printf("ccnew");
579 datalen = 4;
580 LENCHECK(datalen);
581 (void)printf(" %u", EXTRACT_32BITS(cp));
582 break;
585 case CANT_CHECK_SIGNATURE:
586 (void)printf("can't check - ");
587 for (i = 0; i < TCP_SIGLEN; ++i)
588 (void)printf("%02x", cp[i]);
589 break;
590 }
591#else
592 for (i = 0; i < TCP_SIGLEN; ++i)
593 (void)printf("%02x", cp[i]);
594#endif
595 break;
583
596
584 case TCPOPT_CCECHO:
585 (void)printf("ccecho");
586 datalen = 4;
587 LENCHECK(datalen);
588 (void)printf(" %u", EXTRACT_32BITS(cp));
589 break;
597 case TCPOPT_AUTH:
598 (void)printf("keyid %d", *cp++);
599 datalen = len - 3;
600 for (i = 0; i < datalen; ++i) {
601 LENCHECK(i);
602 (void)printf("%02x", cp[i]);
603 }
604 break;
590
605
591 case TCPOPT_SIGNATURE:
592 (void)printf("md5:");
593 datalen = TCP_SIGLEN;
594 LENCHECK(datalen);
595#ifdef HAVE_LIBCRYPTO
596 switch (tcp_verify_signature(ip, tp,
597 bp + TH_OFF(tp) * 4, length, cp)) {
598
606
599 case SIGNATURE_VALID:
600 (void)printf("valid");
601 break;
607 case TCPOPT_EOL:
608 case TCPOPT_NOP:
609 case TCPOPT_SACKOK:
610 /*
611 * Nothing interesting.
612 * fall through
613 */
614 break;
602
615
603 case SIGNATURE_INVALID:
604 (void)printf("invalid");
605 break;
616 default:
617 datalen = len - 2;
618 for (i = 0; i < datalen; ++i) {
619 LENCHECK(i);
620 (void)printf("%02x", cp[i]);
621 }
622 break;
623 }
606
624
607 case CANT_CHECK_SIGNATURE:
608 (void)printf("can't check - ");
609 for (i = 0; i < TCP_SIGLEN; ++i)
610 (void)printf("%02x", cp[i]);
611 break;
612 }
613#else
614 for (i = 0; i < TCP_SIGLEN; ++i)
615 (void)printf("%02x", cp[i]);
616#endif
617 break;
625 /* Account for data printed */
626 cp += datalen;
627 hlen -= datalen;
618
628
619 default:
620 (void)printf("opt-%u:", opt);
621 datalen = len - 2;
622 for (i = 0; i < datalen; ++i) {
623 LENCHECK(i);
624 (void)printf("%02x", cp[i]);
625 }
626 break;
627 }
629 /* Check specification against observed length */
630 ++datalen; /* option octet */
631 if (!ZEROLENOPT(opt))
632 ++datalen; /* size octet */
633 if (datalen != len)
634 (void)printf("[len %d]", len);
635 ch = ',';
636 if (opt == TCPOPT_EOL)
637 break;
638 }
639 putchar(']');
640 }
628
641
629 /* Account for data printed */
630 cp += datalen;
631 hlen -= datalen;
642 /*
643 * Print length field before crawling down the stack.
644 */
645 printf(", length %u", length);
632
646
633 /* Check specification against observed length */
634 ++datalen; /* option octet */
635 if (!ZEROLENOPT(opt))
636 ++datalen; /* size octet */
637 if (datalen != len)
638 (void)printf("[len %d]", len);
639 ch = ',';
640 if (opt == TCPOPT_EOL)
641 break;
642 }
643 putchar('>');
644 }
647 if (length <= 0)
648 return;
645
649
646 if (length <= 0)
647 return;
650 /*
651 * Decode payload if necessary.
652 */
653 bp += TH_OFF(tp) * 4;
654 if ((flags & TH_RST) && vflag) {
655 print_tcp_rst_data(bp, length);
656 return;
657 }
648
658
649 /*
650 * Decode payload if necessary.
651 */
652 bp += TH_OFF(tp) * 4;
653 if (flags & TH_RST) {
654 if (vflag)
655 print_tcp_rst_data(bp, length);
656 } else {
657 if (sport == TELNET_PORT || dport == TELNET_PORT) {
658 if (!qflag && vflag)
659 telnet_print(bp, length);
660 } else if (sport == BGP_PORT || dport == BGP_PORT)
661 bgp_print(bp, length);
662 else if (sport == PPTP_PORT || dport == PPTP_PORT)
663 pptp_print(bp);
659 if (sport == TELNET_PORT || dport == TELNET_PORT) {
660 if (!qflag && vflag)
661 telnet_print(bp, length);
662 } else if (sport == BGP_PORT || dport == BGP_PORT)
663 bgp_print(bp, length);
664 else if (sport == PPTP_PORT || dport == PPTP_PORT)
665 pptp_print(bp);
664#ifdef TCPDUMP_DO_SMB
666#ifdef TCPDUMP_DO_SMB
665 else if (sport == NETBIOS_SSN_PORT || dport == NETBIOS_SSN_PORT)
666 nbt_tcp_print(bp, length);
667 else if (sport == NETBIOS_SSN_PORT || dport == NETBIOS_SSN_PORT)
668 nbt_tcp_print(bp, length);
669 else if (sport == SMB_PORT || dport == SMB_PORT)
670 smb_tcp_print(bp, length);
667#endif
671#endif
668 else if (sport == BEEP_PORT || dport == BEEP_PORT)
669 beep_print(bp, length);
670 else if (length > 2 &&
671 (sport == NAMESERVER_PORT || dport == NAMESERVER_PORT ||
672 sport == MULTICASTDNS_PORT || dport == MULTICASTDNS_PORT)) {
673 /*
674 * TCP DNS query has 2byte length at the head.
675 * XXX packet could be unaligned, it can go strange
676 */
677 ns_print(bp + 2, length - 2, 0);
678 } else if (sport == MSDP_PORT || dport == MSDP_PORT) {
679 msdp_print(bp, length);
680 }
681 else if (length > 0 && (sport == LDP_PORT || dport == LDP_PORT)) {
682 ldp_print(bp, length);
683 }
684 }
685 return;
686bad:
687 fputs("[bad opt]", stdout);
688 if (ch != '\0')
689 putchar('>');
690 return;
691trunc:
692 fputs("[|tcp]", stdout);
693 if (ch != '\0')
694 putchar('>');
672 else if (sport == BEEP_PORT || dport == BEEP_PORT)
673 beep_print(bp, length);
674 else if (length > 2 &&
675 (sport == NAMESERVER_PORT || dport == NAMESERVER_PORT ||
676 sport == MULTICASTDNS_PORT || dport == MULTICASTDNS_PORT)) {
677 /*
678 * TCP DNS query has 2byte length at the head.
679 * XXX packet could be unaligned, it can go strange
680 */
681 ns_print(bp + 2, length - 2, 0);
682 } else if (sport == MSDP_PORT || dport == MSDP_PORT) {
683 msdp_print(bp, length);
684 }
685 else if (length > 0 && (sport == LDP_PORT || dport == LDP_PORT)) {
686 ldp_print(bp, length);
687 }
688
689 return;
690 bad:
691 fputs("[bad opt]", stdout);
692 if (ch != '\0')
693 putchar('>');
694 return;
695 trunc:
696 fputs("[|tcp]", stdout);
697 if (ch != '\0')
698 putchar('>');
695}
696
697/*
698 * RFC1122 says the following on data in RST segments:
699 *
700 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
701 *
702 * A TCP SHOULD allow a received RST segment to include data.

--- 4 unchanged lines hidden (view full) ---

707 * RST. No standard has yet been established for such
708 * data.
709 *
710 */
711
712static void
713print_tcp_rst_data(register const u_char *sp, u_int length)
714{
699}
700
701/*
702 * RFC1122 says the following on data in RST segments:
703 *
704 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
705 *
706 * A TCP SHOULD allow a received RST segment to include data.

--- 4 unchanged lines hidden (view full) ---

711 * RST. No standard has yet been established for such
712 * data.
713 *
714 */
715
716static void
717print_tcp_rst_data(register const u_char *sp, u_int length)
718{
715 int c;
719 int c;
716
720
717 if (TTEST2(*sp, length))
718 printf(" [RST");
719 else
720 printf(" [!RST");
721 if (length > MAX_RST_DATA_LEN) {
722 length = MAX_RST_DATA_LEN; /* can use -X for longer */
723 putchar('+'); /* indicate we truncate */
724 }
725 putchar(' ');
726 while (length-- && sp <= snapend) {
727 c = *sp++;
728 safeputchar(c);
729 }
730 putchar(']');
721 if (TTEST2(*sp, length))
722 printf(" [RST");
723 else
724 printf(" [!RST");
725 if (length > MAX_RST_DATA_LEN) {
726 length = MAX_RST_DATA_LEN; /* can use -X for longer */
727 putchar('+'); /* indicate we truncate */
728 }
729 putchar(' ');
730 while (length-- && sp <= snapend) {
731 c = *sp++;
732 safeputchar(c);
733 }
734 putchar(']');
731}
732
733#ifdef HAVE_LIBCRYPTO
734static int
735tcp_verify_signature(const struct ip *ip, const struct tcphdr *tp,
735}
736
737#ifdef HAVE_LIBCRYPTO
738static int
739tcp_verify_signature(const struct ip *ip, const struct tcphdr *tp,
736 const u_char *data, int length, const u_char *rcvsig)
740 const u_char *data, int length, const u_char *rcvsig)
737{
738 struct tcphdr tp1;
741{
742 struct tcphdr tp1;
739 u_char sig[TCP_SIGLEN];
740 char zero_proto = 0;
741 MD5_CTX ctx;
742 u_int16_t savecsum, tlen;
743 u_char sig[TCP_SIGLEN];
744 char zero_proto = 0;
745 MD5_CTX ctx;
746 u_int16_t savecsum, tlen;
743#ifdef INET6
747#ifdef INET6
744 struct ip6_hdr *ip6;
748 struct ip6_hdr *ip6;
749 u_int32_t len32;
750 u_int8_t nxt;
745#endif
751#endif
746 u_int32_t len32;
747 u_int8_t nxt;
748
752
749 tp1 = *tp;
753 tp1 = *tp;
750
754
751 if (tcpmd5secret == NULL)
752 return (CANT_CHECK_SIGNATURE);
755 if (tcpmd5secret == NULL)
756 return (CANT_CHECK_SIGNATURE);
753
757
754 MD5_Init(&ctx);
755 /*
756 * Step 1: Update MD5 hash with IP pseudo-header.
757 */
758 if (IP_V(ip) == 4) {
759 MD5_Update(&ctx, (char *)&ip->ip_src, sizeof(ip->ip_src));
760 MD5_Update(&ctx, (char *)&ip->ip_dst, sizeof(ip->ip_dst));
761 MD5_Update(&ctx, (char *)&zero_proto, sizeof(zero_proto));
762 MD5_Update(&ctx, (char *)&ip->ip_p, sizeof(ip->ip_p));
763 tlen = EXTRACT_16BITS(&ip->ip_len) - IP_HL(ip) * 4;
764 tlen = htons(tlen);
765 MD5_Update(&ctx, (char *)&tlen, sizeof(tlen));
758 MD5_Init(&ctx);
759 /*
760 * Step 1: Update MD5 hash with IP pseudo-header.
761 */
762 if (IP_V(ip) == 4) {
763 MD5_Update(&ctx, (char *)&ip->ip_src, sizeof(ip->ip_src));
764 MD5_Update(&ctx, (char *)&ip->ip_dst, sizeof(ip->ip_dst));
765 MD5_Update(&ctx, (char *)&zero_proto, sizeof(zero_proto));
766 MD5_Update(&ctx, (char *)&ip->ip_p, sizeof(ip->ip_p));
767 tlen = EXTRACT_16BITS(&ip->ip_len) - IP_HL(ip) * 4;
768 tlen = htons(tlen);
769 MD5_Update(&ctx, (char *)&tlen, sizeof(tlen));
766#ifdef INET6
770#ifdef INET6
767 } else if (IP_V(ip) == 6) {
768 ip6 = (struct ip6_hdr *)ip;
769 MD5_Update(&ctx, (char *)&ip6->ip6_src, sizeof(ip6->ip6_src));
770 MD5_Update(&ctx, (char *)&ip6->ip6_dst, sizeof(ip6->ip6_dst));
771 len32 = htonl(ntohs(ip6->ip6_plen));
772 MD5_Update(&ctx, (char *)&len32, sizeof(len32));
773 nxt = 0;
774 MD5_Update(&ctx, (char *)&nxt, sizeof(nxt));
775 MD5_Update(&ctx, (char *)&nxt, sizeof(nxt));
776 MD5_Update(&ctx, (char *)&nxt, sizeof(nxt));
777 nxt = IPPROTO_TCP;
778 MD5_Update(&ctx, (char *)&nxt, sizeof(nxt));
771 } else if (IP_V(ip) == 6) {
772 ip6 = (struct ip6_hdr *)ip;
773 MD5_Update(&ctx, (char *)&ip6->ip6_src, sizeof(ip6->ip6_src));
774 MD5_Update(&ctx, (char *)&ip6->ip6_dst, sizeof(ip6->ip6_dst));
775 len32 = htonl(ntohs(ip6->ip6_plen));
776 MD5_Update(&ctx, (char *)&len32, sizeof(len32));
777 nxt = 0;
778 MD5_Update(&ctx, (char *)&nxt, sizeof(nxt));
779 MD5_Update(&ctx, (char *)&nxt, sizeof(nxt));
780 MD5_Update(&ctx, (char *)&nxt, sizeof(nxt));
781 nxt = IPPROTO_TCP;
782 MD5_Update(&ctx, (char *)&nxt, sizeof(nxt));
779#endif
783#endif
780 } else
781 return (CANT_CHECK_SIGNATURE);
784 } else
785 return (CANT_CHECK_SIGNATURE);
782
786
783 /*
784 * Step 2: Update MD5 hash with TCP header, excluding options.
785 * The TCP checksum must be set to zero.
786 */
787 savecsum = tp1.th_sum;
788 tp1.th_sum = 0;
789 MD5_Update(&ctx, (char *)&tp1, sizeof(struct tcphdr));
790 tp1.th_sum = savecsum;
791 /*
792 * Step 3: Update MD5 hash with TCP segment data, if present.
793 */
794 if (length > 0)
795 MD5_Update(&ctx, data, length);
796 /*
797 * Step 4: Update MD5 hash with shared secret.
798 */
799 MD5_Update(&ctx, tcpmd5secret, strlen(tcpmd5secret));
800 MD5_Final(sig, &ctx);
787 /*
788 * Step 2: Update MD5 hash with TCP header, excluding options.
789 * The TCP checksum must be set to zero.
790 */
791 savecsum = tp1.th_sum;
792 tp1.th_sum = 0;
793 MD5_Update(&ctx, (char *)&tp1, sizeof(struct tcphdr));
794 tp1.th_sum = savecsum;
795 /*
796 * Step 3: Update MD5 hash with TCP segment data, if present.
797 */
798 if (length > 0)
799 MD5_Update(&ctx, data, length);
800 /*
801 * Step 4: Update MD5 hash with shared secret.
802 */
803 MD5_Update(&ctx, tcpmd5secret, strlen(tcpmd5secret));
804 MD5_Final(sig, &ctx);
801
805
802 if (memcmp(rcvsig, sig, TCP_SIGLEN) == 0)
803 return (SIGNATURE_VALID);
804 else
805 return (SIGNATURE_INVALID);
806 if (memcmp(rcvsig, sig, TCP_SIGLEN) == 0)
807 return (SIGNATURE_VALID);
808 else
809 return (SIGNATURE_INVALID);
806}
807#endif /* HAVE_LIBCRYPTO */
810}
811#endif /* HAVE_LIBCRYPTO */
812
813/*
814 * Local Variables:
815 * c-style: whitesmith
816 * c-basic-offset: 8
817 * End:
818 */