blob: 058cb941bfbcd84568936a3e5fcc4e066cc2b8c9 [file] [log] [blame]
Hugo Benichi59c8e422017-10-12 21:33:40 +09001/*
2 * Copyright 2017 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
17package android.net;
18
Hugo Benichi84bb7fc2017-11-16 14:40:16 +090019import android.annotation.IntDef;
Hugo Benichi48872c62018-01-12 09:46:29 +090020import android.annotation.NonNull;
Etan Cohena8923c52018-11-02 15:07:20 -070021import android.annotation.Nullable;
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010022import android.annotation.UnsupportedAppUsage;
Hugo Benichiac52e402017-11-09 00:22:25 +090023import android.os.Parcel;
24import android.os.Parcelable;
25
26import com.android.internal.util.BitUtils;
Hugo Benichi84bb7fc2017-11-16 14:40:16 +090027import com.android.internal.util.Preconditions;
Hugo Benichi59c8e422017-10-12 21:33:40 +090028
Hugo Benichi84bb7fc2017-11-16 14:40:16 +090029import java.lang.annotation.Retention;
30import java.lang.annotation.RetentionPolicy;
Etan Cohena8923c52018-11-02 15:07:20 -070031import java.net.Inet6Address;
32import java.net.UnknownHostException;
Jong Wook Kimf0a55cc2018-01-31 19:03:19 -080033import java.security.SecureRandom;
Hugo Benichi59c8e422017-10-12 21:33:40 +090034import java.util.Arrays;
Hugo Benichiac52e402017-11-09 00:22:25 +090035import java.util.Random;
Hugo Benichi59c8e422017-10-12 21:33:40 +090036
37/**
Hugo Benichi84bb7fc2017-11-16 14:40:16 +090038 * Representation of a MAC address.
39 *
40 * This class only supports 48 bits long addresses and does not support 64 bits long addresses.
41 * Instances of this class are immutable.
Hugo Benichi59c8e422017-10-12 21:33:40 +090042 */
Hugo Benichiac52e402017-11-09 00:22:25 +090043public final class MacAddress implements Parcelable {
Hugo Benichi59c8e422017-10-12 21:33:40 +090044
45 private static final int ETHER_ADDR_LEN = 6;
Hugo Benichiac52e402017-11-09 00:22:25 +090046 private static final byte[] ETHER_ADDR_BROADCAST = addr(0xff, 0xff, 0xff, 0xff, 0xff, 0xff);
Hugo Benichi59c8e422017-10-12 21:33:40 +090047
Hugo Benichi84bb7fc2017-11-16 14:40:16 +090048 /**
49 * The MacAddress representing the unique broadcast MAC address.
50 */
51 public static final MacAddress BROADCAST_ADDRESS = MacAddress.fromBytes(ETHER_ADDR_BROADCAST);
Hugo Benichiac52e402017-11-09 00:22:25 +090052
Hugo Benichi84bb7fc2017-11-16 14:40:16 +090053 /**
54 * The MacAddress zero MAC address.
55 * @hide
56 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010057 @UnsupportedAppUsage
Hugo Benichiac52e402017-11-09 00:22:25 +090058 public static final MacAddress ALL_ZEROS_ADDRESS = new MacAddress(0);
59
Hugo Benichi84bb7fc2017-11-16 14:40:16 +090060 /** @hide */
61 @Retention(RetentionPolicy.SOURCE)
62 @IntDef(prefix = { "TYPE_" }, value = {
63 TYPE_UNKNOWN,
64 TYPE_UNICAST,
65 TYPE_MULTICAST,
66 TYPE_BROADCAST,
67 })
68 public @interface MacAddressType { }
Hugo Benichi59c8e422017-10-12 21:33:40 +090069
Hugo Benichi48872c62018-01-12 09:46:29 +090070 /** @hide Indicates a MAC address of unknown type. */
Hugo Benichi84bb7fc2017-11-16 14:40:16 +090071 public static final int TYPE_UNKNOWN = 0;
72 /** Indicates a MAC address is a unicast address. */
73 public static final int TYPE_UNICAST = 1;
74 /** Indicates a MAC address is a multicast address. */
75 public static final int TYPE_MULTICAST = 2;
76 /** Indicates a MAC address is the broadcast address. */
77 public static final int TYPE_BROADCAST = 3;
Hugo Benichiac52e402017-11-09 00:22:25 +090078
Hugo Benichi84bb7fc2017-11-16 14:40:16 +090079 private static final long VALID_LONG_MASK = (1L << 48) - 1;
80 private static final long LOCALLY_ASSIGNED_MASK = MacAddress.fromString("2:0:0:0:0:0").mAddr;
81 private static final long MULTICAST_MASK = MacAddress.fromString("1:0:0:0:0:0").mAddr;
82 private static final long OUI_MASK = MacAddress.fromString("ff:ff:ff:0:0:0").mAddr;
83 private static final long NIC_MASK = MacAddress.fromString("0:0:0:ff:ff:ff").mAddr;
84 private static final MacAddress BASE_GOOGLE_MAC = MacAddress.fromString("da:a1:19:0:0:0");
85
86 // Internal representation of the MAC address as a single 8 byte long.
Hugo Benichiac52e402017-11-09 00:22:25 +090087 // The encoding scheme sets the two most significant bytes to 0. The 6 bytes of the
Hugo Benichi84bb7fc2017-11-16 14:40:16 +090088 // MAC address are encoded in the 6 least significant bytes of the long, where the first
Hugo Benichiac52e402017-11-09 00:22:25 +090089 // byte of the array is mapped to the 3rd highest logical byte of the long, the second
90 // byte of the array is mapped to the 4th highest logical byte of the long, and so on.
91 private final long mAddr;
92
93 private MacAddress(long addr) {
Hugo Benichi84bb7fc2017-11-16 14:40:16 +090094 mAddr = (VALID_LONG_MASK & addr);
Hugo Benichiac52e402017-11-09 00:22:25 +090095 }
96
Hugo Benichi84bb7fc2017-11-16 14:40:16 +090097 /**
98 * Returns the type of this address.
99 *
100 * @return the int constant representing the MAC address type of this MacAddress.
101 */
Hugo Benichi48872c62018-01-12 09:46:29 +0900102 public @MacAddressType int getAddressType() {
Hugo Benichiac52e402017-11-09 00:22:25 +0900103 if (equals(BROADCAST_ADDRESS)) {
Hugo Benichi84bb7fc2017-11-16 14:40:16 +0900104 return TYPE_BROADCAST;
Hugo Benichiac52e402017-11-09 00:22:25 +0900105 }
106 if (isMulticastAddress()) {
Hugo Benichi84bb7fc2017-11-16 14:40:16 +0900107 return TYPE_MULTICAST;
Hugo Benichiac52e402017-11-09 00:22:25 +0900108 }
Hugo Benichi84bb7fc2017-11-16 14:40:16 +0900109 return TYPE_UNICAST;
Hugo Benichiac52e402017-11-09 00:22:25 +0900110 }
111
Hugo Benichi84bb7fc2017-11-16 14:40:16 +0900112 /**
113 * @return true if this MacAddress is a multicast address.
114 * @hide
115 */
Hugo Benichiac52e402017-11-09 00:22:25 +0900116 public boolean isMulticastAddress() {
117 return (mAddr & MULTICAST_MASK) != 0;
118 }
119
Hugo Benichi84bb7fc2017-11-16 14:40:16 +0900120 /**
121 * @return true if this MacAddress is a locally assigned address.
122 */
Hugo Benichiac52e402017-11-09 00:22:25 +0900123 public boolean isLocallyAssigned() {
124 return (mAddr & LOCALLY_ASSIGNED_MASK) != 0;
125 }
126
Hugo Benichi84bb7fc2017-11-16 14:40:16 +0900127 /**
128 * @return a byte array representation of this MacAddress.
129 */
Hugo Benichi48872c62018-01-12 09:46:29 +0900130 public @NonNull byte[] toByteArray() {
Hugo Benichiac52e402017-11-09 00:22:25 +0900131 return byteAddrFromLongAddr(mAddr);
132 }
133
134 @Override
Hugo Benichi48872c62018-01-12 09:46:29 +0900135 public @NonNull String toString() {
Hugo Benichi84bb7fc2017-11-16 14:40:16 +0900136 return stringAddrFromLongAddr(mAddr);
137 }
138
139 /**
Hugo Benichia0ecf382017-12-15 10:07:35 +0900140 * @return a String representation of the OUI part of this MacAddress made of 3 hexadecimal
141 * numbers in [0,ff] joined by ':' characters.
Hugo Benichi84bb7fc2017-11-16 14:40:16 +0900142 */
Hugo Benichi48872c62018-01-12 09:46:29 +0900143 public @NonNull String toOuiString() {
Hugo Benichia0ecf382017-12-15 10:07:35 +0900144 return String.format(
145 "%02x:%02x:%02x", (mAddr >> 40) & 0xff, (mAddr >> 32) & 0xff, (mAddr >> 24) & 0xff);
Hugo Benichiac52e402017-11-09 00:22:25 +0900146 }
147
148 @Override
149 public int hashCode() {
150 return (int) ((mAddr >> 32) ^ mAddr);
151 }
152
153 @Override
154 public boolean equals(Object o) {
155 return (o instanceof MacAddress) && ((MacAddress) o).mAddr == mAddr;
156 }
157
158 @Override
159 public void writeToParcel(Parcel out, int flags) {
160 out.writeLong(mAddr);
161 }
162
163 @Override
164 public int describeContents() {
165 return 0;
166 }
167
168 public static final Parcelable.Creator<MacAddress> CREATOR =
169 new Parcelable.Creator<MacAddress>() {
170 public MacAddress createFromParcel(Parcel in) {
171 return new MacAddress(in.readLong());
172 }
173
174 public MacAddress[] newArray(int size) {
175 return new MacAddress[size];
176 }
177 };
178
Hugo Benichi84bb7fc2017-11-16 14:40:16 +0900179 /**
180 * Returns true if the given byte array is an valid MAC address.
181 * A valid byte array representation for a MacAddress is a non-null array of length 6.
182 *
183 * @param addr a byte array.
184 * @return true if the given byte array is not null and has the length of a MAC address.
185 *
186 * @hide
187 */
Hugo Benichi59c8e422017-10-12 21:33:40 +0900188 public static boolean isMacAddress(byte[] addr) {
189 return addr != null && addr.length == ETHER_ADDR_LEN;
190 }
191
192 /**
Hugo Benichi84bb7fc2017-11-16 14:40:16 +0900193 * Returns the MAC address type of the MAC address represented by the given byte array,
194 * or null if the given byte array does not represent a MAC address.
195 * A valid byte array representation for a MacAddress is a non-null array of length 6.
196 *
197 * @param addr a byte array representing a MAC address.
198 * @return the int constant representing the MAC address type of the MAC address represented
199 * by the given byte array, or type UNKNOWN if the byte array is not a valid MAC address.
200 *
201 * @hide
Hugo Benichiac52e402017-11-09 00:22:25 +0900202 */
Hugo Benichi84bb7fc2017-11-16 14:40:16 +0900203 public static int macAddressType(byte[] addr) {
Hugo Benichi59c8e422017-10-12 21:33:40 +0900204 if (!isMacAddress(addr)) {
Hugo Benichi84bb7fc2017-11-16 14:40:16 +0900205 return TYPE_UNKNOWN;
Hugo Benichi59c8e422017-10-12 21:33:40 +0900206 }
Hugo Benichi48872c62018-01-12 09:46:29 +0900207 return MacAddress.fromBytes(addr).getAddressType();
Hugo Benichiac52e402017-11-09 00:22:25 +0900208 }
209
Hugo Benichi84bb7fc2017-11-16 14:40:16 +0900210 /**
211 * Converts a String representation of a MAC address to a byte array representation.
212 * A valid String representation for a MacAddress is a series of 6 values in the
213 * range [0,ff] printed in hexadecimal and joined by ':' characters.
214 *
215 * @param addr a String representation of a MAC address.
216 * @return the byte representation of the MAC address.
217 * @throws IllegalArgumentException if the given String is not a valid representation.
218 *
219 * @hide
220 */
Hugo Benichi48872c62018-01-12 09:46:29 +0900221 public static @NonNull byte[] byteAddrFromStringAddr(String addr) {
Hugo Benichi84bb7fc2017-11-16 14:40:16 +0900222 Preconditions.checkNotNull(addr);
Hugo Benichiac52e402017-11-09 00:22:25 +0900223 String[] parts = addr.split(":");
224 if (parts.length != ETHER_ADDR_LEN) {
225 throw new IllegalArgumentException(addr + " was not a valid MAC address");
Hugo Benichi59c8e422017-10-12 21:33:40 +0900226 }
Hugo Benichiac52e402017-11-09 00:22:25 +0900227 byte[] bytes = new byte[ETHER_ADDR_LEN];
228 for (int i = 0; i < ETHER_ADDR_LEN; i++) {
229 int x = Integer.valueOf(parts[i], 16);
230 if (x < 0 || 0xff < x) {
231 throw new IllegalArgumentException(addr + "was not a valid MAC address");
232 }
233 bytes[i] = (byte) x;
234 }
235 return bytes;
236 }
237
Hugo Benichi84bb7fc2017-11-16 14:40:16 +0900238 /**
239 * Converts a byte array representation of a MAC address to a String representation made
240 * of 6 hexadecimal numbers in [0,ff] joined by ':' characters.
241 * A valid byte array representation for a MacAddress is a non-null array of length 6.
242 *
243 * @param addr a byte array representation of a MAC address.
244 * @return the String representation of the MAC address.
245 * @throws IllegalArgumentException if the given byte array is not a valid representation.
246 *
247 * @hide
248 */
Hugo Benichi48872c62018-01-12 09:46:29 +0900249 public static @NonNull String stringAddrFromByteAddr(byte[] addr) {
Hugo Benichiac52e402017-11-09 00:22:25 +0900250 if (!isMacAddress(addr)) {
251 return null;
252 }
Hugo Benichi84bb7fc2017-11-16 14:40:16 +0900253 return String.format("%02x:%02x:%02x:%02x:%02x:%02x",
254 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
Hugo Benichiac52e402017-11-09 00:22:25 +0900255 }
256
Hugo Benichi84bb7fc2017-11-16 14:40:16 +0900257 private static byte[] byteAddrFromLongAddr(long addr) {
Hugo Benichiac52e402017-11-09 00:22:25 +0900258 byte[] bytes = new byte[ETHER_ADDR_LEN];
259 int index = ETHER_ADDR_LEN;
260 while (index-- > 0) {
261 bytes[index] = (byte) addr;
262 addr = addr >> 8;
263 }
264 return bytes;
265 }
266
Hugo Benichi84bb7fc2017-11-16 14:40:16 +0900267 private static long longAddrFromByteAddr(byte[] addr) {
268 Preconditions.checkNotNull(addr);
Hugo Benichiac52e402017-11-09 00:22:25 +0900269 if (!isMacAddress(addr)) {
270 throw new IllegalArgumentException(
271 Arrays.toString(addr) + " was not a valid MAC address");
272 }
273 long longAddr = 0;
274 for (byte b : addr) {
275 longAddr = (longAddr << 8) + BitUtils.uint8(b);
276 }
277 return longAddr;
278 }
279
Hugo Benichi84bb7fc2017-11-16 14:40:16 +0900280 // Internal conversion function equivalent to longAddrFromByteAddr(byteAddrFromStringAddr(addr))
281 // that avoids the allocation of an intermediary byte[].
282 private static long longAddrFromStringAddr(String addr) {
283 Preconditions.checkNotNull(addr);
Hugo Benichiac52e402017-11-09 00:22:25 +0900284 String[] parts = addr.split(":");
285 if (parts.length != ETHER_ADDR_LEN) {
286 throw new IllegalArgumentException(addr + " was not a valid MAC address");
287 }
288 long longAddr = 0;
Hugo Benichid2c5b192017-12-05 13:14:08 +0900289 for (int i = 0; i < parts.length; i++) {
290 int x = Integer.valueOf(parts[i], 16);
Hugo Benichiac52e402017-11-09 00:22:25 +0900291 if (x < 0 || 0xff < x) {
292 throw new IllegalArgumentException(addr + "was not a valid MAC address");
293 }
294 longAddr = x + (longAddr << 8);
295 }
296 return longAddr;
297 }
298
Hugo Benichi84bb7fc2017-11-16 14:40:16 +0900299 // Internal conversion function equivalent to stringAddrFromByteAddr(byteAddrFromLongAddr(addr))
300 // that avoids the allocation of an intermediary byte[].
Hugo Benichi48872c62018-01-12 09:46:29 +0900301 private static @NonNull String stringAddrFromLongAddr(long addr) {
Hugo Benichi84bb7fc2017-11-16 14:40:16 +0900302 return String.format("%02x:%02x:%02x:%02x:%02x:%02x",
303 (addr >> 40) & 0xff,
304 (addr >> 32) & 0xff,
305 (addr >> 24) & 0xff,
306 (addr >> 16) & 0xff,
307 (addr >> 8) & 0xff,
308 addr & 0xff);
Hugo Benichiac52e402017-11-09 00:22:25 +0900309 }
310
311 /**
Hugo Benichi84bb7fc2017-11-16 14:40:16 +0900312 * Creates a MacAddress from the given String representation. A valid String representation
313 * for a MacAddress is a series of 6 values in the range [0,ff] printed in hexadecimal
314 * and joined by ':' characters.
315 *
316 * @param addr a String representation of a MAC address.
317 * @return the MacAddress corresponding to the given String representation.
318 * @throws IllegalArgumentException if the given String is not a valid representation.
Hugo Benichiac52e402017-11-09 00:22:25 +0900319 */
Hugo Benichi48872c62018-01-12 09:46:29 +0900320 public static @NonNull MacAddress fromString(@NonNull String addr) {
Hugo Benichi84bb7fc2017-11-16 14:40:16 +0900321 return new MacAddress(longAddrFromStringAddr(addr));
Hugo Benichiac52e402017-11-09 00:22:25 +0900322 }
323
324 /**
Hugo Benichi84bb7fc2017-11-16 14:40:16 +0900325 * Creates a MacAddress from the given byte array representation.
326 * A valid byte array representation for a MacAddress is a non-null array of length 6.
327 *
328 * @param addr a byte array representation of a MAC address.
329 * @return the MacAddress corresponding to the given byte array representation.
330 * @throws IllegalArgumentException if the given byte array is not a valid representation.
Hugo Benichiac52e402017-11-09 00:22:25 +0900331 */
Hugo Benichi48872c62018-01-12 09:46:29 +0900332 public static @NonNull MacAddress fromBytes(@NonNull byte[] addr) {
Hugo Benichi84bb7fc2017-11-16 14:40:16 +0900333 return new MacAddress(longAddrFromByteAddr(addr));
334 }
335
336 /**
337 * Returns a generated MAC address whose 24 least significant bits constituting the
Jong Wook Kimf0a55cc2018-01-31 19:03:19 -0800338 * NIC part of the address are randomly selected and has Google OUI base.
Hugo Benichi84bb7fc2017-11-16 14:40:16 +0900339 *
340 * The locally assigned bit is always set to 1. The multicast bit is always set to 0.
341 *
Jong Wook Kimf0a55cc2018-01-31 19:03:19 -0800342 * @return a random locally assigned, unicast MacAddress with Google OUI.
343 *
344 * @hide
345 */
346 public static @NonNull MacAddress createRandomUnicastAddressWithGoogleBase() {
347 return createRandomUnicastAddress(BASE_GOOGLE_MAC, new SecureRandom());
348 }
349
350 /**
351 * Returns a generated MAC address whose 46 bits, excluding the locally assigned bit and the
352 * unicast bit, are randomly selected.
353 *
354 * The locally assigned bit is always set to 1. The multicast bit is always set to 0.
355 *
356 * @return a random locally assigned, unicast MacAddress.
Hugo Benichi84bb7fc2017-11-16 14:40:16 +0900357 *
358 * @hide
359 */
Hugo Benichi48872c62018-01-12 09:46:29 +0900360 public static @NonNull MacAddress createRandomUnicastAddress() {
Jong Wook Kimf0a55cc2018-01-31 19:03:19 -0800361 SecureRandom r = new SecureRandom();
362 long addr = r.nextLong() & VALID_LONG_MASK;
363 addr |= LOCALLY_ASSIGNED_MASK;
364 addr &= ~MULTICAST_MASK;
365 return new MacAddress(addr);
Hugo Benichi84bb7fc2017-11-16 14:40:16 +0900366 }
367
368 /**
369 * Returns a randomly generated MAC address using the given Random object and the same
370 * OUI values as the given MacAddress.
371 *
372 * The locally assigned bit is always set to 1. The multicast bit is always set to 0.
373 *
374 * @param base a base MacAddress whose OUI is used for generating the random address.
375 * @param r a standard Java Random object used for generating the random address.
376 * @return a random locally assigned MacAddress.
377 *
378 * @hide
379 */
Hugo Benichi48872c62018-01-12 09:46:29 +0900380 public static @NonNull MacAddress createRandomUnicastAddress(MacAddress base, Random r) {
Hugo Benichi84bb7fc2017-11-16 14:40:16 +0900381 long addr = (base.mAddr & OUI_MASK) | (NIC_MASK & r.nextLong());
Jong Wook Kimf0a55cc2018-01-31 19:03:19 -0800382 addr |= LOCALLY_ASSIGNED_MASK;
383 addr &= ~MULTICAST_MASK;
Hugo Benichi84bb7fc2017-11-16 14:40:16 +0900384 return new MacAddress(addr);
Hugo Benichiac52e402017-11-09 00:22:25 +0900385 }
386
387 // Convenience function for working around the lack of byte literals.
388 private static byte[] addr(int... in) {
389 if (in.length != ETHER_ADDR_LEN) {
390 throw new IllegalArgumentException(Arrays.toString(in)
391 + " was not an array with length equal to " + ETHER_ADDR_LEN);
392 }
393 byte[] out = new byte[ETHER_ADDR_LEN];
394 for (int i = 0; i < ETHER_ADDR_LEN; i++) {
395 out[i] = (byte) in[i];
396 }
397 return out;
Hugo Benichi59c8e422017-10-12 21:33:40 +0900398 }
Etan Cohena8923c52018-11-02 15:07:20 -0700399
400 /**
401 * Create a link-local Inet6Address from the MAC address. The EUI-48 MAC address is converted
402 * to an EUI-64 MAC address per RFC 4291. The resulting EUI-64 is used to construct a link-local
403 * IPv6 address per RFC 4862.
404 *
405 * @return A link-local Inet6Address constructed from the MAC address.
406 * @hide
407 */
408 public @Nullable Inet6Address getLinkLocalIpv6FromEui48Mac() {
409 byte[] macEui48Bytes = toByteArray();
410 byte[] addr = new byte[16];
411
412 addr[0] = (byte) 0xfe;
413 addr[1] = (byte) 0x80;
414 addr[8] = (byte) (macEui48Bytes[0] ^ (byte) 0x02); // flip the link-local bit
415 addr[9] = macEui48Bytes[1];
416 addr[10] = macEui48Bytes[2];
417 addr[11] = (byte) 0xff;
418 addr[12] = (byte) 0xfe;
419 addr[13] = macEui48Bytes[3];
420 addr[14] = macEui48Bytes[4];
421 addr[15] = macEui48Bytes[5];
422
423 try {
424 return Inet6Address.getByAddress(null, addr, 0);
425 } catch (UnknownHostException e) {
426 return null;
427 }
428 }
Hugo Benichi59c8e422017-10-12 21:33:40 +0900429}