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 module. 88c2ecf20Sopenharmony_ci * 98c2ecf20Sopenharmony_ci * Version: @(#)udp.h 1.0.2 05/07/93 108c2ecf20Sopenharmony_ci * 118c2ecf20Sopenharmony_ci * Authors: Ross Biro 128c2ecf20Sopenharmony_ci * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> 138c2ecf20Sopenharmony_ci * 148c2ecf20Sopenharmony_ci * Fixes: 158c2ecf20Sopenharmony_ci * Alan Cox : Turned on udp checksums. I don't want to 168c2ecf20Sopenharmony_ci * chase 'memory corruption' bugs that aren't! 178c2ecf20Sopenharmony_ci */ 188c2ecf20Sopenharmony_ci#ifndef _UDP_H 198c2ecf20Sopenharmony_ci#define _UDP_H 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_ci#include <linux/list.h> 228c2ecf20Sopenharmony_ci#include <linux/bug.h> 238c2ecf20Sopenharmony_ci#include <net/inet_sock.h> 248c2ecf20Sopenharmony_ci#include <net/sock.h> 258c2ecf20Sopenharmony_ci#include <net/snmp.h> 268c2ecf20Sopenharmony_ci#include <net/ip.h> 278c2ecf20Sopenharmony_ci#include <linux/ipv6.h> 288c2ecf20Sopenharmony_ci#include <linux/seq_file.h> 298c2ecf20Sopenharmony_ci#include <linux/poll.h> 308c2ecf20Sopenharmony_ci#include <linux/indirect_call_wrapper.h> 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ci/** 338c2ecf20Sopenharmony_ci * struct udp_skb_cb - UDP(-Lite) private variables 348c2ecf20Sopenharmony_ci * 358c2ecf20Sopenharmony_ci * @header: private variables used by IPv4/IPv6 368c2ecf20Sopenharmony_ci * @cscov: checksum coverage length (UDP-Lite only) 378c2ecf20Sopenharmony_ci * @partial_cov: if set indicates partial csum coverage 388c2ecf20Sopenharmony_ci */ 398c2ecf20Sopenharmony_cistruct udp_skb_cb { 408c2ecf20Sopenharmony_ci union { 418c2ecf20Sopenharmony_ci struct inet_skb_parm h4; 428c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_IPV6) 438c2ecf20Sopenharmony_ci struct inet6_skb_parm h6; 448c2ecf20Sopenharmony_ci#endif 458c2ecf20Sopenharmony_ci } header; 468c2ecf20Sopenharmony_ci __u16 cscov; 478c2ecf20Sopenharmony_ci __u8 partial_cov; 488c2ecf20Sopenharmony_ci}; 498c2ecf20Sopenharmony_ci#define UDP_SKB_CB(__skb) ((struct udp_skb_cb *)((__skb)->cb)) 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_ci/** 528c2ecf20Sopenharmony_ci * struct udp_hslot - UDP hash slot 538c2ecf20Sopenharmony_ci * 548c2ecf20Sopenharmony_ci * @head: head of list of sockets 558c2ecf20Sopenharmony_ci * @count: number of sockets in 'head' list 568c2ecf20Sopenharmony_ci * @lock: spinlock protecting changes to head/count 578c2ecf20Sopenharmony_ci */ 588c2ecf20Sopenharmony_cistruct udp_hslot { 598c2ecf20Sopenharmony_ci struct hlist_head head; 608c2ecf20Sopenharmony_ci int count; 618c2ecf20Sopenharmony_ci spinlock_t lock; 628c2ecf20Sopenharmony_ci} __attribute__((aligned(2 * sizeof(long)))); 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci/** 658c2ecf20Sopenharmony_ci * struct udp_table - UDP table 668c2ecf20Sopenharmony_ci * 678c2ecf20Sopenharmony_ci * @hash: hash table, sockets are hashed on (local port) 688c2ecf20Sopenharmony_ci * @hash2: hash table, sockets are hashed on (local port, local address) 698c2ecf20Sopenharmony_ci * @mask: number of slots in hash tables, minus 1 708c2ecf20Sopenharmony_ci * @log: log2(number of slots in hash table) 718c2ecf20Sopenharmony_ci */ 728c2ecf20Sopenharmony_cistruct udp_table { 738c2ecf20Sopenharmony_ci struct udp_hslot *hash; 748c2ecf20Sopenharmony_ci struct udp_hslot *hash2; 758c2ecf20Sopenharmony_ci unsigned int mask; 768c2ecf20Sopenharmony_ci unsigned int log; 778c2ecf20Sopenharmony_ci}; 788c2ecf20Sopenharmony_ciextern struct udp_table udp_table; 798c2ecf20Sopenharmony_civoid udp_table_init(struct udp_table *, const char *); 808c2ecf20Sopenharmony_cistatic inline struct udp_hslot *udp_hashslot(struct udp_table *table, 818c2ecf20Sopenharmony_ci struct net *net, unsigned int num) 828c2ecf20Sopenharmony_ci{ 838c2ecf20Sopenharmony_ci return &table->hash[udp_hashfn(net, num, table->mask)]; 848c2ecf20Sopenharmony_ci} 858c2ecf20Sopenharmony_ci/* 868c2ecf20Sopenharmony_ci * For secondary hash, net_hash_mix() is performed before calling 878c2ecf20Sopenharmony_ci * udp_hashslot2(), this explains difference with udp_hashslot() 888c2ecf20Sopenharmony_ci */ 898c2ecf20Sopenharmony_cistatic inline struct udp_hslot *udp_hashslot2(struct udp_table *table, 908c2ecf20Sopenharmony_ci unsigned int hash) 918c2ecf20Sopenharmony_ci{ 928c2ecf20Sopenharmony_ci return &table->hash2[hash & table->mask]; 938c2ecf20Sopenharmony_ci} 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_ciextern struct proto udp_prot; 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_ciextern atomic_long_t udp_memory_allocated; 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_ci/* sysctl variables for udp */ 1008c2ecf20Sopenharmony_ciextern long sysctl_udp_mem[3]; 1018c2ecf20Sopenharmony_ciextern int sysctl_udp_rmem_min; 1028c2ecf20Sopenharmony_ciextern int sysctl_udp_wmem_min; 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_cistruct sk_buff; 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ci/* 1078c2ecf20Sopenharmony_ci * Generic checksumming routines for UDP(-Lite) v4 and v6 1088c2ecf20Sopenharmony_ci */ 1098c2ecf20Sopenharmony_cistatic inline __sum16 __udp_lib_checksum_complete(struct sk_buff *skb) 1108c2ecf20Sopenharmony_ci{ 1118c2ecf20Sopenharmony_ci return (UDP_SKB_CB(skb)->cscov == skb->len ? 1128c2ecf20Sopenharmony_ci __skb_checksum_complete(skb) : 1138c2ecf20Sopenharmony_ci __skb_checksum_complete_head(skb, UDP_SKB_CB(skb)->cscov)); 1148c2ecf20Sopenharmony_ci} 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_cistatic inline int udp_lib_checksum_complete(struct sk_buff *skb) 1178c2ecf20Sopenharmony_ci{ 1188c2ecf20Sopenharmony_ci return !skb_csum_unnecessary(skb) && 1198c2ecf20Sopenharmony_ci __udp_lib_checksum_complete(skb); 1208c2ecf20Sopenharmony_ci} 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_ci/** 1238c2ecf20Sopenharmony_ci * udp_csum_outgoing - compute UDPv4/v6 checksum over fragments 1248c2ecf20Sopenharmony_ci * @sk: socket we are writing to 1258c2ecf20Sopenharmony_ci * @skb: sk_buff containing the filled-in UDP header 1268c2ecf20Sopenharmony_ci * (checksum field must be zeroed out) 1278c2ecf20Sopenharmony_ci */ 1288c2ecf20Sopenharmony_cistatic inline __wsum udp_csum_outgoing(struct sock *sk, struct sk_buff *skb) 1298c2ecf20Sopenharmony_ci{ 1308c2ecf20Sopenharmony_ci __wsum csum = csum_partial(skb_transport_header(skb), 1318c2ecf20Sopenharmony_ci sizeof(struct udphdr), 0); 1328c2ecf20Sopenharmony_ci skb_queue_walk(&sk->sk_write_queue, skb) { 1338c2ecf20Sopenharmony_ci csum = csum_add(csum, skb->csum); 1348c2ecf20Sopenharmony_ci } 1358c2ecf20Sopenharmony_ci return csum; 1368c2ecf20Sopenharmony_ci} 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_cistatic inline __wsum udp_csum(struct sk_buff *skb) 1398c2ecf20Sopenharmony_ci{ 1408c2ecf20Sopenharmony_ci __wsum csum = csum_partial(skb_transport_header(skb), 1418c2ecf20Sopenharmony_ci sizeof(struct udphdr), skb->csum); 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_ci for (skb = skb_shinfo(skb)->frag_list; skb; skb = skb->next) { 1448c2ecf20Sopenharmony_ci csum = csum_add(csum, skb->csum); 1458c2ecf20Sopenharmony_ci } 1468c2ecf20Sopenharmony_ci return csum; 1478c2ecf20Sopenharmony_ci} 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_cistatic inline __sum16 udp_v4_check(int len, __be32 saddr, 1508c2ecf20Sopenharmony_ci __be32 daddr, __wsum base) 1518c2ecf20Sopenharmony_ci{ 1528c2ecf20Sopenharmony_ci return csum_tcpudp_magic(saddr, daddr, len, IPPROTO_UDP, base); 1538c2ecf20Sopenharmony_ci} 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_civoid udp_set_csum(bool nocheck, struct sk_buff *skb, 1568c2ecf20Sopenharmony_ci __be32 saddr, __be32 daddr, int len); 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_cistatic inline void udp_csum_pull_header(struct sk_buff *skb) 1598c2ecf20Sopenharmony_ci{ 1608c2ecf20Sopenharmony_ci if (!skb->csum_valid && skb->ip_summed == CHECKSUM_NONE) 1618c2ecf20Sopenharmony_ci skb->csum = csum_partial(skb->data, sizeof(struct udphdr), 1628c2ecf20Sopenharmony_ci skb->csum); 1638c2ecf20Sopenharmony_ci skb_pull_rcsum(skb, sizeof(struct udphdr)); 1648c2ecf20Sopenharmony_ci UDP_SKB_CB(skb)->cscov -= sizeof(struct udphdr); 1658c2ecf20Sopenharmony_ci} 1668c2ecf20Sopenharmony_ci 1678c2ecf20Sopenharmony_citypedef struct sock *(*udp_lookup_t)(struct sk_buff *skb, __be16 sport, 1688c2ecf20Sopenharmony_ci __be16 dport); 1698c2ecf20Sopenharmony_ci 1708c2ecf20Sopenharmony_ciINDIRECT_CALLABLE_DECLARE(struct sk_buff *udp4_gro_receive(struct list_head *, 1718c2ecf20Sopenharmony_ci struct sk_buff *)); 1728c2ecf20Sopenharmony_ciINDIRECT_CALLABLE_DECLARE(int udp4_gro_complete(struct sk_buff *, int)); 1738c2ecf20Sopenharmony_ciINDIRECT_CALLABLE_DECLARE(struct sk_buff *udp6_gro_receive(struct list_head *, 1748c2ecf20Sopenharmony_ci struct sk_buff *)); 1758c2ecf20Sopenharmony_ciINDIRECT_CALLABLE_DECLARE(int udp6_gro_complete(struct sk_buff *, int)); 1768c2ecf20Sopenharmony_cistruct sk_buff *udp_gro_receive(struct list_head *head, struct sk_buff *skb, 1778c2ecf20Sopenharmony_ci struct udphdr *uh, struct sock *sk); 1788c2ecf20Sopenharmony_ciint udp_gro_complete(struct sk_buff *skb, int nhoff, udp_lookup_t lookup); 1798c2ecf20Sopenharmony_civoid udp_v6_early_demux(struct sk_buff *skb); 1808c2ecf20Sopenharmony_ci 1818c2ecf20Sopenharmony_cistruct sk_buff *__udp_gso_segment(struct sk_buff *gso_skb, 1828c2ecf20Sopenharmony_ci netdev_features_t features, bool is_ipv6); 1838c2ecf20Sopenharmony_ci 1848c2ecf20Sopenharmony_cistatic inline struct udphdr *udp_gro_udphdr(struct sk_buff *skb) 1858c2ecf20Sopenharmony_ci{ 1868c2ecf20Sopenharmony_ci struct udphdr *uh; 1878c2ecf20Sopenharmony_ci unsigned int hlen, off; 1888c2ecf20Sopenharmony_ci 1898c2ecf20Sopenharmony_ci off = skb_gro_offset(skb); 1908c2ecf20Sopenharmony_ci hlen = off + sizeof(*uh); 1918c2ecf20Sopenharmony_ci uh = skb_gro_header_fast(skb, off); 1928c2ecf20Sopenharmony_ci if (skb_gro_header_hard(skb, hlen)) 1938c2ecf20Sopenharmony_ci uh = skb_gro_header_slow(skb, hlen, off); 1948c2ecf20Sopenharmony_ci 1958c2ecf20Sopenharmony_ci return uh; 1968c2ecf20Sopenharmony_ci} 1978c2ecf20Sopenharmony_ci 1988c2ecf20Sopenharmony_ci/* hash routines shared between UDPv4/6 and UDP-Litev4/6 */ 1998c2ecf20Sopenharmony_cistatic inline int udp_lib_hash(struct sock *sk) 2008c2ecf20Sopenharmony_ci{ 2018c2ecf20Sopenharmony_ci BUG(); 2028c2ecf20Sopenharmony_ci return 0; 2038c2ecf20Sopenharmony_ci} 2048c2ecf20Sopenharmony_ci 2058c2ecf20Sopenharmony_civoid udp_lib_unhash(struct sock *sk); 2068c2ecf20Sopenharmony_civoid udp_lib_rehash(struct sock *sk, u16 new_hash); 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_cistatic inline void udp_lib_close(struct sock *sk, long timeout) 2098c2ecf20Sopenharmony_ci{ 2108c2ecf20Sopenharmony_ci sk_common_release(sk); 2118c2ecf20Sopenharmony_ci} 2128c2ecf20Sopenharmony_ci 2138c2ecf20Sopenharmony_ciint udp_lib_get_port(struct sock *sk, unsigned short snum, 2148c2ecf20Sopenharmony_ci unsigned int hash2_nulladdr); 2158c2ecf20Sopenharmony_ci 2168c2ecf20Sopenharmony_ciu32 udp_flow_hashrnd(void); 2178c2ecf20Sopenharmony_ci 2188c2ecf20Sopenharmony_cistatic inline __be16 udp_flow_src_port(struct net *net, struct sk_buff *skb, 2198c2ecf20Sopenharmony_ci int min, int max, bool use_eth) 2208c2ecf20Sopenharmony_ci{ 2218c2ecf20Sopenharmony_ci u32 hash; 2228c2ecf20Sopenharmony_ci 2238c2ecf20Sopenharmony_ci if (min >= max) { 2248c2ecf20Sopenharmony_ci /* Use default range */ 2258c2ecf20Sopenharmony_ci inet_get_local_port_range(net, &min, &max); 2268c2ecf20Sopenharmony_ci } 2278c2ecf20Sopenharmony_ci 2288c2ecf20Sopenharmony_ci hash = skb_get_hash(skb); 2298c2ecf20Sopenharmony_ci if (unlikely(!hash)) { 2308c2ecf20Sopenharmony_ci if (use_eth) { 2318c2ecf20Sopenharmony_ci /* Can't find a normal hash, caller has indicated an 2328c2ecf20Sopenharmony_ci * Ethernet packet so use that to compute a hash. 2338c2ecf20Sopenharmony_ci */ 2348c2ecf20Sopenharmony_ci hash = jhash(skb->data, 2 * ETH_ALEN, 2358c2ecf20Sopenharmony_ci (__force u32) skb->protocol); 2368c2ecf20Sopenharmony_ci } else { 2378c2ecf20Sopenharmony_ci /* Can't derive any sort of hash for the packet, set 2388c2ecf20Sopenharmony_ci * to some consistent random value. 2398c2ecf20Sopenharmony_ci */ 2408c2ecf20Sopenharmony_ci hash = udp_flow_hashrnd(); 2418c2ecf20Sopenharmony_ci } 2428c2ecf20Sopenharmony_ci } 2438c2ecf20Sopenharmony_ci 2448c2ecf20Sopenharmony_ci /* Since this is being sent on the wire obfuscate hash a bit 2458c2ecf20Sopenharmony_ci * to minimize possbility that any useful information to an 2468c2ecf20Sopenharmony_ci * attacker is leaked. Only upper 16 bits are relevant in the 2478c2ecf20Sopenharmony_ci * computation for 16 bit port value. 2488c2ecf20Sopenharmony_ci */ 2498c2ecf20Sopenharmony_ci hash ^= hash << 16; 2508c2ecf20Sopenharmony_ci 2518c2ecf20Sopenharmony_ci return htons((((u64) hash * (max - min)) >> 32) + min); 2528c2ecf20Sopenharmony_ci} 2538c2ecf20Sopenharmony_ci 2548c2ecf20Sopenharmony_cistatic inline int udp_rqueue_get(struct sock *sk) 2558c2ecf20Sopenharmony_ci{ 2568c2ecf20Sopenharmony_ci return sk_rmem_alloc_get(sk) - READ_ONCE(udp_sk(sk)->forward_deficit); 2578c2ecf20Sopenharmony_ci} 2588c2ecf20Sopenharmony_ci 2598c2ecf20Sopenharmony_cistatic inline bool udp_sk_bound_dev_eq(struct net *net, int bound_dev_if, 2608c2ecf20Sopenharmony_ci int dif, int sdif) 2618c2ecf20Sopenharmony_ci{ 2628c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_NET_L3_MASTER_DEV) 2638c2ecf20Sopenharmony_ci return inet_bound_dev_eq(!!READ_ONCE(net->ipv4.sysctl_udp_l3mdev_accept), 2648c2ecf20Sopenharmony_ci bound_dev_if, dif, sdif); 2658c2ecf20Sopenharmony_ci#else 2668c2ecf20Sopenharmony_ci return inet_bound_dev_eq(true, bound_dev_if, dif, sdif); 2678c2ecf20Sopenharmony_ci#endif 2688c2ecf20Sopenharmony_ci} 2698c2ecf20Sopenharmony_ci 2708c2ecf20Sopenharmony_ci/* net/ipv4/udp.c */ 2718c2ecf20Sopenharmony_civoid udp_destruct_common(struct sock *sk); 2728c2ecf20Sopenharmony_civoid skb_consume_udp(struct sock *sk, struct sk_buff *skb, int len); 2738c2ecf20Sopenharmony_ciint __udp_enqueue_schedule_skb(struct sock *sk, struct sk_buff *skb); 2748c2ecf20Sopenharmony_civoid udp_skb_destructor(struct sock *sk, struct sk_buff *skb); 2758c2ecf20Sopenharmony_cistruct sk_buff *__skb_recv_udp(struct sock *sk, unsigned int flags, 2768c2ecf20Sopenharmony_ci int noblock, int *off, int *err); 2778c2ecf20Sopenharmony_cistatic inline struct sk_buff *skb_recv_udp(struct sock *sk, unsigned int flags, 2788c2ecf20Sopenharmony_ci int noblock, int *err) 2798c2ecf20Sopenharmony_ci{ 2808c2ecf20Sopenharmony_ci int off = 0; 2818c2ecf20Sopenharmony_ci 2828c2ecf20Sopenharmony_ci return __skb_recv_udp(sk, flags, noblock, &off, err); 2838c2ecf20Sopenharmony_ci} 2848c2ecf20Sopenharmony_ci 2858c2ecf20Sopenharmony_ciint udp_v4_early_demux(struct sk_buff *skb); 2868c2ecf20Sopenharmony_cibool udp_sk_rx_dst_set(struct sock *sk, struct dst_entry *dst); 2878c2ecf20Sopenharmony_ciint udp_get_port(struct sock *sk, unsigned short snum, 2888c2ecf20Sopenharmony_ci int (*saddr_cmp)(const struct sock *, 2898c2ecf20Sopenharmony_ci const struct sock *)); 2908c2ecf20Sopenharmony_ciint udp_err(struct sk_buff *, u32); 2918c2ecf20Sopenharmony_ciint udp_abort(struct sock *sk, int err); 2928c2ecf20Sopenharmony_ciint udp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len); 2938c2ecf20Sopenharmony_ciint udp_push_pending_frames(struct sock *sk); 2948c2ecf20Sopenharmony_civoid udp_flush_pending_frames(struct sock *sk); 2958c2ecf20Sopenharmony_ciint udp_cmsg_send(struct sock *sk, struct msghdr *msg, u16 *gso_size); 2968c2ecf20Sopenharmony_civoid udp4_hwcsum(struct sk_buff *skb, __be32 src, __be32 dst); 2978c2ecf20Sopenharmony_ciint udp_rcv(struct sk_buff *skb); 2988c2ecf20Sopenharmony_ciint udp_ioctl(struct sock *sk, int cmd, unsigned long arg); 2998c2ecf20Sopenharmony_ciint udp_init_sock(struct sock *sk); 3008c2ecf20Sopenharmony_ciint udp_pre_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len); 3018c2ecf20Sopenharmony_ciint __udp_disconnect(struct sock *sk, int flags); 3028c2ecf20Sopenharmony_ciint udp_disconnect(struct sock *sk, int flags); 3038c2ecf20Sopenharmony_ci__poll_t udp_poll(struct file *file, struct socket *sock, poll_table *wait); 3048c2ecf20Sopenharmony_cistruct sk_buff *skb_udp_tunnel_segment(struct sk_buff *skb, 3058c2ecf20Sopenharmony_ci netdev_features_t features, 3068c2ecf20Sopenharmony_ci bool is_ipv6); 3078c2ecf20Sopenharmony_ciint udp_lib_getsockopt(struct sock *sk, int level, int optname, 3088c2ecf20Sopenharmony_ci char __user *optval, int __user *optlen); 3098c2ecf20Sopenharmony_ciint udp_lib_setsockopt(struct sock *sk, int level, int optname, 3108c2ecf20Sopenharmony_ci sockptr_t optval, unsigned int optlen, 3118c2ecf20Sopenharmony_ci int (*push_pending_frames)(struct sock *)); 3128c2ecf20Sopenharmony_cistruct sock *udp4_lib_lookup(struct net *net, __be32 saddr, __be16 sport, 3138c2ecf20Sopenharmony_ci __be32 daddr, __be16 dport, int dif); 3148c2ecf20Sopenharmony_cistruct sock *__udp4_lib_lookup(struct net *net, __be32 saddr, __be16 sport, 3158c2ecf20Sopenharmony_ci __be32 daddr, __be16 dport, int dif, int sdif, 3168c2ecf20Sopenharmony_ci struct udp_table *tbl, struct sk_buff *skb); 3178c2ecf20Sopenharmony_cistruct sock *udp4_lib_lookup_skb(struct sk_buff *skb, 3188c2ecf20Sopenharmony_ci __be16 sport, __be16 dport); 3198c2ecf20Sopenharmony_cistruct sock *udp6_lib_lookup(struct net *net, 3208c2ecf20Sopenharmony_ci const struct in6_addr *saddr, __be16 sport, 3218c2ecf20Sopenharmony_ci const struct in6_addr *daddr, __be16 dport, 3228c2ecf20Sopenharmony_ci int dif); 3238c2ecf20Sopenharmony_cistruct sock *__udp6_lib_lookup(struct net *net, 3248c2ecf20Sopenharmony_ci const struct in6_addr *saddr, __be16 sport, 3258c2ecf20Sopenharmony_ci const struct in6_addr *daddr, __be16 dport, 3268c2ecf20Sopenharmony_ci int dif, int sdif, struct udp_table *tbl, 3278c2ecf20Sopenharmony_ci struct sk_buff *skb); 3288c2ecf20Sopenharmony_cistruct sock *udp6_lib_lookup_skb(struct sk_buff *skb, 3298c2ecf20Sopenharmony_ci __be16 sport, __be16 dport); 3308c2ecf20Sopenharmony_ci 3318c2ecf20Sopenharmony_ci/* UDP uses skb->dev_scratch to cache as much information as possible and avoid 3328c2ecf20Sopenharmony_ci * possibly multiple cache miss on dequeue() 3338c2ecf20Sopenharmony_ci */ 3348c2ecf20Sopenharmony_cistruct udp_dev_scratch { 3358c2ecf20Sopenharmony_ci /* skb->truesize and the stateless bit are embedded in a single field; 3368c2ecf20Sopenharmony_ci * do not use a bitfield since the compiler emits better/smaller code 3378c2ecf20Sopenharmony_ci * this way 3388c2ecf20Sopenharmony_ci */ 3398c2ecf20Sopenharmony_ci u32 _tsize_state; 3408c2ecf20Sopenharmony_ci 3418c2ecf20Sopenharmony_ci#if BITS_PER_LONG == 64 3428c2ecf20Sopenharmony_ci /* len and the bit needed to compute skb_csum_unnecessary 3438c2ecf20Sopenharmony_ci * will be on cold cache lines at recvmsg time. 3448c2ecf20Sopenharmony_ci * skb->len can be stored on 16 bits since the udp header has been 3458c2ecf20Sopenharmony_ci * already validated and pulled. 3468c2ecf20Sopenharmony_ci */ 3478c2ecf20Sopenharmony_ci u16 len; 3488c2ecf20Sopenharmony_ci bool is_linear; 3498c2ecf20Sopenharmony_ci bool csum_unnecessary; 3508c2ecf20Sopenharmony_ci#endif 3518c2ecf20Sopenharmony_ci}; 3528c2ecf20Sopenharmony_ci 3538c2ecf20Sopenharmony_cistatic inline struct udp_dev_scratch *udp_skb_scratch(struct sk_buff *skb) 3548c2ecf20Sopenharmony_ci{ 3558c2ecf20Sopenharmony_ci return (struct udp_dev_scratch *)&skb->dev_scratch; 3568c2ecf20Sopenharmony_ci} 3578c2ecf20Sopenharmony_ci 3588c2ecf20Sopenharmony_ci#if BITS_PER_LONG == 64 3598c2ecf20Sopenharmony_cistatic inline unsigned int udp_skb_len(struct sk_buff *skb) 3608c2ecf20Sopenharmony_ci{ 3618c2ecf20Sopenharmony_ci return udp_skb_scratch(skb)->len; 3628c2ecf20Sopenharmony_ci} 3638c2ecf20Sopenharmony_ci 3648c2ecf20Sopenharmony_cistatic inline bool udp_skb_csum_unnecessary(struct sk_buff *skb) 3658c2ecf20Sopenharmony_ci{ 3668c2ecf20Sopenharmony_ci return udp_skb_scratch(skb)->csum_unnecessary; 3678c2ecf20Sopenharmony_ci} 3688c2ecf20Sopenharmony_ci 3698c2ecf20Sopenharmony_cistatic inline bool udp_skb_is_linear(struct sk_buff *skb) 3708c2ecf20Sopenharmony_ci{ 3718c2ecf20Sopenharmony_ci return udp_skb_scratch(skb)->is_linear; 3728c2ecf20Sopenharmony_ci} 3738c2ecf20Sopenharmony_ci 3748c2ecf20Sopenharmony_ci#else 3758c2ecf20Sopenharmony_cistatic inline unsigned int udp_skb_len(struct sk_buff *skb) 3768c2ecf20Sopenharmony_ci{ 3778c2ecf20Sopenharmony_ci return skb->len; 3788c2ecf20Sopenharmony_ci} 3798c2ecf20Sopenharmony_ci 3808c2ecf20Sopenharmony_cistatic inline bool udp_skb_csum_unnecessary(struct sk_buff *skb) 3818c2ecf20Sopenharmony_ci{ 3828c2ecf20Sopenharmony_ci return skb_csum_unnecessary(skb); 3838c2ecf20Sopenharmony_ci} 3848c2ecf20Sopenharmony_ci 3858c2ecf20Sopenharmony_cistatic inline bool udp_skb_is_linear(struct sk_buff *skb) 3868c2ecf20Sopenharmony_ci{ 3878c2ecf20Sopenharmony_ci return !skb_is_nonlinear(skb); 3888c2ecf20Sopenharmony_ci} 3898c2ecf20Sopenharmony_ci#endif 3908c2ecf20Sopenharmony_ci 3918c2ecf20Sopenharmony_cistatic inline int copy_linear_skb(struct sk_buff *skb, int len, int off, 3928c2ecf20Sopenharmony_ci struct iov_iter *to) 3938c2ecf20Sopenharmony_ci{ 3948c2ecf20Sopenharmony_ci int n; 3958c2ecf20Sopenharmony_ci 3968c2ecf20Sopenharmony_ci n = copy_to_iter(skb->data + off, len, to); 3978c2ecf20Sopenharmony_ci if (n == len) 3988c2ecf20Sopenharmony_ci return 0; 3998c2ecf20Sopenharmony_ci 4008c2ecf20Sopenharmony_ci iov_iter_revert(to, n); 4018c2ecf20Sopenharmony_ci return -EFAULT; 4028c2ecf20Sopenharmony_ci} 4038c2ecf20Sopenharmony_ci 4048c2ecf20Sopenharmony_ci/* 4058c2ecf20Sopenharmony_ci * SNMP statistics for UDP and UDP-Lite 4068c2ecf20Sopenharmony_ci */ 4078c2ecf20Sopenharmony_ci#define UDP_INC_STATS(net, field, is_udplite) do { \ 4088c2ecf20Sopenharmony_ci if (is_udplite) SNMP_INC_STATS((net)->mib.udplite_statistics, field); \ 4098c2ecf20Sopenharmony_ci else SNMP_INC_STATS((net)->mib.udp_statistics, field); } while(0) 4108c2ecf20Sopenharmony_ci#define __UDP_INC_STATS(net, field, is_udplite) do { \ 4118c2ecf20Sopenharmony_ci if (is_udplite) __SNMP_INC_STATS((net)->mib.udplite_statistics, field); \ 4128c2ecf20Sopenharmony_ci else __SNMP_INC_STATS((net)->mib.udp_statistics, field); } while(0) 4138c2ecf20Sopenharmony_ci 4148c2ecf20Sopenharmony_ci#define __UDP6_INC_STATS(net, field, is_udplite) do { \ 4158c2ecf20Sopenharmony_ci if (is_udplite) __SNMP_INC_STATS((net)->mib.udplite_stats_in6, field);\ 4168c2ecf20Sopenharmony_ci else __SNMP_INC_STATS((net)->mib.udp_stats_in6, field); \ 4178c2ecf20Sopenharmony_ci} while(0) 4188c2ecf20Sopenharmony_ci#define UDP6_INC_STATS(net, field, __lite) do { \ 4198c2ecf20Sopenharmony_ci if (__lite) SNMP_INC_STATS((net)->mib.udplite_stats_in6, field); \ 4208c2ecf20Sopenharmony_ci else SNMP_INC_STATS((net)->mib.udp_stats_in6, field); \ 4218c2ecf20Sopenharmony_ci} while(0) 4228c2ecf20Sopenharmony_ci 4238c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_IPV6) 4248c2ecf20Sopenharmony_ci#define __UDPX_MIB(sk, ipv4) \ 4258c2ecf20Sopenharmony_ci({ \ 4268c2ecf20Sopenharmony_ci ipv4 ? (IS_UDPLITE(sk) ? sock_net(sk)->mib.udplite_statistics : \ 4278c2ecf20Sopenharmony_ci sock_net(sk)->mib.udp_statistics) : \ 4288c2ecf20Sopenharmony_ci (IS_UDPLITE(sk) ? sock_net(sk)->mib.udplite_stats_in6 : \ 4298c2ecf20Sopenharmony_ci sock_net(sk)->mib.udp_stats_in6); \ 4308c2ecf20Sopenharmony_ci}) 4318c2ecf20Sopenharmony_ci#else 4328c2ecf20Sopenharmony_ci#define __UDPX_MIB(sk, ipv4) \ 4338c2ecf20Sopenharmony_ci({ \ 4348c2ecf20Sopenharmony_ci IS_UDPLITE(sk) ? sock_net(sk)->mib.udplite_statistics : \ 4358c2ecf20Sopenharmony_ci sock_net(sk)->mib.udp_statistics; \ 4368c2ecf20Sopenharmony_ci}) 4378c2ecf20Sopenharmony_ci#endif 4388c2ecf20Sopenharmony_ci 4398c2ecf20Sopenharmony_ci#define __UDPX_INC_STATS(sk, field) \ 4408c2ecf20Sopenharmony_ci __SNMP_INC_STATS(__UDPX_MIB(sk, (sk)->sk_family == AF_INET), field) 4418c2ecf20Sopenharmony_ci 4428c2ecf20Sopenharmony_ci#ifdef CONFIG_PROC_FS 4438c2ecf20Sopenharmony_cistruct udp_seq_afinfo { 4448c2ecf20Sopenharmony_ci sa_family_t family; 4458c2ecf20Sopenharmony_ci struct udp_table *udp_table; 4468c2ecf20Sopenharmony_ci}; 4478c2ecf20Sopenharmony_ci 4488c2ecf20Sopenharmony_cistruct udp_iter_state { 4498c2ecf20Sopenharmony_ci struct seq_net_private p; 4508c2ecf20Sopenharmony_ci int bucket; 4518c2ecf20Sopenharmony_ci struct udp_seq_afinfo *bpf_seq_afinfo; 4528c2ecf20Sopenharmony_ci}; 4538c2ecf20Sopenharmony_ci 4548c2ecf20Sopenharmony_civoid *udp_seq_start(struct seq_file *seq, loff_t *pos); 4558c2ecf20Sopenharmony_civoid *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos); 4568c2ecf20Sopenharmony_civoid udp_seq_stop(struct seq_file *seq, void *v); 4578c2ecf20Sopenharmony_ci 4588c2ecf20Sopenharmony_ciextern const struct seq_operations udp_seq_ops; 4598c2ecf20Sopenharmony_ciextern const struct seq_operations udp6_seq_ops; 4608c2ecf20Sopenharmony_ci 4618c2ecf20Sopenharmony_ciint udp4_proc_init(void); 4628c2ecf20Sopenharmony_civoid udp4_proc_exit(void); 4638c2ecf20Sopenharmony_ci#endif /* CONFIG_PROC_FS */ 4648c2ecf20Sopenharmony_ci 4658c2ecf20Sopenharmony_ciint udpv4_offload_init(void); 4668c2ecf20Sopenharmony_ci 4678c2ecf20Sopenharmony_civoid udp_init(void); 4688c2ecf20Sopenharmony_ci 4698c2ecf20Sopenharmony_ciDECLARE_STATIC_KEY_FALSE(udp_encap_needed_key); 4708c2ecf20Sopenharmony_civoid udp_encap_enable(void); 4718c2ecf20Sopenharmony_civoid udp_encap_disable(void); 4728c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_IPV6) 4738c2ecf20Sopenharmony_ciDECLARE_STATIC_KEY_FALSE(udpv6_encap_needed_key); 4748c2ecf20Sopenharmony_civoid udpv6_encap_enable(void); 4758c2ecf20Sopenharmony_ci#endif 4768c2ecf20Sopenharmony_ci 4778c2ecf20Sopenharmony_cistatic inline struct sk_buff *udp_rcv_segment(struct sock *sk, 4788c2ecf20Sopenharmony_ci struct sk_buff *skb, bool ipv4) 4798c2ecf20Sopenharmony_ci{ 4808c2ecf20Sopenharmony_ci netdev_features_t features = NETIF_F_SG; 4818c2ecf20Sopenharmony_ci struct sk_buff *segs; 4828c2ecf20Sopenharmony_ci 4838c2ecf20Sopenharmony_ci /* Avoid csum recalculation by skb_segment unless userspace explicitly 4848c2ecf20Sopenharmony_ci * asks for the final checksum values 4858c2ecf20Sopenharmony_ci */ 4868c2ecf20Sopenharmony_ci if (!inet_get_convert_csum(sk)) 4878c2ecf20Sopenharmony_ci features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM; 4888c2ecf20Sopenharmony_ci 4898c2ecf20Sopenharmony_ci /* UDP segmentation expects packets of type CHECKSUM_PARTIAL or 4908c2ecf20Sopenharmony_ci * CHECKSUM_NONE in __udp_gso_segment. UDP GRO indeed builds partial 4918c2ecf20Sopenharmony_ci * packets in udp_gro_complete_segment. As does UDP GSO, verified by 4928c2ecf20Sopenharmony_ci * udp_send_skb. But when those packets are looped in dev_loopback_xmit 4938c2ecf20Sopenharmony_ci * their ip_summed CHECKSUM_NONE is changed to CHECKSUM_UNNECESSARY. 4948c2ecf20Sopenharmony_ci * Reset in this specific case, where PARTIAL is both correct and 4958c2ecf20Sopenharmony_ci * required. 4968c2ecf20Sopenharmony_ci */ 4978c2ecf20Sopenharmony_ci if (skb->pkt_type == PACKET_LOOPBACK) 4988c2ecf20Sopenharmony_ci skb->ip_summed = CHECKSUM_PARTIAL; 4998c2ecf20Sopenharmony_ci 5008c2ecf20Sopenharmony_ci /* the GSO CB lays after the UDP one, no need to save and restore any 5018c2ecf20Sopenharmony_ci * CB fragment 5028c2ecf20Sopenharmony_ci */ 5038c2ecf20Sopenharmony_ci segs = __skb_gso_segment(skb, features, false); 5048c2ecf20Sopenharmony_ci if (IS_ERR_OR_NULL(segs)) { 5058c2ecf20Sopenharmony_ci int segs_nr = skb_shinfo(skb)->gso_segs; 5068c2ecf20Sopenharmony_ci 5078c2ecf20Sopenharmony_ci atomic_add(segs_nr, &sk->sk_drops); 5088c2ecf20Sopenharmony_ci SNMP_ADD_STATS(__UDPX_MIB(sk, ipv4), UDP_MIB_INERRORS, segs_nr); 5098c2ecf20Sopenharmony_ci kfree_skb(skb); 5108c2ecf20Sopenharmony_ci return NULL; 5118c2ecf20Sopenharmony_ci } 5128c2ecf20Sopenharmony_ci 5138c2ecf20Sopenharmony_ci consume_skb(skb); 5148c2ecf20Sopenharmony_ci return segs; 5158c2ecf20Sopenharmony_ci} 5168c2ecf20Sopenharmony_ci 5178c2ecf20Sopenharmony_ci#ifdef CONFIG_BPF_STREAM_PARSER 5188c2ecf20Sopenharmony_cistruct sk_psock; 5198c2ecf20Sopenharmony_cistruct proto *udp_bpf_get_proto(struct sock *sk, struct sk_psock *psock); 5208c2ecf20Sopenharmony_ci#endif /* BPF_STREAM_PARSER */ 5218c2ecf20Sopenharmony_ci 5228c2ecf20Sopenharmony_ci#endif /* _UDP_H */ 523