diff options
author | Remi NGUYEN VAN <reminv@google.com> | 2019-04-05 04:44:45 -0700 |
---|---|---|
committer | Remi NGUYEN VAN <reminv@google.com> | 2019-04-08 01:41:39 +0000 |
commit | f38a564a2ae4550fccce6ffa9007478135ebaaff (patch) | |
tree | bac517c8a5c68d0f50ad41d5d777e2d32f5d795c /src/android | |
parent | fb5e4e2035f20464fa199b1ae8ba5d9f0cda4666 (diff) |
Move attach*Filter() and addArpEntry() methods to NetworkStack
The SocketUtils.attach*Filter and SocketUtils.addArpEntry methods
were added there because they could not be added as JNI inside
the NetworkStack. This was not possible because on Go devices,
the NetworkStack was a jar library. But now, Go also uses an APK.
Hence, move these methods to the NetworkStack.
Fixes: 129433183
Merged-In: I66d7b3e4fbfa32bb0bc853e8cf9399031daff8a9
(cherry picked from commit fe71be2b04a3213828dc0347a1dd4a3675d20562)
Change-Id: Ice433a41469e784385f19498c154345d7b9c69b5
Diffstat (limited to 'src/android')
-rw-r--r-- | src/android/net/apf/ApfFilter.java | 3 | ||||
-rw-r--r-- | src/android/net/dhcp/DhcpClient.java | 3 | ||||
-rw-r--r-- | src/android/net/dhcp/DhcpServer.java | 3 | ||||
-rw-r--r-- | src/android/net/ip/ConnectivityPacketTracker.java | 4 | ||||
-rw-r--r-- | src/android/net/util/NetworkStackUtils.java | 41 |
5 files changed, 47 insertions, 7 deletions
diff --git a/src/android/net/apf/ApfFilter.java b/src/android/net/apf/ApfFilter.java index d2f3259..663e2f1 100644 --- a/src/android/net/apf/ApfFilter.java +++ b/src/android/net/apf/ApfFilter.java @@ -49,7 +49,6 @@ import android.net.metrics.IpConnectivityLog; import android.net.metrics.RaEvent; import android.net.util.InterfaceParams; import android.net.util.NetworkStackUtils; -import android.net.util.SocketUtils; import android.os.PowerManager; import android.os.SystemClock; import android.system.ErrnoException; @@ -478,7 +477,7 @@ public class ApfFilter { SocketAddress addr = makePacketSocketAddress( (short) ETH_P_IPV6, mInterfaceParams.index); Os.bind(socket, addr); - SocketUtils.attachRaFilter(socket, mApfCapabilities.apfPacketFormat); + NetworkStackUtils.attachRaFilter(socket, mApfCapabilities.apfPacketFormat); } catch(SocketException|ErrnoException e) { Log.e(TAG, "Error starting filter", e); return; diff --git a/src/android/net/dhcp/DhcpClient.java b/src/android/net/dhcp/DhcpClient.java index 79d6a55..64adc0d 100644 --- a/src/android/net/dhcp/DhcpClient.java +++ b/src/android/net/dhcp/DhcpClient.java @@ -51,6 +51,7 @@ import android.net.metrics.DhcpClientEvent; import android.net.metrics.DhcpErrorEvent; import android.net.metrics.IpConnectivityLog; import android.net.util.InterfaceParams; +import android.net.util.NetworkStackUtils; import android.net.util.SocketUtils; import android.os.Message; import android.os.SystemClock; @@ -319,7 +320,7 @@ public class DhcpClient extends StateMachine { mPacketSock = Os.socket(AF_PACKET, SOCK_RAW, ETH_P_IP); SocketAddress addr = makePacketSocketAddress((short) ETH_P_IP, mIface.index); Os.bind(mPacketSock, addr); - SocketUtils.attachDhcpFilter(mPacketSock); + NetworkStackUtils.attachDhcpFilter(mPacketSock); } catch(SocketException|ErrnoException e) { Log.e(TAG, "Error creating packet socket", e); return false; diff --git a/src/android/net/dhcp/DhcpServer.java b/src/android/net/dhcp/DhcpServer.java index cd993e9..8832eaa 100644 --- a/src/android/net/dhcp/DhcpServer.java +++ b/src/android/net/dhcp/DhcpServer.java @@ -45,6 +45,7 @@ import android.annotation.Nullable; import android.net.INetworkStackStatusCallback; import android.net.MacAddress; import android.net.TrafficStats; +import android.net.util.NetworkStackUtils; import android.net.util.SharedLog; import android.net.util.SocketUtils; import android.os.Handler; @@ -207,7 +208,7 @@ public class DhcpServer extends IDhcpServer.Stub { @Override public void addArpEntry(@NonNull Inet4Address ipv4Addr, @NonNull MacAddress ethAddr, @NonNull String ifname, @NonNull FileDescriptor fd) throws IOException { - SocketUtils.addArpEntry(ipv4Addr, ethAddr, ifname, fd); + NetworkStackUtils.addArpEntry(ipv4Addr, ethAddr, ifname, fd); } @Override diff --git a/src/android/net/ip/ConnectivityPacketTracker.java b/src/android/net/ip/ConnectivityPacketTracker.java index de54824..eb49218 100644 --- a/src/android/net/ip/ConnectivityPacketTracker.java +++ b/src/android/net/ip/ConnectivityPacketTracker.java @@ -25,8 +25,8 @@ import static android.system.OsConstants.SOCK_RAW; import android.net.util.ConnectivityPacketSummary; import android.net.util.InterfaceParams; +import android.net.util.NetworkStackUtils; import android.net.util.PacketReader; -import android.net.util.SocketUtils; import android.os.Handler; import android.system.ErrnoException; import android.system.Os; @@ -103,7 +103,7 @@ public class ConnectivityPacketTracker { FileDescriptor s = null; try { s = Os.socket(AF_PACKET, SOCK_RAW | SOCK_NONBLOCK, 0); - SocketUtils.attachControlPacketFilter(s, ARPHRD_ETHER); + NetworkStackUtils.attachControlPacketFilter(s, ARPHRD_ETHER); Os.bind(s, makePacketSocketAddress((short) ETH_P_ALL, mInterface.index)); } catch (ErrnoException | IOException e) { logError("Failed to create packet tracking socket: ", e); diff --git a/src/android/net/util/NetworkStackUtils.java b/src/android/net/util/NetworkStackUtils.java index 9d2df57..dada61c 100644 --- a/src/android/net/util/NetworkStackUtils.java +++ b/src/android/net/util/NetworkStackUtils.java @@ -23,14 +23,18 @@ import android.util.SparseArray; import java.io.FileDescriptor; import java.io.IOException; +import java.net.Inet4Address; +import java.net.SocketException; import java.util.List; import java.util.function.Predicate; - /** * Collection of utilities for the network stack. */ public class NetworkStackUtils { + static { + System.loadLibrary("networkstackutilsjni"); + } /** * @return True if the array is null or 0-length. @@ -98,4 +102,39 @@ public class NetworkStackUtils { String value = DeviceConfig.getProperty(namespace, name); return value != null ? value : defaultValue; } + + /** + * Attaches a socket filter that accepts DHCP packets to the given socket. + */ + public static native void attachDhcpFilter(FileDescriptor fd) throws SocketException; + + /** + * Attaches a socket filter that accepts ICMPv6 router advertisements to the given socket. + * @param fd the socket's {@link FileDescriptor}. + * @param packetType the hardware address type, one of ARPHRD_*. + */ + public static native void attachRaFilter(FileDescriptor fd, int packetType) + throws SocketException; + + /** + * Attaches a socket filter that accepts L2-L4 signaling traffic required for IP connectivity. + * + * This includes: all ARP, ICMPv6 RS/RA/NS/NA messages, and DHCPv4 exchanges. + * + * @param fd the socket's {@link FileDescriptor}. + * @param packetType the hardware address type, one of ARPHRD_*. + */ + public static native void attachControlPacketFilter(FileDescriptor fd, int packetType) + throws SocketException; + + /** + * Add an entry into the ARP cache. + */ + public static void addArpEntry(Inet4Address ipv4Addr, android.net.MacAddress ethAddr, + String ifname, FileDescriptor fd) throws IOException { + addArpEntry(ethAddr.toByteArray(), ipv4Addr.getAddress(), ifname, fd); + } + + private static native void addArpEntry(byte[] ethAddr, byte[] netAddr, String ifname, + FileDescriptor fd) throws IOException; } |