xref: /openbmc/qemu/include/net/eth.h (revision 3f4ad55ea28f75804d999cd3e1169c188bde052a)
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  #define ETH_ZLEN 60     /* Min. octets in frame without FCS */
35  #define ETH_FCS_LEN 4
36  #define ETH_MTU 1500
37  
38  struct eth_header {
39      uint8_t  h_dest[ETH_ALEN];   /* destination eth addr */
40      uint8_t  h_source[ETH_ALEN]; /* source ether addr    */
41      uint16_t h_proto;            /* packet type ID field */
42  };
43  
44  struct vlan_header {
45      uint16_t  h_tci;     /* priority and VLAN ID  */
46      uint16_t  h_proto;   /* encapsulated protocol */
47  };
48  
49  struct ip_header {
50      uint8_t  ip_ver_len;     /* version and header length */
51      uint8_t  ip_tos;         /* type of service */
52      uint16_t ip_len;         /* total length */
53      uint16_t ip_id;          /* identification */
54      uint16_t ip_off;         /* fragment offset field */
55      uint8_t  ip_ttl;         /* time to live */
56      uint8_t  ip_p;           /* protocol */
57      uint16_t ip_sum;         /* checksum */
58      uint32_t ip_src, ip_dst; /* source and destination address */
59  } QEMU_PACKED;
60  
61  typedef struct tcp_header {
62      uint16_t th_sport;          /* source port */
63      uint16_t th_dport;          /* destination port */
64      uint32_t th_seq;            /* sequence number */
65      uint32_t th_ack;            /* acknowledgment number */
66      uint16_t th_offset_flags;   /* data offset, reserved 6 bits, */
67                                  /* TCP protocol flags */
68      uint16_t th_win;            /* window */
69      uint16_t th_sum;            /* checksum */
70      uint16_t th_urp;            /* urgent pointer */
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  
83  typedef struct udp_header {
84      uint16_t uh_sport; /* source port */
85      uint16_t uh_dport; /* destination port */
86      uint16_t uh_ulen;  /* udp length */
87      uint16_t uh_sum;   /* udp checksum */
88  } udp_header;
89  
90  typedef 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  /* IPv6 address */
99  struct in6_address {
100      union {
101          uint8_t __u6_addr8[16];
102      } __in6_u;
103  };
104  
105  struct ip6_header {
106      union {
107          struct ip6_hdrctl {
108              uint32_t ip6_un1_flow; /* 4 bits version, 8 bits TC,
109                                        20 bits flow-ID */
110              uint16_t ip6_un1_plen; /* payload length */
111              uint8_t  ip6_un1_nxt;  /* next header */
112              uint8_t  ip6_un1_hlim; /* hop limit */
113          } ip6_un1;
114          uint8_t ip6_un2_vfc;       /* 4 bits version, top 4 bits tclass */
115          struct ip6_ecn_access {
116              uint8_t  ip6_un3_vfc;  /* 4 bits version, top 4 bits tclass */
117              uint8_t  ip6_un3_ecn;  /* 2 bits ECN, top 6 bits payload length */
118          } ip6_un3;
119      } ip6_ctlun;
120      struct in6_address ip6_src;    /* source address */
121      struct in6_address ip6_dst;    /* destination address */
122  };
123  
124  typedef 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  
132  struct ip6_ext_hdr {
133      uint8_t        ip6r_nxt;   /* next header */
134      uint8_t        ip6r_len;   /* length in units of 8 octets */
135  };
136  
137  struct 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  
145  struct 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  
152  struct udp_hdr {
153    uint16_t uh_sport;           /* source port */
154    uint16_t uh_dport;           /* destination port */
155    uint16_t uh_ulen;            /* udp length */
156    uint16_t uh_sum;             /* udp checksum */
157  };
158  
159  struct tcp_hdr {
160      u_short     th_sport;   /* source port */
161      u_short     th_dport;   /* destination port */
162      uint32_t    th_seq;     /* sequence number */
163      uint32_t    th_ack;     /* acknowledgment number */
164  #if HOST_BIG_ENDIAN
165      u_char  th_off : 4,     /* data offset */
166          th_x2:4;            /* (unused) */
167  #else
168      u_char  th_x2 : 4,      /* (unused) */
169          th_off:4;           /* data offset */
170  #endif
171  
172  #define TH_ELN  0x1 /* explicit loss notification */
173  #define TH_ECN  0x2 /* explicit congestion notification */
174  #define TH_FS   0x4 /* fast start */
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;      /* window */
186      u_short th_sum;      /* checksum */
187      u_short th_urp;      /* urgent pointer */
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)      /* Internet Protocol packet  */
216  #define ETH_P_ARP                 (0x0806)      /* Address Resolution packet */
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           /* reserved fragment flag */
248  #define IP_DF                 0x4000           /* don't fragment flag */
249  #define IP_MF                 0x2000           /* more fragments flag */
250  #define IP_OFFMASK            0x1fff           /* mask for fragmenting bits */
251  
252  #define IP6_EXT_GRANULARITY   (8)  /* Size granularity for
253                                        IPv6 extension headers */
254  
255  /* IP6 extension header types */
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  
is_multicast_ether_addr(const uint8_t * addr)265  static inline int is_multicast_ether_addr(const uint8_t *addr)
266  {
267      return 0x01 & addr[0];
268  }
269  
is_broadcast_ether_addr(const uint8_t * addr)270  static 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  
is_unicast_ether_addr(const uint8_t * addr)275  static inline int is_unicast_ether_addr(const uint8_t *addr)
276  {
277      return !is_multicast_ether_addr(addr);
278  }
279  
280  typedef enum {
281      ETH_PKT_UCAST = 0xAABBCC00,
282      ETH_PKT_BCAST,
283      ETH_PKT_MCAST
284  } eth_pkt_types_e;
285  
286  static inline eth_pkt_types_e
get_eth_packet_type(const struct eth_header * ehdr)287  get_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 { /* unicast */
294          return ETH_PKT_UCAST;
295      }
296  }
297  
298  static inline uint32_t
eth_get_l2_hdr_length(const void * p)299  eth_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  
317  static inline uint32_t
eth_get_l2_hdr_length_iov(const struct iovec * iov,size_t iovcnt,size_t iovoff)318  eth_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  
330  static inline uint16_t
eth_get_pkt_tci(const void * p)331  eth_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  
344  size_t
345  eth_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  
349  size_t
350  eth_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  
354  uint16_t
355  eth_get_l3_proto(const struct iovec *l2hdr_iov, int iovcnt, size_t l2hdr_len);
356  
357  void eth_setup_vlan_headers(struct eth_header *ehdr, size_t *ehdr_size,
358                              uint16_t vlan_tag, uint16_t vlan_ethtype);
359  
360  
361  uint8_t eth_get_gso_type(uint16_t l3_proto, uint8_t *l3_hdr, uint8_t l4proto);
362  
363  typedef 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  
375  typedef struct eth_ip4_hdr_info_st {
376      struct ip_header ip4_hdr;
377      bool   fragment;
378  } eth_ip4_hdr_info;
379  
380  typedef 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  
387  typedef 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  
397  void 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  
406  void
407  eth_fix_ip4_checksum(void *l3hdr, size_t l3hdr_len);
408  
409  uint32_t
410  eth_calc_ip4_pseudo_hdr_csum(struct ip_header *iphdr,
411                               uint16_t csl,
412                               uint32_t *cso);
413  
414  uint32_t
415  eth_calc_ip6_pseudo_hdr_csum(struct ip6_header *iphdr,
416                               uint16_t csl,
417                               uint8_t l4_proto,
418                               uint32_t *cso);
419  
420  bool
421  eth_parse_ipv6_hdr(const struct iovec *pkt, int pkt_frags,
422                     size_t ip6hdr_off, eth_ip6_hdr_info *info);
423  
424  /**
425   * eth_pad_short_frame - pad a short frame to the minimum Ethernet frame length
426   *
427   * If the Ethernet frame size is shorter than 60 bytes, it will be padded to
428   * 60 bytes at the address @padded_pkt.
429   *
430   * @padded_pkt: buffer address to hold the padded frame
431   * @padded_buflen: pointer holding length of @padded_pkt. If the frame is
432   *                 padded, the length will be updated to the padded one.
433   * @pkt: address to hold the original Ethernet frame
434   * @pkt_size: size of the original Ethernet frame
435   * @return true if the frame is padded, otherwise false
436   */
437  bool eth_pad_short_frame(uint8_t *padded_pkt, size_t *padded_buflen,
438                           const void *pkt, size_t pkt_size);
439  
440  #endif
441