7e2d4e991851c4f25826ca5416d15c28f6f5fe3f
[sliver-openvswitch.git] / lib / packets.h
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
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 struct flow;
33
34 bool dpid_from_string(const char *s, uint64_t *dpidp);
35
36 #define ETH_ADDR_LEN           6
37
38 static const uint8_t eth_addr_broadcast[ETH_ADDR_LEN] OVS_UNUSED
39     = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
40
41 static const uint8_t eth_addr_stp[ETH_ADDR_LEN] OVS_UNUSED
42     = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x00 };
43
44 static const uint8_t eth_addr_lacp[ETH_ADDR_LEN] OVS_UNUSED
45     = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x02 };
46
47 static inline bool eth_addr_is_broadcast(const uint8_t ea[6])
48 {
49     return (ea[0] & ea[1] & ea[2] & ea[3] & ea[4] & ea[5]) == 0xff;
50 }
51
52 static inline bool eth_addr_is_multicast(const uint8_t ea[6])
53 {
54     return ea[0] & 1;
55 }
56 static inline bool eth_addr_is_local(const uint8_t ea[6])
57 {
58     /* Local if it is either a locally administered address or a Nicira random
59      * address. */
60     return ea[0] & 2
61        || (ea[0] == 0x00 && ea[1] == 0x23 && ea[2] == 0x20 && ea[3] & 0x80);
62 }
63 static inline bool eth_addr_is_zero(const uint8_t ea[6])
64 {
65     return !(ea[0] | ea[1] | ea[2] | ea[3] | ea[4] | ea[5]);
66 }
67
68 static inline int eth_mask_is_exact(const uint8_t ea[ETH_ADDR_LEN])
69 {
70     return (ea[0] & ea[1] & ea[2] & ea[3] & ea[4] & ea[5]) == 0xff;
71 }
72
73 static inline int eth_addr_compare_3way(const uint8_t a[ETH_ADDR_LEN],
74                                         const uint8_t b[ETH_ADDR_LEN])
75 {
76     return memcmp(a, b, ETH_ADDR_LEN);
77 }
78 static inline bool eth_addr_equals(const uint8_t a[ETH_ADDR_LEN],
79                                    const uint8_t b[ETH_ADDR_LEN])
80 {
81     return !eth_addr_compare_3way(a, b);
82 }
83 static inline bool eth_addr_equal_except(const uint8_t a[ETH_ADDR_LEN],
84                                     const uint8_t b[ETH_ADDR_LEN],
85                                     const uint8_t mask[ETH_ADDR_LEN])
86 {
87     return !(((a[0] ^ b[0]) & mask[0])
88              || ((a[1] ^ b[1]) & mask[1])
89              || ((a[2] ^ b[2]) & mask[2])
90              || ((a[3] ^ b[3]) & mask[3])
91              || ((a[4] ^ b[4]) & mask[4])
92              || ((a[5] ^ b[5]) & mask[5]));
93 }
94 static inline uint64_t eth_addr_to_uint64(const uint8_t ea[ETH_ADDR_LEN])
95 {
96     return (((uint64_t) ea[0] << 40)
97             | ((uint64_t) ea[1] << 32)
98             | ((uint64_t) ea[2] << 24)
99             | ((uint64_t) ea[3] << 16)
100             | ((uint64_t) ea[4] << 8)
101             | ea[5]);
102 }
103 static inline void eth_addr_from_uint64(uint64_t x, uint8_t ea[ETH_ADDR_LEN])
104 {
105     ea[0] = x >> 40;
106     ea[1] = x >> 32;
107     ea[2] = x >> 24;
108     ea[3] = x >> 16;
109     ea[4] = x >> 8;
110     ea[5] = x;
111 }
112 static inline void eth_addr_mark_random(uint8_t ea[ETH_ADDR_LEN])
113 {
114     ea[0] &= ~1;                /* Unicast. */
115     ea[0] |= 2;                 /* Private. */
116 }
117 static inline void eth_addr_random(uint8_t ea[ETH_ADDR_LEN])
118 {
119     random_bytes(ea, ETH_ADDR_LEN);
120     eth_addr_mark_random(ea);
121 }
122 static inline void eth_addr_nicira_random(uint8_t ea[ETH_ADDR_LEN])
123 {
124     eth_addr_random(ea);
125
126     /* Set the OUI to the Nicira one. */
127     ea[0] = 0x00;
128     ea[1] = 0x23;
129     ea[2] = 0x20;
130
131     /* Set the top bit to indicate random Nicira address. */
132     ea[3] |= 0x80;
133 }
134
135 bool eth_addr_is_reserved(const uint8_t ea[ETH_ADDR_LEN]);
136 bool eth_addr_from_string(const char *, uint8_t ea[ETH_ADDR_LEN]);
137
138 void compose_rarp(struct ofpbuf *, const uint8_t eth_src[ETH_ADDR_LEN]);
139
140 void eth_push_vlan(struct ofpbuf *, ovs_be16 tci);
141 void eth_pop_vlan(struct ofpbuf *);
142
143 const char *eth_from_hex(const char *hex, struct ofpbuf **packetp);
144 void eth_format_masked(const uint8_t eth[ETH_ADDR_LEN],
145                        const uint8_t mask[ETH_ADDR_LEN], struct ds *s);
146 void eth_addr_bitand(const uint8_t src[ETH_ADDR_LEN],
147                      const uint8_t mask[ETH_ADDR_LEN],
148                      uint8_t dst[ETH_ADDR_LEN]);
149
150 /* Example:
151  *
152  * uint8_t mac[ETH_ADDR_LEN];
153  *    [...]
154  * printf("The Ethernet address is "ETH_ADDR_FMT"\n", ETH_ADDR_ARGS(mac));
155  *
156  */
157 #define ETH_ADDR_FMT                                                    \
158     "%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8
159 #define ETH_ADDR_ARGS(ea)                                   \
160     (ea)[0], (ea)[1], (ea)[2], (ea)[3], (ea)[4], (ea)[5]
161
162 /* Example:
163  *
164  * char *string = "1 00:11:22:33:44:55 2";
165  * uint8_t mac[ETH_ADDR_LEN];
166  * int a, b;
167  *
168  * if (sscanf(string, "%d"ETH_ADDR_SCAN_FMT"%d",
169  *     &a, ETH_ADDR_SCAN_ARGS(mac), &b) == 1 + ETH_ADDR_SCAN_COUNT + 1) {
170  *     ...
171  * }
172  */
173 #define ETH_ADDR_SCAN_FMT "%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8
174 #define ETH_ADDR_SCAN_ARGS(ea) \
175         &(ea)[0], &(ea)[1], &(ea)[2], &(ea)[3], &(ea)[4], &(ea)[5]
176 #define ETH_ADDR_SCAN_COUNT 6
177
178 #define ETH_TYPE_IP            0x0800
179 #define ETH_TYPE_ARP           0x0806
180 #define ETH_TYPE_VLAN_8021Q    0x8100
181 #define ETH_TYPE_VLAN          ETH_TYPE_VLAN_8021Q
182 #define ETH_TYPE_VLAN_8021AD   0x88a8
183 #define ETH_TYPE_IPV6          0x86dd
184 #define ETH_TYPE_LACP          0x8809
185 #define ETH_TYPE_RARP          0x8035
186 #define ETH_TYPE_MPLS          0x8847
187 #define ETH_TYPE_MPLS_MCAST    0x8848
188
189 /* Minimum value for an Ethernet type.  Values below this are IEEE 802.2 frame
190  * lengths. */
191 #define ETH_TYPE_MIN           0x600
192
193 #define ETH_HEADER_LEN 14
194 #define ETH_PAYLOAD_MIN 46
195 #define ETH_PAYLOAD_MAX 1500
196 #define ETH_TOTAL_MIN (ETH_HEADER_LEN + ETH_PAYLOAD_MIN)
197 #define ETH_TOTAL_MAX (ETH_HEADER_LEN + ETH_PAYLOAD_MAX)
198 #define ETH_VLAN_TOTAL_MAX (ETH_HEADER_LEN + VLAN_HEADER_LEN + ETH_PAYLOAD_MAX)
199 struct eth_header {
200     uint8_t eth_dst[ETH_ADDR_LEN];
201     uint8_t eth_src[ETH_ADDR_LEN];
202     ovs_be16 eth_type;
203 } __attribute__((packed));
204 BUILD_ASSERT_DECL(ETH_HEADER_LEN == sizeof(struct eth_header));
205
206 #define LLC_DSAP_SNAP 0xaa
207 #define LLC_SSAP_SNAP 0xaa
208 #define LLC_CNTL_SNAP 3
209
210 #define LLC_HEADER_LEN 3
211 struct llc_header {
212     uint8_t llc_dsap;
213     uint8_t llc_ssap;
214     uint8_t llc_cntl;
215 } __attribute__((packed));
216 BUILD_ASSERT_DECL(LLC_HEADER_LEN == sizeof(struct llc_header));
217
218 #define SNAP_ORG_ETHERNET "\0\0" /* The compiler adds a null byte, so
219                                     sizeof(SNAP_ORG_ETHERNET) == 3. */
220 #define SNAP_HEADER_LEN 5
221 struct snap_header {
222     uint8_t snap_org[3];
223     ovs_be16 snap_type;
224 } __attribute__((packed));
225 BUILD_ASSERT_DECL(SNAP_HEADER_LEN == sizeof(struct snap_header));
226
227 #define LLC_SNAP_HEADER_LEN (LLC_HEADER_LEN + SNAP_HEADER_LEN)
228 struct llc_snap_header {
229     struct llc_header llc;
230     struct snap_header snap;
231 } __attribute__((packed));
232 BUILD_ASSERT_DECL(LLC_SNAP_HEADER_LEN == sizeof(struct llc_snap_header));
233
234 #define VLAN_VID_MASK 0x0fff
235 #define VLAN_VID_SHIFT 0
236
237 #define VLAN_PCP_MASK 0xe000
238 #define VLAN_PCP_SHIFT 13
239
240 #define VLAN_CFI 0x1000
241
242 /* Given the vlan_tci field from an 802.1Q header, in network byte order,
243  * returns the VLAN ID in host byte order. */
244 static inline uint16_t
245 vlan_tci_to_vid(ovs_be16 vlan_tci)
246 {
247     return (ntohs(vlan_tci) & VLAN_VID_MASK) >> VLAN_VID_SHIFT;
248 }
249
250 /* Given the vlan_tci field from an 802.1Q header, in network byte order,
251  * returns the priority code point (PCP) in host byte order. */
252 static inline int
253 vlan_tci_to_pcp(ovs_be16 vlan_tci)
254 {
255     return (ntohs(vlan_tci) & VLAN_PCP_MASK) >> VLAN_PCP_SHIFT;
256 }
257
258 #define VLAN_HEADER_LEN 4
259 struct vlan_header {
260     ovs_be16 vlan_tci;          /* Lowest 12 bits are VLAN ID. */
261     ovs_be16 vlan_next_type;
262 };
263 BUILD_ASSERT_DECL(VLAN_HEADER_LEN == sizeof(struct vlan_header));
264
265 #define VLAN_ETH_HEADER_LEN (ETH_HEADER_LEN + VLAN_HEADER_LEN)
266 struct vlan_eth_header {
267     uint8_t veth_dst[ETH_ADDR_LEN];
268     uint8_t veth_src[ETH_ADDR_LEN];
269     ovs_be16 veth_type;         /* Always htons(ETH_TYPE_VLAN). */
270     ovs_be16 veth_tci;          /* Lowest 12 bits are VLAN ID. */
271     ovs_be16 veth_next_type;
272 } __attribute__((packed));
273 BUILD_ASSERT_DECL(VLAN_ETH_HEADER_LEN == sizeof(struct vlan_eth_header));
274
275 #define IP_FMT "%"PRIu32".%"PRIu32".%"PRIu32".%"PRIu32
276 #define IP_ARGS(ip)                             \
277     ntohl(ip) >> 24,                            \
278     (ntohl(ip) >> 16) & 0xff,                   \
279     (ntohl(ip) >> 8) & 0xff,                    \
280     ntohl(ip) & 0xff
281
282 /* Example:
283  *
284  * char *string = "1 33.44.55.66 2";
285  * ovs_be32 ip;
286  * int a, b;
287  *
288  * if (sscanf(string, "%d"IP_SCAN_FMT"%d",
289  *     &a, IP_SCAN_ARGS(&ip), &b) == 1 + IP_SCAN_COUNT + 1) {
290  *     ...
291  * }
292  */
293 #define IP_SCAN_FMT "%"SCNu8".%"SCNu8".%"SCNu8".%"SCNu8
294 #define IP_SCAN_ARGS(ip)                                    \
295         ((void) (ovs_be32) *(ip), &((uint8_t *) ip)[0]),    \
296         &((uint8_t *) ip)[1],                               \
297         &((uint8_t *) ip)[2],                               \
298         &((uint8_t *) ip)[3]
299 #define IP_SCAN_COUNT 4
300
301 /* Returns true if 'netmask' is a CIDR netmask, that is, if it consists of N
302  * high-order 1-bits and 32-N low-order 0-bits. */
303 static inline bool
304 ip_is_cidr(ovs_be32 netmask)
305 {
306     uint32_t x = ~ntohl(netmask);
307     return !(x & (x + 1));
308 }
309 static inline bool
310 ip_is_multicast(ovs_be32 ip)
311 {
312     return (ip & htonl(0xf0000000)) == htonl(0xe0000000);
313 }
314 int ip_count_cidr_bits(ovs_be32 netmask);
315 void ip_format_masked(ovs_be32 ip, ovs_be32 mask, struct ds *);
316
317 #define IP_VER(ip_ihl_ver) ((ip_ihl_ver) >> 4)
318 #define IP_IHL(ip_ihl_ver) ((ip_ihl_ver) & 15)
319 #define IP_IHL_VER(ihl, ver) (((ver) << 4) | (ihl))
320
321 #ifndef IPPROTO_SCTP
322 #define IPPROTO_SCTP 132
323 #endif
324
325 /* TOS fields. */
326 #define IP_ECN_NOT_ECT 0x0
327 #define IP_ECN_ECT_1 0x01
328 #define IP_ECN_ECT_0 0x02
329 #define IP_ECN_CE 0x03
330 #define IP_ECN_MASK 0x03
331 #define IP_DSCP_MASK 0xfc
332
333 #define IP_VERSION 4
334
335 #define IP_DONT_FRAGMENT  0x4000 /* Don't fragment. */
336 #define IP_MORE_FRAGMENTS 0x2000 /* More fragments. */
337 #define IP_FRAG_OFF_MASK  0x1fff /* Fragment offset. */
338 #define IP_IS_FRAGMENT(ip_frag_off) \
339         ((ip_frag_off) & htons(IP_MORE_FRAGMENTS | IP_FRAG_OFF_MASK))
340
341 #define IP_HEADER_LEN 20
342 struct ip_header {
343     uint8_t ip_ihl_ver;
344     uint8_t ip_tos;
345     ovs_be16 ip_tot_len;
346     ovs_be16 ip_id;
347     ovs_be16 ip_frag_off;
348     uint8_t ip_ttl;
349     uint8_t ip_proto;
350     ovs_be16 ip_csum;
351     ovs_be32 ip_src;
352     ovs_be32 ip_dst;
353 };
354 BUILD_ASSERT_DECL(IP_HEADER_LEN == sizeof(struct ip_header));
355
356 #define ICMP_HEADER_LEN 8
357 struct icmp_header {
358     uint8_t icmp_type;
359     uint8_t icmp_code;
360     ovs_be16 icmp_csum;
361     union {
362         struct {
363             ovs_be16 id;
364             ovs_be16 seq;
365         } echo;
366         struct {
367             ovs_be16 empty;
368             ovs_be16 mtu;
369         } frag;
370         ovs_be32 gateway;
371     } icmp_fields;
372     uint8_t icmp_data[0];
373 };
374 BUILD_ASSERT_DECL(ICMP_HEADER_LEN == sizeof(struct icmp_header));
375
376 #define UDP_HEADER_LEN 8
377 struct udp_header {
378     ovs_be16 udp_src;
379     ovs_be16 udp_dst;
380     ovs_be16 udp_len;
381     ovs_be16 udp_csum;
382 };
383 BUILD_ASSERT_DECL(UDP_HEADER_LEN == sizeof(struct udp_header));
384
385 #define TCP_FIN 0x01
386 #define TCP_SYN 0x02
387 #define TCP_RST 0x04
388 #define TCP_PSH 0x08
389 #define TCP_ACK 0x10
390 #define TCP_URG 0x20
391
392 #define TCP_CTL(flags, offset) (htons((flags) | ((offset) << 12)))
393 #define TCP_FLAGS(tcp_ctl) (ntohs(tcp_ctl) & 0x003f)
394 #define TCP_OFFSET(tcp_ctl) (ntohs(tcp_ctl) >> 12)
395
396 #define TCP_HEADER_LEN 20
397 struct tcp_header {
398     ovs_be16 tcp_src;
399     ovs_be16 tcp_dst;
400     ovs_be32 tcp_seq;
401     ovs_be32 tcp_ack;
402     ovs_be16 tcp_ctl;
403     ovs_be16 tcp_winsz;
404     ovs_be16 tcp_csum;
405     ovs_be16 tcp_urg;
406 };
407 BUILD_ASSERT_DECL(TCP_HEADER_LEN == sizeof(struct tcp_header));
408
409 #define ARP_HRD_ETHERNET 1
410 #define ARP_PRO_IP 0x0800
411 #define ARP_OP_REQUEST 1
412 #define ARP_OP_REPLY 2
413 #define ARP_OP_RARP 3
414
415 #define ARP_ETH_HEADER_LEN 28
416 struct arp_eth_header {
417     /* Generic members. */
418     ovs_be16 ar_hrd;           /* Hardware type. */
419     ovs_be16 ar_pro;           /* Protocol type. */
420     uint8_t ar_hln;            /* Hardware address length. */
421     uint8_t ar_pln;            /* Protocol address length. */
422     ovs_be16 ar_op;            /* Opcode. */
423
424     /* Ethernet+IPv4 specific members. */
425     uint8_t ar_sha[ETH_ADDR_LEN]; /* Sender hardware address. */
426     ovs_be32 ar_spa;           /* Sender protocol address. */
427     uint8_t ar_tha[ETH_ADDR_LEN]; /* Target hardware address. */
428     ovs_be32 ar_tpa;           /* Target protocol address. */
429 } __attribute__((packed));
430 BUILD_ASSERT_DECL(ARP_ETH_HEADER_LEN == sizeof(struct arp_eth_header));
431
432 /* The IPv6 flow label is in the lower 20 bits of the first 32-bit word. */
433 #define IPV6_LABEL_MASK 0x000fffff
434
435 /* Example:
436  *
437  * char *string = "1 ::1 2";
438  * char ipv6_s[IPV6_SCAN_LEN + 1];
439  * struct in6_addr ipv6;
440  *
441  * if (sscanf(string, "%d"IPV6_SCAN_FMT"%d", &a, ipv6_s, &b) == 3
442  *     && inet_pton(AF_INET6, ipv6_s, &ipv6) == 1) {
443  *     ...
444  * }
445  */
446 #define IPV6_SCAN_FMT "%46[0123456789abcdefABCDEF:.]"
447 #define IPV6_SCAN_LEN 46
448
449 extern const struct in6_addr in6addr_exact;
450 #define IN6ADDR_EXACT_INIT { { { 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, \
451                                  0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff } } }
452
453 static inline bool ipv6_addr_equals(const struct in6_addr *a,
454                                     const struct in6_addr *b)
455 {
456 #ifdef IN6_ARE_ADDR_EQUAL
457     return IN6_ARE_ADDR_EQUAL(a, b);
458 #else
459     return !memcmp(a, b, sizeof(*a));
460 #endif
461 }
462
463 static inline bool ipv6_mask_is_any(const struct in6_addr *mask) {
464     return ipv6_addr_equals(mask, &in6addr_any);
465 }
466
467 static inline bool ipv6_mask_is_exact(const struct in6_addr *mask) {
468     return ipv6_addr_equals(mask, &in6addr_exact);
469 }
470
471 void format_ipv6_addr(char *addr_str, const struct in6_addr *addr);
472 void print_ipv6_addr(struct ds *string, const struct in6_addr *addr);
473 void print_ipv6_masked(struct ds *string, const struct in6_addr *addr,
474                        const struct in6_addr *mask);
475 struct in6_addr ipv6_addr_bitand(const struct in6_addr *src,
476                                  const struct in6_addr *mask);
477 struct in6_addr ipv6_create_mask(int mask);
478 int ipv6_count_cidr_bits(const struct in6_addr *netmask);
479 bool ipv6_is_cidr(const struct in6_addr *netmask);
480
481 void *eth_compose(struct ofpbuf *, const uint8_t eth_dst[ETH_ADDR_LEN],
482                   const uint8_t eth_src[ETH_ADDR_LEN], uint16_t eth_type,
483                   size_t size);
484 void *snap_compose(struct ofpbuf *, const uint8_t eth_dst[ETH_ADDR_LEN],
485                    const uint8_t eth_src[ETH_ADDR_LEN],
486                    unsigned int oui, uint16_t snap_type, size_t size);
487 void packet_set_ipv4(struct ofpbuf *, ovs_be32 src, ovs_be32 dst, uint8_t tos,
488                      uint8_t ttl);
489 void packet_set_ipv6(struct ofpbuf *, uint8_t proto, const ovs_be32 src[4],
490                      const ovs_be32 dst[4], uint8_t tc,
491                      ovs_be32 fl, uint8_t hlmit);
492 void packet_set_tcp_port(struct ofpbuf *, ovs_be16 src, ovs_be16 dst);
493 void packet_set_udp_port(struct ofpbuf *, ovs_be16 src, ovs_be16 dst);
494
495 uint8_t packet_get_tcp_flags(const struct ofpbuf *, const struct flow *);
496 void packet_format_tcp_flags(struct ds *, uint8_t);
497
498 #endif /* packets.h */