Bernie Innocenti | 318ed2d | 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 | e71a28a | 2019-05-29 00:42:35 +0900 | [diff] [blame] | 28 | |
| 29 | #pragma once |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 30 | |
Bernie Innocenti | 758005f | 2019-02-19 18:08:36 +0900 | [diff] [blame] | 31 | #include "params.h" |
Luke Huang | ba7bef9 | 2018-12-26 16:53:03 +0800 | [diff] [blame] | 32 | |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 33 | #include <netinet/in.h> |
Bernie Innocenti | a74088b | 2018-09-13 16:00:42 +0900 | [diff] [blame] | 34 | |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 35 | /* |
Mike Yu | 19108d5 | 2018-11-15 21:58:19 +0800 | [diff] [blame] | 36 | * Passing NETID_UNSET as the netId causes system/netd/resolv/DnsProxyListener.cpp to |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 37 | * 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 Yu | bfb1b34 | 2018-11-06 15:42:36 +0800 | [diff] [blame] | 46 | /* |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 47 | * 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 | */ |
| 57 | struct 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 Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 64 | }; |
| 65 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 66 | #define NET_CONTEXT_INVALID_UID ((uid_t) -1) |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 67 | #define NET_CONTEXT_FLAG_USE_LOCAL_NAMESERVERS 0x00000001 |
| 68 | #define NET_CONTEXT_FLAG_USE_EDNS 0x00000002 |
chenbruce | d8cbb9b | 2019-06-20 18:25:28 +0800 | [diff] [blame] | 69 | #define NET_CONTEXT_FLAG_USE_DNS_OVER_TLS 0x00000004 |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 70 | |
Mike Yu | 19108d5 | 2018-11-15 21:58:19 +0800 | [diff] [blame] | 71 | // 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. |
| 73 | typedef bool (*check_calling_permission_callback)(const char* permission); |
Bernie Innocenti | 23c6e2a | 2019-05-16 15:18:35 +0900 | [diff] [blame] | 74 | typedef void (*get_network_context_callback)(unsigned netid, uid_t uid, |
| 75 | android_net_context* netcontext); |
Luke Huang | 9f8d8b7 | 2019-03-26 15:15:44 +0800 | [diff] [blame] | 76 | typedef void (*log_callback)(const char* msg); |
Sehee Park | d975bf3 | 2019-08-07 13:21:16 +0900 | [diff] [blame] | 77 | typedef int (*tagSocketCallback)(int sockFd, uint32_t tag, uid_t uid); |
Mike Yu | 19108d5 | 2018-11-15 21:58:19 +0800 | [diff] [blame] | 78 | |
Bernie Innocenti | 23c6e2a | 2019-05-16 15:18:35 +0900 | [diff] [blame] | 79 | /* |
| 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 Huang | 36796f3 | 2019-03-13 02:54:45 +0800 | [diff] [blame] | 85 | struct ResolverNetdCallbacks { |
Mike Yu | 19108d5 | 2018-11-15 21:58:19 +0800 | [diff] [blame] | 86 | check_calling_permission_callback check_calling_permission; |
| 87 | get_network_context_callback get_network_context; |
Luke Huang | 9f8d8b7 | 2019-03-26 15:15:44 +0800 | [diff] [blame] | 88 | log_callback log; |
Sehee Park | d975bf3 | 2019-08-07 13:21:16 +0900 | [diff] [blame] | 89 | tagSocketCallback tagSocket; |
Mike Yu | 19108d5 | 2018-11-15 21:58:19 +0800 | [diff] [blame] | 90 | }; |
| 91 | |
Sehee Park | d975bf3 | 2019-08-07 13:21:16 +0900 | [diff] [blame] | 92 | #define TAG_SYSTEM_DNS 0xFFFFFF82 |
| 93 | |
Luke Huang | 9f8d8b7 | 2019-03-26 15:15:44 +0800 | [diff] [blame] | 94 | LIBNETD_RESOLV_PUBLIC bool resolv_has_nameservers(unsigned netid); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 95 | |
Luke Huang | 36796f3 | 2019-03-13 02:54:45 +0800 | [diff] [blame] | 96 | // Set callbacks and bring DnsResolver up. |
Bernie Innocenti | c19a464 | 2019-05-16 18:38:47 +0900 | [diff] [blame] | 97 | LIBNETD_RESOLV_PUBLIC bool resolv_init(const ResolverNetdCallbacks* callbacks); |