Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 /*
0003  * NET      An implementation of the SOCKET network access protocol.
0004  *      This is the master header file for the Linux NET layer,
0005  *      or, in plain English: the networking handling part of the
0006  *      kernel.
0007  *
0008  * Version: @(#)net.h   1.0.3   05/25/93
0009  *
0010  * Authors: Orest Zborowski, <obz@Kodak.COM>
0011  *      Ross Biro
0012  *      Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
0013  */
0014 #ifndef _LINUX_NET_H
0015 #define _LINUX_NET_H
0016 
0017 #include <linux/stringify.h>
0018 #include <linux/random.h>
0019 #include <linux/wait.h>
0020 #include <linux/fcntl.h>    /* For O_CLOEXEC and O_NONBLOCK */
0021 #include <linux/rcupdate.h>
0022 #include <linux/once.h>
0023 #include <linux/fs.h>
0024 #include <linux/mm.h>
0025 #include <linux/sockptr.h>
0026 
0027 #include <uapi/linux/net.h>
0028 
0029 struct poll_table_struct;
0030 struct pipe_inode_info;
0031 struct inode;
0032 struct file;
0033 struct net;
0034 
0035 /* Historically, SOCKWQ_ASYNC_NOSPACE & SOCKWQ_ASYNC_WAITDATA were located
0036  * in sock->flags, but moved into sk->sk_wq->flags to be RCU protected.
0037  * Eventually all flags will be in sk->sk_wq->flags.
0038  */
0039 #define SOCKWQ_ASYNC_NOSPACE    0
0040 #define SOCKWQ_ASYNC_WAITDATA   1
0041 #define SOCK_NOSPACE        2
0042 #define SOCK_PASSCRED       3
0043 #define SOCK_PASSSEC        4
0044 
0045 #ifndef ARCH_HAS_SOCKET_TYPES
0046 /**
0047  * enum sock_type - Socket types
0048  * @SOCK_STREAM: stream (connection) socket
0049  * @SOCK_DGRAM: datagram (conn.less) socket
0050  * @SOCK_RAW: raw socket
0051  * @SOCK_RDM: reliably-delivered message
0052  * @SOCK_SEQPACKET: sequential packet socket
0053  * @SOCK_DCCP: Datagram Congestion Control Protocol socket
0054  * @SOCK_PACKET: linux specific way of getting packets at the dev level.
0055  *        For writing rarp and other similar things on the user level.
0056  *
0057  * When adding some new socket type please
0058  * grep ARCH_HAS_SOCKET_TYPE include/asm-* /socket.h, at least MIPS
0059  * overrides this enum for binary compat reasons.
0060  */
0061 enum sock_type {
0062     SOCK_STREAM = 1,
0063     SOCK_DGRAM  = 2,
0064     SOCK_RAW    = 3,
0065     SOCK_RDM    = 4,
0066     SOCK_SEQPACKET  = 5,
0067     SOCK_DCCP   = 6,
0068     SOCK_PACKET = 10,
0069 };
0070 
0071 #define SOCK_MAX (SOCK_PACKET + 1)
0072 /* Mask which covers at least up to SOCK_MASK-1.  The
0073  * remaining bits are used as flags. */
0074 #define SOCK_TYPE_MASK 0xf
0075 
0076 /* Flags for socket, socketpair, accept4 */
0077 #define SOCK_CLOEXEC    O_CLOEXEC
0078 #ifndef SOCK_NONBLOCK
0079 #define SOCK_NONBLOCK   O_NONBLOCK
0080 #endif
0081 
0082 #endif /* ARCH_HAS_SOCKET_TYPES */
0083 
0084 /**
0085  * enum sock_shutdown_cmd - Shutdown types
0086  * @SHUT_RD: shutdown receptions
0087  * @SHUT_WR: shutdown transmissions
0088  * @SHUT_RDWR: shutdown receptions/transmissions
0089  */
0090 enum sock_shutdown_cmd {
0091     SHUT_RD,
0092     SHUT_WR,
0093     SHUT_RDWR,
0094 };
0095 
0096 struct socket_wq {
0097     /* Note: wait MUST be first field of socket_wq */
0098     wait_queue_head_t   wait;
0099     struct fasync_struct    *fasync_list;
0100     unsigned long       flags; /* %SOCKWQ_ASYNC_NOSPACE, etc */
0101     struct rcu_head     rcu;
0102 } ____cacheline_aligned_in_smp;
0103 
0104 /**
0105  *  struct socket - general BSD socket
0106  *  @state: socket state (%SS_CONNECTED, etc)
0107  *  @type: socket type (%SOCK_STREAM, etc)
0108  *  @flags: socket flags (%SOCK_NOSPACE, etc)
0109  *  @ops: protocol specific socket operations
0110  *  @file: File back pointer for gc
0111  *  @sk: internal networking protocol agnostic socket representation
0112  *  @wq: wait queue for several uses
0113  */
0114 struct socket {
0115     socket_state        state;
0116 
0117     short           type;
0118 
0119     unsigned long       flags;
0120 
0121     struct file     *file;
0122     struct sock     *sk;
0123     const struct proto_ops  *ops;
0124 
0125     struct socket_wq    wq;
0126 };
0127 
0128 /*
0129  * "descriptor" for what we're up to with a read.
0130  * This allows us to use the same read code yet
0131  * have multiple different users of the data that
0132  * we read from a file.
0133  *
0134  * The simplest case just copies the data to user
0135  * mode.
0136  */
0137 typedef struct {
0138     size_t written;
0139     size_t count;
0140     union {
0141         char __user *buf;
0142         void *data;
0143     } arg;
0144     int error;
0145 } read_descriptor_t;
0146 
0147 struct vm_area_struct;
0148 struct page;
0149 struct sockaddr;
0150 struct msghdr;
0151 struct module;
0152 struct sk_buff;
0153 typedef int (*sk_read_actor_t)(read_descriptor_t *, struct sk_buff *,
0154                    unsigned int, size_t);
0155 typedef int (*skb_read_actor_t)(struct sock *, struct sk_buff *);
0156 
0157 
0158 struct proto_ops {
0159     int     family;
0160     struct module   *owner;
0161     int     (*release)   (struct socket *sock);
0162     int     (*bind)      (struct socket *sock,
0163                       struct sockaddr *myaddr,
0164                       int sockaddr_len);
0165     int     (*connect)   (struct socket *sock,
0166                       struct sockaddr *vaddr,
0167                       int sockaddr_len, int flags);
0168     int     (*socketpair)(struct socket *sock1,
0169                       struct socket *sock2);
0170     int     (*accept)    (struct socket *sock,
0171                       struct socket *newsock, int flags, bool kern);
0172     int     (*getname)   (struct socket *sock,
0173                       struct sockaddr *addr,
0174                       int peer);
0175     __poll_t    (*poll)      (struct file *file, struct socket *sock,
0176                       struct poll_table_struct *wait);
0177     int     (*ioctl)     (struct socket *sock, unsigned int cmd,
0178                       unsigned long arg);
0179 #ifdef CONFIG_COMPAT
0180     int     (*compat_ioctl) (struct socket *sock, unsigned int cmd,
0181                       unsigned long arg);
0182 #endif
0183     int     (*gettstamp) (struct socket *sock, void __user *userstamp,
0184                       bool timeval, bool time32);
0185     int     (*listen)    (struct socket *sock, int len);
0186     int     (*shutdown)  (struct socket *sock, int flags);
0187     int     (*setsockopt)(struct socket *sock, int level,
0188                       int optname, sockptr_t optval,
0189                       unsigned int optlen);
0190     int     (*getsockopt)(struct socket *sock, int level,
0191                       int optname, char __user *optval, int __user *optlen);
0192     void        (*show_fdinfo)(struct seq_file *m, struct socket *sock);
0193     int     (*sendmsg)   (struct socket *sock, struct msghdr *m,
0194                       size_t total_len);
0195     /* Notes for implementing recvmsg:
0196      * ===============================
0197      * msg->msg_namelen should get updated by the recvmsg handlers
0198      * iff msg_name != NULL. It is by default 0 to prevent
0199      * returning uninitialized memory to user space.  The recvfrom
0200      * handlers can assume that msg.msg_name is either NULL or has
0201      * a minimum size of sizeof(struct sockaddr_storage).
0202      */
0203     int     (*recvmsg)   (struct socket *sock, struct msghdr *m,
0204                       size_t total_len, int flags);
0205     int     (*mmap)      (struct file *file, struct socket *sock,
0206                       struct vm_area_struct * vma);
0207     ssize_t     (*sendpage)  (struct socket *sock, struct page *page,
0208                       int offset, size_t size, int flags);
0209     ssize_t     (*splice_read)(struct socket *sock,  loff_t *ppos,
0210                        struct pipe_inode_info *pipe, size_t len, unsigned int flags);
0211     int     (*set_peek_off)(struct sock *sk, int val);
0212     int     (*peek_len)(struct socket *sock);
0213 
0214     /* The following functions are called internally by kernel with
0215      * sock lock already held.
0216      */
0217     int     (*read_sock)(struct sock *sk, read_descriptor_t *desc,
0218                      sk_read_actor_t recv_actor);
0219     /* This is different from read_sock(), it reads an entire skb at a time. */
0220     int     (*read_skb)(struct sock *sk, skb_read_actor_t recv_actor);
0221     int     (*sendpage_locked)(struct sock *sk, struct page *page,
0222                        int offset, size_t size, int flags);
0223     int     (*sendmsg_locked)(struct sock *sk, struct msghdr *msg,
0224                       size_t size);
0225     int     (*set_rcvlowat)(struct sock *sk, int val);
0226 };
0227 
0228 #define DECLARE_SOCKADDR(type, dst, src)    \
0229     type dst = ({ __sockaddr_check_size(sizeof(*dst)); (type) src; })
0230 
0231 struct net_proto_family {
0232     int     family;
0233     int     (*create)(struct net *net, struct socket *sock,
0234                   int protocol, int kern);
0235     struct module   *owner;
0236 };
0237 
0238 struct iovec;
0239 struct kvec;
0240 
0241 enum {
0242     SOCK_WAKE_IO,
0243     SOCK_WAKE_WAITD,
0244     SOCK_WAKE_SPACE,
0245     SOCK_WAKE_URG,
0246 };
0247 
0248 int sock_wake_async(struct socket_wq *sk_wq, int how, int band);
0249 int sock_register(const struct net_proto_family *fam);
0250 void sock_unregister(int family);
0251 bool sock_is_registered(int family);
0252 int __sock_create(struct net *net, int family, int type, int proto,
0253           struct socket **res, int kern);
0254 int sock_create(int family, int type, int proto, struct socket **res);
0255 int sock_create_kern(struct net *net, int family, int type, int proto, struct socket **res);
0256 int sock_create_lite(int family, int type, int proto, struct socket **res);
0257 struct socket *sock_alloc(void);
0258 void sock_release(struct socket *sock);
0259 int sock_sendmsg(struct socket *sock, struct msghdr *msg);
0260 int sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags);
0261 struct file *sock_alloc_file(struct socket *sock, int flags, const char *dname);
0262 struct socket *sockfd_lookup(int fd, int *err);
0263 struct socket *sock_from_file(struct file *file);
0264 #define          sockfd_put(sock) fput(sock->file)
0265 int net_ratelimit(void);
0266 
0267 #define net_ratelimited_function(function, ...)         \
0268 do {                                \
0269     if (net_ratelimit())                    \
0270         function(__VA_ARGS__);              \
0271 } while (0)
0272 
0273 #define net_emerg_ratelimited(fmt, ...)             \
0274     net_ratelimited_function(pr_emerg, fmt, ##__VA_ARGS__)
0275 #define net_alert_ratelimited(fmt, ...)             \
0276     net_ratelimited_function(pr_alert, fmt, ##__VA_ARGS__)
0277 #define net_crit_ratelimited(fmt, ...)              \
0278     net_ratelimited_function(pr_crit, fmt, ##__VA_ARGS__)
0279 #define net_err_ratelimited(fmt, ...)               \
0280     net_ratelimited_function(pr_err, fmt, ##__VA_ARGS__)
0281 #define net_notice_ratelimited(fmt, ...)            \
0282     net_ratelimited_function(pr_notice, fmt, ##__VA_ARGS__)
0283 #define net_warn_ratelimited(fmt, ...)              \
0284     net_ratelimited_function(pr_warn, fmt, ##__VA_ARGS__)
0285 #define net_info_ratelimited(fmt, ...)              \
0286     net_ratelimited_function(pr_info, fmt, ##__VA_ARGS__)
0287 #if defined(CONFIG_DYNAMIC_DEBUG) || \
0288     (defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
0289 #define net_dbg_ratelimited(fmt, ...)                   \
0290 do {                                    \
0291     DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, fmt);         \
0292     if (DYNAMIC_DEBUG_BRANCH(descriptor) &&             \
0293         net_ratelimit())                        \
0294         __dynamic_pr_debug(&descriptor, pr_fmt(fmt),        \
0295                            ##__VA_ARGS__);          \
0296 } while (0)
0297 #elif defined(DEBUG)
0298 #define net_dbg_ratelimited(fmt, ...)               \
0299     net_ratelimited_function(pr_debug, fmt, ##__VA_ARGS__)
0300 #else
0301 #define net_dbg_ratelimited(fmt, ...)               \
0302     do {                            \
0303         if (0)                      \
0304             no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); \
0305     } while (0)
0306 #endif
0307 
0308 #define net_get_random_once(buf, nbytes)            \
0309     get_random_once((buf), (nbytes))
0310 
0311 /*
0312  * E.g. XFS meta- & log-data is in slab pages, or bcache meta
0313  * data pages, or other high order pages allocated by
0314  * __get_free_pages() without __GFP_COMP, which have a page_count
0315  * of 0 and/or have PageSlab() set. We cannot use send_page for
0316  * those, as that does get_page(); put_page(); and would cause
0317  * either a VM_BUG directly, or __page_cache_release a page that
0318  * would actually still be referenced by someone, leading to some
0319  * obscure delayed Oops somewhere else.
0320  */
0321 static inline bool sendpage_ok(struct page *page)
0322 {
0323     return !PageSlab(page) && page_count(page) >= 1;
0324 }
0325 
0326 int kernel_sendmsg(struct socket *sock, struct msghdr *msg, struct kvec *vec,
0327            size_t num, size_t len);
0328 int kernel_sendmsg_locked(struct sock *sk, struct msghdr *msg,
0329               struct kvec *vec, size_t num, size_t len);
0330 int kernel_recvmsg(struct socket *sock, struct msghdr *msg, struct kvec *vec,
0331            size_t num, size_t len, int flags);
0332 
0333 int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen);
0334 int kernel_listen(struct socket *sock, int backlog);
0335 int kernel_accept(struct socket *sock, struct socket **newsock, int flags);
0336 int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
0337            int flags);
0338 int kernel_getsockname(struct socket *sock, struct sockaddr *addr);
0339 int kernel_getpeername(struct socket *sock, struct sockaddr *addr);
0340 int kernel_sendpage(struct socket *sock, struct page *page, int offset,
0341             size_t size, int flags);
0342 int kernel_sendpage_locked(struct sock *sk, struct page *page, int offset,
0343                size_t size, int flags);
0344 int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how);
0345 
0346 /* Routine returns the IP overhead imposed by a (caller-protected) socket. */
0347 u32 kernel_sock_ip_overhead(struct sock *sk);
0348 
0349 #define MODULE_ALIAS_NETPROTO(proto) \
0350     MODULE_ALIAS("net-pf-" __stringify(proto))
0351 
0352 #define MODULE_ALIAS_NET_PF_PROTO(pf, proto) \
0353     MODULE_ALIAS("net-pf-" __stringify(pf) "-proto-" __stringify(proto))
0354 
0355 #define MODULE_ALIAS_NET_PF_PROTO_TYPE(pf, proto, type) \
0356     MODULE_ALIAS("net-pf-" __stringify(pf) "-proto-" __stringify(proto) \
0357              "-type-" __stringify(type))
0358 
0359 #define MODULE_ALIAS_NET_PF_PROTO_NAME(pf, proto, name) \
0360     MODULE_ALIAS("net-pf-" __stringify(pf) "-proto-" __stringify(proto) \
0361              name)
0362 #endif  /* _LINUX_NET_H */