offload.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. return sctp_compute_cksum(skb, skb_transport_offset(skb));
  35. }
  36. static struct sk_buff *sctp_gso_segment(struct sk_buff *skb,
  37. netdev_features_t features)
  38. {
  39. struct sk_buff *segs = ERR_PTR(-EINVAL);
  40. struct sctphdr *sh;
  41. if (!(skb_shinfo(skb)->gso_type & SKB_GSO_SCTP))
  42. goto out;
  43. sh = sctp_hdr(skb);
  44. if (!pskb_may_pull(skb, sizeof(*sh)))
  45. goto out;
  46. __skb_pull(skb, sizeof(*sh));
  47. if (skb_gso_ok(skb, features | NETIF_F_GSO_ROBUST)) {
  48. /* Packet is from an untrusted source, reset gso_segs. */
  49. struct skb_shared_info *pinfo = skb_shinfo(skb);
  50. struct sk_buff *frag_iter;
  51. pinfo->gso_segs = 0;
  52. if (skb->len != skb->data_len) {
  53. /* Means we have chunks in here too */
  54. pinfo->gso_segs++;
  55. }
  56. skb_walk_frags(skb, frag_iter)
  57. pinfo->gso_segs++;
  58. segs = NULL;
  59. goto out;
  60. }
  61. segs = skb_segment(skb, features | NETIF_F_HW_CSUM | NETIF_F_SG);
  62. if (IS_ERR(segs))
  63. goto out;
  64. /* All that is left is update SCTP CRC if necessary */
  65. if (!(features & NETIF_F_SCTP_CRC)) {
  66. for (skb = segs; skb; skb = skb->next) {
  67. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  68. sh = sctp_hdr(skb);
  69. sh->checksum = sctp_gso_make_checksum(skb);
  70. }
  71. }
  72. }
  73. out:
  74. return segs;
  75. }
  76. static const struct net_offload sctp_offload = {
  77. .callbacks = {
  78. .gso_segment = sctp_gso_segment,
  79. },
  80. };
  81. static const struct net_offload sctp6_offload = {
  82. .callbacks = {
  83. .gso_segment = sctp_gso_segment,
  84. },
  85. };
  86. int __init sctp_offload_init(void)
  87. {
  88. int ret;
  89. ret = inet_add_offload(&sctp_offload, IPPROTO_SCTP);
  90. if (ret)
  91. goto out;
  92. ret = inet6_add_offload(&sctp6_offload, IPPROTO_SCTP);
  93. if (ret)
  94. goto ipv4;
  95. return ret;
  96. ipv4:
  97. inet_del_offload(&sctp_offload, IPPROTO_SCTP);
  98. out:
  99. return ret;
  100. }