blob: 8738424e1a507f427915e49faf9341732894c835 [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 Sharkey8fc27e82012-04-04 20:40:58 -070020import static android.net.NetworkPolicy.CYCLE_NONE;
Jeff Sharkeycd2ca402011-06-10 15:14:07 -070021import static android.text.format.Time.MONTH_DAY;
22
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 Sharkeyd5cdd592011-05-03 20:27:17 -070028import android.os.RemoteException;
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -070029import android.os.UserHandle;
Jeff Sharkeycd2ca402011-06-10 15:14:07 -070030import android.text.format.Time;
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070031
Jeff Sharkey4414cea2011-06-24 17:05:24 -070032import com.google.android.collect.Sets;
33
Jeff Sharkey1b861272011-05-22 00:34:52 -070034import java.io.PrintWriter;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070035import java.util.HashSet;
Jeff Sharkey1b861272011-05-22 00:34:52 -070036
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070037/**
38 * Manager for creating and modifying network policy rules.
39 *
40 * {@hide}
41 */
42public class NetworkPolicyManager {
43
Amith Yamasani15e472352015-04-24 19:06:07 -070044 /* POLICY_* are masks and can be ORed */
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070045 /** No specific network policy, use system default. */
46 public static final int POLICY_NONE = 0x0;
Jeff Sharkeyfdfef572011-06-16 15:07:48 -070047 /** Reject network usage on metered networks when application in background. */
48 public static final int POLICY_REJECT_METERED_BACKGROUND = 0x1;
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -070049 /** Allow network use (metered or not) in the background in battery save mode. */
50 public static final int POLICY_ALLOW_BACKGROUND_BATTERY_SAVE = 0x2;
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -070051
Amith Yamasani15e472352015-04-24 19:06:07 -070052 /* RULE_* are not masks and they must be exclusive */
Jeff Sharkeydc988062015-09-14 10:09:47 -070053 public static final int RULE_UNKNOWN = -1;
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -070054 /** All network traffic should be allowed. */
Jeff Sharkeydc988062015-09-14 10:09:47 -070055 public static final int RULE_ALLOW_ALL = 0;
Jeff Sharkeyfdfef572011-06-16 15:07:48 -070056 /** Reject traffic on metered networks. */
Jeff Sharkeydc988062015-09-14 10:09:47 -070057 public static final int RULE_REJECT_METERED = 1;
Amith Yamasani15e472352015-04-24 19:06:07 -070058 /** Reject traffic on all networks. */
Jeff Sharkeydc988062015-09-14 10:09:47 -070059 public static final int RULE_REJECT_ALL = 2;
Amith Yamasani15e472352015-04-24 19:06:07 -070060
61 public static final int FIREWALL_RULE_DEFAULT = 0;
62 public static final int FIREWALL_RULE_ALLOW = 1;
63 public static final int FIREWALL_RULE_DENY = 2;
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070064
Xiaohui Chenb41c9f72015-06-17 15:55:37 -070065 public static final int FIREWALL_TYPE_WHITELIST = 0;
66 public static final int FIREWALL_TYPE_BLACKLIST = 1;
67
68 public static final int FIREWALL_CHAIN_NONE = 0;
69 public static final int FIREWALL_CHAIN_DOZABLE = 1;
70 public static final int FIREWALL_CHAIN_STANDBY = 2;
Felipe Leme011b98f2016-02-10 17:28:31 -080071 public static final int FIREWALL_CHAIN_POWERSAVE = 3;
Xiaohui Chenb41c9f72015-06-17 15:55:37 -070072
73 public static final String FIREWALL_CHAIN_NAME_NONE = "none";
74 public static final String FIREWALL_CHAIN_NAME_DOZABLE = "dozable";
75 public static final String FIREWALL_CHAIN_NAME_STANDBY = "standby";
Felipe Leme011b98f2016-02-10 17:28:31 -080076 public static final String FIREWALL_CHAIN_NAME_POWERSAVE = "powersave";
Xiaohui Chenb41c9f72015-06-17 15:55:37 -070077
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -070078 private static final boolean ALLOW_PLATFORM_APP_POLICY = true;
79
Jeff Sharkey14711eb2011-06-15 10:29:17 -070080 /**
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -070081 * {@link Intent} extra that indicates which {@link NetworkTemplate} rule it
82 * applies to.
Jeff Sharkey14711eb2011-06-15 10:29:17 -070083 */
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -070084 public static final String EXTRA_NETWORK_TEMPLATE = "android.net.NETWORK_TEMPLATE";
Jeff Sharkey14711eb2011-06-15 10:29:17 -070085
Svet Ganov16a16892015-04-16 10:32:04 -070086 private final Context mContext;
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070087 private INetworkPolicyManager mService;
88
Svet Ganov16a16892015-04-16 10:32:04 -070089 public NetworkPolicyManager(Context context, INetworkPolicyManager service) {
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070090 if (service == null) {
91 throw new IllegalArgumentException("missing INetworkPolicyManager");
92 }
Svet Ganov16a16892015-04-16 10:32:04 -070093 mContext = context;
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070094 mService = service;
95 }
96
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070097 public static NetworkPolicyManager from(Context context) {
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070098 return (NetworkPolicyManager) context.getSystemService(Context.NETWORK_POLICY_SERVICE);
99 }
100
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -0700101 /**
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700102 * Set policy flags for specific UID.
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -0700103 *
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700104 * @param policy {@link #POLICY_NONE} or combination of flags like
Amith Yamasani15e472352015-04-24 19:06:07 -0700105 * {@link #POLICY_REJECT_METERED_BACKGROUND} or {@link #POLICY_ALLOW_BACKGROUND_BATTERY_SAVE}.
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -0700106 */
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700107 public void setUidPolicy(int uid, int policy) {
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -0700108 try {
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700109 mService.setUidPolicy(uid, policy);
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -0700110 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700111 throw e.rethrowFromSystemServer();
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -0700112 }
113 }
114
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700115 /**
116 * Add policy flags for specific UID. The given policy bits will be set for
117 * the uid. Policy flags may be either
118 * {@link #POLICY_REJECT_METERED_BACKGROUND} or {@link #POLICY_ALLOW_BACKGROUND_BATTERY_SAVE}.
119 */
120 public void addUidPolicy(int uid, int policy) {
121 try {
122 mService.addUidPolicy(uid, policy);
123 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700124 throw e.rethrowFromSystemServer();
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700125 }
126 }
127
128 /**
129 * Clear/remove policy flags for specific UID. The given policy bits will be set for
130 * the uid. Policy flags may be either
131 * {@link #POLICY_REJECT_METERED_BACKGROUND} or {@link #POLICY_ALLOW_BACKGROUND_BATTERY_SAVE}.
132 */
133 public void removeUidPolicy(int uid, int policy) {
134 try {
135 mService.removeUidPolicy(uid, policy);
136 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700137 throw e.rethrowFromSystemServer();
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700138 }
139 }
140
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700141 public int getUidPolicy(int uid) {
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -0700142 try {
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700143 return mService.getUidPolicy(uid);
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -0700144 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700145 throw e.rethrowFromSystemServer();
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -0700146 }
147 }
Jeff Sharkeycd2ca402011-06-10 15:14:07 -0700148
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700149 public int[] getUidsWithPolicy(int policy) {
Jeff Sharkey854b2b12012-04-13 16:03:40 -0700150 try {
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700151 return mService.getUidsWithPolicy(policy);
Jeff Sharkey854b2b12012-04-13 16:03:40 -0700152 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700153 throw e.rethrowFromSystemServer();
Jeff Sharkey854b2b12012-04-13 16:03:40 -0700154 }
155 }
156
Jeff Sharkey1a303952011-06-16 13:04:20 -0700157 public void registerListener(INetworkPolicyListener listener) {
158 try {
159 mService.registerListener(listener);
160 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700161 throw e.rethrowFromSystemServer();
Jeff Sharkey1a303952011-06-16 13:04:20 -0700162 }
163 }
164
165 public void unregisterListener(INetworkPolicyListener listener) {
166 try {
167 mService.unregisterListener(listener);
168 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700169 throw e.rethrowFromSystemServer();
Jeff Sharkey1a303952011-06-16 13:04:20 -0700170 }
171 }
172
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700173 public void setNetworkPolicies(NetworkPolicy[] policies) {
174 try {
175 mService.setNetworkPolicies(policies);
176 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700177 throw e.rethrowFromSystemServer();
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700178 }
179 }
180
181 public NetworkPolicy[] getNetworkPolicies() {
182 try {
Svet Ganov16a16892015-04-16 10:32:04 -0700183 return mService.getNetworkPolicies(mContext.getOpPackageName());
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700184 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700185 throw e.rethrowFromSystemServer();
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700186 }
187 }
188
189 public void setRestrictBackground(boolean restrictBackground) {
190 try {
191 mService.setRestrictBackground(restrictBackground);
192 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700193 throw e.rethrowFromSystemServer();
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700194 }
195 }
196
197 public boolean getRestrictBackground() {
198 try {
199 return mService.getRestrictBackground();
200 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700201 throw e.rethrowFromSystemServer();
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700202 }
203 }
204
Jeff Sharkeycd2ca402011-06-10 15:14:07 -0700205 /**
Stuart Scott984dc852015-03-30 13:17:11 -0700206 * Resets network policy settings back to factory defaults.
207 *
208 * @hide
209 */
210 public void factoryReset(String subscriber) {
Stuart Scottf1fb3972015-04-02 18:00:02 -0700211 try {
212 mService.factoryReset(subscriber);
213 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700214 throw e.rethrowFromSystemServer();
Stuart Scott984dc852015-03-30 13:17:11 -0700215 }
216 }
217
218 /**
Jeff Sharkeycd2ca402011-06-10 15:14:07 -0700219 * Compute the last cycle boundary for the given {@link NetworkPolicy}. For
220 * example, if cycle day is 20th, and today is June 15th, it will return May
221 * 20th. When cycle day doesn't exist in current month, it snaps to the 1st
222 * of following month.
223 *
224 * @hide
225 */
226 public static long computeLastCycleBoundary(long currentTime, NetworkPolicy policy) {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700227 if (policy.cycleDay == CYCLE_NONE) {
228 throw new IllegalArgumentException("Unable to compute boundary without cycleDay");
229 }
230
Jeff Sharkey9bf31502012-03-09 17:07:21 -0800231 final Time now = new Time(policy.cycleTimezone);
Jeff Sharkeycd2ca402011-06-10 15:14:07 -0700232 now.set(currentTime);
233
234 // first, find cycle boundary for current month
235 final Time cycle = new Time(now);
236 cycle.hour = cycle.minute = cycle.second = 0;
237 snapToCycleDay(cycle, policy.cycleDay);
238
239 if (Time.compare(cycle, now) >= 0) {
240 // cycle boundary is beyond now, use last cycle boundary; start by
241 // pushing ourselves squarely into last month.
242 final Time lastMonth = new Time(now);
243 lastMonth.hour = lastMonth.minute = lastMonth.second = 0;
244 lastMonth.monthDay = 1;
245 lastMonth.month -= 1;
246 lastMonth.normalize(true);
247
248 cycle.set(lastMonth);
249 snapToCycleDay(cycle, policy.cycleDay);
250 }
251
252 return cycle.toMillis(true);
253 }
254
255 /** {@hide} */
256 public static long computeNextCycleBoundary(long currentTime, NetworkPolicy policy) {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700257 if (policy.cycleDay == CYCLE_NONE) {
258 throw new IllegalArgumentException("Unable to compute boundary without cycleDay");
259 }
260
Jeff Sharkey9bf31502012-03-09 17:07:21 -0800261 final Time now = new Time(policy.cycleTimezone);
Jeff Sharkeycd2ca402011-06-10 15:14:07 -0700262 now.set(currentTime);
263
264 // first, find cycle boundary for current month
265 final Time cycle = new Time(now);
266 cycle.hour = cycle.minute = cycle.second = 0;
267 snapToCycleDay(cycle, policy.cycleDay);
268
269 if (Time.compare(cycle, now) <= 0) {
270 // cycle boundary is before now, use next cycle boundary; start by
271 // pushing ourselves squarely into next month.
272 final Time nextMonth = new Time(now);
273 nextMonth.hour = nextMonth.minute = nextMonth.second = 0;
274 nextMonth.monthDay = 1;
275 nextMonth.month += 1;
276 nextMonth.normalize(true);
277
278 cycle.set(nextMonth);
279 snapToCycleDay(cycle, policy.cycleDay);
280 }
281
282 return cycle.toMillis(true);
283 }
284
285 /**
286 * Snap to the cycle day for the current month given; when cycle day doesn't
Jeff Sharkey9bf31502012-03-09 17:07:21 -0800287 * exist, it snaps to last second of current month.
Jeff Sharkeycd2ca402011-06-10 15:14:07 -0700288 *
289 * @hide
290 */
291 public static void snapToCycleDay(Time time, int cycleDay) {
292 if (cycleDay > time.getActualMaximum(MONTH_DAY)) {
Jeff Sharkeyaf82ea22011-08-04 15:38:48 -0700293 // cycle day isn't valid this month; snap to last second of month
Jeff Sharkeycd2ca402011-06-10 15:14:07 -0700294 time.month += 1;
295 time.monthDay = 1;
Jeff Sharkeyaf82ea22011-08-04 15:38:48 -0700296 time.second = -1;
Jeff Sharkeycd2ca402011-06-10 15:14:07 -0700297 } else {
298 time.monthDay = cycleDay;
299 }
300 time.normalize(true);
301 }
302
Jeff Sharkey497e4432011-06-14 17:27:29 -0700303 /**
304 * Check if given UID can have a {@link #setUidPolicy(int, int)} defined,
305 * usually to protect critical system services.
306 */
Jeff Sharkey8a8b5812012-03-21 18:13:36 -0700307 @Deprecated
Jeff Sharkey497e4432011-06-14 17:27:29 -0700308 public static boolean isUidValidForPolicy(Context context, int uid) {
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700309 // first, quick-reject non-applications
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700310 if (!UserHandle.isApp(uid)) {
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700311 return false;
312 }
313
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700314 if (!ALLOW_PLATFORM_APP_POLICY) {
315 final PackageManager pm = context.getPackageManager();
316 final HashSet<Signature> systemSignature;
317 try {
318 systemSignature = Sets.newHashSet(
319 pm.getPackageInfo("android", GET_SIGNATURES).signatures);
320 } catch (NameNotFoundException e) {
321 throw new RuntimeException("problem finding system signature", e);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700322 }
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700323
324 try {
325 // reject apps signed with platform cert
326 for (String packageName : pm.getPackagesForUid(uid)) {
327 final HashSet<Signature> packageSignature = Sets.newHashSet(
328 pm.getPackageInfo(packageName, GET_SIGNATURES).signatures);
329 if (packageSignature.containsAll(systemSignature)) {
330 return false;
331 }
332 }
333 } catch (NameNotFoundException e) {
334 }
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700335 }
336
337 // nothing found above; we can apply policy to UID
338 return true;
Jeff Sharkey497e4432011-06-14 17:27:29 -0700339 }
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -0700340}