1 /* 2 * Copyright (C) 2020 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.net.module.util; 18 19 import android.net.InetAddresses; 20 21 import java.net.Inet4Address; 22 import java.net.Inet6Address; 23 import java.net.InetAddress; 24 import java.net.UnknownHostException; 25 26 /** 27 * Network constants used by the network stack. 28 * @hide 29 */ 30 public final class NetworkStackConstants { 31 32 /** 33 * Ethernet constants. 34 * 35 * See also: 36 * - https://tools.ietf.org/html/rfc894 37 * - https://tools.ietf.org/html/rfc2464 38 * - https://tools.ietf.org/html/rfc7042 39 * - http://www.iana.org/assignments/ethernet-numbers/ethernet-numbers.xhtml 40 * - http://www.iana.org/assignments/ieee-802-numbers/ieee-802-numbers.xhtml 41 */ 42 public static final int ETHER_DST_ADDR_OFFSET = 0; 43 public static final int ETHER_SRC_ADDR_OFFSET = 6; 44 public static final int ETHER_ADDR_LEN = 6; 45 public static final int ETHER_TYPE_OFFSET = 12; 46 public static final int ETHER_TYPE_LENGTH = 2; 47 public static final int ETHER_TYPE_ARP = 0x0806; 48 public static final int ETHER_TYPE_IPV4 = 0x0800; 49 public static final int ETHER_TYPE_IPV6 = 0x86dd; 50 public static final int ETHER_HEADER_LEN = 14; 51 public static final int ETHER_MTU = 1500; 52 public static final byte[] ETHER_BROADCAST = new byte[] { 53 (byte) 0xff, (byte) 0xff, (byte) 0xff, 54 (byte) 0xff, (byte) 0xff, (byte) 0xff, 55 }; 56 57 /** 58 * ARP constants. 59 * 60 * See also: 61 * - https://tools.ietf.org/html/rfc826 62 * - http://www.iana.org/assignments/arp-parameters/arp-parameters.xhtml 63 */ 64 public static final int ARP_PAYLOAD_LEN = 28; // For Ethernet+IPv4. 65 public static final int ARP_ETHER_IPV4_LEN = ARP_PAYLOAD_LEN + ETHER_HEADER_LEN; 66 public static final int ARP_REQUEST = 1; 67 public static final int ARP_REPLY = 2; 68 public static final int ARP_HWTYPE_RESERVED_LO = 0; 69 public static final int ARP_HWTYPE_ETHER = 1; 70 public static final int ARP_HWTYPE_RESERVED_HI = 0xffff; 71 72 /** 73 * IPv4 Address Conflict Detection constants. 74 * 75 * See also: 76 * - https://tools.ietf.org/html/rfc5227 77 */ 78 public static final int IPV4_CONFLICT_PROBE_NUM = 3; 79 public static final int IPV4_CONFLICT_ANNOUNCE_NUM = 2; 80 81 /** 82 * IPv4 constants. 83 * 84 * See also: 85 * - https://tools.ietf.org/html/rfc791 86 */ 87 public static final int IPV4_ADDR_BITS = 32; 88 public static final int IPV4_MIN_MTU = 68; 89 public static final int IPV4_MAX_MTU = 65_535; 90 public static final int IPV4_HEADER_MIN_LEN = 20; 91 public static final int IPV4_IHL_MASK = 0xf; 92 public static final int IPV4_LENGTH_OFFSET = 2; 93 public static final int IPV4_FLAGS_OFFSET = 6; 94 public static final int IPV4_FRAGMENT_MASK = 0x1fff; 95 public static final int IPV4_PROTOCOL_OFFSET = 9; 96 public static final int IPV4_CHECKSUM_OFFSET = 10; 97 public static final int IPV4_SRC_ADDR_OFFSET = 12; 98 public static final int IPV4_DST_ADDR_OFFSET = 16; 99 public static final int IPV4_ADDR_LEN = 4; 100 public static final int IPV4_FLAG_MF = 0x2000; 101 public static final int IPV4_FLAG_DF = 0x4000; 102 public static final Inet4Address IPV4_ADDR_ALL = makeInet4Address( 103 (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff); 104 public static final Inet4Address IPV4_ADDR_ANY = makeInet4Address( 105 (byte) 0, (byte) 0, (byte) 0, (byte) 0); 106 public static final Inet6Address IPV6_ADDR_ANY = makeInet6Address(new byte[]{ 107 (byte) 0, (byte) 0, (byte) 0, (byte) 0, 108 (byte) 0, (byte) 0, (byte) 0, (byte) 0, 109 (byte) 0, (byte) 0, (byte) 0, (byte) 0, 110 (byte) 0, (byte) 0, (byte) 0, (byte) 0 }); 111 /** 112 * IPv6 constants. 113 * 114 * See also: 115 * - https://tools.ietf.org/html/rfc2460 116 */ 117 public static final int IPV6_ADDR_LEN = 16; 118 public static final int IPV6_HEADER_LEN = 40; 119 public static final int IPV6_LEN_OFFSET = 4; 120 public static final int IPV6_PROTOCOL_OFFSET = 6; 121 public static final int IPV6_SRC_ADDR_OFFSET = 8; 122 public static final int IPV6_DST_ADDR_OFFSET = 24; 123 public static final int IPV6_MIN_MTU = 1280; 124 public static final Inet6Address IPV6_ADDR_ALL_NODES_MULTICAST = 125 (Inet6Address) InetAddresses.parseNumericAddress("ff02::1"); 126 public static final Inet6Address IPV6_ADDR_ALL_ROUTERS_MULTICAST = 127 (Inet6Address) InetAddresses.parseNumericAddress("ff02::2"); 128 public static final Inet6Address IPV6_ADDR_ALL_HOSTS_MULTICAST = 129 (Inet6Address) InetAddresses.parseNumericAddress("ff02::3"); 130 131 /** 132 * ICMP constants. 133 * 134 * See also: 135 * - https://tools.ietf.org/html/rfc792 136 */ 137 public static final int ICMP_CHECKSUM_OFFSET = 2; 138 public static final int ICMP_HEADER_LEN = 8; 139 /** 140 * ICMPv6 constants. 141 * 142 * See also: 143 * - https://tools.ietf.org/html/rfc4443 144 * - https://tools.ietf.org/html/rfc4861 145 */ 146 public static final int ICMPV6_HEADER_MIN_LEN = 4; 147 public static final int ICMPV6_CHECKSUM_OFFSET = 2; 148 public static final int ICMPV6_ECHO_REPLY_TYPE = 129; 149 public static final int ICMPV6_ECHO_REQUEST_TYPE = 128; 150 public static final int ICMPV6_ROUTER_SOLICITATION = 133; 151 public static final int ICMPV6_ROUTER_ADVERTISEMENT = 134; 152 public static final int ICMPV6_NEIGHBOR_SOLICITATION = 135; 153 public static final int ICMPV6_NEIGHBOR_ADVERTISEMENT = 136; 154 public static final int ICMPV6_ND_OPTION_MIN_LENGTH = 8; 155 public static final int ICMPV6_ND_OPTION_LENGTH_SCALING_FACTOR = 8; 156 public static final int ICMPV6_ND_OPTION_SLLA = 1; 157 public static final int ICMPV6_ND_OPTION_TLLA = 2; 158 public static final int ICMPV6_ND_OPTION_PIO = 3; 159 public static final int ICMPV6_ND_OPTION_MTU = 5; 160 public static final int ICMPV6_ND_OPTION_RDNSS = 25; 161 public static final int ICMPV6_ND_OPTION_PREF64 = 38; 162 163 public static final int ICMPV6_RS_HEADER_LEN = 8; 164 public static final int ICMPV6_RA_HEADER_LEN = 16; 165 public static final int ICMPV6_NS_HEADER_LEN = 24; 166 public static final int ICMPV6_NA_HEADER_LEN = 24; 167 168 public static final int NEIGHBOR_ADVERTISEMENT_FLAG_ROUTER = 1 << 31; 169 public static final int NEIGHBOR_ADVERTISEMENT_FLAG_SOLICITED = 1 << 30; 170 public static final int NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE = 1 << 29; 171 172 public static final byte ROUTER_ADVERTISEMENT_FLAG_MANAGED_ADDRESS = (byte) (1 << 7); 173 public static final byte ROUTER_ADVERTISEMENT_FLAG_OTHER = (byte) (1 << 6); 174 175 public static final byte PIO_FLAG_ON_LINK = (byte) (1 << 7); 176 public static final byte PIO_FLAG_AUTONOMOUS = (byte) (1 << 6); 177 178 /** 179 * TCP constants. 180 * 181 * See also: 182 * - https://tools.ietf.org/html/rfc793 183 */ 184 public static final int TCP_HEADER_MIN_LEN = 20; 185 public static final int TCP_CHECKSUM_OFFSET = 16; 186 public static final byte TCPHDR_FIN = (byte) (1 << 0); 187 public static final byte TCPHDR_SYN = (byte) (1 << 1); 188 public static final byte TCPHDR_RST = (byte) (1 << 2); 189 public static final byte TCPHDR_PSH = (byte) (1 << 3); 190 public static final byte TCPHDR_ACK = (byte) (1 << 4); 191 public static final byte TCPHDR_URG = (byte) (1 << 5); 192 193 /** 194 * UDP constants. 195 * 196 * See also: 197 * - https://tools.ietf.org/html/rfc768 198 */ 199 public static final int UDP_HEADER_LEN = 8; 200 public static final int UDP_LENGTH_OFFSET = 4; 201 public static final int UDP_CHECKSUM_OFFSET = 6; 202 203 /** 204 * DHCP constants. 205 * 206 * See also: 207 * - https://tools.ietf.org/html/rfc2131 208 */ 209 public static final int INFINITE_LEASE = 0xffffffff; 210 public static final int DHCP4_CLIENT_PORT = 68; 211 // The maximum length of a DHCP packet that can be constructed. 212 public static final int DHCP_MAX_LENGTH = 1500; 213 public static final int DHCP_MAX_OPTION_LEN = 255; 214 215 /** 216 * DHCPv6 constants. 217 * 218 * See also: 219 * - https://datatracker.ietf.org/doc/html/rfc8415 220 * - https://www.iana.org/assignments/dhcpv6-parameters/dhcpv6-parameters.xhtml 221 */ 222 public static final int DHCP6_CLIENT_PORT = 546; 223 public static final int DHCP6_SERVER_PORT = 547; 224 public static final Inet6Address ALL_DHCP_RELAY_AGENTS_AND_SERVERS = 225 (Inet6Address) InetAddresses.parseNumericAddress("ff02::1:2"); 226 public static final int DHCP6_OPTION_IA_PD = 25; 227 public static final int DHCP6_OPTION_IAPREFIX = 26; 228 229 /** 230 * IEEE802.11 standard constants. 231 * 232 * See also: 233 * - https://ieeexplore.ieee.org/document/7786995 234 */ 235 public static final int VENDOR_SPECIFIC_IE_ID = 0xdd; 236 237 238 /** 239 * TrafficStats constants. 240 */ 241 // These tags are used by the network stack to do traffic for its own purposes. Traffic 242 // tagged with these will be counted toward the network stack and must stay inside the 243 // range defined by 244 // {@link android.net.TrafficStats#TAG_NETWORK_STACK_RANGE_START} and 245 // {@link android.net.TrafficStats#TAG_NETWORK_STACK_RANGE_END}. 246 public static final int TAG_SYSTEM_DHCP = 0xFFFFFE01; 247 public static final int TAG_SYSTEM_NEIGHBOR = 0xFFFFFE02; 248 public static final int TAG_SYSTEM_DHCP_SERVER = 0xFFFFFE03; 249 250 // These tags are used by the network stack to do traffic on behalf of apps. Traffic 251 // tagged with these will be counted toward the app on behalf of which the network 252 // stack is doing this traffic. These values must stay inside the range defined by 253 // {@link android.net.TrafficStats#TAG_NETWORK_STACK_IMPERSONATION_RANGE_START} and 254 // {@link android.net.TrafficStats#TAG_NETWORK_STACK_IMPERSONATION_RANGE_END}. 255 public static final int TAG_SYSTEM_PROBE = 0xFFFFFF81; 256 public static final int TAG_SYSTEM_DNS = 0xFFFFFF82; 257 258 /** 259 * A test URL used to override configuration settings and overlays for the network validation 260 * HTTPS URL, when set in {@link android.provider.DeviceConfig} configuration. 261 * 262 * <p>This URL will be ignored if the host is not "localhost" (it can only be used to test with 263 * a local test server), and must not be set in production scenarios (as enforced by CTS tests). 264 * 265 * <p>{@link #TEST_URL_EXPIRATION_TIME} must also be set to use this setting. 266 */ 267 public static final String TEST_CAPTIVE_PORTAL_HTTPS_URL = "test_captive_portal_https_url"; 268 /** 269 * A test URL used to override configuration settings and overlays for the network validation 270 * HTTP URL, when set in {@link android.provider.DeviceConfig} configuration. 271 * 272 * <p>This URL will be ignored if the host is not "localhost" (it can only be used to test with 273 * a local test server), and must not be set in production scenarios (as enforced by CTS tests). 274 * 275 * <p>{@link #TEST_URL_EXPIRATION_TIME} must also be set to use this setting. 276 */ 277 public static final String TEST_CAPTIVE_PORTAL_HTTP_URL = "test_captive_portal_http_url"; 278 /** 279 * Expiration time of the test URL, in ms, relative to {@link System#currentTimeMillis()}. 280 * 281 * <p>After this expiration time, test URLs will be ignored. They will also be ignored if 282 * the expiration time is more than 10 minutes in the future, to avoid misconfiguration 283 * following test runs. 284 */ 285 public static final String TEST_URL_EXPIRATION_TIME = "test_url_expiration_time"; 286 287 // TODO: Move to Inet4AddressUtils 288 // See aosp/1455936: NetworkStackConstants can't depend on it as it causes jarjar-related issues 289 // for users of both the net-utils-device-common and net-utils-framework-common libraries. 290 // Jarjar rule management needs to be simplified for that: b/170445871 291 292 /** 293 * Make an Inet4Address from 4 bytes in network byte order. 294 */ makeInet4Address(byte b1, byte b2, byte b3, byte b4)295 private static Inet4Address makeInet4Address(byte b1, byte b2, byte b3, byte b4) { 296 try { 297 return (Inet4Address) InetAddress.getByAddress(new byte[] { b1, b2, b3, b4 }); 298 } catch (UnknownHostException e) { 299 throw new IllegalArgumentException("addr must be 4 bytes: this should never happen"); 300 } 301 } 302 303 /** 304 * Make an Inet6Address from 16 bytes in network byte order. 305 */ makeInet6Address(byte[] bytes)306 private static Inet6Address makeInet6Address(byte[] bytes) { 307 try { 308 return (Inet6Address) InetAddress.getByAddress(bytes); 309 } catch (UnknownHostException e) { 310 throw new IllegalArgumentException("addr must be 16 bytes: this should never happen"); 311 } 312 } NetworkStackConstants()313 private NetworkStackConstants() { 314 throw new UnsupportedOperationException("This class is not to be instantiated"); 315 } 316 } 317