trafgen: proto: Fix bad field masking
[netsniff-ng.git] / trafgen_parser.y
blob655b0baa33e9961fb4d33edc3b29229987a2219a
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 <signal.h>
16 #include <stdint.h>
17 #include <errno.h>
18 #include <stdbool.h>
19 #include <libgen.h>
20 #include <net/if_arp.h>
21 #include <netinet/in.h>
22 #include <linux/if_ether.h>
24 #include "xmalloc.h"
25 #include "trafgen_parser.tab.h"
26 #include "trafgen_conf.h"
27 #include "trafgen_proto.h"
28 #include "trafgen_l2.h"
29 #include "trafgen_l3.h"
30 #include "trafgen_l4.h"
31 #include "built_in.h"
32 #include "die.h"
33 #include "str.h"
34 #include "csum.h"
35 #include "cpp.h"
37 #define YYERROR_VERBOSE 0
38 #define YYDEBUG 0
39 #define YYENABLE_NLS 1
40 #define YYLTYPE_IS_TRIVIAL 1
41 #define ENABLE_NLS 1
43 extern FILE *yyin;
44 extern int yylex(void);
45 extern void yy_scan_string(char *);
46 extern void yylex_destroy();
47 extern void yyerror(const char *);
48 extern int yylineno;
49 extern char *yytext;
51 extern struct packet *packets;
52 extern size_t plen;
54 #define packet_last (plen - 1)
56 #define payload_last (packets[packet_last].len - 1)
58 extern struct packet_dyn *packet_dyn;
59 extern size_t dlen;
61 #define packetd_last (dlen - 1)
63 #define packetdc_last (packet_dyn[packetd_last].clen - 1)
64 #define packetdr_last (packet_dyn[packetd_last].rlen - 1)
65 #define packetds_last (packet_dyn[packetd_last].slen - 1)
67 static int our_cpu, min_cpu = -1, max_cpu = -1;
69 static struct proto_hdr *hdr;
71 static inline int test_ignore(void)
73 if (min_cpu < 0 && max_cpu < 0)
74 return 0;
75 else if (max_cpu >= our_cpu && min_cpu <= our_cpu)
76 return 0;
77 else
78 return 1;
81 static inline void __init_new_packet_slot(struct packet *slot)
83 slot->payload = NULL;
84 slot->len = 0;
87 static inline void __init_new_counter_slot(struct packet_dyn *slot)
89 slot->cnt = NULL;
90 slot->clen = 0;
93 static inline void __init_new_randomizer_slot(struct packet_dyn *slot)
95 slot->rnd = NULL;
96 slot->rlen = 0;
99 static inline void __init_new_csum_slot(struct packet_dyn *slot)
101 slot->csum = NULL;
102 slot->slen = 0;
105 static inline void __setup_new_counter(struct counter *c, uint8_t start,
106 uint8_t stop, uint8_t stepping,
107 int type)
109 c->min = start;
110 c->max = stop;
111 c->inc = stepping;
112 c->val = (type == TYPE_INC) ? start : stop;
113 c->off = payload_last;
114 c->type = type;
117 static inline void __setup_new_randomizer(struct randomizer *r)
119 r->off = payload_last;
122 static inline void __setup_new_csum16(struct csum16 *s, off_t from, off_t to,
123 enum csum which)
125 s->off = payload_last - 1;
126 s->from = from;
127 s->to = to;
128 s->which = which;
131 static void realloc_packet(void)
133 if (test_ignore())
134 return;
136 plen++;
137 packets = xrealloc(packets, plen * sizeof(*packets));
139 __init_new_packet_slot(&packets[packet_last]);
141 dlen++;
142 packet_dyn = xrealloc(packet_dyn, dlen * sizeof(*packet_dyn));
144 __init_new_counter_slot(&packet_dyn[packetd_last]);
145 __init_new_randomizer_slot(&packet_dyn[packetd_last]);
146 __init_new_csum_slot(&packet_dyn[packetd_last]);
149 struct packet *current_packet(void)
151 return &packets[packet_last];
154 static void set_byte(uint8_t val)
156 struct packet *pkt = &packets[packet_last];
158 if (test_ignore())
159 return;
161 pkt->len++;
162 pkt->payload = xrealloc(pkt->payload, pkt->len);
163 pkt->payload[payload_last] = val;
166 static void set_multi_byte(uint8_t *s, size_t len)
168 size_t i;
170 for (i = 0; i < len; ++i)
171 set_byte(s[i]);
174 void set_fill(uint8_t val, size_t len)
176 size_t i;
177 struct packet *pkt = &packets[packet_last];
179 if (test_ignore())
180 return;
182 pkt->len += len;
183 pkt->payload = xrealloc(pkt->payload, pkt->len);
184 for (i = 0; i < len; ++i)
185 pkt->payload[payload_last - i] = val;
188 static void __set_csum16_dynamic(size_t from, size_t to, enum csum which)
190 struct packet *pkt = &packets[packet_last];
191 struct packet_dyn *pktd = &packet_dyn[packetd_last];
193 pkt->len += 2;
194 pkt->payload = xrealloc(pkt->payload, pkt->len);
196 pktd->slen++;
197 pktd->csum = xrealloc(pktd->csum, pktd->slen * sizeof(struct csum16));
199 __setup_new_csum16(&pktd->csum[packetds_last], from, to, which);
202 static void __set_csum16_static(size_t from, size_t to, enum csum which __maybe_unused)
204 struct packet *pkt = &packets[packet_last];
205 uint16_t sum;
206 uint8_t *psum;
208 sum = htons(calc_csum(pkt->payload + from, to - from));
209 psum = (uint8_t *) &sum;
211 set_byte(psum[0]);
212 set_byte(psum[1]);
215 static inline bool is_dynamic_csum(enum csum which)
217 switch (which) {
218 case CSUM_UDP:
219 case CSUM_TCP:
220 case CSUM_UDP6:
221 case CSUM_TCP6:
222 return true;
223 default:
224 return false;
228 static void set_csum16(size_t from, size_t to, enum csum which)
230 struct packet *pkt = &packets[packet_last];
231 struct packet_dyn *pktd = &packet_dyn[packetd_last];
233 if (test_ignore())
234 return;
236 if (to < from) {
237 size_t tmp = to;
239 to = from;
240 from = tmp;
243 bug_on(!(from < to));
245 if (packet_dyn_has_elems(pktd) || to >= pkt->len || is_dynamic_csum(which))
246 __set_csum16_dynamic(from, to, which);
247 else
248 __set_csum16_static(from, to, which);
251 static void set_rnd(size_t len)
253 size_t i;
254 struct packet *pkt = &packets[packet_last];
256 if (test_ignore())
257 return;
259 pkt->len += len;
260 pkt->payload = xrealloc(pkt->payload, pkt->len);
261 for (i = 0; i < len; ++i)
262 pkt->payload[payload_last - i] = (uint8_t) rand();
265 static void set_sequential_inc(uint8_t start, size_t len, uint8_t stepping)
267 size_t i;
268 struct packet *pkt = &packets[packet_last];
270 if (test_ignore())
271 return;
273 pkt->len += len;
274 pkt->payload = xrealloc(pkt->payload, pkt->len);
275 for (i = 0; i < len; ++i) {
276 off_t off = len - 1 - i;
278 pkt->payload[payload_last - off] = start;
279 start += stepping;
283 static void set_sequential_dec(uint8_t start, size_t len, uint8_t stepping)
285 size_t i;
286 struct packet *pkt = &packets[packet_last];
288 if (test_ignore())
289 return;
291 pkt->len += len;
292 pkt->payload = xrealloc(pkt->payload, pkt->len);
293 for (i = 0; i < len; ++i) {
294 int off = len - 1 - i;
296 pkt->payload[payload_last - off] = start;
297 start -= stepping;
301 static void set_dynamic_rnd(void)
303 struct packet *pkt = &packets[packet_last];
304 struct packet_dyn *pktd = &packet_dyn[packetd_last];
306 if (test_ignore())
307 return;
309 pkt->len++;
310 pkt->payload = xrealloc(pkt->payload, pkt->len);
312 pktd->rlen++;
313 pktd->rnd = xrealloc(pktd->rnd, pktd->rlen * sizeof(struct randomizer));
315 __setup_new_randomizer(&pktd->rnd[packetdr_last]);
318 static void set_dynamic_incdec(uint8_t start, uint8_t stop, uint8_t stepping,
319 int type)
321 struct packet *pkt = &packets[packet_last];
322 struct packet_dyn *pktd = &packet_dyn[packetd_last];
324 if (test_ignore())
325 return;
327 pkt->len++;
328 pkt->payload = xrealloc(pkt->payload, pkt->len);
330 pktd->clen++;
331 pktd->cnt = xrealloc(pktd->cnt, pktd->clen * sizeof(struct counter));
333 __setup_new_counter(&pktd->cnt[packetdc_last], start, stop, stepping, type);
336 static void proto_add(enum proto_id pid)
338 hdr = proto_header_init(pid);
343 %union {
344 struct in_addr ip4_addr;
345 long long int number;
346 uint8_t bytes[256];
347 char *str;
350 %token K_COMMENT K_FILL K_RND K_SEQINC K_SEQDEC K_DRND K_DINC K_DDEC K_WHITE
351 %token K_CPU K_CSUMIP K_CSUMUDP K_CSUMTCP K_CSUMUDP6 K_CSUMTCP6 K_CONST8 K_CONST16 K_CONST32 K_CONST64
353 %token K_DADDR K_SADDR K_ETYPE
354 %token K_OPER K_SHA K_SPA K_THA K_TPA K_REQUEST K_REPLY K_PTYPE K_HTYPE
355 %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
356 %token K_SPORT K_DPORT
357 %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
358 %token K_TPID K_TCI K_PCP K_DEI K_1Q K_1AD
360 %token K_ETH
361 %token K_VLAN
362 %token K_ARP
363 %token K_IP4
364 %token K_UDP K_TCP
366 %token ',' '{' '}' '(' ')' '[' ']' ':' '-' '+' '*' '/' '%' '&' '|' '<' '>' '^'
368 %token number string mac ip4_addr
370 %type <number> number expression
371 %type <str> string
372 %type <bytes> mac
373 %type <ip4_addr> ip4_addr
375 %left '-' '+' '*' '/' '%' '&' '|' '<' '>' '^'
379 packets
380 : { }
381 | packets packet { }
382 | packets inline_comment { }
383 | packets K_WHITE { }
386 inline_comment
387 : K_COMMENT { }
390 cpu_delim
391 : ':' { }
392 | '-' { }
395 delimiter_nowhite
396 : ',' { }
397 | ',' K_WHITE { }
400 noenforce_white
401 : { }
402 | K_WHITE { }
403 | delimiter_nowhite { }
406 skip_white
407 : { }
408 | K_WHITE { }
410 packet
411 : '{' noenforce_white payload noenforce_white '}' {
412 min_cpu = max_cpu = -1;
414 proto_packet_finish();
416 realloc_packet();
418 | K_CPU '(' number cpu_delim number ')' ':' noenforce_white '{' noenforce_white payload noenforce_white '}' {
419 min_cpu = $3;
420 max_cpu = $5;
422 if (min_cpu > max_cpu) {
423 int tmp = min_cpu;
425 min_cpu = max_cpu;
426 max_cpu = tmp;
429 proto_packet_finish();
431 realloc_packet();
433 | K_CPU '(' number ')' ':' noenforce_white '{' noenforce_white payload noenforce_white '}' {
434 min_cpu = max_cpu = $3;
436 proto_packet_finish();
438 realloc_packet();
442 payload
443 : elem { }
444 | payload elem_delimiter { }
447 delimiter
448 : delimiter_nowhite { }
449 | K_WHITE { }
452 elem_delimiter
453 : delimiter elem { }
456 elem
457 : number { set_byte((uint8_t) $1); }
458 | string { set_multi_byte((uint8_t *) $1 + 1, strlen($1) - 2); }
459 | fill { }
460 | rnd { }
461 | drnd { }
462 | seqinc { }
463 | seqdec { }
464 | dinc { }
465 | ddec { }
466 | csum { }
467 | const { }
468 | proto { proto_header_finish(hdr); }
469 | inline_comment { }
472 expression
473 : number
474 { $$ = $1; }
475 | expression '+' expression
476 { $$ = $1 + $3; }
477 | expression '-' expression
478 { $$ = $1 - $3; }
479 | expression '*' expression
480 { $$ = $1 * $3; }
481 | expression '/' expression
482 { $$ = $1 / $3; }
483 | expression '%' expression
484 { $$ = $1 % $3; }
485 | expression '&' expression
486 { $$ = $1 & $3; }
487 | expression '|' expression
488 { $$ = $1 | $3; }
489 | expression '^' expression
490 { $$ = $1 ^ $3; }
491 | expression '<' '<' expression
492 { $$ = $1 << $4; }
493 | expression '>' '>' expression
494 { $$ = $1 >> $4; }
495 | '-' expression
496 { $$ = -1 * $2; }
497 | '(' expression ')'
498 { $$ = $2;}
501 fill
502 : K_FILL '(' number delimiter number ')'
503 { set_fill($3, $5); }
506 const
507 : K_CONST8 '(' expression ')'
508 { set_byte((uint8_t) $3); }
509 | K_CONST16 '(' expression ')' {
510 uint16_t __c = cpu_to_be16((uint16_t) $3);
512 set_multi_byte((uint8_t *) &__c, sizeof(__c));
514 | K_CONST32 '(' expression ')' {
515 uint32_t __c = cpu_to_be32((uint32_t) $3);
517 set_multi_byte((uint8_t *) &__c, sizeof(__c));
519 | K_CONST64 '(' expression ')' {
520 uint64_t __c = cpu_to_be64((uint64_t) $3);
522 set_multi_byte((uint8_t *) &__c, sizeof(__c));
527 : K_RND '(' number ')'
528 { set_rnd($3); }
531 csum
532 : K_CSUMIP '(' number delimiter number ')'
533 { set_csum16($3, $5, CSUM_IP); }
534 | K_CSUMTCP '(' number delimiter number ')'
535 { set_csum16($3, $5, CSUM_TCP); }
536 | K_CSUMUDP '(' number delimiter number ')'
537 { set_csum16($3, $5, CSUM_UDP); }
538 | K_CSUMTCP6 '(' number delimiter number ')'
539 { set_csum16($3, $5, CSUM_TCP6); }
540 | K_CSUMUDP6 '(' number delimiter number ')'
541 { set_csum16($3, $5, CSUM_UDP6); }
544 seqinc
545 : K_SEQINC '(' number delimiter number ')'
546 { set_sequential_inc($3, $5, 1); }
547 | K_SEQINC '(' number delimiter number delimiter number ')'
548 { set_sequential_inc($3, $5, $7); }
551 seqdec
552 : K_SEQDEC '(' number delimiter number ')'
553 { set_sequential_dec($3, $5, 1); }
554 | K_SEQDEC '(' number delimiter number delimiter number ')'
555 { set_sequential_dec($3, $5, $7); }
558 drnd
559 : K_DRND '(' ')'
560 { set_dynamic_rnd(); }
561 | K_DRND '(' number ')'
563 int i, max = $3;
564 for (i = 0; i < max; ++i)
565 set_dynamic_rnd();
569 dinc
570 : K_DINC '(' number delimiter number ')'
571 { set_dynamic_incdec($3, $5, 1, TYPE_INC); }
572 | K_DINC '(' number delimiter number delimiter number ')'
573 { set_dynamic_incdec($3, $5, $7, TYPE_INC); }
576 ddec
577 : K_DDEC '(' number delimiter number ')'
578 { set_dynamic_incdec($3, $5, 1, TYPE_DEC); }
579 | K_DDEC '(' number delimiter number delimiter number ')'
580 { set_dynamic_incdec($3, $5, $7, TYPE_DEC); }
583 proto
584 : eth_proto { }
585 | vlan_proto { }
586 | arp_proto { }
587 | ip4_proto { }
588 | udp_proto { }
589 | tcp_proto { }
592 eth_proto
593 : eth '(' eth_param_list ')' { }
597 : K_ETH { proto_add(PROTO_ETH); }
600 eth_param_list
601 : { }
602 | eth_field { }
603 | eth_field delimiter eth_param_list { }
606 eth_type
607 : K_ETYPE { }
608 | K_PROT { }
611 eth_field
612 : K_DADDR skip_white '=' skip_white mac
613 { proto_field_set_bytes(hdr, ETH_DST_ADDR, $5); }
614 | K_SADDR skip_white '=' skip_white mac
615 { proto_field_set_bytes(hdr, ETH_SRC_ADDR, $5); }
616 | eth_type skip_white '=' skip_white number
617 { proto_field_set_be16(hdr, ETH_TYPE, $5); }
620 vlan_proto
621 : vlan '(' vlan_param_list ')' { }
624 vlan
625 : K_VLAN { proto_add(PROTO_VLAN); }
628 vlan_param_list
629 : { }
630 | vlan_field { }
631 | vlan_field delimiter vlan_param_list { }
634 vlan_type
635 : K_TPID { }
636 | K_PROT
639 vlan_field
640 : vlan_type skip_white '=' skip_white number
641 { proto_field_set_be16(hdr, VLAN_TPID, $5); }
642 | K_1Q
643 { proto_field_set_be16(hdr, VLAN_TPID, ETH_P_8021Q); }
644 | K_1AD
645 { proto_field_set_be16(hdr, VLAN_TPID, ETH_P_8021AD); }
646 | K_TCI skip_white '=' skip_white number
647 { proto_field_set_be16(hdr, VLAN_TCI, $5); }
648 | K_PCP skip_white '=' skip_white number
649 { proto_field_set_be16(hdr, VLAN_PCP, $5); }
650 | K_DEI skip_white '=' skip_white number
651 { proto_field_set_be16(hdr, VLAN_DEI, $5); }
652 | K_ID skip_white '=' skip_white number
653 { proto_field_set_be16(hdr, VLAN_VID, $5); }
656 arp_proto
657 : arp '(' arp_param_list ')' { }
660 arp_param_list
661 : { }
662 | arp_field { }
663 | arp_field delimiter arp_param_list { }
666 arp_field
667 : K_OPER skip_white '=' skip_white K_REQUEST
668 { proto_field_set_be16(hdr, ARP_OPER, ARPOP_REQUEST); }
669 | K_OPER skip_white '=' skip_white K_REPLY
670 { proto_field_set_be16(hdr, ARP_OPER, ARPOP_REPLY); }
671 | K_OPER skip_white '=' skip_white number
672 { proto_field_set_be16(hdr, ARP_OPER, $5); }
673 | K_REQUEST
674 { proto_field_set_be16(hdr, ARP_OPER, ARPOP_REQUEST); }
675 | K_REPLY
676 { proto_field_set_be16(hdr, ARP_OPER, ARPOP_REPLY); }
677 | K_HTYPE skip_white '=' skip_white number
678 { proto_field_set_be16(hdr, ARP_HTYPE, $5); }
679 | K_PTYPE skip_white '=' skip_white number
680 { proto_field_set_be16(hdr, ARP_PTYPE, $5); }
681 | K_SHA skip_white '=' skip_white mac
682 { proto_field_set_bytes(hdr, ARP_SHA, $5); }
683 | K_THA skip_white '=' skip_white mac
684 { proto_field_set_bytes(hdr, ARP_THA, $5); }
685 | K_SPA skip_white '=' skip_white ip4_addr
686 { proto_field_set_u32(hdr, ARP_SPA, $5.s_addr); }
687 | K_TPA skip_white '=' skip_white ip4_addr
688 { proto_field_set_u32(hdr, ARP_TPA, $5.s_addr); }
691 : K_ARP { proto_add(PROTO_ARP); }
694 ip4_proto
695 : ip4 '(' ip4_param_list ')' { }
698 ip4_param_list
699 : { }
700 | ip4_field { }
701 | ip4_field delimiter ip4_param_list { }
704 ip4_field
705 : K_VER skip_white '=' skip_white number
706 { proto_field_set_u8(hdr, IP4_VER, $5); }
707 | K_IHL skip_white '=' skip_white number
708 { proto_field_set_u8(hdr, IP4_IHL, $5); }
709 | K_DADDR skip_white '=' skip_white ip4_addr
710 { proto_field_set_u32(hdr, IP4_DADDR, $5.s_addr); }
711 | K_SADDR skip_white '=' skip_white ip4_addr
712 { proto_field_set_u32(hdr, IP4_SADDR, $5.s_addr); }
713 | K_PROT skip_white '=' skip_white number
714 { proto_field_set_u8(hdr, IP4_PROTO, $5); }
715 | K_TTL skip_white '=' skip_white number
716 { proto_field_set_u8(hdr, IP4_TTL, $5); }
717 | K_DSCP skip_white '=' skip_white number
718 { proto_field_set_u8(hdr, IP4_DSCP, $5); }
719 | K_ECN skip_white '=' skip_white number
720 { proto_field_set_u8(hdr, IP4_ECN, $5); }
721 | K_TOS skip_white '=' skip_white number
722 { proto_field_set_u8(hdr, IP4_TOS, $5); }
723 | K_LEN skip_white '=' skip_white number
724 { proto_field_set_be16(hdr, IP4_LEN, $5); }
725 | K_ID skip_white '=' skip_white number
726 { proto_field_set_be16(hdr, IP4_ID, $5); }
727 | K_FLAGS skip_white '=' skip_white number
728 { proto_field_set_be16(hdr, IP4_FLAGS, $5); }
729 | K_DF { proto_field_set_be16(hdr, IP4_DF, 1); }
730 | K_MF { proto_field_set_be16(hdr, IP4_MF, 1); }
731 | K_FRAG skip_white '=' skip_white number
732 { proto_field_set_be16(hdr, IP4_FRAG_OFFS, $5); }
733 | K_CSUM skip_white '=' skip_white number
734 { proto_field_set_be16(hdr, IP4_CSUM, $5); }
738 : K_IP4 { proto_add(PROTO_IP4); }
741 udp_proto
742 : udp '(' udp_param_list ')' { }
745 udp_param_list
746 : { }
747 | udp_field { }
748 | udp_field delimiter udp_param_list { }
751 udp_field
752 : K_SPORT skip_white '=' skip_white number
753 { proto_field_set_be16(hdr, UDP_SPORT, $5); }
754 | K_DPORT skip_white '=' skip_white number
755 { proto_field_set_be16(hdr, UDP_DPORT, $5); }
756 | K_LEN skip_white '=' skip_white number
757 { proto_field_set_be16(hdr, UDP_LEN, $5); }
758 | K_CSUM skip_white '=' skip_white number
759 { proto_field_set_be16(hdr, UDP_CSUM, $5); }
763 : K_UDP { proto_add(PROTO_UDP); }
766 tcp_proto
767 : tcp '(' tcp_param_list ')' { }
770 tcp_param_list
771 : { }
772 | tcp_field { }
773 | tcp_field delimiter tcp_param_list { }
776 tcp_field
777 : K_SPORT skip_white '=' skip_white number
778 { proto_field_set_be16(hdr, TCP_SPORT, $5); }
779 | K_DPORT skip_white '=' skip_white number
780 { proto_field_set_be16(hdr, TCP_DPORT, $5); }
781 | K_SEQ skip_white '=' skip_white number
782 { proto_field_set_be32(hdr, TCP_SEQ, $5); }
783 | K_ACK_SEQ skip_white '=' skip_white number
784 { proto_field_set_be32(hdr, TCP_ACK_SEQ, $5); }
785 | K_DOFF skip_white '=' skip_white number
786 { proto_field_set_be16(hdr, TCP_DOFF, $5); }
787 | K_CWR { proto_field_set_be16(hdr, TCP_CWR, 1); }
788 | K_ECE { proto_field_set_be16(hdr, TCP_ECE, 1); }
789 | K_URG { proto_field_set_be16(hdr, TCP_URG, 1); }
790 | K_ACK { proto_field_set_be16(hdr, TCP_ACK, 1); }
791 | K_PSH { proto_field_set_be16(hdr, TCP_PSH, 1); }
792 | K_RST { proto_field_set_be16(hdr, TCP_RST, 1); }
793 | K_SYN { proto_field_set_be16(hdr, TCP_SYN, 1); }
794 | K_FIN { proto_field_set_be16(hdr, TCP_FIN, 1); }
795 | K_WINDOW skip_white '=' skip_white number
796 { proto_field_set_be16(hdr, TCP_WINDOW, $5); }
797 | K_CSUM skip_white '=' skip_white number
798 { proto_field_set_be16(hdr, TCP_CSUM, $5); }
799 | K_URG_PTR skip_white '=' skip_white number
800 { proto_field_set_be16(hdr, TCP_URG_PTR, $5); }
804 : K_TCP { proto_add(PROTO_TCP); }
809 static void finalize_packet(void)
811 /* XXX hack ... we allocated one packet pointer too much */
812 plen--;
813 dlen--;
816 static void dump_conf(void)
818 size_t i, j;
820 for (i = 0; i < plen; ++i) {
821 printf("[%zu] pkt\n", i);
822 printf(" len %zu cnts %zu rnds %zu\n",
823 packets[i].len,
824 packet_dyn[i].clen,
825 packet_dyn[i].rlen);
827 printf(" payload ");
828 for (j = 0; j < packets[i].len; ++j)
829 printf("%02x ", packets[i].payload[j]);
830 printf("\n");
832 for (j = 0; j < packet_dyn[i].clen; ++j)
833 printf(" cnt%zu [%u,%u], inc %u, off %jd type %s\n", j,
834 packet_dyn[i].cnt[j].min,
835 packet_dyn[i].cnt[j].max,
836 packet_dyn[i].cnt[j].inc,
837 (intmax_t)packet_dyn[i].cnt[j].off,
838 packet_dyn[i].cnt[j].type == TYPE_INC ?
839 "inc" : "dec");
841 for (j = 0; j < packet_dyn[i].rlen; ++j)
842 printf(" rnd%zu off %jd\n", j,
843 (intmax_t)packet_dyn[i].rnd[j].off);
847 void cleanup_packets(void)
849 size_t i;
851 for (i = 0; i < plen; ++i) {
852 if (packets[i].len > 0)
853 xfree(packets[i].payload);
856 free(packets);
858 for (i = 0; i < dlen; ++i) {
859 free(packet_dyn[i].cnt);
860 free(packet_dyn[i].rnd);
863 free(packet_dyn);
866 void compile_packets(char *file, bool verbose, unsigned int cpu,
867 bool invoke_cpp, char *const cpp_argv[])
869 char tmp_file[128];
870 int ret = -1;
872 memset(tmp_file, 0, sizeof(tmp_file));
873 our_cpu = cpu;
875 if (invoke_cpp) {
876 if (cpp_exec(file, tmp_file, sizeof(tmp_file), cpp_argv)) {
877 fprintf(stderr, "Failed to invoke C preprocessor!\n");
878 goto err;
880 file = tmp_file;
883 if (!strncmp("-", file, strlen("-")))
884 yyin = stdin;
885 else
886 yyin = fopen(file, "r");
887 if (!yyin) {
888 fprintf(stderr, "Cannot open %s: %s!\n", file, strerror(errno));
889 goto err;
892 realloc_packet();
893 if (yyparse() != 0)
894 goto err;
895 finalize_packet();
897 if (our_cpu == 0 && verbose)
898 dump_conf();
900 ret = 0;
901 err:
902 if (yyin && yyin != stdin)
903 fclose(yyin);
905 if (invoke_cpp)
906 unlink(tmp_file);
907 if (ret)
908 die();
911 void compile_packets_str(char *str, bool verbose, unsigned int cpu)
913 int ret = 1;
915 our_cpu = cpu;
916 realloc_packet();
918 yy_scan_string(str);
919 if (yyparse() != 0)
920 goto err;
922 finalize_packet();
923 if (our_cpu == 0 && verbose)
924 dump_conf();
926 ret = 0;
927 err:
928 yylex_destroy();
930 if (ret)
931 die();
934 void yyerror(const char *err)
936 fprintf(stderr, "Syntax error at line %d, char '%s': %s\n", yylineno, yytext, err);