blob: f7f09cbb243b1d42c7b10dbbbf3904634b1c28e9 [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 * translate.c - CLAT functions / partial implementation of rfc6145
17 */
18#include <string.h>
Lorenzo Colittif9390602014-02-13 12:53:35 +090019#include <sys/uio.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050020
Lorenzo Colitticd70b352013-04-10 12:24:56 +090021#include "icmp.h"
Lorenzo Colittid9084182013-03-22 00:42:21 +090022#include "translate.h"
Daniel Drowna45056e2012-03-23 10:42:54 -050023#include "checksum.h"
24#include "clatd.h"
25#include "config.h"
26#include "logging.h"
27#include "debug.h"
28
Lorenzo Colittiee80ca62013-04-10 16:52:22 +090029/* function: packet_checksum
30 * calculates the checksum over all the packet components starting from pos
31 * checksum - checksum of packet components before pos
32 * packet - packet to calculate the checksum of
33 * pos - position to start counting from
34 * returns - the completed 16-bit checksum, ready to write into a checksum header field
35 */
36uint16_t packet_checksum(uint32_t checksum, clat_packet packet, int pos) {
37 int i;
38 for (i = pos; i < CLAT_POS_MAX; i++) {
39 if (packet[i].iov_len > 0) {
40 checksum = ip_checksum_add(checksum, packet[i].iov_base, packet[i].iov_len);
41 }
42 }
43 return ip_checksum_finish(checksum);
44}
45
46/* function: packet_length
47 * returns the total length of all the packet components after pos
Lorenzo Colittid9084182013-03-22 00:42:21 +090048 * packet - packet to calculate the length of
Lorenzo Colitticd70b352013-04-10 12:24:56 +090049 * pos - position to start counting after
Lorenzo Colittid9084182013-03-22 00:42:21 +090050 * returns: the total length of the packet components after pos
51 */
Lorenzo Colittiee80ca62013-04-10 16:52:22 +090052uint16_t packet_length(clat_packet packet, int pos) {
Lorenzo Colittid9084182013-03-22 00:42:21 +090053 size_t len = 0;
54 int i;
Lorenzo Colittiee80ca62013-04-10 16:52:22 +090055 for (i = pos + 1; i < CLAT_POS_MAX; i++) {
Lorenzo Colittid9084182013-03-22 00:42:21 +090056 len += packet[i].iov_len;
57 }
58 return len;
59}
60
Lorenzo Colittiee80ca62013-04-10 16:52:22 +090061/* function: is_in_plat_subnet
62 * returns true iff the given IPv6 address is in the plat subnet.
63 * addr - IPv6 address
64 */
65int is_in_plat_subnet(const struct in6_addr *addr6) {
66 // Assumes a /96 plat subnet.
67 return (addr6 != NULL) && (memcmp(addr6, &Global_Clatd_Config.plat_subnet, 12) == 0);
68}
69
70/* function: ipv6_addr_to_ipv4_addr
71 * return the corresponding ipv4 address for the given ipv6 address
72 * addr6 - ipv6 address
73 * returns: the IPv4 address
74 */
75uint32_t ipv6_addr_to_ipv4_addr(const struct in6_addr *addr6) {
Lorenzo Colittiee80ca62013-04-10 16:52:22 +090076 if (is_in_plat_subnet(addr6)) {
77 // Assumes a /96 plat subnet.
78 return addr6->s6_addr32[3];
Lorenzo Colitticd70b352013-04-10 12:24:56 +090079 } else if (IN6_ARE_ADDR_EQUAL(addr6, &Global_Clatd_Config.ipv6_local_subnet)) {
80 // Special-case our own address.
Lorenzo Colittiee80ca62013-04-10 16:52:22 +090081 return Global_Clatd_Config.ipv4_local_subnet.s_addr;
Lorenzo Colitticd70b352013-04-10 12:24:56 +090082 } else {
83 // Third party packet. Let the caller deal with it.
84 return INADDR_NONE;
Lorenzo Colittiee80ca62013-04-10 16:52:22 +090085 }
86}
87
88/* function: ipv4_addr_to_ipv6_addr
89 * return the corresponding ipv6 address for the given ipv4 address
90 * addr4 - ipv4 address
91 */
92struct in6_addr ipv4_addr_to_ipv6_addr(uint32_t addr4) {
93 struct in6_addr addr6;
94 // Both addresses are in network byte order (addr4 comes from a network packet, and the config
95 // file entry is read using inet_ntop).
96 if (addr4 == Global_Clatd_Config.ipv4_local_subnet.s_addr) {
97 return Global_Clatd_Config.ipv6_local_subnet;
98 } else {
99 // Assumes a /96 plat subnet.
100 addr6 = Global_Clatd_Config.plat_subnet;
101 addr6.s6_addr32[3] = addr4;
102 return addr6;
103 }
104}
105
Daniel Drowna45056e2012-03-23 10:42:54 -0500106/* function: fill_tun_header
107 * fill in the header for the tun fd
108 * tun_header - tunnel header, already allocated
109 * proto - ethernet protocol id: ETH_P_IP(ipv4) or ETH_P_IPV6(ipv6)
110 */
111void fill_tun_header(struct tun_pi *tun_header, uint16_t proto) {
112 tun_header->flags = 0;
113 tun_header->proto = htons(proto);
114}
115
Daniel Drowna45056e2012-03-23 10:42:54 -0500116/* function: fill_ip_header
Lorenzo Colittid9084182013-03-22 00:42:21 +0900117 * generate an ipv4 header from an ipv6 header
118 * ip_targ - (ipv4) target packet header, source: original ipv4 addr, dest: local subnet addr
Daniel Drowna45056e2012-03-23 10:42:54 -0500119 * payload_len - length of other data inside packet
120 * protocol - protocol number (tcp, udp, etc)
Lorenzo Colittid9084182013-03-22 00:42:21 +0900121 * old_header - (ipv6) source packet header, source: nat64 prefix, dest: local subnet prefix
Daniel Drowna45056e2012-03-23 10:42:54 -0500122 */
Lorenzo Colittid9084182013-03-22 00:42:21 +0900123void fill_ip_header(struct iphdr *ip, uint16_t payload_len, uint8_t protocol,
124 const struct ip6_hdr *old_header) {
Lorenzo Colitticd70b352013-04-10 12:24:56 +0900125 int ttl_guess;
Lorenzo Colittid9084182013-03-22 00:42:21 +0900126 memset(ip, 0, sizeof(struct iphdr));
Daniel Drowna45056e2012-03-23 10:42:54 -0500127
Lorenzo Colittid9084182013-03-22 00:42:21 +0900128 ip->ihl = 5;
129 ip->version = 4;
130 ip->tos = 0;
131 ip->tot_len = htons(sizeof(struct iphdr) + payload_len);
132 ip->id = 0;
133 ip->frag_off = htons(IP_DF);
134 ip->ttl = old_header->ip6_hlim;
135 ip->protocol = protocol;
136 ip->check = 0;
Daniel Drowna45056e2012-03-23 10:42:54 -0500137
Lorenzo Colittiee80ca62013-04-10 16:52:22 +0900138 ip->saddr = ipv6_addr_to_ipv4_addr(&old_header->ip6_src);
139 ip->daddr = ipv6_addr_to_ipv4_addr(&old_header->ip6_dst);
Lorenzo Colitticd70b352013-04-10 12:24:56 +0900140
141 // Third-party ICMPv6 message. This may have been originated by an native IPv6 address.
142 // In that case, the source IPv6 address can't be translated and we need to make up an IPv4
143 // source address. For now, use 255.0.0.<ttl>, which at least looks useful in traceroute.
Lorenzo Colitti9477a462013-11-18 15:56:02 +0900144 if ((uint32_t) ip->saddr == INADDR_NONE) {
Lorenzo Colitticd70b352013-04-10 12:24:56 +0900145 ttl_guess = icmp_guess_ttl(old_header->ip6_hlim);
146 ip->saddr = htonl((0xff << 24) + ttl_guess);
147 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500148}
149
150/* function: fill_ip6_header
Lorenzo Colittid9084182013-03-22 00:42:21 +0900151 * generate an ipv6 header from an ipv4 header
152 * ip6 - (ipv6) target packet header, source: local subnet prefix, dest: nat64 prefix
Daniel Drowna45056e2012-03-23 10:42:54 -0500153 * payload_len - length of other data inside packet
154 * protocol - protocol number (tcp, udp, etc)
Lorenzo Colittid9084182013-03-22 00:42:21 +0900155 * old_header - (ipv4) source packet header, source: local subnet addr, dest: internet's ipv4 addr
Daniel Drowna45056e2012-03-23 10:42:54 -0500156 */
Lorenzo Colittid9084182013-03-22 00:42:21 +0900157void fill_ip6_header(struct ip6_hdr *ip6, uint16_t payload_len, uint8_t protocol,
158 const struct iphdr *old_header) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500159 memset(ip6, 0, sizeof(struct ip6_hdr));
160
161 ip6->ip6_vfc = 6 << 4;
162 ip6->ip6_plen = htons(payload_len);
163 ip6->ip6_nxt = protocol;
164 ip6->ip6_hlim = old_header->ttl;
165
Lorenzo Colittiee80ca62013-04-10 16:52:22 +0900166 ip6->ip6_src = ipv4_addr_to_ipv6_addr(old_header->saddr);
167 ip6->ip6_dst = ipv4_addr_to_ipv6_addr(old_header->daddr);
Daniel Drowna45056e2012-03-23 10:42:54 -0500168}
169
Lorenzo Colittif9390602014-02-13 12:53:35 +0900170
Daniel Drowna45056e2012-03-23 10:42:54 -0500171/* function: icmp_to_icmp6
Lorenzo Colitticd70b352013-04-10 12:24:56 +0900172 * translate ipv4 icmp to ipv6 icmp
Lorenzo Colittid9084182013-03-22 00:42:21 +0900173 * out - output packet
Daniel Drowna45056e2012-03-23 10:42:54 -0500174 * icmp - source packet icmp header
Lorenzo Colittid9084182013-03-22 00:42:21 +0900175 * checksum - pseudo-header checksum
Daniel Drowna45056e2012-03-23 10:42:54 -0500176 * payload - icmp payload
177 * payload_size - size of payload
Lorenzo Colittid9084182013-03-22 00:42:21 +0900178 * returns: the highest position in the output clat_packet that's filled in
Daniel Drowna45056e2012-03-23 10:42:54 -0500179 */
Lorenzo Colittid9084182013-03-22 00:42:21 +0900180int icmp_to_icmp6(clat_packet out, int pos, const struct icmphdr *icmp, uint32_t checksum,
181 const char *payload, size_t payload_size) {
182 struct icmp6_hdr *icmp6_targ = out[pos].iov_base;
Lorenzo Colitticd70b352013-04-10 12:24:56 +0900183 uint8_t icmp6_type;
184 int clat_packet_len;
Daniel Drowna45056e2012-03-23 10:42:54 -0500185
Lorenzo Colittid9084182013-03-22 00:42:21 +0900186 memset(icmp6_targ, 0, sizeof(struct icmp6_hdr));
Lorenzo Colitticd70b352013-04-10 12:24:56 +0900187
188 icmp6_type = icmp_to_icmp6_type(icmp->type, icmp->code);
189 icmp6_targ->icmp6_type = icmp6_type;
190 icmp6_targ->icmp6_code = icmp_to_icmp6_code(icmp->type, icmp->code);
Daniel Drowna45056e2012-03-23 10:42:54 -0500191
Lorenzo Colittid9084182013-03-22 00:42:21 +0900192 out[pos].iov_len = sizeof(struct icmp6_hdr);
Lorenzo Colitticd70b352013-04-10 12:24:56 +0900193
194 if (pos == CLAT_POS_TRANSPORTHDR &&
195 is_icmp_error(icmp->type) &&
196 icmp6_type != ICMP6_PARAM_PROB) {
197 // An ICMP error we understand, one level deep.
198 // Translate the nested packet (the one that caused the error).
199 clat_packet_len = ipv4_packet(out, pos + 1, payload, payload_size);
200
201 // The pseudo-header checksum was calculated on the transport length of the original IPv4
202 // packet that we were asked to translate. This transport length is 20 bytes smaller than it
203 // needs to be, because the ICMP error contains an IPv4 header, which we will be translating to
Lorenzo Colitti5a50c022014-02-10 09:20:05 +0900204 // an IPv6 header, which is 20 bytes longer. Fix it up here.
Lorenzo Colitticd70b352013-04-10 12:24:56 +0900205 // We only need to do this for ICMP->ICMPv6, not ICMPv6->ICMP, because ICMP does not use the
206 // pseudo-header when calculating its checksum (as the IPv4 header has its own checksum).
Lorenzo Colitti5a50c022014-02-10 09:20:05 +0900207 checksum = checksum + htons(20);
Lorenzo Colitticd70b352013-04-10 12:24:56 +0900208 } else if (icmp6_type == ICMP6_ECHO_REQUEST || icmp6_type == ICMP6_ECHO_REPLY) {
209 // Ping packet.
210 icmp6_targ->icmp6_id = icmp->un.echo.id;
211 icmp6_targ->icmp6_seq = icmp->un.echo.sequence;
212 out[CLAT_POS_PAYLOAD].iov_base = (char *) payload;
213 out[CLAT_POS_PAYLOAD].iov_len = payload_size;
214 clat_packet_len = CLAT_POS_PAYLOAD + 1;
215 } else {
216 // Unknown type/code. The type/code conversion functions have already logged an error.
217 return 0;
218 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500219
Lorenzo Colittiee80ca62013-04-10 16:52:22 +0900220 icmp6_targ->icmp6_cksum = 0; // Checksum field must be 0 when calculating checksum.
221 icmp6_targ->icmp6_cksum = packet_checksum(checksum, out, pos);
222
Lorenzo Colitticd70b352013-04-10 12:24:56 +0900223 return clat_packet_len;
Daniel Drowna45056e2012-03-23 10:42:54 -0500224}
225
226/* function: icmp6_to_icmp
Lorenzo Colitticd70b352013-04-10 12:24:56 +0900227 * translate ipv6 icmp to ipv4 icmp
Lorenzo Colittid9084182013-03-22 00:42:21 +0900228 * out - output packet
Daniel Drowna45056e2012-03-23 10:42:54 -0500229 * icmp6 - source packet icmp6 header
230 * payload - icmp6 payload
231 * payload_size - size of payload
Lorenzo Colittid9084182013-03-22 00:42:21 +0900232 * returns: the highest position in the output clat_packet that's filled in
Daniel Drowna45056e2012-03-23 10:42:54 -0500233 */
Lorenzo Colitti9477a462013-11-18 15:56:02 +0900234int icmp6_to_icmp(clat_packet out, int pos, const struct icmp6_hdr *icmp6,
Lorenzo Colittid9084182013-03-22 00:42:21 +0900235 const char *payload, size_t payload_size) {
236 struct icmphdr *icmp_targ = out[pos].iov_base;
Lorenzo Colitticd70b352013-04-10 12:24:56 +0900237 uint8_t icmp_type;
Lorenzo Colitticd70b352013-04-10 12:24:56 +0900238 int clat_packet_len;
Daniel Drowna45056e2012-03-23 10:42:54 -0500239
Lorenzo Colittid9084182013-03-22 00:42:21 +0900240 memset(icmp_targ, 0, sizeof(struct icmphdr));
Daniel Drowna45056e2012-03-23 10:42:54 -0500241
Lorenzo Colitticd70b352013-04-10 12:24:56 +0900242 icmp_type = icmp6_to_icmp_type(icmp6->icmp6_type, icmp6->icmp6_code);
243 icmp_targ->type = icmp_type;
244 icmp_targ->code = icmp6_to_icmp_code(icmp6->icmp6_type, icmp6->icmp6_code);
Daniel Drowna45056e2012-03-23 10:42:54 -0500245
Lorenzo Colittid9084182013-03-22 00:42:21 +0900246 out[pos].iov_len = sizeof(struct icmphdr);
Lorenzo Colitticd70b352013-04-10 12:24:56 +0900247
248 if (pos == CLAT_POS_TRANSPORTHDR &&
249 is_icmp6_error(icmp6->icmp6_type) &&
250 icmp_type != ICMP_PARAMETERPROB) {
251 // An ICMPv6 error we understand, one level deep.
252 // Translate the nested packet (the one that caused the error).
253 clat_packet_len = ipv6_packet(out, pos + 1, payload, payload_size);
254 } else if (icmp_type == ICMP_ECHO || icmp_type == ICMP_ECHOREPLY) {
255 // Ping packet.
256 icmp_targ->un.echo.id = icmp6->icmp6_id;
257 icmp_targ->un.echo.sequence = icmp6->icmp6_seq;
258 out[CLAT_POS_PAYLOAD].iov_base = (char *) payload;
259 out[CLAT_POS_PAYLOAD].iov_len = payload_size;
260 clat_packet_len = CLAT_POS_PAYLOAD + 1;
261 } else {
262 // Unknown type/code. The type/code conversion functions have already logged an error.
263 return 0;
264 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500265
Lorenzo Colittiee80ca62013-04-10 16:52:22 +0900266 icmp_targ->checksum = 0; // Checksum field must be 0 when calculating checksum.
267 icmp_targ->checksum = packet_checksum(0, out, pos);
268
Lorenzo Colitticd70b352013-04-10 12:24:56 +0900269 return clat_packet_len;
Lorenzo Colittid9084182013-03-22 00:42:21 +0900270}
Daniel Drowna45056e2012-03-23 10:42:54 -0500271
Lorenzo Colittic9f4c892013-11-18 12:59:44 +0900272/* function: generic_packet
273 * takes a generic IP packet and sets it up for translation
274 * out - output packet
275 * pos - position in the output packet of the transport header
276 * payload - pointer to IP payload
277 * len - size of ip payload
278 * returns: the highest position in the output clat_packet that's filled in
279 */
280int generic_packet(clat_packet out, int pos, const char *payload, size_t len) {
281 out[pos].iov_len = 0;
282 out[CLAT_POS_PAYLOAD].iov_base = (char *) payload;
283 out[CLAT_POS_PAYLOAD].iov_len = len;
284
285 return CLAT_POS_PAYLOAD + 1;
286}
287
Lorenzo Colittid9084182013-03-22 00:42:21 +0900288/* function: udp_packet
289 * takes a udp packet and sets it up for translation
290 * out - output packet
291 * udp - pointer to udp header in packet
Lorenzo Colitti5a50c022014-02-10 09:20:05 +0900292 * old_sum - pseudo-header checksum of old header
293 * new_sum - pseudo-header checksum of new header
Lorenzo Colittid9084182013-03-22 00:42:21 +0900294 * len - size of ip payload
295 */
Lorenzo Colitti5a50c022014-02-10 09:20:05 +0900296int udp_packet(clat_packet out, int pos, const struct udphdr *udp,
297 uint32_t old_sum, uint32_t new_sum, size_t len) {
Lorenzo Colittid9084182013-03-22 00:42:21 +0900298 const char *payload;
299 size_t payload_size;
300
301 if(len < sizeof(struct udphdr)) {
302 logmsg_dbg(ANDROID_LOG_ERROR,"udp_packet/(too small)");
303 return 0;
304 }
305
306 payload = (const char *) (udp + 1);
307 payload_size = len - sizeof(struct udphdr);
308
Lorenzo Colitti5a50c022014-02-10 09:20:05 +0900309 return udp_translate(out, pos, udp, old_sum, new_sum, payload, payload_size);
Lorenzo Colittid9084182013-03-22 00:42:21 +0900310}
311
312/* function: tcp_packet
313 * takes a tcp packet and sets it up for translation
314 * out - output packet
315 * tcp - pointer to tcp header in packet
316 * checksum - pseudo-header checksum
317 * len - size of ip payload
318 * returns: the highest position in the output clat_packet that's filled in
319 */
Lorenzo Colitti5a50c022014-02-10 09:20:05 +0900320int tcp_packet(clat_packet out, int pos, const struct tcphdr *tcp,
321 uint32_t old_sum, uint32_t new_sum, size_t len) {
Lorenzo Colittid9084182013-03-22 00:42:21 +0900322 const char *payload;
323 size_t payload_size, header_size;
324
325 if(len < sizeof(struct tcphdr)) {
326 logmsg_dbg(ANDROID_LOG_ERROR,"tcp_packet/(too small)");
327 return 0;
328 }
329
330 if(tcp->doff < 5) {
331 logmsg_dbg(ANDROID_LOG_ERROR,"tcp_packet/tcp header length set to less than 5: %x", tcp->doff);
332 return 0;
333 }
334
335 if((size_t) tcp->doff*4 > len) {
336 logmsg_dbg(ANDROID_LOG_ERROR,"tcp_packet/tcp header length set too large: %x", tcp->doff);
337 return 0;
338 }
339
340 header_size = tcp->doff * 4;
341 payload = ((const char *) tcp) + header_size;
342 payload_size = len - header_size;
343
Lorenzo Colitti5a50c022014-02-10 09:20:05 +0900344 return tcp_translate(out, pos, tcp, header_size, old_sum, new_sum, payload, payload_size);
Daniel Drowna45056e2012-03-23 10:42:54 -0500345}
346
347/* function: udp_translate
348 * common between ipv4/ipv6 - setup checksum and send udp packet
Lorenzo Colittid9084182013-03-22 00:42:21 +0900349 * out - output packet
350 * udp - udp header
Lorenzo Colitti5a50c022014-02-10 09:20:05 +0900351 * old_sum - pseudo-header checksum of old header
352 * new_sum - pseudo-header checksum of new header
Lorenzo Colittid9084182013-03-22 00:42:21 +0900353 * payload - tcp payload
Daniel Drowna45056e2012-03-23 10:42:54 -0500354 * payload_size - size of payload
Lorenzo Colittid9084182013-03-22 00:42:21 +0900355 * returns: the highest position in the output clat_packet that's filled in
Daniel Drowna45056e2012-03-23 10:42:54 -0500356 */
Lorenzo Colitti5a50c022014-02-10 09:20:05 +0900357int udp_translate(clat_packet out, int pos, const struct udphdr *udp, uint32_t old_sum,
358 uint32_t new_sum, const char *payload, size_t payload_size) {
Lorenzo Colittid9084182013-03-22 00:42:21 +0900359 struct udphdr *udp_targ = out[pos].iov_base;
Daniel Drowna45056e2012-03-23 10:42:54 -0500360
Lorenzo Colittid9084182013-03-22 00:42:21 +0900361 memcpy(udp_targ, udp, sizeof(struct udphdr));
Daniel Drowna45056e2012-03-23 10:42:54 -0500362
Lorenzo Colittid9084182013-03-22 00:42:21 +0900363 out[pos].iov_len = sizeof(struct udphdr);
Lorenzo Colittiee80ca62013-04-10 16:52:22 +0900364 out[CLAT_POS_PAYLOAD].iov_base = (char *) payload;
365 out[CLAT_POS_PAYLOAD].iov_len = payload_size;
Daniel Drowna45056e2012-03-23 10:42:54 -0500366
Lorenzo Colitti5a50c022014-02-10 09:20:05 +0900367 if (udp_targ->check) {
368 udp_targ->check = ip_checksum_adjust(udp->check, old_sum, new_sum);
369 } else {
370 // Zero checksums are special. RFC 768 says, "An all zero transmitted checksum value means that
371 // the transmitter generated no checksum (for debugging or for higher level protocols that
372 // don't care)." However, in IPv6 zero UDP checksums were only permitted by RFC 6935 (2013). So
373 // for safety we recompute it.
374 udp_targ->check = 0; // Checksum field must be 0 when calculating checksum.
375 udp_targ->check = packet_checksum(new_sum, out, pos);
376 }
377
378 // RFC 768: "If the computed checksum is zero, it is transmitted as all ones (the equivalent
379 // in one's complement arithmetic)."
380 if (!udp_targ->check) {
381 udp_targ->check = 0xffff;
382 }
Lorenzo Colittiee80ca62013-04-10 16:52:22 +0900383
384 return CLAT_POS_PAYLOAD + 1;
Daniel Drowna45056e2012-03-23 10:42:54 -0500385}
386
387/* function: tcp_translate
388 * common between ipv4/ipv6 - setup checksum and send tcp packet
Lorenzo Colittid9084182013-03-22 00:42:21 +0900389 * out - output packet
390 * tcp - tcp header
391 * header_size - size of tcp header including options
392 * checksum - partial checksum covering ipv4/ipv6 header
Daniel Drowna45056e2012-03-23 10:42:54 -0500393 * payload - tcp payload
394 * payload_size - size of payload
Lorenzo Colittid9084182013-03-22 00:42:21 +0900395 * returns: the highest position in the output clat_packet that's filled in
Daniel Drowna45056e2012-03-23 10:42:54 -0500396 *
397 * TODO: mss rewrite
398 * TODO: hosts without pmtu discovery - non DF packets will rely on fragmentation (unimplemented)
399 */
Lorenzo Colittid9084182013-03-22 00:42:21 +0900400int tcp_translate(clat_packet out, int pos, const struct tcphdr *tcp, size_t header_size,
Lorenzo Colitti5a50c022014-02-10 09:20:05 +0900401 uint32_t old_sum, uint32_t new_sum, const char *payload, size_t payload_size) {
Lorenzo Colittid9084182013-03-22 00:42:21 +0900402 struct tcphdr *tcp_targ = out[pos].iov_base;
403 out[pos].iov_len = header_size;
Daniel Drowna45056e2012-03-23 10:42:54 -0500404
Lorenzo Colittid9084182013-03-22 00:42:21 +0900405 if (header_size > MAX_TCP_HDR) {
406 // A TCP header cannot be more than MAX_TCP_HDR bytes long because it's a 4-bit field that
407 // counts in 4-byte words. So this can never happen unless there is a bug in the caller.
Lorenzo Colitti5cc877d2013-04-08 19:12:43 +0900408 logmsg(ANDROID_LOG_ERROR, "tcp_translate: header too long %d > %d, truncating",
Lorenzo Colittid9084182013-03-22 00:42:21 +0900409 header_size, MAX_TCP_HDR);
410 header_size = MAX_TCP_HDR;
Daniel Drowna45056e2012-03-23 10:42:54 -0500411 }
Lorenzo Colitti5cc877d2013-04-08 19:12:43 +0900412
Lorenzo Colittid9084182013-03-22 00:42:21 +0900413 memcpy(tcp_targ, tcp, header_size);
Lorenzo Colitti5cc877d2013-04-08 19:12:43 +0900414
Lorenzo Colittiee80ca62013-04-10 16:52:22 +0900415 out[CLAT_POS_PAYLOAD].iov_base = (char *)payload;
416 out[CLAT_POS_PAYLOAD].iov_len = payload_size;
Daniel Drowna45056e2012-03-23 10:42:54 -0500417
Lorenzo Colitti5a50c022014-02-10 09:20:05 +0900418 tcp_targ->check = ip_checksum_adjust(tcp->check, old_sum, new_sum);
Daniel Drowna45056e2012-03-23 10:42:54 -0500419
Lorenzo Colittiee80ca62013-04-10 16:52:22 +0900420 return CLAT_POS_PAYLOAD + 1;
Daniel Drowna45056e2012-03-23 10:42:54 -0500421}
Lorenzo Colittif9390602014-02-13 12:53:35 +0900422
423/* function: translate_packet
424 * takes a tun header and a packet and sends it down the stack
425 * tunnel - tun device data
426 * tun_header - tun header
427 * packet - packet
428 * packetsize - size of packet
429 */
430void translate_packet(const struct tun_data *tunnel, struct tun_pi *tun_header, const char *packet,
431 size_t packetsize) {
432 int fd;
433 int iov_len = 0;
434
435 // Allocate buffers for all packet headers.
436 struct tun_pi tun_targ;
437 char iphdr[sizeof(struct ip6_hdr)];
438 char transporthdr[MAX_TCP_HDR];
439 char icmp_iphdr[sizeof(struct ip6_hdr)];
440 char icmp_transporthdr[MAX_TCP_HDR];
441
442 // iovec of the packets we'll send. This gets passed down to the translation functions.
443 clat_packet out = {
444 { &tun_targ, sizeof(tun_targ) }, // Tunnel header.
445 { iphdr, 0 }, // IP header.
446 { transporthdr, 0 }, // Transport layer header.
447 { icmp_iphdr, 0 }, // ICMP error inner IP header.
448 { icmp_transporthdr, 0 }, // ICMP error transport layer header.
449 { NULL, 0 }, // Payload. No buffer, it's a pointer to the original payload.
450 };
451
452 if(tun_header->flags != 0) {
453 logmsg(ANDROID_LOG_WARN, "translate_packet: unexpected flags = %d", tun_header->flags);
454 }
455
456 if(ntohs(tun_header->proto) == ETH_P_IP) {
457 fd = tunnel->fd6;
458 fill_tun_header(&tun_targ, ETH_P_IPV6);
459 iov_len = ipv4_packet(out, CLAT_POS_IPHDR, packet, packetsize);
460 } else if(ntohs(tun_header->proto) == ETH_P_IPV6) {
461 fd = tunnel->fd4;
462 fill_tun_header(&tun_targ, ETH_P_IP);
463 iov_len = ipv6_packet(out, CLAT_POS_IPHDR, packet, packetsize);
464 } else {
465 logmsg(ANDROID_LOG_WARN, "translate_packet: unknown packet type = %x",tun_header->proto);
466 }
467
468 if (iov_len > 0) {
469 writev(fd, out, iov_len);
470 }
471}