blob: 93ae2a639a4efede3989bee4ee614a2fd4b0857b [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001/**
Ray Milkey269ffb92014-04-03 14:43:30 -07002 * Copyright 2011, Big Switch Networks, Inc.
3 * Originally created by David Erickson, Stanford University
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may
6 * not use this file except in compliance with the License. You may obtain
7 * a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 * License for the specific language governing permissions and limitations
15 * under the License.
16 **/
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080017
Jonathan Hartdeda0ba2014-04-03 11:14:12 -070018package net.onrc.onos.core.packet;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080019
20import java.nio.ByteBuffer;
21import java.util.Arrays;
22import java.util.HashMap;
23import java.util.Map;
24
25import net.floodlightcontroller.util.MACAddress;
Jonathan Harta99ec672014-04-03 11:30:34 -070026
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080027import org.openflow.util.HexString;
28
29/**
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080030 * @author David Erickson (daviderickson@cs.stanford.edu)
31 */
32public class Ethernet extends BasePacket {
33 private static String HEXES = "0123456789ABCDEF";
34 public static final short TYPE_ARP = 0x0806;
35 public static final short TYPE_RARP = (short) 0x8035;
Ray Milkey5c9f2db2014-04-09 10:31:21 -070036 public static final short TYPE_IPV4 = 0x0800;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080037 public static final short TYPE_LLDP = (short) 0x88cc;
38 public static final short TYPE_BSN = (short) 0x8942;
Ray Milkey269ffb92014-04-03 14:43:30 -070039 public static final short VLAN_UNTAGGED = (short) 0xffff;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080040 public static final short DATALAYER_ADDRESS_LENGTH = 6; // bytes
Pavlin Radoslavov608fac32014-04-09 12:40:24 -070041 public static final Map<Short, Class<? extends IPacket>> ETHER_TYPE_CLASS_MAP;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080042
43 static {
Pavlin Radoslavov608fac32014-04-09 12:40:24 -070044 ETHER_TYPE_CLASS_MAP = new HashMap<Short, Class<? extends IPacket>>();
45 ETHER_TYPE_CLASS_MAP.put(TYPE_ARP, ARP.class);
46 ETHER_TYPE_CLASS_MAP.put(TYPE_RARP, ARP.class);
47 ETHER_TYPE_CLASS_MAP.put(TYPE_IPV4, IPv4.class);
48 ETHER_TYPE_CLASS_MAP.put(TYPE_LLDP, LLDP.class);
49 ETHER_TYPE_CLASS_MAP.put(TYPE_BSN, BSN.class);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080050 }
51
52 protected MACAddress destinationMACAddress;
53 protected MACAddress sourceMACAddress;
54 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 = VLAN_UNTAGGED;
65 }
Ray Milkey269ffb92014-04-03 14:43:30 -070066
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080067 /**
68 * @return the destination MAC as a byte array
69 */
70 public byte[] getDestinationMACAddress() {
71 return destinationMACAddress.toBytes();
72 }
Ray Milkey269ffb92014-04-03 14:43:30 -070073
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080074 /**
75 * @return the destination MAC
76 */
77 public MACAddress getDestinationMAC() {
78 return destinationMACAddress;
79 }
80
81 /**
82 * @param destinationMACAddress the destination MAC to set
83 */
84 public Ethernet setDestinationMACAddress(byte[] destinationMACAddress) {
85 this.destinationMACAddress = MACAddress.valueOf(destinationMACAddress);
86 return this;
87 }
88
89 /**
90 * @param destinationMACAddress the destination MAC to set
91 */
92 public Ethernet setDestinationMACAddress(String destinationMACAddress) {
93 this.destinationMACAddress = MACAddress.valueOf(destinationMACAddress);
94 return this;
95 }
96
97 /**
98 * @return the source MACAddress as a byte array
99 */
100 public byte[] getSourceMACAddress() {
101 return sourceMACAddress.toBytes();
102 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700103
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800104 /**
105 * @return the source MACAddress
106 */
107 public MACAddress getSourceMAC() {
108 return sourceMACAddress;
109 }
110
111 /**
112 * @param sourceMACAddress the source MAC to set
113 */
114 public Ethernet setSourceMACAddress(byte[] sourceMACAddress) {
115 this.sourceMACAddress = MACAddress.valueOf(sourceMACAddress);
116 return this;
117 }
118
119 /**
120 * @param sourceMACAddress the source MAC to set
121 */
122 public Ethernet setSourceMACAddress(String sourceMACAddress) {
123 this.sourceMACAddress = MACAddress.valueOf(sourceMACAddress);
124 return this;
125 }
126
127 /**
128 * @return the priorityCode
129 */
130 public byte getPriorityCode() {
131 return priorityCode;
132 }
133
134 /**
135 * @param priorityCode the priorityCode to set
136 */
137 public Ethernet setPriorityCode(byte priorityCode) {
138 this.priorityCode = priorityCode;
139 return this;
140 }
141
142 /**
143 * @return the vlanID
144 */
145 public short getVlanID() {
146 return vlanID;
147 }
148
149 /**
150 * @param vlanID the vlanID to set
151 */
152 public Ethernet setVlanID(short vlanID) {
153 this.vlanID = vlanID;
154 return this;
155 }
156
157 /**
158 * @return the etherType
159 */
160 public short getEtherType() {
161 return etherType;
162 }
163
164 /**
165 * @param etherType the etherType to set
166 */
167 public Ethernet setEtherType(short etherType) {
168 this.etherType = etherType;
169 return this;
170 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700171
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800172 /**
173 * @return True if the Ethernet frame is broadcast, false otherwise
174 */
175 public boolean isBroadcast() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700176 assert (destinationMACAddress.length() == 6);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800177 return destinationMACAddress.isBroadcast();
178 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700179
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800180 /**
181 * @return True is the Ethernet frame is multicast, False otherwise
182 */
183 public boolean isMulticast() {
184 return destinationMACAddress.isMulticast();
185 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700186
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800187 /**
188 * Pad this packet to 60 bytes minimum, filling with zeros?
Ray Milkey269ffb92014-04-03 14:43:30 -0700189 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800190 * @return the pad
191 */
192 public boolean isPad() {
193 return pad;
194 }
195
196 /**
197 * Pad this packet to 60 bytes minimum, filling with zeros?
Ray Milkey269ffb92014-04-03 14:43:30 -0700198 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800199 * @param pad the pad to set
200 */
201 public Ethernet setPad(boolean pad) {
202 this.pad = pad;
203 return this;
204 }
205
206 public byte[] serialize() {
207 byte[] payloadData = null;
208 if (payload != null) {
209 payload.setParent(this);
210 payloadData = payload.serialize();
211 }
212 int length = 14 + ((vlanID == VLAN_UNTAGGED) ? 0 : 4) +
Ray Milkey269ffb92014-04-03 14:43:30 -0700213 ((payloadData == null) ? 0 : payloadData.length);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800214 if (pad && length < 60) {
215 length = 60;
216 }
217 byte[] data = new byte[length];
218 ByteBuffer bb = ByteBuffer.wrap(data);
219 bb.put(destinationMACAddress.toBytes());
220 bb.put(sourceMACAddress.toBytes());
221 if (vlanID != VLAN_UNTAGGED) {
222 bb.putShort((short) 0x8100);
223 bb.putShort((short) ((priorityCode << 13) | (vlanID & 0x0fff)));
224 }
225 bb.putShort(etherType);
226 if (payloadData != null)
227 bb.put(payloadData);
228 if (pad) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700229 Arrays.fill(data, bb.position(), data.length, (byte) 0x0);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800230 }
231 return data;
232 }
233
234 @Override
235 public IPacket deserialize(byte[] data, int offset, int length) {
236 if (length <= 0)
237 return null;
238 ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
239 if (this.destinationMACAddress == null)
240 this.destinationMACAddress = MACAddress.valueOf(new byte[6]);
241 byte[] dstAddr = new byte[MACAddress.MAC_ADDRESS_LENGTH];
242 bb.get(dstAddr);
243 this.destinationMACAddress = MACAddress.valueOf(dstAddr);
244
245 if (this.sourceMACAddress == null)
246 this.sourceMACAddress = MACAddress.valueOf(new byte[6]);
247 byte[] srcAddr = new byte[MACAddress.MAC_ADDRESS_LENGTH];
248 bb.get(srcAddr);
249 this.sourceMACAddress = MACAddress.valueOf(srcAddr);
250
251 short etherType = bb.getShort();
252 if (etherType == (short) 0x8100) {
253 short tci = bb.getShort();
254 this.priorityCode = (byte) ((tci >> 13) & 0x07);
255 this.vlanID = (short) (tci & 0x0fff);
256 etherType = bb.getShort();
257 } else {
258 this.vlanID = VLAN_UNTAGGED;
259 }
260 this.etherType = etherType;
Ray Milkey269ffb92014-04-03 14:43:30 -0700261
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800262 IPacket payload;
Pavlin Radoslavov608fac32014-04-09 12:40:24 -0700263 if (Ethernet.ETHER_TYPE_CLASS_MAP.containsKey(this.etherType)) {
264 Class<? extends IPacket> clazz = Ethernet.ETHER_TYPE_CLASS_MAP.get(this.etherType);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800265 try {
266 payload = clazz.newInstance();
267 } catch (Exception e) {
268 throw new RuntimeException("Error parsing payload for Ethernet packet", e);
269 }
270 } else {
271 payload = new Data();
272 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700273 this.payload = payload.deserialize(data, bb.position(), bb.limit() - bb.position());
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800274 this.payload.setParent(this);
275 return this;
276 }
277
278 /**
279 * Checks to see if a string is a valid MAC address.
Ray Milkey269ffb92014-04-03 14:43:30 -0700280 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800281 * @param macAddress
282 * @return True if macAddress is a valid MAC, False otherwise
283 */
284 public static boolean isMACAddress(String macAddress) {
285 String[] macBytes = macAddress.split(":");
286 if (macBytes.length != 6)
287 return false;
288 for (int i = 0; i < 6; ++i) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700289 if (HEXES.indexOf(macBytes[i].toUpperCase().charAt(0)) == -1 ||
290 HEXES.indexOf(macBytes[i].toUpperCase().charAt(1)) == -1) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800291 return false;
292 }
293 }
294 return true;
295 }
296
297 /**
298 * Accepts a MAC address of the form 00:aa:11:bb:22:cc, case does not
299 * matter, and returns a corresponding byte[].
Ray Milkey269ffb92014-04-03 14:43:30 -0700300 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800301 * @param macAddress The MAC address to convert into a bye array
Ray Milkey269ffb92014-04-03 14:43:30 -0700302 * @return The macAddress as a byte array
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800303 */
304 public static byte[] toMACAddress(String macAddress) {
305 return MACAddress.valueOf(macAddress).toBytes();
306 }
307
308
309 /**
310 * Accepts a MAC address and returns the corresponding long, where the
311 * MAC bytes are set on the lower order bytes of the long.
Ray Milkey269ffb92014-04-03 14:43:30 -0700312 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800313 * @param macAddress
314 * @return a long containing the mac address bytes
315 */
316 public static long toLong(byte[] macAddress) {
317 return MACAddress.valueOf(macAddress).toLong();
318 }
319
320 /**
321 * Convert a long MAC address to a byte array
Ray Milkey269ffb92014-04-03 14:43:30 -0700322 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800323 * @param macAddress
324 * @return the bytes of the mac address
325 */
326 public static byte[] toByteArray(long macAddress) {
327 return MACAddress.valueOf(macAddress).toBytes();
328 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700329
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800330 /* (non-Javadoc)
331 * @see java.lang.Object#hashCode()
332 */
333 @Override
334 public int hashCode() {
335 final int prime = 7867;
336 int result = super.hashCode();
337 result = prime * result + destinationMACAddress.hashCode();
338 result = prime * result + etherType;
339 result = prime * result + vlanID;
340 result = prime * result + priorityCode;
341 result = prime * result + (pad ? 1231 : 1237);
342 result = prime * result + sourceMACAddress.hashCode();
343 return result;
344 }
345
346 /* (non-Javadoc)
347 * @see java.lang.Object#equals(java.lang.Object)
348 */
349 @Override
350 public boolean equals(Object obj) {
351 if (this == obj)
352 return true;
353 if (!super.equals(obj))
354 return false;
355 if (!(obj instanceof Ethernet))
356 return false;
357 Ethernet other = (Ethernet) obj;
358 if (!destinationMACAddress.equals(other.destinationMACAddress))
359 return false;
360 if (priorityCode != other.priorityCode)
361 return false;
362 if (vlanID != other.vlanID)
363 return false;
364 if (etherType != other.etherType)
365 return false;
366 if (pad != other.pad)
367 return false;
368 if (!sourceMACAddress.equals(other.sourceMACAddress))
369 return false;
370 return true;
371 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700372
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800373 /* (non-Javadoc)
374 * @see java.lang.Object#toString(java.lang.Object)
375 */
376 @Override
377 public String toString() {
378
379 StringBuffer sb = new StringBuffer("\n");
380
381 IPacket pkt = (IPacket) this.getPayload();
382
383 if (pkt instanceof ARP)
384 sb.append("arp");
385 else if (pkt instanceof LLDP)
386 sb.append("lldp");
387 else if (pkt instanceof ICMP)
388 sb.append("icmp");
389 else if (pkt instanceof IPv4)
390 sb.append("ip");
391 else if (pkt instanceof DHCP)
392 sb.append("dhcp");
Ray Milkey269ffb92014-04-03 14:43:30 -0700393 else sb.append(this.getEtherType());
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800394
395 sb.append("\ndl_vlan: ");
396 if (this.getVlanID() == Ethernet.VLAN_UNTAGGED)
397 sb.append("untagged");
398 else
399 sb.append(this.getVlanID());
400 sb.append("\ndl_vlan_pcp: ");
401 sb.append(this.getPriorityCode());
402 sb.append("\ndl_src: ");
403 sb.append(HexString.toHexString(this.getSourceMACAddress()));
404 sb.append("\ndl_dst: ");
405 sb.append(HexString.toHexString(this.getDestinationMACAddress()));
406
407
408 if (pkt instanceof ARP) {
409 ARP p = (ARP) pkt;
410 sb.append("\nnw_src: ");
411 sb.append(IPv4.fromIPv4Address(IPv4.toIPv4Address(p.getSenderProtocolAddress())));
412 sb.append("\nnw_dst: ");
413 sb.append(IPv4.fromIPv4Address(IPv4.toIPv4Address(p.getTargetProtocolAddress())));
Ray Milkey269ffb92014-04-03 14:43:30 -0700414 } else if (pkt instanceof LLDP) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800415 sb.append("lldp packet");
Ray Milkey269ffb92014-04-03 14:43:30 -0700416 } else if (pkt instanceof ICMP) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800417 ICMP icmp = (ICMP) pkt;
418 sb.append("\nicmp_type: ");
419 sb.append(icmp.getIcmpType());
420 sb.append("\nicmp_code: ");
421 sb.append(icmp.getIcmpCode());
Ray Milkey269ffb92014-04-03 14:43:30 -0700422 } else if (pkt instanceof IPv4) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800423 IPv4 p = (IPv4) pkt;
424 sb.append("\nnw_src: ");
425 sb.append(IPv4.fromIPv4Address(p.getSourceAddress()));
426 sb.append("\nnw_dst: ");
427 sb.append(IPv4.fromIPv4Address(p.getDestinationAddress()));
428 sb.append("\nnw_tos: ");
429 sb.append(p.getDiffServ());
430 sb.append("\nnw_proto: ");
431 sb.append(p.getProtocol());
432
433 if (pkt instanceof TCP) {
434 sb.append("\ntp_src: ");
435 sb.append(((TCP) pkt).getSourcePort());
436 sb.append("\ntp_dst: ");
437 sb.append(((TCP) pkt).getDestinationPort());
438
439 } else if (pkt instanceof UDP) {
440 sb.append("\ntp_src: ");
441 sb.append(((UDP) pkt).getSourcePort());
442 sb.append("\ntp_dst: ");
443 sb.append(((UDP) pkt).getDestinationPort());
444 }
445
446 if (pkt instanceof ICMP) {
447 ICMP icmp = (ICMP) pkt;
448 sb.append("\nicmp_type: ");
449 sb.append(icmp.getIcmpType());
450 sb.append("\nicmp_code: ");
451 sb.append(icmp.getIcmpCode());
452 }
453
Ray Milkey269ffb92014-04-03 14:43:30 -0700454 } else if (pkt instanceof DHCP) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800455 sb.append("\ndhcp packet");
Ray Milkey269ffb92014-04-03 14:43:30 -0700456 } else if (pkt instanceof Data) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800457 sb.append("\ndata packet");
Ray Milkey269ffb92014-04-03 14:43:30 -0700458 } else if (pkt instanceof LLC) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800459 sb.append("\nllc packet");
Ray Milkey269ffb92014-04-03 14:43:30 -0700460 } else if (pkt instanceof BPDU) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800461 sb.append("\nbpdu packet");
Ray Milkey269ffb92014-04-03 14:43:30 -0700462 } else sb.append("\nunknwon packet");
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800463
464 return sb.toString();
465 }
466
467}