Rusty Eddy | 1da61a2 | 2015-09-01 00:48:58 +0000 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
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 | /** |
Rusty Eddy | 1da61a2 | 2015-09-01 00:48:58 +0000 | [diff] [blame] | 192 | * Deserializer function for IPv4 packets. |
| 193 | * |
| 194 | * @return deserializer function |
| 195 | */ |
| 196 | public static Deserializer<IGMP> deserializer() { |
| 197 | return (data, offset, length) -> { |
Luca Prete | c8ec4b5 | 2016-09-22 16:40:13 -0700 | [diff] [blame] | 198 | checkInput(data, offset, length, IGMPv2.HEADER_LENGTH); |
Rusty Eddy | 1da61a2 | 2015-09-01 00:48:58 +0000 | [diff] [blame] | 199 | |
ke han | de5f6a7 | 2017-06-23 10:36:48 +0800 | [diff] [blame] | 200 | final ByteBuffer bb = ByteBuffer.wrap(data, offset, length); |
| 201 | byte igmpType = bb.get(); |
| 202 | boolean isV2; |
| 203 | if (igmpType == TYPE_IGMPV2_MEMBERSHIP_REPORT || igmpType == TYPE_IGMPV2_LEAVE_GROUP || |
| 204 | length == IGMPv2.HEADER_LENGTH) { |
| 205 | isV2 = true; |
| 206 | } else { |
| 207 | isV2 = false; |
| 208 | } |
Luca Prete | c8ec4b5 | 2016-09-22 16:40:13 -0700 | [diff] [blame] | 209 | |
| 210 | IGMP igmp = isV2 ? new IGMPv2() : new IGMPv3(); |
Rusty Eddy | 1da61a2 | 2015-09-01 00:48:58 +0000 | [diff] [blame] | 211 | |
ke han | de5f6a7 | 2017-06-23 10:36:48 +0800 | [diff] [blame] | 212 | igmp.igmpType = igmpType; |
Rusty Eddy | 1da61a2 | 2015-09-01 00:48:58 +0000 | [diff] [blame] | 213 | igmp.resField = bb.get(); |
| 214 | igmp.checksum = bb.getShort(); |
Jonathan Hart | 81d7310 | 2016-02-19 10:32:05 -0800 | [diff] [blame] | 215 | |
Luca Prete | c8ec4b5 | 2016-09-22 16:40:13 -0700 | [diff] [blame] | 216 | if (isV2) { |
| 217 | igmp.addGroup(new IGMPQuery(IpAddress.valueOf(bb.getInt()), 0)); |
| 218 | if (igmp.validChecksum()) { |
| 219 | return igmp; |
| 220 | } |
| 221 | throw new DeserializationException("invalid checksum"); |
| 222 | } |
| 223 | |
| 224 | // second check for IGMPv3 |
| 225 | checkInput(data, offset, length, IGMPv3.MINIMUM_HEADER_LEN); |
| 226 | |
Rusty Eddy | 1da61a2 | 2015-09-01 00:48:58 +0000 | [diff] [blame] | 227 | String msg; |
| 228 | |
| 229 | switch (igmp.igmpType) { |
| 230 | |
| 231 | case TYPE_IGMPV3_MEMBERSHIP_QUERY: |
| 232 | IGMPQuery qgroup = new IGMPQuery(); |
| 233 | qgroup.deserialize(bb); |
| 234 | igmp.groups.add(qgroup); |
| 235 | break; |
| 236 | |
| 237 | case TYPE_IGMPV3_MEMBERSHIP_REPORT: |
| 238 | bb.getShort(); // Ignore resvd |
| 239 | int ngrps = bb.getShort(); |
| 240 | |
| 241 | for (; ngrps > 0; ngrps--) { |
| 242 | IGMPMembership mgroup = new IGMPMembership(); |
| 243 | mgroup.deserialize(bb); |
| 244 | igmp.groups.add(mgroup); |
| 245 | } |
| 246 | break; |
| 247 | |
| 248 | /* |
| 249 | * NOTE: according to the IGMPv3 spec. These previous IGMP type fields |
| 250 | * must be supported. At this time we are going to <b>assume</b> we run |
| 251 | * in a modern network where all devices are IGMPv3 capable. |
| 252 | */ |
| 253 | case TYPE_IGMPV1_MEMBERSHIP_REPORT: |
| 254 | case TYPE_IGMPV2_MEMBERSHIP_REPORT: |
| 255 | case TYPE_IGMPV2_LEAVE_GROUP: |
| 256 | igmp.unsupportTypeData = bb.array(); // Is this the entire array? |
| 257 | msg = "IGMP message type: " + igmp.igmpType + " is not supported"; |
| 258 | igmp.log.debug(msg); |
| 259 | break; |
| 260 | |
| 261 | default: |
alshabib | 0588e57 | 2015-09-04 12:00:42 -0700 | [diff] [blame] | 262 | msg = "IGMP message type: " + igmp.igmpType + " is not recognized"; |
| 263 | igmp.unsupportTypeData = bb.array(); |
Rusty Eddy | 1da61a2 | 2015-09-01 00:48:58 +0000 | [diff] [blame] | 264 | igmp.log.debug(msg); |
| 265 | break; |
| 266 | } |
| 267 | return igmp; |
| 268 | }; |
| 269 | } |
| 270 | |
Luca Prete | c8ec4b5 | 2016-09-22 16:40:13 -0700 | [diff] [blame] | 271 | /** |
| 272 | * Validates the message's checksum. |
| 273 | * |
| 274 | * @return true if valid, false if not |
| 275 | */ |
| 276 | protected abstract boolean validChecksum(); |
| 277 | |
Rusty Eddy | 1da61a2 | 2015-09-01 00:48:58 +0000 | [diff] [blame] | 278 | /* |
| 279 | * (non-Javadoc) |
| 280 | * |
| 281 | * @see java.lang.Object#equals(java.lang.Object) |
| 282 | */ |
| 283 | @Override |
| 284 | public boolean equals(final Object obj) { |
| 285 | if (this == obj) { |
| 286 | return true; |
| 287 | } |
| 288 | if (!super.equals(obj)) { |
| 289 | return false; |
| 290 | } |
| 291 | if (!(obj instanceof IGMP)) { |
| 292 | return false; |
| 293 | } |
| 294 | final IGMP other = (IGMP) obj; |
| 295 | if (this.igmpType != other.igmpType) { |
| 296 | return false; |
| 297 | } |
| 298 | if (this.resField != other.resField) { |
| 299 | return false; |
| 300 | } |
| 301 | if (this.checksum != other.checksum) { |
| 302 | return false; |
| 303 | } |
| 304 | if (this.groups.size() != other.groups.size()) { |
| 305 | return false; |
| 306 | } |
| 307 | // TODO: equals should be true regardless of order. |
| 308 | if (!groups.equals(other.groups)) { |
| 309 | return false; |
| 310 | } |
| 311 | return true; |
| 312 | } |
| 313 | |
| 314 | /* |
| 315 | * (non-Javadoc) |
| 316 | * |
| 317 | * @see java.lang.Object#hashCode() |
| 318 | */ |
| 319 | @Override |
| 320 | public int hashCode() { |
| 321 | final int prime = 2521; |
| 322 | int result = super.hashCode(); |
| 323 | result = prime * result + this.igmpType; |
| 324 | result = prime * result + this.groups.size(); |
| 325 | result = prime * result + this.resField; |
| 326 | result = prime * result + this.checksum; |
| 327 | result = prime * result + this.groups.hashCode(); |
| 328 | return result; |
| 329 | } |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 330 | |
| 331 | @Override |
| 332 | public String toString() { |
| 333 | return toStringHelper(getClass()) |
| 334 | .add("igmpType", Byte.toString(igmpType)) |
| 335 | .add("resField", Byte.toString(resField)) |
| 336 | .add("checksum", Short.toString(checksum)) |
| 337 | .add("unsupportTypeData", Arrays.toString(unsupportTypeData)) |
| 338 | .toString(); |
| 339 | // TODO: need to handle groups |
| 340 | } |
Luca Prete | c8ec4b5 | 2016-09-22 16:40:13 -0700 | [diff] [blame] | 341 | |
| 342 | public static class IGMPv3 extends IGMP { |
| 343 | public static final int MINIMUM_HEADER_LEN = 12; |
| 344 | |
| 345 | @Override |
| 346 | public void setMaxRespCode(byte respCode) { |
| 347 | if (igmpType != IGMP.TYPE_IGMPV3_MEMBERSHIP_QUERY) { |
| 348 | log.debug("Requesting the max response code for an incorrect field: "); |
| 349 | } |
| 350 | this.resField = respCode; |
| 351 | } |
| 352 | |
| 353 | @Override |
| 354 | public boolean addGroup(IGMPGroup group) { |
| 355 | checkNotNull(group); |
| 356 | switch (this.igmpType) { |
| 357 | case TYPE_IGMPV3_MEMBERSHIP_QUERY: |
| 358 | if (group instanceof IGMPMembership) { |
| 359 | return false; |
| 360 | } |
| 361 | |
| 362 | if (group.sources.size() > 1) { |
| 363 | return false; |
| 364 | } |
| 365 | break; |
| 366 | |
| 367 | case TYPE_IGMPV3_MEMBERSHIP_REPORT: |
ke han | e908271 | 2016-10-18 13:19:05 +0800 | [diff] [blame] | 368 | if (group instanceof IGMPQuery) { |
Luca Prete | c8ec4b5 | 2016-09-22 16:40:13 -0700 | [diff] [blame] | 369 | return false; |
| 370 | } |
| 371 | break; |
| 372 | |
| 373 | default: |
| 374 | log.debug("Warning no IGMP message type has been set"); |
| 375 | } |
| 376 | |
| 377 | this.groups.add(group); |
| 378 | return true; |
| 379 | } |
| 380 | |
| 381 | @Override |
| 382 | protected boolean validChecksum() { |
| 383 | return true; //FIXME |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | public static class IGMPv2 extends IGMP { |
| 388 | public static final int HEADER_LENGTH = 8; |
| 389 | |
| 390 | @Override |
| 391 | public void setMaxRespCode(byte respCode) { |
| 392 | this.resField = respCode; |
| 393 | } |
| 394 | |
| 395 | @Override |
| 396 | public boolean addGroup(IGMPGroup group) { |
| 397 | if (groups.isEmpty()) { |
| 398 | groups = ImmutableList.of(group); |
| 399 | return true; |
| 400 | } |
| 401 | return false; |
| 402 | } |
| 403 | |
| 404 | @Override |
| 405 | protected boolean validChecksum() { |
| 406 | int accumulation = (((int) this.igmpType) & 0xff) << 8; |
| 407 | accumulation += ((int) this.resField) & 0xff; |
| 408 | if (!groups.isEmpty()) { |
| 409 | int ipaddr = groups.get(0).getGaddr().getIp4Address().toInt(); |
| 410 | accumulation += (ipaddr >> 16) & 0xffff; |
| 411 | accumulation += ipaddr & 0xffff; |
| 412 | } |
| 413 | accumulation = (accumulation >> 16 & 0xffff) |
| 414 | + (accumulation & 0xffff); |
| 415 | short checksum = (short) (~accumulation & 0xffff); |
| 416 | return checksum == this.checksum; |
| 417 | } |
| 418 | } |
Rusty Eddy | 1da61a2 | 2015-09-01 00:48:58 +0000 | [diff] [blame] | 419 | } |