blob: 2a89a5502b208c826379b32e1788408409cef24a [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
alshabibc4901cd2014-09-05 16:50:40 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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
alshabibc4901cd2014-09-05 16:50:40 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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.
Thomas Vachuska24c849c2014-10-27 09:53:05 -070015 */
alshabibc4901cd2014-09-05 16:50:40 -070016package org.onlab.packet;
17
Yuta HIGUCHIbfc2e922017-06-07 21:46:05 -070018import com.google.common.collect.ImmutableSet;
alshabibc4901cd2014-09-05 16:50:40 -070019import java.util.Arrays;
Charles Chan928ff8b2017-05-04 12:22:54 -070020import java.util.Set;
Dominika Molenda52cc3a92017-06-19 17:08:35 +020021import java.util.regex.Matcher;
22import java.util.regex.Pattern;
alshabibc4901cd2014-09-05 16:50:40 -070023
24/**
25 * The class representing MAC address.
alshabibc4901cd2014-09-05 16:50:40 -070026 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070027public class MacAddress {
Jonathan Hart8f6f1ea2014-10-03 16:05:19 -070028
Dominika Molenda52cc3a92017-06-19 17:08:35 +020029 private static final Pattern MAC_PATTERN = Pattern.compile("^([0-9A-Fa-f]{2}[:]){5}([0-9A-Fa-f]{2})$");
Charles Chanba138712016-04-06 22:19:13 -070030 /**
Charles Chan928ff8b2017-05-04 12:22:54 -070031 * First MAC address in ONOS OUI range.
32 */
33 public static final MacAddress ONOS = valueOf("a4:23:05:00:00:00");
34 /**
Charles Chan79cddb12017-05-12 18:07:16 -070035 * Dummy MAC address. We use the first MAC address in ONOS OUI range as the dummy MAC address.
36 */
37 public static final MacAddress NONE = ONOS;
38 /**
jaegonkim47b1b4a2017-12-04 14:08:26 +090039 * ONOS LLDP MAC address with slow protocol destination address.
Charles Chan928ff8b2017-05-04 12:22:54 -070040 */
jaegonkim47b1b4a2017-12-04 14:08:26 +090041 public static final MacAddress ONOS_LLDP = valueOf("01:80:C2:00:00:0E");
Charles Chanba138712016-04-06 22:19:13 -070042 /**
43 * All-zero MAC address.
44 */
tom545708e2014-10-09 17:10:02 -070045 public static final MacAddress ZERO = valueOf("00:00:00:00:00:00");
Charles Chanba138712016-04-06 22:19:13 -070046 /**
47 * Broadcast MAC address.
48 */
tom545708e2014-10-09 17:10:02 -070049 public static final MacAddress BROADCAST = valueOf("ff:ff:ff:ff:ff:ff");
Charles Chanba138712016-04-06 22:19:13 -070050 /**
Charles Chan384aea22018-08-23 22:08:02 -070051 * Mask that indicates exact match on the MacAddress.
52 */
53 public static final MacAddress EXACT_MASK = valueOf("ff:ff:ff:ff:ff:ff");
54 /**
Charles Chanba138712016-04-06 22:19:13 -070055 * IPv4 multicast MAC address.
56 */
57 public static final MacAddress IPV4_MULTICAST = valueOf("01:00:5e:00:00:00");
58 /**
59 * IPv4 multicast MAC mask.
60 */
61 public static final MacAddress IPV4_MULTICAST_MASK = valueOf("ff:ff:ff:80:00:00");
Charles Chan928ff8b2017-05-04 12:22:54 -070062 /**
Julia Ferguson65428c32017-08-10 18:15:24 +000063 * IPv6 multicast MAC address.
64 */
65 public static final MacAddress IPV6_MULTICAST = valueOf("33:33:00:00:00:00");
66 /**
67 * IPv6 multicast MAC mask.
68 */
69 public static final MacAddress IPV6_MULTICAST_MASK = valueOf("FF:FF:00:00:00:00");
70 /**
Charles Chan928ff8b2017-05-04 12:22:54 -070071 * A set of LLDP MAC addresses.
72 */
Yuta HIGUCHIbfc2e922017-06-07 21:46:05 -070073 public static final Set<MacAddress> LLDP = ImmutableSet.of(
Charles Chan928ff8b2017-05-04 12:22:54 -070074 MacAddress.valueOf("01:80:c2:00:00:00"),
75 MacAddress.valueOf("01:80:c2:00:00:03"),
76 MacAddress.valueOf("01:80:c2:00:00:0e"));
Charles Chan64c2dfd2018-07-24 11:58:08 -070077 /**
78 * LACP MAC address.
79 */
80 public static final MacAddress LACP = valueOf("01:80:C2:00:00:02");
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070081
alshabibc4901cd2014-09-05 16:50:40 -070082 public static final int MAC_ADDRESS_LENGTH = 6;
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070083 private byte[] address = new byte[MacAddress.MAC_ADDRESS_LENGTH];
alshabibc4901cd2014-09-05 16:50:40 -070084
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070085 public MacAddress(final byte[] address) {
86 this.address = Arrays.copyOf(address, MacAddress.MAC_ADDRESS_LENGTH);
alshabibc4901cd2014-09-05 16:50:40 -070087 }
88
89 /**
90 * Returns a MAC address instance representing the value of the specified
91 * {@code String}.
92 *
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070093 * @param address the String representation of the MAC Address to be parsed.
alshabibc4901cd2014-09-05 16:50:40 -070094 * @return a MAC Address instance representing the value of the specified
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070095 * {@code String}.
96 * @throws IllegalArgumentException if the string cannot be parsed as a MAC address.
alshabibc4901cd2014-09-05 16:50:40 -070097 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070098 public static MacAddress valueOf(final String address) {
Dominika Molenda52cc3a92017-06-19 17:08:35 +020099 if (!isValid(address)) {
alshabibc4901cd2014-09-05 16:50:40 -0700100 throw new IllegalArgumentException(
101 "Specified MAC Address must contain 12 hex digits"
102 + " separated pairwise by :'s.");
103 }
Dominika Molenda52cc3a92017-06-19 17:08:35 +0200104 final String[] elements = address.split(":");
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700105 final byte[] addressInBytes = new byte[MacAddress.MAC_ADDRESS_LENGTH];
106 for (int i = 0; i < MacAddress.MAC_ADDRESS_LENGTH; i++) {
alshabibc4901cd2014-09-05 16:50:40 -0700107 final String element = elements[i];
108 addressInBytes[i] = (byte) Integer.parseInt(element, 16);
109 }
110
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700111 return new MacAddress(addressInBytes);
alshabibc4901cd2014-09-05 16:50:40 -0700112 }
113
114 /**
115 * Returns a MAC address instance representing the specified {@code byte}
116 * array.
117 *
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700118 * @param address the byte array to be parsed.
alshabibc4901cd2014-09-05 16:50:40 -0700119 * @return a MAC address instance representing the specified {@code byte}
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700120 * array.
121 * @throws IllegalArgumentException if the byte array cannot be parsed as a MAC address.
alshabibc4901cd2014-09-05 16:50:40 -0700122 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700123 public static MacAddress valueOf(final byte[] address) {
124 if (address.length != MacAddress.MAC_ADDRESS_LENGTH) {
alshabibc4901cd2014-09-05 16:50:40 -0700125 throw new IllegalArgumentException("the length is not "
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700126 + MacAddress.MAC_ADDRESS_LENGTH);
alshabibc4901cd2014-09-05 16:50:40 -0700127 }
128
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700129 return new MacAddress(address);
alshabibc4901cd2014-09-05 16:50:40 -0700130 }
131
132 /**
133 * Returns a MAC address instance representing the specified {@code long}
134 * value. The lower 48 bits of the long value are used to parse as a MAC
135 * address.
136 *
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700137 * @param address the long value to be parsed. The lower 48 bits are used for a
138 * MAC address.
alshabibc4901cd2014-09-05 16:50:40 -0700139 * @return a MAC address instance representing the specified {@code long}
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700140 * value.
141 * @throws IllegalArgumentException if the long value cannot be parsed as a MAC address.
alshabibc4901cd2014-09-05 16:50:40 -0700142 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700143 public static MacAddress valueOf(final long address) {
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700144 final byte[] addressInBytes = new byte[]{
alshabibc4901cd2014-09-05 16:50:40 -0700145 (byte) (address >> 40 & 0xff), (byte) (address >> 32 & 0xff),
146 (byte) (address >> 24 & 0xff), (byte) (address >> 16 & 0xff),
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700147 (byte) (address >> 8 & 0xff), (byte) (address >> 0 & 0xff)};
alshabibc4901cd2014-09-05 16:50:40 -0700148
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700149 return new MacAddress(addressInBytes);
alshabibc4901cd2014-09-05 16:50:40 -0700150 }
151
152 /**
153 * Returns the length of the {@code MACAddress}.
154 *
155 * @return the length of the {@code MACAddress}.
156 */
157 public int length() {
158 return this.address.length;
159 }
160
161 /**
162 * Returns the value of the {@code MACAddress} as a {@code byte} array.
163 *
164 * @return the numeric value represented by this object after conversion to
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700165 * type {@code byte} array.
alshabibc4901cd2014-09-05 16:50:40 -0700166 */
167 public byte[] toBytes() {
168 return Arrays.copyOf(this.address, this.address.length);
169 }
170
171 /**
172 * Returns the value of the {@code MACAddress} as a {@code long}.
173 *
174 * @return the numeric value represented by this object after conversion to
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700175 * type {@code long}.
alshabibc4901cd2014-09-05 16:50:40 -0700176 */
177 public long toLong() {
178 long mac = 0;
179 for (int i = 0; i < 6; i++) {
180 final long t = (this.address[i] & 0xffL) << (5 - i) * 8;
181 mac |= t;
182 }
183 return mac;
184 }
185
186 /**
187 * Returns {@code true} if the MAC address is the broadcast address.
188 *
189 * @return {@code true} if the MAC address is the broadcast address.
190 */
191 public boolean isBroadcast() {
192 for (final byte b : this.address) {
193 if (b != -1) {
194 return false;
195 }
196 }
197 return true;
198 }
199
200 /**
201 * Returns {@code true} if the MAC address is the multicast address.
202 *
203 * @return {@code true} if the MAC address is the multicast address.
204 */
205 public boolean isMulticast() {
206 if (this.isBroadcast()) {
207 return false;
208 }
209 return (this.address[0] & 0x01) != 0;
210 }
211
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700212 /**
Charles Chan928ff8b2017-05-04 12:22:54 -0700213 * Returns true if this MAC address is used by link layer discovery protocol.
214 *
215 * @return true if this MAC is LLDP MAC.
216 */
217 public boolean isLldp() {
218 return LLDP.contains(this);
219 }
220
221 /**
222 * Returns true if the Organizationally Unique Identifier (OUI) of this MAC
223 * address matches ONOS OUI.
224 *
225 * @return true if the OUI of this MAC address matches ONOS OUI.
226 */
227 public boolean isOnos() {
228 return Arrays.equals(this.oui(), ONOS.oui());
229 }
230
231 /**
232 * Returns the Organizationally Unique Identifier (OUI) of this MAC address.
233 *
234 * @return the OUI of this MAC address.
235 */
236 public byte[] oui() {
237 return Arrays.copyOfRange(this.address, 0, 3);
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700238 }
239
alshabibc4901cd2014-09-05 16:50:40 -0700240 @Override
241 public boolean equals(final Object o) {
242 if (o == this) {
243 return true;
244 }
245
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700246 if (!(o instanceof MacAddress)) {
alshabibc4901cd2014-09-05 16:50:40 -0700247 return false;
248 }
249
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700250 final MacAddress other = (MacAddress) o;
alshabibc4901cd2014-09-05 16:50:40 -0700251 return Arrays.equals(this.address, other.address);
252 }
253
254 @Override
255 public int hashCode() {
Brian O'Connor8576c2a2014-11-19 16:49:26 -0800256 return Long.hashCode(toLong());
alshabibc4901cd2014-09-05 16:50:40 -0700257 }
258
259 @Override
260 public String toString() {
Yuta HIGUCHIed1ef3a2017-05-15 18:10:01 -0700261 final StringBuilder builder = new StringBuilder(2 * 6 + 5);
alshabibc4901cd2014-09-05 16:50:40 -0700262 for (final byte b : this.address) {
263 if (builder.length() > 0) {
Yuta HIGUCHIed1ef3a2017-05-15 18:10:01 -0700264 builder.append(':');
alshabibc4901cd2014-09-05 16:50:40 -0700265 }
266 builder.append(String.format("%02X", b & 0xFF));
267 }
268 return builder.toString();
269 }
270
271 /**
272 * @return MAC address in string representation without colons (useful for
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700273 * radix tree storage)
alshabibc4901cd2014-09-05 16:50:40 -0700274 */
275 public String toStringNoColon() {
276 final StringBuilder builder = new StringBuilder();
277 for (final byte b : this.address) {
278 builder.append(String.format("%02X", b & 0xFF));
279 }
280 return builder.toString();
281 }
Dominika Molenda52cc3a92017-06-19 17:08:35 +0200282
283 private static boolean isValid(final String mac) {
284 Matcher matcher = MAC_PATTERN.matcher(mac);
285 return matcher.matches();
286 }
pierventre53d76ed2020-09-15 12:44:52 +0200287
288 /**
289 * Checks if the Mac Address is inside a range defined by macAddr and mask.
290 *
291 * @param macAddr the mac address
292 * @param maskAddr the mask
293 * @return true if in range, false otherwise.
294 */
295 public boolean inRange(MacAddress macAddr, MacAddress maskAddr) {
296 byte[] min = macAddr.toBytes();
297 byte[] mask = maskAddr.toBytes();
298 boolean inRange = true;
299
300 int i = 0;
301
302 //if mask is 00 stop
303 while (inRange && i < mask.length && (mask[i] & 0xFF) != 0) {
304 int ibmac = this.address[i] & 0xFF;
305 int ibmin = min[i] & 0xFF;
306 int ibmask = mask[i] & 0xFF;
307 if (ibmask == 255) {
308 inRange = ibmac == ibmin;
309 } else if (ibmac < ibmin || ibmac >= ibmask) {
310 inRange = false;
311 break;
312 }
313 i++;
314 }
315
316 return inRange;
317 }
alshabibc4901cd2014-09-05 16:50:40 -0700318}