Rusty Eddy | 1da61a2 | 2015-09-01 00:48:58 +0000 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Rusty Eddy | 1da61a2 | 2015-09-01 00:48:58 +0000 | [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 | package org.onlab.packet; |
| 17 | |
Luca Prete | c8ec4b5 | 2016-09-22 16:40:13 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
Jonathan Hart | 81d7310 | 2016-02-19 10:32:05 -0800 | [diff] [blame] | 19 | import org.slf4j.Logger; |
| 20 | |
Rusty Eddy | 1da61a2 | 2015-09-01 00:48:58 +0000 | [diff] [blame] | 21 | import java.nio.ByteBuffer; |
| 22 | import java.util.ArrayList; |
Ray Milkey | 9f87e51 | 2016-01-05 10:00:22 -0800 | [diff] [blame] | 23 | import java.util.Arrays; |
Rusty Eddy | 1da61a2 | 2015-09-01 00:48:58 +0000 | [diff] [blame] | 24 | import java.util.List; |
Ray Milkey | 9f87e51 | 2016-01-05 10:00:22 -0800 | [diff] [blame] | 25 | |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 26 | import static com.google.common.base.MoreObjects.toStringHelper; |
Rusty Eddy | 1da61a2 | 2015-09-01 00:48:58 +0000 | [diff] [blame] | 27 | import static com.google.common.base.Preconditions.checkNotNull; |
| 28 | import static org.onlab.packet.PacketUtils.checkInput; |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 29 | import static org.slf4j.LoggerFactory.getLogger; |
Rusty Eddy | 1da61a2 | 2015-09-01 00:48:58 +0000 | [diff] [blame] | 30 | |
Rusty Eddy | 1da61a2 | 2015-09-01 00:48:58 +0000 | [diff] [blame] | 31 | /** |
| 32 | * Implements IGMP control packet format. |
| 33 | */ |
Luca Prete | c8ec4b5 | 2016-09-22 16:40:13 -0700 | [diff] [blame] | 34 | public abstract class IGMP extends BasePacket { |
| 35 | protected static final Logger log = getLogger(IGMP.class); |
Rusty Eddy | 1da61a2 | 2015-09-01 00:48:58 +0000 | [diff] [blame] | 36 | |
| 37 | public static final byte TYPE_IGMPV3_MEMBERSHIP_QUERY = 0x11; |
| 38 | public static final byte TYPE_IGMPV1_MEMBERSHIP_REPORT = 0x12; |
| 39 | public static final byte TYPE_IGMPV2_MEMBERSHIP_REPORT = 0x16; |
| 40 | public static final byte TYPE_IGMPV2_LEAVE_GROUP = 0x17; |
| 41 | public static final byte TYPE_IGMPV3_MEMBERSHIP_REPORT = 0x22; |
Rusty Eddy | 1da61a2 | 2015-09-01 00:48:58 +0000 | [diff] [blame] | 42 | |
| 43 | List<IGMPGroup> groups = new ArrayList<>(); |
| 44 | |
| 45 | // Fields contained in the IGMP header |
Luca Prete | c8ec4b5 | 2016-09-22 16:40:13 -0700 | [diff] [blame] | 46 | protected byte igmpType; |
| 47 | protected byte resField = 0; |
| 48 | protected short checksum = 0; |
Rusty Eddy | 1da61a2 | 2015-09-01 00:48:58 +0000 | [diff] [blame] | 49 | |
| 50 | private byte[] unsupportTypeData; |
| 51 | |
| 52 | public IGMP() { |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Get the IGMP message type. |
| 57 | * |
| 58 | * @return the IGMP message type |
| 59 | */ |
| 60 | public byte getIgmpType() { |
| 61 | return igmpType; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Set the IGMP message type. |
| 66 | * |
| 67 | * @param msgType IGMP message type |
| 68 | */ |
| 69 | public void setIgmpType(byte msgType) { |
| 70 | igmpType = msgType; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Get the checksum of this message. |
| 75 | * |
| 76 | * @return the checksum |
| 77 | */ |
| 78 | public short getChecksum() { |
| 79 | return checksum; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * get the Max Resp Code. |
| 84 | * |
| 85 | * @return The Maximum Time allowed before before sending a responding report. |
| 86 | */ |
| 87 | public byte getMaxRespField() { |
| 88 | return resField; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Set the Max Resp Code. |
| 93 | * |
| 94 | * @param respCode the Maximum Response Code. |
| 95 | */ |
Luca Prete | c8ec4b5 | 2016-09-22 16:40:13 -0700 | [diff] [blame] | 96 | public abstract void setMaxRespCode(byte respCode); |
Rusty Eddy | 1da61a2 | 2015-09-01 00:48:58 +0000 | [diff] [blame] | 97 | |
| 98 | /** |
| 99 | * Get the list of IGMPGroups. The group objects will be either IGMPQuery or IGMPMembership |
| 100 | * depending on the IGMP message type. For IGMP Query, the groups list should only be |
| 101 | * one group. |
| 102 | * |
| 103 | * @return The list of IGMP groups. |
| 104 | */ |
| 105 | public List<IGMPGroup> getGroups() { |
Luca Prete | c8ec4b5 | 2016-09-22 16:40:13 -0700 | [diff] [blame] | 106 | return ImmutableList.copyOf(groups); |
Rusty Eddy | 1da61a2 | 2015-09-01 00:48:58 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Add a multicast group to this IGMP message. |
| 111 | * |
| 112 | * @param group the IGMPGroup will be IGMPQuery or IGMPMembership depending on the message type. |
| 113 | * @return true if group was valid and added, false otherwise. |
| 114 | */ |
Luca Prete | c8ec4b5 | 2016-09-22 16:40:13 -0700 | [diff] [blame] | 115 | public abstract boolean addGroup(IGMPGroup group); |
Rusty Eddy | 1da61a2 | 2015-09-01 00:48:58 +0000 | [diff] [blame] | 116 | |
| 117 | /** |
| 118 | * Serialize this IGMP packet. This will take care |
| 119 | * of serializing IGMPv3 Queries and IGMPv3 Membership |
| 120 | * Reports. |
| 121 | * |
| 122 | * @return the serialized IGMP message |
| 123 | */ |
Ray Milkey | aef4585 | 2016-01-11 17:13:19 -0800 | [diff] [blame] | 124 | @java.lang.SuppressWarnings("squid:S128") // suppress switch fall through warning |
Rusty Eddy | 1da61a2 | 2015-09-01 00:48:58 +0000 | [diff] [blame] | 125 | @Override |
| 126 | public byte[] serialize() { |
Jian Li | 68c4fc4 | 2016-01-11 16:07:03 -0800 | [diff] [blame] | 127 | byte[] data = new byte[8915]; |
Rusty Eddy | 1da61a2 | 2015-09-01 00:48:58 +0000 | [diff] [blame] | 128 | |
| 129 | ByteBuffer bb = ByteBuffer.wrap(data); |
| 130 | bb.put(this.getIgmpType()); |
| 131 | |
| 132 | // reserved or max resp code depending on type. |
| 133 | bb.put(this.resField); |
| 134 | |
| 135 | // Must calculate checksum |
| 136 | bb.putShort((short) 0); |
| 137 | |
Luca Prete | c8ec4b5 | 2016-09-22 16:40:13 -0700 | [diff] [blame] | 138 | if (this instanceof IGMPv3) { |
| 139 | switch (this.igmpType) { |
Jonathan Hart | 81d7310 | 2016-02-19 10:32:05 -0800 | [diff] [blame] | 140 | |
Luca Prete | c8ec4b5 | 2016-09-22 16:40:13 -0700 | [diff] [blame] | 141 | case IGMP.TYPE_IGMPV3_MEMBERSHIP_REPORT: |
| 142 | // reserved |
| 143 | bb.putShort((short) 0); |
| 144 | // Number of groups |
| 145 | bb.putShort((short) groups.size()); |
| 146 | // Fall through |
Jonathan Hart | 81d7310 | 2016-02-19 10:32:05 -0800 | [diff] [blame] | 147 | |
Luca Prete | c8ec4b5 | 2016-09-22 16:40:13 -0700 | [diff] [blame] | 148 | case IGMP.TYPE_IGMPV3_MEMBERSHIP_QUERY: |
Rusty Eddy | 1da61a2 | 2015-09-01 00:48:58 +0000 | [diff] [blame] | 149 | |
Luca Prete | c8ec4b5 | 2016-09-22 16:40:13 -0700 | [diff] [blame] | 150 | for (IGMPGroup grp : groups) { |
| 151 | grp.serialize(bb); |
| 152 | } |
| 153 | break; |
Rusty Eddy | 1da61a2 | 2015-09-01 00:48:58 +0000 | [diff] [blame] | 154 | |
Luca Prete | c8ec4b5 | 2016-09-22 16:40:13 -0700 | [diff] [blame] | 155 | default: |
| 156 | bb.put(this.unsupportTypeData); |
| 157 | break; |
| 158 | } |
| 159 | } else if (this instanceof IGMPv2) { |
| 160 | if (this.groups.isEmpty()) { |
| 161 | bb.putInt(0); |
| 162 | } else { |
| 163 | bb.putInt(groups.get(0).getGaddr().getIp4Address().toInt()); |
| 164 | } |
| 165 | } else { |
| 166 | throw new UnsupportedOperationException(); |
Rusty Eddy | 1da61a2 | 2015-09-01 00:48:58 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | int size = bb.position(); |
Jonathan Hart | 81d7310 | 2016-02-19 10:32:05 -0800 | [diff] [blame] | 170 | |
| 171 | // compute checksum if needed |
| 172 | if (this.checksum == 0) { |
| 173 | bb.rewind(); |
| 174 | int accumulation = 0; |
| 175 | for (int i = 0; i < size * 2; ++i) { |
| 176 | accumulation += 0xffff & bb.getShort(); |
| 177 | } |
| 178 | accumulation = (accumulation >> 16 & 0xffff) |
| 179 | + (accumulation & 0xffff); |
| 180 | this.checksum = (short) (~accumulation & 0xffff); |
| 181 | bb.putShort(2, this.checksum); |
| 182 | } |
| 183 | |
| 184 | |
Rusty Eddy | 1da61a2 | 2015-09-01 00:48:58 +0000 | [diff] [blame] | 185 | bb.position(0); |
Jian Li | 68c4fc4 | 2016-01-11 16:07:03 -0800 | [diff] [blame] | 186 | byte[] rdata = new byte[size]; |
Rusty Eddy | 1da61a2 | 2015-09-01 00:48:58 +0000 | [diff] [blame] | 187 | bb.get(rdata, 0, size); |
| 188 | return rdata; |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Deserialize an IGMP message. |
| 193 | * |
| 194 | * @param data bytes to deserialize |
| 195 | * @param offset offset to start deserializing from |
| 196 | * @param length length of the data to deserialize |
| 197 | * @return populated IGMP object |
| 198 | */ |
| 199 | @Override |
| 200 | public IPacket deserialize(final byte[] data, final int offset, |
| 201 | final int length) { |
| 202 | |
Luca Prete | c8ec4b5 | 2016-09-22 16:40:13 -0700 | [diff] [blame] | 203 | final IGMP igmp; |
Rusty Eddy | 1da61a2 | 2015-09-01 00:48:58 +0000 | [diff] [blame] | 204 | try { |
| 205 | igmp = IGMP.deserializer().deserialize(data, offset, length); |
| 206 | } catch (DeserializationException e) { |
Luca Prete | c8ec4b5 | 2016-09-22 16:40:13 -0700 | [diff] [blame] | 207 | log.error("Deserialization exception", e); |
Rusty Eddy | 1da61a2 | 2015-09-01 00:48:58 +0000 | [diff] [blame] | 208 | return this; |
| 209 | } |
| 210 | this.igmpType = igmp.igmpType; |
| 211 | this.resField = igmp.resField; |
| 212 | this.checksum = igmp.checksum; |
| 213 | this.groups = igmp.groups; |
| 214 | return this; |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Deserializer function for IPv4 packets. |
| 219 | * |
| 220 | * @return deserializer function |
| 221 | */ |
| 222 | public static Deserializer<IGMP> deserializer() { |
| 223 | return (data, offset, length) -> { |
Luca Prete | c8ec4b5 | 2016-09-22 16:40:13 -0700 | [diff] [blame] | 224 | checkInput(data, offset, length, IGMPv2.HEADER_LENGTH); |
Rusty Eddy | 1da61a2 | 2015-09-01 00:48:58 +0000 | [diff] [blame] | 225 | |
Luca Prete | c8ec4b5 | 2016-09-22 16:40:13 -0700 | [diff] [blame] | 226 | // we will assume that this is IGMPv2 if the length is 8 |
| 227 | boolean isV2 = length == IGMPv2.HEADER_LENGTH; |
| 228 | |
| 229 | IGMP igmp = isV2 ? new IGMPv2() : new IGMPv3(); |
Rusty Eddy | 1da61a2 | 2015-09-01 00:48:58 +0000 | [diff] [blame] | 230 | |
Rusty Eddy | 158d5d8 | 2015-10-12 16:59:04 -0700 | [diff] [blame] | 231 | final ByteBuffer bb = ByteBuffer.wrap(data, offset, length); |
Rusty Eddy | 1da61a2 | 2015-09-01 00:48:58 +0000 | [diff] [blame] | 232 | igmp.igmpType = bb.get(); |
| 233 | igmp.resField = bb.get(); |
| 234 | igmp.checksum = bb.getShort(); |
Jonathan Hart | 81d7310 | 2016-02-19 10:32:05 -0800 | [diff] [blame] | 235 | |
Luca Prete | c8ec4b5 | 2016-09-22 16:40:13 -0700 | [diff] [blame] | 236 | if (isV2) { |
| 237 | igmp.addGroup(new IGMPQuery(IpAddress.valueOf(bb.getInt()), 0)); |
| 238 | if (igmp.validChecksum()) { |
| 239 | return igmp; |
| 240 | } |
| 241 | throw new DeserializationException("invalid checksum"); |
| 242 | } |
| 243 | |
| 244 | // second check for IGMPv3 |
| 245 | checkInput(data, offset, length, IGMPv3.MINIMUM_HEADER_LEN); |
| 246 | |
Rusty Eddy | 1da61a2 | 2015-09-01 00:48:58 +0000 | [diff] [blame] | 247 | String msg; |
| 248 | |
| 249 | switch (igmp.igmpType) { |
| 250 | |
| 251 | case TYPE_IGMPV3_MEMBERSHIP_QUERY: |
| 252 | IGMPQuery qgroup = new IGMPQuery(); |
| 253 | qgroup.deserialize(bb); |
| 254 | igmp.groups.add(qgroup); |
| 255 | break; |
| 256 | |
| 257 | case TYPE_IGMPV3_MEMBERSHIP_REPORT: |
| 258 | bb.getShort(); // Ignore resvd |
| 259 | int ngrps = bb.getShort(); |
| 260 | |
| 261 | for (; ngrps > 0; ngrps--) { |
| 262 | IGMPMembership mgroup = new IGMPMembership(); |
| 263 | mgroup.deserialize(bb); |
| 264 | igmp.groups.add(mgroup); |
| 265 | } |
| 266 | break; |
| 267 | |
| 268 | /* |
| 269 | * NOTE: according to the IGMPv3 spec. These previous IGMP type fields |
| 270 | * must be supported. At this time we are going to <b>assume</b> we run |
| 271 | * in a modern network where all devices are IGMPv3 capable. |
| 272 | */ |
| 273 | case TYPE_IGMPV1_MEMBERSHIP_REPORT: |
| 274 | case TYPE_IGMPV2_MEMBERSHIP_REPORT: |
| 275 | case TYPE_IGMPV2_LEAVE_GROUP: |
| 276 | igmp.unsupportTypeData = bb.array(); // Is this the entire array? |
| 277 | msg = "IGMP message type: " + igmp.igmpType + " is not supported"; |
| 278 | igmp.log.debug(msg); |
| 279 | break; |
| 280 | |
| 281 | default: |
alshabib | 0588e57 | 2015-09-04 12:00:42 -0700 | [diff] [blame] | 282 | msg = "IGMP message type: " + igmp.igmpType + " is not recognized"; |
| 283 | igmp.unsupportTypeData = bb.array(); |
Rusty Eddy | 1da61a2 | 2015-09-01 00:48:58 +0000 | [diff] [blame] | 284 | igmp.log.debug(msg); |
| 285 | break; |
| 286 | } |
| 287 | return igmp; |
| 288 | }; |
| 289 | } |
| 290 | |
Luca Prete | c8ec4b5 | 2016-09-22 16:40:13 -0700 | [diff] [blame] | 291 | /** |
| 292 | * Validates the message's checksum. |
| 293 | * |
| 294 | * @return true if valid, false if not |
| 295 | */ |
| 296 | protected abstract boolean validChecksum(); |
| 297 | |
Rusty Eddy | 1da61a2 | 2015-09-01 00:48:58 +0000 | [diff] [blame] | 298 | /* |
| 299 | * (non-Javadoc) |
| 300 | * |
| 301 | * @see java.lang.Object#equals(java.lang.Object) |
| 302 | */ |
| 303 | @Override |
| 304 | public boolean equals(final Object obj) { |
| 305 | if (this == obj) { |
| 306 | return true; |
| 307 | } |
| 308 | if (!super.equals(obj)) { |
| 309 | return false; |
| 310 | } |
| 311 | if (!(obj instanceof IGMP)) { |
| 312 | return false; |
| 313 | } |
| 314 | final IGMP other = (IGMP) obj; |
| 315 | if (this.igmpType != other.igmpType) { |
| 316 | return false; |
| 317 | } |
| 318 | if (this.resField != other.resField) { |
| 319 | return false; |
| 320 | } |
| 321 | if (this.checksum != other.checksum) { |
| 322 | return false; |
| 323 | } |
| 324 | if (this.groups.size() != other.groups.size()) { |
| 325 | return false; |
| 326 | } |
| 327 | // TODO: equals should be true regardless of order. |
| 328 | if (!groups.equals(other.groups)) { |
| 329 | return false; |
| 330 | } |
| 331 | return true; |
| 332 | } |
| 333 | |
| 334 | /* |
| 335 | * (non-Javadoc) |
| 336 | * |
| 337 | * @see java.lang.Object#hashCode() |
| 338 | */ |
| 339 | @Override |
| 340 | public int hashCode() { |
| 341 | final int prime = 2521; |
| 342 | int result = super.hashCode(); |
| 343 | result = prime * result + this.igmpType; |
| 344 | result = prime * result + this.groups.size(); |
| 345 | result = prime * result + this.resField; |
| 346 | result = prime * result + this.checksum; |
| 347 | result = prime * result + this.groups.hashCode(); |
| 348 | return result; |
| 349 | } |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 350 | |
| 351 | @Override |
| 352 | public String toString() { |
| 353 | return toStringHelper(getClass()) |
| 354 | .add("igmpType", Byte.toString(igmpType)) |
| 355 | .add("resField", Byte.toString(resField)) |
| 356 | .add("checksum", Short.toString(checksum)) |
| 357 | .add("unsupportTypeData", Arrays.toString(unsupportTypeData)) |
| 358 | .toString(); |
| 359 | // TODO: need to handle groups |
| 360 | } |
Luca Prete | c8ec4b5 | 2016-09-22 16:40:13 -0700 | [diff] [blame] | 361 | |
| 362 | public static class IGMPv3 extends IGMP { |
| 363 | public static final int MINIMUM_HEADER_LEN = 12; |
| 364 | |
| 365 | @Override |
| 366 | public void setMaxRespCode(byte respCode) { |
| 367 | if (igmpType != IGMP.TYPE_IGMPV3_MEMBERSHIP_QUERY) { |
| 368 | log.debug("Requesting the max response code for an incorrect field: "); |
| 369 | } |
| 370 | this.resField = respCode; |
| 371 | } |
| 372 | |
| 373 | @Override |
| 374 | public boolean addGroup(IGMPGroup group) { |
| 375 | checkNotNull(group); |
| 376 | switch (this.igmpType) { |
| 377 | case TYPE_IGMPV3_MEMBERSHIP_QUERY: |
| 378 | if (group instanceof IGMPMembership) { |
| 379 | return false; |
| 380 | } |
| 381 | |
| 382 | if (group.sources.size() > 1) { |
| 383 | return false; |
| 384 | } |
| 385 | break; |
| 386 | |
| 387 | case TYPE_IGMPV3_MEMBERSHIP_REPORT: |
ke han | e908271 | 2016-10-18 13:19:05 +0800 | [diff] [blame] | 388 | if (group instanceof IGMPQuery) { |
Luca Prete | c8ec4b5 | 2016-09-22 16:40:13 -0700 | [diff] [blame] | 389 | return false; |
| 390 | } |
| 391 | break; |
| 392 | |
| 393 | default: |
| 394 | log.debug("Warning no IGMP message type has been set"); |
| 395 | } |
| 396 | |
| 397 | this.groups.add(group); |
| 398 | return true; |
| 399 | } |
| 400 | |
| 401 | @Override |
| 402 | protected boolean validChecksum() { |
| 403 | return true; //FIXME |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | public static class IGMPv2 extends IGMP { |
| 408 | public static final int HEADER_LENGTH = 8; |
| 409 | |
| 410 | @Override |
| 411 | public void setMaxRespCode(byte respCode) { |
| 412 | this.resField = respCode; |
| 413 | } |
| 414 | |
| 415 | @Override |
| 416 | public boolean addGroup(IGMPGroup group) { |
| 417 | if (groups.isEmpty()) { |
| 418 | groups = ImmutableList.of(group); |
| 419 | return true; |
| 420 | } |
| 421 | return false; |
| 422 | } |
| 423 | |
| 424 | @Override |
| 425 | protected boolean validChecksum() { |
| 426 | int accumulation = (((int) this.igmpType) & 0xff) << 8; |
| 427 | accumulation += ((int) this.resField) & 0xff; |
| 428 | if (!groups.isEmpty()) { |
| 429 | int ipaddr = groups.get(0).getGaddr().getIp4Address().toInt(); |
| 430 | accumulation += (ipaddr >> 16) & 0xffff; |
| 431 | accumulation += ipaddr & 0xffff; |
| 432 | } |
| 433 | accumulation = (accumulation >> 16 & 0xffff) |
| 434 | + (accumulation & 0xffff); |
| 435 | short checksum = (short) (~accumulation & 0xffff); |
| 436 | return checksum == this.checksum; |
| 437 | } |
| 438 | } |
Rusty Eddy | 1da61a2 | 2015-09-01 00:48:58 +0000 | [diff] [blame] | 439 | } |