Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 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> |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 19 | |
| 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 Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 29 | #include "translate.h" |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 30 | #include "checksum.h" |
| 31 | #include "clatd.h" |
| 32 | #include "config.h" |
| 33 | #include "logging.h" |
| 34 | #include "debug.h" |
| 35 | |
Lorenzo Colitti | ee80ca6 | 2013-04-10 16:52:22 +0900 | [diff] [blame^] | 36 | /* 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 | */ |
| 43 | uint16_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 Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 55 | * 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 Colitti | ee80ca6 | 2013-04-10 16:52:22 +0900 | [diff] [blame^] | 59 | uint16_t packet_length(clat_packet packet, int pos) { |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 60 | size_t len = 0; |
| 61 | int i; |
Lorenzo Colitti | ee80ca6 | 2013-04-10 16:52:22 +0900 | [diff] [blame^] | 62 | for (i = pos + 1; i < CLAT_POS_MAX; i++) { |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 63 | len += packet[i].iov_len; |
| 64 | } |
| 65 | return len; |
| 66 | } |
| 67 | |
Lorenzo Colitti | ee80ca6 | 2013-04-10 16:52:22 +0900 | [diff] [blame^] | 68 | /* function: is_in_plat_subnet |
| 69 | * returns true iff the given IPv6 address is in the plat subnet. |
| 70 | * addr - IPv6 address |
| 71 | */ |
| 72 | int 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 | */ |
| 82 | uint32_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 | */ |
| 97 | struct 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 Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 111 | /* 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 | */ |
| 116 | void 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 Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 121 | /* function: fill_ip_header |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 122 | * generate an ipv4 header from an ipv6 header |
| 123 | * ip_targ - (ipv4) target packet header, source: original ipv4 addr, dest: local subnet addr |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 124 | * payload_len - length of other data inside packet |
| 125 | * protocol - protocol number (tcp, udp, etc) |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 126 | * old_header - (ipv6) source packet header, source: nat64 prefix, dest: local subnet prefix |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 127 | */ |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 128 | void 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 Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 131 | |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 132 | 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 Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 141 | |
Lorenzo Colitti | ee80ca6 | 2013-04-10 16:52:22 +0900 | [diff] [blame^] | 142 | ip->saddr = ipv6_addr_to_ipv4_addr(&old_header->ip6_src); |
| 143 | ip->daddr = ipv6_addr_to_ipv4_addr(&old_header->ip6_dst); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | /* function: fill_ip6_header |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 147 | * generate an ipv6 header from an ipv4 header |
| 148 | * ip6 - (ipv6) target packet header, source: local subnet prefix, dest: nat64 prefix |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 149 | * payload_len - length of other data inside packet |
| 150 | * protocol - protocol number (tcp, udp, etc) |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 151 | * old_header - (ipv4) source packet header, source: local subnet addr, dest: internet's ipv4 addr |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 152 | */ |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 153 | void fill_ip6_header(struct ip6_hdr *ip6, uint16_t payload_len, uint8_t protocol, |
| 154 | const struct iphdr *old_header) { |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 155 | 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 Colitti | ee80ca6 | 2013-04-10 16:52:22 +0900 | [diff] [blame^] | 162 | ip6->ip6_src = ipv4_addr_to_ipv6_addr(old_header->saddr); |
| 163 | ip6->ip6_dst = ipv4_addr_to_ipv6_addr(old_header->daddr); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | /* function: icmp_to_icmp6 |
| 167 | * translate ipv4 icmp to ipv6 icmp (only currently supports echo/echo reply) |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 168 | * out - output packet |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 169 | * icmp - source packet icmp header |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 170 | * checksum - pseudo-header checksum |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 171 | * payload - icmp payload |
| 172 | * payload_size - size of payload |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 173 | * returns: the highest position in the output clat_packet that's filled in |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 174 | */ |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 175 | int 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 Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 178 | uint32_t checksum_temp; |
| 179 | |
| 180 | if((icmp->type != ICMP_ECHO) && (icmp->type != ICMP_ECHOREPLY)) { |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 181 | logmsg_dbg(ANDROID_LOG_WARN,"icmp_to_icmp6/unhandled icmp type: 0x%x", icmp->type); |
| 182 | return 0; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 183 | } |
| 184 | |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 185 | 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 Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 190 | |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 191 | out[pos].iov_len = sizeof(struct icmp6_hdr); |
Lorenzo Colitti | ee80ca6 | 2013-04-10 16:52:22 +0900 | [diff] [blame^] | 192 | out[CLAT_POS_PAYLOAD].iov_base = (char *) payload; |
| 193 | out[CLAT_POS_PAYLOAD].iov_len = payload_size; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 194 | |
Lorenzo Colitti | ee80ca6 | 2013-04-10 16:52:22 +0900 | [diff] [blame^] | 195 | 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 Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | /* function: icmp6_to_icmp |
| 202 | * translate ipv6 icmp to ipv4 icmp (only currently supports echo/echo reply) |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 203 | * out - output packet |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 204 | * icmp6 - source packet icmp6 header |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 205 | * checksum - pseudo-header checksum (unused) |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 206 | * payload - icmp6 payload |
| 207 | * payload_size - size of payload |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 208 | * returns: the highest position in the output clat_packet that's filled in |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 209 | */ |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 210 | int 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 Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 213 | |
| 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 Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 216 | return 0; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 217 | } |
| 218 | |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 219 | memset(icmp_targ, 0, sizeof(struct icmphdr)); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 220 | |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 221 | 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 Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 225 | |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 226 | out[pos].iov_len = sizeof(struct icmphdr); |
Lorenzo Colitti | ee80ca6 | 2013-04-10 16:52:22 +0900 | [diff] [blame^] | 227 | out[CLAT_POS_PAYLOAD].iov_base = (char *) payload; |
| 228 | out[CLAT_POS_PAYLOAD].iov_len = payload_size; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 229 | |
Lorenzo Colitti | ee80ca6 | 2013-04-10 16:52:22 +0900 | [diff] [blame^] | 230 | 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 Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 234 | } |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 235 | |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 236 | /* 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 | */ |
| 243 | int 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 | */ |
| 266 | int 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 Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | /* function: udp_translate |
| 293 | * common between ipv4/ipv6 - setup checksum and send udp packet |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 294 | * out - output packet |
| 295 | * udp - udp header |
| 296 | * checksum - pseudo-header checksum |
| 297 | * payload - tcp payload |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 298 | * payload_size - size of payload |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 299 | * returns: the highest position in the output clat_packet that's filled in |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 300 | */ |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 301 | int 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 Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 304 | |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 305 | memcpy(udp_targ, udp, sizeof(struct udphdr)); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 306 | |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 307 | out[pos].iov_len = sizeof(struct udphdr); |
Lorenzo Colitti | ee80ca6 | 2013-04-10 16:52:22 +0900 | [diff] [blame^] | 308 | out[CLAT_POS_PAYLOAD].iov_base = (char *) payload; |
| 309 | out[CLAT_POS_PAYLOAD].iov_len = payload_size; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 310 | |
Lorenzo Colitti | ee80ca6 | 2013-04-10 16:52:22 +0900 | [diff] [blame^] | 311 | 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 Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | /* function: tcp_translate |
| 318 | * common between ipv4/ipv6 - setup checksum and send tcp packet |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 319 | * 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 Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 323 | * payload - tcp payload |
| 324 | * payload_size - size of payload |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 325 | * returns: the highest position in the output clat_packet that's filled in |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 326 | * |
| 327 | * TODO: mss rewrite |
| 328 | * TODO: hosts without pmtu discovery - non DF packets will rely on fragmentation (unimplemented) |
| 329 | */ |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 330 | int 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 Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 334 | |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 335 | 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 Colitti | 5cc877d | 2013-04-08 19:12:43 +0900 | [diff] [blame] | 338 | logmsg(ANDROID_LOG_ERROR, "tcp_translate: header too long %d > %d, truncating", |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 339 | header_size, MAX_TCP_HDR); |
| 340 | header_size = MAX_TCP_HDR; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 341 | } |
Lorenzo Colitti | 5cc877d | 2013-04-08 19:12:43 +0900 | [diff] [blame] | 342 | |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 343 | memcpy(tcp_targ, tcp, header_size); |
Lorenzo Colitti | 5cc877d | 2013-04-08 19:12:43 +0900 | [diff] [blame] | 344 | |
Lorenzo Colitti | ee80ca6 | 2013-04-10 16:52:22 +0900 | [diff] [blame^] | 345 | out[CLAT_POS_PAYLOAD].iov_base = (char *)payload; |
| 346 | out[CLAT_POS_PAYLOAD].iov_len = payload_size; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 347 | |
Lorenzo Colitti | ee80ca6 | 2013-04-10 16:52:22 +0900 | [diff] [blame^] | 348 | tcp_targ->check = 0; // Checksum field must be 0 when calculating checksum. |
| 349 | tcp_targ->check = packet_checksum(checksum, out, pos); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 350 | |
Lorenzo Colitti | ee80ca6 | 2013-04-10 16:52:22 +0900 | [diff] [blame^] | 351 | return CLAT_POS_PAYLOAD + 1; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 352 | } |