rtl8139: Move more TCP definitions to common header
[qemu/ar7.git] / include / net / eth.h
blob5a32259938e027be04a19f8fc05c83a735bdb800
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
35 struct eth_header {
36 uint8_t h_dest[ETH_ALEN]; /* destination eth addr */
37 uint8_t h_source[ETH_ALEN]; /* source ether addr */
38 uint16_t h_proto; /* packet type ID field */
41 struct vlan_header {
42 uint16_t h_tci; /* priority and VLAN ID */
43 uint16_t h_proto; /* encapsulated protocol */
46 struct ip_header {
47 uint8_t ip_ver_len; /* version and header length */
48 uint8_t ip_tos; /* type of service */
49 uint16_t ip_len; /* total length */
50 uint16_t ip_id; /* identification */
51 uint16_t ip_off; /* fragment offset field */
52 uint8_t ip_ttl; /* time to live */
53 uint8_t ip_p; /* protocol */
54 uint16_t ip_sum; /* checksum */
55 uint32_t ip_src, ip_dst; /* source and destination address */
58 typedef struct tcp_header {
59 uint16_t th_sport; /* source port */
60 uint16_t th_dport; /* destination port */
61 uint32_t th_seq; /* sequence number */
62 uint32_t th_ack; /* acknowledgment number */
63 uint16_t th_offset_flags; /* data offset, reserved 6 bits, */
64 /* TCP protocol flags */
65 uint16_t th_win; /* window */
66 uint16_t th_sum; /* checksum */
67 uint16_t th_urp; /* urgent pointer */
68 } tcp_header;
70 #define TCP_FLAGS_ONLY(flags) ((flags) & 0x3f)
72 #define TCP_HEADER_FLAGS(tcp) \
73 TCP_FLAGS_ONLY(be16_to_cpu((tcp)->th_offset_flags))
75 #define TCP_HEADER_DATA_OFFSET(tcp) \
76 (((be16_to_cpu((tcp)->th_offset_flags) >> 12) & 0xf) << 2)
78 typedef struct udp_header {
79 uint16_t uh_sport; /* source port */
80 uint16_t uh_dport; /* destination port */
81 uint16_t uh_ulen; /* udp length */
82 uint16_t uh_sum; /* udp checksum */
83 } udp_header;
85 typedef struct ip_pseudo_header {
86 uint32_t ip_src;
87 uint32_t ip_dst;
88 uint8_t zeros;
89 uint8_t ip_proto;
90 uint16_t ip_payload;
91 } ip_pseudo_header;
93 /* IPv6 address */
94 struct in6_address {
95 union {
96 uint8_t __u6_addr8[16];
97 } __in6_u;
100 struct ip6_header {
101 union {
102 struct ip6_hdrctl {
103 uint32_t ip6_un1_flow; /* 4 bits version, 8 bits TC,
104 20 bits flow-ID */
105 uint16_t ip6_un1_plen; /* payload length */
106 uint8_t ip6_un1_nxt; /* next header */
107 uint8_t ip6_un1_hlim; /* hop limit */
108 } ip6_un1;
109 uint8_t ip6_un2_vfc; /* 4 bits version, top 4 bits tclass */
110 struct ip6_ecn_access {
111 uint8_t ip6_un3_vfc; /* 4 bits version, top 4 bits tclass */
112 uint8_t ip6_un3_ecn; /* 2 bits ECN, top 6 bits payload length */
113 } ip6_un3;
114 } ip6_ctlun;
115 struct in6_address ip6_src; /* source address */
116 struct in6_address ip6_dst; /* destination address */
119 struct ip6_ext_hdr {
120 uint8_t ip6r_nxt; /* next header */
121 uint8_t ip6r_len; /* length in units of 8 octets */
124 struct udp_hdr {
125 uint16_t uh_sport; /* source port */
126 uint16_t uh_dport; /* destination port */
127 uint16_t uh_ulen; /* udp length */
128 uint16_t uh_sum; /* udp checksum */
131 struct tcp_hdr {
132 u_short th_sport; /* source port */
133 u_short th_dport; /* destination port */
134 uint32_t th_seq; /* sequence number */
135 uint32_t th_ack; /* acknowledgment number */
136 #ifdef HOST_WORDS_BIGENDIAN
137 u_char th_off : 4, /* data offset */
138 th_x2:4; /* (unused) */
139 #else
140 u_char th_x2 : 4, /* (unused) */
141 th_off:4; /* data offset */
142 #endif
144 #define TH_ELN 0x1 /* explicit loss notification */
145 #define TH_ECN 0x2 /* explicit congestion notification */
146 #define TH_FS 0x4 /* fast start */
148 u_char th_flags;
149 #define TH_FIN 0x01
150 #define TH_SYN 0x02
151 #define TH_RST 0x04
152 #define TH_PUSH 0x08
153 #define TH_ACK 0x10
154 #define TH_URG 0x20
155 u_short th_win; /* window */
156 u_short th_sum; /* checksum */
157 u_short th_urp; /* urgent pointer */
160 #define ip6_nxt ip6_ctlun.ip6_un1.ip6_un1_nxt
161 #define ip6_ecn_acc ip6_ctlun.ip6_un3.ip6_un3_ecn
163 #define PKT_GET_ETH_HDR(p) \
164 ((struct eth_header *)(p))
165 #define PKT_GET_VLAN_HDR(p) \
166 ((struct vlan_header *) (((uint8_t *)(p)) + sizeof(struct eth_header)))
167 #define PKT_GET_DVLAN_HDR(p) \
168 (PKT_GET_VLAN_HDR(p) + 1)
169 #define PKT_GET_IP_HDR(p) \
170 ((struct ip_header *)(((uint8_t *)(p)) + eth_get_l2_hdr_length(p)))
171 #define IP_HDR_GET_LEN(p) \
172 ((((struct ip_header *)p)->ip_ver_len & 0x0F) << 2)
173 #define PKT_GET_IP_HDR_LEN(p) \
174 (IP_HDR_GET_LEN(PKT_GET_IP_HDR(p)))
175 #define PKT_GET_IP6_HDR(p) \
176 ((struct ip6_header *) (((uint8_t *)(p)) + eth_get_l2_hdr_length(p)))
177 #define IP_HEADER_VERSION(ip) \
178 ((ip->ip_ver_len >> 4)&0xf)
180 #define ETH_P_IP (0x0800) /* Internet Protocol packet */
181 #define ETH_P_ARP (0x0806) /* Address Resolution packet */
182 #define ETH_P_IPV6 (0x86dd)
183 #define ETH_P_VLAN (0x8100)
184 #define ETH_P_DVLAN (0x88a8)
185 #define VLAN_VID_MASK 0x0fff
186 #define IP_HEADER_VERSION_4 (4)
187 #define IP_HEADER_VERSION_6 (6)
188 #define IP_PROTO_TCP (6)
189 #define IP_PROTO_UDP (17)
190 #define IPTOS_ECN_MASK 0x03
191 #define IPTOS_ECN(x) ((x) & IPTOS_ECN_MASK)
192 #define IPTOS_ECN_CE 0x03
193 #define IP6_ECN_MASK 0xC0
194 #define IP6_ECN(x) ((x) & IP6_ECN_MASK)
195 #define IP6_ECN_CE 0xC0
196 #define IP4_DONT_FRAGMENT_FLAG (1 << 14)
198 #define IS_SPECIAL_VLAN_ID(x) \
199 (((x) == 0) || ((x) == 0xFFF))
201 #define ETH_MAX_L2_HDR_LEN \
202 (sizeof(struct eth_header) + 2 * sizeof(struct vlan_header))
204 #define ETH_MAX_IP4_HDR_LEN (60)
205 #define ETH_MAX_IP_DGRAM_LEN (0xFFFF)
207 #define IP_FRAG_UNIT_SIZE (8)
208 #define IP_FRAG_ALIGN_SIZE(x) ((x) & ~0x7)
209 #define IP_RF 0x8000 /* reserved fragment flag */
210 #define IP_DF 0x4000 /* don't fragment flag */
211 #define IP_MF 0x2000 /* more fragments flag */
212 #define IP_OFFMASK 0x1fff /* mask for fragmenting bits */
214 #define IP6_EXT_GRANULARITY (8) /* Size granularity for
215 IPv6 extension headers */
217 /* IP6 extension header types */
218 #define IP6_HOP_BY_HOP (0)
219 #define IP6_ROUTING (43)
220 #define IP6_FRAGMENT (44)
221 #define IP6_ESP (50)
222 #define IP6_AUTHENTICATION (51)
223 #define IP6_NONE (59)
224 #define IP6_DESTINATON (60)
225 #define IP6_MOBILITY (135)
227 static inline int is_multicast_ether_addr(const uint8_t *addr)
229 return 0x01 & addr[0];
232 static inline int is_broadcast_ether_addr(const uint8_t *addr)
234 return (addr[0] & addr[1] & addr[2] & addr[3] & addr[4] & addr[5]) == 0xff;
237 static inline int is_unicast_ether_addr(const uint8_t *addr)
239 return !is_multicast_ether_addr(addr);
242 typedef enum {
243 ETH_PKT_UCAST = 0xAABBCC00,
244 ETH_PKT_BCAST,
245 ETH_PKT_MCAST
246 } eth_pkt_types_e;
248 static inline eth_pkt_types_e
249 get_eth_packet_type(const struct eth_header *ehdr)
251 if (is_broadcast_ether_addr(ehdr->h_dest)) {
252 return ETH_PKT_BCAST;
253 } else if (is_multicast_ether_addr(ehdr->h_dest)) {
254 return ETH_PKT_MCAST;
255 } else { /* unicast */
256 return ETH_PKT_UCAST;
260 static inline uint32_t
261 eth_get_l2_hdr_length(const void *p)
263 uint16_t proto = be16_to_cpu(PKT_GET_ETH_HDR(p)->h_proto);
264 struct vlan_header *hvlan = PKT_GET_VLAN_HDR(p);
265 switch (proto) {
266 case ETH_P_VLAN:
267 return sizeof(struct eth_header) + sizeof(struct vlan_header);
268 case ETH_P_DVLAN:
269 if (hvlan->h_proto == ETH_P_VLAN) {
270 return sizeof(struct eth_header) + 2 * sizeof(struct vlan_header);
271 } else {
272 return sizeof(struct eth_header) + sizeof(struct vlan_header);
274 default:
275 return sizeof(struct eth_header);
279 static inline uint16_t
280 eth_get_pkt_tci(const void *p)
282 uint16_t proto = be16_to_cpu(PKT_GET_ETH_HDR(p)->h_proto);
283 struct vlan_header *hvlan = PKT_GET_VLAN_HDR(p);
284 switch (proto) {
285 case ETH_P_VLAN:
286 case ETH_P_DVLAN:
287 return be16_to_cpu(hvlan->h_tci);
288 default:
289 return 0;
293 static inline bool
294 eth_strip_vlan(const void *p, uint8_t *new_ehdr_buf,
295 uint16_t *payload_offset, uint16_t *tci)
297 uint16_t proto = be16_to_cpu(PKT_GET_ETH_HDR(p)->h_proto);
298 struct vlan_header *hvlan = PKT_GET_VLAN_HDR(p);
299 struct eth_header *new_ehdr = (struct eth_header *) new_ehdr_buf;
301 switch (proto) {
302 case ETH_P_VLAN:
303 case ETH_P_DVLAN:
304 memcpy(new_ehdr->h_source, PKT_GET_ETH_HDR(p)->h_source, ETH_ALEN);
305 memcpy(new_ehdr->h_dest, PKT_GET_ETH_HDR(p)->h_dest, ETH_ALEN);
306 new_ehdr->h_proto = hvlan->h_proto;
307 *tci = be16_to_cpu(hvlan->h_tci);
308 *payload_offset =
309 sizeof(struct eth_header) + sizeof(struct vlan_header);
310 if (be16_to_cpu(new_ehdr->h_proto) == ETH_P_VLAN) {
311 memcpy(PKT_GET_VLAN_HDR(new_ehdr),
312 PKT_GET_DVLAN_HDR(p),
313 sizeof(struct vlan_header));
314 *payload_offset += sizeof(struct vlan_header);
316 return true;
317 default:
318 return false;
322 static inline uint16_t
323 eth_get_l3_proto(const void *l2hdr, size_t l2hdr_len)
325 uint8_t *proto_ptr = (uint8_t *) l2hdr + l2hdr_len - sizeof(uint16_t);
326 return be16_to_cpup((uint16_t *)proto_ptr);
329 void eth_setup_vlan_headers(struct eth_header *ehdr, uint16_t vlan_tag,
330 bool *is_new);
332 uint8_t eth_get_gso_type(uint16_t l3_proto, uint8_t *l3_hdr, uint8_t l4proto);
334 void eth_get_protocols(const uint8_t *headers,
335 uint32_t hdr_length,
336 bool *isip4, bool *isip6,
337 bool *isudp, bool *istcp);
339 void eth_setup_ip4_fragmentation(const void *l2hdr, size_t l2hdr_len,
340 void *l3hdr, size_t l3hdr_len,
341 size_t l3payload_len,
342 size_t frag_offset, bool more_frags);
344 void
345 eth_fix_ip4_checksum(void *l3hdr, size_t l3hdr_len);
347 uint32_t
348 eth_calc_pseudo_hdr_csum(struct ip_header *iphdr, uint16_t csl);
350 bool
351 eth_parse_ipv6_hdr(struct iovec *pkt, int pkt_frags,
352 size_t ip6hdr_off, uint8_t *l4proto,
353 size_t *full_hdr_len);
355 #endif