blob: 935b79546d63e688a0294d7a1b1b9c635f525edc [file] [log] [blame]
Erik Kline79868f82017-01-21 14:33:56 +09001/*
2 * Copyright (C) 2017 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
17package com.android.server.connectivity.tethering;
18
Erik Kline54f2f372017-05-15 21:11:47 +090019import static android.content.Context.TELEPHONY_SERVICE;
Erik Kline1e543512017-03-09 11:44:11 +090020import static android.net.ConnectivityManager.TYPE_ETHERNET;
Erik Kline79868f82017-01-21 14:33:56 +090021import static android.net.ConnectivityManager.TYPE_MOBILE;
22import static android.net.ConnectivityManager.TYPE_MOBILE_DUN;
23import static android.net.ConnectivityManager.TYPE_MOBILE_HIPRI;
Remi NGUYEN VANe3bb5c52018-06-12 15:57:04 +090024import static android.provider.Settings.Global.TETHER_ENABLE_LEGACY_DHCP_SERVER;
25
Erik Klinee0f34032018-02-28 15:01:35 +090026import static com.android.internal.R.array.config_mobile_hotspot_provision_app;
27import static com.android.internal.R.array.config_tether_bluetooth_regexs;
28import static com.android.internal.R.array.config_tether_dhcp_range;
Erik Klinee0f34032018-02-28 15:01:35 +090029import static com.android.internal.R.array.config_tether_upstream_types;
markchien0b595072019-01-08 23:52:21 +080030import static com.android.internal.R.array.config_tether_usb_regexs;
Erik Klinee0f34032018-02-28 15:01:35 +090031import static com.android.internal.R.array.config_tether_wifi_regexs;
Erik Kline72302902018-06-14 17:36:40 +090032import static com.android.internal.R.bool.config_tether_upstream_automatic;
Erik Klinee0f34032018-02-28 15:01:35 +090033import static com.android.internal.R.string.config_mobile_hotspot_provision_app_no_ui;
Erik Kline79868f82017-01-21 14:33:56 +090034
Remi NGUYEN VANe3bb5c52018-06-12 15:57:04 +090035import android.content.ContentResolver;
Erik Kline79868f82017-01-21 14:33:56 +090036import android.content.Context;
37import android.content.res.Resources;
Erik Kline9db1b542017-03-16 14:10:27 +090038import android.net.ConnectivityManager;
Erik Klinee93ed612018-04-10 07:01:16 +000039import android.net.util.SharedLog;
Remi NGUYEN VANe3bb5c52018-06-12 15:57:04 +090040import android.provider.Settings;
markchien0b595072019-01-08 23:52:21 +080041import android.telephony.SubscriptionManager;
Erik Klinee0f34032018-02-28 15:01:35 +090042import android.telephony.TelephonyManager;
43import android.text.TextUtils;
Erik Kline79868f82017-01-21 14:33:56 +090044
Chalard Jean918a68b2018-01-19 17:00:47 +090045import com.android.internal.annotations.VisibleForTesting;
46
Erik Kline9db1b542017-03-16 14:10:27 +090047import java.io.PrintWriter;
Erik Kline79868f82017-01-21 14:33:56 +090048import java.util.ArrayList;
49import java.util.Arrays;
50import java.util.Collection;
Erik Kline9db1b542017-03-16 14:10:27 +090051import java.util.StringJoiner;
Erik Kline79868f82017-01-21 14:33:56 +090052
53
54/**
55 * A utility class to encapsulate the various tethering configuration elements.
56 *
57 * This configuration data includes elements describing upstream properties
58 * (preferred and required types of upstream connectivity as well as default
59 * DNS servers to use if none are available) and downstream properties (such
60 * as regular expressions use to match suitable downstream interfaces and the
61 * DHCPv4 ranges to use).
62 *
63 * @hide
64 */
65public class TetheringConfiguration {
66 private static final String TAG = TetheringConfiguration.class.getSimpleName();
67
Erik Klinee0f34032018-02-28 15:01:35 +090068 private static final String[] EMPTY_STRING_ARRAY = new String[0];
69
Remi NGUYEN VANe3bb5c52018-06-12 15:57:04 +090070 // Default ranges used for the legacy DHCP server.
Erik Kline79868f82017-01-21 14:33:56 +090071 // USB is 192.168.42.1 and 255.255.255.0
72 // Wifi is 192.168.43.1 and 255.255.255.0
73 // BT is limited to max default of 5 connections. 192.168.44.1 to 192.168.48.1
74 // with 255.255.255.0
75 // P2P is 192.168.49.1 and 255.255.255.0
Remi NGUYEN VANe3bb5c52018-06-12 15:57:04 +090076 private static final String[] LEGACY_DHCP_DEFAULT_RANGE = {
Erik Kline79868f82017-01-21 14:33:56 +090077 "192.168.42.2", "192.168.42.254", "192.168.43.2", "192.168.43.254",
78 "192.168.44.2", "192.168.44.254", "192.168.45.2", "192.168.45.254",
79 "192.168.46.2", "192.168.46.254", "192.168.47.2", "192.168.47.254",
80 "192.168.48.2", "192.168.48.254", "192.168.49.2", "192.168.49.254",
81 };
82
83 private final String[] DEFAULT_IPV4_DNS = {"8.8.4.4", "8.8.8.8"};
84
85 public final String[] tetherableUsbRegexs;
86 public final String[] tetherableWifiRegexs;
87 public final String[] tetherableBluetoothRegexs;
88 public final boolean isDunRequired;
Erik Kline72302902018-06-14 17:36:40 +090089 public final boolean chooseUpstreamAutomatically;
Erik Kline79868f82017-01-21 14:33:56 +090090 public final Collection<Integer> preferredUpstreamIfaceTypes;
Remi NGUYEN VANe3bb5c52018-06-12 15:57:04 +090091 public final String[] legacyDhcpRanges;
Erik Kline79868f82017-01-21 14:33:56 +090092 public final String[] defaultIPv4DNS;
Remi NGUYEN VANe3bb5c52018-06-12 15:57:04 +090093 public final boolean enableLegacyDhcpServer;
Erik Kline79868f82017-01-21 14:33:56 +090094
Erik Klinee0f34032018-02-28 15:01:35 +090095 public final String[] provisioningApp;
96 public final String provisioningAppNoUi;
97
markchien0b595072019-01-08 23:52:21 +080098 public final int subId;
99
100 public TetheringConfiguration(Context ctx, SharedLog log, int id) {
Erik Kline6bd74532017-05-19 10:10:41 +0900101 final SharedLog configLog = log.forSubComponent("config");
102
markchien0b595072019-01-08 23:52:21 +0800103 subId = id;
104 Resources res = getResources(ctx, subId);
105
106 tetherableUsbRegexs = getResourceStringArray(res, config_tether_usb_regexs);
Erik Kline9e225542017-06-08 17:48:48 +0900107 // TODO: Evaluate deleting this altogether now that Wi-Fi always passes
108 // us an interface name. Careful consideration needs to be given to
109 // implications for Settings and for provisioning checks.
markchien0b595072019-01-08 23:52:21 +0800110 tetherableWifiRegexs = getResourceStringArray(res, config_tether_wifi_regexs);
111 tetherableBluetoothRegexs = getResourceStringArray(res, config_tether_bluetooth_regexs);
Erik Kline79868f82017-01-21 14:33:56 +0900112
markchiendb3a2362018-10-05 12:36:08 +0800113 isDunRequired = checkDunRequired(ctx);
Erik Kline6bd74532017-05-19 10:10:41 +0900114
markchien0b595072019-01-08 23:52:21 +0800115 chooseUpstreamAutomatically = getResourceBoolean(res, config_tether_upstream_automatic);
markchiendb3a2362018-10-05 12:36:08 +0800116 preferredUpstreamIfaceTypes = getUpstreamIfaceTypes(res, isDunRequired);
Erik Kline79868f82017-01-21 14:33:56 +0900117
markchien0b595072019-01-08 23:52:21 +0800118 legacyDhcpRanges = getLegacyDhcpRanges(res);
Erik Kline79868f82017-01-21 14:33:56 +0900119 defaultIPv4DNS = copy(DEFAULT_IPV4_DNS);
Remi NGUYEN VANe3bb5c52018-06-12 15:57:04 +0900120 enableLegacyDhcpServer = getEnableLegacyDhcpServer(ctx);
Erik Kline6bd74532017-05-19 10:10:41 +0900121
markchien0b595072019-01-08 23:52:21 +0800122 provisioningApp = getResourceStringArray(res, config_mobile_hotspot_provision_app);
123 provisioningAppNoUi = getProvisioningAppNoUi(res);
Erik Klinee0f34032018-02-28 15:01:35 +0900124
Erik Kline6bd74532017-05-19 10:10:41 +0900125 configLog.log(toString());
Erik Kline79868f82017-01-21 14:33:56 +0900126 }
127
128 public boolean isUsb(String iface) {
129 return matchesDownstreamRegexs(iface, tetherableUsbRegexs);
130 }
131
132 public boolean isWifi(String iface) {
133 return matchesDownstreamRegexs(iface, tetherableWifiRegexs);
134 }
135
136 public boolean isBluetooth(String iface) {
137 return matchesDownstreamRegexs(iface, tetherableBluetoothRegexs);
138 }
139
Erik Klinee0f34032018-02-28 15:01:35 +0900140 public boolean hasMobileHotspotProvisionApp() {
141 return !TextUtils.isEmpty(provisioningAppNoUi);
142 }
143
Erik Kline9db1b542017-03-16 14:10:27 +0900144 public void dump(PrintWriter pw) {
markchien0b595072019-01-08 23:52:21 +0800145 pw.print("subId: ");
146 pw.println(subId);
147
Erik Kline9db1b542017-03-16 14:10:27 +0900148 dumpStringArray(pw, "tetherableUsbRegexs", tetherableUsbRegexs);
149 dumpStringArray(pw, "tetherableWifiRegexs", tetherableWifiRegexs);
150 dumpStringArray(pw, "tetherableBluetoothRegexs", tetherableBluetoothRegexs);
151
152 pw.print("isDunRequired: ");
153 pw.println(isDunRequired);
154
Erik Kline72302902018-06-14 17:36:40 +0900155 pw.print("chooseUpstreamAutomatically: ");
156 pw.println(chooseUpstreamAutomatically);
Erik Kline6bd74532017-05-19 10:10:41 +0900157 dumpStringArray(pw, "preferredUpstreamIfaceTypes",
158 preferredUpstreamNames(preferredUpstreamIfaceTypes));
Erik Kline9db1b542017-03-16 14:10:27 +0900159
Remi NGUYEN VANe3bb5c52018-06-12 15:57:04 +0900160 dumpStringArray(pw, "legacyDhcpRanges", legacyDhcpRanges);
Erik Kline9db1b542017-03-16 14:10:27 +0900161 dumpStringArray(pw, "defaultIPv4DNS", defaultIPv4DNS);
Erik Klinee0f34032018-02-28 15:01:35 +0900162
163 dumpStringArray(pw, "provisioningApp", provisioningApp);
164 pw.print("provisioningAppNoUi: ");
165 pw.println(provisioningAppNoUi);
Remi NGUYEN VAN9865cc12018-08-30 16:49:52 +0900166
167 pw.print("enableLegacyDhcpServer: ");
168 pw.println(enableLegacyDhcpServer);
Erik Kline9db1b542017-03-16 14:10:27 +0900169 }
170
Erik Kline6bd74532017-05-19 10:10:41 +0900171 public String toString() {
172 final StringJoiner sj = new StringJoiner(" ");
markchien0b595072019-01-08 23:52:21 +0800173 sj.add(String.format("subId:%d", subId));
Erik Kline6bd74532017-05-19 10:10:41 +0900174 sj.add(String.format("tetherableUsbRegexs:%s", makeString(tetherableUsbRegexs)));
175 sj.add(String.format("tetherableWifiRegexs:%s", makeString(tetherableWifiRegexs)));
176 sj.add(String.format("tetherableBluetoothRegexs:%s",
177 makeString(tetherableBluetoothRegexs)));
178 sj.add(String.format("isDunRequired:%s", isDunRequired));
Erik Kline72302902018-06-14 17:36:40 +0900179 sj.add(String.format("chooseUpstreamAutomatically:%s", chooseUpstreamAutomatically));
Erik Kline6bd74532017-05-19 10:10:41 +0900180 sj.add(String.format("preferredUpstreamIfaceTypes:%s",
181 makeString(preferredUpstreamNames(preferredUpstreamIfaceTypes))));
Erik Klinee0f34032018-02-28 15:01:35 +0900182 sj.add(String.format("provisioningApp:%s", makeString(provisioningApp)));
183 sj.add(String.format("provisioningAppNoUi:%s", provisioningAppNoUi));
Remi NGUYEN VAN9865cc12018-08-30 16:49:52 +0900184 sj.add(String.format("enableLegacyDhcpServer:%s", enableLegacyDhcpServer));
Erik Kline6bd74532017-05-19 10:10:41 +0900185 return String.format("TetheringConfiguration{%s}", sj.toString());
186 }
187
Erik Kline9db1b542017-03-16 14:10:27 +0900188 private static void dumpStringArray(PrintWriter pw, String label, String[] values) {
189 pw.print(label);
190 pw.print(": ");
191
192 if (values != null) {
193 final StringJoiner sj = new StringJoiner(", ", "[", "]");
194 for (String value : values) { sj.add(value); }
195 pw.print(sj.toString());
196 } else {
197 pw.print("null");
198 }
199
200 pw.println();
201 }
202
Erik Kline6bd74532017-05-19 10:10:41 +0900203 private static String makeString(String[] strings) {
Erik Klinee0f34032018-02-28 15:01:35 +0900204 if (strings == null) return "null";
Erik Kline6bd74532017-05-19 10:10:41 +0900205 final StringJoiner sj = new StringJoiner(",", "[", "]");
206 for (String s : strings) sj.add(s);
207 return sj.toString();
208 }
209
210 private static String[] preferredUpstreamNames(Collection<Integer> upstreamTypes) {
211 String[] upstreamNames = null;
212
213 if (upstreamTypes != null) {
214 upstreamNames = new String[upstreamTypes.size()];
215 int i = 0;
216 for (Integer netType : upstreamTypes) {
217 upstreamNames[i] = ConnectivityManager.getNetworkTypeName(netType);
218 i++;
219 }
220 }
221
222 return upstreamNames;
223 }
224
markchiendb3a2362018-10-05 12:36:08 +0800225 /** Check whether dun is required. */
226 public static boolean checkDunRequired(Context ctx) {
Erik Kline54f2f372017-05-15 21:11:47 +0900227 final TelephonyManager tm = (TelephonyManager) ctx.getSystemService(TELEPHONY_SERVICE);
markchiendb3a2362018-10-05 12:36:08 +0800228 return (tm != null) ? tm.getTetherApnRequired() : false;
Erik Kline79868f82017-01-21 14:33:56 +0900229 }
230
markchiendb3a2362018-10-05 12:36:08 +0800231 private static Collection<Integer> getUpstreamIfaceTypes(Resources res, boolean dunRequired) {
markchien0b595072019-01-08 23:52:21 +0800232 final int[] ifaceTypes = res.getIntArray(config_tether_upstream_types);
Erik Kline79868f82017-01-21 14:33:56 +0900233 final ArrayList<Integer> upstreamIfaceTypes = new ArrayList<>(ifaceTypes.length);
234 for (int i : ifaceTypes) {
235 switch (i) {
236 case TYPE_MOBILE:
237 case TYPE_MOBILE_HIPRI:
markchiendb3a2362018-10-05 12:36:08 +0800238 if (dunRequired) continue;
Erik Kline79868f82017-01-21 14:33:56 +0900239 break;
240 case TYPE_MOBILE_DUN:
markchiendb3a2362018-10-05 12:36:08 +0800241 if (!dunRequired) continue;
Erik Kline79868f82017-01-21 14:33:56 +0900242 break;
243 }
244 upstreamIfaceTypes.add(i);
245 }
246
247 // Fix up upstream interface types for DUN or mobile. NOTE: independent
markchiendb3a2362018-10-05 12:36:08 +0800248 // of the value of |dunRequired|, cell data of one form or another is
Erik Kline79868f82017-01-21 14:33:56 +0900249 // *always* an upstream, regardless of the upstream interface types
250 // specified by configuration resources.
markchiendb3a2362018-10-05 12:36:08 +0800251 if (dunRequired) {
Erik Kline1e543512017-03-09 11:44:11 +0900252 appendIfNotPresent(upstreamIfaceTypes, TYPE_MOBILE_DUN);
Jayachandran C58059822017-05-17 23:53:59 -0700253 } else {
Jayachandran C58059822017-05-17 23:53:59 -0700254 // Do not modify if a cellular interface type is already present in the
255 // upstream interface types. Add TYPE_MOBILE and TYPE_MOBILE_HIPRI if no
256 // cellular interface types are found in the upstream interface types.
markchiendb3a2362018-10-05 12:36:08 +0800257 // This preserves backwards compatibility and prevents the DUN and default
258 // mobile types incorrectly appearing together, which could happen on
259 // previous releases in the common case where checkDunRequired returned
260 // DUN_UNSPECIFIED.
261 if (!containsOneOf(upstreamIfaceTypes, TYPE_MOBILE, TYPE_MOBILE_HIPRI)) {
Jayachandran C58059822017-05-17 23:53:59 -0700262 upstreamIfaceTypes.add(TYPE_MOBILE);
263 upstreamIfaceTypes.add(TYPE_MOBILE_HIPRI);
264 }
Erik Kline79868f82017-01-21 14:33:56 +0900265 }
266
Erik Kline1e543512017-03-09 11:44:11 +0900267 // Always make sure our good friend Ethernet is present.
268 // TODO: consider unilaterally forcing this at the front.
269 prependIfNotPresent(upstreamIfaceTypes, TYPE_ETHERNET);
270
Erik Kline79868f82017-01-21 14:33:56 +0900271 return upstreamIfaceTypes;
272 }
273
274 private static boolean matchesDownstreamRegexs(String iface, String[] regexs) {
275 for (String regex : regexs) {
276 if (iface.matches(regex)) return true;
277 }
278 return false;
279 }
280
markchien0b595072019-01-08 23:52:21 +0800281 private static String[] getLegacyDhcpRanges(Resources res) {
282 final String[] fromResource = getResourceStringArray(res, config_tether_dhcp_range);
Erik Kline79868f82017-01-21 14:33:56 +0900283 if ((fromResource.length > 0) && (fromResource.length % 2 == 0)) {
284 return fromResource;
285 }
Remi NGUYEN VANe3bb5c52018-06-12 15:57:04 +0900286 return copy(LEGACY_DHCP_DEFAULT_RANGE);
Erik Kline79868f82017-01-21 14:33:56 +0900287 }
288
markchien0b595072019-01-08 23:52:21 +0800289 private static String getProvisioningAppNoUi(Resources res) {
Erik Klinee0f34032018-02-28 15:01:35 +0900290 try {
markchien0b595072019-01-08 23:52:21 +0800291 return res.getString(config_mobile_hotspot_provision_app_no_ui);
Erik Klinee0f34032018-02-28 15:01:35 +0900292 } catch (Resources.NotFoundException e) {
293 return "";
294 }
295 }
296
markchien0b595072019-01-08 23:52:21 +0800297 private static boolean getResourceBoolean(Resources res, int resId) {
Erik Kline72302902018-06-14 17:36:40 +0900298 try {
markchien0b595072019-01-08 23:52:21 +0800299 return res.getBoolean(resId);
Erik Kline72302902018-06-14 17:36:40 +0900300 } catch (Resources.NotFoundException e404) {
301 return false;
302 }
303 }
304
markchien0b595072019-01-08 23:52:21 +0800305 private static String[] getResourceStringArray(Resources res, int resId) {
Erik Klinee0f34032018-02-28 15:01:35 +0900306 try {
markchien0b595072019-01-08 23:52:21 +0800307 final String[] strArray = res.getStringArray(resId);
Erik Klinee0f34032018-02-28 15:01:35 +0900308 return (strArray != null) ? strArray : EMPTY_STRING_ARRAY;
309 } catch (Resources.NotFoundException e404) {
310 return EMPTY_STRING_ARRAY;
311 }
312 }
313
Remi NGUYEN VANe3bb5c52018-06-12 15:57:04 +0900314 private static boolean getEnableLegacyDhcpServer(Context ctx) {
Remi NGUYEN VANe3bb5c52018-06-12 15:57:04 +0900315 final ContentResolver cr = ctx.getContentResolver();
Remi NGUYEN VANe0d8c0e2018-09-26 18:11:48 +0900316 final int intVal = Settings.Global.getInt(cr, TETHER_ENABLE_LEGACY_DHCP_SERVER, 0);
Remi NGUYEN VANe3bb5c52018-06-12 15:57:04 +0900317 return intVal != 0;
318 }
319
markchien0b595072019-01-08 23:52:21 +0800320 private Resources getResources(Context ctx, int subId) {
321 if (subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
322 return getResourcesForSubIdWrapper(ctx, subId);
323 } else {
324 return ctx.getResources();
325 }
326 }
327
328 @VisibleForTesting
329 protected Resources getResourcesForSubIdWrapper(Context ctx, int subId) {
330 return SubscriptionManager.getResourcesForSubId(ctx, subId);
331 }
332
Erik Kline79868f82017-01-21 14:33:56 +0900333 private static String[] copy(String[] strarray) {
334 return Arrays.copyOf(strarray, strarray.length);
335 }
Erik Kline1e543512017-03-09 11:44:11 +0900336
337 private static void prependIfNotPresent(ArrayList<Integer> list, int value) {
338 if (list.contains(value)) return;
339 list.add(0, value);
340 }
341
342 private static void appendIfNotPresent(ArrayList<Integer> list, int value) {
343 if (list.contains(value)) return;
344 list.add(value);
345 }
346
347 private static boolean containsOneOf(ArrayList<Integer> list, Integer... values) {
348 for (Integer value : values) {
349 if (list.contains(value)) return true;
350 }
351 return false;
352 }
Erik Kline79868f82017-01-21 14:33:56 +0900353}