blob: e4a73fe7cb5f799e4cebcbde1f5dab5d722d0b77 [file] [log] [blame]
Daniel Drowna45056e2012-03-23 10:42:54 -05001/*
2 * Copyright 2011 Daniel Drown
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * ipv6.c - takes ipv6 packets, finds their headers, and then calls translation functions on them
17 */
18#include <string.h>
19
20#include <netinet/in.h>
21#include <netinet/ip.h>
22#include <netinet/ip_icmp.h>
23#include <netinet/udp.h>
24#include <netinet/tcp.h>
25#include <netinet/ip6.h>
26#include <netinet/icmp6.h>
27#include <linux/icmp.h>
Lorenzo Colittid9084182013-03-22 00:42:21 +090028#include <arpa/inet.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050029
30#include "translate.h"
31#include "checksum.h"
Daniel Drowna45056e2012-03-23 10:42:54 -050032#include "logging.h"
33#include "dump.h"
34#include "config.h"
35#include "debug.h"
36
37/* function: icmp6_packet
38 * takes an icmp6 packet and sets it up for translation
Lorenzo Colittid9084182013-03-22 00:42:21 +090039 * out - output packet
40 * icmp6 - pointer to icmp6 header in packet
41 * checksum - pseudo-header checksum (unused)
42 * len - size of ip payload
43 * returns: the highest position in the output clat_packet that's filled in
Daniel Drowna45056e2012-03-23 10:42:54 -050044 */
Lorenzo Colitti9477a462013-11-18 15:56:02 +090045int icmp6_packet(clat_packet out, int pos, const struct icmp6_hdr *icmp6, size_t len) {
Daniel Drowna45056e2012-03-23 10:42:54 -050046 const char *payload;
47 size_t payload_size;
48
Lorenzo Colittid9084182013-03-22 00:42:21 +090049 if(len < sizeof(struct icmp6_hdr)) {
50 logmsg_dbg(ANDROID_LOG_ERROR, "icmp6_packet/(too small)");
51 return 0;
Daniel Drowna45056e2012-03-23 10:42:54 -050052 }
53
Lorenzo Colittid9084182013-03-22 00:42:21 +090054 payload = (const char *) (icmp6 + 1);
55 payload_size = len - sizeof(struct icmp6_hdr);
Daniel Drowna45056e2012-03-23 10:42:54 -050056
Lorenzo Colitti9477a462013-11-18 15:56:02 +090057 return icmp6_to_icmp(out, pos, icmp6, payload, payload_size);
Daniel Drowna45056e2012-03-23 10:42:54 -050058}
59
60/* function: log_bad_address
61 * logs a bad address to android's log buffer if debugging is turned on
62 * fmt - printf-style format, use %s to place the address
63 * badaddr - the bad address in question
64 */
Daniel Drowna45056e2012-03-23 10:42:54 -050065#if CLAT_DEBUG
Lorenzo Colitti9477a462013-11-18 15:56:02 +090066void log_bad_address(const char *fmt, const struct in6_addr *src, const struct in6_addr *dst) {
Lorenzo Colitticd70b352013-04-10 12:24:56 +090067 char srcstr[INET6_ADDRSTRLEN];
68 char dststr[INET6_ADDRSTRLEN];
Daniel Drowna45056e2012-03-23 10:42:54 -050069
Lorenzo Colitticd70b352013-04-10 12:24:56 +090070 inet_ntop(AF_INET6, src, srcstr, sizeof(srcstr));
71 inet_ntop(AF_INET6, dst, dststr, sizeof(dststr));
72 logmsg_dbg(ANDROID_LOG_ERROR, fmt, srcstr, dststr);
Daniel Drowna45056e2012-03-23 10:42:54 -050073}
Lorenzo Colitti9477a462013-11-18 15:56:02 +090074#else
75#define log_bad_address(fmt, src, dst)
76#endif
Daniel Drowna45056e2012-03-23 10:42:54 -050077
78/* function: ipv6_packet
79 * takes an ipv6 packet and hands it off to the layer 4 protocol function
Lorenzo Colittid9084182013-03-22 00:42:21 +090080 * out - output packet
Daniel Drowna45056e2012-03-23 10:42:54 -050081 * packet - packet data
82 * len - size of packet
Lorenzo Colittid9084182013-03-22 00:42:21 +090083 * returns: the highest position in the output clat_packet that's filled in
Daniel Drowna45056e2012-03-23 10:42:54 -050084 */
Lorenzo Colittid9084182013-03-22 00:42:21 +090085int ipv6_packet(clat_packet out, int pos, const char *packet, size_t len) {
86 const struct ip6_hdr *ip6 = (struct ip6_hdr *) packet;
87 struct iphdr *ip_targ = (struct iphdr *) out[pos].iov_base;
88 uint8_t protocol;
Daniel Drowna45056e2012-03-23 10:42:54 -050089 const char *next_header;
90 size_t len_left;
Lorenzo Colitti5a50c022014-02-10 09:20:05 +090091 uint32_t old_sum, new_sum;
Lorenzo Colittid9084182013-03-22 00:42:21 +090092 int iov_len;
Daniel Drowna45056e2012-03-23 10:42:54 -050093
Lorenzo Colittid9084182013-03-22 00:42:21 +090094 if(len < sizeof(struct ip6_hdr)) {
Lorenzo Colitticd70b352013-04-10 12:24:56 +090095 logmsg_dbg(ANDROID_LOG_ERROR, "ipv6_packet/too short for an ip6 header: %d", len);
Lorenzo Colittid9084182013-03-22 00:42:21 +090096 return 0;
Daniel Drowna45056e2012-03-23 10:42:54 -050097 }
98
Lorenzo Colittid9084182013-03-22 00:42:21 +090099 if(IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
Lorenzo Colitticd70b352013-04-10 12:24:56 +0900100 log_bad_address("ipv6_packet/multicast %s->%s", &ip6->ip6_src, &ip6->ip6_dst);
Lorenzo Colittid9084182013-03-22 00:42:21 +0900101 return 0; // silently ignore
Daniel Drowna45056e2012-03-23 10:42:54 -0500102 }
103
Lorenzo Colitticd70b352013-04-10 12:24:56 +0900104 // If the packet is not from the plat subnet to the local subnet, or vice versa, drop it, unless
105 // it's an ICMP packet (which can come from anywhere). We do not send IPv6 packets from the plat
106 // subnet to the local subnet, but these can appear as inner packets in ICMP errors, so we need
107 // to translate them. We accept third-party ICMPv6 errors, even though their source addresses
108 // cannot be translated, so that things like unreachables and traceroute will work. fill_ip_header
109 // takes care of faking a source address for them.
110 if (!(is_in_plat_subnet(&ip6->ip6_src) &&
111 IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &Global_Clatd_Config.ipv6_local_subnet)) &&
112 !(is_in_plat_subnet(&ip6->ip6_dst) &&
113 IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &Global_Clatd_Config.ipv6_local_subnet)) &&
114 ip6->ip6_nxt != IPPROTO_ICMPV6) {
115 log_bad_address("ipv6_packet/wrong source address: %s->%s", &ip6->ip6_src, &ip6->ip6_dst);
Lorenzo Colittid9084182013-03-22 00:42:21 +0900116 return 0;
Daniel Drowna45056e2012-03-23 10:42:54 -0500117 }
118
Lorenzo Colittid9084182013-03-22 00:42:21 +0900119 next_header = packet + sizeof(struct ip6_hdr);
120 len_left = len - sizeof(struct ip6_hdr);
121
122 protocol = ip6->ip6_nxt;
123 if (protocol == IPPROTO_ICMPV6) {
124 // ICMP and ICMPv6 have different protocol numbers.
125 protocol = IPPROTO_ICMP;
126 }
127
128 /* Fill in the IPv4 header. We need to do this before we translate the packet because TCP and
129 * UDP include parts of the IP header in the checksum. Set the length to zero because we don't
130 * know it yet.
131 */
132 fill_ip_header(ip_targ, 0, protocol, ip6);
133 out[pos].iov_len = sizeof(struct iphdr);
134
135 // Calculate the pseudo-header checksum.
Lorenzo Colitti5a50c022014-02-10 09:20:05 +0900136 old_sum = ipv6_pseudo_header_checksum(0, ip6, len_left);
137 new_sum = ipv4_pseudo_header_checksum(0, ip_targ, len_left);
Lorenzo Colittid9084182013-03-22 00:42:21 +0900138
Daniel Drowna45056e2012-03-23 10:42:54 -0500139 // does not support IPv6 extension headers, this will drop any packet with them
Lorenzo Colittic9f4c892013-11-18 12:59:44 +0900140 if (protocol == IPPROTO_ICMP) {
Lorenzo Colitti9477a462013-11-18 15:56:02 +0900141 iov_len = icmp6_packet(out, pos + 1, (const struct icmp6_hdr *) next_header, len_left);
Lorenzo Colittic9f4c892013-11-18 12:59:44 +0900142 } else if (ip6->ip6_nxt == IPPROTO_TCP) {
Lorenzo Colitti5a50c022014-02-10 09:20:05 +0900143 iov_len = tcp_packet(out, pos + 1, (const struct tcphdr *) next_header, old_sum, new_sum,
Lorenzo Colittid9084182013-03-22 00:42:21 +0900144 len_left);
Lorenzo Colittic9f4c892013-11-18 12:59:44 +0900145 } else if (ip6->ip6_nxt == IPPROTO_UDP) {
Lorenzo Colitti5a50c022014-02-10 09:20:05 +0900146 iov_len = udp_packet(out, pos + 1, (const struct udphdr *) next_header, old_sum, new_sum,
Lorenzo Colittid9084182013-03-22 00:42:21 +0900147 len_left);
Lorenzo Colittic9f4c892013-11-18 12:59:44 +0900148 } else if (ip6->ip6_nxt == IPPROTO_GRE) {
149 iov_len = generic_packet(out, pos + 1, next_header, len_left);
Daniel Drowna45056e2012-03-23 10:42:54 -0500150 } else {
151#if CLAT_DEBUG
Lorenzo Colitticd70b352013-04-10 12:24:56 +0900152 logmsg(ANDROID_LOG_ERROR, "ipv6_packet/unknown next header type: %x", ip6->ip6_nxt);
Daniel Drowna45056e2012-03-23 10:42:54 -0500153 logcat_hexdump("ipv6/nxthdr", packet, len);
154#endif
Lorenzo Colittid9084182013-03-22 00:42:21 +0900155 return 0;
Daniel Drowna45056e2012-03-23 10:42:54 -0500156 }
Lorenzo Colittid9084182013-03-22 00:42:21 +0900157
158 // Set the length and calculate the checksum.
Lorenzo Colittiee80ca62013-04-10 16:52:22 +0900159 ip_targ->tot_len = htons(ntohs(ip_targ->tot_len) + packet_length(out, pos));
Lorenzo Colittid9084182013-03-22 00:42:21 +0900160 ip_targ->check = ip_checksum(ip_targ, sizeof(struct iphdr));
161 return iov_len;
Daniel Drowna45056e2012-03-23 10:42:54 -0500162}