2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2012 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,
5 * Swiss federal institute of technology (ETH Zurich)
6 * Subject to the GPL, version 2.
9 /* yaac-func-prefix: yy */
20 #include <net/if_arp.h>
21 #include <netinet/in.h>
22 #include <linux/icmp.h>
23 #include <linux/if_ether.h>
24 #include <linux/icmpv6.h>
27 #include "trafgen_parser.tab.h"
28 #include "trafgen_conf.h"
29 #include "trafgen_proto.h"
30 #include "trafgen_l2.h"
31 #include "trafgen_l3.h"
32 #include "trafgen_l4.h"
39 #define YYERROR_VERBOSE 0
41 #define YYENABLE_NLS 1
42 #define YYLTYPE_IS_TRIVIAL 1
46 extern
int yylex(void);
47 extern
void yy_scan_string
(char *);
48 extern
void yylex_destroy
();
49 extern
void yyerror(const char *);
53 extern
struct packet
*packets
;
56 #define packet_last (plen - 1)
58 #define payload_last (packets[packet_last].len - 1)
60 extern
struct packet_dyn
*packet_dyn
;
63 #define packetd_last (dlen - 1)
65 #define packetdc_last (packet_dyn[packetd_last].clen - 1)
66 #define packetdr_last (packet_dyn[packetd_last].rlen - 1)
67 #define packetds_last (packet_dyn[packetd_last].slen - 1)
69 static int our_cpu
, min_cpu
= -1, max_cpu
= -1;
71 static struct proto_hdr
*hdr
;
73 static inline
int test_ignore
(void)
75 if
(min_cpu
< 0 && max_cpu
< 0)
77 else if
(max_cpu
>= our_cpu
&& min_cpu
<= our_cpu
)
83 static inline
void __init_new_packet_slot
(struct packet
*slot
)
89 static inline
void __init_new_counter_slot
(struct packet_dyn
*slot
)
95 static inline
void __init_new_randomizer_slot
(struct packet_dyn
*slot
)
101 static inline
void __init_new_csum_slot
(struct packet_dyn
*slot
)
107 static inline
void __setup_new_counter
(struct counter
*c
, uint8_t start
,
108 uint8_t stop
, uint8_t stepping
,
114 c
->val
= (type
== TYPE_INC
) ? start
: stop
;
115 c
->off
= payload_last
;
119 static inline
void __setup_new_randomizer
(struct randomizer
*r
)
121 r
->off
= payload_last
;
124 static inline
void __setup_new_csum16
(struct csum16
*s
, off_t from
, off_t to
,
127 s
->off
= payload_last
- 1;
133 static void realloc_packet
(void)
139 packets
= xrealloc
(packets
, plen
* sizeof
(*packets
));
141 __init_new_packet_slot
(&packets
[packet_last
]);
144 packet_dyn
= xrealloc
(packet_dyn
, dlen
* sizeof
(*packet_dyn
));
146 __init_new_counter_slot
(&packet_dyn
[packetd_last
]);
147 __init_new_randomizer_slot
(&packet_dyn
[packetd_last
]);
148 __init_new_csum_slot
(&packet_dyn
[packetd_last
]);
151 struct packet
*current_packet
(void)
153 return
&packets
[packet_last
];
156 uint32_t current_packet_id
(void)
161 struct packet
*packet_get
(uint32_t id
)
166 static void set_byte
(uint8_t val
)
168 struct packet
*pkt
= &packets
[packet_last
];
174 pkt
->payload
= xrealloc
(pkt
->payload
, pkt
->len
);
175 pkt
->payload
[payload_last
] = val
;
178 static void set_multi_byte
(uint8_t *s
, size_t len
)
182 for
(i
= 0; i
< len
; ++i
)
186 void set_fill
(uint8_t val
, size_t len
)
189 struct packet
*pkt
= &packets
[packet_last
];
195 pkt
->payload
= xrealloc
(pkt
->payload
, pkt
->len
);
196 for
(i
= 0; i
< len
; ++i
)
197 pkt
->payload
[payload_last
- i
] = val
;
200 static void __set_csum16_dynamic
(size_t from
, size_t to
, enum csum which
)
202 struct packet
*pkt
= &packets
[packet_last
];
203 struct packet_dyn
*pktd
= &packet_dyn
[packetd_last
];
206 pkt
->payload
= xrealloc
(pkt
->payload
, pkt
->len
);
209 pktd
->csum
= xrealloc
(pktd
->csum
, pktd
->slen
* sizeof
(struct csum16
));
211 __setup_new_csum16
(&pktd
->csum
[packetds_last
], from
, to
, which
);
214 static void __set_csum16_static
(size_t from
, size_t to
, enum csum which __maybe_unused
)
216 struct packet
*pkt
= &packets
[packet_last
];
220 sum
= htons
(calc_csum
(pkt
->payload
+ from
, to
- from
));
221 psum
= (uint8_t *) &sum
;
227 static inline
bool is_dynamic_csum
(enum csum which
)
240 static void set_csum16
(size_t from
, size_t to
, enum csum which
)
242 struct packet
*pkt
= &packets
[packet_last
];
243 struct packet_dyn
*pktd
= &packet_dyn
[packetd_last
];
255 bug_on
(!(from
< to
));
257 if
(packet_dyn_has_elems
(pktd
) || to
>= pkt
->len || is_dynamic_csum
(which
))
258 __set_csum16_dynamic
(from
, to
, which
);
260 __set_csum16_static
(from
, to
, which
);
263 static void set_rnd
(size_t len
)
266 struct packet
*pkt
= &packets
[packet_last
];
272 pkt
->payload
= xrealloc
(pkt
->payload
, pkt
->len
);
273 for
(i
= 0; i
< len
; ++i
)
274 pkt
->payload
[payload_last
- i
] = (uint8_t) rand
();
277 static void set_sequential_inc
(uint8_t start
, size_t len
, uint8_t stepping
)
280 struct packet
*pkt
= &packets
[packet_last
];
286 pkt
->payload
= xrealloc
(pkt
->payload
, pkt
->len
);
287 for
(i
= 0; i
< len
; ++i
) {
288 off_t off
= len
- 1 - i
;
290 pkt
->payload
[payload_last
- off
] = start
;
295 static void set_sequential_dec
(uint8_t start
, size_t len
, uint8_t stepping
)
298 struct packet
*pkt
= &packets
[packet_last
];
304 pkt
->payload
= xrealloc
(pkt
->payload
, pkt
->len
);
305 for
(i
= 0; i
< len
; ++i
) {
306 int off
= len
- 1 - i
;
308 pkt
->payload
[payload_last
- off
] = start
;
313 static void set_dynamic_rnd
(void)
315 struct packet
*pkt
= &packets
[packet_last
];
316 struct packet_dyn
*pktd
= &packet_dyn
[packetd_last
];
322 pkt
->payload
= xrealloc
(pkt
->payload
, pkt
->len
);
325 pktd
->rnd
= xrealloc
(pktd
->rnd
, pktd
->rlen
* sizeof
(struct randomizer
));
327 __setup_new_randomizer
(&pktd
->rnd
[packetdr_last
]);
330 static void set_dynamic_incdec
(uint8_t start
, uint8_t stop
, uint8_t stepping
,
333 struct packet
*pkt
= &packets
[packet_last
];
334 struct packet_dyn
*pktd
= &packet_dyn
[packetd_last
];
340 pkt
->payload
= xrealloc
(pkt
->payload
, pkt
->len
);
343 pktd
->cnt
= xrealloc
(pktd
->cnt
, pktd
->clen
* sizeof
(struct counter
));
345 __setup_new_counter
(&pktd
->cnt
[packetdc_last
], start
, stop
, stepping
, type
);
348 static void proto_add
(enum proto_id pid
)
350 hdr
= proto_header_init
(pid
);
356 struct in_addr ip4_addr
;
357 struct in6_addr ip6_addr
;
358 long long int number
;
363 %token K_COMMENT K_FILL K_RND K_SEQINC K_SEQDEC K_DRND K_DINC K_DDEC K_WHITE
364 %token K_CPU K_CSUMIP K_CSUMUDP K_CSUMTCP K_CSUMUDP6 K_CSUMTCP6 K_CONST8 K_CONST16 K_CONST32 K_CONST64
366 %token K_DADDR K_SADDR K_ETYPE K_TYPE
367 %token K_OPER K_SHA K_SPA K_THA K_TPA K_REQUEST K_REPLY K_PTYPE K_HTYPE
368 %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
369 %token K_FLOW K_NEXT_HDR K_HOP_LIMIT
370 %token K_CODE K_ECHO_REQUEST K_ECHO_REPLY
371 %token K_SPORT K_DPORT
372 %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
373 %token K_TPID K_TCI K_PCP K_DEI K_1Q K_1AD
374 %token K_LABEL K_TC K_LAST K_EXP
382 %token K_ICMP4 K_ICMP6
385 %token
',' '{' '}' '(' ')' '[' ']' ':' '-' '+' '*' '/' '%' '&' '|' '<' '>' '^'
387 %token number
string mac ip4_addr ip6_addr
389 %type
<number
> number expression
392 %type
<ip4_addr
> ip4_addr
393 %type
<ip6_addr
> ip6_addr
395 %left
'-' '+' '*' '/' '%' '&' '|' '<' '>' '^'
402 | packets inline_comment
{ }
403 | packets K_WHITE
{ }
423 | delimiter_nowhite
{ }
431 : '{' noenforce_white payload noenforce_white
'}' {
432 min_cpu
= max_cpu
= -1;
434 proto_packet_finish
();
438 | K_CPU
'(' number cpu_delim number
')' ':' noenforce_white
'{' noenforce_white payload noenforce_white
'}' {
442 if
(min_cpu
> max_cpu
) {
449 proto_packet_finish
();
453 | K_CPU
'(' number
')' ':' noenforce_white
'{' noenforce_white payload noenforce_white
'}' {
454 min_cpu
= max_cpu
= $3;
456 proto_packet_finish
();
464 | payload elem_delimiter
{ }
468 : delimiter_nowhite
{ }
477 : number
{ set_byte
((uint8_t) $1); }
478 |
string { set_multi_byte
((uint8_t *) $1 + 1, strlen
($1) - 2); }
488 | proto
{ proto_header_finish
(hdr
); }
495 | expression
'+' expression
497 | expression
'-' expression
499 | expression
'*' expression
501 | expression
'/' expression
503 | expression
'%' expression
505 | expression
'&' expression
507 | expression
'|' expression
509 | expression
'^' expression
511 | expression
'<' '<' expression
513 | expression
'>' '>' expression
522 : K_FILL
'(' number delimiter number
')'
523 { set_fill
($3, $5); }
527 : K_CONST8
'(' expression
')'
528 { set_byte
((uint8_t) $3); }
529 | K_CONST16
'(' expression
')' {
530 uint16_t __c
= cpu_to_be16
((uint16_t) $3);
532 set_multi_byte
((uint8_t *) &__c
, sizeof
(__c
));
534 | K_CONST32
'(' expression
')' {
535 uint32_t __c
= cpu_to_be32
((uint32_t) $3);
537 set_multi_byte
((uint8_t *) &__c
, sizeof
(__c
));
539 | K_CONST64
'(' expression
')' {
540 uint64_t __c
= cpu_to_be64
((uint64_t) $3);
542 set_multi_byte
((uint8_t *) &__c
, sizeof
(__c
));
547 : K_RND
'(' number
')'
552 : K_CSUMIP
'(' number delimiter number
')'
553 { set_csum16
($3, $5, CSUM_IP
); }
554 | K_CSUMTCP
'(' number delimiter number
')'
555 { set_csum16
($3, $5, CSUM_TCP
); }
556 | K_CSUMUDP
'(' number delimiter number
')'
557 { set_csum16
($3, $5, CSUM_UDP
); }
558 | K_CSUMTCP6
'(' number delimiter number
')'
559 { set_csum16
($3, $5, CSUM_TCP6
); }
560 | K_CSUMUDP6
'(' number delimiter number
')'
561 { set_csum16
($3, $5, CSUM_UDP6
); }
565 : K_SEQINC
'(' number delimiter number
')'
566 { set_sequential_inc
($3, $5, 1); }
567 | K_SEQINC
'(' number delimiter number delimiter number
')'
568 { set_sequential_inc
($3, $5, $7); }
572 : K_SEQDEC
'(' number delimiter number
')'
573 { set_sequential_dec
($3, $5, 1); }
574 | K_SEQDEC
'(' number delimiter number delimiter number
')'
575 { set_sequential_dec
($3, $5, $7); }
580 { set_dynamic_rnd
(); }
581 | K_DRND
'(' number
')'
584 for
(i
= 0; i
< max
; ++i
)
590 : K_DINC
'(' number delimiter number
')'
591 { set_dynamic_incdec
($3, $5, 1, TYPE_INC
); }
592 | K_DINC
'(' number delimiter number delimiter number
')'
593 { set_dynamic_incdec
($3, $5, $7, TYPE_INC
); }
597 : K_DDEC
'(' number delimiter number
')'
598 { set_dynamic_incdec
($3, $5, 1, TYPE_DEC
); }
599 | K_DDEC
'(' number delimiter number delimiter number
')'
600 { set_dynamic_incdec
($3, $5, $7, TYPE_DEC
); }
617 : eth
'(' eth_param_list
')' { }
621 : K_ETH
{ proto_add
(PROTO_ETH
); }
627 | eth_field delimiter eth_param_list
{ }
637 : K_DADDR skip_white
'=' skip_white mac
638 { proto_field_set_bytes
(hdr
, ETH_DST_ADDR
, $5); }
639 | K_SADDR skip_white
'=' skip_white mac
640 { proto_field_set_bytes
(hdr
, ETH_SRC_ADDR
, $5); }
641 | eth_type skip_white
'=' skip_white number
642 { proto_field_set_be16
(hdr
, ETH_TYPE
, $5); }
646 : vlan
'(' vlan_param_list
')' { }
650 : K_VLAN
{ proto_add
(PROTO_VLAN
); }
656 | vlan_field delimiter vlan_param_list
{ }
665 : vlan_type skip_white
'=' skip_white number
666 { proto_field_set_be16
(hdr
, VLAN_TPID
, $5); }
668 { proto_field_set_be16
(hdr
, VLAN_TPID
, ETH_P_8021Q
); }
670 { proto_field_set_be16
(hdr
, VLAN_TPID
, ETH_P_8021AD
); }
671 | K_TCI skip_white
'=' skip_white number
672 { proto_field_set_be16
(hdr
, VLAN_TCI
, $5); }
673 | K_PCP skip_white
'=' skip_white number
674 { proto_field_set_be16
(hdr
, VLAN_PCP
, $5); }
675 | K_DEI skip_white
'=' skip_white number
676 { proto_field_set_be16
(hdr
, VLAN_DEI
, $5); }
677 | K_ID skip_white
'=' skip_white number
678 { proto_field_set_be16
(hdr
, VLAN_VID
, $5); }
682 : mpls
'(' mpls_param_list
')' { }
686 : K_MPLS
{ proto_add
(PROTO_MPLS
); }
692 | mpls_field delimiter mpls_param_list
{ }
701 : K_LABEL skip_white
'=' skip_white number
702 { proto_field_set_be32
(hdr
, MPLS_LABEL
, $5); }
703 | mpls_tc skip_white
'=' skip_white number
704 { proto_field_set_be32
(hdr
, MPLS_TC
, $5); }
705 | K_LAST skip_white
'=' skip_white number
706 { proto_field_set_be32
(hdr
, MPLS_LAST
, $5); }
707 | K_TTL skip_white
'=' skip_white number
708 { proto_field_set_be32
(hdr
, MPLS_TTL
, $5); }
712 : arp
'(' arp_param_list
')' { }
718 | arp_field delimiter arp_param_list
{ }
722 : K_OPER skip_white
'=' skip_white K_REQUEST
723 { proto_field_set_be16
(hdr
, ARP_OPER
, ARPOP_REQUEST
); }
724 | K_OPER skip_white
'=' skip_white K_REPLY
725 { proto_field_set_be16
(hdr
, ARP_OPER
, ARPOP_REPLY
); }
726 | K_OPER skip_white
'=' skip_white number
727 { proto_field_set_be16
(hdr
, ARP_OPER
, $5); }
729 { proto_field_set_be16
(hdr
, ARP_OPER
, ARPOP_REQUEST
); }
731 { proto_field_set_be16
(hdr
, ARP_OPER
, ARPOP_REPLY
); }
732 | K_HTYPE skip_white
'=' skip_white number
733 { proto_field_set_be16
(hdr
, ARP_HTYPE
, $5); }
734 | K_PTYPE skip_white
'=' skip_white number
735 { proto_field_set_be16
(hdr
, ARP_PTYPE
, $5); }
736 | K_SHA skip_white
'=' skip_white mac
737 { proto_field_set_bytes
(hdr
, ARP_SHA
, $5); }
738 | K_THA skip_white
'=' skip_white mac
739 { proto_field_set_bytes
(hdr
, ARP_THA
, $5); }
740 | K_SPA skip_white
'=' skip_white ip4_addr
741 { proto_field_set_u32
(hdr
, ARP_SPA
, $5.s_addr
); }
742 | K_TPA skip_white
'=' skip_white ip4_addr
743 { proto_field_set_u32
(hdr
, ARP_TPA
, $5.s_addr
); }
746 : K_ARP
{ proto_add
(PROTO_ARP
); }
750 : ip4
'(' ip4_param_list
')' { }
756 | ip4_field delimiter ip4_param_list
{ }
760 : K_VER skip_white
'=' skip_white number
761 { proto_field_set_u8
(hdr
, IP4_VER
, $5); }
762 | K_IHL skip_white
'=' skip_white number
763 { proto_field_set_u8
(hdr
, IP4_IHL
, $5); }
764 | K_DADDR skip_white
'=' skip_white ip4_addr
765 { proto_field_set_u32
(hdr
, IP4_DADDR
, $5.s_addr
); }
766 | K_SADDR skip_white
'=' skip_white ip4_addr
767 { proto_field_set_u32
(hdr
, IP4_SADDR
, $5.s_addr
); }
768 | K_PROT skip_white
'=' skip_white number
769 { proto_field_set_u8
(hdr
, IP4_PROTO
, $5); }
770 | K_TTL skip_white
'=' skip_white number
771 { proto_field_set_u8
(hdr
, IP4_TTL
, $5); }
772 | K_DSCP skip_white
'=' skip_white number
773 { proto_field_set_u8
(hdr
, IP4_DSCP
, $5); }
774 | K_ECN skip_white
'=' skip_white number
775 { proto_field_set_u8
(hdr
, IP4_ECN
, $5); }
776 | K_TOS skip_white
'=' skip_white number
777 { proto_field_set_u8
(hdr
, IP4_TOS
, $5); }
778 | K_LEN skip_white
'=' skip_white number
779 { proto_field_set_be16
(hdr
, IP4_LEN
, $5); }
780 | K_ID skip_white
'=' skip_white number
781 { proto_field_set_be16
(hdr
, IP4_ID
, $5); }
782 | K_FLAGS skip_white
'=' skip_white number
783 { proto_field_set_be16
(hdr
, IP4_FLAGS
, $5); }
784 | K_DF
{ proto_field_set_be16
(hdr
, IP4_DF
, 1); }
785 | K_MF
{ proto_field_set_be16
(hdr
, IP4_MF
, 1); }
786 | K_FRAG skip_white
'=' skip_white number
787 { proto_field_set_be16
(hdr
, IP4_FRAG_OFFS
, $5); }
788 | K_CSUM skip_white
'=' skip_white number
789 { proto_field_set_be16
(hdr
, IP4_CSUM
, $5); }
793 : K_IP4
{ proto_add
(PROTO_IP4
); }
797 : ip6
'(' ip6_param_list
')' { }
803 | ip6_field delimiter ip6_param_list
{ }
812 : K_VER skip_white
'=' skip_white number
813 { proto_field_set_be32
(hdr
, IP6_VER
, $5); }
814 | K_TC skip_white
'=' skip_white number
815 { proto_field_set_be32
(hdr
, IP6_CLASS
, $5); }
816 | K_FLOW skip_white
'=' skip_white number
817 { proto_field_set_be32
(hdr
, IP6_FLOW_LBL
, $5); }
818 | K_LEN skip_white
'=' skip_white number
819 { proto_field_set_be16
(hdr
, IP6_LEN
, $5); }
820 | K_NEXT_HDR skip_white
'=' skip_white number
821 { proto_field_set_u8
(hdr
, IP6_NEXT_HDR
, $5); }
822 | ip6_hop_limit skip_white
'=' skip_white number
823 { proto_field_set_u8
(hdr
, IP6_HOP_LIMIT
, $5); }
824 | K_SADDR skip_white
'=' skip_white ip6_addr
825 { proto_field_set_bytes
(hdr
, IP6_SADDR
, (uint8_t *)&($5.s6_addr
)); }
826 | K_DADDR skip_white
'=' skip_white ip6_addr
827 { proto_field_set_bytes
(hdr
, IP6_DADDR
, (uint8_t *)&($5.s6_addr
)); }
831 : K_IP6
{ proto_add
(PROTO_IP6
); }
835 : icmp4
'(' icmp4_param_list
')' { }
841 | icmp4_field delimiter icmp4_param_list
{ }
845 : K_TYPE skip_white
'=' skip_white number
846 { proto_field_set_u8
(hdr
, ICMPV4_TYPE
, $5); }
847 | K_CODE skip_white
'=' skip_white number
848 { proto_field_set_u8
(hdr
, ICMPV4_CODE
, $5); }
849 | K_ID skip_white
'=' skip_white number
850 { proto_field_set_be16
(hdr
, ICMPV4_ID
, $5); }
851 | K_SEQ skip_white
'=' skip_white number
852 { proto_field_set_be16
(hdr
, ICMPV4_SEQ
, $5); }
853 | K_MTU skip_white
'=' skip_white number
854 { proto_field_set_be16
(hdr
, ICMPV4_MTU
, $5); }
855 | K_ADDR skip_white
'=' skip_white ip4_addr
856 { proto_field_set_u32
(hdr
, ICMPV4_REDIR_ADDR
, $5.s_addr
); }
858 { proto_field_set_u8
(hdr
, ICMPV4_TYPE
, ICMP_ECHO
);
859 proto_field_set_u8
(hdr
, ICMPV4_CODE
, 0); }
861 { proto_field_set_u8
(hdr
, ICMPV4_TYPE
, ICMP_ECHOREPLY
);
862 proto_field_set_u8
(hdr
, ICMPV4_CODE
, 0); }
866 : K_ICMP4
{ proto_add
(PROTO_ICMP4
); }
870 : icmp6
'(' icmp6_param_list
')' { }
875 | icmp6_field delimiter icmp6_param_list
{ }
879 : K_TYPE skip_white
'=' skip_white number
880 { proto_field_set_u8
(hdr
, ICMPV6_TYPE
, $5); }
881 | K_TYPE skip_white
'=' K_ECHO_REQUEST
882 { proto_field_set_u8
(hdr
, ICMPV6_TYPE
, ICMPV6_ECHO_REQUEST
); }
884 { proto_field_set_u8
(hdr
, ICMPV6_TYPE
, ICMPV6_ECHO_REQUEST
); }
885 | K_TYPE skip_white
'=' K_ECHO_REPLY
886 { proto_field_set_u8
(hdr
, ICMPV6_TYPE
, ICMPV6_ECHO_REPLY
); }
888 { proto_field_set_u8
(hdr
, ICMPV6_TYPE
, ICMPV6_ECHO_REPLY
); }
889 | K_CODE skip_white
'=' skip_white number
890 { proto_field_set_u8
(hdr
, ICMPV6_CODE
, $5); }
891 | K_CSUM skip_white
'=' skip_white number
892 { proto_field_set_be16
(hdr
, ICMPV6_CSUM
, $5); }
896 : K_ICMP6
{ proto_add
(PROTO_ICMP6
); }
900 : udp
'(' udp_param_list
')' { }
906 | udp_field delimiter udp_param_list
{ }
910 : K_SPORT skip_white
'=' skip_white number
911 { proto_field_set_be16
(hdr
, UDP_SPORT
, $5); }
912 | K_DPORT skip_white
'=' skip_white number
913 { proto_field_set_be16
(hdr
, UDP_DPORT
, $5); }
914 | K_LEN skip_white
'=' skip_white number
915 { proto_field_set_be16
(hdr
, UDP_LEN
, $5); }
916 | K_CSUM skip_white
'=' skip_white number
917 { proto_field_set_be16
(hdr
, UDP_CSUM
, $5); }
921 : K_UDP
{ proto_add
(PROTO_UDP
); }
925 : tcp
'(' tcp_param_list
')' { }
931 | tcp_field delimiter tcp_param_list
{ }
935 : K_SPORT skip_white
'=' skip_white number
936 { proto_field_set_be16
(hdr
, TCP_SPORT
, $5); }
937 | K_DPORT skip_white
'=' skip_white number
938 { proto_field_set_be16
(hdr
, TCP_DPORT
, $5); }
939 | K_SEQ skip_white
'=' skip_white number
940 { proto_field_set_be32
(hdr
, TCP_SEQ
, $5); }
941 | K_ACK_SEQ skip_white
'=' skip_white number
942 { proto_field_set_be32
(hdr
, TCP_ACK_SEQ
, $5); }
943 | K_DOFF skip_white
'=' skip_white number
944 { proto_field_set_be16
(hdr
, TCP_DOFF
, $5); }
945 | K_CWR
{ proto_field_set_be16
(hdr
, TCP_CWR
, 1); }
946 | K_ECE
{ proto_field_set_be16
(hdr
, TCP_ECE
, 1); }
947 | K_URG
{ proto_field_set_be16
(hdr
, TCP_URG
, 1); }
948 | K_ACK
{ proto_field_set_be16
(hdr
, TCP_ACK
, 1); }
949 | K_PSH
{ proto_field_set_be16
(hdr
, TCP_PSH
, 1); }
950 | K_RST
{ proto_field_set_be16
(hdr
, TCP_RST
, 1); }
951 | K_SYN
{ proto_field_set_be16
(hdr
, TCP_SYN
, 1); }
952 | K_FIN
{ proto_field_set_be16
(hdr
, TCP_FIN
, 1); }
953 | K_WINDOW skip_white
'=' skip_white number
954 { proto_field_set_be16
(hdr
, TCP_WINDOW
, $5); }
955 | K_CSUM skip_white
'=' skip_white number
956 { proto_field_set_be16
(hdr
, TCP_CSUM
, $5); }
957 | K_URG_PTR skip_white
'=' skip_white number
958 { proto_field_set_be16
(hdr
, TCP_URG_PTR
, $5); }
962 : K_TCP
{ proto_add
(PROTO_TCP
); }
967 static void finalize_packet
(void)
969 /* XXX hack ... we allocated one packet pointer too much */
974 static void dump_conf
(void)
978 for
(i
= 0; i
< plen
; ++i
) {
979 printf
("[%zu] pkt\n", i
);
980 printf
(" len %zu cnts %zu rnds %zu\n",
986 for
(j
= 0; j
< packets
[i
].len
; ++j
)
987 printf
("%02x ", packets
[i
].payload
[j
]);
990 for
(j
= 0; j
< packet_dyn
[i
].clen
; ++j
)
991 printf
(" cnt%zu [%u,%u], inc %u, off %jd type %s\n", j
,
992 packet_dyn
[i
].cnt
[j
].min
,
993 packet_dyn
[i
].cnt
[j
].max
,
994 packet_dyn
[i
].cnt
[j
].inc
,
995 (intmax_t)packet_dyn
[i
].cnt
[j
].off
,
996 packet_dyn
[i
].cnt
[j
].type
== TYPE_INC ?
999 for
(j
= 0; j
< packet_dyn
[i
].rlen
; ++j
)
1000 printf
(" rnd%zu off %jd\n", j
,
1001 (intmax_t)packet_dyn
[i
].rnd
[j
].off
);
1005 void cleanup_packets
(void)
1009 for
(i
= 0; i
< plen
; ++i
) {
1010 struct packet
*pkt
= &packets
[i
];
1013 xfree
(pkt
->payload
);
1015 for
(j
= 0; j
< pkt
->headers_count
; j
++) {
1016 struct proto_hdr
*hdr
= pkt
->headers
[j
];
1026 for
(i
= 0; i
< dlen
; ++i
) {
1027 free
(packet_dyn
[i
].cnt
);
1028 free
(packet_dyn
[i
].rnd
);
1034 void compile_packets
(char *file
, bool verbose
, unsigned int cpu
,
1035 bool invoke_cpp
, char *const cpp_argv
[])
1040 memset
(tmp_file
, 0, sizeof
(tmp_file
));
1044 if
(cpp_exec
(file
, tmp_file
, sizeof
(tmp_file
), cpp_argv
)) {
1045 fprintf
(stderr
, "Failed to invoke C preprocessor!\n");
1051 if
(!strncmp
("-", file
, strlen
("-")))
1054 yyin
= fopen
(file
, "r");
1056 fprintf
(stderr
, "Cannot open %s: %s!\n", file
, strerror
(errno
));
1065 if
(our_cpu
== 0 && verbose
)
1070 if
(yyin
&& yyin
!= stdin
)
1079 void compile_packets_str
(char *str
, bool verbose
, unsigned int cpu
)
1086 yy_scan_string
(str
);
1091 if
(our_cpu
== 0 && verbose
)
1102 void yyerror(const char *err
)
1104 fprintf
(stderr
, "Syntax error at line %d, char '%s': %s\n", yylineno
, yytext
, err
);