1 /* SPDX-License-Identifier: GPL-2.0-or-later */
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
8 * Version: @(#)net.h 1.0.3 05/25/93
10 * Authors: Orest Zborowski, <obz@Kodak.COM>
12 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
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>
25 #include <linux/sockptr.h>
27 #include <uapi/linux/net.h>
29 struct poll_table_struct
;
30 struct pipe_inode_info
;
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.
37 * Eventually all flags will be in sk->sk_wq->flags.
39 #define SOCKWQ_ASYNC_NOSPACE 0
40 #define SOCKWQ_ASYNC_WAITDATA 1
41 #define SOCK_NOSPACE 2
42 #define SOCK_PASSCRED 3
43 #define SOCK_PASSSEC 4
44 #define SOCK_SUPPORT_ZC 5
45 #define SOCK_CUSTOM_SOCKOPT 6
46 #define SOCK_PASSPIDFD 7
48 #ifndef ARCH_HAS_SOCKET_TYPES
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
56 * @SOCK_DCCP: Datagram Congestion Control Protocol socket
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.
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.
74 #define SOCK_MAX (SOCK_PACKET + 1)
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
79 /* Flags for socket, socketpair, accept4 */
80 #define SOCK_CLOEXEC O_CLOEXEC
82 #define SOCK_NONBLOCK O_NONBLOCK
85 #endif /* ARCH_HAS_SOCKET_TYPES */
88 * enum sock_shutdown_cmd - Shutdown types
89 * @SHUT_RD: shutdown receptions
90 * @SHUT_WR: shutdown transmissions
91 * @SHUT_RDWR: shutdown receptions/transmissions
93 enum sock_shutdown_cmd
{
100 /* Note: wait MUST be first field of socket_wq */
101 wait_queue_head_t wait
;
102 struct fasync_struct
*fasync_list
;
103 unsigned long flags
; /* %SOCKWQ_ASYNC_NOSPACE, etc */
105 } ____cacheline_aligned_in_smp
;
108 * struct socket - general BSD socket
109 * @state: socket state (%SS_CONNECTED, etc)
110 * @type: socket type (%SOCK_STREAM, etc)
111 * @flags: socket flags (%SOCK_NOSPACE, etc)
112 * @ops: protocol specific socket operations
113 * @file: File back pointer for gc
114 * @sk: internal networking protocol agnostic socket representation
115 * @wq: wait queue for several uses
126 const struct proto_ops
*ops
; /* Might change with IPV6_ADDRFORM or MPTCP. */
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.
137 * The simplest case just copies the data to user
150 struct vm_area_struct
;
156 struct proto_accept_arg
;
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
*);
164 struct module
*owner
;
165 int (*release
) (struct socket
*sock
);
166 int (*bind
) (struct socket
*sock
,
167 struct sockaddr
*myaddr
,
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
,
176 struct proto_accept_arg
*arg
);
177 int (*getname
) (struct socket
*sock
,
178 struct sockaddr
*addr
,
180 __poll_t (*poll
) (struct file
*file
, struct socket
*sock
,
181 struct poll_table_struct
*wait
);
182 int (*ioctl
) (struct socket
*sock
, unsigned int cmd
,
185 int (*compat_ioctl
) (struct socket
*sock
, unsigned int cmd
,
188 int (*gettstamp
) (struct socket
*sock
, void __user
*userstamp
,
189 bool timeval
, bool time32
);
190 int (*listen
) (struct socket
*sock
, int len
);
191 int (*shutdown
) (struct socket
*sock
, int flags
);
192 int (*setsockopt
)(struct socket
*sock
, int level
,
193 int optname
, sockptr_t optval
,
194 unsigned int optlen
);
195 int (*getsockopt
)(struct socket
*sock
, int level
,
196 int optname
, char __user
*optval
, int __user
*optlen
);
197 void (*show_fdinfo
)(struct seq_file
*m
, struct socket
*sock
);
198 int (*sendmsg
) (struct socket
*sock
, struct msghdr
*m
,
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).
208 int (*recvmsg
) (struct socket
*sock
, struct msghdr
*m
,
209 size_t total_len
, int flags
);
210 int (*mmap
) (struct file
*file
, struct socket
*sock
,
211 struct vm_area_struct
* vma
);
212 ssize_t (*splice_read
)(struct socket
*sock
, loff_t
*ppos
,
213 struct pipe_inode_info
*pipe
, size_t len
, unsigned int flags
);
214 void (*splice_eof
)(struct socket
*sock
);
215 int (*set_peek_off
)(struct sock
*sk
, int val
);
216 int (*peek_len
)(struct socket
*sock
);
218 /* The following functions are called internally by kernel with
219 * sock lock already held.
221 int (*read_sock
)(struct sock
*sk
, read_descriptor_t
*desc
,
222 sk_read_actor_t recv_actor
);
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
);
225 int (*sendmsg_locked
)(struct sock
*sk
, struct msghdr
*msg
,
227 int (*set_rcvlowat
)(struct sock
*sk
, int val
);
230 #define DECLARE_SOCKADDR(type, dst, src) \
231 type dst = ({ __sockaddr_check_size(sizeof(*dst)); (type) src; })
233 struct net_proto_family
{
235 int (*create
)(struct net
*net
, struct socket
*sock
,
236 int protocol
, int kern
);
237 struct module
*owner
;
250 int sock_wake_async(struct socket_wq
*sk_wq
, int how
, int band
);
251 int sock_register(const struct net_proto_family
*fam
);
252 void sock_unregister(int family
);
253 bool sock_is_registered(int family
);
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
);
257 int sock_create_kern(struct net
*net
, int family
, int type
, int proto
, struct socket
**res
);
258 int sock_create_lite(int family
, int type
, int proto
, struct socket
**res
);
259 struct socket
*sock_alloc(void);
260 void sock_release(struct socket
*sock
);
261 int sock_sendmsg(struct socket
*sock
, struct msghdr
*msg
);
262 int sock_recvmsg(struct socket
*sock
, struct msghdr
*msg
, int flags
);
263 struct file
*sock_alloc_file(struct socket
*sock
, int flags
, const char *dname
);
264 struct socket
*sockfd_lookup(int fd
, int *err
);
265 struct socket
*sock_from_file(struct file
*file
);
266 #define sockfd_put(sock) fput(sock->file)
267 int net_ratelimit(void);
269 #define net_ratelimited_function(function, ...) \
271 if (net_ratelimit()) \
272 function(__VA_ARGS__); \
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__)
289 #if defined(CONFIG_DYNAMIC_DEBUG) || \
290 (defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
291 #define net_dbg_ratelimited(fmt, ...) \
293 DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, fmt); \
294 if (DYNAMIC_DEBUG_BRANCH(descriptor) && \
296 __dynamic_pr_debug(&descriptor, pr_fmt(fmt), \
300 #define net_dbg_ratelimited(fmt, ...) \
301 net_ratelimited_function(pr_debug, fmt, ##__VA_ARGS__)
303 #define net_dbg_ratelimited(fmt, ...) \
304 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
307 #define net_get_random_once(buf, nbytes) \
308 get_random_once((buf), (nbytes))
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.
320 static inline bool sendpage_ok(struct page
*page
)
322 return !PageSlab(page
) && page_count(page
) >= 1;
326 * Check sendpage_ok on contiguous pages.
328 static inline bool sendpages_ok(struct page
*page
, size_t len
, size_t offset
)
330 struct page
*p
= page
+ (offset
>> PAGE_SHIFT
);
333 while (count
< len
) {
344 int kernel_sendmsg(struct socket
*sock
, struct msghdr
*msg
, struct kvec
*vec
,
345 size_t num
, size_t len
);
346 int kernel_sendmsg_locked(struct sock
*sk
, struct msghdr
*msg
,
347 struct kvec
*vec
, size_t num
, size_t len
);
348 int kernel_recvmsg(struct socket
*sock
, struct msghdr
*msg
, struct kvec
*vec
,
349 size_t num
, size_t len
, int flags
);
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
,
356 int kernel_getsockname(struct socket
*sock
, struct sockaddr
*addr
);
357 int kernel_getpeername(struct socket
*sock
, struct sockaddr
*addr
);
358 int kernel_sock_shutdown(struct socket
*sock
, enum sock_shutdown_cmd how
);
360 /* Routine returns the IP overhead imposed by a (caller-protected) socket. */
361 u32
kernel_sock_ip_overhead(struct sock
*sk
);
363 #define MODULE_ALIAS_NETPROTO(proto) \
364 MODULE_ALIAS("net-pf-" __stringify(proto))
366 #define MODULE_ALIAS_NET_PF_PROTO(pf, proto) \
367 MODULE_ALIAS("net-pf-" __stringify(pf) "-proto-" __stringify(proto))
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))
373 #define MODULE_ALIAS_NET_PF_PROTO_NAME(pf, proto, name) \
374 MODULE_ALIAS("net-pf-" __stringify(pf) "-proto-" __stringify(proto) \
376 #endif /* _LINUX_NET_H */