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 yyerror(const char *);
41 extern
struct packet
*packets
;
44 #define packet_last (plen - 1)
46 #define payload_last (packets[packet_last].len - 1)
48 extern
struct packet_dyn
*packet_dyn
;
51 #define packetd_last (dlen - 1)
53 #define packetdc_last (packet_dyn[packetd_last].clen - 1)
54 #define packetdr_last (packet_dyn[packetd_last].rlen - 1)
55 #define packetds_last (packet_dyn[packetd_last].slen - 1)
57 static int our_cpu
, min_cpu
= -1, max_cpu
= -1;
59 static inline
int test_ignore
(void)
61 if
(min_cpu
< 0 && max_cpu
< 0)
63 else if
(max_cpu
>= our_cpu
&& min_cpu
<= our_cpu
)
69 static inline
int has_dynamic_elems
(struct packet_dyn
*p
)
71 return
(p
->rlen
+ p
->slen
+ p
->clen
);
74 static inline
void __init_new_packet_slot
(struct packet
*slot
)
80 static inline
void __init_new_counter_slot
(struct packet_dyn
*slot
)
86 static inline
void __init_new_randomizer_slot
(struct packet_dyn
*slot
)
92 static inline
void __init_new_csum_slot
(struct packet_dyn
*slot
)
98 static inline
void __setup_new_counter
(struct counter
*c
, uint8_t start
,
99 uint8_t stop
, uint8_t stepping
,
105 c
->val
= (type
== TYPE_INC
) ? start
: stop
;
106 c
->off
= payload_last
;
110 static inline
void __setup_new_randomizer
(struct randomizer
*r
)
112 r
->off
= payload_last
;
115 static inline
void __setup_new_csum16
(struct csum16
*s
, off_t from
, off_t to
,
118 s
->off
= payload_last
- 1;
124 static void realloc_packet
(void)
130 packets
= xrealloc
(packets
, 1, plen
* sizeof
(*packets
));
132 __init_new_packet_slot
(&packets
[packet_last
]);
135 packet_dyn
= xrealloc
(packet_dyn
, 1, dlen
* sizeof
(*packet_dyn
));
137 __init_new_counter_slot
(&packet_dyn
[packetd_last
]);
138 __init_new_randomizer_slot
(&packet_dyn
[packetd_last
]);
139 __init_new_csum_slot
(&packet_dyn
[packetd_last
]);
142 static void set_byte
(uint8_t val
)
144 struct packet
*pkt
= &packets
[packet_last
];
150 pkt
->payload
= xrealloc
(pkt
->payload
, 1, pkt
->len
);
151 pkt
->payload
[payload_last
] = val
;
154 static void set_multi_byte
(uint8_t *s
, size_t len
)
158 for
(i
= 0; i
< len
; ++i
)
162 static void set_fill
(uint8_t val
, size_t len
)
165 struct packet
*pkt
= &packets
[packet_last
];
171 pkt
->payload
= xrealloc
(pkt
->payload
, 1, pkt
->len
);
172 for
(i
= 0; i
< len
; ++i
)
173 pkt
->payload
[payload_last
- i
] = val
;
176 static void __set_csum16_dynamic
(size_t from
, size_t to
, enum csum which
)
178 struct packet
*pkt
= &packets
[packet_last
];
179 struct packet_dyn
*pktd
= &packet_dyn
[packetd_last
];
182 pkt
->payload
= xrealloc
(pkt
->payload
, 1, pkt
->len
);
185 pktd
->csum
= xrealloc
(pktd
->csum
, 1, pktd
->slen
* sizeof
(struct csum16
));
187 __setup_new_csum16
(&pktd
->csum
[packetds_last
], from
, to
, which
);
190 static void __set_csum16_static
(size_t from
, size_t to
, enum csum which
)
192 struct packet
*pkt
= &packets
[packet_last
];
196 sum
= htons
(calc_csum
(pkt
->payload
+ from
, to
- from
, 0));
197 psum
= (uint8_t *) &sum
;
203 static void set_csum16
(size_t from
, size_t to
, enum csum which
)
205 int make_it_dynamic
= 0;
206 struct packet
*pkt
= &packets
[packet_last
];
207 struct packet_dyn
*pktd
= &packet_dyn
[packetd_last
];
219 bug_on
(!(from
< to
));
221 if
(to
>= pkt
->len || which
== CSUM_TCP || which
== CSUM_UDP
)
224 if
(has_dynamic_elems
(pktd
) || make_it_dynamic
)
225 __set_csum16_dynamic
(from
, to
, which
);
227 __set_csum16_static
(from
, to
, which
);
230 static void set_rnd
(size_t len
)
233 struct packet
*pkt
= &packets
[packet_last
];
239 pkt
->payload
= xrealloc
(pkt
->payload
, 1, pkt
->len
);
240 for
(i
= 0; i
< len
; ++i
)
241 pkt
->payload
[payload_last
- i
] = (uint8_t) rand
();
244 static void set_sequential_inc
(uint8_t start
, size_t len
, uint8_t stepping
)
247 struct packet
*pkt
= &packets
[packet_last
];
253 pkt
->payload
= xrealloc
(pkt
->payload
, 1, pkt
->len
);
254 for
(i
= 0; i
< len
; ++i
) {
255 off_t off
= len
- 1 - i
;
257 pkt
->payload
[payload_last
- off
] = start
;
262 static void set_sequential_dec
(uint8_t start
, size_t len
, uint8_t stepping
)
265 struct packet
*pkt
= &packets
[packet_last
];
271 pkt
->payload
= xrealloc
(pkt
->payload
, 1, pkt
->len
);
272 for
(i
= 0; i
< len
; ++i
) {
273 int off
= len
- 1 - i
;
275 pkt
->payload
[payload_last
- off
] = start
;
280 static void set_dynamic_rnd
(void)
282 struct packet
*pkt
= &packets
[packet_last
];
283 struct packet_dyn
*pktd
= &packet_dyn
[packetd_last
];
289 pkt
->payload
= xrealloc
(pkt
->payload
, 1, pkt
->len
);
292 pktd
->rnd
= xrealloc
(pktd
->rnd
, 1, pktd
->rlen
* sizeof
(struct randomizer
));
294 __setup_new_randomizer
(&pktd
->rnd
[packetdr_last
]);
297 static void set_dynamic_incdec
(uint8_t start
, uint8_t stop
, uint8_t stepping
,
300 struct packet
*pkt
= &packets
[packet_last
];
301 struct packet_dyn
*pktd
= &packet_dyn
[packetd_last
];
307 pkt
->payload
= xrealloc
(pkt
->payload
, 1, pkt
->len
);
310 pktd
->cnt
=xrealloc
(pktd
->cnt
, 1, pktd
->clen
* sizeof
(struct counter
));
312 __setup_new_counter
(&pktd
->cnt
[packetdc_last
], start
, stop
, stepping
, type
);
318 long long int number
;
322 %token K_COMMENT K_FILL K_RND K_SEQINC K_SEQDEC K_DRND K_DINC K_DDEC K_WHITE
323 %token K_CPU K_CSUMIP K_CSUMUDP K_CSUMTCP K_CONST8 K_CONST16 K_CONST32 K_CONST64
325 %token
',' '{' '}' '(' ')' '[' ']' ':' '-' '+' '*' '/' '%' '&' '|' '<' '>' '^'
329 %type
<number
> number expression
332 %left
'-' '+' '*' '/' '%' '&' '|' '<' '>' '^'
339 | packets inline_comment
{ }
340 | packets K_WHITE
{ }
348 : '{' delimiter payload delimiter
'}' {
349 min_cpu
= max_cpu
= -1;
352 | K_CPU
'(' number
':' number
')' ':' K_WHITE
'{' delimiter payload delimiter
'}' {
356 if
(min_cpu
> max_cpu
) {
365 | K_CPU
'(' number
')' ':' K_WHITE
'{' delimiter payload delimiter
'}' {
366 min_cpu
= max_cpu
= $3;
373 | payload elem_delimiter
{ }
387 : number
{ set_byte
((uint8_t) $1); }
388 |
string { set_multi_byte
((uint8_t *) $1 + 1, strlen
($1) - 2); }
404 | expression
'+' expression
406 | expression
'-' expression
408 | expression
'*' expression
410 | expression
'/' expression
412 | expression
'%' expression
414 | expression
'&' expression
416 | expression
'|' expression
418 | expression
'^' expression
420 | expression
'<' '<' expression
422 | expression
'>' '>' expression
429 : K_FILL
'(' number delimiter number
')'
430 { set_fill
($3, $5); }
434 : K_CONST8
'(' expression
')'
435 { set_byte
((uint8_t) $3); }
436 | K_CONST16
'(' expression
')' {
437 uint16_t __c
= cpu_to_be16
((uint16_t) $3);
439 set_multi_byte
((uint8_t *) &__c
, sizeof
(__c
));
441 | K_CONST32
'(' expression
')' {
442 uint32_t __c
= cpu_to_be32
((uint32_t) $3);
444 set_multi_byte
((uint8_t *) &__c
, sizeof
(__c
));
446 | K_CONST64
'(' expression
')' {
447 uint64_t __c
= cpu_to_be64
((uint64_t) $3);
449 set_multi_byte
((uint8_t *) &__c
, sizeof
(__c
));
454 : K_RND
'(' number
')'
459 : K_CSUMIP
'(' number delimiter number
')'
460 { set_csum16
($3, $5, CSUM_IP
); }
461 | K_CSUMTCP
'(' number delimiter number
')'
462 { set_csum16
($3, $5, CSUM_TCP
); }
463 | K_CSUMUDP
'(' number delimiter number
')'
464 { set_csum16
($3, $5, CSUM_UDP
); }
468 : K_SEQINC
'(' number delimiter number
')'
469 { set_sequential_inc
($3, $5, 1); }
470 | K_SEQINC
'(' number delimiter number delimiter number
')'
471 { set_sequential_inc
($3, $5, $7); }
475 : K_SEQDEC
'(' number delimiter number
')'
476 { set_sequential_dec
($3, $5, 1); }
477 | K_SEQDEC
'(' number delimiter number delimiter number
')'
478 { set_sequential_dec
($3, $5, $7); }
483 { set_dynamic_rnd
(); }
484 | K_DRND
'(' number
')'
487 for
(i
= 0; i
< max
; ++i
)
493 : K_DINC
'(' number delimiter number
')'
494 { set_dynamic_incdec
($3, $5, 1, TYPE_INC
); }
495 | K_DINC
'(' number delimiter number delimiter number
')'
496 { set_dynamic_incdec
($3, $5, $7, TYPE_INC
); }
500 : K_DDEC
'(' number delimiter number
')'
501 { set_dynamic_incdec
($3, $5, 1, TYPE_DEC
); }
502 | K_DDEC
'(' number delimiter number delimiter number
')'
503 { set_dynamic_incdec
($3, $5, $7, TYPE_DEC
); }
508 static void finalize_packet
(void)
510 /* XXX hack ... we allocated one packet pointer too much */
515 static void dump_conf
(void)
519 for
(i
= 0; i
< plen
; ++i
) {
520 printf
("[%zu] pkt\n", i
);
521 printf
(" len %zu cnts %zu rnds %zu\n",
527 for
(j
= 0; j
< packets
[i
].len
; ++j
)
528 printf
("%02x ", packets
[i
].payload
[j
]);
531 for
(j
= 0; j
< packet_dyn
[i
].clen
; ++j
)
532 printf
(" cnt%zu [%u,%u], inc %u, off %ld type %s\n", j
,
533 packet_dyn
[i
].cnt
[j
].min
,
534 packet_dyn
[i
].cnt
[j
].max
,
535 packet_dyn
[i
].cnt
[j
].inc
,
536 packet_dyn
[i
].cnt
[j
].off
,
537 packet_dyn
[i
].cnt
[j
].type
== TYPE_INC ?
540 for
(j
= 0; j
< packet_dyn
[i
].rlen
; ++j
)
541 printf
(" rnd%zu off %ld\n", j
,
542 packet_dyn
[i
].rnd
[j
].off
);
546 void cleanup_packets
(void)
550 for
(i
= 0; i
< plen
; ++i
) {
551 if
(packets
[i
].len
> 0)
552 xfree
(packets
[i
].payload
);
557 for
(i
= 0; i
< dlen
; ++i
) {
558 free
(packet_dyn
[i
].cnt
);
559 free
(packet_dyn
[i
].rnd
);
565 int compile_packets
(char *file
, int verbose
, int cpu
, bool invoke_cpp
)
569 memset
(tmp_file
, 0, sizeof
(tmp_file
));
573 char cmd
[256], *dir
, *base
, *a
, *b
;
575 dir
= dirname
((a
= xstrdup
(file
)));
576 base
= basename
((b
= xstrdup
(file
)));
578 slprintf
(tmp_file
, sizeof
(tmp_file
), "%s/.tmp-%u-%s", dir
, rand
(), base
);
579 slprintf
(cmd
, sizeof
(cmd
), "cpp -I/etc/netsniff-ng/ %s > %s", file
, tmp_file
);
587 if
(!strncmp
("-", file
, strlen
("-")))
590 yyin
= fopen
(file
, "r");
592 panic
("Cannot open %s: %s!\n", file
, strerror
(errno
));
598 if
(our_cpu
== 0 && verbose
)
608 void yyerror(const char *err
)
610 panic
("Syntax error at line%d, at char '%s'! %s!\n", yylineno
, yytext
, err
);