1 /*
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 "checksum.h"
21 #include "debug.h"
22 #include "dump.h"
23 #include "logging.h"
24 #include "translate.h"
25
26 /* function: icmp_packet
27 * 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
33 */
icmp_packet(clat_packet out,clat_packet_index pos,const struct icmphdr * icmp,uint32_t checksum,size_t len)34 int icmp_packet(clat_packet out, clat_packet_index pos, const struct icmphdr *icmp,
35 uint32_t checksum, size_t len) {
36 const uint8_t *payload;
37 size_t payload_size;
38
39 if (len < sizeof(struct icmphdr)) {
40 logmsg_dbg(ANDROID_LOG_ERROR, "icmp_packet/(too small)");
41 return 0;
42 }
43
44 payload = (const uint8_t *)(icmp + 1);
45 payload_size = len - sizeof(struct icmphdr);
46
47 return icmp_to_icmp6(out, pos, icmp, checksum, payload, payload_size);
48 }
49
50 /* function: ipv4_packet
51 * translates an ipv4 packet
52 * out - output packet
53 * packet - packet data
54 * len - size of packet
55 * returns: the highest position in the output clat_packet that's filled in
56 */
ipv4_packet(clat_packet out,clat_packet_index pos,const uint8_t * packet,size_t len)57 int ipv4_packet(clat_packet out, clat_packet_index pos, const uint8_t *packet, size_t len) {
58 const struct iphdr *header = (struct iphdr *)packet;
59 struct ip6_hdr *ip6_targ = (struct ip6_hdr *)out[pos].iov_base;
60 struct ip6_frag *frag_hdr;
61 size_t frag_hdr_len;
62 uint8_t nxthdr;
63 const uint8_t *next_header;
64 size_t len_left;
65 uint32_t old_sum, new_sum;
66 int iov_len;
67
68 if (len < sizeof(struct iphdr)) {
69 logmsg_dbg(ANDROID_LOG_ERROR, "ip_packet/too short for an ip header");
70 return 0;
71 }
72
73 if (header->ihl < 5) {
74 logmsg_dbg(ANDROID_LOG_ERROR, "ip_packet/ip header length set to less than 5: %x", header->ihl);
75 return 0;
76 }
77
78 if ((size_t)header->ihl * 4 > len) { // ip header length larger than entire packet
79 logmsg_dbg(ANDROID_LOG_ERROR, "ip_packet/ip header length set too large: %x", header->ihl);
80 return 0;
81 }
82
83 if (header->version != 4) {
84 logmsg_dbg(ANDROID_LOG_ERROR, "ip_packet/ip header version not 4: %x", header->version);
85 return 0;
86 }
87
88 if ((header->daddr & htonl(0xf0000000)) == htonl(0xe0000000)) {
89 logmsg_dbg(ANDROID_LOG_INFO, "ip_packet/daddr is multicast: %x", ntohl(header->daddr));
90 return 0;
91 }
92
93 /* rfc6145 - If any IPv4 options are present in the IPv4 packet, they MUST be
94 * ignored and the packet translated normally; there is no attempt to
95 * translate the options.
96 */
97
98 next_header = packet + header->ihl * 4;
99 len_left = len - header->ihl * 4;
100
101 nxthdr = header->protocol;
102 if (nxthdr == IPPROTO_ICMP) {
103 // ICMP and ICMPv6 have different protocol numbers.
104 nxthdr = IPPROTO_ICMPV6;
105 }
106
107 /* Fill in the IPv6 header. We need to do this before we translate the packet because TCP and
108 * UDP include parts of the IP header in the checksum. Set the length to zero because we don't
109 * know it yet.
110 */
111 fill_ip6_header(ip6_targ, 0, nxthdr, header);
112 out[pos].iov_len = sizeof(struct ip6_hdr);
113
114 /* Calculate the pseudo-header checksum.
115 * Technically, the length that is used in the pseudo-header checksum is the transport layer
116 * length, which is not the same as len_left in the case of fragmented packets. But since
117 * translation does not change the transport layer length, the checksum is unaffected.
118 */
119 old_sum = ipv4_pseudo_header_checksum(header, len_left);
120 new_sum = ipv6_pseudo_header_checksum(ip6_targ, len_left, nxthdr);
121
122 // If the IPv4 packet is fragmented, add a Fragment header.
123 frag_hdr = (struct ip6_frag *)out[pos + 1].iov_base;
124 frag_hdr_len = maybe_fill_frag_header(frag_hdr, ip6_targ, header);
125 out[pos + 1].iov_len = frag_hdr_len;
126
127 if (frag_hdr_len && frag_hdr->ip6f_offlg & IP6F_OFF_MASK) {
128 // Non-first fragment. Copy the rest of the packet as is.
129 iov_len = generic_packet(out, pos + 2, next_header, len_left);
130 } else if (nxthdr == IPPROTO_ICMPV6) {
131 iov_len = icmp_packet(out, pos + 2, (const struct icmphdr *)next_header, new_sum, len_left);
132 } else if (nxthdr == IPPROTO_TCP) {
133 iov_len =
134 tcp_packet(out, pos + 2, (const struct tcphdr *)next_header, old_sum, new_sum, len_left);
135 } else if (nxthdr == IPPROTO_UDP) {
136 iov_len =
137 udp_packet(out, pos + 2, (const struct udphdr *)next_header, old_sum, new_sum, len_left);
138 } else if (nxthdr == IPPROTO_GRE || nxthdr == IPPROTO_ESP) {
139 iov_len = generic_packet(out, pos + 2, next_header, len_left);
140 } else {
141 #if CLAT_DEBUG
142 logmsg_dbg(ANDROID_LOG_ERROR, "ip_packet/unknown protocol: %x", header->protocol);
143 logcat_hexdump("ipv4/protocol", packet, len);
144 #endif
145 return 0;
146 }
147
148 // Set the length.
149 ip6_targ->ip6_plen = htons(packet_length(out, pos));
150 return iov_len;
151 }
152