blob: 73350fc53ec6dd3b5cea63648c87a7f86244402d [file] [log] [blame]
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +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 */
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080016package org.onlab.packet.ndp;
17
18import org.onlab.packet.BasePacket;
Jonathan Hart2a655752015-04-07 16:46:33 -070019import org.onlab.packet.Deserializer;
Pier Ventre78e73f62016-12-02 19:59:28 -080020import org.onlab.packet.Ethernet;
21import org.onlab.packet.ICMP6;
Pier Ventre78e73f62016-12-02 19:59:28 -080022import org.onlab.packet.IPv6;
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080023import org.onlab.packet.Ip6Address;
Pier Ventre78e73f62016-12-02 19:59:28 -080024import org.onlab.packet.MacAddress;
25import org.onlab.packet.VlanId;
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080026
27import java.nio.ByteBuffer;
28import java.util.Arrays;
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080029import java.util.List;
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080030
Jian Li5fc14292015-12-04 11:30:46 -080031import static com.google.common.base.MoreObjects.toStringHelper;
Bartlomiej Goluszka08537a82017-06-12 15:18:32 +020032import static com.google.common.base.Preconditions.checkNotNull;
Jonathan Hart2a655752015-04-07 16:46:33 -070033import static org.onlab.packet.PacketUtils.checkInput;
34
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080035/**
36 * Implements ICMPv6 Neighbor Solicitation packet format. (RFC 4861)
37 */
38public class NeighborSolicitation extends BasePacket {
39 public static final byte HEADER_LENGTH = 20; // bytes
Bartlomiej Goluszka08537a82017-06-12 15:18:32 +020040 // Constants for NDP reply
Charles Chaneeb10fc2017-12-01 17:44:35 -080041 protected static final byte NDP_HOP_LIMIT = (byte) 255;
Bartlomiej Goluszka08537a82017-06-12 15:18:32 +020042 protected static final byte RESERVED_CODE = (byte) 0x0;
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080043
44 protected byte[] targetAddress = new byte[Ip6Address.BYTE_LENGTH];
45
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080046 private final NeighborDiscoveryOptions options =
47 new NeighborDiscoveryOptions();
48
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080049 /**
50 * Gets target address.
51 *
52 * @return the target IPv6 address
53 */
54 public byte[] getTargetAddress() {
55 return this.targetAddress;
56 }
57
58 /**
59 * Sets target address.
60 *
61 * @param targetAddress the target IPv6 address to set
62 * @return this
63 */
64 public NeighborSolicitation setTargetAddress(final byte[] targetAddress) {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080065 this.targetAddress =
66 Arrays.copyOfRange(targetAddress, 0, Ip6Address.BYTE_LENGTH);
67 return this;
68 }
69
70 /**
71 * Gets the Neighbor Discovery Protocol packet options.
72 *
73 * @return the Neighbor Discovery Protocol packet options
74 */
75 public List<NeighborDiscoveryOptions.Option> getOptions() {
76 return this.options.options();
77 }
78
79 /**
80 * Adds a Neighbor Discovery Protocol packet option.
81 *
82 * @param type the option type
83 * @param data the option data
84 * @return this
85 */
86 public NeighborSolicitation addOption(final byte type,
87 final byte[] data) {
88 this.options.addOption(type, data);
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080089 return this;
90 }
91
92 @Override
93 public byte[] serialize() {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080094 byte[] optionsData = null;
95 if (this.options.hasOptions()) {
96 optionsData = this.options.serialize();
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080097 }
98
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080099 int optionsLength = 0;
100 if (optionsData != null) {
101 optionsLength = optionsData.length;
Charles M.C. Chan5c0b4762015-01-10 18:38:37 +0800102 }
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800103
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800104 final byte[] data = new byte[HEADER_LENGTH + optionsLength];
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800105 final ByteBuffer bb = ByteBuffer.wrap(data);
106
107 bb.putInt(0);
108 bb.put(this.targetAddress, 0, Ip6Address.BYTE_LENGTH);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800109 if (optionsData != null) {
110 bb.put(optionsData);
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800111 }
112
113 return data;
114 }
115
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800116
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800117
118 /*
119 * (non-Javadoc)
120 *
121 * @see java.lang.Object#hashCode()
122 */
123 @Override
124 public int hashCode() {
125 final int prime = 5807;
126 int result = super.hashCode();
127 ByteBuffer bb;
128 bb = ByteBuffer.wrap(this.targetAddress);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800129 for (int i = 0; i < this.targetAddress.length / 4; i++) {
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800130 result = prime * result + bb.getInt();
131 }
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800132 result = prime * result + this.options.hashCode();
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800133 return result;
134 }
135
136 /*
137 * (non-Javadoc)
138 *
139 * @see java.lang.Object#equals(java.lang.Object)
140 */
141 @Override
142 public boolean equals(final Object obj) {
143 if (this == obj) {
144 return true;
145 }
146 if (!super.equals(obj)) {
147 return false;
148 }
149 if (!(obj instanceof NeighborSolicitation)) {
150 return false;
151 }
152 final NeighborSolicitation other = (NeighborSolicitation) obj;
153 if (!Arrays.equals(this.targetAddress, other.targetAddress)) {
154 return false;
155 }
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800156 if (!this.options.equals(other.options)) {
157 return false;
158 }
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800159 return true;
160 }
Jonathan Hart2a655752015-04-07 16:46:33 -0700161
162 /**
163 * Deserializer function for neighbor solicitation packets.
164 *
165 * @return deserializer function
166 */
167 public static Deserializer<NeighborSolicitation> deserializer() {
168 return (data, offset, length) -> {
169 checkInput(data, offset, length, HEADER_LENGTH);
170
171 NeighborSolicitation neighborSolicitation = new NeighborSolicitation();
172
173 ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
174
175 bb.getInt();
176 bb.get(neighborSolicitation.targetAddress, 0, Ip6Address.BYTE_LENGTH);
177
Charles Chan3599d632015-09-05 14:47:51 +0800178 if (bb.limit() - bb.position() > 0) {
179 NeighborDiscoveryOptions options = NeighborDiscoveryOptions.deserializer()
180 .deserialize(data, bb.position(), bb.limit() - bb.position());
Jonathan Hart2a655752015-04-07 16:46:33 -0700181
Charles Chan3599d632015-09-05 14:47:51 +0800182 for (NeighborDiscoveryOptions.Option option : options.options()) {
183 neighborSolicitation.addOption(option.type(), option.data());
184 }
Jonathan Hart2a655752015-04-07 16:46:33 -0700185 }
186
187 return neighborSolicitation;
188 };
189 }
Jian Li5fc14292015-12-04 11:30:46 -0800190
191 @Override
192 public String toString() {
193 return toStringHelper(getClass())
194 .add("targetAddress", Arrays.toString(targetAddress))
195 .toString();
196 // TODO: need to handle options
197 }
Pier Ventre78e73f62016-12-02 19:59:28 -0800198
199 /**
200 * Builds a NDP solicitation using the supplied parameters.
201 *
202 * @param targetIp the target ip
203 * @param sourceIp the source ip
204 * @param destinationIp the destination ip
205 * @param sourceMac the source mac address
206 * @param destinationMac the destination mac address
207 * @param vlan the vlan id
208 * @return the ethernet packet containing the ndp solicitation
Bartlomiej Goluszka08537a82017-06-12 15:18:32 +0200209 */
210 public static Ethernet buildNdpSolicit(Ip6Address targetIp,
211 Ip6Address sourceIp,
212 Ip6Address destinationIp,
213 MacAddress sourceMac,
214 MacAddress destinationMac,
215 VlanId vlan) {
216
217 checkNotNull(targetIp, "Target IP address cannot be null");
218 checkNotNull(sourceIp, "Source IP address cannot be null");
219 checkNotNull(destinationIp, "Destination IP address cannot be null");
220 checkNotNull(sourceMac, "Source MAC address cannot be null");
221 checkNotNull(destinationMac, "Destination MAC address cannot be null");
222 checkNotNull(vlan, "Vlan cannot be null");
223
224 // Here we craft the Ethernet packet.
225 Ethernet ethernet = new Ethernet();
226 ethernet.setEtherType(Ethernet.TYPE_IPV6)
227 .setDestinationMACAddress(destinationMac)
228 .setSourceMACAddress(sourceMac);
229 ethernet.setVlanID(vlan.id());
230 // IPv6 packet is created.
231 IPv6 ipv6 = new IPv6();
232 ipv6.setSourceAddress(sourceIp.toOctets());
233 ipv6.setDestinationAddress(destinationIp.toOctets());
234 ipv6.setHopLimit(NDP_HOP_LIMIT);
235 // Create the ICMPv6 packet.
236 ICMP6 icmp6 = new ICMP6();
237 icmp6.setIcmpType(ICMP6.NEIGHBOR_SOLICITATION);
238 icmp6.setIcmpCode(RESERVED_CODE);
239 // Create the Neighbor Solicitation packet.
240 NeighborSolicitation ns = new NeighborSolicitation();
241 ns.setTargetAddress(targetIp.toOctets());
242 // DAD packets should not contain SRC_LL_ADDR option
243 if (!Arrays.equals(sourceIp.toOctets(), Ip6Address.ZERO.toOctets())) {
244 ns.addOption(NeighborDiscoveryOptions.TYPE_SOURCE_LL_ADDRESS, sourceMac.toBytes());
245 }
246 // Set the payloads
247 icmp6.setPayload(ns);
248 ipv6.setPayload(icmp6);
249 ethernet.setPayload(ipv6);
250
251 return ethernet;
252 }
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800253}