blob: 8ced1c22020be648b6b6f788a755fce8a02ac570 [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 Leme57e3d312016-08-23 14:42:52 -070019import static android.net.NetworkPolicyManager.POLICY_ALLOW_METERED_BACKGROUND;
Felipe Leme9982b302016-03-01 13:40:31 -080020import static android.net.NetworkPolicyManager.POLICY_NONE;
21import static android.net.NetworkPolicyManager.POLICY_REJECT_METERED_BACKGROUND;
Felipe Lemeb1a65ee2016-02-08 10:12:01 -080022import static android.net.wifi.WifiInfo.removeDoubleQuotes;
Felipe Leme57e3d312016-08-23 14:42:52 -070023
Felipe Lemeb1a65ee2016-02-08 10:12:01 -080024import static com.android.server.net.NetworkPolicyManagerService.newWifiPolicy;
Felipe Lemede4e8e32016-02-02 18:55:22 -080025import static com.android.server.net.NetworkPolicyManagerService.TAG;
Felipe Leme50a235e2016-01-15 18:37:06 -080026
Felipe Lemede4e8e32016-02-02 18:55:22 -080027import java.io.PrintWriter;
28import java.util.ArrayList;
Felipe Lemeb1a65ee2016-02-08 10:12:01 -080029import java.util.HashSet;
Felipe Lemede4e8e32016-02-02 18:55:22 -080030import java.util.List;
Felipe Lemeb1a65ee2016-02-08 10:12:01 -080031import java.util.Set;
Felipe Lemede4e8e32016-02-02 18:55:22 -080032
Felipe Lemeb1a65ee2016-02-08 10:12:01 -080033import android.content.Context;
Felipe Leme50a235e2016-01-15 18:37:06 -080034import android.net.INetworkPolicyManager;
Felipe Lemede4e8e32016-02-02 18:55:22 -080035import android.net.NetworkPolicy;
Felipe Lemeb1a65ee2016-02-08 10:12:01 -080036import android.net.NetworkTemplate;
37import android.net.wifi.WifiConfiguration;
38import android.net.wifi.WifiManager;
Felipe Leme50a235e2016-01-15 18:37:06 -080039import android.os.RemoteException;
40import android.os.ShellCommand;
Felipe Lemede4e8e32016-02-02 18:55:22 -080041import android.util.Log;
Felipe Leme50a235e2016-01-15 18:37:06 -080042
Felipe Lemede4e8e32016-02-02 18:55:22 -080043class NetworkPolicyManagerShellCommand extends ShellCommand {
Felipe Leme50a235e2016-01-15 18:37:06 -080044
Felipe Lemeb1a65ee2016-02-08 10:12:01 -080045 private final INetworkPolicyManager mInterface;
46 private final WifiManager mWifiManager;
Felipe Leme50a235e2016-01-15 18:37:06 -080047
Felipe Lemeb1a65ee2016-02-08 10:12:01 -080048 NetworkPolicyManagerShellCommand(Context context, INetworkPolicyManager service) {
Felipe Leme50a235e2016-01-15 18:37:06 -080049 mInterface = service;
Felipe Lemeb1a65ee2016-02-08 10:12:01 -080050 mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
Felipe Leme50a235e2016-01-15 18:37:06 -080051 }
52
53 @Override
54 public int onCommand(String cmd) {
55 if (cmd == null) {
56 return handleDefaultCommands(cmd);
57 }
58 final PrintWriter pw = getOutPrintWriter();
59 try {
60 switch(cmd) {
61 case "get":
62 return runGet();
63 case "set":
64 return runSet();
65 case "list":
66 return runList();
67 case "add":
68 return runAdd();
69 case "remove":
70 return runRemove();
71 default:
72 return handleDefaultCommands(cmd);
73 }
74 } catch (RemoteException e) {
75 pw.println("Remote exception: " + e);
76 }
77 return -1;
78 }
79
80 @Override
81 public void onHelp() {
82 final PrintWriter pw = getOutPrintWriter();
83 pw.println("Network policy manager (netpolicy) commands:");
84 pw.println(" help");
85 pw.println(" Print this help text.");
86 pw.println("");
Felipe Leme50a235e2016-01-15 18:37:06 -080087 pw.println(" add restrict-background-whitelist UID");
88 pw.println(" Adds a UID to the whitelist for restrict background usage.");
Felipe Leme9982b302016-03-01 13:40:31 -080089 pw.println(" add restrict-background-blacklist UID");
90 pw.println(" Adds a UID to the blacklist for restrict background usage.");
Felipe Lemede4e8e32016-02-02 18:55:22 -080091 pw.println(" get restrict-background");
92 pw.println(" Gets the global restrict background usage status.");
Felipe Lemeb9aee90a2016-03-30 16:14:54 -070093 pw.println(" list wifi-networks [BOOLEAN]");
94 pw.println(" Lists all saved wifi networks and whether they are metered or not.");
Felipe Lemede4e8e32016-02-02 18:55:22 -080095 pw.println(" If a boolean argument is passed, filters just the metered (or unmetered)");
96 pw.println(" networks.");
97 pw.println(" list restrict-background-whitelist");
98 pw.println(" Lists UIDs that are whitelisted for restrict background usage.");
Felipe Leme9982b302016-03-01 13:40:31 -080099 pw.println(" list restrict-background-blacklist");
100 pw.println(" Lists UIDs that are blacklisted for restrict background usage.");
Felipe Leme50a235e2016-01-15 18:37:06 -0800101 pw.println(" remove restrict-background-whitelist UID");
102 pw.println(" Removes a UID from the whitelist for restrict background usage.");
Felipe Leme9982b302016-03-01 13:40:31 -0800103 pw.println(" remove restrict-background-blacklist UID");
104 pw.println(" Removes a UID from the blacklist for restrict background usage.");
Felipe Lemede4e8e32016-02-02 18:55:22 -0800105 pw.println(" set metered-network ID BOOLEAN");
Felipe Lemeb9aee90a2016-03-30 16:14:54 -0700106 pw.println(" Toggles whether the given wi-fi network is metered.");
Felipe Lemede4e8e32016-02-02 18:55:22 -0800107 pw.println(" set restrict-background BOOLEAN");
108 pw.println(" Sets the global restrict background usage status.");
Felipe Leme50a235e2016-01-15 18:37:06 -0800109 }
110
111 private int runGet() throws RemoteException {
112 final PrintWriter pw = getOutPrintWriter();
113 final String type = getNextArg();
114 if (type == null) {
115 pw.println("Error: didn't specify type of data to get");
116 return -1;
117 }
118 switch(type) {
119 case "restrict-background":
Felipe Leme9778f762016-01-27 14:46:39 -0800120 return getRestrictBackground();
Felipe Leme50a235e2016-01-15 18:37:06 -0800121 }
122 pw.println("Error: unknown get type '" + type + "'");
123 return -1;
124 }
125
126 private int runSet() throws RemoteException {
127 final PrintWriter pw = getOutPrintWriter();
128 final String type = getNextArg();
129 if (type == null) {
130 pw.println("Error: didn't specify type of data to set");
131 return -1;
132 }
133 switch(type) {
Felipe Lemede4e8e32016-02-02 18:55:22 -0800134 case "metered-network":
Felipe Lemeb1a65ee2016-02-08 10:12:01 -0800135 return setMeteredWifiNetwork();
Felipe Leme50a235e2016-01-15 18:37:06 -0800136 case "restrict-background":
Felipe Leme9778f762016-01-27 14:46:39 -0800137 return setRestrictBackground();
Felipe Leme50a235e2016-01-15 18:37:06 -0800138 }
139 pw.println("Error: unknown set type '" + type + "'");
140 return -1;
141 }
142
143 private int runList() throws RemoteException {
144 final PrintWriter pw = getOutPrintWriter();
145 final String type = getNextArg();
146 if (type == null) {
147 pw.println("Error: didn't specify type of data to list");
148 return -1;
149 }
150 switch(type) {
Felipe Lemeb9aee90a2016-03-30 16:14:54 -0700151 case "wifi-networks":
152 return listWifiNetworks();
Felipe Leme50a235e2016-01-15 18:37:06 -0800153 case "restrict-background-whitelist":
Felipe Lemeb1a65ee2016-02-08 10:12:01 -0800154 return listRestrictBackgroundWhitelist();
Felipe Leme9982b302016-03-01 13:40:31 -0800155 case "restrict-background-blacklist":
156 return listRestrictBackgroundBlacklist();
Felipe Leme50a235e2016-01-15 18:37:06 -0800157 }
158 pw.println("Error: unknown list type '" + type + "'");
159 return -1;
160 }
161
162 private int runAdd() throws RemoteException {
163 final PrintWriter pw = getOutPrintWriter();
164 final String type = getNextArg();
165 if (type == null) {
166 pw.println("Error: didn't specify type of data to add");
167 return -1;
168 }
169 switch(type) {
170 case "restrict-background-whitelist":
171 return addRestrictBackgroundWhitelist();
Felipe Leme9982b302016-03-01 13:40:31 -0800172 case "restrict-background-blacklist":
173 return addRestrictBackgroundBlacklist();
Felipe Leme50a235e2016-01-15 18:37:06 -0800174 }
175 pw.println("Error: unknown add type '" + type + "'");
176 return -1;
177 }
178
179 private int runRemove() throws RemoteException {
180 final PrintWriter pw = getOutPrintWriter();
181 final String type = getNextArg();
182 if (type == null) {
183 pw.println("Error: didn't specify type of data to remove");
184 return -1;
185 }
186 switch(type) {
187 case "restrict-background-whitelist":
188 return removeRestrictBackgroundWhitelist();
Felipe Leme9982b302016-03-01 13:40:31 -0800189 case "restrict-background-blacklist":
190 return removeRestrictBackgroundBlacklist();
Felipe Leme50a235e2016-01-15 18:37:06 -0800191 }
192 pw.println("Error: unknown remove type '" + type + "'");
193 return -1;
194 }
195
Felipe Leme57e3d312016-08-23 14:42:52 -0700196 private int listUidPolicies(String msg, int policy) throws RemoteException {
Felipe Leme50a235e2016-01-15 18:37:06 -0800197 final PrintWriter pw = getOutPrintWriter();
Felipe Leme57e3d312016-08-23 14:42:52 -0700198 final int[] uids = mInterface.getUidsWithPolicy(policy);
199 pw.print(msg); pw.print(": ");
Felipe Leme50a235e2016-01-15 18:37:06 -0800200 if (uids.length == 0) {
201 pw.println("none");
202 } else {
203 for (int i = 0; i < uids.length; i++) {
204 int uid = uids[i];
205 pw.print(uid);
206 pw.print(' ');
207 }
208 }
209 pw.println();
210 return 0;
211 }
212
Felipe Leme57e3d312016-08-23 14:42:52 -0700213 private int listRestrictBackgroundWhitelist() throws RemoteException {
214 return listUidPolicies("Restrict background whitelisted UIDs",
215 POLICY_ALLOW_METERED_BACKGROUND);
216 }
Felipe Leme9982b302016-03-01 13:40:31 -0800217
Felipe Leme57e3d312016-08-23 14:42:52 -0700218 private int listRestrictBackgroundBlacklist() throws RemoteException {
219 return listUidPolicies("Restrict background blacklisted UIDs",
220 POLICY_REJECT_METERED_BACKGROUND);
Felipe Leme9982b302016-03-01 13:40:31 -0800221 }
222
Felipe Leme9778f762016-01-27 14:46:39 -0800223 private int getRestrictBackground() throws RemoteException {
Felipe Leme50a235e2016-01-15 18:37:06 -0800224 final PrintWriter pw = getOutPrintWriter();
225 pw.print("Restrict background status: ");
226 pw.println(mInterface.getRestrictBackground() ? "enabled" : "disabled");
227 return 0;
228 }
229
Felipe Leme9778f762016-01-27 14:46:39 -0800230 private int setRestrictBackground() throws RemoteException {
Felipe Leme50a235e2016-01-15 18:37:06 -0800231 final int enabled = getNextBooleanArg();
232 if (enabled < 0) {
233 return enabled;
234 }
Felipe Leme6a05eee2016-02-19 14:43:51 -0800235 mInterface.setRestrictBackground(enabled > 0);
Felipe Leme50a235e2016-01-15 18:37:06 -0800236 return 0;
237 }
238
Felipe Leme57e3d312016-08-23 14:42:52 -0700239 private int setUidPolicy(int policy) throws RemoteException {
Felipe Leme50a235e2016-01-15 18:37:06 -0800240 final int uid = getUidFromNextArg();
241 if (uid < 0) {
242 return uid;
243 }
Felipe Leme57e3d312016-08-23 14:42:52 -0700244 mInterface.setUidPolicy(uid, policy);
Felipe Leme50a235e2016-01-15 18:37:06 -0800245 return 0;
246 }
247
Felipe Leme57e3d312016-08-23 14:42:52 -0700248 private int resetUidPolicy(String errorMessage, int expectedPolicy) throws RemoteException {
Felipe Leme9982b302016-03-01 13:40:31 -0800249 final int uid = getUidFromNextArg();
250 if (uid < 0) {
251 return uid;
252 }
Felipe Leme57e3d312016-08-23 14:42:52 -0700253 int actualPolicy = mInterface.getUidPolicy(uid);
254 if (actualPolicy != expectedPolicy) {
255 final PrintWriter pw = getOutPrintWriter();
256 pw.print("Error: UID "); pw.print(uid); pw.print(' '); pw.println(errorMessage);
257 return -1;
Felipe Leme9982b302016-03-01 13:40:31 -0800258 }
259 mInterface.setUidPolicy(uid, POLICY_NONE);
260 return 0;
261 }
262
Felipe Leme57e3d312016-08-23 14:42:52 -0700263 private int addRestrictBackgroundWhitelist() throws RemoteException {
264 return setUidPolicy(POLICY_ALLOW_METERED_BACKGROUND);
265 }
266
267 private int removeRestrictBackgroundWhitelist() throws RemoteException {
268 return resetUidPolicy("not whitelisted", POLICY_ALLOW_METERED_BACKGROUND);
269 }
270
271 private int addRestrictBackgroundBlacklist() throws RemoteException {
272 return setUidPolicy(POLICY_REJECT_METERED_BACKGROUND);
273 }
274
275 private int removeRestrictBackgroundBlacklist() throws RemoteException {
276 return resetUidPolicy("not blacklisted", POLICY_REJECT_METERED_BACKGROUND);
277 }
278
Felipe Lemeb9aee90a2016-03-30 16:14:54 -0700279 private int listWifiNetworks() throws RemoteException {
Felipe Lemede4e8e32016-02-02 18:55:22 -0800280 final PrintWriter pw = getOutPrintWriter();
281 final String arg = getNextArg();
282 final Boolean filter = arg == null ? null : Boolean.valueOf(arg);
Felipe Lemeb1a65ee2016-02-08 10:12:01 -0800283 for (NetworkPolicy policy : getWifiPolicies()) {
Felipe Lemede4e8e32016-02-02 18:55:22 -0800284 if (filter != null && filter.booleanValue() != policy.metered) {
285 continue;
286 }
287 pw.print(getNetworkId(policy));
288 pw.print(';');
289 pw.println(policy.metered);
290 }
291 return 0;
292 }
293
Felipe Lemeb1a65ee2016-02-08 10:12:01 -0800294 private int setMeteredWifiNetwork() throws RemoteException {
Felipe Lemede4e8e32016-02-02 18:55:22 -0800295 final PrintWriter pw = getOutPrintWriter();
296 final String id = getNextArg();
297 if (id == null) {
298 pw.println("Error: didn't specify ID");
299 return -1;
300 }
301 final String arg = getNextArg();
302 if (arg == null) {
303 pw.println("Error: didn't specify BOOLEAN");
304 return -1;
305 }
306 final boolean metered = Boolean.valueOf(arg);
307 final NetworkPolicy[] policies = mInterface.getNetworkPolicies(null);
308 boolean changed = false;
Felipe Lemeb1a65ee2016-02-08 10:12:01 -0800309 // First try to find a policy with such id
Felipe Lemede4e8e32016-02-02 18:55:22 -0800310 for (NetworkPolicy policy : policies) {
311 if (policy.template.isMatchRuleMobile() || policy.metered == metered) {
312 continue;
313 }
314 final String networkId = getNetworkId(policy);
315 if (id.equals(networkId)) {
316 Log.i(TAG, "Changing " + networkId + " metered status to " + metered);
317 policy.metered = metered;
318 changed = true;
319 }
320 }
321 if (changed) {
322 mInterface.setNetworkPolicies(policies);
Felipe Lemeb1a65ee2016-02-08 10:12:01 -0800323 return 0;
324 }
325 // Policy not found: check if there is a saved wi-fi with such id.
326 for (WifiConfiguration config : mWifiManager.getConfiguredNetworks()) {
327 final String ssid = removeDoubleQuotes(config.SSID);
328 if (id.equals(ssid)) {
329 final NetworkPolicy policy = newPolicy(ssid);
Felipe Leme02e92142016-04-14 12:46:09 -0700330 policy.metered = true;
Felipe Lemeb1a65ee2016-02-08 10:12:01 -0800331 Log.i(TAG, "Creating new policy for " + ssid + ": " + policy);
332 final NetworkPolicy[] newPolicies = new NetworkPolicy[policies.length + 1];
333 System.arraycopy(policies, 0, newPolicies, 0, policies.length);
334 newPolicies[newPolicies.length - 1] = policy;
335 mInterface.setNetworkPolicies(newPolicies);
Felipe Leme70f30ec2016-09-27 17:31:09 -0700336 return 0;
Felipe Lemeb1a65ee2016-02-08 10:12:01 -0800337 }
Felipe Lemede4e8e32016-02-02 18:55:22 -0800338 }
Felipe Leme70f30ec2016-09-27 17:31:09 -0700339 pw.print("Error: didn't find network with SSID "); pw.println(id);
340 return -1;
Felipe Lemede4e8e32016-02-02 18:55:22 -0800341 }
342
Felipe Lemeb1a65ee2016-02-08 10:12:01 -0800343 private List<NetworkPolicy> getWifiPolicies() throws RemoteException {
344 // First gets a list of saved wi-fi networks.
345 final List<WifiConfiguration> configs = mWifiManager.getConfiguredNetworks();
Felipe Lemec8ce9a82016-03-04 16:11:29 -0800346 final int size = configs != null ? configs.size() : 0;
347 final Set<String> ssids = new HashSet<>(size);
348 if (configs != null) {
349 for (WifiConfiguration config : configs) {
350 ssids.add(removeDoubleQuotes(config.SSID));
351 }
Felipe Lemeb1a65ee2016-02-08 10:12:01 -0800352 }
353
354 // Then gets the saved policies.
Felipe Lemede4e8e32016-02-02 18:55:22 -0800355 final NetworkPolicy[] policies = mInterface.getNetworkPolicies(null);
Felipe Lemeb1a65ee2016-02-08 10:12:01 -0800356 final List<NetworkPolicy> wifiPolicies = new ArrayList<NetworkPolicy>(policies.length);
Felipe Lemede4e8e32016-02-02 18:55:22 -0800357 for (NetworkPolicy policy: policies) {
358 if (!policy.template.isMatchRuleMobile()) {
Felipe Lemeb1a65ee2016-02-08 10:12:01 -0800359 wifiPolicies.add(policy);
360 final String netId = getNetworkId(policy);
361 ssids.remove(netId);
Felipe Lemede4e8e32016-02-02 18:55:22 -0800362 }
363 }
Felipe Lemeb1a65ee2016-02-08 10:12:01 -0800364 // Finally, creates new default policies for saved WI-FIs not policied yet.
365 for (String ssid : ssids) {
366 final NetworkPolicy policy = newPolicy(ssid);
367 wifiPolicies.add(policy);
368 }
369 return wifiPolicies;
370 }
371
372 private NetworkPolicy newPolicy(String ssid) {
373 final NetworkTemplate template = NetworkTemplate.buildTemplateWifi(ssid);
374 final NetworkPolicy policy = newWifiPolicy(template, false);
375 return policy;
Felipe Lemede4e8e32016-02-02 18:55:22 -0800376 }
377
378 private String getNetworkId(NetworkPolicy policy) {
Felipe Lemeb1a65ee2016-02-08 10:12:01 -0800379 return removeDoubleQuotes(policy.template.getNetworkId());
Felipe Lemede4e8e32016-02-02 18:55:22 -0800380 }
381
Felipe Leme50a235e2016-01-15 18:37:06 -0800382 private int getNextBooleanArg() {
383 final PrintWriter pw = getOutPrintWriter();
384 final String arg = getNextArg();
385 if (arg == null) {
386 pw.println("Error: didn't specify BOOLEAN");
387 return -1;
388 }
389 return Boolean.valueOf(arg) ? 1 : 0;
390 }
391
392 private int getUidFromNextArg() {
393 final PrintWriter pw = getOutPrintWriter();
394 final String arg = getNextArg();
395 if (arg == null) {
396 pw.println("Error: didn't specify UID");
397 return -1;
398 }
399 try {
400 return Integer.parseInt(arg);
401 } catch (NumberFormatException e) {
402 pw.println("Error: UID (" + arg + ") should be a number");
403 return -2;
404 }
405 }
406}