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 | |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 19 | #include <arpa/inet.h> |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 20 | #include <errno.h> |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 21 | #include <limits.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 25 | #include <unistd.h> |
| 26 | |
| 27 | #include <cutils/config_utils.h> |
Lorenzo Colitti | 98de595 | 2019-01-20 11:45:03 +0900 | [diff] [blame] | 28 | #include <netutils/checksum.h> |
Lorenzo Colitti | 2596f42 | 2014-11-10 17:00:02 -0800 | [diff] [blame] | 29 | #include <netutils/ifc.h> |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 30 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 31 | #include "clatd.h" |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 32 | #include "config.h" |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 33 | #include "getaddr.h" |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 34 | #include "logging.h" |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 35 | |
| 36 | struct clat_config Global_Clatd_Config; |
| 37 | |
| 38 | /* function: config_item_str |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 39 | * locates the config item and returns the pointer to a string, or NULL on failure. Caller frees |
| 40 | * pointer |
| 41 | * root - parsed configuration |
| 42 | * item_name - name of config item to locate |
| 43 | * defaultvar - value to use if config item isn't present |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 44 | */ |
| 45 | char *config_item_str(cnode *root, const char *item_name, const char *defaultvar) { |
| 46 | const char *tmp; |
| 47 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 48 | if (!(tmp = config_str(root, item_name, defaultvar))) { |
| 49 | logmsg(ANDROID_LOG_FATAL, "%s config item needed", item_name); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 50 | return NULL; |
| 51 | } |
| 52 | return strdup(tmp); |
| 53 | } |
| 54 | |
| 55 | /* function: config_item_int16_t |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 56 | * locates the config item, parses the integer, and returns the pointer ret_val_ptr, or NULL on |
| 57 | * failure |
| 58 | * root - parsed configuration |
| 59 | * item_name - name of config item to locate |
| 60 | * defaultvar - value to use if config item isn't present |
| 61 | * ret_val_ptr - pointer for return value storage |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 62 | */ |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 63 | int16_t *config_item_int16_t(cnode *root, const char *item_name, const char *defaultvar, |
| 64 | int16_t *ret_val_ptr) { |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 65 | const char *tmp; |
| 66 | char *endptr; |
| 67 | long int conf_int; |
| 68 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 69 | if (!(tmp = config_str(root, item_name, defaultvar))) { |
| 70 | logmsg(ANDROID_LOG_FATAL, "%s config item needed", item_name); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 71 | return NULL; |
| 72 | } |
| 73 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 74 | errno = 0; |
| 75 | conf_int = strtol(tmp, &endptr, 10); |
| 76 | if (errno > 0) { |
| 77 | logmsg(ANDROID_LOG_FATAL, "%s config item is not numeric: %s (error=%s)", item_name, tmp, |
| 78 | strerror(errno)); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 79 | return NULL; |
| 80 | } |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 81 | if (endptr == tmp || *tmp == '\0') { |
| 82 | logmsg(ANDROID_LOG_FATAL, "%s config item is not numeric: %s", item_name, tmp); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 83 | return NULL; |
| 84 | } |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 85 | if (*endptr != '\0') { |
| 86 | logmsg(ANDROID_LOG_FATAL, "%s config item contains non-numeric characters: %s", item_name, |
| 87 | endptr); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 88 | return NULL; |
| 89 | } |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 90 | if (conf_int > INT16_MAX || conf_int < INT16_MIN) { |
| 91 | logmsg(ANDROID_LOG_FATAL, "%s config item is too big/small: %d", item_name, conf_int); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 92 | return NULL; |
| 93 | } |
| 94 | *ret_val_ptr = conf_int; |
| 95 | return ret_val_ptr; |
| 96 | } |
| 97 | |
| 98 | /* function: config_item_ip |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 99 | * locates the config item, parses the ipv4 address, and returns the pointer ret_val_ptr, or NULL on |
| 100 | * failure |
| 101 | * root - parsed configuration |
| 102 | * item_name - name of config item to locate |
| 103 | * defaultvar - value to use if config item isn't present |
| 104 | * ret_val_ptr - pointer for return value storage |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 105 | */ |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 106 | struct in_addr *config_item_ip(cnode *root, const char *item_name, const char *defaultvar, |
| 107 | struct in_addr *ret_val_ptr) { |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 108 | const char *tmp; |
| 109 | int status; |
| 110 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 111 | if (!(tmp = config_str(root, item_name, defaultvar))) { |
| 112 | logmsg(ANDROID_LOG_FATAL, "%s config item needed", item_name); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 113 | return NULL; |
| 114 | } |
| 115 | |
| 116 | status = inet_pton(AF_INET, tmp, ret_val_ptr); |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 117 | if (status <= 0) { |
| 118 | logmsg(ANDROID_LOG_FATAL, "invalid IPv4 address specified for %s: %s", item_name, tmp); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 119 | return NULL; |
| 120 | } |
| 121 | |
| 122 | return ret_val_ptr; |
| 123 | } |
| 124 | |
| 125 | /* function: config_item_ip6 |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 126 | * locates the config item, parses the ipv6 address, and returns the pointer ret_val_ptr, or NULL on |
| 127 | * failure |
| 128 | * root - parsed configuration |
| 129 | * item_name - name of config item to locate |
| 130 | * defaultvar - value to use if config item isn't present |
| 131 | * ret_val_ptr - pointer for return value storage |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 132 | */ |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 133 | struct in6_addr *config_item_ip6(cnode *root, const char *item_name, const char *defaultvar, |
| 134 | struct in6_addr *ret_val_ptr) { |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 135 | const char *tmp; |
| 136 | int status; |
| 137 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 138 | if (!(tmp = config_str(root, item_name, defaultvar))) { |
| 139 | logmsg(ANDROID_LOG_FATAL, "%s config item needed", item_name); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 140 | return NULL; |
| 141 | } |
| 142 | |
| 143 | status = inet_pton(AF_INET6, tmp, ret_val_ptr); |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 144 | if (status <= 0) { |
| 145 | logmsg(ANDROID_LOG_FATAL, "invalid IPv6 address specified for %s: %s", item_name, tmp); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 146 | return NULL; |
| 147 | } |
| 148 | |
| 149 | return ret_val_ptr; |
| 150 | } |
| 151 | |
Lorenzo Colitti | 9808952 | 2014-10-09 22:29:45 +0900 | [diff] [blame] | 152 | /* function: ipv6_prefix_equal |
| 153 | * compares the prefixes two ipv6 addresses. assumes the prefix lengths are both /64. |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 154 | * a1 - first address |
| 155 | * a2 - second address |
| 156 | * returns: 0 if the subnets are different, 1 if they are the same. |
Lorenzo Colitti | 9808952 | 2014-10-09 22:29:45 +0900 | [diff] [blame] | 157 | */ |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 158 | int ipv6_prefix_equal(struct in6_addr *a1, struct in6_addr *a2) { return !memcmp(a1, a2, 8); } |
Lorenzo Colitti | 9808952 | 2014-10-09 22:29:45 +0900 | [diff] [blame] | 159 | |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 160 | /* function: read_config |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 161 | * reads the config file and parses it into the global variable Global_Clatd_Config. returns 0 on |
| 162 | * failure, 1 on success |
| 163 | * file - filename to parse |
| 164 | * uplink_interface - interface to use to reach the internet and supplier of address space |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 165 | */ |
Lorenzo Colitti | 27da0ad | 2020-06-01 12:15:20 +0900 | [diff] [blame] | 166 | int read_config(const char *file, const char *uplink_interface) { |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 167 | cnode *root = config_node("", ""); |
Lorenzo Colitti | 2596f42 | 2014-11-10 17:00:02 -0800 | [diff] [blame] | 168 | unsigned flags; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 169 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 170 | if (!root) { |
| 171 | logmsg(ANDROID_LOG_FATAL, "out of memory"); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | memset(&Global_Clatd_Config, '\0', sizeof(Global_Clatd_Config)); |
| 176 | |
| 177 | config_load_file(root, file); |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 178 | if (root->first_child == NULL) { |
| 179 | logmsg(ANDROID_LOG_FATAL, "Could not read config file %s", file); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 180 | goto failed; |
| 181 | } |
| 182 | |
Lorenzo Colitti | 1352a3a | 2014-10-21 13:41:21 +0900 | [diff] [blame] | 183 | Global_Clatd_Config.default_pdp_interface = strdup(uplink_interface); |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 184 | if (!Global_Clatd_Config.default_pdp_interface) goto failed; |
| 185 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 186 | if (!config_item_ip(root, "ipv4_local_subnet", DEFAULT_IPV4_LOCAL_SUBNET, |
| 187 | &Global_Clatd_Config.ipv4_local_subnet)) |
Lorenzo Colitti | 1352a3a | 2014-10-21 13:41:21 +0900 | [diff] [blame] | 188 | goto failed; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 189 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 190 | if (!config_item_int16_t(root, "ipv4_local_prefixlen", DEFAULT_IPV4_LOCAL_PREFIXLEN, |
| 191 | &Global_Clatd_Config.ipv4_local_prefixlen)) |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 192 | goto failed; |
| 193 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 194 | if (!config_item_ip6(root, "ipv6_host_id", "::", &Global_Clatd_Config.ipv6_host_id)) goto failed; |
Lorenzo Colitti | 9808952 | 2014-10-09 22:29:45 +0900 | [diff] [blame] | 195 | |
Lorenzo Colitti | 2596f42 | 2014-11-10 17:00:02 -0800 | [diff] [blame] | 196 | /* In order to prevent multiple devices attempting to use the same clat address, never use a |
| 197 | statically-configured interface ID on a broadcast interface such as wifi. */ |
| 198 | if (!IN6_IS_ADDR_UNSPECIFIED(&Global_Clatd_Config.ipv6_host_id)) { |
| 199 | ifc_init(); |
| 200 | ifc_get_info(Global_Clatd_Config.default_pdp_interface, NULL, NULL, &flags); |
| 201 | ifc_close(); |
| 202 | Global_Clatd_Config.use_dynamic_iid = (flags & IFF_BROADCAST) != 0; |
| 203 | } else { |
| 204 | Global_Clatd_Config.use_dynamic_iid = 1; |
| 205 | } |
| 206 | |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 207 | return 1; |
| 208 | |
| 209 | failed: |
| 210 | free(root); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 211 | return 0; |
| 212 | } |