Lorenzo Colitti | eb92f48 | 2019-01-04 14:59:11 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 The Android Open Source Project |
| 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 | * main.c - main function |
| 17 | */ |
| 18 | |
Lorenzo Colitti | 27da0ad | 2020-06-01 12:15:20 +0900 | [diff] [blame] | 19 | #include <arpa/inet.h> |
Lorenzo Colitti | eb92f48 | 2019-01-04 14:59:11 +0900 | [diff] [blame] | 20 | #include <errno.h> |
| 21 | #include <netinet/in.h> |
| 22 | #include <stdint.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <sys/capability.h> |
| 26 | #include <unistd.h> |
| 27 | |
Lorenzo Colitti | eb92f48 | 2019-01-04 14:59:11 +0900 | [diff] [blame] | 28 | #include "clatd.h" |
| 29 | #include "common.h" |
| 30 | #include "config.h" |
| 31 | #include "logging.h" |
Lorenzo Colitti | eb92f48 | 2019-01-04 14:59:11 +0900 | [diff] [blame] | 32 | |
| 33 | #define DEVICEPREFIX "v4-" |
| 34 | |
Hungming Chen | 935a5ee | 2021-12-16 12:56:36 +0800 | [diff] [blame] | 35 | // Sync MARK_UNSET from netid_client.h |
| 36 | // TODO: remove MARK_UNSET definition once argument "-m" is removed. |
| 37 | #define MARK_UNSET 0u |
| 38 | |
Maciej Żenczykowski | 8ab7e13 | 2021-02-03 17:15:41 -0800 | [diff] [blame] | 39 | /* function: stop_loop |
| 40 | * signal handler: stop the event loop |
| 41 | */ |
| 42 | static void stop_loop() { running = 0; }; |
| 43 | |
Lorenzo Colitti | eb92f48 | 2019-01-04 14:59:11 +0900 | [diff] [blame] | 44 | /* function: print_help |
| 45 | * in case the user is running this on the command line |
| 46 | */ |
| 47 | void print_help() { |
| 48 | printf("android-clat arguments:\n"); |
| 49 | printf("-i [uplink interface]\n"); |
| 50 | printf("-p [plat prefix]\n"); |
Lorenzo Colitti | f0fac86 | 2019-01-11 18:10:11 +0900 | [diff] [blame] | 51 | printf("-4 [IPv4 address]\n"); |
| 52 | printf("-6 [IPv6 address]\n"); |
Lorenzo Colitti | eb92f48 | 2019-01-04 14:59:11 +0900 | [diff] [blame] | 53 | printf("-m [socket mark]\n"); |
Maciej Żenczykowski | 716518d | 2019-04-08 17:46:48 -0700 | [diff] [blame] | 54 | printf("-t [tun file descriptor number]\n"); |
Hungming Chen | 06367f3 | 2021-11-24 17:22:52 +0800 | [diff] [blame] | 55 | printf("-r [read socket descriptor number]\n"); |
Nucca Chen | 0714a18 | 2021-12-13 09:24:38 +0000 | [diff] [blame] | 56 | printf("-w [write socket descriptor number]\n"); |
Lorenzo Colitti | eb92f48 | 2019-01-04 14:59:11 +0900 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | /* function: main |
| 60 | * allocate and setup the tun device, then run the event loop |
| 61 | */ |
| 62 | int main(int argc, char **argv) { |
| 63 | struct tun_data tunnel; |
| 64 | int opt; |
Lorenzo Colitti | 27da0ad | 2020-06-01 12:15:20 +0900 | [diff] [blame] | 65 | char *uplink_interface = NULL, *plat_prefix = NULL, *mark_str = NULL; |
Hungming Chen | 06367f3 | 2021-11-24 17:22:52 +0800 | [diff] [blame] | 66 | char *v4_addr = NULL, *v6_addr = NULL, *tunfd_str = NULL, *read_sock_str = NULL, |
| 67 | *write_sock_str = NULL; |
Lorenzo Colitti | eb92f48 | 2019-01-04 14:59:11 +0900 | [diff] [blame] | 68 | uint32_t mark = MARK_UNSET; |
| 69 | unsigned len; |
| 70 | |
Hungming Chen | 06367f3 | 2021-11-24 17:22:52 +0800 | [diff] [blame] | 71 | while ((opt = getopt(argc, argv, "i:p:4:6:m:t:r:w:h")) != -1) { |
Lorenzo Colitti | eb92f48 | 2019-01-04 14:59:11 +0900 | [diff] [blame] | 72 | switch (opt) { |
| 73 | case 'i': |
| 74 | uplink_interface = optarg; |
| 75 | break; |
| 76 | case 'p': |
| 77 | plat_prefix = optarg; |
| 78 | break; |
Lorenzo Colitti | f0fac86 | 2019-01-11 18:10:11 +0900 | [diff] [blame] | 79 | case '4': |
| 80 | v4_addr = optarg; |
| 81 | break; |
| 82 | case '6': |
| 83 | v6_addr = optarg; |
| 84 | break; |
Lorenzo Colitti | eb92f48 | 2019-01-04 14:59:11 +0900 | [diff] [blame] | 85 | case 'm': |
| 86 | mark_str = optarg; |
| 87 | break; |
Maciej Żenczykowski | 716518d | 2019-04-08 17:46:48 -0700 | [diff] [blame] | 88 | case 't': |
| 89 | tunfd_str = optarg; |
| 90 | break; |
Hungming Chen | 06367f3 | 2021-11-24 17:22:52 +0800 | [diff] [blame] | 91 | case 'r': |
| 92 | read_sock_str = optarg; |
| 93 | break; |
Nucca Chen | 0714a18 | 2021-12-13 09:24:38 +0000 | [diff] [blame] | 94 | case 'w': |
| 95 | write_sock_str = optarg; |
| 96 | break; |
Lorenzo Colitti | eb92f48 | 2019-01-04 14:59:11 +0900 | [diff] [blame] | 97 | case 'h': |
| 98 | print_help(); |
| 99 | exit(0); |
| 100 | default: |
| 101 | logmsg(ANDROID_LOG_FATAL, "Unknown option -%c. Exiting.", (char)optopt); |
| 102 | exit(1); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | if (uplink_interface == NULL) { |
| 107 | logmsg(ANDROID_LOG_FATAL, "clatd called without an interface"); |
| 108 | exit(1); |
| 109 | } |
| 110 | |
Lorenzo Colitti | eb92f48 | 2019-01-04 14:59:11 +0900 | [diff] [blame] | 111 | if (mark_str != NULL && !parse_unsigned(mark_str, &mark)) { |
| 112 | logmsg(ANDROID_LOG_FATAL, "invalid mark %s", mark_str); |
| 113 | exit(1); |
| 114 | } |
| 115 | |
Maciej Żenczykowski | 716518d | 2019-04-08 17:46:48 -0700 | [diff] [blame] | 116 | if (tunfd_str != NULL && !parse_int(tunfd_str, &tunnel.fd4)) { |
| 117 | logmsg(ANDROID_LOG_FATAL, "invalid tunfd %s", tunfd_str); |
| 118 | exit(1); |
| 119 | } |
| 120 | if (!tunnel.fd4) { |
| 121 | logmsg(ANDROID_LOG_FATAL, "no tunfd specified on commandline."); |
| 122 | exit(1); |
| 123 | } |
| 124 | |
Hungming Chen | 06367f3 | 2021-11-24 17:22:52 +0800 | [diff] [blame] | 125 | if (read_sock_str != NULL && !parse_int(read_sock_str, &tunnel.read_fd6)) { |
| 126 | logmsg(ANDROID_LOG_FATAL, "invalid sock_write %s", read_sock_str); |
| 127 | exit(1); |
| 128 | } |
| 129 | if (!tunnel.read_fd6) { |
| 130 | logmsg(ANDROID_LOG_FATAL, "no read_fd6 specified on commandline."); |
| 131 | exit(1); |
| 132 | } |
| 133 | |
Nucca Chen | 0714a18 | 2021-12-13 09:24:38 +0000 | [diff] [blame] | 134 | if (write_sock_str != NULL && !parse_int(write_sock_str, &tunnel.write_fd6)) { |
| 135 | logmsg(ANDROID_LOG_FATAL, "invalid sock_write %s", write_sock_str); |
| 136 | exit(1); |
| 137 | } |
| 138 | if (!tunnel.write_fd6) { |
| 139 | logmsg(ANDROID_LOG_FATAL, "no write_fd6 specified on commandline."); |
| 140 | exit(1); |
| 141 | } |
| 142 | |
Lorenzo Colitti | eb92f48 | 2019-01-04 14:59:11 +0900 | [diff] [blame] | 143 | len = snprintf(tunnel.device4, sizeof(tunnel.device4), "%s%s", DEVICEPREFIX, uplink_interface); |
| 144 | if (len >= sizeof(tunnel.device4)) { |
| 145 | logmsg(ANDROID_LOG_FATAL, "interface name too long '%s'", tunnel.device4); |
| 146 | exit(1); |
| 147 | } |
| 148 | |
Hungming Chen | 5c11213 | 2021-11-25 09:40:17 +0800 | [diff] [blame] | 149 | Global_Clatd_Config.native_ipv6_interface = uplink_interface; |
| 150 | if (!plat_prefix || inet_pton(AF_INET6, plat_prefix, &Global_Clatd_Config.plat_subnet) <= 0) { |
| 151 | logmsg(ANDROID_LOG_FATAL, "invalid IPv6 address specified for plat prefix: %s", plat_prefix); |
| 152 | exit(1); |
| 153 | } |
| 154 | |
Hungming Chen | 5dafb0e | 2021-11-24 20:19:43 +0800 | [diff] [blame] | 155 | if (!v4_addr || !inet_pton(AF_INET, v4_addr, &Global_Clatd_Config.ipv4_local_subnet.s_addr)) { |
| 156 | logmsg(ANDROID_LOG_FATAL, "Invalid IPv4 address %s", v4_addr); |
| 157 | exit(1); |
| 158 | } |
| 159 | |
Hungming Chen | 5c11213 | 2021-11-25 09:40:17 +0800 | [diff] [blame] | 160 | if (!v6_addr || !inet_pton(AF_INET6, v6_addr, &Global_Clatd_Config.ipv6_local_subnet)) { |
| 161 | logmsg(ANDROID_LOG_FATAL, "Invalid source address %s", v6_addr); |
| 162 | exit(1); |
| 163 | } |
| 164 | |
Lorenzo Colitti | 27da0ad | 2020-06-01 12:15:20 +0900 | [diff] [blame] | 165 | logmsg(ANDROID_LOG_INFO, "Starting clat version %s on %s mark=%s plat=%s v4=%s v6=%s", |
| 166 | CLATD_VERSION, uplink_interface, mark_str ? mark_str : "(none)", |
| 167 | plat_prefix ? plat_prefix : "(none)", v4_addr ? v4_addr : "(none)", |
| 168 | v6_addr ? v6_addr : "(none)"); |
Lorenzo Colitti | eb92f48 | 2019-01-04 14:59:11 +0900 | [diff] [blame] | 169 | |
Maciej Żenczykowski | b64249e | 2021-10-22 18:31:50 -0700 | [diff] [blame] | 170 | // run under a regular user with no capabilities |
| 171 | drop_root_and_caps(); |
Lorenzo Colitti | eb92f48 | 2019-01-04 14:59:11 +0900 | [diff] [blame] | 172 | |
| 173 | // Loop until someone sends us a signal or brings down the tun interface. |
| 174 | if (signal(SIGTERM, stop_loop) == SIG_ERR) { |
| 175 | logmsg(ANDROID_LOG_FATAL, "sigterm handler failed: %s", strerror(errno)); |
| 176 | exit(1); |
| 177 | } |
| 178 | |
| 179 | event_loop(&tunnel); |
| 180 | |
| 181 | logmsg(ANDROID_LOG_INFO, "Shutting down clat on %s", uplink_interface); |
Maciej Żenczykowski | 05b0541 | 2021-04-01 05:06:14 -0700 | [diff] [blame] | 182 | |
| 183 | if (running) { |
| 184 | logmsg(ANDROID_LOG_INFO, "Clatd on %s waiting for SIGTERM", uplink_interface); |
| 185 | while (running) sleep(60); |
| 186 | logmsg(ANDROID_LOG_INFO, "Clatd on %s received SIGTERM", uplink_interface); |
| 187 | } else { |
| 188 | logmsg(ANDROID_LOG_INFO, "Clatd on %s already received SIGTERM", uplink_interface); |
| 189 | } |
Lorenzo Colitti | eb92f48 | 2019-01-04 14:59:11 +0900 | [diff] [blame] | 190 | return 0; |
| 191 | } |