2 * Routines for TCP packet disassembly
4 * $Id: packet-tcp.c,v 1.56 2000/01/16 02:54:49 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>
44 #ifdef NEED_SNPRINTF_H
50 # include "snprintf.h"
55 #ifndef __PACKET_IP_H__
56 #include "packet-ip.h"
59 extern FILE* data_out_file;
61 static gchar info_str[COL_MAX_LEN];
64 static int proto_tcp = -1;
65 static int hf_tcp_srcport = -1;
66 static int hf_tcp_dstport = -1;
67 static int hf_tcp_port = -1;
68 static int hf_tcp_seq = -1;
69 static int hf_tcp_ack = -1;
70 static int hf_tcp_hdr_len = -1;
71 static int hf_tcp_flags = -1;
72 static int hf_tcp_flags_urg = -1;
73 static int hf_tcp_flags_ack = -1;
74 static int hf_tcp_flags_push = -1;
75 static int hf_tcp_flags_reset = -1;
76 static int hf_tcp_flags_syn = -1;
77 static int hf_tcp_flags_fin = -1;
78 static int hf_tcp_window_size = -1;
79 static int hf_tcp_checksum = -1;
80 static int hf_tcp_urgent_pointer = -1;
82 static gint ett_tcp = -1;
83 static gint ett_tcp_flags = -1;
84 static gint ett_tcp_options = -1;
85 static gint ett_tcp_option_sack = -1;
89 #define TCP_PORT_FTPDATA 20
90 #define TCP_PORT_FTP 21
91 #define TCP_PORT_TELNET 23
92 #define TCP_PORT_SMTP 25
93 #define TCP_PORT_HTTP 80
94 #define TCP_PORT_TACACS 49
95 #define TCP_PORT_POP 110
96 #define TCP_PORT_NNTP 119
97 #define TCP_PORT_NTP 123
98 #define TCP_PORT_NBSS 139
99 #define TCP_PORT_IMAP 143
100 #define TCP_PORT_BGP 179
101 #define TCP_PORT_LDAP 389
102 #define TCP_PORT_SRVLOC 427
103 #define TCP_PORT_PRINTER 515
104 #define TCP_PORT_NCP 524
105 #define TCP_PORT_RTSP 554
106 #define TCP_PORT_MAPI 1065
107 #define TCP_PORT_TNS 1521
108 #define TCP_PORT_PPTP 1723
109 #define TCP_PORT_PROXY_HTTP 3128
110 #define TCP_PORT_PROXY_ADMIN_HTTP 3132
111 #define TCP_PORT_YHOO 5050
112 #define TCP_ALT_PORT_HTTP 8080
113 #define TCP_PORT_IRC 6667
114 /* good candidate for dynamic port specification */
116 /* TCP structs and definitions */
118 typedef struct _e_tcphdr {
123 guint8 th_off_x2; /* combines th_off and th_x2 */
140 #define TCPOPT_NOP 1 /* Padding */
141 #define TCPOPT_EOL 0 /* End of options */
142 #define TCPOPT_MSS 2 /* Segment size negotiating */
143 #define TCPOPT_WINDOW 3 /* Window scaling */
144 #define TCPOPT_SACK_PERM 4 /* SACK Permitted */
145 #define TCPOPT_SACK 5 /* SACK Block */
146 #define TCPOPT_ECHO 6
147 #define TCPOPT_ECHOREPLY 7
148 #define TCPOPT_TIMESTAMP 8 /* Better RTT estimations/PAWS */
150 #define TCPOPT_CCNEW 12
151 #define TCPOPT_CCECHO 13
157 #define TCPOLEN_MSS 4
158 #define TCPOLEN_WINDOW 3
159 #define TCPOLEN_SACK_PERM 2
160 #define TCPOLEN_SACK_MIN 2
161 #define TCPOLEN_ECHO 6
162 #define TCPOLEN_ECHOREPLY 6
163 #define TCPOLEN_TIMESTAMP 10
165 #define TCPOLEN_CCNEW 6
166 #define TCPOLEN_CCECHO 6
169 tcp_info_append_uint(const char *abbrev, guint32 val) {
174 add_len = snprintf(&info_str[info_len], COL_MAX_LEN - info_len, " %s=%u",
181 dissect_tcpopt_maxseg(const ip_tcp_opt *optp, const u_char *opd,
182 int offset, guint optlen, proto_tree *opt_tree)
184 proto_tree_add_text(opt_tree, offset, optlen,
185 "%s: %u bytes", optp->name, pntohs(opd));
186 tcp_info_append_uint("MSS", pntohs(opd));
190 dissect_tcpopt_wscale(const ip_tcp_opt *optp, const u_char *opd,
191 int offset, guint optlen, proto_tree *opt_tree)
193 proto_tree_add_text(opt_tree, offset, optlen,
194 "%s: %u bytes", optp->name, *opd);
195 tcp_info_append_uint("WS", *opd);
199 dissect_tcpopt_sack(const ip_tcp_opt *optp, const u_char *opd,
200 int offset, guint optlen, proto_tree *opt_tree)
202 proto_tree *field_tree = NULL;
204 guint leftedge, rightedge;
206 tf = proto_tree_add_text(opt_tree, offset, optlen, "%s:", optp->name);
207 offset += 2; /* skip past type and length */
208 optlen -= 2; /* subtract size of type and length */
210 if (field_tree == NULL) {
211 /* Haven't yet made a subtree out of this option. Do so. */
212 field_tree = proto_item_add_subtree(tf, *optp->subtree_index);
215 proto_tree_add_text(field_tree, offset, optlen,
216 "(suboption would go past end of option)");
219 /* XXX - check whether it goes past end of packet */
220 leftedge = pntohl(opd);
224 proto_tree_add_text(field_tree, offset, optlen,
225 "(suboption would go past end of option)");
228 /* XXX - check whether it goes past end of packet */
229 rightedge = pntohl(opd);
232 proto_tree_add_text(field_tree, offset, 8,
233 "left edge = %u, right edge = %u", leftedge, rightedge);
234 tcp_info_append_uint("SLE", leftedge);
235 tcp_info_append_uint("SRE", rightedge);
241 dissect_tcpopt_echo(const ip_tcp_opt *optp, const u_char *opd,
242 int offset, guint optlen, proto_tree *opt_tree)
244 proto_tree_add_text(opt_tree, offset, optlen,
245 "%s: %u", optp->name, pntohl(opd));
246 tcp_info_append_uint("ECHO", pntohl(opd));
250 dissect_tcpopt_timestamp(const ip_tcp_opt *optp, const u_char *opd,
251 int offset, guint optlen, proto_tree *opt_tree)
253 proto_tree_add_text(opt_tree, offset, optlen,
254 "%s: tsval %u, tsecr %u", optp->name, pntohl(opd), pntohl(opd + 4));
255 tcp_info_append_uint("TSV", pntohl(opd));
256 tcp_info_append_uint("TSER", pntohl(opd + 4));
260 dissect_tcpopt_cc(const ip_tcp_opt *optp, const u_char *opd,
261 int offset, guint optlen, proto_tree *opt_tree)
263 proto_tree_add_text(opt_tree, offset, optlen,
264 "%s: %u", optp->name, pntohl(opd));
265 tcp_info_append_uint("CC", pntohl(opd));
268 static const ip_tcp_opt tcpopts[] = {
287 "Maximum segment size",
291 dissect_tcpopt_maxseg
299 dissect_tcpopt_wscale
312 &ett_tcp_option_sack,
339 dissect_tcpopt_timestamp
367 #define N_TCP_OPTS (sizeof tcpopts / sizeof tcpopts[0])
370 static const true_false_string flags_set_truth = {
376 dissect_tcp(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
378 proto_tree *tcp_tree = NULL, *field_tree = NULL;
380 gchar flags[64] = "<None>";
381 gchar *fstr[] = {"FIN", "SYN", "RST", "PSH", "ACK", "URG"};
386 guint packet_max = pi.len;
388 /* To do: Check for {cap len,pkt len} < struct len */
389 /* Avoids alignment problems on many architectures. */
390 memcpy(&th, &pd[offset], sizeof(e_tcphdr));
391 th.th_sport = ntohs(th.th_sport);
392 th.th_dport = ntohs(th.th_dport);
393 th.th_win = ntohs(th.th_win);
394 th.th_sum = ntohs(th.th_sum);
395 th.th_urp = ntohs(th.th_urp);
396 th.th_seq = ntohl(th.th_seq);
397 th.th_ack = ntohl(th.th_ack);
401 if (check_col(fd, COL_PROTOCOL) || tree) {
402 for (i = 0; i < 6; i++) {
404 if (th.th_flags & bpos) {
406 strcpy(&flags[fpos], ", ");
409 strcpy(&flags[fpos], fstr[i]);
416 hlen = hi_nibble(th.th_off_x2) * 4; /* TCP header length, in bytes */
418 if (check_col(fd, COL_PROTOCOL))
419 col_add_str(fd, COL_PROTOCOL, "TCP");
420 if (check_col(fd, COL_INFO)) {
421 /* Copy the data into info_str in case one of the option handling
422 routines needs to append to it. */
423 if (th.th_flags & TH_URG)
424 info_len = snprintf(info_str, COL_MAX_LEN, "%s > %s [%s] Seq=%u Ack=%u Win=%u Urg=%u",
425 get_tcp_port(th.th_sport), get_tcp_port(th.th_dport), flags,
426 th.th_seq, th.th_ack, th.th_win, th.th_urp);
428 info_len = snprintf(info_str, COL_MAX_LEN, "%s > %s [%s] Seq=%u Ack=%u Win=%u",
429 get_tcp_port(th.th_sport), get_tcp_port(th.th_dport), flags,
430 th.th_seq, th.th_ack, th.th_win);
431 /* The info column is actually written after the options are decoded */
435 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);
436 tcp_tree = proto_item_add_subtree(ti, ett_tcp);
437 proto_tree_add_item_format(tcp_tree, hf_tcp_srcport, offset, 2, th.th_sport,
438 "Source port: %s (%u)", get_tcp_port(th.th_sport), th.th_sport);
439 proto_tree_add_item_format(tcp_tree, hf_tcp_dstport, offset + 2, 2, th.th_dport,
440 "Destination port: %s (%u)", get_tcp_port(th.th_dport), th.th_dport);
441 proto_tree_add_item_hidden(tcp_tree, hf_tcp_port, offset, 2, th.th_sport);
442 proto_tree_add_item_hidden(tcp_tree, hf_tcp_port, offset + 2, 2, th.th_dport);
443 proto_tree_add_item(tcp_tree, hf_tcp_seq, offset + 4, 4, th.th_seq);
444 if (th.th_flags & TH_ACK)
445 proto_tree_add_item(tcp_tree, hf_tcp_ack, offset + 8, 4, th.th_ack);
446 proto_tree_add_item_format(tcp_tree, hf_tcp_hdr_len, offset + 12, 1, hlen,
447 "Header length: %u bytes", hlen);
448 tf = proto_tree_add_item_format(tcp_tree, hf_tcp_flags, offset + 13, 1,
449 th.th_flags, "Flags: 0x%04x (%s)", th.th_flags, flags);
450 field_tree = proto_item_add_subtree(tf, ett_tcp_flags);
451 proto_tree_add_item(field_tree, hf_tcp_flags_urg, offset + 13, 1, th.th_flags);
452 proto_tree_add_item(field_tree, hf_tcp_flags_ack, offset + 13, 1, th.th_flags);
453 proto_tree_add_item(field_tree, hf_tcp_flags_push, offset + 13, 1, th.th_flags);
454 proto_tree_add_item(field_tree, hf_tcp_flags_reset, offset + 13, 1, th.th_flags);
455 proto_tree_add_item(field_tree, hf_tcp_flags_syn, offset + 13, 1, th.th_flags);
456 proto_tree_add_item(field_tree, hf_tcp_flags_fin, offset + 13, 1, th.th_flags);
457 proto_tree_add_item(tcp_tree, hf_tcp_window_size, offset + 14, 2, th.th_win);
458 proto_tree_add_item(tcp_tree, hf_tcp_checksum, offset + 16, 2, th.th_sum);
459 if (th.th_flags & TH_URG)
460 proto_tree_add_item(tcp_tree, hf_tcp_urgent_pointer, offset + 18, 2, th.th_urp);
463 /* Decode TCP options, if any. */
464 if (tree && hlen > sizeof (e_tcphdr)) {
465 /* There's more than just the fixed-length header. Decode the
467 optlen = hlen - sizeof (e_tcphdr); /* length of options, in bytes */
468 tf = proto_tree_add_text(tcp_tree, offset + 20, optlen,
469 "Options: (%d bytes)", optlen);
470 field_tree = proto_item_add_subtree(tf, ett_tcp_options);
471 dissect_ip_tcp_options(&pd[offset + 20], offset + 20, optlen,
472 tcpopts, N_TCP_OPTS, TCPOPT_EOL, field_tree);
475 if (check_col(fd, COL_INFO))
476 col_add_str(fd, COL_INFO, info_str);
478 /* Skip over header + options */
482 pi.srcport = th.th_sport;
483 pi.destport = th.th_dport;
485 /* Check the packet length to see if there's more data
486 (it could be an ACK-only packet) */
487 if (packet_max > offset) {
489 /* try to apply the plugins */
491 plugin *pt_plug = plugin_list;
495 if (pt_plug->enabled && !strcmp(pt_plug->protocol, "tcp") &&
496 tree && dfilter_apply(pt_plug->filter, tree, pd)) {
497 pt_plug->dissector(pd, offset, fd, tree);
500 pt_plug = pt_plug->next;
505 /* ONC RPC. We can't base this on anything in the TCP header; we have
506 to look at the payload. If "dissect_rpc()" returns TRUE, it was
507 an RPC packet, otherwise it's some other type of packet. */
508 if (dissect_rpc(pd, offset, fd, tree))
511 /* XXX - this should be handled the way UDP handles this, with a table
512 of port numbers to which stuff can be added */
513 #define PORT_IS(port) (th.th_sport == port || th.th_dport == port)
514 if (PORT_IS(TCP_PORT_PRINTER))
515 dissect_lpd(pd, offset, fd, tree);
516 else if (PORT_IS(TCP_PORT_TELNET)) {
517 pi.match_port = TCP_PORT_TELNET;
518 dissect_telnet(pd, offset, fd, tree);
519 } else if (PORT_IS(TCP_PORT_FTPDATA)) {
520 pi.match_port = TCP_PORT_FTPDATA;
521 dissect_ftpdata(pd, offset, fd, tree);
522 } else if (PORT_IS(TCP_PORT_FTP)) {
523 pi.match_port = TCP_PORT_FTP;
524 dissect_ftp(pd, offset, fd, tree);
525 } else if (PORT_IS(TCP_PORT_POP)) {
526 pi.match_port = TCP_PORT_POP;
527 dissect_pop(pd, offset, fd, tree);
528 } else if (PORT_IS(TCP_PORT_IMAP)) {
529 pi.match_port = TCP_PORT_IMAP;
530 dissect_imap(pd, offset, fd, tree);
531 } else if (PORT_IS(TCP_PORT_NNTP)) {
532 pi.match_port = TCP_PORT_NNTP;
533 dissect_nntp(pd, offset, fd, tree);
534 } else if (PORT_IS(TCP_PORT_NTP)) {
535 pi.match_port = TCP_PORT_NTP;
536 dissect_ntp(pd, offset, fd, tree);
537 } else if (PORT_IS(TCP_PORT_PPTP)) {
538 pi.match_port = TCP_PORT_PPTP;
539 dissect_pptp(pd, offset, fd, tree);
540 } else if (PORT_IS(TCP_PORT_HTTP) || PORT_IS(TCP_ALT_PORT_HTTP)
541 || PORT_IS(631) || PORT_IS(TCP_PORT_PROXY_HTTP)
542 || PORT_IS(TCP_PORT_PROXY_ADMIN_HTTP))
543 dissect_http(pd, offset, fd, tree);
544 else if (PORT_IS(TCP_PORT_NBSS)) {
545 pi.match_port = TCP_PORT_NBSS;
546 dissect_nbss(pd, offset, fd, tree);
547 } else if (PORT_IS(TCP_PORT_RTSP))
548 dissect_rtsp(pd, offset, fd, tree);
549 else if (PORT_IS(TCP_PORT_BGP)) {
550 pi.match_port = TCP_PORT_BGP;
551 dissect_bgp(pd, offset, fd, tree);
552 } else if (PORT_IS(TCP_PORT_TACACS)) {
553 pi.match_port = TCP_PORT_TACACS;
554 dissect_tacplus(pd, offset, fd, tree);
555 } else if (PORT_IS(TCP_PORT_MAPI)) {
556 pi.match_port = TCP_PORT_MAPI;
557 dissect_mapi(pd, offset, fd, tree);
558 } else if (PORT_IS(TCP_PORT_TNS)) {
559 pi.match_port = TCP_PORT_TNS;
560 dissect_tns(pd, offset, fd, tree);
561 } else if (PORT_IS(TCP_PORT_IRC)) {
562 pi.match_port = TCP_PORT_IRC;
563 dissect_irc(pd, offset, fd, tree);
564 } else if (PORT_IS(TCP_PORT_LDAP)) {
565 pi.match_port = TCP_PORT_LDAP;
566 dissect_ldap(pd, offset, fd, tree);
567 } else if (PORT_IS(TCP_PORT_SRVLOC)) {
568 pi.match_port = TCP_PORT_SRVLOC;
569 dissect_srvloc(pd, offset, fd, tree);
570 } else if (PORT_IS(TCP_PORT_NCP)) {
571 pi.match_port = TCP_PORT_NCP;
572 dissect_ncp(pd, offset, fd, tree);
574 /* check existence of high level protocols */
576 if (memcmp(&pd[offset], "GIOP", 4) == 0) {
577 dissect_giop(pd, offset, fd, tree);
579 else if ( PORT_IS(TCP_PORT_YHOO) &&
580 (memcmp(&pd[offset], "YPNS", 4) == 0 ||
581 memcmp(&pd[offset], "YHOO", 4) == 0 )) {
582 dissect_yhoo(pd, offset, fd, tree);
585 dissect_data(pd, offset, fd, tree);
592 if( data_out_file ) {
593 reassemble_tcp( th.th_seq, /* sequence number */
594 ( pi.len - offset ), /* data length */
595 ( pd+offset ), /* data */
596 ( pi.captured_len - offset ), /* captured data length */
597 ( th.th_flags & TH_SYN ), /* is syn set? */
608 proto_register_tcp(void)
610 static hf_register_info hf[] = {
613 { "Source Port", "tcp.srcport", FT_UINT16, BASE_DEC, NULL, 0x0,
617 { "Destination Port", "tcp.dstport", FT_UINT16, BASE_DEC, NULL, 0x0,
621 { "Source or Destination Port", "tcp.port", FT_UINT16, BASE_DEC, NULL, 0x0,
625 { "Sequence number", "tcp.seq", FT_UINT32, BASE_DEC, NULL, 0x0,
629 { "Acknowledgement number", "tcp.ack", FT_UINT32, BASE_DEC, NULL, 0x0,
633 { "Header Length", "tcp.hdr_len", FT_UINT8, BASE_DEC, NULL, 0x0,
637 { "Flags", "tcp.flags", FT_UINT8, BASE_HEX, NULL, 0x0,
641 { "Urgent", "tcp.flags.urg", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_URG,
645 { "Acknowledgment", "tcp.flags.ack", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_ACK,
648 { &hf_tcp_flags_push,
649 { "Push", "tcp.flags.push", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_PUSH,
652 { &hf_tcp_flags_reset,
653 { "Reset", "tcp.flags.reset", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_RST,
657 { "Syn", "tcp.flags.syn", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_SYN,
661 { "Fin", "tcp.flags.fin", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_FIN,
664 { &hf_tcp_window_size,
665 { "Window size", "tcp.window_size", FT_UINT16, BASE_DEC, NULL, 0x0,
669 { "Checksum", "tcp.checksum", FT_UINT16, BASE_HEX, NULL, 0x0,
672 { &hf_tcp_urgent_pointer,
673 { "Urgent pointer", "tcp.urgent_pointer", FT_UINT16, BASE_DEC, NULL, 0x0,
676 static gint *ett[] = {
680 &ett_tcp_option_sack,
683 proto_tcp = proto_register_protocol ("Transmission Control Protocol", "tcp");
684 proto_register_field_array(proto_tcp, hf, array_length(hf));
685 proto_register_subtree_array(ett, array_length(ett));