File: route_write.c

package info (click to toggle)
snmp 3.6-1
  • links: PTS
  • area: main
  • in suites: slink
  • size: 1,284 kB
  • ctags: 1,929
  • sloc: ansic: 18,710; sh: 585; makefile: 311
file content (529 lines) | stat: -rw-r--r-- 10,008 bytes parent folder | download
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
#include <sys/types.h>
#include <sys/param.h>
#include <sys/file.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#ifndef linux
#include <sys/mbuf.h>
#endif

#ifdef linux
/*
 * route setting is buggy and does currently not work due to a bug in
 * the snmp module - if you have a fix, let me know
 * (schoenfr@ibr.cs.tu-bs.de).
 * to compile with the old libc (v4.5.x) you may consider to define 
 * LIBC45X, but then this cannot work.
 *
 * with linux v2.1.16 rtentry is not useable. this code will probably
 * never be useable...
 */

/* hack: define this for old comatibility
         -> better upgrade to a newer libc (v4.6.20 or above): */
# undef LIBC45X

/** # ifndef LIBC45X
#  include <net/if_route.h>
# else **/
#  include <net/route.h>
/** # endif **/
#else
# include <net/route.h>
#endif
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>


#include "asn1.h"
#include "snmp.h"
#include "snmp_impl.h"
#include "snmp_vars.h"


#ifdef DEBUG
#define dfprintf	if (1) fprintf
#else
#define dfprintf	if (0) fprintf
#endif


int
addRoute (dstip, gwip, iff, flags)
u_long  dstip;
u_long  gwip;
u_long  iff;
u_short  flags;
{
#ifdef linux
    fprintf (stderr, "snmpd: adding routes is not supported.\n");
    return 0;
#else
    struct sockaddr_in     dst;
    struct sockaddr_in     gateway;
    int                    s;
    struct rtentry  route;

    dfprintf (stderr, "** addRoute: entered\n");

    s = socket(AF_INET, SOCK_RAW, 0);
    if (s<0) {
        perror("socket");
	return 0;
    }
    

    flags |= RTF_UP;

    dst.sin_family       = AF_INET;
    dst.sin_addr.s_addr  = htonl(dstip);


    gateway.sin_family        = AF_INET;
    gateway.sin_addr.s_addr   = htonl(gwip);

#ifdef LIBC45X
    /* sorry sir...*/
#else
    bcopy((char*) &dst, (char *) &route.rt_dst, sizeof(struct  sockaddr_in));
#endif
    bcopy((char*) &gateway, (char *) &route.rt_gateway, sizeof(struct  sockaddr_in));

    route.rt_flags = flags;
#ifdef LIBC45X
    /* sorry sir...*/
#else
    route.rt_hash  = iff;
#endif
    return (ioctl(s, SIOCADDRT , (caddr_t)&route));
#endif
}



int  delRoute(dstip, gwip, iff, flags)
u_long  dstip;
u_long  gwip;
u_long  iff;
u_short  flags;
{
#ifdef linux
    fprintf (stderr, "snmpd: deleting routes is not supported.\n");
    return 0;
#else
    struct sockaddr_in     dst;
    struct sockaddr_in     gateway;
    int                    s;
    struct rtentry  route;

    dfprintf (stderr, "** delRoute: entered\n");

    s = socket(AF_INET, SOCK_RAW, 0);
    if (s<0) {
        perror("socket");
	return 0;
    }
    

    flags |= RTF_UP;

    dst.sin_family       = AF_INET;
    dst.sin_addr.s_addr  = htonl(dstip);


    gateway.sin_family        = AF_INET;
    gateway.sin_addr.s_addr   = htonl(gwip);

#ifdef LIBC45X
    /* sorry sir...*/
#else
    bcopy((char*) &dst, (char *) &route.rt_dst, sizeof(struct  sockaddr_in));
#endif
    bcopy((char*) &gateway, (char *) &route.rt_gateway, sizeof(struct  sockaddr_in));

    route.rt_flags = flags;
#ifdef LIBC45X
    /* sorry sir...*/
#else
    route.rt_hash  = iff;
#endif

    return (ioctl(s, SIOCDELRT , (caddr_t)&route));
#endif
}



#define  MAX_CACHE   8


struct rtent {

    u_long    in_use;
    u_long    old_dst;
    u_long    old_nextIR;
    u_long    old_ifix;
    u_long    old_flags;

