Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 1 | /* |
Dusan Pajin | a22b970 | 2015-02-12 16:25:23 +0100 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 3 | * |
| 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. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 16 | package org.onlab.packet.ndp; |
| 17 | |
| 18 | import org.onlab.packet.BasePacket; |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 19 | import org.onlab.packet.Deserializer; |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 20 | import org.onlab.packet.IPacket; |
| 21 | import org.onlab.packet.Ip6Address; |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 22 | |
| 23 | import java.nio.ByteBuffer; |
| 24 | import java.util.Arrays; |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 25 | import java.util.List; |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 26 | |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 27 | import static com.google.common.base.MoreObjects.toStringHelper; |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 28 | import static org.onlab.packet.PacketUtils.checkInput; |
| 29 | |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 30 | /** |
Dusan Pajin | a22b970 | 2015-02-12 16:25:23 +0100 | [diff] [blame] | 31 | * Implements ICMPv6 Neighbor Advertisement packet format (RFC 4861). |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 32 | */ |
| 33 | public 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 Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 41 | private final NeighborDiscoveryOptions options = |
| 42 | new NeighborDiscoveryOptions(); |
| 43 | |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 44 | /** |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 45 | * Gets router flag. |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 46 | * |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 47 | * @return the router flag |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 48 | */ |
| 49 | public byte getRouterFlag() { |
| 50 | return this.routerFlag; |
| 51 | } |
| 52 | |
| 53 | /** |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 54 | * Sets router flag. |
| 55 | * |
| 56 | * @param routerFlag the router flag to set |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 57 | * @return this |
| 58 | */ |
| 59 | public NeighborAdvertisement setRouterFlag(final byte routerFlag) { |
| 60 | this.routerFlag = routerFlag; |
| 61 | return this; |
| 62 | } |
| 63 | |
| 64 | /** |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 65 | * Gets solicited flag. |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 66 | * |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 67 | * @return the solicited flag |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 68 | */ |
| 69 | public byte getSolicitedFlag() { |
| 70 | return this.solicitedFlag; |
| 71 | } |
| 72 | |
| 73 | /** |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 74 | * Sets solicited flag. |
| 75 | * |
| 76 | * @param solicitedFlag the solicited flag to set |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 77 | * @return this |
| 78 | */ |
| 79 | public NeighborAdvertisement setSolicitedFlag(final byte solicitedFlag) { |
| 80 | this.solicitedFlag = solicitedFlag; |
| 81 | return this; |
| 82 | } |
| 83 | |
| 84 | /** |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 85 | * Gets override flag. |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 86 | * |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 87 | * @return the override flag |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 88 | */ |
| 89 | public byte getOverrideFlag() { |
| 90 | return this.overrideFlag; |
| 91 | } |
| 92 | |
| 93 | /** |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 94 | * Sets override flag. |
| 95 | * |
| 96 | * @param overrideFlag the override flag to set |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 97 | * @return this |
| 98 | */ |
| 99 | public NeighborAdvertisement setOverrideFlag(final byte overrideFlag) { |
| 100 | this.overrideFlag = overrideFlag; |
| 101 | return this; |
| 102 | } |
| 103 | |
| 104 | /** |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 105 | * Gets target address. |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 106 | * |
| 107 | * @return the target IPv6 address |
| 108 | */ |
| 109 | public byte[] getTargetAddress() { |
| 110 | return this.targetAddress; |
| 111 | } |
| 112 | |
| 113 | /** |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 114 | * Sets target address. |
| 115 | * |
| 116 | * @param targetAddress the target IPv6 address to set |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 117 | * @return this |
| 118 | */ |
| 119 | public NeighborAdvertisement setTargetAddress(final byte[] targetAddress) { |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 120 | 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. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 144 | return this; |
| 145 | } |
| 146 | |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 147 | @Override |
| 148 | public byte[] serialize() { |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 149 | byte[] optionsData = null; |
| 150 | if (this.options.hasOptions()) { |
| 151 | optionsData = this.options.serialize(); |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 152 | } |
| 153 | |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 154 | int optionsLength = 0; |
| 155 | if (optionsData != null) { |
| 156 | optionsLength = optionsData.length; |
Charles M.C. Chan | 5c0b476 | 2015-01-10 18:38:37 +0800 | [diff] [blame] | 157 | } |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 158 | |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 159 | final byte[] data = new byte[HEADER_LENGTH + optionsLength]; |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 160 | final ByteBuffer bb = ByteBuffer.wrap(data); |
| 161 | |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 162 | bb.putInt((this.routerFlag & 0x1) << 31 | |
| 163 | (this.solicitedFlag & 0x1) << 30 | |
| 164 | (this.overrideFlag & 0x1) << 29); |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 165 | bb.put(this.targetAddress, 0, Ip6Address.BYTE_LENGTH); |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 166 | if (optionsData != null) { |
| 167 | bb.put(optionsData); |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 168 | } |
| 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 Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 184 | this.options.deserialize(data, bb.position(), |
| 185 | bb.limit() - bb.position()); |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 186 | |
| 187 | return this; |
| 188 | } |
| 189 | |
| 190 | /* |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 191 | * (non-Javadoc) |
| 192 | * |
| 193 | * @see java.lang.Object#hashCode() |
| 194 | */ |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 195 | @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 Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 204 | for (int i = 0; i < this.targetAddress.length / 4; i++) { |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 205 | result = prime * result + bb.getInt(); |
| 206 | } |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 207 | result = prime * result + this.options.hashCode(); |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 208 | 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 Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 240 | if (!this.options.equals(other.options)) { |
| 241 | return false; |
| 242 | } |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 243 | return true; |
| 244 | } |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 245 | |
| 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 Chan | 3599d63 | 2015-09-05 14:47:51 +0800 | [diff] [blame] | 267 | if (bb.limit() - bb.position() > 0) { |
| 268 | NeighborDiscoveryOptions options = NeighborDiscoveryOptions.deserializer() |
| 269 | .deserialize(data, bb.position(), bb.limit() - bb.position()); |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 270 | |
Charles Chan | 3599d63 | 2015-09-05 14:47:51 +0800 | [diff] [blame] | 271 | for (NeighborDiscoveryOptions.Option option : options.options()) { |
| 272 | neighborAdvertisement.addOption(option.type(), option.data()); |
| 273 | } |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | return neighborAdvertisement; |
| 277 | }; |
| 278 | } |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 279 | |
| 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. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 289 | } |