• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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>
19 
20 #include "netutils/checksum.h"
21 
22 #include "clatd.h"
23 #include "common.h"
24 #include "config.h"
25 #include "debug.h"
26 #include "icmp.h"
27 #include "logging.h"
28 #include "translate.h"
29 #include "tun.h"
30 
31 /* function: packet_checksum
32  * calculates the checksum over all the packet components starting from pos
33  * checksum - checksum of packet components before pos
34  * packet   - packet to calculate the checksum of
35  * pos      - position to start counting from
36  * returns  - the completed 16-bit checksum, ready to write into a checksum header field
37  */
packet_checksum(uint32_t checksum,clat_packet packet,clat_packet_index pos)38 uint16_t packet_checksum(uint32_t checksum, clat_packet packet, clat_packet_index pos) {
39   int i;
40   for (i = pos; i < CLAT_POS_MAX; i++) {
41     if (packet[i].iov_len > 0) {
42       checksum = ip_checksum_add(checksum, packet[i].iov_base, packet[i].iov_len);
43     }
44   }
45   return ip_checksum_finish(checksum);
46 }
47 
48 /* function: packet_length
49  * returns the total length of all the packet components after pos
50  * packet - packet to calculate the length of
51  * pos    - position to start counting after
52  * returns: the total length of the packet components after pos
53  */
packet_length(clat_packet packet,clat_packet_index pos)54 uint16_t packet_length(clat_packet packet, clat_packet_index pos) {
55   size_t len = 0;
56   int i;
57   for (i = pos + 1; i < CLAT_POS_MAX; i++) {
58     len += packet[i].iov_len;
59   }
60   return len;
61 }
62 
63 /* function: is_in_plat_subnet
64  * returns true iff the given IPv6 address is in the plat subnet.
65  * addr - IPv6 address
66  */
is_in_plat_subnet(const struct in6_addr * addr6)67 int is_in_plat_subnet(const struct in6_addr *addr6) {
68   // Assumes a /96 plat subnet.
69   return (addr6 != NULL) && (memcmp(addr6, &Global_Clatd_Config.plat_subnet, 12) == 0);
70 }
71 
72 /* function: ipv6_addr_to_ipv4_addr
73  * return the corresponding ipv4 address for the given ipv6 address
74  * addr6 - ipv6 address
75  * returns: the IPv4 address
76  */
ipv6_addr_to_ipv4_addr(const struct in6_addr * addr6)77 uint32_t ipv6_addr_to_ipv4_addr(const struct in6_addr *addr6) {
78   if (is_in_plat_subnet(addr6)) {
79     // Assumes a /96 plat subnet.
80     return addr6->s6_addr32[3];
81   } else if (IN6_ARE_ADDR_EQUAL(addr6, &Global_Clatd_Config.ipv6_local_subnet)) {
82     // Special-case our own address.
83     return Global_Clatd_Config.ipv4_local_subnet.s_addr;
84   } else {
85     // Third party packet. Let the caller deal with it.
86     return INADDR_NONE;
87   }
88 }
89 
90 /* function: ipv4_addr_to_ipv6_addr
91  * return the corresponding ipv6 address for the given ipv4 address
92  * addr4 - ipv4 address
93  */
ipv4_addr_to_ipv6_addr(uint32_t addr4)94 struct in6_addr ipv4_addr_to_ipv6_addr(uint32_t addr4) {
95   struct in6_addr addr6;
96   // Both addresses are in network byte order (addr4 comes from a network packet, and the config
97   // file entry is read using inet_ntop).
98   if (addr4 == Global_Clatd_Config.ipv4_local_subnet.s_addr) {
99     return Global_Clatd_Config.ipv6_local_subnet;
100   } else {
101     // Assumes a /96 plat subnet.
102     addr6              = Global_Clatd_Config.plat_subnet;
103     addr6.s6_addr32[3] = addr4;
104     return addr6;
105   }
106 }
107 
108 /* function: fill_tun_header
109  * fill in the header for the tun fd
110  * tun_header - tunnel header, already allocated
111  * proto      - ethernet protocol id: ETH_P_IP(ipv4) or ETH_P_IPV6(ipv6)
112  */
fill_tun_header(struct tun_pi * tun_header,uint16_t proto)113 void fill_tun_header(struct tun_pi *tun_header, uint16_t proto) {
114   tun_header->flags = 0;
115   tun_header->proto = htons(proto);
116 }
117 
118 /* function: fill_ip_header
119  * generate an ipv4 header from an ipv6 header
120  * ip_targ     - (ipv4) target packet header, source: original ipv4 addr, dest: local subnet addr
121  * payload_len - length of other data inside packet
122  * protocol    - protocol number (tcp, udp, etc)
123  * old_header  - (ipv6) source packet header, source: nat64 prefix, dest: local subnet prefix
124  */
fill_ip_header(struct iphdr * ip,uint16_t payload_len,uint8_t protocol,const struct ip6_hdr * old_header)125 void fill_ip_header(struct iphdr *ip, uint16_t payload_len, uint8_t protocol,
126                     const struct ip6_hdr *old_header) {
127   int ttl_guess;
128   memset(ip, 0, sizeof(struct iphdr));
129 
130   ip->ihl      = 5;
131   ip->version  = 4;
132   ip->tos      = 0;
133   ip->tot_len  = htons(sizeof(struct iphdr) + payload_len);
134   ip->id       = 0;
135   ip->frag_off = htons(IP_DF);
136   ip->ttl      = old_header->ip6_hlim;
137   ip->protocol = protocol;
138   ip->check    = 0;
139 
140   ip->saddr = ipv6_addr_to_ipv4_addr(&old_header->ip6_src);
141   ip->daddr = ipv6_addr_to_ipv4_addr(&old_header->ip6_dst);
142 
143   // Third-party ICMPv6 message. This may have been originated by an native IPv6 address.
144   // In that case, the source IPv6 address can't be translated and we need to make up an IPv4
145   // source address. For now, use 255.0.0.<ttl>, which at least looks useful in traceroute.
146   if ((uint32_t)ip->saddr == INADDR_NONE) {
147     ttl_guess = icmp_guess_ttl(old_header->ip6_hlim);
148     ip->saddr = htonl((0xff << 24) + ttl_guess);
149   }
150 }
151 
152 /* function: fill_ip6_header
153  * generate an ipv6 header from an ipv4 header
154  * ip6         - (ipv6) target packet header, source: local subnet prefix, dest: nat64 prefix
155  * payload_len - length of other data inside packet
156  * protocol    - protocol number (tcp, udp, etc)
157  * old_header  - (ipv4) source packet header, source: local subnet addr, dest: internet's ipv4 addr
158  */
fill_ip6_header(struct ip6_hdr * ip6,uint16_t payload_len,uint8_t protocol,const struct iphdr * old_header)159 void fill_ip6_header(struct ip6_hdr *ip6, uint16_t payload_len, uint8_t protocol,
160                      const struct iphdr *old_header) {
161   memset(ip6, 0, sizeof(struct ip6_hdr));
162 
163   ip6->ip6_vfc  = 6 << 4;
164   ip6->ip6_plen = htons(payload_len);
165   ip6->ip6_nxt  = protocol;
166   ip6->ip6_hlim = old_header->ttl;
167 
168   ip6->ip6_src = ipv4_addr_to_ipv6_addr(old_header->saddr);
169   ip6->ip6_dst = ipv4_addr_to_ipv6_addr(old_header->daddr);
170 }
171 
172 /* function: maybe_fill_frag_header
173  * fills a fragmentation header
174  * generate an ipv6 fragment header from an ipv4 header
175  * frag_hdr    - target (ipv6) fragmentation header
176  * ip6_targ    - target (ipv6) header
177  * old_header  - (ipv4) source packet header
178  * returns: the length of the fragmentation header if present, or zero if not present
179  */
maybe_fill_frag_header(struct ip6_frag * frag_hdr,struct ip6_hdr * ip6_targ,const struct iphdr * old_header)180 size_t maybe_fill_frag_header(struct ip6_frag *frag_hdr, struct ip6_hdr *ip6_targ,
181                               const struct iphdr *old_header) {
182   uint16_t frag_flags = ntohs(old_header->frag_off);
183   uint16_t frag_off   = frag_flags & IP_OFFMASK;
184   if (frag_off == 0 && (frag_flags & IP_MF) == 0) {
185     // Not a fragment.
186     return 0;
187   }
188 
189   frag_hdr->ip6f_nxt      = ip6_targ->ip6_nxt;
190   frag_hdr->ip6f_reserved = 0;
191   // In IPv4, the offset is the bottom 13 bits; in IPv6 it's the top 13 bits.
192   frag_hdr->ip6f_offlg = htons(frag_off << 3);
193   if (frag_flags & IP_MF) {
194     frag_hdr->ip6f_offlg |= IP6F_MORE_FRAG;
195   }
196   frag_hdr->ip6f_ident = htonl(ntohs(old_header->id));
197   ip6_targ->ip6_nxt    = IPPROTO_FRAGMENT;
198 
199   return sizeof(*frag_hdr);
200 }
201 
202 /* function: parse_frag_header
203  * return the length of the fragmentation header if present, or zero if not present
204  * generate an ipv6 fragment header from an ipv4 header
205  * frag_hdr    - (ipv6) fragmentation header
206  * ip_targ     - target (ipv4) header
207  * returns: the next header value
208  */
parse_frag_header(const struct ip6_frag * frag_hdr,struct iphdr * ip_targ)209 uint8_t parse_frag_header(const struct ip6_frag *frag_hdr, struct iphdr *ip_targ) {
210   uint16_t frag_off = (ntohs(frag_hdr->ip6f_offlg & IP6F_OFF_MASK) >> 3);
211   if (frag_hdr->ip6f_offlg & IP6F_MORE_FRAG) {
212     frag_off |= IP_MF;
213   }
214   ip_targ->frag_off = htons(frag_off);
215   ip_targ->id       = htons(ntohl(frag_hdr->ip6f_ident) & 0xffff);
216   ip_targ->protocol = frag_hdr->ip6f_nxt;
217   return frag_hdr->ip6f_nxt;
218 }
219 
220 /* function: icmp_to_icmp6
221  * translate ipv4 icmp to ipv6 icmp
222  * out          - output packet
223  * icmp         - source packet icmp header
224  * checksum     - pseudo-header checksum
225  * payload      - icmp payload
226  * payload_size - size of payload
227  * returns: the highest position in the output clat_packet that's filled in
228  */
icmp_to_icmp6(clat_packet out,clat_packet_index pos,const struct icmphdr * icmp,uint32_t checksum,const uint8_t * payload,size_t payload_size)229 int icmp_to_icmp6(clat_packet out, clat_packet_index pos, const struct icmphdr *icmp,
230                   uint32_t checksum, const uint8_t *payload, size_t payload_size) {
231   struct icmp6_hdr *icmp6_targ = out[pos].iov_base;
232   uint8_t icmp6_type;
233   int clat_packet_len;
234 
235   memset(icmp6_targ, 0, sizeof(struct icmp6_hdr));
236 
237   icmp6_type             = icmp_to_icmp6_type(icmp->type, icmp->code);
238   icmp6_targ->icmp6_type = icmp6_type;
239   icmp6_targ->icmp6_code = icmp_to_icmp6_code(icmp->type, icmp->code);
240 
241   out[pos].iov_len = sizeof(struct icmp6_hdr);
242 
243   if (pos == CLAT_POS_TRANSPORTHDR && is_icmp_error(icmp->type) && icmp6_type != ICMP6_PARAM_PROB) {
244     // An ICMP error we understand, one level deep.
245     // Translate the nested packet (the one that caused the error).
246     clat_packet_len = ipv4_packet(out, pos + 1, payload, payload_size);
247 
248     // The pseudo-header checksum was calculated on the transport length of the original IPv4
249     // packet that we were asked to translate. This transport length is 20 bytes smaller than it
250     // needs to be, because the ICMP error contains an IPv4 header, which we will be translating to
251     // an IPv6 header, which is 20 bytes longer. Fix it up here.
252     // We only need to do this for ICMP->ICMPv6, not ICMPv6->ICMP, because ICMP does not use the
253     // pseudo-header when calculating its checksum (as the IPv4 header has its own checksum).
254     checksum = checksum + htons(20);
255   } else if (icmp6_type == ICMP6_ECHO_REQUEST || icmp6_type == ICMP6_ECHO_REPLY) {
256     // Ping packet.
257     icmp6_targ->icmp6_id           = icmp->un.echo.id;
258     icmp6_targ->icmp6_seq          = icmp->un.echo.sequence;
259     out[CLAT_POS_PAYLOAD].iov_base = (uint8_t *)payload;
260     out[CLAT_POS_PAYLOAD].iov_len  = payload_size;
261     clat_packet_len                = CLAT_POS_PAYLOAD + 1;
262   } else {
263     // Unknown type/code. The type/code conversion functions have already logged an error.
264     return 0;
265   }
266 
267   icmp6_targ->icmp6_cksum = 0;  // Checksum field must be 0 when calculating checksum.
268   icmp6_targ->icmp6_cksum = packet_checksum(checksum, out, pos);
269 
270   return clat_packet_len;
271 }
272 
273 /* function: icmp6_to_icmp
274  * translate ipv6 icmp to ipv4 icmp
275  * out          - output packet
276  * icmp6        - source packet icmp6 header
277  * payload      - icmp6 payload
278  * payload_size - size of payload
279  * returns: the highest position in the output clat_packet that's filled in
280  */
icmp6_to_icmp(clat_packet out,clat_packet_index pos,const struct icmp6_hdr * icmp6,const uint8_t * payload,size_t payload_size)281 int icmp6_to_icmp(clat_packet out, clat_packet_index pos, const struct icmp6_hdr *icmp6,
282                   const uint8_t *payload, size_t payload_size) {
283   struct icmphdr *icmp_targ = out[pos].iov_base;
284   uint8_t icmp_type;
285   int clat_packet_len;
286 
287   memset(icmp_targ, 0, sizeof(struct icmphdr));
288 
289   icmp_type       = icmp6_to_icmp_type(icmp6->icmp6_type, icmp6->icmp6_code);
290   icmp_targ->type = icmp_type;
291   icmp_targ->code = icmp6_to_icmp_code(icmp6->icmp6_type, icmp6->icmp6_code);
292 
293   out[pos].iov_len = sizeof(struct icmphdr);
294 
295   if (pos == CLAT_POS_TRANSPORTHDR && is_icmp6_error(icmp6->icmp6_type) &&
296       icmp_type != ICMP_PARAMETERPROB) {
297     // An ICMPv6 error we understand, one level deep.
298     // Translate the nested packet (the one that caused the error).
299     clat_packet_len = ipv6_packet(out, pos + 1, payload, payload_size);
300   } else if (icmp_type == ICMP_ECHO || icmp_type == ICMP_ECHOREPLY) {
301     // Ping packet.
302     icmp_targ->un.echo.id          = icmp6->icmp6_id;
303     icmp_targ->un.echo.sequence    = icmp6->icmp6_seq;
304     out[CLAT_POS_PAYLOAD].iov_base = (uint8_t *)payload;
305     out[CLAT_POS_PAYLOAD].iov_len  = payload_size;
306     clat_packet_len                = CLAT_POS_PAYLOAD + 1;
307   } else {
308     // Unknown type/code. The type/code conversion functions have already logged an error.
309     return 0;
310   }
311 
312   icmp_targ->checksum = 0;  // Checksum field must be 0 when calculating checksum.
313   icmp_targ->checksum = packet_checksum(0, out, pos);
314 
315   return clat_packet_len;
316 }
317 
318 /* function: generic_packet
319  * takes a generic IP packet and sets it up for translation
320  * out      - output packet
321  * pos      - position in the output packet of the transport header
322  * payload  - pointer to IP payload
323  * len      - size of ip payload
324  * returns: the highest position in the output clat_packet that's filled in
325  */
generic_packet(clat_packet out,clat_packet_index pos,const uint8_t * payload,size_t len)326 int generic_packet(clat_packet out, clat_packet_index pos, const uint8_t *payload, size_t len) {
327   out[pos].iov_len               = 0;
328   out[CLAT_POS_PAYLOAD].iov_base = (uint8_t *)payload;
329   out[CLAT_POS_PAYLOAD].iov_len  = len;
330 
331   return CLAT_POS_PAYLOAD + 1;
332 }
333 
334 /* function: udp_packet
335  * takes a udp packet and sets it up for translation
336  * out      - output packet
337  * udp      - pointer to udp header in packet
338  * old_sum  - pseudo-header checksum of old header
339  * new_sum  - pseudo-header checksum of new header
340  * len      - size of ip payload
341  */
udp_packet(clat_packet out,clat_packet_index pos,const struct udphdr * udp,uint32_t old_sum,uint32_t new_sum,size_t len)342 int udp_packet(clat_packet out, clat_packet_index pos, const struct udphdr *udp, uint32_t old_sum,
343                uint32_t new_sum, size_t len) {
344   const uint8_t *payload;
345   size_t payload_size;
346 
347   if (len < sizeof(struct udphdr)) {
348     logmsg_dbg(ANDROID_LOG_ERROR, "udp_packet/(too small)");
349     return 0;
350   }
351 
352   payload      = (const uint8_t *)(udp + 1);
353   payload_size = len - sizeof(struct udphdr);
354 
355   return udp_translate(out, pos, udp, old_sum, new_sum, payload, payload_size);
356 }
357 
358 /* function: tcp_packet
359  * takes a tcp packet and sets it up for translation
360  * out      - output packet
361  * tcp      - pointer to tcp header in packet
362  * checksum - pseudo-header checksum
363  * len      - size of ip payload
364  * returns: the highest position in the output clat_packet that's filled in
365  */
tcp_packet(clat_packet out,clat_packet_index pos,const struct tcphdr * tcp,uint32_t old_sum,uint32_t new_sum,size_t len)366 int tcp_packet(clat_packet out, clat_packet_index pos, const struct tcphdr *tcp, uint32_t old_sum,
367                uint32_t new_sum, size_t len) {
368   const uint8_t *payload;
369   size_t payload_size, header_size;
370 
371   if (len < sizeof(struct tcphdr)) {
372     logmsg_dbg(ANDROID_LOG_ERROR, "tcp_packet/(too small)");
373     return 0;
374   }
375 
376   if (tcp->doff < 5) {
377     logmsg_dbg(ANDROID_LOG_ERROR, "tcp_packet/tcp header length set to less than 5: %x", tcp->doff);
378     return 0;
379   }
380 
381   if ((size_t)tcp->doff * 4 > len) {
382     logmsg_dbg(ANDROID_LOG_ERROR, "tcp_packet/tcp header length set too large: %x", tcp->doff);
383     return 0;
384   }
385 
386   header_size  = tcp->doff * 4;
387   payload      = ((const uint8_t *)tcp) + header_size;
388   payload_size = len - header_size;
389 
390   return tcp_translate(out, pos, tcp, header_size, old_sum, new_sum, payload, payload_size);
391 }
392 
393 /* function: udp_translate
394  * common between ipv4/ipv6 - setup checksum and send udp packet
395  * out          - output packet
396  * udp          - udp header
397  * old_sum      - pseudo-header checksum of old header
398  * new_sum      - pseudo-header checksum of new header
399  * payload      - tcp payload
400  * payload_size - size of payload
401  * returns: the highest position in the output clat_packet that's filled in
402  */
udp_translate(clat_packet out,clat_packet_index pos,const struct udphdr * udp,uint32_t old_sum,uint32_t new_sum,const uint8_t * payload,size_t payload_size)403 int udp_translate(clat_packet out, clat_packet_index pos, const struct udphdr *udp,
404                   uint32_t old_sum, uint32_t new_sum, const uint8_t *payload, size_t payload_size) {
405   struct udphdr *udp_targ = out[pos].iov_base;
406 
407   memcpy(udp_targ, udp, sizeof(struct udphdr));
408 
409   out[pos].iov_len               = sizeof(struct udphdr);
410   out[CLAT_POS_PAYLOAD].iov_base = (uint8_t *)payload;
411   out[CLAT_POS_PAYLOAD].iov_len  = payload_size;
412 
413   if (udp_targ->check) {
414     udp_targ->check = ip_checksum_adjust(udp->check, old_sum, new_sum);
415   } else {
416     // Zero checksums are special. RFC 768 says, "An all zero transmitted checksum value means that
417     // the transmitter generated no checksum (for debugging or for higher level protocols that
418     // don't care)." However, in IPv6 zero UDP checksums were only permitted by RFC 6935 (2013). So
419     // for safety we recompute it.
420     udp_targ->check = 0;  // Checksum field must be 0 when calculating checksum.
421     udp_targ->check = packet_checksum(new_sum, out, pos);
422   }
423 
424   // RFC 768: "If the computed checksum is zero, it is transmitted as all ones (the equivalent
425   // in one's complement arithmetic)."
426   if (!udp_targ->check) {
427     udp_targ->check = 0xffff;
428   }
429 
430   return CLAT_POS_PAYLOAD + 1;
431 }
432 
433 /* function: tcp_translate
434  * common between ipv4/ipv6 - setup checksum and send tcp packet
435  * out          - output packet
436  * tcp          - tcp header
437  * header_size  - size of tcp header including options
438  * checksum     - partial checksum covering ipv4/ipv6 header
439  * payload      - tcp payload
440  * payload_size - size of payload
441  * returns: the highest position in the output clat_packet that's filled in
442  */
tcp_translate(clat_packet out,clat_packet_index pos,const struct tcphdr * tcp,size_t header_size,uint32_t old_sum,uint32_t new_sum,const uint8_t * payload,size_t payload_size)443 int tcp_translate(clat_packet out, clat_packet_index pos, const struct tcphdr *tcp,
444                   size_t header_size, uint32_t old_sum, uint32_t new_sum, const uint8_t *payload,
445                   size_t payload_size) {
446   struct tcphdr *tcp_targ = out[pos].iov_base;
447   out[pos].iov_len        = header_size;
448 
449   if (header_size > MAX_TCP_HDR) {
450     // A TCP header cannot be more than MAX_TCP_HDR bytes long because it's a 4-bit field that
451     // counts in 4-byte words. So this can never happen unless there is a bug in the caller.
452     logmsg(ANDROID_LOG_ERROR, "tcp_translate: header too long %d > %d, truncating", header_size,
453            MAX_TCP_HDR);
454     header_size = MAX_TCP_HDR;
455   }
456 
457   memcpy(tcp_targ, tcp, header_size);
458 
459   out[CLAT_POS_PAYLOAD].iov_base = (uint8_t *)payload;
460   out[CLAT_POS_PAYLOAD].iov_len  = payload_size;
461 
462   tcp_targ->check = ip_checksum_adjust(tcp->check, old_sum, new_sum);
463 
464   return CLAT_POS_PAYLOAD + 1;
465 }
466 
467 // Weak symbol so we can override it in the unit test.
468 void send_rawv6(int fd, clat_packet out, int iov_len) __attribute__((weak));
469 
send_rawv6(int fd,clat_packet out,int iov_len)470 void send_rawv6(int fd, clat_packet out, int iov_len) {
471   // A send on a raw socket requires a destination address to be specified even if the socket's
472   // protocol is IPPROTO_RAW. This is the address that will be used in routing lookups; the
473   // destination address in the packet header only affects what appears on the wire, not where the
474   // packet is sent to.
475   static struct sockaddr_in6 sin6 = { AF_INET6, 0, 0, { { { 0, 0, 0, 0 } } }, 0 };
476   static struct msghdr msg        = {
477     .msg_name    = &sin6,
478     .msg_namelen = sizeof(sin6),
479   };
480 
481   msg.msg_iov = out, msg.msg_iovlen = iov_len,
482   sin6.sin6_addr = ((struct ip6_hdr *)out[CLAT_POS_IPHDR].iov_base)->ip6_dst;
483   sendmsg(fd, &msg, 0);
484 }
485 
486 /* function: translate_packet
487  * takes a packet, translates it, and writes it to fd
488  * fd         - fd to write translated packet to
489  * to_ipv6    - true if translating to ipv6, false if translating to ipv4
490  * packet     - packet
491  * packetsize - size of packet
492  */
translate_packet(int fd,int to_ipv6,const uint8_t * packet,size_t packetsize)493 void translate_packet(int fd, int to_ipv6, const uint8_t *packet, size_t packetsize) {
494   int iov_len = 0;
495 
496   // Allocate buffers for all packet headers.
497   struct tun_pi tun_targ;
498   char iphdr[sizeof(struct ip6_hdr)];
499   char fraghdr[sizeof(struct ip6_frag)];
500   char transporthdr[MAX_TCP_HDR];
501   char icmp_iphdr[sizeof(struct ip6_hdr)];
502   char icmp_fraghdr[sizeof(struct ip6_frag)];
503   char icmp_transporthdr[MAX_TCP_HDR];
504 
505   // iovec of the packets we'll send. This gets passed down to the translation functions.
506   clat_packet out = {
507     { &tun_targ, 0 },          // Tunnel header.
508     { iphdr, 0 },              // IP header.
509     { fraghdr, 0 },            // Fragment header.
510     { transporthdr, 0 },       // Transport layer header.
511     { icmp_iphdr, 0 },         // ICMP error inner IP header.
512     { icmp_fraghdr, 0 },       // ICMP error fragmentation header.
513     { icmp_transporthdr, 0 },  // ICMP error transport layer header.
514     { NULL, 0 },               // Payload. No buffer, it's a pointer to the original payload.
515   };
516 
517   if (to_ipv6) {
518     iov_len = ipv4_packet(out, CLAT_POS_IPHDR, packet, packetsize);
519     if (iov_len > 0) {
520       send_rawv6(fd, out, iov_len);
521     }
522   } else {
523     iov_len = ipv6_packet(out, CLAT_POS_IPHDR, packet, packetsize);
524     if (iov_len > 0) {
525       fill_tun_header(&tun_targ, ETH_P_IP);
526       out[CLAT_POS_TUNHDR].iov_len = sizeof(tun_targ);
527       send_tun(fd, out, iov_len);
528     }
529   }
530 }
531