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 /* yacc-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"
34 #include "trafgen_l7.h"
42 #define ETH_P_8021AD 0x88A8
45 #define YYERROR_VERBOSE 0
47 #define YYLTYPE_IS_TRIVIAL 1
50 extern
int yylex(void);
51 extern
void yy_scan_string
(char *);
52 extern
void yylex_destroy
();
53 extern
void yyerror(const char *);
57 extern
struct packet
*packets
;
60 #define packet_last (plen - 1)
62 #define payload_last (packets[packet_last].len - 1)
64 extern
struct packet_dyn
*packet_dyn
;
67 #define packetd_last (dlen - 1)
69 #define packetdc_last (packet_dyn[packetd_last].clen - 1)
70 #define packetdr_last (packet_dyn[packetd_last].rlen - 1)
71 #define packetds_last (packet_dyn[packetd_last].slen - 1)
73 static int our_cpu
, min_cpu
= -1, max_cpu
= -1;
75 enum field_expr_type_t
{
76 FIELD_EXPR_UNKNOWN
= 0,
77 FIELD_EXPR_NUMB
= 1 << 0,
78 FIELD_EXPR_MAC
= 1 << 1,
79 FIELD_EXPR_IP4_ADDR
= 1 << 2,
80 FIELD_EXPR_IP6_ADDR
= 1 << 3,
81 FIELD_EXPR_INC
= 1 << 4,
82 FIELD_EXPR_RND
= 1 << 5,
83 FIELD_EXPR_OFFSET
= 1 << 6,
84 FIELD_EXPR_STRING
= 1 << 7,
85 FIELD_EXPR_FQDN
= 1 << 8,
88 struct proto_field_expr
{
89 enum field_expr_type_t type
;
90 struct proto_field
*field
;
93 struct in_addr ip4_addr
;
94 struct in6_addr ip6_addr
;
98 struct proto_field_func func
;
102 static struct proto_field_expr field_expr
;
103 static struct proto_hdr
*hdr
;
105 static inline
int test_ignore
(void)
107 if
(min_cpu
< 0 && max_cpu
< 0)
109 else if
(max_cpu
>= our_cpu
&& min_cpu
<= our_cpu
)
115 static inline
void __init_new_packet_slot
(struct packet
*slot
)
117 memset
(slot
, 0, sizeof
(*slot
));
120 static inline
void __init_new_counter_slot
(struct packet_dyn
*slot
)
126 static inline
void __init_new_randomizer_slot
(struct packet_dyn
*slot
)
132 static inline
void __init_new_csum_slot
(struct packet_dyn
*slot
)
138 static inline
void __init_new_fields_slot
(struct packet_dyn
*slot
)
144 static inline
void __setup_new_counter
(struct counter
*c
, uint8_t start
,
145 uint8_t stop
, uint8_t stepping
,
150 c
->inc
= (type
== TYPE_INC
) ? stepping
: -stepping
;
151 c
->val
= (type
== TYPE_INC
) ? start
: stop
;
152 c
->off
= payload_last
;
156 static inline
void __setup_new_randomizer
(struct randomizer
*r
)
158 r
->off
= payload_last
;
161 static inline
void __setup_new_csum16
(struct csum16
*s
, off_t from
, off_t to
,
164 s
->off
= payload_last
- 1;
170 struct packet
*realloc_packet
(void)
178 packets
= xrealloc
(packets
, plen
* sizeof
(*packets
));
180 __init_new_packet_slot
(&packets
[packet_last
]);
183 packet_dyn
= xrealloc
(packet_dyn
, dlen
* sizeof
(*packet_dyn
));
185 __init_new_counter_slot
(&packet_dyn
[packetd_last
]);
186 __init_new_randomizer_slot
(&packet_dyn
[packetd_last
]);
187 __init_new_csum_slot
(&packet_dyn
[packetd_last
]);
188 __init_new_fields_slot
(&packet_dyn
[packetd_last
]);
190 for
(i
= 0; i
< plen
; i
++)
193 return
&packets
[packet_last
];
196 struct packet
*current_packet
(void)
198 return
&packets
[packet_last
];
201 uint32_t current_packet_id
(void)
206 struct packet
*packet_get
(uint32_t id
)
211 static void set_byte
(uint8_t val
)
213 struct packet
*pkt
= &packets
[packet_last
];
219 pkt
->payload
= xrealloc
(pkt
->payload
, pkt
->len
);
220 pkt
->payload
[payload_last
] = val
;
223 static void set_multi_byte
(uint8_t *s
, size_t len
)
227 for
(i
= 0; i
< len
; ++i
)
231 void set_fill
(uint8_t val
, size_t len
)
234 struct packet
*pkt
= &packets
[packet_last
];
240 pkt
->payload
= xrealloc
(pkt
->payload
, pkt
->len
);
241 for
(i
= 0; i
< len
; ++i
)
242 pkt
->payload
[payload_last
- i
] = val
;
245 static void __set_csum16_dynamic
(size_t from
, size_t to
, enum csum which
)
247 struct packet
*pkt
= &packets
[packet_last
];
248 struct packet_dyn
*pktd
= &packet_dyn
[packetd_last
];
251 pkt
->payload
= xrealloc
(pkt
->payload
, pkt
->len
);
254 pktd
->csum
= xrealloc
(pktd
->csum
, pktd
->slen
* sizeof
(struct csum16
));
256 __setup_new_csum16
(&pktd
->csum
[packetds_last
], from
, to
, which
);
259 static void __set_csum16_static
(size_t from
, size_t to
, enum csum which __maybe_unused
)
261 struct packet
*pkt
= &packets
[packet_last
];
265 sum
= htons
(calc_csum
(pkt
->payload
+ from
, to
- from
));
266 psum
= (uint8_t *) &sum
;
272 static inline
bool is_dynamic_csum
(enum csum which
)
285 static void set_csum16
(size_t from
, size_t to
, enum csum which
)
287 struct packet
*pkt
= &packets
[packet_last
];
288 struct packet_dyn
*pktd
= &packet_dyn
[packetd_last
];
300 bug_on
(!(from
< to
));
302 if
(packet_dyn_has_elems
(pktd
) || to
>= pkt
->len || is_dynamic_csum
(which
))
303 __set_csum16_dynamic
(from
, to
, which
);
305 __set_csum16_static
(from
, to
, which
);
308 static void set_rnd
(size_t len
)
311 struct packet
*pkt
= &packets
[packet_last
];
317 pkt
->payload
= xrealloc
(pkt
->payload
, pkt
->len
);
318 for
(i
= 0; i
< len
; ++i
)
319 pkt
->payload
[payload_last
- i
] = (uint8_t) rand
();
322 static void set_sequential_inc
(uint8_t start
, size_t len
, uint8_t stepping
)
325 struct packet
*pkt
= &packets
[packet_last
];
331 pkt
->payload
= xrealloc
(pkt
->payload
, pkt
->len
);
332 for
(i
= 0; i
< len
; ++i
) {
333 off_t off
= len
- 1 - i
;
335 pkt
->payload
[payload_last
- off
] = start
;
340 static void set_sequential_dec
(uint8_t start
, size_t len
, uint8_t stepping
)
343 struct packet
*pkt
= &packets
[packet_last
];
349 pkt
->payload
= xrealloc
(pkt
->payload
, pkt
->len
);
350 for
(i
= 0; i
< len
; ++i
) {
351 int off
= len
- 1 - i
;
353 pkt
->payload
[payload_last
- off
] = start
;
358 static void set_dynamic_rnd
(void)
360 struct packet
*pkt
= &packets
[packet_last
];
361 struct packet_dyn
*pktd
= &packet_dyn
[packetd_last
];
367 pkt
->payload
= xrealloc
(pkt
->payload
, pkt
->len
);
370 pktd
->rnd
= xrealloc
(pktd
->rnd
, pktd
->rlen
* sizeof
(struct randomizer
));
372 __setup_new_randomizer
(&pktd
->rnd
[packetdr_last
]);
375 static void set_dynamic_incdec
(uint8_t start
, uint8_t stop
, uint8_t stepping
,
378 struct packet
*pkt
= &packets
[packet_last
];
379 struct packet_dyn
*pktd
= &packet_dyn
[packetd_last
];
385 pkt
->payload
= xrealloc
(pkt
->payload
, pkt
->len
);
388 pktd
->cnt
= xrealloc
(pktd
->cnt
, pktd
->clen
* sizeof
(struct counter
));
390 __setup_new_counter
(&pktd
->cnt
[packetdc_last
], start
, stop
, stepping
, type
);
393 static void proto_add
(enum proto_id pid
)
395 hdr
= proto_header_push
(pid
);
398 static void proto_field_set
(uint32_t fid
)
400 memset
(&field_expr
, 0, sizeof
(field_expr
));
401 field_expr.field
= proto_hdr_field_by_id
(hdr
, fid
);
404 static void proto_field_func_setup
(struct proto_field
*field
, struct proto_field_func
*func
)
406 struct proto_field
*field_copy
;
407 struct packet_dyn
*pkt_dyn
;
409 field_copy
= xmalloc
(sizeof
(*field
));
410 memcpy
(field_copy
, field
, sizeof
(*field
));
412 field_copy
->pkt_offset
+= func
->offset
;
414 field_copy
->len
= func
->len
;
416 proto_field_func_add
(field_copy
, func
);
418 pkt_dyn
= &packet_dyn
[packetd_last
];
420 pkt_dyn
->fields
= xrealloc
(pkt_dyn
->fields
, pkt_dyn
->flen
*
421 sizeof
(struct proto_field
*));
423 pkt_dyn
->fields
[pkt_dyn
->flen
- 1] = field_copy
;
426 static void proto_field_expr_eval
(void)
428 struct proto_field
*field
= field_expr.field
;
430 if
((field_expr.type
& FIELD_EXPR_OFFSET
) &&
431 !((field_expr.type
& FIELD_EXPR_INC
) ||
432 (field_expr.type
& FIELD_EXPR_RND
))) {
434 panic
("Field offset expression is valid only with function expression\n");
437 if
(field_expr.type
& FIELD_EXPR_NUMB
) {
439 proto_field_set_u8
(field
, field_expr.val.number
);
440 else if
(field
->len
== 2)
441 proto_field_set_be16
(field
, field_expr.val.number
);
442 else if
(field
->len
== 4)
443 proto_field_set_be32
(field
, field_expr.val.number
);
445 panic
("Invalid value length %zu, can be 1,2 or 4\n", field
->len
);
446 } else if
(field_expr.type
& FIELD_EXPR_MAC
) {
447 proto_field_set_bytes
(field
, field_expr.val.mac
, 6);
448 } else if
(field_expr.type
& FIELD_EXPR_FQDN
) {
449 char *fqdn
= str2fqdn
(field_expr.val.str
);
450 proto_field_set_bytes
(field
, (uint8_t *) fqdn
, strlen
(fqdn
) + 1);
451 xfree
(field_expr.val.str
);
453 } else if
(field_expr.type
& FIELD_EXPR_STRING
) {
454 proto_field_set_string
(field
, field_expr.val.str
);
455 xfree
(field_expr.val.str
);
456 } else if
(field_expr.type
& FIELD_EXPR_IP4_ADDR
) {
457 proto_field_set_u32
(field
, field_expr.val.ip4_addr.s_addr
);
458 } else if
(field_expr.type
& FIELD_EXPR_IP6_ADDR
) {
459 proto_field_set_bytes
(field
, (uint8_t *)&field_expr.val.ip6_addr.s6_addr
, 16);
460 } else if
((field_expr.type
& FIELD_EXPR_INC
) ||
461 (field_expr.type
& FIELD_EXPR_RND
)) {
463 if
(field_expr.val.func.min
464 && field_expr.val.func.min
>= field_expr.val.func.max
)
465 panic
("dinc(): min(%u) can't be >= max(%u)\n",
466 field_expr.val.func.min
, field_expr.val.func.max
);
468 proto_field_func_setup
(field
, &field_expr.val.func
);
469 } else if
((field_expr.type
& FIELD_EXPR_OFFSET
) &&
470 !((field_expr.type
& FIELD_EXPR_INC
) ||
471 (field_expr.type
& FIELD_EXPR_RND
))) {
473 panic
("Field expression is valid only for function value expression\n");
478 memset
(&field_expr
, 0, sizeof
(field_expr
));
481 static void field_index_validate
(struct proto_field
*field
, uint16_t index
, size_t len
)
483 if
(field_expr.field
->len
<= index
) {
484 yyerror("Invalid [index] parameter");
485 panic
("Index (%u) is bigger than field's length (%zu)\n",
488 if
(len
!= 1 && len
!= 2 && len
!= 4) {
489 yyerror("Invalid [index:len] parameter");
490 panic
("Invalid index length - 1,2 or 4 is only allowed\n");
494 static void proto_push_sub_hdr
(uint32_t id
)
496 hdr
= proto_hdr_push_sub_header
(hdr
, id
);
499 static void proto_pop_sub_hdr
(void)
501 if
(hdr
->ops
->header_finish
)
502 hdr
->ops
->header_finish
(hdr
);
510 struct in_addr ip4_addr
;
511 struct in6_addr ip6_addr
;
512 long long int number
;
517 %token K_COMMENT K_FILL K_RND K_SEQINC K_SEQDEC K_DRND K_DINC K_DDEC K_WHITE
518 %token K_CPU K_CSUMIP K_CSUMUDP K_CSUMTCP K_CSUMUDP6 K_CSUMTCP6 K_CONST8 K_CONST16 K_CONST32 K_CONST64
520 %token K_DADDR K_SADDR K_ETYPE K_TYPE
522 %token K_OPER K_SHA K_SPA K_THA K_TPA K_REQUEST K_REPLY K_PTYPE K_HTYPE
523 %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
524 %token K_FLOW K_NEXT_HDR K_HOP_LIMIT
525 %token K_CODE K_ECHO_REQUEST K_ECHO_REPLY
526 %token K_SPORT K_DPORT
527 %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
528 %token K_TPID K_TCI K_PCP K_DEI K_1Q K_1AD
529 %token K_LABEL K_TC K_LAST K_EXP
533 %token K_QR K_AANSWER K_TRUNC K_RAVAIL K_RDESIRED K_ZERO K_RCODE K_QDCOUNT K_ANCOUNT K_NSCOUNT K_ARCOUNT
534 %token K_QUERY K_ANSWER K_AUTH K_ADD
535 %token K_NAME K_CLASS K_DATA K_NS K_CNAME K_PTR
543 %token K_ICMP4 K_ICMP6
547 %token
',' '{' '}' '(' ')' '[' ']' ':' '-' '+' '*' '/' '%' '&' '|' '<' '>' '^'
549 %token number
string mac ip4_addr ip6_addr
551 %type
<number
> number expression
554 %type
<ip4_addr
> ip4_addr
555 %type
<ip6_addr
> ip6_addr
557 %left
'-' '+' '*' '/' '%' '&' '|' '<' '>' '^'
564 | packets inline_comment
{ }
565 | packets K_WHITE
{ }
585 | delimiter_nowhite
{ }
593 : '{' noenforce_white payload noenforce_white
'}' {
594 min_cpu
= max_cpu
= -1;
596 proto_packet_finish
();
600 | K_CPU
'(' number cpu_delim number
')' ':' noenforce_white
'{' noenforce_white payload noenforce_white
'}' {
604 if
(min_cpu
> max_cpu
) {
611 proto_packet_finish
();
615 | K_CPU
'(' number
')' ':' noenforce_white
'{' noenforce_white payload noenforce_white
'}' {
616 min_cpu
= max_cpu
= $3;
618 proto_packet_finish
();
626 | payload elem_delimiter
{ }
630 : delimiter_nowhite
{ }
639 : number
{ set_byte
((uint8_t) $1); }
640 |
string { set_multi_byte
((uint8_t *) $1 + 1, strlen
($1) - 2); }
650 | proto
{ proto_header_finish
(hdr
); }
657 | expression
'+' expression
659 | expression
'-' expression
661 | expression
'*' expression
663 | expression
'/' expression
665 | expression
'%' expression
667 | expression
'&' expression
669 | expression
'|' expression
671 | expression
'^' expression
673 | expression
'<' '<' expression
675 | expression
'>' '>' expression
684 : K_FILL
'(' number delimiter number
')'
685 { set_fill
($3, $5); }
689 : K_CONST8
'(' expression
')'
690 { set_byte
((uint8_t) $3); }
691 | K_CONST16
'(' expression
')' {
692 uint16_t __c
= cpu_to_be16
((uint16_t) $3);
694 set_multi_byte
((uint8_t *) &__c
, sizeof
(__c
));
696 | K_CONST32
'(' expression
')' {
697 uint32_t __c
= cpu_to_be32
((uint32_t) $3);
699 set_multi_byte
((uint8_t *) &__c
, sizeof
(__c
));
701 | K_CONST64
'(' expression
')' {
702 uint64_t __c
= cpu_to_be64
((uint64_t) $3);
704 set_multi_byte
((uint8_t *) &__c
, sizeof
(__c
));
709 : K_RND
'(' number
')'
714 : K_CSUMIP
'(' number delimiter number
')'
715 { set_csum16
($3, $5, CSUM_IP
); }
716 | K_CSUMTCP
'(' number delimiter number
')'
717 { set_csum16
($3, $5, CSUM_TCP
); }
718 | K_CSUMUDP
'(' number delimiter number
')'
719 { set_csum16
($3, $5, CSUM_UDP
); }
720 | K_CSUMTCP6
'(' number delimiter number
')'
721 { set_csum16
($3, $5, CSUM_TCP6
); }
722 | K_CSUMUDP6
'(' number delimiter number
')'
723 { set_csum16
($3, $5, CSUM_UDP6
); }
727 : K_SEQINC
'(' number delimiter number
')'
728 { set_sequential_inc
($3, $5, 1); }
729 | K_SEQINC
'(' number delimiter number delimiter number
')'
730 { set_sequential_inc
($3, $5, $7); }
734 : K_SEQDEC
'(' number delimiter number
')'
735 { set_sequential_dec
($3, $5, 1); }
736 | K_SEQDEC
'(' number delimiter number delimiter number
')'
737 { set_sequential_dec
($3, $5, $7); }
742 { set_dynamic_rnd
(); }
743 | K_DRND
'(' number
')'
746 for
(i
= 0; i
< max
; ++i
)
752 : K_DINC
'(' number delimiter number
')'
753 { set_dynamic_incdec
($3, $5, 1, TYPE_INC
); }
754 | K_DINC
'(' number delimiter number delimiter number
')'
755 { set_dynamic_incdec
($3, $5, $7, TYPE_INC
); }
759 : K_DDEC
'(' number delimiter number
')'
760 { set_dynamic_incdec
($3, $5, 1, TYPE_DEC
); }
761 | K_DDEC
'(' number delimiter number delimiter number
')'
762 { set_dynamic_incdec
($3, $5, $7, TYPE_DEC
); }
782 : '[' skip_white number skip_white
']'
783 { field_index_validate
(field_expr.field
, $3, 1);
784 field_expr.type |
= FIELD_EXPR_OFFSET
;
785 field_expr.val.func.offset
= $3;
786 field_expr.val.func.len
= 1; }
787 |
'[' skip_white number skip_white
':' skip_white number skip_white
']'
788 { field_index_validate
(field_expr.field
, $3, $7);
789 field_expr.type |
= FIELD_EXPR_OFFSET
;
790 field_expr.val.func.offset
= $3;
791 field_expr.val.func.len
= $7; }
795 : number
{ field_expr.type |
= FIELD_EXPR_NUMB
;
796 field_expr.val.number
= $1; }
797 | mac
{ field_expr.type |
= FIELD_EXPR_MAC
;
798 memcpy
(field_expr.val.mac
, $1, sizeof
(field_expr.val.mac
)); }
799 |
string { field_expr.type |
= FIELD_EXPR_STRING
;
800 field_expr.val.str
= xstrdup
($1 + 1);
801 field_expr.val.str
[strlen
($1 + 1) - 1] = '\0'; }
802 | ip4_addr
{ field_expr.type |
= FIELD_EXPR_IP4_ADDR
;
803 field_expr.val.ip4_addr
= $1; }
804 | ip6_addr
{ field_expr.type |
= FIELD_EXPR_IP6_ADDR
;
805 field_expr.val.ip6_addr
= $1; }
806 | K_DINC
'(' ')' { field_expr.type |
= FIELD_EXPR_INC
;
807 field_expr.val.func.type
= PROTO_FIELD_FUNC_INC
;
808 field_expr.val.func.inc
= 1; }
809 | K_DINC
'(' number
')'
810 { field_expr.type |
= FIELD_EXPR_INC
;
811 field_expr.val.func.type
= PROTO_FIELD_FUNC_INC
;
812 field_expr.val.func.inc
= $3; }
813 | K_DINC
'(' number delimiter number
')'
814 { field_expr.type |
= FIELD_EXPR_INC
;
815 field_expr.val.func.type
= PROTO_FIELD_FUNC_INC
;
816 field_expr.val.func.type |
= PROTO_FIELD_FUNC_MIN
;
817 field_expr.val.func.min
= $3;
818 field_expr.val.func.max
= $5;
819 field_expr.val.func.inc
= 1; }
820 | K_DINC
'(' number delimiter number delimiter number
')'
821 { field_expr.type |
= FIELD_EXPR_INC
;
822 field_expr.val.func.type
= PROTO_FIELD_FUNC_INC
;
823 field_expr.val.func.type |
= PROTO_FIELD_FUNC_MIN
;
824 field_expr.val.func.min
= $3;
825 field_expr.val.func.max
= $5;
826 field_expr.val.func.inc
= $7; }
827 | K_DRND
'(' ')' { field_expr.type |
= FIELD_EXPR_RND
;
828 field_expr.val.func.type
= PROTO_FIELD_FUNC_RND
; }
829 | K_DRND
'(' number delimiter number
')'
830 { field_expr.type |
= FIELD_EXPR_RND
;
831 field_expr.val.func.type
= PROTO_FIELD_FUNC_RND
;
832 field_expr.val.func.min
= $3;
833 field_expr.val.func.max
= $5; }
837 : eth
'(' eth_param_list
')' { }
841 : K_ETH
{ proto_add
(PROTO_ETH
); }
847 | eth_expr delimiter eth_param_list
{ }
857 : K_DADDR
{ proto_field_set
(ETH_DST_ADDR
); }
858 | K_SADDR
{ proto_field_set
(ETH_SRC_ADDR
); }
859 | eth_type
{ proto_field_set
(ETH_TYPE
); }
862 : eth_field field_expr skip_white
'=' skip_white field_value_expr
863 { proto_field_expr_eval
(); }
864 | eth_field skip_white
'=' skip_white field_value_expr
865 { proto_field_expr_eval
(); }
869 : pause
'(' pause_param_list
')' { }
873 : K_PAUSE
{ proto_add
(PROTO_PAUSE
); }
879 | pause_expr delimiter pause_param_list
{ }
883 : K_CODE
{ proto_field_set
(PAUSE_OPCODE
); }
884 | K_TIME
{ proto_field_set
(PAUSE_TIME
); }
888 : pause_field field_expr skip_white
'=' skip_white field_value_expr
889 { proto_field_expr_eval
(); }
890 | pause_field skip_white
'=' skip_white field_value_expr
891 { proto_field_expr_eval
(); }
895 : pfc
'(' pfc_param_list
')' { }
899 : K_PFC
{ proto_add
(PROTO_PFC
); }
905 | pfc_expr delimiter pfc_param_list
{ }
909 : K_CODE
{ proto_field_set
(PFC_OPCODE
); }
910 | K_PRIO
{ proto_field_set
(PFC_PRIO
); }
911 | K_PRIO
'(' number
')'
913 yyerror("pfc: Invalid prio(index) parameter");
914 panic
("pfc: prio(0)..prio(7) is allowed only\n");
916 proto_field_set
(PFC_PRIO_0
+ $3); }
917 | K_TIME
'(' number
')'
919 yyerror("pfc: Invalid time(index) parameter");
920 panic
("pfc: time(0)..time(7) is allowed only\n");
922 proto_field_set
(PFC_TIME_0
+ $3); }
926 : pfc_field field_expr skip_white
'=' skip_white field_value_expr
927 { proto_field_expr_eval
(); }
928 | pfc_field skip_white
'=' skip_white field_value_expr
929 { proto_field_expr_eval
(); }
933 : vlan
'(' vlan_param_list
')' { }
937 : K_VLAN
{ proto_add
(PROTO_VLAN
); }
943 | vlan_expr delimiter vlan_param_list
{ }
952 : vlan_type
{ proto_field_set
(VLAN_TPID
); }
953 | K_TCI
{ proto_field_set
(VLAN_TCI
); }
954 | K_PCP
{ proto_field_set
(VLAN_PCP
); }
955 | K_DEI
{ proto_field_set
(VLAN_DEI
); }
956 | K_ID
{ proto_field_set
(VLAN_VID
); }
960 : vlan_field field_expr skip_white
'=' skip_white field_value_expr
961 { proto_field_expr_eval
(); }
962 | vlan_field skip_white
'=' skip_white field_value_expr
963 { proto_field_expr_eval
(); }
965 { proto_hdr_field_set_be16
(hdr
, VLAN_TPID
, ETH_P_8021Q
); }
967 { proto_hdr_field_set_be16
(hdr
, VLAN_TPID
, ETH_P_8021AD
); }
971 : mpls
'(' mpls_param_list
')' { }
975 : K_MPLS
{ proto_add
(PROTO_MPLS
); }
981 | mpls_expr delimiter mpls_param_list
{ }
990 : K_LABEL
{ proto_field_set
(MPLS_LABEL
); }
991 | mpls_tc
{ proto_field_set
(MPLS_TC
); }
992 | K_LAST
{ proto_field_set
(MPLS_LAST
); }
993 | K_TTL
{ proto_field_set
(MPLS_TTL
); }
997 : mpls_field field_expr skip_white
'=' skip_white field_value_expr
998 { proto_field_expr_eval
(); }
999 | mpls_field skip_white
'=' skip_white field_value_expr
1000 { proto_field_expr_eval
(); }
1004 : arp
'(' arp_param_list
')' { }
1010 | arp_expr delimiter arp_param_list
{ }
1015 { proto_field_set
(ARP_HTYPE
); }
1017 { proto_field_set
(ARP_PTYPE
); }
1019 { proto_field_set
(ARP_SHA
); }
1021 { proto_field_set
(ARP_THA
); }
1023 { proto_field_set
(ARP_SPA
); }
1025 { proto_field_set
(ARP_TPA
); }
1029 : arp_field field_expr skip_white
'=' skip_white field_value_expr
1030 { proto_field_expr_eval
(); }
1031 | arp_field skip_white
'=' skip_white field_value_expr
1032 { proto_field_expr_eval
(); }
1033 | K_OPER field_expr skip_white
'=' skip_white field_value_expr
1034 { proto_field_set
(ARP_OPER
);
1035 proto_field_expr_eval
(); }
1036 | K_OPER skip_white
'=' skip_white field_value_expr
1037 { proto_field_set
(ARP_OPER
);
1038 proto_field_expr_eval
(); }
1039 | K_OPER skip_white
'=' skip_white K_REQUEST
1040 { proto_hdr_field_set_be16
(hdr
, ARP_OPER
, ARPOP_REQUEST
); }
1041 | K_OPER skip_white
'=' skip_white K_REPLY
1042 { proto_hdr_field_set_be16
(hdr
, ARP_OPER
, ARPOP_REPLY
); }
1044 { proto_hdr_field_set_be16
(hdr
, ARP_OPER
, ARPOP_REQUEST
); }
1046 { proto_hdr_field_set_be16
(hdr
, ARP_OPER
, ARPOP_REPLY
); }
1050 : K_ARP
{ proto_add
(PROTO_ARP
); }
1054 : ip4
'(' ip4_param_list
')' { }
1060 | ip4_expr delimiter ip4_param_list
{ }
1064 : K_VER
{ proto_field_set
(IP4_VER
); }
1065 | K_IHL
{ proto_field_set
(IP4_IHL
); }
1066 | K_DADDR
{ proto_field_set
(IP4_DADDR
); }
1067 | K_SADDR
{ proto_field_set
(IP4_SADDR
); }
1068 | K_PROT
{ proto_field_set
(IP4_PROTO
); }
1069 | K_TTL
{ proto_field_set
(IP4_TTL
); }
1070 | K_DSCP
{ proto_field_set
(IP4_DSCP
); }
1071 | K_ECN
{ proto_field_set
(IP4_ECN
); }
1072 | K_TOS
{ proto_field_set
(IP4_TOS
); }
1073 | K_LEN
{ proto_field_set
(IP4_LEN
); }
1074 | K_ID
{ proto_field_set
(IP4_ID
); }
1075 | K_FLAGS
{ proto_field_set
(IP4_FLAGS
); }
1076 | K_FRAG
{ proto_field_set
(IP4_FRAG_OFFS
); }
1077 | K_CSUM
{ proto_field_set
(IP4_CSUM
); }
1081 : ip4_field field_expr skip_white
'=' skip_white field_value_expr
1082 { proto_field_expr_eval
(); }
1083 | ip4_field skip_white
'=' skip_white field_value_expr
1084 { proto_field_expr_eval
(); }
1085 | K_DF
{ proto_hdr_field_set_be16
(hdr
, IP4_DF
, 1); }
1086 | K_MF
{ proto_hdr_field_set_be16
(hdr
, IP4_MF
, 1); }
1090 : K_IP4
{ proto_add
(PROTO_IP4
); }
1094 : ip6
'(' ip6_param_list
')' { }
1100 | ip6_expr delimiter ip6_param_list
{ }
1109 : K_VER
{ proto_field_set
(IP6_VER
); }
1110 | K_TC
{ proto_field_set
(IP6_CLASS
); }
1111 | K_FLOW
{ proto_field_set
(IP6_FLOW_LBL
); }
1112 | K_LEN
{ proto_field_set
(IP6_LEN
); }
1113 | K_NEXT_HDR
{ proto_field_set
(IP6_NEXT_HDR
); }
1114 | ip6_hop_limit
{ proto_field_set
(IP6_HOP_LIMIT
); }
1115 | K_SADDR
{ proto_field_set
(IP6_SADDR
); }
1116 | K_DADDR
{ proto_field_set
(IP6_DADDR
) ; }
1120 : ip6_field field_expr skip_white
'=' skip_white field_value_expr
1121 { proto_field_expr_eval
(); }
1122 | ip6_field skip_white
'=' skip_white field_value_expr
1123 { proto_field_expr_eval
(); }
1127 : K_IP6
{ proto_add
(PROTO_IP6
); }
1131 : icmp4
'(' icmp4_param_list
')' { }
1137 | icmp4_expr delimiter icmp4_param_list
{ }
1141 : K_TYPE
{ proto_field_set
(ICMPV4_TYPE
); }
1142 | K_CODE
{ proto_field_set
(ICMPV4_CODE
); }
1143 | K_ID
{ proto_field_set
(ICMPV4_ID
); }
1144 | K_SEQ
{ proto_field_set
(ICMPV4_SEQ
); }
1145 | K_MTU
{ proto_field_set
(ICMPV4_MTU
); }
1146 | K_ADDR
{ proto_field_set
(ICMPV4_REDIR_ADDR
); }
1150 : icmp4_field field_expr skip_white
'=' skip_white field_value_expr
1151 { proto_field_expr_eval
(); }
1152 | icmp4_field skip_white
'=' skip_white field_value_expr
1153 { proto_field_expr_eval
(); }
1155 { proto_hdr_field_set_u8
(hdr
, ICMPV4_TYPE
, ICMP_ECHO
);
1156 proto_hdr_field_set_u8
(hdr
, ICMPV4_CODE
, 0); }
1158 { proto_hdr_field_set_u8
(hdr
, ICMPV4_TYPE
, ICMP_ECHOREPLY
);
1159 proto_hdr_field_set_u8
(hdr
, ICMPV4_CODE
, 0); }
1163 : K_ICMP4
{ proto_add
(PROTO_ICMP4
); }
1167 : icmp6
'(' icmp6_param_list
')' { }
1173 | icmp6_expr delimiter icmp6_param_list
{ }
1177 : K_CODE
{ proto_field_set
(ICMPV6_CODE
); }
1178 | K_CSUM
{ proto_field_set
(ICMPV6_CSUM
); }
1182 : icmp6_field field_expr skip_white
'=' skip_white field_value_expr
1183 { proto_field_expr_eval
(); }
1184 | icmp6_field skip_white
'=' skip_white field_value_expr
1185 { proto_field_expr_eval
(); }
1186 | K_TYPE field_expr skip_white
'=' skip_white field_value_expr
1187 { proto_field_set
(ICMPV6_TYPE
);
1188 proto_field_expr_eval
(); }
1189 | K_TYPE skip_white
'=' skip_white field_value_expr
1190 { proto_field_set
(ICMPV6_TYPE
);
1191 proto_field_expr_eval
(); }
1192 | K_TYPE skip_white
'=' K_ECHO_REQUEST
1193 { proto_hdr_field_set_u8
(hdr
, ICMPV6_TYPE
, ICMPV6_ECHO_REQUEST
); }
1195 { proto_hdr_field_set_u8
(hdr
, ICMPV6_TYPE
, ICMPV6_ECHO_REQUEST
); }
1196 | K_TYPE skip_white
'=' K_ECHO_REPLY
1197 { proto_hdr_field_set_u8
(hdr
, ICMPV6_TYPE
, ICMPV6_ECHO_REPLY
); }
1199 { proto_hdr_field_set_u8
(hdr
, ICMPV6_TYPE
, ICMPV6_ECHO_REPLY
); }
1202 : K_ICMP6
{ proto_add
(PROTO_ICMP6
); }
1206 : udp
'(' udp_param_list
')' { }
1212 | udp_expr delimiter udp_param_list
{ }
1216 : K_SPORT
{ proto_field_set
(UDP_SPORT
); }
1217 | K_DPORT
{ proto_field_set
(UDP_DPORT
); }
1218 | K_LEN
{ proto_field_set
(UDP_LEN
); }
1219 | K_CSUM
{ proto_field_set
(UDP_CSUM
); }
1223 : udp_field field_expr skip_white
'=' skip_white field_value_expr
1224 { proto_field_expr_eval
(); }
1225 | udp_field skip_white
'=' skip_white field_value_expr
1226 { proto_field_expr_eval
(); }
1230 : K_UDP
{ proto_add
(PROTO_UDP
); }
1234 : tcp
'(' tcp_param_list
')' { }
1240 | tcp_expr delimiter tcp_param_list
{ }
1244 : K_SPORT
{ proto_field_set
(TCP_SPORT
); }
1245 | K_DPORT
{ proto_field_set
(TCP_DPORT
); }
1246 | K_SEQ
{ proto_field_set
(TCP_SEQ
); }
1247 | K_ACK_SEQ
{ proto_field_set
(TCP_ACK_SEQ
); }
1248 | K_DOFF
{ proto_field_set
(TCP_DOFF
); }
1249 | K_WINDOW
{ proto_field_set
(TCP_WINDOW
); }
1250 | K_CSUM
{ proto_field_set
(TCP_CSUM
); }
1251 | K_URG_PTR
{ proto_field_set
(TCP_URG_PTR
); }
1255 : tcp_field field_expr skip_white
'=' skip_white field_value_expr
1256 { proto_field_expr_eval
(); }
1257 | tcp_field skip_white
'=' skip_white field_value_expr
1258 { proto_field_expr_eval
(); }
1259 | K_CWR
{ proto_hdr_field_set_be16
(hdr
, TCP_CWR
, 1); }
1260 | K_ECE
{ proto_hdr_field_set_be16
(hdr
, TCP_ECE
, 1); }
1261 | K_URG
{ proto_hdr_field_set_be16
(hdr
, TCP_URG
, 1); }
1262 | K_ACK
{ proto_hdr_field_set_be16
(hdr
, TCP_ACK
, 1); }
1263 | K_PSH
{ proto_hdr_field_set_be16
(hdr
, TCP_PSH
, 1); }
1264 | K_RST
{ proto_hdr_field_set_be16
(hdr
, TCP_RST
, 1); }
1265 | K_SYN
{ proto_hdr_field_set_be16
(hdr
, TCP_SYN
, 1); }
1266 | K_FIN
{ proto_hdr_field_set_be16
(hdr
, TCP_FIN
, 1); }
1270 : K_TCP
{ proto_add
(PROTO_TCP
); }
1274 : dns
'(' dns_param_list
')' { }
1280 | dns_expr delimiter dns_param_list
{ }
1284 : K_ID
{ proto_field_set
(DNS_ID
); }
1285 | K_QR
{ proto_field_set
(DNS_QR
); }
1286 | K_OPER
{ proto_field_set
(DNS_OPCODE
); }
1287 | K_AANSWER
{ proto_field_set
(DNS_AA
); }
1288 | K_TRUNC
{ proto_field_set
(DNS_TC
); }
1289 | K_RDESIRED
{ proto_field_set
(DNS_RD
); }
1290 | K_RAVAIL
{ proto_field_set
(DNS_RA
); }
1291 | K_ZERO
{ proto_field_set
(DNS_ZERO
); }
1292 | K_RCODE
{ proto_field_set
(DNS_RCODE
); }
1293 | K_QDCOUNT
{ proto_field_set
(DNS_QD_COUNT
); }
1294 | K_ANCOUNT
{ proto_field_set
(DNS_AN_COUNT
); }
1295 | K_NSCOUNT
{ proto_field_set
(DNS_NS_COUNT
); }
1296 | K_ARCOUNT
{ proto_field_set
(DNS_AR_COUNT
); }
1300 : K_QUERY
{ proto_push_sub_hdr
(DNS_QUERY_HDR
); }
1304 : K_NAME
{ proto_field_set
(DNS_QUERY_NAME
); }
1308 : K_TYPE
{ proto_field_set
(DNS_QUERY_TYPE
); }
1309 | K_CLASS
{ proto_field_set
(DNS_QUERY_CLASS
); }
1313 : dns_query_field field_expr skip_white
'=' skip_white field_value_expr
1314 { proto_field_expr_eval
(); }
1315 | dns_query_field skip_white
'=' skip_white field_value_expr
1316 { proto_field_expr_eval
(); }
1317 | dns_query_name field_expr skip_white
'=' skip_white field_value_expr
1318 { if
(field_expr.type
& FIELD_EXPR_STRING
)
1319 field_expr.type
= FIELD_EXPR_FQDN
;
1320 proto_field_expr_eval
(); }
1321 | dns_query_name skip_white
'=' skip_white field_value_expr
1322 { if
(field_expr.type
& FIELD_EXPR_STRING
)
1323 field_expr.type
= FIELD_EXPR_FQDN
;
1324 proto_field_expr_eval
(); }
1327 dns_query_param_list
1329 | dns_query_expr
{ }
1330 | dns_query_expr delimiter dns_query_param_list
{ }
1334 : dns_query
'(' dns_query_param_list
')' { }
1338 : K_ANSWER
{ proto_push_sub_hdr
(DNS_ANSWER_HDR
); }
1339 | K_AUTH
{ proto_push_sub_hdr
(DNS_AUTH_HDR
); }
1340 | K_ADD
{ proto_push_sub_hdr
(DNS_ADD_HDR
); }
1344 : K_NAME
{ proto_field_set
(DNS_RRECORD_NAME
); }
1347 dns_rrecord_data_addr
1349 { proto_hdr_field_set_u32
(hdr
, DNS_RRECORD_DATA
, $1.s_addr
);
1350 proto_hdr_field_set_be16
(hdr
, DNS_RRECORD_TYPE
, 1); }
1352 { proto_hdr_field_set_bytes
(hdr
, DNS_RRECORD_DATA
, (uint8_t *)&$1.s6_addr
, 16);
1353 proto_hdr_field_set_be16
(hdr
, DNS_RRECORD_TYPE
, 28); }
1356 dns_rrecord_data_fqdn
1358 { char *str
= xstrdup
($1 + 1);
1360 str
[strlen
($1 + 1) - 1] = '\0';
1361 fqdn
= str2fqdn
(str
);
1362 proto_hdr_field_set_bytes
(hdr
, DNS_RRECORD_DATA
, (uint8_t *) fqdn
, strlen
(fqdn
) + 1);
1367 dns_rrecord_data_expr
1368 : K_ADDR
'(' skip_white dns_rrecord_data_addr skip_white
')'
1370 | K_NS
'(' skip_white dns_rrecord_data_fqdn skip_white
')'
1371 { proto_hdr_field_set_be16
(hdr
, DNS_RRECORD_TYPE
, 2); }
1372 | K_CNAME
'(' skip_white dns_rrecord_data_fqdn skip_white
')'
1373 { proto_hdr_field_set_be16
(hdr
, DNS_RRECORD_TYPE
, 5); }
1374 | K_PTR
'(' skip_white dns_rrecord_data_fqdn skip_white
')'
1375 { proto_hdr_field_set_be16
(hdr
, DNS_RRECORD_TYPE
, 12); }
1379 : K_TYPE
{ proto_field_set
(DNS_RRECORD_TYPE
); }
1380 | K_CLASS
{ proto_field_set
(DNS_RRECORD_CLASS
); }
1381 | K_TTL
{ proto_field_set
(DNS_RRECORD_TTL
); }
1382 | K_LEN
{ proto_field_set
(DNS_RRECORD_LEN
); }
1383 | K_DATA
{ proto_field_set
(DNS_RRECORD_DATA
); }
1387 : dns_rrecord_field field_expr skip_white
'=' skip_white field_value_expr
1388 { proto_field_expr_eval
(); }
1389 | dns_rrecord_field skip_white
'=' skip_white field_value_expr
1390 { proto_field_expr_eval
(); }
1391 | dns_rrecord_name field_expr skip_white
'=' skip_white field_value_expr
1392 { if
(field_expr.type
& FIELD_EXPR_STRING
)
1393 field_expr.type
= FIELD_EXPR_FQDN
;
1394 proto_field_expr_eval
(); }
1395 | dns_rrecord_name skip_white
'=' skip_white field_value_expr
1396 { if
(field_expr.type
& FIELD_EXPR_STRING
)
1397 field_expr.type
= FIELD_EXPR_FQDN
;
1398 proto_field_expr_eval
(); }
1399 | dns_rrecord_data_expr
1403 dns_rrecord_param_list
1405 | dns_rrecord_expr
{ }
1406 | dns_rrecord_expr delimiter dns_rrecord_param_list
{ }
1410 : dns_rrecord
'(' dns_rrecord_param_list
')' { }
1414 : dns_field field_expr skip_white
'=' skip_white field_value_expr
1415 { proto_field_expr_eval
(); }
1416 | dns_field skip_white
'=' skip_white field_value_expr
1417 { proto_field_expr_eval
(); }
1418 | dns_query_hdr
{ proto_pop_sub_hdr
(); }
1419 | dns_rrecord_hdr
{ proto_pop_sub_hdr
(); }
1423 : K_DNS
{ proto_add
(PROTO_DNS
); }
1427 static void finalize_packet
(void)
1429 /* XXX hack ... we allocated one packet pointer too much */
1434 static void dump_conf
(void)
1438 for
(i
= 0; i
< plen
; ++i
) {
1439 printf
("[%zu] pkt\n", i
);
1440 printf
(" len %zu cnts %zu rnds %zu\n",
1443 packet_dyn
[i
].rlen
);
1445 printf
(" payload ");
1446 for
(j
= 0; j
< packets
[i
].len
; ++j
)
1447 printf
("%02x ", packets
[i
].payload
[j
]);
1450 for
(j
= 0; j
< packet_dyn
[i
].clen
; ++j
)
1451 printf
(" cnt%zu [%u,%u], inc %u, off %jd type %s\n", j
,
1452 packet_dyn
[i
].cnt
[j
].min
,
1453 packet_dyn
[i
].cnt
[j
].max
,
1454 packet_dyn
[i
].cnt
[j
].inc
,
1455 (intmax_t)packet_dyn
[i
].cnt
[j
].off
,
1456 packet_dyn
[i
].cnt
[j
].type
== TYPE_INC ?
1459 for
(j
= 0; j
< packet_dyn
[i
].rlen
; ++j
)
1460 printf
(" rnd%zu off %jd\n", j
,
1461 (intmax_t)packet_dyn
[i
].rnd
[j
].off
);
1465 void cleanup_packets
(void)
1469 for
(i
= 0; i
< plen
; ++i
) {
1470 struct packet
*pkt
= &packets
[i
];
1473 xfree
(pkt
->payload
);
1475 for
(j
= 0; j
< pkt
->headers_count
; j
++) {
1476 struct proto_hdr
*hdr
= pkt
->headers
[j
];
1479 for
(k
= 0; k
< hdr
->sub_headers_count
; k
++)
1480 xfree
(hdr
->sub_headers
[k
]);
1482 if
(hdr
->sub_headers
)
1483 xfree
(hdr
->sub_headers
);
1494 for
(i
= 0; i
< dlen
; ++i
) {
1495 free
(packet_dyn
[i
].cnt
);
1496 free
(packet_dyn
[i
].rnd
);
1498 for
(j
= 0; j
< packet_dyn
[j
].flen
; j
++)
1499 xfree
(packet_dyn
[i
].fields
[j
]);
1501 free
(packet_dyn
[i
].fields
);
1507 void compile_packets
(char *file
, bool verbose
, unsigned int cpu
,
1508 bool invoke_cpp
, char *const cpp_argv
[])
1513 if
(access
(file
, R_OK
)) {
1514 fprintf
(stderr
, "Cannot access %s: %s!\n", file
, strerror
(errno
));
1518 memset
(tmp_file
, 0, sizeof
(tmp_file
));
1522 if
(cpp_exec
(file
, tmp_file
, sizeof
(tmp_file
), cpp_argv
)) {
1523 fprintf
(stderr
, "Failed to invoke C preprocessor!\n");
1529 if
(!strncmp
("-", file
, strlen
("-")))
1532 yyin
= fopen
(file
, "r");
1534 fprintf
(stderr
, "Cannot open %s: %s!\n", file
, strerror
(errno
));
1543 if
(our_cpu
== 0 && verbose
)
1548 if
(yyin
&& yyin
!= stdin
)
1557 void compile_packets_str
(char *str
, bool verbose
, unsigned int cpu
)
1564 yy_scan_string
(str
);
1569 if
(our_cpu
== 0 && verbose
)
1580 void yyerror(const char *err
)
1582 fprintf
(stderr
, "Syntax error at line %d, char '%s': %s\n", yylineno
, yytext
, err
);