2 * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <sys/types.h>
22 #include <netinet/in.h>
26 #include "openvswitch/types.h"
33 bool dpid_from_string(const char *s, uint64_t *dpidp);
35 #define ETH_ADDR_LEN 6
37 static const uint8_t eth_addr_broadcast[ETH_ADDR_LEN] OVS_UNUSED
38 = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
40 static const uint8_t eth_addr_stp[ETH_ADDR_LEN] OVS_UNUSED
41 = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x00 };
43 static const uint8_t eth_addr_lacp[ETH_ADDR_LEN] OVS_UNUSED
44 = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x02 };
46 static inline bool eth_addr_is_broadcast(const uint8_t ea[6])
48 return (ea[0] & ea[1] & ea[2] & ea[3] & ea[4] & ea[5]) == 0xff;
51 static inline bool eth_addr_is_multicast(const uint8_t ea[6])
55 static inline bool eth_addr_is_local(const uint8_t ea[6])
57 /* Local if it is either a locally administered address or a Nicira random
60 || (ea[0] == 0x00 && ea[1] == 0x23 && ea[2] == 0x20 && ea[3] & 0x80);
62 static inline bool eth_addr_is_zero(const uint8_t ea[6])
64 return !(ea[0] | ea[1] | ea[2] | ea[3] | ea[4] | ea[5]);
66 static inline int eth_addr_compare_3way(const uint8_t a[ETH_ADDR_LEN],
67 const uint8_t b[ETH_ADDR_LEN])
69 return memcmp(a, b, ETH_ADDR_LEN);
71 static inline bool eth_addr_equals(const uint8_t a[ETH_ADDR_LEN],
72 const uint8_t b[ETH_ADDR_LEN])
74 return !eth_addr_compare_3way(a, b);
76 static inline uint64_t eth_addr_to_uint64(const uint8_t ea[ETH_ADDR_LEN])
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)
85 static inline void eth_addr_from_uint64(uint64_t x, uint8_t ea[ETH_ADDR_LEN])
94 static inline void eth_addr_mark_random(uint8_t ea[ETH_ADDR_LEN])
96 ea[0] &= ~1; /* Unicast. */
97 ea[0] |= 2; /* Private. */
99 static inline void eth_addr_random(uint8_t ea[ETH_ADDR_LEN])
101 random_bytes(ea, ETH_ADDR_LEN);
102 eth_addr_mark_random(ea);
104 static inline void eth_addr_nicira_random(uint8_t ea[ETH_ADDR_LEN])
108 /* Set the OUI to the Nicira one. */
113 /* Set the top bit to indicate random Nicira address. */
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])
120 return (ea[0] == 0x01
125 && (ea[5] & 0xf0) == 0x00);
128 bool eth_addr_from_string(const char *, uint8_t ea[ETH_ADDR_LEN]);
130 void compose_benign_packet(struct ofpbuf *, const char *tag,
132 const uint8_t eth_src[ETH_ADDR_LEN]);
134 void eth_push_vlan(struct ofpbuf *, ovs_be16 tci);
138 * uint8_t mac[ETH_ADDR_LEN];
140 * printf("The Ethernet address is "ETH_ADDR_FMT"\n", ETH_ADDR_ARGS(mac));
143 #define ETH_ADDR_FMT \
144 "%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8
145 #define ETH_ADDR_ARGS(ea) \
146 (ea)[0], (ea)[1], (ea)[2], (ea)[3], (ea)[4], (ea)[5]
150 * char *string = "1 00:11:22:33:44:55 2";
151 * uint8_t mac[ETH_ADDR_LEN];
154 * if (sscanf(string, "%d"ETH_ADDR_SCAN_FMT"%d",
155 * &a, ETH_ADDR_SCAN_ARGS(mac), &b) == 1 + ETH_ADDR_SCAN_COUNT + 1) {
159 #define ETH_ADDR_SCAN_FMT "%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8
160 #define ETH_ADDR_SCAN_ARGS(ea) \
161 &(ea)[0], &(ea)[1], &(ea)[2], &(ea)[3], &(ea)[4], &(ea)[5]
162 #define ETH_ADDR_SCAN_COUNT 6
164 #define ETH_TYPE_IP 0x0800
165 #define ETH_TYPE_ARP 0x0806
166 #define ETH_TYPE_VLAN 0x8100
167 #define ETH_TYPE_IPV6 0x86dd
168 #define ETH_TYPE_LACP 0x8809
170 /* Minimum value for an Ethernet type. Values below this are IEEE 802.2 frame
172 #define ETH_TYPE_MIN 0x600
174 #define ETH_HEADER_LEN 14
175 #define ETH_PAYLOAD_MIN 46
176 #define ETH_PAYLOAD_MAX 1500
177 #define ETH_TOTAL_MIN (ETH_HEADER_LEN + ETH_PAYLOAD_MIN)
178 #define ETH_TOTAL_MAX (ETH_HEADER_LEN + ETH_PAYLOAD_MAX)
179 #define ETH_VLAN_TOTAL_MAX (ETH_HEADER_LEN + VLAN_HEADER_LEN + ETH_PAYLOAD_MAX)
181 uint8_t eth_dst[ETH_ADDR_LEN];
182 uint8_t eth_src[ETH_ADDR_LEN];
184 } __attribute__((packed));
185 BUILD_ASSERT_DECL(ETH_HEADER_LEN == sizeof(struct eth_header));
187 #define LLC_DSAP_SNAP 0xaa
188 #define LLC_SSAP_SNAP 0xaa
189 #define LLC_CNTL_SNAP 3
191 #define LLC_HEADER_LEN 3
196 } __attribute__((packed));
197 BUILD_ASSERT_DECL(LLC_HEADER_LEN == sizeof(struct llc_header));
199 #define SNAP_ORG_ETHERNET "\0\0" /* The compiler adds a null byte, so
200 sizeof(SNAP_ORG_ETHERNET) == 3. */
201 #define SNAP_HEADER_LEN 5
205 } __attribute__((packed));
206 BUILD_ASSERT_DECL(SNAP_HEADER_LEN == sizeof(struct snap_header));
208 #define LLC_SNAP_HEADER_LEN (LLC_HEADER_LEN + SNAP_HEADER_LEN)
209 struct llc_snap_header {
210 struct llc_header llc;
211 struct snap_header snap;
212 } __attribute__((packed));
213 BUILD_ASSERT_DECL(LLC_SNAP_HEADER_LEN == sizeof(struct llc_snap_header));
215 #define VLAN_VID_MASK 0x0fff
216 #define VLAN_VID_SHIFT 0
218 #define VLAN_PCP_MASK 0xe000
219 #define VLAN_PCP_SHIFT 13
221 #define VLAN_CFI 0x1000
223 /* Given the vlan_tci field from an 802.1Q header, in network byte order,
224 * returns the VLAN ID in host byte order. */
225 static inline uint16_t
226 vlan_tci_to_vid(ovs_be16 vlan_tci)
228 return (ntohs(vlan_tci) & VLAN_VID_MASK) >> VLAN_VID_SHIFT;
231 /* Given the vlan_tci field from an 802.1Q header, in network byte order,
232 * returns the priority code point (PCP) in host byte order. */
234 vlan_tci_to_pcp(ovs_be16 vlan_tci)
236 return (ntohs(vlan_tci) & VLAN_PCP_MASK) >> VLAN_PCP_SHIFT;
239 #define VLAN_HEADER_LEN 4
241 ovs_be16 vlan_tci; /* Lowest 12 bits are VLAN ID. */
242 ovs_be16 vlan_next_type;
244 BUILD_ASSERT_DECL(VLAN_HEADER_LEN == sizeof(struct vlan_header));
246 #define VLAN_ETH_HEADER_LEN (ETH_HEADER_LEN + VLAN_HEADER_LEN)
247 struct vlan_eth_header {
248 uint8_t veth_dst[ETH_ADDR_LEN];
249 uint8_t veth_src[ETH_ADDR_LEN];
250 ovs_be16 veth_type; /* Always htons(ETH_TYPE_VLAN). */
251 ovs_be16 veth_tci; /* Lowest 12 bits are VLAN ID. */
252 ovs_be16 veth_next_type;
253 } __attribute__((packed));
254 BUILD_ASSERT_DECL(VLAN_ETH_HEADER_LEN == sizeof(struct vlan_eth_header));
256 /* The "(void) (ip)[0]" below has no effect on the value, since it's the first
257 * argument of a comma expression, but it makes sure that 'ip' is a pointer.
258 * This is useful since a common mistake is to pass an integer instead of a
259 * pointer to IP_ARGS. */
260 #define IP_FMT "%"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8
261 #define IP_ARGS(ip) \
262 ((void) (ip)[0], ((uint8_t *) ip)[0]), \
263 ((uint8_t *) ip)[1], \
264 ((uint8_t *) ip)[2], \
269 * char *string = "1 33.44.55.66 2";
273 * if (sscanf(string, "%d"IP_SCAN_FMT"%d",
274 * &a, IP_SCAN_ARGS(&ip), &b) == 1 + IP_SCAN_COUNT + 1) {
278 #define IP_SCAN_FMT "%"SCNu8".%"SCNu8".%"SCNu8".%"SCNu8
279 #define IP_SCAN_ARGS(ip) \
280 ((void) (ovs_be32) *(ip), &((uint8_t *) ip)[0]), \
281 &((uint8_t *) ip)[1], \
282 &((uint8_t *) ip)[2], \
284 #define IP_SCAN_COUNT 4
286 /* Returns true if 'netmask' is a CIDR netmask, that is, if it consists of N
287 * high-order 1-bits and 32-N low-order 0-bits. */
289 ip_is_cidr(ovs_be32 netmask)
291 uint32_t x = ~ntohl(netmask);
292 return !(x & (x + 1));
295 ip_is_multicast(ovs_be32 ip)
297 return (ip & htonl(0xf0000000)) == htonl(0xe0000000);
299 int ip_count_cidr_bits(ovs_be32 netmask);
300 void ip_format_masked(ovs_be32 ip, ovs_be32 mask, struct ds *);
302 #define IP_VER(ip_ihl_ver) ((ip_ihl_ver) >> 4)
303 #define IP_IHL(ip_ihl_ver) ((ip_ihl_ver) & 15)
304 #define IP_IHL_VER(ihl, ver) (((ver) << 4) | (ihl))
307 #define IP_ECN_MASK 0x03
308 #define IP_DSCP_MASK 0xfc
312 #define IP_DONT_FRAGMENT 0x4000 /* Don't fragment. */
313 #define IP_MORE_FRAGMENTS 0x2000 /* More fragments. */
314 #define IP_FRAG_OFF_MASK 0x1fff /* Fragment offset. */
315 #define IP_IS_FRAGMENT(ip_frag_off) \
316 ((ip_frag_off) & htons(IP_MORE_FRAGMENTS | IP_FRAG_OFF_MASK))
318 #define IP_HEADER_LEN 20
324 ovs_be16 ip_frag_off;
331 BUILD_ASSERT_DECL(IP_HEADER_LEN == sizeof(struct ip_header));
333 #define ICMP_HEADER_LEN 4
339 BUILD_ASSERT_DECL(ICMP_HEADER_LEN == sizeof(struct icmp_header));
341 #define UDP_HEADER_LEN 8
348 BUILD_ASSERT_DECL(UDP_HEADER_LEN == sizeof(struct udp_header));
357 #define TCP_FLAGS(tcp_ctl) (ntohs(tcp_ctl) & 0x003f)
358 #define TCP_OFFSET(tcp_ctl) (ntohs(tcp_ctl) >> 12)
360 #define TCP_HEADER_LEN 20
371 BUILD_ASSERT_DECL(TCP_HEADER_LEN == sizeof(struct tcp_header));
373 #define ARP_HRD_ETHERNET 1
374 #define ARP_PRO_IP 0x0800
375 #define ARP_OP_REQUEST 1
376 #define ARP_OP_REPLY 2
378 #define ARP_ETH_HEADER_LEN 28
379 struct arp_eth_header {
380 /* Generic members. */
381 ovs_be16 ar_hrd; /* Hardware type. */
382 ovs_be16 ar_pro; /* Protocol type. */
383 uint8_t ar_hln; /* Hardware address length. */
384 uint8_t ar_pln; /* Protocol address length. */
385 ovs_be16 ar_op; /* Opcode. */
387 /* Ethernet+IPv4 specific members. */
388 uint8_t ar_sha[ETH_ADDR_LEN]; /* Sender hardware address. */
389 ovs_be32 ar_spa; /* Sender protocol address. */
390 uint8_t ar_tha[ETH_ADDR_LEN]; /* Target hardware address. */
391 ovs_be32 ar_tpa; /* Target protocol address. */
392 } __attribute__((packed));
393 BUILD_ASSERT_DECL(ARP_ETH_HEADER_LEN == sizeof(struct arp_eth_header));
395 /* The IPv6 flow label is in the lower 20 bits of the first 32-bit word. */
396 #define IPV6_LABEL_MASK 0x000fffff
400 * char *string = "1 ::1 2";
401 * char ipv6_s[IPV6_SCAN_LEN + 1];
402 * struct in6_addr ipv6;
404 * if (sscanf(string, "%d"IPV6_SCAN_FMT"%d", &a, ipv6_s, &b) == 3
405 * && inet_pton(AF_INET6, ipv6_s, &ipv6) == 1) {
409 #define IPV6_SCAN_FMT "%46[0123456789abcdefABCDEF:.]"
410 #define IPV6_SCAN_LEN 46
412 extern const struct in6_addr in6addr_exact;
413 #define IN6ADDR_EXACT_INIT { { { 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, \
414 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff } } }
416 static inline bool ipv6_addr_equals(const struct in6_addr *a,
417 const struct in6_addr *b)
419 #ifdef IN6_ARE_ADDR_EQUAL
420 return IN6_ARE_ADDR_EQUAL(a, b);
422 return !memcmp(a, b, sizeof(*a));
426 static inline bool ipv6_mask_is_any(const struct in6_addr *mask) {
427 return ipv6_addr_equals(mask, &in6addr_any);
430 static inline bool ipv6_mask_is_exact(const struct in6_addr *mask) {
431 return ipv6_addr_equals(mask, &in6addr_exact);
434 void format_ipv6_addr(char *addr_str, const struct in6_addr *addr);
435 void print_ipv6_addr(struct ds *string, const struct in6_addr *addr);
436 void print_ipv6_masked(struct ds *string, const struct in6_addr *addr,
437 const struct in6_addr *mask);
438 struct in6_addr ipv6_addr_bitand(const struct in6_addr *src,
439 const struct in6_addr *mask);
440 struct in6_addr ipv6_create_mask(int mask);
441 int ipv6_count_cidr_bits(const struct in6_addr *netmask);
442 bool ipv6_is_cidr(const struct in6_addr *netmask);
444 void *eth_compose(struct ofpbuf *, const uint8_t eth_dst[ETH_ADDR_LEN],
445 const uint8_t eth_src[ETH_ADDR_LEN], uint16_t eth_type,
447 void *snap_compose(struct ofpbuf *, const uint8_t eth_dst[ETH_ADDR_LEN],
448 const uint8_t eth_src[ETH_ADDR_LEN],
449 unsigned int oui, uint16_t snap_type, size_t size);
451 #endif /* packets.h */