blob: afd63f51bf4e46df44521eda75a3551e43de8e21 [file] [log] [blame]
Bernie Innocenti318ed2d2018-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 Innocentie71a28a2019-05-29 00:42:35 +090028
29#pragma once
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090030
Bernie Innocenti758005f2019-02-19 18:08:36 +090031#include "params.h"
Luke Huangba7bef92018-12-26 16:53:03 +080032
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090033#include <netinet/in.h>
Bernie Innocentia74088b2018-09-13 16:00:42 +090034
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090035/*
Mike Yu19108d52018-11-15 21:58:19 +080036 * Passing NETID_UNSET as the netId causes system/netd/resolv/DnsProxyListener.cpp to
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090037 * fill in the appropriate default netId for the query.
38 */
39#define NETID_UNSET 0u
40
41/*
42 * MARK_UNSET represents the default (i.e. unset) value for a socket mark.
43 */
44#define MARK_UNSET 0u
45
Mike Yubfb1b342018-11-06 15:42:36 +080046/*
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090047 * A struct to capture context relevant to network operations.
48 *
49 * Application and DNS netids/marks can differ from one another under certain
50 * circumstances, notably when a VPN applies to the given uid's traffic but the
51 * VPN network does not have its own DNS servers explicitly provisioned.
52 *
53 * The introduction of per-UID routing means the uid is also an essential part
54 * of the evaluation context. Its proper uninitialized value is
55 * NET_CONTEXT_INVALID_UID.
56 */
57struct android_net_context {
58 unsigned app_netid;
59 unsigned app_mark;
60 unsigned dns_netid;
61 unsigned dns_mark;
62 uid_t uid;
63 unsigned flags;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090064};
65
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090066#define NET_CONTEXT_INVALID_UID ((uid_t) -1)
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090067#define NET_CONTEXT_FLAG_USE_LOCAL_NAMESERVERS 0x00000001
68#define NET_CONTEXT_FLAG_USE_EDNS 0x00000002
chenbruced8cbb9b2019-06-20 18:25:28 +080069#define NET_CONTEXT_FLAG_USE_DNS_OVER_TLS 0x00000004
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090070
Mike Yu19108d52018-11-15 21:58:19 +080071// TODO: investigate having the resolver check permissions itself, either by adding support to
72// libbinder_ndk or by converting IPermissionController into a stable AIDL interface.
73typedef bool (*check_calling_permission_callback)(const char* permission);
Bernie Innocenti23c6e2a2019-05-16 15:18:35 +090074typedef void (*get_network_context_callback)(unsigned netid, uid_t uid,
75 android_net_context* netcontext);
Luke Huang9f8d8b72019-03-26 15:15:44 +080076typedef void (*log_callback)(const char* msg);
Sehee Parkd975bf32019-08-07 13:21:16 +090077typedef int (*tagSocketCallback)(int sockFd, uint32_t tag, uid_t uid);
Mike Yu19108d52018-11-15 21:58:19 +080078
Bernie Innocenti23c6e2a2019-05-16 15:18:35 +090079/*
80 * Some functions needed by the resolver (e.g. checkCallingPermission()) live in
81 * libraries with no ABI stability guarantees, such as libbinder.so.
82 * As a temporary workaround, we keep these functions in netd and call them via
83 * function pointers.
84 */
Luke Huang36796f32019-03-13 02:54:45 +080085struct ResolverNetdCallbacks {
Mike Yu19108d52018-11-15 21:58:19 +080086 check_calling_permission_callback check_calling_permission;
87 get_network_context_callback get_network_context;
Luke Huang9f8d8b72019-03-26 15:15:44 +080088 log_callback log;
Sehee Parkd975bf32019-08-07 13:21:16 +090089 tagSocketCallback tagSocket;
Mike Yu19108d52018-11-15 21:58:19 +080090};
91
Sehee Parkd975bf32019-08-07 13:21:16 +090092#define TAG_SYSTEM_DNS 0xFFFFFF82
93
Luke Huang9f8d8b72019-03-26 15:15:44 +080094LIBNETD_RESOLV_PUBLIC bool resolv_has_nameservers(unsigned netid);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090095
Luke Huang36796f32019-03-13 02:54:45 +080096// Set callbacks and bring DnsResolver up.
Bernie Innocentic19a4642019-05-16 18:38:47 +090097LIBNETD_RESOLV_PUBLIC bool resolv_init(const ResolverNetdCallbacks* callbacks);