blob: ffb7e52fb0d5c36d32a19442cd7fa735b5c91ad5 [file] [log] [blame]
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-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 */
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;
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080025
26import java.nio.ByteBuffer;
27import java.util.Arrays;
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080028import java.util.List;
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080029
Jian Li5fc14292015-12-04 11:30:46 -080030import static com.google.common.base.MoreObjects.toStringHelper;
Bartlomiej Goluszka08537a82017-06-12 15:18:32 +020031import static com.google.common.base.Preconditions.checkArgument;
32import 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. Chan93b7fb02014-12-28 03:59:36 +080035/**
Dusan Pajina22b9702015-02-12 16:25:23 +010036 * Implements ICMPv6 Neighbor Advertisement packet format (RFC 4861).
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080037 */
38public class NeighborAdvertisement extends BasePacket {
39 public static final byte HEADER_LENGTH = 20; // bytes
Bartlomiej Goluszka08537a82017-06-12 15:18:32 +020040 // Constants for NDP reply
41 protected static final byte NDP_HOP_LIMIT = (byte) 0x255;
42 protected static final byte NDP_SOLICITED_FLAG = (byte) 0x1;
43 protected static final byte NDP_OVERRIDE_FLAG = (byte) 0x1;
44 protected static final byte RESERVED_CODE = (byte) 0x0;
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080045
46 protected byte routerFlag;
47 protected byte solicitedFlag;
48 protected byte overrideFlag;
49 protected byte[] targetAddress = new byte[Ip6Address.BYTE_LENGTH];
50
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080051 private final NeighborDiscoveryOptions options =
52 new NeighborDiscoveryOptions();
53
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080054 /**
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080055 * Gets router flag.
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080056 *
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080057 * @return the router flag
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080058 */
59 public byte getRouterFlag() {
60 return this.routerFlag;
61 }
62
63 /**
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080064 * Sets router flag.
65 *
66 * @param routerFlag the router flag to set
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080067 * @return this
68 */
69 public NeighborAdvertisement setRouterFlag(final byte routerFlag) {
70 this.routerFlag = routerFlag;
71 return this;
72 }
73
74 /**
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080075 * Gets solicited flag.
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080076 *
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080077 * @return the solicited flag
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080078 */
79 public byte getSolicitedFlag() {
80 return this.solicitedFlag;
81 }
82
83 /**
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080084 * Sets solicited flag.
85 *
86 * @param solicitedFlag the solicited flag to set
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080087 * @return this
88 */
89 public NeighborAdvertisement setSolicitedFlag(final byte solicitedFlag) {
90 this.solicitedFlag = solicitedFlag;
91 return this;
92 }
93
94 /**
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080095 * Gets override flag.
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080096 *
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080097 * @return the override flag
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080098 */
99 public byte getOverrideFlag() {
100 return this.overrideFlag;
101 }
102
103 /**
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800104 * Sets override flag.
105 *
106 * @param overrideFlag the override flag to set
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800107 * @return this
108 */
109 public NeighborAdvertisement setOverrideFlag(final byte overrideFlag) {
110 this.overrideFlag = overrideFlag;
111 return this;
112 }
113
114 /**
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800115 * Gets target address.
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800116 *
117 * @return the target IPv6 address
118 */
119 public byte[] getTargetAddress() {
120 return this.targetAddress;
121 }
122
123 /**
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800124 * Sets target address.
125 *
126 * @param targetAddress the target IPv6 address to set
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800127 * @return this
128 */
129 public NeighborAdvertisement setTargetAddress(final byte[] targetAddress) {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800130 this.targetAddress =
131 Arrays.copyOfRange(targetAddress, 0, Ip6Address.BYTE_LENGTH);
132 return this;
133 }
134
135 /**
136 * Gets the Neighbor Discovery Protocol packet options.
137 *
138 * @return the Neighbor Discovery Protocol packet options
139 */
140 public List<NeighborDiscoveryOptions.Option> getOptions() {
141 return this.options.options();
142 }
143
144 /**
145 * Adds a Neighbor Discovery Protocol packet option.
146 *
147 * @param type the option type
148 * @param data the option data
149 * @return this
150 */
151 public NeighborAdvertisement addOption(final byte type,
152 final byte[] data) {
153 this.options.addOption(type, data);
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800154 return this;
155 }
156
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800157 @Override
158 public byte[] serialize() {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800159 byte[] optionsData = null;
160 if (this.options.hasOptions()) {
161 optionsData = this.options.serialize();
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800162 }
163
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800164 int optionsLength = 0;
165 if (optionsData != null) {
166 optionsLength = optionsData.length;
Charles M.C. Chan5c0b4762015-01-10 18:38:37 +0800167 }
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800168
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800169 final byte[] data = new byte[HEADER_LENGTH + optionsLength];
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800170 final ByteBuffer bb = ByteBuffer.wrap(data);
171
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800172 bb.putInt((this.routerFlag & 0x1) << 31 |
173 (this.solicitedFlag & 0x1) << 30 |
174 (this.overrideFlag & 0x1) << 29);
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800175 bb.put(this.targetAddress, 0, Ip6Address.BYTE_LENGTH);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800176 if (optionsData != null) {
177 bb.put(optionsData);
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800178 }
179
180 return data;
181 }
182
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800183
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800184
185 /*
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800186 * (non-Javadoc)
187 *
188 * @see java.lang.Object#hashCode()
189 */
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800190 @Override
191 public int hashCode() {
192 final int prime = 5807;
193 int result = super.hashCode();
194 ByteBuffer bb;
195 result = prime * result + this.routerFlag;
196 result = prime * result + this.solicitedFlag;
197 result = prime * result + this.overrideFlag;
198 bb = ByteBuffer.wrap(this.targetAddress);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800199 for (int i = 0; i < this.targetAddress.length / 4; i++) {
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800200 result = prime * result + bb.getInt();
201 }
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800202 result = prime * result + this.options.hashCode();
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800203 return result;
204 }
205
206 /*
207 * (non-Javadoc)
208 *
209 * @see java.lang.Object#equals(java.lang.Object)
210 */
211 @Override
212 public boolean equals(final Object obj) {
213 if (this == obj) {
214 return true;
215 }
216 if (!super.equals(obj)) {
217 return false;
218 }
219 if (!(obj instanceof NeighborAdvertisement)) {
220 return false;
221 }
222 final NeighborAdvertisement other = (NeighborAdvertisement) obj;
223 if (this.routerFlag != other.routerFlag) {
224 return false;
225 }
226 if (this.solicitedFlag != other.solicitedFlag) {
227 return false;
228 }
229 if (this.overrideFlag != other.overrideFlag) {
230 return false;
231 }
232 if (!Arrays.equals(this.targetAddress, other.targetAddress)) {
233 return false;
234 }
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800235 if (!this.options.equals(other.options)) {
236 return false;
237 }
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800238 return true;
239 }
Jonathan Hart2a655752015-04-07 16:46:33 -0700240
241 /**
242 * Deserializer function for neighbor advertisement packets.
243 *
244 * @return deserializer function
245 */
246 public static Deserializer<NeighborAdvertisement> deserializer() {
247 return (data, offset, length) -> {
248 checkInput(data, offset, length, HEADER_LENGTH);
249
250 NeighborAdvertisement neighborAdvertisement = new NeighborAdvertisement();
251
252 ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
253
254 int iscratch;
255
256 iscratch = bb.getInt();
257 neighborAdvertisement.routerFlag = (byte) (iscratch >> 31 & 0x1);
258 neighborAdvertisement.solicitedFlag = (byte) (iscratch >> 30 & 0x1);
259 neighborAdvertisement.overrideFlag = (byte) (iscratch >> 29 & 0x1);
260 bb.get(neighborAdvertisement.targetAddress, 0, Ip6Address.BYTE_LENGTH);
261
Charles Chan3599d632015-09-05 14:47:51 +0800262 if (bb.limit() - bb.position() > 0) {
263 NeighborDiscoveryOptions options = NeighborDiscoveryOptions.deserializer()
264 .deserialize(data, bb.position(), bb.limit() - bb.position());
Jonathan Hart2a655752015-04-07 16:46:33 -0700265
Charles Chan3599d632015-09-05 14:47:51 +0800266 for (NeighborDiscoveryOptions.Option option : options.options()) {
267 neighborAdvertisement.addOption(option.type(), option.data());
268 }
Jonathan Hart2a655752015-04-07 16:46:33 -0700269 }
270
271 return neighborAdvertisement;
272 };
273 }
Jian Li5fc14292015-12-04 11:30:46 -0800274
275 @Override
276 public String toString() {
277 return toStringHelper(getClass())
278 .add("routerFlag", Byte.toString(routerFlag))
279 .add("solicitedFlag", Byte.toString(solicitedFlag))
280 .add("overrideFlag", Byte.toString(overrideFlag))
281 .add("targetAddress", Arrays.toString(targetAddress))
282 .toString();
283 }
Pier Ventre78e73f62016-12-02 19:59:28 -0800284
285 /**
286 * Builds an NDP reply based on a request.
287 *
288 * @param srcIp the IP address to use as the reply source
289 * @param srcMac the MAC address to use as the reply source
290 * @param request the Neighbor Solicitation request we got
291 * @return an Ethernet frame containing the Neighbor Advertisement reply
Bartlomiej Goluszka08537a82017-06-12 15:18:32 +0200292 */
293 public static Ethernet buildNdpAdv(Ip6Address srcIp,
294 MacAddress srcMac,
295 Ethernet request) {
296
297 checkNotNull(srcIp, "IP address cannot be null");
298 checkNotNull(srcMac, "MAC address cannot be null");
299 checkNotNull(request, "Request cannot be null");
300 checkArgument(request.getEtherType() == Ethernet.TYPE_IPV6,
301 "EtherType must be IPv6");
302
303 final IPv6 ipv6Request = (IPv6) request.getPayload();
304
305 checkArgument(ipv6Request.getNextHeader() == IPv6.PROTOCOL_ICMP6,
306 "Protocol must be ICMP6");
307
308 final ICMP6 icmpv6 = (ICMP6) ipv6Request.getPayload();
309
310 checkArgument(icmpv6.getIcmpType() == ICMP6.NEIGHBOR_SOLICITATION,
311 "ICMP6 type must be NEIGHBOR_SOLICITATION");
312
313 Ethernet eth = new Ethernet();
314 eth.setDestinationMACAddress(request.getSourceMAC());
315 eth.setSourceMACAddress(srcMac);
316 eth.setEtherType(Ethernet.TYPE_IPV6);
317 eth.setVlanID(request.getVlanID());
318
319 IPv6 ipv6 = new IPv6();
320 ipv6.setSourceAddress(srcIp.toOctets());
321 ipv6.setDestinationAddress(ipv6Request.getSourceAddress());
322 ipv6.setHopLimit(NDP_HOP_LIMIT);
323 ipv6.setNextHeader(IPv6.PROTOCOL_ICMP6);
324
325 ICMP6 icmp6 = new ICMP6();
326 icmp6.setIcmpType(ICMP6.NEIGHBOR_ADVERTISEMENT);
327 icmp6.setIcmpCode(RESERVED_CODE);
328
329 NeighborAdvertisement nadv = new NeighborAdvertisement();
330 nadv.setTargetAddress(srcIp.toOctets());
331 nadv.setSolicitedFlag(NDP_SOLICITED_FLAG);
332 nadv.setOverrideFlag(NDP_OVERRIDE_FLAG);
333 nadv.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
334 srcMac.toBytes());
335
336 icmp6.setPayload(nadv);
337 ipv6.setPayload(icmp6);
338 eth.setPayload(ipv6);
339 return eth;
340 }
341
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800342}