blob: c455d598c9ce4fb8c1bbfba267e5eab4b534aa5b [file] [log] [blame]
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +08003 *
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
7 *
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.
15 */
16
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080017package org.onlab.packet;
18
Charles M.C. Chan94f37372015-01-10 17:53:42 +080019import org.onlab.packet.ipv6.Authentication;
20import org.onlab.packet.ipv6.DestinationOptions;
21import org.onlab.packet.ipv6.EncapSecurityPayload;
22import org.onlab.packet.ipv6.Fragment;
Charles M.C. Chan94f37372015-01-10 17:53:42 +080023import org.onlab.packet.ipv6.HopByHopOptions;
Jonathan Hart2a655752015-04-07 16:46:33 -070024import org.onlab.packet.ipv6.IExtensionHeader;
Charles M.C. Chan94f37372015-01-10 17:53:42 +080025import org.onlab.packet.ipv6.Routing;
Jonathan Hart2a655752015-04-07 16:46:33 -070026
Yuta HIGUCHIbfc2e922017-06-07 21:46:05 -070027import com.google.common.collect.ImmutableMap;
28
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080029import java.nio.ByteBuffer;
30import java.util.Arrays;
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080031import java.util.Map;
32
Jian Li5fc14292015-12-04 11:30:46 -080033import static com.google.common.base.MoreObjects.toStringHelper;
Pier Luigi37b687b2017-01-16 22:46:33 -080034import static com.google.common.base.Preconditions.checkArgument;
Jonathan Hart2a655752015-04-07 16:46:33 -070035import static org.onlab.packet.PacketUtils.checkInput;
36
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080037/**
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080038 * Implements IPv6 packet format. (RFC 2460)
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080039 */
Jian Li1270aea2016-09-15 13:32:24 +090040public class IPv6 extends IP implements IExtensionHeader {
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080041 public static final byte FIXED_HEADER_LENGTH = 40; // bytes
42
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080043 public static final byte PROTOCOL_TCP = 0x6;
44 public static final byte PROTOCOL_UDP = 0x11;
45 public static final byte PROTOCOL_ICMP6 = 0x3A;
Charles M.C. Chan94f37372015-01-10 17:53:42 +080046 public static final byte PROTOCOL_HOPOPT = 0x00;
47 public static final byte PROTOCOL_ROUTING = 0x2B;
48 public static final byte PROTOCOL_FRAG = 0x2C;
49 public static final byte PROTOCOL_ESP = 0x32;
50 public static final byte PROTOCOL_AH = 0x33;
51 public static final byte PROTOCOL_DSTOPT = 0x3C;
52
Pier Luigi37b687b2017-01-16 22:46:33 -080053 public static final byte LINK_LOCAL_0 = (byte) 0xfe;
54 public static final byte LINK_LOCAL_1 = (byte) 0x80;
Charles M.C. Chan94f37372015-01-10 17:53:42 +080055
Jonathan Hart2a655752015-04-07 16:46:33 -070056 public static final Map<Byte, Deserializer<? extends IPacket>> PROTOCOL_DESERIALIZER_MAP =
Yuta HIGUCHIbfc2e922017-06-07 21:46:05 -070057 ImmutableMap.<Byte, Deserializer<? extends IPacket>>builder()
58 .put(IPv6.PROTOCOL_ICMP6, ICMP6.deserializer())
59 .put(IPv6.PROTOCOL_TCP, TCP.deserializer())
60 .put(IPv6.PROTOCOL_UDP, UDP.deserializer())
61 .put(IPv6.PROTOCOL_HOPOPT, HopByHopOptions.deserializer())
62 .put(IPv6.PROTOCOL_ROUTING, Routing.deserializer())
63 .put(IPv6.PROTOCOL_FRAG, Fragment.deserializer())
64 .put(IPv6.PROTOCOL_ESP, EncapSecurityPayload.deserializer())
65 .put(IPv6.PROTOCOL_AH, Authentication.deserializer())
66 .put(IPv6.PROTOCOL_DSTOPT, DestinationOptions.deserializer())
67 .build();
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080068
69 protected byte version;
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080070 protected byte trafficClass;
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080071 protected int flowLabel;
72 protected short payloadLength;
73 protected byte nextHeader;
74 protected byte hopLimit;
75 protected byte[] sourceAddress = new byte[Ip6Address.BYTE_LENGTH];
76 protected byte[] destinationAddress = new byte[Ip6Address.BYTE_LENGTH];
77
78 /**
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080079 * Default constructor that sets the version to 6.
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080080 */
81 public IPv6() {
82 super();
83 this.version = 6;
84 }
85
Jian Li1270aea2016-09-15 13:32:24 +090086 @Override
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080087 public byte getVersion() {
88 return this.version;
89 }
90
Jian Li1270aea2016-09-15 13:32:24 +090091 @Override
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080092 public IPv6 setVersion(final byte version) {
93 this.version = version;
94 return this;
95 }
96
97 /**
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080098 * Gets traffic class.
99 *
100 * @return the traffic class
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800101 */
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800102 public byte getTrafficClass() {
103 return this.trafficClass;
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800104 }
105
106 /**
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800107 * Sets traffic class.
108 *
109 * @param trafficClass the traffic class to set
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800110 * @return this
111 */
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800112 public IPv6 setTrafficClass(final byte trafficClass) {
113 this.trafficClass = trafficClass;
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800114 return this;
115 }
116
117 /**
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800118 * Gets flow label.
119 *
120 * @return the flow label
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800121 */
122 public int getFlowLabel() {
123 return this.flowLabel;
124 }
125
126 /**
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800127 * Sets flow label.
128 *
129 * @param flowLabel the flow label to set
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800130 * @return this
131 */
132 public IPv6 setFlowLabel(final int flowLabel) {
133 this.flowLabel = flowLabel;
134 return this;
135 }
136
Charles M.C. Chan94f37372015-01-10 17:53:42 +0800137 @Override
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800138 public byte getNextHeader() {
139 return this.nextHeader;
140 }
141
Charles M.C. Chan94f37372015-01-10 17:53:42 +0800142 @Override
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800143 public IPv6 setNextHeader(final byte nextHeader) {
144 this.nextHeader = nextHeader;
145 return this;
146 }
147
148 /**
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800149 * Gets hop limit.
150 *
151 * @return the hop limit
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800152 */
153 public byte getHopLimit() {
154 return this.hopLimit;
155 }
156
157 /**
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800158 * Sets hop limit.
159 *
160 * @param hopLimit the hop limit to set
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800161 * @return this
162 */
163 public IPv6 setHopLimit(final byte hopLimit) {
164 this.hopLimit = hopLimit;
165 return this;
166 }
167
168 /**
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800169 * Gets source address.
170 *
171 * @return the IPv6 source address
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800172 */
173 public byte[] getSourceAddress() {
174 return this.sourceAddress;
175 }
176
177 /**
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800178 * Sets source address.
179 *
180 * @param sourceAddress the IPv6 source address to set
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800181 * @return this
182 */
183 public IPv6 setSourceAddress(final byte[] sourceAddress) {
184 this.sourceAddress = Arrays.copyOfRange(sourceAddress, 0, Ip6Address.BYTE_LENGTH);
185 return this;
186 }
187
188 /**
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800189 * Gets destination address.
190 *
191 * @return the IPv6 destination address
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800192 */
193 public byte[] getDestinationAddress() {
194 return this.destinationAddress;
195 }
196
197 /**
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800198 * Sets destination address.
199 *
200 * @param destinationAddress the IPv6 destination address to set
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800201 * @return this
202 */
203 public IPv6 setDestinationAddress(final byte[] destinationAddress) {
204 this.destinationAddress = Arrays.copyOfRange(destinationAddress, 0, Ip6Address.BYTE_LENGTH);
205 return this;
206 }
207
208 @Override
209 public byte[] serialize() {
210 byte[] payloadData = null;
211 if (this.payload != null) {
212 this.payload.setParent(this);
213 payloadData = this.payload.serialize();
214 }
215
Charles M.C. Chan5c0b4762015-01-10 18:38:37 +0800216 this.payloadLength = 0;
217 if (payloadData != null) {
218 this.payloadLength = (short) payloadData.length;
219 }
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800220
221 final byte[] data = new byte[FIXED_HEADER_LENGTH + payloadLength];
222 final ByteBuffer bb = ByteBuffer.wrap(data);
223
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800224 bb.putInt((this.version & 0xf) << 28 | (this.trafficClass & 0xff) << 20 | this.flowLabel & 0xfffff);
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800225 bb.putShort(this.payloadLength);
226 bb.put(this.nextHeader);
227 bb.put(this.hopLimit);
228 bb.put(this.sourceAddress, 0, Ip6Address.BYTE_LENGTH);
229 bb.put(this.destinationAddress, 0, Ip6Address.BYTE_LENGTH);
230
231 if (payloadData != null) {
232 bb.put(payloadData);
233 }
234
235 return data;
236 }
237
238 @Override
239 public IPacket deserialize(final byte[] data, final int offset,
240 final int length) {
241 final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
242 int iscratch;
243
244 iscratch = bb.getInt();
245 this.version = (byte) (iscratch >> 28 & 0xf);
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800246 this.trafficClass = (byte) (iscratch >> 20 & 0xff);
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800247 this.flowLabel = iscratch & 0xfffff;
248 this.payloadLength = bb.getShort();
249 this.nextHeader = bb.get();
250 this.hopLimit = bb.get();
251 bb.get(this.sourceAddress, 0, Ip6Address.BYTE_LENGTH);
252 bb.get(this.destinationAddress, 0, Ip6Address.BYTE_LENGTH);
253
Jonathan Hart2a655752015-04-07 16:46:33 -0700254 Deserializer<? extends IPacket> deserializer;
255 if (IPv6.PROTOCOL_DESERIALIZER_MAP.containsKey(this.nextHeader)) {
256 deserializer = IPv6.PROTOCOL_DESERIALIZER_MAP.get(this.nextHeader);
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800257 } else {
Jonathan Hart2a655752015-04-07 16:46:33 -0700258 deserializer = Data.deserializer();
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800259 }
Jonathan Hart2a655752015-04-07 16:46:33 -0700260 try {
261 this.payload = deserializer.deserialize(data, bb.position(),
262 bb.limit() - bb.position());
263 this.payload.setParent(this);
264 } catch (DeserializationException e) {
265 return this;
266 }
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800267
268 return this;
269 }
270
271 /*
272 * (non-Javadoc)
273 *
274 * @see java.lang.Object#hashCode()
275 */
276 @Override
277 public int hashCode() {
278 final int prime = 2521;
279 int result = super.hashCode();
280 ByteBuffer bb;
281 bb = ByteBuffer.wrap(this.destinationAddress);
282 for (int i = 0; i < 4; i++) {
283 result = prime * result + bb.getInt();
284 }
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800285 result = prime * result + this.trafficClass;
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800286 result = prime * result + this.flowLabel;
287 result = prime * result + this.hopLimit;
288 result = prime * result + this.nextHeader;
289 result = prime * result + this.payloadLength;
290 bb = ByteBuffer.wrap(this.sourceAddress);
291 for (int i = 0; i < 4; i++) {
292 result = prime * result + bb.getInt();
293 }
294 result = prime * result + this.version;
295 return result;
296 }
297
298 /*
299 * (non-Javadoc)
300 *
301 * @see java.lang.Object#equals(java.lang.Object)
302 */
303 @Override
304 public boolean equals(final Object obj) {
305 if (this == obj) {
306 return true;
307 }
308 if (!super.equals(obj)) {
309 return false;
310 }
311 if (!(obj instanceof IPv6)) {
312 return false;
313 }
314 final IPv6 other = (IPv6) obj;
315 if (!Arrays.equals(this.destinationAddress, other.destinationAddress)) {
316 return false;
317 }
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800318 if (this.trafficClass != other.trafficClass) {
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800319 return false;
320 }
321 if (this.flowLabel != other.flowLabel) {
322 return false;
323 }
324 if (this.hopLimit != other.hopLimit) {
325 return false;
326 }
327 if (this.nextHeader != other.nextHeader) {
328 return false;
329 }
330 if (this.payloadLength != other.payloadLength) {
331 return false;
332 }
333 if (!Arrays.equals(this.sourceAddress, other.sourceAddress)) {
334 return false;
335 }
336 return true;
337 }
Jonathan Hart2a655752015-04-07 16:46:33 -0700338
339 /**
340 * Deserializer function for IPv6 packets.
341 *
342 * @return deserializer function
343 */
344 public static Deserializer<IPv6> deserializer() {
345 return (data, offset, length) -> {
346 checkInput(data, offset, length, FIXED_HEADER_LENGTH);
347
348 IPv6 ipv6 = new IPv6();
349
350 ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
351
352 int iscratch = bb.getInt();
353
354 ipv6.version = (byte) (iscratch >> 28 & 0xf);
355 ipv6.trafficClass = (byte) (iscratch >> 20 & 0xff);
356 ipv6.flowLabel = iscratch & 0xfffff;
357 ipv6.payloadLength = bb.getShort();
358 ipv6.nextHeader = bb.get();
359 ipv6.hopLimit = bb.get();
360 bb.get(ipv6.sourceAddress, 0, Ip6Address.BYTE_LENGTH);
361 bb.get(ipv6.destinationAddress, 0, Ip6Address.BYTE_LENGTH);
362
363 Deserializer<? extends IPacket> deserializer;
364 if (IPv6.PROTOCOL_DESERIALIZER_MAP.containsKey(ipv6.nextHeader)) {
365 deserializer = IPv6.PROTOCOL_DESERIALIZER_MAP.get(ipv6.nextHeader);
366 } else {
367 deserializer = Data.deserializer();
368 }
Charles Chan89c4a642017-04-13 17:43:52 -0700369
370 int remainingLength = bb.limit() - bb.position();
371 int payloadLength = ipv6.payloadLength;
372 int bytesToRead = (payloadLength <= remainingLength) ?
373 payloadLength : remainingLength;
374 ipv6.payload = deserializer.deserialize(data, bb.position(), bytesToRead);
Jonathan Hart2a655752015-04-07 16:46:33 -0700375 ipv6.payload.setParent(ipv6);
376
377 return ipv6;
378 };
379 }
Jian Li5fc14292015-12-04 11:30:46 -0800380
381 @Override
382 public String toString() {
383 return toStringHelper(getClass())
384 .add("version", Byte.toString(version))
385 .add("trafficClass", Byte.toString(trafficClass))
386 .add("flowLabel", Integer.toString(flowLabel))
387 .add("payloadLength", Short.toString(payloadLength))
388 .add("nextHeader", Byte.toString(nextHeader))
389 .add("hopLimit", Byte.toString(hopLimit))
390 .add("sourceAddress", Arrays.toString(sourceAddress))
391 .add("destinationAddress", Arrays.toString(destinationAddress))
392 .toString();
393 }
Pier Ventre78e73f62016-12-02 19:59:28 -0800394
395 /**
396 * According to the RFC 4291, the solicitation node addresses are
397 * formed by taking the low-order 24 bits of an address (unicast or anycast)
398 * and appending those bits to the prefix FF02:0:0:0:0:1:FF00::/104.
399 *
400 * Solicited-Node Address: FF02:0:0:0:0:1:FFXX:XXXX
401 *
402 * @param targetIp the unicast or anycast address
403 * @return the computed solicitation node address
404 */
Pier Luigi37b687b2017-01-16 22:46:33 -0800405 public static byte[] getSolicitNodeAddress(byte[] targetIp) {
406 checkArgument(targetIp.length == Ip6Address.BYTE_LENGTH);
407 return new byte[] {
Pier Ventre78e73f62016-12-02 19:59:28 -0800408 (byte) 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
409 0x00, 0x00, 0x00, 0x01, (byte) 0xff,
410 targetIp[targetIp.length - 3],
411 targetIp[targetIp.length - 2],
412 targetIp[targetIp.length - 1]
413 };
414 }
415
416 /**
417 * According to the RFC 2464, an IPv6 packet with a multicast
418 * destination address DST, consisting of the sixteen octets DST[1]
419 * through DST[16], is transmitted to the Ethernet multicast address
420 * whose first two octets are the value 3333 hexadecimal and whose last
421 * four octets are the last four octets of DST.
422 *
423 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
424 * |0 0 1 1 0 0 1 1|0 0 1 1 0 0 1 1|
425 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
426 * | DST[13] | DST[14] |
427 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
428 * | DST[15] | DST[16] |
429 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
430 *
431 * @param targetIp the multicast address.
432 * @return the multicast mac address
433 */
Pier Luigi37b687b2017-01-16 22:46:33 -0800434 public static byte[] getMCastMacAddress(byte[] targetIp) {
435 checkArgument(targetIp.length == Ip6Address.BYTE_LENGTH);
436 return new byte[] {
Pier Ventre78e73f62016-12-02 19:59:28 -0800437 0x33, 0x33,
438 targetIp[targetIp.length - 4],
439 targetIp[targetIp.length - 3],
440 targetIp[targetIp.length - 2],
441 targetIp[targetIp.length - 1],
442 };
443 }
Pier Luigi37b687b2017-01-16 22:46:33 -0800444
445 /**
446 * According to the RFC 4291, an IPv6 link local address is an IPv6
447 * unicast address that can be automatically configured on any interface
448 * using the link-local prefix FE80::/10 (1111 1110 10) and the interface
449 * identifier in the modified EUI-64 format.
450 *
451 * +----------------------------------------------------------------+
452 * | 10 bits | 54 bits | 64 bits |
453 * +----------- +-------------------------+-------------------------+
454 * | 1111111010 | 0 | interface ID |
455 * +----------- +-------------------------+-------------------------+
456 *
457 * @param targetIp the ip address to verify
458 * @return true if the ipv6 address is link local,
459 * false otherwise
460 */
461 public static boolean isLinkLocalAddress(byte[] targetIp) {
462 checkArgument(targetIp.length == Ip6Address.BYTE_LENGTH);
463 return (targetIp[0] & 0xff) == 0xfe && (targetIp[1] & 0xc0) == 0x80;
464 }
465
466 /**
467 * Returns the auto-generated link local address using the
468 * mac address as parameter.
469 *
470 * @param macAddress the mac address to use
471 * @return the ipv6 link local address
472 */
473 public static byte[] getLinkLocalAddress(byte[] macAddress) {
474 checkArgument(macAddress.length == MacAddress.MAC_ADDRESS_LENGTH);
475 return new byte[] {
476 LINK_LOCAL_0,
477 LINK_LOCAL_1,
478 0, 0, 0, 0, 0, 0,
479 (byte) (macAddress[0] ^ (1 << 1)),
480 macAddress[1],
481 macAddress[2],
482 (byte) 0xff,
483 (byte) 0xfe,
484 macAddress[3],
485 macAddress[4],
486 macAddress[5],
487 };
488 }
489
490 /**
491 * Returns the mac address from the auto-generated
492 * link local address.
493 *
494 * @param linkLocalAddress the ipv6 to use
495 * @return the mac address
496 */
497 public static byte[] getMacAddress(byte[] linkLocalAddress) {
498 return !isLinkLocalAddress(linkLocalAddress) ? null : new byte[] {
499 (byte) (linkLocalAddress[8] ^ (1 << 1)),
500 linkLocalAddress[9],
501 linkLocalAddress[10],
502 linkLocalAddress[13],
503 linkLocalAddress[14],
504 linkLocalAddress[15],
505 };
506 }
507
508
509
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800510}