staging: compilation fix with new gcc
[netsniff-ng.git] / trafgen_parser.y
blob656c4f6a31c36c23a78a40594204fa99ff31220f
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 /* yacc-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 "trafgen_l7.h"
35 #include "built_in.h"
36 #include "die.h"
37 #include "str.h"
38 #include "csum.h"
39 #include "cpp.h"
41 #ifndef ETH_P_8021AD
42 #define ETH_P_8021AD 0x88A8
43 #endif
45 #define YYERROR_VERBOSE 0
46 #define YYDEBUG 0
47 #define YYLTYPE_IS_TRIVIAL 1
49 extern FILE *yyin;
50 extern int yylex(void);
51 extern void yy_scan_string(char *);
52 extern void yylex_destroy();
53 extern void yyerror(const char *);
54 extern int yylineno;
55 extern char *yytext;
57 extern struct packet *packets;
58 extern size_t plen;
60 #define packet_last (plen - 1)
62 #define payload_last (packets[packet_last].len - 1)
64 extern struct packet_dyn *packet_dyn;
65 extern size_t dlen;
67 #define packetd_last (dlen - 1)
69 #define packetdc_last (packet_dyn[packetd_last].clen - 1)
70 #define packetdr_last (packet_dyn[packetd_last].rlen - 1)
71 #define packetds_last (packet_dyn[packetd_last].slen - 1)
73 static int our_cpu, min_cpu = -1, max_cpu = -1;
75 enum field_expr_type_t {
76 FIELD_EXPR_UNKNOWN = 0,
77 FIELD_EXPR_NUMB = 1 << 0,
78 FIELD_EXPR_MAC = 1 << 1,
79 FIELD_EXPR_IP4_ADDR = 1 << 2,
80 FIELD_EXPR_IP6_ADDR = 1 << 3,
81 FIELD_EXPR_INC = 1 << 4,
82 FIELD_EXPR_RND = 1 << 5,
83 FIELD_EXPR_OFFSET = 1 << 6,
84 FIELD_EXPR_STRING = 1 << 7,
85 FIELD_EXPR_FQDN = 1 << 8,
88 struct proto_field_expr {
89 enum field_expr_type_t type;
90 struct proto_field *field;
92 union {
93 struct in_addr ip4_addr;
94 struct in6_addr ip6_addr;
95 long long int number;
96 uint8_t mac[256];
97 char *str;
98 struct proto_field_func func;
99 } val;
102 static struct proto_field_expr field_expr;
103 static struct proto_hdr *hdr;
105 static inline int test_ignore(void)
107 if (min_cpu < 0 && max_cpu < 0)
108 return 0;
109 else if (max_cpu >= our_cpu && min_cpu <= our_cpu)
110 return 0;
111 else
112 return 1;
115 static inline void __init_new_packet_slot(struct packet *slot)
117 memset(slot, 0, sizeof(*slot));
120 static inline void __init_new_counter_slot(struct packet_dyn *slot)
122 slot->cnt = NULL;
123 slot->clen = 0;
126 static inline void __init_new_randomizer_slot(struct packet_dyn *slot)
128 slot->rnd = NULL;
129 slot->rlen = 0;
132 static inline void __init_new_csum_slot(struct packet_dyn *slot)
134 slot->csum = NULL;
135 slot->slen = 0;
138 static inline void __init_new_fields_slot(struct packet_dyn *slot)
140 slot->fields = NULL;
141 slot->flen = 0;
144 static inline void __setup_new_counter(struct counter *c, uint8_t start,
145 uint8_t stop, uint8_t stepping,
146 int type)
148 c->min = start;
149 c->max = stop;
150 c->inc = stepping;
151 c->val = (type == TYPE_INC) ? start : stop;
152 c->off = payload_last;
153 c->type = type;
156 static inline void __setup_new_randomizer(struct randomizer *r)
158 r->off = payload_last;
161 static inline void __setup_new_csum16(struct csum16 *s, off_t from, off_t to,
162 enum csum which)
164 s->off = payload_last - 1;
165 s->from = from;
166 s->to = to;
167 s->which = which;
170 void realloc_packet(void)
172 if (test_ignore())
173 return;
175 plen++;
176 packets = xrealloc(packets, plen * sizeof(*packets));
178 __init_new_packet_slot(&packets[packet_last]);
180 dlen++;
181 packet_dyn = xrealloc(packet_dyn, dlen * sizeof(*packet_dyn));
183 __init_new_counter_slot(&packet_dyn[packetd_last]);
184 __init_new_randomizer_slot(&packet_dyn[packetd_last]);
185 __init_new_csum_slot(&packet_dyn[packetd_last]);
186 __init_new_fields_slot(&packet_dyn[packetd_last]);
189 struct packet *current_packet(void)
191 return &packets[packet_last];
194 uint32_t current_packet_id(void)
196 return packet_last;
199 struct packet *packet_get(uint32_t id)
201 return &packets[id];
204 static void set_byte(uint8_t val)
206 struct packet *pkt = &packets[packet_last];
208 if (test_ignore())
209 return;
211 pkt->len++;
212 pkt->payload = xrealloc(pkt->payload, pkt->len);
213 pkt->payload[payload_last] = val;
216 static void set_multi_byte(uint8_t *s, size_t len)
218 size_t i;
220 for (i = 0; i < len; ++i)
221 set_byte(s[i]);
224 void set_fill(uint8_t val, size_t len)
226 size_t i;
227 struct packet *pkt = &packets[packet_last];
229 if (test_ignore())
230 return;
232 pkt->len += len;
233 pkt->payload = xrealloc(pkt->payload, pkt->len);
234 for (i = 0; i < len; ++i)
235 pkt->payload[payload_last - i] = val;
238 static void __set_csum16_dynamic(size_t from, size_t to, enum csum which)
240 struct packet *pkt = &packets[packet_last];
241 struct packet_dyn *pktd = &packet_dyn[packetd_last];
243 pkt->len += 2;
244 pkt->payload = xrealloc(pkt->payload, pkt->len);
246 pktd->slen++;
247 pktd->csum = xrealloc(pktd->csum, pktd->slen * sizeof(struct csum16));
249 __setup_new_csum16(&pktd->csum[packetds_last], from, to, which);
252 static void __set_csum16_static(size_t from, size_t to, enum csum which __maybe_unused)
254 struct packet *pkt = &packets[packet_last];
255 uint16_t sum;
256 uint8_t *psum;
258 sum = htons(calc_csum(pkt->payload + from, to - from));
259 psum = (uint8_t *) &sum;
261 set_byte(psum[0]);
262 set_byte(psum[1]);
265 static inline bool is_dynamic_csum(enum csum which)
267 switch (which) {
268 case CSUM_UDP:
269 case CSUM_TCP:
270 case CSUM_UDP6:
271 case CSUM_TCP6:
272 return true;
273 default:
274 return false;
278 static void set_csum16(size_t from, size_t to, enum csum which)
280 struct packet *pkt = &packets[packet_last];
281 struct packet_dyn *pktd = &packet_dyn[packetd_last];
283 if (test_ignore())
284 return;
286 if (to < from) {
287 size_t tmp = to;
289 to = from;
290 from = tmp;
293 bug_on(!(from < to));
295 if (packet_dyn_has_elems(pktd) || to >= pkt->len || is_dynamic_csum(which))
296 __set_csum16_dynamic(from, to, which);
297 else
298 __set_csum16_static(from, to, which);
301 static void set_rnd(size_t len)
303 size_t i;
304 struct packet *pkt = &packets[packet_last];
306 if (test_ignore())
307 return;
309 pkt->len += len;
310 pkt->payload = xrealloc(pkt->payload, pkt->len);
311 for (i = 0; i < len; ++i)
312 pkt->payload[payload_last - i] = (uint8_t) rand();
315 static void set_sequential_inc(uint8_t start, size_t len, uint8_t stepping)
317 size_t i;
318 struct packet *pkt = &packets[packet_last];
320 if (test_ignore())
321 return;
323 pkt->len += len;
324 pkt->payload = xrealloc(pkt->payload, pkt->len);
325 for (i = 0; i < len; ++i) {
326 off_t off = len - 1 - i;
328 pkt->payload[payload_last - off] = start;
329 start += stepping;
333 static void set_sequential_dec(uint8_t start, size_t len, uint8_t stepping)
335 size_t i;
336 struct packet *pkt = &packets[packet_last];
338 if (test_ignore())
339 return;
341 pkt->len += len;
342 pkt->payload = xrealloc(pkt->payload, pkt->len);
343 for (i = 0; i < len; ++i) {
344 int off = len - 1 - i;
346 pkt->payload[payload_last - off] = start;
347 start -= stepping;
351 static void set_dynamic_rnd(void)
353 struct packet *pkt = &packets[packet_last];
354 struct packet_dyn *pktd = &packet_dyn[packetd_last];
356 if (test_ignore())
357 return;
359 pkt->len++;
360 pkt->payload = xrealloc(pkt->payload, pkt->len);
362 pktd->rlen++;
363 pktd->rnd = xrealloc(pktd->rnd, pktd->rlen * sizeof(struct randomizer));
365 __setup_new_randomizer(&pktd->rnd[packetdr_last]);
368 static void set_dynamic_incdec(uint8_t start, uint8_t stop, uint8_t stepping,
369 int type)
371 struct packet *pkt = &packets[packet_last];
372 struct packet_dyn *pktd = &packet_dyn[packetd_last];
374 if (test_ignore())
375 return;
377 pkt->len++;
378 pkt->payload = xrealloc(pkt->payload, pkt->len);
380 pktd->clen++;
381 pktd->cnt = xrealloc(pktd->cnt, pktd->clen * sizeof(struct counter));
383 __setup_new_counter(&pktd->cnt[packetdc_last], start, stop, stepping, type);
386 static void proto_add(enum proto_id pid)
388 hdr = proto_header_push(pid);
391 static void proto_field_set(uint32_t fid)
393 memset(&field_expr, 0, sizeof(field_expr));
394 field_expr.field = proto_hdr_field_by_id(hdr, fid);
397 static void proto_field_func_setup(struct proto_field *field, struct proto_field_func *func)
399 struct proto_field *field_copy;
400 struct packet_dyn *pkt_dyn;
402 field_copy = xmalloc(sizeof(*field));
403 memcpy(field_copy, field, sizeof(*field));
405 field_copy->pkt_offset += func->offset;
406 if (func->len)
407 field_copy->len = func->len;
409 proto_field_func_add(field_copy, func);
411 pkt_dyn = &packet_dyn[packetd_last];
412 pkt_dyn->flen++;
413 pkt_dyn->fields = xrealloc(pkt_dyn->fields, pkt_dyn->flen *
414 sizeof(struct proto_field *));
416 pkt_dyn->fields[pkt_dyn->flen - 1] = field_copy;
419 static void proto_field_expr_eval(void)
421 struct proto_field *field = field_expr.field;
423 if ((field_expr.type & FIELD_EXPR_OFFSET) &&
424 !((field_expr.type & FIELD_EXPR_INC) ||
425 (field_expr.type & FIELD_EXPR_RND))) {
427 panic("Field offset expression is valid only with function expression\n");
430 if (field_expr.type & FIELD_EXPR_NUMB) {
431 if (field->len == 1)
432 proto_field_set_u8(field, field_expr.val.number);
433 else if (field->len == 2)
434 proto_field_set_be16(field, field_expr.val.number);
435 else if (field->len == 4)
436 proto_field_set_be32(field, field_expr.val.number);
437 else
438 panic("Invalid value length %zu, can be 1,2 or 4\n", field->len);
439 } else if (field_expr.type & FIELD_EXPR_MAC) {
440 proto_field_set_bytes(field, field_expr.val.mac, 6);
441 } else if (field_expr.type & FIELD_EXPR_FQDN) {
442 char *fqdn = str2fqdn(field_expr.val.str);
443 proto_field_set_bytes(field, (uint8_t *) fqdn, strlen(fqdn) + 1);
444 xfree(field_expr.val.str);
445 xfree(fqdn);
446 } else if (field_expr.type & FIELD_EXPR_STRING) {
447 proto_field_set_string(field, field_expr.val.str);
448 xfree(field_expr.val.str);
449 } else if (field_expr.type & FIELD_EXPR_IP4_ADDR) {
450 proto_field_set_u32(field, field_expr.val.ip4_addr.s_addr);
451 } else if (field_expr.type & FIELD_EXPR_IP6_ADDR) {
452 proto_field_set_bytes(field, (uint8_t *)&field_expr.val.ip6_addr.s6_addr, 16);
453 } else if ((field_expr.type & FIELD_EXPR_INC) ||
454 (field_expr.type & FIELD_EXPR_RND)) {
456 if (field_expr.val.func.min
457 && field_expr.val.func.min >= field_expr.val.func.max)
458 panic("dinc(): min(%u) can't be >= max(%u)\n",
459 field_expr.val.func.min, field_expr.val.func.max);
461 proto_field_func_setup(field, &field_expr.val.func);
462 } else if ((field_expr.type & FIELD_EXPR_OFFSET) &&
463 !((field_expr.type & FIELD_EXPR_INC) ||
464 (field_expr.type & FIELD_EXPR_RND))) {
466 panic("Field expression is valid only for function value expression\n");
467 } else {
468 bug();
471 memset(&field_expr, 0, sizeof(field_expr));
474 static void field_index_validate(struct proto_field *field, uint16_t index, size_t len)
476 if (field_expr.field->len <= index) {
477 yyerror("Invalid [index] parameter");
478 panic("Index (%u) is bigger than field's length (%zu)\n",
479 index, field->len);
481 if (len != 1 && len != 2 && len != 4) {
482 yyerror("Invalid [index:len] parameter");
483 panic("Invalid index length - 1,2 or 4 is only allowed\n");
487 static void proto_push_sub_hdr(uint32_t id)
489 hdr = proto_hdr_push_sub_header(hdr, id);
492 static void proto_pop_sub_hdr(void)
494 if (hdr->ops->header_finish)
495 hdr->ops->header_finish(hdr);
497 hdr = hdr->parent;
502 %union {
503 struct in_addr ip4_addr;
504 struct in6_addr ip6_addr;
505 long long int number;
506 uint8_t mac[6];
507 char *str;
510 %token K_COMMENT K_FILL K_RND K_SEQINC K_SEQDEC K_DRND K_DINC K_DDEC K_WHITE
511 %token K_CPU K_CSUMIP K_CSUMUDP K_CSUMTCP K_CSUMUDP6 K_CSUMTCP6 K_CONST8 K_CONST16 K_CONST32 K_CONST64
513 %token K_DADDR K_SADDR K_ETYPE K_TYPE
514 %token K_TIME K_PRIO
515 %token K_OPER K_SHA K_SPA K_THA K_TPA K_REQUEST K_REPLY K_PTYPE K_HTYPE
516 %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
517 %token K_FLOW K_NEXT_HDR K_HOP_LIMIT
518 %token K_CODE K_ECHO_REQUEST K_ECHO_REPLY
519 %token K_SPORT K_DPORT
520 %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
521 %token K_TPID K_TCI K_PCP K_DEI K_1Q K_1AD
522 %token K_LABEL K_TC K_LAST K_EXP
524 %token K_ADDR K_MTU
526 %token K_QR K_AANSWER K_TRUNC K_RAVAIL K_RDESIRED K_ZERO K_RCODE K_QDCOUNT K_ANCOUNT K_NSCOUNT K_ARCOUNT
527 %token K_QUERY K_ANSWER K_AUTH K_ADD
528 %token K_NAME K_CLASS K_DATA K_NS K_CNAME K_PTR
530 %token K_ETH
531 %token K_PAUSE
532 %token K_PFC
533 %token K_VLAN K_MPLS
534 %token K_ARP
535 %token K_IP4 K_IP6
536 %token K_ICMP4 K_ICMP6
537 %token K_UDP K_TCP
538 %token K_DNS
540 %token ',' '{' '}' '(' ')' '[' ']' ':' '-' '+' '*' '/' '%' '&' '|' '<' '>' '^'
542 %token number string mac ip4_addr ip6_addr
544 %type <number> number expression
545 %type <str> string
546 %type <mac> mac
547 %type <ip4_addr> ip4_addr
548 %type <ip6_addr> ip6_addr
550 %left '-' '+' '*' '/' '%' '&' '|' '<' '>' '^'
554 packets
555 : { }
556 | packets packet { }
557 | packets inline_comment { }
558 | packets K_WHITE { }
561 inline_comment
562 : K_COMMENT { }
565 cpu_delim
566 : ':' { }
567 | '-' { }
570 delimiter_nowhite
571 : ',' { }
572 | ',' K_WHITE { }
575 noenforce_white
576 : { }
577 | K_WHITE { }
578 | delimiter_nowhite { }
581 skip_white
582 : { }
583 | K_WHITE { }
585 packet
586 : '{' noenforce_white payload noenforce_white '}' {
587 min_cpu = max_cpu = -1;
589 proto_packet_finish();
591 realloc_packet();
593 | K_CPU '(' number cpu_delim number ')' ':' noenforce_white '{' noenforce_white payload noenforce_white '}' {
594 min_cpu = $3;
595 max_cpu = $5;
597 if (min_cpu > max_cpu) {
598 int tmp = min_cpu;
600 min_cpu = max_cpu;
601 max_cpu = tmp;
604 proto_packet_finish();
606 realloc_packet();
608 | K_CPU '(' number ')' ':' noenforce_white '{' noenforce_white payload noenforce_white '}' {
609 min_cpu = max_cpu = $3;
611 proto_packet_finish();
613 realloc_packet();
617 payload
618 : elem { }
619 | payload elem_delimiter { }
622 delimiter
623 : delimiter_nowhite { }
624 | K_WHITE { }
627 elem_delimiter
628 : delimiter elem { }
631 elem
632 : number { set_byte((uint8_t) $1); }
633 | string { set_multi_byte((uint8_t *) $1 + 1, strlen($1) - 2); }
634 | fill { }
635 | rnd { }
636 | drnd { }
637 | seqinc { }
638 | seqdec { }
639 | dinc { }
640 | ddec { }
641 | csum { }
642 | const { }
643 | proto { proto_header_finish(hdr); }
644 | inline_comment { }
647 expression
648 : number
649 { $$ = $1; }
650 | expression '+' expression
651 { $$ = $1 + $3; }
652 | expression '-' expression
653 { $$ = $1 - $3; }
654 | expression '*' expression
655 { $$ = $1 * $3; }
656 | expression '/' expression
657 { $$ = $1 / $3; }
658 | expression '%' expression
659 { $$ = $1 % $3; }
660 | expression '&' expression
661 { $$ = $1 & $3; }
662 | expression '|' expression
663 { $$ = $1 | $3; }
664 | expression '^' expression
665 { $$ = $1 ^ $3; }
666 | expression '<' '<' expression
667 { $$ = $1 << $4; }
668 | expression '>' '>' expression
669 { $$ = $1 >> $4; }
670 | '-' expression
671 { $$ = -1 * $2; }
672 | '(' expression ')'
673 { $$ = $2;}
676 fill
677 : K_FILL '(' number delimiter number ')'
678 { set_fill($3, $5); }
681 const
682 : K_CONST8 '(' expression ')'
683 { set_byte((uint8_t) $3); }
684 | K_CONST16 '(' expression ')' {
685 uint16_t __c = cpu_to_be16((uint16_t) $3);
687 set_multi_byte((uint8_t *) &__c, sizeof(__c));
689 | K_CONST32 '(' expression ')' {
690 uint32_t __c = cpu_to_be32((uint32_t) $3);
692 set_multi_byte((uint8_t *) &__c, sizeof(__c));
694 | K_CONST64 '(' expression ')' {
695 uint64_t __c = cpu_to_be64((uint64_t) $3);
697 set_multi_byte((uint8_t *) &__c, sizeof(__c));
702 : K_RND '(' number ')'
703 { set_rnd($3); }
706 csum
707 : K_CSUMIP '(' number delimiter number ')'
708 { set_csum16($3, $5, CSUM_IP); }
709 | K_CSUMTCP '(' number delimiter number ')'
710 { set_csum16($3, $5, CSUM_TCP); }
711 | K_CSUMUDP '(' number delimiter number ')'
712 { set_csum16($3, $5, CSUM_UDP); }
713 | K_CSUMTCP6 '(' number delimiter number ')'
714 { set_csum16($3, $5, CSUM_TCP6); }
715 | K_CSUMUDP6 '(' number delimiter number ')'
716 { set_csum16($3, $5, CSUM_UDP6); }
719 seqinc
720 : K_SEQINC '(' number delimiter number ')'
721 { set_sequential_inc($3, $5, 1); }
722 | K_SEQINC '(' number delimiter number delimiter number ')'
723 { set_sequential_inc($3, $5, $7); }
726 seqdec
727 : K_SEQDEC '(' number delimiter number ')'
728 { set_sequential_dec($3, $5, 1); }
729 | K_SEQDEC '(' number delimiter number delimiter number ')'
730 { set_sequential_dec($3, $5, $7); }
733 drnd
734 : K_DRND '(' ')'
735 { set_dynamic_rnd(); }
736 | K_DRND '(' number ')'
738 int i, max = $3;
739 for (i = 0; i < max; ++i)
740 set_dynamic_rnd();
744 dinc
745 : K_DINC '(' number delimiter number ')'
746 { set_dynamic_incdec($3, $5, 1, TYPE_INC); }
747 | K_DINC '(' number delimiter number delimiter number ')'
748 { set_dynamic_incdec($3, $5, $7, TYPE_INC); }
751 ddec
752 : K_DDEC '(' number delimiter number ')'
753 { set_dynamic_incdec($3, $5, 1, TYPE_DEC); }
754 | K_DDEC '(' number delimiter number delimiter number ')'
755 { set_dynamic_incdec($3, $5, $7, TYPE_DEC); }
758 proto
759 : eth_proto { }
760 | pause_proto { }
761 | pfc_proto { }
762 | vlan_proto { }
763 | mpls_proto { }
764 | arp_proto { }
765 | ip4_proto { }
766 | ip6_proto { }
767 | icmp4_proto { }
768 | icmpv6_proto { }
769 | udp_proto { }
770 | tcp_proto { }
771 | dns_proto { }
774 field_expr
775 : '[' skip_white number skip_white ']'
776 { field_index_validate(field_expr.field, $3, 1);
777 field_expr.type |= FIELD_EXPR_OFFSET;
778 field_expr.val.func.offset = $3;
779 field_expr.val.func.len = 1; }
780 | '[' skip_white number skip_white ':' skip_white number skip_white ']'
781 { field_index_validate(field_expr.field, $3, $7);
782 field_expr.type |= FIELD_EXPR_OFFSET;
783 field_expr.val.func.offset = $3;
784 field_expr.val.func.len = $7; }
787 field_value_expr
788 : number { field_expr.type |= FIELD_EXPR_NUMB;
789 field_expr.val.number = $1; }
790 | mac { field_expr.type |= FIELD_EXPR_MAC;
791 memcpy(field_expr.val.mac, $1, sizeof(field_expr.val.mac)); }
792 | string { field_expr.type |= FIELD_EXPR_STRING;
793 field_expr.val.str = xstrdup($1 + 1);
794 field_expr.val.str[strlen($1 + 1) - 1] = '\0'; }
795 | ip4_addr { field_expr.type |= FIELD_EXPR_IP4_ADDR;
796 field_expr.val.ip4_addr = $1; }
797 | ip6_addr { field_expr.type |= FIELD_EXPR_IP6_ADDR;
798 field_expr.val.ip6_addr = $1; }
799 | K_DINC '(' ')' { field_expr.type |= FIELD_EXPR_INC;
800 field_expr.val.func.type = PROTO_FIELD_FUNC_INC;
801 field_expr.val.func.inc = 1; }
802 | K_DINC '(' number ')'
803 { field_expr.type |= FIELD_EXPR_INC;
804 field_expr.val.func.type = PROTO_FIELD_FUNC_INC;
805 field_expr.val.func.inc = $3; }
806 | K_DINC '(' number delimiter number ')'
807 { field_expr.type |= FIELD_EXPR_INC;
808 field_expr.val.func.type = PROTO_FIELD_FUNC_INC;
809 field_expr.val.func.type |= PROTO_FIELD_FUNC_MIN;
810 field_expr.val.func.min = $3;
811 field_expr.val.func.max = $5;
812 field_expr.val.func.inc = 1; }
813 | K_DINC '(' number delimiter number delimiter number ')'
814 { field_expr.type |= FIELD_EXPR_INC;
815 field_expr.val.func.type = PROTO_FIELD_FUNC_INC;
816 field_expr.val.func.type |= PROTO_FIELD_FUNC_MIN;
817 field_expr.val.func.min = $3;
818 field_expr.val.func.max = $5;
819 field_expr.val.func.inc = $7; }
820 | K_DRND '(' ')' { field_expr.type |= FIELD_EXPR_RND;
821 field_expr.val.func.type = PROTO_FIELD_FUNC_RND; }
822 | K_DRND '(' number delimiter number ')'
823 { field_expr.type |= FIELD_EXPR_RND;
824 field_expr.val.func.type = PROTO_FIELD_FUNC_RND;
825 field_expr.val.func.min = $3;
826 field_expr.val.func.max = $5; }
829 eth_proto
830 : eth '(' eth_param_list ')' { }
834 : K_ETH { proto_add(PROTO_ETH); }
837 eth_param_list
838 : { }
839 | eth_expr { }
840 | eth_expr delimiter eth_param_list { }
843 eth_type
844 : K_ETYPE { }
845 | K_TYPE { }
846 | K_PROT { }
849 eth_field
850 : K_DADDR { proto_field_set(ETH_DST_ADDR); }
851 | K_SADDR { proto_field_set(ETH_SRC_ADDR); }
852 | eth_type { proto_field_set(ETH_TYPE); }
854 eth_expr
855 : eth_field field_expr skip_white '=' skip_white field_value_expr
856 { proto_field_expr_eval(); }
857 | eth_field skip_white '=' skip_white field_value_expr
858 { proto_field_expr_eval(); }
861 pause_proto
862 : pause '(' pause_param_list ')' { }
865 pause
866 : K_PAUSE { proto_add(PROTO_PAUSE); }
869 pause_param_list
870 : { }
871 | pause_expr { }
872 | pause_expr delimiter pause_param_list { }
875 pause_field
876 : K_CODE { proto_field_set(PAUSE_OPCODE); }
877 | K_TIME { proto_field_set(PAUSE_TIME); }
880 pause_expr
881 : pause_field field_expr skip_white '=' skip_white field_value_expr
882 { proto_field_expr_eval(); }
883 | pause_field skip_white '=' skip_white field_value_expr
884 { proto_field_expr_eval(); }
887 pfc_proto
888 : pfc '(' pfc_param_list ')' { }
892 : K_PFC { proto_add(PROTO_PFC); }
895 pfc_param_list
896 : { }
897 | pfc_expr { }
898 | pfc_expr delimiter pfc_param_list { }
901 pfc_field
902 : K_CODE { proto_field_set(PFC_OPCODE); }
903 | K_PRIO { proto_field_set(PFC_PRIO); }
904 | K_PRIO '(' number ')'
905 { if ($3 > 7) {
906 yyerror("pfc: Invalid prio(index) parameter");
907 panic("pfc: prio(0)..prio(7) is allowed only\n");
909 proto_field_set(PFC_PRIO_0 + $3); }
910 | K_TIME '(' number ')'
911 { if ($3 > 7) {
912 yyerror("pfc: Invalid time(index) parameter");
913 panic("pfc: time(0)..time(7) is allowed only\n");
915 proto_field_set(PFC_TIME_0 + $3); }
918 pfc_expr
919 : pfc_field field_expr skip_white '=' skip_white field_value_expr
920 { proto_field_expr_eval(); }
921 | pfc_field skip_white '=' skip_white field_value_expr
922 { proto_field_expr_eval(); }
925 vlan_proto
926 : vlan '(' vlan_param_list ')' { }
929 vlan
930 : K_VLAN { proto_add(PROTO_VLAN); }
933 vlan_param_list
934 : { }
935 | vlan_expr { }
936 | vlan_expr delimiter vlan_param_list { }
939 vlan_type
940 : K_TPID { }
941 | K_PROT
944 vlan_field
945 : vlan_type { proto_field_set(VLAN_TPID); }
946 | K_TCI { proto_field_set(VLAN_TCI); }
947 | K_PCP { proto_field_set(VLAN_PCP); }
948 | K_DEI { proto_field_set(VLAN_DEI); }
949 | K_ID { proto_field_set(VLAN_VID); }
952 vlan_expr
953 : vlan_field field_expr skip_white '=' skip_white field_value_expr
954 { proto_field_expr_eval(); }
955 | vlan_field skip_white '=' skip_white field_value_expr
956 { proto_field_expr_eval(); }
957 | K_1Q
958 { proto_hdr_field_set_be16(hdr, VLAN_TPID, ETH_P_8021Q); }
959 | K_1AD
960 { proto_hdr_field_set_be16(hdr, VLAN_TPID, ETH_P_8021AD); }
963 mpls_proto
964 : mpls '(' mpls_param_list ')' { }
967 mpls
968 : K_MPLS { proto_add(PROTO_MPLS); }
971 mpls_param_list
972 : { }
973 | mpls_expr { }
974 | mpls_expr delimiter mpls_param_list { }
977 mpls_tc
978 : K_TC { }
979 | K_EXP { }
982 mpls_field
983 : K_LABEL { proto_field_set(MPLS_LABEL); }
984 | mpls_tc { proto_field_set(MPLS_TC); }
985 | K_LAST { proto_field_set(MPLS_LAST); }
986 | K_TTL { proto_field_set(MPLS_TTL); }
989 mpls_expr
990 : mpls_field field_expr skip_white '=' skip_white field_value_expr
991 { proto_field_expr_eval(); }
992 | mpls_field skip_white '=' skip_white field_value_expr
993 { proto_field_expr_eval(); }
996 arp_proto
997 : arp '(' arp_param_list ')' { }
1000 arp_param_list
1001 : { }
1002 | arp_expr { }
1003 | arp_expr delimiter arp_param_list { }
1006 arp_field
1007 : K_HTYPE
1008 { proto_field_set(ARP_HTYPE); }
1009 | K_PTYPE
1010 { proto_field_set(ARP_PTYPE); }
1011 | K_SHA
1012 { proto_field_set(ARP_SHA); }
1013 | K_THA
1014 { proto_field_set(ARP_THA); }
1015 | K_SPA
1016 { proto_field_set(ARP_SPA); }
1017 | K_TPA
1018 { proto_field_set(ARP_TPA); }
1021 arp_expr
1022 : arp_field field_expr skip_white '=' skip_white field_value_expr
1023 { proto_field_expr_eval(); }
1024 | arp_field skip_white '=' skip_white field_value_expr
1025 { proto_field_expr_eval(); }
1026 | K_OPER field_expr skip_white '=' skip_white field_value_expr
1027 { proto_field_set(ARP_OPER);
1028 proto_field_expr_eval(); }
1029 | K_OPER skip_white '=' skip_white field_value_expr
1030 { proto_field_set(ARP_OPER);
1031 proto_field_expr_eval(); }
1032 | K_OPER skip_white '=' skip_white K_REQUEST
1033 { proto_hdr_field_set_be16(hdr, ARP_OPER, ARPOP_REQUEST); }
1034 | K_OPER skip_white '=' skip_white K_REPLY
1035 { proto_hdr_field_set_be16(hdr, ARP_OPER, ARPOP_REPLY); }
1036 | K_REQUEST
1037 { proto_hdr_field_set_be16(hdr, ARP_OPER, ARPOP_REQUEST); }
1038 | K_REPLY
1039 { proto_hdr_field_set_be16(hdr, ARP_OPER, ARPOP_REPLY); }
1043 : K_ARP { proto_add(PROTO_ARP); }
1046 ip4_proto
1047 : ip4 '(' ip4_param_list ')' { }
1050 ip4_param_list
1051 : { }
1052 | ip4_expr { }
1053 | ip4_expr delimiter ip4_param_list { }
1056 ip4_field
1057 : K_VER { proto_field_set(IP4_VER); }
1058 | K_IHL { proto_field_set(IP4_IHL); }
1059 | K_DADDR { proto_field_set(IP4_DADDR); }
1060 | K_SADDR { proto_field_set(IP4_SADDR); }
1061 | K_PROT { proto_field_set(IP4_PROTO); }
1062 | K_TTL { proto_field_set(IP4_TTL); }
1063 | K_DSCP { proto_field_set(IP4_DSCP); }
1064 | K_ECN { proto_field_set(IP4_ECN); }
1065 | K_TOS { proto_field_set(IP4_TOS); }
1066 | K_LEN { proto_field_set(IP4_LEN); }
1067 | K_ID { proto_field_set(IP4_ID); }
1068 | K_FLAGS { proto_field_set(IP4_FLAGS); }
1069 | K_FRAG { proto_field_set(IP4_FRAG_OFFS); }
1070 | K_CSUM { proto_field_set(IP4_CSUM); }
1073 ip4_expr
1074 : ip4_field field_expr skip_white '=' skip_white field_value_expr
1075 { proto_field_expr_eval(); }
1076 | ip4_field skip_white '=' skip_white field_value_expr
1077 { proto_field_expr_eval(); }
1078 | K_DF { proto_hdr_field_set_be16(hdr, IP4_DF, 1); }
1079 | K_MF { proto_hdr_field_set_be16(hdr, IP4_MF, 1); }
1083 : K_IP4 { proto_add(PROTO_IP4); }
1086 ip6_proto
1087 : ip6 '(' ip6_param_list ')' { }
1090 ip6_param_list
1091 : { }
1092 | ip6_expr { }
1093 | ip6_expr delimiter ip6_param_list { }
1096 ip6_hop_limit
1097 : K_HOP_LIMIT { }
1098 | K_TTL { }
1101 ip6_field
1102 : K_VER { proto_field_set(IP6_VER); }
1103 | K_TC { proto_field_set(IP6_CLASS); }
1104 | K_FLOW { proto_field_set(IP6_FLOW_LBL); }
1105 | K_LEN { proto_field_set(IP6_LEN); }
1106 | K_NEXT_HDR { proto_field_set(IP6_NEXT_HDR); }
1107 | ip6_hop_limit { proto_field_set(IP6_HOP_LIMIT); }
1108 | K_SADDR { proto_field_set(IP6_SADDR); }
1109 | K_DADDR { proto_field_set(IP6_DADDR) ; }
1112 ip6_expr
1113 : ip6_field field_expr skip_white '=' skip_white field_value_expr
1114 { proto_field_expr_eval(); }
1115 | ip6_field skip_white '=' skip_white field_value_expr
1116 { proto_field_expr_eval(); }
1120 : K_IP6 { proto_add(PROTO_IP6); }
1123 icmp4_proto
1124 : icmp4 '(' icmp4_param_list ')' { }
1127 icmp4_param_list
1128 : { }
1129 | icmp4_expr { }
1130 | icmp4_expr delimiter icmp4_param_list { }
1133 icmp4_field
1134 : K_TYPE { proto_field_set(ICMPV4_TYPE); }
1135 | K_CODE { proto_field_set(ICMPV4_CODE); }
1136 | K_ID { proto_field_set(ICMPV4_ID); }
1137 | K_SEQ { proto_field_set(ICMPV4_SEQ); }
1138 | K_MTU { proto_field_set(ICMPV4_MTU); }
1139 | K_ADDR { proto_field_set(ICMPV4_REDIR_ADDR); }
1142 icmp4_expr
1143 : icmp4_field field_expr skip_white '=' skip_white field_value_expr
1144 { proto_field_expr_eval(); }
1145 | icmp4_field skip_white '=' skip_white field_value_expr
1146 { proto_field_expr_eval(); }
1147 | K_ECHO_REQUEST
1148 { proto_hdr_field_set_u8(hdr, ICMPV4_TYPE, ICMP_ECHO);
1149 proto_hdr_field_set_u8(hdr, ICMPV4_CODE, 0); }
1150 | K_ECHO_REPLY
1151 { proto_hdr_field_set_u8(hdr, ICMPV4_TYPE, ICMP_ECHOREPLY);
1152 proto_hdr_field_set_u8(hdr, ICMPV4_CODE, 0); }
1155 icmp4
1156 : K_ICMP4 { proto_add(PROTO_ICMP4); }
1159 icmpv6_proto
1160 : icmp6 '(' icmp6_param_list ')' { }
1163 icmp6_param_list
1164 : { }
1165 | icmp6_expr { }
1166 | icmp6_expr delimiter icmp6_param_list { }
1169 icmp6_field
1170 : K_CODE { proto_field_set(ICMPV6_CODE); }
1171 | K_CSUM { proto_field_set(ICMPV6_CSUM); }
1174 icmp6_expr
1175 : icmp6_field field_expr skip_white '=' skip_white field_value_expr
1176 { proto_field_expr_eval(); }
1177 | icmp6_field skip_white '=' skip_white field_value_expr
1178 { proto_field_expr_eval(); }
1179 | K_TYPE field_expr skip_white '=' skip_white field_value_expr
1180 { proto_field_set(ICMPV6_TYPE);
1181 proto_field_expr_eval(); }
1182 | K_TYPE skip_white '=' skip_white field_value_expr
1183 { proto_field_set(ICMPV6_TYPE);
1184 proto_field_expr_eval(); }
1185 | K_TYPE skip_white '=' K_ECHO_REQUEST
1186 { proto_hdr_field_set_u8(hdr, ICMPV6_TYPE, ICMPV6_ECHO_REQUEST); }
1187 | K_ECHO_REQUEST
1188 { proto_hdr_field_set_u8(hdr, ICMPV6_TYPE, ICMPV6_ECHO_REQUEST); }
1189 | K_TYPE skip_white '=' K_ECHO_REPLY
1190 { proto_hdr_field_set_u8(hdr, ICMPV6_TYPE, ICMPV6_ECHO_REPLY); }
1191 | K_ECHO_REPLY
1192 { proto_hdr_field_set_u8(hdr, ICMPV6_TYPE, ICMPV6_ECHO_REPLY); }
1194 icmp6
1195 : K_ICMP6 { proto_add(PROTO_ICMP6); }
1198 udp_proto
1199 : udp '(' udp_param_list ')' { }
1202 udp_param_list
1203 : { }
1204 | udp_expr { }
1205 | udp_expr delimiter udp_param_list { }
1208 udp_field
1209 : K_SPORT { proto_field_set(UDP_SPORT); }
1210 | K_DPORT { proto_field_set(UDP_DPORT); }
1211 | K_LEN { proto_field_set(UDP_LEN); }
1212 | K_CSUM { proto_field_set(UDP_CSUM); }
1215 udp_expr
1216 : udp_field field_expr skip_white '=' skip_white field_value_expr
1217 { proto_field_expr_eval(); }
1218 | udp_field skip_white '=' skip_white field_value_expr
1219 { proto_field_expr_eval(); }
1223 : K_UDP { proto_add(PROTO_UDP); }
1226 tcp_proto
1227 : tcp '(' tcp_param_list ')' { }
1230 tcp_param_list
1231 : { }
1232 | tcp_expr { }
1233 | tcp_expr delimiter tcp_param_list { }
1236 tcp_field
1237 : K_SPORT { proto_field_set(TCP_SPORT); }
1238 | K_DPORT { proto_field_set(TCP_DPORT); }
1239 | K_SEQ { proto_field_set(TCP_SEQ); }
1240 | K_ACK_SEQ { proto_field_set(TCP_ACK_SEQ); }
1241 | K_DOFF { proto_field_set(TCP_DOFF); }
1242 | K_WINDOW { proto_field_set(TCP_WINDOW); }
1243 | K_CSUM { proto_field_set(TCP_CSUM); }
1244 | K_URG_PTR { proto_field_set(TCP_URG_PTR); }
1247 tcp_expr
1248 : tcp_field field_expr skip_white '=' skip_white field_value_expr
1249 { proto_field_expr_eval(); }
1250 | tcp_field skip_white '=' skip_white field_value_expr
1251 { proto_field_expr_eval(); }
1252 | K_CWR { proto_hdr_field_set_be16(hdr, TCP_CWR, 1); }
1253 | K_ECE { proto_hdr_field_set_be16(hdr, TCP_ECE, 1); }
1254 | K_URG { proto_hdr_field_set_be16(hdr, TCP_URG, 1); }
1255 | K_ACK { proto_hdr_field_set_be16(hdr, TCP_ACK, 1); }
1256 | K_PSH { proto_hdr_field_set_be16(hdr, TCP_PSH, 1); }
1257 | K_RST { proto_hdr_field_set_be16(hdr, TCP_RST, 1); }
1258 | K_SYN { proto_hdr_field_set_be16(hdr, TCP_SYN, 1); }
1259 | K_FIN { proto_hdr_field_set_be16(hdr, TCP_FIN, 1); }
1263 : K_TCP { proto_add(PROTO_TCP); }
1266 dns_proto
1267 : dns '(' dns_param_list ')' { }
1270 dns_param_list
1271 : { }
1272 | dns_expr { }
1273 | dns_expr delimiter dns_param_list { }
1276 dns_field
1277 : K_ID { proto_field_set(DNS_ID); }
1278 | K_QR { proto_field_set(DNS_QR); }
1279 | K_OPER { proto_field_set(DNS_OPCODE); }
1280 | K_AANSWER { proto_field_set(DNS_AA); }
1281 | K_TRUNC { proto_field_set(DNS_TC); }
1282 | K_RDESIRED { proto_field_set(DNS_RD); }
1283 | K_RAVAIL { proto_field_set(DNS_RA); }
1284 | K_ZERO { proto_field_set(DNS_ZERO); }
1285 | K_RCODE { proto_field_set(DNS_RCODE); }
1286 | K_QDCOUNT { proto_field_set(DNS_QD_COUNT); }
1287 | K_ANCOUNT { proto_field_set(DNS_AN_COUNT); }
1288 | K_NSCOUNT { proto_field_set(DNS_NS_COUNT); }
1289 | K_ARCOUNT { proto_field_set(DNS_AR_COUNT); }
1292 dns_query
1293 : K_QUERY { proto_push_sub_hdr(DNS_QUERY_HDR); }
1296 dns_query_name
1297 : K_NAME { proto_field_set(DNS_QUERY_NAME); }
1300 dns_query_field
1301 : K_TYPE { proto_field_set(DNS_QUERY_TYPE); }
1302 | K_CLASS { proto_field_set(DNS_QUERY_CLASS); }
1305 dns_query_expr
1306 : dns_query_field field_expr skip_white '=' skip_white field_value_expr
1307 { proto_field_expr_eval(); }
1308 | dns_query_field skip_white '=' skip_white field_value_expr
1309 { proto_field_expr_eval(); }
1310 | dns_query_name field_expr skip_white '=' skip_white field_value_expr
1311 { if (field_expr.type & FIELD_EXPR_STRING)
1312 field_expr.type = FIELD_EXPR_FQDN;
1313 proto_field_expr_eval(); }
1314 | dns_query_name skip_white '=' skip_white field_value_expr
1315 { if (field_expr.type & FIELD_EXPR_STRING)
1316 field_expr.type = FIELD_EXPR_FQDN;
1317 proto_field_expr_eval(); }
1320 dns_query_param_list
1321 : { }
1322 | dns_query_expr { }
1323 | dns_query_expr delimiter dns_query_param_list { }
1326 dns_query_hdr
1327 : dns_query '(' dns_query_param_list ')' { }
1330 dns_rrecord
1331 : K_ANSWER { proto_push_sub_hdr(DNS_ANSWER_HDR); }
1332 | K_AUTH { proto_push_sub_hdr(DNS_AUTH_HDR); }
1333 | K_ADD { proto_push_sub_hdr(DNS_ADD_HDR); }
1336 dns_rrecord_name
1337 : K_NAME { proto_field_set(DNS_RRECORD_NAME); }
1340 dns_rrecord_data_addr
1341 : ip4_addr
1342 { proto_hdr_field_set_u32(hdr, DNS_RRECORD_DATA, $1.s_addr);
1343 proto_hdr_field_set_be16(hdr, DNS_RRECORD_TYPE, 1); }
1344 | ip6_addr
1345 { proto_hdr_field_set_bytes(hdr, DNS_RRECORD_DATA, (uint8_t *)&$1.s6_addr, 16);
1346 proto_hdr_field_set_be16(hdr, DNS_RRECORD_TYPE, 28); }
1349 dns_rrecord_data_fqdn
1350 : string
1351 { char *str = xstrdup($1 + 1);
1352 char *fqdn;
1353 str[strlen($1 + 1) - 1] = '\0';
1354 fqdn = str2fqdn(str);
1355 proto_hdr_field_set_bytes(hdr, DNS_RRECORD_DATA, (uint8_t *) fqdn, strlen(fqdn) + 1);
1356 xfree(fqdn); }
1359 dns_rrecord_data_expr
1360 : K_ADDR '(' skip_white dns_rrecord_data_addr skip_white ')'
1362 | K_NS '(' skip_white dns_rrecord_data_fqdn skip_white ')'
1363 { proto_hdr_field_set_be16(hdr, DNS_RRECORD_TYPE, 2); }
1364 | K_CNAME '(' skip_white dns_rrecord_data_fqdn skip_white ')'
1365 { proto_hdr_field_set_be16(hdr, DNS_RRECORD_TYPE, 5); }
1366 | K_PTR '(' skip_white dns_rrecord_data_fqdn skip_white ')'
1367 { proto_hdr_field_set_be16(hdr, DNS_RRECORD_TYPE, 12); }
1370 dns_rrecord_field
1371 : K_TYPE { proto_field_set(DNS_RRECORD_TYPE); }
1372 | K_CLASS { proto_field_set(DNS_RRECORD_CLASS); }
1373 | K_TTL { proto_field_set(DNS_RRECORD_TTL); }
1374 | K_LEN { proto_field_set(DNS_RRECORD_LEN); }
1375 | K_DATA { proto_field_set(DNS_RRECORD_DATA); }
1378 dns_rrecord_expr
1379 : dns_rrecord_field field_expr skip_white '=' skip_white field_value_expr
1380 { proto_field_expr_eval(); }
1381 | dns_rrecord_field skip_white '=' skip_white field_value_expr
1382 { proto_field_expr_eval(); }
1383 | dns_rrecord_name field_expr skip_white '=' skip_white field_value_expr
1384 { if (field_expr.type & FIELD_EXPR_STRING)
1385 field_expr.type = FIELD_EXPR_FQDN;
1386 proto_field_expr_eval(); }
1387 | dns_rrecord_name skip_white '=' skip_white field_value_expr
1388 { if (field_expr.type & FIELD_EXPR_STRING)
1389 field_expr.type = FIELD_EXPR_FQDN;
1390 proto_field_expr_eval(); }
1391 | dns_rrecord_data_expr
1395 dns_rrecord_param_list
1396 : { }
1397 | dns_rrecord_expr { }
1398 | dns_rrecord_expr delimiter dns_rrecord_param_list { }
1401 dns_rrecord_hdr
1402 : dns_rrecord '(' dns_rrecord_param_list ')' { }
1405 dns_expr
1406 : dns_field field_expr skip_white '=' skip_white field_value_expr
1407 { proto_field_expr_eval(); }
1408 | dns_field skip_white '=' skip_white field_value_expr
1409 { proto_field_expr_eval(); }
1410 | dns_query_hdr { proto_pop_sub_hdr(); }
1411 | dns_rrecord_hdr { proto_pop_sub_hdr(); }
1415 : K_DNS { proto_add(PROTO_DNS); }
1419 static void finalize_packet(void)
1421 /* XXX hack ... we allocated one packet pointer too much */
1422 plen--;
1423 dlen--;
1426 static void dump_conf(void)
1428 size_t i, j;
1430 for (i = 0; i < plen; ++i) {
1431 printf("[%zu] pkt\n", i);
1432 printf(" len %zu cnts %zu rnds %zu\n",
1433 packets[i].len,
1434 packet_dyn[i].clen,
1435 packet_dyn[i].rlen);
1437 printf(" payload ");
1438 for (j = 0; j < packets[i].len; ++j)
1439 printf("%02x ", packets[i].payload[j]);
1440 printf("\n");
1442 for (j = 0; j < packet_dyn[i].clen; ++j)
1443 printf(" cnt%zu [%u,%u], inc %u, off %jd type %s\n", j,
1444 packet_dyn[i].cnt[j].min,
1445 packet_dyn[i].cnt[j].max,
1446 packet_dyn[i].cnt[j].inc,
1447 (intmax_t)packet_dyn[i].cnt[j].off,
1448 packet_dyn[i].cnt[j].type == TYPE_INC ?
1449 "inc" : "dec");
1451 for (j = 0; j < packet_dyn[i].rlen; ++j)
1452 printf(" rnd%zu off %jd\n", j,
1453 (intmax_t)packet_dyn[i].rnd[j].off);
1457 void cleanup_packets(void)
1459 size_t i, j;
1461 for (i = 0; i < plen; ++i) {
1462 struct packet *pkt = &packets[i];
1464 if (pkt->len > 0)
1465 xfree(pkt->payload);
1467 for (j = 0; j < pkt->headers_count; j++) {
1468 struct proto_hdr *hdr = pkt->headers[j];
1469 int k;
1471 for (k = 0; k < hdr->sub_headers_count; k++)
1472 xfree(hdr->sub_headers[k]);
1474 if (hdr->sub_headers)
1475 xfree(hdr->sub_headers);
1477 if (hdr->fields)
1478 xfree(hdr->fields);
1480 xfree(hdr);
1484 free(packets);
1486 for (i = 0; i < dlen; ++i) {
1487 free(packet_dyn[i].cnt);
1488 free(packet_dyn[i].rnd);
1490 for (j = 0; j < packet_dyn[j].flen; j++)
1491 xfree(packet_dyn[i].fields[j]);
1493 free(packet_dyn[i].fields);
1496 free(packet_dyn);
1499 void compile_packets(char *file, bool verbose, unsigned int cpu,
1500 bool invoke_cpp, char *const cpp_argv[])
1502 char tmp_file[128];
1503 int ret = -1;
1505 if (access(file, R_OK)) {
1506 fprintf(stderr, "Cannot access %s: %s!\n", file, strerror(errno));
1507 die();
1510 memset(tmp_file, 0, sizeof(tmp_file));
1511 our_cpu = cpu;
1513 if (invoke_cpp) {
1514 if (cpp_exec(file, tmp_file, sizeof(tmp_file), cpp_argv)) {
1515 fprintf(stderr, "Failed to invoke C preprocessor!\n");
1516 goto err;
1518 file = tmp_file;
1521 if (!strncmp("-", file, strlen("-")))
1522 yyin = stdin;
1523 else
1524 yyin = fopen(file, "r");
1525 if (!yyin) {
1526 fprintf(stderr, "Cannot open %s: %s!\n", file, strerror(errno));
1527 goto err;
1530 realloc_packet();
1531 if (yyparse() != 0)
1532 goto err;
1533 finalize_packet();
1535 if (our_cpu == 0 && verbose)
1536 dump_conf();
1538 ret = 0;
1539 err:
1540 if (yyin && yyin != stdin)
1541 fclose(yyin);
1543 if (invoke_cpp)
1544 unlink(tmp_file);
1545 if (ret)
1546 die();
1549 void compile_packets_str(char *str, bool verbose, unsigned int cpu)
1551 int ret = 1;
1553 our_cpu = cpu;
1554 realloc_packet();
1556 yy_scan_string(str);
1557 if (yyparse() != 0)
1558 goto err;
1560 finalize_packet();
1561 if (our_cpu == 0 && verbose)
1562 dump_conf();
1564 ret = 0;
1565 err:
1566 yylex_destroy();
1568 if (ret)
1569 die();
1572 void yyerror(const char *err)
1574 fprintf(stderr, "Syntax error at line %d, char '%s': %s\n", yylineno, yytext, err);