File: ip_options.c

package info (click to toggle)
sing 1.1-13
  • links: PTS
  • area: main
  • in suites: etch-m68k
  • size: 2,480 kB
  • ctags: 2,092
  • sloc: ansic: 20,740; sh: 4,857; makefile: 718; yacc: 234; lex: 203
file content (418 lines) | stat: -rw-r--r-- 9,593 bytes parent folder | download | duplicates (5)
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/******************************************/
/* Functions that send and receive the IP */
/* Options Header. Types supported:       */
/*   - Loose Source Routing.              */
/*   - Strict Source Routing.             */
/*   - Record Routing.                    */
/******************************************/
#ifndef lint
static const char rcsid[] = 
       "$Id: ip_options.c,v 1.4 2000/10/04 17:40:54 slay Exp $";
#endif

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>

#ifdef HAVE_NETINET_IN_SYSTM_H
#include <netinet/in_systm.h>
#else
#ifdef HAVE_NETINET_IN_SYSTEM_H
#include <netinet/in_system.h>
#endif
#endif

#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#ifdef HAVE_STRING_H
#include <string.h>
#endif

#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif

#ifdef HAVE_BSTRING_H
#include <bstring.h>
#endif

#ifdef STDC_HEADERS
#include <stdlib.h>
#endif

#include <ctype.h>

#if defined(FREEBSD) || defined(OPENBSD) || defined(NETBSD)
#include<sys/sysctl.h>
#endif

#ifdef LINUX
#include <linux/version.h>
#ifndef KERNEL_VERSION
#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
#endif
#else
#  define LINUX_VERSION_CODE 0
#  define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
#endif

#include "ip_options.h"

void 
print_ip_opt(void)
{
  if ( !packet.len_ipopt )
  {
     write_log(1,"IPOPT rx!!");  
     return;
  }
 
  if ( packet.rr )
     print_rr();
  else
     if ( verbose )
        print_lssrr();
}


/*******************************************/
/* Print a received Record Route IP Option */
/*******************************************/
void 
print_rr( void )
{
  u_char *cursor = ip_opt_rcv, *opt_len = NULL;
  static char rroute[IP_OPT_DFL];
  static int rroute_len;
  u_int loop, len;
  struct in_addr gway;

  if ( *cursor != RR_OPT )
  {
     write_log(1,"Sent RR -> received %d (len %d)", *cursor, *(++cursor));
     return;
  }

  ++cursor;
  opt_len = cursor;

  if ( (*opt_len > IP_OPT_DFL) || (*opt_len < 8))
  {
    write_log(1,"Over/Undersized IP Options length!!");
    return;
  } 
  
  if (*opt_len==rroute_len && !memcmp(ip_opt_rcv, rroute, *opt_len) )
  {
     write_log(1," (same song)");
     return;
  }

  rroute_len=*opt_len;
  copymem(ip_opt_rcv, rroute, *opt_len);
  
  cursor+=2; 
  len = 3;
  loop = 1;
 
  while ( len <= opt_len_rcv )
  {
     if ( *cursor == END_OPT )
        return;
     copymem( cursor, (u_char *)&gway, 4);
     write_log(1,"\n                   RR%d = %s", loop++,
                    inet_ntoa(gway));
     cursor+=4;
     len+=4;
  }
}


