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>
94 #include <linux/uaccess.h>
95 #include <asm/unistd.h>
97 #include <net/compat.h>
99 #include <net/cls_cgroup.h>
101 #include <net/sock.h>
102 #include <linux/netfilter.h>
104 #include <linux/if_tun.h>
105 #include <linux/ipv6_route.h>
106 #include <linux/route.h>
107 #include <linux/sockios.h>
108 #include <net/busy_poll.h>
109 #include <linux/errqueue.h>
111 #ifdef CONFIG_NET_RX_BUSY_POLL
112 unsigned int sysctl_net_busy_read __read_mostly
;
113 unsigned int sysctl_net_busy_poll __read_mostly
;
116 static ssize_t
sock_read_iter(struct kiocb
*iocb
, struct iov_iter
*to
);
117 static ssize_t
sock_write_iter(struct kiocb
*iocb
, struct iov_iter
*from
);
118 static int sock_mmap(struct file
*file
, struct vm_area_struct
*vma
);
120 static int sock_close(struct inode
*inode
, struct file
*file
);
121 static __poll_t
sock_poll(struct file
*file
,
122 struct poll_table_struct
*wait
);
123 static long sock_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
);
125 static long compat_sock_ioctl(struct file
*file
,
126 unsigned int cmd
, unsigned long arg
);
128 static int sock_fasync(int fd
, struct file
*filp
, int on
);
129 static ssize_t
sock_sendpage(struct file
*file
, struct page
*page
,
130 int offset
, size_t size
, loff_t
*ppos
, int more
);
131 static ssize_t
sock_splice_read(struct file
*file
, loff_t
*ppos
,
132 struct pipe_inode_info
*pipe
, size_t len
,
136 * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
137 * in the operation structures but are done directly via the socketcall() multiplexor.
140 static const struct file_operations socket_file_ops
= {
141 .owner
= THIS_MODULE
,
143 .read_iter
= sock_read_iter
,
144 .write_iter
= sock_write_iter
,
146 .unlocked_ioctl
= sock_ioctl
,
148 .compat_ioctl
= compat_sock_ioctl
,
151 .release
= sock_close
,
152 .fasync
= sock_fasync
,
153 .sendpage
= sock_sendpage
,
154 .splice_write
= generic_splice_sendpage
,
155 .splice_read
= sock_splice_read
,
159 * The protocol list. Each protocol is registered in here.
162 static DEFINE_SPINLOCK(net_family_lock
);
163 static const struct net_proto_family __rcu
*net_families
[NPROTO
] __read_mostly
;
167 * Move socket addresses back and forth across the kernel/user
168 * divide and look after the messy bits.
172 * move_addr_to_kernel - copy a socket address into kernel space
173 * @uaddr: Address in user space
174 * @kaddr: Address in kernel space
175 * @ulen: Length in user space
177 * The address is copied into kernel space. If the provided address is
178 * too long an error code of -EINVAL is returned. If the copy gives
179 * invalid addresses -EFAULT is returned. On a success 0 is returned.
182 int move_addr_to_kernel(void __user
*uaddr
, int ulen
, struct sockaddr_storage
*kaddr
)
184 if (ulen
< 0 || ulen
> sizeof(struct sockaddr_storage
))
188 if (copy_from_user(kaddr
, uaddr
, ulen
))
190 return audit_sockaddr(ulen
, kaddr
);
194 * move_addr_to_user - copy an address to user space
195 * @kaddr: kernel space address
196 * @klen: length of address in kernel
197 * @uaddr: user space address
198 * @ulen: pointer to user length field
200 * The value pointed to by ulen on entry is the buffer length available.
201 * This is overwritten with the buffer space used. -EINVAL is returned
202 * if an overlong buffer is specified or a negative buffer size. -EFAULT
203 * is returned if either the buffer or the length field are not
205 * After copying the data up to the limit the user specifies, the true
206 * length of the data is written over the length limit the user
207 * specified. Zero is returned for a success.
210 static int move_addr_to_user(struct sockaddr_storage
*kaddr
, int klen
,
211 void __user
*uaddr
, int __user
*ulen
)
216 BUG_ON(klen
> sizeof(struct sockaddr_storage
));
217 err
= get_user(len
, ulen
);
225 if (audit_sockaddr(klen
, kaddr
))
227 if (copy_to_user(uaddr
, kaddr
, len
))
231 * "fromlen shall refer to the value before truncation.."
234 return __put_user(klen
, ulen
);
237 static struct kmem_cache
*sock_inode_cachep __ro_after_init
;
239 static struct inode
*sock_alloc_inode(struct super_block
*sb
)
241 struct socket_alloc
*ei
;
242 struct socket_wq
*wq
;
244 ei
= kmem_cache_alloc(sock_inode_cachep
, GFP_KERNEL
);
247 wq
= kmalloc(sizeof(*wq
), GFP_KERNEL
);
249 kmem_cache_free(sock_inode_cachep
, ei
);
252 init_waitqueue_head(&wq
->wait
);
253 wq
->fasync_list
= NULL
;
257 ei
->socket
.state
= SS_UNCONNECTED
;
258 ei
->socket
.flags
= 0;
259 ei
->socket
.ops
= NULL
;
260 ei
->socket
.sk
= NULL
;
261 ei
->socket
.file
= NULL
;
263 return &ei
->vfs_inode
;
266 static void sock_destroy_inode(struct inode
*inode
)
268 struct socket_alloc
*ei
;
270 ei
= container_of(inode
, struct socket_alloc
, vfs_inode
);
271 kfree_rcu(ei
->socket
.wq
, rcu
);
272 kmem_cache_free(sock_inode_cachep
, ei
);
275 static void init_once(void *foo
)
277 struct socket_alloc
*ei
= (struct socket_alloc
*)foo
;
279 inode_init_once(&ei
->vfs_inode
);
282 static void init_inodecache(void)
284 sock_inode_cachep
= kmem_cache_create("sock_inode_cache",
285 sizeof(struct socket_alloc
),
287 (SLAB_HWCACHE_ALIGN
|
288 SLAB_RECLAIM_ACCOUNT
|
289 SLAB_MEM_SPREAD
| SLAB_ACCOUNT
),
291 BUG_ON(sock_inode_cachep
== NULL
);
294 static const struct super_operations sockfs_ops
= {
295 .alloc_inode
= sock_alloc_inode
,
296 .destroy_inode
= sock_destroy_inode
,
297 .statfs
= simple_statfs
,
301 * sockfs_dname() is called from d_path().
303 static char *sockfs_dname(struct dentry
*dentry
, char *buffer
, int buflen
)
305 return dynamic_dname(dentry
, buffer
, buflen
, "socket:[%lu]",
306 d_inode(dentry
)->i_ino
);
309 static const struct dentry_operations sockfs_dentry_operations
= {
310 .d_dname
= sockfs_dname
,
313 static int sockfs_xattr_get(const struct xattr_handler
*handler
,
314 struct dentry
*dentry
, struct inode
*inode
,
315 const char *suffix
, void *value
, size_t size
)
318 if (dentry
->d_name
.len
+ 1 > size
)
320 memcpy(value
, dentry
->d_name
.name
, dentry
->d_name
.len
+ 1);
322 return dentry
->d_name
.len
+ 1;
325 #define XATTR_SOCKPROTONAME_SUFFIX "sockprotoname"
326 #define XATTR_NAME_SOCKPROTONAME (XATTR_SYSTEM_PREFIX XATTR_SOCKPROTONAME_SUFFIX)
327 #define XATTR_NAME_SOCKPROTONAME_LEN (sizeof(XATTR_NAME_SOCKPROTONAME)-1)
329 static const struct xattr_handler sockfs_xattr_handler
= {
330 .name
= XATTR_NAME_SOCKPROTONAME
,
331 .get
= sockfs_xattr_get
,
334 static int sockfs_security_xattr_set(const struct xattr_handler
*handler
,
335 struct dentry
*dentry
, struct inode
*inode
,
336 const char *suffix
, const void *value
,
337 size_t size
, int flags
)
339 /* Handled by LSM. */
343 static const struct xattr_handler sockfs_security_xattr_handler
= {
344 .prefix
= XATTR_SECURITY_PREFIX
,
345 .set
= sockfs_security_xattr_set
,
348 static const struct xattr_handler
*sockfs_xattr_handlers
[] = {
349 &sockfs_xattr_handler
,
350 &sockfs_security_xattr_handler
,
354 static struct dentry
*sockfs_mount(struct file_system_type
*fs_type
,
355 int flags
, const char *dev_name
, void *data
)
357 return mount_pseudo_xattr(fs_type
, "socket:", &sockfs_ops
,
358 sockfs_xattr_handlers
,
359 &sockfs_dentry_operations
, SOCKFS_MAGIC
);
362 static struct vfsmount
*sock_mnt __read_mostly
;
364 static struct file_system_type sock_fs_type
= {
366 .mount
= sockfs_mount
,
367 .kill_sb
= kill_anon_super
,
371 * Obtains the first available file descriptor and sets it up for use.
373 * These functions create file structures and maps them to fd space
374 * of the current process. On success it returns file descriptor
375 * and file struct implicitly stored in sock->file.
376 * Note that another thread may close file descriptor before we return
377 * from this function. We use the fact that now we do not refer
378 * to socket after mapping. If one day we will need it, this
379 * function will increment ref. count on file by 1.
381 * In any case returned fd MAY BE not valid!
382 * This race condition is unavoidable
383 * with shared fd spaces, we cannot solve it inside kernel,
384 * but we take care of internal coherence yet.
388 * sock_alloc_file - Bind a &socket to a &file
390 * @flags: file status flags
391 * @dname: protocol name
393 * Returns the &file bound with @sock, implicitly storing it
394 * in sock->file. If dname is %NULL, sets to "".
395 * On failure the return is a ERR pointer (see linux/err.h).
396 * This function uses GFP_KERNEL internally.
399 struct file
*sock_alloc_file(struct socket
*sock
, int flags
, const char *dname
)
404 dname
= sock
->sk
? sock
->sk
->sk_prot_creator
->name
: "";
406 file
= alloc_file_pseudo(SOCK_INODE(sock
), sock_mnt
, dname
,
407 O_RDWR
| (flags
& O_NONBLOCK
),
415 file
->private_data
= sock
;
418 EXPORT_SYMBOL(sock_alloc_file
);
420 static int sock_map_fd(struct socket
*sock
, int flags
)
422 struct file
*newfile
;
423 int fd
= get_unused_fd_flags(flags
);
424 if (unlikely(fd
< 0)) {
429 newfile
= sock_alloc_file(sock
, flags
, NULL
);
430 if (likely(!IS_ERR(newfile
))) {
431 fd_install(fd
, newfile
);
436 return PTR_ERR(newfile
);
440 * sock_from_file - Return the &socket bounded to @file.
442 * @err: pointer to an error code return
444 * On failure returns %NULL and assigns -ENOTSOCK to @err.
447 struct socket
*sock_from_file(struct file
*file
, int *err
)
449 if (file
->f_op
== &socket_file_ops
)
450 return file
->private_data
; /* set in sock_map_fd */
455 EXPORT_SYMBOL(sock_from_file
);
458 * sockfd_lookup - Go from a file number to its socket slot
460 * @err: pointer to an error code return
462 * The file handle passed in is locked and the socket it is bound
463 * to is returned. If an error occurs the err pointer is overwritten
464 * with a negative errno code and NULL is returned. The function checks
465 * for both invalid handles and passing a handle which is not a socket.
467 * On a success the socket object pointer is returned.
470 struct socket
*sockfd_lookup(int fd
, int *err
)
481 sock
= sock_from_file(file
, err
);
486 EXPORT_SYMBOL(sockfd_lookup
);
488 static struct socket
*sockfd_lookup_light(int fd
, int *err
, int *fput_needed
)
490 struct fd f
= fdget(fd
);
495 sock
= sock_from_file(f
.file
, err
);
497 *fput_needed
= f
.flags
;
505 static ssize_t
sockfs_listxattr(struct dentry
*dentry
, char *buffer
,
511 len
= security_inode_listsecurity(d_inode(dentry
), buffer
, size
);
521 len
= (XATTR_NAME_SOCKPROTONAME_LEN
+ 1);
526 memcpy(buffer
, XATTR_NAME_SOCKPROTONAME
, len
);
533 static int sockfs_setattr(struct dentry
*dentry
, struct iattr
*iattr
)
535 int err
= simple_setattr(dentry
, iattr
);
537 if (!err
&& (iattr
->ia_valid
& ATTR_UID
)) {
538 struct socket
*sock
= SOCKET_I(d_inode(dentry
));
541 sock
->sk
->sk_uid
= iattr
->ia_uid
;
549 static const struct inode_operations sockfs_inode_ops
= {
550 .listxattr
= sockfs_listxattr
,
551 .setattr
= sockfs_setattr
,
555 * sock_alloc - allocate a socket
557 * Allocate a new inode and socket object. The two are bound together
558 * and initialised. The socket is then returned. If we are out of inodes
559 * NULL is returned. This functions uses GFP_KERNEL internally.
562 struct socket
*sock_alloc(void)
567 inode
= new_inode_pseudo(sock_mnt
->mnt_sb
);
571 sock
= SOCKET_I(inode
);
573 inode
->i_ino
= get_next_ino();
574 inode
->i_mode
= S_IFSOCK
| S_IRWXUGO
;
575 inode
->i_uid
= current_fsuid();
576 inode
->i_gid
= current_fsgid();
577 inode
->i_op
= &sockfs_inode_ops
;
581 EXPORT_SYMBOL(sock_alloc
);
584 * sock_release - close a socket
585 * @sock: socket to close
587 * The socket is released from the protocol stack if it has a release
588 * callback, and the inode is then released if the socket is bound to
589 * an inode not a file.
592 static void __sock_release(struct socket
*sock
, struct inode
*inode
)
595 struct module
*owner
= sock
->ops
->owner
;
599 sock
->ops
->release(sock
);
607 if (sock
->wq
->fasync_list
)
608 pr_err("%s: fasync list not empty!\n", __func__
);
611 iput(SOCK_INODE(sock
));
617 void sock_release(struct socket
*sock
)
619 __sock_release(sock
, NULL
);
621 EXPORT_SYMBOL(sock_release
);
623 void __sock_tx_timestamp(__u16 tsflags
, __u8
*tx_flags
)
625 u8 flags
= *tx_flags
;
627 if (tsflags
& SOF_TIMESTAMPING_TX_HARDWARE
)
628 flags
|= SKBTX_HW_TSTAMP
;
630 if (tsflags
& SOF_TIMESTAMPING_TX_SOFTWARE
)
631 flags
|= SKBTX_SW_TSTAMP
;
633 if (tsflags
& SOF_TIMESTAMPING_TX_SCHED
)
634 flags
|= SKBTX_SCHED_TSTAMP
;
638 EXPORT_SYMBOL(__sock_tx_timestamp
);
641 * sock_sendmsg - send a message through @sock
643 * @msg: message to send
645 * Sends @msg through @sock, passing through LSM.
646 * Returns the number of bytes sent, or an error code.
649 static inline int sock_sendmsg_nosec(struct socket
*sock
, struct msghdr
*msg
)
651 int ret
= sock
->ops
->sendmsg(sock
, msg
, msg_data_left(msg
));
652 BUG_ON(ret
== -EIOCBQUEUED
);
656 int sock_sendmsg(struct socket
*sock
, struct msghdr
*msg
)
658 int err
= security_socket_sendmsg(sock
, msg
,
661 return err
?: sock_sendmsg_nosec(sock
, msg
);
663 EXPORT_SYMBOL(sock_sendmsg
);
666 * kernel_sendmsg - send a message through @sock (kernel-space)
668 * @msg: message header
670 * @num: vec array length
671 * @size: total message data size
673 * Builds the message data with @vec and sends it through @sock.
674 * Returns the number of bytes sent, or an error code.
677 int kernel_sendmsg(struct socket
*sock
, struct msghdr
*msg
,
678 struct kvec
*vec
, size_t num
, size_t size
)
680 iov_iter_kvec(&msg
->msg_iter
, WRITE
, vec
, num
, size
);
681 return sock_sendmsg(sock
, msg
);
683 EXPORT_SYMBOL(kernel_sendmsg
);
686 * kernel_sendmsg_locked - send a message through @sock (kernel-space)
688 * @msg: message header
689 * @vec: output s/g array
690 * @num: output s/g array length
691 * @size: total message data size
693 * Builds the message data with @vec and sends it through @sock.
694 * Returns the number of bytes sent, or an error code.
695 * Caller must hold @sk.
698 int kernel_sendmsg_locked(struct sock
*sk
, struct msghdr
*msg
,
699 struct kvec
*vec
, size_t num
, size_t size
)
701 struct socket
*sock
= sk
->sk_socket
;
703 if (!sock
->ops
->sendmsg_locked
)
704 return sock_no_sendmsg_locked(sk
, msg
, size
);
706 iov_iter_kvec(&msg
->msg_iter
, WRITE
, vec
, num
, size
);
708 return sock
->ops
->sendmsg_locked(sk
, msg
, msg_data_left(msg
));
710 EXPORT_SYMBOL(kernel_sendmsg_locked
);
712 static bool skb_is_err_queue(const struct sk_buff
*skb
)
714 /* pkt_type of skbs enqueued on the error queue are set to
715 * PACKET_OUTGOING in skb_set_err_queue(). This is only safe to do
716 * in recvmsg, since skbs received on a local socket will never
717 * have a pkt_type of PACKET_OUTGOING.
719 return skb
->pkt_type
== PACKET_OUTGOING
;
722 /* On transmit, software and hardware timestamps are returned independently.
723 * As the two skb clones share the hardware timestamp, which may be updated
724 * before the software timestamp is received, a hardware TX timestamp may be
725 * returned only if there is no software TX timestamp. Ignore false software
726 * timestamps, which may be made in the __sock_recv_timestamp() call when the
727 * option SO_TIMESTAMP_OLD(NS) is enabled on the socket, even when the skb has a
728 * hardware timestamp.
730 static bool skb_is_swtx_tstamp(const struct sk_buff
*skb
, int false_tstamp
)
732 return skb
->tstamp
&& !false_tstamp
&& skb_is_err_queue(skb
);
735 static void put_ts_pktinfo(struct msghdr
*msg
, struct sk_buff
*skb
)
737 struct scm_ts_pktinfo ts_pktinfo
;
738 struct net_device
*orig_dev
;
740 if (!skb_mac_header_was_set(skb
))
743 memset(&ts_pktinfo
, 0, sizeof(ts_pktinfo
));
746 orig_dev
= dev_get_by_napi_id(skb_napi_id(skb
));
748 ts_pktinfo
.if_index
= orig_dev
->ifindex
;
751 ts_pktinfo
.pkt_length
= skb
->len
- skb_mac_offset(skb
);
752 put_cmsg(msg
, SOL_SOCKET
, SCM_TIMESTAMPING_PKTINFO
,
753 sizeof(ts_pktinfo
), &ts_pktinfo
);
757 * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
759 void __sock_recv_timestamp(struct msghdr
*msg
, struct sock
*sk
,
762 int need_software_tstamp
= sock_flag(sk
, SOCK_RCVTSTAMP
);
763 int new_tstamp
= sock_flag(sk
, SOCK_TSTAMP_NEW
);
764 struct scm_timestamping_internal tss
;
766 int empty
= 1, false_tstamp
= 0;
767 struct skb_shared_hwtstamps
*shhwtstamps
=
770 /* Race occurred between timestamp enabling and packet
771 receiving. Fill in the current time for now. */
772 if (need_software_tstamp
&& skb
->tstamp
== 0) {
773 __net_timestamp(skb
);
777 if (need_software_tstamp
) {
778 if (!sock_flag(sk
, SOCK_RCVTSTAMPNS
)) {
780 struct __kernel_sock_timeval tv
;
782 skb_get_new_timestamp(skb
, &tv
);
783 put_cmsg(msg
, SOL_SOCKET
, SO_TIMESTAMP_NEW
,
786 struct __kernel_old_timeval tv
;
788 skb_get_timestamp(skb
, &tv
);
789 put_cmsg(msg
, SOL_SOCKET
, SO_TIMESTAMP_OLD
,
794 struct __kernel_timespec ts
;
796 skb_get_new_timestampns(skb
, &ts
);
797 put_cmsg(msg
, SOL_SOCKET
, SO_TIMESTAMPNS_NEW
,
802 skb_get_timestampns(skb
, &ts
);
803 put_cmsg(msg
, SOL_SOCKET
, SO_TIMESTAMPNS_OLD
,
809 memset(&tss
, 0, sizeof(tss
));
810 if ((sk
->sk_tsflags
& SOF_TIMESTAMPING_SOFTWARE
) &&
811 ktime_to_timespec64_cond(skb
->tstamp
, tss
.ts
+ 0))
814 (sk
->sk_tsflags
& SOF_TIMESTAMPING_RAW_HARDWARE
) &&
815 !skb_is_swtx_tstamp(skb
, false_tstamp
) &&
816 ktime_to_timespec64_cond(shhwtstamps
->hwtstamp
, tss
.ts
+ 2)) {
818 if ((sk
->sk_tsflags
& SOF_TIMESTAMPING_OPT_PKTINFO
) &&
819 !skb_is_err_queue(skb
))
820 put_ts_pktinfo(msg
, skb
);
823 if (sock_flag(sk
, SOCK_TSTAMP_NEW
))
824 put_cmsg_scm_timestamping64(msg
, &tss
);
826 put_cmsg_scm_timestamping(msg
, &tss
);
828 if (skb_is_err_queue(skb
) && skb
->len
&&
829 SKB_EXT_ERR(skb
)->opt_stats
)
830 put_cmsg(msg
, SOL_SOCKET
, SCM_TIMESTAMPING_OPT_STATS
,
831 skb
->len
, skb
->data
);
834 EXPORT_SYMBOL_GPL(__sock_recv_timestamp
);
836 void __sock_recv_wifi_status(struct msghdr
*msg
, struct sock
*sk
,
841 if (!sock_flag(sk
, SOCK_WIFI_STATUS
))
843 if (!skb
->wifi_acked_valid
)
846 ack
= skb
->wifi_acked
;
848 put_cmsg(msg
, SOL_SOCKET
, SCM_WIFI_STATUS
, sizeof(ack
), &ack
);
850 EXPORT_SYMBOL_GPL(__sock_recv_wifi_status
);
852 static inline void sock_recv_drops(struct msghdr
*msg
, struct sock
*sk
,
855 if (sock_flag(sk
, SOCK_RXQ_OVFL
) && skb
&& SOCK_SKB_CB(skb
)->dropcount
)
856 put_cmsg(msg
, SOL_SOCKET
, SO_RXQ_OVFL
,
857 sizeof(__u32
), &SOCK_SKB_CB(skb
)->dropcount
);
860 void __sock_recv_ts_and_drops(struct msghdr
*msg
, struct sock
*sk
,
863 sock_recv_timestamp(msg
, sk
, skb
);
864 sock_recv_drops(msg
, sk
, skb
);
866 EXPORT_SYMBOL_GPL(__sock_recv_ts_and_drops
);
869 * sock_recvmsg - receive a message from @sock
871 * @msg: message to receive
872 * @flags: message flags
874 * Receives @msg from @sock, passing through LSM. Returns the total number
875 * of bytes received, or an error.
878 static inline int sock_recvmsg_nosec(struct socket
*sock
, struct msghdr
*msg
,
881 return sock
->ops
->recvmsg(sock
, msg
, msg_data_left(msg
), flags
);
884 int sock_recvmsg(struct socket
*sock
, struct msghdr
*msg
, int flags
)
886 int err
= security_socket_recvmsg(sock
, msg
, msg_data_left(msg
), flags
);
888 return err
?: sock_recvmsg_nosec(sock
, msg
, flags
);
890 EXPORT_SYMBOL(sock_recvmsg
);
893 * kernel_recvmsg - Receive a message from a socket (kernel space)
894 * @sock: The socket to receive the message from
895 * @msg: Received message
896 * @vec: Input s/g array for message data
897 * @num: Size of input s/g array
898 * @size: Number of bytes to read
899 * @flags: Message flags (MSG_DONTWAIT, etc...)
901 * On return the msg structure contains the scatter/gather array passed in the
902 * vec argument. The array is modified so that it consists of the unfilled
903 * portion of the original array.
905 * The returned value is the total number of bytes received, or an error.
908 int kernel_recvmsg(struct socket
*sock
, struct msghdr
*msg
,
909 struct kvec
*vec
, size_t num
, size_t size
, int flags
)
911 mm_segment_t oldfs
= get_fs();
914 iov_iter_kvec(&msg
->msg_iter
, READ
, vec
, num
, size
);
916 result
= sock_recvmsg(sock
, msg
, flags
);
920 EXPORT_SYMBOL(kernel_recvmsg
);
922 static ssize_t
sock_sendpage(struct file
*file
, struct page
*page
,
923 int offset
, size_t size
, loff_t
*ppos
, int more
)
928 sock
= file
->private_data
;
930 flags
= (file
->f_flags
& O_NONBLOCK
) ? MSG_DONTWAIT
: 0;
931 /* more is a combination of MSG_MORE and MSG_SENDPAGE_NOTLAST */
934 return kernel_sendpage(sock
, page
, offset
, size
, flags
);
937 static ssize_t
sock_splice_read(struct file
*file
, loff_t
*ppos
,
938 struct pipe_inode_info
*pipe
, size_t len
,
941 struct socket
*sock
= file
->private_data
;
943 if (unlikely(!sock
->ops
->splice_read
))
944 return generic_file_splice_read(file
, ppos
, pipe
, len
, flags
);
946 return sock
->ops
->splice_read(sock
, ppos
, pipe
, len
, flags
);
949 static ssize_t
sock_read_iter(struct kiocb
*iocb
, struct iov_iter
*to
)
951 struct file
*file
= iocb
->ki_filp
;
952 struct socket
*sock
= file
->private_data
;
953 struct msghdr msg
= {.msg_iter
= *to
,
957 if (file
->f_flags
& O_NONBLOCK
)
958 msg
.msg_flags
= MSG_DONTWAIT
;
960 if (iocb
->ki_pos
!= 0)
963 if (!iov_iter_count(to
)) /* Match SYS5 behaviour */
966 res
= sock_recvmsg(sock
, &msg
, msg
.msg_flags
);
971 static ssize_t
sock_write_iter(struct kiocb
*iocb
, struct iov_iter
*from
)
973 struct file
*file
= iocb
->ki_filp
;
974 struct socket
*sock
= file
->private_data
;
975 struct msghdr msg
= {.msg_iter
= *from
,
979 if (iocb
->ki_pos
!= 0)
982 if (file
->f_flags
& O_NONBLOCK
)
983 msg
.msg_flags
= MSG_DONTWAIT
;
985 if (sock
->type
== SOCK_SEQPACKET
)
986 msg
.msg_flags
|= MSG_EOR
;
988 res
= sock_sendmsg(sock
, &msg
);
989 *from
= msg
.msg_iter
;
994 * Atomic setting of ioctl hooks to avoid race
995 * with module unload.
998 static DEFINE_MUTEX(br_ioctl_mutex
);
999 static int (*br_ioctl_hook
) (struct net
*, unsigned int cmd
, void __user
*arg
);
1001 void brioctl_set(int (*hook
) (struct net
*, unsigned int, void __user
*))
1003 mutex_lock(&br_ioctl_mutex
);
1004 br_ioctl_hook
= hook
;
1005 mutex_unlock(&br_ioctl_mutex
);
1007 EXPORT_SYMBOL(brioctl_set
);
1009 static DEFINE_MUTEX(vlan_ioctl_mutex
);
1010 static int (*vlan_ioctl_hook
) (struct net
*, void __user
*arg
);
1012 void vlan_ioctl_set(int (*hook
) (struct net
*, void __user
*))
1014 mutex_lock(&vlan_ioctl_mutex
);
1015 vlan_ioctl_hook
= hook
;
1016 mutex_unlock(&vlan_ioctl_mutex
);
1018 EXPORT_SYMBOL(vlan_ioctl_set
);
1020 static DEFINE_MUTEX(dlci_ioctl_mutex
);
1021 static int (*dlci_ioctl_hook
) (unsigned int, void __user
*);
1023 void dlci_ioctl_set(int (*hook
) (unsigned int, void __user
*))
1025 mutex_lock(&dlci_ioctl_mutex
);
1026 dlci_ioctl_hook
= hook
;
1027 mutex_unlock(&dlci_ioctl_mutex
);
1029 EXPORT_SYMBOL(dlci_ioctl_set
);
1031 static long sock_do_ioctl(struct net
*net
, struct socket
*sock
,
1032 unsigned int cmd
, unsigned long arg
)
1035 void __user
*argp
= (void __user
*)arg
;
1037 err
= sock
->ops
->ioctl(sock
, cmd
, arg
);
1040 * If this ioctl is unknown try to hand it down
1041 * to the NIC driver.
1043 if (err
!= -ENOIOCTLCMD
)
1046 if (cmd
== SIOCGIFCONF
) {
1048 if (copy_from_user(&ifc
, argp
, sizeof(struct ifconf
)))
1051 err
= dev_ifconf(net
, &ifc
, sizeof(struct ifreq
));
1053 if (!err
&& copy_to_user(argp
, &ifc
, sizeof(struct ifconf
)))
1058 if (copy_from_user(&ifr
, argp
, sizeof(struct ifreq
)))
1060 err
= dev_ioctl(net
, cmd
, &ifr
, &need_copyout
);
1061 if (!err
&& need_copyout
)
1062 if (copy_to_user(argp
, &ifr
, sizeof(struct ifreq
)))
1069 * With an ioctl, arg may well be a user mode pointer, but we don't know
1070 * what to do with it - that's up to the protocol still.
1074 * get_net_ns - increment the refcount of the network namespace
1075 * @ns: common namespace (net)
1077 * Returns the net's common namespace.
1080 struct ns_common
*get_net_ns(struct ns_common
*ns
)
1082 return &get_net(container_of(ns
, struct net
, ns
))->ns
;
1084 EXPORT_SYMBOL_GPL(get_net_ns
);
1086 static long sock_ioctl(struct file
*file
, unsigned cmd
, unsigned long arg
)
1088 struct socket
*sock
;
1090 void __user
*argp
= (void __user
*)arg
;
1094 sock
= file
->private_data
;
1097 if (unlikely(cmd
>= SIOCDEVPRIVATE
&& cmd
<= (SIOCDEVPRIVATE
+ 15))) {
1100 if (copy_from_user(&ifr
, argp
, sizeof(struct ifreq
)))
1102 err
= dev_ioctl(net
, cmd
, &ifr
, &need_copyout
);
1103 if (!err
&& need_copyout
)
1104 if (copy_to_user(argp
, &ifr
, sizeof(struct ifreq
)))
1107 #ifdef CONFIG_WEXT_CORE
1108 if (cmd
>= SIOCIWFIRST
&& cmd
<= SIOCIWLAST
) {
1109 err
= wext_handle_ioctl(net
, cmd
, argp
);
1116 if (get_user(pid
, (int __user
*)argp
))
1118 err
= f_setown(sock
->file
, pid
, 1);
1122 err
= put_user(f_getown(sock
->file
),
1123 (int __user
*)argp
);
1131 request_module("bridge");
1133 mutex_lock(&br_ioctl_mutex
);
1135 err
= br_ioctl_hook(net
, cmd
, argp
);
1136 mutex_unlock(&br_ioctl_mutex
);
1141 if (!vlan_ioctl_hook
)
1142 request_module("8021q");
1144 mutex_lock(&vlan_ioctl_mutex
);
1145 if (vlan_ioctl_hook
)
1146 err
= vlan_ioctl_hook(net
, argp
);
1147 mutex_unlock(&vlan_ioctl_mutex
);
1152 if (!dlci_ioctl_hook
)
1153 request_module("dlci");
1155 mutex_lock(&dlci_ioctl_mutex
);
1156 if (dlci_ioctl_hook
)
1157 err
= dlci_ioctl_hook(cmd
, argp
);
1158 mutex_unlock(&dlci_ioctl_mutex
);
1162 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
1165 err
= open_related_ns(&net
->ns
, get_net_ns
);
1168 err
= sock_do_ioctl(net
, sock
, cmd
, arg
);
1175 * sock_create_lite - creates a socket
1176 * @family: protocol family (AF_INET, ...)
1177 * @type: communication type (SOCK_STREAM, ...)
1178 * @protocol: protocol (0, ...)
1181 * Creates a new socket and assigns it to @res, passing through LSM.
1182 * The new socket initialization is not complete, see kernel_accept().
1183 * Returns 0 or an error. On failure @res is set to %NULL.
1184 * This function internally uses GFP_KERNEL.
1187 int sock_create_lite(int family
, int type
, int protocol
, struct socket
**res
)
1190 struct socket
*sock
= NULL
;
1192 err
= security_socket_create(family
, type
, protocol
, 1);
1196 sock
= sock_alloc();
1203 err
= security_socket_post_create(sock
, family
, type
, protocol
, 1);
1215 EXPORT_SYMBOL(sock_create_lite
);
1217 /* No kernel lock held - perfect */
1218 static __poll_t
sock_poll(struct file
*file
, poll_table
*wait
)
1220 struct socket
*sock
= file
->private_data
;
1221 __poll_t events
= poll_requested_events(wait
), flag
= 0;
1223 if (!sock
->ops
->poll
)
1226 if (sk_can_busy_loop(sock
->sk
)) {
1227 /* poll once if requested by the syscall */
1228 if (events
& POLL_BUSY_LOOP
)
1229 sk_busy_loop(sock
->sk
, 1);
1231 /* if this socket can poll_ll, tell the system call */
1232 flag
= POLL_BUSY_LOOP
;
1235 return sock
->ops
->poll(file
, sock
, wait
) | flag
;
1238 static int sock_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1240 struct socket
*sock
= file
->private_data
;
1242 return sock
->ops
->mmap(file
, sock
, vma
);
1245 static int sock_close(struct inode
*inode
, struct file
*filp
)
1247 __sock_release(SOCKET_I(inode
), inode
);
1252 * Update the socket async list
1254 * Fasync_list locking strategy.
1256 * 1. fasync_list is modified only under process context socket lock
1257 * i.e. under semaphore.
1258 * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
1259 * or under socket lock
1262 static int sock_fasync(int fd
, struct file
*filp
, int on
)
1264 struct socket
*sock
= filp
->private_data
;
1265 struct sock
*sk
= sock
->sk
;
1266 struct socket_wq
*wq
;
1273 fasync_helper(fd
, filp
, on
, &wq
->fasync_list
);
1275 if (!wq
->fasync_list
)
1276 sock_reset_flag(sk
, SOCK_FASYNC
);
1278 sock_set_flag(sk
, SOCK_FASYNC
);
1284 /* This function may be called only under rcu_lock */
1286 int sock_wake_async(struct socket_wq
*wq
, int how
, int band
)
1288 if (!wq
|| !wq
->fasync_list
)
1292 case SOCK_WAKE_WAITD
:
1293 if (test_bit(SOCKWQ_ASYNC_WAITDATA
, &wq
->flags
))
1296 case SOCK_WAKE_SPACE
:
1297 if (!test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE
, &wq
->flags
))
1302 kill_fasync(&wq
->fasync_list
, SIGIO
, band
);
1305 kill_fasync(&wq
->fasync_list
, SIGURG
, band
);
1310 EXPORT_SYMBOL(sock_wake_async
);
1313 * __sock_create - creates a socket
1314 * @net: net namespace
1315 * @family: protocol family (AF_INET, ...)
1316 * @type: communication type (SOCK_STREAM, ...)
1317 * @protocol: protocol (0, ...)
1319 * @kern: boolean for kernel space sockets
1321 * Creates a new socket and assigns it to @res, passing through LSM.
1322 * Returns 0 or an error. On failure @res is set to %NULL. @kern must
1323 * be set to true if the socket resides in kernel space.
1324 * This function internally uses GFP_KERNEL.
1327 int __sock_create(struct net
*net
, int family
, int type
, int protocol
,
1328 struct socket
**res
, int kern
)
1331 struct socket
*sock
;
1332 const struct net_proto_family
*pf
;
1335 * Check protocol is in range
1337 if (family
< 0 || family
>= NPROTO
)
1338 return -EAFNOSUPPORT
;
1339 if (type
< 0 || type
>= SOCK_MAX
)
1344 This uglymoron is moved from INET layer to here to avoid
1345 deadlock in module load.
1347 if (family
== PF_INET
&& type
== SOCK_PACKET
) {
1348 pr_info_once("%s uses obsolete (PF_INET,SOCK_PACKET)\n",
1353 err
= security_socket_create(family
, type
, protocol
, kern
);
1358 * Allocate the socket and allow the family to set things up. if
1359 * the protocol is 0, the family is instructed to select an appropriate
1362 sock
= sock_alloc();
1364 net_warn_ratelimited("socket: no more sockets\n");
1365 return -ENFILE
; /* Not exactly a match, but its the
1366 closest posix thing */
1371 #ifdef CONFIG_MODULES
1372 /* Attempt to load a protocol module if the find failed.
1374 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
1375 * requested real, full-featured networking support upon configuration.
1376 * Otherwise module support will break!
1378 if (rcu_access_pointer(net_families
[family
]) == NULL
)
1379 request_module("net-pf-%d", family
);
1383 pf
= rcu_dereference(net_families
[family
]);
1384 err
= -EAFNOSUPPORT
;
1389 * We will call the ->create function, that possibly is in a loadable
1390 * module, so we have to bump that loadable module refcnt first.
1392 if (!try_module_get(pf
->owner
))
1395 /* Now protected by module ref count */
1398 err
= pf
->create(net
, sock
, protocol
, kern
);
1400 goto out_module_put
;
1403 * Now to bump the refcnt of the [loadable] module that owns this
1404 * socket at sock_release time we decrement its refcnt.
1406 if (!try_module_get(sock
->ops
->owner
))
1407 goto out_module_busy
;
1410 * Now that we're done with the ->create function, the [loadable]
1411 * module can have its refcnt decremented
1413 module_put(pf
->owner
);
1414 err
= security_socket_post_create(sock
, family
, type
, protocol
, kern
);
1416 goto out_sock_release
;
1422 err
= -EAFNOSUPPORT
;
1425 module_put(pf
->owner
);
1432 goto out_sock_release
;
1434 EXPORT_SYMBOL(__sock_create
);
1437 * sock_create - creates a socket
1438 * @family: protocol family (AF_INET, ...)
1439 * @type: communication type (SOCK_STREAM, ...)
1440 * @protocol: protocol (0, ...)
1443 * A wrapper around __sock_create().
1444 * Returns 0 or an error. This function internally uses GFP_KERNEL.
1447 int sock_create(int family
, int type
, int protocol
, struct socket
**res
)
1449 return __sock_create(current
->nsproxy
->net_ns
, family
, type
, protocol
, res
, 0);
1451 EXPORT_SYMBOL(sock_create
);
1454 * sock_create_kern - creates a socket (kernel space)
1455 * @net: net namespace
1456 * @family: protocol family (AF_INET, ...)
1457 * @type: communication type (SOCK_STREAM, ...)
1458 * @protocol: protocol (0, ...)
1461 * A wrapper around __sock_create().
1462 * Returns 0 or an error. This function internally uses GFP_KERNEL.
1465 int sock_create_kern(struct net
*net
, int family
, int type
, int protocol
, struct socket
**res
)
1467 return __sock_create(net
, family
, type
, protocol
, res
, 1);
1469 EXPORT_SYMBOL(sock_create_kern
);
1471 int __sys_socket(int family
, int type
, int protocol
)
1474 struct socket
*sock
;
1477 /* Check the SOCK_* constants for consistency. */
1478 BUILD_BUG_ON(SOCK_CLOEXEC
!= O_CLOEXEC
);
1479 BUILD_BUG_ON((SOCK_MAX
| SOCK_TYPE_MASK
) != SOCK_TYPE_MASK
);
1480 BUILD_BUG_ON(SOCK_CLOEXEC
& SOCK_TYPE_MASK
);
1481 BUILD_BUG_ON(SOCK_NONBLOCK
& SOCK_TYPE_MASK
);
1483 flags
= type
& ~SOCK_TYPE_MASK
;
1484 if (flags
& ~(SOCK_CLOEXEC
| SOCK_NONBLOCK
))
1486 type
&= SOCK_TYPE_MASK
;
1488 if (SOCK_NONBLOCK
!= O_NONBLOCK
&& (flags
& SOCK_NONBLOCK
))
1489 flags
= (flags
& ~SOCK_NONBLOCK
) | O_NONBLOCK
;
1491 retval
= sock_create(family
, type
, protocol
, &sock
);
1495 return sock_map_fd(sock
, flags
& (O_CLOEXEC
| O_NONBLOCK
));
1498 SYSCALL_DEFINE3(socket
, int, family
, int, type
, int, protocol
)
1500 return __sys_socket(family
, type
, protocol
);
1504 * Create a pair of connected sockets.
1507 int __sys_socketpair(int family
, int type
, int protocol
, int __user
*usockvec
)
1509 struct socket
*sock1
, *sock2
;
1511 struct file
*newfile1
, *newfile2
;
1514 flags
= type
& ~SOCK_TYPE_MASK
;
1515 if (flags
& ~(SOCK_CLOEXEC
| SOCK_NONBLOCK
))
1517 type
&= SOCK_TYPE_MASK
;
1519 if (SOCK_NONBLOCK
!= O_NONBLOCK
&& (flags
& SOCK_NONBLOCK
))
1520 flags
= (flags
& ~SOCK_NONBLOCK
) | O_NONBLOCK
;
1523 * reserve descriptors and make sure we won't fail
1524 * to return them to userland.
1526 fd1
= get_unused_fd_flags(flags
);
1527 if (unlikely(fd1
< 0))
1530 fd2
= get_unused_fd_flags(flags
);
1531 if (unlikely(fd2
< 0)) {
1536 err
= put_user(fd1
, &usockvec
[0]);
1540 err
= put_user(fd2
, &usockvec
[1]);
1545 * Obtain the first socket and check if the underlying protocol
1546 * supports the socketpair call.
1549 err
= sock_create(family
, type
, protocol
, &sock1
);
1550 if (unlikely(err
< 0))
1553 err
= sock_create(family
, type
, protocol
, &sock2
);
1554 if (unlikely(err
< 0)) {
1555 sock_release(sock1
);
1559 err
= security_socket_socketpair(sock1
, sock2
);
1560 if (unlikely(err
)) {
1561 sock_release(sock2
);
1562 sock_release(sock1
);
1566 err
= sock1
->ops
->socketpair(sock1
, sock2
);
1567 if (unlikely(err
< 0)) {
1568 sock_release(sock2
);
1569 sock_release(sock1
);
1573 newfile1
= sock_alloc_file(sock1
, flags
, NULL
);
1574 if (IS_ERR(newfile1
)) {
1575 err
= PTR_ERR(newfile1
);
1576 sock_release(sock2
);
1580 newfile2
= sock_alloc_file(sock2
, flags
, NULL
);
1581 if (IS_ERR(newfile2
)) {
1582 err
= PTR_ERR(newfile2
);
1587 audit_fd_pair(fd1
, fd2
);
1589 fd_install(fd1
, newfile1
);
1590 fd_install(fd2
, newfile2
);
1599 SYSCALL_DEFINE4(socketpair
, int, family
, int, type
, int, protocol
,
1600 int __user
*, usockvec
)
1602 return __sys_socketpair(family
, type
, protocol
, usockvec
);
1606 * Bind a name to a socket. Nothing much to do here since it's
1607 * the protocol's responsibility to handle the local address.
1609 * We move the socket address to kernel space before we call
1610 * the protocol layer (having also checked the address is ok).
1613 int __sys_bind(int fd
, struct sockaddr __user
*umyaddr
, int addrlen
)
1615 struct socket
*sock
;
1616 struct sockaddr_storage address
;
1617 int err
, fput_needed
;
1619 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1621 err
= move_addr_to_kernel(umyaddr
, addrlen
, &address
);
1623 err
= security_socket_bind(sock
,
1624 (struct sockaddr
*)&address
,
1627 err
= sock
->ops
->bind(sock
,
1631 fput_light(sock
->file
, fput_needed
);
1636 SYSCALL_DEFINE3(bind
, int, fd
, struct sockaddr __user
*, umyaddr
, int, addrlen
)
1638 return __sys_bind(fd
, umyaddr
, addrlen
);
1642 * Perform a listen. Basically, we allow the protocol to do anything
1643 * necessary for a listen, and if that works, we mark the socket as
1644 * ready for listening.
1647 int __sys_listen(int fd
, int backlog
)
1649 struct socket
*sock
;
1650 int err
, fput_needed
;
1653 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1655 somaxconn
= sock_net(sock
->sk
)->core
.sysctl_somaxconn
;
1656 if ((unsigned int)backlog
> somaxconn
)
1657 backlog
= somaxconn
;
1659 err
= security_socket_listen(sock
, backlog
);
1661 err
= sock
->ops
->listen(sock
, backlog
);
1663 fput_light(sock
->file
, fput_needed
);
1668 SYSCALL_DEFINE2(listen
, int, fd
, int, backlog
)
1670 return __sys_listen(fd
, backlog
);
1674 * For accept, we attempt to create a new socket, set up the link
1675 * with the client, wake up the client, then return the new
1676 * connected fd. We collect the address of the connector in kernel
1677 * space and move it to user at the very end. This is unclean because
1678 * we open the socket then return an error.
1680 * 1003.1g adds the ability to recvmsg() to query connection pending
1681 * status to recvmsg. We need to add that support in a way thats
1682 * clean when we restructure accept also.
1685 int __sys_accept4(int fd
, struct sockaddr __user
*upeer_sockaddr
,
1686 int __user
*upeer_addrlen
, int flags
)
1688 struct socket
*sock
, *newsock
;
1689 struct file
*newfile
;
1690 int err
, len
, newfd
, fput_needed
;
1691 struct sockaddr_storage address
;
1693 if (flags
& ~(SOCK_CLOEXEC
| SOCK_NONBLOCK
))
1696 if (SOCK_NONBLOCK
!= O_NONBLOCK
&& (flags
& SOCK_NONBLOCK
))
1697 flags
= (flags
& ~SOCK_NONBLOCK
) | O_NONBLOCK
;
1699 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1704 newsock
= sock_alloc();
1708 newsock
->type
= sock
->type
;
1709 newsock
->ops
= sock
->ops
;
1712 * We don't need try_module_get here, as the listening socket (sock)
1713 * has the protocol module (sock->ops->owner) held.
1715 __module_get(newsock
->ops
->owner
);
1717 newfd
= get_unused_fd_flags(flags
);
1718 if (unlikely(newfd
< 0)) {
1720 sock_release(newsock
);
1723 newfile
= sock_alloc_file(newsock
, flags
, sock
->sk
->sk_prot_creator
->name
);
1724 if (IS_ERR(newfile
)) {
1725 err
= PTR_ERR(newfile
);
1726 put_unused_fd(newfd
);
1730 err
= security_socket_accept(sock
, newsock
);
1734 err
= sock
->ops
->accept(sock
, newsock
, sock
->file
->f_flags
, false);
1738 if (upeer_sockaddr
) {
1739 len
= newsock
->ops
->getname(newsock
,
1740 (struct sockaddr
*)&address
, 2);
1742 err
= -ECONNABORTED
;
1745 err
= move_addr_to_user(&address
,
1746 len
, upeer_sockaddr
, upeer_addrlen
);
1751 /* File flags are not inherited via accept() unlike another OSes. */
1753 fd_install(newfd
, newfile
);
1757 fput_light(sock
->file
, fput_needed
);
1762 put_unused_fd(newfd
);
1766 SYSCALL_DEFINE4(accept4
, int, fd
, struct sockaddr __user
*, upeer_sockaddr
,
1767 int __user
*, upeer_addrlen
, int, flags
)
1769 return __sys_accept4(fd
, upeer_sockaddr
, upeer_addrlen
, flags
);
1772 SYSCALL_DEFINE3(accept
, int, fd
, struct sockaddr __user
*, upeer_sockaddr
,
1773 int __user
*, upeer_addrlen
)
1775 return __sys_accept4(fd
, upeer_sockaddr
, upeer_addrlen
, 0);
1779 * Attempt to connect to a socket with the server address. The address
1780 * is in user space so we verify it is OK and move it to kernel space.
1782 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
1785 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
1786 * other SEQPACKET protocols that take time to connect() as it doesn't
1787 * include the -EINPROGRESS status for such sockets.
1790 int __sys_connect(int fd
, struct sockaddr __user
*uservaddr
, int addrlen
)
1792 struct socket
*sock
;
1793 struct sockaddr_storage address
;
1794 int err
, fput_needed
;
1796 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1799 err
= move_addr_to_kernel(uservaddr
, addrlen
, &address
);
1804 security_socket_connect(sock
, (struct sockaddr
*)&address
, addrlen
);
1808 err
= sock
->ops
->connect(sock
, (struct sockaddr
*)&address
, addrlen
,
1809 sock
->file
->f_flags
);
1811 fput_light(sock
->file
, fput_needed
);
1816 SYSCALL_DEFINE3(connect
, int, fd
, struct sockaddr __user
*, uservaddr
,
1819 return __sys_connect(fd
, uservaddr
, addrlen
);
1823 * Get the local address ('name') of a socket object. Move the obtained
1824 * name to user space.
1827 int __sys_getsockname(int fd
, struct sockaddr __user
*usockaddr
,
1828 int __user
*usockaddr_len
)
1830 struct socket
*sock
;
1831 struct sockaddr_storage address
;
1832 int err
, fput_needed
;
1834 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1838 err
= security_socket_getsockname(sock
);
1842 err
= sock
->ops
->getname(sock
, (struct sockaddr
*)&address
, 0);
1845 /* "err" is actually length in this case */
1846 err
= move_addr_to_user(&address
, err
, usockaddr
, usockaddr_len
);
1849 fput_light(sock
->file
, fput_needed
);
1854 SYSCALL_DEFINE3(getsockname
, int, fd
, struct sockaddr __user
*, usockaddr
,
1855 int __user
*, usockaddr_len
)
1857 return __sys_getsockname(fd
, usockaddr
, usockaddr_len
);
1861 * Get the remote address ('name') of a socket object. Move the obtained
1862 * name to user space.
1865 int __sys_getpeername(int fd
, struct sockaddr __user
*usockaddr
,
1866 int __user
*usockaddr_len
)
1868 struct socket
*sock
;
1869 struct sockaddr_storage address
;
1870 int err
, fput_needed
;
1872 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1874 err
= security_socket_getpeername(sock
);
1876 fput_light(sock
->file
, fput_needed
);
1880 err
= sock
->ops
->getname(sock
, (struct sockaddr
*)&address
, 1);
1882 /* "err" is actually length in this case */
1883 err
= move_addr_to_user(&address
, err
, usockaddr
,
1885 fput_light(sock
->file
, fput_needed
);
1890 SYSCALL_DEFINE3(getpeername
, int, fd
, struct sockaddr __user
*, usockaddr
,
1891 int __user
*, usockaddr_len
)
1893 return __sys_getpeername(fd
, usockaddr
, usockaddr_len
);
1897 * Send a datagram to a given address. We move the address into kernel
1898 * space and check the user space data area is readable before invoking
1901 int __sys_sendto(int fd
, void __user
*buff
, size_t len
, unsigned int flags
,
1902 struct sockaddr __user
*addr
, int addr_len
)
1904 struct socket
*sock
;
1905 struct sockaddr_storage address
;
1911 err
= import_single_range(WRITE
, buff
, len
, &iov
, &msg
.msg_iter
);
1914 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1918 msg
.msg_name
= NULL
;
1919 msg
.msg_control
= NULL
;
1920 msg
.msg_controllen
= 0;
1921 msg
.msg_namelen
= 0;
1923 err
= move_addr_to_kernel(addr
, addr_len
, &address
);
1926 msg
.msg_name
= (struct sockaddr
*)&address
;
1927 msg
.msg_namelen
= addr_len
;
1929 if (sock
->file
->f_flags
& O_NONBLOCK
)
1930 flags
|= MSG_DONTWAIT
;
1931 msg
.msg_flags
= flags
;
1932 err
= sock_sendmsg(sock
, &msg
);
1935 fput_light(sock
->file
, fput_needed
);
1940 SYSCALL_DEFINE6(sendto
, int, fd
, void __user
*, buff
, size_t, len
,
1941 unsigned int, flags
, struct sockaddr __user
*, addr
,
1944 return __sys_sendto(fd
, buff
, len
, flags
, addr
, addr_len
);
1948 * Send a datagram down a socket.
1951 SYSCALL_DEFINE4(send
, int, fd
, void __user
*, buff
, size_t, len
,
1952 unsigned int, flags
)
1954 return __sys_sendto(fd
, buff
, len
, flags
, NULL
, 0);
1958 * Receive a frame from the socket and optionally record the address of the
1959 * sender. We verify the buffers are writable and if needed move the
1960 * sender address from kernel to user space.
1962 int __sys_recvfrom(int fd
, void __user
*ubuf
, size_t size
, unsigned int flags
,
1963 struct sockaddr __user
*addr
, int __user
*addr_len
)
1965 struct socket
*sock
;
1968 struct sockaddr_storage address
;
1972 err
= import_single_range(READ
, ubuf
, size
, &iov
, &msg
.msg_iter
);
1975 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1979 msg
.msg_control
= NULL
;
1980 msg
.msg_controllen
= 0;
1981 /* Save some cycles and don't copy the address if not needed */
1982 msg
.msg_name
= addr
? (struct sockaddr
*)&address
: NULL
;
1983 /* We assume all kernel code knows the size of sockaddr_storage */
1984 msg
.msg_namelen
= 0;
1985 msg
.msg_iocb
= NULL
;
1987 if (sock
->file
->f_flags
& O_NONBLOCK
)
1988 flags
|= MSG_DONTWAIT
;
1989 err
= sock_recvmsg(sock
, &msg
, flags
);
1991 if (err
>= 0 && addr
!= NULL
) {
1992 err2
= move_addr_to_user(&address
,
1993 msg
.msg_namelen
, addr
, addr_len
);
1998 fput_light(sock
->file
, fput_needed
);
2003 SYSCALL_DEFINE6(recvfrom
, int, fd
, void __user
*, ubuf
, size_t, size
,
2004 unsigned int, flags
, struct sockaddr __user
*, addr
,
2005 int __user
*, addr_len
)
2007 return __sys_recvfrom(fd
, ubuf
, size
, flags
, addr
, addr_len
);
2011 * Receive a datagram from a socket.
2014 SYSCALL_DEFINE4(recv
, int, fd
, void __user
*, ubuf
, size_t, size
,
2015 unsigned int, flags
)
2017 return __sys_recvfrom(fd
, ubuf
, size
, flags
, NULL
, NULL
);
2021 * Set a socket option. Because we don't know the option lengths we have
2022 * to pass the user mode parameter for the protocols to sort out.
2025 static int __sys_setsockopt(int fd
, int level
, int optname
,
2026 char __user
*optval
, int optlen
)
2028 int err
, fput_needed
;
2029 struct socket
*sock
;
2034 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
2036 err
= security_socket_setsockopt(sock
, level
, optname
);
2040 if (level
== SOL_SOCKET
)
2042 sock_setsockopt(sock
, level
, optname
, optval
,
2046 sock
->ops
->setsockopt(sock
, level
, optname
, optval
,
2049 fput_light(sock
->file
, fput_needed
);
2054 SYSCALL_DEFINE5(setsockopt
, int, fd
, int, level
, int, optname
,
2055 char __user
*, optval
, int, optlen
)
2057 return __sys_setsockopt(fd
, level
, optname
, optval
, optlen
);
2061 * Get a socket option. Because we don't know the option lengths we have
2062 * to pass a user mode parameter for the protocols to sort out.
2065 static int __sys_getsockopt(int fd
, int level
, int optname
,
2066 char __user
*optval
, int __user
*optlen
)
2068 int err
, fput_needed
;
2069 struct socket
*sock
;
2071 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
2073 err
= security_socket_getsockopt(sock
, level
, optname
);
2077 if (level
== SOL_SOCKET
)
2079 sock_getsockopt(sock
, level
, optname
, optval
,
2083 sock
->ops
->getsockopt(sock
, level
, optname
, optval
,
2086 fput_light(sock
->file
, fput_needed
);
2091 SYSCALL_DEFINE5(getsockopt
, int, fd
, int, level
, int, optname
,
2092 char __user
*, optval
, int __user
*, optlen
)
2094 return __sys_getsockopt(fd
, level
, optname
, optval
, optlen
);
2098 * Shutdown a socket.
2101 int __sys_shutdown(int fd
, int how
)
2103 int err
, fput_needed
;
2104 struct socket
*sock
;
2106 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
2108 err
= security_socket_shutdown(sock
, how
);
2110 err
= sock
->ops
->shutdown(sock
, how
);
2111 fput_light(sock
->file
, fput_needed
);
2116 SYSCALL_DEFINE2(shutdown
, int, fd
, int, how
)
2118 return __sys_shutdown(fd
, how
);
2121 /* A couple of helpful macros for getting the address of the 32/64 bit
2122 * fields which are the same type (int / unsigned) on our platforms.
2124 #define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
2125 #define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
2126 #define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
2128 struct used_address
{
2129 struct sockaddr_storage name
;
2130 unsigned int name_len
;
2133 static int copy_msghdr_from_user(struct msghdr
*kmsg
,
2134 struct user_msghdr __user
*umsg
,
2135 struct sockaddr __user
**save_addr
,
2138 struct user_msghdr msg
;
2141 if (copy_from_user(&msg
, umsg
, sizeof(*umsg
)))
2144 kmsg
->msg_control
= (void __force
*)msg
.msg_control
;
2145 kmsg
->msg_controllen
= msg
.msg_controllen
;
2146 kmsg
->msg_flags
= msg
.msg_flags
;
2148 kmsg
->msg_namelen
= msg
.msg_namelen
;
2150 kmsg
->msg_namelen
= 0;
2152 if (kmsg
->msg_namelen
< 0)
2155 if (kmsg
->msg_namelen
> sizeof(struct sockaddr_storage
))
2156 kmsg
->msg_namelen
= sizeof(struct sockaddr_storage
);
2159 *save_addr
= msg
.msg_name
;
2161 if (msg
.msg_name
&& kmsg
->msg_namelen
) {
2163 err
= move_addr_to_kernel(msg
.msg_name
,
2170 kmsg
->msg_name
= NULL
;
2171 kmsg
->msg_namelen
= 0;
2174 if (msg
.msg_iovlen
> UIO_MAXIOV
)
2177 kmsg
->msg_iocb
= NULL
;
2179 return import_iovec(save_addr
? READ
: WRITE
,
2180 msg
.msg_iov
, msg
.msg_iovlen
,
2181 UIO_FASTIOV
, iov
, &kmsg
->msg_iter
);
2184 static int ___sys_sendmsg(struct socket
*sock
, struct user_msghdr __user
*msg
,
2185 struct msghdr
*msg_sys
, unsigned int flags
,
2186 struct used_address
*used_address
,
2187 unsigned int allowed_msghdr_flags
)
2189 struct compat_msghdr __user
*msg_compat
=
2190 (struct compat_msghdr __user
*)msg
;
2191 struct sockaddr_storage address
;
2192 struct iovec iovstack
[UIO_FASTIOV
], *iov
= iovstack
;
2193 unsigned char ctl
[sizeof(struct cmsghdr
) + 20]
2194 __aligned(sizeof(__kernel_size_t
));
2195 /* 20 is size of ipv6_pktinfo */
2196 unsigned char *ctl_buf
= ctl
;
2200 msg_sys
->msg_name
= &address
;
2202 if (MSG_CMSG_COMPAT
& flags
)
2203 err
= get_compat_msghdr(msg_sys
, msg_compat
, NULL
, &iov
);
2205 err
= copy_msghdr_from_user(msg_sys
, msg
, NULL
, &iov
);
2211 if (msg_sys
->msg_controllen
> INT_MAX
)
2213 flags
|= (msg_sys
->msg_flags
& allowed_msghdr_flags
);
2214 ctl_len
= msg_sys
->msg_controllen
;
2215 if ((MSG_CMSG_COMPAT
& flags
) && ctl_len
) {
2217 cmsghdr_from_user_compat_to_kern(msg_sys
, sock
->sk
, ctl
,
2221 ctl_buf
= msg_sys
->msg_control
;
2222 ctl_len
= msg_sys
->msg_controllen
;
2223 } else if (ctl_len
) {
2224 BUILD_BUG_ON(sizeof(struct cmsghdr
) !=
2225 CMSG_ALIGN(sizeof(struct cmsghdr
)));
2226 if (ctl_len
> sizeof(ctl
)) {
2227 ctl_buf
= sock_kmalloc(sock
->sk
, ctl_len
, GFP_KERNEL
);
2228 if (ctl_buf
== NULL
)
2233 * Careful! Before this, msg_sys->msg_control contains a user pointer.
2234 * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
2235 * checking falls down on this.
2237 if (copy_from_user(ctl_buf
,
2238 (void __user __force
*)msg_sys
->msg_control
,
2241 msg_sys
->msg_control
= ctl_buf
;
2243 msg_sys
->msg_flags
= flags
;
2245 if (sock
->file
->f_flags
& O_NONBLOCK
)
2246 msg_sys
->msg_flags
|= MSG_DONTWAIT
;
2248 * If this is sendmmsg() and current destination address is same as
2249 * previously succeeded address, omit asking LSM's decision.
2250 * used_address->name_len is initialized to UINT_MAX so that the first
2251 * destination address never matches.
2253 if (used_address
&& msg_sys
->msg_name
&&
2254 used_address
->name_len
== msg_sys
->msg_namelen
&&
2255 !memcmp(&used_address
->name
, msg_sys
->msg_name
,
2256 used_address
->name_len
)) {
2257 err
= sock_sendmsg_nosec(sock
, msg_sys
);
2260 err
= sock_sendmsg(sock
, msg_sys
);
2262 * If this is sendmmsg() and sending to current destination address was
2263 * successful, remember it.
2265 if (used_address
&& err
>= 0) {
2266 used_address
->name_len
= msg_sys
->msg_namelen
;
2267 if (msg_sys
->msg_name
)
2268 memcpy(&used_address
->name
, msg_sys
->msg_name
,
2269 used_address
->name_len
);
2274 sock_kfree_s(sock
->sk
, ctl_buf
, ctl_len
);
2281 * BSD sendmsg interface
2284 long __sys_sendmsg(int fd
, struct user_msghdr __user
*msg
, unsigned int flags
,
2285 bool forbid_cmsg_compat
)
2287 int fput_needed
, err
;
2288 struct msghdr msg_sys
;
2289 struct socket
*sock
;
2291 if (forbid_cmsg_compat
&& (flags
& MSG_CMSG_COMPAT
))
2294 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
2298 err
= ___sys_sendmsg(sock
, msg
, &msg_sys
, flags
, NULL
, 0);
2300 fput_light(sock
->file
, fput_needed
);
2305 SYSCALL_DEFINE3(sendmsg
, int, fd
, struct user_msghdr __user
*, msg
, unsigned int, flags
)
2307 return __sys_sendmsg(fd
, msg
, flags
, true);
2311 * Linux sendmmsg interface
2314 int __sys_sendmmsg(int fd
, struct mmsghdr __user
*mmsg
, unsigned int vlen
,
2315 unsigned int flags
, bool forbid_cmsg_compat
)
2317 int fput_needed
, err
, datagrams
;
2318 struct socket
*sock
;
2319 struct mmsghdr __user
*entry
;
2320 struct compat_mmsghdr __user
*compat_entry
;
2321 struct msghdr msg_sys
;
2322 struct used_address used_address
;
2323 unsigned int oflags
= flags
;
2325 if (forbid_cmsg_compat
&& (flags
& MSG_CMSG_COMPAT
))
2328 if (vlen
> UIO_MAXIOV
)
2333 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
2337 used_address
.name_len
= UINT_MAX
;
2339 compat_entry
= (struct compat_mmsghdr __user
*)mmsg
;
2343 while (datagrams
< vlen
) {
2344 if (datagrams
== vlen
- 1)
2347 if (MSG_CMSG_COMPAT
& flags
) {
2348 err
= ___sys_sendmsg(sock
, (struct user_msghdr __user
*)compat_entry
,
2349 &msg_sys
, flags
, &used_address
, MSG_EOR
);
2352 err
= __put_user(err
, &compat_entry
->msg_len
);
2355 err
= ___sys_sendmsg(sock
,
2356 (struct user_msghdr __user
*)entry
,
2357 &msg_sys
, flags
, &used_address
, MSG_EOR
);
2360 err
= put_user(err
, &entry
->msg_len
);
2367 if (msg_data_left(&msg_sys
))
2372 fput_light(sock
->file
, fput_needed
);
2374 /* We only return an error if no datagrams were able to be sent */
2381 SYSCALL_DEFINE4(sendmmsg
, int, fd
, struct mmsghdr __user
*, mmsg
,
2382 unsigned int, vlen
, unsigned int, flags
)
2384 return __sys_sendmmsg(fd
, mmsg
, vlen
, flags
, true);
2387 static int ___sys_recvmsg(struct socket
*sock
, struct user_msghdr __user
*msg
,
2388 struct msghdr
*msg_sys
, unsigned int flags
, int nosec
)
2390 struct compat_msghdr __user
*msg_compat
=
2391 (struct compat_msghdr __user
*)msg
;
2392 struct iovec iovstack
[UIO_FASTIOV
];
2393 struct iovec
*iov
= iovstack
;
2394 unsigned long cmsg_ptr
;
2398 /* kernel mode address */
2399 struct sockaddr_storage addr
;
2401 /* user mode address pointers */
2402 struct sockaddr __user
*uaddr
;
2403 int __user
*uaddr_len
= COMPAT_NAMELEN(msg
);
2405 msg_sys
->msg_name
= &addr
;
2407 if (MSG_CMSG_COMPAT
& flags
)
2408 err
= get_compat_msghdr(msg_sys
, msg_compat
, &uaddr
, &iov
);
2410 err
= copy_msghdr_from_user(msg_sys
, msg
, &uaddr
, &iov
);
2414 cmsg_ptr
= (unsigned long)msg_sys
->msg_control
;
2415 msg_sys
->msg_flags
= flags
& (MSG_CMSG_CLOEXEC
|MSG_CMSG_COMPAT
);
2417 /* We assume all kernel code knows the size of sockaddr_storage */
2418 msg_sys
->msg_namelen
= 0;
2420 if (sock
->file
->f_flags
& O_NONBLOCK
)
2421 flags
|= MSG_DONTWAIT
;
2422 err
= (nosec
? sock_recvmsg_nosec
: sock_recvmsg
)(sock
, msg_sys
, flags
);
2427 if (uaddr
!= NULL
) {
2428 err
= move_addr_to_user(&addr
,
2429 msg_sys
->msg_namelen
, uaddr
,
2434 err
= __put_user((msg_sys
->msg_flags
& ~MSG_CMSG_COMPAT
),
2438 if (MSG_CMSG_COMPAT
& flags
)
2439 err
= __put_user((unsigned long)msg_sys
->msg_control
- cmsg_ptr
,
2440 &msg_compat
->msg_controllen
);
2442 err
= __put_user((unsigned long)msg_sys
->msg_control
- cmsg_ptr
,
2443 &msg
->msg_controllen
);
2454 * BSD recvmsg interface
2457 long __sys_recvmsg(int fd
, struct user_msghdr __user
*msg
, unsigned int flags
,
2458 bool forbid_cmsg_compat
)
2460 int fput_needed
, err
;
2461 struct msghdr msg_sys
;
2462 struct socket
*sock
;
2464 if (forbid_cmsg_compat
&& (flags
& MSG_CMSG_COMPAT
))
2467 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
2471 err
= ___sys_recvmsg(sock
, msg
, &msg_sys
, flags
, 0);
2473 fput_light(sock
->file
, fput_needed
);
2478 SYSCALL_DEFINE3(recvmsg
, int, fd
, struct user_msghdr __user
*, msg
,
2479 unsigned int, flags
)
2481 return __sys_recvmsg(fd
, msg
, flags
, true);
2485 * Linux recvmmsg interface
2488 static int do_recvmmsg(int fd
, struct mmsghdr __user
*mmsg
,
2489 unsigned int vlen
, unsigned int flags
,
2490 struct timespec64
*timeout
)
2492 int fput_needed
, err
, datagrams
;
2493 struct socket
*sock
;
2494 struct mmsghdr __user
*entry
;
2495 struct compat_mmsghdr __user
*compat_entry
;
2496 struct msghdr msg_sys
;
2497 struct timespec64 end_time
;
2498 struct timespec64 timeout64
;
2501 poll_select_set_timeout(&end_time
, timeout
->tv_sec
,
2507 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
2511 if (likely(!(flags
& MSG_ERRQUEUE
))) {
2512 err
= sock_error(sock
->sk
);
2520 compat_entry
= (struct compat_mmsghdr __user
*)mmsg
;
2522 while (datagrams
< vlen
) {
2524 * No need to ask LSM for more than the first datagram.
2526 if (MSG_CMSG_COMPAT
& flags
) {
2527 err
= ___sys_recvmsg(sock
, (struct user_msghdr __user
*)compat_entry
,
2528 &msg_sys
, flags
& ~MSG_WAITFORONE
,
2532 err
= __put_user(err
, &compat_entry
->msg_len
);
2535 err
= ___sys_recvmsg(sock
,
2536 (struct user_msghdr __user
*)entry
,
2537 &msg_sys
, flags
& ~MSG_WAITFORONE
,
2541 err
= put_user(err
, &entry
->msg_len
);
2549 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
2550 if (flags
& MSG_WAITFORONE
)
2551 flags
|= MSG_DONTWAIT
;
2554 ktime_get_ts64(&timeout64
);
2555 *timeout
= timespec64_sub(end_time
, timeout64
);
2556 if (timeout
->tv_sec
< 0) {
2557 timeout
->tv_sec
= timeout
->tv_nsec
= 0;
2561 /* Timeout, return less than vlen datagrams */
2562 if (timeout
->tv_nsec
== 0 && timeout
->tv_sec
== 0)
2566 /* Out of band data, return right away */
2567 if (msg_sys
.msg_flags
& MSG_OOB
)
2575 if (datagrams
== 0) {
2581 * We may return less entries than requested (vlen) if the
2582 * sock is non block and there aren't enough datagrams...
2584 if (err
!= -EAGAIN
) {
2586 * ... or if recvmsg returns an error after we
2587 * received some datagrams, where we record the
2588 * error to return on the next call or if the
2589 * app asks about it using getsockopt(SO_ERROR).
2591 sock
->sk
->sk_err
= -err
;
2594 fput_light(sock
->file
, fput_needed
);
2599 int __sys_recvmmsg(int fd
, struct mmsghdr __user
*mmsg
,
2600 unsigned int vlen
, unsigned int flags
,
2601 struct __kernel_timespec __user
*timeout
,
2602 struct old_timespec32 __user
*timeout32
)
2605 struct timespec64 timeout_sys
;
2607 if (timeout
&& get_timespec64(&timeout_sys
, timeout
))
2610 if (timeout32
&& get_old_timespec32(&timeout_sys
, timeout32
))
2613 if (!timeout
&& !timeout32
)
2614 return do_recvmmsg(fd
, mmsg
, vlen
, flags
, NULL
);
2616 datagrams
= do_recvmmsg(fd
, mmsg
, vlen
, flags
, &timeout_sys
);
2621 if (timeout
&& put_timespec64(&timeout_sys
, timeout
))
2622 datagrams
= -EFAULT
;
2624 if (timeout32
&& put_old_timespec32(&timeout_sys
, timeout32
))
2625 datagrams
= -EFAULT
;
2630 SYSCALL_DEFINE5(recvmmsg
, int, fd
, struct mmsghdr __user
*, mmsg
,
2631 unsigned int, vlen
, unsigned int, flags
,
2632 struct __kernel_timespec __user
*, timeout
)
2634 if (flags
& MSG_CMSG_COMPAT
)
2637 return __sys_recvmmsg(fd
, mmsg
, vlen
, flags
, timeout
, NULL
);
2640 #ifdef CONFIG_COMPAT_32BIT_TIME
2641 SYSCALL_DEFINE5(recvmmsg_time32
, int, fd
, struct mmsghdr __user
*, mmsg
,
2642 unsigned int, vlen
, unsigned int, flags
,
2643 struct old_timespec32 __user
*, timeout
)
2645 if (flags
& MSG_CMSG_COMPAT
)
2648 return __sys_recvmmsg(fd
, mmsg
, vlen
, flags
, NULL
, timeout
);
2652 #ifdef __ARCH_WANT_SYS_SOCKETCALL
2653 /* Argument list sizes for sys_socketcall */
2654 #define AL(x) ((x) * sizeof(unsigned long))
2655 static const unsigned char nargs
[21] = {
2656 AL(0), AL(3), AL(3), AL(3), AL(2), AL(3),
2657 AL(3), AL(3), AL(4), AL(4), AL(4), AL(6),
2658 AL(6), AL(2), AL(5), AL(5), AL(3), AL(3),
2665 * System call vectors.
2667 * Argument checking cleaned up. Saved 20% in size.
2668 * This function doesn't need to set the kernel lock because
2669 * it is set by the callees.
2672 SYSCALL_DEFINE2(socketcall
, int, call
, unsigned long __user
*, args
)
2674 unsigned long a
[AUDITSC_ARGS
];
2675 unsigned long a0
, a1
;
2679 if (call
< 1 || call
> SYS_SENDMMSG
)
2681 call
= array_index_nospec(call
, SYS_SENDMMSG
+ 1);
2684 if (len
> sizeof(a
))
2687 /* copy_from_user should be SMP safe. */
2688 if (copy_from_user(a
, args
, len
))
2691 err
= audit_socketcall(nargs
[call
] / sizeof(unsigned long), a
);
2700 err
= __sys_socket(a0
, a1
, a
[2]);
2703 err
= __sys_bind(a0
, (struct sockaddr __user
*)a1
, a
[2]);
2706 err
= __sys_connect(a0
, (struct sockaddr __user
*)a1
, a
[2]);
2709 err
= __sys_listen(a0
, a1
);
2712 err
= __sys_accept4(a0
, (struct sockaddr __user
*)a1
,
2713 (int __user
*)a
[2], 0);
2715 case SYS_GETSOCKNAME
:
2717 __sys_getsockname(a0
, (struct sockaddr __user
*)a1
,
2718 (int __user
*)a
[2]);
2720 case SYS_GETPEERNAME
:
2722 __sys_getpeername(a0
, (struct sockaddr __user
*)a1
,
2723 (int __user
*)a
[2]);
2725 case SYS_SOCKETPAIR
:
2726 err
= __sys_socketpair(a0
, a1
, a
[2], (int __user
*)a
[3]);
2729 err
= __sys_sendto(a0
, (void __user
*)a1
, a
[2], a
[3],
2733 err
= __sys_sendto(a0
, (void __user
*)a1
, a
[2], a
[3],
2734 (struct sockaddr __user
*)a
[4], a
[5]);
2737 err
= __sys_recvfrom(a0
, (void __user
*)a1
, a
[2], a
[3],
2741 err
= __sys_recvfrom(a0
, (void __user
*)a1
, a
[2], a
[3],
2742 (struct sockaddr __user
*)a
[4],
2743 (int __user
*)a
[5]);
2746 err
= __sys_shutdown(a0
, a1
);
2748 case SYS_SETSOCKOPT
:
2749 err
= __sys_setsockopt(a0
, a1
, a
[2], (char __user
*)a
[3],
2752 case SYS_GETSOCKOPT
:
2754 __sys_getsockopt(a0
, a1
, a
[2], (char __user
*)a
[3],
2755 (int __user
*)a
[4]);
2758 err
= __sys_sendmsg(a0
, (struct user_msghdr __user
*)a1
,
2762 err
= __sys_sendmmsg(a0
, (struct mmsghdr __user
*)a1
, a
[2],
2766 err
= __sys_recvmsg(a0
, (struct user_msghdr __user
*)a1
,
2770 if (IS_ENABLED(CONFIG_64BIT
) || !IS_ENABLED(CONFIG_64BIT_TIME
))
2771 err
= __sys_recvmmsg(a0
, (struct mmsghdr __user
*)a1
,
2773 (struct __kernel_timespec __user
*)a
[4],
2776 err
= __sys_recvmmsg(a0
, (struct mmsghdr __user
*)a1
,
2778 (struct old_timespec32 __user
*)a
[4]);
2781 err
= __sys_accept4(a0
, (struct sockaddr __user
*)a1
,
2782 (int __user
*)a
[2], a
[3]);
2791 #endif /* __ARCH_WANT_SYS_SOCKETCALL */
2794 * sock_register - add a socket protocol handler
2795 * @ops: description of protocol
2797 * This function is called by a protocol handler that wants to
2798 * advertise its address family, and have it linked into the
2799 * socket interface. The value ops->family corresponds to the
2800 * socket system call protocol family.
2802 int sock_register(const struct net_proto_family
*ops
)
2806 if (ops
->family
>= NPROTO
) {
2807 pr_crit("protocol %d >= NPROTO(%d)\n", ops
->family
, NPROTO
);
2811 spin_lock(&net_family_lock
);
2812 if (rcu_dereference_protected(net_families
[ops
->family
],
2813 lockdep_is_held(&net_family_lock
)))
2816 rcu_assign_pointer(net_families
[ops
->family
], ops
);
2819 spin_unlock(&net_family_lock
);
2821 pr_info("NET: Registered protocol family %d\n", ops
->family
);
2824 EXPORT_SYMBOL(sock_register
);
2827 * sock_unregister - remove a protocol handler
2828 * @family: protocol family to remove
2830 * This function is called by a protocol handler that wants to
2831 * remove its address family, and have it unlinked from the
2832 * new socket creation.
2834 * If protocol handler is a module, then it can use module reference
2835 * counts to protect against new references. If protocol handler is not
2836 * a module then it needs to provide its own protection in
2837 * the ops->create routine.
2839 void sock_unregister(int family
)
2841 BUG_ON(family
< 0 || family
>= NPROTO
);
2843 spin_lock(&net_family_lock
);
2844 RCU_INIT_POINTER(net_families
[family
], NULL
);
2845 spin_unlock(&net_family_lock
);
2849 pr_info("NET: Unregistered protocol family %d\n", family
);
2851 EXPORT_SYMBOL(sock_unregister
);
2853 bool sock_is_registered(int family
)
2855 return family
< NPROTO
&& rcu_access_pointer(net_families
[family
]);
2858 static int __init
sock_init(void)
2862 * Initialize the network sysctl infrastructure.
2864 err
= net_sysctl_init();
2869 * Initialize skbuff SLAB cache
2874 * Initialize the protocols module.
2879 err
= register_filesystem(&sock_fs_type
);
2882 sock_mnt
= kern_mount(&sock_fs_type
);
2883 if (IS_ERR(sock_mnt
)) {
2884 err
= PTR_ERR(sock_mnt
);
2888 /* The real protocol initialization is performed in later initcalls.
2891 #ifdef CONFIG_NETFILTER
2892 err
= netfilter_init();
2897 ptp_classifier_init();
2903 unregister_filesystem(&sock_fs_type
);
2908 core_initcall(sock_init
); /* early initcall */
2910 #ifdef CONFIG_PROC_FS
2911 void socket_seq_show(struct seq_file
*seq
)
2913 seq_printf(seq
, "sockets: used %d\n",
2914 sock_inuse_get(seq
->private));
2916 #endif /* CONFIG_PROC_FS */
2918 #ifdef CONFIG_COMPAT
2919 static int do_siocgstamp(struct net
*net
, struct socket
*sock
,
2920 unsigned int cmd
, void __user
*up
)
2922 mm_segment_t old_fs
= get_fs();
2927 err
= sock_do_ioctl(net
, sock
, cmd
, (unsigned long)&ktv
);
2930 err
= compat_put_timeval(&ktv
, up
);
2935 static int do_siocgstampns(struct net
*net
, struct socket
*sock
,
2936 unsigned int cmd
, void __user
*up
)
2938 mm_segment_t old_fs
= get_fs();
2939 struct timespec kts
;
2943 err
= sock_do_ioctl(net
, sock
, cmd
, (unsigned long)&kts
);
2946 err
= compat_put_timespec(&kts
, up
);
2951 static int compat_dev_ifconf(struct net
*net
, struct compat_ifconf __user
*uifc32
)
2953 struct compat_ifconf ifc32
;
2957 if (copy_from_user(&ifc32
, uifc32
, sizeof(struct compat_ifconf
)))
2960 ifc
.ifc_len
= ifc32
.ifc_len
;
2961 ifc
.ifc_req
= compat_ptr(ifc32
.ifcbuf
);
2964 err
= dev_ifconf(net
, &ifc
, sizeof(struct compat_ifreq
));
2969 ifc32
.ifc_len
= ifc
.ifc_len
;
2970 if (copy_to_user(uifc32
, &ifc32
, sizeof(struct compat_ifconf
)))
2976 static int ethtool_ioctl(struct net
*net
, struct compat_ifreq __user
*ifr32
)
2978 struct compat_ethtool_rxnfc __user
*compat_rxnfc
;
2979 bool convert_in
= false, convert_out
= false;
2980 size_t buf_size
= 0;
2981 struct ethtool_rxnfc __user
*rxnfc
= NULL
;
2983 u32 rule_cnt
= 0, actual_rule_cnt
;
2988 if (get_user(data
, &ifr32
->ifr_ifru
.ifru_data
))
2991 compat_rxnfc
= compat_ptr(data
);
2993 if (get_user(ethcmd
, &compat_rxnfc
->cmd
))
2996 /* Most ethtool structures are defined without padding.
2997 * Unfortunately struct ethtool_rxnfc is an exception.
3002 case ETHTOOL_GRXCLSRLALL
:
3003 /* Buffer size is variable */
3004 if (get_user(rule_cnt
, &compat_rxnfc
->rule_cnt
))
3006 if (rule_cnt
> KMALLOC_MAX_SIZE
/ sizeof(u32
))
3008 buf_size
+= rule_cnt
* sizeof(u32
);
3010 case ETHTOOL_GRXRINGS
:
3011 case ETHTOOL_GRXCLSRLCNT
:
3012 case ETHTOOL_GRXCLSRULE
:
3013 case ETHTOOL_SRXCLSRLINS
:
3016 case ETHTOOL_SRXCLSRLDEL
:
3017 buf_size
+= sizeof(struct ethtool_rxnfc
);
3019 rxnfc
= compat_alloc_user_space(buf_size
);
3023 if (copy_from_user(&ifr
.ifr_name
, &ifr32
->ifr_name
, IFNAMSIZ
))
3026 ifr
.ifr_data
= convert_in
? rxnfc
: (void __user
*)compat_rxnfc
;
3029 /* We expect there to be holes between fs.m_ext and
3030 * fs.ring_cookie and at the end of fs, but nowhere else.
3032 BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc
, fs
.m_ext
) +
3033 sizeof(compat_rxnfc
->fs
.m_ext
) !=
3034 offsetof(struct ethtool_rxnfc
, fs
.m_ext
) +
3035 sizeof(rxnfc
->fs
.m_ext
));
3037 offsetof(struct compat_ethtool_rxnfc
, fs
.location
) -
3038 offsetof(struct compat_ethtool_rxnfc
, fs
.ring_cookie
) !=
3039 offsetof(struct ethtool_rxnfc
, fs
.location
) -
3040 offsetof(struct ethtool_rxnfc
, fs
.ring_cookie
));
3042 if (copy_in_user(rxnfc
, compat_rxnfc
,
3043 (void __user
*)(&rxnfc
->fs
.m_ext
+ 1) -
3044 (void __user
*)rxnfc
) ||
3045 copy_in_user(&rxnfc
->fs
.ring_cookie
,
3046 &compat_rxnfc
->fs
.ring_cookie
,
3047 (void __user
*)(&rxnfc
->fs
.location
+ 1) -
3048 (void __user
*)&rxnfc
->fs
.ring_cookie
))
3050 if (ethcmd
== ETHTOOL_GRXCLSRLALL
) {
3051 if (put_user(rule_cnt
, &rxnfc
->rule_cnt
))
3053 } else if (copy_in_user(&rxnfc
->rule_cnt
,
3054 &compat_rxnfc
->rule_cnt
,
3055 sizeof(rxnfc
->rule_cnt
)))
3059 ret
= dev_ioctl(net
, SIOCETHTOOL
, &ifr
, NULL
);
3064 if (copy_in_user(compat_rxnfc
, rxnfc
,
3065 (const void __user
*)(&rxnfc
->fs
.m_ext
+ 1) -
3066 (const void __user
*)rxnfc
) ||
3067 copy_in_user(&compat_rxnfc
->fs
.ring_cookie
,
3068 &rxnfc
->fs
.ring_cookie
,
3069 (const void __user
*)(&rxnfc
->fs
.location
+ 1) -
3070 (const void __user
*)&rxnfc
->fs
.ring_cookie
) ||
3071 copy_in_user(&compat_rxnfc
->rule_cnt
, &rxnfc
->rule_cnt
,
3072 sizeof(rxnfc
->rule_cnt
)))
3075 if (ethcmd
== ETHTOOL_GRXCLSRLALL
) {
3076 /* As an optimisation, we only copy the actual
3077 * number of rules that the underlying
3078 * function returned. Since Mallory might
3079 * change the rule count in user memory, we
3080 * check that it is less than the rule count
3081 * originally given (as the user buffer size),
3082 * which has been range-checked.
3084 if (get_user(actual_rule_cnt
, &rxnfc
->rule_cnt
))
3086 if (actual_rule_cnt
< rule_cnt
)
3087 rule_cnt
= actual_rule_cnt
;
3088 if (copy_in_user(&compat_rxnfc
->rule_locs
[0],
3089 &rxnfc
->rule_locs
[0],
3090 rule_cnt
* sizeof(u32
)))
3098 static int compat_siocwandev(struct net
*net
, struct compat_ifreq __user
*uifr32
)
3100 compat_uptr_t uptr32
;
3105 if (copy_from_user(&ifr
, uifr32
, sizeof(struct compat_ifreq
)))
3108 if (get_user(uptr32
, &uifr32
->ifr_settings
.ifs_ifsu
))
3111 saved
= ifr
.ifr_settings
.ifs_ifsu
.raw_hdlc
;
3112 ifr
.ifr_settings
.ifs_ifsu
.raw_hdlc
= compat_ptr(uptr32
);
3114 err
= dev_ioctl(net
, SIOCWANDEV
, &ifr
, NULL
);
3116 ifr
.ifr_settings
.ifs_ifsu
.raw_hdlc
= saved
;
3117 if (copy_to_user(uifr32
, &ifr
, sizeof(struct compat_ifreq
)))
3123 /* Handle ioctls that use ifreq::ifr_data and just need struct ifreq converted */
3124 static int compat_ifr_data_ioctl(struct net
*net
, unsigned int cmd
,
3125 struct compat_ifreq __user
*u_ifreq32
)
3130 if (copy_from_user(ifreq
.ifr_name
, u_ifreq32
->ifr_name
, IFNAMSIZ
))
3132 if (get_user(data32
, &u_ifreq32
->ifr_data
))
3134 ifreq
.ifr_data
= compat_ptr(data32
);
3136 return dev_ioctl(net
, cmd
, &ifreq
, NULL
);
3139 static int compat_ifreq_ioctl(struct net
*net
, struct socket
*sock
,
3141 struct compat_ifreq __user
*uifr32
)
3143 struct ifreq __user
*uifr
;
3146 /* Handle the fact that while struct ifreq has the same *layout* on
3147 * 32/64 for everything but ifreq::ifru_ifmap and ifreq::ifru_data,
3148 * which are handled elsewhere, it still has different *size* due to
3149 * ifreq::ifru_ifmap (which is 16 bytes on 32 bit, 24 bytes on 64-bit,
3150 * resulting in struct ifreq being 32 and 40 bytes respectively).
3151 * As a result, if the struct happens to be at the end of a page and
3152 * the next page isn't readable/writable, we get a fault. To prevent
3153 * that, copy back and forth to the full size.
3156 uifr
= compat_alloc_user_space(sizeof(*uifr
));
3157 if (copy_in_user(uifr
, uifr32
, sizeof(*uifr32
)))
3160 err
= sock_do_ioctl(net
, sock
, cmd
, (unsigned long)uifr
);
3171 case SIOCGIFBRDADDR
:
3172 case SIOCGIFDSTADDR
:
3173 case SIOCGIFNETMASK
:
3179 if (copy_in_user(uifr32
, uifr
, sizeof(*uifr32
)))
3187 static int compat_sioc_ifmap(struct net
*net
, unsigned int cmd
,
3188 struct compat_ifreq __user
*uifr32
)
3191 struct compat_ifmap __user
*uifmap32
;
3194 uifmap32
= &uifr32
->ifr_ifru
.ifru_map
;
3195 err
= copy_from_user(&ifr
, uifr32
, sizeof(ifr
.ifr_name
));
3196 err
|= get_user(ifr
.ifr_map
.mem_start
, &uifmap32
->mem_start
);
3197 err
|= get_user(ifr
.ifr_map
.mem_end
, &uifmap32
->mem_end
);
3198 err
|= get_user(ifr
.ifr_map
.base_addr
, &uifmap32
->base_addr
);
3199 err
|= get_user(ifr
.ifr_map
.irq
, &uifmap32
->irq
);
3200 err
|= get_user(ifr
.ifr_map
.dma
, &uifmap32
->dma
);
3201 err
|= get_user(ifr
.ifr_map
.port
, &uifmap32
->port
);
3205 err
= dev_ioctl(net
, cmd
, &ifr
, NULL
);
3207 if (cmd
== SIOCGIFMAP
&& !err
) {
3208 err
= copy_to_user(uifr32
, &ifr
, sizeof(ifr
.ifr_name
));
3209 err
|= put_user(ifr
.ifr_map
.mem_start
, &uifmap32
->mem_start
);
3210 err
|= put_user(ifr
.ifr_map
.mem_end
, &uifmap32
->mem_end
);
3211 err
|= put_user(ifr
.ifr_map
.base_addr
, &uifmap32
->base_addr
);
3212 err
|= put_user(ifr
.ifr_map
.irq
, &uifmap32
->irq
);
3213 err
|= put_user(ifr
.ifr_map
.dma
, &uifmap32
->dma
);
3214 err
|= put_user(ifr
.ifr_map
.port
, &uifmap32
->port
);
3223 struct sockaddr rt_dst
; /* target address */
3224 struct sockaddr rt_gateway
; /* gateway addr (RTF_GATEWAY) */
3225 struct sockaddr rt_genmask
; /* target network mask (IP) */
3226 unsigned short rt_flags
;
3229 unsigned char rt_tos
;
3230 unsigned char rt_class
;
3232 short rt_metric
; /* +1 for binary compatibility! */
3233 /* char * */ u32 rt_dev
; /* forcing the device at add */
3234 u32 rt_mtu
; /* per route MTU/Window */
3235 u32 rt_window
; /* Window clamping */
3236 unsigned short rt_irtt
; /* Initial RTT */
3239 struct in6_rtmsg32
{
3240 struct in6_addr rtmsg_dst
;
3241 struct in6_addr rtmsg_src
;
3242 struct in6_addr rtmsg_gateway
;
3252 static int routing_ioctl(struct net
*net
, struct socket
*sock
,
3253 unsigned int cmd
, void __user
*argp
)
3257 struct in6_rtmsg r6
;
3261 mm_segment_t old_fs
= get_fs();
3263 if (sock
&& sock
->sk
&& sock
->sk
->sk_family
== AF_INET6
) { /* ipv6 */
3264 struct in6_rtmsg32 __user
*ur6
= argp
;
3265 ret
= copy_from_user(&r6
.rtmsg_dst
, &(ur6
->rtmsg_dst
),
3266 3 * sizeof(struct in6_addr
));
3267 ret
|= get_user(r6
.rtmsg_type
, &(ur6
->rtmsg_type
));
3268 ret
|= get_user(r6
.rtmsg_dst_len
, &(ur6
->rtmsg_dst_len
));
3269 ret
|= get_user(r6
.rtmsg_src_len
, &(ur6
->rtmsg_src_len
));
3270 ret
|= get_user(r6
.rtmsg_metric
, &(ur6
->rtmsg_metric
));
3271 ret
|= get_user(r6
.rtmsg_info
, &(ur6
->rtmsg_info
));
3272 ret
|= get_user(r6
.rtmsg_flags
, &(ur6
->rtmsg_flags
));
3273 ret
|= get_user(r6
.rtmsg_ifindex
, &(ur6
->rtmsg_ifindex
));
3277 struct rtentry32 __user
*ur4
= argp
;
3278 ret
= copy_from_user(&r4
.rt_dst
, &(ur4
->rt_dst
),
3279 3 * sizeof(struct sockaddr
));
3280 ret
|= get_user(r4
.rt_flags
, &(ur4
->rt_flags
));
3281 ret
|= get_user(r4
.rt_metric
, &(ur4
->rt_metric
));
3282 ret
|= get_user(r4
.rt_mtu
, &(ur4
->rt_mtu
));
3283 ret
|= get_user(r4
.rt_window
, &(ur4
->rt_window
));
3284 ret
|= get_user(r4
.rt_irtt
, &(ur4
->rt_irtt
));
3285 ret
|= get_user(rtdev
, &(ur4
->rt_dev
));
3287 ret
|= copy_from_user(devname
, compat_ptr(rtdev
), 15);
3288 r4
.rt_dev
= (char __user __force
*)devname
;
3302 ret
= sock_do_ioctl(net
, sock
, cmd
, (unsigned long) r
);
3309 /* Since old style bridge ioctl's endup using SIOCDEVPRIVATE
3310 * for some operations; this forces use of the newer bridge-utils that
3311 * use compatible ioctls
3313 static int old_bridge_ioctl(compat_ulong_t __user
*argp
)
3317 if (get_user(tmp
, argp
))
3319 if (tmp
== BRCTL_GET_VERSION
)
3320 return BRCTL_VERSION
+ 1;
3324 static int compat_sock_ioctl_trans(struct file
*file
, struct socket
*sock
,
3325 unsigned int cmd
, unsigned long arg
)
3327 void __user
*argp
= compat_ptr(arg
);
3328 struct sock
*sk
= sock
->sk
;
3329 struct net
*net
= sock_net(sk
);
3331 if (cmd
>= SIOCDEVPRIVATE
&& cmd
<= (SIOCDEVPRIVATE
+ 15))
3332 return compat_ifr_data_ioctl(net
, cmd
, argp
);
3337 return old_bridge_ioctl(argp
);
3339 return compat_dev_ifconf(net
, argp
);
3341 return ethtool_ioctl(net
, argp
);
3343 return compat_siocwandev(net
, argp
);
3346 return compat_sioc_ifmap(net
, cmd
, argp
);
3349 return routing_ioctl(net
, sock
, cmd
, argp
);
3351 return do_siocgstamp(net
, sock
, cmd
, argp
);
3353 return do_siocgstampns(net
, sock
, cmd
, argp
);
3354 case SIOCBONDSLAVEINFOQUERY
:
3355 case SIOCBONDINFOQUERY
:
3358 return compat_ifr_data_ioctl(net
, cmd
, argp
);
3371 return sock_ioctl(file
, cmd
, arg
);
3388 case SIOCSIFHWBROADCAST
:
3390 case SIOCGIFBRDADDR
:
3391 case SIOCSIFBRDADDR
:
3392 case SIOCGIFDSTADDR
:
3393 case SIOCSIFDSTADDR
:
3394 case SIOCGIFNETMASK
:
3395 case SIOCSIFNETMASK
:
3407 case SIOCBONDENSLAVE
:
3408 case SIOCBONDRELEASE
:
3409 case SIOCBONDSETHWADDR
:
3410 case SIOCBONDCHANGEACTIVE
:
3411 return compat_ifreq_ioctl(net
, sock
, cmd
, argp
);
3417 return sock_do_ioctl(net
, sock
, cmd
, arg
);
3420 return -ENOIOCTLCMD
;
3423 static long compat_sock_ioctl(struct file
*file
, unsigned int cmd
,
3426 struct socket
*sock
= file
->private_data
;
3427 int ret
= -ENOIOCTLCMD
;
3434 if (sock
->ops
->compat_ioctl
)
3435 ret
= sock
->ops
->compat_ioctl(sock
, cmd
, arg
);
3437 if (ret
== -ENOIOCTLCMD
&&
3438 (cmd
>= SIOCIWFIRST
&& cmd
<= SIOCIWLAST
))
3439 ret
= compat_wext_handle_ioctl(net
, cmd
, arg
);
3441 if (ret
== -ENOIOCTLCMD
)
3442 ret
= compat_sock_ioctl_trans(file
, sock
, cmd
, arg
);
3449 * kernel_bind - bind an address to a socket (kernel space)
3452 * @addrlen: length of address
3454 * Returns 0 or an error.
3457 int kernel_bind(struct socket
*sock
, struct sockaddr
*addr
, int addrlen
)
3459 return sock
->ops
->bind(sock
, addr
, addrlen
);
3461 EXPORT_SYMBOL(kernel_bind
);
3464 * kernel_listen - move socket to listening state (kernel space)
3466 * @backlog: pending connections queue size
3468 * Returns 0 or an error.
3471 int kernel_listen(struct socket
*sock
, int backlog
)
3473 return sock
->ops
->listen(sock
, backlog
);
3475 EXPORT_SYMBOL(kernel_listen
);
3478 * kernel_accept - accept a connection (kernel space)
3479 * @sock: listening socket
3480 * @newsock: new connected socket
3483 * @flags must be SOCK_CLOEXEC, SOCK_NONBLOCK or 0.
3484 * If it fails, @newsock is guaranteed to be %NULL.
3485 * Returns 0 or an error.
3488 int kernel_accept(struct socket
*sock
, struct socket
**newsock
, int flags
)
3490 struct sock
*sk
= sock
->sk
;
3493 err
= sock_create_lite(sk
->sk_family
, sk
->sk_type
, sk
->sk_protocol
,
3498 err
= sock
->ops
->accept(sock
, *newsock
, flags
, true);
3500 sock_release(*newsock
);
3505 (*newsock
)->ops
= sock
->ops
;
3506 __module_get((*newsock
)->ops
->owner
);
3511 EXPORT_SYMBOL(kernel_accept
);
3514 * kernel_connect - connect a socket (kernel space)
3517 * @addrlen: address length
3518 * @flags: flags (O_NONBLOCK, ...)
3520 * For datagram sockets, @addr is the addres to which datagrams are sent
3521 * by default, and the only address from which datagrams are received.
3522 * For stream sockets, attempts to connect to @addr.
3523 * Returns 0 or an error code.
3526 int kernel_connect(struct socket
*sock
, struct sockaddr
*addr
, int addrlen
,
3529 return sock
->ops
->connect(sock
, addr
, addrlen
, flags
);
3531 EXPORT_SYMBOL(kernel_connect
);
3534 * kernel_getsockname - get the address which the socket is bound (kernel space)
3536 * @addr: address holder
3538 * Fills the @addr pointer with the address which the socket is bound.
3539 * Returns 0 or an error code.
3542 int kernel_getsockname(struct socket
*sock
, struct sockaddr
*addr
)
3544 return sock
->ops
->getname(sock
, addr
, 0);
3546 EXPORT_SYMBOL(kernel_getsockname
);
3549 * kernel_peername - get the address which the socket is connected (kernel space)
3551 * @addr: address holder
3553 * Fills the @addr pointer with the address which the socket is connected.
3554 * Returns 0 or an error code.
3557 int kernel_getpeername(struct socket
*sock
, struct sockaddr
*addr
)
3559 return sock
->ops
->getname(sock
, addr
, 1);
3561 EXPORT_SYMBOL(kernel_getpeername
);
3564 * kernel_getsockopt - get a socket option (kernel space)
3566 * @level: API level (SOL_SOCKET, ...)
3567 * @optname: option tag
3568 * @optval: option value
3569 * @optlen: option length
3571 * Assigns the option length to @optlen.
3572 * Returns 0 or an error.
3575 int kernel_getsockopt(struct socket
*sock
, int level
, int optname
,
3576 char *optval
, int *optlen
)
3578 mm_segment_t oldfs
= get_fs();
3579 char __user
*uoptval
;
3580 int __user
*uoptlen
;
3583 uoptval
= (char __user __force
*) optval
;
3584 uoptlen
= (int __user __force
*) optlen
;
3587 if (level
== SOL_SOCKET
)
3588 err
= sock_getsockopt(sock
, level
, optname
, uoptval
, uoptlen
);
3590 err
= sock
->ops
->getsockopt(sock
, level
, optname
, uoptval
,
3595 EXPORT_SYMBOL(kernel_getsockopt
);
3598 * kernel_setsockopt - set a socket option (kernel space)
3600 * @level: API level (SOL_SOCKET, ...)
3601 * @optname: option tag
3602 * @optval: option value
3603 * @optlen: option length
3605 * Returns 0 or an error.
3608 int kernel_setsockopt(struct socket
*sock
, int level
, int optname
,
3609 char *optval
, unsigned int optlen
)
3611 mm_segment_t oldfs
= get_fs();
3612 char __user
*uoptval
;
3615 uoptval
= (char __user __force
*) optval
;
3618 if (level
== SOL_SOCKET
)
3619 err
= sock_setsockopt(sock
, level
, optname
, uoptval
, optlen
);
3621 err
= sock
->ops
->setsockopt(sock
, level
, optname
, uoptval
,
3626 EXPORT_SYMBOL(kernel_setsockopt
);
3629 * kernel_sendpage - send a &page through a socket (kernel space)
3632 * @offset: page offset
3633 * @size: total size in bytes
3634 * @flags: flags (MSG_DONTWAIT, ...)
3636 * Returns the total amount sent in bytes or an error.
3639 int kernel_sendpage(struct socket
*sock
, struct page
*page
, int offset
,
3640 size_t size
, int flags
)
3642 if (sock
->ops
->sendpage
)
3643 return sock
->ops
->sendpage(sock
, page
, offset
, size
, flags
);
3645 return sock_no_sendpage(sock
, page
, offset
, size
, flags
);
3647 EXPORT_SYMBOL(kernel_sendpage
);
3650 * kernel_sendpage_locked - send a &page through the locked sock (kernel space)
3653 * @offset: page offset
3654 * @size: total size in bytes
3655 * @flags: flags (MSG_DONTWAIT, ...)
3657 * Returns the total amount sent in bytes or an error.
3658 * Caller must hold @sk.
3661 int kernel_sendpage_locked(struct sock
*sk
, struct page
*page
, int offset
,
3662 size_t size
, int flags
)
3664 struct socket
*sock
= sk
->sk_socket
;
3666 if (sock
->ops
->sendpage_locked
)
3667 return sock
->ops
->sendpage_locked(sk
, page
, offset
, size
,
3670 return sock_no_sendpage_locked(sk
, page
, offset
, size
, flags
);
3672 EXPORT_SYMBOL(kernel_sendpage_locked
);
3675 * kernel_shutdown - shut down part of a full-duplex connection (kernel space)
3677 * @how: connection part
3679 * Returns 0 or an error.
3682 int kernel_sock_shutdown(struct socket
*sock
, enum sock_shutdown_cmd how
)
3684 return sock
->ops
->shutdown(sock
, how
);
3686 EXPORT_SYMBOL(kernel_sock_shutdown
);
3689 * kernel_sock_ip_overhead - returns the IP overhead imposed by a socket
3692 * This routine returns the IP overhead imposed by a socket i.e.
3693 * the length of the underlying IP header, depending on whether
3694 * this is an IPv4 or IPv6 socket and the length from IP options turned
3695 * on at the socket. Assumes that the caller has a lock on the socket.
3698 u32
kernel_sock_ip_overhead(struct sock
*sk
)
3700 struct inet_sock
*inet
;
3701 struct ip_options_rcu
*opt
;
3703 #if IS_ENABLED(CONFIG_IPV6)
3704 struct ipv6_pinfo
*np
;
3705 struct ipv6_txoptions
*optv6
= NULL
;
3706 #endif /* IS_ENABLED(CONFIG_IPV6) */
3711 switch (sk
->sk_family
) {
3714 overhead
+= sizeof(struct iphdr
);
3715 opt
= rcu_dereference_protected(inet
->inet_opt
,
3716 sock_owned_by_user(sk
));
3718 overhead
+= opt
->opt
.optlen
;
3720 #if IS_ENABLED(CONFIG_IPV6)
3723 overhead
+= sizeof(struct ipv6hdr
);
3725 optv6
= rcu_dereference_protected(np
->opt
,
3726 sock_owned_by_user(sk
));
3728 overhead
+= (optv6
->opt_flen
+ optv6
->opt_nflen
);
3730 #endif /* IS_ENABLED(CONFIG_IPV6) */
3731 default: /* Returns 0 overhead if the socket is not ipv4 or ipv6 */
3735 EXPORT_SYMBOL(kernel_sock_ip_overhead
);