1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in 12 * the documentation and/or other materials provided with the 13 * distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #pragma once 30 31 #include <android-base/format.h> 32 #include <android-base/strings.h> 33 #include <arpa/nameser.h> 34 #include <netinet/in.h> 35 36 /* 37 * Passing NETID_UNSET as the netId causes system/netd/resolv/DnsProxyListener.cpp to 38 * fill in the appropriate default netId for the query. 39 */ 40 #define NETID_UNSET 0u 41 42 /* 43 * MARK_UNSET represents the default (i.e. unset) value for a socket mark. 44 */ 45 #define MARK_UNSET 0u 46 47 /* 48 * Passing APP_SOCKET_NONE as the app_socket in getaddrinfo, gethostbyname, 49 * gethostbyaddr, res_nsend means that the query is not tied to a listening socket 50 * in a querying process, and it will not be cancellable based on a socket state. 51 */ 52 constexpr std::optional<int> APP_SOCKET_NONE = std::nullopt; 53 54 #define NET_CONTEXT_INVALID_UID ((uid_t)-1) 55 #define NET_CONTEXT_INVALID_PID ((pid_t)-1) 56 57 /* 58 * A struct to capture context relevant to network operations. 59 * 60 * Application and DNS netids/marks can differ from one another under certain 61 * circumstances, notably when a VPN applies to the given uid's traffic but the 62 * VPN network does not have its own DNS servers explicitly provisioned. 63 * 64 * The introduction of per-UID routing means the uid is also an essential part 65 * of the evaluation context. Its proper uninitialized value is 66 * NET_CONTEXT_INVALID_UID. 67 */ 68 struct android_net_context { 69 unsigned app_netid; 70 unsigned app_mark; 71 unsigned dns_netid; 72 unsigned dns_mark; 73 uid_t uid = NET_CONTEXT_INVALID_UID; 74 unsigned flags; 75 // Variable to store the pid of the application sending DNS query. 76 pid_t pid = NET_CONTEXT_INVALID_PID; 77 toStringandroid_net_context78 std::string toString() const { 79 return fmt::format("{} {} {} {} {} {}", app_netid, app_mark, dns_netid, dns_mark, uid, 80 flags); 81 } 82 }; 83 84 #define NET_CONTEXT_FLAG_USE_LOCAL_NAMESERVERS 0x00000001 85 #define NET_CONTEXT_FLAG_USE_EDNS 0x00000002 86 #define NET_CONTEXT_FLAG_USE_DNS_OVER_TLS 0x00000004 87 88 // TODO: investigate having the resolver check permissions itself, either by adding support to 89 // libbinder_ndk or by converting IPermissionController into a stable AIDL interface. 90 typedef bool (*check_calling_permission_callback)(const char* permission); 91 typedef void (*get_network_context_callback)(unsigned netid, uid_t uid, 92 android_net_context* netcontext); 93 typedef void (*log_callback)(const char* msg); 94 typedef int (*tagSocketCallback)(int sockFd, uint32_t tag, uid_t uid, pid_t pid); 95 96 // The DnsResolver module invokes this callback once before starting each DNS 97 // lookup. The callback receives the android_net_context associated with the 98 // request, and the (possibly unqualified) hostname requested by the app via 99 // getaddrinfo() or gethostbyname(). 100 // 101 // If the callback returns false, the DnsResolver will abort the request 102 // returning EAI_SYSTEM. If the callback returns true, the query will proceed as 103 // usual. 104 // 105 // If this callback is not present (i.e. set to nullptr), the effect is the same 106 // of returning true. 107 // 108 // This callback *will* be invoked concurrently from multiple threads. It must 109 // peform its own locking when accessing shared data structures. Furthermore, 110 // the callback must not sleep nor perform RPC requests. 111 // 112 // Be mindful that hostnames could contain sensitive user data. Do not log them 113 // and do not transmit them to third parties without explicit user 114 // authorization. 115 // 116 typedef bool (*evaluate_domain_name_callback)( 117 const android_net_context &netcontext, const char *host); 118 119 /* 120 * Some functions needed by the resolver (e.g. checkCallingPermission()) live in 121 * libraries with no ABI stability guarantees, such as libbinder.so. 122 * As a temporary workaround, we keep these functions in netd and call them via 123 * function pointers. 124 */ 125 struct ResolverNetdCallbacks { 126 check_calling_permission_callback check_calling_permission; 127 get_network_context_callback get_network_context; 128 log_callback log; 129 tagSocketCallback tagSocket; 130 evaluate_domain_name_callback evaluate_domain_name; 131 }; 132 133 #define TAG_SYSTEM_DNS 0xFFFFFF82 134 135 #define LIBNETD_RESOLV_PUBLIC extern "C" [[gnu::visibility("default")]] 136 137 LIBNETD_RESOLV_PUBLIC bool resolv_has_nameservers(unsigned netid); 138 139 // Set callbacks and bring DnsResolver up. 140 LIBNETD_RESOLV_PUBLIC bool resolv_init(const ResolverNetdCallbacks* callbacks); 141 142 // Function that performs RDNS in local cache. The |domain_name_size| is the size of domain_name 143 // buffer, which is recommended to NS_MAXDNAME. Function return false if hostname not found or 144 // domain_name_size > NS_MAXDNAME. 145 LIBNETD_RESOLV_PUBLIC bool resolv_gethostbyaddr_from_cache(unsigned netId, char domain_name[], 146 size_t domain_name_size, 147 const char* ip_address, int af); 148