blob: 1c380dcc7919558f9052a443f47f8e60b9472474 [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
Daniel Drowna45056e2012-03-23 10:42:54 -0500160/* function: read_config
junyulaic4e591a2018-11-26 22:36:10 +0900161 * reads the config file and parses it into the global variable Global_Clatd_Config. returns 0 on
162 * failure, 1 on success
163 * file - filename to parse
164 * uplink_interface - interface to use to reach the internet and supplier of address space
Daniel Drowna45056e2012-03-23 10:42:54 -0500165 */
Lorenzo Colitti27da0ad2020-06-01 12:15:20 +0900166int read_config(const char *file, const char *uplink_interface) {
junyulaic4e591a2018-11-26 22:36:10 +0900167 cnode *root = config_node("", "");
Lorenzo Colitti2596f422014-11-10 17:00:02 -0800168 unsigned flags;
Daniel Drowna45056e2012-03-23 10:42:54 -0500169
junyulaic4e591a2018-11-26 22:36:10 +0900170 if (!root) {
171 logmsg(ANDROID_LOG_FATAL, "out of memory");
Daniel Drowna45056e2012-03-23 10:42:54 -0500172 return 0;
173 }
174
175 memset(&Global_Clatd_Config, '\0', sizeof(Global_Clatd_Config));
176
177 config_load_file(root, file);
junyulaic4e591a2018-11-26 22:36:10 +0900178 if (root->first_child == NULL) {
179 logmsg(ANDROID_LOG_FATAL, "Could not read config file %s", file);
Daniel Drowna45056e2012-03-23 10:42:54 -0500180 goto failed;
181 }
182
Lorenzo Colitti1352a3a2014-10-21 13:41:21 +0900183 Global_Clatd_Config.default_pdp_interface = strdup(uplink_interface);
junyulaic4e591a2018-11-26 22:36:10 +0900184 if (!Global_Clatd_Config.default_pdp_interface) goto failed;
185
junyulaic4e591a2018-11-26 22:36:10 +0900186 if (!config_item_ip(root, "ipv4_local_subnet", DEFAULT_IPV4_LOCAL_SUBNET,
187 &Global_Clatd_Config.ipv4_local_subnet))
Lorenzo Colitti1352a3a2014-10-21 13:41:21 +0900188 goto failed;
Daniel Drowna45056e2012-03-23 10:42:54 -0500189
junyulaic4e591a2018-11-26 22:36:10 +0900190 if (!config_item_int16_t(root, "ipv4_local_prefixlen", DEFAULT_IPV4_LOCAL_PREFIXLEN,
191 &Global_Clatd_Config.ipv4_local_prefixlen))
Daniel Drowna45056e2012-03-23 10:42:54 -0500192 goto failed;
193
junyulaic4e591a2018-11-26 22:36:10 +0900194 if (!config_item_ip6(root, "ipv6_host_id", "::", &Global_Clatd_Config.ipv6_host_id)) goto failed;
Lorenzo Colitti98089522014-10-09 22:29:45 +0900195
Lorenzo Colitti2596f422014-11-10 17:00:02 -0800196 /* In order to prevent multiple devices attempting to use the same clat address, never use a
197 statically-configured interface ID on a broadcast interface such as wifi. */
198 if (!IN6_IS_ADDR_UNSPECIFIED(&Global_Clatd_Config.ipv6_host_id)) {
199 ifc_init();
200 ifc_get_info(Global_Clatd_Config.default_pdp_interface, NULL, NULL, &flags);
201 ifc_close();
202 Global_Clatd_Config.use_dynamic_iid = (flags & IFF_BROADCAST) != 0;
203 } else {
204 Global_Clatd_Config.use_dynamic_iid = 1;
205 }
206
Daniel Drowna45056e2012-03-23 10:42:54 -0500207 return 1;
208
209failed:
210 free(root);
Daniel Drowna45056e2012-03-23 10:42:54 -0500211 return 0;
212}