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 #define NET_CONTEXT_INVALID_UID ((uid_t)-1) 48 #define NET_CONTEXT_INVALID_PID ((pid_t)-1) 49 50 /* 51 * A struct to capture context relevant to network operations. 52 * 53 * Application and DNS netids/marks can differ from one another under certain 54 * circumstances, notably when a VPN applies to the given uid's traffic but the 55 * VPN network does not have its own DNS servers explicitly provisioned. 56 * 57 * The introduction of per-UID routing means the uid is also an essential part 58 * of the evaluation context. Its proper uninitialized value is 59 * NET_CONTEXT_INVALID_UID. 60 */ 61 struct android_net_context { 62 unsigned app_netid; 63 unsigned app_mark; 64 unsigned dns_netid; 65 unsigned dns_mark; 66 uid_t uid = NET_CONTEXT_INVALID_UID; 67 unsigned flags; 68 // Variable to store the pid of the application sending DNS query. 69 pid_t pid = NET_CONTEXT_INVALID_PID; 70 toStringandroid_net_context71 std::string toString() const { 72 return fmt::format("{} {} {} {} {} {}", app_netid, app_mark, dns_netid, dns_mark, uid, 73 flags); 74 } 75 }; 76 77 #define NET_CONTEXT_FLAG_USE_LOCAL_NAMESERVERS 0x00000001 78 #define NET_CONTEXT_FLAG_USE_EDNS 0x00000002 79 #define NET_CONTEXT_FLAG_USE_DNS_OVER_TLS 0x00000004 80 81 // TODO: investigate having the resolver check permissions itself, either by adding support to 82 // libbinder_ndk or by converting IPermissionController into a stable AIDL interface. 83 typedef bool (*check_calling_permission_callback)(const char* permission); 84 typedef void (*get_network_context_callback)(unsigned netid, uid_t uid, 85 android_net_context* netcontext); 86 typedef void (*log_callback)(const char* msg); 87 typedef int (*tagSocketCallback)(int sockFd, uint32_t tag, uid_t uid, pid_t pid); 88 89 // The DnsResolver module invokes this callback once before starting each DNS 90 // lookup. The callback receives the android_net_context associated with the 91 // request, and the (possibly unqualified) hostname requested by the app via 92 // getaddrinfo() or gethostbyname(). 93 // 94 // If the callback returns false, the DnsResolver will abort the request 95 // returning EAI_SYSTEM. If the callback returns true, the query will proceed as 96 // usual. 97 // 98 // If this callback is not present (i.e. set to nullptr), the effect is the same 99 // of returning true. 100 // 101 // This callback *will* be invoked concurrently from multiple threads. It must 102 // peform its own locking when accessing shared data structures. Furthermore, 103 // the callback must not sleep nor perform RPC requests. 104 // 105 // Be mindful that hostnames could contain sensitive user data. Do not log them 106 // and do not transmit them to third parties without explicit user 107 // authorization. 108 // 109 typedef bool (*evaluate_domain_name_callback)( 110 const android_net_context &netcontext, const char *host); 111 112 /* 113 * Some functions needed by the resolver (e.g. checkCallingPermission()) live in 114 * libraries with no ABI stability guarantees, such as libbinder.so. 115 * As a temporary workaround, we keep these functions in netd and call them via 116 * function pointers. 117 */ 118 struct ResolverNetdCallbacks { 119 check_calling_permission_callback check_calling_permission; 120 get_network_context_callback get_network_context; 121 log_callback log; 122 tagSocketCallback tagSocket; 123 evaluate_domain_name_callback evaluate_domain_name; 124 }; 125 126 #define TAG_SYSTEM_DNS 0xFFFFFF82 127 128 #define LIBNETD_RESOLV_PUBLIC extern "C" [[gnu::visibility("default")]] 129 130 LIBNETD_RESOLV_PUBLIC bool resolv_has_nameservers(unsigned netid); 131 132 // Set callbacks and bring DnsResolver up. 133 LIBNETD_RESOLV_PUBLIC bool resolv_init(const ResolverNetdCallbacks* callbacks); 134 135 // Function that performs RDNS in local cache. The |domain_name_size| is the size of domain_name 136 // buffer, which is recommended to NS_MAXDNAME. Function return false if hostname not found or 137 // domain_name_size > NS_MAXDNAME. 138 LIBNETD_RESOLV_PUBLIC bool resolv_gethostbyaddr_from_cache(unsigned netId, char domain_name[], 139 size_t domain_name_size, 140 const char* ip_address, int af); 141