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>
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/sockios.h>
107 #include <net/busy_poll.h>
108 #include <linux/errqueue.h>
110 #ifdef CONFIG_NET_RX_BUSY_POLL
111 unsigned int sysctl_net_busy_read __read_mostly
;
112 unsigned int sysctl_net_busy_poll __read_mostly
;
115 static ssize_t
sock_read_iter(struct kiocb
*iocb
, struct iov_iter
*to
);
116 static ssize_t
sock_write_iter(struct kiocb
*iocb
, struct iov_iter
*from
);
117 static int sock_mmap(struct file
*file
, struct vm_area_struct
*vma
);
119 static int sock_close(struct inode
*inode
, struct file
*file
);
120 static struct wait_queue_head
*sock_get_poll_head(struct file
*file
,
122 static __poll_t
sock_poll_mask(struct file
*file
, __poll_t
);
123 static __poll_t
sock_poll(struct file
*file
, struct poll_table_struct
*wait
);
124 static long sock_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
);
126 static long compat_sock_ioctl(struct file
*file
,
127 unsigned int cmd
, unsigned long arg
);
129 static int sock_fasync(int fd
, struct file
*filp
, int on
);
130 static ssize_t
sock_sendpage(struct file
*file
, struct page
*page
,
131 int offset
, size_t size
, loff_t
*ppos
, int more
);
132 static ssize_t
sock_splice_read(struct file
*file
, loff_t
*ppos
,
133 struct pipe_inode_info
*pipe
, size_t len
,
137 * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
138 * in the operation structures but are done directly via the socketcall() multiplexor.
141 static const struct file_operations socket_file_ops
= {
142 .owner
= THIS_MODULE
,
144 .read_iter
= sock_read_iter
,
145 .write_iter
= sock_write_iter
,
146 .get_poll_head
= sock_get_poll_head
,
147 .poll_mask
= sock_poll_mask
,
149 .unlocked_ioctl
= sock_ioctl
,
151 .compat_ioctl
= compat_sock_ioctl
,
154 .release
= sock_close
,
155 .fasync
= sock_fasync
,
156 .sendpage
= sock_sendpage
,
157 .splice_write
= generic_splice_sendpage
,
158 .splice_read
= sock_splice_read
,
162 * The protocol list. Each protocol is registered in here.
165 static DEFINE_SPINLOCK(net_family_lock
);
166 static const struct net_proto_family __rcu
*net_families
[NPROTO
] __read_mostly
;
170 * Move socket addresses back and forth across the kernel/user
171 * divide and look after the messy bits.
175 * move_addr_to_kernel - copy a socket address into kernel space
176 * @uaddr: Address in user space
177 * @kaddr: Address in kernel space
178 * @ulen: Length in user space
180 * The address is copied into kernel space. If the provided address is
181 * too long an error code of -EINVAL is returned. If the copy gives
182 * invalid addresses -EFAULT is returned. On a success 0 is returned.
185 int move_addr_to_kernel(void __user
*uaddr
, int ulen
, struct sockaddr_storage
*kaddr
)
187 if (ulen
< 0 || ulen
> sizeof(struct sockaddr_storage
))
191 if (copy_from_user(kaddr
, uaddr
, ulen
))
193 return audit_sockaddr(ulen
, kaddr
);
197 * move_addr_to_user - copy an address to user space
198 * @kaddr: kernel space address
199 * @klen: length of address in kernel
200 * @uaddr: user space address
201 * @ulen: pointer to user length field
203 * The value pointed to by ulen on entry is the buffer length available.
204 * This is overwritten with the buffer space used. -EINVAL is returned
205 * if an overlong buffer is specified or a negative buffer size. -EFAULT
206 * is returned if either the buffer or the length field are not
208 * After copying the data up to the limit the user specifies, the true
209 * length of the data is written over the length limit the user
210 * specified. Zero is returned for a success.
213 static int move_addr_to_user(struct sockaddr_storage
*kaddr
, int klen
,
214 void __user
*uaddr
, int __user
*ulen
)
219 BUG_ON(klen
> sizeof(struct sockaddr_storage
));
220 err
= get_user(len
, ulen
);
228 if (audit_sockaddr(klen
, kaddr
))
230 if (copy_to_user(uaddr
, kaddr
, len
))
234 * "fromlen shall refer to the value before truncation.."
237 return __put_user(klen
, ulen
);
240 static struct kmem_cache
*sock_inode_cachep __ro_after_init
;
242 static struct inode
*sock_alloc_inode(struct super_block
*sb
)
244 struct socket_alloc
*ei
;
245 struct socket_wq
*wq
;
247 ei
= kmem_cache_alloc(sock_inode_cachep
, GFP_KERNEL
);
250 wq
= kmalloc(sizeof(*wq
), GFP_KERNEL
);
252 kmem_cache_free(sock_inode_cachep
, ei
);
255 init_waitqueue_head(&wq
->wait
);
256 wq
->fasync_list
= NULL
;
258 RCU_INIT_POINTER(ei
->socket
.wq
, wq
);
260 ei
->socket
.state
= SS_UNCONNECTED
;
261 ei
->socket
.flags
= 0;
262 ei
->socket
.ops
= NULL
;
263 ei
->socket
.sk
= NULL
;
264 ei
->socket
.file
= NULL
;
266 return &ei
->vfs_inode
;
269 static void sock_destroy_inode(struct inode
*inode
)
271 struct socket_alloc
*ei
;
272 struct socket_wq
*wq
;
274 ei
= container_of(inode
, struct socket_alloc
, vfs_inode
);
275 wq
= rcu_dereference_protected(ei
->socket
.wq
, 1);
277 kmem_cache_free(sock_inode_cachep
, ei
);
280 static void init_once(void *foo
)
282 struct socket_alloc
*ei
= (struct socket_alloc
*)foo
;
284 inode_init_once(&ei
->vfs_inode
);
287 static void init_inodecache(void)
289 sock_inode_cachep
= kmem_cache_create("sock_inode_cache",
290 sizeof(struct socket_alloc
),
292 (SLAB_HWCACHE_ALIGN
|
293 SLAB_RECLAIM_ACCOUNT
|
294 SLAB_MEM_SPREAD
| SLAB_ACCOUNT
),
296 BUG_ON(sock_inode_cachep
== NULL
);
299 static const struct super_operations sockfs_ops
= {
300 .alloc_inode
= sock_alloc_inode
,
301 .destroy_inode
= sock_destroy_inode
,
302 .statfs
= simple_statfs
,
306 * sockfs_dname() is called from d_path().
308 static char *sockfs_dname(struct dentry
*dentry
, char *buffer
, int buflen
)
310 return dynamic_dname(dentry
, buffer
, buflen
, "socket:[%lu]",
311 d_inode(dentry
)->i_ino
);
314 static const struct dentry_operations sockfs_dentry_operations
= {
315 .d_dname
= sockfs_dname
,
318 static int sockfs_xattr_get(const struct xattr_handler
*handler
,
319 struct dentry
*dentry
, struct inode
*inode
,
320 const char *suffix
, void *value
, size_t size
)
323 if (dentry
->d_name
.len
+ 1 > size
)
325 memcpy(value
, dentry
->d_name
.name
, dentry
->d_name
.len
+ 1);
327 return dentry
->d_name
.len
+ 1;
330 #define XATTR_SOCKPROTONAME_SUFFIX "sockprotoname"
331 #define XATTR_NAME_SOCKPROTONAME (XATTR_SYSTEM_PREFIX XATTR_SOCKPROTONAME_SUFFIX)
332 #define XATTR_NAME_SOCKPROTONAME_LEN (sizeof(XATTR_NAME_SOCKPROTONAME)-1)
334 static const struct xattr_handler sockfs_xattr_handler
= {
335 .name
= XATTR_NAME_SOCKPROTONAME
,
336 .get
= sockfs_xattr_get
,
339 static int sockfs_security_xattr_set(const struct xattr_handler
*handler
,
340 struct dentry
*dentry
, struct inode
*inode
,
341 const char *suffix
, const void *value
,
342 size_t size
, int flags
)
344 /* Handled by LSM. */
348 static const struct xattr_handler sockfs_security_xattr_handler
= {
349 .prefix
= XATTR_SECURITY_PREFIX
,
350 .set
= sockfs_security_xattr_set
,
353 static const struct xattr_handler
*sockfs_xattr_handlers
[] = {
354 &sockfs_xattr_handler
,
355 &sockfs_security_xattr_handler
,
359 static struct dentry
*sockfs_mount(struct file_system_type
*fs_type
,
360 int flags
, const char *dev_name
, void *data
)
362 return mount_pseudo_xattr(fs_type
, "socket:", &sockfs_ops
,
363 sockfs_xattr_handlers
,
364 &sockfs_dentry_operations
, SOCKFS_MAGIC
);
367 static struct vfsmount
*sock_mnt __read_mostly
;
369 static struct file_system_type sock_fs_type
= {
371 .mount
= sockfs_mount
,
372 .kill_sb
= kill_anon_super
,
376 * Obtains the first available file descriptor and sets it up for use.
378 * These functions create file structures and maps them to fd space
379 * of the current process. On success it returns file descriptor
380 * and file struct implicitly stored in sock->file.
381 * Note that another thread may close file descriptor before we return
382 * from this function. We use the fact that now we do not refer
383 * to socket after mapping. If one day we will need it, this
384 * function will increment ref. count on file by 1.
386 * In any case returned fd MAY BE not valid!
387 * This race condition is unavoidable
388 * with shared fd spaces, we cannot solve it inside kernel,
389 * but we take care of internal coherence yet.
392 struct file
*sock_alloc_file(struct socket
*sock
, int flags
, const char *dname
)
394 struct qstr name
= { .name
= "" };
400 name
.len
= strlen(name
.name
);
401 } else if (sock
->sk
) {
402 name
.name
= sock
->sk
->sk_prot_creator
->name
;
403 name
.len
= strlen(name
.name
);
405 path
.dentry
= d_alloc_pseudo(sock_mnt
->mnt_sb
, &name
);
406 if (unlikely(!path
.dentry
)) {
408 return ERR_PTR(-ENOMEM
);
410 path
.mnt
= mntget(sock_mnt
);
412 d_instantiate(path
.dentry
, SOCK_INODE(sock
));
414 file
= alloc_file(&path
, FMODE_READ
| FMODE_WRITE
,
417 /* drop dentry, keep inode for a bit */
418 ihold(d_inode(path
.dentry
));
420 /* ... and now kill it properly */
426 file
->f_flags
= O_RDWR
| (flags
& O_NONBLOCK
);
427 file
->private_data
= sock
;
430 EXPORT_SYMBOL(sock_alloc_file
);
432 static int sock_map_fd(struct socket
*sock
, int flags
)
434 struct file
*newfile
;
435 int fd
= get_unused_fd_flags(flags
);
436 if (unlikely(fd
< 0)) {
441 newfile
= sock_alloc_file(sock
, flags
, NULL
);
442 if (likely(!IS_ERR(newfile
))) {
443 fd_install(fd
, newfile
);
448 return PTR_ERR(newfile
);
451 struct socket
*sock_from_file(struct file
*file
, int *err
)
453 if (file
->f_op
== &socket_file_ops
)
454 return file
->private_data
; /* set in sock_map_fd */
459 EXPORT_SYMBOL(sock_from_file
);
462 * sockfd_lookup - Go from a file number to its socket slot
464 * @err: pointer to an error code return
466 * The file handle passed in is locked and the socket it is bound
467 * to is returned. If an error occurs the err pointer is overwritten
468 * with a negative errno code and NULL is returned. The function checks
469 * for both invalid handles and passing a handle which is not a socket.
471 * On a success the socket object pointer is returned.
474 struct socket
*sockfd_lookup(int fd
, int *err
)
485 sock
= sock_from_file(file
, err
);
490 EXPORT_SYMBOL(sockfd_lookup
);
492 static struct socket
*sockfd_lookup_light(int fd
, int *err
, int *fput_needed
)
494 struct fd f
= fdget(fd
);
499 sock
= sock_from_file(f
.file
, err
);
501 *fput_needed
= f
.flags
;
509 static ssize_t
sockfs_listxattr(struct dentry
*dentry
, char *buffer
,
515 len
= security_inode_listsecurity(d_inode(dentry
), buffer
, size
);
525 len
= (XATTR_NAME_SOCKPROTONAME_LEN
+ 1);
530 memcpy(buffer
, XATTR_NAME_SOCKPROTONAME
, len
);
537 static int sockfs_setattr(struct dentry
*dentry
, struct iattr
*iattr
)
539 int err
= simple_setattr(dentry
, iattr
);
541 if (!err
&& (iattr
->ia_valid
& ATTR_UID
)) {
542 struct socket
*sock
= SOCKET_I(d_inode(dentry
));
545 sock
->sk
->sk_uid
= iattr
->ia_uid
;
553 static const struct inode_operations sockfs_inode_ops
= {
554 .listxattr
= sockfs_listxattr
,
555 .setattr
= sockfs_setattr
,
559 * sock_alloc - allocate a socket
561 * Allocate a new inode and socket object. The two are bound together
562 * and initialised. The socket is then returned. If we are out of inodes
566 struct socket
*sock_alloc(void)
571 inode
= new_inode_pseudo(sock_mnt
->mnt_sb
);
575 sock
= SOCKET_I(inode
);
577 inode
->i_ino
= get_next_ino();
578 inode
->i_mode
= S_IFSOCK
| S_IRWXUGO
;
579 inode
->i_uid
= current_fsuid();
580 inode
->i_gid
= current_fsgid();
581 inode
->i_op
= &sockfs_inode_ops
;
585 EXPORT_SYMBOL(sock_alloc
);
588 * sock_release - close a socket
589 * @sock: socket to close
591 * The socket is released from the protocol stack if it has a release
592 * callback, and the inode is then released if the socket is bound to
593 * an inode not a file.
596 static void __sock_release(struct socket
*sock
, struct inode
*inode
)
599 struct module
*owner
= sock
->ops
->owner
;
603 sock
->ops
->release(sock
);
610 if (rcu_dereference_protected(sock
->wq
, 1)->fasync_list
)
611 pr_err("%s: fasync list not empty!\n", __func__
);
614 iput(SOCK_INODE(sock
));
620 void sock_release(struct socket
*sock
)
622 __sock_release(sock
, NULL
);
624 EXPORT_SYMBOL(sock_release
);
626 void __sock_tx_timestamp(__u16 tsflags
, __u8
*tx_flags
)
628 u8 flags
= *tx_flags
;
630 if (tsflags
& SOF_TIMESTAMPING_TX_HARDWARE
)
631 flags
|= SKBTX_HW_TSTAMP
;
633 if (tsflags
& SOF_TIMESTAMPING_TX_SOFTWARE
)
634 flags
|= SKBTX_SW_TSTAMP
;
636 if (tsflags
& SOF_TIMESTAMPING_TX_SCHED
)
637 flags
|= SKBTX_SCHED_TSTAMP
;
641 EXPORT_SYMBOL(__sock_tx_timestamp
);
643 static inline int sock_sendmsg_nosec(struct socket
*sock
, struct msghdr
*msg
)
645 int ret
= sock
->ops
->sendmsg(sock
, msg
, msg_data_left(msg
));
646 BUG_ON(ret
== -EIOCBQUEUED
);
650 int sock_sendmsg(struct socket
*sock
, struct msghdr
*msg
)
652 int err
= security_socket_sendmsg(sock
, msg
,
655 return err
?: sock_sendmsg_nosec(sock
, msg
);
657 EXPORT_SYMBOL(sock_sendmsg
);
659 int kernel_sendmsg(struct socket
*sock
, struct msghdr
*msg
,
660 struct kvec
*vec
, size_t num
, size_t size
)
662 iov_iter_kvec(&msg
->msg_iter
, WRITE
| ITER_KVEC
, vec
, num
, size
);
663 return sock_sendmsg(sock
, msg
);
665 EXPORT_SYMBOL(kernel_sendmsg
);
667 int kernel_sendmsg_locked(struct sock
*sk
, struct msghdr
*msg
,
668 struct kvec
*vec
, size_t num
, size_t size
)
670 struct socket
*sock
= sk
->sk_socket
;
672 if (!sock
->ops
->sendmsg_locked
)
673 return sock_no_sendmsg_locked(sk
, msg
, size
);
675 iov_iter_kvec(&msg
->msg_iter
, WRITE
| ITER_KVEC
, vec
, num
, size
);
677 return sock
->ops
->sendmsg_locked(sk
, msg
, msg_data_left(msg
));
679 EXPORT_SYMBOL(kernel_sendmsg_locked
);
681 static bool skb_is_err_queue(const struct sk_buff
*skb
)
683 /* pkt_type of skbs enqueued on the error queue are set to
684 * PACKET_OUTGOING in skb_set_err_queue(). This is only safe to do
685 * in recvmsg, since skbs received on a local socket will never
686 * have a pkt_type of PACKET_OUTGOING.
688 return skb
->pkt_type
== PACKET_OUTGOING
;
691 /* On transmit, software and hardware timestamps are returned independently.
692 * As the two skb clones share the hardware timestamp, which may be updated
693 * before the software timestamp is received, a hardware TX timestamp may be
694 * returned only if there is no software TX timestamp. Ignore false software
695 * timestamps, which may be made in the __sock_recv_timestamp() call when the
696 * option SO_TIMESTAMP(NS) is enabled on the socket, even when the skb has a
697 * hardware timestamp.
699 static bool skb_is_swtx_tstamp(const struct sk_buff
*skb
, int false_tstamp
)
701 return skb
->tstamp
&& !false_tstamp
&& skb_is_err_queue(skb
);
704 static void put_ts_pktinfo(struct msghdr
*msg
, struct sk_buff
*skb
)
706 struct scm_ts_pktinfo ts_pktinfo
;
707 struct net_device
*orig_dev
;
709 if (!skb_mac_header_was_set(skb
))
712 memset(&ts_pktinfo
, 0, sizeof(ts_pktinfo
));
715 orig_dev
= dev_get_by_napi_id(skb_napi_id(skb
));
717 ts_pktinfo
.if_index
= orig_dev
->ifindex
;
720 ts_pktinfo
.pkt_length
= skb
->len
- skb_mac_offset(skb
);
721 put_cmsg(msg
, SOL_SOCKET
, SCM_TIMESTAMPING_PKTINFO
,
722 sizeof(ts_pktinfo
), &ts_pktinfo
);
726 * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
728 void __sock_recv_timestamp(struct msghdr
*msg
, struct sock
*sk
,
731 int need_software_tstamp
= sock_flag(sk
, SOCK_RCVTSTAMP
);
732 struct scm_timestamping tss
;
733 int empty
= 1, false_tstamp
= 0;
734 struct skb_shared_hwtstamps
*shhwtstamps
=
737 /* Race occurred between timestamp enabling and packet
738 receiving. Fill in the current time for now. */
739 if (need_software_tstamp
&& skb
->tstamp
== 0) {
740 __net_timestamp(skb
);
744 if (need_software_tstamp
) {
745 if (!sock_flag(sk
, SOCK_RCVTSTAMPNS
)) {
747 skb_get_timestamp(skb
, &tv
);
748 put_cmsg(msg
, SOL_SOCKET
, SCM_TIMESTAMP
,
752 skb_get_timestampns(skb
, &ts
);
753 put_cmsg(msg
, SOL_SOCKET
, SCM_TIMESTAMPNS
,
758 memset(&tss
, 0, sizeof(tss
));
759 if ((sk
->sk_tsflags
& SOF_TIMESTAMPING_SOFTWARE
) &&
760 ktime_to_timespec_cond(skb
->tstamp
, tss
.ts
+ 0))
763 (sk
->sk_tsflags
& SOF_TIMESTAMPING_RAW_HARDWARE
) &&
764 !skb_is_swtx_tstamp(skb
, false_tstamp
) &&
765 ktime_to_timespec_cond(shhwtstamps
->hwtstamp
, tss
.ts
+ 2)) {
767 if ((sk
->sk_tsflags
& SOF_TIMESTAMPING_OPT_PKTINFO
) &&
768 !skb_is_err_queue(skb
))
769 put_ts_pktinfo(msg
, skb
);
772 put_cmsg(msg
, SOL_SOCKET
,
773 SCM_TIMESTAMPING
, sizeof(tss
), &tss
);
775 if (skb_is_err_queue(skb
) && skb
->len
&&
776 SKB_EXT_ERR(skb
)->opt_stats
)
777 put_cmsg(msg
, SOL_SOCKET
, SCM_TIMESTAMPING_OPT_STATS
,
778 skb
->len
, skb
->data
);
781 EXPORT_SYMBOL_GPL(__sock_recv_timestamp
);
783 void __sock_recv_wifi_status(struct msghdr
*msg
, struct sock
*sk
,
788 if (!sock_flag(sk
, SOCK_WIFI_STATUS
))
790 if (!skb
->wifi_acked_valid
)
793 ack
= skb
->wifi_acked
;
795 put_cmsg(msg
, SOL_SOCKET
, SCM_WIFI_STATUS
, sizeof(ack
), &ack
);
797 EXPORT_SYMBOL_GPL(__sock_recv_wifi_status
);
799 static inline void sock_recv_drops(struct msghdr
*msg
, struct sock
*sk
,
802 if (sock_flag(sk
, SOCK_RXQ_OVFL
) && skb
&& SOCK_SKB_CB(skb
)->dropcount
)
803 put_cmsg(msg
, SOL_SOCKET
, SO_RXQ_OVFL
,
804 sizeof(__u32
), &SOCK_SKB_CB(skb
)->dropcount
);
807 void __sock_recv_ts_and_drops(struct msghdr
*msg
, struct sock
*sk
,
810 sock_recv_timestamp(msg
, sk
, skb
);
811 sock_recv_drops(msg
, sk
, skb
);
813 EXPORT_SYMBOL_GPL(__sock_recv_ts_and_drops
);
815 static inline int sock_recvmsg_nosec(struct socket
*sock
, struct msghdr
*msg
,
818 return sock
->ops
->recvmsg(sock
, msg
, msg_data_left(msg
), flags
);
821 int sock_recvmsg(struct socket
*sock
, struct msghdr
*msg
, int flags
)
823 int err
= security_socket_recvmsg(sock
, msg
, msg_data_left(msg
), flags
);
825 return err
?: sock_recvmsg_nosec(sock
, msg
, flags
);
827 EXPORT_SYMBOL(sock_recvmsg
);
830 * kernel_recvmsg - Receive a message from a socket (kernel space)
831 * @sock: The socket to receive the message from
832 * @msg: Received message
833 * @vec: Input s/g array for message data
834 * @num: Size of input s/g array
835 * @size: Number of bytes to read
836 * @flags: Message flags (MSG_DONTWAIT, etc...)
838 * On return the msg structure contains the scatter/gather array passed in the
839 * vec argument. The array is modified so that it consists of the unfilled
840 * portion of the original array.
842 * The returned value is the total number of bytes received, or an error.
844 int kernel_recvmsg(struct socket
*sock
, struct msghdr
*msg
,
845 struct kvec
*vec
, size_t num
, size_t size
, int flags
)
847 mm_segment_t oldfs
= get_fs();
850 iov_iter_kvec(&msg
->msg_iter
, READ
| ITER_KVEC
, vec
, num
, size
);
852 result
= sock_recvmsg(sock
, msg
, flags
);
856 EXPORT_SYMBOL(kernel_recvmsg
);
858 static ssize_t
sock_sendpage(struct file
*file
, struct page
*page
,
859 int offset
, size_t size
, loff_t
*ppos
, int more
)
864 sock
= file
->private_data
;
866 flags
= (file
->f_flags
& O_NONBLOCK
) ? MSG_DONTWAIT
: 0;
867 /* more is a combination of MSG_MORE and MSG_SENDPAGE_NOTLAST */
870 return kernel_sendpage(sock
, page
, offset
, size
, flags
);
873 static ssize_t
sock_splice_read(struct file
*file
, loff_t
*ppos
,
874 struct pipe_inode_info
*pipe
, size_t len
,
877 struct socket
*sock
= file
->private_data
;
879 if (unlikely(!sock
->ops
->splice_read
))
882 return sock
->ops
->splice_read(sock
, ppos
, pipe
, len
, flags
);
885 static ssize_t
sock_read_iter(struct kiocb
*iocb
, struct iov_iter
*to
)
887 struct file
*file
= iocb
->ki_filp
;
888 struct socket
*sock
= file
->private_data
;
889 struct msghdr msg
= {.msg_iter
= *to
,
893 if (file
->f_flags
& O_NONBLOCK
)
894 msg
.msg_flags
= MSG_DONTWAIT
;
896 if (iocb
->ki_pos
!= 0)
899 if (!iov_iter_count(to
)) /* Match SYS5 behaviour */
902 res
= sock_recvmsg(sock
, &msg
, msg
.msg_flags
);
907 static ssize_t
sock_write_iter(struct kiocb
*iocb
, struct iov_iter
*from
)
909 struct file
*file
= iocb
->ki_filp
;
910 struct socket
*sock
= file
->private_data
;
911 struct msghdr msg
= {.msg_iter
= *from
,
915 if (iocb
->ki_pos
!= 0)
918 if (file
->f_flags
& O_NONBLOCK
)
919 msg
.msg_flags
= MSG_DONTWAIT
;
921 if (sock
->type
== SOCK_SEQPACKET
)
922 msg
.msg_flags
|= MSG_EOR
;
924 res
= sock_sendmsg(sock
, &msg
);
925 *from
= msg
.msg_iter
;
930 * Atomic setting of ioctl hooks to avoid race
931 * with module unload.
934 static DEFINE_MUTEX(br_ioctl_mutex
);
935 static int (*br_ioctl_hook
) (struct net
*, unsigned int cmd
, void __user
*arg
);
937 void brioctl_set(int (*hook
) (struct net
*, unsigned int, void __user
*))
939 mutex_lock(&br_ioctl_mutex
);
940 br_ioctl_hook
= hook
;
941 mutex_unlock(&br_ioctl_mutex
);
943 EXPORT_SYMBOL(brioctl_set
);
945 static DEFINE_MUTEX(vlan_ioctl_mutex
);
946 static int (*vlan_ioctl_hook
) (struct net
*, void __user
*arg
);
948 void vlan_ioctl_set(int (*hook
) (struct net
*, void __user
*))
950 mutex_lock(&vlan_ioctl_mutex
);
951 vlan_ioctl_hook
= hook
;
952 mutex_unlock(&vlan_ioctl_mutex
);
954 EXPORT_SYMBOL(vlan_ioctl_set
);
956 static DEFINE_MUTEX(dlci_ioctl_mutex
);
957 static int (*dlci_ioctl_hook
) (unsigned int, void __user
*);
959 void dlci_ioctl_set(int (*hook
) (unsigned int, void __user
*))
961 mutex_lock(&dlci_ioctl_mutex
);
962 dlci_ioctl_hook
= hook
;
963 mutex_unlock(&dlci_ioctl_mutex
);
965 EXPORT_SYMBOL(dlci_ioctl_set
);
967 static long sock_do_ioctl(struct net
*net
, struct socket
*sock
,
968 unsigned int cmd
, unsigned long arg
)
971 void __user
*argp
= (void __user
*)arg
;
973 err
= sock
->ops
->ioctl(sock
, cmd
, arg
);
976 * If this ioctl is unknown try to hand it down
979 if (err
!= -ENOIOCTLCMD
)
982 if (cmd
== SIOCGIFCONF
) {
984 if (copy_from_user(&ifc
, argp
, sizeof(struct ifconf
)))
987 err
= dev_ifconf(net
, &ifc
, sizeof(struct ifreq
));
989 if (!err
&& copy_to_user(argp
, &ifc
, sizeof(struct ifconf
)))
994 if (copy_from_user(&ifr
, argp
, sizeof(struct ifreq
)))
996 err
= dev_ioctl(net
, cmd
, &ifr
, &need_copyout
);
997 if (!err
&& need_copyout
)
998 if (copy_to_user(argp
, &ifr
, sizeof(struct ifreq
)))
1005 * With an ioctl, arg may well be a user mode pointer, but we don't know
1006 * what to do with it - that's up to the protocol still.
1009 struct ns_common
*get_net_ns(struct ns_common
*ns
)
1011 return &get_net(container_of(ns
, struct net
, ns
))->ns
;
1013 EXPORT_SYMBOL_GPL(get_net_ns
);
1015 static long sock_ioctl(struct file
*file
, unsigned cmd
, unsigned long arg
)
1017 struct socket
*sock
;
1019 void __user
*argp
= (void __user
*)arg
;
1023 sock
= file
->private_data
;
1026 if (unlikely(cmd
>= SIOCDEVPRIVATE
&& cmd
<= (SIOCDEVPRIVATE
+ 15))) {
1029 if (copy_from_user(&ifr
, argp
, sizeof(struct ifreq
)))
1031 err
= dev_ioctl(net
, cmd
, &ifr
, &need_copyout
);
1032 if (!err
&& need_copyout
)
1033 if (copy_to_user(argp
, &ifr
, sizeof(struct ifreq
)))
1036 #ifdef CONFIG_WEXT_CORE
1037 if (cmd
>= SIOCIWFIRST
&& cmd
<= SIOCIWLAST
) {
1038 err
= wext_handle_ioctl(net
, cmd
, argp
);
1045 if (get_user(pid
, (int __user
*)argp
))
1047 err
= f_setown(sock
->file
, pid
, 1);
1051 err
= put_user(f_getown(sock
->file
),
1052 (int __user
*)argp
);
1060 request_module("bridge");
1062 mutex_lock(&br_ioctl_mutex
);
1064 err
= br_ioctl_hook(net
, cmd
, argp
);
1065 mutex_unlock(&br_ioctl_mutex
);
1070 if (!vlan_ioctl_hook
)
1071 request_module("8021q");
1073 mutex_lock(&vlan_ioctl_mutex
);
1074 if (vlan_ioctl_hook
)
1075 err
= vlan_ioctl_hook(net
, argp
);
1076 mutex_unlock(&vlan_ioctl_mutex
);
1081 if (!dlci_ioctl_hook
)
1082 request_module("dlci");
1084 mutex_lock(&dlci_ioctl_mutex
);
1085 if (dlci_ioctl_hook
)
1086 err
= dlci_ioctl_hook(cmd
, argp
);
1087 mutex_unlock(&dlci_ioctl_mutex
);
1091 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
1094 err
= open_related_ns(&net
->ns
, get_net_ns
);
1097 err
= sock_do_ioctl(net
, sock
, cmd
, arg
);
1103 int sock_create_lite(int family
, int type
, int protocol
, struct socket
**res
)
1106 struct socket
*sock
= NULL
;
1108 err
= security_socket_create(family
, type
, protocol
, 1);
1112 sock
= sock_alloc();
1119 err
= security_socket_post_create(sock
, family
, type
, protocol
, 1);
1131 EXPORT_SYMBOL(sock_create_lite
);
1133 static struct wait_queue_head
*sock_get_poll_head(struct file
*file
,
1136 struct socket
*sock
= file
->private_data
;
1138 if (!sock
->ops
->poll_mask
)
1140 sock_poll_busy_loop(sock
, events
);
1141 return sk_sleep(sock
->sk
);
1144 static __poll_t
sock_poll_mask(struct file
*file
, __poll_t events
)
1146 struct socket
*sock
= file
->private_data
;
1149 * We need to be sure we are in sync with the socket flags modification.
1151 * This memory barrier is paired in the wq_has_sleeper.
1155 /* this socket can poll_ll so tell the system call */
1156 return sock
->ops
->poll_mask(sock
, events
) |
1157 (sk_can_busy_loop(sock
->sk
) ? POLL_BUSY_LOOP
: 0);
1160 /* No kernel lock held - perfect */
1161 static __poll_t
sock_poll(struct file
*file
, poll_table
*wait
)
1163 struct socket
*sock
= file
->private_data
;
1164 __poll_t events
= poll_requested_events(wait
), mask
= 0;
1166 if (sock
->ops
->poll
) {
1167 sock_poll_busy_loop(sock
, events
);
1168 mask
= sock
->ops
->poll(file
, sock
, wait
);
1169 } else if (sock
->ops
->poll_mask
) {
1170 sock_poll_wait(file
, sock_get_poll_head(file
, events
), wait
);
1171 mask
= sock
->ops
->poll_mask(sock
, events
);
1174 return mask
| sock_poll_busy_flag(sock
);
1177 static int sock_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1179 struct socket
*sock
= file
->private_data
;
1181 return sock
->ops
->mmap(file
, sock
, vma
);
1184 static int sock_close(struct inode
*inode
, struct file
*filp
)
1186 __sock_release(SOCKET_I(inode
), inode
);
1191 * Update the socket async list
1193 * Fasync_list locking strategy.
1195 * 1. fasync_list is modified only under process context socket lock
1196 * i.e. under semaphore.
1197 * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
1198 * or under socket lock
1201 static int sock_fasync(int fd
, struct file
*filp
, int on
)
1203 struct socket
*sock
= filp
->private_data
;
1204 struct sock
*sk
= sock
->sk
;
1205 struct socket_wq
*wq
;
1211 wq
= rcu_dereference_protected(sock
->wq
, lockdep_sock_is_held(sk
));
1212 fasync_helper(fd
, filp
, on
, &wq
->fasync_list
);
1214 if (!wq
->fasync_list
)
1215 sock_reset_flag(sk
, SOCK_FASYNC
);
1217 sock_set_flag(sk
, SOCK_FASYNC
);
1223 /* This function may be called only under rcu_lock */
1225 int sock_wake_async(struct socket_wq
*wq
, int how
, int band
)
1227 if (!wq
|| !wq
->fasync_list
)
1231 case SOCK_WAKE_WAITD
:
1232 if (test_bit(SOCKWQ_ASYNC_WAITDATA
, &wq
->flags
))
1235 case SOCK_WAKE_SPACE
:
1236 if (!test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE
, &wq
->flags
))
1241 kill_fasync(&wq
->fasync_list
, SIGIO
, band
);
1244 kill_fasync(&wq
->fasync_list
, SIGURG
, band
);
1249 EXPORT_SYMBOL(sock_wake_async
);
1251 int __sock_create(struct net
*net
, int family
, int type
, int protocol
,
1252 struct socket
**res
, int kern
)
1255 struct socket
*sock
;
1256 const struct net_proto_family
*pf
;
1259 * Check protocol is in range
1261 if (family
< 0 || family
>= NPROTO
)
1262 return -EAFNOSUPPORT
;
1263 if (type
< 0 || type
>= SOCK_MAX
)
1268 This uglymoron is moved from INET layer to here to avoid
1269 deadlock in module load.
1271 if (family
== PF_INET
&& type
== SOCK_PACKET
) {
1272 pr_info_once("%s uses obsolete (PF_INET,SOCK_PACKET)\n",
1277 err
= security_socket_create(family
, type
, protocol
, kern
);
1282 * Allocate the socket and allow the family to set things up. if
1283 * the protocol is 0, the family is instructed to select an appropriate
1286 sock
= sock_alloc();
1288 net_warn_ratelimited("socket: no more sockets\n");
1289 return -ENFILE
; /* Not exactly a match, but its the
1290 closest posix thing */
1295 #ifdef CONFIG_MODULES
1296 /* Attempt to load a protocol module if the find failed.
1298 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
1299 * requested real, full-featured networking support upon configuration.
1300 * Otherwise module support will break!
1302 if (rcu_access_pointer(net_families
[family
]) == NULL
)
1303 request_module("net-pf-%d", family
);
1307 pf
= rcu_dereference(net_families
[family
]);
1308 err
= -EAFNOSUPPORT
;
1313 * We will call the ->create function, that possibly is in a loadable
1314 * module, so we have to bump that loadable module refcnt first.
1316 if (!try_module_get(pf
->owner
))
1319 /* Now protected by module ref count */
1322 err
= pf
->create(net
, sock
, protocol
, kern
);
1324 goto out_module_put
;
1327 * Now to bump the refcnt of the [loadable] module that owns this
1328 * socket at sock_release time we decrement its refcnt.
1330 if (!try_module_get(sock
->ops
->owner
))
1331 goto out_module_busy
;
1334 * Now that we're done with the ->create function, the [loadable]
1335 * module can have its refcnt decremented
1337 module_put(pf
->owner
);
1338 err
= security_socket_post_create(sock
, family
, type
, protocol
, kern
);
1340 goto out_sock_release
;
1346 err
= -EAFNOSUPPORT
;
1349 module_put(pf
->owner
);
1356 goto out_sock_release
;
1358 EXPORT_SYMBOL(__sock_create
);
1360 int sock_create(int family
, int type
, int protocol
, struct socket
**res
)
1362 return __sock_create(current
->nsproxy
->net_ns
, family
, type
, protocol
, res
, 0);
1364 EXPORT_SYMBOL(sock_create
);
1366 int sock_create_kern(struct net
*net
, int family
, int type
, int protocol
, struct socket
**res
)
1368 return __sock_create(net
, family
, type
, protocol
, res
, 1);
1370 EXPORT_SYMBOL(sock_create_kern
);
1372 int __sys_socket(int family
, int type
, int protocol
)
1375 struct socket
*sock
;
1378 /* Check the SOCK_* constants for consistency. */
1379 BUILD_BUG_ON(SOCK_CLOEXEC
!= O_CLOEXEC
);
1380 BUILD_BUG_ON((SOCK_MAX
| SOCK_TYPE_MASK
) != SOCK_TYPE_MASK
);
1381 BUILD_BUG_ON(SOCK_CLOEXEC
& SOCK_TYPE_MASK
);
1382 BUILD_BUG_ON(SOCK_NONBLOCK
& SOCK_TYPE_MASK
);
1384 flags
= type
& ~SOCK_TYPE_MASK
;
1385 if (flags
& ~(SOCK_CLOEXEC
| SOCK_NONBLOCK
))
1387 type
&= SOCK_TYPE_MASK
;
1389 if (SOCK_NONBLOCK
!= O_NONBLOCK
&& (flags
& SOCK_NONBLOCK
))
1390 flags
= (flags
& ~SOCK_NONBLOCK
) | O_NONBLOCK
;
1392 retval
= sock_create(family
, type
, protocol
, &sock
);
1396 return sock_map_fd(sock
, flags
& (O_CLOEXEC
| O_NONBLOCK
));
1399 SYSCALL_DEFINE3(socket
, int, family
, int, type
, int, protocol
)
1401 return __sys_socket(family
, type
, protocol
);
1405 * Create a pair of connected sockets.
1408 int __sys_socketpair(int family
, int type
, int protocol
, int __user
*usockvec
)
1410 struct socket
*sock1
, *sock2
;
1412 struct file
*newfile1
, *newfile2
;
1415 flags
= type
& ~SOCK_TYPE_MASK
;
1416 if (flags
& ~(SOCK_CLOEXEC
| SOCK_NONBLOCK
))
1418 type
&= SOCK_TYPE_MASK
;
1420 if (SOCK_NONBLOCK
!= O_NONBLOCK
&& (flags
& SOCK_NONBLOCK
))
1421 flags
= (flags
& ~SOCK_NONBLOCK
) | O_NONBLOCK
;
1424 * reserve descriptors and make sure we won't fail
1425 * to return them to userland.
1427 fd1
= get_unused_fd_flags(flags
);
1428 if (unlikely(fd1
< 0))
1431 fd2
= get_unused_fd_flags(flags
);
1432 if (unlikely(fd2
< 0)) {
1437 err
= put_user(fd1
, &usockvec
[0]);
1441 err
= put_user(fd2
, &usockvec
[1]);
1446 * Obtain the first socket and check if the underlying protocol
1447 * supports the socketpair call.
1450 err
= sock_create(family
, type
, protocol
, &sock1
);
1451 if (unlikely(err
< 0))
1454 err
= sock_create(family
, type
, protocol
, &sock2
);
1455 if (unlikely(err
< 0)) {
1456 sock_release(sock1
);
1460 err
= security_socket_socketpair(sock1
, sock2
);
1461 if (unlikely(err
)) {
1462 sock_release(sock2
);
1463 sock_release(sock1
);
1467 err
= sock1
->ops
->socketpair(sock1
, sock2
);
1468 if (unlikely(err
< 0)) {
1469 sock_release(sock2
);
1470 sock_release(sock1
);
1474 newfile1
= sock_alloc_file(sock1
, flags
, NULL
);
1475 if (IS_ERR(newfile1
)) {
1476 err
= PTR_ERR(newfile1
);
1477 sock_release(sock2
);
1481 newfile2
= sock_alloc_file(sock2
, flags
, NULL
);
1482 if (IS_ERR(newfile2
)) {
1483 err
= PTR_ERR(newfile2
);
1488 audit_fd_pair(fd1
, fd2
);
1490 fd_install(fd1
, newfile1
);
1491 fd_install(fd2
, newfile2
);
1500 SYSCALL_DEFINE4(socketpair
, int, family
, int, type
, int, protocol
,
1501 int __user
*, usockvec
)
1503 return __sys_socketpair(family
, type
, protocol
, usockvec
);
1507 * Bind a name to a socket. Nothing much to do here since it's
1508 * the protocol's responsibility to handle the local address.
1510 * We move the socket address to kernel space before we call
1511 * the protocol layer (having also checked the address is ok).
1514 int __sys_bind(int fd
, struct sockaddr __user
*umyaddr
, int addrlen
)
1516 struct socket
*sock
;
1517 struct sockaddr_storage address
;
1518 int err
, fput_needed
;
1520 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1522 err
= move_addr_to_kernel(umyaddr
, addrlen
, &address
);
1524 err
= security_socket_bind(sock
,
1525 (struct sockaddr
*)&address
,
1528 err
= sock
->ops
->bind(sock
,
1532 fput_light(sock
->file
, fput_needed
);
1537 SYSCALL_DEFINE3(bind
, int, fd
, struct sockaddr __user
*, umyaddr
, int, addrlen
)
1539 return __sys_bind(fd
, umyaddr
, addrlen
);
1543 * Perform a listen. Basically, we allow the protocol to do anything
1544 * necessary for a listen, and if that works, we mark the socket as
1545 * ready for listening.
1548 int __sys_listen(int fd
, int backlog
)
1550 struct socket
*sock
;
1551 int err
, fput_needed
;
1554 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1556 somaxconn
= sock_net(sock
->sk
)->core
.sysctl_somaxconn
;
1557 if ((unsigned int)backlog
> somaxconn
)
1558 backlog
= somaxconn
;
1560 err
= security_socket_listen(sock
, backlog
);
1562 err
= sock
->ops
->listen(sock
, backlog
);
1564 fput_light(sock
->file
, fput_needed
);
1569 SYSCALL_DEFINE2(listen
, int, fd
, int, backlog
)
1571 return __sys_listen(fd
, backlog
);
1575 * For accept, we attempt to create a new socket, set up the link
1576 * with the client, wake up the client, then return the new
1577 * connected fd. We collect the address of the connector in kernel
1578 * space and move it to user at the very end. This is unclean because
1579 * we open the socket then return an error.
1581 * 1003.1g adds the ability to recvmsg() to query connection pending
1582 * status to recvmsg. We need to add that support in a way thats
1583 * clean when we restructure accept also.
1586 int __sys_accept4(int fd
, struct sockaddr __user
*upeer_sockaddr
,
1587 int __user
*upeer_addrlen
, int flags
)
1589 struct socket
*sock
, *newsock
;
1590 struct file
*newfile
;
1591 int err
, len
, newfd
, fput_needed
;
1592 struct sockaddr_storage address
;
1594 if (flags
& ~(SOCK_CLOEXEC
| SOCK_NONBLOCK
))
1597 if (SOCK_NONBLOCK
!= O_NONBLOCK
&& (flags
& SOCK_NONBLOCK
))
1598 flags
= (flags
& ~SOCK_NONBLOCK
) | O_NONBLOCK
;
1600 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1605 newsock
= sock_alloc();
1609 newsock
->type
= sock
->type
;
1610 newsock
->ops
= sock
->ops
;
1613 * We don't need try_module_get here, as the listening socket (sock)
1614 * has the protocol module (sock->ops->owner) held.
1616 __module_get(newsock
->ops
->owner
);
1618 newfd
= get_unused_fd_flags(flags
);
1619 if (unlikely(newfd
< 0)) {
1621 sock_release(newsock
);
1624 newfile
= sock_alloc_file(newsock
, flags
, sock
->sk
->sk_prot_creator
->name
);
1625 if (IS_ERR(newfile
)) {
1626 err
= PTR_ERR(newfile
);
1627 put_unused_fd(newfd
);
1631 err
= security_socket_accept(sock
, newsock
);
1635 err
= sock
->ops
->accept(sock
, newsock
, sock
->file
->f_flags
, false);
1639 if (upeer_sockaddr
) {
1640 len
= newsock
->ops
->getname(newsock
,
1641 (struct sockaddr
*)&address
, 2);
1643 err
= -ECONNABORTED
;
1646 err
= move_addr_to_user(&address
,
1647 len
, upeer_sockaddr
, upeer_addrlen
);
1652 /* File flags are not inherited via accept() unlike another OSes. */
1654 fd_install(newfd
, newfile
);
1658 fput_light(sock
->file
, fput_needed
);
1663 put_unused_fd(newfd
);
1667 SYSCALL_DEFINE4(accept4
, int, fd
, struct sockaddr __user
*, upeer_sockaddr
,
1668 int __user
*, upeer_addrlen
, int, flags
)
1670 return __sys_accept4(fd
, upeer_sockaddr
, upeer_addrlen
, flags
);
1673 SYSCALL_DEFINE3(accept
, int, fd
, struct sockaddr __user
*, upeer_sockaddr
,
1674 int __user
*, upeer_addrlen
)
1676 return __sys_accept4(fd
, upeer_sockaddr
, upeer_addrlen
, 0);
1680 * Attempt to connect to a socket with the server address. The address
1681 * is in user space so we verify it is OK and move it to kernel space.
1683 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
1686 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
1687 * other SEQPACKET protocols that take time to connect() as it doesn't
1688 * include the -EINPROGRESS status for such sockets.
1691 int __sys_connect(int fd
, struct sockaddr __user
*uservaddr
, int addrlen
)
1693 struct socket
*sock
;
1694 struct sockaddr_storage address
;
1695 int err
, fput_needed
;
1697 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1700 err
= move_addr_to_kernel(uservaddr
, addrlen
, &address
);
1705 security_socket_connect(sock
, (struct sockaddr
*)&address
, addrlen
);
1709 err
= sock
->ops
->connect(sock
, (struct sockaddr
*)&address
, addrlen
,
1710 sock
->file
->f_flags
);
1712 fput_light(sock
->file
, fput_needed
);
1717 SYSCALL_DEFINE3(connect
, int, fd
, struct sockaddr __user
*, uservaddr
,
1720 return __sys_connect(fd
, uservaddr
, addrlen
);
1724 * Get the local address ('name') of a socket object. Move the obtained
1725 * name to user space.
1728 int __sys_getsockname(int fd
, struct sockaddr __user
*usockaddr
,
1729 int __user
*usockaddr_len
)
1731 struct socket
*sock
;
1732 struct sockaddr_storage address
;
1733 int err
, fput_needed
;
1735 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1739 err
= security_socket_getsockname(sock
);
1743 err
= sock
->ops
->getname(sock
, (struct sockaddr
*)&address
, 0);
1746 /* "err" is actually length in this case */
1747 err
= move_addr_to_user(&address
, err
, usockaddr
, usockaddr_len
);
1750 fput_light(sock
->file
, fput_needed
);
1755 SYSCALL_DEFINE3(getsockname
, int, fd
, struct sockaddr __user
*, usockaddr
,
1756 int __user
*, usockaddr_len
)
1758 return __sys_getsockname(fd
, usockaddr
, usockaddr_len
);
1762 * Get the remote address ('name') of a socket object. Move the obtained
1763 * name to user space.
1766 int __sys_getpeername(int fd
, struct sockaddr __user
*usockaddr
,
1767 int __user
*usockaddr_len
)
1769 struct socket
*sock
;
1770 struct sockaddr_storage address
;
1771 int err
, fput_needed
;
1773 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1775 err
= security_socket_getpeername(sock
);
1777 fput_light(sock
->file
, fput_needed
);
1781 err
= sock
->ops
->getname(sock
, (struct sockaddr
*)&address
, 1);
1783 /* "err" is actually length in this case */
1784 err
= move_addr_to_user(&address
, err
, usockaddr
,
1786 fput_light(sock
->file
, fput_needed
);
1791 SYSCALL_DEFINE3(getpeername
, int, fd
, struct sockaddr __user
*, usockaddr
,
1792 int __user
*, usockaddr_len
)
1794 return __sys_getpeername(fd
, usockaddr
, usockaddr_len
);
1798 * Send a datagram to a given address. We move the address into kernel
1799 * space and check the user space data area is readable before invoking
1802 int __sys_sendto(int fd
, void __user
*buff
, size_t len
, unsigned int flags
,
1803 struct sockaddr __user
*addr
, int addr_len
)
1805 struct socket
*sock
;
1806 struct sockaddr_storage address
;
1812 err
= import_single_range(WRITE
, buff
, len
, &iov
, &msg
.msg_iter
);
1815 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1819 msg
.msg_name
= NULL
;
1820 msg
.msg_control
= NULL
;
1821 msg
.msg_controllen
= 0;
1822 msg
.msg_namelen
= 0;
1824 err
= move_addr_to_kernel(addr
, addr_len
, &address
);
1827 msg
.msg_name
= (struct sockaddr
*)&address
;
1828 msg
.msg_namelen
= addr_len
;
1830 if (sock
->file
->f_flags
& O_NONBLOCK
)
1831 flags
|= MSG_DONTWAIT
;
1832 msg
.msg_flags
= flags
;
1833 err
= sock_sendmsg(sock
, &msg
);
1836 fput_light(sock
->file
, fput_needed
);
1841 SYSCALL_DEFINE6(sendto
, int, fd
, void __user
*, buff
, size_t, len
,
1842 unsigned int, flags
, struct sockaddr __user
*, addr
,
1845 return __sys_sendto(fd
, buff
, len
, flags
, addr
, addr_len
);
1849 * Send a datagram down a socket.
1852 SYSCALL_DEFINE4(send
, int, fd
, void __user
*, buff
, size_t, len
,
1853 unsigned int, flags
)
1855 return __sys_sendto(fd
, buff
, len
, flags
, NULL
, 0);
1859 * Receive a frame from the socket and optionally record the address of the
1860 * sender. We verify the buffers are writable and if needed move the
1861 * sender address from kernel to user space.
1863 int __sys_recvfrom(int fd
, void __user
*ubuf
, size_t size
, unsigned int flags
,
1864 struct sockaddr __user
*addr
, int __user
*addr_len
)
1866 struct socket
*sock
;
1869 struct sockaddr_storage address
;
1873 err
= import_single_range(READ
, ubuf
, size
, &iov
, &msg
.msg_iter
);
1876 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1880 msg
.msg_control
= NULL
;
1881 msg
.msg_controllen
= 0;
1882 /* Save some cycles and don't copy the address if not needed */
1883 msg
.msg_name
= addr
? (struct sockaddr
*)&address
: NULL
;
1884 /* We assume all kernel code knows the size of sockaddr_storage */
1885 msg
.msg_namelen
= 0;
1886 msg
.msg_iocb
= NULL
;
1888 if (sock
->file
->f_flags
& O_NONBLOCK
)
1889 flags
|= MSG_DONTWAIT
;
1890 err
= sock_recvmsg(sock
, &msg
, flags
);
1892 if (err
>= 0 && addr
!= NULL
) {
1893 err2
= move_addr_to_user(&address
,
1894 msg
.msg_namelen
, addr
, addr_len
);
1899 fput_light(sock
->file
, fput_needed
);
1904 SYSCALL_DEFINE6(recvfrom
, int, fd
, void __user
*, ubuf
, size_t, size
,
1905 unsigned int, flags
, struct sockaddr __user
*, addr
,
1906 int __user
*, addr_len
)
1908 return __sys_recvfrom(fd
, ubuf
, size
, flags
, addr
, addr_len
);
1912 * Receive a datagram from a socket.
1915 SYSCALL_DEFINE4(recv
, int, fd
, void __user
*, ubuf
, size_t, size
,
1916 unsigned int, flags
)
1918 return __sys_recvfrom(fd
, ubuf
, size
, flags
, NULL
, NULL
);
1922 * Set a socket option. Because we don't know the option lengths we have
1923 * to pass the user mode parameter for the protocols to sort out.
1926 static int __sys_setsockopt(int fd
, int level
, int optname
,
1927 char __user
*optval
, int optlen
)
1929 int err
, fput_needed
;
1930 struct socket
*sock
;
1935 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1937 err
= security_socket_setsockopt(sock
, level
, optname
);
1941 if (level
== SOL_SOCKET
)
1943 sock_setsockopt(sock
, level
, optname
, optval
,
1947 sock
->ops
->setsockopt(sock
, level
, optname
, optval
,
1950 fput_light(sock
->file
, fput_needed
);
1955 SYSCALL_DEFINE5(setsockopt
, int, fd
, int, level
, int, optname
,
1956 char __user
*, optval
, int, optlen
)
1958 return __sys_setsockopt(fd
, level
, optname
, optval
, optlen
);
1962 * Get a socket option. Because we don't know the option lengths we have
1963 * to pass a user mode parameter for the protocols to sort out.
1966 static int __sys_getsockopt(int fd
, int level
, int optname
,
1967 char __user
*optval
, int __user
*optlen
)
1969 int err
, fput_needed
;
1970 struct socket
*sock
;
1972 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1974 err
= security_socket_getsockopt(sock
, level
, optname
);
1978 if (level
== SOL_SOCKET
)
1980 sock_getsockopt(sock
, level
, optname
, optval
,
1984 sock
->ops
->getsockopt(sock
, level
, optname
, optval
,
1987 fput_light(sock
->file
, fput_needed
);
1992 SYSCALL_DEFINE5(getsockopt
, int, fd
, int, level
, int, optname
,
1993 char __user
*, optval
, int __user
*, optlen
)
1995 return __sys_getsockopt(fd
, level
, optname
, optval
, optlen
);
1999 * Shutdown a socket.
2002 int __sys_shutdown(int fd
, int how
)
2004 int err
, fput_needed
;
2005 struct socket
*sock
;
2007 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
2009 err
= security_socket_shutdown(sock
, how
);
2011 err
= sock
->ops
->shutdown(sock
, how
);
2012 fput_light(sock
->file
, fput_needed
);
2017 SYSCALL_DEFINE2(shutdown
, int, fd
, int, how
)
2019 return __sys_shutdown(fd
, how
);
2022 /* A couple of helpful macros for getting the address of the 32/64 bit
2023 * fields which are the same type (int / unsigned) on our platforms.
2025 #define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
2026 #define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
2027 #define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
2029 struct used_address
{
2030 struct sockaddr_storage name
;
2031 unsigned int name_len
;
2034 static int copy_msghdr_from_user(struct msghdr
*kmsg
,
2035 struct user_msghdr __user
*umsg
,
2036 struct sockaddr __user
**save_addr
,
2039 struct user_msghdr msg
;
2042 if (copy_from_user(&msg
, umsg
, sizeof(*umsg
)))
2045 kmsg
->msg_control
= (void __force
*)msg
.msg_control
;
2046 kmsg
->msg_controllen
= msg
.msg_controllen
;
2047 kmsg
->msg_flags
= msg
.msg_flags
;
2049 kmsg
->msg_namelen
= msg
.msg_namelen
;
2051 kmsg
->msg_namelen
= 0;
2053 if (kmsg
->msg_namelen
< 0)
2056 if (kmsg
->msg_namelen
> sizeof(struct sockaddr_storage
))
2057 kmsg
->msg_namelen
= sizeof(struct sockaddr_storage
);
2060 *save_addr
= msg
.msg_name
;
2062 if (msg
.msg_name
&& kmsg
->msg_namelen
) {
2064 err
= move_addr_to_kernel(msg
.msg_name
,
2071 kmsg
->msg_name
= NULL
;
2072 kmsg
->msg_namelen
= 0;
2075 if (msg
.msg_iovlen
> UIO_MAXIOV
)
2078 kmsg
->msg_iocb
= NULL
;
2080 return import_iovec(save_addr
? READ
: WRITE
,
2081 msg
.msg_iov
, msg
.msg_iovlen
,
2082 UIO_FASTIOV
, iov
, &kmsg
->msg_iter
);
2085 static int ___sys_sendmsg(struct socket
*sock
, struct user_msghdr __user
*msg
,
2086 struct msghdr
*msg_sys
, unsigned int flags
,
2087 struct used_address
*used_address
,
2088 unsigned int allowed_msghdr_flags
)
2090 struct compat_msghdr __user
*msg_compat
=
2091 (struct compat_msghdr __user
*)msg
;
2092 struct sockaddr_storage address
;
2093 struct iovec iovstack
[UIO_FASTIOV
], *iov
= iovstack
;
2094 unsigned char ctl
[sizeof(struct cmsghdr
) + 20]
2095 __aligned(sizeof(__kernel_size_t
));
2096 /* 20 is size of ipv6_pktinfo */
2097 unsigned char *ctl_buf
= ctl
;
2101 msg_sys
->msg_name
= &address
;
2103 if (MSG_CMSG_COMPAT
& flags
)
2104 err
= get_compat_msghdr(msg_sys
, msg_compat
, NULL
, &iov
);
2106 err
= copy_msghdr_from_user(msg_sys
, msg
, NULL
, &iov
);
2112 if (msg_sys
->msg_controllen
> INT_MAX
)
2114 flags
|= (msg_sys
->msg_flags
& allowed_msghdr_flags
);
2115 ctl_len
= msg_sys
->msg_controllen
;
2116 if ((MSG_CMSG_COMPAT
& flags
) && ctl_len
) {
2118 cmsghdr_from_user_compat_to_kern(msg_sys
, sock
->sk
, ctl
,
2122 ctl_buf
= msg_sys
->msg_control
;
2123 ctl_len
= msg_sys
->msg_controllen
;
2124 } else if (ctl_len
) {
2125 BUILD_BUG_ON(sizeof(struct cmsghdr
) !=
2126 CMSG_ALIGN(sizeof(struct cmsghdr
)));
2127 if (ctl_len
> sizeof(ctl
)) {
2128 ctl_buf
= sock_kmalloc(sock
->sk
, ctl_len
, GFP_KERNEL
);
2129 if (ctl_buf
== NULL
)
2134 * Careful! Before this, msg_sys->msg_control contains a user pointer.
2135 * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
2136 * checking falls down on this.
2138 if (copy_from_user(ctl_buf
,
2139 (void __user __force
*)msg_sys
->msg_control
,
2142 msg_sys
->msg_control
= ctl_buf
;
2144 msg_sys
->msg_flags
= flags
;
2146 if (sock
->file
->f_flags
& O_NONBLOCK
)
2147 msg_sys
->msg_flags
|= MSG_DONTWAIT
;
2149 * If this is sendmmsg() and current destination address is same as
2150 * previously succeeded address, omit asking LSM's decision.
2151 * used_address->name_len is initialized to UINT_MAX so that the first
2152 * destination address never matches.
2154 if (used_address
&& msg_sys
->msg_name
&&
2155 used_address
->name_len
== msg_sys
->msg_namelen
&&
2156 !memcmp(&used_address
->name
, msg_sys
->msg_name
,
2157 used_address
->name_len
)) {
2158 err
= sock_sendmsg_nosec(sock
, msg_sys
);
2161 err
= sock_sendmsg(sock
, msg_sys
);
2163 * If this is sendmmsg() and sending to current destination address was
2164 * successful, remember it.
2166 if (used_address
&& err
>= 0) {
2167 used_address
->name_len
= msg_sys
->msg_namelen
;
2168 if (msg_sys
->msg_name
)
2169 memcpy(&used_address
->name
, msg_sys
->msg_name
,
2170 used_address
->name_len
);
2175 sock_kfree_s(sock
->sk
, ctl_buf
, ctl_len
);
2182 * BSD sendmsg interface
2185 long __sys_sendmsg(int fd
, struct user_msghdr __user
*msg
, unsigned int flags
,
2186 bool forbid_cmsg_compat
)
2188 int fput_needed
, err
;
2189 struct msghdr msg_sys
;
2190 struct socket
*sock
;
2192 if (forbid_cmsg_compat
&& (flags
& MSG_CMSG_COMPAT
))
2195 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
2199 err
= ___sys_sendmsg(sock
, msg
, &msg_sys
, flags
, NULL
, 0);
2201 fput_light(sock
->file
, fput_needed
);
2206 SYSCALL_DEFINE3(sendmsg
, int, fd
, struct user_msghdr __user
*, msg
, unsigned int, flags
)
2208 return __sys_sendmsg(fd
, msg
, flags
, true);
2212 * Linux sendmmsg interface
2215 int __sys_sendmmsg(int fd
, struct mmsghdr __user
*mmsg
, unsigned int vlen
,
2216 unsigned int flags
, bool forbid_cmsg_compat
)
2218 int fput_needed
, err
, datagrams
;
2219 struct socket
*sock
;
2220 struct mmsghdr __user
*entry
;
2221 struct compat_mmsghdr __user
*compat_entry
;
2222 struct msghdr msg_sys
;
2223 struct used_address used_address
;
2224 unsigned int oflags
= flags
;
2226 if (forbid_cmsg_compat
&& (flags
& MSG_CMSG_COMPAT
))
2229 if (vlen
> UIO_MAXIOV
)
2234 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
2238 used_address
.name_len
= UINT_MAX
;
2240 compat_entry
= (struct compat_mmsghdr __user
*)mmsg
;
2244 while (datagrams
< vlen
) {
2245 if (datagrams
== vlen
- 1)
2248 if (MSG_CMSG_COMPAT
& flags
) {
2249 err
= ___sys_sendmsg(sock
, (struct user_msghdr __user
*)compat_entry
,
2250 &msg_sys
, flags
, &used_address
, MSG_EOR
);
2253 err
= __put_user(err
, &compat_entry
->msg_len
);
2256 err
= ___sys_sendmsg(sock
,
2257 (struct user_msghdr __user
*)entry
,
2258 &msg_sys
, flags
, &used_address
, MSG_EOR
);
2261 err
= put_user(err
, &entry
->msg_len
);
2268 if (msg_data_left(&msg_sys
))
2273 fput_light(sock
->file
, fput_needed
);
2275 /* We only return an error if no datagrams were able to be sent */
2282 SYSCALL_DEFINE4(sendmmsg
, int, fd
, struct mmsghdr __user
*, mmsg
,
2283 unsigned int, vlen
, unsigned int, flags
)
2285 return __sys_sendmmsg(fd
, mmsg
, vlen
, flags
, true);
2288 static int ___sys_recvmsg(struct socket
*sock
, struct user_msghdr __user
*msg
,
2289 struct msghdr
*msg_sys
, unsigned int flags
, int nosec
)
2291 struct compat_msghdr __user
*msg_compat
=
2292 (struct compat_msghdr __user
*)msg
;
2293 struct iovec iovstack
[UIO_FASTIOV
];
2294 struct iovec
*iov
= iovstack
;
2295 unsigned long cmsg_ptr
;
2299 /* kernel mode address */
2300 struct sockaddr_storage addr
;
2302 /* user mode address pointers */
2303 struct sockaddr __user
*uaddr
;
2304 int __user
*uaddr_len
= COMPAT_NAMELEN(msg
);
2306 msg_sys
->msg_name
= &addr
;
2308 if (MSG_CMSG_COMPAT
& flags
)
2309 err
= get_compat_msghdr(msg_sys
, msg_compat
, &uaddr
, &iov
);
2311 err
= copy_msghdr_from_user(msg_sys
, msg
, &uaddr
, &iov
);
2315 cmsg_ptr
= (unsigned long)msg_sys
->msg_control
;
2316 msg_sys
->msg_flags
= flags
& (MSG_CMSG_CLOEXEC
|MSG_CMSG_COMPAT
);
2318 /* We assume all kernel code knows the size of sockaddr_storage */
2319 msg_sys
->msg_namelen
= 0;
2321 if (sock
->file
->f_flags
& O_NONBLOCK
)
2322 flags
|= MSG_DONTWAIT
;
2323 err
= (nosec
? sock_recvmsg_nosec
: sock_recvmsg
)(sock
, msg_sys
, flags
);
2328 if (uaddr
!= NULL
) {
2329 err
= move_addr_to_user(&addr
,
2330 msg_sys
->msg_namelen
, uaddr
,
2335 err
= __put_user((msg_sys
->msg_flags
& ~MSG_CMSG_COMPAT
),
2339 if (MSG_CMSG_COMPAT
& flags
)
2340 err
= __put_user((unsigned long)msg_sys
->msg_control
- cmsg_ptr
,
2341 &msg_compat
->msg_controllen
);
2343 err
= __put_user((unsigned long)msg_sys
->msg_control
- cmsg_ptr
,
2344 &msg
->msg_controllen
);
2355 * BSD recvmsg interface
2358 long __sys_recvmsg(int fd
, struct user_msghdr __user
*msg
, unsigned int flags
,
2359 bool forbid_cmsg_compat
)
2361 int fput_needed
, err
;
2362 struct msghdr msg_sys
;
2363 struct socket
*sock
;
2365 if (forbid_cmsg_compat
&& (flags
& MSG_CMSG_COMPAT
))
2368 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
2372 err
= ___sys_recvmsg(sock
, msg
, &msg_sys
, flags
, 0);
2374 fput_light(sock
->file
, fput_needed
);
2379 SYSCALL_DEFINE3(recvmsg
, int, fd
, struct user_msghdr __user
*, msg
,
2380 unsigned int, flags
)
2382 return __sys_recvmsg(fd
, msg
, flags
, true);
2386 * Linux recvmmsg interface
2389 int __sys_recvmmsg(int fd
, struct mmsghdr __user
*mmsg
, unsigned int vlen
,
2390 unsigned int flags
, struct timespec
*timeout
)
2392 int fput_needed
, err
, datagrams
;
2393 struct socket
*sock
;
2394 struct mmsghdr __user
*entry
;
2395 struct compat_mmsghdr __user
*compat_entry
;
2396 struct msghdr msg_sys
;
2397 struct timespec64 end_time
;
2398 struct timespec64 timeout64
;
2401 poll_select_set_timeout(&end_time
, timeout
->tv_sec
,
2407 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
2411 if (likely(!(flags
& MSG_ERRQUEUE
))) {
2412 err
= sock_error(sock
->sk
);
2420 compat_entry
= (struct compat_mmsghdr __user
*)mmsg
;
2422 while (datagrams
< vlen
) {
2424 * No need to ask LSM for more than the first datagram.
2426 if (MSG_CMSG_COMPAT
& flags
) {
2427 err
= ___sys_recvmsg(sock
, (struct user_msghdr __user
*)compat_entry
,
2428 &msg_sys
, flags
& ~MSG_WAITFORONE
,
2432 err
= __put_user(err
, &compat_entry
->msg_len
);
2435 err
= ___sys_recvmsg(sock
,
2436 (struct user_msghdr __user
*)entry
,
2437 &msg_sys
, flags
& ~MSG_WAITFORONE
,
2441 err
= put_user(err
, &entry
->msg_len
);
2449 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
2450 if (flags
& MSG_WAITFORONE
)
2451 flags
|= MSG_DONTWAIT
;
2454 ktime_get_ts64(&timeout64
);
2455 *timeout
= timespec64_to_timespec(
2456 timespec64_sub(end_time
, timeout64
));
2457 if (timeout
->tv_sec
< 0) {
2458 timeout
->tv_sec
= timeout
->tv_nsec
= 0;
2462 /* Timeout, return less than vlen datagrams */
2463 if (timeout
->tv_nsec
== 0 && timeout
->tv_sec
== 0)
2467 /* Out of band data, return right away */
2468 if (msg_sys
.msg_flags
& MSG_OOB
)
2476 if (datagrams
== 0) {
2482 * We may return less entries than requested (vlen) if the
2483 * sock is non block and there aren't enough datagrams...
2485 if (err
!= -EAGAIN
) {
2487 * ... or if recvmsg returns an error after we
2488 * received some datagrams, where we record the
2489 * error to return on the next call or if the
2490 * app asks about it using getsockopt(SO_ERROR).
2492 sock
->sk
->sk_err
= -err
;
2495 fput_light(sock
->file
, fput_needed
);
2500 static int do_sys_recvmmsg(int fd
, struct mmsghdr __user
*mmsg
,
2501 unsigned int vlen
, unsigned int flags
,
2502 struct timespec __user
*timeout
)
2505 struct timespec timeout_sys
;
2507 if (flags
& MSG_CMSG_COMPAT
)
2511 return __sys_recvmmsg(fd
, mmsg
, vlen
, flags
, NULL
);
2513 if (copy_from_user(&timeout_sys
, timeout
, sizeof(timeout_sys
)))
2516 datagrams
= __sys_recvmmsg(fd
, mmsg
, vlen
, flags
, &timeout_sys
);
2518 if (datagrams
> 0 &&
2519 copy_to_user(timeout
, &timeout_sys
, sizeof(timeout_sys
)))
2520 datagrams
= -EFAULT
;
2525 SYSCALL_DEFINE5(recvmmsg
, int, fd
, struct mmsghdr __user
*, mmsg
,
2526 unsigned int, vlen
, unsigned int, flags
,
2527 struct timespec __user
*, timeout
)
2529 return do_sys_recvmmsg(fd
, mmsg
, vlen
, flags
, timeout
);
2532 #ifdef __ARCH_WANT_SYS_SOCKETCALL
2533 /* Argument list sizes for sys_socketcall */
2534 #define AL(x) ((x) * sizeof(unsigned long))
2535 static const unsigned char nargs
[21] = {
2536 AL(0), AL(3), AL(3), AL(3), AL(2), AL(3),
2537 AL(3), AL(3), AL(4), AL(4), AL(4), AL(6),
2538 AL(6), AL(2), AL(5), AL(5), AL(3), AL(3),
2545 * System call vectors.
2547 * Argument checking cleaned up. Saved 20% in size.
2548 * This function doesn't need to set the kernel lock because
2549 * it is set by the callees.
2552 SYSCALL_DEFINE2(socketcall
, int, call
, unsigned long __user
*, args
)
2554 unsigned long a
[AUDITSC_ARGS
];
2555 unsigned long a0
, a1
;
2559 if (call
< 1 || call
> SYS_SENDMMSG
)
2563 if (len
> sizeof(a
))
2566 /* copy_from_user should be SMP safe. */
2567 if (copy_from_user(a
, args
, len
))
2570 err
= audit_socketcall(nargs
[call
] / sizeof(unsigned long), a
);
2579 err
= __sys_socket(a0
, a1
, a
[2]);
2582 err
= __sys_bind(a0
, (struct sockaddr __user
*)a1
, a
[2]);
2585 err
= __sys_connect(a0
, (struct sockaddr __user
*)a1
, a
[2]);
2588 err
= __sys_listen(a0
, a1
);
2591 err
= __sys_accept4(a0
, (struct sockaddr __user
*)a1
,
2592 (int __user
*)a
[2], 0);
2594 case SYS_GETSOCKNAME
:
2596 __sys_getsockname(a0
, (struct sockaddr __user
*)a1
,
2597 (int __user
*)a
[2]);
2599 case SYS_GETPEERNAME
:
2601 __sys_getpeername(a0
, (struct sockaddr __user
*)a1
,
2602 (int __user
*)a
[2]);
2604 case SYS_SOCKETPAIR
:
2605 err
= __sys_socketpair(a0
, a1
, a
[2], (int __user
*)a
[3]);
2608 err
= __sys_sendto(a0
, (void __user
*)a1
, a
[2], a
[3],
2612 err
= __sys_sendto(a0
, (void __user
*)a1
, a
[2], a
[3],
2613 (struct sockaddr __user
*)a
[4], a
[5]);
2616 err
= __sys_recvfrom(a0
, (void __user
*)a1
, a
[2], a
[3],
2620 err
= __sys_recvfrom(a0
, (void __user
*)a1
, a
[2], a
[3],
2621 (struct sockaddr __user
*)a
[4],
2622 (int __user
*)a
[5]);
2625 err
= __sys_shutdown(a0
, a1
);
2627 case SYS_SETSOCKOPT
:
2628 err
= __sys_setsockopt(a0
, a1
, a
[2], (char __user
*)a
[3],
2631 case SYS_GETSOCKOPT
:
2633 __sys_getsockopt(a0
, a1
, a
[2], (char __user
*)a
[3],
2634 (int __user
*)a
[4]);
2637 err
= __sys_sendmsg(a0
, (struct user_msghdr __user
*)a1
,
2641 err
= __sys_sendmmsg(a0
, (struct mmsghdr __user
*)a1
, a
[2],
2645 err
= __sys_recvmsg(a0
, (struct user_msghdr __user
*)a1
,
2649 err
= do_sys_recvmmsg(a0
, (struct mmsghdr __user
*)a1
, a
[2],
2650 a
[3], (struct timespec __user
*)a
[4]);
2653 err
= __sys_accept4(a0
, (struct sockaddr __user
*)a1
,
2654 (int __user
*)a
[2], a
[3]);
2663 #endif /* __ARCH_WANT_SYS_SOCKETCALL */
2666 * sock_register - add a socket protocol handler
2667 * @ops: description of protocol
2669 * This function is called by a protocol handler that wants to
2670 * advertise its address family, and have it linked into the
2671 * socket interface. The value ops->family corresponds to the
2672 * socket system call protocol family.
2674 int sock_register(const struct net_proto_family
*ops
)
2678 if (ops
->family
>= NPROTO
) {
2679 pr_crit("protocol %d >= NPROTO(%d)\n", ops
->family
, NPROTO
);
2683 spin_lock(&net_family_lock
);
2684 if (rcu_dereference_protected(net_families
[ops
->family
],
2685 lockdep_is_held(&net_family_lock
)))
2688 rcu_assign_pointer(net_families
[ops
->family
], ops
);
2691 spin_unlock(&net_family_lock
);
2693 pr_info("NET: Registered protocol family %d\n", ops
->family
);
2696 EXPORT_SYMBOL(sock_register
);
2699 * sock_unregister - remove a protocol handler
2700 * @family: protocol family to remove
2702 * This function is called by a protocol handler that wants to
2703 * remove its address family, and have it unlinked from the
2704 * new socket creation.
2706 * If protocol handler is a module, then it can use module reference
2707 * counts to protect against new references. If protocol handler is not
2708 * a module then it needs to provide its own protection in
2709 * the ops->create routine.
2711 void sock_unregister(int family
)
2713 BUG_ON(family
< 0 || family
>= NPROTO
);
2715 spin_lock(&net_family_lock
);
2716 RCU_INIT_POINTER(net_families
[family
], NULL
);
2717 spin_unlock(&net_family_lock
);
2721 pr_info("NET: Unregistered protocol family %d\n", family
);
2723 EXPORT_SYMBOL(sock_unregister
);
2725 bool sock_is_registered(int family
)
2727 return family
< NPROTO
&& rcu_access_pointer(net_families
[family
]);
2730 static int __init
sock_init(void)
2734 * Initialize the network sysctl infrastructure.
2736 err
= net_sysctl_init();
2741 * Initialize skbuff SLAB cache
2746 * Initialize the protocols module.
2751 err
= register_filesystem(&sock_fs_type
);
2754 sock_mnt
= kern_mount(&sock_fs_type
);
2755 if (IS_ERR(sock_mnt
)) {
2756 err
= PTR_ERR(sock_mnt
);
2760 /* The real protocol initialization is performed in later initcalls.
2763 #ifdef CONFIG_NETFILTER
2764 err
= netfilter_init();
2769 ptp_classifier_init();
2775 unregister_filesystem(&sock_fs_type
);
2780 core_initcall(sock_init
); /* early initcall */
2782 #ifdef CONFIG_PROC_FS
2783 void socket_seq_show(struct seq_file
*seq
)
2785 seq_printf(seq
, "sockets: used %d\n",
2786 sock_inuse_get(seq
->private));
2788 #endif /* CONFIG_PROC_FS */
2790 #ifdef CONFIG_COMPAT
2791 static int do_siocgstamp(struct net
*net
, struct socket
*sock
,
2792 unsigned int cmd
, void __user
*up
)
2794 mm_segment_t old_fs
= get_fs();
2799 err
= sock_do_ioctl(net
, sock
, cmd
, (unsigned long)&ktv
);
2802 err
= compat_put_timeval(&ktv
, up
);
2807 static int do_siocgstampns(struct net
*net
, struct socket
*sock
,
2808 unsigned int cmd
, void __user
*up
)
2810 mm_segment_t old_fs
= get_fs();
2811 struct timespec kts
;
2815 err
= sock_do_ioctl(net
, sock
, cmd
, (unsigned long)&kts
);
2818 err
= compat_put_timespec(&kts
, up
);
2823 static int compat_dev_ifconf(struct net
*net
, struct compat_ifconf __user
*uifc32
)
2825 struct compat_ifconf ifc32
;
2829 if (copy_from_user(&ifc32
, uifc32
, sizeof(struct compat_ifconf
)))
2832 ifc
.ifc_len
= ifc32
.ifc_len
;
2833 ifc
.ifc_req
= compat_ptr(ifc32
.ifcbuf
);
2836 err
= dev_ifconf(net
, &ifc
, sizeof(struct compat_ifreq
));
2841 ifc32
.ifc_len
= ifc
.ifc_len
;
2842 if (copy_to_user(uifc32
, &ifc32
, sizeof(struct compat_ifconf
)))
2848 static int ethtool_ioctl(struct net
*net
, struct compat_ifreq __user
*ifr32
)
2850 struct compat_ethtool_rxnfc __user
*compat_rxnfc
;
2851 bool convert_in
= false, convert_out
= false;
2852 size_t buf_size
= 0;
2853 struct ethtool_rxnfc __user
*rxnfc
= NULL
;
2855 u32 rule_cnt
= 0, actual_rule_cnt
;
2860 if (get_user(data
, &ifr32
->ifr_ifru
.ifru_data
))
2863 compat_rxnfc
= compat_ptr(data
);
2865 if (get_user(ethcmd
, &compat_rxnfc
->cmd
))
2868 /* Most ethtool structures are defined without padding.
2869 * Unfortunately struct ethtool_rxnfc is an exception.
2874 case ETHTOOL_GRXCLSRLALL
:
2875 /* Buffer size is variable */
2876 if (get_user(rule_cnt
, &compat_rxnfc
->rule_cnt
))
2878 if (rule_cnt
> KMALLOC_MAX_SIZE
/ sizeof(u32
))
2880 buf_size
+= rule_cnt
* sizeof(u32
);
2882 case ETHTOOL_GRXRINGS
:
2883 case ETHTOOL_GRXCLSRLCNT
:
2884 case ETHTOOL_GRXCLSRULE
:
2885 case ETHTOOL_SRXCLSRLINS
:
2888 case ETHTOOL_SRXCLSRLDEL
:
2889 buf_size
+= sizeof(struct ethtool_rxnfc
);
2891 rxnfc
= compat_alloc_user_space(buf_size
);
2895 if (copy_from_user(&ifr
.ifr_name
, &ifr32
->ifr_name
, IFNAMSIZ
))
2898 ifr
.ifr_data
= convert_in
? rxnfc
: (void __user
*)compat_rxnfc
;
2901 /* We expect there to be holes between fs.m_ext and
2902 * fs.ring_cookie and at the end of fs, but nowhere else.
2904 BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc
, fs
.m_ext
) +
2905 sizeof(compat_rxnfc
->fs
.m_ext
) !=
2906 offsetof(struct ethtool_rxnfc
, fs
.m_ext
) +
2907 sizeof(rxnfc
->fs
.m_ext
));
2909 offsetof(struct compat_ethtool_rxnfc
, fs
.location
) -
2910 offsetof(struct compat_ethtool_rxnfc
, fs
.ring_cookie
) !=
2911 offsetof(struct ethtool_rxnfc
, fs
.location
) -
2912 offsetof(struct ethtool_rxnfc
, fs
.ring_cookie
));
2914 if (copy_in_user(rxnfc
, compat_rxnfc
,
2915 (void __user
*)(&rxnfc
->fs
.m_ext
+ 1) -
2916 (void __user
*)rxnfc
) ||
2917 copy_in_user(&rxnfc
->fs
.ring_cookie
,
2918 &compat_rxnfc
->fs
.ring_cookie
,
2919 (void __user
*)(&rxnfc
->fs
.location
+ 1) -
2920 (void __user
*)&rxnfc
->fs
.ring_cookie
) ||
2921 copy_in_user(&rxnfc
->rule_cnt
, &compat_rxnfc
->rule_cnt
,
2922 sizeof(rxnfc
->rule_cnt
)))
2926 ret
= dev_ioctl(net
, SIOCETHTOOL
, &ifr
, NULL
);
2931 if (copy_in_user(compat_rxnfc
, rxnfc
,
2932 (const void __user
*)(&rxnfc
->fs
.m_ext
+ 1) -
2933 (const void __user
*)rxnfc
) ||
2934 copy_in_user(&compat_rxnfc
->fs
.ring_cookie
,
2935 &rxnfc
->fs
.ring_cookie
,
2936 (const void __user
*)(&rxnfc
->fs
.location
+ 1) -
2937 (const void __user
*)&rxnfc
->fs
.ring_cookie
) ||
2938 copy_in_user(&compat_rxnfc
->rule_cnt
, &rxnfc
->rule_cnt
,
2939 sizeof(rxnfc
->rule_cnt
)))
2942 if (ethcmd
== ETHTOOL_GRXCLSRLALL
) {
2943 /* As an optimisation, we only copy the actual
2944 * number of rules that the underlying
2945 * function returned. Since Mallory might
2946 * change the rule count in user memory, we
2947 * check that it is less than the rule count
2948 * originally given (as the user buffer size),
2949 * which has been range-checked.
2951 if (get_user(actual_rule_cnt
, &rxnfc
->rule_cnt
))
2953 if (actual_rule_cnt
< rule_cnt
)
2954 rule_cnt
= actual_rule_cnt
;
2955 if (copy_in_user(&compat_rxnfc
->rule_locs
[0],
2956 &rxnfc
->rule_locs
[0],
2957 rule_cnt
* sizeof(u32
)))
2965 static int compat_siocwandev(struct net
*net
, struct compat_ifreq __user
*uifr32
)
2967 compat_uptr_t uptr32
;
2972 if (copy_from_user(&ifr
, uifr32
, sizeof(struct compat_ifreq
)))
2975 if (get_user(uptr32
, &uifr32
->ifr_settings
.ifs_ifsu
))
2978 saved
= ifr
.ifr_settings
.ifs_ifsu
.raw_hdlc
;
2979 ifr
.ifr_settings
.ifs_ifsu
.raw_hdlc
= compat_ptr(uptr32
);
2981 err
= dev_ioctl(net
, SIOCWANDEV
, &ifr
, NULL
);
2983 ifr
.ifr_settings
.ifs_ifsu
.raw_hdlc
= saved
;
2984 if (copy_to_user(uifr32
, &ifr
, sizeof(struct compat_ifreq
)))
2990 /* Handle ioctls that use ifreq::ifr_data and just need struct ifreq converted */
2991 static int compat_ifr_data_ioctl(struct net
*net
, unsigned int cmd
,
2992 struct compat_ifreq __user
*u_ifreq32
)
2997 if (copy_from_user(ifreq
.ifr_name
, u_ifreq32
->ifr_name
, IFNAMSIZ
))
2999 if (get_user(data32
, &u_ifreq32
->ifr_data
))
3001 ifreq
.ifr_data
= compat_ptr(data32
);
3003 return dev_ioctl(net
, cmd
, &ifreq
, NULL
);
3006 static int compat_sioc_ifmap(struct net
*net
, unsigned int cmd
,
3007 struct compat_ifreq __user
*uifr32
)
3010 struct compat_ifmap __user
*uifmap32
;
3013 uifmap32
= &uifr32
->ifr_ifru
.ifru_map
;
3014 err
= copy_from_user(&ifr
, uifr32
, sizeof(ifr
.ifr_name
));
3015 err
|= get_user(ifr
.ifr_map
.mem_start
, &uifmap32
->mem_start
);
3016 err
|= get_user(ifr
.ifr_map
.mem_end
, &uifmap32
->mem_end
);
3017 err
|= get_user(ifr
.ifr_map
.base_addr
, &uifmap32
->base_addr
);
3018 err
|= get_user(ifr
.ifr_map
.irq
, &uifmap32
->irq
);
3019 err
|= get_user(ifr
.ifr_map
.dma
, &uifmap32
->dma
);
3020 err
|= get_user(ifr
.ifr_map
.port
, &uifmap32
->port
);
3024 err
= dev_ioctl(net
, cmd
, &ifr
, NULL
);
3026 if (cmd
== SIOCGIFMAP
&& !err
) {
3027 err
= copy_to_user(uifr32
, &ifr
, sizeof(ifr
.ifr_name
));
3028 err
|= put_user(ifr
.ifr_map
.mem_start
, &uifmap32
->mem_start
);
3029 err
|= put_user(ifr
.ifr_map
.mem_end
, &uifmap32
->mem_end
);
3030 err
|= put_user(ifr
.ifr_map
.base_addr
, &uifmap32
->base_addr
);
3031 err
|= put_user(ifr
.ifr_map
.irq
, &uifmap32
->irq
);
3032 err
|= put_user(ifr
.ifr_map
.dma
, &uifmap32
->dma
);
3033 err
|= put_user(ifr
.ifr_map
.port
, &uifmap32
->port
);
3042 struct sockaddr rt_dst
; /* target address */
3043 struct sockaddr rt_gateway
; /* gateway addr (RTF_GATEWAY) */
3044 struct sockaddr rt_genmask
; /* target network mask (IP) */
3045 unsigned short rt_flags
;
3048 unsigned char rt_tos
;
3049 unsigned char rt_class
;
3051 short rt_metric
; /* +1 for binary compatibility! */
3052 /* char * */ u32 rt_dev
; /* forcing the device at add */
3053 u32 rt_mtu
; /* per route MTU/Window */
3054 u32 rt_window
; /* Window clamping */
3055 unsigned short rt_irtt
; /* Initial RTT */
3058 struct in6_rtmsg32
{
3059 struct in6_addr rtmsg_dst
;
3060 struct in6_addr rtmsg_src
;
3061 struct in6_addr rtmsg_gateway
;
3071 static int routing_ioctl(struct net
*net
, struct socket
*sock
,
3072 unsigned int cmd
, void __user
*argp
)
3076 struct in6_rtmsg r6
;
3080 mm_segment_t old_fs
= get_fs();
3082 if (sock
&& sock
->sk
&& sock
->sk
->sk_family
== AF_INET6
) { /* ipv6 */
3083 struct in6_rtmsg32 __user
*ur6
= argp
;
3084 ret
= copy_from_user(&r6
.rtmsg_dst
, &(ur6
->rtmsg_dst
),
3085 3 * sizeof(struct in6_addr
));
3086 ret
|= get_user(r6
.rtmsg_type
, &(ur6
->rtmsg_type
));
3087 ret
|= get_user(r6
.rtmsg_dst_len
, &(ur6
->rtmsg_dst_len
));
3088 ret
|= get_user(r6
.rtmsg_src_len
, &(ur6
->rtmsg_src_len
));
3089 ret
|= get_user(r6
.rtmsg_metric
, &(ur6
->rtmsg_metric
));
3090 ret
|= get_user(r6
.rtmsg_info
, &(ur6
->rtmsg_info
));
3091 ret
|= get_user(r6
.rtmsg_flags
, &(ur6
->rtmsg_flags
));
3092 ret
|= get_user(r6
.rtmsg_ifindex
, &(ur6
->rtmsg_ifindex
));
3096 struct rtentry32 __user
*ur4
= argp
;
3097 ret
= copy_from_user(&r4
.rt_dst
, &(ur4
->rt_dst
),
3098 3 * sizeof(struct sockaddr
));
3099 ret
|= get_user(r4
.rt_flags
, &(ur4
->rt_flags
));
3100 ret
|= get_user(r4
.rt_metric
, &(ur4
->rt_metric
));
3101 ret
|= get_user(r4
.rt_mtu
, &(ur4
->rt_mtu
));
3102 ret
|= get_user(r4
.rt_window
, &(ur4
->rt_window
));
3103 ret
|= get_user(r4
.rt_irtt
, &(ur4
->rt_irtt
));
3104 ret
|= get_user(rtdev
, &(ur4
->rt_dev
));
3106 ret
|= copy_from_user(devname
, compat_ptr(rtdev
), 15);
3107 r4
.rt_dev
= (char __user __force
*)devname
;
3121 ret
= sock_do_ioctl(net
, sock
, cmd
, (unsigned long) r
);
3128 /* Since old style bridge ioctl's endup using SIOCDEVPRIVATE
3129 * for some operations; this forces use of the newer bridge-utils that
3130 * use compatible ioctls
3132 static int old_bridge_ioctl(compat_ulong_t __user
*argp
)
3136 if (get_user(tmp
, argp
))
3138 if (tmp
== BRCTL_GET_VERSION
)
3139 return BRCTL_VERSION
+ 1;
3143 static int compat_sock_ioctl_trans(struct file
*file
, struct socket
*sock
,
3144 unsigned int cmd
, unsigned long arg
)
3146 void __user
*argp
= compat_ptr(arg
);
3147 struct sock
*sk
= sock
->sk
;
3148 struct net
*net
= sock_net(sk
);
3150 if (cmd
>= SIOCDEVPRIVATE
&& cmd
<= (SIOCDEVPRIVATE
+ 15))
3151 return compat_ifr_data_ioctl(net
, cmd
, argp
);
3156 return old_bridge_ioctl(argp
);
3158 return compat_dev_ifconf(net
, argp
);
3160 return ethtool_ioctl(net
, argp
);
3162 return compat_siocwandev(net
, argp
);
3165 return compat_sioc_ifmap(net
, cmd
, argp
);
3168 return routing_ioctl(net
, sock
, cmd
, argp
);
3170 return do_siocgstamp(net
, sock
, cmd
, argp
);
3172 return do_siocgstampns(net
, sock
, cmd
, argp
);
3173 case SIOCBONDSLAVEINFOQUERY
:
3174 case SIOCBONDINFOQUERY
:
3177 return compat_ifr_data_ioctl(net
, cmd
, argp
);
3190 return sock_ioctl(file
, cmd
, arg
);
3207 case SIOCSIFHWBROADCAST
:
3209 case SIOCGIFBRDADDR
:
3210 case SIOCSIFBRDADDR
:
3211 case SIOCGIFDSTADDR
:
3212 case SIOCSIFDSTADDR
:
3213 case SIOCGIFNETMASK
:
3214 case SIOCSIFNETMASK
:
3229 case SIOCBONDENSLAVE
:
3230 case SIOCBONDRELEASE
:
3231 case SIOCBONDSETHWADDR
:
3232 case SIOCBONDCHANGEACTIVE
:
3234 return sock_do_ioctl(net
, sock
, cmd
, arg
);
3237 return -ENOIOCTLCMD
;
3240 static long compat_sock_ioctl(struct file
*file
, unsigned int cmd
,
3243 struct socket
*sock
= file
->private_data
;
3244 int ret
= -ENOIOCTLCMD
;
3251 if (sock
->ops
->compat_ioctl
)
3252 ret
= sock
->ops
->compat_ioctl(sock
, cmd
, arg
);
3254 if (ret
== -ENOIOCTLCMD
&&
3255 (cmd
>= SIOCIWFIRST
&& cmd
<= SIOCIWLAST
))
3256 ret
= compat_wext_handle_ioctl(net
, cmd
, arg
);
3258 if (ret
== -ENOIOCTLCMD
)
3259 ret
= compat_sock_ioctl_trans(file
, sock
, cmd
, arg
);
3265 int kernel_bind(struct socket
*sock
, struct sockaddr
*addr
, int addrlen
)
3267 return sock
->ops
->bind(sock
, addr
, addrlen
);
3269 EXPORT_SYMBOL(kernel_bind
);
3271 int kernel_listen(struct socket
*sock
, int backlog
)
3273 return sock
->ops
->listen(sock
, backlog
);
3275 EXPORT_SYMBOL(kernel_listen
);
3277 int kernel_accept(struct socket
*sock
, struct socket
**newsock
, int flags
)
3279 struct sock
*sk
= sock
->sk
;
3282 err
= sock_create_lite(sk
->sk_family
, sk
->sk_type
, sk
->sk_protocol
,
3287 err
= sock
->ops
->accept(sock
, *newsock
, flags
, true);
3289 sock_release(*newsock
);
3294 (*newsock
)->ops
= sock
->ops
;
3295 __module_get((*newsock
)->ops
->owner
);
3300 EXPORT_SYMBOL(kernel_accept
);
3302 int kernel_connect(struct socket
*sock
, struct sockaddr
*addr
, int addrlen
,
3305 return sock
->ops
->connect(sock
, addr
, addrlen
, flags
);
3307 EXPORT_SYMBOL(kernel_connect
);
3309 int kernel_getsockname(struct socket
*sock
, struct sockaddr
*addr
)
3311 return sock
->ops
->getname(sock
, addr
, 0);
3313 EXPORT_SYMBOL(kernel_getsockname
);
3315 int kernel_getpeername(struct socket
*sock
, struct sockaddr
*addr
)
3317 return sock
->ops
->getname(sock
, addr
, 1);
3319 EXPORT_SYMBOL(kernel_getpeername
);
3321 int kernel_getsockopt(struct socket
*sock
, int level
, int optname
,
3322 char *optval
, int *optlen
)
3324 mm_segment_t oldfs
= get_fs();
3325 char __user
*uoptval
;
3326 int __user
*uoptlen
;
3329 uoptval
= (char __user __force
*) optval
;
3330 uoptlen
= (int __user __force
*) optlen
;
3333 if (level
== SOL_SOCKET
)
3334 err
= sock_getsockopt(sock
, level
, optname
, uoptval
, uoptlen
);
3336 err
= sock
->ops
->getsockopt(sock
, level
, optname
, uoptval
,
3341 EXPORT_SYMBOL(kernel_getsockopt
);
3343 int kernel_setsockopt(struct socket
*sock
, int level
, int optname
,
3344 char *optval
, unsigned int optlen
)
3346 mm_segment_t oldfs
= get_fs();
3347 char __user
*uoptval
;
3350 uoptval
= (char __user __force
*) optval
;
3353 if (level
== SOL_SOCKET
)
3354 err
= sock_setsockopt(sock
, level
, optname
, uoptval
, optlen
);
3356 err
= sock
->ops
->setsockopt(sock
, level
, optname
, uoptval
,
3361 EXPORT_SYMBOL(kernel_setsockopt
);
3363 int kernel_sendpage(struct socket
*sock
, struct page
*page
, int offset
,
3364 size_t size
, int flags
)
3366 if (sock
->ops
->sendpage
)
3367 return sock
->ops
->sendpage(sock
, page
, offset
, size
, flags
);
3369 return sock_no_sendpage(sock
, page
, offset
, size
, flags
);
3371 EXPORT_SYMBOL(kernel_sendpage
);
3373 int kernel_sendpage_locked(struct sock
*sk
, struct page
*page
, int offset
,
3374 size_t size
, int flags
)
3376 struct socket
*sock
= sk
->sk_socket
;
3378 if (sock
->ops
->sendpage_locked
)
3379 return sock
->ops
->sendpage_locked(sk
, page
, offset
, size
,
3382 return sock_no_sendpage_locked(sk
, page
, offset
, size
, flags
);
3384 EXPORT_SYMBOL(kernel_sendpage_locked
);
3386 int kernel_sock_shutdown(struct socket
*sock
, enum sock_shutdown_cmd how
)
3388 return sock
->ops
->shutdown(sock
, how
);
3390 EXPORT_SYMBOL(kernel_sock_shutdown
);
3392 /* This routine returns the IP overhead imposed by a socket i.e.
3393 * the length of the underlying IP header, depending on whether
3394 * this is an IPv4 or IPv6 socket and the length from IP options turned
3395 * on at the socket. Assumes that the caller has a lock on the socket.
3397 u32
kernel_sock_ip_overhead(struct sock
*sk
)
3399 struct inet_sock
*inet
;
3400 struct ip_options_rcu
*opt
;
3402 #if IS_ENABLED(CONFIG_IPV6)
3403 struct ipv6_pinfo
*np
;
3404 struct ipv6_txoptions
*optv6
= NULL
;
3405 #endif /* IS_ENABLED(CONFIG_IPV6) */
3410 switch (sk
->sk_family
) {
3413 overhead
+= sizeof(struct iphdr
);
3414 opt
= rcu_dereference_protected(inet
->inet_opt
,
3415 sock_owned_by_user(sk
));
3417 overhead
+= opt
->opt
.optlen
;
3419 #if IS_ENABLED(CONFIG_IPV6)
3422 overhead
+= sizeof(struct ipv6hdr
);
3424 optv6
= rcu_dereference_protected(np
->opt
,
3425 sock_owned_by_user(sk
));
3427 overhead
+= (optv6
->opt_flen
+ optv6
->opt_nflen
);
3429 #endif /* IS_ENABLED(CONFIG_IPV6) */
3430 default: /* Returns 0 overhead if the socket is not ipv4 or ipv6 */
3434 EXPORT_SYMBOL(kernel_sock_ip_overhead
);