Plumb WiFi multicast filter through to ApfFilter

Use APF to implement WifiManager.MulticastLock, if APF isn't available
fallback to the present behavior of using DRIVER RXFILTER. Since we
don't know whether APF is supported until we're connected, postpone
enabling/disabling the multicast filter until then; this should be
fine as there isn't much need to filter packets if there aren't any
packets going by since we're not connected.

Bug: 26238573
Change-Id: I862c053f1c8c3a41de50c2951cf14b3ca6923a2a
diff --git a/services/net/java/android/net/apf/ApfFilter.java b/services/net/java/android/net/apf/ApfFilter.java
index ebbf991..194650b 100644
--- a/services/net/java/android/net/apf/ApfFilter.java
+++ b/services/net/java/android/net/apf/ApfFilter.java
@@ -147,10 +147,11 @@
     private boolean mMulticastFilter;
 
     private ApfFilter(ApfCapabilities apfCapabilities, NetworkInterface networkInterface,
-            IpManager.Callback ipManagerCallback) {
+            IpManager.Callback ipManagerCallback, boolean multicastFilter) {
         mApfCapabilities = apfCapabilities;
         mIpManagerCallback = ipManagerCallback;
         mNetworkInterface = networkInterface;
+        mMulticastFilter = multicastFilter;
 
         maybeStartFilter();
     }
@@ -752,7 +753,8 @@
      * filtering using APF programs.
      */
     public static ApfFilter maybeCreate(ApfCapabilities apfCapabilities,
-            NetworkInterface networkInterface, IpManager.Callback ipManagerCallback) {
+            NetworkInterface networkInterface, IpManager.Callback ipManagerCallback,
+            boolean multicastFilter) {
         if (apfCapabilities == null || networkInterface == null) return null;
         if (apfCapabilities.apfVersionSupported == 0) return null;
         if (apfCapabilities.maximumApfProgramSize < 512) {
@@ -768,7 +770,7 @@
             Log.e(TAG, "Unsupported APF version: " + apfCapabilities.apfVersionSupported);
             return null;
         }
-        return new ApfFilter(apfCapabilities, networkInterface, ipManagerCallback);
+        return new ApfFilter(apfCapabilities, networkInterface, ipManagerCallback, multicastFilter);
     }
 
     public synchronized void shutdown() {
diff --git a/services/net/java/android/net/ip/IpManager.java b/services/net/java/android/net/ip/IpManager.java
index c25fae3..288006f 100644
--- a/services/net/java/android/net/ip/IpManager.java
+++ b/services/net/java/android/net/ip/IpManager.java
@@ -116,6 +116,10 @@
 
         // Install an APF program to filter incoming packets.
         public void installPacketFilter(byte[] filter) {}
+
+        // If multicast filtering cannot be accomplished with APF, this function will be called to
+        // actuate multicast filtering using another means.
+        public void setFallbackMulticastFilter(boolean enabled) {}
     }
 
     public static class WaitForProvisioningCallback extends Callback {
@@ -222,6 +226,7 @@
     private static final int EVENT_NETLINK_LINKPROPERTIES_CHANGED = 5;
     private static final int CMD_UPDATE_TCP_BUFFER_SIZES = 6;
     private static final int CMD_UPDATE_HTTP_PROXY = 7;
+    private static final int CMD_SET_MULTICAST_FILTER = 8;
 
     private static final int MAX_LOG_RECORDS = 1000;
 
@@ -258,6 +263,7 @@
     private String mTcpBufferSizes;
     private ProxyInfo mHttpProxy;
     private ApfFilter mApfFilter;
+    private boolean mMulticastFiltering;
 
     /**
      * Member variables accessed both from within the StateMachine thread
@@ -390,6 +396,14 @@
         sendMessage(CMD_UPDATE_HTTP_PROXY, proxyInfo);
     }
 
+    /**
+     * Enable or disable the multicast filter.  Attempts to use APF to accomplish the filtering,
+     * if not, Callback.setFallbackMulticastFilter() is called.
+     */
+    public void setMulticastFilter(boolean enabled) {
+        sendMessage(CMD_SET_MULTICAST_FILTER, enabled);
+    }
+
     public LinkProperties getLinkProperties() {
         synchronized (mLock) {
             return new LinkProperties(mLinkProperties);
@@ -725,6 +739,10 @@
                     handleLinkPropertiesUpdate(NO_CALLBACKS);
                     break;
 
+                case CMD_SET_MULTICAST_FILTER:
+                    mMulticastFiltering = (boolean) msg.obj;
+                    break;
+
                 case DhcpClient.CMD_ON_QUIT:
                     // Everything is already stopped.
                     Log.e(mTag, "Unexpected CMD_ON_QUIT (already stopped).");
@@ -765,7 +783,10 @@
         @Override
         public void enter() {
             mApfFilter = ApfFilter.maybeCreate(mConfiguration.mApfCapabilities, mNetworkInterface,
-                    mCallback);
+                    mCallback, mMulticastFiltering);
+            // TODO: investigate the effects of any multicast filtering racing/interfering with the
+            // rest of this IP configuration startup.
+            if (mApfFilter == null) mCallback.setFallbackMulticastFilter(mMulticastFiltering);
             // Set privacy extensions.
             try {
                 mNwService.setInterfaceIpv6PrivacyExtensions(mInterfaceName, true);
@@ -878,6 +899,16 @@
                     handleLinkPropertiesUpdate(SEND_CALLBACKS);
                     break;
 
+                case CMD_SET_MULTICAST_FILTER: {
+                    mMulticastFiltering = (boolean) msg.obj;
+                    if (mApfFilter != null) {
+                        mApfFilter.setMulticastFilter(mMulticastFiltering);
+                    } else {
+                        mCallback.setFallbackMulticastFilter(mMulticastFiltering);
+                    }
+                    break;
+                }
+
                 case DhcpClient.CMD_PRE_DHCP_ACTION:
                     if (VDBG) { Log.d(mTag, "onPreDhcpAction()"); }
                     if (mConfiguration.mRequestedPreDhcpAction) {