Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 15 | */ |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 16 | package org.onlab.packet; |
| 17 | |
Yuta HIGUCHI | bfc2e92 | 2017-06-07 21:46:05 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableSet; |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 19 | import java.util.Arrays; |
Charles Chan | 928ff8b | 2017-05-04 12:22:54 -0700 | [diff] [blame] | 20 | import java.util.Set; |
Dominika Molenda | 52cc3a9 | 2017-06-19 17:08:35 +0200 | [diff] [blame] | 21 | import java.util.regex.Matcher; |
| 22 | import java.util.regex.Pattern; |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 23 | |
| 24 | /** |
| 25 | * The class representing MAC address. |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 26 | */ |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 27 | public class MacAddress { |
Jonathan Hart | 8f6f1ea | 2014-10-03 16:05:19 -0700 | [diff] [blame] | 28 | |
Dominika Molenda | 52cc3a9 | 2017-06-19 17:08:35 +0200 | [diff] [blame] | 29 | private static final Pattern MAC_PATTERN = Pattern.compile("^([0-9A-Fa-f]{2}[:]){5}([0-9A-Fa-f]{2})$"); |
Charles Chan | ba13871 | 2016-04-06 22:19:13 -0700 | [diff] [blame] | 30 | /** |
Charles Chan | 928ff8b | 2017-05-04 12:22:54 -0700 | [diff] [blame] | 31 | * First MAC address in ONOS OUI range. |
| 32 | */ |
| 33 | public static final MacAddress ONOS = valueOf("a4:23:05:00:00:00"); |
| 34 | /** |
Charles Chan | 79cddb1 | 2017-05-12 18:07:16 -0700 | [diff] [blame] | 35 | * 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 | /** |
jaegonkim | 47b1b4a | 2017-12-04 14:08:26 +0900 | [diff] [blame] | 39 | * ONOS LLDP MAC address with slow protocol destination address. |
Charles Chan | 928ff8b | 2017-05-04 12:22:54 -0700 | [diff] [blame] | 40 | */ |
jaegonkim | 47b1b4a | 2017-12-04 14:08:26 +0900 | [diff] [blame] | 41 | public static final MacAddress ONOS_LLDP = valueOf("01:80:C2:00:00:0E"); |
Charles Chan | ba13871 | 2016-04-06 22:19:13 -0700 | [diff] [blame] | 42 | /** |
| 43 | * All-zero MAC address. |
| 44 | */ |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 45 | public static final MacAddress ZERO = valueOf("00:00:00:00:00:00"); |
Charles Chan | ba13871 | 2016-04-06 22:19:13 -0700 | [diff] [blame] | 46 | /** |
| 47 | * Broadcast MAC address. |
| 48 | */ |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 49 | public static final MacAddress BROADCAST = valueOf("ff:ff:ff:ff:ff:ff"); |
Charles Chan | ba13871 | 2016-04-06 22:19:13 -0700 | [diff] [blame] | 50 | /** |
Charles Chan | 0865779 | 2018-08-23 22:08:02 -0700 | [diff] [blame] | 51 | * 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 Chan | ba13871 | 2016-04-06 22:19:13 -0700 | [diff] [blame] | 55 | * 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 Chan | 928ff8b | 2017-05-04 12:22:54 -0700 | [diff] [blame] | 62 | /** |
Julia Ferguson | 65428c3 | 2017-08-10 18:15:24 +0000 | [diff] [blame] | 63 | * 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 Chan | 928ff8b | 2017-05-04 12:22:54 -0700 | [diff] [blame] | 71 | * A set of LLDP MAC addresses. |
| 72 | */ |
Yuta HIGUCHI | bfc2e92 | 2017-06-07 21:46:05 -0700 | [diff] [blame] | 73 | public static final Set<MacAddress> LLDP = ImmutableSet.of( |
Charles Chan | 928ff8b | 2017-05-04 12:22:54 -0700 | [diff] [blame] | 74 | 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")); |
Thomas Vachuska | e1bcb0b | 2014-10-27 17:45:10 -0700 | [diff] [blame] | 77 | |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 78 | public static final int MAC_ADDRESS_LENGTH = 6; |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 79 | private byte[] address = new byte[MacAddress.MAC_ADDRESS_LENGTH]; |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 80 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 81 | public MacAddress(final byte[] address) { |
| 82 | this.address = Arrays.copyOf(address, MacAddress.MAC_ADDRESS_LENGTH); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Returns a MAC address instance representing the value of the specified |
| 87 | * {@code String}. |
| 88 | * |
Thomas Vachuska | e1bcb0b | 2014-10-27 17:45:10 -0700 | [diff] [blame] | 89 | * @param address the String representation of the MAC Address to be parsed. |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 90 | * @return a MAC Address instance representing the value of the specified |
Thomas Vachuska | e1bcb0b | 2014-10-27 17:45:10 -0700 | [diff] [blame] | 91 | * {@code String}. |
| 92 | * @throws IllegalArgumentException if the string cannot be parsed as a MAC address. |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 93 | */ |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 94 | public static MacAddress valueOf(final String address) { |
Dominika Molenda | 52cc3a9 | 2017-06-19 17:08:35 +0200 | [diff] [blame] | 95 | if (!isValid(address)) { |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 96 | throw new IllegalArgumentException( |
| 97 | "Specified MAC Address must contain 12 hex digits" |
| 98 | + " separated pairwise by :'s."); |
| 99 | } |
Dominika Molenda | 52cc3a9 | 2017-06-19 17:08:35 +0200 | [diff] [blame] | 100 | final String[] elements = address.split(":"); |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 101 | final byte[] addressInBytes = new byte[MacAddress.MAC_ADDRESS_LENGTH]; |
| 102 | for (int i = 0; i < MacAddress.MAC_ADDRESS_LENGTH; i++) { |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 103 | final String element = elements[i]; |
| 104 | addressInBytes[i] = (byte) Integer.parseInt(element, 16); |
| 105 | } |
| 106 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 107 | return new MacAddress(addressInBytes); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Returns a MAC address instance representing the specified {@code byte} |
| 112 | * array. |
| 113 | * |
Thomas Vachuska | e1bcb0b | 2014-10-27 17:45:10 -0700 | [diff] [blame] | 114 | * @param address the byte array to be parsed. |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 115 | * @return a MAC address instance representing the specified {@code byte} |
Thomas Vachuska | e1bcb0b | 2014-10-27 17:45:10 -0700 | [diff] [blame] | 116 | * array. |
| 117 | * @throws IllegalArgumentException if the byte array cannot be parsed as a MAC address. |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 118 | */ |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 119 | public static MacAddress valueOf(final byte[] address) { |
| 120 | if (address.length != MacAddress.MAC_ADDRESS_LENGTH) { |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 121 | throw new IllegalArgumentException("the length is not " |
Thomas Vachuska | e1bcb0b | 2014-10-27 17:45:10 -0700 | [diff] [blame] | 122 | + MacAddress.MAC_ADDRESS_LENGTH); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 125 | return new MacAddress(address); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Returns a MAC address instance representing the specified {@code long} |
| 130 | * value. The lower 48 bits of the long value are used to parse as a MAC |
| 131 | * address. |
| 132 | * |
Thomas Vachuska | e1bcb0b | 2014-10-27 17:45:10 -0700 | [diff] [blame] | 133 | * @param address the long value to be parsed. The lower 48 bits are used for a |
| 134 | * MAC address. |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 135 | * @return a MAC address instance representing the specified {@code long} |
Thomas Vachuska | e1bcb0b | 2014-10-27 17:45:10 -0700 | [diff] [blame] | 136 | * value. |
| 137 | * @throws IllegalArgumentException if the long value cannot be parsed as a MAC address. |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 138 | */ |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 139 | public static MacAddress valueOf(final long address) { |
Thomas Vachuska | e1bcb0b | 2014-10-27 17:45:10 -0700 | [diff] [blame] | 140 | final byte[] addressInBytes = new byte[]{ |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 141 | (byte) (address >> 40 & 0xff), (byte) (address >> 32 & 0xff), |
| 142 | (byte) (address >> 24 & 0xff), (byte) (address >> 16 & 0xff), |
Thomas Vachuska | e1bcb0b | 2014-10-27 17:45:10 -0700 | [diff] [blame] | 143 | (byte) (address >> 8 & 0xff), (byte) (address >> 0 & 0xff)}; |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 144 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 145 | return new MacAddress(addressInBytes); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Returns the length of the {@code MACAddress}. |
| 150 | * |
| 151 | * @return the length of the {@code MACAddress}. |
| 152 | */ |
| 153 | public int length() { |
| 154 | return this.address.length; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Returns the value of the {@code MACAddress} as a {@code byte} array. |
| 159 | * |
| 160 | * @return the numeric value represented by this object after conversion to |
Thomas Vachuska | e1bcb0b | 2014-10-27 17:45:10 -0700 | [diff] [blame] | 161 | * type {@code byte} array. |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 162 | */ |
| 163 | public byte[] toBytes() { |
| 164 | return Arrays.copyOf(this.address, this.address.length); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Returns the value of the {@code MACAddress} as a {@code long}. |
| 169 | * |
| 170 | * @return the numeric value represented by this object after conversion to |
Thomas Vachuska | e1bcb0b | 2014-10-27 17:45:10 -0700 | [diff] [blame] | 171 | * type {@code long}. |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 172 | */ |
| 173 | public long toLong() { |
| 174 | long mac = 0; |
| 175 | for (int i = 0; i < 6; i++) { |
| 176 | final long t = (this.address[i] & 0xffL) << (5 - i) * 8; |
| 177 | mac |= t; |
| 178 | } |
| 179 | return mac; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Returns {@code true} if the MAC address is the broadcast address. |
| 184 | * |
| 185 | * @return {@code true} if the MAC address is the broadcast address. |
| 186 | */ |
| 187 | public boolean isBroadcast() { |
| 188 | for (final byte b : this.address) { |
| 189 | if (b != -1) { |
| 190 | return false; |
| 191 | } |
| 192 | } |
| 193 | return true; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Returns {@code true} if the MAC address is the multicast address. |
| 198 | * |
| 199 | * @return {@code true} if the MAC address is the multicast address. |
| 200 | */ |
| 201 | public boolean isMulticast() { |
| 202 | if (this.isBroadcast()) { |
| 203 | return false; |
| 204 | } |
| 205 | return (this.address[0] & 0x01) != 0; |
| 206 | } |
| 207 | |
Thomas Vachuska | e1bcb0b | 2014-10-27 17:45:10 -0700 | [diff] [blame] | 208 | /** |
| 209 | * Returns true if this MAC address is link local. |
| 210 | * |
| 211 | * @return true if link local |
Charles Chan | 928ff8b | 2017-05-04 12:22:54 -0700 | [diff] [blame] | 212 | * @deprecated in Kingfisher release. Link local is not a correct description for |
| 213 | * this MAC address. Replaced with {@link #isLldp()} |
Thomas Vachuska | e1bcb0b | 2014-10-27 17:45:10 -0700 | [diff] [blame] | 214 | */ |
Charles Chan | 928ff8b | 2017-05-04 12:22:54 -0700 | [diff] [blame] | 215 | @Deprecated |
Thomas Vachuska | e1bcb0b | 2014-10-27 17:45:10 -0700 | [diff] [blame] | 216 | public boolean isLinkLocal() { |
Charles Chan | 928ff8b | 2017-05-04 12:22:54 -0700 | [diff] [blame] | 217 | return isLldp(); |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Returns true if this MAC address is used by link layer discovery protocol. |
| 222 | * |
| 223 | * @return true if this MAC is LLDP MAC. |
| 224 | */ |
| 225 | public boolean isLldp() { |
| 226 | return LLDP.contains(this); |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Returns true if the Organizationally Unique Identifier (OUI) of this MAC |
| 231 | * address matches ONOS OUI. |
| 232 | * |
| 233 | * @return true if the OUI of this MAC address matches ONOS OUI. |
| 234 | */ |
| 235 | public boolean isOnos() { |
| 236 | return Arrays.equals(this.oui(), ONOS.oui()); |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Returns the Organizationally Unique Identifier (OUI) of this MAC address. |
| 241 | * |
| 242 | * @return the OUI of this MAC address. |
| 243 | */ |
| 244 | public byte[] oui() { |
| 245 | return Arrays.copyOfRange(this.address, 0, 3); |
Thomas Vachuska | e1bcb0b | 2014-10-27 17:45:10 -0700 | [diff] [blame] | 246 | } |
| 247 | |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 248 | @Override |
| 249 | public boolean equals(final Object o) { |
| 250 | if (o == this) { |
| 251 | return true; |
| 252 | } |
| 253 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 254 | if (!(o instanceof MacAddress)) { |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 255 | return false; |
| 256 | } |
| 257 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 258 | final MacAddress other = (MacAddress) o; |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 259 | return Arrays.equals(this.address, other.address); |
| 260 | } |
| 261 | |
| 262 | @Override |
| 263 | public int hashCode() { |
Brian O'Connor | 8576c2a | 2014-11-19 16:49:26 -0800 | [diff] [blame] | 264 | return Long.hashCode(toLong()); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | @Override |
| 268 | public String toString() { |
Yuta HIGUCHI | ed1ef3a | 2017-05-15 18:10:01 -0700 | [diff] [blame] | 269 | final StringBuilder builder = new StringBuilder(2 * 6 + 5); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 270 | for (final byte b : this.address) { |
| 271 | if (builder.length() > 0) { |
Yuta HIGUCHI | ed1ef3a | 2017-05-15 18:10:01 -0700 | [diff] [blame] | 272 | builder.append(':'); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 273 | } |
| 274 | builder.append(String.format("%02X", b & 0xFF)); |
| 275 | } |
| 276 | return builder.toString(); |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * @return MAC address in string representation without colons (useful for |
Thomas Vachuska | e1bcb0b | 2014-10-27 17:45:10 -0700 | [diff] [blame] | 281 | * radix tree storage) |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 282 | */ |
| 283 | public String toStringNoColon() { |
| 284 | final StringBuilder builder = new StringBuilder(); |
| 285 | for (final byte b : this.address) { |
| 286 | builder.append(String.format("%02X", b & 0xFF)); |
| 287 | } |
| 288 | return builder.toString(); |
| 289 | } |
Dominika Molenda | 52cc3a9 | 2017-06-19 17:08:35 +0200 | [diff] [blame] | 290 | |
| 291 | private static boolean isValid(final String mac) { |
| 292 | Matcher matcher = MAC_PATTERN.matcher(mac); |
| 293 | return matcher.matches(); |
| 294 | } |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 295 | } |