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 */
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>
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"
40 #define YYERROR_VERBOSE 0
42 #define YYENABLE_NLS 1
43 #define YYLTYPE_IS_TRIVIAL 1
47 extern
int yylex(void);
48 extern
void yy_scan_string
(char *);
49 extern
void yylex_destroy
();
50 extern
void yyerror(const char *);
54 extern
struct packet
*packets
;
57 #define packet_last (plen - 1)
59 #define payload_last (packets[packet_last].len - 1)
61 extern
struct packet_dyn
*packet_dyn
;
64 #define packetd_last (dlen - 1)
66 #define packetdc_last (packet_dyn[packetd_last].clen - 1)
67 #define packetdr_last (packet_dyn[packetd_last].rlen - 1)
68 #define packetds_last (packet_dyn[packetd_last].slen - 1)
70 static int our_cpu
, min_cpu
= -1, max_cpu
= -1;
72 static struct proto_hdr
*hdr
;
74 static inline
int test_ignore
(void)
76 if
(min_cpu
< 0 && max_cpu
< 0)
78 else if
(max_cpu
>= our_cpu
&& min_cpu
<= our_cpu
)
84 static inline
void __init_new_packet_slot
(struct packet
*slot
)
90 static inline
void __init_new_counter_slot
(struct packet_dyn
*slot
)
96 static inline
void __init_new_randomizer_slot
(struct packet_dyn
*slot
)
102 static inline
void __init_new_csum_slot
(struct packet_dyn
*slot
)
108 static inline
void __setup_new_counter
(struct counter
*c
, uint8_t start
,
109 uint8_t stop
, uint8_t stepping
,
115 c
->val
= (type
== TYPE_INC
) ? start
: stop
;
116 c
->off
= payload_last
;
120 static inline
void __setup_new_randomizer
(struct randomizer
*r
)
122 r
->off
= payload_last
;
125 static inline
void __setup_new_csum16
(struct csum16
*s
, off_t from
, off_t to
,
128 s
->off
= payload_last
- 1;
134 static void realloc_packet
(void)
140 packets
= xrealloc
(packets
, plen
* sizeof
(*packets
));
142 __init_new_packet_slot
(&packets
[packet_last
]);
145 packet_dyn
= xrealloc
(packet_dyn
, dlen
* sizeof
(*packet_dyn
));
147 __init_new_counter_slot
(&packet_dyn
[packetd_last
]);
148 __init_new_randomizer_slot
(&packet_dyn
[packetd_last
]);
149 __init_new_csum_slot
(&packet_dyn
[packetd_last
]);
152 struct packet
*current_packet
(void)
154 return
&packets
[packet_last
];
157 uint32_t current_packet_id
(void)
162 struct packet
*packet_get
(uint32_t id
)
167 static void set_byte
(uint8_t val
)
169 struct packet
*pkt
= &packets
[packet_last
];
175 pkt
->payload
= xrealloc
(pkt
->payload
, pkt
->len
);
176 pkt
->payload
[payload_last
] = val
;
179 static void set_multi_byte
(uint8_t *s
, size_t len
)
183 for
(i
= 0; i
< len
; ++i
)
187 void set_fill
(uint8_t val
, size_t len
)
190 struct packet
*pkt
= &packets
[packet_last
];
196 pkt
->payload
= xrealloc
(pkt
->payload
, pkt
->len
);
197 for
(i
= 0; i
< len
; ++i
)
198 pkt
->payload
[payload_last
- i
] = val
;
201 static void __set_csum16_dynamic
(size_t from
, size_t to
, enum csum which
)
203 struct packet
*pkt
= &packets
[packet_last
];
204 struct packet_dyn
*pktd
= &packet_dyn
[packetd_last
];
207 pkt
->payload
= xrealloc
(pkt
->payload
, pkt
->len
);
210 pktd
->csum
= xrealloc
(pktd
->csum
, pktd
->slen
* sizeof
(struct csum16
));
212 __setup_new_csum16
(&pktd
->csum
[packetds_last
], from
, to
, which
);
215 static void __set_csum16_static
(size_t from
, size_t to
, enum csum which __maybe_unused
)
217 struct packet
*pkt
= &packets
[packet_last
];
221 sum
= htons
(calc_csum
(pkt
->payload
+ from
, to
- from
));
222 psum
= (uint8_t *) &sum
;
228 static inline
bool is_dynamic_csum
(enum csum which
)
241 static void set_csum16
(size_t from
, size_t to
, enum csum which
)
243 struct packet
*pkt
= &packets
[packet_last
];
244 struct packet_dyn
*pktd
= &packet_dyn
[packetd_last
];
256 bug_on
(!(from
< to
));
258 if
(packet_dyn_has_elems
(pktd
) || to
>= pkt
->len || is_dynamic_csum
(which
))
259 __set_csum16_dynamic
(from
, to
, which
);
261 __set_csum16_static
(from
, to
, which
);
264 static void set_rnd
(size_t len
)
267 struct packet
*pkt
= &packets
[packet_last
];
273 pkt
->payload
= xrealloc
(pkt
->payload
, pkt
->len
);
274 for
(i
= 0; i
< len
; ++i
)
275 pkt
->payload
[payload_last
- i
] = (uint8_t) rand
();
278 static void set_sequential_inc
(uint8_t start
, size_t len
, uint8_t stepping
)
281 struct packet
*pkt
= &packets
[packet_last
];
287 pkt
->payload
= xrealloc
(pkt
->payload
, pkt
->len
);
288 for
(i
= 0; i
< len
; ++i
) {
289 off_t off
= len
- 1 - i
;
291 pkt
->payload
[payload_last
- off
] = start
;
296 static void set_sequential_dec
(uint8_t start
, size_t len
, uint8_t stepping
)
299 struct packet
*pkt
= &packets
[packet_last
];
305 pkt
->payload
= xrealloc
(pkt
->payload
, pkt
->len
);
306 for
(i
= 0; i
< len
; ++i
) {
307 int off
= len
- 1 - i
;
309 pkt
->payload
[payload_last
- off
] = start
;
314 static void set_dynamic_rnd
(void)
316 struct packet
*pkt
= &packets
[packet_last
];
317 struct packet_dyn
*pktd
= &packet_dyn
[packetd_last
];
323 pkt
->payload
= xrealloc
(pkt
->payload
, pkt
->len
);
326 pktd
->rnd
= xrealloc
(pktd
->rnd
, pktd
->rlen
* sizeof
(struct randomizer
));
328 __setup_new_randomizer
(&pktd
->rnd
[packetdr_last
]);
331 static void set_dynamic_incdec
(uint8_t start
, uint8_t stop
, uint8_t stepping
,
334 struct packet
*pkt
= &packets
[packet_last
];
335 struct packet_dyn
*pktd
= &packet_dyn
[packetd_last
];
341 pkt
->payload
= xrealloc
(pkt
->payload
, pkt
->len
);
344 pktd
->cnt
= xrealloc
(pktd
->cnt
, pktd
->clen
* sizeof
(struct counter
));
346 __setup_new_counter
(&pktd
->cnt
[packetdc_last
], start
, stop
, stepping
, type
);
349 static void proto_add
(enum proto_id pid
)
351 hdr
= proto_header_push
(pid
);
357 struct in_addr ip4_addr
;
358 struct in6_addr ip6_addr
;
359 long long int number
;
364 %token K_COMMENT K_FILL K_RND K_SEQINC K_SEQDEC K_DRND K_DINC K_DDEC K_WHITE
365 %token K_CPU K_CSUMIP K_CSUMUDP K_CSUMTCP K_CSUMUDP6 K_CSUMTCP6 K_CONST8 K_CONST16 K_CONST32 K_CONST64
367 %token K_DADDR K_SADDR K_ETYPE K_TYPE
368 %token K_OPER K_SHA K_SPA K_THA K_TPA K_REQUEST K_REPLY K_PTYPE K_HTYPE
369 %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
370 %token K_FLOW K_NEXT_HDR K_HOP_LIMIT
371 %token K_CODE K_ECHO_REQUEST K_ECHO_REPLY
372 %token K_SPORT K_DPORT
373 %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
374 %token K_TPID K_TCI K_PCP K_DEI K_1Q K_1AD
375 %token K_LABEL K_TC K_LAST K_EXP
383 %token K_ICMP4 K_ICMP6
386 %token
',' '{' '}' '(' ')' '[' ']' ':' '-' '+' '*' '/' '%' '&' '|' '<' '>' '^'
388 %token number
string mac ip4_addr ip6_addr
390 %type
<number
> number expression
393 %type
<ip4_addr
> ip4_addr
394 %type
<ip6_addr
> ip6_addr
396 %left
'-' '+' '*' '/' '%' '&' '|' '<' '>' '^'
403 | packets inline_comment
{ }
404 | packets K_WHITE
{ }
424 | delimiter_nowhite
{ }
432 : '{' noenforce_white payload noenforce_white
'}' {
433 min_cpu
= max_cpu
= -1;
435 proto_packet_finish
();
439 | K_CPU
'(' number cpu_delim number
')' ':' noenforce_white
'{' noenforce_white payload noenforce_white
'}' {
443 if
(min_cpu
> max_cpu
) {
450 proto_packet_finish
();
454 | K_CPU
'(' number
')' ':' noenforce_white
'{' noenforce_white payload noenforce_white
'}' {
455 min_cpu
= max_cpu
= $3;
457 proto_packet_finish
();
465 | payload elem_delimiter
{ }
469 : delimiter_nowhite
{ }
478 : number
{ set_byte
((uint8_t) $1); }
479 |
string { set_multi_byte
((uint8_t *) $1 + 1, strlen
($1) - 2); }
489 | proto
{ proto_header_finish
(hdr
); }
496 | expression
'+' expression
498 | expression
'-' expression
500 | expression
'*' expression
502 | expression
'/' expression
504 | expression
'%' expression
506 | expression
'&' expression
508 | expression
'|' expression
510 | expression
'^' expression
512 | expression
'<' '<' expression
514 | expression
'>' '>' expression
523 : K_FILL
'(' number delimiter number
')'
524 { set_fill
($3, $5); }
528 : K_CONST8
'(' expression
')'
529 { set_byte
((uint8_t) $3); }
530 | K_CONST16
'(' expression
')' {
531 uint16_t __c
= cpu_to_be16
((uint16_t) $3);
533 set_multi_byte
((uint8_t *) &__c
, sizeof
(__c
));
535 | K_CONST32
'(' expression
')' {
536 uint32_t __c
= cpu_to_be32
((uint32_t) $3);
538 set_multi_byte
((uint8_t *) &__c
, sizeof
(__c
));
540 | K_CONST64
'(' expression
')' {
541 uint64_t __c
= cpu_to_be64
((uint64_t) $3);
543 set_multi_byte
((uint8_t *) &__c
, sizeof
(__c
));
548 : K_RND
'(' number
')'
553 : K_CSUMIP
'(' number delimiter number
')'
554 { set_csum16
($3, $5, CSUM_IP
); }
555 | K_CSUMTCP
'(' number delimiter number
')'
556 { set_csum16
($3, $5, CSUM_TCP
); }
557 | K_CSUMUDP
'(' number delimiter number
')'
558 { set_csum16
($3, $5, CSUM_UDP
); }
559 | K_CSUMTCP6
'(' number delimiter number
')'
560 { set_csum16
($3, $5, CSUM_TCP6
); }
561 | K_CSUMUDP6
'(' number delimiter number
')'
562 { set_csum16
($3, $5, CSUM_UDP6
); }
566 : K_SEQINC
'(' number delimiter number
')'
567 { set_sequential_inc
($3, $5, 1); }
568 | K_SEQINC
'(' number delimiter number delimiter number
')'
569 { set_sequential_inc
($3, $5, $7); }
573 : K_SEQDEC
'(' number delimiter number
')'
574 { set_sequential_dec
($3, $5, 1); }
575 | K_SEQDEC
'(' number delimiter number delimiter number
')'
576 { set_sequential_dec
($3, $5, $7); }
581 { set_dynamic_rnd
(); }
582 | K_DRND
'(' number
')'
585 for
(i
= 0; i
< max
; ++i
)
591 : K_DINC
'(' number delimiter number
')'
592 { set_dynamic_incdec
($3, $5, 1, TYPE_INC
); }
593 | K_DINC
'(' number delimiter number delimiter number
')'
594 { set_dynamic_incdec
($3, $5, $7, TYPE_INC
); }
598 : K_DDEC
'(' number delimiter number
')'
599 { set_dynamic_incdec
($3, $5, 1, TYPE_DEC
); }
600 | K_DDEC
'(' number delimiter number delimiter number
')'
601 { set_dynamic_incdec
($3, $5, $7, TYPE_DEC
); }
618 : eth
'(' eth_param_list
')' { }
622 : K_ETH
{ proto_add
(PROTO_ETH
); }
628 | eth_field delimiter eth_param_list
{ }
638 : K_DADDR skip_white
'=' skip_white mac
639 { proto_field_set_bytes
(hdr
, ETH_DST_ADDR
, $5); }
640 | K_SADDR skip_white
'=' skip_white mac
641 { proto_field_set_bytes
(hdr
, ETH_SRC_ADDR
, $5); }
642 | eth_type skip_white
'=' skip_white number
643 { proto_field_set_be16
(hdr
, ETH_TYPE
, $5); }
647 : vlan
'(' vlan_param_list
')' { }
651 : K_VLAN
{ proto_add
(PROTO_VLAN
); }
657 | vlan_field delimiter vlan_param_list
{ }
666 : vlan_type skip_white
'=' skip_white number
667 { proto_field_set_be16
(hdr
, VLAN_TPID
, $5); }
669 { proto_field_set_be16
(hdr
, VLAN_TPID
, ETH_P_8021Q
); }
671 { proto_field_set_be16
(hdr
, VLAN_TPID
, ETH_P_8021AD
); }
672 | K_TCI skip_white
'=' skip_white number
673 { proto_field_set_be16
(hdr
, VLAN_TCI
, $5); }
674 | K_PCP skip_white
'=' skip_white number
675 { proto_field_set_be16
(hdr
, VLAN_PCP
, $5); }
676 | K_DEI skip_white
'=' skip_white number
677 { proto_field_set_be16
(hdr
, VLAN_DEI
, $5); }
678 | K_ID skip_white
'=' skip_white number
679 { proto_field_set_be16
(hdr
, VLAN_VID
, $5); }
683 : mpls
'(' mpls_param_list
')' { }
687 : K_MPLS
{ proto_add
(PROTO_MPLS
); }
693 | mpls_field delimiter mpls_param_list
{ }
702 : K_LABEL skip_white
'=' skip_white number
703 { proto_field_set_be32
(hdr
, MPLS_LABEL
, $5); }
704 | mpls_tc skip_white
'=' skip_white number
705 { proto_field_set_be32
(hdr
, MPLS_TC
, $5); }
706 | K_LAST skip_white
'=' skip_white number
707 { proto_field_set_be32
(hdr
, MPLS_LAST
, $5); }
708 | K_TTL skip_white
'=' skip_white number
709 { proto_field_set_be32
(hdr
, MPLS_TTL
, $5); }
713 : arp
'(' arp_param_list
')' { }
719 | arp_field delimiter arp_param_list
{ }
723 : K_OPER skip_white
'=' skip_white K_REQUEST
724 { proto_field_set_be16
(hdr
, ARP_OPER
, ARPOP_REQUEST
); }
725 | K_OPER skip_white
'=' skip_white K_REPLY
726 { proto_field_set_be16
(hdr
, ARP_OPER
, ARPOP_REPLY
); }
727 | K_OPER skip_white
'=' skip_white number
728 { proto_field_set_be16
(hdr
, ARP_OPER
, $5); }
730 { proto_field_set_be16
(hdr
, ARP_OPER
, ARPOP_REQUEST
); }
732 { proto_field_set_be16
(hdr
, ARP_OPER
, ARPOP_REPLY
); }
733 | K_HTYPE skip_white
'=' skip_white number
734 { proto_field_set_be16
(hdr
, ARP_HTYPE
, $5); }
735 | K_PTYPE skip_white
'=' skip_white number
736 { proto_field_set_be16
(hdr
, ARP_PTYPE
, $5); }
737 | K_SHA skip_white
'=' skip_white mac
738 { proto_field_set_bytes
(hdr
, ARP_SHA
, $5); }
739 | K_THA skip_white
'=' skip_white mac
740 { proto_field_set_bytes
(hdr
, ARP_THA
, $5); }
741 | K_SPA skip_white
'=' skip_white ip4_addr
742 { proto_field_set_u32
(hdr
, ARP_SPA
, $5.s_addr
); }
743 | K_TPA skip_white
'=' skip_white ip4_addr
744 { proto_field_set_u32
(hdr
, ARP_TPA
, $5.s_addr
); }
747 : K_ARP
{ proto_add
(PROTO_ARP
); }
751 : ip4
'(' ip4_param_list
')' { }
757 | ip4_field delimiter ip4_param_list
{ }
761 : K_VER skip_white
'=' skip_white number
762 { proto_field_set_u8
(hdr
, IP4_VER
, $5); }
763 | K_IHL skip_white
'=' skip_white number
764 { proto_field_set_u8
(hdr
, IP4_IHL
, $5); }
765 | K_DADDR skip_white
'=' skip_white ip4_addr
766 { proto_field_set_u32
(hdr
, IP4_DADDR
, $5.s_addr
); }
767 | K_SADDR skip_white
'=' skip_white ip4_addr
768 { proto_field_set_u32
(hdr
, IP4_SADDR
, $5.s_addr
); }
769 | K_PROT skip_white
'=' skip_white number
770 { proto_field_set_u8
(hdr
, IP4_PROTO
, $5); }
771 | K_TTL skip_white
'=' skip_white number
772 { proto_field_set_u8
(hdr
, IP4_TTL
, $5); }
773 | K_DSCP skip_white
'=' skip_white number
774 { proto_field_set_u8
(hdr
, IP4_DSCP
, $5); }
775 | K_ECN skip_white
'=' skip_white number
776 { proto_field_set_u8
(hdr
, IP4_ECN
, $5); }
777 | K_TOS skip_white
'=' skip_white number
778 { proto_field_set_u8
(hdr
, IP4_TOS
, $5); }
779 | K_LEN skip_white
'=' skip_white number
780 { proto_field_set_be16
(hdr
, IP4_LEN
, $5); }
781 | K_ID skip_white
'=' skip_white number
782 { proto_field_set_be16
(hdr
, IP4_ID
, $5); }
783 | K_FLAGS skip_white
'=' skip_white number
784 { proto_field_set_be16
(hdr
, IP4_FLAGS
, $5); }
785 | K_DF
{ proto_field_set_be16
(hdr
, IP4_DF
, 1); }
786 | K_MF
{ proto_field_set_be16
(hdr
, IP4_MF
, 1); }
787 | K_FRAG skip_white
'=' skip_white number
788 { proto_field_set_be16
(hdr
, IP4_FRAG_OFFS
, $5); }
789 | K_CSUM skip_white
'=' skip_white number
790 { proto_field_set_be16
(hdr
, IP4_CSUM
, $5); }
794 : K_IP4
{ proto_add
(PROTO_IP4
); }
798 : ip6
'(' ip6_param_list
')' { }
804 | ip6_field delimiter ip6_param_list
{ }
813 : K_VER skip_white
'=' skip_white number
814 { proto_field_set_be32
(hdr
, IP6_VER
, $5); }
815 | K_TC skip_white
'=' skip_white number
816 { proto_field_set_be32
(hdr
, IP6_CLASS
, $5); }
817 | K_FLOW skip_white
'=' skip_white number
818 { proto_field_set_be32
(hdr
, IP6_FLOW_LBL
, $5); }
819 | K_LEN skip_white
'=' skip_white number
820 { proto_field_set_be16
(hdr
, IP6_LEN
, $5); }
821 | K_NEXT_HDR skip_white
'=' skip_white number
822 { proto_field_set_u8
(hdr
, IP6_NEXT_HDR
, $5); }
823 | ip6_hop_limit skip_white
'=' skip_white number
824 { proto_field_set_u8
(hdr
, IP6_HOP_LIMIT
, $5); }
825 | K_SADDR skip_white
'=' skip_white ip6_addr
826 { proto_field_set_bytes
(hdr
, IP6_SADDR
, (uint8_t *)&($5.s6_addr
)); }
827 | K_DADDR skip_white
'=' skip_white ip6_addr
828 { proto_field_set_bytes
(hdr
, IP6_DADDR
, (uint8_t *)&($5.s6_addr
)); }
832 : K_IP6
{ proto_add
(PROTO_IP6
); }
836 : icmp4
'(' icmp4_param_list
')' { }
842 | icmp4_field delimiter icmp4_param_list
{ }
846 : K_TYPE skip_white
'=' skip_white number
847 { proto_field_set_u8
(hdr
, ICMPV4_TYPE
, $5); }
848 | K_CODE skip_white
'=' skip_white number
849 { proto_field_set_u8
(hdr
, ICMPV4_CODE
, $5); }
850 | K_ID skip_white
'=' skip_white number
851 { proto_field_set_be16
(hdr
, ICMPV4_ID
, $5); }
852 | K_SEQ skip_white
'=' skip_white number
853 { proto_field_set_be16
(hdr
, ICMPV4_SEQ
, $5); }
854 | K_MTU skip_white
'=' skip_white number
855 { proto_field_set_be16
(hdr
, ICMPV4_MTU
, $5); }
856 | K_ADDR skip_white
'=' skip_white ip4_addr
857 { proto_field_set_u32
(hdr
, ICMPV4_REDIR_ADDR
, $5.s_addr
); }
859 { proto_field_set_u8
(hdr
, ICMPV4_TYPE
, ICMP_ECHO
);
860 proto_field_set_u8
(hdr
, ICMPV4_CODE
, 0); }
862 { proto_field_set_u8
(hdr
, ICMPV4_TYPE
, ICMP_ECHOREPLY
);
863 proto_field_set_u8
(hdr
, ICMPV4_CODE
, 0); }
867 : K_ICMP4
{ proto_add
(PROTO_ICMP4
); }
871 : icmp6
'(' icmp6_param_list
')' { }
876 | icmp6_field delimiter icmp6_param_list
{ }
880 : K_TYPE skip_white
'=' skip_white number
881 { proto_field_set_u8
(hdr
, ICMPV6_TYPE
, $5); }
882 | K_TYPE skip_white
'=' K_ECHO_REQUEST
883 { proto_field_set_u8
(hdr
, ICMPV6_TYPE
, ICMPV6_ECHO_REQUEST
); }
885 { proto_field_set_u8
(hdr
, ICMPV6_TYPE
, ICMPV6_ECHO_REQUEST
); }
886 | K_TYPE skip_white
'=' K_ECHO_REPLY
887 { proto_field_set_u8
(hdr
, ICMPV6_TYPE
, ICMPV6_ECHO_REPLY
); }
889 { proto_field_set_u8
(hdr
, ICMPV6_TYPE
, ICMPV6_ECHO_REPLY
); }
890 | K_CODE skip_white
'=' skip_white number
891 { proto_field_set_u8
(hdr
, ICMPV6_CODE
, $5); }
892 | K_CSUM skip_white
'=' skip_white number
893 { proto_field_set_be16
(hdr
, ICMPV6_CSUM
, $5); }
897 : K_ICMP6
{ proto_add
(PROTO_ICMP6
); }
901 : udp
'(' udp_param_list
')' { }
907 | udp_field delimiter udp_param_list
{ }
911 : K_SPORT skip_white
'=' skip_white number
912 { proto_field_set_be16
(hdr
, UDP_SPORT
, $5); }
913 | K_DPORT skip_white
'=' skip_white number
914 { proto_field_set_be16
(hdr
, UDP_DPORT
, $5); }
915 | K_LEN skip_white
'=' skip_white number
916 { proto_field_set_be16
(hdr
, UDP_LEN
, $5); }
917 | K_CSUM skip_white
'=' skip_white number
918 { proto_field_set_be16
(hdr
, UDP_CSUM
, $5); }
922 : K_UDP
{ proto_add
(PROTO_UDP
); }
926 : tcp
'(' tcp_param_list
')' { }
932 | tcp_field delimiter tcp_param_list
{ }
936 : K_SPORT skip_white
'=' skip_white number
937 { proto_field_set_be16
(hdr
, TCP_SPORT
, $5); }
938 | K_DPORT skip_white
'=' skip_white number
939 { proto_field_set_be16
(hdr
, TCP_DPORT
, $5); }
940 | K_SEQ skip_white
'=' skip_white number
941 { proto_field_set_be32
(hdr
, TCP_SEQ
, $5); }
942 | K_ACK_SEQ skip_white
'=' skip_white number
943 { proto_field_set_be32
(hdr
, TCP_ACK_SEQ
, $5); }
944 | K_DOFF skip_white
'=' skip_white number
945 { proto_field_set_be16
(hdr
, TCP_DOFF
, $5); }
946 | K_CWR
{ proto_field_set_be16
(hdr
, TCP_CWR
, 1); }
947 | K_ECE
{ proto_field_set_be16
(hdr
, TCP_ECE
, 1); }
948 | K_URG
{ proto_field_set_be16
(hdr
, TCP_URG
, 1); }
949 | K_ACK
{ proto_field_set_be16
(hdr
, TCP_ACK
, 1); }
950 | K_PSH
{ proto_field_set_be16
(hdr
, TCP_PSH
, 1); }
951 | K_RST
{ proto_field_set_be16
(hdr
, TCP_RST
, 1); }
952 | K_SYN
{ proto_field_set_be16
(hdr
, TCP_SYN
, 1); }
953 | K_FIN
{ proto_field_set_be16
(hdr
, TCP_FIN
, 1); }
954 | K_WINDOW skip_white
'=' skip_white number
955 { proto_field_set_be16
(hdr
, TCP_WINDOW
, $5); }
956 | K_CSUM skip_white
'=' skip_white number
957 { proto_field_set_be16
(hdr
, TCP_CSUM
, $5); }
958 | K_URG_PTR skip_white
'=' skip_white number
959 { proto_field_set_be16
(hdr
, TCP_URG_PTR
, $5); }
963 : K_TCP
{ proto_add
(PROTO_TCP
); }
968 static void finalize_packet
(void)
970 /* XXX hack ... we allocated one packet pointer too much */
975 static void dump_conf
(void)
979 for
(i
= 0; i
< plen
; ++i
) {
980 printf
("[%zu] pkt\n", i
);
981 printf
(" len %zu cnts %zu rnds %zu\n",
987 for
(j
= 0; j
< packets
[i
].len
; ++j
)
988 printf
("%02x ", packets
[i
].payload
[j
]);
991 for
(j
= 0; j
< packet_dyn
[i
].clen
; ++j
)
992 printf
(" cnt%zu [%u,%u], inc %u, off %jd type %s\n", j
,
993 packet_dyn
[i
].cnt
[j
].min
,
994 packet_dyn
[i
].cnt
[j
].max
,
995 packet_dyn
[i
].cnt
[j
].inc
,
996 (intmax_t)packet_dyn
[i
].cnt
[j
].off
,
997 packet_dyn
[i
].cnt
[j
].type
== TYPE_INC ?
1000 for
(j
= 0; j
< packet_dyn
[i
].rlen
; ++j
)
1001 printf
(" rnd%zu off %jd\n", j
,
1002 (intmax_t)packet_dyn
[i
].rnd
[j
].off
);
1006 void cleanup_packets
(void)
1010 for
(i
= 0; i
< plen
; ++i
) {
1011 struct packet
*pkt
= &packets
[i
];
1014 xfree
(pkt
->payload
);
1016 for
(j
= 0; j
< pkt
->headers_count
; j
++) {
1017 struct proto_hdr
*hdr
= pkt
->headers
[j
];
1027 for
(i
= 0; i
< dlen
; ++i
) {
1028 free
(packet_dyn
[i
].cnt
);
1029 free
(packet_dyn
[i
].rnd
);
1035 void compile_packets
(char *file
, bool verbose
, unsigned int cpu
,
1036 bool invoke_cpp
, char *const cpp_argv
[])
1041 if
(access
(file
, R_OK
)) {
1042 fprintf
(stderr
, "Cannot access %s: %s!\n", file
, strerror
(errno
));
1046 memset
(tmp_file
, 0, sizeof
(tmp_file
));
1050 if
(cpp_exec
(file
, tmp_file
, sizeof
(tmp_file
), cpp_argv
)) {
1051 fprintf
(stderr
, "Failed to invoke C preprocessor!\n");
1057 if
(!strncmp
("-", file
, strlen
("-")))
1060 yyin
= fopen
(file
, "r");
1062 fprintf
(stderr
, "Cannot open %s: %s!\n", file
, strerror
(errno
));
1071 if
(our_cpu
== 0 && verbose
)
1076 if
(yyin
&& yyin
!= stdin
)
1085 void compile_packets_str
(char *str
, bool verbose
, unsigned int cpu
)
1092 yy_scan_string
(str
);
1097 if
(our_cpu
== 0 && verbose
)
1108 void yyerror(const char *err
)
1110 fprintf
(stderr
, "Syntax error at line %d, char '%s': %s\n", yylineno
, yytext
, err
);