udp_offload.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  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. #include <net/udp_tunnel.h>
  15. #if IS_ENABLED(CONFIG_NET_UDP_TUNNEL)
  16. /*
  17. * Dummy GRO tunnel callback, exists mainly to avoid dangling/NULL
  18. * values for the udp tunnel static call.
  19. */
  20. static struct sk_buff *dummy_gro_rcv(struct sock *sk,
  21. struct list_head *head,
  22. struct sk_buff *skb)
  23. {
  24. NAPI_GRO_CB(skb)->flush = 1;
  25. return NULL;
  26. }
  27. typedef struct sk_buff *(*udp_tunnel_gro_rcv_t)(struct sock *sk,
  28. struct list_head *head,
  29. struct sk_buff *skb);
  30. struct udp_tunnel_type_entry {
  31. udp_tunnel_gro_rcv_t gro_receive;
  32. refcount_t count;
  33. };
  34. #define UDP_MAX_TUNNEL_TYPES (IS_ENABLED(CONFIG_GENEVE) + \
  35. IS_ENABLED(CONFIG_VXLAN) * 2 + \
  36. IS_ENABLED(CONFIG_NET_FOU) * 2 + \
  37. IS_ENABLED(CONFIG_XFRM) * 2)
  38. DEFINE_STATIC_CALL(udp_tunnel_gro_rcv, dummy_gro_rcv);
  39. static DEFINE_STATIC_KEY_FALSE(udp_tunnel_static_call);
  40. static DEFINE_MUTEX(udp_tunnel_gro_type_lock);
  41. static struct udp_tunnel_type_entry udp_tunnel_gro_types[UDP_MAX_TUNNEL_TYPES];
  42. static unsigned int udp_tunnel_gro_type_nr;
  43. static DEFINE_SPINLOCK(udp_tunnel_gro_lock);
  44. void udp_tunnel_update_gro_lookup(struct net *net, struct sock *sk, bool add)
  45. {
  46. bool is_ipv6 = sk->sk_family == AF_INET6;
  47. struct udp_sock *tup, *up = udp_sk(sk);
  48. struct udp_tunnel_gro *udp_tunnel_gro;
  49. spin_lock(&udp_tunnel_gro_lock);
  50. udp_tunnel_gro = &net->ipv4.udp_tunnel_gro[is_ipv6];
  51. if (add)
  52. hlist_add_head(&up->tunnel_list, &udp_tunnel_gro->list);
  53. else if (up->tunnel_list.pprev)
  54. hlist_del_init(&up->tunnel_list);
  55. if (udp_tunnel_gro->list.first &&
  56. !udp_tunnel_gro->list.first->next) {
  57. tup = hlist_entry(udp_tunnel_gro->list.first, struct udp_sock,
  58. tunnel_list);
  59. rcu_assign_pointer(udp_tunnel_gro->sk, (struct sock *)tup);
  60. } else {
  61. RCU_INIT_POINTER(udp_tunnel_gro->sk, NULL);
  62. }
  63. spin_unlock(&udp_tunnel_gro_lock);
  64. }
  65. EXPORT_SYMBOL_GPL(udp_tunnel_update_gro_lookup);
  66. void udp_tunnel_update_gro_rcv(struct sock *sk, bool add)
  67. {
  68. struct udp_tunnel_type_entry *cur = NULL;
  69. struct udp_sock *up = udp_sk(sk);
  70. int i, old_gro_type_nr;
  71. if (!UDP_MAX_TUNNEL_TYPES || !up->gro_receive)
  72. return;
  73. mutex_lock(&udp_tunnel_gro_type_lock);
  74. /* Check if the static call is permanently disabled. */
  75. if (udp_tunnel_gro_type_nr > UDP_MAX_TUNNEL_TYPES)
  76. goto out;
  77. for (i = 0; i < udp_tunnel_gro_type_nr; i++)
  78. if (udp_tunnel_gro_types[i].gro_receive == up->gro_receive)
  79. cur = &udp_tunnel_gro_types[i];
  80. old_gro_type_nr = udp_tunnel_gro_type_nr;
  81. if (add) {
  82. /*
  83. * Update the matching entry, if found, or add a new one
  84. * if needed
  85. */
  86. if (cur) {
  87. refcount_inc(&cur->count);
  88. goto out;
  89. }
  90. if (unlikely(udp_tunnel_gro_type_nr == UDP_MAX_TUNNEL_TYPES)) {
  91. pr_err_once("Too many UDP tunnel types, please increase UDP_MAX_TUNNEL_TYPES\n");
  92. /* Ensure static call will never be enabled */
  93. udp_tunnel_gro_type_nr = UDP_MAX_TUNNEL_TYPES + 1;
  94. } else {
  95. cur = &udp_tunnel_gro_types[udp_tunnel_gro_type_nr++];
  96. refcount_set(&cur->count, 1);
  97. cur->gro_receive = up->gro_receive;
  98. }
  99. } else {
  100. /*
  101. * The stack cleanups only successfully added tunnel, the
  102. * lookup on removal should never fail.
  103. */
  104. if (WARN_ON_ONCE(!cur))
  105. goto out;
  106. if (!refcount_dec_and_test(&cur->count))
  107. goto out;
  108. /* Avoid gaps, so that the enable tunnel has always id 0 */
  109. *cur = udp_tunnel_gro_types[--udp_tunnel_gro_type_nr];
  110. }
  111. if (udp_tunnel_gro_type_nr == 1) {
  112. static_call_update(udp_tunnel_gro_rcv,
  113. udp_tunnel_gro_types[0].gro_receive);
  114. static_branch_enable(&udp_tunnel_static_call);
  115. } else if (old_gro_type_nr == 1) {
  116. static_branch_disable(&udp_tunnel_static_call);
  117. static_call_update(udp_tunnel_gro_rcv, dummy_gro_rcv);
  118. }
  119. out:
  120. mutex_unlock(&udp_tunnel_gro_type_lock);
  121. }
  122. EXPORT_SYMBOL_GPL(udp_tunnel_update_gro_rcv);
  123. static struct sk_buff *udp_tunnel_gro_rcv(struct sock *sk,
  124. struct list_head *head,
  125. struct sk_buff *skb)
  126. {
  127. if (static_branch_likely(&udp_tunnel_static_call)) {
  128. if (unlikely(gro_recursion_inc_test(skb))) {
  129. NAPI_GRO_CB(skb)->flush |= 1;
  130. return NULL;
  131. }
  132. return static_call(udp_tunnel_gro_rcv)(sk, head, skb);
  133. }
  134. return call_gro_receive_sk(udp_sk(sk)->gro_receive, sk, head, skb);
  135. }
  136. #else
  137. static struct sk_buff *udp_tunnel_gro_rcv(struct sock *sk,
  138. struct list_head *head,
  139. struct sk_buff *skb)
  140. {
  141. return call_gro_receive_sk(udp_sk(sk)->gro_receive, sk, head, skb);
  142. }
  143. #endif
  144. static struct sk_buff *__skb_udp_tunnel_segment(struct sk_buff *skb,
  145. netdev_features_t features,
  146. struct sk_buff *(*gso_inner_segment)(struct sk_buff *skb,
  147. netdev_features_t features),
  148. __be16 new_protocol, bool is_ipv6)
  149. {
  150. int tnl_hlen = skb_inner_mac_header(skb) - skb_transport_header(skb);
  151. bool remcsum, need_csum, offload_csum, gso_partial;
  152. struct sk_buff *segs = ERR_PTR(-EINVAL);
  153. struct udphdr *uh = udp_hdr(skb);
  154. u16 mac_offset = skb->mac_header;
  155. __be16 protocol = skb->protocol;
  156. u16 mac_len = skb->mac_len;
  157. int udp_offset, outer_hlen;
  158. __wsum partial;
  159. bool need_ipsec;
  160. if (unlikely(!pskb_may_pull(skb, tnl_hlen)))
  161. goto out;
  162. /* Adjust partial header checksum to negate old length.
  163. * We cannot rely on the value contained in uh->len as it is
  164. * possible that the actual value exceeds the boundaries of the
  165. * 16 bit length field due to the header being added outside of an
  166. * IP or IPv6 frame that was already limited to 64K - 1.
  167. */
  168. if (skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL)
  169. partial = (__force __wsum)uh->len;
  170. else
  171. partial = (__force __wsum)htonl(skb->len);
  172. partial = csum_sub(csum_unfold(uh->check), partial);
  173. /* setup inner skb. */
  174. skb->encapsulation = 0;
  175. SKB_GSO_CB(skb)->encap_level = 0;
  176. __skb_pull(skb, tnl_hlen);
  177. skb_reset_mac_header(skb);
  178. skb_set_network_header(skb, skb_inner_network_offset(skb));
  179. skb_set_transport_header(skb, skb_inner_transport_offset(skb));
  180. skb->mac_len = skb_inner_network_offset(skb);
  181. skb->protocol = new_protocol;
  182. need_csum = !!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_TUNNEL_CSUM);
  183. skb->encap_hdr_csum = need_csum;
  184. remcsum = !!(skb_shinfo(skb)->gso_type & SKB_GSO_TUNNEL_REMCSUM);
  185. skb->remcsum_offload = remcsum;
  186. need_ipsec = (skb_dst(skb) && dst_xfrm(skb_dst(skb))) || skb_sec_path(skb);
  187. /* Try to offload checksum if possible */
  188. offload_csum = !!(need_csum &&
  189. !need_ipsec &&
  190. (skb->dev->features &
  191. (is_ipv6 ? (NETIF_F_HW_CSUM | NETIF_F_IPV6_CSUM) :
  192. (NETIF_F_HW_CSUM | NETIF_F_IP_CSUM))));
  193. features &= skb->dev->hw_enc_features;
  194. if (need_csum)
  195. features &= ~NETIF_F_SCTP_CRC;
  196. /* The only checksum offload we care about from here on out is the
  197. * outer one so strip the existing checksum feature flags and
  198. * instead set the flag based on our outer checksum offload value.
  199. */
  200. if (remcsum) {
  201. features &= ~NETIF_F_CSUM_MASK;
  202. if (!need_csum || offload_csum)
  203. features |= NETIF_F_HW_CSUM;
  204. }
  205. /* segment inner packet. */
  206. segs = gso_inner_segment(skb, features);
  207. if (IS_ERR_OR_NULL(segs)) {
  208. skb_gso_error_unwind(skb, protocol, tnl_hlen, mac_offset,
  209. mac_len);
  210. goto out;
  211. }
  212. gso_partial = !!(skb_shinfo(segs)->gso_type & SKB_GSO_PARTIAL);
  213. outer_hlen = skb_tnl_header_len(skb);
  214. udp_offset = outer_hlen - tnl_hlen;
  215. skb = segs;
  216. do {
  217. unsigned int len;
  218. if (remcsum)
  219. skb->ip_summed = CHECKSUM_NONE;
  220. /* Set up inner headers if we are offloading inner checksum */
  221. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  222. skb_reset_inner_headers(skb);
  223. skb->encapsulation = 1;
  224. }
  225. skb->mac_len = mac_len;
  226. skb->protocol = protocol;
  227. __skb_push(skb, outer_hlen);
  228. skb_reset_mac_header(skb);
  229. skb_set_network_header(skb, mac_len);
  230. skb_set_transport_header(skb, udp_offset);
  231. len = skb->len - udp_offset;
  232. uh = udp_hdr(skb);
  233. /* If we are only performing partial GSO the inner header
  234. * will be using a length value equal to only one MSS sized
  235. * segment instead of the entire frame.
  236. */
  237. if (gso_partial && skb_is_gso(skb)) {
  238. uh->len = htons(skb_shinfo(skb)->gso_size +
  239. SKB_GSO_CB(skb)->data_offset +
  240. skb->head - (unsigned char *)uh);
  241. } else {
  242. uh->len = htons(len);
  243. }
  244. if (!need_csum)
  245. continue;
  246. uh->check = ~csum_fold(csum_add(partial,
  247. (__force __wsum)htonl(len)));
  248. if (skb->encapsulation || !offload_csum) {
  249. uh->check = gso_make_checksum(skb, ~uh->check);
  250. if (uh->check == 0)
  251. uh->check = CSUM_MANGLED_0;
  252. } else {
  253. skb->ip_summed = CHECKSUM_PARTIAL;
  254. skb->csum_start = skb_transport_header(skb) - skb->head;
  255. skb->csum_offset = offsetof(struct udphdr, check);
  256. }
  257. } while ((skb = skb->next));
  258. out:
  259. return segs;
  260. }
  261. struct sk_buff *skb_udp_tunnel_segment(struct sk_buff *skb,
  262. netdev_features_t features,
  263. bool is_ipv6)
  264. {
  265. const struct net_offload __rcu **offloads;
  266. __be16 protocol = skb->protocol;
  267. const struct net_offload *ops;
  268. struct sk_buff *segs = ERR_PTR(-EINVAL);
  269. struct sk_buff *(*gso_inner_segment)(struct sk_buff *skb,
  270. netdev_features_t features);
  271. rcu_read_lock();
  272. switch (skb->inner_protocol_type) {
  273. case ENCAP_TYPE_ETHER:
  274. protocol = skb->inner_protocol;
  275. gso_inner_segment = skb_mac_gso_segment;
  276. break;
  277. case ENCAP_TYPE_IPPROTO:
  278. offloads = is_ipv6 ? inet6_offloads : inet_offloads;
  279. ops = rcu_dereference(offloads[skb->inner_ipproto]);
  280. if (!ops || !ops->callbacks.gso_segment)
  281. goto out_unlock;
  282. gso_inner_segment = ops->callbacks.gso_segment;
  283. break;
  284. default:
  285. goto out_unlock;
  286. }
  287. segs = __skb_udp_tunnel_segment(skb, features, gso_inner_segment,
  288. protocol, is_ipv6);
  289. out_unlock:
  290. rcu_read_unlock();
  291. return segs;
  292. }
  293. EXPORT_SYMBOL(skb_udp_tunnel_segment);
  294. static void __udpv4_gso_segment_csum(struct sk_buff *seg,
  295. __be32 *oldip, __be32 *newip,
  296. __be16 *oldport, __be16 *newport)
  297. {
  298. struct udphdr *uh;
  299. struct iphdr *iph;
  300. if (*oldip == *newip && *oldport == *newport)
  301. return;
  302. uh = udp_hdr(seg);
  303. iph = ip_hdr(seg);
  304. if (uh->check) {
  305. inet_proto_csum_replace4(&uh->check, seg, *oldip, *newip,
  306. true);
  307. inet_proto_csum_replace2(&uh->check, seg, *oldport, *newport,
  308. false);
  309. if (!uh->check)
  310. uh->check = CSUM_MANGLED_0;
  311. }
  312. *oldport = *newport;
  313. csum_replace4(&iph->check, *oldip, *newip);
  314. *oldip = *newip;
  315. }
  316. static struct sk_buff *__udpv4_gso_segment_list_csum(struct sk_buff *segs)
  317. {
  318. struct sk_buff *seg;
  319. struct udphdr *uh, *uh2;
  320. struct iphdr *iph, *iph2;
  321. seg = segs;
  322. uh = udp_hdr(seg);
  323. iph = ip_hdr(seg);
  324. if ((udp_hdr(seg)->dest == udp_hdr(seg->next)->dest) &&
  325. (udp_hdr(seg)->source == udp_hdr(seg->next)->source) &&
  326. (ip_hdr(seg)->daddr == ip_hdr(seg->next)->daddr) &&
  327. (ip_hdr(seg)->saddr == ip_hdr(seg->next)->saddr))
  328. return segs;
  329. while ((seg = seg->next)) {
  330. uh2 = udp_hdr(seg);
  331. iph2 = ip_hdr(seg);
  332. __udpv4_gso_segment_csum(seg,
  333. &iph2->saddr, &iph->saddr,
  334. &uh2->source, &uh->source);
  335. __udpv4_gso_segment_csum(seg,
  336. &iph2->daddr, &iph->daddr,
  337. &uh2->dest, &uh->dest);
  338. }
  339. return segs;
  340. }
  341. static void __udpv6_gso_segment_csum(struct sk_buff *seg,
  342. struct in6_addr *oldip,
  343. const struct in6_addr *newip,
  344. __be16 *oldport, __be16 newport)
  345. {
  346. struct udphdr *uh = udp_hdr(seg);
  347. if (ipv6_addr_equal(oldip, newip) && *oldport == newport)
  348. return;
  349. if (uh->check) {
  350. inet_proto_csum_replace16(&uh->check, seg, oldip->s6_addr32,
  351. newip->s6_addr32, true);
  352. inet_proto_csum_replace2(&uh->check, seg, *oldport, newport,
  353. false);
  354. if (!uh->check)
  355. uh->check = CSUM_MANGLED_0;
  356. }
  357. *oldip = *newip;
  358. *oldport = newport;
  359. }
  360. static struct sk_buff *__udpv6_gso_segment_list_csum(struct sk_buff *segs)
  361. {
  362. const struct ipv6hdr *iph;
  363. const struct udphdr *uh;
  364. struct ipv6hdr *iph2;
  365. struct sk_buff *seg;
  366. struct udphdr *uh2;
  367. seg = segs;
  368. uh = udp_hdr(seg);
  369. iph = ipv6_hdr(seg);
  370. uh2 = udp_hdr(seg->next);
  371. iph2 = ipv6_hdr(seg->next);
  372. if (!(*(const u32 *)&uh->source ^ *(const u32 *)&uh2->source) &&
  373. ipv6_addr_equal(&iph->saddr, &iph2->saddr) &&
  374. ipv6_addr_equal(&iph->daddr, &iph2->daddr))
  375. return segs;
  376. while ((seg = seg->next)) {
  377. uh2 = udp_hdr(seg);
  378. iph2 = ipv6_hdr(seg);
  379. __udpv6_gso_segment_csum(seg, &iph2->saddr, &iph->saddr,
  380. &uh2->source, uh->source);
  381. __udpv6_gso_segment_csum(seg, &iph2->daddr, &iph->daddr,
  382. &uh2->dest, uh->dest);
  383. }
  384. return segs;
  385. }
  386. static struct sk_buff *__udp_gso_segment_list(struct sk_buff *skb,
  387. netdev_features_t features,
  388. bool is_ipv6)
  389. {
  390. unsigned int mss = skb_shinfo(skb)->gso_size;
  391. skb = skb_segment_list(skb, features, skb_mac_header_len(skb));
  392. if (IS_ERR(skb))
  393. return skb;
  394. udp_hdr(skb)->len = htons(sizeof(struct udphdr) + mss);
  395. if (is_ipv6)
  396. return __udpv6_gso_segment_list_csum(skb);
  397. else
  398. return __udpv4_gso_segment_list_csum(skb);
  399. }
  400. struct sk_buff *__udp_gso_segment(struct sk_buff *gso_skb,
  401. netdev_features_t features, bool is_ipv6)
  402. {
  403. struct sock *sk = gso_skb->sk;
  404. unsigned int sum_truesize = 0;
  405. struct sk_buff *segs, *seg;
  406. __be16 newlen, msslen;
  407. struct udphdr *uh;
  408. unsigned int mss;
  409. bool copy_dtor;
  410. __sum16 check;
  411. int ret = 0;
  412. mss = skb_shinfo(gso_skb)->gso_size;
  413. if (gso_skb->len <= sizeof(*uh) + mss)
  414. return ERR_PTR(-EINVAL);
  415. if (unlikely(skb_checksum_start(gso_skb) !=
  416. skb_transport_header(gso_skb) &&
  417. !(skb_shinfo(gso_skb)->gso_type & SKB_GSO_FRAGLIST)))
  418. return ERR_PTR(-EINVAL);
  419. /* We don't know if egress device can segment and checksum the packet
  420. * when IPv6 extension headers are present. Fall back to software GSO.
  421. */
  422. if (gso_skb->ip_summed != CHECKSUM_PARTIAL)
  423. features &= ~(NETIF_F_GSO_UDP_L4 | NETIF_F_CSUM_MASK);
  424. if (skb_gso_ok(gso_skb, features | NETIF_F_GSO_ROBUST)) {
  425. /* Packet is from an untrusted source, reset gso_segs. */
  426. skb_shinfo(gso_skb)->gso_segs = DIV_ROUND_UP(gso_skb->len - sizeof(*uh),
  427. mss);
  428. return NULL;
  429. }
  430. if (skb_shinfo(gso_skb)->gso_type & SKB_GSO_FRAGLIST) {
  431. /* Detect modified geometry and pass those to skb_segment. */
  432. if ((skb_pagelen(gso_skb) - sizeof(*uh) == skb_shinfo(gso_skb)->gso_size) &&
  433. !(skb_shinfo(gso_skb)->gso_type & SKB_GSO_DODGY))
  434. return __udp_gso_segment_list(gso_skb, features, is_ipv6);
  435. ret = __skb_linearize(gso_skb);
  436. if (ret)
  437. return ERR_PTR(ret);
  438. /* Setup csum, as fraglist skips this in udp4_gro_receive. */
  439. gso_skb->csum_start = skb_transport_header(gso_skb) - gso_skb->head;
  440. gso_skb->csum_offset = offsetof(struct udphdr, check);
  441. gso_skb->ip_summed = CHECKSUM_PARTIAL;
  442. uh = udp_hdr(gso_skb);
  443. if (is_ipv6)
  444. uh->check = ~udp_v6_check(gso_skb->len,
  445. &ipv6_hdr(gso_skb)->saddr,
  446. &ipv6_hdr(gso_skb)->daddr, 0);
  447. else
  448. uh->check = ~udp_v4_check(gso_skb->len,
  449. ip_hdr(gso_skb)->saddr,
  450. ip_hdr(gso_skb)->daddr, 0);
  451. }
  452. skb_pull(gso_skb, sizeof(*uh));
  453. /* clear destructor to avoid skb_segment assigning it to tail */
  454. copy_dtor = gso_skb->destructor == sock_wfree;
  455. if (copy_dtor) {
  456. gso_skb->destructor = NULL;
  457. gso_skb->sk = NULL;
  458. }
  459. segs = skb_segment(gso_skb, features);
  460. if (IS_ERR_OR_NULL(segs)) {
  461. if (copy_dtor) {
  462. gso_skb->destructor = sock_wfree;
  463. gso_skb->sk = sk;
  464. }
  465. return segs;
  466. }
  467. msslen = htons(sizeof(*uh) + mss);
  468. /* GSO partial and frag_list segmentation only requires splitting
  469. * the frame into an MSS multiple and possibly a remainder, both
  470. * cases return a GSO skb. So update the mss now.
  471. */
  472. if (skb_is_gso(segs))
  473. mss *= skb_shinfo(segs)->gso_segs;
  474. seg = segs;
  475. uh = udp_hdr(seg);
  476. /* preserve TX timestamp flags and TS key for first segment */
  477. skb_shinfo(seg)->tskey = skb_shinfo(gso_skb)->tskey;
  478. skb_shinfo(seg)->tx_flags |=
  479. (skb_shinfo(gso_skb)->tx_flags & SKBTX_ANY_TSTAMP);
  480. /* compute checksum adjustment based on old length versus new */
  481. newlen = htons(sizeof(*uh) + mss);
  482. check = csum16_add(csum16_sub(uh->check, uh->len), newlen);
  483. for (;;) {
  484. if (copy_dtor) {
  485. seg->destructor = sock_wfree;
  486. seg->sk = sk;
  487. sum_truesize += seg->truesize;
  488. }
  489. if (!seg->next)
  490. break;
  491. uh->len = msslen;
  492. uh->check = check;
  493. if (seg->ip_summed == CHECKSUM_PARTIAL)
  494. gso_reset_checksum(seg, ~check);
  495. else
  496. uh->check = gso_make_checksum(seg, ~check) ? :
  497. CSUM_MANGLED_0;
  498. seg = seg->next;
  499. uh = udp_hdr(seg);
  500. }
  501. /* last packet can be partial gso_size, account for that in checksum */
  502. newlen = htons(skb_tail_pointer(seg) - skb_transport_header(seg) +
  503. seg->data_len);
  504. check = csum16_add(csum16_sub(uh->check, uh->len), newlen);
  505. uh->len = newlen;
  506. uh->check = check;
  507. if (seg->ip_summed == CHECKSUM_PARTIAL)
  508. gso_reset_checksum(seg, ~check);
  509. else
  510. uh->check = gso_make_checksum(seg, ~check) ? : CSUM_MANGLED_0;
  511. /* On the TX path, CHECKSUM_NONE and CHECKSUM_UNNECESSARY have the same
  512. * meaning. However, check for bad offloads in the GSO stack expects the
  513. * latter, if the checksum was calculated in software. To vouch for the
  514. * segment skbs we actually need to set it on the gso_skb.
  515. */
  516. if (gso_skb->ip_summed == CHECKSUM_NONE)
  517. gso_skb->ip_summed = CHECKSUM_UNNECESSARY;
  518. /* update refcount for the packet */
  519. if (copy_dtor) {
  520. int delta = sum_truesize - gso_skb->truesize;
  521. /* In some pathological cases, delta can be negative.
  522. * We need to either use refcount_add() or refcount_sub_and_test()
  523. */
  524. if (likely(delta >= 0))
  525. refcount_add(delta, &sk->sk_wmem_alloc);
  526. else
  527. WARN_ON_ONCE(refcount_sub_and_test(-delta, &sk->sk_wmem_alloc));
  528. }
  529. return segs;
  530. }
  531. EXPORT_SYMBOL_GPL(__udp_gso_segment);
  532. static struct sk_buff *udp4_ufo_fragment(struct sk_buff *skb,
  533. netdev_features_t features)
  534. {
  535. struct sk_buff *segs = ERR_PTR(-EINVAL);
  536. unsigned int mss;
  537. __wsum csum;
  538. struct udphdr *uh;
  539. struct iphdr *iph;
  540. if (skb->encapsulation &&
  541. (skb_shinfo(skb)->gso_type &
  542. (SKB_GSO_UDP_TUNNEL|SKB_GSO_UDP_TUNNEL_CSUM))) {
  543. segs = skb_udp_tunnel_segment(skb, features, false);
  544. goto out;
  545. }
  546. if (!(skb_shinfo(skb)->gso_type & (SKB_GSO_UDP | SKB_GSO_UDP_L4)))
  547. goto out;
  548. if (!pskb_may_pull(skb, sizeof(struct udphdr)))
  549. goto out;
  550. if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4)
  551. return __udp_gso_segment(skb, features, false);
  552. mss = skb_shinfo(skb)->gso_size;
  553. if (unlikely(skb->len <= mss))
  554. goto out;
  555. /* Do software UFO. Complete and fill in the UDP checksum as
  556. * HW cannot do checksum of UDP packets sent as multiple
  557. * IP fragments.
  558. */
  559. uh = udp_hdr(skb);
  560. iph = ip_hdr(skb);
  561. uh->check = 0;
  562. csum = skb_checksum(skb, 0, skb->len, 0);
  563. uh->check = udp_v4_check(skb->len, iph->saddr, iph->daddr, csum);
  564. if (uh->check == 0)
  565. uh->check = CSUM_MANGLED_0;
  566. skb->ip_summed = CHECKSUM_UNNECESSARY;
  567. /* If there is no outer header we can fake a checksum offload
  568. * due to the fact that we have already done the checksum in
  569. * software prior to segmenting the frame.
  570. */
  571. if (!skb->encap_hdr_csum)
  572. features |= NETIF_F_HW_CSUM;
  573. /* Fragment the skb. IP headers of the fragments are updated in
  574. * inet_gso_segment()
  575. */
  576. segs = skb_segment(skb, features);
  577. out:
  578. return segs;
  579. }
  580. #define UDP_GRO_CNT_MAX 64
  581. static struct sk_buff *udp_gro_receive_segment(struct list_head *head,
  582. struct sk_buff *skb)
  583. {
  584. struct udphdr *uh = udp_gro_udphdr(skb);
  585. struct sk_buff *pp = NULL;
  586. struct udphdr *uh2;
  587. struct sk_buff *p;
  588. unsigned int ulen;
  589. int ret = 0;
  590. int flush;
  591. /* requires non zero csum, for symmetry with GSO */
  592. if (!uh->check) {
  593. NAPI_GRO_CB(skb)->flush = 1;
  594. return NULL;
  595. }
  596. /* Do not deal with padded or malicious packets, sorry ! */
  597. ulen = ntohs(uh->len);
  598. if (ulen <= sizeof(*uh) || ulen != skb_gro_len(skb)) {
  599. NAPI_GRO_CB(skb)->flush = 1;
  600. return NULL;
  601. }
  602. /* pull encapsulating udp header */
  603. skb_gro_pull(skb, sizeof(struct udphdr));
  604. list_for_each_entry(p, head, list) {
  605. if (!NAPI_GRO_CB(p)->same_flow)
  606. continue;
  607. uh2 = udp_hdr(p);
  608. /* Match ports only, as csum is always non zero */
  609. if ((*(u32 *)&uh->source != *(u32 *)&uh2->source)) {
  610. NAPI_GRO_CB(p)->same_flow = 0;
  611. continue;
  612. }
  613. if (NAPI_GRO_CB(skb)->is_flist != NAPI_GRO_CB(p)->is_flist) {
  614. NAPI_GRO_CB(skb)->flush = 1;
  615. return p;
  616. }
  617. flush = gro_receive_network_flush(uh, uh2, p);
  618. /* Terminate the flow on len mismatch or if it grow "too much".
  619. * Under small packet flood GRO count could elsewhere grow a lot
  620. * leading to excessive truesize values.
  621. * On len mismatch merge the first packet shorter than gso_size,
  622. * otherwise complete the GRO packet.
  623. */
  624. if (ulen > ntohs(uh2->len) || flush) {
  625. pp = p;
  626. } else {
  627. if (NAPI_GRO_CB(skb)->is_flist) {
  628. if (!pskb_may_pull(skb, skb_gro_offset(skb))) {
  629. NAPI_GRO_CB(skb)->flush = 1;
  630. return NULL;
  631. }
  632. if ((skb->ip_summed != p->ip_summed) ||
  633. (skb->csum_level != p->csum_level)) {
  634. NAPI_GRO_CB(skb)->flush = 1;
  635. return NULL;
  636. }
  637. skb_set_network_header(skb, skb_gro_receive_network_offset(skb));
  638. ret = skb_gro_receive_list(p, skb);
  639. } else {
  640. skb_gro_postpull_rcsum(skb, uh,
  641. sizeof(struct udphdr));
  642. ret = skb_gro_receive(p, skb);
  643. }
  644. }
  645. if (ret || ulen != ntohs(uh2->len) ||
  646. NAPI_GRO_CB(p)->count >= UDP_GRO_CNT_MAX)
  647. pp = p;
  648. return pp;
  649. }
  650. /* mismatch, but we never need to flush */
  651. return NULL;
  652. }
  653. struct sk_buff *udp_gro_receive(struct list_head *head, struct sk_buff *skb,
  654. struct udphdr *uh, struct sock *sk)
  655. {
  656. struct sk_buff *pp = NULL;
  657. struct sk_buff *p;
  658. struct udphdr *uh2;
  659. unsigned int off = skb_gro_offset(skb);
  660. int flush = 1;
  661. /* We can do L4 aggregation only if the packet can't land in a tunnel
  662. * otherwise we could corrupt the inner stream. Detecting such packets
  663. * cannot be foolproof and the aggregation might still happen in some
  664. * cases. Such packets should be caught in udp_unexpected_gso later.
  665. */
  666. NAPI_GRO_CB(skb)->is_flist = 0;
  667. if (!sk || !udp_sk(sk)->gro_receive) {
  668. /* If the packet was locally encapsulated in a UDP tunnel that
  669. * wasn't detected above, do not GRO.
  670. */
  671. if (skb->encapsulation)
  672. goto out;
  673. if (skb->dev->features & NETIF_F_GRO_FRAGLIST)
  674. NAPI_GRO_CB(skb)->is_flist = sk ? !udp_test_bit(GRO_ENABLED, sk) : 1;
  675. if ((!sk && (skb->dev->features & NETIF_F_GRO_UDP_FWD)) ||
  676. (sk && udp_test_bit(GRO_ENABLED, sk)) || NAPI_GRO_CB(skb)->is_flist)
  677. return call_gro_receive(udp_gro_receive_segment, head, skb);
  678. /* no GRO, be sure flush the current packet */
  679. goto out;
  680. }
  681. if (NAPI_GRO_CB(skb)->encap_mark ||
  682. (uh->check && skb->ip_summed != CHECKSUM_PARTIAL &&
  683. NAPI_GRO_CB(skb)->csum_cnt == 0 &&
  684. !NAPI_GRO_CB(skb)->csum_valid))
  685. goto out;
  686. /* mark that this skb passed once through the tunnel gro layer */
  687. NAPI_GRO_CB(skb)->encap_mark = 1;
  688. flush = 0;
  689. list_for_each_entry(p, head, list) {
  690. if (!NAPI_GRO_CB(p)->same_flow)
  691. continue;
  692. uh2 = (struct udphdr *)(p->data + off);
  693. /* Match ports and either checksums are either both zero
  694. * or nonzero.
  695. */
  696. if ((*(u32 *)&uh->source != *(u32 *)&uh2->source) ||
  697. (!uh->check ^ !uh2->check)) {
  698. NAPI_GRO_CB(p)->same_flow = 0;
  699. continue;
  700. }
  701. }
  702. skb_gro_pull(skb, sizeof(struct udphdr)); /* pull encapsulating udp header */
  703. skb_gro_postpull_rcsum(skb, uh, sizeof(struct udphdr));
  704. pp = udp_tunnel_gro_rcv(sk, head, skb);
  705. out:
  706. skb_gro_flush_final(skb, pp, flush);
  707. return pp;
  708. }
  709. EXPORT_SYMBOL(udp_gro_receive);
  710. static struct sock *udp4_gro_lookup_skb(struct sk_buff *skb, __be16 sport,
  711. __be16 dport)
  712. {
  713. const struct iphdr *iph = skb_gro_network_header(skb);
  714. struct net *net = dev_net_rcu(skb->dev);
  715. struct sock *sk;
  716. int iif, sdif;
  717. sk = udp_tunnel_sk(net, false);
  718. if (sk && dport == htons(sk->sk_num))
  719. return sk;
  720. inet_get_iif_sdif(skb, &iif, &sdif);
  721. return __udp4_lib_lookup(net, iph->saddr, sport,
  722. iph->daddr, dport, iif,
  723. sdif, net->ipv4.udp_table, NULL);
  724. }
  725. INDIRECT_CALLABLE_SCOPE
  726. struct sk_buff *udp4_gro_receive(struct list_head *head, struct sk_buff *skb)
  727. {
  728. struct udphdr *uh = udp_gro_udphdr(skb);
  729. struct sock *sk = NULL;
  730. struct sk_buff *pp;
  731. if (unlikely(!uh))
  732. goto flush;
  733. /* Don't bother verifying checksum if we're going to flush anyway. */
  734. if (NAPI_GRO_CB(skb)->flush)
  735. goto skip;
  736. if (skb_gro_checksum_validate_zero_check(skb, IPPROTO_UDP, uh->check,
  737. inet_gro_compute_pseudo))
  738. goto flush;
  739. else if (uh->check)
  740. skb_gro_checksum_try_convert(skb, IPPROTO_UDP,
  741. inet_gro_compute_pseudo);
  742. skip:
  743. if (static_branch_unlikely(&udp_encap_needed_key))
  744. sk = udp4_gro_lookup_skb(skb, uh->source, uh->dest);
  745. pp = udp_gro_receive(head, skb, uh, sk);
  746. return pp;
  747. flush:
  748. NAPI_GRO_CB(skb)->flush = 1;
  749. return NULL;
  750. }
  751. static int udp_gro_complete_segment(struct sk_buff *skb)
  752. {
  753. struct udphdr *uh = udp_hdr(skb);
  754. skb->csum_start = (unsigned char *)uh - skb->head;
  755. skb->csum_offset = offsetof(struct udphdr, check);
  756. skb->ip_summed = CHECKSUM_PARTIAL;
  757. skb_shinfo(skb)->gso_segs = NAPI_GRO_CB(skb)->count;
  758. skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_L4;
  759. if (skb->encapsulation)
  760. skb->inner_transport_header = skb->transport_header;
  761. return 0;
  762. }
  763. int udp_gro_complete(struct sk_buff *skb, int nhoff,
  764. udp_lookup_t lookup)
  765. {
  766. __be16 newlen = htons(skb->len - nhoff);
  767. struct udphdr *uh = (struct udphdr *)(skb->data + nhoff);
  768. struct sock *sk;
  769. int err;
  770. uh->len = newlen;
  771. sk = INDIRECT_CALL_INET(lookup, udp6_lib_lookup_skb,
  772. udp4_lib_lookup_skb, skb, uh->source, uh->dest);
  773. if (sk && udp_sk(sk)->gro_complete) {
  774. skb_shinfo(skb)->gso_type = uh->check ? SKB_GSO_UDP_TUNNEL_CSUM
  775. : SKB_GSO_UDP_TUNNEL;
  776. /* clear the encap mark, so that inner frag_list gro_complete
  777. * can take place
  778. */
  779. NAPI_GRO_CB(skb)->encap_mark = 0;
  780. /* Set encapsulation before calling into inner gro_complete()
  781. * functions to make them set up the inner offsets.
  782. */
  783. skb->encapsulation = 1;
  784. err = udp_sk(sk)->gro_complete(sk, skb,
  785. nhoff + sizeof(struct udphdr));
  786. } else {
  787. err = udp_gro_complete_segment(skb);
  788. }
  789. if (skb->remcsum_offload)
  790. skb_shinfo(skb)->gso_type |= SKB_GSO_TUNNEL_REMCSUM;
  791. return err;
  792. }
  793. EXPORT_SYMBOL(udp_gro_complete);
  794. INDIRECT_CALLABLE_SCOPE int udp4_gro_complete(struct sk_buff *skb, int nhoff)
  795. {
  796. const u16 offset = NAPI_GRO_CB(skb)->network_offsets[skb->encapsulation];
  797. const struct iphdr *iph = (struct iphdr *)(skb->data + offset);
  798. struct udphdr *uh = (struct udphdr *)(skb->data + nhoff);
  799. /* do fraglist only if there is no outer UDP encap (or we already processed it) */
  800. if (NAPI_GRO_CB(skb)->is_flist && !NAPI_GRO_CB(skb)->encap_mark) {
  801. uh->len = htons(skb->len - nhoff);
  802. skb_shinfo(skb)->gso_type |= (SKB_GSO_FRAGLIST|SKB_GSO_UDP_L4);
  803. skb_shinfo(skb)->gso_segs = NAPI_GRO_CB(skb)->count;
  804. __skb_incr_checksum_unnecessary(skb);
  805. return 0;
  806. }
  807. if (uh->check)
  808. uh->check = ~udp_v4_check(skb->len - nhoff, iph->saddr,
  809. iph->daddr, 0);
  810. return udp_gro_complete(skb, nhoff, udp4_lib_lookup_skb);
  811. }
  812. int __init udpv4_offload_init(void)
  813. {
  814. net_hotdata.udpv4_offload = (struct net_offload) {
  815. .callbacks = {
  816. .gso_segment = udp4_ufo_fragment,
  817. .gro_receive = udp4_gro_receive,
  818. .gro_complete = udp4_gro_complete,
  819. },
  820. };
  821. return inet_add_offload(&net_hotdata.udpv4_offload, IPPROTO_UDP);
  822. }