blob: 89e47e475fb005003a2676316ae8e2f05da67046 [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 * ipv4.c - takes ipv4 packets, finds their headers, and then calls translation functions on them
17 */
18#include <string.h>
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
29#include "translate.h"
30#include "checksum.h"
31#include "ipv4.h"
32#include "logging.h"
33#include "debug.h"
Lorenzo Colittid9084182013-03-22 00:42:21 +090034#include "dump.h"
Daniel Drowna45056e2012-03-23 10:42:54 -050035
36/* function: icmp_packet
Lorenzo Colittid9084182013-03-22 00:42:21 +090037 * translates an icmp packet
38 * out - output packet
39 * icmp - pointer to icmp header in packet
40 * checksum - pseudo-header checksum
41 * len - size of ip payload
42 * returns: the highest position in the output clat_packet that's filled in
Daniel Drowna45056e2012-03-23 10:42:54 -050043 */
Lorenzo Colittid9084182013-03-22 00:42:21 +090044int icmp_packet(clat_packet out, int pos, const struct icmphdr *icmp, uint32_t checksum,
45 size_t len) {
Daniel Drowna45056e2012-03-23 10:42:54 -050046 const char *payload;
47 size_t payload_size;
48
Lorenzo Colittid9084182013-03-22 00:42:21 +090049 if(len < sizeof(struct icmphdr)) {
50 logmsg_dbg(ANDROID_LOG_ERROR, "icmp_packet/(too small)");
51 return 0;
Daniel Drowna45056e2012-03-23 10:42:54 -050052 }
53
Lorenzo Colittid9084182013-03-22 00:42:21 +090054 payload = (const char *) (icmp + 1);
55 payload_size = len - sizeof(struct icmphdr);
Daniel Drowna45056e2012-03-23 10:42:54 -050056
Lorenzo Colittid9084182013-03-22 00:42:21 +090057 return icmp_to_icmp6(out, pos, icmp, checksum, payload, payload_size);
Daniel Drowna45056e2012-03-23 10:42:54 -050058}
59
Lorenzo Colittid9084182013-03-22 00:42:21 +090060/* function: ipv4_packet
61 * translates an ipv4 packet
62 * out - output packet
Daniel Drowna45056e2012-03-23 10:42:54 -050063 * packet - packet data
64 * len - size of packet
Lorenzo Colittid9084182013-03-22 00:42:21 +090065 * returns: the highest position in the output clat_packet that's filled in
Daniel Drowna45056e2012-03-23 10:42:54 -050066 */
Lorenzo Colittid9084182013-03-22 00:42:21 +090067int ipv4_packet(clat_packet out, int pos, const char *packet, size_t len) {
68 const struct iphdr *header = (struct iphdr *) packet;
69 struct ip6_hdr *ip6_targ = (struct ip6_hdr *) out[pos].iov_base;
Daniel Drowna45056e2012-03-23 10:42:54 -050070 uint16_t frag_flags;
Lorenzo Colittid9084182013-03-22 00:42:21 +090071 uint8_t nxthdr;
Daniel Drowna45056e2012-03-23 10:42:54 -050072 const char *next_header;
73 size_t len_left;
Lorenzo Colittid9084182013-03-22 00:42:21 +090074 uint32_t checksum;
75 int iov_len;
Daniel Drowna45056e2012-03-23 10:42:54 -050076
Lorenzo Colittid9084182013-03-22 00:42:21 +090077 if(len < sizeof(struct iphdr)) {
78 logmsg_dbg(ANDROID_LOG_ERROR, "ip_packet/too short for an ip header");
79 return 0;
Daniel Drowna45056e2012-03-23 10:42:54 -050080 }
81
Lorenzo Colittid9084182013-03-22 00:42:21 +090082 frag_flags = ntohs(header->frag_off);
Daniel Drowna45056e2012-03-23 10:42:54 -050083 if(frag_flags & IP_MF) { // this could theoretically be supported, but isn't
Lorenzo Colittid9084182013-03-22 00:42:21 +090084 logmsg_dbg(ANDROID_LOG_ERROR, "ip_packet/more fragments set, dropping");
85 return 0;
Daniel Drowna45056e2012-03-23 10:42:54 -050086 }
87
Lorenzo Colittid9084182013-03-22 00:42:21 +090088 if(header->ihl < 5) {
89 logmsg_dbg(ANDROID_LOG_ERROR, "ip_packet/ip header length set to less than 5: %x", header->ihl);
90 return 0;
Daniel Drowna45056e2012-03-23 10:42:54 -050091 }
92
Lorenzo Colittid9084182013-03-22 00:42:21 +090093 if((size_t) header->ihl * 4 > len) { // ip header length larger than entire packet
94 logmsg_dbg(ANDROID_LOG_ERROR, "ip_packet/ip header length set too large: %x", header->ihl);
95 return 0;
Daniel Drowna45056e2012-03-23 10:42:54 -050096 }
97
Lorenzo Colittid9084182013-03-22 00:42:21 +090098 if(header->version != 4) {
99 logmsg_dbg(ANDROID_LOG_ERROR, "ip_packet/ip header version not 4: %x", header->version);
100 return 0;
Daniel Drowna45056e2012-03-23 10:42:54 -0500101 }
102
103 /* rfc6145 - If any IPv4 options are present in the IPv4 packet, they MUST be
104 * ignored and the packet translated normally; there is no attempt to
105 * translate the options.
106 */
107
Lorenzo Colittid9084182013-03-22 00:42:21 +0900108 next_header = packet + header->ihl*4;
109 len_left = len - header->ihl * 4;
Daniel Drowna45056e2012-03-23 10:42:54 -0500110
Lorenzo Colittid9084182013-03-22 00:42:21 +0900111 nxthdr = header->protocol;
112 if (nxthdr == IPPROTO_ICMP) {
113 // ICMP and ICMPv6 have different protocol numbers.
114 nxthdr = IPPROTO_ICMPV6;
115 }
116
117 /* Fill in the IPv6 header. We need to do this before we translate the packet because TCP and
118 * UDP include parts of the IP header in the checksum. Set the length to zero because we don't
119 * know it yet.
120 */
121 fill_ip6_header(ip6_targ, 0, nxthdr, header);
122 out[pos].iov_len = sizeof(struct ip6_hdr);
123
124 // Calculate the pseudo-header checksum.
125 checksum = ipv6_pseudo_header_checksum(0, ip6_targ, len_left);
126
127 if(nxthdr == IPPROTO_ICMPV6) {
128 iov_len = icmp_packet(out, pos + 1, (const struct icmphdr *) next_header, checksum, len_left);
129 } else if(nxthdr == IPPROTO_TCP) {
130 iov_len = tcp_packet(out, pos + 1, (const struct tcphdr *) next_header, checksum, len_left);
131 } else if(nxthdr == IPPROTO_UDP) {
132 iov_len = udp_packet(out, pos + 1, (const struct udphdr *) next_header, checksum, len_left);
Daniel Drowna45056e2012-03-23 10:42:54 -0500133 } else {
134#if CLAT_DEBUG
Lorenzo Colittid9084182013-03-22 00:42:21 +0900135 logmsg_dbg(ANDROID_LOG_ERROR, "ip_packet/unknown protocol: %x",header->protocol);
Daniel Drowna45056e2012-03-23 10:42:54 -0500136 logcat_hexdump("ipv4/protocol", packet, len);
137#endif
Lorenzo Colittid9084182013-03-22 00:42:21 +0900138 return 0;
Daniel Drowna45056e2012-03-23 10:42:54 -0500139 }
Lorenzo Colittid9084182013-03-22 00:42:21 +0900140
141 // Set the length.
Lorenzo Colittiee80ca62013-04-10 16:52:22 +0900142 ip6_targ->ip6_plen = htons(packet_length(out, pos));
Lorenzo Colittid9084182013-03-22 00:42:21 +0900143 return iov_len;
Daniel Drowna45056e2012-03-23 10:42:54 -0500144}