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> |
Willem de Bruijn | 9274124 | 2020-05-04 12:48:54 -0400 | [diff] [blame] | 6 | #include <uapi/linux/tcp.h> |
| 7 | #include <uapi/linux/udp.h> |
Mike Rapoport | fd2a043 | 2016-06-08 16:09:18 +0300 | [diff] [blame] | 8 | #include <uapi/linux/virtio_net.h> |
| 9 | |
Jianfeng Tan | 9d2f67e | 2018-09-29 15:41:27 +0000 | [diff] [blame] | 10 | static inline int virtio_net_hdr_set_proto(struct sk_buff *skb, |
| 11 | const struct virtio_net_hdr *hdr) |
| 12 | { |
| 13 | switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) { |
| 14 | case VIRTIO_NET_HDR_GSO_TCPV4: |
| 15 | case VIRTIO_NET_HDR_GSO_UDP: |
| 16 | skb->protocol = cpu_to_be16(ETH_P_IP); |
| 17 | break; |
| 18 | case VIRTIO_NET_HDR_GSO_TCPV6: |
| 19 | skb->protocol = cpu_to_be16(ETH_P_IPV6); |
| 20 | break; |
| 21 | default: |
| 22 | return -EINVAL; |
| 23 | } |
| 24 | |
| 25 | return 0; |
| 26 | } |
| 27 | |
Mike Rapoport | fd2a043 | 2016-06-08 16:09:18 +0300 | [diff] [blame] | 28 | static inline int virtio_net_hdr_to_skb(struct sk_buff *skb, |
| 29 | const struct virtio_net_hdr *hdr, |
| 30 | bool little_endian) |
| 31 | { |
Willem de Bruijn | 0c19f846 | 2017-11-21 10:22:25 -0500 | [diff] [blame] | 32 | unsigned int gso_type = 0; |
Willem de Bruijn | 9274124 | 2020-05-04 12:48:54 -0400 | [diff] [blame] | 33 | unsigned int thlen = 0; |
Willem de Bruijn | 6dd912f | 2020-05-25 15:07:40 -0400 | [diff] [blame] | 34 | unsigned int p_off = 0; |
Willem de Bruijn | 9274124 | 2020-05-04 12:48:54 -0400 | [diff] [blame] | 35 | unsigned int ip_proto; |
Mike Rapoport | fd2a043 | 2016-06-08 16:09:18 +0300 | [diff] [blame] | 36 | |
| 37 | if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) { |
| 38 | switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) { |
| 39 | case VIRTIO_NET_HDR_GSO_TCPV4: |
| 40 | gso_type = SKB_GSO_TCPV4; |
Willem de Bruijn | 9274124 | 2020-05-04 12:48:54 -0400 | [diff] [blame] | 41 | ip_proto = IPPROTO_TCP; |
| 42 | thlen = sizeof(struct tcphdr); |
Mike Rapoport | fd2a043 | 2016-06-08 16:09:18 +0300 | [diff] [blame] | 43 | break; |
| 44 | case VIRTIO_NET_HDR_GSO_TCPV6: |
| 45 | gso_type = SKB_GSO_TCPV6; |
Willem de Bruijn | 9274124 | 2020-05-04 12:48:54 -0400 | [diff] [blame] | 46 | ip_proto = IPPROTO_TCP; |
| 47 | thlen = sizeof(struct tcphdr); |
Mike Rapoport | fd2a043 | 2016-06-08 16:09:18 +0300 | [diff] [blame] | 48 | break; |
Willem de Bruijn | 0c19f846 | 2017-11-21 10:22:25 -0500 | [diff] [blame] | 49 | case VIRTIO_NET_HDR_GSO_UDP: |
| 50 | gso_type = SKB_GSO_UDP; |
Willem de Bruijn | 9274124 | 2020-05-04 12:48:54 -0400 | [diff] [blame] | 51 | ip_proto = IPPROTO_UDP; |
| 52 | thlen = sizeof(struct udphdr); |
Willem de Bruijn | 0c19f846 | 2017-11-21 10:22:25 -0500 | [diff] [blame] | 53 | break; |
Mike Rapoport | fd2a043 | 2016-06-08 16:09:18 +0300 | [diff] [blame] | 54 | default: |
| 55 | return -EINVAL; |
| 56 | } |
| 57 | |
| 58 | if (hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN) |
| 59 | gso_type |= SKB_GSO_TCP_ECN; |
| 60 | |
| 61 | if (hdr->gso_size == 0) |
| 62 | return -EINVAL; |
| 63 | } |
| 64 | |
| 65 | if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) { |
| 66 | u16 start = __virtio16_to_cpu(little_endian, hdr->csum_start); |
| 67 | u16 off = __virtio16_to_cpu(little_endian, hdr->csum_offset); |
| 68 | |
| 69 | if (!skb_partial_csum_set(skb, start, off)) |
| 70 | return -EINVAL; |
Willem de Bruijn | 9274124 | 2020-05-04 12:48:54 -0400 | [diff] [blame] | 71 | |
Willem de Bruijn | 6dd912f | 2020-05-25 15:07:40 -0400 | [diff] [blame] | 72 | p_off = skb_transport_offset(skb) + thlen; |
| 73 | if (p_off > skb_headlen(skb)) |
Willem de Bruijn | 9274124 | 2020-05-04 12:48:54 -0400 | [diff] [blame] | 74 | return -EINVAL; |
Willem de Bruijn | d5be7f6 | 2019-02-15 12:15:47 -0500 | [diff] [blame] | 75 | } else { |
| 76 | /* gso packets without NEEDS_CSUM do not set transport_offset. |
| 77 | * probe and drop if does not match one of the above types. |
| 78 | */ |
Willem de Bruijn | 9e8db59 | 2019-02-18 23:37:12 -0500 | [diff] [blame] | 79 | if (gso_type && skb->network_header) { |
Willem de Bruijn | 9274124 | 2020-05-04 12:48:54 -0400 | [diff] [blame] | 80 | struct flow_keys_basic keys; |
| 81 | |
Willem de Bruijn | 9e8db59 | 2019-02-18 23:37:12 -0500 | [diff] [blame] | 82 | if (!skb->protocol) |
| 83 | virtio_net_hdr_set_proto(skb, hdr); |
| 84 | retry: |
Willem de Bruijn | 9274124 | 2020-05-04 12:48:54 -0400 | [diff] [blame] | 85 | if (!skb_flow_dissect_flow_keys_basic(NULL, skb, &keys, |
| 86 | NULL, 0, 0, 0, |
| 87 | 0)) { |
Willem de Bruijn | 9e8db59 | 2019-02-18 23:37:12 -0500 | [diff] [blame] | 88 | /* UFO does not specify ipv4 or 6: try both */ |
| 89 | if (gso_type & SKB_GSO_UDP && |
| 90 | skb->protocol == htons(ETH_P_IP)) { |
| 91 | skb->protocol = htons(ETH_P_IPV6); |
| 92 | goto retry; |
| 93 | } |
Willem de Bruijn | d5be7f6 | 2019-02-15 12:15:47 -0500 | [diff] [blame] | 94 | return -EINVAL; |
Willem de Bruijn | 9e8db59 | 2019-02-18 23:37:12 -0500 | [diff] [blame] | 95 | } |
Willem de Bruijn | 9274124 | 2020-05-04 12:48:54 -0400 | [diff] [blame] | 96 | |
Willem de Bruijn | 6dd912f | 2020-05-25 15:07:40 -0400 | [diff] [blame] | 97 | p_off = keys.control.thoff + thlen; |
| 98 | if (p_off > skb_headlen(skb) || |
Willem de Bruijn | 9274124 | 2020-05-04 12:48:54 -0400 | [diff] [blame] | 99 | keys.basic.ip_proto != ip_proto) |
| 100 | return -EINVAL; |
| 101 | |
| 102 | skb_set_transport_header(skb, keys.control.thoff); |
Willem de Bruijn | 6dd912f | 2020-05-25 15:07:40 -0400 | [diff] [blame] | 103 | } else if (gso_type) { |
| 104 | p_off = thlen; |
| 105 | if (p_off > skb_headlen(skb)) |
| 106 | return -EINVAL; |
Willem de Bruijn | d5be7f6 | 2019-02-15 12:15:47 -0500 | [diff] [blame] | 107 | } |
Mike Rapoport | fd2a043 | 2016-06-08 16:09:18 +0300 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) { |
| 111 | u16 gso_size = __virtio16_to_cpu(little_endian, hdr->gso_size); |
Eric Dumazet | 7c6d2ec | 2020-05-28 14:57:47 -0700 | [diff] [blame^] | 112 | struct skb_shared_info *shinfo = skb_shinfo(skb); |
Mike Rapoport | fd2a043 | 2016-06-08 16:09:18 +0300 | [diff] [blame] | 113 | |
Eric Dumazet | 7c6d2ec | 2020-05-28 14:57:47 -0700 | [diff] [blame^] | 114 | /* Too small packets are not really GSO ones. */ |
| 115 | if (skb->len - p_off > gso_size) { |
| 116 | shinfo->gso_size = gso_size; |
| 117 | shinfo->gso_type = gso_type; |
Willem de Bruijn | 6dd912f | 2020-05-25 15:07:40 -0400 | [diff] [blame] | 118 | |
Eric Dumazet | 7c6d2ec | 2020-05-28 14:57:47 -0700 | [diff] [blame^] | 119 | /* Header must be checked, and gso_segs computed. */ |
| 120 | shinfo->gso_type |= SKB_GSO_DODGY; |
| 121 | shinfo->gso_segs = 0; |
| 122 | } |
Mike Rapoport | fd2a043 | 2016-06-08 16:09:18 +0300 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | static inline int virtio_net_hdr_from_skb(const struct sk_buff *skb, |
| 129 | struct virtio_net_hdr *hdr, |
Jason Wang | 6391a44 | 2017-01-20 14:32:42 +0800 | [diff] [blame] | 130 | bool little_endian, |
Willem de Bruijn | fd3a886 | 2018-06-06 11:23:01 -0400 | [diff] [blame] | 131 | bool has_data_valid, |
| 132 | int vlan_hlen) |
Mike Rapoport | fd2a043 | 2016-06-08 16:09:18 +0300 | [diff] [blame] | 133 | { |
Jarno Rajahalme | 9403cd7 | 2016-11-18 15:40:40 -0800 | [diff] [blame] | 134 | memset(hdr, 0, sizeof(*hdr)); /* no info leak */ |
Mike Rapoport | fd2a043 | 2016-06-08 16:09:18 +0300 | [diff] [blame] | 135 | |
| 136 | if (skb_is_gso(skb)) { |
| 137 | struct skb_shared_info *sinfo = skb_shinfo(skb); |
| 138 | |
| 139 | /* This is a hint as to how much should be linear. */ |
| 140 | hdr->hdr_len = __cpu_to_virtio16(little_endian, |
| 141 | skb_headlen(skb)); |
| 142 | hdr->gso_size = __cpu_to_virtio16(little_endian, |
| 143 | sinfo->gso_size); |
| 144 | if (sinfo->gso_type & SKB_GSO_TCPV4) |
| 145 | hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4; |
| 146 | else if (sinfo->gso_type & SKB_GSO_TCPV6) |
| 147 | hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6; |
Mike Rapoport | fd2a043 | 2016-06-08 16:09:18 +0300 | [diff] [blame] | 148 | else |
| 149 | return -EINVAL; |
| 150 | if (sinfo->gso_type & SKB_GSO_TCP_ECN) |
| 151 | hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN; |
| 152 | } else |
| 153 | hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE; |
| 154 | |
| 155 | if (skb->ip_summed == CHECKSUM_PARTIAL) { |
| 156 | hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM; |
Willem de Bruijn | fd3a886 | 2018-06-06 11:23:01 -0400 | [diff] [blame] | 157 | hdr->csum_start = __cpu_to_virtio16(little_endian, |
| 158 | skb_checksum_start_offset(skb) + vlan_hlen); |
Mike Rapoport | fd2a043 | 2016-06-08 16:09:18 +0300 | [diff] [blame] | 159 | hdr->csum_offset = __cpu_to_virtio16(little_endian, |
| 160 | skb->csum_offset); |
Jason Wang | 6391a44 | 2017-01-20 14:32:42 +0800 | [diff] [blame] | 161 | } else if (has_data_valid && |
| 162 | skb->ip_summed == CHECKSUM_UNNECESSARY) { |
| 163 | hdr->flags = VIRTIO_NET_HDR_F_DATA_VALID; |
Mike Rapoport | fd2a043 | 2016-06-08 16:09:18 +0300 | [diff] [blame] | 164 | } /* else everything is zero */ |
| 165 | |
| 166 | return 0; |
| 167 | } |
| 168 | |
Jarno Rajahalme | d66016a | 2016-11-18 15:40:39 -0800 | [diff] [blame] | 169 | #endif /* _LINUX_VIRTIO_NET_H */ |