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