eth.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. #ifndef QEMU_ETH_H
  26. #define QEMU_ETH_H
  27. #include "qemu/bswap.h"
  28. #include "qemu/iov.h"
  29. #define ETH_ALEN 6
  30. #define ETH_HLEN 14
  31. #define ETH_ZLEN 60 /* Min. octets in frame without FCS */
  32. #define ETH_FCS_LEN 4
  33. #define ETH_MTU 1500
  34. struct eth_header {
  35. uint8_t h_dest[ETH_ALEN]; /* destination eth addr */
  36. uint8_t h_source[ETH_ALEN]; /* source ether addr */
  37. uint16_t h_proto; /* packet type ID field */
  38. };
  39. struct vlan_header {
  40. uint16_t h_tci; /* priority and VLAN ID */
  41. uint16_t h_proto; /* encapsulated protocol */
  42. };
  43. struct ip_header {
  44. uint8_t ip_ver_len; /* version and header length */
  45. uint8_t ip_tos; /* type of service */
  46. uint16_t ip_len; /* total length */
  47. uint16_t ip_id; /* identification */
  48. uint16_t ip_off; /* fragment offset field */
  49. uint8_t ip_ttl; /* time to live */
  50. uint8_t ip_p; /* protocol */
  51. uint16_t ip_sum; /* checksum */
  52. uint32_t ip_src, ip_dst; /* source and destination address */
  53. } QEMU_PACKED;
  54. typedef struct tcp_header {
  55. uint16_t th_sport; /* source port */
  56. uint16_t th_dport; /* destination port */
  57. uint32_t th_seq; /* sequence number */
  58. uint32_t th_ack; /* acknowledgment number */
  59. uint16_t th_offset_flags; /* data offset, reserved 6 bits, */
  60. /* TCP protocol flags */
  61. uint16_t th_win; /* window */
  62. uint16_t th_sum; /* checksum */
  63. uint16_t th_urp; /* urgent pointer */
  64. } tcp_header;
  65. #define TCP_FLAGS_ONLY(flags) ((flags) & 0x3f)
  66. #define TCP_HEADER_FLAGS(tcp) \
  67. TCP_FLAGS_ONLY(be16_to_cpu((tcp)->th_offset_flags))
  68. #define TCP_FLAG_ACK 0x10
  69. #define TCP_HEADER_DATA_OFFSET(tcp) \
  70. (((be16_to_cpu((tcp)->th_offset_flags) >> 12) & 0xf) << 2)
  71. typedef struct udp_header {
  72. uint16_t uh_sport; /* source port */
  73. uint16_t uh_dport; /* destination port */
  74. uint16_t uh_ulen; /* udp length */
  75. uint16_t uh_sum; /* udp checksum */
  76. } udp_header;
  77. typedef struct ip_pseudo_header {
  78. uint32_t ip_src;
  79. uint32_t ip_dst;
  80. uint8_t zeros;
  81. uint8_t ip_proto;
  82. uint16_t ip_payload;
  83. } ip_pseudo_header;
  84. /* IPv6 address */
  85. struct in6_address {
  86. union {
  87. uint8_t __u6_addr8[16];
  88. } __in6_u;
  89. };
  90. struct ip6_header {
  91. union {
  92. struct ip6_hdrctl {
  93. uint32_t ip6_un1_flow; /* 4 bits version, 8 bits TC,
  94. 20 bits flow-ID */
  95. uint16_t ip6_un1_plen; /* payload length */
  96. uint8_t ip6_un1_nxt; /* next header */
  97. uint8_t ip6_un1_hlim; /* hop limit */
  98. } ip6_un1;
  99. uint8_t ip6_un2_vfc; /* 4 bits version, top 4 bits tclass */
  100. struct ip6_ecn_access {
  101. uint8_t ip6_un3_vfc; /* 4 bits version, top 4 bits tclass */
  102. uint8_t ip6_un3_ecn; /* 2 bits ECN, top 6 bits payload length */
  103. } ip6_un3;
  104. } ip6_ctlun;
  105. struct in6_address ip6_src; /* source address */
  106. struct in6_address ip6_dst; /* destination address */
  107. };
  108. typedef struct ip6_pseudo_header {
  109. struct in6_address ip6_src;
  110. struct in6_address ip6_dst;
  111. uint32_t len;
  112. uint8_t zero[3];
  113. uint8_t next_hdr;
  114. } ip6_pseudo_header;
  115. struct ip6_ext_hdr {
  116. uint8_t ip6r_nxt; /* next header */
  117. uint8_t ip6r_len; /* length in units of 8 octets */
  118. };
  119. struct ip6_ext_hdr_routing {
  120. uint8_t nxt;
  121. uint8_t len;
  122. uint8_t rtype;
  123. uint8_t segleft;
  124. uint8_t rsvd[4];
  125. };
  126. struct ip6_option_hdr {
  127. #define IP6_OPT_PAD1 (0x00)
  128. #define IP6_OPT_HOME (0xC9)
  129. uint8_t type;
  130. uint8_t len;
  131. };
  132. struct udp_hdr {
  133. uint16_t uh_sport; /* source port */
  134. uint16_t uh_dport; /* destination port */
  135. uint16_t uh_ulen; /* udp length */
  136. uint16_t uh_sum; /* udp checksum */
  137. };
  138. struct tcp_hdr {
  139. u_short th_sport; /* source port */
  140. u_short th_dport; /* destination port */
  141. uint32_t th_seq; /* sequence number */
  142. uint32_t th_ack; /* acknowledgment number */
  143. #if HOST_BIG_ENDIAN
  144. u_char th_off : 4, /* data offset */
  145. th_x2:4; /* (unused) */
  146. #else
  147. u_char th_x2 : 4, /* (unused) */
  148. th_off:4; /* data offset */
  149. #endif
  150. #define TH_ELN 0x1 /* explicit loss notification */
  151. #define TH_ECN 0x2 /* explicit congestion notification */
  152. #define TH_FS 0x4 /* fast start */
  153. u_char th_flags;
  154. #define TH_FIN 0x01
  155. #define TH_SYN 0x02
  156. #define TH_RST 0x04
  157. #define TH_PUSH 0x08
  158. #define TH_ACK 0x10
  159. #define TH_URG 0x20
  160. #define TH_ECE 0x40
  161. #define TH_CWR 0x80
  162. u_short th_win; /* window */
  163. u_short th_sum; /* checksum */
  164. u_short th_urp; /* urgent pointer */
  165. };
  166. #define ip6_nxt ip6_ctlun.ip6_un1.ip6_un1_nxt
  167. #define ip6_ecn_acc ip6_ctlun.ip6_un3.ip6_un3_ecn
  168. #define ip6_plen ip6_ctlun.ip6_un1.ip6_un1_plen
  169. #define PKT_GET_ETH_HDR(p) \
  170. ((struct eth_header *)(p))
  171. #define PKT_GET_VLAN_HDR(p) \
  172. ((struct vlan_header *) (((uint8_t *)(p)) + sizeof(struct eth_header)))
  173. #define PKT_GET_DVLAN_HDR(p) \
  174. (PKT_GET_VLAN_HDR(p) + 1)
  175. #define PKT_GET_IP_HDR(p) \
  176. ((struct ip_header *)(((uint8_t *)(p)) + eth_get_l2_hdr_length(p)))
  177. #define IP_HDR_GET_LEN(p) \
  178. ((ldub_p(p + offsetof(struct ip_header, ip_ver_len)) & 0x0F) << 2)
  179. #define IP_HDR_GET_P(p) \
  180. (ldub_p(p + offsetof(struct ip_header, ip_p)))
  181. #define PKT_GET_IP_HDR_LEN(p) \
  182. (IP_HDR_GET_LEN(PKT_GET_IP_HDR(p)))
  183. #define PKT_GET_IP6_HDR(p) \
  184. ((struct ip6_header *) (((uint8_t *)(p)) + eth_get_l2_hdr_length(p)))
  185. #define IP_HEADER_VERSION(ip) \
  186. (((ip)->ip_ver_len >> 4) & 0xf)
  187. #define IP4_IS_FRAGMENT(ip) \
  188. ((be16_to_cpu((ip)->ip_off) & (IP_OFFMASK | IP_MF)) != 0)
  189. #define ETH_P_IP (0x0800) /* Internet Protocol packet */
  190. #define ETH_P_ARP (0x0806) /* Address Resolution packet */
  191. #define ETH_P_IPV6 (0x86dd)
  192. #define ETH_P_VLAN (0x8100)
  193. #define ETH_P_DVLAN (0x88a8)
  194. #define ETH_P_NCSI (0x88f8)
  195. #define ETH_P_UNKNOWN (0xffff)
  196. #define VLAN_VID_MASK 0x0fff
  197. #define IP_HEADER_VERSION_4 (4)
  198. #define IP_HEADER_VERSION_6 (6)
  199. #define IP_PROTO_TCP (6)
  200. #define IP_PROTO_UDP (17)
  201. #define IP_PROTO_SCTP (132)
  202. #define IPTOS_ECN_MASK 0x03
  203. #define IPTOS_ECN(x) ((x) & IPTOS_ECN_MASK)
  204. #define IPTOS_ECN_CE 0x03
  205. #define IP6_ECN_MASK 0xC0
  206. #define IP6_ECN(x) ((x) & IP6_ECN_MASK)
  207. #define IP6_ECN_CE 0xC0
  208. #define IP4_DONT_FRAGMENT_FLAG (1 << 14)
  209. #define IS_SPECIAL_VLAN_ID(x) \
  210. (((x) == 0) || ((x) == 0xFFF))
  211. #define ETH_MAX_L2_HDR_LEN \
  212. (sizeof(struct eth_header) + 2 * sizeof(struct vlan_header))
  213. #define ETH_MAX_IP4_HDR_LEN (60)
  214. #define ETH_MAX_IP_DGRAM_LEN (0xFFFF)
  215. #define IP_FRAG_UNIT_SIZE (8)
  216. #define IP_FRAG_ALIGN_SIZE(x) ((x) & ~0x7)
  217. #define IP_RF 0x8000 /* reserved fragment flag */
  218. #define IP_DF 0x4000 /* don't fragment flag */
  219. #define IP_MF 0x2000 /* more fragments flag */
  220. #define IP_OFFMASK 0x1fff /* mask for fragmenting bits */
  221. #define IP6_EXT_GRANULARITY (8) /* Size granularity for
  222. IPv6 extension headers */
  223. /* IP6 extension header types */
  224. #define IP6_HOP_BY_HOP (0)
  225. #define IP6_ROUTING (43)
  226. #define IP6_FRAGMENT (44)
  227. #define IP6_ESP (50)
  228. #define IP6_AUTHENTICATION (51)
  229. #define IP6_NONE (59)
  230. #define IP6_DESTINATON (60)
  231. #define IP6_MOBILITY (135)
  232. static inline int is_multicast_ether_addr(const uint8_t *addr)
  233. {
  234. return 0x01 & addr[0];
  235. }
  236. static inline int is_broadcast_ether_addr(const uint8_t *addr)
  237. {
  238. return (addr[0] & addr[1] & addr[2] & addr[3] & addr[4] & addr[5]) == 0xff;
  239. }
  240. static inline int is_unicast_ether_addr(const uint8_t *addr)
  241. {
  242. return !is_multicast_ether_addr(addr);
  243. }
  244. typedef enum {
  245. ETH_PKT_UCAST = 0xAABBCC00,
  246. ETH_PKT_BCAST,
  247. ETH_PKT_MCAST
  248. } eth_pkt_types_e;
  249. static inline eth_pkt_types_e
  250. get_eth_packet_type(const struct eth_header *ehdr)
  251. {
  252. if (is_broadcast_ether_addr(ehdr->h_dest)) {
  253. return ETH_PKT_BCAST;
  254. } else if (is_multicast_ether_addr(ehdr->h_dest)) {
  255. return ETH_PKT_MCAST;
  256. } else { /* unicast */
  257. return ETH_PKT_UCAST;
  258. }
  259. }
  260. static inline uint32_t
  261. eth_get_l2_hdr_length(const void *p)
  262. {
  263. uint16_t proto = be16_to_cpu(PKT_GET_ETH_HDR(p)->h_proto);
  264. struct vlan_header *hvlan = PKT_GET_VLAN_HDR(p);
  265. switch (proto) {
  266. case ETH_P_VLAN:
  267. return sizeof(struct eth_header) + sizeof(struct vlan_header);
  268. case ETH_P_DVLAN:
  269. if (be16_to_cpu(hvlan->h_proto) == ETH_P_VLAN) {
  270. return sizeof(struct eth_header) + 2 * sizeof(struct vlan_header);
  271. } else {
  272. return sizeof(struct eth_header) + sizeof(struct vlan_header);
  273. }
  274. default:
  275. return sizeof(struct eth_header);
  276. }
  277. }
  278. static inline uint32_t
  279. eth_get_l2_hdr_length_iov(const struct iovec *iov, size_t iovcnt, size_t iovoff)
  280. {
  281. uint8_t p[sizeof(struct eth_header) + sizeof(struct vlan_header)];
  282. size_t copied = iov_to_buf(iov, iovcnt, iovoff, p, ARRAY_SIZE(p));
  283. if (copied < ARRAY_SIZE(p)) {
  284. return copied;
  285. }
  286. return eth_get_l2_hdr_length(p);
  287. }
  288. static inline uint16_t
  289. eth_get_pkt_tci(const void *p)
  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. switch (proto) {
  294. case ETH_P_VLAN:
  295. case ETH_P_DVLAN:
  296. return be16_to_cpu(hvlan->h_tci);
  297. default:
  298. return 0;
  299. }
  300. }
  301. size_t
  302. eth_strip_vlan(const struct iovec *iov, int iovcnt, size_t iovoff,
  303. void *new_ehdr_buf,
  304. uint16_t *payload_offset, uint16_t *tci);
  305. size_t
  306. eth_strip_vlan_ex(const struct iovec *iov, int iovcnt, size_t iovoff, int index,
  307. uint16_t vet, uint16_t vet_ext, void *new_ehdr_buf,
  308. uint16_t *payload_offset, uint16_t *tci);
  309. uint16_t
  310. eth_get_l3_proto(const struct iovec *l2hdr_iov, int iovcnt, size_t l2hdr_len);
  311. void eth_setup_vlan_headers(struct eth_header *ehdr, size_t *ehdr_size,
  312. uint16_t vlan_tag, uint16_t vlan_ethtype);
  313. uint8_t eth_get_gso_type(uint16_t l3_proto, uint8_t *l3_hdr, uint8_t l4proto);
  314. typedef struct eth_ip6_hdr_info_st {
  315. uint8_t l4proto;
  316. size_t full_hdr_len;
  317. struct ip6_header ip6_hdr;
  318. bool has_ext_hdrs;
  319. bool rss_ex_src_valid;
  320. struct in6_address rss_ex_src;
  321. bool rss_ex_dst_valid;
  322. struct in6_address rss_ex_dst;
  323. bool fragment;
  324. } eth_ip6_hdr_info;
  325. typedef struct eth_ip4_hdr_info_st {
  326. struct ip_header ip4_hdr;
  327. bool fragment;
  328. } eth_ip4_hdr_info;
  329. typedef enum EthL4HdrProto {
  330. ETH_L4_HDR_PROTO_INVALID,
  331. ETH_L4_HDR_PROTO_TCP,
  332. ETH_L4_HDR_PROTO_UDP,
  333. ETH_L4_HDR_PROTO_SCTP
  334. } EthL4HdrProto;
  335. typedef struct eth_l4_hdr_info_st {
  336. union {
  337. struct tcp_header tcp;
  338. struct udp_header udp;
  339. } hdr;
  340. EthL4HdrProto proto;
  341. bool has_tcp_data;
  342. } eth_l4_hdr_info;
  343. void eth_get_protocols(const struct iovec *iov, size_t iovcnt, size_t iovoff,
  344. bool *hasip4, bool *hasip6,
  345. size_t *l3hdr_off,
  346. size_t *l4hdr_off,
  347. size_t *l5hdr_off,
  348. eth_ip6_hdr_info *ip6hdr_info,
  349. eth_ip4_hdr_info *ip4hdr_info,
  350. eth_l4_hdr_info *l4hdr_info);
  351. void
  352. eth_fix_ip4_checksum(void *l3hdr, size_t l3hdr_len);
  353. uint32_t
  354. eth_calc_ip4_pseudo_hdr_csum(struct ip_header *iphdr,
  355. uint16_t csl,
  356. uint32_t *cso);
  357. uint32_t
  358. eth_calc_ip6_pseudo_hdr_csum(struct ip6_header *iphdr,
  359. uint16_t csl,
  360. uint8_t l4_proto,
  361. uint32_t *cso);
  362. bool
  363. eth_parse_ipv6_hdr(const struct iovec *pkt, int pkt_frags,
  364. size_t ip6hdr_off, eth_ip6_hdr_info *info);
  365. /**
  366. * eth_pad_short_frame - pad a short frame to the minimum Ethernet frame length
  367. *
  368. * If the Ethernet frame size is shorter than 60 bytes, it will be padded to
  369. * 60 bytes at the address @padded_pkt.
  370. *
  371. * @padded_pkt: buffer address to hold the padded frame
  372. * @padded_buflen: pointer holding length of @padded_pkt. If the frame is
  373. * padded, the length will be updated to the padded one.
  374. * @pkt: address to hold the original Ethernet frame
  375. * @pkt_size: size of the original Ethernet frame
  376. * @return true if the frame is padded, otherwise false
  377. */
  378. bool eth_pad_short_frame(uint8_t *padded_pkt, size_t *padded_buflen,
  379. const void *pkt, size_t pkt_size);
  380. #endif