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.32 2001/12/10 00:25:36 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"
76 #include "alignment.h"
77 #include "conversation.h"
79 #include "packet-tcp.h"
80 #include "packet-udp.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_command = -1;
125 /************* Dissector handles ***********/
127 static dissector_handle_t socks_handle;
128 static dissector_handle_t socks_udp_handle;
130 /************* State Machine names ***********/
155 guint32 udp_remote_port;
158 row_pointer_type v4_name_row;
159 row_pointer_type v4_user_name_row;
160 row_pointer_type connect_row;
161 row_pointer_type cmd_reply_row;
162 row_pointer_type bind_reply_row;
163 row_pointer_type command_row;
164 row_pointer_type auth_method_row;
165 row_pointer_type user_name_auth_row;
166 guint32 start_done_row;
168 guint32 dst_addr; /* this needs to handle IPv6 */
174 static char *address_type_table[] = {
184 /* String table for the V4 reply status messages */
186 static char *reply_table_v4[] = {
188 "Rejected or Failed",
189 "Rejected because SOCKS server cannot connect to identd on the client",
190 "Rejected because the client program and identd report different user-ids",
195 /* String table for the V5 reply status messages */
197 static char *reply_table_v5[] = {
199 "General SOCKS server failure",
200 "Connection not allowed by ruleset",
201 "Network unreachable",
203 "Connection refused",
205 "Command not supported",
206 "Address type not supported",
211 #define socks_hash_init_count 20
212 #define socks_hash_val_length (sizeof(socks_hash_entry_t))
214 static GMemChunk *socks_vals = NULL;
217 /************************* Support routines ***************************/
220 static int display_string(tvbuff_t *tvb, int offset, packet_info *pinfo,
221 proto_tree *tree, char *label){
223 /* display a string with a length, characters encoding */
224 /* they are displayed under a tree with the name in Label variable */
225 /* return the length of the string and the length byte */
228 proto_tree *name_tree;
233 int length = tvb_get_guint8(tvb, offset);
235 strncpy( temp, tvb_get_ptr(tvb, offset+1, -1), length);
238 ti = proto_tree_add_text(tree, tvb, offset, length + 1,
239 "%s: %s" , label, temp);
242 name_tree = proto_item_add_subtree(ti, ett_socks_name);
244 proto_tree_add_text( name_tree, tvb, offset, 1, "Length: %d", length);
248 proto_tree_add_text( name_tree, tvb, offset, length, "String: %s", temp);
255 static char *get_auth_method_name( guint Number){
257 /* return the name of the authenication method */
259 if ( Number == 0) return "No authentication";
260 if ( Number == 1) return "GSSAPI";
261 if ( Number == 2) return "Username/Password";
262 if ( Number == 3) return "Chap";
263 if (( Number >= 4) && ( Number <= 0x7f))return "IANA assigned";
264 if (( Number >= 0x80) && ( Number <= 0xfe)) return "private method";
265 if ( Number == 0xff) return "no acceptable method";
267 /* shouldn't reach here */
269 return "Bad method number (not 0-0xff)";
273 static char *get_command_name( guint Number){
275 /* return the name of the command as a string */
277 if ( Number == 0) return "Unknow";
278 if ( Number == 1) return "Connect";
279 if ( Number == 2) return "Bind";
280 if ( Number == 3) return "UdpAssociate";
281 if ( Number == 0x80) return "Ping";
282 if ( Number == 0x81) return "Traceroute";
287 static int display_address(tvbuff_t *tvb, int offset,
288 packet_info *pinfo, proto_tree *tree) {
290 /* decode and display the v5 address, return offset of next byte */
292 int a_type = tvb_get_guint8(tvb, offset);
294 proto_tree_add_text( tree, tvb, offset, 1,
295 "Address Type: %d (%s)", a_type,
296 address_type_table[ MIN( (guint) a_type,
297 array_length( address_type_table)-1) ]);
301 if ( a_type == 1){ /* IPv4 address */
302 proto_tree_add_item( tree, hf_socks_ip_dst, tvb, offset,
306 else if ( a_type == 3){ /* domain name address */
308 offset += display_string(tvb, offset, pinfo, tree,
311 else if ( a_type == 4){ /* IPv6 address */
312 proto_tree_add_item( tree, hf_socks_ip6_dst, tvb, offset,
321 static int get_address_v5(tvbuff_t *tvb, int offset,
322 socks_hash_entry_t *hash_info) {
324 /* decode the v5 address and return offset of next byte */
325 /*XXX this needs to handle IPV6 and domain name addresses */
328 int a_type = tvb_get_guint8(tvb, offset++);
330 if ( a_type == 1){ /* IPv4 address */
333 tvb_memcpy(tvb, (guint8 *)&hash_info->dst_addr,
338 else if ( a_type == 4) /* IPv6 address */
341 else if ( a_type == 3) /* domain name address */
342 offset += tvb_get_guint8(tvb, offset) + 1;
347 /********************* V5 UDP Associate handlers ***********************/
350 socks_udp_dissector(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) {
352 /* Conversation dissector called from UDP dissector. Decode and display */
353 /* the socks header, the pass the rest of the data to the udp port */
354 /* decode routine to handle the payload. */
358 socks_hash_entry_t *hash_info;
359 conversation_t *conversation;
360 proto_tree *socks_tree;
363 conversation = find_conversation( &pinfo->src, &pinfo->dst, pinfo->ptype,
364 pinfo->srcport, pinfo->destport, 0);
366 g_assert( conversation); /* should always find a conversation */
368 hash_info = conversation_get_proto_data(conversation, proto_socks);
370 if (check_col(pinfo->cinfo, COL_PROTOCOL))
371 col_set_str(pinfo->cinfo, COL_PROTOCOL, "Socks");
373 if (check_col(pinfo->cinfo, COL_INFO))
374 col_add_fstr(pinfo->cinfo, COL_INFO, "Version: 5, UDP Associated packet");
377 ti = proto_tree_add_protocol_format( tree, proto_socks, tvb, offset,
378 tvb_length_remaining(tvb, offset), "Socks" );
380 socks_tree = proto_item_add_subtree(ti, ett_socks);
382 proto_tree_add_text( socks_tree, tvb, offset, 2, "Reserved");
385 proto_tree_add_text( socks_tree, tvb, offset, 1, "Fragment Number: %d", tvb_get_guint8(tvb, offset));
389 offset = display_address( tvb, offset, pinfo, socks_tree);
390 hash_info->udp_remote_port = tvb_get_ntohs(tvb, offset);
392 proto_tree_add_uint( socks_tree, hf_socks_dstport, tvb,
393 offset, 2, hash_info->udp_remote_port);
397 else { /* no tree, skip past the socks header */
399 offset = get_address_v5( tvb, offset, 0) + 2;
403 /* set pi src/dst port and call the udp sub-dissector lookup */
405 if ( pinfo->srcport == hash_info->port)
406 ptr = &pinfo->destport;
408 ptr = &pinfo->srcport;
410 *ptr = hash_info->udp_remote_port;
412 decode_udp_ports( tvb, offset, pinfo, tree, pinfo->srcport, pinfo->destport);
414 *ptr = hash_info->udp_port;
420 new_udp_conversation( socks_hash_entry_t *hash_info, packet_info *pinfo){
422 conversation_t *conversation = conversation_new( &pinfo->src, &pinfo->dst, PT_UDP,
423 hash_info->udp_port, hash_info->port, 0);
425 g_assert( conversation);
427 conversation_add_proto_data(conversation, proto_socks, hash_info);
428 conversation_set_dissector(conversation, socks_udp_handle);
434 /**************** Protocol Tree Display routines ******************/
437 display_socks_v4(tvbuff_t *tvb, int offset, packet_info *pinfo,
438 proto_tree *parent, proto_tree *tree, socks_hash_entry_t *hash_info) {
441 /* Display the protocol tree for the V5 version. This routine uses the */
442 /* stored conversation information to decide what to do with the row. */
443 /* Per packet information would have been better to do this, but we */
444 /* didn't have that when I wrote this. And I didn't expect this to get */
450 /* Display command from client */
451 if (compare_packet( hash_info->connect_row)){
453 proto_tree_add_text( tree, tvb, offset, 1,
454 "Version: %u ", hash_info->version);
456 command = tvb_get_guint8(tvb, offset);
458 proto_tree_add_text( tree, tvb, offset, 1,
459 "Command: %u (%s)", command,
460 get_command_name( command));
464 proto_tree_add_item( tree, hf_socks_dstport, tvb, offset, 2,
468 /* Do destination address */
469 proto_tree_add_item( tree, hf_socks_ip_dst, tvb, offset,
474 /*XXX check this, needs to do length checking */
475 /* Should perhaps do TCP reassembly as well */
476 if ( tvb_offset_exists(tvb, offset)) {
477 /* display user name */
478 proto_tree_add_string( tree, hf_user_name, tvb, offset,
479 strlen( tvb_get_ptr(tvb, offset, -1)) + 1,
480 tvb_get_ptr(tvb, offset, -1));
484 /*Display command response from server*/
486 else if ( compare_packet( hash_info->cmd_reply_row)){
488 proto_tree_add_text( tree, tvb, offset, 1,
489 "Version: %u (should be 0) ", tvb_get_guint8(tvb, offset));
491 /* Do results code */
492 proto_tree_add_text( tree, tvb, offset, 1,
493 "Result Code: %u (%s)", tvb_get_guint8(tvb, offset) ,
494 reply_table_v4[ MAX(0, MIN( tvb_get_guint8(tvb, offset) - 90, 4))]);
498 proto_tree_add_item( tree, hf_socks_dstport, tvb, offset, 2,
501 /* Do remote address */
502 proto_tree_add_item( tree, hf_socks_ip_dst, tvb, offset, 4,
506 else if ( compare_packet( hash_info->v4_user_name_row)){
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 proto_tree_add_text( tree, tvb, offset,
512 strlen( tvb_get_ptr(tvb, offset, -1)),
513 "User Name: %s", tvb_get_ptr(tvb, offset, -1));
520 display_socks_v5(tvbuff_t *tvb, int offset, packet_info *pinfo,
521 proto_tree *parent, proto_tree *tree, socks_hash_entry_t *hash_info) {
523 /* Display the protocol tree for the version. This routine uses the */
524 /* stored conversation information to decide what to do with the row. */
525 /* Per packet information would have been better to do this, but we */
526 /* didn't have that when I wrote this. And I didn't expect this to get */
529 unsigned int i, command;
534 if (compare_packet( hash_info->connect_row)){
536 proto_tree *AuthTree;
540 proto_tree_add_uint( tree, hf_socks_ver, tvb, offset, 1,
544 temp = tvb_get_guint8(tvb, offset); /* Get Auth method count */
545 /* build auth tree */
546 ti = proto_tree_add_text( tree, tvb, offset, 1,
547 "Client Authentication Methods");
549 AuthTree = proto_item_add_subtree(ti, ett_socks_auth);
551 proto_tree_add_text( AuthTree, tvb, offset, 1,
555 for( i = 0; i < temp; ++i) {
557 AuthMethodStr = get_auth_method_name(
558 tvb_get_guint8( tvb, offset + i));
559 proto_tree_add_text( AuthTree, tvb, offset + i, 1,
560 "Method[%d]: %u (%s)", i,
561 tvb_get_guint8( tvb, offset + i), AuthMethodStr);
564 } /* Get accepted auth method */
565 else if (compare_packet( hash_info->auth_method_row)) {
569 proto_tree_add_text( tree, tvb, offset, 1,
570 "Accepted Auth Method: 0x%0x (%s)", tvb_get_guint8( tvb, offset),
571 get_auth_method_name( tvb_get_guint8( tvb, offset)));
574 } /* handle user/password auth */
575 else if (compare_packet( hash_info->user_name_auth_row)) {
577 proto_tree_add_text( tree, tvb, offset, 1,
578 "Version: %u ", hash_info->version);
580 /* process user name */
581 offset += display_string( tvb, offset, pinfo, tree,
583 /* process password */
584 offset += display_string( tvb, offset, pinfo, tree,
587 /* command to the server */
588 /* command response from server */
589 else if ((compare_packet( hash_info->command_row)) ||
590 (compare_packet( hash_info->cmd_reply_row)) ||
591 (compare_packet( hash_info->bind_reply_row))){
593 proto_tree_add_text( tree, tvb, offset, 1,
594 "Version: %u ", hash_info->version);
598 command = tvb_get_guint8(tvb, offset);
600 if (compare_packet( hash_info->command_row))
601 proto_tree_add_text( tree, tvb, offset, 1, "Command: %u (%s)",
602 command, get_command_name( command));
604 proto_tree_add_text( tree, tvb, offset, 1, "Status: %d (%s)",
605 tvb_get_guint8(tvb, offset), reply_table_v5[ MAX( 0,
606 MIN(tvb_get_guint8(tvb, offset) - 90, 9))]);
609 proto_tree_add_text( tree, tvb, offset, 1,
610 "Reserved: 0x%0x (should = 0x00)", tvb_get_guint8(tvb, offset));
613 offset = display_address(tvb, offset, pinfo, tree);
616 proto_tree_add_text( tree, tvb, offset, 2,
618 (compare_packet( hash_info->bind_reply_row) ?
619 "Remote Host " : ""),
620 tvb_get_ntohs(tvb, offset));
626 /**************** Decoder State Machines ******************/
630 state_machine_v4( socks_hash_entry_t *hash_info, tvbuff_t *tvb,
631 int offset, packet_info *pinfo) {
633 /* Decode V4 protocol. This is done on the first pass through the */
634 /* list. Based upon the current state, decode the packet and determine */
635 /* what the next state should be. If we had per packet information, */
636 /* this would be the place to load them up. */
638 if ( hash_info->state == None) { /* new connection */
640 if (check_col(pinfo->cinfo, COL_INFO))
641 col_append_str(pinfo->cinfo, COL_INFO, " Connect to server request");
643 hash_info->state = Connecting; /* change state */
645 hash_info->command = tvb_get_guint8(tvb, offset + 1);
646 /* get remote port */
647 if ( hash_info->command == CONNECT_COMMAND)
648 hash_info->port = tvb_get_ntohs(tvb, offset + 2);
649 /* get remote address */
651 tvb_memcpy(tvb, (guint8 *)&hash_info->dst_addr, offset + 4, 4);
653 /* save the packet pointer */
654 hash_info->connect_row = get_packet_ptr;
656 /* skip past this stuff */
657 hash_info->connect_offset = offset + 8;
661 if ( !tvb_offset_exists(tvb, offset)) /* if no user name */
663 hash_info->state = V4UserNameWait;
666 hash_info->connect_offset += strlen( tvb_get_ptr(tvb, offset, -1)) + 1;
668 if ( !hash_info->dst_addr){ /* if no dest address */
670 if ( tvb_offset_exists(tvb, hash_info->connect_offset)) {
671 /*XXX copy remote name here ??? */
672 hash_info->state = Connecting;
675 hash_info->state = V4NameWait;
677 /* waiting for V4 user name */
678 }else if ( hash_info->state == V4UserNameWait){
680 if (check_col(pinfo->cinfo, COL_INFO))
681 col_append_str(pinfo->cinfo, COL_INFO, " Connect Request (User name)");
683 hash_info->v4_user_name_row = get_packet_ptr;
684 /*XXX may need to check for domain name here */
685 hash_info->state = Connecting;
687 /* waiting for V4 domain name */
688 else if ( hash_info->state == V4NameWait){
690 hash_info->v4_name_row = get_packet_ptr;
691 hash_info->state = Connecting;
694 else if ( hash_info->state == Connecting){
696 if (check_col(pinfo->cinfo, COL_INFO))
697 col_append_str(pinfo->cinfo, COL_INFO, " Connect Response");
699 /* save packet pointer */
700 hash_info->cmd_reply_row = get_packet_ptr;
701 hash_info->state = Done; /* change state */
711 state_machine_v5( socks_hash_entry_t *hash_info, tvbuff_t *tvb,
712 int offset, packet_info *pinfo) {
714 /* Decode V5 protocol. This is done on the first pass through the */
715 /* list. Based upon the current state, decode the packet and determine */
716 /* what the next state should be. If we had per packet information, */
717 /* this would be the place to load them up. */
722 if ( hash_info->state == None) {
724 if (check_col(pinfo->cinfo, COL_INFO))
725 col_append_str(pinfo->cinfo, COL_INFO, " Connect to server request");
727 hash_info->state = Connecting; /* change state */
728 hash_info->connect_row = get_packet_ptr;
730 temp = tvb_get_guint8(tvb, offset + 1);
731 /* skip past auth methods */
732 offset = hash_info->connect_offset = offset + 1 + temp;
734 else if ( hash_info->state == Connecting){
736 guint AuthMethod = tvb_get_guint8(tvb, offset + 1);
738 if (check_col(pinfo->cinfo, COL_INFO))
739 col_append_str(pinfo->cinfo, COL_INFO, " Connect to server response");
741 hash_info->auth_method_row = get_packet_ptr;
743 if ( AuthMethod == NO_AUTHENTICATION)
744 hash_info->state = V5Command;
746 else if ( AuthMethod == USER_NAME_AUTHENTICATION)
747 hash_info->state = UserNameAuth;
749 else if ( AuthMethod == GSS_API_AUTHENTICATION)
750 /*XXX should be this hash_info->state = GssApiAuth; */
751 hash_info->state = Done;
753 else hash_info->state = Done; /*Auth failed or error*/
757 else if ( hash_info->state == V5Command) { /* Handle V5 Command */
761 hash_info->command = tvb_get_guint8(tvb, offset + 1); /* get command */
763 if (check_col(pinfo->cinfo, COL_INFO))
764 col_append_fstr(pinfo->cinfo, COL_INFO, " Command Request - %s",
765 get_command_name(hash_info->command));
767 hash_info->state = V5Reply;
768 hash_info->command_row = get_packet_ptr;
770 offset += 3; /* skip to address type */
772 offset = get_address_v5(tvb, offset, hash_info);
774 temp = tvb_get_guint8(tvb, offset);
776 if (( hash_info->command == CONNECT_COMMAND) ||
777 ( hash_info->command == UDP_ASSOCIATE_COMMAND))
778 /* get remote port */
779 hash_info->port = tvb_get_ntohs(tvb, offset);
782 else if ( hash_info->state == V5Reply) { /* V5 Command Reply */
785 if (check_col(pinfo->cinfo, COL_INFO))
786 col_append_fstr(pinfo->cinfo, COL_INFO, " Command Response - %s",
787 get_command_name(hash_info->command));
789 hash_info->cmd_reply_row = get_packet_ptr;
791 if (( hash_info->command == CONNECT_COMMAND) ||
792 (hash_info->command == PING_COMMAND) ||
793 (hash_info->command == TRACERT_COMMAND))
794 hash_info->state = Done;
796 else if ( hash_info->command == BIND_COMMAND)
797 hash_info->state = V5BindReply;
799 else if ( hash_info->command == UDP_ASSOCIATE_COMMAND){
800 offset += 3; /* skip to address type */
801 offset = get_address_v5(tvb, offset, hash_info);
803 /* save server udp port and create udp conversation */
804 hash_info->udp_port = tvb_get_ntohs(tvb, offset);
806 if (!pinfo->fd->flags.visited)
807 new_udp_conversation( hash_info, pinfo);
809 /*XXX may need else statement to handle unknows and generate error message */
813 else if ( hash_info->state == V5BindReply) { /* V5 Bind Second Reply */
815 if (check_col(pinfo->cinfo, COL_INFO))
816 col_append_str(pinfo->cinfo, COL_INFO, " Command Response: Bind remote host info");
818 hash_info->bind_reply_row = get_packet_ptr;
819 hash_info->state = Done;
821 else if ( hash_info->state == UserNameAuth) { /* Handle V5 User Auth*/
822 if (check_col(pinfo->cinfo, COL_INFO))
823 col_append_str(pinfo->cinfo, COL_INFO,
824 " User authentication response");
826 hash_info->user_name_auth_row = get_packet_ptr;
827 hash_info->state = AuthReply;
830 else if ( hash_info->state == AuthReply){ /* V5 User Auth reply */
831 hash_info->cmd_reply_row = get_packet_ptr;
832 if (check_col(pinfo->cinfo, COL_INFO))
833 col_append_str(pinfo->cinfo, COL_INFO, " User authentication reply");
834 hash_info->state = V5Command;
841 display_ping_and_tracert(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, socks_hash_entry_t *hash_info) {
843 /* Display the ping/trace_route conversation */
846 const u_char *data, *dataend;
847 const u_char *lineend, *eol;
850 /* handle the end command */
851 if ( pinfo->destport == TCP_PORT_SOCKS){
852 if (check_col(pinfo->cinfo, COL_INFO))
853 col_append_str(pinfo->cinfo, COL_INFO, ", Terminate Request");
856 proto_tree_add_text(tree, tvb, offset, 1,
857 (hash_info->command == PING_COMMAND) ?
858 "Ping: End command" :
859 "Traceroute: End command");
861 else{ /* display the PING or Traceroute results */
862 if (check_col(pinfo->cinfo, COL_INFO))
863 col_append_str(pinfo->cinfo, COL_INFO, ", Results");
866 proto_tree_add_text(tree, tvb, offset,
867 tvb_length_remaining(tvb, offset),
868 (hash_info->command == PING_COMMAND) ?
870 "Traceroute Results");
872 data = tvb_get_ptr(tvb, offset, -1);
873 dataend = data + tvb_length_remaining(tvb, offset);
875 while (data < dataend) {
877 lineend = find_line_end(data, dataend, &eol);
878 linelen = lineend - data;
880 proto_tree_add_text( tree, tvb, offset, linelen,
881 format_text(data, linelen));
891 static void call_next_dissector(tvbuff_t *tvb, int offset, packet_info *pinfo,
892 proto_tree *tree, socks_hash_entry_t *hash_info) {
894 /* Display the results for PING and TRACERT extensions or */
895 /* Call TCP dissector for the port that was passed during the */
896 /* connect process */
897 /* Load pointer to pinfo->XXXport depending upon the direction, */
898 /* change pinfo port to the remote port, call next dissecotr to decode */
899 /* the payload, and restore the pinfo port after that is done. */
903 if (( hash_info->command == PING_COMMAND) ||
904 ( hash_info->command == TRACERT_COMMAND))
906 display_ping_and_tracert(tvb, offset, pinfo, tree, hash_info);
908 else { /* call the tcp port decoder to handle the payload */
910 /*XXX may want to load dest address here */
912 if ( pinfo->destport == TCP_PORT_SOCKS)
913 ptr = &pinfo->destport;
915 ptr = &pinfo->srcport;
917 *ptr = hash_info->port;
918 decode_tcp_ports( tvb, offset, pinfo, tree, pinfo->srcport, pinfo->destport);
919 *ptr = TCP_PORT_SOCKS;
926 dissect_socks(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) {
929 proto_tree *socks_tree;
931 socks_hash_entry_t *hash_info;
932 conversation_t *conversation;
934 conversation = find_conversation( &pinfo->src, &pinfo->dst, pinfo->ptype,
935 pinfo->srcport, pinfo->destport, 0);
938 conversation = conversation_new( &pinfo->src, &pinfo->dst, pinfo->ptype,
939 pinfo->srcport, pinfo->destport, 0);
941 hash_info = conversation_get_proto_data(conversation,proto_socks);
943 hash_info = g_mem_chunk_alloc(socks_vals);
944 hash_info->start_done_row = G_MAXINT;
945 hash_info->state = None;
946 //XX hash_info->port = -1;
948 hash_info->version = tvb_get_guint8(tvb, offset); /* get version*/
950 if (( hash_info->version != 4) && /* error test version */
951 ( hash_info->version != 5))
952 hash_info->state = Done;
954 conversation_add_proto_data(conversation, proto_socks,
957 /* set dissector for now */
958 conversation_set_dissector(conversation, socks_handle);
961 /* display summary window information */
963 if (check_col(pinfo->cinfo, COL_PROTOCOL))
964 col_set_str(pinfo->cinfo, COL_PROTOCOL, "Socks");
966 if (check_col(pinfo->cinfo, COL_INFO)){
967 if (( hash_info->version == 4) || ( hash_info->version == 5)){
968 col_add_fstr(pinfo->cinfo, COL_INFO, "Version: %d",
971 else /* unknown version display error */
972 col_set_str(pinfo->cinfo, COL_INFO, "Unknown");
975 if ( hash_info->command == PING_COMMAND)
976 col_append_str(pinfo->cinfo, COL_INFO, ", Ping Req");
977 if ( hash_info->command == TRACERT_COMMAND)
978 col_append_str(pinfo->cinfo, COL_INFO, ", Traceroute Req");
980 /*XXX if ( hash_info->port != -1) */
981 if ( hash_info->port != 0)
982 col_append_fstr(pinfo->cinfo, COL_INFO, ", Remote Port: %d",
987 /* run state machine if needed */
989 if ((hash_info->state != Done) && ( !pinfo->fd->flags.visited)){
991 if ( hash_info->version == 4)
992 state_machine_v4( hash_info, tvb, offset, pinfo);
994 else if ( hash_info->version == 5)
995 state_machine_v5( hash_info, tvb, offset, pinfo);
997 if (hash_info->state == Done) { /* if done now */
998 hash_info->start_done_row = pinfo->fd->num;
1002 /* if proto tree, decode and display */
1005 ti = proto_tree_add_item( tree, proto_socks, tvb, offset,
1006 tvb_length_remaining(tvb, offset), FALSE );
1008 socks_tree = proto_item_add_subtree(ti, ett_socks);
1010 if ( hash_info->version == 4)
1011 display_socks_v4(tvb, offset, pinfo, tree, socks_tree,
1014 else if ( hash_info->version == 5)
1015 display_socks_v5(tvb, offset, pinfo, tree, socks_tree,
1018 /* if past startup, add the faked stuff */
1019 if ( pinfo->fd->num > hash_info->start_done_row){
1020 /* add info to tree */
1021 proto_tree_add_text( socks_tree, tvb, offset, 0,
1022 "Command: %d (%s)", hash_info->command,
1023 get_command_name(hash_info->command));
1025 proto_tree_add_ipv4( socks_tree, hf_socks_ip_dst, tvb,
1026 offset, 0, hash_info->dst_addr);
1028 /* no fake address for ping & traceroute */
1030 if (( hash_info->command != PING_COMMAND) &&
1031 ( hash_info->command != TRACERT_COMMAND)){
1032 proto_tree_add_uint( socks_tree, hf_socks_dstport, tvb,
1033 offset, 0, hash_info->port);
1040 /* call next dissector if ready */
1042 if ( pinfo->fd->num > hash_info->start_done_row){
1043 call_next_dissector(tvb, offset, pinfo, tree, hash_info);
1049 static void socks_reinit( void){
1051 /* Do the cleanup work when a new pass through the packet list is */
1052 /* performed. Reset the highest row seen counter and re-initialize the */
1053 /* conversation memory chunks. */
1056 g_mem_chunk_destroy(socks_vals);
1058 socks_vals = g_mem_chunk_new("socks_vals", socks_hash_val_length,
1059 socks_hash_init_count * socks_hash_val_length,
1065 proto_register_socks( void){
1067 /*** Prep the socks protocol, register it and a initialization routine */
1068 /* to clear the hash stuff. */
1071 static gint *ett[] = {
1078 static hf_register_info hf[] = {
1082 { "Version", "socks.ver", FT_UINT8, BASE_DEC, NULL,
1087 { "Remote Address", "socks.dst", FT_IPv4, BASE_NONE, NULL,
1091 { &hf_socks_ip6_dst,
1092 { "Remote Address", "socks.dstV6", FT_IPv6, BASE_NONE, NULL,
1098 { "User Name", "socks.username", FT_STRING, BASE_NONE,
1099 NULL, 0x0, "", HFILL
1102 { &hf_socks_dstport,
1103 { "Remote Port", "socks.dstport", FT_UINT16,
1104 BASE_DEC, NULL, 0x0, "", HFILL
1107 { &hf_socks_command,
1108 { "Command", "socks.command", FT_UINT16,
1109 BASE_DEC, NULL, 0x0, "", HFILL
1116 proto_socks = proto_register_protocol (
1117 "Socks Protocol", "Socks", "socks");
1119 proto_register_field_array(proto_socks, hf, array_length(hf));
1120 proto_register_subtree_array(ett, array_length(ett));
1122 register_init_routine( &socks_reinit); /* register re-init routine */
1124 socks_udp_handle = create_dissector_handle(socks_udp_dissector,
1126 socks_handle = create_dissector_handle(dissect_socks, proto_socks);
1131 proto_reg_handoff_socks(void) {
1133 /* dissector install routine */
1135 dissector_add("tcp.port", TCP_PORT_SOCKS, socks_handle);