udp_offload.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * IPV4 GSO/GRO offload support
  4. * Linux INET implementation
  5. *
  6. * UDPv4 GSO support
  7. */
  8. #include <linux/skbuff.h>
  9. #include <net/gro.h>
  10. #include <net/gso.h>
  11. #include <net/udp.h>
  12. #include <net/protocol.h>
  13. #include <net/inet_common.h>
  14. static struct sk_buff *__skb_udp_tunnel_segment(struct sk_buff *skb,
  15. netdev_features_t features,
  16. struct sk_buff *(*gso_inner_segment)(struct sk_buff *skb,
  17. netdev_features_t features),
  18. __be16 new_protocol, bool is_ipv6)
  19. {
  20. int tnl_hlen = skb_inner_mac_header(skb) - skb_transport_header(skb);
  21. bool remcsum, need_csum, offload_csum, gso_partial;
  22. struct sk_buff *segs = ERR_PTR(-EINVAL);
  23. struct udphdr *uh = udp_hdr(skb);
  24. u16 mac_offset = skb->mac_header;
  25. __be16 protocol = skb->protocol;
  26. u16 mac_len = skb->mac_len;
  27. int udp_offset, outer_hlen;
  28. __wsum partial;
  29. bool need_ipsec;
  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_set_transport_header(skb, skb_inner_transport_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))) || skb_sec_path(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. if (need_csum)
  65. features &= ~NETIF_F_SCTP_CRC;
  66. /* The only checksum offload we care about from here on out is the
  67. * outer one so strip the existing checksum feature flags and
  68. * instead set the flag based on our outer checksum offload value.
  69. */
  70. if (remcsum) {
  71. features &= ~NETIF_F_CSUM_MASK;
  72. if (!need_csum || offload_csum)
  73. features |= NETIF_F_HW_CSUM;
  74. }
  75. /* segment inner packet. */
  76. segs = gso_inner_segment(skb, features);
  77. if (IS_ERR_OR_NULL(segs)) {
  78. skb_gso_error_unwind(skb, protocol, tnl_hlen, mac_offset,
  79. mac_len);
  80. goto out;
  81. }
  82. gso_partial = !!(skb_shinfo(segs)->gso_type & SKB_GSO_PARTIAL);
  83. outer_hlen = skb_tnl_header_len(skb);
  84. udp_offset = outer_hlen - tnl_hlen;
  85. skb = segs;
  86. do {
  87. unsigned int len;
  88. if (remcsum)
  89. skb->ip_summed = CHECKSUM_NONE;
  90. /* Set up inner headers if we are offloading inner checksum */
  91. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  92. skb_reset_inner_headers(skb);
  93. skb->encapsulation = 1;
  94. }
  95. skb->mac_len = mac_len;
  96. skb->protocol = protocol;
  97. __skb_push(skb, outer_hlen);
  98. skb_reset_mac_header(skb);
  99. skb_set_network_header(skb, mac_len);
  100. skb_set_transport_header(skb, udp_offset);
  101. len = skb->len - udp_offset;
  102. uh = udp_hdr(skb);
  103. /* If we are only performing partial GSO the inner header
  104. * will be using a length value equal to only one MSS sized
  105. * segment instead of the entire frame.
  106. */
  107. if (gso_partial && skb_is_gso(skb)) {
  108. uh->len = htons(skb_shinfo(skb)->gso_size +
  109. SKB_GSO_CB(skb)->data_offset +
  110. skb->head - (unsigned char *)uh);
  111. } else {
  112. uh->len = htons(len);
  113. }
  114. if (!need_csum)
  115. continue;
  116. uh->check = ~csum_fold(csum_add(partial,
  117. (__force __wsum)htonl(len)));
  118. if (skb->encapsulation || !offload_csum) {
  119. uh->check = gso_make_checksum(skb, ~uh->check);
  120. if (uh->check == 0)
  121. uh->check = CSUM_MANGLED_0;
  122. } else {
  123. skb->ip_summed = CHECKSUM_PARTIAL;
  124. skb->csum_start = skb_transport_header(skb) - skb->head;
  125. skb->csum_offset = offsetof(struct udphdr, check);
  126. }
  127. } while ((skb = skb->next));
  128. out:
  129. return segs;
  130. }
  131. struct sk_buff *skb_udp_tunnel_segment(struct sk_buff *skb,
  132. netdev_features_t features,
  133. bool is_ipv6)
  134. {
  135. const struct net_offload __rcu **offloads;
  136. __be16 protocol = skb->protocol;
  137. const struct net_offload *ops;
  138. struct sk_buff *segs = ERR_PTR(-EINVAL);
  139. struct sk_buff *(*gso_inner_segment)(struct sk_buff *skb,
  140. netdev_features_t features);
  141. rcu_read_lock();
  142. switch (skb->inner_protocol_type) {
  143. case ENCAP_TYPE_ETHER:
  144. protocol = skb->inner_protocol;
  145. gso_inner_segment = skb_mac_gso_segment;
  146. break;
  147. case ENCAP_TYPE_IPPROTO:
  148. offloads = is_ipv6 ? inet6_offloads : inet_offloads;
  149. ops = rcu_dereference(offloads[skb->inner_ipproto]);
  150. if (!ops || !ops->callbacks.gso_segment)
  151. goto out_unlock;
  152. gso_inner_segment = ops->callbacks.gso_segment;
  153. break;
  154. default:
  155. goto out_unlock;
  156. }
  157. segs = __skb_udp_tunnel_segment(skb, features, gso_inner_segment,
  158. protocol, is_ipv6);
  159. out_unlock:
  160. rcu_read_unlock();
  161. return segs;
  162. }
  163. EXPORT_SYMBOL(skb_udp_tunnel_segment);
  164. static void __udpv4_gso_segment_csum(struct sk_buff *seg,
  165. __be32 *oldip, __be32 *newip,
  166. __be16 *oldport, __be16 *newport)
  167. {
  168. struct udphdr *uh;
  169. struct iphdr *iph;
  170. if (*oldip == *newip && *oldport == *newport)
  171. return;
  172. uh = udp_hdr(seg);
  173. iph = ip_hdr(seg);
  174. if (uh->check) {
  175. inet_proto_csum_replace4(&uh->check, seg, *oldip, *newip,
  176. true);
  177. inet_proto_csum_replace2(&uh->check, seg, *oldport, *newport,
  178. false);
  179. if (!uh->check)
  180. uh->check = CSUM_MANGLED_0;
  181. }
  182. *oldport = *newport;
  183. csum_replace4(&iph->check, *oldip, *newip);
  184. *oldip = *newip;
  185. }
  186. static struct sk_buff *__udpv4_gso_segment_list_csum(struct sk_buff *segs)
  187. {
  188. struct sk_buff *seg;
  189. struct udphdr *uh, *uh2;
  190. struct iphdr *iph, *iph2;
  191. seg = segs;
  192. uh = udp_hdr(seg);
  193. iph = ip_hdr(seg);
  194. if ((udp_hdr(seg)->dest == udp_hdr(seg->next)->dest) &&
  195. (udp_hdr(seg)->source == udp_hdr(seg->next)->source) &&
  196. (ip_hdr(seg)->daddr == ip_hdr(seg->next)->daddr) &&
  197. (ip_hdr(seg)->saddr == ip_hdr(seg->next)->saddr))
  198. return segs;
  199. while ((seg = seg->next)) {
  200. uh2 = udp_hdr(seg);
  201. iph2 = ip_hdr(seg);
  202. __udpv4_gso_segment_csum(seg,
  203. &iph2->saddr, &iph->saddr,
  204. &uh2->source, &uh->source);
  205. __udpv4_gso_segment_csum(seg,
  206. &iph2->daddr, &iph->daddr,
  207. &uh2->dest, &uh->dest);
  208. }
  209. return segs;
  210. }
  211. static void __udpv6_gso_segment_csum(struct sk_buff *seg,
  212. struct in6_addr *oldip,
  213. const struct in6_addr *newip,
  214. __be16 *oldport, __be16 newport)
  215. {
  216. struct udphdr *uh = udp_hdr(seg);
  217. if (ipv6_addr_equal(oldip, newip) && *oldport == newport)
  218. return;
  219. if (uh->check) {
  220. inet_proto_csum_replace16(&uh->check, seg, oldip->s6_addr32,
  221. newip->s6_addr32, true);
  222. inet_proto_csum_replace2(&uh->check, seg, *oldport, newport,
  223. false);
  224. if (!uh->check)
  225. uh->check = CSUM_MANGLED_0;
  226. }
  227. *oldip = *newip;
  228. *oldport = newport;
  229. }
  230. static struct sk_buff *__udpv6_gso_segment_list_csum(struct sk_buff *segs)
  231. {
  232. const struct ipv6hdr *iph;
  233. const struct udphdr *uh;
  234. struct ipv6hdr *iph2;
  235. struct sk_buff *seg;
  236. struct udphdr *uh2;
  237. seg = segs;
  238. uh = udp_hdr(seg);
  239. iph = ipv6_hdr(seg);
  240. uh2 = udp_hdr(seg->next);
  241. iph2 = ipv6_hdr(seg->next);
  242. if (!(*(const u32 *)&uh->source ^ *(const u32 *)&uh2->source) &&
  243. ipv6_addr_equal(&iph->saddr, &iph2->saddr) &&
  244. ipv6_addr_equal(&iph->daddr, &iph2->daddr))
  245. return segs;
  246. while ((seg = seg->next)) {
  247. uh2 = udp_hdr(seg);
  248. iph2 = ipv6_hdr(seg);
  249. __udpv6_gso_segment_csum(seg, &iph2->saddr, &iph->saddr,
  250. &uh2->source, uh->source);
  251. __udpv6_gso_segment_csum(seg, &iph2->daddr, &iph->daddr,
  252. &uh2->dest, uh->dest);
  253. }
  254. return segs;
  255. }
  256. static struct sk_buff *__udp_gso_segment_list(struct sk_buff *skb,
  257. netdev_features_t features,
  258. bool is_ipv6)
  259. {
  260. unsigned int mss = skb_shinfo(skb)->gso_size;
  261. skb = skb_segment_list(skb, features, skb_mac_header_len(skb));
  262. if (IS_ERR(skb))
  263. return skb;
  264. udp_hdr(skb)->len = htons(sizeof(struct udphdr) + mss);
  265. if (is_ipv6)
  266. return __udpv6_gso_segment_list_csum(skb);
  267. else
  268. return __udpv4_gso_segment_list_csum(skb);
  269. }
  270. struct sk_buff *__udp_gso_segment(struct sk_buff *gso_skb,
  271. netdev_features_t features, bool is_ipv6)
  272. {
  273. struct sock *sk = gso_skb->sk;
  274. unsigned int sum_truesize = 0;
  275. struct sk_buff *segs, *seg;
  276. struct udphdr *uh;
  277. unsigned int mss;
  278. bool copy_dtor;
  279. __sum16 check;
  280. __be16 newlen;
  281. int ret = 0;
  282. mss = skb_shinfo(gso_skb)->gso_size;
  283. if (gso_skb->len <= sizeof(*uh) + mss)
  284. return ERR_PTR(-EINVAL);
  285. if (unlikely(skb_checksum_start(gso_skb) !=
  286. skb_transport_header(gso_skb) &&
  287. !(skb_shinfo(gso_skb)->gso_type & SKB_GSO_FRAGLIST)))
  288. return ERR_PTR(-EINVAL);
  289. /* We don't know if egress device can segment and checksum the packet
  290. * when IPv6 extension headers are present. Fall back to software GSO.
  291. */
  292. if (gso_skb->ip_summed != CHECKSUM_PARTIAL)
  293. features &= ~(NETIF_F_GSO_UDP_L4 | NETIF_F_CSUM_MASK);
  294. if (skb_gso_ok(gso_skb, features | NETIF_F_GSO_ROBUST)) {
  295. /* Packet is from an untrusted source, reset gso_segs. */
  296. skb_shinfo(gso_skb)->gso_segs = DIV_ROUND_UP(gso_skb->len - sizeof(*uh),
  297. mss);
  298. return NULL;
  299. }
  300. if (skb_shinfo(gso_skb)->gso_type & SKB_GSO_FRAGLIST) {
  301. /* Detect modified geometry and pass those to skb_segment. */
  302. if (skb_pagelen(gso_skb) - sizeof(*uh) == skb_shinfo(gso_skb)->gso_size)
  303. return __udp_gso_segment_list(gso_skb, features, is_ipv6);
  304. ret = __skb_linearize(gso_skb);
  305. if (ret)
  306. return ERR_PTR(ret);
  307. /* Setup csum, as fraglist skips this in udp4_gro_receive. */
  308. gso_skb->csum_start = skb_transport_header(gso_skb) - gso_skb->head;
  309. gso_skb->csum_offset = offsetof(struct udphdr, check);
  310. gso_skb->ip_summed = CHECKSUM_PARTIAL;
  311. uh = udp_hdr(gso_skb);
  312. if (is_ipv6)
  313. uh->check = ~udp_v6_check(gso_skb->len,
  314. &ipv6_hdr(gso_skb)->saddr,
  315. &ipv6_hdr(gso_skb)->daddr, 0);
  316. else
  317. uh->check = ~udp_v4_check(gso_skb->len,
  318. ip_hdr(gso_skb)->saddr,
  319. ip_hdr(gso_skb)->daddr, 0);
  320. }
  321. skb_pull(gso_skb, sizeof(*uh));
  322. /* clear destructor to avoid skb_segment assigning it to tail */
  323. copy_dtor = gso_skb->destructor == sock_wfree;
  324. if (copy_dtor) {
  325. gso_skb->destructor = NULL;
  326. gso_skb->sk = NULL;
  327. }
  328. segs = skb_segment(gso_skb, features);
  329. if (IS_ERR_OR_NULL(segs)) {
  330. if (copy_dtor) {
  331. gso_skb->destructor = sock_wfree;
  332. gso_skb->sk = sk;
  333. }
  334. return segs;
  335. }
  336. /* GSO partial and frag_list segmentation only requires splitting
  337. * the frame into an MSS multiple and possibly a remainder, both
  338. * cases return a GSO skb. So update the mss now.
  339. */
  340. if (skb_is_gso(segs))
  341. mss *= skb_shinfo(segs)->gso_segs;
  342. seg = segs;
  343. uh = udp_hdr(seg);
  344. /* preserve TX timestamp flags and TS key for first segment */
  345. skb_shinfo(seg)->tskey = skb_shinfo(gso_skb)->tskey;
  346. skb_shinfo(seg)->tx_flags |=
  347. (skb_shinfo(gso_skb)->tx_flags & SKBTX_ANY_TSTAMP);
  348. /* compute checksum adjustment based on old length versus new */
  349. newlen = htons(sizeof(*uh) + mss);
  350. check = csum16_add(csum16_sub(uh->check, uh->len), newlen);
  351. for (;;) {
  352. if (copy_dtor) {
  353. seg->destructor = sock_wfree;
  354. seg->sk = sk;
  355. sum_truesize += seg->truesize;
  356. }
  357. if (!seg->next)
  358. break;
  359. uh->len = newlen;
  360. uh->check = check;
  361. if (seg->ip_summed == CHECKSUM_PARTIAL)
  362. gso_reset_checksum(seg, ~check);
  363. else
  364. uh->check = gso_make_checksum(seg, ~check) ? :
  365. CSUM_MANGLED_0;
  366. seg = seg->next;
  367. uh = udp_hdr(seg);
  368. }
  369. /* last packet can be partial gso_size, account for that in checksum */
  370. newlen = htons(skb_tail_pointer(seg) - skb_transport_header(seg) +
  371. seg->data_len);
  372. check = csum16_add(csum16_sub(uh->check, uh->len), newlen);
  373. uh->len = newlen;
  374. uh->check = check;
  375. if (seg->ip_summed == CHECKSUM_PARTIAL)
  376. gso_reset_checksum(seg, ~check);
  377. else
  378. uh->check = gso_make_checksum(seg, ~check) ? : CSUM_MANGLED_0;
  379. /* On the TX path, CHECKSUM_NONE and CHECKSUM_UNNECESSARY have the same
  380. * meaning. However, check for bad offloads in the GSO stack expects the
  381. * latter, if the checksum was calculated in software. To vouch for the
  382. * segment skbs we actually need to set it on the gso_skb.
  383. */
  384. if (gso_skb->ip_summed == CHECKSUM_NONE)
  385. gso_skb->ip_summed = CHECKSUM_UNNECESSARY;
  386. /* update refcount for the packet */
  387. if (copy_dtor) {
  388. int delta = sum_truesize - gso_skb->truesize;
  389. /* In some pathological cases, delta can be negative.
  390. * We need to either use refcount_add() or refcount_sub_and_test()
  391. */
  392. if (likely(delta >= 0))
  393. refcount_add(delta, &sk->sk_wmem_alloc);
  394. else
  395. WARN_ON_ONCE(refcount_sub_and_test(-delta, &sk->sk_wmem_alloc));
  396. }
  397. return segs;
  398. }
  399. EXPORT_SYMBOL_GPL(__udp_gso_segment);
  400. static struct sk_buff *udp4_ufo_fragment(struct sk_buff *skb,
  401. netdev_features_t features)
  402. {
  403. struct sk_buff *segs = ERR_PTR(-EINVAL);
  404. unsigned int mss;
  405. __wsum csum;
  406. struct udphdr *uh;
  407. struct iphdr *iph;
  408. if (skb->encapsulation &&
  409. (skb_shinfo(skb)->gso_type &
  410. (SKB_GSO_UDP_TUNNEL|SKB_GSO_UDP_TUNNEL_CSUM))) {
  411. segs = skb_udp_tunnel_segment(skb, features, false);
  412. goto out;
  413. }
  414. if (!(skb_shinfo(skb)->gso_type & (SKB_GSO_UDP | SKB_GSO_UDP_L4)))
  415. goto out;
  416. if (!pskb_may_pull(skb, sizeof(struct udphdr)))
  417. goto out;
  418. if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4)
  419. return __udp_gso_segment(skb, features, false);
  420. mss = skb_shinfo(skb)->gso_size;
  421. if (unlikely(skb->len <= mss))
  422. goto out;
  423. /* Do software UFO. Complete and fill in the UDP checksum as
  424. * HW cannot do checksum of UDP packets sent as multiple
  425. * IP fragments.
  426. */
  427. uh = udp_hdr(skb);
  428. iph = ip_hdr(skb);
  429. uh->check = 0;
  430. csum = skb_checksum(skb, 0, skb->len, 0);
  431. uh->check = udp_v4_check(skb->len, iph->saddr, iph->daddr, csum);
  432. if (uh->check == 0)
  433. uh->check = CSUM_MANGLED_0;
  434. skb->ip_summed = CHECKSUM_UNNECESSARY;
  435. /* If there is no outer header we can fake a checksum offload
  436. * due to the fact that we have already done the checksum in
  437. * software prior to segmenting the frame.
  438. */
  439. if (!skb->encap_hdr_csum)
  440. features |= NETIF_F_HW_CSUM;
  441. /* Fragment the skb. IP headers of the fragments are updated in
  442. * inet_gso_segment()
  443. */
  444. segs = skb_segment(skb, features);
  445. out:
  446. return segs;
  447. }
  448. #define UDP_GRO_CNT_MAX 64
  449. static struct sk_buff *udp_gro_receive_segment(struct list_head *head,
  450. struct sk_buff *skb)
  451. {
  452. struct udphdr *uh = udp_gro_udphdr(skb);
  453. struct sk_buff *pp = NULL;
  454. struct udphdr *uh2;
  455. struct sk_buff *p;
  456. unsigned int ulen;
  457. int ret = 0;
  458. int flush;
  459. /* requires non zero csum, for symmetry with GSO */
  460. if (!uh->check) {
  461. NAPI_GRO_CB(skb)->flush = 1;
  462. return NULL;
  463. }
  464. /* Do not deal with padded or malicious packets, sorry ! */
  465. ulen = ntohs(uh->len);
  466. if (ulen <= sizeof(*uh) || ulen != skb_gro_len(skb)) {
  467. NAPI_GRO_CB(skb)->flush = 1;
  468. return NULL;
  469. }
  470. /* pull encapsulating udp header */
  471. skb_gro_pull(skb, sizeof(struct udphdr));
  472. list_for_each_entry(p, head, list) {
  473. if (!NAPI_GRO_CB(p)->same_flow)
  474. continue;
  475. uh2 = udp_hdr(p);
  476. /* Match ports only, as csum is always non zero */
  477. if ((*(u32 *)&uh->source != *(u32 *)&uh2->source)) {
  478. NAPI_GRO_CB(p)->same_flow = 0;
  479. continue;
  480. }
  481. if (NAPI_GRO_CB(skb)->is_flist != NAPI_GRO_CB(p)->is_flist) {
  482. NAPI_GRO_CB(skb)->flush = 1;
  483. return p;
  484. }
  485. flush = gro_receive_network_flush(uh, uh2, p);
  486. /* Terminate the flow on len mismatch or if it grow "too much".
  487. * Under small packet flood GRO count could elsewhere grow a lot
  488. * leading to excessive truesize values.
  489. * On len mismatch merge the first packet shorter than gso_size,
  490. * otherwise complete the GRO packet.
  491. */
  492. if (ulen > ntohs(uh2->len) || flush) {
  493. pp = p;
  494. } else {
  495. if (NAPI_GRO_CB(skb)->is_flist) {
  496. if (!pskb_may_pull(skb, skb_gro_offset(skb))) {
  497. NAPI_GRO_CB(skb)->flush = 1;
  498. return NULL;
  499. }
  500. if ((skb->ip_summed != p->ip_summed) ||
  501. (skb->csum_level != p->csum_level)) {
  502. NAPI_GRO_CB(skb)->flush = 1;
  503. return NULL;
  504. }
  505. skb_set_network_header(skb, skb_gro_receive_network_offset(skb));
  506. ret = skb_gro_receive_list(p, skb);
  507. } else {
  508. skb_gro_postpull_rcsum(skb, uh,
  509. sizeof(struct udphdr));
  510. ret = skb_gro_receive(p, skb);
  511. }
  512. }
  513. if (ret || ulen != ntohs(uh2->len) ||
  514. NAPI_GRO_CB(p)->count >= UDP_GRO_CNT_MAX)
  515. pp = p;
  516. return pp;
  517. }
  518. /* mismatch, but we never need to flush */
  519. return NULL;
  520. }
  521. struct sk_buff *udp_gro_receive(struct list_head *head, struct sk_buff *skb,
  522. struct udphdr *uh, struct sock *sk)
  523. {
  524. struct sk_buff *pp = NULL;
  525. struct sk_buff *p;
  526. struct udphdr *uh2;
  527. unsigned int off = skb_gro_offset(skb);
  528. int flush = 1;
  529. /* We can do L4 aggregation only if the packet can't land in a tunnel
  530. * otherwise we could corrupt the inner stream. Detecting such packets
  531. * cannot be foolproof and the aggregation might still happen in some
  532. * cases. Such packets should be caught in udp_unexpected_gso later.
  533. */
  534. NAPI_GRO_CB(skb)->is_flist = 0;
  535. if (!sk || !udp_sk(sk)->gro_receive) {
  536. /* If the packet was locally encapsulated in a UDP tunnel that
  537. * wasn't detected above, do not GRO.
  538. */
  539. if (skb->encapsulation)
  540. goto out;
  541. if (skb->dev->features & NETIF_F_GRO_FRAGLIST)
  542. NAPI_GRO_CB(skb)->is_flist = sk ? !udp_test_bit(GRO_ENABLED, sk) : 1;
  543. if ((!sk && (skb->dev->features & NETIF_F_GRO_UDP_FWD)) ||
  544. (sk && udp_test_bit(GRO_ENABLED, sk)) || NAPI_GRO_CB(skb)->is_flist)
  545. return call_gro_receive(udp_gro_receive_segment, head, skb);
  546. /* no GRO, be sure flush the current packet */
  547. goto out;
  548. }
  549. if (NAPI_GRO_CB(skb)->encap_mark ||
  550. (uh->check && skb->ip_summed != CHECKSUM_PARTIAL &&
  551. NAPI_GRO_CB(skb)->csum_cnt == 0 &&
  552. !NAPI_GRO_CB(skb)->csum_valid))
  553. goto out;
  554. /* mark that this skb passed once through the tunnel gro layer */
  555. NAPI_GRO_CB(skb)->encap_mark = 1;
  556. flush = 0;
  557. list_for_each_entry(p, head, list) {
  558. if (!NAPI_GRO_CB(p)->same_flow)
  559. continue;
  560. uh2 = (struct udphdr *)(p->data + off);
  561. /* Match ports and either checksums are either both zero
  562. * or nonzero.
  563. */
  564. if ((*(u32 *)&uh->source != *(u32 *)&uh2->source) ||
  565. (!uh->check ^ !uh2->check)) {
  566. NAPI_GRO_CB(p)->same_flow = 0;
  567. continue;
  568. }
  569. }
  570. skb_gro_pull(skb, sizeof(struct udphdr)); /* pull encapsulating udp header */
  571. skb_gro_postpull_rcsum(skb, uh, sizeof(struct udphdr));
  572. pp = call_gro_receive_sk(udp_sk(sk)->gro_receive, sk, head, skb);
  573. out:
  574. skb_gro_flush_final(skb, pp, flush);
  575. return pp;
  576. }
  577. EXPORT_SYMBOL(udp_gro_receive);
  578. static struct sock *udp4_gro_lookup_skb(struct sk_buff *skb, __be16 sport,
  579. __be16 dport)
  580. {
  581. const struct iphdr *iph = skb_gro_network_header(skb);
  582. struct net *net = dev_net(skb->dev);
  583. int iif, sdif;
  584. inet_get_iif_sdif(skb, &iif, &sdif);
  585. return __udp4_lib_lookup(net, iph->saddr, sport,
  586. iph->daddr, dport, iif,
  587. sdif, net->ipv4.udp_table, NULL);
  588. }
  589. INDIRECT_CALLABLE_SCOPE
  590. struct sk_buff *udp4_gro_receive(struct list_head *head, struct sk_buff *skb)
  591. {
  592. struct udphdr *uh = udp_gro_udphdr(skb);
  593. struct sock *sk = NULL;
  594. struct sk_buff *pp;
  595. if (unlikely(!uh))
  596. goto flush;
  597. /* Don't bother verifying checksum if we're going to flush anyway. */
  598. if (NAPI_GRO_CB(skb)->flush)
  599. goto skip;
  600. if (skb_gro_checksum_validate_zero_check(skb, IPPROTO_UDP, uh->check,
  601. inet_gro_compute_pseudo))
  602. goto flush;
  603. else if (uh->check)
  604. skb_gro_checksum_try_convert(skb, IPPROTO_UDP,
  605. inet_gro_compute_pseudo);
  606. skip:
  607. NAPI_GRO_CB(skb)->is_ipv6 = 0;
  608. if (static_branch_unlikely(&udp_encap_needed_key))
  609. sk = udp4_gro_lookup_skb(skb, uh->source, uh->dest);
  610. pp = udp_gro_receive(head, skb, uh, sk);
  611. return pp;
  612. flush:
  613. NAPI_GRO_CB(skb)->flush = 1;
  614. return NULL;
  615. }
  616. static int udp_gro_complete_segment(struct sk_buff *skb)
  617. {
  618. struct udphdr *uh = udp_hdr(skb);
  619. skb->csum_start = (unsigned char *)uh - skb->head;
  620. skb->csum_offset = offsetof(struct udphdr, check);
  621. skb->ip_summed = CHECKSUM_PARTIAL;
  622. skb_shinfo(skb)->gso_segs = NAPI_GRO_CB(skb)->count;
  623. skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_L4;
  624. if (skb->encapsulation)
  625. skb->inner_transport_header = skb->transport_header;
  626. return 0;
  627. }
  628. int udp_gro_complete(struct sk_buff *skb, int nhoff,
  629. udp_lookup_t lookup)
  630. {
  631. __be16 newlen = htons(skb->len - nhoff);
  632. struct udphdr *uh = (struct udphdr *)(skb->data + nhoff);
  633. struct sock *sk;
  634. int err;
  635. uh->len = newlen;
  636. sk = INDIRECT_CALL_INET(lookup, udp6_lib_lookup_skb,
  637. udp4_lib_lookup_skb, skb, uh->source, uh->dest);
  638. if (sk && udp_sk(sk)->gro_complete) {
  639. skb_shinfo(skb)->gso_type = uh->check ? SKB_GSO_UDP_TUNNEL_CSUM
  640. : SKB_GSO_UDP_TUNNEL;
  641. /* clear the encap mark, so that inner frag_list gro_complete
  642. * can take place
  643. */
  644. NAPI_GRO_CB(skb)->encap_mark = 0;
  645. /* Set encapsulation before calling into inner gro_complete()
  646. * functions to make them set up the inner offsets.
  647. */
  648. skb->encapsulation = 1;
  649. err = udp_sk(sk)->gro_complete(sk, skb,
  650. nhoff + sizeof(struct udphdr));
  651. } else {
  652. err = udp_gro_complete_segment(skb);
  653. }
  654. if (skb->remcsum_offload)
  655. skb_shinfo(skb)->gso_type |= SKB_GSO_TUNNEL_REMCSUM;
  656. return err;
  657. }
  658. EXPORT_SYMBOL(udp_gro_complete);
  659. INDIRECT_CALLABLE_SCOPE int udp4_gro_complete(struct sk_buff *skb, int nhoff)
  660. {
  661. const u16 offset = NAPI_GRO_CB(skb)->network_offsets[skb->encapsulation];
  662. const struct iphdr *iph = (struct iphdr *)(skb->data + offset);
  663. struct udphdr *uh = (struct udphdr *)(skb->data + nhoff);
  664. /* do fraglist only if there is no outer UDP encap (or we already processed it) */
  665. if (NAPI_GRO_CB(skb)->is_flist && !NAPI_GRO_CB(skb)->encap_mark) {
  666. uh->len = htons(skb->len - nhoff);
  667. skb_shinfo(skb)->gso_type |= (SKB_GSO_FRAGLIST|SKB_GSO_UDP_L4);
  668. skb_shinfo(skb)->gso_segs = NAPI_GRO_CB(skb)->count;
  669. __skb_incr_checksum_unnecessary(skb);
  670. return 0;
  671. }
  672. if (uh->check)
  673. uh->check = ~udp_v4_check(skb->len - nhoff, iph->saddr,
  674. iph->daddr, 0);
  675. return udp_gro_complete(skb, nhoff, udp4_lib_lookup_skb);
  676. }
  677. int __init udpv4_offload_init(void)
  678. {
  679. net_hotdata.udpv4_offload = (struct net_offload) {
  680. .callbacks = {
  681. .gso_segment = udp4_ufo_fragment,
  682. .gro_receive = udp4_gro_receive,
  683. .gro_complete = udp4_gro_complete,
  684. },
  685. };
  686. return inet_add_offload(&net_hotdata.udpv4_offload, IPPROTO_UDP);
  687. }