blob: bc6b6524696e3b3cd258962996941cc0cba5b0fc [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
junyulaib5e8f972018-10-29 23:10:15 +080060/* function: set_capability
61 * set the permitted, effective and inheritable capabilities of the current
62 * thread
Daniel Drowna45056e2012-03-23 10:42:54 -050063 */
junyulaib5e8f972018-10-29 23:10:15 +080064void set_capability(uint64_t target_cap) {
65 struct __user_cap_header_struct header = {
66 .version = _LINUX_CAPABILITY_VERSION_3,
67 .pid = 0 // 0 = change myself
68 };
69 struct __user_cap_data_struct cap[_LINUX_CAPABILITY_U32S_3] = {};
70
71 cap[0].permitted = cap[0].effective = cap[0].inheritable = target_cap;
72 cap[1].permitted = cap[1].effective = cap[1].inheritable = target_cap >> 32;
73
74 if (capset(&header, cap) < 0) {
75 logmsg(ANDROID_LOG_FATAL, "capset failed: %s", strerror(errno));
76 exit(1);
77 }
78}
79
Maciej Żenczykowskib64249e2021-10-22 18:31:50 -070080/* function: drop_root_and_caps
81 * drops root privs and all capabilities
junyulaib5e8f972018-10-29 23:10:15 +080082 */
Maciej Żenczykowskib64249e2021-10-22 18:31:50 -070083void drop_root_and_caps() {
Maciej Żenczykowski7c87aaa2021-10-22 16:07:00 -070084 // see man setgroups: this drops all supplementary groups
85 if (setgroups(0, NULL) < 0) {
junyulaib5e8f972018-10-29 23:10:15 +080086 logmsg(ANDROID_LOG_FATAL, "setgroups failed: %s", strerror(errno));
Daniel Drowna45056e2012-03-23 10:42:54 -050087 exit(1);
88 }
89
junyulaib5e8f972018-10-29 23:10:15 +080090 if (setresgid(AID_CLAT, AID_CLAT, AID_CLAT) < 0) {
91 logmsg(ANDROID_LOG_FATAL, "setresgid failed: %s", strerror(errno));
Daniel Drowna45056e2012-03-23 10:42:54 -050092 exit(1);
93 }
junyulaib5e8f972018-10-29 23:10:15 +080094 if (setresuid(AID_CLAT, AID_CLAT, AID_CLAT) < 0) {
95 logmsg(ANDROID_LOG_FATAL, "setresuid failed: %s", strerror(errno));
Daniel Drowna45056e2012-03-23 10:42:54 -050096 exit(1);
97 }
98
Maciej Żenczykowskib64249e2021-10-22 18:31:50 -070099 set_capability(0);
Daniel Drowna45056e2012-03-23 10:42:54 -0500100}
101
Lorenzo Colitti66deecd2019-01-04 12:27:27 +0900102int ipv6_address_changed(const char *interface) {
103 union anyip *interface_ip;
104
105 interface_ip = getinterface_ip(interface, AF_INET6);
106 if (!interface_ip) {
107 logmsg(ANDROID_LOG_ERROR, "Unable to find an IPv6 address on interface %s", interface);
108 return 1;
109 }
110
111 if (!ipv6_prefix_equal(&interface_ip->ip6, &Global_Clatd_Config.ipv6_local_subnet)) {
112 char oldstr[INET6_ADDRSTRLEN];
113 char newstr[INET6_ADDRSTRLEN];
114 inet_ntop(AF_INET6, &Global_Clatd_Config.ipv6_local_subnet, oldstr, sizeof(oldstr));
115 inet_ntop(AF_INET6, &interface_ip->ip6, newstr, sizeof(newstr));
116 logmsg(ANDROID_LOG_INFO, "IPv6 prefix on %s changed: %s -> %s", interface, oldstr, newstr);
117 free(interface_ip);
118 return 1;
119 } else {
120 free(interface_ip);
121 return 0;
122 }
123}
124
Daniel Drowna45056e2012-03-23 10:42:54 -0500125/* function: read_packet
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900126 * reads a packet from the tunnel fd and translates it
junyulaic4e591a2018-11-26 22:36:10 +0900127 * read_fd - file descriptor to read original packet from
128 * write_fd - file descriptor to write translated packet to
129 * to_ipv6 - whether the packet is to be translated to ipv6 or ipv4
Daniel Drowna45056e2012-03-23 10:42:54 -0500130 */
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900131void read_packet(int read_fd, int write_fd, int to_ipv6) {
Maciej Żenczykowski50303532020-06-02 14:46:45 -0700132 uint8_t buf[PACKETLEN];
133 ssize_t readlen = read(read_fd, buf, PACKETLEN);
Daniel Drowna45056e2012-03-23 10:42:54 -0500134
junyulaic4e591a2018-11-26 22:36:10 +0900135 if (readlen < 0) {
Lorenzo Colitti49454812015-01-31 19:18:47 +0900136 if (errno != EAGAIN) {
junyulaic4e591a2018-11-26 22:36:10 +0900137 logmsg(ANDROID_LOG_WARN, "read_packet/read error: %s", strerror(errno));
Lorenzo Colitti49454812015-01-31 19:18:47 +0900138 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500139 return;
junyulaic4e591a2018-11-26 22:36:10 +0900140 } else if (readlen == 0) {
141 logmsg(ANDROID_LOG_WARN, "read_packet/tun interface removed");
Daniel Drowna45056e2012-03-23 10:42:54 -0500142 running = 0;
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900143 return;
144 }
145
Maciej Żenczykowski50303532020-06-02 14:46:45 -0700146 if (!to_ipv6) {
147 translate_packet(write_fd, 0 /* to_ipv6 */, buf, readlen);
148 return;
149 }
150
junyulaic4e591a2018-11-26 22:36:10 +0900151 struct tun_pi *tun_header = (struct tun_pi *)buf;
152 if (readlen < (ssize_t)sizeof(*tun_header)) {
153 logmsg(ANDROID_LOG_WARN, "read_packet/short read: got %ld bytes", readlen);
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900154 return;
Daniel Drowna45056e2012-03-23 10:42:54 -0500155 }
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900156
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900157 uint16_t proto = ntohs(tun_header->proto);
158 if (proto != ETH_P_IP) {
159 logmsg(ANDROID_LOG_WARN, "%s: unknown packet type = 0x%x", __func__, proto);
160 return;
161 }
162
junyulaic4e591a2018-11-26 22:36:10 +0900163 if (tun_header->flags != 0) {
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900164 logmsg(ANDROID_LOG_WARN, "%s: unexpected flags = %d", __func__, tun_header->flags);
165 }
166
Maciej Żenczykowski50303532020-06-02 14:46:45 -0700167 uint8_t *packet = (uint8_t *)(tun_header + 1);
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900168 readlen -= sizeof(*tun_header);
Maciej Żenczykowski50303532020-06-02 14:46:45 -0700169 translate_packet(write_fd, 1 /* to_ipv6 */, packet, readlen);
Daniel Drowna45056e2012-03-23 10:42:54 -0500170}
171
172/* function: event_loop
173 * reads packets from the tun network interface and passes them down the stack
junyulaic4e591a2018-11-26 22:36:10 +0900174 * tunnel - tun device data
Daniel Drowna45056e2012-03-23 10:42:54 -0500175 */
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900176void event_loop(struct tun_data *tunnel) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500177 time_t last_interface_poll;
Lorenzo Colittidce3ddf2014-08-25 16:07:12 -0700178 struct pollfd wait_fd[] = {
179 { tunnel->read_fd6, POLLIN, 0 },
180 { tunnel->fd4, POLLIN, 0 },
181 };
Daniel Drowna45056e2012-03-23 10:42:54 -0500182
183 // start the poll timer
184 last_interface_poll = time(NULL);
185
junyulaic4e591a2018-11-26 22:36:10 +0900186 while (running) {
187 if (poll(wait_fd, ARRAY_SIZE(wait_fd), NO_TRAFFIC_INTERFACE_POLL_FREQUENCY * 1000) == -1) {
Bernie Innocenti69dc60d2018-05-14 20:40:49 +0900188 if (errno != EINTR) {
junyulaic4e591a2018-11-26 22:36:10 +0900189 logmsg(ANDROID_LOG_WARN, "event_loop/poll returned an error: %s", strerror(errno));
Daniel Drowna45056e2012-03-23 10:42:54 -0500190 }
191 } else {
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900192 // Call read_packet if the socket has data to be read, but also if an
193 // error is waiting. If we don't call read() after getting POLLERR, a
194 // subsequent poll() will return immediately with POLLERR again,
195 // causing this code to spin in a loop. Calling read() will clear the
196 // socket error flag instead.
Maciej Żenczykowski50303532020-06-02 14:46:45 -0700197 if (wait_fd[0].revents) read_packet(tunnel->read_fd6, tunnel->fd4, 0 /* to_ipv6 */);
198 if (wait_fd[1].revents) read_packet(tunnel->fd4, tunnel->write_fd6, 1 /* to_ipv6 */);
Daniel Drowna45056e2012-03-23 10:42:54 -0500199 }
200
201 time_t now = time(NULL);
Rocco Yuee4b7da62020-09-02 15:21:41 +0800202 if (now >= (last_interface_poll + INTERFACE_POLL_FREQUENCY)) {
203 last_interface_poll = now;
Maciej Żenczykowskiba667df2020-06-02 01:41:54 -0700204 if (ipv6_address_changed(Global_Clatd_Config.native_ipv6_interface)) {
Lorenzo Colitti66deecd2019-01-04 12:27:27 +0900205 break;
206 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500207 }
208 }
209}