1 /*
2  * Copyright (C) 2014 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 com.android.server.connectivity;
18 
19 import static android.net.CaptivePortal.APP_RETURN_DISMISSED;
20 import static android.net.CaptivePortal.APP_RETURN_UNWANTED;
21 import static android.net.CaptivePortal.APP_RETURN_WANTED_AS_IS;
22 import static android.net.ConnectivityManager.EXTRA_CAPTIVE_PORTAL_PROBE_SPEC;
23 import static android.net.ConnectivityManager.EXTRA_CAPTIVE_PORTAL_URL;
24 import static android.net.ConnectivityManager.TYPE_MOBILE;
25 import static android.net.ConnectivityManager.TYPE_WIFI;
26 import static android.net.DnsResolver.FLAG_EMPTY;
27 import static android.net.INetworkMonitor.NETWORK_TEST_RESULT_INVALID;
28 import static android.net.INetworkMonitor.NETWORK_TEST_RESULT_PARTIAL_CONNECTIVITY;
29 import static android.net.INetworkMonitor.NETWORK_TEST_RESULT_VALID;
30 import static android.net.INetworkMonitor.NETWORK_VALIDATION_PROBE_DNS;
31 import static android.net.INetworkMonitor.NETWORK_VALIDATION_PROBE_FALLBACK;
32 import static android.net.INetworkMonitor.NETWORK_VALIDATION_PROBE_HTTP;
33 import static android.net.INetworkMonitor.NETWORK_VALIDATION_PROBE_HTTPS;
34 import static android.net.INetworkMonitor.NETWORK_VALIDATION_PROBE_PRIVDNS;
35 import static android.net.INetworkMonitor.NETWORK_VALIDATION_RESULT_PARTIAL;
36 import static android.net.INetworkMonitor.NETWORK_VALIDATION_RESULT_VALID;
37 import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_METERED;
38 import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR;
39 import static android.net.NetworkCapabilities.TRANSPORT_WIFI;
40 import static android.net.captiveportal.CaptivePortalProbeSpec.parseCaptivePortalProbeSpecs;
41 import static android.net.metrics.ValidationProbeEvent.DNS_FAILURE;
42 import static android.net.metrics.ValidationProbeEvent.DNS_SUCCESS;
43 import static android.net.metrics.ValidationProbeEvent.PROBE_FALLBACK;
44 import static android.net.metrics.ValidationProbeEvent.PROBE_PRIVDNS;
45 import static android.net.util.DataStallUtils.CONFIG_DATA_STALL_CONSECUTIVE_DNS_TIMEOUT_THRESHOLD;
46 import static android.net.util.DataStallUtils.CONFIG_DATA_STALL_EVALUATION_TYPE;
47 import static android.net.util.DataStallUtils.CONFIG_DATA_STALL_MIN_EVALUATE_INTERVAL;
48 import static android.net.util.DataStallUtils.CONFIG_DATA_STALL_TCP_POLLING_INTERVAL;
49 import static android.net.util.DataStallUtils.CONFIG_DATA_STALL_VALID_DNS_TIME_THRESHOLD;
50 import static android.net.util.DataStallUtils.DATA_STALL_EVALUATION_TYPE_DNS;
51 import static android.net.util.DataStallUtils.DATA_STALL_EVALUATION_TYPE_NONE;
52 import static android.net.util.DataStallUtils.DATA_STALL_EVALUATION_TYPE_TCP;
53 import static android.net.util.DataStallUtils.DEFAULT_CONSECUTIVE_DNS_TIMEOUT_THRESHOLD;
54 import static android.net.util.DataStallUtils.DEFAULT_DATA_STALL_EVALUATION_TYPES;
55 import static android.net.util.DataStallUtils.DEFAULT_DATA_STALL_MIN_EVALUATE_TIME_MS;
56 import static android.net.util.DataStallUtils.DEFAULT_DATA_STALL_VALID_DNS_TIME_THRESHOLD_MS;
57 import static android.net.util.DataStallUtils.DEFAULT_DNS_LOG_SIZE;
58 import static android.net.util.DataStallUtils.DEFAULT_TCP_POLLING_INTERVAL_MS;
59 import static android.net.util.NetworkStackUtils.CAPTIVE_PORTAL_FALLBACK_PROBE_SPECS;
60 import static android.net.util.NetworkStackUtils.CAPTIVE_PORTAL_FALLBACK_URL;
61 import static android.net.util.NetworkStackUtils.CAPTIVE_PORTAL_HTTPS_URL;
62 import static android.net.util.NetworkStackUtils.CAPTIVE_PORTAL_HTTP_URL;
63 import static android.net.util.NetworkStackUtils.CAPTIVE_PORTAL_MODE;
64 import static android.net.util.NetworkStackUtils.CAPTIVE_PORTAL_MODE_IGNORE;
65 import static android.net.util.NetworkStackUtils.CAPTIVE_PORTAL_MODE_PROMPT;
66 import static android.net.util.NetworkStackUtils.CAPTIVE_PORTAL_OTHER_FALLBACK_URLS;
67 import static android.net.util.NetworkStackUtils.CAPTIVE_PORTAL_OTHER_HTTPS_URLS;
68 import static android.net.util.NetworkStackUtils.CAPTIVE_PORTAL_OTHER_HTTP_URLS;
69 import static android.net.util.NetworkStackUtils.CAPTIVE_PORTAL_USER_AGENT;
70 import static android.net.util.NetworkStackUtils.CAPTIVE_PORTAL_USE_HTTPS;
71 import static android.net.util.NetworkStackUtils.DEFAULT_CAPTIVE_PORTAL_DNS_PROBE_TIMEOUT;
72 import static android.net.util.NetworkStackUtils.DEFAULT_CAPTIVE_PORTAL_FALLBACK_PROBE_SPECS;
73 import static android.net.util.NetworkStackUtils.DEFAULT_CAPTIVE_PORTAL_HTTPS_URLS;
74 import static android.net.util.NetworkStackUtils.DEFAULT_CAPTIVE_PORTAL_HTTP_URLS;
75 import static android.net.util.NetworkStackUtils.DISMISS_PORTAL_IN_VALIDATED_NETWORK;
76 import static android.net.util.NetworkStackUtils.DNS_PROBE_PRIVATE_IP_NO_INTERNET_VERSION;
77 import static android.net.util.NetworkStackUtils.TEST_CAPTIVE_PORTAL_HTTPS_URL;
78 import static android.net.util.NetworkStackUtils.TEST_CAPTIVE_PORTAL_HTTP_URL;
79 import static android.net.util.NetworkStackUtils.TEST_URL_EXPIRATION_TIME;
80 import static android.net.util.NetworkStackUtils.isEmpty;
81 import static android.net.util.NetworkStackUtils.isIPv6ULA;
82 import static android.provider.DeviceConfig.NAMESPACE_CONNECTIVITY;
83 
84 import static com.android.networkstack.apishim.ConstantsShim.DETECTION_METHOD_DNS_EVENTS;
85 import static com.android.networkstack.apishim.ConstantsShim.DETECTION_METHOD_TCP_METRICS;
86 import static com.android.networkstack.apishim.ConstantsShim.TRANSPORT_TEST;
87 import static com.android.networkstack.util.DnsUtils.PRIVATE_DNS_PROBE_HOST_SUFFIX;
88 import static com.android.networkstack.util.DnsUtils.TYPE_ADDRCONFIG;
89 
90 import android.app.PendingIntent;
91 import android.content.BroadcastReceiver;
92 import android.content.Context;
93 import android.content.Intent;
94 import android.content.IntentFilter;
95 import android.content.pm.PackageManager;
96 import android.content.res.Configuration;
97 import android.content.res.Resources;
98 import android.net.ConnectivityManager;
99 import android.net.DataStallReportParcelable;
100 import android.net.DnsResolver;
101 import android.net.INetworkMonitorCallbacks;
102 import android.net.LinkProperties;
103 import android.net.Network;
104 import android.net.NetworkCapabilities;
105 import android.net.NetworkTestResultParcelable;
106 import android.net.ProxyInfo;
107 import android.net.TrafficStats;
108 import android.net.Uri;
109 import android.net.captiveportal.CapportApiProbeResult;
110 import android.net.captiveportal.CaptivePortalProbeResult;
111 import android.net.captiveportal.CaptivePortalProbeSpec;
112 import android.net.metrics.IpConnectivityLog;
113 import android.net.metrics.NetworkEvent;
114 import android.net.metrics.ValidationProbeEvent;
115 import android.net.shared.NetworkMonitorUtils;
116 import android.net.shared.PrivateDnsConfig;
117 import android.net.util.DataStallUtils.EvaluationType;
118 import android.net.util.NetworkStackUtils;
119 import android.net.util.SharedLog;
120 import android.net.util.Stopwatch;
121 import android.net.wifi.WifiInfo;
122 import android.net.wifi.WifiManager;
123 import android.os.Build;
124 import android.os.Bundle;
125 import android.os.Message;
126 import android.os.Process;
127 import android.os.RemoteException;
128 import android.os.SystemClock;
129 import android.os.UserHandle;
130 import android.provider.DeviceConfig;
131 import android.provider.Settings;
132 import android.stats.connectivity.ProbeResult;
133 import android.stats.connectivity.ProbeType;
134 import android.telephony.AccessNetworkConstants;
135 import android.telephony.CellIdentityNr;
136 import android.telephony.CellInfo;
137 import android.telephony.CellInfoGsm;
138 import android.telephony.CellInfoLte;
139 import android.telephony.CellInfoNr;
140 import android.telephony.CellInfoTdscdma;
141 import android.telephony.CellInfoWcdma;
142 import android.telephony.CellSignalStrength;
143 import android.telephony.NetworkRegistrationInfo;
144 import android.telephony.ServiceState;
145 import android.telephony.SignalStrength;
146 import android.telephony.TelephonyManager;
147 import android.text.TextUtils;
148 import android.util.Log;
149 import android.util.Pair;
150 import android.util.SparseArray;
151 
152 import androidx.annotation.ArrayRes;
153 import androidx.annotation.BoolRes;
154 import androidx.annotation.IntegerRes;
155 import androidx.annotation.NonNull;
156 import androidx.annotation.Nullable;
157 import androidx.annotation.StringRes;
158 import androidx.annotation.VisibleForTesting;
159 
160 import com.android.internal.annotations.GuardedBy;
161 import com.android.internal.util.RingBufferIndices;
162 import com.android.internal.util.State;
163 import com.android.internal.util.StateMachine;
164 import com.android.internal.util.TrafficStatsConstants;
165 import com.android.networkstack.NetworkStackNotifier;
166 import com.android.networkstack.R;
167 import com.android.networkstack.apishim.CaptivePortalDataShimImpl;
168 import com.android.networkstack.apishim.NetworkInformationShimImpl;
169 import com.android.networkstack.apishim.common.CaptivePortalDataShim;
170 import com.android.networkstack.apishim.common.ShimUtils;
171 import com.android.networkstack.apishim.common.UnsupportedApiLevelException;
172 import com.android.networkstack.metrics.DataStallDetectionStats;
173 import com.android.networkstack.metrics.DataStallStatsUtils;
174 import com.android.networkstack.metrics.NetworkValidationMetrics;
175 import com.android.networkstack.netlink.TcpSocketTracker;
176 import com.android.networkstack.util.DnsUtils;
177 import com.android.server.NetworkStackService.NetworkStackServiceManager;
178 
179 import org.json.JSONException;
180 import org.json.JSONObject;
181 
182 import java.io.BufferedInputStream;
183 import java.io.IOException;
184 import java.io.InputStream;
185 import java.io.InputStreamReader;
186 import java.io.InterruptedIOException;
187 import java.net.HttpURLConnection;
188 import java.net.InetAddress;
189 import java.net.MalformedURLException;
190 import java.net.URL;
191 import java.net.UnknownHostException;
192 import java.nio.charset.Charset;
193 import java.nio.charset.StandardCharsets;
194 import java.util.ArrayList;
195 import java.util.Arrays;
196 import java.util.Collections;
197 import java.util.HashMap;
198 import java.util.LinkedHashMap;
199 import java.util.List;
200 import java.util.Map;
201 import java.util.Objects;
202 import java.util.Random;
203 import java.util.StringJoiner;
204 import java.util.UUID;
205 import java.util.concurrent.CompletionService;
206 import java.util.concurrent.CountDownLatch;
207 import java.util.concurrent.ExecutionException;
208 import java.util.concurrent.ExecutorCompletionService;
209 import java.util.concurrent.ExecutorService;
210 import java.util.concurrent.Executors;
211 import java.util.concurrent.Future;
212 import java.util.concurrent.TimeUnit;
213 import java.util.concurrent.atomic.AtomicInteger;
214 import java.util.function.Function;
215 import java.util.regex.Matcher;
216 import java.util.regex.Pattern;
217 import java.util.regex.PatternSyntaxException;
218 
219 /**
220  * {@hide}
221  */
222 public class NetworkMonitor extends StateMachine {
223     private static final String TAG = NetworkMonitor.class.getSimpleName();
224     private static final boolean DBG  = true;
225     private static final boolean VDBG = false;
226     private static final boolean VDBG_STALL = Log.isLoggable(TAG, Log.DEBUG);
227     private static final String DEFAULT_USER_AGENT    = "Mozilla/5.0 (X11; Linux x86_64) "
228                                                       + "AppleWebKit/537.36 (KHTML, like Gecko) "
229                                                       + "Chrome/60.0.3112.32 Safari/537.36";
230 
231     @VisibleForTesting
232     static final String CONFIG_CAPTIVE_PORTAL_DNS_PROBE_TIMEOUT =
233             "captive_portal_dns_probe_timeout";
234 
235     private static final int SOCKET_TIMEOUT_MS = 10000;
236     private static final int PROBE_TIMEOUT_MS  = 3000;
237     private static final long TEST_URL_EXPIRATION_MS = TimeUnit.MINUTES.toMillis(10);
238 
239     private static final int UNSET_MCC_OR_MNC = -1;
240 
241     private static final int CAPPORT_API_MAX_JSON_LENGTH = 4096;
242     private static final String ACCEPT_HEADER = "Accept";
243     private static final String CONTENT_TYPE_HEADER = "Content-Type";
244     private static final String CAPPORT_API_CONTENT_TYPE = "application/captive+json";
245 
246     enum EvaluationResult {
247         VALIDATED(true),
248         CAPTIVE_PORTAL(false);
249         final boolean mIsValidated;
EvaluationResult(boolean isValidated)250         EvaluationResult(boolean isValidated) {
251             this.mIsValidated = isValidated;
252         }
253     }
254 
255     enum ValidationStage {
256         FIRST_VALIDATION(true),
257         REVALIDATION(false);
258         final boolean mIsFirstValidation;
ValidationStage(boolean isFirstValidation)259         ValidationStage(boolean isFirstValidation) {
260             this.mIsFirstValidation = isFirstValidation;
261         }
262     }
263 
264     @VisibleForTesting
265     protected static final class MccMncOverrideInfo {
266         public final int mcc;
267         public final int mnc;
MccMncOverrideInfo(int mcc, int mnc)268         MccMncOverrideInfo(int mcc, int mnc) {
269             this.mcc = mcc;
270             this.mnc = mnc;
271         }
272     }
273 
274     @VisibleForTesting
275     protected static final SparseArray<MccMncOverrideInfo> sCarrierIdToMccMnc = new SparseArray<>();
276 
277     static {
278         // CTC
279         sCarrierIdToMccMnc.put(1854, new MccMncOverrideInfo(460, 03));
280     }
281 
282     /**
283      * ConnectivityService has sent a notification to indicate that network has connected.
284      * Initiates Network Validation.
285      */
286     private static final int CMD_NETWORK_CONNECTED = 1;
287 
288     /**
289      * Message to self indicating it's time to evaluate a network's connectivity.
290      * arg1 = Token to ignore old messages.
291      */
292     private static final int CMD_REEVALUATE = 6;
293 
294     /**
295      * ConnectivityService has sent a notification to indicate that network has disconnected.
296      */
297     private static final int CMD_NETWORK_DISCONNECTED = 7;
298 
299     /**
300      * Force evaluation even if it has succeeded in the past.
301      * arg1 = UID responsible for requesting this reeval.  Will be billed for data.
302      */
303     private static final int CMD_FORCE_REEVALUATION = 8;
304 
305     /**
306      * Message to self indicating captive portal app finished.
307      * arg1 = one of: APP_RETURN_DISMISSED,
308      *                APP_RETURN_UNWANTED,
309      *                APP_RETURN_WANTED_AS_IS
310      * obj = mCaptivePortalLoggedInResponseToken as String
311      */
312     private static final int CMD_CAPTIVE_PORTAL_APP_FINISHED = 9;
313 
314     /**
315      * Message indicating sign-in app should be launched.
316      * Sent by mLaunchCaptivePortalAppBroadcastReceiver when the
317      * user touches the sign in notification, or sent by
318      * ConnectivityService when the user touches the "sign into
319      * network" button in the wifi access point detail page.
320      */
321     private static final int CMD_LAUNCH_CAPTIVE_PORTAL_APP = 11;
322 
323     /**
324      * Retest network to see if captive portal is still in place.
325      * arg1 = UID responsible for requesting this reeval.  Will be billed for data.
326      *        0 indicates self-initiated, so nobody to blame.
327      */
328     private static final int CMD_CAPTIVE_PORTAL_RECHECK = 12;
329 
330     /**
331      * ConnectivityService notifies NetworkMonitor of settings changes to
332      * Private DNS. If a DNS resolution is required, e.g. for DNS-over-TLS in
333      * strict mode, then an event is sent back to ConnectivityService with the
334      * result of the resolution attempt.
335      *
336      * A separate message is used to trigger (re)evaluation of the Private DNS
337      * configuration, so that the message can be handled as needed in different
338      * states, including being ignored until after an ongoing captive portal
339      * validation phase is completed.
340      */
341     private static final int CMD_PRIVATE_DNS_SETTINGS_CHANGED = 13;
342     private static final int CMD_EVALUATE_PRIVATE_DNS = 15;
343 
344     /**
345      * Message to self indicating captive portal detection is completed.
346      * obj = CaptivePortalProbeResult for detection result;
347      */
348     private static final int CMD_PROBE_COMPLETE = 16;
349 
350     /**
351      * ConnectivityService notifies NetworkMonitor of DNS query responses event.
352      * arg1 = returncode in OnDnsEvent which indicates the response code for the DNS query.
353      */
354     private static final int EVENT_DNS_NOTIFICATION = 17;
355 
356     /**
357      * ConnectivityService notifies NetworkMonitor that the user accepts partial connectivity and
358      * NetworkMonitor should ignore the https probe.
359      */
360     private static final int EVENT_ACCEPT_PARTIAL_CONNECTIVITY = 18;
361 
362     /**
363      * ConnectivityService notifies NetworkMonitor of changed LinkProperties.
364      * obj = new LinkProperties.
365      */
366     private static final int EVENT_LINK_PROPERTIES_CHANGED = 19;
367 
368     /**
369      * ConnectivityService notifies NetworkMonitor of changed NetworkCapabilities.
370      * obj = new NetworkCapabilities.
371      */
372     private static final int EVENT_NETWORK_CAPABILITIES_CHANGED = 20;
373 
374     /**
375      * Message to self to poll current tcp status from kernel.
376      */
377     private static final int EVENT_POLL_TCPINFO = 21;
378 
379     /**
380      * Message to self to do the bandwidth check in EvaluatingBandwidthState.
381      */
382     private static final int CMD_EVALUATE_BANDWIDTH = 22;
383 
384     /**
385      * Message to self to know the bandwidth check is completed.
386      */
387     private static final int CMD_BANDWIDTH_CHECK_COMPLETE = 23;
388 
389     /**
390      * Message to self to know the bandwidth check is timeouted.
391      */
392     private static final int CMD_BANDWIDTH_CHECK_TIMEOUT = 24;
393 
394     // Start mReevaluateDelayMs at this value and double.
395     @VisibleForTesting
396     static final int INITIAL_REEVALUATE_DELAY_MS = 1000;
397     private static final int MAX_REEVALUATE_DELAY_MS = 10 * 60 * 1000;
398     // Default timeout of evaluating network bandwidth.
399     private static final int DEFAULT_EVALUATING_BANDWIDTH_TIMEOUT_MS = 10_000;
400     // Before network has been evaluated this many times, ignore repeated reevaluate requests.
401     private static final int IGNORE_REEVALUATE_ATTEMPTS = 5;
402     private int mReevaluateToken = 0;
403     private static final int NO_UID = 0;
404     private static final int INVALID_UID = -1;
405     private int mUidResponsibleForReeval = INVALID_UID;
406     // Stop blaming UID that requested re-evaluation after this many attempts.
407     private static final int BLAME_FOR_EVALUATION_ATTEMPTS = 5;
408     // Delay between reevaluations once a captive portal has been found.
409     private static final int CAPTIVE_PORTAL_REEVALUATE_DELAY_MS = 10 * 60 * 1000;
410     private static final int NETWORK_VALIDATION_RESULT_INVALID = 0;
411     // Max thread pool size for parallel probing. Fixed thread pool size to control the thread
412     // number used for either HTTP or HTTPS probing.
413     @VisibleForTesting
414     static final int MAX_PROBE_THREAD_POOL_SIZE = 5;
415     private String mPrivateDnsProviderHostname = "";
416 
417     private final Context mContext;
418     private final INetworkMonitorCallbacks mCallback;
419     private final int mCallbackVersion;
420     private final Network mCleartextDnsNetwork;
421     private final Network mNetwork;
422     private final TelephonyManager mTelephonyManager;
423     private final WifiManager mWifiManager;
424     private final ConnectivityManager mCm;
425     @Nullable
426     private final NetworkStackNotifier mNotifier;
427     private final IpConnectivityLog mMetricsLog;
428     private final Dependencies mDependencies;
429     private final TcpSocketTracker mTcpTracker;
430     // Configuration values for captive portal detection probes.
431     private final String mCaptivePortalUserAgent;
432     private final URL[] mCaptivePortalFallbackUrls;
433     @NonNull
434     private final URL[] mCaptivePortalHttpUrls;
435     @NonNull
436     private final URL[] mCaptivePortalHttpsUrls;
437     @Nullable
438     private final CaptivePortalProbeSpec[] mCaptivePortalFallbackSpecs;
439     // Configuration values for network bandwidth check.
440     @Nullable
441     private final String mEvaluatingBandwidthUrl;
442     private final int mMaxRetryTimerMs;
443     private final int mEvaluatingBandwidthTimeoutMs;
444     private final AtomicInteger mNextEvaluatingBandwidthThreadId = new AtomicInteger(1);
445 
446     private NetworkCapabilities mNetworkCapabilities;
447     private LinkProperties mLinkProperties;
448 
449     @VisibleForTesting
450     protected boolean mIsCaptivePortalCheckEnabled;
451 
452     private boolean mUseHttps;
453     /**
454      * The total number of completed validation attempts (network validated or a captive portal was
455      * detected) for this NetworkMonitor instance.
456      * This does not include attempts that were interrupted, retried or finished with a result that
457      * is not success or portal. See {@code mValidationIndex} in {@link NetworkValidationMetrics}
458      * for a count of all attempts.
459      * TODO: remove when removing legacy metrics.
460      */
461     private int mValidations = 0;
462 
463     // Set if the user explicitly selected "Do not use this network" in captive portal sign-in app.
464     private boolean mUserDoesNotWant = false;
465     // Avoids surfacing "Sign in to network" notification.
466     private boolean mDontDisplaySigninNotification = false;
467     // Set to true once the evaluating network bandwidth is passed or the captive portal respond
468     // APP_RETURN_WANTED_AS_IS which means the user wants to use this network anyway.
469     @VisibleForTesting
470     protected boolean mIsBandwidthCheckPassedOrIgnored = false;
471 
472     private final State mDefaultState = new DefaultState();
473     private final State mValidatedState = new ValidatedState();
474     private final State mMaybeNotifyState = new MaybeNotifyState();
475     private final State mEvaluatingState = new EvaluatingState();
476     private final State mCaptivePortalState = new CaptivePortalState();
477     private final State mEvaluatingPrivateDnsState = new EvaluatingPrivateDnsState();
478     private final State mProbingState = new ProbingState();
479     private final State mWaitingForNextProbeState = new WaitingForNextProbeState();
480     private final State mEvaluatingBandwidthState = new EvaluatingBandwidthState();
481 
482     private CustomIntentReceiver mLaunchCaptivePortalAppBroadcastReceiver = null;
483 
484     private final SharedLog mValidationLogs;
485 
486     private final Stopwatch mEvaluationTimer = new Stopwatch();
487 
488     // This variable is set before transitioning to the mCaptivePortalState.
489     private CaptivePortalProbeResult mLastPortalProbeResult =
490             CaptivePortalProbeResult.failed(CaptivePortalProbeResult.PROBE_UNKNOWN);
491 
492     // Random generator to select fallback URL index
493     private final Random mRandom;
494     private int mNextFallbackUrlIndex = 0;
495 
496 
497     private int mReevaluateDelayMs = INITIAL_REEVALUATE_DELAY_MS;
498     private int mEvaluateAttempts = 0;
499     private volatile int mProbeToken = 0;
500     private final int mConsecutiveDnsTimeoutThreshold;
501     private final int mDataStallMinEvaluateTime;
502     private final int mDataStallValidDnsTimeThreshold;
503     private final int mDataStallEvaluationType;
504     @Nullable
505     private final DnsStallDetector mDnsStallDetector;
506     private long mLastProbeTime;
507     // The signal causing a data stall to be suspected. Reset to 0 after metrics are sent to statsd.
508     private @EvaluationType int mDataStallTypeToCollect;
509     private boolean mAcceptPartialConnectivity = false;
510     private final EvaluationState mEvaluationState = new EvaluationState();
511 
512     private final boolean mPrivateIpNoInternetEnabled;
513 
514     private final boolean mMetricsEnabled;
515 
516     // The validation metrics are accessed by individual probe threads, and by the StateMachine
517     // thread. All accesses must be synchronized to make sure the StateMachine thread can see
518     // reports from all probes.
519     // TODO: as that most usage is in the StateMachine thread and probes only add their probe
520     // events, consider having probes return their stats to the StateMachine, and only access this
521     // member on the StateMachine thread without synchronization.
522     @GuardedBy("mNetworkValidationMetrics")
523     private final NetworkValidationMetrics mNetworkValidationMetrics =
524             new NetworkValidationMetrics();
525 
getCallbackVersion(INetworkMonitorCallbacks cb)526     private int getCallbackVersion(INetworkMonitorCallbacks cb) {
527         int version;
528         try {
529             version = cb.getInterfaceVersion();
530         } catch (RemoteException e) {
531             version = 0;
532         }
533         // The AIDL was freezed from Q beta 5 but it's unfreezing from R before releasing. In order
534         // to distinguish the behavior between R and Q beta 5 and before Q beta 5, add SDK and
535         // CODENAME check here. Basically, it's only expected to return 0 for Q beta 4 and below
536         // because the test result has changed.
537         if (Build.VERSION.SDK_INT == Build.VERSION_CODES.Q
538                 && Build.VERSION.CODENAME.equals("REL")
539                 && version == Build.VERSION_CODES.CUR_DEVELOPMENT) version = 0;
540         return version;
541     }
542 
NetworkMonitor(Context context, INetworkMonitorCallbacks cb, Network network, SharedLog validationLog, @NonNull NetworkStackServiceManager serviceManager)543     public NetworkMonitor(Context context, INetworkMonitorCallbacks cb, Network network,
544             SharedLog validationLog, @NonNull NetworkStackServiceManager serviceManager) {
545         this(context, cb, network, new IpConnectivityLog(), validationLog, serviceManager,
546                 Dependencies.DEFAULT, getTcpSocketTrackerOrNull(context, network));
547     }
548 
549     @VisibleForTesting
NetworkMonitor(Context context, INetworkMonitorCallbacks cb, Network network, IpConnectivityLog logger, SharedLog validationLogs, @NonNull NetworkStackServiceManager serviceManager, Dependencies deps, @Nullable TcpSocketTracker tst)550     public NetworkMonitor(Context context, INetworkMonitorCallbacks cb, Network network,
551             IpConnectivityLog logger, SharedLog validationLogs,
552             @NonNull NetworkStackServiceManager serviceManager, Dependencies deps,
553             @Nullable TcpSocketTracker tst) {
554         // Add suffix indicating which NetworkMonitor we're talking about.
555         super(TAG + "/" + network.toString());
556 
557         // Logs with a tag of the form given just above, e.g.
558         //     <timestamp>   862  2402 D NetworkMonitor/NetworkAgentInfo [WIFI () - 100]: ...
559         setDbg(VDBG);
560 
561         mContext = context;
562         mMetricsLog = logger;
563         mValidationLogs = validationLogs;
564         mCallback = cb;
565         mCallbackVersion = getCallbackVersion(cb);
566         mDependencies = deps;
567         mNetwork = network;
568         mCleartextDnsNetwork = deps.getPrivateDnsBypassNetwork(network);
569         mTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
570         mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
571         mCm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
572         mNotifier = serviceManager.getNotifier();
573 
574         // CHECKSTYLE:OFF IndentationCheck
575         addState(mDefaultState);
576         addState(mMaybeNotifyState, mDefaultState);
577             addState(mEvaluatingState, mMaybeNotifyState);
578                 addState(mProbingState, mEvaluatingState);
579                 addState(mWaitingForNextProbeState, mEvaluatingState);
580             addState(mCaptivePortalState, mMaybeNotifyState);
581         addState(mEvaluatingPrivateDnsState, mDefaultState);
582         addState(mEvaluatingBandwidthState, mDefaultState);
583         addState(mValidatedState, mDefaultState);
584         setInitialState(mDefaultState);
585         // CHECKSTYLE:ON IndentationCheck
586 
587         mIsCaptivePortalCheckEnabled = getIsCaptivePortalCheckEnabled();
588         mPrivateIpNoInternetEnabled = getIsPrivateIpNoInternetEnabled();
589         mMetricsEnabled = deps.isFeatureEnabled(context, NAMESPACE_CONNECTIVITY,
590                 NetworkStackUtils.VALIDATION_METRICS_VERSION, true /* defaultEnabled */);
591         mUseHttps = getUseHttpsValidation();
592         mCaptivePortalUserAgent = getCaptivePortalUserAgent();
593         mCaptivePortalHttpsUrls = makeCaptivePortalHttpsUrls();
594         mCaptivePortalHttpUrls = makeCaptivePortalHttpUrls();
595         mCaptivePortalFallbackUrls = makeCaptivePortalFallbackUrls();
596         mCaptivePortalFallbackSpecs = makeCaptivePortalFallbackProbeSpecs();
597         mRandom = deps.getRandom();
598         // TODO: Evaluate to move data stall configuration to a specific class.
599         mConsecutiveDnsTimeoutThreshold = getConsecutiveDnsTimeoutThreshold();
600         mDataStallMinEvaluateTime = getDataStallMinEvaluateTime();
601         mDataStallValidDnsTimeThreshold = getDataStallValidDnsTimeThreshold();
602         mDataStallEvaluationType = getDataStallEvaluationType();
603         mDnsStallDetector = initDnsStallDetectorIfRequired(mDataStallEvaluationType,
604                 mConsecutiveDnsTimeoutThreshold);
605         mTcpTracker = tst;
606         // Read the configurations of evaluating network bandwidth.
607         mEvaluatingBandwidthUrl = getResStringConfig(mContext,
608                 R.string.config_evaluating_bandwidth_url, null);
609         mMaxRetryTimerMs = getResIntConfig(mContext,
610                 R.integer.config_evaluating_bandwidth_max_retry_timer_ms,
611                 MAX_REEVALUATE_DELAY_MS);
612         mEvaluatingBandwidthTimeoutMs = getResIntConfig(mContext,
613                 R.integer.config_evaluating_bandwidth_timeout_ms,
614                 DEFAULT_EVALUATING_BANDWIDTH_TIMEOUT_MS);
615 
616         // Provide empty LinkProperties and NetworkCapabilities to make sure they are never null,
617         // even before notifyNetworkConnected.
618         mLinkProperties = new LinkProperties();
619         mNetworkCapabilities = new NetworkCapabilities(null);
620     }
621 
622     /**
623      * ConnectivityService notifies NetworkMonitor that the user already accepted partial
624      * connectivity previously, so NetworkMonitor can validate the network even if it has partial
625      * connectivity.
626      */
setAcceptPartialConnectivity()627     public void setAcceptPartialConnectivity() {
628         sendMessage(EVENT_ACCEPT_PARTIAL_CONNECTIVITY);
629     }
630 
631     /**
632      * Request the NetworkMonitor to reevaluate the network.
633      */
forceReevaluation(int responsibleUid)634     public void forceReevaluation(int responsibleUid) {
635         sendMessage(CMD_FORCE_REEVALUATION, responsibleUid, 0);
636     }
637 
638     /**
639      * Send a notification to NetworkMonitor indicating that there was a DNS query response event.
640      * @param returnCode the DNS return code of the response.
641      */
notifyDnsResponse(int returnCode)642     public void notifyDnsResponse(int returnCode) {
643         sendMessage(EVENT_DNS_NOTIFICATION, returnCode);
644     }
645 
646     /**
647      * Send a notification to NetworkMonitor indicating that private DNS settings have changed.
648      * @param newCfg The new private DNS configuration.
649      */
notifyPrivateDnsSettingsChanged(PrivateDnsConfig newCfg)650     public void notifyPrivateDnsSettingsChanged(PrivateDnsConfig newCfg) {
651         // Cancel any outstanding resolutions.
652         removeMessages(CMD_PRIVATE_DNS_SETTINGS_CHANGED);
653         // Send the update to the proper thread.
654         sendMessage(CMD_PRIVATE_DNS_SETTINGS_CHANGED, newCfg);
655     }
656 
657     /**
658      * Send a notification to NetworkMonitor indicating that the network is now connected.
659      */
notifyNetworkConnected(LinkProperties lp, NetworkCapabilities nc)660     public void notifyNetworkConnected(LinkProperties lp, NetworkCapabilities nc) {
661         sendMessage(CMD_NETWORK_CONNECTED, new Pair<>(
662                 new LinkProperties(lp), new NetworkCapabilities(nc)));
663     }
664 
updateConnectedNetworkAttributes(Message connectedMsg)665     private void updateConnectedNetworkAttributes(Message connectedMsg) {
666         final Pair<LinkProperties, NetworkCapabilities> attrs =
667                 (Pair<LinkProperties, NetworkCapabilities>) connectedMsg.obj;
668         mLinkProperties = attrs.first;
669         mNetworkCapabilities = attrs.second;
670     }
671 
672     /**
673      * Send a notification to NetworkMonitor indicating that the network is now disconnected.
674      */
notifyNetworkDisconnected()675     public void notifyNetworkDisconnected() {
676         sendMessage(CMD_NETWORK_DISCONNECTED);
677     }
678 
679     /**
680      * Send a notification to NetworkMonitor indicating that link properties have changed.
681      */
notifyLinkPropertiesChanged(final LinkProperties lp)682     public void notifyLinkPropertiesChanged(final LinkProperties lp) {
683         sendMessage(EVENT_LINK_PROPERTIES_CHANGED, new LinkProperties(lp));
684     }
685 
686     /**
687      * Send a notification to NetworkMonitor indicating that network capabilities have changed.
688      */
notifyNetworkCapabilitiesChanged(final NetworkCapabilities nc)689     public void notifyNetworkCapabilitiesChanged(final NetworkCapabilities nc) {
690         sendMessage(EVENT_NETWORK_CAPABILITIES_CHANGED, new NetworkCapabilities(nc));
691     }
692 
693     /**
694      * Request the captive portal application to be launched.
695      */
launchCaptivePortalApp()696     public void launchCaptivePortalApp() {
697         sendMessage(CMD_LAUNCH_CAPTIVE_PORTAL_APP);
698     }
699 
700     /**
701      * Notify that the captive portal app was closed with the provided response code.
702      */
notifyCaptivePortalAppFinished(int response)703     public void notifyCaptivePortalAppFinished(int response) {
704         sendMessage(CMD_CAPTIVE_PORTAL_APP_FINISHED, response);
705     }
706 
707     @Override
log(String s)708     protected void log(String s) {
709         if (DBG) Log.d(TAG + "/" + mCleartextDnsNetwork.toString(), s);
710     }
711 
validationLog(int probeType, Object url, String msg)712     private void validationLog(int probeType, Object url, String msg) {
713         String probeName = ValidationProbeEvent.getProbeName(probeType);
714         validationLog(String.format("%s %s %s", probeName, url, msg));
715     }
716 
validationLog(String s)717     private void validationLog(String s) {
718         if (DBG) log(s);
719         mValidationLogs.log(s);
720     }
721 
validationStage()722     private ValidationStage validationStage() {
723         return 0 == mValidations ? ValidationStage.FIRST_VALIDATION : ValidationStage.REVALIDATION;
724     }
725 
isValidationRequired()726     private boolean isValidationRequired() {
727         return NetworkMonitorUtils.isValidationRequired(mNetworkCapabilities);
728     }
729 
isPrivateDnsValidationRequired()730     private boolean isPrivateDnsValidationRequired() {
731         return NetworkMonitorUtils.isPrivateDnsValidationRequired(mNetworkCapabilities);
732     }
733 
notifyNetworkTested(NetworkTestResultParcelable result)734     private void notifyNetworkTested(NetworkTestResultParcelable result) {
735         try {
736             if (mCallbackVersion <= 5) {
737                 mCallback.notifyNetworkTested(
738                         getLegacyTestResult(result.result, result.probesSucceeded),
739                         result.redirectUrl);
740             } else {
741                 mCallback.notifyNetworkTestedWithExtras(result);
742             }
743         } catch (RemoteException e) {
744             Log.e(TAG, "Error sending network test result", e);
745         }
746     }
747 
748     /**
749      * Get the test result that was used as an int up to interface version 5.
750      *
751      * <p>For callback version < 3 (only used in Q beta preview builds), the int represented one of
752      * the NETWORK_TEST_RESULT_* constants.
753      *
754      * <p>Q released with version 3, which used a single int for both the evaluation result bitmask,
755      * and the probesSucceeded bitmask.
756      */
getLegacyTestResult(int evaluationResult, int probesSucceeded)757     protected int getLegacyTestResult(int evaluationResult, int probesSucceeded) {
758         if (mCallbackVersion < 3) {
759             if ((evaluationResult & NETWORK_VALIDATION_RESULT_VALID) != 0) {
760                 return NETWORK_TEST_RESULT_VALID;
761             }
762             if ((evaluationResult & NETWORK_VALIDATION_RESULT_PARTIAL) != 0) {
763                 return NETWORK_TEST_RESULT_PARTIAL_CONNECTIVITY;
764             }
765             return NETWORK_TEST_RESULT_INVALID;
766         }
767 
768         return evaluationResult | probesSucceeded;
769     }
770 
notifyProbeStatusChanged(int probesCompleted, int probesSucceeded)771     private void notifyProbeStatusChanged(int probesCompleted, int probesSucceeded) {
772         try {
773             mCallback.notifyProbeStatusChanged(probesCompleted, probesSucceeded);
774         } catch (RemoteException e) {
775             Log.e(TAG, "Error sending probe status", e);
776         }
777     }
778 
showProvisioningNotification(String action)779     private void showProvisioningNotification(String action) {
780         try {
781             mCallback.showProvisioningNotification(action, mContext.getPackageName());
782         } catch (RemoteException e) {
783             Log.e(TAG, "Error showing provisioning notification", e);
784         }
785     }
786 
hideProvisioningNotification()787     private void hideProvisioningNotification() {
788         try {
789             mCallback.hideProvisioningNotification();
790         } catch (RemoteException e) {
791             Log.e(TAG, "Error hiding provisioning notification", e);
792         }
793     }
794 
notifyDataStallSuspected(@onNull DataStallReportParcelable p)795     private void notifyDataStallSuspected(@NonNull DataStallReportParcelable p) {
796         try {
797             mCallback.notifyDataStallSuspected(p);
798         } catch (RemoteException e) {
799             Log.e(TAG, "Error sending notification for suspected data stall", e);
800         }
801     }
802 
startMetricsCollection()803     private void startMetricsCollection() {
804         if (!mMetricsEnabled) return;
805         try {
806             synchronized (mNetworkValidationMetrics) {
807                 mNetworkValidationMetrics.startCollection(mNetworkCapabilities);
808             }
809         } catch (Exception e) {
810             Log.wtf(TAG, "Error resetting validation metrics", e);
811         }
812     }
813 
recordProbeEventMetrics(ProbeType type, long latencyMicros, ProbeResult result, CaptivePortalDataShim capportData)814     private void recordProbeEventMetrics(ProbeType type, long latencyMicros, ProbeResult result,
815             CaptivePortalDataShim capportData) {
816         if (!mMetricsEnabled) return;
817         try {
818             synchronized (mNetworkValidationMetrics) {
819                 mNetworkValidationMetrics.addProbeEvent(type, latencyMicros, result, capportData);
820             }
821         } catch (Exception e) {
822             Log.wtf(TAG, "Error recording probe event", e);
823         }
824     }
825 
recordValidationResult(int result, String redirectUrl)826     private void recordValidationResult(int result, String redirectUrl) {
827         if (!mMetricsEnabled) return;
828         try {
829             synchronized (mNetworkValidationMetrics) {
830                 mNetworkValidationMetrics.setValidationResult(result, redirectUrl);
831             }
832         } catch (Exception e) {
833             Log.wtf(TAG, "Error recording validation result", e);
834         }
835     }
836 
maybeStopCollectionAndSendMetrics()837     private void maybeStopCollectionAndSendMetrics() {
838         if (!mMetricsEnabled) return;
839         try {
840             synchronized (mNetworkValidationMetrics) {
841                 mNetworkValidationMetrics.maybeStopCollectionAndSend();
842             }
843         } catch (Exception e) {
844             Log.wtf(TAG, "Error sending validation stats", e);
845         }
846     }
847 
848     // DefaultState is the parent of all States.  It exists only to handle CMD_* messages but
849     // does not entail any real state (hence no enter() or exit() routines).
850     private class DefaultState extends State {
851         @Override
processMessage(Message message)852         public boolean processMessage(Message message) {
853             switch (message.what) {
854                 case CMD_NETWORK_CONNECTED:
855                     updateConnectedNetworkAttributes(message);
856                     logNetworkEvent(NetworkEvent.NETWORK_CONNECTED);
857                     transitionTo(mEvaluatingState);
858                     return HANDLED;
859                 case CMD_NETWORK_DISCONNECTED:
860                     maybeStopCollectionAndSendMetrics();
861                     logNetworkEvent(NetworkEvent.NETWORK_DISCONNECTED);
862                     quit();
863                     return HANDLED;
864                 case CMD_FORCE_REEVALUATION:
865                 case CMD_CAPTIVE_PORTAL_RECHECK:
866                     if (getCurrentState() == mDefaultState) {
867                         // Before receiving CMD_NETWORK_CONNECTED (when still in mDefaultState),
868                         // requests to reevaluate are not valid: drop them.
869                         return HANDLED;
870                     }
871                     String msg = "Forcing reevaluation for UID " + message.arg1;
872                     final DnsStallDetector dsd = getDnsStallDetector();
873                     if (dsd != null) {
874                         msg += ". Dns signal count: " + dsd.getConsecutiveTimeoutCount();
875                     }
876                     validationLog(msg);
877                     mUidResponsibleForReeval = message.arg1;
878                     transitionTo(mEvaluatingState);
879                     return HANDLED;
880                 case CMD_CAPTIVE_PORTAL_APP_FINISHED:
881                     log("CaptivePortal App responded with " + message.arg1);
882 
883                     // If the user has seen and acted on a captive portal notification, and the
884                     // captive portal app is now closed, disable HTTPS probes. This avoids the
885                     // following pathological situation:
886                     //
887                     // 1. HTTP probe returns a captive portal, HTTPS probe fails or times out.
888                     // 2. User opens the app and logs into the captive portal.
889                     // 3. HTTP starts working, but HTTPS still doesn't work for some other reason -
890                     //    perhaps due to the network blocking HTTPS?
891                     //
892                     // In this case, we'll fail to validate the network even after the app is
893                     // dismissed. There is now no way to use this network, because the app is now
894                     // gone, so the user cannot select "Use this network as is".
895                     mUseHttps = false;
896 
897                     switch (message.arg1) {
898                         case APP_RETURN_DISMISSED:
899                             sendMessage(CMD_FORCE_REEVALUATION, NO_UID, 0);
900                             break;
901                         case APP_RETURN_WANTED_AS_IS:
902                             mDontDisplaySigninNotification = true;
903                             // If the user wants to use this network anyway, there is no need to
904                             // perform the bandwidth check even if configured.
905                             mIsBandwidthCheckPassedOrIgnored = true;
906                             // TODO: Distinguish this from a network that actually validates.
907                             // Displaying the "x" on the system UI icon may still be a good idea.
908                             transitionTo(mEvaluatingPrivateDnsState);
909                             break;
910                         case APP_RETURN_UNWANTED:
911                             mDontDisplaySigninNotification = true;
912                             mUserDoesNotWant = true;
913                             mEvaluationState.reportEvaluationResult(
914                                     NETWORK_VALIDATION_RESULT_INVALID, null);
915                             // TODO: Should teardown network.
916                             mUidResponsibleForReeval = 0;
917                             transitionTo(mEvaluatingState);
918                             break;
919                     }
920                     return HANDLED;
921                 case CMD_PRIVATE_DNS_SETTINGS_CHANGED: {
922                     final PrivateDnsConfig cfg = (PrivateDnsConfig) message.obj;
923                     if (!isPrivateDnsValidationRequired() || cfg == null || !cfg.inStrictMode()) {
924                         // No DNS resolution required.
925                         //
926                         // We don't force any validation in opportunistic mode
927                         // here. Opportunistic mode nameservers are validated
928                         // separately within netd.
929                         //
930                         // Reset Private DNS settings state.
931                         mPrivateDnsProviderHostname = "";
932                         break;
933                     }
934 
935                     mPrivateDnsProviderHostname = cfg.hostname;
936 
937                     // DNS resolutions via Private DNS strict mode block for a
938                     // few seconds (~4.2) checking for any IP addresses to
939                     // arrive and validate. Initiating a (re)evaluation now
940                     // should not significantly alter the validation outcome.
941                     //
942                     // No matter what: enqueue a validation request; one of
943                     // three things can happen with this request:
944                     //     [1] ignored (EvaluatingState or CaptivePortalState)
945                     //     [2] transition to EvaluatingPrivateDnsState
946                     //         (DefaultState and ValidatedState)
947                     //     [3] handled (EvaluatingPrivateDnsState)
948                     //
949                     // The Private DNS configuration to be evaluated will:
950                     //     [1] be skipped (not in strict mode), or
951                     //     [2] validate (huzzah), or
952                     //     [3] encounter some problem (invalid hostname,
953                     //         no resolved IP addresses, IPs unreachable,
954                     //         port 853 unreachable, port 853 is not running a
955                     //         DNS-over-TLS server, et cetera).
956                     // Cancel any outstanding CMD_EVALUATE_PRIVATE_DNS.
957                     removeMessages(CMD_EVALUATE_PRIVATE_DNS);
958                     sendMessage(CMD_EVALUATE_PRIVATE_DNS);
959                     break;
960                 }
961                 case EVENT_DNS_NOTIFICATION:
962                     final DnsStallDetector detector = getDnsStallDetector();
963                     if (detector != null) {
964                         detector.accumulateConsecutiveDnsTimeoutCount(message.arg1);
965                     }
966                     break;
967                 // Set mAcceptPartialConnectivity to true and if network start evaluating or
968                 // re-evaluating and get the result of partial connectivity, ProbingState will
969                 // disable HTTPS probe and transition to EvaluatingPrivateDnsState.
970                 case EVENT_ACCEPT_PARTIAL_CONNECTIVITY:
971                     maybeDisableHttpsProbing(true /* acceptPartial */);
972                     break;
973                 case EVENT_LINK_PROPERTIES_CHANGED:
974                     final Uri oldCapportUrl = getCaptivePortalApiUrl(mLinkProperties);
975                     mLinkProperties = (LinkProperties) message.obj;
976                     final Uri newCapportUrl = getCaptivePortalApiUrl(mLinkProperties);
977                     if (!Objects.equals(oldCapportUrl, newCapportUrl)) {
978                         sendMessage(CMD_FORCE_REEVALUATION, NO_UID, 0);
979                     }
980                     break;
981                 case EVENT_NETWORK_CAPABILITIES_CHANGED:
982                     mNetworkCapabilities = (NetworkCapabilities) message.obj;
983                     break;
984                 default:
985                     break;
986             }
987             return HANDLED;
988         }
989     }
990 
991     // Being in the ValidatedState State indicates a Network is:
992     // - Successfully validated, or
993     // - Wanted "as is" by the user, or
994     // - Does not satisfy the default NetworkRequest and so validation has been skipped.
995     private class ValidatedState extends State {
996         @Override
enter()997         public void enter() {
998             maybeLogEvaluationResult(
999                     networkEventType(validationStage(), EvaluationResult.VALIDATED));
1000             // If the user has accepted partial connectivity and HTTPS probing is disabled, then
1001             // mark the network as validated and partial so that settings can keep informing the
1002             // user that the connection is limited.
1003             int result = NETWORK_VALIDATION_RESULT_VALID;
1004             if (!mUseHttps && mAcceptPartialConnectivity) {
1005                 result |= NETWORK_VALIDATION_RESULT_PARTIAL;
1006             }
1007             mEvaluationState.reportEvaluationResult(result, null /* redirectUrl */);
1008             mValidations++;
1009             initSocketTrackingIfRequired();
1010             // start periodical polling.
1011             sendTcpPollingEvent();
1012             maybeStopCollectionAndSendMetrics();
1013         }
1014 
initSocketTrackingIfRequired()1015         private void initSocketTrackingIfRequired() {
1016             if (!isValidationRequired()) return;
1017 
1018             final TcpSocketTracker tst = getTcpSocketTracker();
1019             if (tst != null) {
1020                 tst.pollSocketsInfo();
1021             }
1022         }
1023 
1024         @Override
processMessage(Message message)1025         public boolean processMessage(Message message) {
1026             switch (message.what) {
1027                 case CMD_NETWORK_CONNECTED:
1028                     updateConnectedNetworkAttributes(message);
1029                     transitionTo(mValidatedState);
1030                     break;
1031                 case CMD_EVALUATE_PRIVATE_DNS:
1032                     // TODO: this causes reevaluation of a single probe that is not counted in
1033                     // metrics. Add support for such reevaluation probes in metrics, and log them
1034                     // separately.
1035                     transitionTo(mEvaluatingPrivateDnsState);
1036                     break;
1037                 case EVENT_DNS_NOTIFICATION:
1038                     final DnsStallDetector dsd = getDnsStallDetector();
1039                     if (dsd == null) break;
1040 
1041                     dsd.accumulateConsecutiveDnsTimeoutCount(message.arg1);
1042                     if (evaluateDataStall()) {
1043                         transitionTo(mEvaluatingState);
1044                     }
1045                     break;
1046                 case EVENT_POLL_TCPINFO:
1047                     final TcpSocketTracker tst = getTcpSocketTracker();
1048                     if (tst == null) break;
1049                     // Transit if retrieve socket info is succeeded and suspected as a stall.
1050                     if (tst.pollSocketsInfo() && evaluateDataStall()) {
1051                         transitionTo(mEvaluatingState);
1052                     } else {
1053                         sendTcpPollingEvent();
1054                     }
1055                     break;
1056                 default:
1057                     return NOT_HANDLED;
1058             }
1059             return HANDLED;
1060         }
1061 
evaluateDataStall()1062         boolean evaluateDataStall() {
1063             if (isDataStall()) {
1064                 validationLog("Suspecting data stall, reevaluate");
1065                 return true;
1066             }
1067             return false;
1068         }
1069 
1070         @Override
exit()1071         public void exit() {
1072             // Not useful for non-ValidatedState.
1073             removeMessages(EVENT_POLL_TCPINFO);
1074         }
1075     }
1076 
1077     @VisibleForTesting
sendTcpPollingEvent()1078     void sendTcpPollingEvent() {
1079         if (isValidationRequired()) {
1080             sendMessageDelayed(EVENT_POLL_TCPINFO, getTcpPollingInterval());
1081         }
1082     }
1083 
maybeWriteDataStallStats(@onNull final CaptivePortalProbeResult result)1084     private void maybeWriteDataStallStats(@NonNull final CaptivePortalProbeResult result) {
1085         if (mDataStallTypeToCollect == DATA_STALL_EVALUATION_TYPE_NONE) return;
1086         /*
1087          * Collect data stall detection level information for each transport type. Collect type
1088          * specific information for cellular and wifi only currently. Generate
1089          * DataStallDetectionStats for each transport type. E.g., if a network supports both
1090          * TRANSPORT_WIFI and TRANSPORT_VPN, two DataStallDetectionStats will be generated.
1091          */
1092         final int[] transports = mNetworkCapabilities.getTransportTypes();
1093         for (int i = 0; i < transports.length; i++) {
1094             final DataStallDetectionStats stats =
1095                     buildDataStallDetectionStats(transports[i], mDataStallTypeToCollect);
1096             mDependencies.writeDataStallDetectionStats(stats, result);
1097         }
1098         mDataStallTypeToCollect = DATA_STALL_EVALUATION_TYPE_NONE;
1099     }
1100 
1101     @VisibleForTesting
buildDataStallDetectionStats(int transport, @EvaluationType int evaluationType)1102     protected DataStallDetectionStats buildDataStallDetectionStats(int transport,
1103             @EvaluationType int evaluationType) {
1104         final DataStallDetectionStats.Builder stats = new DataStallDetectionStats.Builder();
1105         if (VDBG_STALL) {
1106             log("collectDataStallMetrics: type=" + transport + ", evaluation=" + evaluationType);
1107         }
1108         stats.setEvaluationType(evaluationType);
1109         stats.setNetworkType(transport);
1110         switch (transport) {
1111             case NetworkCapabilities.TRANSPORT_WIFI:
1112                 // TODO: Update it if status query in dual wifi is supported.
1113                 final WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
1114                 stats.setWiFiData(wifiInfo);
1115                 break;
1116             case NetworkCapabilities.TRANSPORT_CELLULAR:
1117                 final boolean isRoaming = !mNetworkCapabilities.hasCapability(
1118                         NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING);
1119                 final SignalStrength ss = mTelephonyManager.getSignalStrength();
1120                 // TODO(b/120452078): Support multi-sim.
1121                 stats.setCellData(
1122                         mTelephonyManager.getDataNetworkType(),
1123                         isRoaming,
1124                         mTelephonyManager.getNetworkOperator(),
1125                         mTelephonyManager.getSimOperator(),
1126                         (ss != null)
1127                         ? ss.getLevel() : CellSignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN);
1128                 break;
1129             default:
1130                 // No transport type specific information for the other types.
1131                 break;
1132         }
1133 
1134         addDnsEvents(stats);
1135         addTcpStats(stats);
1136 
1137         return stats.build();
1138     }
1139 
addTcpStats(@onNull final DataStallDetectionStats.Builder stats)1140     private void addTcpStats(@NonNull final DataStallDetectionStats.Builder stats) {
1141         final TcpSocketTracker tst = getTcpSocketTracker();
1142         if (tst == null) return;
1143 
1144         stats.setTcpSentSinceLastRecv(tst.getSentSinceLastRecv());
1145         stats.setTcpFailRate(tst.getLatestPacketFailPercentage());
1146     }
1147 
1148     @VisibleForTesting
addDnsEvents(@onNull final DataStallDetectionStats.Builder stats)1149     protected void addDnsEvents(@NonNull final DataStallDetectionStats.Builder stats) {
1150         final DnsStallDetector dsd = getDnsStallDetector();
1151         if (dsd == null) return;
1152 
1153         final int size = dsd.mResultIndices.size();
1154         for (int i = 1; i <= DEFAULT_DNS_LOG_SIZE && i <= size; i++) {
1155             final int index = dsd.mResultIndices.indexOf(size - i);
1156             stats.addDnsEvent(dsd.mDnsEvents[index].mReturnCode, dsd.mDnsEvents[index].mTimeStamp);
1157         }
1158     }
1159 
1160 
1161     // Being in the MaybeNotifyState State indicates the user may have been notified that sign-in
1162     // is required.  This State takes care to clear the notification upon exit from the State.
1163     private class MaybeNotifyState extends State {
1164         @Override
processMessage(Message message)1165         public boolean processMessage(Message message) {
1166             switch (message.what) {
1167                 case CMD_LAUNCH_CAPTIVE_PORTAL_APP:
1168                     final Bundle appExtras = new Bundle();
1169                     // OneAddressPerFamilyNetwork is not parcelable across processes.
1170                     final Network network = new Network(mCleartextDnsNetwork);
1171                     appExtras.putParcelable(ConnectivityManager.EXTRA_NETWORK, network);
1172                     final CaptivePortalProbeResult probeRes = mLastPortalProbeResult;
1173                     // Use redirect URL from AP if exists.
1174                     final String portalUrl =
1175                             (useRedirectUrlForPortal() && makeURL(probeRes.redirectUrl) != null)
1176                             ? probeRes.redirectUrl : probeRes.detectUrl;
1177                     appExtras.putString(EXTRA_CAPTIVE_PORTAL_URL, portalUrl);
1178                     if (probeRes.probeSpec != null) {
1179                         final String encodedSpec = probeRes.probeSpec.getEncodedSpec();
1180                         appExtras.putString(EXTRA_CAPTIVE_PORTAL_PROBE_SPEC, encodedSpec);
1181                     }
1182                     appExtras.putString(ConnectivityManager.EXTRA_CAPTIVE_PORTAL_USER_AGENT,
1183                             mCaptivePortalUserAgent);
1184                     if (mNotifier != null) {
1185                         mNotifier.notifyCaptivePortalValidationPending(network);
1186                     }
1187                     mCm.startCaptivePortalApp(network, appExtras);
1188                     return HANDLED;
1189                 default:
1190                     return NOT_HANDLED;
1191             }
1192         }
1193 
useRedirectUrlForPortal()1194         private boolean useRedirectUrlForPortal() {
1195             // It must match the conditions in CaptivePortalLogin in which the redirect URL is not
1196             // used to validate that the portal is gone.
1197             final boolean aboveQ =
1198                     ShimUtils.isReleaseOrDevelopmentApiAbove(Build.VERSION_CODES.Q);
1199             return aboveQ && mDependencies.isFeatureEnabled(mContext, NAMESPACE_CONNECTIVITY,
1200                     DISMISS_PORTAL_IN_VALIDATED_NETWORK, aboveQ /* defaultEnabled */);
1201         }
1202 
1203         @Override
exit()1204         public void exit() {
1205             if (mLaunchCaptivePortalAppBroadcastReceiver != null) {
1206                 mContext.unregisterReceiver(mLaunchCaptivePortalAppBroadcastReceiver);
1207                 mLaunchCaptivePortalAppBroadcastReceiver = null;
1208             }
1209             hideProvisioningNotification();
1210         }
1211     }
1212 
1213     // Being in the EvaluatingState State indicates the Network is being evaluated for internet
1214     // connectivity, or that the user has indicated that this network is unwanted.
1215     private class EvaluatingState extends State {
1216         private Uri mEvaluatingCapportUrl;
1217 
1218         @Override
enter()1219         public void enter() {
1220             // If we have already started to track time spent in EvaluatingState
1221             // don't reset the timer due simply to, say, commands or events that
1222             // cause us to exit and re-enter EvaluatingState.
1223             if (!mEvaluationTimer.isStarted()) {
1224                 mEvaluationTimer.start();
1225             }
1226             sendMessage(CMD_REEVALUATE, ++mReevaluateToken, 0);
1227             if (mUidResponsibleForReeval != INVALID_UID) {
1228                 TrafficStats.setThreadStatsUid(mUidResponsibleForReeval);
1229                 mUidResponsibleForReeval = INVALID_UID;
1230             }
1231             mReevaluateDelayMs = INITIAL_REEVALUATE_DELAY_MS;
1232             mEvaluateAttempts = 0;
1233             mEvaluatingCapportUrl = getCaptivePortalApiUrl(mLinkProperties);
1234             // Reset all current probe results to zero, but retain current validation state until
1235             // validation succeeds or fails.
1236             mEvaluationState.clearProbeResults();
1237         }
1238 
1239         @Override
processMessage(Message message)1240         public boolean processMessage(Message message) {
1241             switch (message.what) {
1242                 case CMD_REEVALUATE:
1243                     if (message.arg1 != mReevaluateToken || mUserDoesNotWant) {
1244                         return HANDLED;
1245                     }
1246                     // Don't bother validating networks that don't satisfy the default request.
1247                     // This includes:
1248                     //  - VPNs which can be considered explicitly desired by the user and the
1249                     //    user's desire trumps whether the network validates.
1250                     //  - Networks that don't provide Internet access.  It's unclear how to
1251                     //    validate such networks.
1252                     //  - Untrusted networks.  It's unsafe to prompt the user to sign-in to
1253                     //    such networks and the user didn't express interest in connecting to
1254                     //    such networks (an app did) so the user may be unhappily surprised when
1255                     //    asked to sign-in to a network they didn't want to connect to in the
1256                     //    first place.  Validation could be done to adjust the network scores
1257                     //    however these networks are app-requested and may not be intended for
1258                     //    general usage, in which case general validation may not be an accurate
1259                     //    measure of the network's quality.  Only the app knows how to evaluate
1260                     //    the network so don't bother validating here.  Furthermore sending HTTP
1261                     //    packets over the network may be undesirable, for example an extremely
1262                     //    expensive metered network, or unwanted leaking of the User Agent string.
1263                     //
1264                     // On networks that need to support private DNS in strict mode (e.g., VPNs, but
1265                     // not networks that don't provide Internet access), we still need to perform
1266                     // private DNS server resolution.
1267                     if (!isValidationRequired()) {
1268                         if (isPrivateDnsValidationRequired()) {
1269                             validationLog("Network would not satisfy default request, "
1270                                     + "resolving private DNS");
1271                             transitionTo(mEvaluatingPrivateDnsState);
1272                         } else {
1273                             validationLog("Network would not satisfy default request, "
1274                                     + "not validating");
1275                             transitionTo(mValidatedState);
1276                         }
1277                         return HANDLED;
1278                     }
1279                     mEvaluateAttempts++;
1280 
1281                     transitionTo(mProbingState);
1282                     return HANDLED;
1283                 case CMD_FORCE_REEVALUATION:
1284                     // The evaluation process restarts via EvaluatingState#enter.
1285                     return shouldAcceptForceRevalidation() ? NOT_HANDLED : HANDLED;
1286                 // Disable HTTPS probe and transition to EvaluatingPrivateDnsState because:
1287                 // 1. Network is connected and finish the network validation.
1288                 // 2. NetworkMonitor detects network is partial connectivity and user accepts it.
1289                 case EVENT_ACCEPT_PARTIAL_CONNECTIVITY:
1290                     maybeDisableHttpsProbing(true /* acceptPartial */);
1291                     transitionTo(mEvaluatingPrivateDnsState);
1292                     return HANDLED;
1293                 default:
1294                     return NOT_HANDLED;
1295             }
1296         }
1297 
shouldAcceptForceRevalidation()1298         private boolean shouldAcceptForceRevalidation() {
1299             // If the captive portal URL has changed since the last evaluation attempt, always
1300             // revalidate. Otherwise, ignore any re-evaluation requests before
1301             // IGNORE_REEVALUATE_ATTEMPTS are made.
1302             return mEvaluateAttempts >= IGNORE_REEVALUATE_ATTEMPTS
1303                     || !Objects.equals(
1304                             mEvaluatingCapportUrl, getCaptivePortalApiUrl(mLinkProperties));
1305         }
1306 
1307         @Override
exit()1308         public void exit() {
1309             TrafficStats.clearThreadStatsUid();
1310         }
1311     }
1312 
1313     // BroadcastReceiver that waits for a particular Intent and then posts a message.
1314     private class CustomIntentReceiver extends BroadcastReceiver {
1315         private final int mToken;
1316         private final int mWhat;
1317         private final String mAction;
CustomIntentReceiver(String action, int token, int what)1318         CustomIntentReceiver(String action, int token, int what) {
1319             mToken = token;
1320             mWhat = what;
1321             mAction = action + "_" + mCleartextDnsNetwork.getNetworkHandle() + "_" + token;
1322             mContext.registerReceiver(this, new IntentFilter(mAction));
1323         }
getPendingIntent()1324         public PendingIntent getPendingIntent() {
1325             final Intent intent = new Intent(mAction);
1326             intent.setPackage(mContext.getPackageName());
1327             return PendingIntent.getBroadcast(mContext, 0, intent, 0);
1328         }
1329         @Override
onReceive(Context context, Intent intent)1330         public void onReceive(Context context, Intent intent) {
1331             if (intent.getAction().equals(mAction)) sendMessage(obtainMessage(mWhat, mToken));
1332         }
1333     }
1334 
1335     // Being in the CaptivePortalState State indicates a captive portal was detected and the user
1336     // has been shown a notification to sign-in.
1337     private class CaptivePortalState extends State {
1338         private static final String ACTION_LAUNCH_CAPTIVE_PORTAL_APP =
1339                 "android.net.netmon.launchCaptivePortalApp";
1340 
1341         @Override
enter()1342         public void enter() {
1343             maybeLogEvaluationResult(
1344                     networkEventType(validationStage(), EvaluationResult.CAPTIVE_PORTAL));
1345             // Don't annoy user with sign-in notifications.
1346             if (mDontDisplaySigninNotification) return;
1347             // Create a CustomIntentReceiver that sends us a
1348             // CMD_LAUNCH_CAPTIVE_PORTAL_APP message when the user
1349             // touches the notification.
1350             if (mLaunchCaptivePortalAppBroadcastReceiver == null) {
1351                 // Wait for result.
1352                 mLaunchCaptivePortalAppBroadcastReceiver = new CustomIntentReceiver(
1353                         ACTION_LAUNCH_CAPTIVE_PORTAL_APP, new Random().nextInt(),
1354                         CMD_LAUNCH_CAPTIVE_PORTAL_APP);
1355                 // Display the sign in notification.
1356                 // Only do this once for every time we enter MaybeNotifyState. b/122164725
1357                 showProvisioningNotification(mLaunchCaptivePortalAppBroadcastReceiver.mAction);
1358             }
1359             // Retest for captive portal occasionally.
1360             sendMessageDelayed(CMD_CAPTIVE_PORTAL_RECHECK, 0 /* no UID */,
1361                     CAPTIVE_PORTAL_REEVALUATE_DELAY_MS);
1362             mValidations++;
1363             maybeStopCollectionAndSendMetrics();
1364         }
1365 
1366         @Override
exit()1367         public void exit() {
1368             removeMessages(CMD_CAPTIVE_PORTAL_RECHECK);
1369         }
1370     }
1371 
1372     private class EvaluatingPrivateDnsState extends State {
1373         private int mPrivateDnsReevalDelayMs;
1374         private PrivateDnsConfig mPrivateDnsConfig;
1375 
1376         @Override
enter()1377         public void enter() {
1378             mPrivateDnsReevalDelayMs = INITIAL_REEVALUATE_DELAY_MS;
1379             mPrivateDnsConfig = null;
1380             sendMessage(CMD_EVALUATE_PRIVATE_DNS);
1381         }
1382 
1383         @Override
processMessage(Message msg)1384         public boolean processMessage(Message msg) {
1385             switch (msg.what) {
1386                 case CMD_EVALUATE_PRIVATE_DNS:
1387                     if (inStrictMode()) {
1388                         if (!isStrictModeHostnameResolved()) {
1389                             resolveStrictModeHostname();
1390 
1391                             if (isStrictModeHostnameResolved()) {
1392                                 notifyPrivateDnsConfigResolved();
1393                             } else {
1394                                 handlePrivateDnsEvaluationFailure();
1395                                 // The private DNS probe fails-fast if the server hostname cannot
1396                                 // be resolved. Record it as a failure with zero latency.
1397                                 // TODO: refactor this together with the probe recorded in
1398                                 // sendPrivateDnsProbe, so logging is symmetric / easier to follow.
1399                                 recordProbeEventMetrics(ProbeType.PT_PRIVDNS, 0 /* latency */,
1400                                         ProbeResult.PR_FAILURE, null /* capportData */);
1401                                 break;
1402                             }
1403                         }
1404 
1405                         // Look up a one-time hostname, to bypass caching.
1406                         //
1407                         // Note that this will race with ConnectivityService
1408                         // code programming the DNS-over-TLS server IP addresses
1409                         // into netd (if invoked, above). If netd doesn't know
1410                         // the IP addresses yet, or if the connections to the IP
1411                         // addresses haven't yet been validated, netd will block
1412                         // for up to a few seconds before failing the lookup.
1413                         if (!sendPrivateDnsProbe()) {
1414                             handlePrivateDnsEvaluationFailure();
1415                             break;
1416                         }
1417                         handlePrivateDnsEvaluationSuccess();
1418                     } else {
1419                         mEvaluationState.removeProbeResult(NETWORK_VALIDATION_PROBE_PRIVDNS);
1420                     }
1421 
1422                     if (needEvaluatingBandwidth()) {
1423                         transitionTo(mEvaluatingBandwidthState);
1424                     } else {
1425                         // All good!
1426                         transitionTo(mValidatedState);
1427                     }
1428                     break;
1429                 case CMD_PRIVATE_DNS_SETTINGS_CHANGED:
1430                     // When settings change the reevaluation timer must be reset.
1431                     mPrivateDnsReevalDelayMs = INITIAL_REEVALUATE_DELAY_MS;
1432                     // Let the message bubble up and be handled by parent states as usual.
1433                     return NOT_HANDLED;
1434                 default:
1435                     return NOT_HANDLED;
1436             }
1437             return HANDLED;
1438         }
1439 
inStrictMode()1440         private boolean inStrictMode() {
1441             return !TextUtils.isEmpty(mPrivateDnsProviderHostname);
1442         }
1443 
isStrictModeHostnameResolved()1444         private boolean isStrictModeHostnameResolved() {
1445             return (mPrivateDnsConfig != null)
1446                     && mPrivateDnsConfig.hostname.equals(mPrivateDnsProviderHostname)
1447                     && (mPrivateDnsConfig.ips.length > 0);
1448         }
1449 
resolveStrictModeHostname()1450         private void resolveStrictModeHostname() {
1451             try {
1452                 // Do a blocking DNS resolution using the network-assigned nameservers.
1453                 final InetAddress[] ips = DnsUtils.getAllByName(mDependencies.getDnsResolver(),
1454                         mCleartextDnsNetwork, mPrivateDnsProviderHostname, getDnsProbeTimeout(),
1455                         str -> validationLog("Strict mode hostname resolution " + str));
1456                 mPrivateDnsConfig = new PrivateDnsConfig(mPrivateDnsProviderHostname, ips);
1457             } catch (UnknownHostException uhe) {
1458                 mPrivateDnsConfig = null;
1459             }
1460         }
1461 
notifyPrivateDnsConfigResolved()1462         private void notifyPrivateDnsConfigResolved() {
1463             try {
1464                 mCallback.notifyPrivateDnsConfigResolved(mPrivateDnsConfig.toParcel());
1465             } catch (RemoteException e) {
1466                 Log.e(TAG, "Error sending private DNS config resolved notification", e);
1467             }
1468         }
1469 
handlePrivateDnsEvaluationSuccess()1470         private void handlePrivateDnsEvaluationSuccess() {
1471             mEvaluationState.noteProbeResult(NETWORK_VALIDATION_PROBE_PRIVDNS,
1472                     true /* succeeded */);
1473         }
1474 
handlePrivateDnsEvaluationFailure()1475         private void handlePrivateDnsEvaluationFailure() {
1476             mEvaluationState.noteProbeResult(NETWORK_VALIDATION_PROBE_PRIVDNS,
1477                     false /* succeeded */);
1478             mEvaluationState.reportEvaluationResult(NETWORK_VALIDATION_RESULT_INVALID,
1479                     null /* redirectUrl */);
1480             // Queue up a re-evaluation with backoff.
1481             //
1482             // TODO: Consider abandoning this state after a few attempts and
1483             // transitioning back to EvaluatingState, to perhaps give ourselves
1484             // the opportunity to (re)detect a captive portal or something.
1485             //
1486             sendMessageDelayed(CMD_EVALUATE_PRIVATE_DNS, mPrivateDnsReevalDelayMs);
1487             mPrivateDnsReevalDelayMs *= 2;
1488             if (mPrivateDnsReevalDelayMs > MAX_REEVALUATE_DELAY_MS) {
1489                 mPrivateDnsReevalDelayMs = MAX_REEVALUATE_DELAY_MS;
1490             }
1491         }
1492 
sendPrivateDnsProbe()1493         private boolean sendPrivateDnsProbe() {
1494             final String host = UUID.randomUUID().toString().substring(0, 8)
1495                     + PRIVATE_DNS_PROBE_HOST_SUFFIX;
1496             final Stopwatch watch = new Stopwatch().start();
1497             boolean success = false;
1498             long time;
1499             try {
1500                 final InetAddress[] ips = mNetwork.getAllByName(host);
1501                 time = watch.stop();
1502                 final String strIps = Arrays.toString(ips);
1503                 success = (ips != null && ips.length > 0);
1504                 validationLog(PROBE_PRIVDNS, host, String.format("%dus: %s", time, strIps));
1505             } catch (UnknownHostException uhe) {
1506                 time = watch.stop();
1507                 validationLog(PROBE_PRIVDNS, host,
1508                         String.format("%dus - Error: %s", time, uhe.getMessage()));
1509             }
1510             recordProbeEventMetrics(ProbeType.PT_PRIVDNS, time, success ? ProbeResult.PR_SUCCESS :
1511                     ProbeResult.PR_FAILURE, null /* capportData */);
1512             logValidationProbe(time, PROBE_PRIVDNS, success ? DNS_SUCCESS : DNS_FAILURE);
1513             return success;
1514         }
1515     }
1516 
1517     private class ProbingState extends State {
1518         private Thread mThread;
1519 
1520         @Override
enter()1521         public void enter() {
1522             // When starting a full probe cycle here, record any pending stats (for example if
1523             // CMD_FORCE_REEVALUATE was called before evaluation finished, as can happen in
1524             // EvaluatingPrivateDnsState).
1525             maybeStopCollectionAndSendMetrics();
1526             // Restart the metrics collection timers. Metrics will be stopped and sent when the
1527             // validation attempt finishes (as success, failure or portal), or if it is interrupted
1528             // (by being restarted or if NetworkMonitor stops).
1529             startMetricsCollection();
1530             if (mEvaluateAttempts >= BLAME_FOR_EVALUATION_ATTEMPTS) {
1531                 //Don't continue to blame UID forever.
1532                 TrafficStats.clearThreadStatsUid();
1533             }
1534 
1535             final int token = ++mProbeToken;
1536             final EvaluationThreadDeps deps = new EvaluationThreadDeps(mNetworkCapabilities);
1537             mThread = new Thread(() -> sendMessage(obtainMessage(CMD_PROBE_COMPLETE, token, 0,
1538                     isCaptivePortal(deps))));
1539             mThread.start();
1540         }
1541 
1542         @Override
processMessage(Message message)1543         public boolean processMessage(Message message) {
1544             switch (message.what) {
1545                 case CMD_PROBE_COMPLETE:
1546                     // Ensure that CMD_PROBE_COMPLETE from stale threads are ignored.
1547                     if (message.arg1 != mProbeToken) {
1548                         return HANDLED;
1549                     }
1550 
1551                     final CaptivePortalProbeResult probeResult =
1552                             (CaptivePortalProbeResult) message.obj;
1553                     mLastProbeTime = SystemClock.elapsedRealtime();
1554 
1555                     maybeWriteDataStallStats(probeResult);
1556 
1557                     if (probeResult.isSuccessful()) {
1558                         // Transit EvaluatingPrivateDnsState to get to Validated
1559                         // state (even if no Private DNS validation required).
1560                         transitionTo(mEvaluatingPrivateDnsState);
1561                     } else if (probeResult.isPortal()) {
1562                         mEvaluationState.reportEvaluationResult(NETWORK_VALIDATION_RESULT_INVALID,
1563                                 probeResult.redirectUrl);
1564                         mLastPortalProbeResult = probeResult;
1565                         transitionTo(mCaptivePortalState);
1566                     } else if (probeResult.isPartialConnectivity()) {
1567                         mEvaluationState.reportEvaluationResult(NETWORK_VALIDATION_RESULT_PARTIAL,
1568                                 null /* redirectUrl */);
1569                         maybeDisableHttpsProbing(mAcceptPartialConnectivity);
1570                         if (mAcceptPartialConnectivity) {
1571                             transitionTo(mEvaluatingPrivateDnsState);
1572                         } else {
1573                             transitionTo(mWaitingForNextProbeState);
1574                         }
1575                     } else {
1576                         logNetworkEvent(NetworkEvent.NETWORK_VALIDATION_FAILED);
1577                         mEvaluationState.reportEvaluationResult(NETWORK_VALIDATION_RESULT_INVALID,
1578                                 null /* redirectUrl */);
1579                         transitionTo(mWaitingForNextProbeState);
1580                     }
1581                     return HANDLED;
1582                 case EVENT_DNS_NOTIFICATION:
1583                 case EVENT_ACCEPT_PARTIAL_CONNECTIVITY:
1584                     // Leave the event to DefaultState.
1585                     return NOT_HANDLED;
1586                 default:
1587                     // Wait for probe result and defer events to next state by default.
1588                     deferMessage(message);
1589                     return HANDLED;
1590             }
1591         }
1592 
1593         @Override
exit()1594         public void exit() {
1595             if (mThread.isAlive()) {
1596                 mThread.interrupt();
1597             }
1598             mThread = null;
1599         }
1600     }
1601 
1602     // Being in the WaitingForNextProbeState indicates that evaluating probes failed and state is
1603     // transited from ProbingState. This ensures that the state machine is only in ProbingState
1604     // while a probe is in progress, not while waiting to perform the next probe. That allows
1605     // ProbingState to defer most messages until the probe is complete, which keeps the code simple
1606     // and matches the pre-Q behaviour where probes were a blocking operation performed on the state
1607     // machine thread.
1608     private class WaitingForNextProbeState extends State {
1609         @Override
enter()1610         public void enter() {
1611             // Send metrics for this evaluation attempt. Metrics collection (and its timers) will be
1612             // restarted when the next probe starts.
1613             maybeStopCollectionAndSendMetrics();
1614             scheduleNextProbe();
1615         }
1616 
scheduleNextProbe()1617         private void scheduleNextProbe() {
1618             final Message msg = obtainMessage(CMD_REEVALUATE, ++mReevaluateToken, 0);
1619             sendMessageDelayed(msg, mReevaluateDelayMs);
1620             mReevaluateDelayMs *= 2;
1621             if (mReevaluateDelayMs > MAX_REEVALUATE_DELAY_MS) {
1622                 mReevaluateDelayMs = MAX_REEVALUATE_DELAY_MS;
1623             }
1624         }
1625 
1626         @Override
processMessage(Message message)1627         public boolean processMessage(Message message) {
1628             return NOT_HANDLED;
1629         }
1630     }
1631 
1632     private final class EvaluatingBandwidthThread extends Thread {
1633         final int mThreadId;
1634 
EvaluatingBandwidthThread(int id)1635         EvaluatingBandwidthThread(int id) {
1636             mThreadId = id;
1637         }
1638 
1639         @Override
run()1640         public void run() {
1641             HttpURLConnection urlConnection = null;
1642             try {
1643                 final URL url = makeURL(mEvaluatingBandwidthUrl);
1644                 urlConnection = makeProbeConnection(url, true /* followRedirects */);
1645                 // In order to exclude the time of DNS lookup, send the delay message of timeout
1646                 // here.
1647                 sendMessageDelayed(CMD_BANDWIDTH_CHECK_TIMEOUT, mEvaluatingBandwidthTimeoutMs);
1648                 readContentFromDownloadUrl(urlConnection);
1649             } catch (InterruptedIOException e) {
1650                 // There is a timing issue that someone triggers the forcing reevaluation when
1651                 // executing the getInputStream(). The InterruptedIOException is thrown by
1652                 // Timeout#throwIfReached, it will reset the interrupt flag of Thread. So just
1653                 // return and wait for the bandwidth reevaluation, otherwise the
1654                 // CMD_BANDWIDTH_CHECK_COMPLETE will be sent.
1655                 validationLog("The thread is interrupted when executing the getInputStream(),"
1656                         + " return and wait for the bandwidth reevaluation");
1657                 return;
1658             } catch (IOException e) {
1659                 validationLog("Evaluating bandwidth failed: " + e + ", if the thread is not"
1660                         + " interrupted, transition to validated state directly to make sure user"
1661                         + " can use wifi normally.");
1662             } finally {
1663                 if (urlConnection != null) {
1664                     urlConnection.disconnect();
1665                 }
1666             }
1667             // Don't send CMD_BANDWIDTH_CHECK_COMPLETE if the IO is interrupted or timeout.
1668             // Only send CMD_BANDWIDTH_CHECK_COMPLETE when the download is finished normally.
1669             // Add a serial number for CMD_BANDWIDTH_CHECK_COMPLETE to prevent handling the obsolete
1670             // CMD_BANDWIDTH_CHECK_COMPLETE.
1671             if (!isInterrupted()) sendMessage(CMD_BANDWIDTH_CHECK_COMPLETE, mThreadId);
1672         }
1673 
readContentFromDownloadUrl(@onNull final HttpURLConnection conn)1674         private void readContentFromDownloadUrl(@NonNull final HttpURLConnection conn)
1675                 throws IOException {
1676             final byte[] buffer = new byte[1000];
1677             final InputStream is = conn.getInputStream();
1678             while (!isInterrupted() && is.read(buffer) > 0) { /* read again */ }
1679         }
1680     }
1681 
1682     private class EvaluatingBandwidthState extends State {
1683         private EvaluatingBandwidthThread mEvaluatingBandwidthThread;
1684         private int mRetryBandwidthDelayMs;
1685         private int mCurrentThreadId;
1686 
1687         @Override
enter()1688         public void enter() {
1689             mRetryBandwidthDelayMs = getResIntConfig(mContext,
1690                     R.integer.config_evaluating_bandwidth_min_retry_timer_ms,
1691                     INITIAL_REEVALUATE_DELAY_MS);
1692             sendMessage(CMD_EVALUATE_BANDWIDTH);
1693         }
1694 
1695         @Override
processMessage(Message msg)1696         public boolean processMessage(Message msg) {
1697             switch (msg.what) {
1698                 case CMD_EVALUATE_BANDWIDTH:
1699                     mCurrentThreadId = mNextEvaluatingBandwidthThreadId.getAndIncrement();
1700                     mEvaluatingBandwidthThread = new EvaluatingBandwidthThread(mCurrentThreadId);
1701                     mEvaluatingBandwidthThread.start();
1702                     break;
1703                 case CMD_BANDWIDTH_CHECK_COMPLETE:
1704                     // Only handle the CMD_BANDWIDTH_CHECK_COMPLETE which is sent by the newest
1705                     // EvaluatingBandwidthThread.
1706                     if (mCurrentThreadId == msg.arg1) {
1707                         mIsBandwidthCheckPassedOrIgnored = true;
1708                         transitionTo(mValidatedState);
1709                     }
1710                     break;
1711                 case CMD_BANDWIDTH_CHECK_TIMEOUT:
1712                     validationLog("Evaluating bandwidth timeout!");
1713                     mEvaluatingBandwidthThread.interrupt();
1714                     scheduleReevaluatingBandwidth();
1715                     break;
1716                 default:
1717                     return NOT_HANDLED;
1718             }
1719             return HANDLED;
1720         }
1721 
scheduleReevaluatingBandwidth()1722         private void scheduleReevaluatingBandwidth() {
1723             sendMessageDelayed(obtainMessage(CMD_EVALUATE_BANDWIDTH), mRetryBandwidthDelayMs);
1724             mRetryBandwidthDelayMs *= 2;
1725             if (mRetryBandwidthDelayMs > mMaxRetryTimerMs) {
1726                 mRetryBandwidthDelayMs = mMaxRetryTimerMs;
1727             }
1728         }
1729 
1730         @Override
exit()1731         public void exit() {
1732             mEvaluatingBandwidthThread.interrupt();
1733             removeMessages(CMD_EVALUATE_BANDWIDTH);
1734             removeMessages(CMD_BANDWIDTH_CHECK_TIMEOUT);
1735         }
1736     }
1737 
1738     // Limits the list of IP addresses returned by getAllByName or tried by openConnection to at
1739     // most one per address family. This ensures we only wait up to 20 seconds for TCP connections
1740     // to complete, regardless of how many IP addresses a host has.
1741     private static class OneAddressPerFamilyNetwork extends Network {
OneAddressPerFamilyNetwork(Network network)1742         OneAddressPerFamilyNetwork(Network network) {
1743             // Always bypass Private DNS.
1744             super(network.getPrivateDnsBypassingCopy());
1745         }
1746 
1747         @Override
getAllByName(String host)1748         public InetAddress[] getAllByName(String host) throws UnknownHostException {
1749             final List<InetAddress> addrs = Arrays.asList(super.getAllByName(host));
1750 
1751             // Ensure the address family of the first address is tried first.
1752             LinkedHashMap<Class, InetAddress> addressByFamily = new LinkedHashMap<>();
1753             addressByFamily.put(addrs.get(0).getClass(), addrs.get(0));
1754             Collections.shuffle(addrs);
1755 
1756             for (InetAddress addr : addrs) {
1757                 addressByFamily.put(addr.getClass(), addr);
1758             }
1759 
1760             return addressByFamily.values().toArray(new InetAddress[addressByFamily.size()]);
1761         }
1762     }
1763 
1764     @VisibleForTesting
onlyWifiTransport()1765     boolean onlyWifiTransport() {
1766         int[] transportTypes = mNetworkCapabilities.getTransportTypes();
1767         return transportTypes.length == 1
1768                 && transportTypes[0] == NetworkCapabilities.TRANSPORT_WIFI;
1769     }
1770 
1771     @VisibleForTesting
needEvaluatingBandwidth()1772     boolean needEvaluatingBandwidth() {
1773         if (mIsBandwidthCheckPassedOrIgnored
1774                 || TextUtils.isEmpty(mEvaluatingBandwidthUrl)
1775                 || !mNetworkCapabilities.hasCapability(NET_CAPABILITY_NOT_METERED)
1776                 || !onlyWifiTransport()) {
1777             return false;
1778         }
1779 
1780         return true;
1781     }
1782 
getIsCaptivePortalCheckEnabled()1783     private boolean getIsCaptivePortalCheckEnabled() {
1784         String symbol = CAPTIVE_PORTAL_MODE;
1785         int defaultValue = CAPTIVE_PORTAL_MODE_PROMPT;
1786         int mode = mDependencies.getSetting(mContext, symbol, defaultValue);
1787         return mode != CAPTIVE_PORTAL_MODE_IGNORE;
1788     }
1789 
getIsPrivateIpNoInternetEnabled()1790     private boolean getIsPrivateIpNoInternetEnabled() {
1791         return mDependencies.isFeatureEnabled(mContext, DNS_PROBE_PRIVATE_IP_NO_INTERNET_VERSION)
1792                 || mContext.getResources().getBoolean(
1793                         R.bool.config_force_dns_probe_private_ip_no_internet);
1794     }
1795 
getUseHttpsValidation()1796     private boolean getUseHttpsValidation() {
1797         return mDependencies.getDeviceConfigPropertyInt(NAMESPACE_CONNECTIVITY,
1798                 CAPTIVE_PORTAL_USE_HTTPS, 1) == 1;
1799     }
1800 
1801     @Nullable
getMccFromCellInfo(final CellInfo cell)1802     private String getMccFromCellInfo(final CellInfo cell) {
1803         if (cell instanceof CellInfoGsm) {
1804             return ((CellInfoGsm) cell).getCellIdentity().getMccString();
1805         } else if (cell instanceof CellInfoLte) {
1806             return ((CellInfoLte) cell).getCellIdentity().getMccString();
1807         } else if (cell instanceof CellInfoWcdma) {
1808             return ((CellInfoWcdma) cell).getCellIdentity().getMccString();
1809         } else if (cell instanceof CellInfoTdscdma) {
1810             return ((CellInfoTdscdma) cell).getCellIdentity().getMccString();
1811         } else if (cell instanceof CellInfoNr) {
1812             return ((CellIdentityNr) ((CellInfoNr) cell).getCellIdentity()).getMccString();
1813         } else {
1814             return null;
1815         }
1816     }
1817 
1818     /**
1819      * Return location mcc.
1820      */
1821     @VisibleForTesting
1822     @Nullable
getLocationMcc()1823     protected String getLocationMcc() {
1824         // Adding this check is because the new permission won't be granted by mainline update,
1825         // the new permission only be granted by OTA for current design. Tracking: b/145774617.
1826         if (mContext.checkPermission(android.Manifest.permission.ACCESS_FINE_LOCATION,
1827                 Process.myPid(), Process.myUid())
1828                 == PackageManager.PERMISSION_DENIED) {
1829             log("getLocationMcc : NetworkStack does not hold ACCESS_FINE_LOCATION");
1830             return null;
1831         }
1832         try {
1833             final List<CellInfo> cells = mTelephonyManager.getAllCellInfo();
1834             if (cells == null) return null;
1835             final Map<String, Integer> countryCodeMap = new HashMap<>();
1836             int maxCount = 0;
1837             for (final CellInfo cell : cells) {
1838                 final String mcc = getMccFromCellInfo(cell);
1839                 if (mcc != null) {
1840                     final int count = countryCodeMap.getOrDefault(mcc, 0) + 1;
1841                     countryCodeMap.put(mcc, count);
1842                 }
1843             }
1844             // Return the MCC which occurs most.
1845             if (countryCodeMap.size() <= 0) return null;
1846             return Collections.max(countryCodeMap.entrySet(),
1847                     (e1, e2) -> e1.getValue().compareTo(e2.getValue())).getKey();
1848         } catch (SecurityException e) {
1849             log("Permission is not granted:" + e);
1850             return null;
1851         }
1852     }
1853 
1854     /**
1855      * Return a matched MccMncOverrideInfo if carrier id and sim mccmnc are matching a record in
1856      * sCarrierIdToMccMnc.
1857      */
1858     @VisibleForTesting
1859     @Nullable
getMccMncOverrideInfo()1860     MccMncOverrideInfo getMccMncOverrideInfo() {
1861         final int carrierId = mTelephonyManager.getSimCarrierId();
1862         return sCarrierIdToMccMnc.get(carrierId);
1863     }
1864 
getContextByMccMnc(final int mcc, final int mnc)1865     private Context getContextByMccMnc(final int mcc, final int mnc) {
1866         final Configuration config = mContext.getResources().getConfiguration();
1867         if (mcc != UNSET_MCC_OR_MNC) config.mcc = mcc;
1868         if (mnc != UNSET_MCC_OR_MNC) config.mnc = mnc;
1869         return mContext.createConfigurationContext(config);
1870     }
1871 
1872     @VisibleForTesting
getCustomizedContextOrDefault()1873     protected Context getCustomizedContextOrDefault() {
1874         // Return customized context if carrier id can match a record in sCarrierIdToMccMnc.
1875         final MccMncOverrideInfo overrideInfo = getMccMncOverrideInfo();
1876         if (overrideInfo != null) {
1877             return getContextByMccMnc(overrideInfo.mcc, overrideInfo.mnc);
1878         }
1879 
1880         // Use neighbor mcc feature only works when the config_no_sim_card_uses_neighbor_mcc is
1881         // true and there is no sim card inserted.
1882         final boolean useNeighborResource =
1883                 getResBooleanConfig(mContext, R.bool.config_no_sim_card_uses_neighbor_mcc, false);
1884         if (!useNeighborResource
1885                 || TelephonyManager.SIM_STATE_READY == mTelephonyManager.getSimState()) {
1886             return mContext;
1887         }
1888 
1889         final String mcc = getLocationMcc();
1890         if (TextUtils.isEmpty(mcc)) {
1891             return mContext;
1892         }
1893 
1894         return getContextByMccMnc(Integer.parseInt(mcc), UNSET_MCC_OR_MNC);
1895     }
1896 
1897     @Nullable
getTestUrl(@onNull String key)1898     private String getTestUrl(@NonNull String key) {
1899         final String strExpiration = mDependencies.getDeviceConfigProperty(NAMESPACE_CONNECTIVITY,
1900                 TEST_URL_EXPIRATION_TIME, null);
1901         if (strExpiration == null) return null;
1902 
1903         final long expTime;
1904         try {
1905             expTime = Long.parseUnsignedLong(strExpiration);
1906         } catch (NumberFormatException e) {
1907             loge("Invalid test URL expiration time format", e);
1908             return null;
1909         }
1910 
1911         final long now = System.currentTimeMillis();
1912         if (expTime < now || (expTime - now) > TEST_URL_EXPIRATION_MS) return null;
1913 
1914         return mDependencies.getDeviceConfigProperty(NAMESPACE_CONNECTIVITY,
1915                 key, null /* defaultValue */);
1916     }
1917 
getCaptivePortalServerHttpsUrl()1918     private String getCaptivePortalServerHttpsUrl() {
1919         final String testUrl = getTestUrl(TEST_CAPTIVE_PORTAL_HTTPS_URL);
1920         if (isValidTestUrl(testUrl)) return testUrl;
1921         final Context targetContext = getCustomizedContextOrDefault();
1922         return getSettingFromResource(targetContext,
1923                 R.string.config_captive_portal_https_url, CAPTIVE_PORTAL_HTTPS_URL,
1924                 targetContext.getResources().getString(R.string.default_captive_portal_https_url));
1925     }
1926 
isValidTestUrl(@ullable String url)1927     private static boolean isValidTestUrl(@Nullable String url) {
1928         if (TextUtils.isEmpty(url)) return false;
1929 
1930         try {
1931             // Only accept test URLs on localhost
1932             return Uri.parse(url).getHost().equals("localhost");
1933         } catch (Throwable e) {
1934             Log.wtf(TAG, "Error parsing test URL", e);
1935             return false;
1936         }
1937     }
1938 
getDnsProbeTimeout()1939     private int getDnsProbeTimeout() {
1940         return getIntSetting(mContext, R.integer.config_captive_portal_dns_probe_timeout,
1941                 CONFIG_CAPTIVE_PORTAL_DNS_PROBE_TIMEOUT, DEFAULT_CAPTIVE_PORTAL_DNS_PROBE_TIMEOUT);
1942     }
1943 
1944     /**
1945      * Gets an integer setting from resources or device config
1946      *
1947      * configResource is used if set, followed by device config if set, followed by defaultValue.
1948      * If none of these are set then an exception is thrown.
1949      *
1950      * TODO: move to a common location such as a ConfigUtils class.
1951      * TODO(b/130324939): test that the resources can be overlayed by an RRO package.
1952      */
1953     @VisibleForTesting
getIntSetting(@onNull final Context context, @StringRes int configResource, @NonNull String symbol, int defaultValue)1954     int getIntSetting(@NonNull final Context context, @StringRes int configResource,
1955             @NonNull String symbol, int defaultValue) {
1956         final Resources res = context.getResources();
1957         try {
1958             return res.getInteger(configResource);
1959         } catch (Resources.NotFoundException e) {
1960             return mDependencies.getDeviceConfigPropertyInt(NAMESPACE_CONNECTIVITY,
1961                     symbol, defaultValue);
1962         }
1963     }
1964 
1965     @VisibleForTesting
getResBooleanConfig(@onNull final Context context, @BoolRes int configResource, final boolean defaultValue)1966     boolean getResBooleanConfig(@NonNull final Context context,
1967             @BoolRes int configResource, final boolean defaultValue) {
1968         final Resources res = context.getResources();
1969         try {
1970             return res.getBoolean(configResource);
1971         } catch (Resources.NotFoundException e) {
1972             return defaultValue;
1973         }
1974     }
1975 
1976     /**
1977      * Gets integer config from resources.
1978      */
1979     @VisibleForTesting
getResIntConfig(@onNull final Context context, @IntegerRes final int configResource, final int defaultValue)1980     int getResIntConfig(@NonNull final Context context,
1981             @IntegerRes final int configResource, final int defaultValue) {
1982         final Resources res = context.getResources();
1983         try {
1984             return res.getInteger(configResource);
1985         } catch (Resources.NotFoundException e) {
1986             return defaultValue;
1987         }
1988     }
1989 
1990     /**
1991      * Gets string config from resources.
1992      */
1993     @VisibleForTesting
getResStringConfig(@onNull final Context context, @StringRes final int configResource, @Nullable final String defaultValue)1994     String getResStringConfig(@NonNull final Context context,
1995             @StringRes final int configResource, @Nullable final String defaultValue) {
1996         final Resources res = context.getResources();
1997         try {
1998             return res.getString(configResource);
1999         } catch (Resources.NotFoundException e) {
2000             return defaultValue;
2001         }
2002     }
2003 
2004     /**
2005      * Get the captive portal server HTTP URL that is configured on the device.
2006      *
2007      * NetworkMonitor does not use {@link ConnectivityManager#getCaptivePortalServerUrl()} as
2008      * it has its own updatable strategies to detect captive portals. The framework only advises
2009      * on one URL that can be used, while NetworkMonitor may implement more complex logic.
2010      */
getCaptivePortalServerHttpUrl()2011     public String getCaptivePortalServerHttpUrl() {
2012         final String testUrl = getTestUrl(TEST_CAPTIVE_PORTAL_HTTP_URL);
2013         if (isValidTestUrl(testUrl)) return testUrl;
2014         final Context targetContext = getCustomizedContextOrDefault();
2015         return getSettingFromResource(targetContext,
2016                 R.string.config_captive_portal_http_url, CAPTIVE_PORTAL_HTTP_URL,
2017                 targetContext.getResources().getString(R.string.default_captive_portal_http_url));
2018     }
2019 
getConsecutiveDnsTimeoutThreshold()2020     private int getConsecutiveDnsTimeoutThreshold() {
2021         return mDependencies.getDeviceConfigPropertyInt(NAMESPACE_CONNECTIVITY,
2022                 CONFIG_DATA_STALL_CONSECUTIVE_DNS_TIMEOUT_THRESHOLD,
2023                 DEFAULT_CONSECUTIVE_DNS_TIMEOUT_THRESHOLD);
2024     }
2025 
getDataStallMinEvaluateTime()2026     private int getDataStallMinEvaluateTime() {
2027         return mDependencies.getDeviceConfigPropertyInt(NAMESPACE_CONNECTIVITY,
2028                 CONFIG_DATA_STALL_MIN_EVALUATE_INTERVAL,
2029                 DEFAULT_DATA_STALL_MIN_EVALUATE_TIME_MS);
2030     }
2031 
getDataStallValidDnsTimeThreshold()2032     private int getDataStallValidDnsTimeThreshold() {
2033         return mDependencies.getDeviceConfigPropertyInt(NAMESPACE_CONNECTIVITY,
2034                 CONFIG_DATA_STALL_VALID_DNS_TIME_THRESHOLD,
2035                 DEFAULT_DATA_STALL_VALID_DNS_TIME_THRESHOLD_MS);
2036     }
2037 
2038     @VisibleForTesting
getDataStallEvaluationType()2039     int getDataStallEvaluationType() {
2040         return mDependencies.getDeviceConfigPropertyInt(NAMESPACE_CONNECTIVITY,
2041                 CONFIG_DATA_STALL_EVALUATION_TYPE,
2042                 DEFAULT_DATA_STALL_EVALUATION_TYPES);
2043     }
2044 
getTcpPollingInterval()2045     private int getTcpPollingInterval() {
2046         return mDependencies.getDeviceConfigPropertyInt(NAMESPACE_CONNECTIVITY,
2047                 CONFIG_DATA_STALL_TCP_POLLING_INTERVAL,
2048                 DEFAULT_TCP_POLLING_INTERVAL_MS);
2049     }
2050 
2051     @VisibleForTesting
makeCaptivePortalFallbackUrls()2052     URL[] makeCaptivePortalFallbackUrls() {
2053         try {
2054             final String firstUrl = mDependencies.getSetting(mContext, CAPTIVE_PORTAL_FALLBACK_URL,
2055                     null);
2056             final URL[] settingProviderUrls =
2057                 combineCaptivePortalUrls(firstUrl, CAPTIVE_PORTAL_OTHER_FALLBACK_URLS);
2058             return getProbeUrlArrayConfig(settingProviderUrls,
2059                     R.array.config_captive_portal_fallback_urls,
2060                     R.array.default_captive_portal_fallback_urls,
2061                     this::makeURL);
2062         } catch (Exception e) {
2063             // Don't let a misconfiguration bootloop the system.
2064             Log.e(TAG, "Error parsing configured fallback URLs", e);
2065             return new URL[0];
2066         }
2067     }
2068 
makeCaptivePortalFallbackProbeSpecs()2069     private CaptivePortalProbeSpec[] makeCaptivePortalFallbackProbeSpecs() {
2070         try {
2071             final String settingsValue = mDependencies.getDeviceConfigProperty(
2072                     NAMESPACE_CONNECTIVITY, CAPTIVE_PORTAL_FALLBACK_PROBE_SPECS, null);
2073 
2074             final CaptivePortalProbeSpec[] emptySpecs = new CaptivePortalProbeSpec[0];
2075             final CaptivePortalProbeSpec[] providerValue = TextUtils.isEmpty(settingsValue)
2076                     ? emptySpecs
2077                     : parseCaptivePortalProbeSpecs(settingsValue).toArray(emptySpecs);
2078 
2079             return getProbeUrlArrayConfig(providerValue,
2080                     R.array.config_captive_portal_fallback_probe_specs,
2081                     DEFAULT_CAPTIVE_PORTAL_FALLBACK_PROBE_SPECS,
2082                     CaptivePortalProbeSpec::parseSpecOrNull);
2083         } catch (Exception e) {
2084             // Don't let a misconfiguration bootloop the system.
2085             Log.e(TAG, "Error parsing configured fallback probe specs", e);
2086             return null;
2087         }
2088     }
2089 
makeCaptivePortalHttpsUrls()2090     private URL[] makeCaptivePortalHttpsUrls() {
2091         final String firstUrl = getCaptivePortalServerHttpsUrl();
2092         try {
2093             final URL[] settingProviderUrls =
2094                 combineCaptivePortalUrls(firstUrl, CAPTIVE_PORTAL_OTHER_HTTPS_URLS);
2095             // firstUrl will at least be default configuration, so default value in
2096             // getProbeUrlArrayConfig is actually never used.
2097             return getProbeUrlArrayConfig(settingProviderUrls,
2098                     R.array.config_captive_portal_https_urls,
2099                     DEFAULT_CAPTIVE_PORTAL_HTTPS_URLS, this::makeURL);
2100         } catch (Exception e) {
2101             // Don't let a misconfiguration bootloop the system.
2102             Log.e(TAG, "Error parsing configured https URLs", e);
2103             // Ensure URL aligned with legacy configuration.
2104             return new URL[]{makeURL(firstUrl)};
2105         }
2106     }
2107 
makeCaptivePortalHttpUrls()2108     private URL[] makeCaptivePortalHttpUrls() {
2109         final String firstUrl = getCaptivePortalServerHttpUrl();
2110         try {
2111             final URL[] settingProviderUrls =
2112                     combineCaptivePortalUrls(firstUrl, CAPTIVE_PORTAL_OTHER_HTTP_URLS);
2113             // firstUrl will at least be default configuration, so default value in
2114             // getProbeUrlArrayConfig is actually never used.
2115             return getProbeUrlArrayConfig(settingProviderUrls,
2116                     R.array.config_captive_portal_http_urls,
2117                     DEFAULT_CAPTIVE_PORTAL_HTTP_URLS, this::makeURL);
2118         } catch (Exception e) {
2119             // Don't let a misconfiguration bootloop the system.
2120             Log.e(TAG, "Error parsing configured http URLs", e);
2121             // Ensure URL aligned with legacy configuration.
2122             return new URL[]{makeURL(firstUrl)};
2123         }
2124     }
2125 
combineCaptivePortalUrls(final String firstUrl, final String propertyName)2126     private URL[] combineCaptivePortalUrls(final String firstUrl, final String propertyName) {
2127         if (TextUtils.isEmpty(firstUrl)) return new URL[0];
2128 
2129         final String otherUrls = mDependencies.getDeviceConfigProperty(
2130                 NAMESPACE_CONNECTIVITY, propertyName, "");
2131         // otherUrls may be empty, but .split() ignores trailing empty strings
2132         final String separator = ",";
2133         final String[] urls = (firstUrl + separator + otherUrls).split(separator);
2134         return convertStrings(urls, this::makeURL, new URL[0]);
2135     }
2136 
2137     /**
2138      * Read a setting from a resource or the settings provider.
2139      *
2140      * <p>The configuration resource is prioritized, then the provider value.
2141      * @param context The context
2142      * @param configResource The resource id for the configuration parameter
2143      * @param symbol The symbol in the settings provider
2144      * @param defaultValue The default value
2145      * @return The best available value
2146      */
2147     @Nullable
getSettingFromResource(@onNull final Context context, @StringRes int configResource, @NonNull String symbol, @NonNull String defaultValue)2148     private String getSettingFromResource(@NonNull final Context context,
2149             @StringRes int configResource, @NonNull String symbol, @NonNull String defaultValue) {
2150         final Resources res = context.getResources();
2151         String setting = res.getString(configResource);
2152 
2153         if (!TextUtils.isEmpty(setting)) return setting;
2154 
2155         setting = mDependencies.getSetting(context, symbol, null);
2156 
2157         if (!TextUtils.isEmpty(setting)) return setting;
2158 
2159         return defaultValue;
2160     }
2161 
2162     /**
2163      * Get an array configuration from resources or the settings provider.
2164      *
2165      * <p>The configuration resource is prioritized, then the provider values, then the default
2166      * resource values.
2167      * @param providerValue Values obtained from the setting provider.
2168      * @param configResId ID of the configuration resource.
2169      * @param defaultResId ID of the default resource.
2170      * @param resourceConverter Converter from the resource strings to stored setting class. Null
2171      *                          return values are ignored.
2172      */
getProbeUrlArrayConfig(@onNull T[] providerValue, @ArrayRes int configResId, @ArrayRes int defaultResId, @NonNull Function<String, T> resourceConverter)2173     private <T> T[] getProbeUrlArrayConfig(@NonNull T[] providerValue, @ArrayRes int configResId,
2174             @ArrayRes int defaultResId, @NonNull Function<String, T> resourceConverter) {
2175         final Resources res = getCustomizedContextOrDefault().getResources();
2176         return getProbeUrlArrayConfig(providerValue, configResId, res.getStringArray(defaultResId),
2177                 resourceConverter);
2178     }
2179 
2180     /**
2181      * Get an array configuration from resources or the settings provider.
2182      *
2183      * <p>The configuration resource is prioritized, then the provider values, then the default
2184      * resource values.
2185      * @param providerValue Values obtained from the setting provider.
2186      * @param configResId ID of the configuration resource.
2187      * @param defaultConfig Values of default configuration.
2188      * @param resourceConverter Converter from the resource strings to stored setting class. Null
2189      *                          return values are ignored.
2190      */
getProbeUrlArrayConfig(@onNull T[] providerValue, @ArrayRes int configResId, String[] defaultConfig, @NonNull Function<String, T> resourceConverter)2191     private <T> T[] getProbeUrlArrayConfig(@NonNull T[] providerValue, @ArrayRes int configResId,
2192             String[] defaultConfig, @NonNull Function<String, T> resourceConverter) {
2193         final Resources res = getCustomizedContextOrDefault().getResources();
2194         String[] configValue = res.getStringArray(configResId);
2195 
2196         if (configValue.length == 0) {
2197             if (providerValue.length > 0) {
2198                 return providerValue;
2199             }
2200 
2201             configValue = defaultConfig;
2202         }
2203 
2204         return convertStrings(configValue, resourceConverter, Arrays.copyOf(providerValue, 0));
2205     }
2206 
2207     /**
2208      * Convert a String array to an array of some other type using the specified converter.
2209      *
2210      * <p>Any null value, or value for which the converter throws a {@link RuntimeException}, will
2211      * not be added to the output array, so the output array may be smaller than the input.
2212      */
convertStrings( @onNull String[] strings, Function<String, T> converter, T[] emptyArray)2213     private <T> T[] convertStrings(
2214             @NonNull String[] strings, Function<String, T> converter, T[] emptyArray) {
2215         final ArrayList<T> convertedValues = new ArrayList<>(strings.length);
2216         for (String configString : strings) {
2217             T convertedValue = null;
2218             try {
2219                 convertedValue = converter.apply(configString);
2220             } catch (Exception e) {
2221                 Log.e(TAG, "Error parsing configuration", e);
2222                 // Fall through
2223             }
2224             if (convertedValue != null) {
2225                 convertedValues.add(convertedValue);
2226             }
2227         }
2228         return convertedValues.toArray(emptyArray);
2229     }
2230 
getCaptivePortalUserAgent()2231     private String getCaptivePortalUserAgent() {
2232         return mDependencies.getDeviceConfigProperty(NAMESPACE_CONNECTIVITY,
2233                 CAPTIVE_PORTAL_USER_AGENT, DEFAULT_USER_AGENT);
2234     }
2235 
nextFallbackUrl()2236     private URL nextFallbackUrl() {
2237         if (mCaptivePortalFallbackUrls.length == 0) {
2238             return null;
2239         }
2240         int idx = Math.abs(mNextFallbackUrlIndex) % mCaptivePortalFallbackUrls.length;
2241         mNextFallbackUrlIndex += mRandom.nextInt(); // randomly change url without memory.
2242         return mCaptivePortalFallbackUrls[idx];
2243     }
2244 
nextFallbackSpec()2245     private CaptivePortalProbeSpec nextFallbackSpec() {
2246         if (isEmpty(mCaptivePortalFallbackSpecs)) {
2247             return null;
2248         }
2249         // Randomly change spec without memory. Also randomize the first attempt.
2250         final int idx = Math.abs(mRandom.nextInt()) % mCaptivePortalFallbackSpecs.length;
2251         return mCaptivePortalFallbackSpecs[idx];
2252     }
2253 
2254     /**
2255      * Parameters that can be accessed by the evaluation thread in a thread-safe way.
2256      *
2257      * Parameters such as LinkProperties and NetworkCapabilities cannot be accessed by the
2258      * evaluation thread directly, as they are managed in the state machine thread and not
2259      * synchronized. This class provides a copy of the required data that is not modified and can be
2260      * used safely by the evaluation thread.
2261      */
2262     private static class EvaluationThreadDeps {
2263         // TODO: add parameters that are accessed in a non-thread-safe way from the evaluation
2264         // thread (read from LinkProperties, NetworkCapabilities, useHttps, validationStage)
2265         private final boolean mIsTestNetwork;
2266 
EvaluationThreadDeps(NetworkCapabilities nc)2267         EvaluationThreadDeps(NetworkCapabilities nc) {
2268             this.mIsTestNetwork = nc.hasTransport(TRANSPORT_TEST);
2269         }
2270     }
2271 
isCaptivePortal(EvaluationThreadDeps deps)2272     private CaptivePortalProbeResult isCaptivePortal(EvaluationThreadDeps deps) {
2273         if (!mIsCaptivePortalCheckEnabled) {
2274             validationLog("Validation disabled.");
2275             return CaptivePortalProbeResult.success(CaptivePortalProbeResult.PROBE_UNKNOWN);
2276         }
2277 
2278         URL pacUrl = null;
2279         final URL[] httpsUrls = mCaptivePortalHttpsUrls;
2280         final URL[] httpUrls = mCaptivePortalHttpUrls;
2281 
2282         // On networks with a PAC instead of fetching a URL that should result in a 204
2283         // response, we instead simply fetch the PAC script.  This is done for a few reasons:
2284         // 1. At present our PAC code does not yet handle multiple PACs on multiple networks
2285         //    until something like https://android-review.googlesource.com/#/c/115180/ lands.
2286         //    Network.openConnection() will ignore network-specific PACs and instead fetch
2287         //    using NO_PROXY.  If a PAC is in place, the only fetch we know will succeed with
2288         //    NO_PROXY is the fetch of the PAC itself.
2289         // 2. To proxy the generate_204 fetch through a PAC would require a number of things
2290         //    happen before the fetch can commence, namely:
2291         //        a) the PAC script be fetched
2292         //        b) a PAC script resolver service be fired up and resolve the captive portal
2293         //           server.
2294         //    Network validation could be delayed until these prerequisities are satisifed or
2295         //    could simply be left to race them.  Neither is an optimal solution.
2296         // 3. PAC scripts are sometimes used to block or restrict Internet access and may in
2297         //    fact block fetching of the generate_204 URL which would lead to false negative
2298         //    results for network validation.
2299         final ProxyInfo proxyInfo = mLinkProperties.getHttpProxy();
2300         if (proxyInfo != null && !Uri.EMPTY.equals(proxyInfo.getPacFileUrl())) {
2301             pacUrl = makeURL(proxyInfo.getPacFileUrl().toString());
2302             if (pacUrl == null) {
2303                 return CaptivePortalProbeResult.failed(CaptivePortalProbeResult.PROBE_UNKNOWN);
2304             }
2305         }
2306 
2307         if ((pacUrl == null) && (httpUrls.length == 0 || httpsUrls.length == 0
2308                 || httpUrls[0] == null || httpsUrls[0] == null)) {
2309             return CaptivePortalProbeResult.failed(CaptivePortalProbeResult.PROBE_UNKNOWN);
2310         }
2311 
2312         long startTime = SystemClock.elapsedRealtime();
2313 
2314         final CaptivePortalProbeResult result;
2315         if (pacUrl != null) {
2316             result = sendDnsAndHttpProbes(null, pacUrl, ValidationProbeEvent.PROBE_PAC);
2317             reportHttpProbeResult(NETWORK_VALIDATION_PROBE_HTTP, result);
2318         } else if (mUseHttps && httpsUrls.length == 1 && httpUrls.length == 1) {
2319             // Probe results are reported inside sendHttpAndHttpsParallelWithFallbackProbes.
2320             result = sendHttpAndHttpsParallelWithFallbackProbes(deps, proxyInfo,
2321                     httpsUrls[0], httpUrls[0]);
2322         } else if (mUseHttps) {
2323             // Support result aggregation from multiple Urls.
2324             result = sendMultiParallelHttpAndHttpsProbes(deps, proxyInfo, httpsUrls, httpUrls);
2325         } else {
2326             result = sendDnsAndHttpProbes(proxyInfo, httpUrls[0], ValidationProbeEvent.PROBE_HTTP);
2327             reportHttpProbeResult(NETWORK_VALIDATION_PROBE_HTTP, result);
2328         }
2329 
2330         long endTime = SystemClock.elapsedRealtime();
2331 
2332         sendNetworkConditionsBroadcast(true /* response received */,
2333                 result.isPortal() /* isCaptivePortal */,
2334                 startTime, endTime);
2335 
2336         log("isCaptivePortal: isSuccessful()=" + result.isSuccessful()
2337                 + " isPortal()=" + result.isPortal()
2338                 + " RedirectUrl=" + result.redirectUrl
2339                 + " isPartialConnectivity()=" + result.isPartialConnectivity()
2340                 + " Time=" + (endTime - startTime) + "ms");
2341 
2342         return result;
2343     }
2344 
2345     /**
2346      * Do a DNS resolution and URL fetch on a known web server to see if we get the data we expect.
2347      * @return a CaptivePortalProbeResult inferred from the HTTP response.
2348      */
sendDnsAndHttpProbes(ProxyInfo proxy, URL url, int probeType)2349     private CaptivePortalProbeResult sendDnsAndHttpProbes(ProxyInfo proxy, URL url, int probeType) {
2350         // Pre-resolve the captive portal server host so we can log it.
2351         // Only do this if HttpURLConnection is about to, to avoid any potentially
2352         // unnecessary resolution.
2353         final String host = (proxy != null) ? proxy.getHost() : url.getHost();
2354         // This method cannot safely report probe results because it might not be running on the
2355         // state machine thread. Reporting results here would cause races and potentially send
2356         // information to callers that does not make sense because the state machine has already
2357         // changed state.
2358         final InetAddress[] resolvedAddr = sendDnsProbe(host);
2359         // The private IP logic only applies to captive portal detection (the HTTP probe), not
2360         // network validation (the HTTPS probe, which would likely fail anyway) or the PAC probe.
2361         if (mPrivateIpNoInternetEnabled && probeType == ValidationProbeEvent.PROBE_HTTP
2362                 && (proxy == null) && hasPrivateIpAddress(resolvedAddr)) {
2363             recordProbeEventMetrics(NetworkValidationMetrics.probeTypeToEnum(probeType),
2364                     0 /* latency */, ProbeResult.PR_PRIVATE_IP_DNS, null /* capportData */);
2365             return CaptivePortalProbeResult.PRIVATE_IP;
2366         }
2367         return sendHttpProbe(url, probeType, null);
2368     }
2369 
2370     /** Do a DNS lookup for the given server, or throw UnknownHostException after timeoutMs */
2371     @VisibleForTesting
sendDnsProbeWithTimeout(String host, int timeoutMs)2372     protected InetAddress[] sendDnsProbeWithTimeout(String host, int timeoutMs)
2373                 throws UnknownHostException {
2374         return DnsUtils.getAllByName(mDependencies.getDnsResolver(), mCleartextDnsNetwork, host,
2375                 TYPE_ADDRCONFIG, FLAG_EMPTY, timeoutMs,
2376                 str -> validationLog(ValidationProbeEvent.PROBE_DNS, host, str));
2377     }
2378 
2379     /** Do a DNS resolution of the given server. */
sendDnsProbe(String host)2380     private InetAddress[] sendDnsProbe(String host) {
2381         if (TextUtils.isEmpty(host)) {
2382             return null;
2383         }
2384 
2385         final Stopwatch watch = new Stopwatch().start();
2386         int result;
2387         InetAddress[] addresses;
2388         try {
2389             addresses = sendDnsProbeWithTimeout(host, getDnsProbeTimeout());
2390             result = ValidationProbeEvent.DNS_SUCCESS;
2391         } catch (UnknownHostException e) {
2392             addresses = null;
2393             result = ValidationProbeEvent.DNS_FAILURE;
2394         }
2395         final long latency = watch.stop();
2396         recordProbeEventMetrics(ProbeType.PT_DNS, latency,
2397                 (result == ValidationProbeEvent.DNS_SUCCESS) ? ProbeResult.PR_SUCCESS :
2398                 ProbeResult.PR_FAILURE, null /* capportData */);
2399         logValidationProbe(latency, ValidationProbeEvent.PROBE_DNS, result);
2400         return addresses;
2401     }
2402 
2403     /**
2404      * Check if any of the provided IP addresses include a private IP.
2405      * @return true if an IP address is private.
2406      */
hasPrivateIpAddress(@ullable InetAddress[] addresses)2407     private static boolean hasPrivateIpAddress(@Nullable InetAddress[] addresses) {
2408         if (addresses == null) {
2409             return false;
2410         }
2411         for (InetAddress address : addresses) {
2412             if (address.isLinkLocalAddress() || address.isSiteLocalAddress()
2413                     || isIPv6ULA(address)) {
2414                 return true;
2415             }
2416         }
2417         return false;
2418     }
2419 
2420     /**
2421      * Do a URL fetch on a known web server to see if we get the data we expect.
2422      * @return a CaptivePortalProbeResult inferred from the HTTP response.
2423      */
2424     @VisibleForTesting
sendHttpProbe(URL url, int probeType, @Nullable CaptivePortalProbeSpec probeSpec)2425     protected CaptivePortalProbeResult sendHttpProbe(URL url, int probeType,
2426             @Nullable CaptivePortalProbeSpec probeSpec) {
2427         HttpURLConnection urlConnection = null;
2428         int httpResponseCode = CaptivePortalProbeResult.FAILED_CODE;
2429         String redirectUrl = null;
2430         final Stopwatch probeTimer = new Stopwatch().start();
2431         final int oldTag = TrafficStats.getAndSetThreadStatsTag(
2432                 TrafficStatsConstants.TAG_SYSTEM_PROBE);
2433         try {
2434             // Follow redirects for PAC probes as such probes verify connectivity by fetching the
2435             // PAC proxy file, which may be configured behind a redirect.
2436             final boolean followRedirect = probeType == ValidationProbeEvent.PROBE_PAC;
2437             urlConnection = makeProbeConnection(url, followRedirect);
2438             // cannot read request header after connection
2439             String requestHeader = urlConnection.getRequestProperties().toString();
2440 
2441             // Time how long it takes to get a response to our request
2442             long requestTimestamp = SystemClock.elapsedRealtime();
2443 
2444             httpResponseCode = urlConnection.getResponseCode();
2445             redirectUrl = urlConnection.getHeaderField("location");
2446 
2447             // Time how long it takes to get a response to our request
2448             long responseTimestamp = SystemClock.elapsedRealtime();
2449 
2450             validationLog(probeType, url, "time=" + (responseTimestamp - requestTimestamp) + "ms"
2451                     + " ret=" + httpResponseCode
2452                     + " request=" + requestHeader
2453                     + " headers=" + urlConnection.getHeaderFields());
2454             // NOTE: We may want to consider an "HTTP/1.0 204" response to be a captive
2455             // portal.  The only example of this seen so far was a captive portal.  For
2456             // the time being go with prior behavior of assuming it's not a captive
2457             // portal.  If it is considered a captive portal, a different sign-in URL
2458             // is needed (i.e. can't browse a 204).  This could be the result of an HTTP
2459             // proxy server.
2460             if (httpResponseCode == 200) {
2461                 long contentLength = urlConnection.getContentLengthLong();
2462                 if (probeType == ValidationProbeEvent.PROBE_PAC) {
2463                     validationLog(
2464                             probeType, url, "PAC fetch 200 response interpreted as 204 response.");
2465                     httpResponseCode = CaptivePortalProbeResult.SUCCESS_CODE;
2466                 } else if (contentLength == -1) {
2467                     // When no Content-length (default value == -1), attempt to read a byte
2468                     // from the response. Do not use available() as it is unreliable.
2469                     // See http://b/33498325.
2470                     if (urlConnection.getInputStream().read() == -1) {
2471                         validationLog(probeType, url,
2472                                 "Empty 200 response interpreted as failed response.");
2473                         httpResponseCode = CaptivePortalProbeResult.FAILED_CODE;
2474                     }
2475                 } else if (matchesHttpContentLength(contentLength)) {
2476                     final InputStream is = new BufferedInputStream(urlConnection.getInputStream());
2477                     final String content = readAsString(is, (int) contentLength,
2478                             extractCharset(urlConnection.getContentType()));
2479                     if (matchesHttpContent(content,
2480                             R.string.config_network_validation_failed_content_regexp)) {
2481                         httpResponseCode = CaptivePortalProbeResult.FAILED_CODE;
2482                     } else if (matchesHttpContent(content,
2483                             R.string.config_network_validation_success_content_regexp)) {
2484                         httpResponseCode = CaptivePortalProbeResult.SUCCESS_CODE;
2485                     }
2486 
2487                     if (httpResponseCode != 200) {
2488                         validationLog(probeType, url, "200 response with Content-length ="
2489                                 + contentLength + ", content matches custom regexp, interpreted"
2490                                 + " as " + httpResponseCode
2491                                 + " response.");
2492                     }
2493                 } else if (contentLength <= 4) {
2494                     // Consider 200 response with "Content-length <= 4" to not be a captive
2495                     // portal. There's no point in considering this a captive portal as the
2496                     // user cannot sign-in to an empty page. Probably the result of a broken
2497                     // transparent proxy. See http://b/9972012 and http://b/122999481.
2498                     validationLog(probeType, url, "200 response with Content-length <= 4"
2499                             + " interpreted as failed response.");
2500                     httpResponseCode = CaptivePortalProbeResult.FAILED_CODE;
2501                 }
2502             }
2503         } catch (IOException e) {
2504             validationLog(probeType, url, "Probe failed with exception " + e);
2505             if (httpResponseCode == CaptivePortalProbeResult.FAILED_CODE) {
2506                 // TODO: Ping gateway and DNS server and log results.
2507             }
2508         } finally {
2509             if (urlConnection != null) {
2510                 urlConnection.disconnect();
2511             }
2512             TrafficStats.setThreadStatsTag(oldTag);
2513         }
2514         logValidationProbe(probeTimer.stop(), probeType, httpResponseCode);
2515 
2516         final CaptivePortalProbeResult probeResult;
2517         if (probeSpec == null) {
2518             probeResult = new CaptivePortalProbeResult(httpResponseCode, redirectUrl,
2519                     url.toString(),   1 << probeType);
2520         } else {
2521             probeResult = probeSpec.getResult(httpResponseCode, redirectUrl);
2522         }
2523         recordProbeEventMetrics(NetworkValidationMetrics.probeTypeToEnum(probeType),
2524                 probeTimer.stop(), NetworkValidationMetrics.httpProbeResultToEnum(probeResult),
2525                 null /* capportData */);
2526         return probeResult;
2527     }
2528 
2529     @VisibleForTesting
matchesHttpContent(final String content, @StringRes final int configResource)2530     boolean matchesHttpContent(final String content, @StringRes final int configResource) {
2531         final String resString = getResStringConfig(mContext, configResource, "");
2532         try {
2533             return content.matches(resString);
2534         } catch (PatternSyntaxException e) {
2535             Log.e(TAG, "Pattern syntax exception occurs when matching the resource=" + resString,
2536                     e);
2537             return false;
2538         }
2539     }
2540 
2541     @VisibleForTesting
matchesHttpContentLength(final long contentLength)2542     boolean matchesHttpContentLength(final long contentLength) {
2543         // Consider that the Resources#getInteger() is returning an integer, so if the contentLength
2544         // is lower or equal to 0 or higher than Integer.MAX_VALUE, then it's an invalid value.
2545         if (contentLength <= 0) return false;
2546         if (contentLength > Integer.MAX_VALUE) {
2547             logw("matchesHttpContentLength : Get invalid contentLength = " + contentLength);
2548             return false;
2549         }
2550         return (contentLength > getResIntConfig(mContext,
2551                 R.integer.config_min_matches_http_content_length, Integer.MAX_VALUE)
2552                 &&
2553                 contentLength < getResIntConfig(mContext,
2554                 R.integer.config_max_matches_http_content_length, 0));
2555     }
2556 
makeProbeConnection(URL url, boolean followRedirects)2557     private HttpURLConnection makeProbeConnection(URL url, boolean followRedirects)
2558             throws IOException {
2559         final HttpURLConnection conn = (HttpURLConnection) mCleartextDnsNetwork.openConnection(url);
2560         conn.setInstanceFollowRedirects(followRedirects);
2561         conn.setConnectTimeout(SOCKET_TIMEOUT_MS);
2562         conn.setReadTimeout(SOCKET_TIMEOUT_MS);
2563         conn.setRequestProperty("Connection", "close");
2564         conn.setUseCaches(false);
2565         if (mCaptivePortalUserAgent != null) {
2566             conn.setRequestProperty("User-Agent", mCaptivePortalUserAgent);
2567         }
2568         return conn;
2569     }
2570 
2571     @VisibleForTesting
2572     @NonNull
readAsString(InputStream is, int maxLength, Charset charset)2573     protected static String readAsString(InputStream is, int maxLength, Charset charset)
2574             throws IOException {
2575         final InputStreamReader reader = new InputStreamReader(is, charset);
2576         final char[] buffer = new char[1000];
2577         final StringBuilder builder = new StringBuilder();
2578         int totalReadLength = 0;
2579         while (totalReadLength < maxLength) {
2580             final int availableLength = Math.min(maxLength - totalReadLength, buffer.length);
2581             final int currentLength = reader.read(buffer, 0, availableLength);
2582             if (currentLength < 0) break; // EOF
2583 
2584             totalReadLength += currentLength;
2585             builder.append(buffer, 0, currentLength);
2586         }
2587         return builder.toString();
2588     }
2589 
2590     /**
2591      * Attempt to extract the {@link Charset} of the response from its Content-Type header.
2592      *
2593      * <p>If the {@link Charset} cannot be extracted, UTF-8 is returned by default.
2594      */
2595     @VisibleForTesting
2596     @NonNull
extractCharset(@ullable String contentTypeHeader)2597     protected static Charset extractCharset(@Nullable String contentTypeHeader) {
2598         if (contentTypeHeader == null) return StandardCharsets.UTF_8;
2599         // See format in https://tools.ietf.org/html/rfc7231#section-3.1.1.1
2600         final Pattern charsetPattern = Pattern.compile("; *charset=\"?([^ ;\"]+)\"?",
2601                 Pattern.CASE_INSENSITIVE);
2602         final Matcher matcher = charsetPattern.matcher(contentTypeHeader);
2603         if (!matcher.find()) return StandardCharsets.UTF_8;
2604 
2605         try {
2606             return Charset.forName(matcher.group(1));
2607         } catch (IllegalArgumentException e) {
2608             return StandardCharsets.UTF_8;
2609         }
2610     }
2611 
2612     private class ProbeThread extends Thread {
2613         private final CountDownLatch mLatch;
2614         private final Probe mProbe;
2615 
ProbeThread(CountDownLatch latch, EvaluationThreadDeps deps, ProxyInfo proxy, URL url, int probeType, Uri captivePortalApiUrl)2616         ProbeThread(CountDownLatch latch, EvaluationThreadDeps deps, ProxyInfo proxy, URL url,
2617                 int probeType, Uri captivePortalApiUrl) {
2618             mLatch = latch;
2619             mProbe = (probeType == ValidationProbeEvent.PROBE_HTTPS)
2620                     ? new HttpsProbe(deps, proxy, url, captivePortalApiUrl)
2621                     : new HttpProbe(deps, proxy, url, captivePortalApiUrl);
2622             mResult = CaptivePortalProbeResult.failed(probeType);
2623         }
2624 
2625         private volatile CaptivePortalProbeResult mResult;
2626 
result()2627         public CaptivePortalProbeResult result() {
2628             return mResult;
2629         }
2630 
2631         @Override
run()2632         public void run() {
2633             mResult = mProbe.sendProbe();
2634             if (isConclusiveResult(mResult, mProbe.mCaptivePortalApiUrl)) {
2635                 // Stop waiting immediately if any probe is conclusive.
2636                 while (mLatch.getCount() > 0) {
2637                     mLatch.countDown();
2638                 }
2639             }
2640             // Signal this probe has completed.
2641             mLatch.countDown();
2642         }
2643     }
2644 
2645     private abstract static class Probe {
2646         protected final EvaluationThreadDeps mDeps;
2647         protected final ProxyInfo mProxy;
2648         protected final URL mUrl;
2649         protected final Uri mCaptivePortalApiUrl;
2650 
Probe(EvaluationThreadDeps deps, ProxyInfo proxy, URL url, Uri captivePortalApiUrl)2651         protected Probe(EvaluationThreadDeps deps, ProxyInfo proxy, URL url,
2652                 Uri captivePortalApiUrl) {
2653             mDeps = deps;
2654             mProxy = proxy;
2655             mUrl = url;
2656             mCaptivePortalApiUrl = captivePortalApiUrl;
2657         }
2658         // sendProbe() is synchronous and blocks until it has the result.
sendProbe()2659         protected abstract CaptivePortalProbeResult sendProbe();
2660     }
2661 
2662     final class HttpsProbe extends Probe {
HttpsProbe(EvaluationThreadDeps deps, ProxyInfo proxy, URL url, Uri captivePortalApiUrl)2663         HttpsProbe(EvaluationThreadDeps deps, ProxyInfo proxy, URL url, Uri captivePortalApiUrl) {
2664             super(deps, proxy, url, captivePortalApiUrl);
2665         }
2666 
2667         @Override
sendProbe()2668         protected CaptivePortalProbeResult sendProbe() {
2669             return sendDnsAndHttpProbes(mProxy, mUrl, ValidationProbeEvent.PROBE_HTTPS);
2670         }
2671     }
2672 
2673     final class HttpProbe extends Probe {
HttpProbe(EvaluationThreadDeps deps, ProxyInfo proxy, URL url, Uri captivePortalApiUrl)2674         HttpProbe(EvaluationThreadDeps deps, ProxyInfo proxy, URL url, Uri captivePortalApiUrl) {
2675             super(deps, proxy, url, captivePortalApiUrl);
2676         }
2677 
sendCapportApiProbe()2678         private CaptivePortalDataShim sendCapportApiProbe() {
2679             // TODO: consider adding metrics counters for each case returning null in this method
2680             // (cases where the API is not implemented properly).
2681             validationLog("Fetching captive portal data from " + mCaptivePortalApiUrl);
2682 
2683             final String apiContent;
2684             try {
2685                 final URL url = new URL(mCaptivePortalApiUrl.toString());
2686                 // Protocol must be HTTPS
2687                 // (as per https://www.ietf.org/id/draft-ietf-capport-api-07.txt, #4).
2688                 // Only allow HTTP on localhost, for testing.
2689                 final boolean isTestLocalhostHttp = mDeps.mIsTestNetwork
2690                         && "localhost".equals(url.getHost()) && "http".equals(url.getProtocol());
2691                 if (!"https".equals(url.getProtocol()) && !isTestLocalhostHttp) {
2692                     validationLog("Invalid captive portal API protocol: " + url.getProtocol());
2693                     return null;
2694                 }
2695 
2696                 final HttpURLConnection conn = makeProbeConnection(
2697                         url, true /* followRedirects */);
2698                 conn.setRequestProperty(ACCEPT_HEADER, CAPPORT_API_CONTENT_TYPE);
2699                 final int responseCode = conn.getResponseCode();
2700                 if (responseCode != 200) {
2701                     validationLog("Non-200 API response code: " + conn.getResponseCode());
2702                     return null;
2703                 }
2704                 final Charset charset = extractCharset(conn.getHeaderField(CONTENT_TYPE_HEADER));
2705                 if (charset != StandardCharsets.UTF_8) {
2706                     validationLog("Invalid charset for capport API: " + charset);
2707                     return null;
2708                 }
2709 
2710                 apiContent = readAsString(conn.getInputStream(),
2711                         CAPPORT_API_MAX_JSON_LENGTH, charset);
2712             } catch (IOException e) {
2713                 validationLog("I/O error reading capport data: " + e.getMessage());
2714                 return null;
2715             }
2716 
2717             try {
2718                 final JSONObject info = new JSONObject(apiContent);
2719                 final CaptivePortalDataShim capportData = CaptivePortalDataShimImpl.fromJson(info);
2720                 if (capportData != null && capportData.isCaptive()
2721                         && capportData.getUserPortalUrl() == null) {
2722                     validationLog("Missing user-portal-url from capport response");
2723                     return null;
2724                 }
2725                 return capportData;
2726             } catch (JSONException e) {
2727                 validationLog("Could not parse capport API JSON: " + e.getMessage());
2728                 return null;
2729             } catch (UnsupportedApiLevelException e) {
2730                 // This should never happen because LinkProperties would not have a capport URL
2731                 // before R.
2732                 validationLog("Platform API too low to support capport API");
2733                 return null;
2734             }
2735         }
2736 
tryCapportApiProbe()2737         private CaptivePortalDataShim tryCapportApiProbe() {
2738             if (mCaptivePortalApiUrl == null) return null;
2739             final Stopwatch capportApiWatch = new Stopwatch().start();
2740             final CaptivePortalDataShim capportData = sendCapportApiProbe();
2741             recordProbeEventMetrics(ProbeType.PT_CAPPORT_API, capportApiWatch.stop(),
2742                     capportData == null ? ProbeResult.PR_FAILURE : ProbeResult.PR_SUCCESS,
2743                     capportData);
2744             return capportData;
2745         }
2746 
2747         @Override
sendProbe()2748         protected CaptivePortalProbeResult sendProbe() {
2749             final CaptivePortalDataShim capportData = tryCapportApiProbe();
2750             if (capportData != null && capportData.isCaptive()) {
2751                 final String loginUrlString = capportData.getUserPortalUrl().toString();
2752                 // Starting from R (where CaptivePortalData was introduced), the captive portal app
2753                 // delegates to NetworkMonitor for verifying when the network validates instead of
2754                 // probing the detectUrl. So pass the detectUrl to have the portal open on that,
2755                 // page; CaptivePortalLogin will not use it for probing.
2756                 return new CapportApiProbeResult(
2757                         CaptivePortalProbeResult.PORTAL_CODE,
2758                         loginUrlString /* redirectUrl */,
2759                         loginUrlString /* detectUrl */,
2760                         capportData,
2761                         1 << ValidationProbeEvent.PROBE_HTTP);
2762             }
2763 
2764             // If the API says it's not captive, still check for HTTP connectivity. This helps
2765             // with partial connectivity detection, and a broken API saying that there is no
2766             // redirect when there is one.
2767             final CaptivePortalProbeResult res =
2768                     sendDnsAndHttpProbes(mProxy, mUrl, ValidationProbeEvent.PROBE_HTTP);
2769             return mCaptivePortalApiUrl == null ? res : new CapportApiProbeResult(res, capportData);
2770         }
2771     }
2772 
isConclusiveResult(@onNull CaptivePortalProbeResult result, @Nullable Uri captivePortalApiUrl)2773     private static boolean isConclusiveResult(@NonNull CaptivePortalProbeResult result,
2774             @Nullable Uri captivePortalApiUrl) {
2775         // isPortal() is not expected on the HTTPS probe, but treat the network as portal would make
2776         // sense if the probe reports portal. In case the capport API is available, the API is
2777         // authoritative on whether there is a portal, so the HTTPS probe is not enough to conclude
2778         // there is connectivity, and a determination will be made once the capport API probe
2779         // returns. Note that the API can only force the system to detect a portal even if the HTTPS
2780         // probe succeeds. It cannot force the system to detect no portal if the HTTPS probe fails.
2781         return result.isPortal()
2782                 || (result.isConcludedFromHttps() && result.isSuccessful()
2783                         && captivePortalApiUrl == null);
2784     }
2785 
sendMultiParallelHttpAndHttpsProbes( @onNull EvaluationThreadDeps deps, @Nullable ProxyInfo proxy, @NonNull URL[] httpsUrls, @NonNull URL[] httpUrls)2786     private CaptivePortalProbeResult sendMultiParallelHttpAndHttpsProbes(
2787             @NonNull EvaluationThreadDeps deps, @Nullable ProxyInfo proxy, @NonNull URL[] httpsUrls,
2788             @NonNull URL[] httpUrls) {
2789         // If multiple URLs are required to ensure the correctness of validation, send parallel
2790         // probes to explore the result in separate probe threads and aggregate those results into
2791         // one as the final result for either HTTP or HTTPS.
2792 
2793         // Number of probes to wait for.
2794         final int num = httpsUrls.length + httpUrls.length;
2795         // Fixed pool to prevent configuring too many urls to exhaust system resource.
2796         final ExecutorService executor = Executors.newFixedThreadPool(
2797                 Math.min(num, MAX_PROBE_THREAD_POOL_SIZE));
2798         final CompletionService<CaptivePortalProbeResult> ecs =
2799                 new ExecutorCompletionService<CaptivePortalProbeResult>(executor);
2800         final Uri capportApiUrl = getCaptivePortalApiUrl(mLinkProperties);
2801         final List<Future<CaptivePortalProbeResult>> futures = new ArrayList<>();
2802 
2803         try {
2804             // Queue https and http probe.
2805 
2806             // Each of these HTTP probes will start with probing capport API if present. So if
2807             // multiple HTTP URLs are configured, AP will send multiple identical accesses to the
2808             // capport URL. Thus, send capport API probing with one of the HTTP probe is enough.
2809             // Probe capport API with the first HTTP probe.
2810             // TODO: Have the capport probe as a different probe for cleanliness.
2811             final URL urlMaybeWithCapport = httpUrls[0];
2812             for (final URL url : httpUrls) {
2813                 futures.add(ecs.submit(() -> new HttpProbe(deps, proxy, url,
2814                         url.equals(urlMaybeWithCapport) ? capportApiUrl : null).sendProbe()));
2815             }
2816 
2817             for (final URL url : httpsUrls) {
2818                 futures.add(ecs.submit(() -> new HttpsProbe(deps, proxy, url, capportApiUrl)
2819                         .sendProbe()));
2820             }
2821 
2822             final ArrayList<CaptivePortalProbeResult> completedProbes = new ArrayList<>();
2823             for (int i = 0; i < num; i++) {
2824                 completedProbes.add(ecs.take().get());
2825                 final CaptivePortalProbeResult res = evaluateCapportResult(
2826                         completedProbes, httpsUrls.length, capportApiUrl != null /* hasCapport */);
2827                 if (res != null) {
2828                     reportProbeResult(res);
2829                     return res;
2830                 }
2831             }
2832         } catch (ExecutionException e) {
2833             Log.e(TAG, "Error sending probes.", e);
2834         } catch (InterruptedException e) {
2835             // Ignore interrupted probe result because result is not important to conclude the
2836             // result.
2837         } finally {
2838             // Interrupt ongoing probes since we have already gotten result from one of them.
2839             futures.forEach(future -> future.cancel(true));
2840             executor.shutdownNow();
2841         }
2842 
2843         return CaptivePortalProbeResult.failed(ValidationProbeEvent.PROBE_HTTPS);
2844     }
2845 
2846     @Nullable
evaluateCapportResult( List<CaptivePortalProbeResult> probes, int numHttps, boolean hasCapport)2847     private CaptivePortalProbeResult evaluateCapportResult(
2848             List<CaptivePortalProbeResult> probes, int numHttps, boolean hasCapport) {
2849         CaptivePortalProbeResult capportResult = null;
2850         CaptivePortalProbeResult httpPortalResult = null;
2851         int httpSuccesses = 0;
2852         int httpsSuccesses = 0;
2853         int httpsFailures = 0;
2854 
2855         for (CaptivePortalProbeResult probe : probes) {
2856             if (probe instanceof CapportApiProbeResult) {
2857                 capportResult = probe;
2858             } else if (probe.isConcludedFromHttps()) {
2859                 if (probe.isSuccessful()) httpsSuccesses++;
2860                 else httpsFailures++;
2861             } else { // http probes
2862                 if (probe.isPortal()) {
2863                     // Unlike https probe, http probe may have redirect url information kept in the
2864                     // probe result. Thus, the result can not be newly created with response code
2865                     // only. If the captive portal behavior will be varied because of different
2866                     // probe URLs, this means that if the portal returns different redirect URLs for
2867                     // different probes and has a different behavior depending on the URL, then the
2868                     // behavior of the login page may differ depending on the order in which the
2869                     // probes terminate. However, NetworkMonitor does have to choose one of the
2870                     // redirect URLs and right now there is no clue at all which of the probe has
2871                     // the better redirect URL, so there is no telling which is best to use.
2872                     // Therefore the current code just uses whichever happens to be the last one to
2873                     // complete.
2874                     httpPortalResult = probe;
2875                 } else if (probe.isSuccessful()) {
2876                     httpSuccesses++;
2877                 }
2878             }
2879         }
2880         // If there is Capport url configured but the result is not available yet, wait for it.
2881         if (hasCapport && capportResult == null) return null;
2882         // Capport API saying it's a portal is authoritative.
2883         if (capportResult != null && capportResult.isPortal()) return capportResult;
2884         // Any HTTP probes saying probe portal is conclusive.
2885         if (httpPortalResult != null) return httpPortalResult;
2886         // Any HTTPS probes works then the network validates.
2887         if (httpsSuccesses > 0) {
2888             return CaptivePortalProbeResult.success(1 << ValidationProbeEvent.PROBE_HTTPS);
2889         }
2890         // All HTTPS failed and at least one HTTP succeeded, then it's partial.
2891         if (httpsFailures == numHttps && httpSuccesses > 0) {
2892             return CaptivePortalProbeResult.PARTIAL;
2893         }
2894         // Otherwise, the result is unknown yet.
2895         return null;
2896     }
2897 
reportProbeResult(@onNull CaptivePortalProbeResult res)2898     private void reportProbeResult(@NonNull CaptivePortalProbeResult res) {
2899         if (res instanceof CapportApiProbeResult) {
2900             maybeReportCaptivePortalData(((CapportApiProbeResult) res).getCaptivePortalData());
2901         }
2902 
2903         // This is not a if-else case since partial connectivity will concluded from both HTTP and
2904         // HTTPS probe. Both HTTP and HTTPS result should be reported.
2905         if (res.isConcludedFromHttps()) {
2906             reportHttpProbeResult(NETWORK_VALIDATION_PROBE_HTTPS, res);
2907         }
2908 
2909         if (res.isConcludedFromHttp()) {
2910             reportHttpProbeResult(NETWORK_VALIDATION_PROBE_HTTP, res);
2911         }
2912     }
2913 
sendHttpAndHttpsParallelWithFallbackProbes( EvaluationThreadDeps deps, ProxyInfo proxy, URL httpsUrl, URL httpUrl)2914     private CaptivePortalProbeResult sendHttpAndHttpsParallelWithFallbackProbes(
2915             EvaluationThreadDeps deps, ProxyInfo proxy, URL httpsUrl, URL httpUrl) {
2916         // Number of probes to wait for. If a probe completes with a conclusive answer
2917         // it shortcuts the latch immediately by forcing the count to 0.
2918         final CountDownLatch latch = new CountDownLatch(2);
2919 
2920         final Uri capportApiUrl = getCaptivePortalApiUrl(mLinkProperties);
2921         final ProbeThread httpsProbe = new ProbeThread(latch, deps, proxy, httpsUrl,
2922                 ValidationProbeEvent.PROBE_HTTPS, capportApiUrl);
2923         final ProbeThread httpProbe = new ProbeThread(latch, deps, proxy, httpUrl,
2924                 ValidationProbeEvent.PROBE_HTTP, capportApiUrl);
2925 
2926         try {
2927             httpsProbe.start();
2928             httpProbe.start();
2929             latch.await(PROBE_TIMEOUT_MS, TimeUnit.MILLISECONDS);
2930         } catch (InterruptedException e) {
2931             validationLog("Error: probes wait interrupted!");
2932             return CaptivePortalProbeResult.failed(CaptivePortalProbeResult.PROBE_UNKNOWN);
2933         }
2934 
2935         final CaptivePortalProbeResult httpsResult = httpsProbe.result();
2936         final CaptivePortalProbeResult httpResult = httpProbe.result();
2937 
2938         // Look for a conclusive probe result first.
2939         if (isConclusiveResult(httpResult, capportApiUrl)) {
2940             reportProbeResult(httpProbe.result());
2941             return httpResult;
2942         }
2943 
2944         if (isConclusiveResult(httpsResult, capportApiUrl)) {
2945             reportProbeResult(httpsProbe.result());
2946             return httpsResult;
2947         }
2948         // Consider a DNS response with a private IP address on the HTTP probe as an indication that
2949         // the network is not connected to the Internet, and have the whole evaluation fail in that
2950         // case, instead of potentially detecting a captive portal. This logic only affects portal
2951         // detection, not network validation.
2952         // This only applies if the DNS probe completed within PROBE_TIMEOUT_MS, as the fallback
2953         // probe should not be delayed by this check.
2954         if (mPrivateIpNoInternetEnabled && (httpResult.isDnsPrivateIpResponse())) {
2955             validationLog("DNS response to the URL is private IP");
2956             return CaptivePortalProbeResult.failed(1 << ValidationProbeEvent.PROBE_HTTP);
2957         }
2958         // If a fallback method exists, use it to retry portal detection.
2959         // If we have new-style probe specs, use those. Otherwise, use the fallback URLs.
2960         final CaptivePortalProbeSpec probeSpec = nextFallbackSpec();
2961         final URL fallbackUrl = (probeSpec != null) ? probeSpec.getUrl() : nextFallbackUrl();
2962         CaptivePortalProbeResult fallbackProbeResult = null;
2963         if (fallbackUrl != null) {
2964             fallbackProbeResult = sendHttpProbe(fallbackUrl, PROBE_FALLBACK, probeSpec);
2965             reportHttpProbeResult(NETWORK_VALIDATION_PROBE_FALLBACK, fallbackProbeResult);
2966             if (fallbackProbeResult.isPortal()) {
2967                 return fallbackProbeResult;
2968             }
2969         }
2970         // Otherwise wait until http and https probes completes and use their results.
2971         try {
2972             httpProbe.join();
2973             reportProbeResult(httpProbe.result());
2974 
2975             if (httpProbe.result().isPortal()) {
2976                 return httpProbe.result();
2977             }
2978 
2979             httpsProbe.join();
2980             reportHttpProbeResult(NETWORK_VALIDATION_PROBE_HTTPS, httpsProbe.result());
2981 
2982             final boolean isHttpSuccessful =
2983                     (httpProbe.result().isSuccessful()
2984                     || (fallbackProbeResult != null && fallbackProbeResult.isSuccessful()));
2985             if (httpsProbe.result().isFailed() && isHttpSuccessful) {
2986                 return CaptivePortalProbeResult.PARTIAL;
2987             }
2988             return httpsProbe.result();
2989         } catch (InterruptedException e) {
2990             validationLog("Error: http or https probe wait interrupted!");
2991             return CaptivePortalProbeResult.failed(CaptivePortalProbeResult.PROBE_UNKNOWN);
2992         }
2993     }
2994 
makeURL(String url)2995     private URL makeURL(String url) {
2996         if (url != null) {
2997             try {
2998                 return new URL(url);
2999             } catch (MalformedURLException e) {
3000                 validationLog("Bad URL: " + url);
3001             }
3002         }
3003         return null;
3004     }
3005 
3006     /**
3007      * @param responseReceived - whether or not we received a valid HTTP response to our request.
3008      * If false, isCaptivePortal and responseTimestampMs are ignored
3009      * TODO: This should be moved to the transports.  The latency could be passed to the transports
3010      * along with the captive portal result.  Currently the TYPE_MOBILE broadcasts appear unused so
3011      * perhaps this could just be added to the WiFi transport only.
3012      */
sendNetworkConditionsBroadcast(boolean responseReceived, boolean isCaptivePortal, long requestTimestampMs, long responseTimestampMs)3013     private void sendNetworkConditionsBroadcast(boolean responseReceived, boolean isCaptivePortal,
3014             long requestTimestampMs, long responseTimestampMs) {
3015         Intent latencyBroadcast =
3016                 new Intent(NetworkMonitorUtils.ACTION_NETWORK_CONDITIONS_MEASURED);
3017         if (mNetworkCapabilities.hasTransport(TRANSPORT_WIFI)) {
3018             if (!mWifiManager.isScanAlwaysAvailable()) {
3019                 return;
3020             }
3021 
3022             WifiInfo currentWifiInfo = mWifiManager.getConnectionInfo();
3023             if (currentWifiInfo != null) {
3024                 // NOTE: getSSID()'s behavior changed in API 17; before that, SSIDs were not
3025                 // surrounded by double quotation marks (thus violating the Javadoc), but this
3026                 // was changed to match the Javadoc in API 17. Since clients may have started
3027                 // sanitizing the output of this method since API 17 was released, we should
3028                 // not change it here as it would become impossible to tell whether the SSID is
3029                 // simply being surrounded by quotes due to the API, or whether those quotes
3030                 // are actually part of the SSID.
3031                 latencyBroadcast.putExtra(NetworkMonitorUtils.EXTRA_SSID,
3032                         currentWifiInfo.getSSID());
3033                 latencyBroadcast.putExtra(NetworkMonitorUtils.EXTRA_BSSID,
3034                         currentWifiInfo.getBSSID());
3035             } else {
3036                 if (VDBG) logw("network info is TYPE_WIFI but no ConnectionInfo found");
3037                 return;
3038             }
3039             latencyBroadcast.putExtra(NetworkMonitorUtils.EXTRA_CONNECTIVITY_TYPE, TYPE_WIFI);
3040         } else if (mNetworkCapabilities.hasTransport(TRANSPORT_CELLULAR)) {
3041             // TODO(b/123893112): Support multi-sim.
3042             latencyBroadcast.putExtra(NetworkMonitorUtils.EXTRA_NETWORK_TYPE,
3043                     mTelephonyManager.getNetworkType());
3044             final ServiceState dataSs = mTelephonyManager.getServiceState();
3045             if (dataSs == null) {
3046                 logw("failed to retrieve ServiceState");
3047                 return;
3048             }
3049             // See if the data sub is registered for PS services on cell.
3050             final NetworkRegistrationInfo nri = dataSs.getNetworkRegistrationInfo(
3051                     NetworkRegistrationInfo.DOMAIN_PS,
3052                     AccessNetworkConstants.TRANSPORT_TYPE_WWAN);
3053             latencyBroadcast.putExtra(
3054                     NetworkMonitorUtils.EXTRA_CELL_ID,
3055                     nri == null ? null : nri.getCellIdentity());
3056             latencyBroadcast.putExtra(NetworkMonitorUtils.EXTRA_CONNECTIVITY_TYPE, TYPE_MOBILE);
3057         } else {
3058             return;
3059         }
3060         latencyBroadcast.putExtra(NetworkMonitorUtils.EXTRA_RESPONSE_RECEIVED,
3061                 responseReceived);
3062         latencyBroadcast.putExtra(NetworkMonitorUtils.EXTRA_REQUEST_TIMESTAMP_MS,
3063                 requestTimestampMs);
3064 
3065         if (responseReceived) {
3066             latencyBroadcast.putExtra(NetworkMonitorUtils.EXTRA_IS_CAPTIVE_PORTAL,
3067                     isCaptivePortal);
3068             latencyBroadcast.putExtra(NetworkMonitorUtils.EXTRA_RESPONSE_TIMESTAMP_MS,
3069                     responseTimestampMs);
3070         }
3071         mDependencies.sendNetworkConditionsBroadcast(mContext, latencyBroadcast);
3072     }
3073 
logNetworkEvent(int evtype)3074     private void logNetworkEvent(int evtype) {
3075         int[] transports = mNetworkCapabilities.getTransportTypes();
3076         mMetricsLog.log(mCleartextDnsNetwork, transports, new NetworkEvent(evtype));
3077     }
3078 
networkEventType(ValidationStage s, EvaluationResult r)3079     private int networkEventType(ValidationStage s, EvaluationResult r) {
3080         if (s.mIsFirstValidation) {
3081             if (r.mIsValidated) {
3082                 return NetworkEvent.NETWORK_FIRST_VALIDATION_SUCCESS;
3083             } else {
3084                 return NetworkEvent.NETWORK_FIRST_VALIDATION_PORTAL_FOUND;
3085             }
3086         } else {
3087             if (r.mIsValidated) {
3088                 return NetworkEvent.NETWORK_REVALIDATION_SUCCESS;
3089             } else {
3090                 return NetworkEvent.NETWORK_REVALIDATION_PORTAL_FOUND;
3091             }
3092         }
3093     }
3094 
maybeLogEvaluationResult(int evtype)3095     private void maybeLogEvaluationResult(int evtype) {
3096         if (mEvaluationTimer.isRunning()) {
3097             int[] transports = mNetworkCapabilities.getTransportTypes();
3098             mMetricsLog.log(mCleartextDnsNetwork, transports,
3099                     new NetworkEvent(evtype, mEvaluationTimer.stop() / 1000));
3100             mEvaluationTimer.reset();
3101         }
3102     }
3103 
logValidationProbe(long durationUs, int probeType, int probeResult)3104     private void logValidationProbe(long durationUs, int probeType, int probeResult) {
3105         int[] transports = mNetworkCapabilities.getTransportTypes();
3106         boolean isFirstValidation = validationStage().mIsFirstValidation;
3107         ValidationProbeEvent ev = new ValidationProbeEvent.Builder()
3108                 .setProbeType(probeType, isFirstValidation)
3109                 .setReturnCode(probeResult)
3110                 .setDurationMs(durationUs / 1000)
3111                 .build();
3112         mMetricsLog.log(mCleartextDnsNetwork, transports, ev);
3113     }
3114 
3115     @VisibleForTesting
3116     public static class Dependencies {
getPrivateDnsBypassNetwork(Network network)3117         public Network getPrivateDnsBypassNetwork(Network network) {
3118             return new OneAddressPerFamilyNetwork(network);
3119         }
3120 
getDnsResolver()3121         public DnsResolver getDnsResolver() {
3122             return DnsResolver.getInstance();
3123         }
3124 
getRandom()3125         public Random getRandom() {
3126             return new Random();
3127         }
3128 
3129         /**
3130          * Get the value of a global integer setting.
3131          * @param symbol Name of the setting
3132          * @param defaultValue Value to return if the setting is not defined.
3133          */
getSetting(Context context, String symbol, int defaultValue)3134         public int getSetting(Context context, String symbol, int defaultValue) {
3135             return Settings.Global.getInt(context.getContentResolver(), symbol, defaultValue);
3136         }
3137 
3138         /**
3139          * Get the value of a global String setting.
3140          * @param symbol Name of the setting
3141          * @param defaultValue Value to return if the setting is not defined.
3142          */
getSetting(Context context, String symbol, String defaultValue)3143         public String getSetting(Context context, String symbol, String defaultValue) {
3144             final String value = Settings.Global.getString(context.getContentResolver(), symbol);
3145             return value != null ? value : defaultValue;
3146         }
3147 
3148         /**
3149          * Look up the value of a property in DeviceConfig.
3150          * @param namespace The namespace containing the property to look up.
3151          * @param name The name of the property to look up.
3152          * @param defaultValue The value to return if the property does not exist or has no non-null
3153          *                     value.
3154          * @return the corresponding value, or defaultValue if none exists.
3155          */
3156         @Nullable
getDeviceConfigProperty(@onNull String namespace, @NonNull String name, @Nullable String defaultValue)3157         public String getDeviceConfigProperty(@NonNull String namespace, @NonNull String name,
3158                 @Nullable String defaultValue) {
3159             return NetworkStackUtils.getDeviceConfigProperty(namespace, name, defaultValue);
3160         }
3161 
3162         /**
3163          * Look up the value of a property in DeviceConfig.
3164          * @param namespace The namespace containing the property to look up.
3165          * @param name The name of the property to look up.
3166          * @param defaultValue The value to return if the property does not exist or has no non-null
3167          *                     value.
3168          * @return the corresponding value, or defaultValue if none exists.
3169          */
getDeviceConfigPropertyInt(@onNull String namespace, @NonNull String name, int defaultValue)3170         public int getDeviceConfigPropertyInt(@NonNull String namespace, @NonNull String name,
3171                 int defaultValue) {
3172             return NetworkStackUtils.getDeviceConfigPropertyInt(namespace, name, defaultValue);
3173         }
3174 
3175         /**
3176          * Check whether or not one experimental feature in the connectivity namespace is
3177          * enabled.
3178          * @param name Flag name of the experiment in the connectivity namespace.
3179          * @see NetworkStackUtils#isFeatureEnabled(Context, String, String)
3180          */
isFeatureEnabled(@onNull Context context, @NonNull String name)3181         public boolean isFeatureEnabled(@NonNull Context context, @NonNull String name) {
3182             return NetworkStackUtils.isFeatureEnabled(context, NAMESPACE_CONNECTIVITY, name);
3183         }
3184 
3185         /**
3186          * Send a broadcast indicating network conditions.
3187          */
sendNetworkConditionsBroadcast(@onNull Context context, @NonNull Intent broadcast)3188         public void sendNetworkConditionsBroadcast(@NonNull Context context,
3189                 @NonNull Intent broadcast) {
3190             context.sendBroadcastAsUser(broadcast, UserHandle.CURRENT,
3191                     NetworkMonitorUtils.PERMISSION_ACCESS_NETWORK_CONDITIONS);
3192         }
3193 
3194         /**
3195          * Check whether or not one specific experimental feature for a particular namespace from
3196          * {@link DeviceConfig} is enabled by comparing NetworkStack module version
3197          * {@link NetworkStack} with current version of property. If this property version is valid,
3198          * the corresponding experimental feature would be enabled, otherwise disabled.
3199          * @param context The global context information about an app environment.
3200          * @param namespace The namespace containing the property to look up.
3201          * @param name The name of the property to look up.
3202          * @param defaultEnabled The value to return if the property does not exist or its value is
3203          *                       null.
3204          * @return true if this feature is enabled, or false if disabled.
3205          */
isFeatureEnabled(@onNull Context context, @NonNull String namespace, @NonNull String name, boolean defaultEnabled)3206         public boolean isFeatureEnabled(@NonNull Context context, @NonNull String namespace,
3207                 @NonNull String name, boolean defaultEnabled) {
3208             return NetworkStackUtils.isFeatureEnabled(context, namespace, name, defaultEnabled);
3209         }
3210 
3211         /**
3212          * Collect data stall detection level information for each transport type. Write metrics
3213          * data to statsd pipeline.
3214          * @param stats a {@link DataStallDetectionStats} that contains the detection level
3215          *              information.
3216          * @para result the network reevaluation result.
3217          */
writeDataStallDetectionStats(@onNull final DataStallDetectionStats stats, @NonNull final CaptivePortalProbeResult result)3218         public void writeDataStallDetectionStats(@NonNull final DataStallDetectionStats stats,
3219                 @NonNull final CaptivePortalProbeResult result) {
3220             DataStallStatsUtils.write(stats, result);
3221         }
3222 
3223         public static final Dependencies DEFAULT = new Dependencies();
3224     }
3225 
3226     /**
3227      * Methods in this class perform no locking because all accesses are performed on the state
3228      * machine's thread. Need to consider the thread safety if it ever could be accessed outside the
3229      * state machine.
3230      */
3231     @VisibleForTesting
3232     protected class DnsStallDetector {
3233         private int mConsecutiveTimeoutCount = 0;
3234         private int mSize;
3235         final DnsResult[] mDnsEvents;
3236         final RingBufferIndices mResultIndices;
3237 
DnsStallDetector(int size)3238         DnsStallDetector(int size) {
3239             mSize = Math.max(DEFAULT_DNS_LOG_SIZE, size);
3240             mDnsEvents = new DnsResult[mSize];
3241             mResultIndices = new RingBufferIndices(mSize);
3242         }
3243 
3244         @VisibleForTesting
accumulateConsecutiveDnsTimeoutCount(int code)3245         protected void accumulateConsecutiveDnsTimeoutCount(int code) {
3246             final DnsResult result = new DnsResult(code);
3247             mDnsEvents[mResultIndices.add()] = result;
3248             if (result.isTimeout()) {
3249                 mConsecutiveTimeoutCount++;
3250             } else {
3251                 // Keep the event in mDnsEvents without clearing it so that there are logs to do the
3252                 // simulation and analysis.
3253                 mConsecutiveTimeoutCount = 0;
3254             }
3255         }
3256 
isDataStallSuspected(int timeoutCountThreshold, int validTime)3257         private boolean isDataStallSuspected(int timeoutCountThreshold, int validTime) {
3258             if (timeoutCountThreshold <= 0) {
3259                 Log.wtf(TAG, "Timeout count threshold should be larger than 0.");
3260                 return false;
3261             }
3262 
3263             // Check if the consecutive timeout count reach the threshold or not.
3264             if (mConsecutiveTimeoutCount < timeoutCountThreshold) {
3265                 return false;
3266             }
3267 
3268             // Check if the target dns event index is valid or not.
3269             final int firstConsecutiveTimeoutIndex =
3270                     mResultIndices.indexOf(mResultIndices.size() - timeoutCountThreshold);
3271 
3272             // If the dns timeout events happened long time ago, the events are meaningless for
3273             // data stall evaluation. Thus, check if the first consecutive timeout dns event
3274             // considered in the evaluation happened in defined threshold time.
3275             final long now = SystemClock.elapsedRealtime();
3276             final long firstTimeoutTime = now - mDnsEvents[firstConsecutiveTimeoutIndex].mTimeStamp;
3277             return (firstTimeoutTime < validTime);
3278         }
3279 
getConsecutiveTimeoutCount()3280         int getConsecutiveTimeoutCount() {
3281             return mConsecutiveTimeoutCount;
3282         }
3283     }
3284 
3285     private static class DnsResult {
3286         // TODO: Need to move the DNS return code definition to a specific class once unify DNS
3287         // response code is done.
3288         private static final int RETURN_CODE_DNS_TIMEOUT = 255;
3289 
3290         private final long mTimeStamp;
3291         private final int mReturnCode;
3292 
DnsResult(int code)3293         DnsResult(int code) {
3294             mTimeStamp = SystemClock.elapsedRealtime();
3295             mReturnCode = code;
3296         }
3297 
isTimeout()3298         private boolean isTimeout() {
3299             return mReturnCode == RETURN_CODE_DNS_TIMEOUT;
3300         }
3301     }
3302 
3303     @VisibleForTesting
3304     @Nullable
getDnsStallDetector()3305     protected DnsStallDetector getDnsStallDetector() {
3306         return mDnsStallDetector;
3307     }
3308 
3309     @Nullable
getTcpSocketTracker()3310     private TcpSocketTracker getTcpSocketTracker() {
3311         return mTcpTracker;
3312     }
3313 
dataStallEvaluateTypeEnabled(int type)3314     private boolean dataStallEvaluateTypeEnabled(int type) {
3315         return (mDataStallEvaluationType & type) != 0;
3316     }
3317 
3318     @VisibleForTesting
getLastProbeTime()3319     protected long getLastProbeTime() {
3320         return mLastProbeTime;
3321     }
3322 
3323     @VisibleForTesting
isDataStall()3324     protected boolean isDataStall() {
3325         if (!isValidationRequired()) {
3326             return false;
3327         }
3328 
3329         Boolean result = null;
3330         final StringJoiner msg = (DBG || VDBG_STALL) ? new StringJoiner(", ") : null;
3331         // Reevaluation will generate traffic. Thus, set a minimal reevaluation timer to limit the
3332         // possible traffic cost in metered network.
3333         if (!mNetworkCapabilities.hasCapability(NET_CAPABILITY_NOT_METERED)
3334                 && (SystemClock.elapsedRealtime() - getLastProbeTime()
3335                 < mDataStallMinEvaluateTime)) {
3336             return false;
3337         }
3338         // Check TCP signal. Suspect it may be a data stall if :
3339         // 1. TCP connection fail rate(lost+retrans) is higher than threshold.
3340         // 2. Accumulate enough packets count.
3341         final TcpSocketTracker tst = getTcpSocketTracker();
3342         if (dataStallEvaluateTypeEnabled(DATA_STALL_EVALUATION_TYPE_TCP) && tst != null) {
3343             if (tst.getLatestReceivedCount() > 0) {
3344                 result = false;
3345             } else if (tst.isDataStallSuspected()) {
3346                 result = true;
3347                 mDataStallTypeToCollect = DATA_STALL_EVALUATION_TYPE_TCP;
3348 
3349                 final DataStallReportParcelable p = new DataStallReportParcelable();
3350                 p.detectionMethod = DETECTION_METHOD_TCP_METRICS;
3351                 p.timestampMillis = SystemClock.elapsedRealtime();
3352                 p.tcpPacketFailRate = tst.getLatestPacketFailPercentage();
3353                 p.tcpMetricsCollectionPeriodMillis = getTcpPollingInterval();
3354 
3355                 notifyDataStallSuspected(p);
3356             }
3357             if (DBG || VDBG_STALL) {
3358                 msg.add("tcp packets received=" + tst.getLatestReceivedCount())
3359                     .add("latest tcp fail rate=" + tst.getLatestPacketFailPercentage());
3360             }
3361         }
3362 
3363         // Check dns signal. Suspect it may be a data stall if both :
3364         // 1. The number of consecutive DNS query timeouts >= mConsecutiveDnsTimeoutThreshold.
3365         // 2. Those consecutive DNS queries happened in the last mValidDataStallDnsTimeThreshold ms.
3366         final DnsStallDetector dsd = getDnsStallDetector();
3367         if ((result == null) && (dsd != null)
3368                 && dataStallEvaluateTypeEnabled(DATA_STALL_EVALUATION_TYPE_DNS)) {
3369             if (dsd.isDataStallSuspected(mConsecutiveDnsTimeoutThreshold,
3370                     mDataStallValidDnsTimeThreshold)) {
3371                 result = true;
3372                 mDataStallTypeToCollect = DATA_STALL_EVALUATION_TYPE_DNS;
3373                 logNetworkEvent(NetworkEvent.NETWORK_CONSECUTIVE_DNS_TIMEOUT_FOUND);
3374 
3375                 final DataStallReportParcelable p = new DataStallReportParcelable();
3376                 p.detectionMethod = DETECTION_METHOD_DNS_EVENTS;
3377                 p.timestampMillis = SystemClock.elapsedRealtime();
3378                 p.dnsConsecutiveTimeouts = mDnsStallDetector.getConsecutiveTimeoutCount();
3379                 notifyDataStallSuspected(p);
3380             }
3381             if (DBG || VDBG_STALL) {
3382                 msg.add("consecutive dns timeout count=" + dsd.getConsecutiveTimeoutCount());
3383             }
3384         }
3385         // log only data stall suspected.
3386         if ((DBG && Boolean.TRUE.equals(result)) || VDBG_STALL) {
3387             log("isDataStall: result=" + result + ", " + msg);
3388         }
3389 
3390         return (result == null) ? false : result;
3391     }
3392 
3393     // Class to keep state of evaluation results and probe results.
3394     //
3395     // The main purpose was to ensure NetworkMonitor can notify ConnectivityService of probe results
3396     // as soon as they happen, without triggering any other changes. This requires keeping state on
3397     // the most recent evaluation result. Calling noteProbeResult will ensure that the results
3398     // reported to ConnectivityService contain the previous evaluation result, and thus won't
3399     // trigger a validation or partial connectivity state change.
3400     //
3401     // Note that this class is not currently being used for this purpose. The reason is that some
3402     // of the system behaviour triggered by reporting network validation - notably, NetworkAgent
3403     // behaviour - depends not only on the value passed by notifyNetworkTested, but also on the
3404     // fact that notifyNetworkTested was called. For example, telephony triggers network recovery
3405     // any time it is told that validation failed, i.e., if the result does not contain
3406     // NETWORK_VALIDATION_RESULT_VALID. But with this scheme, the first two or three validation
3407     // reports are all failures, because they are "HTTP succeeded but validation not yet passed",
3408     // "HTTP and HTTPS succeeded but validation not yet passed", etc.
3409     @VisibleForTesting
3410     protected class EvaluationState {
3411         // The latest validation result for this network. This is a bitmask of
3412         // INetworkMonitor.NETWORK_VALIDATION_RESULT_* constants.
3413         private int mEvaluationResult = NETWORK_VALIDATION_RESULT_INVALID;
3414         // Indicates which probes have succeeded since clearProbeResults was called.
3415         // This is a bitmask of INetworkMonitor.NETWORK_VALIDATION_PROBE_* constants.
3416         private int mProbeResults = 0;
3417         // A bitmask to record which probes are completed.
3418         private int mProbeCompleted = 0;
3419         // The latest redirect URL.
3420         private String mRedirectUrl;
3421 
clearProbeResults()3422         protected void clearProbeResults() {
3423             mProbeResults = 0;
3424             mProbeCompleted = 0;
3425         }
3426 
maybeNotifyProbeResults(@onNull final Runnable modif)3427         private void maybeNotifyProbeResults(@NonNull final Runnable modif) {
3428             final int oldCompleted = mProbeCompleted;
3429             final int oldResults = mProbeResults;
3430             modif.run();
3431             if (oldCompleted != mProbeCompleted || oldResults != mProbeResults) {
3432                 notifyProbeStatusChanged(mProbeCompleted, mProbeResults);
3433             }
3434         }
3435 
removeProbeResult(final int probeResult)3436         protected void removeProbeResult(final int probeResult) {
3437             maybeNotifyProbeResults(() -> {
3438                 mProbeCompleted &= ~probeResult;
3439                 mProbeResults &= ~probeResult;
3440             });
3441         }
3442 
noteProbeResult(final int probeResult, final boolean succeeded)3443         protected void noteProbeResult(final int probeResult, final boolean succeeded) {
3444             maybeNotifyProbeResults(() -> {
3445                 mProbeCompleted |= probeResult;
3446                 if (succeeded) {
3447                     mProbeResults |= probeResult;
3448                 } else {
3449                     mProbeResults &= ~probeResult;
3450                 }
3451             });
3452         }
3453 
reportEvaluationResult(int result, @Nullable String redirectUrl)3454         protected void reportEvaluationResult(int result, @Nullable String redirectUrl) {
3455             mEvaluationResult = result;
3456             mRedirectUrl = redirectUrl;
3457             final NetworkTestResultParcelable p = new NetworkTestResultParcelable();
3458             p.result = result;
3459             p.probesSucceeded = mProbeResults;
3460             p.probesAttempted = mProbeCompleted;
3461             p.redirectUrl = redirectUrl;
3462             p.timestampMillis = SystemClock.elapsedRealtime();
3463             notifyNetworkTested(p);
3464             recordValidationResult(result, redirectUrl);
3465         }
3466 
3467         @VisibleForTesting
getEvaluationResult()3468         protected int getEvaluationResult() {
3469             return mEvaluationResult;
3470         }
3471 
3472         @VisibleForTesting
getProbeResults()3473         protected int getProbeResults() {
3474             return mProbeResults;
3475         }
3476 
3477         @VisibleForTesting
getProbeCompletedResult()3478         protected int getProbeCompletedResult() {
3479             return mProbeCompleted;
3480         }
3481     }
3482 
3483     @VisibleForTesting
getEvaluationState()3484     protected EvaluationState getEvaluationState() {
3485         return mEvaluationState;
3486     }
3487 
maybeDisableHttpsProbing(boolean acceptPartial)3488     private void maybeDisableHttpsProbing(boolean acceptPartial) {
3489         mAcceptPartialConnectivity = acceptPartial;
3490         // Ignore https probe in next validation if user accept partial connectivity on a partial
3491         // connectivity network.
3492         if (((mEvaluationState.getEvaluationResult() & NETWORK_VALIDATION_RESULT_PARTIAL) != 0)
3493                 && mAcceptPartialConnectivity) {
3494             mUseHttps = false;
3495         }
3496     }
3497 
3498     // Report HTTP, HTTP or FALLBACK probe result.
3499     @VisibleForTesting
reportHttpProbeResult(int probeResult, @NonNull final CaptivePortalProbeResult result)3500     protected void reportHttpProbeResult(int probeResult,
3501                 @NonNull final CaptivePortalProbeResult result) {
3502         boolean succeeded = result.isSuccessful();
3503         // The success of a HTTP probe does not tell us whether the DNS probe succeeded.
3504         // The DNS and HTTP probes run one after the other in sendDnsAndHttpProbes, and that
3505         // method cannot report the result of the DNS probe because that it could be running
3506         // on a different thread which is racing with the main state machine thread. So, if
3507         // an HTTP or HTTPS probe succeeded, assume that the DNS probe succeeded. But if an
3508         // HTTP or HTTPS probe failed, don't assume that DNS is not working.
3509         // TODO: fix this.
3510         if (succeeded) {
3511             probeResult |= NETWORK_VALIDATION_PROBE_DNS;
3512         }
3513         mEvaluationState.noteProbeResult(probeResult, succeeded);
3514     }
3515 
maybeReportCaptivePortalData(@ullable CaptivePortalDataShim data)3516     private void maybeReportCaptivePortalData(@Nullable CaptivePortalDataShim data) {
3517         // Do not clear data even if it is null: access points should not stop serving the API, so
3518         // if the API disappears this is treated as a temporary failure, and previous data should
3519         // remain valid.
3520         if (data == null) return;
3521         try {
3522             data.notifyChanged(mCallback);
3523         } catch (RemoteException e) {
3524             Log.e(TAG, "Error notifying ConnectivityService of new capport data", e);
3525         }
3526     }
3527 
3528     /**
3529      * Interface for logging dns results.
3530      */
3531     public interface DnsLogFunc {
3532         /**
3533          * Log function.
3534          */
log(String s)3535         void log(String s);
3536     }
3537 
3538     @Nullable
getTcpSocketTrackerOrNull(Context context, Network network)3539     private static TcpSocketTracker getTcpSocketTrackerOrNull(Context context, Network network) {
3540         return ((Dependencies.DEFAULT.getDeviceConfigPropertyInt(
3541                 NAMESPACE_CONNECTIVITY,
3542                 CONFIG_DATA_STALL_EVALUATION_TYPE,
3543                 DEFAULT_DATA_STALL_EVALUATION_TYPES)
3544                 & DATA_STALL_EVALUATION_TYPE_TCP) != 0)
3545                     ? new TcpSocketTracker(new TcpSocketTracker.Dependencies(context), network)
3546                     : null;
3547     }
3548 
3549     @Nullable
initDnsStallDetectorIfRequired(int type, int threshold)3550     private DnsStallDetector initDnsStallDetectorIfRequired(int type, int threshold) {
3551         return ((type & DATA_STALL_EVALUATION_TYPE_DNS) != 0)
3552                 ? new DnsStallDetector(threshold) : null;
3553     }
3554 
getCaptivePortalApiUrl(LinkProperties lp)3555     private static Uri getCaptivePortalApiUrl(LinkProperties lp) {
3556         return NetworkInformationShimImpl.newInstance().getCaptivePortalApiUrl(lp);
3557     }
3558 }
3559