Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 /*
0003  * INET     An implementation of the TCP/IP protocol suite for the LINUX
0004  *      operating system.  INET is implemented using the  BSD Socket
0005  *      interface as the means of communication with the user level.
0006  *
0007  *      Definitions for the UDP protocol.
0008  *
0009  * Version: @(#)udp.h   1.0.2   04/28/93
0010  *
0011  * Author:  Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
0012  */
0013 #ifndef _LINUX_UDP_H
0014 #define _LINUX_UDP_H
0015 
0016 #include <net/inet_sock.h>
0017 #include <linux/skbuff.h>
0018 #include <net/netns/hash.h>
0019 #include <uapi/linux/udp.h>
0020 
0021 static inline struct udphdr *udp_hdr(const struct sk_buff *skb)
0022 {
0023     return (struct udphdr *)skb_transport_header(skb);
0024 }
0025 
0026 #define UDP_HTABLE_SIZE_MIN     (CONFIG_BASE_SMALL ? 128 : 256)
0027 
0028 static inline u32 udp_hashfn(const struct net *net, u32 num, u32 mask)
0029 {
0030     return (num + net_hash_mix(net)) & mask;
0031 }
0032 
0033 struct udp_sock {
0034     /* inet_sock has to be the first member */
0035     struct inet_sock inet;
0036 #define udp_port_hash       inet.sk.__sk_common.skc_u16hashes[0]
0037 #define udp_portaddr_hash   inet.sk.__sk_common.skc_u16hashes[1]
0038 #define udp_portaddr_node   inet.sk.__sk_common.skc_portaddr_node
0039     int      pending;   /* Any pending frames ? */
0040     unsigned int     corkflag;  /* Cork is required */
0041     __u8         encap_type;    /* Is this an Encapsulation socket? */
0042     unsigned char    no_check6_tx:1,/* Send zero UDP6 checksums on TX? */
0043              no_check6_rx:1,/* Allow zero UDP6 checksums on RX? */
0044              encap_enabled:1, /* This socket enabled encap
0045                        * processing; UDP tunnels and
0046                        * different encapsulation layer set
0047                        * this
0048                        */
0049              gro_enabled:1, /* Request GRO aggregation */
0050              accept_udp_l4:1,
0051              accept_udp_fraglist:1;
0052     /*
0053      * Following member retains the information to create a UDP header
0054      * when the socket is uncorked.
0055      */
0056     __u16        len;       /* total length of pending frames */
0057     __u16        gso_size;
0058     /*
0059      * Fields specific to UDP-Lite.
0060      */
0061     __u16        pcslen;
0062     __u16        pcrlen;
0063 /* indicator bits used by pcflag: */
0064 #define UDPLITE_BIT      0x1        /* set by udplite proto init function */
0065 #define UDPLITE_SEND_CC  0x2        /* set via udplite setsockopt         */
0066 #define UDPLITE_RECV_CC  0x4        /* set via udplite setsocktopt        */
0067     __u8         pcflag;        /* marks socket as UDP-Lite if > 0    */
0068     __u8         unused[3];
0069     /*
0070      * For encapsulation sockets.
0071      */
0072     int (*encap_rcv)(struct sock *sk, struct sk_buff *skb);
0073     void (*encap_err_rcv)(struct sock *sk, struct sk_buff *skb, unsigned int udp_offset);
0074     int (*encap_err_lookup)(struct sock *sk, struct sk_buff *skb);
0075     void (*encap_destroy)(struct sock *sk);
0076 
0077     /* GRO functions for UDP socket */
0078     struct sk_buff *    (*gro_receive)(struct sock *sk,
0079                            struct list_head *head,
0080                            struct sk_buff *skb);
0081     int         (*gro_complete)(struct sock *sk,
0082                         struct sk_buff *skb,
0083                         int nhoff);
0084 
0085     /* udp_recvmsg try to use this before splicing sk_receive_queue */
0086     struct sk_buff_head reader_queue ____cacheline_aligned_in_smp;
0087 
0088     /* This field is dirtied by udp_recvmsg() */
0089     int     forward_deficit;
0090 };
0091 
0092 #define UDP_MAX_SEGMENTS    (1 << 6UL)
0093 
0094 static inline struct udp_sock *udp_sk(const struct sock *sk)
0095 {
0096     return (struct udp_sock *)sk;
0097 }
0098 
0099 static inline void udp_set_no_check6_tx(struct sock *sk, bool val)
0100 {
0101     udp_sk(sk)->no_check6_tx = val;
0102 }
0103 
0104 static inline void udp_set_no_check6_rx(struct sock *sk, bool val)
0105 {
0106     udp_sk(sk)->no_check6_rx = val;
0107 }
0108 
0109 static inline bool udp_get_no_check6_tx(struct sock *sk)
0110 {
0111     return udp_sk(sk)->no_check6_tx;
0112 }
0113 
0114 static inline bool udp_get_no_check6_rx(struct sock *sk)
0115 {
0116     return udp_sk(sk)->no_check6_rx;
0117 }
0118 
0119 static inline void udp_cmsg_recv(struct msghdr *msg, struct sock *sk,
0120                  struct sk_buff *skb)
0121 {
0122     int gso_size;
0123 
0124     if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) {
0125         gso_size = skb_shinfo(skb)->gso_size;
0126         put_cmsg(msg, SOL_UDP, UDP_GRO, sizeof(gso_size), &gso_size);
0127     }
0128 }
0129 
0130 static inline bool udp_unexpected_gso(struct sock *sk, struct sk_buff *skb)
0131 {
0132     if (!skb_is_gso(skb))
0133         return false;
0134 
0135     if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4 && !udp_sk(sk)->accept_udp_l4)
0136         return true;
0137 
0138     if (skb_shinfo(skb)->gso_type & SKB_GSO_FRAGLIST && !udp_sk(sk)->accept_udp_fraglist)
0139         return true;
0140 
0141     return false;
0142 }
0143 
0144 static inline void udp_allow_gso(struct sock *sk)
0145 {
0146     udp_sk(sk)->accept_udp_l4 = 1;
0147     udp_sk(sk)->accept_udp_fraglist = 1;
0148 }
0149 
0150 #define udp_portaddr_for_each_entry(__sk, list) \
0151     hlist_for_each_entry(__sk, list, __sk_common.skc_portaddr_node)
0152 
0153 #define udp_portaddr_for_each_entry_rcu(__sk, list) \
0154     hlist_for_each_entry_rcu(__sk, list, __sk_common.skc_portaddr_node)
0155 
0156 #define IS_UDPLITE(__sk) (__sk->sk_protocol == IPPROTO_UDPLITE)
0157 
0158 #endif  /* _LINUX_UDP_H */