Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame^] | 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 "setroute.h" |
| 35 | |
| 36 | struct 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 | */ |
| 44 | char *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 | */ |
| 61 | int16_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 | */ |
| 100 | struct 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 | */ |
| 125 | struct 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 | */ |
| 146 | void 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 |
| 155 | */ |
| 156 | void dns64_detection() { |
| 157 | int i, 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,&tmp_ptr); |
| 164 | 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 | */ |
| 188 | void 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 | */ |
| 201 | int 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 |
| 226 | */ |
| 227 | int read_config(const char *file, const char *uplink_interface, const char *plat_prefix) { |
| 228 | cnode *root = config_node("", ""); |
| 229 | void *tmp_ptr = NULL; |
| 230 | |
| 231 | if(!root) { |
| 232 | logmsg(ANDROID_LOG_FATAL,"out of memory"); |
| 233 | return 0; |
| 234 | } |
| 235 | |
| 236 | memset(&Global_Clatd_Config, '\0', sizeof(Global_Clatd_Config)); |
| 237 | |
| 238 | config_load_file(root, file); |
| 239 | if(root->first_child == NULL) { |
| 240 | logmsg(ANDROID_LOG_FATAL,"Could not read config file %s", file); |
| 241 | goto failed; |
| 242 | } |
| 243 | |
| 244 | strncpy(Global_Clatd_Config.default_pdp_interface, uplink_interface, sizeof(Global_Clatd_Config.default_pdp_interface)); |
| 245 | |
| 246 | if(!subnet_from_interface(root,Global_Clatd_Config.default_pdp_interface)) |
| 247 | goto failed; |
| 248 | |
| 249 | if(!config_item_int16_t(root, "mtu", "-1", &Global_Clatd_Config.mtu)) |
| 250 | goto failed; |
| 251 | |
| 252 | if(!config_item_int16_t(root, "ipv4mtu", "-1", &Global_Clatd_Config.ipv4mtu)) |
| 253 | goto failed; |
| 254 | |
| 255 | if(!config_item_ip(root, "ipv4_local_subnet", DEFAULT_IPV4_LOCAL_SUBNET, &Global_Clatd_Config.ipv4_local_subnet)) |
| 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(); |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | |
| 282 | return 1; |
| 283 | |
| 284 | failed: |
| 285 | free(root); |
| 286 | free_config(); |
| 287 | return 0; |
| 288 | } |
| 289 | |
| 290 | /* function; dump_config |
| 291 | * prints the current config |
| 292 | */ |
| 293 | void 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_subnet = %s",inet_ntop(AF_INET6, &Global_Clatd_Config.ipv6_local_subnet, charbuffer, sizeof(charbuffer))); |
| 299 | logmsg(ANDROID_LOG_DEBUG,"ipv4_local_subnet = %s",inet_ntop(AF_INET, &Global_Clatd_Config.ipv4_local_subnet, charbuffer, sizeof(charbuffer))); |
| 300 | logmsg(ANDROID_LOG_DEBUG,"plat_subnet = %s",inet_ntop(AF_INET6, &Global_Clatd_Config.plat_subnet, charbuffer, sizeof(charbuffer))); |
| 301 | logmsg(ANDROID_LOG_DEBUG,"default_pdp_interface = %s",Global_Clatd_Config.default_pdp_interface); |
| 302 | } |