1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 /* 3 * INET An implementation of the TCP/IP protocol suite for the LINUX 4 * operating system. INET is implemented using the BSD Socket 5 * interface as the means of communication with the user level. 6 * 7 * Definitions for the UDP module. 8 * 9 * Version: @(#)udp.h 1.0.2 05/07/93 10 * 11 * Authors: Ross Biro 12 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> 13 * 14 * Fixes: 15 * Alan Cox : Turned on udp checksums. I don't want to 16 * chase 'memory corruption' bugs that aren't! 17 */ 18 #ifndef _UDP_H 19 #define _UDP_H 20 21 #include <linux/list.h> 22 #include <linux/bug.h> 23 #include <net/inet_sock.h> 24 #include <net/gso.h> 25 #include <net/sock.h> 26 #include <net/snmp.h> 27 #include <net/ip.h> 28 #include <linux/ipv6.h> 29 #include <linux/seq_file.h> 30 #include <linux/poll.h> 31 #include <linux/indirect_call_wrapper.h> 32 #include <linux/math.h> 33 34 /** 35 * struct udp_skb_cb - UDP(-Lite) private variables 36 * 37 * @header: private variables used by IPv4/IPv6 38 * @cscov: checksum coverage length (UDP-Lite only) 39 * @partial_cov: if set indicates partial csum coverage 40 */ 41 struct udp_skb_cb { 42 union { 43 struct inet_skb_parm h4; 44 #if IS_ENABLED(CONFIG_IPV6) 45 struct inet6_skb_parm h6; 46 #endif 47 } header; 48 __u16 cscov; 49 __u8 partial_cov; 50 }; 51 #define UDP_SKB_CB(__skb) ((struct udp_skb_cb *)((__skb)->cb)) 52 53 /** 54 * struct udp_hslot - UDP hash slot used by udp_table.hash/hash4 55 * 56 * @head: head of list of sockets 57 * @nulls_head: head of list of sockets, only used by hash4 58 * @count: number of sockets in 'head' list 59 * @lock: spinlock protecting changes to head/count 60 */ 61 struct udp_hslot { 62 union { 63 struct hlist_head head; 64 /* hash4 uses hlist_nulls to avoid moving wrongly onto another 65 * hlist, because rehash() can happen with lookup(). 66 */ 67 struct hlist_nulls_head nulls_head; 68 }; 69 int count; 70 spinlock_t lock; 71 } __aligned(2 * sizeof(long)); 72 73 /** 74 * struct udp_hslot_main - UDP hash slot used by udp_table.hash2 75 * 76 * @hslot: basic hash slot 77 * @hash4_cnt: number of sockets in hslot4 of the same 78 * (local port, local address) 79 */ 80 struct udp_hslot_main { 81 struct udp_hslot hslot; /* must be the first member */ 82 #if !IS_ENABLED(CONFIG_BASE_SMALL) 83 u32 hash4_cnt; 84 #endif 85 } __aligned(2 * sizeof(long)); 86 #define UDP_HSLOT_MAIN(__hslot) ((struct udp_hslot_main *)(__hslot)) 87 88 /** 89 * struct udp_table - UDP table 90 * 91 * @hash: hash table, sockets are hashed on (local port) 92 * @hash2: hash table, sockets are hashed on (local port, local address) 93 * @hash4: hash table, connected sockets are hashed on 94 * (local port, local address, remote port, remote address) 95 * @mask: number of slots in hash tables, minus 1 96 * @log: log2(number of slots in hash table) 97 */ 98 struct udp_table { 99 struct udp_hslot *hash; 100 struct udp_hslot_main *hash2; 101 #if !IS_ENABLED(CONFIG_BASE_SMALL) 102 struct udp_hslot *hash4; 103 #endif 104 unsigned int mask; 105 unsigned int log; 106 }; 107 extern struct udp_table udp_table; 108 void udp_table_init(struct udp_table *, const char *); 109 static inline struct udp_hslot *udp_hashslot(struct udp_table *table, 110 const struct net *net, 111 unsigned int num) 112 { 113 return &table->hash[udp_hashfn(net, num, table->mask)]; 114 } 115 116 /* 117 * For secondary hash, net_hash_mix() is performed before calling 118 * udp_hashslot2(), this explains difference with udp_hashslot() 119 */ 120 static inline struct udp_hslot *udp_hashslot2(struct udp_table *table, 121 unsigned int hash) 122 { 123 return &table->hash2[hash & table->mask].hslot; 124 } 125 126 #if IS_ENABLED(CONFIG_BASE_SMALL) 127 static inline void udp_table_hash4_init(struct udp_table *table) 128 { 129 } 130 131 static inline struct udp_hslot *udp_hashslot4(struct udp_table *table, 132 unsigned int hash) 133 { 134 BUILD_BUG(); 135 return NULL; 136 } 137 138 static inline bool udp_hashed4(const struct sock *sk) 139 { 140 return false; 141 } 142 143 static inline unsigned int udp_hash4_slot_size(void) 144 { 145 return 0; 146 } 147 148 static inline bool udp_has_hash4(const struct udp_hslot *hslot2) 149 { 150 return false; 151 } 152 153 static inline void udp_hash4_inc(struct udp_hslot *hslot2) 154 { 155 } 156 157 static inline void udp_hash4_dec(struct udp_hslot *hslot2) 158 { 159 } 160 #else /* !CONFIG_BASE_SMALL */ 161 162 /* Must be called with table->hash2 initialized */ 163 static inline void udp_table_hash4_init(struct udp_table *table) 164 { 165 table->hash4 = (void *)(table->hash2 + (table->mask + 1)); 166 for (int i = 0; i <= table->mask; i++) { 167 table->hash2[i].hash4_cnt = 0; 168 169 INIT_HLIST_NULLS_HEAD(&table->hash4[i].nulls_head, i); 170 table->hash4[i].count = 0; 171 spin_lock_init(&table->hash4[i].lock); 172 } 173 } 174 175 static inline struct udp_hslot *udp_hashslot4(struct udp_table *table, 176 unsigned int hash) 177 { 178 return &table->hash4[hash & table->mask]; 179 } 180 181 static inline bool udp_hashed4(const struct sock *sk) 182 { 183 return !hlist_nulls_unhashed(&udp_sk(sk)->udp_lrpa_node); 184 } 185 186 static inline unsigned int udp_hash4_slot_size(void) 187 { 188 return sizeof(struct udp_hslot); 189 } 190 191 static inline bool udp_has_hash4(const struct udp_hslot *hslot2) 192 { 193 return UDP_HSLOT_MAIN(hslot2)->hash4_cnt; 194 } 195 196 static inline void udp_hash4_inc(struct udp_hslot *hslot2) 197 { 198 UDP_HSLOT_MAIN(hslot2)->hash4_cnt++; 199 } 200 201 static inline void udp_hash4_dec(struct udp_hslot *hslot2) 202 { 203 UDP_HSLOT_MAIN(hslot2)->hash4_cnt--; 204 } 205 #endif /* CONFIG_BASE_SMALL */ 206 207 extern struct proto udp_prot; 208 209 DECLARE_PER_CPU(int, udp_memory_per_cpu_fw_alloc); 210 211 /* sysctl variables for udp */ 212 extern long sysctl_udp_mem[3]; 213 extern int sysctl_udp_rmem_min; 214 extern int sysctl_udp_wmem_min; 215 216 struct sk_buff; 217 218 /* 219 * Generic checksumming routines for UDP(-Lite) v4 and v6 220 */ 221 static inline __sum16 __udp_lib_checksum_complete(struct sk_buff *skb) 222 { 223 return (UDP_SKB_CB(skb)->cscov == skb->len ? 224 __skb_checksum_complete(skb) : 225 __skb_checksum_complete_head(skb, UDP_SKB_CB(skb)->cscov)); 226 } 227 228 static inline int udp_lib_checksum_complete(struct sk_buff *skb) 229 { 230 return !skb_csum_unnecessary(skb) && 231 __udp_lib_checksum_complete(skb); 232 } 233 234 /** 235 * udp_csum_outgoing - compute UDPv4/v6 checksum over fragments 236 * @sk: socket we are writing to 237 * @skb: sk_buff containing the filled-in UDP header 238 * (checksum field must be zeroed out) 239 */ 240 static inline __wsum udp_csum_outgoing(struct sock *sk, struct sk_buff *skb) 241 { 242 __wsum csum = csum_partial(skb_transport_header(skb), 243 sizeof(struct udphdr), 0); 244 skb_queue_walk(&sk->sk_write_queue, skb) { 245 csum = csum_add(csum, skb->csum); 246 } 247 return csum; 248 } 249 250 static inline __wsum udp_csum(struct sk_buff *skb) 251 { 252 __wsum csum = csum_partial(skb_transport_header(skb), 253 sizeof(struct udphdr), skb->csum); 254 255 for (skb = skb_shinfo(skb)->frag_list; skb; skb = skb->next) { 256 csum = csum_add(csum, skb->csum); 257 } 258 return csum; 259 } 260 261 static inline __sum16 udp_v4_check(int len, __be32 saddr, 262 __be32 daddr, __wsum base) 263 { 264 return csum_tcpudp_magic(saddr, daddr, len, IPPROTO_UDP, base); 265 } 266 267 void udp_set_csum(bool nocheck, struct sk_buff *skb, 268 __be32 saddr, __be32 daddr, int len); 269 270 static inline void udp_csum_pull_header(struct sk_buff *skb) 271 { 272 if (!skb->csum_valid && skb->ip_summed == CHECKSUM_NONE) 273 skb->csum = csum_partial(skb->data, sizeof(struct udphdr), 274 skb->csum); 275 skb_pull_rcsum(skb, sizeof(struct udphdr)); 276 UDP_SKB_CB(skb)->cscov -= sizeof(struct udphdr); 277 } 278 279 typedef struct sock *(*udp_lookup_t)(const struct sk_buff *skb, __be16 sport, 280 __be16 dport); 281 282 void udp_v6_early_demux(struct sk_buff *skb); 283 INDIRECT_CALLABLE_DECLARE(int udpv6_rcv(struct sk_buff *)); 284 285 struct sk_buff *__udp_gso_segment(struct sk_buff *gso_skb, 286 netdev_features_t features, bool is_ipv6); 287 288 static inline int udp_lib_init_sock(struct sock *sk) 289 { 290 struct udp_sock *up = udp_sk(sk); 291 292 sk->sk_drop_counters = &up->drop_counters; 293 skb_queue_head_init(&up->reader_queue); 294 INIT_HLIST_NODE(&up->tunnel_list); 295 up->forward_threshold = sk->sk_rcvbuf >> 2; 296 set_bit(SOCK_CUSTOM_SOCKOPT, &sk->sk_socket->flags); 297 298 up->udp_prod_queue = kzalloc_objs(*up->udp_prod_queue, nr_node_ids); 299 if (!up->udp_prod_queue) 300 return -ENOMEM; 301 for (int i = 0; i < nr_node_ids; i++) 302 init_llist_head(&up->udp_prod_queue[i].ll_root); 303 return 0; 304 } 305 306 static inline void udp_drops_inc(struct sock *sk) 307 { 308 numa_drop_add(&udp_sk(sk)->drop_counters, 1); 309 } 310 311 /* hash routines shared between UDPv4/6 and UDP-Litev4/6 */ 312 static inline int udp_lib_hash(struct sock *sk) 313 { 314 BUG(); 315 return 0; 316 } 317 318 void udp_lib_unhash(struct sock *sk); 319 void udp_lib_rehash(struct sock *sk, u16 new_hash, u16 new_hash4); 320 u32 udp_ehashfn(const struct net *net, const __be32 laddr, const __u16 lport, 321 const __be32 faddr, const __be16 fport); 322 323 static inline void udp_lib_close(struct sock *sk, long timeout) 324 { 325 sk_common_release(sk); 326 } 327 328 /* hash4 routines shared between UDPv4/6 */ 329 #if IS_ENABLED(CONFIG_BASE_SMALL) 330 static inline void udp_lib_hash4(struct sock *sk, u16 hash) 331 { 332 } 333 334 static inline void udp4_hash4(struct sock *sk) 335 { 336 } 337 #else /* !CONFIG_BASE_SMALL */ 338 void udp_lib_hash4(struct sock *sk, u16 hash); 339 void udp4_hash4(struct sock *sk); 340 #endif /* CONFIG_BASE_SMALL */ 341 342 int udp_lib_get_port(struct sock *sk, unsigned short snum, 343 unsigned int hash2_nulladdr); 344 345 u32 udp_flow_hashrnd(void); 346 347 static inline __be16 udp_flow_src_port(struct net *net, struct sk_buff *skb, 348 int min, int max, bool use_eth) 349 { 350 u32 hash; 351 352 if (min >= max) { 353 /* Use default range */ 354 inet_get_local_port_range(net, &min, &max); 355 } 356 357 hash = skb_get_hash(skb); 358 if (unlikely(!hash)) { 359 if (use_eth) { 360 /* Can't find a normal hash, caller has indicated an 361 * Ethernet packet so use that to compute a hash. 362 */ 363 hash = jhash(skb->data, 2 * ETH_ALEN, 364 (__force u32) skb->protocol); 365 } else { 366 /* Can't derive any sort of hash for the packet, set 367 * to some consistent random value. 368 */ 369 hash = udp_flow_hashrnd(); 370 } 371 } 372 373 /* Since this is being sent on the wire obfuscate hash a bit 374 * to minimize possibility that any useful information to an 375 * attacker is leaked. Only upper 16 bits are relevant in the 376 * computation for 16 bit port value. 377 */ 378 hash ^= hash << 16; 379 380 return htons(reciprocal_scale(hash, max - min + 1) + min); 381 } 382 383 static inline int udp_rqueue_get(struct sock *sk) 384 { 385 return sk_rmem_alloc_get(sk) - READ_ONCE(udp_sk(sk)->forward_deficit); 386 } 387 388 static inline bool udp_sk_bound_dev_eq(const struct net *net, int bound_dev_if, 389 int dif, int sdif) 390 { 391 #if IS_ENABLED(CONFIG_NET_L3_MASTER_DEV) 392 return inet_bound_dev_eq(!!READ_ONCE(net->ipv4.sysctl_udp_l3mdev_accept), 393 bound_dev_if, dif, sdif); 394 #else 395 return inet_bound_dev_eq(true, bound_dev_if, dif, sdif); 396 #endif 397 } 398 399 /* net/ipv4/udp.c */ 400 void udp_destruct_common(struct sock *sk); 401 void skb_consume_udp(struct sock *sk, struct sk_buff *skb, int len); 402 int __udp_enqueue_schedule_skb(struct sock *sk, struct sk_buff *skb); 403 void udp_skb_destructor(struct sock *sk, struct sk_buff *skb); 404 struct sk_buff *__skb_recv_udp(struct sock *sk, unsigned int flags, int *off, 405 int *err); 406 static inline struct sk_buff *skb_recv_udp(struct sock *sk, unsigned int flags, 407 int *err) 408 { 409 int off = 0; 410 411 return __skb_recv_udp(sk, flags, &off, err); 412 } 413 414 enum skb_drop_reason udp_v4_early_demux(struct sk_buff *skb); 415 bool udp_sk_rx_dst_set(struct sock *sk, struct dst_entry *dst); 416 int udp_err(struct sk_buff *, u32); 417 int udp_abort(struct sock *sk, int err); 418 int udp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len); 419 void udp_splice_eof(struct socket *sock); 420 int udp_push_pending_frames(struct sock *sk); 421 void udp_flush_pending_frames(struct sock *sk); 422 int udp_cmsg_send(struct sock *sk, struct msghdr *msg, u16 *gso_size); 423 void udp4_hwcsum(struct sk_buff *skb, __be32 src, __be32 dst); 424 int udp_rcv(struct sk_buff *skb); 425 int udp_ioctl(struct sock *sk, int cmd, int *karg); 426 int udp_init_sock(struct sock *sk); 427 int udp_pre_connect(struct sock *sk, struct sockaddr_unsized *uaddr, int addr_len); 428 int __udp_disconnect(struct sock *sk, int flags); 429 int udp_disconnect(struct sock *sk, int flags); 430 __poll_t udp_poll(struct file *file, struct socket *sock, poll_table *wait); 431 struct sk_buff *skb_udp_tunnel_segment(struct sk_buff *skb, 432 netdev_features_t features, 433 bool is_ipv6); 434 int udp_lib_getsockopt(struct sock *sk, int level, int optname, 435 char __user *optval, int __user *optlen); 436 int udp_lib_setsockopt(struct sock *sk, int level, int optname, 437 sockptr_t optval, unsigned int optlen, 438 int (*push_pending_frames)(struct sock *)); 439 struct sock *udp4_lib_lookup(const struct net *net, __be32 saddr, __be16 sport, 440 __be32 daddr, __be16 dport, int dif); 441 struct sock *__udp4_lib_lookup(const struct net *net, __be32 saddr, 442 __be16 sport, 443 __be32 daddr, __be16 dport, int dif, int sdif, 444 struct udp_table *tbl, struct sk_buff *skb); 445 struct sock *udp4_lib_lookup_skb(const struct sk_buff *skb, 446 __be16 sport, __be16 dport); 447 struct sock *udp6_lib_lookup(const struct net *net, 448 const struct in6_addr *saddr, __be16 sport, 449 const struct in6_addr *daddr, __be16 dport, 450 int dif); 451 struct sock *__udp6_lib_lookup(const struct net *net, 452 const struct in6_addr *saddr, __be16 sport, 453 const struct in6_addr *daddr, __be16 dport, 454 int dif, int sdif, struct udp_table *tbl, 455 struct sk_buff *skb); 456 struct sock *udp6_lib_lookup_skb(const struct sk_buff *skb, 457 __be16 sport, __be16 dport); 458 int udp_read_skb(struct sock *sk, skb_read_actor_t recv_actor); 459 460 /* UDP uses skb->dev_scratch to cache as much information as possible and avoid 461 * possibly multiple cache miss on dequeue() 462 */ 463 struct udp_dev_scratch { 464 /* skb->truesize and the stateless bit are embedded in a single field; 465 * do not use a bitfield since the compiler emits better/smaller code 466 * this way 467 */ 468 u32 _tsize_state; 469 470 #if BITS_PER_LONG == 64 471 /* len and the bit needed to compute skb_csum_unnecessary 472 * will be on cold cache lines at recvmsg time. 473 * skb->len can be stored on 16 bits since the udp header has been 474 * already validated and pulled. 475 */ 476 u16 len; 477 bool is_linear; 478 bool csum_unnecessary; 479 #endif 480 }; 481 482 static inline struct udp_dev_scratch *udp_skb_scratch(struct sk_buff *skb) 483 { 484 return (struct udp_dev_scratch *)&skb->dev_scratch; 485 } 486 487 #if BITS_PER_LONG == 64 488 static inline unsigned int udp_skb_len(struct sk_buff *skb) 489 { 490 return udp_skb_scratch(skb)->len; 491 } 492 493 static inline bool udp_skb_csum_unnecessary(struct sk_buff *skb) 494 { 495 return udp_skb_scratch(skb)->csum_unnecessary; 496 } 497 498 static inline bool udp_skb_is_linear(struct sk_buff *skb) 499 { 500 return udp_skb_scratch(skb)->is_linear; 501 } 502 503 #else 504 static inline unsigned int udp_skb_len(struct sk_buff *skb) 505 { 506 return skb->len; 507 } 508 509 static inline bool udp_skb_csum_unnecessary(struct sk_buff *skb) 510 { 511 return skb_csum_unnecessary(skb); 512 } 513 514 static inline bool udp_skb_is_linear(struct sk_buff *skb) 515 { 516 return !skb_is_nonlinear(skb); 517 } 518 #endif 519 520 static inline int copy_linear_skb(struct sk_buff *skb, int len, int off, 521 struct iov_iter *to) 522 { 523 return copy_to_iter_full(skb->data + off, len, to) ? 0 : -EFAULT; 524 } 525 526 /* 527 * SNMP statistics for UDP and UDP-Lite 528 */ 529 #define UDP_INC_STATS(net, field, is_udplite) do { \ 530 if (unlikely(is_udplite)) SNMP_INC_STATS((net)->mib.udplite_statistics, field); \ 531 else SNMP_INC_STATS((net)->mib.udp_statistics, field); } while(0) 532 #define __UDP_INC_STATS(net, field, is_udplite) do { \ 533 if (unlikely(is_udplite)) __SNMP_INC_STATS((net)->mib.udplite_statistics, field); \ 534 else __SNMP_INC_STATS((net)->mib.udp_statistics, field); } while(0) 535 536 #define __UDP6_INC_STATS(net, field, is_udplite) do { \ 537 if (unlikely(is_udplite)) __SNMP_INC_STATS((net)->mib.udplite_stats_in6, field); \ 538 else __SNMP_INC_STATS((net)->mib.udp_stats_in6, field); \ 539 } while(0) 540 #define UDP6_INC_STATS(net, field, __lite) do { \ 541 if (unlikely(__lite)) SNMP_INC_STATS((net)->mib.udplite_stats_in6, field); \ 542 else SNMP_INC_STATS((net)->mib.udp_stats_in6, field); \ 543 } while(0) 544 545 #if IS_ENABLED(CONFIG_IPV6) 546 #define __UDPX_MIB(sk, ipv4) \ 547 ({ \ 548 ipv4 ? (IS_UDPLITE(sk) ? sock_net(sk)->mib.udplite_statistics : \ 549 sock_net(sk)->mib.udp_statistics) : \ 550 (IS_UDPLITE(sk) ? sock_net(sk)->mib.udplite_stats_in6 : \ 551 sock_net(sk)->mib.udp_stats_in6); \ 552 }) 553 #else 554 #define __UDPX_MIB(sk, ipv4) \ 555 ({ \ 556 IS_UDPLITE(sk) ? sock_net(sk)->mib.udplite_statistics : \ 557 sock_net(sk)->mib.udp_statistics; \ 558 }) 559 #endif 560 561 #define __UDPX_INC_STATS(sk, field) \ 562 __SNMP_INC_STATS(__UDPX_MIB(sk, (sk)->sk_family == AF_INET), field) 563 564 #ifdef CONFIG_PROC_FS 565 struct udp_seq_afinfo { 566 sa_family_t family; 567 struct udp_table *udp_table; 568 }; 569 570 struct udp_iter_state { 571 struct seq_net_private p; 572 int bucket; 573 }; 574 575 void *udp_seq_start(struct seq_file *seq, loff_t *pos); 576 void *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos); 577 void udp_seq_stop(struct seq_file *seq, void *v); 578 579 extern const struct seq_operations udp_seq_ops; 580 extern const struct seq_operations udp6_seq_ops; 581 582 int udp4_proc_init(void); 583 void udp4_proc_exit(void); 584 #endif /* CONFIG_PROC_FS */ 585 586 int udpv4_offload_init(void); 587 588 void udp_init(void); 589 590 DECLARE_STATIC_KEY_FALSE(udp_encap_needed_key); 591 void udp_encap_enable(void); 592 void udp_encap_disable(void); 593 #if IS_ENABLED(CONFIG_IPV6) 594 DECLARE_STATIC_KEY_FALSE(udpv6_encap_needed_key); 595 void udpv6_encap_enable(void); 596 #endif 597 598 static inline struct sk_buff *udp_rcv_segment(struct sock *sk, 599 struct sk_buff *skb, bool ipv4) 600 { 601 netdev_features_t features = NETIF_F_SG; 602 struct sk_buff *segs; 603 int drop_count; 604 605 /* 606 * Segmentation in UDP receive path is only for UDP GRO, drop udp 607 * fragmentation offload (UFO) packets. 608 */ 609 if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP) { 610 drop_count = 1; 611 goto drop; 612 } 613 614 /* Avoid csum recalculation by skb_segment unless userspace explicitly 615 * asks for the final checksum values 616 */ 617 if (!inet_get_convert_csum(sk)) 618 features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM; 619 620 /* UDP segmentation expects packets of type CHECKSUM_PARTIAL or 621 * CHECKSUM_NONE in __udp_gso_segment. UDP GRO indeed builds partial 622 * packets in udp_gro_complete_segment. As does UDP GSO, verified by 623 * udp_send_skb. But when those packets are looped in dev_loopback_xmit 624 * their ip_summed CHECKSUM_NONE is changed to CHECKSUM_UNNECESSARY. 625 * Reset in this specific case, where PARTIAL is both correct and 626 * required. 627 */ 628 if (skb->pkt_type == PACKET_LOOPBACK) 629 skb->ip_summed = CHECKSUM_PARTIAL; 630 631 /* the GSO CB lays after the UDP one, no need to save and restore any 632 * CB fragment 633 */ 634 segs = __skb_gso_segment(skb, features, false); 635 if (IS_ERR_OR_NULL(segs)) { 636 drop_count = skb_shinfo(skb)->gso_segs; 637 goto drop; 638 } 639 640 consume_skb(skb); 641 return segs; 642 643 drop: 644 sk_drops_add(sk, drop_count); 645 SNMP_ADD_STATS(__UDPX_MIB(sk, ipv4), UDP_MIB_INERRORS, drop_count); 646 kfree_skb(skb); 647 return NULL; 648 } 649 650 static inline void udp_post_segment_fix_csum(struct sk_buff *skb) 651 { 652 /* UDP-lite can't land here - no GRO */ 653 WARN_ON_ONCE(UDP_SKB_CB(skb)->partial_cov); 654 655 /* UDP packets generated with UDP_SEGMENT and traversing: 656 * 657 * UDP tunnel(xmit) -> veth (segmentation) -> veth (gro) -> UDP tunnel (rx) 658 * 659 * can reach an UDP socket with CHECKSUM_NONE, because 660 * __iptunnel_pull_header() converts CHECKSUM_PARTIAL into NONE. 661 * SKB_GSO_UDP_L4 or SKB_GSO_FRAGLIST packets with no UDP tunnel will 662 * have a valid checksum, as the GRO engine validates the UDP csum 663 * before the aggregation and nobody strips such info in between. 664 * Instead of adding another check in the tunnel fastpath, we can force 665 * a valid csum after the segmentation. 666 * Additionally fixup the UDP CB. 667 */ 668 UDP_SKB_CB(skb)->cscov = skb->len; 669 if (skb->ip_summed == CHECKSUM_NONE && !skb->csum_valid) 670 skb->csum_valid = 1; 671 } 672 673 #ifdef CONFIG_BPF_SYSCALL 674 struct sk_psock; 675 int udp_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore); 676 #endif 677 678 #endif /* _UDP_H */ 679