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

sp_ipoption_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 #ifdef HAVE_CONFIG_H
00021 #include "config.h"
00022 #endif
00023 
00024 #include <sys/types.h>
00025 #include <stdlib.h>
00026 #include <ctype.h>
00027 #include <string.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 _IpOptionData
00039 {
00040     u_char ip_option;
00041     u_char any_flag;
00042 
00043 } IpOptionData;
00044 
00045 void IpOptionInit(char *, OptTreeNode *, int);
00046 void ParseIpOptionData(char *, OptTreeNode *);
00047 int CheckIpOptions(Packet *, struct _OptTreeNode *, OptFpList *);
00048 
00049 /****************************************************************************
00050  * 
00051  * Function: SetupTemplate()
00052  *
00053  * Purpose: Generic detection engine plugin template.  Registers the
00054  *          configuration function and links it to a rule keyword.
00055  *
00056  * Arguments: None.
00057  *
00058  * Returns: void function
00059  *
00060  ****************************************************************************/
00061 void SetupIpOptionCheck(void)
00062 {
00063     /* map the keyword to an initialization/processing function */
00064     RegisterPlugin("ipopts", IpOptionInit);
00065     DEBUG_WRAP(DebugMessage(DEBUG_PLUGIN,"Plugin: IpOptionCheck Initialized\n"););
00066 }
00067 
00068 
00069 /****************************************************************************
00070  * 
00071  * Function: TemplateInit(char *, OptTreeNode *)
00072  *
00073  * Purpose: Generic rule configuration function.  Handles parsing the rule 
00074  *          information and attaching the associated detection function to
00075  *          the OTN.
00076  *
00077  * Arguments: data => rule arguments/data
00078  *            otn => pointer to the current rule option list node
00079  *
00080  * Returns: void function
00081  *
00082  ****************************************************************************/
00083 void IpOptionInit(char *data, OptTreeNode *otn, int protocol)
00084 {
00085     /* multiple declaration check */ 
00086     if(otn->ds_list[PLUGIN_IPOPTION_CHECK])
00087     {
00088         FatalError("%s(%d): Multiple ipopts options in rule\n", file_name,
00089                 file_line);
00090     }
00091 
00092     /* allocate the data structure and attach it to the
00093        rule's data struct list */
00094     otn->ds_list[PLUGIN_IPOPTION_CHECK] = (IpOptionData *)
00095             SnortAlloc(sizeof(IpOptionData));
00096 
00097     /* this is where the keyword arguments are processed and placed into the 
00098        rule option's data structure */
00099     ParseIpOptionData(data, otn);
00100 
00101     /* finally, attach the option's detection function to the rule's 
00102        detect function pointer list */
00103     AddOptFuncToList(CheckIpOptions, otn);
00104 }
00105 
00106 
00107 
00108 /****************************************************************************
00109  * 
00110  * Function: TemplateRuleParseFunction(char *, OptTreeNode *)
00111  *
00112  * Purpose: This is the function that is used to process the option keyword's
00113  *          arguments and attach them to the rule's data structures.
00114  *
00115  * Arguments: data => argument data
00116  *            otn => pointer to the current rule's OTN
00117  *
00118  * Returns: void function
00119  *
00120  ****************************************************************************/
00121 void ParseIpOptionData(char *data, OptTreeNode *otn)
00122 {
00123     IpOptionData *ds_ptr;  /* data struct pointer */
00124 
00125     /* set the ds pointer to make it easier to reference the option's
00126        particular data struct */
00127     ds_ptr = otn->ds_list[PLUGIN_IPOPTION_CHECK];
00128 
00129     if(data == NULL)
00130     {
00131         FatalError("%s(%d): IP Option keyword missing argument!\n", file_name, file_line);
00132     }
00133 
00134     while(isspace((u_char)*data))
00135         data++; 
00136 
00137 
00138     if(!strncasecmp(data, "rr", 2))
00139     {
00140         ds_ptr->ip_option = IPOPT_RR;
00141         return;
00142     }
00143     else if(!strncasecmp(data, "eol", 3))
00144     {
00145         ds_ptr->ip_option = IPOPT_EOL;
00146         return;
00147     }
00148     else if(!strncasecmp(data, "nop", 3))
00149     {
00150         ds_ptr->ip_option = IPOPT_NOP;
00151         return;
00152     }
00153     else if(!strncasecmp(data, "ts", 2))
00154     {
00155         ds_ptr->ip_option = IPOPT_TS;
00156         return;
00157     }
00158     else if(!strncasecmp(data, "sec", 3))
00159     {
00160         ds_ptr->ip_option = IPOPT_SECURITY;
00161         return;
00162     }
00163     else if(!strncasecmp(data, "lsrr", 4))
00164     {
00165         ds_ptr->ip_option = IPOPT_LSRR;
00166         return;
00167     }
00168     else if(!strncasecmp(data, "lsrre", 5))
00169     {
00170         ds_ptr->ip_option = IPOPT_LSRR_E;
00171         return;
00172     }
00173     else if(!strncasecmp(data, "satid", 5))
00174     {
00175         ds_ptr->ip_option = IPOPT_SATID;
00176         return;
00177     }
00178     else if(!strncasecmp(data, "ssrr", 4))
00179     {
00180         ds_ptr->ip_option = IPOPT_SSRR;
00181         return;
00182     }
00183     else if(!strncasecmp(data, "any", 3))
00184     {
00185         ds_ptr->ip_option = 0;
00186         ds_ptr->any_flag = 1;
00187         return;
00188     }
00189     else
00190     {
00191         FatalError("%s(%d) => Unknown IP option argument: %s!\n",
00192                    file_name, file_line, data);
00193     }
00194 }
00195 
00196 
00197 /****************************************************************************
00198  * 
00199  * Function: TemplateDetectorFunction(char *, OptTreeNode *)
00200  *
00201  * Purpose: Use this function to perform the particular detection routine
00202  *          that this rule keyword is supposed to encompass.
00203  *
00204  * Arguments: data => argument data
00205  *            otn => pointer to the current rule's OTN
00206  *
00207  * Returns: void function
00208  *
00209  ****************************************************************************/
00210 int CheckIpOptions(Packet *p, struct _OptTreeNode *otn, OptFpList *fp_list)
00211 {
00212     int i;
00213     DEBUG_WRAP(DebugMessage(DEBUG_PLUGIN, "CheckIpOptions:"););
00214     if(!p->iph)
00215         return 0; /* if error occured while ip header
00216                    * was processed, return 0 automagically.
00217                */
00218 
00219     if((((IpOptionData *)otn->ds_list[PLUGIN_IPOPTION_CHECK])->any_flag == 1) 
00220        && (p->ip_option_count > 0))
00221     {
00222         DEBUG_WRAP(DebugMessage(DEBUG_PLUGIN, "Matched any ip options!\n"););
00223         /* call the next function in the function list recursively */
00224         return fp_list->next->OptTestFunc(p, otn, fp_list->next);
00225     }
00226 
00227     for(i=0; i< (int) p->ip_option_count; i++)
00228     {
00229         DEBUG_WRAP(DebugMessage(DEBUG_PLUGIN, "testing pkt(%d):rule(%d)\n",
00230                                 ((IpOptionData *)otn->ds_list[PLUGIN_IPOPTION_CHECK])->ip_option,
00231                                 p->ip_options[i].code); );
00232 
00233         if(((IpOptionData *)otn->ds_list[PLUGIN_IPOPTION_CHECK])->ip_option == p->ip_options[i].code)
00234         {
00235             /* call the next function in the function list recursively */
00236             return fp_list->next->OptTestFunc(p, otn, fp_list->next);
00237         }
00238     }
00239 
00240     /* if the test isn't successful, return 0 */
00241     return 0;
00242 }

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