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