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 | |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 38 | /* function: config_item_ip |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 39 | * locates the config item, parses the ipv4 address, and returns the pointer ret_val_ptr, or NULL on |
| 40 | * failure |
| 41 | * root - parsed configuration |
| 42 | * item_name - name of config item to locate |
| 43 | * defaultvar - value to use if config item isn't present |
| 44 | * ret_val_ptr - pointer for return value storage |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 45 | */ |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 46 | struct in_addr *config_item_ip(cnode *root, const char *item_name, const char *defaultvar, |
| 47 | struct in_addr *ret_val_ptr) { |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 48 | const char *tmp; |
| 49 | int status; |
| 50 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 51 | if (!(tmp = config_str(root, item_name, defaultvar))) { |
| 52 | logmsg(ANDROID_LOG_FATAL, "%s config item needed", item_name); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 53 | return NULL; |
| 54 | } |
| 55 | |
| 56 | status = inet_pton(AF_INET, tmp, ret_val_ptr); |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 57 | if (status <= 0) { |
| 58 | 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] | 59 | return NULL; |
| 60 | } |
| 61 | |
| 62 | return ret_val_ptr; |
| 63 | } |
| 64 | |
Lorenzo Colitti | 9808952 | 2014-10-09 22:29:45 +0900 | [diff] [blame] | 65 | /* function: ipv6_prefix_equal |
| 66 | * compares the prefixes two ipv6 addresses. assumes the prefix lengths are both /64. |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 67 | * a1 - first address |
| 68 | * a2 - second address |
| 69 | * 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] | 70 | */ |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 71 | 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] | 72 | |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 73 | /* function: read_config |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 74 | * reads the config file and parses it into the global variable Global_Clatd_Config. returns 0 on |
| 75 | * failure, 1 on success |
| 76 | * file - filename to parse |
| 77 | * 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] | 78 | */ |
Lorenzo Colitti | 27da0ad | 2020-06-01 12:15:20 +0900 | [diff] [blame] | 79 | int read_config(const char *file, const char *uplink_interface) { |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 80 | cnode *root = config_node("", ""); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 81 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 82 | if (!root) { |
| 83 | logmsg(ANDROID_LOG_FATAL, "out of memory"); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | memset(&Global_Clatd_Config, '\0', sizeof(Global_Clatd_Config)); |
| 88 | |
| 89 | config_load_file(root, file); |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 90 | if (root->first_child == NULL) { |
| 91 | logmsg(ANDROID_LOG_FATAL, "Could not read config file %s", file); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 92 | goto failed; |
| 93 | } |
| 94 | |
Lorenzo Colitti | 1352a3a | 2014-10-21 13:41:21 +0900 | [diff] [blame] | 95 | Global_Clatd_Config.default_pdp_interface = strdup(uplink_interface); |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 96 | if (!Global_Clatd_Config.default_pdp_interface) goto failed; |
| 97 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 98 | if (!config_item_ip(root, "ipv4_local_subnet", DEFAULT_IPV4_LOCAL_SUBNET, |
| 99 | &Global_Clatd_Config.ipv4_local_subnet)) |
Lorenzo Colitti | 1352a3a | 2014-10-21 13:41:21 +0900 | [diff] [blame] | 100 | goto failed; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 101 | |
Maciej Żenczykowski | 649581f | 2020-06-02 01:26:52 -0700 | [diff] [blame] | 102 | Global_Clatd_Config.ipv4_local_prefixlen = 29; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 103 | |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 104 | return 1; |
| 105 | |
| 106 | failed: |
| 107 | free(root); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 108 | return 0; |
| 109 | } |