xref: /linux/include/linux/net.h (revision 8f7aa3d3c7323f4ca2768a9e74ebbe359c4f8f88)
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 the
6  *		kernel.
7  *
8  * Version:	@(#)net.h	1.0.3	05/25/93
9  *
10  * Authors:	Orest Zborowski, <obz@Kodak.COM>
11  *		Ross Biro
12  *		Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
13  */
14 #ifndef _LINUX_NET_H
15 #define _LINUX_NET_H
16 
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 
29 struct poll_table_struct;
30 struct pipe_inode_info;
31 struct inode;
32 struct file;
33 struct net;
34 
35 /* Historically, SOCKWQ_ASYNC_NOSPACE & SOCKWQ_ASYNC_WAITDATA were located
36  * 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 enum socket_flags {
40 	SOCKWQ_ASYNC_NOSPACE,
41 	SOCKWQ_ASYNC_WAITDATA,
42 	SOCK_NOSPACE,
43 	SOCK_SUPPORT_ZC,
44 	SOCK_CUSTOM_SOCKOPT,
45 };
46 
47 #ifndef ARCH_HAS_SOCKET_TYPES
48 /**
49  * enum sock_type - Socket types
50  * @SOCK_STREAM: stream (connection) socket
51  * @SOCK_DGRAM: datagram (conn.less) socket
52  * @SOCK_RAW: raw socket
53  * @SOCK_RDM: reliably-delivered message
54  * @SOCK_SEQPACKET: sequential packet socket
55  * @SOCK_DCCP: Datagram Congestion Control Protocol socket
56  * @SOCK_PACKET: linux specific way of getting packets at the dev level.
57  *		  For writing rarp and other similar things on the user level.
58  *
59  * When adding some new socket type please
60  * grep ARCH_HAS_SOCKET_TYPE include/asm-* /socket.h, at least MIPS
61  * overrides this enum for binary compat reasons.
62  */
63 enum sock_type {
64 	SOCK_STREAM	= 1,
65 	SOCK_DGRAM	= 2,
66 	SOCK_RAW	= 3,
67 	SOCK_RDM	= 4,
68 	SOCK_SEQPACKET	= 5,
69 	SOCK_DCCP	= 6,
70 	SOCK_PACKET	= 10,
71 };
72 #endif /* ARCH_HAS_SOCKET_TYPES */
73 
74 #define SOCK_MAX (SOCK_PACKET + 1)
75 /* Mask which covers at least up to SOCK_MASK-1.  The
76  * remaining bits are used as flags. */
77 #define SOCK_TYPE_MASK 0xf
78 
79 /* Flags for socket, socketpair, accept4 */
80 #define SOCK_CLOEXEC	O_CLOEXEC
81 #ifndef SOCK_NONBLOCK
82 #define SOCK_NONBLOCK	O_NONBLOCK
83 #endif
84 #define SOCK_COREDUMP	O_NOCTTY
85 
86 /**
87  * enum sock_shutdown_cmd - Shutdown types
88  * @SHUT_RD: shutdown receptions
89  * @SHUT_WR: shutdown transmissions
90  * @SHUT_RDWR: shutdown receptions/transmissions
91  */
92 enum sock_shutdown_cmd {
93 	SHUT_RD,
94 	SHUT_WR,
95 	SHUT_RDWR,
96 };
97 
98 struct socket_wq {
99 	/* Note: wait MUST be first field of socket_wq */
100 	wait_queue_head_t	wait;
101 	struct fasync_struct	*fasync_list;
102 	unsigned long		flags; /* %SOCKWQ_ASYNC_NOSPACE, etc */
103 	struct rcu_head		rcu;
104 } ____cacheline_aligned_in_smp;
105 
106 /**
107  *  struct socket - general BSD socket
108  *  @state: socket state (%SS_CONNECTED, etc)
109  *  @type: socket type (%SOCK_STREAM, etc)
110  *  @flags: socket flags (%SOCK_NOSPACE, etc)
111  *  @ops: protocol specific socket operations
112  *  @file: File back pointer for gc
113  *  @sk: internal networking protocol agnostic socket representation
114  *  @wq: wait queue for several uses
115  */
116 struct socket {
117 	socket_state		state;
118 
119 	short			type;
120 
121 	unsigned long		flags;
122 
123 	struct file		*file;
124 	struct sock		*sk;
125 	const struct proto_ops	*ops; /* Might change with IPV6_ADDRFORM or MPTCP. */
126 
127 	struct socket_wq	wq;
128 };
129 
130 /*
131  * "descriptor" for what we're up to with a read.
132  * This allows us to use the same read code yet
133  * have multiple different users of the data that
134  * we read from a file.
135  *
136  * The simplest case just copies the data to user
137  * mode.
138  */
139 typedef struct {
140 	size_t written;
141 	size_t count;
142 	union {
143 		char __user *buf;
144 		void *data;
145 	} arg;
146 	int error;
147 } read_descriptor_t;
148 
149 struct vm_area_struct;
150 struct page;
151 struct msghdr;
152 struct module;
153 struct sk_buff;
154 struct proto_accept_arg;
155 typedef int (*sk_read_actor_t)(read_descriptor_t *, struct sk_buff *,
156 			       unsigned int, size_t);
157 typedef int (*skb_read_actor_t)(struct sock *, struct sk_buff *);
158 
159 
160 struct proto_ops {
161 	int		family;
162 	struct module	*owner;
163 	int		(*release)   (struct socket *sock);
164 	int		(*bind)	     (struct socket *sock,
165 				      struct sockaddr_unsized *myaddr,
166 				      int sockaddr_len);
167 	int		(*connect)   (struct socket *sock,
168 				      struct sockaddr_unsized *vaddr,
169 				      int sockaddr_len, int flags);
170 	int		(*socketpair)(struct socket *sock1,
171 				      struct socket *sock2);
172 	int		(*accept)    (struct socket *sock,
173 				      struct socket *newsock,
174 				      struct proto_accept_arg *arg);
175 	int		(*getname)   (struct socket *sock,
176 				      struct sockaddr *addr,
177 				      int peer);
178 	__poll_t	(*poll)	     (struct file *file, struct socket *sock,
179 				      struct poll_table_struct *wait);
180 	int		(*ioctl)     (struct socket *sock, unsigned int cmd,
181 				      unsigned long arg);
182 #ifdef CONFIG_COMPAT
183 	int	 	(*compat_ioctl) (struct socket *sock, unsigned int cmd,
184 				      unsigned long arg);
185 #endif
186 	int		(*gettstamp) (struct socket *sock, void __user *userstamp,
187 				      bool timeval, bool time32);
188 	int		(*listen)    (struct socket *sock, int len);
189 	int		(*shutdown)  (struct socket *sock, int flags);
190 	int		(*setsockopt)(struct socket *sock, int level,
191 				      int optname, sockptr_t optval,
192 				      unsigned int optlen);
193 	int		(*getsockopt)(struct socket *sock, int level,
194 				      int optname, char __user *optval, int __user *optlen);
195 	void		(*show_fdinfo)(struct seq_file *m, struct socket *sock);
196 	int		(*sendmsg)   (struct socket *sock, struct msghdr *m,
197 				      size_t total_len);
198 	/* Notes for implementing recvmsg:
199 	 * ===============================
200 	 * msg->msg_namelen should get updated by the recvmsg handlers
201 	 * iff msg_name != NULL. It is by default 0 to prevent
202 	 * returning uninitialized memory to user space.  The recvfrom
203 	 * handlers can assume that msg.msg_name is either NULL or has
204 	 * a minimum size of sizeof(struct sockaddr_storage).
205 	 */
206 	int		(*recvmsg)   (struct socket *sock, struct msghdr *m,
207 				      size_t total_len, int flags);
208 	int		(*mmap)	     (struct file *file, struct socket *sock,
209 				      struct vm_area_struct * vma);
210 	ssize_t 	(*splice_read)(struct socket *sock,  loff_t *ppos,
211 				       struct pipe_inode_info *pipe, size_t len, unsigned int flags);
212 	void		(*splice_eof)(struct socket *sock);
213 	int		(*set_peek_off)(struct sock *sk, int val);
214 	int		(*peek_len)(struct socket *sock);
215 
216 	/* The following functions are called internally by kernel with
217 	 * sock lock already held.
218 	 */
219 	int		(*read_sock)(struct sock *sk, read_descriptor_t *desc,
220 				     sk_read_actor_t recv_actor);
221 	/* This is different from read_sock(), it reads an entire skb at a time. */
222 	int		(*read_skb)(struct sock *sk, skb_read_actor_t recv_actor);
223 	int		(*sendmsg_locked)(struct sock *sk, struct msghdr *msg,
224 					  size_t size);
225 	int		(*set_rcvlowat)(struct sock *sk, int val);
226 };
227 
228 #define DECLARE_SOCKADDR(type, dst, src)	\
229 	type dst = ({ __sockaddr_check_size(sizeof(*dst)); (type) src; })
230 
231 struct net_proto_family {
232 	int		family;
233 	int		(*create)(struct net *net, struct socket *sock,
234 				  int protocol, int kern);
235 	struct module	*owner;
236 };
237 
238 struct iovec;
239 struct kvec;
240 
241 enum {
242 	SOCK_WAKE_IO,
243 	SOCK_WAKE_WAITD,
244 	SOCK_WAKE_SPACE,
245 	SOCK_WAKE_URG,
246 };
247 
248 int sock_wake_async(struct socket_wq *sk_wq, int how, int band);
249 int sock_register(const struct net_proto_family *fam);
250 void sock_unregister(int family);
251 bool sock_is_registered(int family);
252 int __sock_create(struct net *net, int family, int type, int proto,
253 		  struct socket **res, int kern);
254 int sock_create(int family, int type, int proto, struct socket **res);
255 int sock_create_kern(struct net *net, int family, int type, int proto, struct socket **res);
256 int sock_create_lite(int family, int type, int proto, struct socket **res);
257 struct socket *sock_alloc(void);
258 void sock_release(struct socket *sock);
259 int sock_sendmsg(struct socket *sock, struct msghdr *msg);
260 int sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags);
261 struct file *sock_alloc_file(struct socket *sock, int flags, const char *dname);
262 struct socket *sockfd_lookup(int fd, int *err);
263 struct socket *sock_from_file(struct file *file);
264 #define		     sockfd_put(sock) fput(sock->file)
265 int net_ratelimit(void);
266 
267 #define net_ratelimited_function(function, ...)			\
268 do {								\
269 	if (net_ratelimit())					\
270 		function(__VA_ARGS__);				\
271 } while (0)
272 
273 #define net_emerg_ratelimited(fmt, ...)				\
274 	net_ratelimited_function(pr_emerg, fmt, ##__VA_ARGS__)
275 #define net_alert_ratelimited(fmt, ...)				\
276 	net_ratelimited_function(pr_alert, fmt, ##__VA_ARGS__)
277 #define net_crit_ratelimited(fmt, ...)				\
278 	net_ratelimited_function(pr_crit, fmt, ##__VA_ARGS__)
279 #define net_err_ratelimited(fmt, ...)				\
280 	net_ratelimited_function(pr_err, fmt, ##__VA_ARGS__)
281 #define net_notice_ratelimited(fmt, ...)			\
282 	net_ratelimited_function(pr_notice, fmt, ##__VA_ARGS__)
283 #define net_warn_ratelimited(fmt, ...)				\
284 	net_ratelimited_function(pr_warn, fmt, ##__VA_ARGS__)
285 #define net_info_ratelimited(fmt, ...)				\
286 	net_ratelimited_function(pr_info, fmt, ##__VA_ARGS__)
287 #if defined(CONFIG_DYNAMIC_DEBUG) || \
288 	(defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
289 #define net_dbg_ratelimited(fmt, ...)					\
290 do {									\
291 	DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, fmt);			\
292 	if (DYNAMIC_DEBUG_BRANCH(descriptor) &&				\
293 	    net_ratelimit())						\
294 		__dynamic_pr_debug(&descriptor, pr_fmt(fmt),		\
295 		                   ##__VA_ARGS__);			\
296 } while (0)
297 #elif defined(DEBUG)
298 #define net_dbg_ratelimited(fmt, ...)				\
299 	net_ratelimited_function(pr_debug, fmt, ##__VA_ARGS__)
300 #else
301 #define net_dbg_ratelimited(fmt, ...)				\
302 	no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
303 #endif
304 
305 #define net_get_random_once(buf, nbytes)			\
306 	get_random_once((buf), (nbytes))
307 
308 /*
309  * E.g. XFS meta- & log-data is in slab pages, or bcache meta
310  * data pages, or other high order pages allocated by
311  * __get_free_pages() without __GFP_COMP, which have a page_count
312  * of 0 and/or have PageSlab() set. We cannot use send_page for
313  * those, as that does get_page(); put_page(); and would cause
314  * either a VM_BUG directly, or __page_cache_release a page that
315  * would actually still be referenced by someone, leading to some
316  * obscure delayed Oops somewhere else.
317  */
sendpage_ok(struct page * page)318 static inline bool sendpage_ok(struct page *page)
319 {
320 	return !PageSlab(page) && page_count(page) >= 1;
321 }
322 
323 /*
324  * Check sendpage_ok on contiguous pages.
325  */
sendpages_ok(struct page * page,size_t len,size_t offset)326 static inline bool sendpages_ok(struct page *page, size_t len, size_t offset)
327 {
328 	struct page *p = page + (offset >> PAGE_SHIFT);
329 	size_t count = 0;
330 
331 	while (count < len) {
332 		if (!sendpage_ok(p))
333 			return false;
334 
335 		p++;
336 		count += PAGE_SIZE;
337 	}
338 
339 	return true;
340 }
341 
342 int kernel_sendmsg(struct socket *sock, struct msghdr *msg, struct kvec *vec,
343 		   size_t num, size_t len);
344 int kernel_recvmsg(struct socket *sock, struct msghdr *msg, struct kvec *vec,
345 		   size_t num, size_t len, int flags);
346 
347 int kernel_bind(struct socket *sock, struct sockaddr_unsized *addr, int addrlen);
348 int kernel_listen(struct socket *sock, int backlog);
349 int kernel_accept(struct socket *sock, struct socket **newsock, int flags);
350 int kernel_connect(struct socket *sock, struct sockaddr_unsized *addr, int addrlen,
351 		   int flags);
352 int kernel_getsockname(struct socket *sock, struct sockaddr *addr);
353 int kernel_getpeername(struct socket *sock, struct sockaddr *addr);
354 int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how);
355 
356 /* Routine returns the IP overhead imposed by a (caller-protected) socket. */
357 u32 kernel_sock_ip_overhead(struct sock *sk);
358 
359 #define MODULE_ALIAS_NETPROTO(proto) \
360 	MODULE_ALIAS("net-pf-" __stringify(proto))
361 
362 #define MODULE_ALIAS_NET_PF_PROTO(pf, proto) \
363 	MODULE_ALIAS("net-pf-" __stringify(pf) "-proto-" __stringify(proto))
364 
365 #define MODULE_ALIAS_NET_PF_PROTO_TYPE(pf, proto, type) \
366 	MODULE_ALIAS("net-pf-" __stringify(pf) "-proto-" __stringify(proto) \
367 		     "-type-" __stringify(type))
368 
369 #define MODULE_ALIAS_NET_PF_PROTO_NAME(pf, proto, name) \
370 	MODULE_ALIAS("net-pf-" __stringify(pf) "-proto-" __stringify(proto) \
371 		     name)
372 #endif	/* _LINUX_NET_H */
373