xref: /netbsd-src/external/bsd/tcpdump/dist/print-tcp.c (revision ba65fde2d7fefa7d39838fa5fa855e62bd606b5e)
1 /*	NetBSD: print-tcp.c,v 1.9 2007/07/26 18:15:12 plunky Exp 	*/
2 
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
11  * retain the above copyright notice and this paragraph in its entirety, (2)
12  * distributions including binary code include the above copyright notice and
13  * this paragraph in its entirety in the documentation or other materials
14  * provided with the distribution, and (3) all advertising materials mentioning
15  * features or use of this software display the following acknowledgement:
16  * ``This product includes software developed by the University of California,
17  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
18  * the University nor the names of its contributors may be used to endorse
19  * or promote products derived from this software without specific prior
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 #include <sys/cdefs.h>
27 #ifndef lint
28 #if 0
29 static const char rcsid[] _U_ =
30 "@(#) Header: /tcpdump/master/tcpdump/print-tcp.c,v 1.135 2008-11-09 23:35:03 mcr Exp (LBL)";
31 #else
32 __RCSID("$NetBSD: print-tcp.c,v 1.2 2010/12/05 05:11:31 christos Exp $");
33 #endif
34 #endif
35 
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39 
40 #include <tcpdump-stdinc.h>
41 
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 
46 #include "interface.h"
47 #include "addrtoname.h"
48 #include "extract.h"
49 
50 #include "tcp.h"
51 
52 #include "ip.h"
53 #ifdef INET6
54 #include "ip6.h"
55 #endif
56 #include "ipproto.h"
57 #include "rpc_auth.h"
58 #include "rpc_msg.h"
59 
60 #include "nameser.h"
61 
62 #ifdef HAVE_LIBCRYPTO
63 #include <openssl/md5.h>
64 #include <signature.h>
65 
66 static int tcp_verify_signature(const struct ip *ip, const struct tcphdr *tp,
67                                 const u_char *data, int length, const u_char *rcvsig);
68 #endif
69 
70 static void print_tcp_rst_data(register const u_char *sp, u_int length);
71 
72 #define MAX_RST_DATA_LEN	30
73 
74 
75 struct tha {
76 #ifndef INET6
77         struct in_addr src;
78         struct in_addr dst;
79 #else
80         struct in6_addr src;
81         struct in6_addr dst;
82 #endif /*INET6*/
83         u_int port;
84 };
85 
86 struct tcp_seq_hash {
87         struct tcp_seq_hash *nxt;
88         struct tha addr;
89         tcp_seq seq;
90         tcp_seq ack;
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 
98 static struct tcp_seq_hash tcp_seq_hash[TSEQ_HASHSIZE];
99 
100 struct 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 };
111 
112 struct 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         { TCPOPT_UTO, "uto" },
128         { 0, NULL }
129 };
130 
131 static int tcp_cksum(register const struct ip *ip,
132 		     register const struct tcphdr *tp,
133 		     register u_int len)
134 {
135         union phu {
136                 struct phdr {
137                         u_int32_t src;
138                         u_int32_t dst;
139                         u_char mbz;
140                         u_char proto;
141                         u_int16_t len;
142                 } ph;
143                 u_int16_t pa[6];
144         } phu;
145         const u_int16_t *sp;
146 
147         /* pseudo-header.. */
148         phu.ph.len = htons((u_int16_t)len);
149         phu.ph.mbz = 0;
150         phu.ph.proto = IPPROTO_TCP;
151         memcpy(&phu.ph.src, &ip->ip_src.s_addr, sizeof(u_int32_t));
152         if (IP_HL(ip) == 5)
153                 memcpy(&phu.ph.dst, &ip->ip_dst.s_addr, sizeof(u_int32_t));
154         else
155                 phu.ph.dst = ip_finddst(ip);
156 
157         sp = &phu.pa[0];
158         return in_cksum((u_short *)tp, len,
159                         sp[0]+sp[1]+sp[2]+sp[3]+sp[4]+sp[5]);
160 }
161 
162 void
163 tcp_print(register const u_char *bp, register u_int length,
164 	  register const u_char *bp2, int fragmented)
165 {
166         register const struct tcphdr *tp;
167         register const struct ip *ip;
168         register u_char flags;
169         register u_int hlen;
170         register char ch;
171         u_int16_t sport, dport, win, urp;
172         u_int32_t seq, ack, thseq, thack;
173         u_int utoval;
174         int threv;
175 #ifdef INET6
176         register const struct ip6_hdr *ip6;
177 #endif
178 
179         tp = (struct tcphdr *)bp;
180         ip = (struct ip *)bp2;
181 #ifdef INET6
182         if (IP_V(ip) == 6)
183                 ip6 = (struct ip6_hdr *)bp2;
184         else
185                 ip6 = NULL;
186 #endif /*INET6*/
187         ch = '\0';
188         if (!TTEST(tp->th_dport)) {
189                 (void)printf("%s > %s: [|tcp]",
190                              ipaddr_string(&ip->ip_src),
191                              ipaddr_string(&ip->ip_dst));
192                 return;
193         }
194 
195         sport = EXTRACT_16BITS(&tp->th_sport);
196         dport = EXTRACT_16BITS(&tp->th_dport);
197 
198         hlen = TH_OFF(tp) * 4;
199 
200         /*
201 	 * If data present, header length valid, and NFS port used,
202 	 * assume NFS.
203 	 * Pass offset of data plus 4 bytes for RPC TCP msg length
204 	 * to NFS print routines.
205 	 */
206 	if (!qflag && hlen >= sizeof(*tp) && hlen <= length &&
207 	    (length - hlen) >= 4) {
208 		u_char *fraglenp;
209 		u_int32_t fraglen;
210 		register struct sunrpc_msg *rp;
211 		enum sunrpc_msg_type direction;
212 
213 		fraglenp = (u_char *)tp + hlen;
214 		if (TTEST2(*fraglenp, 4)) {
215 			fraglen = EXTRACT_32BITS(fraglenp) & 0x7FFFFFFF;
216 			if (fraglen > (length - hlen) - 4)
217 				fraglen = (length - hlen) - 4;
218 			rp = (struct sunrpc_msg *)(fraglenp + 4);
219 			if (TTEST(rp->rm_direction)) {
220 				direction = (enum sunrpc_msg_type)EXTRACT_32BITS(&rp->rm_direction);
221 				if (dport == NFS_PORT &&
222 				    direction == SUNRPC_CALL) {
223 					nfsreq_print((u_char *)rp, fraglen,
224 					    (u_char *)ip);
225 					return;
226 				}
227 				if (sport == NFS_PORT &&
228 				    direction == SUNRPC_REPLY) {
229 					nfsreply_print((u_char *)rp, fraglen,
230 					    (u_char *)ip);
231 					return;
232 				}
233 			}
234                 }
235         }
236 #ifdef INET6
237         if (ip6) {
238                 if (ip6->ip6_nxt == IPPROTO_TCP) {
239                         (void)printf("%s.%s > %s.%s: ",
240                                      ip6addr_string(&ip6->ip6_src),
241                                      tcpport_string(sport),
242                                      ip6addr_string(&ip6->ip6_dst),
243                                      tcpport_string(dport));
244                 } else {
245                         (void)printf("%s > %s: ",
246                                      tcpport_string(sport), tcpport_string(dport));
247                 }
248         } else
249 #endif /*INET6*/
250         {
251                 if (ip->ip_p == IPPROTO_TCP) {
252                         (void)printf("%s.%s > %s.%s: ",
253                                      ipaddr_string(&ip->ip_src),
254                                      tcpport_string(sport),
255                                      ipaddr_string(&ip->ip_dst),
256                                      tcpport_string(dport));
257                 } else {
258                         (void)printf("%s > %s: ",
259                                      tcpport_string(sport), tcpport_string(dport));
260                 }
261         }
262 
263         if (hlen < sizeof(*tp)) {
264                 (void)printf(" tcp %d [bad hdr length %u - too short, < %lu]",
265                              length - hlen, hlen, (unsigned long)sizeof(*tp));
266                 return;
267         }
268 
269         TCHECK(*tp);
270 
271         seq = EXTRACT_32BITS(&tp->th_seq);
272         ack = EXTRACT_32BITS(&tp->th_ack);
273         win = EXTRACT_16BITS(&tp->th_win);
274         urp = EXTRACT_16BITS(&tp->th_urp);
275 
276         if (qflag) {
277                 (void)printf("tcp %d", length - hlen);
278                 if (hlen > length) {
279                         (void)printf(" [bad hdr length %u - too long, > %u]",
280                                      hlen, length);
281                 }
282                 return;
283         }
284 
285         flags = tp->th_flags;
286         printf("Flags [%s]", bittok2str_nosep(tcp_flag_values, "none", flags));
287 
288         if (!Sflag && (flags & TH_ACK)) {
289                 register struct tcp_seq_hash *th;
290                 const void *src, *dst;
291                 register int rev;
292                 struct tha tha;
293                 /*
294                  * Find (or record) the initial sequence numbers for
295                  * this conversation.  (we pick an arbitrary
296                  * collating order so there's only one entry for
297                  * both directions).
298                  */
299 #ifdef INET6
300                 memset(&tha, 0, sizeof(tha));
301                 rev = 0;
302                 if (ip6) {
303                         src = &ip6->ip6_src;
304                         dst = &ip6->ip6_dst;
305                         if (sport > dport)
306                                 rev = 1;
307                         else if (sport == dport) {
308                                 if (memcmp(src, dst, sizeof ip6->ip6_dst) > 0)
309                                         rev = 1;
310                         }
311                         if (rev) {
312                                 memcpy(&tha.src, dst, sizeof ip6->ip6_dst);
313                                 memcpy(&tha.dst, src, sizeof ip6->ip6_src);
314                                 tha.port = dport << 16 | sport;
315                         } else {
316                                 memcpy(&tha.dst, dst, sizeof ip6->ip6_dst);
317                                 memcpy(&tha.src, src, sizeof ip6->ip6_src);
318                                 tha.port = sport << 16 | dport;
319                         }
320                 } else {
321                         src = &ip->ip_src;
322                         dst = &ip->ip_dst;
323                         if (sport > dport)
324                                 rev = 1;
325                         else if (sport == dport) {
326                                 if (memcmp(src, dst, sizeof ip->ip_dst) > 0)
327                                         rev = 1;
328                         }
329                         if (rev) {
330                                 memcpy(&tha.src, dst, sizeof ip->ip_dst);
331                                 memcpy(&tha.dst, src, sizeof ip->ip_src);
332                                 tha.port = dport << 16 | sport;
333                         } else {
334                                 memcpy(&tha.dst, dst, sizeof ip->ip_dst);
335                                 memcpy(&tha.src, src, sizeof ip->ip_src);
336                                 tha.port = sport << 16 | dport;
337                         }
338                 }
339 #else
340                 rev = 0;
341                 src = &ip->ip_src;
342                 dst = &ip->ip_dst;
343                 if (sport > dport)
344                         rev = 1;
345                 else if (sport == dport) {
346                         if (memcmp(src, dst, sizeof ip->ip_dst) > 0)
347                                 rev = 1;
348                 }
349                 if (rev) {
350                         memcpy(&tha.src, dst, sizeof ip->ip_dst);
351                         memcpy(&tha.dst, src, sizeof ip->ip_src);
352                         tha.port = dport << 16 | sport;
353                 } else {
354                         memcpy(&tha.dst, dst, sizeof ip->ip_dst);
355                         memcpy(&tha.src, src, sizeof ip->ip_src);
356                         tha.port = sport << 16 | dport;
357                 }
358 #endif
359 
360                 threv = rev;
361                 for (th = &tcp_seq_hash[tha.port % TSEQ_HASHSIZE];
362                      th->nxt; th = th->nxt)
363                         if (memcmp((char *)&tha, (char *)&th->addr,
364                                    sizeof(th->addr)) == 0)
365                                 break;
366 
367                 if (!th->nxt || (flags & TH_SYN)) {
368                         /* didn't find it or new conversation */
369                         if (th->nxt == NULL) {
370                                 th->nxt = (struct tcp_seq_hash *)
371                                         calloc(1, sizeof(*th));
372                                 if (th->nxt == NULL)
373                                         error("tcp_print: calloc");
374                         }
375                         th->addr = tha;
376                         if (rev)
377                                 th->ack = seq, th->seq = ack - 1;
378                         else
379                                 th->seq = seq, th->ack = ack - 1;
380                 } else {
381                         if (rev)
382                                 seq -= th->ack, ack -= th->seq;
383                         else
384                                 seq -= th->seq, ack -= th->ack;
385                 }
386 
387                 thseq = th->seq;
388                 thack = th->ack;
389         } else {
390                 /*fool gcc*/
391                 thseq = thack = threv = 0;
392         }
393         if (hlen > length) {
394                 (void)printf(" [bad hdr length %u - too long, > %u]",
395                              hlen, length);
396                 return;
397         }
398 
399         if (IP_V(ip) == 4 && vflag && !Kflag && !fragmented) {
400                 u_int16_t sum, tcp_sum;
401                 if (TTEST2(tp->th_sport, length)) {
402                         sum = tcp_cksum(ip, tp, length);
403 
404                         (void)printf(", cksum 0x%04x",EXTRACT_16BITS(&tp->th_sum));
405                         if (sum != 0) {
406                                 tcp_sum = EXTRACT_16BITS(&tp->th_sum);
407                                 (void)printf(" (incorrect -> 0x%04x)",in_cksum_shouldbe(tcp_sum, sum));
408                         } else
409                                 (void)printf(" (correct)");
410                 }
411         }
412 #ifdef INET6
413         if (IP_V(ip) == 6 && ip6->ip6_plen && vflag && !Kflag && !fragmented) {
414                 u_int16_t sum,tcp_sum;
415                 if (TTEST2(tp->th_sport, length)) {
416                         sum = nextproto6_cksum(ip6, (u_short *)tp, length, IPPROTO_TCP);
417                         (void)printf(", cksum 0x%04x",EXTRACT_16BITS(&tp->th_sum));
418                         if (sum != 0) {
419                                 tcp_sum = EXTRACT_16BITS(&tp->th_sum);
420                                 (void)printf(" (incorrect -> 0x%04x)",in_cksum_shouldbe(tcp_sum, sum));
421                         } else
422                                 (void)printf(" (correct)");
423 
424                 }
425         }
426 #endif
427 
428         length -= hlen;
429         if (vflag > 1 || length > 0 || flags & (TH_SYN | TH_FIN | TH_RST)) {
430                 (void)printf(", seq %u", seq);
431 
432                 if (length > 0) {
433                         (void)printf(":%u", seq + length);
434                 }
435         }
436 
437         if (flags & TH_ACK) {
438                 (void)printf(", ack %u", ack);
439         }
440 
441         (void)printf(", win %d", win);
442 
443         if (flags & TH_URG)
444                 (void)printf(", urg %d", urp);
445         /*
446          * Handle any options.
447          */
448         if (hlen > sizeof(*tp)) {
449                 register const u_char *cp;
450                 register u_int i, opt, datalen;
451                 register u_int len;
452 
453                 hlen -= sizeof(*tp);
454                 cp = (const u_char *)tp + sizeof(*tp);
455                 printf(", options [");
456                 while (hlen > 0) {
457                         if (ch != '\0')
458                                 putchar(ch);
459                         TCHECK(*cp);
460                         opt = *cp++;
461                         if (ZEROLENOPT(opt))
462                                 len = 1;
463                         else {
464                                 TCHECK(*cp);
465                                 len = *cp++;	/* total including type, len */
466                                 if (len < 2 || len > hlen)
467                                         goto bad;
468                                 --hlen;		/* account for length byte */
469                         }
470                         --hlen;			/* account for type byte */
471                         datalen = 0;
472 
473 /* Bail if "l" bytes of data are not left or were not captured  */
474 #define LENCHECK(l) { if ((l) > hlen) goto bad; TCHECK2(*cp, l); }
475 
476 
477                         printf("%s", tok2str(tcp_option_values, "Unknown Option %u", opt));
478 
479                         switch (opt) {
480 
481                         case TCPOPT_MAXSEG:
482                                 datalen = 2;
483                                 LENCHECK(datalen);
484                                 (void)printf(" %u", EXTRACT_16BITS(cp));
485                                 break;
486 
487                         case TCPOPT_WSCALE:
488                                 datalen = 1;
489                                 LENCHECK(datalen);
490                                 (void)printf(" %u", *cp);
491                                 break;
492 
493                         case TCPOPT_SACK:
494                                 datalen = len - 2;
495                                 if (datalen % 8 != 0) {
496                                         (void)printf("malformed sack");
497                                 } else {
498                                         u_int32_t s, e;
499 
500                                         (void)printf(" %d ", datalen / 8);
501                                         for (i = 0; i < datalen; i += 8) {
502                                                 LENCHECK(i + 4);
503                                                 s = EXTRACT_32BITS(cp + i);
504                                                 LENCHECK(i + 8);
505                                                 e = EXTRACT_32BITS(cp + i + 4);
506                                                 if (threv) {
507                                                         s -= thseq;
508                                                         e -= thseq;
509                                                 } else {
510                                                         s -= thack;
511                                                         e -= thack;
512                                                 }
513                                                 (void)printf("{%u:%u}", s, e);
514                                         }
515                                 }
516                                 break;
517 
518                         case TCPOPT_CC:
519                         case TCPOPT_CCNEW:
520                         case TCPOPT_CCECHO:
521                         case TCPOPT_ECHO:
522                         case TCPOPT_ECHOREPLY:
523 
524                                 /*
525                                  * those options share their semantics.
526                                  * fall through
527                                  */
528                                 datalen = 4;
529                                 LENCHECK(datalen);
530                                 (void)printf(" %u", EXTRACT_32BITS(cp));
531                                 break;
532 
533                         case TCPOPT_TIMESTAMP:
534                                 datalen = 8;
535                                 LENCHECK(datalen);
536                                 (void)printf(" val %u ecr %u",
537                                              EXTRACT_32BITS(cp),
538                                              EXTRACT_32BITS(cp + 4));
539                                 break;
540 
541                         case TCPOPT_SIGNATURE:
542                                 datalen = TCP_SIGLEN;
543                                 LENCHECK(datalen);
544 #ifdef HAVE_LIBCRYPTO
545                                 switch (tcp_verify_signature(ip, tp,
546                                                              bp + TH_OFF(tp) * 4, length, cp)) {
547 
548                                 case SIGNATURE_VALID:
549                                         (void)printf("valid");
550                                         break;
551 
552                                 case SIGNATURE_INVALID:
553                                         (void)printf("invalid");
554                                         break;
555 
556                                 case CANT_CHECK_SIGNATURE:
557                                         (void)printf("can't check - ");
558                                         for (i = 0; i < TCP_SIGLEN; ++i)
559                                                 (void)printf("%02x", cp[i]);
560                                         break;
561                                 }
562 #else
563                                 for (i = 0; i < TCP_SIGLEN; ++i)
564                                         (void)printf("%02x", cp[i]);
565 #endif
566                                 break;
567 
568                         case TCPOPT_AUTH:
569                                 (void)printf("keyid %d", *cp++);
570                                 datalen = len - 3;
571                                 for (i = 0; i < datalen; ++i) {
572                                         LENCHECK(i);
573                                         (void)printf("%02x", cp[i]);
574                                 }
575                                 break;
576 
577 
578                         case TCPOPT_EOL:
579                         case TCPOPT_NOP:
580                         case TCPOPT_SACKOK:
581                                 /*
582                                  * Nothing interesting.
583                                  * fall through
584                                  */
585                                 break;
586 
587                         case TCPOPT_UTO:
588                                 datalen = 2;
589                                 LENCHECK(datalen);
590                                 utoval = EXTRACT_16BITS(cp);
591                                 (void)printf("0x%x", utoval);
592                                 if (utoval & 0x0001)
593                                         utoval = (utoval >> 1) * 60;
594                                 else
595                                         utoval >>= 1;
596                                 (void)printf(" %u", utoval);
597                                 break;
598 
599                         default:
600                                 datalen = len - 2;
601                                 for (i = 0; i < datalen; ++i) {
602                                         LENCHECK(i);
603                                         (void)printf("%02x", cp[i]);
604                                 }
605                                 break;
606                         }
607 
608                         /* Account for data printed */
609                         cp += datalen;
610                         hlen -= datalen;
611 
612                         /* Check specification against observed length */
613                         ++datalen;			/* option octet */
614                         if (!ZEROLENOPT(opt))
615                                 ++datalen;		/* size octet */
616                         if (datalen != len)
617                                 (void)printf("[len %d]", len);
618                         ch = ',';
619                         if (opt == TCPOPT_EOL)
620                                 break;
621                 }
622                 putchar(']');
623         }
624 
625         /*
626          * Print length field before crawling down the stack.
627          */
628         printf(", length %u", length);
629 
630         if (length <= 0)
631                 return;
632 
633         /*
634          * Decode payload if necessary.
635          */
636         bp += TH_OFF(tp) * 4;
637         if ((flags & TH_RST) && vflag) {
638                 print_tcp_rst_data(bp, length);
639                 return;
640         }
641 
642         if (sport == TELNET_PORT || dport == TELNET_PORT) {
643                 if (!qflag && vflag)
644                         telnet_print(bp, length);
645         } else if (sport == BGP_PORT || dport == BGP_PORT)
646                 bgp_print(bp, length);
647         else if (sport == PPTP_PORT || dport == PPTP_PORT)
648                 pptp_print(bp);
649 #ifdef TCPDUMP_DO_SMB
650         else if (sport == NETBIOS_SSN_PORT || dport == NETBIOS_SSN_PORT)
651                 nbt_tcp_print(bp, length);
652 	else if (sport == SMB_PORT || dport == SMB_PORT)
653 		smb_tcp_print(bp, length);
654 #endif
655         else if (sport == BEEP_PORT || dport == BEEP_PORT)
656                 beep_print(bp, length);
657         else if (length > 2 &&
658                  (sport == NAMESERVER_PORT || dport == NAMESERVER_PORT ||
659                   sport == MULTICASTDNS_PORT || dport == MULTICASTDNS_PORT)) {
660                 /*
661                  * TCP DNS query has 2byte length at the head.
662                  * XXX packet could be unaligned, it can go strange
663                  */
664                 ns_print(bp + 2, length - 2, 0);
665         } else if (sport == MSDP_PORT || dport == MSDP_PORT) {
666                 msdp_print(bp, length);
667         }
668         else if (length > 0 && (sport == LDP_PORT || dport == LDP_PORT)) {
669                 ldp_print(bp, length);
670         }
671 
672         return;
673  bad:
674         fputs("[bad opt]", stdout);
675         if (ch != '\0')
676                 putchar('>');
677         return;
678  trunc:
679         fputs("[|tcp]", stdout);
680         if (ch != '\0')
681                 putchar('>');
682 }
683 
684 /*
685  * RFC1122 says the following on data in RST segments:
686  *
687  *         4.2.2.12  RST Segment: RFC-793 Section 3.4
688  *
689  *            A TCP SHOULD allow a received RST segment to include data.
690  *
691  *            DISCUSSION
692  *                 It has been suggested that a RST segment could contain
693  *                 ASCII text that encoded and explained the cause of the
694  *                 RST.  No standard has yet been established for such
695  *                 data.
696  *
697  */
698 
699 static void
700 print_tcp_rst_data(register const u_char *sp, u_int length)
701 {
702         int c;
703 
704         if (TTEST2(*sp, length))
705                 printf(" [RST");
706         else
707                 printf(" [!RST");
708         if (length > MAX_RST_DATA_LEN) {
709                 length = MAX_RST_DATA_LEN;	/* can use -X for longer */
710                 putchar('+');			/* indicate we truncate */
711         }
712         putchar(' ');
713         while (length-- && sp <= snapend) {
714                 c = *sp++;
715                 safeputchar(c);
716         }
717         putchar(']');
718 }
719 
720 #ifdef HAVE_LIBCRYPTO
721 static int
722 tcp_verify_signature(const struct ip *ip, const struct tcphdr *tp,
723                      const u_char *data, int length, const u_char *rcvsig)
724 {
725         struct tcphdr tp1;
726         u_char sig[TCP_SIGLEN];
727         char zero_proto = 0;
728         MD5_CTX ctx;
729         u_int16_t savecsum, tlen;
730 #ifdef INET6
731         struct ip6_hdr *ip6;
732         u_int32_t len32;
733         u_int8_t nxt;
734 #endif
735 
736 	if (data + length > snapend) {
737 		printf("snaplen too short, ");
738 		return (CANT_CHECK_SIGNATURE);
739 	}
740 
741         tp1 = *tp;
742 
743         if (sigsecret == NULL) {
744 		printf("shared secret not supplied with -M, ");
745                 return (CANT_CHECK_SIGNATURE);
746         }
747 
748         MD5_Init(&ctx);
749         /*
750          * Step 1: Update MD5 hash with IP pseudo-header.
751          */
752         if (IP_V(ip) == 4) {
753                 MD5_Update(&ctx, (char *)&ip->ip_src, sizeof(ip->ip_src));
754                 MD5_Update(&ctx, (char *)&ip->ip_dst, sizeof(ip->ip_dst));
755                 MD5_Update(&ctx, (char *)&zero_proto, sizeof(zero_proto));
756                 MD5_Update(&ctx, (char *)&ip->ip_p, sizeof(ip->ip_p));
757                 tlen = EXTRACT_16BITS(&ip->ip_len) - IP_HL(ip) * 4;
758                 tlen = htons(tlen);
759                 MD5_Update(&ctx, (char *)&tlen, sizeof(tlen));
760 #ifdef INET6
761         } else if (IP_V(ip) == 6) {
762                 ip6 = (struct ip6_hdr *)ip;
763                 MD5_Update(&ctx, (char *)&ip6->ip6_src, sizeof(ip6->ip6_src));
764                 MD5_Update(&ctx, (char *)&ip6->ip6_dst, sizeof(ip6->ip6_dst));
765                 len32 = htonl(EXTRACT_16BITS(&ip6->ip6_plen));
766                 MD5_Update(&ctx, (char *)&len32, sizeof(len32));
767                 nxt = 0;
768                 MD5_Update(&ctx, (char *)&nxt, sizeof(nxt));
769                 MD5_Update(&ctx, (char *)&nxt, sizeof(nxt));
770                 MD5_Update(&ctx, (char *)&nxt, sizeof(nxt));
771                 nxt = IPPROTO_TCP;
772                 MD5_Update(&ctx, (char *)&nxt, sizeof(nxt));
773 #endif
774         } else {
775 #ifdef INET6
776 		printf("IP version not 4 or 6, ");
777 #else
778 		printf("IP version not 4, ");
779 #endif
780                 return (CANT_CHECK_SIGNATURE);
781         }
782 
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, sigsecret, strlen(sigsecret));
800         MD5_Final(sig, &ctx);
801 
802         if (memcmp(rcvsig, sig, TCP_SIGLEN) == 0)
803                 return (SIGNATURE_VALID);
804         else
805                 return (SIGNATURE_INVALID);
806 }
807 #endif /* HAVE_LIBCRYPTO */
808 
809 /*
810  * Local Variables:
811  * c-style: whitesmith
812  * c-basic-offset: 8
813  * End:
814  */
815