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 | |
| 42 | #include <private/android_filesystem_config.h> |
| 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" |
| 49 | #include "mtu.h" |
| 50 | #include "resolv_netid.h" |
Lorenzo Colitti | 9353be2 | 2014-12-03 15:18:29 +0900 | [diff] [blame] | 51 | #include "ring.h" |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame^] | 52 | #include "setif.h" |
| 53 | #include "translate.h" |
| 54 | #include "tun.h" |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 55 | |
Lorenzo Colitti | 7612916 | 2014-10-20 17:24:51 +0900 | [diff] [blame] | 56 | #define DEVICEPREFIX "v4-" |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 57 | |
Lorenzo Colitti | 57d480d | 2014-02-09 10:35:38 +0900 | [diff] [blame] | 58 | /* 40 bytes IPv6 header - 20 bytes IPv4 header + 8 bytes fragment header */ |
| 59 | #define MTU_DELTA 28 |
| 60 | |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 61 | volatile sig_atomic_t running = 1; |
| 62 | |
Lorenzo Colitti | baf6299 | 2013-03-01 20:29:39 +0900 | [diff] [blame] | 63 | /* function: stop_loop |
| 64 | * signal handler: stop the event loop |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 65 | */ |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame^] | 66 | void stop_loop() { running = 0; } |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 67 | |
Lorenzo Colitti | f08c5aa | 2014-06-03 12:56:38 +0900 | [diff] [blame] | 68 | /* function: configure_packet_socket |
| 69 | * Binds the packet socket and attaches the receive filter to it. |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame^] | 70 | * sock - the socket to configure |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 71 | */ |
Lorenzo Colitti | f08c5aa | 2014-06-03 12:56:38 +0900 | [diff] [blame] | 72 | int configure_packet_socket(int sock) { |
| 73 | struct sockaddr_ll sll = { |
| 74 | .sll_family = AF_PACKET, |
| 75 | .sll_protocol = htons(ETH_P_IPV6), |
Masaya Hoshina | 96d6901 | 2015-06-10 22:17:49 +0900 | [diff] [blame] | 76 | .sll_ifindex = if_nametoindex(Global_Clatd_Config.default_pdp_interface), |
Lorenzo Colitti | f08c5aa | 2014-06-03 12:56:38 +0900 | [diff] [blame] | 77 | .sll_pkttype = PACKET_OTHERHOST, // The 464xlat IPv6 address is not assigned to the kernel. |
| 78 | }; |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame^] | 79 | if (bind(sock, (struct sockaddr *)&sll, sizeof(sll))) { |
Lorenzo Colitti | f08c5aa | 2014-06-03 12:56:38 +0900 | [diff] [blame] | 80 | logmsg(ANDROID_LOG_FATAL, "binding packet socket: %s", strerror(errno)); |
| 81 | return 0; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 82 | } |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 83 | |
Lorenzo Colitti | f08c5aa | 2014-06-03 12:56:38 +0900 | [diff] [blame] | 84 | uint32_t *ipv6 = Global_Clatd_Config.ipv6_local_subnet.s6_addr32; |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame^] | 85 | |
| 86 | // clang-format off |
Lorenzo Colitti | f08c5aa | 2014-06-03 12:56:38 +0900 | [diff] [blame] | 87 | struct sock_filter filter_code[] = { |
| 88 | // Load the first four bytes of the IPv6 destination address (starts 24 bytes in). |
| 89 | // Compare it against the first four bytes of our IPv6 address, in host byte order (BPF loads |
| 90 | // are always in host byte order). If it matches, continue with next instruction (JMP 0). If it |
| 91 | // doesn't match, jump ahead to statement that returns 0 (ignore packet). Repeat for the other |
| 92 | // three words of the IPv6 address, and if they all match, return PACKETLEN (accept packet). |
| 93 | BPF_STMT(BPF_LD | BPF_W | BPF_ABS, 24), |
| 94 | BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(ipv6[0]), 0, 7), |
| 95 | BPF_STMT(BPF_LD | BPF_W | BPF_ABS, 28), |
| 96 | BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(ipv6[1]), 0, 5), |
| 97 | BPF_STMT(BPF_LD | BPF_W | BPF_ABS, 32), |
| 98 | BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(ipv6[2]), 0, 3), |
| 99 | BPF_STMT(BPF_LD | BPF_W | BPF_ABS, 36), |
| 100 | BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(ipv6[3]), 0, 1), |
| 101 | BPF_STMT(BPF_RET | BPF_K, PACKETLEN), |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame^] | 102 | BPF_STMT(BPF_RET | BPF_K, 0), |
Lorenzo Colitti | f08c5aa | 2014-06-03 12:56:38 +0900 | [diff] [blame] | 103 | }; |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame^] | 104 | // clang-format on |
| 105 | struct sock_fprog filter = { sizeof(filter_code) / sizeof(filter_code[0]), filter_code }; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 106 | |
Lorenzo Colitti | f08c5aa | 2014-06-03 12:56:38 +0900 | [diff] [blame] | 107 | if (setsockopt(sock, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter))) { |
| 108 | logmsg(ANDROID_LOG_FATAL, "attach packet filter failed: %s", strerror(errno)); |
| 109 | return 0; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 110 | } |
Lorenzo Colitti | f08c5aa | 2014-06-03 12:56:38 +0900 | [diff] [blame] | 111 | |
| 112 | return 1; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 113 | } |
| 114 | |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 115 | /* function: configure_tun_ip |
| 116 | * configures the ipv4 and ipv6 addresses on the tunnel interface |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame^] | 117 | * tunnel - tun device data |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 118 | */ |
| 119 | void configure_tun_ip(const struct tun_data *tunnel) { |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 120 | int status; |
| 121 | |
Lorenzo Colitti | 798f993 | 2014-10-31 21:54:33 +0900 | [diff] [blame] | 122 | // Pick an IPv4 address to use by finding a free address in the configured prefix. Technically, |
| 123 | // there is a race here - if another clatd calls config_select_ipv4_address after we do, but |
| 124 | // before we call add_address, it can end up having the same IP address as we do. But the time |
| 125 | // window in which this can happen is extremely small, and even if we end up with a duplicate |
| 126 | // address, the only damage is that IPv4 TCP connections won't be reset until both interfaces go |
| 127 | // down. |
| 128 | in_addr_t localaddr = config_select_ipv4_address(&Global_Clatd_Config.ipv4_local_subnet, |
| 129 | Global_Clatd_Config.ipv4_local_prefixlen); |
| 130 | if (localaddr == INADDR_NONE) { |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame^] | 131 | logmsg(ANDROID_LOG_FATAL, "No free IPv4 address in %s/%d", |
Lorenzo Colitti | 798f993 | 2014-10-31 21:54:33 +0900 | [diff] [blame] | 132 | inet_ntoa(Global_Clatd_Config.ipv4_local_subnet), |
| 133 | Global_Clatd_Config.ipv4_local_prefixlen); |
| 134 | exit(1); |
| 135 | } |
| 136 | Global_Clatd_Config.ipv4_local_subnet.s_addr = localaddr; |
| 137 | |
Lorenzo Colitti | 3ca0302 | 2013-03-27 12:39:10 +0900 | [diff] [blame] | 138 | // Configure the interface before bringing it up. As soon as we bring the interface up, the |
| 139 | // framework will be notified and will assume the interface's configuration has been finalized. |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame^] | 140 | status = add_address(tunnel->device4, AF_INET, &Global_Clatd_Config.ipv4_local_subnet, 32, |
| 141 | &Global_Clatd_Config.ipv4_local_subnet); |
| 142 | if (status < 0) { |
| 143 | logmsg(ANDROID_LOG_FATAL, "configure_tun_ip/if_address(4) failed: %s", strerror(-status)); |
Lorenzo Colitti | 3ca0302 | 2013-03-27 12:39:10 +0900 | [diff] [blame] | 144 | exit(1); |
| 145 | } |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 146 | |
Lorenzo Colitti | 798f993 | 2014-10-31 21:54:33 +0900 | [diff] [blame] | 147 | char addrstr[INET_ADDRSTRLEN]; |
| 148 | inet_ntop(AF_INET, &Global_Clatd_Config.ipv4_local_subnet, addrstr, sizeof(addrstr)); |
| 149 | logmsg(ANDROID_LOG_INFO, "Using IPv4 address %s on %s", addrstr, tunnel->device4); |
| 150 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame^] | 151 | if ((status = if_up(tunnel->device4, Global_Clatd_Config.ipv4mtu)) < 0) { |
| 152 | logmsg(ANDROID_LOG_FATAL, "configure_tun_ip/if_up(4) failed: %s", strerror(-status)); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 153 | exit(1); |
| 154 | } |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | /* function: drop_root |
| 158 | * drops root privs but keeps the needed capability |
| 159 | */ |
| 160 | void drop_root() { |
Lorenzo Colitti | 7045e27 | 2014-06-13 21:36:01 +0900 | [diff] [blame] | 161 | gid_t groups[] = { AID_INET, AID_VPN }; |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame^] | 162 | if (setgroups(sizeof(groups) / sizeof(groups[0]), groups) < 0) { |
| 163 | logmsg(ANDROID_LOG_FATAL, "drop_root/setgroups failed: %s", strerror(errno)); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 164 | exit(1); |
| 165 | } |
| 166 | |
| 167 | prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0); |
| 168 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame^] | 169 | if (setgid(AID_CLAT) < 0) { |
| 170 | logmsg(ANDROID_LOG_FATAL, "drop_root/setgid failed: %s", strerror(errno)); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 171 | exit(1); |
| 172 | } |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame^] | 173 | if (setuid(AID_CLAT) < 0) { |
| 174 | logmsg(ANDROID_LOG_FATAL, "drop_root/setuid failed: %s", strerror(errno)); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 175 | exit(1); |
| 176 | } |
| 177 | |
| 178 | struct __user_cap_header_struct header; |
| 179 | struct __user_cap_data_struct cap; |
| 180 | memset(&header, 0, sizeof(header)); |
| 181 | memset(&cap, 0, sizeof(cap)); |
| 182 | |
| 183 | header.version = _LINUX_CAPABILITY_VERSION; |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame^] | 184 | header.pid = 0; // 0 = change myself |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 185 | cap.effective = cap.permitted = (1 << CAP_NET_ADMIN); |
| 186 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame^] | 187 | if (capset(&header, &cap) < 0) { |
| 188 | logmsg(ANDROID_LOG_FATAL, "drop_root/capset failed: %s", strerror(errno)); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 189 | exit(1); |
| 190 | } |
| 191 | } |
| 192 | |
Lorenzo Colitti | 7564761 | 2014-06-09 13:55:50 +0900 | [diff] [blame] | 193 | /* function: open_sockets |
| 194 | * opens a packet socket to receive IPv6 packets and a raw socket to send them |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame^] | 195 | * tunnel - tun device data |
| 196 | * mark - the socket mark to use for the sending raw socket |
Lorenzo Colitti | ce14088 | 2014-06-02 21:20:40 +0900 | [diff] [blame] | 197 | */ |
Lorenzo Colitti | 7564761 | 2014-06-09 13:55:50 +0900 | [diff] [blame] | 198 | void open_sockets(struct tun_data *tunnel, uint32_t mark) { |
Lorenzo Colitti | 4945481 | 2015-01-31 19:18:47 +0900 | [diff] [blame] | 199 | int rawsock = socket(AF_INET6, SOCK_RAW | SOCK_NONBLOCK, IPPROTO_RAW); |
Lorenzo Colitti | ce14088 | 2014-06-02 21:20:40 +0900 | [diff] [blame] | 200 | if (rawsock < 0) { |
| 201 | logmsg(ANDROID_LOG_FATAL, "raw socket failed: %s", strerror(errno)); |
| 202 | exit(1); |
| 203 | } |
| 204 | |
| 205 | int off = 0; |
| 206 | if (setsockopt(rawsock, SOL_IPV6, IPV6_CHECKSUM, &off, sizeof(off)) < 0) { |
| 207 | logmsg(ANDROID_LOG_WARN, "could not disable checksum on raw socket: %s", strerror(errno)); |
| 208 | } |
Lorenzo Colitti | 7564761 | 2014-06-09 13:55:50 +0900 | [diff] [blame] | 209 | if (mark != MARK_UNSET && setsockopt(rawsock, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)) < 0) { |
| 210 | logmsg(ANDROID_LOG_ERROR, "could not set mark on raw socket: %s", strerror(errno)); |
| 211 | } |
Lorenzo Colitti | ce14088 | 2014-06-02 21:20:40 +0900 | [diff] [blame] | 212 | |
| 213 | tunnel->write_fd6 = rawsock; |
Lorenzo Colitti | f08c5aa | 2014-06-03 12:56:38 +0900 | [diff] [blame] | 214 | |
Lorenzo Colitti | 9353be2 | 2014-12-03 15:18:29 +0900 | [diff] [blame] | 215 | tunnel->read_fd6 = ring_create(tunnel); |
| 216 | if (tunnel->read_fd6 < 0) { |
Lorenzo Colitti | f08c5aa | 2014-06-03 12:56:38 +0900 | [diff] [blame] | 217 | exit(1); |
| 218 | } |
Lorenzo Colitti | ce14088 | 2014-06-02 21:20:40 +0900 | [diff] [blame] | 219 | } |
| 220 | |
Lorenzo Colitti | 1352a3a | 2014-10-21 13:41:21 +0900 | [diff] [blame] | 221 | /* function: update_clat_ipv6_address |
| 222 | * picks the clat IPv6 address and configures packet translation to use it. |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame^] | 223 | * tunnel - tun device data |
| 224 | * interface - uplink interface name |
| 225 | * returns: 1 on success, 0 on failure |
Lorenzo Colitti | 1352a3a | 2014-10-21 13:41:21 +0900 | [diff] [blame] | 226 | */ |
| 227 | int update_clat_ipv6_address(const struct tun_data *tunnel, const char *interface) { |
| 228 | union anyip *interface_ip; |
| 229 | char addrstr[INET6_ADDRSTRLEN]; |
| 230 | |
| 231 | // TODO: check that the prefix length is /64. |
| 232 | interface_ip = getinterface_ip(interface, AF_INET6); |
| 233 | if (!interface_ip) { |
| 234 | logmsg(ANDROID_LOG_ERROR, "Unable to find an IPv6 address on interface %s", interface); |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | // If our prefix hasn't changed, do nothing. (If this is the first time we configure an IPv6 |
| 239 | // address, Global_Clatd_Config.ipv6_local_subnet will be ::, which won't match our new prefix.) |
| 240 | if (ipv6_prefix_equal(&interface_ip->ip6, &Global_Clatd_Config.ipv6_local_subnet)) { |
| 241 | free(interface_ip); |
| 242 | return 1; |
| 243 | } |
| 244 | |
| 245 | // Generate an interface ID. |
| 246 | config_generate_local_ipv6_subnet(&interface_ip->ip6); |
| 247 | inet_ntop(AF_INET6, &interface_ip->ip6, addrstr, sizeof(addrstr)); |
| 248 | |
| 249 | if (IN6_IS_ADDR_UNSPECIFIED(&Global_Clatd_Config.ipv6_local_subnet)) { |
| 250 | // Startup. |
Lorenzo Colitti | 798f993 | 2014-10-31 21:54:33 +0900 | [diff] [blame] | 251 | logmsg(ANDROID_LOG_INFO, "Using IPv6 address %s on %s", addrstr, interface); |
Lorenzo Colitti | 1352a3a | 2014-10-21 13:41:21 +0900 | [diff] [blame] | 252 | } else { |
| 253 | // Prefix change. |
| 254 | char from_addr[INET6_ADDRSTRLEN]; |
| 255 | inet_ntop(AF_INET6, &Global_Clatd_Config.ipv6_local_subnet, from_addr, sizeof(from_addr)); |
| 256 | logmsg(ANDROID_LOG_INFO, "clat IPv6 address changed from %s to %s", from_addr, addrstr); |
Lorenzo Colitti | 8a41a5d | 2014-10-21 12:37:48 +0900 | [diff] [blame] | 257 | del_anycast_address(tunnel->write_fd6, &Global_Clatd_Config.ipv6_local_subnet); |
Lorenzo Colitti | 1352a3a | 2014-10-21 13:41:21 +0900 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | // Start translating packets to the new prefix. |
| 261 | Global_Clatd_Config.ipv6_local_subnet = interface_ip->ip6; |
Lorenzo Colitti | 8a41a5d | 2014-10-21 12:37:48 +0900 | [diff] [blame] | 262 | add_anycast_address(tunnel->write_fd6, &Global_Clatd_Config.ipv6_local_subnet, interface); |
Lorenzo Colitti | 1352a3a | 2014-10-21 13:41:21 +0900 | [diff] [blame] | 263 | free(interface_ip); |
| 264 | |
| 265 | // Update our packet socket filter to reflect the new 464xlat IP address. |
| 266 | if (!configure_packet_socket(tunnel->read_fd6)) { |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame^] | 267 | // Things aren't going to work. Bail out and hope we have better luck next time. |
| 268 | // We don't log an error here because configure_packet_socket has already done so. |
| 269 | exit(1); |
Lorenzo Colitti | 1352a3a | 2014-10-21 13:41:21 +0900 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | return 1; |
| 273 | } |
| 274 | |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 275 | /* function: configure_interface |
| 276 | * reads the configuration and applies it to the interface |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame^] | 277 | * uplink_interface - network interface to use to reach the ipv6 internet |
| 278 | * plat_prefix - PLAT prefix to use |
| 279 | * tunnel - tun device data |
| 280 | * net_id - NetID to use, NETID_UNSET indicates use of default network |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 281 | */ |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame^] | 282 | void configure_interface(const char *uplink_interface, const char *plat_prefix, |
| 283 | struct tun_data *tunnel, unsigned net_id) { |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 284 | int error; |
| 285 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame^] | 286 | if (!read_config("/system/etc/clatd.conf", uplink_interface, plat_prefix, net_id)) { |
| 287 | logmsg(ANDROID_LOG_FATAL, "read_config failed"); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 288 | exit(1); |
| 289 | } |
| 290 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame^] | 291 | if (Global_Clatd_Config.mtu > MAXMTU) { |
| 292 | logmsg(ANDROID_LOG_WARN, "Max MTU is %d, requested %d", MAXMTU, Global_Clatd_Config.mtu); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 293 | Global_Clatd_Config.mtu = MAXMTU; |
| 294 | } |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame^] | 295 | if (Global_Clatd_Config.mtu <= 0) { |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 296 | Global_Clatd_Config.mtu = getifmtu(Global_Clatd_Config.default_pdp_interface); |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame^] | 297 | logmsg(ANDROID_LOG_WARN, "ifmtu=%d", Global_Clatd_Config.mtu); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 298 | } |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame^] | 299 | if (Global_Clatd_Config.mtu < 1280) { |
| 300 | logmsg(ANDROID_LOG_WARN, "mtu too small = %d", Global_Clatd_Config.mtu); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 301 | Global_Clatd_Config.mtu = 1280; |
| 302 | } |
| 303 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame^] | 304 | if (Global_Clatd_Config.ipv4mtu <= 0 || |
| 305 | Global_Clatd_Config.ipv4mtu > Global_Clatd_Config.mtu - MTU_DELTA) { |
Lorenzo Colitti | 57d480d | 2014-02-09 10:35:38 +0900 | [diff] [blame] | 306 | Global_Clatd_Config.ipv4mtu = Global_Clatd_Config.mtu - MTU_DELTA; |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame^] | 307 | logmsg(ANDROID_LOG_WARN, "ipv4mtu now set to = %d", Global_Clatd_Config.ipv4mtu); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 308 | } |
| 309 | |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 310 | error = tun_alloc(tunnel->device4, tunnel->fd4); |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame^] | 311 | if (error < 0) { |
| 312 | logmsg(ANDROID_LOG_FATAL, "tun_alloc/4 failed: %s", strerror(errno)); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 313 | exit(1); |
| 314 | } |
| 315 | |
Lorenzo Colitti | 4945481 | 2015-01-31 19:18:47 +0900 | [diff] [blame] | 316 | error = set_nonblocking(tunnel->fd4); |
| 317 | if (error < 0) { |
| 318 | logmsg(ANDROID_LOG_FATAL, "set_nonblocking failed: %s", strerror(errno)); |
| 319 | exit(1); |
| 320 | } |
| 321 | |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 322 | configure_tun_ip(tunnel); |
| 323 | } |
| 324 | |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 325 | /* function: read_packet |
Lorenzo Colitti | 9353be2 | 2014-12-03 15:18:29 +0900 | [diff] [blame] | 326 | * reads a packet from the tunnel fd and translates it |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame^] | 327 | * read_fd - file descriptor to read original packet from |
| 328 | * write_fd - file descriptor to write translated packet to |
| 329 | * 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] | 330 | */ |
Lorenzo Colitti | 9353be2 | 2014-12-03 15:18:29 +0900 | [diff] [blame] | 331 | void read_packet(int read_fd, int write_fd, int to_ipv6) { |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 332 | ssize_t readlen; |
Lorenzo Colitti | f08c5aa | 2014-06-03 12:56:38 +0900 | [diff] [blame] | 333 | uint8_t buf[PACKETLEN], *packet; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 334 | |
Lorenzo Colitti | 9353be2 | 2014-12-03 15:18:29 +0900 | [diff] [blame] | 335 | readlen = read(read_fd, buf, PACKETLEN); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 336 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame^] | 337 | if (readlen < 0) { |
Lorenzo Colitti | 4945481 | 2015-01-31 19:18:47 +0900 | [diff] [blame] | 338 | if (errno != EAGAIN) { |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame^] | 339 | logmsg(ANDROID_LOG_WARN, "read_packet/read error: %s", strerror(errno)); |
Lorenzo Colitti | 4945481 | 2015-01-31 19:18:47 +0900 | [diff] [blame] | 340 | } |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 341 | return; |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame^] | 342 | } else if (readlen == 0) { |
| 343 | logmsg(ANDROID_LOG_WARN, "read_packet/tun interface removed"); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 344 | running = 0; |
Lorenzo Colitti | f08c5aa | 2014-06-03 12:56:38 +0900 | [diff] [blame] | 345 | return; |
| 346 | } |
| 347 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame^] | 348 | struct tun_pi *tun_header = (struct tun_pi *)buf; |
| 349 | if (readlen < (ssize_t)sizeof(*tun_header)) { |
| 350 | logmsg(ANDROID_LOG_WARN, "read_packet/short read: got %ld bytes", readlen); |
Lorenzo Colitti | 9353be2 | 2014-12-03 15:18:29 +0900 | [diff] [blame] | 351 | return; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 352 | } |
Lorenzo Colitti | f08c5aa | 2014-06-03 12:56:38 +0900 | [diff] [blame] | 353 | |
Lorenzo Colitti | 9353be2 | 2014-12-03 15:18:29 +0900 | [diff] [blame] | 354 | uint16_t proto = ntohs(tun_header->proto); |
| 355 | if (proto != ETH_P_IP) { |
| 356 | logmsg(ANDROID_LOG_WARN, "%s: unknown packet type = 0x%x", __func__, proto); |
| 357 | return; |
| 358 | } |
| 359 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame^] | 360 | if (tun_header->flags != 0) { |
Lorenzo Colitti | 9353be2 | 2014-12-03 15:18:29 +0900 | [diff] [blame] | 361 | logmsg(ANDROID_LOG_WARN, "%s: unexpected flags = %d", __func__, tun_header->flags); |
| 362 | } |
| 363 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame^] | 364 | packet = (uint8_t *)(tun_header + 1); |
Lorenzo Colitti | 9353be2 | 2014-12-03 15:18:29 +0900 | [diff] [blame] | 365 | readlen -= sizeof(*tun_header); |
| 366 | translate_packet(write_fd, to_ipv6, packet, readlen); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | /* function: event_loop |
| 370 | * reads packets from the tun network interface and passes them down the stack |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame^] | 371 | * tunnel - tun device data |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 372 | */ |
Lorenzo Colitti | 9353be2 | 2014-12-03 15:18:29 +0900 | [diff] [blame] | 373 | void event_loop(struct tun_data *tunnel) { |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 374 | time_t last_interface_poll; |
Lorenzo Colitti | dce3ddf | 2014-08-25 16:07:12 -0700 | [diff] [blame] | 375 | struct pollfd wait_fd[] = { |
| 376 | { tunnel->read_fd6, POLLIN, 0 }, |
| 377 | { tunnel->fd4, POLLIN, 0 }, |
| 378 | }; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 379 | |
| 380 | // start the poll timer |
| 381 | last_interface_poll = time(NULL); |
| 382 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame^] | 383 | while (running) { |
| 384 | 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] | 385 | if (errno != EINTR) { |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame^] | 386 | 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] | 387 | } |
| 388 | } else { |
Bernie Innocenti | 69dc60d | 2018-05-14 20:40:49 +0900 | [diff] [blame] | 389 | if (wait_fd[0].revents & POLLIN) { |
| 390 | ring_read(&tunnel->ring, tunnel->fd4, 0 /* to_ipv6 */); |
| 391 | } |
| 392 | // If any other bit is set, assume it's due to an error (i.e. POLLERR). |
| 393 | if (wait_fd[0].revents & ~POLLIN) { |
| 394 | // ring_read doesn't clear the error indication on the socket. |
| 395 | recv(tunnel->read_fd6, NULL, 0, MSG_PEEK); |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame^] | 396 | logmsg(ANDROID_LOG_WARN, "event_loop: clearing error on read_fd6: %s", strerror(errno)); |
Bernie Innocenti | 69dc60d | 2018-05-14 20:40:49 +0900 | [diff] [blame] | 397 | } |
| 398 | |
Lorenzo Colitti | 9353be2 | 2014-12-03 15:18:29 +0900 | [diff] [blame] | 399 | // Call read_packet if the socket has data to be read, but also if an |
| 400 | // error is waiting. If we don't call read() after getting POLLERR, a |
| 401 | // subsequent poll() will return immediately with POLLERR again, |
| 402 | // causing this code to spin in a loop. Calling read() will clear the |
| 403 | // socket error flag instead. |
Lorenzo Colitti | 9353be2 | 2014-12-03 15:18:29 +0900 | [diff] [blame] | 404 | if (wait_fd[1].revents) { |
| 405 | read_packet(tunnel->fd4, tunnel->write_fd6, 1 /* to_ipv6 */); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 406 | } |
| 407 | } |
| 408 | |
| 409 | time_t now = time(NULL); |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame^] | 410 | if (last_interface_poll < (now - INTERFACE_POLL_FREQUENCY)) { |
Lorenzo Colitti | 1352a3a | 2014-10-21 13:41:21 +0900 | [diff] [blame] | 411 | update_clat_ipv6_address(tunnel, Global_Clatd_Config.default_pdp_interface); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 412 | last_interface_poll = now; |
| 413 | } |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | /* function: print_help |
| 418 | * in case the user is running this on the command line |
| 419 | */ |
| 420 | void print_help() { |
| 421 | printf("android-clat arguments:\n"); |
| 422 | printf("-i [uplink interface]\n"); |
| 423 | printf("-p [plat prefix]\n"); |
Paul Jensen | 247dd71 | 2014-05-30 13:19:10 -0400 | [diff] [blame] | 424 | printf("-n [NetId]\n"); |
Lorenzo Colitti | 7564761 | 2014-06-09 13:55:50 +0900 | [diff] [blame] | 425 | printf("-m [socket mark]\n"); |
| 426 | } |
| 427 | |
| 428 | /* function: parse_unsigned |
| 429 | * parses a string as a decimal/hex/octal unsigned integer |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame^] | 430 | * str - the string to parse |
| 431 | * out - the unsigned integer to write to, gets clobbered on failure |
Lorenzo Colitti | 7564761 | 2014-06-09 13:55:50 +0900 | [diff] [blame] | 432 | */ |
| 433 | int parse_unsigned(const char *str, unsigned *out) { |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame^] | 434 | char *end_ptr; |
| 435 | *out = strtoul(str, &end_ptr, 0); |
| 436 | return *str && !*end_ptr; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | /* function: main |
| 440 | * allocate and setup the tun device, then run the event loop |
| 441 | */ |
| 442 | int main(int argc, char **argv) { |
| 443 | struct tun_data tunnel; |
| 444 | int opt; |
Lorenzo Colitti | 7564761 | 2014-06-09 13:55:50 +0900 | [diff] [blame] | 445 | char *uplink_interface = NULL, *plat_prefix = NULL, *net_id_str = NULL, *mark_str = NULL; |
Paul Jensen | 247dd71 | 2014-05-30 13:19:10 -0400 | [diff] [blame] | 446 | unsigned net_id = NETID_UNSET; |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame^] | 447 | uint32_t mark = MARK_UNSET; |
Lorenzo Colitti | 7612916 | 2014-10-20 17:24:51 +0900 | [diff] [blame] | 448 | unsigned len; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 449 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame^] | 450 | while ((opt = getopt(argc, argv, "i:p:n:m:h")) != -1) { |
| 451 | switch (opt) { |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 452 | case 'i': |
| 453 | uplink_interface = optarg; |
| 454 | break; |
| 455 | case 'p': |
| 456 | plat_prefix = optarg; |
| 457 | break; |
Paul Jensen | 247dd71 | 2014-05-30 13:19:10 -0400 | [diff] [blame] | 458 | case 'n': |
| 459 | net_id_str = optarg; |
| 460 | break; |
Lorenzo Colitti | 7564761 | 2014-06-09 13:55:50 +0900 | [diff] [blame] | 461 | case 'm': |
| 462 | mark_str = optarg; |
| 463 | break; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 464 | case 'h': |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 465 | print_help(); |
Lorenzo Colitti | b9b471e | 2014-06-09 19:39:01 +0900 | [diff] [blame] | 466 | exit(0); |
| 467 | default: |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame^] | 468 | logmsg(ANDROID_LOG_FATAL, "Unknown option -%c. Exiting.", (char)optopt); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 469 | exit(1); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 470 | } |
| 471 | } |
| 472 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame^] | 473 | if (uplink_interface == NULL) { |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 474 | logmsg(ANDROID_LOG_FATAL, "clatd called without an interface"); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 475 | exit(1); |
| 476 | } |
Lorenzo Colitti | 7564761 | 2014-06-09 13:55:50 +0900 | [diff] [blame] | 477 | |
| 478 | if (net_id_str != NULL && !parse_unsigned(net_id_str, &net_id)) { |
| 479 | logmsg(ANDROID_LOG_FATAL, "invalid NetID %s", net_id_str); |
| 480 | exit(1); |
Paul Jensen | 247dd71 | 2014-05-30 13:19:10 -0400 | [diff] [blame] | 481 | } |
Lorenzo Colitti | 7564761 | 2014-06-09 13:55:50 +0900 | [diff] [blame] | 482 | |
| 483 | if (mark_str != NULL && !parse_unsigned(mark_str, &mark)) { |
| 484 | logmsg(ANDROID_LOG_FATAL, "invalid mark %s", mark_str); |
| 485 | exit(1); |
| 486 | } |
| 487 | |
Lorenzo Colitti | 7612916 | 2014-10-20 17:24:51 +0900 | [diff] [blame] | 488 | len = snprintf(tunnel.device4, sizeof(tunnel.device4), "%s%s", DEVICEPREFIX, uplink_interface); |
| 489 | if (len >= sizeof(tunnel.device4)) { |
| 490 | logmsg(ANDROID_LOG_FATAL, "interface name too long '%s'", tunnel.device4); |
| 491 | exit(1); |
| 492 | } |
| 493 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame^] | 494 | logmsg(ANDROID_LOG_INFO, "Starting clat version %s on %s netid=%s mark=%s", CLATD_VERSION, |
| 495 | uplink_interface, net_id_str ? net_id_str : "(none)", mark_str ? mark_str : "(none)"); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 496 | |
Lorenzo Colitti | 7045e27 | 2014-06-13 21:36:01 +0900 | [diff] [blame] | 497 | // open our raw sockets before dropping privs |
| 498 | open_sockets(&tunnel, mark); |
| 499 | |
| 500 | // run under a regular user |
| 501 | drop_root(); |
| 502 | |
| 503 | // we can create tun devices as non-root because we're in the VPN group. |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 504 | tunnel.fd4 = tun_open(); |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame^] | 505 | if (tunnel.fd4 < 0) { |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 506 | logmsg(ANDROID_LOG_FATAL, "tun_open4 failed: %s", strerror(errno)); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 507 | exit(1); |
| 508 | } |
| 509 | |
Jayachandran Chinnakkannu | f5fe267 | 2018-01-04 18:08:53 +0000 | [diff] [blame] | 510 | // When run from netd, the environment variable ANDROID_DNS_MODE is set to |
| 511 | // "local", but that only works for the netd process itself. Removing the |
| 512 | // following line causes XLAT failure in permissive mode. |
| 513 | unsetenv("ANDROID_DNS_MODE"); |
| 514 | |
Paul Jensen | 247dd71 | 2014-05-30 13:19:10 -0400 | [diff] [blame] | 515 | configure_interface(uplink_interface, plat_prefix, &tunnel, net_id); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 516 | |
Lorenzo Colitti | 1352a3a | 2014-10-21 13:41:21 +0900 | [diff] [blame] | 517 | update_clat_ipv6_address(&tunnel, uplink_interface); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 518 | |
Lorenzo Colitti | baf6299 | 2013-03-01 20:29:39 +0900 | [diff] [blame] | 519 | // Loop until someone sends us a signal or brings down the tun interface. |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame^] | 520 | if (signal(SIGTERM, stop_loop) == SIG_ERR) { |
Lorenzo Colitti | baf6299 | 2013-03-01 20:29:39 +0900 | [diff] [blame] | 521 | logmsg(ANDROID_LOG_FATAL, "sigterm handler failed: %s", strerror(errno)); |
| 522 | exit(1); |
| 523 | } |
Lorenzo Colitti | 8a41a5d | 2014-10-21 12:37:48 +0900 | [diff] [blame] | 524 | |
Lorenzo Colitti | baf6299 | 2013-03-01 20:29:39 +0900 | [diff] [blame] | 525 | event_loop(&tunnel); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 526 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame^] | 527 | logmsg(ANDROID_LOG_INFO, "Shutting down clat on %s", uplink_interface); |
Lorenzo Colitti | 8a41a5d | 2014-10-21 12:37:48 +0900 | [diff] [blame] | 528 | del_anycast_address(tunnel.write_fd6, &Global_Clatd_Config.ipv6_local_subnet); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 529 | |
| 530 | return 0; |
| 531 | } |