blob: 865a568ac8908754504f6cfd8ca804fdc908ff25 [file] [log] [blame]
Daniel Drowna45056e2012-03-23 10:42:54 -05001/*
2 * Copyright 2012 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 * clatd.c - tun interface setup and main event loop
17 */
junyulaic4e591a2018-11-26 22:36:10 +090018#include <arpa/inet.h>
19#include <errno.h>
20#include <fcntl.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050021#include <poll.h>
22#include <signal.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050023#include <stdio.h>
junyulaic4e591a2018-11-26 22:36:10 +090024#include <stdlib.h>
25#include <string.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050026#include <sys/ioctl.h>
Elliott Hughes3afe9ae2014-07-18 17:25:26 -070027#include <sys/prctl.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050028#include <sys/stat.h>
junyulaic4e591a2018-11-26 22:36:10 +090029#include <sys/types.h>
30#include <time.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050031#include <unistd.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050032
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +090033#include <linux/filter.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050034#include <linux/if.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050035#include <linux/if_ether.h>
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +090036#include <linux/if_packet.h>
junyulaic4e591a2018-11-26 22:36:10 +090037#include <linux/if_tun.h>
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +090038#include <net/if.h>
junyulaic4e591a2018-11-26 22:36:10 +090039#include <sys/capability.h>
40#include <sys/uio.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050041
Lorenzo Colitti27da0ad2020-06-01 12:15:20 +090042#include <netid_client.h> // For MARK_UNSET.
43#include <private/android_filesystem_config.h> // For AID_CLAT.
Daniel Drowna45056e2012-03-23 10:42:54 -050044
Daniel Drowna45056e2012-03-23 10:42:54 -050045#include "clatd.h"
46#include "config.h"
Daniel Drowna45056e2012-03-23 10:42:54 -050047#include "dump.h"
junyulaic4e591a2018-11-26 22:36:10 +090048#include "getaddr.h"
49#include "logging.h"
junyulaic4e591a2018-11-26 22:36:10 +090050#include "setif.h"
51#include "translate.h"
Daniel Drowna45056e2012-03-23 10:42:54 -050052
Maciej Żenczykowski5ce6cda2020-06-02 14:39:33 -070053struct clat_config Global_Clatd_Config;
54
Lorenzo Colitti57d480d2014-02-09 10:35:38 +090055/* 40 bytes IPv6 header - 20 bytes IPv4 header + 8 bytes fragment header */
56#define MTU_DELTA 28
57
Daniel Drowna45056e2012-03-23 10:42:54 -050058volatile sig_atomic_t running = 1;
59
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +090060/* function: configure_packet_socket
61 * Binds the packet socket and attaches the receive filter to it.
junyulaic4e591a2018-11-26 22:36:10 +090062 * sock - the socket to configure
Daniel Drowna45056e2012-03-23 10:42:54 -050063 */
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +090064int configure_packet_socket(int sock) {
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +090065 uint32_t *ipv6 = Global_Clatd_Config.ipv6_local_subnet.s6_addr32;
junyulaic4e591a2018-11-26 22:36:10 +090066
67 // clang-format off
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +090068 struct sock_filter filter_code[] = {
69 // Load the first four bytes of the IPv6 destination address (starts 24 bytes in).
70 // Compare it against the first four bytes of our IPv6 address, in host byte order (BPF loads
71 // are always in host byte order). If it matches, continue with next instruction (JMP 0). If it
72 // doesn't match, jump ahead to statement that returns 0 (ignore packet). Repeat for the other
73 // three words of the IPv6 address, and if they all match, return PACKETLEN (accept packet).
74 BPF_STMT(BPF_LD | BPF_W | BPF_ABS, 24),
75 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(ipv6[0]), 0, 7),
76 BPF_STMT(BPF_LD | BPF_W | BPF_ABS, 28),
77 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(ipv6[1]), 0, 5),
78 BPF_STMT(BPF_LD | BPF_W | BPF_ABS, 32),
79 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(ipv6[2]), 0, 3),
80 BPF_STMT(BPF_LD | BPF_W | BPF_ABS, 36),
81 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(ipv6[3]), 0, 1),
82 BPF_STMT(BPF_RET | BPF_K, PACKETLEN),
junyulaic4e591a2018-11-26 22:36:10 +090083 BPF_STMT(BPF_RET | BPF_K, 0),
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +090084 };
junyulaic4e591a2018-11-26 22:36:10 +090085 // clang-format on
86 struct sock_fprog filter = { sizeof(filter_code) / sizeof(filter_code[0]), filter_code };
Daniel Drowna45056e2012-03-23 10:42:54 -050087
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +090088 if (setsockopt(sock, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter))) {
89 logmsg(ANDROID_LOG_FATAL, "attach packet filter failed: %s", strerror(errno));
90 return 0;
Daniel Drowna45056e2012-03-23 10:42:54 -050091 }
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +090092
Maciej Żenczykowski69c840f2019-11-18 12:00:49 -080093 struct sockaddr_ll sll = {
94 .sll_family = AF_PACKET,
95 .sll_protocol = htons(ETH_P_IPV6),
Maciej Żenczykowskiba667df2020-06-02 01:41:54 -070096 .sll_ifindex = if_nametoindex(Global_Clatd_Config.native_ipv6_interface),
Maciej Żenczykowski69c840f2019-11-18 12:00:49 -080097 .sll_pkttype = PACKET_OTHERHOST, // The 464xlat IPv6 address is not assigned to the kernel.
98 };
99 if (bind(sock, (struct sockaddr *)&sll, sizeof(sll))) {
100 logmsg(ANDROID_LOG_FATAL, "binding packet socket: %s", strerror(errno));
101 return 0;
102 }
103
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900104 return 1;
Daniel Drowna45056e2012-03-23 10:42:54 -0500105}
106
junyulaib5e8f972018-10-29 23:10:15 +0800107/* function: set_capability
108 * set the permitted, effective and inheritable capabilities of the current
109 * thread
Daniel Drowna45056e2012-03-23 10:42:54 -0500110 */
junyulaib5e8f972018-10-29 23:10:15 +0800111void set_capability(uint64_t target_cap) {
112 struct __user_cap_header_struct header = {
113 .version = _LINUX_CAPABILITY_VERSION_3,
114 .pid = 0 // 0 = change myself
115 };
116 struct __user_cap_data_struct cap[_LINUX_CAPABILITY_U32S_3] = {};
117
118 cap[0].permitted = cap[0].effective = cap[0].inheritable = target_cap;
119 cap[1].permitted = cap[1].effective = cap[1].inheritable = target_cap >> 32;
120
121 if (capset(&header, cap) < 0) {
122 logmsg(ANDROID_LOG_FATAL, "capset failed: %s", strerror(errno));
123 exit(1);
124 }
125}
126
Maciej Żenczykowskib64249e2021-10-22 18:31:50 -0700127/* function: drop_root_and_caps
128 * drops root privs and all capabilities
junyulaib5e8f972018-10-29 23:10:15 +0800129 */
Maciej Żenczykowskib64249e2021-10-22 18:31:50 -0700130void drop_root_and_caps() {
Maciej Żenczykowski7c87aaa2021-10-22 16:07:00 -0700131 // see man setgroups: this drops all supplementary groups
132 if (setgroups(0, NULL) < 0) {
junyulaib5e8f972018-10-29 23:10:15 +0800133 logmsg(ANDROID_LOG_FATAL, "setgroups failed: %s", strerror(errno));
Daniel Drowna45056e2012-03-23 10:42:54 -0500134 exit(1);
135 }
136
junyulaib5e8f972018-10-29 23:10:15 +0800137 if (setresgid(AID_CLAT, AID_CLAT, AID_CLAT) < 0) {
138 logmsg(ANDROID_LOG_FATAL, "setresgid failed: %s", strerror(errno));
Daniel Drowna45056e2012-03-23 10:42:54 -0500139 exit(1);
140 }
junyulaib5e8f972018-10-29 23:10:15 +0800141 if (setresuid(AID_CLAT, AID_CLAT, AID_CLAT) < 0) {
142 logmsg(ANDROID_LOG_FATAL, "setresuid failed: %s", strerror(errno));
Daniel Drowna45056e2012-03-23 10:42:54 -0500143 exit(1);
144 }
145
Maciej Żenczykowskib64249e2021-10-22 18:31:50 -0700146 set_capability(0);
Daniel Drowna45056e2012-03-23 10:42:54 -0500147}
148
Lorenzo Colitti66deecd2019-01-04 12:27:27 +0900149int ipv6_address_changed(const char *interface) {
150 union anyip *interface_ip;
151
152 interface_ip = getinterface_ip(interface, AF_INET6);
153 if (!interface_ip) {
154 logmsg(ANDROID_LOG_ERROR, "Unable to find an IPv6 address on interface %s", interface);
155 return 1;
156 }
157
158 if (!ipv6_prefix_equal(&interface_ip->ip6, &Global_Clatd_Config.ipv6_local_subnet)) {
159 char oldstr[INET6_ADDRSTRLEN];
160 char newstr[INET6_ADDRSTRLEN];
161 inet_ntop(AF_INET6, &Global_Clatd_Config.ipv6_local_subnet, oldstr, sizeof(oldstr));
162 inet_ntop(AF_INET6, &interface_ip->ip6, newstr, sizeof(newstr));
163 logmsg(ANDROID_LOG_INFO, "IPv6 prefix on %s changed: %s -> %s", interface, oldstr, newstr);
164 free(interface_ip);
165 return 1;
166 } else {
167 free(interface_ip);
168 return 0;
169 }
170}
171
Lorenzo Colittif0fac862019-01-11 18:10:11 +0900172/* function: configure_clat_ipv6_address
173 * picks the clat IPv6 address and configures packet translation to use it.
174 * tunnel - tun device data
175 * interface - uplink interface name
176 * returns: 1 on success, 0 on failure
177 */
178int configure_clat_ipv6_address(const struct tun_data *tunnel, const char *interface,
179 const char *v6_addr) {
Lorenzo Colittibaa3c6a2020-06-02 01:55:12 +0900180 if (!v6_addr || !inet_pton(AF_INET6, v6_addr, &Global_Clatd_Config.ipv6_local_subnet)) {
181 logmsg(ANDROID_LOG_FATAL, "Invalid source address %s", v6_addr);
182 return 0;
Lorenzo Colittif0fac862019-01-11 18:10:11 +0900183 }
Lorenzo Colittif0fac862019-01-11 18:10:11 +0900184
185 char addrstr[INET6_ADDRSTRLEN];
186 inet_ntop(AF_INET6, &Global_Clatd_Config.ipv6_local_subnet, addrstr, sizeof(addrstr));
Lorenzo Colitti66deecd2019-01-04 12:27:27 +0900187 logmsg(ANDROID_LOG_INFO, "Using IPv6 address %s on %s", addrstr, interface);
Lorenzo Colitti1352a3a2014-10-21 13:41:21 +0900188
189 // Start translating packets to the new prefix.
Lorenzo Colitti8a41a5d2014-10-21 12:37:48 +0900190 add_anycast_address(tunnel->write_fd6, &Global_Clatd_Config.ipv6_local_subnet, interface);
Lorenzo Colitti1352a3a2014-10-21 13:41:21 +0900191
192 // Update our packet socket filter to reflect the new 464xlat IP address.
193 if (!configure_packet_socket(tunnel->read_fd6)) {
junyulaic4e591a2018-11-26 22:36:10 +0900194 // Things aren't going to work. Bail out and hope we have better luck next time.
195 // We don't log an error here because configure_packet_socket has already done so.
Lorenzo Colitti66deecd2019-01-04 12:27:27 +0900196 return 0;
Lorenzo Colitti1352a3a2014-10-21 13:41:21 +0900197 }
198
199 return 1;
200}
201
Daniel Drowna45056e2012-03-23 10:42:54 -0500202/* function: configure_interface
203 * reads the configuration and applies it to the interface
junyulaic4e591a2018-11-26 22:36:10 +0900204 * uplink_interface - network interface to use to reach the ipv6 internet
205 * plat_prefix - PLAT prefix to use
Lorenzo Colitti27da0ad2020-06-01 12:15:20 +0900206 * v6_addr - the v6 address to use on the native interface
junyulaic4e591a2018-11-26 22:36:10 +0900207 * tunnel - tun device data
Daniel Drowna45056e2012-03-23 10:42:54 -0500208 */
Hungming Chen5dafb0e2021-11-24 20:19:43 +0800209void configure_interface(const char *uplink_interface, const char *plat_prefix, const char *v6_addr,
210 struct tun_data *tunnel) {
Maciej Żenczykowskiba667df2020-06-02 01:41:54 -0700211 Global_Clatd_Config.native_ipv6_interface = uplink_interface;
Lorenzo Colitti27da0ad2020-06-01 12:15:20 +0900212 if (!plat_prefix || inet_pton(AF_INET6, plat_prefix, &Global_Clatd_Config.plat_subnet) <= 0) {
213 logmsg(ANDROID_LOG_FATAL, "invalid IPv6 address specified for plat prefix: %s", plat_prefix);
214 exit(1);
215 }
216
Lorenzo Colittif0fac862019-01-11 18:10:11 +0900217 if (!configure_clat_ipv6_address(tunnel, uplink_interface, v6_addr)) {
Lorenzo Colitti66deecd2019-01-04 12:27:27 +0900218 exit(1);
219 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500220}
221
Daniel Drowna45056e2012-03-23 10:42:54 -0500222/* function: read_packet
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900223 * reads a packet from the tunnel fd and translates it
junyulaic4e591a2018-11-26 22:36:10 +0900224 * read_fd - file descriptor to read original packet from
225 * write_fd - file descriptor to write translated packet to
226 * to_ipv6 - whether the packet is to be translated to ipv6 or ipv4
Daniel Drowna45056e2012-03-23 10:42:54 -0500227 */
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900228void read_packet(int read_fd, int write_fd, int to_ipv6) {
Maciej Żenczykowski50303532020-06-02 14:46:45 -0700229 uint8_t buf[PACKETLEN];
230 ssize_t readlen = read(read_fd, buf, PACKETLEN);
Daniel Drowna45056e2012-03-23 10:42:54 -0500231
junyulaic4e591a2018-11-26 22:36:10 +0900232 if (readlen < 0) {
Lorenzo Colitti49454812015-01-31 19:18:47 +0900233 if (errno != EAGAIN) {
junyulaic4e591a2018-11-26 22:36:10 +0900234 logmsg(ANDROID_LOG_WARN, "read_packet/read error: %s", strerror(errno));
Lorenzo Colitti49454812015-01-31 19:18:47 +0900235 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500236 return;
junyulaic4e591a2018-11-26 22:36:10 +0900237 } else if (readlen == 0) {
238 logmsg(ANDROID_LOG_WARN, "read_packet/tun interface removed");
Daniel Drowna45056e2012-03-23 10:42:54 -0500239 running = 0;
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900240 return;
241 }
242
Maciej Żenczykowski50303532020-06-02 14:46:45 -0700243 if (!to_ipv6) {
244 translate_packet(write_fd, 0 /* to_ipv6 */, buf, readlen);
245 return;
246 }
247
junyulaic4e591a2018-11-26 22:36:10 +0900248 struct tun_pi *tun_header = (struct tun_pi *)buf;
249 if (readlen < (ssize_t)sizeof(*tun_header)) {
250 logmsg(ANDROID_LOG_WARN, "read_packet/short read: got %ld bytes", readlen);
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900251 return;
Daniel Drowna45056e2012-03-23 10:42:54 -0500252 }
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900253
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900254 uint16_t proto = ntohs(tun_header->proto);
255 if (proto != ETH_P_IP) {
256 logmsg(ANDROID_LOG_WARN, "%s: unknown packet type = 0x%x", __func__, proto);
257 return;
258 }
259
junyulaic4e591a2018-11-26 22:36:10 +0900260 if (tun_header->flags != 0) {
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900261 logmsg(ANDROID_LOG_WARN, "%s: unexpected flags = %d", __func__, tun_header->flags);
262 }
263
Maciej Żenczykowski50303532020-06-02 14:46:45 -0700264 uint8_t *packet = (uint8_t *)(tun_header + 1);
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900265 readlen -= sizeof(*tun_header);
Maciej Żenczykowski50303532020-06-02 14:46:45 -0700266 translate_packet(write_fd, 1 /* to_ipv6 */, packet, readlen);
Daniel Drowna45056e2012-03-23 10:42:54 -0500267}
268
269/* function: event_loop
270 * reads packets from the tun network interface and passes them down the stack
junyulaic4e591a2018-11-26 22:36:10 +0900271 * tunnel - tun device data
Daniel Drowna45056e2012-03-23 10:42:54 -0500272 */
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900273void event_loop(struct tun_data *tunnel) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500274 time_t last_interface_poll;
Lorenzo Colittidce3ddf2014-08-25 16:07:12 -0700275 struct pollfd wait_fd[] = {
276 { tunnel->read_fd6, POLLIN, 0 },
277 { tunnel->fd4, POLLIN, 0 },
278 };
Daniel Drowna45056e2012-03-23 10:42:54 -0500279
280 // start the poll timer
281 last_interface_poll = time(NULL);
282
junyulaic4e591a2018-11-26 22:36:10 +0900283 while (running) {
284 if (poll(wait_fd, ARRAY_SIZE(wait_fd), NO_TRAFFIC_INTERFACE_POLL_FREQUENCY * 1000) == -1) {
Bernie Innocenti69dc60d2018-05-14 20:40:49 +0900285 if (errno != EINTR) {
junyulaic4e591a2018-11-26 22:36:10 +0900286 logmsg(ANDROID_LOG_WARN, "event_loop/poll returned an error: %s", strerror(errno));
Daniel Drowna45056e2012-03-23 10:42:54 -0500287 }
288 } else {
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900289 // Call read_packet if the socket has data to be read, but also if an
290 // error is waiting. If we don't call read() after getting POLLERR, a
291 // subsequent poll() will return immediately with POLLERR again,
292 // causing this code to spin in a loop. Calling read() will clear the
293 // socket error flag instead.
Maciej Żenczykowski50303532020-06-02 14:46:45 -0700294 if (wait_fd[0].revents) read_packet(tunnel->read_fd6, tunnel->fd4, 0 /* to_ipv6 */);
295 if (wait_fd[1].revents) read_packet(tunnel->fd4, tunnel->write_fd6, 1 /* to_ipv6 */);
Daniel Drowna45056e2012-03-23 10:42:54 -0500296 }
297
298 time_t now = time(NULL);
Rocco Yuee4b7da62020-09-02 15:21:41 +0800299 if (now >= (last_interface_poll + INTERFACE_POLL_FREQUENCY)) {
300 last_interface_poll = now;
Maciej Żenczykowskiba667df2020-06-02 01:41:54 -0700301 if (ipv6_address_changed(Global_Clatd_Config.native_ipv6_interface)) {
Lorenzo Colitti66deecd2019-01-04 12:27:27 +0900302 break;
303 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500304 }
305 }
306}