blob: 96ec021b7548a7601983f1d92e8cea7685cc5d3c [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
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
Yuta HIGUCHI3e848a82014-11-02 20:19:42 -080021import static com.google.common.base.Preconditions.checkNotNull;
22
alshabibc4901cd2014-09-05 16:50:40 -070023import java.nio.ByteBuffer;
24import java.util.Arrays;
25import java.util.HashMap;
26import java.util.Map;
27
28
29/**
30 *
alshabibc4901cd2014-09-05 16:50:40 -070031 */
32public class Ethernet extends BasePacket {
33 private static final String HEXES = "0123456789ABCDEF";
34 public static final short TYPE_ARP = 0x0806;
35 public static final short TYPE_RARP = (short) 0x8035;
36 public static final short TYPE_IPV4 = 0x0800;
37 public static final short TYPE_LLDP = (short) 0x88cc;
38 public static final short TYPE_BSN = (short) 0x8942;
39 public static final short VLAN_UNTAGGED = (short) 0xffff;
40 public static final short DATALAYER_ADDRESS_LENGTH = 6; // bytes
41 public static Map<Short, Class<? extends IPacket>> etherTypeClassMap;
42
43 static {
44 Ethernet.etherTypeClassMap = new HashMap<Short, Class<? extends IPacket>>();
45 Ethernet.etherTypeClassMap.put(Ethernet.TYPE_ARP, ARP.class);
46 Ethernet.etherTypeClassMap.put(Ethernet.TYPE_RARP, ARP.class);
47 Ethernet.etherTypeClassMap.put(Ethernet.TYPE_IPV4, IPv4.class);
48 Ethernet.etherTypeClassMap.put(Ethernet.TYPE_LLDP, LLDP.class);
alshabib7911a052014-10-16 17:49:37 -070049 Ethernet.etherTypeClassMap.put(Ethernet.TYPE_BSN, LLDP.class);
alshabibc4901cd2014-09-05 16:50:40 -070050 }
51
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070052 protected MacAddress destinationMACAddress;
53 protected MacAddress sourceMACAddress;
alshabibc4901cd2014-09-05 16:50:40 -070054 protected byte priorityCode;
55 protected short vlanID;
56 protected short etherType;
57 protected boolean pad = false;
58
59 /**
60 * By default, set Ethernet to untagged.
61 */
62 public Ethernet() {
63 super();
64 this.vlanID = Ethernet.VLAN_UNTAGGED;
65 }
66
67 /**
68 * Gets the destination MAC address.
69 *
70 * @return the destination MAC as a byte array
71 */
72 public byte[] getDestinationMACAddress() {
73 return this.destinationMACAddress.toBytes();
74 }
75
76 /**
77 * Gets the destination MAC address.
78 *
79 * @return the destination MAC
80 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070081 public MacAddress getDestinationMAC() {
alshabibc4901cd2014-09-05 16:50:40 -070082 return this.destinationMACAddress;
83 }
84
85 /**
86 * Sets the destination MAC address.
87 *
tom5f18cf32014-09-13 14:10:57 -070088 * @param destMac the destination MAC to set
alshabibc4901cd2014-09-05 16:50:40 -070089 * @return the Ethernet frame
90 */
Yuta HIGUCHI3e848a82014-11-02 20:19:42 -080091 public Ethernet setDestinationMACAddress(final MacAddress destMac) {
92 this.destinationMACAddress = checkNotNull(destMac);
93 return this;
94 }
95
96 /**
97 * Sets the destination MAC address.
98 *
99 * @param destMac the destination MAC to set
100 * @return the Ethernet frame
101 */
alshabibc4901cd2014-09-05 16:50:40 -0700102 public Ethernet setDestinationMACAddress(final byte[] destMac) {
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700103 this.destinationMACAddress = MacAddress.valueOf(destMac);
alshabibc4901cd2014-09-05 16:50:40 -0700104 return this;
105 }
106
107 /**
108 * Sets the destination MAC address.
109 *
tom5f18cf32014-09-13 14:10:57 -0700110 * @param destMac the destination MAC to set
alshabibc4901cd2014-09-05 16:50:40 -0700111 * @return the Ethernet frame
112 */
113 public Ethernet setDestinationMACAddress(final String destMac) {
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700114 this.destinationMACAddress = MacAddress.valueOf(destMac);
alshabibc4901cd2014-09-05 16:50:40 -0700115 return this;
116 }
117
118 /**
119 * Gets the source MAC address.
120 *
121 * @return the source MACAddress as a byte array
122 */
123 public byte[] getSourceMACAddress() {
124 return this.sourceMACAddress.toBytes();
125 }
126
127 /**
128 * Gets the source MAC address.
129 *
130 * @return the source MACAddress
131 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700132 public MacAddress getSourceMAC() {
alshabibc4901cd2014-09-05 16:50:40 -0700133 return this.sourceMACAddress;
134 }
135
136 /**
137 * Sets the source MAC address.
138 *
tom5f18cf32014-09-13 14:10:57 -0700139 * @param sourceMac the source MAC to set
alshabibc4901cd2014-09-05 16:50:40 -0700140 * @return the Ethernet frame
141 */
Yuta HIGUCHI3e848a82014-11-02 20:19:42 -0800142 public Ethernet setSourceMACAddress(final MacAddress sourceMac) {
143 this.sourceMACAddress = checkNotNull(sourceMac);
144 return this;
145 }
146
147 /**
148 * Sets the source MAC address.
149 *
150 * @param sourceMac the source MAC to set
151 * @return the Ethernet frame
152 */
alshabibc4901cd2014-09-05 16:50:40 -0700153 public Ethernet setSourceMACAddress(final byte[] sourceMac) {
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700154 this.sourceMACAddress = MacAddress.valueOf(sourceMac);
alshabibc4901cd2014-09-05 16:50:40 -0700155 return this;
156 }
157
158 /**
159 * Sets the source MAC address.
160 *
tom5f18cf32014-09-13 14:10:57 -0700161 * @param sourceMac the source MAC to set
alshabibc4901cd2014-09-05 16:50:40 -0700162 * @return the Ethernet frame
163 */
164 public Ethernet setSourceMACAddress(final String sourceMac) {
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700165 this.sourceMACAddress = MacAddress.valueOf(sourceMac);
alshabibc4901cd2014-09-05 16:50:40 -0700166 return this;
167 }
168
169 /**
170 * Gets the priority code.
171 *
172 * @return the priorityCode
173 */
174 public byte getPriorityCode() {
175 return this.priorityCode;
176 }
177
178 /**
179 * Sets the priority code.
180 *
tom5f18cf32014-09-13 14:10:57 -0700181 * @param priority the priorityCode to set
alshabibc4901cd2014-09-05 16:50:40 -0700182 * @return the Ethernet frame
183 */
184 public Ethernet setPriorityCode(final byte priority) {
185 this.priorityCode = priority;
186 return this;
187 }
188
189 /**
190 * Gets the VLAN ID.
191 *
192 * @return the vlanID
193 */
194 public short getVlanID() {
195 return this.vlanID;
196 }
197
198 /**
199 * Sets the VLAN ID.
200 *
tom5f18cf32014-09-13 14:10:57 -0700201 * @param vlan the vlanID to set
alshabibc4901cd2014-09-05 16:50:40 -0700202 * @return the Ethernet frame
203 */
204 public Ethernet setVlanID(final short vlan) {
205 this.vlanID = vlan;
206 return this;
207 }
208
209 /**
210 * Gets the Ethernet type.
211 *
212 * @return the etherType
213 */
214 public short getEtherType() {
215 return this.etherType;
216 }
217
218 /**
219 * Sets the Ethernet type.
220 *
tom5f18cf32014-09-13 14:10:57 -0700221 * @param ethType the etherType to set
alshabibc4901cd2014-09-05 16:50:40 -0700222 * @return the Ethernet frame
223 */
224 public Ethernet setEtherType(final short ethType) {
225 this.etherType = ethType;
226 return this;
227 }
228
229 /**
230 * @return True if the Ethernet frame is broadcast, false otherwise
231 */
232 public boolean isBroadcast() {
233 assert this.destinationMACAddress.length() == 6;
234 return this.destinationMACAddress.isBroadcast();
235 }
236
237 /**
238 * @return True is the Ethernet frame is multicast, False otherwise
239 */
240 public boolean isMulticast() {
241 return this.destinationMACAddress.isMulticast();
242 }
243
244 /**
245 * Pad this packet to 60 bytes minimum, filling with zeros?
246 *
247 * @return the pad
248 */
249 public boolean isPad() {
250 return this.pad;
251 }
252
253 /**
254 * Pad this packet to 60 bytes minimum, filling with zeros?
255 *
tom5f18cf32014-09-13 14:10:57 -0700256 * @param pd
alshabibc4901cd2014-09-05 16:50:40 -0700257 * the pad to set
258 */
259 public Ethernet setPad(final boolean pd) {
260 this.pad = pd;
261 return this;
262 }
263
264 @Override
265 public byte[] serialize() {
266 byte[] payloadData = null;
267 if (this.payload != null) {
268 this.payload.setParent(this);
269 payloadData = this.payload.serialize();
270 }
271 int length = 14 + (this.vlanID == Ethernet.VLAN_UNTAGGED ? 0 : 4)
272 + (payloadData == null ? 0 : payloadData.length);
273 if (this.pad && length < 60) {
274 length = 60;
275 }
276 final byte[] data = new byte[length];
277 final ByteBuffer bb = ByteBuffer.wrap(data);
278 bb.put(this.destinationMACAddress.toBytes());
279 bb.put(this.sourceMACAddress.toBytes());
280 if (this.vlanID != Ethernet.VLAN_UNTAGGED) {
281 bb.putShort((short) 0x8100);
282 bb.putShort((short) (this.priorityCode << 13 | this.vlanID & 0x0fff));
283 }
284 bb.putShort(this.etherType);
285 if (payloadData != null) {
286 bb.put(payloadData);
287 }
288 if (this.pad) {
289 Arrays.fill(data, bb.position(), data.length, (byte) 0x0);
290 }
291 return data;
292 }
293
294 @Override
295 public IPacket deserialize(final byte[] data, final int offset,
296 final int length) {
297 if (length <= 0) {
298 return null;
299 }
300 final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
301 if (this.destinationMACAddress == null) {
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700302 this.destinationMACAddress = MacAddress.valueOf(new byte[6]);
alshabibc4901cd2014-09-05 16:50:40 -0700303 }
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700304 final byte[] dstAddr = new byte[MacAddress.MAC_ADDRESS_LENGTH];
alshabibc4901cd2014-09-05 16:50:40 -0700305 bb.get(dstAddr);
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700306 this.destinationMACAddress = MacAddress.valueOf(dstAddr);
alshabibc4901cd2014-09-05 16:50:40 -0700307
308 if (this.sourceMACAddress == null) {
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700309 this.sourceMACAddress = MacAddress.valueOf(new byte[6]);
alshabibc4901cd2014-09-05 16:50:40 -0700310 }
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700311 final byte[] srcAddr = new byte[MacAddress.MAC_ADDRESS_LENGTH];
alshabibc4901cd2014-09-05 16:50:40 -0700312 bb.get(srcAddr);
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700313 this.sourceMACAddress = MacAddress.valueOf(srcAddr);
alshabibc4901cd2014-09-05 16:50:40 -0700314
315 short ethType = bb.getShort();
316 if (ethType == (short) 0x8100) {
317 final short tci = bb.getShort();
318 this.priorityCode = (byte) (tci >> 13 & 0x07);
319 this.vlanID = (short) (tci & 0x0fff);
320 ethType = bb.getShort();
321 } else {
322 this.vlanID = Ethernet.VLAN_UNTAGGED;
323 }
324 this.etherType = ethType;
325
326 IPacket payload;
327 if (Ethernet.etherTypeClassMap.containsKey(this.etherType)) {
328 final Class<? extends IPacket> clazz = Ethernet.etherTypeClassMap
329 .get(this.etherType);
330 try {
331 payload = clazz.newInstance();
332 } catch (final Exception e) {
333 throw new RuntimeException(
334 "Error parsing payload for Ethernet packet", e);
335 }
336 } else {
337 payload = new Data();
338 }
339 this.payload = payload.deserialize(data, bb.position(),
340 bb.limit() - bb.position());
341 this.payload.setParent(this);
342 return this;
343 }
344
345 /**
346 * Checks to see if a string is a valid MAC address.
347 *
348 * @param macAddress
349 * @return True if macAddress is a valid MAC, False otherwise
350 */
351 public static boolean isMACAddress(final String macAddress) {
352 final String[] macBytes = macAddress.split(":");
353 if (macBytes.length != 6) {
354 return false;
355 }
356 for (int i = 0; i < 6; ++i) {
357 if (Ethernet.HEXES.indexOf(macBytes[i].toUpperCase().charAt(0)) == -1
358 || Ethernet.HEXES.indexOf(macBytes[i].toUpperCase().charAt(
359 1)) == -1) {
360 return false;
361 }
362 }
363 return true;
364 }
365
366 /**
367 * Accepts a MAC address of the form 00:aa:11:bb:22:cc, case does not
368 * matter, and returns a corresponding byte[].
369 *
370 * @param macAddress
371 * The MAC address to convert into a bye array
372 * @return The macAddress as a byte array
373 */
374 public static byte[] toMACAddress(final String macAddress) {
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700375 return MacAddress.valueOf(macAddress).toBytes();
alshabibc4901cd2014-09-05 16:50:40 -0700376 }
377
378 /**
379 * Accepts a MAC address and returns the corresponding long, where the MAC
380 * bytes are set on the lower order bytes of the long.
381 *
382 * @param macAddress
383 * @return a long containing the mac address bytes
384 */
385 public static long toLong(final byte[] macAddress) {
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700386 return MacAddress.valueOf(macAddress).toLong();
alshabibc4901cd2014-09-05 16:50:40 -0700387 }
388
389 /**
390 * Converts a long MAC address to a byte array.
391 *
392 * @param macAddress
393 * @return the bytes of the mac address
394 */
395 public static byte[] toByteArray(final long macAddress) {
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700396 return MacAddress.valueOf(macAddress).toBytes();
alshabibc4901cd2014-09-05 16:50:40 -0700397 }
398
399 /*
400 * (non-Javadoc)
401 *
402 * @see java.lang.Object#hashCode()
403 */
404 @Override
405 public int hashCode() {
406 final int prime = 7867;
407 int result = super.hashCode();
408 result = prime * result + this.destinationMACAddress.hashCode();
409 result = prime * result + this.etherType;
410 result = prime * result + this.vlanID;
411 result = prime * result + this.priorityCode;
412 result = prime * result + (this.pad ? 1231 : 1237);
413 result = prime * result + this.sourceMACAddress.hashCode();
414 return result;
415 }
416
417 /*
418 * (non-Javadoc)
419 *
420 * @see java.lang.Object#equals(java.lang.Object)
421 */
422 @Override
423 public boolean equals(final Object obj) {
424 if (this == obj) {
425 return true;
426 }
427 if (!super.equals(obj)) {
428 return false;
429 }
430 if (!(obj instanceof Ethernet)) {
431 return false;
432 }
433 final Ethernet other = (Ethernet) obj;
434 if (!this.destinationMACAddress.equals(other.destinationMACAddress)) {
435 return false;
436 }
437 if (this.priorityCode != other.priorityCode) {
438 return false;
439 }
440 if (this.vlanID != other.vlanID) {
441 return false;
442 }
443 if (this.etherType != other.etherType) {
444 return false;
445 }
446 if (this.pad != other.pad) {
447 return false;
448 }
449 if (!this.sourceMACAddress.equals(other.sourceMACAddress)) {
450 return false;
451 }
452 return true;
453 }
454
455 /*
456 * (non-Javadoc)
457 *
458 * @see java.lang.Object#toString(java.lang.Object)
459 */
460 @Override
461 public String toString() {
462
463 final StringBuffer sb = new StringBuffer("\n");
464
465 final IPacket pkt = this.getPayload();
466
467 if (pkt instanceof ARP) {
468 sb.append("arp");
469 } else if (pkt instanceof LLDP) {
470 sb.append("lldp");
471 } else if (pkt instanceof ICMP) {
472 sb.append("icmp");
473 } else if (pkt instanceof IPv4) {
474 sb.append("ip");
475 } else if (pkt instanceof DHCP) {
476 sb.append("dhcp");
477 } else {
478 sb.append(this.getEtherType());
479 }
480
481 sb.append("\ndl_vlan: ");
482 if (this.getVlanID() == Ethernet.VLAN_UNTAGGED) {
483 sb.append("untagged");
484 } else {
485 sb.append(this.getVlanID());
486 }
487 sb.append("\ndl_vlan_pcp: ");
488 sb.append(this.getPriorityCode());
489 sb.append("\ndl_src: ");
490 sb.append(bytesToHex(this.getSourceMACAddress()));
491 sb.append("\ndl_dst: ");
492 sb.append(bytesToHex(this.getDestinationMACAddress()));
493
494 if (pkt instanceof ARP) {
495 final ARP p = (ARP) pkt;
496 sb.append("\nnw_src: ");
497 sb.append(IPv4.fromIPv4Address(IPv4.toIPv4Address(p
498 .getSenderProtocolAddress())));
499 sb.append("\nnw_dst: ");
500 sb.append(IPv4.fromIPv4Address(IPv4.toIPv4Address(p
501 .getTargetProtocolAddress())));
502 } else if (pkt instanceof LLDP) {
503 sb.append("lldp packet");
504 } else if (pkt instanceof ICMP) {
505 final ICMP icmp = (ICMP) pkt;
506 sb.append("\nicmp_type: ");
507 sb.append(icmp.getIcmpType());
508 sb.append("\nicmp_code: ");
509 sb.append(icmp.getIcmpCode());
510 } else if (pkt instanceof IPv4) {
511 final IPv4 p = (IPv4) pkt;
512 sb.append("\nnw_src: ");
513 sb.append(IPv4.fromIPv4Address(p.getSourceAddress()));
514 sb.append("\nnw_dst: ");
515 sb.append(IPv4.fromIPv4Address(p.getDestinationAddress()));
516 sb.append("\nnw_tos: ");
517 sb.append(p.getDiffServ());
518 sb.append("\nnw_proto: ");
519 sb.append(p.getProtocol());
520
521 if (pkt instanceof TCP) {
522 sb.append("\ntp_src: ");
523 sb.append(((TCP) pkt).getSourcePort());
524 sb.append("\ntp_dst: ");
525 sb.append(((TCP) pkt).getDestinationPort());
526
527 } else if (pkt instanceof UDP) {
528 sb.append("\ntp_src: ");
529 sb.append(((UDP) pkt).getSourcePort());
530 sb.append("\ntp_dst: ");
531 sb.append(((UDP) pkt).getDestinationPort());
532 }
533
534 if (pkt instanceof ICMP) {
535 final ICMP icmp = (ICMP) pkt;
536 sb.append("\nicmp_type: ");
537 sb.append(icmp.getIcmpType());
538 sb.append("\nicmp_code: ");
539 sb.append(icmp.getIcmpCode());
540 }
541
542 } else if (pkt instanceof DHCP) {
543 sb.append("\ndhcp packet");
544 } else if (pkt instanceof Data) {
545 sb.append("\ndata packet");
546 } else if (pkt instanceof LLC) {
547 sb.append("\nllc packet");
548 } else {
tom5f18cf32014-09-13 14:10:57 -0700549 sb.append("\nunknown packet");
alshabibc4901cd2014-09-05 16:50:40 -0700550 }
551
552 return sb.toString();
553 }
554
555 public static String bytesToHex(byte[] in) {
556 final StringBuilder builder = new StringBuilder();
557 for (byte b : in) {
558 builder.append(String.format("%02x", b));
559 }
560 return builder.toString();
561 }
562
563}