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