blob: fb249a9985bd28690b6933722652c956e3c6d60a [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 */
16
17
alshabibc4901cd2014-09-05 16:50:40 -070018
19package org.onlab.packet;
20
Charles Chan119eeb52015-10-27 12:40:25 -070021import org.onlab.packet.ndp.NeighborAdvertisement;
22import org.onlab.packet.ndp.NeighborSolicitation;
23import org.onlab.packet.ndp.Redirect;
24import org.onlab.packet.ndp.RouterAdvertisement;
25import org.onlab.packet.ndp.RouterSolicitation;
26
Yuta HIGUCHIbfc2e922017-06-07 21:46:05 -070027import com.google.common.collect.ImmutableMap;
28
alshabibc4901cd2014-09-05 16:50:40 -070029import java.nio.ByteBuffer;
30import java.util.Arrays;
alshabibc4901cd2014-09-05 16:50:40 -070031import java.util.Map;
32
Jonathan Hart2a655752015-04-07 16:46:33 -070033import static com.google.common.base.Preconditions.checkNotNull;
34import static org.onlab.packet.PacketUtils.checkHeaderLength;
35import static org.onlab.packet.PacketUtils.checkInput;
alshabibc4901cd2014-09-05 16:50:40 -070036
37/**
Jian Li5fc14292015-12-04 11:30:46 -080038 * Ethernet Packet.
alshabibc4901cd2014-09-05 16:50:40 -070039 */
40public class Ethernet extends BasePacket {
41 private static final String HEXES = "0123456789ABCDEF";
Pier Ventre48ca5192016-11-28 16:52:24 -080042 private static final String HEX_PROTO = "0x%s";
Thomas Vachuska5dd52f72014-11-28 19:27:45 -080043
alshabib7b808c52015-06-26 14:22:24 -070044 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 Ghosh764a1b42017-03-18 08:20:06 +000050 public static final short TYPE_QINQ = EthType.EtherType.QINQ.ethType().toShort();
alshabib7b808c52015-06-26 14:22:24 -070051 public static final short TYPE_BSN = EthType.EtherType.BDDP.ethType().toShort();
52
Jon Hall8c7b06a2017-02-22 13:37:33 -080053 public static final short MPLS_UNICAST = EthType.EtherType.MPLS_UNICAST.ethType().toShort();
alshabib7b808c52015-06-26 14:22:24 -070054 public static final short MPLS_MULTICAST = EthType.EtherType.MPLS_MULTICAST.ethType().toShort();
alshabibcaf1ca22015-06-25 15:18:16 -070055
Jonathan Hart2a655752015-04-07 16:46:33 -070056
alshabibcaf1ca22015-06-25 15:18:16 -070057 public static final short VLAN_UNTAGGED = (short) 0xffff;
Jonathan Hart2a655752015-04-07 16:46:33 -070058
59 public static final short ETHERNET_HEADER_LENGTH = 14; // bytes
60 public static final short VLAN_HEADER_LENGTH = 4; // bytes
61
alshabibc4901cd2014-09-05 16:50:40 -070062 public static final short DATALAYER_ADDRESS_LENGTH = 6; // bytes
Jonathan Hart2a655752015-04-07 16:46:33 -070063
Yuta HIGUCHIbfc2e922017-06-07 21:46:05 -070064 private static final Map<Short, Deserializer<? extends IPacket>> ETHERTYPE_DESERIALIZER_MAP;
alshabibc4901cd2014-09-05 16:50:40 -070065
66 static {
Yuta HIGUCHIbfc2e922017-06-07 21:46:05 -070067 ImmutableMap.Builder<Short, Deserializer<? extends IPacket>> builder =
68 ImmutableMap.builder();
69
alshabibcaf1ca22015-06-25 15:18:16 -070070 for (EthType.EtherType ethType : EthType.EtherType.values()) {
alshabib7b808c52015-06-26 14:22:24 -070071 if (ethType.deserializer() != null) {
Yuta HIGUCHIbfc2e922017-06-07 21:46:05 -070072 builder.put(ethType.ethType().toShort(), ethType.deserializer());
alshabibcaf1ca22015-06-25 15:18:16 -070073 }
74 }
Yuta HIGUCHIbfc2e922017-06-07 21:46:05 -070075 ETHERTYPE_DESERIALIZER_MAP = builder.build();
alshabibc4901cd2014-09-05 16:50:40 -070076 }
77
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070078 protected MacAddress destinationMACAddress;
79 protected MacAddress sourceMACAddress;
alshabibc4901cd2014-09-05 16:50:40 -070080 protected byte priorityCode;
Amit Ghosh764a1b42017-03-18 08:20:06 +000081 protected byte qInQPriorityCode;
alshabibc4901cd2014-09-05 16:50:40 -070082 protected short vlanID;
Amit Ghosh764a1b42017-03-18 08:20:06 +000083 protected short qinqVID;
84 protected short qinqTPID;
alshabibc4901cd2014-09-05 16:50:40 -070085 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 Ghosh764a1b42017-03-18 08:20:06 +000094 this.qinqVID = Ethernet.VLAN_UNTAGGED;
95 this.qinqTPID = TYPE_QINQ;
alshabibc4901cd2014-09-05 16:50:40 -070096 }
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 Koshibea9c199f2014-09-16 16:21:40 -0700112 public MacAddress getDestinationMAC() {
alshabibc4901cd2014-09-05 16:50:40 -0700113 return this.destinationMACAddress;
114 }
115
116 /**
117 * Sets the destination MAC address.
118 *
tom5f18cf32014-09-13 14:10:57 -0700119 * @param destMac the destination MAC to set
alshabibc4901cd2014-09-05 16:50:40 -0700120 * @return the Ethernet frame
121 */
Yuta HIGUCHI3e848a82014-11-02 20:19:42 -0800122 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 */
alshabibc4901cd2014-09-05 16:50:40 -0700133 public Ethernet setDestinationMACAddress(final byte[] destMac) {
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700134 this.destinationMACAddress = MacAddress.valueOf(destMac);
alshabibc4901cd2014-09-05 16:50:40 -0700135 return this;
136 }
137
138 /**
139 * Sets the destination MAC address.
140 *
tom5f18cf32014-09-13 14:10:57 -0700141 * @param destMac the destination MAC to set
alshabibc4901cd2014-09-05 16:50:40 -0700142 * @return the Ethernet frame
143 */
144 public Ethernet setDestinationMACAddress(final String destMac) {
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700145 this.destinationMACAddress = MacAddress.valueOf(destMac);
alshabibc4901cd2014-09-05 16:50:40 -0700146 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 Koshibea9c199f2014-09-16 16:21:40 -0700163 public MacAddress getSourceMAC() {
alshabibc4901cd2014-09-05 16:50:40 -0700164 return this.sourceMACAddress;
165 }
166
167 /**
168 * Sets the source MAC address.
169 *
tom5f18cf32014-09-13 14:10:57 -0700170 * @param sourceMac the source MAC to set
alshabibc4901cd2014-09-05 16:50:40 -0700171 * @return the Ethernet frame
172 */
Yuta HIGUCHI3e848a82014-11-02 20:19:42 -0800173 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 */
alshabibc4901cd2014-09-05 16:50:40 -0700184 public Ethernet setSourceMACAddress(final byte[] sourceMac) {
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700185 this.sourceMACAddress = MacAddress.valueOf(sourceMac);
alshabibc4901cd2014-09-05 16:50:40 -0700186 return this;
187 }
188
189 /**
190 * Sets the source MAC address.
191 *
tom5f18cf32014-09-13 14:10:57 -0700192 * @param sourceMac the source MAC to set
alshabibc4901cd2014-09-05 16:50:40 -0700193 * @return the Ethernet frame
194 */
195 public Ethernet setSourceMACAddress(final String sourceMac) {
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700196 this.sourceMACAddress = MacAddress.valueOf(sourceMac);
alshabibc4901cd2014-09-05 16:50:40 -0700197 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 *
tom5f18cf32014-09-13 14:10:57 -0700212 * @param priority the priorityCode to set
alshabibc4901cd2014-09-05 16:50:40 -0700213 * @return the Ethernet frame
214 */
215 public Ethernet setPriorityCode(final byte priority) {
216 this.priorityCode = priority;
217 return this;
218 }
219
220 /**
Amit Ghosh764a1b42017-03-18 08:20:06 +0000221 * 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 /**
alshabibc4901cd2014-09-05 16:50:40 -0700241 * 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 *
tom5f18cf32014-09-13 14:10:57 -0700252 * @param vlan the vlanID to set
alshabibc4901cd2014-09-05 16:50:40 -0700253 * @return the Ethernet frame
254 */
255 public Ethernet setVlanID(final short vlan) {
256 this.vlanID = vlan;
257 return this;
258 }
259
260 /**
Amit Ghosh764a1b42017-03-18 08:20:06 +0000261 * 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 /**
alshabibc4901cd2014-09-05 16:50:40 -0700302 * 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 *
tom5f18cf32014-09-13 14:10:57 -0700313 * @param ethType the etherType to set
alshabibc4901cd2014-09-05 16:50:40 -0700314 * @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 *
tom5f18cf32014-09-13 14:10:57 -0700348 * @param pd
alshabibc4901cd2014-09-05 16:50:40 -0700349 * the pad to set
Yuta HIGUCHI2281b3f2014-11-04 00:20:48 -0800350 * @return this
alshabibc4901cd2014-09-05 16:50:40 -0700351 */
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 Ghosh764a1b42017-03-18 08:20:06 +0000365 + (this.qinqVID == Ethernet.VLAN_UNTAGGED ? 0 : 4)
alshabibc4901cd2014-09-05 16:50:40 -0700366 + (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 Ghosh764a1b42017-03-18 08:20:06 +0000374 if (this.qinqVID != Ethernet.VLAN_UNTAGGED) {
375 bb.putShort(this.qinqTPID);
376 bb.putShort((short) (this.qInQPriorityCode << 13 | this.qinqVID & 0x0fff));
377 }
alshabibc4901cd2014-09-05 16:50:40 -0700378 if (this.vlanID != Ethernet.VLAN_UNTAGGED) {
alshabib10580802015-02-18 18:30:33 -0800379 bb.putShort(TYPE_VLAN);
alshabibc4901cd2014-09-05 16:50:40 -0700380 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
alshabibc4901cd2014-09-05 16:50:40 -0700392 /**
393 * Checks to see if a string is a valid MAC address.
394 *
Yuta HIGUCHI2281b3f2014-11-04 00:20:48 -0800395 * @param macAddress string to test if it is a valid MAC
alshabibc4901cd2014-09-05 16:50:40 -0700396 * @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 HIGUCHI2281b3f2014-11-04 00:20:48 -0800418 * The MAC address to convert into a byte array
alshabibc4901cd2014-09-05 16:50:40 -0700419 * @return The macAddress as a byte array
420 */
421 public static byte[] toMACAddress(final String macAddress) {
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700422 return MacAddress.valueOf(macAddress).toBytes();
alshabibc4901cd2014-09-05 16:50:40 -0700423 }
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 HIGUCHI2281b3f2014-11-04 00:20:48 -0800429 * @param macAddress MAC address as a byte array
alshabibc4901cd2014-09-05 16:50:40 -0700430 * @return a long containing the mac address bytes
431 */
432 public static long toLong(final byte[] macAddress) {
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700433 return MacAddress.valueOf(macAddress).toLong();
alshabibc4901cd2014-09-05 16:50:40 -0700434 }
435
436 /**
437 * Converts a long MAC address to a byte array.
438 *
Yuta HIGUCHI2281b3f2014-11-04 00:20:48 -0800439 * @param macAddress MAC address set on the lower order bytes of the long
alshabibc4901cd2014-09-05 16:50:40 -0700440 * @return the bytes of the mac address
441 */
442 public static byte[] toByteArray(final long macAddress) {
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700443 return MacAddress.valueOf(macAddress).toBytes();
alshabibc4901cd2014-09-05 16:50:40 -0700444 }
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 Ghosh764a1b42017-03-18 08:20:06 +0000457 result = prime * result + this.qinqVID;
458 result = prime * result + this.qInQPriorityCode;
alshabibc4901cd2014-09-05 16:50:40 -0700459 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 Ghosh764a1b42017-03-18 08:20:06 +0000486 if (this.qInQPriorityCode != other.qInQPriorityCode) {
487 return false;
488 }
489 if (this.qinqVID != other.qinqVID) {
490 return false;
491 }
alshabibc4901cd2014-09-05 16:50:40 -0700492 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() {
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -0700517 final StringBuilder sb = new StringBuilder("\n");
alshabibc4901cd2014-09-05 16:50:40 -0700518 final IPacket pkt = this.getPayload();
519
520 if (pkt instanceof ARP) {
521 sb.append("arp");
pierf8e328d2019-06-28 22:17:31 +0200522 } else if (pkt instanceof MPLS) {
523 sb.append("mpls");
alshabibc4901cd2014-09-05 16:50:40 -0700524 } 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 {
pierf8e328d2019-06-28 22:17:31 +0200533 // Just print the ethertype
Pier Ventre48ca5192016-11-28 16:52:24 -0800534 sb.append(String.format(HEX_PROTO,
535 Integer.toHexString(this.getEtherType() & 0xffff)));
alshabibc4901cd2014-09-05 16:50:40 -0700536 }
537
Amit Ghosh764a1b42017-03-18 08:20:06 +0000538 if (this.getQinQVID() != Ethernet.VLAN_UNTAGGED) {
539 sb.append("\ndl_qinqVlan: ");
540 sb.append(this.getQinQVID());
541 sb.append("\ndl_qinqVlan_pcp: ");
542 sb.append(this.getQinQPriorityCode());
543 }
544
alshabibc4901cd2014-09-05 16:50:40 -0700545 sb.append("\ndl_vlan: ");
546 if (this.getVlanID() == Ethernet.VLAN_UNTAGGED) {
547 sb.append("untagged");
548 } else {
549 sb.append(this.getVlanID());
550 }
551 sb.append("\ndl_vlan_pcp: ");
552 sb.append(this.getPriorityCode());
553 sb.append("\ndl_src: ");
554 sb.append(bytesToHex(this.getSourceMACAddress()));
555 sb.append("\ndl_dst: ");
556 sb.append(bytesToHex(this.getDestinationMACAddress()));
557
558 if (pkt instanceof ARP) {
559 final ARP p = (ARP) pkt;
560 sb.append("\nnw_src: ");
561 sb.append(IPv4.fromIPv4Address(IPv4.toIPv4Address(p
562 .getSenderProtocolAddress())));
563 sb.append("\nnw_dst: ");
564 sb.append(IPv4.fromIPv4Address(IPv4.toIPv4Address(p
565 .getTargetProtocolAddress())));
pierf8e328d2019-06-28 22:17:31 +0200566 } else if (pkt instanceof MPLS) {
567 final MPLS p = (MPLS) pkt;
568 sb.append("\nmpls: ");
569 sb.append(this.etherType == MPLS_UNICAST ? "unicast" : "multicast");
570 sb.append("\nmpls_label: ");
571 sb.append(p.label);
alshabibc4901cd2014-09-05 16:50:40 -0700572 } else if (pkt instanceof LLDP) {
573 sb.append("lldp packet");
574 } else if (pkt instanceof ICMP) {
575 final ICMP icmp = (ICMP) pkt;
576 sb.append("\nicmp_type: ");
577 sb.append(icmp.getIcmpType());
578 sb.append("\nicmp_code: ");
579 sb.append(icmp.getIcmpCode());
580 } else if (pkt instanceof IPv4) {
581 final IPv4 p = (IPv4) pkt;
582 sb.append("\nnw_src: ");
583 sb.append(IPv4.fromIPv4Address(p.getSourceAddress()));
584 sb.append("\nnw_dst: ");
585 sb.append(IPv4.fromIPv4Address(p.getDestinationAddress()));
586 sb.append("\nnw_tos: ");
587 sb.append(p.getDiffServ());
588 sb.append("\nnw_proto: ");
589 sb.append(p.getProtocol());
590
Charles Chan119eeb52015-10-27 12:40:25 -0700591 IPacket payload = pkt.getPayload();
592 if (payload != null) {
593 if (payload instanceof TCP) {
594 sb.append("\ntp_src: ");
595 sb.append(((TCP) payload).getSourcePort());
596 sb.append("\ntp_dst: ");
597 sb.append(((TCP) payload).getDestinationPort());
Charles Chan119eeb52015-10-27 12:40:25 -0700598 } else if (payload instanceof UDP) {
599 sb.append("\ntp_src: ");
600 sb.append(((UDP) payload).getSourcePort());
601 sb.append("\ntp_dst: ");
602 sb.append(((UDP) payload).getDestinationPort());
603 } else if (payload instanceof ICMP) {
604 final ICMP icmp = (ICMP) payload;
605 sb.append("\nicmp_type: ");
606 sb.append(icmp.getIcmpType());
607 sb.append("\nicmp_code: ");
608 sb.append(icmp.getIcmpCode());
609 }
alshabibc4901cd2014-09-05 16:50:40 -0700610 }
Charles Chan119eeb52015-10-27 12:40:25 -0700611 } else if (pkt instanceof IPv6) {
612 final IPv6 ipv6 = (IPv6) pkt;
613 sb.append("\nipv6_src: ");
614 sb.append(Ip6Address.valueOf(ipv6.getSourceAddress()).toString());
615 sb.append("\nipv6_dst: ");
616 sb.append(Ip6Address.valueOf(ipv6.getDestinationAddress()).toString());
617 sb.append("\nipv6_proto: ");
618 sb.append(ipv6.getNextHeader());
alshabibc4901cd2014-09-05 16:50:40 -0700619
Charles Chan119eeb52015-10-27 12:40:25 -0700620 IPacket payload = pkt.getPayload();
621 if (payload != null && payload instanceof ICMP6) {
622 final ICMP6 icmp6 = (ICMP6) payload;
623 sb.append("\nicmp6_type: ");
624 sb.append(icmp6.getIcmpType());
625 sb.append("\nicmp6_code: ");
626 sb.append(icmp6.getIcmpCode());
627
628 payload = payload.getPayload();
629 if (payload != null) {
630 if (payload instanceof NeighborSolicitation) {
631 final NeighborSolicitation ns = (NeighborSolicitation) payload;
632 sb.append("\nns_target_addr: ");
633 sb.append(Ip6Address.valueOf(ns.getTargetAddress()).toString());
634 ns.getOptions().forEach(option -> {
635 sb.append("\noption_type: ");
636 sb.append(option.type());
637 sb.append("\noption_data: ");
638 sb.append(bytesToHex(option.data()));
639 });
640 } else if (payload instanceof NeighborAdvertisement) {
641 final NeighborAdvertisement na = (NeighborAdvertisement) payload;
642 sb.append("\nna_target_addr: ");
643 sb.append(Ip6Address.valueOf(na.getTargetAddress()).toString());
644 sb.append("\nna_solicited_flag: ");
645 sb.append(na.getSolicitedFlag());
646 sb.append("\nna_router_flag: ");
647 sb.append(na.getRouterFlag());
648 sb.append("\nna_override_flag: ");
649 sb.append(na.getOverrideFlag());
650 na.getOptions().forEach(option -> {
651 sb.append("\noption_type: ");
652 sb.append(option.type());
653 sb.append("\noption_data: ");
654 sb.append(bytesToHex(option.data()));
655 });
656 } else if (payload instanceof RouterSolicitation) {
657 final RouterSolicitation rs = (RouterSolicitation) payload;
658 sb.append("\nrs");
659 rs.getOptions().forEach(option -> {
660 sb.append("\noption_type: ");
661 sb.append(option.type());
662 sb.append("\noption_data: ");
663 sb.append(bytesToHex(option.data()));
664 });
665 } else if (payload instanceof RouterAdvertisement) {
666 final RouterAdvertisement ra = (RouterAdvertisement) payload;
667 sb.append("\nra_hop_limit: ");
668 sb.append(ra.getCurrentHopLimit());
669 sb.append("\nra_mflag: ");
670 sb.append(ra.getMFlag());
671 sb.append("\nra_oflag: ");
672 sb.append(ra.getOFlag());
673 sb.append("\nra_reachable_time: ");
674 sb.append(ra.getReachableTime());
675 sb.append("\nra_retransmit_time: ");
676 sb.append(ra.getRetransmitTimer());
677 sb.append("\nra_router_lifetime: ");
678 sb.append(ra.getRouterLifetime());
679 ra.getOptions().forEach(option -> {
680 sb.append("\noption_type: ");
681 sb.append(option.type());
682 sb.append("\noption_data: ");
683 sb.append(bytesToHex(option.data()));
684 });
685 } else if (payload instanceof Redirect) {
686 final Redirect rd = (Redirect) payload;
687 sb.append("\nrd_target_addr: ");
688 sb.append(Ip6Address.valueOf(rd.getTargetAddress()).toString());
689 rd.getOptions().forEach(option -> {
690 sb.append("\noption_type: ");
691 sb.append(option.type());
692 sb.append("\noption_data: ");
693 sb.append(bytesToHex(option.data()));
694 });
695 }
696 }
alshabibc4901cd2014-09-05 16:50:40 -0700697 }
alshabibc4901cd2014-09-05 16:50:40 -0700698 } else if (pkt instanceof DHCP) {
699 sb.append("\ndhcp packet");
700 } else if (pkt instanceof Data) {
701 sb.append("\ndata packet");
702 } else if (pkt instanceof LLC) {
703 sb.append("\nllc packet");
Saurav Dasa432cf82018-10-11 15:29:24 -0700704 } else if (pkt instanceof EAPOL) {
705 sb.append("\neapol");
alshabibc4901cd2014-09-05 16:50:40 -0700706 } else {
tom5f18cf32014-09-13 14:10:57 -0700707 sb.append("\nunknown packet");
alshabibc4901cd2014-09-05 16:50:40 -0700708 }
alshabibc4901cd2014-09-05 16:50:40 -0700709 return sb.toString();
710 }
711
712 public static String bytesToHex(byte[] in) {
713 final StringBuilder builder = new StringBuilder();
714 for (byte b : in) {
715 builder.append(String.format("%02x", b));
716 }
717 return builder.toString();
718 }
719
Jonathan Hart2a655752015-04-07 16:46:33 -0700720 /**
721 * Deserializer function for Ethernet packets.
722 *
723 * @return deserializer function
724 */
725 public static Deserializer<Ethernet> deserializer() {
726 return (data, offset, length) -> {
727 checkInput(data, offset, length, ETHERNET_HEADER_LENGTH);
728
729 byte[] addressBuffer = new byte[DATALAYER_ADDRESS_LENGTH];
730
731 ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
732 Ethernet eth = new Ethernet();
733 // Read destination MAC address into buffer
734 bb.get(addressBuffer);
735 eth.setDestinationMACAddress(addressBuffer);
736
737 // Read source MAC address into buffer
738 bb.get(addressBuffer);
739 eth.setSourceMACAddress(addressBuffer);
740
741 short ethType = bb.getShort();
Amit Ghosh764a1b42017-03-18 08:20:06 +0000742 if (ethType == TYPE_QINQ) {
743 // in this case we excpect 2 VLAN headers
744 checkHeaderLength(length, ETHERNET_HEADER_LENGTH + VLAN_HEADER_LENGTH + VLAN_HEADER_LENGTH);
745 final short tci = bb.getShort();
746 eth.setQinQPriorityCode((byte) (tci >> 13 & 0x07));
747 eth.setQinQVID((short) (tci & 0x0fff));
748 eth.setQinQTPID(TYPE_QINQ);
749 ethType = bb.getShort();
750 }
Jonathan Hart2a655752015-04-07 16:46:33 -0700751 if (ethType == TYPE_VLAN) {
752 checkHeaderLength(length, ETHERNET_HEADER_LENGTH + VLAN_HEADER_LENGTH);
753 final short tci = bb.getShort();
754 eth.setPriorityCode((byte) (tci >> 13 & 0x07));
755 eth.setVlanID((short) (tci & 0x0fff));
756 ethType = bb.getShort();
Amit Ghosh764a1b42017-03-18 08:20:06 +0000757
758 if (ethType == TYPE_VLAN) {
759 // We handle only double tagged packets here and assume that in this case
760 // TYPE_QINQ above was not hit
761 // We put the values retrieved above with TYPE_VLAN in
762 // qInQ fields
763 checkHeaderLength(length, ETHERNET_HEADER_LENGTH + VLAN_HEADER_LENGTH);
764 eth.setQinQPriorityCode(eth.getPriorityCode());
765 eth.setQinQVID(eth.getVlanID());
766 eth.setQinQTPID(TYPE_VLAN);
767
768 final short innerTci = bb.getShort();
769 eth.setPriorityCode((byte) (innerTci >> 13 & 0x07));
770 eth.setVlanID((short) (innerTci & 0x0fff));
771 ethType = bb.getShort();
772 }
Jonathan Hart2a655752015-04-07 16:46:33 -0700773 } else {
774 eth.setVlanID(Ethernet.VLAN_UNTAGGED);
775 }
776 eth.setEtherType(ethType);
777
778 IPacket payload;
779 Deserializer<? extends IPacket> deserializer;
780 if (Ethernet.ETHERTYPE_DESERIALIZER_MAP.containsKey(ethType)) {
781 deserializer = Ethernet.ETHERTYPE_DESERIALIZER_MAP.get(ethType);
782 } else {
783 deserializer = Data.deserializer();
784 }
785 payload = deserializer.deserialize(data, bb.position(),
786 bb.limit() - bb.position());
787 payload.setParent(eth);
788 eth.setPayload(payload);
789
790 return eth;
791 };
792 }
Ray Milkeyf0c47612017-09-28 11:29:38 -0700793
794 /**
795 * Make an exact copy of the ethernet packet.
796 *
797 * @return copy of the packet
798 */
799 public Ethernet duplicate() {
800 try {
801 byte[] data = serialize();
802 return deserializer().deserialize(data, 0, data.length);
803 } catch (DeserializationException dex) {
804 // If we can't make an object out of the serialized data, its a defect
805 throw new IllegalStateException(dex);
806 }
807 }
alshabibc4901cd2014-09-05 16:50:40 -0700808}