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 | 34de3ba | 2019-02-19 18:08:36 +0900 | [diff] [blame] | 31 | #include "params.h" |
Luke Huang | 952d094 | 2018-12-26 16:53:03 +0800 | [diff] [blame] | 32 | |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 33 | #include <netinet/in.h> |
Bernie Innocenti | a8cfe09 | 2018-09-13 16:00:42 +0900 | [diff] [blame] | 34 | |
Bernie Innocenti | 34de3ba | 2019-02-19 18:08:36 +0900 | [diff] [blame] | 35 | struct addrinfo; |
| 36 | struct hostent; |
| 37 | struct res_params; |
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 | /* |
Mike Yu | 0ae31af | 2018-11-15 21:58:19 +0800 | [diff] [blame] | 46 | * 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] | 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 | /* |
| 64 | * A struct to capture context relevant to network operations. |
| 65 | * |
| 66 | * Application and DNS netids/marks can differ from one another under certain |
| 67 | * circumstances, notably when a VPN applies to the given uid's traffic but the |
| 68 | * VPN network does not have its own DNS servers explicitly provisioned. |
| 69 | * |
| 70 | * The introduction of per-UID routing means the uid is also an essential part |
| 71 | * of the evaluation context. Its proper uninitialized value is |
| 72 | * NET_CONTEXT_INVALID_UID. |
| 73 | */ |
| 74 | struct android_net_context { |
| 75 | unsigned app_netid; |
| 76 | unsigned app_mark; |
| 77 | unsigned dns_netid; |
| 78 | unsigned dns_mark; |
| 79 | uid_t uid; |
| 80 | unsigned flags; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 81 | }; |
| 82 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 83 | #define NET_CONTEXT_INVALID_UID ((uid_t) -1) |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 84 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 85 | #define NET_CONTEXT_FLAG_USE_LOCAL_NAMESERVERS 0x00000001 |
| 86 | #define NET_CONTEXT_FLAG_USE_EDNS 0x00000002 |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 87 | |
Mike Yu | a46fae7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 88 | struct ExternalPrivateDnsStatus { |
| 89 | PrivateDnsMode mode; |
Bernie Innocenti | 45238a1 | 2018-12-04 14:57:48 +0900 | [diff] [blame] | 90 | int numServers; |
Mike Yu | a46fae7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 91 | struct PrivateDnsInfo { |
| 92 | sockaddr_storage ss; |
| 93 | const char* hostname; |
| 94 | Validation validation; |
| 95 | } serverStatus[MAXNS]; |
| 96 | }; |
| 97 | |
Mike Yu | 0ae31af | 2018-11-15 21:58:19 +0800 | [diff] [blame] | 98 | /* |
| 99 | * Some of functions (e.g. checkCallingPermission()) require the dependency on libbinder.so, |
| 100 | * but we can't include the library since it's not stable. Move the functions to netd and use |
| 101 | * these function pointers pointing to them. |
| 102 | */ |
| 103 | typedef void (*get_network_context_callback)(unsigned netid, uid_t uid, |
| 104 | android_net_context* netcontext); |
| 105 | |
| 106 | // TODO: investigate having the resolver check permissions itself, either by adding support to |
| 107 | // libbinder_ndk or by converting IPermissionController into a stable AIDL interface. |
| 108 | typedef bool (*check_calling_permission_callback)(const char* permission); |
| 109 | |
Mike Yu | 0ae31af | 2018-11-15 21:58:19 +0800 | [diff] [blame] | 110 | // TODO: Remove the callback after moving NAT64 prefix discovery out of netd to libnetd_resolv. |
Luke Huang | f29fe68 | 2019-03-26 15:15:44 +0800 | [diff] [blame^] | 111 | typedef void (*log_callback)(const char* msg); |
Mike Yu | 0ae31af | 2018-11-15 21:58:19 +0800 | [diff] [blame] | 112 | |
Luke Huang | 71eac7c | 2019-03-13 02:54:45 +0800 | [diff] [blame] | 113 | struct ResolverNetdCallbacks { |
Mike Yu | 0ae31af | 2018-11-15 21:58:19 +0800 | [diff] [blame] | 114 | check_calling_permission_callback check_calling_permission; |
| 115 | get_network_context_callback get_network_context; |
Luke Huang | f29fe68 | 2019-03-26 15:15:44 +0800 | [diff] [blame^] | 116 | log_callback log; |
Mike Yu | 0ae31af | 2018-11-15 21:58:19 +0800 | [diff] [blame] | 117 | }; |
| 118 | |
Luke Huang | f29fe68 | 2019-03-26 15:15:44 +0800 | [diff] [blame^] | 119 | int android_gethostbyaddrfornetcontext(const void*, socklen_t, int, const android_net_context*, |
| 120 | hostent**); |
| 121 | int android_gethostbynamefornetcontext(const char*, int, const android_net_context*, hostent**); |
| 122 | int android_getaddrinfofornetcontext(const char*, const char*, const addrinfo*, |
| 123 | const android_net_context*, addrinfo**); |
Luke Huang | eefb777 | 2018-11-19 20:17:26 +0800 | [diff] [blame] | 124 | // Query dns with raw msg |
Luke Huang | f29fe68 | 2019-03-26 15:15:44 +0800 | [diff] [blame^] | 125 | int resolv_res_nsend(const android_net_context* netContext, const uint8_t* msg, int msgLen, |
| 126 | uint8_t* ans, int ansLen, int* rcode, uint32_t flags); |
Luke Huang | eefb777 | 2018-11-19 20:17:26 +0800 | [diff] [blame] | 127 | |
Bernie Innocenti | ee1b85b | 2018-09-25 14:23:19 +0900 | [diff] [blame] | 128 | // Set name servers for a network |
Luke Huang | f29fe68 | 2019-03-26 15:15:44 +0800 | [diff] [blame^] | 129 | int resolv_set_nameservers_for_net(unsigned netid, const char** servers, int numservers, |
| 130 | const char* domains, const res_params* params); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 131 | |
Luke Huang | f29fe68 | 2019-03-26 15:15:44 +0800 | [diff] [blame^] | 132 | int resolv_set_private_dns_for_net(unsigned netid, uint32_t mark, const char** servers, |
| 133 | int numServers, const char* tlsName, |
| 134 | const uint8_t** fingerprints, int numFingerprints); |
Mike Yu | a46fae7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 135 | |
Luke Huang | f29fe68 | 2019-03-26 15:15:44 +0800 | [diff] [blame^] | 136 | void resolv_delete_private_dns_for_net(unsigned netid); |
Mike Yu | a46fae7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 137 | |
Luke Huang | f29fe68 | 2019-03-26 15:15:44 +0800 | [diff] [blame^] | 138 | void resolv_get_private_dns_status_for_net(unsigned netid, ExternalPrivateDnsStatus* status); |
Mike Yu | a46fae7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 139 | |
Bernie Innocenti | ee1b85b | 2018-09-25 14:23:19 +0900 | [diff] [blame] | 140 | // Delete the cache associated with a certain network |
Luke Huang | f29fe68 | 2019-03-26 15:15:44 +0800 | [diff] [blame^] | 141 | void resolv_delete_cache_for_net(unsigned netid); |
| 142 | |
| 143 | LIBNETD_RESOLV_PUBLIC bool resolv_has_nameservers(unsigned netid); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 144 | |
Luke Huang | 71eac7c | 2019-03-13 02:54:45 +0800 | [diff] [blame] | 145 | // Set callbacks and bring DnsResolver up. |
| 146 | LIBNETD_RESOLV_PUBLIC bool resolv_init(const ResolverNetdCallbacks& callbacks); |
Mike Yu | 0ae31af | 2018-11-15 21:58:19 +0800 | [diff] [blame] | 147 | |
Bernie Innocenti | 189eb50 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 148 | #endif // NETD_RESOLV_RESOLV_H |