예제 #1
0
        /*
         * Function: Parse
         * Description: Rearrange and then parse the rearranged data.
         */
        public byte Parse(CircularFrameBuffer cfb, bool skip = false)
        {
            var result = Rearrange(cfb, skip);

            ParseArranged();
            return(result);
        }
 public static void SetDestInterface(byte portNumber, CircularFrameBuffer cfb)
 {
     Emu.Status = 0xff;
     cfb.Peek();
     cfb.PeekData.TuserLow = (cfb.PeekData.TuserLow & 0xFFFFFFFF00FFFFFF) | ((ulong)portNumber << 24);
     cfb.UpdatePeek(cfb.PeekData);
 }
예제 #3
0
        /*
         * Function: PushHeader
         * Description: Deparse the provided data and push onto the buffer
         */
        public static void PushHeader(CircularFrameBuffer cfb, ulong destMac, ulong srcMac, uint ethertype,
                                      ulong metadata)
        {
            WriteToBuffer(cfb.PushData, destMac, srcMac, ethertype, metadata);

            cfb.Push(cfb.PushData);
        }
예제 #4
0
        /*
         * Function: UpdateHeader
         * Description: Deparse the provided header data and write to current peek location
         */
        public void UpdateHeader(CircularFrameBuffer cfb, ulong destMac, ulong srcMac, uint ethertype,
                                 ulong metadata)
        {
            cfb.RewindPeek();
            WriteToBuffer(cfb.PeekData, destMac, srcMac, ethertype, metadata);

            cfb.UpdatePeek(cfb.PeekData);
        }
예제 #5
0
        /*
         * Function: Parse
         * Description: Parse peeked data from buffer
         */
        public int Parse(CircularFrameBuffer cfb)
        {
            //while (!cfb.CanAdvance()) return 2;

            cfb.Peek();
            //lock (cfb.PeekData)
            {
                return(Parse(cfb.PeekData.Tdata0, cfb.PeekData.Tdata1, cfb.PeekData.TuserLow));
            }
        }
예제 #6
0
        /*
         * Function: Rearrange
         * Description: Rearrange the buffer data into a temporary holding buffer to prepare for parsing
         */
        public byte Rearrange(CircularFrameBuffer cfb, bool skip = false)
        {
            //lock (cfb.PeekData)
            {
                IHL = (byte)((cfb.PeekData.Tdata1 >> 48) & 0x0f);

                data_0  = cfb.PeekData.Tdata1 >> 48;
                data_0 |= cfb.PeekData.Tdata2 << 16;
                data_1  = cfb.PeekData.Tdata2 >> 48;
                data_1 |= cfb.PeekData.Tdata3 << 16;
                data_2  = cfb.PeekData.Tdata3 >> 48;


                if (!cfb.CanAdvance())
                {
                    return(2);
                }

                cfb.AdvancePeek();


                if (IHL == 5)
                {
                    data_2 |= (cfb.PeekData.Tdata0 & 0xffff) << 16;
                }
                else
                {
                    data_2 |= cfb.PeekData.Tdata0 << 16;
                    if (IHL == 7)
                    {
                        data_3  = cfb.PeekData.Tdata0 >> 48;
                        data_3 |= (cfb.PeekData.Tdata1 & 0xffff) << 16;
                    }
                    else if (IHL == 8)
                    {
                        data_3  = cfb.PeekData.Tdata0 >> 48;
                        data_3 |= cfb.PeekData.Tdata1 << 16;
                    }
                }
            }

            return(0);
        }
예제 #7
0
        /*
         * Function: WriteHeader
         * Description: Write the temporary buffer back into the main buffer.
         */
        public void WriteHeader(CircularFrameBuffer cfb, bool push, bool assembled = false)
        {
            if (!assembled)
            {
                AssembleHeader();
            }

            cfb.RewindPeek();
            //lock (cfb.PeekData)
            {
                cfb.PeekData.Tdata1 = (cfb.PeekData.Tdata1 & 0x0000ffffffffffff) | (data_0 << 48);
                cfb.PeekData.Tdata2 = (data_0 >> 16) | (data_1 << 48);
                cfb.PeekData.Tdata3 = (data_1 >> 16) | (data_2 << 48);

                cfb.UpdatePeek(cfb.PeekData);

                if (!push)
                {
                    cfb.AdvancePeek();
                }

                if (IHL == 5)
                {
                    cfb.PeekData.Tdata0 = (cfb.PeekData.Tdata0 & 0xffffffffffff0000) | (data_2 >> 16);
                }
                else if (IHL == 6)
                {
                    cfb.PeekData.Tdata0 = (cfb.PeekData.Tdata0 & 0xffff000000000000) | (data_2 >> 16);
                }
                else if (IHL == 7)
                {
                    cfb.PeekData.Tdata0 = (data_2 >> 16) | (data_3 << 48);
                    cfb.PeekData.Tdata1 = (cfb.PeekData.Tdata1 & 0xffffffffffff0000) | (data_3 >> 16);
                }
                else if (IHL == 8)
                {
                    cfb.PeekData.Tdata0 = (data_2 >> 16) | (data_3 << 48);
                    cfb.PeekData.Tdata1 = (cfb.PeekData.Tdata1 & 0xffff000000000000) | (data_3 >> 16);
                }
            }
        }
