blob: 74fb0c2cb2d3cdaf8b46c3e393ff7fecf0a9591a [file] [log] [blame]
Lorenzo Colittieb92f482019-01-04 14:59:11 +09001/*
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 Colitti27da0ad2020-06-01 12:15:20 +090019#include <arpa/inet.h>
Lorenzo Colittieb92f482019-01-04 14:59:11 +090020#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 Colitti27da0ad2020-06-01 12:15:20 +090028#include <netid_client.h> // For MARK_UNSET.
Lorenzo Colittieb92f482019-01-04 14:59:11 +090029
30#include "clatd.h"
31#include "common.h"
32#include "config.h"
33#include "logging.h"
Lorenzo Colittieb92f482019-01-04 14:59:11 +090034
35#define DEVICEPREFIX "v4-"
36
Maciej Żenczykowski8ab7e132021-02-03 17:15:41 -080037/* function: stop_loop
38 * signal handler: stop the event loop
39 */
40static void stop_loop() { running = 0; };
41
Lorenzo Colittieb92f482019-01-04 14:59:11 +090042/* function: print_help
43 * in case the user is running this on the command line
44 */
45void print_help() {
46 printf("android-clat arguments:\n");
47 printf("-i [uplink interface]\n");
48 printf("-p [plat prefix]\n");
Lorenzo Colittif0fac862019-01-11 18:10:11 +090049 printf("-4 [IPv4 address]\n");
50 printf("-6 [IPv6 address]\n");
Lorenzo Colittieb92f482019-01-04 14:59:11 +090051 printf("-m [socket mark]\n");
Maciej Żenczykowski716518d2019-04-08 17:46:48 -070052 printf("-t [tun file descriptor number]\n");
Hungming Chen06367f32021-11-24 17:22:52 +080053 printf("-r [read socket descriptor number]\n");
Nucca Chen0714a182021-12-13 09:24:38 +000054 printf("-w [write socket descriptor number]\n");
Lorenzo Colittieb92f482019-01-04 14:59:11 +090055}
56
57/* function: main
58 * allocate and setup the tun device, then run the event loop
59 */
60int main(int argc, char **argv) {
61 struct tun_data tunnel;
62 int opt;
Lorenzo Colitti27da0ad2020-06-01 12:15:20 +090063 char *uplink_interface = NULL, *plat_prefix = NULL, *mark_str = NULL;
Hungming Chen06367f32021-11-24 17:22:52 +080064 char *v4_addr = NULL, *v6_addr = NULL, *tunfd_str = NULL, *read_sock_str = NULL,
65 *write_sock_str = NULL;
Lorenzo Colittieb92f482019-01-04 14:59:11 +090066 uint32_t mark = MARK_UNSET;
67 unsigned len;
68
Hungming Chen06367f32021-11-24 17:22:52 +080069 while ((opt = getopt(argc, argv, "i:p:4:6:m:t:r:w:h")) != -1) {
Lorenzo Colittieb92f482019-01-04 14:59:11 +090070 switch (opt) {
71 case 'i':
72 uplink_interface = optarg;
73 break;
74 case 'p':
75 plat_prefix = optarg;
76 break;
Lorenzo Colittif0fac862019-01-11 18:10:11 +090077 case '4':
78 v4_addr = optarg;
79 break;
80 case '6':
81 v6_addr = optarg;
82 break;
Lorenzo Colittieb92f482019-01-04 14:59:11 +090083 case 'm':
84 mark_str = optarg;
85 break;
Maciej Żenczykowski716518d2019-04-08 17:46:48 -070086 case 't':
87 tunfd_str = optarg;
88 break;
Hungming Chen06367f32021-11-24 17:22:52 +080089 case 'r':
90 read_sock_str = optarg;
91 break;
Nucca Chen0714a182021-12-13 09:24:38 +000092 case 'w':
93 write_sock_str = optarg;
94 break;
Lorenzo Colittieb92f482019-01-04 14:59:11 +090095 case 'h':
96 print_help();
97 exit(0);
98 default:
99 logmsg(ANDROID_LOG_FATAL, "Unknown option -%c. Exiting.", (char)optopt);
100 exit(1);
101 }
102 }
103
104 if (uplink_interface == NULL) {
105 logmsg(ANDROID_LOG_FATAL, "clatd called without an interface");
106 exit(1);
107 }
108
Lorenzo Colittieb92f482019-01-04 14:59:11 +0900109 if (mark_str != NULL && !parse_unsigned(mark_str, &mark)) {
110 logmsg(ANDROID_LOG_FATAL, "invalid mark %s", mark_str);
111 exit(1);
112 }
113
Maciej Żenczykowski716518d2019-04-08 17:46:48 -0700114 if (tunfd_str != NULL && !parse_int(tunfd_str, &tunnel.fd4)) {
115 logmsg(ANDROID_LOG_FATAL, "invalid tunfd %s", tunfd_str);
116 exit(1);
117 }
118 if (!tunnel.fd4) {
119 logmsg(ANDROID_LOG_FATAL, "no tunfd specified on commandline.");
120 exit(1);
121 }
122
Hungming Chen06367f32021-11-24 17:22:52 +0800123 if (read_sock_str != NULL && !parse_int(read_sock_str, &tunnel.read_fd6)) {
124 logmsg(ANDROID_LOG_FATAL, "invalid sock_write %s", read_sock_str);
125 exit(1);
126 }
127 if (!tunnel.read_fd6) {
128 logmsg(ANDROID_LOG_FATAL, "no read_fd6 specified on commandline.");
129 exit(1);
130 }
131
Nucca Chen0714a182021-12-13 09:24:38 +0000132 if (write_sock_str != NULL && !parse_int(write_sock_str, &tunnel.write_fd6)) {
133 logmsg(ANDROID_LOG_FATAL, "invalid sock_write %s", write_sock_str);
134 exit(1);
135 }
136 if (!tunnel.write_fd6) {
137 logmsg(ANDROID_LOG_FATAL, "no write_fd6 specified on commandline.");
138 exit(1);
139 }
140
Lorenzo Colittieb92f482019-01-04 14:59:11 +0900141 len = snprintf(tunnel.device4, sizeof(tunnel.device4), "%s%s", DEVICEPREFIX, uplink_interface);
142 if (len >= sizeof(tunnel.device4)) {
143 logmsg(ANDROID_LOG_FATAL, "interface name too long '%s'", tunnel.device4);
144 exit(1);
145 }
146
Hungming Chen5c112132021-11-25 09:40:17 +0800147 Global_Clatd_Config.native_ipv6_interface = uplink_interface;
148 if (!plat_prefix || inet_pton(AF_INET6, plat_prefix, &Global_Clatd_Config.plat_subnet) <= 0) {
149 logmsg(ANDROID_LOG_FATAL, "invalid IPv6 address specified for plat prefix: %s", plat_prefix);
150 exit(1);
151 }
152
Hungming Chen5dafb0e2021-11-24 20:19:43 +0800153 if (!v4_addr || !inet_pton(AF_INET, v4_addr, &Global_Clatd_Config.ipv4_local_subnet.s_addr)) {
154 logmsg(ANDROID_LOG_FATAL, "Invalid IPv4 address %s", v4_addr);
155 exit(1);
156 }
157
Hungming Chen5c112132021-11-25 09:40:17 +0800158 if (!v6_addr || !inet_pton(AF_INET6, v6_addr, &Global_Clatd_Config.ipv6_local_subnet)) {
159 logmsg(ANDROID_LOG_FATAL, "Invalid source address %s", v6_addr);
160 exit(1);
161 }
162
Lorenzo Colitti27da0ad2020-06-01 12:15:20 +0900163 logmsg(ANDROID_LOG_INFO, "Starting clat version %s on %s mark=%s plat=%s v4=%s v6=%s",
164 CLATD_VERSION, uplink_interface, mark_str ? mark_str : "(none)",
165 plat_prefix ? plat_prefix : "(none)", v4_addr ? v4_addr : "(none)",
166 v6_addr ? v6_addr : "(none)");
Lorenzo Colittieb92f482019-01-04 14:59:11 +0900167
Maciej Żenczykowskib64249e2021-10-22 18:31:50 -0700168 // run under a regular user with no capabilities
169 drop_root_and_caps();
Lorenzo Colittieb92f482019-01-04 14:59:11 +0900170
171 // Loop until someone sends us a signal or brings down the tun interface.
172 if (signal(SIGTERM, stop_loop) == SIG_ERR) {
173 logmsg(ANDROID_LOG_FATAL, "sigterm handler failed: %s", strerror(errno));
174 exit(1);
175 }
176
177 event_loop(&tunnel);
178
179 logmsg(ANDROID_LOG_INFO, "Shutting down clat on %s", uplink_interface);
Maciej Żenczykowski05b05412021-04-01 05:06:14 -0700180
181 if (running) {
182 logmsg(ANDROID_LOG_INFO, "Clatd on %s waiting for SIGTERM", uplink_interface);
183 while (running) sleep(60);
184 logmsg(ANDROID_LOG_INFO, "Clatd on %s received SIGTERM", uplink_interface);
185 } else {
186 logmsg(ANDROID_LOG_INFO, "Clatd on %s already received SIGTERM", uplink_interface);
187 }
Lorenzo Colittieb92f482019-01-04 14:59:11 +0900188 return 0;
189}