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"
32 bool dpid_from_string(const char *s, uint64_t *dpidp);
34 #define ETH_ADDR_LEN 6
36 static const uint8_t eth_addr_broadcast[ETH_ADDR_LEN] OVS_UNUSED
37 = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
39 static const uint8_t eth_addr_stp[ETH_ADDR_LEN] OVS_UNUSED
40 = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x01 };
42 static inline bool eth_addr_is_broadcast(const uint8_t ea[6])
44 return (ea[0] & ea[1] & ea[2] & ea[3] & ea[4] & ea[5]) == 0xff;
47 static inline bool eth_addr_is_multicast(const uint8_t ea[6])
51 static inline bool eth_addr_is_local(const uint8_t ea[6])
53 /* Local if it is either a locally administered address or a Nicira random
56 || (ea[0] == 0x00 && ea[1] == 0x23 && ea[2] == 0x20 && !!(ea[3] & 0x80));
58 static inline bool eth_addr_is_zero(const uint8_t ea[6])
60 return !(ea[0] | ea[1] | ea[2] | ea[3] | ea[4] | ea[5]);
62 static inline bool eth_addr_equals(const uint8_t a[ETH_ADDR_LEN],
63 const uint8_t b[ETH_ADDR_LEN])
65 return !memcmp(a, b, ETH_ADDR_LEN);
67 static inline uint64_t eth_addr_to_uint64(const uint8_t ea[ETH_ADDR_LEN])
69 return (((uint64_t) ea[0] << 40)
70 | ((uint64_t) ea[1] << 32)
71 | ((uint64_t) ea[2] << 24)
72 | ((uint64_t) ea[3] << 16)
73 | ((uint64_t) ea[4] << 8)
76 static inline void eth_addr_from_uint64(uint64_t x, uint8_t ea[ETH_ADDR_LEN])
85 static inline void eth_addr_mark_random(uint8_t ea[ETH_ADDR_LEN])
87 ea[0] &= ~1; /* Unicast. */
88 ea[0] |= 2; /* Private. */
90 static inline void eth_addr_random(uint8_t ea[ETH_ADDR_LEN])
92 random_bytes(ea, ETH_ADDR_LEN);
93 eth_addr_mark_random(ea);
95 static inline void eth_addr_nicira_random(uint8_t ea[ETH_ADDR_LEN])
99 /* Set the OUI to the Nicira one. */
104 /* Set the top bit to indicate random Nicira address. */
107 /* Returns true if 'ea' is a reserved multicast address, that a bridge must
108 * never forward, false otherwise. */
109 static inline bool eth_addr_is_reserved(const uint8_t ea[ETH_ADDR_LEN])
111 return (ea[0] == 0x01
116 && (ea[5] & 0xf0) == 0x00);
119 bool eth_addr_from_string(const char *, uint8_t ea[ETH_ADDR_LEN]);
121 void compose_benign_packet(struct ofpbuf *, const char *tag,
123 const uint8_t eth_src[ETH_ADDR_LEN]);
127 * uint8_t mac[ETH_ADDR_LEN];
129 * printf("The Ethernet address is "ETH_ADDR_FMT"\n", ETH_ADDR_ARGS(mac));
132 #define ETH_ADDR_FMT \
133 "%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8
134 #define ETH_ADDR_ARGS(ea) \
135 (ea)[0], (ea)[1], (ea)[2], (ea)[3], (ea)[4], (ea)[5]
139 * char *string = "1 00:11:22:33:44:55 2";
140 * uint8_t mac[ETH_ADDR_LEN];
143 * if (sscanf(string, "%d"ETH_ADDR_SCAN_FMT"%d",
144 * &a, ETH_ADDR_SCAN_ARGS(mac), &b) == 1 + ETH_ADDR_SCAN_COUNT + 1) {
148 #define ETH_ADDR_SCAN_FMT "%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8
149 #define ETH_ADDR_SCAN_ARGS(ea) \
150 &(ea)[0], &(ea)[1], &(ea)[2], &(ea)[3], &(ea)[4], &(ea)[5]
151 #define ETH_ADDR_SCAN_COUNT 6
153 #define ETH_TYPE_IP 0x0800
154 #define ETH_TYPE_ARP 0x0806
155 #define ETH_TYPE_VLAN 0x8100
156 #define ETH_TYPE_CFM 0x8902
158 #define ETH_HEADER_LEN 14
159 #define ETH_PAYLOAD_MIN 46
160 #define ETH_PAYLOAD_MAX 1500
161 #define ETH_TOTAL_MIN (ETH_HEADER_LEN + ETH_PAYLOAD_MIN)
162 #define ETH_TOTAL_MAX (ETH_HEADER_LEN + ETH_PAYLOAD_MAX)
163 #define ETH_VLAN_TOTAL_MAX (ETH_HEADER_LEN + VLAN_HEADER_LEN + ETH_PAYLOAD_MAX)
165 uint8_t eth_dst[ETH_ADDR_LEN];
166 uint8_t eth_src[ETH_ADDR_LEN];
168 } __attribute__((packed));
169 BUILD_ASSERT_DECL(ETH_HEADER_LEN == sizeof(struct eth_header));
171 #define LLC_DSAP_SNAP 0xaa
172 #define LLC_SSAP_SNAP 0xaa
173 #define LLC_CNTL_SNAP 3
175 #define LLC_HEADER_LEN 3
180 } __attribute__((packed));
181 BUILD_ASSERT_DECL(LLC_HEADER_LEN == sizeof(struct llc_header));
183 #define SNAP_ORG_ETHERNET "\0\0" /* The compiler adds a null byte, so
184 sizeof(SNAP_ORG_ETHERNET) == 3. */
185 #define SNAP_HEADER_LEN 5
189 } __attribute__((packed));
190 BUILD_ASSERT_DECL(SNAP_HEADER_LEN == sizeof(struct snap_header));
192 #define LLC_SNAP_HEADER_LEN (LLC_HEADER_LEN + SNAP_HEADER_LEN)
193 struct llc_snap_header {
194 struct llc_header llc;
195 struct snap_header snap;
196 } __attribute__((packed));
197 BUILD_ASSERT_DECL(LLC_SNAP_HEADER_LEN == sizeof(struct llc_snap_header));
199 #define VLAN_VID_MASK 0x0fff
200 #define VLAN_VID_SHIFT 0
202 #define VLAN_PCP_MASK 0xe000
203 #define VLAN_PCP_SHIFT 13
205 #define VLAN_CFI 0x1000
207 /* Given the vlan_tci field from an 802.1Q header, in network byte order,
208 * returns the VLAN ID in host byte order. */
209 static inline uint16_t
210 vlan_tci_to_vid(uint16_t vlan_tci)
212 return (ntohs(vlan_tci) & VLAN_VID_MASK) >> VLAN_VID_SHIFT;
215 /* Given the vlan_tci field from an 802.1Q header, in network byte order,
216 * returns the priority code point (PCP) in host byte order. */
218 vlan_tci_to_pcp(uint16_t vlan_tci)
220 return (ntohs(vlan_tci) & VLAN_PCP_MASK) >> VLAN_PCP_SHIFT;
223 #define VLAN_HEADER_LEN 4
225 uint16_t vlan_tci; /* Lowest 12 bits are VLAN ID. */
226 uint16_t vlan_next_type;
228 BUILD_ASSERT_DECL(VLAN_HEADER_LEN == sizeof(struct vlan_header));
230 #define VLAN_ETH_HEADER_LEN (ETH_HEADER_LEN + VLAN_HEADER_LEN)
231 struct vlan_eth_header {
232 uint8_t veth_dst[ETH_ADDR_LEN];
233 uint8_t veth_src[ETH_ADDR_LEN];
234 uint16_t veth_type; /* Always htons(ETH_TYPE_VLAN). */
235 uint16_t veth_tci; /* Lowest 12 bits are VLAN ID. */
236 uint16_t veth_next_type;
237 } __attribute__((packed));
238 BUILD_ASSERT_DECL(VLAN_ETH_HEADER_LEN == sizeof(struct vlan_eth_header));
240 /* A 'ccm' represents a Continuity Check Message from the 802.1ag specification.
241 * Continuity Check Messages are broadcast periodically so that hosts can
242 * determine who they have connectivity to. */
244 #define CCM_MAID_LEN 48
246 uint8_t mdlevel_version; /* MD Level and Version */
252 uint8_t maid[CCM_MAID_LEN];
253 uint8_t zero[16]; /* Defined by ITU-T Y.1731 should be zero */
254 } __attribute__((packed));
255 BUILD_ASSERT_DECL(CCM_LEN == sizeof(struct ccm));
257 /* The "(void) (ip)[0]" below has no effect on the value, since it's the first
258 * argument of a comma expression, but it makes sure that 'ip' is a pointer.
259 * This is useful since a common mistake is to pass an integer instead of a
260 * pointer to IP_ARGS. */
261 #define IP_FMT "%"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8
262 #define IP_ARGS(ip) \
263 ((void) (ip)[0], ((uint8_t *) ip)[0]), \
264 ((uint8_t *) ip)[1], \
265 ((uint8_t *) ip)[2], \
268 /* Returns true if 'netmask' is a CIDR netmask, that is, if it consists of N
269 * high-order 1-bits and 32-N low-order 0-bits. */
271 ip_is_cidr(ovs_be32 netmask)
273 uint32_t x = ~ntohl(netmask);
274 return !(x & (x + 1));
277 #define IP_VER(ip_ihl_ver) ((ip_ihl_ver) >> 4)
278 #define IP_IHL(ip_ihl_ver) ((ip_ihl_ver) & 15)
279 #define IP_IHL_VER(ihl, ver) (((ver) << 4) | (ihl))
282 #define IP_ECN_MASK 0x03
283 #define IP_DSCP_MASK 0xfc
285 #define IP_TYPE_ICMP 1
286 #define IP_TYPE_TCP 6
287 #define IP_TYPE_UDP 17
291 #define IP_DONT_FRAGMENT 0x4000 /* Don't fragment. */
292 #define IP_MORE_FRAGMENTS 0x2000 /* More fragments. */
293 #define IP_FRAG_OFF_MASK 0x1fff /* Fragment offset. */
294 #define IP_IS_FRAGMENT(ip_frag_off) \
295 ((ip_frag_off) & htons(IP_MORE_FRAGMENTS | IP_FRAG_OFF_MASK))
297 #define IP_HEADER_LEN 20
303 uint16_t ip_frag_off;
310 BUILD_ASSERT_DECL(IP_HEADER_LEN == sizeof(struct ip_header));
312 #define ICMP_HEADER_LEN 4
318 BUILD_ASSERT_DECL(ICMP_HEADER_LEN == sizeof(struct icmp_header));
320 #define UDP_HEADER_LEN 8
327 BUILD_ASSERT_DECL(UDP_HEADER_LEN == sizeof(struct udp_header));
336 #define TCP_FLAGS(tcp_ctl) (htons(tcp_ctl) & 0x003f)
337 #define TCP_OFFSET(tcp_ctl) (htons(tcp_ctl) >> 12)
339 #define TCP_HEADER_LEN 20
350 BUILD_ASSERT_DECL(TCP_HEADER_LEN == sizeof(struct tcp_header));
352 #define ARP_HRD_ETHERNET 1
353 #define ARP_PRO_IP 0x0800
354 #define ARP_OP_REQUEST 1
355 #define ARP_OP_REPLY 2
357 #define ARP_ETH_HEADER_LEN 28
358 struct arp_eth_header {
359 /* Generic members. */
360 uint16_t ar_hrd; /* Hardware type. */
361 uint16_t ar_pro; /* Protocol type. */
362 uint8_t ar_hln; /* Hardware address length. */
363 uint8_t ar_pln; /* Protocol address length. */
364 uint16_t ar_op; /* Opcode. */
366 /* Ethernet+IPv4 specific members. */
367 uint8_t ar_sha[ETH_ADDR_LEN]; /* Sender hardware address. */
368 uint32_t ar_spa; /* Sender protocol address. */
369 uint8_t ar_tha[ETH_ADDR_LEN]; /* Target hardware address. */
370 uint32_t ar_tpa; /* Target protocol address. */
371 } __attribute__((packed));
372 BUILD_ASSERT_DECL(ARP_ETH_HEADER_LEN == sizeof(struct arp_eth_header));
374 #endif /* packets.h */