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"
41 #define ETH_P_8021AD 0x88A8
44 #define YYERROR_VERBOSE 0
46 #define YYENABLE_NLS 1
47 #define YYLTYPE_IS_TRIVIAL 1
51 extern
int yylex(void);
52 extern
void yy_scan_string
(char *);
53 extern
void yylex_destroy
();
54 extern
void yyerror(const char *);
58 extern
struct packet
*packets
;
61 #define packet_last (plen - 1)
63 #define payload_last (packets[packet_last].len - 1)
65 extern
struct packet_dyn
*packet_dyn
;
68 #define packetd_last (dlen - 1)
70 #define packetdc_last (packet_dyn[packetd_last].clen - 1)
71 #define packetdr_last (packet_dyn[packetd_last].rlen - 1)
72 #define packetds_last (packet_dyn[packetd_last].slen - 1)
74 static int our_cpu
, min_cpu
= -1, max_cpu
= -1;
76 enum field_expr_type_t
{
86 struct proto_field_expr
{
87 enum field_expr_type_t type
;
88 struct proto_field
*field
;
91 struct in_addr ip4_addr
;
92 struct in6_addr ip6_addr
;
95 struct proto_field_func func
;
99 static struct proto_field_expr field_expr
;
100 static struct proto_hdr
*hdr
;
102 static inline
int test_ignore
(void)
104 if
(min_cpu
< 0 && max_cpu
< 0)
106 else if
(max_cpu
>= our_cpu
&& min_cpu
<= our_cpu
)
112 static inline
void __init_new_packet_slot
(struct packet
*slot
)
114 slot
->payload
= NULL
;
118 static inline
void __init_new_counter_slot
(struct packet_dyn
*slot
)
120 memset
(slot
, 0, sizeof
(*slot
));
123 static inline
void __init_new_randomizer_slot
(struct packet_dyn
*slot
)
129 static inline
void __init_new_csum_slot
(struct packet_dyn
*slot
)
135 static inline
void __init_new_fields_slot
(struct packet_dyn
*slot
)
141 static inline
void __setup_new_counter
(struct counter
*c
, uint8_t start
,
142 uint8_t stop
, uint8_t stepping
,
148 c
->val
= (type
== TYPE_INC
) ? start
: stop
;
149 c
->off
= payload_last
;
153 static inline
void __setup_new_randomizer
(struct randomizer
*r
)
155 r
->off
= payload_last
;
158 static inline
void __setup_new_csum16
(struct csum16
*s
, off_t from
, off_t to
,
161 s
->off
= payload_last
- 1;
167 static void realloc_packet
(void)
173 packets
= xrealloc
(packets
, plen
* sizeof
(*packets
));
175 __init_new_packet_slot
(&packets
[packet_last
]);
178 packet_dyn
= xrealloc
(packet_dyn
, dlen
* sizeof
(*packet_dyn
));
180 __init_new_counter_slot
(&packet_dyn
[packetd_last
]);
181 __init_new_randomizer_slot
(&packet_dyn
[packetd_last
]);
182 __init_new_csum_slot
(&packet_dyn
[packetd_last
]);
183 __init_new_fields_slot
(&packet_dyn
[packetd_last
]);
186 struct packet
*current_packet
(void)
188 return
&packets
[packet_last
];
191 uint32_t current_packet_id
(void)
196 struct packet
*packet_get
(uint32_t id
)
201 static void set_byte
(uint8_t val
)
203 struct packet
*pkt
= &packets
[packet_last
];
209 pkt
->payload
= xrealloc
(pkt
->payload
, pkt
->len
);
210 pkt
->payload
[payload_last
] = val
;
213 static void set_multi_byte
(uint8_t *s
, size_t len
)
217 for
(i
= 0; i
< len
; ++i
)
221 void set_fill
(uint8_t val
, size_t len
)
224 struct packet
*pkt
= &packets
[packet_last
];
230 pkt
->payload
= xrealloc
(pkt
->payload
, pkt
->len
);
231 for
(i
= 0; i
< len
; ++i
)
232 pkt
->payload
[payload_last
- i
] = val
;
235 static void __set_csum16_dynamic
(size_t from
, size_t to
, enum csum which
)
237 struct packet
*pkt
= &packets
[packet_last
];
238 struct packet_dyn
*pktd
= &packet_dyn
[packetd_last
];
241 pkt
->payload
= xrealloc
(pkt
->payload
, pkt
->len
);
244 pktd
->csum
= xrealloc
(pktd
->csum
, pktd
->slen
* sizeof
(struct csum16
));
246 __setup_new_csum16
(&pktd
->csum
[packetds_last
], from
, to
, which
);
249 static void __set_csum16_static
(size_t from
, size_t to
, enum csum which __maybe_unused
)
251 struct packet
*pkt
= &packets
[packet_last
];
255 sum
= htons
(calc_csum
(pkt
->payload
+ from
, to
- from
));
256 psum
= (uint8_t *) &sum
;
262 static inline
bool is_dynamic_csum
(enum csum which
)
275 static void set_csum16
(size_t from
, size_t to
, enum csum which
)
277 struct packet
*pkt
= &packets
[packet_last
];
278 struct packet_dyn
*pktd
= &packet_dyn
[packetd_last
];
290 bug_on
(!(from
< to
));
292 if
(packet_dyn_has_elems
(pktd
) || to
>= pkt
->len || is_dynamic_csum
(which
))
293 __set_csum16_dynamic
(from
, to
, which
);
295 __set_csum16_static
(from
, to
, which
);
298 static void set_rnd
(size_t len
)
301 struct packet
*pkt
= &packets
[packet_last
];
307 pkt
->payload
= xrealloc
(pkt
->payload
, pkt
->len
);
308 for
(i
= 0; i
< len
; ++i
)
309 pkt
->payload
[payload_last
- i
] = (uint8_t) rand
();
312 static void set_sequential_inc
(uint8_t start
, size_t len
, uint8_t stepping
)
315 struct packet
*pkt
= &packets
[packet_last
];
321 pkt
->payload
= xrealloc
(pkt
->payload
, pkt
->len
);
322 for
(i
= 0; i
< len
; ++i
) {
323 off_t off
= len
- 1 - i
;
325 pkt
->payload
[payload_last
- off
] = start
;
330 static void set_sequential_dec
(uint8_t start
, size_t len
, uint8_t stepping
)
333 struct packet
*pkt
= &packets
[packet_last
];
339 pkt
->payload
= xrealloc
(pkt
->payload
, pkt
->len
);
340 for
(i
= 0; i
< len
; ++i
) {
341 int off
= len
- 1 - i
;
343 pkt
->payload
[payload_last
- off
] = start
;
348 static void set_dynamic_rnd
(void)
350 struct packet
*pkt
= &packets
[packet_last
];
351 struct packet_dyn
*pktd
= &packet_dyn
[packetd_last
];
357 pkt
->payload
= xrealloc
(pkt
->payload
, pkt
->len
);
360 pktd
->rnd
= xrealloc
(pktd
->rnd
, pktd
->rlen
* sizeof
(struct randomizer
));
362 __setup_new_randomizer
(&pktd
->rnd
[packetdr_last
]);
365 static void set_dynamic_incdec
(uint8_t start
, uint8_t stop
, uint8_t stepping
,
368 struct packet
*pkt
= &packets
[packet_last
];
369 struct packet_dyn
*pktd
= &packet_dyn
[packetd_last
];
375 pkt
->payload
= xrealloc
(pkt
->payload
, pkt
->len
);
378 pktd
->cnt
= xrealloc
(pktd
->cnt
, pktd
->clen
* sizeof
(struct counter
));
380 __setup_new_counter
(&pktd
->cnt
[packetdc_last
], start
, stop
, stepping
, type
);
383 static void proto_add
(enum proto_id pid
)
385 hdr
= proto_header_push
(pid
);
388 static void proto_field_set
(uint32_t fid
)
390 field_expr.field
= proto_field_by_id
(hdr
, fid
);
393 static void proto_field_func_setup
(struct proto_field
*field
, struct proto_field_func
*func
)
395 struct packet_dyn
*pkt_dyn
;
397 proto_field_func_add
(field
->hdr
, field
->id
, func
);
399 pkt_dyn
= &packet_dyn
[packetd_last
];
401 pkt_dyn
->fields
= xrealloc
(pkt_dyn
->fields
, pkt_dyn
->flen
*
402 sizeof
(struct proto_field
*));
403 pkt_dyn
->fields
[pkt_dyn
->flen
- 1] = field
;
406 static void proto_field_expr_eval
(void)
408 struct proto_field
*field
= field_expr.field
;
410 switch
(field_expr.type
) {
411 case FIELD_EXPR_NUMB
:
413 proto_field_set_u8
(hdr
, field
->id
, field_expr.val.number
);
414 else if
(field
->len
== 2)
415 proto_field_set_be16
(hdr
, field
->id
, field_expr.val.number
);
416 else if
(field
->len
== 4)
417 proto_field_set_be32
(hdr
, field
->id
, field_expr.val.number
);
423 proto_field_set_bytes
(hdr
, field
->id
, field_expr.val.bytes
);
426 case FIELD_EXPR_IP4_ADDR
:
427 proto_field_set_u32
(hdr
, field
->id
, field_expr.val.ip4_addr.s_addr
);
430 case FIELD_EXPR_IP6_ADDR
:
431 proto_field_set_bytes
(hdr
, field
->id
,
432 (uint8_t *)&field_expr.val.ip6_addr.s6_addr
);
437 if
(field_expr.val.func.min
438 && field_expr.val.func.min
>= field_expr.val.func.max
)
439 panic
("dinc(): min(%u) can't be >= max(%u)\n",
440 field_expr.val.func.min
, field_expr.val.func.max
);
442 proto_field_func_setup
(field
, &field_expr.val.func
);
445 case FIELD_EXPR_UNKNOWN
:
450 memset
(&field_expr
, 0, sizeof
(field_expr
));
456 struct in_addr ip4_addr
;
457 struct in6_addr ip6_addr
;
458 long long int number
;
463 %token K_COMMENT K_FILL K_RND K_SEQINC K_SEQDEC K_DRND K_DINC K_DDEC K_WHITE
464 %token K_CPU K_CSUMIP K_CSUMUDP K_CSUMTCP K_CSUMUDP6 K_CSUMTCP6 K_CONST8 K_CONST16 K_CONST32 K_CONST64
466 %token K_DADDR K_SADDR K_ETYPE K_TYPE
468 %token K_OPER K_SHA K_SPA K_THA K_TPA K_REQUEST K_REPLY K_PTYPE K_HTYPE
469 %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
470 %token K_FLOW K_NEXT_HDR K_HOP_LIMIT
471 %token K_CODE K_ECHO_REQUEST K_ECHO_REPLY
472 %token K_SPORT K_DPORT
473 %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
474 %token K_TPID K_TCI K_PCP K_DEI K_1Q K_1AD
475 %token K_LABEL K_TC K_LAST K_EXP
485 %token K_ICMP4 K_ICMP6
488 %token
',' '{' '}' '(' ')' '[' ']' ':' '-' '+' '*' '/' '%' '&' '|' '<' '>' '^'
490 %token number
string mac ip4_addr ip6_addr
492 %type
<number
> number expression
495 %type
<ip4_addr
> ip4_addr
496 %type
<ip6_addr
> ip6_addr
498 %left
'-' '+' '*' '/' '%' '&' '|' '<' '>' '^'
505 | packets inline_comment
{ }
506 | packets K_WHITE
{ }
526 | delimiter_nowhite
{ }
534 : '{' noenforce_white payload noenforce_white
'}' {
535 min_cpu
= max_cpu
= -1;
537 proto_packet_finish
();
541 | K_CPU
'(' number cpu_delim number
')' ':' noenforce_white
'{' noenforce_white payload noenforce_white
'}' {
545 if
(min_cpu
> max_cpu
) {
552 proto_packet_finish
();
556 | K_CPU
'(' number
')' ':' noenforce_white
'{' noenforce_white payload noenforce_white
'}' {
557 min_cpu
= max_cpu
= $3;
559 proto_packet_finish
();
567 | payload elem_delimiter
{ }
571 : delimiter_nowhite
{ }
580 : number
{ set_byte
((uint8_t) $1); }
581 |
string { set_multi_byte
((uint8_t *) $1 + 1, strlen
($1) - 2); }
591 | proto
{ proto_header_finish
(hdr
); }
598 | expression
'+' expression
600 | expression
'-' expression
602 | expression
'*' expression
604 | expression
'/' expression
606 | expression
'%' expression
608 | expression
'&' expression
610 | expression
'|' expression
612 | expression
'^' expression
614 | expression
'<' '<' expression
616 | expression
'>' '>' expression
625 : K_FILL
'(' number delimiter number
')'
626 { set_fill
($3, $5); }
630 : K_CONST8
'(' expression
')'
631 { set_byte
((uint8_t) $3); }
632 | K_CONST16
'(' expression
')' {
633 uint16_t __c
= cpu_to_be16
((uint16_t) $3);
635 set_multi_byte
((uint8_t *) &__c
, sizeof
(__c
));
637 | K_CONST32
'(' expression
')' {
638 uint32_t __c
= cpu_to_be32
((uint32_t) $3);
640 set_multi_byte
((uint8_t *) &__c
, sizeof
(__c
));
642 | K_CONST64
'(' expression
')' {
643 uint64_t __c
= cpu_to_be64
((uint64_t) $3);
645 set_multi_byte
((uint8_t *) &__c
, sizeof
(__c
));
650 : K_RND
'(' number
')'
655 : K_CSUMIP
'(' number delimiter number
')'
656 { set_csum16
($3, $5, CSUM_IP
); }
657 | K_CSUMTCP
'(' number delimiter number
')'
658 { set_csum16
($3, $5, CSUM_TCP
); }
659 | K_CSUMUDP
'(' number delimiter number
')'
660 { set_csum16
($3, $5, CSUM_UDP
); }
661 | K_CSUMTCP6
'(' number delimiter number
')'
662 { set_csum16
($3, $5, CSUM_TCP6
); }
663 | K_CSUMUDP6
'(' number delimiter number
')'
664 { set_csum16
($3, $5, CSUM_UDP6
); }
668 : K_SEQINC
'(' number delimiter number
')'
669 { set_sequential_inc
($3, $5, 1); }
670 | K_SEQINC
'(' number delimiter number delimiter number
')'
671 { set_sequential_inc
($3, $5, $7); }
675 : K_SEQDEC
'(' number delimiter number
')'
676 { set_sequential_dec
($3, $5, 1); }
677 | K_SEQDEC
'(' number delimiter number delimiter number
')'
678 { set_sequential_dec
($3, $5, $7); }
683 { set_dynamic_rnd
(); }
684 | K_DRND
'(' number
')'
687 for
(i
= 0; i
< max
; ++i
)
693 : K_DINC
'(' number delimiter number
')'
694 { set_dynamic_incdec
($3, $5, 1, TYPE_INC
); }
695 | K_DINC
'(' number delimiter number delimiter number
')'
696 { set_dynamic_incdec
($3, $5, $7, TYPE_INC
); }
700 : K_DDEC
'(' number delimiter number
')'
701 { set_dynamic_incdec
($3, $5, 1, TYPE_DEC
); }
702 | K_DDEC
'(' number delimiter number delimiter number
')'
703 { set_dynamic_incdec
($3, $5, $7, TYPE_DEC
); }
722 : number
{ field_expr.type
= FIELD_EXPR_NUMB
;
723 field_expr.val.number
= $1; }
724 | mac
{ field_expr.type
= FIELD_EXPR_MAC
;
725 memcpy
(field_expr.val.bytes
, $1, sizeof
(field_expr.val.bytes
)); }
726 | ip4_addr
{ field_expr.type
= FIELD_EXPR_IP4_ADDR
;
727 field_expr.val.ip4_addr
= $1; }
728 | ip6_addr
{ field_expr.type
= FIELD_EXPR_IP6_ADDR
;
729 field_expr.val.ip6_addr
= $1; }
730 | K_DINC
'(' ')' { field_expr.type
= FIELD_EXPR_INC
;
731 field_expr.val.func.type
= PROTO_FIELD_FUNC_INC
;
732 field_expr.val.func.inc
= 1; }
733 | K_DINC
'(' number
')'
734 { field_expr.type
= FIELD_EXPR_INC
;
735 field_expr.val.func.type
= PROTO_FIELD_FUNC_INC
;
736 field_expr.val.func.inc
= $3; }
737 | K_DINC
'(' number delimiter number
')'
738 { field_expr.type
= FIELD_EXPR_INC
;
739 field_expr.val.func.type
= PROTO_FIELD_FUNC_INC
;
740 field_expr.val.func.type |
= PROTO_FIELD_FUNC_MIN
;
741 field_expr.val.func.min
= $3;
742 field_expr.val.func.max
= $5;
743 field_expr.val.func.inc
= 1; }
744 | K_DINC
'(' number delimiter number delimiter number
')'
745 { field_expr.type
= FIELD_EXPR_INC
;
746 field_expr.val.func.type
= PROTO_FIELD_FUNC_INC
;
747 field_expr.val.func.type |
= PROTO_FIELD_FUNC_MIN
;
748 field_expr.val.func.min
= $3;
749 field_expr.val.func.max
= $5;
750 field_expr.val.func.inc
= $7; }
751 | K_DRND
'(' ')' { field_expr.type
= FIELD_EXPR_RND
;
752 field_expr.val.func.type
= PROTO_FIELD_FUNC_RND
; }
753 | K_DRND
'(' number delimiter number
')'
754 { field_expr.type
= FIELD_EXPR_RND
;
755 field_expr.val.func.type
= PROTO_FIELD_FUNC_RND
;
756 field_expr.val.func.min
= $3;
757 field_expr.val.func.max
= $5; }
761 : eth
'(' eth_param_list
')' { }
765 : K_ETH
{ proto_add
(PROTO_ETH
); }
771 | eth_expr delimiter eth_param_list
{ }
781 : K_DADDR
{ proto_field_set
(ETH_DST_ADDR
); }
782 | K_SADDR
{ proto_field_set
(ETH_SRC_ADDR
); }
783 | eth_type
{ proto_field_set
(ETH_TYPE
); }
786 : eth_field skip_white
'=' skip_white field_expr
787 { proto_field_expr_eval
(); }
791 : pause
'(' pause_param_list
')' { }
795 : K_PAUSE
{ proto_add
(PROTO_PAUSE
); }
801 | pause_expr delimiter pause_param_list
{ }
805 : K_CODE
{ proto_field_set
(PAUSE_OPCODE
); }
806 | K_TIME
{ proto_field_set
(PAUSE_TIME
); }
810 : pause_field skip_white
'=' skip_white field_expr
811 { proto_field_expr_eval
(); }
815 : pfc
'(' pfc_param_list
')' { }
819 : K_PFC
{ proto_add
(PROTO_PFC
); }
825 | pfc_expr delimiter pfc_param_list
{ }
829 : K_CODE
{ proto_field_set
(PFC_OPCODE
); }
830 | K_PRIO
{ proto_field_set
(PFC_PRIO
); }
831 | K_PRIO
'(' number
')'
833 yyerror("pfc: Invalid prio(index) parameter");
834 panic
("pfc: prio(0)..prio(7) is allowed only\n");
836 proto_field_set
(PFC_PRIO_0
+ $3); }
837 | K_TIME
'(' number
')'
839 yyerror("pfc: Invalid time(index) parameter");
840 panic
("pfc: time(0)..time(7) is allowed only\n");
842 proto_field_set
(PFC_TIME_0
+ $3); }
846 : pfc_field skip_white
'=' skip_white field_expr
847 { proto_field_expr_eval
(); }
851 : vlan
'(' vlan_param_list
')' { }
855 : K_VLAN
{ proto_add
(PROTO_VLAN
); }
861 | vlan_expr delimiter vlan_param_list
{ }
870 : vlan_type
{ proto_field_set
(VLAN_TPID
); }
871 | K_TCI
{ proto_field_set
(VLAN_TCI
); }
872 | K_PCP
{ proto_field_set
(VLAN_PCP
); }
873 | K_DEI
{ proto_field_set
(VLAN_DEI
); }
874 | K_ID
{ proto_field_set
(VLAN_VID
); }
878 : vlan_field skip_white
'=' skip_white field_expr
879 { proto_field_expr_eval
(); }
881 { proto_field_set_be16
(hdr
, VLAN_TPID
, ETH_P_8021Q
); }
883 { proto_field_set_be16
(hdr
, VLAN_TPID
, ETH_P_8021AD
); }
887 : mpls
'(' mpls_param_list
')' { }
891 : K_MPLS
{ proto_add
(PROTO_MPLS
); }
897 | mpls_expr delimiter mpls_param_list
{ }
906 : K_LABEL
{ proto_field_set
(MPLS_LABEL
); }
907 | mpls_tc
{ proto_field_set
(MPLS_TC
); }
908 | K_LAST
{ proto_field_set
(MPLS_LAST
); }
909 | K_TTL
{ proto_field_set
(MPLS_TTL
); }
913 : mpls_field skip_white
'=' skip_white field_expr
914 { proto_field_expr_eval
(); }
917 : arp
'(' arp_param_list
')' { }
923 | arp_expr delimiter arp_param_list
{ }
928 { proto_field_set
(ARP_HTYPE
); }
930 { proto_field_set
(ARP_PTYPE
); }
932 { proto_field_set
(ARP_SHA
); }
934 { proto_field_set
(ARP_THA
); }
936 { proto_field_set
(ARP_SPA
); }
938 { proto_field_set
(ARP_TPA
); }
942 : arp_field skip_white
'=' skip_white field_expr
943 { proto_field_expr_eval
(); }
944 | K_OPER skip_white
'=' skip_white field_expr
945 { proto_field_set
(ARP_OPER
);
946 proto_field_expr_eval
(); }
947 | K_OPER skip_white
'=' skip_white K_REQUEST
948 { proto_field_set_be16
(hdr
, ARP_OPER
, ARPOP_REQUEST
); }
949 | K_OPER skip_white
'=' skip_white K_REPLY
950 { proto_field_set_be16
(hdr
, ARP_OPER
, ARPOP_REPLY
); }
952 { proto_field_set_be16
(hdr
, ARP_OPER
, ARPOP_REQUEST
); }
954 { proto_field_set_be16
(hdr
, ARP_OPER
, ARPOP_REPLY
); }
957 : K_ARP
{ proto_add
(PROTO_ARP
); }
961 : ip4
'(' ip4_param_list
')' { }
967 | ip4_expr delimiter ip4_param_list
{ }
971 : K_VER
{ proto_field_set
(IP4_VER
); }
972 | K_IHL
{ proto_field_set
(IP4_IHL
); }
973 | K_DADDR
{ proto_field_set
(IP4_DADDR
); }
974 | K_SADDR
{ proto_field_set
(IP4_SADDR
); }
975 | K_PROT
{ proto_field_set
(IP4_PROTO
); }
976 | K_TTL
{ proto_field_set
(IP4_TTL
); }
977 | K_DSCP
{ proto_field_set
(IP4_DSCP
); }
978 | K_ECN
{ proto_field_set
(IP4_ECN
); }
979 | K_TOS
{ proto_field_set
(IP4_TOS
); }
980 | K_LEN
{ proto_field_set
(IP4_LEN
); }
981 | K_ID
{ proto_field_set
(IP4_ID
); }
982 | K_FLAGS
{ proto_field_set
(IP4_FLAGS
); }
983 | K_FRAG
{ proto_field_set
(IP4_FRAG_OFFS
); }
984 | K_CSUM
{ proto_field_set
(IP4_CSUM
); }
988 : ip4_field skip_white
'=' skip_white field_expr
989 { proto_field_expr_eval
(); }
990 | K_DF
{ proto_field_set_be16
(hdr
, IP4_DF
, 1); }
991 | K_MF
{ proto_field_set_be16
(hdr
, IP4_MF
, 1); }
995 : K_IP4
{ proto_add
(PROTO_IP4
); }
999 : ip6
'(' ip6_param_list
')' { }
1005 | ip6_expr delimiter ip6_param_list
{ }
1014 : K_VER
{ proto_field_set
(IP6_VER
); }
1015 | K_TC
{ proto_field_set
(IP6_CLASS
); }
1016 | K_FLOW
{ proto_field_set
(IP6_FLOW_LBL
); }
1017 | K_LEN
{ proto_field_set
(IP6_LEN
); }
1018 | K_NEXT_HDR
{ proto_field_set
(IP6_NEXT_HDR
); }
1019 | ip6_hop_limit
{ proto_field_set
(IP6_HOP_LIMIT
); }
1020 | K_SADDR
{ proto_field_set
(IP6_SADDR
); }
1021 | K_DADDR
{ proto_field_set
(IP6_DADDR
) ; }
1025 : ip6_field skip_white
'=' skip_white field_expr
1026 { proto_field_expr_eval
(); }
1030 : K_IP6
{ proto_add
(PROTO_IP6
); }
1034 : icmp4
'(' icmp4_param_list
')' { }
1040 | icmp4_expr delimiter icmp4_param_list
{ }
1044 : K_TYPE
{ proto_field_set
(ICMPV4_TYPE
); }
1045 | K_CODE
{ proto_field_set
(ICMPV4_CODE
); }
1046 | K_ID
{ proto_field_set
(ICMPV4_ID
); }
1047 | K_SEQ
{ proto_field_set
(ICMPV4_SEQ
); }
1048 | K_MTU
{ proto_field_set
(ICMPV4_MTU
); }
1049 | K_ADDR
{ proto_field_set
(ICMPV4_REDIR_ADDR
); }
1053 : icmp4_field skip_white
'=' skip_white field_expr
1054 { proto_field_expr_eval
(); }
1056 { proto_field_set_u8
(hdr
, ICMPV4_TYPE
, ICMP_ECHO
);
1057 proto_field_set_u8
(hdr
, ICMPV4_CODE
, 0); }
1059 { proto_field_set_u8
(hdr
, ICMPV4_TYPE
, ICMP_ECHOREPLY
);
1060 proto_field_set_u8
(hdr
, ICMPV4_CODE
, 0); }
1064 : K_ICMP4
{ proto_add
(PROTO_ICMP4
); }
1068 : icmp6
'(' icmp6_param_list
')' { }
1073 | icmp6_expr delimiter icmp6_param_list
{ }
1077 : K_CODE
{ proto_field_set
(ICMPV6_CODE
); }
1078 | K_CSUM
{ proto_field_set
(ICMPV6_CSUM
); }
1082 : icmp6_field skip_white
'=' skip_white field_expr
1083 { proto_field_expr_eval
(); }
1084 | K_TYPE skip_white
'=' skip_white field_expr
1085 { proto_field_set
(ICMPV6_TYPE
);
1086 proto_field_expr_eval
(); }
1087 | K_TYPE skip_white
'=' K_ECHO_REQUEST
1088 { proto_field_set_u8
(hdr
, ICMPV6_TYPE
, ICMPV6_ECHO_REQUEST
); }
1090 { proto_field_set_u8
(hdr
, ICMPV6_TYPE
, ICMPV6_ECHO_REQUEST
); }
1091 | K_TYPE skip_white
'=' K_ECHO_REPLY
1092 { proto_field_set_u8
(hdr
, ICMPV6_TYPE
, ICMPV6_ECHO_REPLY
); }
1094 { proto_field_set_u8
(hdr
, ICMPV6_TYPE
, ICMPV6_ECHO_REPLY
); }
1097 : K_ICMP6
{ proto_add
(PROTO_ICMP6
); }
1101 : udp
'(' udp_param_list
')' { }
1107 | udp_expr delimiter udp_param_list
{ }
1111 : K_SPORT
{ proto_field_set
(UDP_SPORT
); }
1112 | K_DPORT
{ proto_field_set
(UDP_DPORT
); }
1113 | K_LEN
{ proto_field_set
(UDP_LEN
); }
1114 | K_CSUM
{ proto_field_set
(UDP_CSUM
); }
1118 : udp_field skip_white
'=' skip_white field_expr
1119 { proto_field_expr_eval
(); }
1123 : K_UDP
{ proto_add
(PROTO_UDP
); }
1127 : tcp
'(' tcp_param_list
')' { }
1133 | tcp_expr delimiter tcp_param_list
{ }
1137 : K_SPORT
{ proto_field_set
(TCP_SPORT
); }
1138 | K_DPORT
{ proto_field_set
(TCP_DPORT
); }
1139 | K_SEQ
{ proto_field_set
(TCP_SEQ
); }
1140 | K_ACK_SEQ
{ proto_field_set
(TCP_ACK_SEQ
); }
1141 | K_DOFF
{ proto_field_set
(TCP_DOFF
); }
1142 | K_WINDOW
{ proto_field_set
(TCP_WINDOW
); }
1143 | K_CSUM
{ proto_field_set
(TCP_CSUM
); }
1144 | K_URG_PTR
{ proto_field_set
(TCP_URG_PTR
); }
1148 : tcp_field skip_white
'=' skip_white field_expr
1149 { proto_field_expr_eval
(); }
1150 | K_CWR
{ proto_field_set_be16
(hdr
, TCP_CWR
, 1); }
1151 | K_ECE
{ proto_field_set_be16
(hdr
, TCP_ECE
, 1); }
1152 | K_URG
{ proto_field_set_be16
(hdr
, TCP_URG
, 1); }
1153 | K_ACK
{ proto_field_set_be16
(hdr
, TCP_ACK
, 1); }
1154 | K_PSH
{ proto_field_set_be16
(hdr
, TCP_PSH
, 1); }
1155 | K_RST
{ proto_field_set_be16
(hdr
, TCP_RST
, 1); }
1156 | K_SYN
{ proto_field_set_be16
(hdr
, TCP_SYN
, 1); }
1157 | K_FIN
{ proto_field_set_be16
(hdr
, TCP_FIN
, 1); }
1161 : K_TCP
{ proto_add
(PROTO_TCP
); }
1166 static void finalize_packet
(void)
1168 /* XXX hack ... we allocated one packet pointer too much */
1173 static void dump_conf
(void)
1177 for
(i
= 0; i
< plen
; ++i
) {
1178 printf
("[%zu] pkt\n", i
);
1179 printf
(" len %zu cnts %zu rnds %zu\n",
1182 packet_dyn
[i
].rlen
);
1184 printf
(" payload ");
1185 for
(j
= 0; j
< packets
[i
].len
; ++j
)
1186 printf
("%02x ", packets
[i
].payload
[j
]);
1189 for
(j
= 0; j
< packet_dyn
[i
].clen
; ++j
)
1190 printf
(" cnt%zu [%u,%u], inc %u, off %jd type %s\n", j
,
1191 packet_dyn
[i
].cnt
[j
].min
,
1192 packet_dyn
[i
].cnt
[j
].max
,
1193 packet_dyn
[i
].cnt
[j
].inc
,
1194 (intmax_t)packet_dyn
[i
].cnt
[j
].off
,
1195 packet_dyn
[i
].cnt
[j
].type
== TYPE_INC ?
1198 for
(j
= 0; j
< packet_dyn
[i
].rlen
; ++j
)
1199 printf
(" rnd%zu off %jd\n", j
,
1200 (intmax_t)packet_dyn
[i
].rnd
[j
].off
);
1204 void cleanup_packets
(void)
1208 for
(i
= 0; i
< plen
; ++i
) {
1209 struct packet
*pkt
= &packets
[i
];
1212 xfree
(pkt
->payload
);
1214 for
(j
= 0; j
< pkt
->headers_count
; j
++) {
1215 struct proto_hdr
*hdr
= pkt
->headers
[j
];
1225 for
(i
= 0; i
< dlen
; ++i
) {
1226 free
(packet_dyn
[i
].cnt
);
1227 free
(packet_dyn
[i
].rnd
);
1228 free
(packet_dyn
[i
].fields
);
1234 void compile_packets
(char *file
, bool verbose
, unsigned int cpu
,
1235 bool invoke_cpp
, char *const cpp_argv
[])
1240 if
(access
(file
, R_OK
)) {
1241 fprintf
(stderr
, "Cannot access %s: %s!\n", file
, strerror
(errno
));
1245 memset
(tmp_file
, 0, sizeof
(tmp_file
));
1249 if
(cpp_exec
(file
, tmp_file
, sizeof
(tmp_file
), cpp_argv
)) {
1250 fprintf
(stderr
, "Failed to invoke C preprocessor!\n");
1256 if
(!strncmp
("-", file
, strlen
("-")))
1259 yyin
= fopen
(file
, "r");
1261 fprintf
(stderr
, "Cannot open %s: %s!\n", file
, strerror
(errno
));
1270 if
(our_cpu
== 0 && verbose
)
1275 if
(yyin
&& yyin
!= stdin
)
1284 void compile_packets_str
(char *str
, bool verbose
, unsigned int cpu
)
1291 yy_scan_string
(str
);
1296 if
(our_cpu
== 0 && verbose
)
1307 void yyerror(const char *err
)
1309 fprintf
(stderr
, "Syntax error at line %d, char '%s': %s\n", yylineno
, yytext
, err
);