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 struct proto_accept_arg;
158 typedef int (*sk_read_actor_t)(read_descriptor_t *, struct sk_buff *,
159 unsigned int, size_t);
160 typedef int (*skb_read_actor_t)(struct sock *, struct sk_buff *);
161
162
163 struct proto_ops {
164 int family;
165 struct module *owner;
166 int (*release) (struct socket *sock);
167 int (*bind) (struct socket *sock,
168 struct sockaddr *myaddr,
169 int sockaddr_len);
170 int (*connect) (struct socket *sock,
171 struct sockaddr *vaddr,
172 int sockaddr_len, int flags);
173 int (*socketpair)(struct socket *sock1,
174 struct socket *sock2);
175 int (*accept) (struct socket *sock,
176 struct socket *newsock,
177 struct proto_accept_arg *arg);
178 int (*getname) (struct socket *sock,
179 struct sockaddr *addr,
180 int peer);
181 __poll_t (*poll) (struct file *file, struct socket *sock,
182 struct poll_table_struct *wait);
183 int (*ioctl) (struct socket *sock, unsigned int cmd,
184 unsigned long arg);
185 #ifdef CONFIG_COMPAT
186 int (*compat_ioctl) (struct socket *sock, unsigned int cmd,
187 unsigned long arg);
188 #endif
189 int (*gettstamp) (struct socket *sock, void __user *userstamp,
190 bool timeval, bool time32);
191 int (*listen) (struct socket *sock, int len);
192 int (*shutdown) (struct socket *sock, int flags);
193 int (*setsockopt)(struct socket *sock, int level,
194 int optname, sockptr_t optval,
195 unsigned int optlen);
196 int (*getsockopt)(struct socket *sock, int level,
197 int optname, char __user *optval, int __user *optlen);
198 void (*show_fdinfo)(struct seq_file *m, struct socket *sock);
199 int (*sendmsg) (struct socket *sock, struct msghdr *m,
200 size_t total_len);
201 /* Notes for implementing recvmsg:
202 * ===============================
203 * msg->msg_namelen should get updated by the recvmsg handlers
204 * iff msg_name != NULL. It is by default 0 to prevent
205 * returning uninitialized memory to user space. The recvfrom
206 * handlers can assume that msg.msg_name is either NULL or has
207 * a minimum size of sizeof(struct sockaddr_storage).
208 */
209 int (*recvmsg) (struct socket *sock, struct msghdr *m,
210 size_t total_len, int flags);
211 int (*mmap) (struct file *file, struct socket *sock,
212 struct vm_area_struct * vma);
213 ssize_t (*splice_read)(struct socket *sock, loff_t *ppos,
214 struct pipe_inode_info *pipe, size_t len, unsigned int flags);
215 void (*splice_eof)(struct socket *sock);
216 int (*set_peek_off)(struct sock *sk, int val);
217 int (*peek_len)(struct socket *sock);
218
219 /* The following functions are called internally by kernel with
220 * sock lock already held.
221 */
222 int (*read_sock)(struct sock *sk, read_descriptor_t *desc,
223 sk_read_actor_t recv_actor);
224 /* This is different from read_sock(), it reads an entire skb at a time. */
225 int (*read_skb)(struct sock *sk, skb_read_actor_t recv_actor);
226 int (*sendmsg_locked)(struct sock *sk, struct msghdr *msg,
227 size_t size);
228 int (*set_rcvlowat)(struct sock *sk, int val);
229
230 ANDROID_KABI_RESERVE(1);
231 ANDROID_KABI_RESERVE(2);
232 ANDROID_KABI_RESERVE(3);
233 ANDROID_KABI_RESERVE(4);
234 };
235
236 #define DECLARE_SOCKADDR(type, dst, src) \
237 type dst = ({ __sockaddr_check_size(sizeof(*dst)); (type) src; })
238
239 struct net_proto_family {
240 int family;
241 int (*create)(struct net *net, struct socket *sock,
242 int protocol, int kern);
243 struct module *owner;
244 };
245
246 struct iovec;
247 struct kvec;
248
249 enum {
250 SOCK_WAKE_IO,
251 SOCK_WAKE_WAITD,
252 SOCK_WAKE_SPACE,
253 SOCK_WAKE_URG,
254 };
255
256 int sock_wake_async(struct socket_wq *sk_wq, int how, int band);
257 int sock_register(const struct net_proto_family *fam);
258 void sock_unregister(int family);
259 bool sock_is_registered(int family);
260 int __sock_create(struct net *net, int family, int type, int proto,
261 struct socket **res, int kern);
262 int sock_create(int family, int type, int proto, struct socket **res);
263 int sock_create_kern(struct net *net, int family, int type, int proto, struct socket **res);
264 int sock_create_lite(int family, int type, int proto, struct socket **res);
265 struct socket *sock_alloc(void);
266 void sock_release(struct socket *sock);
267 int sock_sendmsg(struct socket *sock, struct msghdr *msg);
268 int sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags);
269 struct file *sock_alloc_file(struct socket *sock, int flags, const char *dname);
270 struct socket *sockfd_lookup(int fd, int *err);
271 struct socket *sock_from_file(struct file *file);
272 #define sockfd_put(sock) fput(sock->file)
273 int net_ratelimit(void);
274
275 #define net_ratelimited_function(function, ...) \
276 do { \
277 if (net_ratelimit()) \
278 function(__VA_ARGS__); \
279 } while (0)
280
281 #define net_emerg_ratelimited(fmt, ...) \
282 net_ratelimited_function(pr_emerg, fmt, ##__VA_ARGS__)
283 #define net_alert_ratelimited(fmt, ...) \
284 net_ratelimited_function(pr_alert, fmt, ##__VA_ARGS__)
285 #define net_crit_ratelimited(fmt, ...) \
286 net_ratelimited_function(pr_crit, fmt, ##__VA_ARGS__)
287 #define net_err_ratelimited(fmt, ...) \
288 net_ratelimited_function(pr_err, fmt, ##__VA_ARGS__)
289 #define net_notice_ratelimited(fmt, ...) \
290 net_ratelimited_function(pr_notice, fmt, ##__VA_ARGS__)
291 #define net_warn_ratelimited(fmt, ...) \
292 net_ratelimited_function(pr_warn, fmt, ##__VA_ARGS__)
293 #define net_info_ratelimited(fmt, ...) \
294 net_ratelimited_function(pr_info, fmt, ##__VA_ARGS__)
295 #if defined(CONFIG_DYNAMIC_DEBUG) || \
296 (defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
297 #define net_dbg_ratelimited(fmt, ...) \
298 do { \
299 DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, fmt); \
300 if (DYNAMIC_DEBUG_BRANCH(descriptor) && \
301 net_ratelimit()) \
302 __dynamic_pr_debug(&descriptor, pr_fmt(fmt), \
303 ##__VA_ARGS__); \
304 } while (0)
305 #elif defined(DEBUG)
306 #define net_dbg_ratelimited(fmt, ...) \
307 net_ratelimited_function(pr_debug, fmt, ##__VA_ARGS__)
308 #else
309 #define net_dbg_ratelimited(fmt, ...) \
310 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
311 #endif
312
313 #define net_get_random_once(buf, nbytes) \
314 get_random_once((buf), (nbytes))
315
316 /*
317 * E.g. XFS meta- & log-data is in slab pages, or bcache meta
318 * data pages, or other high order pages allocated by
319 * __get_free_pages() without __GFP_COMP, which have a page_count
320 * of 0 and/or have PageSlab() set. We cannot use send_page for
321 * those, as that does get_page(); put_page(); and would cause
322 * either a VM_BUG directly, or __page_cache_release a page that
323 * would actually still be referenced by someone, leading to some
324 * obscure delayed Oops somewhere else.
325 */
sendpage_ok(struct page * page)326 static inline bool sendpage_ok(struct page *page)
327 {
328 return !PageSlab(page) && page_count(page) >= 1;
329 }
330
331 /*
332 * Check sendpage_ok on contiguous pages.
333 */
sendpages_ok(struct page * page,size_t len,size_t offset)334 static inline bool sendpages_ok(struct page *page, size_t len, size_t offset)
335 {
336 struct page *p = page + (offset >> PAGE_SHIFT);
337 size_t count = 0;
338
339 while (count < len) {
340 if (!sendpage_ok(p))
341 return false;
342
343 p++;
344 count += PAGE_SIZE;
345 }
346
347 return true;
348 }
349
350 int kernel_sendmsg(struct socket *sock, struct msghdr *msg, struct kvec *vec,
351 size_t num, size_t len);
352 int kernel_sendmsg_locked(struct sock *sk, struct msghdr *msg,
353 struct kvec *vec, size_t num, size_t len);
354 int kernel_recvmsg(struct socket *sock, struct msghdr *msg, struct kvec *vec,
355 size_t num, size_t len, int flags);
356
357 int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen);
358 int kernel_listen(struct socket *sock, int backlog);
359 int kernel_accept(struct socket *sock, struct socket **newsock, int flags);
360 int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
361 int flags);
362 int kernel_getsockname(struct socket *sock, struct sockaddr *addr);
363 int kernel_getpeername(struct socket *sock, struct sockaddr *addr);
364 int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how);
365
366 /* Routine returns the IP overhead imposed by a (caller-protected) socket. */
367 u32 kernel_sock_ip_overhead(struct sock *sk);
368
369 #define MODULE_ALIAS_NETPROTO(proto) \
370 MODULE_ALIAS("net-pf-" __stringify(proto))
371
372 #define MODULE_ALIAS_NET_PF_PROTO(pf, proto) \
373 MODULE_ALIAS("net-pf-" __stringify(pf) "-proto-" __stringify(proto))
374
375 #define MODULE_ALIAS_NET_PF_PROTO_TYPE(pf, proto, type) \
376 MODULE_ALIAS("net-pf-" __stringify(pf) "-proto-" __stringify(proto) \
377 "-type-" __stringify(type))
378
379 #define MODULE_ALIAS_NET_PF_PROTO_NAME(pf, proto, name) \
380 MODULE_ALIAS("net-pf-" __stringify(pf) "-proto-" __stringify(proto) \
381 name)
382 #endif /* _LINUX_NET_H */
383