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 | */ |
| 16 | |
| 17 | |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 18 | |
| 19 | package org.onlab.packet; |
| 20 | |
Charles Chan | 119eeb5 | 2015-10-27 12:40:25 -0700 | [diff] [blame] | 21 | import org.onlab.packet.ndp.NeighborAdvertisement; |
| 22 | import org.onlab.packet.ndp.NeighborSolicitation; |
| 23 | import org.onlab.packet.ndp.Redirect; |
| 24 | import org.onlab.packet.ndp.RouterAdvertisement; |
| 25 | import org.onlab.packet.ndp.RouterSolicitation; |
| 26 | |
Yuta HIGUCHI | bfc2e92 | 2017-06-07 21:46:05 -0700 | [diff] [blame] | 27 | import com.google.common.collect.ImmutableMap; |
| 28 | |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 29 | import java.nio.ByteBuffer; |
| 30 | import java.util.Arrays; |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 31 | import java.util.Map; |
| 32 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 33 | import static com.google.common.base.Preconditions.checkNotNull; |
| 34 | import static org.onlab.packet.PacketUtils.checkHeaderLength; |
| 35 | import static org.onlab.packet.PacketUtils.checkInput; |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 36 | |
| 37 | /** |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 38 | * Ethernet Packet. |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 39 | */ |
| 40 | public class Ethernet extends BasePacket { |
| 41 | private static final String HEXES = "0123456789ABCDEF"; |
Pier Ventre | 48ca519 | 2016-11-28 16:52:24 -0800 | [diff] [blame] | 42 | private static final String HEX_PROTO = "0x%s"; |
Thomas Vachuska | 5dd52f7 | 2014-11-28 19:27:45 -0800 | [diff] [blame] | 43 | |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 44 | public static final short TYPE_ARP = EthType.EtherType.ARP.ethType().toShort(); |
| 45 | public static final short TYPE_RARP = EthType.EtherType.RARP.ethType().toShort(); |
| 46 | public static final short TYPE_IPV4 = EthType.EtherType.IPV4.ethType().toShort(); |
| 47 | public static final short TYPE_IPV6 = EthType.EtherType.IPV6.ethType().toShort(); |
| 48 | public static final short TYPE_LLDP = EthType.EtherType.LLDP.ethType().toShort(); |
| 49 | public static final short TYPE_VLAN = EthType.EtherType.VLAN.ethType().toShort(); |
Amit Ghosh | 764a1b4 | 2017-03-18 08:20:06 +0000 | [diff] [blame] | 50 | public static final short TYPE_QINQ = EthType.EtherType.QINQ.ethType().toShort(); |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 51 | public static final short TYPE_BSN = EthType.EtherType.BDDP.ethType().toShort(); |
| 52 | |
Jon Hall | 8c7b06a | 2017-02-22 13:37:33 -0800 | [diff] [blame] | 53 | public static final short MPLS_UNICAST = EthType.EtherType.MPLS_UNICAST.ethType().toShort(); |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 54 | public static final short MPLS_MULTICAST = EthType.EtherType.MPLS_MULTICAST.ethType().toShort(); |
alshabib | caf1ca2 | 2015-06-25 15:18:16 -0700 | [diff] [blame] | 55 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 56 | |
alshabib | caf1ca2 | 2015-06-25 15:18:16 -0700 | [diff] [blame] | 57 | public static final short VLAN_UNTAGGED = (short) 0xffff; |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 58 | |
| 59 | public static final short ETHERNET_HEADER_LENGTH = 14; // bytes |
| 60 | public static final short VLAN_HEADER_LENGTH = 4; // bytes |
| 61 | |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 62 | public static final short DATALAYER_ADDRESS_LENGTH = 6; // bytes |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 63 | |
Yuta HIGUCHI | bfc2e92 | 2017-06-07 21:46:05 -0700 | [diff] [blame] | 64 | private static final Map<Short, Deserializer<? extends IPacket>> ETHERTYPE_DESERIALIZER_MAP; |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 65 | |
| 66 | static { |
Yuta HIGUCHI | bfc2e92 | 2017-06-07 21:46:05 -0700 | [diff] [blame] | 67 | ImmutableMap.Builder<Short, Deserializer<? extends IPacket>> builder = |
| 68 | ImmutableMap.builder(); |
| 69 | |
alshabib | caf1ca2 | 2015-06-25 15:18:16 -0700 | [diff] [blame] | 70 | for (EthType.EtherType ethType : EthType.EtherType.values()) { |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 71 | if (ethType.deserializer() != null) { |
Yuta HIGUCHI | bfc2e92 | 2017-06-07 21:46:05 -0700 | [diff] [blame] | 72 | builder.put(ethType.ethType().toShort(), ethType.deserializer()); |
alshabib | caf1ca2 | 2015-06-25 15:18:16 -0700 | [diff] [blame] | 73 | } |
| 74 | } |
Yuta HIGUCHI | bfc2e92 | 2017-06-07 21:46:05 -0700 | [diff] [blame] | 75 | ETHERTYPE_DESERIALIZER_MAP = builder.build(); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 78 | protected MacAddress destinationMACAddress; |
| 79 | protected MacAddress sourceMACAddress; |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 80 | protected byte priorityCode; |
Amit Ghosh | 764a1b4 | 2017-03-18 08:20:06 +0000 | [diff] [blame] | 81 | protected byte qInQPriorityCode; |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 82 | protected short vlanID; |
Amit Ghosh | 764a1b4 | 2017-03-18 08:20:06 +0000 | [diff] [blame] | 83 | protected short qinqVID; |
| 84 | protected short qinqTPID; |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 85 | protected short etherType; |
| 86 | protected boolean pad = false; |
| 87 | |
| 88 | /** |
| 89 | * By default, set Ethernet to untagged. |
| 90 | */ |
| 91 | public Ethernet() { |
| 92 | super(); |
| 93 | this.vlanID = Ethernet.VLAN_UNTAGGED; |
Amit Ghosh | 764a1b4 | 2017-03-18 08:20:06 +0000 | [diff] [blame] | 94 | this.qinqVID = Ethernet.VLAN_UNTAGGED; |
| 95 | this.qinqTPID = TYPE_QINQ; |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Gets the destination MAC address. |
| 100 | * |
| 101 | * @return the destination MAC as a byte array |
| 102 | */ |
| 103 | public byte[] getDestinationMACAddress() { |
| 104 | return this.destinationMACAddress.toBytes(); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Gets the destination MAC address. |
| 109 | * |
| 110 | * @return the destination MAC |
| 111 | */ |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 112 | public MacAddress getDestinationMAC() { |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 113 | return this.destinationMACAddress; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Sets the destination MAC address. |
| 118 | * |
tom | 5f18cf3 | 2014-09-13 14:10:57 -0700 | [diff] [blame] | 119 | * @param destMac the destination MAC to set |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 120 | * @return the Ethernet frame |
| 121 | */ |
Yuta HIGUCHI | 3e848a8 | 2014-11-02 20:19:42 -0800 | [diff] [blame] | 122 | public Ethernet setDestinationMACAddress(final MacAddress destMac) { |
| 123 | this.destinationMACAddress = checkNotNull(destMac); |
| 124 | return this; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Sets the destination MAC address. |
| 129 | * |
| 130 | * @param destMac the destination MAC to set |
| 131 | * @return the Ethernet frame |
| 132 | */ |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 133 | public Ethernet setDestinationMACAddress(final byte[] destMac) { |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 134 | this.destinationMACAddress = MacAddress.valueOf(destMac); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 135 | return this; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Sets the destination MAC address. |
| 140 | * |
tom | 5f18cf3 | 2014-09-13 14:10:57 -0700 | [diff] [blame] | 141 | * @param destMac the destination MAC to set |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 142 | * @return the Ethernet frame |
| 143 | */ |
| 144 | public Ethernet setDestinationMACAddress(final String destMac) { |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 145 | this.destinationMACAddress = MacAddress.valueOf(destMac); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 146 | return this; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Gets the source MAC address. |
| 151 | * |
| 152 | * @return the source MACAddress as a byte array |
| 153 | */ |
| 154 | public byte[] getSourceMACAddress() { |
| 155 | return this.sourceMACAddress.toBytes(); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Gets the source MAC address. |
| 160 | * |
| 161 | * @return the source MACAddress |
| 162 | */ |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 163 | public MacAddress getSourceMAC() { |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 164 | return this.sourceMACAddress; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Sets the source MAC address. |
| 169 | * |
tom | 5f18cf3 | 2014-09-13 14:10:57 -0700 | [diff] [blame] | 170 | * @param sourceMac the source MAC to set |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 171 | * @return the Ethernet frame |
| 172 | */ |
Yuta HIGUCHI | 3e848a8 | 2014-11-02 20:19:42 -0800 | [diff] [blame] | 173 | public Ethernet setSourceMACAddress(final MacAddress sourceMac) { |
| 174 | this.sourceMACAddress = checkNotNull(sourceMac); |
| 175 | return this; |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Sets the source MAC address. |
| 180 | * |
| 181 | * @param sourceMac the source MAC to set |
| 182 | * @return the Ethernet frame |
| 183 | */ |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 184 | public Ethernet setSourceMACAddress(final byte[] sourceMac) { |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 185 | this.sourceMACAddress = MacAddress.valueOf(sourceMac); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 186 | return this; |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Sets the source MAC address. |
| 191 | * |
tom | 5f18cf3 | 2014-09-13 14:10:57 -0700 | [diff] [blame] | 192 | * @param sourceMac the source MAC to set |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 193 | * @return the Ethernet frame |
| 194 | */ |
| 195 | public Ethernet setSourceMACAddress(final String sourceMac) { |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 196 | this.sourceMACAddress = MacAddress.valueOf(sourceMac); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 197 | return this; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Gets the priority code. |
| 202 | * |
| 203 | * @return the priorityCode |
| 204 | */ |
| 205 | public byte getPriorityCode() { |
| 206 | return this.priorityCode; |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Sets the priority code. |
| 211 | * |
tom | 5f18cf3 | 2014-09-13 14:10:57 -0700 | [diff] [blame] | 212 | * @param priority the priorityCode to set |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 213 | * @return the Ethernet frame |
| 214 | */ |
| 215 | public Ethernet setPriorityCode(final byte priority) { |
| 216 | this.priorityCode = priority; |
| 217 | return this; |
| 218 | } |
| 219 | |
| 220 | /** |
Amit Ghosh | 764a1b4 | 2017-03-18 08:20:06 +0000 | [diff] [blame] | 221 | * Gets the QinQ priority code. |
| 222 | * |
| 223 | * @return the qInQPriorityCode |
| 224 | */ |
| 225 | public byte getQinQPriorityCode() { |
| 226 | return this.qInQPriorityCode; |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Sets the QinQ priority code. |
| 231 | * |
| 232 | * @param priority the priorityCode to set |
| 233 | * @return the Ethernet frame |
| 234 | */ |
| 235 | public Ethernet setQinQPriorityCode(final byte priority) { |
| 236 | this.qInQPriorityCode = priority; |
| 237 | return this; |
| 238 | } |
| 239 | |
| 240 | /** |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 241 | * Gets the VLAN ID. |
| 242 | * |
| 243 | * @return the vlanID |
| 244 | */ |
| 245 | public short getVlanID() { |
| 246 | return this.vlanID; |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Sets the VLAN ID. |
| 251 | * |
tom | 5f18cf3 | 2014-09-13 14:10:57 -0700 | [diff] [blame] | 252 | * @param vlan the vlanID to set |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 253 | * @return the Ethernet frame |
| 254 | */ |
| 255 | public Ethernet setVlanID(final short vlan) { |
| 256 | this.vlanID = vlan; |
| 257 | return this; |
| 258 | } |
| 259 | |
| 260 | /** |
Amit Ghosh | 764a1b4 | 2017-03-18 08:20:06 +0000 | [diff] [blame] | 261 | * Gets the QinQ VLAN ID. |
| 262 | * |
| 263 | * @return the QinQ vlanID |
| 264 | */ |
| 265 | public short getQinQVID() { |
| 266 | return this.qinqVID; |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Sets the QinQ VLAN ID. |
| 271 | * |
| 272 | * @param vlan the vlanID to set |
| 273 | * @return the Ethernet frame |
| 274 | */ |
| 275 | public Ethernet setQinQVID(final short vlan) { |
| 276 | this.qinqVID = vlan; |
| 277 | return this; |
| 278 | } |
| 279 | /** |
| 280 | * Gets the QinQ TPID. |
| 281 | * |
| 282 | * @return the QinQ TPID |
| 283 | */ |
| 284 | public short getQinQTPID() { |
| 285 | return this.qinqTPID; |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Sets the QinQ TPID. |
| 290 | * |
| 291 | * @param tpId the TPID to set |
| 292 | * @return the Ethernet frame |
| 293 | */ |
| 294 | public Ethernet setQinQTPID(final short tpId) { |
| 295 | if (tpId != TYPE_VLAN && tpId != TYPE_QINQ) { |
| 296 | return null; |
| 297 | } |
| 298 | this.qinqTPID = tpId; |
| 299 | return this; |
| 300 | } |
| 301 | /** |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 302 | * Gets the Ethernet type. |
| 303 | * |
| 304 | * @return the etherType |
| 305 | */ |
| 306 | public short getEtherType() { |
| 307 | return this.etherType; |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Sets the Ethernet type. |
| 312 | * |
tom | 5f18cf3 | 2014-09-13 14:10:57 -0700 | [diff] [blame] | 313 | * @param ethType the etherType to set |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 314 | * @return the Ethernet frame |
| 315 | */ |
| 316 | public Ethernet setEtherType(final short ethType) { |
| 317 | this.etherType = ethType; |
| 318 | return this; |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * @return True if the Ethernet frame is broadcast, false otherwise |
| 323 | */ |
| 324 | public boolean isBroadcast() { |
| 325 | assert this.destinationMACAddress.length() == 6; |
| 326 | return this.destinationMACAddress.isBroadcast(); |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * @return True is the Ethernet frame is multicast, False otherwise |
| 331 | */ |
| 332 | public boolean isMulticast() { |
| 333 | return this.destinationMACAddress.isMulticast(); |
| 334 | } |
| 335 | |
| 336 | /** |
| 337 | * Pad this packet to 60 bytes minimum, filling with zeros? |
| 338 | * |
| 339 | * @return the pad |
| 340 | */ |
| 341 | public boolean isPad() { |
| 342 | return this.pad; |
| 343 | } |
| 344 | |
| 345 | /** |
| 346 | * Pad this packet to 60 bytes minimum, filling with zeros? |
| 347 | * |
tom | 5f18cf3 | 2014-09-13 14:10:57 -0700 | [diff] [blame] | 348 | * @param pd |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 349 | * the pad to set |
Yuta HIGUCHI | 2281b3f | 2014-11-04 00:20:48 -0800 | [diff] [blame] | 350 | * @return this |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 351 | */ |
| 352 | public Ethernet setPad(final boolean pd) { |
| 353 | this.pad = pd; |
| 354 | return this; |
| 355 | } |
| 356 | |
| 357 | @Override |
| 358 | public byte[] serialize() { |
| 359 | byte[] payloadData = null; |
| 360 | if (this.payload != null) { |
| 361 | this.payload.setParent(this); |
| 362 | payloadData = this.payload.serialize(); |
| 363 | } |
| 364 | int length = 14 + (this.vlanID == Ethernet.VLAN_UNTAGGED ? 0 : 4) |
Amit Ghosh | 764a1b4 | 2017-03-18 08:20:06 +0000 | [diff] [blame] | 365 | + (this.qinqVID == Ethernet.VLAN_UNTAGGED ? 0 : 4) |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 366 | + (payloadData == null ? 0 : payloadData.length); |
| 367 | if (this.pad && length < 60) { |
| 368 | length = 60; |
| 369 | } |
| 370 | final byte[] data = new byte[length]; |
| 371 | final ByteBuffer bb = ByteBuffer.wrap(data); |
| 372 | bb.put(this.destinationMACAddress.toBytes()); |
| 373 | bb.put(this.sourceMACAddress.toBytes()); |
Amit Ghosh | 764a1b4 | 2017-03-18 08:20:06 +0000 | [diff] [blame] | 374 | if (this.qinqVID != Ethernet.VLAN_UNTAGGED) { |
| 375 | bb.putShort(this.qinqTPID); |
| 376 | bb.putShort((short) (this.qInQPriorityCode << 13 | this.qinqVID & 0x0fff)); |
| 377 | } |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 378 | if (this.vlanID != Ethernet.VLAN_UNTAGGED) { |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 379 | bb.putShort(TYPE_VLAN); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 380 | bb.putShort((short) (this.priorityCode << 13 | this.vlanID & 0x0fff)); |
| 381 | } |
| 382 | bb.putShort(this.etherType); |
| 383 | if (payloadData != null) { |
| 384 | bb.put(payloadData); |
| 385 | } |
| 386 | if (this.pad) { |
| 387 | Arrays.fill(data, bb.position(), data.length, (byte) 0x0); |
| 388 | } |
| 389 | return data; |
| 390 | } |
| 391 | |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 392 | /** |
| 393 | * Checks to see if a string is a valid MAC address. |
| 394 | * |
Yuta HIGUCHI | 2281b3f | 2014-11-04 00:20:48 -0800 | [diff] [blame] | 395 | * @param macAddress string to test if it is a valid MAC |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 396 | * @return True if macAddress is a valid MAC, False otherwise |
| 397 | */ |
| 398 | public static boolean isMACAddress(final String macAddress) { |
| 399 | final String[] macBytes = macAddress.split(":"); |
| 400 | if (macBytes.length != 6) { |
| 401 | return false; |
| 402 | } |
| 403 | for (int i = 0; i < 6; ++i) { |
| 404 | if (Ethernet.HEXES.indexOf(macBytes[i].toUpperCase().charAt(0)) == -1 |
| 405 | || Ethernet.HEXES.indexOf(macBytes[i].toUpperCase().charAt( |
| 406 | 1)) == -1) { |
| 407 | return false; |
| 408 | } |
| 409 | } |
| 410 | return true; |
| 411 | } |
| 412 | |
| 413 | /** |
| 414 | * Accepts a MAC address of the form 00:aa:11:bb:22:cc, case does not |
| 415 | * matter, and returns a corresponding byte[]. |
| 416 | * |
| 417 | * @param macAddress |
Yuta HIGUCHI | 2281b3f | 2014-11-04 00:20:48 -0800 | [diff] [blame] | 418 | * The MAC address to convert into a byte array |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 419 | * @return The macAddress as a byte array |
| 420 | */ |
| 421 | public static byte[] toMACAddress(final String macAddress) { |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 422 | return MacAddress.valueOf(macAddress).toBytes(); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | /** |
| 426 | * Accepts a MAC address and returns the corresponding long, where the MAC |
| 427 | * bytes are set on the lower order bytes of the long. |
| 428 | * |
Yuta HIGUCHI | 2281b3f | 2014-11-04 00:20:48 -0800 | [diff] [blame] | 429 | * @param macAddress MAC address as a byte array |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 430 | * @return a long containing the mac address bytes |
| 431 | */ |
| 432 | public static long toLong(final byte[] macAddress) { |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 433 | return MacAddress.valueOf(macAddress).toLong(); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | /** |
| 437 | * Converts a long MAC address to a byte array. |
| 438 | * |
Yuta HIGUCHI | 2281b3f | 2014-11-04 00:20:48 -0800 | [diff] [blame] | 439 | * @param macAddress MAC address set on the lower order bytes of the long |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 440 | * @return the bytes of the mac address |
| 441 | */ |
| 442 | public static byte[] toByteArray(final long macAddress) { |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 443 | return MacAddress.valueOf(macAddress).toBytes(); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | /* |
| 447 | * (non-Javadoc) |
| 448 | * |
| 449 | * @see java.lang.Object#hashCode() |
| 450 | */ |
| 451 | @Override |
| 452 | public int hashCode() { |
| 453 | final int prime = 7867; |
| 454 | int result = super.hashCode(); |
| 455 | result = prime * result + this.destinationMACAddress.hashCode(); |
| 456 | result = prime * result + this.etherType; |
Amit Ghosh | 764a1b4 | 2017-03-18 08:20:06 +0000 | [diff] [blame] | 457 | result = prime * result + this.qinqVID; |
| 458 | result = prime * result + this.qInQPriorityCode; |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 459 | result = prime * result + this.vlanID; |
| 460 | result = prime * result + this.priorityCode; |
| 461 | result = prime * result + (this.pad ? 1231 : 1237); |
| 462 | result = prime * result + this.sourceMACAddress.hashCode(); |
| 463 | return result; |
| 464 | } |
| 465 | |
| 466 | /* |
| 467 | * (non-Javadoc) |
| 468 | * |
| 469 | * @see java.lang.Object#equals(java.lang.Object) |
| 470 | */ |
| 471 | @Override |
| 472 | public boolean equals(final Object obj) { |
| 473 | if (this == obj) { |
| 474 | return true; |
| 475 | } |
| 476 | if (!super.equals(obj)) { |
| 477 | return false; |
| 478 | } |
| 479 | if (!(obj instanceof Ethernet)) { |
| 480 | return false; |
| 481 | } |
| 482 | final Ethernet other = (Ethernet) obj; |
| 483 | if (!this.destinationMACAddress.equals(other.destinationMACAddress)) { |
| 484 | return false; |
| 485 | } |
Amit Ghosh | 764a1b4 | 2017-03-18 08:20:06 +0000 | [diff] [blame] | 486 | if (this.qInQPriorityCode != other.qInQPriorityCode) { |
| 487 | return false; |
| 488 | } |
| 489 | if (this.qinqVID != other.qinqVID) { |
| 490 | return false; |
| 491 | } |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 492 | if (this.priorityCode != other.priorityCode) { |
| 493 | return false; |
| 494 | } |
| 495 | if (this.vlanID != other.vlanID) { |
| 496 | return false; |
| 497 | } |
| 498 | if (this.etherType != other.etherType) { |
| 499 | return false; |
| 500 | } |
| 501 | if (this.pad != other.pad) { |
| 502 | return false; |
| 503 | } |
| 504 | if (!this.sourceMACAddress.equals(other.sourceMACAddress)) { |
| 505 | return false; |
| 506 | } |
| 507 | return true; |
| 508 | } |
| 509 | |
| 510 | /* |
| 511 | * (non-Javadoc) |
| 512 | * |
| 513 | * @see java.lang.Object#toString(java.lang.Object) |
| 514 | */ |
| 515 | @Override |
| 516 | public String toString() { |
| 517 | |
Yuta HIGUCHI | 2dce08a | 2017-04-20 21:57:48 -0700 | [diff] [blame] | 518 | final StringBuilder sb = new StringBuilder("\n"); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 519 | |
| 520 | final IPacket pkt = this.getPayload(); |
| 521 | |
| 522 | if (pkt instanceof ARP) { |
| 523 | sb.append("arp"); |
| 524 | } else if (pkt instanceof LLDP) { |
| 525 | sb.append("lldp"); |
| 526 | } else if (pkt instanceof ICMP) { |
| 527 | sb.append("icmp"); |
| 528 | } else if (pkt instanceof IPv4) { |
| 529 | sb.append("ip"); |
| 530 | } else if (pkt instanceof DHCP) { |
| 531 | sb.append("dhcp"); |
| 532 | } else { |
Pier Ventre | 48ca519 | 2016-11-28 16:52:24 -0800 | [diff] [blame] | 533 | /* |
| 534 | * When we don't know the protocol, we print using |
| 535 | * the well known hex format instead of a decimal |
| 536 | * value. |
| 537 | */ |
| 538 | sb.append(String.format(HEX_PROTO, |
| 539 | Integer.toHexString(this.getEtherType() & 0xffff))); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 540 | } |
| 541 | |
Amit Ghosh | 764a1b4 | 2017-03-18 08:20:06 +0000 | [diff] [blame] | 542 | if (this.getQinQVID() != Ethernet.VLAN_UNTAGGED) { |
| 543 | sb.append("\ndl_qinqVlan: "); |
| 544 | sb.append(this.getQinQVID()); |
| 545 | sb.append("\ndl_qinqVlan_pcp: "); |
| 546 | sb.append(this.getQinQPriorityCode()); |
| 547 | } |
| 548 | |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 549 | sb.append("\ndl_vlan: "); |
| 550 | if (this.getVlanID() == Ethernet.VLAN_UNTAGGED) { |
| 551 | sb.append("untagged"); |
| 552 | } else { |
| 553 | sb.append(this.getVlanID()); |
| 554 | } |
| 555 | sb.append("\ndl_vlan_pcp: "); |
| 556 | sb.append(this.getPriorityCode()); |
| 557 | sb.append("\ndl_src: "); |
| 558 | sb.append(bytesToHex(this.getSourceMACAddress())); |
| 559 | sb.append("\ndl_dst: "); |
| 560 | sb.append(bytesToHex(this.getDestinationMACAddress())); |
| 561 | |
| 562 | if (pkt instanceof ARP) { |
| 563 | final ARP p = (ARP) pkt; |
| 564 | sb.append("\nnw_src: "); |
| 565 | sb.append(IPv4.fromIPv4Address(IPv4.toIPv4Address(p |
| 566 | .getSenderProtocolAddress()))); |
| 567 | sb.append("\nnw_dst: "); |
| 568 | sb.append(IPv4.fromIPv4Address(IPv4.toIPv4Address(p |
| 569 | .getTargetProtocolAddress()))); |
| 570 | } else if (pkt instanceof LLDP) { |
| 571 | sb.append("lldp packet"); |
| 572 | } else if (pkt instanceof ICMP) { |
| 573 | final ICMP icmp = (ICMP) pkt; |
| 574 | sb.append("\nicmp_type: "); |
| 575 | sb.append(icmp.getIcmpType()); |
| 576 | sb.append("\nicmp_code: "); |
| 577 | sb.append(icmp.getIcmpCode()); |
| 578 | } else if (pkt instanceof IPv4) { |
| 579 | final IPv4 p = (IPv4) pkt; |
| 580 | sb.append("\nnw_src: "); |
| 581 | sb.append(IPv4.fromIPv4Address(p.getSourceAddress())); |
| 582 | sb.append("\nnw_dst: "); |
| 583 | sb.append(IPv4.fromIPv4Address(p.getDestinationAddress())); |
| 584 | sb.append("\nnw_tos: "); |
| 585 | sb.append(p.getDiffServ()); |
| 586 | sb.append("\nnw_proto: "); |
| 587 | sb.append(p.getProtocol()); |
| 588 | |
Charles Chan | 119eeb5 | 2015-10-27 12:40:25 -0700 | [diff] [blame] | 589 | IPacket payload = pkt.getPayload(); |
| 590 | if (payload != null) { |
| 591 | if (payload instanceof TCP) { |
| 592 | sb.append("\ntp_src: "); |
| 593 | sb.append(((TCP) payload).getSourcePort()); |
| 594 | sb.append("\ntp_dst: "); |
| 595 | sb.append(((TCP) payload).getDestinationPort()); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 596 | |
Charles Chan | 119eeb5 | 2015-10-27 12:40:25 -0700 | [diff] [blame] | 597 | } else if (payload instanceof UDP) { |
| 598 | sb.append("\ntp_src: "); |
| 599 | sb.append(((UDP) payload).getSourcePort()); |
| 600 | sb.append("\ntp_dst: "); |
| 601 | sb.append(((UDP) payload).getDestinationPort()); |
| 602 | } else if (payload instanceof ICMP) { |
| 603 | final ICMP icmp = (ICMP) payload; |
| 604 | sb.append("\nicmp_type: "); |
| 605 | sb.append(icmp.getIcmpType()); |
| 606 | sb.append("\nicmp_code: "); |
| 607 | sb.append(icmp.getIcmpCode()); |
| 608 | } |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 609 | } |
Charles Chan | 119eeb5 | 2015-10-27 12:40:25 -0700 | [diff] [blame] | 610 | } else if (pkt instanceof IPv6) { |
| 611 | final IPv6 ipv6 = (IPv6) pkt; |
| 612 | sb.append("\nipv6_src: "); |
| 613 | sb.append(Ip6Address.valueOf(ipv6.getSourceAddress()).toString()); |
| 614 | sb.append("\nipv6_dst: "); |
| 615 | sb.append(Ip6Address.valueOf(ipv6.getDestinationAddress()).toString()); |
| 616 | sb.append("\nipv6_proto: "); |
| 617 | sb.append(ipv6.getNextHeader()); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 618 | |
Charles Chan | 119eeb5 | 2015-10-27 12:40:25 -0700 | [diff] [blame] | 619 | IPacket payload = pkt.getPayload(); |
| 620 | if (payload != null && payload instanceof ICMP6) { |
| 621 | final ICMP6 icmp6 = (ICMP6) payload; |
| 622 | sb.append("\nicmp6_type: "); |
| 623 | sb.append(icmp6.getIcmpType()); |
| 624 | sb.append("\nicmp6_code: "); |
| 625 | sb.append(icmp6.getIcmpCode()); |
| 626 | |
| 627 | payload = payload.getPayload(); |
| 628 | if (payload != null) { |
| 629 | if (payload instanceof NeighborSolicitation) { |
| 630 | final NeighborSolicitation ns = (NeighborSolicitation) payload; |
| 631 | sb.append("\nns_target_addr: "); |
| 632 | sb.append(Ip6Address.valueOf(ns.getTargetAddress()).toString()); |
| 633 | ns.getOptions().forEach(option -> { |
| 634 | sb.append("\noption_type: "); |
| 635 | sb.append(option.type()); |
| 636 | sb.append("\noption_data: "); |
| 637 | sb.append(bytesToHex(option.data())); |
| 638 | }); |
| 639 | } else if (payload instanceof NeighborAdvertisement) { |
| 640 | final NeighborAdvertisement na = (NeighborAdvertisement) payload; |
| 641 | sb.append("\nna_target_addr: "); |
| 642 | sb.append(Ip6Address.valueOf(na.getTargetAddress()).toString()); |
| 643 | sb.append("\nna_solicited_flag: "); |
| 644 | sb.append(na.getSolicitedFlag()); |
| 645 | sb.append("\nna_router_flag: "); |
| 646 | sb.append(na.getRouterFlag()); |
| 647 | sb.append("\nna_override_flag: "); |
| 648 | sb.append(na.getOverrideFlag()); |
| 649 | na.getOptions().forEach(option -> { |
| 650 | sb.append("\noption_type: "); |
| 651 | sb.append(option.type()); |
| 652 | sb.append("\noption_data: "); |
| 653 | sb.append(bytesToHex(option.data())); |
| 654 | }); |
| 655 | } else if (payload instanceof RouterSolicitation) { |
| 656 | final RouterSolicitation rs = (RouterSolicitation) payload; |
| 657 | sb.append("\nrs"); |
| 658 | rs.getOptions().forEach(option -> { |
| 659 | sb.append("\noption_type: "); |
| 660 | sb.append(option.type()); |
| 661 | sb.append("\noption_data: "); |
| 662 | sb.append(bytesToHex(option.data())); |
| 663 | }); |
| 664 | } else if (payload instanceof RouterAdvertisement) { |
| 665 | final RouterAdvertisement ra = (RouterAdvertisement) payload; |
| 666 | sb.append("\nra_hop_limit: "); |
| 667 | sb.append(ra.getCurrentHopLimit()); |
| 668 | sb.append("\nra_mflag: "); |
| 669 | sb.append(ra.getMFlag()); |
| 670 | sb.append("\nra_oflag: "); |
| 671 | sb.append(ra.getOFlag()); |
| 672 | sb.append("\nra_reachable_time: "); |
| 673 | sb.append(ra.getReachableTime()); |
| 674 | sb.append("\nra_retransmit_time: "); |
| 675 | sb.append(ra.getRetransmitTimer()); |
| 676 | sb.append("\nra_router_lifetime: "); |
| 677 | sb.append(ra.getRouterLifetime()); |
| 678 | ra.getOptions().forEach(option -> { |
| 679 | sb.append("\noption_type: "); |
| 680 | sb.append(option.type()); |
| 681 | sb.append("\noption_data: "); |
| 682 | sb.append(bytesToHex(option.data())); |
| 683 | }); |
| 684 | } else if (payload instanceof Redirect) { |
| 685 | final Redirect rd = (Redirect) payload; |
| 686 | sb.append("\nrd_target_addr: "); |
| 687 | sb.append(Ip6Address.valueOf(rd.getTargetAddress()).toString()); |
| 688 | rd.getOptions().forEach(option -> { |
| 689 | sb.append("\noption_type: "); |
| 690 | sb.append(option.type()); |
| 691 | sb.append("\noption_data: "); |
| 692 | sb.append(bytesToHex(option.data())); |
| 693 | }); |
| 694 | } |
| 695 | } |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 696 | } |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 697 | } else if (pkt instanceof DHCP) { |
| 698 | sb.append("\ndhcp packet"); |
| 699 | } else if (pkt instanceof Data) { |
| 700 | sb.append("\ndata packet"); |
| 701 | } else if (pkt instanceof LLC) { |
| 702 | sb.append("\nllc packet"); |
| 703 | } else { |
tom | 5f18cf3 | 2014-09-13 14:10:57 -0700 | [diff] [blame] | 704 | sb.append("\nunknown packet"); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | return sb.toString(); |
| 708 | } |
| 709 | |
| 710 | public static String bytesToHex(byte[] in) { |
| 711 | final StringBuilder builder = new StringBuilder(); |
| 712 | for (byte b : in) { |
| 713 | builder.append(String.format("%02x", b)); |
| 714 | } |
| 715 | return builder.toString(); |
| 716 | } |
| 717 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 718 | /** |
| 719 | * Deserializer function for Ethernet packets. |
| 720 | * |
| 721 | * @return deserializer function |
| 722 | */ |
| 723 | public static Deserializer<Ethernet> deserializer() { |
| 724 | return (data, offset, length) -> { |
| 725 | checkInput(data, offset, length, ETHERNET_HEADER_LENGTH); |
| 726 | |
| 727 | byte[] addressBuffer = new byte[DATALAYER_ADDRESS_LENGTH]; |
| 728 | |
| 729 | ByteBuffer bb = ByteBuffer.wrap(data, offset, length); |
| 730 | Ethernet eth = new Ethernet(); |
| 731 | // Read destination MAC address into buffer |
| 732 | bb.get(addressBuffer); |
| 733 | eth.setDestinationMACAddress(addressBuffer); |
| 734 | |
| 735 | // Read source MAC address into buffer |
| 736 | bb.get(addressBuffer); |
| 737 | eth.setSourceMACAddress(addressBuffer); |
| 738 | |
| 739 | short ethType = bb.getShort(); |
Amit Ghosh | 764a1b4 | 2017-03-18 08:20:06 +0000 | [diff] [blame] | 740 | if (ethType == TYPE_QINQ) { |
| 741 | // in this case we excpect 2 VLAN headers |
| 742 | checkHeaderLength(length, ETHERNET_HEADER_LENGTH + VLAN_HEADER_LENGTH + VLAN_HEADER_LENGTH); |
| 743 | final short tci = bb.getShort(); |
| 744 | eth.setQinQPriorityCode((byte) (tci >> 13 & 0x07)); |
| 745 | eth.setQinQVID((short) (tci & 0x0fff)); |
| 746 | eth.setQinQTPID(TYPE_QINQ); |
| 747 | ethType = bb.getShort(); |
| 748 | } |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 749 | if (ethType == TYPE_VLAN) { |
| 750 | checkHeaderLength(length, ETHERNET_HEADER_LENGTH + VLAN_HEADER_LENGTH); |
| 751 | final short tci = bb.getShort(); |
| 752 | eth.setPriorityCode((byte) (tci >> 13 & 0x07)); |
| 753 | eth.setVlanID((short) (tci & 0x0fff)); |
| 754 | ethType = bb.getShort(); |
Amit Ghosh | 764a1b4 | 2017-03-18 08:20:06 +0000 | [diff] [blame] | 755 | |
| 756 | if (ethType == TYPE_VLAN) { |
| 757 | // We handle only double tagged packets here and assume that in this case |
| 758 | // TYPE_QINQ above was not hit |
| 759 | // We put the values retrieved above with TYPE_VLAN in |
| 760 | // qInQ fields |
| 761 | checkHeaderLength(length, ETHERNET_HEADER_LENGTH + VLAN_HEADER_LENGTH); |
| 762 | eth.setQinQPriorityCode(eth.getPriorityCode()); |
| 763 | eth.setQinQVID(eth.getVlanID()); |
| 764 | eth.setQinQTPID(TYPE_VLAN); |
| 765 | |
| 766 | final short innerTci = bb.getShort(); |
| 767 | eth.setPriorityCode((byte) (innerTci >> 13 & 0x07)); |
| 768 | eth.setVlanID((short) (innerTci & 0x0fff)); |
| 769 | ethType = bb.getShort(); |
| 770 | } |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 771 | } else { |
| 772 | eth.setVlanID(Ethernet.VLAN_UNTAGGED); |
| 773 | } |
| 774 | eth.setEtherType(ethType); |
| 775 | |
| 776 | IPacket payload; |
| 777 | Deserializer<? extends IPacket> deserializer; |
| 778 | if (Ethernet.ETHERTYPE_DESERIALIZER_MAP.containsKey(ethType)) { |
| 779 | deserializer = Ethernet.ETHERTYPE_DESERIALIZER_MAP.get(ethType); |
| 780 | } else { |
| 781 | deserializer = Data.deserializer(); |
| 782 | } |
| 783 | payload = deserializer.deserialize(data, bb.position(), |
| 784 | bb.limit() - bb.position()); |
| 785 | payload.setParent(eth); |
| 786 | eth.setPayload(payload); |
| 787 | |
| 788 | return eth; |
| 789 | }; |
| 790 | } |
Ray Milkey | f0c4761 | 2017-09-28 11:29:38 -0700 | [diff] [blame] | 791 | |
| 792 | /** |
| 793 | * Make an exact copy of the ethernet packet. |
| 794 | * |
| 795 | * @return copy of the packet |
| 796 | */ |
| 797 | public Ethernet duplicate() { |
| 798 | try { |
| 799 | byte[] data = serialize(); |
| 800 | return deserializer().deserialize(data, 0, data.length); |
| 801 | } catch (DeserializationException dex) { |
| 802 | // If we can't make an object out of the serialized data, its a defect |
| 803 | throw new IllegalStateException(dex); |
| 804 | } |
| 805 | } |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 806 | } |