Initial import
[sliver-openvswitch.git] / include / packets.h
1 #ifndef PACKETS_H
2 #define PACKETS_H 1
3
4 #include <stdint.h>
5 #include "util.h"
6
7 /* Ethernet frames. */
8 #define ETH_ADDR_LEN           6
9
10 #define ETH_TYPE_IP            0x0800
11 #define ETH_TYPE_ARP           0x0806
12 #define ETH_TYPE_VLAN          0x8100
13
14 #define ETH_HEADER_LEN 14
15 #define ETH_PAYLOAD_MIN 46
16 #define ETH_TOTAL_MIN (ETH_HEADER_LEN + ETH_PAYLOAD_MIN)
17 struct eth_header {
18     uint8_t eth_dst[ETH_ADDR_LEN];
19     uint8_t eth_src[ETH_ADDR_LEN];
20     uint16_t eth_type;
21 };
22 BUILD_ASSERT_DECL(ETH_HEADER_LEN == sizeof(struct eth_header));
23
24 #define LLC_DSAP_SNAP 0xaa
25 #define LLC_SSAP_SNAP 0xaa
26 #define LLC_CNTL_SNAP 3
27
28 #define LLC_HEADER_LEN 3
29 struct llc_header {
30     uint8_t llc_dsap;
31     uint8_t llc_ssap;
32     uint8_t llc_cntl;
33 };
34 BUILD_ASSERT_DECL(LLC_HEADER_LEN == sizeof(struct llc_header));
35
36 #define SNAP_ORG_ETHERNET "\0\0" /* The compiler adds a null byte, so
37                                     sizeof(SNAP_ORG_ETHERNET) == 3. */
38 #define SNAP_HEADER_LEN 5
39 struct snap_header {
40     uint8_t snap_org[3];
41     uint16_t snap_type;
42 } __attribute__((packed));
43 BUILD_ASSERT_DECL(SNAP_HEADER_LEN == sizeof(struct snap_header));
44
45 #define LLC_SNAP_HEADER_LEN (LLC_HEADER_LEN + SNAP_HEADER_LEN)
46 struct llc_snap_header {
47     struct llc_header llc;
48     struct snap_header snap;
49 };
50 BUILD_ASSERT_DECL(LLC_SNAP_HEADER_LEN == sizeof(struct llc_snap_header));
51
52 #define VLAN_VID 0x0fff
53
54 #define VLAN_HEADER_LEN 4
55 struct vlan_header {
56     uint16_t vlan_tci;          /* Lowest 12 bits are VLAN ID. */
57     uint16_t vlan_next_type;
58 };
59 BUILD_ASSERT_DECL(VLAN_HEADER_LEN == sizeof(struct vlan_header));
60
61 #define IP_VER(ip_ihl_ver) ((ip_ihl_ver) >> 4)
62 #define IP_IHL(ip_ihl_ver) ((ip_ihl_ver) & 15)
63
64 #define IP_TYPE_TCP 6
65 #define IP_TYPE_UDP 17
66
67 #define IP_HEADER_LEN 20
68 struct ip_header {
69     uint8_t ip_ihl_ver;
70     uint8_t ip_tos;
71     uint16_t ip_tot_len;
72     uint16_t ip_id;
73     uint16_t ip_frag_off;
74     uint8_t ip_ttl;
75     uint8_t ip_proto;
76     uint16_t ip_csum;
77     uint32_t ip_src;
78     uint32_t ip_dst;
79 };
80 BUILD_ASSERT_DECL(IP_HEADER_LEN == sizeof(struct ip_header));
81
82 #define UDP_HEADER_LEN 8
83 struct udp_header {
84     uint16_t udp_src;
85     uint16_t udp_dst;
86     uint16_t udp_len;
87     uint16_t udp_csum;
88 };
89 BUILD_ASSERT_DECL(UDP_HEADER_LEN == sizeof(struct udp_header));
90
91 #define TCP_FIN 0x01
92 #define TCP_SYN 0x02
93 #define TCP_RST 0x04
94 #define TCP_PSH 0x08
95 #define TCP_ACK 0x10
96 #define TCP_URG 0x20
97
98 #define TCP_FLAGS(tcp_ctl) (htons(tcp_ctl) & 0x003f)
99 #define TCP_OFFSET(tcp_ctl) (htons(tcp_ctl) >> 12)
100
101 #define TCP_HEADER_LEN 20
102 struct tcp_header {
103     uint16_t tcp_src;
104     uint16_t tcp_dst;
105     uint32_t tcp_seq;
106     uint32_t tcp_ack;
107     uint16_t tcp_ctl;
108     uint16_t tcp_winsz;
109     uint16_t tcp_csum;
110     uint16_t tcp_urg;
111 };
112 BUILD_ASSERT_DECL(TCP_HEADER_LEN == sizeof(struct tcp_header));
113
114 #define ARP_HRD_ETHERNET 1
115 #define ARP_PRO_IP 0x0800
116 #define ARP_OP_REQUEST 1
117 #define ARP_OP_REPLY 2
118
119 #define ARP_ETH_HEADER_LEN 28
120 struct arp_eth_header {
121     /* Generic members. */
122     uint16_t ar_hrd;           /* Hardware type. */
123     uint16_t ar_pro;           /* Protocol type. */
124     uint8_t ar_hln;            /* Hardware address length. */
125     uint8_t ar_pln;            /* Protocol address length. */
126     uint16_t ar_op;            /* Opcode. */
127
128     /* Ethernet+IPv4 specific members. */
129     uint8_t ar_sha[ETH_ADDR_LEN]; /* Sender hardware address. */
130     uint32_t ar_spa;           /* Sender protocol address. */
131     uint8_t ar_tha[ETH_ADDR_LEN]; /* Target hardware address. */
132     uint32_t ar_tpa;           /* Target protocol address. */
133 } __attribute__((packed));
134 BUILD_ASSERT_DECL(ARP_ETH_HEADER_LEN == sizeof(struct arp_eth_header));
135
136 #endif /* packets.h */