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 enum field_expr_type_t
{
80 struct proto_field_expr
{
81 enum field_expr_type_t type
;
82 struct proto_field
*field
;
85 struct in_addr ip4_addr
;
86 struct in6_addr ip6_addr
;
92 static struct proto_field_expr field_expr
;
93 static struct proto_hdr
*hdr
;
95 static inline
int test_ignore
(void)
97 if
(min_cpu
< 0 && max_cpu
< 0)
99 else if
(max_cpu
>= our_cpu
&& min_cpu
<= our_cpu
)
105 static inline
void __init_new_packet_slot
(struct packet
*slot
)
107 slot
->payload
= NULL
;
111 static inline
void __init_new_counter_slot
(struct packet_dyn
*slot
)
117 static inline
void __init_new_randomizer_slot
(struct packet_dyn
*slot
)
123 static inline
void __init_new_csum_slot
(struct packet_dyn
*slot
)
129 static inline
void __setup_new_counter
(struct counter
*c
, uint8_t start
,
130 uint8_t stop
, uint8_t stepping
,
136 c
->val
= (type
== TYPE_INC
) ? start
: stop
;
137 c
->off
= payload_last
;
141 static inline
void __setup_new_randomizer
(struct randomizer
*r
)
143 r
->off
= payload_last
;
146 static inline
void __setup_new_csum16
(struct csum16
*s
, off_t from
, off_t to
,
149 s
->off
= payload_last
- 1;
155 static void realloc_packet
(void)
161 packets
= xrealloc
(packets
, plen
* sizeof
(*packets
));
163 __init_new_packet_slot
(&packets
[packet_last
]);
166 packet_dyn
= xrealloc
(packet_dyn
, dlen
* sizeof
(*packet_dyn
));
168 __init_new_counter_slot
(&packet_dyn
[packetd_last
]);
169 __init_new_randomizer_slot
(&packet_dyn
[packetd_last
]);
170 __init_new_csum_slot
(&packet_dyn
[packetd_last
]);
173 struct packet
*current_packet
(void)
175 return
&packets
[packet_last
];
178 uint32_t current_packet_id
(void)
183 struct packet
*packet_get
(uint32_t id
)
188 static void set_byte
(uint8_t val
)
190 struct packet
*pkt
= &packets
[packet_last
];
196 pkt
->payload
= xrealloc
(pkt
->payload
, pkt
->len
);
197 pkt
->payload
[payload_last
] = val
;
200 static void set_multi_byte
(uint8_t *s
, size_t len
)
204 for
(i
= 0; i
< len
; ++i
)
208 void set_fill
(uint8_t val
, size_t len
)
211 struct packet
*pkt
= &packets
[packet_last
];
217 pkt
->payload
= xrealloc
(pkt
->payload
, pkt
->len
);
218 for
(i
= 0; i
< len
; ++i
)
219 pkt
->payload
[payload_last
- i
] = val
;
222 static void __set_csum16_dynamic
(size_t from
, size_t to
, enum csum which
)
224 struct packet
*pkt
= &packets
[packet_last
];
225 struct packet_dyn
*pktd
= &packet_dyn
[packetd_last
];
228 pkt
->payload
= xrealloc
(pkt
->payload
, pkt
->len
);
231 pktd
->csum
= xrealloc
(pktd
->csum
, pktd
->slen
* sizeof
(struct csum16
));
233 __setup_new_csum16
(&pktd
->csum
[packetds_last
], from
, to
, which
);
236 static void __set_csum16_static
(size_t from
, size_t to
, enum csum which __maybe_unused
)
238 struct packet
*pkt
= &packets
[packet_last
];
242 sum
= htons
(calc_csum
(pkt
->payload
+ from
, to
- from
));
243 psum
= (uint8_t *) &sum
;
249 static inline
bool is_dynamic_csum
(enum csum which
)
262 static void set_csum16
(size_t from
, size_t to
, enum csum which
)
264 struct packet
*pkt
= &packets
[packet_last
];
265 struct packet_dyn
*pktd
= &packet_dyn
[packetd_last
];
277 bug_on
(!(from
< to
));
279 if
(packet_dyn_has_elems
(pktd
) || to
>= pkt
->len || is_dynamic_csum
(which
))
280 __set_csum16_dynamic
(from
, to
, which
);
282 __set_csum16_static
(from
, to
, which
);
285 static void set_rnd
(size_t len
)
288 struct packet
*pkt
= &packets
[packet_last
];
294 pkt
->payload
= xrealloc
(pkt
->payload
, pkt
->len
);
295 for
(i
= 0; i
< len
; ++i
)
296 pkt
->payload
[payload_last
- i
] = (uint8_t) rand
();
299 static void set_sequential_inc
(uint8_t start
, size_t len
, uint8_t stepping
)
302 struct packet
*pkt
= &packets
[packet_last
];
308 pkt
->payload
= xrealloc
(pkt
->payload
, pkt
->len
);
309 for
(i
= 0; i
< len
; ++i
) {
310 off_t off
= len
- 1 - i
;
312 pkt
->payload
[payload_last
- off
] = start
;
317 static void set_sequential_dec
(uint8_t start
, size_t len
, uint8_t stepping
)
320 struct packet
*pkt
= &packets
[packet_last
];
326 pkt
->payload
= xrealloc
(pkt
->payload
, pkt
->len
);
327 for
(i
= 0; i
< len
; ++i
) {
328 int off
= len
- 1 - i
;
330 pkt
->payload
[payload_last
- off
] = start
;
335 static void set_dynamic_rnd
(void)
337 struct packet
*pkt
= &packets
[packet_last
];
338 struct packet_dyn
*pktd
= &packet_dyn
[packetd_last
];
344 pkt
->payload
= xrealloc
(pkt
->payload
, pkt
->len
);
347 pktd
->rnd
= xrealloc
(pktd
->rnd
, pktd
->rlen
* sizeof
(struct randomizer
));
349 __setup_new_randomizer
(&pktd
->rnd
[packetdr_last
]);
352 static void set_dynamic_incdec
(uint8_t start
, uint8_t stop
, uint8_t stepping
,
355 struct packet
*pkt
= &packets
[packet_last
];
356 struct packet_dyn
*pktd
= &packet_dyn
[packetd_last
];
362 pkt
->payload
= xrealloc
(pkt
->payload
, pkt
->len
);
365 pktd
->cnt
= xrealloc
(pktd
->cnt
, pktd
->clen
* sizeof
(struct counter
));
367 __setup_new_counter
(&pktd
->cnt
[packetdc_last
], start
, stop
, stepping
, type
);
370 static void proto_add
(enum proto_id pid
)
372 hdr
= proto_header_push
(pid
);
375 static void proto_field_set
(uint32_t fid
)
377 field_expr.field
= proto_field_by_id
(hdr
, fid
);
380 static void proto_field_expr_eval
(void)
382 struct proto_field
*field
= field_expr.field
;
384 switch
(field_expr.type
) {
385 case FIELD_EXPR_NUMB
:
387 proto_field_set_u8
(hdr
, field
->id
, field_expr.val.number
);
388 else if
(field
->len
== 2)
389 proto_field_set_be16
(hdr
, field
->id
, field_expr.val.number
);
390 else if
(field
->len
== 4)
391 proto_field_set_be32
(hdr
, field
->id
, field_expr.val.number
);
397 proto_field_set_bytes
(hdr
, field
->id
, field_expr.val.bytes
);
400 case FIELD_EXPR_IP4_ADDR
:
401 proto_field_set_u32
(hdr
, field
->id
, field_expr.val.ip4_addr.s_addr
);
404 case FIELD_EXPR_IP6_ADDR
:
405 proto_field_set_bytes
(hdr
, field
->id
,
406 (uint8_t *)&field_expr.val.ip6_addr.s6_addr
);
409 case FIELD_EXPR_UNKNOWN
:
414 memset
(&field_expr
, 0, sizeof
(field_expr
));
420 struct in_addr ip4_addr
;
421 struct in6_addr ip6_addr
;
422 long long int number
;
427 %token K_COMMENT K_FILL K_RND K_SEQINC K_SEQDEC K_DRND K_DINC K_DDEC K_WHITE
428 %token K_CPU K_CSUMIP K_CSUMUDP K_CSUMTCP K_CSUMUDP6 K_CSUMTCP6 K_CONST8 K_CONST16 K_CONST32 K_CONST64
430 %token K_DADDR K_SADDR K_ETYPE K_TYPE
431 %token K_OPER K_SHA K_SPA K_THA K_TPA K_REQUEST K_REPLY K_PTYPE K_HTYPE
432 %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
433 %token K_FLOW K_NEXT_HDR K_HOP_LIMIT
434 %token K_CODE K_ECHO_REQUEST K_ECHO_REPLY
435 %token K_SPORT K_DPORT
436 %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
437 %token K_TPID K_TCI K_PCP K_DEI K_1Q K_1AD
438 %token K_LABEL K_TC K_LAST K_EXP
446 %token K_ICMP4 K_ICMP6
449 %token
',' '{' '}' '(' ')' '[' ']' ':' '-' '+' '*' '/' '%' '&' '|' '<' '>' '^'
451 %token number
string mac ip4_addr ip6_addr
453 %type
<number
> number expression
456 %type
<ip4_addr
> ip4_addr
457 %type
<ip6_addr
> ip6_addr
459 %left
'-' '+' '*' '/' '%' '&' '|' '<' '>' '^'
466 | packets inline_comment
{ }
467 | packets K_WHITE
{ }
487 | delimiter_nowhite
{ }
495 : '{' noenforce_white payload noenforce_white
'}' {
496 min_cpu
= max_cpu
= -1;
498 proto_packet_finish
();
502 | K_CPU
'(' number cpu_delim number
')' ':' noenforce_white
'{' noenforce_white payload noenforce_white
'}' {
506 if
(min_cpu
> max_cpu
) {
513 proto_packet_finish
();
517 | K_CPU
'(' number
')' ':' noenforce_white
'{' noenforce_white payload noenforce_white
'}' {
518 min_cpu
= max_cpu
= $3;
520 proto_packet_finish
();
528 | payload elem_delimiter
{ }
532 : delimiter_nowhite
{ }
541 : number
{ set_byte
((uint8_t) $1); }
542 |
string { set_multi_byte
((uint8_t *) $1 + 1, strlen
($1) - 2); }
552 | proto
{ proto_header_finish
(hdr
); }
559 | expression
'+' expression
561 | expression
'-' expression
563 | expression
'*' expression
565 | expression
'/' expression
567 | expression
'%' expression
569 | expression
'&' expression
571 | expression
'|' expression
573 | expression
'^' expression
575 | expression
'<' '<' expression
577 | expression
'>' '>' expression
586 : K_FILL
'(' number delimiter number
')'
587 { set_fill
($3, $5); }
591 : K_CONST8
'(' expression
')'
592 { set_byte
((uint8_t) $3); }
593 | K_CONST16
'(' expression
')' {
594 uint16_t __c
= cpu_to_be16
((uint16_t) $3);
596 set_multi_byte
((uint8_t *) &__c
, sizeof
(__c
));
598 | K_CONST32
'(' expression
')' {
599 uint32_t __c
= cpu_to_be32
((uint32_t) $3);
601 set_multi_byte
((uint8_t *) &__c
, sizeof
(__c
));
603 | K_CONST64
'(' expression
')' {
604 uint64_t __c
= cpu_to_be64
((uint64_t) $3);
606 set_multi_byte
((uint8_t *) &__c
, sizeof
(__c
));
611 : K_RND
'(' number
')'
616 : K_CSUMIP
'(' number delimiter number
')'
617 { set_csum16
($3, $5, CSUM_IP
); }
618 | K_CSUMTCP
'(' number delimiter number
')'
619 { set_csum16
($3, $5, CSUM_TCP
); }
620 | K_CSUMUDP
'(' number delimiter number
')'
621 { set_csum16
($3, $5, CSUM_UDP
); }
622 | K_CSUMTCP6
'(' number delimiter number
')'
623 { set_csum16
($3, $5, CSUM_TCP6
); }
624 | K_CSUMUDP6
'(' number delimiter number
')'
625 { set_csum16
($3, $5, CSUM_UDP6
); }
629 : K_SEQINC
'(' number delimiter number
')'
630 { set_sequential_inc
($3, $5, 1); }
631 | K_SEQINC
'(' number delimiter number delimiter number
')'
632 { set_sequential_inc
($3, $5, $7); }
636 : K_SEQDEC
'(' number delimiter number
')'
637 { set_sequential_dec
($3, $5, 1); }
638 | K_SEQDEC
'(' number delimiter number delimiter number
')'
639 { set_sequential_dec
($3, $5, $7); }
644 { set_dynamic_rnd
(); }
645 | K_DRND
'(' number
')'
648 for
(i
= 0; i
< max
; ++i
)
654 : K_DINC
'(' number delimiter number
')'
655 { set_dynamic_incdec
($3, $5, 1, TYPE_INC
); }
656 | K_DINC
'(' number delimiter number delimiter number
')'
657 { set_dynamic_incdec
($3, $5, $7, TYPE_INC
); }
661 : K_DDEC
'(' number delimiter number
')'
662 { set_dynamic_incdec
($3, $5, 1, TYPE_DEC
); }
663 | K_DDEC
'(' number delimiter number delimiter number
')'
664 { set_dynamic_incdec
($3, $5, $7, TYPE_DEC
); }
681 : number
{ field_expr.type
= FIELD_EXPR_NUMB
;
682 field_expr.val.number
= $1; }
683 | mac
{ field_expr.type
= FIELD_EXPR_MAC
;
684 memcpy
(field_expr.val.bytes
, $1, sizeof
(field_expr.val.bytes
)); }
685 | ip4_addr
{ field_expr.type
= FIELD_EXPR_IP4_ADDR
;
686 field_expr.val.ip4_addr
= $1; }
687 | ip6_addr
{ field_expr.type
= FIELD_EXPR_IP6_ADDR
;
688 field_expr.val.ip6_addr
= $1; }
692 : eth
'(' eth_param_list
')' { }
696 : K_ETH
{ proto_add
(PROTO_ETH
); }
702 | eth_expr delimiter eth_param_list
{ }
712 : K_DADDR
{ proto_field_set
(ETH_DST_ADDR
); }
713 | K_SADDR
{ proto_field_set
(ETH_SRC_ADDR
); }
714 | eth_type
{ proto_field_set
(ETH_TYPE
); }
717 : eth_field skip_white
'=' skip_white field_expr
718 { proto_field_expr_eval
(); }
722 : vlan
'(' vlan_param_list
')' { }
726 : K_VLAN
{ proto_add
(PROTO_VLAN
); }
732 | vlan_expr delimiter vlan_param_list
{ }
741 : vlan_type
{ proto_field_set
(VLAN_TPID
); }
742 | K_TCI
{ proto_field_set
(VLAN_TCI
); }
743 | K_PCP
{ proto_field_set
(VLAN_PCP
); }
744 | K_DEI
{ proto_field_set
(VLAN_DEI
); }
745 | K_ID
{ proto_field_set
(VLAN_VID
); }
749 : vlan_field skip_white
'=' skip_white field_expr
750 { proto_field_expr_eval
(); }
752 { proto_field_set_be16
(hdr
, VLAN_TPID
, ETH_P_8021Q
); }
754 { proto_field_set_be16
(hdr
, VLAN_TPID
, ETH_P_8021AD
); }
758 : mpls
'(' mpls_param_list
')' { }
762 : K_MPLS
{ proto_add
(PROTO_MPLS
); }
768 | mpls_expr delimiter mpls_param_list
{ }
777 : K_LABEL
{ proto_field_set
(MPLS_LABEL
); }
778 | mpls_tc
{ proto_field_set
(MPLS_TC
); }
779 | K_LAST
{ proto_field_set
(MPLS_LAST
); }
780 | K_TTL
{ proto_field_set
(MPLS_TTL
); }
784 : mpls_field skip_white
'=' skip_white field_expr
785 { proto_field_expr_eval
(); }
788 : arp
'(' arp_param_list
')' { }
794 | arp_expr delimiter arp_param_list
{ }
799 { proto_field_set
(ARP_HTYPE
); }
801 { proto_field_set
(ARP_PTYPE
); }
803 { proto_field_set
(ARP_SHA
); }
805 { proto_field_set
(ARP_THA
); }
807 { proto_field_set
(ARP_SPA
); }
809 { proto_field_set
(ARP_TPA
); }
813 : arp_field skip_white
'=' skip_white field_expr
814 { proto_field_expr_eval
(); }
815 | K_OPER skip_white
'=' skip_white field_expr
816 { proto_field_set
(ARP_OPER
);
817 proto_field_expr_eval
(); }
818 | K_OPER skip_white
'=' skip_white K_REQUEST
819 { proto_field_set_be16
(hdr
, ARP_OPER
, ARPOP_REQUEST
); }
820 | K_OPER skip_white
'=' skip_white K_REPLY
821 { proto_field_set_be16
(hdr
, ARP_OPER
, ARPOP_REPLY
); }
823 { proto_field_set_be16
(hdr
, ARP_OPER
, ARPOP_REQUEST
); }
825 { proto_field_set_be16
(hdr
, ARP_OPER
, ARPOP_REPLY
); }
828 : K_ARP
{ proto_add
(PROTO_ARP
); }
832 : ip4
'(' ip4_param_list
')' { }
838 | ip4_expr delimiter ip4_param_list
{ }
842 : K_VER
{ proto_field_set
(IP4_VER
); }
843 | K_IHL
{ proto_field_set
(IP4_IHL
); }
844 | K_DADDR
{ proto_field_set
(IP4_DADDR
); }
845 | K_SADDR
{ proto_field_set
(IP4_SADDR
); }
846 | K_PROT
{ proto_field_set
(IP4_PROTO
); }
847 | K_TTL
{ proto_field_set
(IP4_TTL
); }
848 | K_DSCP
{ proto_field_set
(IP4_DSCP
); }
849 | K_ECN
{ proto_field_set
(IP4_ECN
); }
850 | K_TOS
{ proto_field_set
(IP4_TOS
); }
851 | K_LEN
{ proto_field_set
(IP4_LEN
); }
852 | K_ID
{ proto_field_set
(IP4_ID
); }
853 | K_FLAGS
{ proto_field_set
(IP4_FLAGS
); }
854 | K_FRAG
{ proto_field_set
(IP4_FRAG_OFFS
); }
855 | K_CSUM
{ proto_field_set
(IP4_CSUM
); }
859 : ip4_field skip_white
'=' skip_white field_expr
860 { proto_field_expr_eval
(); }
861 | K_DF
{ proto_field_set_be16
(hdr
, IP4_DF
, 1); }
862 | K_MF
{ proto_field_set_be16
(hdr
, IP4_MF
, 1); }
866 : K_IP4
{ proto_add
(PROTO_IP4
); }
870 : ip6
'(' ip6_param_list
')' { }
876 | ip6_expr delimiter ip6_param_list
{ }
885 : K_VER
{ proto_field_set
(IP6_VER
); }
886 | K_TC
{ proto_field_set
(IP6_CLASS
); }
887 | K_FLOW
{ proto_field_set
(IP6_FLOW_LBL
); }
888 | K_LEN
{ proto_field_set
(IP6_LEN
); }
889 | K_NEXT_HDR
{ proto_field_set
(IP6_NEXT_HDR
); }
890 | ip6_hop_limit
{ proto_field_set
(IP6_HOP_LIMIT
); }
891 | K_SADDR
{ proto_field_set
(IP6_SADDR
); }
892 | K_DADDR
{ proto_field_set
(IP6_DADDR
) ; }
896 : ip6_field skip_white
'=' skip_white field_expr
897 { proto_field_expr_eval
(); }
901 : K_IP6
{ proto_add
(PROTO_IP6
); }
905 : icmp4
'(' icmp4_param_list
')' { }
911 | icmp4_expr delimiter icmp4_param_list
{ }
915 : K_TYPE
{ proto_field_set
(ICMPV4_TYPE
); }
916 | K_CODE
{ proto_field_set
(ICMPV4_CODE
); }
917 | K_ID
{ proto_field_set
(ICMPV4_ID
); }
918 | K_SEQ
{ proto_field_set
(ICMPV4_SEQ
); }
919 | K_MTU
{ proto_field_set
(ICMPV4_MTU
); }
920 | K_ADDR
{ proto_field_set
(ICMPV4_REDIR_ADDR
); }
924 : icmp4_field skip_white
'=' skip_white field_expr
925 { proto_field_expr_eval
(); }
927 { proto_field_set_u8
(hdr
, ICMPV4_TYPE
, ICMP_ECHO
);
928 proto_field_set_u8
(hdr
, ICMPV4_CODE
, 0); }
930 { proto_field_set_u8
(hdr
, ICMPV4_TYPE
, ICMP_ECHOREPLY
);
931 proto_field_set_u8
(hdr
, ICMPV4_CODE
, 0); }
935 : K_ICMP4
{ proto_add
(PROTO_ICMP4
); }
939 : icmp6
'(' icmp6_param_list
')' { }
944 | icmp6_expr delimiter icmp6_param_list
{ }
948 : K_CODE
{ proto_field_set
(ICMPV6_CODE
); }
949 | K_CSUM
{ proto_field_set
(ICMPV6_CSUM
); }
953 : icmp6_field skip_white
'=' skip_white field_expr
954 { proto_field_expr_eval
(); }
955 | K_TYPE skip_white
'=' skip_white field_expr
956 { proto_field_set
(ICMPV6_TYPE
);
957 proto_field_expr_eval
(); }
958 | K_TYPE skip_white
'=' K_ECHO_REQUEST
959 { proto_field_set_u8
(hdr
, ICMPV6_TYPE
, ICMPV6_ECHO_REQUEST
); }
961 { proto_field_set_u8
(hdr
, ICMPV6_TYPE
, ICMPV6_ECHO_REQUEST
); }
962 | K_TYPE skip_white
'=' K_ECHO_REPLY
963 { proto_field_set_u8
(hdr
, ICMPV6_TYPE
, ICMPV6_ECHO_REPLY
); }
965 { proto_field_set_u8
(hdr
, ICMPV6_TYPE
, ICMPV6_ECHO_REPLY
); }
968 : K_ICMP6
{ proto_add
(PROTO_ICMP6
); }
972 : udp
'(' udp_param_list
')' { }
978 | udp_expr delimiter udp_param_list
{ }
982 : K_SPORT
{ proto_field_set
(UDP_SPORT
); }
983 | K_DPORT
{ proto_field_set
(UDP_DPORT
); }
984 | K_LEN
{ proto_field_set
(UDP_LEN
); }
985 | K_CSUM
{ proto_field_set
(UDP_CSUM
); }
989 : udp_field skip_white
'=' skip_white field_expr
990 { proto_field_expr_eval
(); }
994 : K_UDP
{ proto_add
(PROTO_UDP
); }
998 : tcp
'(' tcp_param_list
')' { }
1004 | tcp_expr delimiter tcp_param_list
{ }
1008 : K_SPORT
{ proto_field_set
(TCP_SPORT
); }
1009 | K_DPORT
{ proto_field_set
(TCP_DPORT
); }
1010 | K_SEQ
{ proto_field_set
(TCP_SEQ
); }
1011 | K_ACK_SEQ
{ proto_field_set
(TCP_ACK_SEQ
); }
1012 | K_DOFF
{ proto_field_set
(TCP_DOFF
); }
1013 | K_WINDOW
{ proto_field_set
(TCP_WINDOW
); }
1014 | K_CSUM
{ proto_field_set
(TCP_CSUM
); }
1015 | K_URG_PTR
{ proto_field_set
(TCP_URG_PTR
); }
1019 : tcp_field skip_white
'=' skip_white field_expr
1020 { proto_field_expr_eval
(); }
1021 | K_CWR
{ proto_field_set_be16
(hdr
, TCP_CWR
, 1); }
1022 | K_ECE
{ proto_field_set_be16
(hdr
, TCP_ECE
, 1); }
1023 | K_URG
{ proto_field_set_be16
(hdr
, TCP_URG
, 1); }
1024 | K_ACK
{ proto_field_set_be16
(hdr
, TCP_ACK
, 1); }
1025 | K_PSH
{ proto_field_set_be16
(hdr
, TCP_PSH
, 1); }
1026 | K_RST
{ proto_field_set_be16
(hdr
, TCP_RST
, 1); }
1027 | K_SYN
{ proto_field_set_be16
(hdr
, TCP_SYN
, 1); }
1028 | K_FIN
{ proto_field_set_be16
(hdr
, TCP_FIN
, 1); }
1032 : K_TCP
{ proto_add
(PROTO_TCP
); }
1037 static void finalize_packet
(void)
1039 /* XXX hack ... we allocated one packet pointer too much */
1044 static void dump_conf
(void)
1048 for
(i
= 0; i
< plen
; ++i
) {
1049 printf
("[%zu] pkt\n", i
);
1050 printf
(" len %zu cnts %zu rnds %zu\n",
1053 packet_dyn
[i
].rlen
);
1055 printf
(" payload ");
1056 for
(j
= 0; j
< packets
[i
].len
; ++j
)
1057 printf
("%02x ", packets
[i
].payload
[j
]);
1060 for
(j
= 0; j
< packet_dyn
[i
].clen
; ++j
)
1061 printf
(" cnt%zu [%u,%u], inc %u, off %jd type %s\n", j
,
1062 packet_dyn
[i
].cnt
[j
].min
,
1063 packet_dyn
[i
].cnt
[j
].max
,
1064 packet_dyn
[i
].cnt
[j
].inc
,
1065 (intmax_t)packet_dyn
[i
].cnt
[j
].off
,
1066 packet_dyn
[i
].cnt
[j
].type
== TYPE_INC ?
1069 for
(j
= 0; j
< packet_dyn
[i
].rlen
; ++j
)
1070 printf
(" rnd%zu off %jd\n", j
,
1071 (intmax_t)packet_dyn
[i
].rnd
[j
].off
);
1075 void cleanup_packets
(void)
1079 for
(i
= 0; i
< plen
; ++i
) {
1080 struct packet
*pkt
= &packets
[i
];
1083 xfree
(pkt
->payload
);
1085 for
(j
= 0; j
< pkt
->headers_count
; j
++) {
1086 struct proto_hdr
*hdr
= pkt
->headers
[j
];
1096 for
(i
= 0; i
< dlen
; ++i
) {
1097 free
(packet_dyn
[i
].cnt
);
1098 free
(packet_dyn
[i
].rnd
);
1104 void compile_packets
(char *file
, bool verbose
, unsigned int cpu
,
1105 bool invoke_cpp
, char *const cpp_argv
[])
1110 if
(access
(file
, R_OK
)) {
1111 fprintf
(stderr
, "Cannot access %s: %s!\n", file
, strerror
(errno
));
1115 memset
(tmp_file
, 0, sizeof
(tmp_file
));
1119 if
(cpp_exec
(file
, tmp_file
, sizeof
(tmp_file
), cpp_argv
)) {
1120 fprintf
(stderr
, "Failed to invoke C preprocessor!\n");
1126 if
(!strncmp
("-", file
, strlen
("-")))
1129 yyin
= fopen
(file
, "r");
1131 fprintf
(stderr
, "Cannot open %s: %s!\n", file
, strerror
(errno
));
1140 if
(our_cpu
== 0 && verbose
)
1145 if
(yyin
&& yyin
!= stdin
)
1154 void compile_packets_str
(char *str
, bool verbose
, unsigned int cpu
)
1161 yy_scan_string
(str
);
1166 if
(our_cpu
== 0 && verbose
)
1177 void yyerror(const char *err
)
1179 fprintf
(stderr
, "Syntax error at line %d, char '%s': %s\n", yylineno
, yytext
, err
);