1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26#ifndef QEMU_ETH_H
27#define QEMU_ETH_H
28
29#include "qemu/bswap.h"
30#include "qemu/iov.h"
31
32#define ETH_ALEN 6
33#define ETH_HLEN 14
34
35struct eth_header {
36 uint8_t h_dest[ETH_ALEN];
37 uint8_t h_source[ETH_ALEN];
38 uint16_t h_proto;
39};
40
41struct vlan_header {
42 uint16_t h_tci;
43 uint16_t h_proto;
44};
45
46struct ip_header {
47 uint8_t ip_ver_len;
48 uint8_t ip_tos;
49 uint16_t ip_len;
50 uint16_t ip_id;
51 uint16_t ip_off;
52 uint8_t ip_ttl;
53 uint8_t ip_p;
54 uint16_t ip_sum;
55 uint32_t ip_src, ip_dst;
56};
57
58typedef struct tcp_header {
59 uint16_t th_sport;
60 uint16_t th_dport;
61 uint32_t th_seq;
62 uint32_t th_ack;
63 uint16_t th_offset_flags;
64
65 uint16_t th_win;
66 uint16_t th_sum;
67 uint16_t th_urp;
68} tcp_header;
69
70#define TCP_FLAGS_ONLY(flags) ((flags) & 0x3f)
71
72#define TCP_HEADER_FLAGS(tcp) \
73 TCP_FLAGS_ONLY(be16_to_cpu((tcp)->th_offset_flags))
74
75#define TCP_FLAG_ACK 0x10
76
77#define TCP_HEADER_DATA_OFFSET(tcp) \
78 (((be16_to_cpu((tcp)->th_offset_flags) >> 12) & 0xf) << 2)
79
80typedef struct udp_header {
81 uint16_t uh_sport;
82 uint16_t uh_dport;
83 uint16_t uh_ulen;
84 uint16_t uh_sum;
85} udp_header;
86
87typedef struct ip_pseudo_header {
88 uint32_t ip_src;
89 uint32_t ip_dst;
90 uint8_t zeros;
91 uint8_t ip_proto;
92 uint16_t ip_payload;
93} ip_pseudo_header;
94
95
96struct in6_address {
97 union {
98 uint8_t __u6_addr8[16];
99 } __in6_u;
100};
101
102struct ip6_header {
103 union {
104 struct ip6_hdrctl {
105 uint32_t ip6_un1_flow;
106
107 uint16_t ip6_un1_plen;
108 uint8_t ip6_un1_nxt;
109 uint8_t ip6_un1_hlim;
110 } ip6_un1;
111 uint8_t ip6_un2_vfc;
112 struct ip6_ecn_access {
113 uint8_t ip6_un3_vfc;
114 uint8_t ip6_un3_ecn;
115 } ip6_un3;
116 } ip6_ctlun;
117 struct in6_address ip6_src;
118 struct in6_address ip6_dst;
119};
120
121typedef struct ip6_pseudo_header {
122 struct in6_address ip6_src;
123 struct in6_address ip6_dst;
124 uint32_t len;
125 uint8_t zero[3];
126 uint8_t next_hdr;
127} ip6_pseudo_header;
128
129struct ip6_ext_hdr {
130 uint8_t ip6r_nxt;
131 uint8_t ip6r_len;
132};
133
134struct ip6_ext_hdr_routing {
135 uint8_t nxt;
136 uint8_t len;
137 uint8_t rtype;
138 uint8_t segleft;
139 uint8_t rsvd[4];
140};
141
142struct ip6_option_hdr {
143#define IP6_OPT_PAD1 (0x00)
144#define IP6_OPT_HOME (0xC9)
145 uint8_t type;
146 uint8_t len;
147};
148
149struct udp_hdr {
150 uint16_t uh_sport;
151 uint16_t uh_dport;
152 uint16_t uh_ulen;
153 uint16_t uh_sum;
154};
155
156struct tcp_hdr {
157 u_short th_sport;
158 u_short th_dport;
159 uint32_t th_seq;
160 uint32_t th_ack;
161#ifdef HOST_WORDS_BIGENDIAN
162 u_char th_off : 4,
163 th_x2:4;
164#else
165 u_char th_x2 : 4,
166 th_off:4;
167#endif
168
169#define TH_ELN 0x1
170#define TH_ECN 0x2
171#define TH_FS 0x4
172
173 u_char th_flags;
174#define TH_FIN 0x01
175#define TH_SYN 0x02
176#define TH_RST 0x04
177#define TH_PUSH 0x08
178#define TH_ACK 0x10
179#define TH_URG 0x20
180 u_short th_win;
181 u_short th_sum;
182 u_short th_urp;
183};
184
185#define ip6_nxt ip6_ctlun.ip6_un1.ip6_un1_nxt
186#define ip6_ecn_acc ip6_ctlun.ip6_un3.ip6_un3_ecn
187
188#define PKT_GET_ETH_HDR(p) \
189 ((struct eth_header *)(p))
190#define PKT_GET_VLAN_HDR(p) \
191 ((struct vlan_header *) (((uint8_t *)(p)) + sizeof(struct eth_header)))
192#define PKT_GET_DVLAN_HDR(p) \
193 (PKT_GET_VLAN_HDR(p) + 1)
194#define PKT_GET_IP_HDR(p) \
195 ((struct ip_header *)(((uint8_t *)(p)) + eth_get_l2_hdr_length(p)))
196#define IP_HDR_GET_LEN(p) \
197 ((((struct ip_header *)(p))->ip_ver_len & 0x0F) << 2)
198#define PKT_GET_IP_HDR_LEN(p) \
199 (IP_HDR_GET_LEN(PKT_GET_IP_HDR(p)))
200#define PKT_GET_IP6_HDR(p) \
201 ((struct ip6_header *) (((uint8_t *)(p)) + eth_get_l2_hdr_length(p)))
202#define IP_HEADER_VERSION(ip) \
203 (((ip)->ip_ver_len >> 4) & 0xf)
204#define IP4_IS_FRAGMENT(ip) \
205 ((be16_to_cpu((ip)->ip_off) & (IP_OFFMASK | IP_MF)) != 0)
206
207#define ETH_P_IP (0x0800)
208#define ETH_P_ARP (0x0806)
209#define ETH_P_IPV6 (0x86dd)
210#define ETH_P_VLAN (0x8100)
211#define ETH_P_DVLAN (0x88a8)
212#define ETH_P_NCSI (0x88f8)
213#define ETH_P_UNKNOWN (0xffff)
214#define VLAN_VID_MASK 0x0fff
215#define IP_HEADER_VERSION_4 (4)
216#define IP_HEADER_VERSION_6 (6)
217#define IP_PROTO_TCP (6)
218#define IP_PROTO_UDP (17)
219#define IPTOS_ECN_MASK 0x03
220#define IPTOS_ECN(x) ((x) & IPTOS_ECN_MASK)
221#define IPTOS_ECN_CE 0x03
222#define IP6_ECN_MASK 0xC0
223#define IP6_ECN(x) ((x) & IP6_ECN_MASK)
224#define IP6_ECN_CE 0xC0
225#define IP4_DONT_FRAGMENT_FLAG (1 << 14)
226
227#define IS_SPECIAL_VLAN_ID(x) \
228 (((x) == 0) || ((x) == 0xFFF))
229
230#define ETH_MAX_L2_HDR_LEN \
231 (sizeof(struct eth_header) + 2 * sizeof(struct vlan_header))
232
233#define ETH_MAX_IP4_HDR_LEN (60)
234#define ETH_MAX_IP_DGRAM_LEN (0xFFFF)
235
236#define IP_FRAG_UNIT_SIZE (8)
237#define IP_FRAG_ALIGN_SIZE(x) ((x) & ~0x7)
238#define IP_RF 0x8000
239#define IP_DF 0x4000
240#define IP_MF 0x2000
241#define IP_OFFMASK 0x1fff
242
243#define IP6_EXT_GRANULARITY (8)
244
245
246
247#define IP6_HOP_BY_HOP (0)
248#define IP6_ROUTING (43)
249#define IP6_FRAGMENT (44)
250#define IP6_ESP (50)
251#define IP6_AUTHENTICATION (51)
252#define IP6_NONE (59)
253#define IP6_DESTINATON (60)
254#define IP6_MOBILITY (135)
255
256static inline int is_multicast_ether_addr(const uint8_t *addr)
257{
258 return 0x01 & addr[0];
259}
260
261static inline int is_broadcast_ether_addr(const uint8_t *addr)
262{
263 return (addr[0] & addr[1] & addr[2] & addr[3] & addr[4] & addr[5]) == 0xff;
264}
265
266static inline int is_unicast_ether_addr(const uint8_t *addr)
267{
268 return !is_multicast_ether_addr(addr);
269}
270
271typedef enum {
272 ETH_PKT_UCAST = 0xAABBCC00,
273 ETH_PKT_BCAST,
274 ETH_PKT_MCAST
275} eth_pkt_types_e;
276
277static inline eth_pkt_types_e
278get_eth_packet_type(const struct eth_header *ehdr)
279{
280 if (is_broadcast_ether_addr(ehdr->h_dest)) {
281 return ETH_PKT_BCAST;
282 } else if (is_multicast_ether_addr(ehdr->h_dest)) {
283 return ETH_PKT_MCAST;
284 } else {
285 return ETH_PKT_UCAST;
286 }
287}
288
289static inline uint32_t
290eth_get_l2_hdr_length(const void *p)
291{
292 uint16_t proto = be16_to_cpu(PKT_GET_ETH_HDR(p)->h_proto);
293 struct vlan_header *hvlan = PKT_GET_VLAN_HDR(p);
294 switch (proto) {
295 case ETH_P_VLAN:
296 return sizeof(struct eth_header) + sizeof(struct vlan_header);
297 case ETH_P_DVLAN:
298 if (be16_to_cpu(hvlan->h_proto) == ETH_P_VLAN) {
299 return sizeof(struct eth_header) + 2 * sizeof(struct vlan_header);
300 } else {
301 return sizeof(struct eth_header) + sizeof(struct vlan_header);
302 }
303 default:
304 return sizeof(struct eth_header);
305 }
306}
307
308static inline uint32_t
309eth_get_l2_hdr_length_iov(const struct iovec *iov, int iovcnt)
310{
311 uint8_t p[sizeof(struct eth_header) + sizeof(struct vlan_header)];
312 size_t copied = iov_to_buf(iov, iovcnt, 0, p, ARRAY_SIZE(p));
313
314 if (copied < ARRAY_SIZE(p)) {
315 return copied;
316 }
317
318 return eth_get_l2_hdr_length(p);
319}
320
321static inline uint16_t
322eth_get_pkt_tci(const void *p)
323{
324 uint16_t proto = be16_to_cpu(PKT_GET_ETH_HDR(p)->h_proto);
325 struct vlan_header *hvlan = PKT_GET_VLAN_HDR(p);
326 switch (proto) {
327 case ETH_P_VLAN:
328 case ETH_P_DVLAN:
329 return be16_to_cpu(hvlan->h_tci);
330 default:
331 return 0;
332 }
333}
334
335size_t
336eth_strip_vlan(const struct iovec *iov, int iovcnt, size_t iovoff,
337 uint8_t *new_ehdr_buf,
338 uint16_t *payload_offset, uint16_t *tci);
339
340size_t
341eth_strip_vlan_ex(const struct iovec *iov, int iovcnt, size_t iovoff,
342 uint16_t vet, uint8_t *new_ehdr_buf,
343 uint16_t *payload_offset, uint16_t *tci);
344
345uint16_t
346eth_get_l3_proto(const struct iovec *l2hdr_iov, int iovcnt, size_t l2hdr_len);
347
348void eth_setup_vlan_headers_ex(struct eth_header *ehdr, uint16_t vlan_tag,
349 uint16_t vlan_ethtype, bool *is_new);
350
351static inline void
352eth_setup_vlan_headers(struct eth_header *ehdr, uint16_t vlan_tag,
353 bool *is_new)
354{
355 eth_setup_vlan_headers_ex(ehdr, vlan_tag, ETH_P_VLAN, is_new);
356}
357
358
359uint8_t eth_get_gso_type(uint16_t l3_proto, uint8_t *l3_hdr, uint8_t l4proto);
360
361typedef struct eth_ip6_hdr_info_st {
362 uint8_t l4proto;
363 size_t full_hdr_len;
364 struct ip6_header ip6_hdr;
365 bool has_ext_hdrs;
366 bool rss_ex_src_valid;
367 struct in6_address rss_ex_src;
368 bool rss_ex_dst_valid;
369 struct in6_address rss_ex_dst;
370 bool fragment;
371} eth_ip6_hdr_info;
372
373typedef struct eth_ip4_hdr_info_st {
374 struct ip_header ip4_hdr;
375 bool fragment;
376} eth_ip4_hdr_info;
377
378typedef struct eth_l4_hdr_info_st {
379 union {
380 struct tcp_header tcp;
381 struct udp_header udp;
382 } hdr;
383
384 bool has_tcp_data;
385} eth_l4_hdr_info;
386
387void eth_get_protocols(const struct iovec *iov, int iovcnt,
388 bool *isip4, bool *isip6,
389 bool *isudp, bool *istcp,
390 size_t *l3hdr_off,
391 size_t *l4hdr_off,
392 size_t *l5hdr_off,
393 eth_ip6_hdr_info *ip6hdr_info,
394 eth_ip4_hdr_info *ip4hdr_info,
395 eth_l4_hdr_info *l4hdr_info);
396
397void eth_setup_ip4_fragmentation(const void *l2hdr, size_t l2hdr_len,
398 void *l3hdr, size_t l3hdr_len,
399 size_t l3payload_len,
400 size_t frag_offset, bool more_frags);
401
402void
403eth_fix_ip4_checksum(void *l3hdr, size_t l3hdr_len);
404
405uint32_t
406eth_calc_ip4_pseudo_hdr_csum(struct ip_header *iphdr,
407 uint16_t csl,
408 uint32_t *cso);
409
410uint32_t
411eth_calc_ip6_pseudo_hdr_csum(struct ip6_header *iphdr,
412 uint16_t csl,
413 uint8_t l4_proto,
414 uint32_t *cso);
415
416bool
417eth_parse_ipv6_hdr(const struct iovec *pkt, int pkt_frags,
418 size_t ip6hdr_off, eth_ip6_hdr_info *info);
419
420#endif
421