2 * Routines for socks versions 4 &5 packet dissection
3 * Copyright 2000, Jeffrey C. Foster <jfoste@woodward.com>
5 * $Id: packet-socks.c,v 1.40 2002/06/07 11:37:05 guy Exp $
7 * Ethereal - Network traffic analyzer
8 * By Gerald Combs <gerald@ethereal.com>
9 * 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.
26 * The Version 4 decode is based on SOCKS4.protocol and SOCKS4A.protocol.
27 * The Version 5 decoder is based upon rfc-1928
28 * The Version 5 User/Password authentication is based on rfc-1929.
30 * See http://www.socks.nec.com/socksprot.html for these and other documents
34 * 2001-01-08 JCFoster Fixed problem with NULL pointer for hash data.
35 * Now test and exit if hash_info is null.
38 /* Possible enhancements -
40 * Add GSS-API authentication per rfc-1961
41 * Add CHAP authentication
42 * Decode FLAG bits per
43 * http://www.socks.nec.com/draft/draft-ietf-aft-socks-pro-v-04.txt
44 * In call_next_dissector, could load the destination address into the
45 * pi structure before calling next dissector.
46 * remove display_string or at least make it use protocol identifiers
47 * socks_hash_entry_t needs to handle V5 address type and domain names
58 #ifdef HAVE_SYS_TYPES_H
59 # include <sys/types.h>
62 #ifdef HAVE_NETINET_IN_H
63 # include <netinet/in.h>
70 #ifdef NEED_SNPRINTF_H
71 # include "snprintf.h"
74 #include <epan/packet.h>
75 #include <epan/resolv.h>
76 #include "alignment.h"
77 #include <epan/conversation.h>
79 #include "packet-tcp.h"
80 #include "packet-udp.h"
81 #include <epan/strutil.h>
84 #define compare_packet(X) (X == (pinfo->fd->num))
85 #define get_packet_ptr (pinfo->fd->num)
86 #define row_pointer_type guint32
88 #define TCP_PORT_SOCKS 1080
91 /**************** Socks commands ******************/
93 #define CONNECT_COMMAND 1
94 #define BIND_COMMAND 2
95 #define UDP_ASSOCIATE_COMMAND 3
96 #define PING_COMMAND 0x80
97 #define TRACERT_COMMAND 0x81
100 /********** V5 Authentication methods *************/
102 #define NO_AUTHENTICATION 0
103 #define GSS_API_AUTHENTICATION 1
104 #define USER_NAME_AUTHENTICATION 2
105 #define CHAP_AUTHENTICATION 3
106 #define AUTHENTICATION_FAILED 0xff
109 /*********** Header field identifiers *************/
111 static int proto_socks = -1;
113 static int ett_socks = -1;
114 static int ett_socks_auth = -1;
115 static int ett_socks_name = -1;
117 static int hf_socks_ver = -1;
118 static int hf_socks_ip_dst = -1;
119 static int hf_socks_ip6_dst = -1;
120 static int hf_user_name = -1;
121 static int hf_socks_dstport = -1;
122 static int hf_socks_cmd = -1;
123 static int hf_socks_results = -1;
124 static int hf_socks_results_4 = -1;
125 static int hf_socks_results_5 = -1;
128 /************* Dissector handles ***********/
130 static dissector_handle_t socks_handle;
131 static dissector_handle_t socks_udp_handle;
133 /************* State Machine names ***********/
158 guint32 udp_remote_port;
161 row_pointer_type v4_name_row;
162 row_pointer_type v4_user_name_row;
163 row_pointer_type connect_row;
164 row_pointer_type cmd_reply_row;
165 row_pointer_type bind_reply_row;
166 row_pointer_type command_row;
167 row_pointer_type auth_method_row;
168 row_pointer_type user_name_auth_row;
169 guint32 start_done_row;
171 guint32 dst_addr; /* this needs to handle IPv6 */
177 static char *address_type_table[] = {
187 /* String table for the V4 reply status messages */
190 static char *reply_table_v4[] = {
192 "Rejected or Failed",
193 "Rejected because SOCKS server cannot connect to identd on the client",
194 "Rejected because the client program and identd report different user-ids",
199 static const value_string reply_table_v4[] = {
201 {91, "Rejected or Failed"},
202 {92, "Rejected because SOCKS server cannot connect to identd on the client"},
203 {93, "Rejected because the client program and identd report different user-ids"},
207 /* String table for the V5 reply status messages */
210 static char *reply_table_v5[] = {
212 "General SOCKS server failure",
213 "Connection not allowed by ruleset",
214 "Network unreachable",
216 "Connection refused",
218 "Command not supported",
219 "Address type not supported",
224 static const value_string reply_table_v5[] = {
226 {1, "General SOCKS server failure"},
227 {2, "Connection not allowed by ruleset"},
228 {3, "Network unreachable"},
229 {4, "Host unreachable"},
230 {5, "Connection refused"},
232 {7, "Command not supported"},
233 {8, "Address type not supported"}
236 static const value_string cmd_strings[] = {
242 {0x81, "Traceroute"},
246 #define socks_hash_init_count 20
247 #define socks_hash_val_length (sizeof(socks_hash_entry_t))
249 static GMemChunk *socks_vals = NULL;
252 /************************* Support routines ***************************/
255 static int display_string(tvbuff_t *tvb, int offset,
256 proto_tree *tree, char *label){
258 /* display a string with a length, characters encoding */
259 /* they are displayed under a tree with the name in Label variable */
260 /* return the length of the string and the length byte */
263 proto_tree *name_tree;
267 int length = tvb_get_guint8(tvb, offset);
269 tvb_memcpy(tvb, (guint8 *)temp, offset+1, length);
272 ti = proto_tree_add_text(tree, tvb, offset, length + 1,
273 "%s: %s" , label, temp);
276 name_tree = proto_item_add_subtree(ti, ett_socks_name);
278 proto_tree_add_text( name_tree, tvb, offset, 1, "Length: %u", length);
282 proto_tree_add_text( name_tree, tvb, offset, length, "String: %s", temp);
289 static char *get_auth_method_name( guint Number){
291 /* return the name of the authenication method */
293 if ( Number == 0) return "No authentication";
294 if ( Number == 1) return "GSSAPI";
295 if ( Number == 2) return "Username/Password";
296 if ( Number == 3) return "Chap";
297 if (( Number >= 4) && ( Number <= 0x7f))return "IANA assigned";
298 if (( Number >= 0x80) && ( Number <= 0xfe)) return "private method";
299 if ( Number == 0xff) return "no acceptable method";
301 /* shouldn't reach here */
303 return "Bad method number (not 0-0xff)";
307 static char *get_command_name( guint Number){
309 /* return the name of the command as a string */
311 if ( Number == 0) return "Unknow";
312 if ( Number == 1) return "Connect";
313 if ( Number == 2) return "Bind";
314 if ( Number == 3) return "UdpAssociate";
315 if ( Number == 0x80) return "Ping";
316 if ( Number == 0x81) return "Traceroute";
321 static int display_address(tvbuff_t *tvb, int offset, proto_tree *tree) {
323 /* decode and display the v5 address, return offset of next byte */
325 int a_type = tvb_get_guint8(tvb, offset);
327 proto_tree_add_text( tree, tvb, offset, 1,
328 "Address Type: %d (%s)", a_type,
329 address_type_table[ MIN( (guint) a_type,
330 array_length( address_type_table)-1) ]);
334 if ( a_type == 1){ /* IPv4 address */
335 proto_tree_add_item( tree, hf_socks_ip_dst, tvb, offset,
339 else if ( a_type == 3){ /* domain name address */
341 offset += display_string(tvb, offset, tree,
344 else if ( a_type == 4){ /* IPv6 address */
345 proto_tree_add_item( tree, hf_socks_ip6_dst, tvb, offset,
354 static int get_address_v5(tvbuff_t *tvb, int offset,
355 socks_hash_entry_t *hash_info) {
357 /* decode the v5 address and return offset of next byte */
358 /*XXX this needs to handle IPV6 and domain name addresses */
361 int a_type = tvb_get_guint8(tvb, offset++);
363 if ( a_type == 1){ /* IPv4 address */
366 tvb_memcpy(tvb, (guint8 *)&hash_info->dst_addr,
371 else if ( a_type == 4) /* IPv6 address */
374 else if ( a_type == 3) /* domain name address */
375 offset += tvb_get_guint8(tvb, offset) + 1;
380 /********************* V5 UDP Associate handlers ***********************/
383 socks_udp_dissector(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) {
385 /* Conversation dissector called from UDP dissector. Decode and display */
386 /* the socks header, the pass the rest of the data to the udp port */
387 /* decode routine to handle the payload. */
391 socks_hash_entry_t *hash_info;
392 conversation_t *conversation;
393 proto_tree *socks_tree;
396 conversation = find_conversation( &pinfo->src, &pinfo->dst, pinfo->ptype,
397 pinfo->srcport, pinfo->destport, 0);
399 g_assert( conversation); /* should always find a conversation */
401 hash_info = conversation_get_proto_data(conversation, proto_socks);
403 if (check_col(pinfo->cinfo, COL_PROTOCOL))
404 col_set_str(pinfo->cinfo, COL_PROTOCOL, "Socks");
406 if (check_col(pinfo->cinfo, COL_INFO))
407 col_add_fstr(pinfo->cinfo, COL_INFO, "Version: 5, UDP Associated packet");
410 ti = proto_tree_add_protocol_format( tree, proto_socks, tvb,
411 offset, -1, "Socks" );
413 socks_tree = proto_item_add_subtree(ti, ett_socks);
415 proto_tree_add_text( socks_tree, tvb, offset, 2, "Reserved");
418 proto_tree_add_text( socks_tree, tvb, offset, 1, "Fragment Number: %d", tvb_get_guint8(tvb, offset));
422 offset = display_address( tvb, offset, socks_tree);
423 hash_info->udp_remote_port = tvb_get_ntohs(tvb, offset);
425 proto_tree_add_uint( socks_tree, hf_socks_dstport, tvb,
426 offset, 2, hash_info->udp_remote_port);
430 else { /* no tree, skip past the socks header */
432 offset = get_address_v5( tvb, offset, 0) + 2;
436 /* set pi src/dst port and call the udp sub-dissector lookup */
438 if ( pinfo->srcport == hash_info->port)
439 ptr = &pinfo->destport;
441 ptr = &pinfo->srcport;
443 *ptr = hash_info->udp_remote_port;
445 decode_udp_ports( tvb, offset, pinfo, tree, pinfo->srcport, pinfo->destport);
447 *ptr = hash_info->udp_port;
453 new_udp_conversation( socks_hash_entry_t *hash_info, packet_info *pinfo){
455 conversation_t *conversation = conversation_new( &pinfo->src, &pinfo->dst, PT_UDP,
456 hash_info->udp_port, hash_info->port, 0);
458 g_assert( conversation);
460 conversation_add_proto_data(conversation, proto_socks, hash_info);
461 conversation_set_dissector(conversation, socks_udp_handle);
467 /**************** Protocol Tree Display routines ******************/
470 display_socks_v4(tvbuff_t *tvb, int offset, packet_info *pinfo,
471 proto_tree *tree, socks_hash_entry_t *hash_info) {
474 /* Display the protocol tree for the V4 version. This routine uses the */
475 /* stored conversation information to decide what to do with the row. */
476 /* Per packet information would have been better to do this, but we */
477 /* didn't have that when I wrote this. And I didn't expect this to get */
483 /* Display command from client */
484 if (compare_packet( hash_info->connect_row)){
486 proto_tree_add_text( tree, tvb, offset, 1,
487 "Version: %u ", hash_info->version);
489 command = tvb_get_guint8(tvb, offset);
491 proto_tree_add_text( tree, tvb, offset, 1,
492 "Command: %u (%s)", command,
493 get_command_name( command));
497 proto_tree_add_item( tree, hf_socks_dstport, tvb, offset, 2,
501 /* Do destination address */
502 proto_tree_add_item( tree, hf_socks_ip_dst, tvb, offset,
507 /*XXX check this, needs to do length checking */
508 /* Should perhaps do TCP reassembly as well */
509 if ( tvb_offset_exists(tvb, offset)) {
510 /* display user name */
511 proto_tree_add_string( tree, hf_user_name, tvb, offset,
512 tvb_strsize(tvb, offset),
513 tvb_get_ptr(tvb, offset, -1));
517 /*Display command response from server*/
519 else if ( compare_packet( hash_info->cmd_reply_row)){
521 proto_tree_add_item( tree, hf_socks_ver, tvb, offset, 1,
524 /* Do results code */
525 proto_tree_add_item( tree, hf_socks_results_4, tvb, offset, 1, FALSE);
526 proto_tree_add_item_hidden(tree, hf_socks_results, tvb, offset, 1, FALSE);
531 proto_tree_add_item( tree, hf_socks_dstport, tvb, offset, 2,
534 /* Do remote address */
535 proto_tree_add_item( tree, hf_socks_ip_dst, tvb, offset, 4,
539 else if ( compare_packet( hash_info->v4_user_name_row)){
541 /*XXX check this, needs to do length checking */
542 /* Should perhaps do TCP reassembly as well */
543 if ( tvb_offset_exists(tvb, offset)) {
544 proto_tree_add_text( tree, tvb, offset,
545 tvb_strsize(tvb, offset),
546 "User Name: %s", tvb_get_ptr(tvb, offset, -1));
553 display_socks_v5(tvbuff_t *tvb, int offset, packet_info *pinfo,
554 proto_tree *tree, socks_hash_entry_t *hash_info) {
556 /* Display the protocol tree for the version. This routine uses the */
557 /* stored conversation information to decide what to do with the row. */
558 /* Per packet information would have been better to do this, but we */
559 /* didn't have that when I wrote this. And I didn't expect this to get */
562 unsigned int i, command;
567 if (compare_packet( hash_info->connect_row)){
569 proto_tree *AuthTree;
573 proto_tree_add_item( tree, hf_socks_ver, tvb, offset, 1,
577 temp = tvb_get_guint8(tvb, offset); /* Get Auth method count */
578 /* build auth tree */
579 ti = proto_tree_add_text( tree, tvb, offset, 1,
580 "Client Authentication Methods");
582 AuthTree = proto_item_add_subtree(ti, ett_socks_auth);
584 proto_tree_add_text( AuthTree, tvb, offset, 1,
588 for( i = 0; i < temp; ++i) {
590 AuthMethodStr = get_auth_method_name(
591 tvb_get_guint8( tvb, offset + i));
592 proto_tree_add_text( AuthTree, tvb, offset + i, 1,
593 "Method[%d]: %u (%s)", i,
594 tvb_get_guint8( tvb, offset + i), AuthMethodStr);
597 } /* Get accepted auth method */
598 else if (compare_packet( hash_info->auth_method_row)) {
602 proto_tree_add_text( tree, tvb, offset, 1,
603 "Accepted Auth Method: 0x%0x (%s)", tvb_get_guint8( tvb, offset),
604 get_auth_method_name( tvb_get_guint8( tvb, offset)));
607 } /* handle user/password auth */
608 else if (compare_packet( hash_info->user_name_auth_row)) {
610 proto_tree_add_item( tree, hf_socks_ver, tvb, offset, 1, FALSE);
612 /* process user name */
613 offset += display_string( tvb, offset, tree,
615 /* process password */
616 offset += display_string( tvb, offset, tree,
619 /* command to the server */
620 /* command response from server */
621 else if ((compare_packet( hash_info->command_row)) ||
622 (compare_packet( hash_info->cmd_reply_row)) ||
623 (compare_packet( hash_info->bind_reply_row))){
625 proto_tree_add_item( tree, hf_socks_ver, tvb, offset, 1, FALSE);
629 command = tvb_get_guint8(tvb, offset);
631 if (compare_packet( hash_info->command_row))
632 proto_tree_add_uint( tree, hf_socks_cmd, tvb, offset, 1,
636 proto_tree_add_item( tree, hf_socks_results_5, tvb, offset, 1, FALSE);
637 proto_tree_add_item_hidden(tree, hf_socks_results, tvb, offset, 1, FALSE);
642 proto_tree_add_text( tree, tvb, offset, 1,
643 "Reserved: 0x%0x (should = 0x00)", tvb_get_guint8(tvb, offset));
646 offset = display_address(tvb, offset, tree);
647 /*XXX Add remote port for search somehow */
649 proto_tree_add_text( tree, tvb, offset, 2,
651 (compare_packet( hash_info->bind_reply_row) ?
652 "Remote Host " : ""),
653 tvb_get_ntohs(tvb, offset));
659 /**************** Decoder State Machines ******************/
663 state_machine_v4( socks_hash_entry_t *hash_info, tvbuff_t *tvb,
664 int offset, packet_info *pinfo) {
666 /* Decode V4 protocol. This is done on the first pass through the */
667 /* list. Based upon the current state, decode the packet and determine */
668 /* what the next state should be. If we had per packet information, */
669 /* this would be the place to load them up. */
671 if ( hash_info->state == None) { /* new connection */
673 if (check_col(pinfo->cinfo, COL_INFO))
674 col_append_str(pinfo->cinfo, COL_INFO, " Connect to server request");
676 hash_info->state = Connecting; /* change state */
678 hash_info->command = tvb_get_guint8(tvb, offset + 1);
679 /* get remote port */
680 if ( hash_info->command == CONNECT_COMMAND)
681 hash_info->port = tvb_get_ntohs(tvb, offset + 2);
682 /* get remote address */
684 tvb_memcpy(tvb, (guint8 *)&hash_info->dst_addr, offset + 4, 4);
686 /* save the packet pointer */
687 hash_info->connect_row = get_packet_ptr;
689 /* skip past this stuff */
690 hash_info->connect_offset = offset + 8;
694 if ( !tvb_offset_exists(tvb, offset)) { /* if no user name */
696 hash_info->state = V4UserNameWait;
698 * XXX - add 1, or leave it alone?
699 * We were adding "strlen(...) + 1".
701 hash_info->connect_offset += 1;
704 * Add in the length of the user name.
705 * XXX - what if the user name is split between
708 hash_info->connect_offset += tvb_strsize(tvb, offset);
711 if ( !hash_info->dst_addr){ /* if no dest address */
713 if ( tvb_offset_exists(tvb, hash_info->connect_offset)) {
714 /*XXX copy remote name here ??? */
715 hash_info->state = Connecting;
718 hash_info->state = V4NameWait;
720 /* waiting for V4 user name */
721 }else if ( hash_info->state == V4UserNameWait){
723 if (check_col(pinfo->cinfo, COL_INFO))
724 col_append_str(pinfo->cinfo, COL_INFO, " Connect Request (User name)");
726 hash_info->v4_user_name_row = get_packet_ptr;
727 /*XXX may need to check for domain name here */
728 hash_info->state = Connecting;
730 /* waiting for V4 domain name */
731 else if ( hash_info->state == V4NameWait){
733 hash_info->v4_name_row = get_packet_ptr;
734 hash_info->state = Connecting;
737 else if ( hash_info->state == Connecting){
739 if (check_col(pinfo->cinfo, COL_INFO))
740 col_append_str(pinfo->cinfo, COL_INFO, " Connect Response");
742 /* save packet pointer */
743 hash_info->cmd_reply_row = get_packet_ptr;
744 hash_info->state = Done; /* change state */
754 state_machine_v5( socks_hash_entry_t *hash_info, tvbuff_t *tvb,
755 int offset, packet_info *pinfo) {
757 /* Decode V5 protocol. This is done on the first pass through the */
758 /* list. Based upon the current state, decode the packet and determine */
759 /* what the next state should be. If we had per packet information, */
760 /* this would be the place to load them up. */
765 if ( hash_info->state == None) {
767 if (check_col(pinfo->cinfo, COL_INFO))
768 col_append_str(pinfo->cinfo, COL_INFO, " Connect to server request");
770 hash_info->state = Connecting; /* change state */
771 hash_info->connect_row = get_packet_ptr;
773 temp = tvb_get_guint8(tvb, offset + 1);
774 /* skip past auth methods */
775 offset = hash_info->connect_offset = offset + 1 + temp;
777 else if ( hash_info->state == Connecting){
779 guint AuthMethod = tvb_get_guint8(tvb, offset + 1);
781 if (check_col(pinfo->cinfo, COL_INFO))
782 col_append_str(pinfo->cinfo, COL_INFO, " Connect to server response");
784 hash_info->auth_method_row = get_packet_ptr;
786 if ( AuthMethod == NO_AUTHENTICATION)
787 hash_info->state = V5Command;
789 else if ( AuthMethod == USER_NAME_AUTHENTICATION)
790 hash_info->state = UserNameAuth;
792 else if ( AuthMethod == GSS_API_AUTHENTICATION)
793 /*XXX should be this hash_info->state = GssApiAuth; */
794 hash_info->state = Done;
796 else hash_info->state = Done; /*Auth failed or error*/
800 else if ( hash_info->state == V5Command) { /* Handle V5 Command */
804 hash_info->command = tvb_get_guint8(tvb, offset + 1); /* get command */
806 if (check_col(pinfo->cinfo, COL_INFO))
807 col_append_fstr(pinfo->cinfo, COL_INFO, " Command Request - %s",
808 get_command_name(hash_info->command));
810 hash_info->state = V5Reply;
811 hash_info->command_row = get_packet_ptr;
813 offset += 3; /* skip to address type */
815 offset = get_address_v5(tvb, offset, hash_info);
817 temp = tvb_get_guint8(tvb, offset);
819 if (( hash_info->command == CONNECT_COMMAND) ||
820 ( hash_info->command == UDP_ASSOCIATE_COMMAND))
821 /* get remote port */
822 hash_info->port = tvb_get_ntohs(tvb, offset);
825 else if ( hash_info->state == V5Reply) { /* V5 Command Reply */
828 if (check_col(pinfo->cinfo, COL_INFO))
829 col_append_fstr(pinfo->cinfo, COL_INFO, " Command Response - %s",
830 get_command_name(hash_info->command));
832 hash_info->cmd_reply_row = get_packet_ptr;
834 if (( hash_info->command == CONNECT_COMMAND) ||
835 (hash_info->command == PING_COMMAND) ||
836 (hash_info->command == TRACERT_COMMAND))
837 hash_info->state = Done;
839 else if ( hash_info->command == BIND_COMMAND)
840 hash_info->state = V5BindReply;
842 else if ( hash_info->command == UDP_ASSOCIATE_COMMAND){
843 offset += 3; /* skip to address type */
844 offset = get_address_v5(tvb, offset, hash_info);
846 /* save server udp port and create udp conversation */
847 hash_info->udp_port = tvb_get_ntohs(tvb, offset);
849 if (!pinfo->fd->flags.visited)
850 new_udp_conversation( hash_info, pinfo);
852 /*XXX may need else statement to handle unknows and generate error message */
856 else if ( hash_info->state == V5BindReply) { /* V5 Bind Second Reply */
858 if (check_col(pinfo->cinfo, COL_INFO))
859 col_append_str(pinfo->cinfo, COL_INFO, " Command Response: Bind remote host info");
861 hash_info->bind_reply_row = get_packet_ptr;
862 hash_info->state = Done;
864 else if ( hash_info->state == UserNameAuth) { /* Handle V5 User Auth*/
865 if (check_col(pinfo->cinfo, COL_INFO))
866 col_append_str(pinfo->cinfo, COL_INFO,
867 " User authentication response");
869 hash_info->user_name_auth_row = get_packet_ptr;
870 hash_info->state = AuthReply;
873 else if ( hash_info->state == AuthReply){ /* V5 User Auth reply */
874 hash_info->cmd_reply_row = get_packet_ptr;
875 if (check_col(pinfo->cinfo, COL_INFO))
876 col_append_str(pinfo->cinfo, COL_INFO, " User authentication reply");
877 hash_info->state = V5Command;
884 display_ping_and_tracert(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, socks_hash_entry_t *hash_info) {
886 /* Display the ping/trace_route conversation */
889 const u_char *data, *dataend;
890 const u_char *lineend, *eol;
893 /* handle the end command */
894 if ( pinfo->destport == TCP_PORT_SOCKS){
895 if (check_col(pinfo->cinfo, COL_INFO))
896 col_append_str(pinfo->cinfo, COL_INFO, ", Terminate Request");
899 proto_tree_add_text(tree, tvb, offset, 1,
900 (hash_info->command == PING_COMMAND) ?
901 "Ping: End command" :
902 "Traceroute: End command");
904 else{ /* display the PING or Traceroute results */
905 if (check_col(pinfo->cinfo, COL_INFO))
906 col_append_str(pinfo->cinfo, COL_INFO, ", Results");
909 proto_tree_add_text(tree, tvb, offset, -1,
910 (hash_info->command == PING_COMMAND) ?
912 "Traceroute Results");
914 data = tvb_get_ptr(tvb, offset, -1);
915 dataend = data + tvb_length_remaining(tvb, offset);
917 while (data < dataend) {
919 lineend = find_line_end(data, dataend, &eol);
920 linelen = lineend - data;
922 proto_tree_add_text( tree, tvb, offset, linelen,
923 format_text(data, linelen));
933 static void call_next_dissector(tvbuff_t *tvb, int offset, packet_info *pinfo,
934 proto_tree *tree, socks_hash_entry_t *hash_info) {
936 /* Display the results for PING and TRACERT extensions or */
937 /* Call TCP dissector for the port that was passed during the */
938 /* connect process */
939 /* Load pointer to pinfo->XXXport depending upon the direction, */
940 /* change pinfo port to the remote port, call next dissecotr to decode */
941 /* the payload, and restore the pinfo port after that is done. */
945 if (( hash_info->command == PING_COMMAND) ||
946 ( hash_info->command == TRACERT_COMMAND))
948 display_ping_and_tracert(tvb, offset, pinfo, tree, hash_info);
950 else { /* call the tcp port decoder to handle the payload */
952 /*XXX may want to load dest address here */
954 if ( pinfo->destport == TCP_PORT_SOCKS)
955 ptr = &pinfo->destport;
957 ptr = &pinfo->srcport;
959 *ptr = hash_info->port;
960 decode_tcp_ports( tvb, offset, pinfo, tree, pinfo->srcport, pinfo->destport);
961 *ptr = TCP_PORT_SOCKS;
968 dissect_socks(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) {
971 proto_tree *socks_tree;
973 socks_hash_entry_t *hash_info;
974 conversation_t *conversation;
976 conversation = find_conversation( &pinfo->src, &pinfo->dst, pinfo->ptype,
977 pinfo->srcport, pinfo->destport, 0);
980 conversation = conversation_new( &pinfo->src, &pinfo->dst, pinfo->ptype,
981 pinfo->srcport, pinfo->destport, 0);
983 hash_info = conversation_get_proto_data(conversation,proto_socks);
985 hash_info = g_mem_chunk_alloc(socks_vals);
986 hash_info->start_done_row = G_MAXINT;
987 hash_info->state = None;
989 hash_info->version = tvb_get_guint8(tvb, offset); /* get version*/
991 if (( hash_info->version != 4) && /* error test version */
992 ( hash_info->version != 5))
993 hash_info->state = Done;
995 conversation_add_proto_data(conversation, proto_socks,
998 /* set dissector for now */
999 conversation_set_dissector(conversation, socks_handle);
1002 /* display summary window information */
1004 if (check_col(pinfo->cinfo, COL_PROTOCOL))
1005 col_set_str(pinfo->cinfo, COL_PROTOCOL, "Socks");
1007 if (check_col(pinfo->cinfo, COL_INFO)){
1008 if (( hash_info->version == 4) || ( hash_info->version == 5)){
1009 col_add_fstr(pinfo->cinfo, COL_INFO, "Version: %d",
1010 hash_info->version);
1012 else /* unknown version display error */
1013 col_set_str(pinfo->cinfo, COL_INFO, "Unknown");
1016 if ( hash_info->command == PING_COMMAND)
1017 col_append_str(pinfo->cinfo, COL_INFO, ", Ping Req");
1018 if ( hash_info->command == TRACERT_COMMAND)
1019 col_append_str(pinfo->cinfo, COL_INFO, ", Traceroute Req");
1021 /*XXX if ( hash_info->port != -1) */
1022 if ( hash_info->port != 0)
1023 col_append_fstr(pinfo->cinfo, COL_INFO, ", Remote Port: %d",
1028 /* run state machine if needed */
1030 if ((hash_info->state != Done) && ( !pinfo->fd->flags.visited)){
1032 if ( hash_info->version == 4)
1033 state_machine_v4( hash_info, tvb, offset, pinfo);
1035 else if ( hash_info->version == 5)
1036 state_machine_v5( hash_info, tvb, offset, pinfo);
1038 if (hash_info->state == Done) { /* if done now */
1039 hash_info->start_done_row = pinfo->fd->num;
1043 /* if proto tree, decode and display */
1046 ti = proto_tree_add_item( tree, proto_socks, tvb, offset, -1,
1049 socks_tree = proto_item_add_subtree(ti, ett_socks);
1051 if ( hash_info->version == 4)
1052 display_socks_v4(tvb, offset, pinfo, socks_tree,
1055 else if ( hash_info->version == 5)
1056 display_socks_v5(tvb, offset, pinfo, socks_tree,
1059 /* if past startup, add the faked stuff */
1060 if ( pinfo->fd->num > hash_info->start_done_row){
1061 /* add info to tree */
1062 proto_tree_add_text( socks_tree, tvb, offset, 0,
1063 "Command: %d (%s)", hash_info->command,
1064 get_command_name(hash_info->command));
1066 proto_tree_add_ipv4( socks_tree, hf_socks_ip_dst, tvb,
1067 offset, 0, hash_info->dst_addr);
1069 /* no fake address for ping & traceroute */
1071 if (( hash_info->command != PING_COMMAND) &&
1072 ( hash_info->command != TRACERT_COMMAND)){
1073 proto_tree_add_uint( socks_tree, hf_socks_dstport, tvb,
1074 offset, 0, hash_info->port);
1081 /* call next dissector if ready */
1083 if ( pinfo->fd->num > hash_info->start_done_row){
1084 call_next_dissector(tvb, offset, pinfo, tree, hash_info);
1090 static void socks_reinit( void){
1092 /* Do the cleanup work when a new pass through the packet list is */
1093 /* performed. Reset the highest row seen counter and re-initialize the */
1094 /* conversation memory chunks. */
1097 g_mem_chunk_destroy(socks_vals);
1099 socks_vals = g_mem_chunk_new("socks_vals", socks_hash_val_length,
1100 socks_hash_init_count * socks_hash_val_length,
1106 proto_register_socks( void){
1108 /*** Prep the socks protocol, register it and a initialization routine */
1109 /* to clear the hash stuff. */
1112 static gint *ett[] = {
1119 static hf_register_info hf[] = {
1123 { "Version", "socks.version", FT_UINT8, BASE_DEC, NULL,
1128 { "Remote Address", "socks.dst", FT_IPv4, BASE_NONE, NULL,
1132 { &hf_socks_ip6_dst,
1133 { "Remote Address(ipv6)", "socks.dstV6", FT_IPv6, BASE_NONE, NULL,
1139 { "User Name", "socks.username", FT_STRING, BASE_NONE,
1140 NULL, 0x0, "", HFILL
1143 { &hf_socks_dstport,
1144 { "Remote Port", "socks.dstport", FT_UINT16,
1145 BASE_DEC, NULL, 0x0, "", HFILL
1149 { "Command", "socks.command", FT_UINT8,
1150 BASE_DEC, VALS(cmd_strings), 0x0, "", HFILL
1153 { &hf_socks_results_4,
1154 { "Results(V4)", "socks.results_v4", FT_UINT8,
1155 BASE_DEC, VALS(reply_table_v4), 0x0, "", HFILL
1158 { &hf_socks_results_5,
1159 { "Results(V5)", "socks.results_v5", FT_UINT8,
1160 BASE_DEC, VALS(reply_table_v5), 0x0, "", HFILL
1163 { &hf_socks_results,
1164 { "Results(V5)", "socks.results", FT_UINT8,
1165 BASE_DEC, NULL, 0x0, "", HFILL
1172 proto_socks = proto_register_protocol (
1173 "Socks Protocol", "Socks", "socks");
1175 proto_register_field_array(proto_socks, hf, array_length(hf));
1176 proto_register_subtree_array(ett, array_length(ett));
1178 register_init_routine( &socks_reinit); /* register re-init routine */
1180 socks_udp_handle = create_dissector_handle(socks_udp_dissector,
1182 socks_handle = create_dissector_handle(dissect_socks, proto_socks);
1187 proto_reg_handoff_socks(void) {
1189 /* dissector install routine */
1191 dissector_add("tcp.port", TCP_PORT_SOCKS, socks_handle);