udp_offload.c 10 KB

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