blob: 4be61a76522d70eecfd68a10d686e04d7550c403 [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
Daniel Drowna45056e2012-03-23 10:42:54 -050038/* function: config_item_int16_t
junyulaic4e591a2018-11-26 22:36:10 +090039 * locates the config item, parses the integer, and returns the pointer ret_val_ptr, or NULL on
40 * failure
41 * root - parsed configuration
42 * item_name - name of config item to locate
43 * defaultvar - value to use if config item isn't present
44 * ret_val_ptr - pointer for return value storage
Daniel Drowna45056e2012-03-23 10:42:54 -050045 */
junyulaic4e591a2018-11-26 22:36:10 +090046int16_t *config_item_int16_t(cnode *root, const char *item_name, const char *defaultvar,
47 int16_t *ret_val_ptr) {
Daniel Drowna45056e2012-03-23 10:42:54 -050048 const char *tmp;
49 char *endptr;
50 long int conf_int;
51
junyulaic4e591a2018-11-26 22:36:10 +090052 if (!(tmp = config_str(root, item_name, defaultvar))) {
53 logmsg(ANDROID_LOG_FATAL, "%s config item needed", item_name);
Daniel Drowna45056e2012-03-23 10:42:54 -050054 return NULL;
55 }
56
junyulaic4e591a2018-11-26 22:36:10 +090057 errno = 0;
58 conf_int = strtol(tmp, &endptr, 10);
59 if (errno > 0) {
60 logmsg(ANDROID_LOG_FATAL, "%s config item is not numeric: %s (error=%s)", item_name, tmp,
61 strerror(errno));
Daniel Drowna45056e2012-03-23 10:42:54 -050062 return NULL;
63 }
junyulaic4e591a2018-11-26 22:36:10 +090064 if (endptr == tmp || *tmp == '\0') {
65 logmsg(ANDROID_LOG_FATAL, "%s config item is not numeric: %s", item_name, tmp);
Daniel Drowna45056e2012-03-23 10:42:54 -050066 return NULL;
67 }
junyulaic4e591a2018-11-26 22:36:10 +090068 if (*endptr != '\0') {
69 logmsg(ANDROID_LOG_FATAL, "%s config item contains non-numeric characters: %s", item_name,
70 endptr);
Daniel Drowna45056e2012-03-23 10:42:54 -050071 return NULL;
72 }
junyulaic4e591a2018-11-26 22:36:10 +090073 if (conf_int > INT16_MAX || conf_int < INT16_MIN) {
74 logmsg(ANDROID_LOG_FATAL, "%s config item is too big/small: %d", item_name, conf_int);
Daniel Drowna45056e2012-03-23 10:42:54 -050075 return NULL;
76 }
77 *ret_val_ptr = conf_int;
78 return ret_val_ptr;
79}
80
81/* function: config_item_ip
junyulaic4e591a2018-11-26 22:36:10 +090082 * locates the config item, parses the ipv4 address, and returns the pointer ret_val_ptr, or NULL on
83 * failure
84 * root - parsed configuration
85 * item_name - name of config item to locate
86 * defaultvar - value to use if config item isn't present
87 * ret_val_ptr - pointer for return value storage
Daniel Drowna45056e2012-03-23 10:42:54 -050088 */
junyulaic4e591a2018-11-26 22:36:10 +090089struct in_addr *config_item_ip(cnode *root, const char *item_name, const char *defaultvar,
90 struct in_addr *ret_val_ptr) {
Daniel Drowna45056e2012-03-23 10:42:54 -050091 const char *tmp;
92 int status;
93
junyulaic4e591a2018-11-26 22:36:10 +090094 if (!(tmp = config_str(root, item_name, defaultvar))) {
95 logmsg(ANDROID_LOG_FATAL, "%s config item needed", item_name);
Daniel Drowna45056e2012-03-23 10:42:54 -050096 return NULL;
97 }
98
99 status = inet_pton(AF_INET, tmp, ret_val_ptr);
junyulaic4e591a2018-11-26 22:36:10 +0900100 if (status <= 0) {
101 logmsg(ANDROID_LOG_FATAL, "invalid IPv4 address specified for %s: %s", item_name, tmp);
Daniel Drowna45056e2012-03-23 10:42:54 -0500102 return NULL;
103 }
104
105 return ret_val_ptr;
106}
107
108/* function: config_item_ip6
junyulaic4e591a2018-11-26 22:36:10 +0900109 * locates the config item, parses the ipv6 address, and returns the pointer ret_val_ptr, or NULL on
110 * failure
111 * root - parsed configuration
112 * item_name - name of config item to locate
113 * defaultvar - value to use if config item isn't present
114 * ret_val_ptr - pointer for return value storage
Daniel Drowna45056e2012-03-23 10:42:54 -0500115 */
junyulaic4e591a2018-11-26 22:36:10 +0900116struct in6_addr *config_item_ip6(cnode *root, const char *item_name, const char *defaultvar,
117 struct in6_addr *ret_val_ptr) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500118 const char *tmp;
119 int status;
120
junyulaic4e591a2018-11-26 22:36:10 +0900121 if (!(tmp = config_str(root, item_name, defaultvar))) {
122 logmsg(ANDROID_LOG_FATAL, "%s config item needed", item_name);
Daniel Drowna45056e2012-03-23 10:42:54 -0500123 return NULL;
124 }
125
126 status = inet_pton(AF_INET6, tmp, ret_val_ptr);
junyulaic4e591a2018-11-26 22:36:10 +0900127 if (status <= 0) {
128 logmsg(ANDROID_LOG_FATAL, "invalid IPv6 address specified for %s: %s", item_name, tmp);
Daniel Drowna45056e2012-03-23 10:42:54 -0500129 return NULL;
130 }
131
132 return ret_val_ptr;
133}
134
Lorenzo Colitti98089522014-10-09 22:29:45 +0900135/* function: ipv6_prefix_equal
136 * compares the prefixes two ipv6 addresses. assumes the prefix lengths are both /64.
junyulaic4e591a2018-11-26 22:36:10 +0900137 * a1 - first address
138 * a2 - second address
139 * returns: 0 if the subnets are different, 1 if they are the same.
Lorenzo Colitti98089522014-10-09 22:29:45 +0900140 */
junyulaic4e591a2018-11-26 22:36:10 +0900141int ipv6_prefix_equal(struct in6_addr *a1, struct in6_addr *a2) { return !memcmp(a1, a2, 8); }
Lorenzo Colitti98089522014-10-09 22:29:45 +0900142
Daniel Drowna45056e2012-03-23 10:42:54 -0500143/* function: read_config
junyulaic4e591a2018-11-26 22:36:10 +0900144 * reads the config file and parses it into the global variable Global_Clatd_Config. returns 0 on
145 * failure, 1 on success
146 * file - filename to parse
147 * uplink_interface - interface to use to reach the internet and supplier of address space
Daniel Drowna45056e2012-03-23 10:42:54 -0500148 */
Lorenzo Colitti27da0ad2020-06-01 12:15:20 +0900149int read_config(const char *file, const char *uplink_interface) {
junyulaic4e591a2018-11-26 22:36:10 +0900150 cnode *root = config_node("", "");
Daniel Drowna45056e2012-03-23 10:42:54 -0500151
junyulaic4e591a2018-11-26 22:36:10 +0900152 if (!root) {
153 logmsg(ANDROID_LOG_FATAL, "out of memory");
Daniel Drowna45056e2012-03-23 10:42:54 -0500154 return 0;
155 }
156
157 memset(&Global_Clatd_Config, '\0', sizeof(Global_Clatd_Config));
158
159 config_load_file(root, file);
junyulaic4e591a2018-11-26 22:36:10 +0900160 if (root->first_child == NULL) {
161 logmsg(ANDROID_LOG_FATAL, "Could not read config file %s", file);
Daniel Drowna45056e2012-03-23 10:42:54 -0500162 goto failed;
163 }
164
Lorenzo Colitti1352a3a2014-10-21 13:41:21 +0900165 Global_Clatd_Config.default_pdp_interface = strdup(uplink_interface);
junyulaic4e591a2018-11-26 22:36:10 +0900166 if (!Global_Clatd_Config.default_pdp_interface) goto failed;
167
junyulaic4e591a2018-11-26 22:36:10 +0900168 if (!config_item_ip(root, "ipv4_local_subnet", DEFAULT_IPV4_LOCAL_SUBNET,
169 &Global_Clatd_Config.ipv4_local_subnet))
Lorenzo Colitti1352a3a2014-10-21 13:41:21 +0900170 goto failed;
Daniel Drowna45056e2012-03-23 10:42:54 -0500171
junyulaic4e591a2018-11-26 22:36:10 +0900172 if (!config_item_int16_t(root, "ipv4_local_prefixlen", DEFAULT_IPV4_LOCAL_PREFIXLEN,
173 &Global_Clatd_Config.ipv4_local_prefixlen))
Daniel Drowna45056e2012-03-23 10:42:54 -0500174 goto failed;
175
Daniel Drowna45056e2012-03-23 10:42:54 -0500176 return 1;
177
178failed:
179 free(root);
Daniel Drowna45056e2012-03-23 10:42:54 -0500180 return 0;
181}