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
{
82 struct proto_field_expr
{
83 enum field_expr_type_t type
;
84 struct proto_field
*field
;
87 struct in_addr ip4_addr
;
88 struct in6_addr ip6_addr
;
91 struct proto_field_func func
;
95 static struct proto_field_expr field_expr
;
96 static struct proto_hdr
*hdr
;
98 static inline
int test_ignore
(void)
100 if
(min_cpu
< 0 && max_cpu
< 0)
102 else if
(max_cpu
>= our_cpu
&& min_cpu
<= our_cpu
)
108 static inline
void __init_new_packet_slot
(struct packet
*slot
)
110 slot
->payload
= NULL
;
114 static inline
void __init_new_counter_slot
(struct packet_dyn
*slot
)
120 static inline
void __init_new_randomizer_slot
(struct packet_dyn
*slot
)
126 static inline
void __init_new_csum_slot
(struct packet_dyn
*slot
)
132 static inline
void __init_new_fields_slot
(struct packet_dyn
*slot
)
138 static inline
void __setup_new_counter
(struct counter
*c
, uint8_t start
,
139 uint8_t stop
, uint8_t stepping
,
145 c
->val
= (type
== TYPE_INC
) ? start
: stop
;
146 c
->off
= payload_last
;
150 static inline
void __setup_new_randomizer
(struct randomizer
*r
)
152 r
->off
= payload_last
;
155 static inline
void __setup_new_csum16
(struct csum16
*s
, off_t from
, off_t to
,
158 s
->off
= payload_last
- 1;
164 static void realloc_packet
(void)
170 packets
= xrealloc
(packets
, plen
* sizeof
(*packets
));
172 __init_new_packet_slot
(&packets
[packet_last
]);
175 packet_dyn
= xrealloc
(packet_dyn
, dlen
* sizeof
(*packet_dyn
));
177 __init_new_counter_slot
(&packet_dyn
[packetd_last
]);
178 __init_new_randomizer_slot
(&packet_dyn
[packetd_last
]);
179 __init_new_csum_slot
(&packet_dyn
[packetd_last
]);
180 __init_new_fields_slot
(&packet_dyn
[packetd_last
]);
183 struct packet
*current_packet
(void)
185 return
&packets
[packet_last
];
188 uint32_t current_packet_id
(void)
193 struct packet
*packet_get
(uint32_t id
)
198 static void set_byte
(uint8_t val
)
200 struct packet
*pkt
= &packets
[packet_last
];
206 pkt
->payload
= xrealloc
(pkt
->payload
, pkt
->len
);
207 pkt
->payload
[payload_last
] = val
;
210 static void set_multi_byte
(uint8_t *s
, size_t len
)
214 for
(i
= 0; i
< len
; ++i
)
218 void set_fill
(uint8_t val
, size_t len
)
221 struct packet
*pkt
= &packets
[packet_last
];
227 pkt
->payload
= xrealloc
(pkt
->payload
, pkt
->len
);
228 for
(i
= 0; i
< len
; ++i
)
229 pkt
->payload
[payload_last
- i
] = val
;
232 static void __set_csum16_dynamic
(size_t from
, size_t to
, enum csum which
)
234 struct packet
*pkt
= &packets
[packet_last
];
235 struct packet_dyn
*pktd
= &packet_dyn
[packetd_last
];
238 pkt
->payload
= xrealloc
(pkt
->payload
, pkt
->len
);
241 pktd
->csum
= xrealloc
(pktd
->csum
, pktd
->slen
* sizeof
(struct csum16
));
243 __setup_new_csum16
(&pktd
->csum
[packetds_last
], from
, to
, which
);
246 static void __set_csum16_static
(size_t from
, size_t to
, enum csum which __maybe_unused
)
248 struct packet
*pkt
= &packets
[packet_last
];
252 sum
= htons
(calc_csum
(pkt
->payload
+ from
, to
- from
));
253 psum
= (uint8_t *) &sum
;
259 static inline
bool is_dynamic_csum
(enum csum which
)
272 static void set_csum16
(size_t from
, size_t to
, enum csum which
)
274 struct packet
*pkt
= &packets
[packet_last
];
275 struct packet_dyn
*pktd
= &packet_dyn
[packetd_last
];
287 bug_on
(!(from
< to
));
289 if
(packet_dyn_has_elems
(pktd
) || to
>= pkt
->len || is_dynamic_csum
(which
))
290 __set_csum16_dynamic
(from
, to
, which
);
292 __set_csum16_static
(from
, to
, which
);
295 static void set_rnd
(size_t len
)
298 struct packet
*pkt
= &packets
[packet_last
];
304 pkt
->payload
= xrealloc
(pkt
->payload
, pkt
->len
);
305 for
(i
= 0; i
< len
; ++i
)
306 pkt
->payload
[payload_last
- i
] = (uint8_t) rand
();
309 static void set_sequential_inc
(uint8_t start
, size_t len
, uint8_t stepping
)
312 struct packet
*pkt
= &packets
[packet_last
];
318 pkt
->payload
= xrealloc
(pkt
->payload
, pkt
->len
);
319 for
(i
= 0; i
< len
; ++i
) {
320 off_t off
= len
- 1 - i
;
322 pkt
->payload
[payload_last
- off
] = start
;
327 static void set_sequential_dec
(uint8_t start
, size_t len
, uint8_t stepping
)
330 struct packet
*pkt
= &packets
[packet_last
];
336 pkt
->payload
= xrealloc
(pkt
->payload
, pkt
->len
);
337 for
(i
= 0; i
< len
; ++i
) {
338 int off
= len
- 1 - i
;
340 pkt
->payload
[payload_last
- off
] = start
;
345 static void set_dynamic_rnd
(void)
347 struct packet
*pkt
= &packets
[packet_last
];
348 struct packet_dyn
*pktd
= &packet_dyn
[packetd_last
];
354 pkt
->payload
= xrealloc
(pkt
->payload
, pkt
->len
);
357 pktd
->rnd
= xrealloc
(pktd
->rnd
, pktd
->rlen
* sizeof
(struct randomizer
));
359 __setup_new_randomizer
(&pktd
->rnd
[packetdr_last
]);
362 static void set_dynamic_incdec
(uint8_t start
, uint8_t stop
, uint8_t stepping
,
365 struct packet
*pkt
= &packets
[packet_last
];
366 struct packet_dyn
*pktd
= &packet_dyn
[packetd_last
];
372 pkt
->payload
= xrealloc
(pkt
->payload
, pkt
->len
);
375 pktd
->cnt
= xrealloc
(pktd
->cnt
, pktd
->clen
* sizeof
(struct counter
));
377 __setup_new_counter
(&pktd
->cnt
[packetdc_last
], start
, stop
, stepping
, type
);
380 static void proto_add
(enum proto_id pid
)
382 hdr
= proto_header_push
(pid
);
385 static void proto_field_set
(uint32_t fid
)
387 field_expr.field
= proto_field_by_id
(hdr
, fid
);
390 static void proto_field_func_setup
(struct proto_field
*field
, struct proto_field_func
*func
)
392 struct packet_dyn
*pkt_dyn
;
394 proto_field_func_add
(field
->hdr
, field
->id
, func
);
396 pkt_dyn
= &packet_dyn
[packetd_last
];
398 pkt_dyn
->fields
= xrealloc
(pkt_dyn
->fields
, pkt_dyn
->flen
*
399 sizeof
(struct proto_field
*));
400 pkt_dyn
->fields
[pkt_dyn
->flen
- 1] = field
;
403 static void proto_field_expr_eval
(void)
405 struct proto_field
*field
= field_expr.field
;
407 switch
(field_expr.type
) {
408 case FIELD_EXPR_NUMB
:
410 proto_field_set_u8
(hdr
, field
->id
, field_expr.val.number
);
411 else if
(field
->len
== 2)
412 proto_field_set_be16
(hdr
, field
->id
, field_expr.val.number
);
413 else if
(field
->len
== 4)
414 proto_field_set_be32
(hdr
, field
->id
, field_expr.val.number
);
420 proto_field_set_bytes
(hdr
, field
->id
, field_expr.val.bytes
);
423 case FIELD_EXPR_IP4_ADDR
:
424 proto_field_set_u32
(hdr
, field
->id
, field_expr.val.ip4_addr.s_addr
);
427 case FIELD_EXPR_IP6_ADDR
:
428 proto_field_set_bytes
(hdr
, field
->id
,
429 (uint8_t *)&field_expr.val.ip6_addr.s6_addr
);
434 if
(field_expr.val.func.min
435 && field_expr.val.func.min
>= field_expr.val.func.max
)
436 panic
("dinc(): min(%u) can't be >= max(%u)\n",
437 field_expr.val.func.min
, field_expr.val.func.max
);
439 proto_field_func_setup
(field
, &field_expr.val.func
);
442 case FIELD_EXPR_UNKNOWN
:
447 memset
(&field_expr
, 0, sizeof
(field_expr
));
453 struct in_addr ip4_addr
;
454 struct in6_addr ip6_addr
;
455 long long int number
;
460 %token K_COMMENT K_FILL K_RND K_SEQINC K_SEQDEC K_DRND K_DINC K_DDEC K_WHITE
461 %token K_CPU K_CSUMIP K_CSUMUDP K_CSUMTCP K_CSUMUDP6 K_CSUMTCP6 K_CONST8 K_CONST16 K_CONST32 K_CONST64
463 %token K_DADDR K_SADDR K_ETYPE K_TYPE
464 %token K_OPER K_SHA K_SPA K_THA K_TPA K_REQUEST K_REPLY K_PTYPE K_HTYPE
465 %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
466 %token K_FLOW K_NEXT_HDR K_HOP_LIMIT
467 %token K_CODE K_ECHO_REQUEST K_ECHO_REPLY
468 %token K_SPORT K_DPORT
469 %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
470 %token K_TPID K_TCI K_PCP K_DEI K_1Q K_1AD
471 %token K_LABEL K_TC K_LAST K_EXP
479 %token K_ICMP4 K_ICMP6
482 %token
',' '{' '}' '(' ')' '[' ']' ':' '-' '+' '*' '/' '%' '&' '|' '<' '>' '^'
484 %token number
string mac ip4_addr ip6_addr
486 %type
<number
> number expression
489 %type
<ip4_addr
> ip4_addr
490 %type
<ip6_addr
> ip6_addr
492 %left
'-' '+' '*' '/' '%' '&' '|' '<' '>' '^'
499 | packets inline_comment
{ }
500 | packets K_WHITE
{ }
520 | delimiter_nowhite
{ }
528 : '{' noenforce_white payload noenforce_white
'}' {
529 min_cpu
= max_cpu
= -1;
531 proto_packet_finish
();
535 | K_CPU
'(' number cpu_delim number
')' ':' noenforce_white
'{' noenforce_white payload noenforce_white
'}' {
539 if
(min_cpu
> max_cpu
) {
546 proto_packet_finish
();
550 | K_CPU
'(' number
')' ':' noenforce_white
'{' noenforce_white payload noenforce_white
'}' {
551 min_cpu
= max_cpu
= $3;
553 proto_packet_finish
();
561 | payload elem_delimiter
{ }
565 : delimiter_nowhite
{ }
574 : number
{ set_byte
((uint8_t) $1); }
575 |
string { set_multi_byte
((uint8_t *) $1 + 1, strlen
($1) - 2); }
585 | proto
{ proto_header_finish
(hdr
); }
592 | expression
'+' expression
594 | expression
'-' expression
596 | expression
'*' expression
598 | expression
'/' expression
600 | expression
'%' expression
602 | expression
'&' expression
604 | expression
'|' expression
606 | expression
'^' expression
608 | expression
'<' '<' expression
610 | expression
'>' '>' expression
619 : K_FILL
'(' number delimiter number
')'
620 { set_fill
($3, $5); }
624 : K_CONST8
'(' expression
')'
625 { set_byte
((uint8_t) $3); }
626 | K_CONST16
'(' expression
')' {
627 uint16_t __c
= cpu_to_be16
((uint16_t) $3);
629 set_multi_byte
((uint8_t *) &__c
, sizeof
(__c
));
631 | K_CONST32
'(' expression
')' {
632 uint32_t __c
= cpu_to_be32
((uint32_t) $3);
634 set_multi_byte
((uint8_t *) &__c
, sizeof
(__c
));
636 | K_CONST64
'(' expression
')' {
637 uint64_t __c
= cpu_to_be64
((uint64_t) $3);
639 set_multi_byte
((uint8_t *) &__c
, sizeof
(__c
));
644 : K_RND
'(' number
')'
649 : K_CSUMIP
'(' number delimiter number
')'
650 { set_csum16
($3, $5, CSUM_IP
); }
651 | K_CSUMTCP
'(' number delimiter number
')'
652 { set_csum16
($3, $5, CSUM_TCP
); }
653 | K_CSUMUDP
'(' number delimiter number
')'
654 { set_csum16
($3, $5, CSUM_UDP
); }
655 | K_CSUMTCP6
'(' number delimiter number
')'
656 { set_csum16
($3, $5, CSUM_TCP6
); }
657 | K_CSUMUDP6
'(' number delimiter number
')'
658 { set_csum16
($3, $5, CSUM_UDP6
); }
662 : K_SEQINC
'(' number delimiter number
')'
663 { set_sequential_inc
($3, $5, 1); }
664 | K_SEQINC
'(' number delimiter number delimiter number
')'
665 { set_sequential_inc
($3, $5, $7); }
669 : K_SEQDEC
'(' number delimiter number
')'
670 { set_sequential_dec
($3, $5, 1); }
671 | K_SEQDEC
'(' number delimiter number delimiter number
')'
672 { set_sequential_dec
($3, $5, $7); }
677 { set_dynamic_rnd
(); }
678 | K_DRND
'(' number
')'
681 for
(i
= 0; i
< max
; ++i
)
687 : K_DINC
'(' number delimiter number
')'
688 { set_dynamic_incdec
($3, $5, 1, TYPE_INC
); }
689 | K_DINC
'(' number delimiter number delimiter number
')'
690 { set_dynamic_incdec
($3, $5, $7, TYPE_INC
); }
694 : K_DDEC
'(' number delimiter number
')'
695 { set_dynamic_incdec
($3, $5, 1, TYPE_DEC
); }
696 | K_DDEC
'(' number delimiter number delimiter number
')'
697 { set_dynamic_incdec
($3, $5, $7, TYPE_DEC
); }
714 : number
{ field_expr.type
= FIELD_EXPR_NUMB
;
715 field_expr.val.number
= $1; }
716 | mac
{ field_expr.type
= FIELD_EXPR_MAC
;
717 memcpy
(field_expr.val.bytes
, $1, sizeof
(field_expr.val.bytes
)); }
718 | ip4_addr
{ field_expr.type
= FIELD_EXPR_IP4_ADDR
;
719 field_expr.val.ip4_addr
= $1; }
720 | ip6_addr
{ field_expr.type
= FIELD_EXPR_IP6_ADDR
;
721 field_expr.val.ip6_addr
= $1; }
722 | K_DINC
'(' ')' { field_expr.type
= FIELD_EXPR_INC
;
723 field_expr.val.func.type
= PROTO_FIELD_FUNC_INC
;
724 field_expr.val.func.inc
= 1; }
725 | K_DINC
'(' number
')'
726 { field_expr.type
= FIELD_EXPR_INC
;
727 field_expr.val.func.type
= PROTO_FIELD_FUNC_INC
;
728 field_expr.val.func.inc
= $3; }
729 | K_DINC
'(' number delimiter number
')'
730 { field_expr.type
= FIELD_EXPR_INC
;
731 field_expr.val.func.type
= PROTO_FIELD_FUNC_INC
;
732 field_expr.val.func.type |
= PROTO_FIELD_FUNC_MIN
;
733 field_expr.val.func.min
= $3;
734 field_expr.val.func.max
= $5;
735 field_expr.val.func.inc
= 1; }
736 | K_DINC
'(' number delimiter number delimiter number
')'
737 { field_expr.type
= FIELD_EXPR_INC
;
738 field_expr.val.func.type
= PROTO_FIELD_FUNC_INC
;
739 field_expr.val.func.type |
= PROTO_FIELD_FUNC_MIN
;
740 field_expr.val.func.min
= $3;
741 field_expr.val.func.max
= $5;
742 field_expr.val.func.inc
= $7; }
743 | K_DRND
'(' ')' { field_expr.type
= FIELD_EXPR_RND
;
744 field_expr.val.func.type
= PROTO_FIELD_FUNC_RND
; }
745 | K_DRND
'(' number delimiter number
')'
746 { field_expr.type
= FIELD_EXPR_RND
;
747 field_expr.val.func.type
= PROTO_FIELD_FUNC_RND
;
748 field_expr.val.func.min
= $3;
749 field_expr.val.func.max
= $5; }
753 : eth
'(' eth_param_list
')' { }
757 : K_ETH
{ proto_add
(PROTO_ETH
); }
763 | eth_expr delimiter eth_param_list
{ }
773 : K_DADDR
{ proto_field_set
(ETH_DST_ADDR
); }
774 | K_SADDR
{ proto_field_set
(ETH_SRC_ADDR
); }
775 | eth_type
{ proto_field_set
(ETH_TYPE
); }
778 : eth_field skip_white
'=' skip_white field_expr
779 { proto_field_expr_eval
(); }
783 : vlan
'(' vlan_param_list
')' { }
787 : K_VLAN
{ proto_add
(PROTO_VLAN
); }
793 | vlan_expr delimiter vlan_param_list
{ }
802 : vlan_type
{ proto_field_set
(VLAN_TPID
); }
803 | K_TCI
{ proto_field_set
(VLAN_TCI
); }
804 | K_PCP
{ proto_field_set
(VLAN_PCP
); }
805 | K_DEI
{ proto_field_set
(VLAN_DEI
); }
806 | K_ID
{ proto_field_set
(VLAN_VID
); }
810 : vlan_field skip_white
'=' skip_white field_expr
811 { proto_field_expr_eval
(); }
813 { proto_field_set_be16
(hdr
, VLAN_TPID
, ETH_P_8021Q
); }
815 { proto_field_set_be16
(hdr
, VLAN_TPID
, ETH_P_8021AD
); }
819 : mpls
'(' mpls_param_list
')' { }
823 : K_MPLS
{ proto_add
(PROTO_MPLS
); }
829 | mpls_expr delimiter mpls_param_list
{ }
838 : K_LABEL
{ proto_field_set
(MPLS_LABEL
); }
839 | mpls_tc
{ proto_field_set
(MPLS_TC
); }
840 | K_LAST
{ proto_field_set
(MPLS_LAST
); }
841 | K_TTL
{ proto_field_set
(MPLS_TTL
); }
845 : mpls_field skip_white
'=' skip_white field_expr
846 { proto_field_expr_eval
(); }
849 : arp
'(' arp_param_list
')' { }
855 | arp_expr delimiter arp_param_list
{ }
860 { proto_field_set
(ARP_HTYPE
); }
862 { proto_field_set
(ARP_PTYPE
); }
864 { proto_field_set
(ARP_SHA
); }
866 { proto_field_set
(ARP_THA
); }
868 { proto_field_set
(ARP_SPA
); }
870 { proto_field_set
(ARP_TPA
); }
874 : arp_field skip_white
'=' skip_white field_expr
875 { proto_field_expr_eval
(); }
876 | K_OPER skip_white
'=' skip_white field_expr
877 { proto_field_set
(ARP_OPER
);
878 proto_field_expr_eval
(); }
879 | K_OPER skip_white
'=' skip_white K_REQUEST
880 { proto_field_set_be16
(hdr
, ARP_OPER
, ARPOP_REQUEST
); }
881 | K_OPER skip_white
'=' skip_white K_REPLY
882 { proto_field_set_be16
(hdr
, ARP_OPER
, ARPOP_REPLY
); }
884 { proto_field_set_be16
(hdr
, ARP_OPER
, ARPOP_REQUEST
); }
886 { proto_field_set_be16
(hdr
, ARP_OPER
, ARPOP_REPLY
); }
889 : K_ARP
{ proto_add
(PROTO_ARP
); }
893 : ip4
'(' ip4_param_list
')' { }
899 | ip4_expr delimiter ip4_param_list
{ }
903 : K_VER
{ proto_field_set
(IP4_VER
); }
904 | K_IHL
{ proto_field_set
(IP4_IHL
); }
905 | K_DADDR
{ proto_field_set
(IP4_DADDR
); }
906 | K_SADDR
{ proto_field_set
(IP4_SADDR
); }
907 | K_PROT
{ proto_field_set
(IP4_PROTO
); }
908 | K_TTL
{ proto_field_set
(IP4_TTL
); }
909 | K_DSCP
{ proto_field_set
(IP4_DSCP
); }
910 | K_ECN
{ proto_field_set
(IP4_ECN
); }
911 | K_TOS
{ proto_field_set
(IP4_TOS
); }
912 | K_LEN
{ proto_field_set
(IP4_LEN
); }
913 | K_ID
{ proto_field_set
(IP4_ID
); }
914 | K_FLAGS
{ proto_field_set
(IP4_FLAGS
); }
915 | K_FRAG
{ proto_field_set
(IP4_FRAG_OFFS
); }
916 | K_CSUM
{ proto_field_set
(IP4_CSUM
); }
920 : ip4_field skip_white
'=' skip_white field_expr
921 { proto_field_expr_eval
(); }
922 | K_DF
{ proto_field_set_be16
(hdr
, IP4_DF
, 1); }
923 | K_MF
{ proto_field_set_be16
(hdr
, IP4_MF
, 1); }
927 : K_IP4
{ proto_add
(PROTO_IP4
); }
931 : ip6
'(' ip6_param_list
')' { }
937 | ip6_expr delimiter ip6_param_list
{ }
946 : K_VER
{ proto_field_set
(IP6_VER
); }
947 | K_TC
{ proto_field_set
(IP6_CLASS
); }
948 | K_FLOW
{ proto_field_set
(IP6_FLOW_LBL
); }
949 | K_LEN
{ proto_field_set
(IP6_LEN
); }
950 | K_NEXT_HDR
{ proto_field_set
(IP6_NEXT_HDR
); }
951 | ip6_hop_limit
{ proto_field_set
(IP6_HOP_LIMIT
); }
952 | K_SADDR
{ proto_field_set
(IP6_SADDR
); }
953 | K_DADDR
{ proto_field_set
(IP6_DADDR
) ; }
957 : ip6_field skip_white
'=' skip_white field_expr
958 { proto_field_expr_eval
(); }
962 : K_IP6
{ proto_add
(PROTO_IP6
); }
966 : icmp4
'(' icmp4_param_list
')' { }
972 | icmp4_expr delimiter icmp4_param_list
{ }
976 : K_TYPE
{ proto_field_set
(ICMPV4_TYPE
); }
977 | K_CODE
{ proto_field_set
(ICMPV4_CODE
); }
978 | K_ID
{ proto_field_set
(ICMPV4_ID
); }
979 | K_SEQ
{ proto_field_set
(ICMPV4_SEQ
); }
980 | K_MTU
{ proto_field_set
(ICMPV4_MTU
); }
981 | K_ADDR
{ proto_field_set
(ICMPV4_REDIR_ADDR
); }
985 : icmp4_field skip_white
'=' skip_white field_expr
986 { proto_field_expr_eval
(); }
988 { proto_field_set_u8
(hdr
, ICMPV4_TYPE
, ICMP_ECHO
);
989 proto_field_set_u8
(hdr
, ICMPV4_CODE
, 0); }
991 { proto_field_set_u8
(hdr
, ICMPV4_TYPE
, ICMP_ECHOREPLY
);
992 proto_field_set_u8
(hdr
, ICMPV4_CODE
, 0); }
996 : K_ICMP4
{ proto_add
(PROTO_ICMP4
); }
1000 : icmp6
'(' icmp6_param_list
')' { }
1005 | icmp6_expr delimiter icmp6_param_list
{ }
1009 : K_CODE
{ proto_field_set
(ICMPV6_CODE
); }
1010 | K_CSUM
{ proto_field_set
(ICMPV6_CSUM
); }
1014 : icmp6_field skip_white
'=' skip_white field_expr
1015 { proto_field_expr_eval
(); }
1016 | K_TYPE skip_white
'=' skip_white field_expr
1017 { proto_field_set
(ICMPV6_TYPE
);
1018 proto_field_expr_eval
(); }
1019 | K_TYPE skip_white
'=' K_ECHO_REQUEST
1020 { proto_field_set_u8
(hdr
, ICMPV6_TYPE
, ICMPV6_ECHO_REQUEST
); }
1022 { proto_field_set_u8
(hdr
, ICMPV6_TYPE
, ICMPV6_ECHO_REQUEST
); }
1023 | K_TYPE skip_white
'=' K_ECHO_REPLY
1024 { proto_field_set_u8
(hdr
, ICMPV6_TYPE
, ICMPV6_ECHO_REPLY
); }
1026 { proto_field_set_u8
(hdr
, ICMPV6_TYPE
, ICMPV6_ECHO_REPLY
); }
1029 : K_ICMP6
{ proto_add
(PROTO_ICMP6
); }
1033 : udp
'(' udp_param_list
')' { }
1039 | udp_expr delimiter udp_param_list
{ }
1043 : K_SPORT
{ proto_field_set
(UDP_SPORT
); }
1044 | K_DPORT
{ proto_field_set
(UDP_DPORT
); }
1045 | K_LEN
{ proto_field_set
(UDP_LEN
); }
1046 | K_CSUM
{ proto_field_set
(UDP_CSUM
); }
1050 : udp_field skip_white
'=' skip_white field_expr
1051 { proto_field_expr_eval
(); }
1055 : K_UDP
{ proto_add
(PROTO_UDP
); }
1059 : tcp
'(' tcp_param_list
')' { }
1065 | tcp_expr delimiter tcp_param_list
{ }
1069 : K_SPORT
{ proto_field_set
(TCP_SPORT
); }
1070 | K_DPORT
{ proto_field_set
(TCP_DPORT
); }
1071 | K_SEQ
{ proto_field_set
(TCP_SEQ
); }
1072 | K_ACK_SEQ
{ proto_field_set
(TCP_ACK_SEQ
); }
1073 | K_DOFF
{ proto_field_set
(TCP_DOFF
); }
1074 | K_WINDOW
{ proto_field_set
(TCP_WINDOW
); }
1075 | K_CSUM
{ proto_field_set
(TCP_CSUM
); }
1076 | K_URG_PTR
{ proto_field_set
(TCP_URG_PTR
); }
1080 : tcp_field skip_white
'=' skip_white field_expr
1081 { proto_field_expr_eval
(); }
1082 | K_CWR
{ proto_field_set_be16
(hdr
, TCP_CWR
, 1); }
1083 | K_ECE
{ proto_field_set_be16
(hdr
, TCP_ECE
, 1); }
1084 | K_URG
{ proto_field_set_be16
(hdr
, TCP_URG
, 1); }
1085 | K_ACK
{ proto_field_set_be16
(hdr
, TCP_ACK
, 1); }
1086 | K_PSH
{ proto_field_set_be16
(hdr
, TCP_PSH
, 1); }
1087 | K_RST
{ proto_field_set_be16
(hdr
, TCP_RST
, 1); }
1088 | K_SYN
{ proto_field_set_be16
(hdr
, TCP_SYN
, 1); }
1089 | K_FIN
{ proto_field_set_be16
(hdr
, TCP_FIN
, 1); }
1093 : K_TCP
{ proto_add
(PROTO_TCP
); }
1098 static void finalize_packet
(void)
1100 /* XXX hack ... we allocated one packet pointer too much */
1105 static void dump_conf
(void)
1109 for
(i
= 0; i
< plen
; ++i
) {
1110 printf
("[%zu] pkt\n", i
);
1111 printf
(" len %zu cnts %zu rnds %zu\n",
1114 packet_dyn
[i
].rlen
);
1116 printf
(" payload ");
1117 for
(j
= 0; j
< packets
[i
].len
; ++j
)
1118 printf
("%02x ", packets
[i
].payload
[j
]);
1121 for
(j
= 0; j
< packet_dyn
[i
].clen
; ++j
)
1122 printf
(" cnt%zu [%u,%u], inc %u, off %jd type %s\n", j
,
1123 packet_dyn
[i
].cnt
[j
].min
,
1124 packet_dyn
[i
].cnt
[j
].max
,
1125 packet_dyn
[i
].cnt
[j
].inc
,
1126 (intmax_t)packet_dyn
[i
].cnt
[j
].off
,
1127 packet_dyn
[i
].cnt
[j
].type
== TYPE_INC ?
1130 for
(j
= 0; j
< packet_dyn
[i
].rlen
; ++j
)
1131 printf
(" rnd%zu off %jd\n", j
,
1132 (intmax_t)packet_dyn
[i
].rnd
[j
].off
);
1136 void cleanup_packets
(void)
1140 for
(i
= 0; i
< plen
; ++i
) {
1141 struct packet
*pkt
= &packets
[i
];
1144 xfree
(pkt
->payload
);
1146 for
(j
= 0; j
< pkt
->headers_count
; j
++) {
1147 struct proto_hdr
*hdr
= pkt
->headers
[j
];
1157 for
(i
= 0; i
< dlen
; ++i
) {
1158 free
(packet_dyn
[i
].cnt
);
1159 free
(packet_dyn
[i
].rnd
);
1160 free
(packet_dyn
[i
].fields
);
1166 void compile_packets
(char *file
, bool verbose
, unsigned int cpu
,
1167 bool invoke_cpp
, char *const cpp_argv
[])
1172 if
(access
(file
, R_OK
)) {
1173 fprintf
(stderr
, "Cannot access %s: %s!\n", file
, strerror
(errno
));
1177 memset
(tmp_file
, 0, sizeof
(tmp_file
));
1181 if
(cpp_exec
(file
, tmp_file
, sizeof
(tmp_file
), cpp_argv
)) {
1182 fprintf
(stderr
, "Failed to invoke C preprocessor!\n");
1188 if
(!strncmp
("-", file
, strlen
("-")))
1191 yyin
= fopen
(file
, "r");
1193 fprintf
(stderr
, "Cannot open %s: %s!\n", file
, strerror
(errno
));
1202 if
(our_cpu
== 0 && verbose
)
1207 if
(yyin
&& yyin
!= stdin
)
1216 void compile_packets_str
(char *str
, bool verbose
, unsigned int cpu
)
1223 yy_scan_string
(str
);
1228 if
(our_cpu
== 0 && verbose
)
1239 void yyerror(const char *err
)
1241 fprintf
(stderr
, "Syntax error at line %d, char '%s': %s\n", yylineno
, yytext
, err
);