blob: f7c97241e4595b989252c3de80b175e3070da787 [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
Lorenzo Colitti98089522014-10-09 22:29:45 +090038/* function: ipv6_prefix_equal
39 * compares the prefixes two ipv6 addresses. assumes the prefix lengths are both /64.
junyulaic4e591a2018-11-26 22:36:10 +090040 * a1 - first address
41 * a2 - second address
42 * returns: 0 if the subnets are different, 1 if they are the same.
Lorenzo Colitti98089522014-10-09 22:29:45 +090043 */
junyulaic4e591a2018-11-26 22:36:10 +090044int ipv6_prefix_equal(struct in6_addr *a1, struct in6_addr *a2) { return !memcmp(a1, a2, 8); }
Lorenzo Colitti98089522014-10-09 22:29:45 +090045
Daniel Drowna45056e2012-03-23 10:42:54 -050046/* function: read_config
junyulaic4e591a2018-11-26 22:36:10 +090047 * reads the config file and parses it into the global variable Global_Clatd_Config. returns 0 on
48 * failure, 1 on success
49 * file - filename to parse
50 * uplink_interface - interface to use to reach the internet and supplier of address space
Daniel Drowna45056e2012-03-23 10:42:54 -050051 */
Lorenzo Colittic29e1782020-06-01 08:48:07 +000052int read_config(const char *file, const char *uplink_interface) {
junyulaic4e591a2018-11-26 22:36:10 +090053 cnode *root = config_node("", "");
Daniel Drowna45056e2012-03-23 10:42:54 -050054
junyulaic4e591a2018-11-26 22:36:10 +090055 if (!root) {
56 logmsg(ANDROID_LOG_FATAL, "out of memory");
Daniel Drowna45056e2012-03-23 10:42:54 -050057 return 0;
58 }
59
60 memset(&Global_Clatd_Config, '\0', sizeof(Global_Clatd_Config));
61
62 config_load_file(root, file);
junyulaic4e591a2018-11-26 22:36:10 +090063 if (root->first_child == NULL) {
64 logmsg(ANDROID_LOG_FATAL, "Could not read config file %s", file);
Daniel Drowna45056e2012-03-23 10:42:54 -050065 goto failed;
66 }
67
Lorenzo Colitti1352a3a2014-10-21 13:41:21 +090068 Global_Clatd_Config.default_pdp_interface = strdup(uplink_interface);
junyulaic4e591a2018-11-26 22:36:10 +090069 if (!Global_Clatd_Config.default_pdp_interface) goto failed;
70
Maciej Żenczykowski6be24d82020-06-02 12:56:40 +000071 Global_Clatd_Config.ipv4_local_prefixlen = 29;
Daniel Drowna45056e2012-03-23 10:42:54 -050072
Daniel Drowna45056e2012-03-23 10:42:54 -050073 return 1;
74
75failed:
76 free(root);
Daniel Drowna45056e2012-03-23 10:42:54 -050077 return 0;
78}