blob: c91dfdd3eec372086493c852327887fb25e31111 [file] [log] [blame]
Chetan Gaonkercfcce782016-05-10 10:10:42 -07001#
2# Copyright 2016-present Ciena Corporation
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#
Chetan Gaonker25470972016-02-26 08:52:15 -080016from socket import *
17from struct import *
18from scapy.all import *
19from itertools import *
20
Chetan Gaonker5a5204e2016-03-02 01:35:13 -080021IGMP_TYPE_MEMBERSHIP_QUERY = 0x11
22IGMP_TYPE_V3_MEMBERSHIP_REPORT = 0x22
23IGMP_TYPE_V1_MEMBERSHIP_REPORT = 0x12
24IGMP_TYPE_V2_MEMBERSHIP_REPORT = 0x16
25IGMP_TYPE_V2_LEAVE_GROUP = 0x17
26
27IGMP_V3_GR_TYPE_INCLUDE = 0x01
28IGMP_V3_GR_TYPE_EXCLUDE = 0x02
29IGMP_V3_GR_TYPE_CHANGE_TO_INCLUDE = 0x03
30IGMP_V3_GR_TYPE_CHANGE_TO_EXCLUDE = 0x04
31IGMP_V3_GR_TYPE_ALLOW_NEW = 0x05
32IGMP_V3_GR_TYPE_BLOCK_OLD = 0x06
33
34"""
Chetan Gaonker25470972016-02-26 08:52:15 -080035IGMPV3_ALL_ROUTERS = '224.0.0.22'
36IGMPv3 = 3
37IP_SRC = '1.2.3.4'
38ETHERTYPE_IP = 0x0800
39IGMP_DST_MAC = "01:00:5e:00:01:01"
40IGMP_SRC_MAC = "5a:e1:ac:ec:4d:a1"
Chetan Gaonker5a5204e2016-03-02 01:35:13 -080041"""
Chetan Gaonker25470972016-02-26 08:52:15 -080042
Chetan Gaonker25470972016-02-26 08:52:15 -080043
Chetan Gaonker5a5204e2016-03-02 01:35:13 -080044class IGMPv3gr(Packet):
45 """IGMPv3 Group Record, used in membership report"""
Chetan Gaonker25470972016-02-26 08:52:15 -080046
Chetan Gaonker5a5204e2016-03-02 01:35:13 -080047 name = "IGMPv3gr"
Chetan Gaonker25470972016-02-26 08:52:15 -080048
Chetan Gaonker5a5204e2016-03-02 01:35:13 -080049 igmp_v3_gr_types = {
50 IGMP_V3_GR_TYPE_INCLUDE: "Include Mode",
51 IGMP_V3_GR_TYPE_EXCLUDE: "Exclude Mode",
52 IGMP_V3_GR_TYPE_CHANGE_TO_INCLUDE: "Change to Include Mode",
53 IGMP_V3_GR_TYPE_CHANGE_TO_EXCLUDE: "Change to Exclude Mode",
54 IGMP_V3_GR_TYPE_ALLOW_NEW: "Allow New Sources",
55 IGMP_V3_GR_TYPE_BLOCK_OLD: "Block Old Sources"
56 }
Chetan Gaonker25470972016-02-26 08:52:15 -080057
Chetan Gaonker5a5204e2016-03-02 01:35:13 -080058 fields_desc = [
59 ByteEnumField("rtype", IGMP_V3_GR_TYPE_INCLUDE, igmp_v3_gr_types),
60 ByteField("aux_data_len", 0),
61 FieldLenField("numsrc", None, count_of="sources"),
62 IPField("mcaddr", "0.0.0.0"),
63 FieldListField("sources", None, IPField("src", "0.0.0.0"), "numsrc")
64 ]
Chetan Gaonker25470972016-02-26 08:52:15 -080065
Chetan Gaonker5a5204e2016-03-02 01:35:13 -080066 def post_build(self, pkt, payload):
67 pkt += payload
68 if self.aux_data_len != 0:
69 print "WARNING: Auxiliary Data Length must be zero (0)"
Chetan Gaonker25470972016-02-26 08:52:15 -080070 return pkt
71
Chetan Gaonker25470972016-02-26 08:52:15 -080072
Chetan Gaonker5a5204e2016-03-02 01:35:13 -080073class IGMPv3(Packet):
Chetan Gaonker25470972016-02-26 08:52:15 -080074
Chetan Gaonker5a5204e2016-03-02 01:35:13 -080075 name = "IGMPv3"
Chetan Gaonker25470972016-02-26 08:52:15 -080076
Chetan Gaonker5a5204e2016-03-02 01:35:13 -080077 igmp_v3_types = {
78 IGMP_TYPE_MEMBERSHIP_QUERY: "Membership Query",
79 IGMP_TYPE_V3_MEMBERSHIP_REPORT: " Version 3 Mebership Report",
80 IGMP_TYPE_V2_MEMBERSHIP_REPORT: " Version 2 Mebership Report",
81 IGMP_TYPE_V1_MEMBERSHIP_REPORT: " Version 1 Mebership Report",
82 IGMP_TYPE_V2_LEAVE_GROUP: "Version 2 Leave Group"
83 }
Chetan Gaonker25470972016-02-26 08:52:15 -080084
Chetan Gaonker5a5204e2016-03-02 01:35:13 -080085 fields_desc = [
86 ByteEnumField("type", IGMP_TYPE_MEMBERSHIP_QUERY, igmp_v3_types),
87 ByteField("max_resp_code", 0),
88 XShortField("checksum", None),
89 #IPField("group_address", "0.0.0.0"),
Chetan Gaonker25470972016-02-26 08:52:15 -080090
Chetan Gaonker5a5204e2016-03-02 01:35:13 -080091 # membership query fields
92 ConditionalField(IPField("gaddr", "0.0.0.0"), lambda pkt: pkt.type == IGMP_TYPE_MEMBERSHIP_QUERY),
93 ConditionalField(BitField("resv", 0, 4), lambda pkt: pkt.type == IGMP_TYPE_MEMBERSHIP_QUERY),
94 ConditionalField(BitField("s", 0, 1), lambda pkt: pkt.type == IGMP_TYPE_MEMBERSHIP_QUERY),
95 ConditionalField(BitField("qrv", 0, 3), lambda pkt: pkt.type == IGMP_TYPE_MEMBERSHIP_QUERY),
96 ConditionalField(ByteField("qqic", 0), lambda pkt: pkt.type == IGMP_TYPE_MEMBERSHIP_QUERY),
97 ConditionalField(FieldLenField("numsrc", None, count_of="srcs"), lambda pkt: pkt.type == IGMP_TYPE_MEMBERSHIP_QUERY),
98 ConditionalField(FieldListField("srcs", None, IPField("src", "0.0.0.0"), "numsrc"), lambda pkt: pkt.type == IGMP_TYPE_MEMBERSHIP_QUERY),
99
100 # membership report fields
101 ConditionalField(ShortField("resv2", 0), lambda pkt: pkt.type == IGMP_TYPE_V3_MEMBERSHIP_REPORT),
102 ConditionalField(FieldLenField("numgrp", None, count_of="grps"), lambda pkt: pkt.type == IGMP_TYPE_V3_MEMBERSHIP_REPORT),
103 ConditionalField(PacketListField("grps", [], IGMPv3gr), lambda pkt: pkt.type == IGMP_TYPE_V3_MEMBERSHIP_REPORT)
104
105 # TODO: v2 and v3 membership reports?
106
107 ]
108
109 def post_build(self, pkt, payload):
110
111 pkt += payload
112
113 if self.type in [IGMP_TYPE_V3_MEMBERSHIP_REPORT,]: # max_resp_code field is reserved (0)
114 mrc = 0
115 else:
116 mrc = self.encode_float(self.max_resp_code)
117 pkt = pkt[:1] + chr(mrc) + pkt[2:]
118
119 if self.checksum is None:
120 chksum = checksum(pkt)
121 pkt = pkt[:2] + chr(chksum >> 8) + chr(chksum & 0xff) + pkt[4:]
122
123 return pkt
124
125 def encode_float(self, value):
126 """Encode max response time value per RFC 3376."""
127 if value < 128:
128 return value
129 if value > 31743:
130 return 255
131 exp = 0
132 value >>= 3
133 while value > 31:
134 exp += 1
135 value >>= 1
136 return 0x80 | (exp << 4) | (value & 0xf)
137
138
139 def decode_float(self, code):
140 if code < 128:
141 return code
142 mant = code & 0xf
143 exp = (code >> 4) & 0x7
144 return (mant | 0x10) << (exp + 3)
145
146 @staticmethod
147 def is_valid_mcaddr(ip):
148 byte1 = atol(ip) >> 24 & 0xff
149 return (byte1 & 0xf0) == 0xe0
150
151 @staticmethod
152 def fixup(pkt):
153 """Fixes up the underlying IP() and Ether() headers."""
154 assert pkt.haslayer(IGMPv3), "This packet is not an IGMPv4 packet; cannot fix it up"
155
156 igmp = pkt.getlayer(IGMPv3)
157
158 if pkt.haslayer(IP):
159 ip = pkt.getlayer(IP)
160 ip.ttl = 1
161 ip.proto = 2
162 ip.tos = 0xc0
163 ip.options = [IPOption_Router_Alert()]
164
165 if igmp.type == IGMP_TYPE_MEMBERSHIP_QUERY:
166 if igmp.gaddr == "0.0.0.0":
167 ip.dst = "224.0.0.1"
168 else:
169 assert IGMPv3.is_valid_mcaddr(igmp.gaddr), "IGMP membership query with invalid mcast address"
170 ip.dst = igmp.gaddr
171
172 elif igmp.type == IGMP_TYPE_V2_LEAVE_GROUP and IGMPv3.is_valid_mcaddr(igmp.gaddr):
173 ip.dst = "224.0.0.2"
174
175 elif (igmp.type in (IGMP_TYPE_V1_MEMBERSHIP_REPORT, IGMP_TYPE_V2_MEMBERSHIP_REPORT) and
176 IGMPv3.is_valid_mcaddr(igmp.gaddr)):
177 ip.dst = igmp.gaddr
178
179 # We do not need to fixup the ether layer, it is done by scapy
180 #
181 # if pkt.haslayer(Ether):
182 # eth = pkt.getlayer(Ether)
183 # ip_long = atol(ip.dst)
184 # ether.dst = '01:00:5e:%02x:%02x:%02x' % ( (ip_long >> 16) & 0x7f, (ip_long >> 8) & 0xff, ip_long & 0xff )
185
186
187 return pkt
188
189
190bind_layers(IP, IGMPv3, frag=0, proto=2, ttl=1, tos=0xc0)
191bind_layers(IGMPv3, IGMPv3gr, frag=0, proto=2)
192bind_layers(IGMPv3gr, IGMPv3gr, frag=0, proto=2)
193
194
195if __name__ == "__main__":
196
197 print "test float encoding"
198 from math import log
199 max_expected_error = 1.0 / (2<<3) # four bit precision
200 p = IGMPv3()
201 for v in range(0, 31745):
202 c = p.encode_float(v)
203 d = p.decode_float(c)
204 rel_err = float(v-d)/v if v!=0 else 0.0
205 assert rel_err <= max_expected_error
206
207 print "construct membership query - general query"
208 mq = IGMPv3(type=IGMP_TYPE_MEMBERSHIP_QUERY, max_resp_code=120)
209 hexdump(str(mq))
210
211 print "construct membership query - group-specific query"
212 mq = IGMPv3(type=IGMP_TYPE_MEMBERSHIP_QUERY, max_resp_code=120, gaddr="224.0.0.1")
213 hexdump(str(mq))
214
215 print "construct membership query - group-and-source-specific query"
216 mq = IGMPv3(type=IGMP_TYPE_MEMBERSHIP_QUERY, max_resp_code=120, gaddr="224.0.0.1")
217 mq.srcs = ['1.2.3.4', '5.6.7.8']
218 hexdump(str(mq))
219
220 print "fixup"
221 mq = IGMPv3(type=IGMP_TYPE_MEMBERSHIP_QUERY)
222 mq.srcs = ['1.2.3.4', '5.6.7.8']
223 pkt = Ether() / IP() / mq
224 print "before fixup:"
225 hexdump(str(pkt))
226
227 print "after fixup:"
228 IGMPv3.fixup(pkt)
229 hexdump(str(pkt))
230
231 print "construct v3 membership report - join a single group"
232 mr = IGMPv3(type=IGMP_TYPE_V3_MEMBERSHIP_REPORT, max_resp_code=30, gaddr="224.0.0.1")
233 mr.grps = [IGMPv3gr( rtype=IGMP_V3_GR_TYPE_EXCLUDE, mcaddr="229.10.20.30")]
234 hexdump(mr)
235
236 print "construct v3 membership report - join two groups"
237 mr = IGMPv3(type=IGMP_TYPE_V3_MEMBERSHIP_REPORT, max_resp_code=30, gaddr="224.0.0.1")
238 mr.grps = [
239 IGMPv3gr(rtype=IGMP_V3_GR_TYPE_EXCLUDE, mcaddr="229.10.20.30"),
240 IGMPv3gr(rtype=IGMP_V3_GR_TYPE_EXCLUDE, mcaddr="229.10.20.31")
241 ]
242 hexdump(mr)
243
244 print "construct v3 membership report - leave a group"
245 mr = IGMPv3(type=IGMP_TYPE_V3_MEMBERSHIP_REPORT, max_resp_code=30, gaddr="224.0.0.1")
246 mr.grps = [IGMPv3gr(rtype=IGMP_V3_GR_TYPE_INCLUDE, mcaddr="229.10.20.30")]
247 hexdump(mr)
248
249 print "all ok"