Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/scapy/layers/igmp.py: 68%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

192 statements  

1# SPDX-License-Identifier: GPL-2.0-or-later 

2# This file is part of Scapy 

3# See https://scapy.net/ for more information 

4 

5# scapy.contrib.description = Internet Group Management Protocol v1/v2/v3 

6# scapy.contrib.status = loads 

7 

8""" 

9IGMP v1/v2/v3 as per RFC2236 and RFC9776 

10 

11Example: IGMPv1 Membership Report (=Join) 

12 

13 >>> send(IP() / IGMP_MR(gaddr="224.0.0.251")) 

14 

15Example: IGMPv3 Membership Report (=Join) 

16 

17 >>> send(IP() / IGMPv3_MR(records=[IGMPv3_MR_Group(rtype=4, maddr="224.0.0.251")])) 

18 

19Example: IGMPv1/v2 Membership Query 

20 

21 >>> sr(IP() / IGMP_MQ(gaddr="224.0.0.251"), multi=True) 

22 

23Example: IGMPv3 Membership Query 

24 

25 >>> sr(IP() / IGMPv3_MQ(gaddr="224.0.0.251"), multi=True) 

26 

27Example: IGMPv2 Leave 

28 

29 >>> send(IP() / IGMP_LG(gaddr="225.0.0.251")) 

30""" 

31 

32import struct 

33 

34from scapy.config import conf 

35from scapy.fields import ( 

36 BitField, 

37 ByteEnumField, 

38 ByteField, 

39 FieldLenField, 

40 FieldListField, 

41 FlagsField, 

42 IPField, 

43 PacketListField, 

44 ScalingField, 

45 ShortField, 

46 XShortField, 

47) 

48from scapy.packet import bind_layers, Packet, bind_top_down 

49from scapy.plist import PacketList 

50from scapy.sendrecv import sr, send 

51from scapy.utils import checksum 

52 

53from scapy.layers.inet import IP, IPOption_Router_Alert, DestIPField 

54 

55 

56class _MRCodeField(ScalingField): 

57 """ 

58 Max Resp Code field. 

59 

60 """ 

61 

62 def i2m(self, pkt, x): 

63 if x is None: 

64 if pkt.type == 0x11: # Membership Query 

65 return 20 

66 else: 

67 return 0 

68 if pkt.type == 0x11 and isinstance(pkt, IGMPv3): 

69 # IGMP v3 - RFC9776 sect 4.1.1 

70 if x < 128: 

71 return x 

72 else: 

73 exp = 0 

74 x >>= 3 

75 while x > 31: 

76 exp += 1 

77 x >>= 1 

78 exp <<= 4 

79 return 0x80 | exp | (x & 0x0F) 

80 return super(_MRCodeField, self).i2m(pkt, x) 

81 

82 def i2h(self, pkt, x): 

83 return super(_MRCodeField, self).i2h(pkt, self.i2m(pkt, x)) 

84 

85 def m2i(self, pkt, x): 

86 if pkt.type == 0x11 and isinstance(pkt, IGMPv3): 

87 # IGMP v3 - RFC9776 sect 4.1.1 

88 if x < 128: 

89 return x 

90 else: 

91 mant, exp = (x & 0x0F), (x >> 4) & 0x7 

92 return (mant | 0x10) << (exp + 3) 

93 return super(_MRCodeField, self).i2m(pkt, x) 

94 

95 

96class IGMP(Packet): 

97 """ 

98 General IGMP v1/v2 message. 

99 """ 

100 

101 fields_desc = [ 

102 ByteEnumField( 

103 "type", 

104 0x11, 

105 { 

106 0x11: "Group Membership Query", 

107 0x12: "Version 1 - Membership Report", 

108 0x16: "Version 2 - Membership Report", 

109 0x17: "Leave Group", 

110 }, 

111 ), 

112 _MRCodeField("mrcode", None, unit="1/10sec"), 

113 XShortField("chksum", None), 

114 IPField("gaddr", "0.0.0.0"), 

115 ] 

116 

117 def post_build(self, p, pay): 

118 p += pay 

119 if self.chksum is None: 

120 p = p[:2] + struct.pack("!H", checksum(p)) + p[4:] 

121 return p 

122 

123 def add_underlayer(self, underlayer): 

124 # Add option to parent IP layer 

125 if isinstance(underlayer, IP): 

126 if not underlayer.options: 

127 underlayer.options.append(IPOption_Router_Alert()) 

128 super(IGMP, self).add_underlayer(underlayer) 

