blob: 2be02e34c2da58335ce66a384297be5fe6f1cf6b [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
Lorenzo Colitti0cd5aa52021-12-09 15:05:52 +090020#include "checksum.h"
Daniel Drowna45056e2012-03-23 10:42:54 -050021#include "debug.h"
Lorenzo Colittid9084182013-03-22 00:42:21 +090022#include "dump.h"
junyulaic4e591a2018-11-26 22:36:10 +090023#include "logging.h"
24#include "translate.h"
Daniel Drowna45056e2012-03-23 10:42:54 -050025
26/* function: icmp_packet
Lorenzo Colittid9084182013-03-22 00:42:21 +090027 * 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 Drowna45056e2012-03-23 10:42:54 -050033 */
Lorenzo Colittia4454bf2014-02-25 09:27:31 +090034int icmp_packet(clat_packet out, clat_packet_index pos, const struct icmphdr *icmp,
35 uint32_t checksum, size_t len) {
Brian Carlstromfcac4102014-02-24 20:03:01 -080036 const uint8_t *payload;
Daniel Drowna45056e2012-03-23 10:42:54 -050037 size_t payload_size;
38
junyulaic4e591a2018-11-26 22:36:10 +090039 if (len < sizeof(struct icmphdr)) {
Lorenzo Colittid9084182013-03-22 00:42:21 +090040 logmsg_dbg(ANDROID_LOG_ERROR, "icmp_packet/(too small)");
41 return 0;
Daniel Drowna45056e2012-03-23 10:42:54 -050042 }
43
junyulaic4e591a2018-11-26 22:36:10 +090044 payload = (const uint8_t *)(icmp + 1);
Lorenzo Colittid9084182013-03-22 00:42:21 +090045 payload_size = len - sizeof(struct icmphdr);
Daniel Drowna45056e2012-03-23 10:42:54 -050046
Lorenzo Colittid9084182013-03-22 00:42:21 +090047 return icmp_to_icmp6(out, pos, icmp, checksum, payload, payload_size);
Daniel Drowna45056e2012-03-23 10:42:54 -050048}
49
Lorenzo Colittid9084182013-03-22 00:42:21 +090050/* function: ipv4_packet
51 * translates an ipv4 packet
52 * out - output packet
Daniel Drowna45056e2012-03-23 10:42:54 -050053 * packet - packet data
54 * len - size of packet
Lorenzo Colittid9084182013-03-22 00:42:21 +090055 * returns: the highest position in the output clat_packet that's filled in
Daniel Drowna45056e2012-03-23 10:42:54 -050056 */
Lorenzo Colittia4454bf2014-02-25 09:27:31 +090057int ipv4_packet(clat_packet out, clat_packet_index pos, const uint8_t *packet, size_t len) {
junyulaic4e591a2018-11-26 22:36:10 +090058 const struct iphdr *header = (struct iphdr *)packet;
59 struct ip6_hdr *ip6_targ = (struct ip6_hdr *)out[pos].iov_base;
Lorenzo Colitti57d480d2014-02-09 10:35:38 +090060 struct ip6_frag *frag_hdr;
61 size_t frag_hdr_len;
Lorenzo Colittid9084182013-03-22 00:42:21 +090062 uint8_t nxthdr;
Brian Carlstromfcac4102014-02-24 20:03:01 -080063 const uint8_t *next_header;
Daniel Drowna45056e2012-03-23 10:42:54 -050064 size_t len_left;
Lorenzo Colitti5a50c022014-02-10 09:20:05 +090065 uint32_t old_sum, new_sum;
Lorenzo Colittid9084182013-03-22 00:42:21 +090066 int iov_len;
Daniel Drowna45056e2012-03-23 10:42:54 -050067
junyulaic4e591a2018-11-26 22:36:10 +090068 if (len < sizeof(struct iphdr)) {
Lorenzo Colittid9084182013-03-22 00:42:21 +090069 logmsg_dbg(ANDROID_LOG_ERROR, "ip_packet/too short for an ip header");
70 return 0;
Daniel Drowna45056e2012-03-23 10:42:54 -050071 }
72
junyulaic4e591a2018-11-26 22:36:10 +090073 if (header->ihl < 5) {
Lorenzo Colittid9084182013-03-22 00:42:21 +090074 logmsg_dbg(ANDROID_LOG_ERROR, "ip_packet/ip header length set to less than 5: %x", header->ihl);
75 return 0;
Daniel Drowna45056e2012-03-23 10:42:54 -050076 }
77
junyulaic4e591a2018-11-26 22:36:10 +090078 if ((size_t)header->ihl * 4 > len) { // ip header length larger than entire packet
Lorenzo Colittid9084182013-03-22 00:42:21 +090079 logmsg_dbg(ANDROID_LOG_ERROR, "ip_packet/ip header length set too large: %x", header->ihl);
80 return 0;
Daniel Drowna45056e2012-03-23 10:42:54 -050081 }
82
junyulaic4e591a2018-11-26 22:36:10 +090083 if (header->version != 4) {
Lorenzo Colittid9084182013-03-22 00:42:21 +090084 logmsg_dbg(ANDROID_LOG_ERROR, "ip_packet/ip header version not 4: %x", header->version);
85 return 0;
Daniel Drowna45056e2012-03-23 10:42:54 -050086 }
87
88 /* rfc6145 - If any IPv4 options are present in the IPv4 packet, they MUST be
89 * ignored and the packet translated normally; there is no attempt to
90 * translate the options.
91 */
92
junyulaic4e591a2018-11-26 22:36:10 +090093 next_header = packet + header->ihl * 4;
94 len_left = len - header->ihl * 4;
Daniel Drowna45056e2012-03-23 10:42:54 -050095
Lorenzo Colittid9084182013-03-22 00:42:21 +090096 nxthdr = header->protocol;
97 if (nxthdr == IPPROTO_ICMP) {
98 // ICMP and ICMPv6 have different protocol numbers.
99 nxthdr = IPPROTO_ICMPV6;
100 }
101
102 /* Fill in the IPv6 header. We need to do this before we translate the packet because TCP and
103 * UDP include parts of the IP header in the checksum. Set the length to zero because we don't
104 * know it yet.
105 */
106 fill_ip6_header(ip6_targ, 0, nxthdr, header);
107 out[pos].iov_len = sizeof(struct ip6_hdr);
108
Lorenzo Colitti57d480d2014-02-09 10:35:38 +0900109 /* Calculate the pseudo-header checksum.
110 * Technically, the length that is used in the pseudo-header checksum is the transport layer
111 * length, which is not the same as len_left in the case of fragmented packets. But since
112 * translation does not change the transport layer length, the checksum is unaffected.
113 */
Lorenzo Colitti07f02652014-02-20 14:28:43 +0900114 old_sum = ipv4_pseudo_header_checksum(header, len_left);
115 new_sum = ipv6_pseudo_header_checksum(ip6_targ, len_left, nxthdr);
Lorenzo Colittid9084182013-03-22 00:42:21 +0900116
Lorenzo Colitti57d480d2014-02-09 10:35:38 +0900117 // If the IPv4 packet is fragmented, add a Fragment header.
junyulaic4e591a2018-11-26 22:36:10 +0900118 frag_hdr = (struct ip6_frag *)out[pos + 1].iov_base;
119 frag_hdr_len = maybe_fill_frag_header(frag_hdr, ip6_targ, header);
Lorenzo Colitti57d480d2014-02-09 10:35:38 +0900120 out[pos + 1].iov_len = frag_hdr_len;
121
122 if (frag_hdr_len && frag_hdr->ip6f_offlg & IP6F_OFF_MASK) {
123 // Non-first fragment. Copy the rest of the packet as is.
124 iov_len = generic_packet(out, pos + 2, next_header, len_left);
125 } else if (nxthdr == IPPROTO_ICMPV6) {
junyulaic4e591a2018-11-26 22:36:10 +0900126 iov_len = icmp_packet(out, pos + 2, (const struct icmphdr *)next_header, new_sum, len_left);
Lorenzo Colittic9f4c892013-11-18 12:59:44 +0900127 } else if (nxthdr == IPPROTO_TCP) {
junyulaic4e591a2018-11-26 22:36:10 +0900128 iov_len =
129 tcp_packet(out, pos + 2, (const struct tcphdr *)next_header, old_sum, new_sum, len_left);
Lorenzo Colittic9f4c892013-11-18 12:59:44 +0900130 } else if (nxthdr == IPPROTO_UDP) {
junyulaic4e591a2018-11-26 22:36:10 +0900131 iov_len =
132 udp_packet(out, pos + 2, (const struct udphdr *)next_header, old_sum, new_sum, len_left);
Maciej Żenczykowski5f689822019-08-20 17:48:48 -0700133 } else if (nxthdr == IPPROTO_GRE || nxthdr == IPPROTO_ESP) {
Lorenzo Colitti57d480d2014-02-09 10:35:38 +0900134 iov_len = generic_packet(out, pos + 2, next_header, len_left);
Daniel Drowna45056e2012-03-23 10:42:54 -0500135 } else {
136#if CLAT_DEBUG
junyulaic4e591a2018-11-26 22:36:10 +0900137 logmsg_dbg(ANDROID_LOG_ERROR, "ip_packet/unknown protocol: %x", header->protocol);
Daniel Drowna45056e2012-03-23 10:42:54 -0500138 logcat_hexdump("ipv4/protocol", packet, len);
139#endif
Lorenzo Colittid9084182013-03-22 00:42:21 +0900140 return 0;
Daniel Drowna45056e2012-03-23 10:42:54 -0500141 }
Lorenzo Colittid9084182013-03-22 00:42:21 +0900142
143 // Set the length.
Lorenzo Colittiee80ca62013-04-10 16:52:22 +0900144 ip6_targ->ip6_plen = htons(packet_length(out, pos));
Lorenzo Colittid9084182013-03-22 00:42:21 +0900145 return iov_len;
Daniel Drowna45056e2012-03-23 10:42:54 -0500146}