Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 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 | */ |
Bernie Innocenti | 189eb50 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 28 | #ifndef NETD_RESOLV_RESOLV_H |
| 29 | #define NETD_RESOLV_RESOLV_H |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 30 | |
Bernie Innocenti | 189eb50 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 31 | /* |
| 32 | * This header contains declarations related to per-network DNS server selection. |
| 33 | * They are used by system/netd/ and should not be exposed by the public NDK headers. |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 34 | */ |
Luke Huang | 952d094 | 2018-12-26 16:53:03 +0800 | [diff] [blame] | 35 | #include <android/multinetwork.h> // ResNsendFlags |
| 36 | |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 37 | #include <netinet/in.h> |
Bernie Innocenti | a8cfe09 | 2018-09-13 16:00:42 +0900 | [diff] [blame] | 38 | |
Bernie Innocenti | 189eb50 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 39 | #include "params.h" |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 40 | |
nuccachen | e172a4e | 2018-10-23 17:10:58 +0800 | [diff] [blame] | 41 | typedef union sockaddr_union { |
| 42 | struct sockaddr sa; |
| 43 | struct sockaddr_in sin; |
| 44 | struct sockaddr_in6 sin6; |
| 45 | } sockaddr_union; |
| 46 | |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 47 | /* |
Mike Yu | 0ae31af | 2018-11-15 21:58:19 +0800 | [diff] [blame^] | 48 | * Passing NETID_UNSET as the netId causes system/netd/resolv/DnsProxyListener.cpp to |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 49 | * fill in the appropriate default netId for the query. |
| 50 | */ |
| 51 | #define NETID_UNSET 0u |
| 52 | |
| 53 | /* |
| 54 | * MARK_UNSET represents the default (i.e. unset) value for a socket mark. |
| 55 | */ |
| 56 | #define MARK_UNSET 0u |
| 57 | |
Mike Yu | 69615f6 | 2018-11-06 15:42:36 +0800 | [diff] [blame] | 58 | /* |
| 59 | * Error code extending EAI_* codes defined in bionic/libc/include/netdb.h. |
| 60 | * This error code, including EAI_*, returned from android_getaddrinfofornetcontext() |
| 61 | * and android_gethostbynamefornetcontext() are used for DNS metrics. |
| 62 | */ |
| 63 | #define NETD_RESOLV_TIMEOUT 255 // consistent with RCODE_TIMEOUT |
| 64 | |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 65 | struct __res_params; |
| 66 | struct addrinfo; |
Bernie Innocenti | ee1b85b | 2018-09-25 14:23:19 +0900 | [diff] [blame] | 67 | struct hostent; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 68 | |
| 69 | /* |
| 70 | * A struct to capture context relevant to network operations. |
| 71 | * |
| 72 | * Application and DNS netids/marks can differ from one another under certain |
| 73 | * circumstances, notably when a VPN applies to the given uid's traffic but the |
| 74 | * VPN network does not have its own DNS servers explicitly provisioned. |
| 75 | * |
| 76 | * The introduction of per-UID routing means the uid is also an essential part |
| 77 | * of the evaluation context. Its proper uninitialized value is |
| 78 | * NET_CONTEXT_INVALID_UID. |
| 79 | */ |
| 80 | struct android_net_context { |
| 81 | unsigned app_netid; |
| 82 | unsigned app_mark; |
| 83 | unsigned dns_netid; |
| 84 | unsigned dns_mark; |
| 85 | uid_t uid; |
| 86 | unsigned flags; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 87 | }; |
| 88 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 89 | #define NET_CONTEXT_INVALID_UID ((uid_t) -1) |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 90 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 91 | #define NET_CONTEXT_FLAG_USE_LOCAL_NAMESERVERS 0x00000001 |
| 92 | #define NET_CONTEXT_FLAG_USE_EDNS 0x00000002 |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 93 | |
Mike Yu | a46fae7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 94 | struct ExternalPrivateDnsStatus { |
| 95 | PrivateDnsMode mode; |
Bernie Innocenti | 45238a1 | 2018-12-04 14:57:48 +0900 | [diff] [blame] | 96 | int numServers; |
Mike Yu | a46fae7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 97 | struct PrivateDnsInfo { |
| 98 | sockaddr_storage ss; |
| 99 | const char* hostname; |
| 100 | Validation validation; |
| 101 | } serverStatus[MAXNS]; |
| 102 | }; |
| 103 | |
Mike Yu | 0ae31af | 2018-11-15 21:58:19 +0800 | [diff] [blame^] | 104 | /* |
| 105 | * Some of functions (e.g. checkCallingPermission()) require the dependency on libbinder.so, |
| 106 | * but we can't include the library since it's not stable. Move the functions to netd and use |
| 107 | * these function pointers pointing to them. |
| 108 | */ |
| 109 | typedef void (*get_network_context_callback)(unsigned netid, uid_t uid, |
| 110 | android_net_context* netcontext); |
| 111 | |
| 112 | // TODO: investigate having the resolver check permissions itself, either by adding support to |
| 113 | // libbinder_ndk or by converting IPermissionController into a stable AIDL interface. |
| 114 | typedef bool (*check_calling_permission_callback)(const char* permission); |
| 115 | |
| 116 | // TODO: Remove the callback. |
Mike Yu | a46fae7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 117 | typedef void (*private_dns_validated_callback)(unsigned netid, const char* server, |
| 118 | const char* hostname, bool success); |
| 119 | |
Mike Yu | 0ae31af | 2018-11-15 21:58:19 +0800 | [diff] [blame^] | 120 | // TODO: Remove the callback after moving NAT64 prefix discovery out of netd to libnetd_resolv. |
| 121 | typedef bool (*get_dns64_prefix_callback)(unsigned netid, in6_addr* prefix, uint8_t* prefix_len); |
| 122 | |
| 123 | struct dnsproxylistener_callbacks { |
| 124 | check_calling_permission_callback check_calling_permission; |
| 125 | get_network_context_callback get_network_context; |
| 126 | get_dns64_prefix_callback get_dns64_prefix; |
| 127 | }; |
| 128 | |
Hungming Chen | 806d446 | 2018-12-26 17:04:43 +0800 | [diff] [blame] | 129 | LIBNETD_RESOLV_PUBLIC int android_gethostbyaddrfornetcontext(const void*, socklen_t, int, |
| 130 | const android_net_context*, hostent**); |
Mike Yu | cac05e4 | 2018-11-06 19:20:07 +0800 | [diff] [blame] | 131 | LIBNETD_RESOLV_PUBLIC int android_gethostbynamefornetcontext(const char*, int, |
| 132 | const android_net_context*, hostent**); |
Bernie Innocenti | ee1b85b | 2018-09-25 14:23:19 +0900 | [diff] [blame] | 133 | LIBNETD_RESOLV_PUBLIC int android_getaddrinfofornetcontext(const char*, const char*, |
| 134 | const addrinfo*, |
| 135 | const android_net_context*, addrinfo**); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 136 | |
cken | b1a69a4 | 2018-12-01 17:45:18 +0900 | [diff] [blame] | 137 | LIBNETD_RESOLV_PUBLIC bool resolv_has_nameservers(unsigned netid); |
| 138 | |
Luke Huang | eefb777 | 2018-11-19 20:17:26 +0800 | [diff] [blame] | 139 | // Query dns with raw msg |
Luke Huang | 952d094 | 2018-12-26 16:53:03 +0800 | [diff] [blame] | 140 | LIBNETD_RESOLV_PUBLIC int resolv_res_nsend(const android_net_context* netContext, |
| 141 | const uint8_t* msg, int msgLen, uint8_t* ans, int ansLen, |
| 142 | int* rcode, uint32_t flags); |
Luke Huang | eefb777 | 2018-11-19 20:17:26 +0800 | [diff] [blame] | 143 | |
Bernie Innocenti | ee1b85b | 2018-09-25 14:23:19 +0900 | [diff] [blame] | 144 | // Set name servers for a network |
Bernie Innocenti | 189eb50 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 145 | LIBNETD_RESOLV_PUBLIC int resolv_set_nameservers_for_net(unsigned netid, const char** servers, |
Bernie Innocenti | 45238a1 | 2018-12-04 14:57:48 +0900 | [diff] [blame] | 146 | int numservers, const char* domains, |
Bernie Innocenti | 189eb50 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 147 | const __res_params* params); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 148 | |
Mike Yu | a46fae7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 149 | LIBNETD_RESOLV_PUBLIC int resolv_set_private_dns_for_net(unsigned netid, uint32_t mark, |
Bernie Innocenti | 45238a1 | 2018-12-04 14:57:48 +0900 | [diff] [blame] | 150 | const char** servers, int numServers, |
Mike Yu | a46fae7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 151 | const char* tlsName, |
| 152 | const uint8_t** fingerprints, |
Bernie Innocenti | 45238a1 | 2018-12-04 14:57:48 +0900 | [diff] [blame] | 153 | int numFingerprints); |
Mike Yu | a46fae7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 154 | |
| 155 | LIBNETD_RESOLV_PUBLIC void resolv_delete_private_dns_for_net(unsigned netid); |
| 156 | |
| 157 | LIBNETD_RESOLV_PUBLIC void resolv_get_private_dns_status_for_net(unsigned netid, |
| 158 | ExternalPrivateDnsStatus* status); |
| 159 | |
| 160 | // Register callback to listen whether private DNS validated |
Mike Yu | 0ae31af | 2018-11-15 21:58:19 +0800 | [diff] [blame^] | 161 | // TODO: Remove it. Use ResolverEventReporter instead. |
Mike Yu | a46fae7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 162 | LIBNETD_RESOLV_PUBLIC void resolv_register_private_dns_callback( |
| 163 | private_dns_validated_callback callback); |
| 164 | |
Bernie Innocenti | ee1b85b | 2018-09-25 14:23:19 +0900 | [diff] [blame] | 165 | // Delete the cache associated with a certain network |
Bernie Innocenti | 189eb50 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 166 | LIBNETD_RESOLV_PUBLIC void resolv_delete_cache_for_net(unsigned netid); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 167 | |
Mike Yu | 0ae31af | 2018-11-15 21:58:19 +0800 | [diff] [blame^] | 168 | // Set callbacks to DnsProxyListener, and bring it up. |
| 169 | LIBNETD_RESOLV_PUBLIC bool resolv_init(const dnsproxylistener_callbacks& callbacks); |
| 170 | |
Bernie Innocenti | 189eb50 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 171 | #endif // NETD_RESOLV_RESOLV_H |