udp_offload.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * IPV4 GSO/GRO offload support
  3. * Linux INET implementation
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version
  8. * 2 of the License, or (at your option) any later version.
  9. *
  10. * UDPv4 GSO support
  11. */
  12. #include <linux/skbuff.h>
  13. #include <net/udp.h>
  14. #include <net/protocol.h>
  15. static int udp4_ufo_send_check(struct sk_buff *skb)
  16. {
  17. if (!pskb_may_pull(skb, sizeof(struct udphdr)))
  18. return -EINVAL;
  19. if (likely(!skb->encapsulation)) {
  20. const struct iphdr *iph;
  21. struct udphdr *uh;
  22. iph = ip_hdr(skb);
  23. uh = udp_hdr(skb);
  24. uh->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr, skb->len,
  25. IPPROTO_UDP, 0);
  26. skb->csum_start = skb_transport_header(skb) - skb->head;
  27. skb->csum_offset = offsetof(struct udphdr, check);
  28. skb->ip_summed = CHECKSUM_PARTIAL;
  29. }
  30. return 0;
  31. }
  32. static struct sk_buff *udp4_ufo_fragment(struct sk_buff *skb,
  33. netdev_features_t features)
  34. {
  35. struct sk_buff *segs = ERR_PTR(-EINVAL);
  36. unsigned int mss;
  37. mss = skb_shinfo(skb)->gso_size;
  38. if (unlikely(skb->len <= mss))
  39. goto out;
  40. if (skb_gso_ok(skb, features | NETIF_F_GSO_ROBUST)) {
  41. /* Packet is from an untrusted source, reset gso_segs. */
  42. int type = skb_shinfo(skb)->gso_type;
  43. if (unlikely(type & ~(SKB_GSO_UDP | SKB_GSO_DODGY |
  44. SKB_GSO_UDP_TUNNEL |
  45. SKB_GSO_GRE | SKB_GSO_MPLS) ||
  46. !(type & (SKB_GSO_UDP))))
  47. goto out;
  48. skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(skb->len, mss);
  49. segs = NULL;
  50. goto out;
  51. }
  52. /* Fragment the skb. IP headers of the fragments are updated in
  53. * inet_gso_segment()
  54. */
  55. if (skb->encapsulation && skb_shinfo(skb)->gso_type & SKB_GSO_UDP_TUNNEL)
  56. segs = skb_udp_tunnel_segment(skb, features);
  57. else {
  58. int offset;
  59. __wsum csum;
  60. /* Do software UFO. Complete and fill in the UDP checksum as
  61. * HW cannot do checksum of UDP packets sent as multiple
  62. * IP fragments.
  63. */
  64. offset = skb_checksum_start_offset(skb);
  65. csum = skb_checksum(skb, offset, skb->len - offset, 0);
  66. offset += skb->csum_offset;
  67. *(__sum16 *)(skb->data + offset) = csum_fold(csum);
  68. skb->ip_summed = CHECKSUM_NONE;
  69. segs = skb_segment(skb, features);
  70. }
  71. out:
  72. return segs;
  73. }
  74. static const struct net_offload udpv4_offload = {
  75. .callbacks = {
  76. .gso_send_check = udp4_ufo_send_check,
  77. .gso_segment = udp4_ufo_fragment,
  78. },
  79. };
  80. int __init udpv4_offload_init(void)
  81. {
  82. return inet_add_offload(&udpv4_offload, IPPROTO_UDP);
  83. }