summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLorenzo Colitti <lorenzo@google.com>2014-06-02 15:49:36 +0900
committerLorenzo Colitti <lorenzo@google.com>2014-06-10 21:32:52 +0900
commit91d0f1bc6dd24e54ed3caef9b08525b332ab0adf (patch)
treef03037b2887e6e5787d4d47fe1967c57dbcfd3f2
parentfc0f94a75c7a6d16ea9e327092f132a0d2c9175e (diff)
downloadplatform_external_android-clat-91d0f1bc6dd24e54ed3caef9b08525b332ab0adf.tar.gz
platform_external_android-clat-91d0f1bc6dd24e54ed3caef9b08525b332ab0adf.tar.bz2
platform_external_android-clat-91d0f1bc6dd24e54ed3caef9b08525b332ab0adf.zip
Make translate_packet take a fd instead of a tun header.
This will make it easier to use separate fds for reading and writing in a future change. Bug: 15340961 Change-Id: I5b081b05765cae0488ac599be5738ce9737cae41
-rw-r--r--clatd.c21
-rw-r--r--clatd_test.cpp10
-rw-r--r--translate.c25
-rw-r--r--translate.h3
4 files changed, 31 insertions, 28 deletions
diff --git a/clatd.c b/clatd.c
index d8a9e72..af54ff2 100644
--- a/clatd.c
+++ b/clatd.c
@@ -306,7 +306,8 @@ void read_packet(int active_fd, const struct tun_data *tunnel) {
ssize_t readlen;
uint8_t packet[PACKETLEN];
- // in case something ignores the packet length
+ // In case something ignores the packet length.
+ // TODO: remove it.
memset(packet, 0, PACKETLEN);
readlen = read(active_fd,packet,PACKETLEN);
@@ -325,7 +326,23 @@ void read_packet(int active_fd, const struct tun_data *tunnel) {
return;
}
- translate_packet(tunnel, (struct tun_pi *) packet, packet + header_size, readlen - header_size);
+ struct tun_pi *tun_header = (struct tun_pi *) packet;
+ if(tun_header->flags != 0) {
+ logmsg(ANDROID_LOG_WARN, "%s: unexpected flags = %d", __func__, tun_header->flags);
+ }
+
+ int fd;
+ uint16_t proto = ntohs(tun_header->proto);
+ if (proto == ETH_P_IP) {
+ fd = tunnel->fd6;
+ } else if (proto == ETH_P_IPV6) {
+ fd = tunnel->fd4;
+ } else {
+ logmsg(ANDROID_LOG_WARN, "%s: unknown packet type = 0x%x", __func__, proto);
+ return;
+ }
+
+ translate_packet(fd, (proto == ETH_P_IP), packet + header_size, readlen - header_size);
}
}
diff --git a/clatd_test.cpp b/clatd_test.cpp
index 5172d3f..bc32a84 100644
--- a/clatd_test.cpp
+++ b/clatd_test.cpp
@@ -424,17 +424,13 @@ void do_translate_packet(const uint8_t *original, size_t original_len, uint8_t *
if (socketpair(AF_UNIX, SOCK_DGRAM | SOCK_NONBLOCK, 0, fds)) {
abort();
}
- struct tun_data tunnel = {
- "clat", "clat4",
- fds[0], fds[1]
- };
struct tun_pi tun_header = { 0, 0 };
char foo[512];
snprintf(foo, sizeof(foo), "%s: Invalid original packet", msg);
check_packet(original, original_len, foo);
- int read_fd;
+ int read_fd, write_fd;
uint16_t expected_proto;
int version = ip_version(original);
switch (version) {
@@ -442,18 +438,20 @@ void do_translate_packet(const uint8_t *original, size_t original_len, uint8_t *
tun_header.proto = htons(ETH_P_IP);
expected_proto = htons(ETH_P_IPV6);
read_fd = fds[1];
+ write_fd = fds[0];
break;
case 6:
tun_header.proto = htons(ETH_P_IPV6);
expected_proto = htons(ETH_P_IP);
read_fd = fds[0];
+ write_fd = fds[1];
break;
default:
FAIL() << msg << ": Unsupported IP version " << version << "\n";
break;
}
- translate_packet(&tunnel, &tun_header, original, original_len);
+ translate_packet(write_fd, (version == 4), original, original_len);
struct tun_pi new_tun_header;
struct iovec iov[] = {
diff --git a/translate.c b/translate.c
index e93a93a..958ce2f 100644
--- a/translate.c
+++ b/translate.c
@@ -466,15 +466,13 @@ int tcp_translate(clat_packet out, clat_packet_index pos, const struct tcphdr *t
}
/* function: translate_packet
- * takes a tun header and a packet and sends it down the stack
- * tunnel - tun device data
- * tun_header - tun header
+ * takes a packet, translates it, and writes it to fd
+ * fd - fd to write translated packet to
+ * to_ipv6 - true if translating to ipv6, false if translating to ipv4
* packet - packet
* packetsize - size of packet
*/
-void translate_packet(const struct tun_data *tunnel, struct tun_pi *tun_header,
- const uint8_t *packet, size_t packetsize) {
- int fd;
+void translate_packet(int fd, int to_ipv6, const uint8_t *packet, size_t packetsize) {
int iov_len = 0;
// Allocate buffers for all packet headers.
@@ -498,23 +496,14 @@ void translate_packet(const struct tun_data *tunnel, struct tun_pi *tun_header,
{ NULL, 0 }, // Payload. No buffer, it's a pointer to the original payload.
};
- if(tun_header->flags != 0) {
- logmsg(ANDROID_LOG_WARN, "translate_packet: unexpected flags = %d", tun_header->flags);
- }
-
- if(ntohs(tun_header->proto) == ETH_P_IP) {
- fd = tunnel->fd6;
- fill_tun_header(&tun_targ, ETH_P_IPV6);
+ if (to_ipv6) {
iov_len = ipv4_packet(out, CLAT_POS_IPHDR, packet, packetsize);
- } else if(ntohs(tun_header->proto) == ETH_P_IPV6) {
- fd = tunnel->fd4;
- fill_tun_header(&tun_targ, ETH_P_IP);
- iov_len = ipv6_packet(out, CLAT_POS_IPHDR, packet, packetsize);
} else {
- logmsg(ANDROID_LOG_WARN, "translate_packet: unknown packet type = %x",tun_header->proto);
+ iov_len = ipv6_packet(out, CLAT_POS_IPHDR, packet, packetsize);
}
if (iov_len > 0) {
+ fill_tun_header(&tun_targ, to_ipv6 ? ETH_P_IPV6 : ETH_P_IP);
writev(fd, out, iov_len);
}
}
diff --git a/translate.h b/translate.h
index 6d4f126..46e178b 100644
--- a/translate.h
+++ b/translate.h
@@ -60,8 +60,7 @@ void fill_ip6_header(struct ip6_hdr *ip6, uint16_t payload_len, uint8_t protocol
const struct iphdr *old_header);
// Translate and send packets.
-void translate_packet(const struct tun_data *tunnel, struct tun_pi *tun_header,
- const uint8_t *packet, size_t packetsize);
+void translate_packet(int fd, int to_ipv6, const uint8_t *packet, size_t packetsize);
// Translate IPv4 and IPv6 packets.
int ipv4_packet(clat_packet out, clat_packet_index pos, const uint8_t *packet, size_t len);