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 | |
Lorenzo Colitti | 798f993 | 2014-10-31 21:54:33 +0900 | [diff] [blame] | 160 | /* function: gen_random_iid |
| 161 | * picks a random interface ID that is checksum neutral with the IPv4 address and the NAT64 prefix |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 162 | * myaddr - IPv6 address to write to |
| 163 | * ipv4_local_subnet - clat IPv4 address |
| 164 | * plat_subnet - NAT64 prefix |
Lorenzo Colitti | 798f993 | 2014-10-31 21:54:33 +0900 | [diff] [blame] | 165 | */ |
Lorenzo Colitti | 9808952 | 2014-10-09 22:29:45 +0900 | [diff] [blame] | 166 | void gen_random_iid(struct in6_addr *myaddr, struct in_addr *ipv4_local_subnet, |
| 167 | struct in6_addr *plat_subnet) { |
| 168 | // Fill last 8 bytes of IPv6 address with random bits. |
| 169 | arc4random_buf(&myaddr->s6_addr[8], 8); |
| 170 | |
| 171 | // Make the IID checksum-neutral. That is, make it so that: |
| 172 | // checksum(Local IPv4 | Remote IPv4) = checksum(Local IPv6 | Remote IPv6) |
| 173 | // in other words (because remote IPv6 = NAT64 prefix | Remote IPv4): |
| 174 | // checksum(Local IPv4) = checksum(Local IPv6 | NAT64 prefix) |
| 175 | // Do this by adjusting the two bytes in the middle of the IID. |
| 176 | |
| 177 | uint16_t middlebytes = (myaddr->s6_addr[11] << 8) + myaddr->s6_addr[12]; |
| 178 | |
| 179 | uint32_t c1 = ip_checksum_add(0, ipv4_local_subnet, sizeof(*ipv4_local_subnet)); |
| 180 | uint32_t c2 = ip_checksum_add(0, plat_subnet, sizeof(*plat_subnet)) + |
| 181 | ip_checksum_add(0, myaddr, sizeof(*myaddr)); |
| 182 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 183 | uint16_t delta = ip_checksum_adjust(middlebytes, c1, c2); |
Lorenzo Colitti | 9808952 | 2014-10-09 22:29:45 +0900 | [diff] [blame] | 184 | myaddr->s6_addr[11] = delta >> 8; |
| 185 | myaddr->s6_addr[12] = delta & 0xff; |
| 186 | } |
| 187 | |
Lorenzo Colitti | 798f993 | 2014-10-31 21:54:33 +0900 | [diff] [blame] | 188 | // Factored out to a separate function for testability. |
| 189 | int connect_is_ipv4_address_free(in_addr_t addr) { |
Maciej Żenczykowski | 60bce37 | 2019-04-09 01:58:52 -0700 | [diff] [blame] | 190 | int s = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0); |
Lorenzo Colitti | 798f993 | 2014-10-31 21:54:33 +0900 | [diff] [blame] | 191 | if (s == -1) { |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | // Attempt to connect to the address. If the connection succeeds and getsockname returns the same |
| 196 | // the address then the address is already assigned to the system and we can't use it. |
Maciej Żenczykowski | a02523b | 2019-10-31 23:53:37 -0700 | [diff] [blame] | 197 | struct sockaddr_in sin = { .sin_family = AF_INET, .sin_addr = { addr }, .sin_port = htons(53) }; |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 198 | socklen_t len = sizeof(sin); |
| 199 | int inuse = connect(s, (struct sockaddr *)&sin, sizeof(sin)) == 0 && |
| 200 | getsockname(s, (struct sockaddr *)&sin, &len) == 0 && (size_t)len >= sizeof(sin) && |
Lorenzo Colitti | 798f993 | 2014-10-31 21:54:33 +0900 | [diff] [blame] | 201 | sin.sin_addr.s_addr == addr; |
| 202 | |
| 203 | close(s); |
| 204 | return !inuse; |
| 205 | } |
| 206 | |
| 207 | addr_free_func config_is_ipv4_address_free = connect_is_ipv4_address_free; |
| 208 | |
| 209 | /* function: config_select_ipv4_address |
| 210 | * picks a free IPv4 address, starting from ip and trying all addresses in the prefix in order |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 211 | * ip - the IP address from the configuration file |
| 212 | * prefixlen - the length of the prefix from which addresses may be selected. |
| 213 | * returns: the IPv4 address, or INADDR_NONE if no addresses were available |
Lorenzo Colitti | 798f993 | 2014-10-31 21:54:33 +0900 | [diff] [blame] | 214 | */ |
| 215 | in_addr_t config_select_ipv4_address(const struct in_addr *ip, int16_t prefixlen) { |
| 216 | in_addr_t chosen = INADDR_NONE; |
| 217 | |
| 218 | // Don't accept prefixes that are too large because we scan addresses one by one. |
| 219 | if (prefixlen < 16 || prefixlen > 32) { |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 220 | return chosen; |
Lorenzo Colitti | 798f993 | 2014-10-31 21:54:33 +0900 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | // All these are in host byte order. |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 224 | in_addr_t mask = 0xffffffff >> (32 - prefixlen) << (32 - prefixlen); |
| 225 | in_addr_t ipv4 = ntohl(ip->s_addr); |
Lorenzo Colitti | 798f993 | 2014-10-31 21:54:33 +0900 | [diff] [blame] | 226 | in_addr_t first_ipv4 = ipv4; |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 227 | in_addr_t prefix = ipv4 & mask; |
Lorenzo Colitti | 798f993 | 2014-10-31 21:54:33 +0900 | [diff] [blame] | 228 | |
| 229 | // Pick the first IPv4 address in the pool, wrapping around if necessary. |
| 230 | // So, for example, 192.0.0.4 -> 192.0.0.5 -> 192.0.0.6 -> 192.0.0.7 -> 192.0.0.0. |
| 231 | do { |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 232 | if (config_is_ipv4_address_free(htonl(ipv4))) { |
| 233 | chosen = htonl(ipv4); |
| 234 | break; |
| 235 | } |
| 236 | ipv4 = prefix | ((ipv4 + 1) & ~mask); |
Lorenzo Colitti | 798f993 | 2014-10-31 21:54:33 +0900 | [diff] [blame] | 237 | } while (ipv4 != first_ipv4); |
| 238 | |
| 239 | return chosen; |
| 240 | } |
| 241 | |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 242 | /* function: config_generate_local_ipv6_subnet |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 243 | * generates the local ipv6 subnet when given the interface ip requires config.ipv6_host_id |
| 244 | * interface_ip - in: interface ip, out: local ipv6 host address |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 245 | */ |
| 246 | void config_generate_local_ipv6_subnet(struct in6_addr *interface_ip) { |
| 247 | int i; |
| 248 | |
Lorenzo Colitti | 2596f42 | 2014-11-10 17:00:02 -0800 | [diff] [blame] | 249 | if (Global_Clatd_Config.use_dynamic_iid) { |
Lorenzo Colitti | 9808952 | 2014-10-09 22:29:45 +0900 | [diff] [blame] | 250 | /* Generate a random interface ID. */ |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 251 | gen_random_iid(interface_ip, &Global_Clatd_Config.ipv4_local_subnet, |
Lorenzo Colitti | 9808952 | 2014-10-09 22:29:45 +0900 | [diff] [blame] | 252 | &Global_Clatd_Config.plat_subnet); |
| 253 | } else { |
| 254 | /* Use the specified interface ID. */ |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 255 | for (i = 2; i < 4; i++) { |
Lorenzo Colitti | 9808952 | 2014-10-09 22:29:45 +0900 | [diff] [blame] | 256 | interface_ip->s6_addr32[i] = Global_Clatd_Config.ipv6_host_id.s6_addr32[i]; |
| 257 | } |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 261 | /* function: read_config |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 262 | * reads the config file and parses it into the global variable Global_Clatd_Config. returns 0 on |
| 263 | * failure, 1 on success |
| 264 | * file - filename to parse |
| 265 | * 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] | 266 | */ |
Lorenzo Colitti | 27da0ad | 2020-06-01 12:15:20 +0900 | [diff] [blame^] | 267 | int read_config(const char *file, const char *uplink_interface) { |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 268 | cnode *root = config_node("", ""); |
Lorenzo Colitti | 2596f42 | 2014-11-10 17:00:02 -0800 | [diff] [blame] | 269 | unsigned flags; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 270 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 271 | if (!root) { |
| 272 | logmsg(ANDROID_LOG_FATAL, "out of memory"); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | memset(&Global_Clatd_Config, '\0', sizeof(Global_Clatd_Config)); |
| 277 | |
| 278 | config_load_file(root, file); |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 279 | if (root->first_child == NULL) { |
| 280 | logmsg(ANDROID_LOG_FATAL, "Could not read config file %s", file); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 281 | goto failed; |
| 282 | } |
| 283 | |
Lorenzo Colitti | 1352a3a | 2014-10-21 13:41:21 +0900 | [diff] [blame] | 284 | Global_Clatd_Config.default_pdp_interface = strdup(uplink_interface); |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 285 | if (!Global_Clatd_Config.default_pdp_interface) goto failed; |
| 286 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 287 | if (!config_item_ip(root, "ipv4_local_subnet", DEFAULT_IPV4_LOCAL_SUBNET, |
| 288 | &Global_Clatd_Config.ipv4_local_subnet)) |
Lorenzo Colitti | 1352a3a | 2014-10-21 13:41:21 +0900 | [diff] [blame] | 289 | goto failed; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 290 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 291 | if (!config_item_int16_t(root, "ipv4_local_prefixlen", DEFAULT_IPV4_LOCAL_PREFIXLEN, |
| 292 | &Global_Clatd_Config.ipv4_local_prefixlen)) |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 293 | goto failed; |
| 294 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 295 | 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] | 296 | |
Lorenzo Colitti | 2596f42 | 2014-11-10 17:00:02 -0800 | [diff] [blame] | 297 | /* In order to prevent multiple devices attempting to use the same clat address, never use a |
| 298 | statically-configured interface ID on a broadcast interface such as wifi. */ |
| 299 | if (!IN6_IS_ADDR_UNSPECIFIED(&Global_Clatd_Config.ipv6_host_id)) { |
| 300 | ifc_init(); |
| 301 | ifc_get_info(Global_Clatd_Config.default_pdp_interface, NULL, NULL, &flags); |
| 302 | ifc_close(); |
| 303 | Global_Clatd_Config.use_dynamic_iid = (flags & IFF_BROADCAST) != 0; |
| 304 | } else { |
| 305 | Global_Clatd_Config.use_dynamic_iid = 1; |
| 306 | } |
| 307 | |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 308 | return 1; |
| 309 | |
| 310 | failed: |
| 311 | free(root); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 312 | return 0; |
| 313 | } |
| 314 | |
| 315 | /* function; dump_config |
| 316 | * prints the current config |
| 317 | */ |
| 318 | void dump_config() { |
| 319 | char charbuffer[INET6_ADDRSTRLEN]; |
| 320 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 321 | logmsg( |
| 322 | ANDROID_LOG_DEBUG, "ipv6_local_subnet = %s", |
| 323 | inet_ntop(AF_INET6, &Global_Clatd_Config.ipv6_local_subnet, charbuffer, sizeof(charbuffer))); |
| 324 | logmsg( |
| 325 | ANDROID_LOG_DEBUG, "ipv4_local_subnet = %s", |
| 326 | inet_ntop(AF_INET, &Global_Clatd_Config.ipv4_local_subnet, charbuffer, sizeof(charbuffer))); |
| 327 | logmsg(ANDROID_LOG_DEBUG, "ipv4_local_prefixlen = %d", Global_Clatd_Config.ipv4_local_prefixlen); |
| 328 | logmsg(ANDROID_LOG_DEBUG, "plat_subnet = %s", |
| 329 | inet_ntop(AF_INET6, &Global_Clatd_Config.plat_subnet, charbuffer, sizeof(charbuffer))); |
| 330 | logmsg(ANDROID_LOG_DEBUG, "default_pdp_interface = %s", |
| 331 | Global_Clatd_Config.default_pdp_interface); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 332 | } |