daemon: Fix bogus error message in read_pidfile() when pidfile is empty.
[sliver-openvswitch.git] / lib / packets.h
1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
2  * Junior University
3  * 
4  * We are making the OpenFlow specification and associated documentation
5  * (Software) available for public use and benefit with the expectation
6  * that others will use, modify and enhance the Software and contribute
7  * those enhancements back to the community. However, since we would
8  * like to make the Software available for broadest use, with as few
9  * restrictions as possible permission is hereby granted, free of
10  * charge, to any person obtaining a copy of this Software to deal in
11  * the Software under the copyrights without restriction, including
12  * without limitation the rights to use, copy, modify, merge, publish,
13  * distribute, sublicense, and/or sell copies of the Software, and to
14  * permit persons to whom the Software is furnished to do so, subject to
15  * the following conditions:
16  * 
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  * 
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27  * SOFTWARE.
28  * 
29  * The name and trademarks of copyright holder(s) may NOT be used in
30  * advertising or publicity pertaining to the Software or any
31  * derivatives without specific, written prior permission.
32  */
33 #ifndef PACKETS_H
34 #define PACKETS_H 1
35
36 #include <stdint.h>
37 #include <string.h>
38 #include "compiler.h"
39 #include "random.h"
40 #include "util.h"
41
42 #define ETH_ADDR_LEN           6
43
44 static const uint8_t eth_addr_broadcast[ETH_ADDR_LEN] UNUSED
45     = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
46
47 static inline bool eth_addr_is_broadcast(const uint8_t ea[6])
48 {
49     return (ea[0] & ea[1] & ea[2] & ea[3] & ea[4] & ea[5]) == 0xff;
50 }
51 static inline bool eth_addr_is_multicast(const uint8_t ea[6])
52 {
53     return ea[0] & 1;
54 }
55 static inline bool eth_addr_is_local(const uint8_t ea[6]) 
56 {
57     return ea[0] & 2;
58 }
59 static inline bool eth_addr_is_zero(const uint8_t ea[6]) 
60 {
61     return !(ea[0] | ea[1] | ea[2] | ea[3] | ea[4] | ea[5]);
62 }
63 static inline bool eth_addr_equals(const uint8_t a[ETH_ADDR_LEN],
64                                    const uint8_t b[ETH_ADDR_LEN]) 
65 {
66     return !memcmp(a, b, ETH_ADDR_LEN);
67 }
68 static inline uint64_t eth_addr_to_uint64(const uint8_t ea[ETH_ADDR_LEN])
69 {
70     return (((uint64_t) ea[0] << 40)
71             | ((uint64_t) ea[1] << 32)
72             | ((uint64_t) ea[2] << 24)
73             | ((uint64_t) ea[3] << 16)
74             | ((uint64_t) ea[4] << 8)
75             | ea[5]);
76 }
77 static inline void eth_addr_from_uint64(uint64_t x, uint8_t ea[ETH_ADDR_LEN])
78 {
79     ea[0] = x >> 40;
80     ea[1] = x >> 32;
81     ea[2] = x >> 24;
82     ea[3] = x >> 16;
83     ea[4] = x >> 8;
84     ea[5] = x;
85 }
86 static inline void eth_addr_random(uint8_t ea[ETH_ADDR_LEN])
87 {
88     random_bytes(ea, ETH_ADDR_LEN);
89     ea[0] &= ~1;                /* Unicast. */
90     ea[0] |= 2;                 /* Private. */
91 }
92 /* Returns true if 'ea' is a reserved multicast address, that a bridge must
93  * never forward, false otherwise. */
94 static inline bool eth_addr_is_reserved(const uint8_t ea[ETH_ADDR_LEN])
95 {
96     return (ea[0] == 0x01
97             && ea[1] == 0x80
98             && ea[2] == 0xc2
99             && ea[3] == 0x00
100             && ea[4] == 0x00
101             && (ea[5] & 0xf0) == 0x00);
102 }
103
104 #define ETH_ADDR_FMT                                                    \
105     "%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8
106 #define ETH_ADDR_ARGS(ea)                                   \
107     (ea)[0], (ea)[1], (ea)[2], (ea)[3], (ea)[4], (ea)[5]
108
109 #define ETH_TYPE_IP            0x0800
110 #define ETH_TYPE_ARP           0x0806
111 #define ETH_TYPE_VLAN          0x8100
112
113 #define ETH_HEADER_LEN 14
114 #define ETH_PAYLOAD_MIN 46
115 #define ETH_PAYLOAD_MAX 1500
116 #define ETH_TOTAL_MIN (ETH_HEADER_LEN + ETH_PAYLOAD_MIN)
117 #define ETH_TOTAL_MAX (ETH_HEADER_LEN + ETH_PAYLOAD_MAX)
118 #define ETH_VLAN_TOTAL_MAX (ETH_HEADER_LEN + VLAN_HEADER_LEN + ETH_PAYLOAD_MAX)
119 struct eth_header {
120     uint8_t eth_dst[ETH_ADDR_LEN];
121     uint8_t eth_src[ETH_ADDR_LEN];
122     uint16_t eth_type;
123 } __attribute__((packed));
124 BUILD_ASSERT_DECL(ETH_HEADER_LEN == sizeof(struct eth_header));
125
126 #define LLC_DSAP_SNAP 0xaa
127 #define LLC_SSAP_SNAP 0xaa
128 #define LLC_CNTL_SNAP 3
129
130 #define LLC_HEADER_LEN 3
131 struct llc_header {
132     uint8_t llc_dsap;
133     uint8_t llc_ssap;
134     uint8_t llc_cntl;
135 } __attribute__((packed));
136 BUILD_ASSERT_DECL(LLC_HEADER_LEN == sizeof(struct llc_header));
137
138 #define SNAP_ORG_ETHERNET "\0\0" /* The compiler adds a null byte, so
139                                     sizeof(SNAP_ORG_ETHERNET) == 3. */
140 #define SNAP_HEADER_LEN 5
141 struct snap_header {
142     uint8_t snap_org[3];
143     uint16_t snap_type;
144 } __attribute__((packed));
145 BUILD_ASSERT_DECL(SNAP_HEADER_LEN == sizeof(struct snap_header));
146
147 #define LLC_SNAP_HEADER_LEN (LLC_HEADER_LEN + SNAP_HEADER_LEN)
148 struct llc_snap_header {
149     struct llc_header llc;
150     struct snap_header snap;
151 } __attribute__((packed));
152 BUILD_ASSERT_DECL(LLC_SNAP_HEADER_LEN == sizeof(struct llc_snap_header));
153
154 #define VLAN_VID_MASK 0x0fff
155 #define VLAN_PCP_MASK 0xe000
156
157 #define VLAN_HEADER_LEN 4
158 struct vlan_header {
159     uint16_t vlan_tci;          /* Lowest 12 bits are VLAN ID. */
160     uint16_t vlan_next_type;
161 };
162 BUILD_ASSERT_DECL(VLAN_HEADER_LEN == sizeof(struct vlan_header));
163
164 #define VLAN_ETH_HEADER_LEN (ETH_HEADER_LEN + VLAN_HEADER_LEN)
165 struct vlan_eth_header {
166     uint8_t veth_dst[ETH_ADDR_LEN];
167     uint8_t veth_src[ETH_ADDR_LEN];
168     uint16_t veth_type;         /* Always htons(ETH_TYPE_VLAN). */
169     uint16_t veth_tci;          /* Lowest 12 bits are VLAN ID. */
170     uint16_t veth_next_type;
171 } __attribute__((packed));
172 BUILD_ASSERT_DECL(VLAN_ETH_HEADER_LEN == sizeof(struct vlan_eth_header));
173
174 /* The "(void) (ip)[0]" below has no effect on the value, since it's the first
175  * argument of a comma expression, but it makes sure that 'ip' is a pointer.
176  * This is useful since a common mistake is to pass an integer instead of a
177  * pointer to IP_ARGS. */
178 #define IP_FMT "%"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8
179 #define IP_ARGS(ip)                             \
180         ((void) (ip)[0], ((uint8_t *) ip)[0]),  \
181         ((uint8_t *) ip)[1],                    \
182         ((uint8_t *) ip)[2],                    \
183         ((uint8_t *) ip)[3]
184
185 #define IP_VER(ip_ihl_ver) ((ip_ihl_ver) >> 4)
186 #define IP_IHL(ip_ihl_ver) ((ip_ihl_ver) & 15)
187 #define IP_IHL_VER(ihl, ver) (((ver) << 4) | (ihl))
188
189 #define IP_TYPE_ICMP 1
190 #define IP_TYPE_TCP 6
191 #define IP_TYPE_UDP 17
192
193 #define IP_VERSION 4
194
195 #define IP_DONT_FRAGMENT  0x4000 /* Don't fragment. */
196 #define IP_MORE_FRAGMENTS 0x2000 /* More fragments. */
197 #define IP_FRAG_OFF_MASK  0x1fff /* Fragment offset. */
198 #define IP_IS_FRAGMENT(ip_frag_off) \
199         ((ip_frag_off) & htons(IP_MORE_FRAGMENTS | IP_FRAG_OFF_MASK))
200
201 #define IP_HEADER_LEN 20
202 struct ip_header {
203     uint8_t ip_ihl_ver;
204     uint8_t ip_tos;
205     uint16_t ip_tot_len;
206     uint16_t ip_id;
207     uint16_t ip_frag_off;
208     uint8_t ip_ttl;
209     uint8_t ip_proto;
210     uint16_t ip_csum;
211     uint32_t ip_src;
212     uint32_t ip_dst;
213 };
214 BUILD_ASSERT_DECL(IP_HEADER_LEN == sizeof(struct ip_header));
215
216 #define ICMP_HEADER_LEN 4
217 struct icmp_header {
218     uint8_t icmp_type;
219     uint8_t icmp_code;
220     uint16_t icmp_csum;
221 };
222 BUILD_ASSERT_DECL(ICMP_HEADER_LEN == sizeof(struct icmp_header));
223
224 #define UDP_HEADER_LEN 8
225 struct udp_header {
226     uint16_t udp_src;
227     uint16_t udp_dst;
228     uint16_t udp_len;
229     uint16_t udp_csum;
230 };
231 BUILD_ASSERT_DECL(UDP_HEADER_LEN == sizeof(struct udp_header));
232
233 #define TCP_FIN 0x01
234 #define TCP_SYN 0x02
235 #define TCP_RST 0x04
236 #define TCP_PSH 0x08
237 #define TCP_ACK 0x10
238 #define TCP_URG 0x20
239
240 #define TCP_FLAGS(tcp_ctl) (htons(tcp_ctl) & 0x003f)
241 #define TCP_OFFSET(tcp_ctl) (htons(tcp_ctl) >> 12)
242
243 #define TCP_HEADER_LEN 20
244 struct tcp_header {
245     uint16_t tcp_src;
246     uint16_t tcp_dst;
247     uint32_t tcp_seq;
248     uint32_t tcp_ack;
249     uint16_t tcp_ctl;
250     uint16_t tcp_winsz;
251     uint16_t tcp_csum;
252     uint16_t tcp_urg;
253 };
254 BUILD_ASSERT_DECL(TCP_HEADER_LEN == sizeof(struct tcp_header));
255
256 #define ARP_HRD_ETHERNET 1
257 #define ARP_PRO_IP 0x0800
258 #define ARP_OP_REQUEST 1
259 #define ARP_OP_REPLY 2
260
261 #define ARP_ETH_HEADER_LEN 28
262 struct arp_eth_header {
263     /* Generic members. */
264     uint16_t ar_hrd;           /* Hardware type. */
265     uint16_t ar_pro;           /* Protocol type. */
266     uint8_t ar_hln;            /* Hardware address length. */
267     uint8_t ar_pln;            /* Protocol address length. */
268     uint16_t ar_op;            /* Opcode. */
269
270     /* Ethernet+IPv4 specific members. */
271     uint8_t ar_sha[ETH_ADDR_LEN]; /* Sender hardware address. */
272     uint32_t ar_spa;           /* Sender protocol address. */
273     uint8_t ar_tha[ETH_ADDR_LEN]; /* Target hardware address. */
274     uint32_t ar_tpa;           /* Target protocol address. */
275 } __attribute__((packed));
276 BUILD_ASSERT_DECL(ARP_ETH_HEADER_LEN == sizeof(struct arp_eth_header));
277
278 #endif /* packets.h */