diff options
author | Maciej Żenczykowski <maze@google.com> | 2023-03-13 21:50:01 +0000 |
---|---|---|
committer | Maciej Żenczykowski <maze@google.com> | 2023-03-13 21:56:16 +0000 |
commit | f3eeff99df06d1370a71706dfd0f0a90202c47cc (patch) | |
tree | 588a1a731bbb204358e31de288c39ad49992504b | |
parent | 1f395ef0818211be563c42b8e958b062ed18ef6a (diff) | |
download | android-clat-f3eeff99df06d1370a71706dfd0f0a90202c47cc.tar.gz |
clatd: rework buffer sizes
The function internal implementation details of the true
required number of bytes for these buffers doesn't really
belong in the header file.
Test: TreeHugger
Bug: 265591307
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I1aef7adc4c8af9b53d2c0b8da0eb2c1c6e6dd12a
-rw-r--r-- | clatd.c | 12 | ||||
-rw-r--r-- | clatd.h | 15 |
2 files changed, 14 insertions, 13 deletions
@@ -80,10 +80,10 @@ int ipv6_address_changed(const char *interface) { // reads L3 IPv6 packet from AF_PACKET socket, translates to IPv4, writes to tun void process_packet_6_to_4(struct tun_data *tunnel) { char cmsg_buf[CMSG_SPACE(sizeof(struct tpacket_auxdata))]; - uint8_t buf[MAXMTU]; + uint8_t buf[MAXMTU + 1]; // +1 to make packet truncation obvious struct iovec iov = { .iov_base = buf, - .iov_len = MAXMTU, + .iov_len = sizeof(buf), }; struct msghdr msgh = { .msg_iov = &iov, @@ -102,7 +102,7 @@ void process_packet_6_to_4(struct tun_data *tunnel) { logmsg(ANDROID_LOG_WARN, "%s: packet socket removed?", __func__); running = 0; return; - } else if (readlen >= MAXMTU) { + } else if (readlen >= sizeof(buf)) { logmsg(ANDROID_LOG_WARN, "%s: read truncation - ignoring pkt", __func__); return; } @@ -131,8 +131,8 @@ void process_packet_6_to_4(struct tun_data *tunnel) { // reads TUN_PI + L3 IPv4 packet from tun, translates to IPv6, writes to AF_INET6/RAW socket void process_packet_4_to_6(struct tun_data *tunnel) { - uint8_t buf[PACKETLEN]; - ssize_t readlen = read(tunnel->fd4, buf, PACKETLEN); + uint8_t buf[sizeof(struct tun_pi) + MAXMTU + 1]; // +1 to make packet truncation obvious + ssize_t readlen = read(tunnel->fd4, buf, sizeof(buf)); if (readlen < 0) { if (errno != EAGAIN) { @@ -143,7 +143,7 @@ void process_packet_4_to_6(struct tun_data *tunnel) { logmsg(ANDROID_LOG_WARN, "%s: tun interface removed", __func__); running = 0; return; - } else if (readlen >= PACKETLEN) { + } else if (readlen >= sizeof(buf)) { logmsg(ANDROID_LOG_WARN, "%s: read truncation - ignoring pkt", __func__); return; } @@ -38,14 +38,15 @@ struct tun_data; // // (since there's no jumbogram support in IPv4, IPv6 jumbograms cannot be meaningfully // converted to IPv4 anyway, and are thus entirely unsupported) -// -// We bump it by one more, since it makes truncation more obvious. -// ie. if we ever read >= MAXMTU bytes we should discard. -#define MAXMTU (0xFFFF + 28 + 1) -#define PACKETLEN (sizeof(struct tun_pi) + MAXMTU) +#define MAXMTU (0xFFFF + 28) -// logcat_hexdump() maximum binary data length -#define MAXDUMPLEN PACKETLEN +// logcat_hexdump() maximum binary data length, this is the maximum packet size +// plus some extra space for various headers: +// struct tun_pi (4 bytes) +// struct virtio_net_hdr (10 bytes) +// ethernet (14 bytes), potentially including vlan tag (4) or tags (8 or 12) +// plus some extra just-in-case headroom, because it doesn't hurt. +#define MAXDUMPLEN (64 + MAXMTU) #define CLATD_VERSION "1.6" |