blob: 3fe9b0d745388992e68ea633061c2611f66ad480 [file] [log] [blame]
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -07001/*
2 * Copyright (C) 2011 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 android.net;
18
Jeff Sharkey4414cea2011-06-24 17:05:24 -070019import static android.content.pm.PackageManager.GET_SIGNATURES;
Jeff Sharkeycd2ca402011-06-10 15:14:07 -070020
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060021import android.annotation.SystemService;
Sudheer Shankae359c3d2017-02-22 18:41:29 -080022import android.app.ActivityManager;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070023import android.content.Context;
Jeff Sharkey14711eb2011-06-15 10:29:17 -070024import android.content.Intent;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070025import android.content.pm.PackageManager;
26import android.content.pm.PackageManager.NameNotFoundException;
27import android.content.pm.Signature;
Jeff Sharkey43d2a172017-07-12 10:50:42 -060028import android.net.wifi.WifiConfiguration;
29import android.net.wifi.WifiInfo;
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070030import android.os.RemoteException;
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -070031import android.os.UserHandle;
Jeff Sharkey53313d72017-07-13 16:47:32 -060032import android.telephony.SubscriptionPlan;
Felipe Leme46c4fc32016-05-04 09:21:43 -070033import android.util.DebugUtils;
Jeff Sharkey53313d72017-07-13 16:47:32 -060034import android.util.Pair;
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070035
Jeff Sharkey4414cea2011-06-24 17:05:24 -070036import com.google.android.collect.Sets;
37
Jeff Sharkey53313d72017-07-13 16:47:32 -060038import java.time.ZonedDateTime;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070039import java.util.HashSet;
Jeff Sharkey53313d72017-07-13 16:47:32 -060040import java.util.Iterator;
Jeff Sharkey1b861272011-05-22 00:34:52 -070041
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070042/**
43 * Manager for creating and modifying network policy rules.
44 *
45 * {@hide}
46 */
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060047@SystemService(Context.NETWORK_POLICY_SERVICE)
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070048public class NetworkPolicyManager {
49
Felipe Leme46b451f2016-08-19 08:46:17 -070050 /* POLICY_* are masks and can be ORed, although currently they are not.*/
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070051 /** No specific network policy, use system default. */
52 public static final int POLICY_NONE = 0x0;
Jeff Sharkeyfdfef572011-06-16 15:07:48 -070053 /** Reject network usage on metered networks when application in background. */
54 public static final int POLICY_REJECT_METERED_BACKGROUND = 0x1;
Felipe Leme46b451f2016-08-19 08:46:17 -070055 /** Allow metered network use in the background even when in data usage save mode. */
56 public static final int POLICY_ALLOW_METERED_BACKGROUND = 0x4;
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -070057
Felipe Leme46c4fc32016-05-04 09:21:43 -070058 /*
59 * Rules defining whether an uid has access to a network given its type (metered / non-metered).
60 *
61 * These rules are bits and can be used in bitmask operations; in particular:
62 * - rule & RULE_MASK_METERED: returns the metered-networks status.
63 * - rule & RULE_MASK_ALL: returns the all-networks status.
64 *
65 * The RULE_xxx_ALL rules applies to all networks (metered or non-metered), but on
66 * metered networks, the RULE_xxx_METERED rules should be checked first. For example,
67 * if the device is on Battery Saver Mode and Data Saver Mode simulatenously, and a uid
68 * is whitelisted for the former but not the latter, its status would be
69 * RULE_REJECT_METERED | RULE_ALLOW_ALL, meaning it could have access to non-metered
70 * networks but not to metered networks.
71 *
72 * See network-policy-restrictions.md for more info.
73 */
74 /** No specific rule was set */
75 public static final int RULE_NONE = 0;
Felipe Leme70c57c22016-03-29 10:45:13 -070076 /** Allow traffic on metered networks. */
Felipe Leme46c4fc32016-05-04 09:21:43 -070077 public static final int RULE_ALLOW_METERED = 1 << 0;
Felipe Leme70c57c22016-03-29 10:45:13 -070078 /** Temporarily allow traffic on metered networks because app is on foreground. */
Felipe Leme46c4fc32016-05-04 09:21:43 -070079 public static final int RULE_TEMPORARY_ALLOW_METERED = 1 << 1;
80 /** Reject traffic on metered networks. */
81 public static final int RULE_REJECT_METERED = 1 << 2;
82 /** Network traffic should be allowed on all networks (metered or non-metered), although
83 * metered-network restrictions could still apply. */
84 public static final int RULE_ALLOW_ALL = 1 << 5;
85 /** Reject traffic on all networks. */
86 public static final int RULE_REJECT_ALL = 1 << 6;
87 /** Mask used to get the {@code RULE_xxx_METERED} rules */
88 public static final int MASK_METERED_NETWORKS = 0b00001111;
89 /** Mask used to get the {@code RULE_xxx_ALL} rules */
90 public static final int MASK_ALL_NETWORKS = 0b11110000;
Amith Yamasani15e472352015-04-24 19:06:07 -070091
92 public static final int FIREWALL_RULE_DEFAULT = 0;
93 public static final int FIREWALL_RULE_ALLOW = 1;
94 public static final int FIREWALL_RULE_DENY = 2;
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070095
Xiaohui Chenb41c9f72015-06-17 15:55:37 -070096 public static final int FIREWALL_TYPE_WHITELIST = 0;
97 public static final int FIREWALL_TYPE_BLACKLIST = 1;
98
99 public static final int FIREWALL_CHAIN_NONE = 0;
100 public static final int FIREWALL_CHAIN_DOZABLE = 1;
101 public static final int FIREWALL_CHAIN_STANDBY = 2;
Felipe Leme011b98f2016-02-10 17:28:31 -0800102 public static final int FIREWALL_CHAIN_POWERSAVE = 3;
Xiaohui Chenb41c9f72015-06-17 15:55:37 -0700103
104 public static final String FIREWALL_CHAIN_NAME_NONE = "none";
105 public static final String FIREWALL_CHAIN_NAME_DOZABLE = "dozable";
106 public static final String FIREWALL_CHAIN_NAME_STANDBY = "standby";
Felipe Leme011b98f2016-02-10 17:28:31 -0800107 public static final String FIREWALL_CHAIN_NAME_POWERSAVE = "powersave";
Xiaohui Chenb41c9f72015-06-17 15:55:37 -0700108
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700109 private static final boolean ALLOW_PLATFORM_APP_POLICY = true;
110
Jeff Sharkey14711eb2011-06-15 10:29:17 -0700111 /**
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700112 * {@link Intent} extra that indicates which {@link NetworkTemplate} rule it
113 * applies to.
Jeff Sharkey14711eb2011-06-15 10:29:17 -0700114 */
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700115 public static final String EXTRA_NETWORK_TEMPLATE = "android.net.NETWORK_TEMPLATE";
Jeff Sharkey14711eb2011-06-15 10:29:17 -0700116
Svet Ganov16a16892015-04-16 10:32:04 -0700117 private final Context mContext;
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -0700118 private INetworkPolicyManager mService;
119
Svet Ganov16a16892015-04-16 10:32:04 -0700120 public NetworkPolicyManager(Context context, INetworkPolicyManager service) {
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -0700121 if (service == null) {
122 throw new IllegalArgumentException("missing INetworkPolicyManager");
123 }
Svet Ganov16a16892015-04-16 10:32:04 -0700124 mContext = context;
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -0700125 mService = service;
126 }
127
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700128 public static NetworkPolicyManager from(Context context) {
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700129 return (NetworkPolicyManager) context.getSystemService(Context.NETWORK_POLICY_SERVICE);
130 }
131
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -0700132 /**
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700133 * Set policy flags for specific UID.
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -0700134 *
Felipe Leme8546a442016-08-23 09:38:20 -0700135 * @param policy should be {@link #POLICY_NONE} or any combination of {@code POLICY_} flags,
136 * although it is not validated.
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -0700137 */
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700138 public void setUidPolicy(int uid, int policy) {
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -0700139 try {
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700140 mService.setUidPolicy(uid, policy);
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -0700141 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700142 throw e.rethrowFromSystemServer();
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -0700143 }
144 }
145
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700146 /**
Felipe Leme8546a442016-08-23 09:38:20 -0700147 * Add policy flags for specific UID.
148 *
149 * <p>The given policy bits will be set for the uid.
150 *
151 * @param policy should be {@link #POLICY_NONE} or any combination of {@code POLICY_} flags,
152 * although it is not validated.
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700153 */
154 public void addUidPolicy(int uid, int policy) {
155 try {
156 mService.addUidPolicy(uid, policy);
157 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700158 throw e.rethrowFromSystemServer();
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700159 }
160 }
161
162 /**
Felipe Leme8546a442016-08-23 09:38:20 -0700163 * Clear/remove policy flags for specific UID.
164 *
165 * <p>The given policy bits will be set for the uid.
166 *
167 * @param policy should be {@link #POLICY_NONE} or any combination of {@code POLICY_} flags,
168 * although it is not validated.
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700169 */
170 public void removeUidPolicy(int uid, int policy) {
171 try {
172 mService.removeUidPolicy(uid, policy);
173 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700174 throw e.rethrowFromSystemServer();
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700175 }
176 }
177
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700178 public int getUidPolicy(int uid) {
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -0700179 try {
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700180 return mService.getUidPolicy(uid);
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -0700181 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700182 throw e.rethrowFromSystemServer();
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -0700183 }
184 }
Jeff Sharkeycd2ca402011-06-10 15:14:07 -0700185
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700186 public int[] getUidsWithPolicy(int policy) {
Jeff Sharkey854b2b12012-04-13 16:03:40 -0700187 try {
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700188 return mService.getUidsWithPolicy(policy);
Jeff Sharkey854b2b12012-04-13 16:03:40 -0700189 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700190 throw e.rethrowFromSystemServer();
Jeff Sharkey854b2b12012-04-13 16:03:40 -0700191 }
192 }
193
Jeff Sharkey1a303952011-06-16 13:04:20 -0700194 public void registerListener(INetworkPolicyListener listener) {
195 try {
196 mService.registerListener(listener);
197 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700198 throw e.rethrowFromSystemServer();
Jeff Sharkey1a303952011-06-16 13:04:20 -0700199 }
200 }
201
202 public void unregisterListener(INetworkPolicyListener listener) {
203 try {
204 mService.unregisterListener(listener);
205 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700206 throw e.rethrowFromSystemServer();
Jeff Sharkey1a303952011-06-16 13:04:20 -0700207 }
208 }
209
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700210 public void setNetworkPolicies(NetworkPolicy[] policies) {
211 try {
212 mService.setNetworkPolicies(policies);
213 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700214 throw e.rethrowFromSystemServer();
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700215 }
216 }
217
218 public NetworkPolicy[] getNetworkPolicies() {
219 try {
Svet Ganov16a16892015-04-16 10:32:04 -0700220 return mService.getNetworkPolicies(mContext.getOpPackageName());
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700221 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700222 throw e.rethrowFromSystemServer();
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700223 }
224 }
225
226 public void setRestrictBackground(boolean restrictBackground) {
227 try {
228 mService.setRestrictBackground(restrictBackground);
229 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700230 throw e.rethrowFromSystemServer();
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700231 }
232 }
233
234 public boolean getRestrictBackground() {
235 try {
236 return mService.getRestrictBackground();
237 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700238 throw e.rethrowFromSystemServer();
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700239 }
240 }
241
Jeff Sharkeycd2ca402011-06-10 15:14:07 -0700242 /**
Stuart Scott984dc852015-03-30 13:17:11 -0700243 * Resets network policy settings back to factory defaults.
244 *
245 * @hide
246 */
247 public void factoryReset(String subscriber) {
Stuart Scottf1fb3972015-04-02 18:00:02 -0700248 try {
249 mService.factoryReset(subscriber);
250 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700251 throw e.rethrowFromSystemServer();
Stuart Scott984dc852015-03-30 13:17:11 -0700252 }
253 }
254
Jeff Sharkeycd2ca402011-06-10 15:14:07 -0700255 /** {@hide} */
Jeff Sharkey53313d72017-07-13 16:47:32 -0600256 public static Iterator<Pair<ZonedDateTime, ZonedDateTime>> cycleIterator(NetworkPolicy policy) {
257 return SubscriptionPlan.convert(policy).cycleIterator();
Jeff Sharkeycd2ca402011-06-10 15:14:07 -0700258 }
259
Jeff Sharkey497e4432011-06-14 17:27:29 -0700260 /**
261 * Check if given UID can have a {@link #setUidPolicy(int, int)} defined,
262 * usually to protect critical system services.
263 */
Jeff Sharkey8a8b5812012-03-21 18:13:36 -0700264 @Deprecated
Jeff Sharkey497e4432011-06-14 17:27:29 -0700265 public static boolean isUidValidForPolicy(Context context, int uid) {
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700266 // first, quick-reject non-applications
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700267 if (!UserHandle.isApp(uid)) {
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700268 return false;
269 }
270
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700271 if (!ALLOW_PLATFORM_APP_POLICY) {
272 final PackageManager pm = context.getPackageManager();
273 final HashSet<Signature> systemSignature;
274 try {
275 systemSignature = Sets.newHashSet(
276 pm.getPackageInfo("android", GET_SIGNATURES).signatures);
277 } catch (NameNotFoundException e) {
278 throw new RuntimeException("problem finding system signature", e);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700279 }
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700280
281 try {
282 // reject apps signed with platform cert
283 for (String packageName : pm.getPackagesForUid(uid)) {
284 final HashSet<Signature> packageSignature = Sets.newHashSet(
285 pm.getPackageInfo(packageName, GET_SIGNATURES).signatures);
286 if (packageSignature.containsAll(systemSignature)) {
287 return false;
288 }
289 }
290 } catch (NameNotFoundException e) {
291 }
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700292 }
293
294 // nothing found above; we can apply policy to UID
295 return true;
Jeff Sharkey497e4432011-06-14 17:27:29 -0700296 }
Felipe Leme46c4fc32016-05-04 09:21:43 -0700297
Felipe Lemeb146f762016-08-19 09:52:16 -0700298 /**
Felipe Leme46c4fc32016-05-04 09:21:43 -0700299 * @hide
300 */
301 public static String uidRulesToString(int uidRules) {
302 final StringBuilder string = new StringBuilder().append(uidRules).append(" (");
303 if (uidRules == RULE_NONE) {
304 string.append("NONE");
305 } else {
306 string.append(DebugUtils.flagsToString(NetworkPolicyManager.class, "RULE_", uidRules));
307 }
308 string.append(")");
309 return string.toString();
310 }
Felipe Lemeb146f762016-08-19 09:52:16 -0700311
312 /**
313 * @hide
314 */
315 public static String uidPoliciesToString(int uidPolicies) {
316 final StringBuilder string = new StringBuilder().append(uidPolicies).append(" (");
317 if (uidPolicies == POLICY_NONE) {
318 string.append("NONE");
319 } else {
320 string.append(DebugUtils.flagsToString(NetworkPolicyManager.class,
321 "POLICY_", uidPolicies));
322 }
323 string.append(")");
324 return string.toString();
325 }
Sudheer Shankae359c3d2017-02-22 18:41:29 -0800326
327 /**
328 * Returns true if {@param procState} is considered foreground and as such will be allowed
329 * to access network when the device is idle or in battery saver mode. Otherwise, false.
330 */
331 public static boolean isProcStateAllowedWhileIdleOrPowerSaveMode(int procState) {
332 return procState <= ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE;
333 }
334
335 /**
336 * Returns true if {@param procState} is considered foreground and as such will be allowed
337 * to access network when the device is in data saver mode. Otherwise, false.
338 */
339 public static boolean isProcStateAllowedWhileOnRestrictBackground(int procState) {
340 return procState <= ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE;
341 }
Jeff Sharkey43d2a172017-07-12 10:50:42 -0600342
343 public static String resolveNetworkId(WifiConfiguration config) {
344 return WifiInfo.removeDoubleQuotes(config.isPasspoint()
345 ? config.providerFriendlyName : config.SSID);
346 }
347
348 public static String resolveNetworkId(String ssid) {
349 return WifiInfo.removeDoubleQuotes(ssid);
350 }
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -0700351}