예제 #8
0
        /*
         * Function: Parse
         * Description: Parse peeked data from buffer
         */
        public byte Parse(CircularFrameBuffer cfb, bool skip = false)
        {
            if (!skip)
            //lock (cfb.PeekData)
            {
                Version      = (byte)((cfb.PeekData.Tdata1 >> 52) & 0x0f);
                TrafficClass =
                    (byte)(((cfb.PeekData.Tdata1 >> 48) & 0x0f) | (((cfb.PeekData.Tdata1 >> 56) & 0x0f) << 4));

                PayloadLength = (uint)((cfb.PeekData.Tdata2 >> 16) & 0x00ffff);
                Protocol      = (byte)((cfb.PeekData.Tdata2 >> 32) & 0x00ff);
                HopLimit      = (byte)((cfb.PeekData.Tdata2 >> 40) & 0x00ff);

                SrcIp1        = (cfb.PeekData.Tdata2 >> 48) & 0x00ffff;
                SrcIp1       |= (cfb.PeekData.Tdata3 & 0x00ffffffffffff) << 16;
                _tmp_src_ip_2 = (cfb.PeekData.Tdata3 >> 48) & 0x00ffff;
            }

            if (!cfb.CanAdvance())
            {
                return(2);
            }

            cfb.AdvancePeek();

            //lock (cfb.PeekData)
            {
                SrcIp2   = _tmp_src_ip_2 | ((cfb.PeekData.Tdata0 & 0x00ffffffffffff) << 16);
                DestIp1  = (cfb.PeekData.Tdata0 >> 48) & 0x00ffff;
                DestIp1 |= (cfb.PeekData.Tdata1 & 0x00ffffffffffff) << 16;
                DestIp2  = (cfb.PeekData.Tdata1 >> 48) & 0x00ffff;
                DestIp2 |= (cfb.PeekData.Tdata2 & 0x00ffffffffffff) << 16;
            }

            return(0);
        }
예제 #9
0
        /*
         * Function: PushHeader
         * Description: Deparse the data within the header and push onto the buffer
         */
        public void PushHeader(CircularFrameBuffer cfb)
        {
            WriteToBuffer(cfb.PushData);

            cfb.Push(cfb.PushData);
        }
예제 #10
0
        /*
         * Function: Parse
         * Description: Parse peeked data from buffer
         */
        public byte Parse(CircularFrameBuffer cfb, uint ipHeaderLength, bool skip = false)
        {
            var startloc = 1 + ipHeaderLength / 64 - 4;
            var offset   = (int)(48 + ipHeaderLength % 64);

            if (offset >= 64)
            {
                offset -= 64;
                startloc++;
            }

            ulong data0, data1, data2;

            switch (startloc)
            {
            case 0:
                data0 = cfb.PeekData.Tdata0;
                data1 = cfb.PeekData.Tdata1;
                data2 = cfb.PeekData.Tdata2;
                break;

            case 1:
                data0 = cfb.PeekData.Tdata1;
                data1 = cfb.PeekData.Tdata2;
                data2 = cfb.PeekData.Tdata3;
                break;

            case 2:
                data0 = cfb.PeekData.Tdata2;
                data1 = cfb.PeekData.Tdata3;
                data2 = 0;
                break;

            default:
                return(1);
            }

            if (offset <= 48)
            {
                SrcPort = (uint)(data0 >> offset) & 0xFFFF;
                offset += 16;
            }
            else
            {
                SrcPort  = (uint)(data0 >> offset);
                data0    = data1;
                data1    = data2;
                SrcPort |= (uint)((uint)data0 & (0xFFFF >> offset)) << offset;
                offset   = 64 - offset;
            }

            if (offset <= 48)
            {
                DestPort = (uint)(data0 >> offset) & 0xFFFF;
                offset  += 16;
            }
            else
            {
                DestPort  = (uint)(data0 >> offset);
                data0     = data1;
                data1     = data2;
                DestPort |= (uint)((uint)data0 & (0xFFFF >> offset)) << offset;
                offset    = 64 - offset;
            }

            if (offset <= 32)
            {
                SeqNumber = (uint)(data0 >> offset) & 0xFFFFFFFF;
                offset   += 32;
            }
            else
            {
                SeqNumber  = (uint)(data0 >> offset);
                data0      = data1;
                data1      = data2;
                SeqNumber |= (uint)((uint)data0 & (0xFFFFFFFF >> offset)) << offset;
                offset     = 64 - offset;
            }

            if (offset <= 48)
            {
                uint tmp = (uint)(data0 >> offset) & 0xFFFF;
                DataOffset = (byte)((tmp & 0xF0) >> 4);

                offset += 16;
            }
            else
            {
                uint tmp = (uint)(data0 >> offset);
                data0      = data1;
                data1      = data2;
                tmp       |= (uint)((uint)data0 & (0xFFFF >> offset)) << offset;
                offset     = 64 - offset;
                DataOffset = (byte)((tmp & 0xF0) >> 4);
            }

            if (offset <= 48)
            {
                WindowSize = (uint)(data0 >> offset) & 0xFFFF;
                offset    += 16;
            }
            else
            {
                WindowSize  = (uint)(data0 >> offset);
                data0       = data1;
                data1       = data2;
                WindowSize |= (uint)((uint)data0 & (0xFFFF >> offset)) << offset;
                offset      = 64 - offset;
            }


            return(0);
        }
