ip6_offload.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. #include <linux/kernel.h>
  11. #include <linux/socket.h>
  12. #include <linux/netdevice.h>
  13. #include <linux/skbuff.h>
  14. #include <linux/printk.h>
  15. #include <net/protocol.h>
  16. #include <net/ipv6.h>
  17. #include "ip6_offload.h"
  18. static int ipv6_gso_pull_exthdrs(struct sk_buff *skb, int proto)
  19. {
  20. const struct net_offload *ops = NULL;
  21. for (;;) {
  22. struct ipv6_opt_hdr *opth;
  23. int len;
  24. if (proto != NEXTHDR_HOP) {
  25. ops = rcu_dereference(inet6_offloads[proto]);
  26. if (unlikely(!ops))
  27. break;
  28. if (!(ops->flags & INET6_PROTO_GSO_EXTHDR))
  29. break;
  30. }
  31. if (unlikely(!pskb_may_pull(skb, 8)))
  32. break;
  33. opth = (void *)skb->data;
  34. len = ipv6_optlen(opth);
  35. if (unlikely(!pskb_may_pull(skb, len)))
  36. break;
  37. proto = opth->nexthdr;
  38. __skb_pull(skb, len);
  39. }
  40. return proto;
  41. }
  42. static int ipv6_gso_send_check(struct sk_buff *skb)
  43. {
  44. const struct ipv6hdr *ipv6h;
  45. const struct net_offload *ops;
  46. int err = -EINVAL;
  47. if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h))))
  48. goto out;
  49. ipv6h = ipv6_hdr(skb);
  50. __skb_pull(skb, sizeof(*ipv6h));
  51. err = -EPROTONOSUPPORT;
  52. rcu_read_lock();
  53. ops = rcu_dereference(inet6_offloads[
  54. ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr)]);
  55. if (likely(ops && ops->callbacks.gso_send_check)) {
  56. skb_reset_transport_header(skb);
  57. err = ops->callbacks.gso_send_check(skb);
  58. }
  59. rcu_read_unlock();
  60. out:
  61. return err;
  62. }
  63. static struct sk_buff *ipv6_gso_segment(struct sk_buff *skb,
  64. netdev_features_t features)
  65. {
  66. struct sk_buff *segs = ERR_PTR(-EINVAL);
  67. struct ipv6hdr *ipv6h;
  68. const struct net_offload *ops;
  69. int proto;
  70. struct frag_hdr *fptr;
  71. unsigned int unfrag_ip6hlen;
  72. u8 *prevhdr;
  73. int offset = 0;
  74. if (!(features & NETIF_F_V6_CSUM))
  75. features &= ~NETIF_F_SG;
  76. if (unlikely(skb_shinfo(skb)->gso_type &
  77. ~(SKB_GSO_UDP |
  78. SKB_GSO_DODGY |
  79. SKB_GSO_TCP_ECN |
  80. SKB_GSO_GRE |
  81. SKB_GSO_TCPV6 |
  82. 0)))
  83. goto out;
  84. if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h))))
  85. goto out;
  86. ipv6h = ipv6_hdr(skb);
  87. __skb_pull(skb, sizeof(*ipv6h));
  88. segs = ERR_PTR(-EPROTONOSUPPORT);
  89. proto = ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr);
  90. rcu_read_lock();
  91. ops = rcu_dereference(inet6_offloads[proto]);
  92. if (likely(ops && ops->callbacks.gso_segment)) {
  93. skb_reset_transport_header(skb);
  94. segs = ops->callbacks.gso_segment(skb, features);
  95. }
  96. rcu_read_unlock();
  97. if (IS_ERR(segs))
  98. goto out;
  99. for (skb = segs; skb; skb = skb->next) {
  100. ipv6h = ipv6_hdr(skb);
  101. ipv6h->payload_len = htons(skb->len - skb->mac_len -
  102. sizeof(*ipv6h));
  103. if (proto == IPPROTO_UDP) {
  104. unfrag_ip6hlen = ip6_find_1stfragopt(skb, &prevhdr);
  105. fptr = (struct frag_hdr *)(skb_network_header(skb) +
  106. unfrag_ip6hlen);
  107. fptr->frag_off = htons(offset);
  108. if (skb->next != NULL)
  109. fptr->frag_off |= htons(IP6_MF);
  110. offset += (ntohs(ipv6h->payload_len) -
  111. sizeof(struct frag_hdr));
  112. }
  113. }
  114. out:
  115. return segs;
  116. }
  117. static struct sk_buff **ipv6_gro_receive(struct sk_buff **head,
  118. struct sk_buff *skb)
  119. {
  120. const struct net_offload *ops;
  121. struct sk_buff **pp = NULL;
  122. struct sk_buff *p;
  123. struct ipv6hdr *iph;
  124. unsigned int nlen;
  125. unsigned int hlen;
  126. unsigned int off;
  127. int flush = 1;
  128. int proto;
  129. __wsum csum;
  130. off = skb_gro_offset(skb);
  131. hlen = off + sizeof(*iph);
  132. iph = skb_gro_header_fast(skb, off);
  133. if (skb_gro_header_hard(skb, hlen)) {
  134. iph = skb_gro_header_slow(skb, hlen, off);
  135. if (unlikely(!iph))
  136. goto out;
  137. }
  138. skb_gro_pull(skb, sizeof(*iph));
  139. skb_set_transport_header(skb, skb_gro_offset(skb));
  140. flush += ntohs(iph->payload_len) != skb_gro_len(skb);
  141. rcu_read_lock();
  142. proto = iph->nexthdr;
  143. ops = rcu_dereference(inet6_offloads[proto]);
  144. if (!ops || !ops->callbacks.gro_receive) {
  145. __pskb_pull(skb, skb_gro_offset(skb));
  146. proto = ipv6_gso_pull_exthdrs(skb, proto);
  147. skb_gro_pull(skb, -skb_transport_offset(skb));
  148. skb_reset_transport_header(skb);
  149. __skb_push(skb, skb_gro_offset(skb));
  150. ops = rcu_dereference(inet6_offloads[proto]);
  151. if (!ops || !ops->callbacks.gro_receive)
  152. goto out_unlock;
  153. iph = ipv6_hdr(skb);
  154. }
  155. NAPI_GRO_CB(skb)->proto = proto;
  156. flush--;
  157. nlen = skb_network_header_len(skb);
  158. for (p = *head; p; p = p->next) {
  159. const struct ipv6hdr *iph2;
  160. __be32 first_word; /* <Version:4><Traffic_Class:8><Flow_Label:20> */
  161. if (!NAPI_GRO_CB(p)->same_flow)
  162. continue;
  163. iph2 = ipv6_hdr(p);
  164. first_word = *(__be32 *)iph ^ *(__be32 *)iph2 ;
  165. /* All fields must match except length and Traffic Class. */
  166. if (nlen != skb_network_header_len(p) ||
  167. (first_word & htonl(0xF00FFFFF)) ||
  168. memcmp(&iph->nexthdr, &iph2->nexthdr,
  169. nlen - offsetof(struct ipv6hdr, nexthdr))) {
  170. NAPI_GRO_CB(p)->same_flow = 0;
  171. continue;
  172. }
  173. /* flush if Traffic Class fields are different */
  174. NAPI_GRO_CB(p)->flush |= !!(first_word & htonl(0x0FF00000));
  175. NAPI_GRO_CB(p)->flush |= flush;
  176. }
  177. NAPI_GRO_CB(skb)->flush |= flush;
  178. csum = skb->csum;
  179. skb_postpull_rcsum(skb, iph, skb_network_header_len(skb));
  180. pp = ops->callbacks.gro_receive(head, skb);
  181. skb->csum = csum;
  182. out_unlock:
  183. rcu_read_unlock();
  184. out:
  185. NAPI_GRO_CB(skb)->flush |= flush;
  186. return pp;
  187. }
  188. static int ipv6_gro_complete(struct sk_buff *skb)
  189. {
  190. const struct net_offload *ops;
  191. struct ipv6hdr *iph = ipv6_hdr(skb);
  192. int err = -ENOSYS;
  193. iph->payload_len = htons(skb->len - skb_network_offset(skb) -
  194. sizeof(*iph));
  195. rcu_read_lock();
  196. ops = rcu_dereference(inet6_offloads[NAPI_GRO_CB(skb)->proto]);
  197. if (WARN_ON(!ops || !ops->callbacks.gro_complete))
  198. goto out_unlock;
  199. err = ops->callbacks.gro_complete(skb);
  200. out_unlock:
  201. rcu_read_unlock();
  202. return err;
  203. }
  204. static struct packet_offload ipv6_packet_offload __read_mostly = {
  205. .type = cpu_to_be16(ETH_P_IPV6),
  206. .callbacks = {
  207. .gso_send_check = ipv6_gso_send_check,
  208. .gso_segment = ipv6_gso_segment,
  209. .gro_receive = ipv6_gro_receive,
  210. .gro_complete = ipv6_gro_complete,
  211. },
  212. };
  213. static int __init ipv6_offload_init(void)
  214. {
  215. if (tcpv6_offload_init() < 0)
  216. pr_crit("%s: Cannot add TCP protocol offload\n", __func__);
  217. if (udp_offload_init() < 0)
  218. pr_crit("%s: Cannot add UDP protocol offload\n", __func__);
  219. if (ipv6_exthdrs_offload_init() < 0)
  220. pr_crit("%s: Cannot add EXTHDRS protocol offload\n", __func__);
  221. dev_add_offload(&ipv6_packet_offload);
  222. return 0;
  223. }
  224. fs_initcall(ipv6_offload_init);