ip6_offload.c 8.0 KB

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