print-tcp.c revision 236192
15992SN/A/*	$NetBSD: print-tcp.c,v 1.9 2007/07/26 18:15:12 plunky Exp $	*/
25992SN/A
35992SN/A/*
45992SN/A * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
55992SN/A *	The Regents of the University of California.  All rights reserved.
65992SN/A *
75992SN/A * Copyright (c) 1999-2004 The tcpdump.org project
85992SN/A *
95992SN/A * Redistribution and use in source and binary forms, with or without
105992SN/A * modification, are permitted provided that: (1) source code distributions
115992SN/A * retain the above copyright notice and this paragraph in its entirety, (2)
125992SN/A * distributions including binary code include the above copyright notice and
135992SN/A * this paragraph in its entirety in the documentation or other materials
145992SN/A * provided with the distribution, and (3) all advertising materials mentioning
155992SN/A * features or use of this software display the following acknowledgement:
165992SN/A * ``This product includes software developed by the University of California,
175992SN/A * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
185992SN/A * the University nor the names of its contributors may be used to endorse
195992SN/A * or promote products derived from this software without specific prior
205992SN/A * written permission.
215992SN/A * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
225992SN/A * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
235992SN/A * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
245992SN/A */
255992SN/A
265992SN/A#ifndef lint
275992SN/Astatic const char rcsid[] _U_ =
285992SN/A"@(#) $Header: /tcpdump/master/tcpdump/print-tcp.c,v 1.135 2008-11-09 23:35:03 mcr Exp $ (LBL)";
295992SN/A#else
305992SN/A__RCSID("$NetBSD: print-tcp.c,v 1.8 2007/07/24 11:53:48 drochner Exp $");
315992SN/A#endif
325992SN/A
335992SN/A#ifdef HAVE_CONFIG_H
345992SN/A#include "config.h"
355992SN/A#endif
365992SN/A
375992SN/A#include <tcpdump-stdinc.h>
385992SN/A
395992SN/A#include <stdio.h>
405992SN/A#include <stdlib.h>
415992SN/A#include <string.h>
425992SN/A
435992SN/A#include "interface.h"
445992SN/A#include "addrtoname.h"
455992SN/A#include "extract.h"
465992SN/A
475992SN/A#include "tcp.h"
485992SN/A
495992SN/A#include "ip.h"
505992SN/A#ifdef INET6
515992SN/A#include "ip6.h"
525992SN/A#endif
535992SN/A#include "ipproto.h"
545992SN/A#include "rpc_auth.h"
555992SN/A#include "rpc_msg.h"
565992SN/A
575992SN/A#include "nameser.h"
585992SN/A
595992SN/A#ifdef HAVE_LIBCRYPTO
605992SN/A#include <openssl/md5.h>
615992SN/A#include <signature.h>
625992SN/A
635992SN/Astatic int tcp_verify_signature(const struct ip *ip, const struct tcphdr *tp,
645992SN/A                                const u_char *data, int length, const u_char *rcvsig);
655992SN/A#endif
665992SN/A
675992SN/Astatic void print_tcp_rst_data(register const u_char *sp, u_int length);
685992SN/A
695992SN/A#define MAX_RST_DATA_LEN	30
705992SN/A
715992SN/A
725992SN/Astruct tha {
735992SN/A#ifndef INET6
745992SN/A        struct in_addr src;
755992SN/A        struct in_addr dst;
765992SN/A#else
775992SN/A        struct in6_addr src;
785992SN/A        struct in6_addr dst;
795992SN/A#endif /*INET6*/
805992SN/A        u_int port;
815992SN/A};
825992SN/A
835992SN/Astruct tcp_seq_hash {
845992SN/A        struct tcp_seq_hash *nxt;
855992SN/A        struct tha addr;
867084SN/A        tcp_seq seq;
877084SN/A        tcp_seq ack;
887084SN/A};
897084SN/A
905992SN/A#define TSEQ_HASHSIZE 919
915992SN/A
925992SN/A/* These tcp optinos do not have the size octet */
935992SN/A#define ZEROLENOPT(o) ((o) == TCPOPT_EOL || (o) == TCPOPT_NOP)
945992SN/A
955992SN/Astatic struct tcp_seq_hash tcp_seq_hash[TSEQ_HASHSIZE];
965992SN/A
975992SN/Astruct tok tcp_flag_values[] = {
985992SN/A        { TH_FIN, "F" },
995992SN/A        { TH_SYN, "S" },
1005992SN/A        { TH_RST, "R" },
1015992SN/A        { TH_PUSH, "P" },
1025992SN/A        { TH_ACK, "." },
1035992SN/A        { TH_URG, "U" },
1045992SN/A        { TH_ECNECHO, "E" },
1057084SN/A        { TH_CWR, "W" },
1065992SN/A        { 0, NULL }
1075992SN/A};
1085992SN/A
1095992SN/Astruct tok tcp_option_values[] = {
1107084SN/A        { TCPOPT_EOL, "eol" },
1115992SN/A        { TCPOPT_NOP, "nop" },
1125992SN/A        { TCPOPT_MAXSEG, "mss" },
113        { TCPOPT_WSCALE, "wscale" },
114        { TCPOPT_SACKOK, "sackOK" },
115        { TCPOPT_SACK, "sack" },
116        { TCPOPT_ECHO, "echo" },
117        { TCPOPT_ECHOREPLY, "echoreply" },
118        { TCPOPT_TIMESTAMP, "TS" },
119        { TCPOPT_CC, "cc" },
120        { TCPOPT_CCNEW, "ccnew" },
121        { TCPOPT_CCECHO, "" },
122        { TCPOPT_SIGNATURE, "md5" },
123        { TCPOPT_AUTH, "enhanced auth" },
124        { TCPOPT_UTO, "uto" },
125        { 0, NULL }
126};
127
128static int tcp_cksum(register const struct ip *ip,
129		     register const struct tcphdr *tp,
130		     register u_int len)
131{
132	return (nextproto4_cksum(ip, (const u_int8_t *)tp, len,
133	    IPPROTO_TCP));
134}
135
136void
137tcp_print(register const u_char *bp, register u_int length,
138	  register const u_char *bp2, int fragmented)
139{
140        register const struct tcphdr *tp;
141        register const struct ip *ip;
142        register u_char flags;
143        register u_int hlen;
144        register char ch;
145        u_int16_t sport, dport, win, urp;
146        u_int32_t seq, ack, thseq, thack;
147        u_int utoval;
148        int threv;
149#ifdef INET6
150        register const struct ip6_hdr *ip6;
151#endif
152
153        tp = (struct tcphdr *)bp;
154        ip = (struct ip *)bp2;
155#ifdef INET6
156        if (IP_V(ip) == 6)
157                ip6 = (struct ip6_hdr *)bp2;
158        else
159                ip6 = NULL;
160#endif /*INET6*/
161        ch = '\0';
162        if (!TTEST(tp->th_dport)) {
163                (void)printf("%s > %s: [|tcp]",
164                             ipaddr_string(&ip->ip_src),
165                             ipaddr_string(&ip->ip_dst));
166                return;
167        }
168
169        sport = EXTRACT_16BITS(&tp->th_sport);
170        dport = EXTRACT_16BITS(&tp->th_dport);
171
172        hlen = TH_OFF(tp) * 4;
173
174        /*
175	 * If data present, header length valid, and NFS port used,
176	 * assume NFS.
177	 * Pass offset of data plus 4 bytes for RPC TCP msg length
178	 * to NFS print routines.
179	 */
180	if (!qflag && hlen >= sizeof(*tp) && hlen <= length &&
181	    (length - hlen) >= 4) {
182		u_char *fraglenp;
183		u_int32_t fraglen;
184		register struct sunrpc_msg *rp;
185		enum sunrpc_msg_type direction;
186
187		fraglenp = (u_char *)tp + hlen;
188		if (TTEST2(*fraglenp, 4)) {
189			fraglen = EXTRACT_32BITS(fraglenp) & 0x7FFFFFFF;
190			if (fraglen > (length - hlen) - 4)
191				fraglen = (length - hlen) - 4;
192			rp = (struct sunrpc_msg *)(fraglenp + 4);
193			if (TTEST(rp->rm_direction)) {
194				direction = (enum sunrpc_msg_type)EXTRACT_32BITS(&rp->rm_direction);
195				if (dport == NFS_PORT &&
196				    direction == SUNRPC_CALL) {
197					nfsreq_print((u_char *)rp, fraglen,
198					    (u_char *)ip);
199					return;
200				}
201				if (sport == NFS_PORT &&
202				    direction == SUNRPC_REPLY) {
203					nfsreply_print((u_char *)rp, fraglen,
204					    (u_char *)ip);
205					return;
206				}
207			}
208                }
209        }
210#ifdef INET6
211        if (ip6) {
212                if (ip6->ip6_nxt == IPPROTO_TCP) {
213                        (void)printf("%s.%s > %s.%s: ",
214                                     ip6addr_string(&ip6->ip6_src),
215                                     tcpport_string(sport),
216                                     ip6addr_string(&ip6->ip6_dst),
217                                     tcpport_string(dport));
218                } else {
219                        (void)printf("%s > %s: ",
220                                     tcpport_string(sport), tcpport_string(dport));
221                }
222        } else
223#endif /*INET6*/
224        {
225                if (ip->ip_p == IPPROTO_TCP) {
226                        (void)printf("%s.%s > %s.%s: ",
227                                     ipaddr_string(&ip->ip_src),
228                                     tcpport_string(sport),
229                                     ipaddr_string(&ip->ip_dst),
230                                     tcpport_string(dport));
231                } else {
232                        (void)printf("%s > %s: ",
233                                     tcpport_string(sport), tcpport_string(dport));
234                }
235        }
236
237        if (hlen < sizeof(*tp)) {
238                (void)printf(" tcp %d [bad hdr length %u - too short, < %lu]",
239                             length - hlen, hlen, (unsigned long)sizeof(*tp));
240                return;
241        }
242
243        TCHECK(*tp);
244
245        seq = EXTRACT_32BITS(&tp->th_seq);
246        ack = EXTRACT_32BITS(&tp->th_ack);
247        win = EXTRACT_16BITS(&tp->th_win);
248        urp = EXTRACT_16BITS(&tp->th_urp);
249
250        if (qflag) {
251                (void)printf("tcp %d", length - hlen);
252                if (hlen > length) {
253                        (void)printf(" [bad hdr length %u - too long, > %u]",
254                                     hlen, length);
255                }
256                return;
257        }
258
259        flags = tp->th_flags;
260        printf("Flags [%s]", bittok2str_nosep(tcp_flag_values, "none", flags));
261
262        if (!Sflag && (flags & TH_ACK)) {
263                register struct tcp_seq_hash *th;
264                const void *src, *dst;
265                register int rev;
266                struct tha tha;
267                /*
268                 * Find (or record) the initial sequence numbers for
269                 * this conversation.  (we pick an arbitrary
270                 * collating order so there's only one entry for
271                 * both directions).
272                 */
273#ifdef INET6
274                rev = 0;
275                if (ip6) {
276                        src = &ip6->ip6_src;
277                        dst = &ip6->ip6_dst;
278                        if (sport > dport)
279                                rev = 1;
280                        else if (sport == dport) {
281                                if (memcmp(src, dst, sizeof ip6->ip6_dst) > 0)
282                                        rev = 1;
283                        }
284                        if (rev) {
285                                memcpy(&tha.src, dst, sizeof ip6->ip6_dst);
286                                memcpy(&tha.dst, src, sizeof ip6->ip6_src);
287                                tha.port = dport << 16 | sport;
288                        } else {
289                                memcpy(&tha.dst, dst, sizeof ip6->ip6_dst);
290                                memcpy(&tha.src, src, sizeof ip6->ip6_src);
291                                tha.port = sport << 16 | dport;
292                        }
293                } else {
294                        /*
295                         * Zero out the tha structure; the src and dst
296                         * fields are big enough to hold an IPv6
297                         * address, but we only have IPv4 addresses
298                         * and thus must clear out the remaining 124
299                         * bits.
300                         *
301                         * XXX - should we just clear those bytes after
302                         * copying the IPv4 addresses, rather than
303                         * zeroing out the entire structure and then
304                         * overwriting some of the zeroes?
305                         *
306                         * XXX - this could fail if we see TCP packets
307                         * with an IPv6 address with the lower 124 bits
308                         * all zero and also see TCP packes with an
309                         * IPv4 address with the same 32 bits as the
310                         * upper 32 bits of the IPv6 address in question.
311                         * Can that happen?  Is it likely enough to be
312                         * an issue?
313                         */
314                        memset(&tha, 0, sizeof(tha));
315                        src = &ip->ip_src;
316                        dst = &ip->ip_dst;
317                        if (sport > dport)
318                                rev = 1;
319                        else if (sport == dport) {
320                                if (memcmp(src, dst, sizeof ip->ip_dst) > 0)
321                                        rev = 1;
322                        }
323                        if (rev) {
324                                memcpy(&tha.src, dst, sizeof ip->ip_dst);
325                                memcpy(&tha.dst, src, sizeof ip->ip_src);
326                                tha.port = dport << 16 | sport;
327                        } else {
328                                memcpy(&tha.dst, dst, sizeof ip->ip_dst);
329                                memcpy(&tha.src, src, sizeof ip->ip_src);
330                                tha.port = sport << 16 | dport;
331                        }
332                }
333#else
334                rev = 0;
335                src = &ip->ip_src;
336                dst = &ip->ip_dst;
337                if (sport > dport)
338                        rev = 1;
339                else if (sport == dport) {
340                        if (memcmp(src, dst, sizeof ip->ip_dst) > 0)
341                                rev = 1;
342                }
343                if (rev) {
344                        memcpy(&tha.src, dst, sizeof ip->ip_dst);
345                        memcpy(&tha.dst, src, sizeof ip->ip_src);
346                        tha.port = dport << 16 | sport;
347                } else {
348                        memcpy(&tha.dst, dst, sizeof ip->ip_dst);
349                        memcpy(&tha.src, src, sizeof ip->ip_src);
350                        tha.port = sport << 16 | dport;
351                }
352#endif
353
354                threv = rev;
355                for (th = &tcp_seq_hash[tha.port % TSEQ_HASHSIZE];
356                     th->nxt; th = th->nxt)
357                        if (memcmp((char *)&tha, (char *)&th->addr,
358                                   sizeof(th->addr)) == 0)
359                                break;
360
361                if (!th->nxt || (flags & TH_SYN)) {
362                        /* didn't find it or new conversation */
363                        if (th->nxt == NULL) {
364                                th->nxt = (struct tcp_seq_hash *)
365                                        calloc(1, sizeof(*th));
366                                if (th->nxt == NULL)
367                                        error("tcp_print: calloc");
368                        }
369                        th->addr = tha;
370                        if (rev)
371                                th->ack = seq, th->seq = ack - 1;
372                        else
373                                th->seq = seq, th->ack = ack - 1;
374                } else {
375                        if (rev)
376                                seq -= th->ack, ack -= th->seq;
377                        else
378                                seq -= th->seq, ack -= th->ack;
379                }
380
381                thseq = th->seq;
382                thack = th->ack;
383        } else {
384                /*fool gcc*/
385                thseq = thack = threv = 0;
386        }
387        if (hlen > length) {
388                (void)printf(" [bad hdr length %u - too long, > %u]",
389                             hlen, length);
390                return;
391        }
392
393        if (vflag && !Kflag && !fragmented) {
394                /* Check the checksum, if possible. */
395                u_int16_t sum, tcp_sum;
396
397                if (IP_V(ip) == 4) {
398                        if (TTEST2(tp->th_sport, length)) {
399                                sum = tcp_cksum(ip, tp, length);
400                                tcp_sum = EXTRACT_16BITS(&tp->th_sum);
401
402                                (void)printf(", cksum 0x%04x", tcp_sum);
403                                if (sum != 0)
404                                        (void)printf(" (incorrect -> 0x%04x)",
405                                            in_cksum_shouldbe(tcp_sum, sum));
406                                else
407                                        (void)printf(" (correct)");
408                        }
409                }
410#ifdef INET6
411                else if (IP_V(ip) == 6 && ip6->ip6_plen) {
412                        if (TTEST2(tp->th_sport, length)) {
413                                sum = nextproto6_cksum(ip6, (const u_int8_t *)tp, length, IPPROTO_TCP);
414                                tcp_sum = EXTRACT_16BITS(&tp->th_sum);
415
416                                (void)printf(", cksum 0x%04x", tcp_sum);
417                                if (sum != 0)
418                                        (void)printf(" (incorrect -> 0x%04x)",
419                                            in_cksum_shouldbe(tcp_sum, sum));
420                                else
421                                        (void)printf(" (correct)");
422
423                        }
424                }
425#endif
426        }
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        } else if (sport == RPKI_RTR_PORT || dport == RPKI_RTR_PORT) {
668                rpki_rtr_print(bp, length);
669        }
670        else if (length > 0 && (sport == LDP_PORT || dport == LDP_PORT)) {
671                ldp_print(bp, length);
672        }
673
674        return;
675 bad:
676        fputs("[bad opt]", stdout);
677        if (ch != '\0')
678                putchar('>');
679        return;
680 trunc:
681        fputs("[|tcp]", stdout);
682        if (ch != '\0')
683                putchar('>');
684}
685
686/*
687 * RFC1122 says the following on data in RST segments:
688 *
689 *         4.2.2.12  RST Segment: RFC-793 Section 3.4
690 *
691 *            A TCP SHOULD allow a received RST segment to include data.
692 *
693 *            DISCUSSION
694 *                 It has been suggested that a RST segment could contain
695 *                 ASCII text that encoded and explained the cause of the
696 *                 RST.  No standard has yet been established for such
697 *                 data.
698 *
699 */
700
701static void
702print_tcp_rst_data(register const u_char *sp, u_int length)
703{
704        int c;
705
706        if (TTEST2(*sp, length))
707                printf(" [RST");
708        else
709                printf(" [!RST");
710        if (length > MAX_RST_DATA_LEN) {
711                length = MAX_RST_DATA_LEN;	/* can use -X for longer */
712                putchar('+');			/* indicate we truncate */
713        }
714        putchar(' ');
715        while (length-- && sp <= snapend) {
716                c = *sp++;
717                safeputchar(c);
718        }
719        putchar(']');
720}
721
722#ifdef HAVE_LIBCRYPTO
723static int
724tcp_verify_signature(const struct ip *ip, const struct tcphdr *tp,
725                     const u_char *data, int length, const u_char *rcvsig)
726{
727        struct tcphdr tp1;
728        u_char sig[TCP_SIGLEN];
729        char zero_proto = 0;
730        MD5_CTX ctx;
731        u_int16_t savecsum, tlen;
732#ifdef INET6
733        struct ip6_hdr *ip6;
734        u_int32_t len32;
735        u_int8_t nxt;
736#endif
737
738	if (data + length > snapend) {
739		printf("snaplen too short, ");
740		return (CANT_CHECK_SIGNATURE);
741	}
742
743        tp1 = *tp;
744
745        if (sigsecret == NULL) {
746		printf("shared secret not supplied with -M, ");
747                return (CANT_CHECK_SIGNATURE);
748        }
749
750        MD5_Init(&ctx);
751        /*
752         * Step 1: Update MD5 hash with IP pseudo-header.
753         */
754        if (IP_V(ip) == 4) {
755                MD5_Update(&ctx, (char *)&ip->ip_src, sizeof(ip->ip_src));
756                MD5_Update(&ctx, (char *)&ip->ip_dst, sizeof(ip->ip_dst));
757                MD5_Update(&ctx, (char *)&zero_proto, sizeof(zero_proto));
758                MD5_Update(&ctx, (char *)&ip->ip_p, sizeof(ip->ip_p));
759                tlen = EXTRACT_16BITS(&ip->ip_len) - IP_HL(ip) * 4;
760                tlen = htons(tlen);
761                MD5_Update(&ctx, (char *)&tlen, sizeof(tlen));
762#ifdef INET6
763        } else if (IP_V(ip) == 6) {
764                ip6 = (struct ip6_hdr *)ip;
765                MD5_Update(&ctx, (char *)&ip6->ip6_src, sizeof(ip6->ip6_src));
766                MD5_Update(&ctx, (char *)&ip6->ip6_dst, sizeof(ip6->ip6_dst));
767                len32 = htonl(EXTRACT_16BITS(&ip6->ip6_plen));
768                MD5_Update(&ctx, (char *)&len32, sizeof(len32));
769                nxt = 0;
770                MD5_Update(&ctx, (char *)&nxt, sizeof(nxt));
771                MD5_Update(&ctx, (char *)&nxt, sizeof(nxt));
772                MD5_Update(&ctx, (char *)&nxt, sizeof(nxt));
773                nxt = IPPROTO_TCP;
774                MD5_Update(&ctx, (char *)&nxt, sizeof(nxt));
775#endif
776        } else {
777#ifdef INET6
778		printf("IP version not 4 or 6, ");
779#else
780		printf("IP version not 4, ");
781#endif
782                return (CANT_CHECK_SIGNATURE);
783        }
784
785        /*
786         * Step 2: Update MD5 hash with TCP header, excluding options.
787         * The TCP checksum must be set to zero.
788         */
789        savecsum = tp1.th_sum;
790        tp1.th_sum = 0;
791        MD5_Update(&ctx, (char *)&tp1, sizeof(struct tcphdr));
792        tp1.th_sum = savecsum;
793        /*
794         * Step 3: Update MD5 hash with TCP segment data, if present.
795         */
796        if (length > 0)
797                MD5_Update(&ctx, data, length);
798        /*
799         * Step 4: Update MD5 hash with shared secret.
800         */
801        MD5_Update(&ctx, sigsecret, strlen(sigsecret));
802        MD5_Final(sig, &ctx);
803
804        if (memcmp(rcvsig, sig, TCP_SIGLEN) == 0)
805                return (SIGNATURE_VALID);
806        else
807                return (SIGNATURE_INVALID);
808}
809#endif /* HAVE_LIBCRYPTO */
810
811/*
812 * Local Variables:
813 * c-style: whitesmith
814 * c-basic-offset: 8
815 * End:
816 */
817