blob: c8b5f85e5bc4bad8439a1963ddb5b1bbc40bd8dc [file] [log] [blame]
Rusty Eddy1da61a22015-09-01 00:48:58 +00001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Rusty Eddy1da61a22015-09-01 00:48:58 +00003 *
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 */
16package org.onlab.packet;
17
Luca Pretec8ec4b52016-09-22 16:40:13 -070018import com.google.common.collect.ImmutableList;
Jonathan Hart81d73102016-02-19 10:32:05 -080019import org.slf4j.Logger;
20
Rusty Eddy1da61a22015-09-01 00:48:58 +000021import java.nio.ByteBuffer;
22import java.util.ArrayList;
Ray Milkey9f87e512016-01-05 10:00:22 -080023import java.util.Arrays;
Rusty Eddy1da61a22015-09-01 00:48:58 +000024import java.util.List;
Ray Milkey9f87e512016-01-05 10:00:22 -080025
Jian Li5fc14292015-12-04 11:30:46 -080026import static com.google.common.base.MoreObjects.toStringHelper;
Rusty Eddy1da61a22015-09-01 00:48:58 +000027import static com.google.common.base.Preconditions.checkNotNull;
28import static org.onlab.packet.PacketUtils.checkInput;
Jian Li5fc14292015-12-04 11:30:46 -080029import static org.slf4j.LoggerFactory.getLogger;
Rusty Eddy1da61a22015-09-01 00:48:58 +000030
Rusty Eddy1da61a22015-09-01 00:48:58 +000031/**
32 * Implements IGMP control packet format.
33 */
Luca Pretec8ec4b52016-09-22 16:40:13 -070034public abstract class IGMP extends BasePacket {
Ray Milkey9c9cde42018-01-12 14:22:06 -080035 private static final Logger log = getLogger(IGMP.class);
Rusty Eddy1da61a22015-09-01 00:48:58 +000036
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 Eddy1da61a22015-09-01 00:48:58 +000042
43 List<IGMPGroup> groups = new ArrayList<>();
44
45 // Fields contained in the IGMP header
Luca Pretec8ec4b52016-09-22 16:40:13 -070046 protected byte igmpType;
47 protected byte resField = 0;
48 protected short checksum = 0;
Rusty Eddy1da61a22015-09-01 00:48:58 +000049
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 Pretec8ec4b52016-09-22 16:40:13 -070096 public abstract void setMaxRespCode(byte respCode);
Rusty Eddy1da61a22015-09-01 00:48:58 +000097
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 Pretec8ec4b52016-09-22 16:40:13 -0700106 return ImmutableList.copyOf(groups);
Rusty Eddy1da61a22015-09-01 00:48:58 +0000107 }
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 Pretec8ec4b52016-09-22 16:40:13 -0700115 public abstract boolean addGroup(IGMPGroup group);
Rusty Eddy1da61a22015-09-01 00:48:58 +0000116
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 Milkeyaef45852016-01-11 17:13:19 -0800124 @java.lang.SuppressWarnings("squid:S128") // suppress switch fall through warning
Rusty Eddy1da61a22015-09-01 00:48:58 +0000125 @Override
126 public byte[] serialize() {
Jian Li68c4fc42016-01-11 16:07:03 -0800127 byte[] data = new byte[8915];
Rusty Eddy1da61a22015-09-01 00:48:58 +0000128
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 Pretec8ec4b52016-09-22 16:40:13 -0700138 if (this instanceof IGMPv3) {
139 switch (this.igmpType) {
Jonathan Hart81d73102016-02-19 10:32:05 -0800140
Luca Pretec8ec4b52016-09-22 16:40:13 -0700141 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 Hart81d73102016-02-19 10:32:05 -0800147
Luca Pretec8ec4b52016-09-22 16:40:13 -0700148 case IGMP.TYPE_IGMPV3_MEMBERSHIP_QUERY:
Rusty Eddy1da61a22015-09-01 00:48:58 +0000149
Luca Pretec8ec4b52016-09-22 16:40:13 -0700150 for (IGMPGroup grp : groups) {
151 grp.serialize(bb);
152 }
153 break;
Rusty Eddy1da61a22015-09-01 00:48:58 +0000154
Luca Pretec8ec4b52016-09-22 16:40:13 -0700155 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 Eddy1da61a22015-09-01 00:48:58 +0000167 }
168
169 int size = bb.position();
Jonathan Hart81d73102016-02-19 10:32:05 -0800170
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 Eddy1da61a22015-09-01 00:48:58 +0000185 bb.position(0);
Jian Li68c4fc42016-01-11 16:07:03 -0800186 byte[] rdata = new byte[size];
Rusty Eddy1da61a22015-09-01 00:48:58 +0000187 bb.get(rdata, 0, size);
188 return rdata;
189 }
190
191 /**
Rusty Eddy1da61a22015-09-01 00:48:58 +0000192 * Deserializer function for IPv4 packets.
193 *
194 * @return deserializer function
195 */
196 public static Deserializer<IGMP> deserializer() {
197 return (data, offset, length) -> {
Luca Pretec8ec4b52016-09-22 16:40:13 -0700198 checkInput(data, offset, length, IGMPv2.HEADER_LENGTH);
Rusty Eddy1da61a22015-09-01 00:48:58 +0000199
ke hande5f6a72017-06-23 10:36:48 +0800200 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 Pretec8ec4b52016-09-22 16:40:13 -0700209
210 IGMP igmp = isV2 ? new IGMPv2() : new IGMPv3();
Rusty Eddy1da61a22015-09-01 00:48:58 +0000211
ke hande5f6a72017-06-23 10:36:48 +0800212 igmp.igmpType = igmpType;
Rusty Eddy1da61a22015-09-01 00:48:58 +0000213 igmp.resField = bb.get();
214 igmp.checksum = bb.getShort();
Jonathan Hart81d73102016-02-19 10:32:05 -0800215
Luca Pretec8ec4b52016-09-22 16:40:13 -0700216 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 Eddy1da61a22015-09-01 00:48:58 +0000227 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:
alshabib0588e572015-09-04 12:00:42 -0700262 msg = "IGMP message type: " + igmp.igmpType + " is not recognized";
263 igmp.unsupportTypeData = bb.array();
Rusty Eddy1da61a22015-09-01 00:48:58 +0000264 igmp.log.debug(msg);
265 break;
266 }
267 return igmp;
268 };
269 }
270
Luca Pretec8ec4b52016-09-22 16:40:13 -0700271 /**
272 * Validates the message's checksum.
273 *
274 * @return true if valid, false if not
275 */
276 protected abstract boolean validChecksum();
277
Rusty Eddy1da61a22015-09-01 00:48:58 +0000278 /*
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 Li5fc14292015-12-04 11:30:46 -0800330
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 Pretec8ec4b52016-09-22 16:40:13 -0700341
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 hane9082712016-10-18 13:19:05 +0800368 if (group instanceof IGMPQuery) {
Luca Pretec8ec4b52016-09-22 16:40:13 -0700369 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 Eddy1da61a22015-09-01 00:48:58 +0000419}