Remi NGUYEN VAN | 811f638 | 2019-01-20 12:08:21 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | |
| 17 | package android.net.util; |
| 18 | |
| 19 | import static android.system.OsConstants.SOL_SOCKET; |
| 20 | import static android.system.OsConstants.SO_BINDTODEVICE; |
| 21 | |
Remi NGUYEN VAN | b7bda39 | 2019-03-06 18:02:34 +0900 | [diff] [blame] | 22 | import android.annotation.NonNull; |
| 23 | import android.annotation.Nullable; |
Remi NGUYEN VAN | 811f638 | 2019-01-20 12:08:21 +0900 | [diff] [blame] | 24 | import android.annotation.SystemApi; |
Remi NGUYEN VAN | 53b0314 | 2019-01-23 23:11:12 +0900 | [diff] [blame] | 25 | import android.annotation.TestApi; |
Remi NGUYEN VAN | 5c5f1ba | 2019-01-29 12:08:43 +0900 | [diff] [blame] | 26 | import android.net.MacAddress; |
Remi NGUYEN VAN | 811f638 | 2019-01-20 12:08:21 +0900 | [diff] [blame] | 27 | import android.net.NetworkUtils; |
| 28 | import android.system.ErrnoException; |
| 29 | import android.system.NetlinkSocketAddress; |
| 30 | import android.system.Os; |
| 31 | import android.system.PacketSocketAddress; |
Remi NGUYEN VAN | 249da09 | 2019-01-29 15:17:00 +0900 | [diff] [blame] | 32 | import android.system.StructTimeval; |
Remi NGUYEN VAN | 811f638 | 2019-01-20 12:08:21 +0900 | [diff] [blame] | 33 | |
Remi NGUYEN VAN | 53b0314 | 2019-01-23 23:11:12 +0900 | [diff] [blame] | 34 | import libcore.io.IoBridge; |
| 35 | |
Remi NGUYEN VAN | 811f638 | 2019-01-20 12:08:21 +0900 | [diff] [blame] | 36 | import java.io.FileDescriptor; |
Remi NGUYEN VAN | 53b0314 | 2019-01-23 23:11:12 +0900 | [diff] [blame] | 37 | import java.io.IOException; |
Remi NGUYEN VAN | 5c5f1ba | 2019-01-29 12:08:43 +0900 | [diff] [blame] | 38 | import java.net.Inet4Address; |
Remi NGUYEN VAN | 811f638 | 2019-01-20 12:08:21 +0900 | [diff] [blame] | 39 | import java.net.SocketAddress; |
Remi NGUYEN VAN | 5c5f1ba | 2019-01-29 12:08:43 +0900 | [diff] [blame] | 40 | import java.net.SocketException; |
Remi NGUYEN VAN | 811f638 | 2019-01-20 12:08:21 +0900 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * Collection of utilities to interact with raw sockets. |
| 44 | * @hide |
| 45 | */ |
| 46 | @SystemApi |
Remi NGUYEN VAN | 53b0314 | 2019-01-23 23:11:12 +0900 | [diff] [blame] | 47 | @TestApi |
Remi NGUYEN VAN | 811f638 | 2019-01-20 12:08:21 +0900 | [diff] [blame] | 48 | public class SocketUtils { |
| 49 | /** |
| 50 | * Create a raw datagram socket that is bound to an interface. |
| 51 | * |
| 52 | * <p>Data sent through the socket will go directly to the underlying network, ignoring VPNs. |
| 53 | */ |
Remi NGUYEN VAN | b7bda39 | 2019-03-06 18:02:34 +0900 | [diff] [blame] | 54 | public static void bindSocketToInterface(@NonNull FileDescriptor socket, @NonNull String iface) |
Remi NGUYEN VAN | 811f638 | 2019-01-20 12:08:21 +0900 | [diff] [blame] | 55 | throws ErrnoException { |
| 56 | // SO_BINDTODEVICE actually takes a string. This works because the first member |
| 57 | // of struct ifreq is a NULL-terminated interface name. |
| 58 | // TODO: add a setsockoptString() |
| 59 | Os.setsockoptIfreq(socket, SOL_SOCKET, SO_BINDTODEVICE, iface); |
| 60 | NetworkUtils.protectFromVpn(socket); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Make a socket address to communicate with netlink. |
| 65 | */ |
| 66 | public static SocketAddress makeNetlinkSocketAddress(int portId, int groupsMask) { |
| 67 | return new NetlinkSocketAddress(portId, groupsMask); |
| 68 | } |
| 69 | |
| 70 | /** |
Remi NGUYEN VAN | 53b0314 | 2019-01-23 23:11:12 +0900 | [diff] [blame] | 71 | * Make socket address that packet sockets can bind to. |
Remi NGUYEN VAN | 811f638 | 2019-01-20 12:08:21 +0900 | [diff] [blame] | 72 | */ |
| 73 | public static SocketAddress makePacketSocketAddress(short protocol, int ifIndex) { |
| 74 | return new PacketSocketAddress(protocol, ifIndex); |
| 75 | } |
| 76 | |
| 77 | /** |
Remi NGUYEN VAN | 53b0314 | 2019-01-23 23:11:12 +0900 | [diff] [blame] | 78 | * Make a socket address that packet socket can send packets to. |
Remi NGUYEN VAN | 811f638 | 2019-01-20 12:08:21 +0900 | [diff] [blame] | 79 | */ |
Remi NGUYEN VAN | b7bda39 | 2019-03-06 18:02:34 +0900 | [diff] [blame] | 80 | public static SocketAddress makePacketSocketAddress(int ifIndex, @NonNull byte[] hwAddr) { |
Remi NGUYEN VAN | 811f638 | 2019-01-20 12:08:21 +0900 | [diff] [blame] | 81 | return new PacketSocketAddress(ifIndex, hwAddr); |
| 82 | } |
| 83 | |
Remi NGUYEN VAN | 53b0314 | 2019-01-23 23:11:12 +0900 | [diff] [blame] | 84 | /** |
Remi NGUYEN VAN | 249da09 | 2019-01-29 15:17:00 +0900 | [diff] [blame] | 85 | * Set an option on a socket that takes a time value argument. |
| 86 | */ |
| 87 | public static void setSocketTimeValueOption( |
Remi NGUYEN VAN | b7bda39 | 2019-03-06 18:02:34 +0900 | [diff] [blame] | 88 | @NonNull FileDescriptor fd, int level, int option, long millis) throws ErrnoException { |
Remi NGUYEN VAN | 249da09 | 2019-01-29 15:17:00 +0900 | [diff] [blame] | 89 | Os.setsockoptTimeval(fd, level, option, StructTimeval.fromMillis(millis)); |
| 90 | } |
| 91 | |
| 92 | /** |
Remi NGUYEN VAN | 53b0314 | 2019-01-23 23:11:12 +0900 | [diff] [blame] | 93 | * @see IoBridge#closeAndSignalBlockedThreads(FileDescriptor) |
| 94 | */ |
Remi NGUYEN VAN | b7bda39 | 2019-03-06 18:02:34 +0900 | [diff] [blame] | 95 | public static void closeSocket(@Nullable FileDescriptor fd) throws IOException { |
Remi NGUYEN VAN | 53b0314 | 2019-01-23 23:11:12 +0900 | [diff] [blame] | 96 | IoBridge.closeAndSignalBlockedThreads(fd); |
| 97 | } |
| 98 | |
Remi NGUYEN VAN | 5c5f1ba | 2019-01-29 12:08:43 +0900 | [diff] [blame] | 99 | /** |
| 100 | * Attaches a socket filter that accepts DHCP packets to the given socket. |
| 101 | */ |
Remi NGUYEN VAN | b7bda39 | 2019-03-06 18:02:34 +0900 | [diff] [blame] | 102 | public static void attachDhcpFilter(@NonNull FileDescriptor fd) throws SocketException { |
Remi NGUYEN VAN | 5c5f1ba | 2019-01-29 12:08:43 +0900 | [diff] [blame] | 103 | NetworkUtils.attachDhcpFilter(fd); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Attaches a socket filter that accepts ICMPv6 router advertisements to the given socket. |
| 108 | * @param fd the socket's {@link FileDescriptor}. |
| 109 | * @param packetType the hardware address type, one of ARPHRD_*. |
| 110 | */ |
Remi NGUYEN VAN | b7bda39 | 2019-03-06 18:02:34 +0900 | [diff] [blame] | 111 | public static void attachRaFilter(@NonNull FileDescriptor fd, int packetType) |
| 112 | throws SocketException { |
Remi NGUYEN VAN | 5c5f1ba | 2019-01-29 12:08:43 +0900 | [diff] [blame] | 113 | NetworkUtils.attachRaFilter(fd, packetType); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Attaches a socket filter that accepts L2-L4 signaling traffic required for IP connectivity. |
| 118 | * |
| 119 | * This includes: all ARP, ICMPv6 RS/RA/NS/NA messages, and DHCPv4 exchanges. |
| 120 | * |
| 121 | * @param fd the socket's {@link FileDescriptor}. |
| 122 | * @param packetType the hardware address type, one of ARPHRD_*. |
| 123 | */ |
Remi NGUYEN VAN | b7bda39 | 2019-03-06 18:02:34 +0900 | [diff] [blame] | 124 | public static void attachControlPacketFilter(@NonNull FileDescriptor fd, int packetType) |
Remi NGUYEN VAN | 5c5f1ba | 2019-01-29 12:08:43 +0900 | [diff] [blame] | 125 | throws SocketException { |
| 126 | NetworkUtils.attachControlPacketFilter(fd, packetType); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Add an entry into the ARP cache. |
| 131 | */ |
Remi NGUYEN VAN | b7bda39 | 2019-03-06 18:02:34 +0900 | [diff] [blame] | 132 | public static void addArpEntry(@NonNull Inet4Address ipv4Addr, @NonNull MacAddress ethAddr, |
| 133 | @NonNull String ifname, @NonNull FileDescriptor fd) throws IOException { |
Remi NGUYEN VAN | 5c5f1ba | 2019-01-29 12:08:43 +0900 | [diff] [blame] | 134 | NetworkUtils.addArpEntry(ipv4Addr, ethAddr, ifname, fd); |
| 135 | } |
| 136 | |
Remi NGUYEN VAN | 811f638 | 2019-01-20 12:08:21 +0900 | [diff] [blame] | 137 | private SocketUtils() {} |
| 138 | } |