1 /* 2 * Copyright (C) 2023 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 com.android.server.thread; 18 19 import static android.system.OsConstants.IPPROTO_IPV6; 20 import static android.system.OsConstants.IPPROTO_RAW; 21 import static android.system.OsConstants.IPV6_CHECKSUM; 22 import static android.system.OsConstants.IPV6_MULTICAST_HOPS; 23 import static android.system.OsConstants.IPV6_RECVHOPLIMIT; 24 import static android.system.OsConstants.IPV6_RECVPKTINFO; 25 import static android.system.OsConstants.IPV6_UNICAST_HOPS; 26 27 import android.net.util.SocketUtils; 28 import android.os.ParcelFileDescriptor; 29 import android.system.ErrnoException; 30 import android.system.Os; 31 32 import java.io.FileDescriptor; 33 import java.io.IOException; 34 35 /** Controller for the infrastructure network interface. */ 36 public class InfraInterfaceController { 37 private static final String TAG = "InfraIfController"; 38 39 private static final int ENABLE = 1; 40 private static final int IPV6_CHECKSUM_OFFSET = 2; 41 private static final int HOP_LIMIT = 255; 42 43 static { 44 System.loadLibrary("service-thread-jni"); 45 } 46 47 /** 48 * Creates a socket on the infrastructure network interface for sending/receiving ICMPv6 49 * Neighbor Discovery messages. 50 * 51 * @param infraInterfaceName the infrastructure network interface name. 52 * @return an ICMPv6 socket file descriptor on the Infrastructure network interface. 53 * @throws IOException when fails to create the socket. 54 */ createIcmp6Socket(String infraInterfaceName)55 public ParcelFileDescriptor createIcmp6Socket(String infraInterfaceName) throws IOException { 56 ParcelFileDescriptor parcelFd = 57 ParcelFileDescriptor.adoptFd(nativeCreateFilteredIcmp6Socket()); 58 FileDescriptor fd = parcelFd.getFileDescriptor(); 59 try { 60 Os.setsockoptInt(fd, IPPROTO_RAW, IPV6_CHECKSUM, IPV6_CHECKSUM_OFFSET); 61 Os.setsockoptInt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, ENABLE); 62 Os.setsockoptInt(fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, ENABLE); 63 Os.setsockoptInt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, HOP_LIMIT); 64 Os.setsockoptInt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, HOP_LIMIT); 65 SocketUtils.bindSocketToInterface(fd, infraInterfaceName); 66 } catch (ErrnoException e) { 67 throw new IOException("Failed to setsockopt for the ICMPv6 socket", e); 68 } 69 return parcelFd; 70 } 71 nativeCreateFilteredIcmp6Socket()72 private static native int nativeCreateFilteredIcmp6Socket() throws IOException; 73 } 74