1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.net.shared;
18 
19 import static android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET;
20 import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED;
21 import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_VPN;
22 import static android.net.NetworkCapabilities.NET_CAPABILITY_TRUSTED;
23 import static android.net.NetworkCapabilities.TRANSPORT_BLUETOOTH;
24 import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR;
25 import static android.net.NetworkCapabilities.TRANSPORT_ETHERNET;
26 import static android.net.NetworkCapabilities.TRANSPORT_WIFI;
27 
28 import android.net.NetworkCapabilities;
29 
30 /** @hide */
31 public class NetworkMonitorUtils {
32     // This class is used by both NetworkMonitor and ConnectivityService, so it cannot use
33     // NetworkStack shims, but at the same time cannot use non-system APIs.
34     // TRANSPORT_TEST is test API as of R (so it is enforced to always be 7 and can't be changed),
35     // and it is being added as a system API in S.
36     // TODO: use NetworkCapabilities.TRANSPORT_TEST once NetworkStack builds against API 31.
37     private static final int TRANSPORT_TEST = 7;
38 
39     // Network conditions broadcast constants
40     public static final String ACTION_NETWORK_CONDITIONS_MEASURED =
41             "android.net.conn.NETWORK_CONDITIONS_MEASURED";
42     public static final String EXTRA_CONNECTIVITY_TYPE = "extra_connectivity_type";
43     public static final String EXTRA_NETWORK_TYPE = "extra_network_type";
44     public static final String EXTRA_RESPONSE_RECEIVED = "extra_response_received";
45     public static final String EXTRA_IS_CAPTIVE_PORTAL = "extra_is_captive_portal";
46     public static final String EXTRA_CELL_ID = "extra_cellid";
47     public static final String EXTRA_SSID = "extra_ssid";
48     public static final String EXTRA_BSSID = "extra_bssid";
49     /** real time since boot */
50     public static final String EXTRA_REQUEST_TIMESTAMP_MS = "extra_request_timestamp_ms";
51     public static final String EXTRA_RESPONSE_TIMESTAMP_MS = "extra_response_timestamp_ms";
52     public static final String PERMISSION_ACCESS_NETWORK_CONDITIONS =
53             "android.permission.ACCESS_NETWORK_CONDITIONS";
54 
55     /**
56      * Return whether validation is required for private DNS in strict mode.
57      * @param nc Network capabilities of the network to test.
58      */
isPrivateDnsValidationRequired(NetworkCapabilities nc)59     public static boolean isPrivateDnsValidationRequired(NetworkCapabilities nc) {
60         if (nc == null) return false;
61 
62         // TODO: Consider requiring validation for DUN networks.
63         if (nc.hasCapability(NET_CAPABILITY_INTERNET)
64                 && nc.hasCapability(NET_CAPABILITY_NOT_RESTRICTED)
65                 && nc.hasCapability(NET_CAPABILITY_TRUSTED)) {
66             // Real networks
67             return true;
68         }
69 
70         // TODO: once TRANSPORT_TEST is @SystemApi in S and S SDK is stable (so constant shims can
71         // be replaced with the SDK constant that will be inlined), replace isTestNetwork with
72         // hasTransport(TRANSPORT_TEST)
73 
74         // Test networks that also have one of the major transport types are attempting to replicate
75         // that transport on a test interface (for example, test ethernet networks with
76         // EthernetManager#setIncludeTestInterfaces). Run validation on them for realistic tests.
77         // See also comments on EthernetManager#setIncludeTestInterfaces and on TestNetworkManager.
78         if (nc.hasTransport(TRANSPORT_TEST) && nc.hasCapability(NET_CAPABILITY_NOT_RESTRICTED) && (
79                 nc.hasTransport(TRANSPORT_WIFI)
80                 || nc.hasTransport(TRANSPORT_CELLULAR)
81                 || nc.hasTransport(TRANSPORT_BLUETOOTH)
82                 || nc.hasTransport(TRANSPORT_ETHERNET))) {
83             return true;
84         }
85 
86         return false;
87     }
88 
89     /**
90      * Return whether validation is required for a network.
91      * @param nc Network capabilities of the network to test.
92      */
isValidationRequired(NetworkCapabilities nc)93     public static boolean isValidationRequired(NetworkCapabilities nc) {
94         // TODO: Consider requiring validation for DUN networks.
95         return isPrivateDnsValidationRequired(nc) && nc.hasCapability(NET_CAPABILITY_NOT_VPN);
96     }
97 }
98