trafgen: man: Add description for 'dinc' and 'drnd' field functions
[netsniff-ng-new.git] / trafgen_parser.y
blob7e41bff78b2c513b60ddd51ca7092bdae34361dc
1 /*
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.
7 */
9 /* yaac-func-prefix: yy */
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <stdint.h>
16 #include <stdbool.h>
17 #include <errno.h>
18 #include <libgen.h>
19 #include <signal.h>
20 #include <unistd.h>
21 #include <net/if_arp.h>
22 #include <netinet/in.h>
23 #include <linux/icmp.h>
24 #include <linux/if_ether.h>
25 #include <linux/icmpv6.h>
27 #include "xmalloc.h"
28 #include "trafgen_parser.tab.h"
29 #include "trafgen_conf.h"
30 #include "trafgen_proto.h"
31 #include "trafgen_l2.h"
32 #include "trafgen_l3.h"
33 #include "trafgen_l4.h"
34 #include "built_in.h"
35 #include "die.h"
36 #include "str.h"
37 #include "csum.h"
38 #include "cpp.h"
40 #define YYERROR_VERBOSE 0
41 #define YYDEBUG 0
42 #define YYENABLE_NLS 1
43 #define YYLTYPE_IS_TRIVIAL 1
44 #define ENABLE_NLS 1
46 extern FILE *yyin;
47 extern int yylex(void);
48 extern void yy_scan_string(char *);
49 extern void yylex_destroy();
50 extern void yyerror(const char *);
51 extern int yylineno;
52 extern char *yytext;
54 extern struct packet *packets;
55 extern size_t plen;
57 #define packet_last (plen - 1)
59 #define payload_last (packets[packet_last].len - 1)
61 extern struct packet_dyn *packet_dyn;
62 extern size_t dlen;
64 #define packetd_last (dlen - 1)
66 #define packetdc_last (packet_dyn[packetd_last].clen - 1)
67 #define packetdr_last (packet_dyn[packetd_last].rlen - 1)
68 #define packetds_last (packet_dyn[packetd_last].slen - 1)
70 static int our_cpu, min_cpu = -1, max_cpu = -1;
72 enum field_expr_type_t {
73 FIELD_EXPR_UNKNOWN,
74 FIELD_EXPR_NUMB,
75 FIELD_EXPR_MAC,
76 FIELD_EXPR_IP4_ADDR,
77 FIELD_EXPR_IP6_ADDR,
78 FIELD_EXPR_INC,
79 FIELD_EXPR_RND,
82 struct proto_field_expr {
83 enum field_expr_type_t type;
84 struct proto_field *field;
86 union {
87 struct in_addr ip4_addr;
88 struct in6_addr ip6_addr;
89 long long int number;
90 uint8_t bytes[256];
91 struct proto_field_func func;
92 } val;
95 static struct proto_field_expr field_expr;
96 static struct proto_hdr *hdr;
98 static inline int test_ignore(void)
100 if (min_cpu < 0 && max_cpu < 0)
101 return 0;
102 else if (max_cpu >= our_cpu && min_cpu <= our_cpu)
103 return 0;
104 else
105 return 1;
108 static inline void __init_new_packet_slot(struct packet *slot)
110 slot->payload = NULL;
111 slot->len = 0;
114 static inline void __init_new_counter_slot(struct packet_dyn *slot)
116 slot->cnt = NULL;
117 slot->clen = 0;
120 static inline void __init_new_randomizer_slot(struct packet_dyn *slot)
122 slot->rnd = NULL;
123 slot->rlen = 0;
126 static inline void __init_new_csum_slot(struct packet_dyn *slot)
128 slot->csum = NULL;
129 slot->slen = 0;
132 static inline void __init_new_fields_slot(struct packet_dyn *slot)
134 slot->fields = NULL;
135 slot->flen = 0;
138 static inline void __setup_new_counter(struct counter *c, uint8_t start,
139 uint8_t stop, uint8_t stepping,
140 int type)
142 c->min = start;
143 c->max = stop;
144 c->inc = stepping;
145 c->val = (type == TYPE_INC) ? start : stop;
146 c->off = payload_last;
147 c->type = type;
150 static inline void __setup_new_randomizer(struct randomizer *r)
152 r->off = payload_last;
155 static inline void __setup_new_csum16(struct csum16 *s, off_t from, off_t to,
156 enum csum which)
158 s->off = payload_last - 1;
159 s->from = from;
160 s->to = to;
161 s->which = which;
164 static void realloc_packet(void)
166 if (test_ignore())
167 return;
169 plen++;
170 packets = xrealloc(packets, plen * sizeof(*packets));
172 __init_new_packet_slot(&packets[packet_last]);
174 dlen++;
175 packet_dyn = xrealloc(packet_dyn, dlen * sizeof(*packet_dyn));
177 __init_new_counter_slot(&packet_dyn[packetd_last]);
178 __init_new_randomizer_slot(&packet_dyn[packetd_last]);
179 __init_new_csum_slot(&packet_dyn[packetd_last]);
180 __init_new_fields_slot(&packet_dyn[packetd_last]);
183 struct packet *current_packet(void)
185 return &packets[packet_last];
188 uint32_t current_packet_id(void)
190 return packet_last;
193 struct packet *packet_get(uint32_t id)
195 return &packets[id];
198 static void set_byte(uint8_t val)
200 struct packet *pkt = &packets[packet_last];
202 if (test_ignore())
203 return;
205 pkt->len++;
206 pkt->payload = xrealloc(pkt->payload, pkt->len);
207 pkt->payload[payload_last] = val;
210 static void set_multi_byte(uint8_t *s, size_t len)
212 size_t i;
214 for (i = 0; i < len; ++i)
215 set_byte(s[i]);
218 void set_fill(uint8_t val, size_t len)
220 size_t i;
221 struct packet *pkt = &packets[packet_last];
223 if (test_ignore())
224 return;
226 pkt->len += len;
227 pkt->payload = xrealloc(pkt->payload, pkt->len);
228 for (i = 0; i < len; ++i)
229 pkt->payload[payload_last - i] = val;
232 static void __set_csum16_dynamic(size_t from, size_t to, enum csum which)
234 struct packet *pkt = &packets[packet_last];
235 struct packet_dyn *pktd = &packet_dyn[packetd_last];
237 pkt->len += 2;
238 pkt->payload = xrealloc(pkt->payload, pkt->len);
240 pktd->slen++;
241 pktd->csum = xrealloc(pktd->csum, pktd->slen * sizeof(struct csum16));
243 __setup_new_csum16(&pktd->csum[packetds_last], from, to, which);
246 static void __set_csum16_static(size_t from, size_t to, enum csum which __maybe_unused)
248 struct packet *pkt = &packets[packet_last];
249 uint16_t sum;
250 uint8_t *psum;
252 sum = htons(calc_csum(pkt->payload + from, to - from));
253 psum = (uint8_t *) &sum;
255 set_byte(psum[0]);
256 set_byte(psum[1]);
259 static inline bool is_dynamic_csum(enum csum which)
261 switch (which) {
262 case CSUM_UDP:
263 case CSUM_TCP:
264 case CSUM_UDP6:
265 case CSUM_TCP6:
266 return true;
267 default:
268 return false;
272 static void set_csum16(size_t from, size_t to, enum csum which)
274 struct packet *pkt = &packets[packet_last];
275 struct packet_dyn *pktd = &packet_dyn[packetd_last];
277 if (test_ignore())
278 return;
280 if (to < from) {
281 size_t tmp = to;
283 to = from;
284 from = tmp;
287 bug_on(!(from < to));
289 if (packet_dyn_has_elems(pktd) || to >= pkt->len || is_dynamic_csum(which))
290 __set_csum16_dynamic(from, to, which);
291 else
292 __set_csum16_static(from, to, which);
295 static void set_rnd(size_t len)
297 size_t i;
298 struct packet *pkt = &packets[packet_last];
300 if (test_ignore())
301 return;
303 pkt->len += len;
304 pkt->payload = xrealloc(pkt->payload, pkt->len);
305 for (i = 0; i < len; ++i)
306 pkt->payload[payload_last - i] = (uint8_t) rand();
309 static void set_sequential_inc(uint8_t start, size_t len, uint8_t stepping)
311 size_t i;
312 struct packet *pkt = &packets[packet_last];
314 if (test_ignore())
315 return;
317 pkt->len += len;
318 pkt->payload = xrealloc(pkt->payload, pkt->len);
319 for (i = 0; i < len; ++i) {
320 off_t off = len - 1 - i;
322 pkt->payload[payload_last - off] = start;
323 start += stepping;
327 static void set_sequential_dec(uint8_t start, size_t len, uint8_t stepping)
329 size_t i;
330 struct packet *pkt = &packets[packet_last];
332 if (test_ignore())
333 return;
335 pkt->len += len;
336 pkt->payload = xrealloc(pkt->payload, pkt->len);
337 for (i = 0; i < len; ++i) {
338 int off = len - 1 - i;
340 pkt->payload[payload_last - off] = start;
341 start -= stepping;
345 static void set_dynamic_rnd(void)
347 struct packet *pkt = &packets[packet_last];
348 struct packet_dyn *pktd = &packet_dyn[packetd_last];
350 if (test_ignore())
351 return;
353 pkt->len++;
354 pkt->payload = xrealloc(pkt->payload, pkt->len);
356 pktd->rlen++;
357 pktd->rnd = xrealloc(pktd->rnd, pktd->rlen * sizeof(struct randomizer));
359 __setup_new_randomizer(&pktd->rnd[packetdr_last]);
362 static void set_dynamic_incdec(uint8_t start, uint8_t stop, uint8_t stepping,
363 int type)
365 struct packet *pkt = &packets[packet_last];
366 struct packet_dyn *pktd = &packet_dyn[packetd_last];
368 if (test_ignore())
369 return;
371 pkt->len++;
372 pkt->payload = xrealloc(pkt->payload, pkt->len);
374 pktd->clen++;
375 pktd->cnt = xrealloc(pktd->cnt, pktd->clen * sizeof(struct counter));
377 __setup_new_counter(&pktd->cnt[packetdc_last], start, stop, stepping, type);
380 static void proto_add(enum proto_id pid)
382 hdr = proto_header_push(pid);
385 static void proto_field_set(uint32_t fid)
387 field_expr.field = proto_field_by_id(hdr, fid);
390 static void proto_field_func_setup(struct proto_field *field, struct proto_field_func *func)
392 struct packet_dyn *pkt_dyn;
394 proto_field_func_add(field->hdr, field->id, func);
396 pkt_dyn = &packet_dyn[packetd_last];
397 pkt_dyn->flen++;
398 pkt_dyn->fields = xrealloc(pkt_dyn->fields, pkt_dyn->flen *
399 sizeof(struct proto_field *));
400 pkt_dyn->fields[pkt_dyn->flen - 1] = field;
403 static void proto_field_expr_eval(void)
405 struct proto_field *field = field_expr.field;
407 switch (field_expr.type) {
408 case FIELD_EXPR_NUMB:
409 if (field->len == 1)
410 proto_field_set_u8(hdr, field->id, field_expr.val.number);
411 else if (field->len == 2)
412 proto_field_set_be16(hdr, field->id, field_expr.val.number);
413 else if (field->len == 4)
414 proto_field_set_be32(hdr, field->id, field_expr.val.number);
415 else
416 bug();
417 break;
419 case FIELD_EXPR_MAC:
420 proto_field_set_bytes(hdr, field->id, field_expr.val.bytes);
421 break;
423 case FIELD_EXPR_IP4_ADDR:
424 proto_field_set_u32(hdr, field->id, field_expr.val.ip4_addr.s_addr);
425 break;
427 case FIELD_EXPR_IP6_ADDR:
428 proto_field_set_bytes(hdr, field->id,
429 (uint8_t *)&field_expr.val.ip6_addr.s6_addr);
430 break;
432 case FIELD_EXPR_INC:
433 case FIELD_EXPR_RND:
434 if (field_expr.val.func.min
435 && field_expr.val.func.min >= field_expr.val.func.max)
436 panic("dinc(): min(%u) can't be >= max(%u)\n",
437 field_expr.val.func.min, field_expr.val.func.max);
439 proto_field_func_setup(field, &field_expr.val.func);
440 break;
442 case FIELD_EXPR_UNKNOWN:
443 default:
444 bug();
447 memset(&field_expr, 0, sizeof(field_expr));
452 %union {
453 struct in_addr ip4_addr;
454 struct in6_addr ip6_addr;
455 long long int number;
456 uint8_t bytes[256];
457 char *str;
460 %token K_COMMENT K_FILL K_RND K_SEQINC K_SEQDEC K_DRND K_DINC K_DDEC K_WHITE
461 %token K_CPU K_CSUMIP K_CSUMUDP K_CSUMTCP K_CSUMUDP6 K_CSUMTCP6 K_CONST8 K_CONST16 K_CONST32 K_CONST64
463 %token K_DADDR K_SADDR K_ETYPE K_TYPE
464 %token K_OPER K_SHA K_SPA K_THA K_TPA K_REQUEST K_REPLY K_PTYPE K_HTYPE
465 %token K_PROT K_TTL K_DSCP K_ECN K_TOS K_LEN K_ID K_FLAGS K_FRAG K_IHL K_VER K_CSUM K_DF K_MF
466 %token K_FLOW K_NEXT_HDR K_HOP_LIMIT
467 %token K_CODE K_ECHO_REQUEST K_ECHO_REPLY
468 %token K_SPORT K_DPORT
469 %token K_SEQ K_ACK_SEQ K_DOFF K_CWR K_ECE K_URG K_ACK K_PSH K_RST K_SYN K_FIN K_WINDOW K_URG_PTR
470 %token K_TPID K_TCI K_PCP K_DEI K_1Q K_1AD
471 %token K_LABEL K_TC K_LAST K_EXP
473 %token K_ADDR K_MTU
475 %token K_ETH
476 %token K_VLAN K_MPLS
477 %token K_ARP
478 %token K_IP4 K_IP6
479 %token K_ICMP4 K_ICMP6
480 %token K_UDP K_TCP
482 %token ',' '{' '}' '(' ')' '[' ']' ':' '-' '+' '*' '/' '%' '&' '|' '<' '>' '^'
484 %token number string mac ip4_addr ip6_addr
486 %type <number> number expression
487 %type <str> string
488 %type <bytes> mac
489 %type <ip4_addr> ip4_addr
490 %type <ip6_addr> ip6_addr
492 %left '-' '+' '*' '/' '%' '&' '|' '<' '>' '^'
496 packets
497 : { }
498 | packets packet { }
499 | packets inline_comment { }
500 | packets K_WHITE { }
503 inline_comment
504 : K_COMMENT { }
507 cpu_delim
508 : ':' { }
509 | '-' { }
512 delimiter_nowhite
513 : ',' { }
514 | ',' K_WHITE { }
517 noenforce_white
518 : { }
519 | K_WHITE { }
520 | delimiter_nowhite { }
523 skip_white
524 : { }
525 | K_WHITE { }
527 packet
528 : '{' noenforce_white payload noenforce_white '}' {
529 min_cpu = max_cpu = -1;
531 proto_packet_finish();
533 realloc_packet();
535 | K_CPU '(' number cpu_delim number ')' ':' noenforce_white '{' noenforce_white payload noenforce_white '}' {
536 min_cpu = $3;
537 max_cpu = $5;
539 if (min_cpu > max_cpu) {
540 int tmp = min_cpu;
542 min_cpu = max_cpu;
543 max_cpu = tmp;
546 proto_packet_finish();
548 realloc_packet();
550 | K_CPU '(' number ')' ':' noenforce_white '{' noenforce_white payload noenforce_white '}' {
551 min_cpu = max_cpu = $3;
553 proto_packet_finish();
555 realloc_packet();
559 payload
560 : elem { }
561 | payload elem_delimiter { }
564 delimiter
565 : delimiter_nowhite { }
566 | K_WHITE { }
569 elem_delimiter
570 : delimiter elem { }
573 elem
574 : number { set_byte((uint8_t) $1); }
575 | string { set_multi_byte((uint8_t *) $1 + 1, strlen($1) - 2); }
576 | fill { }
577 | rnd { }
578 | drnd { }
579 | seqinc { }
580 | seqdec { }
581 | dinc { }
582 | ddec { }
583 | csum { }
584 | const { }
585 | proto { proto_header_finish(hdr); }
586 | inline_comment { }
589 expression
590 : number
591 { $$ = $1; }
592 | expression '+' expression
593 { $$ = $1 + $3; }
594 | expression '-' expression
595 { $$ = $1 - $3; }
596 | expression '*' expression
597 { $$ = $1 * $3; }
598 | expression '/' expression
599 { $$ = $1 / $3; }
600 | expression '%' expression
601 { $$ = $1 % $3; }
602 | expression '&' expression
603 { $$ = $1 & $3; }
604 | expression '|' expression
605 { $$ = $1 | $3; }
606 | expression '^' expression
607 { $$ = $1 ^ $3; }
608 | expression '<' '<' expression
609 { $$ = $1 << $4; }
610 | expression '>' '>' expression
611 { $$ = $1 >> $4; }
612 | '-' expression
613 { $$ = -1 * $2; }
614 | '(' expression ')'
615 { $$ = $2;}
618 fill
619 : K_FILL '(' number delimiter number ')'
620 { set_fill($3, $5); }
623 const
624 : K_CONST8 '(' expression ')'
625 { set_byte((uint8_t) $3); }
626 | K_CONST16 '(' expression ')' {
627 uint16_t __c = cpu_to_be16((uint16_t) $3);
629 set_multi_byte((uint8_t *) &__c, sizeof(__c));
631 | K_CONST32 '(' expression ')' {
632 uint32_t __c = cpu_to_be32((uint32_t) $3);
634 set_multi_byte((uint8_t *) &__c, sizeof(__c));
636 | K_CONST64 '(' expression ')' {
637 uint64_t __c = cpu_to_be64((uint64_t) $3);
639 set_multi_byte((uint8_t *) &__c, sizeof(__c));
644 : K_RND '(' number ')'
645 { set_rnd($3); }
648 csum
649 : K_CSUMIP '(' number delimiter number ')'
650 { set_csum16($3, $5, CSUM_IP); }
651 | K_CSUMTCP '(' number delimiter number ')'
652 { set_csum16($3, $5, CSUM_TCP); }
653 | K_CSUMUDP '(' number delimiter number ')'
654 { set_csum16($3, $5, CSUM_UDP); }
655 | K_CSUMTCP6 '(' number delimiter number ')'
656 { set_csum16($3, $5, CSUM_TCP6); }
657 | K_CSUMUDP6 '(' number delimiter number ')'
658 { set_csum16($3, $5, CSUM_UDP6); }
661 seqinc
662 : K_SEQINC '(' number delimiter number ')'
663 { set_sequential_inc($3, $5, 1); }
664 | K_SEQINC '(' number delimiter number delimiter number ')'
665 { set_sequential_inc($3, $5, $7); }
668 seqdec
669 : K_SEQDEC '(' number delimiter number ')'
670 { set_sequential_dec($3, $5, 1); }
671 | K_SEQDEC '(' number delimiter number delimiter number ')'
672 { set_sequential_dec($3, $5, $7); }
675 drnd
676 : K_DRND '(' ')'
677 { set_dynamic_rnd(); }
678 | K_DRND '(' number ')'
680 int i, max = $3;
681 for (i = 0; i < max; ++i)
682 set_dynamic_rnd();
686 dinc
687 : K_DINC '(' number delimiter number ')'
688 { set_dynamic_incdec($3, $5, 1, TYPE_INC); }
689 | K_DINC '(' number delimiter number delimiter number ')'
690 { set_dynamic_incdec($3, $5, $7, TYPE_INC); }
693 ddec
694 : K_DDEC '(' number delimiter number ')'
695 { set_dynamic_incdec($3, $5, 1, TYPE_DEC); }
696 | K_DDEC '(' number delimiter number delimiter number ')'
697 { set_dynamic_incdec($3, $5, $7, TYPE_DEC); }
700 proto
701 : eth_proto { }
702 | vlan_proto { }
703 | mpls_proto { }
704 | arp_proto { }
705 | ip4_proto { }
706 | ip6_proto { }
707 | icmp4_proto { }
708 | icmpv6_proto { }
709 | udp_proto { }
710 | tcp_proto { }
713 field_expr
714 : number { field_expr.type = FIELD_EXPR_NUMB;
715 field_expr.val.number = $1; }
716 | mac { field_expr.type = FIELD_EXPR_MAC;
717 memcpy(field_expr.val.bytes, $1, sizeof(field_expr.val.bytes)); }
718 | ip4_addr { field_expr.type = FIELD_EXPR_IP4_ADDR;
719 field_expr.val.ip4_addr = $1; }
720 | ip6_addr { field_expr.type = FIELD_EXPR_IP6_ADDR;
721 field_expr.val.ip6_addr = $1; }
722 | K_DINC '(' ')' { field_expr.type = FIELD_EXPR_INC;
723 field_expr.val.func.type = PROTO_FIELD_FUNC_INC;
724 field_expr.val.func.inc = 1; }
725 | K_DINC '(' number ')'
726 { field_expr.type = FIELD_EXPR_INC;
727 field_expr.val.func.type = PROTO_FIELD_FUNC_INC;
728 field_expr.val.func.inc = $3; }
729 | K_DINC '(' number delimiter number ')'
730 { field_expr.type = FIELD_EXPR_INC;
731 field_expr.val.func.type = PROTO_FIELD_FUNC_INC;
732 field_expr.val.func.type |= PROTO_FIELD_FUNC_MIN;
733 field_expr.val.func.min = $3;
734 field_expr.val.func.max = $5;
735 field_expr.val.func.inc = 1; }
736 | K_DINC '(' number delimiter number delimiter number ')'
737 { field_expr.type = FIELD_EXPR_INC;
738 field_expr.val.func.type = PROTO_FIELD_FUNC_INC;
739 field_expr.val.func.type |= PROTO_FIELD_FUNC_MIN;
740 field_expr.val.func.min = $3;
741 field_expr.val.func.max = $5;
742 field_expr.val.func.inc = $7; }
743 | K_DRND '(' ')' { field_expr.type = FIELD_EXPR_RND;
744 field_expr.val.func.type = PROTO_FIELD_FUNC_RND; }
745 | K_DRND '(' number delimiter number ')'
746 { field_expr.type = FIELD_EXPR_RND;
747 field_expr.val.func.type = PROTO_FIELD_FUNC_RND;
748 field_expr.val.func.min = $3;
749 field_expr.val.func.max = $5; }
752 eth_proto
753 : eth '(' eth_param_list ')' { }
757 : K_ETH { proto_add(PROTO_ETH); }
760 eth_param_list
761 : { }
762 | eth_expr { }
763 | eth_expr delimiter eth_param_list { }
766 eth_type
767 : K_ETYPE { }
768 | K_TYPE { }
769 | K_PROT { }
772 eth_field
773 : K_DADDR { proto_field_set(ETH_DST_ADDR); }
774 | K_SADDR { proto_field_set(ETH_SRC_ADDR); }
775 | eth_type { proto_field_set(ETH_TYPE); }
777 eth_expr
778 : eth_field skip_white '=' skip_white field_expr
779 { proto_field_expr_eval(); }
782 vlan_proto
783 : vlan '(' vlan_param_list ')' { }
786 vlan
787 : K_VLAN { proto_add(PROTO_VLAN); }
790 vlan_param_list
791 : { }
792 | vlan_expr { }
793 | vlan_expr delimiter vlan_param_list { }
796 vlan_type
797 : K_TPID { }
798 | K_PROT
801 vlan_field
802 : vlan_type { proto_field_set(VLAN_TPID); }
803 | K_TCI { proto_field_set(VLAN_TCI); }
804 | K_PCP { proto_field_set(VLAN_PCP); }
805 | K_DEI { proto_field_set(VLAN_DEI); }
806 | K_ID { proto_field_set(VLAN_VID); }
809 vlan_expr
810 : vlan_field skip_white '=' skip_white field_expr
811 { proto_field_expr_eval(); }
812 | K_1Q
813 { proto_field_set_be16(hdr, VLAN_TPID, ETH_P_8021Q); }
814 | K_1AD
815 { proto_field_set_be16(hdr, VLAN_TPID, ETH_P_8021AD); }
818 mpls_proto
819 : mpls '(' mpls_param_list ')' { }
822 mpls
823 : K_MPLS { proto_add(PROTO_MPLS); }
826 mpls_param_list
827 : { }
828 | mpls_expr { }
829 | mpls_expr delimiter mpls_param_list { }
832 mpls_tc
833 : K_TC { }
834 | K_EXP { }
837 mpls_field
838 : K_LABEL { proto_field_set(MPLS_LABEL); }
839 | mpls_tc { proto_field_set(MPLS_TC); }
840 | K_LAST { proto_field_set(MPLS_LAST); }
841 | K_TTL { proto_field_set(MPLS_TTL); }
844 mpls_expr
845 : mpls_field skip_white '=' skip_white field_expr
846 { proto_field_expr_eval(); }
848 arp_proto
849 : arp '(' arp_param_list ')' { }
852 arp_param_list
853 : { }
854 | arp_expr { }
855 | arp_expr delimiter arp_param_list { }
858 arp_field
859 : K_HTYPE
860 { proto_field_set(ARP_HTYPE); }
861 | K_PTYPE
862 { proto_field_set(ARP_PTYPE); }
863 | K_SHA
864 { proto_field_set(ARP_SHA); }
865 | K_THA
866 { proto_field_set(ARP_THA); }
867 | K_SPA
868 { proto_field_set(ARP_SPA); }
869 | K_TPA
870 { proto_field_set(ARP_TPA); }
873 arp_expr
874 : arp_field skip_white '=' skip_white field_expr
875 { proto_field_expr_eval(); }
876 | K_OPER skip_white '=' skip_white field_expr
877 { proto_field_set(ARP_OPER);
878 proto_field_expr_eval(); }
879 | K_OPER skip_white '=' skip_white K_REQUEST
880 { proto_field_set_be16(hdr, ARP_OPER, ARPOP_REQUEST); }
881 | K_OPER skip_white '=' skip_white K_REPLY
882 { proto_field_set_be16(hdr, ARP_OPER, ARPOP_REPLY); }
883 | K_REQUEST
884 { proto_field_set_be16(hdr, ARP_OPER, ARPOP_REQUEST); }
885 | K_REPLY
886 { proto_field_set_be16(hdr, ARP_OPER, ARPOP_REPLY); }
889 : K_ARP { proto_add(PROTO_ARP); }
892 ip4_proto
893 : ip4 '(' ip4_param_list ')' { }
896 ip4_param_list
897 : { }
898 | ip4_expr { }
899 | ip4_expr delimiter ip4_param_list { }
902 ip4_field
903 : K_VER { proto_field_set(IP4_VER); }
904 | K_IHL { proto_field_set(IP4_IHL); }
905 | K_DADDR { proto_field_set(IP4_DADDR); }
906 | K_SADDR { proto_field_set(IP4_SADDR); }
907 | K_PROT { proto_field_set(IP4_PROTO); }
908 | K_TTL { proto_field_set(IP4_TTL); }
909 | K_DSCP { proto_field_set(IP4_DSCP); }
910 | K_ECN { proto_field_set(IP4_ECN); }
911 | K_TOS { proto_field_set(IP4_TOS); }
912 | K_LEN { proto_field_set(IP4_LEN); }
913 | K_ID { proto_field_set(IP4_ID); }
914 | K_FLAGS { proto_field_set(IP4_FLAGS); }
915 | K_FRAG { proto_field_set(IP4_FRAG_OFFS); }
916 | K_CSUM { proto_field_set(IP4_CSUM); }
919 ip4_expr
920 : ip4_field skip_white '=' skip_white field_expr
921 { proto_field_expr_eval(); }
922 | K_DF { proto_field_set_be16(hdr, IP4_DF, 1); }
923 | K_MF { proto_field_set_be16(hdr, IP4_MF, 1); }
927 : K_IP4 { proto_add(PROTO_IP4); }
930 ip6_proto
931 : ip6 '(' ip6_param_list ')' { }
934 ip6_param_list
935 : { }
936 | ip6_expr { }
937 | ip6_expr delimiter ip6_param_list { }
940 ip6_hop_limit
941 : K_HOP_LIMIT { }
942 | K_TTL { }
945 ip6_field
946 : K_VER { proto_field_set(IP6_VER); }
947 | K_TC { proto_field_set(IP6_CLASS); }
948 | K_FLOW { proto_field_set(IP6_FLOW_LBL); }
949 | K_LEN { proto_field_set(IP6_LEN); }
950 | K_NEXT_HDR { proto_field_set(IP6_NEXT_HDR); }
951 | ip6_hop_limit { proto_field_set(IP6_HOP_LIMIT); }
952 | K_SADDR { proto_field_set(IP6_SADDR); }
953 | K_DADDR { proto_field_set(IP6_DADDR) ; }
956 ip6_expr
957 : ip6_field skip_white '=' skip_white field_expr
958 { proto_field_expr_eval(); }
962 : K_IP6 { proto_add(PROTO_IP6); }
965 icmp4_proto
966 : icmp4 '(' icmp4_param_list ')' { }
969 icmp4_param_list
970 : { }
971 | icmp4_expr { }
972 | icmp4_expr delimiter icmp4_param_list { }
975 icmp4_field
976 : K_TYPE { proto_field_set(ICMPV4_TYPE); }
977 | K_CODE { proto_field_set(ICMPV4_CODE); }
978 | K_ID { proto_field_set(ICMPV4_ID); }
979 | K_SEQ { proto_field_set(ICMPV4_SEQ); }
980 | K_MTU { proto_field_set(ICMPV4_MTU); }
981 | K_ADDR { proto_field_set(ICMPV4_REDIR_ADDR); }
984 icmp4_expr
985 : icmp4_field skip_white '=' skip_white field_expr
986 { proto_field_expr_eval(); }
987 | K_ECHO_REQUEST
988 { proto_field_set_u8(hdr, ICMPV4_TYPE, ICMP_ECHO);
989 proto_field_set_u8(hdr, ICMPV4_CODE, 0); }
990 | K_ECHO_REPLY
991 { proto_field_set_u8(hdr, ICMPV4_TYPE, ICMP_ECHOREPLY);
992 proto_field_set_u8(hdr, ICMPV4_CODE, 0); }
995 icmp4
996 : K_ICMP4 { proto_add(PROTO_ICMP4); }
999 icmpv6_proto
1000 : icmp6 '(' icmp6_param_list ')' { }
1002 icmp6_param_list
1003 : { }
1004 | icmp6_expr { }
1005 | icmp6_expr delimiter icmp6_param_list { }
1008 icmp6_field
1009 : K_CODE { proto_field_set(ICMPV6_CODE); }
1010 | K_CSUM { proto_field_set(ICMPV6_CSUM); }
1013 icmp6_expr
1014 : icmp6_field skip_white '=' skip_white field_expr
1015 { proto_field_expr_eval(); }
1016 | K_TYPE skip_white '=' skip_white field_expr
1017 { proto_field_set(ICMPV6_TYPE);
1018 proto_field_expr_eval(); }
1019 | K_TYPE skip_white '=' K_ECHO_REQUEST
1020 { proto_field_set_u8(hdr, ICMPV6_TYPE, ICMPV6_ECHO_REQUEST); }
1021 | K_ECHO_REQUEST
1022 { proto_field_set_u8(hdr, ICMPV6_TYPE, ICMPV6_ECHO_REQUEST); }
1023 | K_TYPE skip_white '=' K_ECHO_REPLY
1024 { proto_field_set_u8(hdr, ICMPV6_TYPE, ICMPV6_ECHO_REPLY); }
1025 | K_ECHO_REPLY
1026 { proto_field_set_u8(hdr, ICMPV6_TYPE, ICMPV6_ECHO_REPLY); }
1028 icmp6
1029 : K_ICMP6 { proto_add(PROTO_ICMP6); }
1032 udp_proto
1033 : udp '(' udp_param_list ')' { }
1036 udp_param_list
1037 : { }
1038 | udp_expr { }
1039 | udp_expr delimiter udp_param_list { }
1042 udp_field
1043 : K_SPORT { proto_field_set(UDP_SPORT); }
1044 | K_DPORT { proto_field_set(UDP_DPORT); }
1045 | K_LEN { proto_field_set(UDP_LEN); }
1046 | K_CSUM { proto_field_set(UDP_CSUM); }
1049 udp_expr
1050 : udp_field skip_white '=' skip_white field_expr
1051 { proto_field_expr_eval(); }
1055 : K_UDP { proto_add(PROTO_UDP); }
1058 tcp_proto
1059 : tcp '(' tcp_param_list ')' { }
1062 tcp_param_list
1063 : { }
1064 | tcp_expr { }
1065 | tcp_expr delimiter tcp_param_list { }
1068 tcp_field
1069 : K_SPORT { proto_field_set(TCP_SPORT); }
1070 | K_DPORT { proto_field_set(TCP_DPORT); }
1071 | K_SEQ { proto_field_set(TCP_SEQ); }
1072 | K_ACK_SEQ { proto_field_set(TCP_ACK_SEQ); }
1073 | K_DOFF { proto_field_set(TCP_DOFF); }
1074 | K_WINDOW { proto_field_set(TCP_WINDOW); }
1075 | K_CSUM { proto_field_set(TCP_CSUM); }
1076 | K_URG_PTR { proto_field_set(TCP_URG_PTR); }
1079 tcp_expr
1080 : tcp_field skip_white '=' skip_white field_expr
1081 { proto_field_expr_eval(); }
1082 | K_CWR { proto_field_set_be16(hdr, TCP_CWR, 1); }
1083 | K_ECE { proto_field_set_be16(hdr, TCP_ECE, 1); }
1084 | K_URG { proto_field_set_be16(hdr, TCP_URG, 1); }
1085 | K_ACK { proto_field_set_be16(hdr, TCP_ACK, 1); }
1086 | K_PSH { proto_field_set_be16(hdr, TCP_PSH, 1); }
1087 | K_RST { proto_field_set_be16(hdr, TCP_RST, 1); }
1088 | K_SYN { proto_field_set_be16(hdr, TCP_SYN, 1); }
1089 | K_FIN { proto_field_set_be16(hdr, TCP_FIN, 1); }
1093 : K_TCP { proto_add(PROTO_TCP); }
1098 static void finalize_packet(void)
1100 /* XXX hack ... we allocated one packet pointer too much */
1101 plen--;
1102 dlen--;
1105 static void dump_conf(void)
1107 size_t i, j;
1109 for (i = 0; i < plen; ++i) {
1110 printf("[%zu] pkt\n", i);
1111 printf(" len %zu cnts %zu rnds %zu\n",
1112 packets[i].len,
1113 packet_dyn[i].clen,
1114 packet_dyn[i].rlen);
1116 printf(" payload ");
1117 for (j = 0; j < packets[i].len; ++j)
1118 printf("%02x ", packets[i].payload[j]);
1119 printf("\n");
1121 for (j = 0; j < packet_dyn[i].clen; ++j)
1122 printf(" cnt%zu [%u,%u], inc %u, off %jd type %s\n", j,
1123 packet_dyn[i].cnt[j].min,
1124 packet_dyn[i].cnt[j].max,
1125 packet_dyn[i].cnt[j].inc,
1126 (intmax_t)packet_dyn[i].cnt[j].off,
1127 packet_dyn[i].cnt[j].type == TYPE_INC ?
1128 "inc" : "dec");
1130 for (j = 0; j < packet_dyn[i].rlen; ++j)
1131 printf(" rnd%zu off %jd\n", j,
1132 (intmax_t)packet_dyn[i].rnd[j].off);
1136 void cleanup_packets(void)
1138 size_t i, j;
1140 for (i = 0; i < plen; ++i) {
1141 struct packet *pkt = &packets[i];
1143 if (pkt->len > 0)
1144 xfree(pkt->payload);
1146 for (j = 0; j < pkt->headers_count; j++) {
1147 struct proto_hdr *hdr = pkt->headers[j];
1149 if (hdr->fields)
1150 xfree(hdr->fields);
1151 xfree(hdr);
1155 free(packets);
1157 for (i = 0; i < dlen; ++i) {
1158 free(packet_dyn[i].cnt);
1159 free(packet_dyn[i].rnd);
1160 free(packet_dyn[i].fields);
1163 free(packet_dyn);
1166 void compile_packets(char *file, bool verbose, unsigned int cpu,
1167 bool invoke_cpp, char *const cpp_argv[])
1169 char tmp_file[128];
1170 int ret = -1;
1172 if (access(file, R_OK)) {
1173 fprintf(stderr, "Cannot access %s: %s!\n", file, strerror(errno));
1174 die();
1177 memset(tmp_file, 0, sizeof(tmp_file));
1178 our_cpu = cpu;
1180 if (invoke_cpp) {
1181 if (cpp_exec(file, tmp_file, sizeof(tmp_file), cpp_argv)) {
1182 fprintf(stderr, "Failed to invoke C preprocessor!\n");
1183 goto err;
1185 file = tmp_file;
1188 if (!strncmp("-", file, strlen("-")))
1189 yyin = stdin;
1190 else
1191 yyin = fopen(file, "r");
1192 if (!yyin) {
1193 fprintf(stderr, "Cannot open %s: %s!\n", file, strerror(errno));
1194 goto err;
1197 realloc_packet();
1198 if (yyparse() != 0)
1199 goto err;
1200 finalize_packet();
1202 if (our_cpu == 0 && verbose)
1203 dump_conf();
1205 ret = 0;
1206 err:
1207 if (yyin && yyin != stdin)
1208 fclose(yyin);
1210 if (invoke_cpp)
1211 unlink(tmp_file);
1212 if (ret)
1213 die();
1216 void compile_packets_str(char *str, bool verbose, unsigned int cpu)
1218 int ret = 1;
1220 our_cpu = cpu;
1221 realloc_packet();
1223 yy_scan_string(str);
1224 if (yyparse() != 0)
1225 goto err;
1227 finalize_packet();
1228 if (our_cpu == 0 && verbose)
1229 dump_conf();
1231 ret = 0;
1232 err:
1233 yylex_destroy();
1235 if (ret)
1236 die();
1239 void yyerror(const char *err)
1241 fprintf(stderr, "Syntax error at line %d, char '%s': %s\n", yylineno, yytext, err);