• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "quiche/quic/qbone/platform/icmp_packet.h"
6 
7 #include <netinet/ip6.h>
8 
9 #include "absl/strings/string_view.h"
10 #include "quiche/quic/qbone/platform/internet_checksum.h"
11 #include "quiche/common/quiche_endian.h"
12 
13 namespace quic {
14 namespace {
15 
16 constexpr size_t kIPv6AddressSize = sizeof(in6_addr);
17 constexpr size_t kIPv6HeaderSize = sizeof(ip6_hdr);
18 constexpr size_t kICMPv6HeaderSize = sizeof(icmp6_hdr);
19 constexpr size_t kIPv6MinPacketSize = 1280;
20 
21 // Hop limit set to 255 to satisfy:
22 // https://datatracker.ietf.org/doc/html/rfc4861#section-11.2
23 constexpr size_t kIcmpTtl = 255;
24 constexpr size_t kICMPv6BodyMaxSize =
25     kIPv6MinPacketSize - kIPv6HeaderSize - kICMPv6HeaderSize;
26 
27 struct ICMPv6Packet {
28   ip6_hdr ip_header;
29   icmp6_hdr icmp_header;
30   uint8_t body[kICMPv6BodyMaxSize];
31 };
32 
33 // pseudo header as described in RFC 2460 Section 8.1 (excluding addresses)
34 struct IPv6PseudoHeader {
35   uint32_t payload_size{};
36   uint8_t zeros[3] = {0, 0, 0};
37   uint8_t next_header = IPPROTO_ICMPV6;
38 };
39 
40 }  // namespace
41 
CreateIcmpPacket(in6_addr src,in6_addr dst,const icmp6_hdr & icmp_header,absl::string_view body,const std::function<void (absl::string_view)> & cb)42 void CreateIcmpPacket(in6_addr src, in6_addr dst, const icmp6_hdr& icmp_header,
43                       absl::string_view body,
44                       const std::function<void(absl::string_view)>& cb) {
45   const size_t body_size = std::min(body.size(), kICMPv6BodyMaxSize);
46   const size_t payload_size = kICMPv6HeaderSize + body_size;
47 
48   ICMPv6Packet icmp_packet{};
49   // Set version to 6.
50   icmp_packet.ip_header.ip6_vfc = 0x6 << 4;
51   // Set the payload size, protocol and TTL.
52   icmp_packet.ip_header.ip6_plen =
53       quiche::QuicheEndian::HostToNet16(payload_size);
54   icmp_packet.ip_header.ip6_nxt = IPPROTO_ICMPV6;
55   icmp_packet.ip_header.ip6_hops = kIcmpTtl;
56   // Set the source address to the specified self IP.
57   icmp_packet.ip_header.ip6_src = src;
58   icmp_packet.ip_header.ip6_dst = dst;
59 
60   icmp_packet.icmp_header = icmp_header;
61   // Per RFC 4443 Section 2.3, set checksum field to 0 prior to computing it
62   icmp_packet.icmp_header.icmp6_cksum = 0;
63 
64   IPv6PseudoHeader pseudo_header{};
65   pseudo_header.payload_size = quiche::QuicheEndian::HostToNet32(payload_size);
66 
67   InternetChecksum checksum;
68   // Pseudoheader.
69   checksum.Update(icmp_packet.ip_header.ip6_src.s6_addr, kIPv6AddressSize);
70   checksum.Update(icmp_packet.ip_header.ip6_dst.s6_addr, kIPv6AddressSize);
71   checksum.Update(reinterpret_cast<char*>(&pseudo_header),
72                   sizeof(pseudo_header));
73   // ICMP header.
74   checksum.Update(reinterpret_cast<const char*>(&icmp_packet.icmp_header),
75                   sizeof(icmp_packet.icmp_header));
76   // Body.
77   checksum.Update(body.data(), body_size);
78   icmp_packet.icmp_header.icmp6_cksum = checksum.Value();
79 
80   memcpy(icmp_packet.body, body.data(), body_size);
81 
82   const char* packet = reinterpret_cast<char*>(&icmp_packet);
83   const size_t packet_size = offsetof(ICMPv6Packet, body) + body_size;
84 
85   cb(absl::string_view(packet, packet_size));
86 }
87 
88 }  // namespace quic
89