blob: d8bec8091078272fcbe393db2db4678f48777a31 [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
19#include <string.h>
20#include <stdlib.h>
21#include <arpa/inet.h>
22#include <stdio.h>
23#include <limits.h>
24#include <errno.h>
25#include <unistd.h>
26
27#include <cutils/config_utils.h>
28
29#include "config.h"
30#include "dns64.h"
31#include "logging.h"
32#include "getaddr.h"
33#include "clatd.h"
Daniel Drowna45056e2012-03-23 10:42:54 -050034
35struct clat_config Global_Clatd_Config;
36
37/* function: config_item_str
38 * locates the config item and returns the pointer to a string, or NULL on failure. Caller frees pointer
39 * root - parsed configuration
40 * item_name - name of config item to locate
41 * defaultvar - value to use if config item isn't present
42 */
43char *config_item_str(cnode *root, const char *item_name, const char *defaultvar) {
44 const char *tmp;
45
46 if(!(tmp = config_str(root, item_name, defaultvar))) {
47 logmsg(ANDROID_LOG_FATAL,"%s config item needed",item_name);
48 return NULL;
49 }
50 return strdup(tmp);
51}
52
53/* function: config_item_int16_t
54 * locates the config item, parses the integer, and returns the pointer ret_val_ptr, or NULL on failure
55 * root - parsed configuration
56 * item_name - name of config item to locate
57 * defaultvar - value to use if config item isn't present
58 * ret_val_ptr - pointer for return value storage
59 */
60int16_t *config_item_int16_t(cnode *root, const char *item_name, const char *defaultvar, int16_t *ret_val_ptr) {
61 const char *tmp;
62 char *endptr;
63 long int conf_int;
64
65 if(!(tmp = config_str(root, item_name, defaultvar))) {
66 logmsg(ANDROID_LOG_FATAL,"%s config item needed",item_name);
67 return NULL;
68 }
69
70 errno = 0;
71 conf_int = strtol(tmp,&endptr,10);
72 if(errno > 0) {
73 logmsg(ANDROID_LOG_FATAL,"%s config item is not numeric: %s (error=%s)",item_name,tmp,strerror(errno));
74 return NULL;
75 }
76 if(endptr == tmp || *tmp == '\0') {
77 logmsg(ANDROID_LOG_FATAL,"%s config item is not numeric: %s",item_name,tmp);
78 return NULL;
79 }
80 if(*endptr != '\0') {
81 logmsg(ANDROID_LOG_FATAL,"%s config item contains non-numeric characters: %s",item_name,endptr);
82 return NULL;
83 }
84 if(conf_int > INT16_MAX || conf_int < INT16_MIN) {
85 logmsg(ANDROID_LOG_FATAL,"%s config item is too big/small: %d",item_name,conf_int);
86 return NULL;
87 }
88 *ret_val_ptr = conf_int;
89 return ret_val_ptr;
90}
91
92/* function: config_item_ip
93 * locates the config item, parses the ipv4 address, and returns the pointer ret_val_ptr, or NULL on failure
94 * root - parsed configuration
95 * item_name - name of config item to locate
96 * defaultvar - value to use if config item isn't present
97 * ret_val_ptr - pointer for return value storage
98 */
99struct in_addr *config_item_ip(cnode *root, const char *item_name, const char *defaultvar, struct in_addr *ret_val_ptr) {
100 const char *tmp;
101 int status;
102
103 if(!(tmp = config_str(root, item_name, defaultvar))) {
104 logmsg(ANDROID_LOG_FATAL,"%s config item needed",item_name);
105 return NULL;
106 }
107
108 status = inet_pton(AF_INET, tmp, ret_val_ptr);
109 if(status <= 0) {
110 logmsg(ANDROID_LOG_FATAL,"invalid IPv4 address specified for %s: %s", item_name, tmp);
111 return NULL;
112 }
113
114 return ret_val_ptr;
115}
116
117/* function: config_item_ip6
118 * locates the config item, parses the ipv6 address, and returns the pointer ret_val_ptr, or NULL on failure
119 * root - parsed configuration
120 * item_name - name of config item to locate
121 * defaultvar - value to use if config item isn't present
122 * ret_val_ptr - pointer for return value storage
123 */
124struct in6_addr *config_item_ip6(cnode *root, const char *item_name, const char *defaultvar, struct in6_addr *ret_val_ptr) {
125 const char *tmp;
126 int status;
127
128 if(!(tmp = config_str(root, item_name, defaultvar))) {
129 logmsg(ANDROID_LOG_FATAL,"%s config item needed",item_name);
130 return NULL;
131 }
132
133 status = inet_pton(AF_INET6, tmp, ret_val_ptr);
134 if(status <= 0) {
135 logmsg(ANDROID_LOG_FATAL,"invalid IPv6 address specified for %s: %s", item_name, tmp);
136 return NULL;
137 }
138
139 return ret_val_ptr;
140}
141
142/* function: free_config
143 * frees the memory used by the global config variable
144 */
145void free_config() {
146 if(Global_Clatd_Config.plat_from_dns64_hostname) {
147 free(Global_Clatd_Config.plat_from_dns64_hostname);
148 Global_Clatd_Config.plat_from_dns64_hostname = NULL;
149 }
150}
151
152/* function: dns64_detection
153 * does dns lookups to set the plat subnet or exits on failure, waits forever for a dns response with a query backoff timer
Paul Jensena1c871c2014-05-30 13:19:10 -0400154 * net_id - (optional) netId to use, NETID_UNSET indicates use of default network
Daniel Drowna45056e2012-03-23 10:42:54 -0500155 */
Paul Jensena1c871c2014-05-30 13:19:10 -0400156void dns64_detection(unsigned net_id) {
Bernhard Rosenkränzera33592b2013-12-12 10:28:16 +0100157 int backoff_sleep, status;
Daniel Drowna45056e2012-03-23 10:42:54 -0500158 struct in6_addr tmp_ptr;
159
160 backoff_sleep = 1;
161
162 while(1) {
Paul Jensena1c871c2014-05-30 13:19:10 -0400163 status = plat_prefix(Global_Clatd_Config.plat_from_dns64_hostname,net_id,&tmp_ptr);
Daniel Drowna45056e2012-03-23 10:42:54 -0500164 if(status > 0) {
165 memcpy(&Global_Clatd_Config.plat_subnet, &tmp_ptr, sizeof(struct in6_addr));
166 return;
167 }
168 if(status < 0) {
169 logmsg(ANDROID_LOG_FATAL, "dns64_detection/no dns64, giving up\n");
170 exit(1);
171 }
172 logmsg(ANDROID_LOG_WARN, "dns64_detection failed, sleeping for %d seconds", backoff_sleep);
173 sleep(backoff_sleep);
174 if(backoff_sleep >= 120) {
175 backoff_sleep = 120;
176 } else {
177 backoff_sleep *= 2;
178 }
179 }
180}
181
182
183/* function: config_generate_local_ipv6_subnet
184 * generates the local ipv6 subnet when given the interface ip
185 * requires config.ipv6_host_id
186 * interface_ip - in: interface ip, out: local ipv6 host address
187 */
188void config_generate_local_ipv6_subnet(struct in6_addr *interface_ip) {
189 int i;
190
191 for(i = 2; i < 4; i++) {
192 interface_ip->s6_addr32[i] = Global_Clatd_Config.ipv6_host_id.s6_addr32[i];
193 }
194}
195
196/* function: subnet_from_interface
197 * finds the ipv6 subnet configured on the specified interface
198 * root - parsed configuration
199 * interface - network interface name
200 */
201int subnet_from_interface(cnode *root, const char *interface) {
202 union anyip *interface_ip;
203
204 if(!config_item_ip6(root, "ipv6_host_id", "::200:5E10:0:0", &Global_Clatd_Config.ipv6_host_id))
205 return 0;
206
207 interface_ip = getinterface_ip(interface, AF_INET6);
208 if(!interface_ip) {
209 logmsg(ANDROID_LOG_FATAL,"unable to find an ipv6 ip on interface %s",interface);
210 return 0;
211 }
212
213 memcpy(&Global_Clatd_Config.ipv6_local_subnet, &interface_ip->ip6, sizeof(struct in6_addr));
214 free(interface_ip);
215
216 config_generate_local_ipv6_subnet(&Global_Clatd_Config.ipv6_local_subnet);
217
218 return 1;
219}
220
221/* function: read_config
222 * reads the config file and parses it into the global variable Global_Clatd_Config. returns 0 on failure, 1 on success
223 * file - filename to parse
224 * uplink_interface - interface to use to reach the internet and supplier of address space
225 * plat_prefix - (optional) plat prefix to use, otherwise follow config file
Paul Jensena1c871c2014-05-30 13:19:10 -0400226 * net_id - (optional) netId to use, NETID_UNSET indicates use of default network
Daniel Drowna45056e2012-03-23 10:42:54 -0500227 */
Paul Jensena1c871c2014-05-30 13:19:10 -0400228int read_config(const char *file, const char *uplink_interface, const char *plat_prefix,
229 unsigned net_id) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500230 cnode *root = config_node("", "");
231 void *tmp_ptr = NULL;
232
233 if(!root) {
234 logmsg(ANDROID_LOG_FATAL,"out of memory");
235 return 0;
236 }
237
238 memset(&Global_Clatd_Config, '\0', sizeof(Global_Clatd_Config));
239
240 config_load_file(root, file);
241 if(root->first_child == NULL) {
242 logmsg(ANDROID_LOG_FATAL,"Could not read config file %s", file);
243 goto failed;
244 }
245
246 strncpy(Global_Clatd_Config.default_pdp_interface, uplink_interface, sizeof(Global_Clatd_Config.default_pdp_interface));
247
248 if(!subnet_from_interface(root,Global_Clatd_Config.default_pdp_interface))
249 goto failed;
250
251 if(!config_item_int16_t(root, "mtu", "-1", &Global_Clatd_Config.mtu))
252 goto failed;
253
254 if(!config_item_int16_t(root, "ipv4mtu", "-1", &Global_Clatd_Config.ipv4mtu))
255 goto failed;
256
257 if(!config_item_ip(root, "ipv4_local_subnet", DEFAULT_IPV4_LOCAL_SUBNET, &Global_Clatd_Config.ipv4_local_subnet))
258 goto failed;
259
JP Abgrall4e0dd832013-12-20 14:51:26 -0800260 if(!config_item_ip6(root, "ipv6_local_address", DEFAULT_IPV6_LOCAL_ADDRESS, &Global_Clatd_Config.ipv6_local_address))
261 goto failed;
262
Daniel Drowna45056e2012-03-23 10:42:54 -0500263 if(plat_prefix) { // plat subnet is coming from the command line
264 if(inet_pton(AF_INET6, plat_prefix, &Global_Clatd_Config.plat_subnet) <= 0) {
265 logmsg(ANDROID_LOG_FATAL,"invalid IPv6 address specified for plat prefix: %s", plat_prefix);
266 goto failed;
267 }
268 } else {
269 tmp_ptr = (void *)config_item_str(root, "plat_from_dns64", "yes");
270 if(!tmp_ptr || strcmp(tmp_ptr, "no") == 0) {
271 free(tmp_ptr);
272
273 if(!config_item_ip6(root, "plat_subnet", NULL, &Global_Clatd_Config.plat_subnet)) {
274 logmsg(ANDROID_LOG_FATAL, "plat_from_dns64 disabled, but no plat_subnet specified");
275 goto failed;
276 }
277 } else {
278 free(tmp_ptr);
279
280 if(!(Global_Clatd_Config.plat_from_dns64_hostname = config_item_str(root, "plat_from_dns64_hostname", DEFAULT_DNS64_DETECTION_HOSTNAME)))
281 goto failed;
Paul Jensena1c871c2014-05-30 13:19:10 -0400282 dns64_detection(net_id);
Daniel Drowna45056e2012-03-23 10:42:54 -0500283 }
284 }
285
286
287 return 1;
288
289failed:
290 free(root);
291 free_config();
292 return 0;
293}
294
295/* function; dump_config
296 * prints the current config
297 */
298void dump_config() {
299 char charbuffer[INET6_ADDRSTRLEN];
300
301 logmsg(ANDROID_LOG_DEBUG,"mtu = %d",Global_Clatd_Config.mtu);
302 logmsg(ANDROID_LOG_DEBUG,"ipv4mtu = %d",Global_Clatd_Config.ipv4mtu);
JP Abgrall4e0dd832013-12-20 14:51:26 -0800303 logmsg(ANDROID_LOG_DEBUG,"ipv6_local_address = %s",inet_ntop(AF_INET6, &Global_Clatd_Config.ipv6_local_address, charbuffer, sizeof(charbuffer)));
Daniel Drowna45056e2012-03-23 10:42:54 -0500304 logmsg(ANDROID_LOG_DEBUG,"ipv6_local_subnet = %s",inet_ntop(AF_INET6, &Global_Clatd_Config.ipv6_local_subnet, charbuffer, sizeof(charbuffer)));
305 logmsg(ANDROID_LOG_DEBUG,"ipv4_local_subnet = %s",inet_ntop(AF_INET, &Global_Clatd_Config.ipv4_local_subnet, charbuffer, sizeof(charbuffer)));
306 logmsg(ANDROID_LOG_DEBUG,"plat_subnet = %s",inet_ntop(AF_INET6, &Global_Clatd_Config.plat_subnet, charbuffer, sizeof(charbuffer)));
307 logmsg(ANDROID_LOG_DEBUG,"default_pdp_interface = %s",Global_Clatd_Config.default_pdp_interface);
308}