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" |
| 35 | #include "tun.h" |
| 36 | |
| 37 | #define DEVICEPREFIX "v4-" |
| 38 | |
| 39 | /* function: print_help |
| 40 | * in case the user is running this on the command line |
| 41 | */ |
| 42 | void print_help() { |
| 43 | printf("android-clat arguments:\n"); |
| 44 | printf("-i [uplink interface]\n"); |
| 45 | printf("-p [plat prefix]\n"); |
Lorenzo Colitti | f0fac86 | 2019-01-11 18:10:11 +0900 | [diff] [blame] | 46 | printf("-4 [IPv4 address]\n"); |
| 47 | printf("-6 [IPv6 address]\n"); |
Lorenzo Colitti | eb92f48 | 2019-01-04 14:59:11 +0900 | [diff] [blame] | 48 | printf("-m [socket mark]\n"); |
Maciej Żenczykowski | 716518d | 2019-04-08 17:46:48 -0700 | [diff] [blame] | 49 | printf("-t [tun file descriptor number]\n"); |
Lorenzo Colitti | eb92f48 | 2019-01-04 14:59:11 +0900 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | /* function: main |
| 53 | * allocate and setup the tun device, then run the event loop |
| 54 | */ |
| 55 | int main(int argc, char **argv) { |
| 56 | struct tun_data tunnel; |
| 57 | int opt; |
Lorenzo Colitti | 27da0ad | 2020-06-01 12:15:20 +0900 | [diff] [blame^] | 58 | char *uplink_interface = NULL, *plat_prefix = NULL, *mark_str = NULL; |
Maciej Żenczykowski | 716518d | 2019-04-08 17:46:48 -0700 | [diff] [blame] | 59 | char *v4_addr = NULL, *v6_addr = NULL, *tunfd_str = NULL; |
Lorenzo Colitti | eb92f48 | 2019-01-04 14:59:11 +0900 | [diff] [blame] | 60 | uint32_t mark = MARK_UNSET; |
| 61 | unsigned len; |
| 62 | |
Lorenzo Colitti | 27da0ad | 2020-06-01 12:15:20 +0900 | [diff] [blame^] | 63 | while ((opt = getopt(argc, argv, "i:p:4:6:m:t:h")) != -1) { |
Lorenzo Colitti | eb92f48 | 2019-01-04 14:59:11 +0900 | [diff] [blame] | 64 | switch (opt) { |
| 65 | case 'i': |
| 66 | uplink_interface = optarg; |
| 67 | break; |
| 68 | case 'p': |
| 69 | plat_prefix = optarg; |
| 70 | break; |
Lorenzo Colitti | f0fac86 | 2019-01-11 18:10:11 +0900 | [diff] [blame] | 71 | case '4': |
| 72 | v4_addr = optarg; |
| 73 | break; |
| 74 | case '6': |
| 75 | v6_addr = optarg; |
| 76 | break; |
Lorenzo Colitti | eb92f48 | 2019-01-04 14:59:11 +0900 | [diff] [blame] | 77 | case 'm': |
| 78 | mark_str = optarg; |
| 79 | break; |
Maciej Żenczykowski | 716518d | 2019-04-08 17:46:48 -0700 | [diff] [blame] | 80 | case 't': |
| 81 | tunfd_str = optarg; |
| 82 | break; |
Lorenzo Colitti | eb92f48 | 2019-01-04 14:59:11 +0900 | [diff] [blame] | 83 | case 'h': |
| 84 | print_help(); |
| 85 | exit(0); |
| 86 | default: |
| 87 | logmsg(ANDROID_LOG_FATAL, "Unknown option -%c. Exiting.", (char)optopt); |
| 88 | exit(1); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | if (uplink_interface == NULL) { |
| 93 | logmsg(ANDROID_LOG_FATAL, "clatd called without an interface"); |
| 94 | exit(1); |
| 95 | } |
| 96 | |
Lorenzo Colitti | eb92f48 | 2019-01-04 14:59:11 +0900 | [diff] [blame] | 97 | if (mark_str != NULL && !parse_unsigned(mark_str, &mark)) { |
| 98 | logmsg(ANDROID_LOG_FATAL, "invalid mark %s", mark_str); |
| 99 | exit(1); |
| 100 | } |
| 101 | |
Maciej Żenczykowski | 716518d | 2019-04-08 17:46:48 -0700 | [diff] [blame] | 102 | if (tunfd_str != NULL && !parse_int(tunfd_str, &tunnel.fd4)) { |
| 103 | logmsg(ANDROID_LOG_FATAL, "invalid tunfd %s", tunfd_str); |
| 104 | exit(1); |
| 105 | } |
| 106 | if (!tunnel.fd4) { |
| 107 | logmsg(ANDROID_LOG_FATAL, "no tunfd specified on commandline."); |
| 108 | exit(1); |
| 109 | } |
| 110 | |
Lorenzo Colitti | eb92f48 | 2019-01-04 14:59:11 +0900 | [diff] [blame] | 111 | len = snprintf(tunnel.device4, sizeof(tunnel.device4), "%s%s", DEVICEPREFIX, uplink_interface); |
| 112 | if (len >= sizeof(tunnel.device4)) { |
| 113 | logmsg(ANDROID_LOG_FATAL, "interface name too long '%s'", tunnel.device4); |
| 114 | exit(1); |
| 115 | } |
| 116 | |
Lorenzo Colitti | 27da0ad | 2020-06-01 12:15:20 +0900 | [diff] [blame^] | 117 | logmsg(ANDROID_LOG_INFO, "Starting clat version %s on %s mark=%s plat=%s v4=%s v6=%s", |
| 118 | CLATD_VERSION, uplink_interface, mark_str ? mark_str : "(none)", |
| 119 | plat_prefix ? plat_prefix : "(none)", v4_addr ? v4_addr : "(none)", |
| 120 | v6_addr ? v6_addr : "(none)"); |
Lorenzo Colitti | eb92f48 | 2019-01-04 14:59:11 +0900 | [diff] [blame] | 121 | |
| 122 | // run under a regular user but keep needed capabilities |
| 123 | drop_root_but_keep_caps(); |
| 124 | |
| 125 | // open our raw sockets before dropping privs |
| 126 | open_sockets(&tunnel, mark); |
| 127 | |
| 128 | // keeps only admin capability |
| 129 | set_capability(1 << CAP_NET_ADMIN); |
| 130 | |
Lorenzo Colitti | 27da0ad | 2020-06-01 12:15:20 +0900 | [diff] [blame^] | 131 | configure_interface(uplink_interface, plat_prefix, v4_addr, v6_addr, &tunnel, mark); |
Lorenzo Colitti | eb92f48 | 2019-01-04 14:59:11 +0900 | [diff] [blame] | 132 | |
Lorenzo Colitti | 66deecd | 2019-01-04 12:27:27 +0900 | [diff] [blame] | 133 | // Drop all remaining capabilities. |
| 134 | set_capability(0); |
Lorenzo Colitti | eb92f48 | 2019-01-04 14:59:11 +0900 | [diff] [blame] | 135 | |
| 136 | // Loop until someone sends us a signal or brings down the tun interface. |
| 137 | if (signal(SIGTERM, stop_loop) == SIG_ERR) { |
| 138 | logmsg(ANDROID_LOG_FATAL, "sigterm handler failed: %s", strerror(errno)); |
| 139 | exit(1); |
| 140 | } |
| 141 | |
| 142 | event_loop(&tunnel); |
| 143 | |
| 144 | logmsg(ANDROID_LOG_INFO, "Shutting down clat on %s", uplink_interface); |
| 145 | del_anycast_address(tunnel.write_fd6, &Global_Clatd_Config.ipv6_local_subnet); |
| 146 | |
| 147 | return 0; |
| 148 | } |