• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2019 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.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.net.MacAddress;
22 
23 import java.security.SecureRandom;
24 import java.util.Arrays;
25 import java.util.Objects;
26 import java.util.Random;
27 
28 /**
29  * Collection of MAC address utilities.
30  * @hide
31  */
32 public final class MacAddressUtils {
33 
34     private static final long VALID_LONG_MASK = (1L << 48) - 1;
35     private static final long LOCALLY_ASSIGNED_MASK = longAddrFromByteAddr(
36             MacAddress.fromString("2:0:0:0:0:0").toByteArray());
37     private static final long MULTICAST_MASK = longAddrFromByteAddr(
38             MacAddress.fromString("1:0:0:0:0:0").toByteArray());
39     private static final long OUI_MASK = longAddrFromByteAddr(
40             MacAddress.fromString("ff:ff:ff:0:0:0").toByteArray());
41     private static final long NIC_MASK = longAddrFromByteAddr(
42             MacAddress.fromString("0:0:0:ff:ff:ff").toByteArray());
43     // Matches WifiInfo.DEFAULT_MAC_ADDRESS
44     private static final String DEFAULT_MAC_ADDRESS = "02:00:00:00:00:00";
45     private static final int ETHER_ADDR_LEN = 6;
46 
47     /**
48      * @return true if this MacAddress is a multicast address.
49      */
isMulticastAddress(@onNull MacAddress address)50     public static boolean isMulticastAddress(@NonNull MacAddress address) {
51         return (longAddrFromByteAddr(address.toByteArray()) & MULTICAST_MASK) != 0;
52     }
53 
54     /**
55      * Returns a generated MAC address whose 46 bits, excluding the locally assigned bit and the
56      * unicast bit, are randomly selected.
57      *
58      * The locally assigned bit is always set to 1. The multicast bit is always set to 0.
59      *
60      * @return a random locally assigned, unicast MacAddress.
61      */
createRandomUnicastAddress()62     public static @NonNull MacAddress createRandomUnicastAddress() {
63         return createRandomUnicastAddress(null, new SecureRandom());
64     }
65 
66     /**
67      * Returns a randomly generated MAC address using the given Random object and the same
68      * OUI values as the given MacAddress.
69      *
70      * The locally assigned bit is always set to 1. The multicast bit is always set to 0.
71      *
72      * @param base a base MacAddress whose OUI is used for generating the random address.
73      *             If base == null then the OUI will also be randomized.
74      * @param r a standard Java Random object used for generating the random address.
75      * @return a random locally assigned MacAddress.
76      */
createRandomUnicastAddress(@ullable MacAddress base, @NonNull Random r)77     public static @NonNull MacAddress createRandomUnicastAddress(@Nullable MacAddress base,
78             @NonNull Random r) {
79         long addr;
80 
81         if (base == null) {
82             addr = r.nextLong() & VALID_LONG_MASK;
83         } else {
84             addr = (longAddrFromByteAddr(base.toByteArray()) & OUI_MASK)
85                     | (NIC_MASK & r.nextLong());
86         }
87         addr |= LOCALLY_ASSIGNED_MASK;
88         addr &= ~MULTICAST_MASK;
89         MacAddress mac = MacAddress.fromBytes(byteAddrFromLongAddr(addr));
90         if (mac.equals(DEFAULT_MAC_ADDRESS)) {
91             return createRandomUnicastAddress(base, r);
92         }
93         return mac;
94     }
95 
96     /**
97      * Convert a byte address to long address.
98      */
longAddrFromByteAddr(byte[] addr)99     public static long longAddrFromByteAddr(byte[] addr) {
100         Objects.requireNonNull(addr);
101         if (!isMacAddress(addr)) {
102             throw new IllegalArgumentException(
103                     Arrays.toString(addr) + " was not a valid MAC address");
104         }
105         long longAddr = 0;
106         for (byte b : addr) {
107             final int uint8Byte = b & 0xff;
108             longAddr = (longAddr << 8) + uint8Byte;
109         }
110         return longAddr;
111     }
112 
113     /**
114      * Convert a long address to byte address.
115      */
byteAddrFromLongAddr(long addr)116     public static byte[] byteAddrFromLongAddr(long addr) {
117         byte[] bytes = new byte[ETHER_ADDR_LEN];
118         int index = ETHER_ADDR_LEN;
119         while (index-- > 0) {
120             bytes[index] = (byte) addr;
121             addr = addr >> 8;
122         }
123         return bytes;
124     }
125 
126     /**
127      * Returns true if the given byte array is a valid MAC address.
128      * A valid byte array representation for a MacAddress is a non-null array of length 6.
129      *
130      * @param addr a byte array.
131      * @return true if the given byte array is not null and has the length of a MAC address.
132      *
133      */
isMacAddress(byte[] addr)134     public static boolean isMacAddress(byte[] addr) {
135         return addr != null && addr.length == ETHER_ADDR_LEN;
136     }
137 }
138