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 | * ipv4.c - takes ipv4 packets, finds their headers, and then calls translation functions on them |
| 17 | */ |
| 18 | #include <string.h> |
| 19 | |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 20 | #include "translate.h" |
| 21 | #include "checksum.h" |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 22 | #include "logging.h" |
| 23 | #include "debug.h" |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 24 | #include "dump.h" |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 25 | |
| 26 | /* function: icmp_packet |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 27 | * translates an icmp packet |
| 28 | * out - output packet |
| 29 | * icmp - pointer to icmp header in packet |
| 30 | * checksum - pseudo-header checksum |
| 31 | * len - size of ip payload |
| 32 | * 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] | 33 | */ |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 34 | int icmp_packet(clat_packet out, int pos, const struct icmphdr *icmp, uint32_t checksum, |
| 35 | size_t len) { |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 36 | const char *payload; |
| 37 | size_t payload_size; |
| 38 | |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 39 | if(len < sizeof(struct icmphdr)) { |
| 40 | logmsg_dbg(ANDROID_LOG_ERROR, "icmp_packet/(too small)"); |
| 41 | return 0; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 42 | } |
| 43 | |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 44 | payload = (const char *) (icmp + 1); |
| 45 | payload_size = len - sizeof(struct icmphdr); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 46 | |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 47 | return icmp_to_icmp6(out, pos, icmp, checksum, payload, payload_size); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 48 | } |
| 49 | |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 50 | /* function: ipv4_packet |
| 51 | * translates an ipv4 packet |
| 52 | * out - output packet |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 53 | * packet - packet data |
| 54 | * len - size of packet |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 55 | * 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] | 56 | */ |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 57 | int ipv4_packet(clat_packet out, int pos, const char *packet, size_t len) { |
| 58 | const struct iphdr *header = (struct iphdr *) packet; |
| 59 | struct ip6_hdr *ip6_targ = (struct ip6_hdr *) out[pos].iov_base; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 60 | uint16_t frag_flags; |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 61 | uint8_t nxthdr; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 62 | const char *next_header; |
| 63 | size_t len_left; |
Lorenzo Colitti | 5a50c02 | 2014-02-10 09:20:05 +0900 | [diff] [blame] | 64 | uint32_t old_sum, new_sum; |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 65 | int iov_len; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 66 | |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 67 | if(len < sizeof(struct iphdr)) { |
| 68 | logmsg_dbg(ANDROID_LOG_ERROR, "ip_packet/too short for an ip header"); |
| 69 | return 0; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 70 | } |
| 71 | |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 72 | frag_flags = ntohs(header->frag_off); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 73 | if(frag_flags & IP_MF) { // this could theoretically be supported, but isn't |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 74 | logmsg_dbg(ANDROID_LOG_ERROR, "ip_packet/more fragments set, dropping"); |
| 75 | return 0; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 76 | } |
| 77 | |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 78 | if(header->ihl < 5) { |
| 79 | logmsg_dbg(ANDROID_LOG_ERROR, "ip_packet/ip header length set to less than 5: %x", header->ihl); |
| 80 | return 0; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 81 | } |
| 82 | |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 83 | if((size_t) header->ihl * 4 > len) { // ip header length larger than entire packet |
| 84 | logmsg_dbg(ANDROID_LOG_ERROR, "ip_packet/ip header length set too large: %x", header->ihl); |
| 85 | return 0; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 86 | } |
| 87 | |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 88 | if(header->version != 4) { |
| 89 | logmsg_dbg(ANDROID_LOG_ERROR, "ip_packet/ip header version not 4: %x", header->version); |
| 90 | return 0; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | /* rfc6145 - If any IPv4 options are present in the IPv4 packet, they MUST be |
| 94 | * ignored and the packet translated normally; there is no attempt to |
| 95 | * translate the options. |
| 96 | */ |
| 97 | |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 98 | next_header = packet + header->ihl*4; |
| 99 | len_left = len - header->ihl * 4; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 100 | |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 101 | nxthdr = header->protocol; |
| 102 | if (nxthdr == IPPROTO_ICMP) { |
| 103 | // ICMP and ICMPv6 have different protocol numbers. |
| 104 | nxthdr = IPPROTO_ICMPV6; |
| 105 | } |
| 106 | |
| 107 | /* Fill in the IPv6 header. We need to do this before we translate the packet because TCP and |
| 108 | * UDP include parts of the IP header in the checksum. Set the length to zero because we don't |
| 109 | * know it yet. |
| 110 | */ |
| 111 | fill_ip6_header(ip6_targ, 0, nxthdr, header); |
| 112 | out[pos].iov_len = sizeof(struct ip6_hdr); |
| 113 | |
| 114 | // Calculate the pseudo-header checksum. |
Lorenzo Colitti | 07f0265 | 2014-02-20 14:28:43 +0900 | [diff] [blame^] | 115 | old_sum = ipv4_pseudo_header_checksum(header, len_left); |
| 116 | new_sum = ipv6_pseudo_header_checksum(ip6_targ, len_left, nxthdr); |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 117 | |
Lorenzo Colitti | c9f4c89 | 2013-11-18 12:59:44 +0900 | [diff] [blame] | 118 | if (nxthdr == IPPROTO_ICMPV6) { |
Lorenzo Colitti | 5a50c02 | 2014-02-10 09:20:05 +0900 | [diff] [blame] | 119 | iov_len = icmp_packet(out, pos + 1, (const struct icmphdr *) next_header, new_sum, len_left); |
Lorenzo Colitti | c9f4c89 | 2013-11-18 12:59:44 +0900 | [diff] [blame] | 120 | } else if (nxthdr == IPPROTO_TCP) { |
Lorenzo Colitti | 5a50c02 | 2014-02-10 09:20:05 +0900 | [diff] [blame] | 121 | iov_len = tcp_packet(out, pos + 1, (const struct tcphdr *) next_header, old_sum, new_sum, |
| 122 | len_left); |
Lorenzo Colitti | c9f4c89 | 2013-11-18 12:59:44 +0900 | [diff] [blame] | 123 | } else if (nxthdr == IPPROTO_UDP) { |
Lorenzo Colitti | 5a50c02 | 2014-02-10 09:20:05 +0900 | [diff] [blame] | 124 | iov_len = udp_packet(out, pos + 1, (const struct udphdr *) next_header, old_sum, new_sum, |
| 125 | len_left); |
Lorenzo Colitti | c9f4c89 | 2013-11-18 12:59:44 +0900 | [diff] [blame] | 126 | } else if (nxthdr == IPPROTO_GRE) { |
| 127 | iov_len = generic_packet(out, pos + 1, next_header, len_left); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 128 | } else { |
| 129 | #if CLAT_DEBUG |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 130 | logmsg_dbg(ANDROID_LOG_ERROR, "ip_packet/unknown protocol: %x",header->protocol); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 131 | logcat_hexdump("ipv4/protocol", packet, len); |
| 132 | #endif |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 133 | return 0; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 134 | } |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 135 | |
| 136 | // Set the length. |
Lorenzo Colitti | ee80ca6 | 2013-04-10 16:52:22 +0900 | [diff] [blame] | 137 | ip6_targ->ip6_plen = htons(packet_length(out, pos)); |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 138 | return iov_len; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 139 | } |