2 * Copyright (c) 2008, 2009, 2010 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, 0x01 };
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]);
136 * uint8_t mac[ETH_ADDR_LEN];
138 * printf("The Ethernet address is "ETH_ADDR_FMT"\n", ETH_ADDR_ARGS(mac));
141 #define ETH_ADDR_FMT \
142 "%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8
143 #define ETH_ADDR_ARGS(ea) \
144 (ea)[0], (ea)[1], (ea)[2], (ea)[3], (ea)[4], (ea)[5]
148 * char *string = "1 00:11:22:33:44:55 2";
149 * uint8_t mac[ETH_ADDR_LEN];
152 * if (sscanf(string, "%d"ETH_ADDR_SCAN_FMT"%d",
153 * &a, ETH_ADDR_SCAN_ARGS(mac), &b) == 1 + ETH_ADDR_SCAN_COUNT + 1) {
157 #define ETH_ADDR_SCAN_FMT "%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8
158 #define ETH_ADDR_SCAN_ARGS(ea) \
159 &(ea)[0], &(ea)[1], &(ea)[2], &(ea)[3], &(ea)[4], &(ea)[5]
160 #define ETH_ADDR_SCAN_COUNT 6
162 #define ETH_TYPE_IP 0x0800
163 #define ETH_TYPE_ARP 0x0806
164 #define ETH_TYPE_VLAN 0x8100
165 #define ETH_TYPE_IPV6 0x86dd
166 #define ETH_TYPE_CFM 0x8902
167 #define ETH_TYPE_LACP 0x8809
169 /* Minimum value for an Ethernet type. Values below this are IEEE 802.2 frame
171 #define ETH_TYPE_MIN 0x600
173 #define ETH_HEADER_LEN 14
174 #define ETH_PAYLOAD_MIN 46
175 #define ETH_PAYLOAD_MAX 1500
176 #define ETH_TOTAL_MIN (ETH_HEADER_LEN + ETH_PAYLOAD_MIN)
177 #define ETH_TOTAL_MAX (ETH_HEADER_LEN + ETH_PAYLOAD_MAX)
178 #define ETH_VLAN_TOTAL_MAX (ETH_HEADER_LEN + VLAN_HEADER_LEN + ETH_PAYLOAD_MAX)
180 uint8_t eth_dst[ETH_ADDR_LEN];
181 uint8_t eth_src[ETH_ADDR_LEN];
183 } __attribute__((packed));
184 BUILD_ASSERT_DECL(ETH_HEADER_LEN == sizeof(struct eth_header));
186 #define LLC_DSAP_SNAP 0xaa
187 #define LLC_SSAP_SNAP 0xaa
188 #define LLC_CNTL_SNAP 3
190 #define LLC_HEADER_LEN 3
195 } __attribute__((packed));
196 BUILD_ASSERT_DECL(LLC_HEADER_LEN == sizeof(struct llc_header));
198 #define SNAP_ORG_ETHERNET "\0\0" /* The compiler adds a null byte, so
199 sizeof(SNAP_ORG_ETHERNET) == 3. */
200 #define SNAP_HEADER_LEN 5
204 } __attribute__((packed));
205 BUILD_ASSERT_DECL(SNAP_HEADER_LEN == sizeof(struct snap_header));
207 #define LLC_SNAP_HEADER_LEN (LLC_HEADER_LEN + SNAP_HEADER_LEN)
208 struct llc_snap_header {
209 struct llc_header llc;
210 struct snap_header snap;
211 } __attribute__((packed));
212 BUILD_ASSERT_DECL(LLC_SNAP_HEADER_LEN == sizeof(struct llc_snap_header));
214 #define VLAN_VID_MASK 0x0fff
215 #define VLAN_VID_SHIFT 0
217 #define VLAN_PCP_MASK 0xe000
218 #define VLAN_PCP_SHIFT 13
220 #define VLAN_CFI 0x1000
222 /* Given the vlan_tci field from an 802.1Q header, in network byte order,
223 * returns the VLAN ID in host byte order. */
224 static inline uint16_t
225 vlan_tci_to_vid(ovs_be16 vlan_tci)
227 return (ntohs(vlan_tci) & VLAN_VID_MASK) >> VLAN_VID_SHIFT;
230 /* Given the vlan_tci field from an 802.1Q header, in network byte order,
231 * returns the priority code point (PCP) in host byte order. */
233 vlan_tci_to_pcp(ovs_be16 vlan_tci)
235 return (ntohs(vlan_tci) & VLAN_PCP_MASK) >> VLAN_PCP_SHIFT;
238 #define VLAN_HEADER_LEN 4
240 ovs_be16 vlan_tci; /* Lowest 12 bits are VLAN ID. */
241 ovs_be16 vlan_next_type;
243 BUILD_ASSERT_DECL(VLAN_HEADER_LEN == sizeof(struct vlan_header));
245 #define VLAN_ETH_HEADER_LEN (ETH_HEADER_LEN + VLAN_HEADER_LEN)
246 struct vlan_eth_header {
247 uint8_t veth_dst[ETH_ADDR_LEN];
248 uint8_t veth_src[ETH_ADDR_LEN];
249 ovs_be16 veth_type; /* Always htons(ETH_TYPE_VLAN). */
250 ovs_be16 veth_tci; /* Lowest 12 bits are VLAN ID. */
251 ovs_be16 veth_next_type;
252 } __attribute__((packed));
253 BUILD_ASSERT_DECL(VLAN_ETH_HEADER_LEN == sizeof(struct vlan_eth_header));
255 /* A 'ccm' represents a Continuity Check Message from the 802.1ag specification.
256 * Continuity Check Messages are broadcast periodically so that hosts can
257 * determine who they have connectivity to. */
259 #define CCM_MAID_LEN 48
261 uint8_t mdlevel_version; /* MD Level and Version */
267 uint8_t maid[CCM_MAID_LEN];
268 uint8_t zero[16]; /* Defined by ITU-T Y.1731 should be zero */
269 } __attribute__((packed));
270 BUILD_ASSERT_DECL(CCM_LEN == sizeof(struct ccm));
272 /* The "(void) (ip)[0]" below has no effect on the value, since it's the first
273 * argument of a comma expression, but it makes sure that 'ip' is a pointer.
274 * This is useful since a common mistake is to pass an integer instead of a
275 * pointer to IP_ARGS. */
276 #define IP_FMT "%"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8
277 #define IP_ARGS(ip) \
278 ((void) (ip)[0], ((uint8_t *) ip)[0]), \
279 ((uint8_t *) ip)[1], \
280 ((uint8_t *) ip)[2], \
283 /* Returns true if 'netmask' is a CIDR netmask, that is, if it consists of N
284 * high-order 1-bits and 32-N low-order 0-bits. */
286 ip_is_cidr(ovs_be32 netmask)
288 uint32_t x = ~ntohl(netmask);
289 return !(x & (x + 1));
292 #define IP_VER(ip_ihl_ver) ((ip_ihl_ver) >> 4)
293 #define IP_IHL(ip_ihl_ver) ((ip_ihl_ver) & 15)
294 #define IP_IHL_VER(ihl, ver) (((ver) << 4) | (ihl))
297 #define IP_ECN_MASK 0x03
298 #define IP_DSCP_MASK 0xfc
302 #define IP_DONT_FRAGMENT 0x4000 /* Don't fragment. */
303 #define IP_MORE_FRAGMENTS 0x2000 /* More fragments. */
304 #define IP_FRAG_OFF_MASK 0x1fff /* Fragment offset. */
305 #define IP_IS_FRAGMENT(ip_frag_off) \
306 ((ip_frag_off) & htons(IP_MORE_FRAGMENTS | IP_FRAG_OFF_MASK))
308 #define IP_HEADER_LEN 20
314 ovs_be16 ip_frag_off;
321 BUILD_ASSERT_DECL(IP_HEADER_LEN == sizeof(struct ip_header));
323 #define ICMP_HEADER_LEN 4
329 BUILD_ASSERT_DECL(ICMP_HEADER_LEN == sizeof(struct icmp_header));
331 #define UDP_HEADER_LEN 8
338 BUILD_ASSERT_DECL(UDP_HEADER_LEN == sizeof(struct udp_header));
347 #define TCP_FLAGS(tcp_ctl) (htons(tcp_ctl) & 0x003f)
348 #define TCP_OFFSET(tcp_ctl) (htons(tcp_ctl) >> 12)
350 #define TCP_HEADER_LEN 20
361 BUILD_ASSERT_DECL(TCP_HEADER_LEN == sizeof(struct tcp_header));
363 #define ARP_HRD_ETHERNET 1
364 #define ARP_PRO_IP 0x0800
365 #define ARP_OP_REQUEST 1
366 #define ARP_OP_REPLY 2
368 #define ARP_ETH_HEADER_LEN 28
369 struct arp_eth_header {
370 /* Generic members. */
371 ovs_be16 ar_hrd; /* Hardware type. */
372 ovs_be16 ar_pro; /* Protocol type. */
373 uint8_t ar_hln; /* Hardware address length. */
374 uint8_t ar_pln; /* Protocol address length. */
375 ovs_be16 ar_op; /* Opcode. */
377 /* Ethernet+IPv4 specific members. */
378 uint8_t ar_sha[ETH_ADDR_LEN]; /* Sender hardware address. */
379 ovs_be32 ar_spa; /* Sender protocol address. */
380 uint8_t ar_tha[ETH_ADDR_LEN]; /* Target hardware address. */
381 ovs_be32 ar_tpa; /* Target protocol address. */
382 } __attribute__((packed));
383 BUILD_ASSERT_DECL(ARP_ETH_HEADER_LEN == sizeof(struct arp_eth_header));
385 extern const struct in6_addr in6addr_exact;
386 #define IN6ADDR_EXACT_INIT { { { 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, \
387 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff } } }
389 static inline bool ipv6_addr_equals(const struct in6_addr *a,
390 const struct in6_addr *b)
392 #ifdef IN6_ARE_ADDR_EQUAL
393 return IN6_ARE_ADDR_EQUAL(a, b);
395 return !memcmp(a, b, sizeof(*a));
399 static inline bool ipv6_mask_is_any(const struct in6_addr *mask) {
400 return ipv6_addr_equals(mask, &in6addr_any);
403 static inline bool ipv6_mask_is_exact(const struct in6_addr *mask) {
404 return ipv6_addr_equals(mask, &in6addr_exact);
407 void format_ipv6_addr(char *addr_str, const struct in6_addr *addr);
408 void print_ipv6_addr(struct ds *string, const struct in6_addr *addr);
409 struct in6_addr ipv6_addr_bitand(const struct in6_addr *src,
410 const struct in6_addr *mask);
411 struct in6_addr ipv6_create_mask(int mask);
412 int ipv6_count_cidr_bits(const struct in6_addr *netmask);
413 bool ipv6_is_cidr(const struct in6_addr *netmask);
415 /* Masks for lacp_info state member. */
416 #define LACP_STATE_ACT 0x01 /* Activity. Active or passive? */
417 #define LACP_STATE_TIME 0x02 /* Timeout. Short or long timeout? */
418 #define LACP_STATE_AGG 0x04 /* Aggregation. Is the link is bondable? */
419 #define LACP_STATE_SYNC 0x08 /* Synchronization. Is the link in up to date? */
420 #define LACP_STATE_COL 0x10 /* Collecting. Is the link receiving frames? */
421 #define LACP_STATE_DIST 0x20 /* Distributing. Is the link sending frames? */
422 #define LACP_STATE_DEF 0x40 /* Defaulted. Using default partner info? */
423 #define LACP_STATE_EXP 0x80 /* Expired. Using expired partner info? */
425 #define LACP_FAST_TIME_TX 1000 /* Fast transmission rate. */
426 #define LACP_SLOW_TIME_TX 30000 /* Slow transmission rate. */
427 #define LACP_FAST_TIME_RX (LACP_FAST_TIME_TX * 3) /* Fast receive rate. */
428 #define LACP_SLOW_TIME_RX (LACP_SLOW_TIME_TX * 3) /* Slow receive rate. */
430 #define LACP_INFO_LEN 15
432 ovs_be16 sys_priority; /* System priority. */
433 uint8_t sysid[ETH_ADDR_LEN]; /* System ID. */
434 ovs_be16 key; /* Operational key. */
435 ovs_be16 port_priority; /* Port priority. */
436 ovs_be16 portid; /* Port ID. */
437 uint8_t state; /* State mask. See LACP_STATE macros. */
438 } __attribute__((packed));
439 BUILD_ASSERT_DECL(LACP_INFO_LEN == sizeof(struct lacp_info));
441 #define LACP_PDU_LEN 110
443 uint8_t subtype; /* Always 1. */
444 uint8_t version; /* Always 1. */
446 uint8_t actor_type; /* Always 1. */
447 uint8_t actor_len; /* Always 20. */
448 struct lacp_info actor; /* LACP actor information. */
449 uint8_t z1[3]; /* Reserved. Always 0. */
451 uint8_t partner_type; /* Always 2. */
452 uint8_t partner_len; /* Always 20. */
453 struct lacp_info partner; /* LACP partner information. */
454 uint8_t z2[3]; /* Reserved. Always 0. */
456 uint8_t collector_type; /* Always 3. */
457 uint8_t collector_len; /* Always 16. */
458 ovs_be16 collector_delay; /* Maximum collector delay. Set to UINT16_MAX. */
459 uint8_t z3[64]; /* Combination of several fields. Always 0. */
460 } __attribute__((packed));
461 BUILD_ASSERT_DECL(LACP_PDU_LEN == sizeof(struct lacp_pdu));
463 void compose_lacp_packet(struct ofpbuf *, struct lacp_info *actor,
464 struct lacp_info *partner,
465 const uint8_t eth_src[ETH_ADDR_LEN]);
467 const struct lacp_pdu *parse_lacp_packet(const struct ofpbuf *);
469 #endif /* packets.h */