blob: 82071dabf19bfa4458a8749b3d5325db86d6db91 [file] [log] [blame]
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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;
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080022import org.onlab.packet.IPacket;
Pier Ventre78e73f62016-12-02 19:59:28 -080023import org.onlab.packet.IPv6;
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080024import org.onlab.packet.Ip6Address;
Pier Ventre78e73f62016-12-02 19:59:28 -080025import org.onlab.packet.MacAddress;
26import org.onlab.packet.VlanId;
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080027
28import java.nio.ByteBuffer;
29import java.util.Arrays;
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080030import java.util.List;
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080031
Jian Li5fc14292015-12-04 11:30:46 -080032import static com.google.common.base.MoreObjects.toStringHelper;
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
40
41 protected byte[] targetAddress = new byte[Ip6Address.BYTE_LENGTH];
42
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080043 private final NeighborDiscoveryOptions options =
44 new NeighborDiscoveryOptions();
45
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080046 /**
47 * Gets target address.
48 *
49 * @return the target IPv6 address
50 */
51 public byte[] getTargetAddress() {
52 return this.targetAddress;
53 }
54
55 /**
56 * Sets target address.
57 *
58 * @param targetAddress the target IPv6 address to set
59 * @return this
60 */
61 public NeighborSolicitation setTargetAddress(final byte[] targetAddress) {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080062 this.targetAddress =
63 Arrays.copyOfRange(targetAddress, 0, Ip6Address.BYTE_LENGTH);
64 return this;
65 }
66
67 /**
68 * Gets the Neighbor Discovery Protocol packet options.
69 *
70 * @return the Neighbor Discovery Protocol packet options
71 */
72 public List<NeighborDiscoveryOptions.Option> getOptions() {
73 return this.options.options();
74 }
75
76 /**
77 * Adds a Neighbor Discovery Protocol packet option.
78 *
79 * @param type the option type
80 * @param data the option data
81 * @return this
82 */
83 public NeighborSolicitation addOption(final byte type,
84 final byte[] data) {
85 this.options.addOption(type, data);
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080086 return this;
87 }
88
89 @Override
90 public byte[] serialize() {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080091 byte[] optionsData = null;
92 if (this.options.hasOptions()) {
93 optionsData = this.options.serialize();
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080094 }
95
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080096 int optionsLength = 0;
97 if (optionsData != null) {
98 optionsLength = optionsData.length;
Charles M.C. Chan5c0b4762015-01-10 18:38:37 +080099 }
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800100
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800101 final byte[] data = new byte[HEADER_LENGTH + optionsLength];
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800102 final ByteBuffer bb = ByteBuffer.wrap(data);
103
104 bb.putInt(0);
105 bb.put(this.targetAddress, 0, Ip6Address.BYTE_LENGTH);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800106 if (optionsData != null) {
107 bb.put(optionsData);
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800108 }
109
110 return data;
111 }
112
113 @Override
114 public IPacket deserialize(byte[] data, int offset, int length) {
115 final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
116
117 bb.getInt();
118 bb.get(this.targetAddress, 0, Ip6Address.BYTE_LENGTH);
119
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800120 this.options.deserialize(data, bb.position(),
121 bb.limit() - bb.position());
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800122
123 return this;
124 }
125
126 /*
127 * (non-Javadoc)
128 *
129 * @see java.lang.Object#hashCode()
130 */
131 @Override
132 public int hashCode() {
133 final int prime = 5807;
134 int result = super.hashCode();
135 ByteBuffer bb;
136 bb = ByteBuffer.wrap(this.targetAddress);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800137 for (int i = 0; i < this.targetAddress.length / 4; i++) {
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800138 result = prime * result + bb.getInt();
139 }
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800140 result = prime * result + this.options.hashCode();
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800141 return result;
142 }
143
144 /*
145 * (non-Javadoc)
146 *
147 * @see java.lang.Object#equals(java.lang.Object)
148 */
149 @Override
150 public boolean equals(final Object obj) {
151 if (this == obj) {
152 return true;
153 }
154 if (!super.equals(obj)) {
155 return false;
156 }
157 if (!(obj instanceof NeighborSolicitation)) {
158 return false;
159 }
160 final NeighborSolicitation other = (NeighborSolicitation) obj;
161 if (!Arrays.equals(this.targetAddress, other.targetAddress)) {
162 return false;
163 }
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800164 if (!this.options.equals(other.options)) {
165 return false;
166 }
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800167 return true;
168 }
Jonathan Hart2a655752015-04-07 16:46:33 -0700169
170 /**
171 * Deserializer function for neighbor solicitation packets.
172 *
173 * @return deserializer function
174 */
175 public static Deserializer<NeighborSolicitation> deserializer() {
176 return (data, offset, length) -> {
177 checkInput(data, offset, length, HEADER_LENGTH);
178
179 NeighborSolicitation neighborSolicitation = new NeighborSolicitation();
180
181 ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
182
183 bb.getInt();
184 bb.get(neighborSolicitation.targetAddress, 0, Ip6Address.BYTE_LENGTH);
185
Charles Chan3599d632015-09-05 14:47:51 +0800186 if (bb.limit() - bb.position() > 0) {
187 NeighborDiscoveryOptions options = NeighborDiscoveryOptions.deserializer()
188 .deserialize(data, bb.position(), bb.limit() - bb.position());
Jonathan Hart2a655752015-04-07 16:46:33 -0700189
Charles Chan3599d632015-09-05 14:47:51 +0800190 for (NeighborDiscoveryOptions.Option option : options.options()) {
191 neighborSolicitation.addOption(option.type(), option.data());
192 }
Jonathan Hart2a655752015-04-07 16:46:33 -0700193 }
194
195 return neighborSolicitation;
196 };
197 }
Jian Li5fc14292015-12-04 11:30:46 -0800198
199 @Override
200 public String toString() {
201 return toStringHelper(getClass())
202 .add("targetAddress", Arrays.toString(targetAddress))
203 .toString();
204 // TODO: need to handle options
205 }
Pier Ventre78e73f62016-12-02 19:59:28 -0800206
207 /**
208 * Builds a NDP solicitation using the supplied parameters.
209 *
210 * @param targetIp the target ip
211 * @param sourceIp the source ip
212 * @param destinationIp the destination ip
213 * @param sourceMac the source mac address
214 * @param destinationMac the destination mac address
215 * @param vlan the vlan id
216 * @return the ethernet packet containing the ndp solicitation
217 */
218 public static Ethernet buildNdpSolicit(byte[] targetIp,
219 byte[] sourceIp,
220 byte[] destinationIp,
221 byte[] sourceMac,
222 byte[] destinationMac,
223 VlanId vlan) {
224
225 if (targetIp.length != Ip6Address.BYTE_LENGTH ||
226 sourceIp.length != Ip6Address.BYTE_LENGTH ||
227 destinationIp.length != Ip6Address.BYTE_LENGTH ||
228 sourceMac.length != MacAddress.MAC_ADDRESS_LENGTH ||
229 destinationMac.length != MacAddress.MAC_ADDRESS_LENGTH) {
230 return null;
231 }
232
Charles Chan4b873892017-05-25 18:30:09 -0700233 // Here we craft the Ethernet packet.
Pier Ventre78e73f62016-12-02 19:59:28 -0800234 Ethernet ethernet = new Ethernet();
235 ethernet.setEtherType(Ethernet.TYPE_IPV6)
236 .setDestinationMACAddress(destinationMac)
237 .setSourceMACAddress(sourceMac);
238 ethernet.setVlanID(vlan.id());
Charles Chan4b873892017-05-25 18:30:09 -0700239 // IPv6 packet is created.
Pier Ventre78e73f62016-12-02 19:59:28 -0800240 IPv6 ipv6 = new IPv6();
241 ipv6.setSourceAddress(sourceIp);
242 ipv6.setDestinationAddress(destinationIp);
243 ipv6.setHopLimit((byte) 255);
Charles Chan4b873892017-05-25 18:30:09 -0700244 // Create the ICMPv6 packet.
Pier Ventre78e73f62016-12-02 19:59:28 -0800245 ICMP6 icmp6 = new ICMP6();
246 icmp6.setIcmpType(ICMP6.NEIGHBOR_SOLICITATION);
247 icmp6.setIcmpCode((byte) 0);
Charles Chan4b873892017-05-25 18:30:09 -0700248 // Create the Neighbor Solicitation packet.
Pier Ventre78e73f62016-12-02 19:59:28 -0800249 NeighborSolicitation ns = new NeighborSolicitation();
250 ns.setTargetAddress(targetIp);
Charles Chan4b873892017-05-25 18:30:09 -0700251 // DAD packets should not contain SRC_LL_ADDR option
252 if (!Arrays.equals(sourceIp, Ip6Address.ZERO.toOctets())) {
253 ns.addOption(NeighborDiscoveryOptions.TYPE_SOURCE_LL_ADDRESS, sourceMac);
254 }
255 // Set the payloads
Pier Ventre78e73f62016-12-02 19:59:28 -0800256 icmp6.setPayload(ns);
257 ipv6.setPayload(icmp6);
258 ethernet.setPayload(ipv6);
259
260 return ethernet;
261 }
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800262}