2 * NET An implementation of the SOCKET network access protocol.
4 * Version: @(#)socket.c 1.1.93 18/02/95
6 * Authors: Orest Zborowski, <obz@Kodak.COM>
8 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
11 * Anonymous : NOTSOCK/BADF cleanup. Error fix in
13 * Alan Cox : verify_area() fixes
14 * Alan Cox : Removed DDI
15 * Jonathan Kamens : SOCK_DGRAM reconnect bug
16 * Alan Cox : Moved a load of checks to the very
18 * Alan Cox : Move address structures to/from user
19 * mode above the protocol layers.
20 * Rob Janssen : Allow 0 length sends.
21 * Alan Cox : Asynchronous I/O support (cribbed from the
23 * Niibe Yutaka : Asynchronous I/O for writes (4.4BSD style)
24 * Jeff Uphoff : Made max number of sockets command-line
26 * Matti Aarnio : Made the number of sockets dynamic,
27 * to be allocated when needed, and mr.
28 * Uphoff's max is used as max to be
29 * allowed to allocate.
30 * Linus : Argh. removed all the socket allocation
31 * altogether: it's in the inode now.
32 * Alan Cox : Made sock_alloc()/sock_release() public
33 * for NetROM and future kernel nfsd type
35 * Alan Cox : sendmsg/recvmsg basics.
36 * Tom Dyas : Export net symbols.
37 * Marcin Dalecki : Fixed problems with CONFIG_NET="n".
38 * Alan Cox : Added thread locking to sys_* calls
39 * for sockets. May have errors at the
41 * Kevin Buhr : Fixed the dumb errors in the above.
42 * Andi Kleen : Some small cleanups, optimizations,
43 * and fixed a copy_from_user() bug.
44 * Tigran Aivazian : sys_send(args) calls sys_sendto(args, NULL, 0)
45 * Tigran Aivazian : Made listen(2) backlog sanity checks
46 * protocol-independent
49 * This program is free software; you can redistribute it and/or
50 * modify it under the terms of the GNU General Public License
51 * as published by the Free Software Foundation; either version
52 * 2 of the License, or (at your option) any later version.
55 * This module is effectively the top level interface to the BSD socket
58 * Based upon Swansea University Computer Society NET3.039
62 #include <linux/socket.h>
63 #include <linux/file.h>
64 #include <linux/net.h>
65 #include <linux/interrupt.h>
66 #include <linux/thread_info.h>
67 #include <linux/rcupdate.h>
68 #include <linux/netdevice.h>
69 #include <linux/proc_fs.h>
70 #include <linux/seq_file.h>
71 #include <linux/mutex.h>
72 #include <linux/if_bridge.h>
73 #include <linux/if_frad.h>
74 #include <linux/if_vlan.h>
75 #include <linux/ptp_classify.h>
76 #include <linux/init.h>
77 #include <linux/poll.h>
78 #include <linux/cache.h>
79 #include <linux/module.h>
80 #include <linux/highmem.h>
81 #include <linux/mount.h>
82 #include <linux/security.h>
83 #include <linux/syscalls.h>
84 #include <linux/compat.h>
85 #include <linux/kmod.h>
86 #include <linux/audit.h>
87 #include <linux/wireless.h>
88 #include <linux/nsproxy.h>
89 #include <linux/magic.h>
90 #include <linux/slab.h>
91 #include <linux/xattr.h>
92 #include <linux/nospec.h>
93 #include <linux/indirect_call_wrapper.h>
95 #include <linux/uaccess.h>
96 #include <asm/unistd.h>
98 #include <net/compat.h>
100 #include <net/cls_cgroup.h>
102 #include <net/sock.h>
103 #include <linux/netfilter.h>
105 #include <linux/if_tun.h>
106 #include <linux/ipv6_route.h>
107 #include <linux/route.h>
108 #include <linux/sockios.h>
109 #include <net/busy_poll.h>
110 #include <linux/errqueue.h>
112 /* proto_ops for ipv4 and ipv6 use the same {recv,send}msg function */
113 #if IS_ENABLED(CONFIG_INET)
114 #define INDIRECT_CALL_INET4(f, f1, ...) INDIRECT_CALL_1(f, f1, __VA_ARGS__)
116 #define INDIRECT_CALL_INET4(f, f1, ...) f(__VA_ARGS__)
119 #ifdef CONFIG_NET_RX_BUSY_POLL
120 unsigned int sysctl_net_busy_read __read_mostly;
121 unsigned int sysctl_net_busy_poll __read_mostly;
124 static ssize_t sock_read_iter(struct kiocb *iocb, struct iov_iter *to);
125 static ssize_t sock_write_iter(struct kiocb *iocb, struct iov_iter *from);
126 static int sock_mmap(struct file *file, struct vm_area_struct *vma);
128 static int sock_close(struct inode *inode, struct file *file);
129 static __poll_t sock_poll(struct file *file,
130 struct poll_table_struct *wait);
131 static long sock_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
133 static long compat_sock_ioctl(struct file *file,
134 unsigned int cmd, unsigned long arg);
136 static int sock_fasync(int fd, struct file *filp, int on);
137 static ssize_t sock_sendpage(struct file *file, struct page *page,
138 int offset, size_t size, loff_t *ppos, int more);
139 static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
140 struct pipe_inode_info *pipe, size_t len,
144 * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
145 * in the operation structures but are done directly via the socketcall() multiplexor.
148 static const struct file_operations socket_file_ops = {
149 .owner = THIS_MODULE,
151 .read_iter = sock_read_iter,
152 .write_iter = sock_write_iter,
154 .unlocked_ioctl = sock_ioctl,
156 .compat_ioctl = compat_sock_ioctl,
159 .release = sock_close,
160 .fasync = sock_fasync,
161 .sendpage = sock_sendpage,
162 .splice_write = generic_splice_sendpage,
163 .splice_read = sock_splice_read,
167 * The protocol list. Each protocol is registered in here.
170 static DEFINE_SPINLOCK(net_family_lock);
171 static const struct net_proto_family __rcu *net_families[NPROTO] __read_mostly;
175 * Move socket addresses back and forth across the kernel/user
176 * divide and look after the messy bits.
180 * move_addr_to_kernel - copy a socket address into kernel space
181 * @uaddr: Address in user space
182 * @kaddr: Address in kernel space
183 * @ulen: Length in user space
185 * The address is copied into kernel space. If the provided address is
186 * too long an error code of -EINVAL is returned. If the copy gives
187 * invalid addresses -EFAULT is returned. On a success 0 is returned.
190 int move_addr_to_kernel(void __user *uaddr, int ulen, struct sockaddr_storage *kaddr)
192 if (ulen < 0 || ulen > sizeof(struct sockaddr_storage))
196 if (copy_from_user(kaddr, uaddr, ulen))
198 return audit_sockaddr(ulen, kaddr);
202 * move_addr_to_user - copy an address to user space
203 * @kaddr: kernel space address
204 * @klen: length of address in kernel
205 * @uaddr: user space address
206 * @ulen: pointer to user length field
208 * The value pointed to by ulen on entry is the buffer length available.
209 * This is overwritten with the buffer space used. -EINVAL is returned
210 * if an overlong buffer is specified or a negative buffer size. -EFAULT
211 * is returned if either the buffer or the length field are not
213 * After copying the data up to the limit the user specifies, the true
214 * length of the data is written over the length limit the user
215 * specified. Zero is returned for a success.
218 static int move_addr_to_user(struct sockaddr_storage *kaddr, int klen,
219 void __user *uaddr, int __user *ulen)
224 BUG_ON(klen > sizeof(struct sockaddr_storage));
225 err = get_user(len, ulen);
233 if (audit_sockaddr(klen, kaddr))
235 if (copy_to_user(uaddr, kaddr, len))
239 * "fromlen shall refer to the value before truncation.."
242 return __put_user(klen, ulen);
245 static struct kmem_cache *sock_inode_cachep __ro_after_init;
247 static struct inode *sock_alloc_inode(struct super_block *sb)
249 struct socket_alloc *ei;
250 struct socket_wq *wq;
252 ei = kmem_cache_alloc(sock_inode_cachep, GFP_KERNEL);
255 wq = kmalloc(sizeof(*wq), GFP_KERNEL);
257 kmem_cache_free(sock_inode_cachep, ei);
260 init_waitqueue_head(&wq->wait);
261 wq->fasync_list = NULL;
265 ei->socket.state = SS_UNCONNECTED;
266 ei->socket.flags = 0;
267 ei->socket.ops = NULL;
268 ei->socket.sk = NULL;
269 ei->socket.file = NULL;
271 return &ei->vfs_inode;
274 static void sock_destroy_inode(struct inode *inode)
276 struct socket_alloc *ei;
278 ei = container_of(inode, struct socket_alloc, vfs_inode);
279 kfree_rcu(ei->socket.wq, rcu);
280 kmem_cache_free(sock_inode_cachep, ei);
283 static void init_once(void *foo)
285 struct socket_alloc *ei = (struct socket_alloc *)foo;
287 inode_init_once(&ei->vfs_inode);
290 static void init_inodecache(void)
292 sock_inode_cachep = kmem_cache_create("sock_inode_cache",
293 sizeof(struct socket_alloc),
295 (SLAB_HWCACHE_ALIGN |
296 SLAB_RECLAIM_ACCOUNT |
297 SLAB_MEM_SPREAD | SLAB_ACCOUNT),
299 BUG_ON(sock_inode_cachep == NULL);
302 static const struct super_operations sockfs_ops = {
303 .alloc_inode = sock_alloc_inode,
304 .destroy_inode = sock_destroy_inode,
305 .statfs = simple_statfs,
309 * sockfs_dname() is called from d_path().
311 static char *sockfs_dname(struct dentry *dentry, char *buffer, int buflen)
313 return dynamic_dname(dentry, buffer, buflen, "socket:[%lu]",
314 d_inode(dentry)->i_ino);
317 static const struct dentry_operations sockfs_dentry_operations = {
318 .d_dname = sockfs_dname,
321 static int sockfs_xattr_get(const struct xattr_handler *handler,
322 struct dentry *dentry, struct inode *inode,
323 const char *suffix, void *value, size_t size)
326 if (dentry->d_name.len + 1 > size)
328 memcpy(value, dentry->d_name.name, dentry->d_name.len + 1);
330 return dentry->d_name.len + 1;
333 #define XATTR_SOCKPROTONAME_SUFFIX "sockprotoname"
334 #define XATTR_NAME_SOCKPROTONAME (XATTR_SYSTEM_PREFIX XATTR_SOCKPROTONAME_SUFFIX)
335 #define XATTR_NAME_SOCKPROTONAME_LEN (sizeof(XATTR_NAME_SOCKPROTONAME)-1)
337 static const struct xattr_handler sockfs_xattr_handler = {
338 .name = XATTR_NAME_SOCKPROTONAME,
339 .get = sockfs_xattr_get,
342 static int sockfs_security_xattr_set(const struct xattr_handler *handler,
343 struct dentry *dentry, struct inode *inode,
344 const char *suffix, const void *value,
345 size_t size, int flags)
347 /* Handled by LSM. */
351 static const struct xattr_handler sockfs_security_xattr_handler = {
352 .prefix = XATTR_SECURITY_PREFIX,
353 .set = sockfs_security_xattr_set,
356 static const struct xattr_handler *sockfs_xattr_handlers[] = {
357 &sockfs_xattr_handler,
358 &sockfs_security_xattr_handler,
362 static struct dentry *sockfs_mount(struct file_system_type *fs_type,
363 int flags, const char *dev_name, void *data)
365 return mount_pseudo_xattr(fs_type, "socket:", &sockfs_ops,
366 sockfs_xattr_handlers,
367 &sockfs_dentry_operations, SOCKFS_MAGIC);
370 static struct vfsmount *sock_mnt __read_mostly;
372 static struct file_system_type sock_fs_type = {
374 .mount = sockfs_mount,
375 .kill_sb = kill_anon_super,
379 * Obtains the first available file descriptor and sets it up for use.
381 * These functions create file structures and maps them to fd space
382 * of the current process. On success it returns file descriptor
383 * and file struct implicitly stored in sock->file.
384 * Note that another thread may close file descriptor before we return
385 * from this function. We use the fact that now we do not refer
386 * to socket after mapping. If one day we will need it, this
387 * function will increment ref. count on file by 1.
389 * In any case returned fd MAY BE not valid!
390 * This race condition is unavoidable
391 * with shared fd spaces, we cannot solve it inside kernel,
392 * but we take care of internal coherence yet.
396 * sock_alloc_file - Bind a &socket to a &file
398 * @flags: file status flags
399 * @dname: protocol name
401 * Returns the &file bound with @sock, implicitly storing it
402 * in sock->file. If dname is %NULL, sets to "".
403 * On failure the return is a ERR pointer (see linux/err.h).
404 * This function uses GFP_KERNEL internally.
407 struct file *sock_alloc_file(struct socket *sock, int flags, const char *dname)
412 dname = sock->sk ? sock->sk->sk_prot_creator->name : "";
414 file = alloc_file_pseudo(SOCK_INODE(sock), sock_mnt, dname,
415 O_RDWR | (flags & O_NONBLOCK),
423 file->private_data = sock;
426 EXPORT_SYMBOL(sock_alloc_file);
428 static int sock_map_fd(struct socket *sock, int flags)
430 struct file *newfile;
431 int fd = get_unused_fd_flags(flags);
432 if (unlikely(fd < 0)) {
437 newfile = sock_alloc_file(sock, flags, NULL);
438 if (likely(!IS_ERR(newfile))) {
439 fd_install(fd, newfile);
444 return PTR_ERR(newfile);
448 * sock_from_file - Return the &socket bounded to @file.
450 * @err: pointer to an error code return
452 * On failure returns %NULL and assigns -ENOTSOCK to @err.
455 struct socket *sock_from_file(struct file *file, int *err)
457 if (file->f_op == &socket_file_ops)
458 return file->private_data; /* set in sock_map_fd */
463 EXPORT_SYMBOL(sock_from_file);
466 * sockfd_lookup - Go from a file number to its socket slot
468 * @err: pointer to an error code return
470 * The file handle passed in is locked and the socket it is bound
471 * to is returned. If an error occurs the err pointer is overwritten
472 * with a negative errno code and NULL is returned. The function checks
473 * for both invalid handles and passing a handle which is not a socket.
475 * On a success the socket object pointer is returned.
478 struct socket *sockfd_lookup(int fd, int *err)
489 sock = sock_from_file(file, err);
494 EXPORT_SYMBOL(sockfd_lookup);
496 static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)
498 struct fd f = fdget(fd);
503 sock = sock_from_file(f.file, err);
505 *fput_needed = f.flags & FDPUT_FPUT;
513 static ssize_t sockfs_listxattr(struct dentry *dentry, char *buffer,
519 len = security_inode_listsecurity(d_inode(dentry), buffer, size);
529 len = (XATTR_NAME_SOCKPROTONAME_LEN + 1);
534 memcpy(buffer, XATTR_NAME_SOCKPROTONAME, len);
541 static int sockfs_setattr(struct dentry *dentry, struct iattr *iattr)
543 int err = simple_setattr(dentry, iattr);
545 if (!err && (iattr->ia_valid & ATTR_UID)) {
546 struct socket *sock = SOCKET_I(d_inode(dentry));
549 sock->sk->sk_uid = iattr->ia_uid;
557 static const struct inode_operations sockfs_inode_ops = {
558 .listxattr = sockfs_listxattr,
559 .setattr = sockfs_setattr,
563 * sock_alloc - allocate a socket
565 * Allocate a new inode and socket object. The two are bound together
566 * and initialised. The socket is then returned. If we are out of inodes
567 * NULL is returned. This functions uses GFP_KERNEL internally.
570 struct socket *sock_alloc(void)
575 inode = new_inode_pseudo(sock_mnt->mnt_sb);
579 sock = SOCKET_I(inode);
581 inode->i_ino = get_next_ino();
582 inode->i_mode = S_IFSOCK | S_IRWXUGO;
583 inode->i_uid = current_fsuid();
584 inode->i_gid = current_fsgid();
585 inode->i_op = &sockfs_inode_ops;
589 EXPORT_SYMBOL(sock_alloc);
592 * sock_release - close a socket
593 * @sock: socket to close
595 * The socket is released from the protocol stack if it has a release
596 * callback, and the inode is then released if the socket is bound to
597 * an inode not a file.
600 static void __sock_release(struct socket *sock, struct inode *inode)
603 struct module *owner = sock->ops->owner;
607 sock->ops->release(sock);
615 if (sock->wq->fasync_list)
616 pr_err("%s: fasync list not empty!\n", __func__);
619 iput(SOCK_INODE(sock));
625 void sock_release(struct socket *sock)
627 __sock_release(sock, NULL);
629 EXPORT_SYMBOL(sock_release);
631 void __sock_tx_timestamp(__u16 tsflags, __u8 *tx_flags)
633 u8 flags = *tx_flags;
635 if (tsflags & SOF_TIMESTAMPING_TX_HARDWARE)
636 flags |= SKBTX_HW_TSTAMP;
638 if (tsflags & SOF_TIMESTAMPING_TX_SOFTWARE)
639 flags |= SKBTX_SW_TSTAMP;
641 if (tsflags & SOF_TIMESTAMPING_TX_SCHED)
642 flags |= SKBTX_SCHED_TSTAMP;
646 EXPORT_SYMBOL(__sock_tx_timestamp);
648 INDIRECT_CALLABLE_DECLARE(int inet_sendmsg(struct socket *, struct msghdr *,
650 static inline int sock_sendmsg_nosec(struct socket *sock, struct msghdr *msg)
652 int ret = INDIRECT_CALL_INET4(sock->ops->sendmsg, inet_sendmsg, sock,
653 msg, msg_data_left(msg));
654 BUG_ON(ret == -EIOCBQUEUED);
658 static int __sock_sendmsg(struct socket *sock, struct msghdr *msg)
660 int err = security_socket_sendmsg(sock, msg,
663 return err ?: sock_sendmsg_nosec(sock, msg);
667 * sock_sendmsg - send a message through @sock
669 * @msg: message to send
671 * Sends @msg through @sock, passing through LSM.
672 * Returns the number of bytes sent, or an error code.
674 int sock_sendmsg(struct socket *sock, struct msghdr *msg)
676 struct sockaddr_storage *save_addr = (struct sockaddr_storage *)msg->msg_name;
677 struct sockaddr_storage address;
678 int save_len = msg->msg_namelen;
682 memcpy(&address, msg->msg_name, msg->msg_namelen);
683 msg->msg_name = &address;
686 ret = __sock_sendmsg(sock, msg);
687 msg->msg_name = save_addr;
688 msg->msg_namelen = save_len;
692 EXPORT_SYMBOL(sock_sendmsg);
695 * kernel_sendmsg - send a message through @sock (kernel-space)
697 * @msg: message header
699 * @num: vec array length
700 * @size: total message data size
702 * Builds the message data with @vec and sends it through @sock.
703 * Returns the number of bytes sent, or an error code.
706 int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
707 struct kvec *vec, size_t num, size_t size)
709 iov_iter_kvec(&msg->msg_iter, WRITE | ITER_KVEC, vec, num, size);
710 return sock_sendmsg(sock, msg);
712 EXPORT_SYMBOL(kernel_sendmsg);
715 * kernel_sendmsg_locked - send a message through @sock (kernel-space)
717 * @msg: message header
718 * @vec: output s/g array
719 * @num: output s/g array length
720 * @size: total message data size
722 * Builds the message data with @vec and sends it through @sock.
723 * Returns the number of bytes sent, or an error code.
724 * Caller must hold @sk.
727 int kernel_sendmsg_locked(struct sock *sk, struct msghdr *msg,
728 struct kvec *vec, size_t num, size_t size)
730 struct socket *sock = sk->sk_socket;
732 if (!sock->ops->sendmsg_locked)
733 return sock_no_sendmsg_locked(sk, msg, size);
735 iov_iter_kvec(&msg->msg_iter, WRITE | ITER_KVEC, vec, num, size);
737 return sock->ops->sendmsg_locked(sk, msg, msg_data_left(msg));
739 EXPORT_SYMBOL(kernel_sendmsg_locked);
741 static bool skb_is_err_queue(const struct sk_buff *skb)
743 /* pkt_type of skbs enqueued on the error queue are set to
744 * PACKET_OUTGOING in skb_set_err_queue(). This is only safe to do
745 * in recvmsg, since skbs received on a local socket will never
746 * have a pkt_type of PACKET_OUTGOING.
748 return skb->pkt_type == PACKET_OUTGOING;
751 /* On transmit, software and hardware timestamps are returned independently.
752 * As the two skb clones share the hardware timestamp, which may be updated
753 * before the software timestamp is received, a hardware TX timestamp may be
754 * returned only if there is no software TX timestamp. Ignore false software
755 * timestamps, which may be made in the __sock_recv_timestamp() call when the
756 * option SO_TIMESTAMP(NS) is enabled on the socket, even when the skb has a
757 * hardware timestamp.
759 static bool skb_is_swtx_tstamp(const struct sk_buff *skb, int false_tstamp)
761 return skb->tstamp && !false_tstamp && skb_is_err_queue(skb);
764 static void put_ts_pktinfo(struct msghdr *msg, struct sk_buff *skb)
766 struct scm_ts_pktinfo ts_pktinfo;
767 struct net_device *orig_dev;
769 if (!skb_mac_header_was_set(skb))
772 memset(&ts_pktinfo, 0, sizeof(ts_pktinfo));
775 orig_dev = dev_get_by_napi_id(skb_napi_id(skb));
777 ts_pktinfo.if_index = orig_dev->ifindex;
780 ts_pktinfo.pkt_length = skb->len - skb_mac_offset(skb);
781 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPING_PKTINFO,
782 sizeof(ts_pktinfo), &ts_pktinfo);
786 * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
788 void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
791 int need_software_tstamp = sock_flag(sk, SOCK_RCVTSTAMP);
792 struct scm_timestamping tss;
793 int empty = 1, false_tstamp = 0;
794 struct skb_shared_hwtstamps *shhwtstamps =
797 /* Race occurred between timestamp enabling and packet
798 receiving. Fill in the current time for now. */
799 if (need_software_tstamp && skb->tstamp == 0) {
800 __net_timestamp(skb);
804 if (need_software_tstamp) {
805 if (!sock_flag(sk, SOCK_RCVTSTAMPNS)) {
807 skb_get_timestamp(skb, &tv);
808 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMP,
812 skb_get_timestampns(skb, &ts);
813 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPNS,
818 memset(&tss, 0, sizeof(tss));
819 if ((sk->sk_tsflags & SOF_TIMESTAMPING_SOFTWARE) &&
820 ktime_to_timespec_cond(skb->tstamp, tss.ts + 0))
823 (sk->sk_tsflags & SOF_TIMESTAMPING_RAW_HARDWARE) &&
824 !skb_is_swtx_tstamp(skb, false_tstamp) &&
825 ktime_to_timespec_cond(shhwtstamps->hwtstamp, tss.ts + 2)) {
827 if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_PKTINFO) &&
828 !skb_is_err_queue(skb))
829 put_ts_pktinfo(msg, skb);
832 put_cmsg(msg, SOL_SOCKET,
833 SCM_TIMESTAMPING, sizeof(tss), &tss);
835 if (skb_is_err_queue(skb) && skb->len &&
836 SKB_EXT_ERR(skb)->opt_stats)
837 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPING_OPT_STATS,
838 skb->len, skb->data);
841 EXPORT_SYMBOL_GPL(__sock_recv_timestamp);
843 void __sock_recv_wifi_status(struct msghdr *msg, struct sock *sk,
848 if (!sock_flag(sk, SOCK_WIFI_STATUS))
850 if (!skb->wifi_acked_valid)
853 ack = skb->wifi_acked;
855 put_cmsg(msg, SOL_SOCKET, SCM_WIFI_STATUS, sizeof(ack), &ack);
857 EXPORT_SYMBOL_GPL(__sock_recv_wifi_status);
859 static inline void sock_recv_drops(struct msghdr *msg, struct sock *sk,
862 if (sock_flag(sk, SOCK_RXQ_OVFL) && skb && SOCK_SKB_CB(skb)->dropcount)
863 put_cmsg(msg, SOL_SOCKET, SO_RXQ_OVFL,
864 sizeof(__u32), &SOCK_SKB_CB(skb)->dropcount);
867 void __sock_recv_ts_and_drops(struct msghdr *msg, struct sock *sk,
870 sock_recv_timestamp(msg, sk, skb);
871 sock_recv_drops(msg, sk, skb);
873 EXPORT_SYMBOL_GPL(__sock_recv_ts_and_drops);
875 INDIRECT_CALLABLE_DECLARE(int inet_recvmsg(struct socket *, struct msghdr *,
877 static inline int sock_recvmsg_nosec(struct socket *sock, struct msghdr *msg,
880 return INDIRECT_CALL_INET4(sock->ops->recvmsg, inet_recvmsg, sock, msg,
881 msg_data_left(msg), flags);
885 * sock_recvmsg - receive a message from @sock
887 * @msg: message to receive
888 * @flags: message flags
890 * Receives @msg from @sock, passing through LSM. Returns the total number
891 * of bytes received, or an error.
893 int sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags)
895 int err = security_socket_recvmsg(sock, msg, msg_data_left(msg), flags);
897 return err ?: sock_recvmsg_nosec(sock, msg, flags);
899 EXPORT_SYMBOL(sock_recvmsg);
902 * kernel_recvmsg - Receive a message from a socket (kernel space)
903 * @sock: The socket to receive the message from
904 * @msg: Received message
905 * @vec: Input s/g array for message data
906 * @num: Size of input s/g array
907 * @size: Number of bytes to read
908 * @flags: Message flags (MSG_DONTWAIT, etc...)
910 * On return the msg structure contains the scatter/gather array passed in the
911 * vec argument. The array is modified so that it consists of the unfilled
912 * portion of the original array.
914 * The returned value is the total number of bytes received, or an error.
917 int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
918 struct kvec *vec, size_t num, size_t size, int flags)
920 mm_segment_t oldfs = get_fs();
923 iov_iter_kvec(&msg->msg_iter, READ | ITER_KVEC, vec, num, size);
925 result = sock_recvmsg(sock, msg, flags);
929 EXPORT_SYMBOL(kernel_recvmsg);
931 static ssize_t sock_sendpage(struct file *file, struct page *page,
932 int offset, size_t size, loff_t *ppos, int more)
937 sock = file->private_data;
939 flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
940 /* more is a combination of MSG_MORE and MSG_SENDPAGE_NOTLAST */
943 return kernel_sendpage(sock, page, offset, size, flags);
946 static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
947 struct pipe_inode_info *pipe, size_t len,
950 struct socket *sock = file->private_data;
952 if (unlikely(!sock->ops->splice_read))
955 return sock->ops->splice_read(sock, ppos, pipe, len, flags);
958 static ssize_t sock_read_iter(struct kiocb *iocb, struct iov_iter *to)
960 struct file *file = iocb->ki_filp;
961 struct socket *sock = file->private_data;
962 struct msghdr msg = {.msg_iter = *to,
966 if (file->f_flags & O_NONBLOCK || (iocb->ki_flags & IOCB_NOWAIT))
967 msg.msg_flags = MSG_DONTWAIT;
969 if (iocb->ki_pos != 0)
972 if (!iov_iter_count(to)) /* Match SYS5 behaviour */
975 res = sock_recvmsg(sock, &msg, msg.msg_flags);
980 static ssize_t sock_write_iter(struct kiocb *iocb, struct iov_iter *from)
982 struct file *file = iocb->ki_filp;
983 struct socket *sock = file->private_data;
984 struct msghdr msg = {.msg_iter = *from,
988 if (iocb->ki_pos != 0)
991 if (file->f_flags & O_NONBLOCK || (iocb->ki_flags & IOCB_NOWAIT))
992 msg.msg_flags = MSG_DONTWAIT;
994 if (sock->type == SOCK_SEQPACKET)
995 msg.msg_flags |= MSG_EOR;
997 res = __sock_sendmsg(sock, &msg);
998 *from = msg.msg_iter;
1003 * Atomic setting of ioctl hooks to avoid race
1004 * with module unload.
1007 static DEFINE_MUTEX(br_ioctl_mutex);
1008 static int (*br_ioctl_hook) (struct net *, unsigned int cmd, void __user *arg);
1010 void brioctl_set(int (*hook) (struct net *, unsigned int, void __user *))
1012 mutex_lock(&br_ioctl_mutex);
1013 br_ioctl_hook = hook;
1014 mutex_unlock(&br_ioctl_mutex);
1016 EXPORT_SYMBOL(brioctl_set);
1018 static DEFINE_MUTEX(vlan_ioctl_mutex);
1019 static int (*vlan_ioctl_hook) (struct net *, void __user *arg);
1021 void vlan_ioctl_set(int (*hook) (struct net *, void __user *))
1023 mutex_lock(&vlan_ioctl_mutex);
1024 vlan_ioctl_hook = hook;
1025 mutex_unlock(&vlan_ioctl_mutex);
1027 EXPORT_SYMBOL(vlan_ioctl_set);
1029 static DEFINE_MUTEX(dlci_ioctl_mutex);
1030 static int (*dlci_ioctl_hook) (unsigned int, void __user *);
1032 void dlci_ioctl_set(int (*hook) (unsigned int, void __user *))
1034 mutex_lock(&dlci_ioctl_mutex);
1035 dlci_ioctl_hook = hook;
1036 mutex_unlock(&dlci_ioctl_mutex);
1038 EXPORT_SYMBOL(dlci_ioctl_set);
1040 static long sock_do_ioctl(struct net *net, struct socket *sock,
1041 unsigned int cmd, unsigned long arg)
1044 void __user *argp = (void __user *)arg;
1046 err = sock->ops->ioctl(sock, cmd, arg);
1049 * If this ioctl is unknown try to hand it down
1050 * to the NIC driver.
1052 if (err != -ENOIOCTLCMD)
1055 if (cmd == SIOCGIFCONF) {
1057 if (copy_from_user(&ifc, argp, sizeof(struct ifconf)))
1060 err = dev_ifconf(net, &ifc, sizeof(struct ifreq));
1062 if (!err && copy_to_user(argp, &ifc, sizeof(struct ifconf)))
1064 } else if (is_socket_ioctl_cmd(cmd)) {
1067 if (copy_from_user(&ifr, argp, sizeof(struct ifreq)))
1069 err = dev_ioctl(net, cmd, &ifr, &need_copyout);
1070 if (!err && need_copyout)
1071 if (copy_to_user(argp, &ifr, sizeof(struct ifreq)))
1080 * With an ioctl, arg may well be a user mode pointer, but we don't know
1081 * what to do with it - that's up to the protocol still.
1084 static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
1086 struct socket *sock;
1088 void __user *argp = (void __user *)arg;
1092 sock = file->private_data;
1095 if (unlikely(cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))) {
1098 if (copy_from_user(&ifr, argp, sizeof(struct ifreq)))
1100 err = dev_ioctl(net, cmd, &ifr, &need_copyout);
1101 if (!err && need_copyout)
1102 if (copy_to_user(argp, &ifr, sizeof(struct ifreq)))
1105 #ifdef CONFIG_WEXT_CORE
1106 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
1107 err = wext_handle_ioctl(net, cmd, argp);
1114 if (get_user(pid, (int __user *)argp))
1116 err = f_setown(sock->file, pid, 1);
1120 err = put_user(f_getown(sock->file),
1121 (int __user *)argp);
1129 request_module("bridge");
1131 mutex_lock(&br_ioctl_mutex);
1133 err = br_ioctl_hook(net, cmd, argp);
1134 mutex_unlock(&br_ioctl_mutex);
1139 if (!vlan_ioctl_hook)
1140 request_module("8021q");
1142 mutex_lock(&vlan_ioctl_mutex);
1143 if (vlan_ioctl_hook)
1144 err = vlan_ioctl_hook(net, argp);
1145 mutex_unlock(&vlan_ioctl_mutex);
1150 if (!dlci_ioctl_hook)
1151 request_module("dlci");
1153 mutex_lock(&dlci_ioctl_mutex);
1154 if (dlci_ioctl_hook)
1155 err = dlci_ioctl_hook(cmd, argp);
1156 mutex_unlock(&dlci_ioctl_mutex);
1160 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1163 err = open_related_ns(&net->ns, get_net_ns);
1166 err = sock_do_ioctl(net, sock, cmd, arg);
1173 * sock_create_lite - creates a socket
1174 * @family: protocol family (AF_INET, ...)
1175 * @type: communication type (SOCK_STREAM, ...)
1176 * @protocol: protocol (0, ...)
1179 * Creates a new socket and assigns it to @res, passing through LSM.
1180 * The new socket initialization is not complete, see kernel_accept().
1181 * Returns 0 or an error. On failure @res is set to %NULL.
1182 * This function internally uses GFP_KERNEL.
1185 int sock_create_lite(int family, int type, int protocol, struct socket **res)
1188 struct socket *sock = NULL;
1190 err = security_socket_create(family, type, protocol, 1);
1194 sock = sock_alloc();
1201 err = security_socket_post_create(sock, family, type, protocol, 1);
1213 EXPORT_SYMBOL(sock_create_lite);
1215 /* No kernel lock held - perfect */
1216 static __poll_t sock_poll(struct file *file, poll_table *wait)
1218 struct socket *sock = file->private_data;
1219 __poll_t events = poll_requested_events(wait), flag = 0;
1221 if (!sock->ops->poll)
1224 if (sk_can_busy_loop(sock->sk)) {
1225 /* poll once if requested by the syscall */
1226 if (events & POLL_BUSY_LOOP)
1227 sk_busy_loop(sock->sk, 1);
1229 /* if this socket can poll_ll, tell the system call */
1230 flag = POLL_BUSY_LOOP;
1233 return sock->ops->poll(file, sock, wait) | flag;
1236 static int sock_mmap(struct file *file, struct vm_area_struct *vma)
1238 struct socket *sock = file->private_data;
1240 return sock->ops->mmap(file, sock, vma);
1243 static int sock_close(struct inode *inode, struct file *filp)
1245 __sock_release(SOCKET_I(inode), inode);
1250 * Update the socket async list
1252 * Fasync_list locking strategy.
1254 * 1. fasync_list is modified only under process context socket lock
1255 * i.e. under semaphore.
1256 * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
1257 * or under socket lock
1260 static int sock_fasync(int fd, struct file *filp, int on)
1262 struct socket *sock = filp->private_data;
1263 struct sock *sk = sock->sk;
1264 struct socket_wq *wq;
1271 fasync_helper(fd, filp, on, &wq->fasync_list);
1273 if (!wq->fasync_list)
1274 sock_reset_flag(sk, SOCK_FASYNC);
1276 sock_set_flag(sk, SOCK_FASYNC);
1282 /* This function may be called only under rcu_lock */
1284 int sock_wake_async(struct socket_wq *wq, int how, int band)
1286 if (!wq || !wq->fasync_list)
1290 case SOCK_WAKE_WAITD:
1291 if (test_bit(SOCKWQ_ASYNC_WAITDATA, &wq->flags))
1294 case SOCK_WAKE_SPACE:
1295 if (!test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags))
1300 kill_fasync(&wq->fasync_list, SIGIO, band);
1303 kill_fasync(&wq->fasync_list, SIGURG, band);
1308 EXPORT_SYMBOL(sock_wake_async);
1311 * __sock_create - creates a socket
1312 * @net: net namespace
1313 * @family: protocol family (AF_INET, ...)
1314 * @type: communication type (SOCK_STREAM, ...)
1315 * @protocol: protocol (0, ...)
1317 * @kern: boolean for kernel space sockets
1319 * Creates a new socket and assigns it to @res, passing through LSM.
1320 * Returns 0 or an error. On failure @res is set to %NULL. @kern must
1321 * be set to true if the socket resides in kernel space.
1322 * This function internally uses GFP_KERNEL.
1325 int __sock_create(struct net *net, int family, int type, int protocol,
1326 struct socket **res, int kern)
1329 struct socket *sock;
1330 const struct net_proto_family *pf;
1333 * Check protocol is in range
1335 if (family < 0 || family >= NPROTO)
1336 return -EAFNOSUPPORT;
1337 if (type < 0 || type >= SOCK_MAX)
1342 This uglymoron is moved from INET layer to here to avoid
1343 deadlock in module load.
1345 if (family == PF_INET && type == SOCK_PACKET) {
1346 pr_info_once("%s uses obsolete (PF_INET,SOCK_PACKET)\n",
1351 err = security_socket_create(family, type, protocol, kern);
1356 * Allocate the socket and allow the family to set things up. if
1357 * the protocol is 0, the family is instructed to select an appropriate
1360 sock = sock_alloc();
1362 net_warn_ratelimited("socket: no more sockets\n");
1363 return -ENFILE; /* Not exactly a match, but its the
1364 closest posix thing */
1369 #ifdef CONFIG_MODULES
1370 /* Attempt to load a protocol module if the find failed.
1372 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
1373 * requested real, full-featured networking support upon configuration.
1374 * Otherwise module support will break!
1376 if (rcu_access_pointer(net_families[family]) == NULL)
1377 request_module("net-pf-%d", family);
1381 pf = rcu_dereference(net_families[family]);
1382 err = -EAFNOSUPPORT;
1387 * We will call the ->create function, that possibly is in a loadable
1388 * module, so we have to bump that loadable module refcnt first.
1390 if (!try_module_get(pf->owner))
1393 /* Now protected by module ref count */
1396 err = pf->create(net, sock, protocol, kern);
1398 goto out_module_put;
1401 * Now to bump the refcnt of the [loadable] module that owns this
1402 * socket at sock_release time we decrement its refcnt.
1404 if (!try_module_get(sock->ops->owner))
1405 goto out_module_busy;
1408 * Now that we're done with the ->create function, the [loadable]
1409 * module can have its refcnt decremented
1411 module_put(pf->owner);
1412 err = security_socket_post_create(sock, family, type, protocol, kern);
1414 goto out_sock_release;
1420 err = -EAFNOSUPPORT;
1423 module_put(pf->owner);
1430 goto out_sock_release;
1432 EXPORT_SYMBOL(__sock_create);
1435 * sock_create - creates a socket
1436 * @family: protocol family (AF_INET, ...)
1437 * @type: communication type (SOCK_STREAM, ...)
1438 * @protocol: protocol (0, ...)
1441 * A wrapper around __sock_create().
1442 * Returns 0 or an error. This function internally uses GFP_KERNEL.
1445 int sock_create(int family, int type, int protocol, struct socket **res)
1447 return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0);
1449 EXPORT_SYMBOL(sock_create);
1452 * sock_create_kern - creates a socket (kernel space)
1453 * @net: net namespace
1454 * @family: protocol family (AF_INET, ...)
1455 * @type: communication type (SOCK_STREAM, ...)
1456 * @protocol: protocol (0, ...)
1459 * A wrapper around __sock_create().
1460 * Returns 0 or an error. This function internally uses GFP_KERNEL.
1463 int sock_create_kern(struct net *net, int family, int type, int protocol, struct socket **res)
1465 return __sock_create(net, family, type, protocol, res, 1);
1467 EXPORT_SYMBOL(sock_create_kern);
1469 int __sys_socket(int family, int type, int protocol)
1472 struct socket *sock;
1475 /* Check the SOCK_* constants for consistency. */
1476 BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC);
1477 BUILD_BUG_ON((SOCK_MAX | SOCK_TYPE_MASK) != SOCK_TYPE_MASK);
1478 BUILD_BUG_ON(SOCK_CLOEXEC & SOCK_TYPE_MASK);
1479 BUILD_BUG_ON(SOCK_NONBLOCK & SOCK_TYPE_MASK);
1481 flags = type & ~SOCK_TYPE_MASK;
1482 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1484 type &= SOCK_TYPE_MASK;
1486 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1487 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1489 retval = sock_create(family, type, protocol, &sock);
1493 return sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK));
1496 SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol)
1498 return __sys_socket(family, type, protocol);
1502 * Create a pair of connected sockets.
1505 int __sys_socketpair(int family, int type, int protocol, int __user *usockvec)
1507 struct socket *sock1, *sock2;
1509 struct file *newfile1, *newfile2;
1512 flags = type & ~SOCK_TYPE_MASK;
1513 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1515 type &= SOCK_TYPE_MASK;
1517 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1518 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1521 * reserve descriptors and make sure we won't fail
1522 * to return them to userland.
1524 fd1 = get_unused_fd_flags(flags);
1525 if (unlikely(fd1 < 0))
1528 fd2 = get_unused_fd_flags(flags);
1529 if (unlikely(fd2 < 0)) {
1534 err = put_user(fd1, &usockvec[0]);
1538 err = put_user(fd2, &usockvec[1]);
1543 * Obtain the first socket and check if the underlying protocol
1544 * supports the socketpair call.
1547 err = sock_create(family, type, protocol, &sock1);
1548 if (unlikely(err < 0))
1551 err = sock_create(family, type, protocol, &sock2);
1552 if (unlikely(err < 0)) {
1553 sock_release(sock1);
1557 err = security_socket_socketpair(sock1, sock2);
1558 if (unlikely(err)) {
1559 sock_release(sock2);
1560 sock_release(sock1);
1564 err = sock1->ops->socketpair(sock1, sock2);
1565 if (unlikely(err < 0)) {
1566 sock_release(sock2);
1567 sock_release(sock1);
1571 newfile1 = sock_alloc_file(sock1, flags, NULL);
1572 if (IS_ERR(newfile1)) {
1573 err = PTR_ERR(newfile1);
1574 sock_release(sock2);
1578 newfile2 = sock_alloc_file(sock2, flags, NULL);
1579 if (IS_ERR(newfile2)) {
1580 err = PTR_ERR(newfile2);
1585 audit_fd_pair(fd1, fd2);
1587 fd_install(fd1, newfile1);
1588 fd_install(fd2, newfile2);
1597 SYSCALL_DEFINE4(socketpair, int, family, int, type, int, protocol,
1598 int __user *, usockvec)
1600 return __sys_socketpair(family, type, protocol, usockvec);
1604 * Bind a name to a socket. Nothing much to do here since it's
1605 * the protocol's responsibility to handle the local address.
1607 * We move the socket address to kernel space before we call
1608 * the protocol layer (having also checked the address is ok).
1611 int __sys_bind(int fd, struct sockaddr __user *umyaddr, int addrlen)
1613 struct socket *sock;
1614 struct sockaddr_storage address;
1615 int err, fput_needed;
1617 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1619 err = move_addr_to_kernel(umyaddr, addrlen, &address);
1621 err = security_socket_bind(sock,
1622 (struct sockaddr *)&address,
1625 err = sock->ops->bind(sock,
1629 fput_light(sock->file, fput_needed);
1634 SYSCALL_DEFINE3(bind, int, fd, struct sockaddr __user *, umyaddr, int, addrlen)
1636 return __sys_bind(fd, umyaddr, addrlen);
1640 * Perform a listen. Basically, we allow the protocol to do anything
1641 * necessary for a listen, and if that works, we mark the socket as
1642 * ready for listening.
1645 int __sys_listen(int fd, int backlog)
1647 struct socket *sock;
1648 int err, fput_needed;
1651 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1653 somaxconn = READ_ONCE(sock_net(sock->sk)->core.sysctl_somaxconn);
1654 if ((unsigned int)backlog > somaxconn)
1655 backlog = somaxconn;
1657 err = security_socket_listen(sock, backlog);
1659 err = sock->ops->listen(sock, backlog);
1661 fput_light(sock->file, fput_needed);
1666 SYSCALL_DEFINE2(listen, int, fd, int, backlog)
1668 return __sys_listen(fd, backlog);
1672 * For accept, we attempt to create a new socket, set up the link
1673 * with the client, wake up the client, then return the new
1674 * connected fd. We collect the address of the connector in kernel
1675 * space and move it to user at the very end. This is unclean because
1676 * we open the socket then return an error.
1678 * 1003.1g adds the ability to recvmsg() to query connection pending
1679 * status to recvmsg. We need to add that support in a way thats
1680 * clean when we restructure accept also.
1683 int __sys_accept4(int fd, struct sockaddr __user *upeer_sockaddr,
1684 int __user *upeer_addrlen, int flags)
1686 struct socket *sock, *newsock;
1687 struct file *newfile;
1688 int err, len, newfd, fput_needed;
1689 struct sockaddr_storage address;
1691 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1694 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1695 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1697 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1702 newsock = sock_alloc();
1706 newsock->type = sock->type;
1707 newsock->ops = sock->ops;
1710 * We don't need try_module_get here, as the listening socket (sock)
1711 * has the protocol module (sock->ops->owner) held.
1713 __module_get(newsock->ops->owner);
1715 newfd = get_unused_fd_flags(flags);
1716 if (unlikely(newfd < 0)) {
1718 sock_release(newsock);
1721 newfile = sock_alloc_file(newsock, flags, sock->sk->sk_prot_creator->name);
1722 if (IS_ERR(newfile)) {
1723 err = PTR_ERR(newfile);
1724 put_unused_fd(newfd);
1728 err = security_socket_accept(sock, newsock);
1732 err = sock->ops->accept(sock, newsock, sock->file->f_flags, false);
1736 if (upeer_sockaddr) {
1737 len = newsock->ops->getname(newsock,
1738 (struct sockaddr *)&address, 2);
1740 err = -ECONNABORTED;
1743 err = move_addr_to_user(&address,
1744 len, upeer_sockaddr, upeer_addrlen);
1749 /* File flags are not inherited via accept() unlike another OSes. */
1751 fd_install(newfd, newfile);
1755 fput_light(sock->file, fput_needed);
1760 put_unused_fd(newfd);
1764 SYSCALL_DEFINE4(accept4, int, fd, struct sockaddr __user *, upeer_sockaddr,
1765 int __user *, upeer_addrlen, int, flags)
1767 return __sys_accept4(fd, upeer_sockaddr, upeer_addrlen, flags);
1770 SYSCALL_DEFINE3(accept, int, fd, struct sockaddr __user *, upeer_sockaddr,
1771 int __user *, upeer_addrlen)
1773 return __sys_accept4(fd, upeer_sockaddr, upeer_addrlen, 0);
1777 * Attempt to connect to a socket with the server address. The address
1778 * is in user space so we verify it is OK and move it to kernel space.
1780 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
1783 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
1784 * other SEQPACKET protocols that take time to connect() as it doesn't
1785 * include the -EINPROGRESS status for such sockets.
1788 int __sys_connect(int fd, struct sockaddr __user *uservaddr, int addrlen)
1790 struct socket *sock;
1791 struct sockaddr_storage address;
1792 int err, fput_needed;
1794 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1797 err = move_addr_to_kernel(uservaddr, addrlen, &address);
1802 security_socket_connect(sock, (struct sockaddr *)&address, addrlen);
1806 err = sock->ops->connect(sock, (struct sockaddr *)&address, addrlen,
1807 sock->file->f_flags);
1809 fput_light(sock->file, fput_needed);
1814 SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
1817 return __sys_connect(fd, uservaddr, addrlen);
1821 * Get the local address ('name') of a socket object. Move the obtained
1822 * name to user space.
1825 int __sys_getsockname(int fd, struct sockaddr __user *usockaddr,
1826 int __user *usockaddr_len)
1828 struct socket *sock;
1829 struct sockaddr_storage address;
1830 int err, fput_needed;
1832 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1836 err = security_socket_getsockname(sock);
1840 err = sock->ops->getname(sock, (struct sockaddr *)&address, 0);
1843 /* "err" is actually length in this case */
1844 err = move_addr_to_user(&address, err, usockaddr, usockaddr_len);
1847 fput_light(sock->file, fput_needed);
1852 SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr,
1853 int __user *, usockaddr_len)
1855 return __sys_getsockname(fd, usockaddr, usockaddr_len);
1859 * Get the remote address ('name') of a socket object. Move the obtained
1860 * name to user space.
1863 int __sys_getpeername(int fd, struct sockaddr __user *usockaddr,
1864 int __user *usockaddr_len)
1866 struct socket *sock;
1867 struct sockaddr_storage address;
1868 int err, fput_needed;
1870 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1872 err = security_socket_getpeername(sock);
1874 fput_light(sock->file, fput_needed);
1878 err = sock->ops->getname(sock, (struct sockaddr *)&address, 1);
1880 /* "err" is actually length in this case */
1881 err = move_addr_to_user(&address, err, usockaddr,
1883 fput_light(sock->file, fput_needed);
1888 SYSCALL_DEFINE3(getpeername, int, fd, struct sockaddr __user *, usockaddr,
1889 int __user *, usockaddr_len)
1891 return __sys_getpeername(fd, usockaddr, usockaddr_len);
1895 * Send a datagram to a given address. We move the address into kernel
1896 * space and check the user space data area is readable before invoking
1899 int __sys_sendto(int fd, void __user *buff, size_t len, unsigned int flags,
1900 struct sockaddr __user *addr, int addr_len)
1902 struct socket *sock;
1903 struct sockaddr_storage address;
1909 err = import_single_range(WRITE, buff, len, &iov, &msg.msg_iter);
1912 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1916 msg.msg_name = NULL;
1917 msg.msg_control = NULL;
1918 msg.msg_controllen = 0;
1919 msg.msg_namelen = 0;
1921 err = move_addr_to_kernel(addr, addr_len, &address);
1924 msg.msg_name = (struct sockaddr *)&address;
1925 msg.msg_namelen = addr_len;
1927 if (sock->file->f_flags & O_NONBLOCK)
1928 flags |= MSG_DONTWAIT;
1929 msg.msg_flags = flags;
1930 err = __sock_sendmsg(sock, &msg);
1933 fput_light(sock->file, fput_needed);
1938 SYSCALL_DEFINE6(sendto, int, fd, void __user *, buff, size_t, len,
1939 unsigned int, flags, struct sockaddr __user *, addr,
1942 return __sys_sendto(fd, buff, len, flags, addr, addr_len);
1946 * Send a datagram down a socket.
1949 SYSCALL_DEFINE4(send, int, fd, void __user *, buff, size_t, len,
1950 unsigned int, flags)
1952 return __sys_sendto(fd, buff, len, flags, NULL, 0);
1956 * Receive a frame from the socket and optionally record the address of the
1957 * sender. We verify the buffers are writable and if needed move the
1958 * sender address from kernel to user space.
1960 int __sys_recvfrom(int fd, void __user *ubuf, size_t size, unsigned int flags,
1961 struct sockaddr __user *addr, int __user *addr_len)
1963 struct socket *sock;
1966 struct sockaddr_storage address;
1970 err = import_single_range(READ, ubuf, size, &iov, &msg.msg_iter);
1973 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1977 msg.msg_control = NULL;
1978 msg.msg_controllen = 0;
1979 /* Save some cycles and don't copy the address if not needed */
1980 msg.msg_name = addr ? (struct sockaddr *)&address : NULL;
1981 /* We assume all kernel code knows the size of sockaddr_storage */
1982 msg.msg_namelen = 0;
1983 msg.msg_iocb = NULL;
1985 if (sock->file->f_flags & O_NONBLOCK)
1986 flags |= MSG_DONTWAIT;
1987 err = sock_recvmsg(sock, &msg, flags);
1989 if (err >= 0 && addr != NULL) {
1990 err2 = move_addr_to_user(&address,
1991 msg.msg_namelen, addr, addr_len);
1996 fput_light(sock->file, fput_needed);
2001 SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, ubuf, size_t, size,
2002 unsigned int, flags, struct sockaddr __user *, addr,
2003 int __user *, addr_len)
2005 return __sys_recvfrom(fd, ubuf, size, flags, addr, addr_len);
2009 * Receive a datagram from a socket.
2012 SYSCALL_DEFINE4(recv, int, fd, void __user *, ubuf, size_t, size,
2013 unsigned int, flags)
2015 return __sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
2019 * Set a socket option. Because we don't know the option lengths we have
2020 * to pass the user mode parameter for the protocols to sort out.
2023 static int __sys_setsockopt(int fd, int level, int optname,
2024 char __user *optval, int optlen)
2026 int err, fput_needed;
2027 struct socket *sock;
2032 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2034 err = security_socket_setsockopt(sock, level, optname);
2038 if (level == SOL_SOCKET)
2040 sock_setsockopt(sock, level, optname, optval,
2044 sock->ops->setsockopt(sock, level, optname, optval,
2047 fput_light(sock->file, fput_needed);
2052 SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,
2053 char __user *, optval, int, optlen)
2055 return __sys_setsockopt(fd, level, optname, optval, optlen);
2059 * Get a socket option. Because we don't know the option lengths we have
2060 * to pass a user mode parameter for the protocols to sort out.
2063 static int __sys_getsockopt(int fd, int level, int optname,
2064 char __user *optval, int __user *optlen)
2066 int err, fput_needed;
2067 struct socket *sock;
2069 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2071 err = security_socket_getsockopt(sock, level, optname);
2075 if (level == SOL_SOCKET)
2077 sock_getsockopt(sock, level, optname, optval,
2081 sock->ops->getsockopt(sock, level, optname, optval,
2084 fput_light(sock->file, fput_needed);
2089 SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname,
2090 char __user *, optval, int __user *, optlen)
2092 return __sys_getsockopt(fd, level, optname, optval, optlen);
2096 * Shutdown a socket.
2099 int __sys_shutdown(int fd, int how)
2101 int err, fput_needed;
2102 struct socket *sock;
2104 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2106 err = security_socket_shutdown(sock, how);
2108 err = sock->ops->shutdown(sock, how);
2109 fput_light(sock->file, fput_needed);
2114 SYSCALL_DEFINE2(shutdown, int, fd, int, how)
2116 return __sys_shutdown(fd, how);
2119 /* A couple of helpful macros for getting the address of the 32/64 bit
2120 * fields which are the same type (int / unsigned) on our platforms.
2122 #define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
2123 #define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
2124 #define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
2126 struct used_address {
2127 struct sockaddr_storage name;
2128 unsigned int name_len;
2131 static int copy_msghdr_from_user(struct msghdr *kmsg,
2132 struct user_msghdr __user *umsg,
2133 struct sockaddr __user **save_addr,
2136 struct user_msghdr msg;
2139 if (copy_from_user(&msg, umsg, sizeof(*umsg)))
2142 kmsg->msg_control = (void __force *)msg.msg_control;
2143 kmsg->msg_controllen = msg.msg_controllen;
2144 kmsg->msg_flags = msg.msg_flags;
2146 kmsg->msg_namelen = msg.msg_namelen;
2148 kmsg->msg_namelen = 0;
2150 if (kmsg->msg_namelen < 0)
2153 if (kmsg->msg_namelen > sizeof(struct sockaddr_storage))
2154 kmsg->msg_namelen = sizeof(struct sockaddr_storage);
2157 *save_addr = msg.msg_name;
2159 if (msg.msg_name && kmsg->msg_namelen) {
2161 err = move_addr_to_kernel(msg.msg_name,
2168 kmsg->msg_name = NULL;
2169 kmsg->msg_namelen = 0;
2172 if (msg.msg_iovlen > UIO_MAXIOV)
2175 kmsg->msg_iocb = NULL;
2177 return import_iovec(save_addr ? READ : WRITE,
2178 msg.msg_iov, msg.msg_iovlen,
2179 UIO_FASTIOV, iov, &kmsg->msg_iter);
2182 static int ___sys_sendmsg(struct socket *sock, struct user_msghdr __user *msg,
2183 struct msghdr *msg_sys, unsigned int flags,
2184 struct used_address *used_address,
2185 unsigned int allowed_msghdr_flags)
2187 struct compat_msghdr __user *msg_compat =
2188 (struct compat_msghdr __user *)msg;
2189 struct sockaddr_storage address;
2190 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
2191 unsigned char ctl[sizeof(struct cmsghdr) + 20]
2192 __aligned(sizeof(__kernel_size_t));
2193 /* 20 is size of ipv6_pktinfo */
2194 unsigned char *ctl_buf = ctl;
2198 msg_sys->msg_name = &address;
2200 if (MSG_CMSG_COMPAT & flags)
2201 err = get_compat_msghdr(msg_sys, msg_compat, NULL, &iov);
2203 err = copy_msghdr_from_user(msg_sys, msg, NULL, &iov);
2209 if (msg_sys->msg_controllen > INT_MAX)
2211 flags |= (msg_sys->msg_flags & allowed_msghdr_flags);
2212 ctl_len = msg_sys->msg_controllen;
2213 if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
2215 cmsghdr_from_user_compat_to_kern(msg_sys, sock->sk, ctl,
2219 ctl_buf = msg_sys->msg_control;
2220 ctl_len = msg_sys->msg_controllen;
2221 } else if (ctl_len) {
2222 BUILD_BUG_ON(sizeof(struct cmsghdr) !=
2223 CMSG_ALIGN(sizeof(struct cmsghdr)));
2224 if (ctl_len > sizeof(ctl)) {
2225 ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
2226 if (ctl_buf == NULL)
2231 * Careful! Before this, msg_sys->msg_control contains a user pointer.
2232 * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
2233 * checking falls down on this.
2235 if (copy_from_user(ctl_buf,
2236 (void __user __force *)msg_sys->msg_control,
2239 msg_sys->msg_control = ctl_buf;
2241 msg_sys->msg_flags = flags;
2243 if (sock->file->f_flags & O_NONBLOCK)
2244 msg_sys->msg_flags |= MSG_DONTWAIT;
2246 * If this is sendmmsg() and current destination address is same as
2247 * previously succeeded address, omit asking LSM's decision.
2248 * used_address->name_len is initialized to UINT_MAX so that the first
2249 * destination address never matches.
2251 if (used_address && msg_sys->msg_name &&
2252 used_address->name_len == msg_sys->msg_namelen &&
2253 !memcmp(&used_address->name, msg_sys->msg_name,
2254 used_address->name_len)) {
2255 err = sock_sendmsg_nosec(sock, msg_sys);
2258 err = __sock_sendmsg(sock, msg_sys);
2260 * If this is sendmmsg() and sending to current destination address was
2261 * successful, remember it.
2263 if (used_address && err >= 0) {
2264 used_address->name_len = msg_sys->msg_namelen;
2265 if (msg_sys->msg_name)
2266 memcpy(&used_address->name, msg_sys->msg_name,
2267 used_address->name_len);
2272 sock_kfree_s(sock->sk, ctl_buf, ctl_len);
2279 * BSD sendmsg interface
2282 long __sys_sendmsg(int fd, struct user_msghdr __user *msg, unsigned int flags,
2283 bool forbid_cmsg_compat)
2285 int fput_needed, err;
2286 struct msghdr msg_sys;
2287 struct socket *sock;
2289 if (forbid_cmsg_compat && (flags & MSG_CMSG_COMPAT))
2292 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2296 err = ___sys_sendmsg(sock, msg, &msg_sys, flags, NULL, 0);
2298 fput_light(sock->file, fput_needed);
2303 SYSCALL_DEFINE3(sendmsg, int, fd, struct user_msghdr __user *, msg, unsigned int, flags)
2305 return __sys_sendmsg(fd, msg, flags, true);
2309 * Linux sendmmsg interface
2312 int __sys_sendmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
2313 unsigned int flags, bool forbid_cmsg_compat)
2315 int fput_needed, err, datagrams;
2316 struct socket *sock;
2317 struct mmsghdr __user *entry;
2318 struct compat_mmsghdr __user *compat_entry;
2319 struct msghdr msg_sys;
2320 struct used_address used_address;
2321 unsigned int oflags = flags;
2323 if (forbid_cmsg_compat && (flags & MSG_CMSG_COMPAT))
2326 if (vlen > UIO_MAXIOV)
2331 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2335 used_address.name_len = UINT_MAX;
2337 compat_entry = (struct compat_mmsghdr __user *)mmsg;
2341 while (datagrams < vlen) {
2342 if (datagrams == vlen - 1)
2345 if (MSG_CMSG_COMPAT & flags) {
2346 err = ___sys_sendmsg(sock, (struct user_msghdr __user *)compat_entry,
2347 &msg_sys, flags, &used_address, MSG_EOR);
2350 err = __put_user(err, &compat_entry->msg_len);
2353 err = ___sys_sendmsg(sock,
2354 (struct user_msghdr __user *)entry,
2355 &msg_sys, flags, &used_address, MSG_EOR);
2358 err = put_user(err, &entry->msg_len);
2365 if (msg_data_left(&msg_sys))
2370 fput_light(sock->file, fput_needed);
2372 /* We only return an error if no datagrams were able to be sent */
2379 SYSCALL_DEFINE4(sendmmsg, int, fd, struct mmsghdr __user *, mmsg,
2380 unsigned int, vlen, unsigned int, flags)
2382 return __sys_sendmmsg(fd, mmsg, vlen, flags, true);
2385 static int ___sys_recvmsg(struct socket *sock, struct user_msghdr __user *msg,
2386 struct msghdr *msg_sys, unsigned int flags, int nosec)
2388 struct compat_msghdr __user *msg_compat =
2389 (struct compat_msghdr __user *)msg;
2390 struct iovec iovstack[UIO_FASTIOV];
2391 struct iovec *iov = iovstack;
2392 unsigned long cmsg_ptr;
2396 /* kernel mode address */
2397 struct sockaddr_storage addr;
2399 /* user mode address pointers */
2400 struct sockaddr __user *uaddr;
2401 int __user *uaddr_len = COMPAT_NAMELEN(msg);
2403 msg_sys->msg_name = &addr;
2405 if (MSG_CMSG_COMPAT & flags)
2406 err = get_compat_msghdr(msg_sys, msg_compat, &uaddr, &iov);
2408 err = copy_msghdr_from_user(msg_sys, msg, &uaddr, &iov);
2412 cmsg_ptr = (unsigned long)msg_sys->msg_control;
2413 msg_sys->msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
2415 /* We assume all kernel code knows the size of sockaddr_storage */
2416 msg_sys->msg_namelen = 0;
2418 if (sock->file->f_flags & O_NONBLOCK)
2419 flags |= MSG_DONTWAIT;
2420 err = (nosec ? sock_recvmsg_nosec : sock_recvmsg)(sock, msg_sys, flags);
2425 if (uaddr != NULL) {
2426 err = move_addr_to_user(&addr,
2427 msg_sys->msg_namelen, uaddr,
2432 err = __put_user((msg_sys->msg_flags & ~MSG_CMSG_COMPAT),
2436 if (MSG_CMSG_COMPAT & flags)
2437 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
2438 &msg_compat->msg_controllen);
2440 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
2441 &msg->msg_controllen);
2452 * BSD recvmsg interface
2455 long __sys_recvmsg(int fd, struct user_msghdr __user *msg, unsigned int flags,
2456 bool forbid_cmsg_compat)
2458 int fput_needed, err;
2459 struct msghdr msg_sys;
2460 struct socket *sock;
2462 if (forbid_cmsg_compat && (flags & MSG_CMSG_COMPAT))
2465 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2469 err = ___sys_recvmsg(sock, msg, &msg_sys, flags, 0);
2471 fput_light(sock->file, fput_needed);
2476 SYSCALL_DEFINE3(recvmsg, int, fd, struct user_msghdr __user *, msg,
2477 unsigned int, flags)
2479 return __sys_recvmsg(fd, msg, flags, true);
2483 * Linux recvmmsg interface
2486 int __sys_recvmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
2487 unsigned int flags, struct timespec *timeout)
2489 int fput_needed, err, datagrams;
2490 struct socket *sock;
2491 struct mmsghdr __user *entry;
2492 struct compat_mmsghdr __user *compat_entry;
2493 struct msghdr msg_sys;
2494 struct timespec64 end_time;
2495 struct timespec64 timeout64;
2498 poll_select_set_timeout(&end_time, timeout->tv_sec,
2504 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2508 if (likely(!(flags & MSG_ERRQUEUE))) {
2509 err = sock_error(sock->sk);
2517 compat_entry = (struct compat_mmsghdr __user *)mmsg;
2519 while (datagrams < vlen) {
2521 * No need to ask LSM for more than the first datagram.
2523 if (MSG_CMSG_COMPAT & flags) {
2524 err = ___sys_recvmsg(sock, (struct user_msghdr __user *)compat_entry,
2525 &msg_sys, flags & ~MSG_WAITFORONE,
2529 err = __put_user(err, &compat_entry->msg_len);
2532 err = ___sys_recvmsg(sock,
2533 (struct user_msghdr __user *)entry,
2534 &msg_sys, flags & ~MSG_WAITFORONE,
2538 err = put_user(err, &entry->msg_len);
2546 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
2547 if (flags & MSG_WAITFORONE)
2548 flags |= MSG_DONTWAIT;
2551 ktime_get_ts64(&timeout64);
2552 *timeout = timespec64_to_timespec(
2553 timespec64_sub(end_time, timeout64));
2554 if (timeout->tv_sec < 0) {
2555 timeout->tv_sec = timeout->tv_nsec = 0;
2559 /* Timeout, return less than vlen datagrams */
2560 if (timeout->tv_nsec == 0 && timeout->tv_sec == 0)
2564 /* Out of band data, return right away */
2565 if (msg_sys.msg_flags & MSG_OOB)
2573 if (datagrams == 0) {
2579 * We may return less entries than requested (vlen) if the
2580 * sock is non block and there aren't enough datagrams...
2582 if (err != -EAGAIN) {
2584 * ... or if recvmsg returns an error after we
2585 * received some datagrams, where we record the
2586 * error to return on the next call or if the
2587 * app asks about it using getsockopt(SO_ERROR).
2589 WRITE_ONCE(sock->sk->sk_err, -err);
2592 fput_light(sock->file, fput_needed);
2597 static int do_sys_recvmmsg(int fd, struct mmsghdr __user *mmsg,
2598 unsigned int vlen, unsigned int flags,
2599 struct timespec __user *timeout)
2602 struct timespec timeout_sys;
2604 if (flags & MSG_CMSG_COMPAT)
2608 return __sys_recvmmsg(fd, mmsg, vlen, flags, NULL);
2610 if (copy_from_user(&timeout_sys, timeout, sizeof(timeout_sys)))
2613 datagrams = __sys_recvmmsg(fd, mmsg, vlen, flags, &timeout_sys);
2615 if (datagrams > 0 &&
2616 copy_to_user(timeout, &timeout_sys, sizeof(timeout_sys)))
2617 datagrams = -EFAULT;
2622 SYSCALL_DEFINE5(recvmmsg, int, fd, struct mmsghdr __user *, mmsg,
2623 unsigned int, vlen, unsigned int, flags,
2624 struct timespec __user *, timeout)
2626 return do_sys_recvmmsg(fd, mmsg, vlen, flags, timeout);
2629 #ifdef __ARCH_WANT_SYS_SOCKETCALL
2630 /* Argument list sizes for sys_socketcall */
2631 #define AL(x) ((x) * sizeof(unsigned long))
2632 static const unsigned char nargs[21] = {
2633 AL(0), AL(3), AL(3), AL(3), AL(2), AL(3),
2634 AL(3), AL(3), AL(4), AL(4), AL(4), AL(6),
2635 AL(6), AL(2), AL(5), AL(5), AL(3), AL(3),
2642 * System call vectors.
2644 * Argument checking cleaned up. Saved 20% in size.
2645 * This function doesn't need to set the kernel lock because
2646 * it is set by the callees.
2649 SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args)
2651 unsigned long a[AUDITSC_ARGS];
2652 unsigned long a0, a1;
2656 if (call < 1 || call > SYS_SENDMMSG)
2658 call = array_index_nospec(call, SYS_SENDMMSG + 1);
2661 if (len > sizeof(a))
2664 /* copy_from_user should be SMP safe. */
2665 if (copy_from_user(a, args, len))
2668 err = audit_socketcall(nargs[call] / sizeof(unsigned long), a);
2677 err = __sys_socket(a0, a1, a[2]);
2680 err = __sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
2683 err = __sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
2686 err = __sys_listen(a0, a1);
2689 err = __sys_accept4(a0, (struct sockaddr __user *)a1,
2690 (int __user *)a[2], 0);
2692 case SYS_GETSOCKNAME:
2694 __sys_getsockname(a0, (struct sockaddr __user *)a1,
2695 (int __user *)a[2]);
2697 case SYS_GETPEERNAME:
2699 __sys_getpeername(a0, (struct sockaddr __user *)a1,
2700 (int __user *)a[2]);
2702 case SYS_SOCKETPAIR:
2703 err = __sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
2706 err = __sys_sendto(a0, (void __user *)a1, a[2], a[3],
2710 err = __sys_sendto(a0, (void __user *)a1, a[2], a[3],
2711 (struct sockaddr __user *)a[4], a[5]);
2714 err = __sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
2718 err = __sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
2719 (struct sockaddr __user *)a[4],
2720 (int __user *)a[5]);
2723 err = __sys_shutdown(a0, a1);
2725 case SYS_SETSOCKOPT:
2726 err = __sys_setsockopt(a0, a1, a[2], (char __user *)a[3],
2729 case SYS_GETSOCKOPT:
2731 __sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
2732 (int __user *)a[4]);
2735 err = __sys_sendmsg(a0, (struct user_msghdr __user *)a1,
2739 err = __sys_sendmmsg(a0, (struct mmsghdr __user *)a1, a[2],
2743 err = __sys_recvmsg(a0, (struct user_msghdr __user *)a1,
2747 err = do_sys_recvmmsg(a0, (struct mmsghdr __user *)a1, a[2],
2748 a[3], (struct timespec __user *)a[4]);
2751 err = __sys_accept4(a0, (struct sockaddr __user *)a1,
2752 (int __user *)a[2], a[3]);
2761 #endif /* __ARCH_WANT_SYS_SOCKETCALL */
2764 * sock_register - add a socket protocol handler
2765 * @ops: description of protocol
2767 * This function is called by a protocol handler that wants to
2768 * advertise its address family, and have it linked into the
2769 * socket interface. The value ops->family corresponds to the
2770 * socket system call protocol family.
2772 int sock_register(const struct net_proto_family *ops)
2776 if (ops->family >= NPROTO) {
2777 pr_crit("protocol %d >= NPROTO(%d)\n", ops->family, NPROTO);
2781 spin_lock(&net_family_lock);
2782 if (rcu_dereference_protected(net_families[ops->family],
2783 lockdep_is_held(&net_family_lock)))
2786 rcu_assign_pointer(net_families[ops->family], ops);
2789 spin_unlock(&net_family_lock);
2791 pr_info("NET: Registered protocol family %d\n", ops->family);
2794 EXPORT_SYMBOL(sock_register);
2797 * sock_unregister - remove a protocol handler
2798 * @family: protocol family to remove
2800 * This function is called by a protocol handler that wants to
2801 * remove its address family, and have it unlinked from the
2802 * new socket creation.
2804 * If protocol handler is a module, then it can use module reference
2805 * counts to protect against new references. If protocol handler is not
2806 * a module then it needs to provide its own protection in
2807 * the ops->create routine.
2809 void sock_unregister(int family)
2811 BUG_ON(family < 0 || family >= NPROTO);
2813 spin_lock(&net_family_lock);
2814 RCU_INIT_POINTER(net_families[family], NULL);
2815 spin_unlock(&net_family_lock);
2819 pr_info("NET: Unregistered protocol family %d\n", family);
2821 EXPORT_SYMBOL(sock_unregister);
2823 bool sock_is_registered(int family)
2825 return family < NPROTO && rcu_access_pointer(net_families[family]);
2828 static int __init sock_init(void)
2832 * Initialize the network sysctl infrastructure.
2834 err = net_sysctl_init();
2839 * Initialize skbuff SLAB cache
2844 * Initialize the protocols module.
2849 err = register_filesystem(&sock_fs_type);
2852 sock_mnt = kern_mount(&sock_fs_type);
2853 if (IS_ERR(sock_mnt)) {
2854 err = PTR_ERR(sock_mnt);
2858 /* The real protocol initialization is performed in later initcalls.
2861 #ifdef CONFIG_NETFILTER
2862 err = netfilter_init();
2867 ptp_classifier_init();
2873 unregister_filesystem(&sock_fs_type);
2878 core_initcall(sock_init); /* early initcall */
2880 #ifdef CONFIG_PROC_FS
2881 void socket_seq_show(struct seq_file *seq)
2883 seq_printf(seq, "sockets: used %d\n",
2884 sock_inuse_get(seq->private));
2886 #endif /* CONFIG_PROC_FS */
2888 #ifdef CONFIG_COMPAT
2889 static int do_siocgstamp(struct net *net, struct socket *sock,
2890 unsigned int cmd, void __user *up)
2892 mm_segment_t old_fs = get_fs();
2897 err = sock_do_ioctl(net, sock, cmd, (unsigned long)&ktv);
2900 err = compat_put_timeval(&ktv, up);
2905 static int do_siocgstampns(struct net *net, struct socket *sock,
2906 unsigned int cmd, void __user *up)
2908 mm_segment_t old_fs = get_fs();
2909 struct timespec kts;
2913 err = sock_do_ioctl(net, sock, cmd, (unsigned long)&kts);
2916 err = compat_put_timespec(&kts, up);
2921 static int compat_dev_ifconf(struct net *net, struct compat_ifconf __user *uifc32)
2923 struct compat_ifconf ifc32;
2927 if (copy_from_user(&ifc32, uifc32, sizeof(struct compat_ifconf)))
2930 ifc.ifc_len = ifc32.ifc_len;
2931 ifc.ifc_req = compat_ptr(ifc32.ifcbuf);
2934 err = dev_ifconf(net, &ifc, sizeof(struct compat_ifreq));
2939 ifc32.ifc_len = ifc.ifc_len;
2940 if (copy_to_user(uifc32, &ifc32, sizeof(struct compat_ifconf)))
2946 static int ethtool_ioctl(struct net *net, struct compat_ifreq __user *ifr32)
2948 struct compat_ethtool_rxnfc __user *compat_rxnfc;
2949 bool convert_in = false, convert_out = false;
2950 size_t buf_size = 0;
2951 struct ethtool_rxnfc __user *rxnfc = NULL;
2953 u32 rule_cnt = 0, actual_rule_cnt;
2958 if (get_user(data, &ifr32->ifr_ifru.ifru_data))
2961 compat_rxnfc = compat_ptr(data);
2963 if (get_user(ethcmd, &compat_rxnfc->cmd))
2966 /* Most ethtool structures are defined without padding.
2967 * Unfortunately struct ethtool_rxnfc is an exception.
2972 case ETHTOOL_GRXCLSRLALL:
2973 /* Buffer size is variable */
2974 if (get_user(rule_cnt, &compat_rxnfc->rule_cnt))
2976 if (rule_cnt > KMALLOC_MAX_SIZE / sizeof(u32))
2978 buf_size += rule_cnt * sizeof(u32);
2980 case ETHTOOL_GRXRINGS:
2981 case ETHTOOL_GRXCLSRLCNT:
2982 case ETHTOOL_GRXCLSRULE:
2983 case ETHTOOL_SRXCLSRLINS:
2986 case ETHTOOL_SRXCLSRLDEL:
2987 buf_size += sizeof(struct ethtool_rxnfc);
2989 rxnfc = compat_alloc_user_space(buf_size);
2993 if (copy_from_user(&ifr.ifr_name, &ifr32->ifr_name, IFNAMSIZ))
2996 ifr.ifr_data = convert_in ? rxnfc : (void __user *)compat_rxnfc;
2999 /* We expect there to be holes between fs.m_ext and
3000 * fs.ring_cookie and at the end of fs, but nowhere else.
3002 BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc, fs.m_ext) +
3003 sizeof(compat_rxnfc->fs.m_ext) !=
3004 offsetof(struct ethtool_rxnfc, fs.m_ext) +
3005 sizeof(rxnfc->fs.m_ext));
3007 offsetof(struct compat_ethtool_rxnfc, fs.location) -
3008 offsetof(struct compat_ethtool_rxnfc, fs.ring_cookie) !=
3009 offsetof(struct ethtool_rxnfc, fs.location) -
3010 offsetof(struct ethtool_rxnfc, fs.ring_cookie));
3012 if (copy_in_user(rxnfc, compat_rxnfc,
3013 (void __user *)(&rxnfc->fs.m_ext + 1) -
3014 (void __user *)rxnfc) ||
3015 copy_in_user(&rxnfc->fs.ring_cookie,
3016 &compat_rxnfc->fs.ring_cookie,
3017 (void __user *)(&rxnfc->fs.location + 1) -
3018 (void __user *)&rxnfc->fs.ring_cookie))
3020 if (ethcmd == ETHTOOL_GRXCLSRLALL) {
3021 if (put_user(rule_cnt, &rxnfc->rule_cnt))
3023 } else if (copy_in_user(&rxnfc->rule_cnt,
3024 &compat_rxnfc->rule_cnt,
3025 sizeof(rxnfc->rule_cnt)))
3029 ret = dev_ioctl(net, SIOCETHTOOL, &ifr, NULL);
3034 if (copy_in_user(compat_rxnfc, rxnfc,
3035 (const void __user *)(&rxnfc->fs.m_ext + 1) -
3036 (const void __user *)rxnfc) ||
3037 copy_in_user(&compat_rxnfc->fs.ring_cookie,
3038 &rxnfc->fs.ring_cookie,
3039 (const void __user *)(&rxnfc->fs.location + 1) -
3040 (const void __user *)&rxnfc->fs.ring_cookie) ||
3041 copy_in_user(&compat_rxnfc->rule_cnt, &rxnfc->rule_cnt,
3042 sizeof(rxnfc->rule_cnt)))
3045 if (ethcmd == ETHTOOL_GRXCLSRLALL) {
3046 /* As an optimisation, we only copy the actual
3047 * number of rules that the underlying
3048 * function returned. Since Mallory might
3049 * change the rule count in user memory, we
3050 * check that it is less than the rule count
3051 * originally given (as the user buffer size),
3052 * which has been range-checked.
3054 if (get_user(actual_rule_cnt, &rxnfc->rule_cnt))
3056 if (actual_rule_cnt < rule_cnt)
3057 rule_cnt = actual_rule_cnt;
3058 if (copy_in_user(&compat_rxnfc->rule_locs[0],
3059 &rxnfc->rule_locs[0],
3060 rule_cnt * sizeof(u32)))
3068 static int compat_siocwandev(struct net *net, struct compat_ifreq __user *uifr32)
3070 compat_uptr_t uptr32;
3075 if (copy_from_user(&ifr, uifr32, sizeof(struct compat_ifreq)))
3078 if (get_user(uptr32, &uifr32->ifr_settings.ifs_ifsu))
3081 saved = ifr.ifr_settings.ifs_ifsu.raw_hdlc;
3082 ifr.ifr_settings.ifs_ifsu.raw_hdlc = compat_ptr(uptr32);
3084 err = dev_ioctl(net, SIOCWANDEV, &ifr, NULL);
3086 ifr.ifr_settings.ifs_ifsu.raw_hdlc = saved;
3087 if (copy_to_user(uifr32, &ifr, sizeof(struct compat_ifreq)))
3093 /* Handle ioctls that use ifreq::ifr_data and just need struct ifreq converted */
3094 static int compat_ifr_data_ioctl(struct net *net, unsigned int cmd,
3095 struct compat_ifreq __user *u_ifreq32)
3100 if (!is_socket_ioctl_cmd(cmd))
3102 if (copy_from_user(ifreq.ifr_name, u_ifreq32->ifr_name, IFNAMSIZ))
3104 if (get_user(data32, &u_ifreq32->ifr_data))
3106 ifreq.ifr_data = compat_ptr(data32);
3108 return dev_ioctl(net, cmd, &ifreq, NULL);
3111 static int compat_ifreq_ioctl(struct net *net, struct socket *sock,
3113 struct compat_ifreq __user *uifr32)
3115 struct ifreq __user *uifr;
3118 /* Handle the fact that while struct ifreq has the same *layout* on
3119 * 32/64 for everything but ifreq::ifru_ifmap and ifreq::ifru_data,
3120 * which are handled elsewhere, it still has different *size* due to
3121 * ifreq::ifru_ifmap (which is 16 bytes on 32 bit, 24 bytes on 64-bit,
3122 * resulting in struct ifreq being 32 and 40 bytes respectively).
3123 * As a result, if the struct happens to be at the end of a page and
3124 * the next page isn't readable/writable, we get a fault. To prevent
3125 * that, copy back and forth to the full size.
3128 uifr = compat_alloc_user_space(sizeof(*uifr));
3129 if (copy_in_user(uifr, uifr32, sizeof(*uifr32)))
3132 err = sock_do_ioctl(net, sock, cmd, (unsigned long)uifr);
3143 case SIOCGIFBRDADDR:
3144 case SIOCGIFDSTADDR:
3145 case SIOCGIFNETMASK:
3151 if (copy_in_user(uifr32, uifr, sizeof(*uifr32)))
3159 static int compat_sioc_ifmap(struct net *net, unsigned int cmd,
3160 struct compat_ifreq __user *uifr32)
3163 struct compat_ifmap __user *uifmap32;
3166 uifmap32 = &uifr32->ifr_ifru.ifru_map;
3167 err = copy_from_user(&ifr, uifr32, sizeof(ifr.ifr_name));
3168 err |= get_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
3169 err |= get_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
3170 err |= get_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
3171 err |= get_user(ifr.ifr_map.irq, &uifmap32->irq);
3172 err |= get_user(ifr.ifr_map.dma, &uifmap32->dma);
3173 err |= get_user(ifr.ifr_map.port, &uifmap32->port);
3177 err = dev_ioctl(net, cmd, &ifr, NULL);
3179 if (cmd == SIOCGIFMAP && !err) {
3180 err = copy_to_user(uifr32, &ifr, sizeof(ifr.ifr_name));
3181 err |= put_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
3182 err |= put_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
3183 err |= put_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
3184 err |= put_user(ifr.ifr_map.irq, &uifmap32->irq);
3185 err |= put_user(ifr.ifr_map.dma, &uifmap32->dma);
3186 err |= put_user(ifr.ifr_map.port, &uifmap32->port);
3195 struct sockaddr rt_dst; /* target address */
3196 struct sockaddr rt_gateway; /* gateway addr (RTF_GATEWAY) */
3197 struct sockaddr rt_genmask; /* target network mask (IP) */
3198 unsigned short rt_flags;
3201 unsigned char rt_tos;
3202 unsigned char rt_class;
3204 short rt_metric; /* +1 for binary compatibility! */
3205 /* char * */ u32 rt_dev; /* forcing the device at add */
3206 u32 rt_mtu; /* per route MTU/Window */
3207 u32 rt_window; /* Window clamping */
3208 unsigned short rt_irtt; /* Initial RTT */
3211 struct in6_rtmsg32 {
3212 struct in6_addr rtmsg_dst;
3213 struct in6_addr rtmsg_src;
3214 struct in6_addr rtmsg_gateway;
3224 static int routing_ioctl(struct net *net, struct socket *sock,
3225 unsigned int cmd, void __user *argp)
3229 struct in6_rtmsg r6;
3233 mm_segment_t old_fs = get_fs();
3235 if (sock && sock->sk && sock->sk->sk_family == AF_INET6) { /* ipv6 */
3236 struct in6_rtmsg32 __user *ur6 = argp;
3237 ret = copy_from_user(&r6.rtmsg_dst, &(ur6->rtmsg_dst),
3238 3 * sizeof(struct in6_addr));
3239 ret |= get_user(r6.rtmsg_type, &(ur6->rtmsg_type));
3240 ret |= get_user(r6.rtmsg_dst_len, &(ur6->rtmsg_dst_len));
3241 ret |= get_user(r6.rtmsg_src_len, &(ur6->rtmsg_src_len));
3242 ret |= get_user(r6.rtmsg_metric, &(ur6->rtmsg_metric));
3243 ret |= get_user(r6.rtmsg_info, &(ur6->rtmsg_info));
3244 ret |= get_user(r6.rtmsg_flags, &(ur6->rtmsg_flags));
3245 ret |= get_user(r6.rtmsg_ifindex, &(ur6->rtmsg_ifindex));
3249 struct rtentry32 __user *ur4 = argp;
3250 ret = copy_from_user(&r4.rt_dst, &(ur4->rt_dst),
3251 3 * sizeof(struct sockaddr));
3252 ret |= get_user(r4.rt_flags, &(ur4->rt_flags));
3253 ret |= get_user(r4.rt_metric, &(ur4->rt_metric));
3254 ret |= get_user(r4.rt_mtu, &(ur4->rt_mtu));
3255 ret |= get_user(r4.rt_window, &(ur4->rt_window));
3256 ret |= get_user(r4.rt_irtt, &(ur4->rt_irtt));
3257 ret |= get_user(rtdev, &(ur4->rt_dev));
3259 ret |= copy_from_user(devname, compat_ptr(rtdev), 15);
3260 r4.rt_dev = (char __user __force *)devname;
3274 ret = sock_do_ioctl(net, sock, cmd, (unsigned long) r);
3281 /* Since old style bridge ioctl's endup using SIOCDEVPRIVATE
3282 * for some operations; this forces use of the newer bridge-utils that
3283 * use compatible ioctls
3285 static int old_bridge_ioctl(compat_ulong_t __user *argp)
3289 if (get_user(tmp, argp))
3291 if (tmp == BRCTL_GET_VERSION)
3292 return BRCTL_VERSION + 1;
3296 static int compat_sock_ioctl_trans(struct file *file, struct socket *sock,
3297 unsigned int cmd, unsigned long arg)
3299 void __user *argp = compat_ptr(arg);
3300 struct sock *sk = sock->sk;
3301 struct net *net = sock_net(sk);
3303 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))
3304 return compat_ifr_data_ioctl(net, cmd, argp);
3309 return old_bridge_ioctl(argp);
3311 return compat_dev_ifconf(net, argp);
3313 return ethtool_ioctl(net, argp);
3315 return compat_siocwandev(net, argp);
3318 return compat_sioc_ifmap(net, cmd, argp);
3321 return routing_ioctl(net, sock, cmd, argp);
3323 return do_siocgstamp(net, sock, cmd, argp);
3325 return do_siocgstampns(net, sock, cmd, argp);
3326 case SIOCBONDSLAVEINFOQUERY:
3327 case SIOCBONDINFOQUERY:
3330 return compat_ifr_data_ioctl(net, cmd, argp);
3343 return sock_ioctl(file, cmd, arg);
3360 case SIOCSIFHWBROADCAST:
3362 case SIOCGIFBRDADDR:
3363 case SIOCSIFBRDADDR:
3364 case SIOCGIFDSTADDR:
3365 case SIOCSIFDSTADDR:
3366 case SIOCGIFNETMASK:
3367 case SIOCSIFNETMASK:
3379 case SIOCBONDENSLAVE:
3380 case SIOCBONDRELEASE:
3381 case SIOCBONDSETHWADDR:
3382 case SIOCBONDCHANGEACTIVE:
3383 return compat_ifreq_ioctl(net, sock, cmd, argp);
3390 return sock_do_ioctl(net, sock, cmd, arg);
3393 return -ENOIOCTLCMD;
3396 static long compat_sock_ioctl(struct file *file, unsigned int cmd,
3399 struct socket *sock = file->private_data;
3400 int ret = -ENOIOCTLCMD;
3407 if (sock->ops->compat_ioctl)
3408 ret = sock->ops->compat_ioctl(sock, cmd, arg);
3410 if (ret == -ENOIOCTLCMD &&
3411 (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST))
3412 ret = compat_wext_handle_ioctl(net, cmd, arg);
3414 if (ret == -ENOIOCTLCMD)
3415 ret = compat_sock_ioctl_trans(file, sock, cmd, arg);
3422 * kernel_bind - bind an address to a socket (kernel space)
3425 * @addrlen: length of address
3427 * Returns 0 or an error.
3430 int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
3432 struct sockaddr_storage address;
3434 memcpy(&address, addr, addrlen);
3436 return sock->ops->bind(sock, (struct sockaddr *)&address, addrlen);
3438 EXPORT_SYMBOL(kernel_bind);
3441 * kernel_listen - move socket to listening state (kernel space)
3443 * @backlog: pending connections queue size
3445 * Returns 0 or an error.
3448 int kernel_listen(struct socket *sock, int backlog)
3450 return sock->ops->listen(sock, backlog);
3452 EXPORT_SYMBOL(kernel_listen);
3455 * kernel_accept - accept a connection (kernel space)
3456 * @sock: listening socket
3457 * @newsock: new connected socket
3460 * @flags must be SOCK_CLOEXEC, SOCK_NONBLOCK or 0.
3461 * If it fails, @newsock is guaranteed to be %NULL.
3462 * Returns 0 or an error.
3465 int kernel_accept(struct socket *sock, struct socket **newsock, int flags)
3467 struct sock *sk = sock->sk;
3470 err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
3475 err = sock->ops->accept(sock, *newsock, flags, true);
3477 sock_release(*newsock);
3482 (*newsock)->ops = sock->ops;
3483 __module_get((*newsock)->ops->owner);
3488 EXPORT_SYMBOL(kernel_accept);
3491 * kernel_connect - connect a socket (kernel space)
3494 * @addrlen: address length
3495 * @flags: flags (O_NONBLOCK, ...)
3497 * For datagram sockets, @addr is the addres to which datagrams are sent
3498 * by default, and the only address from which datagrams are received.
3499 * For stream sockets, attempts to connect to @addr.
3500 * Returns 0 or an error code.
3503 int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
3506 struct sockaddr_storage address;
3508 memcpy(&address, addr, addrlen);
3510 return sock->ops->connect(sock, (struct sockaddr *)&address, addrlen, flags);
3512 EXPORT_SYMBOL(kernel_connect);
3515 * kernel_getsockname - get the address which the socket is bound (kernel space)
3517 * @addr: address holder
3519 * Fills the @addr pointer with the address which the socket is bound.
3520 * Returns 0 or an error code.
3523 int kernel_getsockname(struct socket *sock, struct sockaddr *addr)
3525 return sock->ops->getname(sock, addr, 0);
3527 EXPORT_SYMBOL(kernel_getsockname);
3530 * kernel_peername - get the address which the socket is connected (kernel space)
3532 * @addr: address holder
3534 * Fills the @addr pointer with the address which the socket is connected.
3535 * Returns 0 or an error code.
3538 int kernel_getpeername(struct socket *sock, struct sockaddr *addr)
3540 return sock->ops->getname(sock, addr, 1);
3542 EXPORT_SYMBOL(kernel_getpeername);
3545 * kernel_getsockopt - get a socket option (kernel space)
3547 * @level: API level (SOL_SOCKET, ...)
3548 * @optname: option tag
3549 * @optval: option value
3550 * @optlen: option length
3552 * Assigns the option length to @optlen.
3553 * Returns 0 or an error.
3556 int kernel_getsockopt(struct socket *sock, int level, int optname,
3557 char *optval, int *optlen)
3559 mm_segment_t oldfs = get_fs();
3560 char __user *uoptval;
3561 int __user *uoptlen;
3564 uoptval = (char __user __force *) optval;
3565 uoptlen = (int __user __force *) optlen;
3568 if (level == SOL_SOCKET)
3569 err = sock_getsockopt(sock, level, optname, uoptval, uoptlen);
3571 err = sock->ops->getsockopt(sock, level, optname, uoptval,
3576 EXPORT_SYMBOL(kernel_getsockopt);
3579 * kernel_setsockopt - set a socket option (kernel space)
3581 * @level: API level (SOL_SOCKET, ...)
3582 * @optname: option tag
3583 * @optval: option value
3584 * @optlen: option length
3586 * Returns 0 or an error.
3589 int kernel_setsockopt(struct socket *sock, int level, int optname,
3590 char *optval, unsigned int optlen)
3592 mm_segment_t oldfs = get_fs();
3593 char __user *uoptval;
3596 uoptval = (char __user __force *) optval;
3599 if (level == SOL_SOCKET)
3600 err = sock_setsockopt(sock, level, optname, uoptval, optlen);
3602 err = sock->ops->setsockopt(sock, level, optname, uoptval,
3607 EXPORT_SYMBOL(kernel_setsockopt);
3610 * kernel_sendpage - send a &page through a socket (kernel space)
3613 * @offset: page offset
3614 * @size: total size in bytes
3615 * @flags: flags (MSG_DONTWAIT, ...)
3617 * Returns the total amount sent in bytes or an error.
3620 int kernel_sendpage(struct socket *sock, struct page *page, int offset,
3621 size_t size, int flags)
3623 if (sock->ops->sendpage)
3624 return sock->ops->sendpage(sock, page, offset, size, flags);
3626 return sock_no_sendpage(sock, page, offset, size, flags);
3628 EXPORT_SYMBOL(kernel_sendpage);
3631 * kernel_sendpage_locked - send a &page through the locked sock (kernel space)
3634 * @offset: page offset
3635 * @size: total size in bytes
3636 * @flags: flags (MSG_DONTWAIT, ...)
3638 * Returns the total amount sent in bytes or an error.
3639 * Caller must hold @sk.
3642 int kernel_sendpage_locked(struct sock *sk, struct page *page, int offset,
3643 size_t size, int flags)
3645 struct socket *sock = sk->sk_socket;
3647 if (sock->ops->sendpage_locked)
3648 return sock->ops->sendpage_locked(sk, page, offset, size,
3651 return sock_no_sendpage_locked(sk, page, offset, size, flags);
3653 EXPORT_SYMBOL(kernel_sendpage_locked);
3656 * kernel_shutdown - shut down part of a full-duplex connection (kernel space)
3658 * @how: connection part
3660 * Returns 0 or an error.
3663 int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how)
3665 return sock->ops->shutdown(sock, how);
3667 EXPORT_SYMBOL(kernel_sock_shutdown);
3670 * kernel_sock_ip_overhead - returns the IP overhead imposed by a socket
3673 * This routine returns the IP overhead imposed by a socket i.e.
3674 * the length of the underlying IP header, depending on whether
3675 * this is an IPv4 or IPv6 socket and the length from IP options turned
3676 * on at the socket. Assumes that the caller has a lock on the socket.
3679 u32 kernel_sock_ip_overhead(struct sock *sk)
3681 struct inet_sock *inet;
3682 struct ip_options_rcu *opt;
3684 #if IS_ENABLED(CONFIG_IPV6)
3685 struct ipv6_pinfo *np;
3686 struct ipv6_txoptions *optv6 = NULL;
3687 #endif /* IS_ENABLED(CONFIG_IPV6) */
3692 switch (sk->sk_family) {
3695 overhead += sizeof(struct iphdr);
3696 opt = rcu_dereference_protected(inet->inet_opt,
3697 sock_owned_by_user(sk));
3699 overhead += opt->opt.optlen;
3701 #if IS_ENABLED(CONFIG_IPV6)
3704 overhead += sizeof(struct ipv6hdr);
3706 optv6 = rcu_dereference_protected(np->opt,
3707 sock_owned_by_user(sk));
3709 overhead += (optv6->opt_flen + optv6->opt_nflen);
3711 #endif /* IS_ENABLED(CONFIG_IPV6) */
3712 default: /* Returns 0 overhead if the socket is not ipv4 or ipv6 */
3716 EXPORT_SYMBOL(kernel_sock_ip_overhead);