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