/*******************************************/
/* Print a received Loose or Strict Source */
/* Route Option.                           */
/*******************************************/
void 
print_lssrr( void )
{
  u_char *cursor = ip_opt_rcv, symbol;
  static char sr[IP_OPT_DFL];
  static int sr_len;
  u_int len=0, flag=0;

  struct in_addr gway;

  if ( (opt_len_rcv > IP_OPT_DFL) || (opt_len_rcv < 3))
  {
     write_log(1,"Over/Undersized IP Options length!!");
     return;
  }

  if (opt_len_rcv==sr_len && !memcmp(ip_opt_rcv, sr, opt_len_rcv) )
  {
     write_log(1," (same song)");
     return;
  }

  sr_len=opt_len_rcv;
  copymem(ip_opt_rcv, sr, opt_len_rcv);
  
  while( (*cursor == NOOP_OPT) && (len <= opt_len_rcv)  )
  {
     cursor++;
     len++;
  }

  if ( (*cursor != LSRR_OPT) && (*cursor != SSRR_OPT) )
  {
     write_log(1,"IPOPT received %d (len %d)!!", *cursor, *(++cursor));
     return;
  }

  symbol = (*cursor == LSRR_OPT)?LOOSE_SYM:STRICT_SYM;
  
  cursor+=3;
  len+=3;
  write_log(1,"\n              SR= ");
  while ( len < opt_len_rcv )
  {
     if ( *cursor == END_OPT )
        return;
     copymem( cursor, (u_char *)&gway, 4);
     if ( flag )
        write_log(1,"%c", symbol );
     write_log(1,"%s", inet_ntoa(gway) );
     flag = 1;
     cursor+=4;
     len+=4;
  }
}


/************************************/
/* Does the destination host have a */
/* '!' or a '@'?                    */ 
/* If yes then make a LSRR or SSRR  */
/* else make a RR option.           */
/* **********************************/
void 
ip_opt_control( char *host )
{
   u_char *punt, *ip_opt;
   
   if ( ( punt = strpbrk(host, SR_SYMS) ) )
   {
      packet.ipopt = (char *)malloc(IP_OPT_DFL);
      if (!packet.ipopt)
         go_out_error(3,"Out of memory on ip_opt_control");
      initmem(packet.ipopt, IP_OPT_DFL);
      packet.rr = 0;
      if ( *punt == LOOSE_SYM )  /* LSRR */
      {
         make_ipopt( host, LSRR_OPT );
      }
      if ( *punt == STRICT_SYM ) /* SSRR */
      {
         make_ipopt( host, SSRR_OPT );
      }
   }
   else
   {
      if ( packet.rr ) /* If RR activated... */
      {
         packet.len_ipopt = IP_OPT_DFL;
         ip_opt = packet.ipopt = (char *)malloc(IP_OPT_DFL);
         initmem(packet.ipopt, IP_OPT_DFL);
         *ip_opt++ = RR_OPT;         /* RR IP Option Code */
         *ip_opt++ = IP_OPT_DFL - 1; /* Length            */
         *ip_opt   = 4;              /* IP Option Offset  */
      }
   }
}

/*******************************/
/* Parse the gateways for a    */
/* LSRR or SSRR IP option and  */
/* build the IP Option header. */
/*******************************/
void 
make_ipopt( char *host, int code )
{
   u_char *opt_cursor, *opt_len, *host_cursor, *cursor, symbol;
   u_long *gway_cursor;
#ifdef SOLARIS   
   u_short aux=0;
#endif   
   int hay_host = 0, i;
   
   symbol = ( code == LSRR_OPT)?LOOSE_SYM:STRICT_SYM;
   
   opt_cursor = packet.ipopt = (char *)malloc(IP_OPT_DFL);
   initmem(packet.ipopt, IP_OPT_DFL);
   
   *opt_cursor++ = NOOP_OPT;
   *opt_cursor++ = code;    /* IP Option Code          */
   opt_len = opt_cursor++;  /* Length. Filled in later */
   *opt_cursor++   = 4;     /* IP Option Pointer       */
   
   cursor = host_cursor = host;   

   while ( *cursor && (cont_gways < 10) )
   {
      if ( *cursor == symbol )
      {
         if ( hay_host )
         {
            if ( cont_gways == 9 )
               go_out(1, "Can't handle more than 10 gateways");

            *cursor=0;
            hay_host = 0;
            if ( exist_host( host_cursor, &gway_ip[cont_gways]) )
               go_out(1, "Wrong gateway address/name on IP %s option -> %s",
                          (code==LSRR_OPT)?"LSRR":"SSRR", host_cursor);
            cont_gways++;  
            host_cursor=cursor+1;
         }
         else
            host_cursor++;
      }
      else
         hay_host = 1;
     cursor++;
   }

   if ( hay_host )
   {
      if ( exist_host( host_cursor, &gway_ip[cont_gways]) )
         go_out(1, "Wrong gateway address/name on IP %s option -> %s", 
                    (code==LSRR_OPT)?"LSRR":"SSRR", host_cursor);
      cont_gways++;
   }
   if ( cont_gways < 2 ) /* If only 1 host discard IP Options header */
   {
      free(packet.ipopt);
      packet.ipopt = NULL;
      return;
   }

   gway_cursor = (u_long *)opt_cursor;
   
#ifdef SOLARIS
   if (!packet.mac_src)
      aux=1;
   *opt_len = ( (cont_gways - aux) << 2 ) + 3 ;
   for (i=aux; i <= (cont_gways-1); i++)
#else
   *opt_len = ( (cont_gways - 1) << 2 ) + 3 ;
   for (i=1; i <= (cont_gways-1); i++)   
#endif
   {
      copymem((char *)&gway_ip[i], (char *)gway_cursor, 4);
      gway_cursor++;
   }
   packet.len_ipopt = *opt_len + 1;   
}


