-
Notifications
You must be signed in to change notification settings - Fork 22
/
fripadm_white_in_exe.c
586 lines (465 loc) · 14.3 KB
/
fripadm_white_in_exe.c
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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<linux/ioctl.h>
/********/
#ifndef FR_IP_MEMDEV_MAJOR
#define FR_IP_MEMDEV_MAJOR 0
#endif
#ifndef FR_IP_MEMDEV_NR_DEVS
#define FR_IP_MEMDEV_NR_DEVS 2
#endif
#ifndef FR_IP_MEMDEV_SIZE
#define FR_IP_MEMDEV_SIZE 4096
#endif
/* for fr_ip_ioctl_para_t's type */
/* in-black ip */
#define FR_IP_IOCTL_TYPE_FIND 1
#define FR_IP_IOCTL_TYPE_FIND_BYSTRINGS 2 // * :)
#define FR_IP_IOCTL_TYPE_MIRROR_INSERT_IP 3
#define FR_IP_IOCTL_TYPE_MIRROR_INSERT_BYSTRINGS 4 // * :)
#define FR_IP_IOCTL_TYPE_MIRROR_DELETE_IP 5 // * :)
#define FR_IP_IOCTL_TYPE_MIRROR_DELETE_IP_RANDOMLY 6 //* :)
#define FR_IP_IOCTL_TYPE_MIRROR_DELETE_BYSTRINGS 7 // * :)
#define FR_IP_IOCTL_TYPE_MIRROR_DELETE_ALL 8 //* :)
#define FR_IP_IOCTL_TYPE_SWITCH_MIRROR_UPDATE 9 // * :)
#define FR_IP_IOCTL_TYPE_REBUILD 10 // * :)
#define FR_IP_IOCTL_TYPE_COPY_HASH_STRUCT 11 // *
#define FR_IP_IOCTL_TYPE_DUMP 12 //* :)
#define FR_IP_IOCTL_TYPE_MIRROR_INSERT_IP_BINS 13 //* :)
#define FR_IP_IOCTL_TYPE_MIRROR_DELETE_IP_BINS 14 //* :)
/* in-white ip */
#define FR_IP_IOCTL_TYPE_WHITE_FIND 101
#define FR_IP_IOCTL_TYPE_WHITE_FIND_BYSTRINGS 102 // * :)
#define FR_IP_IOCTL_TYPE_WHITE_MIRROR_INSERT_IP 103
#define FR_IP_IOCTL_TYPE_WHITE_MIRROR_INSERT_BYSTRINGS 104 // * :)
#define FR_IP_IOCTL_TYPE_WHITE_MIRROR_DELETE_IP 105
#define FR_IP_IOCTL_TYPE_WHITE_MIRROR_DELETE_IP_RANDOMLY 106 // * :)
#define FR_IP_IOCTL_TYPE_WHITE_MIRROR_DELETE_BYSTRINGS 107 // * :)
#define FR_IP_IOCTL_TYPE_WHITE_MIRROR_DELETE_ALL 108 //* :)
#define FR_IP_IOCTL_TYPE_WHITE_SWITCH_MIRROR_UPDATE 109 // * :)
#define FR_IP_IOCTL_TYPE_WHITE_REBUILD 110 // * :)
#define FR_IP_IOCTL_TYPE_WHITE_COPY_HASH_STRUCT 111 // *
#define FR_IP_IOCTL_TYPE_WHITE_DUMP 112 //* :)
#define FR_IP_IOCTL_TYPE_WHITE_MIRROR_INSERT_IP_BINS 113 //* :)
#define FR_IP_IOCTL_TYPE_WHITE_MIRROR_DELETE_IP_BINS 114 //* :)
struct fr_ip_mem_dev
{
char *data;
unsigned long size;
};
struct fr_ip_ioctl_para_t {
void * ptr;
unsigned int size;
unsigned int type;
unsigned int ret;
};
#define FR_IP_MEMDEV_IOC_MAGIC 'k'
#define FR_IP_MEMDEV_IOCPRINT _IO(FR_IP_MEMDEV_IOC_MAGIC, 1)
#define FR_IP_MEMDEV_IOCGETDATA _IOR(FR_IP_MEMDEV_IOC_MAGIC, 2, int)
#define FR_IP_MEMDEV_IOCSETDATA _IOW(FR_IP_MEMDEV_IOC_MAGIC, 3, int)
#define FR_IP_MEMDEV_IOC_MAXNR 3
/********/
struct fr_ip_ioctl_para_t gl_para;
static unsigned int double_hash_find_bystrings(int fd,char * str, unsigned int size) {
if ( (fd<0)||(str==NULL)||(size==0) ){
fprintf(stderr,"double_hash_find_bystrings input invalid!\n");
return 3;
}
gl_para.ptr=str;
gl_para.size=size;
gl_para.type=FR_IP_IOCTL_TYPE_FIND_BYSTRINGS;
if (ioctl(fd, FR_IP_MEMDEV_IOCSETDATA, &gl_para) < 0)
{
fprintf(stderr,"double_hash_find_bystrings cmd MEMDEV_IOCSETDATA fail\n");
return 4;
}
return gl_para.ret;
}
static unsigned int double_hash_mirror_insert_bystrings(int fd,char * str, unsigned int size) {
if ( (fd<0)||(str==NULL)||(size==0) ){
fprintf(stderr,"double_hash_mirror_insert_bystrings input invalid!\n");
return 3;
}
gl_para.ptr=str;
gl_para.size=size;
gl_para.type=FR_IP_IOCTL_TYPE_MIRROR_INSERT_BYSTRINGS;
if (ioctl(fd, FR_IP_MEMDEV_IOCSETDATA, &gl_para) < 0)
{
fprintf(stderr,"double_hash_mirror_insert_bystrings cmd MEMDEV_IOCSETDATA fail\n");
return 4;
}
return gl_para.ret;
}
static unsigned int double_hash_mirror_insert_bins(int fd,char * str, unsigned int size) {
if ( (fd<0)||(str==NULL)||(size==0) ){
fprintf(stderr,"double_hash_mirror_insert_bins input invalid!\n");
return 3;
}
gl_para.ptr=str;
gl_para.size=size;
gl_para.type=FR_IP_IOCTL_TYPE_MIRROR_INSERT_IP_BINS;
if (ioctl(fd, FR_IP_MEMDEV_IOCSETDATA, &gl_para) < 0)
{
fprintf(stderr,"double_hash_mirror_insert_bins cmd MEMDEV_IOCSETDATA fail\n");
return 4;
}
return gl_para.ret;
}
static unsigned int double_hash_mirror_delete_bystrings(int fd,char * str, unsigned int size) {
if ( (fd<0)||(str==NULL)||(size==0) ){
fprintf(stderr,"double_hash_mirror_delete_bystrings input invalid!\n");
return 3;
}
gl_para.ptr=str;
gl_para.size=size;
gl_para.type=FR_IP_IOCTL_TYPE_MIRROR_DELETE_BYSTRINGS;
if (ioctl(fd, FR_IP_MEMDEV_IOCSETDATA, &gl_para) < 0)
{
fprintf(stderr,"double_hash_mirror_delete_bystrings cmd MEMDEV_IOCSETDATA fail\n");
return 4;
}
return gl_para.ret;
}
static unsigned int double_hash_mirror_delete_bins(int fd,char * str, unsigned int size) {
if ( (fd<0)||(str==NULL)||(size==0) ){
fprintf(stderr,"double_hash_mirror_delete_bins input invalid!\n");
return 3;
}
gl_para.ptr=str;
gl_para.size=size;
gl_para.type=FR_IP_IOCTL_TYPE_MIRROR_DELETE_IP_BINS;
if (ioctl(fd, FR_IP_MEMDEV_IOCSETDATA, &gl_para) < 0)
{
fprintf(stderr,"double_hash_mirror_delete_bins cmd MEMDEV_IOCSETDATA fail\n");
return 4;
}
return gl_para.ret;
}
static unsigned int double_hash_mirror_delete_ip_randomly(int fd,unsigned int size) {
if ( (fd<0)||(size==0) ){
fprintf(stderr,"double_hash_mirror_delete_ip_randomly input invalid!\n");
return 3;
}
gl_para.size=size;
gl_para.type=FR_IP_IOCTL_TYPE_MIRROR_DELETE_IP_RANDOMLY;
if (ioctl(fd, FR_IP_MEMDEV_IOCSETDATA, &gl_para) < 0)
{
fprintf(stderr,"double_hash_mirror_delete_ip_randomly cmd MEMDEV_IOCSETDATA fail\n");
return 4;
}
return gl_para.ret;
}
static unsigned int double_hash_mirror_delete_all(int fd) {
if ( (fd<0) ){
fprintf(stderr,"double_hash_mirror_delete_all input invalid!\n");
return 3;
}
gl_para.type=FR_IP_IOCTL_TYPE_MIRROR_DELETE_ALL;
if (ioctl(fd, FR_IP_MEMDEV_IOCSETDATA, &gl_para) < 0)
{
fprintf(stderr,"double_hash_mirror_delete_all cmd MEMDEV_IOCSETDATA fail\n");
return 4;
}
return gl_para.ret;
}
static unsigned int double_hash_switch_mirror_update(int fd) {
if ( (fd<0) ){
fprintf(stderr,"double_hash_switch_mirror_update input invalid!\n");
return 3;
}
gl_para.type=FR_IP_IOCTL_TYPE_SWITCH_MIRROR_UPDATE;
if (ioctl(fd, FR_IP_MEMDEV_IOCSETDATA, &gl_para) < 0)
{
fprintf(stderr,"double_hash_switch_mirror_update cmd MEMDEV_IOCSETDATA fail\n");
return 4;
}
return gl_para.ret;
}
static unsigned int double_hash_rebuild(int fd,unsigned int modular, unsigned int rnd) {
unsigned int in[2];
if ( (fd<0)||(modular==0) ){
fprintf(stderr,"double_hash_rebuild input invalid!\n");
return 3;
}
in[0]=modular;
in[1]=rnd;
gl_para.ptr=in;
gl_para.size=sizeof(unsigned int)*2;
gl_para.type=FR_IP_IOCTL_TYPE_REBUILD;
if (ioctl(fd, FR_IP_MEMDEV_IOCSETDATA, &gl_para) < 0)
{
fprintf(stderr,"double_hash_rebuild cmd MEMDEV_IOCSETDATA fail\n");
return 4;
}
return gl_para.ret;
}
static unsigned int double_hash_dump(int fd) {
if ( (fd<0) ){
fprintf(stderr,"double_hash_dump input invalid!\n");
return 3;
}
gl_para.type=FR_IP_IOCTL_TYPE_DUMP;
if (ioctl(fd, FR_IP_MEMDEV_IOCSETDATA, &gl_para) < 0)
{
fprintf(stderr,"double_hash_switch_mirror_update cmd MEMDEV_IOCSETDATA fail\n");
return 4;
}
return gl_para.ret;
}
static unsigned int double_hash_white_find_bystrings(int fd,char * str, unsigned int size) {
if ( (fd<0)||(str==NULL)||(size==0) ){
fprintf(stderr,"double_hash_white_find_bystrings input invalid!\n");
return 3;
}
gl_para.ptr=str;
gl_para.size=size;
gl_para.type=FR_IP_IOCTL_TYPE_WHITE_FIND_BYSTRINGS;
if (ioctl(fd, FR_IP_MEMDEV_IOCSETDATA, &gl_para) < 0)
{
fprintf(stderr,"double_hash_white_find_bystrings cmd MEMDEV_IOCSETDATA fail\n");
return 4;
}
return gl_para.ret;
}
static unsigned int double_hash_white_mirror_insert_bystrings(int fd,char * str, unsigned int size) {
if ( (fd<0)||(str==NULL)||(size==0) ){
fprintf(stderr,"double_hash_white_mirror_insert_bystrings input invalid!\n");
return 3;
}
gl_para.ptr=str;
gl_para.size=size;
gl_para.type=FR_IP_IOCTL_TYPE_WHITE_MIRROR_INSERT_BYSTRINGS;
if (ioctl(fd, FR_IP_MEMDEV_IOCSETDATA, &gl_para) < 0)
{
fprintf(stderr,"double_hash_white_mirror_insert_bystrings cmd MEMDEV_IOCSETDATA fail\n");
return 4;
}
return gl_para.ret;
}
static unsigned int double_hash_white_mirror_insert_bins(int fd,char * str, unsigned int size) {
if ( (fd<0)||(str==NULL)||(size==0) ){
fprintf(stderr,"double_hash_white_mirror_insert_bins input invalid!\n");
return 3;
}
gl_para.ptr=str;
gl_para.size=size;
gl_para.type=FR_IP_IOCTL_TYPE_WHITE_MIRROR_INSERT_IP_BINS;
if (ioctl(fd, FR_IP_MEMDEV_IOCSETDATA, &gl_para) < 0)
{
fprintf(stderr,"double_hash_white_mirror_insert_bins cmd MEMDEV_IOCSETDATA fail\n");
return 4;
}
return gl_para.ret;
}
static unsigned int double_hash_white_mirror_delete_bystrings(int fd,char * str, unsigned int size) {
if ( (fd<0)||(str==NULL)||(size==0) ){
fprintf(stderr,"double_hash_white_mirror_delete_bystrings input invalid!\n");
return 3;
}
gl_para.ptr=str;
gl_para.size=size;
gl_para.type=FR_IP_IOCTL_TYPE_WHITE_MIRROR_DELETE_BYSTRINGS;
if (ioctl(fd, FR_IP_MEMDEV_IOCSETDATA, &gl_para) < 0)
{
fprintf(stderr,"double_hash_white_mirror_delete_bystrings cmd MEMDEV_IOCSETDATA fail\n");
return 4;
}
return gl_para.ret;
}
static unsigned int double_hash_white_mirror_delete_bins(int fd,char * str, unsigned int size) {
if ( (fd<0)||(str==NULL)||(size==0) ){
fprintf(stderr,"double_hash_white_mirror_delete_bins input invalid!\n");
return 3;
}
gl_para.ptr=str;
gl_para.size=size;
gl_para.type=FR_IP_IOCTL_TYPE_WHITE_MIRROR_DELETE_IP_BINS;
if (ioctl(fd, FR_IP_MEMDEV_IOCSETDATA, &gl_para) < 0)
{
fprintf(stderr,"double_hash_white_mirror_delete_bins cmd MEMDEV_IOCSETDATA fail\n");
return 4;
}
return gl_para.ret;
}
static unsigned int double_hash_white_mirror_delete_ip_randomly(int fd,unsigned int size) {
if ( (fd<0)||(size==0) ){
fprintf(stderr,"double_hash_white_mirror_delete_ip_randomly input invalid!\n");
return 3;
}
gl_para.size=size;
gl_para.type=FR_IP_IOCTL_TYPE_WHITE_MIRROR_DELETE_IP_RANDOMLY;
if (ioctl(fd, FR_IP_MEMDEV_IOCSETDATA, &gl_para) < 0)
{
fprintf(stderr,"double_hash_white_mirror_delete_ip_randomly cmd MEMDEV_IOCSETDATA fail\n");
return 4;
}
return gl_para.ret;
}
static unsigned int double_hash_white_mirror_delete_all(int fd) {
if ( (fd<0) ){
fprintf(stderr,"double_hash_white_mirror_delete_all input invalid!\n");
return 3;
}
gl_para.type=FR_IP_IOCTL_TYPE_WHITE_MIRROR_DELETE_ALL;
if (ioctl(fd, FR_IP_MEMDEV_IOCSETDATA, &gl_para) < 0)
{
fprintf(stderr,"double_hash_white_mirror_delete_all cmd MEMDEV_IOCSETDATA fail\n");
return 4;
}
return gl_para.ret;
}
static unsigned int double_hash_white_switch_mirror_update(int fd) {
if ( (fd<0) ){
fprintf(stderr,"double_hash_white_switch_mirror_update input invalid!\n");
return 3;
}
gl_para.type=FR_IP_IOCTL_TYPE_WHITE_SWITCH_MIRROR_UPDATE;
if (ioctl(fd, FR_IP_MEMDEV_IOCSETDATA, &gl_para) < 0)
{
fprintf(stderr,"double_hash_white_switch_mirror_update cmd MEMDEV_IOCSETDATA fail\n");
return 4;
}
return gl_para.ret;
}
static unsigned int double_hash_white_rebuild(int fd,unsigned int modular, unsigned int rnd) {
unsigned int in[2];
if ( (fd<0)||(modular==0) ){
fprintf(stderr,"double_hash_white_rebuild input invalid!\n");
return 3;
}
in[0]=modular;
in[1]=rnd;
gl_para.ptr=in;
gl_para.size=sizeof(unsigned int)*2;
gl_para.type=FR_IP_IOCTL_TYPE_WHITE_REBUILD;
if (ioctl(fd, FR_IP_MEMDEV_IOCSETDATA, &gl_para) < 0)
{
fprintf(stderr,"double_hash_white_rebuild cmd MEMDEV_IOCSETDATA fail\n");
return 4;
}
return gl_para.ret;
}
static unsigned int double_hash_white_dump(int fd) {
if ( (fd<0) ){
fprintf(stderr,"double_hash_white_dump input invalid!\n");
return 3;
}
gl_para.type=FR_IP_IOCTL_TYPE_WHITE_DUMP;
if (ioctl(fd, FR_IP_MEMDEV_IOCSETDATA, &gl_para) < 0)
{
fprintf(stderr,"double_hash_white_switch_mirror_update cmd MEMDEV_IOCSETDATA fail\n");
return 4;
}
return gl_para.ret;
}
int main(int argc, char *argv[])
{
int fd = 0;
int type;
unsigned int ret;
fd = open("/dev/frdev0",O_RDWR);
if (fd < 0)
{
fprintf(stderr,"Open Dev Mem0 Error!\n");
return -1;
}
if(argc<=1) {
fprintf(stderr,"%s usage: ... \n",argv[0]);
ret=-1;
goto end;
}
// fripadm 2 "192.168.31.98 "
type=atoi(argv[1]);
if(type==FR_IP_IOCTL_TYPE_FIND_BYSTRINGS) {
if(argc==3) {
ret=double_hash_white_find_bystrings(fd,argv[2],strlen(argv[2]));
if(ret==1) {
printf("%s is in the double_hash_white_table\n",argv[2]);
ret=0;
goto end;
}
}
else {
fprintf(stderr,"%s usage: ... \n",argv[0]);
ret=-1;
goto end;
}
}
else if(type==FR_IP_IOCTL_TYPE_MIRROR_INSERT_BYSTRINGS) {
if(argc==3) {
ret=double_hash_white_mirror_insert_bystrings(fd,argv[2],strlen(argv[2]));
goto end;
}
else {
fprintf(stderr,"%s usage: ... \n",argv[0]);
ret=-1;
goto end;
}
}
else if(type==FR_IP_IOCTL_TYPE_MIRROR_DELETE_BYSTRINGS) {
if(argc==3) {
ret=double_hash_white_mirror_delete_bystrings(fd,argv[2],strlen(argv[2]));
goto end;
}
else {
fprintf(stderr,"%s usage: ... \n",argv[0]);
ret=-1;
goto end;
}
}
else if(type==FR_IP_IOCTL_TYPE_MIRROR_DELETE_ALL) {
if(argc==2) {
ret=double_hash_white_mirror_delete_all(fd);
goto end;
}
else {
fprintf(stderr,"%s usage: ... \n",argv[0]);
ret=-1;
goto end;
}
}
else if(type==FR_IP_IOCTL_TYPE_SWITCH_MIRROR_UPDATE) {
if(argc==2) {
ret=double_hash_white_switch_mirror_update(fd);
goto end;
}
else {
fprintf(stderr,"%s usage: ... \n",argv[0]);
ret=-1;
goto end;
}
}
else if(type==FR_IP_IOCTL_TYPE_REBUILD) {
if(argc==4) {
ret=double_hash_white_rebuild(fd,(unsigned int)atoi(argv[2]),(unsigned int)atoi(argv[3]));
goto end;
}
else {
fprintf(stderr,"%s usage: ... \n",argv[0]);
ret=-1;
goto end;
}
}
else if(type==FR_IP_IOCTL_TYPE_DUMP) {
if(argc==2) {
ret=double_hash_white_dump(fd);
goto end;
}
else {
fprintf(stderr,"%s usage: ... \n",argv[0]);
ret=-1;
goto end;
}
}
else {
fprintf(stderr,"%s %d error: unknown type :( \n",argv[0],type);
ret=-1;
goto end;
}
end:
close(fd);
return ret;
}