net/eth: Use void pointers
[qemu/armbru.git] / include / net / eth.h
blob95ff24d6b85e943ac454d68d297257bd477c7a8f
1 /*
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)
16 * Authors:
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.
26 #ifndef QEMU_ETH_H
27 #define QEMU_ETH_H
29 #include "qemu/bswap.h"
30 #include "qemu/iov.h"
32 #define ETH_ALEN 6
33 #define ETH_HLEN 14
34 #define ETH_ZLEN 60 /* Min. octets in frame without FCS */
35 #define ETH_FCS_LEN 4
36 #define ETH_MTU 1500
38 struct eth_header {
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 */
44 struct vlan_header {
45 uint16_t h_tci; /* priority and VLAN ID */
46 uint16_t h_proto; /* encapsulated protocol */
49 struct ip_header {
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 */
71 } tcp_header;
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 */
88 } udp_header;
90 typedef struct ip_pseudo_header {
91 uint32_t ip_src;
92 uint32_t ip_dst;
93 uint8_t zeros;
94 uint8_t ip_proto;
95 uint16_t ip_payload;
96 } ip_pseudo_header;
98 /* IPv6 address */
99 struct in6_address {
100 union {
101 uint8_t __u6_addr8[16];
102 } __in6_u;
105 struct ip6_header {
106 union {
107 struct ip6_hdrctl {
108 uint32_t ip6_un1_flow; /* 4 bits version, 8 bits TC,
109 20 bits flow-ID */
110 uint16_t ip6_un1_plen; /* payload length */
111 uint8_t ip6_un1_nxt; /* next header */
112 uint8_t ip6_un1_hlim; /* hop limit */
113 } ip6_un1;
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 */
118 } ip6_un3;
119 } ip6_ctlun;
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;
127 uint32_t len;
128 uint8_t zero[3];
129 uint8_t next_hdr;
130 } ip6_pseudo_header;
132 struct ip6_ext_hdr {
133 uint8_t ip6r_nxt; /* next header */
134 uint8_t ip6r_len; /* length in units of 8 octets */
137 struct ip6_ext_hdr_routing {
138 uint8_t nxt;
139 uint8_t len;
140 uint8_t rtype;
141 uint8_t segleft;
142 uint8_t rsvd[4];
145 struct ip6_option_hdr {
146 #define IP6_OPT_PAD1 (0x00)
147 #define IP6_OPT_HOME (0xC9)
148 uint8_t type;
149 uint8_t len;
152 struct udp_hdr {
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 */
159 struct tcp_hdr {
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 */
164 #if HOST_BIG_ENDIAN
165 u_char th_off : 4, /* data offset */
166 th_x2:4; /* (unused) */
167 #else
168 u_char th_x2 : 4, /* (unused) */
169 th_off:4; /* data offset */
170 #endif
172 #define TH_ELN 0x1 /* explicit loss notification */
173 #define TH_ECN 0x2 /* explicit congestion notification */
174 #define TH_FS 0x4 /* fast start */
176 u_char th_flags;
177 #define TH_FIN 0x01
178 #define TH_SYN 0x02
179 #define TH_RST 0x04
180 #define TH_PUSH 0x08
181 #define TH_ACK 0x10
182 #define TH_URG 0x20
183 #define TH_ECE 0x40
184 #define TH_CWR 0x80
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 IPTOS_ECN_MASK 0x03
228 #define IPTOS_ECN(x) ((x) & IPTOS_ECN_MASK)
229 #define IPTOS_ECN_CE 0x03
230 #define IP6_ECN_MASK 0xC0
231 #define IP6_ECN(x) ((x) & IP6_ECN_MASK)
232 #define IP6_ECN_CE 0xC0
233 #define IP4_DONT_FRAGMENT_FLAG (1 << 14)
235 #define IS_SPECIAL_VLAN_ID(x) \
236 (((x) == 0) || ((x) == 0xFFF))
238 #define ETH_MAX_L2_HDR_LEN \
239 (sizeof(struct eth_header) + 2 * sizeof(struct vlan_header))
241 #define ETH_MAX_IP4_HDR_LEN (60)
242 #define ETH_MAX_IP_DGRAM_LEN (0xFFFF)
244 #define IP_FRAG_UNIT_SIZE (8)
245 #define IP_FRAG_ALIGN_SIZE(x) ((x) & ~0x7)
246 #define IP_RF 0x8000 /* reserved fragment flag */
247 #define IP_DF 0x4000 /* don't fragment flag */
248 #define IP_MF 0x2000 /* more fragments flag */
249 #define IP_OFFMASK 0x1fff /* mask for fragmenting bits */
251 #define IP6_EXT_GRANULARITY (8) /* Size granularity for
252 IPv6 extension headers */
254 /* IP6 extension header types */
255 #define IP6_HOP_BY_HOP (0)
256 #define IP6_ROUTING (43)
257 #define IP6_FRAGMENT (44)
258 #define IP6_ESP (50)
259 #define IP6_AUTHENTICATION (51)
260 #define IP6_NONE (59)
261 #define IP6_DESTINATON (60)
262 #define IP6_MOBILITY (135)
264 static inline int is_multicast_ether_addr(const uint8_t *addr)
266 return 0x01 & addr[0];
269 static inline int is_broadcast_ether_addr(const uint8_t *addr)
271 return (addr[0] & addr[1] & addr[2] & addr[3] & addr[4] & addr[5]) == 0xff;
274 static inline int is_unicast_ether_addr(const uint8_t *addr)
276 return !is_multicast_ether_addr(addr);
279 typedef enum {
280 ETH_PKT_UCAST = 0xAABBCC00,
281 ETH_PKT_BCAST,
282 ETH_PKT_MCAST
283 } eth_pkt_types_e;
285 static inline eth_pkt_types_e
286 get_eth_packet_type(const struct eth_header *ehdr)
288 if (is_broadcast_ether_addr(ehdr->h_dest)) {
289 return ETH_PKT_BCAST;
290 } else if (is_multicast_ether_addr(ehdr->h_dest)) {
291 return ETH_PKT_MCAST;
292 } else { /* unicast */
293 return ETH_PKT_UCAST;
297 static inline uint32_t
298 eth_get_l2_hdr_length(const void *p)
300 uint16_t proto = be16_to_cpu(PKT_GET_ETH_HDR(p)->h_proto);
301 struct vlan_header *hvlan = PKT_GET_VLAN_HDR(p);
302 switch (proto) {
303 case ETH_P_VLAN:
304 return sizeof(struct eth_header) + sizeof(struct vlan_header);
305 case ETH_P_DVLAN:
306 if (be16_to_cpu(hvlan->h_proto) == ETH_P_VLAN) {
307 return sizeof(struct eth_header) + 2 * sizeof(struct vlan_header);
308 } else {
309 return sizeof(struct eth_header) + sizeof(struct vlan_header);
311 default:
312 return sizeof(struct eth_header);
316 static inline uint32_t
317 eth_get_l2_hdr_length_iov(const struct iovec *iov, size_t iovcnt, size_t iovoff)
319 uint8_t p[sizeof(struct eth_header) + sizeof(struct vlan_header)];
320 size_t copied = iov_to_buf(iov, iovcnt, iovoff, p, ARRAY_SIZE(p));
322 if (copied < ARRAY_SIZE(p)) {
323 return copied;
326 return eth_get_l2_hdr_length(p);
329 static inline uint16_t
330 eth_get_pkt_tci(const void *p)
332 uint16_t proto = be16_to_cpu(PKT_GET_ETH_HDR(p)->h_proto);
333 struct vlan_header *hvlan = PKT_GET_VLAN_HDR(p);
334 switch (proto) {
335 case ETH_P_VLAN:
336 case ETH_P_DVLAN:
337 return be16_to_cpu(hvlan->h_tci);
338 default:
339 return 0;
343 size_t
344 eth_strip_vlan(const struct iovec *iov, int iovcnt, size_t iovoff,
345 void *new_ehdr_buf,
346 uint16_t *payload_offset, uint16_t *tci);
348 size_t
349 eth_strip_vlan_ex(const struct iovec *iov, int iovcnt, size_t iovoff,
350 uint16_t vet, void *new_ehdr_buf,
351 uint16_t *payload_offset, uint16_t *tci);
353 uint16_t
354 eth_get_l3_proto(const struct iovec *l2hdr_iov, int iovcnt, size_t l2hdr_len);
356 void eth_setup_vlan_headers(struct eth_header *ehdr, uint16_t vlan_tag,
357 uint16_t vlan_ethtype, bool *is_new);
360 uint8_t eth_get_gso_type(uint16_t l3_proto, uint8_t *l3_hdr, uint8_t l4proto);
362 typedef struct eth_ip6_hdr_info_st {
363 uint8_t l4proto;
364 size_t full_hdr_len;
365 struct ip6_header ip6_hdr;
366 bool has_ext_hdrs;
367 bool rss_ex_src_valid;
368 struct in6_address rss_ex_src;
369 bool rss_ex_dst_valid;
370 struct in6_address rss_ex_dst;
371 bool fragment;
372 } eth_ip6_hdr_info;
374 typedef struct eth_ip4_hdr_info_st {
375 struct ip_header ip4_hdr;
376 bool fragment;
377 } eth_ip4_hdr_info;
379 typedef enum EthL4HdrProto {
380 ETH_L4_HDR_PROTO_INVALID,
381 ETH_L4_HDR_PROTO_TCP,
382 ETH_L4_HDR_PROTO_UDP
383 } EthL4HdrProto;
385 typedef struct eth_l4_hdr_info_st {
386 union {
387 struct tcp_header tcp;
388 struct udp_header udp;
389 } hdr;
391 EthL4HdrProto proto;
392 bool has_tcp_data;
393 } eth_l4_hdr_info;
395 void eth_get_protocols(const struct iovec *iov, size_t iovcnt, size_t iovoff,
396 bool *hasip4, bool *hasip6,
397 size_t *l3hdr_off,
398 size_t *l4hdr_off,
399 size_t *l5hdr_off,
400 eth_ip6_hdr_info *ip6hdr_info,
401 eth_ip4_hdr_info *ip4hdr_info,
402 eth_l4_hdr_info *l4hdr_info);
404 void
405 eth_fix_ip4_checksum(void *l3hdr, size_t l3hdr_len);
407 uint32_t
408 eth_calc_ip4_pseudo_hdr_csum(struct ip_header *iphdr,
409 uint16_t csl,
410 uint32_t *cso);
412 uint32_t
413 eth_calc_ip6_pseudo_hdr_csum(struct ip6_header *iphdr,
414 uint16_t csl,
415 uint8_t l4_proto,
416 uint32_t *cso);
418 bool
419 eth_parse_ipv6_hdr(const struct iovec *pkt, int pkt_frags,
420 size_t ip6hdr_off, eth_ip6_hdr_info *info);
423 * eth_pad_short_frame - pad a short frame to the minimum Ethernet frame length
425 * If the Ethernet frame size is shorter than 60 bytes, it will be padded to
426 * 60 bytes at the address @padded_pkt.
428 * @padded_pkt: buffer address to hold the padded frame
429 * @padded_buflen: pointer holding length of @padded_pkt. If the frame is
430 * padded, the length will be updated to the padded one.
431 * @pkt: address to hold the original Ethernet frame
432 * @pkt_size: size of the original Ethernet frame
433 * @return true if the frame is padded, otherwise false
435 bool eth_pad_short_frame(uint8_t *padded_pkt, size_t *padded_buflen,
436 const void *pkt, size_t pkt_size);
438 #endif