1 /* 2 * IPV4 GSO/GRO offload support 3 * Linux INET implementation 4 * 5 * Copyright (C) 2016 secunet Security Networks AG 6 * Author: Steffen Klassert <steffen.klassert@secunet.com> 7 * 8 * This program is free software; you can redistribute it and/or modify it 9 * under the terms and conditions of the GNU General Public License, 10 * version 2, as published by the Free Software Foundation. 11 * 12 * ESP GRO support 13 */ 14 15 #include <linux/skbuff.h> 16 #include <linux/init.h> 17 #include <net/protocol.h> 18 #include <crypto/aead.h> 19 #include <crypto/authenc.h> 20 #include <linux/err.h> 21 #include <linux/module.h> 22 #include <net/ip.h> 23 #include <net/xfrm.h> 24 #include <net/esp.h> 25 #include <linux/scatterlist.h> 26 #include <linux/kernel.h> 27 #include <linux/slab.h> 28 #include <linux/spinlock.h> 29 #include <net/udp.h> 30 31 static struct sk_buff *esp4_gro_receive(struct list_head *head, 32 struct sk_buff *skb) 33 { 34 int offset = skb_gro_offset(skb); 35 struct xfrm_offload *xo; 36 struct xfrm_state *x; 37 __be32 seq; 38 __be32 spi; 39 int err; 40 41 if (!pskb_pull(skb, offset)) 42 return NULL; 43 44 if ((err = xfrm_parse_spi(skb, IPPROTO_ESP, &spi, &seq)) != 0) 45 goto out; 46 47 xo = xfrm_offload(skb); 48 if (!xo || !(xo->flags & CRYPTO_DONE)) { 49 struct sec_path *sp = secpath_set(skb); 50 51 if (!sp) 52 goto out; 53 54 if (sp->len == XFRM_MAX_DEPTH) 55 goto out_reset; 56 57 x = xfrm_state_lookup(dev_net(skb->dev), skb->mark, 58 (xfrm_address_t *)&ip_hdr(skb)->daddr, 59 spi, IPPROTO_ESP, AF_INET); 60 if (!x) 61 goto out_reset; 62 63 sp->xvec[sp->len++] = x; 64 sp->olen++; 65 66 xo = xfrm_offload(skb); 67 if (!xo) { 68 xfrm_state_put(x); 69 goto out_reset; 70 } 71 } 72 73 xo->flags |= XFRM_GRO; 74 75 XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = NULL; 76 XFRM_SPI_SKB_CB(skb)->family = AF_INET; 77 XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct iphdr, daddr); 78 XFRM_SPI_SKB_CB(skb)->seq = seq; 79 80 /* We don't need to handle errors from xfrm_input, it does all 81 * the error handling and frees the resources on error. */ 82 xfrm_input(skb, IPPROTO_ESP, spi, -2); 83 84 return ERR_PTR(-EINPROGRESS); 85 out_reset: 86 secpath_reset(skb); 87 out: 88 skb_push(skb, offset); 89 NAPI_GRO_CB(skb)->same_flow = 0; 90 NAPI_GRO_CB(skb)->flush = 1; 91 92 return NULL; 93 } 94 95 static void esp4_gso_encap(struct xfrm_state *x, struct sk_buff *skb) 96 { 97 struct ip_esp_hdr *esph; 98 struct iphdr *iph = ip_hdr(skb); 99 struct xfrm_offload *xo = xfrm_offload(skb); 100 int proto = iph->protocol; 101 102 skb_push(skb, -skb_network_offset(skb)); 103 esph = ip_esp_hdr(skb); 104 *skb_mac_header(skb) = IPPROTO_ESP; 105 106 esph->spi = x->id.spi; 107 esph->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output.low); 108 109 xo->proto = proto; 110 } 111 112 static struct sk_buff *esp4_gso_segment(struct sk_buff *skb, 113 netdev_features_t features) 114 { 115 struct xfrm_state *x; 116 struct ip_esp_hdr *esph; 117 struct crypto_aead *aead; 118 netdev_features_t esp_features = features; 119 struct xfrm_offload *xo = xfrm_offload(skb); 120 struct sec_path *sp; 121 122 if (!xo) 123 return ERR_PTR(-EINVAL); 124 125 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_ESP)) 126 return ERR_PTR(-EINVAL); 127 128 sp = skb_sec_path(skb); 129 x = sp->xvec[sp->len - 1]; 130 aead = x->data; 131 esph = ip_esp_hdr(skb); 132 133 if (esph->spi != x->id.spi) 134 return ERR_PTR(-EINVAL); 135 136 if (!pskb_may_pull(skb, sizeof(*esph) + crypto_aead_ivsize(aead))) 137 return ERR_PTR(-EINVAL); 138 139 __skb_pull(skb, sizeof(*esph) + crypto_aead_ivsize(aead)); 140 141 skb->encap_hdr_csum = 1; 142 143 if (!(features & NETIF_F_HW_ESP) || x->xso.dev != skb->dev) 144 esp_features = features & ~(NETIF_F_SG | NETIF_F_CSUM_MASK); 145 else if (!(features & NETIF_F_HW_ESP_TX_CSUM)) 146 esp_features = features & ~NETIF_F_CSUM_MASK; 147 148 xo->flags |= XFRM_GSO_SEGMENT; 149 150 return x->outer_mode->gso_segment(x, skb, esp_features); 151 } 152 153 static int esp_input_tail(struct xfrm_state *x, struct sk_buff *skb) 154 { 155 struct crypto_aead *aead = x->data; 156 struct xfrm_offload *xo = xfrm_offload(skb); 157 158 if (!pskb_may_pull(skb, sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead))) 159 return -EINVAL; 160 161 if (!(xo->flags & CRYPTO_DONE)) 162 skb->ip_summed = CHECKSUM_NONE; 163 164 return esp_input_done2(skb, 0); 165 } 166 167 static int esp_xmit(struct xfrm_state *x, struct sk_buff *skb, netdev_features_t features) 168 { 169 int err; 170 int alen; 171 int blksize; 172 struct xfrm_offload *xo; 173 struct ip_esp_hdr *esph; 174 struct crypto_aead *aead; 175 struct esp_info esp; 176 bool hw_offload = true; 177 __u32 seq; 178 179 esp.inplace = true; 180 181 xo = xfrm_offload(skb); 182 183 if (!xo) 184 return -EINVAL; 185 186 if (!(features & NETIF_F_HW_ESP) || x->xso.dev != skb->dev) { 187 xo->flags |= CRYPTO_FALLBACK; 188 hw_offload = false; 189 } 190 191 esp.proto = xo->proto; 192 193 /* skb is pure payload to encrypt */ 194 195 aead = x->data; 196 alen = crypto_aead_authsize(aead); 197 198 esp.tfclen = 0; 199 /* XXX: Add support for tfc padding here. */ 200 201 blksize = ALIGN(crypto_aead_blocksize(aead), 4); 202 esp.clen = ALIGN(skb->len + 2 + esp.tfclen, blksize); 203 esp.plen = esp.clen - skb->len - esp.tfclen; 204 esp.tailen = esp.tfclen + esp.plen + alen; 205 206 esp.esph = ip_esp_hdr(skb); 207 208 209 if (!hw_offload || (hw_offload && !skb_is_gso(skb))) { 210 esp.nfrags = esp_output_head(x, skb, &esp); 211 if (esp.nfrags < 0) 212 return esp.nfrags; 213 } 214 215 seq = xo->seq.low; 216 217 esph = esp.esph; 218 esph->spi = x->id.spi; 219 220 skb_push(skb, -skb_network_offset(skb)); 221 222 if (xo->flags & XFRM_GSO_SEGMENT) { 223 esph->seq_no = htonl(seq); 224 225 if (!skb_is_gso(skb)) 226 xo->seq.low++; 227 else 228 xo->seq.low += skb_shinfo(skb)->gso_segs; 229 } 230 231 esp.seqno = cpu_to_be64(seq + ((u64)xo->seq.hi << 32)); 232 233 ip_hdr(skb)->tot_len = htons(skb->len); 234 ip_send_check(ip_hdr(skb)); 235 236 if (hw_offload) 237 return 0; 238 239 err = esp_output_tail(x, skb, &esp); 240 if (err) 241 return err; 242 243 secpath_reset(skb); 244 245 return 0; 246 } 247 248 static const struct net_offload esp4_offload = { 249 .callbacks = { 250 .gro_receive = esp4_gro_receive, 251 .gso_segment = esp4_gso_segment, 252 }, 253 }; 254 255 static const struct xfrm_type_offload esp_type_offload = { 256 .description = "ESP4 OFFLOAD", 257 .owner = THIS_MODULE, 258 .proto = IPPROTO_ESP, 259 .input_tail = esp_input_tail, 260 .xmit = esp_xmit, 261 .encap = esp4_gso_encap, 262 }; 263 264 static int __init esp4_offload_init(void) 265 { 266 if (xfrm_register_type_offload(&esp_type_offload, AF_INET) < 0) { 267 pr_info("%s: can't add xfrm type offload\n", __func__); 268 return -EAGAIN; 269 } 270 271 return inet_add_offload(&esp4_offload, IPPROTO_ESP); 272 } 273 274 static void __exit esp4_offload_exit(void) 275 { 276 if (xfrm_unregister_type_offload(&esp_type_offload, AF_INET) < 0) 277 pr_info("%s: can't remove xfrm type offload\n", __func__); 278 279 inet_del_offload(&esp4_offload, IPPROTO_ESP); 280 } 281 282 module_init(esp4_offload_init); 283 module_exit(esp4_offload_exit); 284 MODULE_LICENSE("GPL"); 285 MODULE_AUTHOR("Steffen Klassert <steffen.klassert@secunet.com>"); 286 MODULE_ALIAS_XFRM_OFFLOAD_TYPE(AF_INET, XFRM_PROTO_ESP); 287