-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ip6: add basic icmp6 echo and reply support
Add code to handle ICMPv6 echo requests. Update the IPv6 forwarding test to also verify that grout responds to them. Signed-off-by: Robin Jarry <[email protected]>
- Loading branch information
Showing
7 changed files
with
321 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
// Copyright (c) 2024 Robin Jarry | ||
|
||
#include <gr_datapath.h> | ||
#include <gr_graph.h> | ||
#include <gr_icmp6.h> | ||
#include <gr_ip6_control.h> | ||
#include <gr_ip6_datapath.h> | ||
#include <gr_log.h> | ||
#include <gr_mbuf.h> | ||
|
||
#include <rte_byteorder.h> | ||
#include <rte_ether.h> | ||
#include <rte_graph_worker.h> | ||
#include <rte_ip.h> | ||
|
||
enum { | ||
ICMP6_OUTPUT = 0, | ||
BAD_CHECKSUM, | ||
INVALID, | ||
UNSUPPORTED, | ||
EDGE_COUNT, | ||
}; | ||
|
||
static uint16_t | ||
icmp6_input_process(struct rte_graph *graph, struct rte_node *node, void **objs, uint16_t nb_objs) { | ||
struct ip6_local_mbuf_data *d; | ||
struct icmp6 *icmp6; | ||
struct rte_ipv6_addr tmp_ip; | ||
struct rte_mbuf *mbuf; | ||
rte_edge_t next; | ||
|
||
for (uint16_t i = 0; i < nb_objs; i++) { | ||
mbuf = objs[i]; | ||
icmp6 = rte_pktmbuf_mtod(mbuf, struct icmp6 *); | ||
d = ip6_local_mbuf_data(mbuf); | ||
|
||
switch (icmp6->type) { | ||
case ICMP6_TYPE_ECHO_REQUEST: | ||
if (icmp6->code != 0) { | ||
next = INVALID; | ||
goto next; | ||
} | ||
icmp6->type = ICMP6_TYPE_ECHO_REPLY; | ||
// swap source/destination addresses | ||
rte_ipv6_addr_cpy(&tmp_ip, &d->dst); | ||
rte_ipv6_addr_cpy(&d->dst, &d->src); | ||
rte_ipv6_addr_cpy(&d->src, &tmp_ip); | ||
next = ICMP6_OUTPUT; | ||
break; | ||
default: | ||
next = UNSUPPORTED; | ||
} | ||
next: | ||
rte_node_enqueue_x1(graph, node, next, mbuf); | ||
} | ||
|
||
return nb_objs; | ||
} | ||
|
||
static void icmp6_input_register(void) { | ||
ip6_input_local_add_proto(IPPROTO_ICMPV6, "icmp6_input"); | ||
} | ||
|
||
static struct rte_node_register icmp6_input_node = { | ||
.name = "icmp6_input", | ||
|
||
.process = icmp6_input_process, | ||
|
||
.nb_edges = EDGE_COUNT, | ||
.next_nodes = { | ||
[ICMP6_OUTPUT] = "icmp6_output", | ||
[BAD_CHECKSUM] = "icmp6_input_bad_checksum", | ||
[INVALID] = "icmp6_input_invalid", | ||
[UNSUPPORTED] = "icmp6_input_unsupported", | ||
}, | ||
}; | ||
|
||
static struct gr_node_info icmp6_input_info = { | ||
.node = &icmp6_input_node, | ||
.register_callback = icmp6_input_register, | ||
}; | ||
|
||
GR_NODE_REGISTER(icmp6_input_info); | ||
|
||
GR_DROP_REGISTER(icmp6_input_bad_checksum); | ||
GR_DROP_REGISTER(icmp6_input_invalid); | ||
GR_DROP_REGISTER(icmp6_input_unsupported); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
// Copyright (c) 2024 Robin Jarry | ||
|
||
#include <gr_datapath.h> | ||
#include <gr_graph.h> | ||
#include <gr_icmp6.h> | ||
#include <gr_ip6_control.h> | ||
#include <gr_ip6_datapath.h> | ||
#include <gr_log.h> | ||
#include <gr_mbuf.h> | ||
|
||
#include <rte_graph_worker.h> | ||
#include <rte_ip.h> | ||
|
||
#include <netinet/in.h> | ||
|
||
enum { | ||
OUTPUT = 0, | ||
NO_HEADROOM, | ||
NO_ROUTE, | ||
EDGE_COUNT, | ||
}; | ||
|
||
static uint16_t icmp6_output_process( | ||
struct rte_graph *graph, | ||
struct rte_node *node, | ||
void **objs, | ||
uint16_t nb_objs | ||
) { | ||
struct ip6_output_mbuf_data *o; | ||
struct ip6_local_mbuf_data *d; | ||
const struct iface *iface; | ||
struct rte_ipv6_hdr *ip; | ||
struct rte_mbuf *mbuf; | ||
struct nexthop6 *nh; | ||
struct icmp6 *icmp6; | ||
rte_edge_t edge; | ||
|
||
for (uint16_t i = 0; i < nb_objs; i++) { | ||
mbuf = objs[i]; | ||
d = ip6_local_mbuf_data(mbuf); | ||
|
||
icmp6 = rte_pktmbuf_mtod(mbuf, struct icmp6 *); | ||
ip = (struct rte_ipv6_hdr *)rte_pktmbuf_prepend(mbuf, sizeof(*ip)); | ||
if (unlikely(ip == NULL)) { | ||
edge = NO_HEADROOM; | ||
goto next; | ||
} | ||
ip6_set_fields(ip, d->len, IPPROTO_ICMPV6, &d->src, &d->dst); | ||
// Compute ICMP6 checksum with pseudo header | ||
icmp6->cksum = 0; | ||
icmp6->cksum = rte_ipv6_udptcp_cksum(ip, icmp6); | ||
|
||
if ((nh = ip6_route_lookup(d->input_iface->vrf_id, &d->dst)) == NULL) { | ||
edge = NO_ROUTE; | ||
goto next; | ||
} | ||
o = ip6_output_mbuf_data(mbuf); | ||
iface = d->input_iface; | ||
o->nh = nh; | ||
o->input_iface = iface; | ||
edge = OUTPUT; | ||
next: | ||
rte_node_enqueue_x1(graph, node, edge, mbuf); | ||
} | ||
|
||
return nb_objs; | ||
} | ||
|
||
static struct rte_node_register icmp6_output_node = { | ||
.name = "icmp6_output", | ||
|
||
.process = icmp6_output_process, | ||
|
||
.nb_edges = EDGE_COUNT, | ||
.next_nodes = { | ||
[OUTPUT] = "ip6_output", | ||
[NO_HEADROOM] = "error_no_headroom", | ||
[NO_ROUTE] = "icmp6_output_no_route", | ||
}, | ||
}; | ||
|
||
static struct gr_node_info icmp6_output_info = { | ||
.node = &icmp6_output_node, | ||
}; | ||
|
||
GR_NODE_REGISTER(icmp6_output_info); | ||
|
||
GR_DROP_REGISTER(icmp6_output_no_route) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
// Copyright (c) 2024 Robin Jarry | ||
|
||
#include <gr_datapath.h> | ||
#include <gr_graph.h> | ||
#include <gr_ip6_control.h> | ||
#include <gr_ip6_datapath.h> | ||
#include <gr_log.h> | ||
|
||
#include <rte_graph_worker.h> | ||
#include <rte_ip6.h> | ||
#include <rte_mbuf.h> | ||
|
||
enum { | ||
UNKNOWN_PROTO = 0, | ||
BAD_CHECKSUM, | ||
EDGE_COUNT, | ||
}; | ||
static rte_edge_t edges[256] = {UNKNOWN_PROTO}; | ||
|
||
void ip6_input_local_add_proto(uint8_t proto, const char *next_node) { | ||
LOG(DEBUG, "ip6_input_local: proto=%hhu -> %s", proto, next_node); | ||
if (edges[proto] != UNKNOWN_PROTO) | ||
ABORT("next node already registered for proto=%hhu", proto); | ||
edges[proto] = gr_node_attach_parent("ip6_input_local", next_node); | ||
} | ||
|
||
static uint16_t ip6_input_local_process( | ||
struct rte_graph *graph, | ||
struct rte_node *node, | ||
void **objs, | ||
uint16_t nb_objs | ||
) { | ||
struct ip6_local_mbuf_data *d; | ||
const struct iface *iface; | ||
struct rte_ipv6_hdr *ip; | ||
struct rte_mbuf *m; | ||
rte_edge_t edge; | ||
uint16_t i; | ||
|
||
for (i = 0; i < nb_objs; i++) { | ||
m = objs[i]; | ||
ip = rte_pktmbuf_mtod(m, struct rte_ipv6_hdr *); | ||
|
||
edge = edges[ip->proto]; | ||
if (edge == UNKNOWN_PROTO) | ||
goto next; | ||
|
||
// prepare ip local data | ||
iface = ip6_output_mbuf_data(m)->input_iface; | ||
d = ip6_local_mbuf_data(m); | ||
rte_ipv6_addr_cpy(&d->src, &ip->src_addr); | ||
rte_ipv6_addr_cpy(&d->dst, &ip->dst_addr); | ||
d->len = rte_be_to_cpu_16(ip->payload_len); | ||
d->hop_limit = ip->hop_limits; | ||
d->proto = ip->proto; | ||
d->input_iface = iface; | ||
rte_pktmbuf_adj(m, sizeof(*ip)); | ||
|
||
// strip IPv6 extension headers | ||
while (rte_pktmbuf_pkt_len(m) > 0) { | ||
size_t ext_size = 0; | ||
int next_proto = rte_ipv6_get_next_ext( | ||
rte_pktmbuf_mtod(m, uint8_t *), d->proto, &ext_size | ||
); | ||
if (next_proto < 0) | ||
break; | ||
rte_pktmbuf_adj(m, ext_size); | ||
d->proto = next_proto; | ||
}; | ||
|
||
// verify checksum if not already checked by hardware | ||
switch (m->ol_flags & RTE_MBUF_F_RX_L4_CKSUM_MASK) { | ||
case RTE_MBUF_F_RX_L4_CKSUM_NONE: | ||
case RTE_MBUF_F_RX_L4_CKSUM_UNKNOWN: | ||
if (rte_ipv6_udptcp_cksum_verify(ip, rte_pktmbuf_mtod(m, void *))) | ||
edge = BAD_CHECKSUM; | ||
break; | ||
case RTE_MBUF_F_RX_L4_CKSUM_BAD: | ||
edge = BAD_CHECKSUM; | ||
break; | ||
} | ||
next: | ||
rte_node_enqueue_x1(graph, node, edge, m); | ||
} | ||
|
||
return nb_objs; | ||
} | ||
|
||
static struct rte_node_register input_node = { | ||
.name = "ip6_input_local", | ||
.process = ip6_input_local_process, | ||
.nb_edges = EDGE_COUNT, | ||
.next_nodes = { | ||
[UNKNOWN_PROTO] = "ip6_input_local_unknown_proto", | ||
[BAD_CHECKSUM] = "ip6_input_local_bad_checksum", | ||
}, | ||
}; | ||
|
||
static struct gr_node_info info = { | ||
.node = &input_node, | ||
}; | ||
|
||
GR_NODE_REGISTER(info); | ||
|
||
GR_DROP_REGISTER(ip6_input_local_unknown_proto); | ||
GR_DROP_REGISTER(ip6_input_local_bad_checksum); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters