treewide: Remove trailing whitespace
[sliver-openvswitch.git] / lib / packets.h
1 /*
2  * Copyright (c) 2008, 2009, 2010 Nicira Networks.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef PACKETS_H
18 #define PACKETS_H 1
19
20 #include <inttypes.h>
21 #include <stdint.h>
22 #include <string.h>
23 #include "compiler.h"
24 #include "random.h"
25 #include "util.h"
26
27 struct ofpbuf;
28
29 bool dpid_from_string(const char *s, uint64_t *dpidp);
30
31 #define ETH_ADDR_LEN           6
32
33 static const uint8_t eth_addr_broadcast[ETH_ADDR_LEN] OVS_UNUSED
34     = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
35
36 static const uint8_t eth_addr_stp[ETH_ADDR_LEN] OVS_UNUSED
37     = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x01 };
38
39 static inline bool eth_addr_is_broadcast(const uint8_t ea[6])
40 {
41     return (ea[0] & ea[1] & ea[2] & ea[3] & ea[4] & ea[5]) == 0xff;
42 }
43
44 static inline bool eth_addr_is_multicast(const uint8_t ea[6])
45 {
46     return ea[0] & 1;
47 }
48 static inline bool eth_addr_is_local(const uint8_t ea[6])
49 {
50     /* Local if it is either a locally administered address or a Nicira random
51      * address. */
52     return !!(ea[0] & 2)
53        || (ea[0] == 0x00 && ea[1] == 0x23 && ea[2] == 0x20 && !!(ea[3] & 0x80));
54 }
55 static inline bool eth_addr_is_zero(const uint8_t ea[6])
56 {
57     return !(ea[0] | ea[1] | ea[2] | ea[3] | ea[4] | ea[5]);
58 }
59 static inline bool eth_addr_equals(const uint8_t a[ETH_ADDR_LEN],
60                                    const uint8_t b[ETH_ADDR_LEN])
61 {
62     return !memcmp(a, b, ETH_ADDR_LEN);
63 }
64 static inline uint64_t eth_addr_to_uint64(const uint8_t ea[ETH_ADDR_LEN])
65 {
66     return (((uint64_t) ea[0] << 40)
67             | ((uint64_t) ea[1] << 32)
68             | ((uint64_t) ea[2] << 24)
69             | ((uint64_t) ea[3] << 16)
70             | ((uint64_t) ea[4] << 8)
71             | ea[5]);
72 }
73 static inline void eth_addr_from_uint64(uint64_t x, uint8_t ea[ETH_ADDR_LEN])
74 {
75     ea[0] = x >> 40;
76     ea[1] = x >> 32;
77     ea[2] = x >> 24;
78     ea[3] = x >> 16;
79     ea[4] = x >> 8;
80     ea[5] = x;
81 }
82 static inline void eth_addr_mark_random(uint8_t ea[ETH_ADDR_LEN])
83 {
84     ea[0] &= ~1;                /* Unicast. */
85     ea[0] |= 2;                 /* Private. */
86 }
87 static inline void eth_addr_random(uint8_t ea[ETH_ADDR_LEN])
88 {
89     random_bytes(ea, ETH_ADDR_LEN);
90     eth_addr_mark_random(ea);
91 }
92 static inline void eth_addr_nicira_random(uint8_t ea[ETH_ADDR_LEN])
93 {
94     eth_addr_random(ea);
95
96     /* Set the OUI to the Nicira one. */
97     ea[0] = 0x00;
98     ea[1] = 0x23;
99     ea[2] = 0x20;
100
101     /* Set the top bit to indicate random Nicira address. */
102     ea[3] |= 0x80;
103 }
104 /* Returns true if 'ea' is a reserved multicast address, that a bridge must
105  * never forward, false otherwise. */
106 static inline bool eth_addr_is_reserved(const uint8_t ea[ETH_ADDR_LEN])
107 {
108     return (ea[0] == 0x01
109             && ea[1] == 0x80
110             && ea[2] == 0xc2
111             && ea[3] == 0x00
112             && ea[4] == 0x00
113             && (ea[5] & 0xf0) == 0x00);
114 }
115
116 bool eth_addr_from_string(const char *, uint8_t ea[ETH_ADDR_LEN]);
117
118 void compose_benign_packet(struct ofpbuf *, const char *tag,
119                            uint16_t snap_type,
120                            const uint8_t eth_src[ETH_ADDR_LEN]);
121
122 /* Example:
123  *
124  * uint8_t mac[ETH_ADDR_LEN];
125  *    [...]
126  * printf("The Ethernet address is "ETH_ADDR_FMT"\n", ETH_ADDR_ARGS(mac));
127  *
128  */
129 #define ETH_ADDR_FMT                                                    \
130     "%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8
131 #define ETH_ADDR_ARGS(ea)                                   \
132     (ea)[0], (ea)[1], (ea)[2], (ea)[3], (ea)[4], (ea)[5]
133
134 /* Example:
135  *
136  * char *string = "1 00:11:22:33:44:55 2";
137  * uint8_t mac[ETH_ADDR_LEN];
138  * int a, b;
139  *
140  * if (sscanf(string, "%d"ETH_ADDR_SCAN_FMT"%d",
141  *     &a, ETH_ADDR_SCAN_ARGS(mac), &b) == 1 + ETH_ADDR_SCAN_COUNT + 1) {
142  *     ...
143  * }
144  */
145 #define ETH_ADDR_SCAN_FMT "%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8
146 #define ETH_ADDR_SCAN_ARGS(ea) \
147         &(ea)[0], &(ea)[1], &(ea)[2], &(ea)[3], &(ea)[4], &(ea)[5]
148 #define ETH_ADDR_SCAN_COUNT 6
149
150 #define ETH_TYPE_IP            0x0800
151 #define ETH_TYPE_ARP           0x0806
152 #define ETH_TYPE_VLAN          0x8100
153
154 #define ETH_HEADER_LEN 14
155 #define ETH_PAYLOAD_MIN 46
156 #define ETH_PAYLOAD_MAX 1500
157 #define ETH_TOTAL_MIN (ETH_HEADER_LEN + ETH_PAYLOAD_MIN)
158 #define ETH_TOTAL_MAX (ETH_HEADER_LEN + ETH_PAYLOAD_MAX)
159 #define ETH_VLAN_TOTAL_MAX (ETH_HEADER_LEN + VLAN_HEADER_LEN + ETH_PAYLOAD_MAX)
160 struct eth_header {
161     uint8_t eth_dst[ETH_ADDR_LEN];
162     uint8_t eth_src[ETH_ADDR_LEN];
163     uint16_t eth_type;
164 } __attribute__((packed));
165 BUILD_ASSERT_DECL(ETH_HEADER_LEN == sizeof(struct eth_header));
166
167 #define LLC_DSAP_SNAP 0xaa
168 #define LLC_SSAP_SNAP 0xaa
169 #define LLC_CNTL_SNAP 3
170
171 #define LLC_HEADER_LEN 3
172 struct llc_header {
173     uint8_t llc_dsap;
174     uint8_t llc_ssap;
175     uint8_t llc_cntl;
176 } __attribute__((packed));
177 BUILD_ASSERT_DECL(LLC_HEADER_LEN == sizeof(struct llc_header));
178
179 #define SNAP_ORG_ETHERNET "\0\0" /* The compiler adds a null byte, so
180                                     sizeof(SNAP_ORG_ETHERNET) == 3. */
181 #define SNAP_HEADER_LEN 5
182 struct snap_header {
183     uint8_t snap_org[3];
184     uint16_t snap_type;
185 } __attribute__((packed));
186 BUILD_ASSERT_DECL(SNAP_HEADER_LEN == sizeof(struct snap_header));
187
188 #define LLC_SNAP_HEADER_LEN (LLC_HEADER_LEN + SNAP_HEADER_LEN)
189 struct llc_snap_header {
190     struct llc_header llc;
191     struct snap_header snap;
192 } __attribute__((packed));
193 BUILD_ASSERT_DECL(LLC_SNAP_HEADER_LEN == sizeof(struct llc_snap_header));
194
195 #define VLAN_VID_MASK 0x0fff
196 #define VLAN_VID_SHIFT 0
197
198 #define VLAN_PCP_MASK 0xe000
199 #define VLAN_PCP_SHIFT 13
200
201 #define VLAN_HEADER_LEN 4
202 struct vlan_header {
203     uint16_t vlan_tci;          /* Lowest 12 bits are VLAN ID. */
204     uint16_t vlan_next_type;
205 };
206 BUILD_ASSERT_DECL(VLAN_HEADER_LEN == sizeof(struct vlan_header));
207
208 #define VLAN_ETH_HEADER_LEN (ETH_HEADER_LEN + VLAN_HEADER_LEN)
209 struct vlan_eth_header {
210     uint8_t veth_dst[ETH_ADDR_LEN];
211     uint8_t veth_src[ETH_ADDR_LEN];
212     uint16_t veth_type;         /* Always htons(ETH_TYPE_VLAN). */
213     uint16_t veth_tci;          /* Lowest 12 bits are VLAN ID. */
214     uint16_t veth_next_type;
215 } __attribute__((packed));
216 BUILD_ASSERT_DECL(VLAN_ETH_HEADER_LEN == sizeof(struct vlan_eth_header));
217
218 /* The "(void) (ip)[0]" below has no effect on the value, since it's the first
219  * argument of a comma expression, but it makes sure that 'ip' is a pointer.
220  * This is useful since a common mistake is to pass an integer instead of a
221  * pointer to IP_ARGS. */
222 #define IP_FMT "%"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8
223 #define IP_ARGS(ip)                             \
224         ((void) (ip)[0], ((uint8_t *) ip)[0]),  \
225         ((uint8_t *) ip)[1],                    \
226         ((uint8_t *) ip)[2],                    \
227         ((uint8_t *) ip)[3]
228
229 #define IP_VER(ip_ihl_ver) ((ip_ihl_ver) >> 4)
230 #define IP_IHL(ip_ihl_ver) ((ip_ihl_ver) & 15)
231 #define IP_IHL_VER(ihl, ver) (((ver) << 4) | (ihl))
232
233 /* TOS fields. */
234 #define IP_ECN_MASK 0x03
235 #define IP_DSCP_MASK 0xfc
236
237 #define IP_TYPE_ICMP 1
238 #define IP_TYPE_TCP 6
239 #define IP_TYPE_UDP 17
240
241 #define IP_VERSION 4
242
243 #define IP_DONT_FRAGMENT  0x4000 /* Don't fragment. */
244 #define IP_MORE_FRAGMENTS 0x2000 /* More fragments. */
245 #define IP_FRAG_OFF_MASK  0x1fff /* Fragment offset. */
246 #define IP_IS_FRAGMENT(ip_frag_off) \
247         ((ip_frag_off) & htons(IP_MORE_FRAGMENTS | IP_FRAG_OFF_MASK))
248
249 #define IP_HEADER_LEN 20
250 struct ip_header {
251     uint8_t ip_ihl_ver;
252     uint8_t ip_tos;
253     uint16_t ip_tot_len;
254     uint16_t ip_id;
255     uint16_t ip_frag_off;
256     uint8_t ip_ttl;
257     uint8_t ip_proto;
258     uint16_t ip_csum;
259     uint32_t ip_src;
260     uint32_t ip_dst;
261 };
262 BUILD_ASSERT_DECL(IP_HEADER_LEN == sizeof(struct ip_header));
263
264 #define ICMP_HEADER_LEN 4
265 struct icmp_header {
266     uint8_t icmp_type;
267     uint8_t icmp_code;
268     uint16_t icmp_csum;
269 };
270 BUILD_ASSERT_DECL(ICMP_HEADER_LEN == sizeof(struct icmp_header));
271
272 #define UDP_HEADER_LEN 8
273 struct udp_header {
274     uint16_t udp_src;
275     uint16_t udp_dst;
276     uint16_t udp_len;
277     uint16_t udp_csum;
278 };
279 BUILD_ASSERT_DECL(UDP_HEADER_LEN == sizeof(struct udp_header));
280
281 #define TCP_FIN 0x01
282 #define TCP_SYN 0x02
283 #define TCP_RST 0x04
284 #define TCP_PSH 0x08
285 #define TCP_ACK 0x10
286 #define TCP_URG 0x20
287
288 #define TCP_FLAGS(tcp_ctl) (htons(tcp_ctl) & 0x003f)
289 #define TCP_OFFSET(tcp_ctl) (htons(tcp_ctl) >> 12)
290
291 #define TCP_HEADER_LEN 20
292 struct tcp_header {
293     uint16_t tcp_src;
294     uint16_t tcp_dst;
295     uint32_t tcp_seq;
296     uint32_t tcp_ack;
297     uint16_t tcp_ctl;
298     uint16_t tcp_winsz;
299     uint16_t tcp_csum;
300     uint16_t tcp_urg;
301 };
302 BUILD_ASSERT_DECL(TCP_HEADER_LEN == sizeof(struct tcp_header));
303
304 #define ARP_HRD_ETHERNET 1
305 #define ARP_PRO_IP 0x0800
306 #define ARP_OP_REQUEST 1
307 #define ARP_OP_REPLY 2
308
309 #define ARP_ETH_HEADER_LEN 28
310 struct arp_eth_header {
311     /* Generic members. */
312     uint16_t ar_hrd;           /* Hardware type. */
313     uint16_t ar_pro;           /* Protocol type. */
314     uint8_t ar_hln;            /* Hardware address length. */
315     uint8_t ar_pln;            /* Protocol address length. */
316     uint16_t ar_op;            /* Opcode. */
317
318     /* Ethernet+IPv4 specific members. */
319     uint8_t ar_sha[ETH_ADDR_LEN]; /* Sender hardware address. */
320     uint32_t ar_spa;           /* Sender protocol address. */
321     uint8_t ar_tha[ETH_ADDR_LEN]; /* Target hardware address. */
322     uint32_t ar_tpa;           /* Target protocol address. */
323 } __attribute__((packed));
324 BUILD_ASSERT_DECL(ARP_ETH_HEADER_LEN == sizeof(struct arp_eth_header));
325
326 #endif /* packets.h */