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