Rename UNUSED macro to OVS_UNUSED to avoid naming conflict.
[cascardo/ovs.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 #define ETH_ADDR_LEN           6
30
31 static const uint8_t eth_addr_broadcast[ETH_ADDR_LEN] OVS_UNUSED
32     = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
33
34 static inline bool eth_addr_is_broadcast(const uint8_t ea[6])
35 {
36     return (ea[0] & ea[1] & ea[2] & ea[3] & ea[4] & ea[5]) == 0xff;
37 }
38
39 /* Returns true if 'ea' is an Ethernet address used for virtual interfaces
40  * under XenServer.  Generally the actual Ethernet address is FE:FF:FF:FF:FF:FF
41  * but it can be FE:FE:FE:FE:FE:FE in some cases. */
42 static inline bool eth_addr_is_vif(const uint8_t ea[6])
43 {
44     return ea[0] == 0xfe && (ea[1] & ea[2] & ea[3] & ea[4] & ea[5]) >= 0xfe;
45 }
46
47 static inline bool eth_addr_is_multicast(const uint8_t ea[6])
48 {
49     return ea[0] & 1;
50 }
51 static inline bool eth_addr_is_local(const uint8_t ea[6]) 
52 {
53     return ea[0] & 2;
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 /* 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 void compose_benign_packet(struct ofpbuf *, const char *tag,
105                            uint16_t snap_type,
106                            const uint8_t eth_src[ETH_ADDR_LEN]);
107
108 /* Example:
109  *
110  * uint8_t mac[ETH_ADDR_LEN];
111  *    [...]
112  * printf("The Ethernet address is "ETH_ADDR_FMT"\n", ETH_ADDR_ARGS(mac));
113  *
114  */
115 #define ETH_ADDR_FMT                                                    \
116     "%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8
117 #define ETH_ADDR_ARGS(ea)                                   \
118     (ea)[0], (ea)[1], (ea)[2], (ea)[3], (ea)[4], (ea)[5]
119
120 /* Example:
121  *
122  * char *string = "1 00:11:22:33:44:55 2";
123  * uint8_t mac[ETH_ADDR_LEN];
124  * int a, b;
125  *
126  * if (sscanf(string, "%d"ETH_ADDR_SCAN_FMT"%d",
127  *     &a, ETH_ADDR_SCAN_ARGS(mac), &b) == 1 + ETH_ADDR_SCAN_COUNT + 1) {
128  *     ...
129  * }
130  */
131 #define ETH_ADDR_SCAN_FMT "%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8
132 #define ETH_ADDR_SCAN_ARGS(ea) \
133         &(ea)[0], &(ea)[1], &(ea)[2], &(ea)[3], &(ea)[4], &(ea)[5]
134 #define ETH_ADDR_SCAN_COUNT 6
135
136 #define ETH_TYPE_IP            0x0800
137 #define ETH_TYPE_ARP           0x0806
138 #define ETH_TYPE_VLAN          0x8100
139
140 #define ETH_HEADER_LEN 14
141 #define ETH_PAYLOAD_MIN 46
142 #define ETH_PAYLOAD_MAX 1500
143 #define ETH_TOTAL_MIN (ETH_HEADER_LEN + ETH_PAYLOAD_MIN)
144 #define ETH_TOTAL_MAX (ETH_HEADER_LEN + ETH_PAYLOAD_MAX)
145 #define ETH_VLAN_TOTAL_MAX (ETH_HEADER_LEN + VLAN_HEADER_LEN + ETH_PAYLOAD_MAX)
146 struct eth_header {
147     uint8_t eth_dst[ETH_ADDR_LEN];
148     uint8_t eth_src[ETH_ADDR_LEN];
149     uint16_t eth_type;
150 } __attribute__((packed));
151 BUILD_ASSERT_DECL(ETH_HEADER_LEN == sizeof(struct eth_header));
152
153 #define LLC_DSAP_SNAP 0xaa
154 #define LLC_SSAP_SNAP 0xaa
155 #define LLC_CNTL_SNAP 3
156
157 #define LLC_HEADER_LEN 3
158 struct llc_header {
159     uint8_t llc_dsap;
160     uint8_t llc_ssap;
161     uint8_t llc_cntl;
162 } __attribute__((packed));
163 BUILD_ASSERT_DECL(LLC_HEADER_LEN == sizeof(struct llc_header));
164
165 #define SNAP_ORG_ETHERNET "\0\0" /* The compiler adds a null byte, so
166                                     sizeof(SNAP_ORG_ETHERNET) == 3. */
167 #define SNAP_HEADER_LEN 5
168 struct snap_header {
169     uint8_t snap_org[3];
170     uint16_t snap_type;
171 } __attribute__((packed));
172 BUILD_ASSERT_DECL(SNAP_HEADER_LEN == sizeof(struct snap_header));
173
174 #define LLC_SNAP_HEADER_LEN (LLC_HEADER_LEN + SNAP_HEADER_LEN)
175 struct llc_snap_header {
176     struct llc_header llc;
177     struct snap_header snap;
178 } __attribute__((packed));
179 BUILD_ASSERT_DECL(LLC_SNAP_HEADER_LEN == sizeof(struct llc_snap_header));
180
181 #define VLAN_VID_MASK 0x0fff
182 #define VLAN_PCP_MASK 0xe000
183
184 #define VLAN_HEADER_LEN 4
185 struct vlan_header {
186     uint16_t vlan_tci;          /* Lowest 12 bits are VLAN ID. */
187     uint16_t vlan_next_type;
188 };
189 BUILD_ASSERT_DECL(VLAN_HEADER_LEN == sizeof(struct vlan_header));
190
191 #define VLAN_ETH_HEADER_LEN (ETH_HEADER_LEN + VLAN_HEADER_LEN)
192 struct vlan_eth_header {
193     uint8_t veth_dst[ETH_ADDR_LEN];
194     uint8_t veth_src[ETH_ADDR_LEN];
195     uint16_t veth_type;         /* Always htons(ETH_TYPE_VLAN). */
196     uint16_t veth_tci;          /* Lowest 12 bits are VLAN ID. */
197     uint16_t veth_next_type;
198 } __attribute__((packed));
199 BUILD_ASSERT_DECL(VLAN_ETH_HEADER_LEN == sizeof(struct vlan_eth_header));
200
201 /* The "(void) (ip)[0]" below has no effect on the value, since it's the first
202  * argument of a comma expression, but it makes sure that 'ip' is a pointer.
203  * This is useful since a common mistake is to pass an integer instead of a
204  * pointer to IP_ARGS. */
205 #define IP_FMT "%"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8
206 #define IP_ARGS(ip)                             \
207         ((void) (ip)[0], ((uint8_t *) ip)[0]),  \
208         ((uint8_t *) ip)[1],                    \
209         ((uint8_t *) ip)[2],                    \
210         ((uint8_t *) ip)[3]
211
212 #define IP_VER(ip_ihl_ver) ((ip_ihl_ver) >> 4)
213 #define IP_IHL(ip_ihl_ver) ((ip_ihl_ver) & 15)
214 #define IP_IHL_VER(ihl, ver) (((ver) << 4) | (ihl))
215
216 #define IP_TYPE_ICMP 1
217 #define IP_TYPE_TCP 6
218 #define IP_TYPE_UDP 17
219
220 #define IP_VERSION 4
221
222 #define IP_DONT_FRAGMENT  0x4000 /* Don't fragment. */
223 #define IP_MORE_FRAGMENTS 0x2000 /* More fragments. */
224 #define IP_FRAG_OFF_MASK  0x1fff /* Fragment offset. */
225 #define IP_IS_FRAGMENT(ip_frag_off) \
226         ((ip_frag_off) & htons(IP_MORE_FRAGMENTS | IP_FRAG_OFF_MASK))
227
228 #define IP_HEADER_LEN 20
229 struct ip_header {
230     uint8_t ip_ihl_ver;
231     uint8_t ip_tos;
232     uint16_t ip_tot_len;
233     uint16_t ip_id;
234     uint16_t ip_frag_off;
235     uint8_t ip_ttl;
236     uint8_t ip_proto;
237     uint16_t ip_csum;
238     uint32_t ip_src;
239     uint32_t ip_dst;
240 };
241 BUILD_ASSERT_DECL(IP_HEADER_LEN == sizeof(struct ip_header));
242
243 #define ICMP_HEADER_LEN 4
244 struct icmp_header {
245     uint8_t icmp_type;
246     uint8_t icmp_code;
247     uint16_t icmp_csum;
248 };
249 BUILD_ASSERT_DECL(ICMP_HEADER_LEN == sizeof(struct icmp_header));
250
251 #define UDP_HEADER_LEN 8
252 struct udp_header {
253     uint16_t udp_src;
254     uint16_t udp_dst;
255     uint16_t udp_len;
256     uint16_t udp_csum;
257 };
258 BUILD_ASSERT_DECL(UDP_HEADER_LEN == sizeof(struct udp_header));
259
260 #define TCP_FIN 0x01
261 #define TCP_SYN 0x02
262 #define TCP_RST 0x04
263 #define TCP_PSH 0x08
264 #define TCP_ACK 0x10
265 #define TCP_URG 0x20
266
267 #define TCP_FLAGS(tcp_ctl) (htons(tcp_ctl) & 0x003f)
268 #define TCP_OFFSET(tcp_ctl) (htons(tcp_ctl) >> 12)
269
270 #define TCP_HEADER_LEN 20
271 struct tcp_header {
272     uint16_t tcp_src;
273     uint16_t tcp_dst;
274     uint32_t tcp_seq;
275     uint32_t tcp_ack;
276     uint16_t tcp_ctl;
277     uint16_t tcp_winsz;
278     uint16_t tcp_csum;
279     uint16_t tcp_urg;
280 };
281 BUILD_ASSERT_DECL(TCP_HEADER_LEN == sizeof(struct tcp_header));
282
283 #define ARP_HRD_ETHERNET 1
284 #define ARP_PRO_IP 0x0800
285 #define ARP_OP_REQUEST 1
286 #define ARP_OP_REPLY 2
287
288 #define ARP_ETH_HEADER_LEN 28
289 struct arp_eth_header {
290     /* Generic members. */
291     uint16_t ar_hrd;           /* Hardware type. */
292     uint16_t ar_pro;           /* Protocol type. */
293     uint8_t ar_hln;            /* Hardware address length. */
294     uint8_t ar_pln;            /* Protocol address length. */
295     uint16_t ar_op;            /* Opcode. */
296
297     /* Ethernet+IPv4 specific members. */
298     uint8_t ar_sha[ETH_ADDR_LEN]; /* Sender hardware address. */
299     uint32_t ar_spa;           /* Sender protocol address. */
300     uint8_t ar_tha[ETH_ADDR_LEN]; /* Target hardware address. */
301     uint32_t ar_tpa;           /* Target protocol address. */
302 } __attribute__((packed));
303 BUILD_ASSERT_DECL(ARP_ETH_HEADER_LEN == sizeof(struct arp_eth_header));
304
305 #endif /* packets.h */