summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLorenzo Colitti <lorenzo@google.com>2014-02-13 12:53:35 +0900
committerLorenzo Colitti <lorenzo@google.com>2014-03-09 02:22:22 +0900
commit6c9862b607a198cfd1ee0141e4a19fcaafdff729 (patch)
tree5c362380f3ff1c02afd0bac9d1fc2b6ea16d205f
parent489e108988036facb25c59d59eb5250cf076fd3a (diff)
downloadandroid_external_android-clat-6c9862b607a198cfd1ee0141e4a19fcaafdff729.tar.gz
android_external_android-clat-6c9862b607a198cfd1ee0141e4a19fcaafdff729.tar.bz2
android_external_android-clat-6c9862b607a198cfd1ee0141e4a19fcaafdff729.zip
DO NOT MERGE: Move translation entry point into translate.c.
The entry point to the translation code is currently called packet_handler and lives in clatd.c. Move it into translate.c and rename it to translate_packet, since that's what it does. Also get rid of some redundant includes. Bug: 11542311 Change-Id: I8529fb87f3a86ee6724fad54787c33a5e86c56ab
-rw-r--r--clatd.c66
-rw-r--r--clatd.h6
-rw-r--r--ipv4.c9
-rw-r--r--ipv6.c8
-rw-r--r--translate.c61
-rw-r--r--translate.h14
6 files changed, 73 insertions, 91 deletions
diff --git a/clatd.c b/clatd.c
index a0ee07d..3ab2371 100644
--- a/clatd.c
+++ b/clatd.c
@@ -29,15 +29,6 @@
#include <arpa/inet.h>
#include <fcntl.h>
-#include <netinet/in.h>
-#include <netinet/ip.h>
-#include <netinet/ip_icmp.h>
-#include <netinet/udp.h>
-#include <netinet/tcp.h>
-#include <netinet/ip6.h>
-#include <netinet/icmp6.h>
-#include <linux/icmp.h>
-
#include <sys/capability.h>
#include <sys/uio.h>
#include <linux/prctl.h>
@@ -63,11 +54,6 @@
int forwarding_fd = -1;
volatile sig_atomic_t running = 1;
-struct tun_data {
- char device6[IFNAMSIZ], device4[IFNAMSIZ];
- int fd6, fd4;
-};
-
/* function: set_forwarding
* enables/disables ipv6 forwarding
*/
@@ -305,56 +291,6 @@ void configure_interface(const char *uplink_interface, const char *plat_prefix,
configure_tun_ip(tunnel);
}
-/* function: packet_handler
- * takes a tun header and a packet and sends it down the stack
- * tunnel - tun device data
- * tun_header - tun header
- * packet - packet
- * packetsize - size of packet
- */
-void packet_handler(const struct tun_data *tunnel, struct tun_pi *tun_header, const char *packet,
- size_t packetsize) {
- int fd;
- int iov_len = 0;
-
- // Allocate buffers for all packet headers.
- struct tun_pi tun_targ;
- char iphdr[sizeof(struct ip6_hdr)];
- char transporthdr[MAX_TCP_HDR];
- char icmp_iphdr[sizeof(struct ip6_hdr)];
- char icmp_transporthdr[MAX_TCP_HDR];
-
- // iovec of the packets we'll send. This gets passed down to the translation functions.
- clat_packet out = {
- { &tun_targ, sizeof(tun_targ) }, // Tunnel header.
- { iphdr, 0 }, // IP header.
- { transporthdr, 0 }, // Transport layer header.
- { icmp_iphdr, 0 }, // ICMP error inner IP header.
- { icmp_transporthdr, 0 }, // ICMP error transport layer header.
- { NULL, 0 }, // Payload. No buffer, it's a pointer to the original payload.
- };
-
- if(tun_header->flags != 0) {
- logmsg(ANDROID_LOG_WARN,"packet_handler: 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);
- 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,"packet_handler: unknown packet type = %x",tun_header->proto);
- }
-
- if (iov_len > 0) {
- writev(fd, out, iov_len);
- }
-}
-
/* function: read_packet
* reads a packet from the tunnel fd and passes it down the stack
* active_fd - tun file descriptor marked ready for reading
@@ -383,7 +319,7 @@ void read_packet(int active_fd, const struct tun_data *tunnel) {
return;
}
- packet_handler(tunnel, (struct tun_pi *) packet, packet + header_size, readlen - header_size);
+ translate_packet(tunnel, (struct tun_pi *) packet, packet + header_size, readlen - header_size);
}
}
diff --git a/clatd.h b/clatd.h
index 3459b09..44a655e 100644
--- a/clatd.h
+++ b/clatd.h
@@ -18,6 +18,7 @@
#ifndef __CLATD_H__
#define __CLATD_H__
+#include <linux/if.h>
#include <linux/if_tun.h>
#define MAXMTU 1500
@@ -30,4 +31,9 @@
// how frequently (in seconds) to poll for an address change while there is no traffic
#define NO_TRAFFIC_INTERFACE_POLL_FREQUENCY 90
+struct tun_data {
+ char device6[IFNAMSIZ], device4[IFNAMSIZ];
+ int fd6, fd4;
+};
+
#endif /* __CLATD_H__ */
diff --git a/ipv4.c b/ipv4.c
index 1d5b0b2..c828ffa 100644
--- a/ipv4.c
+++ b/ipv4.c
@@ -17,15 +17,6 @@
*/
#include <string.h>
-#include <netinet/in.h>
-#include <netinet/ip.h>
-#include <netinet/ip_icmp.h>
-#include <netinet/udp.h>
-#include <netinet/tcp.h>
-#include <netinet/ip6.h>
-#include <netinet/icmp6.h>
-#include <linux/icmp.h>
-
#include "translate.h"
#include "checksum.h"
#include "logging.h"
diff --git a/ipv6.c b/ipv6.c
index e4a73fe..371d9e1 100644
--- a/ipv6.c
+++ b/ipv6.c
@@ -17,14 +17,6 @@
*/
#include <string.h>
-#include <netinet/in.h>
-#include <netinet/ip.h>
-#include <netinet/ip_icmp.h>
-#include <netinet/udp.h>
-#include <netinet/tcp.h>
-#include <netinet/ip6.h>
-#include <netinet/icmp6.h>
-#include <linux/icmp.h>
#include <arpa/inet.h>
#include "translate.h"
diff --git a/translate.c b/translate.c
index 9a0f1b5..f7f09cb 100644
--- a/translate.c
+++ b/translate.c
@@ -16,15 +16,7 @@
* translate.c - CLAT functions / partial implementation of rfc6145
*/
#include <string.h>
-
-#include <netinet/in.h>
-#include <netinet/ip.h>
-#include <netinet/ip_icmp.h>
-#include <netinet/udp.h>
-#include <netinet/tcp.h>
-#include <netinet/ip6.h>
-#include <netinet/icmp6.h>
-#include <linux/icmp.h>
+#include <sys/uio.h>
#include "icmp.h"
#include "translate.h"
@@ -175,6 +167,7 @@ void fill_ip6_header(struct ip6_hdr *ip6, uint16_t payload_len, uint8_t protocol
ip6->ip6_dst = ipv4_addr_to_ipv6_addr(old_header->daddr);
}
+
/* function: icmp_to_icmp6
* translate ipv4 icmp to ipv6 icmp
* out - output packet
@@ -426,3 +419,53 @@ int tcp_translate(clat_packet out, int pos, const struct tcphdr *tcp, size_t hea
return CLAT_POS_PAYLOAD + 1;
}
+
+/* function: translate_packet
+ * takes a tun header and a packet and sends it down the stack
+ * tunnel - tun device data
+ * tun_header - tun header
+ * packet - packet
+ * packetsize - size of packet
+ */
+void translate_packet(const struct tun_data *tunnel, struct tun_pi *tun_header, const char *packet,
+ size_t packetsize) {
+ int fd;
+ int iov_len = 0;
+
+ // Allocate buffers for all packet headers.
+ struct tun_pi tun_targ;
+ char iphdr[sizeof(struct ip6_hdr)];
+ char transporthdr[MAX_TCP_HDR];
+ char icmp_iphdr[sizeof(struct ip6_hdr)];
+ char icmp_transporthdr[MAX_TCP_HDR];
+
+ // iovec of the packets we'll send. This gets passed down to the translation functions.
+ clat_packet out = {
+ { &tun_targ, sizeof(tun_targ) }, // Tunnel header.
+ { iphdr, 0 }, // IP header.
+ { transporthdr, 0 }, // Transport layer header.
+ { icmp_iphdr, 0 }, // ICMP error inner IP header.
+ { icmp_transporthdr, 0 }, // ICMP error transport layer 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);
+ 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);
+ }
+
+ if (iov_len > 0) {
+ writev(fd, out, iov_len);
+ }
+}
diff --git a/translate.h b/translate.h
index cfb7bbb..3378254 100644
--- a/translate.h
+++ b/translate.h
@@ -18,8 +18,18 @@
#ifndef __TRANSLATE_H__
#define __TRANSLATE_H__
+#include <netinet/in.h>
+#include <netinet/ip.h>
+#include <netinet/ip_icmp.h>
+#include <netinet/udp.h>
+#include <netinet/tcp.h>
+#include <netinet/ip6.h>
+#include <netinet/icmp6.h>
+#include <linux/icmp.h>
#include <linux/if_tun.h>
+#include "clatd.h"
+
#define MAX_TCP_HDR (15 * 4) // Data offset field is 4 bits and counts in 32-bit words.
// A clat_packet is an array of iovec structures representing a packet that we are translating.
@@ -47,6 +57,10 @@ void fill_ip_header(struct iphdr *ip_targ, uint16_t payload_len, uint8_t protoco
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 char *packet,
+ size_t packetsize);
+
// Translate IPv4 and IPv6 packets.
int ipv4_packet(clat_packet out, int pos, const char *packet, size_t len);
int ipv6_packet(clat_packet out, int pos, const char *packet, size_t len);