18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */ 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * INET An implementation of the TCP/IP protocol suite for the LINUX 48c2ecf20Sopenharmony_ci * operating system. INET is implemented using the BSD Socket 58c2ecf20Sopenharmony_ci * interface as the means of communication with the user level. 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * Definitions for the UDP protocol. 88c2ecf20Sopenharmony_ci * 98c2ecf20Sopenharmony_ci * Version: @(#)udp.h 1.0.2 04/28/93 108c2ecf20Sopenharmony_ci * 118c2ecf20Sopenharmony_ci * Author: Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> 128c2ecf20Sopenharmony_ci */ 138c2ecf20Sopenharmony_ci#ifndef _LINUX_UDP_H 148c2ecf20Sopenharmony_ci#define _LINUX_UDP_H 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ci#include <net/inet_sock.h> 178c2ecf20Sopenharmony_ci#include <linux/skbuff.h> 188c2ecf20Sopenharmony_ci#include <net/netns/hash.h> 198c2ecf20Sopenharmony_ci#include <uapi/linux/udp.h> 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_cistatic inline struct udphdr *udp_hdr(const struct sk_buff *skb) 228c2ecf20Sopenharmony_ci{ 238c2ecf20Sopenharmony_ci return (struct udphdr *)skb_transport_header(skb); 248c2ecf20Sopenharmony_ci} 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_cistatic inline struct udphdr *inner_udp_hdr(const struct sk_buff *skb) 278c2ecf20Sopenharmony_ci{ 288c2ecf20Sopenharmony_ci return (struct udphdr *)skb_inner_transport_header(skb); 298c2ecf20Sopenharmony_ci} 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_ci#define UDP_HTABLE_SIZE_MIN (CONFIG_BASE_SMALL ? 128 : 256) 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_cistatic inline u32 udp_hashfn(const struct net *net, u32 num, u32 mask) 348c2ecf20Sopenharmony_ci{ 358c2ecf20Sopenharmony_ci return (num + net_hash_mix(net)) & mask; 368c2ecf20Sopenharmony_ci} 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_cistruct udp_sock { 398c2ecf20Sopenharmony_ci /* inet_sock has to be the first member */ 408c2ecf20Sopenharmony_ci struct inet_sock inet; 418c2ecf20Sopenharmony_ci#define udp_port_hash inet.sk.__sk_common.skc_u16hashes[0] 428c2ecf20Sopenharmony_ci#define udp_portaddr_hash inet.sk.__sk_common.skc_u16hashes[1] 438c2ecf20Sopenharmony_ci#define udp_portaddr_node inet.sk.__sk_common.skc_portaddr_node 448c2ecf20Sopenharmony_ci int pending; /* Any pending frames ? */ 458c2ecf20Sopenharmony_ci unsigned int corkflag; /* Cork is required */ 468c2ecf20Sopenharmony_ci __u8 encap_type; /* Is this an Encapsulation socket? */ 478c2ecf20Sopenharmony_ci unsigned char no_check6_tx:1,/* Send zero UDP6 checksums on TX? */ 488c2ecf20Sopenharmony_ci no_check6_rx:1,/* Allow zero UDP6 checksums on RX? */ 498c2ecf20Sopenharmony_ci encap_enabled:1, /* This socket enabled encap 508c2ecf20Sopenharmony_ci * processing; UDP tunnels and 518c2ecf20Sopenharmony_ci * different encapsulation layer set 528c2ecf20Sopenharmony_ci * this 538c2ecf20Sopenharmony_ci */ 548c2ecf20Sopenharmony_ci gro_enabled:1, /* Request GRO aggregation */ 558c2ecf20Sopenharmony_ci accept_udp_l4:1, 568c2ecf20Sopenharmony_ci accept_udp_fraglist:1; 578c2ecf20Sopenharmony_ci /* 588c2ecf20Sopenharmony_ci * Following member retains the information to create a UDP header 598c2ecf20Sopenharmony_ci * when the socket is uncorked. 608c2ecf20Sopenharmony_ci */ 618c2ecf20Sopenharmony_ci __u16 len; /* total length of pending frames */ 628c2ecf20Sopenharmony_ci __u16 gso_size; 638c2ecf20Sopenharmony_ci /* 648c2ecf20Sopenharmony_ci * Fields specific to UDP-Lite. 658c2ecf20Sopenharmony_ci */ 668c2ecf20Sopenharmony_ci __u16 pcslen; 678c2ecf20Sopenharmony_ci __u16 pcrlen; 688c2ecf20Sopenharmony_ci/* indicator bits used by pcflag: */ 698c2ecf20Sopenharmony_ci#define UDPLITE_BIT 0x1 /* set by udplite proto init function */ 708c2ecf20Sopenharmony_ci#define UDPLITE_SEND_CC 0x2 /* set via udplite setsockopt */ 718c2ecf20Sopenharmony_ci#define UDPLITE_RECV_CC 0x4 /* set via udplite setsocktopt */ 728c2ecf20Sopenharmony_ci __u8 pcflag; /* marks socket as UDP-Lite if > 0 */ 738c2ecf20Sopenharmony_ci __u8 unused[3]; 748c2ecf20Sopenharmony_ci /* 758c2ecf20Sopenharmony_ci * For encapsulation sockets. 768c2ecf20Sopenharmony_ci */ 778c2ecf20Sopenharmony_ci int (*encap_rcv)(struct sock *sk, struct sk_buff *skb); 788c2ecf20Sopenharmony_ci int (*encap_err_lookup)(struct sock *sk, struct sk_buff *skb); 798c2ecf20Sopenharmony_ci void (*encap_destroy)(struct sock *sk); 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci /* GRO functions for UDP socket */ 828c2ecf20Sopenharmony_ci struct sk_buff * (*gro_receive)(struct sock *sk, 838c2ecf20Sopenharmony_ci struct list_head *head, 848c2ecf20Sopenharmony_ci struct sk_buff *skb); 858c2ecf20Sopenharmony_ci int (*gro_complete)(struct sock *sk, 868c2ecf20Sopenharmony_ci struct sk_buff *skb, 878c2ecf20Sopenharmony_ci int nhoff); 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_ci /* udp_recvmsg try to use this before splicing sk_receive_queue */ 908c2ecf20Sopenharmony_ci struct sk_buff_head reader_queue ____cacheline_aligned_in_smp; 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ci /* This field is dirtied by udp_recvmsg() */ 938c2ecf20Sopenharmony_ci int forward_deficit; 948c2ecf20Sopenharmony_ci}; 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_ci#define UDP_MAX_SEGMENTS (1 << 6UL) 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_cistatic inline struct udp_sock *udp_sk(const struct sock *sk) 998c2ecf20Sopenharmony_ci{ 1008c2ecf20Sopenharmony_ci return (struct udp_sock *)sk; 1018c2ecf20Sopenharmony_ci} 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_cistatic inline void udp_set_no_check6_tx(struct sock *sk, bool val) 1048c2ecf20Sopenharmony_ci{ 1058c2ecf20Sopenharmony_ci udp_sk(sk)->no_check6_tx = val; 1068c2ecf20Sopenharmony_ci} 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_cistatic inline void udp_set_no_check6_rx(struct sock *sk, bool val) 1098c2ecf20Sopenharmony_ci{ 1108c2ecf20Sopenharmony_ci udp_sk(sk)->no_check6_rx = val; 1118c2ecf20Sopenharmony_ci} 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_cistatic inline bool udp_get_no_check6_tx(struct sock *sk) 1148c2ecf20Sopenharmony_ci{ 1158c2ecf20Sopenharmony_ci return udp_sk(sk)->no_check6_tx; 1168c2ecf20Sopenharmony_ci} 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_cistatic inline bool udp_get_no_check6_rx(struct sock *sk) 1198c2ecf20Sopenharmony_ci{ 1208c2ecf20Sopenharmony_ci return udp_sk(sk)->no_check6_rx; 1218c2ecf20Sopenharmony_ci} 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_cistatic inline void udp_cmsg_recv(struct msghdr *msg, struct sock *sk, 1248c2ecf20Sopenharmony_ci struct sk_buff *skb) 1258c2ecf20Sopenharmony_ci{ 1268c2ecf20Sopenharmony_ci int gso_size; 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_ci if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) { 1298c2ecf20Sopenharmony_ci gso_size = skb_shinfo(skb)->gso_size; 1308c2ecf20Sopenharmony_ci put_cmsg(msg, SOL_UDP, UDP_GRO, sizeof(gso_size), &gso_size); 1318c2ecf20Sopenharmony_ci } 1328c2ecf20Sopenharmony_ci} 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_ciDECLARE_STATIC_KEY_FALSE(udp_encap_needed_key); 1358c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_IPV6) 1368c2ecf20Sopenharmony_ciDECLARE_STATIC_KEY_FALSE(udpv6_encap_needed_key); 1378c2ecf20Sopenharmony_ci#endif 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_cistatic inline bool udp_encap_needed(void) 1408c2ecf20Sopenharmony_ci{ 1418c2ecf20Sopenharmony_ci if (static_branch_unlikely(&udp_encap_needed_key)) 1428c2ecf20Sopenharmony_ci return true; 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_IPV6) 1458c2ecf20Sopenharmony_ci if (static_branch_unlikely(&udpv6_encap_needed_key)) 1468c2ecf20Sopenharmony_ci return true; 1478c2ecf20Sopenharmony_ci#endif 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_ci return false; 1508c2ecf20Sopenharmony_ci} 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_cistatic inline bool udp_unexpected_gso(struct sock *sk, struct sk_buff *skb) 1538c2ecf20Sopenharmony_ci{ 1548c2ecf20Sopenharmony_ci if (!skb_is_gso(skb)) 1558c2ecf20Sopenharmony_ci return false; 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_ci if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4 && !udp_sk(sk)->accept_udp_l4) 1588c2ecf20Sopenharmony_ci return true; 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_ci if (skb_shinfo(skb)->gso_type & SKB_GSO_FRAGLIST && !udp_sk(sk)->accept_udp_fraglist) 1618c2ecf20Sopenharmony_ci return true; 1628c2ecf20Sopenharmony_ci 1638c2ecf20Sopenharmony_ci /* GSO packets lacking the SKB_GSO_UDP_TUNNEL/_CSUM bits might still 1648c2ecf20Sopenharmony_ci * land in a tunnel as the socket check in udp_gro_receive cannot be 1658c2ecf20Sopenharmony_ci * foolproof. 1668c2ecf20Sopenharmony_ci */ 1678c2ecf20Sopenharmony_ci if (udp_encap_needed() && 1688c2ecf20Sopenharmony_ci READ_ONCE(udp_sk(sk)->encap_rcv) && 1698c2ecf20Sopenharmony_ci !(skb_shinfo(skb)->gso_type & 1708c2ecf20Sopenharmony_ci (SKB_GSO_UDP_TUNNEL | SKB_GSO_UDP_TUNNEL_CSUM))) 1718c2ecf20Sopenharmony_ci return true; 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_ci return false; 1748c2ecf20Sopenharmony_ci} 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_ci#define udp_portaddr_for_each_entry(__sk, list) \ 1778c2ecf20Sopenharmony_ci hlist_for_each_entry(__sk, list, __sk_common.skc_portaddr_node) 1788c2ecf20Sopenharmony_ci 1798c2ecf20Sopenharmony_ci#define udp_portaddr_for_each_entry_rcu(__sk, list) \ 1808c2ecf20Sopenharmony_ci hlist_for_each_entry_rcu(__sk, list, __sk_common.skc_portaddr_node) 1818c2ecf20Sopenharmony_ci 1828c2ecf20Sopenharmony_ci#define IS_UDPLITE(__sk) (__sk->sk_protocol == IPPROTO_UDPLITE) 1838c2ecf20Sopenharmony_ci 1848c2ecf20Sopenharmony_ci#endif /* _LINUX_UDP_H */ 185