blob: 54d12d17c261240901adab5b31883a2654abbbf5 [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
19#include <errno.h>
20#include <netinet/in.h>
21#include <stdint.h>
22#include <stdlib.h>
23#include <string.h>
24#include <sys/capability.h>
25#include <unistd.h>
26
27#include "resolv_netid.h"
28
29#include "clatd.h"
30#include "common.h"
31#include "config.h"
32#include "logging.h"
33#include "setif.h"
34#include "tun.h"
35
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("-n [NetId]\n");
48 printf("-m [socket mark]\n");
49}
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;
57 char *uplink_interface = NULL, *plat_prefix = NULL, *net_id_str = NULL, *mark_str = NULL;
Lorenzo Colittif0fac862019-01-11 18:10:11 +090058 char *v4_addr = NULL, *v6_addr = NULL;
Lorenzo Colittieb92f482019-01-04 14:59:11 +090059 unsigned net_id = NETID_UNSET;
60 uint32_t mark = MARK_UNSET;
61 unsigned len;
62
Lorenzo Colittif0fac862019-01-11 18:10:11 +090063 while ((opt = getopt(argc, argv, "i:p:4:6:n:m:h")) != -1) {
Lorenzo Colittieb92f482019-01-04 14:59:11 +090064 switch (opt) {
65 case 'i':
66 uplink_interface = optarg;
67 break;
68 case 'p':
69 plat_prefix = optarg;
70 break;
Lorenzo Colittif0fac862019-01-11 18:10:11 +090071 case '4':
72 v4_addr = optarg;
73 break;
74 case '6':
75 v6_addr = optarg;
76 break;
Lorenzo Colittieb92f482019-01-04 14:59:11 +090077 case 'n':
78 net_id_str = optarg;
79 break;
80 case 'm':
81 mark_str = optarg;
82 break;
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
97 if (net_id_str != NULL && !parse_unsigned(net_id_str, &net_id)) {
98 logmsg(ANDROID_LOG_FATAL, "invalid NetID %s", net_id_str);
99 exit(1);
100 }
101
102 if (mark_str != NULL && !parse_unsigned(mark_str, &mark)) {
103 logmsg(ANDROID_LOG_FATAL, "invalid mark %s", mark_str);
104 exit(1);
105 }
106
107 len = snprintf(tunnel.device4, sizeof(tunnel.device4), "%s%s", DEVICEPREFIX, uplink_interface);
108 if (len >= sizeof(tunnel.device4)) {
109 logmsg(ANDROID_LOG_FATAL, "interface name too long '%s'", tunnel.device4);
110 exit(1);
111 }
112
Lorenzo Colittif0fac862019-01-11 18:10:11 +0900113 logmsg(ANDROID_LOG_INFO, "Starting clat version %s on %s netid=%s mark=%s plat=%s v4=%s v6=%s",
114 CLATD_VERSION, uplink_interface, net_id_str ? net_id_str : "(none)",
115 mark_str ? mark_str : "(none)", plat_prefix ? plat_prefix : "(none)",
116 v4_addr ? v4_addr : "(none)", v6_addr ? v6_addr : "(none)");
Lorenzo Colittieb92f482019-01-04 14:59:11 +0900117
118 // run under a regular user but keep needed capabilities
119 drop_root_but_keep_caps();
120
121 // open our raw sockets before dropping privs
122 open_sockets(&tunnel, mark);
123
124 // keeps only admin capability
125 set_capability(1 << CAP_NET_ADMIN);
126
127 // we can create tun devices as non-root because we're in the VPN group.
128 tunnel.fd4 = tun_open();
129 if (tunnel.fd4 < 0) {
130 logmsg(ANDROID_LOG_FATAL, "tun_open4 failed: %s", strerror(errno));
131 exit(1);
132 }
133
134 // When run from netd, the environment variable ANDROID_DNS_MODE is set to
135 // "local", but that only works for the netd process itself. Removing the
136 // following line causes XLAT failure in permissive mode.
137 unsetenv("ANDROID_DNS_MODE");
138
Lorenzo Colittif0fac862019-01-11 18:10:11 +0900139 configure_interface(uplink_interface, plat_prefix, v4_addr, v6_addr, &tunnel, net_id);
Lorenzo Colittieb92f482019-01-04 14:59:11 +0900140
Lorenzo Colitti66deecd2019-01-04 12:27:27 +0900141 // Drop all remaining capabilities.
142 set_capability(0);
Lorenzo Colittieb92f482019-01-04 14:59:11 +0900143
144 // Loop until someone sends us a signal or brings down the tun interface.
145 if (signal(SIGTERM, stop_loop) == SIG_ERR) {
146 logmsg(ANDROID_LOG_FATAL, "sigterm handler failed: %s", strerror(errno));
147 exit(1);
148 }
149
150 event_loop(&tunnel);
151
152 logmsg(ANDROID_LOG_INFO, "Shutting down clat on %s", uplink_interface);
153 del_anycast_address(tunnel.write_fd6, &Global_Clatd_Config.ipv6_local_subnet);
154
155 return 0;
156}