Skip to content

Commit c268f0b

Browse files
committed
Framework interface for netd firewall.
Bug: 5756357 Change-Id: If8b9f738fcea2cf16bd01682220718b57346c7cc
1 parent 899223b commit c268f0b

File tree

3 files changed

+96
-2
lines changed

3 files changed

+96
-2
lines changed

core/java/android/os/INetworkManagementService.aidl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,4 +362,11 @@ interface INetworkManagementService
362362
* Flush the DNS cache associated with the specified interface.
363363
*/
364364
void flushInterfaceDnsCache(String iface);
365+
366+
void setFirewallEnabled(boolean enabled);
367+
boolean isFirewallEnabled();
368+
void setInterfaceFirewallRule(String iface, boolean allow);
369+
void setEgressSourceFirewallRule(String addr, boolean allow);
370+
void setEgressDestFirewallRule(String addr, int port, boolean allow);
371+
void setUidFirewallRule(int uid, boolean allow);
365372
}

core/java/com/android/internal/util/Preconditions.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,16 @@ public static <T> T checkNotNull(T reference, Object errorMessage) {
5454
return reference;
5555
}
5656

57+
/**
58+
* Ensures the truth of an expression involving the state of the calling
59+
* instance, but not involving any parameters to the calling method.
60+
*
61+
* @param expression a boolean expression
62+
* @throws IllegalStateException if {@code expression} is false
63+
*/
64+
public static void checkState(boolean expression) {
65+
if (!expression) {
66+
throw new IllegalStateException();
67+
}
68+
}
5769
}

services/java/com/android/server/NetworkManagementService.java

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import static com.android.server.NetworkManagementService.NetdResponseCode.TtyListResult;
3636
import static com.android.server.NetworkManagementSocketTagger.PROP_QTAGUID_ENABLED;
3737

38+
import android.bluetooth.BluetoothTetheringDataTracker;
3839
import android.content.Context;
3940
import android.net.INetworkManagementEventObserver;
4041
import android.net.InterfaceConfiguration;
@@ -55,6 +56,7 @@
5556
import android.util.SparseBooleanArray;
5657

5758
import com.android.internal.net.NetworkStatsFactory;
59+
import com.android.internal.util.Preconditions;
5860
import com.android.server.NativeDaemonConnector.Command;
5961
import com.google.android.collect.Maps;
6062

@@ -78,7 +80,6 @@
7880
import java.util.NoSuchElementException;
7981
import java.util.StringTokenizer;
8082
import java.util.concurrent.CountDownLatch;
81-
import android.bluetooth.BluetoothTetheringDataTracker;
8283

8384
/**
8485
* @hide
@@ -92,6 +93,9 @@ public class NetworkManagementService extends INetworkManagementService.Stub
9293
private static final String ADD = "add";
9394
private static final String REMOVE = "remove";
9495

96+
private static final String ALLOW = "allow";
97+
private static final String DENY = "deny";
98+
9599
private static final String DEFAULT = "default";
96100
private static final String SECONDARY = "secondary";
97101

@@ -169,6 +173,7 @@ private static class IdleTimerParams {
169173
private HashMap<String, IdleTimerParams> mActiveIdleTimers = Maps.newHashMap();
170174

171175
private volatile boolean mBandwidthControlEnabled;
176+
private volatile boolean mFirewallEnabled;
172177

173178
/**
174179
* Constructs a new NetworkManagementService instance
@@ -363,6 +368,9 @@ private void prepareNativeDaemon() {
363368
}
364369
}
365370
}
371+
372+
// TODO: Push any existing firewall state
373+
setFirewallEnabled(mFirewallEnabled);
366374
}
367375

368376
//
@@ -1425,7 +1433,72 @@ public void flushInterfaceDnsCache(String iface) {
14251433
}
14261434
}
14271435

1428-
/** {@inheritDoc} */
1436+
@Override
1437+
public void setFirewallEnabled(boolean enabled) {
1438+
mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1439+
try {
1440+
mConnector.execute("firewall", enabled ? "enable" : "disable");
1441+
mFirewallEnabled = enabled;
1442+
} catch (NativeDaemonConnectorException e) {
1443+
throw e.rethrowAsParcelableException();
1444+
}
1445+
}
1446+
1447+
@Override
1448+
public boolean isFirewallEnabled() {
1449+
mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1450+
return mFirewallEnabled;
1451+
}
1452+
1453+
@Override
1454+
public void setInterfaceFirewallRule(String iface, boolean allow) {
1455+
mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1456+
Preconditions.checkState(mFirewallEnabled);
1457+
final String rule = allow ? ALLOW : DENY;
1458+
try {
1459+
mConnector.execute("firewall", "set_interface_rule", iface, rule);
1460+
} catch (NativeDaemonConnectorException e) {
1461+
throw e.rethrowAsParcelableException();
1462+
}
1463+
}
1464+
1465+
@Override
1466+
public void setEgressSourceFirewallRule(String addr, boolean allow) {
1467+
mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1468+
Preconditions.checkState(mFirewallEnabled);
1469+
final String rule = allow ? ALLOW : DENY;
1470+
try {
1471+
mConnector.execute("firewall", "set_egress_source_rule", addr, rule);
1472+
} catch (NativeDaemonConnectorException e) {
1473+
throw e.rethrowAsParcelableException();
1474+
}
1475+
}
1476+
1477+
@Override
1478+
public void setEgressDestFirewallRule(String addr, int port, boolean allow) {
1479+
mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1480+
Preconditions.checkState(mFirewallEnabled);
1481+
final String rule = allow ? ALLOW : DENY;
1482+
try {
1483+
mConnector.execute("firewall", "set_egress_dest_rule", addr, port, rule);
1484+
} catch (NativeDaemonConnectorException e) {
1485+
throw e.rethrowAsParcelableException();
1486+
}
1487+
}
1488+
1489+
@Override
1490+
public void setUidFirewallRule(int uid, boolean allow) {
1491+
mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1492+
Preconditions.checkState(mFirewallEnabled);
1493+
final String rule = allow ? ALLOW : DENY;
1494+
try {
1495+
mConnector.execute("firewall", "set_uid_rule", uid, rule);
1496+
} catch (NativeDaemonConnectorException e) {
1497+
throw e.rethrowAsParcelableException();
1498+
}
1499+
}
1500+
1501+
@Override
14291502
public void monitor() {
14301503
if (mConnector != null) {
14311504
mConnector.monitor();
@@ -1456,5 +1529,7 @@ protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
14561529
}
14571530
pw.println("]");
14581531
}
1532+
1533+
pw.print("Firewall enabled: "); pw.println(mFirewallEnabled);
14591534
}
14601535
}

0 commit comments

Comments
 (0)