1/*
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
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
154 * net_id - (optional) netId to use, NETID_UNSET indicates use of default network
155 */
156void dns64_detection(unsigned net_id) {
157  int backoff_sleep, status;
158  struct in6_addr tmp_ptr;
159
160  backoff_sleep = 1;
161
162  while(1) {
163    status = plat_prefix(Global_Clatd_Config.plat_from_dns64_hostname,net_id,&tmp_ptr);
164    if(status > 0) {
165      memcpy(&Global_Clatd_Config.plat_subnet, &tmp_ptr, sizeof(struct in6_addr));
166      return;
167    }
168    logmsg(ANDROID_LOG_WARN, "dns64_detection -- error, sleeping for %d seconds", backoff_sleep);
169    sleep(backoff_sleep);
170    backoff_sleep *= 2;
171    if(backoff_sleep >= 120) {
172      backoff_sleep = 120;
173    }
174  }
175}
176
177
178/* function: config_generate_local_ipv6_subnet
179 * generates the local ipv6 subnet when given the interface ip
180 * requires config.ipv6_host_id
181 * interface_ip - in: interface ip, out: local ipv6 host address
182 */
183void config_generate_local_ipv6_subnet(struct in6_addr *interface_ip) {
184  int i;
185
186  for(i = 2; i < 4; i++) {
187    interface_ip->s6_addr32[i] = Global_Clatd_Config.ipv6_host_id.s6_addr32[i];
188  }
189}
190
191/* function: subnet_from_interface
192 * finds the ipv6 subnet configured on the specified interface
193 * root      - parsed configuration
194 * interface - network interface name
195 */
196int subnet_from_interface(cnode *root, const char *interface) {
197  union anyip *interface_ip;
198
199  if(!config_item_ip6(root, "ipv6_host_id", "::200:5E10:0:0", &Global_Clatd_Config.ipv6_host_id))
200    return 0;
201
202  interface_ip = getinterface_ip(interface, AF_INET6);
203  if(!interface_ip) {
204    logmsg(ANDROID_LOG_FATAL,"unable to find an ipv6 ip on interface %s",interface);
205    return 0;
206  }
207
208  memcpy(&Global_Clatd_Config.ipv6_local_subnet, &interface_ip->ip6, sizeof(struct in6_addr));
209  free(interface_ip);
210
211  config_generate_local_ipv6_subnet(&Global_Clatd_Config.ipv6_local_subnet);
212
213  return 1;
214}
215
216/* function: read_config
217 * reads the config file and parses it into the global variable Global_Clatd_Config. returns 0 on failure, 1 on success
218 * file             - filename to parse
219 * uplink_interface - interface to use to reach the internet and supplier of address space
220 * plat_prefix      - (optional) plat prefix to use, otherwise follow config file
221 * net_id           - (optional) netId to use, NETID_UNSET indicates use of default network
222 */
223int read_config(const char *file, const char *uplink_interface, const char *plat_prefix,
224        unsigned net_id) {
225  cnode *root = config_node("", "");
226  void *tmp_ptr = NULL;
227
228  if(!root) {
229    logmsg(ANDROID_LOG_FATAL,"out of memory");
230    return 0;
231  }
232
233  memset(&Global_Clatd_Config, '\0', sizeof(Global_Clatd_Config));
234
235  config_load_file(root, file);
236  if(root->first_child == NULL) {
237    logmsg(ANDROID_LOG_FATAL,"Could not read config file %s", file);
238    goto failed;
239  }
240
241  strncpy(Global_Clatd_Config.default_pdp_interface, uplink_interface, sizeof(Global_Clatd_Config.default_pdp_interface));
242
243  if(!subnet_from_interface(root,Global_Clatd_Config.default_pdp_interface))
244    goto failed;
245
246  if(!config_item_int16_t(root, "mtu", "-1", &Global_Clatd_Config.mtu))
247    goto failed;
248
249  if(!config_item_int16_t(root, "ipv4mtu", "-1", &Global_Clatd_Config.ipv4mtu))
250    goto failed;
251
252  if(!config_item_ip(root, "ipv4_local_subnet", DEFAULT_IPV4_LOCAL_SUBNET, &Global_Clatd_Config.ipv4_local_subnet))
253    goto failed;
254
255  if(!config_item_ip6(root, "ipv6_local_address", DEFAULT_IPV6_LOCAL_ADDRESS, &Global_Clatd_Config.ipv6_local_address))
256    goto failed;
257
258  if(plat_prefix) { // plat subnet is coming from the command line
259    if(inet_pton(AF_INET6, plat_prefix, &Global_Clatd_Config.plat_subnet) <= 0) {
260      logmsg(ANDROID_LOG_FATAL,"invalid IPv6 address specified for plat prefix: %s", plat_prefix);
261      goto failed;
262    }
263  } else {
264    tmp_ptr = (void *)config_item_str(root, "plat_from_dns64", "yes");
265    if(!tmp_ptr || strcmp(tmp_ptr, "no") == 0) {
266      free(tmp_ptr);
267
268      if(!config_item_ip6(root, "plat_subnet", NULL, &Global_Clatd_Config.plat_subnet)) {
269        logmsg(ANDROID_LOG_FATAL, "plat_from_dns64 disabled, but no plat_subnet specified");
270        goto failed;
271      }
272    } else {
273      free(tmp_ptr);
274
275      if(!(Global_Clatd_Config.plat_from_dns64_hostname = config_item_str(root, "plat_from_dns64_hostname", DEFAULT_DNS64_DETECTION_HOSTNAME)))
276        goto failed;
277      dns64_detection(net_id);
278    }
279  }
280
281
282  return 1;
283
284failed:
285  free(root);
286  free_config();
287  return 0;
288}
289
290/* function; dump_config
291 * prints the current config
292 */
293void dump_config() {
294  char charbuffer[INET6_ADDRSTRLEN];
295
296  logmsg(ANDROID_LOG_DEBUG,"mtu = %d",Global_Clatd_Config.mtu);
297  logmsg(ANDROID_LOG_DEBUG,"ipv4mtu = %d",Global_Clatd_Config.ipv4mtu);
298  logmsg(ANDROID_LOG_DEBUG,"ipv6_local_address = %s",inet_ntop(AF_INET6, &Global_Clatd_Config.ipv6_local_address, charbuffer, sizeof(charbuffer)));
299  logmsg(ANDROID_LOG_DEBUG,"ipv6_local_subnet = %s",inet_ntop(AF_INET6, &Global_Clatd_Config.ipv6_local_subnet, charbuffer, sizeof(charbuffer)));
300  logmsg(ANDROID_LOG_DEBUG,"ipv4_local_subnet = %s",inet_ntop(AF_INET, &Global_Clatd_Config.ipv4_local_subnet, charbuffer, sizeof(charbuffer)));
301  logmsg(ANDROID_LOG_DEBUG,"plat_subnet = %s",inet_ntop(AF_INET6, &Global_Clatd_Config.plat_subnet, charbuffer, sizeof(charbuffer)));
302  logmsg(ANDROID_LOG_DEBUG,"default_pdp_interface = %s",Global_Clatd_Config.default_pdp_interface);
303}
304