ip6_offload.c 7.6 KB

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