Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
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; |
Pier Ventre | 78e73f6 | 2016-12-02 19:59:28 -0800 | [diff] [blame] | 20 | import org.onlab.packet.Ethernet; |
| 21 | import org.onlab.packet.ICMP6; |
Pier Ventre | 78e73f6 | 2016-12-02 19:59:28 -0800 | [diff] [blame] | 22 | import org.onlab.packet.IPv6; |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 23 | import org.onlab.packet.Ip6Address; |
Pier Ventre | 78e73f6 | 2016-12-02 19:59:28 -0800 | [diff] [blame] | 24 | import org.onlab.packet.MacAddress; |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 25 | |
| 26 | import java.nio.ByteBuffer; |
| 27 | import java.util.Arrays; |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 28 | import java.util.List; |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 29 | |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 30 | import static com.google.common.base.MoreObjects.toStringHelper; |
Bartlomiej Goluszka | 08537a8 | 2017-06-12 15:18:32 +0200 | [diff] [blame] | 31 | import static com.google.common.base.Preconditions.checkArgument; |
| 32 | import static com.google.common.base.Preconditions.checkNotNull; |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 33 | import static org.onlab.packet.PacketUtils.checkInput; |
| 34 | |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 35 | /** |
Dusan Pajin | a22b970 | 2015-02-12 16:25:23 +0100 | [diff] [blame] | 36 | * Implements ICMPv6 Neighbor Advertisement packet format (RFC 4861). |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 37 | */ |
| 38 | public class NeighborAdvertisement extends BasePacket { |
| 39 | public static final byte HEADER_LENGTH = 20; // bytes |
Bartlomiej Goluszka | 08537a8 | 2017-06-12 15:18:32 +0200 | [diff] [blame] | 40 | // 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. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 45 | |
| 46 | protected byte routerFlag; |
| 47 | protected byte solicitedFlag; |
| 48 | protected byte overrideFlag; |
| 49 | protected byte[] targetAddress = new byte[Ip6Address.BYTE_LENGTH]; |
| 50 | |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 51 | private final NeighborDiscoveryOptions options = |
| 52 | new NeighborDiscoveryOptions(); |
| 53 | |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 54 | /** |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 55 | * Gets router flag. |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 56 | * |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 57 | * @return the router flag |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 58 | */ |
| 59 | public byte getRouterFlag() { |
| 60 | return this.routerFlag; |
| 61 | } |
| 62 | |
| 63 | /** |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 64 | * Sets router flag. |
| 65 | * |
| 66 | * @param routerFlag the router flag to set |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 67 | * @return this |
| 68 | */ |
| 69 | public NeighborAdvertisement setRouterFlag(final byte routerFlag) { |
| 70 | this.routerFlag = routerFlag; |
| 71 | return this; |
| 72 | } |
| 73 | |
| 74 | /** |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 75 | * Gets solicited flag. |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 76 | * |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 77 | * @return the solicited flag |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 78 | */ |
| 79 | public byte getSolicitedFlag() { |
| 80 | return this.solicitedFlag; |
| 81 | } |
| 82 | |
| 83 | /** |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 84 | * Sets solicited flag. |
| 85 | * |
| 86 | * @param solicitedFlag the solicited flag to set |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 87 | * @return this |
| 88 | */ |
| 89 | public NeighborAdvertisement setSolicitedFlag(final byte solicitedFlag) { |
| 90 | this.solicitedFlag = solicitedFlag; |
| 91 | return this; |
| 92 | } |
| 93 | |
| 94 | /** |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 95 | * Gets override flag. |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 96 | * |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 97 | * @return the override flag |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 98 | */ |
| 99 | public byte getOverrideFlag() { |
| 100 | return this.overrideFlag; |
| 101 | } |
| 102 | |
| 103 | /** |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 104 | * Sets override flag. |
| 105 | * |
| 106 | * @param overrideFlag the override flag to set |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 107 | * @return this |
| 108 | */ |
| 109 | public NeighborAdvertisement setOverrideFlag(final byte overrideFlag) { |
| 110 | this.overrideFlag = overrideFlag; |
| 111 | return this; |
| 112 | } |
| 113 | |
| 114 | /** |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 115 | * Gets target address. |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 116 | * |
| 117 | * @return the target IPv6 address |
| 118 | */ |
| 119 | public byte[] getTargetAddress() { |
| 120 | return this.targetAddress; |
| 121 | } |
| 122 | |
| 123 | /** |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 124 | * Sets target address. |
| 125 | * |
| 126 | * @param targetAddress the target IPv6 address to set |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 127 | * @return this |
| 128 | */ |
| 129 | public NeighborAdvertisement setTargetAddress(final byte[] targetAddress) { |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 130 | 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. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 154 | return this; |
| 155 | } |
| 156 | |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 157 | @Override |
| 158 | public byte[] serialize() { |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 159 | byte[] optionsData = null; |
| 160 | if (this.options.hasOptions()) { |
| 161 | optionsData = this.options.serialize(); |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 162 | } |
| 163 | |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 164 | int optionsLength = 0; |
| 165 | if (optionsData != null) { |
| 166 | optionsLength = optionsData.length; |
Charles M.C. Chan | 5c0b476 | 2015-01-10 18:38:37 +0800 | [diff] [blame] | 167 | } |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 168 | |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 169 | final byte[] data = new byte[HEADER_LENGTH + optionsLength]; |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 170 | final ByteBuffer bb = ByteBuffer.wrap(data); |
| 171 | |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 172 | bb.putInt((this.routerFlag & 0x1) << 31 | |
| 173 | (this.solicitedFlag & 0x1) << 30 | |
| 174 | (this.overrideFlag & 0x1) << 29); |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 175 | bb.put(this.targetAddress, 0, Ip6Address.BYTE_LENGTH); |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 176 | if (optionsData != null) { |
| 177 | bb.put(optionsData); |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | return data; |
| 181 | } |
| 182 | |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 183 | |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 184 | |
| 185 | /* |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 186 | * (non-Javadoc) |
| 187 | * |
| 188 | * @see java.lang.Object#hashCode() |
| 189 | */ |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 190 | @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 Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 199 | for (int i = 0; i < this.targetAddress.length / 4; i++) { |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 200 | result = prime * result + bb.getInt(); |
| 201 | } |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 202 | result = prime * result + this.options.hashCode(); |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 203 | 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 Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 235 | if (!this.options.equals(other.options)) { |
| 236 | return false; |
| 237 | } |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 238 | return true; |
| 239 | } |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 240 | |
| 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 Chan | 3599d63 | 2015-09-05 14:47:51 +0800 | [diff] [blame] | 262 | if (bb.limit() - bb.position() > 0) { |
| 263 | NeighborDiscoveryOptions options = NeighborDiscoveryOptions.deserializer() |
| 264 | .deserialize(data, bb.position(), bb.limit() - bb.position()); |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 265 | |
Charles Chan | 3599d63 | 2015-09-05 14:47:51 +0800 | [diff] [blame] | 266 | for (NeighborDiscoveryOptions.Option option : options.options()) { |
| 267 | neighborAdvertisement.addOption(option.type(), option.data()); |
| 268 | } |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | return neighborAdvertisement; |
| 272 | }; |
| 273 | } |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 274 | |
| 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 Ventre | 78e73f6 | 2016-12-02 19:59:28 -0800 | [diff] [blame] | 284 | |
| 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 Goluszka | 08537a8 | 2017-06-12 15:18:32 +0200 | [diff] [blame] | 292 | * @deprecated since 1.11.0 |
Pier Ventre | 78e73f6 | 2016-12-02 19:59:28 -0800 | [diff] [blame] | 293 | */ |
Bartlomiej Goluszka | 08537a8 | 2017-06-12 15:18:32 +0200 | [diff] [blame] | 294 | @Deprecated |
Pier Ventre | 78e73f6 | 2016-12-02 19:59:28 -0800 | [diff] [blame] | 295 | public static Ethernet buildNdpAdv(byte[] srcIp, |
| 296 | byte[] srcMac, |
| 297 | Ethernet request) { |
| 298 | |
| 299 | if (srcIp.length != Ip6Address.BYTE_LENGTH || |
| 300 | srcMac.length != MacAddress.MAC_ADDRESS_LENGTH) { |
| 301 | return null; |
| 302 | } |
| 303 | |
| 304 | if (request.getEtherType() != Ethernet.TYPE_IPV6) { |
| 305 | return null; |
| 306 | } |
| 307 | |
| 308 | IPv6 ipv6Request = (IPv6) request.getPayload(); |
| 309 | |
| 310 | if (ipv6Request.getNextHeader() != IPv6.PROTOCOL_ICMP6) { |
| 311 | return null; |
| 312 | } |
| 313 | |
| 314 | ICMP6 icmpv6 = (ICMP6) ipv6Request.getPayload(); |
| 315 | |
| 316 | if (icmpv6.getIcmpType() != ICMP6.NEIGHBOR_SOLICITATION) { |
| 317 | return null; |
| 318 | } |
| 319 | |
| 320 | Ethernet eth = new Ethernet(); |
| 321 | eth.setDestinationMACAddress(request.getSourceMAC()); |
| 322 | eth.setSourceMACAddress(srcMac); |
| 323 | eth.setEtherType(Ethernet.TYPE_IPV6); |
| 324 | eth.setVlanID(request.getVlanID()); |
| 325 | |
| 326 | IPv6 ipv6 = new IPv6(); |
| 327 | ipv6.setSourceAddress(srcIp); |
| 328 | ipv6.setDestinationAddress(ipv6Request.getSourceAddress()); |
| 329 | ipv6.setHopLimit((byte) 255); |
| 330 | |
| 331 | ICMP6 icmp6 = new ICMP6(); |
| 332 | icmp6.setIcmpType(ICMP6.NEIGHBOR_ADVERTISEMENT); |
| 333 | icmp6.setIcmpCode((byte) 0); |
| 334 | |
| 335 | NeighborAdvertisement nadv = new NeighborAdvertisement(); |
| 336 | nadv.setTargetAddress(srcIp); |
| 337 | nadv.setSolicitedFlag((byte) 1); |
| 338 | nadv.setOverrideFlag((byte) 1); |
| 339 | nadv.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS, |
| 340 | srcMac); |
| 341 | |
| 342 | icmp6.setPayload(nadv); |
| 343 | ipv6.setPayload(icmp6); |
| 344 | eth.setPayload(ipv6); |
| 345 | return eth; |
| 346 | } |
| 347 | |
Bartlomiej Goluszka | 08537a8 | 2017-06-12 15:18:32 +0200 | [diff] [blame] | 348 | /** |
| 349 | * Builds an NDP reply based on a request. |
| 350 | * |
| 351 | * @param srcIp the IP address to use as the reply source |
| 352 | * @param srcMac the MAC address to use as the reply source |
| 353 | * @param request the Neighbor Solicitation request we got |
| 354 | * @return an Ethernet frame containing the Neighbor Advertisement reply |
| 355 | */ |
| 356 | public static Ethernet buildNdpAdv(Ip6Address srcIp, |
| 357 | MacAddress srcMac, |
| 358 | Ethernet request) { |
| 359 | |
| 360 | checkNotNull(srcIp, "IP address cannot be null"); |
| 361 | checkNotNull(srcMac, "MAC address cannot be null"); |
| 362 | checkNotNull(request, "Request cannot be null"); |
| 363 | checkArgument(request.getEtherType() == Ethernet.TYPE_IPV6, |
| 364 | "EtherType must be IPv6"); |
| 365 | |
| 366 | final IPv6 ipv6Request = (IPv6) request.getPayload(); |
| 367 | |
| 368 | checkArgument(ipv6Request.getNextHeader() == IPv6.PROTOCOL_ICMP6, |
| 369 | "Protocol must be ICMP6"); |
| 370 | |
| 371 | final ICMP6 icmpv6 = (ICMP6) ipv6Request.getPayload(); |
| 372 | |
| 373 | checkArgument(icmpv6.getIcmpType() == ICMP6.NEIGHBOR_SOLICITATION, |
| 374 | "ICMP6 type must be NEIGHBOR_SOLICITATION"); |
| 375 | |
| 376 | Ethernet eth = new Ethernet(); |
| 377 | eth.setDestinationMACAddress(request.getSourceMAC()); |
| 378 | eth.setSourceMACAddress(srcMac); |
| 379 | eth.setEtherType(Ethernet.TYPE_IPV6); |
| 380 | eth.setVlanID(request.getVlanID()); |
| 381 | |
| 382 | IPv6 ipv6 = new IPv6(); |
| 383 | ipv6.setSourceAddress(srcIp.toOctets()); |
| 384 | ipv6.setDestinationAddress(ipv6Request.getSourceAddress()); |
| 385 | ipv6.setHopLimit(NDP_HOP_LIMIT); |
| 386 | ipv6.setNextHeader(IPv6.PROTOCOL_ICMP6); |
| 387 | |
| 388 | ICMP6 icmp6 = new ICMP6(); |
| 389 | icmp6.setIcmpType(ICMP6.NEIGHBOR_ADVERTISEMENT); |
| 390 | icmp6.setIcmpCode(RESERVED_CODE); |
| 391 | |
| 392 | NeighborAdvertisement nadv = new NeighborAdvertisement(); |
| 393 | nadv.setTargetAddress(srcIp.toOctets()); |
| 394 | nadv.setSolicitedFlag(NDP_SOLICITED_FLAG); |
| 395 | nadv.setOverrideFlag(NDP_OVERRIDE_FLAG); |
| 396 | nadv.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS, |
| 397 | srcMac.toBytes()); |
| 398 | |
| 399 | icmp6.setPayload(nadv); |
| 400 | ipv6.setPayload(icmp6); |
| 401 | eth.setPayload(ipv6); |
| 402 | return eth; |
| 403 | } |
| 404 | |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 405 | } |