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