Commit | Line | Data |
---|---|---|
2874c5fd | 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
1da177e4 LT |
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> | |
02c30a84 | 11 | * Ross Biro |
1da177e4 | 12 | * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> |
1da177e4 LT |
13 | */ |
14 | #ifndef _LINUX_NET_H | |
15 | #define _LINUX_NET_H | |
16 | ||
eacf17bd | 17 | #include <linux/stringify.h> |
cb4db4c2 | 18 | #include <linux/random.h> |
5770a3fb DW |
19 | #include <linux/wait.h> |
20 | #include <linux/fcntl.h> /* For O_CLOEXEC and O_NONBLOCK */ | |
43815482 | 21 | #include <linux/rcupdate.h> |
46234253 | 22 | #include <linux/once.h> |
0294b625 | 23 | #include <linux/fs.h> |
c381b079 | 24 | #include <linux/mm.h> |
a7b75c5a | 25 | #include <linux/sockptr.h> |
46234253 | 26 | |
607ca46e | 27 | #include <uapi/linux/net.h> |
5770a3fb DW |
28 | |
29 | struct poll_table_struct; | |
30 | struct pipe_inode_info; | |
31 | struct inode; | |
56b31d1c | 32 | struct file; |
5770a3fb | 33 | struct net; |
1da177e4 | 34 | |
ceb5d58b ED |
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. | |
b3dc8f77 | 37 | * Eventually all flags will be in sk->sk_wq->flags. |
ceb5d58b | 38 | */ |
9cd3e072 ED |
39 | #define SOCKWQ_ASYNC_NOSPACE 0 |
40 | #define SOCKWQ_ASYNC_WAITDATA 1 | |
1da177e4 LT |
41 | #define SOCK_NOSPACE 2 |
42 | #define SOCK_PASSCRED 3 | |
877ce7c1 | 43 | #define SOCK_PASSSEC 4 |
e993ffe3 | 44 | #define SOCK_SUPPORT_ZC 5 |
96917bb3 | 45 | #define SOCK_CUSTOM_SOCKOPT 6 |
5e2ff670 | 46 | #define SOCK_PASSPIDFD 7 |
1da177e4 LT |
47 | |
48 | #ifndef ARCH_HAS_SOCKET_TYPES | |
4dc3b16b PP |
49 | /** |
50 | * enum sock_type - Socket types | |
51 | * @SOCK_STREAM: stream (connection) socket | |
52 | * @SOCK_DGRAM: datagram (conn.less) socket | |
53 | * @SOCK_RAW: raw socket | |
54 | * @SOCK_RDM: reliably-delivered message | |
55 | * @SOCK_SEQPACKET: sequential packet socket | |
8f2709b5 | 56 | * @SOCK_DCCP: Datagram Congestion Control Protocol socket |
4dc3b16b PP |
57 | * @SOCK_PACKET: linux specific way of getting packets at the dev level. |
58 | * For writing rarp and other similar things on the user level. | |
59 | * | |
1da177e4 LT |
60 | * When adding some new socket type please |
61 | * grep ARCH_HAS_SOCKET_TYPE include/asm-* /socket.h, at least MIPS | |
62 | * overrides this enum for binary compat reasons. | |
1da177e4 LT |
63 | */ |
64 | enum sock_type { | |
65 | SOCK_STREAM = 1, | |
66 | SOCK_DGRAM = 2, | |
67 | SOCK_RAW = 3, | |
68 | SOCK_RDM = 4, | |
69 | SOCK_SEQPACKET = 5, | |
7c657876 | 70 | SOCK_DCCP = 6, |
1da177e4 LT |
71 | SOCK_PACKET = 10, |
72 | }; | |
73 | ||
74 | #define SOCK_MAX (SOCK_PACKET + 1) | |
a677a039 UD |
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 | ||
de11defe | 79 | /* Flags for socket, socketpair, accept4 */ |
a677a039 | 80 | #define SOCK_CLOEXEC O_CLOEXEC |
c019bbc6 UD |
81 | #ifndef SOCK_NONBLOCK |
82 | #define SOCK_NONBLOCK O_NONBLOCK | |
83 | #endif | |
1da177e4 LT |
84 | |
85 | #endif /* ARCH_HAS_SOCKET_TYPES */ | |
86 | ||
8a3c245c PT |
87 | /** |
88 | * enum sock_shutdown_cmd - Shutdown types | |
89 | * @SHUT_RD: shutdown receptions | |
90 | * @SHUT_WR: shutdown transmissions | |
91 | * @SHUT_RDWR: shutdown receptions/transmissions | |
92 | */ | |
91cf45f0 | 93 | enum sock_shutdown_cmd { |
0e9649c1 JS |
94 | SHUT_RD, |
95 | SHUT_WR, | |
96 | SHUT_RDWR, | |
91cf45f0 TM |
97 | }; |
98 | ||
43815482 | 99 | struct socket_wq { |
eaefd110 | 100 | /* Note: wait MUST be first field of socket_wq */ |
43815482 ED |
101 | wait_queue_head_t wait; |
102 | struct fasync_struct *fasync_list; | |
ceb5d58b | 103 | unsigned long flags; /* %SOCKWQ_ASYNC_NOSPACE, etc */ |
43815482 ED |
104 | struct rcu_head rcu; |
105 | } ____cacheline_aligned_in_smp; | |
106 | ||
1da177e4 LT |
107 | /** |
108 | * struct socket - general BSD socket | |
4dc3b16b | 109 | * @state: socket state (%SS_CONNECTED, etc) |
2c693610 | 110 | * @type: socket type (%SOCK_STREAM, etc) |
9cd3e072 | 111 | * @flags: socket flags (%SOCK_NOSPACE, etc) |
4dc3b16b | 112 | * @ops: protocol specific socket operations |
4dc3b16b PP |
113 | * @file: File back pointer for gc |
114 | * @sk: internal networking protocol agnostic socket representation | |
e2aec372 | 115 | * @wq: wait queue for several uses |
1da177e4 LT |
116 | */ |
117 | struct socket { | |
118 | socket_state state; | |
29a020d3 | 119 | |
2c693610 | 120 | short type; |
29a020d3 | 121 | |
1da177e4 | 122 | unsigned long flags; |
43815482 | 123 | |
1da177e4 LT |
124 | struct file *file; |
125 | struct sock *sk; | |
1ded5e5a | 126 | const struct proto_ops *ops; /* Might change with IPV6_ADDRFORM or MPTCP. */ |
333f7909 AV |
127 | |
128 | struct socket_wq wq; | |
1da177e4 LT |
129 | }; |
130 | ||
b2403a61 MWO |
131 | /* |
132 | * "descriptor" for what we're up to with a read. | |
133 | * This allows us to use the same read code yet | |
134 | * have multiple different users of the data that | |
135 | * we read from a file. | |
136 | * | |
137 | * The simplest case just copies the data to user | |
138 | * mode. | |
139 | */ | |
140 | typedef struct { | |
141 | size_t written; | |
142 | size_t count; | |
143 | union { | |
144 | char __user *buf; | |
145 | void *data; | |
146 | } arg; | |
147 | int error; | |
148 | } read_descriptor_t; | |
149 | ||
1da177e4 LT |
150 | struct vm_area_struct; |
151 | struct page; | |
1da177e4 LT |
152 | struct sockaddr; |
153 | struct msghdr; | |
154 | struct module; | |
0294b625 | 155 | struct sk_buff; |
92ef0fd5 | 156 | struct proto_accept_arg; |
0294b625 TH |
157 | typedef int (*sk_read_actor_t)(read_descriptor_t *, struct sk_buff *, |
158 | unsigned int, size_t); | |
965b57b4 CW |
159 | typedef int (*skb_read_actor_t)(struct sock *, struct sk_buff *); |
160 | ||
1da177e4 LT |
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, | |
92ef0fd5 JA |
175 | struct socket *newsock, |
176 | struct proto_accept_arg *arg); | |
1da177e4 LT |
177 | int (*getname) (struct socket *sock, |
178 | struct sockaddr *addr, | |
9b2c45d4 | 179 | int peer); |
a3f8683b | 180 | __poll_t (*poll) (struct file *file, struct socket *sock, |
1da177e4 LT |
181 | struct poll_table_struct *wait); |
182 | int (*ioctl) (struct socket *sock, unsigned int cmd, | |
183 | unsigned long arg); | |
1621e094 | 184 | #ifdef CONFIG_COMPAT |
89bbfc95 SP |
185 | int (*compat_ioctl) (struct socket *sock, unsigned int cmd, |
186 | unsigned long arg); | |
1621e094 | 187 | #endif |
c7cbdbf2 AB |
188 | int (*gettstamp) (struct socket *sock, void __user *userstamp, |
189 | bool timeval, bool time32); | |
1da177e4 LT |
190 | int (*listen) (struct socket *sock, int len); |
191 | int (*shutdown) (struct socket *sock, int flags); | |
192 | int (*setsockopt)(struct socket *sock, int level, | |
a7b75c5a CH |
193 | int optname, sockptr_t optval, |
194 | unsigned int optlen); | |
1da177e4 LT |
195 | int (*getsockopt)(struct socket *sock, int level, |
196 | int optname, char __user *optval, int __user *optlen); | |
b4653342 | 197 | void (*show_fdinfo)(struct seq_file *m, struct socket *sock); |
1b784140 YX |
198 | int (*sendmsg) (struct socket *sock, struct msghdr *m, |
199 | size_t total_len); | |
f3d33426 HFS |
200 | /* Notes for implementing recvmsg: |
201 | * =============================== | |
202 | * msg->msg_namelen should get updated by the recvmsg handlers | |
203 | * iff msg_name != NULL. It is by default 0 to prevent | |
204 | * returning uninitialized memory to user space. The recvfrom | |
205 | * handlers can assume that msg.msg_name is either NULL or has | |
206 | * a minimum size of sizeof(struct sockaddr_storage). | |
207 | */ | |
1b784140 YX |
208 | int (*recvmsg) (struct socket *sock, struct msghdr *m, |
209 | size_t total_len, int flags); | |
1da177e4 LT |
210 | int (*mmap) (struct file *file, struct socket *sock, |
211 | struct vm_area_struct * vma); | |
9c55e01c JA |
212 | ssize_t (*splice_read)(struct socket *sock, loff_t *ppos, |
213 | struct pipe_inode_info *pipe, size_t len, unsigned int flags); | |
2bfc6685 | 214 | void (*splice_eof)(struct socket *sock); |
12663bfc | 215 | int (*set_peek_off)(struct sock *sk, int val); |
1576d986 | 216 | int (*peek_len)(struct socket *sock); |
306b13eb TH |
217 | |
218 | /* The following functions are called internally by kernel with | |
219 | * sock lock already held. | |
220 | */ | |
0294b625 TH |
221 | int (*read_sock)(struct sock *sk, read_descriptor_t *desc, |
222 | sk_read_actor_t recv_actor); | |
965b57b4 CW |
223 | /* This is different from read_sock(), it reads an entire skb at a time. */ |
224 | int (*read_skb)(struct sock *sk, skb_read_actor_t recv_actor); | |
306b13eb TH |
225 | int (*sendmsg_locked)(struct sock *sk, struct msghdr *msg, |
226 | size_t size); | |
d1361840 | 227 | int (*set_rcvlowat)(struct sock *sk, int val); |
1da177e4 LT |
228 | }; |
229 | ||
38bfd8f5 CG |
230 | #define DECLARE_SOCKADDR(type, dst, src) \ |
231 | type dst = ({ __sockaddr_check_size(sizeof(*dst)); (type) src; }) | |
232 | ||
1da177e4 LT |
233 | struct net_proto_family { |
234 | int family; | |
3f378b68 EP |
235 | int (*create)(struct net *net, struct socket *sock, |
236 | int protocol, int kern); | |
1da177e4 LT |
237 | struct module *owner; |
238 | }; | |
239 | ||
240 | struct iovec; | |
241 | struct kvec; | |
242 | ||
8d8ad9d7 PE |
243 | enum { |
244 | SOCK_WAKE_IO, | |
245 | SOCK_WAKE_WAITD, | |
246 | SOCK_WAKE_SPACE, | |
247 | SOCK_WAKE_URG, | |
248 | }; | |
249 | ||
ceb5d58b | 250 | int sock_wake_async(struct socket_wq *sk_wq, int how, int band); |
7965bd4d JP |
251 | int sock_register(const struct net_proto_family *fam); |
252 | void sock_unregister(int family); | |
bf2ae2e4 | 253 | bool sock_is_registered(int family); |
7965bd4d JP |
254 | int __sock_create(struct net *net, int family, int type, int proto, |
255 | struct socket **res, int kern); | |
256 | int sock_create(int family, int type, int proto, struct socket **res); | |
eeb1bd5c | 257 | int sock_create_kern(struct net *net, int family, int type, int proto, struct socket **res); |
7965bd4d | 258 | int sock_create_lite(int family, int type, int proto, struct socket **res); |
f4a00aac | 259 | struct socket *sock_alloc(void); |
7965bd4d | 260 | void sock_release(struct socket *sock); |
d8725c86 | 261 | int sock_sendmsg(struct socket *sock, struct msghdr *msg); |
2da62906 | 262 | int sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags); |
7965bd4d JP |
263 | struct file *sock_alloc_file(struct socket *sock, int flags, const char *dname); |
264 | struct socket *sockfd_lookup(int fd, int *err); | |
dba4a925 | 265 | struct socket *sock_from_file(struct file *file); |
1da177e4 | 266 | #define sockfd_put(sock) fput(sock->file) |
7965bd4d | 267 | int net_ratelimit(void); |
aaa248f6 | 268 | |
3a3bfb61 JP |
269 | #define net_ratelimited_function(function, ...) \ |
270 | do { \ | |
271 | if (net_ratelimit()) \ | |
272 | function(__VA_ARGS__); \ | |
273 | } while (0) | |
274 | ||
275 | #define net_emerg_ratelimited(fmt, ...) \ | |
276 | net_ratelimited_function(pr_emerg, fmt, ##__VA_ARGS__) | |
277 | #define net_alert_ratelimited(fmt, ...) \ | |
278 | net_ratelimited_function(pr_alert, fmt, ##__VA_ARGS__) | |
279 | #define net_crit_ratelimited(fmt, ...) \ | |
280 | net_ratelimited_function(pr_crit, fmt, ##__VA_ARGS__) | |
281 | #define net_err_ratelimited(fmt, ...) \ | |
282 | net_ratelimited_function(pr_err, fmt, ##__VA_ARGS__) | |
283 | #define net_notice_ratelimited(fmt, ...) \ | |
284 | net_ratelimited_function(pr_notice, fmt, ##__VA_ARGS__) | |
285 | #define net_warn_ratelimited(fmt, ...) \ | |
286 | net_ratelimited_function(pr_warn, fmt, ##__VA_ARGS__) | |
287 | #define net_info_ratelimited(fmt, ...) \ | |
288 | net_ratelimited_function(pr_info, fmt, ##__VA_ARGS__) | |
ceabef7d OZ |
289 | #if defined(CONFIG_DYNAMIC_DEBUG) || \ |
290 | (defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE)) | |
2c94b537 TB |
291 | #define net_dbg_ratelimited(fmt, ...) \ |
292 | do { \ | |
293 | DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, fmt); \ | |
3f16d181 | 294 | if (DYNAMIC_DEBUG_BRANCH(descriptor) && \ |
2c94b537 | 295 | net_ratelimit()) \ |
daddef76 JD |
296 | __dynamic_pr_debug(&descriptor, pr_fmt(fmt), \ |
297 | ##__VA_ARGS__); \ | |
2c94b537 TB |
298 | } while (0) |
299 | #elif defined(DEBUG) | |
3a3bfb61 JP |
300 | #define net_dbg_ratelimited(fmt, ...) \ |
301 | net_ratelimited_function(pr_debug, fmt, ##__VA_ARGS__) | |
d92cff89 JD |
302 | #else |
303 | #define net_dbg_ratelimited(fmt, ...) \ | |
f29f9199 | 304 | no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__) |
d92cff89 | 305 | #endif |
3a3bfb61 | 306 | |
46234253 HFS |
307 | #define net_get_random_once(buf, nbytes) \ |
308 | get_random_once((buf), (nbytes)) | |
a48e4292 | 309 | |
c381b079 CL |
310 | /* |
311 | * E.g. XFS meta- & log-data is in slab pages, or bcache meta | |
312 | * data pages, or other high order pages allocated by | |
313 | * __get_free_pages() without __GFP_COMP, which have a page_count | |
314 | * of 0 and/or have PageSlab() set. We cannot use send_page for | |
315 | * those, as that does get_page(); put_page(); and would cause | |
316 | * either a VM_BUG directly, or __page_cache_release a page that | |
317 | * would actually still be referenced by someone, leading to some | |
318 | * obscure delayed Oops somewhere else. | |
319 | */ | |
320 | static inline bool sendpage_ok(struct page *page) | |
321 | { | |
322 | return !PageSlab(page) && page_count(page) >= 1; | |
323 | } | |
324 | ||
23a55f44 OG |
325 | /* |
326 | * Check sendpage_ok on contiguous pages. | |
327 | */ | |
328 | static inline bool sendpages_ok(struct page *page, size_t len, size_t offset) | |
329 | { | |
330 | struct page *p = page + (offset >> PAGE_SHIFT); | |
331 | size_t count = 0; | |
332 | ||
333 | while (count < len) { | |
334 | if (!sendpage_ok(p)) | |
335 | return false; | |
336 | ||
337 | p++; | |
338 | count += PAGE_SIZE; | |
339 | } | |
340 | ||
341 | return true; | |
342 | } | |
343 | ||
7965bd4d JP |
344 | int kernel_sendmsg(struct socket *sock, struct msghdr *msg, struct kvec *vec, |
345 | size_t num, size_t len); | |
306b13eb TH |
346 | int kernel_sendmsg_locked(struct sock *sk, struct msghdr *msg, |
347 | struct kvec *vec, size_t num, size_t len); | |
7965bd4d JP |
348 | int kernel_recvmsg(struct socket *sock, struct msghdr *msg, struct kvec *vec, |
349 | size_t num, size_t len, int flags); | |
1da177e4 | 350 | |
7965bd4d JP |
351 | int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen); |
352 | int kernel_listen(struct socket *sock, int backlog); | |
353 | int kernel_accept(struct socket *sock, struct socket **newsock, int flags); | |
354 | int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen, | |
355 | int flags); | |
9b2c45d4 DV |
356 | int kernel_getsockname(struct socket *sock, struct sockaddr *addr); |
357 | int kernel_getpeername(struct socket *sock, struct sockaddr *addr); | |
7965bd4d | 358 | int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how); |
ac5a488e | 359 | |
57240d00 | 360 | /* Routine returns the IP overhead imposed by a (caller-protected) socket. */ |
113c3075 P |
361 | u32 kernel_sock_ip_overhead(struct sock *sk); |
362 | ||
1da177e4 LT |
363 | #define MODULE_ALIAS_NETPROTO(proto) \ |
364 | MODULE_ALIAS("net-pf-" __stringify(proto)) | |
365 | ||
4fdb3bb7 HW |
366 | #define MODULE_ALIAS_NET_PF_PROTO(pf, proto) \ |
367 | MODULE_ALIAS("net-pf-" __stringify(pf) "-proto-" __stringify(proto)) | |
368 | ||
305e1e96 JD |
369 | #define MODULE_ALIAS_NET_PF_PROTO_TYPE(pf, proto, type) \ |
370 | MODULE_ALIAS("net-pf-" __stringify(pf) "-proto-" __stringify(proto) \ | |
371 | "-type-" __stringify(type)) | |
372 | ||
2033e9bf NH |
373 | #define MODULE_ALIAS_NET_PF_PROTO_NAME(pf, proto, name) \ |
374 | MODULE_ALIAS("net-pf-" __stringify(pf) "-proto-" __stringify(proto) \ | |
375 | name) | |
1da177e4 | 376 | #endif /* _LINUX_NET_H */ |