Main Page | Modules | Class List | Directories | File List | Class Members | File Members | Related Pages

sp_tcp_ack_check.c

Go to the documentation of this file.
00001 /*
00002 ** Copyright (C) 1998-2002 Martin Roesch <roesch@sourcefire.com>
00003 **
00004 ** This program is free software; you can redistribute it and/or modify
00005 ** it under the terms of the GNU General Public License as published by
00006 ** the Free Software Foundation; either version 2 of the License, or
00007 ** (at your option) any later version.
00008 **
00009 ** This program is distributed in the hope that it will be useful,
00010 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 ** GNU General Public License for more details.
00013 **
00014 ** You should have received a copy of the GNU General Public License
00015 ** along with this program; if not, write to the Free Software
00016 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017 */
00018 
00019 /* $Id$ */
00020 
00021 #ifdef HAVE_CONFIG_H
00022 #include "config.h"
00023 #endif
00024 
00025 #include <sys/types.h>
00026 #include <stdlib.h>
00027 #include <ctype.h>
00028 
00029 #include "rules.h"
00030 #include "decode.h"
00031 #include "plugbase.h"
00032 #include "parser.h"
00033 #include "debug.h"
00034 #include "util.h"
00035 #include "plugin_enum.h"
00036 
00037 
00038 typedef struct _TcpAckCheckData
00039 {
00040     u_long tcp_ack;
00041 } TcpAckCheckData;
00042 
00043 void TcpAckCheckInit(char *, OptTreeNode *, int);
00044 void ParseTcpAck(char *, OptTreeNode *);
00045 int CheckTcpAckEq(Packet *, struct _OptTreeNode *, OptFpList *);
00046 
00047 
00048 
00049 /****************************************************************************
00050  * 
00051  * Function: SetupTcpAckCheck()
00052  *
00053  * Purpose: Link the ack keyword to the initialization function
00054  *
00055  * Arguments: None.
00056  *
00057  * Returns: void function
00058  *
00059  ****************************************************************************/
00060 void SetupTcpAckCheck(void)
00061 {
00062     /* map the keyword to an initialization/processing function */
00063     RegisterPlugin("ack", TcpAckCheckInit);
00064     DEBUG_WRAP(DebugMessage(DEBUG_PLUGIN, "Plugin: TcpAckCheck Initialized\n"););
00065 }
00066 
00067 
00068 /****************************************************************************
00069  * 
00070  * Function: TcpAckCheckInit(char *, OptTreeNode *)
00071  *
00072  * Purpose: Attach the option data to the rule data struct and link in the
00073  *          detection function to the function pointer list.
00074  *
00075  * Arguments: data => rule arguments/data
00076  *            otn => pointer to the current rule option list node
00077  *
00078  * Returns: void function
00079  *
00080  ****************************************************************************/
00081 void TcpAckCheckInit(char *data, OptTreeNode *otn, int protocol)
00082 {
00083 
00084     if(protocol != IPPROTO_TCP)
00085     {
00086         FatalError("%s(%d) TCP Options on non-TCP rule\n", file_name, file_line);
00087     }
00088 
00089     /* multiple declaration check */ 
00090     if(otn->ds_list[PLUGIN_TCP_ACK_CHECK])
00091     {
00092         FatalError("%s(%d): Multiple TCP ack options in rule\n", file_name,
00093                 file_line);
00094     }
00095 
00096     /* allocate the data structure and attach it to the
00097        rule's data struct list */
00098     otn->ds_list[PLUGIN_TCP_ACK_CHECK] = (TcpAckCheckData *)
00099             SnortAlloc(sizeof(TcpAckCheckData));
00100 
00101     /* this is where the keyword arguments are processed and placed into the 
00102        rule option's data structure */
00103     ParseTcpAck(data, otn);
00104 
00105     /* finally, attach the option's detection function to the rule's 
00106        detect function pointer list */
00107     AddOptFuncToList(CheckTcpAckEq, otn);
00108 }
00109 
00110 
00111 
00112 /****************************************************************************
00113  * 
00114  * Function: ParseTcpAck(char *, OptTreeNode *)
00115  *
00116  * Purpose: Attach the option rule's argument to the data struct.
00117  *
00118  * Arguments: data => argument data
00119  *            otn => pointer to the current rule's OTN
00120  *
00121  * Returns: void function
00122  *
00123  ****************************************************************************/
00124 void ParseTcpAck(char *data, OptTreeNode *otn)
00125 {
00126     TcpAckCheckData *ds_ptr;  /* data struct pointer */
00127     char **ep = NULL;
00128 
00129     /* set the ds pointer to make it easier to reference the option's
00130        particular data struct */
00131     ds_ptr = otn->ds_list[PLUGIN_TCP_ACK_CHECK];
00132 
00133     ds_ptr->tcp_ack = strtoul(data, ep, 0);
00134     ds_ptr->tcp_ack = htonl(ds_ptr->tcp_ack);
00135 
00136     DEBUG_WRAP(DebugMessage(DEBUG_PLUGIN,"Ack set to %lX\n", ds_ptr->tcp_ack););
00137 }
00138 
00139 
00140 /****************************************************************************
00141  * 
00142  * Function: CheckTcpAckEq(char *, OptTreeNode *)
00143  *
00144  * Purpose: Check to see if the packet's TCP ack field is equal to the rule
00145  *          ack value.
00146  *
00147  * Arguments: data => argument data
00148  *            otn => pointer to the current rule's OTN
00149  *
00150  * Returns: void function
00151  *
00152  ****************************************************************************/
00153 int CheckTcpAckEq(Packet *p, struct _OptTreeNode *otn, OptFpList *fp_list)
00154 {
00155     if(!p->tcph)
00156         return 0; /* if error appeared when tcp header was processed,
00157                * test fails automagically */
00158     if(((TcpAckCheckData *)otn->ds_list[PLUGIN_TCP_ACK_CHECK])->tcp_ack == p->tcph->th_ack)
00159     {
00160         /* call the next function in the function list recursively */
00161         return fp_list->next->OptTestFunc(p, otn, fp_list->next);
00162     }
00163     else
00164     {
00165         /* you can put debug comments here or not */
00166         DEBUG_WRAP(DebugMessage(DEBUG_PLUGIN,"No match\n"););
00167     }
00168 
00169     /* if the test isn't successful, return 0 */
00170     return 0;
00171 }

Generated on Sun May 14 14:51:13 2006 by  doxygen 1.4.2