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