377 lines · c
1/* SPDX-License-Identifier: GPL-2.0-or-later */2/*3 * NET An implementation of the SOCKET network access protocol.4 * This is the master header file for the Linux NET layer,5 * or, in plain English: the networking handling part of the6 * kernel.7 *8 * Version: @(#)net.h 1.0.3 05/25/939 *10 * Authors: Orest Zborowski, <obz@Kodak.COM>11 * Ross Biro12 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>13 */14#ifndef _LINUX_NET_H15#define _LINUX_NET_H16 17#include <linux/stringify.h>18#include <linux/random.h>19#include <linux/wait.h>20#include <linux/fcntl.h> /* For O_CLOEXEC and O_NONBLOCK */21#include <linux/rcupdate.h>22#include <linux/once.h>23#include <linux/fs.h>24#include <linux/mm.h>25#include <linux/sockptr.h>26 27#include <uapi/linux/net.h>28 29struct poll_table_struct;30struct pipe_inode_info;31struct inode;32struct file;33struct net;34 35/* Historically, SOCKWQ_ASYNC_NOSPACE & SOCKWQ_ASYNC_WAITDATA were located36 * in sock->flags, but moved into sk->sk_wq->flags to be RCU protected.37 * Eventually all flags will be in sk->sk_wq->flags.38 */39#define SOCKWQ_ASYNC_NOSPACE 040#define SOCKWQ_ASYNC_WAITDATA 141#define SOCK_NOSPACE 242#define SOCK_PASSCRED 343#define SOCK_PASSSEC 444#define SOCK_SUPPORT_ZC 545#define SOCK_CUSTOM_SOCKOPT 646#define SOCK_PASSPIDFD 747 48#ifndef ARCH_HAS_SOCKET_TYPES49/**50 * enum sock_type - Socket types51 * @SOCK_STREAM: stream (connection) socket52 * @SOCK_DGRAM: datagram (conn.less) socket53 * @SOCK_RAW: raw socket54 * @SOCK_RDM: reliably-delivered message55 * @SOCK_SEQPACKET: sequential packet socket56 * @SOCK_DCCP: Datagram Congestion Control Protocol socket57 * @SOCK_PACKET: linux specific way of getting packets at the dev level.58 * For writing rarp and other similar things on the user level.59 *60 * When adding some new socket type please61 * grep ARCH_HAS_SOCKET_TYPE include/asm-* /socket.h, at least MIPS62 * overrides this enum for binary compat reasons.63 */64enum sock_type {65 SOCK_STREAM = 1,66 SOCK_DGRAM = 2,67 SOCK_RAW = 3,68 SOCK_RDM = 4,69 SOCK_SEQPACKET = 5,70 SOCK_DCCP = 6,71 SOCK_PACKET = 10,72};73 74#define SOCK_MAX (SOCK_PACKET + 1)75/* Mask which covers at least up to SOCK_MASK-1. The76 * remaining bits are used as flags. */77#define SOCK_TYPE_MASK 0xf78 79/* Flags for socket, socketpair, accept4 */80#define SOCK_CLOEXEC O_CLOEXEC81#ifndef SOCK_NONBLOCK82#define SOCK_NONBLOCK O_NONBLOCK83#endif84 85#endif /* ARCH_HAS_SOCKET_TYPES */86 87/**88 * enum sock_shutdown_cmd - Shutdown types89 * @SHUT_RD: shutdown receptions90 * @SHUT_WR: shutdown transmissions91 * @SHUT_RDWR: shutdown receptions/transmissions92 */93enum sock_shutdown_cmd {94 SHUT_RD,95 SHUT_WR,96 SHUT_RDWR,97};98 99struct socket_wq {100 /* Note: wait MUST be first field of socket_wq */101 wait_queue_head_t wait;102 struct fasync_struct *fasync_list;103 unsigned long flags; /* %SOCKWQ_ASYNC_NOSPACE, etc */104 struct rcu_head rcu;105} ____cacheline_aligned_in_smp;106 107/**108 * struct socket - general BSD socket109 * @state: socket state (%SS_CONNECTED, etc)110 * @type: socket type (%SOCK_STREAM, etc)111 * @flags: socket flags (%SOCK_NOSPACE, etc)112 * @ops: protocol specific socket operations113 * @file: File back pointer for gc114 * @sk: internal networking protocol agnostic socket representation115 * @wq: wait queue for several uses116 */117struct socket {118 socket_state state;119 120 short type;121 122 unsigned long flags;123 124 struct file *file;125 struct sock *sk;126 const struct proto_ops *ops; /* Might change with IPV6_ADDRFORM or MPTCP. */127 128 struct socket_wq wq;129};130 131/*132 * "descriptor" for what we're up to with a read.133 * This allows us to use the same read code yet134 * have multiple different users of the data that135 * we read from a file.136 *137 * The simplest case just copies the data to user138 * mode.139 */140typedef struct {141 size_t written;142 size_t count;143 union {144 char __user *buf;145 void *data;146 } arg;147 int error;148} read_descriptor_t;149 150struct vm_area_struct;151struct page;152struct sockaddr;153struct msghdr;154struct module;155struct sk_buff;156struct proto_accept_arg;157typedef int (*sk_read_actor_t)(read_descriptor_t *, struct sk_buff *,158 unsigned int, size_t);159typedef int (*skb_read_actor_t)(struct sock *, struct sk_buff *);160 161 162struct proto_ops {163 int family;164 struct module *owner;165 int (*release) (struct socket *sock);166 int (*bind) (struct socket *sock,167 struct sockaddr *myaddr,168 int sockaddr_len);169 int (*connect) (struct socket *sock,170 struct sockaddr *vaddr,171 int sockaddr_len, int flags);172 int (*socketpair)(struct socket *sock1,173 struct socket *sock2);174 int (*accept) (struct socket *sock,175 struct socket *newsock,176 struct proto_accept_arg *arg);177 int (*getname) (struct socket *sock,178 struct sockaddr *addr,179 int peer);180 __poll_t (*poll) (struct file *file, struct socket *sock,181 struct poll_table_struct *wait);182 int (*ioctl) (struct socket *sock, unsigned int cmd,183 unsigned long arg);184#ifdef CONFIG_COMPAT185 int (*compat_ioctl) (struct socket *sock, unsigned int cmd,186 unsigned long arg);187#endif188 int (*gettstamp) (struct socket *sock, void __user *userstamp,189 bool timeval, bool time32);190 int (*listen) (struct socket *sock, int len);191 int (*shutdown) (struct socket *sock, int flags);192 int (*setsockopt)(struct socket *sock, int level,193 int optname, sockptr_t optval,194 unsigned int optlen);195 int (*getsockopt)(struct socket *sock, int level,196 int optname, char __user *optval, int __user *optlen);197 void (*show_fdinfo)(struct seq_file *m, struct socket *sock);198 int (*sendmsg) (struct socket *sock, struct msghdr *m,199 size_t total_len);200 /* Notes for implementing recvmsg:201 * ===============================202 * msg->msg_namelen should get updated by the recvmsg handlers203 * iff msg_name != NULL. It is by default 0 to prevent204 * returning uninitialized memory to user space. The recvfrom205 * handlers can assume that msg.msg_name is either NULL or has206 * a minimum size of sizeof(struct sockaddr_storage).207 */208 int (*recvmsg) (struct socket *sock, struct msghdr *m,209 size_t total_len, int flags);210 int (*mmap) (struct file *file, struct socket *sock,211 struct vm_area_struct * vma);212 ssize_t (*splice_read)(struct socket *sock, loff_t *ppos,213 struct pipe_inode_info *pipe, size_t len, unsigned int flags);214 void (*splice_eof)(struct socket *sock);215 int (*set_peek_off)(struct sock *sk, int val);216 int (*peek_len)(struct socket *sock);217 218 /* The following functions are called internally by kernel with219 * sock lock already held.220 */221 int (*read_sock)(struct sock *sk, read_descriptor_t *desc,222 sk_read_actor_t recv_actor);223 /* This is different from read_sock(), it reads an entire skb at a time. */224 int (*read_skb)(struct sock *sk, skb_read_actor_t recv_actor);225 int (*sendmsg_locked)(struct sock *sk, struct msghdr *msg,226 size_t size);227 int (*set_rcvlowat)(struct sock *sk, int val);228};229 230#define DECLARE_SOCKADDR(type, dst, src) \231 type dst = ({ __sockaddr_check_size(sizeof(*dst)); (type) src; })232 233struct net_proto_family {234 int family;235 int (*create)(struct net *net, struct socket *sock,236 int protocol, int kern);237 struct module *owner;238};239 240struct iovec;241struct kvec;242 243enum {244 SOCK_WAKE_IO,245 SOCK_WAKE_WAITD,246 SOCK_WAKE_SPACE,247 SOCK_WAKE_URG,248};249 250int sock_wake_async(struct socket_wq *sk_wq, int how, int band);251int sock_register(const struct net_proto_family *fam);252void sock_unregister(int family);253bool sock_is_registered(int family);254int __sock_create(struct net *net, int family, int type, int proto,255 struct socket **res, int kern);256int sock_create(int family, int type, int proto, struct socket **res);257int sock_create_kern(struct net *net, int family, int type, int proto, struct socket **res);258int sock_create_lite(int family, int type, int proto, struct socket **res);259struct socket *sock_alloc(void);260void sock_release(struct socket *sock);261int sock_sendmsg(struct socket *sock, struct msghdr *msg);262int sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags);263struct file *sock_alloc_file(struct socket *sock, int flags, const char *dname);264struct socket *sockfd_lookup(int fd, int *err);265struct socket *sock_from_file(struct file *file);266#define sockfd_put(sock) fput(sock->file)267int net_ratelimit(void);268 269#define net_ratelimited_function(function, ...) \270do { \271 if (net_ratelimit()) \272 function(__VA_ARGS__); \273} while (0)274 275#define net_emerg_ratelimited(fmt, ...) \276 net_ratelimited_function(pr_emerg, fmt, ##__VA_ARGS__)277#define net_alert_ratelimited(fmt, ...) \278 net_ratelimited_function(pr_alert, fmt, ##__VA_ARGS__)279#define net_crit_ratelimited(fmt, ...) \280 net_ratelimited_function(pr_crit, fmt, ##__VA_ARGS__)281#define net_err_ratelimited(fmt, ...) \282 net_ratelimited_function(pr_err, fmt, ##__VA_ARGS__)283#define net_notice_ratelimited(fmt, ...) \284 net_ratelimited_function(pr_notice, fmt, ##__VA_ARGS__)285#define net_warn_ratelimited(fmt, ...) \286 net_ratelimited_function(pr_warn, fmt, ##__VA_ARGS__)287#define net_info_ratelimited(fmt, ...) \288 net_ratelimited_function(pr_info, fmt, ##__VA_ARGS__)289#if defined(CONFIG_DYNAMIC_DEBUG) || \290 (defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))291#define net_dbg_ratelimited(fmt, ...) \292do { \293 DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, fmt); \294 if (DYNAMIC_DEBUG_BRANCH(descriptor) && \295 net_ratelimit()) \296 __dynamic_pr_debug(&descriptor, pr_fmt(fmt), \297 ##__VA_ARGS__); \298} while (0)299#elif defined(DEBUG)300#define net_dbg_ratelimited(fmt, ...) \301 net_ratelimited_function(pr_debug, fmt, ##__VA_ARGS__)302#else303#define net_dbg_ratelimited(fmt, ...) \304 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)305#endif306 307#define net_get_random_once(buf, nbytes) \308 get_random_once((buf), (nbytes))309 310/*311 * E.g. XFS meta- & log-data is in slab pages, or bcache meta312 * data pages, or other high order pages allocated by313 * __get_free_pages() without __GFP_COMP, which have a page_count314 * of 0 and/or have PageSlab() set. We cannot use send_page for315 * those, as that does get_page(); put_page(); and would cause316 * either a VM_BUG directly, or __page_cache_release a page that317 * would actually still be referenced by someone, leading to some318 * obscure delayed Oops somewhere else.319 */320static inline bool sendpage_ok(struct page *page)321{322 return !PageSlab(page) && page_count(page) >= 1;323}324 325/*326 * Check sendpage_ok on contiguous pages.327 */328static inline bool sendpages_ok(struct page *page, size_t len, size_t offset)329{330 struct page *p = page + (offset >> PAGE_SHIFT);331 size_t count = 0;332 333 while (count < len) {334 if (!sendpage_ok(p))335 return false;336 337 p++;338 count += PAGE_SIZE;339 }340 341 return true;342}343 344int kernel_sendmsg(struct socket *sock, struct msghdr *msg, struct kvec *vec,345 size_t num, size_t len);346int kernel_sendmsg_locked(struct sock *sk, struct msghdr *msg,347 struct kvec *vec, size_t num, size_t len);348int kernel_recvmsg(struct socket *sock, struct msghdr *msg, struct kvec *vec,349 size_t num, size_t len, int flags);350 351int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen);352int kernel_listen(struct socket *sock, int backlog);353int kernel_accept(struct socket *sock, struct socket **newsock, int flags);354int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,355 int flags);356int kernel_getsockname(struct socket *sock, struct sockaddr *addr);357int kernel_getpeername(struct socket *sock, struct sockaddr *addr);358int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how);359 360/* Routine returns the IP overhead imposed by a (caller-protected) socket. */361u32 kernel_sock_ip_overhead(struct sock *sk);362 363#define MODULE_ALIAS_NETPROTO(proto) \364 MODULE_ALIAS("net-pf-" __stringify(proto))365 366#define MODULE_ALIAS_NET_PF_PROTO(pf, proto) \367 MODULE_ALIAS("net-pf-" __stringify(pf) "-proto-" __stringify(proto))368 369#define MODULE_ALIAS_NET_PF_PROTO_TYPE(pf, proto, type) \370 MODULE_ALIAS("net-pf-" __stringify(pf) "-proto-" __stringify(proto) \371 "-type-" __stringify(type))372 373#define MODULE_ALIAS_NET_PF_PROTO_NAME(pf, proto, name) \374 MODULE_ALIAS("net-pf-" __stringify(pf) "-proto-" __stringify(proto) \375 name)376#endif /* _LINUX_NET_H */377