config.c revision 9808952bdf2fa6ce4cbd790d1967691bb95bf327
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#include "checksum.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: ipv6_prefix_equal
154 * compares the prefixes two ipv6 addresses. assumes the prefix lengths are both /64.
155 * a1 - first address
156 * a2 - second address
157 * returns: 0 if the subnets are different, 1 if they are the same.
158 */
159int ipv6_prefix_equal(struct in6_addr *a1, struct in6_addr *a2) {
160    return !memcmp(a1, a2, 8);
161}
162
163/* function: dns64_detection
164 * does dns lookups to set the plat subnet or exits on failure, waits forever for a dns response with a query backoff timer
165 * net_id - (optional) netId to use, NETID_UNSET indicates use of default network
166 */
167void dns64_detection(unsigned net_id) {
168  int backoff_sleep, status;
169  struct in6_addr tmp_ptr;
170
171  backoff_sleep = 1;
172
173  while(1) {
174    status = plat_prefix(Global_Clatd_Config.plat_from_dns64_hostname,net_id,&tmp_ptr);
175    if(status > 0) {
176      memcpy(&Global_Clatd_Config.plat_subnet, &tmp_ptr, sizeof(struct in6_addr));
177      return;
178    }
179    logmsg(ANDROID_LOG_WARN, "dns64_detection -- error, sleeping for %d seconds", backoff_sleep);
180    sleep(backoff_sleep);
181    backoff_sleep *= 2;
182    if(backoff_sleep >= 120) {
183      backoff_sleep = 120;
184    }
185  }
186}
187
188
189void gen_random_iid(struct in6_addr *myaddr, struct in_addr *ipv4_local_subnet,
190                    struct in6_addr *plat_subnet) {
191  // Fill last 8 bytes of IPv6 address with random bits.
192  arc4random_buf(&myaddr->s6_addr[8], 8);
193
194  // Make the IID checksum-neutral. That is, make it so that:
195  //   checksum(Local IPv4 | Remote IPv4) = checksum(Local IPv6 | Remote IPv6)
196  // in other words (because remote IPv6 = NAT64 prefix | Remote IPv4):
197  //   checksum(Local IPv4) = checksum(Local IPv6 | NAT64 prefix)
198  // Do this by adjusting the two bytes in the middle of the IID.
199
200  uint16_t middlebytes = (myaddr->s6_addr[11] << 8) + myaddr->s6_addr[12];
201
202  uint32_t c1 = ip_checksum_add(0, ipv4_local_subnet, sizeof(*ipv4_local_subnet));
203  uint32_t c2 = ip_checksum_add(0, plat_subnet, sizeof(*plat_subnet)) +
204                ip_checksum_add(0, myaddr, sizeof(*myaddr));
205
206  uint16_t delta = ip_checksum_adjust(middlebytes, c1, c2);
207  myaddr->s6_addr[11] = delta >> 8;
208  myaddr->s6_addr[12] = delta & 0xff;
209}
210
211/* function: config_generate_local_ipv6_subnet
212 * generates the local ipv6 subnet when given the interface ip
213 * requires config.ipv6_host_id
214 * interface_ip - in: interface ip, out: local ipv6 host address
215 */
216void config_generate_local_ipv6_subnet(struct in6_addr *interface_ip) {
217  int i;
218
219  if (IN6_IS_ADDR_UNSPECIFIED(&Global_Clatd_Config.ipv6_host_id)) {
220    /* Generate a random interface ID. */
221    gen_random_iid(interface_ip,
222                   &Global_Clatd_Config.ipv4_local_subnet,
223                   &Global_Clatd_Config.plat_subnet);
224  } else {
225    /* Use the specified interface ID. */
226    for(i = 2; i < 4; i++) {
227      interface_ip->s6_addr32[i] = Global_Clatd_Config.ipv6_host_id.s6_addr32[i];
228    }
229  }
230}
231
232/* function: subnet_from_interface
233 * finds the ipv6 subnet configured on the specified interface
234 * root      - parsed configuration
235 * interface - network interface name
236 */
237int subnet_from_interface(cnode *root, const char *interface) {
238  union anyip *interface_ip;
239  char addrstr[INET6_ADDRSTRLEN];
240
241  if(!config_item_ip6(root, "ipv6_host_id", "::", &Global_Clatd_Config.ipv6_host_id))
242    return 0;
243
244  // TODO: check that the prefix length is /64.
245  interface_ip = getinterface_ip(interface, AF_INET6);
246  if(!interface_ip) {
247    logmsg(ANDROID_LOG_FATAL,"unable to find an ipv6 ip on interface %s",interface);
248    return 0;
249  }
250
251  memcpy(&Global_Clatd_Config.ipv6_local_subnet, &interface_ip->ip6, sizeof(struct in6_addr));
252  free(interface_ip);
253
254  config_generate_local_ipv6_subnet(&Global_Clatd_Config.ipv6_local_subnet);
255
256  inet_ntop(AF_INET6, &Global_Clatd_Config.ipv6_local_subnet, addrstr, sizeof(addrstr));
257  logmsg(ANDROID_LOG_INFO, "Using %s on %s", addrstr, interface);
258
259  return 1;
260}
261
262/* function: read_config
263 * reads the config file and parses it into the global variable Global_Clatd_Config. returns 0 on failure, 1 on success
264 * file             - filename to parse
265 * uplink_interface - interface to use to reach the internet and supplier of address space
266 * plat_prefix      - (optional) plat prefix to use, otherwise follow config file
267 * net_id           - (optional) netId to use, NETID_UNSET indicates use of default network
268 */
269int read_config(const char *file, const char *uplink_interface, const char *plat_prefix,
270        unsigned net_id) {
271  cnode *root = config_node("", "");
272  void *tmp_ptr = NULL;
273
274  if(!root) {
275    logmsg(ANDROID_LOG_FATAL,"out of memory");
276    return 0;
277  }
278
279  memset(&Global_Clatd_Config, '\0', sizeof(Global_Clatd_Config));
280
281  config_load_file(root, file);
282  if(root->first_child == NULL) {
283    logmsg(ANDROID_LOG_FATAL,"Could not read config file %s", file);
284    goto failed;
285  }
286
287  strncpy(Global_Clatd_Config.default_pdp_interface, uplink_interface, sizeof(Global_Clatd_Config.default_pdp_interface));
288
289  if(!config_item_int16_t(root, "mtu", "-1", &Global_Clatd_Config.mtu))
290    goto failed;
291
292  if(!config_item_int16_t(root, "ipv4mtu", "-1", &Global_Clatd_Config.ipv4mtu))
293    goto failed;
294
295  if(!config_item_ip(root, "ipv4_local_subnet", DEFAULT_IPV4_LOCAL_SUBNET, &Global_Clatd_Config.ipv4_local_subnet))
296    goto failed;
297
298  if(plat_prefix) { // plat subnet is coming from the command line
299    if(inet_pton(AF_INET6, plat_prefix, &Global_Clatd_Config.plat_subnet) <= 0) {
300      logmsg(ANDROID_LOG_FATAL,"invalid IPv6 address specified for plat prefix: %s", plat_prefix);
301      goto failed;
302    }
303  } else {
304    tmp_ptr = (void *)config_item_str(root, "plat_from_dns64", "yes");
305    if(!tmp_ptr || strcmp(tmp_ptr, "no") == 0) {
306      free(tmp_ptr);
307
308      if(!config_item_ip6(root, "plat_subnet", NULL, &Global_Clatd_Config.plat_subnet)) {
309        logmsg(ANDROID_LOG_FATAL, "plat_from_dns64 disabled, but no plat_subnet specified");
310        goto failed;
311      }
312    } else {
313      free(tmp_ptr);
314
315      if(!(Global_Clatd_Config.plat_from_dns64_hostname = config_item_str(root, "plat_from_dns64_hostname", DEFAULT_DNS64_DETECTION_HOSTNAME)))
316        goto failed;
317      dns64_detection(net_id);
318    }
319  }
320
321  if(!subnet_from_interface(root,Global_Clatd_Config.default_pdp_interface))
322    goto failed;
323
324
325  return 1;
326
327failed:
328  free(root);
329  free_config();
330  return 0;
331}
332
333/* function; dump_config
334 * prints the current config
335 */
336void dump_config() {
337  char charbuffer[INET6_ADDRSTRLEN];
338
339  logmsg(ANDROID_LOG_DEBUG,"mtu = %d",Global_Clatd_Config.mtu);
340  logmsg(ANDROID_LOG_DEBUG,"ipv4mtu = %d",Global_Clatd_Config.ipv4mtu);
341  logmsg(ANDROID_LOG_DEBUG,"ipv6_local_subnet = %s",inet_ntop(AF_INET6, &Global_Clatd_Config.ipv6_local_subnet, charbuffer, sizeof(charbuffer)));
342  logmsg(ANDROID_LOG_DEBUG,"ipv4_local_subnet = %s",inet_ntop(AF_INET, &Global_Clatd_Config.ipv4_local_subnet, charbuffer, sizeof(charbuffer)));
343  logmsg(ANDROID_LOG_DEBUG,"plat_subnet = %s",inet_ntop(AF_INET6, &Global_Clatd_Config.plat_subnet, charbuffer, sizeof(charbuffer)));
344  logmsg(ANDROID_LOG_DEBUG,"default_pdp_interface = %s",Global_Clatd_Config.default_pdp_interface);
345}
346