xref: /linux/include/linux/net.h (revision 0093f7db9c47177fd5a269858002c89c6a3fa340)
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 #include <linux/uio.h>
27 
28 #include <uapi/linux/net.h>
29 
30 /**
31  * struct sockopt - socket option value container
32  * @iter_in: iov_iter for reading optval with the content from the caller.
33  *	     Use copy_from_iter() given this iov direction is ITER_SOURCE
34  * @iter_out: iov_iter for protocols to update optval data to userspace
35  *	      Use _copy_to_iter() given iov direction is ITER_DEST
36  * @optlen: serves as both input (buffer size) and output (returned data size).
37  *
38  * Type-safe wrapper for socket option data that works with both
39  * user and kernel buffers.
40  *
41  * The optlen field allows callbacks to return a specific length value
42  * independent of the bytes written via copy_to_iter().
43  */
44 typedef struct sockopt {
45 	struct iov_iter iter_in;
46 	struct iov_iter iter_out;
47 	int optlen;
48 } sockopt_t;
49 
50 int sockptr_to_sockopt(sockopt_t *opt, sockptr_t optval, sockptr_t optlen,
51 		       struct kvec *kvec);
52 
53 /*
54  * Initialize a user-backed sockopt_t from the (optval, optlen) __user pair of
55  * a getsockopt() callback. Used by transitional __user getsockopt wrappers
56  * while the proto-layer callbacks are converted to take a sockopt_t; the
57  * caller writes opt->optlen back to the user optlen after the callback.
58  */
59 static inline int sockopt_init_user(sockopt_t *opt, char __user *optval,
60 				    int __user *optlen)
61 {
62 	int len;
63 
64 	if (get_user(len, optlen))
65 		return -EFAULT;
66 	if (len < 0)
67 		return -EINVAL;
68 
69 	iov_iter_ubuf(&opt->iter_out, ITER_DEST, optval, len);
70 	iov_iter_ubuf(&opt->iter_in, ITER_SOURCE, optval, len);
71 	opt->optlen = len;
72 
73 	return 0;
74 }
75 
76 /*
77  * Grow optval to @size, for the options whose reply is sized by a count the
78  * caller left in optval rather than by optlen. Those write past optlen today
79  * and userspace relies on it.
80  *
81  * Call it before writing through opt->iter_out: it re-anchors the iterator at
82  * the head of optval. Only a user buffer can be longer than the optlen the
83  * caller declared, so a kernel-backed optval is refused with -EINVAL.
84  */
85 static inline int sockopt_expand_out(sockopt_t *opt, size_t size)
86 {
87 	if (size <= (size_t)opt->optlen)
88 		return 0;
89 
90 	if (size > INT_MAX)
91 		return -EINVAL;
92 
93 	/* Re-anchoring reads iter_out.ubuf, so the iterator has to be a user
94 	 * buffer that nothing has written through yet.
95 	 */
96 	if (WARN_ON_ONCE(!iter_is_ubuf(&opt->iter_out) ||
97 			 iov_iter_count(&opt->iter_out) != (size_t)opt->optlen))
98 		return -EINVAL;
99 
100 	iov_iter_ubuf(&opt->iter_out, ITER_DEST, opt->iter_out.ubuf, size);
101 
102 	return 0;
103 }
104 
105 struct poll_table_struct;
106 struct pipe_inode_info;
107 struct inode;
108 struct file;
109 struct net;
110 
111 /* Historically, SOCKWQ_ASYNC_NOSPACE & SOCKWQ_ASYNC_WAITDATA were located
112  * in sock->flags, but moved into sk->sk_wq->flags to be RCU protected.
113  * Eventually all flags will be in sk->sk_wq->flags.
114  */
115 enum socket_flags {
116 	SOCKWQ_ASYNC_NOSPACE,
117 	SOCKWQ_ASYNC_WAITDATA,
118 	SOCK_NOSPACE,
119 	SOCK_SUPPORT_ZC,
120 	SOCK_CUSTOM_SOCKOPT,
121 };
122 
123 #ifndef ARCH_HAS_SOCKET_TYPES
124 /**
125  * enum sock_type - Socket types
126  * @SOCK_STREAM: stream (connection) socket
127  * @SOCK_DGRAM: datagram (conn.less) socket
128  * @SOCK_RAW: raw socket
129  * @SOCK_RDM: reliably-delivered message
130  * @SOCK_SEQPACKET: sequential packet socket
131  * @SOCK_DCCP: Datagram Congestion Control Protocol socket
132  * @SOCK_PACKET: linux specific way of getting packets at the dev level.
133  *		  For writing rarp and other similar things on the user level.
134  *
135  * When adding some new socket type please
136  * grep ARCH_HAS_SOCKET_TYPE include/asm-* /socket.h, at least MIPS
137  * overrides this enum for binary compat reasons.
138  */
139 enum sock_type {
140 	SOCK_STREAM	= 1,
141 	SOCK_DGRAM	= 2,
142 	SOCK_RAW	= 3,
143 	SOCK_RDM	= 4,
144 	SOCK_SEQPACKET	= 5,
145 	SOCK_DCCP	= 6,
146 	SOCK_PACKET	= 10,
147 };
148 #endif /* ARCH_HAS_SOCKET_TYPES */
149 
150 #define SOCK_MAX (SOCK_PACKET + 1)
151 /* Mask which covers at least up to SOCK_MASK-1.  The
152  * remaining bits are used as flags. */
153 #define SOCK_TYPE_MASK 0xf
154 
155 /* Flags for socket, socketpair, accept4 */
156 #define SOCK_CLOEXEC	O_CLOEXEC
157 #ifndef SOCK_NONBLOCK
158 #define SOCK_NONBLOCK	O_NONBLOCK
159 #endif
160 #define SOCK_COREDUMP	O_NOCTTY
161 
162 /**
163  * enum sock_shutdown_cmd - Shutdown types
164  * @SHUT_RD: shutdown receptions
165  * @SHUT_WR: shutdown transmissions
166  * @SHUT_RDWR: shutdown receptions/transmissions
167  */
168 enum sock_shutdown_cmd {
169 	SHUT_RD,
170 	SHUT_WR,
171 	SHUT_RDWR,
172 };
173 
174 struct socket_wq {
175 	/* Note: wait MUST be first field of socket_wq */
176 	wait_queue_head_t	wait;
177 	struct fasync_struct	*fasync_list;
178 	unsigned long		flags; /* %SOCKWQ_ASYNC_NOSPACE, etc */
179 	struct rcu_head		rcu;
180 } ____cacheline_aligned_in_smp;
181 
182 /**
183  *  struct socket - general BSD socket
184  *  @state: socket state (%SS_CONNECTED, etc)
185  *  @type: socket type (%SOCK_STREAM, etc)
186  *  @flags: socket flags (%SOCK_NOSPACE, etc)
187  *  @ops: protocol specific socket operations
188  *  @file: File back pointer for gc
189  *  @sk: internal networking protocol agnostic socket representation
190  *  @wq: wait queue for several uses
191  */
192 struct socket {
193 	socket_state		state;
194 
195 	short			type;
196 
197 	unsigned long		flags;
198 
199 	struct file		*file;
200 	struct sock		*sk;
201 	const struct proto_ops	*ops; /* Might change with MPTCP. */
202 
203 	struct socket_wq	wq;
204 };
205 
206 /*
207  * "descriptor" for what we're up to with a read.
208  * This allows us to use the same read code yet
209  * have multiple different users of the data that
210  * we read from a file.
211  *
212  * The simplest case just copies the data to user
213  * mode.
214  */
215 typedef struct {
216 	size_t written;
217 	size_t count;
218 	union {
219 		char __user *buf;
220 		void *data;
221 	} arg;
222 	int error;
223 } read_descriptor_t;
224 
225 struct vm_area_struct;
226 struct page;
227 struct msghdr;
228 struct module;
229 struct sk_buff;
230 struct proto_accept_arg;
231 typedef int (*sk_read_actor_t)(read_descriptor_t *, struct sk_buff *,
232 			       unsigned int, size_t);
233 typedef int (*skb_read_actor_t)(struct sock *, struct sk_buff *);
234 
235 
236 struct proto_ops {
237 	int		family;
238 	struct module	*owner;
239 	int		(*release)   (struct socket *sock);
240 	int		(*bind)	     (struct socket *sock,
241 				      struct sockaddr_unsized *myaddr,
242 				      int sockaddr_len);
243 	int		(*connect)   (struct socket *sock,
244 				      struct sockaddr_unsized *vaddr,
245 				      int sockaddr_len, int flags);
246 	int		(*socketpair)(struct socket *sock1,
247 				      struct socket *sock2);
248 	int		(*accept)    (struct socket *sock,
249 				      struct socket *newsock,
250 				      struct proto_accept_arg *arg);
251 	int		(*getname)   (struct socket *sock,
252 				      struct sockaddr *addr,
253 				      int peer);
254 	__poll_t	(*poll)	     (struct file *file, struct socket *sock,
255 				      struct poll_table_struct *wait);
256 	int		(*ioctl)     (struct socket *sock, unsigned int cmd,
257 				      unsigned long arg);
258 #ifdef CONFIG_COMPAT
259 	int	 	(*compat_ioctl) (struct socket *sock, unsigned int cmd,
260 				      unsigned long arg);
261 #endif
262 	int		(*gettstamp) (struct socket *sock, void __user *userstamp,
263 				      bool timeval, bool time32);
264 	int		(*listen)    (struct socket *sock, int len);
265 	int		(*shutdown)  (struct socket *sock, int flags);
266 	int		(*setsockopt)(struct socket *sock, int level,
267 				      int optname, sockptr_t optval,
268 				      unsigned int optlen);
269 	int		(*getsockopt)(struct socket *sock, int level,
270 				      int optname, char __user *optval, int __user *optlen);
271 	int		(*getsockopt_iter)(struct socket *sock, int level,
272 					   int optname, sockopt_t *opt);
273 	void		(*show_fdinfo)(struct seq_file *m, struct socket *sock);
274 	int		(*sendmsg)   (struct socket *sock, struct msghdr *m,
275 				      size_t total_len);
276 	/* Notes for implementing recvmsg:
277 	 * ===============================
278 	 * msg->msg_namelen should get updated by the recvmsg handlers
279 	 * iff msg_name != NULL. It is by default 0 to prevent
280 	 * returning uninitialized memory to user space.  The recvfrom
281 	 * handlers can assume that msg.msg_name is either NULL or has
282 	 * a minimum size of sizeof(struct sockaddr_storage).
283 	 */
284 	int		(*recvmsg)   (struct socket *sock, struct msghdr *m,
285 				      size_t total_len, int flags);
286 	int		(*mmap)	     (struct file *file, struct socket *sock,
287 				      struct vm_area_struct * vma);
288 	ssize_t 	(*splice_read)(struct socket *sock,  loff_t *ppos,
289 				       struct pipe_inode_info *pipe, size_t len, unsigned int flags);
290 	void		(*splice_eof)(struct socket *sock);
291 	int		(*set_peek_off)(struct sock *sk, int val);
292 	int		(*peek_len)(struct socket *sock);
293 
294 	/* The following functions are called internally by kernel with
295 	 * sock lock already held.
296 	 */
297 	int		(*read_sock)(struct sock *sk, read_descriptor_t *desc,
298 				     sk_read_actor_t recv_actor);
299 	/* This is different from read_sock(), it reads an entire skb at a time. */
300 	int		(*read_skb)(struct sock *sk, skb_read_actor_t recv_actor);
301 	int		(*sendmsg_locked)(struct sock *sk, struct msghdr *msg,
302 					  size_t size);
303 	int		(*set_rcvlowat)(struct sock *sk, int val);
304 	void		(*set_rcvbuf)(struct sock *sk, int val);
305 };
306 
307 #define DECLARE_SOCKADDR(type, dst, src)	\
308 	type dst = ({ __sockaddr_check_size(sizeof(*dst)); (type) src; })
309 
310 struct net_proto_family {
311 	int		family;
312 	int		(*create)(struct net *net, struct socket *sock,
313 				  int protocol, int kern);
314 	struct module	*owner;
315 };
316 
317 struct iovec;
318 struct kvec;
319 
320 enum {
321 	SOCK_WAKE_IO,
322 	SOCK_WAKE_WAITD,
323 	SOCK_WAKE_SPACE,
324 	SOCK_WAKE_URG,
325 };
326 
327 int sock_wake_async(struct socket_wq *sk_wq, int how, int band);
328 int sock_register(const struct net_proto_family *fam);
329 void sock_unregister(int family);
330 bool sock_is_registered(int family);
331 int __sock_create(struct net *net, int family, int type, int proto,
332 		  struct socket **res, int kern);
333 int sock_create(int family, int type, int proto, struct socket **res);
334 int sock_create_kern(struct net *net, int family, int type, int proto, struct socket **res);
335 int sock_create_lite(int family, int type, int proto, struct socket **res);
336 struct socket *sock_alloc(void);
337 void sock_release(struct socket *sock);
338 int sock_sendmsg(struct socket *sock, struct msghdr *msg);
339 int sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags);
340 struct file *sock_alloc_file(struct socket *sock, int flags, const char *dname);
341 struct socket *sockfd_lookup(int fd, int *err);
342 struct socket *sock_from_file(struct file *file);
343 int sock_read_xattr(struct socket *sock, const char *name, void *value, size_t size);
344 #define		     sockfd_put(sock) fput(sock->file)
345 int net_ratelimit(void);
346 
347 #define net_ratelimited_function(function, ...)			\
348 do {								\
349 	if (net_ratelimit())					\
350 		function(__VA_ARGS__);				\
351 } while (0)
352 
353 #define net_emerg_ratelimited(fmt, ...)				\
354 	net_ratelimited_function(pr_emerg, fmt, ##__VA_ARGS__)
355 #define net_alert_ratelimited(fmt, ...)				\
356 	net_ratelimited_function(pr_alert, fmt, ##__VA_ARGS__)
357 #define net_crit_ratelimited(fmt, ...)				\
358 	net_ratelimited_function(pr_crit, fmt, ##__VA_ARGS__)
359 #define net_err_ratelimited(fmt, ...)				\
360 	net_ratelimited_function(pr_err, fmt, ##__VA_ARGS__)
361 #define net_notice_ratelimited(fmt, ...)			\
362 	net_ratelimited_function(pr_notice, fmt, ##__VA_ARGS__)
363 #define net_warn_ratelimited(fmt, ...)				\
364 	net_ratelimited_function(pr_warn, fmt, ##__VA_ARGS__)
365 #define net_info_ratelimited(fmt, ...)				\
366 	net_ratelimited_function(pr_info, fmt, ##__VA_ARGS__)
367 #if defined(CONFIG_DYNAMIC_DEBUG) || \
368 	(defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
369 #define net_dbg_ratelimited(fmt, ...)					\
370 do {									\
371 	DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, fmt);			\
372 	if (DYNAMIC_DEBUG_BRANCH(descriptor) &&				\
373 	    net_ratelimit())						\
374 		__dynamic_pr_debug(&descriptor, pr_fmt(fmt),		\
375 		                   ##__VA_ARGS__);			\
376 } while (0)
377 #elif defined(DEBUG)
378 #define net_dbg_ratelimited(fmt, ...)				\
379 	net_ratelimited_function(pr_debug, fmt, ##__VA_ARGS__)
380 #else
381 #define net_dbg_ratelimited(fmt, ...)				\
382 	no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
383 #endif
384 
385 #define net_get_random_once(buf, nbytes)			\
386 	get_random_once((buf), (nbytes))
387 #define net_get_random_sleepable_once(buf, nbytes)		\
388 	get_random_sleepable_once((buf), (nbytes))
389 
390 /*
391  * E.g. XFS meta- & log-data is in slab pages, or bcache meta
392  * data pages, or other high order pages allocated by
393  * __get_free_pages() without __GFP_COMP, which have a page_count
394  * of 0 and/or have PageSlab() set. We cannot use send_page for
395  * those, as that does get_page(); put_page(); and would cause
396  * either a VM_BUG directly, or __page_cache_release a page that
397  * would actually still be referenced by someone, leading to some
398  * obscure delayed Oops somewhere else.
399  */
400 static inline bool sendpage_ok(struct page *page)
401 {
402 	return !PageSlab(page) && page_count(page) >= 1;
403 }
404 
405 /*
406  * Check sendpage_ok on contiguous pages.
407  */
408 static inline bool sendpages_ok(struct page *page, size_t len, size_t offset)
409 {
410 	struct page *p = page + (offset >> PAGE_SHIFT);
411 	size_t count = 0;
412 
413 	while (count < len) {
414 		if (!sendpage_ok(p))
415 			return false;
416 
417 		p++;
418 		count += PAGE_SIZE;
419 	}
420 
421 	return true;
422 }
423 
424 int kernel_sendmsg(struct socket *sock, struct msghdr *msg, struct kvec *vec,
425 		   size_t num, size_t len);
426 int kernel_recvmsg(struct socket *sock, struct msghdr *msg, struct kvec *vec,
427 		   size_t num, size_t len, int flags);
428 
429 int kernel_bind(struct socket *sock, struct sockaddr_unsized *addr, int addrlen);
430 int kernel_listen(struct socket *sock, int backlog);
431 int kernel_accept(struct socket *sock, struct socket **newsock, int flags);
432 int kernel_connect(struct socket *sock, struct sockaddr_unsized *addr, int addrlen,
433 		   int flags);
434 int kernel_getsockname(struct socket *sock, struct sockaddr *addr);
435 int kernel_getpeername(struct socket *sock, struct sockaddr *addr);
436 int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how);
437 
438 /* Routine returns the IP overhead imposed by a (caller-protected) socket. */
439 u32 kernel_sock_ip_overhead(struct sock *sk);
440 
441 #define MODULE_ALIAS_NETPROTO(proto) \
442 	MODULE_ALIAS("net-pf-" __stringify(proto))
443 
444 #define MODULE_ALIAS_NET_PF_PROTO(pf, proto) \
445 	MODULE_ALIAS("net-pf-" __stringify(pf) "-proto-" __stringify(proto))
446 
447 #define MODULE_ALIAS_NET_PF_PROTO_TYPE(pf, proto, type) \
448 	MODULE_ALIAS("net-pf-" __stringify(pf) "-proto-" __stringify(proto) \
449 		     "-type-" __stringify(type))
450 
451 #define MODULE_ALIAS_NET_PF_PROTO_NAME(pf, proto, name) \
452 	MODULE_ALIAS("net-pf-" __stringify(pf) "-proto-" __stringify(proto) \
453 		     name)
454 #endif	/* _LINUX_NET_H */
455