blob: 683b50752dddbeeace424af262a6f2c4057034ad [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
38/* function: print_help
39 * in case the user is running this on the command line
40 */
41void print_help() {
42 printf("android-clat arguments:\n");
43 printf("-i [uplink interface]\n");
44 printf("-p [plat prefix]\n");
Lorenzo Colittif0fac862019-01-11 18:10:11 +090045 printf("-4 [IPv4 address]\n");
46 printf("-6 [IPv6 address]\n");
Lorenzo Colittieb92f482019-01-04 14:59:11 +090047 printf("-m [socket mark]\n");
Maciej Żenczykowski716518d2019-04-08 17:46:48 -070048 printf("-t [tun file descriptor number]\n");
Lorenzo Colittieb92f482019-01-04 14:59:11 +090049}
50
51/* function: main
52 * allocate and setup the tun device, then run the event loop
53 */
54int main(int argc, char **argv) {
55 struct tun_data tunnel;
56 int opt;
Lorenzo Colitti27da0ad2020-06-01 12:15:20 +090057 char *uplink_interface = NULL, *plat_prefix = NULL, *mark_str = NULL;
Maciej Żenczykowski716518d2019-04-08 17:46:48 -070058 char *v4_addr = NULL, *v6_addr = NULL, *tunfd_str = NULL;
Lorenzo Colittieb92f482019-01-04 14:59:11 +090059 uint32_t mark = MARK_UNSET;
60 unsigned len;
61
Lorenzo Colitti27da0ad2020-06-01 12:15:20 +090062 while ((opt = getopt(argc, argv, "i:p:4:6:m:t:h")) != -1) {
Lorenzo Colittieb92f482019-01-04 14:59:11 +090063 switch (opt) {
64 case 'i':
65 uplink_interface = optarg;
66 break;
67 case 'p':
68 plat_prefix = optarg;
69 break;
Lorenzo Colittif0fac862019-01-11 18:10:11 +090070 case '4':
71 v4_addr = optarg;
72 break;
73 case '6':
74 v6_addr = optarg;
75 break;
Lorenzo Colittieb92f482019-01-04 14:59:11 +090076 case 'm':
77 mark_str = optarg;
78 break;
Maciej Żenczykowski716518d2019-04-08 17:46:48 -070079 case 't':
80 tunfd_str = optarg;
81 break;
Lorenzo Colittieb92f482019-01-04 14:59:11 +090082 case 'h':
83 print_help();
84 exit(0);
85 default:
86 logmsg(ANDROID_LOG_FATAL, "Unknown option -%c. Exiting.", (char)optopt);
87 exit(1);
88 }
89 }
90
91 if (uplink_interface == NULL) {
92 logmsg(ANDROID_LOG_FATAL, "clatd called without an interface");
93 exit(1);
94 }
95
Lorenzo Colittieb92f482019-01-04 14:59:11 +090096 if (mark_str != NULL && !parse_unsigned(mark_str, &mark)) {
97 logmsg(ANDROID_LOG_FATAL, "invalid mark %s", mark_str);
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
Lorenzo Colittieb92f482019-01-04 14:59:11 +0900110 len = snprintf(tunnel.device4, sizeof(tunnel.device4), "%s%s", DEVICEPREFIX, uplink_interface);
111 if (len >= sizeof(tunnel.device4)) {
112 logmsg(ANDROID_LOG_FATAL, "interface name too long '%s'", tunnel.device4);
113 exit(1);
114 }
115
Lorenzo Colitti27da0ad2020-06-01 12:15:20 +0900116 logmsg(ANDROID_LOG_INFO, "Starting clat version %s on %s mark=%s plat=%s v4=%s v6=%s",
117 CLATD_VERSION, uplink_interface, mark_str ? mark_str : "(none)",
118 plat_prefix ? plat_prefix : "(none)", v4_addr ? v4_addr : "(none)",
119 v6_addr ? v6_addr : "(none)");
Lorenzo Colittieb92f482019-01-04 14:59:11 +0900120
121 // run under a regular user but keep needed capabilities
122 drop_root_but_keep_caps();
123
124 // open our raw sockets before dropping privs
125 open_sockets(&tunnel, mark);
126
127 // keeps only admin capability
128 set_capability(1 << CAP_NET_ADMIN);
129
Lorenzo Colitti27da0ad2020-06-01 12:15:20 +0900130 configure_interface(uplink_interface, plat_prefix, v4_addr, v6_addr, &tunnel, mark);
Lorenzo Colittieb92f482019-01-04 14:59:11 +0900131
Lorenzo Colitti66deecd2019-01-04 12:27:27 +0900132 // Drop all remaining capabilities.
133 set_capability(0);
Lorenzo Colittieb92f482019-01-04 14:59:11 +0900134
135 // Loop until someone sends us a signal or brings down the tun interface.
136 if (signal(SIGTERM, stop_loop) == SIG_ERR) {
137 logmsg(ANDROID_LOG_FATAL, "sigterm handler failed: %s", strerror(errno));
138 exit(1);
139 }
140
141 event_loop(&tunnel);
142
143 logmsg(ANDROID_LOG_INFO, "Shutting down clat on %s", uplink_interface);
144 del_anycast_address(tunnel.write_fd6, &Global_Clatd_Config.ipv6_local_subnet);
145
146 return 0;
147}