1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * NET An implementation of the SOCKET network access protocol.
5 * Version: @(#)socket.c 1.1.93 18/02/95
7 * Authors: Orest Zborowski, <obz@Kodak.COM>
9 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
12 * Anonymous : NOTSOCK/BADF cleanup. Error fix in
14 * Alan Cox : verify_area() fixes
15 * Alan Cox : Removed DDI
16 * Jonathan Kamens : SOCK_DGRAM reconnect bug
17 * Alan Cox : Moved a load of checks to the very
19 * Alan Cox : Move address structures to/from user
20 * mode above the protocol layers.
21 * Rob Janssen : Allow 0 length sends.
22 * Alan Cox : Asynchronous I/O support (cribbed from the
24 * Niibe Yutaka : Asynchronous I/O for writes (4.4BSD style)
25 * Jeff Uphoff : Made max number of sockets command-line
27 * Matti Aarnio : Made the number of sockets dynamic,
28 * to be allocated when needed, and mr.
29 * Uphoff's max is used as max to be
30 * allowed to allocate.
31 * Linus : Argh. removed all the socket allocation
32 * altogether: it's in the inode now.
33 * Alan Cox : Made sock_alloc()/sock_release() public
34 * for NetROM and future kernel nfsd type
36 * Alan Cox : sendmsg/recvmsg basics.
37 * Tom Dyas : Export net symbols.
38 * Marcin Dalecki : Fixed problems with CONFIG_NET="n".
39 * Alan Cox : Added thread locking to sys_* calls
40 * for sockets. May have errors at the
42 * Kevin Buhr : Fixed the dumb errors in the above.
43 * Andi Kleen : Some small cleanups, optimizations,
44 * and fixed a copy_from_user() bug.
45 * Tigran Aivazian : sys_send(args) calls sys_sendto(args, NULL, 0)
46 * Tigran Aivazian : Made listen(2) backlog sanity checks
47 * protocol-independent
49 * This module is effectively the top level interface to the BSD socket
52 * Based upon Swansea University Computer Society NET3.039
55 #include <linux/bpf-cgroup.h>
56 #include <linux/ethtool.h>
58 #include <linux/socket.h>
59 #include <linux/file.h>
60 #include <linux/splice.h>
61 #include <linux/net.h>
62 #include <linux/interrupt.h>
63 #include <linux/thread_info.h>
64 #include <linux/rcupdate.h>
65 #include <linux/netdevice.h>
66 #include <linux/proc_fs.h>
67 #include <linux/seq_file.h>
68 #include <linux/mutex.h>
69 #include <linux/if_bridge.h>
70 #include <linux/if_vlan.h>
71 #include <linux/ptp_classify.h>
72 #include <linux/init.h>
73 #include <linux/poll.h>
74 #include <linux/cache.h>
75 #include <linux/module.h>
76 #include <linux/highmem.h>
77 #include <linux/mount.h>
78 #include <linux/pseudo_fs.h>
79 #include <linux/security.h>
80 #include <linux/syscalls.h>
81 #include <linux/compat.h>
82 #include <linux/kmod.h>
83 #include <linux/audit.h>
84 #include <linux/wireless.h>
85 #include <linux/nsproxy.h>
86 #include <linux/magic.h>
87 #include <linux/slab.h>
88 #include <linux/xattr.h>
89 #include <linux/nospec.h>
90 #include <linux/indirect_call_wrapper.h>
91 #include <linux/io_uring/net.h>
93 #include <linux/uaccess.h>
94 #include <asm/unistd.h>
96 #include <net/compat.h>
98 #include <net/cls_cgroup.h>
100 #include <net/sock.h>
101 #include <linux/netfilter.h>
103 #include <linux/if_tun.h>
104 #include <linux/ipv6_route.h>
105 #include <linux/route.h>
106 #include <linux/termios.h>
107 #include <linux/sockios.h>
108 #include <net/busy_poll.h>
109 #include <linux/errqueue.h>
110 #include <linux/ptp_clock_kernel.h>
111 #include <trace/events/sock.h>
113 #include "core/dev.h"
115 #ifdef CONFIG_NET_RX_BUSY_POLL
116 unsigned int sysctl_net_busy_read __read_mostly
;
117 unsigned int sysctl_net_busy_poll __read_mostly
;
120 static ssize_t
sock_read_iter(struct kiocb
*iocb
, struct iov_iter
*to
);
121 static ssize_t
sock_write_iter(struct kiocb
*iocb
, struct iov_iter
*from
);
122 static int sock_mmap(struct file
*file
, struct vm_area_struct
*vma
);
124 static int sock_close(struct inode
*inode
, struct file
*file
);
125 static __poll_t
sock_poll(struct file
*file
,
126 struct poll_table_struct
*wait
);
127 static long sock_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
);
129 static long compat_sock_ioctl(struct file
*file
,
130 unsigned int cmd
, unsigned long arg
);
132 static int sock_fasync(int fd
, struct file
*filp
, int on
);
133 static ssize_t
sock_splice_read(struct file
*file
, loff_t
*ppos
,
134 struct pipe_inode_info
*pipe
, size_t len
,
136 static void sock_splice_eof(struct file
*file
);
138 #ifdef CONFIG_PROC_FS
139 static void sock_show_fdinfo(struct seq_file
*m
, struct file
*f
)
141 struct socket
*sock
= f
->private_data
;
142 const struct proto_ops
*ops
= READ_ONCE(sock
->ops
);
144 if (ops
->show_fdinfo
)
145 ops
->show_fdinfo(m
, sock
);
148 #define sock_show_fdinfo NULL
152 * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
153 * in the operation structures but are done directly via the socketcall() multiplexor.
156 static const struct file_operations socket_file_ops
= {
157 .owner
= THIS_MODULE
,
158 .read_iter
= sock_read_iter
,
159 .write_iter
= sock_write_iter
,
161 .unlocked_ioctl
= sock_ioctl
,
163 .compat_ioctl
= compat_sock_ioctl
,
165 .uring_cmd
= io_uring_cmd_sock
,
167 .release
= sock_close
,
168 .fasync
= sock_fasync
,
169 .splice_write
= splice_to_socket
,
170 .splice_read
= sock_splice_read
,
171 .splice_eof
= sock_splice_eof
,
172 .show_fdinfo
= sock_show_fdinfo
,
175 static const char * const pf_family_names
[] = {
176 [PF_UNSPEC
] = "PF_UNSPEC",
177 [PF_UNIX
] = "PF_UNIX/PF_LOCAL",
178 [PF_INET
] = "PF_INET",
179 [PF_AX25
] = "PF_AX25",
181 [PF_APPLETALK
] = "PF_APPLETALK",
182 [PF_NETROM
] = "PF_NETROM",
183 [PF_BRIDGE
] = "PF_BRIDGE",
184 [PF_ATMPVC
] = "PF_ATMPVC",
186 [PF_INET6
] = "PF_INET6",
187 [PF_ROSE
] = "PF_ROSE",
188 [PF_DECnet
] = "PF_DECnet",
189 [PF_NETBEUI
] = "PF_NETBEUI",
190 [PF_SECURITY
] = "PF_SECURITY",
192 [PF_NETLINK
] = "PF_NETLINK/PF_ROUTE",
193 [PF_PACKET
] = "PF_PACKET",
195 [PF_ECONET
] = "PF_ECONET",
196 [PF_ATMSVC
] = "PF_ATMSVC",
199 [PF_IRDA
] = "PF_IRDA",
200 [PF_PPPOX
] = "PF_PPPOX",
201 [PF_WANPIPE
] = "PF_WANPIPE",
204 [PF_MPLS
] = "PF_MPLS",
206 [PF_TIPC
] = "PF_TIPC",
207 [PF_BLUETOOTH
] = "PF_BLUETOOTH",
208 [PF_IUCV
] = "PF_IUCV",
209 [PF_RXRPC
] = "PF_RXRPC",
210 [PF_ISDN
] = "PF_ISDN",
211 [PF_PHONET
] = "PF_PHONET",
212 [PF_IEEE802154
] = "PF_IEEE802154",
213 [PF_CAIF
] = "PF_CAIF",
216 [PF_VSOCK
] = "PF_VSOCK",
218 [PF_QIPCRTR
] = "PF_QIPCRTR",
221 [PF_MCTP
] = "PF_MCTP",
225 * The protocol list. Each protocol is registered in here.
228 static DEFINE_SPINLOCK(net_family_lock
);
229 static const struct net_proto_family __rcu
*net_families
[NPROTO
] __read_mostly
;
233 * Move socket addresses back and forth across the kernel/user
234 * divide and look after the messy bits.
238 * move_addr_to_kernel - copy a socket address into kernel space
239 * @uaddr: Address in user space
240 * @kaddr: Address in kernel space
241 * @ulen: Length in user space
243 * The address is copied into kernel space. If the provided address is
244 * too long an error code of -EINVAL is returned. If the copy gives
245 * invalid addresses -EFAULT is returned. On a success 0 is returned.
248 int move_addr_to_kernel(void __user
*uaddr
, int ulen
, struct sockaddr_storage
*kaddr
)
250 if (ulen
< 0 || ulen
> sizeof(struct sockaddr_storage
))
254 if (copy_from_user(kaddr
, uaddr
, ulen
))
256 return audit_sockaddr(ulen
, kaddr
);
260 * move_addr_to_user - copy an address to user space
261 * @kaddr: kernel space address
262 * @klen: length of address in kernel
263 * @uaddr: user space address
264 * @ulen: pointer to user length field
266 * The value pointed to by ulen on entry is the buffer length available.
267 * This is overwritten with the buffer space used. -EINVAL is returned
268 * if an overlong buffer is specified or a negative buffer size. -EFAULT
269 * is returned if either the buffer or the length field are not
271 * After copying the data up to the limit the user specifies, the true
272 * length of the data is written over the length limit the user
273 * specified. Zero is returned for a success.
276 static int move_addr_to_user(struct sockaddr_storage
*kaddr
, int klen
,
277 void __user
*uaddr
, int __user
*ulen
)
282 BUG_ON(klen
> sizeof(struct sockaddr_storage
));
283 err
= get_user(len
, ulen
);
291 if (audit_sockaddr(klen
, kaddr
))
293 if (copy_to_user(uaddr
, kaddr
, len
))
297 * "fromlen shall refer to the value before truncation.."
300 return __put_user(klen
, ulen
);
303 static struct kmem_cache
*sock_inode_cachep __ro_after_init
;
305 static struct inode
*sock_alloc_inode(struct super_block
*sb
)
307 struct socket_alloc
*ei
;
309 ei
= alloc_inode_sb(sb
, sock_inode_cachep
, GFP_KERNEL
);
312 init_waitqueue_head(&ei
->socket
.wq
.wait
);
313 ei
->socket
.wq
.fasync_list
= NULL
;
314 ei
->socket
.wq
.flags
= 0;
316 ei
->socket
.state
= SS_UNCONNECTED
;
317 ei
->socket
.flags
= 0;
318 ei
->socket
.ops
= NULL
;
319 ei
->socket
.sk
= NULL
;
320 ei
->socket
.file
= NULL
;
322 return &ei
->vfs_inode
;
325 static void sock_free_inode(struct inode
*inode
)
327 struct socket_alloc
*ei
;
329 ei
= container_of(inode
, struct socket_alloc
, vfs_inode
);
330 kmem_cache_free(sock_inode_cachep
, ei
);
333 static void init_once(void *foo
)
335 struct socket_alloc
*ei
= (struct socket_alloc
*)foo
;
337 inode_init_once(&ei
->vfs_inode
);
340 static void init_inodecache(void)
342 sock_inode_cachep
= kmem_cache_create("sock_inode_cache",
343 sizeof(struct socket_alloc
),
345 (SLAB_HWCACHE_ALIGN
|
346 SLAB_RECLAIM_ACCOUNT
|
349 BUG_ON(sock_inode_cachep
== NULL
);
352 static const struct super_operations sockfs_ops
= {
353 .alloc_inode
= sock_alloc_inode
,
354 .free_inode
= sock_free_inode
,
355 .statfs
= simple_statfs
,
359 * sockfs_dname() is called from d_path().
361 static char *sockfs_dname(struct dentry
*dentry
, char *buffer
, int buflen
)
363 return dynamic_dname(buffer
, buflen
, "socket:[%lu]",
364 d_inode(dentry
)->i_ino
);
367 static const struct dentry_operations sockfs_dentry_operations
= {
368 .d_dname
= sockfs_dname
,
371 static int sockfs_xattr_get(const struct xattr_handler
*handler
,
372 struct dentry
*dentry
, struct inode
*inode
,
373 const char *suffix
, void *value
, size_t size
)
376 if (dentry
->d_name
.len
+ 1 > size
)
378 memcpy(value
, dentry
->d_name
.name
, dentry
->d_name
.len
+ 1);
380 return dentry
->d_name
.len
+ 1;
383 #define XATTR_SOCKPROTONAME_SUFFIX "sockprotoname"
384 #define XATTR_NAME_SOCKPROTONAME (XATTR_SYSTEM_PREFIX XATTR_SOCKPROTONAME_SUFFIX)
385 #define XATTR_NAME_SOCKPROTONAME_LEN (sizeof(XATTR_NAME_SOCKPROTONAME)-1)
387 static const struct xattr_handler sockfs_xattr_handler
= {
388 .name
= XATTR_NAME_SOCKPROTONAME
,
389 .get
= sockfs_xattr_get
,
392 static int sockfs_security_xattr_set(const struct xattr_handler
*handler
,
393 struct mnt_idmap
*idmap
,
394 struct dentry
*dentry
, struct inode
*inode
,
395 const char *suffix
, const void *value
,
396 size_t size
, int flags
)
398 /* Handled by LSM. */
402 static const struct xattr_handler sockfs_security_xattr_handler
= {
403 .prefix
= XATTR_SECURITY_PREFIX
,
404 .set
= sockfs_security_xattr_set
,
407 static const struct xattr_handler
* const sockfs_xattr_handlers
[] = {
408 &sockfs_xattr_handler
,
409 &sockfs_security_xattr_handler
,
413 static int sockfs_init_fs_context(struct fs_context
*fc
)
415 struct pseudo_fs_context
*ctx
= init_pseudo(fc
, SOCKFS_MAGIC
);
418 ctx
->ops
= &sockfs_ops
;
419 ctx
->dops
= &sockfs_dentry_operations
;
420 ctx
->xattr
= sockfs_xattr_handlers
;
424 static struct vfsmount
*sock_mnt __read_mostly
;
426 static struct file_system_type sock_fs_type
= {
428 .init_fs_context
= sockfs_init_fs_context
,
429 .kill_sb
= kill_anon_super
,
433 * Obtains the first available file descriptor and sets it up for use.
435 * These functions create file structures and maps them to fd space
436 * of the current process. On success it returns file descriptor
437 * and file struct implicitly stored in sock->file.
438 * Note that another thread may close file descriptor before we return
439 * from this function. We use the fact that now we do not refer
440 * to socket after mapping. If one day we will need it, this
441 * function will increment ref. count on file by 1.
443 * In any case returned fd MAY BE not valid!
444 * This race condition is unavoidable
445 * with shared fd spaces, we cannot solve it inside kernel,
446 * but we take care of internal coherence yet.
450 * sock_alloc_file - Bind a &socket to a &file
452 * @flags: file status flags
453 * @dname: protocol name
455 * Returns the &file bound with @sock, implicitly storing it
456 * in sock->file. If dname is %NULL, sets to "".
458 * On failure @sock is released, and an ERR pointer is returned.
460 * This function uses GFP_KERNEL internally.
463 struct file
*sock_alloc_file(struct socket
*sock
, int flags
, const char *dname
)
468 dname
= sock
->sk
? sock
->sk
->sk_prot_creator
->name
: "";
470 file
= alloc_file_pseudo(SOCK_INODE(sock
), sock_mnt
, dname
,
471 O_RDWR
| (flags
& O_NONBLOCK
),
478 file
->f_mode
|= FMODE_NOWAIT
;
480 file
->private_data
= sock
;
481 stream_open(SOCK_INODE(sock
), file
);
484 EXPORT_SYMBOL(sock_alloc_file
);
486 static int sock_map_fd(struct socket
*sock
, int flags
)
488 struct file
*newfile
;
489 int fd
= get_unused_fd_flags(flags
);
490 if (unlikely(fd
< 0)) {
495 newfile
= sock_alloc_file(sock
, flags
, NULL
);
496 if (!IS_ERR(newfile
)) {
497 fd_install(fd
, newfile
);
502 return PTR_ERR(newfile
);
506 * sock_from_file - Return the &socket bounded to @file.
509 * On failure returns %NULL.
512 struct socket
*sock_from_file(struct file
*file
)
514 if (likely(file
->f_op
== &socket_file_ops
))
515 return file
->private_data
; /* set in sock_alloc_file */
519 EXPORT_SYMBOL(sock_from_file
);
522 * sockfd_lookup - Go from a file number to its socket slot
524 * @err: pointer to an error code return
526 * The file handle passed in is locked and the socket it is bound
527 * to is returned. If an error occurs the err pointer is overwritten
528 * with a negative errno code and NULL is returned. The function checks
529 * for both invalid handles and passing a handle which is not a socket.
531 * On a success the socket object pointer is returned.
534 struct socket
*sockfd_lookup(int fd
, int *err
)
545 sock
= sock_from_file(file
);
552 EXPORT_SYMBOL(sockfd_lookup
);
554 static ssize_t
sockfs_listxattr(struct dentry
*dentry
, char *buffer
,
560 len
= security_inode_listsecurity(d_inode(dentry
), buffer
, size
);
570 len
= (XATTR_NAME_SOCKPROTONAME_LEN
+ 1);
575 memcpy(buffer
, XATTR_NAME_SOCKPROTONAME
, len
);
582 static int sockfs_setattr(struct mnt_idmap
*idmap
,
583 struct dentry
*dentry
, struct iattr
*iattr
)
585 int err
= simple_setattr(&nop_mnt_idmap
, dentry
, iattr
);
587 if (!err
&& (iattr
->ia_valid
& ATTR_UID
)) {
588 struct socket
*sock
= SOCKET_I(d_inode(dentry
));
591 sock
->sk
->sk_uid
= iattr
->ia_uid
;
599 static const struct inode_operations sockfs_inode_ops
= {
600 .listxattr
= sockfs_listxattr
,
601 .setattr
= sockfs_setattr
,
605 * sock_alloc - allocate a socket
607 * Allocate a new inode and socket object. The two are bound together
608 * and initialised. The socket is then returned. If we are out of inodes
609 * NULL is returned. This functions uses GFP_KERNEL internally.
612 struct socket
*sock_alloc(void)
617 inode
= new_inode_pseudo(sock_mnt
->mnt_sb
);
621 sock
= SOCKET_I(inode
);
623 inode
->i_ino
= get_next_ino();
624 inode
->i_mode
= S_IFSOCK
| S_IRWXUGO
;
625 inode
->i_uid
= current_fsuid();
626 inode
->i_gid
= current_fsgid();
627 inode
->i_op
= &sockfs_inode_ops
;
631 EXPORT_SYMBOL(sock_alloc
);
633 static void __sock_release(struct socket
*sock
, struct inode
*inode
)
635 const struct proto_ops
*ops
= READ_ONCE(sock
->ops
);
638 struct module
*owner
= ops
->owner
;
650 if (sock
->wq
.fasync_list
)
651 pr_err("%s: fasync list not empty!\n", __func__
);
654 iput(SOCK_INODE(sock
));
661 * sock_release - close a socket
662 * @sock: socket to close
664 * The socket is released from the protocol stack if it has a release
665 * callback, and the inode is then released if the socket is bound to
666 * an inode not a file.
668 void sock_release(struct socket
*sock
)
670 __sock_release(sock
, NULL
);
672 EXPORT_SYMBOL(sock_release
);
674 void __sock_tx_timestamp(__u32 tsflags
, __u8
*tx_flags
)
676 u8 flags
= *tx_flags
;
678 if (tsflags
& SOF_TIMESTAMPING_TX_HARDWARE
) {
679 flags
|= SKBTX_HW_TSTAMP
;
681 /* PTP hardware clocks can provide a free running cycle counter
682 * as a time base for virtual clocks. Tell driver to use the
683 * free running cycle counter for timestamp if socket is bound
686 if (tsflags
& SOF_TIMESTAMPING_BIND_PHC
)
687 flags
|= SKBTX_HW_TSTAMP_USE_CYCLES
;
690 if (tsflags
& SOF_TIMESTAMPING_TX_SOFTWARE
)
691 flags
|= SKBTX_SW_TSTAMP
;
693 if (tsflags
& SOF_TIMESTAMPING_TX_SCHED
)
694 flags
|= SKBTX_SCHED_TSTAMP
;
698 EXPORT_SYMBOL(__sock_tx_timestamp
);
700 INDIRECT_CALLABLE_DECLARE(int inet_sendmsg(struct socket
*, struct msghdr
*,
702 INDIRECT_CALLABLE_DECLARE(int inet6_sendmsg(struct socket
*, struct msghdr
*,
705 static noinline
void call_trace_sock_send_length(struct sock
*sk
, int ret
,
708 trace_sock_send_length(sk
, ret
, 0);
711 static inline int sock_sendmsg_nosec(struct socket
*sock
, struct msghdr
*msg
)
713 int ret
= INDIRECT_CALL_INET(READ_ONCE(sock
->ops
)->sendmsg
, inet6_sendmsg
,
714 inet_sendmsg
, sock
, msg
,
716 BUG_ON(ret
== -EIOCBQUEUED
);
718 if (trace_sock_send_length_enabled())
719 call_trace_sock_send_length(sock
->sk
, ret
, 0);
723 static int __sock_sendmsg(struct socket
*sock
, struct msghdr
*msg
)
725 int err
= security_socket_sendmsg(sock
, msg
,
728 return err
?: sock_sendmsg_nosec(sock
, msg
);
732 * sock_sendmsg - send a message through @sock
734 * @msg: message to send
736 * Sends @msg through @sock, passing through LSM.
737 * Returns the number of bytes sent, or an error code.
739 int sock_sendmsg(struct socket
*sock
, struct msghdr
*msg
)
741 struct sockaddr_storage
*save_addr
= (struct sockaddr_storage
*)msg
->msg_name
;
742 struct sockaddr_storage address
;
743 int save_len
= msg
->msg_namelen
;
747 memcpy(&address
, msg
->msg_name
, msg
->msg_namelen
);
748 msg
->msg_name
= &address
;
751 ret
= __sock_sendmsg(sock
, msg
);
752 msg
->msg_name
= save_addr
;
753 msg
->msg_namelen
= save_len
;
757 EXPORT_SYMBOL(sock_sendmsg
);
760 * kernel_sendmsg - send a message through @sock (kernel-space)
762 * @msg: message header
764 * @num: vec array length
765 * @size: total message data size
767 * Builds the message data with @vec and sends it through @sock.
768 * Returns the number of bytes sent, or an error code.
771 int kernel_sendmsg(struct socket
*sock
, struct msghdr
*msg
,
772 struct kvec
*vec
, size_t num
, size_t size
)
774 iov_iter_kvec(&msg
->msg_iter
, ITER_SOURCE
, vec
, num
, size
);
775 return sock_sendmsg(sock
, msg
);
777 EXPORT_SYMBOL(kernel_sendmsg
);
779 static bool skb_is_err_queue(const struct sk_buff
*skb
)
781 /* pkt_type of skbs enqueued on the error queue are set to
782 * PACKET_OUTGOING in skb_set_err_queue(). This is only safe to do
783 * in recvmsg, since skbs received on a local socket will never
784 * have a pkt_type of PACKET_OUTGOING.
786 return skb
->pkt_type
== PACKET_OUTGOING
;
789 /* On transmit, software and hardware timestamps are returned independently.
790 * As the two skb clones share the hardware timestamp, which may be updated
791 * before the software timestamp is received, a hardware TX timestamp may be
792 * returned only if there is no software TX timestamp. Ignore false software
793 * timestamps, which may be made in the __sock_recv_timestamp() call when the
794 * option SO_TIMESTAMP_OLD(NS) is enabled on the socket, even when the skb has a
795 * hardware timestamp.
797 static bool skb_is_swtx_tstamp(const struct sk_buff
*skb
, int false_tstamp
)
799 return skb
->tstamp
&& !false_tstamp
&& skb_is_err_queue(skb
);
802 static ktime_t
get_timestamp(struct sock
*sk
, struct sk_buff
*skb
, int *if_index
)
804 bool cycles
= READ_ONCE(sk
->sk_tsflags
) & SOF_TIMESTAMPING_BIND_PHC
;
805 struct skb_shared_hwtstamps
*shhwtstamps
= skb_hwtstamps(skb
);
806 struct net_device
*orig_dev
;
810 orig_dev
= dev_get_by_napi_id(skb_napi_id(skb
));
812 *if_index
= orig_dev
->ifindex
;
813 hwtstamp
= netdev_get_tstamp(orig_dev
, shhwtstamps
, cycles
);
815 hwtstamp
= shhwtstamps
->hwtstamp
;
822 static void put_ts_pktinfo(struct msghdr
*msg
, struct sk_buff
*skb
,
825 struct scm_ts_pktinfo ts_pktinfo
;
826 struct net_device
*orig_dev
;
828 if (!skb_mac_header_was_set(skb
))
831 memset(&ts_pktinfo
, 0, sizeof(ts_pktinfo
));
835 orig_dev
= dev_get_by_napi_id(skb_napi_id(skb
));
837 if_index
= orig_dev
->ifindex
;
840 ts_pktinfo
.if_index
= if_index
;
842 ts_pktinfo
.pkt_length
= skb
->len
- skb_mac_offset(skb
);
843 put_cmsg(msg
, SOL_SOCKET
, SCM_TIMESTAMPING_PKTINFO
,
844 sizeof(ts_pktinfo
), &ts_pktinfo
);
848 * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
850 void __sock_recv_timestamp(struct msghdr
*msg
, struct sock
*sk
,
853 int need_software_tstamp
= sock_flag(sk
, SOCK_RCVTSTAMP
);
854 int new_tstamp
= sock_flag(sk
, SOCK_TSTAMP_NEW
);
855 struct scm_timestamping_internal tss
;
856 int empty
= 1, false_tstamp
= 0;
857 struct skb_shared_hwtstamps
*shhwtstamps
=
863 /* Race occurred between timestamp enabling and packet
864 receiving. Fill in the current time for now. */
865 if (need_software_tstamp
&& skb
->tstamp
== 0) {
866 __net_timestamp(skb
);
870 if (need_software_tstamp
) {
871 if (!sock_flag(sk
, SOCK_RCVTSTAMPNS
)) {
873 struct __kernel_sock_timeval tv
;
875 skb_get_new_timestamp(skb
, &tv
);
876 put_cmsg(msg
, SOL_SOCKET
, SO_TIMESTAMP_NEW
,
879 struct __kernel_old_timeval tv
;
881 skb_get_timestamp(skb
, &tv
);
882 put_cmsg(msg
, SOL_SOCKET
, SO_TIMESTAMP_OLD
,
887 struct __kernel_timespec ts
;
889 skb_get_new_timestampns(skb
, &ts
);
890 put_cmsg(msg
, SOL_SOCKET
, SO_TIMESTAMPNS_NEW
,
893 struct __kernel_old_timespec ts
;
895 skb_get_timestampns(skb
, &ts
);
896 put_cmsg(msg
, SOL_SOCKET
, SO_TIMESTAMPNS_OLD
,
902 memset(&tss
, 0, sizeof(tss
));
903 tsflags
= READ_ONCE(sk
->sk_tsflags
);
904 if ((tsflags
& SOF_TIMESTAMPING_SOFTWARE
&&
905 (tsflags
& SOF_TIMESTAMPING_RX_SOFTWARE
||
906 skb_is_err_queue(skb
) ||
907 !(tsflags
& SOF_TIMESTAMPING_OPT_RX_FILTER
))) &&
908 ktime_to_timespec64_cond(skb
->tstamp
, tss
.ts
+ 0))
911 (tsflags
& SOF_TIMESTAMPING_RAW_HARDWARE
&&
912 (tsflags
& SOF_TIMESTAMPING_RX_HARDWARE
||
913 skb_is_err_queue(skb
) ||
914 !(tsflags
& SOF_TIMESTAMPING_OPT_RX_FILTER
))) &&
915 !skb_is_swtx_tstamp(skb
, false_tstamp
)) {
917 if (skb_shinfo(skb
)->tx_flags
& SKBTX_HW_TSTAMP_NETDEV
)
918 hwtstamp
= get_timestamp(sk
, skb
, &if_index
);
920 hwtstamp
= shhwtstamps
->hwtstamp
;
922 if (tsflags
& SOF_TIMESTAMPING_BIND_PHC
)
923 hwtstamp
= ptp_convert_timestamp(&hwtstamp
,
924 READ_ONCE(sk
->sk_bind_phc
));
926 if (ktime_to_timespec64_cond(hwtstamp
, tss
.ts
+ 2)) {
929 if ((tsflags
& SOF_TIMESTAMPING_OPT_PKTINFO
) &&
930 !skb_is_err_queue(skb
))
931 put_ts_pktinfo(msg
, skb
, if_index
);
935 if (sock_flag(sk
, SOCK_TSTAMP_NEW
))
936 put_cmsg_scm_timestamping64(msg
, &tss
);
938 put_cmsg_scm_timestamping(msg
, &tss
);
940 if (skb_is_err_queue(skb
) && skb
->len
&&
941 SKB_EXT_ERR(skb
)->opt_stats
)
942 put_cmsg(msg
, SOL_SOCKET
, SCM_TIMESTAMPING_OPT_STATS
,
943 skb
->len
, skb
->data
);
946 EXPORT_SYMBOL_GPL(__sock_recv_timestamp
);
948 #ifdef CONFIG_WIRELESS
949 void __sock_recv_wifi_status(struct msghdr
*msg
, struct sock
*sk
,
954 if (!sock_flag(sk
, SOCK_WIFI_STATUS
))
956 if (!skb
->wifi_acked_valid
)
959 ack
= skb
->wifi_acked
;
961 put_cmsg(msg
, SOL_SOCKET
, SCM_WIFI_STATUS
, sizeof(ack
), &ack
);
963 EXPORT_SYMBOL_GPL(__sock_recv_wifi_status
);
966 static inline void sock_recv_drops(struct msghdr
*msg
, struct sock
*sk
,
969 if (sock_flag(sk
, SOCK_RXQ_OVFL
) && skb
&& SOCK_SKB_CB(skb
)->dropcount
)
970 put_cmsg(msg
, SOL_SOCKET
, SO_RXQ_OVFL
,
971 sizeof(__u32
), &SOCK_SKB_CB(skb
)->dropcount
);
974 static void sock_recv_mark(struct msghdr
*msg
, struct sock
*sk
,
977 if (sock_flag(sk
, SOCK_RCVMARK
) && skb
) {
978 /* We must use a bounce buffer for CONFIG_HARDENED_USERCOPY=y */
979 __u32 mark
= skb
->mark
;
981 put_cmsg(msg
, SOL_SOCKET
, SO_MARK
, sizeof(__u32
), &mark
);
985 static void sock_recv_priority(struct msghdr
*msg
, struct sock
*sk
,
988 if (sock_flag(sk
, SOCK_RCVPRIORITY
) && skb
) {
989 __u32 priority
= skb
->priority
;
991 put_cmsg(msg
, SOL_SOCKET
, SO_PRIORITY
, sizeof(__u32
), &priority
);
995 void __sock_recv_cmsgs(struct msghdr
*msg
, struct sock
*sk
,
998 sock_recv_timestamp(msg
, sk
, skb
);
999 sock_recv_drops(msg
, sk
, skb
);
1000 sock_recv_mark(msg
, sk
, skb
);
1001 sock_recv_priority(msg
, sk
, skb
);
1003 EXPORT_SYMBOL_GPL(__sock_recv_cmsgs
);
1005 INDIRECT_CALLABLE_DECLARE(int inet_recvmsg(struct socket
*, struct msghdr
*,
1007 INDIRECT_CALLABLE_DECLARE(int inet6_recvmsg(struct socket
*, struct msghdr
*,
1010 static noinline
void call_trace_sock_recv_length(struct sock
*sk
, int ret
, int flags
)
1012 trace_sock_recv_length(sk
, ret
, flags
);
1015 static inline int sock_recvmsg_nosec(struct socket
*sock
, struct msghdr
*msg
,
1018 int ret
= INDIRECT_CALL_INET(READ_ONCE(sock
->ops
)->recvmsg
,
1020 inet_recvmsg
, sock
, msg
,
1021 msg_data_left(msg
), flags
);
1022 if (trace_sock_recv_length_enabled())
1023 call_trace_sock_recv_length(sock
->sk
, ret
, flags
);
1028 * sock_recvmsg - receive a message from @sock
1030 * @msg: message to receive
1031 * @flags: message flags
1033 * Receives @msg from @sock, passing through LSM. Returns the total number
1034 * of bytes received, or an error.
1036 int sock_recvmsg(struct socket
*sock
, struct msghdr
*msg
, int flags
)
1038 int err
= security_socket_recvmsg(sock
, msg
, msg_data_left(msg
), flags
);
1040 return err
?: sock_recvmsg_nosec(sock
, msg
, flags
);
1042 EXPORT_SYMBOL(sock_recvmsg
);
1045 * kernel_recvmsg - Receive a message from a socket (kernel space)
1046 * @sock: The socket to receive the message from
1047 * @msg: Received message
1048 * @vec: Input s/g array for message data
1049 * @num: Size of input s/g array
1050 * @size: Number of bytes to read
1051 * @flags: Message flags (MSG_DONTWAIT, etc...)
1053 * On return the msg structure contains the scatter/gather array passed in the
1054 * vec argument. The array is modified so that it consists of the unfilled
1055 * portion of the original array.
1057 * The returned value is the total number of bytes received, or an error.
1060 int kernel_recvmsg(struct socket
*sock
, struct msghdr
*msg
,
1061 struct kvec
*vec
, size_t num
, size_t size
, int flags
)
1063 msg
->msg_control_is_user
= false;
1064 iov_iter_kvec(&msg
->msg_iter
, ITER_DEST
, vec
, num
, size
);
1065 return sock_recvmsg(sock
, msg
, flags
);
1067 EXPORT_SYMBOL(kernel_recvmsg
);
1069 static ssize_t
sock_splice_read(struct file
*file
, loff_t
*ppos
,
1070 struct pipe_inode_info
*pipe
, size_t len
,
1073 struct socket
*sock
= file
->private_data
;
1074 const struct proto_ops
*ops
;
1076 ops
= READ_ONCE(sock
->ops
);
1077 if (unlikely(!ops
->splice_read
))
1078 return copy_splice_read(file
, ppos
, pipe
, len
, flags
);
1080 return ops
->splice_read(sock
, ppos
, pipe
, len
, flags
);
1083 static void sock_splice_eof(struct file
*file
)
1085 struct socket
*sock
= file
->private_data
;
1086 const struct proto_ops
*ops
;
1088 ops
= READ_ONCE(sock
->ops
);
1089 if (ops
->splice_eof
)
1090 ops
->splice_eof(sock
);
1093 static ssize_t
sock_read_iter(struct kiocb
*iocb
, struct iov_iter
*to
)
1095 struct file
*file
= iocb
->ki_filp
;
1096 struct socket
*sock
= file
->private_data
;
1097 struct msghdr msg
= {.msg_iter
= *to
,
1101 if (file
->f_flags
& O_NONBLOCK
|| (iocb
->ki_flags
& IOCB_NOWAIT
))
1102 msg
.msg_flags
= MSG_DONTWAIT
;
1104 if (iocb
->ki_pos
!= 0)
1107 if (!iov_iter_count(to
)) /* Match SYS5 behaviour */
1110 res
= sock_recvmsg(sock
, &msg
, msg
.msg_flags
);
1115 static ssize_t
sock_write_iter(struct kiocb
*iocb
, struct iov_iter
*from
)
1117 struct file
*file
= iocb
->ki_filp
;
1118 struct socket
*sock
= file
->private_data
;
1119 struct msghdr msg
= {.msg_iter
= *from
,
1123 if (iocb
->ki_pos
!= 0)
1126 if (file
->f_flags
& O_NONBLOCK
|| (iocb
->ki_flags
& IOCB_NOWAIT
))
1127 msg
.msg_flags
= MSG_DONTWAIT
;
1129 if (sock
->type
== SOCK_SEQPACKET
)
1130 msg
.msg_flags
|= MSG_EOR
;
1132 res
= __sock_sendmsg(sock
, &msg
);
1133 *from
= msg
.msg_iter
;
1138 * Atomic setting of ioctl hooks to avoid race
1139 * with module unload.
1142 static DEFINE_MUTEX(br_ioctl_mutex
);
1143 static int (*br_ioctl_hook
)(struct net
*net
, struct net_bridge
*br
,
1144 unsigned int cmd
, struct ifreq
*ifr
,
1147 void brioctl_set(int (*hook
)(struct net
*net
, struct net_bridge
*br
,
1148 unsigned int cmd
, struct ifreq
*ifr
,
1151 mutex_lock(&br_ioctl_mutex
);
1152 br_ioctl_hook
= hook
;
1153 mutex_unlock(&br_ioctl_mutex
);
1155 EXPORT_SYMBOL(brioctl_set
);
1157 int br_ioctl_call(struct net
*net
, struct net_bridge
*br
, unsigned int cmd
,
1158 struct ifreq
*ifr
, void __user
*uarg
)
1163 request_module("bridge");
1165 mutex_lock(&br_ioctl_mutex
);
1167 err
= br_ioctl_hook(net
, br
, cmd
, ifr
, uarg
);
1168 mutex_unlock(&br_ioctl_mutex
);
1173 static DEFINE_MUTEX(vlan_ioctl_mutex
);
1174 static int (*vlan_ioctl_hook
) (struct net
*, void __user
*arg
);
1176 void vlan_ioctl_set(int (*hook
) (struct net
*, void __user
*))
1178 mutex_lock(&vlan_ioctl_mutex
);
1179 vlan_ioctl_hook
= hook
;
1180 mutex_unlock(&vlan_ioctl_mutex
);
1182 EXPORT_SYMBOL(vlan_ioctl_set
);
1184 static long sock_do_ioctl(struct net
*net
, struct socket
*sock
,
1185 unsigned int cmd
, unsigned long arg
)
1187 const struct proto_ops
*ops
= READ_ONCE(sock
->ops
);
1191 void __user
*argp
= (void __user
*)arg
;
1194 err
= ops
->ioctl(sock
, cmd
, arg
);
1197 * If this ioctl is unknown try to hand it down
1198 * to the NIC driver.
1200 if (err
!= -ENOIOCTLCMD
)
1203 if (!is_socket_ioctl_cmd(cmd
))
1206 if (get_user_ifreq(&ifr
, &data
, argp
))
1208 err
= dev_ioctl(net
, cmd
, &ifr
, data
, &need_copyout
);
1209 if (!err
&& need_copyout
)
1210 if (put_user_ifreq(&ifr
, argp
))
1217 * With an ioctl, arg may well be a user mode pointer, but we don't know
1218 * what to do with it - that's up to the protocol still.
1221 static long sock_ioctl(struct file
*file
, unsigned cmd
, unsigned long arg
)
1223 const struct proto_ops
*ops
;
1224 struct socket
*sock
;
1226 void __user
*argp
= (void __user
*)arg
;
1230 sock
= file
->private_data
;
1231 ops
= READ_ONCE(sock
->ops
);
1234 if (unlikely(cmd
>= SIOCDEVPRIVATE
&& cmd
<= (SIOCDEVPRIVATE
+ 15))) {
1238 if (get_user_ifreq(&ifr
, &data
, argp
))
1240 err
= dev_ioctl(net
, cmd
, &ifr
, data
, &need_copyout
);
1241 if (!err
&& need_copyout
)
1242 if (put_user_ifreq(&ifr
, argp
))
1245 #ifdef CONFIG_WEXT_CORE
1246 if (cmd
>= SIOCIWFIRST
&& cmd
<= SIOCIWLAST
) {
1247 err
= wext_handle_ioctl(net
, cmd
, argp
);
1254 if (get_user(pid
, (int __user
*)argp
))
1256 err
= f_setown(sock
->file
, pid
, 1);
1260 err
= put_user(f_getown(sock
->file
),
1261 (int __user
*)argp
);
1267 err
= br_ioctl_call(net
, NULL
, cmd
, NULL
, argp
);
1272 if (!vlan_ioctl_hook
)
1273 request_module("8021q");
1275 mutex_lock(&vlan_ioctl_mutex
);
1276 if (vlan_ioctl_hook
)
1277 err
= vlan_ioctl_hook(net
, argp
);
1278 mutex_unlock(&vlan_ioctl_mutex
);
1282 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
1285 err
= open_related_ns(&net
->ns
, get_net_ns
);
1287 case SIOCGSTAMP_OLD
:
1288 case SIOCGSTAMPNS_OLD
:
1289 if (!ops
->gettstamp
) {
1293 err
= ops
->gettstamp(sock
, argp
,
1294 cmd
== SIOCGSTAMP_OLD
,
1295 !IS_ENABLED(CONFIG_64BIT
));
1297 case SIOCGSTAMP_NEW
:
1298 case SIOCGSTAMPNS_NEW
:
1299 if (!ops
->gettstamp
) {
1303 err
= ops
->gettstamp(sock
, argp
,
1304 cmd
== SIOCGSTAMP_NEW
,
1309 err
= dev_ifconf(net
, argp
);
1313 err
= sock_do_ioctl(net
, sock
, cmd
, arg
);
1320 * sock_create_lite - creates a socket
1321 * @family: protocol family (AF_INET, ...)
1322 * @type: communication type (SOCK_STREAM, ...)
1323 * @protocol: protocol (0, ...)
1326 * Creates a new socket and assigns it to @res, passing through LSM.
1327 * The new socket initialization is not complete, see kernel_accept().
1328 * Returns 0 or an error. On failure @res is set to %NULL.
1329 * This function internally uses GFP_KERNEL.
1332 int sock_create_lite(int family
, int type
, int protocol
, struct socket
**res
)
1335 struct socket
*sock
= NULL
;
1337 err
= security_socket_create(family
, type
, protocol
, 1);
1341 sock
= sock_alloc();
1348 err
= security_socket_post_create(sock
, family
, type
, protocol
, 1);
1360 EXPORT_SYMBOL(sock_create_lite
);
1362 /* No kernel lock held - perfect */
1363 static __poll_t
sock_poll(struct file
*file
, poll_table
*wait
)
1365 struct socket
*sock
= file
->private_data
;
1366 const struct proto_ops
*ops
= READ_ONCE(sock
->ops
);
1367 __poll_t events
= poll_requested_events(wait
), flag
= 0;
1372 if (sk_can_busy_loop(sock
->sk
)) {
1373 /* poll once if requested by the syscall */
1374 if (events
& POLL_BUSY_LOOP
)
1375 sk_busy_loop(sock
->sk
, 1);
1377 /* if this socket can poll_ll, tell the system call */
1378 flag
= POLL_BUSY_LOOP
;
1381 return ops
->poll(file
, sock
, wait
) | flag
;
1384 static int sock_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1386 struct socket
*sock
= file
->private_data
;
1388 return READ_ONCE(sock
->ops
)->mmap(file
, sock
, vma
);
1391 static int sock_close(struct inode
*inode
, struct file
*filp
)
1393 __sock_release(SOCKET_I(inode
), inode
);
1398 * Update the socket async list
1400 * Fasync_list locking strategy.
1402 * 1. fasync_list is modified only under process context socket lock
1403 * i.e. under semaphore.
1404 * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
1405 * or under socket lock
1408 static int sock_fasync(int fd
, struct file
*filp
, int on
)
1410 struct socket
*sock
= filp
->private_data
;
1411 struct sock
*sk
= sock
->sk
;
1412 struct socket_wq
*wq
= &sock
->wq
;
1418 fasync_helper(fd
, filp
, on
, &wq
->fasync_list
);
1420 if (!wq
->fasync_list
)
1421 sock_reset_flag(sk
, SOCK_FASYNC
);
1423 sock_set_flag(sk
, SOCK_FASYNC
);
1429 /* This function may be called only under rcu_lock */
1431 int sock_wake_async(struct socket_wq
*wq
, int how
, int band
)
1433 if (!wq
|| !wq
->fasync_list
)
1437 case SOCK_WAKE_WAITD
:
1438 if (test_bit(SOCKWQ_ASYNC_WAITDATA
, &wq
->flags
))
1441 case SOCK_WAKE_SPACE
:
1442 if (!test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE
, &wq
->flags
))
1447 kill_fasync(&wq
->fasync_list
, SIGIO
, band
);
1450 kill_fasync(&wq
->fasync_list
, SIGURG
, band
);
1455 EXPORT_SYMBOL(sock_wake_async
);
1458 * __sock_create - creates a socket
1459 * @net: net namespace
1460 * @family: protocol family (AF_INET, ...)
1461 * @type: communication type (SOCK_STREAM, ...)
1462 * @protocol: protocol (0, ...)
1464 * @kern: boolean for kernel space sockets
1466 * Creates a new socket and assigns it to @res, passing through LSM.
1467 * Returns 0 or an error. On failure @res is set to %NULL. @kern must
1468 * be set to true if the socket resides in kernel space.
1469 * This function internally uses GFP_KERNEL.
1472 int __sock_create(struct net
*net
, int family
, int type
, int protocol
,
1473 struct socket
**res
, int kern
)
1476 struct socket
*sock
;
1477 const struct net_proto_family
*pf
;
1480 * Check protocol is in range
1482 if (family
< 0 || family
>= NPROTO
)
1483 return -EAFNOSUPPORT
;
1484 if (type
< 0 || type
>= SOCK_MAX
)
1489 This uglymoron is moved from INET layer to here to avoid
1490 deadlock in module load.
1492 if (family
== PF_INET
&& type
== SOCK_PACKET
) {
1493 pr_info_once("%s uses obsolete (PF_INET,SOCK_PACKET)\n",
1498 err
= security_socket_create(family
, type
, protocol
, kern
);
1503 * Allocate the socket and allow the family to set things up. if
1504 * the protocol is 0, the family is instructed to select an appropriate
1507 sock
= sock_alloc();
1509 net_warn_ratelimited("socket: no more sockets\n");
1510 return -ENFILE
; /* Not exactly a match, but its the
1511 closest posix thing */
1516 #ifdef CONFIG_MODULES
1517 /* Attempt to load a protocol module if the find failed.
1519 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
1520 * requested real, full-featured networking support upon configuration.
1521 * Otherwise module support will break!
1523 if (rcu_access_pointer(net_families
[family
]) == NULL
)
1524 request_module("net-pf-%d", family
);
1528 pf
= rcu_dereference(net_families
[family
]);
1529 err
= -EAFNOSUPPORT
;
1534 * We will call the ->create function, that possibly is in a loadable
1535 * module, so we have to bump that loadable module refcnt first.
1537 if (!try_module_get(pf
->owner
))
1540 /* Now protected by module ref count */
1543 err
= pf
->create(net
, sock
, protocol
, kern
);
1545 /* ->create should release the allocated sock->sk object on error
1546 * and make sure sock->sk is set to NULL to avoid use-after-free
1548 DEBUG_NET_WARN_ONCE(sock
->sk
,
1549 "%ps must clear sock->sk on failure, family: %d, type: %d, protocol: %d\n",
1550 pf
->create
, family
, type
, protocol
);
1551 goto out_module_put
;
1555 * Now to bump the refcnt of the [loadable] module that owns this
1556 * socket at sock_release time we decrement its refcnt.
1558 if (!try_module_get(sock
->ops
->owner
))
1559 goto out_module_busy
;
1562 * Now that we're done with the ->create function, the [loadable]
1563 * module can have its refcnt decremented
1565 module_put(pf
->owner
);
1566 err
= security_socket_post_create(sock
, family
, type
, protocol
, kern
);
1568 goto out_sock_release
;
1574 err
= -EAFNOSUPPORT
;
1577 module_put(pf
->owner
);
1584 goto out_sock_release
;
1586 EXPORT_SYMBOL(__sock_create
);
1589 * sock_create - creates a socket
1590 * @family: protocol family (AF_INET, ...)
1591 * @type: communication type (SOCK_STREAM, ...)
1592 * @protocol: protocol (0, ...)
1595 * A wrapper around __sock_create().
1596 * Returns 0 or an error. This function internally uses GFP_KERNEL.
1599 int sock_create(int family
, int type
, int protocol
, struct socket
**res
)
1601 return __sock_create(current
->nsproxy
->net_ns
, family
, type
, protocol
, res
, 0);
1603 EXPORT_SYMBOL(sock_create
);
1606 * sock_create_kern - creates a socket (kernel space)
1607 * @net: net namespace
1608 * @family: protocol family (AF_INET, ...)
1609 * @type: communication type (SOCK_STREAM, ...)
1610 * @protocol: protocol (0, ...)
1613 * A wrapper around __sock_create().
1614 * Returns 0 or an error. This function internally uses GFP_KERNEL.
1617 int sock_create_kern(struct net
*net
, int family
, int type
, int protocol
, struct socket
**res
)
1619 return __sock_create(net
, family
, type
, protocol
, res
, 1);
1621 EXPORT_SYMBOL(sock_create_kern
);
1623 static struct socket
*__sys_socket_create(int family
, int type
, int protocol
)
1625 struct socket
*sock
;
1628 /* Check the SOCK_* constants for consistency. */
1629 BUILD_BUG_ON(SOCK_CLOEXEC
!= O_CLOEXEC
);
1630 BUILD_BUG_ON((SOCK_MAX
| SOCK_TYPE_MASK
) != SOCK_TYPE_MASK
);
1631 BUILD_BUG_ON(SOCK_CLOEXEC
& SOCK_TYPE_MASK
);
1632 BUILD_BUG_ON(SOCK_NONBLOCK
& SOCK_TYPE_MASK
);
1634 if ((type
& ~SOCK_TYPE_MASK
) & ~(SOCK_CLOEXEC
| SOCK_NONBLOCK
))
1635 return ERR_PTR(-EINVAL
);
1636 type
&= SOCK_TYPE_MASK
;
1638 retval
= sock_create(family
, type
, protocol
, &sock
);
1640 return ERR_PTR(retval
);
1645 struct file
*__sys_socket_file(int family
, int type
, int protocol
)
1647 struct socket
*sock
;
1650 sock
= __sys_socket_create(family
, type
, protocol
);
1652 return ERR_CAST(sock
);
1654 flags
= type
& ~SOCK_TYPE_MASK
;
1655 if (SOCK_NONBLOCK
!= O_NONBLOCK
&& (flags
& SOCK_NONBLOCK
))
1656 flags
= (flags
& ~SOCK_NONBLOCK
) | O_NONBLOCK
;
1658 return sock_alloc_file(sock
, flags
, NULL
);
1661 /* A hook for bpf progs to attach to and update socket protocol.
1663 * A static noinline declaration here could cause the compiler to
1664 * optimize away the function. A global noinline declaration will
1665 * keep the definition, but may optimize away the callsite.
1666 * Therefore, __weak is needed to ensure that the call is still
1667 * emitted, by telling the compiler that we don't know what the
1668 * function might eventually be.
1673 __weak noinline
int update_socket_protocol(int family
, int type
, int protocol
)
1680 int __sys_socket(int family
, int type
, int protocol
)
1682 struct socket
*sock
;
1685 sock
= __sys_socket_create(family
, type
,
1686 update_socket_protocol(family
, type
, protocol
));
1688 return PTR_ERR(sock
);
1690 flags
= type
& ~SOCK_TYPE_MASK
;
1691 if (SOCK_NONBLOCK
!= O_NONBLOCK
&& (flags
& SOCK_NONBLOCK
))
1692 flags
= (flags
& ~SOCK_NONBLOCK
) | O_NONBLOCK
;
1694 return sock_map_fd(sock
, flags
& (O_CLOEXEC
| O_NONBLOCK
));
1697 SYSCALL_DEFINE3(socket
, int, family
, int, type
, int, protocol
)
1699 return __sys_socket(family
, type
, protocol
);
1703 * Create a pair of connected sockets.
1706 int __sys_socketpair(int family
, int type
, int protocol
, int __user
*usockvec
)
1708 struct socket
*sock1
, *sock2
;
1710 struct file
*newfile1
, *newfile2
;
1713 flags
= type
& ~SOCK_TYPE_MASK
;
1714 if (flags
& ~(SOCK_CLOEXEC
| SOCK_NONBLOCK
))
1716 type
&= SOCK_TYPE_MASK
;
1718 if (SOCK_NONBLOCK
!= O_NONBLOCK
&& (flags
& SOCK_NONBLOCK
))
1719 flags
= (flags
& ~SOCK_NONBLOCK
) | O_NONBLOCK
;
1722 * reserve descriptors and make sure we won't fail
1723 * to return them to userland.
1725 fd1
= get_unused_fd_flags(flags
);
1726 if (unlikely(fd1
< 0))
1729 fd2
= get_unused_fd_flags(flags
);
1730 if (unlikely(fd2
< 0)) {
1735 err
= put_user(fd1
, &usockvec
[0]);
1739 err
= put_user(fd2
, &usockvec
[1]);
1744 * Obtain the first socket and check if the underlying protocol
1745 * supports the socketpair call.
1748 err
= sock_create(family
, type
, protocol
, &sock1
);
1749 if (unlikely(err
< 0))
1752 err
= sock_create(family
, type
, protocol
, &sock2
);
1753 if (unlikely(err
< 0)) {
1754 sock_release(sock1
);
1758 err
= security_socket_socketpair(sock1
, sock2
);
1759 if (unlikely(err
)) {
1760 sock_release(sock2
);
1761 sock_release(sock1
);
1765 err
= READ_ONCE(sock1
->ops
)->socketpair(sock1
, sock2
);
1766 if (unlikely(err
< 0)) {
1767 sock_release(sock2
);
1768 sock_release(sock1
);
1772 newfile1
= sock_alloc_file(sock1
, flags
, NULL
);
1773 if (IS_ERR(newfile1
)) {
1774 err
= PTR_ERR(newfile1
);
1775 sock_release(sock2
);
1779 newfile2
= sock_alloc_file(sock2
, flags
, NULL
);
1780 if (IS_ERR(newfile2
)) {
1781 err
= PTR_ERR(newfile2
);
1786 audit_fd_pair(fd1
, fd2
);
1788 fd_install(fd1
, newfile1
);
1789 fd_install(fd2
, newfile2
);
1798 SYSCALL_DEFINE4(socketpair
, int, family
, int, type
, int, protocol
,
1799 int __user
*, usockvec
)
1801 return __sys_socketpair(family
, type
, protocol
, usockvec
);
1804 int __sys_bind_socket(struct socket
*sock
, struct sockaddr_storage
*address
,
1809 err
= security_socket_bind(sock
, (struct sockaddr
*)address
,
1812 err
= READ_ONCE(sock
->ops
)->bind(sock
,
1813 (struct sockaddr
*)address
,
1819 * Bind a name to a socket. Nothing much to do here since it's
1820 * the protocol's responsibility to handle the local address.
1822 * We move the socket address to kernel space before we call
1823 * the protocol layer (having also checked the address is ok).
1826 int __sys_bind(int fd
, struct sockaddr __user
*umyaddr
, int addrlen
)
1828 struct socket
*sock
;
1829 struct sockaddr_storage address
;
1835 sock
= sock_from_file(fd_file(f
));
1836 if (unlikely(!sock
))
1839 err
= move_addr_to_kernel(umyaddr
, addrlen
, &address
);
1843 return __sys_bind_socket(sock
, &address
, addrlen
);
1846 SYSCALL_DEFINE3(bind
, int, fd
, struct sockaddr __user
*, umyaddr
, int, addrlen
)
1848 return __sys_bind(fd
, umyaddr
, addrlen
);
1852 * Perform a listen. Basically, we allow the protocol to do anything
1853 * necessary for a listen, and if that works, we mark the socket as
1854 * ready for listening.
1856 int __sys_listen_socket(struct socket
*sock
, int backlog
)
1860 somaxconn
= READ_ONCE(sock_net(sock
->sk
)->core
.sysctl_somaxconn
);
1861 if ((unsigned int)backlog
> somaxconn
)
1862 backlog
= somaxconn
;
1864 err
= security_socket_listen(sock
, backlog
);
1866 err
= READ_ONCE(sock
->ops
)->listen(sock
, backlog
);
1870 int __sys_listen(int fd
, int backlog
)
1873 struct socket
*sock
;
1877 sock
= sock_from_file(fd_file(f
));
1878 if (unlikely(!sock
))
1881 return __sys_listen_socket(sock
, backlog
);
1884 SYSCALL_DEFINE2(listen
, int, fd
, int, backlog
)
1886 return __sys_listen(fd
, backlog
);
1889 struct file
*do_accept(struct file
*file
, struct proto_accept_arg
*arg
,
1890 struct sockaddr __user
*upeer_sockaddr
,
1891 int __user
*upeer_addrlen
, int flags
)
1893 struct socket
*sock
, *newsock
;
1894 struct file
*newfile
;
1896 struct sockaddr_storage address
;
1897 const struct proto_ops
*ops
;
1899 sock
= sock_from_file(file
);
1901 return ERR_PTR(-ENOTSOCK
);
1903 newsock
= sock_alloc();
1905 return ERR_PTR(-ENFILE
);
1906 ops
= READ_ONCE(sock
->ops
);
1908 newsock
->type
= sock
->type
;
1912 * We don't need try_module_get here, as the listening socket (sock)
1913 * has the protocol module (sock->ops->owner) held.
1915 __module_get(ops
->owner
);
1917 newfile
= sock_alloc_file(newsock
, flags
, sock
->sk
->sk_prot_creator
->name
);
1918 if (IS_ERR(newfile
))
1921 err
= security_socket_accept(sock
, newsock
);
1925 arg
->flags
|= sock
->file
->f_flags
;
1926 err
= ops
->accept(sock
, newsock
, arg
);
1930 if (upeer_sockaddr
) {
1931 len
= ops
->getname(newsock
, (struct sockaddr
*)&address
, 2);
1933 err
= -ECONNABORTED
;
1936 err
= move_addr_to_user(&address
,
1937 len
, upeer_sockaddr
, upeer_addrlen
);
1942 /* File flags are not inherited via accept() unlike another OSes. */
1946 return ERR_PTR(err
);
1949 static int __sys_accept4_file(struct file
*file
, struct sockaddr __user
*upeer_sockaddr
,
1950 int __user
*upeer_addrlen
, int flags
)
1952 struct proto_accept_arg arg
= { };
1953 struct file
*newfile
;
1956 if (flags
& ~(SOCK_CLOEXEC
| SOCK_NONBLOCK
))
1959 if (SOCK_NONBLOCK
!= O_NONBLOCK
&& (flags
& SOCK_NONBLOCK
))
1960 flags
= (flags
& ~SOCK_NONBLOCK
) | O_NONBLOCK
;
1962 newfd
= get_unused_fd_flags(flags
);
1963 if (unlikely(newfd
< 0))
1966 newfile
= do_accept(file
, &arg
, upeer_sockaddr
, upeer_addrlen
,
1968 if (IS_ERR(newfile
)) {
1969 put_unused_fd(newfd
);
1970 return PTR_ERR(newfile
);
1972 fd_install(newfd
, newfile
);
1977 * For accept, we attempt to create a new socket, set up the link
1978 * with the client, wake up the client, then return the new
1979 * connected fd. We collect the address of the connector in kernel
1980 * space and move it to user at the very end. This is unclean because
1981 * we open the socket then return an error.
1983 * 1003.1g adds the ability to recvmsg() to query connection pending
1984 * status to recvmsg. We need to add that support in a way thats
1985 * clean when we restructure accept also.
1988 int __sys_accept4(int fd
, struct sockaddr __user
*upeer_sockaddr
,
1989 int __user
*upeer_addrlen
, int flags
)
1995 return __sys_accept4_file(fd_file(f
), upeer_sockaddr
,
1996 upeer_addrlen
, flags
);
1999 SYSCALL_DEFINE4(accept4
, int, fd
, struct sockaddr __user
*, upeer_sockaddr
,
2000 int __user
*, upeer_addrlen
, int, flags
)
2002 return __sys_accept4(fd
, upeer_sockaddr
, upeer_addrlen
, flags
);
2005 SYSCALL_DEFINE3(accept
, int, fd
, struct sockaddr __user
*, upeer_sockaddr
,
2006 int __user
*, upeer_addrlen
)
2008 return __sys_accept4(fd
, upeer_sockaddr
, upeer_addrlen
, 0);
2012 * Attempt to connect to a socket with the server address. The address
2013 * is in user space so we verify it is OK and move it to kernel space.
2015 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
2018 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
2019 * other SEQPACKET protocols that take time to connect() as it doesn't
2020 * include the -EINPROGRESS status for such sockets.
2023 int __sys_connect_file(struct file
*file
, struct sockaddr_storage
*address
,
2024 int addrlen
, int file_flags
)
2026 struct socket
*sock
;
2029 sock
= sock_from_file(file
);
2036 security_socket_connect(sock
, (struct sockaddr
*)address
, addrlen
);
2040 err
= READ_ONCE(sock
->ops
)->connect(sock
, (struct sockaddr
*)address
,
2041 addrlen
, sock
->file
->f_flags
| file_flags
);
2046 int __sys_connect(int fd
, struct sockaddr __user
*uservaddr
, int addrlen
)
2048 struct sockaddr_storage address
;
2055 ret
= move_addr_to_kernel(uservaddr
, addrlen
, &address
);
2059 return __sys_connect_file(fd_file(f
), &address
, addrlen
, 0);
2062 SYSCALL_DEFINE3(connect
, int, fd
, struct sockaddr __user
*, uservaddr
,
2065 return __sys_connect(fd
, uservaddr
, addrlen
);
2069 * Get the local address ('name') of a socket object. Move the obtained
2070 * name to user space.
2073 int __sys_getsockname(int fd
, struct sockaddr __user
*usockaddr
,
2074 int __user
*usockaddr_len
)
2076 struct socket
*sock
;
2077 struct sockaddr_storage address
;
2083 sock
= sock_from_file(fd_file(f
));
2084 if (unlikely(!sock
))
2087 err
= security_socket_getsockname(sock
);
2091 err
= READ_ONCE(sock
->ops
)->getname(sock
, (struct sockaddr
*)&address
, 0);
2095 /* "err" is actually length in this case */
2096 return move_addr_to_user(&address
, err
, usockaddr
, usockaddr_len
);
2099 SYSCALL_DEFINE3(getsockname
, int, fd
, struct sockaddr __user
*, usockaddr
,
2100 int __user
*, usockaddr_len
)
2102 return __sys_getsockname(fd
, usockaddr
, usockaddr_len
);
2106 * Get the remote address ('name') of a socket object. Move the obtained
2107 * name to user space.
2110 int __sys_getpeername(int fd
, struct sockaddr __user
*usockaddr
,
2111 int __user
*usockaddr_len
)
2113 struct socket
*sock
;
2114 struct sockaddr_storage address
;
2120 sock
= sock_from_file(fd_file(f
));
2121 if (unlikely(!sock
))
2124 err
= security_socket_getpeername(sock
);
2128 err
= READ_ONCE(sock
->ops
)->getname(sock
, (struct sockaddr
*)&address
, 1);
2132 /* "err" is actually length in this case */
2133 return move_addr_to_user(&address
, err
, usockaddr
, usockaddr_len
);
2136 SYSCALL_DEFINE3(getpeername
, int, fd
, struct sockaddr __user
*, usockaddr
,
2137 int __user
*, usockaddr_len
)
2139 return __sys_getpeername(fd
, usockaddr
, usockaddr_len
);
2143 * Send a datagram to a given address. We move the address into kernel
2144 * space and check the user space data area is readable before invoking
2147 int __sys_sendto(int fd
, void __user
*buff
, size_t len
, unsigned int flags
,
2148 struct sockaddr __user
*addr
, int addr_len
)
2150 struct socket
*sock
;
2151 struct sockaddr_storage address
;
2155 err
= import_ubuf(ITER_SOURCE
, buff
, len
, &msg
.msg_iter
);
2162 sock
= sock_from_file(fd_file(f
));
2163 if (unlikely(!sock
))
2166 msg
.msg_name
= NULL
;
2167 msg
.msg_control
= NULL
;
2168 msg
.msg_controllen
= 0;
2169 msg
.msg_namelen
= 0;
2170 msg
.msg_ubuf
= NULL
;
2172 err
= move_addr_to_kernel(addr
, addr_len
, &address
);
2175 msg
.msg_name
= (struct sockaddr
*)&address
;
2176 msg
.msg_namelen
= addr_len
;
2178 flags
&= ~MSG_INTERNAL_SENDMSG_FLAGS
;
2179 if (sock
->file
->f_flags
& O_NONBLOCK
)
2180 flags
|= MSG_DONTWAIT
;
2181 msg
.msg_flags
= flags
;
2182 return __sock_sendmsg(sock
, &msg
);
2185 SYSCALL_DEFINE6(sendto
, int, fd
, void __user
*, buff
, size_t, len
,
2186 unsigned int, flags
, struct sockaddr __user
*, addr
,
2189 return __sys_sendto(fd
, buff
, len
, flags
, addr
, addr_len
);
2193 * Send a datagram down a socket.
2196 SYSCALL_DEFINE4(send
, int, fd
, void __user
*, buff
, size_t, len
,
2197 unsigned int, flags
)
2199 return __sys_sendto(fd
, buff
, len
, flags
, NULL
, 0);
2203 * Receive a frame from the socket and optionally record the address of the
2204 * sender. We verify the buffers are writable and if needed move the
2205 * sender address from kernel to user space.
2207 int __sys_recvfrom(int fd
, void __user
*ubuf
, size_t size
, unsigned int flags
,
2208 struct sockaddr __user
*addr
, int __user
*addr_len
)
2210 struct sockaddr_storage address
;
2211 struct msghdr msg
= {
2212 /* Save some cycles and don't copy the address if not needed */
2213 .msg_name
= addr
? (struct sockaddr
*)&address
: NULL
,
2215 struct socket
*sock
;
2218 err
= import_ubuf(ITER_DEST
, ubuf
, size
, &msg
.msg_iter
);
2226 sock
= sock_from_file(fd_file(f
));
2227 if (unlikely(!sock
))
2230 if (sock
->file
->f_flags
& O_NONBLOCK
)
2231 flags
|= MSG_DONTWAIT
;
2232 err
= sock_recvmsg(sock
, &msg
, flags
);
2234 if (err
>= 0 && addr
!= NULL
) {
2235 err2
= move_addr_to_user(&address
,
2236 msg
.msg_namelen
, addr
, addr_len
);
2243 SYSCALL_DEFINE6(recvfrom
, int, fd
, void __user
*, ubuf
, size_t, size
,
2244 unsigned int, flags
, struct sockaddr __user
*, addr
,
2245 int __user
*, addr_len
)
2247 return __sys_recvfrom(fd
, ubuf
, size
, flags
, addr
, addr_len
);
2251 * Receive a datagram from a socket.
2254 SYSCALL_DEFINE4(recv
, int, fd
, void __user
*, ubuf
, size_t, size
,
2255 unsigned int, flags
)
2257 return __sys_recvfrom(fd
, ubuf
, size
, flags
, NULL
, NULL
);
2260 static bool sock_use_custom_sol_socket(const struct socket
*sock
)
2262 return test_bit(SOCK_CUSTOM_SOCKOPT
, &sock
->flags
);
2265 int do_sock_setsockopt(struct socket
*sock
, bool compat
, int level
,
2266 int optname
, sockptr_t optval
, int optlen
)
2268 const struct proto_ops
*ops
;
2269 char *kernel_optval
= NULL
;
2275 err
= security_socket_setsockopt(sock
, level
, optname
);
2280 err
= BPF_CGROUP_RUN_PROG_SETSOCKOPT(sock
->sk
, &level
, &optname
,
2291 optval
= KERNEL_SOCKPTR(kernel_optval
);
2292 ops
= READ_ONCE(sock
->ops
);
2293 if (level
== SOL_SOCKET
&& !sock_use_custom_sol_socket(sock
))
2294 err
= sock_setsockopt(sock
, level
, optname
, optval
, optlen
);
2295 else if (unlikely(!ops
->setsockopt
))
2298 err
= ops
->setsockopt(sock
, level
, optname
, optval
,
2300 kfree(kernel_optval
);
2304 EXPORT_SYMBOL(do_sock_setsockopt
);
2306 /* Set a socket option. Because we don't know the option lengths we have
2307 * to pass the user mode parameter for the protocols to sort out.
2309 int __sys_setsockopt(int fd
, int level
, int optname
, char __user
*user_optval
,
2312 sockptr_t optval
= USER_SOCKPTR(user_optval
);
2313 bool compat
= in_compat_syscall();
2314 struct socket
*sock
;
2319 sock
= sock_from_file(fd_file(f
));
2320 if (unlikely(!sock
))
2323 return do_sock_setsockopt(sock
, compat
, level
, optname
, optval
, optlen
);
2326 SYSCALL_DEFINE5(setsockopt
, int, fd
, int, level
, int, optname
,
2327 char __user
*, optval
, int, optlen
)
2329 return __sys_setsockopt(fd
, level
, optname
, optval
, optlen
);
2332 INDIRECT_CALLABLE_DECLARE(bool tcp_bpf_bypass_getsockopt(int level
,
2335 int do_sock_getsockopt(struct socket
*sock
, bool compat
, int level
,
2336 int optname
, sockptr_t optval
, sockptr_t optlen
)
2338 int max_optlen __maybe_unused
= 0;
2339 const struct proto_ops
*ops
;
2342 err
= security_socket_getsockopt(sock
, level
, optname
);
2347 copy_from_sockptr(&max_optlen
, optlen
, sizeof(int));
2349 ops
= READ_ONCE(sock
->ops
);
2350 if (level
== SOL_SOCKET
) {
2351 err
= sk_getsockopt(sock
->sk
, level
, optname
, optval
, optlen
);
2352 } else if (unlikely(!ops
->getsockopt
)) {
2355 if (WARN_ONCE(optval
.is_kernel
|| optlen
.is_kernel
,
2356 "Invalid argument type"))
2359 err
= ops
->getsockopt(sock
, level
, optname
, optval
.user
,
2364 err
= BPF_CGROUP_RUN_PROG_GETSOCKOPT(sock
->sk
, level
, optname
,
2365 optval
, optlen
, max_optlen
,
2370 EXPORT_SYMBOL(do_sock_getsockopt
);
2373 * Get a socket option. Because we don't know the option lengths we have
2374 * to pass a user mode parameter for the protocols to sort out.
2376 int __sys_getsockopt(int fd
, int level
, int optname
, char __user
*optval
,
2379 struct socket
*sock
;
2384 sock
= sock_from_file(fd_file(f
));
2385 if (unlikely(!sock
))
2388 return do_sock_getsockopt(sock
, in_compat_syscall(), level
, optname
,
2389 USER_SOCKPTR(optval
), USER_SOCKPTR(optlen
));
2392 SYSCALL_DEFINE5(getsockopt
, int, fd
, int, level
, int, optname
,
2393 char __user
*, optval
, int __user
*, optlen
)
2395 return __sys_getsockopt(fd
, level
, optname
, optval
, optlen
);
2399 * Shutdown a socket.
2402 int __sys_shutdown_sock(struct socket
*sock
, int how
)
2406 err
= security_socket_shutdown(sock
, how
);
2408 err
= READ_ONCE(sock
->ops
)->shutdown(sock
, how
);
2413 int __sys_shutdown(int fd
, int how
)
2415 struct socket
*sock
;
2420 sock
= sock_from_file(fd_file(f
));
2421 if (unlikely(!sock
))
2424 return __sys_shutdown_sock(sock
, how
);
2427 SYSCALL_DEFINE2(shutdown
, int, fd
, int, how
)
2429 return __sys_shutdown(fd
, how
);
2432 /* A couple of helpful macros for getting the address of the 32/64 bit
2433 * fields which are the same type (int / unsigned) on our platforms.
2435 #define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
2436 #define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
2437 #define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
2439 struct used_address
{
2440 struct sockaddr_storage name
;
2441 unsigned int name_len
;
2444 int __copy_msghdr(struct msghdr
*kmsg
,
2445 struct user_msghdr
*msg
,
2446 struct sockaddr __user
**save_addr
)
2450 kmsg
->msg_control_is_user
= true;
2451 kmsg
->msg_get_inq
= 0;
2452 kmsg
->msg_control_user
= msg
->msg_control
;
2453 kmsg
->msg_controllen
= msg
->msg_controllen
;
2454 kmsg
->msg_flags
= msg
->msg_flags
;
2456 kmsg
->msg_namelen
= msg
->msg_namelen
;
2458 kmsg
->msg_namelen
= 0;
2460 if (kmsg
->msg_namelen
< 0)
2463 if (kmsg
->msg_namelen
> sizeof(struct sockaddr_storage
))
2464 kmsg
->msg_namelen
= sizeof(struct sockaddr_storage
);
2467 *save_addr
= msg
->msg_name
;
2469 if (msg
->msg_name
&& kmsg
->msg_namelen
) {
2471 err
= move_addr_to_kernel(msg
->msg_name
,
2478 kmsg
->msg_name
= NULL
;
2479 kmsg
->msg_namelen
= 0;
2482 if (msg
->msg_iovlen
> UIO_MAXIOV
)
2485 kmsg
->msg_iocb
= NULL
;
2486 kmsg
->msg_ubuf
= NULL
;
2490 static int copy_msghdr_from_user(struct msghdr
*kmsg
,
2491 struct user_msghdr __user
*umsg
,
2492 struct sockaddr __user
**save_addr
,
2495 struct user_msghdr msg
;
2498 if (copy_from_user(&msg
, umsg
, sizeof(*umsg
)))
2501 err
= __copy_msghdr(kmsg
, &msg
, save_addr
);
2505 err
= import_iovec(save_addr
? ITER_DEST
: ITER_SOURCE
,
2506 msg
.msg_iov
, msg
.msg_iovlen
,
2507 UIO_FASTIOV
, iov
, &kmsg
->msg_iter
);
2508 return err
< 0 ? err
: 0;
2511 static int ____sys_sendmsg(struct socket
*sock
, struct msghdr
*msg_sys
,
2512 unsigned int flags
, struct used_address
*used_address
,
2513 unsigned int allowed_msghdr_flags
)
2515 unsigned char ctl
[sizeof(struct cmsghdr
) + 20]
2516 __aligned(sizeof(__kernel_size_t
));
2517 /* 20 is size of ipv6_pktinfo */
2518 unsigned char *ctl_buf
= ctl
;
2524 if (msg_sys
->msg_controllen
> INT_MAX
)
2526 flags
|= (msg_sys
->msg_flags
& allowed_msghdr_flags
);
2527 ctl_len
= msg_sys
->msg_controllen
;
2528 if ((MSG_CMSG_COMPAT
& flags
) && ctl_len
) {
2530 cmsghdr_from_user_compat_to_kern(msg_sys
, sock
->sk
, ctl
,
2534 ctl_buf
= msg_sys
->msg_control
;
2535 ctl_len
= msg_sys
->msg_controllen
;
2536 } else if (ctl_len
) {
2537 BUILD_BUG_ON(sizeof(struct cmsghdr
) !=
2538 CMSG_ALIGN(sizeof(struct cmsghdr
)));
2539 if (ctl_len
> sizeof(ctl
)) {
2540 ctl_buf
= sock_kmalloc(sock
->sk
, ctl_len
, GFP_KERNEL
);
2541 if (ctl_buf
== NULL
)
2545 if (copy_from_user(ctl_buf
, msg_sys
->msg_control_user
, ctl_len
))
2547 msg_sys
->msg_control
= ctl_buf
;
2548 msg_sys
->msg_control_is_user
= false;
2550 flags
&= ~MSG_INTERNAL_SENDMSG_FLAGS
;
2551 msg_sys
->msg_flags
= flags
;
2553 if (sock
->file
->f_flags
& O_NONBLOCK
)
2554 msg_sys
->msg_flags
|= MSG_DONTWAIT
;
2556 * If this is sendmmsg() and current destination address is same as
2557 * previously succeeded address, omit asking LSM's decision.
2558 * used_address->name_len is initialized to UINT_MAX so that the first
2559 * destination address never matches.
2561 if (used_address
&& msg_sys
->msg_name
&&
2562 used_address
->name_len
== msg_sys
->msg_namelen
&&
2563 !memcmp(&used_address
->name
, msg_sys
->msg_name
,
2564 used_address
->name_len
)) {
2565 err
= sock_sendmsg_nosec(sock
, msg_sys
);
2568 err
= __sock_sendmsg(sock
, msg_sys
);
2570 * If this is sendmmsg() and sending to current destination address was
2571 * successful, remember it.
2573 if (used_address
&& err
>= 0) {
2574 used_address
->name_len
= msg_sys
->msg_namelen
;
2575 if (msg_sys
->msg_name
)
2576 memcpy(&used_address
->name
, msg_sys
->msg_name
,
2577 used_address
->name_len
);
2582 sock_kfree_s(sock
->sk
, ctl_buf
, ctl_len
);
2587 static int sendmsg_copy_msghdr(struct msghdr
*msg
,
2588 struct user_msghdr __user
*umsg
, unsigned flags
,
2593 if (flags
& MSG_CMSG_COMPAT
) {
2594 struct compat_msghdr __user
*msg_compat
;
2596 msg_compat
= (struct compat_msghdr __user
*) umsg
;
2597 err
= get_compat_msghdr(msg
, msg_compat
, NULL
, iov
);
2599 err
= copy_msghdr_from_user(msg
, umsg
, NULL
, iov
);
2607 static int ___sys_sendmsg(struct socket
*sock
, struct user_msghdr __user
*msg
,
2608 struct msghdr
*msg_sys
, unsigned int flags
,
2609 struct used_address
*used_address
,
2610 unsigned int allowed_msghdr_flags
)
2612 struct sockaddr_storage address
;
2613 struct iovec iovstack
[UIO_FASTIOV
], *iov
= iovstack
;
2616 msg_sys
->msg_name
= &address
;
2618 err
= sendmsg_copy_msghdr(msg_sys
, msg
, flags
, &iov
);
2622 err
= ____sys_sendmsg(sock
, msg_sys
, flags
, used_address
,
2623 allowed_msghdr_flags
);
2629 * BSD sendmsg interface
2631 long __sys_sendmsg_sock(struct socket
*sock
, struct msghdr
*msg
,
2634 return ____sys_sendmsg(sock
, msg
, flags
, NULL
, 0);
2637 long __sys_sendmsg(int fd
, struct user_msghdr __user
*msg
, unsigned int flags
,
2638 bool forbid_cmsg_compat
)
2640 struct msghdr msg_sys
;
2641 struct socket
*sock
;
2643 if (forbid_cmsg_compat
&& (flags
& MSG_CMSG_COMPAT
))
2650 sock
= sock_from_file(fd_file(f
));
2651 if (unlikely(!sock
))
2654 return ___sys_sendmsg(sock
, msg
, &msg_sys
, flags
, NULL
, 0);
2657 SYSCALL_DEFINE3(sendmsg
, int, fd
, struct user_msghdr __user
*, msg
, unsigned int, flags
)
2659 return __sys_sendmsg(fd
, msg
, flags
, true);
2663 * Linux sendmmsg interface
2666 int __sys_sendmmsg(int fd
, struct mmsghdr __user
*mmsg
, unsigned int vlen
,
2667 unsigned int flags
, bool forbid_cmsg_compat
)
2670 struct socket
*sock
;
2671 struct mmsghdr __user
*entry
;
2672 struct compat_mmsghdr __user
*compat_entry
;
2673 struct msghdr msg_sys
;
2674 struct used_address used_address
;
2675 unsigned int oflags
= flags
;
2677 if (forbid_cmsg_compat
&& (flags
& MSG_CMSG_COMPAT
))
2680 if (vlen
> UIO_MAXIOV
)
2689 sock
= sock_from_file(fd_file(f
));
2690 if (unlikely(!sock
))
2693 used_address
.name_len
= UINT_MAX
;
2695 compat_entry
= (struct compat_mmsghdr __user
*)mmsg
;
2699 while (datagrams
< vlen
) {
2700 if (datagrams
== vlen
- 1)
2703 if (MSG_CMSG_COMPAT
& flags
) {
2704 err
= ___sys_sendmsg(sock
, (struct user_msghdr __user
*)compat_entry
,
2705 &msg_sys
, flags
, &used_address
, MSG_EOR
);
2708 err
= __put_user(err
, &compat_entry
->msg_len
);
2711 err
= ___sys_sendmsg(sock
,
2712 (struct user_msghdr __user
*)entry
,
2713 &msg_sys
, flags
, &used_address
, MSG_EOR
);
2716 err
= put_user(err
, &entry
->msg_len
);
2723 if (msg_data_left(&msg_sys
))
2728 /* We only return an error if no datagrams were able to be sent */
2735 SYSCALL_DEFINE4(sendmmsg
, int, fd
, struct mmsghdr __user
*, mmsg
,
2736 unsigned int, vlen
, unsigned int, flags
)
2738 return __sys_sendmmsg(fd
, mmsg
, vlen
, flags
, true);
2741 static int recvmsg_copy_msghdr(struct msghdr
*msg
,
2742 struct user_msghdr __user
*umsg
, unsigned flags
,
2743 struct sockaddr __user
**uaddr
,
2748 if (MSG_CMSG_COMPAT
& flags
) {
2749 struct compat_msghdr __user
*msg_compat
;
2751 msg_compat
= (struct compat_msghdr __user
*) umsg
;
2752 err
= get_compat_msghdr(msg
, msg_compat
, uaddr
, iov
);
2754 err
= copy_msghdr_from_user(msg
, umsg
, uaddr
, iov
);
2762 static int ____sys_recvmsg(struct socket
*sock
, struct msghdr
*msg_sys
,
2763 struct user_msghdr __user
*msg
,
2764 struct sockaddr __user
*uaddr
,
2765 unsigned int flags
, int nosec
)
2767 struct compat_msghdr __user
*msg_compat
=
2768 (struct compat_msghdr __user
*) msg
;
2769 int __user
*uaddr_len
= COMPAT_NAMELEN(msg
);
2770 struct sockaddr_storage addr
;
2771 unsigned long cmsg_ptr
;
2775 msg_sys
->msg_name
= &addr
;
2776 cmsg_ptr
= (unsigned long)msg_sys
->msg_control
;
2777 msg_sys
->msg_flags
= flags
& (MSG_CMSG_CLOEXEC
|MSG_CMSG_COMPAT
);
2779 /* We assume all kernel code knows the size of sockaddr_storage */
2780 msg_sys
->msg_namelen
= 0;
2782 if (sock
->file
->f_flags
& O_NONBLOCK
)
2783 flags
|= MSG_DONTWAIT
;
2785 if (unlikely(nosec
))
2786 err
= sock_recvmsg_nosec(sock
, msg_sys
, flags
);
2788 err
= sock_recvmsg(sock
, msg_sys
, flags
);
2794 if (uaddr
!= NULL
) {
2795 err
= move_addr_to_user(&addr
,
2796 msg_sys
->msg_namelen
, uaddr
,
2801 err
= __put_user((msg_sys
->msg_flags
& ~MSG_CMSG_COMPAT
),
2805 if (MSG_CMSG_COMPAT
& flags
)
2806 err
= __put_user((unsigned long)msg_sys
->msg_control
- cmsg_ptr
,
2807 &msg_compat
->msg_controllen
);
2809 err
= __put_user((unsigned long)msg_sys
->msg_control
- cmsg_ptr
,
2810 &msg
->msg_controllen
);
2818 static int ___sys_recvmsg(struct socket
*sock
, struct user_msghdr __user
*msg
,
2819 struct msghdr
*msg_sys
, unsigned int flags
, int nosec
)
2821 struct iovec iovstack
[UIO_FASTIOV
], *iov
= iovstack
;
2822 /* user mode address pointers */
2823 struct sockaddr __user
*uaddr
;
2826 err
= recvmsg_copy_msghdr(msg_sys
, msg
, flags
, &uaddr
, &iov
);
2830 err
= ____sys_recvmsg(sock
, msg_sys
, msg
, uaddr
, flags
, nosec
);
2836 * BSD recvmsg interface
2839 long __sys_recvmsg_sock(struct socket
*sock
, struct msghdr
*msg
,
2840 struct user_msghdr __user
*umsg
,
2841 struct sockaddr __user
*uaddr
, unsigned int flags
)
2843 return ____sys_recvmsg(sock
, msg
, umsg
, uaddr
, flags
, 0);
2846 long __sys_recvmsg(int fd
, struct user_msghdr __user
*msg
, unsigned int flags
,
2847 bool forbid_cmsg_compat
)
2849 struct msghdr msg_sys
;
2850 struct socket
*sock
;
2852 if (forbid_cmsg_compat
&& (flags
& MSG_CMSG_COMPAT
))
2859 sock
= sock_from_file(fd_file(f
));
2860 if (unlikely(!sock
))
2863 return ___sys_recvmsg(sock
, msg
, &msg_sys
, flags
, 0);
2866 SYSCALL_DEFINE3(recvmsg
, int, fd
, struct user_msghdr __user
*, msg
,
2867 unsigned int, flags
)
2869 return __sys_recvmsg(fd
, msg
, flags
, true);
2873 * Linux recvmmsg interface
2876 static int do_recvmmsg(int fd
, struct mmsghdr __user
*mmsg
,
2877 unsigned int vlen
, unsigned int flags
,
2878 struct timespec64
*timeout
)
2880 int err
= 0, datagrams
;
2881 struct socket
*sock
;
2882 struct mmsghdr __user
*entry
;
2883 struct compat_mmsghdr __user
*compat_entry
;
2884 struct msghdr msg_sys
;
2885 struct timespec64 end_time
;
2886 struct timespec64 timeout64
;
2889 poll_select_set_timeout(&end_time
, timeout
->tv_sec
,
2899 sock
= sock_from_file(fd_file(f
));
2900 if (unlikely(!sock
))
2903 if (likely(!(flags
& MSG_ERRQUEUE
))) {
2904 err
= sock_error(sock
->sk
);
2910 compat_entry
= (struct compat_mmsghdr __user
*)mmsg
;
2912 while (datagrams
< vlen
) {
2914 * No need to ask LSM for more than the first datagram.
2916 if (MSG_CMSG_COMPAT
& flags
) {
2917 err
= ___sys_recvmsg(sock
, (struct user_msghdr __user
*)compat_entry
,
2918 &msg_sys
, flags
& ~MSG_WAITFORONE
,
2922 err
= __put_user(err
, &compat_entry
->msg_len
);
2925 err
= ___sys_recvmsg(sock
,
2926 (struct user_msghdr __user
*)entry
,
2927 &msg_sys
, flags
& ~MSG_WAITFORONE
,
2931 err
= put_user(err
, &entry
->msg_len
);
2939 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
2940 if (flags
& MSG_WAITFORONE
)
2941 flags
|= MSG_DONTWAIT
;
2944 ktime_get_ts64(&timeout64
);
2945 *timeout
= timespec64_sub(end_time
, timeout64
);
2946 if (timeout
->tv_sec
< 0) {
2947 timeout
->tv_sec
= timeout
->tv_nsec
= 0;
2951 /* Timeout, return less than vlen datagrams */
2952 if (timeout
->tv_nsec
== 0 && timeout
->tv_sec
== 0)
2956 /* Out of band data, return right away */
2957 if (msg_sys
.msg_flags
& MSG_OOB
)
2969 * We may return less entries than requested (vlen) if the
2970 * sock is non block and there aren't enough datagrams...
2972 if (err
!= -EAGAIN
) {
2974 * ... or if recvmsg returns an error after we
2975 * received some datagrams, where we record the
2976 * error to return on the next call or if the
2977 * app asks about it using getsockopt(SO_ERROR).
2979 WRITE_ONCE(sock
->sk
->sk_err
, -err
);
2984 int __sys_recvmmsg(int fd
, struct mmsghdr __user
*mmsg
,
2985 unsigned int vlen
, unsigned int flags
,
2986 struct __kernel_timespec __user
*timeout
,
2987 struct old_timespec32 __user
*timeout32
)
2990 struct timespec64 timeout_sys
;
2992 if (timeout
&& get_timespec64(&timeout_sys
, timeout
))
2995 if (timeout32
&& get_old_timespec32(&timeout_sys
, timeout32
))
2998 if (!timeout
&& !timeout32
)
2999 return do_recvmmsg(fd
, mmsg
, vlen
, flags
, NULL
);
3001 datagrams
= do_recvmmsg(fd
, mmsg
, vlen
, flags
, &timeout_sys
);
3006 if (timeout
&& put_timespec64(&timeout_sys
, timeout
))
3007 datagrams
= -EFAULT
;
3009 if (timeout32
&& put_old_timespec32(&timeout_sys
, timeout32
))
3010 datagrams
= -EFAULT
;
3015 SYSCALL_DEFINE5(recvmmsg
, int, fd
, struct mmsghdr __user
*, mmsg
,
3016 unsigned int, vlen
, unsigned int, flags
,
3017 struct __kernel_timespec __user
*, timeout
)
3019 if (flags
& MSG_CMSG_COMPAT
)
3022 return __sys_recvmmsg(fd
, mmsg
, vlen
, flags
, timeout
, NULL
);
3025 #ifdef CONFIG_COMPAT_32BIT_TIME
3026 SYSCALL_DEFINE5(recvmmsg_time32
, int, fd
, struct mmsghdr __user
*, mmsg
,
3027 unsigned int, vlen
, unsigned int, flags
,
3028 struct old_timespec32 __user
*, timeout
)
3030 if (flags
& MSG_CMSG_COMPAT
)
3033 return __sys_recvmmsg(fd
, mmsg
, vlen
, flags
, NULL
, timeout
);
3037 #ifdef __ARCH_WANT_SYS_SOCKETCALL
3038 /* Argument list sizes for sys_socketcall */
3039 #define AL(x) ((x) * sizeof(unsigned long))
3040 static const unsigned char nargs
[21] = {
3041 AL(0), AL(3), AL(3), AL(3), AL(2), AL(3),
3042 AL(3), AL(3), AL(4), AL(4), AL(4), AL(6),
3043 AL(6), AL(2), AL(5), AL(5), AL(3), AL(3),
3050 * System call vectors.
3052 * Argument checking cleaned up. Saved 20% in size.
3053 * This function doesn't need to set the kernel lock because
3054 * it is set by the callees.
3057 SYSCALL_DEFINE2(socketcall
, int, call
, unsigned long __user
*, args
)
3059 unsigned long a
[AUDITSC_ARGS
];
3060 unsigned long a0
, a1
;
3064 if (call
< 1 || call
> SYS_SENDMMSG
)
3066 call
= array_index_nospec(call
, SYS_SENDMMSG
+ 1);
3069 if (len
> sizeof(a
))
3072 /* copy_from_user should be SMP safe. */
3073 if (copy_from_user(a
, args
, len
))
3076 err
= audit_socketcall(nargs
[call
] / sizeof(unsigned long), a
);
3085 err
= __sys_socket(a0
, a1
, a
[2]);
3088 err
= __sys_bind(a0
, (struct sockaddr __user
*)a1
, a
[2]);
3091 err
= __sys_connect(a0
, (struct sockaddr __user
*)a1
, a
[2]);
3094 err
= __sys_listen(a0
, a1
);
3097 err
= __sys_accept4(a0
, (struct sockaddr __user
*)a1
,
3098 (int __user
*)a
[2], 0);
3100 case SYS_GETSOCKNAME
:
3102 __sys_getsockname(a0
, (struct sockaddr __user
*)a1
,
3103 (int __user
*)a
[2]);
3105 case SYS_GETPEERNAME
:
3107 __sys_getpeername(a0
, (struct sockaddr __user
*)a1
,
3108 (int __user
*)a
[2]);
3110 case SYS_SOCKETPAIR
:
3111 err
= __sys_socketpair(a0
, a1
, a
[2], (int __user
*)a
[3]);
3114 err
= __sys_sendto(a0
, (void __user
*)a1
, a
[2], a
[3],
3118 err
= __sys_sendto(a0
, (void __user
*)a1
, a
[2], a
[3],
3119 (struct sockaddr __user
*)a
[4], a
[5]);
3122 err
= __sys_recvfrom(a0
, (void __user
*)a1
, a
[2], a
[3],
3126 err
= __sys_recvfrom(a0
, (void __user
*)a1
, a
[2], a
[3],
3127 (struct sockaddr __user
*)a
[4],
3128 (int __user
*)a
[5]);
3131 err
= __sys_shutdown(a0
, a1
);
3133 case SYS_SETSOCKOPT
:
3134 err
= __sys_setsockopt(a0
, a1
, a
[2], (char __user
*)a
[3],
3137 case SYS_GETSOCKOPT
:
3139 __sys_getsockopt(a0
, a1
, a
[2], (char __user
*)a
[3],
3140 (int __user
*)a
[4]);
3143 err
= __sys_sendmsg(a0
, (struct user_msghdr __user
*)a1
,
3147 err
= __sys_sendmmsg(a0
, (struct mmsghdr __user
*)a1
, a
[2],
3151 err
= __sys_recvmsg(a0
, (struct user_msghdr __user
*)a1
,
3155 if (IS_ENABLED(CONFIG_64BIT
))
3156 err
= __sys_recvmmsg(a0
, (struct mmsghdr __user
*)a1
,
3158 (struct __kernel_timespec __user
*)a
[4],
3161 err
= __sys_recvmmsg(a0
, (struct mmsghdr __user
*)a1
,
3163 (struct old_timespec32 __user
*)a
[4]);
3166 err
= __sys_accept4(a0
, (struct sockaddr __user
*)a1
,
3167 (int __user
*)a
[2], a
[3]);
3176 #endif /* __ARCH_WANT_SYS_SOCKETCALL */
3179 * sock_register - add a socket protocol handler
3180 * @ops: description of protocol
3182 * This function is called by a protocol handler that wants to
3183 * advertise its address family, and have it linked into the
3184 * socket interface. The value ops->family corresponds to the
3185 * socket system call protocol family.
3187 int sock_register(const struct net_proto_family
*ops
)
3191 if (ops
->family
>= NPROTO
) {
3192 pr_crit("protocol %d >= NPROTO(%d)\n", ops
->family
, NPROTO
);
3196 spin_lock(&net_family_lock
);
3197 if (rcu_dereference_protected(net_families
[ops
->family
],
3198 lockdep_is_held(&net_family_lock
)))
3201 rcu_assign_pointer(net_families
[ops
->family
], ops
);
3204 spin_unlock(&net_family_lock
);
3206 pr_info("NET: Registered %s protocol family\n", pf_family_names
[ops
->family
]);
3209 EXPORT_SYMBOL(sock_register
);
3212 * sock_unregister - remove a protocol handler
3213 * @family: protocol family to remove
3215 * This function is called by a protocol handler that wants to
3216 * remove its address family, and have it unlinked from the
3217 * new socket creation.
3219 * If protocol handler is a module, then it can use module reference
3220 * counts to protect against new references. If protocol handler is not
3221 * a module then it needs to provide its own protection in
3222 * the ops->create routine.
3224 void sock_unregister(int family
)
3226 BUG_ON(family
< 0 || family
>= NPROTO
);
3228 spin_lock(&net_family_lock
);
3229 RCU_INIT_POINTER(net_families
[family
], NULL
);
3230 spin_unlock(&net_family_lock
);
3234 pr_info("NET: Unregistered %s protocol family\n", pf_family_names
[family
]);
3236 EXPORT_SYMBOL(sock_unregister
);
3238 bool sock_is_registered(int family
)
3240 return family
< NPROTO
&& rcu_access_pointer(net_families
[family
]);
3243 static int __init
sock_init(void)
3247 * Initialize the network sysctl infrastructure.
3249 err
= net_sysctl_init();
3254 * Initialize skbuff SLAB cache
3259 * Initialize the protocols module.
3264 err
= register_filesystem(&sock_fs_type
);
3267 sock_mnt
= kern_mount(&sock_fs_type
);
3268 if (IS_ERR(sock_mnt
)) {
3269 err
= PTR_ERR(sock_mnt
);
3273 /* The real protocol initialization is performed in later initcalls.
3276 #ifdef CONFIG_NETFILTER
3277 err
= netfilter_init();
3282 ptp_classifier_init();
3288 unregister_filesystem(&sock_fs_type
);
3292 core_initcall(sock_init
); /* early initcall */
3294 #ifdef CONFIG_PROC_FS
3295 void socket_seq_show(struct seq_file
*seq
)
3297 seq_printf(seq
, "sockets: used %d\n",
3298 sock_inuse_get(seq
->private));
3300 #endif /* CONFIG_PROC_FS */
3302 /* Handle the fact that while struct ifreq has the same *layout* on
3303 * 32/64 for everything but ifreq::ifru_ifmap and ifreq::ifru_data,
3304 * which are handled elsewhere, it still has different *size* due to
3305 * ifreq::ifru_ifmap (which is 16 bytes on 32 bit, 24 bytes on 64-bit,
3306 * resulting in struct ifreq being 32 and 40 bytes respectively).
3307 * As a result, if the struct happens to be at the end of a page and
3308 * the next page isn't readable/writable, we get a fault. To prevent
3309 * that, copy back and forth to the full size.
3311 int get_user_ifreq(struct ifreq
*ifr
, void __user
**ifrdata
, void __user
*arg
)
3313 if (in_compat_syscall()) {
3314 struct compat_ifreq
*ifr32
= (struct compat_ifreq
*)ifr
;
3316 memset(ifr
, 0, sizeof(*ifr
));
3317 if (copy_from_user(ifr32
, arg
, sizeof(*ifr32
)))
3321 *ifrdata
= compat_ptr(ifr32
->ifr_data
);
3326 if (copy_from_user(ifr
, arg
, sizeof(*ifr
)))
3330 *ifrdata
= ifr
->ifr_data
;
3334 EXPORT_SYMBOL(get_user_ifreq
);
3336 int put_user_ifreq(struct ifreq
*ifr
, void __user
*arg
)
3338 size_t size
= sizeof(*ifr
);
3340 if (in_compat_syscall())
3341 size
= sizeof(struct compat_ifreq
);
3343 if (copy_to_user(arg
, ifr
, size
))
3348 EXPORT_SYMBOL(put_user_ifreq
);
3350 #ifdef CONFIG_COMPAT
3351 static int compat_siocwandev(struct net
*net
, struct compat_ifreq __user
*uifr32
)
3353 compat_uptr_t uptr32
;
3358 if (get_user_ifreq(&ifr
, NULL
, uifr32
))
3361 if (get_user(uptr32
, &uifr32
->ifr_settings
.ifs_ifsu
))
3364 saved
= ifr
.ifr_settings
.ifs_ifsu
.raw_hdlc
;
3365 ifr
.ifr_settings
.ifs_ifsu
.raw_hdlc
= compat_ptr(uptr32
);
3367 err
= dev_ioctl(net
, SIOCWANDEV
, &ifr
, NULL
, NULL
);
3369 ifr
.ifr_settings
.ifs_ifsu
.raw_hdlc
= saved
;
3370 if (put_user_ifreq(&ifr
, uifr32
))
3376 /* Handle ioctls that use ifreq::ifr_data and just need struct ifreq converted */
3377 static int compat_ifr_data_ioctl(struct net
*net
, unsigned int cmd
,
3378 struct compat_ifreq __user
*u_ifreq32
)
3383 if (!is_socket_ioctl_cmd(cmd
))
3385 if (get_user_ifreq(&ifreq
, &data
, u_ifreq32
))
3387 ifreq
.ifr_data
= data
;
3389 return dev_ioctl(net
, cmd
, &ifreq
, data
, NULL
);
3392 static int compat_sock_ioctl_trans(struct file
*file
, struct socket
*sock
,
3393 unsigned int cmd
, unsigned long arg
)
3395 void __user
*argp
= compat_ptr(arg
);
3396 struct sock
*sk
= sock
->sk
;
3397 struct net
*net
= sock_net(sk
);
3398 const struct proto_ops
*ops
;
3400 if (cmd
>= SIOCDEVPRIVATE
&& cmd
<= (SIOCDEVPRIVATE
+ 15))
3401 return sock_ioctl(file
, cmd
, (unsigned long)argp
);
3405 return compat_siocwandev(net
, argp
);
3406 case SIOCGSTAMP_OLD
:
3407 case SIOCGSTAMPNS_OLD
:
3408 ops
= READ_ONCE(sock
->ops
);
3409 if (!ops
->gettstamp
)
3410 return -ENOIOCTLCMD
;
3411 return ops
->gettstamp(sock
, argp
, cmd
== SIOCGSTAMP_OLD
,
3412 !COMPAT_USE_64BIT_TIME
);
3415 case SIOCBONDSLAVEINFOQUERY
:
3416 case SIOCBONDINFOQUERY
:
3419 return compat_ifr_data_ioctl(net
, cmd
, argp
);
3430 case SIOCGSTAMP_NEW
:
3431 case SIOCGSTAMPNS_NEW
:
3435 return sock_ioctl(file
, cmd
, arg
);
3454 case SIOCSIFHWBROADCAST
:
3456 case SIOCGIFBRDADDR
:
3457 case SIOCSIFBRDADDR
:
3458 case SIOCGIFDSTADDR
:
3459 case SIOCSIFDSTADDR
:
3460 case SIOCGIFNETMASK
:
3461 case SIOCSIFNETMASK
:
3473 case SIOCBONDENSLAVE
:
3474 case SIOCBONDRELEASE
:
3475 case SIOCBONDSETHWADDR
:
3476 case SIOCBONDCHANGEACTIVE
:
3483 return sock_do_ioctl(net
, sock
, cmd
, arg
);
3486 return -ENOIOCTLCMD
;
3489 static long compat_sock_ioctl(struct file
*file
, unsigned int cmd
,
3492 struct socket
*sock
= file
->private_data
;
3493 const struct proto_ops
*ops
= READ_ONCE(sock
->ops
);
3494 int ret
= -ENOIOCTLCMD
;
3501 if (ops
->compat_ioctl
)
3502 ret
= ops
->compat_ioctl(sock
, cmd
, arg
);
3504 if (ret
== -ENOIOCTLCMD
&&
3505 (cmd
>= SIOCIWFIRST
&& cmd
<= SIOCIWLAST
))
3506 ret
= compat_wext_handle_ioctl(net
, cmd
, arg
);
3508 if (ret
== -ENOIOCTLCMD
)
3509 ret
= compat_sock_ioctl_trans(file
, sock
, cmd
, arg
);
3516 * kernel_bind - bind an address to a socket (kernel space)
3519 * @addrlen: length of address
3521 * Returns 0 or an error.
3524 int kernel_bind(struct socket
*sock
, struct sockaddr
*addr
, int addrlen
)
3526 struct sockaddr_storage address
;
3528 memcpy(&address
, addr
, addrlen
);
3530 return READ_ONCE(sock
->ops
)->bind(sock
, (struct sockaddr
*)&address
,
3533 EXPORT_SYMBOL(kernel_bind
);
3536 * kernel_listen - move socket to listening state (kernel space)
3538 * @backlog: pending connections queue size
3540 * Returns 0 or an error.
3543 int kernel_listen(struct socket
*sock
, int backlog
)
3545 return READ_ONCE(sock
->ops
)->listen(sock
, backlog
);
3547 EXPORT_SYMBOL(kernel_listen
);
3550 * kernel_accept - accept a connection (kernel space)
3551 * @sock: listening socket
3552 * @newsock: new connected socket
3555 * @flags must be SOCK_CLOEXEC, SOCK_NONBLOCK or 0.
3556 * If it fails, @newsock is guaranteed to be %NULL.
3557 * Returns 0 or an error.
3560 int kernel_accept(struct socket
*sock
, struct socket
**newsock
, int flags
)
3562 struct sock
*sk
= sock
->sk
;
3563 const struct proto_ops
*ops
= READ_ONCE(sock
->ops
);
3564 struct proto_accept_arg arg
= {
3570 err
= sock_create_lite(sk
->sk_family
, sk
->sk_type
, sk
->sk_protocol
,
3575 err
= ops
->accept(sock
, *newsock
, &arg
);
3577 sock_release(*newsock
);
3582 (*newsock
)->ops
= ops
;
3583 __module_get(ops
->owner
);
3588 EXPORT_SYMBOL(kernel_accept
);
3591 * kernel_connect - connect a socket (kernel space)
3594 * @addrlen: address length
3595 * @flags: flags (O_NONBLOCK, ...)
3597 * For datagram sockets, @addr is the address to which datagrams are sent
3598 * by default, and the only address from which datagrams are received.
3599 * For stream sockets, attempts to connect to @addr.
3600 * Returns 0 or an error code.
3603 int kernel_connect(struct socket
*sock
, struct sockaddr
*addr
, int addrlen
,
3606 struct sockaddr_storage address
;
3608 memcpy(&address
, addr
, addrlen
);
3610 return READ_ONCE(sock
->ops
)->connect(sock
, (struct sockaddr
*)&address
,
3613 EXPORT_SYMBOL(kernel_connect
);
3616 * kernel_getsockname - get the address which the socket is bound (kernel space)
3618 * @addr: address holder
3620 * Fills the @addr pointer with the address which the socket is bound.
3621 * Returns the length of the address in bytes or an error code.
3624 int kernel_getsockname(struct socket
*sock
, struct sockaddr
*addr
)
3626 return READ_ONCE(sock
->ops
)->getname(sock
, addr
, 0);
3628 EXPORT_SYMBOL(kernel_getsockname
);
3631 * kernel_getpeername - get the address which the socket is connected (kernel space)
3633 * @addr: address holder
3635 * Fills the @addr pointer with the address which the socket is connected.
3636 * Returns the length of the address in bytes or an error code.
3639 int kernel_getpeername(struct socket
*sock
, struct sockaddr
*addr
)
3641 return READ_ONCE(sock
->ops
)->getname(sock
, addr
, 1);
3643 EXPORT_SYMBOL(kernel_getpeername
);
3646 * kernel_sock_shutdown - shut down part of a full-duplex connection (kernel space)
3648 * @how: connection part
3650 * Returns 0 or an error.
3653 int kernel_sock_shutdown(struct socket
*sock
, enum sock_shutdown_cmd how
)
3655 return READ_ONCE(sock
->ops
)->shutdown(sock
, how
);
3657 EXPORT_SYMBOL(kernel_sock_shutdown
);
3660 * kernel_sock_ip_overhead - returns the IP overhead imposed by a socket
3663 * This routine returns the IP overhead imposed by a socket i.e.
3664 * the length of the underlying IP header, depending on whether
3665 * this is an IPv4 or IPv6 socket and the length from IP options turned
3666 * on at the socket. Assumes that the caller has a lock on the socket.
3669 u32
kernel_sock_ip_overhead(struct sock
*sk
)
3671 struct inet_sock
*inet
;
3672 struct ip_options_rcu
*opt
;
3674 #if IS_ENABLED(CONFIG_IPV6)
3675 struct ipv6_pinfo
*np
;
3676 struct ipv6_txoptions
*optv6
= NULL
;
3677 #endif /* IS_ENABLED(CONFIG_IPV6) */
3682 switch (sk
->sk_family
) {
3685 overhead
+= sizeof(struct iphdr
);
3686 opt
= rcu_dereference_protected(inet
->inet_opt
,
3687 sock_owned_by_user(sk
));
3689 overhead
+= opt
->opt
.optlen
;
3691 #if IS_ENABLED(CONFIG_IPV6)
3694 overhead
+= sizeof(struct ipv6hdr
);
3696 optv6
= rcu_dereference_protected(np
->opt
,
3697 sock_owned_by_user(sk
));
3699 overhead
+= (optv6
->opt_flen
+ optv6
->opt_nflen
);
3701 #endif /* IS_ENABLED(CONFIG_IPV6) */
3702 default: /* Returns 0 overhead if the socket is not ipv4 or ipv6 */
3706 EXPORT_SYMBOL(kernel_sock_ip_overhead
);