udp_offload.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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, 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. need_ipsec = skb_dst(skb) && dst_xfrm(skb_dst(skb));
  57. /* Try to offload checksum if possible */
  58. offload_csum = !!(need_csum &&
  59. !need_ipsec &&
  60. (skb->dev->features &
  61. (is_ipv6 ? (NETIF_F_HW_CSUM | NETIF_F_IPV6_CSUM) :
  62. (NETIF_F_HW_CSUM | NETIF_F_IP_CSUM))));
  63. features &= skb->dev->hw_enc_features;
  64. /* The only checksum offload we care about from here on out is the
  65. * outer one so strip the existing checksum feature flags and
  66. * instead set the flag based on our outer checksum offload value.
  67. */
  68. if (remcsum) {
  69. features &= ~NETIF_F_CSUM_MASK;
  70. if (!need_csum || offload_csum)
  71. features |= NETIF_F_HW_CSUM;
  72. }
  73. /* segment inner packet. */
  74. segs = gso_inner_segment(skb, features);
  75. if (IS_ERR_OR_NULL(segs)) {
  76. skb_gso_error_unwind(skb, protocol, tnl_hlen, mac_offset,
  77. mac_len);
  78. goto out;
  79. }
  80. gso_partial = !!(skb_shinfo(segs)->gso_type & SKB_GSO_PARTIAL);
  81. outer_hlen = skb_tnl_header_len(skb);
  82. udp_offset = outer_hlen - tnl_hlen;
  83. skb = segs;
  84. do {
  85. unsigned int len;
  86. if (remcsum)
  87. skb->ip_summed = CHECKSUM_NONE;
  88. /* Set up inner headers if we are offloading inner checksum */
  89. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  90. skb_reset_inner_headers(skb);
  91. skb->encapsulation = 1;
  92. }
  93. skb->mac_len = mac_len;
  94. skb->protocol = protocol;
  95. __skb_push(skb, outer_hlen);
  96. skb_reset_mac_header(skb);
  97. skb_set_network_header(skb, mac_len);
  98. skb_set_transport_header(skb, udp_offset);
  99. len = skb->len - udp_offset;
  100. uh = udp_hdr(skb);
  101. /* If we are only performing partial GSO the inner header
  102. * will be using a length value equal to only one MSS sized
  103. * segment instead of the entire frame.
  104. */
  105. if (gso_partial && skb_is_gso(skb)) {
  106. uh->len = htons(skb_shinfo(skb)->gso_size +
  107. SKB_GSO_CB(skb)->data_offset +
  108. skb->head - (unsigned char *)uh);
  109. } else {
  110. uh->len = htons(len);
  111. }
  112. if (!need_csum)
  113. continue;
  114. uh->check = ~csum_fold(csum_add(partial,
  115. (__force __wsum)htonl(len)));
  116. if (skb->encapsulation || !offload_csum) {
  117. uh->check = gso_make_checksum(skb, ~uh->check);
  118. if (uh->check == 0)
  119. uh->check = CSUM_MANGLED_0;
  120. } else {
  121. skb->ip_summed = CHECKSUM_PARTIAL;
  122. skb->csum_start = skb_transport_header(skb) - skb->head;
  123. skb->csum_offset = offsetof(struct udphdr, check);
  124. }
  125. } while ((skb = skb->next));
  126. out:
  127. return segs;
  128. }
  129. struct sk_buff *skb_udp_tunnel_segment(struct sk_buff *skb,
  130. netdev_features_t features,
  131. bool is_ipv6)
  132. {
  133. __be16 protocol = skb->protocol;
  134. const struct net_offload **offloads;
  135. const struct net_offload *ops;
  136. struct sk_buff *segs = ERR_PTR(-EINVAL);
  137. struct sk_buff *(*gso_inner_segment)(struct sk_buff *skb,
  138. netdev_features_t features);
  139. rcu_read_lock();
  140. switch (skb->inner_protocol_type) {
  141. case ENCAP_TYPE_ETHER:
  142. protocol = skb->inner_protocol;
  143. gso_inner_segment = skb_mac_gso_segment;
  144. break;
  145. case ENCAP_TYPE_IPPROTO:
  146. offloads = is_ipv6 ? inet6_offloads : inet_offloads;
  147. ops = rcu_dereference(offloads[skb->inner_ipproto]);
  148. if (!ops || !ops->callbacks.gso_segment)
  149. goto out_unlock;
  150. gso_inner_segment = ops->callbacks.gso_segment;
  151. break;
  152. default:
  153. goto out_unlock;
  154. }
  155. segs = __skb_udp_tunnel_segment(skb, features, gso_inner_segment,
  156. protocol, is_ipv6);
  157. out_unlock:
  158. rcu_read_unlock();
  159. return segs;
  160. }
  161. EXPORT_SYMBOL(skb_udp_tunnel_segment);
  162. struct sk_buff *__udp_gso_segment(struct sk_buff *gso_skb,
  163. netdev_features_t features)
  164. {
  165. struct sock *sk = gso_skb->sk;
  166. unsigned int sum_truesize = 0;
  167. struct sk_buff *segs, *seg;
  168. struct udphdr *uh;
  169. unsigned int mss;
  170. bool copy_dtor;
  171. __sum16 check;
  172. __be16 newlen;
  173. mss = skb_shinfo(gso_skb)->gso_size;
  174. if (gso_skb->len <= sizeof(*uh) + mss)
  175. return ERR_PTR(-EINVAL);
  176. skb_pull(gso_skb, sizeof(*uh));
  177. /* clear destructor to avoid skb_segment assigning it to tail */
  178. copy_dtor = gso_skb->destructor == sock_wfree;
  179. if (copy_dtor)
  180. gso_skb->destructor = NULL;
  181. segs = skb_segment(gso_skb, features);
  182. if (unlikely(IS_ERR_OR_NULL(segs))) {
  183. if (copy_dtor)
  184. gso_skb->destructor = sock_wfree;
  185. return segs;
  186. }
  187. /* GSO partial and frag_list segmentation only requires splitting
  188. * the frame into an MSS multiple and possibly a remainder, both
  189. * cases return a GSO skb. So update the mss now.
  190. */
  191. if (skb_is_gso(segs))
  192. mss *= skb_shinfo(segs)->gso_segs;
  193. seg = segs;
  194. uh = udp_hdr(seg);
  195. /* preserve TX timestamp flags and TS key for first segment */
  196. skb_shinfo(seg)->tskey = skb_shinfo(gso_skb)->tskey;
  197. skb_shinfo(seg)->tx_flags |=
  198. (skb_shinfo(gso_skb)->tx_flags & SKBTX_ANY_TSTAMP);
  199. /* compute checksum adjustment based on old length versus new */
  200. newlen = htons(sizeof(*uh) + mss);
  201. check = csum16_add(csum16_sub(uh->check, uh->len), newlen);
  202. for (;;) {
  203. if (copy_dtor) {
  204. seg->destructor = sock_wfree;
  205. seg->sk = sk;
  206. sum_truesize += seg->truesize;
  207. }
  208. if (!seg->next)
  209. break;
  210. uh->len = newlen;
  211. uh->check = check;
  212. if (seg->ip_summed == CHECKSUM_PARTIAL)
  213. gso_reset_checksum(seg, ~check);
  214. else
  215. uh->check = gso_make_checksum(seg, ~check) ? :
  216. CSUM_MANGLED_0;
  217. seg = seg->next;
  218. uh = udp_hdr(seg);
  219. }
  220. /* last packet can be partial gso_size, account for that in checksum */
  221. newlen = htons(skb_tail_pointer(seg) - skb_transport_header(seg) +
  222. seg->data_len);
  223. check = csum16_add(csum16_sub(uh->check, uh->len), newlen);
  224. uh->len = newlen;
  225. uh->check = check;
  226. if (seg->ip_summed == CHECKSUM_PARTIAL)
  227. gso_reset_checksum(seg, ~check);
  228. else
  229. uh->check = gso_make_checksum(seg, ~check) ? : CSUM_MANGLED_0;
  230. /* update refcount for the packet */
  231. if (copy_dtor) {
  232. int delta = sum_truesize - gso_skb->truesize;
  233. /* In some pathological cases, delta can be negative.
  234. * We need to either use refcount_add() or refcount_sub_and_test()
  235. */
  236. if (likely(delta >= 0))
  237. refcount_add(delta, &sk->sk_wmem_alloc);
  238. else
  239. WARN_ON_ONCE(refcount_sub_and_test(-delta, &sk->sk_wmem_alloc));
  240. }
  241. return segs;
  242. }
  243. EXPORT_SYMBOL_GPL(__udp_gso_segment);
  244. static struct sk_buff *udp4_ufo_fragment(struct sk_buff *skb,
  245. netdev_features_t features)
  246. {
  247. struct sk_buff *segs = ERR_PTR(-EINVAL);
  248. unsigned int mss;
  249. __wsum csum;
  250. struct udphdr *uh;
  251. struct iphdr *iph;
  252. if (skb->encapsulation &&
  253. (skb_shinfo(skb)->gso_type &
  254. (SKB_GSO_UDP_TUNNEL|SKB_GSO_UDP_TUNNEL_CSUM))) {
  255. segs = skb_udp_tunnel_segment(skb, features, false);
  256. goto out;
  257. }
  258. if (!(skb_shinfo(skb)->gso_type & (SKB_GSO_UDP | SKB_GSO_UDP_L4)))
  259. goto out;
  260. if (!pskb_may_pull(skb, sizeof(struct udphdr)))
  261. goto out;
  262. if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4)
  263. return __udp_gso_segment(skb, features);
  264. mss = skb_shinfo(skb)->gso_size;
  265. if (unlikely(skb->len <= mss))
  266. goto out;
  267. /* Do software UFO. Complete and fill in the UDP checksum as
  268. * HW cannot do checksum of UDP packets sent as multiple
  269. * IP fragments.
  270. */
  271. uh = udp_hdr(skb);
  272. iph = ip_hdr(skb);
  273. uh->check = 0;
  274. csum = skb_checksum(skb, 0, skb->len, 0);
  275. uh->check = udp_v4_check(skb->len, iph->saddr, iph->daddr, csum);
  276. if (uh->check == 0)
  277. uh->check = CSUM_MANGLED_0;
  278. skb->ip_summed = CHECKSUM_UNNECESSARY;
  279. /* If there is no outer header we can fake a checksum offload
  280. * due to the fact that we have already done the checksum in
  281. * software prior to segmenting the frame.
  282. */
  283. if (!skb->encap_hdr_csum)
  284. features |= NETIF_F_HW_CSUM;
  285. /* Fragment the skb. IP headers of the fragments are updated in
  286. * inet_gso_segment()
  287. */
  288. segs = skb_segment(skb, features);
  289. out:
  290. return segs;
  291. }
  292. struct sk_buff *udp_gro_receive(struct list_head *head, struct sk_buff *skb,
  293. struct udphdr *uh, udp_lookup_t lookup)
  294. {
  295. struct sk_buff *pp = NULL;
  296. struct sk_buff *p;
  297. struct udphdr *uh2;
  298. unsigned int off = skb_gro_offset(skb);
  299. int flush = 1;
  300. struct sock *sk;
  301. if (NAPI_GRO_CB(skb)->encap_mark ||
  302. (skb->ip_summed != CHECKSUM_PARTIAL &&
  303. NAPI_GRO_CB(skb)->csum_cnt == 0 &&
  304. !NAPI_GRO_CB(skb)->csum_valid))
  305. goto out;
  306. /* mark that this skb passed once through the tunnel gro layer */
  307. NAPI_GRO_CB(skb)->encap_mark = 1;
  308. rcu_read_lock();
  309. sk = (*lookup)(skb, uh->source, uh->dest);
  310. if (sk && udp_sk(sk)->gro_receive)
  311. goto unflush;
  312. goto out_unlock;
  313. unflush:
  314. flush = 0;
  315. list_for_each_entry(p, head, list) {
  316. if (!NAPI_GRO_CB(p)->same_flow)
  317. continue;
  318. uh2 = (struct udphdr *)(p->data + off);
  319. /* Match ports and either checksums are either both zero
  320. * or nonzero.
  321. */
  322. if ((*(u32 *)&uh->source != *(u32 *)&uh2->source) ||
  323. (!uh->check ^ !uh2->check)) {
  324. NAPI_GRO_CB(p)->same_flow = 0;
  325. continue;
  326. }
  327. }
  328. skb_gro_pull(skb, sizeof(struct udphdr)); /* pull encapsulating udp header */
  329. skb_gro_postpull_rcsum(skb, uh, sizeof(struct udphdr));
  330. pp = call_gro_receive_sk(udp_sk(sk)->gro_receive, sk, head, skb);
  331. out_unlock:
  332. rcu_read_unlock();
  333. out:
  334. skb_gro_flush_final(skb, pp, flush);
  335. return pp;
  336. }
  337. EXPORT_SYMBOL(udp_gro_receive);
  338. static struct sk_buff *udp4_gro_receive(struct list_head *head,
  339. struct sk_buff *skb)
  340. {
  341. struct udphdr *uh = udp_gro_udphdr(skb);
  342. if (unlikely(!uh))
  343. goto flush;
  344. /* Don't bother verifying checksum if we're going to flush anyway. */
  345. if (NAPI_GRO_CB(skb)->flush)
  346. goto skip;
  347. if (skb_gro_checksum_validate_zero_check(skb, IPPROTO_UDP, uh->check,
  348. inet_gro_compute_pseudo))
  349. goto flush;
  350. else if (uh->check)
  351. skb_gro_checksum_try_convert(skb, IPPROTO_UDP, uh->check,
  352. inet_gro_compute_pseudo);
  353. skip:
  354. NAPI_GRO_CB(skb)->is_ipv6 = 0;
  355. return udp_gro_receive(head, skb, uh, udp4_lib_lookup_skb);
  356. flush:
  357. NAPI_GRO_CB(skb)->flush = 1;
  358. return NULL;
  359. }
  360. int udp_gro_complete(struct sk_buff *skb, int nhoff,
  361. udp_lookup_t lookup)
  362. {
  363. __be16 newlen = htons(skb->len - nhoff);
  364. struct udphdr *uh = (struct udphdr *)(skb->data + nhoff);
  365. int err = -ENOSYS;
  366. struct sock *sk;
  367. uh->len = newlen;
  368. /* Set encapsulation before calling into inner gro_complete() functions
  369. * to make them set up the inner offsets.
  370. */
  371. skb->encapsulation = 1;
  372. rcu_read_lock();
  373. sk = (*lookup)(skb, uh->source, uh->dest);
  374. if (sk && udp_sk(sk)->gro_complete)
  375. err = udp_sk(sk)->gro_complete(sk, skb,
  376. nhoff + sizeof(struct udphdr));
  377. rcu_read_unlock();
  378. if (skb->remcsum_offload)
  379. skb_shinfo(skb)->gso_type |= SKB_GSO_TUNNEL_REMCSUM;
  380. return err;
  381. }
  382. EXPORT_SYMBOL(udp_gro_complete);
  383. static int udp4_gro_complete(struct sk_buff *skb, int nhoff)
  384. {
  385. const struct iphdr *iph = ip_hdr(skb);
  386. struct udphdr *uh = (struct udphdr *)(skb->data + nhoff);
  387. if (uh->check) {
  388. skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM;
  389. uh->check = ~udp_v4_check(skb->len - nhoff, iph->saddr,
  390. iph->daddr, 0);
  391. } else {
  392. skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
  393. }
  394. return udp_gro_complete(skb, nhoff, udp4_lib_lookup_skb);
  395. }
  396. static const struct net_offload udpv4_offload = {
  397. .callbacks = {
  398. .gso_segment = udp4_ufo_fragment,
  399. .gro_receive = udp4_gro_receive,
  400. .gro_complete = udp4_gro_complete,
  401. },
  402. };
  403. int __init udpv4_offload_init(void)
  404. {
  405. return inet_add_offload(&udpv4_offload, IPPROTO_UDP);
  406. }