From a6fba08f82935d45b9a438898efe0f2dc6e01ac5 Mon Sep 17 00:00:00 2001 From: Xiao Ma Date: Wed, 31 May 2023 20:34:28 +0900 Subject: [PATCH] Add a helper function to send RTM_NEWADDR netlink message to kernel. Bug: 260934173 Test: TH Change-Id: I6e9100e6f04a85550790c90fb0fde8d839e74578 --- .../net/module/util/netlink/NetlinkUtils.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/staticlibs/device/com/android/net/module/util/netlink/NetlinkUtils.java b/staticlibs/device/com/android/net/module/util/netlink/NetlinkUtils.java index b512a9598b..0f7bd80b6e 100644 --- a/staticlibs/device/com/android/net/module/util/netlink/NetlinkUtils.java +++ b/staticlibs/device/com/android/net/module/util/netlink/NetlinkUtils.java @@ -22,6 +22,7 @@ import static android.system.OsConstants.EIO; import static android.system.OsConstants.EPROTO; import static android.system.OsConstants.ETIMEDOUT; import static android.system.OsConstants.NETLINK_INET_DIAG; +import static android.system.OsConstants.NETLINK_ROUTE; import static android.system.OsConstants.SOCK_CLOEXEC; import static android.system.OsConstants.SOCK_DGRAM; import static android.system.OsConstants.SOL_SOCKET; @@ -41,9 +42,11 @@ import androidx.annotation.Nullable; import java.io.FileDescriptor; import java.io.IOException; import java.io.InterruptedIOException; +import java.net.Inet6Address; import java.net.SocketException; import java.nio.ByteBuffer; import java.nio.ByteOrder; +import java.util.Objects; /** * Utilities for netlink related class that may not be able to fit into a specific class. @@ -167,6 +170,31 @@ public class NetlinkUtils { } } + /** + * Send an RTM_NEWADDR message to kernel to add or update an IPv6 address. + * + * @param ifIndex interface index. + * @param ip IPv6 address to be added. + * @param prefixlen IPv6 address prefix length. + * @param flags IPv6 address flags. + * @param scope IPv6 address scope. + * @param preferred The preferred lifetime of IPv6 address. + * @param valid The valid lifetime of IPv6 address. + */ + public static boolean sendRtmNewAddressRequest(int ifIndex, @NonNull final Inet6Address ip, + short prefixlen, int flags, byte scope, long preferred, long valid) { + Objects.requireNonNull(ip, "IPv6 address to be added should not be null."); + final byte[] msg = RtNetlinkAddressMessage.newRtmNewAddressMessage(1 /* seqNo*/, ip, + prefixlen, flags, scope, ifIndex, preferred, valid); + try { + NetlinkUtils.sendOneShotKernelMessage(NETLINK_ROUTE, msg); + return true; + } catch (ErrnoException e) { + Log.e(TAG, "Fail to send RTM_NEWADDR to add " + ip.getHostAddress(), e); + return false; + } + } + /** * Create netlink socket with the given netlink protocol type. *