Add 'icmpv4()' function to create ICMPv4 header.

Parameters suported:

    type      Set type field (default 0: Echo reply)
    code      Set code field (default 0)
    csum      Set checksum field (calculated by default)
    mtu       Set mtu field for destination unreachable (default 0)
    seq       Set sequence field (default 0)
    id        Set identifier field (default 0)
    addr      Set redirect address (default 0)

Example: Send Ping request:

        { icmpv4(echorequest, seq=1, id=1326) }

Signed-off-by: Vadim Kochan <[email protected]>
---
 trafgen_lexer.l  |  6 ++++++
 trafgen_parser.y | 41 ++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 46 insertions(+), 1 deletion(-)

diff --git a/trafgen_lexer.l b/trafgen_lexer.l
index 87db504..5873eec 100644
--- a/trafgen_lexer.l
+++ b/trafgen_lexer.l
@@ -162,6 +162,11 @@ ip6_addr   
(({a_hex}?:)?({a_hex}?:)?({a_hex}?:)?({a_hex}?:)?({a_hex}?:)?({a_hex}?:
 "nh"|"nexthdr" { return K_NEXT_HDR; }
 "hl"|"hoplimit"        { return K_HOP_LIMIT; }
 
+
+       /* ICMPv4 */
+"addr"         { return K_ADDR; }
+"mtu"          { return K_MTU; }
+
        /* ICMPv6 */
 "code"         { return K_CODE; }
 "echorequest"  { return K_ECHO_REQUEST; }
@@ -192,6 +197,7 @@ ip6_addr    
(({a_hex}?:)?({a_hex}?:)?({a_hex}?:)?({a_hex}?:)?({a_hex}?:)?({a_hex}?:
 "arp"          { return K_ARP; }
 "ip4"|"ipv4"   { return K_IP4; }
 "ip6"|"ipv6"   { return K_IP6; }
+"icmp4"|"icmpv4" { return K_ICMP4; }
 "icmp6"|"icmpv6"       { return K_ICMP6; }
 "udp"          { return K_UDP; }
 "tcp"          { return K_TCP; }
diff --git a/trafgen_parser.y b/trafgen_parser.y
index adb7f5e..0fe8674 100644
--- a/trafgen_parser.y
+++ b/trafgen_parser.y
@@ -19,6 +19,7 @@
 #include <libgen.h>
 #include <net/if_arp.h>
 #include <netinet/in.h>
+#include <linux/icmp.h>
 #include <linux/if_ether.h>
 #include <linux/icmpv6.h>
 
@@ -362,11 +363,13 @@ static void proto_add(enum proto_id pid)
 %token K_TPID K_TCI K_PCP K_DEI K_1Q K_1AD
 %token K_LABEL K_TC K_LAST K_EXP
 
+%token K_ADDR K_MTU
+
 %token K_ETH
 %token K_VLAN K_MPLS
 %token K_ARP
 %token K_IP4 K_IP6
-%token K_ICMP6
+%token K_ICMP4 K_ICMP6
 %token K_UDP K_TCP
 
 %token ',' '{' '}' '(' ')' '[' ']' ':' '-' '+' '*' '/' '%' '&' '|' '<' '>' '^'
@@ -594,6 +597,7 @@ proto
        | arp_proto { }
        | ip4_proto { }
        | ip6_proto { }
+       | icmp4_proto { }
        | icmpv6_proto { }
        | udp_proto { }
        | tcp_proto { }
@@ -817,6 +821,41 @@ ip6
        : K_IP6 { proto_add(PROTO_IP6); }
        ;
 
+icmp4_proto
+       : icmp4 '(' icmp4_param_list ')' { }
+       ;
+
+icmp4_param_list
+       : { }
+       | icmp4_field { }
+       | icmp4_field delimiter icmp4_param_list { }
+       ;
+
+icmp4_field
+       : K_TYPE skip_white '=' skip_white number
+               { proto_field_set_u8(hdr, ICMPV4_TYPE, $5); }
+       | K_CODE skip_white '=' skip_white number
+               { proto_field_set_u8(hdr, ICMPV4_CODE, $5); }
+       | K_ID skip_white '=' skip_white number
+               { proto_field_set_be16(hdr, ICMPV4_ID, $5); }
+       | K_SEQ skip_white '=' skip_white number
+               { proto_field_set_be16(hdr, ICMPV4_SEQ, $5); }
+       | K_MTU skip_white '=' skip_white number
+               { proto_field_set_be16(hdr, ICMPV4_MTU, $5); }
+       | K_ADDR skip_white '=' skip_white ip4_addr
+               { proto_field_set_u32(hdr, ICMPV4_REDIR_ADDR, $5.s_addr); }
+       | K_ECHO_REQUEST
+               { proto_field_set_u8(hdr, ICMPV4_TYPE, ICMP_ECHO);
+                 proto_field_set_u8(hdr, ICMPV4_CODE, 0); }
+       | K_ECHO_REPLY
+               { proto_field_set_u8(hdr, ICMPV4_TYPE, ICMP_ECHOREPLY);
+                 proto_field_set_u8(hdr, ICMPV4_CODE, 0); }
+       ;
+
+icmp4
+       : K_ICMP4       { proto_add(PROTO_ICMP4); }
+       ;
+
 icmpv6_proto
        : icmp6 '(' icmp6_param_list ')' { }
 
-- 
2.6.3

-- 
You received this message because you are subscribed to the Google Groups 
"netsniff-ng" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to