blob: 99fa0dd658d0ad36885074f3aea7d0a44b313023 [file] [log] [blame]
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +08001/*
Dusan Pajina22b9702015-02-12 16:25:23 +01002 * Copyright 2014-2015 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
Jonathan Hart2a655752015-04-07 16:46:33 -070027import static org.onlab.packet.PacketUtils.checkInput;
28
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080029/**
Dusan Pajina22b9702015-02-12 16:25:23 +010030 * Implements ICMPv6 Neighbor Advertisement packet format (RFC 4861).
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080031 */
32public class NeighborAdvertisement extends BasePacket {
33 public static final byte HEADER_LENGTH = 20; // bytes
34
35 protected byte routerFlag;
36 protected byte solicitedFlag;
37 protected byte overrideFlag;
38 protected byte[] targetAddress = new byte[Ip6Address.BYTE_LENGTH];
39
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080040 private final NeighborDiscoveryOptions options =
41 new NeighborDiscoveryOptions();
42
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080043 /**
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080044 * Gets router flag.
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080045 *
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080046 * @return the router flag
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080047 */
48 public byte getRouterFlag() {
49 return this.routerFlag;
50 }
51
52 /**
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080053 * Sets router flag.
54 *
55 * @param routerFlag the router flag to set
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080056 * @return this
57 */
58 public NeighborAdvertisement setRouterFlag(final byte routerFlag) {
59 this.routerFlag = routerFlag;
60 return this;
61 }
62
63 /**
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080064 * Gets solicited flag.
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080065 *
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080066 * @return the solicited flag
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080067 */
68 public byte getSolicitedFlag() {
69 return this.solicitedFlag;
70 }
71
72 /**
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080073 * Sets solicited flag.
74 *
75 * @param solicitedFlag the solicited flag to set
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080076 * @return this
77 */
78 public NeighborAdvertisement setSolicitedFlag(final byte solicitedFlag) {
79 this.solicitedFlag = solicitedFlag;
80 return this;
81 }
82
83 /**
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080084 * Gets override flag.
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080085 *
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080086 * @return the override flag
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080087 */
88 public byte getOverrideFlag() {
89 return this.overrideFlag;
90 }
91
92 /**
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080093 * Sets override flag.
94 *
95 * @param overrideFlag the override flag to set
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080096 * @return this
97 */
98 public NeighborAdvertisement setOverrideFlag(final byte overrideFlag) {
99 this.overrideFlag = overrideFlag;
100 return this;
101 }
102
103 /**
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800104 * Gets target address.
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800105 *
106 * @return the target IPv6 address
107 */
108 public byte[] getTargetAddress() {
109 return this.targetAddress;
110 }
111
112 /**
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800113 * Sets target address.
114 *
115 * @param targetAddress the target IPv6 address to set
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800116 * @return this
117 */
118 public NeighborAdvertisement setTargetAddress(final byte[] targetAddress) {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800119 this.targetAddress =
120 Arrays.copyOfRange(targetAddress, 0, Ip6Address.BYTE_LENGTH);
121 return this;
122 }
123
124 /**
125 * Gets the Neighbor Discovery Protocol packet options.
126 *
127 * @return the Neighbor Discovery Protocol packet options
128 */
129 public List<NeighborDiscoveryOptions.Option> getOptions() {
130 return this.options.options();
131 }
132
133 /**
134 * Adds a Neighbor Discovery Protocol packet option.
135 *
136 * @param type the option type
137 * @param data the option data
138 * @return this
139 */
140 public NeighborAdvertisement addOption(final byte type,
141 final byte[] data) {
142 this.options.addOption(type, data);
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800143 return this;
144 }
145
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800146 @Override
147 public byte[] serialize() {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800148 byte[] optionsData = null;
149 if (this.options.hasOptions()) {
150 optionsData = this.options.serialize();
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800151 }
152
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800153 int optionsLength = 0;
154 if (optionsData != null) {
155 optionsLength = optionsData.length;
Charles M.C. Chan5c0b4762015-01-10 18:38:37 +0800156 }
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800157
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800158 final byte[] data = new byte[HEADER_LENGTH + optionsLength];
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800159 final ByteBuffer bb = ByteBuffer.wrap(data);
160
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800161 bb.putInt((this.routerFlag & 0x1) << 31 |
162 (this.solicitedFlag & 0x1) << 30 |
163 (this.overrideFlag & 0x1) << 29);
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800164 bb.put(this.targetAddress, 0, Ip6Address.BYTE_LENGTH);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800165 if (optionsData != null) {
166 bb.put(optionsData);
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800167 }
168
169 return data;
170 }
171
172 @Override
173 public IPacket deserialize(byte[] data, int offset, int length) {
174 final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
175 int iscratch;
176
177 iscratch = bb.getInt();
178 this.routerFlag = (byte) (iscratch >> 31 & 0x1);
179 this.solicitedFlag = (byte) (iscratch >> 30 & 0x1);
180 this.overrideFlag = (byte) (iscratch >> 29 & 0x1);
181 bb.get(this.targetAddress, 0, Ip6Address.BYTE_LENGTH);
182
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800183 this.options.deserialize(data, bb.position(),
184 bb.limit() - bb.position());
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800185
186 return this;
187 }
188
189 /*
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800190 * (non-Javadoc)
191 *
192 * @see java.lang.Object#hashCode()
193 */
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800194 @Override
195 public int hashCode() {
196 final int prime = 5807;
197 int result = super.hashCode();
198 ByteBuffer bb;
199 result = prime * result + this.routerFlag;
200 result = prime * result + this.solicitedFlag;
201 result = prime * result + this.overrideFlag;
202 bb = ByteBuffer.wrap(this.targetAddress);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800203 for (int i = 0; i < this.targetAddress.length / 4; i++) {
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800204 result = prime * result + bb.getInt();
205 }
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800206 result = prime * result + this.options.hashCode();
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800207 return result;
208 }
209
210 /*
211 * (non-Javadoc)
212 *
213 * @see java.lang.Object#equals(java.lang.Object)
214 */
215 @Override
216 public boolean equals(final Object obj) {
217 if (this == obj) {
218 return true;
219 }
220 if (!super.equals(obj)) {
221 return false;
222 }
223 if (!(obj instanceof NeighborAdvertisement)) {
224 return false;
225 }
226 final NeighborAdvertisement other = (NeighborAdvertisement) obj;
227 if (this.routerFlag != other.routerFlag) {
228 return false;
229 }
230 if (this.solicitedFlag != other.solicitedFlag) {
231 return false;
232 }
233 if (this.overrideFlag != other.overrideFlag) {
234 return false;
235 }
236 if (!Arrays.equals(this.targetAddress, other.targetAddress)) {
237 return false;
238 }
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800239 if (!this.options.equals(other.options)) {
240 return false;
241 }
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800242 return true;
243 }
Jonathan Hart2a655752015-04-07 16:46:33 -0700244
245 /**
246 * Deserializer function for neighbor advertisement packets.
247 *
248 * @return deserializer function
249 */
250 public static Deserializer<NeighborAdvertisement> deserializer() {
251 return (data, offset, length) -> {
252 checkInput(data, offset, length, HEADER_LENGTH);
253
254 NeighborAdvertisement neighborAdvertisement = new NeighborAdvertisement();
255
256 ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
257
258 int iscratch;
259
260 iscratch = bb.getInt();
261 neighborAdvertisement.routerFlag = (byte) (iscratch >> 31 & 0x1);
262 neighborAdvertisement.solicitedFlag = (byte) (iscratch >> 30 & 0x1);
263 neighborAdvertisement.overrideFlag = (byte) (iscratch >> 29 & 0x1);
264 bb.get(neighborAdvertisement.targetAddress, 0, Ip6Address.BYTE_LENGTH);
265
Charles Chan3599d632015-09-05 14:47:51 +0800266 if (bb.limit() - bb.position() > 0) {
267 NeighborDiscoveryOptions options = NeighborDiscoveryOptions.deserializer()
268 .deserialize(data, bb.position(), bb.limit() - bb.position());
Jonathan Hart2a655752015-04-07 16:46:33 -0700269
Charles Chan3599d632015-09-05 14:47:51 +0800270 for (NeighborDiscoveryOptions.Option option : options.options()) {
271 neighborAdvertisement.addOption(option.type(), option.data());
272 }
Jonathan Hart2a655752015-04-07 16:46:33 -0700273 }
274
275 return neighborAdvertisement;
276 };
277 }
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800278}