blob: d452985ba3a8dce54856f23bba741f4610fca044 [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"
34#include "setif.h"
Lorenzo Colittieb92f482019-01-04 14:59:11 +090035
36#define DEVICEPREFIX "v4-"
37
Maciej Żenczykowski8ab7e132021-02-03 17:15:41 -080038/* function: stop_loop
39 * signal handler: stop the event loop
40 */
41static void stop_loop() { running = 0; };
42
Lorenzo Colittieb92f482019-01-04 14:59:11 +090043/* function: print_help
44 * in case the user is running this on the command line
45 */
46void print_help() {
47 printf("android-clat arguments:\n");
48 printf("-i [uplink interface]\n");
49 printf("-p [plat prefix]\n");
Lorenzo Colittif0fac862019-01-11 18:10:11 +090050 printf("-4 [IPv4 address]\n");
51 printf("-6 [IPv6 address]\n");
Lorenzo Colittieb92f482019-01-04 14:59:11 +090052 printf("-m [socket mark]\n");
Maciej Żenczykowski716518d2019-04-08 17:46:48 -070053 printf("-t [tun file descriptor number]\n");
Lorenzo Colittieb92f482019-01-04 14:59:11 +090054}
55
56/* function: main
57 * allocate and setup the tun device, then run the event loop
58 */
59int main(int argc, char **argv) {
60 struct tun_data tunnel;
61 int opt;
Lorenzo Colitti27da0ad2020-06-01 12:15:20 +090062 char *uplink_interface = NULL, *plat_prefix = NULL, *mark_str = NULL;
Maciej Żenczykowski716518d2019-04-08 17:46:48 -070063 char *v4_addr = NULL, *v6_addr = NULL, *tunfd_str = NULL;
Lorenzo Colittieb92f482019-01-04 14:59:11 +090064 uint32_t mark = MARK_UNSET;
65 unsigned len;
66
Lorenzo Colitti27da0ad2020-06-01 12:15:20 +090067 while ((opt = getopt(argc, argv, "i:p:4:6:m:t:h")) != -1) {
Lorenzo Colittieb92f482019-01-04 14:59:11 +090068 switch (opt) {
69 case 'i':
70 uplink_interface = optarg;
71 break;
72 case 'p':
73 plat_prefix = optarg;
74 break;
Lorenzo Colittif0fac862019-01-11 18:10:11 +090075 case '4':
76 v4_addr = optarg;
77 break;
78 case '6':
79 v6_addr = optarg;
80 break;
Lorenzo Colittieb92f482019-01-04 14:59:11 +090081 case 'm':
82 mark_str = optarg;
83 break;
Maciej Żenczykowski716518d2019-04-08 17:46:48 -070084 case 't':
85 tunfd_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
Lorenzo Colittieb92f482019-01-04 14:59:11 +0900101 if (mark_str != NULL && !parse_unsigned(mark_str, &mark)) {
102 logmsg(ANDROID_LOG_FATAL, "invalid mark %s", mark_str);
103 exit(1);
104 }
105
Maciej Żenczykowski716518d2019-04-08 17:46:48 -0700106 if (tunfd_str != NULL && !parse_int(tunfd_str, &tunnel.fd4)) {
107 logmsg(ANDROID_LOG_FATAL, "invalid tunfd %s", tunfd_str);
108 exit(1);
109 }
110 if (!tunnel.fd4) {
111 logmsg(ANDROID_LOG_FATAL, "no tunfd specified on commandline.");
112 exit(1);
113 }
114
Lorenzo Colittieb92f482019-01-04 14:59:11 +0900115 len = snprintf(tunnel.device4, sizeof(tunnel.device4), "%s%s", DEVICEPREFIX, uplink_interface);
116 if (len >= sizeof(tunnel.device4)) {
117 logmsg(ANDROID_LOG_FATAL, "interface name too long '%s'", tunnel.device4);
118 exit(1);
119 }
120
Lorenzo Colitti27da0ad2020-06-01 12:15:20 +0900121 logmsg(ANDROID_LOG_INFO, "Starting clat version %s on %s mark=%s plat=%s v4=%s v6=%s",
122 CLATD_VERSION, uplink_interface, mark_str ? mark_str : "(none)",
123 plat_prefix ? plat_prefix : "(none)", v4_addr ? v4_addr : "(none)",
124 v6_addr ? v6_addr : "(none)");
Lorenzo Colittieb92f482019-01-04 14:59:11 +0900125
Lorenzo Colittieb92f482019-01-04 14:59:11 +0900126 // open our raw sockets before dropping privs
127 open_sockets(&tunnel, mark);
128
Lorenzo Colitti27da0ad2020-06-01 12:15:20 +0900129 configure_interface(uplink_interface, plat_prefix, v4_addr, v6_addr, &tunnel, mark);
Lorenzo Colittieb92f482019-01-04 14:59:11 +0900130
Maciej Żenczykowskib64249e2021-10-22 18:31:50 -0700131 // run under a regular user with no capabilities
132 drop_root_and_caps();
Lorenzo Colittieb92f482019-01-04 14:59:11 +0900133
134 // Loop until someone sends us a signal or brings down the tun interface.
135 if (signal(SIGTERM, stop_loop) == SIG_ERR) {
136 logmsg(ANDROID_LOG_FATAL, "sigterm handler failed: %s", strerror(errno));
137 exit(1);
138 }
139
140 event_loop(&tunnel);
141
142 logmsg(ANDROID_LOG_INFO, "Shutting down clat on %s", uplink_interface);
143 del_anycast_address(tunnel.write_fd6, &Global_Clatd_Config.ipv6_local_subnet);
144
Maciej Żenczykowski05b05412021-04-01 05:06:14 -0700145 close(tunnel.write_fd6);
146 close(tunnel.read_fd6);
147 close(tunnel.fd4);
148
149 if (running) {
150 logmsg(ANDROID_LOG_INFO, "Clatd on %s waiting for SIGTERM", uplink_interface);
151 while (running) sleep(60);
152 logmsg(ANDROID_LOG_INFO, "Clatd on %s received SIGTERM", uplink_interface);
153 } else {
154 logmsg(ANDROID_LOG_INFO, "Clatd on %s already received SIGTERM", uplink_interface);
155 }
Lorenzo Colittieb92f482019-01-04 14:59:11 +0900156 return 0;
157}