]> The Tcpdump Group git mirrors - tcpdump/blob - print-tcp.c
NFLOG: fix error message
[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.135 2008-11-09 23:35:03 mcr 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 #include <signature.h>
62
63 static int tcp_verify_signature(const struct ip *ip, const struct tcphdr *tp,
64 const u_char *data, int length, const u_char *rcvsig);
65 #endif
66
67 static void print_tcp_rst_data(register const u_char *sp, u_int length);
68
69 #define MAX_RST_DATA_LEN 30
70
71
72 struct tha {
73 struct in_addr src;
74 struct in_addr dst;
75 u_int port;
76 };
77
78 struct tcp_seq_hash {
79 struct tcp_seq_hash *nxt;
80 struct tha addr;
81 tcp_seq seq;
82 tcp_seq ack;
83 };
84
85 #ifdef INET6
86 struct tha6 {
87 struct in6_addr src;
88 struct in6_addr dst;
89 u_int port;
90 };
91
92 struct tcp_seq_hash6 {
93 struct tcp_seq_hash6 *nxt;
94 struct tha6 addr;
95 tcp_seq seq;
96 tcp_seq ack;
97 };
98 #endif
99
100 #define TSEQ_HASHSIZE 919
101
102 /* These tcp optinos do not have the size octet */
103 #define ZEROLENOPT(o) ((o) == TCPOPT_EOL || (o) == TCPOPT_NOP)
104
105 static struct tcp_seq_hash tcp_seq_hash4[TSEQ_HASHSIZE];
106 #ifdef INET6
107 static struct tcp_seq_hash6 tcp_seq_hash6[TSEQ_HASHSIZE];
108 #endif
109
110 static const struct tok tcp_flag_values[] = {
111 { TH_FIN, "F" },
112 { TH_SYN, "S" },
113 { TH_RST, "R" },
114 { TH_PUSH, "P" },
115 { TH_ACK, "." },
116 { TH_URG, "U" },
117 { TH_ECNECHO, "E" },
118 { TH_CWR, "W" },
119 { 0, NULL }
120 };
121
122 static const struct tok tcp_option_values[] = {
123 { TCPOPT_EOL, "eol" },
124 { TCPOPT_NOP, "nop" },
125 { TCPOPT_MAXSEG, "mss" },
126 { TCPOPT_WSCALE, "wscale" },
127 { TCPOPT_SACKOK, "sackOK" },
128 { TCPOPT_SACK, "sack" },
129 { TCPOPT_ECHO, "echo" },
130 { TCPOPT_ECHOREPLY, "echoreply" },
131 { TCPOPT_TIMESTAMP, "TS" },
132 { TCPOPT_CC, "cc" },
133 { TCPOPT_CCNEW, "ccnew" },
134 { TCPOPT_CCECHO, "" },
135 { TCPOPT_SIGNATURE, "md5" },
136 { TCPOPT_AUTH, "enhanced auth" },
137 { TCPOPT_UTO, "uto" },
138 { TCPOPT_MPTCP, "mptcp" },
139 { TCPOPT_EXPERIMENT2, "exp" },
140 { 0, NULL }
141 };
142
143 static int tcp_cksum(register const struct ip *ip,
144 register const struct tcphdr *tp,
145 register u_int len)
146 {
147 return (nextproto4_cksum(ip, (const u_int8_t *)tp, len,
148 IPPROTO_TCP));
149 }
150
151 void
152 tcp_print(register const u_char *bp, register u_int length,
153 register const u_char *bp2, int fragmented)
154 {
155 register const struct tcphdr *tp;
156 register const struct ip *ip;
157 register u_char flags;
158 register u_int hlen;
159 register char ch;
160 u_int16_t sport, dport, win, urp;
161 u_int32_t seq, ack, thseq, thack;
162 u_int utoval;
163 u_int16_t magic;
164 register int rev;
165 #ifdef INET6
166 register const struct ip6_hdr *ip6;
167 #endif
168
169 tp = (struct tcphdr *)bp;
170 ip = (struct ip *)bp2;
171 #ifdef INET6
172 if (IP_V(ip) == 6)
173 ip6 = (struct ip6_hdr *)bp2;
174 else
175 ip6 = NULL;
176 #endif /*INET6*/
177 ch = '\0';
178 if (!TTEST(tp->th_dport)) {
179 (void)printf("%s > %s: [|tcp]",
180 ipaddr_string(&ip->ip_src),
181 ipaddr_string(&ip->ip_dst));
182 return;
183 }
184
185 sport = EXTRACT_16BITS(&tp->th_sport);
186 dport = EXTRACT_16BITS(&tp->th_dport);
187
188 hlen = TH_OFF(tp) * 4;
189
190 /*
191 * If data present, header length valid, and NFS port used,
192 * assume NFS.
193 * Pass offset of data plus 4 bytes for RPC TCP msg length
194 * to NFS print routines.
195 */
196 if (!qflag && hlen >= sizeof(*tp) && hlen <= length &&
197 (length - hlen) >= 4) {
198 u_char *fraglenp;
199 u_int32_t fraglen;
200 register struct sunrpc_msg *rp;
201 enum sunrpc_msg_type direction;
202
203 fraglenp = (u_char *)tp + hlen;
204 if (TTEST2(*fraglenp, 4)) {
205 fraglen = EXTRACT_32BITS(fraglenp) & 0x7FFFFFFF;
206 if (fraglen > (length - hlen) - 4)
207 fraglen = (length - hlen) - 4;
208 rp = (struct sunrpc_msg *)(fraglenp + 4);
209 if (TTEST(rp->rm_direction)) {
210 direction = (enum sunrpc_msg_type)EXTRACT_32BITS(&rp->rm_direction);
211 if (dport == NFS_PORT &&
212 direction == SUNRPC_CALL) {
213 nfsreq_print((u_char *)rp, fraglen,
214 (u_char *)ip);
215 return;
216 }
217 if (sport == NFS_PORT &&
218 direction == SUNRPC_REPLY) {
219 nfsreply_print((u_char *)rp, fraglen,
220 (u_char *)ip);
221 return;
222 }
223 }
224 }
225 }
226 #ifdef INET6
227 if (ip6) {
228 if (ip6->ip6_nxt == IPPROTO_TCP) {
229 (void)printf("%s.%s > %s.%s: ",
230 ip6addr_string(&ip6->ip6_src),
231 tcpport_string(sport),
232 ip6addr_string(&ip6->ip6_dst),
233 tcpport_string(dport));
234 } else {
235 (void)printf("%s > %s: ",
236 tcpport_string(sport), tcpport_string(dport));
237 }
238 } else
239 #endif /*INET6*/
240 {
241 if (ip->ip_p == IPPROTO_TCP) {
242 (void)printf("%s.%s > %s.%s: ",
243 ipaddr_string(&ip->ip_src),
244 tcpport_string(sport),
245 ipaddr_string(&ip->ip_dst),
246 tcpport_string(dport));
247 } else {
248 (void)printf("%s > %s: ",
249 tcpport_string(sport), tcpport_string(dport));
250 }
251 }
252
253 if (hlen < sizeof(*tp)) {
254 (void)printf(" tcp %d [bad hdr length %u - too short, < %lu]",
255 length - hlen, hlen, (unsigned long)sizeof(*tp));
256 return;
257 }
258
259 TCHECK(*tp);
260
261 seq = EXTRACT_32BITS(&tp->th_seq);
262 ack = EXTRACT_32BITS(&tp->th_ack);
263 win = EXTRACT_16BITS(&tp->th_win);
264 urp = EXTRACT_16BITS(&tp->th_urp);
265
266 if (qflag) {
267 (void)printf("tcp %d", length - hlen);
268 if (hlen > length) {
269 (void)printf(" [bad hdr length %u - too long, > %u]",
270 hlen, length);
271 }
272 return;
273 }
274
275 flags = tp->th_flags;
276 printf("Flags [%s]", bittok2str_nosep(tcp_flag_values, "none", flags));
277
278 if (!Sflag && (flags & TH_ACK)) {
279 /*
280 * Find (or record) the initial sequence numbers for
281 * this conversation. (we pick an arbitrary
282 * collating order so there's only one entry for
283 * both directions).
284 */
285 rev = 0;
286 #ifdef INET6
287 if (ip6) {
288 register struct tcp_seq_hash6 *th;
289 struct tcp_seq_hash6 *tcp_seq_hash;
290 const struct in6_addr *src, *dst;
291 struct tha6 tha;
292
293 tcp_seq_hash = tcp_seq_hash6;
294 src = &ip6->ip6_src;
295 dst = &ip6->ip6_dst;
296 if (sport > dport)
297 rev = 1;
298 else if (sport == dport) {
299 if (memcmp(src, dst, sizeof ip6->ip6_dst) > 0)
300 rev = 1;
301 }
302 if (rev) {
303 memcpy(&tha.src, dst, sizeof ip6->ip6_dst);
304 memcpy(&tha.dst, src, sizeof ip6->ip6_src);
305 tha.port = dport << 16 | sport;
306 } else {
307 memcpy(&tha.dst, dst, sizeof ip6->ip6_dst);
308 memcpy(&tha.src, src, sizeof ip6->ip6_src);
309 tha.port = sport << 16 | dport;
310 }
311
312 for (th = &tcp_seq_hash[tha.port % TSEQ_HASHSIZE];
313 th->nxt; th = th->nxt)
314 if (memcmp((char *)&tha, (char *)&th->addr,
315 sizeof(th->addr)) == 0)
316 break;
317
318 if (!th->nxt || (flags & TH_SYN)) {
319 /* didn't find it or new conversation */
320 if (th->nxt == NULL) {
321 th->nxt = (struct tcp_seq_hash6 *)
322 calloc(1, sizeof(*th));
323 if (th->nxt == NULL)
324 error("tcp_print: calloc");
325 }
326 th->addr = tha;
327 if (rev)
328 th->ack = seq, th->seq = ack - 1;
329 else
330 th->seq = seq, th->ack = ack - 1;
331 } else {
332 if (rev)
333 seq -= th->ack, ack -= th->seq;
334 else
335 seq -= th->seq, ack -= th->ack;
336 }
337
338 thseq = th->seq;
339 thack = th->ack;
340 } else {
341 #else /*INET6*/
342 {
343 #endif /*INET6*/
344 register struct tcp_seq_hash *th;
345 struct tcp_seq_hash *tcp_seq_hash;
346 const struct in_addr *src, *dst;
347 struct tha tha;
348
349 tcp_seq_hash = tcp_seq_hash4;
350 src = &ip->ip_src;
351 dst = &ip->ip_dst;
352 if (sport > dport)
353 rev = 1;
354 else if (sport == dport) {
355 if (memcmp(src, dst, sizeof ip->ip_dst) > 0)
356 rev = 1;
357 }
358 if (rev) {
359 memcpy(&tha.src, dst, sizeof ip->ip_dst);
360 memcpy(&tha.dst, src, sizeof ip->ip_src);
361 tha.port = dport << 16 | sport;
362 } else {
363 memcpy(&tha.dst, dst, sizeof ip->ip_dst);
364 memcpy(&tha.src, src, sizeof ip->ip_src);
365 tha.port = sport << 16 | dport;
366 }
367
368 for (th = &tcp_seq_hash[tha.port % TSEQ_HASHSIZE];
369 th->nxt; th = th->nxt)
370 if (memcmp((char *)&tha, (char *)&th->addr,
371 sizeof(th->addr)) == 0)
372 break;
373
374 if (!th->nxt || (flags & TH_SYN)) {
375 /* didn't find it or new conversation */
376 if (th->nxt == NULL) {
377 th->nxt = (struct tcp_seq_hash *)
378 calloc(1, sizeof(*th));
379 if (th->nxt == NULL)
380 error("tcp_print: calloc");
381 }
382 th->addr = tha;
383 if (rev)
384 th->ack = seq, th->seq = ack - 1;
385 else
386 th->seq = seq, th->ack = ack - 1;
387 } else {
388 if (rev)
389 seq -= th->ack, ack -= th->seq;
390 else
391 seq -= th->seq, ack -= th->ack;
392 }
393
394 thseq = th->seq;
395 thack = th->ack;
396 }
397 } else {
398 /*fool gcc*/
399 thseq = thack = rev = 0;
400 }
401 if (hlen > length) {
402 (void)printf(" [bad hdr length %u - too long, > %u]",
403 hlen, length);
404 return;
405 }
406
407 if (vflag && !Kflag && !fragmented) {
408 /* Check the checksum, if possible. */
409 u_int16_t sum, tcp_sum;
410
411 if (IP_V(ip) == 4) {
412 if (TTEST2(tp->th_sport, length)) {
413 sum = tcp_cksum(ip, tp, length);
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 #ifdef INET6
425 else if (IP_V(ip) == 6 && ip6->ip6_plen) {
426 if (TTEST2(tp->th_sport, length)) {
427 sum = nextproto6_cksum(ip6, (const u_int8_t *)tp, length, IPPROTO_TCP);
428 tcp_sum = EXTRACT_16BITS(&tp->th_sum);
429
430 (void)printf(", cksum 0x%04x", tcp_sum);
431 if (sum != 0)
432 (void)printf(" (incorrect -> 0x%04x)",
433 in_cksum_shouldbe(tcp_sum, sum));
434 else
435 (void)printf(" (correct)");
436
437 }
438 }
439 #endif
440 }
441
442 length -= hlen;
443 if (vflag > 1 || length > 0 || flags & (TH_SYN | TH_FIN | TH_RST)) {
444 (void)printf(", seq %u", seq);
445
446 if (length > 0) {
447 (void)printf(":%u", seq + length);
448 }
449 }
450
451 if (flags & TH_ACK) {
452 (void)printf(", ack %u", ack);
453 }
454
455 (void)printf(", win %d", win);
456
457 if (flags & TH_URG)
458 (void)printf(", urg %d", urp);
459 /*
460 * Handle any options.
461 */
462 if (hlen > sizeof(*tp)) {
463 register const u_char *cp;
464 register u_int i, opt, datalen;
465 register u_int len;
466
467 hlen -= sizeof(*tp);
468 cp = (const u_char *)tp + sizeof(*tp);
469 printf(", options [");
470 while (hlen > 0) {
471 if (ch != '\0')
472 putchar(ch);
473 TCHECK(*cp);
474 opt = *cp++;
475 if (ZEROLENOPT(opt))
476 len = 1;
477 else {
478 TCHECK(*cp);
479 len = *cp++; /* total including type, len */
480 if (len < 2 || len > hlen)
481 goto bad;
482 --hlen; /* account for length byte */
483 }
484 --hlen; /* account for type byte */
485 datalen = 0;
486
487 /* Bail if "l" bytes of data are not left or were not captured */
488 #define LENCHECK(l) { if ((l) > hlen) goto bad; TCHECK2(*cp, l); }
489
490
491 printf("%s", tok2str(tcp_option_values, "unknown-%u", opt));
492
493 switch (opt) {
494
495 case TCPOPT_MAXSEG:
496 datalen = 2;
497 LENCHECK(datalen);
498 (void)printf(" %u", EXTRACT_16BITS(cp));
499 break;
500
501 case TCPOPT_WSCALE:
502 datalen = 1;
503 LENCHECK(datalen);
504 (void)printf(" %u", *cp);
505 break;
506
507 case TCPOPT_SACK:
508 datalen = len - 2;
509 if (datalen % 8 != 0) {
510 (void)printf("malformed sack");
511 } else {
512 u_int32_t s, e;
513
514 (void)printf(" %d ", datalen / 8);
515 for (i = 0; i < datalen; i += 8) {
516 LENCHECK(i + 4);
517 s = EXTRACT_32BITS(cp + i);
518 LENCHECK(i + 8);
519 e = EXTRACT_32BITS(cp + i + 4);
520 if (rev) {
521 s -= thseq;
522 e -= thseq;
523 } else {
524 s -= thack;
525 e -= thack;
526 }
527 (void)printf("{%u:%u}", s, e);
528 }
529 }
530 break;
531
532 case TCPOPT_CC:
533 case TCPOPT_CCNEW:
534 case TCPOPT_CCECHO:
535 case TCPOPT_ECHO:
536 case TCPOPT_ECHOREPLY:
537
538 /*
539 * those options share their semantics.
540 * fall through
541 */
542 datalen = 4;
543 LENCHECK(datalen);
544 (void)printf(" %u", EXTRACT_32BITS(cp));
545 break;
546
547 case TCPOPT_TIMESTAMP:
548 datalen = 8;
549 LENCHECK(datalen);
550 (void)printf(" val %u ecr %u",
551 EXTRACT_32BITS(cp),
552 EXTRACT_32BITS(cp + 4));
553 break;
554
555 case TCPOPT_SIGNATURE:
556 datalen = TCP_SIGLEN;
557 LENCHECK(datalen);
558 #ifdef HAVE_LIBCRYPTO
559 switch (tcp_verify_signature(ip, tp,
560 bp + TH_OFF(tp) * 4, length, cp)) {
561
562 case SIGNATURE_VALID:
563 (void)printf("valid");
564 break;
565
566 case SIGNATURE_INVALID:
567 (void)printf("invalid");
568 break;
569
570 case CANT_CHECK_SIGNATURE:
571 (void)printf("can't check - ");
572 for (i = 0; i < TCP_SIGLEN; ++i)
573 (void)printf("%02x", cp[i]);
574 break;
575 }
576 #else
577 for (i = 0; i < TCP_SIGLEN; ++i)
578 (void)printf("%02x", cp[i]);
579 #endif
580 break;
581
582 case TCPOPT_AUTH:
583 (void)printf("keyid %d", *cp++);
584 datalen = len - 3;
585 for (i = 0; i < datalen; ++i) {
586 LENCHECK(i);
587 (void)printf("%02x", cp[i]);
588 }
589 break;
590
591
592 case TCPOPT_EOL:
593 case TCPOPT_NOP:
594 case TCPOPT_SACKOK:
595 /*
596 * Nothing interesting.
597 * fall through
598 */
599 break;
600
601 case TCPOPT_UTO:
602 datalen = 2;
603 LENCHECK(datalen);
604 utoval = EXTRACT_16BITS(cp);
605 (void)printf("0x%x", utoval);
606 if (utoval & 0x0001)
607 utoval = (utoval >> 1) * 60;
608 else
609 utoval >>= 1;
610 (void)printf(" %u", utoval);
611 break;
612
613 case TCPOPT_MPTCP:
614 datalen = len - 2;
615 LENCHECK(datalen);
616 if (!mptcp_print(cp-2, len, flags))
617 goto bad;
618 break;
619
620 case TCPOPT_EXPERIMENT2:
621 datalen = len - 2;
622 LENCHECK(datalen);
623 if (datalen < 2)
624 goto bad;
625 /* RFC6994 */
626 magic = EXTRACT_16BITS(cp);
627 (void)printf("-");
628
629 switch(magic) {
630
631 case 0xf989:
632 /* TCP Fast Open: draft-ietf-tcpm-fastopen-04 */
633 if (datalen == 2) {
634 /* Fast Open Cookie Request */
635 (void)printf("tfo cookiereq");
636 } else {
637 /* Fast Open Cookie */
638 if (datalen % 2 != 0 || datalen < 6 || datalen > 18) {
639 (void)printf("tfo malformed");
640 } else {
641 (void)printf("tfo cookie ");
642 for (i = 2; i < datalen; ++i)
643 (void)printf("%02x", cp[i]);
644 }
645 }
646 break;
647
648 default:
649 /* Unknown magic number */
650 (void)printf("%04x", magic);
651 break;
652 }
653 break;
654
655 default:
656 datalen = len - 2;
657 if (datalen)
658 printf(" 0x");
659 for (i = 0; i < datalen; ++i) {
660 LENCHECK(i);
661 (void)printf("%02x", cp[i]);
662 }
663 break;
664 }
665
666 /* Account for data printed */
667 cp += datalen;
668 hlen -= datalen;
669
670 /* Check specification against observed length */
671 ++datalen; /* option octet */
672 if (!ZEROLENOPT(opt))
673 ++datalen; /* size octet */
674 if (datalen != len)
675 (void)printf("[len %d]", len);
676 ch = ',';
677 if (opt == TCPOPT_EOL)
678 break;
679 }
680 putchar(']');
681 }
682
683 /*
684 * Print length field before crawling down the stack.
685 */
686 printf(", length %u", length);
687
688 if (length <= 0)
689 return;
690
691 /*
692 * Decode payload if necessary.
693 */
694 bp += TH_OFF(tp) * 4;
695 if ((flags & TH_RST) && vflag) {
696 print_tcp_rst_data(bp, length);
697 return;
698 }
699
700 if (packettype) {
701 switch (packettype) {
702 case PT_ZMTP1:
703 zmtp1_print(bp, length);
704 break;
705 }
706 return;
707 }
708
709 if (sport == TELNET_PORT || dport == TELNET_PORT) {
710 if (!qflag && vflag)
711 telnet_print(bp, length);
712 } else if (sport == BGP_PORT || dport == BGP_PORT)
713 bgp_print(bp, length);
714 else if (sport == PPTP_PORT || dport == PPTP_PORT)
715 pptp_print(bp);
716 #ifdef TCPDUMP_DO_SMB
717 else if (sport == NETBIOS_SSN_PORT || dport == NETBIOS_SSN_PORT)
718 nbt_tcp_print(bp, length);
719 else if (sport == SMB_PORT || dport == SMB_PORT)
720 smb_tcp_print(bp, length);
721 #endif
722 else if (sport == BEEP_PORT || dport == BEEP_PORT)
723 beep_print(bp, length);
724 else if (sport == OPENFLOW_PORT || dport == OPENFLOW_PORT)
725 openflow_print(bp, length);
726 else if (length > 2 &&
727 (sport == NAMESERVER_PORT || dport == NAMESERVER_PORT ||
728 sport == MULTICASTDNS_PORT || dport == MULTICASTDNS_PORT)) {
729 /*
730 * TCP DNS query has 2byte length at the head.
731 * XXX packet could be unaligned, it can go strange
732 */
733 ns_print(bp + 2, length - 2, 0);
734 } else if (sport == MSDP_PORT || dport == MSDP_PORT) {
735 msdp_print(bp, length);
736 } else if (sport == RPKI_RTR_PORT || dport == RPKI_RTR_PORT) {
737 rpki_rtr_print(bp, length);
738 }
739 else if (length > 0 && (sport == LDP_PORT || dport == LDP_PORT)) {
740 ldp_print(bp, length);
741 }
742
743 return;
744 bad:
745 fputs("[bad opt]", stdout);
746 if (ch != '\0')
747 putchar('>');
748 return;
749 trunc:
750 fputs("[|tcp]", stdout);
751 if (ch != '\0')
752 putchar('>');
753 }
754
755 /*
756 * RFC1122 says the following on data in RST segments:
757 *
758 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
759 *
760 * A TCP SHOULD allow a received RST segment to include data.
761 *
762 * DISCUSSION
763 * It has been suggested that a RST segment could contain
764 * ASCII text that encoded and explained the cause of the
765 * RST. No standard has yet been established for such
766 * data.
767 *
768 */
769
770 static void
771 print_tcp_rst_data(register const u_char *sp, u_int length)
772 {
773 int c;
774
775 if (TTEST2(*sp, length))
776 printf(" [RST");
777 else
778 printf(" [!RST");
779 if (length > MAX_RST_DATA_LEN) {
780 length = MAX_RST_DATA_LEN; /* can use -X for longer */
781 putchar('+'); /* indicate we truncate */
782 }
783 putchar(' ');
784 while (length-- && sp <= snapend) {
785 c = *sp++;
786 safeputchar(c);
787 }
788 putchar(']');
789 }
790
791 #ifdef HAVE_LIBCRYPTO
792 USES_APPLE_DEPRECATED_API
793 static int
794 tcp_verify_signature(const struct ip *ip, const struct tcphdr *tp,
795 const u_char *data, int length, const u_char *rcvsig)
796 {
797 struct tcphdr tp1;
798 u_char sig[TCP_SIGLEN];
799 char zero_proto = 0;
800 MD5_CTX ctx;
801 u_int16_t savecsum, tlen;
802 #ifdef INET6
803 struct ip6_hdr *ip6;
804 u_int32_t len32;
805 u_int8_t nxt;
806 #endif
807
808 if (data + length > snapend) {
809 printf("snaplen too short, ");
810 return (CANT_CHECK_SIGNATURE);
811 }
812
813 tp1 = *tp;
814
815 if (sigsecret == NULL) {
816 printf("shared secret not supplied with -M, ");
817 return (CANT_CHECK_SIGNATURE);
818 }
819
820 MD5_Init(&ctx);
821 /*
822 * Step 1: Update MD5 hash with IP pseudo-header.
823 */
824 if (IP_V(ip) == 4) {
825 MD5_Update(&ctx, (char *)&ip->ip_src, sizeof(ip->ip_src));
826 MD5_Update(&ctx, (char *)&ip->ip_dst, sizeof(ip->ip_dst));
827 MD5_Update(&ctx, (char *)&zero_proto, sizeof(zero_proto));
828 MD5_Update(&ctx, (char *)&ip->ip_p, sizeof(ip->ip_p));
829 tlen = EXTRACT_16BITS(&ip->ip_len) - IP_HL(ip) * 4;
830 tlen = htons(tlen);
831 MD5_Update(&ctx, (char *)&tlen, sizeof(tlen));
832 #ifdef INET6
833 } else if (IP_V(ip) == 6) {
834 ip6 = (struct ip6_hdr *)ip;
835 MD5_Update(&ctx, (char *)&ip6->ip6_src, sizeof(ip6->ip6_src));
836 MD5_Update(&ctx, (char *)&ip6->ip6_dst, sizeof(ip6->ip6_dst));
837 len32 = htonl(EXTRACT_16BITS(&ip6->ip6_plen));
838 MD5_Update(&ctx, (char *)&len32, sizeof(len32));
839 nxt = 0;
840 MD5_Update(&ctx, (char *)&nxt, sizeof(nxt));
841 MD5_Update(&ctx, (char *)&nxt, sizeof(nxt));
842 MD5_Update(&ctx, (char *)&nxt, sizeof(nxt));
843 nxt = IPPROTO_TCP;
844 MD5_Update(&ctx, (char *)&nxt, sizeof(nxt));
845 #endif
846 } else {
847 #ifdef INET6
848 printf("IP version not 4 or 6, ");
849 #else
850 printf("IP version not 4, ");
851 #endif
852 return (CANT_CHECK_SIGNATURE);
853 }
854
855 /*
856 * Step 2: Update MD5 hash with TCP header, excluding options.
857 * The TCP checksum must be set to zero.
858 */
859 savecsum = tp1.th_sum;
860 tp1.th_sum = 0;
861 MD5_Update(&ctx, (char *)&tp1, sizeof(struct tcphdr));
862 tp1.th_sum = savecsum;
863 /*
864 * Step 3: Update MD5 hash with TCP segment data, if present.
865 */
866 if (length > 0)
867 MD5_Update(&ctx, data, length);
868 /*
869 * Step 4: Update MD5 hash with shared secret.
870 */
871 MD5_Update(&ctx, sigsecret, strlen(sigsecret));
872 MD5_Final(sig, &ctx);
873
874 if (memcmp(rcvsig, sig, TCP_SIGLEN) == 0)
875 return (SIGNATURE_VALID);
876 else
877 return (SIGNATURE_INVALID);
878 }
879 USES_APPLE_RST
880 #endif /* HAVE_LIBCRYPTO */
881
882 /*
883 * Local Variables:
884 * c-style: whitesmith
885 * c-basic-offset: 8
886 * End:
887 */