Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +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 | 7fee36a | 2014-12-31 00:19:59 +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; |
| 25 | import org.onlab.packet.VlanId; |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 26 | |
| 27 | import java.nio.ByteBuffer; |
| 28 | import java.util.Arrays; |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 29 | import java.util.List; |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 30 | |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 31 | import static com.google.common.base.MoreObjects.toStringHelper; |
Bartlomiej Goluszka | 08537a8 | 2017-06-12 15:18:32 +0200 | [diff] [blame] | 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 | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 35 | /** |
| 36 | * Implements ICMPv6 Neighbor Solicitation packet format. (RFC 4861) |
| 37 | */ |
| 38 | public class NeighborSolicitation 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 |
Charles Chan | 541801c | 2017-12-01 17:44:35 -0800 | [diff] [blame] | 41 | protected static final byte NDP_HOP_LIMIT = (byte) 255; |
Bartlomiej Goluszka | 08537a8 | 2017-06-12 15:18:32 +0200 | [diff] [blame] | 42 | protected static final byte RESERVED_CODE = (byte) 0x0; |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 43 | |
| 44 | protected byte[] targetAddress = new byte[Ip6Address.BYTE_LENGTH]; |
| 45 | |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 46 | private final NeighborDiscoveryOptions options = |
| 47 | new NeighborDiscoveryOptions(); |
| 48 | |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 49 | /** |
| 50 | * Gets target address. |
| 51 | * |
| 52 | * @return the target IPv6 address |
| 53 | */ |
| 54 | public byte[] getTargetAddress() { |
| 55 | return this.targetAddress; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Sets target address. |
| 60 | * |
| 61 | * @param targetAddress the target IPv6 address to set |
| 62 | * @return this |
| 63 | */ |
| 64 | public NeighborSolicitation setTargetAddress(final byte[] targetAddress) { |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 65 | this.targetAddress = |
| 66 | Arrays.copyOfRange(targetAddress, 0, Ip6Address.BYTE_LENGTH); |
| 67 | return this; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Gets the Neighbor Discovery Protocol packet options. |
| 72 | * |
| 73 | * @return the Neighbor Discovery Protocol packet options |
| 74 | */ |
| 75 | public List<NeighborDiscoveryOptions.Option> getOptions() { |
| 76 | return this.options.options(); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Adds a Neighbor Discovery Protocol packet option. |
| 81 | * |
| 82 | * @param type the option type |
| 83 | * @param data the option data |
| 84 | * @return this |
| 85 | */ |
| 86 | public NeighborSolicitation addOption(final byte type, |
| 87 | final byte[] data) { |
| 88 | this.options.addOption(type, data); |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 89 | return this; |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public byte[] serialize() { |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 94 | byte[] optionsData = null; |
| 95 | if (this.options.hasOptions()) { |
| 96 | optionsData = this.options.serialize(); |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 97 | } |
| 98 | |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 99 | int optionsLength = 0; |
| 100 | if (optionsData != null) { |
| 101 | optionsLength = optionsData.length; |
Charles M.C. Chan | 5c0b476 | 2015-01-10 18:38:37 +0800 | [diff] [blame] | 102 | } |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 103 | |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 104 | final byte[] data = new byte[HEADER_LENGTH + optionsLength]; |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 105 | final ByteBuffer bb = ByteBuffer.wrap(data); |
| 106 | |
| 107 | bb.putInt(0); |
| 108 | bb.put(this.targetAddress, 0, Ip6Address.BYTE_LENGTH); |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 109 | if (optionsData != null) { |
| 110 | bb.put(optionsData); |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | return data; |
| 114 | } |
| 115 | |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 116 | |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 117 | |
| 118 | /* |
| 119 | * (non-Javadoc) |
| 120 | * |
| 121 | * @see java.lang.Object#hashCode() |
| 122 | */ |
| 123 | @Override |
| 124 | public int hashCode() { |
| 125 | final int prime = 5807; |
| 126 | int result = super.hashCode(); |
| 127 | ByteBuffer bb; |
| 128 | bb = ByteBuffer.wrap(this.targetAddress); |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 129 | for (int i = 0; i < this.targetAddress.length / 4; i++) { |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 130 | result = prime * result + bb.getInt(); |
| 131 | } |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 132 | result = prime * result + this.options.hashCode(); |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 133 | return result; |
| 134 | } |
| 135 | |
| 136 | /* |
| 137 | * (non-Javadoc) |
| 138 | * |
| 139 | * @see java.lang.Object#equals(java.lang.Object) |
| 140 | */ |
| 141 | @Override |
| 142 | public boolean equals(final Object obj) { |
| 143 | if (this == obj) { |
| 144 | return true; |
| 145 | } |
| 146 | if (!super.equals(obj)) { |
| 147 | return false; |
| 148 | } |
| 149 | if (!(obj instanceof NeighborSolicitation)) { |
| 150 | return false; |
| 151 | } |
| 152 | final NeighborSolicitation other = (NeighborSolicitation) obj; |
| 153 | if (!Arrays.equals(this.targetAddress, other.targetAddress)) { |
| 154 | return false; |
| 155 | } |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 156 | if (!this.options.equals(other.options)) { |
| 157 | return false; |
| 158 | } |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 159 | return true; |
| 160 | } |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 161 | |
| 162 | /** |
| 163 | * Deserializer function for neighbor solicitation packets. |
| 164 | * |
| 165 | * @return deserializer function |
| 166 | */ |
| 167 | public static Deserializer<NeighborSolicitation> deserializer() { |
| 168 | return (data, offset, length) -> { |
| 169 | checkInput(data, offset, length, HEADER_LENGTH); |
| 170 | |
| 171 | NeighborSolicitation neighborSolicitation = new NeighborSolicitation(); |
| 172 | |
| 173 | ByteBuffer bb = ByteBuffer.wrap(data, offset, length); |
| 174 | |
| 175 | bb.getInt(); |
| 176 | bb.get(neighborSolicitation.targetAddress, 0, Ip6Address.BYTE_LENGTH); |
| 177 | |
Charles Chan | 3599d63 | 2015-09-05 14:47:51 +0800 | [diff] [blame] | 178 | if (bb.limit() - bb.position() > 0) { |
| 179 | NeighborDiscoveryOptions options = NeighborDiscoveryOptions.deserializer() |
| 180 | .deserialize(data, bb.position(), bb.limit() - bb.position()); |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 181 | |
Charles Chan | 3599d63 | 2015-09-05 14:47:51 +0800 | [diff] [blame] | 182 | for (NeighborDiscoveryOptions.Option option : options.options()) { |
| 183 | neighborSolicitation.addOption(option.type(), option.data()); |
| 184 | } |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | return neighborSolicitation; |
| 188 | }; |
| 189 | } |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 190 | |
| 191 | @Override |
| 192 | public String toString() { |
| 193 | return toStringHelper(getClass()) |
| 194 | .add("targetAddress", Arrays.toString(targetAddress)) |
| 195 | .toString(); |
| 196 | // TODO: need to handle options |
| 197 | } |
Pier Ventre | 78e73f6 | 2016-12-02 19:59:28 -0800 | [diff] [blame] | 198 | |
| 199 | /** |
| 200 | * Builds a NDP solicitation using the supplied parameters. |
| 201 | * |
| 202 | * @param targetIp the target ip |
| 203 | * @param sourceIp the source ip |
| 204 | * @param destinationIp the destination ip |
| 205 | * @param sourceMac the source mac address |
| 206 | * @param destinationMac the destination mac address |
| 207 | * @param vlan the vlan id |
| 208 | * @return the ethernet packet containing the ndp solicitation |
Bartlomiej Goluszka | 08537a8 | 2017-06-12 15:18:32 +0200 | [diff] [blame] | 209 | * @deprecated since 1.11.0 |
Pier Ventre | 78e73f6 | 2016-12-02 19:59:28 -0800 | [diff] [blame] | 210 | */ |
Bartlomiej Goluszka | 08537a8 | 2017-06-12 15:18:32 +0200 | [diff] [blame] | 211 | @Deprecated |
Pier Ventre | 78e73f6 | 2016-12-02 19:59:28 -0800 | [diff] [blame] | 212 | public static Ethernet buildNdpSolicit(byte[] targetIp, |
| 213 | byte[] sourceIp, |
| 214 | byte[] destinationIp, |
| 215 | byte[] sourceMac, |
| 216 | byte[] destinationMac, |
| 217 | VlanId vlan) { |
| 218 | |
| 219 | if (targetIp.length != Ip6Address.BYTE_LENGTH || |
| 220 | sourceIp.length != Ip6Address.BYTE_LENGTH || |
| 221 | destinationIp.length != Ip6Address.BYTE_LENGTH || |
| 222 | sourceMac.length != MacAddress.MAC_ADDRESS_LENGTH || |
| 223 | destinationMac.length != MacAddress.MAC_ADDRESS_LENGTH) { |
| 224 | return null; |
| 225 | } |
| 226 | |
Charles Chan | 4b87389 | 2017-05-25 18:30:09 -0700 | [diff] [blame] | 227 | // Here we craft the Ethernet packet. |
Pier Ventre | 78e73f6 | 2016-12-02 19:59:28 -0800 | [diff] [blame] | 228 | Ethernet ethernet = new Ethernet(); |
| 229 | ethernet.setEtherType(Ethernet.TYPE_IPV6) |
| 230 | .setDestinationMACAddress(destinationMac) |
| 231 | .setSourceMACAddress(sourceMac); |
| 232 | ethernet.setVlanID(vlan.id()); |
Charles Chan | 4b87389 | 2017-05-25 18:30:09 -0700 | [diff] [blame] | 233 | // IPv6 packet is created. |
Pier Ventre | 78e73f6 | 2016-12-02 19:59:28 -0800 | [diff] [blame] | 234 | IPv6 ipv6 = new IPv6(); |
| 235 | ipv6.setSourceAddress(sourceIp); |
| 236 | ipv6.setDestinationAddress(destinationIp); |
| 237 | ipv6.setHopLimit((byte) 255); |
Charles Chan | 4b87389 | 2017-05-25 18:30:09 -0700 | [diff] [blame] | 238 | // Create the ICMPv6 packet. |
Pier Ventre | 78e73f6 | 2016-12-02 19:59:28 -0800 | [diff] [blame] | 239 | ICMP6 icmp6 = new ICMP6(); |
| 240 | icmp6.setIcmpType(ICMP6.NEIGHBOR_SOLICITATION); |
| 241 | icmp6.setIcmpCode((byte) 0); |
Charles Chan | 4b87389 | 2017-05-25 18:30:09 -0700 | [diff] [blame] | 242 | // Create the Neighbor Solicitation packet. |
Pier Ventre | 78e73f6 | 2016-12-02 19:59:28 -0800 | [diff] [blame] | 243 | NeighborSolicitation ns = new NeighborSolicitation(); |
| 244 | ns.setTargetAddress(targetIp); |
Charles Chan | 4b87389 | 2017-05-25 18:30:09 -0700 | [diff] [blame] | 245 | // DAD packets should not contain SRC_LL_ADDR option |
| 246 | if (!Arrays.equals(sourceIp, Ip6Address.ZERO.toOctets())) { |
| 247 | ns.addOption(NeighborDiscoveryOptions.TYPE_SOURCE_LL_ADDRESS, sourceMac); |
| 248 | } |
| 249 | // Set the payloads |
Pier Ventre | 78e73f6 | 2016-12-02 19:59:28 -0800 | [diff] [blame] | 250 | icmp6.setPayload(ns); |
| 251 | ipv6.setPayload(icmp6); |
| 252 | ethernet.setPayload(ipv6); |
| 253 | |
| 254 | return ethernet; |
| 255 | } |
Bartlomiej Goluszka | 08537a8 | 2017-06-12 15:18:32 +0200 | [diff] [blame] | 256 | |
| 257 | /** |
| 258 | * Builds a NDP solicitation using the supplied parameters. |
| 259 | * |
| 260 | * @param targetIp the target ip |
| 261 | * @param sourceIp the source ip |
| 262 | * @param destinationIp the destination ip |
| 263 | * @param sourceMac the source mac address |
| 264 | * @param destinationMac the destination mac address |
| 265 | * @param vlan the vlan id |
| 266 | * @return the ethernet packet containing the ndp solicitation |
| 267 | */ |
| 268 | public static Ethernet buildNdpSolicit(Ip6Address targetIp, |
| 269 | Ip6Address sourceIp, |
| 270 | Ip6Address destinationIp, |
| 271 | MacAddress sourceMac, |
| 272 | MacAddress destinationMac, |
| 273 | VlanId vlan) { |
| 274 | |
| 275 | checkNotNull(targetIp, "Target IP address cannot be null"); |
| 276 | checkNotNull(sourceIp, "Source IP address cannot be null"); |
| 277 | checkNotNull(destinationIp, "Destination IP address cannot be null"); |
| 278 | checkNotNull(sourceMac, "Source MAC address cannot be null"); |
| 279 | checkNotNull(destinationMac, "Destination MAC address cannot be null"); |
| 280 | checkNotNull(vlan, "Vlan cannot be null"); |
| 281 | |
| 282 | // Here we craft the Ethernet packet. |
| 283 | Ethernet ethernet = new Ethernet(); |
| 284 | ethernet.setEtherType(Ethernet.TYPE_IPV6) |
| 285 | .setDestinationMACAddress(destinationMac) |
| 286 | .setSourceMACAddress(sourceMac); |
| 287 | ethernet.setVlanID(vlan.id()); |
| 288 | // IPv6 packet is created. |
| 289 | IPv6 ipv6 = new IPv6(); |
| 290 | ipv6.setSourceAddress(sourceIp.toOctets()); |
| 291 | ipv6.setDestinationAddress(destinationIp.toOctets()); |
| 292 | ipv6.setHopLimit(NDP_HOP_LIMIT); |
| 293 | // Create the ICMPv6 packet. |
| 294 | ICMP6 icmp6 = new ICMP6(); |
| 295 | icmp6.setIcmpType(ICMP6.NEIGHBOR_SOLICITATION); |
| 296 | icmp6.setIcmpCode(RESERVED_CODE); |
| 297 | // Create the Neighbor Solicitation packet. |
| 298 | NeighborSolicitation ns = new NeighborSolicitation(); |
| 299 | ns.setTargetAddress(targetIp.toOctets()); |
| 300 | // DAD packets should not contain SRC_LL_ADDR option |
| 301 | if (!Arrays.equals(sourceIp.toOctets(), Ip6Address.ZERO.toOctets())) { |
| 302 | ns.addOption(NeighborDiscoveryOptions.TYPE_SOURCE_LL_ADDRESS, sourceMac.toBytes()); |
| 303 | } |
| 304 | // Set the payloads |
| 305 | icmp6.setPayload(ns); |
| 306 | ipv6.setPayload(icmp6); |
| 307 | ethernet.setPayload(ipv6); |
| 308 | |
| 309 | return ethernet; |
| 310 | } |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 311 | } |