Overflow detection, span-based signatures, flatten control flow
This commit is contained in:
@@ -106,23 +106,25 @@ static bool ip_match_or_broadcast(const ip4_addr& dst) {
|
||||
return ip_match(dst) || dst == IP_BROADCAST_ALL || dst == IP_BROADCAST_SUBNET;
|
||||
}
|
||||
|
||||
static void send_raw(const void* data, size_t len) {
|
||||
static void send_raw(std::span<const uint8_t> data) {
|
||||
dlog_if_slow("send_raw", 1000, [&]{
|
||||
w6300::send(raw_socket, std::span<const uint8_t>{static_cast<const uint8_t*>(data), len});
|
||||
w6300::send(raw_socket, data);
|
||||
});
|
||||
}
|
||||
|
||||
static void handle_arp(const uint8_t* frame, size_t len) {
|
||||
if (len < sizeof(arp_packet)) return;
|
||||
auto& pkt = *reinterpret_cast<const arp_packet*>(frame);
|
||||
static void handle_arp(std::span<const uint8_t> frame, span_writer &tx) {
|
||||
if (frame.size() < sizeof(arp_packet)) return;
|
||||
auto& pkt = *reinterpret_cast<const arp_packet*>(frame.data());
|
||||
|
||||
if (pkt.htype != ARP_HTYPE_ETH) return;
|
||||
if (pkt.ptype != ARP_PTYPE_IPV4) return;
|
||||
if (pkt.hlen != 6 || pkt.plen != 4) return;
|
||||
if (pkt.oper != ARP_OP_REQUEST) return;
|
||||
if (!ip_match(pkt.tpa)) return;
|
||||
if (sizeof(arp_packet) > tx.capacity()) return;
|
||||
|
||||
arp_packet reply = {};
|
||||
auto& reply = *reinterpret_cast<arp_packet*>(tx.data());
|
||||
reply = {};
|
||||
reply.eth.dst = pkt.eth.src;
|
||||
reply.eth.src = state.mac;
|
||||
reply.eth.ethertype = ETH_ARP;
|
||||
@@ -136,12 +138,12 @@ static void handle_arp(const uint8_t* frame, size_t len) {
|
||||
reply.tha = pkt.sha;
|
||||
reply.tpa = pkt.spa;
|
||||
|
||||
send_raw(&reply, sizeof(reply));
|
||||
send_raw({tx.data(), sizeof(arp_packet)});
|
||||
}
|
||||
|
||||
static void handle_udp(const uint8_t* frame, size_t len, span_writer &tx) {
|
||||
if (len < sizeof(udp_header)) return;
|
||||
auto& pkt = *reinterpret_cast<const udp_header*>(frame);
|
||||
static void handle_udp(std::span<const uint8_t> frame, span_writer &tx) {
|
||||
if (frame.size() < sizeof(udp_header)) return;
|
||||
auto& pkt = *reinterpret_cast<const udp_header*>(frame.data());
|
||||
|
||||
if (pkt.dst_port != PICOMAP_PORT) return;
|
||||
if (!ip_match_or_broadcast(pkt.ip.dst)) return;
|
||||
@@ -151,12 +153,12 @@ static void handle_udp(const uint8_t* frame, size_t len, span_writer &tx) {
|
||||
if (udp_len < 8) return;
|
||||
if (sizeof(eth_header) + pkt.ip.ip_total_len() < sizeof(udp_header) + udp_len - 8) return;
|
||||
|
||||
auto* payload = frame + sizeof(udp_header);
|
||||
size_t payload_len = udp_len - 8;
|
||||
|
||||
auto resp = tx.subspan(sizeof(udp_header));
|
||||
size_t resp_len = msg_handler(std::span<const uint8_t>{payload, payload_len}, resp);
|
||||
if (resp_len == 0) return;
|
||||
auto result = msg_handler(frame.subspan(sizeof(udp_header), payload_len), resp);
|
||||
if (!result || *result == 0) return;
|
||||
size_t resp_len = *result;
|
||||
|
||||
size_t ip_total = 20 + 8 + resp_len;
|
||||
size_t reply_len = sizeof(eth_header) + ip_total;
|
||||
@@ -183,19 +185,19 @@ static void handle_udp(const uint8_t* frame, size_t len, span_writer &tx) {
|
||||
rudp.length = __builtin_bswap16(8 + resp_len);
|
||||
rudp.checksum = 0;
|
||||
|
||||
send_raw(tx.data(), reply_len);
|
||||
send_raw({tx.data(), reply_len});
|
||||
}
|
||||
|
||||
static void handle_icmp(const uint8_t* frame, size_t len, span_writer &tx) {
|
||||
auto& ip = *reinterpret_cast<const ipv4_header*>(frame);
|
||||
static void handle_icmp(std::span<const uint8_t> frame, span_writer &tx) {
|
||||
auto& ip = *reinterpret_cast<const ipv4_header*>(frame.data());
|
||||
size_t ip_hdr_len = ip.ip_header_len();
|
||||
size_t ip_total = ip.ip_total_len();
|
||||
|
||||
if (sizeof(eth_header) + ip_total > len) return;
|
||||
if (sizeof(eth_header) + ip_total > frame.size()) return;
|
||||
if (ip.protocol != 1) return;
|
||||
if (!ip_match_or_broadcast(ip.dst)) return;
|
||||
|
||||
auto& icmp = *reinterpret_cast<const icmp_echo*>(frame + sizeof(eth_header) + ip_hdr_len);
|
||||
auto& icmp = *reinterpret_cast<const icmp_echo*>(frame.data() + sizeof(eth_header) + ip_hdr_len);
|
||||
size_t icmp_len = ip_total - ip_hdr_len;
|
||||
if (icmp_len < sizeof(icmp_echo)) return;
|
||||
if (icmp.type != 8) return;
|
||||
@@ -203,7 +205,7 @@ static void handle_icmp(const uint8_t* frame, size_t len, span_writer &tx) {
|
||||
size_t reply_len = sizeof(eth_header) + ip_total;
|
||||
if (reply_len > tx.capacity()) return;
|
||||
|
||||
memcpy(tx.data(), frame, reply_len);
|
||||
memcpy(tx.data(), frame.data(), reply_len);
|
||||
auto& rip = *reinterpret_cast<ipv4_header*>(tx.data());
|
||||
rip.eth.dst = ip.eth.src;
|
||||
rip.eth.src = state.mac;
|
||||
@@ -218,36 +220,36 @@ static void handle_icmp(const uint8_t* frame, size_t len, span_writer &tx) {
|
||||
ricmp.checksum = 0;
|
||||
ricmp.checksum = ip_checksum(&ricmp, icmp_len);
|
||||
|
||||
send_raw(tx.data(), reply_len);
|
||||
send_raw({tx.data(), reply_len});
|
||||
}
|
||||
|
||||
static void handle_ipv4(const uint8_t* frame, size_t len, span_writer &tx) {
|
||||
if (len < sizeof(ipv4_header)) return;
|
||||
auto& ip = *reinterpret_cast<const ipv4_header*>(frame);
|
||||
static void handle_ipv4(std::span<const uint8_t> frame, span_writer &tx) {
|
||||
if (frame.size() < sizeof(ipv4_header)) return;
|
||||
auto& ip = *reinterpret_cast<const ipv4_header*>(frame.data());
|
||||
if ((ip.ver_ihl >> 4) != 4) return;
|
||||
|
||||
switch (ip.protocol) {
|
||||
case 1:
|
||||
handle_icmp(frame, len, tx);
|
||||
handle_icmp(frame, tx);
|
||||
break;
|
||||
case 17:
|
||||
handle_udp(frame, len, tx);
|
||||
handle_udp(frame, tx);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void process_frame(const uint8_t* frame, size_t len, span_writer &tx) {
|
||||
if (len < sizeof(eth_header)) return;
|
||||
auto& eth = *reinterpret_cast<const eth_header*>(frame);
|
||||
static void process_frame(std::span<const uint8_t> frame, span_writer &tx) {
|
||||
if (frame.size() < sizeof(eth_header)) return;
|
||||
auto& eth = *reinterpret_cast<const eth_header*>(frame.data());
|
||||
|
||||
if (!mac_match(eth.dst)) return;
|
||||
|
||||
switch (eth.ethertype) {
|
||||
case ETH_ARP:
|
||||
handle_arp(frame, len);
|
||||
handle_arp(frame, tx);
|
||||
break;
|
||||
case ETH_IPV4:
|
||||
handle_ipv4(frame, len, tx);
|
||||
handle_ipv4(frame, tx);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -295,6 +297,6 @@ void net_poll(std::span<uint8_t> tx) {
|
||||
auto result = w6300::recv(raw_socket, std::span{rx_buf});
|
||||
if (!result) break;
|
||||
span_writer tx_writer(tx);
|
||||
process_frame(rx_buf, *result, tx_writer);
|
||||
process_frame({rx_buf, *result}, tx_writer);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user