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