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