blob: 05cd3ab116c5695f2a27e2a86e517c8b3a69cb4e [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 */
Lorenzo Colitti0cd5aa52021-12-09 15:05:52 +090018#include <arpa/inet.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050019#include <string.h>
20
Lorenzo Colitti0cd5aa52021-12-09 15:05:52 +090021#include "checksum.h"
Daniel Drowna45056e2012-03-23 10:42:54 -050022#include "config.h"
23#include "debug.h"
junyulaic4e591a2018-11-26 22:36:10 +090024#include "dump.h"
25#include "logging.h"
26#include "translate.h"
Daniel Drowna45056e2012-03-23 10:42:54 -050027
28/* function: icmp6_packet
29 * takes an icmp6 packet and sets it up for translation
Lorenzo Colittid9084182013-03-22 00:42:21 +090030 * out - output packet
31 * icmp6 - pointer to icmp6 header in packet
32 * checksum - pseudo-header checksum (unused)
33 * len - size of ip payload
34 * returns: the highest position in the output clat_packet that's filled in
Daniel Drowna45056e2012-03-23 10:42:54 -050035 */
Lorenzo Colittia4454bf2014-02-25 09:27:31 +090036int icmp6_packet(clat_packet out, clat_packet_index pos, const struct icmp6_hdr *icmp6,
37 size_t len) {
Brian Carlstromfcac4102014-02-24 20:03:01 -080038 const uint8_t *payload;
Daniel Drowna45056e2012-03-23 10:42:54 -050039 size_t payload_size;
40
junyulaic4e591a2018-11-26 22:36:10 +090041 if (len < sizeof(struct icmp6_hdr)) {
Lorenzo Colittid9084182013-03-22 00:42:21 +090042 logmsg_dbg(ANDROID_LOG_ERROR, "icmp6_packet/(too small)");
43 return 0;
Daniel Drowna45056e2012-03-23 10:42:54 -050044 }
45
junyulaic4e591a2018-11-26 22:36:10 +090046 payload = (const uint8_t *)(icmp6 + 1);
Lorenzo Colittid9084182013-03-22 00:42:21 +090047 payload_size = len - sizeof(struct icmp6_hdr);
Daniel Drowna45056e2012-03-23 10:42:54 -050048
Lorenzo Colitti9477a462013-11-18 15:56:02 +090049 return icmp6_to_icmp(out, pos, icmp6, payload, payload_size);
Daniel Drowna45056e2012-03-23 10:42:54 -050050}
51
52/* function: log_bad_address
53 * logs a bad address to android's log buffer if debugging is turned on
54 * fmt - printf-style format, use %s to place the address
55 * badaddr - the bad address in question
56 */
Daniel Drowna45056e2012-03-23 10:42:54 -050057#if CLAT_DEBUG
Lorenzo Colitti9477a462013-11-18 15:56:02 +090058void log_bad_address(const char *fmt, const struct in6_addr *src, const struct in6_addr *dst) {
Lorenzo Colitticd70b352013-04-10 12:24:56 +090059 char srcstr[INET6_ADDRSTRLEN];
60 char dststr[INET6_ADDRSTRLEN];
Daniel Drowna45056e2012-03-23 10:42:54 -050061
Lorenzo Colitticd70b352013-04-10 12:24:56 +090062 inet_ntop(AF_INET6, src, srcstr, sizeof(srcstr));
63 inet_ntop(AF_INET6, dst, dststr, sizeof(dststr));
64 logmsg_dbg(ANDROID_LOG_ERROR, fmt, srcstr, dststr);
Daniel Drowna45056e2012-03-23 10:42:54 -050065}
Lorenzo Colitti9477a462013-11-18 15:56:02 +090066#else
67#define log_bad_address(fmt, src, dst)
68#endif
Daniel Drowna45056e2012-03-23 10:42:54 -050069
70/* function: ipv6_packet
71 * takes an ipv6 packet and hands it off to the layer 4 protocol function
Lorenzo Colittid9084182013-03-22 00:42:21 +090072 * out - output packet
Daniel Drowna45056e2012-03-23 10:42:54 -050073 * packet - packet data
74 * len - size of packet
Lorenzo Colittid9084182013-03-22 00:42:21 +090075 * returns: the highest position in the output clat_packet that's filled in
Daniel Drowna45056e2012-03-23 10:42:54 -050076 */
Lorenzo Colittia4454bf2014-02-25 09:27:31 +090077int ipv6_packet(clat_packet out, clat_packet_index pos, const uint8_t *packet, size_t len) {
junyulaic4e591a2018-11-26 22:36:10 +090078 const struct ip6_hdr *ip6 = (struct ip6_hdr *)packet;
79 struct iphdr *ip_targ = (struct iphdr *)out[pos].iov_base;
Lorenzo Colitti57d480d2014-02-09 10:35:38 +090080 struct ip6_frag *frag_hdr = NULL;
Lorenzo Colittid9084182013-03-22 00:42:21 +090081 uint8_t protocol;
Brian Carlstromfcac4102014-02-24 20:03:01 -080082 const uint8_t *next_header;
Daniel Drowna45056e2012-03-23 10:42:54 -050083 size_t len_left;
Lorenzo Colitti5a50c022014-02-10 09:20:05 +090084 uint32_t old_sum, new_sum;
Lorenzo Colittid9084182013-03-22 00:42:21 +090085 int iov_len;
Daniel Drowna45056e2012-03-23 10:42:54 -050086
junyulaic4e591a2018-11-26 22:36:10 +090087 if (len < sizeof(struct ip6_hdr)) {
Lorenzo Colitticd70b352013-04-10 12:24:56 +090088 logmsg_dbg(ANDROID_LOG_ERROR, "ipv6_packet/too short for an ip6 header: %d", len);
Lorenzo Colittid9084182013-03-22 00:42:21 +090089 return 0;
Daniel Drowna45056e2012-03-23 10:42:54 -050090 }
91
junyulaic4e591a2018-11-26 22:36:10 +090092 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
Lorenzo Colitticd70b352013-04-10 12:24:56 +090093 log_bad_address("ipv6_packet/multicast %s->%s", &ip6->ip6_src, &ip6->ip6_dst);
junyulaic4e591a2018-11-26 22:36:10 +090094 return 0; // silently ignore
Daniel Drowna45056e2012-03-23 10:42:54 -050095 }
96
Lorenzo Colitticd70b352013-04-10 12:24:56 +090097 // If the packet is not from the plat subnet to the local subnet, or vice versa, drop it, unless
98 // it's an ICMP packet (which can come from anywhere). We do not send IPv6 packets from the plat
99 // subnet to the local subnet, but these can appear as inner packets in ICMP errors, so we need
100 // to translate them. We accept third-party ICMPv6 errors, even though their source addresses
101 // cannot be translated, so that things like unreachables and traceroute will work. fill_ip_header
102 // takes care of faking a source address for them.
103 if (!(is_in_plat_subnet(&ip6->ip6_src) &&
104 IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &Global_Clatd_Config.ipv6_local_subnet)) &&
105 !(is_in_plat_subnet(&ip6->ip6_dst) &&
106 IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &Global_Clatd_Config.ipv6_local_subnet)) &&
107 ip6->ip6_nxt != IPPROTO_ICMPV6) {
108 log_bad_address("ipv6_packet/wrong source address: %s->%s", &ip6->ip6_src, &ip6->ip6_dst);
Lorenzo Colittid9084182013-03-22 00:42:21 +0900109 return 0;
Daniel Drowna45056e2012-03-23 10:42:54 -0500110 }
111
Lorenzo Colittid9084182013-03-22 00:42:21 +0900112 next_header = packet + sizeof(struct ip6_hdr);
junyulaic4e591a2018-11-26 22:36:10 +0900113 len_left = len - sizeof(struct ip6_hdr);
Lorenzo Colittid9084182013-03-22 00:42:21 +0900114
115 protocol = ip6->ip6_nxt;
Lorenzo Colittid9084182013-03-22 00:42:21 +0900116
117 /* Fill in the IPv4 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_ip_header(ip_targ, 0, protocol, ip6);
122 out[pos].iov_len = sizeof(struct iphdr);
123
Lorenzo Colitti57d480d2014-02-09 10:35:38 +0900124 // If there's a Fragment header, parse it and decide what the next header is.
125 // Do this before calculating the pseudo-header checksum because it updates the next header value.
126 if (protocol == IPPROTO_FRAGMENT) {
junyulaic4e591a2018-11-26 22:36:10 +0900127 frag_hdr = (struct ip6_frag *)next_header;
Lorenzo Colitti57d480d2014-02-09 10:35:38 +0900128 if (len_left < sizeof(*frag_hdr)) {
129 logmsg_dbg(ANDROID_LOG_ERROR, "ipv6_packet/too short for fragment header: %d", len);
130 return 0;
131 }
132
133 next_header += sizeof(*frag_hdr);
134 len_left -= sizeof(*frag_hdr);
135
136 protocol = parse_frag_header(frag_hdr, ip_targ);
137 }
138
139 // ICMP and ICMPv6 have different protocol numbers.
140 if (protocol == IPPROTO_ICMPV6) {
junyulaic4e591a2018-11-26 22:36:10 +0900141 protocol = IPPROTO_ICMP;
Lorenzo Colitti57d480d2014-02-09 10:35:38 +0900142 ip_targ->protocol = IPPROTO_ICMP;
143 }
144
145 /* Calculate the pseudo-header checksum.
146 * Technically, the length that is used in the pseudo-header checksum is the transport layer
147 * length, which is not the same as len_left in the case of fragmented packets. But since
148 * translation does not change the transport layer length, the checksum is unaffected.
149 */
Lorenzo Colitti07f02652014-02-20 14:28:43 +0900150 old_sum = ipv6_pseudo_header_checksum(ip6, len_left, protocol);
151 new_sum = ipv4_pseudo_header_checksum(ip_targ, len_left);
Lorenzo Colittid9084182013-03-22 00:42:21 +0900152
Lorenzo Colitti57d480d2014-02-09 10:35:38 +0900153 // Does not support IPv6 extension headers except Fragment.
154 if (frag_hdr && (frag_hdr->ip6f_offlg & IP6F_OFF_MASK)) {
155 iov_len = generic_packet(out, pos + 2, next_header, len_left);
156 } else if (protocol == IPPROTO_ICMP) {
junyulaic4e591a2018-11-26 22:36:10 +0900157 iov_len = icmp6_packet(out, pos + 2, (const struct icmp6_hdr *)next_header, len_left);
Lorenzo Colitti57d480d2014-02-09 10:35:38 +0900158 } else if (protocol == IPPROTO_TCP) {
junyulaic4e591a2018-11-26 22:36:10 +0900159 iov_len =
160 tcp_packet(out, pos + 2, (const struct tcphdr *)next_header, old_sum, new_sum, len_left);
Lorenzo Colitti57d480d2014-02-09 10:35:38 +0900161 } else if (protocol == IPPROTO_UDP) {
junyulaic4e591a2018-11-26 22:36:10 +0900162 iov_len =
163 udp_packet(out, pos + 2, (const struct udphdr *)next_header, old_sum, new_sum, len_left);
Maciej Żenczykowski5f689822019-08-20 17:48:48 -0700164 } else if (protocol == IPPROTO_GRE || protocol == IPPROTO_ESP) {
Lorenzo Colitti57d480d2014-02-09 10:35:38 +0900165 iov_len = generic_packet(out, pos + 2, next_header, len_left);
Daniel Drowna45056e2012-03-23 10:42:54 -0500166 } else {
167#if CLAT_DEBUG
Lorenzo Colitticd70b352013-04-10 12:24:56 +0900168 logmsg(ANDROID_LOG_ERROR, "ipv6_packet/unknown next header type: %x", ip6->ip6_nxt);
Daniel Drowna45056e2012-03-23 10:42:54 -0500169 logcat_hexdump("ipv6/nxthdr", packet, len);
170#endif
Lorenzo Colittid9084182013-03-22 00:42:21 +0900171 return 0;
Daniel Drowna45056e2012-03-23 10:42:54 -0500172 }
Lorenzo Colittid9084182013-03-22 00:42:21 +0900173
174 // Set the length and calculate the checksum.
Lorenzo Colittiee80ca62013-04-10 16:52:22 +0900175 ip_targ->tot_len = htons(ntohs(ip_targ->tot_len) + packet_length(out, pos));
junyulaic4e591a2018-11-26 22:36:10 +0900176 ip_targ->check = ip_checksum(ip_targ, sizeof(struct iphdr));
Lorenzo Colittid9084182013-03-22 00:42:21 +0900177 return iov_len;
Daniel Drowna45056e2012-03-23 10:42:54 -0500178}