Skip to content

Commit 860e2c4

Browse files
committed
packet: add a new parsing helper to parse ICMP/ICMPv6
Adds a new parsing helper which parses the beginning of an ICMP or ICMPv6 header and returns the same structure in both cases. This can be used to parse ICMP in protocol-independent way if we are not interested in the rest of the header. Signed-off-by: Anton Protopopov <a.s.protopopov@gmail.com>
1 parent 99de01c commit 860e2c4

3 files changed

Lines changed: 41 additions & 4 deletions

File tree

common/parsing_helpers.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ struct vlan_hdr {
4242
__be16 h_vlan_encapsulated_proto;
4343
};
4444

45+
/*
46+
* Struct icmphdr_common represents the common part of the icmphdr and icmp6hdr
47+
* structures.
48+
*/
49+
struct icmphdr_common {
50+
__u8 type;
51+
__u8 code;
52+
__sum16 cksum;
53+
};
54+
4555
#define VLAN_MAX_DEPTH 5
4656

4757
static __always_inline int proto_is_vlan(__u16 h_proto)
@@ -160,6 +170,21 @@ static __always_inline int parse_icmphdr(struct hdr_cursor *nh,
160170
return icmph->type;
161171
}
162172

173+
static __always_inline int parse_icmphdr_common(struct hdr_cursor *nh,
174+
void *data_end,
175+
struct icmphdr_common **icmphdr)
176+
{
177+
struct icmphdr_common *h = nh->pos;
178+
179+
if (h + 1 > data_end)
180+
return -1;
181+
182+
nh->pos = h + 1;
183+
*icmphdr = h;
184+
185+
return h->type;
186+
}
187+
163188
static __always_inline struct ethhdr *get_ethhdr(struct hdr_cursor *nh,
164189
void *data_end)
165190
{

packet-solutions/xdp_prog_kern.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ int xdp_icmp_echo_func(struct xdp_md *ctx)
7777
struct iphdr *iphdr;
7878
struct ipv6hdr *ipv6hdr;
7979
__u16 echo_reply, m0, m1;
80-
struct icmphdr *icmphdr;
80+
struct icmphdr_common *icmphdr;
8181
__u32 action = XDP_PASS;
8282

8383
/* These keep track of the next header type and iterator pointer */
@@ -97,7 +97,13 @@ int xdp_icmp_echo_func(struct xdp_md *ctx)
9797
goto out;
9898
}
9999

100-
icmp_type = parse_icmphdr(&nh, data_end, &icmphdr);
100+
/*
101+
* We are using a special parser here which returns a stucture
102+
* containing the "protocol-independent" part of an ICMP or ICMPv6
103+
* header. For purposes of this Assignment we are not interested in
104+
* the rest of the structure.
105+
*/
106+
icmp_type = parse_icmphdr_common(&nh, data_end, &icmphdr);
101107
if (eth_type == ETH_P_IP && icmp_type == ICMP_ECHO) {
102108
/* Swap IP source and destination */
103109
swap_src_dst_ipv4(iphdr);

packet03-redirecting/xdp_prog_kern.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ int xdp_icmp_echo_func(struct xdp_md *ctx)
5858
struct iphdr *iphdr;
5959
struct ipv6hdr *ipv6hdr;
6060
__u16 echo_reply;
61-
struct icmphdr *icmphdr;
61+
struct icmphdr_common *icmphdr;
6262
__u32 action = XDP_PASS;
6363

6464
/* These keep track of the next header type and iterator pointer */
@@ -78,7 +78,13 @@ int xdp_icmp_echo_func(struct xdp_md *ctx)
7878
goto out;
7979
}
8080

81-
icmp_type = parse_icmphdr(&nh, data_end, &icmphdr);
81+
/*
82+
* We are using a special parser here which returns a stucture
83+
* containing the "protocol-independent" part of an ICMP or ICMPv6
84+
* header. For purposes of this Assignment we are not interested in
85+
* the rest of the structure.
86+
*/
87+
icmp_type = parse_icmphdr_common(&nh, data_end, &icmphdr);
8288
if (eth_type == ETH_P_IP && icmp_type == ICMP_ECHO) {
8389
/* Swap IP source and destination */
8490
swap_src_dst_ipv4(iphdr);

0 commit comments

Comments
 (0)