udp_offload.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /*
  2. * IPV4 GSO/GRO offload support
  3. * Linux INET 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. * UDPv4 GSO support
  11. */
  12. #include <linux/skbuff.h>
  13. #include <net/udp.h>
  14. #include <net/protocol.h>
  15. static struct sk_buff *__skb_udp_tunnel_segment(struct sk_buff *skb,
  16. netdev_features_t features,
  17. struct sk_buff *(*gso_inner_segment)(struct sk_buff *skb,
  18. netdev_features_t features),
  19. __be16 new_protocol, bool is_ipv6)
  20. {
  21. int tnl_hlen = skb_inner_mac_header(skb) - skb_transport_header(skb);
  22. bool remcsum, need_csum, offload_csum, ufo, gso_partial;
  23. struct sk_buff *segs = ERR_PTR(-EINVAL);
  24. struct udphdr *uh = udp_hdr(skb);
  25. u16 mac_offset = skb->mac_header;
  26. __be16 protocol = skb->protocol;
  27. u16 mac_len = skb->mac_len;
  28. int udp_offset, outer_hlen;
  29. __wsum partial;
  30. if (unlikely(!pskb_may_pull(skb, tnl_hlen)))
  31. goto out;
  32. /* Adjust partial header checksum to negate old length.
  33. * We cannot rely on the value contained in uh->len as it is
  34. * possible that the actual value exceeds the boundaries of the
  35. * 16 bit length field due to the header being added outside of an
  36. * IP or IPv6 frame that was already limited to 64K - 1.
  37. */
  38. if (skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL)
  39. partial = (__force __wsum)uh->len;
  40. else
  41. partial = (__force __wsum)htonl(skb->len);
  42. partial = csum_sub(csum_unfold(uh->check), partial);
  43. /* setup inner skb. */
  44. skb->encapsulation = 0;
  45. SKB_GSO_CB(skb)->encap_level = 0;
  46. __skb_pull(skb, tnl_hlen);
  47. skb_reset_mac_header(skb);
  48. skb_set_network_header(skb, skb_inner_network_offset(skb));
  49. skb->mac_len = skb_inner_network_offset(skb);
  50. skb->protocol = new_protocol;
  51. need_csum = !!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_TUNNEL_CSUM);
  52. skb->encap_hdr_csum = need_csum;
  53. remcsum = !!(skb_shinfo(skb)->gso_type & SKB_GSO_TUNNEL_REMCSUM);
  54. skb->remcsum_offload = remcsum;
  55. ufo = !!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP);
  56. /* Try to offload checksum if possible */
  57. offload_csum = !!(need_csum &&
  58. (skb->dev->features &
  59. (is_ipv6 ? (NETIF_F_HW_CSUM | NETIF_F_IPV6_CSUM) :
  60. (NETIF_F_HW_CSUM | NETIF_F_IP_CSUM))));
  61. features &= skb->dev->hw_enc_features;
  62. /* The only checksum offload we care about from here on out is the
  63. * outer one so strip the existing checksum feature flags and
  64. * instead set the flag based on our outer checksum offload value.
  65. */
  66. if (remcsum || ufo) {
  67. features &= ~NETIF_F_CSUM_MASK;
  68. if (!need_csum || offload_csum)
  69. features |= NETIF_F_HW_CSUM;
  70. }
  71. /* segment inner packet. */
  72. segs = gso_inner_segment(skb, features);
  73. if (IS_ERR_OR_NULL(segs)) {
  74. skb_gso_error_unwind(skb, protocol, tnl_hlen, mac_offset,
  75. mac_len);
  76. goto out;
  77. }
  78. gso_partial = !!(skb_shinfo(segs)->gso_type & SKB_GSO_PARTIAL);
  79. outer_hlen = skb_tnl_header_len(skb);
  80. udp_offset = outer_hlen - tnl_hlen;
  81. skb = segs;
  82. do {
  83. unsigned int len;
  84. if (remcsum)
  85. skb->ip_summed = CHECKSUM_NONE;
  86. /* Set up inner headers if we are offloading inner checksum */
  87. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  88. skb_reset_inner_headers(skb);
  89. skb->encapsulation = 1;
  90. }
  91. skb->mac_len = mac_len;
  92. skb->protocol = protocol;
  93. __skb_push(skb, outer_hlen);
  94. skb_reset_mac_header(skb);
  95. skb_set_network_header(skb, mac_len);
  96. skb_set_transport_header(skb, udp_offset);
  97. len = skb->len - udp_offset;
  98. uh = udp_hdr(skb);
  99. /* If we are only performing partial GSO the inner header
  100. * will be using a length value equal to only one MSS sized
  101. * segment instead of the entire frame.
  102. */
  103. if (gso_partial) {
  104. uh->len = htons(skb_shinfo(skb)->gso_size +
  105. SKB_GSO_CB(skb)->data_offset +
  106. skb->head - (unsigned char *)uh);
  107. } else {
  108. uh->len = htons(len);
  109. }
  110. if (!need_csum)
  111. continue;
  112. uh->check = ~csum_fold(csum_add(partial,
  113. (__force __wsum)htonl(len)));
  114. if (skb->encapsulation || !offload_csum) {
  115. uh->check = gso_make_checksum(skb, ~uh->check);
  116. if (uh->check == 0)
  117. uh->check = CSUM_MANGLED_0;
  118. } else {
  119. skb->ip_summed = CHECKSUM_PARTIAL;
  120. skb->csum_start = skb_transport_header(skb) - skb->head;
  121. skb->csum_offset = offsetof(struct udphdr, check);
  122. }
  123. } while ((skb = skb->next));
  124. out:
  125. return segs;
  126. }
  127. struct sk_buff *skb_udp_tunnel_segment(struct sk_buff *skb,
  128. netdev_features_t features,
  129. bool is_ipv6)
  130. {
  131. __be16 protocol = skb->protocol;
  132. const struct net_offload **offloads;
  133. const struct net_offload *ops;
  134. struct sk_buff *segs = ERR_PTR(-EINVAL);
  135. struct sk_buff *(*gso_inner_segment)(struct sk_buff *skb,
  136. netdev_features_t features);
  137. rcu_read_lock();
  138. switch (skb->inner_protocol_type) {
  139. case ENCAP_TYPE_ETHER:
  140. protocol = skb->inner_protocol;
  141. gso_inner_segment = skb_mac_gso_segment;
  142. break;
  143. case ENCAP_TYPE_IPPROTO:
  144. offloads = is_ipv6 ? inet6_offloads : inet_offloads;
  145. ops = rcu_dereference(offloads[skb->inner_ipproto]);
  146. if (!ops || !ops->callbacks.gso_segment)
  147. goto out_unlock;
  148. gso_inner_segment = ops->callbacks.gso_segment;
  149. break;
  150. default:
  151. goto out_unlock;
  152. }
  153. segs = __skb_udp_tunnel_segment(skb, features, gso_inner_segment,
  154. protocol, is_ipv6);
  155. out_unlock:
  156. rcu_read_unlock();
  157. return segs;
  158. }
  159. EXPORT_SYMBOL(skb_udp_tunnel_segment);
  160. static struct sk_buff *udp4_ufo_fragment(struct sk_buff *skb,
  161. netdev_features_t features)
  162. {
  163. struct sk_buff *segs = ERR_PTR(-EINVAL);
  164. unsigned int mss;
  165. __wsum csum;
  166. struct udphdr *uh;
  167. struct iphdr *iph;
  168. if (skb->encapsulation &&
  169. (skb_shinfo(skb)->gso_type &
  170. (SKB_GSO_UDP_TUNNEL|SKB_GSO_UDP_TUNNEL_CSUM))) {
  171. segs = skb_udp_tunnel_segment(skb, features, false);
  172. goto out;
  173. }
  174. if (!pskb_may_pull(skb, sizeof(struct udphdr)))
  175. goto out;
  176. mss = skb_shinfo(skb)->gso_size;
  177. if (unlikely(skb->len <= mss))
  178. goto out;
  179. if (skb_gso_ok(skb, features | NETIF_F_GSO_ROBUST)) {
  180. /* Packet is from an untrusted source, reset gso_segs. */
  181. skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(skb->len, mss);
  182. segs = NULL;
  183. goto out;
  184. }
  185. /* Do software UFO. Complete and fill in the UDP checksum as
  186. * HW cannot do checksum of UDP packets sent as multiple
  187. * IP fragments.
  188. */
  189. uh = udp_hdr(skb);
  190. iph = ip_hdr(skb);
  191. uh->check = 0;
  192. csum = skb_checksum(skb, 0, skb->len, 0);
  193. uh->check = udp_v4_check(skb->len, iph->saddr, iph->daddr, csum);
  194. if (uh->check == 0)
  195. uh->check = CSUM_MANGLED_0;
  196. skb->ip_summed = CHECKSUM_NONE;
  197. /* If there is no outer header we can fake a checksum offload
  198. * due to the fact that we have already done the checksum in
  199. * software prior to segmenting the frame.
  200. */
  201. if (!skb->encap_hdr_csum)
  202. features |= NETIF_F_HW_CSUM;
  203. /* Fragment the skb. IP headers of the fragments are updated in
  204. * inet_gso_segment()
  205. */
  206. segs = skb_segment(skb, features);
  207. out:
  208. return segs;
  209. }
  210. struct sk_buff **udp_gro_receive(struct sk_buff **head, struct sk_buff *skb,
  211. struct udphdr *uh, udp_lookup_t lookup)
  212. {
  213. struct sk_buff *p, **pp = NULL;
  214. struct udphdr *uh2;
  215. unsigned int off = skb_gro_offset(skb);
  216. int flush = 1;
  217. struct sock *sk;
  218. if (NAPI_GRO_CB(skb)->encap_mark ||
  219. (skb->ip_summed != CHECKSUM_PARTIAL &&
  220. NAPI_GRO_CB(skb)->csum_cnt == 0 &&
  221. !NAPI_GRO_CB(skb)->csum_valid))
  222. goto out;
  223. /* mark that this skb passed once through the tunnel gro layer */
  224. NAPI_GRO_CB(skb)->encap_mark = 1;
  225. rcu_read_lock();
  226. sk = (*lookup)(skb, uh->source, uh->dest);
  227. if (sk && udp_sk(sk)->gro_receive)
  228. goto unflush;
  229. goto out_unlock;
  230. unflush:
  231. flush = 0;
  232. for (p = *head; p; p = p->next) {
  233. if (!NAPI_GRO_CB(p)->same_flow)
  234. continue;
  235. uh2 = (struct udphdr *)(p->data + off);
  236. /* Match ports and either checksums are either both zero
  237. * or nonzero.
  238. */
  239. if ((*(u32 *)&uh->source != *(u32 *)&uh2->source) ||
  240. (!uh->check ^ !uh2->check)) {
  241. NAPI_GRO_CB(p)->same_flow = 0;
  242. continue;
  243. }
  244. }
  245. skb_gro_pull(skb, sizeof(struct udphdr)); /* pull encapsulating udp header */
  246. skb_gro_postpull_rcsum(skb, uh, sizeof(struct udphdr));
  247. pp = call_gro_receive_sk(udp_sk(sk)->gro_receive, sk, head, skb);
  248. out_unlock:
  249. rcu_read_unlock();
  250. out:
  251. NAPI_GRO_CB(skb)->flush |= flush;
  252. return pp;
  253. }
  254. EXPORT_SYMBOL(udp_gro_receive);
  255. static struct sk_buff **udp4_gro_receive(struct sk_buff **head,
  256. struct sk_buff *skb)
  257. {
  258. struct udphdr *uh = udp_gro_udphdr(skb);
  259. if (unlikely(!uh))
  260. goto flush;
  261. /* Don't bother verifying checksum if we're going to flush anyway. */
  262. if (NAPI_GRO_CB(skb)->flush)
  263. goto skip;
  264. if (skb_gro_checksum_validate_zero_check(skb, IPPROTO_UDP, uh->check,
  265. inet_gro_compute_pseudo))
  266. goto flush;
  267. else if (uh->check)
  268. skb_gro_checksum_try_convert(skb, IPPROTO_UDP, uh->check,
  269. inet_gro_compute_pseudo);
  270. skip:
  271. NAPI_GRO_CB(skb)->is_ipv6 = 0;
  272. return udp_gro_receive(head, skb, uh, udp4_lib_lookup_skb);
  273. flush:
  274. NAPI_GRO_CB(skb)->flush = 1;
  275. return NULL;
  276. }
  277. int udp_gro_complete(struct sk_buff *skb, int nhoff,
  278. udp_lookup_t lookup)
  279. {
  280. __be16 newlen = htons(skb->len - nhoff);
  281. struct udphdr *uh = (struct udphdr *)(skb->data + nhoff);
  282. int err = -ENOSYS;
  283. struct sock *sk;
  284. uh->len = newlen;
  285. /* Set encapsulation before calling into inner gro_complete() functions
  286. * to make them set up the inner offsets.
  287. */
  288. skb->encapsulation = 1;
  289. rcu_read_lock();
  290. sk = (*lookup)(skb, uh->source, uh->dest);
  291. if (sk && udp_sk(sk)->gro_complete)
  292. err = udp_sk(sk)->gro_complete(sk, skb,
  293. nhoff + sizeof(struct udphdr));
  294. rcu_read_unlock();
  295. if (skb->remcsum_offload)
  296. skb_shinfo(skb)->gso_type |= SKB_GSO_TUNNEL_REMCSUM;
  297. return err;
  298. }
  299. EXPORT_SYMBOL(udp_gro_complete);
  300. static int udp4_gro_complete(struct sk_buff *skb, int nhoff)
  301. {
  302. const struct iphdr *iph = ip_hdr(skb);
  303. struct udphdr *uh = (struct udphdr *)(skb->data + nhoff);
  304. if (uh->check) {
  305. skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM;
  306. uh->check = ~udp_v4_check(skb->len - nhoff, iph->saddr,
  307. iph->daddr, 0);
  308. } else {
  309. skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
  310. }
  311. return udp_gro_complete(skb, nhoff, udp4_lib_lookup_skb);
  312. }
  313. static const struct net_offload udpv4_offload = {
  314. .callbacks = {
  315. .gso_segment = udp4_ufo_fragment,
  316. .gro_receive = udp4_gro_receive,
  317. .gro_complete = udp4_gro_complete,
  318. },
  319. };
  320. int __init udpv4_offload_init(void)
  321. {
  322. return inet_add_offload(&udpv4_offload, IPPROTO_UDP);
  323. }