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