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 | cd70b35 | 2013-04-10 12:24:56 +0900 | [diff] [blame] | 29 | #include "icmp.h" |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 30 | #include "translate.h" |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 31 | #include "checksum.h" |
| 32 | #include "clatd.h" |
| 33 | #include "config.h" |
| 34 | #include "logging.h" |
| 35 | #include "debug.h" |
| 36 | |
Lorenzo Colitti | ee80ca6 | 2013-04-10 16:52:22 +0900 | [diff] [blame] | 37 | /* 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 | */ |
| 44 | uint16_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 Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 56 | * packet - packet to calculate the length of |
Lorenzo Colitti | cd70b35 | 2013-04-10 12:24:56 +0900 | [diff] [blame] | 57 | * pos - position to start counting after |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 58 | * returns: the total length of the packet components after pos |
| 59 | */ |
Lorenzo Colitti | ee80ca6 | 2013-04-10 16:52:22 +0900 | [diff] [blame] | 60 | uint16_t packet_length(clat_packet packet, int pos) { |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 61 | size_t len = 0; |
| 62 | int i; |
Lorenzo Colitti | ee80ca6 | 2013-04-10 16:52:22 +0900 | [diff] [blame] | 63 | for (i = pos + 1; i < CLAT_POS_MAX; i++) { |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 64 | len += packet[i].iov_len; |
| 65 | } |
| 66 | return len; |
| 67 | } |
| 68 | |
Lorenzo Colitti | ee80ca6 | 2013-04-10 16:52:22 +0900 | [diff] [blame] | 69 | /* function: is_in_plat_subnet |
| 70 | * returns true iff the given IPv6 address is in the plat subnet. |
| 71 | * addr - IPv6 address |
| 72 | */ |
| 73 | int 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 | */ |
| 83 | uint32_t ipv6_addr_to_ipv4_addr(const struct in6_addr *addr6) { |
Lorenzo Colitti | ee80ca6 | 2013-04-10 16:52:22 +0900 | [diff] [blame] | 84 | if (is_in_plat_subnet(addr6)) { |
| 85 | // Assumes a /96 plat subnet. |
| 86 | return addr6->s6_addr32[3]; |
Lorenzo Colitti | cd70b35 | 2013-04-10 12:24:56 +0900 | [diff] [blame] | 87 | } else if (IN6_ARE_ADDR_EQUAL(addr6, &Global_Clatd_Config.ipv6_local_subnet)) { |
| 88 | // Special-case our own address. |
Lorenzo Colitti | ee80ca6 | 2013-04-10 16:52:22 +0900 | [diff] [blame] | 89 | return Global_Clatd_Config.ipv4_local_subnet.s_addr; |
Lorenzo Colitti | cd70b35 | 2013-04-10 12:24:56 +0900 | [diff] [blame] | 90 | } else { |
| 91 | // Third party packet. Let the caller deal with it. |
| 92 | return INADDR_NONE; |
Lorenzo Colitti | ee80ca6 | 2013-04-10 16:52:22 +0900 | [diff] [blame] | 93 | } |
| 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 | */ |
| 100 | struct 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 Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 114 | /* 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 | */ |
| 119 | void 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 Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 124 | /* function: fill_ip_header |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 125 | * generate an ipv4 header from an ipv6 header |
| 126 | * 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] | 127 | * payload_len - length of other data inside packet |
| 128 | * protocol - protocol number (tcp, udp, etc) |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 129 | * 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] | 130 | */ |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 131 | void fill_ip_header(struct iphdr *ip, uint16_t payload_len, uint8_t protocol, |
| 132 | const struct ip6_hdr *old_header) { |
Lorenzo Colitti | cd70b35 | 2013-04-10 12:24:56 +0900 | [diff] [blame] | 133 | int ttl_guess; |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 134 | memset(ip, 0, sizeof(struct iphdr)); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 135 | |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 136 | 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 Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 145 | |
Lorenzo Colitti | ee80ca6 | 2013-04-10 16:52:22 +0900 | [diff] [blame] | 146 | ip->saddr = ipv6_addr_to_ipv4_addr(&old_header->ip6_src); |
| 147 | ip->daddr = ipv6_addr_to_ipv4_addr(&old_header->ip6_dst); |
Lorenzo Colitti | cd70b35 | 2013-04-10 12:24:56 +0900 | [diff] [blame] | 148 | |
| 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 Colitti | 9477a46 | 2013-11-18 15:56:02 +0900 | [diff] [blame] | 152 | if ((uint32_t) ip->saddr == INADDR_NONE) { |
Lorenzo Colitti | cd70b35 | 2013-04-10 12:24:56 +0900 | [diff] [blame] | 153 | ttl_guess = icmp_guess_ttl(old_header->ip6_hlim); |
| 154 | ip->saddr = htonl((0xff << 24) + ttl_guess); |
| 155 | } |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | /* function: fill_ip6_header |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 159 | * generate an ipv6 header from an ipv4 header |
| 160 | * ip6 - (ipv6) target packet header, source: local subnet prefix, dest: nat64 prefix |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 161 | * payload_len - length of other data inside packet |
| 162 | * protocol - protocol number (tcp, udp, etc) |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 163 | * 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] | 164 | */ |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 165 | void fill_ip6_header(struct ip6_hdr *ip6, uint16_t payload_len, uint8_t protocol, |
| 166 | const struct iphdr *old_header) { |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 167 | 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 Colitti | ee80ca6 | 2013-04-10 16:52:22 +0900 | [diff] [blame] | 174 | ip6->ip6_src = ipv4_addr_to_ipv6_addr(old_header->saddr); |
| 175 | ip6->ip6_dst = ipv4_addr_to_ipv6_addr(old_header->daddr); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | /* function: icmp_to_icmp6 |
Lorenzo Colitti | cd70b35 | 2013-04-10 12:24:56 +0900 | [diff] [blame] | 179 | * translate ipv4 icmp to ipv6 icmp |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 180 | * out - output packet |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 181 | * icmp - source packet icmp header |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 182 | * checksum - pseudo-header checksum |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 183 | * payload - icmp payload |
| 184 | * payload_size - size of payload |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 185 | * 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] | 186 | */ |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 187 | int 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 Colitti | cd70b35 | 2013-04-10 12:24:56 +0900 | [diff] [blame] | 190 | uint8_t icmp6_type; |
| 191 | int clat_packet_len; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 192 | |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 193 | memset(icmp6_targ, 0, sizeof(struct icmp6_hdr)); |
Lorenzo Colitti | cd70b35 | 2013-04-10 12:24:56 +0900 | [diff] [blame] | 194 | |
| 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 Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 198 | |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 199 | out[pos].iov_len = sizeof(struct icmp6_hdr); |
Lorenzo Colitti | cd70b35 | 2013-04-10 12:24:56 +0900 | [diff] [blame] | 200 | |
| 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 Colitti | 5a50c02 | 2014-02-10 09:20:05 +0900 | [diff] [blame^] | 211 | // an IPv6 header, which is 20 bytes longer. Fix it up here. |
Lorenzo Colitti | cd70b35 | 2013-04-10 12:24:56 +0900 | [diff] [blame] | 212 | // 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 Colitti | 5a50c02 | 2014-02-10 09:20:05 +0900 | [diff] [blame^] | 214 | checksum = checksum + htons(20); |
Lorenzo Colitti | cd70b35 | 2013-04-10 12:24:56 +0900 | [diff] [blame] | 215 | } 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 Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 226 | |
Lorenzo Colitti | ee80ca6 | 2013-04-10 16:52:22 +0900 | [diff] [blame] | 227 | 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 Colitti | cd70b35 | 2013-04-10 12:24:56 +0900 | [diff] [blame] | 230 | return clat_packet_len; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | /* function: icmp6_to_icmp |
Lorenzo Colitti | cd70b35 | 2013-04-10 12:24:56 +0900 | [diff] [blame] | 234 | * translate ipv6 icmp to ipv4 icmp |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 235 | * out - output packet |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 236 | * icmp6 - source packet icmp6 header |
| 237 | * payload - icmp6 payload |
| 238 | * payload_size - size of payload |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 239 | * 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] | 240 | */ |
Lorenzo Colitti | 9477a46 | 2013-11-18 15:56:02 +0900 | [diff] [blame] | 241 | int icmp6_to_icmp(clat_packet out, int pos, const struct icmp6_hdr *icmp6, |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 242 | const char *payload, size_t payload_size) { |
| 243 | struct icmphdr *icmp_targ = out[pos].iov_base; |
Lorenzo Colitti | cd70b35 | 2013-04-10 12:24:56 +0900 | [diff] [blame] | 244 | uint8_t icmp_type; |
Lorenzo Colitti | cd70b35 | 2013-04-10 12:24:56 +0900 | [diff] [blame] | 245 | int clat_packet_len; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 246 | |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 247 | memset(icmp_targ, 0, sizeof(struct icmphdr)); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 248 | |
Lorenzo Colitti | cd70b35 | 2013-04-10 12:24:56 +0900 | [diff] [blame] | 249 | 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 Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 252 | |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 253 | out[pos].iov_len = sizeof(struct icmphdr); |
Lorenzo Colitti | cd70b35 | 2013-04-10 12:24:56 +0900 | [diff] [blame] | 254 | |
| 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 Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 272 | |
Lorenzo Colitti | ee80ca6 | 2013-04-10 16:52:22 +0900 | [diff] [blame] | 273 | icmp_targ->checksum = 0; // Checksum field must be 0 when calculating checksum. |
| 274 | icmp_targ->checksum = packet_checksum(0, out, pos); |
| 275 | |
Lorenzo Colitti | cd70b35 | 2013-04-10 12:24:56 +0900 | [diff] [blame] | 276 | return clat_packet_len; |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 277 | } |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 278 | |
Lorenzo Colitti | c9f4c89 | 2013-11-18 12:59:44 +0900 | [diff] [blame] | 279 | /* 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 | */ |
| 287 | int 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 Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 295 | /* 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 Colitti | 5a50c02 | 2014-02-10 09:20:05 +0900 | [diff] [blame^] | 299 | * old_sum - pseudo-header checksum of old header |
| 300 | * new_sum - pseudo-header checksum of new header |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 301 | * len - size of ip payload |
| 302 | */ |
Lorenzo Colitti | 5a50c02 | 2014-02-10 09:20:05 +0900 | [diff] [blame^] | 303 | int udp_packet(clat_packet out, int pos, const struct udphdr *udp, |
| 304 | uint32_t old_sum, uint32_t new_sum, size_t len) { |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 305 | 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 Colitti | 5a50c02 | 2014-02-10 09:20:05 +0900 | [diff] [blame^] | 316 | return udp_translate(out, pos, udp, old_sum, new_sum, payload, payload_size); |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 317 | } |
| 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 Colitti | 5a50c02 | 2014-02-10 09:20:05 +0900 | [diff] [blame^] | 327 | int tcp_packet(clat_packet out, int pos, const struct tcphdr *tcp, |
| 328 | uint32_t old_sum, uint32_t new_sum, size_t len) { |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 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 | |
Lorenzo Colitti | 5a50c02 | 2014-02-10 09:20:05 +0900 | [diff] [blame^] | 351 | return tcp_translate(out, pos, tcp, header_size, old_sum, new_sum, payload, payload_size); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | /* function: udp_translate |
| 355 | * common between ipv4/ipv6 - setup checksum and send udp packet |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 356 | * out - output packet |
| 357 | * udp - udp header |
Lorenzo Colitti | 5a50c02 | 2014-02-10 09:20:05 +0900 | [diff] [blame^] | 358 | * old_sum - pseudo-header checksum of old header |
| 359 | * new_sum - pseudo-header checksum of new header |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 360 | * payload - tcp payload |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 361 | * payload_size - size of payload |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 362 | * 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] | 363 | */ |
Lorenzo Colitti | 5a50c02 | 2014-02-10 09:20:05 +0900 | [diff] [blame^] | 364 | int 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 Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 366 | struct udphdr *udp_targ = out[pos].iov_base; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 367 | |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 368 | memcpy(udp_targ, udp, sizeof(struct udphdr)); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 369 | |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 370 | out[pos].iov_len = sizeof(struct udphdr); |
Lorenzo Colitti | ee80ca6 | 2013-04-10 16:52:22 +0900 | [diff] [blame] | 371 | out[CLAT_POS_PAYLOAD].iov_base = (char *) payload; |
| 372 | out[CLAT_POS_PAYLOAD].iov_len = payload_size; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 373 | |
Lorenzo Colitti | 5a50c02 | 2014-02-10 09:20:05 +0900 | [diff] [blame^] | 374 | 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 Colitti | ee80ca6 | 2013-04-10 16:52:22 +0900 | [diff] [blame] | 390 | |
| 391 | return CLAT_POS_PAYLOAD + 1; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | /* function: tcp_translate |
| 395 | * common between ipv4/ipv6 - setup checksum and send tcp packet |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 396 | * 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 Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 400 | * payload - tcp payload |
| 401 | * payload_size - size of payload |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 402 | * 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] | 403 | * |
| 404 | * TODO: mss rewrite |
| 405 | * TODO: hosts without pmtu discovery - non DF packets will rely on fragmentation (unimplemented) |
| 406 | */ |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 407 | int tcp_translate(clat_packet out, int pos, const struct tcphdr *tcp, size_t header_size, |
Lorenzo Colitti | 5a50c02 | 2014-02-10 09:20:05 +0900 | [diff] [blame^] | 408 | uint32_t old_sum, uint32_t new_sum, const char *payload, size_t payload_size) { |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 409 | struct tcphdr *tcp_targ = out[pos].iov_base; |
| 410 | out[pos].iov_len = header_size; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 411 | |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 412 | 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 Colitti | 5cc877d | 2013-04-08 19:12:43 +0900 | [diff] [blame] | 415 | logmsg(ANDROID_LOG_ERROR, "tcp_translate: header too long %d > %d, truncating", |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 416 | header_size, MAX_TCP_HDR); |
| 417 | header_size = MAX_TCP_HDR; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 418 | } |
Lorenzo Colitti | 5cc877d | 2013-04-08 19:12:43 +0900 | [diff] [blame] | 419 | |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 420 | memcpy(tcp_targ, tcp, header_size); |
Lorenzo Colitti | 5cc877d | 2013-04-08 19:12:43 +0900 | [diff] [blame] | 421 | |
Lorenzo Colitti | ee80ca6 | 2013-04-10 16:52:22 +0900 | [diff] [blame] | 422 | out[CLAT_POS_PAYLOAD].iov_base = (char *)payload; |
| 423 | out[CLAT_POS_PAYLOAD].iov_len = payload_size; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 424 | |
Lorenzo Colitti | 5a50c02 | 2014-02-10 09:20:05 +0900 | [diff] [blame^] | 425 | tcp_targ->check = ip_checksum_adjust(tcp->check, old_sum, new_sum); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 426 | |
Lorenzo Colitti | ee80ca6 | 2013-04-10 16:52:22 +0900 | [diff] [blame] | 427 | return CLAT_POS_PAYLOAD + 1; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 428 | } |