blob: dde600d82f8635a26d136565d8950a7d55705219 [file] [log] [blame]
Bernie Innocenti55864192018-08-30 04:05:20 +09001/*
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 Innocenti189eb502018-10-01 23:10:18 +090028#ifndef NETD_RESOLV_RESOLV_H
29#define NETD_RESOLV_RESOLV_H
Bernie Innocenti55864192018-08-30 04:05:20 +090030
Bernie Innocenti189eb502018-10-01 23:10:18 +090031/*
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 Innocenti55864192018-08-30 04:05:20 +090034 */
Luke Huang952d0942018-12-26 16:53:03 +080035#include <android/multinetwork.h> // ResNsendFlags
36
Bernie Innocenti55864192018-08-30 04:05:20 +090037#include <netinet/in.h>
Bernie Innocentia8cfe092018-09-13 16:00:42 +090038
Bernie Innocenti189eb502018-10-01 23:10:18 +090039#include "params.h"
Bernie Innocenti55864192018-08-30 04:05:20 +090040
nuccachene172a4e2018-10-23 17:10:58 +080041typedef union sockaddr_union {
42 struct sockaddr sa;
43 struct sockaddr_in sin;
44 struct sockaddr_in6 sin6;
45} sockaddr_union;
46
Bernie Innocenti55864192018-08-30 04:05:20 +090047/*
Mike Yu0ae31af2018-11-15 21:58:19 +080048 * Passing NETID_UNSET as the netId causes system/netd/resolv/DnsProxyListener.cpp to
Bernie Innocenti55864192018-08-30 04:05:20 +090049 * 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 Yu69615f62018-11-06 15:42:36 +080058/*
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 Innocenti55864192018-08-30 04:05:20 +090065struct __res_params;
66struct addrinfo;
Bernie Innocentiee1b85b2018-09-25 14:23:19 +090067struct hostent;
Bernie Innocenti55864192018-08-30 04:05:20 +090068
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 */
80struct 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 Innocenti55864192018-08-30 04:05:20 +090087};
88
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090089#define NET_CONTEXT_INVALID_UID ((uid_t) -1)
Bernie Innocenti55864192018-08-30 04:05:20 +090090
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090091#define NET_CONTEXT_FLAG_USE_LOCAL_NAMESERVERS 0x00000001
92#define NET_CONTEXT_FLAG_USE_EDNS 0x00000002
Bernie Innocenti55864192018-08-30 04:05:20 +090093
Mike Yua46fae72018-11-01 20:07:00 +080094struct ExternalPrivateDnsStatus {
95 PrivateDnsMode mode;
Bernie Innocenti45238a12018-12-04 14:57:48 +090096 int numServers;
Mike Yua46fae72018-11-01 20:07:00 +080097 struct PrivateDnsInfo {
98 sockaddr_storage ss;
99 const char* hostname;
100 Validation validation;
101 } serverStatus[MAXNS];
102};
103
Mike Yu0ae31af2018-11-15 21:58:19 +0800104/*
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 */
109typedef 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.
114typedef bool (*check_calling_permission_callback)(const char* permission);
115
116// TODO: Remove the callback.
Mike Yua46fae72018-11-01 20:07:00 +0800117typedef void (*private_dns_validated_callback)(unsigned netid, const char* server,
118 const char* hostname, bool success);
119
Mike Yu0ae31af2018-11-15 21:58:19 +0800120// TODO: Remove the callback after moving NAT64 prefix discovery out of netd to libnetd_resolv.
121typedef bool (*get_dns64_prefix_callback)(unsigned netid, in6_addr* prefix, uint8_t* prefix_len);
122
123struct 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 Chen806d4462018-12-26 17:04:43 +0800129LIBNETD_RESOLV_PUBLIC int android_gethostbyaddrfornetcontext(const void*, socklen_t, int,
130 const android_net_context*, hostent**);
Mike Yucac05e42018-11-06 19:20:07 +0800131LIBNETD_RESOLV_PUBLIC int android_gethostbynamefornetcontext(const char*, int,
132 const android_net_context*, hostent**);
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900133LIBNETD_RESOLV_PUBLIC int android_getaddrinfofornetcontext(const char*, const char*,
134 const addrinfo*,
135 const android_net_context*, addrinfo**);
Bernie Innocenti55864192018-08-30 04:05:20 +0900136
ckenb1a69a42018-12-01 17:45:18 +0900137LIBNETD_RESOLV_PUBLIC bool resolv_has_nameservers(unsigned netid);
138
Luke Huangeefb7772018-11-19 20:17:26 +0800139// Query dns with raw msg
Luke Huang952d0942018-12-26 16:53:03 +0800140LIBNETD_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 Huangeefb7772018-11-19 20:17:26 +0800143
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900144// Set name servers for a network
Bernie Innocenti189eb502018-10-01 23:10:18 +0900145LIBNETD_RESOLV_PUBLIC int resolv_set_nameservers_for_net(unsigned netid, const char** servers,
Bernie Innocenti45238a12018-12-04 14:57:48 +0900146 int numservers, const char* domains,
Bernie Innocenti189eb502018-10-01 23:10:18 +0900147 const __res_params* params);
Bernie Innocenti55864192018-08-30 04:05:20 +0900148
Mike Yua46fae72018-11-01 20:07:00 +0800149LIBNETD_RESOLV_PUBLIC int resolv_set_private_dns_for_net(unsigned netid, uint32_t mark,
Bernie Innocenti45238a12018-12-04 14:57:48 +0900150 const char** servers, int numServers,
Mike Yua46fae72018-11-01 20:07:00 +0800151 const char* tlsName,
152 const uint8_t** fingerprints,
Bernie Innocenti45238a12018-12-04 14:57:48 +0900153 int numFingerprints);
Mike Yua46fae72018-11-01 20:07:00 +0800154
155LIBNETD_RESOLV_PUBLIC void resolv_delete_private_dns_for_net(unsigned netid);
156
157LIBNETD_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 Yu0ae31af2018-11-15 21:58:19 +0800161// TODO: Remove it. Use ResolverEventReporter instead.
Mike Yua46fae72018-11-01 20:07:00 +0800162LIBNETD_RESOLV_PUBLIC void resolv_register_private_dns_callback(
163 private_dns_validated_callback callback);
164
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900165// Delete the cache associated with a certain network
Bernie Innocenti189eb502018-10-01 23:10:18 +0900166LIBNETD_RESOLV_PUBLIC void resolv_delete_cache_for_net(unsigned netid);
Bernie Innocenti55864192018-08-30 04:05:20 +0900167
Mike Yu0ae31af2018-11-15 21:58:19 +0800168// Set callbacks to DnsProxyListener, and bring it up.
169LIBNETD_RESOLV_PUBLIC bool resolv_init(const dnsproxylistener_callbacks& callbacks);
170
Bernie Innocenti189eb502018-10-01 23:10:18 +0900171#endif // NETD_RESOLV_RESOLV_H