root/trunk/src/dataFlowSet.c @ 59

Revision 59, 29.7 KB (checked in by andreu, 15 years ago)

new aggregation method, based on SNMP index. New parameters in renetcolParam.h and new compilation options in configure.in.

  • Property svn:eol-style set to native
Line 
1/*
2 * File: dataFlowSet.c
3 *
4 * Authors: ANDREU Francois-Xavier
5 *
6 * Copyright (C) 2005 2006 2007 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 * Check Data FlowSet
30 *
31 */
32short 
33checkDataFlowSet(short shift,
34                 struct MyPtrs *myPtrs,
35                 int myQueue,
36                 struct PrefixV4 *V4PTab,
37                 size_t nbPV4
38                 )
39{
40  TplFlowSetPtr tmp = NULL;
41  TplOptionPtr tmpOpt = NULL;
42  FieldPtr pftmp = NULL;
43  FieldPtr secondPftmp = NULL;
44  unsigned short data_length = 0;
45  unsigned short flow_size = 0;
46  unsigned short oldOffset = *myPtrs->offsetV9Ptr;
47  unsigned short secondOffset = 0;
48  unsigned short secondOldOffset = 0;
49  int moreIsNecessary = 1;
50  int field_size = 0;
51  int cpt = 0;
52  int secondCpt = 0;
53  int overflow = 0;
54  int noEnd = 1;
55  int i = 0;
56  int j = 0;
57  int pos = 0;
58  unsigned char buffer1; 
59  unsigned char buffer2[2];
60  unsigned char buffer4[4];
61  RulesPtr tmpRuleList = myPtrs->rulesListPtr;
62  RulesPtr tmpRL = myPtrs->rulesListPtr;
63  msgType myMsg;
64  char *msgTextIndex;
65
66  unsigned short tplMsgType = 11;
67  struct PrefixV4 prefixKey, *res, *res2; /* for bsearch */
68  struct AggCache agCache;
69  int bool = 0; /* in IPV4 Agg mode enabled, we need to now if it's an IPv4 */
70                /* flow, we test on the field and then put bool at 1 */
71  int isMplsFlow = 0;
72  int paddingCounter = 0;
73  int crazyCounter = 0;
74
75  buffer2[1] = *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr));
76  (*myPtrs->offsetV9Ptr)++;
77  buffer2[0] = *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr));
78  (*myPtrs->offsetV9Ptr)++;
79  (*myPtrs->currentFlowsetIdPtr) = *((unsigned short*)&buffer2);
80  buffer2[1] = *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr));
81  (*myPtrs->offsetV9Ptr)++;
82  buffer2[0] = *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr));
83  (*myPtrs->offsetV9Ptr)++;
84  data_length = *((unsigned short*)&buffer2);
85
86  if ( (tmp=existTplId(myPtrs->currentRouterPtr,
87                       myPtrs->currentHeaderV9Ptr->sourceId,
88                       (*myPtrs->currentFlowsetIdPtr)))!=NULL ) {
89#ifdef DEBUG
90    fprintf(stderr,
91            "{d id: %hu, lg %hu",
92            (*myPtrs->currentFlowsetIdPtr),
93            data_length);
94    if ( (*myPtrs->currentFlowsetIdPtr) > TRESHOLD_TEMPLATE_ID ) {
95/*      fprintf(stderr,"Bad TID from router: %lu.%lu.%lu.%lu\n", */
96/*              (*myPtrs->currentFlowsetIdPtr), */
97/*              (myPtrs->pcktPtr->ipH->srcAdd>>24), */
98/*              (myPtrs->pcktPtr->ipH->srcAdd<<8>>24), */
99/*              (myPtrs->pcktPtr->ipH->srcAdd<<16>>24), */
100/*              (myPtrs->pcktPtr->ipH->srcAdd<<24>>24)); */
101/*      fprintf(stderr," Bytes : \n"); */
102/*      while ( ( myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr) ) > (myPtrs->ptr_buffer+1416) ){ */
103/*        buffer1= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr));(*myPtrs->offsetV9Ptr)++; */
104/*        tmp = *((unsigned char*)&buffer1); */
105/*        fprintf (stderr, " %s ", tmp); */
106/*      } */
107/*      exit(-1); */
108      return (data_length+shift);
109    }
110#endif
111    pftmp = tmp->lastField;
112    for (; pftmp; pftmp = pftmp->prev) {
113      flow_size += pftmp->fieldLength;
114    }
115    if ( data_length%flow_size >= 9 ) {
116      (*myPtrs->currentFlowsetNumberPtr) = myPtrs->currentHeaderV9Ptr->count;
117      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",
118             (*myPtrs->currentFlowsetIdPtr),
119             (myPtrs->pcktPtr->ipH->srcAdd>>24),
120             (myPtrs->pcktPtr->ipH->srcAdd<<8>>24),
121             (myPtrs->pcktPtr->ipH->srcAdd<<16>>24),
122             (myPtrs->pcktPtr->ipH->srcAdd<<24>>24));
123      return (data_length+shift);
124    }
125    if ( data_length >= 1452 ) {
126      syslog(LOG_INFO, "data flowset length too raised; all next informations of this data flowset are not considered! flowset ID: %hu, from router: %lu.%lu.%lu.%lu",
127             (*myPtrs->currentFlowsetIdPtr),
128             (myPtrs->pcktPtr->ipH->srcAdd>>24),
129             (myPtrs->pcktPtr->ipH->srcAdd<<8>>24),
130             (myPtrs->pcktPtr->ipH->srcAdd<<16>>24),
131             (myPtrs->pcktPtr->ipH->srcAdd<<24>>24));
132      return (-1);
133    }
134#if defined(IPV4AGGIDR) || defined(IPV4AGGIDSNMP)
135    agCache.routerAd = myPtrs->pcktPtr->ipH->srcAdd;
136#endif
137
138    pftmp = tmp->lastField;
139    secondPftmp = tmp->lastField;   
140    secondOffset = *myPtrs->offsetV9Ptr;
141    secondOldOffset = secondOffset;
142    while ( (((*myPtrs->offsetV9Ptr)-48-shift) <= data_length)
143            && (overflow!=1) ) {
144      /*
145       * progression in a data flow Set
146       * notes:
147       *   48=header ip + header netf
148       *   shift = shift if there is a template declaration
149       */
150      cpt++;
151      j = 0;
152      pos = (pftmp->fieldType)*MAX_RULES_PER_FIELD+j;
153      field_size = (int) pftmp->fieldLength;
154     
155      /* special case: check yes on all flows from one router */
156      /* (phantom field nb 0) */
157      /* FIXME : this code is repeated, perhaps past before */
158      while ( ((RuleDefPtr)(*(myPtrs->rulesAddressPtr+j))) != NULL ) {
159        if ( (((RuleDefPtr)(*(myPtrs->rulesAddressPtr+j)))->value->stor.lvalue)
160              == myPtrs->pcktPtr->ipH->srcAdd )
161          {
162            ((RuleDefPtr)(*(myPtrs->rulesAddressPtr+j)))->check = 1;
163          }     
164        j++;
165      }   
166      j = 0;
167     
168      /*
169       * Comparaison between the field value and the rules
170       * ... if one rule exist
171       * FIXME : warning, if no rules, no accounting ?
172       */
173      if (((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos))) != NULL) {
174        oldOffset = *myPtrs->offsetV9Ptr;
175        while (((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos))) != NULL){
176          /*
177           * while on one cache table line
178           */
179          *myPtrs->offsetV9Ptr = oldOffset;
180          switch ((int)
181                  (((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->operator)){
182          case 2:
183            /* operator: "=" */ 
184            switch (field_size) {
185            case 1:
186              buffer1 = *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr));
187              (*myPtrs->offsetV9Ptr)++;
188              /* rule check */
189              if (((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->value->stor.cvalue
190                  == *((unsigned char*)&buffer1)) {
191                ((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->check = 1;
192              }
193              /* end rule check */
194#if defined(IPV4AGGIDR) || defined(IPV4AGGIDSNMP)
195              if ((pftmp->fieldType==9)||(pftmp->fieldType==29)){
196                agCache.maskS = *((unsigned char*)&buffer1);
197              }
198              if ((pftmp->fieldType==13)||(pftmp->fieldType==30)){
199                agCache.maskD = *((unsigned char*)&buffer1);
200              }
201              if (pftmp->fieldType==60){
202                agCache.ipProt = *((unsigned char*)&buffer1);
203              }
204              if (pftmp->fieldType==4){
205                agCache.tProt = *((unsigned char*)&buffer1);
206              }
207              if (pftmp->fieldType==61){
208                agCache.sens = *((unsigned char*)&buffer1);
209              }
210              if (pftmp->fieldType==5){
211                agCache.dscp = *((unsigned char*)&buffer1);
212              }
213#endif       
214              break;
215            case 2:
216              buffer2[1]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr));
217              (*myPtrs->offsetV9Ptr)++;
218              buffer2[0]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr));
219              (*myPtrs->offsetV9Ptr)++;
220              if (((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->value->stor.svalue
221                  == *((unsigned short*)&buffer2))
222                ((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->check = 1;
223#if defined(IPV4AGGIDR) || defined(IPV4AGGIDSNMP)
224              if (pftmp->fieldType==10){
225                agCache.inputSnmp = *((unsigned short*)&buffer2);
226              }
227              if (pftmp->fieldType==14){
228                agCache.outputSnmp = *((unsigned short*)&buffer2);
229              }
230#endif
231              break;
232            case 3:
233              buffer4[3]= 0;
234              buffer4[2]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr));
235              (*myPtrs->offsetV9Ptr)++;
236              buffer4[1]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr));
237              (*myPtrs->offsetV9Ptr)++;
238              buffer4[0]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr));
239              (*myPtrs->offsetV9Ptr)++;
240              if (((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->value->stor.lvalue
241                  == *((unsigned long*)&buffer4))
242                ((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->check = 1;
243              /* aggregation */
244              /* end aggregation */
245              if (pftmp->fieldType==70){
246                isMplsFlow = 1;
247              }
248              break;
249            case 4:
250              buffer4[3]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr));
251              (*myPtrs->offsetV9Ptr)++;
252              buffer4[2]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr));
253              (*myPtrs->offsetV9Ptr)++;
254              buffer4[1]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr));
255              (*myPtrs->offsetV9Ptr)++;
256              buffer4[0]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr));
257              (*myPtrs->offsetV9Ptr)++;
258              /* FIXME : here , add a check on the field type */
259              if ((pftmp->fieldType==8)||(pftmp->fieldType==12)){
260                if ((((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->value->stor.lvalue)
261                    == (*((unsigned long*)&buffer4))>>(32-((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->value->mask)<<(32-((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->value->mask) )
262                  {
263                    ((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->check = 1;
264                  }
265              }
266              if (pftmp->fieldType==1){
267                agCache.bytes = *((unsigned long*)&buffer4);
268                if (
269                    ((unsigned long)((((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->value->stor.lvalue) + ( (((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->value->stor.lvalue)*10/100))
270                     >= (*((unsigned long*)&buffer4)))
271                    &&
272                    ( (unsigned long)((((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->value->stor.lvalue) - ( (((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->value->stor.lvalue)*10/100))
273                      <= (*((unsigned long*)&buffer4)) )
274                    )
275                  {
276                    ((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->check = 1;
277                  }
278              }
279#if defined(IPV4AGGIDR) || defined(IPV4AGGIDSNMP)
280              if ((pftmp->fieldType==8)){
281                bool = 1; /* very important, test if we have an IPv4 flow for Agg */
282                agCache.v4AdS = *((unsigned long*)&buffer4);
283              }
284              if ((pftmp->fieldType==12)){
285                agCache.v4AdD = *((unsigned long*)&buffer4);
286              }
287              if (pftmp->fieldType==1){
288                agCache.bytes = *((unsigned long*)&buffer4);
289              }
290              if (pftmp->fieldType==2){
291                agCache.pkts = *((unsigned long*)&buffer4);
292              }
293#endif
294              break;
295            case 16:
296              for (i=0; i<4; i++) {
297                buffer4[3]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr));
298                (*myPtrs->offsetV9Ptr)++;
299                buffer4[2]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr));
300                (*myPtrs->offsetV9Ptr)++;
301                buffer4[1]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr));
302                (*myPtrs->offsetV9Ptr)++;
303                buffer4[0]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr));
304                (*myPtrs->offsetV9Ptr)++;
305                if (1==moreIsNecessary){
306                  switch(i){
307                  case 0:
308                    if (((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->value->mask <= 32){
309                      if (ntohl(((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->value->stor.tabAdd6[i])
310                          ==
311                          (*((unsigned long*)&buffer4))>>(32-((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->value->mask)<<(32-((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->value->mask)
312                          )
313                        {
314                          ((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->check = 1;
315                          moreIsNecessary = 0;
316                        } else {
317                          ((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->check = 0;
318                          moreIsNecessary = 0;
319                        }
320                    } else {
321                      if (ntohl(((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->value->stor.tabAdd6[i])
322                          ==
323                          (*((unsigned long*)&buffer4))
324                          )
325                        {
326                          ((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->check = 1;
327                        } else {
328                          ((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->check = 0;
329                          moreIsNecessary = 0;
330                        }
331                    }
332                    break;
333                  case 1:
334                    if (((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->value->mask <= 64){
335                      if (ntohl(((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->value->stor.tabAdd6[i])
336                          ==
337                          (*((unsigned long*)&buffer4))>>(64-((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->value->mask)<<(64-((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->value->mask)
338                          )
339                        {
340                          ((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->check =
341                            ((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->check && 1;
342                          moreIsNecessary = 0;
343                        } else {
344                          ((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->check = 0;
345                          moreIsNecessary = 0;
346                        }
347                    } else {
348                      if (ntohl(((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->value->stor.tabAdd6[i])
349                          ==
350                          (*((unsigned long*)&buffer4))
351                          )
352                        {
353                          ((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->check =
354                            ((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->check && 1;
355                        } else {
356                          ((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->check = 0;
357                          moreIsNecessary = 0;
358                        }
359                    }
360                    break;
361                  case 2:
362                    if (((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->value->mask <= 96){
363                      if (ntohl(((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->value->stor.tabAdd6[i])
364                          ==
365                          (*((unsigned long*)&buffer4))>>(96-((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->value->mask)<<(96-((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->value->mask)
366                          )
367                        {
368                          ((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->check =
369                            ((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->check && 1;
370                          moreIsNecessary = 0;
371                        } else {
372                          ((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->check = 0;
373                          moreIsNecessary = 0;
374                        }
375                    } else {
376                      if (ntohl(((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->value->stor.tabAdd6[i])
377                          ==
378                          (*((unsigned long*)&buffer4))
379                          )
380                        {
381                          ((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->check =
382                            ((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->check && 1;
383                        } else {
384                          ((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->check = 0;
385                          moreIsNecessary = 0;
386                        }
387                    }
388                    break;
389                  case 3:
390                    if (((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->value->mask <= 128){
391                      if (ntohl(((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->value->stor.tabAdd6[i])
392                          ==
393                          (*((unsigned long*)&buffer4))>>(128-((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->value->mask)<<(128-((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->value->mask)
394                          )
395                        {
396                          ((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->check =
397                            ((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->check && 1;
398                        } else {
399                          ((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->check = 0;
400                          moreIsNecessary = 0;
401                        }
402                    } else {
403                      if (ntohl(((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->value->stor.tabAdd6[i])
404                          ==
405                          (*((unsigned long*)&buffer4))
406                          )
407                        {
408                          ((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->check =
409                            ((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->check && 1;
410                        } else {
411                          ((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->check = 0;
412                          moreIsNecessary = 0;
413                        }
414                    }
415                    break;
416                  default:
417                    break;
418                  } /* end of switch(i) */
419                } /* end of if moreIsNecessary */
420              }
421              moreIsNecessary = 1;
422              /* aggregation for IPv6 flows */
423             
424              /* end aggregation */
425              break;
426            default:
427              syslog(LOG_INFO, "Field size not known: %d\n", field_size);
428              for (i=0; i<field_size; i++){
429                (*myPtrs->offsetV9Ptr)++;
430              }
431              break;
432            }
433            break;
434          default:
435            syslog(LOG_INFO, "Operator not known: %d\n",
436                   (int)(((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->operator));
437            break;
438          }
439          j++;
440          pos = (pftmp->fieldType)*10+j;
441        } /* end while myPtrs->rulesAddressPtr */
442      } else {
443        /*
444         * no rule within this field type, but we must read the value
445         */
446        switch (field_size) {
447        case 1:
448          buffer1 = *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr));
449          (*myPtrs->offsetV9Ptr)++;
450#if defined(IPV4AGGIDR) || defined(IPV4AGGIDSNMP)
451          if ((pftmp->fieldType==9)||(pftmp->fieldType==29)){
452            agCache.maskS = *((unsigned char*)&buffer1);
453          }
454          if ((pftmp->fieldType==13)||(pftmp->fieldType==30)){
455            agCache.maskD = *((unsigned char*)&buffer1);
456          }
457          if (pftmp->fieldType==60){
458            agCache.ipProt = *((unsigned char*)&buffer1);
459          }
460          if (pftmp->fieldType==4){
461            agCache.tProt = *((unsigned char*)&buffer1);
462          }
463          if (pftmp->fieldType==61){
464            agCache.sens = *((unsigned char*)&buffer1);
465          }
466          if (pftmp->fieldType==5){
467            agCache.dscp = *((unsigned char*)&buffer1);
468          }       
469#endif
470          break;
471        case 2:
472          buffer2[1]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr));
473          (*myPtrs->offsetV9Ptr)++;
474          buffer2[0]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr));
475          (*myPtrs->offsetV9Ptr)++;
476#if defined(IPV4AGGIDR) || defined(IPV4AGGIDSNMP)
477          if (pftmp->fieldType==10){
478            agCache.inputSnmp = *((unsigned short*)&buffer2);
479          }       
480          if (pftmp->fieldType==14){
481            agCache.outputSnmp = *((unsigned short*)&buffer2);
482          }       
483#endif
484          break;
485        case 3:
486          buffer4[3]= 0;
487          buffer4[2]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr));
488          (*myPtrs->offsetV9Ptr)++;
489          buffer4[1]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr));
490          (*myPtrs->offsetV9Ptr)++;
491          buffer4[0]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr));
492          (*myPtrs->offsetV9Ptr)++;
493          /* aggregation */
494          /* end aggregation */
495          if (pftmp->fieldType==70){
496            isMplsFlow = 1;
497          }
498          break;
499        case 4:
500          buffer4[3]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr));
501          (*myPtrs->offsetV9Ptr)++;
502          buffer4[2]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr));
503          (*myPtrs->offsetV9Ptr)++;
504          buffer4[1]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr));
505          (*myPtrs->offsetV9Ptr)++;
506          buffer4[0]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr));
507          (*myPtrs->offsetV9Ptr)++;
508#if defined(IPV4AGGIDR) || defined(IPV4AGGIDSNMP)
509          if ((pftmp->fieldType==8)){
510            bool = 1;
511            agCache.v4AdS = *((unsigned long*)&buffer4);
512          } else if ((pftmp->fieldType==12)){
513            agCache.v4AdD = *((unsigned long*)&buffer4);
514          } else if (pftmp->fieldType==1){
515            agCache.bytes = *((unsigned long*)&buffer4);
516          } else if (pftmp->fieldType==2){
517            agCache.pkts = *((unsigned long*)&buffer4);
518          }
519#endif
520          break;
521        case 16:
522          for (i=0; i<4; i++) {
523            buffer2[1]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr));
524            (*myPtrs->offsetV9Ptr)++;
525            buffer2[0]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr));
526            (*myPtrs->offsetV9Ptr)++;
527            buffer2[1]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr));
528            (*myPtrs->offsetV9Ptr)++;
529            buffer2[0]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr));
530            (*myPtrs->offsetV9Ptr)++;
531          }
532          /* aggregation IPv6 */         
533          /* end aggregation */
534          break;
535        default:
536          syslog(LOG_INFO,"UNKNOWN FIELDS LENGTH: %d ", field_size);
537          for (i=0; i<field_size; i++){
538            (*myPtrs->offsetV9Ptr)++;
539          }
540        }
541      } /* end if one cache table line existence */
542      if (cpt==tmp->fieldCount) {
543        /*
544         * end of one flow (not the flowset)
545         */
546
547        /* put aggregation cache information to IPv4 Prefixes table */
548        /* Aggregation mode must be enable in ./configure options   */
549        /* first we must know if the address belong to our prefix   */
550#if defined(IPV4AGGIDR) || defined(IPV4AGGIDSNMP)
551        if ( (bool == 1) && (isMplsFlow == 0) ){
552          prefixKey.beginning = agCache.v4AdS>>(32-agCache.maskS)<<(32-agCache.maskS);
553          res = bsearch(&prefixKey, V4PTab, nbPV4,
554                        sizeof(struct PrefixV4), prefGlobalCmp);
555          if (res!=NULL){
556#if defined(IPV4AGGIDR)
557            if ( myPtrs->routersID[res->routerNb] == agCache.routerAd  ) {
558#elif defined(IPV4AGGIDSNMP)
559            if ( myPtrs->currentRouterPtr->snmpIndexList[agCache.inputSnmp] == 1  ) {         
560#endif
561              if (myPtrs->currentRouterPtr->sampled != 0 ){
562                /* OUT ("traffic from the prefix/subnet")  */
563                res->bytesNbOUT += agCache.bytes;
564                res->pktsNbOUT += agCache.pkts;
565                (res->flowNbOUT)++;
566              }
567              if (res->sampling == 0
568                  && myPtrs->currentRouterPtr->sampled != 0){
569                res->sampling = myPtrs->currentRouterPtr->sampled;
570              }
571#if defined(MATRIX) && defined(IPV4AGGIDR)
572              /* inter-pop matrix Accounting */
573              prefixKey.beginning = agCache.v4AdD>>(32-agCache.maskD)<<(32-agCache.maskD);
574              res2 = bsearch(&prefixKey, V4PTab, nbPV4,
575                            sizeof(struct PrefixV4), prefGlobalCmp);
576              if (res2!=NULL){         
577                ((struct POP *)((myPtrs->matrixPOP)
578                                +((res->routerNb)*ROUTER_INDEX_MAX)
579                                +((res2->routerNb))))->pktsNb += agCache.pkts;
580                ((struct POP *)((myPtrs->matrixPOP)
581                                +((res->routerNb)*ROUTER_INDEX_MAX)
582                                +((res2->routerNb))))->bytesNb += agCache.bytes;
583                ((struct POP *)((myPtrs->matrixPOP)
584                                +((res->routerNb)*ROUTER_INDEX_MAX)
585                                +((res2->routerNb))))->flowNb++;
586              } else {
587                (
588                 ((struct POP *)(myPtrs->matrixPOP)
589                 +((res->routerNb)*ROUTER_INDEX_MAX)
590                 +((ROUTER_INDEX_MAX-1))))->pktsNb += agCache.pkts;
591                (
592                 ((struct POP *)(myPtrs->matrixPOP)
593                 +((res->routerNb)*ROUTER_INDEX_MAX)
594                 +((ROUTER_INDEX_MAX-1))))->bytesNb += agCache.bytes;
595                (
596                 ((struct POP *)(myPtrs->matrixPOP)
597                 +((res->routerNb)*ROUTER_INDEX_MAX)
598                 +((ROUTER_INDEX_MAX-1))))->flowNb++;
599              }
600              /* end interpop matrix accounting */
601#endif /* matrix */
602            } else {
603              /* doublon or bad reference in prefix list */
604            }
605          } else {
606            prefixKey.beginning = agCache.v4AdD>>(32-agCache.maskD)<<(32-agCache.maskD);
607            res = bsearch(&prefixKey, V4PTab, nbPV4,
608                          sizeof(struct PrefixV4), prefGlobalCmp);
609            if (res!=NULL){
610              if ( myPtrs->currentRouterPtr->snmpIndexList[agCache.inputSnmp] == 1  ) {       
611                if (myPtrs->currentRouterPtr->sampled != 0 ){
612                  /* IN ("traffic to the prefix/subnet")  */
613                  res->bytesNbIN += agCache.bytes;
614                  res->pktsNbIN += agCache.pkts;
615                  (res->flowNbIN)++;
616                }
617#if defined(IPV4AGGIDR)
618              if ( myPtrs->routersID[res->routerNb] == agCache.routerAd  ) {
619                if (myPtrs->currentRouterPtr->sampled != 0 ){
620                  /* IN ("traffic to the prefix")  */
621                  res->bytesNbIN += agCache.bytes;
622                  res->pktsNbIN += agCache.pkts;
623                  (res->flowNbIN)++;
624                }
625#endif
626                if (res->sampling == 0
627                    && myPtrs->currentRouterPtr->sampled != 0 ){
628                  res->sampling = myPtrs->currentRouterPtr->sampled;
629                }
630/*            } else { */
631                /* doublon or bad reference in prefix list */
632              }
633            } else {
634              /* HERE : we are in the cases of the address/mask don't      */
635              /*        belong to a prefix listed in the IPv4 prefix file  */
636              /* possibilities :                                           */
637              /*     -  prefix not referenced but allowed to be routed     */
638              /*     -  prefix not referenced but not allowed to be routed */
639              /*     -  spoofing                                           */
640#ifdef PRINTUNKNOWNSUBNET 
641              fprintf(stderr, "%lu.%lu.%lu.%lu/%hu -> %lu.%lu.%lu.%lu/%hu (R:%lu.%lu.%lu.%lu) \n",
642                      (agCache.v4AdS>>24),
643                      (agCache.v4AdS<<8>>24),
644                      (agCache.v4AdS<<16>>24),
645                      (agCache.v4AdS<<24>>24),
646                      (agCache.maskS),
647                      (agCache.v4AdD>>24),
648                      (agCache.v4AdD<<8>>24),
649                      (agCache.v4AdD<<16>>24),
650                      (agCache.v4AdD<<24>>24),
651                      (agCache.maskD),
652                      (myPtrs->pcktPtr->ipH->srcAdd>>24),
653                      (myPtrs->pcktPtr->ipH->srcAdd<<8>>24),
654                      (myPtrs->pcktPtr->ipH->srcAdd<<16>>24),
655                      (myPtrs->pcktPtr->ipH->srcAdd<<24>>24)
656                      );
657#endif /* PRINTUNKNOWNSUBNET */
658            }
659          }
660        }
661        bool = 0;
662        isMplsFlow = 0;
663#endif /* (IPV4AGGIDR) || (IPV4AGGIDSNMP) */
664        /*
665         * Redirection if needed
666         * switch the rules definition (check & fieldToRecord),
667         * we send the flow or a part of the flow to a remote host or a file.
668         * In a first time (until release 0.0.7, the information was sent via
669         * the process renetcolSender with an IPC message queue. But, the perf
670         * of IPC Msg was very bad with a default system compilation.
671         * So, now, we send information directly from here.
672         */
673        tmpRuleList = myPtrs->rulesListPtr;
674        tmpRL = myPtrs->rulesListPtr;
675        while (tmpRuleList){
676          RuleDefPtr tmpRuleDefList = tmpRuleList->def;
677          unsigned short check = 1;
678          int s=0;
679          secondOffset = secondOldOffset;
680          while (tmpRuleDefList){
681            check = check && tmpRuleDefList->check;
682            tmpRuleDefList->check = 0;
683            tmpRuleDefList = tmpRuleDefList->next;
684          }
685          if ( (tmpRuleList->def != NULL) && (check == 1)) {
686            /* msg building */
687            secondPftmp = tmp->lastField;
688            msgTextIndex = mempcpy(mempcpy(mempcpy(myMsg.text,   
689                                                   &tplMsgType,
690                                                   sizeof(unsigned short)
691                                                   ),
692                                           &tmpRuleList->id,
693                                           sizeof(tmpRuleList->id)
694                                           ),
695                                   &myPtrs->currentRouterPtr->IpAddress,
696                                   sizeof(unsigned long)
697                                   );
698            msgTextIndex = mempcpy(mempcpy(mempcpy(msgTextIndex,
699                                                   &tmp->sourceId,
700                                                   sizeof(unsigned long)
701                                                   ),
702                                           &tmp->templateFlowSetId,
703                                           sizeof(tmp->templateFlowSetId)
704                                           ),
705                                   myPtrs->ptr_buffer+secondOffset,
706                                   flow_size
707                                   );
708            myMsg.type = 1;
709
710
711            /* NEW transfert type */
712            for ( ; tmpRL; tmpRL=tmpRL->next){
713              if (tmpRL->id == tmpRuleList->id){       
714                s = sendMessage(tmpRL->host->sockId, myMsg.text, sizeof(myMsg.text),
715                                tmpRL->host->hostAddressPtr);
716              }
717            }
718            /* FIXME : perhaps check "s" */
719            noEnd = 1;
720            secondCpt=0;
721          } /* end if check */
722          tmpRuleList = tmpRuleList->next;
723          tmpRL = myPtrs->rulesListPtr; /* ticket #11 */
724          secondPftmp = tmp->lastField;
725        } /* end while tmpRuleList */
726        /*
727         * end redirection
728         */
729        secondOffset = *myPtrs->offsetV9Ptr;
730        secondOldOffset = secondOffset;
731        pftmp = tmp->lastField;
732        cpt=0;
733        (*myPtrs->currentFlowsetNumberPtr)++; /* pointer on the flows number */
734        if (((*myPtrs->offsetV9Ptr)-48-shift+flow_size) > data_length){
735          overflow = 1; /* buffer index not correct */
736        }
737      } else {
738        /* not the flow end, progress in field list */
739        pftmp = pftmp->prev;
740      }
741    } /* end of the while on one flow record */
742   
743    while ( ((*myPtrs->offsetV9Ptr)-48-shift) < data_length ) {
744      (*myPtrs->offsetV9Ptr)++; /* if padding */
745      paddingCounter++;
746      if ( paddingCounter > 8 ) {
747#ifdef DEBUG
748        fprintf(stderr," padding too high: %d ", paddingCounter);
749#endif
750        syslog(LOG_INFO,"padding too high: %d ", paddingCounter);
751        return (-1);
752      }
753    }
754    while ( (*myPtrs->offsetV9Ptr)-48-shift > data_length ) {
755      (*myPtrs->offsetV9Ptr)--; /* crazy loop (when bug appears in template def) */
756      crazyCounter++;
757    }
758#ifdef DEBUG
759    if (crazyCounter != 0){ fprintf(stderr," crazyCounter: %d ", crazyCounter); }
760#endif
761    if (crazyCounter!=0) { syslog(LOG_INFO,"crazyCounter: %d ", crazyCounter);}
762   
763#ifdef DEBUG
764    fprintf(stderr,"}");
765#endif
766
767    return (data_length+shift);
768    /* end new place */
769   
770  }else{
771    /*
772     *
773     *
774     *       Optional template
775     *
776     *
777     */
778    if ((tmpOpt=existTplOptId(myPtrs->currentRouterPtr, myPtrs->currentHeaderV9Ptr->sourceId,
779                              (*myPtrs->currentFlowsetIdPtr)))!=NULL) {       
780#ifdef DEBUG
781      fprintf(stderr,"<d id: %hu, lg %hu", (*myPtrs->currentFlowsetIdPtr), data_length);
782#endif     
783      for ( i=0; i<(tmpOpt->optionScopeLg/4); i++){
784        /* FIXME : today we skip the scope fields, it's bad :( */ 
785        if ((pftmp=tmpOpt->lastField)!=NULL) {
786          for (j=0; j<pftmp->fieldLength; j++) {
787            (*myPtrs->offsetV9Ptr)++;
788          }
789          pftmp = pftmp->prev;
790        }
791      }
792      while (pftmp != NULL) {
793        if (pftmp->fieldLength==1){
794          buffer1 = *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr)); (*myPtrs->offsetV9Ptr)++;
795        }
796        if (pftmp->fieldLength==2){
797          buffer2[1]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr)); (*myPtrs->offsetV9Ptr)++;
798          buffer2[0]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr)); (*myPtrs->offsetV9Ptr)++;
799        }
800        if (pftmp->fieldLength==4){
801          buffer4[3]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr)); (*myPtrs->offsetV9Ptr)++;
802          buffer4[2]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr)); (*myPtrs->offsetV9Ptr)++;
803          buffer4[1]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr)); (*myPtrs->offsetV9Ptr)++;
804          buffer4[0]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr)); (*myPtrs->offsetV9Ptr)++;
805          if (pftmp->fieldType==34){
806            if ((*((unsigned long*)&buffer4)==1)
807                || (*((unsigned long*)&buffer4)==10)
808                || (*((unsigned long*)&buffer4)==100)
809                || (*((unsigned long*)&buffer4)==1000)) {
810              if (myPtrs->currentRouterPtr->sampled != *((unsigned long*)&buffer4) ) {
811#ifdef DEBUGAGG
812                fprintf(stderr,"\n SV (p) %lu != %lu (r)", *((unsigned long*)&buffer4),
813                        myPtrs->currentRouterPtr->sampled);
814#endif                     
815                syslog(LOG_INFO,"Sampling value change for router : %lu.%lu.%lu.%lu, old: %lu, new: %lu",
816                       (myPtrs->pcktPtr->ipH->srcAdd>>24),
817                       (myPtrs->pcktPtr->ipH->srcAdd<<8>>24),
818                       (myPtrs->pcktPtr->ipH->srcAdd<<16>>24),
819                       (myPtrs->pcktPtr->ipH->srcAdd<<24>>24),
820                       myPtrs->currentRouterPtr->sampled,
821                       *((unsigned long*)&buffer4)
822                       );
823                myPtrs->currentRouterPtr->sampled = *((unsigned long*)&buffer4);
824              }
825            }else{
826              syslog(LOG_INFO,"Sampling value not standard for router : %lu.%lu.%lu.%lu, value : %lu",
827                     (myPtrs->pcktPtr->ipH->srcAdd>>24),
828                     (myPtrs->pcktPtr->ipH->srcAdd<<8>>24),
829                     (myPtrs->pcktPtr->ipH->srcAdd<<16>>24),
830                     (myPtrs->pcktPtr->ipH->srcAdd<<24>>24),
831                     *((unsigned long*)&buffer4)
832                     );
833            }
834          }
835        }       
836        pftmp = pftmp->prev;
837      }
838      if ( pftmp == NULL ) {
839        while ((*myPtrs->offsetV9Ptr)%4 != 0) {
840          (*myPtrs->offsetV9Ptr)++;
841#ifdef DEBUG
842          fprintf(stderr," p ");
843#endif
844        }
845      } else {
846        fprintf(stderr,"PB ");
847      }
848#ifdef DEBUG
849      fprintf(stderr,">");
850#endif
851      return (data_length+shift);
852    } else {
853      /*
854       * template unknown, we skip this all the data
855       */
856      (*myPtrs->offsetV9Ptr)+=(data_length-4);
857      (*myPtrs->currentFlowsetNumberPtr) = myPtrs->currentHeaderV9Ptr->count;
858#ifdef DEBUG
859      fprintf(stderr, " U, r: %lu.%lu.%lu.%lu, sourceId: %lu, TID: %hu",
860              (myPtrs->pcktPtr->ipH->srcAdd>>24),
861              (myPtrs->pcktPtr->ipH->srcAdd<<8>>24),
862              (myPtrs->pcktPtr->ipH->srcAdd<<16>>24),
863              (myPtrs->pcktPtr->ipH->srcAdd<<24>>24),
864              myPtrs->currentHeaderV9Ptr->sourceId,
865              (*myPtrs->currentFlowsetIdPtr)
866              );
867#endif
868      return (data_length+shift);
869    }
870  }
871}
Note: See TracBrowser for help on using the browser.