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