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