129 

130 @classmethod 

131 def dispatch_hook(cls, _pkt=None, *args, **kargs): 

132 typ = None 

133 

134 # Get packet type from build or dissection 

135 if "type" in kargs: 

136 typ = kargs["type"] 

137 elif _pkt and len(_pkt) >= 4: 

138 typ = _pkt[0] 

139 

140 # Return the proper class depending on the packet type 

141 if typ is not None: 

142 if typ == 0x11: 

143 if (_pkt and len(_pkt) >= 12) or (not _pkt and issubclass(cls, IGMPv3)): 

144 return IGMPv3_MQ 

145 else: 

146 return IGMP_MQ 

147 elif typ == 0x12: 

148 return IGMP_MR 

149 elif typ == 0x16: 

150 return IGMPv2_MR 

151 elif typ == 0x17: 

152 return IGMPv2_LG 

153 elif typ == 0x22: 

154 return IGMPv3_MR 

155 elif typ == 0x30: 

156 return IGMPv3_MRA 

157 elif typ == 0x31: 

158 return IGMPv3_MRS 

159 elif typ == 0x32: 

160 return IGMPv3_MRT 

161 

162 # IGMPv3: allow default IGMPv3() which is empty for compat with <2.8.0 

163 # IGMP: do not let the default IGMP() 

164 if issubclass(cls, IGMPv3): 

165 return cls 

166 else: 

167 return IGMP_MQ 

168 

169 def answers(self, other): 

170 if other.type == 0x11: 

171 return self.type in [0x12, 0x16, 0x22] 

172 elif other.type == 0x31: 

173 return self.type == 0x30 

174 return False 

175 

176 def mysummary(self): 

177 t = self.__class__.__name__ 

178 if isinstance(self.underlayer, IP): 

179 return self.underlayer.sprintf( 

180 f"IGMP: %IP.src% > %IP.dst% %{t}.type% %{t}.gaddr%" 

181 ) 

182 else: 

183 return self.sprintf(f"IGMP %{t}.type% %{t}.gaddr%") 

184 

185 

186class IGMP_MQ(IGMP): 

187 """ 

188 IGMPv1/v2 Membership Query 

189 """ 

190 

191 name = "IGMPv1/v2 Membership Query" 

192 match_subclass = True 

193 type = 0x11 

194 

195 

196class IGMPv2_LG(IGMP): 

197 """ 

198 IGMPv2 Leave Group 

199 """ 

200 

201 name = "IGMPv2 Leave Group" 

202 match_subclass = True 

203 type = 0x17 

204 

205 

206class IGMP_MR(IGMP): 

207 """ 

208 IGMPv1 Membership Report 

209 """ 

210 

211 name = "IGMPv1 Membership Report" 

212 type = 0x12 

213 match_subclass = True 

214 

215 

216class IGMPv2_MR(IGMP): 

217 """ 

218 IGMPv2 Membership Report 

219 """ 

220 

221 name = "IGMPv2 Membership Report" 

222 type = 0x16 

223 match_subclass = True 

224 

225 

226_IGMPv3_Type = { 

227 0x11: "Membership Query", 

228 0x22: "Version 3 - Membership Report", 

229 # RFC 4286 

230 0x30: "Multicast Router Advertisement", 

231 0x31: "Multicast Router Solicitation", 

232 0x32: "Multicast Router Termination", 

233} 

234 

235 

236class IGMPv3(IGMP): 

237 """ 

238 IGMP Message Class for v3 

239 """ 

240 

241 name = "IGMPv3" 

242 

243 fields_desc = [ 

244 ByteEnumField("type", 0x11, _IGMPv3_Type), 

245 ] 

246 

247 @classmethod 

248 def dispatch_hook(cls, _pkt=None, *args, **kargs): 

249 return super(IGMPv3, cls).dispatch_hook(_pkt=_pkt, *args, **kargs) 

250 

251 def mysummary(self): 

252 """Display a summary of the IGMPv3 object.""" 

253 t = self.__class__.__name__ 

254 if isinstance(self.underlayer, IP): 

255 return self.underlayer.sprintf(f"IGMPv3: %IP.src% > %IP.dst% %{t}.type%") 

256 else: 

257 return self.sprintf(f"IGMPv3 %{t}.type%") 

258 

259 

260class IGMPv3_MQ(IGMPv3): 

261 """ 

262 IGMPv3 Membership Query 

263 """ 

264 

265 name = "IGMPv3 Membership Query" 

266 match_subclass = True 

