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 | */ |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 35 | #include <netinet/in.h> |
Bernie Innocenti | a8cfe09 | 2018-09-13 16:00:42 +0900 | [diff] [blame] | 36 | |
Bernie Innocenti | 189eb50 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 37 | #include "params.h" |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 38 | |
nuccachen | e172a4e | 2018-10-23 17:10:58 +0800 | [diff] [blame] | 39 | typedef union sockaddr_union { |
| 40 | struct sockaddr sa; |
| 41 | struct sockaddr_in sin; |
| 42 | struct sockaddr_in6 sin6; |
| 43 | } sockaddr_union; |
| 44 | |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 45 | /* |
| 46 | * Passing NETID_UNSET as the netId causes system/netd/server/DnsProxyListener.cpp to |
| 47 | * fill in the appropriate default netId for the query. |
| 48 | */ |
| 49 | #define NETID_UNSET 0u |
| 50 | |
| 51 | /* |
| 52 | * MARK_UNSET represents the default (i.e. unset) value for a socket mark. |
| 53 | */ |
| 54 | #define MARK_UNSET 0u |
| 55 | |
Mike Yu | 69615f6 | 2018-11-06 15:42:36 +0800 | [diff] [blame] | 56 | /* |
| 57 | * Error code extending EAI_* codes defined in bionic/libc/include/netdb.h. |
| 58 | * This error code, including EAI_*, returned from android_getaddrinfofornetcontext() |
| 59 | * and android_gethostbynamefornetcontext() are used for DNS metrics. |
| 60 | */ |
| 61 | #define NETD_RESOLV_TIMEOUT 255 // consistent with RCODE_TIMEOUT |
| 62 | |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 63 | struct __res_params; |
| 64 | struct addrinfo; |
Bernie Innocenti | ee1b85b | 2018-09-25 14:23:19 +0900 | [diff] [blame] | 65 | struct hostent; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 66 | |
| 67 | /* |
| 68 | * A struct to capture context relevant to network operations. |
| 69 | * |
| 70 | * Application and DNS netids/marks can differ from one another under certain |
| 71 | * circumstances, notably when a VPN applies to the given uid's traffic but the |
| 72 | * VPN network does not have its own DNS servers explicitly provisioned. |
| 73 | * |
| 74 | * The introduction of per-UID routing means the uid is also an essential part |
| 75 | * of the evaluation context. Its proper uninitialized value is |
| 76 | * NET_CONTEXT_INVALID_UID. |
| 77 | */ |
| 78 | struct android_net_context { |
| 79 | unsigned app_netid; |
| 80 | unsigned app_mark; |
| 81 | unsigned dns_netid; |
| 82 | unsigned dns_mark; |
| 83 | uid_t uid; |
| 84 | unsigned flags; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 85 | }; |
| 86 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 87 | #define NET_CONTEXT_INVALID_UID ((uid_t) -1) |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 88 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 89 | #define NET_CONTEXT_FLAG_USE_LOCAL_NAMESERVERS 0x00000001 |
| 90 | #define NET_CONTEXT_FLAG_USE_EDNS 0x00000002 |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 91 | |
Mike Yu | a46fae7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 92 | struct ExternalPrivateDnsStatus { |
| 93 | PrivateDnsMode mode; |
Bernie Innocenti | 45238a1 | 2018-12-04 14:57:48 +0900 | [diff] [blame] | 94 | int numServers; |
Mike Yu | a46fae7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 95 | struct PrivateDnsInfo { |
| 96 | sockaddr_storage ss; |
| 97 | const char* hostname; |
| 98 | Validation validation; |
| 99 | } serverStatus[MAXNS]; |
| 100 | }; |
| 101 | |
| 102 | typedef void (*private_dns_validated_callback)(unsigned netid, const char* server, |
| 103 | const char* hostname, bool success); |
| 104 | |
Hungming Chen | 806d446 | 2018-12-26 17:04:43 +0800 | [diff] [blame] | 105 | LIBNETD_RESOLV_PUBLIC int android_gethostbyaddrfornetcontext(const void*, socklen_t, int, |
| 106 | const android_net_context*, hostent**); |
Mike Yu | cac05e4 | 2018-11-06 19:20:07 +0800 | [diff] [blame] | 107 | LIBNETD_RESOLV_PUBLIC int android_gethostbynamefornetcontext(const char*, int, |
| 108 | const android_net_context*, hostent**); |
Bernie Innocenti | ee1b85b | 2018-09-25 14:23:19 +0900 | [diff] [blame] | 109 | LIBNETD_RESOLV_PUBLIC int android_getaddrinfofornetcontext(const char*, const char*, |
| 110 | const addrinfo*, |
| 111 | const android_net_context*, addrinfo**); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 112 | |
cken | b1a69a4 | 2018-12-01 17:45:18 +0900 | [diff] [blame] | 113 | LIBNETD_RESOLV_PUBLIC bool resolv_has_nameservers(unsigned netid); |
| 114 | |
Luke Huang | eefb777 | 2018-11-19 20:17:26 +0800 | [diff] [blame] | 115 | // Query dns with raw msg |
| 116 | // TODO: Add a way to control query parameter, like flags, or maybe res_options or even res_state. |
| 117 | LIBNETD_RESOLV_PUBLIC int resolv_res_nsend(const android_net_context* netContext, const u_char* msg, |
| 118 | int msgLen, u_char* ans, int ansLen, int* rcode); |
| 119 | |
Bernie Innocenti | ee1b85b | 2018-09-25 14:23:19 +0900 | [diff] [blame] | 120 | // Set name servers for a network |
Bernie Innocenti | 189eb50 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 121 | 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] | 122 | int numservers, const char* domains, |
Bernie Innocenti | 189eb50 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 123 | const __res_params* params); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 124 | |
Mike Yu | a46fae7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 125 | 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] | 126 | const char** servers, int numServers, |
Mike Yu | a46fae7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 127 | const char* tlsName, |
| 128 | const uint8_t** fingerprints, |
Bernie Innocenti | 45238a1 | 2018-12-04 14:57:48 +0900 | [diff] [blame] | 129 | int numFingerprints); |
Mike Yu | a46fae7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 130 | |
| 131 | LIBNETD_RESOLV_PUBLIC void resolv_delete_private_dns_for_net(unsigned netid); |
| 132 | |
| 133 | LIBNETD_RESOLV_PUBLIC void resolv_get_private_dns_status_for_net(unsigned netid, |
| 134 | ExternalPrivateDnsStatus* status); |
| 135 | |
| 136 | // Register callback to listen whether private DNS validated |
| 137 | LIBNETD_RESOLV_PUBLIC void resolv_register_private_dns_callback( |
| 138 | private_dns_validated_callback callback); |
| 139 | |
Bernie Innocenti | ee1b85b | 2018-09-25 14:23:19 +0900 | [diff] [blame] | 140 | // Delete the cache associated with a certain network |
Bernie Innocenti | 189eb50 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 141 | LIBNETD_RESOLV_PUBLIC void resolv_delete_cache_for_net(unsigned netid); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 142 | |
Bernie Innocenti | 189eb50 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 143 | #endif // NETD_RESOLV_RESOLV_H |