    u_long    rt_dst;            /*  main entries    */
    u_long    rt_ifix;
    u_long    rt_metric1;
    u_long    rt_nextIR;
    u_long    rt_type;
    u_long    rt_proto;

    u_long    xx_dst;            /*  shadow entries  */
    u_long    xx_ifix;
    u_long    xx_metric1;
    u_long    xx_nextIR;
    u_long    xx_type;
    u_long    xx_proto;
};



struct  rtent  rtcache[MAX_CACHE];


struct rtent *
findCacheRTE(dst)
u_long dst;
{
    int i;

    dfprintf (stderr, "** findCacheRTE: entered\n");

    for (i = 0; i < MAX_CACHE; i++) {
	
	if (rtcache[i].in_use && (rtcache[i].rt_dst == dst)) {  /* valid & match? */
	    return (&rtcache[i]);
	}
    }
    return 0;
}



struct rtent *
#ifdef linux
newCacheRTE (dst)
u_long dst;
#else
newCacheRTE ()
#endif
{
    int i;

    dfprintf (stderr, "** newCacheRTE: entered\n");

    for (i = 0; i < MAX_CACHE; i++) {
	
	if (!rtcache[i].in_use) {
#ifdef linux
	    rtcache[i].rt_dst = dst;
#endif
	    rtcache[i].in_use = 1;
	    return (&rtcache[i]);
	}
    }
    return 0;

}


int 
delCacheRTE(dst)
u_long dst;
{
    struct  rtent  *rt;

    dfprintf (stderr, "** delCacheRTE: entered\n");

    rt = findCacheRTE(dst);
    if (!rt) {
	return 0;
    }

    rt->in_use = 0;
    return 1;
}



struct  rtent  *
cacheKernelRTE(dst)
u_long dst;
{
#ifdef linux
    struct  rtent  *rt;

    dfprintf (stderr, "** cacheKernelRTE: entered\n");

    rt = findCacheRTE(dst);
    if (!rt)
	return 0;

    rt->rt_dst = dst;
    return rt;

#else /* ! linux */
    return 0;  /* for now */

    /* ...... */
#endif /* ! linux */
}



/*
 * If statP is non-NULL, the referenced object is at that location.
 * If statP is NULL and ap is non-NULL, the instance exists, but not this variable.
 * If statP is NULL and ap is NULL, then neither this instance nor the variable exists.
 */

