ip6_offload.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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 <net/inet_common.h>
  18. #include "ip6_offload.h"
  19. static int ipv6_gso_pull_exthdrs(struct sk_buff *skb, int proto)
  20. {
  21. const struct net_offload *ops = NULL;
  22. for (;;) {
  23. struct ipv6_opt_hdr *opth;
  24. int len;
  25. if (proto != NEXTHDR_HOP) {
  26. ops = rcu_dereference(inet6_offloads[proto]);
  27. if (unlikely(!ops))
  28. break;
  29. if (!(ops->flags & INET6_PROTO_GSO_EXTHDR))
  30. break;
  31. }
  32. if (unlikely(!pskb_may_pull(skb, 8)))
  33. break;
  34. opth = (void *)skb->data;
  35. len = ipv6_optlen(opth);
  36. if (unlikely(!pskb_may_pull(skb, len)))
  37. break;
  38. opth = (void *)skb->data;
  39. proto = opth->nexthdr;
  40. __skb_pull(skb, len);
  41. }
  42. return proto;
  43. }
  44. static struct sk_buff *ipv6_gso_segment(struct sk_buff *skb,
  45. netdev_features_t features)
  46. {
  47. struct sk_buff *segs = ERR_PTR(-EINVAL);
  48. struct ipv6hdr *ipv6h;
  49. const struct net_offload *ops;
  50. int proto;
  51. struct frag_hdr *fptr;
  52. unsigned int payload_len;
  53. u8 *prevhdr;
  54. int offset = 0;
  55. bool encap, udpfrag;
  56. int nhoff;
  57. bool gso_partial;
  58. skb_reset_network_header(skb);
  59. nhoff = skb_network_header(skb) - skb_mac_header(skb);
  60. if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h))))
  61. goto out;
  62. encap = SKB_GSO_CB(skb)->encap_level > 0;
  63. if (encap)
  64. features &= skb->dev->hw_enc_features;
  65. SKB_GSO_CB(skb)->encap_level += sizeof(*ipv6h);
  66. ipv6h = ipv6_hdr(skb);
  67. __skb_pull(skb, sizeof(*ipv6h));
  68. segs = ERR_PTR(-EPROTONOSUPPORT);
  69. proto = ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr);
  70. if (skb->encapsulation &&
  71. skb_shinfo(skb)->gso_type & (SKB_GSO_IPXIP4 | SKB_GSO_IPXIP6))
  72. udpfrag = proto == IPPROTO_UDP && encap;
  73. else
  74. udpfrag = proto == IPPROTO_UDP && !skb->encapsulation;
  75. ops = rcu_dereference(inet6_offloads[proto]);
  76. if (likely(ops && ops->callbacks.gso_segment)) {
  77. skb_reset_transport_header(skb);
  78. segs = ops->callbacks.gso_segment(skb, features);
  79. }
  80. if (IS_ERR_OR_NULL(segs))
  81. goto out;
  82. gso_partial = !!(skb_shinfo(segs)->gso_type & SKB_GSO_PARTIAL);
  83. for (skb = segs; skb; skb = skb->next) {
  84. ipv6h = (struct ipv6hdr *)(skb_mac_header(skb) + nhoff);
  85. if (gso_partial && skb_is_gso(skb))
  86. payload_len = skb_shinfo(skb)->gso_size +
  87. SKB_GSO_CB(skb)->data_offset +
  88. skb->head - (unsigned char *)(ipv6h + 1);
  89. else
  90. payload_len = skb->len - nhoff - sizeof(*ipv6h);
  91. ipv6h->payload_len = htons(payload_len);
  92. skb->network_header = (u8 *)ipv6h - skb->head;
  93. if (udpfrag) {
  94. int err = ip6_find_1stfragopt(skb, &prevhdr);
  95. if (err < 0) {
  96. kfree_skb_list(segs);
  97. return ERR_PTR(err);
  98. }
  99. fptr = (struct frag_hdr *)((u8 *)ipv6h + err);
  100. fptr->frag_off = htons(offset);
  101. if (skb->next)
  102. fptr->frag_off |= htons(IP6_MF);
  103. offset += (ntohs(ipv6h->payload_len) -
  104. sizeof(struct frag_hdr));
  105. }
  106. if (encap)
  107. skb_reset_inner_headers(skb);
  108. }
  109. out:
  110. return segs;
  111. }
  112. /* Return the total length of all the extension hdrs, following the same
  113. * logic in ipv6_gso_pull_exthdrs() when parsing ext-hdrs.
  114. */
  115. static int ipv6_exthdrs_len(struct ipv6hdr *iph,
  116. const struct net_offload **opps)
  117. {
  118. struct ipv6_opt_hdr *opth = (void *)iph;
  119. int len = 0, proto, optlen = sizeof(*iph);
  120. proto = iph->nexthdr;
  121. for (;;) {
  122. if (proto != NEXTHDR_HOP) {
  123. *opps = rcu_dereference(inet6_offloads[proto]);
  124. if (unlikely(!(*opps)))
  125. break;
  126. if (!((*opps)->flags & INET6_PROTO_GSO_EXTHDR))
  127. break;
  128. }
  129. opth = (void *)opth + optlen;
  130. optlen = ipv6_optlen(opth);
  131. len += optlen;
  132. proto = opth->nexthdr;
  133. }
  134. return len;
  135. }
  136. static struct sk_buff **ipv6_gro_receive(struct sk_buff **head,
  137. struct sk_buff *skb)
  138. {
  139. const struct net_offload *ops;
  140. struct sk_buff **pp = NULL;
  141. struct sk_buff *p;
  142. struct ipv6hdr *iph;
  143. unsigned int nlen;
  144. unsigned int hlen;
  145. unsigned int off;
  146. u16 flush = 1;
  147. int proto;
  148. off = skb_gro_offset(skb);
  149. hlen = off + sizeof(*iph);
  150. iph = skb_gro_header_fast(skb, off);
  151. if (skb_gro_header_hard(skb, hlen)) {
  152. iph = skb_gro_header_slow(skb, hlen, off);
  153. if (unlikely(!iph))
  154. goto out;
  155. }
  156. skb_set_network_header(skb, off);
  157. skb_gro_pull(skb, sizeof(*iph));
  158. skb_set_transport_header(skb, skb_gro_offset(skb));
  159. flush += ntohs(iph->payload_len) != skb_gro_len(skb);
  160. rcu_read_lock();
  161. proto = iph->nexthdr;
  162. ops = rcu_dereference(inet6_offloads[proto]);
  163. if (!ops || !ops->callbacks.gro_receive) {
  164. __pskb_pull(skb, skb_gro_offset(skb));
  165. skb_gro_frag0_invalidate(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. /* If the previous IP ID value was based on an atomic
  201. * datagram we can overwrite the value and ignore it.
  202. */
  203. if (NAPI_GRO_CB(skb)->is_atomic)
  204. NAPI_GRO_CB(p)->flush_id = 0;
  205. }
  206. NAPI_GRO_CB(skb)->is_atomic = true;
  207. NAPI_GRO_CB(skb)->flush |= flush;
  208. skb_gro_postpull_rcsum(skb, iph, nlen);
  209. pp = call_gro_receive(ops->callbacks.gro_receive, head, skb);
  210. out_unlock:
  211. rcu_read_unlock();
  212. out:
  213. NAPI_GRO_CB(skb)->flush |= flush;
  214. return pp;
  215. }
  216. static struct sk_buff **sit_ip6ip6_gro_receive(struct sk_buff **head,
  217. struct sk_buff *skb)
  218. {
  219. /* Common GRO receive for SIT and IP6IP6 */
  220. if (NAPI_GRO_CB(skb)->encap_mark) {
  221. NAPI_GRO_CB(skb)->flush = 1;
  222. return NULL;
  223. }
  224. NAPI_GRO_CB(skb)->encap_mark = 1;
  225. return ipv6_gro_receive(head, skb);
  226. }
  227. static struct sk_buff **ip4ip6_gro_receive(struct sk_buff **head,
  228. struct sk_buff *skb)
  229. {
  230. /* Common GRO receive for SIT and IP6IP6 */
  231. if (NAPI_GRO_CB(skb)->encap_mark) {
  232. NAPI_GRO_CB(skb)->flush = 1;
  233. return NULL;
  234. }
  235. NAPI_GRO_CB(skb)->encap_mark = 1;
  236. return inet_gro_receive(head, skb);
  237. }
  238. static int ipv6_gro_complete(struct sk_buff *skb, int nhoff)
  239. {
  240. const struct net_offload *ops;
  241. struct ipv6hdr *iph = (struct ipv6hdr *)(skb->data + nhoff);
  242. int err = -ENOSYS;
  243. if (skb->encapsulation) {
  244. skb_set_inner_protocol(skb, cpu_to_be16(ETH_P_IPV6));
  245. skb_set_inner_network_header(skb, nhoff);
  246. }
  247. iph->payload_len = htons(skb->len - nhoff - sizeof(*iph));
  248. rcu_read_lock();
  249. nhoff += sizeof(*iph) + ipv6_exthdrs_len(iph, &ops);
  250. if (WARN_ON(!ops || !ops->callbacks.gro_complete))
  251. goto out_unlock;
  252. err = ops->callbacks.gro_complete(skb, nhoff);
  253. out_unlock:
  254. rcu_read_unlock();
  255. return err;
  256. }
  257. static int sit_gro_complete(struct sk_buff *skb, int nhoff)
  258. {
  259. skb->encapsulation = 1;
  260. skb_shinfo(skb)->gso_type |= SKB_GSO_IPXIP4;
  261. return ipv6_gro_complete(skb, nhoff);
  262. }
  263. static int ip6ip6_gro_complete(struct sk_buff *skb, int nhoff)
  264. {
  265. skb->encapsulation = 1;
  266. skb_shinfo(skb)->gso_type |= SKB_GSO_IPXIP6;
  267. return ipv6_gro_complete(skb, nhoff);
  268. }
  269. static int ip4ip6_gro_complete(struct sk_buff *skb, int nhoff)
  270. {
  271. skb->encapsulation = 1;
  272. skb_shinfo(skb)->gso_type |= SKB_GSO_IPXIP6;
  273. return inet_gro_complete(skb, nhoff);
  274. }
  275. static struct packet_offload ipv6_packet_offload __read_mostly = {
  276. .type = cpu_to_be16(ETH_P_IPV6),
  277. .callbacks = {
  278. .gso_segment = ipv6_gso_segment,
  279. .gro_receive = ipv6_gro_receive,
  280. .gro_complete = ipv6_gro_complete,
  281. },
  282. };
  283. static const struct net_offload sit_offload = {
  284. .callbacks = {
  285. .gso_segment = ipv6_gso_segment,
  286. .gro_receive = sit_ip6ip6_gro_receive,
  287. .gro_complete = sit_gro_complete,
  288. },
  289. };
  290. static const struct net_offload ip4ip6_offload = {
  291. .callbacks = {
  292. .gso_segment = inet_gso_segment,
  293. .gro_receive = ip4ip6_gro_receive,
  294. .gro_complete = ip4ip6_gro_complete,
  295. },
  296. };
  297. static const struct net_offload ip6ip6_offload = {
  298. .callbacks = {
  299. .gso_segment = ipv6_gso_segment,
  300. .gro_receive = sit_ip6ip6_gro_receive,
  301. .gro_complete = ip6ip6_gro_complete,
  302. },
  303. };
  304. static int __init ipv6_offload_init(void)
  305. {
  306. if (tcpv6_offload_init() < 0)
  307. pr_crit("%s: Cannot add TCP protocol offload\n", __func__);
  308. if (ipv6_exthdrs_offload_init() < 0)
  309. pr_crit("%s: Cannot add EXTHDRS protocol offload\n", __func__);
  310. dev_add_offload(&ipv6_packet_offload);
  311. inet_add_offload(&sit_offload, IPPROTO_IPV6);
  312. inet6_add_offload(&ip6ip6_offload, IPPROTO_IPV6);
  313. inet6_add_offload(&ip4ip6_offload, IPPROTO_IPIP);
  314. return 0;
  315. }
  316. fs_initcall(ipv6_offload_init);