markchien | 74a4fa9 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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.ip; |
| 18 | |
markchien | 74a4fa9 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 19 | import static android.system.OsConstants.AF_INET6; |
| 20 | import static android.system.OsConstants.IPPROTO_ICMPV6; |
KH Shi | bdcd7db | 2023-03-16 15:23:13 +0800 | [diff] [blame^] | 21 | import static android.system.OsConstants.SOCK_NONBLOCK; |
markchien | 74a4fa9 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 22 | import static android.system.OsConstants.SOCK_RAW; |
| 23 | import static android.system.OsConstants.SOL_SOCKET; |
markchien | 74a4fa9 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 24 | import static android.system.OsConstants.SO_SNDTIMEO; |
| 25 | |
Xiao Ma | 3e557d7 | 2021-03-04 06:04:28 +0000 | [diff] [blame] | 26 | import static com.android.net.module.util.NetworkStackConstants.ICMPV6_ND_OPTION_SLLA; |
| 27 | import static com.android.net.module.util.NetworkStackConstants.ICMPV6_RA_HEADER_LEN; |
| 28 | import static com.android.net.module.util.NetworkStackConstants.ICMPV6_ROUTER_ADVERTISEMENT; |
| 29 | import static com.android.net.module.util.NetworkStackConstants.ICMPV6_ROUTER_SOLICITATION; |
| 30 | import static com.android.net.module.util.NetworkStackConstants.IPV6_MIN_MTU; |
| 31 | import static com.android.net.module.util.NetworkStackConstants.PIO_FLAG_AUTONOMOUS; |
| 32 | import static com.android.net.module.util.NetworkStackConstants.PIO_FLAG_ON_LINK; |
Xiao Ma | 1f99330 | 2023-08-09 18:55:49 +0900 | [diff] [blame] | 33 | import static com.android.net.module.util.NetworkStackConstants.RFC7421_PREFIX_LENGTH; |
Xiao Ma | 3e557d7 | 2021-03-04 06:04:28 +0000 | [diff] [blame] | 34 | import static com.android.net.module.util.NetworkStackConstants.TAG_SYSTEM_NEIGHBOR; |
markchien | d02f9af | 2021-11-04 11:26:03 +0800 | [diff] [blame] | 35 | import static com.android.networkstack.tethering.util.TetheringUtils.getAllNodesForScopeId; |
Xiao Ma | 3e557d7 | 2021-03-04 06:04:28 +0000 | [diff] [blame] | 36 | |
markchien | 74a4fa9 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 37 | import android.net.IpPrefix; |
| 38 | import android.net.LinkAddress; |
Xiao Ma | 3e557d7 | 2021-03-04 06:04:28 +0000 | [diff] [blame] | 39 | import android.net.MacAddress; |
markchien | 74a4fa9 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 40 | import android.net.TrafficStats; |
markchien | 6cf0e55 | 2019-12-06 15:24:53 +0800 | [diff] [blame] | 41 | import android.net.util.SocketUtils; |
KH Shi | bdcd7db | 2023-03-16 15:23:13 +0800 | [diff] [blame^] | 42 | import android.os.Handler; |
| 43 | import android.os.HandlerThread; |
| 44 | import android.os.Looper; |
| 45 | import android.os.Message; |
markchien | 74a4fa9 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 46 | import android.system.ErrnoException; |
| 47 | import android.system.Os; |
| 48 | import android.system.StructTimeval; |
| 49 | import android.util.Log; |
| 50 | |
KH Shi | bdcd7db | 2023-03-16 15:23:13 +0800 | [diff] [blame^] | 51 | import androidx.annotation.NonNull; |
| 52 | |
markchien | 74a4fa9 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 53 | import com.android.internal.annotations.GuardedBy; |
KH Shi | bdcd7db | 2023-03-16 15:23:13 +0800 | [diff] [blame^] | 54 | import com.android.net.module.util.FdEventsReader; |
Patrick Rohr | 9f371f0 | 2022-03-04 15:14:27 +0100 | [diff] [blame] | 55 | import com.android.net.module.util.InterfaceParams; |
Xiao Ma | 3e557d7 | 2021-03-04 06:04:28 +0000 | [diff] [blame] | 56 | import com.android.net.module.util.structs.Icmpv6Header; |
| 57 | import com.android.net.module.util.structs.LlaOption; |
| 58 | import com.android.net.module.util.structs.MtuOption; |
| 59 | import com.android.net.module.util.structs.PrefixInformationOption; |
| 60 | import com.android.net.module.util.structs.RaHeader; |
| 61 | import com.android.net.module.util.structs.RdnssOption; |
markchien | d02f9af | 2021-11-04 11:26:03 +0800 | [diff] [blame] | 62 | import com.android.networkstack.tethering.util.TetheringUtils; |
markchien | 74a4fa9 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 63 | |
markchien | 74a4fa9 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 64 | import java.io.FileDescriptor; |
| 65 | import java.io.IOException; |
| 66 | import java.net.Inet6Address; |
| 67 | import java.net.InetAddress; |
| 68 | import java.net.InetSocketAddress; |
| 69 | import java.net.SocketException; |
markchien | 74a4fa9 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 70 | import java.nio.BufferOverflowException; |
| 71 | import java.nio.ByteBuffer; |
| 72 | import java.nio.ByteOrder; |
| 73 | import java.util.HashMap; |
| 74 | import java.util.HashSet; |
| 75 | import java.util.Iterator; |
| 76 | import java.util.Map; |
| 77 | import java.util.Random; |
| 78 | import java.util.Set; |
| 79 | import java.util.concurrent.atomic.AtomicInteger; |
| 80 | |
| 81 | |
| 82 | /** |
| 83 | * Basic IPv6 Router Advertisement Daemon. |
| 84 | * |
| 85 | * TODO: |
| 86 | * |
| 87 | * - Rewrite using Handler (and friends) so that AlarmManager can deliver |
| 88 | * "kick" messages when it's time to send a multicast RA. |
| 89 | * |
| 90 | * @hide |
| 91 | */ |
| 92 | public class RouterAdvertisementDaemon { |
| 93 | private static final String TAG = RouterAdvertisementDaemon.class.getSimpleName(); |
markchien | 74a4fa9 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 94 | |
| 95 | // Summary of various timers and lifetimes. |
| 96 | private static final int MIN_RTR_ADV_INTERVAL_SEC = 300; |
| 97 | private static final int MAX_RTR_ADV_INTERVAL_SEC = 600; |
| 98 | // In general, router, prefix, and DNS lifetimes are all advised to be |
Maciej Żenczykowski | f95fd40 | 2023-02-16 22:37:22 +0000 | [diff] [blame] | 99 | // greater than or equal to 3 * MAX_RTR_ADV_INTERVAL. Here, we quadruple |
markchien | 74a4fa9 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 100 | // that to allow for multicast packet loss. |
| 101 | // |
| 102 | // This MAX_RTR_ADV_INTERVAL_SEC and DEFAULT_LIFETIME are also consistent |
| 103 | // with the https://tools.ietf.org/html/rfc7772#section-4 discussion of |
| 104 | // "approximately 7 RAs per hour". |
Maciej Żenczykowski | f95fd40 | 2023-02-16 22:37:22 +0000 | [diff] [blame] | 105 | private static final int DEFAULT_LIFETIME = 12 * MAX_RTR_ADV_INTERVAL_SEC; |
markchien | 74a4fa9 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 106 | // From https://tools.ietf.org/html/rfc4861#section-10 . |
| 107 | private static final int MIN_DELAY_BETWEEN_RAS_SEC = 3; |
| 108 | // Both initial and final RAs, but also for changes in RA contents. |
| 109 | // From https://tools.ietf.org/html/rfc4861#section-10 . |
| 110 | private static final int MAX_URGENT_RTR_ADVERTISEMENTS = 5; |
| 111 | |
| 112 | private static final int DAY_IN_SECONDS = 86_400; |
| 113 | |
KH Shi | bdcd7db | 2023-03-16 15:23:13 +0800 | [diff] [blame^] | 114 | // Commands for IpServer to control RouterAdvertisementDaemon |
| 115 | private static final int CMD_START = 1; |
| 116 | private static final int CMD_STOP = 2; |
| 117 | |
markchien | 74a4fa9 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 118 | private final InterfaceParams mInterface; |
| 119 | private final InetSocketAddress mAllNodes; |
| 120 | |
| 121 | // This lock is to protect the RA from being updated while being |
| 122 | // transmitted on another thread (multicast or unicast). |
| 123 | // |
| 124 | // TODO: This should be handled with a more RCU-like approach. |
| 125 | private final Object mLock = new Object(); |
| 126 | @GuardedBy("mLock") |
| 127 | private final byte[] mRA = new byte[IPV6_MIN_MTU]; |
| 128 | @GuardedBy("mLock") |
| 129 | private int mRaLength; |
| 130 | @GuardedBy("mLock") |
| 131 | private final DeprecatedInfoTracker mDeprecatedInfoTracker; |
| 132 | @GuardedBy("mLock") |
| 133 | private RaParams mRaParams; |
| 134 | |
KH Shi | bdcd7db | 2023-03-16 15:23:13 +0800 | [diff] [blame^] | 135 | // To be accessed only from RaMessageHandler |
| 136 | private RsPacketListener mRsPacketListener; |
| 137 | |
markchien | 74a4fa9 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 138 | private volatile FileDescriptor mSocket; |
| 139 | private volatile MulticastTransmitter mMulticastTransmitter; |
KH Shi | bdcd7db | 2023-03-16 15:23:13 +0800 | [diff] [blame^] | 140 | private volatile RaMessageHandler mRaMessageHandler; |
| 141 | private volatile HandlerThread mRaHandlerThread; |
markchien | 74a4fa9 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 142 | |
| 143 | /** Encapsulate the RA parameters for RouterAdvertisementDaemon.*/ |
| 144 | public static class RaParams { |
| 145 | // Tethered traffic will have the hop limit properly decremented. |
| 146 | // Consequently, set the hoplimit greater by one than the upstream |
| 147 | // unicast hop limit. |
markchien | 74a4fa9 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 148 | static final byte DEFAULT_HOPLIMIT = 65; |
| 149 | |
| 150 | public boolean hasDefaultRoute; |
| 151 | public byte hopLimit; |
| 152 | public int mtu; |
| 153 | public HashSet<IpPrefix> prefixes; |
| 154 | public HashSet<Inet6Address> dnses; |
| 155 | |
| 156 | public RaParams() { |
| 157 | hasDefaultRoute = false; |
| 158 | hopLimit = DEFAULT_HOPLIMIT; |
| 159 | mtu = IPV6_MIN_MTU; |
| 160 | prefixes = new HashSet<IpPrefix>(); |
| 161 | dnses = new HashSet<Inet6Address>(); |
| 162 | } |
| 163 | |
| 164 | public RaParams(RaParams other) { |
| 165 | hasDefaultRoute = other.hasDefaultRoute; |
| 166 | hopLimit = other.hopLimit; |
| 167 | mtu = other.mtu; |
| 168 | prefixes = (HashSet) other.prefixes.clone(); |
| 169 | dnses = (HashSet) other.dnses.clone(); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Returns the subset of RA parameters that become deprecated when |
| 174 | * moving from announcing oldRa to announcing newRa. |
| 175 | * |
| 176 | * Currently only tracks differences in |prefixes| and |dnses|. |
| 177 | */ |
| 178 | public static RaParams getDeprecatedRaParams(RaParams oldRa, RaParams newRa) { |
| 179 | RaParams newlyDeprecated = new RaParams(); |
| 180 | |
| 181 | if (oldRa != null) { |
| 182 | for (IpPrefix ipp : oldRa.prefixes) { |
| 183 | if (newRa == null || !newRa.prefixes.contains(ipp)) { |
| 184 | newlyDeprecated.prefixes.add(ipp); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | for (Inet6Address dns : oldRa.dnses) { |
| 189 | if (newRa == null || !newRa.dnses.contains(dns)) { |
| 190 | newlyDeprecated.dnses.add(dns); |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | return newlyDeprecated; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | private static class DeprecatedInfoTracker { |
| 200 | private final HashMap<IpPrefix, Integer> mPrefixes = new HashMap<>(); |
| 201 | private final HashMap<Inet6Address, Integer> mDnses = new HashMap<>(); |
| 202 | |
| 203 | Set<IpPrefix> getPrefixes() { |
| 204 | return mPrefixes.keySet(); |
| 205 | } |
| 206 | |
| 207 | void putPrefixes(Set<IpPrefix> prefixes) { |
| 208 | for (IpPrefix ipp : prefixes) { |
| 209 | mPrefixes.put(ipp, MAX_URGENT_RTR_ADVERTISEMENTS); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | void removePrefixes(Set<IpPrefix> prefixes) { |
| 214 | for (IpPrefix ipp : prefixes) { |
| 215 | mPrefixes.remove(ipp); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | Set<Inet6Address> getDnses() { |
| 220 | return mDnses.keySet(); |
| 221 | } |
| 222 | |
| 223 | void putDnses(Set<Inet6Address> dnses) { |
| 224 | for (Inet6Address dns : dnses) { |
| 225 | mDnses.put(dns, MAX_URGENT_RTR_ADVERTISEMENTS); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | void removeDnses(Set<Inet6Address> dnses) { |
| 230 | for (Inet6Address dns : dnses) { |
| 231 | mDnses.remove(dns); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | boolean isEmpty() { |
| 236 | return mPrefixes.isEmpty() && mDnses.isEmpty(); |
| 237 | } |
| 238 | |
| 239 | private boolean decrementCounters() { |
| 240 | boolean removed = decrementCounter(mPrefixes); |
| 241 | removed |= decrementCounter(mDnses); |
| 242 | return removed; |
| 243 | } |
| 244 | |
| 245 | private <T> boolean decrementCounter(HashMap<T, Integer> map) { |
| 246 | boolean removed = false; |
| 247 | |
| 248 | for (Iterator<Map.Entry<T, Integer>> it = map.entrySet().iterator(); |
| 249 | it.hasNext();) { |
| 250 | Map.Entry<T, Integer> kv = it.next(); |
| 251 | if (kv.getValue() == 0) { |
| 252 | it.remove(); |
| 253 | removed = true; |
| 254 | } else { |
| 255 | kv.setValue(kv.getValue() - 1); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | return removed; |
| 260 | } |
| 261 | } |
| 262 | |
KH Shi | bdcd7db | 2023-03-16 15:23:13 +0800 | [diff] [blame^] | 263 | private class RaMessageHandler extends Handler { |
| 264 | RaMessageHandler(Looper looper) { |
| 265 | super(looper); |
| 266 | } |
| 267 | |
| 268 | @Override |
| 269 | public void handleMessage(Message msg) { |
| 270 | switch (msg.what) { |
| 271 | case CMD_START: |
| 272 | mRsPacketListener = new RsPacketListener(this); |
| 273 | mRsPacketListener.start(); |
| 274 | break; |
| 275 | case CMD_STOP: |
| 276 | if (mRsPacketListener != null) { |
| 277 | mRsPacketListener.stop(); |
| 278 | mRsPacketListener = null; |
| 279 | } |
| 280 | break; |
| 281 | default: |
| 282 | Log.e(TAG, "Unknown message, cmd = " + String.valueOf(msg.what)); |
| 283 | break; |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | private class RsPacketListener extends FdEventsReader<RsPacketListener.RecvBuffer> { |
| 289 | private static final class RecvBuffer { |
| 290 | // The recycled buffer for receiving Router Solicitations from clients. |
| 291 | // If the RS is larger than IPV6_MIN_MTU the packets are truncated. |
| 292 | // This is fine since currently only byte 0 is examined anyway. |
| 293 | final byte[] mBytes = new byte[IPV6_MIN_MTU]; |
| 294 | final InetSocketAddress mSrcAddr = new InetSocketAddress(0); |
| 295 | } |
| 296 | |
| 297 | RsPacketListener(@NonNull Handler handler) { |
| 298 | super(handler, new RecvBuffer()); |
| 299 | } |
| 300 | |
| 301 | @Override |
| 302 | protected int recvBufSize(@NonNull RecvBuffer buffer) { |
| 303 | return buffer.mBytes.length; |
| 304 | } |
| 305 | |
| 306 | @Override |
| 307 | protected FileDescriptor createFd() { |
| 308 | return mSocket; |
| 309 | } |
| 310 | |
| 311 | @Override |
| 312 | protected int readPacket(@NonNull FileDescriptor fd, @NonNull RecvBuffer buffer) |
| 313 | throws Exception { |
| 314 | return Os.recvfrom( |
| 315 | fd, buffer.mBytes, 0, buffer.mBytes.length, 0 /* flags */, buffer.mSrcAddr); |
| 316 | } |
| 317 | |
| 318 | @Override |
| 319 | protected final void handlePacket(@NonNull RecvBuffer buffer, int length) { |
| 320 | // Do the least possible amount of validations. |
| 321 | if (buffer.mSrcAddr == null |
| 322 | || length <= 0 |
| 323 | || buffer.mBytes[0] != asByte(ICMPV6_ROUTER_SOLICITATION)) { |
| 324 | return; |
| 325 | } |
| 326 | |
| 327 | maybeSendRA(buffer.mSrcAddr); |
| 328 | } |
| 329 | } |
| 330 | |
markchien | 74a4fa9 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 331 | public RouterAdvertisementDaemon(InterfaceParams ifParams) { |
| 332 | mInterface = ifParams; |
| 333 | mAllNodes = new InetSocketAddress(getAllNodesForScopeId(mInterface.index), 0); |
| 334 | mDeprecatedInfoTracker = new DeprecatedInfoTracker(); |
| 335 | } |
| 336 | |
| 337 | /** Build new RA.*/ |
| 338 | public void buildNewRa(RaParams deprecatedParams, RaParams newParams) { |
| 339 | synchronized (mLock) { |
| 340 | if (deprecatedParams != null) { |
| 341 | mDeprecatedInfoTracker.putPrefixes(deprecatedParams.prefixes); |
| 342 | mDeprecatedInfoTracker.putDnses(deprecatedParams.dnses); |
| 343 | } |
| 344 | |
| 345 | if (newParams != null) { |
| 346 | // Process information that is no longer deprecated. |
| 347 | mDeprecatedInfoTracker.removePrefixes(newParams.prefixes); |
| 348 | mDeprecatedInfoTracker.removeDnses(newParams.dnses); |
| 349 | } |
| 350 | |
| 351 | mRaParams = newParams; |
| 352 | assembleRaLocked(); |
| 353 | } |
| 354 | |
| 355 | maybeNotifyMulticastTransmitter(); |
| 356 | } |
| 357 | |
| 358 | /** Start router advertisement daemon. */ |
| 359 | public boolean start() { |
| 360 | if (!createSocket()) { |
KH Shi | bdcd7db | 2023-03-16 15:23:13 +0800 | [diff] [blame^] | 361 | Log.e(TAG, "Failed to start RouterAdvertisementDaemon."); |
markchien | 74a4fa9 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 362 | return false; |
| 363 | } |
| 364 | |
| 365 | mMulticastTransmitter = new MulticastTransmitter(); |
| 366 | mMulticastTransmitter.start(); |
| 367 | |
KH Shi | bdcd7db | 2023-03-16 15:23:13 +0800 | [diff] [blame^] | 368 | mRaHandlerThread = new HandlerThread(TAG); |
| 369 | mRaHandlerThread.start(); |
| 370 | mRaMessageHandler = new RaMessageHandler(mRaHandlerThread.getLooper()); |
markchien | 74a4fa9 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 371 | |
KH Shi | bdcd7db | 2023-03-16 15:23:13 +0800 | [diff] [blame^] | 372 | return sendMessage(CMD_START); |
markchien | 74a4fa9 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | /** Stop router advertisement daemon. */ |
| 376 | public void stop() { |
KH Shi | bdcd7db | 2023-03-16 15:23:13 +0800 | [diff] [blame^] | 377 | if (!sendMessage(CMD_STOP)) { |
| 378 | Log.e(TAG, "RouterAdvertisementDaemon has been stopped or was never started."); |
| 379 | return; |
| 380 | } |
| 381 | |
| 382 | mRaHandlerThread.quitSafely(); |
| 383 | mRaHandlerThread = null; |
| 384 | mRaMessageHandler = null; |
| 385 | |
markchien | 74a4fa9 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 386 | closeSocket(); |
| 387 | // Wake up mMulticastTransmitter thread to interrupt a potential 1 day sleep before |
| 388 | // the thread's termination. |
| 389 | maybeNotifyMulticastTransmitter(); |
| 390 | mMulticastTransmitter = null; |
markchien | 74a4fa9 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | @GuardedBy("mLock") |
| 394 | private void assembleRaLocked() { |
| 395 | final ByteBuffer ra = ByteBuffer.wrap(mRA); |
| 396 | ra.order(ByteOrder.BIG_ENDIAN); |
| 397 | |
| 398 | final boolean haveRaParams = (mRaParams != null); |
| 399 | boolean shouldSendRA = false; |
| 400 | |
| 401 | try { |
| 402 | putHeader(ra, haveRaParams && mRaParams.hasDefaultRoute, |
| 403 | haveRaParams ? mRaParams.hopLimit : RaParams.DEFAULT_HOPLIMIT); |
| 404 | putSlla(ra, mInterface.macAddr.toByteArray()); |
| 405 | mRaLength = ra.position(); |
| 406 | |
| 407 | // https://tools.ietf.org/html/rfc5175#section-4 says: |
| 408 | // |
| 409 | // "MUST NOT be added to a Router Advertisement message |
| 410 | // if no flags in the option are set." |
| 411 | // |
| 412 | // putExpandedFlagsOption(ra); |
| 413 | |
| 414 | if (haveRaParams) { |
| 415 | putMtu(ra, mRaParams.mtu); |
| 416 | mRaLength = ra.position(); |
| 417 | |
| 418 | for (IpPrefix ipp : mRaParams.prefixes) { |
| 419 | putPio(ra, ipp, DEFAULT_LIFETIME, DEFAULT_LIFETIME); |
| 420 | mRaLength = ra.position(); |
| 421 | shouldSendRA = true; |
| 422 | } |
| 423 | |
| 424 | if (mRaParams.dnses.size() > 0) { |
| 425 | putRdnss(ra, mRaParams.dnses, DEFAULT_LIFETIME); |
| 426 | mRaLength = ra.position(); |
| 427 | shouldSendRA = true; |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | for (IpPrefix ipp : mDeprecatedInfoTracker.getPrefixes()) { |
| 432 | putPio(ra, ipp, 0, 0); |
| 433 | mRaLength = ra.position(); |
| 434 | shouldSendRA = true; |
| 435 | } |
| 436 | |
| 437 | final Set<Inet6Address> deprecatedDnses = mDeprecatedInfoTracker.getDnses(); |
| 438 | if (!deprecatedDnses.isEmpty()) { |
| 439 | putRdnss(ra, deprecatedDnses, 0); |
| 440 | mRaLength = ra.position(); |
| 441 | shouldSendRA = true; |
| 442 | } |
| 443 | } catch (BufferOverflowException e) { |
| 444 | // The packet up to mRaLength is valid, since it has been updated |
| 445 | // progressively as the RA was built. Log an error, and continue |
| 446 | // on as best as possible. |
| 447 | Log.e(TAG, "Could not construct new RA: " + e); |
| 448 | } |
| 449 | |
| 450 | // We have nothing worth announcing; indicate as much to maybeSendRA(). |
| 451 | if (!shouldSendRA) { |
| 452 | mRaLength = 0; |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | private void maybeNotifyMulticastTransmitter() { |
| 457 | final MulticastTransmitter m = mMulticastTransmitter; |
| 458 | if (m != null) { |
| 459 | m.hup(); |
| 460 | } |
| 461 | } |
| 462 | |
markchien | 74a4fa9 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 463 | private static byte asByte(int value) { |
| 464 | return (byte) value; |
| 465 | } |
| 466 | private static short asShort(int value) { |
| 467 | return (short) value; |
| 468 | } |
| 469 | |
| 470 | private static void putHeader(ByteBuffer ra, boolean hasDefaultRoute, byte hopLimit) { |
Xiao Ma | 3e557d7 | 2021-03-04 06:04:28 +0000 | [diff] [blame] | 471 | // RFC 4191 "high" preference, iff. advertising a default route. |
| 472 | final byte flags = hasDefaultRoute ? asByte(0x08) : asByte(0); |
| 473 | final short lifetime = hasDefaultRoute ? asShort(DEFAULT_LIFETIME) : asShort(0); |
| 474 | final Icmpv6Header icmpv6Header = |
| 475 | new Icmpv6Header(asByte(ICMPV6_ROUTER_ADVERTISEMENT) /* type */, |
| 476 | asByte(0) /* code */, asShort(0) /* checksum */); |
| 477 | final RaHeader raHeader = new RaHeader(hopLimit, flags, lifetime, 0 /* reachableTime */, |
| 478 | 0 /* retransTimer */); |
| 479 | icmpv6Header.writeToByteBuffer(ra); |
| 480 | raHeader.writeToByteBuffer(ra); |
markchien | 74a4fa9 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | private static void putSlla(ByteBuffer ra, byte[] slla) { |
markchien | 74a4fa9 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 484 | if (slla == null || slla.length != 6) { |
| 485 | // Only IEEE 802.3 6-byte addresses are supported. |
| 486 | return; |
| 487 | } |
| 488 | |
Xiao Ma | 3e557d7 | 2021-03-04 06:04:28 +0000 | [diff] [blame] | 489 | final ByteBuffer sllaOption = LlaOption.build(asByte(ICMPV6_ND_OPTION_SLLA), |
| 490 | MacAddress.fromBytes(slla)); |
| 491 | ra.put(sllaOption); |
markchien | 74a4fa9 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | private static void putExpandedFlagsOption(ByteBuffer ra) { |
| 495 | /** |
| 496 | Router Advertisement Expanded Flags Option |
| 497 | |
| 498 | 0 1 2 3 |
| 499 | 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 500 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 501 | | Type | Length | Bit fields available .. |
| 502 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 503 | ... for assignment | |
| 504 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 505 | */ |
| 506 | |
| 507 | final byte nd_option__efo = 26; |
| 508 | final byte efo_num_8octets = 1; |
| 509 | |
| 510 | ra.put(nd_option__efo) |
| 511 | .put(efo_num_8octets) |
| 512 | .putShort(asShort(0)) |
| 513 | .putInt(0); |
| 514 | } |
| 515 | |
| 516 | private static void putMtu(ByteBuffer ra, int mtu) { |
Xiao Ma | 3e557d7 | 2021-03-04 06:04:28 +0000 | [diff] [blame] | 517 | final ByteBuffer mtuOption = MtuOption.build((mtu < IPV6_MIN_MTU) ? IPV6_MIN_MTU : mtu); |
| 518 | ra.put(mtuOption); |
markchien | 74a4fa9 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | private static void putPio(ByteBuffer ra, IpPrefix ipp, |
| 522 | int validTime, int preferredTime) { |
markchien | 74a4fa9 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 523 | final int prefixLength = ipp.getPrefixLength(); |
| 524 | if (prefixLength != 64) { |
| 525 | return; |
| 526 | } |
markchien | 74a4fa9 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 527 | |
| 528 | if (validTime < 0) validTime = 0; |
| 529 | if (preferredTime < 0) preferredTime = 0; |
| 530 | if (preferredTime > validTime) preferredTime = validTime; |
| 531 | |
Xiao Ma | 3e557d7 | 2021-03-04 06:04:28 +0000 | [diff] [blame] | 532 | final ByteBuffer pioOption = PrefixInformationOption.build(ipp, |
| 533 | asByte(PIO_FLAG_ON_LINK | PIO_FLAG_AUTONOMOUS), validTime, preferredTime); |
| 534 | ra.put(pioOption); |
markchien | 74a4fa9 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | private static void putRio(ByteBuffer ra, IpPrefix ipp) { |
| 538 | /** |
| 539 | Route Information Option |
| 540 | |
| 541 | 0 1 2 3 |
| 542 | 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 543 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 544 | | Type | Length | Prefix Length |Resvd|Prf|Resvd| |
| 545 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 546 | | Route Lifetime | |
| 547 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 548 | | Prefix (Variable Length) | |
| 549 | . . |
| 550 | . . |
| 551 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 552 | */ |
| 553 | final int prefixLength = ipp.getPrefixLength(); |
| 554 | if (prefixLength > 64) { |
| 555 | return; |
| 556 | } |
| 557 | final byte nd_option_rio = 24; |
| 558 | final byte rio_num_8octets = asByte( |
| 559 | (prefixLength == 0) ? 1 : (prefixLength <= 8) ? 2 : 3); |
| 560 | |
| 561 | final byte[] addr = ipp.getAddress().getAddress(); |
| 562 | ra.put(nd_option_rio) |
| 563 | .put(rio_num_8octets) |
| 564 | .put(asByte(prefixLength)) |
| 565 | .put(asByte(0x18)) |
| 566 | .putInt(DEFAULT_LIFETIME); |
| 567 | |
| 568 | // Rely upon an IpPrefix's address being properly zeroed. |
| 569 | if (prefixLength > 0) { |
| 570 | ra.put(addr, 0, (prefixLength <= 64) ? 8 : 16); |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | private static void putRdnss(ByteBuffer ra, Set<Inet6Address> dnses, int lifetime) { |
markchien | 74a4fa9 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 575 | final HashSet<Inet6Address> filteredDnses = new HashSet<>(); |
| 576 | for (Inet6Address dns : dnses) { |
| 577 | if ((new LinkAddress(dns, RFC7421_PREFIX_LENGTH)).isGlobalPreferred()) { |
| 578 | filteredDnses.add(dns); |
| 579 | } |
| 580 | } |
| 581 | if (filteredDnses.isEmpty()) return; |
| 582 | |
Xiao Ma | 3e557d7 | 2021-03-04 06:04:28 +0000 | [diff] [blame] | 583 | final Inet6Address[] dnsesArray = |
| 584 | filteredDnses.toArray(new Inet6Address[filteredDnses.size()]); |
| 585 | final ByteBuffer rdnssOption = RdnssOption.build(lifetime, dnsesArray); |
| 586 | // NOTE: If the full of list DNS servers doesn't fit in the packet, |
| 587 | // this code will cause a buffer overflow and the RA won't include |
| 588 | // this instance of the option at all. |
| 589 | // |
| 590 | // TODO: Consider looking at ra.remaining() to determine how many |
| 591 | // DNS servers will fit, and adding only those. |
| 592 | ra.put(rdnssOption); |
markchien | 74a4fa9 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | private boolean createSocket() { |
| 596 | final int send_timout_ms = 300; |
| 597 | |
Xiao Ma | 3e557d7 | 2021-03-04 06:04:28 +0000 | [diff] [blame] | 598 | final int oldTag = TrafficStats.getAndSetThreadStatsTag(TAG_SYSTEM_NEIGHBOR); |
markchien | 74a4fa9 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 599 | try { |
KH Shi | bdcd7db | 2023-03-16 15:23:13 +0800 | [diff] [blame^] | 600 | mSocket = Os.socket(AF_INET6, SOCK_RAW | SOCK_NONBLOCK, IPPROTO_ICMPV6); |
markchien | 74a4fa9 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 601 | // Setting SNDTIMEO is purely for defensive purposes. |
| 602 | Os.setsockoptTimeval( |
| 603 | mSocket, SOL_SOCKET, SO_SNDTIMEO, StructTimeval.fromMillis(send_timout_ms)); |
markchien | 6cf0e55 | 2019-12-06 15:24:53 +0800 | [diff] [blame] | 604 | SocketUtils.bindSocketToInterface(mSocket, mInterface.name); |
markchien | f87ebdc | 2019-12-07 22:02:28 +0800 | [diff] [blame] | 605 | TetheringUtils.setupRaSocket(mSocket, mInterface.index); |
markchien | 74a4fa9 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 606 | } catch (ErrnoException | IOException e) { |
| 607 | Log.e(TAG, "Failed to create RA daemon socket: " + e); |
| 608 | return false; |
| 609 | } finally { |
| 610 | TrafficStats.setThreadStatsTag(oldTag); |
| 611 | } |
| 612 | |
| 613 | return true; |
| 614 | } |
| 615 | |
| 616 | private void closeSocket() { |
| 617 | if (mSocket != null) { |
| 618 | try { |
markchien | 6cf0e55 | 2019-12-06 15:24:53 +0800 | [diff] [blame] | 619 | SocketUtils.closeSocket(mSocket); |
markchien | 74a4fa9 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 620 | } catch (IOException ignored) { } |
| 621 | } |
| 622 | mSocket = null; |
| 623 | } |
| 624 | |
| 625 | private boolean isSocketValid() { |
| 626 | final FileDescriptor s = mSocket; |
| 627 | return (s != null) && s.valid(); |
| 628 | } |
| 629 | |
| 630 | private boolean isSuitableDestination(InetSocketAddress dest) { |
| 631 | if (mAllNodes.equals(dest)) { |
| 632 | return true; |
| 633 | } |
| 634 | |
| 635 | final InetAddress destip = dest.getAddress(); |
| 636 | return (destip instanceof Inet6Address) |
| 637 | && destip.isLinkLocalAddress() |
| 638 | && (((Inet6Address) destip).getScopeId() == mInterface.index); |
| 639 | } |
| 640 | |
| 641 | private void maybeSendRA(InetSocketAddress dest) { |
| 642 | if (dest == null || !isSuitableDestination(dest)) { |
| 643 | dest = mAllNodes; |
| 644 | } |
| 645 | |
| 646 | try { |
| 647 | synchronized (mLock) { |
Xiao Ma | 3e557d7 | 2021-03-04 06:04:28 +0000 | [diff] [blame] | 648 | if (mRaLength < ICMPV6_RA_HEADER_LEN) { |
markchien | 74a4fa9 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 649 | // No actual RA to send. |
| 650 | return; |
| 651 | } |
| 652 | Os.sendto(mSocket, mRA, 0, mRaLength, 0, dest); |
| 653 | } |
| 654 | Log.d(TAG, "RA sendto " + dest.getAddress().getHostAddress()); |
| 655 | } catch (ErrnoException | SocketException e) { |
| 656 | if (isSocketValid()) { |
| 657 | Log.e(TAG, "sendto error: " + e); |
| 658 | } |
| 659 | } |
| 660 | } |
| 661 | |
KH Shi | bdcd7db | 2023-03-16 15:23:13 +0800 | [diff] [blame^] | 662 | private boolean sendMessage(int cmd) { |
| 663 | if (mRaMessageHandler == null) { |
| 664 | return false; |
markchien | 74a4fa9 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 665 | } |
KH Shi | bdcd7db | 2023-03-16 15:23:13 +0800 | [diff] [blame^] | 666 | |
| 667 | return mRaMessageHandler.sendMessage(Message.obtain(mRaMessageHandler, cmd)); |
markchien | 74a4fa9 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | // TODO: Consider moving this to run on a provided Looper as a Handler, |
| 671 | // with WakeupMessage-style messages providing the timer driven input. |
| 672 | private final class MulticastTransmitter extends Thread { |
| 673 | private final Random mRandom = new Random(); |
| 674 | private final AtomicInteger mUrgentAnnouncements = new AtomicInteger(0); |
| 675 | |
| 676 | @Override |
| 677 | public void run() { |
| 678 | while (isSocketValid()) { |
| 679 | try { |
| 680 | Thread.sleep(getNextMulticastTransmitDelayMs()); |
| 681 | } catch (InterruptedException ignored) { |
| 682 | // Stop sleeping, immediately send an RA, and continue. |
| 683 | } |
| 684 | |
| 685 | maybeSendRA(mAllNodes); |
| 686 | synchronized (mLock) { |
| 687 | if (mDeprecatedInfoTracker.decrementCounters()) { |
| 688 | // At least one deprecated PIO has been removed; |
| 689 | // reassemble the RA. |
| 690 | assembleRaLocked(); |
| 691 | } |
| 692 | } |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | public void hup() { |
| 697 | // Set to one fewer that the desired number, because as soon as |
| 698 | // the thread interrupt is processed we immediately send an RA |
| 699 | // and mUrgentAnnouncements is not examined until the subsequent |
| 700 | // sleep interval computation (i.e. this way we send 3 and not 4). |
| 701 | mUrgentAnnouncements.set(MAX_URGENT_RTR_ADVERTISEMENTS - 1); |
| 702 | interrupt(); |
| 703 | } |
| 704 | |
| 705 | private int getNextMulticastTransmitDelaySec() { |
| 706 | boolean deprecationInProgress = false; |
| 707 | synchronized (mLock) { |
Xiao Ma | 3e557d7 | 2021-03-04 06:04:28 +0000 | [diff] [blame] | 708 | if (mRaLength < ICMPV6_RA_HEADER_LEN) { |
markchien | 74a4fa9 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 709 | // No actual RA to send; just sleep for 1 day. |
| 710 | return DAY_IN_SECONDS; |
| 711 | } |
| 712 | deprecationInProgress = !mDeprecatedInfoTracker.isEmpty(); |
| 713 | } |
| 714 | |
| 715 | final int urgentPending = mUrgentAnnouncements.getAndDecrement(); |
| 716 | if ((urgentPending > 0) || deprecationInProgress) { |
| 717 | return MIN_DELAY_BETWEEN_RAS_SEC; |
| 718 | } |
| 719 | |
| 720 | return MIN_RTR_ADV_INTERVAL_SEC + mRandom.nextInt( |
| 721 | MAX_RTR_ADV_INTERVAL_SEC - MIN_RTR_ADV_INTERVAL_SEC); |
| 722 | } |
| 723 | |
| 724 | private long getNextMulticastTransmitDelayMs() { |
| 725 | return 1000 * (long) getNextMulticastTransmitDelaySec(); |
| 726 | } |
| 727 | } |
| 728 | } |