]> git.proxmox.com Git - mirror_ovs.git/blob - lib/packets.h
Include "inttypes.h" to pickup definition of PRIu8.
[mirror_ovs.git] / lib / packets.h
1 /*
2 * Copyright (c) 2008, 2009 Nicira Networks.
3 *
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:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16 #ifndef PACKETS_H
17 #define PACKETS_H 1
18
19 #include <inttypes.h>
20 #include <stdint.h>
21 #include <string.h>
22 #include "compiler.h"
23 #include "random.h"
24 #include "util.h"
25
26 #define ETH_ADDR_LEN 6
27
28 static const uint8_t eth_addr_broadcast[ETH_ADDR_LEN] UNUSED
29 = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
30
31 static inline bool eth_addr_is_broadcast(const uint8_t ea[6])
32 {
33 return (ea[0] & ea[1] & ea[2] & ea[3] & ea[4] & ea[5]) == 0xff;
34 }
35
36 /* Returns true if 'ea' is an Ethernet address used for virtual interfaces
37 * under XenServer. Generally the actual Ethernet address is FE:FF:FF:FF:FF:FF
38 * but it can be FE:FE:FE:FE:FE:FE in some cases. */
39 static inline bool eth_addr_is_vif(const uint8_t ea[6])
40 {
41 return ea[0] == 0xfe && (ea[1] & ea[2] & ea[3] & ea[4] & ea[5]) >= 0xfe;
42 }
43
44 static inline bool eth_addr_is_multicast(const uint8_t ea[6])
45 {
46 return ea[0] & 1;
47 }
48 static inline bool eth_addr_is_local(const uint8_t ea[6])
49 {
50 return ea[0] & 2;
51 }
52 static inline bool eth_addr_is_zero(const uint8_t ea[6])
53 {
54 return !(ea[0] | ea[1] | ea[2] | ea[3] | ea[4] | ea[5]);
55 }
56 static inline bool eth_addr_equals(const uint8_t a[ETH_ADDR_LEN],
57 const uint8_t b[ETH_ADDR_LEN])
58 {
59 return !memcmp(a, b, ETH_ADDR_LEN);
60 }
61 static inline uint64_t eth_addr_to_uint64(const uint8_t ea[ETH_ADDR_LEN])
62 {
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)
68 | ea[5]);
69 }
70 static inline void eth_addr_from_uint64(uint64_t x, uint8_t ea[ETH_ADDR_LEN])
71 {
72 ea[0] = x >> 40;
73 ea[1] = x >> 32;
74 ea[2] = x >> 24;
75 ea[3] = x >> 16;
76 ea[4] = x >> 8;
77 ea[5] = x;
78 }
79 static inline void eth_addr_mark_random(uint8_t ea[ETH_ADDR_LEN])
80 {
81 ea[0] &= ~1; /* Unicast. */
82 ea[0] |= 2; /* Private. */
83 }
84 static inline void eth_addr_random(uint8_t ea[ETH_ADDR_LEN])
85 {
86 random_bytes(ea, ETH_ADDR_LEN);
87 eth_addr_mark_random(ea);
88 }
89 /* Returns true if 'ea' is a reserved multicast address, that a bridge must
90 * never forward, false otherwise. */
91 static inline bool eth_addr_is_reserved(const uint8_t ea[ETH_ADDR_LEN])
92 {
93 return (ea[0] == 0x01
94 && ea[1] == 0x80
95 && ea[2] == 0xc2
96 && ea[3] == 0x00
97 && ea[4] == 0x00
98 && (ea[5] & 0xf0) == 0x00);
99 }
100
101 #define ETH_ADDR_FMT \
102 "%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8
103 #define ETH_ADDR_ARGS(ea) \
104 (ea)[0], (ea)[1], (ea)[2], (ea)[3], (ea)[4], (ea)[5]
105
106 #define ETH_TYPE_IP 0x0800
107 #define ETH_TYPE_ARP 0x0806
108 #define ETH_TYPE_VLAN 0x8100
109
110 #define ETH_HEADER_LEN 14
111 #define ETH_PAYLOAD_MIN 46
112 #define ETH_PAYLOAD_MAX 1500
113 #define ETH_TOTAL_MIN (ETH_HEADER_LEN + ETH_PAYLOAD_MIN)
114 #define ETH_TOTAL_MAX (ETH_HEADER_LEN + ETH_PAYLOAD_MAX)
115 #define ETH_VLAN_TOTAL_MAX (ETH_HEADER_LEN + VLAN_HEADER_LEN + ETH_PAYLOAD_MAX)
116 struct eth_header {
117 uint8_t eth_dst[ETH_ADDR_LEN];
118 uint8_t eth_src[ETH_ADDR_LEN];
119 uint16_t eth_type;
120 } __attribute__((packed));
121 BUILD_ASSERT_DECL(ETH_HEADER_LEN == sizeof(struct eth_header));
122
123 #define LLC_DSAP_SNAP 0xaa
124 #define LLC_SSAP_SNAP 0xaa
125 #define LLC_CNTL_SNAP 3
126
127 #define LLC_HEADER_LEN 3
128 struct llc_header {
129 uint8_t llc_dsap;
130 uint8_t llc_ssap;
131 uint8_t llc_cntl;
132 } __attribute__((packed));
133 BUILD_ASSERT_DECL(LLC_HEADER_LEN == sizeof(struct llc_header));
134
135 #define SNAP_ORG_ETHERNET "\0\0" /* The compiler adds a null byte, so
136 sizeof(SNAP_ORG_ETHERNET) == 3. */
137 #define SNAP_HEADER_LEN 5
138 struct snap_header {
139 uint8_t snap_org[3];
140 uint16_t snap_type;
141 } __attribute__((packed));
142 BUILD_ASSERT_DECL(SNAP_HEADER_LEN == sizeof(struct snap_header));
143
144 #define LLC_SNAP_HEADER_LEN (LLC_HEADER_LEN + SNAP_HEADER_LEN)
145 struct llc_snap_header {
146 struct llc_header llc;
147 struct snap_header snap;
148 } __attribute__((packed));
149 BUILD_ASSERT_DECL(LLC_SNAP_HEADER_LEN == sizeof(struct llc_snap_header));
150
151 #define VLAN_VID_MASK 0x0fff
152 #define VLAN_PCP_MASK 0xe000
153
154 #define VLAN_HEADER_LEN 4
155 struct vlan_header {
156 uint16_t vlan_tci; /* Lowest 12 bits are VLAN ID. */
157 uint16_t vlan_next_type;
158 };
159 BUILD_ASSERT_DECL(VLAN_HEADER_LEN == sizeof(struct vlan_header));
160
161 #define VLAN_ETH_HEADER_LEN (ETH_HEADER_LEN + VLAN_HEADER_LEN)
162 struct vlan_eth_header {
163 uint8_t veth_dst[ETH_ADDR_LEN];
164 uint8_t veth_src[ETH_ADDR_LEN];
165 uint16_t veth_type; /* Always htons(ETH_TYPE_VLAN). */
166 uint16_t veth_tci; /* Lowest 12 bits are VLAN ID. */
167 uint16_t veth_next_type;
168 } __attribute__((packed));
169 BUILD_ASSERT_DECL(VLAN_ETH_HEADER_LEN == sizeof(struct vlan_eth_header));
170
171 /* The "(void) (ip)[0]" below has no effect on the value, since it's the first
172 * argument of a comma expression, but it makes sure that 'ip' is a pointer.
173 * This is useful since a common mistake is to pass an integer instead of a
174 * pointer to IP_ARGS. */
175 #define IP_FMT "%"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8
176 #define IP_ARGS(ip) \
177 ((void) (ip)[0], ((uint8_t *) ip)[0]), \
178 ((uint8_t *) ip)[1], \
179 ((uint8_t *) ip)[2], \
180 ((uint8_t *) ip)[3]
181
182 #define IP_VER(ip_ihl_ver) ((ip_ihl_ver) >> 4)
183 #define IP_IHL(ip_ihl_ver) ((ip_ihl_ver) & 15)
184 #define IP_IHL_VER(ihl, ver) (((ver) << 4) | (ihl))
185
186 #define IP_TYPE_ICMP 1
187 #define IP_TYPE_TCP 6
188 #define IP_TYPE_UDP 17
189
190 #define IP_VERSION 4
191
192 #define IP_DONT_FRAGMENT 0x4000 /* Don't fragment. */
193 #define IP_MORE_FRAGMENTS 0x2000 /* More fragments. */
194 #define IP_FRAG_OFF_MASK 0x1fff /* Fragment offset. */
195 #define IP_IS_FRAGMENT(ip_frag_off) \
196 ((ip_frag_off) & htons(IP_MORE_FRAGMENTS | IP_FRAG_OFF_MASK))
197
198 #define IP_HEADER_LEN 20
199 struct ip_header {
200 uint8_t ip_ihl_ver;
201 uint8_t ip_tos;
202 uint16_t ip_tot_len;
203 uint16_t ip_id;
204 uint16_t ip_frag_off;
205 uint8_t ip_ttl;
206 uint8_t ip_proto;
207 uint16_t ip_csum;
208 uint32_t ip_src;
209 uint32_t ip_dst;
210 };
211 BUILD_ASSERT_DECL(IP_HEADER_LEN == sizeof(struct ip_header));
212
213 #define ICMP_HEADER_LEN 4
214 struct icmp_header {
215 uint8_t icmp_type;
216 uint8_t icmp_code;
217 uint16_t icmp_csum;
218 };
219 BUILD_ASSERT_DECL(ICMP_HEADER_LEN == sizeof(struct icmp_header));
220
221 #define UDP_HEADER_LEN 8
222 struct udp_header {
223 uint16_t udp_src;
224 uint16_t udp_dst;
225 uint16_t udp_len;
226 uint16_t udp_csum;
227 };
228 BUILD_ASSERT_DECL(UDP_HEADER_LEN == sizeof(struct udp_header));
229
230 #define TCP_FIN 0x01
231 #define TCP_SYN 0x02
232 #define TCP_RST 0x04
233 #define TCP_PSH 0x08
234 #define TCP_ACK 0x10
235 #define TCP_URG 0x20
236
237 #define TCP_FLAGS(tcp_ctl) (htons(tcp_ctl) & 0x003f)
238 #define TCP_OFFSET(tcp_ctl) (htons(tcp_ctl) >> 12)
239
240 #define TCP_HEADER_LEN 20
241 struct tcp_header {
242 uint16_t tcp_src;
243 uint16_t tcp_dst;
244 uint32_t tcp_seq;
245 uint32_t tcp_ack;
246 uint16_t tcp_ctl;
247 uint16_t tcp_winsz;
248 uint16_t tcp_csum;
249 uint16_t tcp_urg;
250 };
251 BUILD_ASSERT_DECL(TCP_HEADER_LEN == sizeof(struct tcp_header));
252
253 #define ARP_HRD_ETHERNET 1
254 #define ARP_PRO_IP 0x0800
255 #define ARP_OP_REQUEST 1
256 #define ARP_OP_REPLY 2
257
258 #define ARP_ETH_HEADER_LEN 28
259 struct arp_eth_header {
260 /* Generic members. */
261 uint16_t ar_hrd; /* Hardware type. */
262 uint16_t ar_pro; /* Protocol type. */
263 uint8_t ar_hln; /* Hardware address length. */
264 uint8_t ar_pln; /* Protocol address length. */
265 uint16_t ar_op; /* Opcode. */
266
267 /* Ethernet+IPv4 specific members. */
268 uint8_t ar_sha[ETH_ADDR_LEN]; /* Sender hardware address. */
269 uint32_t ar_spa; /* Sender protocol address. */
270 uint8_t ar_tha[ETH_ADDR_LEN]; /* Target hardware address. */
271 uint32_t ar_tpa; /* Target protocol address. */
272 } __attribute__((packed));
273 BUILD_ASSERT_DECL(ARP_ETH_HEADER_LEN == sizeof(struct arp_eth_header));
274
275 #endif /* packets.h */