File: Argus_icmp.c

package info (click to toggle)
argus 1%3A2.0.6.fixes.1-16.3
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 2,016 kB
  • ctags: 4,025
  • sloc: ansic: 23,022; sh: 5,734; makefile: 380; yacc: 255; lex: 234
file content (354 lines) | stat: -rwxr-xr-x 11,936 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
 * Copyright (c) 2000-2004 QoSient, LLC
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#ifndef ArgusICMP
#define ArgusICMP
#endif

#include <stdio.h>
#include <ArgusModeler.h>
#include <ArgusOutput.h>
#include <ArgusSource.h>
#include <ArgusUtil.h>


int
ArgusCreateICMPFlow (struct ip *ip)
{
   int retn = 0;
   unsigned int *icmphdr = (unsigned int *) ArgusThisUpHdr;
   struct icmp *icmp = (struct icmp *) icmphdr;
   struct ArgusICMPFlow *icmpFlow = &ArgusThisFlow->icmp_flow;

   ArgusThisDir = 0;

   icmpFlow->ip_src = ip->ip_src.s_addr;
   icmpFlow->ip_dst = ip->ip_dst.s_addr;
   icmpFlow->ip_p   = ip->ip_p;

   if (STRUCTCAPTURED(*icmphdr)) {
      icmpFlow->type = icmp->icmp_type;
      icmpFlow->code = icmp->icmp_code;

      if (ICMP_INFOTYPE(icmp->icmp_type)) {
         switch (icmp->icmp_type) {
            case ICMP_ECHOREPLY:
               ArgusThisDir = 1;
            case ICMP_ECHO:
               icmpFlow->type = ICMP_ECHO;
               icmpFlow->id = ntohs(icmp->icmp_id);
               icmpFlow->ip_id = ntohs(icmp->icmp_seq);
               break;
            case ICMP_TSTAMPREPLY:
               ArgusThisDir = 1;
            case ICMP_TSTAMP:
               icmpFlow->type = ICMP_TSTAMP;
               break;
            case ICMP_IREQREPLY:
               ArgusThisDir = 1;
            case ICMP_IREQ:
               icmpFlow->type = ICMP_IREQ;
               break;
            case ICMP_MASKREPLY:
               ArgusThisDir = 1;
            case ICMP_MASKREQ:
               icmpFlow->type = ICMP_MASKREQ;
               break;
         }
         retn = 1;

      } else {
         struct ip *oip;
         struct udphdr *ouh;
         int hlen;

         oip = &icmp->icmp_ip;
         hlen = oip->ip_hl << 2;
         ouh = (struct udphdr *) (((u_char *) oip) + hlen);

         switch (icmp->icmp_type) {
            case ICMP_UNREACH:
               if (STRUCTCAPTURED(*icmp)) {
                  switch (icmp->icmp_code) {
                     case ICMP_UNREACH_PROTOCOL:
                        icmpFlow->id = (unsigned short) icmp->icmp_ip.ip_p;
                        break;
                     case ICMP_UNREACH_PORT:
                        icmpFlow->tp_p = oip->ip_p;
                        icmpFlow->id = ntohs((unsigned short) ouh->uh_dport);
                        break;
   
                     case ICMP_UNREACH_NET:
                     case ICMP_UNREACH_HOST:
                        bcopy ((char *) &icmp->icmp_ip.ip_dst.s_addr,
                                        (char *)&icmpFlow->id, sizeof (int));
                        break;
                  }
                  retn = 1;
               }
               break;
            case ICMP_REDIRECT:
               if (STRUCTCAPTURED(*icmp)) {
                  switch (icmp->icmp_code) {
                     case ICMP_REDIRECT_TOSNET:
                     case ICMP_REDIRECT_TOSHOST:
                        icmpFlow->tp_p = oip->ip_tos;

                     case ICMP_REDIRECT_NET:
                     case ICMP_REDIRECT_HOST:
                        bcopy ((char *) &icmp->icmp_ip.ip_dst.s_addr, (char *)&icmpFlow->id, sizeof (int));
                        break;
                  }
                  retn = 1;
               }
               break;

            default:
               retn = 1;
               break;
         }
      }

      if (ArgusThisDir) {
         unsigned int addr = icmpFlow->ip_src;
         icmpFlow->ip_src = icmpFlow->ip_dst;
         icmpFlow->ip_dst = addr;
      }
   }

#ifdef ARGUSDEBUG
   ArgusDebug (8, "ArgusCreateICMPFlow(0x%x) returning %d\n", ip, retn);
#endif 

   return (retn);
}


#include <string.h>
#include <errno.h>

