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_FLAGS_OFFSET = 6;
93     public static final int IPV4_FRAGMENT_MASK = 0x1fff;
94     public static final int IPV4_PROTOCOL_OFFSET = 9;
95     public static final int IPV4_SRC_ADDR_OFFSET = 12;
96     public static final int IPV4_DST_ADDR_OFFSET = 16;
97     public static final int IPV4_ADDR_LEN = 4;
98     public static final Inet4Address IPV4_ADDR_ALL = makeInet4Address(
99             (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff);
100     public static final Inet4Address IPV4_ADDR_ANY = makeInet4Address(
101             (byte) 0, (byte) 0, (byte) 0, (byte) 0);
102     public static final Inet6Address IPV6_ADDR_ANY = makeInet6Address(new byte[]{
103             (byte) 0, (byte) 0, (byte) 0, (byte) 0,
104             (byte) 0, (byte) 0, (byte) 0, (byte) 0,
105             (byte) 0, (byte) 0, (byte) 0, (byte) 0,
106             (byte) 0, (byte) 0, (byte) 0, (byte) 0 });
107     /**
108      * IPv6 constants.
109      *
110      * See also:
111      *     - https://tools.ietf.org/html/rfc2460
112      */
113     public static final int IPV6_ADDR_LEN = 16;
114     public static final int IPV6_HEADER_LEN = 40;
115     public static final int IPV6_LEN_OFFSET = 4;
116     public static final int IPV6_PROTOCOL_OFFSET = 6;
117     public static final int IPV6_SRC_ADDR_OFFSET = 8;
118     public static final int IPV6_DST_ADDR_OFFSET = 24;
119     public static final int IPV6_MIN_MTU = 1280;
120     public static final Inet6Address IPV6_ADDR_ALL_NODES_MULTICAST =
121             (Inet6Address) InetAddresses.parseNumericAddress("ff02::1");
122     public static final Inet6Address IPV6_ADDR_ALL_ROUTERS_MULTICAST =
123             (Inet6Address) InetAddresses.parseNumericAddress("ff02::2");
124     public static final Inet6Address IPV6_ADDR_ALL_HOSTS_MULTICAST =
125             (Inet6Address) InetAddresses.parseNumericAddress("ff02::3");
126 
127     /**
128      * ICMPv6 constants.
129      *
130      * See also:
131      *     - https://tools.ietf.org/html/rfc4443
132      *     - https://tools.ietf.org/html/rfc4861
133      */
134     public static final int ICMPV6_HEADER_MIN_LEN = 4;
135     public static final int ICMPV6_CHECKSUM_OFFSET = 2;
136     public static final int ICMPV6_ECHO_REPLY_TYPE = 129;
137     public static final int ICMPV6_ECHO_REQUEST_TYPE = 128;
138     public static final int ICMPV6_ROUTER_SOLICITATION    = 133;
139     public static final int ICMPV6_ROUTER_ADVERTISEMENT   = 134;
140     public static final int ICMPV6_NEIGHBOR_SOLICITATION  = 135;
141     public static final int ICMPV6_NEIGHBOR_ADVERTISEMENT = 136;
142     public static final int ICMPV6_ND_OPTION_MIN_LENGTH = 8;
143     public static final int ICMPV6_ND_OPTION_LENGTH_SCALING_FACTOR = 8;
144     public static final int ICMPV6_ND_OPTION_SLLA  = 1;
145     public static final int ICMPV6_ND_OPTION_TLLA  = 2;
146     public static final int ICMPV6_ND_OPTION_PIO   = 3;
147     public static final int ICMPV6_ND_OPTION_MTU   = 5;
148     public static final int ICMPV6_ND_OPTION_RDNSS = 25;
149     public static final int ICMPV6_ND_OPTION_PREF64 = 38;
150 
151     public static final int ICMPV6_RA_HEADER_LEN = 16;
152 
153     public static final int NEIGHBOR_ADVERTISEMENT_FLAG_ROUTER    = 1 << 31;
154     public static final int NEIGHBOR_ADVERTISEMENT_FLAG_SOLICITED = 1 << 30;
155     public static final int NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE  = 1 << 29;
156 
157     public static final byte ROUTER_ADVERTISEMENT_FLAG_MANAGED_ADDRESS = (byte) (1 << 7);
158     public static final byte ROUTER_ADVERTISEMENT_FLAG_OTHER = (byte) (1 << 6);
159 
160     public static final byte PIO_FLAG_ON_LINK = (byte) (1 << 7);
161     public static final byte PIO_FLAG_AUTONOMOUS = (byte) (1 << 6);
162 
163     /**
164      * UDP constants.
165      *
166      * See also:
167      *     - https://tools.ietf.org/html/rfc768
168      */
169     public static final int UDP_HEADER_LEN = 8;
170 
171 
172     /**
173      * DHCP constants.
174      *
175      * See also:
176      *     - https://tools.ietf.org/html/rfc2131
177      */
178     public static final int INFINITE_LEASE = 0xffffffff;
179     public static final int DHCP4_CLIENT_PORT = 68;
180 
181     /**
182      * IEEE802.11 standard constants.
183      *
184      * See also:
185      *     - https://ieeexplore.ieee.org/document/7786995
186      */
187     public static final int VENDOR_SPECIFIC_IE_ID = 0xdd;
188 
189 
190     /**
191      * TrafficStats constants.
192      */
193     // These tags are used by the network stack to do traffic for its own purposes. Traffic
194     // tagged with these will be counted toward the network stack and must stay inside the
195     // range defined by
196     // {@link android.net.TrafficStats#TAG_NETWORK_STACK_RANGE_START} and
197     // {@link android.net.TrafficStats#TAG_NETWORK_STACK_RANGE_END}.
198     public static final int TAG_SYSTEM_DHCP = 0xFFFFFE01;
199     public static final int TAG_SYSTEM_NEIGHBOR = 0xFFFFFE02;
200     public static final int TAG_SYSTEM_DHCP_SERVER = 0xFFFFFE03;
201 
202     // These tags are used by the network stack to do traffic on behalf of apps. Traffic
203     // tagged with these will be counted toward the app on behalf of which the network
204     // stack is doing this traffic. These values must stay inside the range defined by
205     // {@link android.net.TrafficStats#TAG_NETWORK_STACK_IMPERSONATION_RANGE_START} and
206     // {@link android.net.TrafficStats#TAG_NETWORK_STACK_IMPERSONATION_RANGE_END}.
207     public static final int TAG_SYSTEM_PROBE = 0xFFFFFF81;
208     public static final int TAG_SYSTEM_DNS = 0xFFFFFF82;
209 
210     // TODO: Move to Inet4AddressUtils
211     // See aosp/1455936: NetworkStackConstants can't depend on it as it causes jarjar-related issues
212     // for users of both the net-utils-device-common and net-utils-framework-common libraries.
213     // Jarjar rule management needs to be simplified for that: b/170445871
214 
215     /**
216      * Make an Inet4Address from 4 bytes in network byte order.
217      */
makeInet4Address(byte b1, byte b2, byte b3, byte b4)218     private static Inet4Address makeInet4Address(byte b1, byte b2, byte b3, byte b4) {
219         try {
220             return (Inet4Address) InetAddress.getByAddress(new byte[] { b1, b2, b3, b4 });
221         } catch (UnknownHostException e) {
222             throw new IllegalArgumentException("addr must be 4 bytes: this should never happen");
223         }
224     }
225 
226     /**
227      * Make an Inet6Address from 16 bytes in network byte order.
228      */
makeInet6Address(byte[] bytes)229     private static Inet6Address makeInet6Address(byte[] bytes) {
230         try {
231             return (Inet6Address) InetAddress.getByAddress(bytes);
232         } catch (UnknownHostException e) {
233             throw new IllegalArgumentException("addr must be 16 bytes: this should never happen");
234         }
235     }
NetworkStackConstants()236     private NetworkStackConstants() {
237         throw new UnsupportedOperationException("This class is not to be instantiated");
238     }
239 }
240