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