esp4_offload.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. #include <linux/skbuff.h>
  15. #include <linux/init.h>
  16. #include <net/protocol.h>
  17. #include <crypto/aead.h>
  18. #include <crypto/authenc.h>
  19. #include <linux/err.h>
  20. #include <linux/module.h>
  21. #include <net/ip.h>
  22. #include <net/xfrm.h>
  23. #include <net/esp.h>
  24. #include <linux/scatterlist.h>
  25. #include <linux/kernel.h>
  26. #include <linux/slab.h>
  27. #include <linux/spinlock.h>
  28. #include <net/udp.h>
  29. static struct sk_buff *esp4_gro_receive(struct list_head *head,
  30. struct sk_buff *skb)
  31. {
  32. int offset = skb_gro_offset(skb);
  33. struct xfrm_offload *xo;
  34. struct xfrm_state *x;
  35. __be32 seq;
  36. __be32 spi;
  37. int err;
  38. if (!pskb_pull(skb, offset))
  39. return NULL;
  40. if ((err = xfrm_parse_spi(skb, IPPROTO_ESP, &spi, &seq)) != 0)
  41. goto out;
  42. xo = xfrm_offload(skb);
  43. if (!xo || !(xo->flags & CRYPTO_DONE)) {
  44. err = secpath_set(skb);
  45. if (err)
  46. goto out;
  47. if (skb->sp->len == XFRM_MAX_DEPTH)
  48. goto out;
  49. x = xfrm_state_lookup(dev_net(skb->dev), skb->mark,
  50. (xfrm_address_t *)&ip_hdr(skb)->daddr,
  51. spi, IPPROTO_ESP, AF_INET);
  52. if (!x)
  53. goto out;
  54. skb->sp->xvec[skb->sp->len++] = x;
  55. skb->sp->olen++;
  56. xo = xfrm_offload(skb);
  57. if (!xo) {
  58. xfrm_state_put(x);
  59. goto out;
  60. }
  61. }
  62. xo->flags |= XFRM_GRO;
  63. XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = NULL;
  64. XFRM_SPI_SKB_CB(skb)->family = AF_INET;
  65. XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct iphdr, daddr);
  66. XFRM_SPI_SKB_CB(skb)->seq = seq;
  67. /* We don't need to handle errors from xfrm_input, it does all
  68. * the error handling and frees the resources on error. */
  69. xfrm_input(skb, IPPROTO_ESP, spi, -2);
  70. return ERR_PTR(-EINPROGRESS);
  71. out:
  72. skb_push(skb, offset);
  73. NAPI_GRO_CB(skb)->same_flow = 0;
  74. NAPI_GRO_CB(skb)->flush = 1;
  75. return NULL;
  76. }
  77. static void esp4_gso_encap(struct xfrm_state *x, struct sk_buff *skb)
  78. {
  79. struct ip_esp_hdr *esph;
  80. struct iphdr *iph = ip_hdr(skb);
  81. struct xfrm_offload *xo = xfrm_offload(skb);
  82. int proto = iph->protocol;
  83. skb_push(skb, -skb_network_offset(skb));
  84. esph = ip_esp_hdr(skb);
  85. *skb_mac_header(skb) = IPPROTO_ESP;
  86. esph->spi = x->id.spi;
  87. esph->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output.low);
  88. xo->proto = proto;
  89. }
  90. static struct sk_buff *esp4_gso_segment(struct sk_buff *skb,
  91. netdev_features_t features)
  92. {
  93. struct xfrm_state *x;
  94. struct ip_esp_hdr *esph;
  95. struct crypto_aead *aead;
  96. netdev_features_t esp_features = features;
  97. struct xfrm_offload *xo = xfrm_offload(skb);
  98. if (!xo)
  99. return ERR_PTR(-EINVAL);
  100. if (!(skb_shinfo(skb)->gso_type & SKB_GSO_ESP))
  101. return ERR_PTR(-EINVAL);
  102. x = skb->sp->xvec[skb->sp->len - 1];
  103. aead = x->data;
  104. esph = ip_esp_hdr(skb);
  105. if (esph->spi != x->id.spi)
  106. return ERR_PTR(-EINVAL);
  107. if (!pskb_may_pull(skb, sizeof(*esph) + crypto_aead_ivsize(aead)))
  108. return ERR_PTR(-EINVAL);
  109. __skb_pull(skb, sizeof(*esph) + crypto_aead_ivsize(aead));
  110. skb->encap_hdr_csum = 1;
  111. if (!(features & NETIF_F_HW_ESP) || x->xso.dev != skb->dev)
  112. esp_features = features & ~(NETIF_F_SG | NETIF_F_CSUM_MASK);
  113. else if (!(features & NETIF_F_HW_ESP_TX_CSUM))
  114. esp_features = features & ~NETIF_F_CSUM_MASK;
  115. xo->flags |= XFRM_GSO_SEGMENT;
  116. return x->outer_mode->gso_segment(x, skb, esp_features);
  117. }
  118. static int esp_input_tail(struct xfrm_state *x, struct sk_buff *skb)
  119. {
  120. struct crypto_aead *aead = x->data;
  121. struct xfrm_offload *xo = xfrm_offload(skb);
  122. if (!pskb_may_pull(skb, sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead)))
  123. return -EINVAL;
  124. if (!(xo->flags & CRYPTO_DONE))
  125. skb->ip_summed = CHECKSUM_NONE;
  126. return esp_input_done2(skb, 0);
  127. }
  128. static int esp_xmit(struct xfrm_state *x, struct sk_buff *skb, netdev_features_t features)
  129. {
  130. int err;
  131. int alen;
  132. int blksize;
  133. struct xfrm_offload *xo;
  134. struct ip_esp_hdr *esph;
  135. struct crypto_aead *aead;
  136. struct esp_info esp;
  137. bool hw_offload = true;
  138. __u32 seq;
  139. esp.inplace = true;
  140. xo = xfrm_offload(skb);
  141. if (!xo)
  142. return -EINVAL;
  143. if (!(features & NETIF_F_HW_ESP) || x->xso.dev != skb->dev) {
  144. xo->flags |= CRYPTO_FALLBACK;
  145. hw_offload = false;
  146. }
  147. esp.proto = xo->proto;
  148. /* skb is pure payload to encrypt */
  149. aead = x->data;
  150. alen = crypto_aead_authsize(aead);
  151. esp.tfclen = 0;
  152. /* XXX: Add support for tfc padding here. */
  153. blksize = ALIGN(crypto_aead_blocksize(aead), 4);
  154. esp.clen = ALIGN(skb->len + 2 + esp.tfclen, blksize);
  155. esp.plen = esp.clen - skb->len - esp.tfclen;
  156. esp.tailen = esp.tfclen + esp.plen + alen;
  157. esp.esph = ip_esp_hdr(skb);
  158. if (!hw_offload || (hw_offload && !skb_is_gso(skb))) {
  159. esp.nfrags = esp_output_head(x, skb, &esp);
  160. if (esp.nfrags < 0)
  161. return esp.nfrags;
  162. }
  163. seq = xo->seq.low;
  164. esph = esp.esph;
  165. esph->spi = x->id.spi;
  166. skb_push(skb, -skb_network_offset(skb));
  167. if (xo->flags & XFRM_GSO_SEGMENT) {
  168. esph->seq_no = htonl(seq);
  169. if (!skb_is_gso(skb))
  170. xo->seq.low++;
  171. else
  172. xo->seq.low += skb_shinfo(skb)->gso_segs;
  173. }
  174. esp.seqno = cpu_to_be64(seq + ((u64)xo->seq.hi << 32));
  175. ip_hdr(skb)->tot_len = htons(skb->len);
  176. ip_send_check(ip_hdr(skb));
  177. if (hw_offload)
  178. return 0;
  179. err = esp_output_tail(x, skb, &esp);
  180. if (err)
  181. return err;
  182. secpath_reset(skb);
  183. return 0;
  184. }
  185. static const struct net_offload esp4_offload = {
  186. .callbacks = {
  187. .gro_receive = esp4_gro_receive,
  188. .gso_segment = esp4_gso_segment,
  189. },
  190. };
  191. static const struct xfrm_type_offload esp_type_offload = {
  192. .description = "ESP4 OFFLOAD",
  193. .owner = THIS_MODULE,
  194. .proto = IPPROTO_ESP,
  195. .input_tail = esp_input_tail,
  196. .xmit = esp_xmit,
  197. .encap = esp4_gso_encap,
  198. };
  199. static int __init esp4_offload_init(void)
  200. {
  201. if (xfrm_register_type_offload(&esp_type_offload, AF_INET) < 0) {
  202. pr_info("%s: can't add xfrm type offload\n", __func__);
  203. return -EAGAIN;
  204. }
  205. return inet_add_offload(&esp4_offload, IPPROTO_ESP);
  206. }
  207. static void __exit esp4_offload_exit(void)
  208. {
  209. if (xfrm_unregister_type_offload(&esp_type_offload, AF_INET) < 0)
  210. pr_info("%s: can't remove xfrm type offload\n", __func__);
  211. inet_del_offload(&esp4_offload, IPPROTO_ESP);
  212. }
  213. module_init(esp4_offload_init);
  214. module_exit(esp4_offload_exit);
  215. MODULE_LICENSE("GPL");
  216. MODULE_AUTHOR("Steffen Klassert <steffen.klassert@secunet.com>");
  217. MODULE_ALIAS_XFRM_OFFLOAD_TYPE(AF_INET, XFRM_PROTO_ESP);