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 2014-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 | */ |
| 16 | |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 17 | package org.onlab.packet; |
| 18 | |
Charles M.C. Chan | 94f3737 | 2015-01-10 17:53:42 +0800 | [diff] [blame] | 19 | import org.onlab.packet.ipv6.Authentication; |
| 20 | import org.onlab.packet.ipv6.DestinationOptions; |
| 21 | import org.onlab.packet.ipv6.EncapSecurityPayload; |
| 22 | import org.onlab.packet.ipv6.Fragment; |
Charles M.C. Chan | 94f3737 | 2015-01-10 17:53:42 +0800 | [diff] [blame] | 23 | import org.onlab.packet.ipv6.HopByHopOptions; |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 24 | import org.onlab.packet.ipv6.IExtensionHeader; |
Charles M.C. Chan | 94f3737 | 2015-01-10 17:53:42 +0800 | [diff] [blame] | 25 | import org.onlab.packet.ipv6.Routing; |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 26 | |
Yuta HIGUCHI | bfc2e92 | 2017-06-07 21:46:05 -0700 | [diff] [blame] | 27 | import com.google.common.collect.ImmutableMap; |
| 28 | |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 29 | import java.nio.ByteBuffer; |
| 30 | import java.util.Arrays; |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 31 | import java.util.Map; |
| 32 | |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 33 | import static com.google.common.base.MoreObjects.toStringHelper; |
Pier Luigi | 37b687b | 2017-01-16 22:46:33 -0800 | [diff] [blame] | 34 | import static com.google.common.base.Preconditions.checkArgument; |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 35 | import static org.onlab.packet.PacketUtils.checkInput; |
| 36 | |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 37 | /** |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 38 | * Implements IPv6 packet format. (RFC 2460) |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 39 | */ |
Jian Li | 1270aea | 2016-09-15 13:32:24 +0900 | [diff] [blame] | 40 | public class IPv6 extends IP implements IExtensionHeader { |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 41 | public static final byte FIXED_HEADER_LENGTH = 40; // bytes |
| 42 | |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 43 | public static final byte PROTOCOL_TCP = 0x6; |
| 44 | public static final byte PROTOCOL_UDP = 0x11; |
| 45 | public static final byte PROTOCOL_ICMP6 = 0x3A; |
Charles M.C. Chan | 94f3737 | 2015-01-10 17:53:42 +0800 | [diff] [blame] | 46 | public static final byte PROTOCOL_HOPOPT = 0x00; |
| 47 | public static final byte PROTOCOL_ROUTING = 0x2B; |
| 48 | public static final byte PROTOCOL_FRAG = 0x2C; |
| 49 | public static final byte PROTOCOL_ESP = 0x32; |
| 50 | public static final byte PROTOCOL_AH = 0x33; |
| 51 | public static final byte PROTOCOL_DSTOPT = 0x3C; |
| 52 | |
Pier Luigi | 37b687b | 2017-01-16 22:46:33 -0800 | [diff] [blame] | 53 | public static final byte LINK_LOCAL_0 = (byte) 0xfe; |
| 54 | public static final byte LINK_LOCAL_1 = (byte) 0x80; |
Charles M.C. Chan | 94f3737 | 2015-01-10 17:53:42 +0800 | [diff] [blame] | 55 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 56 | public static final Map<Byte, Deserializer<? extends IPacket>> PROTOCOL_DESERIALIZER_MAP = |
Yuta HIGUCHI | bfc2e92 | 2017-06-07 21:46:05 -0700 | [diff] [blame] | 57 | ImmutableMap.<Byte, Deserializer<? extends IPacket>>builder() |
| 58 | .put(IPv6.PROTOCOL_ICMP6, ICMP6.deserializer()) |
| 59 | .put(IPv6.PROTOCOL_TCP, TCP.deserializer()) |
| 60 | .put(IPv6.PROTOCOL_UDP, UDP.deserializer()) |
| 61 | .put(IPv6.PROTOCOL_HOPOPT, HopByHopOptions.deserializer()) |
| 62 | .put(IPv6.PROTOCOL_ROUTING, Routing.deserializer()) |
| 63 | .put(IPv6.PROTOCOL_FRAG, Fragment.deserializer()) |
| 64 | .put(IPv6.PROTOCOL_ESP, EncapSecurityPayload.deserializer()) |
| 65 | .put(IPv6.PROTOCOL_AH, Authentication.deserializer()) |
| 66 | .put(IPv6.PROTOCOL_DSTOPT, DestinationOptions.deserializer()) |
| 67 | .build(); |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 68 | |
| 69 | protected byte version; |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 70 | protected byte trafficClass; |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 71 | protected int flowLabel; |
| 72 | protected short payloadLength; |
| 73 | protected byte nextHeader; |
| 74 | protected byte hopLimit; |
| 75 | protected byte[] sourceAddress = new byte[Ip6Address.BYTE_LENGTH]; |
| 76 | protected byte[] destinationAddress = new byte[Ip6Address.BYTE_LENGTH]; |
| 77 | |
| 78 | /** |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 79 | * Default constructor that sets the version to 6. |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 80 | */ |
| 81 | public IPv6() { |
| 82 | super(); |
| 83 | this.version = 6; |
| 84 | } |
| 85 | |
Jian Li | 1270aea | 2016-09-15 13:32:24 +0900 | [diff] [blame] | 86 | @Override |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 87 | public byte getVersion() { |
| 88 | return this.version; |
| 89 | } |
| 90 | |
Jian Li | 1270aea | 2016-09-15 13:32:24 +0900 | [diff] [blame] | 91 | @Override |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 92 | public IPv6 setVersion(final byte version) { |
| 93 | this.version = version; |
| 94 | return this; |
| 95 | } |
| 96 | |
| 97 | /** |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 98 | * Gets traffic class. |
| 99 | * |
| 100 | * @return the traffic class |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 101 | */ |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 102 | public byte getTrafficClass() { |
| 103 | return this.trafficClass; |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | /** |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 107 | * Sets traffic class. |
| 108 | * |
| 109 | * @param trafficClass the traffic class to set |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 110 | * @return this |
| 111 | */ |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 112 | public IPv6 setTrafficClass(final byte trafficClass) { |
| 113 | this.trafficClass = trafficClass; |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 114 | return this; |
| 115 | } |
| 116 | |
| 117 | /** |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 118 | * Gets flow label. |
| 119 | * |
| 120 | * @return the flow label |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 121 | */ |
| 122 | public int getFlowLabel() { |
| 123 | return this.flowLabel; |
| 124 | } |
| 125 | |
| 126 | /** |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 127 | * Sets flow label. |
| 128 | * |
| 129 | * @param flowLabel the flow label to set |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 130 | * @return this |
| 131 | */ |
| 132 | public IPv6 setFlowLabel(final int flowLabel) { |
| 133 | this.flowLabel = flowLabel; |
| 134 | return this; |
| 135 | } |
| 136 | |
Charles M.C. Chan | 94f3737 | 2015-01-10 17:53:42 +0800 | [diff] [blame] | 137 | @Override |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 138 | public byte getNextHeader() { |
| 139 | return this.nextHeader; |
| 140 | } |
| 141 | |
Charles M.C. Chan | 94f3737 | 2015-01-10 17:53:42 +0800 | [diff] [blame] | 142 | @Override |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 143 | public IPv6 setNextHeader(final byte nextHeader) { |
| 144 | this.nextHeader = nextHeader; |
| 145 | return this; |
| 146 | } |
| 147 | |
| 148 | /** |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 149 | * Gets hop limit. |
| 150 | * |
| 151 | * @return the hop limit |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 152 | */ |
| 153 | public byte getHopLimit() { |
| 154 | return this.hopLimit; |
| 155 | } |
| 156 | |
| 157 | /** |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 158 | * Sets hop limit. |
| 159 | * |
| 160 | * @param hopLimit the hop limit to set |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 161 | * @return this |
| 162 | */ |
| 163 | public IPv6 setHopLimit(final byte hopLimit) { |
| 164 | this.hopLimit = hopLimit; |
| 165 | return this; |
| 166 | } |
| 167 | |
| 168 | /** |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 169 | * Gets source address. |
| 170 | * |
| 171 | * @return the IPv6 source address |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 172 | */ |
| 173 | public byte[] getSourceAddress() { |
| 174 | return this.sourceAddress; |
| 175 | } |
| 176 | |
| 177 | /** |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 178 | * Sets source address. |
| 179 | * |
| 180 | * @param sourceAddress the IPv6 source address to set |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 181 | * @return this |
| 182 | */ |
| 183 | public IPv6 setSourceAddress(final byte[] sourceAddress) { |
| 184 | this.sourceAddress = Arrays.copyOfRange(sourceAddress, 0, Ip6Address.BYTE_LENGTH); |
| 185 | return this; |
| 186 | } |
| 187 | |
| 188 | /** |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 189 | * Gets destination address. |
| 190 | * |
| 191 | * @return the IPv6 destination address |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 192 | */ |
| 193 | public byte[] getDestinationAddress() { |
| 194 | return this.destinationAddress; |
| 195 | } |
| 196 | |
| 197 | /** |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 198 | * Sets destination address. |
| 199 | * |
| 200 | * @param destinationAddress the IPv6 destination address to set |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 201 | * @return this |
| 202 | */ |
| 203 | public IPv6 setDestinationAddress(final byte[] destinationAddress) { |
| 204 | this.destinationAddress = Arrays.copyOfRange(destinationAddress, 0, Ip6Address.BYTE_LENGTH); |
| 205 | return this; |
| 206 | } |
| 207 | |
| 208 | @Override |
| 209 | public byte[] serialize() { |
| 210 | byte[] payloadData = null; |
| 211 | if (this.payload != null) { |
| 212 | this.payload.setParent(this); |
| 213 | payloadData = this.payload.serialize(); |
| 214 | } |
| 215 | |
Charles M.C. Chan | 5c0b476 | 2015-01-10 18:38:37 +0800 | [diff] [blame] | 216 | this.payloadLength = 0; |
| 217 | if (payloadData != null) { |
| 218 | this.payloadLength = (short) payloadData.length; |
| 219 | } |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 220 | |
| 221 | final byte[] data = new byte[FIXED_HEADER_LENGTH + payloadLength]; |
| 222 | final ByteBuffer bb = ByteBuffer.wrap(data); |
| 223 | |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 224 | bb.putInt((this.version & 0xf) << 28 | (this.trafficClass & 0xff) << 20 | this.flowLabel & 0xfffff); |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 225 | bb.putShort(this.payloadLength); |
| 226 | bb.put(this.nextHeader); |
| 227 | bb.put(this.hopLimit); |
| 228 | bb.put(this.sourceAddress, 0, Ip6Address.BYTE_LENGTH); |
| 229 | bb.put(this.destinationAddress, 0, Ip6Address.BYTE_LENGTH); |
| 230 | |
| 231 | if (payloadData != null) { |
| 232 | bb.put(payloadData); |
| 233 | } |
| 234 | |
| 235 | return data; |
| 236 | } |
| 237 | |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 238 | /* |
| 239 | * (non-Javadoc) |
| 240 | * |
| 241 | * @see java.lang.Object#hashCode() |
| 242 | */ |
| 243 | @Override |
| 244 | public int hashCode() { |
| 245 | final int prime = 2521; |
| 246 | int result = super.hashCode(); |
| 247 | ByteBuffer bb; |
| 248 | bb = ByteBuffer.wrap(this.destinationAddress); |
| 249 | for (int i = 0; i < 4; i++) { |
| 250 | result = prime * result + bb.getInt(); |
| 251 | } |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 252 | result = prime * result + this.trafficClass; |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 253 | result = prime * result + this.flowLabel; |
| 254 | result = prime * result + this.hopLimit; |
| 255 | result = prime * result + this.nextHeader; |
| 256 | result = prime * result + this.payloadLength; |
| 257 | bb = ByteBuffer.wrap(this.sourceAddress); |
| 258 | for (int i = 0; i < 4; i++) { |
| 259 | result = prime * result + bb.getInt(); |
| 260 | } |
| 261 | result = prime * result + this.version; |
| 262 | return result; |
| 263 | } |
| 264 | |
| 265 | /* |
| 266 | * (non-Javadoc) |
| 267 | * |
| 268 | * @see java.lang.Object#equals(java.lang.Object) |
| 269 | */ |
| 270 | @Override |
| 271 | public boolean equals(final Object obj) { |
| 272 | if (this == obj) { |
| 273 | return true; |
| 274 | } |
| 275 | if (!super.equals(obj)) { |
| 276 | return false; |
| 277 | } |
| 278 | if (!(obj instanceof IPv6)) { |
| 279 | return false; |
| 280 | } |
| 281 | final IPv6 other = (IPv6) obj; |
| 282 | if (!Arrays.equals(this.destinationAddress, other.destinationAddress)) { |
| 283 | return false; |
| 284 | } |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 285 | if (this.trafficClass != other.trafficClass) { |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 286 | return false; |
| 287 | } |
| 288 | if (this.flowLabel != other.flowLabel) { |
| 289 | return false; |
| 290 | } |
| 291 | if (this.hopLimit != other.hopLimit) { |
| 292 | return false; |
| 293 | } |
| 294 | if (this.nextHeader != other.nextHeader) { |
| 295 | return false; |
| 296 | } |
| 297 | if (this.payloadLength != other.payloadLength) { |
| 298 | return false; |
| 299 | } |
| 300 | if (!Arrays.equals(this.sourceAddress, other.sourceAddress)) { |
| 301 | return false; |
| 302 | } |
| 303 | return true; |
| 304 | } |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 305 | |
| 306 | /** |
| 307 | * Deserializer function for IPv6 packets. |
| 308 | * |
| 309 | * @return deserializer function |
| 310 | */ |
| 311 | public static Deserializer<IPv6> deserializer() { |
| 312 | return (data, offset, length) -> { |
| 313 | checkInput(data, offset, length, FIXED_HEADER_LENGTH); |
| 314 | |
| 315 | IPv6 ipv6 = new IPv6(); |
| 316 | |
| 317 | ByteBuffer bb = ByteBuffer.wrap(data, offset, length); |
| 318 | |
| 319 | int iscratch = bb.getInt(); |
| 320 | |
| 321 | ipv6.version = (byte) (iscratch >> 28 & 0xf); |
| 322 | ipv6.trafficClass = (byte) (iscratch >> 20 & 0xff); |
| 323 | ipv6.flowLabel = iscratch & 0xfffff; |
| 324 | ipv6.payloadLength = bb.getShort(); |
| 325 | ipv6.nextHeader = bb.get(); |
| 326 | ipv6.hopLimit = bb.get(); |
| 327 | bb.get(ipv6.sourceAddress, 0, Ip6Address.BYTE_LENGTH); |
| 328 | bb.get(ipv6.destinationAddress, 0, Ip6Address.BYTE_LENGTH); |
| 329 | |
| 330 | Deserializer<? extends IPacket> deserializer; |
| 331 | if (IPv6.PROTOCOL_DESERIALIZER_MAP.containsKey(ipv6.nextHeader)) { |
| 332 | deserializer = IPv6.PROTOCOL_DESERIALIZER_MAP.get(ipv6.nextHeader); |
| 333 | } else { |
| 334 | deserializer = Data.deserializer(); |
| 335 | } |
Charles Chan | 89c4a64 | 2017-04-13 17:43:52 -0700 | [diff] [blame] | 336 | |
| 337 | int remainingLength = bb.limit() - bb.position(); |
| 338 | int payloadLength = ipv6.payloadLength; |
| 339 | int bytesToRead = (payloadLength <= remainingLength) ? |
| 340 | payloadLength : remainingLength; |
| 341 | ipv6.payload = deserializer.deserialize(data, bb.position(), bytesToRead); |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 342 | ipv6.payload.setParent(ipv6); |
| 343 | |
| 344 | return ipv6; |
| 345 | }; |
| 346 | } |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 347 | |
| 348 | @Override |
| 349 | public String toString() { |
| 350 | return toStringHelper(getClass()) |
| 351 | .add("version", Byte.toString(version)) |
| 352 | .add("trafficClass", Byte.toString(trafficClass)) |
| 353 | .add("flowLabel", Integer.toString(flowLabel)) |
| 354 | .add("payloadLength", Short.toString(payloadLength)) |
| 355 | .add("nextHeader", Byte.toString(nextHeader)) |
| 356 | .add("hopLimit", Byte.toString(hopLimit)) |
| 357 | .add("sourceAddress", Arrays.toString(sourceAddress)) |
| 358 | .add("destinationAddress", Arrays.toString(destinationAddress)) |
| 359 | .toString(); |
| 360 | } |
Pier Ventre | 78e73f6 | 2016-12-02 19:59:28 -0800 | [diff] [blame] | 361 | |
| 362 | /** |
| 363 | * According to the RFC 4291, the solicitation node addresses are |
| 364 | * formed by taking the low-order 24 bits of an address (unicast or anycast) |
| 365 | * and appending those bits to the prefix FF02:0:0:0:0:1:FF00::/104. |
| 366 | * |
| 367 | * Solicited-Node Address: FF02:0:0:0:0:1:FFXX:XXXX |
| 368 | * |
| 369 | * @param targetIp the unicast or anycast address |
| 370 | * @return the computed solicitation node address |
| 371 | */ |
Pier Luigi | 37b687b | 2017-01-16 22:46:33 -0800 | [diff] [blame] | 372 | public static byte[] getSolicitNodeAddress(byte[] targetIp) { |
| 373 | checkArgument(targetIp.length == Ip6Address.BYTE_LENGTH); |
| 374 | return new byte[] { |
Pier Ventre | 78e73f6 | 2016-12-02 19:59:28 -0800 | [diff] [blame] | 375 | (byte) 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 376 | 0x00, 0x00, 0x00, 0x01, (byte) 0xff, |
| 377 | targetIp[targetIp.length - 3], |
| 378 | targetIp[targetIp.length - 2], |
| 379 | targetIp[targetIp.length - 1] |
| 380 | }; |
| 381 | } |
| 382 | |
| 383 | /** |
| 384 | * According to the RFC 2464, an IPv6 packet with a multicast |
| 385 | * destination address DST, consisting of the sixteen octets DST[1] |
| 386 | * through DST[16], is transmitted to the Ethernet multicast address |
| 387 | * whose first two octets are the value 3333 hexadecimal and whose last |
| 388 | * four octets are the last four octets of DST. |
| 389 | * |
| 390 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 391 | * |0 0 1 1 0 0 1 1|0 0 1 1 0 0 1 1| |
| 392 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 393 | * | DST[13] | DST[14] | |
| 394 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 395 | * | DST[15] | DST[16] | |
| 396 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 397 | * |
| 398 | * @param targetIp the multicast address. |
| 399 | * @return the multicast mac address |
| 400 | */ |
Pier Luigi | 37b687b | 2017-01-16 22:46:33 -0800 | [diff] [blame] | 401 | public static byte[] getMCastMacAddress(byte[] targetIp) { |
| 402 | checkArgument(targetIp.length == Ip6Address.BYTE_LENGTH); |
| 403 | return new byte[] { |
Pier Ventre | 78e73f6 | 2016-12-02 19:59:28 -0800 | [diff] [blame] | 404 | 0x33, 0x33, |
| 405 | targetIp[targetIp.length - 4], |
| 406 | targetIp[targetIp.length - 3], |
| 407 | targetIp[targetIp.length - 2], |
| 408 | targetIp[targetIp.length - 1], |
| 409 | }; |
| 410 | } |
Pier Luigi | 37b687b | 2017-01-16 22:46:33 -0800 | [diff] [blame] | 411 | |
| 412 | /** |
| 413 | * According to the RFC 4291, an IPv6 link local address is an IPv6 |
| 414 | * unicast address that can be automatically configured on any interface |
| 415 | * using the link-local prefix FE80::/10 (1111 1110 10) and the interface |
| 416 | * identifier in the modified EUI-64 format. |
| 417 | * |
| 418 | * +----------------------------------------------------------------+ |
| 419 | * | 10 bits | 54 bits | 64 bits | |
| 420 | * +----------- +-------------------------+-------------------------+ |
| 421 | * | 1111111010 | 0 | interface ID | |
| 422 | * +----------- +-------------------------+-------------------------+ |
| 423 | * |
| 424 | * @param targetIp the ip address to verify |
| 425 | * @return true if the ipv6 address is link local, |
| 426 | * false otherwise |
| 427 | */ |
| 428 | public static boolean isLinkLocalAddress(byte[] targetIp) { |
| 429 | checkArgument(targetIp.length == Ip6Address.BYTE_LENGTH); |
| 430 | return (targetIp[0] & 0xff) == 0xfe && (targetIp[1] & 0xc0) == 0x80; |
| 431 | } |
| 432 | |
| 433 | /** |
| 434 | * Returns the auto-generated link local address using the |
| 435 | * mac address as parameter. |
| 436 | * |
| 437 | * @param macAddress the mac address to use |
| 438 | * @return the ipv6 link local address |
| 439 | */ |
| 440 | public static byte[] getLinkLocalAddress(byte[] macAddress) { |
| 441 | checkArgument(macAddress.length == MacAddress.MAC_ADDRESS_LENGTH); |
| 442 | return new byte[] { |
| 443 | LINK_LOCAL_0, |
| 444 | LINK_LOCAL_1, |
| 445 | 0, 0, 0, 0, 0, 0, |
| 446 | (byte) (macAddress[0] ^ (1 << 1)), |
| 447 | macAddress[1], |
| 448 | macAddress[2], |
| 449 | (byte) 0xff, |
| 450 | (byte) 0xfe, |
| 451 | macAddress[3], |
| 452 | macAddress[4], |
| 453 | macAddress[5], |
| 454 | }; |
| 455 | } |
| 456 | |
| 457 | /** |
| 458 | * Returns the mac address from the auto-generated |
| 459 | * link local address. |
| 460 | * |
| 461 | * @param linkLocalAddress the ipv6 to use |
| 462 | * @return the mac address |
| 463 | */ |
| 464 | public static byte[] getMacAddress(byte[] linkLocalAddress) { |
| 465 | return !isLinkLocalAddress(linkLocalAddress) ? null : new byte[] { |
| 466 | (byte) (linkLocalAddress[8] ^ (1 << 1)), |
| 467 | linkLocalAddress[9], |
| 468 | linkLocalAddress[10], |
| 469 | linkLocalAddress[13], |
| 470 | linkLocalAddress[14], |
| 471 | linkLocalAddress[15], |
| 472 | }; |
| 473 | } |
| 474 | |
| 475 | |
| 476 | |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 477 | } |