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