diff options
author | Maciej Żenczykowski <maze@google.com> | 2023-03-16 02:42:37 +0000 |
---|---|---|
committer | Maciej Żenczykowski <maze@google.com> | 2023-03-16 05:44:26 +0000 |
commit | a4df1010f69d1cff2a38e4f7aafb45dfc438cd37 (patch) | |
tree | 00864d5374dbfbf01875c38f677c6622403696ea | |
parent | aae2fb956c405449ba76827fa558ef440bd890f9 (diff) | |
download | android-clat-a4df1010f69d1cff2a38e4f7aafb45dfc438cd37.tar.gz |
clatd: use offsetof
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I51fc4c669657042c120d1b8a4d35add6b51b4139
-rw-r--r-- | clatd.c | 18 |
1 files changed, 11 insertions, 7 deletions
@@ -127,13 +127,14 @@ void process_packet_6_to_4(struct tun_data *tunnel) { } } - if (readlen < sizeof(struct virtio_net_hdr) + tp_net) { - logmsg(ANDROID_LOG_WARN, "%s: ignoring %zd byte pkt shorter than %zu+%u L2 header", - __func__, readlen, sizeof(struct virtio_net_hdr), tp_net); + const int payload_offset = offsetof(typeof(buf), payload); + if (readlen < payload_offset + tp_net) { + logmsg(ANDROID_LOG_WARN, "%s: ignoring %zd byte pkt shorter than %d+%u L2 header", + __func__, readlen, payload_offset, tp_net); return; } - const int pkt_len = readlen - sizeof(struct virtio_net_hdr); + const int pkt_len = readlen - payload_offset; // This will detect a skb->ip_summed == CHECKSUM_PARTIAL packet with non-final L4 checksum if (tp_status & TP_STATUS_CSUMNOTREADY) { @@ -186,11 +187,15 @@ void process_packet_4_to_6(struct tun_data *tunnel) { return; } - if (readlen < (ssize_t)sizeof(buf.pi)) { + const int payload_offset = offsetof(typeof(buf), payload); + + if (readlen < payload_offset) { logmsg(ANDROID_LOG_WARN, "%s: short read: got %ld bytes", __func__, readlen); return; } + const int pkt_len = readlen - payload_offset; + uint16_t proto = ntohs(buf.pi.proto); if (proto != ETH_P_IP) { logmsg(ANDROID_LOG_WARN, "%s: unknown packet type = 0x%x", __func__, proto); @@ -201,8 +206,7 @@ void process_packet_4_to_6(struct tun_data *tunnel) { logmsg(ANDROID_LOG_WARN, "%s: unexpected flags = %d", __func__, buf.pi.flags); } - readlen -= sizeof(buf.pi); - translate_packet(tunnel->write_fd6, 1 /* to_ipv6 */, buf.payload, readlen); + translate_packet(tunnel->write_fd6, 1 /* to_ipv6 */, buf.payload, pkt_len); } // IPv6 DAD packet format: |