blob: 8fc36a49c017711be2348df1430c0ebc509acdf6 [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>
28
29#include "translate.h"
30#include "checksum.h"
31#include "ipv6.h"
32#include "logging.h"
33#include "dump.h"
34#include "config.h"
35#include "debug.h"
36
37/* function: icmp6_packet
38 * takes an icmp6 packet and sets it up for translation
39 * fd - tun interface fd
40 * packet - ip payload
41 * len - size of ip payload
42 * ip6 - ip6 header
43 */
44void icmp6_packet(int fd, const char *packet, size_t len, struct ip6_hdr *ip6) {
45 struct icmp6_hdr icmp6;
46 const char *payload;
47 size_t payload_size;
48
49 if(len < sizeof(icmp6)) {
50 logmsg_dbg(ANDROID_LOG_ERROR,"icmp6_packet/(too small)");
51 return;
52 }
53
54 memcpy(&icmp6, packet, sizeof(icmp6));
55 payload = packet + sizeof(icmp6);
56 payload_size = len - sizeof(icmp6);
57
58 icmp6_to_icmp(fd, ip6, &icmp6, payload, payload_size);
59}
60
61/* function: tcp6_packet
62 * takes a tcp packet and sets it up for translation
63 * fd - tun interface fd
64 * packet - ip payload
65 * len - size of ip payload
66 * ip6 - ip6 header
67 */
68void tcp6_packet(int fd, const char *packet, size_t len, struct ip6_hdr *ip6) {
69 struct tcphdr tcp;
70 const char *payload;
71 const char *options;
72 size_t payload_size, options_size;
73
74 if(len < sizeof(tcp)) {
75 logmsg_dbg(ANDROID_LOG_ERROR,"tcp6_packet/(too small)");
76 return;
77 }
78
79 memcpy(&tcp, packet, sizeof(tcp));
80
81 if(tcp.doff < 5) {
82 logmsg_dbg(ANDROID_LOG_ERROR,"tcp6_packet/tcp header length set to less than 5: %x",tcp.doff);
83 return;
84 }
85
86 if((size_t)tcp.doff*4 > len) {
87 logmsg_dbg(ANDROID_LOG_ERROR,"tcp6_packet/tcp header length set too large: %x",tcp.doff);
88 return;
89 }
90
91 if(tcp.doff > 5) {
92 options = packet + sizeof(tcp);
93 options_size = tcp.doff*4 - sizeof(tcp);
94 } else {
95 options = NULL;
96 options_size = 0;
97 }
98
99 payload = packet + tcp.doff*4;
100 payload_size = len - tcp.doff*4;
101
102 tcp6_to_tcp(fd,ip6,&tcp,payload,payload_size,options,options_size);
103}
104
105/* function: udp6_packet
106 * takes a udp packet and sets it up for translation
107 * fd - tun interface fd
108 * packet - ip payload
109 * len - size of ip payload
110 * ip6 - ip6 header
111 */
112void udp6_packet(int fd, const char *packet, size_t len, struct ip6_hdr *ip6) {
113 struct udphdr udp;
114 const char *payload;
115 size_t payload_size;
116
117 if(len < sizeof(udp)) {
118 logmsg_dbg(ANDROID_LOG_ERROR,"udp6_packet/(too small)");
119 return;
120 }
121
122 memcpy(&udp, packet, sizeof(udp));
123 payload = packet + sizeof(udp);
124 payload_size = len - sizeof(udp);
125
126 udp6_to_udp(fd,ip6,&udp,payload,payload_size);
127}
128
129/* function: log_bad_address
130 * logs a bad address to android's log buffer if debugging is turned on
131 * fmt - printf-style format, use %s to place the address
132 * badaddr - the bad address in question
133 */
134void log_bad_address(const char *fmt, const struct in6_addr *badaddr) {
135#if CLAT_DEBUG
136 char badaddr_str[INET6_ADDRSTRLEN];
137
138 inet_ntop(AF_INET6, badaddr, badaddr_str, sizeof(badaddr_str));
139 logmsg_dbg(ANDROID_LOG_ERROR,fmt,badaddr_str);
140#endif
141}
142
143/* function: ipv6_packet
144 * takes an ipv6 packet and hands it off to the layer 4 protocol function
145 * fd - tun interface fd
146 * packet - packet data
147 * len - size of packet
148 */
149void ipv6_packet(int fd, const char *packet, size_t len) {
150 struct ip6_hdr header;
151 const char *next_header;
152 size_t len_left;
153 int i;
154
155 if(len < sizeof(header)) {
156 logmsg_dbg(ANDROID_LOG_ERROR,"ipv6_packet/too short for an ip6 header");
157 return;
158 }
159
160 memcpy(&header, packet, sizeof(header));
161
162 next_header = packet + sizeof(header);
163 len_left = len - sizeof(header);
164
165 if(IN6_IS_ADDR_MULTICAST(&header.ip6_dst)) {
166 log_bad_address("ipv6_packet/multicast %s", &header.ip6_dst);
167 return; // silently ignore
168 }
169
170 for(i = 0; i < 3; i++) {
171 if(header.ip6_src.s6_addr32[i] != Global_Clatd_Config.plat_subnet.s6_addr32[i]) {
172 log_bad_address("ipv6_packet/wrong source address: %s", &header.ip6_src);
173 return;
174 }
175 }
176 if(!IN6_ARE_ADDR_EQUAL(&header.ip6_dst, &Global_Clatd_Config.ipv6_local_subnet)) {
177 log_bad_address("ipv6_packet/wrong destination address: %s", &header.ip6_dst);
178 return;
179 }
180
181 // does not support IPv6 extension headers, this will drop any packet with them
182 if(header.ip6_nxt == IPPROTO_ICMPV6) {
183 icmp6_packet(fd,next_header,len_left,&header);
184 } else if(header.ip6_nxt == IPPROTO_TCP) {
185 tcp6_packet(fd,next_header,len_left,&header);
186 } else if(header.ip6_nxt == IPPROTO_UDP) {
187 udp6_packet(fd,next_header,len_left,&header);
188 } else {
189#if CLAT_DEBUG
190 logmsg(ANDROID_LOG_ERROR,"ipv6_packet/unknown next header type: %x",header.ip6_nxt);
191 logcat_hexdump("ipv6/nxthdr", packet, len);
192#endif
193 }
194}