ip6_offload.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. skb_reset_mac_len(skb);
  94. if (udpfrag) {
  95. int err = ip6_find_1stfragopt(skb, &prevhdr);
  96. if (err < 0) {
  97. kfree_skb_list(segs);
  98. return ERR_PTR(err);
  99. }
  100. fptr = (struct frag_hdr *)((u8 *)ipv6h + err);
  101. fptr->frag_off = htons(offset);
  102. if (skb->next)
  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. skb_gro_frag0_invalidate(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. /* If the previous IP ID value was based on an atomic
  202. * datagram we can overwrite the value and ignore it.
  203. */
  204. if (NAPI_GRO_CB(skb)->is_atomic)
  205. NAPI_GRO_CB(p)->flush_id = 0;
  206. }
  207. NAPI_GRO_CB(skb)->is_atomic = true;
  208. NAPI_GRO_CB(skb)->flush |= flush;
  209. skb_gro_postpull_rcsum(skb, iph, nlen);
  210. pp = call_gro_receive(ops->callbacks.gro_receive, head, skb);
  211. out_unlock:
  212. rcu_read_unlock();
  213. out:
  214. NAPI_GRO_CB(skb)->flush |= flush;
  215. return pp;
  216. }
  217. static struct sk_buff **sit_ip6ip6_gro_receive(struct sk_buff **head,
  218. struct sk_buff *skb)
  219. {
  220. /* Common GRO receive for SIT and IP6IP6 */
  221. if (NAPI_GRO_CB(skb)->encap_mark) {
  222. NAPI_GRO_CB(skb)->flush = 1;
  223. return NULL;
  224. }
  225. NAPI_GRO_CB(skb)->encap_mark = 1;
  226. return ipv6_gro_receive(head, skb);
  227. }
  228. static struct sk_buff **ip4ip6_gro_receive(struct sk_buff **head,
  229. struct sk_buff *skb)
  230. {
  231. /* Common GRO receive for SIT and IP6IP6 */
  232. if (NAPI_GRO_CB(skb)->encap_mark) {
  233. NAPI_GRO_CB(skb)->flush = 1;
  234. return NULL;
  235. }
  236. NAPI_GRO_CB(skb)->encap_mark = 1;
  237. return inet_gro_receive(head, skb);
  238. }
  239. static int ipv6_gro_complete(struct sk_buff *skb, int nhoff)
  240. {
  241. const struct net_offload *ops;
  242. struct ipv6hdr *iph = (struct ipv6hdr *)(skb->data + nhoff);
  243. int err = -ENOSYS;
  244. if (skb->encapsulation) {
  245. skb_set_inner_protocol(skb, cpu_to_be16(ETH_P_IPV6));
  246. skb_set_inner_network_header(skb, nhoff);
  247. }
  248. iph->payload_len = htons(skb->len - nhoff - sizeof(*iph));
  249. rcu_read_lock();
  250. nhoff += sizeof(*iph) + ipv6_exthdrs_len(iph, &ops);
  251. if (WARN_ON(!ops || !ops->callbacks.gro_complete))
  252. goto out_unlock;
  253. err = ops->callbacks.gro_complete(skb, nhoff);
  254. out_unlock:
  255. rcu_read_unlock();
  256. return err;
  257. }
  258. static int sit_gro_complete(struct sk_buff *skb, int nhoff)
  259. {
  260. skb->encapsulation = 1;
  261. skb_shinfo(skb)->gso_type |= SKB_GSO_IPXIP4;
  262. return ipv6_gro_complete(skb, nhoff);
  263. }
  264. static int ip6ip6_gro_complete(struct sk_buff *skb, int nhoff)
  265. {
  266. skb->encapsulation = 1;
  267. skb_shinfo(skb)->gso_type |= SKB_GSO_IPXIP6;
  268. return ipv6_gro_complete(skb, nhoff);
  269. }
  270. static int ip4ip6_gro_complete(struct sk_buff *skb, int nhoff)
  271. {
  272. skb->encapsulation = 1;
  273. skb_shinfo(skb)->gso_type |= SKB_GSO_IPXIP6;
  274. return inet_gro_complete(skb, nhoff);
  275. }
  276. static struct packet_offload ipv6_packet_offload __read_mostly = {
  277. .type = cpu_to_be16(ETH_P_IPV6),
  278. .callbacks = {
  279. .gso_segment = ipv6_gso_segment,
  280. .gro_receive = ipv6_gro_receive,
  281. .gro_complete = ipv6_gro_complete,
  282. },
  283. };
  284. static const struct net_offload sit_offload = {
  285. .callbacks = {
  286. .gso_segment = ipv6_gso_segment,
  287. .gro_receive = sit_ip6ip6_gro_receive,
  288. .gro_complete = sit_gro_complete,
  289. },
  290. };
  291. static const struct net_offload ip4ip6_offload = {
  292. .callbacks = {
  293. .gso_segment = inet_gso_segment,
  294. .gro_receive = ip4ip6_gro_receive,
  295. .gro_complete = ip4ip6_gro_complete,
  296. },
  297. };
  298. static const struct net_offload ip6ip6_offload = {
  299. .callbacks = {
  300. .gso_segment = ipv6_gso_segment,
  301. .gro_receive = sit_ip6ip6_gro_receive,
  302. .gro_complete = ip6ip6_gro_complete,
  303. },
  304. };
  305. static int __init ipv6_offload_init(void)
  306. {
  307. if (tcpv6_offload_init() < 0)
  308. pr_crit("%s: Cannot add TCP protocol offload\n", __func__);
  309. if (ipv6_exthdrs_offload_init() < 0)
  310. pr_crit("%s: Cannot add EXTHDRS protocol offload\n", __func__);
  311. dev_add_offload(&ipv6_packet_offload);
  312. inet_add_offload(&sit_offload, IPPROTO_IPV6);
  313. inet6_add_offload(&ip6ip6_offload, IPPROTO_IPV6);
  314. inet6_add_offload(&ip4ip6_offload, IPPROTO_IPIP);
  315. return 0;
  316. }
  317. fs_initcall(ipv6_offload_init);