예제 #11
0
        /*
         * Function: Parse
         * Description: Parse peeked data from buffer
         */
        public byte Parse(CircularFrameBuffer cfb, uint ipHeaderLength, bool skip = false)
        {
            var startloc = 1 + ipHeaderLength / 64 - 4;
            var offset   = (int)(48 + ipHeaderLength % 64);

            if (offset >= 64)
            {
                offset -= 64;
                startloc++;
            }

            ulong data0, data1;

            switch (startloc)
            {
            case 0:
                data0 = cfb.PeekData.Tdata0;
                data1 = cfb.PeekData.Tdata1;
                break;

            case 1:
                data0 = cfb.PeekData.Tdata1;
                data1 = cfb.PeekData.Tdata2;
                break;

            case 2:
                data0 = cfb.PeekData.Tdata2;
                data1 = cfb.PeekData.Tdata3;
                break;

            default:
                return(1);
            }

            if (offset <= 48)
            {
                SrcPort = (uint)(data0 >> offset) & 0xFFFF;
                offset += 16;
            }
            else
            {
                SrcPort  = (uint)(data0 >> offset);
                data0    = data1;
                SrcPort |= (uint)((uint)data0 & (0xFFFF >> offset)) << offset;
                offset   = 64 - offset;
            }

            if (offset <= 48)
            {
                DestPort = (uint)(data0 >> offset) & 0xFFFF;
                offset  += 16;
            }
            else
            {
                DestPort  = (uint)(data0 >> offset);
                data0     = data1;
                DestPort |= (uint)((uint)data0 & (0xFFFF >> offset)) << offset;
                offset    = 64 - offset;
            }

            if (offset <= 48)
            {
                Length  = (uint)(data0 >> offset) & 0xFFFF;
                offset += 16;
            }
            else
            {
                Length  = (uint)(data0 >> offset);
                data0   = data1;
                Length |= (uint)((uint)data0 & (0xFFFF >> offset)) << offset;
                offset  = 64 - offset;
            }

            if (offset <= 48)
            {
                Checksum = (uint)(data0 >> offset) & 0xFFFF;
                offset  += 16;
            }
            else
            {
                Checksum  = (uint)(data0 >> offset);
                data0     = data1;
                Checksum |= (uint)((uint)data0 & (0xFFFF >> offset)) << offset;
                offset    = 64 - offset;
            }

            return(0);
        }