int
write_rte (action, var_val, var_val_type, var_val_len, statP, name, length)
   int      action;
   u_char   *var_val;
   u_char   var_val_type;
   int      var_val_len;
   u_char   *statP;
   oid      *name;
   int      length;
{
    struct rtent *rp;
    int var;
    long val;
    u_long  dst;
    char    buf[8];
    int     bufsz;
    u_short  flags;
    int      oldty;

    /*
     * object identifier is of form:
     * 1.3.6.1.2.1.4.21.1.X.A.B.C.D ,  where A.B.C.D is IP address.
     * IPADDR starts at offset 10.
     */

    if (length != 14) {
	ERROR("length error");
	return SNMP_ERR_NOCREATION;
    }


    dfprintf (stderr, "** write_rte: entered\n");

    var = name[9];
    
    dst = *((u_long *) & name[10] );

    rp = findCacheRTE(dst);

    if (!rp) {
	rp = cacheKernelRTE(dst);
    }


    if (action == RESERVE1 && !rp) {
#ifdef linux
	rp = newCacheRTE(dst);
#else
	rp = newCacheRTE();
#endif
	if (!rp) {
	    ERROR("newCacheRTE");
	    return SNMP_ERR_RESOURCEUNAVAILABLE;
	}
	rp->rt_type = rp->xx_type = 2;

    } else if (action == COMMIT){

	dfprintf (stderr, "** COMMIT ...\n");

    } else if (action == FREE) {
	if (rp->rt_type == 2) {  /* was invalid before */
	    delCacheRTE(dst);
	}
    }

    
    switch(var){

	case IPROUTEDEST:
	   
            if (action == RESERVE1){

		if (var_val_type != ASN_OCTET_STR) {
		    ERROR("not octet");
		    return SNMP_ERR_WRONGTYPE;
		}

		bufsz = 8;
		asn_parse_string(var_val, &var_val_len, &var_val_type, buf, &bufsz);

		if (var_val_type != ASN_OCTET_STR) {
		    ERROR("not octet2");
		    return SNMP_ERR_WRONGTYPE;
		}
		
		rp->xx_dst = *((u_long *) buf);
		

	    } else if (action == COMMIT) {
		rp->rt_dst = rp->xx_dst;
	    }
	    break;

	case IPROUTEMETRIC1:

	    if (action == RESERVE1) {
		if (var_val_type != ASN_INTEGER) {
		    ERROR("not int1");
		    return SNMP_ERR_WRONGTYPE;
		}
		
		asn_parse_int(var_val, &var_val_len, &var_val_type, &val, sizeof(val));

		if (val < -1) {
		    ERROR("not right1");
		    return SNMP_ERR_WRONGVALUE;
		}

		rp->xx_metric1 = val;

	    } else if (action == RESERVE2) {

		if ((rp->xx_metric1 == 1) && (rp->xx_type != 4)) {
		    ERROR("reserve2 failed\n");
		    return SNMP_ERR_WRONGVALUE;
		}

	    } else if (action == COMMIT) {
		rp->rt_metric1 = rp->xx_metric1;
	    }
	    break;

	case IPROUTEIFINDEX:

	    if (action == RESERVE1) {
		if (var_val_type != ASN_INTEGER) {
		    ERROR("not right2");
		  return SNMP_ERR_WRONGTYPE;
		}
		
		asn_parse_int(var_val, &var_val_len, &var_val_type, &val, sizeof(val));

		if (val <= 0) {
		    ERROR("not right3");
		    return SNMP_ERR_WRONGVALUE;
		}

		rp->xx_ifix = val;

	    } else if (action == COMMIT) {
		rp->rt_ifix = rp->xx_ifix;
	    }
	    break;	    
	    
	case IPROUTENEXTHOP:
	   
            if (action == RESERVE1){

		if (var_val_type != ASN_OCTET_STR) {
		    ERROR("not right4");
		  return SNMP_ERR_WRONGTYPE;
		}

		bufsz = 8;
		asn_parse_string(var_val, &var_val_len, &var_val_type, buf, &bufsz);

		if (var_val_type != ASN_OCTET_STR) {
		    ERROR("not right5");
		    return SNMP_ERR_WRONGTYPE;
		}
		
		rp->xx_nextIR = *((u_long *) buf);

	    } else if (action == COMMIT) {
		rp->rt_nextIR = rp->xx_nextIR;
	    }
	  

	case IPROUTETYPE:

	    /*
	     *  flag meaning:
	     *
	     *  IPROUTEPROTO (rt_proto): none: (cant set == 3 (netmgmt)) 
	     *
	     *  IPROUTEMETRIC1:  1 iff gateway, 0 otherwise
	     *  IPROUTETYPE:     4 iff gateway, 3 otherwise
	     */

	    if (action == RESERVE1) {
		if (var_val_type != ASN_INTEGER) {
		  return SNMP_ERR_WRONGTYPE;
		}
		
		asn_parse_int(var_val, &var_val_len, &var_val_type, &val, sizeof(val));

		if ((val < 2) || (val > 4)) { /* only accept invalid, direct, indirect */
		    ERROR("not right6");
		    return SNMP_ERR_WRONGVALUE;
		}

		rp->xx_type = val;

	    } else if (action == COMMIT) {
		
		oldty = rp->rt_type;
		rp->rt_type = rp->xx_type;
		
		if (rp->rt_type == 2) {  /* invalid, so delete from kernel */

		    if (delRoute(rp->rt_dst, rp->rt_nextIR, rp->rt_ifix , rp->old_flags ) < 0) {
			perror("delRoute");
		    }

		} else {

		    /* it must be valid now, so flush to kernel */
		    
		    if (oldty != 2) {   /* was the old entry valid ?  */
			if (delRoute(rp->old_dst, rp->old_nextIR, rp->old_ifix , rp->old_flags ) < 0) {
			    perror("delRoute");
			}
		    }

		    /* not invalid, so remove from cache */
		    
		    flags = (rp->rt_type == 4 ? RTF_GATEWAY : 0);

		    if (addRoute(rp->rt_dst, rp->rt_nextIR, rp->rt_ifix , flags) < 0) {
			perror("addRoute");
		    }

		    delCacheRTE( rp->rt_type );
		}
	    }
	    break;

	case IPROUTEPROTO:

	default:
	    ERROR("err default");
	    return SNMP_ERR_NOCREATION;
    }


    return SNMP_ERR_NOERROR;
}