3535import static com .android .server .NetworkManagementService .NetdResponseCode .TtyListResult ;
3636import static com .android .server .NetworkManagementSocketTagger .PROP_QTAGUID_ENABLED ;
3737
38+ import android .bluetooth .BluetoothTetheringDataTracker ;
3839import android .content .Context ;
3940import android .net .INetworkManagementEventObserver ;
4041import android .net .InterfaceConfiguration ;
5556import android .util .SparseBooleanArray ;
5657
5758import com .android .internal .net .NetworkStatsFactory ;
59+ import com .android .internal .util .Preconditions ;
5860import com .android .server .NativeDaemonConnector .Command ;
5961import com .google .android .collect .Maps ;
6062
7880import java .util .NoSuchElementException ;
7981import java .util .StringTokenizer ;
8082import 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