Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 1 | /* |
| 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 | */ |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 18 | #include <arpa/inet.h> |
| 19 | #include <errno.h> |
| 20 | #include <fcntl.h> |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 21 | #include <poll.h> |
| 22 | #include <signal.h> |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 23 | #include <stdio.h> |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 26 | #include <sys/ioctl.h> |
Elliott Hughes | 3afe9ae | 2014-07-18 17:25:26 -0700 | [diff] [blame] | 27 | #include <sys/prctl.h> |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 28 | #include <sys/stat.h> |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 29 | #include <sys/types.h> |
| 30 | #include <time.h> |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 31 | #include <unistd.h> |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 32 | |
Lorenzo Colitti | f08c5aa | 2014-06-03 12:56:38 +0900 | [diff] [blame] | 33 | #include <linux/filter.h> |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 34 | #include <linux/if.h> |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 35 | #include <linux/if_ether.h> |
Lorenzo Colitti | f08c5aa | 2014-06-03 12:56:38 +0900 | [diff] [blame] | 36 | #include <linux/if_packet.h> |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 37 | #include <linux/if_tun.h> |
Lorenzo Colitti | f08c5aa | 2014-06-03 12:56:38 +0900 | [diff] [blame] | 38 | #include <net/if.h> |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 39 | #include <sys/capability.h> |
| 40 | #include <sys/uio.h> |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 41 | |
Lorenzo Colitti | 27da0ad | 2020-06-01 12:15:20 +0900 | [diff] [blame] | 42 | #include <private/android_filesystem_config.h> // For AID_CLAT. |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 43 | |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 44 | #include "clatd.h" |
| 45 | #include "config.h" |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 46 | #include "dump.h" |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 47 | #include "getaddr.h" |
| 48 | #include "logging.h" |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 49 | #include "translate.h" |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 50 | |
Maciej Żenczykowski | 5ce6cda | 2020-06-02 14:39:33 -0700 | [diff] [blame] | 51 | struct clat_config Global_Clatd_Config; |
| 52 | |
Lorenzo Colitti | 57d480d | 2014-02-09 10:35:38 +0900 | [diff] [blame] | 53 | /* 40 bytes IPv6 header - 20 bytes IPv4 header + 8 bytes fragment header */ |
| 54 | #define MTU_DELTA 28 |
| 55 | |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 56 | volatile sig_atomic_t running = 1; |
| 57 | |
junyulai | b5e8f97 | 2018-10-29 23:10:15 +0800 | [diff] [blame] | 58 | /* function: set_capability |
| 59 | * set the permitted, effective and inheritable capabilities of the current |
| 60 | * thread |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 61 | */ |
junyulai | b5e8f97 | 2018-10-29 23:10:15 +0800 | [diff] [blame] | 62 | void set_capability(uint64_t target_cap) { |
| 63 | struct __user_cap_header_struct header = { |
| 64 | .version = _LINUX_CAPABILITY_VERSION_3, |
| 65 | .pid = 0 // 0 = change myself |
| 66 | }; |
| 67 | struct __user_cap_data_struct cap[_LINUX_CAPABILITY_U32S_3] = {}; |
| 68 | |
| 69 | cap[0].permitted = cap[0].effective = cap[0].inheritable = target_cap; |
| 70 | cap[1].permitted = cap[1].effective = cap[1].inheritable = target_cap >> 32; |
| 71 | |
| 72 | if (capset(&header, cap) < 0) { |
| 73 | logmsg(ANDROID_LOG_FATAL, "capset failed: %s", strerror(errno)); |
| 74 | exit(1); |
| 75 | } |
| 76 | } |
| 77 | |
Maciej Żenczykowski | b64249e | 2021-10-22 18:31:50 -0700 | [diff] [blame] | 78 | /* function: drop_root_and_caps |
| 79 | * drops root privs and all capabilities |
junyulai | b5e8f97 | 2018-10-29 23:10:15 +0800 | [diff] [blame] | 80 | */ |
Maciej Żenczykowski | b64249e | 2021-10-22 18:31:50 -0700 | [diff] [blame] | 81 | void drop_root_and_caps() { |
Maciej Żenczykowski | 7c87aaa | 2021-10-22 16:07:00 -0700 | [diff] [blame] | 82 | // see man setgroups: this drops all supplementary groups |
| 83 | if (setgroups(0, NULL) < 0) { |
junyulai | b5e8f97 | 2018-10-29 23:10:15 +0800 | [diff] [blame] | 84 | logmsg(ANDROID_LOG_FATAL, "setgroups failed: %s", strerror(errno)); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 85 | exit(1); |
| 86 | } |
| 87 | |
junyulai | b5e8f97 | 2018-10-29 23:10:15 +0800 | [diff] [blame] | 88 | if (setresgid(AID_CLAT, AID_CLAT, AID_CLAT) < 0) { |
| 89 | logmsg(ANDROID_LOG_FATAL, "setresgid failed: %s", strerror(errno)); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 90 | exit(1); |
| 91 | } |
junyulai | b5e8f97 | 2018-10-29 23:10:15 +0800 | [diff] [blame] | 92 | if (setresuid(AID_CLAT, AID_CLAT, AID_CLAT) < 0) { |
| 93 | logmsg(ANDROID_LOG_FATAL, "setresuid failed: %s", strerror(errno)); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 94 | exit(1); |
| 95 | } |
| 96 | |
Maciej Żenczykowski | b64249e | 2021-10-22 18:31:50 -0700 | [diff] [blame] | 97 | set_capability(0); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 98 | } |
| 99 | |
Lorenzo Colitti | 66deecd | 2019-01-04 12:27:27 +0900 | [diff] [blame] | 100 | int ipv6_address_changed(const char *interface) { |
| 101 | union anyip *interface_ip; |
| 102 | |
| 103 | interface_ip = getinterface_ip(interface, AF_INET6); |
| 104 | if (!interface_ip) { |
| 105 | logmsg(ANDROID_LOG_ERROR, "Unable to find an IPv6 address on interface %s", interface); |
| 106 | return 1; |
| 107 | } |
| 108 | |
| 109 | if (!ipv6_prefix_equal(&interface_ip->ip6, &Global_Clatd_Config.ipv6_local_subnet)) { |
| 110 | char oldstr[INET6_ADDRSTRLEN]; |
| 111 | char newstr[INET6_ADDRSTRLEN]; |
| 112 | inet_ntop(AF_INET6, &Global_Clatd_Config.ipv6_local_subnet, oldstr, sizeof(oldstr)); |
| 113 | inet_ntop(AF_INET6, &interface_ip->ip6, newstr, sizeof(newstr)); |
| 114 | logmsg(ANDROID_LOG_INFO, "IPv6 prefix on %s changed: %s -> %s", interface, oldstr, newstr); |
| 115 | free(interface_ip); |
| 116 | return 1; |
| 117 | } else { |
| 118 | free(interface_ip); |
| 119 | return 0; |
| 120 | } |
| 121 | } |
| 122 | |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 123 | /* function: read_packet |
Lorenzo Colitti | 9353be2 | 2014-12-03 15:18:29 +0900 | [diff] [blame] | 124 | * reads a packet from the tunnel fd and translates it |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 125 | * read_fd - file descriptor to read original packet from |
| 126 | * write_fd - file descriptor to write translated packet to |
| 127 | * to_ipv6 - whether the packet is to be translated to ipv6 or ipv4 |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 128 | */ |
Lorenzo Colitti | 9353be2 | 2014-12-03 15:18:29 +0900 | [diff] [blame] | 129 | void read_packet(int read_fd, int write_fd, int to_ipv6) { |
Maciej Żenczykowski | 5030353 | 2020-06-02 14:46:45 -0700 | [diff] [blame] | 130 | uint8_t buf[PACKETLEN]; |
| 131 | ssize_t readlen = read(read_fd, buf, PACKETLEN); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 132 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 133 | if (readlen < 0) { |
Lorenzo Colitti | 4945481 | 2015-01-31 19:18:47 +0900 | [diff] [blame] | 134 | if (errno != EAGAIN) { |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 135 | logmsg(ANDROID_LOG_WARN, "read_packet/read error: %s", strerror(errno)); |
Lorenzo Colitti | 4945481 | 2015-01-31 19:18:47 +0900 | [diff] [blame] | 136 | } |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 137 | return; |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 138 | } else if (readlen == 0) { |
| 139 | logmsg(ANDROID_LOG_WARN, "read_packet/tun interface removed"); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 140 | running = 0; |
Lorenzo Colitti | f08c5aa | 2014-06-03 12:56:38 +0900 | [diff] [blame] | 141 | return; |
| 142 | } |
| 143 | |
Maciej Żenczykowski | 5030353 | 2020-06-02 14:46:45 -0700 | [diff] [blame] | 144 | if (!to_ipv6) { |
| 145 | translate_packet(write_fd, 0 /* to_ipv6 */, buf, readlen); |
| 146 | return; |
| 147 | } |
| 148 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 149 | struct tun_pi *tun_header = (struct tun_pi *)buf; |
| 150 | if (readlen < (ssize_t)sizeof(*tun_header)) { |
| 151 | logmsg(ANDROID_LOG_WARN, "read_packet/short read: got %ld bytes", readlen); |
Lorenzo Colitti | 9353be2 | 2014-12-03 15:18:29 +0900 | [diff] [blame] | 152 | return; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 153 | } |
Lorenzo Colitti | f08c5aa | 2014-06-03 12:56:38 +0900 | [diff] [blame] | 154 | |
Lorenzo Colitti | 9353be2 | 2014-12-03 15:18:29 +0900 | [diff] [blame] | 155 | uint16_t proto = ntohs(tun_header->proto); |
| 156 | if (proto != ETH_P_IP) { |
| 157 | logmsg(ANDROID_LOG_WARN, "%s: unknown packet type = 0x%x", __func__, proto); |
| 158 | return; |
| 159 | } |
| 160 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 161 | if (tun_header->flags != 0) { |
Lorenzo Colitti | 9353be2 | 2014-12-03 15:18:29 +0900 | [diff] [blame] | 162 | logmsg(ANDROID_LOG_WARN, "%s: unexpected flags = %d", __func__, tun_header->flags); |
| 163 | } |
| 164 | |
Maciej Żenczykowski | 5030353 | 2020-06-02 14:46:45 -0700 | [diff] [blame] | 165 | uint8_t *packet = (uint8_t *)(tun_header + 1); |
Lorenzo Colitti | 9353be2 | 2014-12-03 15:18:29 +0900 | [diff] [blame] | 166 | readlen -= sizeof(*tun_header); |
Maciej Żenczykowski | 5030353 | 2020-06-02 14:46:45 -0700 | [diff] [blame] | 167 | translate_packet(write_fd, 1 /* to_ipv6 */, packet, readlen); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | /* function: event_loop |
| 171 | * reads packets from the tun network interface and passes them down the stack |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 172 | * tunnel - tun device data |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 173 | */ |
Lorenzo Colitti | 9353be2 | 2014-12-03 15:18:29 +0900 | [diff] [blame] | 174 | void event_loop(struct tun_data *tunnel) { |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 175 | time_t last_interface_poll; |
Lorenzo Colitti | dce3ddf | 2014-08-25 16:07:12 -0700 | [diff] [blame] | 176 | struct pollfd wait_fd[] = { |
| 177 | { tunnel->read_fd6, POLLIN, 0 }, |
| 178 | { tunnel->fd4, POLLIN, 0 }, |
| 179 | }; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 180 | |
| 181 | // start the poll timer |
| 182 | last_interface_poll = time(NULL); |
| 183 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 184 | while (running) { |
| 185 | if (poll(wait_fd, ARRAY_SIZE(wait_fd), NO_TRAFFIC_INTERFACE_POLL_FREQUENCY * 1000) == -1) { |
Bernie Innocenti | 69dc60d | 2018-05-14 20:40:49 +0900 | [diff] [blame] | 186 | if (errno != EINTR) { |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 187 | logmsg(ANDROID_LOG_WARN, "event_loop/poll returned an error: %s", strerror(errno)); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 188 | } |
| 189 | } else { |
Lorenzo Colitti | 9353be2 | 2014-12-03 15:18:29 +0900 | [diff] [blame] | 190 | // Call read_packet if the socket has data to be read, but also if an |
| 191 | // error is waiting. If we don't call read() after getting POLLERR, a |
| 192 | // subsequent poll() will return immediately with POLLERR again, |
| 193 | // causing this code to spin in a loop. Calling read() will clear the |
| 194 | // socket error flag instead. |
Maciej Żenczykowski | 5030353 | 2020-06-02 14:46:45 -0700 | [diff] [blame] | 195 | if (wait_fd[0].revents) read_packet(tunnel->read_fd6, tunnel->fd4, 0 /* to_ipv6 */); |
| 196 | if (wait_fd[1].revents) read_packet(tunnel->fd4, tunnel->write_fd6, 1 /* to_ipv6 */); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | time_t now = time(NULL); |
Rocco Yue | e4b7da6 | 2020-09-02 15:21:41 +0800 | [diff] [blame] | 200 | if (now >= (last_interface_poll + INTERFACE_POLL_FREQUENCY)) { |
| 201 | last_interface_poll = now; |
Maciej Żenczykowski | ba667df | 2020-06-02 01:41:54 -0700 | [diff] [blame] | 202 | if (ipv6_address_changed(Global_Clatd_Config.native_ipv6_interface)) { |
Lorenzo Colitti | 66deecd | 2019-01-04 12:27:27 +0900 | [diff] [blame] | 203 | break; |
| 204 | } |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | } |