virtio_net.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_VIRTIO_NET_H
  3. #define _LINUX_VIRTIO_NET_H
  4. #include <linux/if_vlan.h>
  5. #include <uapi/linux/virtio_net.h>
  6. static inline int virtio_net_hdr_to_skb(struct sk_buff *skb,
  7. const struct virtio_net_hdr *hdr,
  8. bool little_endian)
  9. {
  10. unsigned int gso_type = 0;
  11. if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
  12. switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
  13. case VIRTIO_NET_HDR_GSO_TCPV4:
  14. gso_type = SKB_GSO_TCPV4;
  15. break;
  16. case VIRTIO_NET_HDR_GSO_TCPV6:
  17. gso_type = SKB_GSO_TCPV6;
  18. break;
  19. case VIRTIO_NET_HDR_GSO_UDP:
  20. gso_type = SKB_GSO_UDP;
  21. break;
  22. default:
  23. return -EINVAL;
  24. }
  25. if (hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN)
  26. gso_type |= SKB_GSO_TCP_ECN;
  27. if (hdr->gso_size == 0)
  28. return -EINVAL;
  29. }
  30. if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
  31. u16 start = __virtio16_to_cpu(little_endian, hdr->csum_start);
  32. u16 off = __virtio16_to_cpu(little_endian, hdr->csum_offset);
  33. if (!skb_partial_csum_set(skb, start, off))
  34. return -EINVAL;
  35. }
  36. if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
  37. u16 gso_size = __virtio16_to_cpu(little_endian, hdr->gso_size);
  38. skb_shinfo(skb)->gso_size = gso_size;
  39. skb_shinfo(skb)->gso_type = gso_type;
  40. /* Header must be checked, and gso_segs computed. */
  41. skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
  42. skb_shinfo(skb)->gso_segs = 0;
  43. }
  44. return 0;
  45. }
  46. static inline int virtio_net_hdr_from_skb(const struct sk_buff *skb,
  47. struct virtio_net_hdr *hdr,
  48. bool little_endian,
  49. bool has_data_valid)
  50. {
  51. memset(hdr, 0, sizeof(*hdr)); /* no info leak */
  52. if (skb_is_gso(skb)) {
  53. struct skb_shared_info *sinfo = skb_shinfo(skb);
  54. /* This is a hint as to how much should be linear. */
  55. hdr->hdr_len = __cpu_to_virtio16(little_endian,
  56. skb_headlen(skb));
  57. hdr->gso_size = __cpu_to_virtio16(little_endian,
  58. sinfo->gso_size);
  59. if (sinfo->gso_type & SKB_GSO_TCPV4)
  60. hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
  61. else if (sinfo->gso_type & SKB_GSO_TCPV6)
  62. hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
  63. else
  64. return -EINVAL;
  65. if (sinfo->gso_type & SKB_GSO_TCP_ECN)
  66. hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
  67. } else
  68. hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
  69. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  70. hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
  71. if (skb_vlan_tag_present(skb))
  72. hdr->csum_start = __cpu_to_virtio16(little_endian,
  73. skb_checksum_start_offset(skb) + VLAN_HLEN);
  74. else
  75. hdr->csum_start = __cpu_to_virtio16(little_endian,
  76. skb_checksum_start_offset(skb));
  77. hdr->csum_offset = __cpu_to_virtio16(little_endian,
  78. skb->csum_offset);
  79. } else if (has_data_valid &&
  80. skb->ip_summed == CHECKSUM_UNNECESSARY) {
  81. hdr->flags = VIRTIO_NET_HDR_F_DATA_VALID;
  82. } /* else everything is zero */
  83. return 0;
  84. }
  85. #endif /* _LINUX_VIRTIO_NET_H */