offload.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * sctp_offload - GRO/GSO Offloading for SCTP
  3. *
  4. * Copyright (C) 2015, Marcelo Ricardo Leitner <marcelo.leitner@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  17. #include <linux/kernel.h>
  18. #include <linux/kprobes.h>
  19. #include <linux/socket.h>
  20. #include <linux/sctp.h>
  21. #include <linux/proc_fs.h>
  22. #include <linux/vmalloc.h>
  23. #include <linux/module.h>
  24. #include <linux/kfifo.h>
  25. #include <linux/time.h>
  26. #include <net/net_namespace.h>
  27. #include <linux/skbuff.h>
  28. #include <net/sctp/sctp.h>
  29. #include <net/sctp/checksum.h>
  30. #include <net/protocol.h>
  31. static __le32 sctp_gso_make_checksum(struct sk_buff *skb)
  32. {
  33. skb->ip_summed = CHECKSUM_NONE;
  34. skb->csum_not_inet = 0;
  35. return sctp_compute_cksum(skb, skb_transport_offset(skb));
  36. }
  37. static struct sk_buff *sctp_gso_segment(struct sk_buff *skb,
  38. netdev_features_t features)
  39. {
  40. struct sk_buff *segs = ERR_PTR(-EINVAL);
  41. struct sctphdr *sh;
  42. sh = sctp_hdr(skb);
  43. if (!pskb_may_pull(skb, sizeof(*sh)))
  44. goto out;
  45. __skb_pull(skb, sizeof(*sh));
  46. if (skb_gso_ok(skb, features | NETIF_F_GSO_ROBUST)) {
  47. /* Packet is from an untrusted source, reset gso_segs. */
  48. struct skb_shared_info *pinfo = skb_shinfo(skb);
  49. struct sk_buff *frag_iter;
  50. pinfo->gso_segs = 0;
  51. if (skb->len != skb->data_len) {
  52. /* Means we have chunks in here too */
  53. pinfo->gso_segs++;
  54. }
  55. skb_walk_frags(skb, frag_iter)
  56. pinfo->gso_segs++;
  57. segs = NULL;
  58. goto out;
  59. }
  60. segs = skb_segment(skb, features | NETIF_F_HW_CSUM | NETIF_F_SG);
  61. if (IS_ERR(segs))
  62. goto out;
  63. /* All that is left is update SCTP CRC if necessary */
  64. if (!(features & NETIF_F_SCTP_CRC)) {
  65. for (skb = segs; skb; skb = skb->next) {
  66. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  67. sh = sctp_hdr(skb);
  68. sh->checksum = sctp_gso_make_checksum(skb);
  69. }
  70. }
  71. }
  72. out:
  73. return segs;
  74. }
  75. static const struct net_offload sctp_offload = {
  76. .callbacks = {
  77. .gso_segment = sctp_gso_segment,
  78. },
  79. };
  80. static const struct net_offload sctp6_offload = {
  81. .callbacks = {
  82. .gso_segment = sctp_gso_segment,
  83. },
  84. };
  85. static const struct skb_checksum_ops crc32c_csum_ops = {
  86. .update = sctp_csum_update,
  87. .combine = sctp_csum_combine,
  88. };
  89. int __init sctp_offload_init(void)
  90. {
  91. int ret;
  92. ret = inet_add_offload(&sctp_offload, IPPROTO_SCTP);
  93. if (ret)
  94. goto out;
  95. ret = inet6_add_offload(&sctp6_offload, IPPROTO_SCTP);
  96. if (ret)
  97. goto ipv4;
  98. crc32c_csum_stub = &crc32c_csum_ops;
  99. return ret;
  100. ipv4:
  101. inet_del_offload(&sctp_offload, IPPROTO_SCTP);
  102. out:
  103. return ret;
  104. }