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