Commit 2d5666f2 authored by Brickner_cp's avatar Brickner_cp

IPv6

parent af901f28
......@@ -85,6 +85,7 @@
<Compile Include="PacketBuilderTests.cs" />
<Compile Include="PacketTests.cs" />
<Compile Include="PayloadLayerTests.cs" />
<Compile Include="PppFrameCheckSequenceCalculatorTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="IpV4AddressTests.cs" />
<Compile Include="TcpTests.cs" />
......
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PcapDotNet.Packets.IpV6;
using PcapDotNet.Packets.TestUtils;
namespace PcapDotNet.Packets.Test
{
/// <summary>
/// Summary description for PppFrameCheckSequenceCalculatorTests
/// </summary>
[TestClass]
public class PppFrameCheckSequenceCalculatorTests
{
/// <summary>
/// Gets or sets the test context which provides
/// information about and functionality for the current test run.
/// </summary>
public TestContext TestContext { get; set; }
#region Additional test attributes
//
// You can use the following additional attributes as you write your tests:
//
// Use ClassInitialize to run code before running the first test in the class
// [ClassInitialize()]
// public static void MyClassInitialize(TestContext testContext) { }
//
// Use ClassCleanup to run code after all tests in a class have run
// [ClassCleanup()]
// public static void MyClassCleanup() { }
//
// Use TestInitialize to run code before running each test
// [TestInitialize()]
// public void MyTestInitialize() { }
//
// Use TestCleanup to run code after each test has run
// [TestCleanup()]
// public void MyTestCleanup() { }
//
#endregion
[TestMethod]
public void RandomFcs16Test()
{
Random random = new Random();
for (int i = 0; i != 100; ++i)
{
DataSegment data = random.NextDataSegment(random.Next(1000));
ushort fcs = PppFrameCheckSequenceCalculator.CalculateFcs16(data);
}
}
[TestMethod]
public void GoodFcs16Test()
{
const ushort GoodFcs16 = 0xf0b8;
for (int fcs16Value = 0; fcs16Value <= ushort.MaxValue; ++fcs16Value)
{
ushort extraValue = (ushort)(fcs16Value ^ 0xffff); // Complement.
Assert.AreEqual(GoodFcs16, PppFrameCheckSequenceCalculator.CalculateFcs16((ushort)fcs16Value, new[] { (byte)extraValue, (byte)(extraValue >> 8) }));
}
}
}
}
\ No newline at end of file
......@@ -48,7 +48,7 @@ namespace PcapDotNet.Packets.Ethernet
}
/// <summary>
/// The 4 bytes of the France Check Sequence (FCS).
/// The 4 bytes of the Frame Check Sequence (FCS).
/// Usually, these bytes won't be available because the device remvoed them after checking their validity.
/// We assume they exist when we see that the Ethernet padding pads to 68 bytes or more.
/// If the padding isn't that long or we don't know how to calculate the real payload length, <see langword="null"/> will be returned.
......
......@@ -1232,7 +1232,247 @@ namespace PcapDotNet.Packets.IpV6
IpOptionQuickStartCommon.WriteData(buffer, ref offset, Function, Rate, Ttl, Nonce);
}
}
// Calipso = 0x07,
/// <summary>
/// RFC 5570.
/// CALIPSO Domain of Interpretation (DOI)
/// </summary>
public enum IpV6CalipsoDomainOfInterpretation : uint
{
/// <summary>
/// RFC 5570.
/// Must not appear in any IPv6 packet on any network.
/// </summary>
Null = 0x0000,
}
/// <summary>
/// RFC 1662.
/// </summary>
public static class PppFrameCheckSequenceCalculator
{
private const ushort InitialValue = 0xffff;
/// <summary>
/// Calculate FCS16.
/// </summary>
public static ushort CalculateFcs16(IEnumerable<byte> values)
{
return CalculateFcs16(InitialValue, values);
}
public static ushort CalculateFcs16(ushort fcs, IEnumerable<byte> values)
{
foreach (byte value in values)
{
ushort tableValue = _fcsTable[(fcs ^ value) & 0xff];
fcs = (ushort)((fcs >> 8) ^ tableValue);
}
return fcs;
}
private static readonly ushort[] _fcsTable =
new ushort[]
{
0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
};
}
/// <summary>
/// RFC 5570.
/// <pre>
/// +-----+-------------+-----------------+
/// | Bit | 0-7 | 8-15 |
/// +-----+-------------+-----------------+
/// | 0 | Option Type | Opt Data Len |
/// +-----+-------------+-----------------+
/// | 16 | Domain of Interpretation |
/// | | |
/// +-----+-------------+-----------------+
/// | 48 | Cmpt Lengt | Sens Level |
/// +-----+-------------+-----------------+
/// | 64 | Checksum (CRC-16) |
/// +-----+-------------------------------+
/// | 80 | Compartment Bitmap (Optional) |
/// | ... | |
/// +-----+-------------------------------+
/// </pre>
/// </summary>
[IpV6OptionTypeRegistration(IpV6OptionType.Calipso)]
public class IpV6OptionCalipso : IpV6OptionComplex
{
private static class Offset
{
public const int DomainOfInterpretation = 0;
public const int CompartmentLength = DomainOfInterpretation + sizeof(uint);
public const int SensitivityLevel = CompartmentLength + sizeof(byte);
public const int Checksum = SensitivityLevel + sizeof(byte);
public const int CompartmentBitmap = Checksum + sizeof(ushort);
}
public const int OptionDataMinimumLength = Offset.CompartmentBitmap;
public IpV6OptionCalipso(IpV6CalipsoDomainOfInterpretation domainOfInterpretation, byte sensitivityLevel, ushort checksum, DataSegment compartmentBitmap)
: base(IpV6OptionType.Calipso)
{
if (compartmentBitmap.Length % sizeof(int) != 0)
throw new ArgumentException(string.Format("Compartment Bitmap length must divide by {0}.", sizeof(int)), "compartmentBitmap");
if (compartmentBitmap.Length / sizeof(int) > byte.MaxValue)
{
throw new ArgumentOutOfRangeException(string.Format("Compartment Bitmap length must not be bigger than {0}.", byte.MaxValue * sizeof(int)),
"compartmentBitmap");
}
DomainOfInterpretation = domainOfInterpretation;
SensitivityLevel = sensitivityLevel;
Checksum = checksum;
CompartmentBitmap = compartmentBitmap;
}
/// <summary>
/// The DOI identifies the rules under which this datagram must be handled and protected.
/// </summary>
public IpV6CalipsoDomainOfInterpretation DomainOfInterpretation { get; private set; }
/// <summary>
/// Specifies the size of the Compartment Bitmap field in 32-bit words.
/// The minimum value is zero, which is used only when the information in this packet is not in any compartment.
/// (In that situation, the CALIPSO Sensitivity Label has no need for a Compartment Bitmap).
/// </summary>
public byte CompartmentLength { get { return (byte)(CompartmentLengthInBytes / sizeof(int)); } }
/// <summary>
/// Specifies the size of the Compartment Bitmap field in bytes.
/// The minimum value is zero, which is used only when the information in this packet is not in any compartment.
/// (In that situation, the CALIPSO Sensitivity Label has no need for a Compartment Bitmap).
/// </summary>
public int CompartmentLengthInBytes
{
get { return CompartmentBitmap.Length; }
}
/// <summary>
/// Contains an opaque octet whose value indicates the relative sensitivity of the data contained in this datagram in the context of the indicated DOI.
/// The values of this field must be ordered, with 00000000 being the lowest Sensitivity Level and 11111111 being the highest Sensitivity Level.
/// However, in a typical deployment, not all 256 Sensitivity Levels will be in use.
/// So the set of valid Sensitivity Level values depends upon the CALIPSO DOI in use.
/// This sensitivity ordering rule is necessary so that Intermediate Systems (e.g., routers or MLS guards) will be able to apply MAC policy
/// with minimal per-packet computation and minimal configuration.
/// </summary>
public byte SensitivityLevel { get; private set; }
/// <summary>
/// Contains the a CRC-16 checksum as defined in RFC 1662.
/// The checksum is calculated over the entire CALIPSO option in this packet, including option header, zeroed-out checksum field, option contents,
/// and any required padding zero bits.
/// The checksum must always be computed on transmission and must always be verified on reception.
/// This checksum only provides protection against accidental corruption of the CALIPSO option in cases where neither the underlying medium
/// nor other mechanisms, such as the IP Authentication Header (AH), are available to protect the integrity of this option.
/// Note that the checksum field is always required, even when other integrity protection mechanisms (e.g., AH) are used.
/// </summary>
public ushort Checksum { get; private set; }
public bool IsChecksumCorrect
{
get
{
if (_isChecksumCorrect == null)
{
byte[] domainOfInterpretationBytes = new byte[sizeof(uint)];
domainOfInterpretationBytes.Write(0, (uint)DomainOfInterpretation, Endianity.Big);
ushort expectedValue =
PppFrameCheckSequenceCalculator.CalculateFcs16(
new byte[0].Concat((byte)OptionType, (byte)DataLength).Concat(domainOfInterpretationBytes)
.Concat<byte>(CompartmentLength, SensitivityLevel, 0, 0).Concat(CompartmentBitmap));
_isChecksumCorrect = (Checksum == expectedValue);
}
return _isChecksumCorrect.Value;
}
}
/// <summary>
/// Each bit represents one compartment within the DOI.
/// Each "1" bit within an octet in the Compartment Bitmap field represents a separate compartment under whose rules the data in this packet
/// must be protected.
/// Hence, each "0" bit indicates that the compartment corresponding with that bit is not applicable to the data in this packet.
/// The assignment of identity to individual bits within a Compartment Bitmap for a given DOI is left to the owner of that DOI.
/// This specification represents a Releasability on the wire as if it were an inverted Compartment.
/// So the Compartment Bitmap holds the sum of both logical Releasabilities and also logical Compartments for a given DOI value.
/// The encoding of the Releasabilities in this field is described elsewhere in this document.
/// The Releasability encoding is designed to permit the Compartment Bitmap evaluation to occur without the evaluator necessarily knowing
/// the human semantic associated with each bit in the Compartment Bitmap.
/// In turn, this facilitates the implementation and configuration of Mandatory Access Controls based on the Compartment Bitmap
/// within IPv6 routers or guard devices.
/// </summary>
public DataSegment CompartmentBitmap { get; private set; }
internal override int DataLength
{
get { return OptionDataMinimumLength + CompartmentLengthInBytes; }
}
internal override IpV6Option CreateInstance(DataSegment data)
{
if (data.Length < OptionDataMinimumLength)
return null;
IpV6CalipsoDomainOfInterpretation domainOfInterpretation = (IpV6CalipsoDomainOfInterpretation)data.ReadUInt(Offset.DomainOfInterpretation,
Endianity.Big);
byte compartmentLength = data[Offset.CompartmentLength];
int compartmentLengthInBytes = compartmentLength * sizeof(int);
if (OptionDataMinimumLength + compartmentLengthInBytes > data.Length)
return null;
byte sensitivityLevel = data[Offset.SensitivityLevel];
ushort checksum = data.ReadUShort(Offset.Checksum, Endianity.Big);
DataSegment compartmentBitmap = data.Subsegment(Offset.CompartmentBitmap, compartmentLengthInBytes);
return new IpV6OptionCalipso(domainOfInterpretation, sensitivityLevel, checksum, compartmentBitmap);
}
internal override void WriteData(byte[] buffer, ref int offset)
{
buffer.Write(ref offset, (uint)DomainOfInterpretation, Endianity.Big);
buffer.Write(ref offset, CompartmentLength);
buffer.Write(ref offset, SensitivityLevel);
buffer.Write(ref offset, Checksum, Endianity.Big);
buffer.Write(ref offset, CompartmentBitmap);
}
private bool? _isChecksumCorrect;
}
// SmfDpd = 0x08,
// HomeAddress = 0xC9,
// EndpointIdentification = 0x8A,
......
......@@ -3,6 +3,7 @@
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/INDENT_ANONYMOUS_METHOD_BLOCK/@EntryValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/INDENT_EMBRACED_INITIALIZER_BLOCK/@EntryValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/SPACE_AFTER_TYPECAST_PARENTHESES/@EntryValue">False</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/SPACE_AROUND_MULTIPLICATIVE_OP/@EntryValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/SPACE_BEFORE_SIZEOF_PARENTHESES/@EntryValue">False</s:Boolean>
<s:Int64 x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_LIMIT/@EntryValue">160</s:Int64>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/EventHandlerPatternLong/@EntryValue">$object$_On$event$</s:String>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment