2 * Copyright (c) 2008, 2009 Nicira Networks.
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 #define ETH_ADDR_LEN 6
27 static const uint8_t eth_addr_broadcast[ETH_ADDR_LEN] UNUSED
28 = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
30 static inline bool eth_addr_is_broadcast(const uint8_t ea[6])
32 return (ea[0] & ea[1] & ea[2] & ea[3] & ea[4] & ea[5]) == 0xff;
35 /* Returns true if 'ea' is an Ethernet address used for virtual interfaces
36 * under XenServer. Generally the actual Ethernet address is FE:FF:FF:FF:FF:FF
37 * but it can be FE:FE:FE:FE:FE:FE in some cases. */
38 static inline bool eth_addr_is_vif(const uint8_t ea[6])
40 return ea[0] == 0xfe && (ea[1] & ea[2] & ea[3] & ea[4] & ea[5]) >= 0xfe;
43 static inline bool eth_addr_is_multicast(const uint8_t ea[6])
47 static inline bool eth_addr_is_local(const uint8_t ea[6])
51 static inline bool eth_addr_is_zero(const uint8_t ea[6])
53 return !(ea[0] | ea[1] | ea[2] | ea[3] | ea[4] | ea[5]);
55 static inline bool eth_addr_equals(const uint8_t a[ETH_ADDR_LEN],
56 const uint8_t b[ETH_ADDR_LEN])
58 return !memcmp(a, b, ETH_ADDR_LEN);
60 static inline uint64_t eth_addr_to_uint64(const uint8_t ea[ETH_ADDR_LEN])
62 return (((uint64_t) ea[0] << 40)
63 | ((uint64_t) ea[1] << 32)
64 | ((uint64_t) ea[2] << 24)
65 | ((uint64_t) ea[3] << 16)
66 | ((uint64_t) ea[4] << 8)
69 static inline void eth_addr_from_uint64(uint64_t x, uint8_t ea[ETH_ADDR_LEN])
78 static inline void eth_addr_mark_random(uint8_t ea[ETH_ADDR_LEN])
80 ea[0] &= ~1; /* Unicast. */
81 ea[0] |= 2; /* Private. */
83 static inline void eth_addr_random(uint8_t ea[ETH_ADDR_LEN])
85 random_bytes(ea, ETH_ADDR_LEN);
86 eth_addr_mark_random(ea);
88 /* Returns true if 'ea' is a reserved multicast address, that a bridge must
89 * never forward, false otherwise. */
90 static inline bool eth_addr_is_reserved(const uint8_t ea[ETH_ADDR_LEN])
97 && (ea[5] & 0xf0) == 0x00);
100 #define ETH_ADDR_FMT \
101 "%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8
102 #define ETH_ADDR_ARGS(ea) \
103 (ea)[0], (ea)[1], (ea)[2], (ea)[3], (ea)[4], (ea)[5]
105 #define ETH_TYPE_IP 0x0800
106 #define ETH_TYPE_ARP 0x0806
107 #define ETH_TYPE_VLAN 0x8100
109 #define ETH_HEADER_LEN 14
110 #define ETH_PAYLOAD_MIN 46
111 #define ETH_PAYLOAD_MAX 1500
112 #define ETH_TOTAL_MIN (ETH_HEADER_LEN + ETH_PAYLOAD_MIN)
113 #define ETH_TOTAL_MAX (ETH_HEADER_LEN + ETH_PAYLOAD_MAX)
114 #define ETH_VLAN_TOTAL_MAX (ETH_HEADER_LEN + VLAN_HEADER_LEN + ETH_PAYLOAD_MAX)
116 uint8_t eth_dst[ETH_ADDR_LEN];
117 uint8_t eth_src[ETH_ADDR_LEN];
119 } __attribute__((packed));
120 BUILD_ASSERT_DECL(ETH_HEADER_LEN == sizeof(struct eth_header));
122 #define LLC_DSAP_SNAP 0xaa
123 #define LLC_SSAP_SNAP 0xaa
124 #define LLC_CNTL_SNAP 3
126 #define LLC_HEADER_LEN 3
131 } __attribute__((packed));
132 BUILD_ASSERT_DECL(LLC_HEADER_LEN == sizeof(struct llc_header));
134 #define SNAP_ORG_ETHERNET "\0\0" /* The compiler adds a null byte, so
135 sizeof(SNAP_ORG_ETHERNET) == 3. */
136 #define SNAP_HEADER_LEN 5
140 } __attribute__((packed));
141 BUILD_ASSERT_DECL(SNAP_HEADER_LEN == sizeof(struct snap_header));
143 #define LLC_SNAP_HEADER_LEN (LLC_HEADER_LEN + SNAP_HEADER_LEN)
144 struct llc_snap_header {
145 struct llc_header llc;
146 struct snap_header snap;
147 } __attribute__((packed));
148 BUILD_ASSERT_DECL(LLC_SNAP_HEADER_LEN == sizeof(struct llc_snap_header));
150 #define VLAN_VID_MASK 0x0fff
151 #define VLAN_PCP_MASK 0xe000
153 #define VLAN_HEADER_LEN 4
155 uint16_t vlan_tci; /* Lowest 12 bits are VLAN ID. */
156 uint16_t vlan_next_type;
158 BUILD_ASSERT_DECL(VLAN_HEADER_LEN == sizeof(struct vlan_header));
160 #define VLAN_ETH_HEADER_LEN (ETH_HEADER_LEN + VLAN_HEADER_LEN)
161 struct vlan_eth_header {
162 uint8_t veth_dst[ETH_ADDR_LEN];
163 uint8_t veth_src[ETH_ADDR_LEN];
164 uint16_t veth_type; /* Always htons(ETH_TYPE_VLAN). */
165 uint16_t veth_tci; /* Lowest 12 bits are VLAN ID. */
166 uint16_t veth_next_type;
167 } __attribute__((packed));
168 BUILD_ASSERT_DECL(VLAN_ETH_HEADER_LEN == sizeof(struct vlan_eth_header));
170 /* The "(void) (ip)[0]" below has no effect on the value, since it's the first
171 * argument of a comma expression, but it makes sure that 'ip' is a pointer.
172 * This is useful since a common mistake is to pass an integer instead of a
173 * pointer to IP_ARGS. */
174 #define IP_FMT "%"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8
175 #define IP_ARGS(ip) \
176 ((void) (ip)[0], ((uint8_t *) ip)[0]), \
177 ((uint8_t *) ip)[1], \
178 ((uint8_t *) ip)[2], \
181 #define IP_VER(ip_ihl_ver) ((ip_ihl_ver) >> 4)
182 #define IP_IHL(ip_ihl_ver) ((ip_ihl_ver) & 15)
183 #define IP_IHL_VER(ihl, ver) (((ver) << 4) | (ihl))
185 #define IP_TYPE_ICMP 1
186 #define IP_TYPE_TCP 6
187 #define IP_TYPE_UDP 17
191 #define IP_DONT_FRAGMENT 0x4000 /* Don't fragment. */
192 #define IP_MORE_FRAGMENTS 0x2000 /* More fragments. */
193 #define IP_FRAG_OFF_MASK 0x1fff /* Fragment offset. */
194 #define IP_IS_FRAGMENT(ip_frag_off) \
195 ((ip_frag_off) & htons(IP_MORE_FRAGMENTS | IP_FRAG_OFF_MASK))
197 #define IP_HEADER_LEN 20
203 uint16_t ip_frag_off;
210 BUILD_ASSERT_DECL(IP_HEADER_LEN == sizeof(struct ip_header));
212 #define ICMP_HEADER_LEN 4
218 BUILD_ASSERT_DECL(ICMP_HEADER_LEN == sizeof(struct icmp_header));
220 #define UDP_HEADER_LEN 8
227 BUILD_ASSERT_DECL(UDP_HEADER_LEN == sizeof(struct udp_header));
236 #define TCP_FLAGS(tcp_ctl) (htons(tcp_ctl) & 0x003f)
237 #define TCP_OFFSET(tcp_ctl) (htons(tcp_ctl) >> 12)
239 #define TCP_HEADER_LEN 20
250 BUILD_ASSERT_DECL(TCP_HEADER_LEN == sizeof(struct tcp_header));
252 #define ARP_HRD_ETHERNET 1
253 #define ARP_PRO_IP 0x0800
254 #define ARP_OP_REQUEST 1
255 #define ARP_OP_REPLY 2
257 #define ARP_ETH_HEADER_LEN 28
258 struct arp_eth_header {
259 /* Generic members. */
260 uint16_t ar_hrd; /* Hardware type. */
261 uint16_t ar_pro; /* Protocol type. */
262 uint8_t ar_hln; /* Hardware address length. */
263 uint8_t ar_pln; /* Protocol address length. */
264 uint16_t ar_op; /* Opcode. */
266 /* Ethernet+IPv4 specific members. */
267 uint8_t ar_sha[ETH_ADDR_LEN]; /* Sender hardware address. */
268 uint32_t ar_spa; /* Sender protocol address. */
269 uint8_t ar_tha[ETH_ADDR_LEN]; /* Target hardware address. */
270 uint32_t ar_tpa; /* Target protocol address. */
271 } __attribute__((packed));
272 BUILD_ASSERT_DECL(ARP_ETH_HEADER_LEN == sizeof(struct arp_eth_header));
274 #endif /* packets.h */