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