packets: Add eth_addr_compare_3way function.
[openvswitch] / 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 <sys/types.h>
22 #include <netinet/in.h>
23 #include <stdint.h>
24 #include <string.h>
25 #include "compiler.h"
26 #include "openvswitch/types.h"
27 #include "random.h"
28 #include "util.h"
29
30 struct ofpbuf;
31 struct ds;
32
33 bool dpid_from_string(const char *s, uint64_t *dpidp);
34
35 #define ETH_ADDR_LEN           6
36
37 static const uint8_t eth_addr_broadcast[ETH_ADDR_LEN] OVS_UNUSED
38     = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
39
40 static const uint8_t eth_addr_stp[ETH_ADDR_LEN] OVS_UNUSED
41     = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x01 };
42
43 static inline bool eth_addr_is_broadcast(const uint8_t ea[6])
44 {
45     return (ea[0] & ea[1] & ea[2] & ea[3] & ea[4] & ea[5]) == 0xff;
46 }
47
48 static inline bool eth_addr_is_multicast(const uint8_t ea[6])
49 {
50     return ea[0] & 1;
51 }
52 static inline bool eth_addr_is_local(const uint8_t ea[6])
53 {
54     /* Local if it is either a locally administered address or a Nicira random
55      * address. */
56     return !!(ea[0] & 2)
57        || (ea[0] == 0x00 && ea[1] == 0x23 && ea[2] == 0x20 && !!(ea[3] & 0x80));
58 }
59 static inline bool eth_addr_is_zero(const uint8_t ea[6])
60 {
61     return !(ea[0] | ea[1] | ea[2] | ea[3] | ea[4] | ea[5]);
62 }
63 static inline int eth_addr_compare_3way(const uint8_t a[ETH_ADDR_LEN],
64                                         const uint8_t b[ETH_ADDR_LEN])
65 {
66     return memcmp(a, b, ETH_ADDR_LEN);
67 }
68 static inline bool eth_addr_equals(const uint8_t a[ETH_ADDR_LEN],
69                                    const uint8_t b[ETH_ADDR_LEN])
70 {
71     return !eth_addr_compare_3way(a, b);
72 }
73 static inline uint64_t eth_addr_to_uint64(const uint8_t ea[ETH_ADDR_LEN])
74 {
75     return (((uint64_t) ea[0] << 40)
76             | ((uint64_t) ea[1] << 32)
77             | ((uint64_t) ea[2] << 24)
78             | ((uint64_t) ea[3] << 16)
79             | ((uint64_t) ea[4] << 8)
80             | ea[5]);
81 }
82 static inline void eth_addr_from_uint64(uint64_t x, uint8_t ea[ETH_ADDR_LEN])
83 {
84     ea[0] = x >> 40;
85     ea[1] = x >> 32;
86     ea[2] = x >> 24;
87     ea[3] = x >> 16;
88     ea[4] = x >> 8;
89     ea[5] = x;
90 }
91 static inline void eth_addr_mark_random(uint8_t ea[ETH_ADDR_LEN])
92 {
93     ea[0] &= ~1;                /* Unicast. */
94     ea[0] |= 2;                 /* Private. */
95 }
96 static inline void eth_addr_random(uint8_t ea[ETH_ADDR_LEN])
97 {
98     random_bytes(ea, ETH_ADDR_LEN);
99     eth_addr_mark_random(ea);
100 }
101 static inline void eth_addr_nicira_random(uint8_t ea[ETH_ADDR_LEN])
102 {
103     eth_addr_random(ea);
104
105     /* Set the OUI to the Nicira one. */
106     ea[0] = 0x00;
107     ea[1] = 0x23;
108     ea[2] = 0x20;
109
110     /* Set the top bit to indicate random Nicira address. */
111     ea[3] |= 0x80;
112 }
113 /* Returns true if 'ea' is a reserved multicast address, that a bridge must
114  * never forward, false otherwise. */
115 static inline bool eth_addr_is_reserved(const uint8_t ea[ETH_ADDR_LEN])
116 {
117     return (ea[0] == 0x01
118             && ea[1] == 0x80
119             && ea[2] == 0xc2
120             && ea[3] == 0x00
121             && ea[4] == 0x00
122             && (ea[5] & 0xf0) == 0x00);
123 }
124
125 bool eth_addr_from_string(const char *, uint8_t ea[ETH_ADDR_LEN]);
126
127 void compose_benign_packet(struct ofpbuf *, const char *tag,
128                            uint16_t snap_type,
129                            const uint8_t eth_src[ETH_ADDR_LEN]);
130
131 /* Example:
132  *
133  * uint8_t mac[ETH_ADDR_LEN];
134  *    [...]
135  * printf("The Ethernet address is "ETH_ADDR_FMT"\n", ETH_ADDR_ARGS(mac));
136  *
137  */
138 #define ETH_ADDR_FMT                                                    \
139     "%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8
140 #define ETH_ADDR_ARGS(ea)                                   \
141     (ea)[0], (ea)[1], (ea)[2], (ea)[3], (ea)[4], (ea)[5]
142
143 /* Example:
144  *
145  * char *string = "1 00:11:22:33:44:55 2";
146  * uint8_t mac[ETH_ADDR_LEN];
147  * int a, b;
148  *
149  * if (sscanf(string, "%d"ETH_ADDR_SCAN_FMT"%d",
150  *     &a, ETH_ADDR_SCAN_ARGS(mac), &b) == 1 + ETH_ADDR_SCAN_COUNT + 1) {
151  *     ...
152  * }
153  */
154 #define ETH_ADDR_SCAN_FMT "%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8
155 #define ETH_ADDR_SCAN_ARGS(ea) \
156         &(ea)[0], &(ea)[1], &(ea)[2], &(ea)[3], &(ea)[4], &(ea)[5]
157 #define ETH_ADDR_SCAN_COUNT 6
158
159 #define ETH_TYPE_IP            0x0800
160 #define ETH_TYPE_ARP           0x0806
161 #define ETH_TYPE_VLAN          0x8100
162 #define ETH_TYPE_IPV6          0x86dd
163 #define ETH_TYPE_CFM           0x8902
164
165 /* Minimum value for an Ethernet type.  Values below this are IEEE 802.2 frame
166  * lengths. */
167 #define ETH_TYPE_MIN           0x600
168
169 #define ETH_HEADER_LEN 14
170 #define ETH_PAYLOAD_MIN 46
171 #define ETH_PAYLOAD_MAX 1500
172 #define ETH_TOTAL_MIN (ETH_HEADER_LEN + ETH_PAYLOAD_MIN)
173 #define ETH_TOTAL_MAX (ETH_HEADER_LEN + ETH_PAYLOAD_MAX)
174 #define ETH_VLAN_TOTAL_MAX (ETH_HEADER_LEN + VLAN_HEADER_LEN + ETH_PAYLOAD_MAX)
175 struct eth_header {
176     uint8_t eth_dst[ETH_ADDR_LEN];
177     uint8_t eth_src[ETH_ADDR_LEN];
178     ovs_be16 eth_type;
179 } __attribute__((packed));
180 BUILD_ASSERT_DECL(ETH_HEADER_LEN == sizeof(struct eth_header));
181
182 #define LLC_DSAP_SNAP 0xaa
183 #define LLC_SSAP_SNAP 0xaa
184 #define LLC_CNTL_SNAP 3
185
186 #define LLC_HEADER_LEN 3
187 struct llc_header {
188     uint8_t llc_dsap;
189     uint8_t llc_ssap;
190     uint8_t llc_cntl;
191 } __attribute__((packed));
192 BUILD_ASSERT_DECL(LLC_HEADER_LEN == sizeof(struct llc_header));
193
194 #define SNAP_ORG_ETHERNET "\0\0" /* The compiler adds a null byte, so
195                                     sizeof(SNAP_ORG_ETHERNET) == 3. */
196 #define SNAP_HEADER_LEN 5
197 struct snap_header {
198     uint8_t snap_org[3];
199     ovs_be16 snap_type;
200 } __attribute__((packed));
201 BUILD_ASSERT_DECL(SNAP_HEADER_LEN == sizeof(struct snap_header));
202
203 #define LLC_SNAP_HEADER_LEN (LLC_HEADER_LEN + SNAP_HEADER_LEN)
204 struct llc_snap_header {
205     struct llc_header llc;
206     struct snap_header snap;
207 } __attribute__((packed));
208 BUILD_ASSERT_DECL(LLC_SNAP_HEADER_LEN == sizeof(struct llc_snap_header));
209
210 #define VLAN_VID_MASK 0x0fff
211 #define VLAN_VID_SHIFT 0
212
213 #define VLAN_PCP_MASK 0xe000
214 #define VLAN_PCP_SHIFT 13
215
216 #define VLAN_CFI 0x1000
217
218 /* Given the vlan_tci field from an 802.1Q header, in network byte order,
219  * returns the VLAN ID in host byte order. */
220 static inline uint16_t
221 vlan_tci_to_vid(ovs_be16 vlan_tci)
222 {
223     return (ntohs(vlan_tci) & VLAN_VID_MASK) >> VLAN_VID_SHIFT;
224 }
225
226 /* Given the vlan_tci field from an 802.1Q header, in network byte order,
227  * returns the priority code point (PCP) in host byte order. */
228 static inline int
229 vlan_tci_to_pcp(ovs_be16 vlan_tci)
230 {
231     return (ntohs(vlan_tci) & VLAN_PCP_MASK) >> VLAN_PCP_SHIFT;
232 }
233
234 #define VLAN_HEADER_LEN 4
235 struct vlan_header {
236     ovs_be16 vlan_tci;          /* Lowest 12 bits are VLAN ID. */
237     ovs_be16 vlan_next_type;
238 };
239 BUILD_ASSERT_DECL(VLAN_HEADER_LEN == sizeof(struct vlan_header));
240
241 #define VLAN_ETH_HEADER_LEN (ETH_HEADER_LEN + VLAN_HEADER_LEN)
242 struct vlan_eth_header {
243     uint8_t veth_dst[ETH_ADDR_LEN];
244     uint8_t veth_src[ETH_ADDR_LEN];
245     ovs_be16 veth_type;         /* Always htons(ETH_TYPE_VLAN). */
246     ovs_be16 veth_tci;          /* Lowest 12 bits are VLAN ID. */
247     ovs_be16 veth_next_type;
248 } __attribute__((packed));
249 BUILD_ASSERT_DECL(VLAN_ETH_HEADER_LEN == sizeof(struct vlan_eth_header));
250
251 /* A 'ccm' represents a Continuity Check Message from the 802.1ag specification.
252  * Continuity Check Messages are broadcast periodically so that hosts can
253  * determine who they have connectivity to. */
254 #define CCM_LEN 74
255 #define CCM_MAID_LEN 48
256 struct ccm {
257     uint8_t  mdlevel_version; /* MD Level and Version */
258     uint8_t  opcode;
259     uint8_t  flags;
260     uint8_t  tlv_offset;
261     ovs_be32 seq;
262     ovs_be16 mpid;
263     uint8_t  maid[CCM_MAID_LEN];
264     uint8_t  zero[16]; /* Defined by ITU-T Y.1731 should be zero */
265 } __attribute__((packed));
266 BUILD_ASSERT_DECL(CCM_LEN == sizeof(struct ccm));
267
268 /* The "(void) (ip)[0]" below has no effect on the value, since it's the first
269  * argument of a comma expression, but it makes sure that 'ip' is a pointer.
270  * This is useful since a common mistake is to pass an integer instead of a
271  * pointer to IP_ARGS. */
272 #define IP_FMT "%"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8
273 #define IP_ARGS(ip)                             \
274         ((void) (ip)[0], ((uint8_t *) ip)[0]),  \
275         ((uint8_t *) ip)[1],                    \
276         ((uint8_t *) ip)[2],                    \
277         ((uint8_t *) ip)[3]
278
279 /* Returns true if 'netmask' is a CIDR netmask, that is, if it consists of N
280  * high-order 1-bits and 32-N low-order 0-bits. */
281 static inline bool
282 ip_is_cidr(ovs_be32 netmask)
283 {
284     uint32_t x = ~ntohl(netmask);
285     return !(x & (x + 1));
286 }
287
288 #define IP_VER(ip_ihl_ver) ((ip_ihl_ver) >> 4)
289 #define IP_IHL(ip_ihl_ver) ((ip_ihl_ver) & 15)
290 #define IP_IHL_VER(ihl, ver) (((ver) << 4) | (ihl))
291
292 /* TOS fields. */
293 #define IP_ECN_MASK 0x03
294 #define IP_DSCP_MASK 0xfc
295
296 #define IP_VERSION 4
297
298 #define IP_DONT_FRAGMENT  0x4000 /* Don't fragment. */
299 #define IP_MORE_FRAGMENTS 0x2000 /* More fragments. */
300 #define IP_FRAG_OFF_MASK  0x1fff /* Fragment offset. */
301 #define IP_IS_FRAGMENT(ip_frag_off) \
302         ((ip_frag_off) & htons(IP_MORE_FRAGMENTS | IP_FRAG_OFF_MASK))
303
304 #define IP_HEADER_LEN 20
305 struct ip_header {
306     uint8_t ip_ihl_ver;
307     uint8_t ip_tos;
308     ovs_be16 ip_tot_len;
309     ovs_be16 ip_id;
310     ovs_be16 ip_frag_off;
311     uint8_t ip_ttl;
312     uint8_t ip_proto;
313     ovs_be16 ip_csum;
314     ovs_be32 ip_src;
315     ovs_be32 ip_dst;
316 };
317 BUILD_ASSERT_DECL(IP_HEADER_LEN == sizeof(struct ip_header));
318
319 #define ICMP_HEADER_LEN 4
320 struct icmp_header {
321     uint8_t icmp_type;
322     uint8_t icmp_code;
323     ovs_be16 icmp_csum;
324 };
325 BUILD_ASSERT_DECL(ICMP_HEADER_LEN == sizeof(struct icmp_header));
326
327 #define UDP_HEADER_LEN 8
328 struct udp_header {
329     ovs_be16 udp_src;
330     ovs_be16 udp_dst;
331     ovs_be16 udp_len;
332     ovs_be16 udp_csum;
333 };
334 BUILD_ASSERT_DECL(UDP_HEADER_LEN == sizeof(struct udp_header));
335
336 #define TCP_FIN 0x01
337 #define TCP_SYN 0x02
338 #define TCP_RST 0x04
339 #define TCP_PSH 0x08
340 #define TCP_ACK 0x10
341 #define TCP_URG 0x20
342
343 #define TCP_FLAGS(tcp_ctl) (htons(tcp_ctl) & 0x003f)
344 #define TCP_OFFSET(tcp_ctl) (htons(tcp_ctl) >> 12)
345
346 #define TCP_HEADER_LEN 20
347 struct tcp_header {
348     ovs_be16 tcp_src;
349     ovs_be16 tcp_dst;
350     ovs_be32 tcp_seq;
351     ovs_be32 tcp_ack;
352     ovs_be16 tcp_ctl;
353     ovs_be16 tcp_winsz;
354     ovs_be16 tcp_csum;
355     ovs_be16 tcp_urg;
356 };
357 BUILD_ASSERT_DECL(TCP_HEADER_LEN == sizeof(struct tcp_header));
358
359 #define ARP_HRD_ETHERNET 1
360 #define ARP_PRO_IP 0x0800
361 #define ARP_OP_REQUEST 1
362 #define ARP_OP_REPLY 2
363
364 #define ARP_ETH_HEADER_LEN 28
365 struct arp_eth_header {
366     /* Generic members. */
367     ovs_be16 ar_hrd;           /* Hardware type. */
368     ovs_be16 ar_pro;           /* Protocol type. */
369     uint8_t ar_hln;            /* Hardware address length. */
370     uint8_t ar_pln;            /* Protocol address length. */
371     ovs_be16 ar_op;            /* Opcode. */
372
373     /* Ethernet+IPv4 specific members. */
374     uint8_t ar_sha[ETH_ADDR_LEN]; /* Sender hardware address. */
375     ovs_be32 ar_spa;           /* Sender protocol address. */
376     uint8_t ar_tha[ETH_ADDR_LEN]; /* Target hardware address. */
377     ovs_be32 ar_tpa;           /* Target protocol address. */
378 } __attribute__((packed));
379 BUILD_ASSERT_DECL(ARP_ETH_HEADER_LEN == sizeof(struct arp_eth_header));
380
381 extern const struct in6_addr in6addr_exact;
382 #define IN6ADDR_EXACT_INIT { { { 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, \
383                                  0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff } } }
384
385 static inline bool ipv6_addr_equals(const struct in6_addr *a,
386                                     const struct in6_addr *b)
387 {
388 #ifdef IN6_ARE_ADDR_EQUAL
389     return IN6_ARE_ADDR_EQUAL(a, b);
390 #else
391     return !memcmp(a, b, sizeof(*a));
392 #endif
393 }
394
395 static inline bool ipv6_mask_is_any(const struct in6_addr *mask) {
396     return ipv6_addr_equals(mask, &in6addr_any);
397 }
398
399 static inline bool ipv6_mask_is_exact(const struct in6_addr *mask) {
400     return ipv6_addr_equals(mask, &in6addr_exact);
401 }
402
403 void format_ipv6_addr(char *addr_str, const struct in6_addr *addr);
404 void print_ipv6_addr(struct ds *string, const struct in6_addr *addr);
405 struct in6_addr ipv6_addr_bitand(const struct in6_addr *src,
406                                  const struct in6_addr *mask);
407 struct in6_addr ipv6_create_mask(int mask);
408 int ipv6_count_cidr_bits(const struct in6_addr *netmask);
409 bool ipv6_is_cidr(const struct in6_addr *netmask);
410
411 #endif /* packets.h */