summaryrefslogtreecommitdiffstats
path: root/ipv6.c
diff options
context:
space:
mode:
authorLorenzo Colitti <lorenzo@google.com>2014-02-09 10:35:38 +0900
committerLorenzo Colitti <lorenzo@google.com>2014-02-22 12:46:06 +0900
commit57d480d2b425ef20d8b6f84abd4e9e3209fa9422 (patch)
tree1d4d53278970b0681042e4f10ef44c1c5334bc5f /ipv6.c
parent07f0265830fcae2632159e9993b93a161d7ea23b (diff)
downloadplatform_external_android-clat-57d480d2b425ef20d8b6f84abd4e9e3209fa9422.tar.gz
platform_external_android-clat-57d480d2b425ef20d8b6f84abd4e9e3209fa9422.tar.bz2
platform_external_android-clat-57d480d2b425ef20d8b6f84abd4e9e3209fa9422.zip
Support translating fragmented packets.
Bug: 11542311 Change-Id: I14a20b9ac669cdb5927f6ac26147bb0109099497
Diffstat (limited to 'ipv6.c')
-rw-r--r--ipv6.c52
1 files changed, 38 insertions, 14 deletions
diff --git a/ipv6.c b/ipv6.c
index d188e47..2e83be1 100644
--- a/ipv6.c
+++ b/ipv6.c
@@ -77,6 +77,7 @@ void log_bad_address(const char *fmt, const struct in6_addr *src, const struct i
int ipv6_packet(clat_packet out, int pos, const char *packet, size_t len) {
const struct ip6_hdr *ip6 = (struct ip6_hdr *) packet;
struct iphdr *ip_targ = (struct iphdr *) out[pos].iov_base;
+ struct ip6_frag *frag_hdr = NULL;
uint8_t protocol;
const char *next_header;
size_t len_left;
@@ -112,10 +113,6 @@ int ipv6_packet(clat_packet out, int pos, const char *packet, size_t len) {
len_left = len - sizeof(struct ip6_hdr);
protocol = ip6->ip6_nxt;
- if (protocol == IPPROTO_ICMPV6) {
- // ICMP and ICMPv6 have different protocol numbers.
- protocol = IPPROTO_ICMP;
- }
/* Fill in the IPv4 header. We need to do this before we translate the packet because TCP and
* UDP include parts of the IP header in the checksum. Set the length to zero because we don't
@@ -124,21 +121,48 @@ int ipv6_packet(clat_packet out, int pos, const char *packet, size_t len) {
fill_ip_header(ip_targ, 0, protocol, ip6);
out[pos].iov_len = sizeof(struct iphdr);
- // Calculate the pseudo-header checksum.
+ // If there's a Fragment header, parse it and decide what the next header is.
+ // Do this before calculating the pseudo-header checksum because it updates the next header value.
+ if (protocol == IPPROTO_FRAGMENT) {
+ frag_hdr = (struct ip6_frag *) next_header;
+ if (len_left < sizeof(*frag_hdr)) {
+ logmsg_dbg(ANDROID_LOG_ERROR, "ipv6_packet/too short for fragment header: %d", len);
+ return 0;
+ }
+
+ next_header += sizeof(*frag_hdr);
+ len_left -= sizeof(*frag_hdr);
+
+ protocol = parse_frag_header(frag_hdr, ip_targ);
+ }
+
+ // ICMP and ICMPv6 have different protocol numbers.
+ if (protocol == IPPROTO_ICMPV6) {
+ protocol = IPPROTO_ICMP;
+ ip_targ->protocol = IPPROTO_ICMP;
+ }
+
+ /* Calculate the pseudo-header checksum.
+ * Technically, the length that is used in the pseudo-header checksum is the transport layer
+ * length, which is not the same as len_left in the case of fragmented packets. But since
+ * translation does not change the transport layer length, the checksum is unaffected.
+ */
old_sum = ipv6_pseudo_header_checksum(ip6, len_left, protocol);
new_sum = ipv4_pseudo_header_checksum(ip_targ, len_left);
- // does not support IPv6 extension headers, this will drop any packet with them
- if (protocol == IPPROTO_ICMP) {
- iov_len = icmp6_packet(out, pos + 1, (const struct icmp6_hdr *) next_header, len_left);
- } else if (ip6->ip6_nxt == IPPROTO_TCP) {
- iov_len = tcp_packet(out, pos + 1, (const struct tcphdr *) next_header, old_sum, new_sum,
+ // Does not support IPv6 extension headers except Fragment.
+ if (frag_hdr && (frag_hdr->ip6f_offlg & IP6F_OFF_MASK)) {
+ iov_len = generic_packet(out, pos + 2, next_header, len_left);
+ } else if (protocol == IPPROTO_ICMP) {
+ iov_len = icmp6_packet(out, pos + 2, (const struct icmp6_hdr *) next_header, len_left);
+ } else if (protocol == IPPROTO_TCP) {
+ iov_len = tcp_packet(out, pos + 2, (const struct tcphdr *) next_header, old_sum, new_sum,
len_left);
- } else if (ip6->ip6_nxt == IPPROTO_UDP) {
- iov_len = udp_packet(out, pos + 1, (const struct udphdr *) next_header, old_sum, new_sum,
+ } else if (protocol == IPPROTO_UDP) {
+ iov_len = udp_packet(out, pos + 2, (const struct udphdr *) next_header, old_sum, new_sum,
len_left);
- } else if (ip6->ip6_nxt == IPPROTO_GRE) {
- iov_len = generic_packet(out, pos + 1, next_header, len_left);
+ } else if (protocol == IPPROTO_GRE) {
+ iov_len = generic_packet(out, pos + 2, next_header, len_left);
} else {
#if CLAT_DEBUG
logmsg(ANDROID_LOG_ERROR, "ipv6_packet/unknown next header type: %x", ip6->ip6_nxt);