summaryrefslogtreecommitdiffstats
path: root/translate.h
diff options
context:
space:
mode:
authorLorenzo Colitti <lorenzo@google.com>2013-03-22 00:42:21 +0900
committerLorenzo Colitti <lorenzo@google.com>2013-04-12 12:35:41 +0900
commitd90841824dc00f65a48a789396c7f428807432ca (patch)
treee91f44a1a698088f69523df7869ba08cedb10941 /translate.h
parent0aff5c273daa16e5af234a904ba4a9cf6dc414a6 (diff)
downloadandroid_external_android-clat-d90841824dc00f65a48a789396c7f428807432ca.tar.gz
android_external_android-clat-d90841824dc00f65a48a789396c7f428807432ca.tar.bz2
android_external_android-clat-d90841824dc00f65a48a789396c7f428807432ca.zip
Pass around packet data instead of fds
The current code calls all the translation functions one after another, accumulating the translated packet into local variables on the stack and calling writev() at the end. This does not allow calling the translation functions re-entrantly, which is needed, for example, to translate ICMP errors (which contain the packet that caused them). Define a clat_packet type to wrap the array of iovecs and an enum of packet positions. Also clean up the code a bit: get rid of a fair bit of duplicated code (though there is still some left), get rid of some redundant memcpy statements, fix style issues, etc. Bug: 8276725 Change-Id: Ib58d2348894e82275234fc67dbdb1f82753f204f
Diffstat (limited to 'translate.h')
-rw-r--r--translate.h41
1 files changed, 33 insertions, 8 deletions
diff --git a/translate.h b/translate.h
index 641768e..07db023 100644
--- a/translate.h
+++ b/translate.h
@@ -18,15 +18,40 @@
#ifndef __TRANSLATE_H__
#define __TRANSLATE_H__
-void icmp_to_icmp6(int fd, const struct iphdr *ip, const struct icmphdr *icmp, const char *payload, size_t payload_size);
-void icmp6_to_icmp(int fd, const struct ip6_hdr *ip6, const struct icmp6_hdr *icmp6, const char *payload, size_t payload_size);
+#include <linux/if_tun.h>
-void udp_to_udp6(int fd, const struct iphdr *ip, const struct udphdr *udp, const char *payload, size_t payload_size);
-void udp6_to_udp(int fd, const struct ip6_hdr *ip6, const struct udphdr *udp, const char *payload, size_t payload_size);
+#define MAX_TCP_HDR (15 * 4) // Data offset field is 4 bits and counts in 32-bit words.
-void tcp_to_tcp6(int fd,const struct iphdr *ip, const struct tcphdr *tcp, size_t header_size,
- const char *payload, size_t payload_size);
-void tcp6_to_tcp(int fd,const struct ip6_hdr *ip6, const struct tcphdr *tcp, size_t header_size,
- const char *payload, size_t payload_size);
+// A clat_packet is an array of iovec structures representing a packet that we are translating.
+// The POS_XXX constants represent the array indices within the clat_packet that contain specific
+// parts of the packet.
+enum clat_packet_index { POS_TUNHDR, POS_IPHDR, POS_TRANSPORTHDR, POS_ICMPIPHDR,
+ POS_PAYLOAD, POS_MAX };
+typedef struct iovec clat_packet[POS_MAX];
+
+// Returns the total length of the packet components after index.
+uint16_t payload_length(clat_packet packet, int index);
+
+// Functions to create tun, IPv4, and IPv6 headers.
+void fill_tun_header(struct tun_pi *tun_header, uint16_t proto);
+void fill_ip_header(struct iphdr *ip_targ, uint16_t payload_len, uint8_t protocol,
+ const struct ip6_hdr *old_header);
+void fill_ip6_header(struct ip6_hdr *ip6, uint16_t payload_len, uint8_t protocol,
+ const struct iphdr *old_header);
+
+// Translate ICMP packets.
+int icmp_to_icmp6(clat_packet out, int pos, const struct icmphdr *icmp, uint32_t checksum,
+ const char *payload, size_t payload_size);
+int icmp6_to_icmp(clat_packet out, int pos, const struct icmp6_hdr *icmp6, uint32_t checksum,
+ const char *payload, size_t payload_size);
+
+// Translate TCP and UDP packets.
+int tcp_packet(clat_packet out, int pos, const struct tcphdr *tcp, uint32_t checksum, size_t len);
+int udp_packet(clat_packet out, int pos, const struct udphdr *udp, uint32_t checksum, size_t len);
+
+int tcp_translate(clat_packet out, int pos, const struct tcphdr *tcp, size_t header_size,
+ uint32_t checksum, const char *payload, size_t payload_size);
+int udp_translate(clat_packet out, int pos, const struct udphdr *udp, uint32_t checksum,
+ const char *payload, size_t payload_size);
#endif /* __TRANSLATE_H__ */