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