예제 #12
0
        /*
         * Function: ParseSplit
         * Description: A two-part parser to allow the first section of the packet to be parsed.
         */
        public byte ParseSplit(CircularFrameBuffer cfb, bool skip = false)
        {
            //lock (cfb.PeekData)
            {
                if (!skip)
                {
                    data_0  = cfb.PeekData.Tdata1 >> 48;
                    data_0 |= cfb.PeekData.Tdata2 << 16;
                    data_1  = cfb.PeekData.Tdata2 >> 48;
                    data_1 |= cfb.PeekData.Tdata3 << 16;
                    data_2  = cfb.PeekData.Tdata3 >> 48;

                    Version = (byte)((cfb.PeekData.Tdata1 >> 52) & 0x0f);
                    if (Version != 4)
                    {
                        DebugFunctions.push_interrupt(DebugFunctions.Errors.ILLEGAL_PACKET_FORMAT);
                    }
                    IHL = (byte)((cfb.PeekData.Tdata1 >> 48) & 0x0f);
                    if (IHL < 5 || IHL > 8)
                    {
                        DebugFunctions.push_interrupt(DebugFunctions.Errors.ILLEGAL_PACKET_FORMAT);
                    }
                    DSCP           = (byte)((cfb.PeekData.Tdata1 >> 58) & 0x3F);
                    ECN            = (byte)((cfb.PeekData.Tdata1 >> 56) & 0x3);
                    TotalLength    = (uint)(cfb.PeekData.Tdata2 & 0x00ffff);
                    Identification = (uint)((cfb.PeekData.Tdata2 >> 16) & 0x00ffff);
                    Flags          = (byte)((cfb.PeekData.Tdata2 >> 37) & 0x07);
                    FragmentOffset = (uint)((((cfb.PeekData.Tdata2 >> 32) & 0x01f) << 8) |
                                            ((cfb.PeekData.Tdata2 >> 40) & 0x0ff));
                    TTL            = (byte)((cfb.PeekData.Tdata2 >> 48) & 0x00ff);
                    Protocol       = (byte)((cfb.PeekData.Tdata2 >> 56) & 0x00ff);
                    HeaderChecksum = (uint)cfb.PeekData.Tdata3 & 0x00ffff;
                    SrcIp          = (cfb.PeekData.Tdata3 >> 16) & 0x00ffffffff;
                    _tmp_dest_ip   = (cfb.PeekData.Tdata3 >> 48) & 0x00ffff;
                }


                if (!cfb.CanAdvance())
                {
                    return(2);
                }

                cfb.AdvancePeek();

                if (IHL == 5)
                {
                    data_2 |= (cfb.PeekData.Tdata0 & 0xffff) << 16;
                }
                else if (IHL == 6)
                {
                    data_2 |= cfb.PeekData.Tdata0 << 16;
                    if (IHL == 7)
                    {
                        data_3  = cfb.PeekData.Tdata0 >> 48;
                        data_3 |= (cfb.PeekData.Tdata1 & 0xffff) << 16;
                    }
                    else if (IHL == 8)
                    {
                        data_3  = cfb.PeekData.Tdata0 >> 48;
                        data_3 |= cfb.PeekData.Tdata1 << 16;
                    }
                }

                DestIp = _tmp_dest_ip | ((cfb.PeekData.Tdata0 & 0x00ffff) << 16);
            }

            return(0);
        }
예제 #13
0
 /*
  * Function: Parse
  * Description: Parse peeked data from buffer
  */
 public int Parse(CircularFrameBuffer cfb)
 {
     cfb.Peek();
     return(Parse(cfb.PeekData.TuserLow));
 }
예제 #14
0
 /*
  * Function: UpdateHeader
  * Description: Deparse the header data and write to current peek location
  */
 public void UpdateHeader(CircularFrameBuffer cfb)
 {
     UpdateHeader(cfb, DestMac, SrcMac, Ethertype, Metadata);
 }
예제 #15
0
        public void Parse(CircularFrameBuffer cfb, bool newFrame)
        {
            if (newFrame)
            {
                _ethParsed       = false;
                _ipParsed1       = false;
                _ipParsed2       = false;
                IpVersion        = 0;
                _transportParsed = false;
                Protocol         = 0;
            }

            if (!_ethParsed)
            {
                if (ep.Parse(cfb) == 0)
                {
                    _ethParsed = true;
                    if (ep.IsIPv4)
                    {
                        IpVersion = 4;
                    }
                    else if (ep.IsIPv6)
                    {
                        IpVersion = 6;
                    }
                }
            }

            switch (IpVersion)
            {
            case 4:
                if (!_ipParsed1)
                {
                    _ipParsed1 = true;
                    if (ipv4.Parse(cfb, false) == 0)
                    {
                        _ipParsed2 = true;
                    }
                }
                else if (!_ipParsed2)
                {
                    if (ipv4.Parse(cfb, true) == 0)
                    {
                        _ipParsed2 = true;
                    }
                }

                _ipHeaderLength = ipv4.IHL * 4U * 8U;

                if (_ipParsed2)
                {
                    Protocol = ipv4.Protocol;
                }


                break;

            case 6:
                if (!_ipParsed1)
                {
                    _ipParsed1 = true;
                    if (ipv6.Parse(cfb, false) == 0)
                    {
                        _ipParsed2 = true;
                    }
                }
                else if (!_ipParsed2)
                {
                    if (ipv6.Parse(cfb, true) == 0)
                    {
                        _ipParsed2 = true;
                    }
                }

                _ipHeaderLength = 320U;

                if (_ipParsed2)
                {
                    Protocol = ipv6.Protocol;
                }
                break;

            default:
                return;
            }

            switch (Protocol)
            {
            case 6:
                tcp.Parse(cfb, _ipHeaderLength);
                break;

            case 17:
                udp.Parse(cfb, _ipHeaderLength);
                break;

            default:
                return;
            }
        }