blob: 9a0f1b5a0d2c6fdc248f5892c9b9c5a5cb46097c [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>
Daniel Drowna45056e2012-03-23 10:42:54 -050019
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>
28
Lorenzo Colitticd70b352013-04-10 12:24:56 +090029#include "icmp.h"
Lorenzo Colittid9084182013-03-22 00:42:21 +090030#include "translate.h"
Daniel Drowna45056e2012-03-23 10:42:54 -050031#include "checksum.h"
32#include "clatd.h"
33#include "config.h"
34#include "logging.h"
35#include "debug.h"
36
Lorenzo Colittiee80ca62013-04-10 16:52:22 +090037/* function: packet_checksum
38 * calculates the checksum over all the packet components starting from pos
39 * checksum - checksum of packet components before pos
40 * packet - packet to calculate the checksum of
41 * pos - position to start counting from
42 * returns - the completed 16-bit checksum, ready to write into a checksum header field
43 */
44uint16_t packet_checksum(uint32_t checksum, clat_packet packet, int pos) {
45 int i;
46 for (i = pos; i < CLAT_POS_MAX; i++) {
47 if (packet[i].iov_len > 0) {
48 checksum = ip_checksum_add(checksum, packet[i].iov_base, packet[i].iov_len);
49 }
50 }
51 return ip_checksum_finish(checksum);
52}
53
54/* function: packet_length
55 * returns the total length of all the packet components after pos
Lorenzo Colittid9084182013-03-22 00:42:21 +090056 * packet - packet to calculate the length of
Lorenzo Colitticd70b352013-04-10 12:24:56 +090057 * pos - position to start counting after
Lorenzo Colittid9084182013-03-22 00:42:21 +090058 * returns: the total length of the packet components after pos
59 */
Lorenzo Colittiee80ca62013-04-10 16:52:22 +090060uint16_t packet_length(clat_packet packet, int pos) {
Lorenzo Colittid9084182013-03-22 00:42:21 +090061 size_t len = 0;
62 int i;
Lorenzo Colittiee80ca62013-04-10 16:52:22 +090063 for (i = pos + 1; i < CLAT_POS_MAX; i++) {
Lorenzo Colittid9084182013-03-22 00:42:21 +090064 len += packet[i].iov_len;
65 }
66 return len;
67}
68
Lorenzo Colittiee80ca62013-04-10 16:52:22 +090069/* function: is_in_plat_subnet
70 * returns true iff the given IPv6 address is in the plat subnet.
71 * addr - IPv6 address
72 */
73int is_in_plat_subnet(const struct in6_addr *addr6) {
74 // Assumes a /96 plat subnet.
75 return (addr6 != NULL) && (memcmp(addr6, &Global_Clatd_Config.plat_subnet, 12) == 0);
76}
77
78/* function: ipv6_addr_to_ipv4_addr
79 * return the corresponding ipv4 address for the given ipv6 address
80 * addr6 - ipv6 address
81 * returns: the IPv4 address
82 */
83uint32_t ipv6_addr_to_ipv4_addr(const struct in6_addr *addr6) {
Lorenzo Colittiee80ca62013-04-10 16:52:22 +090084 if (is_in_plat_subnet(addr6)) {
85 // Assumes a /96 plat subnet.
86 return addr6->s6_addr32[3];
Lorenzo Colitticd70b352013-04-10 12:24:56 +090087 } else if (IN6_ARE_ADDR_EQUAL(addr6, &Global_Clatd_Config.ipv6_local_subnet)) {
88 // Special-case our own address.
Lorenzo Colittiee80ca62013-04-10 16:52:22 +090089 return Global_Clatd_Config.ipv4_local_subnet.s_addr;
Lorenzo Colitticd70b352013-04-10 12:24:56 +090090 } else {
91 // Third party packet. Let the caller deal with it.
92 return INADDR_NONE;
Lorenzo Colittiee80ca62013-04-10 16:52:22 +090093 }
94}
95
96/* function: ipv4_addr_to_ipv6_addr
97 * return the corresponding ipv6 address for the given ipv4 address
98 * addr4 - ipv4 address
99 */
100struct in6_addr ipv4_addr_to_ipv6_addr(uint32_t addr4) {
101 struct in6_addr addr6;
102 // Both addresses are in network byte order (addr4 comes from a network packet, and the config
103 // file entry is read using inet_ntop).
104 if (addr4 == Global_Clatd_Config.ipv4_local_subnet.s_addr) {
105 return Global_Clatd_Config.ipv6_local_subnet;
106 } else {
107 // Assumes a /96 plat subnet.
108 addr6 = Global_Clatd_Config.plat_subnet;
109 addr6.s6_addr32[3] = addr4;
110 return addr6;
111 }
112}
113
Daniel Drowna45056e2012-03-23 10:42:54 -0500114/* function: fill_tun_header
115 * fill in the header for the tun fd
116 * tun_header - tunnel header, already allocated
117 * proto - ethernet protocol id: ETH_P_IP(ipv4) or ETH_P_IPV6(ipv6)
118 */
119void fill_tun_header(struct tun_pi *tun_header, uint16_t proto) {
120 tun_header->flags = 0;
121 tun_header->proto = htons(proto);
122}
123
Daniel Drowna45056e2012-03-23 10:42:54 -0500124/* function: fill_ip_header
Lorenzo Colittid9084182013-03-22 00:42:21 +0900125 * generate an ipv4 header from an ipv6 header
126 * ip_targ - (ipv4) target packet header, source: original ipv4 addr, dest: local subnet addr
Daniel Drowna45056e2012-03-23 10:42:54 -0500127 * payload_len - length of other data inside packet
128 * protocol - protocol number (tcp, udp, etc)
Lorenzo Colittid9084182013-03-22 00:42:21 +0900129 * old_header - (ipv6) source packet header, source: nat64 prefix, dest: local subnet prefix
Daniel Drowna45056e2012-03-23 10:42:54 -0500130 */
Lorenzo Colittid9084182013-03-22 00:42:21 +0900131void fill_ip_header(struct iphdr *ip, uint16_t payload_len, uint8_t protocol,
132 const struct ip6_hdr *old_header) {
Lorenzo Colitticd70b352013-04-10 12:24:56 +0900133 int ttl_guess;
Lorenzo Colittid9084182013-03-22 00:42:21 +0900134 memset(ip, 0, sizeof(struct iphdr));
Daniel Drowna45056e2012-03-23 10:42:54 -0500135
Lorenzo Colittid9084182013-03-22 00:42:21 +0900136 ip->ihl = 5;
137 ip->version = 4;
138 ip->tos = 0;
139 ip->tot_len = htons(sizeof(struct iphdr) + payload_len);
140 ip->id = 0;
141 ip->frag_off = htons(IP_DF);
142 ip->ttl = old_header->ip6_hlim;
143 ip->protocol = protocol;
144 ip->check = 0;
Daniel Drowna45056e2012-03-23 10:42:54 -0500145
Lorenzo Colittiee80ca62013-04-10 16:52:22 +0900146 ip->saddr = ipv6_addr_to_ipv4_addr(&old_header->ip6_src);
147 ip->daddr = ipv6_addr_to_ipv4_addr(&old_header->ip6_dst);
Lorenzo Colitticd70b352013-04-10 12:24:56 +0900148
149 // Third-party ICMPv6 message. This may have been originated by an native IPv6 address.
150 // In that case, the source IPv6 address can't be translated and we need to make up an IPv4
151 // source address. For now, use 255.0.0.<ttl>, which at least looks useful in traceroute.
Lorenzo Colitti9477a462013-11-18 15:56:02 +0900152 if ((uint32_t) ip->saddr == INADDR_NONE) {
Lorenzo Colitticd70b352013-04-10 12:24:56 +0900153 ttl_guess = icmp_guess_ttl(old_header->ip6_hlim);
154 ip->saddr = htonl((0xff << 24) + ttl_guess);
155 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500156}
157
158/* function: fill_ip6_header
Lorenzo Colittid9084182013-03-22 00:42:21 +0900159 * generate an ipv6 header from an ipv4 header
160 * ip6 - (ipv6) target packet header, source: local subnet prefix, dest: nat64 prefix
Daniel Drowna45056e2012-03-23 10:42:54 -0500161 * payload_len - length of other data inside packet
162 * protocol - protocol number (tcp, udp, etc)
Lorenzo Colittid9084182013-03-22 00:42:21 +0900163 * old_header - (ipv4) source packet header, source: local subnet addr, dest: internet's ipv4 addr
Daniel Drowna45056e2012-03-23 10:42:54 -0500164 */
Lorenzo Colittid9084182013-03-22 00:42:21 +0900165void fill_ip6_header(struct ip6_hdr *ip6, uint16_t payload_len, uint8_t protocol,
166 const struct iphdr *old_header) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500167 memset(ip6, 0, sizeof(struct ip6_hdr));
168
169 ip6->ip6_vfc = 6 << 4;
170 ip6->ip6_plen = htons(payload_len);
171 ip6->ip6_nxt = protocol;
172 ip6->ip6_hlim = old_header->ttl;
173
Lorenzo Colittiee80ca62013-04-10 16:52:22 +0900174 ip6->ip6_src = ipv4_addr_to_ipv6_addr(old_header->saddr);
175 ip6->ip6_dst = ipv4_addr_to_ipv6_addr(old_header->daddr);
Daniel Drowna45056e2012-03-23 10:42:54 -0500176}
177
178/* function: icmp_to_icmp6
Lorenzo Colitticd70b352013-04-10 12:24:56 +0900179 * translate ipv4 icmp to ipv6 icmp
Lorenzo Colittid9084182013-03-22 00:42:21 +0900180 * out - output packet
Daniel Drowna45056e2012-03-23 10:42:54 -0500181 * icmp - source packet icmp header
Lorenzo Colittid9084182013-03-22 00:42:21 +0900182 * checksum - pseudo-header checksum
Daniel Drowna45056e2012-03-23 10:42:54 -0500183 * payload - icmp payload
184 * payload_size - size of payload
Lorenzo Colittid9084182013-03-22 00:42:21 +0900185 * returns: the highest position in the output clat_packet that's filled in
Daniel Drowna45056e2012-03-23 10:42:54 -0500186 */
Lorenzo Colittid9084182013-03-22 00:42:21 +0900187int icmp_to_icmp6(clat_packet out, int pos, const struct icmphdr *icmp, uint32_t checksum,
188 const char *payload, size_t payload_size) {
189 struct icmp6_hdr *icmp6_targ = out[pos].iov_base;
Lorenzo Colitticd70b352013-04-10 12:24:56 +0900190 uint8_t icmp6_type;
191 int clat_packet_len;
Daniel Drowna45056e2012-03-23 10:42:54 -0500192
Lorenzo Colittid9084182013-03-22 00:42:21 +0900193 memset(icmp6_targ, 0, sizeof(struct icmp6_hdr));
Lorenzo Colitticd70b352013-04-10 12:24:56 +0900194
195 icmp6_type = icmp_to_icmp6_type(icmp->type, icmp->code);
196 icmp6_targ->icmp6_type = icmp6_type;
197 icmp6_targ->icmp6_code = icmp_to_icmp6_code(icmp->type, icmp->code);
Daniel Drowna45056e2012-03-23 10:42:54 -0500198
Lorenzo Colittid9084182013-03-22 00:42:21 +0900199 out[pos].iov_len = sizeof(struct icmp6_hdr);
Lorenzo Colitticd70b352013-04-10 12:24:56 +0900200
201 if (pos == CLAT_POS_TRANSPORTHDR &&
202 is_icmp_error(icmp->type) &&
203 icmp6_type != ICMP6_PARAM_PROB) {
204 // An ICMP error we understand, one level deep.
205 // Translate the nested packet (the one that caused the error).
206 clat_packet_len = ipv4_packet(out, pos + 1, payload, payload_size);
207
208 // The pseudo-header checksum was calculated on the transport length of the original IPv4
209 // packet that we were asked to translate. This transport length is 20 bytes smaller than it
210 // needs to be, because the ICMP error contains an IPv4 header, which we will be translating to
Lorenzo Colitti5a50c022014-02-10 09:20:05 +0900211 // an IPv6 header, which is 20 bytes longer. Fix it up here.
Lorenzo Colitticd70b352013-04-10 12:24:56 +0900212 // We only need to do this for ICMP->ICMPv6, not ICMPv6->ICMP, because ICMP does not use the
213 // pseudo-header when calculating its checksum (as the IPv4 header has its own checksum).
Lorenzo Colitti5a50c022014-02-10 09:20:05 +0900214 checksum = checksum + htons(20);
Lorenzo Colitticd70b352013-04-10 12:24:56 +0900215 } else if (icmp6_type == ICMP6_ECHO_REQUEST || icmp6_type == ICMP6_ECHO_REPLY) {
216 // Ping packet.
217 icmp6_targ->icmp6_id = icmp->un.echo.id;
218 icmp6_targ->icmp6_seq = icmp->un.echo.sequence;
219 out[CLAT_POS_PAYLOAD].iov_base = (char *) payload;
220 out[CLAT_POS_PAYLOAD].iov_len = payload_size;
221 clat_packet_len = CLAT_POS_PAYLOAD + 1;
222 } else {
223 // Unknown type/code. The type/code conversion functions have already logged an error.
224 return 0;
225 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500226
Lorenzo Colittiee80ca62013-04-10 16:52:22 +0900227 icmp6_targ->icmp6_cksum = 0; // Checksum field must be 0 when calculating checksum.
228 icmp6_targ->icmp6_cksum = packet_checksum(checksum, out, pos);
229
Lorenzo Colitticd70b352013-04-10 12:24:56 +0900230 return clat_packet_len;
Daniel Drowna45056e2012-03-23 10:42:54 -0500231}
232
233/* function: icmp6_to_icmp
Lorenzo Colitticd70b352013-04-10 12:24:56 +0900234 * translate ipv6 icmp to ipv4 icmp
Lorenzo Colittid9084182013-03-22 00:42:21 +0900235 * out - output packet
Daniel Drowna45056e2012-03-23 10:42:54 -0500236 * icmp6 - source packet icmp6 header
237 * payload - icmp6 payload
238 * payload_size - size of payload
Lorenzo Colittid9084182013-03-22 00:42:21 +0900239 * returns: the highest position in the output clat_packet that's filled in
Daniel Drowna45056e2012-03-23 10:42:54 -0500240 */
Lorenzo Colitti9477a462013-11-18 15:56:02 +0900241int icmp6_to_icmp(clat_packet out, int pos, const struct icmp6_hdr *icmp6,
Lorenzo Colittid9084182013-03-22 00:42:21 +0900242 const char *payload, size_t payload_size) {
243 struct icmphdr *icmp_targ = out[pos].iov_base;
Lorenzo Colitticd70b352013-04-10 12:24:56 +0900244 uint8_t icmp_type;
Lorenzo Colitticd70b352013-04-10 12:24:56 +0900245 int clat_packet_len;
Daniel Drowna45056e2012-03-23 10:42:54 -0500246
Lorenzo Colittid9084182013-03-22 00:42:21 +0900247 memset(icmp_targ, 0, sizeof(struct icmphdr));
Daniel Drowna45056e2012-03-23 10:42:54 -0500248
Lorenzo Colitticd70b352013-04-10 12:24:56 +0900249 icmp_type = icmp6_to_icmp_type(icmp6->icmp6_type, icmp6->icmp6_code);
250 icmp_targ->type = icmp_type;
251 icmp_targ->code = icmp6_to_icmp_code(icmp6->icmp6_type, icmp6->icmp6_code);
Daniel Drowna45056e2012-03-23 10:42:54 -0500252
Lorenzo Colittid9084182013-03-22 00:42:21 +0900253 out[pos].iov_len = sizeof(struct icmphdr);
Lorenzo Colitticd70b352013-04-10 12:24:56 +0900254
255 if (pos == CLAT_POS_TRANSPORTHDR &&
256 is_icmp6_error(icmp6->icmp6_type) &&
257 icmp_type != ICMP_PARAMETERPROB) {
258 // An ICMPv6 error we understand, one level deep.
259 // Translate the nested packet (the one that caused the error).
260 clat_packet_len = ipv6_packet(out, pos + 1, payload, payload_size);
261 } else if (icmp_type == ICMP_ECHO || icmp_type == ICMP_ECHOREPLY) {
262 // Ping packet.
263 icmp_targ->un.echo.id = icmp6->icmp6_id;
264 icmp_targ->un.echo.sequence = icmp6->icmp6_seq;
265 out[CLAT_POS_PAYLOAD].iov_base = (char *) payload;
266 out[CLAT_POS_PAYLOAD].iov_len = payload_size;
267 clat_packet_len = CLAT_POS_PAYLOAD + 1;
268 } else {
269 // Unknown type/code. The type/code conversion functions have already logged an error.
270 return 0;
271 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500272
Lorenzo Colittiee80ca62013-04-10 16:52:22 +0900273 icmp_targ->checksum = 0; // Checksum field must be 0 when calculating checksum.
274 icmp_targ->checksum = packet_checksum(0, out, pos);
275
Lorenzo Colitticd70b352013-04-10 12:24:56 +0900276 return clat_packet_len;
Lorenzo Colittid9084182013-03-22 00:42:21 +0900277}
Daniel Drowna45056e2012-03-23 10:42:54 -0500278
Lorenzo Colittic9f4c892013-11-18 12:59:44 +0900279/* function: generic_packet
280 * takes a generic IP packet and sets it up for translation
281 * out - output packet
282 * pos - position in the output packet of the transport header
283 * payload - pointer to IP payload
284 * len - size of ip payload
285 * returns: the highest position in the output clat_packet that's filled in
286 */
287int generic_packet(clat_packet out, int pos, const char *payload, size_t len) {
288 out[pos].iov_len = 0;
289 out[CLAT_POS_PAYLOAD].iov_base = (char *) payload;
290 out[CLAT_POS_PAYLOAD].iov_len = len;
291
292 return CLAT_POS_PAYLOAD + 1;
293}
294
Lorenzo Colittid9084182013-03-22 00:42:21 +0900295/* function: udp_packet
296 * takes a udp packet and sets it up for translation
297 * out - output packet
298 * udp - pointer to udp header in packet
Lorenzo Colitti5a50c022014-02-10 09:20:05 +0900299 * old_sum - pseudo-header checksum of old header
300 * new_sum - pseudo-header checksum of new header
Lorenzo Colittid9084182013-03-22 00:42:21 +0900301 * len - size of ip payload
302 */
Lorenzo Colitti5a50c022014-02-10 09:20:05 +0900303int udp_packet(clat_packet out, int pos, const struct udphdr *udp,
304 uint32_t old_sum, uint32_t new_sum, size_t len) {
Lorenzo Colittid9084182013-03-22 00:42:21 +0900305 const char *payload;
306 size_t payload_size;
307
308 if(len < sizeof(struct udphdr)) {
309 logmsg_dbg(ANDROID_LOG_ERROR,"udp_packet/(too small)");
310 return 0;
311 }
312
313 payload = (const char *) (udp + 1);
314 payload_size = len - sizeof(struct udphdr);
315
Lorenzo Colitti5a50c022014-02-10 09:20:05 +0900316 return udp_translate(out, pos, udp, old_sum, new_sum, payload, payload_size);
Lorenzo Colittid9084182013-03-22 00:42:21 +0900317}
318
319/* function: tcp_packet
320 * takes a tcp packet and sets it up for translation
321 * out - output packet
322 * tcp - pointer to tcp header in packet
323 * checksum - pseudo-header checksum
324 * len - size of ip payload
325 * returns: the highest position in the output clat_packet that's filled in
326 */
Lorenzo Colitti5a50c022014-02-10 09:20:05 +0900327int tcp_packet(clat_packet out, int pos, const struct tcphdr *tcp,
328 uint32_t old_sum, uint32_t new_sum, size_t len) {
Lorenzo Colittid9084182013-03-22 00:42:21 +0900329 const char *payload;
330 size_t payload_size, header_size;
331
332 if(len < sizeof(struct tcphdr)) {
333 logmsg_dbg(ANDROID_LOG_ERROR,"tcp_packet/(too small)");
334 return 0;
335 }
336
337 if(tcp->doff < 5) {
338 logmsg_dbg(ANDROID_LOG_ERROR,"tcp_packet/tcp header length set to less than 5: %x", tcp->doff);
339 return 0;
340 }
341
342 if((size_t) tcp->doff*4 > len) {
343 logmsg_dbg(ANDROID_LOG_ERROR,"tcp_packet/tcp header length set too large: %x", tcp->doff);
344 return 0;
345 }
346
347 header_size = tcp->doff * 4;
348 payload = ((const char *) tcp) + header_size;
349 payload_size = len - header_size;
350
Lorenzo Colitti5a50c022014-02-10 09:20:05 +0900351 return tcp_translate(out, pos, tcp, header_size, old_sum, new_sum, payload, payload_size);
Daniel Drowna45056e2012-03-23 10:42:54 -0500352}
353
354/* function: udp_translate
355 * common between ipv4/ipv6 - setup checksum and send udp packet
Lorenzo Colittid9084182013-03-22 00:42:21 +0900356 * out - output packet
357 * udp - udp header
Lorenzo Colitti5a50c022014-02-10 09:20:05 +0900358 * old_sum - pseudo-header checksum of old header
359 * new_sum - pseudo-header checksum of new header
Lorenzo Colittid9084182013-03-22 00:42:21 +0900360 * payload - tcp payload
Daniel Drowna45056e2012-03-23 10:42:54 -0500361 * payload_size - size of payload
Lorenzo Colittid9084182013-03-22 00:42:21 +0900362 * returns: the highest position in the output clat_packet that's filled in
Daniel Drowna45056e2012-03-23 10:42:54 -0500363 */
Lorenzo Colitti5a50c022014-02-10 09:20:05 +0900364int udp_translate(clat_packet out, int pos, const struct udphdr *udp, uint32_t old_sum,
365 uint32_t new_sum, const char *payload, size_t payload_size) {
Lorenzo Colittid9084182013-03-22 00:42:21 +0900366 struct udphdr *udp_targ = out[pos].iov_base;
Daniel Drowna45056e2012-03-23 10:42:54 -0500367
Lorenzo Colittid9084182013-03-22 00:42:21 +0900368 memcpy(udp_targ, udp, sizeof(struct udphdr));
Daniel Drowna45056e2012-03-23 10:42:54 -0500369
Lorenzo Colittid9084182013-03-22 00:42:21 +0900370 out[pos].iov_len = sizeof(struct udphdr);
Lorenzo Colittiee80ca62013-04-10 16:52:22 +0900371 out[CLAT_POS_PAYLOAD].iov_base = (char *) payload;
372 out[CLAT_POS_PAYLOAD].iov_len = payload_size;
Daniel Drowna45056e2012-03-23 10:42:54 -0500373
Lorenzo Colitti5a50c022014-02-10 09:20:05 +0900374 if (udp_targ->check) {
375 udp_targ->check = ip_checksum_adjust(udp->check, old_sum, new_sum);
376 } else {
377 // Zero checksums are special. RFC 768 says, "An all zero transmitted checksum value means that
378 // the transmitter generated no checksum (for debugging or for higher level protocols that
379 // don't care)." However, in IPv6 zero UDP checksums were only permitted by RFC 6935 (2013). So
380 // for safety we recompute it.
381 udp_targ->check = 0; // Checksum field must be 0 when calculating checksum.
382 udp_targ->check = packet_checksum(new_sum, out, pos);
383 }
384
385 // RFC 768: "If the computed checksum is zero, it is transmitted as all ones (the equivalent
386 // in one's complement arithmetic)."
387 if (!udp_targ->check) {
388 udp_targ->check = 0xffff;
389 }
Lorenzo Colittiee80ca62013-04-10 16:52:22 +0900390
391 return CLAT_POS_PAYLOAD + 1;
Daniel Drowna45056e2012-03-23 10:42:54 -0500392}
393
394/* function: tcp_translate
395 * common between ipv4/ipv6 - setup checksum and send tcp packet
Lorenzo Colittid9084182013-03-22 00:42:21 +0900396 * out - output packet
397 * tcp - tcp header
398 * header_size - size of tcp header including options
399 * checksum - partial checksum covering ipv4/ipv6 header
Daniel Drowna45056e2012-03-23 10:42:54 -0500400 * payload - tcp payload
401 * payload_size - size of payload
Lorenzo Colittid9084182013-03-22 00:42:21 +0900402 * returns: the highest position in the output clat_packet that's filled in
Daniel Drowna45056e2012-03-23 10:42:54 -0500403 *
404 * TODO: mss rewrite
405 * TODO: hosts without pmtu discovery - non DF packets will rely on fragmentation (unimplemented)
406 */
Lorenzo Colittid9084182013-03-22 00:42:21 +0900407int tcp_translate(clat_packet out, int pos, const struct tcphdr *tcp, size_t header_size,
Lorenzo Colitti5a50c022014-02-10 09:20:05 +0900408 uint32_t old_sum, uint32_t new_sum, const char *payload, size_t payload_size) {
Lorenzo Colittid9084182013-03-22 00:42:21 +0900409 struct tcphdr *tcp_targ = out[pos].iov_base;
410 out[pos].iov_len = header_size;
Daniel Drowna45056e2012-03-23 10:42:54 -0500411
Lorenzo Colittid9084182013-03-22 00:42:21 +0900412 if (header_size > MAX_TCP_HDR) {
413 // A TCP header cannot be more than MAX_TCP_HDR bytes long because it's a 4-bit field that
414 // 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 +0900415 logmsg(ANDROID_LOG_ERROR, "tcp_translate: header too long %d > %d, truncating",
Lorenzo Colittid9084182013-03-22 00:42:21 +0900416 header_size, MAX_TCP_HDR);
417 header_size = MAX_TCP_HDR;
Daniel Drowna45056e2012-03-23 10:42:54 -0500418 }
Lorenzo Colitti5cc877d2013-04-08 19:12:43 +0900419
Lorenzo Colittid9084182013-03-22 00:42:21 +0900420 memcpy(tcp_targ, tcp, header_size);
Lorenzo Colitti5cc877d2013-04-08 19:12:43 +0900421
Lorenzo Colittiee80ca62013-04-10 16:52:22 +0900422 out[CLAT_POS_PAYLOAD].iov_base = (char *)payload;
423 out[CLAT_POS_PAYLOAD].iov_len = payload_size;
Daniel Drowna45056e2012-03-23 10:42:54 -0500424
Lorenzo Colitti5a50c022014-02-10 09:20:05 +0900425 tcp_targ->check = ip_checksum_adjust(tcp->check, old_sum, new_sum);
Daniel Drowna45056e2012-03-23 10:42:54 -0500426
Lorenzo Colittiee80ca62013-04-10 16:52:22 +0900427 return CLAT_POS_PAYLOAD + 1;
Daniel Drowna45056e2012-03-23 10:42:54 -0500428}