Linux 4.14.168
[linux/fpc-iii.git] / net / socket.c
blob6a5ec658fcd8350b02f5e70992c1badf2c05f40c
1 /*
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>
7 * Ross Biro
8 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
10 * Fixes:
11 * Anonymous : NOTSOCK/BADF cleanup. Error fix in
12 * shutdown()
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
17 * top level.
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
22 * tty drivers).
23 * Niibe Yutaka : Asynchronous I/O for writes (4.4BSD style)
24 * Jeff Uphoff : Made max number of sockets command-line
25 * configurable.
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
34 * stuff.
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
40 * moment.
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
56 * paradigm.
58 * Based upon Swansea University Computer Society NET3.039
61 #include <linux/mm.h>
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>
98 #include <net/wext.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 <linux/atalk.h>
109 #include <net/busy_poll.h>
110 #include <linux/errqueue.h>
112 #ifdef CONFIG_NET_RX_BUSY_POLL
113 unsigned int sysctl_net_busy_read __read_mostly;
114 unsigned int sysctl_net_busy_poll __read_mostly;
115 #endif
117 static ssize_t sock_read_iter(struct kiocb *iocb, struct iov_iter *to);
118 static ssize_t sock_write_iter(struct kiocb *iocb, struct iov_iter *from);
119 static int sock_mmap(struct file *file, struct vm_area_struct *vma);
121 static int sock_close(struct inode *inode, struct file *file);
122 static unsigned int sock_poll(struct file *file,
123 struct poll_table_struct *wait);
124 static long sock_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
125 #ifdef CONFIG_COMPAT
126 static long compat_sock_ioctl(struct file *file,
127 unsigned int cmd, unsigned long arg);
128 #endif
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,
134 unsigned int flags);
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,
143 .llseek = no_llseek,
144 .read_iter = sock_read_iter,
145 .write_iter = sock_write_iter,
146 .poll = sock_poll,
147 .unlocked_ioctl = sock_ioctl,
148 #ifdef CONFIG_COMPAT
149 .compat_ioctl = compat_sock_ioctl,
150 #endif
151 .mmap = sock_mmap,
152 .release = sock_close,
153 .fasync = sock_fasync,
154 .sendpage = sock_sendpage,
155 .splice_write = generic_splice_sendpage,
156 .splice_read = sock_splice_read,
160 * The protocol list. Each protocol is registered in here.
163 static DEFINE_SPINLOCK(net_family_lock);
164 static const struct net_proto_family __rcu *net_families[NPROTO] __read_mostly;
167 * Statistics counters of the socket lists
170 static DEFINE_PER_CPU(int, sockets_in_use);
173 * Support routines.
174 * Move socket addresses back and forth across the kernel/user
175 * divide and look after the messy bits.
179 * move_addr_to_kernel - copy a socket address into kernel space
180 * @uaddr: Address in user space
181 * @kaddr: Address in kernel space
182 * @ulen: Length in user space
184 * The address is copied into kernel space. If the provided address is
185 * too long an error code of -EINVAL is returned. If the copy gives
186 * invalid addresses -EFAULT is returned. On a success 0 is returned.
189 int move_addr_to_kernel(void __user *uaddr, int ulen, struct sockaddr_storage *kaddr)
191 if (ulen < 0 || ulen > sizeof(struct sockaddr_storage))
192 return -EINVAL;
193 if (ulen == 0)
194 return 0;
195 if (copy_from_user(kaddr, uaddr, ulen))
196 return -EFAULT;
197 return audit_sockaddr(ulen, kaddr);
201 * move_addr_to_user - copy an address to user space
202 * @kaddr: kernel space address
203 * @klen: length of address in kernel
204 * @uaddr: user space address
205 * @ulen: pointer to user length field
207 * The value pointed to by ulen on entry is the buffer length available.
208 * This is overwritten with the buffer space used. -EINVAL is returned
209 * if an overlong buffer is specified or a negative buffer size. -EFAULT
210 * is returned if either the buffer or the length field are not
211 * accessible.
212 * After copying the data up to the limit the user specifies, the true
213 * length of the data is written over the length limit the user
214 * specified. Zero is returned for a success.
217 static int move_addr_to_user(struct sockaddr_storage *kaddr, int klen,
218 void __user *uaddr, int __user *ulen)
220 int err;
221 int len;
223 BUG_ON(klen > sizeof(struct sockaddr_storage));
224 err = get_user(len, ulen);
225 if (err)
226 return err;
227 if (len > klen)
228 len = klen;
229 if (len < 0)
230 return -EINVAL;
231 if (len) {
232 if (audit_sockaddr(klen, kaddr))
233 return -ENOMEM;
234 if (copy_to_user(uaddr, kaddr, len))
235 return -EFAULT;
238 * "fromlen shall refer to the value before truncation.."
239 * 1003.1g
241 return __put_user(klen, ulen);
244 static struct kmem_cache *sock_inode_cachep __read_mostly;
246 static struct inode *sock_alloc_inode(struct super_block *sb)
248 struct socket_alloc *ei;
249 struct socket_wq *wq;
251 ei = kmem_cache_alloc(sock_inode_cachep, GFP_KERNEL);
252 if (!ei)
253 return NULL;
254 wq = kmalloc(sizeof(*wq), GFP_KERNEL);
255 if (!wq) {
256 kmem_cache_free(sock_inode_cachep, ei);
257 return NULL;
259 init_waitqueue_head(&wq->wait);
260 wq->fasync_list = NULL;
261 wq->flags = 0;
262 RCU_INIT_POINTER(ei->socket.wq, wq);
264 ei->socket.state = SS_UNCONNECTED;
265 ei->socket.flags = 0;
266 ei->socket.ops = NULL;
267 ei->socket.sk = NULL;
268 ei->socket.file = NULL;
270 return &ei->vfs_inode;
273 static void sock_destroy_inode(struct inode *inode)
275 struct socket_alloc *ei;
276 struct socket_wq *wq;
278 ei = container_of(inode, struct socket_alloc, vfs_inode);
279 wq = rcu_dereference_protected(ei->socket.wq, 1);
280 kfree_rcu(wq, rcu);
281 kmem_cache_free(sock_inode_cachep, ei);
284 static void init_once(void *foo)
286 struct socket_alloc *ei = (struct socket_alloc *)foo;
288 inode_init_once(&ei->vfs_inode);
291 static void init_inodecache(void)
293 sock_inode_cachep = kmem_cache_create("sock_inode_cache",
294 sizeof(struct socket_alloc),
296 (SLAB_HWCACHE_ALIGN |
297 SLAB_RECLAIM_ACCOUNT |
298 SLAB_MEM_SPREAD | SLAB_ACCOUNT),
299 init_once);
300 BUG_ON(sock_inode_cachep == NULL);
303 static const struct super_operations sockfs_ops = {
304 .alloc_inode = sock_alloc_inode,
305 .destroy_inode = sock_destroy_inode,
306 .statfs = simple_statfs,
310 * sockfs_dname() is called from d_path().
312 static char *sockfs_dname(struct dentry *dentry, char *buffer, int buflen)
314 return dynamic_dname(dentry, buffer, buflen, "socket:[%lu]",
315 d_inode(dentry)->i_ino);
318 static const struct dentry_operations sockfs_dentry_operations = {
319 .d_dname = sockfs_dname,
322 static int sockfs_xattr_get(const struct xattr_handler *handler,
323 struct dentry *dentry, struct inode *inode,
324 const char *suffix, void *value, size_t size)
326 if (value) {
327 if (dentry->d_name.len + 1 > size)
328 return -ERANGE;
329 memcpy(value, dentry->d_name.name, dentry->d_name.len + 1);
331 return dentry->d_name.len + 1;
334 #define XATTR_SOCKPROTONAME_SUFFIX "sockprotoname"
335 #define XATTR_NAME_SOCKPROTONAME (XATTR_SYSTEM_PREFIX XATTR_SOCKPROTONAME_SUFFIX)
336 #define XATTR_NAME_SOCKPROTONAME_LEN (sizeof(XATTR_NAME_SOCKPROTONAME)-1)
338 static const struct xattr_handler sockfs_xattr_handler = {
339 .name = XATTR_NAME_SOCKPROTONAME,
340 .get = sockfs_xattr_get,
343 static int sockfs_security_xattr_set(const struct xattr_handler *handler,
344 struct dentry *dentry, struct inode *inode,
345 const char *suffix, const void *value,
346 size_t size, int flags)
348 /* Handled by LSM. */
349 return -EAGAIN;
352 static const struct xattr_handler sockfs_security_xattr_handler = {
353 .prefix = XATTR_SECURITY_PREFIX,
354 .set = sockfs_security_xattr_set,
357 static const struct xattr_handler *sockfs_xattr_handlers[] = {
358 &sockfs_xattr_handler,
359 &sockfs_security_xattr_handler,
360 NULL
363 static struct dentry *sockfs_mount(struct file_system_type *fs_type,
364 int flags, const char *dev_name, void *data)
366 return mount_pseudo_xattr(fs_type, "socket:", &sockfs_ops,
367 sockfs_xattr_handlers,
368 &sockfs_dentry_operations, SOCKFS_MAGIC);
371 static struct vfsmount *sock_mnt __read_mostly;
373 static struct file_system_type sock_fs_type = {
374 .name = "sockfs",
375 .mount = sockfs_mount,
376 .kill_sb = kill_anon_super,
380 * Obtains the first available file descriptor and sets it up for use.
382 * These functions create file structures and maps them to fd space
383 * of the current process. On success it returns file descriptor
384 * and file struct implicitly stored in sock->file.
385 * Note that another thread may close file descriptor before we return
386 * from this function. We use the fact that now we do not refer
387 * to socket after mapping. If one day we will need it, this
388 * function will increment ref. count on file by 1.
390 * In any case returned fd MAY BE not valid!
391 * This race condition is unavoidable
392 * with shared fd spaces, we cannot solve it inside kernel,
393 * but we take care of internal coherence yet.
396 struct file *sock_alloc_file(struct socket *sock, int flags, const char *dname)
398 struct qstr name = { .name = "" };
399 struct path path;
400 struct file *file;
402 if (dname) {
403 name.name = dname;
404 name.len = strlen(name.name);
405 } else if (sock->sk) {
406 name.name = sock->sk->sk_prot_creator->name;
407 name.len = strlen(name.name);
409 path.dentry = d_alloc_pseudo(sock_mnt->mnt_sb, &name);
410 if (unlikely(!path.dentry))
411 return ERR_PTR(-ENOMEM);
412 path.mnt = mntget(sock_mnt);
414 d_instantiate(path.dentry, SOCK_INODE(sock));
416 file = alloc_file(&path, FMODE_READ | FMODE_WRITE,
417 &socket_file_ops);
418 if (IS_ERR(file)) {
419 /* drop dentry, keep inode */
420 ihold(d_inode(path.dentry));
421 path_put(&path);
422 return file;
425 sock->file = file;
426 file->f_flags = O_RDWR | (flags & O_NONBLOCK);
427 file->private_data = sock;
428 return file;
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))
437 return fd;
439 newfile = sock_alloc_file(sock, flags, NULL);
440 if (likely(!IS_ERR(newfile))) {
441 fd_install(fd, newfile);
442 return fd;
445 put_unused_fd(fd);
446 return PTR_ERR(newfile);
449 struct socket *sock_from_file(struct file *file, int *err)
451 if (file->f_op == &socket_file_ops)
452 return file->private_data; /* set in sock_map_fd */
454 *err = -ENOTSOCK;
455 return NULL;
457 EXPORT_SYMBOL(sock_from_file);
460 * sockfd_lookup - Go from a file number to its socket slot
461 * @fd: file handle
462 * @err: pointer to an error code return
464 * The file handle passed in is locked and the socket it is bound
465 * to is returned. If an error occurs the err pointer is overwritten
466 * with a negative errno code and NULL is returned. The function checks
467 * for both invalid handles and passing a handle which is not a socket.
469 * On a success the socket object pointer is returned.
472 struct socket *sockfd_lookup(int fd, int *err)
474 struct file *file;
475 struct socket *sock;
477 file = fget(fd);
478 if (!file) {
479 *err = -EBADF;
480 return NULL;
483 sock = sock_from_file(file, err);
484 if (!sock)
485 fput(file);
486 return sock;
488 EXPORT_SYMBOL(sockfd_lookup);
490 static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)
492 struct fd f = fdget(fd);
493 struct socket *sock;
495 *err = -EBADF;
496 if (f.file) {
497 sock = sock_from_file(f.file, err);
498 if (likely(sock)) {
499 *fput_needed = f.flags;
500 return sock;
502 fdput(f);
504 return NULL;
507 static ssize_t sockfs_listxattr(struct dentry *dentry, char *buffer,
508 size_t size)
510 ssize_t len;
511 ssize_t used = 0;
513 len = security_inode_listsecurity(d_inode(dentry), buffer, size);
514 if (len < 0)
515 return len;
516 used += len;
517 if (buffer) {
518 if (size < used)
519 return -ERANGE;
520 buffer += len;
523 len = (XATTR_NAME_SOCKPROTONAME_LEN + 1);
524 used += len;
525 if (buffer) {
526 if (size < used)
527 return -ERANGE;
528 memcpy(buffer, XATTR_NAME_SOCKPROTONAME, len);
529 buffer += len;
532 return used;
535 static int sockfs_setattr(struct dentry *dentry, struct iattr *iattr)
537 int err = simple_setattr(dentry, iattr);
539 if (!err && (iattr->ia_valid & ATTR_UID)) {
540 struct socket *sock = SOCKET_I(d_inode(dentry));
542 if (sock->sk)
543 sock->sk->sk_uid = iattr->ia_uid;
544 else
545 err = -ENOENT;
548 return err;
551 static const struct inode_operations sockfs_inode_ops = {
552 .listxattr = sockfs_listxattr,
553 .setattr = sockfs_setattr,
557 * sock_alloc - allocate a socket
559 * Allocate a new inode and socket object. The two are bound together
560 * and initialised. The socket is then returned. If we are out of inodes
561 * NULL is returned.
564 struct socket *sock_alloc(void)
566 struct inode *inode;
567 struct socket *sock;
569 inode = new_inode_pseudo(sock_mnt->mnt_sb);
570 if (!inode)
571 return NULL;
573 sock = SOCKET_I(inode);
575 inode->i_ino = get_next_ino();
576 inode->i_mode = S_IFSOCK | S_IRWXUGO;
577 inode->i_uid = current_fsuid();
578 inode->i_gid = current_fsgid();
579 inode->i_op = &sockfs_inode_ops;
581 this_cpu_add(sockets_in_use, 1);
582 return sock;
584 EXPORT_SYMBOL(sock_alloc);
587 * sock_release - close a socket
588 * @sock: socket to close
590 * The socket is released from the protocol stack if it has a release
591 * callback, and the inode is then released if the socket is bound to
592 * an inode not a file.
595 static void __sock_release(struct socket *sock, struct inode *inode)
597 if (sock->ops) {
598 struct module *owner = sock->ops->owner;
600 if (inode)
601 inode_lock(inode);
602 sock->ops->release(sock);
603 sock->sk = NULL;
604 if (inode)
605 inode_unlock(inode);
606 sock->ops = NULL;
607 module_put(owner);
610 if (rcu_dereference_protected(sock->wq, 1)->fasync_list)
611 pr_err("%s: fasync list not empty!\n", __func__);
613 this_cpu_sub(sockets_in_use, 1);
614 if (!sock->file) {
615 iput(SOCK_INODE(sock));
616 return;
618 sock->file = NULL;
621 void sock_release(struct socket *sock)
623 __sock_release(sock, NULL);
625 EXPORT_SYMBOL(sock_release);
627 void __sock_tx_timestamp(__u16 tsflags, __u8 *tx_flags)
629 u8 flags = *tx_flags;
631 if (tsflags & SOF_TIMESTAMPING_TX_HARDWARE)
632 flags |= SKBTX_HW_TSTAMP;
634 if (tsflags & SOF_TIMESTAMPING_TX_SOFTWARE)
635 flags |= SKBTX_SW_TSTAMP;
637 if (tsflags & SOF_TIMESTAMPING_TX_SCHED)
638 flags |= SKBTX_SCHED_TSTAMP;
640 *tx_flags = flags;
642 EXPORT_SYMBOL(__sock_tx_timestamp);
644 static inline int sock_sendmsg_nosec(struct socket *sock, struct msghdr *msg)
646 int ret = sock->ops->sendmsg(sock, msg, msg_data_left(msg));
647 BUG_ON(ret == -EIOCBQUEUED);
648 return ret;
651 int sock_sendmsg(struct socket *sock, struct msghdr *msg)
653 int err = security_socket_sendmsg(sock, msg,
654 msg_data_left(msg));
656 return err ?: sock_sendmsg_nosec(sock, msg);
658 EXPORT_SYMBOL(sock_sendmsg);
660 int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
661 struct kvec *vec, size_t num, size_t size)
663 iov_iter_kvec(&msg->msg_iter, WRITE | ITER_KVEC, vec, num, size);
664 return sock_sendmsg(sock, msg);
666 EXPORT_SYMBOL(kernel_sendmsg);
668 int kernel_sendmsg_locked(struct sock *sk, struct msghdr *msg,
669 struct kvec *vec, size_t num, size_t size)
671 struct socket *sock = sk->sk_socket;
673 if (!sock->ops->sendmsg_locked)
674 return sock_no_sendmsg_locked(sk, msg, size);
676 iov_iter_kvec(&msg->msg_iter, WRITE | ITER_KVEC, vec, num, size);
678 return sock->ops->sendmsg_locked(sk, msg, msg_data_left(msg));
680 EXPORT_SYMBOL(kernel_sendmsg_locked);
682 static bool skb_is_err_queue(const struct sk_buff *skb)
684 /* pkt_type of skbs enqueued on the error queue are set to
685 * PACKET_OUTGOING in skb_set_err_queue(). This is only safe to do
686 * in recvmsg, since skbs received on a local socket will never
687 * have a pkt_type of PACKET_OUTGOING.
689 return skb->pkt_type == PACKET_OUTGOING;
692 /* On transmit, software and hardware timestamps are returned independently.
693 * As the two skb clones share the hardware timestamp, which may be updated
694 * before the software timestamp is received, a hardware TX timestamp may be
695 * returned only if there is no software TX timestamp. Ignore false software
696 * timestamps, which may be made in the __sock_recv_timestamp() call when the
697 * option SO_TIMESTAMP(NS) is enabled on the socket, even when the skb has a
698 * hardware timestamp.
700 static bool skb_is_swtx_tstamp(const struct sk_buff *skb, int false_tstamp)
702 return skb->tstamp && !false_tstamp && skb_is_err_queue(skb);
705 static void put_ts_pktinfo(struct msghdr *msg, struct sk_buff *skb)
707 struct scm_ts_pktinfo ts_pktinfo;
708 struct net_device *orig_dev;
710 if (!skb_mac_header_was_set(skb))
711 return;
713 memset(&ts_pktinfo, 0, sizeof(ts_pktinfo));
715 rcu_read_lock();
716 orig_dev = dev_get_by_napi_id(skb_napi_id(skb));
717 if (orig_dev)
718 ts_pktinfo.if_index = orig_dev->ifindex;
719 rcu_read_unlock();
721 ts_pktinfo.pkt_length = skb->len - skb_mac_offset(skb);
722 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPING_PKTINFO,
723 sizeof(ts_pktinfo), &ts_pktinfo);
727 * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
729 void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
730 struct sk_buff *skb)
732 int need_software_tstamp = sock_flag(sk, SOCK_RCVTSTAMP);
733 struct scm_timestamping tss;
734 int empty = 1, false_tstamp = 0;
735 struct skb_shared_hwtstamps *shhwtstamps =
736 skb_hwtstamps(skb);
738 /* Race occurred between timestamp enabling and packet
739 receiving. Fill in the current time for now. */
740 if (need_software_tstamp && skb->tstamp == 0) {
741 __net_timestamp(skb);
742 false_tstamp = 1;
745 if (need_software_tstamp) {
746 if (!sock_flag(sk, SOCK_RCVTSTAMPNS)) {
747 struct timeval tv;
748 skb_get_timestamp(skb, &tv);
749 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMP,
750 sizeof(tv), &tv);
751 } else {
752 struct timespec ts;
753 skb_get_timestampns(skb, &ts);
754 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPNS,
755 sizeof(ts), &ts);
759 memset(&tss, 0, sizeof(tss));
760 if ((sk->sk_tsflags & SOF_TIMESTAMPING_SOFTWARE) &&
761 ktime_to_timespec_cond(skb->tstamp, tss.ts + 0))
762 empty = 0;
763 if (shhwtstamps &&
764 (sk->sk_tsflags & SOF_TIMESTAMPING_RAW_HARDWARE) &&
765 !skb_is_swtx_tstamp(skb, false_tstamp) &&
766 ktime_to_timespec_cond(shhwtstamps->hwtstamp, tss.ts + 2)) {
767 empty = 0;
768 if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_PKTINFO) &&
769 !skb_is_err_queue(skb))
770 put_ts_pktinfo(msg, skb);
772 if (!empty) {
773 put_cmsg(msg, SOL_SOCKET,
774 SCM_TIMESTAMPING, sizeof(tss), &tss);
776 if (skb_is_err_queue(skb) && skb->len &&
777 SKB_EXT_ERR(skb)->opt_stats)
778 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPING_OPT_STATS,
779 skb->len, skb->data);
782 EXPORT_SYMBOL_GPL(__sock_recv_timestamp);
784 void __sock_recv_wifi_status(struct msghdr *msg, struct sock *sk,
785 struct sk_buff *skb)
787 int ack;
789 if (!sock_flag(sk, SOCK_WIFI_STATUS))
790 return;
791 if (!skb->wifi_acked_valid)
792 return;
794 ack = skb->wifi_acked;
796 put_cmsg(msg, SOL_SOCKET, SCM_WIFI_STATUS, sizeof(ack), &ack);
798 EXPORT_SYMBOL_GPL(__sock_recv_wifi_status);
800 static inline void sock_recv_drops(struct msghdr *msg, struct sock *sk,
801 struct sk_buff *skb)
803 if (sock_flag(sk, SOCK_RXQ_OVFL) && skb && SOCK_SKB_CB(skb)->dropcount)
804 put_cmsg(msg, SOL_SOCKET, SO_RXQ_OVFL,
805 sizeof(__u32), &SOCK_SKB_CB(skb)->dropcount);
808 void __sock_recv_ts_and_drops(struct msghdr *msg, struct sock *sk,
809 struct sk_buff *skb)
811 sock_recv_timestamp(msg, sk, skb);
812 sock_recv_drops(msg, sk, skb);
814 EXPORT_SYMBOL_GPL(__sock_recv_ts_and_drops);
816 static inline int sock_recvmsg_nosec(struct socket *sock, struct msghdr *msg,
817 int flags)
819 return sock->ops->recvmsg(sock, msg, msg_data_left(msg), flags);
822 int sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags)
824 int err = security_socket_recvmsg(sock, msg, msg_data_left(msg), flags);
826 return err ?: sock_recvmsg_nosec(sock, msg, flags);
828 EXPORT_SYMBOL(sock_recvmsg);
831 * kernel_recvmsg - Receive a message from a socket (kernel space)
832 * @sock: The socket to receive the message from
833 * @msg: Received message
834 * @vec: Input s/g array for message data
835 * @num: Size of input s/g array
836 * @size: Number of bytes to read
837 * @flags: Message flags (MSG_DONTWAIT, etc...)
839 * On return the msg structure contains the scatter/gather array passed in the
840 * vec argument. The array is modified so that it consists of the unfilled
841 * portion of the original array.
843 * The returned value is the total number of bytes received, or an error.
845 int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
846 struct kvec *vec, size_t num, size_t size, int flags)
848 mm_segment_t oldfs = get_fs();
849 int result;
851 iov_iter_kvec(&msg->msg_iter, READ | ITER_KVEC, vec, num, size);
852 set_fs(KERNEL_DS);
853 result = sock_recvmsg(sock, msg, flags);
854 set_fs(oldfs);
855 return result;
857 EXPORT_SYMBOL(kernel_recvmsg);
859 static ssize_t sock_sendpage(struct file *file, struct page *page,
860 int offset, size_t size, loff_t *ppos, int more)
862 struct socket *sock;
863 int flags;
865 sock = file->private_data;
867 flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
868 /* more is a combination of MSG_MORE and MSG_SENDPAGE_NOTLAST */
869 flags |= more;
871 return kernel_sendpage(sock, page, offset, size, flags);
874 static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
875 struct pipe_inode_info *pipe, size_t len,
876 unsigned int flags)
878 struct socket *sock = file->private_data;
880 if (unlikely(!sock->ops->splice_read))
881 return -EINVAL;
883 return sock->ops->splice_read(sock, ppos, pipe, len, flags);
886 static ssize_t sock_read_iter(struct kiocb *iocb, struct iov_iter *to)
888 struct file *file = iocb->ki_filp;
889 struct socket *sock = file->private_data;
890 struct msghdr msg = {.msg_iter = *to,
891 .msg_iocb = iocb};
892 ssize_t res;
894 if (file->f_flags & O_NONBLOCK || (iocb->ki_flags & IOCB_NOWAIT))
895 msg.msg_flags = MSG_DONTWAIT;
897 if (iocb->ki_pos != 0)
898 return -ESPIPE;
900 if (!iov_iter_count(to)) /* Match SYS5 behaviour */
901 return 0;
903 res = sock_recvmsg(sock, &msg, msg.msg_flags);
904 *to = msg.msg_iter;
905 return res;
908 static ssize_t sock_write_iter(struct kiocb *iocb, struct iov_iter *from)
910 struct file *file = iocb->ki_filp;
911 struct socket *sock = file->private_data;
912 struct msghdr msg = {.msg_iter = *from,
913 .msg_iocb = iocb};
914 ssize_t res;
916 if (iocb->ki_pos != 0)
917 return -ESPIPE;
919 if (file->f_flags & O_NONBLOCK || (iocb->ki_flags & IOCB_NOWAIT))
920 msg.msg_flags = MSG_DONTWAIT;
922 if (sock->type == SOCK_SEQPACKET)
923 msg.msg_flags |= MSG_EOR;
925 res = sock_sendmsg(sock, &msg);
926 *from = msg.msg_iter;
927 return res;
931 * Atomic setting of ioctl hooks to avoid race
932 * with module unload.
935 static DEFINE_MUTEX(br_ioctl_mutex);
936 static int (*br_ioctl_hook) (struct net *, unsigned int cmd, void __user *arg);
938 void brioctl_set(int (*hook) (struct net *, unsigned int, void __user *))
940 mutex_lock(&br_ioctl_mutex);
941 br_ioctl_hook = hook;
942 mutex_unlock(&br_ioctl_mutex);
944 EXPORT_SYMBOL(brioctl_set);
946 static DEFINE_MUTEX(vlan_ioctl_mutex);
947 static int (*vlan_ioctl_hook) (struct net *, void __user *arg);
949 void vlan_ioctl_set(int (*hook) (struct net *, void __user *))
951 mutex_lock(&vlan_ioctl_mutex);
952 vlan_ioctl_hook = hook;
953 mutex_unlock(&vlan_ioctl_mutex);
955 EXPORT_SYMBOL(vlan_ioctl_set);
957 static DEFINE_MUTEX(dlci_ioctl_mutex);
958 static int (*dlci_ioctl_hook) (unsigned int, void __user *);
960 void dlci_ioctl_set(int (*hook) (unsigned int, void __user *))
962 mutex_lock(&dlci_ioctl_mutex);
963 dlci_ioctl_hook = hook;
964 mutex_unlock(&dlci_ioctl_mutex);
966 EXPORT_SYMBOL(dlci_ioctl_set);
968 static long sock_do_ioctl(struct net *net, struct socket *sock,
969 unsigned int cmd, unsigned long arg)
971 int err;
972 void __user *argp = (void __user *)arg;
974 err = sock->ops->ioctl(sock, cmd, arg);
977 * If this ioctl is unknown try to hand it down
978 * to the NIC driver.
980 if (err == -ENOIOCTLCMD)
981 err = dev_ioctl(net, cmd, argp);
983 return err;
987 * With an ioctl, arg may well be a user mode pointer, but we don't know
988 * what to do with it - that's up to the protocol still.
991 static struct ns_common *get_net_ns(struct ns_common *ns)
993 return &get_net(container_of(ns, struct net, ns))->ns;
996 static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
998 struct socket *sock;
999 struct sock *sk;
1000 void __user *argp = (void __user *)arg;
1001 int pid, err;
1002 struct net *net;
1004 sock = file->private_data;
1005 sk = sock->sk;
1006 net = sock_net(sk);
1007 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
1008 err = dev_ioctl(net, cmd, argp);
1009 } else
1010 #ifdef CONFIG_WEXT_CORE
1011 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
1012 err = dev_ioctl(net, cmd, argp);
1013 } else
1014 #endif
1015 switch (cmd) {
1016 case FIOSETOWN:
1017 case SIOCSPGRP:
1018 err = -EFAULT;
1019 if (get_user(pid, (int __user *)argp))
1020 break;
1021 err = f_setown(sock->file, pid, 1);
1022 break;
1023 case FIOGETOWN:
1024 case SIOCGPGRP:
1025 err = put_user(f_getown(sock->file),
1026 (int __user *)argp);
1027 break;
1028 case SIOCGIFBR:
1029 case SIOCSIFBR:
1030 case SIOCBRADDBR:
1031 case SIOCBRDELBR:
1032 err = -ENOPKG;
1033 if (!br_ioctl_hook)
1034 request_module("bridge");
1036 mutex_lock(&br_ioctl_mutex);
1037 if (br_ioctl_hook)
1038 err = br_ioctl_hook(net, cmd, argp);
1039 mutex_unlock(&br_ioctl_mutex);
1040 break;
1041 case SIOCGIFVLAN:
1042 case SIOCSIFVLAN:
1043 err = -ENOPKG;
1044 if (!vlan_ioctl_hook)
1045 request_module("8021q");
1047 mutex_lock(&vlan_ioctl_mutex);
1048 if (vlan_ioctl_hook)
1049 err = vlan_ioctl_hook(net, argp);
1050 mutex_unlock(&vlan_ioctl_mutex);
1051 break;
1052 case SIOCADDDLCI:
1053 case SIOCDELDLCI:
1054 err = -ENOPKG;
1055 if (!dlci_ioctl_hook)
1056 request_module("dlci");
1058 mutex_lock(&dlci_ioctl_mutex);
1059 if (dlci_ioctl_hook)
1060 err = dlci_ioctl_hook(cmd, argp);
1061 mutex_unlock(&dlci_ioctl_mutex);
1062 break;
1063 case SIOCGSKNS:
1064 err = -EPERM;
1065 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1066 break;
1068 err = open_related_ns(&net->ns, get_net_ns);
1069 break;
1070 default:
1071 err = sock_do_ioctl(net, sock, cmd, arg);
1072 break;
1074 return err;
1077 int sock_create_lite(int family, int type, int protocol, struct socket **res)
1079 int err;
1080 struct socket *sock = NULL;
1082 err = security_socket_create(family, type, protocol, 1);
1083 if (err)
1084 goto out;
1086 sock = sock_alloc();
1087 if (!sock) {
1088 err = -ENOMEM;
1089 goto out;
1092 sock->type = type;
1093 err = security_socket_post_create(sock, family, type, protocol, 1);
1094 if (err)
1095 goto out_release;
1097 out:
1098 *res = sock;
1099 return err;
1100 out_release:
1101 sock_release(sock);
1102 sock = NULL;
1103 goto out;
1105 EXPORT_SYMBOL(sock_create_lite);
1107 /* No kernel lock held - perfect */
1108 static unsigned int sock_poll(struct file *file, poll_table *wait)
1110 unsigned int busy_flag = 0;
1111 struct socket *sock;
1114 * We can't return errors to poll, so it's either yes or no.
1116 sock = file->private_data;
1118 if (sk_can_busy_loop(sock->sk)) {
1119 /* this socket can poll_ll so tell the system call */
1120 busy_flag = POLL_BUSY_LOOP;
1122 /* once, only if requested by syscall */
1123 if (wait && (wait->_key & POLL_BUSY_LOOP))
1124 sk_busy_loop(sock->sk, 1);
1127 return busy_flag | sock->ops->poll(file, sock, wait);
1130 static int sock_mmap(struct file *file, struct vm_area_struct *vma)
1132 struct socket *sock = file->private_data;
1134 return sock->ops->mmap(file, sock, vma);
1137 static int sock_close(struct inode *inode, struct file *filp)
1139 __sock_release(SOCKET_I(inode), inode);
1140 return 0;
1144 * Update the socket async list
1146 * Fasync_list locking strategy.
1148 * 1. fasync_list is modified only under process context socket lock
1149 * i.e. under semaphore.
1150 * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
1151 * or under socket lock
1154 static int sock_fasync(int fd, struct file *filp, int on)
1156 struct socket *sock = filp->private_data;
1157 struct sock *sk = sock->sk;
1158 struct socket_wq *wq;
1160 if (sk == NULL)
1161 return -EINVAL;
1163 lock_sock(sk);
1164 wq = rcu_dereference_protected(sock->wq, lockdep_sock_is_held(sk));
1165 fasync_helper(fd, filp, on, &wq->fasync_list);
1167 if (!wq->fasync_list)
1168 sock_reset_flag(sk, SOCK_FASYNC);
1169 else
1170 sock_set_flag(sk, SOCK_FASYNC);
1172 release_sock(sk);
1173 return 0;
1176 /* This function may be called only under rcu_lock */
1178 int sock_wake_async(struct socket_wq *wq, int how, int band)
1180 if (!wq || !wq->fasync_list)
1181 return -1;
1183 switch (how) {
1184 case SOCK_WAKE_WAITD:
1185 if (test_bit(SOCKWQ_ASYNC_WAITDATA, &wq->flags))
1186 break;
1187 goto call_kill;
1188 case SOCK_WAKE_SPACE:
1189 if (!test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags))
1190 break;
1191 /* fall through */
1192 case SOCK_WAKE_IO:
1193 call_kill:
1194 kill_fasync(&wq->fasync_list, SIGIO, band);
1195 break;
1196 case SOCK_WAKE_URG:
1197 kill_fasync(&wq->fasync_list, SIGURG, band);
1200 return 0;
1202 EXPORT_SYMBOL(sock_wake_async);
1204 int __sock_create(struct net *net, int family, int type, int protocol,
1205 struct socket **res, int kern)
1207 int err;
1208 struct socket *sock;
1209 const struct net_proto_family *pf;
1212 * Check protocol is in range
1214 if (family < 0 || family >= NPROTO)
1215 return -EAFNOSUPPORT;
1216 if (type < 0 || type >= SOCK_MAX)
1217 return -EINVAL;
1219 /* Compatibility.
1221 This uglymoron is moved from INET layer to here to avoid
1222 deadlock in module load.
1224 if (family == PF_INET && type == SOCK_PACKET) {
1225 pr_info_once("%s uses obsolete (PF_INET,SOCK_PACKET)\n",
1226 current->comm);
1227 family = PF_PACKET;
1230 err = security_socket_create(family, type, protocol, kern);
1231 if (err)
1232 return err;
1235 * Allocate the socket and allow the family to set things up. if
1236 * the protocol is 0, the family is instructed to select an appropriate
1237 * default.
1239 sock = sock_alloc();
1240 if (!sock) {
1241 net_warn_ratelimited("socket: no more sockets\n");
1242 return -ENFILE; /* Not exactly a match, but its the
1243 closest posix thing */
1246 sock->type = type;
1248 #ifdef CONFIG_MODULES
1249 /* Attempt to load a protocol module if the find failed.
1251 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
1252 * requested real, full-featured networking support upon configuration.
1253 * Otherwise module support will break!
1255 if (rcu_access_pointer(net_families[family]) == NULL)
1256 request_module("net-pf-%d", family);
1257 #endif
1259 rcu_read_lock();
1260 pf = rcu_dereference(net_families[family]);
1261 err = -EAFNOSUPPORT;
1262 if (!pf)
1263 goto out_release;
1266 * We will call the ->create function, that possibly is in a loadable
1267 * module, so we have to bump that loadable module refcnt first.
1269 if (!try_module_get(pf->owner))
1270 goto out_release;
1272 /* Now protected by module ref count */
1273 rcu_read_unlock();
1275 err = pf->create(net, sock, protocol, kern);
1276 if (err < 0)
1277 goto out_module_put;
1280 * Now to bump the refcnt of the [loadable] module that owns this
1281 * socket at sock_release time we decrement its refcnt.
1283 if (!try_module_get(sock->ops->owner))
1284 goto out_module_busy;
1287 * Now that we're done with the ->create function, the [loadable]
1288 * module can have its refcnt decremented
1290 module_put(pf->owner);
1291 err = security_socket_post_create(sock, family, type, protocol, kern);
1292 if (err)
1293 goto out_sock_release;
1294 *res = sock;
1296 return 0;
1298 out_module_busy:
1299 err = -EAFNOSUPPORT;
1300 out_module_put:
1301 sock->ops = NULL;
1302 module_put(pf->owner);
1303 out_sock_release:
1304 sock_release(sock);
1305 return err;
1307 out_release:
1308 rcu_read_unlock();
1309 goto out_sock_release;
1311 EXPORT_SYMBOL(__sock_create);
1313 int sock_create(int family, int type, int protocol, struct socket **res)
1315 return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0);
1317 EXPORT_SYMBOL(sock_create);
1319 int sock_create_kern(struct net *net, int family, int type, int protocol, struct socket **res)
1321 return __sock_create(net, family, type, protocol, res, 1);
1323 EXPORT_SYMBOL(sock_create_kern);
1325 SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol)
1327 int retval;
1328 struct socket *sock;
1329 int flags;
1331 /* Check the SOCK_* constants for consistency. */
1332 BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC);
1333 BUILD_BUG_ON((SOCK_MAX | SOCK_TYPE_MASK) != SOCK_TYPE_MASK);
1334 BUILD_BUG_ON(SOCK_CLOEXEC & SOCK_TYPE_MASK);
1335 BUILD_BUG_ON(SOCK_NONBLOCK & SOCK_TYPE_MASK);
1337 flags = type & ~SOCK_TYPE_MASK;
1338 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1339 return -EINVAL;
1340 type &= SOCK_TYPE_MASK;
1342 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1343 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1345 retval = sock_create(family, type, protocol, &sock);
1346 if (retval < 0)
1347 goto out;
1349 retval = sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK));
1350 if (retval < 0)
1351 goto out_release;
1353 out:
1354 /* It may be already another descriptor 8) Not kernel problem. */
1355 return retval;
1357 out_release:
1358 sock_release(sock);
1359 return retval;
1363 * Create a pair of connected sockets.
1366 SYSCALL_DEFINE4(socketpair, int, family, int, type, int, protocol,
1367 int __user *, usockvec)
1369 struct socket *sock1, *sock2;
1370 int fd1, fd2, err;
1371 struct file *newfile1, *newfile2;
1372 int flags;
1374 flags = type & ~SOCK_TYPE_MASK;
1375 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1376 return -EINVAL;
1377 type &= SOCK_TYPE_MASK;
1379 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1380 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1383 * Obtain the first socket and check if the underlying protocol
1384 * supports the socketpair call.
1387 err = sock_create(family, type, protocol, &sock1);
1388 if (err < 0)
1389 goto out;
1391 err = sock_create(family, type, protocol, &sock2);
1392 if (err < 0)
1393 goto out_release_1;
1395 err = sock1->ops->socketpair(sock1, sock2);
1396 if (err < 0)
1397 goto out_release_both;
1399 fd1 = get_unused_fd_flags(flags);
1400 if (unlikely(fd1 < 0)) {
1401 err = fd1;
1402 goto out_release_both;
1405 fd2 = get_unused_fd_flags(flags);
1406 if (unlikely(fd2 < 0)) {
1407 err = fd2;
1408 goto out_put_unused_1;
1411 newfile1 = sock_alloc_file(sock1, flags, NULL);
1412 if (IS_ERR(newfile1)) {
1413 err = PTR_ERR(newfile1);
1414 goto out_put_unused_both;
1417 newfile2 = sock_alloc_file(sock2, flags, NULL);
1418 if (IS_ERR(newfile2)) {
1419 err = PTR_ERR(newfile2);
1420 goto out_fput_1;
1423 err = put_user(fd1, &usockvec[0]);
1424 if (err)
1425 goto out_fput_both;
1427 err = put_user(fd2, &usockvec[1]);
1428 if (err)
1429 goto out_fput_both;
1431 audit_fd_pair(fd1, fd2);
1433 fd_install(fd1, newfile1);
1434 fd_install(fd2, newfile2);
1435 /* fd1 and fd2 may be already another descriptors.
1436 * Not kernel problem.
1439 return 0;
1441 out_fput_both:
1442 fput(newfile2);
1443 fput(newfile1);
1444 put_unused_fd(fd2);
1445 put_unused_fd(fd1);
1446 goto out;
1448 out_fput_1:
1449 fput(newfile1);
1450 put_unused_fd(fd2);
1451 put_unused_fd(fd1);
1452 sock_release(sock2);
1453 goto out;
1455 out_put_unused_both:
1456 put_unused_fd(fd2);
1457 out_put_unused_1:
1458 put_unused_fd(fd1);
1459 out_release_both:
1460 sock_release(sock2);
1461 out_release_1:
1462 sock_release(sock1);
1463 out:
1464 return err;
1468 * Bind a name to a socket. Nothing much to do here since it's
1469 * the protocol's responsibility to handle the local address.
1471 * We move the socket address to kernel space before we call
1472 * the protocol layer (having also checked the address is ok).
1475 SYSCALL_DEFINE3(bind, int, fd, struct sockaddr __user *, umyaddr, int, addrlen)
1477 struct socket *sock;
1478 struct sockaddr_storage address;
1479 int err, fput_needed;
1481 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1482 if (sock) {
1483 err = move_addr_to_kernel(umyaddr, addrlen, &address);
1484 if (err >= 0) {
1485 err = security_socket_bind(sock,
1486 (struct sockaddr *)&address,
1487 addrlen);
1488 if (!err)
1489 err = sock->ops->bind(sock,
1490 (struct sockaddr *)
1491 &address, addrlen);
1493 fput_light(sock->file, fput_needed);
1495 return err;
1499 * Perform a listen. Basically, we allow the protocol to do anything
1500 * necessary for a listen, and if that works, we mark the socket as
1501 * ready for listening.
1504 SYSCALL_DEFINE2(listen, int, fd, int, backlog)
1506 struct socket *sock;
1507 int err, fput_needed;
1508 int somaxconn;
1510 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1511 if (sock) {
1512 somaxconn = sock_net(sock->sk)->core.sysctl_somaxconn;
1513 if ((unsigned int)backlog > somaxconn)
1514 backlog = somaxconn;
1516 err = security_socket_listen(sock, backlog);
1517 if (!err)
1518 err = sock->ops->listen(sock, backlog);
1520 fput_light(sock->file, fput_needed);
1522 return err;
1526 * For accept, we attempt to create a new socket, set up the link
1527 * with the client, wake up the client, then return the new
1528 * connected fd. We collect the address of the connector in kernel
1529 * space and move it to user at the very end. This is unclean because
1530 * we open the socket then return an error.
1532 * 1003.1g adds the ability to recvmsg() to query connection pending
1533 * status to recvmsg. We need to add that support in a way thats
1534 * clean when we restucture accept also.
1537 SYSCALL_DEFINE4(accept4, int, fd, struct sockaddr __user *, upeer_sockaddr,
1538 int __user *, upeer_addrlen, int, flags)
1540 struct socket *sock, *newsock;
1541 struct file *newfile;
1542 int err, len, newfd, fput_needed;
1543 struct sockaddr_storage address;
1545 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1546 return -EINVAL;
1548 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1549 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1551 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1552 if (!sock)
1553 goto out;
1555 err = -ENFILE;
1556 newsock = sock_alloc();
1557 if (!newsock)
1558 goto out_put;
1560 newsock->type = sock->type;
1561 newsock->ops = sock->ops;
1564 * We don't need try_module_get here, as the listening socket (sock)
1565 * has the protocol module (sock->ops->owner) held.
1567 __module_get(newsock->ops->owner);
1569 newfd = get_unused_fd_flags(flags);
1570 if (unlikely(newfd < 0)) {
1571 err = newfd;
1572 sock_release(newsock);
1573 goto out_put;
1575 newfile = sock_alloc_file(newsock, flags, sock->sk->sk_prot_creator->name);
1576 if (IS_ERR(newfile)) {
1577 err = PTR_ERR(newfile);
1578 put_unused_fd(newfd);
1579 sock_release(newsock);
1580 goto out_put;
1583 err = security_socket_accept(sock, newsock);
1584 if (err)
1585 goto out_fd;
1587 err = sock->ops->accept(sock, newsock, sock->file->f_flags, false);
1588 if (err < 0)
1589 goto out_fd;
1591 if (upeer_sockaddr) {
1592 if (newsock->ops->getname(newsock, (struct sockaddr *)&address,
1593 &len, 2) < 0) {
1594 err = -ECONNABORTED;
1595 goto out_fd;
1597 err = move_addr_to_user(&address,
1598 len, upeer_sockaddr, upeer_addrlen);
1599 if (err < 0)
1600 goto out_fd;
1603 /* File flags are not inherited via accept() unlike another OSes. */
1605 fd_install(newfd, newfile);
1606 err = newfd;
1608 out_put:
1609 fput_light(sock->file, fput_needed);
1610 out:
1611 return err;
1612 out_fd:
1613 fput(newfile);
1614 put_unused_fd(newfd);
1615 goto out_put;
1618 SYSCALL_DEFINE3(accept, int, fd, struct sockaddr __user *, upeer_sockaddr,
1619 int __user *, upeer_addrlen)
1621 return sys_accept4(fd, upeer_sockaddr, upeer_addrlen, 0);
1625 * Attempt to connect to a socket with the server address. The address
1626 * is in user space so we verify it is OK and move it to kernel space.
1628 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
1629 * break bindings
1631 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
1632 * other SEQPACKET protocols that take time to connect() as it doesn't
1633 * include the -EINPROGRESS status for such sockets.
1636 SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
1637 int, addrlen)
1639 struct socket *sock;
1640 struct sockaddr_storage address;
1641 int err, fput_needed;
1643 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1644 if (!sock)
1645 goto out;
1646 err = move_addr_to_kernel(uservaddr, addrlen, &address);
1647 if (err < 0)
1648 goto out_put;
1650 err =
1651 security_socket_connect(sock, (struct sockaddr *)&address, addrlen);
1652 if (err)
1653 goto out_put;
1655 err = sock->ops->connect(sock, (struct sockaddr *)&address, addrlen,
1656 sock->file->f_flags);
1657 out_put:
1658 fput_light(sock->file, fput_needed);
1659 out:
1660 return err;
1664 * Get the local address ('name') of a socket object. Move the obtained
1665 * name to user space.
1668 SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr,
1669 int __user *, usockaddr_len)
1671 struct socket *sock;
1672 struct sockaddr_storage address;
1673 int len, err, fput_needed;
1675 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1676 if (!sock)
1677 goto out;
1679 err = security_socket_getsockname(sock);
1680 if (err)
1681 goto out_put;
1683 err = sock->ops->getname(sock, (struct sockaddr *)&address, &len, 0);
1684 if (err)
1685 goto out_put;
1686 err = move_addr_to_user(&address, len, usockaddr, usockaddr_len);
1688 out_put:
1689 fput_light(sock->file, fput_needed);
1690 out:
1691 return err;
1695 * Get the remote address ('name') of a socket object. Move the obtained
1696 * name to user space.
1699 SYSCALL_DEFINE3(getpeername, int, fd, struct sockaddr __user *, usockaddr,
1700 int __user *, usockaddr_len)
1702 struct socket *sock;
1703 struct sockaddr_storage address;
1704 int len, err, fput_needed;
1706 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1707 if (sock != NULL) {
1708 err = security_socket_getpeername(sock);
1709 if (err) {
1710 fput_light(sock->file, fput_needed);
1711 return err;
1714 err =
1715 sock->ops->getname(sock, (struct sockaddr *)&address, &len,
1717 if (!err)
1718 err = move_addr_to_user(&address, len, usockaddr,
1719 usockaddr_len);
1720 fput_light(sock->file, fput_needed);
1722 return err;
1726 * Send a datagram to a given address. We move the address into kernel
1727 * space and check the user space data area is readable before invoking
1728 * the protocol.
1731 SYSCALL_DEFINE6(sendto, int, fd, void __user *, buff, size_t, len,
1732 unsigned int, flags, struct sockaddr __user *, addr,
1733 int, addr_len)
1735 struct socket *sock;
1736 struct sockaddr_storage address;
1737 int err;
1738 struct msghdr msg;
1739 struct iovec iov;
1740 int fput_needed;
1742 err = import_single_range(WRITE, buff, len, &iov, &msg.msg_iter);
1743 if (unlikely(err))
1744 return err;
1745 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1746 if (!sock)
1747 goto out;
1749 msg.msg_name = NULL;
1750 msg.msg_control = NULL;
1751 msg.msg_controllen = 0;
1752 msg.msg_namelen = 0;
1753 if (addr) {
1754 err = move_addr_to_kernel(addr, addr_len, &address);
1755 if (err < 0)
1756 goto out_put;
1757 msg.msg_name = (struct sockaddr *)&address;
1758 msg.msg_namelen = addr_len;
1760 if (sock->file->f_flags & O_NONBLOCK)
1761 flags |= MSG_DONTWAIT;
1762 msg.msg_flags = flags;
1763 err = sock_sendmsg(sock, &msg);
1765 out_put:
1766 fput_light(sock->file, fput_needed);
1767 out:
1768 return err;
1772 * Send a datagram down a socket.
1775 SYSCALL_DEFINE4(send, int, fd, void __user *, buff, size_t, len,
1776 unsigned int, flags)
1778 return sys_sendto(fd, buff, len, flags, NULL, 0);
1782 * Receive a frame from the socket and optionally record the address of the
1783 * sender. We verify the buffers are writable and if needed move the
1784 * sender address from kernel to user space.
1787 SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, ubuf, size_t, size,
1788 unsigned int, flags, struct sockaddr __user *, addr,
1789 int __user *, addr_len)
1791 struct socket *sock;
1792 struct iovec iov;
1793 struct msghdr msg;
1794 struct sockaddr_storage address;
1795 int err, err2;
1796 int fput_needed;
1798 err = import_single_range(READ, ubuf, size, &iov, &msg.msg_iter);
1799 if (unlikely(err))
1800 return err;
1801 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1802 if (!sock)
1803 goto out;
1805 msg.msg_control = NULL;
1806 msg.msg_controllen = 0;
1807 /* Save some cycles and don't copy the address if not needed */
1808 msg.msg_name = addr ? (struct sockaddr *)&address : NULL;
1809 /* We assume all kernel code knows the size of sockaddr_storage */
1810 msg.msg_namelen = 0;
1811 msg.msg_iocb = NULL;
1812 msg.msg_flags = 0;
1813 if (sock->file->f_flags & O_NONBLOCK)
1814 flags |= MSG_DONTWAIT;
1815 err = sock_recvmsg(sock, &msg, flags);
1817 if (err >= 0 && addr != NULL) {
1818 err2 = move_addr_to_user(&address,
1819 msg.msg_namelen, addr, addr_len);
1820 if (err2 < 0)
1821 err = err2;
1824 fput_light(sock->file, fput_needed);
1825 out:
1826 return err;
1830 * Receive a datagram from a socket.
1833 SYSCALL_DEFINE4(recv, int, fd, void __user *, ubuf, size_t, size,
1834 unsigned int, flags)
1836 return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
1840 * Set a socket option. Because we don't know the option lengths we have
1841 * to pass the user mode parameter for the protocols to sort out.
1844 SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,
1845 char __user *, optval, int, optlen)
1847 int err, fput_needed;
1848 struct socket *sock;
1850 if (optlen < 0)
1851 return -EINVAL;
1853 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1854 if (sock != NULL) {
1855 err = security_socket_setsockopt(sock, level, optname);
1856 if (err)
1857 goto out_put;
1859 if (level == SOL_SOCKET)
1860 err =
1861 sock_setsockopt(sock, level, optname, optval,
1862 optlen);
1863 else
1864 err =
1865 sock->ops->setsockopt(sock, level, optname, optval,
1866 optlen);
1867 out_put:
1868 fput_light(sock->file, fput_needed);
1870 return err;
1874 * Get a socket option. Because we don't know the option lengths we have
1875 * to pass a user mode parameter for the protocols to sort out.
1878 SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname,
1879 char __user *, optval, int __user *, optlen)
1881 int err, fput_needed;
1882 struct socket *sock;
1884 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1885 if (sock != NULL) {
1886 err = security_socket_getsockopt(sock, level, optname);
1887 if (err)
1888 goto out_put;
1890 if (level == SOL_SOCKET)
1891 err =
1892 sock_getsockopt(sock, level, optname, optval,
1893 optlen);
1894 else
1895 err =
1896 sock->ops->getsockopt(sock, level, optname, optval,
1897 optlen);
1898 out_put:
1899 fput_light(sock->file, fput_needed);
1901 return err;
1905 * Shutdown a socket.
1908 SYSCALL_DEFINE2(shutdown, int, fd, int, how)
1910 int err, fput_needed;
1911 struct socket *sock;
1913 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1914 if (sock != NULL) {
1915 err = security_socket_shutdown(sock, how);
1916 if (!err)
1917 err = sock->ops->shutdown(sock, how);
1918 fput_light(sock->file, fput_needed);
1920 return err;
1923 /* A couple of helpful macros for getting the address of the 32/64 bit
1924 * fields which are the same type (int / unsigned) on our platforms.
1926 #define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
1927 #define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
1928 #define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
1930 struct used_address {
1931 struct sockaddr_storage name;
1932 unsigned int name_len;
1935 static int copy_msghdr_from_user(struct msghdr *kmsg,
1936 struct user_msghdr __user *umsg,
1937 struct sockaddr __user **save_addr,
1938 struct iovec **iov)
1940 struct user_msghdr msg;
1941 ssize_t err;
1943 if (copy_from_user(&msg, umsg, sizeof(*umsg)))
1944 return -EFAULT;
1946 kmsg->msg_control = (void __force *)msg.msg_control;
1947 kmsg->msg_controllen = msg.msg_controllen;
1948 kmsg->msg_flags = msg.msg_flags;
1950 kmsg->msg_namelen = msg.msg_namelen;
1951 if (!msg.msg_name)
1952 kmsg->msg_namelen = 0;
1954 if (kmsg->msg_namelen < 0)
1955 return -EINVAL;
1957 if (kmsg->msg_namelen > sizeof(struct sockaddr_storage))
1958 kmsg->msg_namelen = sizeof(struct sockaddr_storage);
1960 if (save_addr)
1961 *save_addr = msg.msg_name;
1963 if (msg.msg_name && kmsg->msg_namelen) {
1964 if (!save_addr) {
1965 err = move_addr_to_kernel(msg.msg_name,
1966 kmsg->msg_namelen,
1967 kmsg->msg_name);
1968 if (err < 0)
1969 return err;
1971 } else {
1972 kmsg->msg_name = NULL;
1973 kmsg->msg_namelen = 0;
1976 if (msg.msg_iovlen > UIO_MAXIOV)
1977 return -EMSGSIZE;
1979 kmsg->msg_iocb = NULL;
1981 return import_iovec(save_addr ? READ : WRITE,
1982 msg.msg_iov, msg.msg_iovlen,
1983 UIO_FASTIOV, iov, &kmsg->msg_iter);
1986 static int ___sys_sendmsg(struct socket *sock, struct user_msghdr __user *msg,
1987 struct msghdr *msg_sys, unsigned int flags,
1988 struct used_address *used_address,
1989 unsigned int allowed_msghdr_flags)
1991 struct compat_msghdr __user *msg_compat =
1992 (struct compat_msghdr __user *)msg;
1993 struct sockaddr_storage address;
1994 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
1995 unsigned char ctl[sizeof(struct cmsghdr) + 20]
1996 __aligned(sizeof(__kernel_size_t));
1997 /* 20 is size of ipv6_pktinfo */
1998 unsigned char *ctl_buf = ctl;
1999 int ctl_len;
2000 ssize_t err;
2002 msg_sys->msg_name = &address;
2004 if (MSG_CMSG_COMPAT & flags)
2005 err = get_compat_msghdr(msg_sys, msg_compat, NULL, &iov);
2006 else
2007 err = copy_msghdr_from_user(msg_sys, msg, NULL, &iov);
2008 if (err < 0)
2009 return err;
2011 err = -ENOBUFS;
2013 if (msg_sys->msg_controllen > INT_MAX)
2014 goto out_freeiov;
2015 flags |= (msg_sys->msg_flags & allowed_msghdr_flags);
2016 ctl_len = msg_sys->msg_controllen;
2017 if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
2018 err =
2019 cmsghdr_from_user_compat_to_kern(msg_sys, sock->sk, ctl,
2020 sizeof(ctl));
2021 if (err)
2022 goto out_freeiov;
2023 ctl_buf = msg_sys->msg_control;
2024 ctl_len = msg_sys->msg_controllen;
2025 } else if (ctl_len) {
2026 BUILD_BUG_ON(sizeof(struct cmsghdr) !=
2027 CMSG_ALIGN(sizeof(struct cmsghdr)));
2028 if (ctl_len > sizeof(ctl)) {
2029 ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
2030 if (ctl_buf == NULL)
2031 goto out_freeiov;
2033 err = -EFAULT;
2035 * Careful! Before this, msg_sys->msg_control contains a user pointer.
2036 * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
2037 * checking falls down on this.
2039 if (copy_from_user(ctl_buf,
2040 (void __user __force *)msg_sys->msg_control,
2041 ctl_len))
2042 goto out_freectl;
2043 msg_sys->msg_control = ctl_buf;
2045 msg_sys->msg_flags = flags;
2047 if (sock->file->f_flags & O_NONBLOCK)
2048 msg_sys->msg_flags |= MSG_DONTWAIT;
2050 * If this is sendmmsg() and current destination address is same as
2051 * previously succeeded address, omit asking LSM's decision.
2052 * used_address->name_len is initialized to UINT_MAX so that the first
2053 * destination address never matches.
2055 if (used_address && msg_sys->msg_name &&
2056 used_address->name_len == msg_sys->msg_namelen &&
2057 !memcmp(&used_address->name, msg_sys->msg_name,
2058 used_address->name_len)) {
2059 err = sock_sendmsg_nosec(sock, msg_sys);
2060 goto out_freectl;
2062 err = sock_sendmsg(sock, msg_sys);
2064 * If this is sendmmsg() and sending to current destination address was
2065 * successful, remember it.
2067 if (used_address && err >= 0) {
2068 used_address->name_len = msg_sys->msg_namelen;
2069 if (msg_sys->msg_name)
2070 memcpy(&used_address->name, msg_sys->msg_name,
2071 used_address->name_len);
2074 out_freectl:
2075 if (ctl_buf != ctl)
2076 sock_kfree_s(sock->sk, ctl_buf, ctl_len);
2077 out_freeiov:
2078 kfree(iov);
2079 return err;
2083 * BSD sendmsg interface
2086 long __sys_sendmsg(int fd, struct user_msghdr __user *msg, unsigned flags)
2088 int fput_needed, err;
2089 struct msghdr msg_sys;
2090 struct socket *sock;
2092 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2093 if (!sock)
2094 goto out;
2096 err = ___sys_sendmsg(sock, msg, &msg_sys, flags, NULL, 0);
2098 fput_light(sock->file, fput_needed);
2099 out:
2100 return err;
2103 SYSCALL_DEFINE3(sendmsg, int, fd, struct user_msghdr __user *, msg, unsigned int, flags)
2105 if (flags & MSG_CMSG_COMPAT)
2106 return -EINVAL;
2107 return __sys_sendmsg(fd, msg, flags);
2111 * Linux sendmmsg interface
2114 int __sys_sendmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
2115 unsigned int flags)
2117 int fput_needed, err, datagrams;
2118 struct socket *sock;
2119 struct mmsghdr __user *entry;
2120 struct compat_mmsghdr __user *compat_entry;
2121 struct msghdr msg_sys;
2122 struct used_address used_address;
2123 unsigned int oflags = flags;
2125 if (vlen > UIO_MAXIOV)
2126 vlen = UIO_MAXIOV;
2128 datagrams = 0;
2130 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2131 if (!sock)
2132 return err;
2134 used_address.name_len = UINT_MAX;
2135 entry = mmsg;
2136 compat_entry = (struct compat_mmsghdr __user *)mmsg;
2137 err = 0;
2138 flags |= MSG_BATCH;
2140 while (datagrams < vlen) {
2141 if (datagrams == vlen - 1)
2142 flags = oflags;
2144 if (MSG_CMSG_COMPAT & flags) {
2145 err = ___sys_sendmsg(sock, (struct user_msghdr __user *)compat_entry,
2146 &msg_sys, flags, &used_address, MSG_EOR);
2147 if (err < 0)
2148 break;
2149 err = __put_user(err, &compat_entry->msg_len);
2150 ++compat_entry;
2151 } else {
2152 err = ___sys_sendmsg(sock,
2153 (struct user_msghdr __user *)entry,
2154 &msg_sys, flags, &used_address, MSG_EOR);
2155 if (err < 0)
2156 break;
2157 err = put_user(err, &entry->msg_len);
2158 ++entry;
2161 if (err)
2162 break;
2163 ++datagrams;
2164 if (msg_data_left(&msg_sys))
2165 break;
2166 cond_resched();
2169 fput_light(sock->file, fput_needed);
2171 /* We only return an error if no datagrams were able to be sent */
2172 if (datagrams != 0)
2173 return datagrams;
2175 return err;
2178 SYSCALL_DEFINE4(sendmmsg, int, fd, struct mmsghdr __user *, mmsg,
2179 unsigned int, vlen, unsigned int, flags)
2181 if (flags & MSG_CMSG_COMPAT)
2182 return -EINVAL;
2183 return __sys_sendmmsg(fd, mmsg, vlen, flags);
2186 static int ___sys_recvmsg(struct socket *sock, struct user_msghdr __user *msg,
2187 struct msghdr *msg_sys, unsigned int flags, int nosec)
2189 struct compat_msghdr __user *msg_compat =
2190 (struct compat_msghdr __user *)msg;
2191 struct iovec iovstack[UIO_FASTIOV];
2192 struct iovec *iov = iovstack;
2193 unsigned long cmsg_ptr;
2194 int len;
2195 ssize_t err;
2197 /* kernel mode address */
2198 struct sockaddr_storage addr;
2200 /* user mode address pointers */
2201 struct sockaddr __user *uaddr;
2202 int __user *uaddr_len = COMPAT_NAMELEN(msg);
2204 msg_sys->msg_name = &addr;
2206 if (MSG_CMSG_COMPAT & flags)
2207 err = get_compat_msghdr(msg_sys, msg_compat, &uaddr, &iov);
2208 else
2209 err = copy_msghdr_from_user(msg_sys, msg, &uaddr, &iov);
2210 if (err < 0)
2211 return err;
2213 cmsg_ptr = (unsigned long)msg_sys->msg_control;
2214 msg_sys->msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
2216 /* We assume all kernel code knows the size of sockaddr_storage */
2217 msg_sys->msg_namelen = 0;
2219 if (sock->file->f_flags & O_NONBLOCK)
2220 flags |= MSG_DONTWAIT;
2221 err = (nosec ? sock_recvmsg_nosec : sock_recvmsg)(sock, msg_sys, flags);
2222 if (err < 0)
2223 goto out_freeiov;
2224 len = err;
2226 if (uaddr != NULL) {
2227 err = move_addr_to_user(&addr,
2228 msg_sys->msg_namelen, uaddr,
2229 uaddr_len);
2230 if (err < 0)
2231 goto out_freeiov;
2233 err = __put_user((msg_sys->msg_flags & ~MSG_CMSG_COMPAT),
2234 COMPAT_FLAGS(msg));
2235 if (err)
2236 goto out_freeiov;
2237 if (MSG_CMSG_COMPAT & flags)
2238 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
2239 &msg_compat->msg_controllen);
2240 else
2241 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
2242 &msg->msg_controllen);
2243 if (err)
2244 goto out_freeiov;
2245 err = len;
2247 out_freeiov:
2248 kfree(iov);
2249 return err;
2253 * BSD recvmsg interface
2256 long __sys_recvmsg(int fd, struct user_msghdr __user *msg, unsigned flags)
2258 int fput_needed, err;
2259 struct msghdr msg_sys;
2260 struct socket *sock;
2262 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2263 if (!sock)
2264 goto out;
2266 err = ___sys_recvmsg(sock, msg, &msg_sys, flags, 0);
2268 fput_light(sock->file, fput_needed);
2269 out:
2270 return err;
2273 SYSCALL_DEFINE3(recvmsg, int, fd, struct user_msghdr __user *, msg,
2274 unsigned int, flags)
2276 if (flags & MSG_CMSG_COMPAT)
2277 return -EINVAL;
2278 return __sys_recvmsg(fd, msg, flags);
2282 * Linux recvmmsg interface
2285 int __sys_recvmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
2286 unsigned int flags, struct timespec *timeout)
2288 int fput_needed, err, datagrams;
2289 struct socket *sock;
2290 struct mmsghdr __user *entry;
2291 struct compat_mmsghdr __user *compat_entry;
2292 struct msghdr msg_sys;
2293 struct timespec64 end_time;
2294 struct timespec64 timeout64;
2296 if (timeout &&
2297 poll_select_set_timeout(&end_time, timeout->tv_sec,
2298 timeout->tv_nsec))
2299 return -EINVAL;
2301 datagrams = 0;
2303 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2304 if (!sock)
2305 return err;
2307 err = sock_error(sock->sk);
2308 if (err) {
2309 datagrams = err;
2310 goto out_put;
2313 entry = mmsg;
2314 compat_entry = (struct compat_mmsghdr __user *)mmsg;
2316 while (datagrams < vlen) {
2318 * No need to ask LSM for more than the first datagram.
2320 if (MSG_CMSG_COMPAT & flags) {
2321 err = ___sys_recvmsg(sock, (struct user_msghdr __user *)compat_entry,
2322 &msg_sys, flags & ~MSG_WAITFORONE,
2323 datagrams);
2324 if (err < 0)
2325 break;
2326 err = __put_user(err, &compat_entry->msg_len);
2327 ++compat_entry;
2328 } else {
2329 err = ___sys_recvmsg(sock,
2330 (struct user_msghdr __user *)entry,
2331 &msg_sys, flags & ~MSG_WAITFORONE,
2332 datagrams);
2333 if (err < 0)
2334 break;
2335 err = put_user(err, &entry->msg_len);
2336 ++entry;
2339 if (err)
2340 break;
2341 ++datagrams;
2343 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
2344 if (flags & MSG_WAITFORONE)
2345 flags |= MSG_DONTWAIT;
2347 if (timeout) {
2348 ktime_get_ts64(&timeout64);
2349 *timeout = timespec64_to_timespec(
2350 timespec64_sub(end_time, timeout64));
2351 if (timeout->tv_sec < 0) {
2352 timeout->tv_sec = timeout->tv_nsec = 0;
2353 break;
2356 /* Timeout, return less than vlen datagrams */
2357 if (timeout->tv_nsec == 0 && timeout->tv_sec == 0)
2358 break;
2361 /* Out of band data, return right away */
2362 if (msg_sys.msg_flags & MSG_OOB)
2363 break;
2364 cond_resched();
2367 if (err == 0)
2368 goto out_put;
2370 if (datagrams == 0) {
2371 datagrams = err;
2372 goto out_put;
2376 * We may return less entries than requested (vlen) if the
2377 * sock is non block and there aren't enough datagrams...
2379 if (err != -EAGAIN) {
2381 * ... or if recvmsg returns an error after we
2382 * received some datagrams, where we record the
2383 * error to return on the next call or if the
2384 * app asks about it using getsockopt(SO_ERROR).
2386 sock->sk->sk_err = -err;
2388 out_put:
2389 fput_light(sock->file, fput_needed);
2391 return datagrams;
2394 SYSCALL_DEFINE5(recvmmsg, int, fd, struct mmsghdr __user *, mmsg,
2395 unsigned int, vlen, unsigned int, flags,
2396 struct timespec __user *, timeout)
2398 int datagrams;
2399 struct timespec timeout_sys;
2401 if (flags & MSG_CMSG_COMPAT)
2402 return -EINVAL;
2404 if (!timeout)
2405 return __sys_recvmmsg(fd, mmsg, vlen, flags, NULL);
2407 if (copy_from_user(&timeout_sys, timeout, sizeof(timeout_sys)))
2408 return -EFAULT;
2410 datagrams = __sys_recvmmsg(fd, mmsg, vlen, flags, &timeout_sys);
2412 if (datagrams > 0 &&
2413 copy_to_user(timeout, &timeout_sys, sizeof(timeout_sys)))
2414 datagrams = -EFAULT;
2416 return datagrams;
2419 #ifdef __ARCH_WANT_SYS_SOCKETCALL
2420 /* Argument list sizes for sys_socketcall */
2421 #define AL(x) ((x) * sizeof(unsigned long))
2422 static const unsigned char nargs[21] = {
2423 AL(0), AL(3), AL(3), AL(3), AL(2), AL(3),
2424 AL(3), AL(3), AL(4), AL(4), AL(4), AL(6),
2425 AL(6), AL(2), AL(5), AL(5), AL(3), AL(3),
2426 AL(4), AL(5), AL(4)
2429 #undef AL
2432 * System call vectors.
2434 * Argument checking cleaned up. Saved 20% in size.
2435 * This function doesn't need to set the kernel lock because
2436 * it is set by the callees.
2439 SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args)
2441 unsigned long a[AUDITSC_ARGS];
2442 unsigned long a0, a1;
2443 int err;
2444 unsigned int len;
2446 if (call < 1 || call > SYS_SENDMMSG)
2447 return -EINVAL;
2448 call = array_index_nospec(call, SYS_SENDMMSG + 1);
2450 len = nargs[call];
2451 if (len > sizeof(a))
2452 return -EINVAL;
2454 /* copy_from_user should be SMP safe. */
2455 if (copy_from_user(a, args, len))
2456 return -EFAULT;
2458 err = audit_socketcall(nargs[call] / sizeof(unsigned long), a);
2459 if (err)
2460 return err;
2462 a0 = a[0];
2463 a1 = a[1];
2465 switch (call) {
2466 case SYS_SOCKET:
2467 err = sys_socket(a0, a1, a[2]);
2468 break;
2469 case SYS_BIND:
2470 err = sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
2471 break;
2472 case SYS_CONNECT:
2473 err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
2474 break;
2475 case SYS_LISTEN:
2476 err = sys_listen(a0, a1);
2477 break;
2478 case SYS_ACCEPT:
2479 err = sys_accept4(a0, (struct sockaddr __user *)a1,
2480 (int __user *)a[2], 0);
2481 break;
2482 case SYS_GETSOCKNAME:
2483 err =
2484 sys_getsockname(a0, (struct sockaddr __user *)a1,
2485 (int __user *)a[2]);
2486 break;
2487 case SYS_GETPEERNAME:
2488 err =
2489 sys_getpeername(a0, (struct sockaddr __user *)a1,
2490 (int __user *)a[2]);
2491 break;
2492 case SYS_SOCKETPAIR:
2493 err = sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
2494 break;
2495 case SYS_SEND:
2496 err = sys_send(a0, (void __user *)a1, a[2], a[3]);
2497 break;
2498 case SYS_SENDTO:
2499 err = sys_sendto(a0, (void __user *)a1, a[2], a[3],
2500 (struct sockaddr __user *)a[4], a[5]);
2501 break;
2502 case SYS_RECV:
2503 err = sys_recv(a0, (void __user *)a1, a[2], a[3]);
2504 break;
2505 case SYS_RECVFROM:
2506 err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
2507 (struct sockaddr __user *)a[4],
2508 (int __user *)a[5]);
2509 break;
2510 case SYS_SHUTDOWN:
2511 err = sys_shutdown(a0, a1);
2512 break;
2513 case SYS_SETSOCKOPT:
2514 err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]);
2515 break;
2516 case SYS_GETSOCKOPT:
2517 err =
2518 sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
2519 (int __user *)a[4]);
2520 break;
2521 case SYS_SENDMSG:
2522 err = sys_sendmsg(a0, (struct user_msghdr __user *)a1, a[2]);
2523 break;
2524 case SYS_SENDMMSG:
2525 err = sys_sendmmsg(a0, (struct mmsghdr __user *)a1, a[2], a[3]);
2526 break;
2527 case SYS_RECVMSG:
2528 err = sys_recvmsg(a0, (struct user_msghdr __user *)a1, a[2]);
2529 break;
2530 case SYS_RECVMMSG:
2531 err = sys_recvmmsg(a0, (struct mmsghdr __user *)a1, a[2], a[3],
2532 (struct timespec __user *)a[4]);
2533 break;
2534 case SYS_ACCEPT4:
2535 err = sys_accept4(a0, (struct sockaddr __user *)a1,
2536 (int __user *)a[2], a[3]);
2537 break;
2538 default:
2539 err = -EINVAL;
2540 break;
2542 return err;
2545 #endif /* __ARCH_WANT_SYS_SOCKETCALL */
2548 * sock_register - add a socket protocol handler
2549 * @ops: description of protocol
2551 * This function is called by a protocol handler that wants to
2552 * advertise its address family, and have it linked into the
2553 * socket interface. The value ops->family corresponds to the
2554 * socket system call protocol family.
2556 int sock_register(const struct net_proto_family *ops)
2558 int err;
2560 if (ops->family >= NPROTO) {
2561 pr_crit("protocol %d >= NPROTO(%d)\n", ops->family, NPROTO);
2562 return -ENOBUFS;
2565 spin_lock(&net_family_lock);
2566 if (rcu_dereference_protected(net_families[ops->family],
2567 lockdep_is_held(&net_family_lock)))
2568 err = -EEXIST;
2569 else {
2570 rcu_assign_pointer(net_families[ops->family], ops);
2571 err = 0;
2573 spin_unlock(&net_family_lock);
2575 pr_info("NET: Registered protocol family %d\n", ops->family);
2576 return err;
2578 EXPORT_SYMBOL(sock_register);
2581 * sock_unregister - remove a protocol handler
2582 * @family: protocol family to remove
2584 * This function is called by a protocol handler that wants to
2585 * remove its address family, and have it unlinked from the
2586 * new socket creation.
2588 * If protocol handler is a module, then it can use module reference
2589 * counts to protect against new references. If protocol handler is not
2590 * a module then it needs to provide its own protection in
2591 * the ops->create routine.
2593 void sock_unregister(int family)
2595 BUG_ON(family < 0 || family >= NPROTO);
2597 spin_lock(&net_family_lock);
2598 RCU_INIT_POINTER(net_families[family], NULL);
2599 spin_unlock(&net_family_lock);
2601 synchronize_rcu();
2603 pr_info("NET: Unregistered protocol family %d\n", family);
2605 EXPORT_SYMBOL(sock_unregister);
2607 static int __init sock_init(void)
2609 int err;
2611 * Initialize the network sysctl infrastructure.
2613 err = net_sysctl_init();
2614 if (err)
2615 goto out;
2618 * Initialize skbuff SLAB cache
2620 skb_init();
2623 * Initialize the protocols module.
2626 init_inodecache();
2628 err = register_filesystem(&sock_fs_type);
2629 if (err)
2630 goto out_fs;
2631 sock_mnt = kern_mount(&sock_fs_type);
2632 if (IS_ERR(sock_mnt)) {
2633 err = PTR_ERR(sock_mnt);
2634 goto out_mount;
2637 /* The real protocol initialization is performed in later initcalls.
2640 #ifdef CONFIG_NETFILTER
2641 err = netfilter_init();
2642 if (err)
2643 goto out;
2644 #endif
2646 ptp_classifier_init();
2648 out:
2649 return err;
2651 out_mount:
2652 unregister_filesystem(&sock_fs_type);
2653 out_fs:
2654 goto out;
2657 core_initcall(sock_init); /* early initcall */
2659 #ifdef CONFIG_PROC_FS
2660 void socket_seq_show(struct seq_file *seq)
2662 int cpu;
2663 int counter = 0;
2665 for_each_possible_cpu(cpu)
2666 counter += per_cpu(sockets_in_use, cpu);
2668 /* It can be negative, by the way. 8) */
2669 if (counter < 0)
2670 counter = 0;
2672 seq_printf(seq, "sockets: used %d\n", counter);
2674 #endif /* CONFIG_PROC_FS */
2676 #ifdef CONFIG_COMPAT
2677 static int do_siocgstamp(struct net *net, struct socket *sock,
2678 unsigned int cmd, void __user *up)
2680 mm_segment_t old_fs = get_fs();
2681 struct timeval ktv;
2682 int err;
2684 set_fs(KERNEL_DS);
2685 err = sock_do_ioctl(net, sock, cmd, (unsigned long)&ktv);
2686 set_fs(old_fs);
2687 if (!err)
2688 err = compat_put_timeval(&ktv, up);
2690 return err;
2693 static int do_siocgstampns(struct net *net, struct socket *sock,
2694 unsigned int cmd, void __user *up)
2696 mm_segment_t old_fs = get_fs();
2697 struct timespec kts;
2698 int err;
2700 set_fs(KERNEL_DS);
2701 err = sock_do_ioctl(net, sock, cmd, (unsigned long)&kts);
2702 set_fs(old_fs);
2703 if (!err)
2704 err = compat_put_timespec(&kts, up);
2706 return err;
2709 static int dev_ifname32(struct net *net, struct compat_ifreq __user *uifr32)
2711 struct ifreq __user *uifr;
2712 int err;
2714 uifr = compat_alloc_user_space(sizeof(struct ifreq));
2715 if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
2716 return -EFAULT;
2718 err = dev_ioctl(net, SIOCGIFNAME, uifr);
2719 if (err)
2720 return err;
2722 if (copy_in_user(uifr32, uifr, sizeof(struct compat_ifreq)))
2723 return -EFAULT;
2725 return 0;
2728 static int dev_ifconf(struct net *net, struct compat_ifconf __user *uifc32)
2730 struct compat_ifconf ifc32;
2731 struct ifconf ifc;
2732 struct ifconf __user *uifc;
2733 struct compat_ifreq __user *ifr32;
2734 struct ifreq __user *ifr;
2735 unsigned int i, j;
2736 int err;
2738 if (copy_from_user(&ifc32, uifc32, sizeof(struct compat_ifconf)))
2739 return -EFAULT;
2741 memset(&ifc, 0, sizeof(ifc));
2742 if (ifc32.ifcbuf == 0) {
2743 ifc32.ifc_len = 0;
2744 ifc.ifc_len = 0;
2745 ifc.ifc_req = NULL;
2746 uifc = compat_alloc_user_space(sizeof(struct ifconf));
2747 } else {
2748 size_t len = ((ifc32.ifc_len / sizeof(struct compat_ifreq)) + 1) *
2749 sizeof(struct ifreq);
2750 uifc = compat_alloc_user_space(sizeof(struct ifconf) + len);
2751 ifc.ifc_len = len;
2752 ifr = ifc.ifc_req = (void __user *)(uifc + 1);
2753 ifr32 = compat_ptr(ifc32.ifcbuf);
2754 for (i = 0; i < ifc32.ifc_len; i += sizeof(struct compat_ifreq)) {
2755 if (copy_in_user(ifr, ifr32, sizeof(struct compat_ifreq)))
2756 return -EFAULT;
2757 ifr++;
2758 ifr32++;
2761 if (copy_to_user(uifc, &ifc, sizeof(struct ifconf)))
2762 return -EFAULT;
2764 err = dev_ioctl(net, SIOCGIFCONF, uifc);
2765 if (err)
2766 return err;
2768 if (copy_from_user(&ifc, uifc, sizeof(struct ifconf)))
2769 return -EFAULT;
2771 ifr = ifc.ifc_req;
2772 ifr32 = compat_ptr(ifc32.ifcbuf);
2773 for (i = 0, j = 0;
2774 i + sizeof(struct compat_ifreq) <= ifc32.ifc_len && j < ifc.ifc_len;
2775 i += sizeof(struct compat_ifreq), j += sizeof(struct ifreq)) {
2776 if (copy_in_user(ifr32, ifr, sizeof(struct compat_ifreq)))
2777 return -EFAULT;
2778 ifr32++;
2779 ifr++;
2782 if (ifc32.ifcbuf == 0) {
2783 /* Translate from 64-bit structure multiple to
2784 * a 32-bit one.
2786 i = ifc.ifc_len;
2787 i = ((i / sizeof(struct ifreq)) * sizeof(struct compat_ifreq));
2788 ifc32.ifc_len = i;
2789 } else {
2790 ifc32.ifc_len = i;
2792 if (copy_to_user(uifc32, &ifc32, sizeof(struct compat_ifconf)))
2793 return -EFAULT;
2795 return 0;
2798 static int ethtool_ioctl(struct net *net, struct compat_ifreq __user *ifr32)
2800 struct compat_ethtool_rxnfc __user *compat_rxnfc;
2801 bool convert_in = false, convert_out = false;
2802 size_t buf_size = ALIGN(sizeof(struct ifreq), 8);
2803 struct ethtool_rxnfc __user *rxnfc;
2804 struct ifreq __user *ifr;
2805 u32 rule_cnt = 0, actual_rule_cnt;
2806 u32 ethcmd;
2807 u32 data;
2808 int ret;
2810 if (get_user(data, &ifr32->ifr_ifru.ifru_data))
2811 return -EFAULT;
2813 compat_rxnfc = compat_ptr(data);
2815 if (get_user(ethcmd, &compat_rxnfc->cmd))
2816 return -EFAULT;
2818 /* Most ethtool structures are defined without padding.
2819 * Unfortunately struct ethtool_rxnfc is an exception.
2821 switch (ethcmd) {
2822 default:
2823 break;
2824 case ETHTOOL_GRXCLSRLALL:
2825 /* Buffer size is variable */
2826 if (get_user(rule_cnt, &compat_rxnfc->rule_cnt))
2827 return -EFAULT;
2828 if (rule_cnt > KMALLOC_MAX_SIZE / sizeof(u32))
2829 return -ENOMEM;
2830 buf_size += rule_cnt * sizeof(u32);
2831 /* fall through */
2832 case ETHTOOL_GRXRINGS:
2833 case ETHTOOL_GRXCLSRLCNT:
2834 case ETHTOOL_GRXCLSRULE:
2835 case ETHTOOL_SRXCLSRLINS:
2836 convert_out = true;
2837 /* fall through */
2838 case ETHTOOL_SRXCLSRLDEL:
2839 buf_size += sizeof(struct ethtool_rxnfc);
2840 convert_in = true;
2841 break;
2844 ifr = compat_alloc_user_space(buf_size);
2845 rxnfc = (void __user *)ifr + ALIGN(sizeof(struct ifreq), 8);
2847 if (copy_in_user(&ifr->ifr_name, &ifr32->ifr_name, IFNAMSIZ))
2848 return -EFAULT;
2850 if (put_user(convert_in ? rxnfc : compat_ptr(data),
2851 &ifr->ifr_ifru.ifru_data))
2852 return -EFAULT;
2854 if (convert_in) {
2855 /* We expect there to be holes between fs.m_ext and
2856 * fs.ring_cookie and at the end of fs, but nowhere else.
2858 BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc, fs.m_ext) +
2859 sizeof(compat_rxnfc->fs.m_ext) !=
2860 offsetof(struct ethtool_rxnfc, fs.m_ext) +
2861 sizeof(rxnfc->fs.m_ext));
2862 BUILD_BUG_ON(
2863 offsetof(struct compat_ethtool_rxnfc, fs.location) -
2864 offsetof(struct compat_ethtool_rxnfc, fs.ring_cookie) !=
2865 offsetof(struct ethtool_rxnfc, fs.location) -
2866 offsetof(struct ethtool_rxnfc, fs.ring_cookie));
2868 if (copy_in_user(rxnfc, compat_rxnfc,
2869 (void __user *)(&rxnfc->fs.m_ext + 1) -
2870 (void __user *)rxnfc) ||
2871 copy_in_user(&rxnfc->fs.ring_cookie,
2872 &compat_rxnfc->fs.ring_cookie,
2873 (void __user *)(&rxnfc->fs.location + 1) -
2874 (void __user *)&rxnfc->fs.ring_cookie))
2875 return -EFAULT;
2876 if (ethcmd == ETHTOOL_GRXCLSRLALL) {
2877 if (put_user(rule_cnt, &rxnfc->rule_cnt))
2878 return -EFAULT;
2879 } else if (copy_in_user(&rxnfc->rule_cnt,
2880 &compat_rxnfc->rule_cnt,
2881 sizeof(rxnfc->rule_cnt)))
2882 return -EFAULT;
2885 ret = dev_ioctl(net, SIOCETHTOOL, ifr);
2886 if (ret)
2887 return ret;
2889 if (convert_out) {
2890 if (copy_in_user(compat_rxnfc, rxnfc,
2891 (const void __user *)(&rxnfc->fs.m_ext + 1) -
2892 (const void __user *)rxnfc) ||
2893 copy_in_user(&compat_rxnfc->fs.ring_cookie,
2894 &rxnfc->fs.ring_cookie,
2895 (const void __user *)(&rxnfc->fs.location + 1) -
2896 (const void __user *)&rxnfc->fs.ring_cookie) ||
2897 copy_in_user(&compat_rxnfc->rule_cnt, &rxnfc->rule_cnt,
2898 sizeof(rxnfc->rule_cnt)))
2899 return -EFAULT;
2901 if (ethcmd == ETHTOOL_GRXCLSRLALL) {
2902 /* As an optimisation, we only copy the actual
2903 * number of rules that the underlying
2904 * function returned. Since Mallory might
2905 * change the rule count in user memory, we
2906 * check that it is less than the rule count
2907 * originally given (as the user buffer size),
2908 * which has been range-checked.
2910 if (get_user(actual_rule_cnt, &rxnfc->rule_cnt))
2911 return -EFAULT;
2912 if (actual_rule_cnt < rule_cnt)
2913 rule_cnt = actual_rule_cnt;
2914 if (copy_in_user(&compat_rxnfc->rule_locs[0],
2915 &rxnfc->rule_locs[0],
2916 rule_cnt * sizeof(u32)))
2917 return -EFAULT;
2921 return 0;
2924 static int compat_siocwandev(struct net *net, struct compat_ifreq __user *uifr32)
2926 void __user *uptr;
2927 compat_uptr_t uptr32;
2928 struct ifreq __user *uifr;
2930 uifr = compat_alloc_user_space(sizeof(*uifr));
2931 if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
2932 return -EFAULT;
2934 if (get_user(uptr32, &uifr32->ifr_settings.ifs_ifsu))
2935 return -EFAULT;
2937 uptr = compat_ptr(uptr32);
2939 if (put_user(uptr, &uifr->ifr_settings.ifs_ifsu.raw_hdlc))
2940 return -EFAULT;
2942 return dev_ioctl(net, SIOCWANDEV, uifr);
2945 static int bond_ioctl(struct net *net, unsigned int cmd,
2946 struct compat_ifreq __user *ifr32)
2948 struct ifreq kifr;
2949 mm_segment_t old_fs;
2950 int err;
2952 switch (cmd) {
2953 case SIOCBONDENSLAVE:
2954 case SIOCBONDRELEASE:
2955 case SIOCBONDSETHWADDR:
2956 case SIOCBONDCHANGEACTIVE:
2957 if (copy_from_user(&kifr, ifr32, sizeof(struct compat_ifreq)))
2958 return -EFAULT;
2960 old_fs = get_fs();
2961 set_fs(KERNEL_DS);
2962 err = dev_ioctl(net, cmd,
2963 (struct ifreq __user __force *) &kifr);
2964 set_fs(old_fs);
2966 return err;
2967 default:
2968 return -ENOIOCTLCMD;
2972 /* Handle ioctls that use ifreq::ifr_data and just need struct ifreq converted */
2973 static int compat_ifr_data_ioctl(struct net *net, unsigned int cmd,
2974 struct compat_ifreq __user *u_ifreq32)
2976 struct ifreq __user *u_ifreq64;
2977 char tmp_buf[IFNAMSIZ];
2978 void __user *data64;
2979 u32 data32;
2981 if (copy_from_user(&tmp_buf[0], &(u_ifreq32->ifr_ifrn.ifrn_name[0]),
2982 IFNAMSIZ))
2983 return -EFAULT;
2984 if (get_user(data32, &u_ifreq32->ifr_ifru.ifru_data))
2985 return -EFAULT;
2986 data64 = compat_ptr(data32);
2988 u_ifreq64 = compat_alloc_user_space(sizeof(*u_ifreq64));
2990 if (copy_to_user(&u_ifreq64->ifr_ifrn.ifrn_name[0], &tmp_buf[0],
2991 IFNAMSIZ))
2992 return -EFAULT;
2993 if (put_user(data64, &u_ifreq64->ifr_ifru.ifru_data))
2994 return -EFAULT;
2996 return dev_ioctl(net, cmd, u_ifreq64);
2999 static int dev_ifsioc(struct net *net, struct socket *sock,
3000 unsigned int cmd, struct compat_ifreq __user *uifr32)
3002 struct ifreq __user *uifr;
3003 int err;
3005 uifr = compat_alloc_user_space(sizeof(*uifr));
3006 if (copy_in_user(uifr, uifr32, sizeof(*uifr32)))
3007 return -EFAULT;
3009 err = sock_do_ioctl(net, sock, cmd, (unsigned long)uifr);
3011 if (!err) {
3012 switch (cmd) {
3013 case SIOCGIFFLAGS:
3014 case SIOCGIFMETRIC:
3015 case SIOCGIFMTU:
3016 case SIOCGIFMEM:
3017 case SIOCGIFHWADDR:
3018 case SIOCGIFINDEX:
3019 case SIOCGIFADDR:
3020 case SIOCGIFBRDADDR:
3021 case SIOCGIFDSTADDR:
3022 case SIOCGIFNETMASK:
3023 case SIOCGIFPFLAGS:
3024 case SIOCGIFTXQLEN:
3025 case SIOCGMIIPHY:
3026 case SIOCGMIIREG:
3027 if (copy_in_user(uifr32, uifr, sizeof(*uifr32)))
3028 err = -EFAULT;
3029 break;
3032 return err;
3035 static int compat_sioc_ifmap(struct net *net, unsigned int cmd,
3036 struct compat_ifreq __user *uifr32)
3038 struct ifreq ifr;
3039 struct compat_ifmap __user *uifmap32;
3040 mm_segment_t old_fs;
3041 int err;
3043 uifmap32 = &uifr32->ifr_ifru.ifru_map;
3044 err = copy_from_user(&ifr, uifr32, sizeof(ifr.ifr_name));
3045 err |= get_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
3046 err |= get_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
3047 err |= get_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
3048 err |= get_user(ifr.ifr_map.irq, &uifmap32->irq);
3049 err |= get_user(ifr.ifr_map.dma, &uifmap32->dma);
3050 err |= get_user(ifr.ifr_map.port, &uifmap32->port);
3051 if (err)
3052 return -EFAULT;
3054 old_fs = get_fs();
3055 set_fs(KERNEL_DS);
3056 err = dev_ioctl(net, cmd, (void __user __force *)&ifr);
3057 set_fs(old_fs);
3059 if (cmd == SIOCGIFMAP && !err) {
3060 err = copy_to_user(uifr32, &ifr, sizeof(ifr.ifr_name));
3061 err |= put_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
3062 err |= put_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
3063 err |= put_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
3064 err |= put_user(ifr.ifr_map.irq, &uifmap32->irq);
3065 err |= put_user(ifr.ifr_map.dma, &uifmap32->dma);
3066 err |= put_user(ifr.ifr_map.port, &uifmap32->port);
3067 if (err)
3068 err = -EFAULT;
3070 return err;
3073 struct rtentry32 {
3074 u32 rt_pad1;
3075 struct sockaddr rt_dst; /* target address */
3076 struct sockaddr rt_gateway; /* gateway addr (RTF_GATEWAY) */
3077 struct sockaddr rt_genmask; /* target network mask (IP) */
3078 unsigned short rt_flags;
3079 short rt_pad2;
3080 u32 rt_pad3;
3081 unsigned char rt_tos;
3082 unsigned char rt_class;
3083 short rt_pad4;
3084 short rt_metric; /* +1 for binary compatibility! */
3085 /* char * */ u32 rt_dev; /* forcing the device at add */
3086 u32 rt_mtu; /* per route MTU/Window */
3087 u32 rt_window; /* Window clamping */
3088 unsigned short rt_irtt; /* Initial RTT */
3091 struct in6_rtmsg32 {
3092 struct in6_addr rtmsg_dst;
3093 struct in6_addr rtmsg_src;
3094 struct in6_addr rtmsg_gateway;
3095 u32 rtmsg_type;
3096 u16 rtmsg_dst_len;
3097 u16 rtmsg_src_len;
3098 u32 rtmsg_metric;
3099 u32 rtmsg_info;
3100 u32 rtmsg_flags;
3101 s32 rtmsg_ifindex;
3104 static int routing_ioctl(struct net *net, struct socket *sock,
3105 unsigned int cmd, void __user *argp)
3107 int ret;
3108 void *r = NULL;
3109 struct in6_rtmsg r6;
3110 struct rtentry r4;
3111 char devname[16];
3112 u32 rtdev;
3113 mm_segment_t old_fs = get_fs();
3115 if (sock && sock->sk && sock->sk->sk_family == AF_INET6) { /* ipv6 */
3116 struct in6_rtmsg32 __user *ur6 = argp;
3117 ret = copy_from_user(&r6.rtmsg_dst, &(ur6->rtmsg_dst),
3118 3 * sizeof(struct in6_addr));
3119 ret |= get_user(r6.rtmsg_type, &(ur6->rtmsg_type));
3120 ret |= get_user(r6.rtmsg_dst_len, &(ur6->rtmsg_dst_len));
3121 ret |= get_user(r6.rtmsg_src_len, &(ur6->rtmsg_src_len));
3122 ret |= get_user(r6.rtmsg_metric, &(ur6->rtmsg_metric));
3123 ret |= get_user(r6.rtmsg_info, &(ur6->rtmsg_info));
3124 ret |= get_user(r6.rtmsg_flags, &(ur6->rtmsg_flags));
3125 ret |= get_user(r6.rtmsg_ifindex, &(ur6->rtmsg_ifindex));
3127 r = (void *) &r6;
3128 } else { /* ipv4 */
3129 struct rtentry32 __user *ur4 = argp;
3130 ret = copy_from_user(&r4.rt_dst, &(ur4->rt_dst),
3131 3 * sizeof(struct sockaddr));
3132 ret |= get_user(r4.rt_flags, &(ur4->rt_flags));
3133 ret |= get_user(r4.rt_metric, &(ur4->rt_metric));
3134 ret |= get_user(r4.rt_mtu, &(ur4->rt_mtu));
3135 ret |= get_user(r4.rt_window, &(ur4->rt_window));
3136 ret |= get_user(r4.rt_irtt, &(ur4->rt_irtt));
3137 ret |= get_user(rtdev, &(ur4->rt_dev));
3138 if (rtdev) {
3139 ret |= copy_from_user(devname, compat_ptr(rtdev), 15);
3140 r4.rt_dev = (char __user __force *)devname;
3141 devname[15] = 0;
3142 } else
3143 r4.rt_dev = NULL;
3145 r = (void *) &r4;
3148 if (ret) {
3149 ret = -EFAULT;
3150 goto out;
3153 set_fs(KERNEL_DS);
3154 ret = sock_do_ioctl(net, sock, cmd, (unsigned long) r);
3155 set_fs(old_fs);
3157 out:
3158 return ret;
3161 /* Since old style bridge ioctl's endup using SIOCDEVPRIVATE
3162 * for some operations; this forces use of the newer bridge-utils that
3163 * use compatible ioctls
3165 static int old_bridge_ioctl(compat_ulong_t __user *argp)
3167 compat_ulong_t tmp;
3169 if (get_user(tmp, argp))
3170 return -EFAULT;
3171 if (tmp == BRCTL_GET_VERSION)
3172 return BRCTL_VERSION + 1;
3173 return -EINVAL;
3176 static int compat_sock_ioctl_trans(struct file *file, struct socket *sock,
3177 unsigned int cmd, unsigned long arg)
3179 void __user *argp = compat_ptr(arg);
3180 struct sock *sk = sock->sk;
3181 struct net *net = sock_net(sk);
3183 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))
3184 return compat_ifr_data_ioctl(net, cmd, argp);
3186 switch (cmd) {
3187 case SIOCSIFBR:
3188 case SIOCGIFBR:
3189 return old_bridge_ioctl(argp);
3190 case SIOCGIFNAME:
3191 return dev_ifname32(net, argp);
3192 case SIOCGIFCONF:
3193 return dev_ifconf(net, argp);
3194 case SIOCETHTOOL:
3195 return ethtool_ioctl(net, argp);
3196 case SIOCWANDEV:
3197 return compat_siocwandev(net, argp);
3198 case SIOCGIFMAP:
3199 case SIOCSIFMAP:
3200 return compat_sioc_ifmap(net, cmd, argp);
3201 case SIOCBONDENSLAVE:
3202 case SIOCBONDRELEASE:
3203 case SIOCBONDSETHWADDR:
3204 case SIOCBONDCHANGEACTIVE:
3205 return bond_ioctl(net, cmd, argp);
3206 case SIOCADDRT:
3207 case SIOCDELRT:
3208 return routing_ioctl(net, sock, cmd, argp);
3209 case SIOCGSTAMP:
3210 return do_siocgstamp(net, sock, cmd, argp);
3211 case SIOCGSTAMPNS:
3212 return do_siocgstampns(net, sock, cmd, argp);
3213 case SIOCBONDSLAVEINFOQUERY:
3214 case SIOCBONDINFOQUERY:
3215 case SIOCSHWTSTAMP:
3216 case SIOCGHWTSTAMP:
3217 return compat_ifr_data_ioctl(net, cmd, argp);
3219 case FIOSETOWN:
3220 case SIOCSPGRP:
3221 case FIOGETOWN:
3222 case SIOCGPGRP:
3223 case SIOCBRADDBR:
3224 case SIOCBRDELBR:
3225 case SIOCGIFVLAN:
3226 case SIOCSIFVLAN:
3227 case SIOCADDDLCI:
3228 case SIOCDELDLCI:
3229 case SIOCGSKNS:
3230 return sock_ioctl(file, cmd, arg);
3232 case SIOCGIFFLAGS:
3233 case SIOCSIFFLAGS:
3234 case SIOCGIFMETRIC:
3235 case SIOCSIFMETRIC:
3236 case SIOCGIFMTU:
3237 case SIOCSIFMTU:
3238 case SIOCGIFMEM:
3239 case SIOCSIFMEM:
3240 case SIOCGIFHWADDR:
3241 case SIOCSIFHWADDR:
3242 case SIOCADDMULTI:
3243 case SIOCDELMULTI:
3244 case SIOCGIFINDEX:
3245 case SIOCGIFADDR:
3246 case SIOCSIFADDR:
3247 case SIOCSIFHWBROADCAST:
3248 case SIOCDIFADDR:
3249 case SIOCGIFBRDADDR:
3250 case SIOCSIFBRDADDR:
3251 case SIOCGIFDSTADDR:
3252 case SIOCSIFDSTADDR:
3253 case SIOCGIFNETMASK:
3254 case SIOCSIFNETMASK:
3255 case SIOCSIFPFLAGS:
3256 case SIOCGIFPFLAGS:
3257 case SIOCGIFTXQLEN:
3258 case SIOCSIFTXQLEN:
3259 case SIOCBRADDIF:
3260 case SIOCBRDELIF:
3261 case SIOCSIFNAME:
3262 case SIOCGMIIPHY:
3263 case SIOCGMIIREG:
3264 case SIOCSMIIREG:
3265 return dev_ifsioc(net, sock, cmd, argp);
3267 case SIOCSARP:
3268 case SIOCGARP:
3269 case SIOCDARP:
3270 case SIOCOUTQNSD:
3271 case SIOCATMARK:
3272 return sock_do_ioctl(net, sock, cmd, arg);
3275 return -ENOIOCTLCMD;
3278 static long compat_sock_ioctl(struct file *file, unsigned int cmd,
3279 unsigned long arg)
3281 struct socket *sock = file->private_data;
3282 int ret = -ENOIOCTLCMD;
3283 struct sock *sk;
3284 struct net *net;
3286 sk = sock->sk;
3287 net = sock_net(sk);
3289 if (sock->ops->compat_ioctl)
3290 ret = sock->ops->compat_ioctl(sock, cmd, arg);
3292 if (ret == -ENOIOCTLCMD &&
3293 (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST))
3294 ret = compat_wext_handle_ioctl(net, cmd, arg);
3296 if (ret == -ENOIOCTLCMD)
3297 ret = compat_sock_ioctl_trans(file, sock, cmd, arg);
3299 return ret;
3301 #endif
3303 int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
3305 return sock->ops->bind(sock, addr, addrlen);
3307 EXPORT_SYMBOL(kernel_bind);
3309 int kernel_listen(struct socket *sock, int backlog)
3311 return sock->ops->listen(sock, backlog);
3313 EXPORT_SYMBOL(kernel_listen);
3315 int kernel_accept(struct socket *sock, struct socket **newsock, int flags)
3317 struct sock *sk = sock->sk;
3318 int err;
3320 err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
3321 newsock);
3322 if (err < 0)
3323 goto done;
3325 err = sock->ops->accept(sock, *newsock, flags, true);
3326 if (err < 0) {
3327 sock_release(*newsock);
3328 *newsock = NULL;
3329 goto done;
3332 (*newsock)->ops = sock->ops;
3333 __module_get((*newsock)->ops->owner);
3335 done:
3336 return err;
3338 EXPORT_SYMBOL(kernel_accept);
3340 int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
3341 int flags)
3343 return sock->ops->connect(sock, addr, addrlen, flags);
3345 EXPORT_SYMBOL(kernel_connect);
3347 int kernel_getsockname(struct socket *sock, struct sockaddr *addr,
3348 int *addrlen)
3350 return sock->ops->getname(sock, addr, addrlen, 0);
3352 EXPORT_SYMBOL(kernel_getsockname);
3354 int kernel_getpeername(struct socket *sock, struct sockaddr *addr,
3355 int *addrlen)
3357 return sock->ops->getname(sock, addr, addrlen, 1);
3359 EXPORT_SYMBOL(kernel_getpeername);
3361 int kernel_getsockopt(struct socket *sock, int level, int optname,
3362 char *optval, int *optlen)
3364 mm_segment_t oldfs = get_fs();
3365 char __user *uoptval;
3366 int __user *uoptlen;
3367 int err;
3369 uoptval = (char __user __force *) optval;
3370 uoptlen = (int __user __force *) optlen;
3372 set_fs(KERNEL_DS);
3373 if (level == SOL_SOCKET)
3374 err = sock_getsockopt(sock, level, optname, uoptval, uoptlen);
3375 else
3376 err = sock->ops->getsockopt(sock, level, optname, uoptval,
3377 uoptlen);
3378 set_fs(oldfs);
3379 return err;
3381 EXPORT_SYMBOL(kernel_getsockopt);
3383 int kernel_setsockopt(struct socket *sock, int level, int optname,
3384 char *optval, unsigned int optlen)
3386 mm_segment_t oldfs = get_fs();
3387 char __user *uoptval;
3388 int err;
3390 uoptval = (char __user __force *) optval;
3392 set_fs(KERNEL_DS);
3393 if (level == SOL_SOCKET)
3394 err = sock_setsockopt(sock, level, optname, uoptval, optlen);
3395 else
3396 err = sock->ops->setsockopt(sock, level, optname, uoptval,
3397 optlen);
3398 set_fs(oldfs);
3399 return err;
3401 EXPORT_SYMBOL(kernel_setsockopt);
3403 int kernel_sendpage(struct socket *sock, struct page *page, int offset,
3404 size_t size, int flags)
3406 if (sock->ops->sendpage)
3407 return sock->ops->sendpage(sock, page, offset, size, flags);
3409 return sock_no_sendpage(sock, page, offset, size, flags);
3411 EXPORT_SYMBOL(kernel_sendpage);
3413 int kernel_sendpage_locked(struct sock *sk, struct page *page, int offset,
3414 size_t size, int flags)
3416 struct socket *sock = sk->sk_socket;
3418 if (sock->ops->sendpage_locked)
3419 return sock->ops->sendpage_locked(sk, page, offset, size,
3420 flags);
3422 return sock_no_sendpage_locked(sk, page, offset, size, flags);
3424 EXPORT_SYMBOL(kernel_sendpage_locked);
3426 int kernel_sock_ioctl(struct socket *sock, int cmd, unsigned long arg)
3428 mm_segment_t oldfs = get_fs();
3429 int err;
3431 set_fs(KERNEL_DS);
3432 err = sock->ops->ioctl(sock, cmd, arg);
3433 set_fs(oldfs);
3435 return err;
3437 EXPORT_SYMBOL(kernel_sock_ioctl);
3439 int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how)
3441 return sock->ops->shutdown(sock, how);
3443 EXPORT_SYMBOL(kernel_sock_shutdown);
3445 /* This routine returns the IP overhead imposed by a socket i.e.
3446 * the length of the underlying IP header, depending on whether
3447 * this is an IPv4 or IPv6 socket and the length from IP options turned
3448 * on at the socket. Assumes that the caller has a lock on the socket.
3450 u32 kernel_sock_ip_overhead(struct sock *sk)
3452 struct inet_sock *inet;
3453 struct ip_options_rcu *opt;
3454 u32 overhead = 0;
3455 #if IS_ENABLED(CONFIG_IPV6)
3456 struct ipv6_pinfo *np;
3457 struct ipv6_txoptions *optv6 = NULL;
3458 #endif /* IS_ENABLED(CONFIG_IPV6) */
3460 if (!sk)
3461 return overhead;
3463 switch (sk->sk_family) {
3464 case AF_INET:
3465 inet = inet_sk(sk);
3466 overhead += sizeof(struct iphdr);
3467 opt = rcu_dereference_protected(inet->inet_opt,
3468 sock_owned_by_user(sk));
3469 if (opt)
3470 overhead += opt->opt.optlen;
3471 return overhead;
3472 #if IS_ENABLED(CONFIG_IPV6)
3473 case AF_INET6:
3474 np = inet6_sk(sk);
3475 overhead += sizeof(struct ipv6hdr);
3476 if (np)
3477 optv6 = rcu_dereference_protected(np->opt,
3478 sock_owned_by_user(sk));
3479 if (optv6)
3480 overhead += (optv6->opt_flen + optv6->opt_nflen);
3481 return overhead;
3482 #endif /* IS_ENABLED(CONFIG_IPV6) */
3483 default: /* Returns 0 overhead if the socket is not ipv4 or ipv6 */
3484 return overhead;
3487 EXPORT_SYMBOL(kernel_sock_ip_overhead);