2 * Routines for TCP packet disassembly
4 * $Id: packet-tcp.c,v 1.51 1999/12/07 06:10:00 guy Exp $
6 * Ethereal - Network traffic analyzer
7 * By Gerald Combs <gerald@zing.org>
8 * Copyright 1998 Gerald Combs
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 #ifdef HAVE_SYS_TYPES_H
31 # include <sys/types.h>
34 #ifdef HAVE_NETINET_IN_H
35 # include <netinet/in.h>
45 #ifdef NEED_SNPRINTF_H
51 # include "snprintf.h"
54 #ifndef __PACKET_IP_H__
55 #include "packet-ip.h"
58 extern FILE* data_out_file;
60 static gchar info_str[COL_MAX_LEN];
63 static int proto_tcp = -1;
64 static int hf_tcp_srcport = -1;
65 static int hf_tcp_dstport = -1;
66 static int hf_tcp_port = -1;
67 static int hf_tcp_seq = -1;
68 static int hf_tcp_ack = -1;
69 static int hf_tcp_hdr_len = -1;
70 static int hf_tcp_flags = -1;
71 static int hf_tcp_flags_urg = -1;
72 static int hf_tcp_flags_ack = -1;
73 static int hf_tcp_flags_push = -1;
74 static int hf_tcp_flags_reset = -1;
75 static int hf_tcp_flags_syn = -1;
76 static int hf_tcp_flags_fin = -1;
77 static int hf_tcp_window_size = -1;
78 static int hf_tcp_checksum = -1;
79 static int hf_tcp_urgent_pointer = -1;
81 static gint ett_tcp = -1;
82 static gint ett_tcp_flags = -1;
83 static gint ett_tcp_options = -1;
84 static gint ett_tcp_option_sack = -1;
88 #define TCP_PORT_FTPDATA 20
89 #define TCP_PORT_FTP 21
90 #define TCP_PORT_TELNET 23
91 #define TCP_PORT_SMTP 25
92 #define TCP_PORT_HTTP 80
93 #define TCP_PORT_TACACS 49
94 #define TCP_PORT_POP 110
95 #define TCP_PORT_NNTP 119
96 #define TCP_PORT_NTP 123
97 #define TCP_PORT_NBSS 139
98 #define TCP_PORT_IMAP 143
99 #define TCP_PORT_BGP 179
100 #define TCP_PORT_SRVLOC 427
101 #define TCP_PORT_PRINTER 515
102 #define TCP_PORT_NCP 524
103 #define TCP_ALT_PORT_HTTP 8080
104 #define TCP_PORT_PPTP 1723
105 #define TCP_PORT_RTSP 554
106 #define TCP_PORT_YHOO 5050
107 #define TCP_PORT_MAPI 1065
108 #define TCP_PORT_TNS 1521
109 #define TCP_PORT_IRC 6667 /* good candidate for dynamic port specification */
111 /* TCP structs and definitions */
113 typedef struct _e_tcphdr {
118 guint8 th_off_x2; /* combines th_off and th_x2 */
135 #define TCPOPT_NOP 1 /* Padding */
136 #define TCPOPT_EOL 0 /* End of options */
137 #define TCPOPT_MSS 2 /* Segment size negotiating */
138 #define TCPOPT_WINDOW 3 /* Window scaling */
139 #define TCPOPT_SACK_PERM 4 /* SACK Permitted */
140 #define TCPOPT_SACK 5 /* SACK Block */
141 #define TCPOPT_ECHO 6
142 #define TCPOPT_ECHOREPLY 7
143 #define TCPOPT_TIMESTAMP 8 /* Better RTT estimations/PAWS */
145 #define TCPOPT_CCNEW 12
146 #define TCPOPT_CCECHO 13
152 #define TCPOLEN_MSS 4
153 #define TCPOLEN_WINDOW 3
154 #define TCPOLEN_SACK_PERM 2
155 #define TCPOLEN_SACK_MIN 2
156 #define TCPOLEN_ECHO 6
157 #define TCPOLEN_ECHOREPLY 6
158 #define TCPOLEN_TIMESTAMP 10
160 #define TCPOLEN_CCNEW 6
161 #define TCPOLEN_CCECHO 6
164 tcp_info_append_uint(const char *abbrev, guint32 val) {
169 add_len = snprintf(&info_str[info_len], COL_MAX_LEN - info_len, " %s=%u",
176 dissect_tcpopt_maxseg(const ip_tcp_opt *optp, const u_char *opd,
177 int offset, guint optlen, proto_tree *opt_tree)
179 proto_tree_add_text(opt_tree, offset, optlen,
180 "%s: %u bytes", optp->name, pntohs(opd));
181 tcp_info_append_uint("MSS", pntohs(opd));
185 dissect_tcpopt_wscale(const ip_tcp_opt *optp, const u_char *opd,
186 int offset, guint optlen, proto_tree *opt_tree)
188 proto_tree_add_text(opt_tree, offset, optlen,
189 "%s: %u bytes", optp->name, *opd);
190 tcp_info_append_uint("WS", *opd);
194 dissect_tcpopt_sack(const ip_tcp_opt *optp, const u_char *opd,
195 int offset, guint optlen, proto_tree *opt_tree)
197 proto_tree *field_tree = NULL;
199 guint leftedge, rightedge;
201 tf = proto_tree_add_text(opt_tree, offset, optlen, "%s:", optp->name);
202 offset += 2; /* skip past type and length */
203 optlen -= 2; /* subtract size of type and length */
205 if (field_tree == NULL) {
206 /* Haven't yet made a subtree out of this option. Do so. */
207 field_tree = proto_item_add_subtree(tf, *optp->subtree_index);
210 proto_tree_add_text(field_tree, offset, optlen,
211 "(suboption would go past end of option)");
214 /* XXX - check whether it goes past end of packet */
215 leftedge = pntohl(opd);
219 proto_tree_add_text(field_tree, offset, optlen,
220 "(suboption would go past end of option)");
223 /* XXX - check whether it goes past end of packet */
224 rightedge = pntohl(opd);
227 proto_tree_add_text(field_tree, offset, 8,
228 "left edge = %u, right edge = %u", leftedge, rightedge);
229 tcp_info_append_uint("SLE", leftedge);
230 tcp_info_append_uint("SRE", rightedge);
236 dissect_tcpopt_echo(const ip_tcp_opt *optp, const u_char *opd,
237 int offset, guint optlen, proto_tree *opt_tree)
239 proto_tree_add_text(opt_tree, offset, optlen,
240 "%s: %u", optp->name, pntohl(opd));
241 tcp_info_append_uint("ECHO", pntohl(opd));
245 dissect_tcpopt_timestamp(const ip_tcp_opt *optp, const u_char *opd,
246 int offset, guint optlen, proto_tree *opt_tree)
248 proto_tree_add_text(opt_tree, offset, optlen,
249 "%s: tsval %u, tsecr %u", optp->name, pntohl(opd), pntohl(opd + 4));
250 tcp_info_append_uint("TSV", pntohl(opd));
251 tcp_info_append_uint("TSER", pntohl(opd + 4));
255 dissect_tcpopt_cc(const ip_tcp_opt *optp, const u_char *opd,
256 int offset, guint optlen, proto_tree *opt_tree)
258 proto_tree_add_text(opt_tree, offset, optlen,
259 "%s: %u", optp->name, pntohl(opd));
260 tcp_info_append_uint("CC", pntohl(opd));
263 static const ip_tcp_opt tcpopts[] = {
282 "Maximum segment size",
286 dissect_tcpopt_maxseg
294 dissect_tcpopt_wscale
307 &ett_tcp_option_sack,
334 dissect_tcpopt_timestamp
362 #define N_TCP_OPTS (sizeof tcpopts / sizeof tcpopts[0])
365 static const true_false_string flags_set_truth = {
371 dissect_tcp(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
373 proto_tree *tcp_tree = NULL, *field_tree = NULL;
375 gchar flags[64] = "<None>";
376 gchar *fstr[] = {"FIN", "SYN", "RST", "PSH", "ACK", "URG"};
381 guint packet_max = pi.len;
383 /* To do: Check for {cap len,pkt len} < struct len */
384 /* Avoids alignment problems on many architectures. */
385 memcpy(&th, &pd[offset], sizeof(e_tcphdr));
386 th.th_sport = ntohs(th.th_sport);
387 th.th_dport = ntohs(th.th_dport);
388 th.th_win = ntohs(th.th_win);
389 th.th_sum = ntohs(th.th_sum);
390 th.th_urp = ntohs(th.th_urp);
391 th.th_seq = ntohl(th.th_seq);
392 th.th_ack = ntohl(th.th_ack);
396 if (check_col(fd, COL_PROTOCOL) || tree) {
397 for (i = 0; i < 6; i++) {
399 if (th.th_flags & bpos) {
401 strcpy(&flags[fpos], ", ");
404 strcpy(&flags[fpos], fstr[i]);
411 hlen = hi_nibble(th.th_off_x2) * 4; /* TCP header length, in bytes */
413 if (check_col(fd, COL_PROTOCOL))
414 col_add_str(fd, COL_PROTOCOL, "TCP");
415 if (check_col(fd, COL_INFO)) {
416 /* Copy the data into info_str in case one of the option handling
417 routines needs to append to it. */
418 if (th.th_flags & TH_URG)
419 info_len = snprintf(info_str, COL_MAX_LEN, "%s > %s [%s] Seq=%u Ack=%u Win=%u Urg=%u",
420 get_tcp_port(th.th_sport), get_tcp_port(th.th_dport), flags,
421 th.th_seq, th.th_ack, th.th_win, th.th_urp);
423 info_len = snprintf(info_str, COL_MAX_LEN, "%s > %s [%s] Seq=%u Ack=%u Win=%u",
424 get_tcp_port(th.th_sport), get_tcp_port(th.th_dport), flags,
425 th.th_seq, th.th_ack, th.th_win);
426 /* The info column is actually written after the options are decoded */
430 ti = proto_tree_add_item_format(tree, proto_tcp, offset, hlen, NULL, "Transmission Control Protocol, Src Port: %s (%u), Dst Port: %s (%u), Seq: %u, Ack: %u", get_tcp_port(th.th_sport), th.th_sport, get_tcp_port(th.th_dport), th.th_dport, th.th_seq, th.th_ack);
431 tcp_tree = proto_item_add_subtree(ti, ett_tcp);
432 proto_tree_add_item_format(tcp_tree, hf_tcp_srcport, offset, 2, th.th_sport,
433 "Source port: %s (%u)", get_tcp_port(th.th_sport), th.th_sport);
434 proto_tree_add_item_format(tcp_tree, hf_tcp_dstport, offset + 2, 2, th.th_dport,
435 "Destination port: %s (%u)", get_tcp_port(th.th_dport), th.th_dport);
436 proto_tree_add_item_hidden(tcp_tree, hf_tcp_port, offset, 2, th.th_sport);
437 proto_tree_add_item_hidden(tcp_tree, hf_tcp_port, offset + 2, 2, th.th_dport);
438 proto_tree_add_item(tcp_tree, hf_tcp_seq, offset + 4, 4, th.th_seq);
439 if (th.th_flags & TH_ACK)
440 proto_tree_add_item(tcp_tree, hf_tcp_ack, offset + 8, 4, th.th_ack);
441 proto_tree_add_item_format(tcp_tree, hf_tcp_hdr_len, offset + 12, 1, hlen,
442 "Header length: %u bytes", hlen);
443 tf = proto_tree_add_item_format(tcp_tree, hf_tcp_flags, offset + 13, 1,
444 th.th_flags, "Flags: 0x%04x (%s)", th.th_flags, flags);
445 field_tree = proto_item_add_subtree(tf, ett_tcp_flags);
446 proto_tree_add_item(field_tree, hf_tcp_flags_urg, offset + 13, 1, th.th_flags);
447 proto_tree_add_item(field_tree, hf_tcp_flags_ack, offset + 13, 1, th.th_flags);
448 proto_tree_add_item(field_tree, hf_tcp_flags_push, offset + 13, 1, th.th_flags);
449 proto_tree_add_item(field_tree, hf_tcp_flags_reset, offset + 13, 1, th.th_flags);
450 proto_tree_add_item(field_tree, hf_tcp_flags_syn, offset + 13, 1, th.th_flags);
451 proto_tree_add_item(field_tree, hf_tcp_flags_fin, offset + 13, 1, th.th_flags);
452 proto_tree_add_item(tcp_tree, hf_tcp_window_size, offset + 14, 2, th.th_win);
453 proto_tree_add_item(tcp_tree, hf_tcp_checksum, offset + 16, 2, th.th_sum);
454 if (th.th_flags & TH_URG)
455 proto_tree_add_item(tcp_tree, hf_tcp_urgent_pointer, offset + 18, 2, th.th_urp);
458 /* Decode TCP options, if any. */
459 if (tree && hlen > sizeof (e_tcphdr)) {
460 /* There's more than just the fixed-length header. Decode the
462 optlen = hlen - sizeof (e_tcphdr); /* length of options, in bytes */
463 tf = proto_tree_add_text(tcp_tree, offset + 20, optlen,
464 "Options: (%d bytes)", optlen);
465 field_tree = proto_item_add_subtree(tf, ett_tcp_options);
466 dissect_ip_tcp_options(&pd[offset + 20], offset + 20, optlen,
467 tcpopts, N_TCP_OPTS, TCPOPT_EOL, field_tree);
470 if (check_col(fd, COL_INFO))
471 col_add_str(fd, COL_INFO, info_str);
473 /* Skip over header + options */
477 pi.srcport = th.th_sport;
478 pi.destport = th.th_dport;
480 /* Check the packet length to see if there's more data
481 (it could be an ACK-only packet) */
482 if (packet_max > offset) {
484 /* ONC RPC. We can't base this on anything in the TCP header; we have
485 to look at the payload. If "dissect_rpc()" returns TRUE, it was
486 an RPC packet, otherwise it's some other type of packet. */
487 if (dissect_rpc(pd, offset, fd, tree))
490 /* XXX - this should be handled the way UDP handles this, with a table
491 of port numbers to which stuff can be added */
492 #define PORT_IS(port) (th.th_sport == port || th.th_dport == port)
493 if (PORT_IS(TCP_PORT_PRINTER))
494 dissect_lpd(pd, offset, fd, tree);
495 else if (PORT_IS(TCP_PORT_TELNET)) {
496 pi.match_port = TCP_PORT_TELNET;
497 dissect_telnet(pd, offset, fd, tree);
498 } else if (PORT_IS(TCP_PORT_FTPDATA)) {
499 pi.match_port = TCP_PORT_FTPDATA;
500 dissect_ftpdata(pd, offset, fd, tree);
501 } else if (PORT_IS(TCP_PORT_FTP)) {
502 pi.match_port = TCP_PORT_FTP;
503 dissect_ftp(pd, offset, fd, tree);
504 } else if (PORT_IS(TCP_PORT_POP)) {
505 pi.match_port = TCP_PORT_POP;
506 dissect_pop(pd, offset, fd, tree);
507 } else if (PORT_IS(TCP_PORT_IMAP)) {
508 pi.match_port = TCP_PORT_IMAP;
509 dissect_imap(pd, offset, fd, tree);
510 } else if (PORT_IS(TCP_PORT_NNTP)) {
511 pi.match_port = TCP_PORT_NNTP;
512 dissect_nntp(pd, offset, fd, tree);
513 } else if (PORT_IS(TCP_PORT_NTP)) {
514 pi.match_port = TCP_PORT_NTP;
515 dissect_ntp(pd, offset, fd, tree);
516 } else if (PORT_IS(TCP_PORT_PPTP)) {
517 pi.match_port = TCP_PORT_PPTP;
518 dissect_pptp(pd, offset, fd, tree);
519 } else if (PORT_IS(TCP_PORT_HTTP) || PORT_IS(TCP_ALT_PORT_HTTP)
521 dissect_http(pd, offset, fd, tree);
522 else if (PORT_IS(TCP_PORT_NBSS)) {
523 pi.match_port = TCP_PORT_NBSS;
524 dissect_nbss(pd, offset, fd, tree);
525 } else if (PORT_IS(TCP_PORT_RTSP))
526 dissect_rtsp(pd, offset, fd, tree);
527 else if (PORT_IS(TCP_PORT_BGP)) {
528 pi.match_port = TCP_PORT_BGP;
529 dissect_bgp(pd, offset, fd, tree);
530 } else if (PORT_IS(TCP_PORT_TACACS)) {
531 pi.match_port = TCP_PORT_TACACS;
532 dissect_tacplus(pd, offset, fd, tree);
533 } else if (PORT_IS(TCP_PORT_MAPI)) {
534 pi.match_port = TCP_PORT_MAPI;
535 dissect_mapi(pd, offset, fd, tree);
536 } else if (PORT_IS(TCP_PORT_TNS)) {
537 pi.match_port = TCP_PORT_TNS;
538 dissect_tns(pd, offset, fd, tree);
539 } else if (PORT_IS(TCP_PORT_IRC)) {
540 pi.match_port = TCP_PORT_IRC;
541 dissect_irc(pd, offset, fd, tree);
542 } else if (PORT_IS(TCP_PORT_SRVLOC)) {
543 pi.match_port = TCP_PORT_SRVLOC;
544 dissect_srvloc(pd, offset, fd, tree);
545 } else if (PORT_IS(TCP_PORT_NCP)) {
546 pi.match_port = TCP_PORT_NCP;
547 dissect_ncp(pd, offset, fd, tree);
549 /* check existence of high level protocols */
551 if (memcmp(&pd[offset], "GIOP", 4) == 0) {
552 dissect_giop(pd, offset, fd, tree);
554 else if ( PORT_IS(TCP_PORT_YHOO) &&
555 (memcmp(&pd[offset], "YPNS", 4) == 0 ||
556 memcmp(&pd[offset], "YHOO", 4) == 0 )) {
557 dissect_yhoo(pd, offset, fd, tree);
560 dissect_data(pd, offset, fd, tree);
567 if( data_out_file ) {
568 reassemble_tcp( th.th_seq, /* sequence number */
569 ( pi.len - offset ), /* data length */
570 ( pd+offset ), /* data */
571 ( pi.captured_len - offset ), /* captured data length */
572 ( th.th_flags & TH_SYN ), /* is syn set? */
583 proto_register_tcp(void)
585 static hf_register_info hf[] = {
588 { "Source Port", "tcp.srcport", FT_UINT16, BASE_DEC, NULL, 0x0,
592 { "Destination Port", "tcp.dstport", FT_UINT16, BASE_DEC, NULL, 0x0,
596 { "Source or Destination Port", "tcp.port", FT_UINT16, BASE_DEC, NULL, 0x0,
600 { "Sequence number", "tcp.seq", FT_UINT32, BASE_DEC, NULL, 0x0,
604 { "Acknowledgement number", "tcp.ack", FT_UINT32, BASE_DEC, NULL, 0x0,
608 { "Header Length", "tcp.hdr_len", FT_UINT8, BASE_DEC, NULL, 0x0,
612 { "Flags", "tcp.flags", FT_UINT8, BASE_HEX, NULL, 0x0,
616 { "Urgent", "tcp.flags.urg", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_URG,
620 { "Acknowledgment", "tcp.flags.ack", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_ACK,
623 { &hf_tcp_flags_push,
624 { "Push", "tcp.flags.push", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_PUSH,
627 { &hf_tcp_flags_reset,
628 { "Reset", "tcp.flags.reset", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_RST,
632 { "Syn", "tcp.flags.syn", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_SYN,
636 { "Fin", "tcp.flags.fin", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_FIN,
639 { &hf_tcp_window_size,
640 { "Window size", "tcp.window_size", FT_UINT16, BASE_DEC, NULL, 0x0,
644 { "Checksum", "tcp.checksum", FT_UINT16, BASE_HEX, NULL, 0x0,
647 { &hf_tcp_urgent_pointer,
648 { "Urgent pointer", "tcp.urgent_pointer", FT_UINT16, BASE_DEC, NULL, 0x0,
651 static gint *ett[] = {
655 &ett_tcp_option_sack,
658 proto_tcp = proto_register_protocol ("Transmission Control Protocol", "tcp");
659 proto_register_field_array(proto_tcp, hf, array_length(hf));
660 proto_register_subtree_array(ett, array_length(ett));