blob: db0abc8671bb3f5c03d42846c5b2358d74fe1238 [file] [log] [blame]
Daniel Drowna45056e2012-03-23 10:42:54 -05001/*
2 * Copyright 2011 Daniel Drown
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 * config.c - configuration settings
17 */
18
Daniel Drowna45056e2012-03-23 10:42:54 -050019#include <arpa/inet.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050020#include <errno.h>
junyulaic4e591a2018-11-26 22:36:10 +090021#include <limits.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050025#include <unistd.h>
26
27#include <cutils/config_utils.h>
Lorenzo Colitti98de5952019-01-20 11:45:03 +090028#include <netutils/checksum.h>
Lorenzo Colitti2596f422014-11-10 17:00:02 -080029#include <netutils/ifc.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050030
junyulaic4e591a2018-11-26 22:36:10 +090031#include "clatd.h"
Daniel Drowna45056e2012-03-23 10:42:54 -050032#include "config.h"
Daniel Drowna45056e2012-03-23 10:42:54 -050033#include "getaddr.h"
junyulaic4e591a2018-11-26 22:36:10 +090034#include "logging.h"
Daniel Drowna45056e2012-03-23 10:42:54 -050035
36struct clat_config Global_Clatd_Config;
37
38/* function: config_item_str
junyulaic4e591a2018-11-26 22:36:10 +090039 * locates the config item and returns the pointer to a string, or NULL on failure. Caller frees
40 * pointer
41 * root - parsed configuration
42 * item_name - name of config item to locate
43 * defaultvar - value to use if config item isn't present
Daniel Drowna45056e2012-03-23 10:42:54 -050044 */
45char *config_item_str(cnode *root, const char *item_name, const char *defaultvar) {
46 const char *tmp;
47
junyulaic4e591a2018-11-26 22:36:10 +090048 if (!(tmp = config_str(root, item_name, defaultvar))) {
49 logmsg(ANDROID_LOG_FATAL, "%s config item needed", item_name);
Daniel Drowna45056e2012-03-23 10:42:54 -050050 return NULL;
51 }
52 return strdup(tmp);
53}
54
55/* function: config_item_int16_t
junyulaic4e591a2018-11-26 22:36:10 +090056 * locates the config item, parses the integer, and returns the pointer ret_val_ptr, or NULL on
57 * failure
58 * root - parsed configuration
59 * item_name - name of config item to locate
60 * defaultvar - value to use if config item isn't present
61 * ret_val_ptr - pointer for return value storage
Daniel Drowna45056e2012-03-23 10:42:54 -050062 */
junyulaic4e591a2018-11-26 22:36:10 +090063int16_t *config_item_int16_t(cnode *root, const char *item_name, const char *defaultvar,
64 int16_t *ret_val_ptr) {
Daniel Drowna45056e2012-03-23 10:42:54 -050065 const char *tmp;
66 char *endptr;
67 long int conf_int;
68
junyulaic4e591a2018-11-26 22:36:10 +090069 if (!(tmp = config_str(root, item_name, defaultvar))) {
70 logmsg(ANDROID_LOG_FATAL, "%s config item needed", item_name);
Daniel Drowna45056e2012-03-23 10:42:54 -050071 return NULL;
72 }
73
junyulaic4e591a2018-11-26 22:36:10 +090074 errno = 0;
75 conf_int = strtol(tmp, &endptr, 10);
76 if (errno > 0) {
77 logmsg(ANDROID_LOG_FATAL, "%s config item is not numeric: %s (error=%s)", item_name, tmp,
78 strerror(errno));
Daniel Drowna45056e2012-03-23 10:42:54 -050079 return NULL;
80 }
junyulaic4e591a2018-11-26 22:36:10 +090081 if (endptr == tmp || *tmp == '\0') {
82 logmsg(ANDROID_LOG_FATAL, "%s config item is not numeric: %s", item_name, tmp);
Daniel Drowna45056e2012-03-23 10:42:54 -050083 return NULL;
84 }
junyulaic4e591a2018-11-26 22:36:10 +090085 if (*endptr != '\0') {
86 logmsg(ANDROID_LOG_FATAL, "%s config item contains non-numeric characters: %s", item_name,
87 endptr);
Daniel Drowna45056e2012-03-23 10:42:54 -050088 return NULL;
89 }
junyulaic4e591a2018-11-26 22:36:10 +090090 if (conf_int > INT16_MAX || conf_int < INT16_MIN) {
91 logmsg(ANDROID_LOG_FATAL, "%s config item is too big/small: %d", item_name, conf_int);
Daniel Drowna45056e2012-03-23 10:42:54 -050092 return NULL;
93 }
94 *ret_val_ptr = conf_int;
95 return ret_val_ptr;
96}
97
98/* function: config_item_ip
junyulaic4e591a2018-11-26 22:36:10 +090099 * locates the config item, parses the ipv4 address, and returns the pointer ret_val_ptr, or NULL on
100 * failure
101 * root - parsed configuration
102 * item_name - name of config item to locate
103 * defaultvar - value to use if config item isn't present
104 * ret_val_ptr - pointer for return value storage
Daniel Drowna45056e2012-03-23 10:42:54 -0500105 */
junyulaic4e591a2018-11-26 22:36:10 +0900106struct in_addr *config_item_ip(cnode *root, const char *item_name, const char *defaultvar,
107 struct in_addr *ret_val_ptr) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500108 const char *tmp;
109 int status;
110
junyulaic4e591a2018-11-26 22:36:10 +0900111 if (!(tmp = config_str(root, item_name, defaultvar))) {
112 logmsg(ANDROID_LOG_FATAL, "%s config item needed", item_name);
Daniel Drowna45056e2012-03-23 10:42:54 -0500113 return NULL;
114 }
115
116 status = inet_pton(AF_INET, tmp, ret_val_ptr);
junyulaic4e591a2018-11-26 22:36:10 +0900117 if (status <= 0) {
118 logmsg(ANDROID_LOG_FATAL, "invalid IPv4 address specified for %s: %s", item_name, tmp);
Daniel Drowna45056e2012-03-23 10:42:54 -0500119 return NULL;
120 }
121
122 return ret_val_ptr;
123}
124
125/* function: config_item_ip6
junyulaic4e591a2018-11-26 22:36:10 +0900126 * locates the config item, parses the ipv6 address, and returns the pointer ret_val_ptr, or NULL on
127 * failure
128 * root - parsed configuration
129 * item_name - name of config item to locate
130 * defaultvar - value to use if config item isn't present
131 * ret_val_ptr - pointer for return value storage
Daniel Drowna45056e2012-03-23 10:42:54 -0500132 */
junyulaic4e591a2018-11-26 22:36:10 +0900133struct in6_addr *config_item_ip6(cnode *root, const char *item_name, const char *defaultvar,
134 struct in6_addr *ret_val_ptr) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500135 const char *tmp;
136 int status;
137
junyulaic4e591a2018-11-26 22:36:10 +0900138 if (!(tmp = config_str(root, item_name, defaultvar))) {
139 logmsg(ANDROID_LOG_FATAL, "%s config item needed", item_name);
Daniel Drowna45056e2012-03-23 10:42:54 -0500140 return NULL;
141 }
142
143 status = inet_pton(AF_INET6, tmp, ret_val_ptr);
junyulaic4e591a2018-11-26 22:36:10 +0900144 if (status <= 0) {
145 logmsg(ANDROID_LOG_FATAL, "invalid IPv6 address specified for %s: %s", item_name, tmp);
Daniel Drowna45056e2012-03-23 10:42:54 -0500146 return NULL;
147 }
148
149 return ret_val_ptr;
150}
151
Lorenzo Colitti98089522014-10-09 22:29:45 +0900152/* function: ipv6_prefix_equal
153 * compares the prefixes two ipv6 addresses. assumes the prefix lengths are both /64.
junyulaic4e591a2018-11-26 22:36:10 +0900154 * a1 - first address
155 * a2 - second address
156 * returns: 0 if the subnets are different, 1 if they are the same.
Lorenzo Colitti98089522014-10-09 22:29:45 +0900157 */
junyulaic4e591a2018-11-26 22:36:10 +0900158int ipv6_prefix_equal(struct in6_addr *a1, struct in6_addr *a2) { return !memcmp(a1, a2, 8); }
Lorenzo Colitti98089522014-10-09 22:29:45 +0900159
Lorenzo Colitti798f9932014-10-31 21:54:33 +0900160/* function: gen_random_iid
161 * picks a random interface ID that is checksum neutral with the IPv4 address and the NAT64 prefix
junyulaic4e591a2018-11-26 22:36:10 +0900162 * myaddr - IPv6 address to write to
163 * ipv4_local_subnet - clat IPv4 address
164 * plat_subnet - NAT64 prefix
Lorenzo Colitti798f9932014-10-31 21:54:33 +0900165 */
Lorenzo Colitti98089522014-10-09 22:29:45 +0900166void gen_random_iid(struct in6_addr *myaddr, struct in_addr *ipv4_local_subnet,
167 struct in6_addr *plat_subnet) {
168 // Fill last 8 bytes of IPv6 address with random bits.
169 arc4random_buf(&myaddr->s6_addr[8], 8);
170
171 // Make the IID checksum-neutral. That is, make it so that:
172 // checksum(Local IPv4 | Remote IPv4) = checksum(Local IPv6 | Remote IPv6)
173 // in other words (because remote IPv6 = NAT64 prefix | Remote IPv4):
174 // checksum(Local IPv4) = checksum(Local IPv6 | NAT64 prefix)
175 // Do this by adjusting the two bytes in the middle of the IID.
176
177 uint16_t middlebytes = (myaddr->s6_addr[11] << 8) + myaddr->s6_addr[12];
178
179 uint32_t c1 = ip_checksum_add(0, ipv4_local_subnet, sizeof(*ipv4_local_subnet));
180 uint32_t c2 = ip_checksum_add(0, plat_subnet, sizeof(*plat_subnet)) +
181 ip_checksum_add(0, myaddr, sizeof(*myaddr));
182
junyulaic4e591a2018-11-26 22:36:10 +0900183 uint16_t delta = ip_checksum_adjust(middlebytes, c1, c2);
Lorenzo Colitti98089522014-10-09 22:29:45 +0900184 myaddr->s6_addr[11] = delta >> 8;
185 myaddr->s6_addr[12] = delta & 0xff;
186}
187
Lorenzo Colitti798f9932014-10-31 21:54:33 +0900188// Factored out to a separate function for testability.
189int connect_is_ipv4_address_free(in_addr_t addr) {
Maciej Żenczykowski60bce372019-04-09 01:58:52 -0700190 int s = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
Lorenzo Colitti798f9932014-10-31 21:54:33 +0900191 if (s == -1) {
192 return 0;
193 }
194
195 // Attempt to connect to the address. If the connection succeeds and getsockname returns the same
196 // the address then the address is already assigned to the system and we can't use it.
Maciej Żenczykowskia02523b2019-10-31 23:53:37 -0700197 struct sockaddr_in sin = { .sin_family = AF_INET, .sin_addr = { addr }, .sin_port = htons(53) };
junyulaic4e591a2018-11-26 22:36:10 +0900198 socklen_t len = sizeof(sin);
199 int inuse = connect(s, (struct sockaddr *)&sin, sizeof(sin)) == 0 &&
200 getsockname(s, (struct sockaddr *)&sin, &len) == 0 && (size_t)len >= sizeof(sin) &&
Lorenzo Colitti798f9932014-10-31 21:54:33 +0900201 sin.sin_addr.s_addr == addr;
202
203 close(s);
204 return !inuse;
205}
206
207addr_free_func config_is_ipv4_address_free = connect_is_ipv4_address_free;
208
209/* function: config_select_ipv4_address
210 * picks a free IPv4 address, starting from ip and trying all addresses in the prefix in order
junyulaic4e591a2018-11-26 22:36:10 +0900211 * ip - the IP address from the configuration file
212 * prefixlen - the length of the prefix from which addresses may be selected.
213 * returns: the IPv4 address, or INADDR_NONE if no addresses were available
Lorenzo Colitti798f9932014-10-31 21:54:33 +0900214 */
215in_addr_t config_select_ipv4_address(const struct in_addr *ip, int16_t prefixlen) {
216 in_addr_t chosen = INADDR_NONE;
217
218 // Don't accept prefixes that are too large because we scan addresses one by one.
219 if (prefixlen < 16 || prefixlen > 32) {
junyulaic4e591a2018-11-26 22:36:10 +0900220 return chosen;
Lorenzo Colitti798f9932014-10-31 21:54:33 +0900221 }
222
223 // All these are in host byte order.
junyulaic4e591a2018-11-26 22:36:10 +0900224 in_addr_t mask = 0xffffffff >> (32 - prefixlen) << (32 - prefixlen);
225 in_addr_t ipv4 = ntohl(ip->s_addr);
Lorenzo Colitti798f9932014-10-31 21:54:33 +0900226 in_addr_t first_ipv4 = ipv4;
junyulaic4e591a2018-11-26 22:36:10 +0900227 in_addr_t prefix = ipv4 & mask;
Lorenzo Colitti798f9932014-10-31 21:54:33 +0900228
229 // Pick the first IPv4 address in the pool, wrapping around if necessary.
230 // So, for example, 192.0.0.4 -> 192.0.0.5 -> 192.0.0.6 -> 192.0.0.7 -> 192.0.0.0.
231 do {
junyulaic4e591a2018-11-26 22:36:10 +0900232 if (config_is_ipv4_address_free(htonl(ipv4))) {
233 chosen = htonl(ipv4);
234 break;
235 }
236 ipv4 = prefix | ((ipv4 + 1) & ~mask);
Lorenzo Colitti798f9932014-10-31 21:54:33 +0900237 } while (ipv4 != first_ipv4);
238
239 return chosen;
240}
241
Daniel Drowna45056e2012-03-23 10:42:54 -0500242/* function: config_generate_local_ipv6_subnet
junyulaic4e591a2018-11-26 22:36:10 +0900243 * generates the local ipv6 subnet when given the interface ip requires config.ipv6_host_id
244 * interface_ip - in: interface ip, out: local ipv6 host address
Daniel Drowna45056e2012-03-23 10:42:54 -0500245 */
246void config_generate_local_ipv6_subnet(struct in6_addr *interface_ip) {
247 int i;
248
Lorenzo Colitti2596f422014-11-10 17:00:02 -0800249 if (Global_Clatd_Config.use_dynamic_iid) {
Lorenzo Colitti98089522014-10-09 22:29:45 +0900250 /* Generate a random interface ID. */
junyulaic4e591a2018-11-26 22:36:10 +0900251 gen_random_iid(interface_ip, &Global_Clatd_Config.ipv4_local_subnet,
Lorenzo Colitti98089522014-10-09 22:29:45 +0900252 &Global_Clatd_Config.plat_subnet);
253 } else {
254 /* Use the specified interface ID. */
junyulaic4e591a2018-11-26 22:36:10 +0900255 for (i = 2; i < 4; i++) {
Lorenzo Colitti98089522014-10-09 22:29:45 +0900256 interface_ip->s6_addr32[i] = Global_Clatd_Config.ipv6_host_id.s6_addr32[i];
257 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500258 }
259}
260
Daniel Drowna45056e2012-03-23 10:42:54 -0500261/* function: read_config
junyulaic4e591a2018-11-26 22:36:10 +0900262 * reads the config file and parses it into the global variable Global_Clatd_Config. returns 0 on
263 * failure, 1 on success
264 * file - filename to parse
265 * uplink_interface - interface to use to reach the internet and supplier of address space
Daniel Drowna45056e2012-03-23 10:42:54 -0500266 */
Lorenzo Colitti27da0ad2020-06-01 12:15:20 +0900267int read_config(const char *file, const char *uplink_interface) {
junyulaic4e591a2018-11-26 22:36:10 +0900268 cnode *root = config_node("", "");
Lorenzo Colitti2596f422014-11-10 17:00:02 -0800269 unsigned flags;
Daniel Drowna45056e2012-03-23 10:42:54 -0500270
junyulaic4e591a2018-11-26 22:36:10 +0900271 if (!root) {
272 logmsg(ANDROID_LOG_FATAL, "out of memory");
Daniel Drowna45056e2012-03-23 10:42:54 -0500273 return 0;
274 }
275
276 memset(&Global_Clatd_Config, '\0', sizeof(Global_Clatd_Config));
277
278 config_load_file(root, file);
junyulaic4e591a2018-11-26 22:36:10 +0900279 if (root->first_child == NULL) {
280 logmsg(ANDROID_LOG_FATAL, "Could not read config file %s", file);
Daniel Drowna45056e2012-03-23 10:42:54 -0500281 goto failed;
282 }
283
Lorenzo Colitti1352a3a2014-10-21 13:41:21 +0900284 Global_Clatd_Config.default_pdp_interface = strdup(uplink_interface);
junyulaic4e591a2018-11-26 22:36:10 +0900285 if (!Global_Clatd_Config.default_pdp_interface) goto failed;
286
junyulaic4e591a2018-11-26 22:36:10 +0900287 if (!config_item_ip(root, "ipv4_local_subnet", DEFAULT_IPV4_LOCAL_SUBNET,
288 &Global_Clatd_Config.ipv4_local_subnet))
Lorenzo Colitti1352a3a2014-10-21 13:41:21 +0900289 goto failed;
Daniel Drowna45056e2012-03-23 10:42:54 -0500290
junyulaic4e591a2018-11-26 22:36:10 +0900291 if (!config_item_int16_t(root, "ipv4_local_prefixlen", DEFAULT_IPV4_LOCAL_PREFIXLEN,
292 &Global_Clatd_Config.ipv4_local_prefixlen))
Daniel Drowna45056e2012-03-23 10:42:54 -0500293 goto failed;
294
junyulaic4e591a2018-11-26 22:36:10 +0900295 if (!config_item_ip6(root, "ipv6_host_id", "::", &Global_Clatd_Config.ipv6_host_id)) goto failed;
Lorenzo Colitti98089522014-10-09 22:29:45 +0900296
Lorenzo Colitti2596f422014-11-10 17:00:02 -0800297 /* In order to prevent multiple devices attempting to use the same clat address, never use a
298 statically-configured interface ID on a broadcast interface such as wifi. */
299 if (!IN6_IS_ADDR_UNSPECIFIED(&Global_Clatd_Config.ipv6_host_id)) {
300 ifc_init();
301 ifc_get_info(Global_Clatd_Config.default_pdp_interface, NULL, NULL, &flags);
302 ifc_close();
303 Global_Clatd_Config.use_dynamic_iid = (flags & IFF_BROADCAST) != 0;
304 } else {
305 Global_Clatd_Config.use_dynamic_iid = 1;
306 }
307
Daniel Drowna45056e2012-03-23 10:42:54 -0500308 return 1;
309
310failed:
311 free(root);
Daniel Drowna45056e2012-03-23 10:42:54 -0500312 return 0;
313}
314
315/* function; dump_config
316 * prints the current config
317 */
318void dump_config() {
319 char charbuffer[INET6_ADDRSTRLEN];
320
junyulaic4e591a2018-11-26 22:36:10 +0900321 logmsg(
322 ANDROID_LOG_DEBUG, "ipv6_local_subnet = %s",
323 inet_ntop(AF_INET6, &Global_Clatd_Config.ipv6_local_subnet, charbuffer, sizeof(charbuffer)));
324 logmsg(
325 ANDROID_LOG_DEBUG, "ipv4_local_subnet = %s",
326 inet_ntop(AF_INET, &Global_Clatd_Config.ipv4_local_subnet, charbuffer, sizeof(charbuffer)));
327 logmsg(ANDROID_LOG_DEBUG, "ipv4_local_prefixlen = %d", Global_Clatd_Config.ipv4_local_prefixlen);
328 logmsg(ANDROID_LOG_DEBUG, "plat_subnet = %s",
329 inet_ntop(AF_INET6, &Global_Clatd_Config.plat_subnet, charbuffer, sizeof(charbuffer)));
330 logmsg(ANDROID_LOG_DEBUG, "default_pdp_interface = %s",
331 Global_Clatd_Config.default_pdp_interface);
Daniel Drowna45056e2012-03-23 10:42:54 -0500332}