ip6_offload.c 8.0 KB

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