1 /*
2  * Copyright (C) 2018 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.server.util;
18 
19 import static com.android.net.module.util.Inet4AddressUtils.intToInet4AddressHTH;
20 
21 import java.net.Inet4Address;
22 
23 /**
24  * Network constants used by the network stack.
25  */
26 public final class NetworkStackConstants {
27 
28     /**
29      * Ethernet constants.
30      *
31      * See also:
32      *     - https://tools.ietf.org/html/rfc894
33      *     - https://tools.ietf.org/html/rfc2464
34      *     - https://tools.ietf.org/html/rfc7042
35      *     - http://www.iana.org/assignments/ethernet-numbers/ethernet-numbers.xhtml
36      *     - http://www.iana.org/assignments/ieee-802-numbers/ieee-802-numbers.xhtml
37      */
38     public static final int ETHER_DST_ADDR_OFFSET = 0;
39     public static final int ETHER_SRC_ADDR_OFFSET = 6;
40     public static final int ETHER_ADDR_LEN = 6;
41     public static final int ETHER_TYPE_OFFSET = 12;
42     public static final int ETHER_TYPE_LENGTH = 2;
43     public static final int ETHER_TYPE_ARP  = 0x0806;
44     public static final int ETHER_TYPE_IPV4 = 0x0800;
45     public static final int ETHER_TYPE_IPV6 = 0x86dd;
46     public static final int ETHER_HEADER_LEN = 14;
47     public static final int ETHER_MTU = 1500;
48 
49     /**
50      * ARP constants.
51      *
52      * See also:
53      *     - https://tools.ietf.org/html/rfc826
54      *     - http://www.iana.org/assignments/arp-parameters/arp-parameters.xhtml
55      */
56     public static final int ARP_PAYLOAD_LEN = 28;  // For Ethernet+IPv4.
57     public static final int ARP_ETHER_IPV4_LEN = ARP_PAYLOAD_LEN + ETHER_HEADER_LEN;
58     public static final int ARP_REQUEST = 1;
59     public static final int ARP_REPLY   = 2;
60     public static final int ARP_HWTYPE_RESERVED_LO = 0;
61     public static final int ARP_HWTYPE_ETHER       = 1;
62     public static final int ARP_HWTYPE_RESERVED_HI = 0xffff;
63 
64     /**
65      * IPv4 Address Conflict Detection constants.
66      *
67      * See also:
68      *     - https://tools.ietf.org/html/rfc5227
69      */
70     public static final int IPV4_CONFLICT_PROBE_NUM = 3;
71     public static final int IPV4_CONFLICT_ANNOUNCE_NUM = 2;
72 
73     /**
74      * IPv4 constants.
75      *
76      * See also:
77      *     - https://tools.ietf.org/html/rfc791
78      */
79     public static final int IPV4_ADDR_BITS = 32;
80     public static final int IPV4_MIN_MTU = 68;
81     public static final int IPV4_MAX_MTU = 65_535;
82     public static final int IPV4_HEADER_MIN_LEN = 20;
83     public static final int IPV4_IHL_MASK = 0xf;
84     public static final int IPV4_FLAGS_OFFSET = 6;
85     public static final int IPV4_FRAGMENT_MASK = 0x1fff;
86     public static final int IPV4_PROTOCOL_OFFSET = 9;
87     public static final int IPV4_SRC_ADDR_OFFSET = 12;
88     public static final int IPV4_DST_ADDR_OFFSET = 16;
89     public static final int IPV4_ADDR_LEN = 4;
90     public static final Inet4Address IPV4_ADDR_ALL = intToInet4AddressHTH(0xffffffff);
91     public static final Inet4Address IPV4_ADDR_ANY = intToInet4AddressHTH(0x0);
92 
93     /**
94      * IPv6 constants.
95      *
96      * See also:
97      *     - https://tools.ietf.org/html/rfc2460
98      */
99     public static final int IPV6_ADDR_LEN = 16;
100     public static final int IPV6_HEADER_LEN = 40;
101     public static final int IPV6_LEN_OFFSET = 4;
102     public static final int IPV6_PROTOCOL_OFFSET = 6;
103     public static final int IPV6_SRC_ADDR_OFFSET = 8;
104     public static final int IPV6_DST_ADDR_OFFSET = 24;
105     public static final int IPV6_MIN_MTU = 1280;
106 
107     /**
108      * ICMPv6 constants.
109      *
110      * See also:
111      *     - https://tools.ietf.org/html/rfc4443
112      *     - https://tools.ietf.org/html/rfc4861
113      */
114     public static final int ICMPV6_HEADER_MIN_LEN = 4;
115     public static final int ICMPV6_CHECKSUM_OFFSET = 2;
116     public static final int ICMPV6_ECHO_REPLY_TYPE = 129;
117     public static final int ICMPV6_ECHO_REQUEST_TYPE = 128;
118     public static final int ICMPV6_ROUTER_SOLICITATION    = 133;
119     public static final int ICMPV6_ROUTER_ADVERTISEMENT   = 134;
120     public static final int ICMPV6_NEIGHBOR_SOLICITATION  = 135;
121     public static final int ICMPV6_NEIGHBOR_ADVERTISEMENT = 136;
122     public static final int ICMPV6_ND_OPTION_MIN_LENGTH = 8;
123     public static final int ICMPV6_ND_OPTION_LENGTH_SCALING_FACTOR = 8;
124     public static final int ICMPV6_ND_OPTION_SLLA  = 1;
125     public static final int ICMPV6_ND_OPTION_TLLA  = 2;
126     public static final int ICMPV6_ND_OPTION_PIO   = 3;
127     public static final int ICMPV6_ND_OPTION_MTU   = 5;
128     public static final int ICMPV6_ND_OPTION_RDNSS = 25;
129     public static final int ICMPV6_ND_OPTION_PREF64 = 38;
130 
131 
132     public static final int ICMPV6_RA_HEADER_LEN = 16;
133 
134     /**
135      * UDP constants.
136      *
137      * See also:
138      *     - https://tools.ietf.org/html/rfc768
139      */
140     public static final int UDP_HEADER_LEN = 8;
141 
142 
143     /**
144      * DHCP constants.
145      *
146      * See also:
147      *     - https://tools.ietf.org/html/rfc2131
148      */
149     public static final int INFINITE_LEASE = 0xffffffff;
150     public static final int DHCP4_CLIENT_PORT = 68;
151 
152     /**
153      * IEEE802.11 standard constants.
154      *
155      * See also:
156      *     - https://ieeexplore.ieee.org/document/7786995
157      */
158     public static final int VENDOR_SPECIFIC_IE_ID = 0xdd;
159 
NetworkStackConstants()160     private NetworkStackConstants() {
161         throw new UnsupportedOperationException("This class is not to be instantiated");
162     }
163 }
164