blob: af39d6e31cc135f17fb6f06aa2dc957ee0443873 [file] [log] [blame]
/*
* Copyright (C) 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.internal.net.ipsec.ike.keepalive;
import static android.net.ipsec.ike.IkeManager.getIkeLog;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.net.IpSecManager.UdpEncapsulationSocket;
import android.os.SystemClock;
import android.system.ErrnoException;
import android.system.Os;
import com.android.internal.net.ipsec.ike.IkeSocket;
import java.net.Inet4Address;
import java.net.SocketException;
import java.nio.ByteBuffer;
import java.util.concurrent.TimeUnit;
/** This class provides methods to schedule and send keepalive packet. */
public final class SoftwareKeepaliveImpl implements IkeNattKeepalive.NattKeepalive {
private static final String TAG = "SoftwareKeepaliveImpl";
// NAT-Keepalive packet payload as per RFC 3948
private static final byte[] NATT_KEEPALIVE_PAYLOAD = new byte[] {(byte) 0xff};
private final long mKeepaliveDelayMs;
private final UdpEncapsulationSocket mSocket;
private final Inet4Address mDestAddress;
private final AlarmManager mAlarmMgr;
private final PendingIntent mKeepaliveIntent;
/**
* Construct an instance of SoftwareKeepaliveImpl
*
* <p>Caller that provides keepAliveAlarmIntent is responsible for handling the alarm.
*/
public SoftwareKeepaliveImpl(
Context context,
int keepaliveDelaySeconds,
Inet4Address dest,
UdpEncapsulationSocket socket,
PendingIntent keepAliveAlarmIntent) {
mKeepaliveDelayMs = TimeUnit.SECONDS.toMillis(keepaliveDelaySeconds);
mSocket = socket;
mAlarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
mDestAddress = dest;
mKeepaliveIntent = keepAliveAlarmIntent;
}
@Override
public void start() {
sendKeepaliveAndScheduleNext();
}
@Override
public void stop() {
mAlarmMgr.cancel(mKeepaliveIntent);
mKeepaliveIntent.cancel();
}
@Override
public void onAlarmFired() {
sendKeepaliveAndScheduleNext();
}
/** Send out keepalive packet and schedule next keepalive event */
private void sendKeepaliveAndScheduleNext() {
try {
Os.sendto(
mSocket.getFileDescriptor(),
ByteBuffer.wrap(NATT_KEEPALIVE_PAYLOAD),
0,
mDestAddress,
IkeSocket.SERVER_PORT_UDP_ENCAPSULATED);
} catch (ErrnoException | SocketException e) {
getIkeLog().i(TAG, "Failed to keepalive packet to " + mDestAddress.getHostAddress(), e);
}
// It is time-critical to send packets periodically to keep the dynamic NAT mapping
// alive. Thus, the alarm has to be "setExact" to avoid batching delay (can be at most 75%)
// and allowed to goes off when the device is in doze mode. There will still be a rate limit
// on firing alarms. Please check AlarmManager#setExactAndAllowWhileIdle for more details.
mAlarmMgr.setExactAndAllowWhileIdle(
AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + mKeepaliveDelayMs,
mKeepaliveIntent);
}
}