1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
4 * We are making the OpenFlow specification and associated documentation
5 * (Software) available for public use and benefit with the expectation
6 * that others will use, modify and enhance the Software and contribute
7 * those enhancements back to the community. However, since we would
8 * like to make the Software available for broadest use, with as few
9 * restrictions as possible permission is hereby granted, free of
10 * charge, to any person obtaining a copy of this Software to deal in
11 * the Software under the copyrights without restriction, including
12 * without limitation the rights to use, copy, modify, merge, publish,
13 * distribute, sublicense, and/or sell copies of the Software, and to
14 * permit persons to whom the Software is furnished to do so, subject to
15 * the following conditions:
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 * The name and trademarks of copyright holder(s) may NOT be used in
30 * advertising or publicity pertaining to the Software or any
31 * derivatives without specific, written prior permission.
42 #define ETH_ADDR_LEN 6
44 static const uint8_t eth_addr_broadcast[ETH_ADDR_LEN] UNUSED
45 = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
47 static inline bool eth_addr_is_broadcast(const uint8_t ea[6])
49 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])
59 static inline bool eth_addr_is_zero(const uint8_t ea[6])
61 return !(ea[0] | ea[1] | ea[2] | ea[3] | ea[4] | ea[5]);
63 static inline bool eth_addr_equals(const uint8_t a[ETH_ADDR_LEN],
64 const uint8_t b[ETH_ADDR_LEN])
66 return !memcmp(a, b, ETH_ADDR_LEN);
68 static inline uint64_t eth_addr_to_uint64(const uint8_t ea[ETH_ADDR_LEN])
70 return (((uint64_t) ea[0] << 40)
71 | ((uint64_t) ea[1] << 32)
72 | ((uint64_t) ea[2] << 24)
73 | ((uint64_t) ea[3] << 16)
74 | ((uint64_t) ea[4] << 8)
77 static inline void eth_addr_from_uint64(uint64_t x, uint8_t ea[ETH_ADDR_LEN])
86 static inline void eth_addr_random(uint8_t ea[ETH_ADDR_LEN])
88 random_bytes(ea, ETH_ADDR_LEN);
89 ea[0] &= ~1; /* Unicast. */
90 ea[0] |= 2; /* Private. */
93 #define ETH_ADDR_FMT \
94 "%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8
95 #define ETH_ADDR_ARGS(ea) \
96 (ea)[0], (ea)[1], (ea)[2], (ea)[3], (ea)[4], (ea)[5]
98 #define ETH_TYPE_IP 0x0800
99 #define ETH_TYPE_ARP 0x0806
100 #define ETH_TYPE_VLAN 0x8100
102 #define ETH_HEADER_LEN 14
103 #define ETH_PAYLOAD_MIN 46
104 #define ETH_PAYLOAD_MAX 1500
105 #define ETH_TOTAL_MIN (ETH_HEADER_LEN + ETH_PAYLOAD_MIN)
106 #define ETH_TOTAL_MAX (ETH_HEADER_LEN + ETH_PAYLOAD_MAX)
107 #define ETH_VLAN_TOTAL_MAX (ETH_HEADER_LEN + VLAN_HEADER_LEN + ETH_PAYLOAD_MAX)
109 uint8_t eth_dst[ETH_ADDR_LEN];
110 uint8_t eth_src[ETH_ADDR_LEN];
112 } __attribute__((packed));
113 BUILD_ASSERT_DECL(ETH_HEADER_LEN == sizeof(struct eth_header));
115 #define LLC_DSAP_SNAP 0xaa
116 #define LLC_SSAP_SNAP 0xaa
117 #define LLC_CNTL_SNAP 3
119 #define LLC_HEADER_LEN 3
124 } __attribute__((packed));
125 BUILD_ASSERT_DECL(LLC_HEADER_LEN == sizeof(struct llc_header));
127 #define SNAP_ORG_ETHERNET "\0\0" /* The compiler adds a null byte, so
128 sizeof(SNAP_ORG_ETHERNET) == 3. */
129 #define SNAP_HEADER_LEN 5
133 } __attribute__((packed));
134 BUILD_ASSERT_DECL(SNAP_HEADER_LEN == sizeof(struct snap_header));
136 #define LLC_SNAP_HEADER_LEN (LLC_HEADER_LEN + SNAP_HEADER_LEN)
137 struct llc_snap_header {
138 struct llc_header llc;
139 struct snap_header snap;
140 } __attribute__((packed));
141 BUILD_ASSERT_DECL(LLC_SNAP_HEADER_LEN == sizeof(struct llc_snap_header));
143 #define VLAN_VID_MASK 0x0fff
144 #define VLAN_PCP_MASK 0xe000
146 #define VLAN_HEADER_LEN 4
148 uint16_t vlan_tci; /* Lowest 12 bits are VLAN ID. */
149 uint16_t vlan_next_type;
151 BUILD_ASSERT_DECL(VLAN_HEADER_LEN == sizeof(struct vlan_header));
153 #define VLAN_ETH_HEADER_LEN (ETH_HEADER_LEN + VLAN_HEADER_LEN)
154 struct vlan_eth_header {
155 uint8_t veth_dst[ETH_ADDR_LEN];
156 uint8_t veth_src[ETH_ADDR_LEN];
157 uint16_t veth_type; /* Always htons(ETH_TYPE_VLAN). */
158 uint16_t veth_tci; /* Lowest 12 bits are VLAN ID. */
159 uint16_t veth_next_type;
160 } __attribute__((packed));
161 BUILD_ASSERT_DECL(VLAN_ETH_HEADER_LEN == sizeof(struct vlan_eth_header));
163 #define IP_FMT "%"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8
164 #define IP_ARGS(ip) \
165 ((uint8_t *) ip)[0], \
166 ((uint8_t *) ip)[1], \
167 ((uint8_t *) ip)[2], \
170 #define IP_VER(ip_ihl_ver) ((ip_ihl_ver) >> 4)
171 #define IP_IHL(ip_ihl_ver) ((ip_ihl_ver) & 15)
172 #define IP_IHL_VER(ihl, ver) (((ver) << 4) | (ihl))
174 #define IP_TYPE_ICMP 1
175 #define IP_TYPE_TCP 6
176 #define IP_TYPE_UDP 17
180 #define IP_DONT_FRAGMENT 0x4000 /* Don't fragment. */
181 #define IP_MORE_FRAGMENTS 0x2000 /* More fragments. */
182 #define IP_FRAG_OFF_MASK 0x1fff /* Fragment offset. */
183 #define IP_IS_FRAGMENT(ip_frag_off) \
184 ((ip_frag_off) & htons(IP_MORE_FRAGMENTS | IP_FRAG_OFF_MASK))
186 #define IP_HEADER_LEN 20
192 uint16_t ip_frag_off;
199 BUILD_ASSERT_DECL(IP_HEADER_LEN == sizeof(struct ip_header));
201 #define UDP_HEADER_LEN 8
208 BUILD_ASSERT_DECL(UDP_HEADER_LEN == sizeof(struct udp_header));
217 #define TCP_FLAGS(tcp_ctl) (htons(tcp_ctl) & 0x003f)
218 #define TCP_OFFSET(tcp_ctl) (htons(tcp_ctl) >> 12)
220 #define TCP_HEADER_LEN 20
231 BUILD_ASSERT_DECL(TCP_HEADER_LEN == sizeof(struct tcp_header));
233 #define ARP_HRD_ETHERNET 1
234 #define ARP_PRO_IP 0x0800
235 #define ARP_OP_REQUEST 1
236 #define ARP_OP_REPLY 2
238 #define ARP_ETH_HEADER_LEN 28
239 struct arp_eth_header {
240 /* Generic members. */
241 uint16_t ar_hrd; /* Hardware type. */
242 uint16_t ar_pro; /* Protocol type. */
243 uint8_t ar_hln; /* Hardware address length. */
244 uint8_t ar_pln; /* Protocol address length. */
245 uint16_t ar_op; /* Opcode. */
247 /* Ethernet+IPv4 specific members. */
248 uint8_t ar_sha[ETH_ADDR_LEN]; /* Sender hardware address. */
249 uint32_t ar_spa; /* Sender protocol address. */
250 uint8_t ar_tha[ETH_ADDR_LEN]; /* Target hardware address. */
251 uint32_t ar_tpa; /* Target protocol address. */
252 } __attribute__((packed));
253 BUILD_ASSERT_DECL(ARP_ETH_HEADER_LEN == sizeof(struct arp_eth_header));
255 #endif /* packets.h */