astraceroute: use switch instead of lookup table for short proto id
[netsniff-ng.git] / trafgen_parser.y
bloba42dc30570bbb0b1e0ab8bc123cad6321fb41714
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 struct packet *realloc_packet(void)
172 uint32_t i;
174 if (test_ignore())
175 return NULL;
177 plen++;
178 packets = xrealloc(packets, plen * sizeof(*packets));
180 __init_new_packet_slot(&packets[packet_last]);
182 dlen++;
183 packet_dyn = xrealloc(packet_dyn, dlen * sizeof(*packet_dyn));
185 __init_new_counter_slot(&packet_dyn[packetd_last]);
186 __init_new_randomizer_slot(&packet_dyn[packetd_last]);
187 __init_new_csum_slot(&packet_dyn[packetd_last]);
188 __init_new_fields_slot(&packet_dyn[packetd_last]);
190 for (i = 0; i < plen; i++)
191 packets[i].id = i;
193 return &packets[packet_last];
196 struct packet *current_packet(void)
198 return &packets[packet_last];
201 uint32_t current_packet_id(void)
203 return packet_last;
206 struct packet *packet_get(uint32_t id)
208 return &packets[id];
211 static void set_byte(uint8_t val)
213 struct packet *pkt = &packets[packet_last];
215 if (test_ignore())
216 return;
218 pkt->len++;
219 pkt->payload = xrealloc(pkt->payload, pkt->len);
220 pkt->payload[payload_last] = val;
223 static void set_multi_byte(uint8_t *s, size_t len)
225 size_t i;
227 for (i = 0; i < len; ++i)
228 set_byte(s[i]);
231 void set_fill(uint8_t val, size_t len)
233 size_t i;
234 struct packet *pkt = &packets[packet_last];
236 if (test_ignore())
237 return;
239 pkt->len += len;
240 pkt->payload = xrealloc(pkt->payload, pkt->len);
241 for (i = 0; i < len; ++i)
242 pkt->payload[payload_last - i] = val;
245 static void __set_csum16_dynamic(size_t from, size_t to, enum csum which)
247 struct packet *pkt = &packets[packet_last];
248 struct packet_dyn *pktd = &packet_dyn[packetd_last];
250 pkt->len += 2;
251 pkt->payload = xrealloc(pkt->payload, pkt->len);
253 pktd->slen++;
254 pktd->csum = xrealloc(pktd->csum, pktd->slen * sizeof(struct csum16));
256 __setup_new_csum16(&pktd->csum[packetds_last], from, to, which);
259 static void __set_csum16_static(size_t from, size_t to, enum csum which __maybe_unused)
261 struct packet *pkt = &packets[packet_last];
262 uint16_t sum;
263 uint8_t *psum;
265 sum = htons(calc_csum(pkt->payload + from, to - from));
266 psum = (uint8_t *) &sum;
268 set_byte(psum[0]);
269 set_byte(psum[1]);
272 static inline bool is_dynamic_csum(enum csum which)
274 switch (which) {
275 case CSUM_UDP:
276 case CSUM_TCP:
277 case CSUM_UDP6:
278 case CSUM_TCP6:
279 return true;
280 default:
281 return false;
285 static void set_csum16(size_t from, size_t to, enum csum which)
287 struct packet *pkt = &packets[packet_last];
288 struct packet_dyn *pktd = &packet_dyn[packetd_last];
290 if (test_ignore())
291 return;
293 if (to < from) {
294 size_t tmp = to;
296 to = from;
297 from = tmp;
300 bug_on(!(from < to));
302 if (packet_dyn_has_elems(pktd) || to >= pkt->len || is_dynamic_csum(which))
303 __set_csum16_dynamic(from, to, which);
304 else
305 __set_csum16_static(from, to, which);
308 static void set_rnd(size_t len)
310 size_t i;
311 struct packet *pkt = &packets[packet_last];
313 if (test_ignore())
314 return;
316 pkt->len += len;
317 pkt->payload = xrealloc(pkt->payload, pkt->len);
318 for (i = 0; i < len; ++i)
319 pkt->payload[payload_last - i] = (uint8_t) rand();
322 static void set_sequential_inc(uint8_t start, size_t len, uint8_t stepping)
324 size_t i;
325 struct packet *pkt = &packets[packet_last];
327 if (test_ignore())
328 return;
330 pkt->len += len;
331 pkt->payload = xrealloc(pkt->payload, pkt->len);
332 for (i = 0; i < len; ++i) {
333 off_t off = len - 1 - i;
335 pkt->payload[payload_last - off] = start;
336 start += stepping;
340 static void set_sequential_dec(uint8_t start, size_t len, uint8_t stepping)
342 size_t i;
343 struct packet *pkt = &packets[packet_last];
345 if (test_ignore())
346 return;
348 pkt->len += len;
349 pkt->payload = xrealloc(pkt->payload, pkt->len);
350 for (i = 0; i < len; ++i) {
351 int off = len - 1 - i;
353 pkt->payload[payload_last - off] = start;
354 start -= stepping;
358 static void set_dynamic_rnd(void)
360 struct packet *pkt = &packets[packet_last];
361 struct packet_dyn *pktd = &packet_dyn[packetd_last];
363 if (test_ignore())
364 return;
366 pkt->len++;
367 pkt->payload = xrealloc(pkt->payload, pkt->len);
369 pktd->rlen++;
370 pktd->rnd = xrealloc(pktd->rnd, pktd->rlen * sizeof(struct randomizer));
372 __setup_new_randomizer(&pktd->rnd[packetdr_last]);
375 static void set_dynamic_incdec(uint8_t start, uint8_t stop, uint8_t stepping,
376 int type)
378 struct packet *pkt = &packets[packet_last];
379 struct packet_dyn *pktd = &packet_dyn[packetd_last];
381 if (test_ignore())
382 return;
384 pkt->len++;
385 pkt->payload = xrealloc(pkt->payload, pkt->len);
387 pktd->clen++;
388 pktd->cnt = xrealloc(pktd->cnt, pktd->clen * sizeof(struct counter));
390 __setup_new_counter(&pktd->cnt[packetdc_last], start, stop, stepping, type);
393 static void proto_add(enum proto_id pid)
395 hdr = proto_header_push(pid);
398 static void proto_field_set(uint32_t fid)
400 memset(&field_expr, 0, sizeof(field_expr));
401 field_expr.field = proto_hdr_field_by_id(hdr, fid);
404 static void proto_field_func_setup(struct proto_field *field, struct proto_field_func *func)
406 struct proto_field *field_copy;
407 struct packet_dyn *pkt_dyn;
409 field_copy = xmalloc(sizeof(*field));
410 memcpy(field_copy, field, sizeof(*field));
412 field_copy->pkt_offset += func->offset;
413 if (func->len)
414 field_copy->len = func->len;
416 proto_field_func_add(field_copy, func);
418 pkt_dyn = &packet_dyn[packetd_last];
419 pkt_dyn->flen++;
420 pkt_dyn->fields = xrealloc(pkt_dyn->fields, pkt_dyn->flen *
421 sizeof(struct proto_field *));
423 pkt_dyn->fields[pkt_dyn->flen - 1] = field_copy;
426 static void proto_field_expr_eval(void)
428 struct proto_field *field = field_expr.field;
430 if ((field_expr.type & FIELD_EXPR_OFFSET) &&
431 !((field_expr.type & FIELD_EXPR_INC) ||
432 (field_expr.type & FIELD_EXPR_RND))) {
434 panic("Field offset expression is valid only with function expression\n");
437 if (field_expr.type & FIELD_EXPR_NUMB) {
438 if (field->len == 1)
439 proto_field_set_u8(field, field_expr.val.number);
440 else if (field->len == 2)
441 proto_field_set_be16(field, field_expr.val.number);
442 else if (field->len == 4)
443 proto_field_set_be32(field, field_expr.val.number);
444 else
445 panic("Invalid value length %zu, can be 1,2 or 4\n", field->len);
446 } else if (field_expr.type & FIELD_EXPR_MAC) {
447 proto_field_set_bytes(field, field_expr.val.mac, 6);
448 } else if (field_expr.type & FIELD_EXPR_FQDN) {
449 char *fqdn = str2fqdn(field_expr.val.str);
450 proto_field_set_bytes(field, (uint8_t *) fqdn, strlen(fqdn) + 1);
451 xfree(field_expr.val.str);
452 xfree(fqdn);
453 } else if (field_expr.type & FIELD_EXPR_STRING) {
454 proto_field_set_string(field, field_expr.val.str);
455 xfree(field_expr.val.str);
456 } else if (field_expr.type & FIELD_EXPR_IP4_ADDR) {
457 proto_field_set_u32(field, field_expr.val.ip4_addr.s_addr);
458 } else if (field_expr.type & FIELD_EXPR_IP6_ADDR) {
459 proto_field_set_bytes(field, (uint8_t *)&field_expr.val.ip6_addr.s6_addr, 16);
460 } else if ((field_expr.type & FIELD_EXPR_INC) ||
461 (field_expr.type & FIELD_EXPR_RND)) {
463 if (field_expr.val.func.min
464 && field_expr.val.func.min >= field_expr.val.func.max)
465 panic("dinc(): min(%u) can't be >= max(%u)\n",
466 field_expr.val.func.min, field_expr.val.func.max);
468 proto_field_func_setup(field, &field_expr.val.func);
469 } else if ((field_expr.type & FIELD_EXPR_OFFSET) &&
470 !((field_expr.type & FIELD_EXPR_INC) ||
471 (field_expr.type & FIELD_EXPR_RND))) {
473 panic("Field expression is valid only for function value expression\n");
474 } else {
475 bug();
478 memset(&field_expr, 0, sizeof(field_expr));
481 static void field_index_validate(struct proto_field *field, uint16_t index, size_t len)
483 if (field_expr.field->len <= index) {
484 yyerror("Invalid [index] parameter");
485 panic("Index (%u) is bigger than field's length (%zu)\n",
486 index, field->len);
488 if (len != 1 && len != 2 && len != 4) {
489 yyerror("Invalid [index:len] parameter");
490 panic("Invalid index length - 1,2 or 4 is only allowed\n");
494 static void proto_push_sub_hdr(uint32_t id)
496 hdr = proto_hdr_push_sub_header(hdr, id);
499 static void proto_pop_sub_hdr(void)
501 if (hdr->ops->header_finish)
502 hdr->ops->header_finish(hdr);
504 hdr = hdr->parent;
509 %union {
510 struct in_addr ip4_addr;
511 struct in6_addr ip6_addr;
512 long long int number;
513 uint8_t mac[6];
514 char *str;
517 %token K_COMMENT K_FILL K_RND K_SEQINC K_SEQDEC K_DRND K_DINC K_DDEC K_WHITE
518 %token K_CPU K_CSUMIP K_CSUMUDP K_CSUMTCP K_CSUMUDP6 K_CSUMTCP6 K_CONST8 K_CONST16 K_CONST32 K_CONST64
520 %token K_DADDR K_SADDR K_ETYPE K_TYPE
521 %token K_TIME K_PRIO
522 %token K_OPER K_SHA K_SPA K_THA K_TPA K_REQUEST K_REPLY K_PTYPE K_HTYPE
523 %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
524 %token K_FLOW K_NEXT_HDR K_HOP_LIMIT
525 %token K_CODE K_ECHO_REQUEST K_ECHO_REPLY
526 %token K_SPORT K_DPORT
527 %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
528 %token K_TPID K_TCI K_PCP K_DEI K_1Q K_1AD
529 %token K_LABEL K_TC K_LAST K_EXP
531 %token K_ADDR K_MTU
533 %token K_QR K_AANSWER K_TRUNC K_RAVAIL K_RDESIRED K_ZERO K_RCODE K_QDCOUNT K_ANCOUNT K_NSCOUNT K_ARCOUNT
534 %token K_QUERY K_ANSWER K_AUTH K_ADD
535 %token K_NAME K_CLASS K_DATA K_NS K_CNAME K_PTR
537 %token K_ETH
538 %token K_PAUSE
539 %token K_PFC
540 %token K_VLAN K_MPLS
541 %token K_ARP
542 %token K_IP4 K_IP6
543 %token K_ICMP4 K_ICMP6
544 %token K_UDP K_TCP
545 %token K_DNS
547 %token ',' '{' '}' '(' ')' '[' ']' ':' '-' '+' '*' '/' '%' '&' '|' '<' '>' '^'
549 %token number string mac ip4_addr ip6_addr
551 %type <number> number expression
552 %type <str> string
553 %type <mac> mac
554 %type <ip4_addr> ip4_addr
555 %type <ip6_addr> ip6_addr
557 %left '-' '+' '*' '/' '%' '&' '|' '<' '>' '^'
561 packets
562 : { }
563 | packets packet { }
564 | packets inline_comment { }
565 | packets K_WHITE { }
568 inline_comment
569 : K_COMMENT { }
572 cpu_delim
573 : ':' { }
574 | '-' { }
577 delimiter_nowhite
578 : ',' { }
579 | ',' K_WHITE { }
582 noenforce_white
583 : { }
584 | K_WHITE { }
585 | delimiter_nowhite { }
588 skip_white
589 : { }
590 | K_WHITE { }
592 packet
593 : '{' noenforce_white payload noenforce_white '}' {
594 min_cpu = max_cpu = -1;
596 proto_packet_finish();
598 realloc_packet();
600 | K_CPU '(' number cpu_delim number ')' ':' noenforce_white '{' noenforce_white payload noenforce_white '}' {
601 min_cpu = $3;
602 max_cpu = $5;
604 if (min_cpu > max_cpu) {
605 int tmp = min_cpu;
607 min_cpu = max_cpu;
608 max_cpu = tmp;
611 proto_packet_finish();
613 realloc_packet();
615 | K_CPU '(' number ')' ':' noenforce_white '{' noenforce_white payload noenforce_white '}' {
616 min_cpu = max_cpu = $3;
618 proto_packet_finish();
620 realloc_packet();
624 payload
625 : elem { }
626 | payload elem_delimiter { }
629 delimiter
630 : delimiter_nowhite { }
631 | K_WHITE { }
634 elem_delimiter
635 : delimiter elem { }
638 elem
639 : number { set_byte((uint8_t) $1); }
640 | string { set_multi_byte((uint8_t *) $1 + 1, strlen($1) - 2); }
641 | fill { }
642 | rnd { }
643 | drnd { }
644 | seqinc { }
645 | seqdec { }
646 | dinc { }
647 | ddec { }
648 | csum { }
649 | const { }
650 | proto { proto_header_finish(hdr); }
651 | inline_comment { }
654 expression
655 : number
656 { $$ = $1; }
657 | expression '+' expression
658 { $$ = $1 + $3; }
659 | expression '-' expression
660 { $$ = $1 - $3; }
661 | expression '*' expression
662 { $$ = $1 * $3; }
663 | expression '/' expression
664 { $$ = $1 / $3; }
665 | expression '%' expression
666 { $$ = $1 % $3; }
667 | expression '&' expression
668 { $$ = $1 & $3; }
669 | expression '|' expression
670 { $$ = $1 | $3; }
671 | expression '^' expression
672 { $$ = $1 ^ $3; }
673 | expression '<' '<' expression
674 { $$ = $1 << $4; }
675 | expression '>' '>' expression
676 { $$ = $1 >> $4; }
677 | '-' expression
678 { $$ = -1 * $2; }
679 | '(' expression ')'
680 { $$ = $2;}
683 fill
684 : K_FILL '(' number delimiter number ')'
685 { set_fill($3, $5); }
688 const
689 : K_CONST8 '(' expression ')'
690 { set_byte((uint8_t) $3); }
691 | K_CONST16 '(' expression ')' {
692 uint16_t __c = cpu_to_be16((uint16_t) $3);
694 set_multi_byte((uint8_t *) &__c, sizeof(__c));
696 | K_CONST32 '(' expression ')' {
697 uint32_t __c = cpu_to_be32((uint32_t) $3);
699 set_multi_byte((uint8_t *) &__c, sizeof(__c));
701 | K_CONST64 '(' expression ')' {
702 uint64_t __c = cpu_to_be64((uint64_t) $3);
704 set_multi_byte((uint8_t *) &__c, sizeof(__c));
709 : K_RND '(' number ')'
710 { set_rnd($3); }
713 csum
714 : K_CSUMIP '(' number delimiter number ')'
715 { set_csum16($3, $5, CSUM_IP); }
716 | K_CSUMTCP '(' number delimiter number ')'
717 { set_csum16($3, $5, CSUM_TCP); }
718 | K_CSUMUDP '(' number delimiter number ')'
719 { set_csum16($3, $5, CSUM_UDP); }
720 | K_CSUMTCP6 '(' number delimiter number ')'
721 { set_csum16($3, $5, CSUM_TCP6); }
722 | K_CSUMUDP6 '(' number delimiter number ')'
723 { set_csum16($3, $5, CSUM_UDP6); }
726 seqinc
727 : K_SEQINC '(' number delimiter number ')'
728 { set_sequential_inc($3, $5, 1); }
729 | K_SEQINC '(' number delimiter number delimiter number ')'
730 { set_sequential_inc($3, $5, $7); }
733 seqdec
734 : K_SEQDEC '(' number delimiter number ')'
735 { set_sequential_dec($3, $5, 1); }
736 | K_SEQDEC '(' number delimiter number delimiter number ')'
737 { set_sequential_dec($3, $5, $7); }
740 drnd
741 : K_DRND '(' ')'
742 { set_dynamic_rnd(); }
743 | K_DRND '(' number ')'
745 int i, max = $3;
746 for (i = 0; i < max; ++i)
747 set_dynamic_rnd();
751 dinc
752 : K_DINC '(' number delimiter number ')'
753 { set_dynamic_incdec($3, $5, 1, TYPE_INC); }
754 | K_DINC '(' number delimiter number delimiter number ')'
755 { set_dynamic_incdec($3, $5, $7, TYPE_INC); }
758 ddec
759 : K_DDEC '(' number delimiter number ')'
760 { set_dynamic_incdec($3, $5, 1, TYPE_DEC); }
761 | K_DDEC '(' number delimiter number delimiter number ')'
762 { set_dynamic_incdec($3, $5, $7, TYPE_DEC); }
765 proto
766 : eth_proto { }
767 | pause_proto { }
768 | pfc_proto { }
769 | vlan_proto { }
770 | mpls_proto { }
771 | arp_proto { }
772 | ip4_proto { }
773 | ip6_proto { }
774 | icmp4_proto { }
775 | icmpv6_proto { }
776 | udp_proto { }
777 | tcp_proto { }
778 | dns_proto { }
781 field_expr
782 : '[' skip_white number skip_white ']'
783 { field_index_validate(field_expr.field, $3, 1);
784 field_expr.type |= FIELD_EXPR_OFFSET;
785 field_expr.val.func.offset = $3;
786 field_expr.val.func.len = 1; }
787 | '[' skip_white number skip_white ':' skip_white number skip_white ']'
788 { field_index_validate(field_expr.field, $3, $7);
789 field_expr.type |= FIELD_EXPR_OFFSET;
790 field_expr.val.func.offset = $3;
791 field_expr.val.func.len = $7; }
794 field_value_expr
795 : number { field_expr.type |= FIELD_EXPR_NUMB;
796 field_expr.val.number = $1; }
797 | mac { field_expr.type |= FIELD_EXPR_MAC;
798 memcpy(field_expr.val.mac, $1, sizeof(field_expr.val.mac)); }
799 | string { field_expr.type |= FIELD_EXPR_STRING;
800 field_expr.val.str = xstrdup($1 + 1);
801 field_expr.val.str[strlen($1 + 1) - 1] = '\0'; }
802 | ip4_addr { field_expr.type |= FIELD_EXPR_IP4_ADDR;
803 field_expr.val.ip4_addr = $1; }
804 | ip6_addr { field_expr.type |= FIELD_EXPR_IP6_ADDR;
805 field_expr.val.ip6_addr = $1; }
806 | K_DINC '(' ')' { field_expr.type |= FIELD_EXPR_INC;
807 field_expr.val.func.type = PROTO_FIELD_FUNC_INC;
808 field_expr.val.func.inc = 1; }
809 | K_DINC '(' number ')'
810 { field_expr.type |= FIELD_EXPR_INC;
811 field_expr.val.func.type = PROTO_FIELD_FUNC_INC;
812 field_expr.val.func.inc = $3; }
813 | K_DINC '(' 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 = 1; }
820 | K_DINC '(' number delimiter number delimiter number ')'
821 { field_expr.type |= FIELD_EXPR_INC;
822 field_expr.val.func.type = PROTO_FIELD_FUNC_INC;
823 field_expr.val.func.type |= PROTO_FIELD_FUNC_MIN;
824 field_expr.val.func.min = $3;
825 field_expr.val.func.max = $5;
826 field_expr.val.func.inc = $7; }
827 | K_DRND '(' ')' { field_expr.type |= FIELD_EXPR_RND;
828 field_expr.val.func.type = PROTO_FIELD_FUNC_RND; }
829 | K_DRND '(' number delimiter number ')'
830 { field_expr.type |= FIELD_EXPR_RND;
831 field_expr.val.func.type = PROTO_FIELD_FUNC_RND;
832 field_expr.val.func.min = $3;
833 field_expr.val.func.max = $5; }
836 eth_proto
837 : eth '(' eth_param_list ')' { }
841 : K_ETH { proto_add(PROTO_ETH); }
844 eth_param_list
845 : { }
846 | eth_expr { }
847 | eth_expr delimiter eth_param_list { }
850 eth_type
851 : K_ETYPE { }
852 | K_TYPE { }
853 | K_PROT { }
856 eth_field
857 : K_DADDR { proto_field_set(ETH_DST_ADDR); }
858 | K_SADDR { proto_field_set(ETH_SRC_ADDR); }
859 | eth_type { proto_field_set(ETH_TYPE); }
861 eth_expr
862 : eth_field field_expr skip_white '=' skip_white field_value_expr
863 { proto_field_expr_eval(); }
864 | eth_field skip_white '=' skip_white field_value_expr
865 { proto_field_expr_eval(); }
868 pause_proto
869 : pause '(' pause_param_list ')' { }
872 pause
873 : K_PAUSE { proto_add(PROTO_PAUSE); }
876 pause_param_list
877 : { }
878 | pause_expr { }
879 | pause_expr delimiter pause_param_list { }
882 pause_field
883 : K_CODE { proto_field_set(PAUSE_OPCODE); }
884 | K_TIME { proto_field_set(PAUSE_TIME); }
887 pause_expr
888 : pause_field field_expr skip_white '=' skip_white field_value_expr
889 { proto_field_expr_eval(); }
890 | pause_field skip_white '=' skip_white field_value_expr
891 { proto_field_expr_eval(); }
894 pfc_proto
895 : pfc '(' pfc_param_list ')' { }
899 : K_PFC { proto_add(PROTO_PFC); }
902 pfc_param_list
903 : { }
904 | pfc_expr { }
905 | pfc_expr delimiter pfc_param_list { }
908 pfc_field
909 : K_CODE { proto_field_set(PFC_OPCODE); }
910 | K_PRIO { proto_field_set(PFC_PRIO); }
911 | K_PRIO '(' number ')'
912 { if ($3 > 7) {
913 yyerror("pfc: Invalid prio(index) parameter");
914 panic("pfc: prio(0)..prio(7) is allowed only\n");
916 proto_field_set(PFC_PRIO_0 + $3); }
917 | K_TIME '(' number ')'
918 { if ($3 > 7) {
919 yyerror("pfc: Invalid time(index) parameter");
920 panic("pfc: time(0)..time(7) is allowed only\n");
922 proto_field_set(PFC_TIME_0 + $3); }
925 pfc_expr
926 : pfc_field field_expr skip_white '=' skip_white field_value_expr
927 { proto_field_expr_eval(); }
928 | pfc_field skip_white '=' skip_white field_value_expr
929 { proto_field_expr_eval(); }
932 vlan_proto
933 : vlan '(' vlan_param_list ')' { }
936 vlan
937 : K_VLAN { proto_add(PROTO_VLAN); }
940 vlan_param_list
941 : { }
942 | vlan_expr { }
943 | vlan_expr delimiter vlan_param_list { }
946 vlan_type
947 : K_TPID { }
948 | K_PROT
951 vlan_field
952 : vlan_type { proto_field_set(VLAN_TPID); }
953 | K_TCI { proto_field_set(VLAN_TCI); }
954 | K_PCP { proto_field_set(VLAN_PCP); }
955 | K_DEI { proto_field_set(VLAN_DEI); }
956 | K_ID { proto_field_set(VLAN_VID); }
959 vlan_expr
960 : vlan_field field_expr skip_white '=' skip_white field_value_expr
961 { proto_field_expr_eval(); }
962 | vlan_field skip_white '=' skip_white field_value_expr
963 { proto_field_expr_eval(); }
964 | K_1Q
965 { proto_hdr_field_set_be16(hdr, VLAN_TPID, ETH_P_8021Q); }
966 | K_1AD
967 { proto_hdr_field_set_be16(hdr, VLAN_TPID, ETH_P_8021AD); }
970 mpls_proto
971 : mpls '(' mpls_param_list ')' { }
974 mpls
975 : K_MPLS { proto_add(PROTO_MPLS); }
978 mpls_param_list
979 : { }
980 | mpls_expr { }
981 | mpls_expr delimiter mpls_param_list { }
984 mpls_tc
985 : K_TC { }
986 | K_EXP { }
989 mpls_field
990 : K_LABEL { proto_field_set(MPLS_LABEL); }
991 | mpls_tc { proto_field_set(MPLS_TC); }
992 | K_LAST { proto_field_set(MPLS_LAST); }
993 | K_TTL { proto_field_set(MPLS_TTL); }
996 mpls_expr
997 : mpls_field field_expr skip_white '=' skip_white field_value_expr
998 { proto_field_expr_eval(); }
999 | mpls_field skip_white '=' skip_white field_value_expr
1000 { proto_field_expr_eval(); }
1003 arp_proto
1004 : arp '(' arp_param_list ')' { }
1007 arp_param_list
1008 : { }
1009 | arp_expr { }
1010 | arp_expr delimiter arp_param_list { }
1013 arp_field
1014 : K_HTYPE
1015 { proto_field_set(ARP_HTYPE); }
1016 | K_PTYPE
1017 { proto_field_set(ARP_PTYPE); }
1018 | K_SHA
1019 { proto_field_set(ARP_SHA); }
1020 | K_THA
1021 { proto_field_set(ARP_THA); }
1022 | K_SPA
1023 { proto_field_set(ARP_SPA); }
1024 | K_TPA
1025 { proto_field_set(ARP_TPA); }
1028 arp_expr
1029 : arp_field field_expr skip_white '=' skip_white field_value_expr
1030 { proto_field_expr_eval(); }
1031 | arp_field skip_white '=' skip_white field_value_expr
1032 { proto_field_expr_eval(); }
1033 | K_OPER field_expr skip_white '=' skip_white field_value_expr
1034 { proto_field_set(ARP_OPER);
1035 proto_field_expr_eval(); }
1036 | K_OPER skip_white '=' skip_white field_value_expr
1037 { proto_field_set(ARP_OPER);
1038 proto_field_expr_eval(); }
1039 | K_OPER skip_white '=' skip_white K_REQUEST
1040 { proto_hdr_field_set_be16(hdr, ARP_OPER, ARPOP_REQUEST); }
1041 | K_OPER skip_white '=' skip_white K_REPLY
1042 { proto_hdr_field_set_be16(hdr, ARP_OPER, ARPOP_REPLY); }
1043 | K_REQUEST
1044 { proto_hdr_field_set_be16(hdr, ARP_OPER, ARPOP_REQUEST); }
1045 | K_REPLY
1046 { proto_hdr_field_set_be16(hdr, ARP_OPER, ARPOP_REPLY); }
1050 : K_ARP { proto_add(PROTO_ARP); }
1053 ip4_proto
1054 : ip4 '(' ip4_param_list ')' { }
1057 ip4_param_list
1058 : { }
1059 | ip4_expr { }
1060 | ip4_expr delimiter ip4_param_list { }
1063 ip4_field
1064 : K_VER { proto_field_set(IP4_VER); }
1065 | K_IHL { proto_field_set(IP4_IHL); }
1066 | K_DADDR { proto_field_set(IP4_DADDR); }
1067 | K_SADDR { proto_field_set(IP4_SADDR); }
1068 | K_PROT { proto_field_set(IP4_PROTO); }
1069 | K_TTL { proto_field_set(IP4_TTL); }
1070 | K_DSCP { proto_field_set(IP4_DSCP); }
1071 | K_ECN { proto_field_set(IP4_ECN); }
1072 | K_TOS { proto_field_set(IP4_TOS); }
1073 | K_LEN { proto_field_set(IP4_LEN); }
1074 | K_ID { proto_field_set(IP4_ID); }
1075 | K_FLAGS { proto_field_set(IP4_FLAGS); }
1076 | K_FRAG { proto_field_set(IP4_FRAG_OFFS); }
1077 | K_CSUM { proto_field_set(IP4_CSUM); }
1080 ip4_expr
1081 : ip4_field field_expr skip_white '=' skip_white field_value_expr
1082 { proto_field_expr_eval(); }
1083 | ip4_field skip_white '=' skip_white field_value_expr
1084 { proto_field_expr_eval(); }
1085 | K_DF { proto_hdr_field_set_be16(hdr, IP4_DF, 1); }
1086 | K_MF { proto_hdr_field_set_be16(hdr, IP4_MF, 1); }
1090 : K_IP4 { proto_add(PROTO_IP4); }
1093 ip6_proto
1094 : ip6 '(' ip6_param_list ')' { }
1097 ip6_param_list
1098 : { }
1099 | ip6_expr { }
1100 | ip6_expr delimiter ip6_param_list { }
1103 ip6_hop_limit
1104 : K_HOP_LIMIT { }
1105 | K_TTL { }
1108 ip6_field
1109 : K_VER { proto_field_set(IP6_VER); }
1110 | K_TC { proto_field_set(IP6_CLASS); }
1111 | K_FLOW { proto_field_set(IP6_FLOW_LBL); }
1112 | K_LEN { proto_field_set(IP6_LEN); }
1113 | K_NEXT_HDR { proto_field_set(IP6_NEXT_HDR); }
1114 | ip6_hop_limit { proto_field_set(IP6_HOP_LIMIT); }
1115 | K_SADDR { proto_field_set(IP6_SADDR); }
1116 | K_DADDR { proto_field_set(IP6_DADDR) ; }
1119 ip6_expr
1120 : ip6_field field_expr skip_white '=' skip_white field_value_expr
1121 { proto_field_expr_eval(); }
1122 | ip6_field skip_white '=' skip_white field_value_expr
1123 { proto_field_expr_eval(); }
1127 : K_IP6 { proto_add(PROTO_IP6); }
1130 icmp4_proto
1131 : icmp4 '(' icmp4_param_list ')' { }
1134 icmp4_param_list
1135 : { }
1136 | icmp4_expr { }
1137 | icmp4_expr delimiter icmp4_param_list { }
1140 icmp4_field
1141 : K_TYPE { proto_field_set(ICMPV4_TYPE); }
1142 | K_CODE { proto_field_set(ICMPV4_CODE); }
1143 | K_ID { proto_field_set(ICMPV4_ID); }
1144 | K_SEQ { proto_field_set(ICMPV4_SEQ); }
1145 | K_MTU { proto_field_set(ICMPV4_MTU); }
1146 | K_ADDR { proto_field_set(ICMPV4_REDIR_ADDR); }
1149 icmp4_expr
1150 : icmp4_field field_expr skip_white '=' skip_white field_value_expr
1151 { proto_field_expr_eval(); }
1152 | icmp4_field skip_white '=' skip_white field_value_expr
1153 { proto_field_expr_eval(); }
1154 | K_ECHO_REQUEST
1155 { proto_hdr_field_set_u8(hdr, ICMPV4_TYPE, ICMP_ECHO);
1156 proto_hdr_field_set_u8(hdr, ICMPV4_CODE, 0); }
1157 | K_ECHO_REPLY
1158 { proto_hdr_field_set_u8(hdr, ICMPV4_TYPE, ICMP_ECHOREPLY);
1159 proto_hdr_field_set_u8(hdr, ICMPV4_CODE, 0); }
1162 icmp4
1163 : K_ICMP4 { proto_add(PROTO_ICMP4); }
1166 icmpv6_proto
1167 : icmp6 '(' icmp6_param_list ')' { }
1170 icmp6_param_list
1171 : { }
1172 | icmp6_expr { }
1173 | icmp6_expr delimiter icmp6_param_list { }
1176 icmp6_field
1177 : K_CODE { proto_field_set(ICMPV6_CODE); }
1178 | K_CSUM { proto_field_set(ICMPV6_CSUM); }
1181 icmp6_expr
1182 : icmp6_field field_expr skip_white '=' skip_white field_value_expr
1183 { proto_field_expr_eval(); }
1184 | icmp6_field skip_white '=' skip_white field_value_expr
1185 { proto_field_expr_eval(); }
1186 | K_TYPE field_expr skip_white '=' skip_white field_value_expr
1187 { proto_field_set(ICMPV6_TYPE);
1188 proto_field_expr_eval(); }
1189 | K_TYPE skip_white '=' skip_white field_value_expr
1190 { proto_field_set(ICMPV6_TYPE);
1191 proto_field_expr_eval(); }
1192 | K_TYPE skip_white '=' K_ECHO_REQUEST
1193 { proto_hdr_field_set_u8(hdr, ICMPV6_TYPE, ICMPV6_ECHO_REQUEST); }
1194 | K_ECHO_REQUEST
1195 { proto_hdr_field_set_u8(hdr, ICMPV6_TYPE, ICMPV6_ECHO_REQUEST); }
1196 | K_TYPE skip_white '=' K_ECHO_REPLY
1197 { proto_hdr_field_set_u8(hdr, ICMPV6_TYPE, ICMPV6_ECHO_REPLY); }
1198 | K_ECHO_REPLY
1199 { proto_hdr_field_set_u8(hdr, ICMPV6_TYPE, ICMPV6_ECHO_REPLY); }
1201 icmp6
1202 : K_ICMP6 { proto_add(PROTO_ICMP6); }
1205 udp_proto
1206 : udp '(' udp_param_list ')' { }
1209 udp_param_list
1210 : { }
1211 | udp_expr { }
1212 | udp_expr delimiter udp_param_list { }
1215 udp_field
1216 : K_SPORT { proto_field_set(UDP_SPORT); }
1217 | K_DPORT { proto_field_set(UDP_DPORT); }
1218 | K_LEN { proto_field_set(UDP_LEN); }
1219 | K_CSUM { proto_field_set(UDP_CSUM); }
1222 udp_expr
1223 : udp_field field_expr skip_white '=' skip_white field_value_expr
1224 { proto_field_expr_eval(); }
1225 | udp_field skip_white '=' skip_white field_value_expr
1226 { proto_field_expr_eval(); }
1230 : K_UDP { proto_add(PROTO_UDP); }
1233 tcp_proto
1234 : tcp '(' tcp_param_list ')' { }
1237 tcp_param_list
1238 : { }
1239 | tcp_expr { }
1240 | tcp_expr delimiter tcp_param_list { }
1243 tcp_field
1244 : K_SPORT { proto_field_set(TCP_SPORT); }
1245 | K_DPORT { proto_field_set(TCP_DPORT); }
1246 | K_SEQ { proto_field_set(TCP_SEQ); }
1247 | K_ACK_SEQ { proto_field_set(TCP_ACK_SEQ); }
1248 | K_DOFF { proto_field_set(TCP_DOFF); }
1249 | K_WINDOW { proto_field_set(TCP_WINDOW); }
1250 | K_CSUM { proto_field_set(TCP_CSUM); }
1251 | K_URG_PTR { proto_field_set(TCP_URG_PTR); }
1254 tcp_expr
1255 : tcp_field field_expr skip_white '=' skip_white field_value_expr
1256 { proto_field_expr_eval(); }
1257 | tcp_field skip_white '=' skip_white field_value_expr
1258 { proto_field_expr_eval(); }
1259 | K_CWR { proto_hdr_field_set_be16(hdr, TCP_CWR, 1); }
1260 | K_ECE { proto_hdr_field_set_be16(hdr, TCP_ECE, 1); }
1261 | K_URG { proto_hdr_field_set_be16(hdr, TCP_URG, 1); }
1262 | K_ACK { proto_hdr_field_set_be16(hdr, TCP_ACK, 1); }
1263 | K_PSH { proto_hdr_field_set_be16(hdr, TCP_PSH, 1); }
1264 | K_RST { proto_hdr_field_set_be16(hdr, TCP_RST, 1); }
1265 | K_SYN { proto_hdr_field_set_be16(hdr, TCP_SYN, 1); }
1266 | K_FIN { proto_hdr_field_set_be16(hdr, TCP_FIN, 1); }
1270 : K_TCP { proto_add(PROTO_TCP); }
1273 dns_proto
1274 : dns '(' dns_param_list ')' { }
1277 dns_param_list
1278 : { }
1279 | dns_expr { }
1280 | dns_expr delimiter dns_param_list { }
1283 dns_field
1284 : K_ID { proto_field_set(DNS_ID); }
1285 | K_QR { proto_field_set(DNS_QR); }
1286 | K_OPER { proto_field_set(DNS_OPCODE); }
1287 | K_AANSWER { proto_field_set(DNS_AA); }
1288 | K_TRUNC { proto_field_set(DNS_TC); }
1289 | K_RDESIRED { proto_field_set(DNS_RD); }
1290 | K_RAVAIL { proto_field_set(DNS_RA); }
1291 | K_ZERO { proto_field_set(DNS_ZERO); }
1292 | K_RCODE { proto_field_set(DNS_RCODE); }
1293 | K_QDCOUNT { proto_field_set(DNS_QD_COUNT); }
1294 | K_ANCOUNT { proto_field_set(DNS_AN_COUNT); }
1295 | K_NSCOUNT { proto_field_set(DNS_NS_COUNT); }
1296 | K_ARCOUNT { proto_field_set(DNS_AR_COUNT); }
1299 dns_query
1300 : K_QUERY { proto_push_sub_hdr(DNS_QUERY_HDR); }
1303 dns_query_name
1304 : K_NAME { proto_field_set(DNS_QUERY_NAME); }
1307 dns_query_field
1308 : K_TYPE { proto_field_set(DNS_QUERY_TYPE); }
1309 | K_CLASS { proto_field_set(DNS_QUERY_CLASS); }
1312 dns_query_expr
1313 : dns_query_field field_expr skip_white '=' skip_white field_value_expr
1314 { proto_field_expr_eval(); }
1315 | dns_query_field skip_white '=' skip_white field_value_expr
1316 { proto_field_expr_eval(); }
1317 | dns_query_name field_expr skip_white '=' skip_white field_value_expr
1318 { if (field_expr.type & FIELD_EXPR_STRING)
1319 field_expr.type = FIELD_EXPR_FQDN;
1320 proto_field_expr_eval(); }
1321 | dns_query_name skip_white '=' skip_white field_value_expr
1322 { if (field_expr.type & FIELD_EXPR_STRING)
1323 field_expr.type = FIELD_EXPR_FQDN;
1324 proto_field_expr_eval(); }
1327 dns_query_param_list
1328 : { }
1329 | dns_query_expr { }
1330 | dns_query_expr delimiter dns_query_param_list { }
1333 dns_query_hdr
1334 : dns_query '(' dns_query_param_list ')' { }
1337 dns_rrecord
1338 : K_ANSWER { proto_push_sub_hdr(DNS_ANSWER_HDR); }
1339 | K_AUTH { proto_push_sub_hdr(DNS_AUTH_HDR); }
1340 | K_ADD { proto_push_sub_hdr(DNS_ADD_HDR); }
1343 dns_rrecord_name
1344 : K_NAME { proto_field_set(DNS_RRECORD_NAME); }
1347 dns_rrecord_data_addr
1348 : ip4_addr
1349 { proto_hdr_field_set_u32(hdr, DNS_RRECORD_DATA, $1.s_addr);
1350 proto_hdr_field_set_be16(hdr, DNS_RRECORD_TYPE, 1); }
1351 | ip6_addr
1352 { proto_hdr_field_set_bytes(hdr, DNS_RRECORD_DATA, (uint8_t *)&$1.s6_addr, 16);
1353 proto_hdr_field_set_be16(hdr, DNS_RRECORD_TYPE, 28); }
1356 dns_rrecord_data_fqdn
1357 : string
1358 { char *str = xstrdup($1 + 1);
1359 char *fqdn;
1360 str[strlen($1 + 1) - 1] = '\0';
1361 fqdn = str2fqdn(str);
1362 proto_hdr_field_set_bytes(hdr, DNS_RRECORD_DATA, (uint8_t *) fqdn, strlen(fqdn) + 1);
1363 xfree(str);
1364 xfree(fqdn); }
1367 dns_rrecord_data_expr
1368 : K_ADDR '(' skip_white dns_rrecord_data_addr skip_white ')'
1370 | K_NS '(' skip_white dns_rrecord_data_fqdn skip_white ')'
1371 { proto_hdr_field_set_be16(hdr, DNS_RRECORD_TYPE, 2); }
1372 | K_CNAME '(' skip_white dns_rrecord_data_fqdn skip_white ')'
1373 { proto_hdr_field_set_be16(hdr, DNS_RRECORD_TYPE, 5); }
1374 | K_PTR '(' skip_white dns_rrecord_data_fqdn skip_white ')'
1375 { proto_hdr_field_set_be16(hdr, DNS_RRECORD_TYPE, 12); }
1378 dns_rrecord_field
1379 : K_TYPE { proto_field_set(DNS_RRECORD_TYPE); }
1380 | K_CLASS { proto_field_set(DNS_RRECORD_CLASS); }
1381 | K_TTL { proto_field_set(DNS_RRECORD_TTL); }
1382 | K_LEN { proto_field_set(DNS_RRECORD_LEN); }
1383 | K_DATA { proto_field_set(DNS_RRECORD_DATA); }
1386 dns_rrecord_expr
1387 : dns_rrecord_field field_expr skip_white '=' skip_white field_value_expr
1388 { proto_field_expr_eval(); }
1389 | dns_rrecord_field skip_white '=' skip_white field_value_expr
1390 { proto_field_expr_eval(); }
1391 | dns_rrecord_name field_expr skip_white '=' skip_white field_value_expr
1392 { if (field_expr.type & FIELD_EXPR_STRING)
1393 field_expr.type = FIELD_EXPR_FQDN;
1394 proto_field_expr_eval(); }
1395 | dns_rrecord_name skip_white '=' skip_white field_value_expr
1396 { if (field_expr.type & FIELD_EXPR_STRING)
1397 field_expr.type = FIELD_EXPR_FQDN;
1398 proto_field_expr_eval(); }
1399 | dns_rrecord_data_expr
1403 dns_rrecord_param_list
1404 : { }
1405 | dns_rrecord_expr { }
1406 | dns_rrecord_expr delimiter dns_rrecord_param_list { }
1409 dns_rrecord_hdr
1410 : dns_rrecord '(' dns_rrecord_param_list ')' { }
1413 dns_expr
1414 : dns_field field_expr skip_white '=' skip_white field_value_expr
1415 { proto_field_expr_eval(); }
1416 | dns_field skip_white '=' skip_white field_value_expr
1417 { proto_field_expr_eval(); }
1418 | dns_query_hdr { proto_pop_sub_hdr(); }
1419 | dns_rrecord_hdr { proto_pop_sub_hdr(); }
1423 : K_DNS { proto_add(PROTO_DNS); }
1427 static void finalize_packet(void)
1429 /* XXX hack ... we allocated one packet pointer too much */
1430 plen--;
1431 dlen--;
1434 static void dump_conf(void)
1436 size_t i, j;
1438 for (i = 0; i < plen; ++i) {
1439 printf("[%zu] pkt\n", i);
1440 printf(" len %zu cnts %zu rnds %zu\n",
1441 packets[i].len,
1442 packet_dyn[i].clen,
1443 packet_dyn[i].rlen);
1445 printf(" payload ");
1446 for (j = 0; j < packets[i].len; ++j)
1447 printf("%02x ", packets[i].payload[j]);
1448 printf("\n");
1450 for (j = 0; j < packet_dyn[i].clen; ++j)
1451 printf(" cnt%zu [%u,%u], inc %u, off %jd type %s\n", j,
1452 packet_dyn[i].cnt[j].min,
1453 packet_dyn[i].cnt[j].max,
1454 packet_dyn[i].cnt[j].inc,
1455 (intmax_t)packet_dyn[i].cnt[j].off,
1456 packet_dyn[i].cnt[j].type == TYPE_INC ?
1457 "inc" : "dec");
1459 for (j = 0; j < packet_dyn[i].rlen; ++j)
1460 printf(" rnd%zu off %jd\n", j,
1461 (intmax_t)packet_dyn[i].rnd[j].off);
1465 void cleanup_packets(void)
1467 size_t i, j;
1469 for (i = 0; i < plen; ++i) {
1470 struct packet *pkt = &packets[i];
1472 if (pkt->len > 0)
1473 xfree(pkt->payload);
1475 for (j = 0; j < pkt->headers_count; j++) {
1476 struct proto_hdr *hdr = pkt->headers[j];
1477 int k;
1479 for (k = 0; k < hdr->sub_headers_count; k++)
1480 xfree(hdr->sub_headers[k]);
1482 if (hdr->sub_headers)
1483 xfree(hdr->sub_headers);
1485 if (hdr->fields)
1486 xfree(hdr->fields);
1488 xfree(hdr);
1492 free(packets);
1494 for (i = 0; i < dlen; ++i) {
1495 free(packet_dyn[i].cnt);
1496 free(packet_dyn[i].rnd);
1498 for (j = 0; j < packet_dyn[j].flen; j++)
1499 xfree(packet_dyn[i].fields[j]);
1501 free(packet_dyn[i].fields);
1504 free(packet_dyn);
1507 void compile_packets(char *file, bool verbose, unsigned int cpu,
1508 bool invoke_cpp, char *const cpp_argv[])
1510 char tmp_file[128];
1511 int ret = -1;
1513 if (access(file, R_OK)) {
1514 fprintf(stderr, "Cannot access %s: %s!\n", file, strerror(errno));
1515 die();
1518 memset(tmp_file, 0, sizeof(tmp_file));
1519 our_cpu = cpu;
1521 if (invoke_cpp) {
1522 if (cpp_exec(file, tmp_file, sizeof(tmp_file), cpp_argv)) {
1523 fprintf(stderr, "Failed to invoke C preprocessor!\n");
1524 goto err;
1526 file = tmp_file;
1529 if (!strncmp("-", file, strlen("-")))
1530 yyin = stdin;
1531 else
1532 yyin = fopen(file, "r");
1533 if (!yyin) {
1534 fprintf(stderr, "Cannot open %s: %s!\n", file, strerror(errno));
1535 goto err;
1538 realloc_packet();
1539 if (yyparse() != 0)
1540 goto err;
1541 finalize_packet();
1543 if (our_cpu == 0 && verbose)
1544 dump_conf();
1546 ret = 0;
1547 err:
1548 if (yyin && yyin != stdin)
1549 fclose(yyin);
1551 if (invoke_cpp)
1552 unlink(tmp_file);
1553 if (ret)
1554 die();
1557 void compile_packets_str(char *str, bool verbose, unsigned int cpu)
1559 int ret = 1;
1561 our_cpu = cpu;
1562 realloc_packet();
1564 yy_scan_string(str);
1565 if (yyparse() != 0)
1566 goto err;
1568 finalize_packet();
1569 if (our_cpu == 0 && verbose)
1570 dump_conf();
1572 ret = 0;
1573 err:
1574 yylex_destroy();
1576 if (ret)
1577 die();
1580 void yyerror(const char *err)
1582 fprintf(stderr, "Syntax error at line %d, char '%s': %s\n", yylineno, yytext, err);