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#define ETH_ZLEN 60
35#define ETH_FCS_LEN 4
36#define ETH_MTU 1500
37
38struct eth_header {
39 uint8_t h_dest[ETH_ALEN];
40 uint8_t h_source[ETH_ALEN];
41 uint16_t h_proto;
42};
43
44struct vlan_header {
45 uint16_t h_tci;
46 uint16_t h_proto;
47};
48
49struct ip_header {
50 uint8_t ip_ver_len;
51 uint8_t ip_tos;
52 uint16_t ip_len;
53 uint16_t ip_id;
54 uint16_t ip_off;
55 uint8_t ip_ttl;
56 uint8_t ip_p;
57 uint16_t ip_sum;
58 uint32_t ip_src, ip_dst;
59} QEMU_PACKED;
60
61typedef struct tcp_header {
62 uint16_t th_sport;
63 uint16_t th_dport;
64 uint32_t th_seq;
65 uint32_t th_ack;
66 uint16_t th_offset_flags;
67
68 uint16_t th_win;
69 uint16_t th_sum;
70 uint16_t th_urp;
71} tcp_header;
72
73#define TCP_FLAGS_ONLY(flags) ((flags) & 0x3f)
74
75#define TCP_HEADER_FLAGS(tcp) \
76 TCP_FLAGS_ONLY(be16_to_cpu((tcp)->th_offset_flags))
77
78#define TCP_FLAG_ACK 0x10
79
80#define TCP_HEADER_DATA_OFFSET(tcp) \
81 (((be16_to_cpu((tcp)->th_offset_flags) >> 12) & 0xf) << 2)
82
83typedef struct udp_header {
84 uint16_t uh_sport;
85 uint16_t uh_dport;
86 uint16_t uh_ulen;
87 uint16_t uh_sum;
88} udp_header;
89
90typedef 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;
97
98
99struct in6_address {
100 union {
101 uint8_t __u6_addr8[16];
102 } __in6_u;
103};
104
105struct ip6_header {
106 union {
107 struct ip6_hdrctl {
108 uint32_t ip6_un1_flow;
109
110 uint16_t ip6_un1_plen;
111 uint8_t ip6_un1_nxt;
112 uint8_t ip6_un1_hlim;
113 } ip6_un1;
114 uint8_t ip6_un2_vfc;
115 struct ip6_ecn_access {
116 uint8_t ip6_un3_vfc;
117 uint8_t ip6_un3_ecn;
118 } ip6_un3;
119 } ip6_ctlun;
120 struct in6_address ip6_src;
121 struct in6_address ip6_dst;
122};
123
124typedef 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;
131
132struct ip6_ext_hdr {
133 uint8_t ip6r_nxt;
134 uint8_t ip6r_len;
135};
136
137struct 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];
143};
144
145struct ip6_option_hdr {
146#define IP6_OPT_PAD1 (0x00)
147#define IP6_OPT_HOME (0xC9)
148 uint8_t type;
149 uint8_t len;
150};
151
152struct udp_hdr {
153 uint16_t uh_sport;
154 uint16_t uh_dport;
155 uint16_t uh_ulen;
156 uint16_t uh_sum;
157};
158
159struct tcp_hdr {
160 u_short th_sport;
161 u_short th_dport;
162 uint32_t th_seq;
163 uint32_t th_ack;
164#if HOST_BIG_ENDIAN
165 u_char th_off : 4,
166 th_x2:4;
167#else
168 u_char th_x2 : 4,
169 th_off:4;
170#endif
171
172#define TH_ELN 0x1
173#define TH_ECN 0x2
174#define TH_FS 0x4
175
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;
186 u_short th_sum;
187 u_short th_urp;
188};
189
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
193
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)
214
215#define ETH_P_IP (0x0800)
216#define ETH_P_ARP (0x0806)
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)
235
236#define IS_SPECIAL_VLAN_ID(x) \
237 (((x) == 0) || ((x) == 0xFFF))
238
239#define ETH_MAX_L2_HDR_LEN \
240 (sizeof(struct eth_header) + 2 * sizeof(struct vlan_header))
241
242#define ETH_MAX_IP4_HDR_LEN (60)
243#define ETH_MAX_IP_DGRAM_LEN (0xFFFF)
244
245#define IP_FRAG_UNIT_SIZE (8)
246#define IP_FRAG_ALIGN_SIZE(x) ((x) & ~0x7)
247#define IP_RF 0x8000
248#define IP_DF 0x4000
249#define IP_MF 0x2000
250#define IP_OFFMASK 0x1fff
251
252#define IP6_EXT_GRANULARITY (8)
253
254
255
256#define IP6_HOP_BY_HOP (0)
257#define IP6_ROUTING (43)
258#define IP6_FRAGMENT (44)
259#define IP6_ESP (50)
260#define IP6_AUTHENTICATION (51)
261#define IP6_NONE (59)
262#define IP6_DESTINATON (60)
263#define IP6_MOBILITY (135)
264
265static inline int is_multicast_ether_addr(const uint8_t *addr)
266{
267 return 0x01 & addr[0];
268}
269
270static inline int is_broadcast_ether_addr(const uint8_t *addr)
271{
272 return (addr[0] & addr[1] & addr[2] & addr[3] & addr[4] & addr[5]) == 0xff;
273}
274
275static inline int is_unicast_ether_addr(const uint8_t *addr)
276{
277 return !is_multicast_ether_addr(addr);
278}
279
280typedef enum {
281 ETH_PKT_UCAST = 0xAABBCC00,
282 ETH_PKT_BCAST,
283 ETH_PKT_MCAST
284} eth_pkt_types_e;
285
286static inline eth_pkt_types_e
287get_eth_packet_type(const struct eth_header *ehdr)
288{
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 {
294 return ETH_PKT_UCAST;
295 }
296}
297
298static inline uint32_t
299eth_get_l2_hdr_length(const void *p)
300{
301 uint16_t proto = be16_to_cpu(PKT_GET_ETH_HDR(p)->h_proto);
302 struct vlan_header *hvlan = PKT_GET_VLAN_HDR(p);
303 switch (proto) {
304 case ETH_P_VLAN:
305 return sizeof(struct eth_header) + sizeof(struct vlan_header);
306 case ETH_P_DVLAN:
307 if (be16_to_cpu(hvlan->h_proto) == ETH_P_VLAN) {
308 return sizeof(struct eth_header) + 2 * sizeof(struct vlan_header);
309 } else {
310 return sizeof(struct eth_header) + sizeof(struct vlan_header);
311 }
312 default:
313 return sizeof(struct eth_header);
314 }
315}
316
317static inline uint32_t
318eth_get_l2_hdr_length_iov(const struct iovec *iov, size_t iovcnt, size_t iovoff)
319{
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));
322
323 if (copied < ARRAY_SIZE(p)) {
324 return copied;
325 }
326
327 return eth_get_l2_hdr_length(p);
328}
329
330static inline uint16_t
331eth_get_pkt_tci(const void *p)
332{
333 uint16_t proto = be16_to_cpu(PKT_GET_ETH_HDR(p)->h_proto);
334 struct vlan_header *hvlan = PKT_GET_VLAN_HDR(p);
335 switch (proto) {
336 case ETH_P_VLAN:
337 case ETH_P_DVLAN:
338 return be16_to_cpu(hvlan->h_tci);
339 default:
340 return 0;
341 }
342}
343
344size_t
345eth_strip_vlan(const struct iovec *iov, int iovcnt, size_t iovoff,
346 void *new_ehdr_buf,
347 uint16_t *payload_offset, uint16_t *tci);
348
349size_t
350eth_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);
353
354uint16_t
355eth_get_l3_proto(const struct iovec *l2hdr_iov, int iovcnt, size_t l2hdr_len);
356
357void eth_setup_vlan_headers(struct eth_header *ehdr, size_t *ehdr_size,
358 uint16_t vlan_tag, uint16_t vlan_ethtype);
359
360
361uint8_t eth_get_gso_type(uint16_t l3_proto, uint8_t *l3_hdr, uint8_t l4proto);
362
363typedef struct eth_ip6_hdr_info_st {
364 uint8_t l4proto;
365 size_t full_hdr_len;
366 struct ip6_header ip6_hdr;
367 bool has_ext_hdrs;
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;
372 bool fragment;
373} eth_ip6_hdr_info;
374
375typedef struct eth_ip4_hdr_info_st {
376 struct ip_header ip4_hdr;
377 bool fragment;
378} eth_ip4_hdr_info;
379
380typedef 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
385} EthL4HdrProto;
386
387typedef struct eth_l4_hdr_info_st {
388 union {
389 struct tcp_header tcp;
390 struct udp_header udp;
391 } hdr;
392
393 EthL4HdrProto proto;
394 bool has_tcp_data;
395} eth_l4_hdr_info;
396
397void eth_get_protocols(const struct iovec *iov, size_t iovcnt, size_t iovoff,
398 bool *hasip4, bool *hasip6,
399 size_t *l3hdr_off,
400 size_t *l4hdr_off,
401 size_t *l5hdr_off,
402 eth_ip6_hdr_info *ip6hdr_info,
403 eth_ip4_hdr_info *ip4hdr_info,
404 eth_l4_hdr_info *l4hdr_info);
405
406void
407eth_fix_ip4_checksum(void *l3hdr, size_t l3hdr_len);
408
409uint32_t
410eth_calc_ip4_pseudo_hdr_csum(struct ip_header *iphdr,
411 uint16_t csl,
412 uint32_t *cso);
413
414uint32_t
415eth_calc_ip6_pseudo_hdr_csum(struct ip6_header *iphdr,
416 uint16_t csl,
417 uint8_t l4_proto,
418 uint32_t *cso);
419
420bool
421eth_parse_ipv6_hdr(const struct iovec *pkt, int pkt_frags,
422 size_t ip6hdr_off, eth_ip6_hdr_info *info);
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437bool eth_pad_short_frame(uint8_t *padded_pkt, size_t *padded_buflen,
438 const void *pkt, size_t pkt_size);
439
440#endif
441