void
ArgusUpdateICMPState (struct ArgusFlowStruct *flowstr, unsigned char *state)
{
   struct ip *ArgusTempIpHdr = ArgusThisIpHdr;
   unsigned char *ArgusTempSnapEnd = ArgusThisSnapEnd;
   struct icmp *icmp = (struct icmp *) ArgusThisUpHdr; 
   struct ArgusICMPObject *icmpObj = NULL;

   ArgusUpdateAppState(flowstr, state);

   if (*state == ARGUS_START) {
      ArgusTallyTime (flowstr, *state);
      ArgusTallyStats (flowstr, *state);

      if ((icmpObj = (void *) ArgusCalloc (1, sizeof(struct ArgusICMPObject))) == NULL) {
         ArgusLog (LOG_ERR, "ArgusUpdateICMPState: ArgusCalloc %s", strerror(errno));

      } else {
         if (flowstr->NetworkDSRBuffer != NULL)
            ArgusFree (flowstr->NetworkDSRBuffer);

         flowstr->NetworkDSRBuffer = icmpObj;
         icmpObj->type      = ARGUS_ICMP_DSR;
         icmpObj->length    = sizeof(*icmpObj);
         icmpObj->icmp_type = icmp->icmp_type;
         icmpObj->icmp_code = icmp->icmp_code;
         icmpObj->iseq      = ntohs(icmp->icmp_seq);
         icmpObj->osrcaddr  = ArgusThisIpHdr->ip_src.s_addr;
         icmpObj->odstaddr  = ArgusThisIpHdr->ip_dst.s_addr;

         if (ICMP_INFOTYPE(icmp->icmp_type)) {
            switch (icmp->icmp_type) {
               case ICMP_ECHO:
               case ICMP_IREQ:
               case ICMP_MASKREQ:
               case ICMP_TSTAMP:
                  icmpObj->status = ARGUS_REQUEST;
                  break;

               case ICMP_MASKREPLY:
                  icmp->icmp_mask = ntohl(icmp->icmp_mask);
                  icmpObj->isrcaddr = icmp->icmp_mask;

               case ICMP_ECHOREPLY:
               case ICMP_IREQREPLY:
               case ICMP_TSTAMPREPLY:
                  icmpObj->status = ARGUS_REPLY;
                  break;
            }
         } else {
            struct ip oipbuf, *oip = &icmp->icmp_ip;

            if ((long) oip & (sizeof (long) - 1)) {
               bcopy ((char *)oip, (char *)&oipbuf, sizeof(struct ip));
               oip = &oipbuf;
            }

            icmpObj->isrcaddr  = ntohl(oip->ip_src.s_addr);
            icmpObj->idstaddr  = ntohl(oip->ip_dst.s_addr);
            icmpObj->igwaddr   = ntohl(icmp->icmp_gwaddr.s_addr);
         }
      }
   } else {
      flowstr->ArgusTimeout = ARGUS_IPTIMEOUT;
      if (((icmpObj = flowstr->NetworkDSRBuffer) != NULL) &&
                                       (icmpObj->type == ARGUS_ICMP_DSR)) {
         if (ICMP_INFOTYPE(icmp->icmp_type)) {
            if ((flowstr->state.src.count == 0) && (flowstr->state.dst.count == 0))
               icmpObj->icmp_type = icmp->icmp_type;
   
            switch (icmp->icmp_type) {
               case ICMP_ECHO:
               case ICMP_IREQ:
               case ICMP_TSTAMP:
               case ICMP_MASKREQ:
                  if (ArgusResponseStatus && (icmpObj->status == ARGUS_REQUEST)) {
                     ArgusSendFlowRecord (flowstr, ARGUS_STATUS);
                  }
   
                  *state = ARGUS_START;
                  icmpObj->icmp_type = icmp->icmp_type;
   
                  ArgusTallyTime (flowstr, *state);
                  ArgusTallyStats (flowstr, *state);
   
                  icmpObj->status = ARGUS_REQUEST;
                  break;
   
               case ICMP_ECHOREPLY:
               case ICMP_IREQREPLY:
               case ICMP_TSTAMPREPLY:
               case ICMP_MASKREPLY:
                  ArgusInProtocol = 1;
                  ArgusTallyTime (flowstr, *state);
                  ArgusTallyStats (flowstr, *state);
                  flowstr->state.status |= ARGUS_CONNECTED;
   
                  if (ArgusResponseStatus && (icmpObj->status == ARGUS_REQUEST))
                     ArgusSendFlowRecord (flowstr, ARGUS_STATUS);
   
                  icmpObj->status = ARGUS_REPLY;
                  break;

               default:
                  ArgusTallyTime (flowstr, *state);
                  ArgusTallyStats (flowstr, *state);
                  if (flowstr->state.src.count && flowstr->state.dst.count)
                     flowstr->state.status |= ARGUS_CONNECTED;
                  else
                     flowstr->state.status &= ~ARGUS_CONNECTED;
                  break;
            }
         } else {
            ArgusTallyTime (flowstr, *state);
            ArgusTallyStats (flowstr, *state);
         }
      } else
         icmpObj = NULL;
   }

   if (!(ICMP_INFOTYPE(icmp->icmp_type))) {
      struct ArgusFlowStruct *flow = NULL;
      struct ArgusFlowStats *ArgusThisStats = NULL;
      struct ip *oip = &icmp->icmp_ip;
      int hlen = oip->ip_hl << 2;

      ArgusThisIpHdr = NULL;
      ArgusThisSnapEnd = ((unsigned char *)oip) + (hlen + 4);
      ArgusCreateIPFlow(oip);

      if (ArgusThisIpHdr) {
         ArgusThisUpHdr = (unsigned char *) ArgusThisIpHdr;
         ArgusThisSnapEnd = (unsigned char *) ArgusThisIpHdr;

         if ((flow = ArgusFindFlow()) != NULL) {
            if (flow->state.rev == ArgusThisDir)
               ArgusThisStats = &flow->state.src;
            else
               ArgusThisStats = &flow->state.dst;

            if (icmpObj && ((ArgusThisStats->ip_id == ArgusThisIpHdr->ip_id) ||
                            (ArgusThisStats->ip_id == htons(ArgusThisIpHdr->ip_id)))) {  /* who sends ip_id messed up? */
         
               if (flow->ICMPDSRBuffer == NULL)
                  if ((flow->ICMPDSRBuffer = (void *) ArgusCalloc (1, sizeof(struct ArgusICMPObject))) == NULL)
                     ArgusLog (LOG_ERR, "ArgusUpdateICMPState: ArgusCalloc %s", strerror(errno));

               bcopy (icmpObj, flow->ICMPDSRBuffer, sizeof(*icmpObj));

               switch (icmp->icmp_type) {
                  case ICMP_UNREACH:
                     flow->state.status |= ARGUS_ICMPUNREACH_MAPPED; break;
                  case ICMP_REDIRECT:
                     flow->state.status |= ARGUS_ICMPREDIREC_MAPPED; break;
                  case ICMP_TIMXCEED:
                     flow->state.status |= ARGUS_ICMPTIMXCED_MAPPED; break;
               }

               flow->state.lasttime = ArgusGlobalTime;

               if (ArgusResponseStatus && ((flow->state.src.count + flow->state.dst.count) == 1))
                  ArgusSendFlowRecord(flow, ARGUS_STOP);
            }
         }
      }

      ArgusThisIpHdr   = ArgusTempIpHdr;
      ArgusThisSnapEnd = ArgusTempSnapEnd;
   }

#ifdef ARGUSDEBUG
   ArgusDebug (8, "ArgusUpdateICMPState(0x%x, %d) returning\n", flowstr, state);
#endif 
}


