ip6_offload.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * IPV6 GSO/GRO offload support
  4. * Linux INET6 implementation
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/socket.h>
  8. #include <linux/netdevice.h>
  9. #include <linux/skbuff.h>
  10. #include <linux/printk.h>
  11. #include <net/protocol.h>
  12. #include <net/ipv6.h>
  13. #include <net/inet_common.h>
  14. #include <net/tcp.h>
  15. #include <net/udp.h>
  16. #include <net/gro.h>
  17. #include "ip6_offload.h"
  18. /* All GRO functions are always builtin, except UDP over ipv6, which lays in
  19. * ipv6 module, as it depends on UDPv6 lookup function, so we need special care
  20. * when ipv6 is built as a module
  21. */
  22. #if IS_BUILTIN(CONFIG_IPV6)
  23. #define INDIRECT_CALL_L4(f, f2, f1, ...) INDIRECT_CALL_2(f, f2, f1, __VA_ARGS__)
  24. #else
  25. #define INDIRECT_CALL_L4(f, f2, f1, ...) INDIRECT_CALL_1(f, f2, __VA_ARGS__)
  26. #endif
  27. #define indirect_call_gro_receive_l4(f2, f1, cb, head, skb) \
  28. ({ \
  29. unlikely(gro_recursion_inc_test(skb)) ? \
  30. NAPI_GRO_CB(skb)->flush |= 1, NULL : \
  31. INDIRECT_CALL_L4(cb, f2, f1, head, skb); \
  32. })
  33. static int ipv6_gso_pull_exthdrs(struct sk_buff *skb, int proto)
  34. {
  35. const struct net_offload *ops = NULL;
  36. for (;;) {
  37. struct ipv6_opt_hdr *opth;
  38. int len;
  39. if (proto != NEXTHDR_HOP) {
  40. ops = rcu_dereference(inet6_offloads[proto]);
  41. if (unlikely(!ops))
  42. break;
  43. if (!(ops->flags & INET6_PROTO_GSO_EXTHDR))
  44. break;
  45. }
  46. if (unlikely(!pskb_may_pull(skb, 8)))
  47. break;
  48. opth = (void *)skb->data;
  49. len = ipv6_optlen(opth);
  50. if (unlikely(!pskb_may_pull(skb, len)))
  51. break;
  52. opth = (void *)skb->data;
  53. proto = opth->nexthdr;
  54. __skb_pull(skb, len);
  55. }
  56. return proto;
  57. }
  58. static struct sk_buff *ipv6_gso_segment(struct sk_buff *skb,
  59. netdev_features_t features)
  60. {
  61. struct sk_buff *segs = ERR_PTR(-EINVAL);
  62. struct ipv6hdr *ipv6h;
  63. const struct net_offload *ops;
  64. int proto, nexthdr;
  65. struct frag_hdr *fptr;
  66. unsigned int payload_len;
  67. u8 *prevhdr;
  68. int offset = 0;
  69. bool encap, udpfrag;
  70. int nhoff;
  71. bool gso_partial;
  72. skb_reset_network_header(skb);
  73. nexthdr = ipv6_has_hopopt_jumbo(skb);
  74. if (nexthdr) {
  75. const int hophdr_len = sizeof(struct hop_jumbo_hdr);
  76. int err;
  77. err = skb_cow_head(skb, 0);
  78. if (err < 0)
  79. return ERR_PTR(err);
  80. /* remove the HBH header.
  81. * Layout: [Ethernet header][IPv6 header][HBH][TCP header]
  82. */
  83. memmove(skb_mac_header(skb) + hophdr_len,
  84. skb_mac_header(skb),
  85. ETH_HLEN + sizeof(struct ipv6hdr));
  86. skb->data += hophdr_len;
  87. skb->len -= hophdr_len;
  88. skb->network_header += hophdr_len;
  89. skb->mac_header += hophdr_len;
  90. ipv6h = (struct ipv6hdr *)skb->data;
  91. ipv6h->nexthdr = nexthdr;
  92. }
  93. nhoff = skb_network_header(skb) - skb_mac_header(skb);
  94. if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h))))
  95. goto out;
  96. encap = SKB_GSO_CB(skb)->encap_level > 0;
  97. if (encap)
  98. features &= skb->dev->hw_enc_features;
  99. SKB_GSO_CB(skb)->encap_level += sizeof(*ipv6h);
  100. ipv6h = ipv6_hdr(skb);
  101. __skb_pull(skb, sizeof(*ipv6h));
  102. segs = ERR_PTR(-EPROTONOSUPPORT);
  103. proto = ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr);
  104. if (skb->encapsulation &&
  105. skb_shinfo(skb)->gso_type & (SKB_GSO_IPXIP4 | SKB_GSO_IPXIP6))
  106. udpfrag = proto == IPPROTO_UDP && encap &&
  107. (skb_shinfo(skb)->gso_type & SKB_GSO_UDP);
  108. else
  109. udpfrag = proto == IPPROTO_UDP && !skb->encapsulation &&
  110. (skb_shinfo(skb)->gso_type & SKB_GSO_UDP);
  111. ops = rcu_dereference(inet6_offloads[proto]);
  112. if (likely(ops && ops->callbacks.gso_segment)) {
  113. skb_reset_transport_header(skb);
  114. segs = ops->callbacks.gso_segment(skb, features);
  115. if (!segs)
  116. skb->network_header = skb_mac_header(skb) + nhoff - skb->head;
  117. }
  118. if (IS_ERR_OR_NULL(segs))
  119. goto out;
  120. gso_partial = !!(skb_shinfo(segs)->gso_type & SKB_GSO_PARTIAL);
  121. for (skb = segs; skb; skb = skb->next) {
  122. ipv6h = (struct ipv6hdr *)(skb_mac_header(skb) + nhoff);
  123. if (gso_partial && skb_is_gso(skb))
  124. payload_len = skb_shinfo(skb)->gso_size +
  125. SKB_GSO_CB(skb)->data_offset +
  126. skb->head - (unsigned char *)(ipv6h + 1);
  127. else
  128. payload_len = skb->len - nhoff - sizeof(*ipv6h);
  129. ipv6h->payload_len = htons(payload_len);
  130. skb->network_header = (u8 *)ipv6h - skb->head;
  131. skb_reset_mac_len(skb);
  132. if (udpfrag) {
  133. int err = ip6_find_1stfragopt(skb, &prevhdr);
  134. if (err < 0) {
  135. kfree_skb_list(segs);
  136. return ERR_PTR(err);
  137. }
  138. fptr = (struct frag_hdr *)((u8 *)ipv6h + err);
  139. fptr->frag_off = htons(offset);
  140. if (skb->next)
  141. fptr->frag_off |= htons(IP6_MF);
  142. offset += (ntohs(ipv6h->payload_len) -
  143. sizeof(struct frag_hdr));
  144. }
  145. if (encap)
  146. skb_reset_inner_headers(skb);
  147. }
  148. out:
  149. return segs;
  150. }
  151. /* Return the total length of all the extension hdrs, following the same
  152. * logic in ipv6_gso_pull_exthdrs() when parsing ext-hdrs.
  153. */
  154. static int ipv6_exthdrs_len(struct ipv6hdr *iph,
  155. const struct net_offload **opps)
  156. {
  157. struct ipv6_opt_hdr *opth = (void *)iph;
  158. int len = 0, proto, optlen = sizeof(*iph);
  159. proto = iph->nexthdr;
  160. for (;;) {
  161. if (proto != NEXTHDR_HOP) {
  162. *opps = rcu_dereference(inet6_offloads[proto]);
  163. if (unlikely(!(*opps)))
  164. break;
  165. if (!((*opps)->flags & INET6_PROTO_GSO_EXTHDR))
  166. break;
  167. }
  168. opth = (void *)opth + optlen;
  169. optlen = ipv6_optlen(opth);
  170. len += optlen;
  171. proto = opth->nexthdr;
  172. }
  173. return len;
  174. }
  175. INDIRECT_CALLABLE_SCOPE struct sk_buff *ipv6_gro_receive(struct list_head *head,
  176. struct sk_buff *skb)
  177. {
  178. const struct net_offload *ops;
  179. struct sk_buff *pp = NULL;
  180. struct sk_buff *p;
  181. struct ipv6hdr *iph;
  182. unsigned int nlen;
  183. unsigned int hlen;
  184. unsigned int off;
  185. u16 flush = 1;
  186. int proto;
  187. off = skb_gro_offset(skb);
  188. hlen = off + sizeof(*iph);
  189. iph = skb_gro_header(skb, hlen, off);
  190. if (unlikely(!iph))
  191. goto out;
  192. skb_set_network_header(skb, off);
  193. skb_gro_pull(skb, sizeof(*iph));
  194. skb_set_transport_header(skb, skb_gro_offset(skb));
  195. flush += ntohs(iph->payload_len) != skb_gro_len(skb);
  196. proto = iph->nexthdr;
  197. ops = rcu_dereference(inet6_offloads[proto]);
  198. if (!ops || !ops->callbacks.gro_receive) {
  199. pskb_pull(skb, skb_gro_offset(skb));
  200. skb_gro_frag0_invalidate(skb);
  201. proto = ipv6_gso_pull_exthdrs(skb, proto);
  202. skb_gro_pull(skb, -skb_transport_offset(skb));
  203. skb_reset_transport_header(skb);
  204. __skb_push(skb, skb_gro_offset(skb));
  205. ops = rcu_dereference(inet6_offloads[proto]);
  206. if (!ops || !ops->callbacks.gro_receive)
  207. goto out;
  208. iph = ipv6_hdr(skb);
  209. }
  210. NAPI_GRO_CB(skb)->proto = proto;
  211. flush--;
  212. nlen = skb_network_header_len(skb);
  213. list_for_each_entry(p, head, list) {
  214. const struct ipv6hdr *iph2;
  215. __be32 first_word; /* <Version:4><Traffic_Class:8><Flow_Label:20> */
  216. if (!NAPI_GRO_CB(p)->same_flow)
  217. continue;
  218. iph2 = (struct ipv6hdr *)(p->data + off);
  219. first_word = *(__be32 *)iph ^ *(__be32 *)iph2;
  220. /* All fields must match except length and Traffic Class.
  221. * XXX skbs on the gro_list have all been parsed and pulled
  222. * already so we don't need to compare nlen
  223. * (nlen != (sizeof(*iph2) + ipv6_exthdrs_len(iph2, &ops)))
  224. * memcmp() alone below is sufficient, right?
  225. */
  226. if ((first_word & htonl(0xF00FFFFF)) ||
  227. !ipv6_addr_equal(&iph->saddr, &iph2->saddr) ||
  228. !ipv6_addr_equal(&iph->daddr, &iph2->daddr) ||
  229. iph->nexthdr != iph2->nexthdr) {
  230. not_same_flow:
  231. NAPI_GRO_CB(p)->same_flow = 0;
  232. continue;
  233. }
  234. if (unlikely(nlen > sizeof(struct ipv6hdr))) {
  235. if (memcmp(iph + 1, iph2 + 1,
  236. nlen - sizeof(struct ipv6hdr)))
  237. goto not_same_flow;
  238. }
  239. /* flush if Traffic Class fields are different */
  240. NAPI_GRO_CB(p)->flush |= !!((first_word & htonl(0x0FF00000)) |
  241. (__force __be32)(iph->hop_limit ^ iph2->hop_limit));
  242. NAPI_GRO_CB(p)->flush |= flush;
  243. /* If the previous IP ID value was based on an atomic
  244. * datagram we can overwrite the value and ignore it.
  245. */
  246. if (NAPI_GRO_CB(skb)->is_atomic)
  247. NAPI_GRO_CB(p)->flush_id = 0;
  248. }
  249. NAPI_GRO_CB(skb)->is_atomic = true;
  250. NAPI_GRO_CB(skb)->flush |= flush;
  251. skb_gro_postpull_rcsum(skb, iph, nlen);
  252. pp = indirect_call_gro_receive_l4(tcp6_gro_receive, udp6_gro_receive,
  253. ops->callbacks.gro_receive, head, skb);
  254. out:
  255. skb_gro_flush_final(skb, pp, flush);
  256. return pp;
  257. }
  258. static struct sk_buff *sit_ip6ip6_gro_receive(struct list_head *head,
  259. struct sk_buff *skb)
  260. {
  261. /* Common GRO receive for SIT and IP6IP6 */
  262. if (NAPI_GRO_CB(skb)->encap_mark) {
  263. NAPI_GRO_CB(skb)->flush = 1;
  264. return NULL;
  265. }
  266. NAPI_GRO_CB(skb)->encap_mark = 1;
  267. return ipv6_gro_receive(head, skb);
  268. }
  269. static struct sk_buff *ip4ip6_gro_receive(struct list_head *head,
  270. struct sk_buff *skb)
  271. {
  272. /* Common GRO receive for SIT and IP6IP6 */
  273. if (NAPI_GRO_CB(skb)->encap_mark) {
  274. NAPI_GRO_CB(skb)->flush = 1;
  275. return NULL;
  276. }
  277. NAPI_GRO_CB(skb)->encap_mark = 1;
  278. return inet_gro_receive(head, skb);
  279. }
  280. INDIRECT_CALLABLE_SCOPE int ipv6_gro_complete(struct sk_buff *skb, int nhoff)
  281. {
  282. const struct net_offload *ops;
  283. struct ipv6hdr *iph;
  284. int err = -ENOSYS;
  285. u32 payload_len;
  286. if (skb->encapsulation) {
  287. skb_set_inner_protocol(skb, cpu_to_be16(ETH_P_IPV6));
  288. skb_set_inner_network_header(skb, nhoff);
  289. }
  290. payload_len = skb->len - nhoff - sizeof(*iph);
  291. if (unlikely(payload_len > IPV6_MAXPLEN)) {
  292. struct hop_jumbo_hdr *hop_jumbo;
  293. int hoplen = sizeof(*hop_jumbo);
  294. /* Move network header left */
  295. memmove(skb_mac_header(skb) - hoplen, skb_mac_header(skb),
  296. skb->transport_header - skb->mac_header);
  297. skb->data -= hoplen;
  298. skb->len += hoplen;
  299. skb->mac_header -= hoplen;
  300. skb->network_header -= hoplen;
  301. iph = (struct ipv6hdr *)(skb->data + nhoff);
  302. hop_jumbo = (struct hop_jumbo_hdr *)(iph + 1);
  303. /* Build hop-by-hop options */
  304. hop_jumbo->nexthdr = iph->nexthdr;
  305. hop_jumbo->hdrlen = 0;
  306. hop_jumbo->tlv_type = IPV6_TLV_JUMBO;
  307. hop_jumbo->tlv_len = 4;
  308. hop_jumbo->jumbo_payload_len = htonl(payload_len + hoplen);
  309. iph->nexthdr = NEXTHDR_HOP;
  310. iph->payload_len = 0;
  311. } else {
  312. iph = (struct ipv6hdr *)(skb->data + nhoff);
  313. iph->payload_len = htons(payload_len);
  314. }
  315. nhoff += sizeof(*iph) + ipv6_exthdrs_len(iph, &ops);
  316. if (WARN_ON(!ops || !ops->callbacks.gro_complete))
  317. goto out;
  318. err = INDIRECT_CALL_L4(ops->callbacks.gro_complete, tcp6_gro_complete,
  319. udp6_gro_complete, skb, nhoff);
  320. out:
  321. return err;
  322. }
  323. static int sit_gro_complete(struct sk_buff *skb, int nhoff)
  324. {
  325. skb->encapsulation = 1;
  326. skb_shinfo(skb)->gso_type |= SKB_GSO_IPXIP4;
  327. return ipv6_gro_complete(skb, nhoff);
  328. }
  329. static int ip6ip6_gro_complete(struct sk_buff *skb, int nhoff)
  330. {
  331. skb->encapsulation = 1;
  332. skb_shinfo(skb)->gso_type |= SKB_GSO_IPXIP6;
  333. return ipv6_gro_complete(skb, nhoff);
  334. }
  335. static int ip4ip6_gro_complete(struct sk_buff *skb, int nhoff)
  336. {
  337. skb->encapsulation = 1;
  338. skb_shinfo(skb)->gso_type |= SKB_GSO_IPXIP6;
  339. return inet_gro_complete(skb, nhoff);
  340. }
  341. static struct packet_offload ipv6_packet_offload __read_mostly = {
  342. .type = cpu_to_be16(ETH_P_IPV6),
  343. .callbacks = {
  344. .gso_segment = ipv6_gso_segment,
  345. .gro_receive = ipv6_gro_receive,
  346. .gro_complete = ipv6_gro_complete,
  347. },
  348. };
  349. static struct sk_buff *sit_gso_segment(struct sk_buff *skb,
  350. netdev_features_t features)
  351. {
  352. if (!(skb_shinfo(skb)->gso_type & SKB_GSO_IPXIP4))
  353. return ERR_PTR(-EINVAL);
  354. return ipv6_gso_segment(skb, features);
  355. }
  356. static struct sk_buff *ip4ip6_gso_segment(struct sk_buff *skb,
  357. netdev_features_t features)
  358. {
  359. if (!(skb_shinfo(skb)->gso_type & SKB_GSO_IPXIP6))
  360. return ERR_PTR(-EINVAL);
  361. return inet_gso_segment(skb, features);
  362. }
  363. static struct sk_buff *ip6ip6_gso_segment(struct sk_buff *skb,
  364. netdev_features_t features)
  365. {
  366. if (!(skb_shinfo(skb)->gso_type & SKB_GSO_IPXIP6))
  367. return ERR_PTR(-EINVAL);
  368. return ipv6_gso_segment(skb, features);
  369. }
  370. static const struct net_offload sit_offload = {
  371. .callbacks = {
  372. .gso_segment = sit_gso_segment,
  373. .gro_receive = sit_ip6ip6_gro_receive,
  374. .gro_complete = sit_gro_complete,
  375. },
  376. };
  377. static const struct net_offload ip4ip6_offload = {
  378. .callbacks = {
  379. .gso_segment = ip4ip6_gso_segment,
  380. .gro_receive = ip4ip6_gro_receive,
  381. .gro_complete = ip4ip6_gro_complete,
  382. },
  383. };
  384. static const struct net_offload ip6ip6_offload = {
  385. .callbacks = {
  386. .gso_segment = ip6ip6_gso_segment,
  387. .gro_receive = sit_ip6ip6_gro_receive,
  388. .gro_complete = ip6ip6_gro_complete,
  389. },
  390. };
  391. static int __init ipv6_offload_init(void)
  392. {
  393. if (tcpv6_offload_init() < 0)
  394. pr_crit("%s: Cannot add TCP protocol offload\n", __func__);
  395. if (ipv6_exthdrs_offload_init() < 0)
  396. pr_crit("%s: Cannot add EXTHDRS protocol offload\n", __func__);
  397. dev_add_offload(&ipv6_packet_offload);
  398. inet_add_offload(&sit_offload, IPPROTO_IPV6);
  399. inet6_add_offload(&ip6ip6_offload, IPPROTO_IPV6);
  400. inet6_add_offload(&ip4ip6_offload, IPPROTO_IPIP);
  401. return 0;
  402. }
  403. fs_initcall(ipv6_offload_init);