Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e27aa0f9 authored by Xiao Ma's avatar Xiao Ma Committed by Gerrit Code Review
Browse files

Merge "Revert "Restore the default interface MTU when disconnecting from Wi-Fi AP.""

parents 6e5adbdc cf2245b0
Loading
Loading
Loading
Loading
+3 −36
Original line number Diff line number Diff line
@@ -19,7 +19,6 @@ package android.net.ip;
import static android.net.RouteInfo.RTN_UNICAST;
import static android.net.shared.IpConfigurationParcelableUtil.toStableParcelable;

import static com.android.server.util.NetworkStackConstants.ETHER_MTU;
import static com.android.server.util.PermissionUtil.checkNetworkStackCallingPermission;

import android.annotation.NonNull;
@@ -49,7 +48,6 @@ import android.os.ConditionVariable;
import android.os.IBinder;
import android.os.Message;
import android.os.RemoteException;
import android.os.ServiceSpecificException;
import android.os.SystemClock;
import android.text.TextUtils;
import android.util.LocalLog;
@@ -71,8 +69,6 @@ import com.android.server.NetworkStackService.NetworkStackServiceManager;
import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
@@ -384,8 +380,6 @@ public class IpClient extends StateMachine {
    private final ConditionVariable mApfDataSnapshotComplete = new ConditionVariable();

    public static class Dependencies {
        private static final String TAG = "IpClient.Dependencies";

        /**
         * Get interface parameters for the specified interface.
         */
@@ -393,18 +387,6 @@ public class IpClient extends StateMachine {
            return InterfaceParams.getByName(ifname);
        }

        /**
         * Get the current MTU for the specified interface.
         */
        public int getInterfaceMtu(String ifname) {
            try {
                return NetworkInterface.getByName(ifname).getMTU();
            } catch (SocketException e) {
                Log.e(TAG, "unexpected failure to get the interface MTU");
                return ETHER_MTU;
            }
        }

        /**
         * Get a INetd connector.
         */
@@ -868,14 +850,10 @@ public class IpClient extends StateMachine {
        return shouldLog;
    }

    private void logError(String fmt, Throwable e, Object... args) {
        final String msg = "ERROR " + String.format(fmt, args);
        Log.e(mTag, msg, e);
        mLog.e(msg, e);
    }

    private void logError(String fmt, Object... args) {
        logError(fmt, null, args);
        final String msg = "ERROR " + String.format(fmt, args);
        Log.e(mTag, msg);
        mLog.log(msg);
    }

    // This needs to be called with care to ensure that our LinkProperties
@@ -1373,17 +1351,6 @@ public class IpClient extends StateMachine {
                // There's no DHCPv4 for which to wait; proceed to stopped.
                deferMessage(obtainMessage(CMD_JUMP_STOPPING_TO_STOPPED));
            }

            // Restore the interface MTU to initial value if it has changed.
            final int mtu = mDependencies.getInterfaceMtu(mInterfaceName);
            try {
                if (mtu != mInterfaceParams.defaultMtu) {
                    mNetd.interfaceSetMtu(mInterfaceName, mInterfaceParams.defaultMtu);
                }
            } catch (RemoteException | ServiceSpecificException e) {
                logError("Couldn't reset MTU from "
                        + mtu + " to " + mInterfaceParams.defaultMtu + ": " + e);
            }
        }

        @Override
+0 −2
Original line number Diff line number Diff line
@@ -54,7 +54,6 @@ public final class NetworkStackConstants {
    public static final int ETHER_TYPE_IPV4 = 0x0800;
    public static final int ETHER_TYPE_IPV6 = 0x86dd;
    public static final int ETHER_HEADER_LEN = 14;
    public static final int ETHER_MTU = 1500;

    /**
     * ARP constants.
@@ -98,7 +97,6 @@ public final class NetworkStackConstants {
    public static final int IPV6_PROTOCOL_OFFSET = 6;
    public static final int IPV6_SRC_ADDR_OFFSET = 8;
    public static final int IPV6_DST_ADDR_OFFSET = 24;
    public static final int IPV6_MIN_MTU = 1280;

    /**
     * ICMPv6 constants.
+0 −1
Original line number Diff line number Diff line
@@ -23,7 +23,6 @@ android_test {
        "androidx.annotation_annotation",
        "androidx.test.rules",
        "mockito-target-extended-minus-junit4",
        "net-tests-utils",
        "NetworkStackBase",
        "testables",
    ],
+18 −52
Original line number Diff line number Diff line
@@ -77,7 +77,6 @@ import androidx.test.runner.AndroidJUnit4;
import com.android.server.NetworkObserverRegistry;
import com.android.server.NetworkStackService.NetworkStackServiceManager;
import com.android.server.connectivity.ipmemorystore.IpMemoryStoreService;
import com.android.testutils.HandlerUtilsKt;

import org.junit.After;
import org.junit.Before;
@@ -159,8 +158,7 @@ public class IpClientIntegrationTest {
    private static final Inet4Address BROADCAST_ADDR = getBroadcastAddress(
            SERVER_ADDR, PREFIX_LENGTH);
    private static final String HOSTNAME = "testhostname";
    private static final int TEST_DEFAULT_MTU = 1500;
    private static final int TEST_MIN_MTU = 1280;
    private static final short MTU = 1500;

    private static class TapPacketReader extends PacketReader {
        private final ParcelFileDescriptor mTapFd;
@@ -205,7 +203,6 @@ public class IpClientIntegrationTest {
    private class Dependencies extends IpClient.Dependencies {
        private boolean mIsDhcpLeaseCacheEnabled;
        private boolean mIsDhcpRapidCommitEnabled;
        private int mMtu = TEST_DEFAULT_MTU;

        public void setDhcpLeaseCacheEnabled(final boolean enable) {
            mIsDhcpLeaseCacheEnabled = enable;
@@ -215,10 +212,6 @@ public class IpClientIntegrationTest {
            mIsDhcpRapidCommitEnabled = enable;
        }

        public void setMtu(final int mtu) {
            mMtu = mtu;
        }

        @Override
        public INetd getNetd(Context context) {
            return mNetd;
@@ -249,11 +242,6 @@ public class IpClientIntegrationTest {
                }
            };
        }

        @Override
        public int getInterfaceMtu(String ifName) {
            return mMtu;
        }
    }

    @Before
@@ -357,7 +345,7 @@ public class IpClientIntegrationTest {
    }

    private static ByteBuffer buildDhcpOfferPacket(final DhcpPacket packet,
            final Integer leaseTimeSec, final short mtu) {
            final Integer leaseTimeSec) {
        return DhcpPacket.buildOfferPacket(DhcpPacket.ENCAP_L2, packet.getTransactionId(),
                false /* broadcast */, SERVER_ADDR, INADDR_ANY /* relayIp */,
                CLIENT_ADDR /* yourIp */, packet.getClientMac(), leaseTimeSec,
@@ -365,11 +353,11 @@ public class IpClientIntegrationTest {
                Collections.singletonList(SERVER_ADDR) /* gateways */,
                Collections.singletonList(SERVER_ADDR) /* dnsServers */,
                SERVER_ADDR /* dhcpServerIdentifier */, null /* domainName */, HOSTNAME,
                false /* metered */, mtu);
                false /* metered */, MTU);
    }

    private static ByteBuffer buildDhcpAckPacket(final DhcpPacket packet,
            final Integer leaseTimeSec, final short mtu) {
            final Integer leaseTimeSec) {
        return DhcpPacket.buildAckPacket(DhcpPacket.ENCAP_L2, packet.getTransactionId(),
                false /* broadcast */, SERVER_ADDR, INADDR_ANY /* relayIp */,
                CLIENT_ADDR /* yourIp */, CLIENT_ADDR /* requestIp */, packet.getClientMac(),
@@ -377,7 +365,7 @@ public class IpClientIntegrationTest {
                Collections.singletonList(SERVER_ADDR) /* gateways */,
                Collections.singletonList(SERVER_ADDR) /* dnsServers */,
                SERVER_ADDR /* dhcpServerIdentifier */, null /* domainName */, HOSTNAME,
                false /* metered */, mtu);
                false /* metered */, MTU);
    }

    private static ByteBuffer buildDhcpNakPacket(final DhcpPacket packet) {
@@ -409,7 +397,7 @@ public class IpClientIntegrationTest {
    }

    private void assertIpMemoryStoreNetworkAttributes(final Integer leaseTimeSec,
            final long startTime, final int mtu) {
            final long startTime) {
        final ArgumentCaptor<NetworkAttributes> networkAttributes =
                ArgumentCaptor.forClass(NetworkAttributes.class);

@@ -428,7 +416,7 @@ public class IpClientIntegrationTest {
            assertTrue(lowerBound < expiry);
        }
        assertEquals(Collections.singletonList(SERVER_ADDR), naValueCaptured.dnsAddresses);
        assertEquals(new Integer(mtu), naValueCaptured.mtu);
        assertEquals(new Integer((int) MTU), naValueCaptured.mtu);
    }

    private void assertIpMemoryNeverStoreNetworkAttributes() {
@@ -445,16 +433,13 @@ public class IpClientIntegrationTest {
        while ((packet = getNextDhcpPacket()) != null) {
            if (packet instanceof DhcpDiscoverPacket) {
                if (isDhcpRapidCommitEnabled) {
                    sendResponse(buildDhcpAckPacket(packet, leaseTimeSec,
                            (short) mDependencies.getInterfaceMtu(mIfaceName)));
                    sendResponse(buildDhcpAckPacket(packet, leaseTimeSec));
                } else {
                    sendResponse(buildDhcpOfferPacket(packet, leaseTimeSec,
                            (short) mDependencies.getInterfaceMtu(mIfaceName)));
                    sendResponse(buildDhcpOfferPacket(packet, leaseTimeSec));
                }
            } else if (packet instanceof DhcpRequestPacket) {
                final ByteBuffer byteBuffer = isSuccessLease
                        ? buildDhcpAckPacket(packet, leaseTimeSec,
                                (short) mDependencies.getInterfaceMtu(mIfaceName))
                        ? buildDhcpAckPacket(packet, leaseTimeSec)
                        : buildDhcpNakPacket(packet);
                sendResponse(byteBuffer);
            } else {
@@ -502,7 +487,7 @@ public class IpClientIntegrationTest {
        final long currentTime = System.currentTimeMillis();
        performDhcpHandshake(true /* isSuccessLease */, TEST_LEASE_DURATION_S,
                true /* isDhcpLeaseCacheEnabled */, false /* isDhcpRapidCommitEnabled */);
        assertIpMemoryStoreNetworkAttributes(TEST_LEASE_DURATION_S, currentTime, TEST_DEFAULT_MTU);
        assertIpMemoryStoreNetworkAttributes(TEST_LEASE_DURATION_S, currentTime);
    }

    @Test
@@ -517,7 +502,7 @@ public class IpClientIntegrationTest {
        final long currentTime = System.currentTimeMillis();
        performDhcpHandshake(true /* isSuccessLease */, INFINITE_LEASE,
                true /* isDhcpLeaseCacheEnabled */, false /* isDhcpRapidCommitEnabled */);
        assertIpMemoryStoreNetworkAttributes(INFINITE_LEASE, currentTime, TEST_DEFAULT_MTU);
        assertIpMemoryStoreNetworkAttributes(INFINITE_LEASE, currentTime);
    }

    @Test
@@ -525,7 +510,7 @@ public class IpClientIntegrationTest {
        final long currentTime = System.currentTimeMillis();
        performDhcpHandshake(true /* isSuccessLease */, null /* no lease time */,
                true /* isDhcpLeaseCacheEnabled */, false /* isDhcpRapidCommitEnabled */);
        assertIpMemoryStoreNetworkAttributes(null, currentTime, TEST_DEFAULT_MTU);
        assertIpMemoryStoreNetworkAttributes(null, currentTime);
    }

    @Test
@@ -542,7 +527,7 @@ public class IpClientIntegrationTest {
        final long currentTime = System.currentTimeMillis();
        performDhcpHandshake(true /* isSuccessLease */, TEST_LEASE_DURATION_S,
                true /* isDhcpLeaseCacheEnabled */, true /* isDhcpRapidCommitEnabled */);
        assertIpMemoryStoreNetworkAttributes(TEST_LEASE_DURATION_S, currentTime, TEST_DEFAULT_MTU);
        assertIpMemoryStoreNetworkAttributes(TEST_LEASE_DURATION_S, currentTime);
    }

    @Test
@@ -551,7 +536,7 @@ public class IpClientIntegrationTest {
                new NetworkAttributes.Builder()
                    .setAssignedV4Address(CLIENT_ADDR)
                    .setAssignedV4AddressExpiry(Long.MAX_VALUE) // lease is always valid
                    .setMtu(new Integer(TEST_DEFAULT_MTU))
                    .setMtu(new Integer(MTU))
                    .setGroupHint(TEST_GROUPHINT)
                    .setDnsAddresses(Collections.singletonList(SERVER_ADDR))
                    .build(), false /* timeout */);
@@ -564,7 +549,7 @@ public class IpClientIntegrationTest {
                 new NetworkAttributes.Builder()
                    .setAssignedV4Address(CLIENT_ADDR)
                    .setAssignedV4AddressExpiry(EXPIRED_LEASE)
                    .setMtu(new Integer(TEST_DEFAULT_MTU))
                    .setMtu(new Integer(MTU))
                    .setGroupHint(TEST_GROUPHINT)
                    .setDnsAddresses(Collections.singletonList(SERVER_ADDR))
                    .build(), false /* timeout */);
@@ -583,7 +568,7 @@ public class IpClientIntegrationTest {
                new NetworkAttributes.Builder()
                    .setAssignedV4Address(CLIENT_ADDR)
                    .setAssignedV4AddressExpiry(System.currentTimeMillis() + 3_600_000)
                    .setMtu(new Integer(TEST_DEFAULT_MTU))
                    .setMtu(new Integer(MTU))
                    .setGroupHint(TEST_GROUPHINT)
                    .setDnsAddresses(Collections.singletonList(SERVER_ADDR))
                    .build(), true /* timeout */);
@@ -594,7 +579,7 @@ public class IpClientIntegrationTest {
    public void testDhcpClientStartWithCachedLeaseWithoutIPAddress() throws Exception {
        final DhcpPacket packet = getReplyFromDhcpLease(
                new NetworkAttributes.Builder()
                    .setMtu(new Integer(TEST_DEFAULT_MTU))
                    .setMtu(new Integer(MTU))
                    .setGroupHint(TEST_GROUPHINT)
                    .setDnsAddresses(Collections.singletonList(SERVER_ADDR))
                    .build(), false /* timeout */);
@@ -608,23 +593,4 @@ public class IpClientIntegrationTest {
        final DhcpPacket packet = getNextDhcpPacket();
        assertTrue(DhcpDiscoverPacket.class.isInstance(packet));
    }

    @Test
    public void testRestoreInitialInterfaceMtu() throws Exception {
        final long currentTime = System.currentTimeMillis();

        // check the initial interface MTU
        assertTrue(mDependencies.getInterfaceMtu(mIfaceName) == TEST_DEFAULT_MTU);

        // change the MTU and pass the new MTU into DHCPOFFER and DHCPACK packet
        mDependencies.setMtu(TEST_MIN_MTU);
        performDhcpHandshake(true /* isSuccessLease */, TEST_LEASE_DURATION_S,
                true /* isDhcpLeaseCacheEnabled */, false /* isDhcpRapidCommitEnabled */);
        assertIpMemoryStoreNetworkAttributes(TEST_LEASE_DURATION_S, currentTime, TEST_MIN_MTU);

        mIpc.shutdown();
        HandlerUtilsKt.waitForIdle(mIpc.getHandler(), TEST_TIMEOUT_MS);
        // verify that mtu indeed has been restored
        verify(mNetd, times(1)).interfaceSetMtu(mIfaceName, TEST_DEFAULT_MTU);
    }
}
+0 −2
Original line number Diff line number Diff line
@@ -83,7 +83,6 @@ public class IpClientTest {
    private static final String INVALID = "INVALID";
    private static final String TEST_IFNAME = "test_wlan0";
    private static final int TEST_IFINDEX = 1001;
    private static final int TEST_MTU = 1500;
    // See RFC 7042#section-2.1.2 for EUI-48 documentation values.
    private static final MacAddress TEST_MAC = MacAddress.fromString("00:00:5E:00:53:01");
    private static final int TEST_TIMEOUT_MS = 400;
@@ -123,7 +122,6 @@ public class IpClientTest {
        when(mDependencies.getInterfaceParams(any())).thenReturn(mInterfaceParams);
        when(mDependencies.getIpMemoryStore(mContext, mNetworkStackServiceManager))
                .thenReturn(mIpMemoryStore);
        when(mDependencies.getInterfaceMtu(TEST_IFNAME)).thenReturn(TEST_MTU);

        mIfParams = null;
    }