blob: 2ab865edb23a2d88081bef40a7786eeca5f57e7d [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>
Lorenzo Colittieb92f482019-01-04 14:59:11 +090025#include <unistd.h>
26
Lorenzo Colittieb92f482019-01-04 14:59:11 +090027#include "clatd.h"
28#include "common.h"
29#include "config.h"
30#include "logging.h"
Lorenzo Colittieb92f482019-01-04 14:59:11 +090031
32#define DEVICEPREFIX "v4-"
33
Maciej Żenczykowski8ab7e132021-02-03 17:15:41 -080034/* function: stop_loop
35 * signal handler: stop the event loop
36 */
37static void stop_loop() { running = 0; };
38
Lorenzo Colittieb92f482019-01-04 14:59:11 +090039/* function: print_help
40 * in case the user is running this on the command line
41 */
42void print_help() {
43 printf("android-clat arguments:\n");
44 printf("-i [uplink interface]\n");
45 printf("-p [plat prefix]\n");
Lorenzo Colittif0fac862019-01-11 18:10:11 +090046 printf("-4 [IPv4 address]\n");
47 printf("-6 [IPv6 address]\n");
Maciej Żenczykowski716518d2019-04-08 17:46:48 -070048 printf("-t [tun file descriptor number]\n");
Hungming Chen06367f32021-11-24 17:22:52 +080049 printf("-r [read socket descriptor number]\n");
Nucca Chen0714a182021-12-13 09:24:38 +000050 printf("-w [write socket descriptor number]\n");
Lorenzo Colittieb92f482019-01-04 14:59:11 +090051}
52
53/* function: main
54 * allocate and setup the tun device, then run the event loop
55 */
56int main(int argc, char **argv) {
57 struct tun_data tunnel;
58 int opt;
Hungming Chen9e718f42022-01-04 22:48:54 +080059 char *uplink_interface = NULL, *plat_prefix = NULL;
Hungming Chen06367f32021-11-24 17:22:52 +080060 char *v4_addr = NULL, *v6_addr = NULL, *tunfd_str = NULL, *read_sock_str = NULL,
61 *write_sock_str = NULL;
Lorenzo Colittieb92f482019-01-04 14:59:11 +090062 unsigned len;
63
Hungming Chen9e718f42022-01-04 22:48:54 +080064 while ((opt = getopt(argc, argv, "i:p:4:6:t:r:w:h")) != -1) {
Lorenzo Colittieb92f482019-01-04 14:59:11 +090065 switch (opt) {
66 case 'i':
67 uplink_interface = optarg;
68 break;
69 case 'p':
70 plat_prefix = optarg;
71 break;
Lorenzo Colittif0fac862019-01-11 18:10:11 +090072 case '4':
73 v4_addr = optarg;
74 break;
75 case '6':
76 v6_addr = optarg;
77 break;
Maciej Żenczykowski716518d2019-04-08 17:46:48 -070078 case 't':
79 tunfd_str = optarg;
80 break;
Hungming Chen06367f32021-11-24 17:22:52 +080081 case 'r':
82 read_sock_str = optarg;
83 break;
Nucca Chen0714a182021-12-13 09:24:38 +000084 case 'w':
85 write_sock_str = optarg;
86 break;
Lorenzo Colittieb92f482019-01-04 14:59:11 +090087 case 'h':
88 print_help();
89 exit(0);
90 default:
91 logmsg(ANDROID_LOG_FATAL, "Unknown option -%c. Exiting.", (char)optopt);
92 exit(1);
93 }
94 }
95
96 if (uplink_interface == NULL) {
97 logmsg(ANDROID_LOG_FATAL, "clatd called without an interface");
98 exit(1);
99 }
100
Maciej Żenczykowski716518d2019-04-08 17:46:48 -0700101 if (tunfd_str != NULL && !parse_int(tunfd_str, &tunnel.fd4)) {
102 logmsg(ANDROID_LOG_FATAL, "invalid tunfd %s", tunfd_str);
103 exit(1);
104 }
105 if (!tunnel.fd4) {
106 logmsg(ANDROID_LOG_FATAL, "no tunfd specified on commandline.");
107 exit(1);
108 }
109
Hungming Chen06367f32021-11-24 17:22:52 +0800110 if (read_sock_str != NULL && !parse_int(read_sock_str, &tunnel.read_fd6)) {
Hungming Chen9e718f42022-01-04 22:48:54 +0800111 logmsg(ANDROID_LOG_FATAL, "invalid read socket %s", read_sock_str);
Hungming Chen06367f32021-11-24 17:22:52 +0800112 exit(1);
113 }
114 if (!tunnel.read_fd6) {
115 logmsg(ANDROID_LOG_FATAL, "no read_fd6 specified on commandline.");
116 exit(1);
117 }
118
Nucca Chen0714a182021-12-13 09:24:38 +0000119 if (write_sock_str != NULL && !parse_int(write_sock_str, &tunnel.write_fd6)) {
Hungming Chen9e718f42022-01-04 22:48:54 +0800120 logmsg(ANDROID_LOG_FATAL, "invalid write socket %s", write_sock_str);
Nucca Chen0714a182021-12-13 09:24:38 +0000121 exit(1);
122 }
123 if (!tunnel.write_fd6) {
124 logmsg(ANDROID_LOG_FATAL, "no write_fd6 specified on commandline.");
125 exit(1);
126 }
127
Lorenzo Colittieb92f482019-01-04 14:59:11 +0900128 len = snprintf(tunnel.device4, sizeof(tunnel.device4), "%s%s", DEVICEPREFIX, uplink_interface);
129 if (len >= sizeof(tunnel.device4)) {
130 logmsg(ANDROID_LOG_FATAL, "interface name too long '%s'", tunnel.device4);
131 exit(1);
132 }
133
Hungming Chen5c112132021-11-25 09:40:17 +0800134 Global_Clatd_Config.native_ipv6_interface = uplink_interface;
135 if (!plat_prefix || inet_pton(AF_INET6, plat_prefix, &Global_Clatd_Config.plat_subnet) <= 0) {
136 logmsg(ANDROID_LOG_FATAL, "invalid IPv6 address specified for plat prefix: %s", plat_prefix);
137 exit(1);
138 }
139
Hungming Chen5dafb0e2021-11-24 20:19:43 +0800140 if (!v4_addr || !inet_pton(AF_INET, v4_addr, &Global_Clatd_Config.ipv4_local_subnet.s_addr)) {
141 logmsg(ANDROID_LOG_FATAL, "Invalid IPv4 address %s", v4_addr);
142 exit(1);
143 }
144
Hungming Chen5c112132021-11-25 09:40:17 +0800145 if (!v6_addr || !inet_pton(AF_INET6, v6_addr, &Global_Clatd_Config.ipv6_local_subnet)) {
146 logmsg(ANDROID_LOG_FATAL, "Invalid source address %s", v6_addr);
147 exit(1);
148 }
149
Hungming Chen9e718f42022-01-04 22:48:54 +0800150 logmsg(ANDROID_LOG_INFO, "Starting clat version %s on %s plat=%s v4=%s v6=%s", CLATD_VERSION,
151 uplink_interface, plat_prefix ? plat_prefix : "(none)", v4_addr ? v4_addr : "(none)",
Lorenzo Colitti27da0ad2020-06-01 12:15:20 +0900152 v6_addr ? v6_addr : "(none)");
Lorenzo Colittieb92f482019-01-04 14:59:11 +0900153
Lorenzo Colittieb92f482019-01-04 14:59:11 +0900154 // Loop until someone sends us a signal or brings down the tun interface.
155 if (signal(SIGTERM, stop_loop) == SIG_ERR) {
156 logmsg(ANDROID_LOG_FATAL, "sigterm handler failed: %s", strerror(errno));
157 exit(1);
158 }
159
160 event_loop(&tunnel);
161
162 logmsg(ANDROID_LOG_INFO, "Shutting down clat on %s", uplink_interface);
Maciej Żenczykowski05b05412021-04-01 05:06:14 -0700163
164 if (running) {
165 logmsg(ANDROID_LOG_INFO, "Clatd on %s waiting for SIGTERM", uplink_interface);
166 while (running) sleep(60);
167 logmsg(ANDROID_LOG_INFO, "Clatd on %s received SIGTERM", uplink_interface);
168 } else {
169 logmsg(ANDROID_LOG_INFO, "Clatd on %s already received SIGTERM", uplink_interface);
170 }
Lorenzo Colittieb92f482019-01-04 14:59:11 +0900171 return 0;
172}