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