-
Notifications
You must be signed in to change notification settings - Fork 1
/
ubpf_api.c
1618 lines (1415 loc) · 48.9 KB
/
ubpf_api.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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Created by thomas on 4/11/18.
//
#include <linux/limits.h>
#include <sys/un.h>
#include <include/plugin_arguments.h>
#include "include/ebpf_mod_struct.h"
#include <ubpf_api.h>
#include <stdio.h>
#include <time.h>
#include <arpa/inet.h>
#include "include/tools_ubpf_api.h"
#include "include/plugin_arguments.h"
#include "bpf_plugin.h"
#include <unistd.h>
#include "plugin_extra_configuration.h"
#include "url_parser.h"
#include "log.h"
#include "plugin_socket.h"
#include "static_injection.h"
#include "evt_plugins.h"
#include <netinet/in.h>
#include <float.h>
#include <math.h>
#include <errno.h>
#include <wait.h>
#include <sys/stat.h>
#include <ffi.h>
#include "context_function.h"
uint16_t super_ntohs(context_t *ctx, uint16_t value) {
((void) (ctx));
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
return (((value & 0x00FFu) << 8u) |
((value & 0xFF00u) >> 8u));
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
return value;
#else
# error unsupported endianness
#endif
}
static def_fun_api(super_ntohs, uint16_t, *(uint16_t *) ARGS[0]);
uint32_t super_ntohl(context_t *ctx, uint32_t value) {
((void) (ctx));
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
return (((value & 0x000000FFu) << 24u) |
((value & 0x0000FF00u) << 8u) |
((value & 0x00FF0000u) >> 8u) |
((value & 0xFF000000u) >> 24u));
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
return value;
#else
# error unsupported endianness
#endif
}
static def_fun_api(super_ntohl, uint32_t, *(uint32_t *) ARGS[0]);
uint64_t super_ntohll(context_t *ctx, uint64_t value) {
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
return (
((u_int64_t) (super_ntohl(ctx, (int) ((value << 32u) >> 32u))) << 32u) |
(unsigned int) super_ntohl(ctx, ((int) (value >> 32u)))
);
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
return value;
#else
# error unsupported endianness
#endif
}
static def_fun_api(super_ntohll, uint64_t, *(uint64_t *) ARGS[0])
uint16_t super_htons(context_t *ctx __attribute__((unused)), uint16_t val) {
#if __BYTE_ORDER == __ORDER_LITTLE_ENDIAN__
return (
(((unsigned short) (val) & 0x00FFu)) << 8u |
(((unsigned short) (val) & 0xFF00u) >> 8u)
);
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
return val;
#else
#error unsupported endianness
#endif
}
static def_fun_api(super_htons, uint64_t, *(uint16_t *) ARGS[0])
uint32_t super_htonl(context_t *ctx __attribute__((unused)), uint32_t val) {
#if __BYTE_ORDER == __ORDER_LITTLE_ENDIAN__
return (
((((unsigned long) (val) & 0x000000FFu)) << 24u) |
((((unsigned long) (val) & 0x0000FF00u)) << 8u) |
((((unsigned long) (val) & 0x00FF0000u)) >> 8u) |
((((unsigned long) (val) & 0xFF000000u)) >> 24u)
);
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
return val;
#else
#error unsupported endianness
#endif
}
static def_fun_api(super_htonl, uint64_t, *(uint32_t *) ARGS[0])
uint64_t super_htonll(context_t *ctx, uint64_t val) {
#if __BYTE_ORDER == __ORDER_LITTLE_ENDIAN__
return (
((((uint64_t) super_htonl(ctx, val)) << 32u) + super_htonl(ctx, (val) >> 32u))
);
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
return val;
#else
#error unsupported endianness
#endif
}
static def_fun_api(super_htonll, uint64_t, *(uint64_t *) ARGS[0])
static inline int fill_variadic_arguments(ffi_type **types, void **values, struct vargs *args) {
int i;
for (i = 0; i < args->nb_args; i++) {
switch (args->args[i].type) {
case VT_S8:
types[i] = &ffi_type_sint8;
values[i] = &args->args[i].val.s8;
break;
case VT_U8:
types[i] = &ffi_type_uint8;
values[i] = &args->args[i].val.u8;
break;
case VT_S16:
types[i] = &ffi_type_sint16;
values[i] = &args->args[i].val.s16;
break;
case VT_U16:
types[i] = &ffi_type_uint16;
values[i] = &args->args[i].val.u16;
break;
case VT_S32:
types[i] = &ffi_type_sint32;
values[i] = &args->args[i].val.s32;
break;
case VT_U32:
types[i] = &ffi_type_uint32;
values[i] = &args->args[i].val.u32;
break;
case VT_S64:
types[i] = &ffi_type_sint64;
values[i] = &args->args[i].val.s64;
break;
case VT_U64:
types[i] = &ffi_type_sint64;
values[i] = &args->args[i].val.u64;
break;
case VT_FLOAT:
types[i] = &ffi_type_float;
values[i] = &args->args[i].val.fvalue;
break;
case VT_DOUBLE:
types[i] = &ffi_type_double;
values[i] = &args->args[i].val.dvalue;
break;
case VT_LONGDOUBLE:
types[i] = &ffi_type_longdouble;
values[i] = &args->args[i].val.ldvalue;
break;
case VT_POINTER:
types[i] = &ffi_type_pointer;
values[i] = &args->args[i].val.s8;
break;
case VT_UCHAR:
types[i] = &ffi_type_uchar;
values[i] = &args->args[i].val.uchar;
break;
case VT_SCHAR:
types[i] = &ffi_type_schar;
values[i] = &args->args[i].val.schar;
break;
case VT_USHORT:
types[i] = &ffi_type_ushort;
values[i] = &args->args[i].val.ushort;
break;
case VT_SSHORT:
types[i] = &ffi_type_sshort;
values[i] = &args->args[i].val.sshort;
break;
case VT_UINT:
types[i] = &ffi_type_uint;
values[i] = &args->args[i].val.uint;
break;
case VT_SINT:
types[i] = &ffi_type_sint;
values[i] = &args->args[i].val.sint;
break;
case VT_SLONG:
types[i] = &ffi_type_slong;
values[i] = &args->args[i].val.slong;
break;
case VT_ULONG:
types[i] = &ffi_type_ulong;
values[i] = &args->args[i].val.ulong;
break;
case VT_ULLONG:
types[i] = &ffi_type_uint64;
values[i] = &args->args[i].val.ullong;
break;
case VT_SLLONG:
types[i] = &ffi_type_sint64;
values[i] = &args->args[i].val.sllong;
break;
default:
return -1;
}
}
return 0;
}
static def_fun_api(super_log, int, *(const char **) ARGS[0], *(struct vargs **) ARGS[1])
int super_log(UNUSED context_t *vm_ctx, const char *msg, struct vargs *args) {
int ret_val = 0;
ffi_cif CIF;
ffi_type **types = NULL;
void **values = NULL;
if (*msg < 1 || *msg > 8) {
// bad formatted msg, abort
return 0;
}
types = (ffi_type **) malloc((args->nb_args + 1) * sizeof(ffi_type *));
values = (void **) malloc((args->nb_args + 1) * sizeof(void *));
if (!types || !values) {
goto end;
}
// msg parameter of log_msg
types[0] = &ffi_type_pointer;
values[0] = &msg;
// get variadic arguments contained in args
if (fill_variadic_arguments(types + 1, values + 1, args) != 0) {
goto end;
}
if (ffi_prep_cif_var(&CIF, FFI_DEFAULT_ABI, 1,
args->nb_args + 1, &ffi_type_void, types) == FFI_OK) {
ffi_call(&CIF, FFI_FN(msg_log), NULL, values);
} else {
goto end;
}
/* everything went well so far */
ret_val = 1;
end:
if (types) free(types);
if (values) free(values);
return ret_val;
}
static def_fun_api(ctx_malloc, void *, *(uint64_t *) ARGS[0])
void *ctx_malloc(context_t *vm_ctx, size_t size) {
return mem_alloc(&vm_ctx->p->mem.mgr_heap, size);
}
static def_fun_api(ctx_calloc, void *, *(uint64_t *) ARGS[0], *(uint64_t *) ARGS[1])
void *ctx_calloc(context_t *vm_ctx, uint64_t nmemb, uint64_t size) {
void *ptr;
ptr = ctx_malloc(vm_ctx, nmemb * size);
if (!ptr) return NULL;
memset(ptr, 0, nmemb * size);
return ptr;
}
static def_fun_api(ctx_realloc, void *, *(void **) ARGS[0], *(uint64_t *) ARGS[1])
void *ctx_realloc(context_t *vm_ctx, void *ptr, uint64_t size) {
return mem_realloc(&vm_ctx->p->mem.mgr_heap, ptr, size);
}
static def_fun_api_void(ctx_free, *(void **) ARGS[0])
void ctx_free(UNUSED context_t *vm_ctx, UNUSED void *ptr) {
mem_free(&vm_ctx->p->mem.mgr_heap, ptr);
}
static def_fun_api(ctx_shmnew, void *, *(key_t *) ARGS[0], *(uint64_t *) ARGS[1])
void *ctx_shmnew(context_t *vm_ctx, key_t key, uint64_t size) {
void *addr;
addr = shared_new(&vm_ctx->p->mem.mgr_shared_heap,
&vm_ctx->p->mem.shared_blocks, key, size);
if (addr)
memset(addr, 0, size);
return addr;
}
static def_fun_api(ctx_shmget, void *, *(key_t *) ARGS[0])
void *ctx_shmget(context_t *vm_ctx, key_t key) {
return shared_get(&vm_ctx->p->mem.mgr_shared_heap,
&vm_ctx->p->mem.shared_blocks, key);
}
static def_fun_api_void(ctx_shmrm, *(key_t *) ARGS[0])
void ctx_shmrm(context_t *vm_ctx, key_t key) {
shared_rm(&vm_ctx->p->mem.mgr_shared_heap,
&vm_ctx->p->mem.shared_blocks, key);
}
static def_fun_api(get_time, int, *(struct timespec **) ARGS[0])
int get_time(UNUSED context_t *vm_ctx, struct timespec *spec) {
memset(spec, 0, sizeof(*spec));
if (clock_gettime(CLOCK_MONOTONIC, spec) != 0) {
perror("Clock gettime");
return -1;
}
return 0;
}
static def_fun_api(get_realtime, int, *(struct timespec **) ARGS[0]);
int get_realtime(context_t *vm_ctx UNUSED, struct timespec *spec) {
if (!spec) return -1;
if (clock_gettime(CLOCK_REALTIME, spec) != 0) {
perror("Clock gettime");
return -1;
}
return 0;
}
static def_fun_api(ebpf_print_intern, int, *(const char **) ARGS[0], *(struct vargs **) ARGS[1])
int ebpf_print_intern(UNUSED context_t *vm_ctx, const char *format, struct vargs *args) {
ffi_cif CIF;
ffi_type **types = NULL;
void **values = NULL;
int rvalue = 0;
types = malloc((args->nb_args + 1) * sizeof(ffi_type *));
values = malloc((args->nb_args + 1) * sizeof(void *));
if (!values || !types) goto end;
/* printf 1st argument */
types[0] = &ffi_type_pointer;
values[0] = &format;
if (fill_variadic_arguments(types + 1, values + 1, args) != 0) {
goto end;
}
if (ffi_prep_cif_var(&CIF, FFI_DEFAULT_ABI, 1,
args->nb_args + 1, &ffi_type_sint, types) == FFI_OK) {
ffi_call(&CIF, FFI_FN(printf), &rvalue, values);
} else {
goto end;
}
end:
if (types) free(types);
if (values) free(values);
return rvalue;
}
static def_fun_api(next, int)
int next(context_t *vm_ctx) {
return run_replace_next_replace_function(vm_ctx);
}
/* The following piece of code is taken and adapted from bird routing project */
/* ~~~ BEGIN BIRD CODE ~~~*/
#define ZEROPAD 1u /* pad with zero */
#define SIGN 2u /* unsigned/signed long */
#define PLUS 4u /* show plus */
#define SPACE 8u /* space if plus */
#define LEFT 16u /* left justified */
#define SPECIAL 32u /* 0x */
#define LARGE 64u /* use 'ABCDEF' instead of 'abcdef' */
#define S_ * (uint64_t) 1000000
#define MS_ * (uint64_t) 1000
#define US_ * (uint64_t) 1
#define TO_S /1000000
#define TO_MS /1000
#define TO_US /1
#define S S_
#define MS MS_
#define US US_
#define NS /1000
#define is_digit(c) ((c) >= '0' && (c) <= '9')
static inline int skip_atoi(const char **s) {
int i = 0;
while (is_digit(**s))
i = i * 10 + *((*s)++) - '0';
return i;
}
static inline char *
number(char *str, uint64_t num, uint base, int size, int precision, int type, int remains) {
char c, sign, tmp[66];
const char *digits = "0123456789abcdefghijklmnopqrstuvwxyz";
int i;
if (size >= 0 && (remains -= size) < 0)
return NULL;
if (type & LARGE)
digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
if (type & LEFT)
type &= ~ZEROPAD;
if (base < 2 || base > 36)
return 0;
c = (type & ZEROPAD) ? '0' : ' ';
sign = 0;
if (type & SIGN) {
if (num > (uint64_t) INT64_MAX) {
sign = '-';
num = -num;
size--;
} else if (type & PLUS) {
sign = '+';
size--;
} else if (type & SPACE) {
sign = ' ';
size--;
}
}
if (type & SPECIAL) {
if (base == 16)
size -= 2;
else if (base == 8)
size--;
}
i = 0;
if (num == 0)
tmp[i++] = '0';
else
while (num != 0) {
uint res = num % base;
num = num / base;
tmp[i++] = digits[res];
}
if (i > precision)
precision = i;
size -= precision;
if (size < 0 && -size > remains)
return NULL;
if (!(type & (ZEROPAD + LEFT)))
while (size-- > 0)
*str++ = ' ';
if (sign)
*str++ = sign;
if (type & SPECIAL) {
if (base == 8)
*str++ = '0';
else if (base == 16) {
*str++ = '0';
*str++ = digits[33];
}
}
if (!(type & LEFT))
while (size-- > 0)
*str++ = c;
while (i < precision--)
*str++ = '0';
while (i-- > 0)
*str++ = tmp[i];
while (size-- > 0)
*str++ = ' ';
return str;
}
static def_fun_api(ebpf_bvsnprintf, int, *(char **) ARGS[0], *(int *) ARGS[1], *(const char **) ARGS[2],
*(uintptr_t **) ARGS[3])
int ebpf_bvsnprintf(UNUSED context_t *ctx, char *buf, int size, const char *fmt, uintptr_t *args) {
int curr_args;
int len, i;
uint64_t num;
uint base;
int64_t t;
int64_t t1, t2;
char *str, *start;
const char *s;
int flags; /* flags to number() */
int field_width; /* width of output field */
int precision; /* min. # of digits for integers; max
number of chars for from string */
int qualifier; /* 'h' or 'l' for integer fields */
// nb_args = args[0];
curr_args = 1; // 0 is the number of args
for (start = str = buf; *fmt; ++fmt, size -= (str - start), start = str) {
if (*fmt != '%') {
if (!size)
return -1;
*str++ = *fmt;
continue;
}
/* process flags */
flags = 0;
repeat:
++fmt; /* this also skips first '%' */
switch (*fmt) {
case '-':
flags |= LEFT;
goto repeat;
case '+':
flags |= PLUS;
goto repeat;
case ' ':
flags |= SPACE;
goto repeat;
case '#':
flags |= SPECIAL;
goto repeat;
case '0':
flags |= ZEROPAD;
goto repeat;
}
/* get field width */
field_width = -1;
if (is_digit(*fmt))
field_width = skip_atoi(&fmt);
else if (*fmt == '*') {
++fmt;
/* it's the next argument */
field_width = (int) args[curr_args++];//va_arg(args, int);
if (field_width < 0) {
field_width = -field_width;
flags |= LEFT;
}
}
/* get the precision */
precision = -1;
if (*fmt == '.') {
++fmt;
if (is_digit(*fmt))
precision = skip_atoi(&fmt);
else if (*fmt == '*') {
++fmt;
/* it's the next argument */
precision = (int) args[curr_args++];
}
if (precision < 0)
precision = 0;
}
/* get the conversion qualifier */
qualifier = -1;
if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L') {
qualifier = *fmt;
++fmt;
}
/* default base */
base = 10;
if (field_width > size)
return -1;
switch (*fmt) {
case 'c':
if (!(flags & LEFT))
while (--field_width > 0)
*str++ = ' ';
*str++ = (uint8_t) args[curr_args++];
while (--field_width > 0)
*str++ = ' ';
continue;
case 'm':
if (flags & SPECIAL) {
if (!errno)
continue;
if (size < 2)
return -1;
*str++ = ':';
*str++ = ' ';
start += 2;
size -= 2;
}
s = strerror(errno);
goto str;
case 's':
s = (char *) args[curr_args++];
if (!s)
s = "<NULL>";
str:
len = strnlen(s, size); // prevent buffer overflow when wrong announced format
if (precision >= 0 && len > precision)
len = precision;
if (len > size)
return -1;
if (!(flags & LEFT))
while (len < field_width--)
*str++ = ' ';
for (i = 0; i < len; ++i)
*str++ = *s++;
while (len < field_width--)
*str++ = ' ';
continue;
/*case 'V': { // put this case in standby ! (not really a good feature I guess)
const char *vfmt = (const char *) args[curr_args++];
va_list *vargs = va_arg(args, va_list *);
int res = bvsnprintf(str, size, vfmt, *vargs);
if (res < 0)
return -1;
str += res;
size -= res;
continue;
}*/
case 'p':
if (field_width == -1) {
field_width = 2 * sizeof(void *);
flags |= ZEROPAD;
}
str = number(str, args[curr_args++], 16,
field_width, precision, flags, size);
if (!str)
return -1;
continue;
case 'n':
if (qualifier == 'l') {
int64_t *ip = (int64_t *) args[curr_args++];
*ip = (str - buf);
} else {
int *ip = (int *) args[curr_args++];
*ip = (str - buf);
}
continue;
case 't':
t = (uint64_t) args[curr_args++];
t1 = t TO_S;
t2 = t - t1 S;
if (precision < 0)
precision = 3;
if (precision > 6)
precision = 6;
/* Compute field_width for second part */
if ((precision > 0) && (field_width > 0))
field_width -= (1 + precision);
if (field_width < 0)
field_width = 0;
/* Print seconds */
flags |= SIGN;
str = number(str, (uint64_t) t1, 10, field_width, 0, flags, size);
if (!str)
return -1;
if (precision > 0) {
size -= (str - start);
start = str;
if ((1 + precision) > size)
return -1;
/* Convert microseconds to requested precision */
for (i = precision; i < 6; i++)
t2 /= 10;
/* Print sub-seconds */
*str++ = '.';
str = number(str, (uint64_t) t2, 10, precision, 0, ZEROPAD, size - 1);
if (!str)
return -1;
}
goto done;
/* integer number formats - set up the flags and "break" */
case 'o':
base = 8;
break;
case 'X':
flags |= LARGE;
/* fallthrough */
case 'x':
base = 16;
break;
case 'd':
case 'i':
flags |= SIGN;
case 'u':
break;
default:
if (size < 2)
return -1;
if (*fmt != '%')
*str++ = '%';
if (*fmt)
*str++ = *fmt;
else
--fmt;
continue;
}
if (flags & SIGN) {
/* Conversions valid per ISO C99 6.3.1.3 (2) */
if (qualifier == 'l')
num = (uint64_t) args[curr_args++];
else if (qualifier == 'h')
num = (uint64_t) (
short) args[curr_args++];
else
num = (uint64_t) args[curr_args++];
} else {
if (qualifier == 'l')
num = (uint64_t) args[curr_args++];
else if (qualifier == 'h')
num = (unsigned short) args[curr_args++];
else
num = (uint) args[curr_args++];
}
str = number(str, num, base, field_width, precision, flags, size);
if (!str)
return -1;
done:;
}
if (!size)
return -1;
*str = '\0';
return str - buf;
}
/* ~~~ END BIRD CODE ~~~*/
/// memcpy IS TAKEN FROM
/// http://www.ethernut.de/api/memcpy_8c_source.html
/*
* sizeof(word) MUST BE A POWER OF TWO
* SO THAT wmask BELOW IS ALL ONES
*/
typedef int word; /* "word" used for optimal copy speed */
#define wsize sizeof(word)
#define wmask (wsize - 1)
static def_fun_api(ebpf_memcpy, void *, *(void **) ARGS[0], *(const void **) ARGS[1], *(uint64_t *) ARGS[2])
/*
* Copy a block of memory, handling overlap.
* This is the routine that actually implements
* (the portable versions of) bcopy, memcpy, and memmove.
*/
void *ebpf_memcpy(UNUSED context_t *vm_ctx, void *dst0, const void *src0, uint64_t length) {
char *dst = dst0;
const char *src = src0;
size_t t;
if (length == 0 || dst == src) /* nothing to do */
goto done;
/*
* Macros: loop-t-times; and loop-t-times, t>0
*/
#define TLOOP(s) if (t) TLOOP1(s)
#define TLOOP1(s) do { s; } while (--t)
if ((unsigned long) dst < (unsigned long) src) {
/*
* Copy forward.
*/
t = (uintptr_t) src; /* only need low bits */
if ((t | (uintptr_t) dst) & wmask) {
/*
* Try to align operands. This cannot be done
* unless the low bits match.
*/
if ((t ^ (uintptr_t) dst) & wmask || length < wsize)
t = length;
else
t = wsize - (t & wmask);
length -= t;
TLOOP1(*dst++ = *src++);
}
/*
* Copy whole words, then mop up any trailing bytes.
*/
t = length / wsize;
TLOOP(*(word *) dst = *(const word *) src;
src += wsize;
dst += wsize);
t = length & wmask;
TLOOP(*dst++ = *src++);
} else {
/*
* Copy backwards. Otherwise essentially the same.
* Alignment works as before, except that it takes
* (t&wmask) bytes to align, not wsize-(t&wmask).
*/
src += length;
dst += length;
t = (uintptr_t) src;
if ((t | (uintptr_t) dst) & wmask) {
if ((t ^ (uintptr_t) dst) & wmask || length <= wsize)
t = length;
else
t &= wmask;
length -= t;
TLOOP1(*--dst = *--src);
}
t = length / wsize;
TLOOP(src -= wsize;
dst -= wsize;
*(word *) dst = *(const word *) src);
t = length & wmask;
TLOOP(*--dst = *--src);
}
done:
return (dst0);
}
static def_fun_api(get_arg, void *, *(int *) ARGS[0])
void *get_arg(context_t *vm_ctx, int type) {
int i;
uint8_t *ret_arg;
// fprintf(stderr, "Ptr ctx at %s call --> %p\n", __FUNCTION__, vm_ctx);
args_t *check_args = vm_ctx->args;
if (!check_args) {
return NULL;
}
for (i = 0; i < check_args->nargs; i++) {
if (check_args->args[i].type == type) {
ret_arg = mem_alloc(&vm_ctx->p->mem.mgr_heap, check_args->args[i].len);
if (!ret_arg) return NULL;
memcpy(ret_arg, check_args->args[i].arg, check_args->args[i].len);
return ret_arg;
}
}
return NULL;
}
static def_fun_api_void(membound_fail, *(uint64_t *) ARGS[0], *(uint64_t *) ARGS[1], *(uint64_t *) ARGS[2])
void membound_fail(context_t *ctx __attribute__((unused)), uint64_t val, uint64_t mem_ptr, uint64_t stack_ptr) {
fprintf(stderr, "Out of bound access with val 0x%lx, start of mem is 0x%lx, top of stack is 0x%lx\n", val, mem_ptr,
stack_ptr);
}
static def_fun_api(ebpf_sqrt, uint64_t, *(uint64_t *) ARGS[0], *(unsigned int *) ARGS[2])
uint64_t ebpf_sqrt(context_t *ctx __attribute__((unused)), uint64_t a, unsigned int precision) {
double s_half;
double s;
uint64_t res;
if (a >= DBL_MAX) return 0;
s = a;
s_half = sqrt(s);
res = s_half * pow(10, precision);
return res;
}
static def_fun_api(ebpf_memcmp, int, *(const void **) ARGS[0], *(const void **) ARGS[1], *(uint64_t *) ARGS[2])
int ebpf_memcmp(context_t *ctx UNUSED, const void *s1, const void *s2, uint64_t n) {
return memcmp(s1, s2, n);
}
static def_fun_api(get_extra_info_value, int, *(struct global_info **) ARGS[0], *(void **) ARGS[1],
*(uint64_t *) ARGS[2])
int get_extra_info_value(context_t *ctx UNUSED, struct global_info *info, void *buf, uint64_t len_buf) {
return extra_info_copy_data(info, buf, len_buf);
}
static def_fun_api(get_extra_info_lst_idx, int, *(struct global_info **) ARGS[0], *(int *) ARGS[1],
*(struct global_info **) ARGS[2])
int get_extra_info_lst_idx(context_t *ctx UNUSED, struct global_info *info, int arr_idx, struct global_info *value) {
return get_info_lst_idx(info, arr_idx, value);
}
static def_fun_api(get_extra_info_dict, int, *(struct global_info **) ARGS[0], *(const char **) ARGS[1],
*(struct global_info **) ARGS[2])
int get_extra_info_dict(context_t *ctx UNUSED, struct global_info *info, const char *key, struct global_info *value) {
if (!key) return -1;
return get_info_dict(info, key, value);
}
static def_fun_api(get_extra_info, int, *(const char **) ARGS[0], *(struct global_info **) ARGS[1])
int get_extra_info(context_t *ctx UNUSED, const char *key, struct global_info *info) {
return get_global_info(key, info);
}
static def_fun_api(ebpf_inet_ntop, int, *(uint8_t **) ARGS[0], *(int *) ARGS[1], *(char **) ARGS[2],
*(uint64_t *) ARGS[3])
int ebpf_inet_ntop(context_t *ctx UNUSED, uint8_t *ipaddr, int type, char *buf, uint64_t len) {
struct in_addr ipv4;
struct in6_addr ipv6;
void *ip;
switch (type) {
case AF_INET:
ipv4.s_addr = *(uint32_t *) ipaddr;
ip = &ipv4;
break;
case AF_INET6:
memcpy(&ipv6, ipaddr, sizeof(ipv6));
ip = &ipv6;
break;
default:
return -1;
}
if (!inet_ntop(type, ip, buf, len)) return -1;
return 0;
}
static def_fun_api(ebpf_inet_pton, int, *(int *) ARGS[0], *(const char **) ARGS[1], *(void **) ARGS[2],
*(uint64_t *) ARGS[3])
int ebpf_inet_pton(UNUSED context_t *ctx, int af, const char *src, void *dst, uint64_t buf_len) {
int s;
size_t min_len;
unsigned char buf[sizeof(struct in6_addr)];
switch (af) {
case AF_INET:
min_len = sizeof(struct in_addr);
break;
case AF_INET6:
min_len = sizeof(struct in6_addr);
break;
default:
return -1;
}
if (buf_len < min_len) return -1;
s = inet_pton(af, src, buf);
if (s <= 0) {
return -1;
}
memcpy(dst, buf, min_len);
return 0;
}