2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2012 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,
5 * Swiss federal institute of technology (ETH Zurich)
6 * Subject to the GPL, version 2.
9 /* yaac-func-prefix: yy */
22 #include "trafgen_parser.tab.h"
23 #include "trafgen_conf.h"
29 #define YYERROR_VERBOSE 0
31 #define YYENABLE_NLS 1
32 #define YYLTYPE_IS_TRIVIAL 1
36 extern
int yylex(void);
37 extern
void yy_scan_string
(char *);
38 extern
void yylex_destroy
();
39 extern
void yyerror(const char *);
43 extern
struct packet
*packets
;
46 #define packet_last (plen - 1)
48 #define payload_last (packets[packet_last].len - 1)
50 extern
struct packet_dyn
*packet_dyn
;
53 #define packetd_last (dlen - 1)
55 #define packetdc_last (packet_dyn[packetd_last].clen - 1)
56 #define packetdr_last (packet_dyn[packetd_last].rlen - 1)
57 #define packetds_last (packet_dyn[packetd_last].slen - 1)
59 static int our_cpu
, min_cpu
= -1, max_cpu
= -1;
61 static inline
int test_ignore
(void)
63 if
(min_cpu
< 0 && max_cpu
< 0)
65 else if
(max_cpu
>= our_cpu
&& min_cpu
<= our_cpu
)
71 static inline
void __init_new_packet_slot
(struct packet
*slot
)
77 static inline
void __init_new_counter_slot
(struct packet_dyn
*slot
)
83 static inline
void __init_new_randomizer_slot
(struct packet_dyn
*slot
)
89 static inline
void __init_new_csum_slot
(struct packet_dyn
*slot
)
95 static inline
void __setup_new_counter
(struct counter
*c
, uint8_t start
,
96 uint8_t stop
, uint8_t stepping
,
102 c
->val
= (type
== TYPE_INC
) ? start
: stop
;
103 c
->off
= payload_last
;
107 static inline
void __setup_new_randomizer
(struct randomizer
*r
)
109 r
->off
= payload_last
;
112 static inline
void __setup_new_csum16
(struct csum16
*s
, off_t from
, off_t to
,
115 s
->off
= payload_last
- 1;
121 static void realloc_packet
(void)
127 packets
= xrealloc
(packets
, plen
* sizeof
(*packets
));
129 __init_new_packet_slot
(&packets
[packet_last
]);
132 packet_dyn
= xrealloc
(packet_dyn
, dlen
* sizeof
(*packet_dyn
));
134 __init_new_counter_slot
(&packet_dyn
[packetd_last
]);
135 __init_new_randomizer_slot
(&packet_dyn
[packetd_last
]);
136 __init_new_csum_slot
(&packet_dyn
[packetd_last
]);
139 static void set_byte
(uint8_t val
)
141 struct packet
*pkt
= &packets
[packet_last
];
147 pkt
->payload
= xrealloc
(pkt
->payload
, pkt
->len
);
148 pkt
->payload
[payload_last
] = val
;
151 static void set_multi_byte
(uint8_t *s
, size_t len
)
155 for
(i
= 0; i
< len
; ++i
)
159 static void set_fill
(uint8_t val
, size_t len
)
162 struct packet
*pkt
= &packets
[packet_last
];
168 pkt
->payload
= xrealloc
(pkt
->payload
, pkt
->len
);
169 for
(i
= 0; i
< len
; ++i
)
170 pkt
->payload
[payload_last
- i
] = val
;
173 static void __set_csum16_dynamic
(size_t from
, size_t to
, enum csum which
)
175 struct packet
*pkt
= &packets
[packet_last
];
176 struct packet_dyn
*pktd
= &packet_dyn
[packetd_last
];
179 pkt
->payload
= xrealloc
(pkt
->payload
, pkt
->len
);
182 pktd
->csum
= xrealloc
(pktd
->csum
, pktd
->slen
* sizeof
(struct csum16
));
184 __setup_new_csum16
(&pktd
->csum
[packetds_last
], from
, to
, which
);
187 static void __set_csum16_static
(size_t from
, size_t to
, enum csum which __maybe_unused
)
189 struct packet
*pkt
= &packets
[packet_last
];
193 sum
= htons
(calc_csum
(pkt
->payload
+ from
, to
- from
));
194 psum
= (uint8_t *) &sum
;
200 static inline
bool is_dynamic_csum
(enum csum which
)
213 static void set_csum16
(size_t from
, size_t to
, enum csum which
)
215 struct packet
*pkt
= &packets
[packet_last
];
216 struct packet_dyn
*pktd
= &packet_dyn
[packetd_last
];
228 bug_on
(!(from
< to
));
230 if
(packet_dyn_has_elems
(pktd
) || to
>= pkt
->len || is_dynamic_csum
(which
))
231 __set_csum16_dynamic
(from
, to
, which
);
233 __set_csum16_static
(from
, to
, which
);
236 static void set_rnd
(size_t len
)
239 struct packet
*pkt
= &packets
[packet_last
];
245 pkt
->payload
= xrealloc
(pkt
->payload
, pkt
->len
);
246 for
(i
= 0; i
< len
; ++i
)
247 pkt
->payload
[payload_last
- i
] = (uint8_t) rand
();
250 static void set_sequential_inc
(uint8_t start
, size_t len
, uint8_t stepping
)
253 struct packet
*pkt
= &packets
[packet_last
];
259 pkt
->payload
= xrealloc
(pkt
->payload
, pkt
->len
);
260 for
(i
= 0; i
< len
; ++i
) {
261 off_t off
= len
- 1 - i
;
263 pkt
->payload
[payload_last
- off
] = start
;
268 static void set_sequential_dec
(uint8_t start
, size_t len
, uint8_t stepping
)
271 struct packet
*pkt
= &packets
[packet_last
];
277 pkt
->payload
= xrealloc
(pkt
->payload
, pkt
->len
);
278 for
(i
= 0; i
< len
; ++i
) {
279 int off
= len
- 1 - i
;
281 pkt
->payload
[payload_last
- off
] = start
;
286 static void set_dynamic_rnd
(void)
288 struct packet
*pkt
= &packets
[packet_last
];
289 struct packet_dyn
*pktd
= &packet_dyn
[packetd_last
];
295 pkt
->payload
= xrealloc
(pkt
->payload
, pkt
->len
);
298 pktd
->rnd
= xrealloc
(pktd
->rnd
, pktd
->rlen
* sizeof
(struct randomizer
));
300 __setup_new_randomizer
(&pktd
->rnd
[packetdr_last
]);
303 static void set_dynamic_incdec
(uint8_t start
, uint8_t stop
, uint8_t stepping
,
306 struct packet
*pkt
= &packets
[packet_last
];
307 struct packet_dyn
*pktd
= &packet_dyn
[packetd_last
];
313 pkt
->payload
= xrealloc
(pkt
->payload
, pkt
->len
);
316 pktd
->cnt
= xrealloc
(pktd
->cnt
, pktd
->clen
* sizeof
(struct counter
));
318 __setup_new_counter
(&pktd
->cnt
[packetdc_last
], start
, stop
, stepping
, type
);
324 long long int number
;
328 %token K_COMMENT K_FILL K_RND K_SEQINC K_SEQDEC K_DRND K_DINC K_DDEC K_WHITE
329 %token K_CPU K_CSUMIP K_CSUMUDP K_CSUMTCP K_CSUMUDP6 K_CSUMTCP6 K_CONST8 K_CONST16 K_CONST32 K_CONST64
331 %token
',' '{' '}' '(' ')' '[' ']' ':' '-' '+' '*' '/' '%' '&' '|' '<' '>' '^'
335 %type
<number
> number expression
338 %left
'-' '+' '*' '/' '%' '&' '|' '<' '>' '^'
345 | packets inline_comment
{ }
346 | packets K_WHITE
{ }
366 | delimiter_nowhite
{ }
370 : '{' noenforce_white payload noenforce_white
'}' {
371 min_cpu
= max_cpu
= -1;
374 | K_CPU
'(' number cpu_delim number
')' ':' noenforce_white
'{' noenforce_white payload noenforce_white
'}' {
378 if
(min_cpu
> max_cpu
) {
387 | K_CPU
'(' number
')' ':' noenforce_white
'{' noenforce_white payload noenforce_white
'}' {
388 min_cpu
= max_cpu
= $3;
395 | payload elem_delimiter
{ }
399 : delimiter_nowhite
{ }
408 : number
{ set_byte
((uint8_t) $1); }
409 |
string { set_multi_byte
((uint8_t *) $1 + 1, strlen
($1) - 2); }
425 | expression
'+' expression
427 | expression
'-' expression
429 | expression
'*' expression
431 | expression
'/' expression
433 | expression
'%' expression
435 | expression
'&' expression
437 | expression
'|' expression
439 | expression
'^' expression
441 | expression
'<' '<' expression
443 | expression
'>' '>' expression
452 : K_FILL
'(' number delimiter number
')'
453 { set_fill
($3, $5); }
457 : K_CONST8
'(' expression
')'
458 { set_byte
((uint8_t) $3); }
459 | K_CONST16
'(' expression
')' {
460 uint16_t __c
= cpu_to_be16
((uint16_t) $3);
462 set_multi_byte
((uint8_t *) &__c
, sizeof
(__c
));
464 | K_CONST32
'(' expression
')' {
465 uint32_t __c
= cpu_to_be32
((uint32_t) $3);
467 set_multi_byte
((uint8_t *) &__c
, sizeof
(__c
));
469 | K_CONST64
'(' expression
')' {
470 uint64_t __c
= cpu_to_be64
((uint64_t) $3);
472 set_multi_byte
((uint8_t *) &__c
, sizeof
(__c
));
477 : K_RND
'(' number
')'
482 : K_CSUMIP
'(' number delimiter number
')'
483 { set_csum16
($3, $5, CSUM_IP
); }
484 | K_CSUMTCP
'(' number delimiter number
')'
485 { set_csum16
($3, $5, CSUM_TCP
); }
486 | K_CSUMUDP
'(' number delimiter number
')'
487 { set_csum16
($3, $5, CSUM_UDP
); }
488 | K_CSUMTCP6
'(' number delimiter number
')'
489 { set_csum16
($3, $5, CSUM_TCP6
); }
490 | K_CSUMUDP6
'(' number delimiter number
')'
491 { set_csum16
($3, $5, CSUM_UDP6
); }
495 : K_SEQINC
'(' number delimiter number
')'
496 { set_sequential_inc
($3, $5, 1); }
497 | K_SEQINC
'(' number delimiter number delimiter number
')'
498 { set_sequential_inc
($3, $5, $7); }
502 : K_SEQDEC
'(' number delimiter number
')'
503 { set_sequential_dec
($3, $5, 1); }
504 | K_SEQDEC
'(' number delimiter number delimiter number
')'
505 { set_sequential_dec
($3, $5, $7); }
510 { set_dynamic_rnd
(); }
511 | K_DRND
'(' number
')'
514 for
(i
= 0; i
< max
; ++i
)
520 : K_DINC
'(' number delimiter number
')'
521 { set_dynamic_incdec
($3, $5, 1, TYPE_INC
); }
522 | K_DINC
'(' number delimiter number delimiter number
')'
523 { set_dynamic_incdec
($3, $5, $7, TYPE_INC
); }
527 : K_DDEC
'(' number delimiter number
')'
528 { set_dynamic_incdec
($3, $5, 1, TYPE_DEC
); }
529 | K_DDEC
'(' number delimiter number delimiter number
')'
530 { set_dynamic_incdec
($3, $5, $7, TYPE_DEC
); }
535 static void finalize_packet
(void)
537 /* XXX hack ... we allocated one packet pointer too much */
542 static void dump_conf
(void)
546 for
(i
= 0; i
< plen
; ++i
) {
547 printf
("[%zu] pkt\n", i
);
548 printf
(" len %zu cnts %zu rnds %zu\n",
554 for
(j
= 0; j
< packets
[i
].len
; ++j
)
555 printf
("%02x ", packets
[i
].payload
[j
]);
558 for
(j
= 0; j
< packet_dyn
[i
].clen
; ++j
)
559 printf
(" cnt%zu [%u,%u], inc %u, off %jd type %s\n", j
,
560 packet_dyn
[i
].cnt
[j
].min
,
561 packet_dyn
[i
].cnt
[j
].max
,
562 packet_dyn
[i
].cnt
[j
].inc
,
563 (intmax_t)packet_dyn
[i
].cnt
[j
].off
,
564 packet_dyn
[i
].cnt
[j
].type
== TYPE_INC ?
567 for
(j
= 0; j
< packet_dyn
[i
].rlen
; ++j
)
568 printf
(" rnd%zu off %jd\n", j
,
569 (intmax_t)packet_dyn
[i
].rnd
[j
].off
);
573 void cleanup_packets
(void)
577 for
(i
= 0; i
< plen
; ++i
) {
578 if
(packets
[i
].len
> 0)
579 xfree
(packets
[i
].payload
);
584 for
(i
= 0; i
< dlen
; ++i
) {
585 free
(packet_dyn
[i
].cnt
);
586 free
(packet_dyn
[i
].rnd
);
592 void compile_packets
(char *file
, bool verbose
, unsigned int cpu
, bool invoke_cpp
)
597 memset
(tmp_file
, 0, sizeof
(tmp_file
));
601 char cmd
[256], *dir
, *base
, *a
, *b
;
603 dir
= dirname
((a
= xstrdup
(file
)));
604 base
= basename
((b
= xstrdup
(file
)));
606 slprintf
(tmp_file
, sizeof
(tmp_file
), "%s/.tmp-%u-%s", dir
, rand
(), base
);
607 slprintf
(cmd
, sizeof
(cmd
), "cpp -I" ETCDIRE_STRING
" %s > %s",
609 if
(system
(cmd
) != 0) {
610 fprintf
(stderr
, "Failed to invoke C preprocessor!\n");
619 if
(!strncmp
("-", file
, strlen
("-")))
622 yyin
= fopen
(file
, "r");
624 fprintf
(stderr
, "Cannot open %s: %s!\n", file
, strerror
(errno
));
633 if
(our_cpu
== 0 && verbose
)
647 void compile_packets_str
(char *str
, bool verbose
, unsigned int cpu
)
659 if
(our_cpu
== 0 && verbose
)
670 void yyerror(const char *err
)
672 fprintf
(stderr
, "Syntax error at line %d, char '%s': %s\n", yylineno
, yytext
, err
);