xref: /openbmc/qemu/include/net/eth.h (revision a68694cd)
1 /*
2  * QEMU network structures definitions and helper functions
3  *
4  * Copyright (c) 2012 Ravello Systems LTD (http://ravellosystems.com)
5  *
6  * Developed by Daynix Computing LTD (http://www.daynix.com)
7  *
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)
11  *
12  * Portions developed by Igor Kovalenko
13  * Copyright (c) 2006 Igor Kovalenko
14  * See hw/rtl8139.c (QEMU)
15  *
16  * Authors:
17  * Dmitry Fleytman <dmitry@daynix.com>
18  * Tamir Shomer <tamirs@daynix.com>
19  * Yan Vugenfirer <yan@daynix.com>
20  *
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.
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 
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 */
39 };
40 
41 struct vlan_header {
42     uint16_t  h_tci;     /* priority and VLAN ID  */
43     uint16_t  h_proto;   /* encapsulated protocol */
44 };
45 
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 */
56 };
57 
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;
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 
80 typedef struct udp_header {
81     uint16_t uh_sport; /* source port */
82     uint16_t uh_dport; /* destination port */
83     uint16_t uh_ulen;  /* udp length */
84     uint16_t uh_sum;   /* udp checksum */
85 } udp_header;
86 
87 typedef 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 /* IPv6 address */
96 struct in6_address {
97     union {
98         uint8_t __u6_addr8[16];
99     } __in6_u;
100 };
101 
102 struct ip6_header {
103     union {
104         struct ip6_hdrctl {
105             uint32_t ip6_un1_flow; /* 4 bits version, 8 bits TC,
106                                       20 bits flow-ID */
107             uint16_t ip6_un1_plen; /* payload length */
108             uint8_t  ip6_un1_nxt;  /* next header */
109             uint8_t  ip6_un1_hlim; /* hop limit */
110         } ip6_un1;
111         uint8_t ip6_un2_vfc;       /* 4 bits version, top 4 bits tclass */
112         struct ip6_ecn_access {
113             uint8_t  ip6_un3_vfc;  /* 4 bits version, top 4 bits tclass */
114             uint8_t  ip6_un3_ecn;  /* 2 bits ECN, top 6 bits payload length */
115         } ip6_un3;
116     } ip6_ctlun;
117     struct in6_address ip6_src;    /* source address */
118     struct in6_address ip6_dst;    /* destination address */
119 };
120 
121 typedef 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 
129 struct ip6_ext_hdr {
130     uint8_t        ip6r_nxt;   /* next header */
131     uint8_t        ip6r_len;   /* length in units of 8 octets */
132 };
133 
134 struct 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 
142 struct 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 
149 struct udp_hdr {
150   uint16_t uh_sport;           /* source port */
151   uint16_t uh_dport;           /* destination port */
152   uint16_t uh_ulen;            /* udp length */
153   uint16_t uh_sum;             /* udp checksum */
154 };
155 
156 struct tcp_hdr {
157     u_short     th_sport;   /* source port */
158     u_short     th_dport;   /* destination port */
159     uint32_t    th_seq;     /* sequence number */
160     uint32_t    th_ack;     /* acknowledgment number */
161 #ifdef HOST_WORDS_BIGENDIAN
162     u_char  th_off : 4,     /* data offset */
163         th_x2:4;            /* (unused) */
164 #else
165     u_char  th_x2 : 4,      /* (unused) */
166         th_off:4;           /* data offset */
167 #endif
168 
169 #define TH_ELN  0x1 /* explicit loss notification */
170 #define TH_ECN  0x2 /* explicit congestion notification */
171 #define TH_FS   0x4 /* fast start */
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 #define TH_ECE  0x40
181 #define TH_CWR  0x80
182     u_short th_win;      /* window */
183     u_short th_sum;      /* checksum */
184     u_short th_urp;      /* urgent pointer */
185 };
186 
187 #define ip6_nxt      ip6_ctlun.ip6_un1.ip6_un1_nxt
188 #define ip6_ecn_acc  ip6_ctlun.ip6_un3.ip6_un3_ecn
189 #define ip6_plen     ip6_ctlun.ip6_un1.ip6_un1_plen
190 
191 #define PKT_GET_ETH_HDR(p)        \
192     ((struct eth_header *)(p))
193 #define PKT_GET_VLAN_HDR(p)       \
194     ((struct vlan_header *) (((uint8_t *)(p)) + sizeof(struct eth_header)))
195 #define PKT_GET_DVLAN_HDR(p)       \
196     (PKT_GET_VLAN_HDR(p) + 1)
197 #define PKT_GET_IP_HDR(p)         \
198     ((struct ip_header *)(((uint8_t *)(p)) + eth_get_l2_hdr_length(p)))
199 #define IP_HDR_GET_LEN(p)         \
200     ((ldub_p(p + offsetof(struct ip_header, ip_ver_len)) & 0x0F) << 2)
201 #define IP_HDR_GET_P(p)                                           \
202     (ldub_p(p + offsetof(struct ip_header, ip_p)))
203 #define PKT_GET_IP_HDR_LEN(p)     \
204     (IP_HDR_GET_LEN(PKT_GET_IP_HDR(p)))
205 #define PKT_GET_IP6_HDR(p)        \
206     ((struct ip6_header *) (((uint8_t *)(p)) + eth_get_l2_hdr_length(p)))
207 #define IP_HEADER_VERSION(ip)     \
208     (((ip)->ip_ver_len >> 4) & 0xf)
209 #define IP4_IS_FRAGMENT(ip) \
210     ((be16_to_cpu((ip)->ip_off) & (IP_OFFMASK | IP_MF)) != 0)
211 
212 #define ETH_P_IP                  (0x0800)      /* Internet Protocol packet  */
213 #define ETH_P_ARP                 (0x0806)      /* Address Resolution packet */
214 #define ETH_P_IPV6                (0x86dd)
215 #define ETH_P_VLAN                (0x8100)
216 #define ETH_P_DVLAN               (0x88a8)
217 #define ETH_P_NCSI                (0x88f8)
218 #define ETH_P_UNKNOWN             (0xffff)
219 #define VLAN_VID_MASK             0x0fff
220 #define IP_HEADER_VERSION_4       (4)
221 #define IP_HEADER_VERSION_6       (6)
222 #define IP_PROTO_TCP              (6)
223 #define IP_PROTO_UDP              (17)
224 #define IPTOS_ECN_MASK            0x03
225 #define IPTOS_ECN(x)              ((x) & IPTOS_ECN_MASK)
226 #define IPTOS_ECN_CE              0x03
227 #define IP6_ECN_MASK              0xC0
228 #define IP6_ECN(x)                ((x) & IP6_ECN_MASK)
229 #define IP6_ECN_CE                0xC0
230 #define IP4_DONT_FRAGMENT_FLAG    (1 << 14)
231 
232 #define IS_SPECIAL_VLAN_ID(x)     \
233     (((x) == 0) || ((x) == 0xFFF))
234 
235 #define ETH_MAX_L2_HDR_LEN  \
236     (sizeof(struct eth_header) + 2 * sizeof(struct vlan_header))
237 
238 #define ETH_MAX_IP4_HDR_LEN   (60)
239 #define ETH_MAX_IP_DGRAM_LEN  (0xFFFF)
240 
241 #define IP_FRAG_UNIT_SIZE     (8)
242 #define IP_FRAG_ALIGN_SIZE(x) ((x) & ~0x7)
243 #define IP_RF                 0x8000           /* reserved fragment flag */
244 #define IP_DF                 0x4000           /* don't fragment flag */
245 #define IP_MF                 0x2000           /* more fragments flag */
246 #define IP_OFFMASK            0x1fff           /* mask for fragmenting bits */
247 
248 #define IP6_EXT_GRANULARITY   (8)  /* Size granularity for
249                                       IPv6 extension headers */
250 
251 /* IP6 extension header types */
252 #define IP6_HOP_BY_HOP        (0)
253 #define IP6_ROUTING           (43)
254 #define IP6_FRAGMENT          (44)
255 #define IP6_ESP               (50)
256 #define IP6_AUTHENTICATION    (51)
257 #define IP6_NONE              (59)
258 #define IP6_DESTINATON        (60)
259 #define IP6_MOBILITY          (135)
260 
261 static inline int is_multicast_ether_addr(const uint8_t *addr)
262 {
263     return 0x01 & addr[0];
264 }
265 
266 static inline int is_broadcast_ether_addr(const uint8_t *addr)
267 {
268     return (addr[0] & addr[1] & addr[2] & addr[3] & addr[4] & addr[5]) == 0xff;
269 }
270 
271 static inline int is_unicast_ether_addr(const uint8_t *addr)
272 {
273     return !is_multicast_ether_addr(addr);
274 }
275 
276 typedef enum {
277     ETH_PKT_UCAST = 0xAABBCC00,
278     ETH_PKT_BCAST,
279     ETH_PKT_MCAST
280 } eth_pkt_types_e;
281 
282 static inline eth_pkt_types_e
283 get_eth_packet_type(const struct eth_header *ehdr)
284 {
285     if (is_broadcast_ether_addr(ehdr->h_dest)) {
286         return ETH_PKT_BCAST;
287     } else if (is_multicast_ether_addr(ehdr->h_dest)) {
288         return ETH_PKT_MCAST;
289     } else { /* unicast */
290         return ETH_PKT_UCAST;
291     }
292 }
293 
294 static inline uint32_t
295 eth_get_l2_hdr_length(const void *p)
296 {
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     switch (proto) {
300     case ETH_P_VLAN:
301         return sizeof(struct eth_header) + sizeof(struct vlan_header);
302     case ETH_P_DVLAN:
303         if (be16_to_cpu(hvlan->h_proto) == ETH_P_VLAN) {
304             return sizeof(struct eth_header) + 2 * sizeof(struct vlan_header);
305         } else {
306             return sizeof(struct eth_header) + sizeof(struct vlan_header);
307         }
308     default:
309         return sizeof(struct eth_header);
310     }
311 }
312 
313 static inline uint32_t
314 eth_get_l2_hdr_length_iov(const struct iovec *iov, int iovcnt)
315 {
316     uint8_t p[sizeof(struct eth_header) + sizeof(struct vlan_header)];
317     size_t copied = iov_to_buf(iov, iovcnt, 0, p, ARRAY_SIZE(p));
318 
319     if (copied < ARRAY_SIZE(p)) {
320         return copied;
321     }
322 
323     return eth_get_l2_hdr_length(p);
324 }
325 
326 static inline uint16_t
327 eth_get_pkt_tci(const void *p)
328 {
329     uint16_t proto = be16_to_cpu(PKT_GET_ETH_HDR(p)->h_proto);
330     struct vlan_header *hvlan = PKT_GET_VLAN_HDR(p);
331     switch (proto) {
332     case ETH_P_VLAN:
333     case ETH_P_DVLAN:
334         return be16_to_cpu(hvlan->h_tci);
335     default:
336         return 0;
337     }
338 }
339 
340 size_t
341 eth_strip_vlan(const struct iovec *iov, int iovcnt, size_t iovoff,
342                uint8_t *new_ehdr_buf,
343                uint16_t *payload_offset, uint16_t *tci);
344 
345 size_t
346 eth_strip_vlan_ex(const struct iovec *iov, int iovcnt, size_t iovoff,
347                   uint16_t vet, uint8_t *new_ehdr_buf,
348                   uint16_t *payload_offset, uint16_t *tci);
349 
350 uint16_t
351 eth_get_l3_proto(const struct iovec *l2hdr_iov, int iovcnt, size_t l2hdr_len);
352 
353 void eth_setup_vlan_headers_ex(struct eth_header *ehdr, uint16_t vlan_tag,
354     uint16_t vlan_ethtype, bool *is_new);
355 
356 static inline void
357 eth_setup_vlan_headers(struct eth_header *ehdr, uint16_t vlan_tag,
358     bool *is_new)
359 {
360     eth_setup_vlan_headers_ex(ehdr, vlan_tag, ETH_P_VLAN, is_new);
361 }
362 
363 
364 uint8_t eth_get_gso_type(uint16_t l3_proto, uint8_t *l3_hdr, uint8_t l4proto);
365 
366 typedef struct eth_ip6_hdr_info_st {
367     uint8_t l4proto;
368     size_t  full_hdr_len;
369     struct  ip6_header ip6_hdr;
370     bool    has_ext_hdrs;
371     bool    rss_ex_src_valid;
372     struct  in6_address rss_ex_src;
373     bool    rss_ex_dst_valid;
374     struct  in6_address rss_ex_dst;
375     bool    fragment;
376 } eth_ip6_hdr_info;
377 
378 typedef struct eth_ip4_hdr_info_st {
379     struct ip_header ip4_hdr;
380     bool   fragment;
381 } eth_ip4_hdr_info;
382 
383 typedef struct eth_l4_hdr_info_st {
384     union {
385         struct tcp_header tcp;
386         struct udp_header udp;
387     } hdr;
388 
389     bool has_tcp_data;
390 } eth_l4_hdr_info;
391 
392 void eth_get_protocols(const struct iovec *iov, int iovcnt,
393                        bool *isip4, bool *isip6,
394                        bool *isudp, bool *istcp,
395                        size_t *l3hdr_off,
396                        size_t *l4hdr_off,
397                        size_t *l5hdr_off,
398                        eth_ip6_hdr_info *ip6hdr_info,
399                        eth_ip4_hdr_info *ip4hdr_info,
400                        eth_l4_hdr_info  *l4hdr_info);
401 
402 void eth_setup_ip4_fragmentation(const void *l2hdr, size_t l2hdr_len,
403                                  void *l3hdr, size_t l3hdr_len,
404                                  size_t l3payload_len,
405                                  size_t frag_offset, bool more_frags);
406 
407 void
408 eth_fix_ip4_checksum(void *l3hdr, size_t l3hdr_len);
409 
410 uint32_t
411 eth_calc_ip4_pseudo_hdr_csum(struct ip_header *iphdr,
412                              uint16_t csl,
413                              uint32_t *cso);
414 
415 uint32_t
416 eth_calc_ip6_pseudo_hdr_csum(struct ip6_header *iphdr,
417                              uint16_t csl,
418                              uint8_t l4_proto,
419                              uint32_t *cso);
420 
421 bool
422 eth_parse_ipv6_hdr(const struct iovec *pkt, int pkt_frags,
423                    size_t ip6hdr_off, eth_ip6_hdr_info *info);
424 
425 #endif
426