blob: a626b5b33012f1f64ebcb82205870c2667a58c0f [file] [log] [blame]
Felipe Leme50a235e2016-01-15 18:37:06 -08001/*
2 * Copyright (C) 2016 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.net;
18
Felipe Leme9982b302016-03-01 13:40:31 -080019import static android.net.NetworkPolicyManager.POLICY_NONE;
20import static android.net.NetworkPolicyManager.POLICY_REJECT_METERED_BACKGROUND;
Felipe Lemeb1a65ee2016-02-08 10:12:01 -080021import static android.net.wifi.WifiInfo.removeDoubleQuotes;
22import static com.android.server.net.NetworkPolicyManagerService.newWifiPolicy;
Felipe Lemede4e8e32016-02-02 18:55:22 -080023import static com.android.server.net.NetworkPolicyManagerService.TAG;
Felipe Leme50a235e2016-01-15 18:37:06 -080024
Felipe Lemede4e8e32016-02-02 18:55:22 -080025import java.io.PrintWriter;
26import java.util.ArrayList;
Felipe Lemeb1a65ee2016-02-08 10:12:01 -080027import java.util.HashSet;
Felipe Lemede4e8e32016-02-02 18:55:22 -080028import java.util.List;
Felipe Lemeb1a65ee2016-02-08 10:12:01 -080029import java.util.Set;
Felipe Lemede4e8e32016-02-02 18:55:22 -080030
Felipe Lemeb1a65ee2016-02-08 10:12:01 -080031import android.content.Context;
Felipe Leme50a235e2016-01-15 18:37:06 -080032import android.net.INetworkPolicyManager;
Felipe Lemede4e8e32016-02-02 18:55:22 -080033import android.net.NetworkPolicy;
Felipe Lemeb1a65ee2016-02-08 10:12:01 -080034import android.net.NetworkTemplate;
35import android.net.wifi.WifiConfiguration;
36import android.net.wifi.WifiManager;
Felipe Leme9778f762016-01-27 14:46:39 -080037import android.os.Binder;
Felipe Leme50a235e2016-01-15 18:37:06 -080038import android.os.RemoteException;
39import android.os.ShellCommand;
Felipe Lemede4e8e32016-02-02 18:55:22 -080040import android.util.Log;
Felipe Leme50a235e2016-01-15 18:37:06 -080041
Felipe Lemede4e8e32016-02-02 18:55:22 -080042class NetworkPolicyManagerShellCommand extends ShellCommand {
Felipe Leme50a235e2016-01-15 18:37:06 -080043
Felipe Lemeb1a65ee2016-02-08 10:12:01 -080044 private final INetworkPolicyManager mInterface;
45 private final WifiManager mWifiManager;
Felipe Leme50a235e2016-01-15 18:37:06 -080046
Felipe Lemeb1a65ee2016-02-08 10:12:01 -080047 NetworkPolicyManagerShellCommand(Context context, INetworkPolicyManager service) {
Felipe Leme50a235e2016-01-15 18:37:06 -080048 mInterface = service;
Felipe Lemeb1a65ee2016-02-08 10:12:01 -080049 mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
Felipe Leme50a235e2016-01-15 18:37:06 -080050 }
51
52 @Override
53 public int onCommand(String cmd) {
54 if (cmd == null) {
55 return handleDefaultCommands(cmd);
56 }
57 final PrintWriter pw = getOutPrintWriter();
58 try {
59 switch(cmd) {
60 case "get":
61 return runGet();
62 case "set":
63 return runSet();
64 case "list":
65 return runList();
66 case "add":
67 return runAdd();
68 case "remove":
69 return runRemove();
70 default:
71 return handleDefaultCommands(cmd);
72 }
73 } catch (RemoteException e) {
74 pw.println("Remote exception: " + e);
75 }
76 return -1;
77 }
78
79 @Override
80 public void onHelp() {
81 final PrintWriter pw = getOutPrintWriter();
82 pw.println("Network policy manager (netpolicy) commands:");
83 pw.println(" help");
84 pw.println(" Print this help text.");
85 pw.println("");
Felipe Leme50a235e2016-01-15 18:37:06 -080086 pw.println(" add restrict-background-whitelist UID");
87 pw.println(" Adds a UID to the whitelist for restrict background usage.");
Felipe Leme9982b302016-03-01 13:40:31 -080088 pw.println(" add restrict-background-blacklist UID");
89 pw.println(" Adds a UID to the blacklist for restrict background usage.");
Felipe Lemede4e8e32016-02-02 18:55:22 -080090 pw.println(" get metered-network ID");
91 pw.println(" Checks whether the given non-mobile network is metered or not.");
92 pw.println(" get restrict-background");
93 pw.println(" Gets the global restrict background usage status.");
94 pw.println(" list metered-networks [BOOLEAN]");
95 pw.println(" Lists all non-mobile networks and whether they are metered or not.");
96 pw.println(" If a boolean argument is passed, filters just the metered (or unmetered)");
97 pw.println(" networks.");
98 pw.println(" list restrict-background-whitelist");
99 pw.println(" Lists UIDs that are whitelisted for restrict background usage.");
Felipe Leme9982b302016-03-01 13:40:31 -0800100 pw.println(" list restrict-background-blacklist");
101 pw.println(" Lists UIDs that are blacklisted for restrict background usage.");
Felipe Leme50a235e2016-01-15 18:37:06 -0800102 pw.println(" remove restrict-background-whitelist UID");
103 pw.println(" Removes a UID from the whitelist for restrict background usage.");
Felipe Leme9982b302016-03-01 13:40:31 -0800104 pw.println(" remove restrict-background-blacklist UID");
105 pw.println(" Removes a UID from the blacklist for restrict background usage.");
Felipe Lemede4e8e32016-02-02 18:55:22 -0800106 pw.println(" set metered-network ID BOOLEAN");
107 pw.println(" Toggles whether the given non-mobile network is metered.");
108 pw.println(" set restrict-background BOOLEAN");
109 pw.println(" Sets the global restrict background usage status.");
Felipe Leme50a235e2016-01-15 18:37:06 -0800110 }
111
112 private int runGet() throws RemoteException {
113 final PrintWriter pw = getOutPrintWriter();
114 final String type = getNextArg();
115 if (type == null) {
116 pw.println("Error: didn't specify type of data to get");
117 return -1;
118 }
119 switch(type) {
Felipe Lemede4e8e32016-02-02 18:55:22 -0800120 case "metered-network":
Felipe Lemeb1a65ee2016-02-08 10:12:01 -0800121 return getMeteredWifiNetwork();
Felipe Leme50a235e2016-01-15 18:37:06 -0800122 case "restrict-background":
Felipe Leme9778f762016-01-27 14:46:39 -0800123 return getRestrictBackground();
Felipe Leme50a235e2016-01-15 18:37:06 -0800124 }
125 pw.println("Error: unknown get type '" + type + "'");
126 return -1;
127 }
128
129 private int runSet() throws RemoteException {
130 final PrintWriter pw = getOutPrintWriter();
131 final String type = getNextArg();
132 if (type == null) {
133 pw.println("Error: didn't specify type of data to set");
134 return -1;
135 }
136 switch(type) {
Felipe Lemede4e8e32016-02-02 18:55:22 -0800137 case "metered-network":
Felipe Lemeb1a65ee2016-02-08 10:12:01 -0800138 return setMeteredWifiNetwork();
Felipe Leme50a235e2016-01-15 18:37:06 -0800139 case "restrict-background":
Felipe Leme9778f762016-01-27 14:46:39 -0800140 return setRestrictBackground();
Felipe Leme50a235e2016-01-15 18:37:06 -0800141 }
142 pw.println("Error: unknown set type '" + type + "'");
143 return -1;
144 }
145
146 private int runList() throws RemoteException {
147 final PrintWriter pw = getOutPrintWriter();
148 final String type = getNextArg();
149 if (type == null) {
150 pw.println("Error: didn't specify type of data to list");
151 return -1;
152 }
153 switch(type) {
Felipe Lemede4e8e32016-02-02 18:55:22 -0800154 case "metered-networks":
Felipe Lemeb1a65ee2016-02-08 10:12:01 -0800155 return listMeteredWifiNetworks();
Felipe Leme50a235e2016-01-15 18:37:06 -0800156 case "restrict-background-whitelist":
Felipe Lemeb1a65ee2016-02-08 10:12:01 -0800157 return listRestrictBackgroundWhitelist();
Felipe Leme9982b302016-03-01 13:40:31 -0800158 case "restrict-background-blacklist":
159 return listRestrictBackgroundBlacklist();
Felipe Leme50a235e2016-01-15 18:37:06 -0800160 }
161 pw.println("Error: unknown list type '" + type + "'");
162 return -1;
163 }
164
165 private int runAdd() throws RemoteException {
166 final PrintWriter pw = getOutPrintWriter();
167 final String type = getNextArg();
168 if (type == null) {
169 pw.println("Error: didn't specify type of data to add");
170 return -1;
171 }
172 switch(type) {
173 case "restrict-background-whitelist":
174 return addRestrictBackgroundWhitelist();
Felipe Leme9982b302016-03-01 13:40:31 -0800175 case "restrict-background-blacklist":
176 return addRestrictBackgroundBlacklist();
Felipe Leme50a235e2016-01-15 18:37:06 -0800177 }
178 pw.println("Error: unknown add type '" + type + "'");
179 return -1;
180 }
181
182 private int runRemove() throws RemoteException {
183 final PrintWriter pw = getOutPrintWriter();
184 final String type = getNextArg();
185 if (type == null) {
186 pw.println("Error: didn't specify type of data to remove");
187 return -1;
188 }
189 switch(type) {
190 case "restrict-background-whitelist":
191 return removeRestrictBackgroundWhitelist();
Felipe Leme9982b302016-03-01 13:40:31 -0800192 case "restrict-background-blacklist":
193 return removeRestrictBackgroundBlacklist();
Felipe Leme50a235e2016-01-15 18:37:06 -0800194 }
195 pw.println("Error: unknown remove type '" + type + "'");
196 return -1;
197 }
198
Felipe Lemeb1a65ee2016-02-08 10:12:01 -0800199 private int listRestrictBackgroundWhitelist() throws RemoteException {
Felipe Leme50a235e2016-01-15 18:37:06 -0800200 final PrintWriter pw = getOutPrintWriter();
201 final int[] uids = mInterface.getRestrictBackgroundWhitelistedUids();
202 pw.print("Restrict background whitelisted UIDs: ");
203 if (uids.length == 0) {
204 pw.println("none");
205 } else {
206 for (int i = 0; i < uids.length; i++) {
207 int uid = uids[i];
208 pw.print(uid);
209 pw.print(' ');
210 }
211 }
212 pw.println();
213 return 0;
214 }
215
Felipe Leme9982b302016-03-01 13:40:31 -0800216 private int listRestrictBackgroundBlacklist() throws RemoteException {
217 final PrintWriter pw = getOutPrintWriter();
218
219 final int[] uids = mInterface.getUidsWithPolicy(POLICY_REJECT_METERED_BACKGROUND);
220 pw.print("Restrict background blacklisted UIDs: ");
221 if (uids.length == 0) {
222 pw.println("none");
223 } else {
224 for (int i = 0; i < uids.length; i++) {
225 int uid = uids[i];
226 pw.print(uid);
227 pw.print(' ');
228 }
229 }
230 pw.println();
231 return 0;
232 }
233
Felipe Leme9778f762016-01-27 14:46:39 -0800234 private int getRestrictBackground() throws RemoteException {
Felipe Leme50a235e2016-01-15 18:37:06 -0800235 final PrintWriter pw = getOutPrintWriter();
236 pw.print("Restrict background status: ");
237 pw.println(mInterface.getRestrictBackground() ? "enabled" : "disabled");
238 return 0;
239 }
240
Felipe Leme9778f762016-01-27 14:46:39 -0800241 private int setRestrictBackground() throws RemoteException {
Felipe Leme50a235e2016-01-15 18:37:06 -0800242 final int enabled = getNextBooleanArg();
243 if (enabled < 0) {
244 return enabled;
245 }
Felipe Leme6a05eee2016-02-19 14:43:51 -0800246 mInterface.setRestrictBackground(enabled > 0);
Felipe Leme50a235e2016-01-15 18:37:06 -0800247 return 0;
248 }
249
250 private int addRestrictBackgroundWhitelist() throws RemoteException {
251 final int uid = getUidFromNextArg();
252 if (uid < 0) {
253 return uid;
254 }
Felipe Leme6a05eee2016-02-19 14:43:51 -0800255 mInterface.addRestrictBackgroundWhitelistedUid(uid);
Felipe Leme50a235e2016-01-15 18:37:06 -0800256 return 0;
257 }
258
259 private int removeRestrictBackgroundWhitelist() throws RemoteException {
260 final int uid = getUidFromNextArg();
261 if (uid < 0) {
262 return uid;
263 }
Felipe Leme6a05eee2016-02-19 14:43:51 -0800264 mInterface.removeRestrictBackgroundWhitelistedUid(uid);
Felipe Leme50a235e2016-01-15 18:37:06 -0800265 return 0;
266 }
267
Felipe Leme9982b302016-03-01 13:40:31 -0800268 private int addRestrictBackgroundBlacklist() throws RemoteException {
269 final int uid = getUidFromNextArg();
270 if (uid < 0) {
271 return uid;
272 }
273 mInterface.setUidPolicy(uid, POLICY_REJECT_METERED_BACKGROUND);
274 return 0;
275 }
276
277 private int removeRestrictBackgroundBlacklist() throws RemoteException {
278 final int uid = getUidFromNextArg();
279 if (uid < 0) {
280 return uid;
281 }
282 mInterface.setUidPolicy(uid, POLICY_NONE);
283 return 0;
284 }
285
Felipe Lemeb1a65ee2016-02-08 10:12:01 -0800286 private int listMeteredWifiNetworks() throws RemoteException {
Felipe Lemede4e8e32016-02-02 18:55:22 -0800287 final PrintWriter pw = getOutPrintWriter();
288 final String arg = getNextArg();
289 final Boolean filter = arg == null ? null : Boolean.valueOf(arg);
Felipe Lemeb1a65ee2016-02-08 10:12:01 -0800290 for (NetworkPolicy policy : getWifiPolicies()) {
Felipe Lemede4e8e32016-02-02 18:55:22 -0800291 if (filter != null && filter.booleanValue() != policy.metered) {
292 continue;
293 }
294 pw.print(getNetworkId(policy));
295 pw.print(';');
296 pw.println(policy.metered);
297 }
298 return 0;
299 }
300
Felipe Lemeb1a65ee2016-02-08 10:12:01 -0800301 private int getMeteredWifiNetwork() throws RemoteException {
Felipe Lemede4e8e32016-02-02 18:55:22 -0800302 final PrintWriter pw = getOutPrintWriter();
303 final String id = getNextArg();
304 if (id == null) {
305 pw.println("Error: didn't specify ID");
306 return -1;
307 }
Felipe Lemeb1a65ee2016-02-08 10:12:01 -0800308 final List<NetworkPolicy> policies = getWifiPolicies();
Felipe Lemede4e8e32016-02-02 18:55:22 -0800309 for (NetworkPolicy policy: policies) {
310 if (id.equals(getNetworkId(policy))) {
311 pw.println(policy.metered);
312 return 0;
313 }
314 }
315 return 0;
316 }
317
Felipe Lemeb1a65ee2016-02-08 10:12:01 -0800318 private int setMeteredWifiNetwork() throws RemoteException {
Felipe Lemede4e8e32016-02-02 18:55:22 -0800319 final PrintWriter pw = getOutPrintWriter();
320 final String id = getNextArg();
321 if (id == null) {
322 pw.println("Error: didn't specify ID");
323 return -1;
324 }
325 final String arg = getNextArg();
326 if (arg == null) {
327 pw.println("Error: didn't specify BOOLEAN");
328 return -1;
329 }
330 final boolean metered = Boolean.valueOf(arg);
331 final NetworkPolicy[] policies = mInterface.getNetworkPolicies(null);
332 boolean changed = false;
Felipe Lemeb1a65ee2016-02-08 10:12:01 -0800333 // First try to find a policy with such id
Felipe Lemede4e8e32016-02-02 18:55:22 -0800334 for (NetworkPolicy policy : policies) {
335 if (policy.template.isMatchRuleMobile() || policy.metered == metered) {
336 continue;
337 }
338 final String networkId = getNetworkId(policy);
339 if (id.equals(networkId)) {
340 Log.i(TAG, "Changing " + networkId + " metered status to " + metered);
341 policy.metered = metered;
342 changed = true;
343 }
344 }
345 if (changed) {
346 mInterface.setNetworkPolicies(policies);
Felipe Lemeb1a65ee2016-02-08 10:12:01 -0800347 return 0;
348 }
349 // Policy not found: check if there is a saved wi-fi with such id.
350 for (WifiConfiguration config : mWifiManager.getConfiguredNetworks()) {
351 final String ssid = removeDoubleQuotes(config.SSID);
352 if (id.equals(ssid)) {
353 final NetworkPolicy policy = newPolicy(ssid);
354 Log.i(TAG, "Creating new policy for " + ssid + ": " + policy);
355 final NetworkPolicy[] newPolicies = new NetworkPolicy[policies.length + 1];
356 System.arraycopy(policies, 0, newPolicies, 0, policies.length);
357 newPolicies[newPolicies.length - 1] = policy;
358 mInterface.setNetworkPolicies(newPolicies);
359 }
Felipe Lemede4e8e32016-02-02 18:55:22 -0800360 }
361 return 0;
362 }
363
Felipe Lemeb1a65ee2016-02-08 10:12:01 -0800364 private List<NetworkPolicy> getWifiPolicies() throws RemoteException {
365 // First gets a list of saved wi-fi networks.
366 final List<WifiConfiguration> configs = mWifiManager.getConfiguredNetworks();
367 final Set<String> ssids = new HashSet<>(configs.size());
368 for (WifiConfiguration config : configs) {
369 ssids.add(removeDoubleQuotes(config.SSID));
370 }
371
372 // Then gets the saved policies.
Felipe Lemede4e8e32016-02-02 18:55:22 -0800373 final NetworkPolicy[] policies = mInterface.getNetworkPolicies(null);
Felipe Lemeb1a65ee2016-02-08 10:12:01 -0800374 final List<NetworkPolicy> wifiPolicies = new ArrayList<NetworkPolicy>(policies.length);
Felipe Lemede4e8e32016-02-02 18:55:22 -0800375 for (NetworkPolicy policy: policies) {
376 if (!policy.template.isMatchRuleMobile()) {
Felipe Lemeb1a65ee2016-02-08 10:12:01 -0800377 wifiPolicies.add(policy);
378 final String netId = getNetworkId(policy);
379 ssids.remove(netId);
Felipe Lemede4e8e32016-02-02 18:55:22 -0800380 }
381 }
Felipe Lemeb1a65ee2016-02-08 10:12:01 -0800382 // Finally, creates new default policies for saved WI-FIs not policied yet.
383 for (String ssid : ssids) {
384 final NetworkPolicy policy = newPolicy(ssid);
385 wifiPolicies.add(policy);
386 }
387 return wifiPolicies;
388 }
389
390 private NetworkPolicy newPolicy(String ssid) {
391 final NetworkTemplate template = NetworkTemplate.buildTemplateWifi(ssid);
392 final NetworkPolicy policy = newWifiPolicy(template, false);
393 return policy;
Felipe Lemede4e8e32016-02-02 18:55:22 -0800394 }
395
396 private String getNetworkId(NetworkPolicy policy) {
Felipe Lemeb1a65ee2016-02-08 10:12:01 -0800397 return removeDoubleQuotes(policy.template.getNetworkId());
Felipe Lemede4e8e32016-02-02 18:55:22 -0800398 }
399
Felipe Leme50a235e2016-01-15 18:37:06 -0800400 private int getNextBooleanArg() {
401 final PrintWriter pw = getOutPrintWriter();
402 final String arg = getNextArg();
403 if (arg == null) {
404 pw.println("Error: didn't specify BOOLEAN");
405 return -1;
406 }
407 return Boolean.valueOf(arg) ? 1 : 0;
408 }
409
410 private int getUidFromNextArg() {
411 final PrintWriter pw = getOutPrintWriter();
412 final String arg = getNextArg();
413 if (arg == null) {
414 pw.println("Error: didn't specify UID");
415 return -1;
416 }
417 try {
418 return Integer.parseInt(arg);
419 } catch (NumberFormatException e) {
420 pw.println("Error: UID (" + arg + ") should be a number");
421 return -2;
422 }
423 }
424}