root/trunk/src/dataFlowSet.c @ 14

Revision 14, 13.2 KB (checked in by andreu, 17 years ago)

IPv6 prefix ok

  • Property svn:eol-style set to native
RevLine 
[2]1/*
2 * File: dataFlowSet.c
3 *
4 * Authors: ANDREU François-Xavier
5 *
6 * Copyright (C) 2005 GIP RENATER
7 */
8
9/*  This file is part of renetcol.
10 *
11 *  renetcol is free software; you can redistribute it and/or modify
12 *  it under the terms of the GNU General Public License as published by
13 *  the Free Software Foundation; either version 2 of the License, or
14 *  (at your option) any later version.
15 *
16 *  renetcol 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.
20 *
21 *  You should have received a copy of the GNU General Public License
22 *  along with renetcol; if not, write to the Free Software
23 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
24 */
25
26#include "dataFlowSet.h"
27
28/*
29 * checkDataFlowSet
30 *
31 */
32unsigned short 
33checkDataFlowSet(unsigned short shift, RouterPtr cr, NetFlowV9HeaderPtr v9Ptr,
34                 unsigned short *offV9, unsigned char *buf, short *cFNPtr,
35                 short *cFId, DatagramPtr pcktPtr, RuleDefPtr *rulesCache,
36                 RulesPtr rlPtr, int myQueue)
37{
38  TplFlowSetPtr tmp;
39  FieldPtr pftmp;
40  FieldPtr secondPftmp;
41  unsigned short data_length = 0;
42  unsigned short flow_size = 0;
43  unsigned short oldOffset = *offV9;
44  unsigned short secondOffset = 0;
45  unsigned short secondOldOffset = 0;
[14]46  int moreIsNecessary = 1;
[2]47  int field_size = 0;
48  int cpt = 0;
49  int secondCpt = 0;
50  int overflow = 0;
51  int noEnd = 1;
52  int i=0;
53  int j=0;
54  int pos = 0;
55  unsigned char buffer1; 
56  unsigned char buffer2[2];
57  unsigned char buffer4[4];
58  RulesPtr tmpRuleList = rlPtr;
59  msgType myMsg;
60  char *msgTextIndex;
61  unsigned short tplMsgType = 11;
62
63  buffer2[1] = *(buf+(*offV9));(*offV9)++;
64  buffer2[0] = *(buf+(*offV9));(*offV9)++;
65  (*cFId) = *((unsigned short*)&buffer2);
66  buffer2[1] = *(buf+(*offV9));(*offV9)++;
67  buffer2[0] = *(buf+(*offV9));(*offV9)++;
68  data_length = *((unsigned short*)&buffer2);
69  if ((tmp=existTplId(cr, v9Ptr->sourceId,
70                      (*cFId)))!=NULL) {
71    pftmp = tmp->lastField;
72    for (; pftmp; pftmp = pftmp->prev) {
73      flow_size += pftmp->fieldLength;
74    }
75    if ( data_length%flow_size >= 9 ) {
76      (*cFNPtr) = v9Ptr->count;
77      syslog(LOG_INFO, "data flowset length not match with length from template definition, wrong template definition suspected; all next informations of this data flowset are not considered! flowset ID: %hu, from router: %lu.%lu.%lu.%lu",
78             (*cFId),
79             (pcktPtr->ipH->srcAdd>>24),
80             (pcktPtr->ipH->srcAdd<<8>>24),
81             (pcktPtr->ipH->srcAdd<<16>>24),
82             (pcktPtr->ipH->srcAdd<<24>>24));
83      return (data_length+shift);
84    }
85    pftmp = tmp->lastField;
86    secondPftmp = tmp->lastField;   
87    secondOffset = *offV9;
88    secondOldOffset = secondOffset;
89    while ( (((*offV9)-48-shift) <= data_length) && (overflow!=1) ) {
90      /*
91       * progression in a flow Set
92       * notes:
93       *   48=header ip + header netf
94       *   shift = shift if there is a template declaration
95       */
96      cpt++;
97      j=0;
98      pos = (pftmp->fieldType)*10+j;
99      field_size = (int) pftmp->fieldLength;
100      /*
101       * Comparaison between the field value and the rules
102       * ... if one rule exist
103       */
104      if (((RuleDefPtr)(*(rulesCache+pos))) != NULL) {
105        oldOffset = *offV9;
106        while (((RuleDefPtr)(*(rulesCache+pos))) != NULL){
107          /*
108           * while on one cache table line
109           */
110          *offV9 = oldOffset;
111          switch ((int)
112                  (((RuleDefPtr)(*(rulesCache+pos)))->operator)){
113          case 2:
114            switch (field_size) {
115            case 1:
116              buffer1 = *(buf+(*offV9)); (*offV9)++;
117              if (((RuleDefPtr)(*(rulesCache+pos)))->value->stor.cvalue
118                  == *((unsigned char*)&buffer1)) {
119                ((RuleDefPtr)(*(rulesCache+pos)))->check = 1;
120              }
121              break;
122            case 2:
123              buffer2[1]= *(buf+(*offV9)); (*offV9)++;
124              buffer2[0]= *(buf+(*offV9)); (*offV9)++;
125              if (((RuleDefPtr)(*(rulesCache+pos)))->value->stor.svalue
126                  == *((unsigned short*)&buffer2))
127                ((RuleDefPtr)(*(rulesCache+pos)))->check = 1;
128              break;
129            case 4:
130              buffer4[3]= *(buf+(*offV9)); (*offV9)++;
131              buffer4[2]= *(buf+(*offV9)); (*offV9)++;
132              buffer4[1]= *(buf+(*offV9)); (*offV9)++;
133              buffer4[0]= *(buf+(*offV9)); (*offV9)++;
[11]134              /* here , add a check on the field type */
[2]135              if ((((RuleDefPtr)(*(rulesCache+pos)))->value->stor.lvalue)
[11]136                  == (*((unsigned long*)&buffer4))>>(32-((RuleDefPtr)(*(rulesCache+pos)))->value->mask)<<(32-((RuleDefPtr)(*(rulesCache+pos)))->value->mask) )
[2]137                {
138                  ((RuleDefPtr)(*(rulesCache+pos)))->check = 1;
139                }
140              break;
141            case 16:
142              for (i=0; i<4; i++) {
143                buffer4[3]= *(buf+(*offV9)); (*offV9)++;
144                buffer4[2]= *(buf+(*offV9)); (*offV9)++;
145                buffer4[1]= *(buf+(*offV9)); (*offV9)++;
146                buffer4[0]= *(buf+(*offV9)); (*offV9)++;
[14]147                /* here check a new var : moreIsNecessary */
148                if (1==moreIsNecessary){
149                  switch(i){
150                  case 0:
151/*                  fprintf(stderr, "i:%d case0, N:%lu V:%lu TC:%lu, %hu\n", */
152/*                          i, */
153/*                          ntohl(((RuleDefPtr)(*(rulesCache+pos)))->value->stor.tabAdd6[i]), */
154/*                          (*((unsigned long*)&buffer4)), */
155/*                          (*((unsigned long*)&buffer4))>>(32-((RuleDefPtr)(*(rulesCache+pos)))->value->mask)<<(32-((RuleDefPtr)(*(rulesCache+pos)))->value->mask), */
156/*                          ((RuleDefPtr)(*(rulesCache+pos)))->value->mask */
157/*                          ); */
158                    if (((RuleDefPtr)(*(rulesCache+pos)))->value->mask <= 32){
159                      if (ntohl(((RuleDefPtr)(*(rulesCache+pos)))->value->stor.tabAdd6[i])
160                          ==
161                          (*((unsigned long*)&buffer4))>>(32-((RuleDefPtr)(*(rulesCache+pos)))->value->mask)<<(32-((RuleDefPtr)(*(rulesCache+pos)))->value->mask)
162                          )
163                        {
164                          ((RuleDefPtr)(*(rulesCache+pos)))->check = 1;
165                          moreIsNecessary = 0;
166                        } else {
167                          ((RuleDefPtr)(*(rulesCache+pos)))->check = 0;
168                          moreIsNecessary = 0;
169                        }
[2]170                    } else {
[14]171                      if (ntohl(((RuleDefPtr)(*(rulesCache+pos)))->value->stor.tabAdd6[i])
172                          ==
173                          (*((unsigned long*)&buffer4))
174                          )
175                        {
176                          ((RuleDefPtr)(*(rulesCache+pos)))->check = 1;
177                        } else {
178                          ((RuleDefPtr)(*(rulesCache+pos)))->check = 0;
179                          moreIsNecessary = 0;
180                        }
[2]181                    }
[14]182                    break;
183                  case 1:
184/*                  fprintf(stderr, "i:%d case1, N:%lu V:%lu TC:%lu\n", */
185/*                          i, */
186/*                          ntohl(((RuleDefPtr)(*(rulesCache+pos)))->value->stor.tabAdd6[i]), */
187/*                          (*((unsigned long*)&buffer4)), */
188/*                          (*((unsigned long*)&buffer4))>>(64-((RuleDefPtr)(*(rulesCache+pos)))->value->mask)<<(64-((RuleDefPtr)(*(rulesCache+pos)))->value->mask)); */
189                    if (((RuleDefPtr)(*(rulesCache+pos)))->value->mask <= 64){
190                      if (ntohl(((RuleDefPtr)(*(rulesCache+pos)))->value->stor.tabAdd6[i])
191                          ==
192                          (*((unsigned long*)&buffer4))>>(64-((RuleDefPtr)(*(rulesCache+pos)))->value->mask)<<(64-((RuleDefPtr)(*(rulesCache+pos)))->value->mask)
193                          )
194                        {
195                          ((RuleDefPtr)(*(rulesCache+pos)))->check =
196                            ((RuleDefPtr)(*(rulesCache+pos)))->check && 1;
197                          moreIsNecessary = 0;
198                        } else {
199                          ((RuleDefPtr)(*(rulesCache+pos)))->check = 0;
200                          moreIsNecessary = 0;
201                        }
202                    } else {
203                      if (ntohl(((RuleDefPtr)(*(rulesCache+pos)))->value->stor.tabAdd6[i])
204                          ==
205                          (*((unsigned long*)&buffer4))
206                          )
207                        {
208                          ((RuleDefPtr)(*(rulesCache+pos)))->check =
209                            ((RuleDefPtr)(*(rulesCache+pos)))->check && 1;
210                        } else {
211                          ((RuleDefPtr)(*(rulesCache+pos)))->check = 0;
212                          moreIsNecessary = 0;
213                        }
214                    }
215                    break;
216                  case 2:
217                    if (((RuleDefPtr)(*(rulesCache+pos)))->value->mask <= 96){
218                      if (ntohl(((RuleDefPtr)(*(rulesCache+pos)))->value->stor.tabAdd6[i])
219                          ==
220                          (*((unsigned long*)&buffer4))>>(96-((RuleDefPtr)(*(rulesCache+pos)))->value->mask)<<(96-((RuleDefPtr)(*(rulesCache+pos)))->value->mask)
221                          )
222                        {
223                          ((RuleDefPtr)(*(rulesCache+pos)))->check =
224                            ((RuleDefPtr)(*(rulesCache+pos)))->check && 1;
225                          moreIsNecessary = 0;
226                        } else {
227                          ((RuleDefPtr)(*(rulesCache+pos)))->check = 0;
228                          moreIsNecessary = 0;
229                        }
230                    } else {
231                      if (ntohl(((RuleDefPtr)(*(rulesCache+pos)))->value->stor.tabAdd6[i])
232                          ==
233                          (*((unsigned long*)&buffer4))
234                          )
235                        {
236                          ((RuleDefPtr)(*(rulesCache+pos)))->check =
237                            ((RuleDefPtr)(*(rulesCache+pos)))->check && 1;
238                        } else {
239                          ((RuleDefPtr)(*(rulesCache+pos)))->check = 0;
240                          moreIsNecessary = 0;
241                        }
242                    }
243                    break;
244                  case 3:
245                    if (((RuleDefPtr)(*(rulesCache+pos)))->value->mask <= 128){
246                      if (ntohl(((RuleDefPtr)(*(rulesCache+pos)))->value->stor.tabAdd6[i])
247                          ==
248                          (*((unsigned long*)&buffer4))>>(128-((RuleDefPtr)(*(rulesCache+pos)))->value->mask)<<(128-((RuleDefPtr)(*(rulesCache+pos)))->value->mask)
249                          )
250                        {
251                          ((RuleDefPtr)(*(rulesCache+pos)))->check =
252                            ((RuleDefPtr)(*(rulesCache+pos)))->check && 1;
253                        } else {
254                          ((RuleDefPtr)(*(rulesCache+pos)))->check = 0;
255                          moreIsNecessary = 0;
256                        }
257                    } else {
258                      if (ntohl(((RuleDefPtr)(*(rulesCache+pos)))->value->stor.tabAdd6[i])
259                          ==
260                          (*((unsigned long*)&buffer4))
261                          )
262                        {
263                          ((RuleDefPtr)(*(rulesCache+pos)))->check =
264                            ((RuleDefPtr)(*(rulesCache+pos)))->check && 1;
265                        } else {
266                          ((RuleDefPtr)(*(rulesCache+pos)))->check = 0;
267                          moreIsNecessary = 0;
268                        }
269                    }
270                    break;
271                  default:
272                    break;
273                  } /* end of switch(i) */
274                } /* end of if moreIsNecessary */
[2]275              }
[14]276/*            fprintf(stderr,"GOOD %d\n", */
277/*                    ((RuleDefPtr)(*(rulesCache+pos)))->check); */
278              moreIsNecessary = 1;
[2]279              break;
280            default:
281              syslog(LOG_INFO, "Field size not known: %d\n", field_size);
282              for (i=0; i<field_size; i++){
283                (*offV9)++;
284              }
285              break;
286            }
287            break;
288          default:
289            syslog(LOG_INFO, "Operator not known: %d\n",
290                   (int)(((RuleDefPtr)(*(rulesCache+pos)))->operator));
291            break;
292          }
293          j++;
294          pos = (pftmp->fieldType)*10+j;
295        } /* end while rulesCache */
296      } else {
297        /*
298         * no rule within this field type, but we must reading the value
299         */
300        switch (field_size) {
301        case 1:
302          buffer1 = *(buf+(*offV9)); (*offV9)++;
303          break;
304        case 2:
305          buffer2[1]= *(buf+(*offV9)); (*offV9)++;
306          buffer2[0]= *(buf+(*offV9)); (*offV9)++;
307          break;
308        case 4:
309          buffer4[3]= *(buf+(*offV9)); (*offV9)++;
310          buffer4[2]= *(buf+(*offV9)); (*offV9)++;
311          buffer4[1]= *(buf+(*offV9)); (*offV9)++;
312          buffer4[0]= *(buf+(*offV9)); (*offV9)++;
313          break;
314        case 16:
315          for (i=0; i<4; i++) {
316            buffer2[1]= *(buf+(*offV9)); (*offV9)++;
317            buffer2[0]= *(buf+(*offV9)); (*offV9)++;
318            buffer2[1]= *(buf+(*offV9)); (*offV9)++;
319            buffer2[0]= *(buf+(*offV9)); (*offV9)++;
320          }
321          break;
322        default:
323          syslog(LOG_INFO,"UNKNOWN FIELDS LENGTH: %d ", field_size);
324          for (i=0; i<field_size; i++){
325            (*offV9)++;
326          }
327        }
328      } /* end if one cache table line existence */
329      if (cpt==tmp->fieldCount) {
330        /*
331         * end of one flow (not the flowset)
332         */
333        /*
334         * Redirection if needed
335         * switch the rules definition (check & fieldToRecord),
336         * we send the flow or a part of the flow to the msg queue
337         */
338        tmpRuleList = rlPtr;
339        while (tmpRuleList){
340          unsigned short check = 1;
341          RuleDefPtr tmpRuleDefList = tmpRuleList->def;
342          secondOffset = secondOldOffset;
343          while (tmpRuleDefList){
344            check = check && tmpRuleDefList->check;
345            tmpRuleDefList->check = 0;
346            tmpRuleDefList = tmpRuleDefList->next;
347          }
348          if ( (tmpRuleList->def != NULL) && (check == 1)) {
349            /* msg building */
350            secondPftmp = tmp->lastField;
351/*          memcpy(myMsg.text, (char*)(buf+secondOffset),flow_size); */
352            msgTextIndex = mempcpy(mempcpy(mempcpy(myMsg.text,
353                                                   &tplMsgType,
354                                                   sizeof(unsigned short)
355                                                   ),
356                                           &tmpRuleList->id,
357                                           sizeof(tmpRuleList->id)
358                                           ),
359                                   &cr->IpAddress,
360                                   sizeof(unsigned long)
361                                   );
362            msgTextIndex = mempcpy(mempcpy(mempcpy(msgTextIndex,
363                                                   &tmp->sourceId,
364                                                   sizeof(unsigned long)
365                                                   ),
366                                           &tmp->templateFlowSetId,
367                                           sizeof(tmp->templateFlowSetId)
368                                           ),
369                                   buf+secondOffset,
370                                   flow_size
371                                   );
372            myMsg.type = 1;
373            msgSend( myQueue, myMsg);
374            noEnd = 1;
375            secondCpt=0;
376          } /* end if check */
377          tmpRuleList = tmpRuleList->next;
378          secondPftmp = tmp->lastField;
379        } /* end while tmpRuleList */
380        /*
381         * end redirection
382         */
383        secondOffset = *offV9;
384        secondOldOffset = secondOffset;
385        pftmp = tmp->lastField;
386        cpt=0;
387        (*cFNPtr)++; /* pointer on the flows number */
388        if (((*offV9)-48-shift+flow_size) > data_length){
389          overflow = 1; /* buffer index not correct */
390        }
391      } else {
392        /* not the flow end, progress in field list */
393        pftmp = pftmp->prev;
394      }
395    } /* end of the while on one flow record */   
396  }else{
397    /*
398     * template unknown, we skip this all the data
399     */
400    (*offV9)+=(data_length-4);
401    (*cFNPtr) = v9Ptr->count;
402  }
403  while ( ((*offV9)-48-shift) < data_length ) {
404    (*offV9)++; /* if padding */
405  }
406  while ( (*offV9)-48-shift > data_length ) {
407    (*offV9)--; /* crazy loop (when bug appears in template def) */
408  }
409  return (data_length+shift);
410}
411
412/*     for (i=0; i<(90); i++){ */
413/*       int k=0; */
414/*       pos = i*10+k; */
415/*       if (((RuleDefPtr)(*(rulesCache+pos))) != NULL){ */
416/*      fprintf(stderr," type: %d ",i); */
417/*      while ( ((RuleDefPtr)(*(rulesCache+pos))) != NULL){ */
418/*        fprintf(stderr, "\n op:%hu, pos: %d",  */
419/*                ((RuleDefPtr)(*(rulesCache+pos)))->operator, */
420/*                pos); */
421/*        k++; */
422/*        pos = i*10+k; */
423/*      } */
424/*      fprintf(stderr,"\n"); */
425/*       } */
426/*     }  */
Note: See TracBrowser for help on using the browser.