blob: f7d672cf25b5a99c362885f5b2ff5cc64d5772d1 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001/* SPDX-License-Identifier: GPL-2.0-or-later */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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>
Jesper Juhl02c30a82005-05-05 16:16:16 -070011 * Ross Biro
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 */
14#ifndef _LINUX_NET_H
15#define _LINUX_NET_H
16
David Woodhouseeacf17b2006-04-25 14:46:09 +010017#include <linux/stringify.h>
David Woodhousecb4db4c2006-12-28 21:21:55 -080018#include <linux/random.h>
David Woodhouse5770a3f2008-08-26 15:29:22 +010019#include <linux/wait.h>
20#include <linux/fcntl.h> /* For O_CLOEXEC and O_NONBLOCK */
Eric Dumazet43815482010-04-29 11:01:49 +000021#include <linux/rcupdate.h>
Hannes Frederic Sowa46234252015-10-08 01:20:35 +020022#include <linux/once.h>
Tom Herbert0294b622016-08-28 14:43:17 -070023#include <linux/fs.h>
Hannes Frederic Sowa46234252015-10-08 01:20:35 +020024
David Howells607ca462012-10-13 10:46:48 +010025#include <uapi/linux/net.h>
David Woodhouse5770a3f2008-08-26 15:29:22 +010026
27struct poll_table_struct;
28struct pipe_inode_info;
29struct inode;
Al Viro56b31d12012-08-18 00:25:51 -040030struct file;
David Woodhouse5770a3f2008-08-26 15:29:22 +010031struct net;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Eric Dumazetceb5d582015-11-29 20:03:11 -080033/* Historically, SOCKWQ_ASYNC_NOSPACE & SOCKWQ_ASYNC_WAITDATA were located
34 * in sock->flags, but moved into sk->sk_wq->flags to be RCU protected.
Tonghao Zhangb3dc8f72017-08-15 04:28:54 -070035 * Eventually all flags will be in sk->sk_wq->flags.
Eric Dumazetceb5d582015-11-29 20:03:11 -080036 */
Eric Dumazet9cd3e072015-11-29 20:03:10 -080037#define SOCKWQ_ASYNC_NOSPACE 0
38#define SOCKWQ_ASYNC_WAITDATA 1
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#define SOCK_NOSPACE 2
40#define SOCK_PASSCRED 3
Catherine Zhang877ce7c2006-06-29 12:27:47 -070041#define SOCK_PASSSEC 4
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#ifndef ARCH_HAS_SOCKET_TYPES
Pavel Pisa4dc3b162005-05-01 08:59:25 -070044/**
45 * enum sock_type - Socket types
46 * @SOCK_STREAM: stream (connection) socket
47 * @SOCK_DGRAM: datagram (conn.less) socket
48 * @SOCK_RAW: raw socket
49 * @SOCK_RDM: reliably-delivered message
50 * @SOCK_SEQPACKET: sequential packet socket
Randy Dunlap8f2709b2005-11-07 01:01:05 -080051 * @SOCK_DCCP: Datagram Congestion Control Protocol socket
Pavel Pisa4dc3b162005-05-01 08:59:25 -070052 * @SOCK_PACKET: linux specific way of getting packets at the dev level.
53 * For writing rarp and other similar things on the user level.
54 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 * When adding some new socket type please
56 * grep ARCH_HAS_SOCKET_TYPE include/asm-* /socket.h, at least MIPS
57 * overrides this enum for binary compat reasons.
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 */
59enum sock_type {
60 SOCK_STREAM = 1,
61 SOCK_DGRAM = 2,
62 SOCK_RAW = 3,
63 SOCK_RDM = 4,
64 SOCK_SEQPACKET = 5,
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070065 SOCK_DCCP = 6,
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 SOCK_PACKET = 10,
67};
68
69#define SOCK_MAX (SOCK_PACKET + 1)
Ulrich Dreppera677a032008-07-23 21:29:17 -070070/* Mask which covers at least up to SOCK_MASK-1. The
71 * remaining bits are used as flags. */
72#define SOCK_TYPE_MASK 0xf
73
Ulrich Drepperde11def2008-11-19 15:36:14 -080074/* Flags for socket, socketpair, accept4 */
Ulrich Dreppera677a032008-07-23 21:29:17 -070075#define SOCK_CLOEXEC O_CLOEXEC
Ulrich Drepperc019bbc2008-07-23 21:29:21 -070076#ifndef SOCK_NONBLOCK
77#define SOCK_NONBLOCK O_NONBLOCK
78#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80#endif /* ARCH_HAS_SOCKET_TYPES */
81
Pedro Tammela8a3c2452019-03-14 10:45:23 -030082/**
83 * enum sock_shutdown_cmd - Shutdown types
84 * @SHUT_RD: shutdown receptions
85 * @SHUT_WR: shutdown transmissions
86 * @SHUT_RDWR: shutdown receptions/transmissions
87 */
Trond Myklebust91cf45f2007-11-12 18:10:39 -080088enum sock_shutdown_cmd {
Jean Sacren0e9649c2013-06-01 16:23:16 +000089 SHUT_RD,
90 SHUT_WR,
91 SHUT_RDWR,
Trond Myklebust91cf45f2007-11-12 18:10:39 -080092};
93
Eric Dumazet43815482010-04-29 11:01:49 +000094struct socket_wq {
Eric Dumazeteaefd1102011-02-18 03:26:36 +000095 /* Note: wait MUST be first field of socket_wq */
Eric Dumazet43815482010-04-29 11:01:49 +000096 wait_queue_head_t wait;
97 struct fasync_struct *fasync_list;
Eric Dumazetceb5d582015-11-29 20:03:11 -080098 unsigned long flags; /* %SOCKWQ_ASYNC_NOSPACE, etc */
Eric Dumazet43815482010-04-29 11:01:49 +000099 struct rcu_head rcu;
100} ____cacheline_aligned_in_smp;
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102/**
103 * struct socket - general BSD socket
Pavel Pisa4dc3b162005-05-01 08:59:25 -0700104 * @state: socket state (%SS_CONNECTED, etc)
Richard Kennedy2c693612008-07-08 03:03:01 -0700105 * @type: socket type (%SOCK_STREAM, etc)
Eric Dumazet9cd3e072015-11-29 20:03:10 -0800106 * @flags: socket flags (%SOCK_NOSPACE, etc)
Pavel Pisa4dc3b162005-05-01 08:59:25 -0700107 * @ops: protocol specific socket operations
Pavel Pisa4dc3b162005-05-01 08:59:25 -0700108 * @file: File back pointer for gc
109 * @sk: internal networking protocol agnostic socket representation
Randy Dunlape2aec372010-07-01 13:18:58 +0000110 * @wq: wait queue for several uses
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 */
112struct socket {
113 socket_state state;
Eric Dumazet29a020d2009-09-15 02:39:20 -0700114
Richard Kennedy2c693612008-07-08 03:03:01 -0700115 short type;
Eric Dumazet29a020d2009-09-15 02:39:20 -0700116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 unsigned long flags;
Eric Dumazet43815482010-04-29 11:01:49 +0000118
Christoph Hellwige6476c22018-07-30 09:45:07 +0200119 struct socket_wq *wq;
Eric Dumazet8bdd6632009-03-15 19:59:13 -0700120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 struct file *file;
122 struct sock *sk;
Eric Dumazet8bdd6632009-03-15 19:59:13 -0700123 const struct proto_ops *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124};
125
126struct vm_area_struct;
127struct page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128struct sockaddr;
129struct msghdr;
130struct module;
Tom Herbert0294b622016-08-28 14:43:17 -0700131struct sk_buff;
132typedef int (*sk_read_actor_t)(read_descriptor_t *, struct sk_buff *,
133 unsigned int, size_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135struct proto_ops {
136 int family;
137 struct module *owner;
138 int (*release) (struct socket *sock);
139 int (*bind) (struct socket *sock,
140 struct sockaddr *myaddr,
141 int sockaddr_len);
142 int (*connect) (struct socket *sock,
143 struct sockaddr *vaddr,
144 int sockaddr_len, int flags);
145 int (*socketpair)(struct socket *sock1,
146 struct socket *sock2);
147 int (*accept) (struct socket *sock,
David Howellscdfbabf2017-03-09 08:09:05 +0000148 struct socket *newsock, int flags, bool kern);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 int (*getname) (struct socket *sock,
150 struct sockaddr *addr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100151 int peer);
Al Viroa3f86832017-07-02 22:22:01 -0400152 __poll_t (*poll) (struct file *file, struct socket *sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 struct poll_table_struct *wait);
154 int (*ioctl) (struct socket *sock, unsigned int cmd,
155 unsigned long arg);
Alexey Dobriyan1621e092010-02-01 09:44:19 +0000156#ifdef CONFIG_COMPAT
Shaun Pereira89bbfc92006-03-21 23:58:08 -0800157 int (*compat_ioctl) (struct socket *sock, unsigned int cmd,
158 unsigned long arg);
Alexey Dobriyan1621e092010-02-01 09:44:19 +0000159#endif
Arnd Bergmannc7cbdbf2019-04-17 22:51:48 +0200160 int (*gettstamp) (struct socket *sock, void __user *userstamp,
161 bool timeval, bool time32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 int (*listen) (struct socket *sock, int len);
163 int (*shutdown) (struct socket *sock, int flags);
164 int (*setsockopt)(struct socket *sock, int level,
David S. Millerb7058842009-09-30 16:12:20 -0700165 int optname, char __user *optval, unsigned int optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 int (*getsockopt)(struct socket *sock, int level,
167 int optname, char __user *optval, int __user *optlen);
Alexey Dobriyan1621e092010-02-01 09:44:19 +0000168#ifdef CONFIG_COMPAT
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800169 int (*compat_setsockopt)(struct socket *sock, int level,
David S. Millerb7058842009-09-30 16:12:20 -0700170 int optname, char __user *optval, unsigned int optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800171 int (*compat_getsockopt)(struct socket *sock, int level,
172 int optname, char __user *optval, int __user *optlen);
Alexey Dobriyan1621e092010-02-01 09:44:19 +0000173#endif
Ying Xue1b784142015-03-02 15:37:48 +0800174 int (*sendmsg) (struct socket *sock, struct msghdr *m,
175 size_t total_len);
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +0100176 /* Notes for implementing recvmsg:
177 * ===============================
178 * msg->msg_namelen should get updated by the recvmsg handlers
179 * iff msg_name != NULL. It is by default 0 to prevent
180 * returning uninitialized memory to user space. The recvfrom
181 * handlers can assume that msg.msg_name is either NULL or has
182 * a minimum size of sizeof(struct sockaddr_storage).
183 */
Ying Xue1b784142015-03-02 15:37:48 +0800184 int (*recvmsg) (struct socket *sock, struct msghdr *m,
185 size_t total_len, int flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 int (*mmap) (struct file *file, struct socket *sock,
187 struct vm_area_struct * vma);
188 ssize_t (*sendpage) (struct socket *sock, struct page *page,
189 int offset, size_t size, int flags);
Jens Axboe9c55e012007-11-06 23:30:13 -0800190 ssize_t (*splice_read)(struct socket *sock, loff_t *ppos,
191 struct pipe_inode_info *pipe, size_t len, unsigned int flags);
Sasha Levin12663bf2013-12-07 17:26:27 -0500192 int (*set_peek_off)(struct sock *sk, int val);
Jason Wang1576d982016-06-30 14:45:36 +0800193 int (*peek_len)(struct socket *sock);
Tom Herbert306b13e2017-07-28 16:22:41 -0700194
195 /* The following functions are called internally by kernel with
196 * sock lock already held.
197 */
Tom Herbert0294b622016-08-28 14:43:17 -0700198 int (*read_sock)(struct sock *sk, read_descriptor_t *desc,
199 sk_read_actor_t recv_actor);
Tom Herbert306b13e2017-07-28 16:22:41 -0700200 int (*sendpage_locked)(struct sock *sk, struct page *page,
201 int offset, size_t size, int flags);
202 int (*sendmsg_locked)(struct sock *sk, struct msghdr *msg,
203 size_t size);
Eric Dumazetd1361842018-04-16 10:33:35 -0700204 int (*set_rcvlowat)(struct sock *sk, int val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205};
206
Cyrill Gorcunov38bfd8f2009-10-29 02:59:18 -0700207#define DECLARE_SOCKADDR(type, dst, src) \
208 type dst = ({ __sockaddr_check_size(sizeof(*dst)); (type) src; })
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210struct net_proto_family {
211 int family;
Eric Paris3f378b62009-11-05 22:18:14 -0800212 int (*create)(struct net *net, struct socket *sock,
213 int protocol, int kern);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 struct module *owner;
215};
216
217struct iovec;
218struct kvec;
219
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +0800220enum {
221 SOCK_WAKE_IO,
222 SOCK_WAKE_WAITD,
223 SOCK_WAKE_SPACE,
224 SOCK_WAKE_URG,
225};
226
Eric Dumazetceb5d582015-11-29 20:03:11 -0800227int sock_wake_async(struct socket_wq *sk_wq, int how, int band);
Joe Perches7965bd42013-09-26 14:48:15 -0700228int sock_register(const struct net_proto_family *fam);
229void sock_unregister(int family);
Xin Longbf2ae2e2018-03-10 18:57:50 +0800230bool sock_is_registered(int family);
Joe Perches7965bd42013-09-26 14:48:15 -0700231int __sock_create(struct net *net, int family, int type, int proto,
232 struct socket **res, int kern);
233int sock_create(int family, int type, int proto, struct socket **res);
Eric W. Biedermaneeb1bd52015-05-08 21:08:05 -0500234int sock_create_kern(struct net *net, int family, int type, int proto, struct socket **res);
Joe Perches7965bd42013-09-26 14:48:15 -0700235int sock_create_lite(int family, int type, int proto, struct socket **res);
Tom Herbertf4a00aa2016-03-07 14:11:01 -0800236struct socket *sock_alloc(void);
Joe Perches7965bd42013-09-26 14:48:15 -0700237void sock_release(struct socket *sock);
Al Virod8725c82014-12-11 00:02:50 -0500238int sock_sendmsg(struct socket *sock, struct msghdr *msg);
Al Viro2da62902015-03-14 21:13:46 -0400239int sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags);
Joe Perches7965bd42013-09-26 14:48:15 -0700240struct file *sock_alloc_file(struct socket *sock, int flags, const char *dname);
241struct socket *sockfd_lookup(int fd, int *err);
242struct socket *sock_from_file(struct file *file, int *err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243#define sockfd_put(sock) fput(sock->file)
Joe Perches7965bd42013-09-26 14:48:15 -0700244int net_ratelimit(void);
Stephen Hemmingeraaa248f2006-10-17 00:09:42 -0700245
Joe Perches3a3bfb62012-05-13 21:56:25 +0000246#define net_ratelimited_function(function, ...) \
247do { \
248 if (net_ratelimit()) \
249 function(__VA_ARGS__); \
250} while (0)
251
252#define net_emerg_ratelimited(fmt, ...) \
253 net_ratelimited_function(pr_emerg, fmt, ##__VA_ARGS__)
254#define net_alert_ratelimited(fmt, ...) \
255 net_ratelimited_function(pr_alert, fmt, ##__VA_ARGS__)
256#define net_crit_ratelimited(fmt, ...) \
257 net_ratelimited_function(pr_crit, fmt, ##__VA_ARGS__)
258#define net_err_ratelimited(fmt, ...) \
259 net_ratelimited_function(pr_err, fmt, ##__VA_ARGS__)
260#define net_notice_ratelimited(fmt, ...) \
261 net_ratelimited_function(pr_notice, fmt, ##__VA_ARGS__)
262#define net_warn_ratelimited(fmt, ...) \
263 net_ratelimited_function(pr_warn, fmt, ##__VA_ARGS__)
264#define net_info_ratelimited(fmt, ...) \
265 net_ratelimited_function(pr_info, fmt, ##__VA_ARGS__)
Tim Bingham2c94b532016-04-29 13:30:23 -0400266#if defined(CONFIG_DYNAMIC_DEBUG)
267#define net_dbg_ratelimited(fmt, ...) \
268do { \
269 DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, fmt); \
Rasmus Villemoes3f16d182019-03-07 16:27:25 -0800270 if (DYNAMIC_DEBUG_BRANCH(descriptor) && \
Tim Bingham2c94b532016-04-29 13:30:23 -0400271 net_ratelimit()) \
Jason A. Donenfelddaddef72016-06-15 11:14:53 +0200272 __dynamic_pr_debug(&descriptor, pr_fmt(fmt), \
273 ##__VA_ARGS__); \
Tim Bingham2c94b532016-04-29 13:30:23 -0400274} while (0)
275#elif defined(DEBUG)
Joe Perches3a3bfb62012-05-13 21:56:25 +0000276#define net_dbg_ratelimited(fmt, ...) \
277 net_ratelimited_function(pr_debug, fmt, ##__VA_ARGS__)
Jason A. Donenfeldd92cff82015-08-04 18:26:19 +0200278#else
279#define net_dbg_ratelimited(fmt, ...) \
280 do { \
281 if (0) \
282 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); \
283 } while (0)
284#endif
Joe Perches3a3bfb62012-05-13 21:56:25 +0000285
Hannes Frederic Sowa46234252015-10-08 01:20:35 +0200286#define net_get_random_once(buf, nbytes) \
287 get_random_once((buf), (nbytes))
Jason A. Donenfeldda9ba562017-06-07 20:05:02 -0400288#define net_get_random_once_wait(buf, nbytes) \
289 get_random_once_wait((buf), (nbytes))
Hannes Frederic Sowaa48e4292013-10-19 21:48:55 +0200290
Joe Perches7965bd42013-09-26 14:48:15 -0700291int kernel_sendmsg(struct socket *sock, struct msghdr *msg, struct kvec *vec,
292 size_t num, size_t len);
Tom Herbert306b13e2017-07-28 16:22:41 -0700293int kernel_sendmsg_locked(struct sock *sk, struct msghdr *msg,
294 struct kvec *vec, size_t num, size_t len);
Joe Perches7965bd42013-09-26 14:48:15 -0700295int kernel_recvmsg(struct socket *sock, struct msghdr *msg, struct kvec *vec,
296 size_t num, size_t len, int flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Joe Perches7965bd42013-09-26 14:48:15 -0700298int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen);
299int kernel_listen(struct socket *sock, int backlog);
300int kernel_accept(struct socket *sock, struct socket **newsock, int flags);
301int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
302 int flags);
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100303int kernel_getsockname(struct socket *sock, struct sockaddr *addr);
304int kernel_getpeername(struct socket *sock, struct sockaddr *addr);
Joe Perches7965bd42013-09-26 14:48:15 -0700305int kernel_getsockopt(struct socket *sock, int level, int optname, char *optval,
306 int *optlen);
307int kernel_setsockopt(struct socket *sock, int level, int optname, char *optval,
308 unsigned int optlen);
309int kernel_sendpage(struct socket *sock, struct page *page, int offset,
310 size_t size, int flags);
Tom Herbert306b13e2017-07-28 16:22:41 -0700311int kernel_sendpage_locked(struct sock *sk, struct page *page, int offset,
312 size_t size, int flags);
Joe Perches7965bd42013-09-26 14:48:15 -0700313int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -0700314
R. Parameswaran57240d02017-04-12 18:31:04 -0700315/* Routine returns the IP overhead imposed by a (caller-protected) socket. */
R. Parameswaran113c3072017-04-05 16:50:35 -0700316u32 kernel_sock_ip_overhead(struct sock *sk);
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318#define MODULE_ALIAS_NETPROTO(proto) \
319 MODULE_ALIAS("net-pf-" __stringify(proto))
320
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700321#define MODULE_ALIAS_NET_PF_PROTO(pf, proto) \
322 MODULE_ALIAS("net-pf-" __stringify(pf) "-proto-" __stringify(proto))
323
Jean Delvare305e1e92007-10-21 16:44:04 -0700324#define MODULE_ALIAS_NET_PF_PROTO_TYPE(pf, proto, type) \
325 MODULE_ALIAS("net-pf-" __stringify(pf) "-proto-" __stringify(proto) \
326 "-type-" __stringify(type))
327
Neil Horman2033e9b2012-05-29 09:30:40 +0000328#define MODULE_ALIAS_NET_PF_PROTO_NAME(pf, proto, name) \
329 MODULE_ALIAS("net-pf-" __stringify(pf) "-proto-" __stringify(proto) \
330 name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331#endif /* _LINUX_NET_H */