267 

268 fields_desc = [ 

269 ByteEnumField("type", 0x11, _IGMPv3_Type), 

270 _MRCodeField("mrcode", 20, unit="1/10sec"), 

271 XShortField("chksum", None), 

272 IPField("gaddr", "0.0.0.0"), 

273 FlagsField( 

274 "flags", 

275 0, 

276 4, 

277 { 

278 0x8: "extended", 

279 }, 

280 ), 

281 BitField("s", 0, 1), 

282 BitField("qrv", 0, 3), 

283 ByteField("qqic", 0), 

284 FieldLenField("numsrc", None, count_of="srcaddrs"), 

285 FieldListField( 

286 "srcaddrs", 

287 None, 

288 IPField("sa", "0.0.0.0"), 

289 count_from=lambda x: x.numsrc, 

290 ), 

291 ] 

292 

293 

294class IGMPv3_MR_Group(Packet): 

295 """ 

296 IGMP Group Record for IGMPv3 Membership Report 

297 

298 This class should be added in the records of an instantiation of class IGMPv3_MR. 

299 """ 

300 

301 name = "IGMPv3 Group Record" 

302 fields_desc = [ 

303 ByteEnumField( 

304 "rtype", 

305 4, 

306 { 

307 1: "Mode Is Include", 

308 2: "Mode Is Exclude", 

309 3: "Change To Include Mode", 

310 4: "Change To Exclude Mode", 

311 5: "Allow New Sources", 

312 6: "Block Old Sources", 

313 }, 

314 ), 

315 ByteField("auxdlen", 0), 

316 FieldLenField("numsrc", None, count_of="srcaddrs"), 

317 IPField("maddr", "0.0.0.0"), 

318 FieldListField( 

319 "srcaddrs", 

320 [], 

321 IPField("sa", "0.0.0.0"), 

322 count_from=lambda x: x.numsrc, 

323 ), 

324 ] 

325 

326 def mysummary(self): 

327 """Display a summary of the IGMPv3 group record.""" 

328 return self.sprintf( 

329 "IGMPv3 Group Record %IGMPv3gr.type% %IGMPv3gr.maddr%" 

330 ) # noqa: E501 

331 

332 def default_payload_class(self, payload): 

333 return conf.padding_layer 

334 

335 

336class IGMPv3_MR(IGMPv3): 

337 """ 

338 IGMP Membership Report extension for IGMPv3 

339 """ 

340 

341 name = "IGMPv3 Membership Report" 

342 match_subclass = True 

343 

344 fields_desc = [ 

345 ByteEnumField("type", 0x22, _IGMPv3_Type), 

346 ByteField("reserved", 0), 

347 XShortField("chksum", None), 

348 FlagsField( 

349 "flags", 

350 0, 

351 16, 

352 { 

353 0x8000: "extended", 

354 }, 

355 ), 

356 FieldLenField("numgrp", None, count_of="records"), 

357 PacketListField( 

358 "records", 

359 [], 

360 IGMPv3_MR_Group, 

361 count_from=lambda x: x.numgrp, 

362 ), 

363 ] 

364 

365 

366class IGMPv3_MRA(IGMPv3): 

367 """ 

368 IGMP Multicast Router Advertisement per RFC4286 

369 """ 

370 

371 name = "IGMPv3_MRA" 

372 match_subclass = True 

373 

374 fields_desc = [ 

375 ByteEnumField("type", 0x30, _IGMPv3_Type), 

376 ByteField("advIntvl", 0), 

377 XShortField("chksum", None), 

378 ShortField("qryIntvl", 0), 

379 ShortField("robust", 0), 

380 ] 

381 

382 

383class IGMPv3_MRS(IGMPv3): 

384 """ 

385 IGMP Multicast Router Solicitation per RFC4286 

386 """ 

387 

388 name = "IGMPv3_MRS" 

389 match_subclass = True 

390 

391 fields_desc = [ 

392 ByteEnumField("type", 0x31, _IGMPv3_Type), 

393 ByteField("reserved", 0), 

394 XShortField("chksum", None), 

395 ] 

396 

397 

398class IGMPv3_MRT(IGMPv3): 

399 """ 

400 IGMP Multicast Router Termination per RFC4286 

401 """ 

402 

403 name = "IGMPv3_MRT" 

404 match_subclass = True 

405 

406 fields_desc = [ 

407 ByteEnumField("type", 0x31, _IGMPv3_Type), 

408 ByteField("reserved", 0), 

409 XShortField("chksum", None), 

410 ] 

