| 312 | |
| 313 | |
| 314 | class IPv6(_IPv6GuessPayload, Packet, IPTools): |
| 315 | name = "IPv6" |
| 316 | fields_desc = [BitField("version", 6, 4), |
| 317 | BitField("tc", 0, 8), |
| 318 | BitField("fl", 0, 20), |
| 319 | ShortField("plen", None), |
| 320 | ByteEnumField("nh", 59, ipv6nh), |
| 321 | ByteField("hlim", 64), |
| 322 | SourceIP6Field("src"), |
| 323 | DestIP6Field("dst", "::1")] |
| 324 | |
| 325 | def route(self): |
| 326 | """Used to select the L2 address""" |
| 327 | dst = self.dst |
| 328 | scope = None |
| 329 | if isinstance(dst, (Net6, _ScopedIP)): |
| 330 | scope = dst.scope |
| 331 | if isinstance(dst, (Gen, list)): |
| 332 | dst = next(iter(dst)) |
| 333 | return conf.route6.route(dst, dev=scope) |
| 334 | |
| 335 | def mysummary(self): |
| 336 | return "%s > %s (%i)" % (self.src, self.dst, self.nh) |
| 337 | |
| 338 | def post_build(self, p, pay): |
| 339 | p += pay |
| 340 | if self.plen is None: |
| 341 | tmp_len = len(p) - 40 |
| 342 | p = p[:4] + struct.pack("!H", tmp_len) + p[6:] |
| 343 | return p |
| 344 | |
| 345 | def extract_padding(self, data): |
| 346 | """Extract the IPv6 payload""" |
| 347 | |
| 348 | if self.plen == 0 and self.nh == 0 and len(data) >= 8: |
| 349 | # Extract Hop-by-Hop extension length |
| 350 | hbh_len = orb(data[1]) |
| 351 | hbh_len = 8 + hbh_len * 8 |
| 352 | |
| 353 | # Extract length from the Jumbogram option |
| 354 | # Note: the following algorithm take advantage of the Jumbo option |
| 355 | # mandatory alignment (4n + 2, RFC2675 Section 2) |
| 356 | jumbo_len = None |
| 357 | idx = 0 |
| 358 | offset = 4 * idx + 2 |
| 359 | while offset <= len(data): |
| 360 | opt_type = orb(data[offset]) |
| 361 | if opt_type == 0xc2: # Jumbo option |
| 362 | jumbo_len = struct.unpack("I", data[offset + 2:offset + 2 + 4])[0] # noqa: E501 |
| 363 | break |
| 364 | offset = 4 * idx + 2 |
| 365 | idx += 1 |
| 366 | |
| 367 | if jumbo_len is None: |
| 368 | log_runtime.info("Scapy did not find a Jumbo option") |
| 369 | jumbo_len = 0 |
| 370 | |
| 371 | tmp_len = hbh_len + jumbo_len |
no test coverage detected