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.38 2002/04/14 23:04:04 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;
268 int length = tvb_get_guint8(tvb, offset);
270 strncpy( temp, tvb_get_ptr(tvb, offset+1, -1), length);
273 ti = proto_tree_add_text(tree, tvb, offset, length + 1,
274 "%s: %s" , label, temp);
277 name_tree = proto_item_add_subtree(ti, ett_socks_name);
279 proto_tree_add_text( name_tree, tvb, offset, 1, "Length: %d", length);
283 proto_tree_add_text( name_tree, tvb, offset, length, "String: %s", temp);
290 static char *get_auth_method_name( guint Number){
292 /* return the name of the authenication method */
294 if ( Number == 0) return "No authentication";
295 if ( Number == 1) return "GSSAPI";
296 if ( Number == 2) return "Username/Password";
297 if ( Number == 3) return "Chap";
298 if (( Number >= 4) && ( Number <= 0x7f))return "IANA assigned";
299 if (( Number >= 0x80) && ( Number <= 0xfe)) return "private method";
300 if ( Number == 0xff) return "no acceptable method";
302 /* shouldn't reach here */
304 return "Bad method number (not 0-0xff)";
308 static char *get_command_name( guint Number){
310 /* return the name of the command as a string */
312 if ( Number == 0) return "Unknow";
313 if ( Number == 1) return "Connect";
314 if ( Number == 2) return "Bind";
315 if ( Number == 3) return "UdpAssociate";
316 if ( Number == 0x80) return "Ping";
317 if ( Number == 0x81) return "Traceroute";
322 static int display_address(tvbuff_t *tvb, int offset, proto_tree *tree) {
324 /* decode and display the v5 address, return offset of next byte */
326 int a_type = tvb_get_guint8(tvb, offset);
328 proto_tree_add_text( tree, tvb, offset, 1,
329 "Address Type: %d (%s)", a_type,
330 address_type_table[ MIN( (guint) a_type,
331 array_length( address_type_table)-1) ]);
335 if ( a_type == 1){ /* IPv4 address */
336 proto_tree_add_item( tree, hf_socks_ip_dst, tvb, offset,
340 else if ( a_type == 3){ /* domain name address */
342 offset += display_string(tvb, offset, tree,
345 else if ( a_type == 4){ /* IPv6 address */
346 proto_tree_add_item( tree, hf_socks_ip6_dst, tvb, offset,
355 static int get_address_v5(tvbuff_t *tvb, int offset,
356 socks_hash_entry_t *hash_info) {
358 /* decode the v5 address and return offset of next byte */
359 /*XXX this needs to handle IPV6 and domain name addresses */
362 int a_type = tvb_get_guint8(tvb, offset++);
364 if ( a_type == 1){ /* IPv4 address */
367 tvb_memcpy(tvb, (guint8 *)&hash_info->dst_addr,
372 else if ( a_type == 4) /* IPv6 address */
375 else if ( a_type == 3) /* domain name address */
376 offset += tvb_get_guint8(tvb, offset) + 1;
381 /********************* V5 UDP Associate handlers ***********************/
384 socks_udp_dissector(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) {
386 /* Conversation dissector called from UDP dissector. Decode and display */
387 /* the socks header, the pass the rest of the data to the udp port */
388 /* decode routine to handle the payload. */
392 socks_hash_entry_t *hash_info;
393 conversation_t *conversation;
394 proto_tree *socks_tree;
397 conversation = find_conversation( &pinfo->src, &pinfo->dst, pinfo->ptype,
398 pinfo->srcport, pinfo->destport, 0);
400 g_assert( conversation); /* should always find a conversation */
402 hash_info = conversation_get_proto_data(conversation, proto_socks);
404 if (check_col(pinfo->cinfo, COL_PROTOCOL))
405 col_set_str(pinfo->cinfo, COL_PROTOCOL, "Socks");
407 if (check_col(pinfo->cinfo, COL_INFO))
408 col_add_fstr(pinfo->cinfo, COL_INFO, "Version: 5, UDP Associated packet");
411 ti = proto_tree_add_protocol_format( tree, proto_socks, tvb,
412 offset, -1, "Socks" );
414 socks_tree = proto_item_add_subtree(ti, ett_socks);
416 proto_tree_add_text( socks_tree, tvb, offset, 2, "Reserved");
419 proto_tree_add_text( socks_tree, tvb, offset, 1, "Fragment Number: %d", tvb_get_guint8(tvb, offset));
423 offset = display_address( tvb, offset, socks_tree);
424 hash_info->udp_remote_port = tvb_get_ntohs(tvb, offset);
426 proto_tree_add_uint( socks_tree, hf_socks_dstport, tvb,
427 offset, 2, hash_info->udp_remote_port);
431 else { /* no tree, skip past the socks header */
433 offset = get_address_v5( tvb, offset, 0) + 2;
437 /* set pi src/dst port and call the udp sub-dissector lookup */
439 if ( pinfo->srcport == hash_info->port)
440 ptr = &pinfo->destport;
442 ptr = &pinfo->srcport;
444 *ptr = hash_info->udp_remote_port;
446 decode_udp_ports( tvb, offset, pinfo, tree, pinfo->srcport, pinfo->destport);
448 *ptr = hash_info->udp_port;
454 new_udp_conversation( socks_hash_entry_t *hash_info, packet_info *pinfo){
456 conversation_t *conversation = conversation_new( &pinfo->src, &pinfo->dst, PT_UDP,
457 hash_info->udp_port, hash_info->port, 0);
459 g_assert( conversation);
461 conversation_add_proto_data(conversation, proto_socks, hash_info);
462 conversation_set_dissector(conversation, socks_udp_handle);
468 /**************** Protocol Tree Display routines ******************/
471 display_socks_v4(tvbuff_t *tvb, int offset, packet_info *pinfo,
472 proto_tree *tree, socks_hash_entry_t *hash_info) {
475 /* Display the protocol tree for the V4 version. This routine uses the */
476 /* stored conversation information to decide what to do with the row. */
477 /* Per packet information would have been better to do this, but we */
478 /* didn't have that when I wrote this. And I didn't expect this to get */
484 /* Display command from client */
485 if (compare_packet( hash_info->connect_row)){
487 proto_tree_add_text( tree, tvb, offset, 1,
488 "Version: %u ", hash_info->version);
490 command = tvb_get_guint8(tvb, offset);
492 proto_tree_add_text( tree, tvb, offset, 1,
493 "Command: %u (%s)", command,
494 get_command_name( command));
498 proto_tree_add_item( tree, hf_socks_dstport, tvb, offset, 2,
502 /* Do destination address */
503 proto_tree_add_item( tree, hf_socks_ip_dst, tvb, offset,
508 /*XXX check this, needs to do length checking */
509 /* Should perhaps do TCP reassembly as well */
510 if ( tvb_offset_exists(tvb, offset)) {
511 /* display user name */
512 proto_tree_add_string( tree, hf_user_name, tvb, offset,
513 strlen( tvb_get_ptr(tvb, offset, -1)) + 1,
514 tvb_get_ptr(tvb, offset, -1));
518 /*Display command response from server*/
520 else if ( compare_packet( hash_info->cmd_reply_row)){
522 proto_tree_add_item( tree, hf_socks_ver, tvb, offset, 1,
525 /* Do results code */
526 proto_tree_add_item( tree, hf_socks_results_4, tvb, offset, 1, FALSE);
527 proto_tree_add_item_hidden(tree, hf_socks_results, tvb, offset, 1, FALSE);
532 proto_tree_add_item( tree, hf_socks_dstport, tvb, offset, 2,
535 /* Do remote address */
536 proto_tree_add_item( tree, hf_socks_ip_dst, tvb, offset, 4,
540 else if ( compare_packet( hash_info->v4_user_name_row)){
542 /*XXX check this, needs to do length checking */
543 /* Should perhaps do TCP reassembly as well */
544 if ( tvb_offset_exists(tvb, offset)) {
545 proto_tree_add_text( tree, tvb, offset,
546 strlen( tvb_get_ptr(tvb, offset, -1)),
547 "User Name: %s", tvb_get_ptr(tvb, offset, -1));
554 display_socks_v5(tvbuff_t *tvb, int offset, packet_info *pinfo,
555 proto_tree *tree, socks_hash_entry_t *hash_info) {
557 /* Display the protocol tree for the version. This routine uses the */
558 /* stored conversation information to decide what to do with the row. */
559 /* Per packet information would have been better to do this, but we */
560 /* didn't have that when I wrote this. And I didn't expect this to get */
563 unsigned int i, command;
568 if (compare_packet( hash_info->connect_row)){
570 proto_tree *AuthTree;
574 proto_tree_add_item( tree, hf_socks_ver, tvb, offset, 1,
578 temp = tvb_get_guint8(tvb, offset); /* Get Auth method count */
579 /* build auth tree */
580 ti = proto_tree_add_text( tree, tvb, offset, 1,
581 "Client Authentication Methods");
583 AuthTree = proto_item_add_subtree(ti, ett_socks_auth);
585 proto_tree_add_text( AuthTree, tvb, offset, 1,
589 for( i = 0; i < temp; ++i) {
591 AuthMethodStr = get_auth_method_name(
592 tvb_get_guint8( tvb, offset + i));
593 proto_tree_add_text( AuthTree, tvb, offset + i, 1,
594 "Method[%d]: %u (%s)", i,
595 tvb_get_guint8( tvb, offset + i), AuthMethodStr);
598 } /* Get accepted auth method */
599 else if (compare_packet( hash_info->auth_method_row)) {
603 proto_tree_add_text( tree, tvb, offset, 1,
604 "Accepted Auth Method: 0x%0x (%s)", tvb_get_guint8( tvb, offset),
605 get_auth_method_name( tvb_get_guint8( tvb, offset)));
608 } /* handle user/password auth */
609 else if (compare_packet( hash_info->user_name_auth_row)) {
611 proto_tree_add_item( tree, hf_socks_ver, tvb, offset, 1, FALSE);
613 /* process user name */
614 offset += display_string( tvb, offset, tree,
616 /* process password */
617 offset += display_string( tvb, offset, tree,
620 /* command to the server */
621 /* command response from server */
622 else if ((compare_packet( hash_info->command_row)) ||
623 (compare_packet( hash_info->cmd_reply_row)) ||
624 (compare_packet( hash_info->bind_reply_row))){
626 proto_tree_add_item( tree, hf_socks_ver, tvb, offset, 1, FALSE);
630 command = tvb_get_guint8(tvb, offset);
632 if (compare_packet( hash_info->command_row))
633 proto_tree_add_uint( tree, hf_socks_cmd, tvb, offset, 1,
637 proto_tree_add_item( tree, hf_socks_results_5, tvb, offset, 1, FALSE);
638 proto_tree_add_item_hidden(tree, hf_socks_results, tvb, offset, 1, FALSE);
643 proto_tree_add_text( tree, tvb, offset, 1,
644 "Reserved: 0x%0x (should = 0x00)", tvb_get_guint8(tvb, offset));
647 offset = display_address(tvb, offset, tree);
648 /*XXX Add remote port for search somehow */
650 proto_tree_add_text( tree, tvb, offset, 2,
652 (compare_packet( hash_info->bind_reply_row) ?
653 "Remote Host " : ""),
654 tvb_get_ntohs(tvb, offset));
660 /**************** Decoder State Machines ******************/
664 state_machine_v4( socks_hash_entry_t *hash_info, tvbuff_t *tvb,
665 int offset, packet_info *pinfo) {
667 /* Decode V4 protocol. This is done on the first pass through the */
668 /* list. Based upon the current state, decode the packet and determine */
669 /* what the next state should be. If we had per packet information, */
670 /* this would be the place to load them up. */
672 if ( hash_info->state == None) { /* new connection */
674 if (check_col(pinfo->cinfo, COL_INFO))
675 col_append_str(pinfo->cinfo, COL_INFO, " Connect to server request");
677 hash_info->state = Connecting; /* change state */
679 hash_info->command = tvb_get_guint8(tvb, offset + 1);
680 /* get remote port */
681 if ( hash_info->command == CONNECT_COMMAND)
682 hash_info->port = tvb_get_ntohs(tvb, offset + 2);
683 /* get remote address */
685 tvb_memcpy(tvb, (guint8 *)&hash_info->dst_addr, offset + 4, 4);
687 /* save the packet pointer */
688 hash_info->connect_row = get_packet_ptr;
690 /* skip past this stuff */
691 hash_info->connect_offset = offset + 8;
695 if ( !tvb_offset_exists(tvb, offset)) /* if no user name */
697 hash_info->state = V4UserNameWait;
700 hash_info->connect_offset += strlen( tvb_get_ptr(tvb, offset, -1)) + 1;
702 if ( !hash_info->dst_addr){ /* if no dest address */
704 if ( tvb_offset_exists(tvb, hash_info->connect_offset)) {
705 /*XXX copy remote name here ??? */
706 hash_info->state = Connecting;
709 hash_info->state = V4NameWait;
711 /* waiting for V4 user name */
712 }else if ( hash_info->state == V4UserNameWait){
714 if (check_col(pinfo->cinfo, COL_INFO))
715 col_append_str(pinfo->cinfo, COL_INFO, " Connect Request (User name)");
717 hash_info->v4_user_name_row = get_packet_ptr;
718 /*XXX may need to check for domain name here */
719 hash_info->state = Connecting;
721 /* waiting for V4 domain name */
722 else if ( hash_info->state == V4NameWait){
724 hash_info->v4_name_row = get_packet_ptr;
725 hash_info->state = Connecting;
728 else if ( hash_info->state == Connecting){
730 if (check_col(pinfo->cinfo, COL_INFO))
731 col_append_str(pinfo->cinfo, COL_INFO, " Connect Response");
733 /* save packet pointer */
734 hash_info->cmd_reply_row = get_packet_ptr;
735 hash_info->state = Done; /* change state */
745 state_machine_v5( socks_hash_entry_t *hash_info, tvbuff_t *tvb,
746 int offset, packet_info *pinfo) {
748 /* Decode V5 protocol. This is done on the first pass through the */
749 /* list. Based upon the current state, decode the packet and determine */
750 /* what the next state should be. If we had per packet information, */
751 /* this would be the place to load them up. */
756 if ( hash_info->state == None) {
758 if (check_col(pinfo->cinfo, COL_INFO))
759 col_append_str(pinfo->cinfo, COL_INFO, " Connect to server request");
761 hash_info->state = Connecting; /* change state */
762 hash_info->connect_row = get_packet_ptr;
764 temp = tvb_get_guint8(tvb, offset + 1);
765 /* skip past auth methods */
766 offset = hash_info->connect_offset = offset + 1 + temp;
768 else if ( hash_info->state == Connecting){
770 guint AuthMethod = tvb_get_guint8(tvb, offset + 1);
772 if (check_col(pinfo->cinfo, COL_INFO))
773 col_append_str(pinfo->cinfo, COL_INFO, " Connect to server response");
775 hash_info->auth_method_row = get_packet_ptr;
777 if ( AuthMethod == NO_AUTHENTICATION)
778 hash_info->state = V5Command;
780 else if ( AuthMethod == USER_NAME_AUTHENTICATION)
781 hash_info->state = UserNameAuth;
783 else if ( AuthMethod == GSS_API_AUTHENTICATION)
784 /*XXX should be this hash_info->state = GssApiAuth; */
785 hash_info->state = Done;
787 else hash_info->state = Done; /*Auth failed or error*/
791 else if ( hash_info->state == V5Command) { /* Handle V5 Command */
795 hash_info->command = tvb_get_guint8(tvb, offset + 1); /* get command */
797 if (check_col(pinfo->cinfo, COL_INFO))
798 col_append_fstr(pinfo->cinfo, COL_INFO, " Command Request - %s",
799 get_command_name(hash_info->command));
801 hash_info->state = V5Reply;
802 hash_info->command_row = get_packet_ptr;
804 offset += 3; /* skip to address type */
806 offset = get_address_v5(tvb, offset, hash_info);
808 temp = tvb_get_guint8(tvb, offset);
810 if (( hash_info->command == CONNECT_COMMAND) ||
811 ( hash_info->command == UDP_ASSOCIATE_COMMAND))
812 /* get remote port */
813 hash_info->port = tvb_get_ntohs(tvb, offset);
816 else if ( hash_info->state == V5Reply) { /* V5 Command Reply */
819 if (check_col(pinfo->cinfo, COL_INFO))
820 col_append_fstr(pinfo->cinfo, COL_INFO, " Command Response - %s",
821 get_command_name(hash_info->command));
823 hash_info->cmd_reply_row = get_packet_ptr;
825 if (( hash_info->command == CONNECT_COMMAND) ||
826 (hash_info->command == PING_COMMAND) ||
827 (hash_info->command == TRACERT_COMMAND))
828 hash_info->state = Done;
830 else if ( hash_info->command == BIND_COMMAND)
831 hash_info->state = V5BindReply;
833 else if ( hash_info->command == UDP_ASSOCIATE_COMMAND){
834 offset += 3; /* skip to address type */
835 offset = get_address_v5(tvb, offset, hash_info);
837 /* save server udp port and create udp conversation */
838 hash_info->udp_port = tvb_get_ntohs(tvb, offset);
840 if (!pinfo->fd->flags.visited)
841 new_udp_conversation( hash_info, pinfo);
843 /*XXX may need else statement to handle unknows and generate error message */
847 else if ( hash_info->state == V5BindReply) { /* V5 Bind Second Reply */
849 if (check_col(pinfo->cinfo, COL_INFO))
850 col_append_str(pinfo->cinfo, COL_INFO, " Command Response: Bind remote host info");
852 hash_info->bind_reply_row = get_packet_ptr;
853 hash_info->state = Done;
855 else if ( hash_info->state == UserNameAuth) { /* Handle V5 User Auth*/
856 if (check_col(pinfo->cinfo, COL_INFO))
857 col_append_str(pinfo->cinfo, COL_INFO,
858 " User authentication response");
860 hash_info->user_name_auth_row = get_packet_ptr;
861 hash_info->state = AuthReply;
864 else if ( hash_info->state == AuthReply){ /* V5 User Auth reply */
865 hash_info->cmd_reply_row = get_packet_ptr;
866 if (check_col(pinfo->cinfo, COL_INFO))
867 col_append_str(pinfo->cinfo, COL_INFO, " User authentication reply");
868 hash_info->state = V5Command;
875 display_ping_and_tracert(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, socks_hash_entry_t *hash_info) {
877 /* Display the ping/trace_route conversation */
880 const u_char *data, *dataend;
881 const u_char *lineend, *eol;
884 /* handle the end command */
885 if ( pinfo->destport == TCP_PORT_SOCKS){
886 if (check_col(pinfo->cinfo, COL_INFO))
887 col_append_str(pinfo->cinfo, COL_INFO, ", Terminate Request");
890 proto_tree_add_text(tree, tvb, offset, 1,
891 (hash_info->command == PING_COMMAND) ?
892 "Ping: End command" :
893 "Traceroute: End command");
895 else{ /* display the PING or Traceroute results */
896 if (check_col(pinfo->cinfo, COL_INFO))
897 col_append_str(pinfo->cinfo, COL_INFO, ", Results");
900 proto_tree_add_text(tree, tvb, offset, -1,
901 (hash_info->command == PING_COMMAND) ?
903 "Traceroute Results");
905 data = tvb_get_ptr(tvb, offset, -1);
906 dataend = data + tvb_length_remaining(tvb, offset);
908 while (data < dataend) {
910 lineend = find_line_end(data, dataend, &eol);
911 linelen = lineend - data;
913 proto_tree_add_text( tree, tvb, offset, linelen,
914 format_text(data, linelen));
924 static void call_next_dissector(tvbuff_t *tvb, int offset, packet_info *pinfo,
925 proto_tree *tree, socks_hash_entry_t *hash_info) {
927 /* Display the results for PING and TRACERT extensions or */
928 /* Call TCP dissector for the port that was passed during the */
929 /* connect process */
930 /* Load pointer to pinfo->XXXport depending upon the direction, */
931 /* change pinfo port to the remote port, call next dissecotr to decode */
932 /* the payload, and restore the pinfo port after that is done. */
936 if (( hash_info->command == PING_COMMAND) ||
937 ( hash_info->command == TRACERT_COMMAND))
939 display_ping_and_tracert(tvb, offset, pinfo, tree, hash_info);
941 else { /* call the tcp port decoder to handle the payload */
943 /*XXX may want to load dest address here */
945 if ( pinfo->destport == TCP_PORT_SOCKS)
946 ptr = &pinfo->destport;
948 ptr = &pinfo->srcport;
950 *ptr = hash_info->port;
951 decode_tcp_ports( tvb, offset, pinfo, tree, pinfo->srcport, pinfo->destport);
952 *ptr = TCP_PORT_SOCKS;
959 dissect_socks(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) {
962 proto_tree *socks_tree;
964 socks_hash_entry_t *hash_info;
965 conversation_t *conversation;
967 conversation = find_conversation( &pinfo->src, &pinfo->dst, pinfo->ptype,
968 pinfo->srcport, pinfo->destport, 0);
971 conversation = conversation_new( &pinfo->src, &pinfo->dst, pinfo->ptype,
972 pinfo->srcport, pinfo->destport, 0);
974 hash_info = conversation_get_proto_data(conversation,proto_socks);
976 hash_info = g_mem_chunk_alloc(socks_vals);
977 hash_info->start_done_row = G_MAXINT;
978 hash_info->state = None;
980 hash_info->version = tvb_get_guint8(tvb, offset); /* get version*/
982 if (( hash_info->version != 4) && /* error test version */
983 ( hash_info->version != 5))
984 hash_info->state = Done;
986 conversation_add_proto_data(conversation, proto_socks,
989 /* set dissector for now */
990 conversation_set_dissector(conversation, socks_handle);
993 /* display summary window information */
995 if (check_col(pinfo->cinfo, COL_PROTOCOL))
996 col_set_str(pinfo->cinfo, COL_PROTOCOL, "Socks");
998 if (check_col(pinfo->cinfo, COL_INFO)){
999 if (( hash_info->version == 4) || ( hash_info->version == 5)){
1000 col_add_fstr(pinfo->cinfo, COL_INFO, "Version: %d",
1001 hash_info->version);
1003 else /* unknown version display error */
1004 col_set_str(pinfo->cinfo, COL_INFO, "Unknown");
1007 if ( hash_info->command == PING_COMMAND)
1008 col_append_str(pinfo->cinfo, COL_INFO, ", Ping Req");
1009 if ( hash_info->command == TRACERT_COMMAND)
1010 col_append_str(pinfo->cinfo, COL_INFO, ", Traceroute Req");
1012 /*XXX if ( hash_info->port != -1) */
1013 if ( hash_info->port != 0)
1014 col_append_fstr(pinfo->cinfo, COL_INFO, ", Remote Port: %d",
1019 /* run state machine if needed */
1021 if ((hash_info->state != Done) && ( !pinfo->fd->flags.visited)){
1023 if ( hash_info->version == 4)
1024 state_machine_v4( hash_info, tvb, offset, pinfo);
1026 else if ( hash_info->version == 5)
1027 state_machine_v5( hash_info, tvb, offset, pinfo);
1029 if (hash_info->state == Done) { /* if done now */
1030 hash_info->start_done_row = pinfo->fd->num;
1034 /* if proto tree, decode and display */
1037 ti = proto_tree_add_item( tree, proto_socks, tvb, offset, -1,
1040 socks_tree = proto_item_add_subtree(ti, ett_socks);
1042 if ( hash_info->version == 4)
1043 display_socks_v4(tvb, offset, pinfo, socks_tree,
1046 else if ( hash_info->version == 5)
1047 display_socks_v5(tvb, offset, pinfo, socks_tree,
1050 /* if past startup, add the faked stuff */
1051 if ( pinfo->fd->num > hash_info->start_done_row){
1052 /* add info to tree */
1053 proto_tree_add_text( socks_tree, tvb, offset, 0,
1054 "Command: %d (%s)", hash_info->command,
1055 get_command_name(hash_info->command));
1057 proto_tree_add_ipv4( socks_tree, hf_socks_ip_dst, tvb,
1058 offset, 0, hash_info->dst_addr);
1060 /* no fake address for ping & traceroute */
1062 if (( hash_info->command != PING_COMMAND) &&
1063 ( hash_info->command != TRACERT_COMMAND)){
1064 proto_tree_add_uint( socks_tree, hf_socks_dstport, tvb,
1065 offset, 0, hash_info->port);
1072 /* call next dissector if ready */
1074 if ( pinfo->fd->num > hash_info->start_done_row){
1075 call_next_dissector(tvb, offset, pinfo, tree, hash_info);
1081 static void socks_reinit( void){
1083 /* Do the cleanup work when a new pass through the packet list is */
1084 /* performed. Reset the highest row seen counter and re-initialize the */
1085 /* conversation memory chunks. */
1088 g_mem_chunk_destroy(socks_vals);
1090 socks_vals = g_mem_chunk_new("socks_vals", socks_hash_val_length,
1091 socks_hash_init_count * socks_hash_val_length,
1097 proto_register_socks( void){
1099 /*** Prep the socks protocol, register it and a initialization routine */
1100 /* to clear the hash stuff. */
1103 static gint *ett[] = {
1110 static hf_register_info hf[] = {
1114 { "Version", "socks.version", FT_UINT8, BASE_DEC, NULL,
1119 { "Remote Address", "socks.dst", FT_IPv4, BASE_NONE, NULL,
1123 { &hf_socks_ip6_dst,
1124 { "Remote Address(ipv6)", "socks.dstV6", FT_IPv6, BASE_NONE, NULL,
1130 { "User Name", "socks.username", FT_STRING, BASE_NONE,
1131 NULL, 0x0, "", HFILL
1134 { &hf_socks_dstport,
1135 { "Remote Port", "socks.dstport", FT_UINT16,
1136 BASE_DEC, NULL, 0x0, "", HFILL
1140 { "Command", "socks.command", FT_UINT8,
1141 BASE_DEC, VALS(cmd_strings), 0x0, "", HFILL
1144 { &hf_socks_results_4,
1145 { "Results(V4)", "socks.results_v4", FT_UINT8,
1146 BASE_DEC, VALS(reply_table_v4), 0x0, "", HFILL
1149 { &hf_socks_results_5,
1150 { "Results(V5)", "socks.results_v5", FT_UINT8,
1151 BASE_DEC, VALS(reply_table_v5), 0x0, "", HFILL
1154 { &hf_socks_results,
1155 { "Results(V5)", "socks.results", FT_UINT8,
1156 BASE_DEC, NULL, 0x0, "", HFILL
1163 proto_socks = proto_register_protocol (
1164 "Socks Protocol", "Socks", "socks");
1166 proto_register_field_array(proto_socks, hf, array_length(hf));
1167 proto_register_subtree_array(ett, array_length(ett));
1169 register_init_routine( &socks_reinit); /* register re-init routine */
1171 socks_udp_handle = create_dissector_handle(socks_udp_dissector,
1173 socks_handle = create_dissector_handle(dissect_socks, proto_socks);
1178 proto_reg_handoff_socks(void) {
1180 /* dissector install routine */
1182 dissector_add("tcp.port", TCP_PORT_SOCKS, socks_handle);