blob: 4092bccfb2ba0de1942bf8fb48f46646273c82c1 [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 Colittid9084182013-03-22 00:42:21 +090029#include "translate.h"
Daniel Drowna45056e2012-03-23 10:42:54 -050030#include "checksum.h"
31#include "clatd.h"
32#include "config.h"
33#include "logging.h"
34#include "debug.h"
35
Lorenzo Colittiee80ca62013-04-10 16:52:22 +090036/* function: packet_checksum
37 * calculates the checksum over all the packet components starting from pos
38 * checksum - checksum of packet components before pos
39 * packet - packet to calculate the checksum of
40 * pos - position to start counting from
41 * returns - the completed 16-bit checksum, ready to write into a checksum header field
42 */
43uint16_t packet_checksum(uint32_t checksum, clat_packet packet, int pos) {
44 int i;
45 for (i = pos; i < CLAT_POS_MAX; i++) {
46 if (packet[i].iov_len > 0) {
47 checksum = ip_checksum_add(checksum, packet[i].iov_base, packet[i].iov_len);
48 }
49 }
50 return ip_checksum_finish(checksum);
51}
52
53/* function: packet_length
54 * returns the total length of all the packet components after pos
Lorenzo Colittid9084182013-03-22 00:42:21 +090055 * packet - packet to calculate the length of
56 * pos - position to start counting from
57 * returns: the total length of the packet components after pos
58 */
Lorenzo Colittiee80ca62013-04-10 16:52:22 +090059uint16_t packet_length(clat_packet packet, int pos) {
Lorenzo Colittid9084182013-03-22 00:42:21 +090060 size_t len = 0;
61 int i;
Lorenzo Colittiee80ca62013-04-10 16:52:22 +090062 for (i = pos + 1; i < CLAT_POS_MAX; i++) {
Lorenzo Colittid9084182013-03-22 00:42:21 +090063 len += packet[i].iov_len;
64 }
65 return len;
66}
67
Lorenzo Colittiee80ca62013-04-10 16:52:22 +090068/* function: is_in_plat_subnet
69 * returns true iff the given IPv6 address is in the plat subnet.
70 * addr - IPv6 address
71 */
72int is_in_plat_subnet(const struct in6_addr *addr6) {
73 // Assumes a /96 plat subnet.
74 return (addr6 != NULL) && (memcmp(addr6, &Global_Clatd_Config.plat_subnet, 12) == 0);
75}
76
77/* function: ipv6_addr_to_ipv4_addr
78 * return the corresponding ipv4 address for the given ipv6 address
79 * addr6 - ipv6 address
80 * returns: the IPv4 address
81 */
82uint32_t ipv6_addr_to_ipv4_addr(const struct in6_addr *addr6) {
83
84 if (is_in_plat_subnet(addr6)) {
85 // Assumes a /96 plat subnet.
86 return addr6->s6_addr32[3];
87 } else {
88 // Currently this can only be our own address; other packets are dropped by ipv6_packet.
89 return Global_Clatd_Config.ipv4_local_subnet.s_addr;
90 }
91}
92
93/* function: ipv4_addr_to_ipv6_addr
94 * return the corresponding ipv6 address for the given ipv4 address
95 * addr4 - ipv4 address
96 */
97struct in6_addr ipv4_addr_to_ipv6_addr(uint32_t addr4) {
98 struct in6_addr addr6;
99 // Both addresses are in network byte order (addr4 comes from a network packet, and the config
100 // file entry is read using inet_ntop).
101 if (addr4 == Global_Clatd_Config.ipv4_local_subnet.s_addr) {
102 return Global_Clatd_Config.ipv6_local_subnet;
103 } else {
104 // Assumes a /96 plat subnet.
105 addr6 = Global_Clatd_Config.plat_subnet;
106 addr6.s6_addr32[3] = addr4;
107 return addr6;
108 }
109}
110
Daniel Drowna45056e2012-03-23 10:42:54 -0500111/* function: fill_tun_header
112 * fill in the header for the tun fd
113 * tun_header - tunnel header, already allocated
114 * proto - ethernet protocol id: ETH_P_IP(ipv4) or ETH_P_IPV6(ipv6)
115 */
116void fill_tun_header(struct tun_pi *tun_header, uint16_t proto) {
117 tun_header->flags = 0;
118 tun_header->proto = htons(proto);
119}
120
Daniel Drowna45056e2012-03-23 10:42:54 -0500121/* function: fill_ip_header
Lorenzo Colittid9084182013-03-22 00:42:21 +0900122 * generate an ipv4 header from an ipv6 header
123 * ip_targ - (ipv4) target packet header, source: original ipv4 addr, dest: local subnet addr
Daniel Drowna45056e2012-03-23 10:42:54 -0500124 * payload_len - length of other data inside packet
125 * protocol - protocol number (tcp, udp, etc)
Lorenzo Colittid9084182013-03-22 00:42:21 +0900126 * old_header - (ipv6) source packet header, source: nat64 prefix, dest: local subnet prefix
Daniel Drowna45056e2012-03-23 10:42:54 -0500127 */
Lorenzo Colittid9084182013-03-22 00:42:21 +0900128void fill_ip_header(struct iphdr *ip, uint16_t payload_len, uint8_t protocol,
129 const struct ip6_hdr *old_header) {
130 memset(ip, 0, sizeof(struct iphdr));
Daniel Drowna45056e2012-03-23 10:42:54 -0500131
Lorenzo Colittid9084182013-03-22 00:42:21 +0900132 ip->ihl = 5;
133 ip->version = 4;
134 ip->tos = 0;
135 ip->tot_len = htons(sizeof(struct iphdr) + payload_len);
136 ip->id = 0;
137 ip->frag_off = htons(IP_DF);
138 ip->ttl = old_header->ip6_hlim;
139 ip->protocol = protocol;
140 ip->check = 0;
Daniel Drowna45056e2012-03-23 10:42:54 -0500141
Lorenzo Colittiee80ca62013-04-10 16:52:22 +0900142 ip->saddr = ipv6_addr_to_ipv4_addr(&old_header->ip6_src);
143 ip->daddr = ipv6_addr_to_ipv4_addr(&old_header->ip6_dst);
Daniel Drowna45056e2012-03-23 10:42:54 -0500144}
145
146/* function: fill_ip6_header
Lorenzo Colittid9084182013-03-22 00:42:21 +0900147 * generate an ipv6 header from an ipv4 header
148 * ip6 - (ipv6) target packet header, source: local subnet prefix, dest: nat64 prefix
Daniel Drowna45056e2012-03-23 10:42:54 -0500149 * payload_len - length of other data inside packet
150 * protocol - protocol number (tcp, udp, etc)
Lorenzo Colittid9084182013-03-22 00:42:21 +0900151 * old_header - (ipv4) source packet header, source: local subnet addr, dest: internet's ipv4 addr
Daniel Drowna45056e2012-03-23 10:42:54 -0500152 */
Lorenzo Colittid9084182013-03-22 00:42:21 +0900153void fill_ip6_header(struct ip6_hdr *ip6, uint16_t payload_len, uint8_t protocol,
154 const struct iphdr *old_header) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500155 memset(ip6, 0, sizeof(struct ip6_hdr));
156
157 ip6->ip6_vfc = 6 << 4;
158 ip6->ip6_plen = htons(payload_len);
159 ip6->ip6_nxt = protocol;
160 ip6->ip6_hlim = old_header->ttl;
161
Lorenzo Colittiee80ca62013-04-10 16:52:22 +0900162 ip6->ip6_src = ipv4_addr_to_ipv6_addr(old_header->saddr);
163 ip6->ip6_dst = ipv4_addr_to_ipv6_addr(old_header->daddr);
Daniel Drowna45056e2012-03-23 10:42:54 -0500164}
165
166/* function: icmp_to_icmp6
167 * translate ipv4 icmp to ipv6 icmp (only currently supports echo/echo reply)
Lorenzo Colittid9084182013-03-22 00:42:21 +0900168 * out - output packet
Daniel Drowna45056e2012-03-23 10:42:54 -0500169 * icmp - source packet icmp header
Lorenzo Colittid9084182013-03-22 00:42:21 +0900170 * checksum - pseudo-header checksum
Daniel Drowna45056e2012-03-23 10:42:54 -0500171 * payload - icmp payload
172 * payload_size - size of payload
Lorenzo Colittid9084182013-03-22 00:42:21 +0900173 * returns: the highest position in the output clat_packet that's filled in
Daniel Drowna45056e2012-03-23 10:42:54 -0500174 */
Lorenzo Colittid9084182013-03-22 00:42:21 +0900175int icmp_to_icmp6(clat_packet out, int pos, const struct icmphdr *icmp, uint32_t checksum,
176 const char *payload, size_t payload_size) {
177 struct icmp6_hdr *icmp6_targ = out[pos].iov_base;
Daniel Drowna45056e2012-03-23 10:42:54 -0500178 uint32_t checksum_temp;
179
180 if((icmp->type != ICMP_ECHO) && (icmp->type != ICMP_ECHOREPLY)) {
Lorenzo Colittid9084182013-03-22 00:42:21 +0900181 logmsg_dbg(ANDROID_LOG_WARN,"icmp_to_icmp6/unhandled icmp type: 0x%x", icmp->type);
182 return 0;
Daniel Drowna45056e2012-03-23 10:42:54 -0500183 }
184
Lorenzo Colittid9084182013-03-22 00:42:21 +0900185 memset(icmp6_targ, 0, sizeof(struct icmp6_hdr));
186 icmp6_targ->icmp6_type = (icmp->type == ICMP_ECHO) ? ICMP6_ECHO_REQUEST : ICMP6_ECHO_REPLY;
187 icmp6_targ->icmp6_code = 0;
188 icmp6_targ->icmp6_id = icmp->un.echo.id;
189 icmp6_targ->icmp6_seq = icmp->un.echo.sequence;
Daniel Drowna45056e2012-03-23 10:42:54 -0500190
Lorenzo Colittid9084182013-03-22 00:42:21 +0900191 out[pos].iov_len = sizeof(struct icmp6_hdr);
Lorenzo Colittiee80ca62013-04-10 16:52:22 +0900192 out[CLAT_POS_PAYLOAD].iov_base = (char *) payload;
193 out[CLAT_POS_PAYLOAD].iov_len = payload_size;
Daniel Drowna45056e2012-03-23 10:42:54 -0500194
Lorenzo Colittiee80ca62013-04-10 16:52:22 +0900195 icmp6_targ->icmp6_cksum = 0; // Checksum field must be 0 when calculating checksum.
196 icmp6_targ->icmp6_cksum = packet_checksum(checksum, out, pos);
197
198 return CLAT_POS_PAYLOAD + 1;
Daniel Drowna45056e2012-03-23 10:42:54 -0500199}
200
201/* function: icmp6_to_icmp
202 * translate ipv6 icmp to ipv4 icmp (only currently supports echo/echo reply)
Lorenzo Colittid9084182013-03-22 00:42:21 +0900203 * out - output packet
Daniel Drowna45056e2012-03-23 10:42:54 -0500204 * icmp6 - source packet icmp6 header
Lorenzo Colittid9084182013-03-22 00:42:21 +0900205 * checksum - pseudo-header checksum (unused)
Daniel Drowna45056e2012-03-23 10:42:54 -0500206 * payload - icmp6 payload
207 * payload_size - size of payload
Lorenzo Colittid9084182013-03-22 00:42:21 +0900208 * returns: the highest position in the output clat_packet that's filled in
Daniel Drowna45056e2012-03-23 10:42:54 -0500209 */
Lorenzo Colittid9084182013-03-22 00:42:21 +0900210int icmp6_to_icmp(clat_packet out, int pos, const struct icmp6_hdr *icmp6, uint32_t checksum,
211 const char *payload, size_t payload_size) {
212 struct icmphdr *icmp_targ = out[pos].iov_base;
Daniel Drowna45056e2012-03-23 10:42:54 -0500213
214 if((icmp6->icmp6_type != ICMP6_ECHO_REQUEST) && (icmp6->icmp6_type != ICMP6_ECHO_REPLY)) {
215 logmsg_dbg(ANDROID_LOG_WARN,"icmp6_to_icmp/unhandled icmp6 type: 0x%x",icmp6->icmp6_type);
Lorenzo Colittid9084182013-03-22 00:42:21 +0900216 return 0;
Daniel Drowna45056e2012-03-23 10:42:54 -0500217 }
218
Lorenzo Colittid9084182013-03-22 00:42:21 +0900219 memset(icmp_targ, 0, sizeof(struct icmphdr));
Daniel Drowna45056e2012-03-23 10:42:54 -0500220
Lorenzo Colittid9084182013-03-22 00:42:21 +0900221 icmp_targ->type = (icmp6->icmp6_type == ICMP6_ECHO_REQUEST) ? ICMP_ECHO : ICMP_ECHOREPLY;
222 icmp_targ->code = 0x0;
223 icmp_targ->un.echo.id = icmp6->icmp6_id;
224 icmp_targ->un.echo.sequence = icmp6->icmp6_seq;
Daniel Drowna45056e2012-03-23 10:42:54 -0500225
Lorenzo Colittid9084182013-03-22 00:42:21 +0900226 out[pos].iov_len = sizeof(struct icmphdr);
Lorenzo Colittiee80ca62013-04-10 16:52:22 +0900227 out[CLAT_POS_PAYLOAD].iov_base = (char *) payload;
228 out[CLAT_POS_PAYLOAD].iov_len = payload_size;
Daniel Drowna45056e2012-03-23 10:42:54 -0500229
Lorenzo Colittiee80ca62013-04-10 16:52:22 +0900230 icmp_targ->checksum = 0; // Checksum field must be 0 when calculating checksum.
231 icmp_targ->checksum = packet_checksum(0, out, pos);
232
233 return CLAT_POS_PAYLOAD + 1;
Lorenzo Colittid9084182013-03-22 00:42:21 +0900234}
Daniel Drowna45056e2012-03-23 10:42:54 -0500235
Lorenzo Colittid9084182013-03-22 00:42:21 +0900236/* function: udp_packet
237 * takes a udp packet and sets it up for translation
238 * out - output packet
239 * udp - pointer to udp header in packet
240 * checksum - pseudo-header checksum
241 * len - size of ip payload
242 */
243int udp_packet(clat_packet out, int pos, const struct udphdr *udp, uint32_t checksum, size_t len) {
244 const char *payload;
245 size_t payload_size;
246
247 if(len < sizeof(struct udphdr)) {
248 logmsg_dbg(ANDROID_LOG_ERROR,"udp_packet/(too small)");
249 return 0;
250 }
251
252 payload = (const char *) (udp + 1);
253 payload_size = len - sizeof(struct udphdr);
254
255 return udp_translate(out, pos, udp, checksum, payload, payload_size);
256}
257
258/* function: tcp_packet
259 * takes a tcp packet and sets it up for translation
260 * out - output packet
261 * tcp - pointer to tcp header in packet
262 * checksum - pseudo-header checksum
263 * len - size of ip payload
264 * returns: the highest position in the output clat_packet that's filled in
265 */
266int tcp_packet(clat_packet out, int pos, const struct tcphdr *tcp, uint32_t checksum, size_t len) {
267 const char *payload;
268 size_t payload_size, header_size;
269
270 if(len < sizeof(struct tcphdr)) {
271 logmsg_dbg(ANDROID_LOG_ERROR,"tcp_packet/(too small)");
272 return 0;
273 }
274
275 if(tcp->doff < 5) {
276 logmsg_dbg(ANDROID_LOG_ERROR,"tcp_packet/tcp header length set to less than 5: %x", tcp->doff);
277 return 0;
278 }
279
280 if((size_t) tcp->doff*4 > len) {
281 logmsg_dbg(ANDROID_LOG_ERROR,"tcp_packet/tcp header length set too large: %x", tcp->doff);
282 return 0;
283 }
284
285 header_size = tcp->doff * 4;
286 payload = ((const char *) tcp) + header_size;
287 payload_size = len - header_size;
288
289 return tcp_translate(out, pos, tcp, header_size, checksum, payload, payload_size);
Daniel Drowna45056e2012-03-23 10:42:54 -0500290}
291
292/* function: udp_translate
293 * common between ipv4/ipv6 - setup checksum and send udp packet
Lorenzo Colittid9084182013-03-22 00:42:21 +0900294 * out - output packet
295 * udp - udp header
296 * checksum - pseudo-header checksum
297 * payload - tcp payload
Daniel Drowna45056e2012-03-23 10:42:54 -0500298 * payload_size - size of payload
Lorenzo Colittid9084182013-03-22 00:42:21 +0900299 * returns: the highest position in the output clat_packet that's filled in
Daniel Drowna45056e2012-03-23 10:42:54 -0500300 */
Lorenzo Colittid9084182013-03-22 00:42:21 +0900301int udp_translate(clat_packet out, int pos, const struct udphdr *udp, uint32_t checksum,
302 const char *payload, size_t payload_size) {
303 struct udphdr *udp_targ = out[pos].iov_base;
Daniel Drowna45056e2012-03-23 10:42:54 -0500304
Lorenzo Colittid9084182013-03-22 00:42:21 +0900305 memcpy(udp_targ, udp, sizeof(struct udphdr));
Daniel Drowna45056e2012-03-23 10:42:54 -0500306
Lorenzo Colittid9084182013-03-22 00:42:21 +0900307 out[pos].iov_len = sizeof(struct udphdr);
Lorenzo Colittiee80ca62013-04-10 16:52:22 +0900308 out[CLAT_POS_PAYLOAD].iov_base = (char *) payload;
309 out[CLAT_POS_PAYLOAD].iov_len = payload_size;
Daniel Drowna45056e2012-03-23 10:42:54 -0500310
Lorenzo Colittiee80ca62013-04-10 16:52:22 +0900311 udp_targ->check = 0; // Checksum field must be 0 when calculating checksum.
312 udp_targ->check = packet_checksum(checksum, out, pos);
313
314 return CLAT_POS_PAYLOAD + 1;
Daniel Drowna45056e2012-03-23 10:42:54 -0500315}
316
317/* function: tcp_translate
318 * common between ipv4/ipv6 - setup checksum and send tcp packet
Lorenzo Colittid9084182013-03-22 00:42:21 +0900319 * out - output packet
320 * tcp - tcp header
321 * header_size - size of tcp header including options
322 * checksum - partial checksum covering ipv4/ipv6 header
Daniel Drowna45056e2012-03-23 10:42:54 -0500323 * payload - tcp payload
324 * payload_size - size of payload
Lorenzo Colittid9084182013-03-22 00:42:21 +0900325 * returns: the highest position in the output clat_packet that's filled in
Daniel Drowna45056e2012-03-23 10:42:54 -0500326 *
327 * TODO: mss rewrite
328 * TODO: hosts without pmtu discovery - non DF packets will rely on fragmentation (unimplemented)
329 */
Lorenzo Colittid9084182013-03-22 00:42:21 +0900330int tcp_translate(clat_packet out, int pos, const struct tcphdr *tcp, size_t header_size,
331 uint32_t checksum, const char *payload, size_t payload_size) {
332 struct tcphdr *tcp_targ = out[pos].iov_base;
333 out[pos].iov_len = header_size;
Daniel Drowna45056e2012-03-23 10:42:54 -0500334
Lorenzo Colittid9084182013-03-22 00:42:21 +0900335 if (header_size > MAX_TCP_HDR) {
336 // A TCP header cannot be more than MAX_TCP_HDR bytes long because it's a 4-bit field that
337 // 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 +0900338 logmsg(ANDROID_LOG_ERROR, "tcp_translate: header too long %d > %d, truncating",
Lorenzo Colittid9084182013-03-22 00:42:21 +0900339 header_size, MAX_TCP_HDR);
340 header_size = MAX_TCP_HDR;
Daniel Drowna45056e2012-03-23 10:42:54 -0500341 }
Lorenzo Colitti5cc877d2013-04-08 19:12:43 +0900342
Lorenzo Colittid9084182013-03-22 00:42:21 +0900343 memcpy(tcp_targ, tcp, header_size);
Lorenzo Colitti5cc877d2013-04-08 19:12:43 +0900344
Lorenzo Colittiee80ca62013-04-10 16:52:22 +0900345 out[CLAT_POS_PAYLOAD].iov_base = (char *)payload;
346 out[CLAT_POS_PAYLOAD].iov_len = payload_size;
Daniel Drowna45056e2012-03-23 10:42:54 -0500347
Lorenzo Colittiee80ca62013-04-10 16:52:22 +0900348 tcp_targ->check = 0; // Checksum field must be 0 when calculating checksum.
349 tcp_targ->check = packet_checksum(checksum, out, pos);
Daniel Drowna45056e2012-03-23 10:42:54 -0500350
Lorenzo Colittiee80ca62013-04-10 16:52:22 +0900351 return CLAT_POS_PAYLOAD + 1;
Daniel Drowna45056e2012-03-23 10:42:54 -0500352}