ip6_offload.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. opth = (void *)skb->data;
  38. proto = opth->nexthdr;
  39. __skb_pull(skb, len);
  40. }
  41. return proto;
  42. }
  43. static struct sk_buff *ipv6_gso_segment(struct sk_buff *skb,
  44. netdev_features_t features)
  45. {
  46. struct sk_buff *segs = ERR_PTR(-EINVAL);
  47. struct ipv6hdr *ipv6h;
  48. const struct net_offload *ops;
  49. int proto;
  50. struct frag_hdr *fptr;
  51. unsigned int unfrag_ip6hlen;
  52. u8 *prevhdr;
  53. int offset = 0;
  54. bool encap, udpfrag;
  55. int nhoff;
  56. if (unlikely(skb_shinfo(skb)->gso_type &
  57. ~(SKB_GSO_UDP |
  58. SKB_GSO_DODGY |
  59. SKB_GSO_TCP_ECN |
  60. SKB_GSO_GRE |
  61. SKB_GSO_GRE_CSUM |
  62. SKB_GSO_IPIP |
  63. SKB_GSO_SIT |
  64. SKB_GSO_UDP_TUNNEL |
  65. SKB_GSO_UDP_TUNNEL_CSUM |
  66. SKB_GSO_MPLS |
  67. SKB_GSO_TCPV6 |
  68. 0)))
  69. goto out;
  70. skb_reset_network_header(skb);
  71. nhoff = skb_network_header(skb) - skb_mac_header(skb);
  72. if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h))))
  73. goto out;
  74. encap = SKB_GSO_CB(skb)->encap_level > 0;
  75. if (encap)
  76. features = skb->dev->hw_enc_features & netif_skb_features(skb);
  77. SKB_GSO_CB(skb)->encap_level += sizeof(*ipv6h);
  78. ipv6h = ipv6_hdr(skb);
  79. __skb_pull(skb, sizeof(*ipv6h));
  80. segs = ERR_PTR(-EPROTONOSUPPORT);
  81. proto = ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr);
  82. if (skb->encapsulation &&
  83. skb_shinfo(skb)->gso_type & (SKB_GSO_SIT|SKB_GSO_IPIP))
  84. udpfrag = proto == IPPROTO_UDP && encap;
  85. else
  86. udpfrag = proto == IPPROTO_UDP && !skb->encapsulation;
  87. ops = rcu_dereference(inet6_offloads[proto]);
  88. if (likely(ops && ops->callbacks.gso_segment)) {
  89. skb_reset_transport_header(skb);
  90. segs = ops->callbacks.gso_segment(skb, features);
  91. }
  92. if (IS_ERR(segs))
  93. goto out;
  94. for (skb = segs; skb; skb = skb->next) {
  95. ipv6h = (struct ipv6hdr *)(skb_mac_header(skb) + nhoff);
  96. ipv6h->payload_len = htons(skb->len - nhoff - sizeof(*ipv6h));
  97. skb->network_header = (u8 *)ipv6h - skb->head;
  98. if (udpfrag) {
  99. unfrag_ip6hlen = ip6_find_1stfragopt(skb, &prevhdr);
  100. fptr = (struct frag_hdr *)((u8 *)ipv6h + unfrag_ip6hlen);
  101. fptr->frag_off = htons(offset);
  102. if (skb->next != NULL)
  103. fptr->frag_off |= htons(IP6_MF);
  104. offset += (ntohs(ipv6h->payload_len) -
  105. sizeof(struct frag_hdr));
  106. }
  107. if (encap)
  108. skb_reset_inner_headers(skb);
  109. }
  110. out:
  111. return segs;
  112. }
  113. /* Return the total length of all the extension hdrs, following the same
  114. * logic in ipv6_gso_pull_exthdrs() when parsing ext-hdrs.
  115. */
  116. static int ipv6_exthdrs_len(struct ipv6hdr *iph,
  117. const struct net_offload **opps)
  118. {
  119. struct ipv6_opt_hdr *opth = (void *)iph;
  120. int len = 0, proto, optlen = sizeof(*iph);
  121. proto = iph->nexthdr;
  122. for (;;) {
  123. if (proto != NEXTHDR_HOP) {
  124. *opps = rcu_dereference(inet6_offloads[proto]);
  125. if (unlikely(!(*opps)))
  126. break;
  127. if (!((*opps)->flags & INET6_PROTO_GSO_EXTHDR))
  128. break;
  129. }
  130. opth = (void *)opth + optlen;
  131. optlen = ipv6_optlen(opth);
  132. len += optlen;
  133. proto = opth->nexthdr;
  134. }
  135. return len;
  136. }
  137. static struct sk_buff **ipv6_gro_receive(struct sk_buff **head,
  138. struct sk_buff *skb)
  139. {
  140. const struct net_offload *ops;
  141. struct sk_buff **pp = NULL;
  142. struct sk_buff *p;
  143. struct ipv6hdr *iph;
  144. unsigned int nlen;
  145. unsigned int hlen;
  146. unsigned int off;
  147. u16 flush = 1;
  148. int proto;
  149. off = skb_gro_offset(skb);
  150. hlen = off + sizeof(*iph);
  151. iph = skb_gro_header_fast(skb, off);
  152. if (skb_gro_header_hard(skb, hlen)) {
  153. iph = skb_gro_header_slow(skb, hlen, off);
  154. if (unlikely(!iph))
  155. goto out;
  156. }
  157. skb_set_network_header(skb, off);
  158. skb_gro_pull(skb, sizeof(*iph));
  159. skb_set_transport_header(skb, skb_gro_offset(skb));
  160. flush += ntohs(iph->payload_len) != skb_gro_len(skb);
  161. rcu_read_lock();
  162. proto = iph->nexthdr;
  163. ops = rcu_dereference(inet6_offloads[proto]);
  164. if (!ops || !ops->callbacks.gro_receive) {
  165. __pskb_pull(skb, skb_gro_offset(skb));
  166. proto = ipv6_gso_pull_exthdrs(skb, proto);
  167. skb_gro_pull(skb, -skb_transport_offset(skb));
  168. skb_reset_transport_header(skb);
  169. __skb_push(skb, skb_gro_offset(skb));
  170. ops = rcu_dereference(inet6_offloads[proto]);
  171. if (!ops || !ops->callbacks.gro_receive)
  172. goto out_unlock;
  173. iph = ipv6_hdr(skb);
  174. }
  175. NAPI_GRO_CB(skb)->proto = proto;
  176. flush--;
  177. nlen = skb_network_header_len(skb);
  178. for (p = *head; p; p = p->next) {
  179. const struct ipv6hdr *iph2;
  180. __be32 first_word; /* <Version:4><Traffic_Class:8><Flow_Label:20> */
  181. if (!NAPI_GRO_CB(p)->same_flow)
  182. continue;
  183. iph2 = (struct ipv6hdr *)(p->data + off);
  184. first_word = *(__be32 *)iph ^ *(__be32 *)iph2;
  185. /* All fields must match except length and Traffic Class.
  186. * XXX skbs on the gro_list have all been parsed and pulled
  187. * already so we don't need to compare nlen
  188. * (nlen != (sizeof(*iph2) + ipv6_exthdrs_len(iph2, &ops)))
  189. * memcmp() alone below is suffcient, right?
  190. */
  191. if ((first_word & htonl(0xF00FFFFF)) ||
  192. memcmp(&iph->nexthdr, &iph2->nexthdr,
  193. nlen - offsetof(struct ipv6hdr, nexthdr))) {
  194. NAPI_GRO_CB(p)->same_flow = 0;
  195. continue;
  196. }
  197. /* flush if Traffic Class fields are different */
  198. NAPI_GRO_CB(p)->flush |= !!(first_word & htonl(0x0FF00000));
  199. NAPI_GRO_CB(p)->flush |= flush;
  200. /* Clear flush_id, there's really no concept of ID in IPv6. */
  201. NAPI_GRO_CB(p)->flush_id = 0;
  202. }
  203. NAPI_GRO_CB(skb)->flush |= flush;
  204. skb_gro_postpull_rcsum(skb, iph, nlen);
  205. pp = ops->callbacks.gro_receive(head, skb);
  206. out_unlock:
  207. rcu_read_unlock();
  208. out:
  209. NAPI_GRO_CB(skb)->flush |= flush;
  210. return pp;
  211. }
  212. static int ipv6_gro_complete(struct sk_buff *skb, int nhoff)
  213. {
  214. const struct net_offload *ops;
  215. struct ipv6hdr *iph = (struct ipv6hdr *)(skb->data + nhoff);
  216. int err = -ENOSYS;
  217. iph->payload_len = htons(skb->len - nhoff - sizeof(*iph));
  218. rcu_read_lock();
  219. nhoff += sizeof(*iph) + ipv6_exthdrs_len(iph, &ops);
  220. if (WARN_ON(!ops || !ops->callbacks.gro_complete))
  221. goto out_unlock;
  222. err = ops->callbacks.gro_complete(skb, nhoff);
  223. out_unlock:
  224. rcu_read_unlock();
  225. return err;
  226. }
  227. static struct packet_offload ipv6_packet_offload __read_mostly = {
  228. .type = cpu_to_be16(ETH_P_IPV6),
  229. .callbacks = {
  230. .gso_segment = ipv6_gso_segment,
  231. .gro_receive = ipv6_gro_receive,
  232. .gro_complete = ipv6_gro_complete,
  233. },
  234. };
  235. static const struct net_offload sit_offload = {
  236. .callbacks = {
  237. .gso_segment = ipv6_gso_segment,
  238. .gro_receive = ipv6_gro_receive,
  239. .gro_complete = ipv6_gro_complete,
  240. },
  241. };
  242. static int __init ipv6_offload_init(void)
  243. {
  244. if (tcpv6_offload_init() < 0)
  245. pr_crit("%s: Cannot add TCP protocol offload\n", __func__);
  246. if (udp_offload_init() < 0)
  247. pr_crit("%s: Cannot add UDP protocol offload\n", __func__);
  248. if (ipv6_exthdrs_offload_init() < 0)
  249. pr_crit("%s: Cannot add EXTHDRS protocol offload\n", __func__);
  250. dev_add_offload(&ipv6_packet_offload);
  251. inet_add_offload(&sit_offload, IPPROTO_IPV6);
  252. return 0;
  253. }
  254. fs_initcall(ipv6_offload_init);