/***************************************/
/* Verify if Source Routing is enabled */
/* on BSD systems.                     */
/* Return -1 on error.                 */
/* Return 1 if SRouting enabled.       */
/* Return 0 if SRouting disabled.      */
/***************************************/
#if defined(FREEBSD) || defined(NETBSD)
#define SR "net.inet.ip.accept_sourceroute"
u_char 
vrfy_sr(void)
{
  u_int sroute;
  size_t len;
  len = sizeof(sroute);

  if ( sysctlbyname( SR, &sroute, &len, NULL, 0) == -1 )
     return -1;
  return((sroute)?1:0);
}
#endif  /* Verify Source Routing on BSD systems */


/***************************************/
/* Verify if Source Routing is enabled */
/* on Linux >= 2.2.x systems.          */
/* Return -1 on error.                 */
/* Return 1 if SRouting is enabled.    */
/* Return 0 if SRouting is disabled.   */
/***************************************/
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0))
u_char 
vrfy_sr(void)
{
  FILE *fp;
  u_int sroute;
  char *path1="/proc/sys/net/ipv4/conf/";
  char *path2="/accept_source_route";
  char *path3="/proc/sys/net/ipv4/conf/all/accept_source_route";
  char *iface=packet.listen2dev?packet.listen2dev:packet.iface2route.name;
  char *path2proc=(char *)calloc(64,1);
  
  if (!path2proc)
     go_out_error(3, "Out of memory on path2proc");
  
  if ( (strlen(path1)+strlen(iface)+strlen(path2)) > 64 )
  {
     free(path2proc);
     return -1;
  }

  strncpy(path2proc,path1,strlen(path1));
  strncat(path2proc,iface,strlen(iface));
  strncat(path2proc,path2,strlen(path2));

#ifdef DEBUG
  printf("PATH2PROC = %s\n",path2proc);    
#endif

  if ((fp=fopen( path2proc, "r")) == NULL)
  {
     printf("error fopen\n");
     return -1;
  }

  if ( (sroute=fgetc(fp)) == EOF )
  {
     printf("error fgetc\n");
     fclose(fp);
     free(path2proc);
     return -1;
  }
  fclose(fp);
  free(path2proc);

  if (sroute==48)
     return 0;

  if ((fp=fopen( path3, "r")) == NULL)
     return -1;

  if ( (sroute=fgetc(fp)) == EOF )
  {
     fclose(fp);
     return -1;
  }
  fclose(fp);
  return((sroute==48)?0:1);
}
#endif /* Verify Source Routing on Linux >= 2.2.x systems */