blob: b218ab8b7542e36821aec6e47221d65503039e65 [file] [log] [blame]
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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;
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080020import org.onlab.packet.IPacket;
21import org.onlab.packet.Ip6Address;
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080022
23import java.nio.ByteBuffer;
24import java.util.Arrays;
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080025import java.util.List;
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080026
Jian Li5fc14292015-12-04 11:30:46 -080027import static com.google.common.base.MoreObjects.toStringHelper;
Jonathan Hart2a655752015-04-07 16:46:33 -070028import static org.onlab.packet.PacketUtils.checkInput;
29
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080030/**
Dusan Pajina22b9702015-02-12 16:25:23 +010031 * Implements ICMPv6 Neighbor Advertisement packet format (RFC 4861).
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080032 */
33public class NeighborAdvertisement extends BasePacket {
34 public static final byte HEADER_LENGTH = 20; // bytes
35
36 protected byte routerFlag;
37 protected byte solicitedFlag;
38 protected byte overrideFlag;
39 protected byte[] targetAddress = new byte[Ip6Address.BYTE_LENGTH];
40
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080041 private final NeighborDiscoveryOptions options =
42 new NeighborDiscoveryOptions();
43
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080044 /**
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080045 * Gets router flag.
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080046 *
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080047 * @return the router flag
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080048 */
49 public byte getRouterFlag() {
50 return this.routerFlag;
51 }
52
53 /**
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080054 * Sets router flag.
55 *
56 * @param routerFlag the router flag to set
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080057 * @return this
58 */
59 public NeighborAdvertisement setRouterFlag(final byte routerFlag) {
60 this.routerFlag = routerFlag;
61 return this;
62 }
63
64 /**
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080065 * Gets solicited flag.
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080066 *
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080067 * @return the solicited flag
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080068 */
69 public byte getSolicitedFlag() {
70 return this.solicitedFlag;
71 }
72
73 /**
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080074 * Sets solicited flag.
75 *
76 * @param solicitedFlag the solicited flag to set
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080077 * @return this
78 */
79 public NeighborAdvertisement setSolicitedFlag(final byte solicitedFlag) {
80 this.solicitedFlag = solicitedFlag;
81 return this;
82 }
83
84 /**
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080085 * Gets override flag.
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080086 *
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080087 * @return the override flag
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080088 */
89 public byte getOverrideFlag() {
90 return this.overrideFlag;
91 }
92
93 /**
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080094 * Sets override flag.
95 *
96 * @param overrideFlag the override flag to set
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080097 * @return this
98 */
99 public NeighborAdvertisement setOverrideFlag(final byte overrideFlag) {
100 this.overrideFlag = overrideFlag;
101 return this;
102 }
103
104 /**
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800105 * Gets target address.
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800106 *
107 * @return the target IPv6 address
108 */
109 public byte[] getTargetAddress() {
110 return this.targetAddress;
111 }
112
113 /**
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800114 * Sets target address.
115 *
116 * @param targetAddress the target IPv6 address to set
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800117 * @return this
118 */
119 public NeighborAdvertisement setTargetAddress(final byte[] targetAddress) {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800120 this.targetAddress =
121 Arrays.copyOfRange(targetAddress, 0, Ip6Address.BYTE_LENGTH);
122 return this;
123 }
124
125 /**
126 * Gets the Neighbor Discovery Protocol packet options.
127 *
128 * @return the Neighbor Discovery Protocol packet options
129 */
130 public List<NeighborDiscoveryOptions.Option> getOptions() {
131 return this.options.options();
132 }
133
134 /**
135 * Adds a Neighbor Discovery Protocol packet option.
136 *
137 * @param type the option type
138 * @param data the option data
139 * @return this
140 */
141 public NeighborAdvertisement addOption(final byte type,
142 final byte[] data) {
143 this.options.addOption(type, data);
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800144 return this;
145 }
146
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800147 @Override
148 public byte[] serialize() {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800149 byte[] optionsData = null;
150 if (this.options.hasOptions()) {
151 optionsData = this.options.serialize();
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800152 }
153
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800154 int optionsLength = 0;
155 if (optionsData != null) {
156 optionsLength = optionsData.length;
Charles M.C. Chan5c0b4762015-01-10 18:38:37 +0800157 }
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800158
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800159 final byte[] data = new byte[HEADER_LENGTH + optionsLength];
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800160 final ByteBuffer bb = ByteBuffer.wrap(data);
161
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800162 bb.putInt((this.routerFlag & 0x1) << 31 |
163 (this.solicitedFlag & 0x1) << 30 |
164 (this.overrideFlag & 0x1) << 29);
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800165 bb.put(this.targetAddress, 0, Ip6Address.BYTE_LENGTH);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800166 if (optionsData != null) {
167 bb.put(optionsData);
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800168 }
169
170 return data;
171 }
172
173 @Override
174 public IPacket deserialize(byte[] data, int offset, int length) {
175 final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
176 int iscratch;
177
178 iscratch = bb.getInt();
179 this.routerFlag = (byte) (iscratch >> 31 & 0x1);
180 this.solicitedFlag = (byte) (iscratch >> 30 & 0x1);
181 this.overrideFlag = (byte) (iscratch >> 29 & 0x1);
182 bb.get(this.targetAddress, 0, Ip6Address.BYTE_LENGTH);
183
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800184 this.options.deserialize(data, bb.position(),
185 bb.limit() - bb.position());
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800186
187 return this;
188 }
189
190 /*
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800191 * (non-Javadoc)
192 *
193 * @see java.lang.Object#hashCode()
194 */
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800195 @Override
196 public int hashCode() {
197 final int prime = 5807;
198 int result = super.hashCode();
199 ByteBuffer bb;
200 result = prime * result + this.routerFlag;
201 result = prime * result + this.solicitedFlag;
202 result = prime * result + this.overrideFlag;
203 bb = ByteBuffer.wrap(this.targetAddress);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800204 for (int i = 0; i < this.targetAddress.length / 4; i++) {
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800205 result = prime * result + bb.getInt();
206 }
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800207 result = prime * result + this.options.hashCode();
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800208 return result;
209 }
210
211 /*
212 * (non-Javadoc)
213 *
214 * @see java.lang.Object#equals(java.lang.Object)
215 */
216 @Override
217 public boolean equals(final Object obj) {
218 if (this == obj) {
219 return true;
220 }
221 if (!super.equals(obj)) {
222 return false;
223 }
224 if (!(obj instanceof NeighborAdvertisement)) {
225 return false;
226 }
227 final NeighborAdvertisement other = (NeighborAdvertisement) obj;
228 if (this.routerFlag != other.routerFlag) {
229 return false;
230 }
231 if (this.solicitedFlag != other.solicitedFlag) {
232 return false;
233 }
234 if (this.overrideFlag != other.overrideFlag) {
235 return false;
236 }
237 if (!Arrays.equals(this.targetAddress, other.targetAddress)) {
238 return false;
239 }
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800240 if (!this.options.equals(other.options)) {
241 return false;
242 }
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800243 return true;
244 }
Jonathan Hart2a655752015-04-07 16:46:33 -0700245
246 /**
247 * Deserializer function for neighbor advertisement packets.
248 *
249 * @return deserializer function
250 */
251 public static Deserializer<NeighborAdvertisement> deserializer() {
252 return (data, offset, length) -> {
253 checkInput(data, offset, length, HEADER_LENGTH);
254
255 NeighborAdvertisement neighborAdvertisement = new NeighborAdvertisement();
256
257 ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
258
259 int iscratch;
260
261 iscratch = bb.getInt();
262 neighborAdvertisement.routerFlag = (byte) (iscratch >> 31 & 0x1);
263 neighborAdvertisement.solicitedFlag = (byte) (iscratch >> 30 & 0x1);
264 neighborAdvertisement.overrideFlag = (byte) (iscratch >> 29 & 0x1);
265 bb.get(neighborAdvertisement.targetAddress, 0, Ip6Address.BYTE_LENGTH);
266
Charles Chan3599d632015-09-05 14:47:51 +0800267 if (bb.limit() - bb.position() > 0) {
268 NeighborDiscoveryOptions options = NeighborDiscoveryOptions.deserializer()
269 .deserialize(data, bb.position(), bb.limit() - bb.position());
Jonathan Hart2a655752015-04-07 16:46:33 -0700270
Charles Chan3599d632015-09-05 14:47:51 +0800271 for (NeighborDiscoveryOptions.Option option : options.options()) {
272 neighborAdvertisement.addOption(option.type(), option.data());
273 }
Jonathan Hart2a655752015-04-07 16:46:33 -0700274 }
275
276 return neighborAdvertisement;
277 };
278 }
Jian Li5fc14292015-12-04 11:30:46 -0800279
280 @Override
281 public String toString() {
282 return toStringHelper(getClass())
283 .add("routerFlag", Byte.toString(routerFlag))
284 .add("solicitedFlag", Byte.toString(solicitedFlag))
285 .add("overrideFlag", Byte.toString(overrideFlag))
286 .add("targetAddress", Arrays.toString(targetAddress))
287 .toString();
288 }
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800289}