411 

412 

413bind_layers(IP, IGMP, proto=2) 

414bind_top_down(IP, IGMP, proto=2, ttl=1, tos=0xC0) 

415 

416 

417def _igmp_mq_addr(pkt): 

418 if pkt.gaddr == "0.0.0.0": 

419 # General Query 

420 return "224.0.0.1" 

421 else: 

422 return pkt.gaddr 

423 

424 

425DestIPField.bind_addr(IGMP_MQ, _igmp_mq_addr) 

426DestIPField.bind_addr(IGMPv3_MQ, _igmp_mq_addr) 

427DestIPField.bind_addr(IGMPv2_LG, "224.0.0.2") 

428DestIPField.bind_addr(IGMP_MR, lambda pkt: pkt.gaddr) 

429DestIPField.bind_addr(IGMPv2_MR, lambda pkt: pkt.gaddr) 

430DestIPField.bind_addr(IGMPv3_MR, "224.0.0.22") 

431 

432# RFC4286 

433DestIPField.bind_addr(IGMPv3_MRA, "224.0.0.106") 

434DestIPField.bind_addr(IGMPv3_MRT, "224.0.0.106") 

435DestIPField.bind_addr(IGMPv3_MRS, "224.0.0.2") 

436 

437 

438@conf.commands.register 

439def igmp_join(gaddr: str, version=2, psrc=None, iface=None): 

440 """ 

441 Send a IGMP Membership Report to join a multicast group 

442 

443 :param gaddr: the IPv4 of the group to join 

444 :param version: whether to use IGMPv1, IGMPv2 or IGMPv3. Default: both 2 and 3 

445 :param psrc: (optional) the source IP 

446 """ 

447 if version == 1: 

448 pkt = IP(src=psrc) / IGMP_MR(gaddr=gaddr) 

449 elif version == 2: 

450 pkt = IP(src=psrc) / IGMPv2_MR(gaddr=gaddr) 

451 elif version == 3: 

452 pkt = IP(src=psrc) / IGMPv3_MR(records=[IGMPv3_MR_Group(rtype=4, maddr=gaddr)]) 

453 send(pkt, iface=iface) 

454 

455 

456@conf.commands.register 

457def igmp_leave(gaddr: str, version=2, psrc=None, iface=None): 

458 """ 

459 Send a IGMP Leave Group to leave a multicast group 

460 

461 :param gaddr: the IPv4 of the group to leave 

462 :param psrc: (optional) the source IP 

463 """ 

464 if version == 1: 

465 raise ValueError("IGMPv1 does not include a mechanism to leave !") 

466 elif version == 2: 

467 pkt = IP(src=psrc) / IGMPv2_LG(gaddr=gaddr) 

468 elif version == 3: 

469 pkt = IP(src=psrc) / IGMPv3_MR(records=[IGMPv3_MR_Group(rtype=3, maddr=gaddr)]) 

470 send(pkt, iface=iface) 

471 

472 

473class IGMPMQResult(PacketList): 

474 def __init__( 

475 self, 

476 res=None, 

477 name="IGMP-MR", 

478 stats=None, 

479 ): 

480 PacketList.__init__(self, res, name, stats) 

481 

482 

483@conf.commands.register 

484def igmp_query(gaddr: str = None, version=2, timeout=2): 

485 """ 

486 Send/receive a Membership Query to get the members of a multicast group 

487 

488 :param gaddr: the IPv4 of the group to query 

489 

490 Example:: 

491 

492 >>> pkts = igmp_query("224.0.0.251") 

493 >>> pkts.show() 

494 

495 Example 2:: 

496 

497 >>> pkts = igmp_query("239.255.255.250", version=3) 

498 >>> pkts.show() 

499 """ 

500 if version == 1: 

501 pkt = IP() / IGMP_MQ(mrcode=0, gaddr=gaddr) 

502 elif version == 2: 

503 pkt = IP() / IGMP_MQ(mrcode=timeout * 10, gaddr=gaddr) 

504 elif version == 3: 

505 pkt = IP() / IGMPv3_MQ(mrcode=timeout * 10, gaddr=gaddr) 

506 

507 _old_checkIPaddr = conf.checkIPaddr 

508 conf.checkIPaddr = False 

509 try: 

510 return IGMPMQResult( 

511 [x.answer for x in sr(pkt, multi=True, timeout=timeout + 1)[0]] 

512 ) 

513 finally: 

514 conf.checkIPaddr = _old_checkIPaddr