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