udp_offload.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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;
  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. outer_hlen = skb_tnl_header_len(skb);
  79. udp_offset = outer_hlen - tnl_hlen;
  80. skb = segs;
  81. do {
  82. unsigned int len;
  83. if (remcsum)
  84. skb->ip_summed = CHECKSUM_NONE;
  85. /* Set up inner headers if we are offloading inner checksum */
  86. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  87. skb_reset_inner_headers(skb);
  88. skb->encapsulation = 1;
  89. }
  90. skb->mac_len = mac_len;
  91. skb->protocol = protocol;
  92. __skb_push(skb, outer_hlen);
  93. skb_reset_mac_header(skb);
  94. skb_set_network_header(skb, mac_len);
  95. skb_set_transport_header(skb, udp_offset);
  96. len = skb->len - udp_offset;
  97. uh = udp_hdr(skb);
  98. /* If we are only performing partial GSO the inner header
  99. * will be using a length value equal to only one MSS sized
  100. * segment instead of the entire frame.
  101. */
  102. if (skb_is_gso(skb)) {
  103. uh->len = htons(skb_shinfo(skb)->gso_size +
  104. SKB_GSO_CB(skb)->data_offset +
  105. skb->head - (unsigned char *)uh);
  106. } else {
  107. uh->len = htons(len);
  108. }
  109. if (!need_csum)
  110. continue;
  111. uh->check = ~csum_fold(csum_add(partial,
  112. (__force __wsum)htonl(len)));
  113. if (skb->encapsulation || !offload_csum) {
  114. uh->check = gso_make_checksum(skb, ~uh->check);
  115. if (uh->check == 0)
  116. uh->check = CSUM_MANGLED_0;
  117. } else {
  118. skb->ip_summed = CHECKSUM_PARTIAL;
  119. skb->csum_start = skb_transport_header(skb) - skb->head;
  120. skb->csum_offset = offsetof(struct udphdr, check);
  121. }
  122. } while ((skb = skb->next));
  123. out:
  124. return segs;
  125. }
  126. struct sk_buff *skb_udp_tunnel_segment(struct sk_buff *skb,
  127. netdev_features_t features,
  128. bool is_ipv6)
  129. {
  130. __be16 protocol = skb->protocol;
  131. const struct net_offload **offloads;
  132. const struct net_offload *ops;
  133. struct sk_buff *segs = ERR_PTR(-EINVAL);
  134. struct sk_buff *(*gso_inner_segment)(struct sk_buff *skb,
  135. netdev_features_t features);
  136. rcu_read_lock();
  137. switch (skb->inner_protocol_type) {
  138. case ENCAP_TYPE_ETHER:
  139. protocol = skb->inner_protocol;
  140. gso_inner_segment = skb_mac_gso_segment;
  141. break;
  142. case ENCAP_TYPE_IPPROTO:
  143. offloads = is_ipv6 ? inet6_offloads : inet_offloads;
  144. ops = rcu_dereference(offloads[skb->inner_ipproto]);
  145. if (!ops || !ops->callbacks.gso_segment)
  146. goto out_unlock;
  147. gso_inner_segment = ops->callbacks.gso_segment;
  148. break;
  149. default:
  150. goto out_unlock;
  151. }
  152. segs = __skb_udp_tunnel_segment(skb, features, gso_inner_segment,
  153. protocol, is_ipv6);
  154. out_unlock:
  155. rcu_read_unlock();
  156. return segs;
  157. }
  158. EXPORT_SYMBOL(skb_udp_tunnel_segment);
  159. static struct sk_buff *udp4_ufo_fragment(struct sk_buff *skb,
  160. netdev_features_t features)
  161. {
  162. struct sk_buff *segs = ERR_PTR(-EINVAL);
  163. unsigned int mss;
  164. __wsum csum;
  165. struct udphdr *uh;
  166. struct iphdr *iph;
  167. if (skb->encapsulation &&
  168. (skb_shinfo(skb)->gso_type &
  169. (SKB_GSO_UDP_TUNNEL|SKB_GSO_UDP_TUNNEL_CSUM))) {
  170. segs = skb_udp_tunnel_segment(skb, features, false);
  171. goto out;
  172. }
  173. if (!pskb_may_pull(skb, sizeof(struct udphdr)))
  174. goto out;
  175. mss = skb_shinfo(skb)->gso_size;
  176. if (unlikely(skb->len <= mss))
  177. goto out;
  178. if (skb_gso_ok(skb, features | NETIF_F_GSO_ROBUST)) {
  179. /* Packet is from an untrusted source, reset gso_segs. */
  180. skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(skb->len, mss);
  181. segs = NULL;
  182. goto out;
  183. }
  184. /* Do software UFO. Complete and fill in the UDP checksum as
  185. * HW cannot do checksum of UDP packets sent as multiple
  186. * IP fragments.
  187. */
  188. uh = udp_hdr(skb);
  189. iph = ip_hdr(skb);
  190. uh->check = 0;
  191. csum = skb_checksum(skb, 0, skb->len, 0);
  192. uh->check = udp_v4_check(skb->len, iph->saddr, iph->daddr, csum);
  193. if (uh->check == 0)
  194. uh->check = CSUM_MANGLED_0;
  195. skb->ip_summed = CHECKSUM_NONE;
  196. /* If there is no outer header we can fake a checksum offload
  197. * due to the fact that we have already done the checksum in
  198. * software prior to segmenting the frame.
  199. */
  200. if (!skb->encap_hdr_csum)
  201. features |= NETIF_F_HW_CSUM;
  202. /* Fragment the skb. IP headers of the fragments are updated in
  203. * inet_gso_segment()
  204. */
  205. segs = skb_segment(skb, features);
  206. out:
  207. return segs;
  208. }
  209. struct sk_buff **udp_gro_receive(struct sk_buff **head, struct sk_buff *skb,
  210. struct udphdr *uh, udp_lookup_t lookup)
  211. {
  212. struct sk_buff *p, **pp = NULL;
  213. struct udphdr *uh2;
  214. unsigned int off = skb_gro_offset(skb);
  215. int flush = 1;
  216. struct sock *sk;
  217. if (NAPI_GRO_CB(skb)->encap_mark ||
  218. (skb->ip_summed != CHECKSUM_PARTIAL &&
  219. NAPI_GRO_CB(skb)->csum_cnt == 0 &&
  220. !NAPI_GRO_CB(skb)->csum_valid))
  221. goto out;
  222. /* mark that this skb passed once through the tunnel gro layer */
  223. NAPI_GRO_CB(skb)->encap_mark = 1;
  224. rcu_read_lock();
  225. sk = (*lookup)(skb, uh->source, uh->dest);
  226. if (sk && udp_sk(sk)->gro_receive)
  227. goto unflush;
  228. goto out_unlock;
  229. unflush:
  230. flush = 0;
  231. for (p = *head; p; p = p->next) {
  232. if (!NAPI_GRO_CB(p)->same_flow)
  233. continue;
  234. uh2 = (struct udphdr *)(p->data + off);
  235. /* Match ports and either checksums are either both zero
  236. * or nonzero.
  237. */
  238. if ((*(u32 *)&uh->source != *(u32 *)&uh2->source) ||
  239. (!uh->check ^ !uh2->check)) {
  240. NAPI_GRO_CB(p)->same_flow = 0;
  241. continue;
  242. }
  243. }
  244. skb_gro_pull(skb, sizeof(struct udphdr)); /* pull encapsulating udp header */
  245. skb_gro_postpull_rcsum(skb, uh, sizeof(struct udphdr));
  246. pp = udp_sk(sk)->gro_receive(sk, head, skb);
  247. out_unlock:
  248. rcu_read_unlock();
  249. out:
  250. NAPI_GRO_CB(skb)->flush |= flush;
  251. return pp;
  252. }
  253. EXPORT_SYMBOL(udp_gro_receive);
  254. static struct sk_buff **udp4_gro_receive(struct sk_buff **head,
  255. struct sk_buff *skb)
  256. {
  257. struct udphdr *uh = udp_gro_udphdr(skb);
  258. if (unlikely(!uh))
  259. goto flush;
  260. /* Don't bother verifying checksum if we're going to flush anyway. */
  261. if (NAPI_GRO_CB(skb)->flush)
  262. goto skip;
  263. if (skb_gro_checksum_validate_zero_check(skb, IPPROTO_UDP, uh->check,
  264. inet_gro_compute_pseudo))
  265. goto flush;
  266. else if (uh->check)
  267. skb_gro_checksum_try_convert(skb, IPPROTO_UDP, uh->check,
  268. inet_gro_compute_pseudo);
  269. skip:
  270. NAPI_GRO_CB(skb)->is_ipv6 = 0;
  271. return udp_gro_receive(head, skb, uh, udp4_lib_lookup_skb);
  272. flush:
  273. NAPI_GRO_CB(skb)->flush = 1;
  274. return NULL;
  275. }
  276. int udp_gro_complete(struct sk_buff *skb, int nhoff,
  277. udp_lookup_t lookup)
  278. {
  279. __be16 newlen = htons(skb->len - nhoff);
  280. struct udphdr *uh = (struct udphdr *)(skb->data + nhoff);
  281. int err = -ENOSYS;
  282. struct sock *sk;
  283. uh->len = newlen;
  284. /* Set encapsulation before calling into inner gro_complete() functions
  285. * to make them set up the inner offsets.
  286. */
  287. skb->encapsulation = 1;
  288. rcu_read_lock();
  289. sk = (*lookup)(skb, uh->source, uh->dest);
  290. if (sk && udp_sk(sk)->gro_complete)
  291. err = udp_sk(sk)->gro_complete(sk, skb,
  292. nhoff + sizeof(struct udphdr));
  293. rcu_read_unlock();
  294. if (skb->remcsum_offload)
  295. skb_shinfo(skb)->gso_type |= SKB_GSO_TUNNEL_REMCSUM;
  296. return err;
  297. }
  298. EXPORT_SYMBOL(udp_gro_complete);
  299. static int udp4_gro_complete(struct sk_buff *skb, int nhoff)
  300. {
  301. const struct iphdr *iph = ip_hdr(skb);
  302. struct udphdr *uh = (struct udphdr *)(skb->data + nhoff);
  303. if (uh->check) {
  304. skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM;
  305. uh->check = ~udp_v4_check(skb->len - nhoff, iph->saddr,
  306. iph->daddr, 0);
  307. } else {
  308. skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
  309. }
  310. return udp_gro_complete(skb, nhoff, udp4_lib_lookup_skb);
  311. }
  312. static const struct net_offload udpv4_offload = {
  313. .callbacks = {
  314. .gso_segment = udp4_ufo_fragment,
  315. .gro_receive = udp4_gro_receive,
  316. .gro_complete = udp4_gro_complete,
  317. },
  318. };
  319. int __init udpv4_offload_init(void)
  320. {
  321. return inet_add_offload(&udpv4_offload, IPPROTO_UDP);
  322. }