udp_offload.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * IPV6 GSO/GRO offload support
  3. * Linux INET6 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. * UDPv6 GSO support
  11. */
  12. #include <linux/skbuff.h>
  13. #include <net/protocol.h>
  14. #include <net/ipv6.h>
  15. #include <net/udp.h>
  16. #include <net/ip6_checksum.h>
  17. #include "ip6_offload.h"
  18. static int udp6_ufo_send_check(struct sk_buff *skb)
  19. {
  20. const struct ipv6hdr *ipv6h;
  21. struct udphdr *uh;
  22. /* UDP Tunnel offload on ipv6 is not yet supported. */
  23. if (skb->encapsulation)
  24. return -EINVAL;
  25. if (!pskb_may_pull(skb, sizeof(*uh)))
  26. return -EINVAL;
  27. ipv6h = ipv6_hdr(skb);
  28. uh = udp_hdr(skb);
  29. uh->check = ~csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr, skb->len,
  30. IPPROTO_UDP, 0);
  31. skb->csum_start = skb_transport_header(skb) - skb->head;
  32. skb->csum_offset = offsetof(struct udphdr, check);
  33. skb->ip_summed = CHECKSUM_PARTIAL;
  34. return 0;
  35. }
  36. static struct sk_buff *udp6_ufo_fragment(struct sk_buff *skb,
  37. netdev_features_t features)
  38. {
  39. struct sk_buff *segs = ERR_PTR(-EINVAL);
  40. unsigned int mss;
  41. unsigned int unfrag_ip6hlen, unfrag_len;
  42. struct frag_hdr *fptr;
  43. u8 *packet_start, *prevhdr;
  44. u8 nexthdr;
  45. u8 frag_hdr_sz = sizeof(struct frag_hdr);
  46. int offset;
  47. __wsum csum;
  48. int tnl_hlen;
  49. mss = skb_shinfo(skb)->gso_size;
  50. if (unlikely(skb->len <= mss))
  51. goto out;
  52. if (skb_gso_ok(skb, features | NETIF_F_GSO_ROBUST)) {
  53. /* Packet is from an untrusted source, reset gso_segs. */
  54. int type = skb_shinfo(skb)->gso_type;
  55. if (unlikely(type & ~(SKB_GSO_UDP |
  56. SKB_GSO_DODGY |
  57. SKB_GSO_UDP_TUNNEL |
  58. SKB_GSO_GRE |
  59. SKB_GSO_MPLS) ||
  60. !(type & (SKB_GSO_UDP))))
  61. goto out;
  62. skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(skb->len, mss);
  63. segs = NULL;
  64. goto out;
  65. }
  66. /* Do software UFO. Complete and fill in the UDP checksum as HW cannot
  67. * do checksum of UDP packets sent as multiple IP fragments.
  68. */
  69. offset = skb_checksum_start_offset(skb);
  70. csum = skb_checksum(skb, offset, skb->len - offset, 0);
  71. offset += skb->csum_offset;
  72. *(__sum16 *)(skb->data + offset) = csum_fold(csum);
  73. skb->ip_summed = CHECKSUM_NONE;
  74. /* Check if there is enough headroom to insert fragment header. */
  75. tnl_hlen = skb_tnl_header_len(skb);
  76. if (skb_headroom(skb) < (tnl_hlen + frag_hdr_sz)) {
  77. if (gso_pskb_expand_head(skb, tnl_hlen + frag_hdr_sz))
  78. goto out;
  79. }
  80. /* Find the unfragmentable header and shift it left by frag_hdr_sz
  81. * bytes to insert fragment header.
  82. */
  83. unfrag_ip6hlen = ip6_find_1stfragopt(skb, &prevhdr);
  84. nexthdr = *prevhdr;
  85. *prevhdr = NEXTHDR_FRAGMENT;
  86. unfrag_len = (skb_network_header(skb) - skb_mac_header(skb)) +
  87. unfrag_ip6hlen + tnl_hlen;
  88. packet_start = (u8 *) skb->head + SKB_GSO_CB(skb)->mac_offset;
  89. memmove(packet_start-frag_hdr_sz, packet_start, unfrag_len);
  90. SKB_GSO_CB(skb)->mac_offset -= frag_hdr_sz;
  91. skb->mac_header -= frag_hdr_sz;
  92. skb->network_header -= frag_hdr_sz;
  93. fptr = (struct frag_hdr *)(skb_network_header(skb) + unfrag_ip6hlen);
  94. fptr->nexthdr = nexthdr;
  95. fptr->reserved = 0;
  96. ipv6_select_ident(fptr, (struct rt6_info *)skb_dst(skb));
  97. /* Fragment the skb. ipv6 header and the remaining fields of the
  98. * fragment header are updated in ipv6_gso_segment()
  99. */
  100. segs = skb_segment(skb, features);
  101. out:
  102. return segs;
  103. }
  104. static const struct net_offload udpv6_offload = {
  105. .callbacks = {
  106. .gso_send_check = udp6_ufo_send_check,
  107. .gso_segment = udp6_ufo_fragment,
  108. },
  109. };
  110. int __init udp_offload_init(void)
  111. {
  112. return inet6_add_offload(&udpv6_offload, IPPROTO_UDP);
  113. }