ip6_offload.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. (skb_shinfo(skb)->gso_type & SKB_GSO_UDP);
  74. else
  75. udpfrag = proto == IPPROTO_UDP && !skb->encapsulation &&
  76. (skb_shinfo(skb)->gso_type & SKB_GSO_UDP);
  77. ops = rcu_dereference(inet6_offloads[proto]);
  78. if (likely(ops && ops->callbacks.gso_segment)) {
  79. skb_reset_transport_header(skb);
  80. segs = ops->callbacks.gso_segment(skb, features);
  81. }
  82. if (IS_ERR_OR_NULL(segs))
  83. goto out;
  84. gso_partial = !!(skb_shinfo(segs)->gso_type & SKB_GSO_PARTIAL);
  85. for (skb = segs; skb; skb = skb->next) {
  86. ipv6h = (struct ipv6hdr *)(skb_mac_header(skb) + nhoff);
  87. if (gso_partial && skb_is_gso(skb))
  88. payload_len = skb_shinfo(skb)->gso_size +
  89. SKB_GSO_CB(skb)->data_offset +
  90. skb->head - (unsigned char *)(ipv6h + 1);
  91. else
  92. payload_len = skb->len - nhoff - sizeof(*ipv6h);
  93. ipv6h->payload_len = htons(payload_len);
  94. skb->network_header = (u8 *)ipv6h - skb->head;
  95. skb_reset_mac_len(skb);
  96. if (udpfrag) {
  97. int err = ip6_find_1stfragopt(skb, &prevhdr);
  98. if (err < 0) {
  99. kfree_skb_list(segs);
  100. return ERR_PTR(err);
  101. }
  102. fptr = (struct frag_hdr *)((u8 *)ipv6h + err);
  103. fptr->frag_off = htons(offset);
  104. if (skb->next)
  105. fptr->frag_off |= htons(IP6_MF);
  106. offset += (ntohs(ipv6h->payload_len) -
  107. sizeof(struct frag_hdr));
  108. }
  109. if (encap)
  110. skb_reset_inner_headers(skb);
  111. }
  112. out:
  113. return segs;
  114. }
  115. /* Return the total length of all the extension hdrs, following the same
  116. * logic in ipv6_gso_pull_exthdrs() when parsing ext-hdrs.
  117. */
  118. static int ipv6_exthdrs_len(struct ipv6hdr *iph,
  119. const struct net_offload **opps)
  120. {
  121. struct ipv6_opt_hdr *opth = (void *)iph;
  122. int len = 0, proto, optlen = sizeof(*iph);
  123. proto = iph->nexthdr;
  124. for (;;) {
  125. if (proto != NEXTHDR_HOP) {
  126. *opps = rcu_dereference(inet6_offloads[proto]);
  127. if (unlikely(!(*opps)))
  128. break;
  129. if (!((*opps)->flags & INET6_PROTO_GSO_EXTHDR))
  130. break;
  131. }
  132. opth = (void *)opth + optlen;
  133. optlen = ipv6_optlen(opth);
  134. len += optlen;
  135. proto = opth->nexthdr;
  136. }
  137. return len;
  138. }
  139. static struct sk_buff *ipv6_gro_receive(struct list_head *head,
  140. struct sk_buff *skb)
  141. {
  142. const struct net_offload *ops;
  143. struct sk_buff *pp = NULL;
  144. struct sk_buff *p;
  145. struct ipv6hdr *iph;
  146. unsigned int nlen;
  147. unsigned int hlen;
  148. unsigned int off;
  149. u16 flush = 1;
  150. int proto;
  151. off = skb_gro_offset(skb);
  152. hlen = off + sizeof(*iph);
  153. iph = skb_gro_header_fast(skb, off);
  154. if (skb_gro_header_hard(skb, hlen)) {
  155. iph = skb_gro_header_slow(skb, hlen, off);
  156. if (unlikely(!iph))
  157. goto out;
  158. }
  159. skb_set_network_header(skb, off);
  160. skb_gro_pull(skb, sizeof(*iph));
  161. skb_set_transport_header(skb, skb_gro_offset(skb));
  162. flush += ntohs(iph->payload_len) != skb_gro_len(skb);
  163. rcu_read_lock();
  164. proto = iph->nexthdr;
  165. ops = rcu_dereference(inet6_offloads[proto]);
  166. if (!ops || !ops->callbacks.gro_receive) {
  167. __pskb_pull(skb, skb_gro_offset(skb));
  168. skb_gro_frag0_invalidate(skb);
  169. proto = ipv6_gso_pull_exthdrs(skb, proto);
  170. skb_gro_pull(skb, -skb_transport_offset(skb));
  171. skb_reset_transport_header(skb);
  172. __skb_push(skb, skb_gro_offset(skb));
  173. ops = rcu_dereference(inet6_offloads[proto]);
  174. if (!ops || !ops->callbacks.gro_receive)
  175. goto out_unlock;
  176. iph = ipv6_hdr(skb);
  177. }
  178. NAPI_GRO_CB(skb)->proto = proto;
  179. flush--;
  180. nlen = skb_network_header_len(skb);
  181. list_for_each_entry(p, head, list) {
  182. const struct ipv6hdr *iph2;
  183. __be32 first_word; /* <Version:4><Traffic_Class:8><Flow_Label:20> */
  184. if (!NAPI_GRO_CB(p)->same_flow)
  185. continue;
  186. iph2 = (struct ipv6hdr *)(p->data + off);
  187. first_word = *(__be32 *)iph ^ *(__be32 *)iph2;
  188. /* All fields must match except length and Traffic Class.
  189. * XXX skbs on the gro_list have all been parsed and pulled
  190. * already so we don't need to compare nlen
  191. * (nlen != (sizeof(*iph2) + ipv6_exthdrs_len(iph2, &ops)))
  192. * memcmp() alone below is suffcient, right?
  193. */
  194. if ((first_word & htonl(0xF00FFFFF)) ||
  195. memcmp(&iph->nexthdr, &iph2->nexthdr,
  196. nlen - offsetof(struct ipv6hdr, nexthdr))) {
  197. NAPI_GRO_CB(p)->same_flow = 0;
  198. continue;
  199. }
  200. /* flush if Traffic Class fields are different */
  201. NAPI_GRO_CB(p)->flush |= !!(first_word & htonl(0x0FF00000));
  202. NAPI_GRO_CB(p)->flush |= flush;
  203. /* If the previous IP ID value was based on an atomic
  204. * datagram we can overwrite the value and ignore it.
  205. */
  206. if (NAPI_GRO_CB(skb)->is_atomic)
  207. NAPI_GRO_CB(p)->flush_id = 0;
  208. }
  209. NAPI_GRO_CB(skb)->is_atomic = true;
  210. NAPI_GRO_CB(skb)->flush |= flush;
  211. skb_gro_postpull_rcsum(skb, iph, nlen);
  212. pp = call_gro_receive(ops->callbacks.gro_receive, head, skb);
  213. out_unlock:
  214. rcu_read_unlock();
  215. out:
  216. skb_gro_flush_final(skb, pp, flush);
  217. return pp;
  218. }
  219. static struct sk_buff *sit_ip6ip6_gro_receive(struct list_head *head,
  220. struct sk_buff *skb)
  221. {
  222. /* Common GRO receive for SIT and IP6IP6 */
  223. if (NAPI_GRO_CB(skb)->encap_mark) {
  224. NAPI_GRO_CB(skb)->flush = 1;
  225. return NULL;
  226. }
  227. NAPI_GRO_CB(skb)->encap_mark = 1;
  228. return ipv6_gro_receive(head, skb);
  229. }
  230. static struct sk_buff *ip4ip6_gro_receive(struct list_head *head,
  231. struct sk_buff *skb)
  232. {
  233. /* Common GRO receive for SIT and IP6IP6 */
  234. if (NAPI_GRO_CB(skb)->encap_mark) {
  235. NAPI_GRO_CB(skb)->flush = 1;
  236. return NULL;
  237. }
  238. NAPI_GRO_CB(skb)->encap_mark = 1;
  239. return inet_gro_receive(head, skb);
  240. }
  241. static int ipv6_gro_complete(struct sk_buff *skb, int nhoff)
  242. {
  243. const struct net_offload *ops;
  244. struct ipv6hdr *iph = (struct ipv6hdr *)(skb->data + nhoff);
  245. int err = -ENOSYS;
  246. if (skb->encapsulation) {
  247. skb_set_inner_protocol(skb, cpu_to_be16(ETH_P_IPV6));
  248. skb_set_inner_network_header(skb, nhoff);
  249. }
  250. iph->payload_len = htons(skb->len - nhoff - sizeof(*iph));
  251. rcu_read_lock();
  252. nhoff += sizeof(*iph) + ipv6_exthdrs_len(iph, &ops);
  253. if (WARN_ON(!ops || !ops->callbacks.gro_complete))
  254. goto out_unlock;
  255. err = ops->callbacks.gro_complete(skb, nhoff);
  256. out_unlock:
  257. rcu_read_unlock();
  258. return err;
  259. }
  260. static int sit_gro_complete(struct sk_buff *skb, int nhoff)
  261. {
  262. skb->encapsulation = 1;
  263. skb_shinfo(skb)->gso_type |= SKB_GSO_IPXIP4;
  264. return ipv6_gro_complete(skb, nhoff);
  265. }
  266. static int ip6ip6_gro_complete(struct sk_buff *skb, int nhoff)
  267. {
  268. skb->encapsulation = 1;
  269. skb_shinfo(skb)->gso_type |= SKB_GSO_IPXIP6;
  270. return ipv6_gro_complete(skb, nhoff);
  271. }
  272. static int ip4ip6_gro_complete(struct sk_buff *skb, int nhoff)
  273. {
  274. skb->encapsulation = 1;
  275. skb_shinfo(skb)->gso_type |= SKB_GSO_IPXIP6;
  276. return inet_gro_complete(skb, nhoff);
  277. }
  278. static struct packet_offload ipv6_packet_offload __read_mostly = {
  279. .type = cpu_to_be16(ETH_P_IPV6),
  280. .callbacks = {
  281. .gso_segment = ipv6_gso_segment,
  282. .gro_receive = ipv6_gro_receive,
  283. .gro_complete = ipv6_gro_complete,
  284. },
  285. };
  286. static const struct net_offload sit_offload = {
  287. .callbacks = {
  288. .gso_segment = ipv6_gso_segment,
  289. .gro_receive = sit_ip6ip6_gro_receive,
  290. .gro_complete = sit_gro_complete,
  291. },
  292. };
  293. static const struct net_offload ip4ip6_offload = {
  294. .callbacks = {
  295. .gso_segment = inet_gso_segment,
  296. .gro_receive = ip4ip6_gro_receive,
  297. .gro_complete = ip4ip6_gro_complete,
  298. },
  299. };
  300. static const struct net_offload ip6ip6_offload = {
  301. .callbacks = {
  302. .gso_segment = ipv6_gso_segment,
  303. .gro_receive = sit_ip6ip6_gro_receive,
  304. .gro_complete = ip6ip6_gro_complete,
  305. },
  306. };
  307. static int __init ipv6_offload_init(void)
  308. {
  309. if (tcpv6_offload_init() < 0)
  310. pr_crit("%s: Cannot add TCP protocol offload\n", __func__);
  311. if (ipv6_exthdrs_offload_init() < 0)
  312. pr_crit("%s: Cannot add EXTHDRS protocol offload\n", __func__);
  313. dev_add_offload(&ipv6_packet_offload);
  314. inet_add_offload(&sit_offload, IPPROTO_IPV6);
  315. inet6_add_offload(&ip6ip6_offload, IPPROTO_IPV6);
  316. inet6_add_offload(&ip4ip6_offload, IPPROTO_IPIP);
  317. return 0;
  318. }
  319. fs_initcall(ipv6_offload_init);