2 * QEMU network structures definitions and helper functions
4 * Copyright (c) 2012 Ravello Systems LTD (http://ravellosystems.com)
6 * Developed by Daynix Computing LTD (http://www.daynix.com)
8 * Portions developed by Free Software Foundation, Inc
9 * Copyright (C) 1991-1997, 2001, 2003, 2006 Free Software Foundation, Inc.
10 * See netinet/ip6.h and netinet/in.h (GNU C Library)
12 * Portions developed by Igor Kovalenko
13 * Copyright (c) 2006 Igor Kovalenko
14 * See hw/rtl8139.c (QEMU)
17 * Dmitry Fleytman <dmitry@daynix.com>
18 * Tamir Shomer <tamirs@daynix.com>
19 * Yan Vugenfirer <yan@daynix.com>
21 * This work is licensed under the terms of the GNU GPL, version 2 or later.
22 * See the COPYING file in the top-level directory.
29 #include "qemu/bswap.h"
34 #define ETH_ZLEN 60 /* Min. octets in frame without FCS */
39 uint8_t h_dest
[ETH_ALEN
]; /* destination eth addr */
40 uint8_t h_source
[ETH_ALEN
]; /* source ether addr */
41 uint16_t h_proto
; /* packet type ID field */
45 uint16_t h_tci
; /* priority and VLAN ID */
46 uint16_t h_proto
; /* encapsulated protocol */
50 uint8_t ip_ver_len
; /* version and header length */
51 uint8_t ip_tos
; /* type of service */
52 uint16_t ip_len
; /* total length */
53 uint16_t ip_id
; /* identification */
54 uint16_t ip_off
; /* fragment offset field */
55 uint8_t ip_ttl
; /* time to live */
56 uint8_t ip_p
; /* protocol */
57 uint16_t ip_sum
; /* checksum */
58 uint32_t ip_src
, ip_dst
; /* source and destination address */
61 typedef struct tcp_header
{
62 uint16_t th_sport
; /* source port */
63 uint16_t th_dport
; /* destination port */
64 uint32_t th_seq
; /* sequence number */
65 uint32_t th_ack
; /* acknowledgment number */
66 uint16_t th_offset_flags
; /* data offset, reserved 6 bits, */
67 /* TCP protocol flags */
68 uint16_t th_win
; /* window */
69 uint16_t th_sum
; /* checksum */
70 uint16_t th_urp
; /* urgent pointer */
73 #define TCP_FLAGS_ONLY(flags) ((flags) & 0x3f)
75 #define TCP_HEADER_FLAGS(tcp) \
76 TCP_FLAGS_ONLY(be16_to_cpu((tcp)->th_offset_flags))
78 #define TCP_FLAG_ACK 0x10
80 #define TCP_HEADER_DATA_OFFSET(tcp) \
81 (((be16_to_cpu((tcp)->th_offset_flags) >> 12) & 0xf) << 2)
83 typedef struct udp_header
{
84 uint16_t uh_sport
; /* source port */
85 uint16_t uh_dport
; /* destination port */
86 uint16_t uh_ulen
; /* udp length */
87 uint16_t uh_sum
; /* udp checksum */
90 typedef struct ip_pseudo_header
{
101 uint8_t __u6_addr8
[16];
108 uint32_t ip6_un1_flow
; /* 4 bits version, 8 bits TC,
110 uint16_t ip6_un1_plen
; /* payload length */
111 uint8_t ip6_un1_nxt
; /* next header */
112 uint8_t ip6_un1_hlim
; /* hop limit */
114 uint8_t ip6_un2_vfc
; /* 4 bits version, top 4 bits tclass */
115 struct ip6_ecn_access
{
116 uint8_t ip6_un3_vfc
; /* 4 bits version, top 4 bits tclass */
117 uint8_t ip6_un3_ecn
; /* 2 bits ECN, top 6 bits payload length */
120 struct in6_address ip6_src
; /* source address */
121 struct in6_address ip6_dst
; /* destination address */
124 typedef struct ip6_pseudo_header
{
125 struct in6_address ip6_src
;
126 struct in6_address ip6_dst
;
133 uint8_t ip6r_nxt
; /* next header */
134 uint8_t ip6r_len
; /* length in units of 8 octets */
137 struct ip6_ext_hdr_routing
{
145 struct ip6_option_hdr
{
146 #define IP6_OPT_PAD1 (0x00)
147 #define IP6_OPT_HOME (0xC9)
153 uint16_t uh_sport
; /* source port */
154 uint16_t uh_dport
; /* destination port */
155 uint16_t uh_ulen
; /* udp length */
156 uint16_t uh_sum
; /* udp checksum */
160 u_short th_sport
; /* source port */
161 u_short th_dport
; /* destination port */
162 uint32_t th_seq
; /* sequence number */
163 uint32_t th_ack
; /* acknowledgment number */
165 u_char th_off
: 4, /* data offset */
166 th_x2
:4; /* (unused) */
168 u_char th_x2
: 4, /* (unused) */
169 th_off
:4; /* data offset */
172 #define TH_ELN 0x1 /* explicit loss notification */
173 #define TH_ECN 0x2 /* explicit congestion notification */
174 #define TH_FS 0x4 /* fast start */
185 u_short th_win
; /* window */
186 u_short th_sum
; /* checksum */
187 u_short th_urp
; /* urgent pointer */
190 #define ip6_nxt ip6_ctlun.ip6_un1.ip6_un1_nxt
191 #define ip6_ecn_acc ip6_ctlun.ip6_un3.ip6_un3_ecn
192 #define ip6_plen ip6_ctlun.ip6_un1.ip6_un1_plen
194 #define PKT_GET_ETH_HDR(p) \
195 ((struct eth_header *)(p))
196 #define PKT_GET_VLAN_HDR(p) \
197 ((struct vlan_header *) (((uint8_t *)(p)) + sizeof(struct eth_header)))
198 #define PKT_GET_DVLAN_HDR(p) \
199 (PKT_GET_VLAN_HDR(p) + 1)
200 #define PKT_GET_IP_HDR(p) \
201 ((struct ip_header *)(((uint8_t *)(p)) + eth_get_l2_hdr_length(p)))
202 #define IP_HDR_GET_LEN(p) \
203 ((ldub_p(p + offsetof(struct ip_header, ip_ver_len)) & 0x0F) << 2)
204 #define IP_HDR_GET_P(p) \
205 (ldub_p(p + offsetof(struct ip_header, ip_p)))
206 #define PKT_GET_IP_HDR_LEN(p) \
207 (IP_HDR_GET_LEN(PKT_GET_IP_HDR(p)))
208 #define PKT_GET_IP6_HDR(p) \
209 ((struct ip6_header *) (((uint8_t *)(p)) + eth_get_l2_hdr_length(p)))
210 #define IP_HEADER_VERSION(ip) \
211 (((ip)->ip_ver_len >> 4) & 0xf)
212 #define IP4_IS_FRAGMENT(ip) \
213 ((be16_to_cpu((ip)->ip_off) & (IP_OFFMASK | IP_MF)) != 0)
215 #define ETH_P_IP (0x0800) /* Internet Protocol packet */
216 #define ETH_P_ARP (0x0806) /* Address Resolution packet */
217 #define ETH_P_IPV6 (0x86dd)
218 #define ETH_P_VLAN (0x8100)
219 #define ETH_P_DVLAN (0x88a8)
220 #define ETH_P_NCSI (0x88f8)
221 #define ETH_P_UNKNOWN (0xffff)
222 #define VLAN_VID_MASK 0x0fff
223 #define IP_HEADER_VERSION_4 (4)
224 #define IP_HEADER_VERSION_6 (6)
225 #define IP_PROTO_TCP (6)
226 #define IP_PROTO_UDP (17)
227 #define IP_PROTO_SCTP (132)
228 #define IPTOS_ECN_MASK 0x03
229 #define IPTOS_ECN(x) ((x) & IPTOS_ECN_MASK)
230 #define IPTOS_ECN_CE 0x03
231 #define IP6_ECN_MASK 0xC0
232 #define IP6_ECN(x) ((x) & IP6_ECN_MASK)
233 #define IP6_ECN_CE 0xC0
234 #define IP4_DONT_FRAGMENT_FLAG (1 << 14)
236 #define IS_SPECIAL_VLAN_ID(x) \
237 (((x) == 0) || ((x) == 0xFFF))
239 #define ETH_MAX_L2_HDR_LEN \
240 (sizeof(struct eth_header) + 2 * sizeof(struct vlan_header))
242 #define ETH_MAX_IP4_HDR_LEN (60)
243 #define ETH_MAX_IP_DGRAM_LEN (0xFFFF)
245 #define IP_FRAG_UNIT_SIZE (8)
246 #define IP_FRAG_ALIGN_SIZE(x) ((x) & ~0x7)
247 #define IP_RF 0x8000 /* reserved fragment flag */
248 #define IP_DF 0x4000 /* don't fragment flag */
249 #define IP_MF 0x2000 /* more fragments flag */
250 #define IP_OFFMASK 0x1fff /* mask for fragmenting bits */
252 #define IP6_EXT_GRANULARITY (8) /* Size granularity for
253 IPv6 extension headers */
255 /* IP6 extension header types */
256 #define IP6_HOP_BY_HOP (0)
257 #define IP6_ROUTING (43)
258 #define IP6_FRAGMENT (44)
260 #define IP6_AUTHENTICATION (51)
261 #define IP6_NONE (59)
262 #define IP6_DESTINATON (60)
263 #define IP6_MOBILITY (135)
265 static inline int is_multicast_ether_addr(const uint8_t *addr
)
267 return 0x01 & addr
[0];
270 static inline int is_broadcast_ether_addr(const uint8_t *addr
)
272 return (addr
[0] & addr
[1] & addr
[2] & addr
[3] & addr
[4] & addr
[5]) == 0xff;
275 static inline int is_unicast_ether_addr(const uint8_t *addr
)
277 return !is_multicast_ether_addr(addr
);
281 ETH_PKT_UCAST
= 0xAABBCC00,
286 static inline eth_pkt_types_e
287 get_eth_packet_type(const struct eth_header
*ehdr
)
289 if (is_broadcast_ether_addr(ehdr
->h_dest
)) {
290 return ETH_PKT_BCAST
;
291 } else if (is_multicast_ether_addr(ehdr
->h_dest
)) {
292 return ETH_PKT_MCAST
;
293 } else { /* unicast */
294 return ETH_PKT_UCAST
;
298 static inline uint32_t
299 eth_get_l2_hdr_length(const void *p
)
301 uint16_t proto
= be16_to_cpu(PKT_GET_ETH_HDR(p
)->h_proto
);
302 struct vlan_header
*hvlan
= PKT_GET_VLAN_HDR(p
);
305 return sizeof(struct eth_header
) + sizeof(struct vlan_header
);
307 if (be16_to_cpu(hvlan
->h_proto
) == ETH_P_VLAN
) {
308 return sizeof(struct eth_header
) + 2 * sizeof(struct vlan_header
);
310 return sizeof(struct eth_header
) + sizeof(struct vlan_header
);
313 return sizeof(struct eth_header
);
317 static inline uint32_t
318 eth_get_l2_hdr_length_iov(const struct iovec
*iov
, size_t iovcnt
, size_t iovoff
)
320 uint8_t p
[sizeof(struct eth_header
) + sizeof(struct vlan_header
)];
321 size_t copied
= iov_to_buf(iov
, iovcnt
, iovoff
, p
, ARRAY_SIZE(p
));
323 if (copied
< ARRAY_SIZE(p
)) {
327 return eth_get_l2_hdr_length(p
);
330 static inline uint16_t
331 eth_get_pkt_tci(const void *p
)
333 uint16_t proto
= be16_to_cpu(PKT_GET_ETH_HDR(p
)->h_proto
);
334 struct vlan_header
*hvlan
= PKT_GET_VLAN_HDR(p
);
338 return be16_to_cpu(hvlan
->h_tci
);
345 eth_strip_vlan(const struct iovec
*iov
, int iovcnt
, size_t iovoff
,
347 uint16_t *payload_offset
, uint16_t *tci
);
350 eth_strip_vlan_ex(const struct iovec
*iov
, int iovcnt
, size_t iovoff
, int index
,
351 uint16_t vet
, uint16_t vet_ext
, void *new_ehdr_buf
,
352 uint16_t *payload_offset
, uint16_t *tci
);
355 eth_get_l3_proto(const struct iovec
*l2hdr_iov
, int iovcnt
, size_t l2hdr_len
);
357 void eth_setup_vlan_headers(struct eth_header
*ehdr
, size_t *ehdr_size
,
358 uint16_t vlan_tag
, uint16_t vlan_ethtype
);
361 uint8_t eth_get_gso_type(uint16_t l3_proto
, uint8_t *l3_hdr
, uint8_t l4proto
);
363 typedef struct eth_ip6_hdr_info_st
{
366 struct ip6_header ip6_hdr
;
368 bool rss_ex_src_valid
;
369 struct in6_address rss_ex_src
;
370 bool rss_ex_dst_valid
;
371 struct in6_address rss_ex_dst
;
375 typedef struct eth_ip4_hdr_info_st
{
376 struct ip_header ip4_hdr
;
380 typedef enum EthL4HdrProto
{
381 ETH_L4_HDR_PROTO_INVALID
,
382 ETH_L4_HDR_PROTO_TCP
,
383 ETH_L4_HDR_PROTO_UDP
,
384 ETH_L4_HDR_PROTO_SCTP
387 typedef struct eth_l4_hdr_info_st
{
389 struct tcp_header tcp
;
390 struct udp_header udp
;
397 void eth_get_protocols(const struct iovec
*iov
, size_t iovcnt
, size_t iovoff
,
398 bool *hasip4
, bool *hasip6
,
402 eth_ip6_hdr_info
*ip6hdr_info
,
403 eth_ip4_hdr_info
*ip4hdr_info
,
404 eth_l4_hdr_info
*l4hdr_info
);
407 eth_fix_ip4_checksum(void *l3hdr
, size_t l3hdr_len
);
410 eth_calc_ip4_pseudo_hdr_csum(struct ip_header
*iphdr
,
415 eth_calc_ip6_pseudo_hdr_csum(struct ip6_header
*iphdr
,
421 eth_parse_ipv6_hdr(const struct iovec
*pkt
, int pkt_frags
,
422 size_t ip6hdr_off
, eth_ip6_hdr_info
*info
);
425 * eth_pad_short_frame - pad a short frame to the minimum Ethernet frame length
427 * If the Ethernet frame size is shorter than 60 bytes, it will be padded to
428 * 60 bytes at the address @padded_pkt.
430 * @padded_pkt: buffer address to hold the padded frame
431 * @padded_buflen: pointer holding length of @padded_pkt. If the frame is
432 * padded, the length will be updated to the padded one.
433 * @pkt: address to hold the original Ethernet frame
434 * @pkt_size: size of the original Ethernet frame
435 * @return true if the frame is padded, otherwise false
437 bool eth_pad_short_frame(uint8_t *padded_pkt
, size_t *padded_buflen
,
438 const void *pkt
, size_t pkt_size
);