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.
29 bool dpid_from_string(const char *s, uint64_t *dpidp);
31 #define ETH_ADDR_LEN 6
33 static const uint8_t eth_addr_broadcast[ETH_ADDR_LEN] OVS_UNUSED
34 = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
36 static inline bool eth_addr_is_broadcast(const uint8_t ea[6])
38 return (ea[0] & ea[1] & ea[2] & ea[3] & ea[4] & ea[5]) == 0xff;
41 static inline bool eth_addr_is_multicast(const uint8_t ea[6])
45 static inline bool eth_addr_is_local(const uint8_t ea[6])
47 /* Local if it is either a locally administered address or a Nicira random
50 || (ea[0] == 0x00 && ea[1] == 0x23 && ea[2] == 0x20 && !!(ea[3] & 0x80));
52 static inline bool eth_addr_is_zero(const uint8_t ea[6])
54 return !(ea[0] | ea[1] | ea[2] | ea[3] | ea[4] | ea[5]);
56 static inline bool eth_addr_equals(const uint8_t a[ETH_ADDR_LEN],
57 const uint8_t b[ETH_ADDR_LEN])
59 return !memcmp(a, b, ETH_ADDR_LEN);
61 static inline uint64_t eth_addr_to_uint64(const uint8_t ea[ETH_ADDR_LEN])
63 return (((uint64_t) ea[0] << 40)
64 | ((uint64_t) ea[1] << 32)
65 | ((uint64_t) ea[2] << 24)
66 | ((uint64_t) ea[3] << 16)
67 | ((uint64_t) ea[4] << 8)
70 static inline void eth_addr_from_uint64(uint64_t x, uint8_t ea[ETH_ADDR_LEN])
79 static inline void eth_addr_mark_random(uint8_t ea[ETH_ADDR_LEN])
81 ea[0] &= ~1; /* Unicast. */
82 ea[0] |= 2; /* Private. */
84 static inline void eth_addr_random(uint8_t ea[ETH_ADDR_LEN])
86 random_bytes(ea, ETH_ADDR_LEN);
87 eth_addr_mark_random(ea);
89 static inline void eth_addr_nicira_random(uint8_t ea[ETH_ADDR_LEN])
93 /* Set the OUI to the Nicira one. */
98 /* Set the top bit to indicate random Nicira address. */
101 /* Returns true if 'ea' is a reserved multicast address, that a bridge must
102 * never forward, false otherwise. */
103 static inline bool eth_addr_is_reserved(const uint8_t ea[ETH_ADDR_LEN])
105 return (ea[0] == 0x01
110 && (ea[5] & 0xf0) == 0x00);
113 bool eth_addr_from_string(const char *, uint8_t ea[ETH_ADDR_LEN]);
115 void compose_benign_packet(struct ofpbuf *, const char *tag,
117 const uint8_t eth_src[ETH_ADDR_LEN]);
121 * uint8_t mac[ETH_ADDR_LEN];
123 * printf("The Ethernet address is "ETH_ADDR_FMT"\n", ETH_ADDR_ARGS(mac));
126 #define ETH_ADDR_FMT \
127 "%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8
128 #define ETH_ADDR_ARGS(ea) \
129 (ea)[0], (ea)[1], (ea)[2], (ea)[3], (ea)[4], (ea)[5]
133 * char *string = "1 00:11:22:33:44:55 2";
134 * uint8_t mac[ETH_ADDR_LEN];
137 * if (sscanf(string, "%d"ETH_ADDR_SCAN_FMT"%d",
138 * &a, ETH_ADDR_SCAN_ARGS(mac), &b) == 1 + ETH_ADDR_SCAN_COUNT + 1) {
142 #define ETH_ADDR_SCAN_FMT "%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8
143 #define ETH_ADDR_SCAN_ARGS(ea) \
144 &(ea)[0], &(ea)[1], &(ea)[2], &(ea)[3], &(ea)[4], &(ea)[5]
145 #define ETH_ADDR_SCAN_COUNT 6
147 #define ETH_TYPE_IP 0x0800
148 #define ETH_TYPE_ARP 0x0806
149 #define ETH_TYPE_VLAN 0x8100
151 #define ETH_HEADER_LEN 14
152 #define ETH_PAYLOAD_MIN 46
153 #define ETH_PAYLOAD_MAX 1500
154 #define ETH_TOTAL_MIN (ETH_HEADER_LEN + ETH_PAYLOAD_MIN)
155 #define ETH_TOTAL_MAX (ETH_HEADER_LEN + ETH_PAYLOAD_MAX)
156 #define ETH_VLAN_TOTAL_MAX (ETH_HEADER_LEN + VLAN_HEADER_LEN + ETH_PAYLOAD_MAX)
158 uint8_t eth_dst[ETH_ADDR_LEN];
159 uint8_t eth_src[ETH_ADDR_LEN];
161 } __attribute__((packed));
162 BUILD_ASSERT_DECL(ETH_HEADER_LEN == sizeof(struct eth_header));
164 #define LLC_DSAP_SNAP 0xaa
165 #define LLC_SSAP_SNAP 0xaa
166 #define LLC_CNTL_SNAP 3
168 #define LLC_HEADER_LEN 3
173 } __attribute__((packed));
174 BUILD_ASSERT_DECL(LLC_HEADER_LEN == sizeof(struct llc_header));
176 #define SNAP_ORG_ETHERNET "\0\0" /* The compiler adds a null byte, so
177 sizeof(SNAP_ORG_ETHERNET) == 3. */
178 #define SNAP_HEADER_LEN 5
182 } __attribute__((packed));
183 BUILD_ASSERT_DECL(SNAP_HEADER_LEN == sizeof(struct snap_header));
185 #define LLC_SNAP_HEADER_LEN (LLC_HEADER_LEN + SNAP_HEADER_LEN)
186 struct llc_snap_header {
187 struct llc_header llc;
188 struct snap_header snap;
189 } __attribute__((packed));
190 BUILD_ASSERT_DECL(LLC_SNAP_HEADER_LEN == sizeof(struct llc_snap_header));
192 #define VLAN_VID_MASK 0x0fff
193 #define VLAN_VID_SHIFT 0
195 #define VLAN_PCP_MASK 0xe000
196 #define VLAN_PCP_SHIFT 13
198 #define VLAN_HEADER_LEN 4
200 uint16_t vlan_tci; /* Lowest 12 bits are VLAN ID. */
201 uint16_t vlan_next_type;
203 BUILD_ASSERT_DECL(VLAN_HEADER_LEN == sizeof(struct vlan_header));
205 #define VLAN_ETH_HEADER_LEN (ETH_HEADER_LEN + VLAN_HEADER_LEN)
206 struct vlan_eth_header {
207 uint8_t veth_dst[ETH_ADDR_LEN];
208 uint8_t veth_src[ETH_ADDR_LEN];
209 uint16_t veth_type; /* Always htons(ETH_TYPE_VLAN). */
210 uint16_t veth_tci; /* Lowest 12 bits are VLAN ID. */
211 uint16_t veth_next_type;
212 } __attribute__((packed));
213 BUILD_ASSERT_DECL(VLAN_ETH_HEADER_LEN == sizeof(struct vlan_eth_header));
215 /* The "(void) (ip)[0]" below has no effect on the value, since it's the first
216 * argument of a comma expression, but it makes sure that 'ip' is a pointer.
217 * This is useful since a common mistake is to pass an integer instead of a
218 * pointer to IP_ARGS. */
219 #define IP_FMT "%"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8
220 #define IP_ARGS(ip) \
221 ((void) (ip)[0], ((uint8_t *) ip)[0]), \
222 ((uint8_t *) ip)[1], \
223 ((uint8_t *) ip)[2], \
226 #define IP_VER(ip_ihl_ver) ((ip_ihl_ver) >> 4)
227 #define IP_IHL(ip_ihl_ver) ((ip_ihl_ver) & 15)
228 #define IP_IHL_VER(ihl, ver) (((ver) << 4) | (ihl))
231 #define IP_ECN_MASK 0x03
232 #define IP_DSCP_MASK 0xfc
234 #define IP_TYPE_ICMP 1
235 #define IP_TYPE_TCP 6
236 #define IP_TYPE_UDP 17
240 #define IP_DONT_FRAGMENT 0x4000 /* Don't fragment. */
241 #define IP_MORE_FRAGMENTS 0x2000 /* More fragments. */
242 #define IP_FRAG_OFF_MASK 0x1fff /* Fragment offset. */
243 #define IP_IS_FRAGMENT(ip_frag_off) \
244 ((ip_frag_off) & htons(IP_MORE_FRAGMENTS | IP_FRAG_OFF_MASK))
246 #define IP_HEADER_LEN 20
252 uint16_t ip_frag_off;
259 BUILD_ASSERT_DECL(IP_HEADER_LEN == sizeof(struct ip_header));
261 #define ICMP_HEADER_LEN 4
267 BUILD_ASSERT_DECL(ICMP_HEADER_LEN == sizeof(struct icmp_header));
269 #define UDP_HEADER_LEN 8
276 BUILD_ASSERT_DECL(UDP_HEADER_LEN == sizeof(struct udp_header));
285 #define TCP_FLAGS(tcp_ctl) (htons(tcp_ctl) & 0x003f)
286 #define TCP_OFFSET(tcp_ctl) (htons(tcp_ctl) >> 12)
288 #define TCP_HEADER_LEN 20
299 BUILD_ASSERT_DECL(TCP_HEADER_LEN == sizeof(struct tcp_header));
301 #define ARP_HRD_ETHERNET 1
302 #define ARP_PRO_IP 0x0800
303 #define ARP_OP_REQUEST 1
304 #define ARP_OP_REPLY 2
306 #define ARP_ETH_HEADER_LEN 28
307 struct arp_eth_header {
308 /* Generic members. */
309 uint16_t ar_hrd; /* Hardware type. */
310 uint16_t ar_pro; /* Protocol type. */
311 uint8_t ar_hln; /* Hardware address length. */
312 uint8_t ar_pln; /* Protocol address length. */
313 uint16_t ar_op; /* Opcode. */
315 /* Ethernet+IPv4 specific members. */
316 uint8_t ar_sha[ETH_ADDR_LEN]; /* Sender hardware address. */
317 uint32_t ar_spa; /* Sender protocol address. */
318 uint8_t ar_tha[ETH_ADDR_LEN]; /* Target hardware address. */
319 uint32_t ar_tpa; /* Target protocol address. */
320 } __attribute__((packed));
321 BUILD_ASSERT_DECL(ARP_ETH_HEADER_LEN == sizeof(struct arp_eth_header));
323 #endif /* packets.h */