blob: bcf77bad7c5f585a332fb34ffdee97adeeb99ab3 [file] [log] [blame]
Daniel Drowna45056e2012-03-23 10:42:54 -05001/*
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 Drowna45056e2012-03-23 10:42:54 -050019#include <arpa/inet.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050020#include <errno.h>
junyulaic4e591a2018-11-26 22:36:10 +090021#include <limits.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050025#include <unistd.h>
26
27#include <cutils/config_utils.h>
Lorenzo Colitti98de5952019-01-20 11:45:03 +090028#include <netutils/checksum.h>
Lorenzo Colitti2596f422014-11-10 17:00:02 -080029#include <netutils/ifc.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050030
junyulaic4e591a2018-11-26 22:36:10 +090031#include "clatd.h"
Daniel Drowna45056e2012-03-23 10:42:54 -050032#include "config.h"
Daniel Drowna45056e2012-03-23 10:42:54 -050033#include "getaddr.h"
junyulaic4e591a2018-11-26 22:36:10 +090034#include "logging.h"
Daniel Drowna45056e2012-03-23 10:42:54 -050035
36struct clat_config Global_Clatd_Config;
37
Daniel Drowna45056e2012-03-23 10:42:54 -050038/* function: config_item_ip
junyulaic4e591a2018-11-26 22:36:10 +090039 * 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 Drowna45056e2012-03-23 10:42:54 -050045 */
junyulaic4e591a2018-11-26 22:36:10 +090046struct in_addr *config_item_ip(cnode *root, const char *item_name, const char *defaultvar,
47 struct in_addr *ret_val_ptr) {
Daniel Drowna45056e2012-03-23 10:42:54 -050048 const char *tmp;
49 int status;
50
junyulaic4e591a2018-11-26 22:36:10 +090051 if (!(tmp = config_str(root, item_name, defaultvar))) {
52 logmsg(ANDROID_LOG_FATAL, "%s config item needed", item_name);
Daniel Drowna45056e2012-03-23 10:42:54 -050053 return NULL;
54 }
55
56 status = inet_pton(AF_INET, tmp, ret_val_ptr);
junyulaic4e591a2018-11-26 22:36:10 +090057 if (status <= 0) {
58 logmsg(ANDROID_LOG_FATAL, "invalid IPv4 address specified for %s: %s", item_name, tmp);
Daniel Drowna45056e2012-03-23 10:42:54 -050059 return NULL;
60 }
61
62 return ret_val_ptr;
63}
64
65/* function: config_item_ip6
junyulaic4e591a2018-11-26 22:36:10 +090066 * locates the config item, parses the ipv6 address, and returns the pointer ret_val_ptr, or NULL on
67 * failure
68 * root - parsed configuration
69 * item_name - name of config item to locate
70 * defaultvar - value to use if config item isn't present
71 * ret_val_ptr - pointer for return value storage
Daniel Drowna45056e2012-03-23 10:42:54 -050072 */
junyulaic4e591a2018-11-26 22:36:10 +090073struct in6_addr *config_item_ip6(cnode *root, const char *item_name, const char *defaultvar,
74 struct in6_addr *ret_val_ptr) {
Daniel Drowna45056e2012-03-23 10:42:54 -050075 const char *tmp;
76 int status;
77
junyulaic4e591a2018-11-26 22:36:10 +090078 if (!(tmp = config_str(root, item_name, defaultvar))) {
79 logmsg(ANDROID_LOG_FATAL, "%s config item needed", item_name);
Daniel Drowna45056e2012-03-23 10:42:54 -050080 return NULL;
81 }
82
83 status = inet_pton(AF_INET6, tmp, ret_val_ptr);
junyulaic4e591a2018-11-26 22:36:10 +090084 if (status <= 0) {
85 logmsg(ANDROID_LOG_FATAL, "invalid IPv6 address specified for %s: %s", item_name, tmp);
Daniel Drowna45056e2012-03-23 10:42:54 -050086 return NULL;
87 }
88
89 return ret_val_ptr;
90}
91
Lorenzo Colitti98089522014-10-09 22:29:45 +090092/* function: ipv6_prefix_equal
93 * compares the prefixes two ipv6 addresses. assumes the prefix lengths are both /64.
junyulaic4e591a2018-11-26 22:36:10 +090094 * a1 - first address
95 * a2 - second address
96 * returns: 0 if the subnets are different, 1 if they are the same.
Lorenzo Colitti98089522014-10-09 22:29:45 +090097 */
junyulaic4e591a2018-11-26 22:36:10 +090098int ipv6_prefix_equal(struct in6_addr *a1, struct in6_addr *a2) { return !memcmp(a1, a2, 8); }
Lorenzo Colitti98089522014-10-09 22:29:45 +090099
Daniel Drowna45056e2012-03-23 10:42:54 -0500100/* function: read_config
junyulaic4e591a2018-11-26 22:36:10 +0900101 * reads the config file and parses it into the global variable Global_Clatd_Config. returns 0 on
102 * failure, 1 on success
103 * file - filename to parse
104 * uplink_interface - interface to use to reach the internet and supplier of address space
Daniel Drowna45056e2012-03-23 10:42:54 -0500105 */
Lorenzo Colitti27da0ad2020-06-01 12:15:20 +0900106int read_config(const char *file, const char *uplink_interface) {
junyulaic4e591a2018-11-26 22:36:10 +0900107 cnode *root = config_node("", "");
Daniel Drowna45056e2012-03-23 10:42:54 -0500108
junyulaic4e591a2018-11-26 22:36:10 +0900109 if (!root) {
110 logmsg(ANDROID_LOG_FATAL, "out of memory");
Daniel Drowna45056e2012-03-23 10:42:54 -0500111 return 0;
112 }
113
114 memset(&Global_Clatd_Config, '\0', sizeof(Global_Clatd_Config));
115
116 config_load_file(root, file);
junyulaic4e591a2018-11-26 22:36:10 +0900117 if (root->first_child == NULL) {
118 logmsg(ANDROID_LOG_FATAL, "Could not read config file %s", file);
Daniel Drowna45056e2012-03-23 10:42:54 -0500119 goto failed;
120 }
121
Lorenzo Colitti1352a3a2014-10-21 13:41:21 +0900122 Global_Clatd_Config.default_pdp_interface = strdup(uplink_interface);
junyulaic4e591a2018-11-26 22:36:10 +0900123 if (!Global_Clatd_Config.default_pdp_interface) goto failed;
124
junyulaic4e591a2018-11-26 22:36:10 +0900125 if (!config_item_ip(root, "ipv4_local_subnet", DEFAULT_IPV4_LOCAL_SUBNET,
126 &Global_Clatd_Config.ipv4_local_subnet))
Lorenzo Colitti1352a3a2014-10-21 13:41:21 +0900127 goto failed;
Daniel Drowna45056e2012-03-23 10:42:54 -0500128
Maciej Żenczykowski649581f2020-06-02 01:26:52 -0700129 Global_Clatd_Config.ipv4_local_prefixlen = 29;
Daniel Drowna45056e2012-03-23 10:42:54 -0500130
Daniel Drowna45056e2012-03-23 10:42:54 -0500131 return 1;
132
133failed:
134 free(root);
Daniel Drowna45056e2012-03-23 10:42:54 -0500135 return 0;
136}