#include <argus_out.h>
void
ArgusICMPFlowRecord (struct ArgusFlowStruct *flow, struct ArgusRecord *argus, unsigned char state)
{
   int length = 0;
   struct ArgusICMPObject *icmpObj = (struct ArgusICMPObject *) flow->NetworkDSRBuffer;

   if (icmpObj && ((length = argus->ahdr.length) > 0)) {
      bcopy ((char *)icmpObj, &((char *)argus)[argus->ahdr.length], sizeof(*icmpObj));
      argus->ahdr.length += sizeof(*icmpObj);
   }

#ifdef ARGUSDEBUG
   ArgusDebug (5, "ArgusICMPFlowRecord(0x%x, 0x%x, %d) returning\n", flow, argus, state);
#endif 
}

void
ArgusICMPMappedFlowRecord (struct ArgusFlowStruct *flow, struct ArgusRecord *argus, unsigned char state)
{
   int length = 0;
   struct ArgusICMPObject *icmpObj = (struct ArgusICMPObject *) flow->ICMPDSRBuffer;

   if (icmpObj && ((length = argus->ahdr.length) > 0)) {
      bcopy ((char *)icmpObj, &((char *)argus)[argus->ahdr.length], sizeof(*icmpObj));
      argus->ahdr.length += sizeof(*icmpObj);
   }

#ifdef ARGUSDEBUG
   ArgusDebug (5, "ArgusICMPMappedFlowRecord(0x%x, 0x%x, %d) returning\n", flow, argus, state);
#endif 
}