diff options
author | android-build-team Robot <android-build-team-robot@google.com> | 2020-06-05 01:11:24 +0000 |
---|---|---|
committer | android-build-team Robot <android-build-team-robot@google.com> | 2020-06-05 01:11:24 +0000 |
commit | 032ef54ebbb05ba6d9b116eb5ff9a292c8778cea (patch) | |
tree | ab35db00ba507142ac2f9307bb79478815e933ff | |
parent | 3dae8c9c086fb9ed548f8b7684b919001abf2dcc (diff) | |
parent | 5c4c4ec6219b7e86dff987f4dc50ac6776982fbd (diff) | |
download | android-clat-032ef54ebbb05ba6d9b116eb5ff9a292c8778cea.tar.gz |
Snap for 6560327 from 5c4c4ec6219b7e86dff987f4dc50ac6776982fbd to rvc-d1-releaseandroid-11.0.0_r9android-11.0.0_r8android-11.0.0_r7android-11.0.0_r15android-11.0.0_r14android-11.0.0_r13android-11.0.0_r12android-11.0.0_r11android-11.0.0_r10android11-d1-s7-releaseandroid11-d1-s6-releaseandroid11-d1-s5-releaseandroid11-d1-s1-releaseandroid11-d1-release
Change-Id: I70f7a3685ec1d226c9b70856739c977ecb253c05
-rw-r--r-- | Android.bp | 1 | ||||
-rw-r--r-- | clatd.c | 13 | ||||
-rw-r--r-- | clatd.conf | 1 | ||||
-rw-r--r-- | clatd_test.cpp | 8 | ||||
-rw-r--r-- | config.c | 231 | ||||
-rw-r--r-- | config.h | 25 | ||||
-rw-r--r-- | main.c | 1 | ||||
-rw-r--r-- | ring.c | 2 | ||||
-rw-r--r-- | translate.c | 3 | ||||
-rw-r--r-- | tun.h | 46 |
10 files changed, 25 insertions, 306 deletions
@@ -20,7 +20,6 @@ cc_defaults { filegroup { name: "clatd_common", srcs: [ - "config.c", "clatd.c", "dump.c", "getaddr.c", @@ -50,7 +50,8 @@ #include "ring.h" #include "setif.h" #include "translate.h" -#include "tun.h" + +struct clat_config Global_Clatd_Config; /* 40 bytes IPv6 header - 20 bytes IPv4 header + 8 bytes fragment header */ #define MTU_DELTA 28 @@ -98,7 +99,7 @@ int configure_packet_socket(int sock) { struct sockaddr_ll sll = { .sll_family = AF_PACKET, .sll_protocol = htons(ETH_P_IPV6), - .sll_ifindex = if_nametoindex(Global_Clatd_Config.default_pdp_interface), + .sll_ifindex = if_nametoindex(Global_Clatd_Config.native_ipv6_interface), .sll_pkttype = PACKET_OTHERHOST, // The 464xlat IPv6 address is not assigned to the kernel. }; if (bind(sock, (struct sockaddr *)&sll, sizeof(sll))) { @@ -318,11 +319,7 @@ int detect_mtu(const struct in6_addr *plat_subnet, uint32_t plat_suffix, uint32_ */ void configure_interface(const char *uplink_interface, const char *plat_prefix, const char *v4_addr, const char *v6_addr, struct tun_data *tunnel, uint32_t mark) { - if (!read_config("/system/etc/clatd.conf", uplink_interface)) { - logmsg(ANDROID_LOG_FATAL, "read_config failed"); - exit(1); - } - + Global_Clatd_Config.native_ipv6_interface = uplink_interface; if (!plat_prefix || inet_pton(AF_INET6, plat_prefix, &Global_Clatd_Config.plat_subnet) <= 0) { logmsg(ANDROID_LOG_FATAL, "invalid IPv6 address specified for plat prefix: %s", plat_prefix); exit(1); @@ -430,7 +427,7 @@ void event_loop(struct tun_data *tunnel) { time_t now = time(NULL); if (last_interface_poll < (now - INTERFACE_POLL_FREQUENCY)) { - if (ipv6_address_changed(Global_Clatd_Config.default_pdp_interface)) { + if (ipv6_address_changed(Global_Clatd_Config.native_ipv6_interface)) { break; } } @@ -1 +0,0 @@ -temporary_placeholder 0 diff --git a/clatd_test.cpp b/clatd_test.cpp index 8f10e32..c16a4dd 100644 --- a/clatd_test.cpp +++ b/clatd_test.cpp @@ -34,7 +34,6 @@ extern "C" { #include "getaddr.h" #include "netutils/checksum.h" #include "translate.h" -#include "tun.h" } // For convenience. @@ -585,9 +584,7 @@ class ClatdTest : public ::testing::Test { inet_pton(AF_INET, kIPv4LocalAddr, &Global_Clatd_Config.ipv4_local_subnet); inet_pton(AF_INET6, kIPv6PlatSubnet, &Global_Clatd_Config.plat_subnet); memset(&Global_Clatd_Config.ipv6_local_subnet, 0, sizeof(in6_addr)); - Global_Clatd_Config.ipv6_host_id = in6addr_any; - Global_Clatd_Config.use_dynamic_iid = 1; - Global_Clatd_Config.default_pdp_interface = const_cast<char *>(sTun.name().c_str()); + Global_Clatd_Config.native_ipv6_interface = const_cast<char *>(sTun.name().c_str()); } // Static because setting up the tun interface takes about 40ms. @@ -633,8 +630,6 @@ TEST_F(ClatdTest, DetectMtu) { } TEST_F(ClatdTest, ConfigureTunIpManual) { - Global_Clatd_Config.ipv4_local_prefixlen = 29; - // Create an interface for configure_tun_ip to configure and bring up. TunInterface v4Iface; ASSERT_EQ(0, v4Iface.init()); @@ -854,7 +849,6 @@ void check_translate_checksum_neutral(const uint8_t *original, size_t original_l TEST_F(ClatdTest, TranslateChecksumNeutral) { // Generate a random clat IPv6 address and check that translation is checksum-neutral. - Global_Clatd_Config.ipv6_host_id = in6addr_any; ASSERT_TRUE(inet_pton(AF_INET6, "2001:db8:1:2:f076:ae99:124e:aa54", &Global_Clatd_Config.ipv6_local_subnet)); diff --git a/config.c b/config.c deleted file mode 100644 index d4cc9af..0000000 --- a/config.c +++ /dev/null @@ -1,231 +0,0 @@ -/* - * Copyright 2011 Daniel Drown - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * config.c - configuration settings - */ - -#include <arpa/inet.h> -#include <errno.h> -#include <limits.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <unistd.h> - -#include <cutils/config_utils.h> -#include <netutils/checksum.h> -#include <netutils/ifc.h> - -#include "clatd.h" -#include "config.h" -#include "getaddr.h" -#include "logging.h" - -struct clat_config Global_Clatd_Config; - -/* function: config_item_str - * locates the config item and returns the pointer to a string, or NULL on failure. Caller frees - * pointer - * root - parsed configuration - * item_name - name of config item to locate - * defaultvar - value to use if config item isn't present - */ -char *config_item_str(cnode *root, const char *item_name, const char *defaultvar) { - const char *tmp; - - if (!(tmp = config_str(root, item_name, defaultvar))) { - logmsg(ANDROID_LOG_FATAL, "%s config item needed", item_name); - return NULL; - } - return strdup(tmp); -} - -/* function: config_item_int16_t - * locates the config item, parses the integer, and returns the pointer ret_val_ptr, or NULL on - * failure - * root - parsed configuration - * item_name - name of config item to locate - * defaultvar - value to use if config item isn't present - * ret_val_ptr - pointer for return value storage - */ -int16_t *config_item_int16_t(cnode *root, const char *item_name, const char *defaultvar, - int16_t *ret_val_ptr) { - const char *tmp; - char *endptr; - long int conf_int; - - if (!(tmp = config_str(root, item_name, defaultvar))) { - logmsg(ANDROID_LOG_FATAL, "%s config item needed", item_name); - return NULL; - } - - errno = 0; - conf_int = strtol(tmp, &endptr, 10); - if (errno > 0) { - logmsg(ANDROID_LOG_FATAL, "%s config item is not numeric: %s (error=%s)", item_name, tmp, - strerror(errno)); - return NULL; - } - if (endptr == tmp || *tmp == '\0') { - logmsg(ANDROID_LOG_FATAL, "%s config item is not numeric: %s", item_name, tmp); - return NULL; - } - if (*endptr != '\0') { - logmsg(ANDROID_LOG_FATAL, "%s config item contains non-numeric characters: %s", item_name, - endptr); - return NULL; - } - if (conf_int > INT16_MAX || conf_int < INT16_MIN) { - logmsg(ANDROID_LOG_FATAL, "%s config item is too big/small: %d", item_name, conf_int); - return NULL; - } - *ret_val_ptr = conf_int; - return ret_val_ptr; -} - -/* function: config_item_ip - * locates the config item, parses the ipv4 address, and returns the pointer ret_val_ptr, or NULL on - * failure - * root - parsed configuration - * item_name - name of config item to locate - * defaultvar - value to use if config item isn't present - * ret_val_ptr - pointer for return value storage - */ -struct in_addr *config_item_ip(cnode *root, const char *item_name, const char *defaultvar, - struct in_addr *ret_val_ptr) { - const char *tmp; - int status; - - if (!(tmp = config_str(root, item_name, defaultvar))) { - logmsg(ANDROID_LOG_FATAL, "%s config item needed", item_name); - return NULL; - } - - status = inet_pton(AF_INET, tmp, ret_val_ptr); - if (status <= 0) { - logmsg(ANDROID_LOG_FATAL, "invalid IPv4 address specified for %s: %s", item_name, tmp); - return NULL; - } - - return ret_val_ptr; -} - -/* function: config_item_ip6 - * locates the config item, parses the ipv6 address, and returns the pointer ret_val_ptr, or NULL on - * failure - * root - parsed configuration - * item_name - name of config item to locate - * defaultvar - value to use if config item isn't present - * ret_val_ptr - pointer for return value storage - */ -struct in6_addr *config_item_ip6(cnode *root, const char *item_name, const char *defaultvar, - struct in6_addr *ret_val_ptr) { - const char *tmp; - int status; - - if (!(tmp = config_str(root, item_name, defaultvar))) { - logmsg(ANDROID_LOG_FATAL, "%s config item needed", item_name); - return NULL; - } - - status = inet_pton(AF_INET6, tmp, ret_val_ptr); - if (status <= 0) { - logmsg(ANDROID_LOG_FATAL, "invalid IPv6 address specified for %s: %s", item_name, tmp); - return NULL; - } - - return ret_val_ptr; -} - -/* function: ipv6_prefix_equal - * compares the prefixes two ipv6 addresses. assumes the prefix lengths are both /64. - * a1 - first address - * a2 - second address - * returns: 0 if the subnets are different, 1 if they are the same. - */ -int ipv6_prefix_equal(struct in6_addr *a1, struct in6_addr *a2) { return !memcmp(a1, a2, 8); } - -/* function: read_config - * reads the config file and parses it into the global variable Global_Clatd_Config. returns 0 on - * failure, 1 on success - * file - filename to parse - * uplink_interface - interface to use to reach the internet and supplier of address space - */ -int read_config(const char *file, const char *uplink_interface) { - cnode *root = config_node("", ""); - unsigned flags; - - if (!root) { - logmsg(ANDROID_LOG_FATAL, "out of memory"); - return 0; - } - - memset(&Global_Clatd_Config, '\0', sizeof(Global_Clatd_Config)); - - config_load_file(root, file); - if (root->first_child == NULL) { - logmsg(ANDROID_LOG_FATAL, "Could not read config file %s", file); - goto failed; - } - - Global_Clatd_Config.default_pdp_interface = strdup(uplink_interface); - if (!Global_Clatd_Config.default_pdp_interface) goto failed; - - if (!config_item_ip(root, "ipv4_local_subnet", DEFAULT_IPV4_LOCAL_SUBNET, - &Global_Clatd_Config.ipv4_local_subnet)) - goto failed; - - if (!config_item_int16_t(root, "ipv4_local_prefixlen", DEFAULT_IPV4_LOCAL_PREFIXLEN, - &Global_Clatd_Config.ipv4_local_prefixlen)) - goto failed; - - if (!config_item_ip6(root, "ipv6_host_id", "::", &Global_Clatd_Config.ipv6_host_id)) goto failed; - - /* In order to prevent multiple devices attempting to use the same clat address, never use a - statically-configured interface ID on a broadcast interface such as wifi. */ - if (!IN6_IS_ADDR_UNSPECIFIED(&Global_Clatd_Config.ipv6_host_id)) { - ifc_init(); - ifc_get_info(Global_Clatd_Config.default_pdp_interface, NULL, NULL, &flags); - ifc_close(); - Global_Clatd_Config.use_dynamic_iid = (flags & IFF_BROADCAST) != 0; - } else { - Global_Clatd_Config.use_dynamic_iid = 1; - } - - return 1; - -failed: - free(root); - return 0; -} - -/* function; dump_config - * prints the current config - */ -void dump_config() { - char charbuffer[INET6_ADDRSTRLEN]; - - logmsg( - ANDROID_LOG_DEBUG, "ipv6_local_subnet = %s", - inet_ntop(AF_INET6, &Global_Clatd_Config.ipv6_local_subnet, charbuffer, sizeof(charbuffer))); - logmsg( - ANDROID_LOG_DEBUG, "ipv4_local_subnet = %s", - inet_ntop(AF_INET, &Global_Clatd_Config.ipv4_local_subnet, charbuffer, sizeof(charbuffer))); - logmsg(ANDROID_LOG_DEBUG, "ipv4_local_prefixlen = %d", Global_Clatd_Config.ipv4_local_prefixlen); - logmsg(ANDROID_LOG_DEBUG, "plat_subnet = %s", - inet_ntop(AF_INET6, &Global_Clatd_Config.plat_subnet, charbuffer, sizeof(charbuffer))); - logmsg(ANDROID_LOG_DEBUG, "default_pdp_interface = %s", - Global_Clatd_Config.default_pdp_interface); -} @@ -21,22 +21,31 @@ #include <linux/if.h> #include <netinet/in.h> -#define DEFAULT_IPV4_LOCAL_SUBNET "192.0.0.4" -#define DEFAULT_IPV4_LOCAL_PREFIXLEN "29" +#include "ring.h" + +struct tun_data { + char device4[IFNAMSIZ]; + int read_fd6, write_fd6, fd4; + struct packet_ring ring; +}; struct clat_config { struct in6_addr ipv6_local_subnet; - struct in6_addr ipv6_host_id; struct in_addr ipv4_local_subnet; - int16_t ipv4_local_prefixlen; struct in6_addr plat_subnet; - char *default_pdp_interface; - int use_dynamic_iid; + const char *native_ipv6_interface; }; extern struct clat_config Global_Clatd_Config; -int read_config(const char *file, const char *uplink_interface); -int ipv6_prefix_equal(struct in6_addr *a1, struct in6_addr *a2); +/* function: ipv6_prefix_equal + * compares the /64 prefixes of two ipv6 addresses. + * a1 - first address + * a2 - second address + * returns: 0 if the subnets are different, 1 if they are the same. + */ +static inline int ipv6_prefix_equal(struct in6_addr *a1, struct in6_addr *a2) { + return !memcmp(a1, a2, 8); +} #endif /* __CONFIG_H__ */ @@ -32,7 +32,6 @@ #include "config.h" #include "logging.h" #include "setif.h" -#include "tun.h" #define DEVICEPREFIX "v4-" @@ -24,10 +24,10 @@ #include <sys/mman.h> #include <sys/socket.h> +#include "config.h" #include "logging.h" #include "ring.h" #include "translate.h" -#include "tun.h" int ring_create(struct tun_data *tunnel) { // Will eventually be bound to htons(ETH_P_IPV6) protocol, diff --git a/translate.c b/translate.c index 1632753..728acc3 100644 --- a/translate.c +++ b/translate.c @@ -26,7 +26,6 @@ #include "icmp.h" #include "logging.h" #include "translate.h" -#include "tun.h" /* function: packet_checksum * calculates the checksum over all the packet components starting from pos @@ -524,7 +523,7 @@ void translate_packet(int fd, int to_ipv6, const uint8_t *packet, size_t packets if (iov_len > 0) { fill_tun_header(&tun_targ, ETH_P_IP); out[CLAT_POS_TUNHDR].iov_len = sizeof(tun_targ); - send_tun(fd, out, iov_len); + writev(fd, out, iov_len); } } } @@ -1,46 +0,0 @@ -/* - * Copyright 2014 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * tun.h - tun device functions - */ -#ifndef __TUN_H__ -#define __TUN_H__ - -#include <fcntl.h> -#include <linux/if.h> -#include <sys/uio.h> -#include <unistd.h> - -#include "common.h" -#include "ring.h" - -struct tun_data { - char device4[IFNAMSIZ]; - int read_fd6, write_fd6, fd4; - struct packet_ring ring; -}; - -/* function: send_tun - * sends a clat_packet to a tun interface - * fd - the tun filedescriptor - * out - the packet to send - * iov_len - the number of entries in the clat_packet - * returns: number of bytes read on success, -1 on failure - */ -static inline int send_tun(int fd, clat_packet out, int iov_len) { - return writev(fd, out, iov_len); -} - -#endif |