1 /*
2  * Contains common Ethernet-related definition, not defined in NDIS
3  *
4  * Copyright (c) 2008-2017 Red Hat, Inc.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met :
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and / or other materials provided with the distribution.
14  * 3. Neither the names of the copyright holders nor the names of their contributors
15  *    may be used to endorse or promote products derived from this software
16  *    without specific prior written permission.
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 #ifndef _ETHERNET_UTILS_H
30 #define _ETHERNET_UTILS_H
31 
32 // assuming <ndis.h> included
33 
34 
35 #define ETH_IS_LOCALLY_ADMINISTERED(Address) \
36         (BOOLEAN)(((PUCHAR)(Address))[0] & ((UCHAR)0x02))
37 
38 #define ETH_IS_EMPTY(Address) \
39     ((((PUCHAR)(Address))[0] == ((UCHAR)0x00)) && (((PUCHAR)(Address))[1] == ((UCHAR)0x00)) && (((PUCHAR)(Address))[2] == ((UCHAR)0x00)) && (((PUCHAR)(Address))[3] == ((UCHAR)0x00)) && (((PUCHAR)(Address))[4] == ((UCHAR)0x00)) && (((PUCHAR)(Address))[5] == ((UCHAR)0x00)))
40 
41 #define ETH_HAS_PRIO_HEADER(Address) \
42     (((PUCHAR)(Address))[12] == ((UCHAR)0x81) && ((PUCHAR)(Address))[13] == ((UCHAR)0x00))
43 
44 #include <pshpack1.h>
45 typedef struct _ETH_HEADER
46 {
47     UCHAR   DstAddr[ETH_LENGTH_OF_ADDRESS];
48     UCHAR   SrcAddr[ETH_LENGTH_OF_ADDRESS];
49     USHORT  EthType;
50 } ETH_HEADER, *PETH_HEADER;
51 #include <poppack.h>
52 
53 #define ETH_HEADER_SIZE                     (sizeof(ETH_HEADER))
54 #define ETH_MIN_PACKET_SIZE                 60
55 #define ETH_PRIORITY_HEADER_OFFSET          12
56 #define ETH_PRIORITY_HEADER_SIZE            4
57 
58 
SetPriorityData(UCHAR * pDest,ULONG priority,ULONG VlanID)59 static void FORCEINLINE SetPriorityData(UCHAR *pDest, ULONG priority, ULONG VlanID)
60 {
61     pDest[0] = 0x81;
62     pDest[2] = (UCHAR)(priority << 5);
63     pDest[2] |= (UCHAR)(VlanID >> 8);
64     pDest[3] |= (UCHAR)VlanID;
65 }
66 
67 typedef enum _tag_eInspectedPacketType
68 {
69     iptUnicast,
70     iptBroadcast,
71     iptMulticast,
72     iptInvalid
73 }eInspectedPacketType;
74 
75 // IP Header RFC 791
76 typedef struct _tagIPv4Header {
77     UCHAR       ip_verlen;             // length in 32-bit units(low nibble), version (high nibble)
78     UCHAR       ip_tos;                // Type of service
79     USHORT      ip_length;             // Total length
80     USHORT      ip_id;                 // Identification
81     USHORT      ip_offset;             // fragment offset and flags
82     UCHAR       ip_ttl;                // Time to live
83     UCHAR       ip_protocol;           // Protocol
84     USHORT      ip_xsum;               // Header checksum
85     ULONG       ip_src;                // Source IP address
86     ULONG       ip_dest;               // Destination IP address
87 } IPv4Header;
88 
89 // TCP header RFC 793
90 typedef struct _tagTCPHeader {
91     USHORT      tcp_src;                // Source port
92     USHORT      tcp_dest;               // Destination port
93     ULONG       tcp_seq;                // Sequence number
94     ULONG       tcp_ack;                // Ack number
95     USHORT      tcp_flags;              // header length and flags
96     USHORT      tcp_window;             // Window size
97     USHORT      tcp_xsum;               // Checksum
98     USHORT      tcp_urgent;             // Urgent
99 }TCPHeader;
100 
101 
102 // UDP Header RFC 768
103 typedef struct _tagUDPHeader {
104     USHORT      udp_src;                // Source port
105     USHORT      udp_dest;               // Destination port
106     USHORT      udp_length;             // length of datagram
107     USHORT      udp_xsum;               // checksum
108 }UDPHeader;
109 
110 
111 
112 #define TCP_CHECKSUM_OFFSET     16
113 #define UDP_CHECKSUM_OFFSET     6
114 #define MAX_IPV4_HEADER_SIZE    60
115 #define MAX_TCP_HEADER_SIZE     60
116 
swap_short(USHORT us)117 static __inline USHORT swap_short(USHORT us)
118 {
119     return (us << 8) | (us >> 8);
120 }
121 
122 
123 #endif
124