blob: 927b49c265b764856777c5c48f74509dd5537bb1 [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
211 // an IPv6 header, which is 20 bytes longer. Fix it up here. This is simpler than the
212 // alternative, which is to always update the pseudo-header checksum in all UDP/TCP/ICMP
213 // translation functions (rather than pre-calculating it when translating the IPv4 header).
214 // We only need to do this for ICMP->ICMPv6, not ICMPv6->ICMP, because ICMP does not use the
215 // pseudo-header when calculating its checksum (as the IPv4 header has its own checksum).
216 checksum = htonl(ntohl(checksum) + 20);
217 } else if (icmp6_type == ICMP6_ECHO_REQUEST || icmp6_type == ICMP6_ECHO_REPLY) {
218 // Ping packet.
219 icmp6_targ->icmp6_id = icmp->un.echo.id;
220 icmp6_targ->icmp6_seq = icmp->un.echo.sequence;
221 out[CLAT_POS_PAYLOAD].iov_base = (char *) payload;
222 out[CLAT_POS_PAYLOAD].iov_len = payload_size;
223 clat_packet_len = CLAT_POS_PAYLOAD + 1;
224 } else {
225 // Unknown type/code. The type/code conversion functions have already logged an error.
226 return 0;
227 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500228
Lorenzo Colittiee80ca62013-04-10 16:52:22 +0900229 icmp6_targ->icmp6_cksum = 0; // Checksum field must be 0 when calculating checksum.
230 icmp6_targ->icmp6_cksum = packet_checksum(checksum, out, pos);
231
Lorenzo Colitticd70b352013-04-10 12:24:56 +0900232 return clat_packet_len;
Daniel Drowna45056e2012-03-23 10:42:54 -0500233}
234
235/* function: icmp6_to_icmp
Lorenzo Colitticd70b352013-04-10 12:24:56 +0900236 * translate ipv6 icmp to ipv4 icmp
Lorenzo Colittid9084182013-03-22 00:42:21 +0900237 * out - output packet
Daniel Drowna45056e2012-03-23 10:42:54 -0500238 * icmp6 - source packet icmp6 header
239 * payload - icmp6 payload
240 * payload_size - size of payload
Lorenzo Colittid9084182013-03-22 00:42:21 +0900241 * returns: the highest position in the output clat_packet that's filled in
Daniel Drowna45056e2012-03-23 10:42:54 -0500242 */
Lorenzo Colitti9477a462013-11-18 15:56:02 +0900243int icmp6_to_icmp(clat_packet out, int pos, const struct icmp6_hdr *icmp6,
Lorenzo Colittid9084182013-03-22 00:42:21 +0900244 const char *payload, size_t payload_size) {
245 struct icmphdr *icmp_targ = out[pos].iov_base;
Lorenzo Colitticd70b352013-04-10 12:24:56 +0900246 uint8_t icmp_type;
247 int ttl;
248 int clat_packet_len;
Daniel Drowna45056e2012-03-23 10:42:54 -0500249
Lorenzo Colittid9084182013-03-22 00:42:21 +0900250 memset(icmp_targ, 0, sizeof(struct icmphdr));
Daniel Drowna45056e2012-03-23 10:42:54 -0500251
Lorenzo Colitticd70b352013-04-10 12:24:56 +0900252 icmp_type = icmp6_to_icmp_type(icmp6->icmp6_type, icmp6->icmp6_code);
253 icmp_targ->type = icmp_type;
254 icmp_targ->code = icmp6_to_icmp_code(icmp6->icmp6_type, icmp6->icmp6_code);
Daniel Drowna45056e2012-03-23 10:42:54 -0500255
Lorenzo Colittid9084182013-03-22 00:42:21 +0900256 out[pos].iov_len = sizeof(struct icmphdr);
Lorenzo Colitticd70b352013-04-10 12:24:56 +0900257
258 if (pos == CLAT_POS_TRANSPORTHDR &&
259 is_icmp6_error(icmp6->icmp6_type) &&
260 icmp_type != ICMP_PARAMETERPROB) {
261 // An ICMPv6 error we understand, one level deep.
262 // Translate the nested packet (the one that caused the error).
263 clat_packet_len = ipv6_packet(out, pos + 1, payload, payload_size);
264 } else if (icmp_type == ICMP_ECHO || icmp_type == ICMP_ECHOREPLY) {
265 // Ping packet.
266 icmp_targ->un.echo.id = icmp6->icmp6_id;
267 icmp_targ->un.echo.sequence = icmp6->icmp6_seq;
268 out[CLAT_POS_PAYLOAD].iov_base = (char *) payload;
269 out[CLAT_POS_PAYLOAD].iov_len = payload_size;
270 clat_packet_len = CLAT_POS_PAYLOAD + 1;
271 } else {
272 // Unknown type/code. The type/code conversion functions have already logged an error.
273 return 0;
274 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500275
Lorenzo Colittiee80ca62013-04-10 16:52:22 +0900276 icmp_targ->checksum = 0; // Checksum field must be 0 when calculating checksum.
277 icmp_targ->checksum = packet_checksum(0, out, pos);
278
Lorenzo Colitticd70b352013-04-10 12:24:56 +0900279 return clat_packet_len;
Lorenzo Colittid9084182013-03-22 00:42:21 +0900280}
Daniel Drowna45056e2012-03-23 10:42:54 -0500281
Lorenzo Colittic9f4c892013-11-18 12:59:44 +0900282/* function: generic_packet
283 * takes a generic IP packet and sets it up for translation
284 * out - output packet
285 * pos - position in the output packet of the transport header
286 * payload - pointer to IP payload
287 * len - size of ip payload
288 * returns: the highest position in the output clat_packet that's filled in
289 */
290int generic_packet(clat_packet out, int pos, const char *payload, size_t len) {
291 out[pos].iov_len = 0;
292 out[CLAT_POS_PAYLOAD].iov_base = (char *) payload;
293 out[CLAT_POS_PAYLOAD].iov_len = len;
294
295 return CLAT_POS_PAYLOAD + 1;
296}
297
Lorenzo Colittid9084182013-03-22 00:42:21 +0900298/* function: udp_packet
299 * takes a udp packet and sets it up for translation
300 * out - output packet
301 * udp - pointer to udp header in packet
302 * checksum - pseudo-header checksum
303 * len - size of ip payload
304 */
305int udp_packet(clat_packet out, int pos, const struct udphdr *udp, uint32_t checksum, size_t len) {
306 const char *payload;
307 size_t payload_size;
308
309 if(len < sizeof(struct udphdr)) {
310 logmsg_dbg(ANDROID_LOG_ERROR,"udp_packet/(too small)");
311 return 0;
312 }
313
314 payload = (const char *) (udp + 1);
315 payload_size = len - sizeof(struct udphdr);
316
317 return udp_translate(out, pos, udp, checksum, payload, payload_size);
318}
319
320/* function: tcp_packet
321 * takes a tcp packet and sets it up for translation
322 * out - output packet
323 * tcp - pointer to tcp header in packet
324 * checksum - pseudo-header checksum
325 * len - size of ip payload
326 * returns: the highest position in the output clat_packet that's filled in
327 */
328int tcp_packet(clat_packet out, int pos, const struct tcphdr *tcp, uint32_t checksum, size_t len) {
329 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
351 return tcp_translate(out, pos, tcp, header_size, checksum, 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
358 * checksum - pseudo-header checksum
359 * payload - tcp payload
Daniel Drowna45056e2012-03-23 10:42:54 -0500360 * payload_size - size of payload
Lorenzo Colittid9084182013-03-22 00:42:21 +0900361 * returns: the highest position in the output clat_packet that's filled in
Daniel Drowna45056e2012-03-23 10:42:54 -0500362 */
Lorenzo Colittid9084182013-03-22 00:42:21 +0900363int udp_translate(clat_packet out, int pos, const struct udphdr *udp, uint32_t checksum,
364 const char *payload, size_t payload_size) {
365 struct udphdr *udp_targ = out[pos].iov_base;
Daniel Drowna45056e2012-03-23 10:42:54 -0500366
Lorenzo Colittid9084182013-03-22 00:42:21 +0900367 memcpy(udp_targ, udp, sizeof(struct udphdr));
Daniel Drowna45056e2012-03-23 10:42:54 -0500368
Lorenzo Colittid9084182013-03-22 00:42:21 +0900369 out[pos].iov_len = sizeof(struct udphdr);
Lorenzo Colittiee80ca62013-04-10 16:52:22 +0900370 out[CLAT_POS_PAYLOAD].iov_base = (char *) payload;
371 out[CLAT_POS_PAYLOAD].iov_len = payload_size;
Daniel Drowna45056e2012-03-23 10:42:54 -0500372
Lorenzo Colittiee80ca62013-04-10 16:52:22 +0900373 udp_targ->check = 0; // Checksum field must be 0 when calculating checksum.
374 udp_targ->check = packet_checksum(checksum, out, pos);
375
376 return CLAT_POS_PAYLOAD + 1;
Daniel Drowna45056e2012-03-23 10:42:54 -0500377}
378
379/* function: tcp_translate
380 * common between ipv4/ipv6 - setup checksum and send tcp packet
Lorenzo Colittid9084182013-03-22 00:42:21 +0900381 * out - output packet
382 * tcp - tcp header
383 * header_size - size of tcp header including options
384 * checksum - partial checksum covering ipv4/ipv6 header
Daniel Drowna45056e2012-03-23 10:42:54 -0500385 * payload - tcp payload
386 * payload_size - size of payload
Lorenzo Colittid9084182013-03-22 00:42:21 +0900387 * returns: the highest position in the output clat_packet that's filled in
Daniel Drowna45056e2012-03-23 10:42:54 -0500388 *
389 * TODO: mss rewrite
390 * TODO: hosts without pmtu discovery - non DF packets will rely on fragmentation (unimplemented)
391 */
Lorenzo Colittid9084182013-03-22 00:42:21 +0900392int tcp_translate(clat_packet out, int pos, const struct tcphdr *tcp, size_t header_size,
393 uint32_t checksum, const char *payload, size_t payload_size) {
394 struct tcphdr *tcp_targ = out[pos].iov_base;
395 out[pos].iov_len = header_size;
Daniel Drowna45056e2012-03-23 10:42:54 -0500396
Lorenzo Colittid9084182013-03-22 00:42:21 +0900397 if (header_size > MAX_TCP_HDR) {
398 // A TCP header cannot be more than MAX_TCP_HDR bytes long because it's a 4-bit field that
399 // 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 +0900400 logmsg(ANDROID_LOG_ERROR, "tcp_translate: header too long %d > %d, truncating",
Lorenzo Colittid9084182013-03-22 00:42:21 +0900401 header_size, MAX_TCP_HDR);
402 header_size = MAX_TCP_HDR;
Daniel Drowna45056e2012-03-23 10:42:54 -0500403 }
Lorenzo Colitti5cc877d2013-04-08 19:12:43 +0900404
Lorenzo Colittid9084182013-03-22 00:42:21 +0900405 memcpy(tcp_targ, tcp, header_size);
Lorenzo Colitti5cc877d2013-04-08 19:12:43 +0900406
Lorenzo Colittiee80ca62013-04-10 16:52:22 +0900407 out[CLAT_POS_PAYLOAD].iov_base = (char *)payload;
408 out[CLAT_POS_PAYLOAD].iov_len = payload_size;
Daniel Drowna45056e2012-03-23 10:42:54 -0500409
Lorenzo Colittiee80ca62013-04-10 16:52:22 +0900410 tcp_targ->check = 0; // Checksum field must be 0 when calculating checksum.
411 tcp_targ->check = packet_checksum(checksum, out, pos);
Daniel Drowna45056e2012-03-23 10:42:54 -0500412
Lorenzo Colittiee80ca62013-04-10 16:52:22 +0900413 return CLAT_POS_PAYLOAD + 1;
Daniel Drowna45056e2012-03-23 10:42:54 -0500414}