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