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 "params.h" 32 33 #include <netinet/in.h> 34 35 typedef union sockaddr_union { 36 struct sockaddr sa; 37 struct sockaddr_in sin; 38 struct sockaddr_in6 sin6; 39 } sockaddr_union; 40 41 /* 42 * Passing NETID_UNSET as the netId causes system/netd/resolv/DnsProxyListener.cpp to 43 * fill in the appropriate default netId for the query. 44 */ 45 #define NETID_UNSET 0u 46 47 /* 48 * MARK_UNSET represents the default (i.e. unset) value for a socket mark. 49 */ 50 #define MARK_UNSET 0u 51 52 /* 53 * Error code extending EAI_* codes defined in bionic/libc/include/netdb.h. 54 * This error code, including EAI_*, returned from android_getaddrinfofornetcontext() 55 * and android_gethostbynamefornetcontext() are used for DNS metrics. 56 */ 57 #define NETD_RESOLV_TIMEOUT 255 // consistent with RCODE_TIMEOUT 58 59 /* 60 * A struct to capture context relevant to network operations. 61 * 62 * Application and DNS netids/marks can differ from one another under certain 63 * circumstances, notably when a VPN applies to the given uid's traffic but the 64 * VPN network does not have its own DNS servers explicitly provisioned. 65 * 66 * The introduction of per-UID routing means the uid is also an essential part 67 * of the evaluation context. Its proper uninitialized value is 68 * NET_CONTEXT_INVALID_UID. 69 */ 70 struct android_net_context { 71 unsigned app_netid; 72 unsigned app_mark; 73 unsigned dns_netid; 74 unsigned dns_mark; 75 uid_t uid; 76 unsigned flags; 77 }; 78 79 #define NET_CONTEXT_INVALID_UID ((uid_t) -1) 80 #define NET_CONTEXT_FLAG_USE_LOCAL_NAMESERVERS 0x00000001 81 #define NET_CONTEXT_FLAG_USE_EDNS 0x00000002 82 83 // TODO: investigate having the resolver check permissions itself, either by adding support to 84 // libbinder_ndk or by converting IPermissionController into a stable AIDL interface. 85 typedef bool (*check_calling_permission_callback)(const char* permission); 86 typedef void (*get_network_context_callback)(unsigned netid, uid_t uid, 87 android_net_context* netcontext); 88 typedef void (*log_callback)(const char* msg); 89 90 /* 91 * Some functions needed by the resolver (e.g. checkCallingPermission()) live in 92 * libraries with no ABI stability guarantees, such as libbinder.so. 93 * As a temporary workaround, we keep these functions in netd and call them via 94 * function pointers. 95 */ 96 struct ResolverNetdCallbacks { 97 check_calling_permission_callback check_calling_permission; 98 get_network_context_callback get_network_context; 99 log_callback log; 100 }; 101 102 LIBNETD_RESOLV_PUBLIC bool resolv_has_nameservers(unsigned netid); 103 104 // Set callbacks and bring DnsResolver up. 105 LIBNETD_RESOLV_PUBLIC bool resolv_init(const ResolverNetdCallbacks* callbacks); 106