IPv6Header.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright (c) 2021 The Linux Foundation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are
  6. * met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above
  10. * copyright notice, this list of conditions and the following
  11. * disclaimer in the documentation and/or other materials provided
  12. * with the distribution.
  13. * * Neither the name of The Linux Foundation nor the names of its
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
  18. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
  21. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  24. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  25. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  26. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  27. * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #ifndef NETWORK_TRAFFIC_IPV6HEADER_H
  30. #define NETWORK_TRAFFIC_IPV6HEADER_H
  31. #include <netinet/in.h>
  32. #include <arpa/inet.h>
  33. #include "InternetHeader.h"
  34. class IPv6Header: public InternetHeader {
  35. public:
  36. const static unsigned int mSize {40};
  37. explicit IPv6Header(const uint8_t *start) {
  38. unsigned int bufIndex = 0;
  39. setBitsFromArray<uint8_t, 4>(mVersion, start, bufIndex);
  40. setBitsFromArray<uint8_t, 8>(mTrafficClass, start, bufIndex);
  41. setBitsFromArray<uint8_t, 20>(mFlowLabel, start, bufIndex);
  42. setBitsFromArray<uint8_t, 16>(mPayloadLength, start, bufIndex);
  43. setBitsFromArray<uint8_t, 8>(mNextHeader, start, bufIndex);
  44. setBitsFromArray<uint8_t, 8>(mHopLimit, start, bufIndex);
  45. setBitsFromArray<uint8_t, 128>(mSourceIpAddress, start, bufIndex);
  46. setBitsFromArray<uint8_t, 128>(mDestIpAddress, start, bufIndex);
  47. }
  48. DECLARE_BITSET(Version, 4, 6);
  49. DECLARE_BITSET(TrafficClass, 8, 0);
  50. DECLARE_BITSET(FlowLabel, 20, 377341);
  51. DECLARE_BITSET(PayloadLength, 16, 0);
  52. DECLARE_BITSET(NextHeader, 8, 0);
  53. DECLARE_BITSET(HopLimit, 8, 1);
  54. DECLARE_BITSET(SourceIpAddress, 128, 3232236051);
  55. DECLARE_BITSET(DestIpAddress, 128, 3232236136);
  56. IPv6Header() = default;
  57. vector<bool> asVector() const override {
  58. vector<bool> outVec;
  59. auto inserter = [](vector<bool>& vec, auto val){
  60. vector<bool> valAsVector = bitsetToVector<val.size()>(val);
  61. toggleLsbMsb(valAsVector, CHAR_BIT);
  62. vec.insert(vec.end(), valAsVector.begin(), valAsVector.end());};
  63. vector<bool> v = bitsetToVector<4>(mVersion);
  64. vector<bool> v2 = bitsetToVector<8>(mTrafficClass);
  65. v.insert(v.end(), v2.begin(), v2.end());
  66. v2 = bitsetToVector<20>(mFlowLabel);
  67. v.insert(v.end(), v2.begin(), v2.end());
  68. toggleLsbMsb(v, CHAR_BIT);
  69. outVec.insert(outVec.end(), v.begin(), v.end());
  70. inserter(outVec, mPayloadLength);
  71. inserter(outVec, mNextHeader);
  72. inserter(outVec, mHopLimit);
  73. inserter(outVec, mSourceIpAddress);
  74. inserter(outVec, mDestIpAddress);
  75. return outVec;
  76. }
  77. size_t size() const override {
  78. return mSize;
  79. }
  80. void adjust(size_t payloadSize, uint8_t protocol){
  81. mPayloadLength = payloadSize;
  82. mNextHeader = protocol;
  83. }
  84. void tcpChecksumPseudoHeader(uint8_t* pseudoHeaderBuf, uint8_t* ipHeader) const {
  85. memcpy(pseudoHeaderBuf, ipHeader + 8, 32);
  86. *reinterpret_cast<uint32_t*>(pseudoHeaderBuf + 32) =
  87. htons(static_cast<uint16_t>(mPayloadLength.to_ulong()));
  88. *reinterpret_cast<uint32_t*>(pseudoHeaderBuf + 36) = 0;
  89. pseudoHeaderBuf[39] = 6;
  90. }
  91. void udpChecksumPseudoHeader(uint8_t* pseudoHeaderBuf, const uint8_t* ipHeader) const {
  92. memcpy(pseudoHeaderBuf, ipHeader + 8, 32);
  93. *reinterpret_cast<uint32_t*>(pseudoHeaderBuf + 32) = htons(static_cast<uint16_t>(mPayloadLength.to_ulong()));
  94. *reinterpret_cast<uint32_t*>(pseudoHeaderBuf + 36) = 0;
  95. pseudoHeaderBuf[39] = 17;
  96. }
  97. static size_t l3ChecksumPseudoHeaderSize(){
  98. return 40;
  99. }
  100. static size_t getEtherType(){
  101. return 0x86dd;
  102. }
  103. string name() const override {
  104. return string("IPV6");
  105. }
  106. void streamFields(std::ostream &out) const override {
  107. uint8_t buf[32];
  108. vector<bool> v = asVector();
  109. v = vector<bool>(v.begin() + 8, v.end());
  110. toArray(v, buf);
  111. out << "Version: " << mVersion.to_ulong() << ", "
  112. << "IHL: " << mTrafficClass.to_ulong() << ", "
  113. << "DSCP: " << mFlowLabel.to_ulong() << ", "
  114. << "ECN: " << mPayloadLength.to_ulong() << ", "
  115. << "Total Length: " << mNextHeader.to_ulong() << ", "
  116. << "ID: " << mHopLimit.to_ulong() << ", "
  117. << "Source IP Address: " << expandIpv6(buf) << ", "
  118. << "Destination IP Address: " << expandIpv6(buf) << "\n";
  119. }
  120. };
  121. #endif //NETWORK_TRAFFIC_IPV6HEADER_H