virtio_net.h 2.8 KB

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