blob: 8011ce9f206d8194f01ff40af4970b0fff826ee5 [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 * ipv6.c - takes ipv6 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>
Lorenzo Colittid9084182013-03-22 00:42:21 +090028#include <arpa/inet.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050029
30#include "translate.h"
31#include "checksum.h"
32#include "ipv6.h"
33#include "logging.h"
34#include "dump.h"
35#include "config.h"
36#include "debug.h"
37
38/* function: icmp6_packet
39 * takes an icmp6 packet and sets it up for translation
Lorenzo Colittid9084182013-03-22 00:42:21 +090040 * out - output packet
41 * icmp6 - pointer to icmp6 header in packet
42 * checksum - pseudo-header checksum (unused)
43 * len - size of ip payload
44 * returns: the highest position in the output clat_packet that's filled in
Daniel Drowna45056e2012-03-23 10:42:54 -050045 */
Lorenzo Colittid9084182013-03-22 00:42:21 +090046int icmp6_packet(clat_packet out, int pos, const struct icmp6_hdr *icmp6, uint32_t checksum,
47 size_t len) {
Daniel Drowna45056e2012-03-23 10:42:54 -050048 const char *payload;
49 size_t payload_size;
50
Lorenzo Colittid9084182013-03-22 00:42:21 +090051 if(len < sizeof(struct icmp6_hdr)) {
52 logmsg_dbg(ANDROID_LOG_ERROR, "icmp6_packet/(too small)");
53 return 0;
Daniel Drowna45056e2012-03-23 10:42:54 -050054 }
55
Lorenzo Colittid9084182013-03-22 00:42:21 +090056 payload = (const char *) (icmp6 + 1);
57 payload_size = len - sizeof(struct icmp6_hdr);
Daniel Drowna45056e2012-03-23 10:42:54 -050058
Lorenzo Colittid9084182013-03-22 00:42:21 +090059 return icmp6_to_icmp(out, pos, icmp6, checksum, payload, payload_size);
Daniel Drowna45056e2012-03-23 10:42:54 -050060}
61
62/* function: log_bad_address
63 * logs a bad address to android's log buffer if debugging is turned on
64 * fmt - printf-style format, use %s to place the address
65 * badaddr - the bad address in question
66 */
67void log_bad_address(const char *fmt, const struct in6_addr *badaddr) {
68#if CLAT_DEBUG
69 char badaddr_str[INET6_ADDRSTRLEN];
70
71 inet_ntop(AF_INET6, badaddr, badaddr_str, sizeof(badaddr_str));
72 logmsg_dbg(ANDROID_LOG_ERROR,fmt,badaddr_str);
73#endif
74}
75
76/* function: ipv6_packet
77 * takes an ipv6 packet and hands it off to the layer 4 protocol function
Lorenzo Colittid9084182013-03-22 00:42:21 +090078 * out - output packet
Daniel Drowna45056e2012-03-23 10:42:54 -050079 * packet - packet data
80 * len - size of packet
Lorenzo Colittid9084182013-03-22 00:42:21 +090081 * returns: the highest position in the output clat_packet that's filled in
Daniel Drowna45056e2012-03-23 10:42:54 -050082 */
Lorenzo Colittid9084182013-03-22 00:42:21 +090083int ipv6_packet(clat_packet out, int pos, const char *packet, size_t len) {
84 const struct ip6_hdr *ip6 = (struct ip6_hdr *) packet;
85 struct iphdr *ip_targ = (struct iphdr *) out[pos].iov_base;
86 uint8_t protocol;
Daniel Drowna45056e2012-03-23 10:42:54 -050087 const char *next_header;
88 size_t len_left;
Lorenzo Colittid9084182013-03-22 00:42:21 +090089 uint32_t checksum;
90 int iov_len;
Daniel Drowna45056e2012-03-23 10:42:54 -050091 int i;
92
Lorenzo Colittid9084182013-03-22 00:42:21 +090093 if(len < sizeof(struct ip6_hdr)) {
94 logmsg_dbg(ANDROID_LOG_ERROR, "ipv6_packet/too short for an ip6 header");
95 return 0;
Daniel Drowna45056e2012-03-23 10:42:54 -050096 }
97
Lorenzo Colittid9084182013-03-22 00:42:21 +090098 if(IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
99 log_bad_address("ipv6_packet/multicast %s", &ip6->ip6_dst);
100 return 0; // silently ignore
Daniel Drowna45056e2012-03-23 10:42:54 -0500101 }
102
103 for(i = 0; i < 3; i++) {
Lorenzo Colittid9084182013-03-22 00:42:21 +0900104 if(ip6->ip6_src.s6_addr32[i] != Global_Clatd_Config.plat_subnet.s6_addr32[i]) {
105 log_bad_address("ipv6_packet/wrong source address: %s", &ip6->ip6_src);
106 return 0;
Daniel Drowna45056e2012-03-23 10:42:54 -0500107 }
108 }
Lorenzo Colittid9084182013-03-22 00:42:21 +0900109 if(!IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &Global_Clatd_Config.ipv6_local_subnet)) {
110 log_bad_address("ipv6_packet/wrong destination address: %s", &ip6->ip6_dst);
111 return 0;
Daniel Drowna45056e2012-03-23 10:42:54 -0500112 }
113
Lorenzo Colittid9084182013-03-22 00:42:21 +0900114 next_header = packet + sizeof(struct ip6_hdr);
115 len_left = len - sizeof(struct ip6_hdr);
116
117 protocol = ip6->ip6_nxt;
118 if (protocol == IPPROTO_ICMPV6) {
119 // ICMP and ICMPv6 have different protocol numbers.
120 protocol = IPPROTO_ICMP;
121 }
122
123 /* Fill in the IPv4 header. We need to do this before we translate the packet because TCP and
124 * UDP include parts of the IP header in the checksum. Set the length to zero because we don't
125 * know it yet.
126 */
127 fill_ip_header(ip_targ, 0, protocol, ip6);
128 out[pos].iov_len = sizeof(struct iphdr);
129
130 // Calculate the pseudo-header checksum.
131 checksum = ipv4_pseudo_header_checksum(0, ip_targ, len_left);
132
Daniel Drowna45056e2012-03-23 10:42:54 -0500133 // does not support IPv6 extension headers, this will drop any packet with them
Lorenzo Colittid9084182013-03-22 00:42:21 +0900134 if(protocol == IPPROTO_ICMP) {
135 iov_len = icmp6_packet(out, pos + 1, (const struct icmp6_hdr *) next_header, checksum,
136 len_left);
137 } else if(ip6->ip6_nxt == IPPROTO_TCP) {
138 iov_len = tcp_packet(out, pos + 1, (const struct tcphdr *) next_header, checksum,
139 len_left);
140 } else if(ip6->ip6_nxt == IPPROTO_UDP) {
141 iov_len = udp_packet(out, pos + 1, (const struct udphdr *) next_header, checksum,
142 len_left);
Daniel Drowna45056e2012-03-23 10:42:54 -0500143 } else {
144#if CLAT_DEBUG
Lorenzo Colittid9084182013-03-22 00:42:21 +0900145 logmsg(ANDROID_LOG_ERROR, "ipv6_packet/unknown next header type: %x",ip6->ip6_nxt);
Daniel Drowna45056e2012-03-23 10:42:54 -0500146 logcat_hexdump("ipv6/nxthdr", packet, len);
147#endif
Lorenzo Colittid9084182013-03-22 00:42:21 +0900148 return 0;
Daniel Drowna45056e2012-03-23 10:42:54 -0500149 }
Lorenzo Colittid9084182013-03-22 00:42:21 +0900150
151 // Set the length and calculate the checksum.
152 ip_targ->tot_len = htons(ntohs(ip_targ->tot_len) + payload_length(out, pos));
153 ip_targ->check = ip_checksum(ip_targ, sizeof(struct iphdr));
154 return iov_len;
Daniel Drowna45056e2012-03-23 10:42:54 -0500155}