esp4_offload.c 6.6 KB

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