Linux 4.14.74
[linux-stable.git] / net / socket.c
blobd27922639a209cefafa44cf30efa9fe76970967b
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 if (inode)
604 inode_unlock(inode);
605 sock->ops = NULL;
606 module_put(owner);
609 if (rcu_dereference_protected(sock->wq, 1)->fasync_list)
610 pr_err("%s: fasync list not empty!\n", __func__);
612 this_cpu_sub(sockets_in_use, 1);
613 if (!sock->file) {
614 iput(SOCK_INODE(sock));
615 return;
617 sock->file = NULL;
620 void sock_release(struct socket *sock)
622 __sock_release(sock, NULL);
624 EXPORT_SYMBOL(sock_release);
626 void __sock_tx_timestamp(__u16 tsflags, __u8 *tx_flags)
628 u8 flags = *tx_flags;
630 if (tsflags & SOF_TIMESTAMPING_TX_HARDWARE)
631 flags |= SKBTX_HW_TSTAMP;
633 if (tsflags & SOF_TIMESTAMPING_TX_SOFTWARE)
634 flags |= SKBTX_SW_TSTAMP;
636 if (tsflags & SOF_TIMESTAMPING_TX_SCHED)
637 flags |= SKBTX_SCHED_TSTAMP;
639 *tx_flags = flags;
641 EXPORT_SYMBOL(__sock_tx_timestamp);
643 static inline int sock_sendmsg_nosec(struct socket *sock, struct msghdr *msg)
645 int ret = sock->ops->sendmsg(sock, msg, msg_data_left(msg));
646 BUG_ON(ret == -EIOCBQUEUED);
647 return ret;
650 int sock_sendmsg(struct socket *sock, struct msghdr *msg)
652 int err = security_socket_sendmsg(sock, msg,
653 msg_data_left(msg));
655 return err ?: sock_sendmsg_nosec(sock, msg);
657 EXPORT_SYMBOL(sock_sendmsg);
659 int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
660 struct kvec *vec, size_t num, size_t size)
662 iov_iter_kvec(&msg->msg_iter, WRITE | ITER_KVEC, vec, num, size);
663 return sock_sendmsg(sock, msg);
665 EXPORT_SYMBOL(kernel_sendmsg);
667 int kernel_sendmsg_locked(struct sock *sk, struct msghdr *msg,
668 struct kvec *vec, size_t num, size_t size)
670 struct socket *sock = sk->sk_socket;
672 if (!sock->ops->sendmsg_locked)
673 return sock_no_sendmsg_locked(sk, msg, size);
675 iov_iter_kvec(&msg->msg_iter, WRITE | ITER_KVEC, vec, num, size);
677 return sock->ops->sendmsg_locked(sk, msg, msg_data_left(msg));
679 EXPORT_SYMBOL(kernel_sendmsg_locked);
681 static bool skb_is_err_queue(const struct sk_buff *skb)
683 /* pkt_type of skbs enqueued on the error queue are set to
684 * PACKET_OUTGOING in skb_set_err_queue(). This is only safe to do
685 * in recvmsg, since skbs received on a local socket will never
686 * have a pkt_type of PACKET_OUTGOING.
688 return skb->pkt_type == PACKET_OUTGOING;
691 /* On transmit, software and hardware timestamps are returned independently.
692 * As the two skb clones share the hardware timestamp, which may be updated
693 * before the software timestamp is received, a hardware TX timestamp may be
694 * returned only if there is no software TX timestamp. Ignore false software
695 * timestamps, which may be made in the __sock_recv_timestamp() call when the
696 * option SO_TIMESTAMP(NS) is enabled on the socket, even when the skb has a
697 * hardware timestamp.
699 static bool skb_is_swtx_tstamp(const struct sk_buff *skb, int false_tstamp)
701 return skb->tstamp && !false_tstamp && skb_is_err_queue(skb);
704 static void put_ts_pktinfo(struct msghdr *msg, struct sk_buff *skb)
706 struct scm_ts_pktinfo ts_pktinfo;
707 struct net_device *orig_dev;
709 if (!skb_mac_header_was_set(skb))
710 return;
712 memset(&ts_pktinfo, 0, sizeof(ts_pktinfo));
714 rcu_read_lock();
715 orig_dev = dev_get_by_napi_id(skb_napi_id(skb));
716 if (orig_dev)
717 ts_pktinfo.if_index = orig_dev->ifindex;
718 rcu_read_unlock();
720 ts_pktinfo.pkt_length = skb->len - skb_mac_offset(skb);
721 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPING_PKTINFO,
722 sizeof(ts_pktinfo), &ts_pktinfo);
726 * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
728 void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
729 struct sk_buff *skb)
731 int need_software_tstamp = sock_flag(sk, SOCK_RCVTSTAMP);
732 struct scm_timestamping tss;
733 int empty = 1, false_tstamp = 0;
734 struct skb_shared_hwtstamps *shhwtstamps =
735 skb_hwtstamps(skb);
737 /* Race occurred between timestamp enabling and packet
738 receiving. Fill in the current time for now. */
739 if (need_software_tstamp && skb->tstamp == 0) {
740 __net_timestamp(skb);
741 false_tstamp = 1;
744 if (need_software_tstamp) {
745 if (!sock_flag(sk, SOCK_RCVTSTAMPNS)) {
746 struct timeval tv;
747 skb_get_timestamp(skb, &tv);
748 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMP,
749 sizeof(tv), &tv);
750 } else {
751 struct timespec ts;
752 skb_get_timestampns(skb, &ts);
753 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPNS,
754 sizeof(ts), &ts);
758 memset(&tss, 0, sizeof(tss));
759 if ((sk->sk_tsflags & SOF_TIMESTAMPING_SOFTWARE) &&
760 ktime_to_timespec_cond(skb->tstamp, tss.ts + 0))
761 empty = 0;
762 if (shhwtstamps &&
763 (sk->sk_tsflags & SOF_TIMESTAMPING_RAW_HARDWARE) &&
764 !skb_is_swtx_tstamp(skb, false_tstamp) &&
765 ktime_to_timespec_cond(shhwtstamps->hwtstamp, tss.ts + 2)) {
766 empty = 0;
767 if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_PKTINFO) &&
768 !skb_is_err_queue(skb))
769 put_ts_pktinfo(msg, skb);
771 if (!empty) {
772 put_cmsg(msg, SOL_SOCKET,
773 SCM_TIMESTAMPING, sizeof(tss), &tss);
775 if (skb_is_err_queue(skb) && skb->len &&
776 SKB_EXT_ERR(skb)->opt_stats)
777 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPING_OPT_STATS,
778 skb->len, skb->data);
781 EXPORT_SYMBOL_GPL(__sock_recv_timestamp);
783 void __sock_recv_wifi_status(struct msghdr *msg, struct sock *sk,
784 struct sk_buff *skb)
786 int ack;
788 if (!sock_flag(sk, SOCK_WIFI_STATUS))
789 return;
790 if (!skb->wifi_acked_valid)
791 return;
793 ack = skb->wifi_acked;
795 put_cmsg(msg, SOL_SOCKET, SCM_WIFI_STATUS, sizeof(ack), &ack);
797 EXPORT_SYMBOL_GPL(__sock_recv_wifi_status);
799 static inline void sock_recv_drops(struct msghdr *msg, struct sock *sk,
800 struct sk_buff *skb)
802 if (sock_flag(sk, SOCK_RXQ_OVFL) && skb && SOCK_SKB_CB(skb)->dropcount)
803 put_cmsg(msg, SOL_SOCKET, SO_RXQ_OVFL,
804 sizeof(__u32), &SOCK_SKB_CB(skb)->dropcount);
807 void __sock_recv_ts_and_drops(struct msghdr *msg, struct sock *sk,
808 struct sk_buff *skb)
810 sock_recv_timestamp(msg, sk, skb);
811 sock_recv_drops(msg, sk, skb);
813 EXPORT_SYMBOL_GPL(__sock_recv_ts_and_drops);
815 static inline int sock_recvmsg_nosec(struct socket *sock, struct msghdr *msg,
816 int flags)
818 return sock->ops->recvmsg(sock, msg, msg_data_left(msg), flags);
821 int sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags)
823 int err = security_socket_recvmsg(sock, msg, msg_data_left(msg), flags);
825 return err ?: sock_recvmsg_nosec(sock, msg, flags);
827 EXPORT_SYMBOL(sock_recvmsg);
830 * kernel_recvmsg - Receive a message from a socket (kernel space)
831 * @sock: The socket to receive the message from
832 * @msg: Received message
833 * @vec: Input s/g array for message data
834 * @num: Size of input s/g array
835 * @size: Number of bytes to read
836 * @flags: Message flags (MSG_DONTWAIT, etc...)
838 * On return the msg structure contains the scatter/gather array passed in the
839 * vec argument. The array is modified so that it consists of the unfilled
840 * portion of the original array.
842 * The returned value is the total number of bytes received, or an error.
844 int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
845 struct kvec *vec, size_t num, size_t size, int flags)
847 mm_segment_t oldfs = get_fs();
848 int result;
850 iov_iter_kvec(&msg->msg_iter, READ | ITER_KVEC, vec, num, size);
851 set_fs(KERNEL_DS);
852 result = sock_recvmsg(sock, msg, flags);
853 set_fs(oldfs);
854 return result;
856 EXPORT_SYMBOL(kernel_recvmsg);
858 static ssize_t sock_sendpage(struct file *file, struct page *page,
859 int offset, size_t size, loff_t *ppos, int more)
861 struct socket *sock;
862 int flags;
864 sock = file->private_data;
866 flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
867 /* more is a combination of MSG_MORE and MSG_SENDPAGE_NOTLAST */
868 flags |= more;
870 return kernel_sendpage(sock, page, offset, size, flags);
873 static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
874 struct pipe_inode_info *pipe, size_t len,
875 unsigned int flags)
877 struct socket *sock = file->private_data;
879 if (unlikely(!sock->ops->splice_read))
880 return -EINVAL;
882 return sock->ops->splice_read(sock, ppos, pipe, len, flags);
885 static ssize_t sock_read_iter(struct kiocb *iocb, struct iov_iter *to)
887 struct file *file = iocb->ki_filp;
888 struct socket *sock = file->private_data;
889 struct msghdr msg = {.msg_iter = *to,
890 .msg_iocb = iocb};
891 ssize_t res;
893 if (file->f_flags & O_NONBLOCK)
894 msg.msg_flags = MSG_DONTWAIT;
896 if (iocb->ki_pos != 0)
897 return -ESPIPE;
899 if (!iov_iter_count(to)) /* Match SYS5 behaviour */
900 return 0;
902 res = sock_recvmsg(sock, &msg, msg.msg_flags);
903 *to = msg.msg_iter;
904 return res;
907 static ssize_t sock_write_iter(struct kiocb *iocb, struct iov_iter *from)
909 struct file *file = iocb->ki_filp;
910 struct socket *sock = file->private_data;
911 struct msghdr msg = {.msg_iter = *from,
912 .msg_iocb = iocb};
913 ssize_t res;
915 if (iocb->ki_pos != 0)
916 return -ESPIPE;
918 if (file->f_flags & O_NONBLOCK)
919 msg.msg_flags = MSG_DONTWAIT;
921 if (sock->type == SOCK_SEQPACKET)
922 msg.msg_flags |= MSG_EOR;
924 res = sock_sendmsg(sock, &msg);
925 *from = msg.msg_iter;
926 return res;
930 * Atomic setting of ioctl hooks to avoid race
931 * with module unload.
934 static DEFINE_MUTEX(br_ioctl_mutex);
935 static int (*br_ioctl_hook) (struct net *, unsigned int cmd, void __user *arg);
937 void brioctl_set(int (*hook) (struct net *, unsigned int, void __user *))
939 mutex_lock(&br_ioctl_mutex);
940 br_ioctl_hook = hook;
941 mutex_unlock(&br_ioctl_mutex);
943 EXPORT_SYMBOL(brioctl_set);
945 static DEFINE_MUTEX(vlan_ioctl_mutex);
946 static int (*vlan_ioctl_hook) (struct net *, void __user *arg);
948 void vlan_ioctl_set(int (*hook) (struct net *, void __user *))
950 mutex_lock(&vlan_ioctl_mutex);
951 vlan_ioctl_hook = hook;
952 mutex_unlock(&vlan_ioctl_mutex);
954 EXPORT_SYMBOL(vlan_ioctl_set);
956 static DEFINE_MUTEX(dlci_ioctl_mutex);
957 static int (*dlci_ioctl_hook) (unsigned int, void __user *);
959 void dlci_ioctl_set(int (*hook) (unsigned int, void __user *))
961 mutex_lock(&dlci_ioctl_mutex);
962 dlci_ioctl_hook = hook;
963 mutex_unlock(&dlci_ioctl_mutex);
965 EXPORT_SYMBOL(dlci_ioctl_set);
967 static long sock_do_ioctl(struct net *net, struct socket *sock,
968 unsigned int cmd, unsigned long arg)
970 int err;
971 void __user *argp = (void __user *)arg;
973 err = sock->ops->ioctl(sock, cmd, arg);
976 * If this ioctl is unknown try to hand it down
977 * to the NIC driver.
979 if (err == -ENOIOCTLCMD)
980 err = dev_ioctl(net, cmd, argp);
982 return err;
986 * With an ioctl, arg may well be a user mode pointer, but we don't know
987 * what to do with it - that's up to the protocol still.
990 static struct ns_common *get_net_ns(struct ns_common *ns)
992 return &get_net(container_of(ns, struct net, ns))->ns;
995 static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
997 struct socket *sock;
998 struct sock *sk;
999 void __user *argp = (void __user *)arg;
1000 int pid, err;
1001 struct net *net;
1003 sock = file->private_data;
1004 sk = sock->sk;
1005 net = sock_net(sk);
1006 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
1007 err = dev_ioctl(net, cmd, argp);
1008 } else
1009 #ifdef CONFIG_WEXT_CORE
1010 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
1011 err = dev_ioctl(net, cmd, argp);
1012 } else
1013 #endif
1014 switch (cmd) {
1015 case FIOSETOWN:
1016 case SIOCSPGRP:
1017 err = -EFAULT;
1018 if (get_user(pid, (int __user *)argp))
1019 break;
1020 err = f_setown(sock->file, pid, 1);
1021 break;
1022 case FIOGETOWN:
1023 case SIOCGPGRP:
1024 err = put_user(f_getown(sock->file),
1025 (int __user *)argp);
1026 break;
1027 case SIOCGIFBR:
1028 case SIOCSIFBR:
1029 case SIOCBRADDBR:
1030 case SIOCBRDELBR:
1031 err = -ENOPKG;
1032 if (!br_ioctl_hook)
1033 request_module("bridge");
1035 mutex_lock(&br_ioctl_mutex);
1036 if (br_ioctl_hook)
1037 err = br_ioctl_hook(net, cmd, argp);
1038 mutex_unlock(&br_ioctl_mutex);
1039 break;
1040 case SIOCGIFVLAN:
1041 case SIOCSIFVLAN:
1042 err = -ENOPKG;
1043 if (!vlan_ioctl_hook)
1044 request_module("8021q");
1046 mutex_lock(&vlan_ioctl_mutex);
1047 if (vlan_ioctl_hook)
1048 err = vlan_ioctl_hook(net, argp);
1049 mutex_unlock(&vlan_ioctl_mutex);
1050 break;
1051 case SIOCADDDLCI:
1052 case SIOCDELDLCI:
1053 err = -ENOPKG;
1054 if (!dlci_ioctl_hook)
1055 request_module("dlci");
1057 mutex_lock(&dlci_ioctl_mutex);
1058 if (dlci_ioctl_hook)
1059 err = dlci_ioctl_hook(cmd, argp);
1060 mutex_unlock(&dlci_ioctl_mutex);
1061 break;
1062 case SIOCGSKNS:
1063 err = -EPERM;
1064 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1065 break;
1067 err = open_related_ns(&net->ns, get_net_ns);
1068 break;
1069 default:
1070 err = sock_do_ioctl(net, sock, cmd, arg);
1071 break;
1073 return err;
1076 int sock_create_lite(int family, int type, int protocol, struct socket **res)
1078 int err;
1079 struct socket *sock = NULL;
1081 err = security_socket_create(family, type, protocol, 1);
1082 if (err)
1083 goto out;
1085 sock = sock_alloc();
1086 if (!sock) {
1087 err = -ENOMEM;
1088 goto out;
1091 sock->type = type;
1092 err = security_socket_post_create(sock, family, type, protocol, 1);
1093 if (err)
1094 goto out_release;
1096 out:
1097 *res = sock;
1098 return err;
1099 out_release:
1100 sock_release(sock);
1101 sock = NULL;
1102 goto out;
1104 EXPORT_SYMBOL(sock_create_lite);
1106 /* No kernel lock held - perfect */
1107 static unsigned int sock_poll(struct file *file, poll_table *wait)
1109 unsigned int busy_flag = 0;
1110 struct socket *sock;
1113 * We can't return errors to poll, so it's either yes or no.
1115 sock = file->private_data;
1117 if (sk_can_busy_loop(sock->sk)) {
1118 /* this socket can poll_ll so tell the system call */
1119 busy_flag = POLL_BUSY_LOOP;
1121 /* once, only if requested by syscall */
1122 if (wait && (wait->_key & POLL_BUSY_LOOP))
1123 sk_busy_loop(sock->sk, 1);
1126 return busy_flag | sock->ops->poll(file, sock, wait);
1129 static int sock_mmap(struct file *file, struct vm_area_struct *vma)
1131 struct socket *sock = file->private_data;
1133 return sock->ops->mmap(file, sock, vma);
1136 static int sock_close(struct inode *inode, struct file *filp)
1138 __sock_release(SOCKET_I(inode), inode);
1139 return 0;
1143 * Update the socket async list
1145 * Fasync_list locking strategy.
1147 * 1. fasync_list is modified only under process context socket lock
1148 * i.e. under semaphore.
1149 * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
1150 * or under socket lock
1153 static int sock_fasync(int fd, struct file *filp, int on)
1155 struct socket *sock = filp->private_data;
1156 struct sock *sk = sock->sk;
1157 struct socket_wq *wq;
1159 if (sk == NULL)
1160 return -EINVAL;
1162 lock_sock(sk);
1163 wq = rcu_dereference_protected(sock->wq, lockdep_sock_is_held(sk));
1164 fasync_helper(fd, filp, on, &wq->fasync_list);
1166 if (!wq->fasync_list)
1167 sock_reset_flag(sk, SOCK_FASYNC);
1168 else
1169 sock_set_flag(sk, SOCK_FASYNC);
1171 release_sock(sk);
1172 return 0;
1175 /* This function may be called only under rcu_lock */
1177 int sock_wake_async(struct socket_wq *wq, int how, int band)
1179 if (!wq || !wq->fasync_list)
1180 return -1;
1182 switch (how) {
1183 case SOCK_WAKE_WAITD:
1184 if (test_bit(SOCKWQ_ASYNC_WAITDATA, &wq->flags))
1185 break;
1186 goto call_kill;
1187 case SOCK_WAKE_SPACE:
1188 if (!test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags))
1189 break;
1190 /* fall through */
1191 case SOCK_WAKE_IO:
1192 call_kill:
1193 kill_fasync(&wq->fasync_list, SIGIO, band);
1194 break;
1195 case SOCK_WAKE_URG:
1196 kill_fasync(&wq->fasync_list, SIGURG, band);
1199 return 0;
1201 EXPORT_SYMBOL(sock_wake_async);
1203 int __sock_create(struct net *net, int family, int type, int protocol,
1204 struct socket **res, int kern)
1206 int err;
1207 struct socket *sock;
1208 const struct net_proto_family *pf;
1211 * Check protocol is in range
1213 if (family < 0 || family >= NPROTO)
1214 return -EAFNOSUPPORT;
1215 if (type < 0 || type >= SOCK_MAX)
1216 return -EINVAL;
1218 /* Compatibility.
1220 This uglymoron is moved from INET layer to here to avoid
1221 deadlock in module load.
1223 if (family == PF_INET && type == SOCK_PACKET) {
1224 pr_info_once("%s uses obsolete (PF_INET,SOCK_PACKET)\n",
1225 current->comm);
1226 family = PF_PACKET;
1229 err = security_socket_create(family, type, protocol, kern);
1230 if (err)
1231 return err;
1234 * Allocate the socket and allow the family to set things up. if
1235 * the protocol is 0, the family is instructed to select an appropriate
1236 * default.
1238 sock = sock_alloc();
1239 if (!sock) {
1240 net_warn_ratelimited("socket: no more sockets\n");
1241 return -ENFILE; /* Not exactly a match, but its the
1242 closest posix thing */
1245 sock->type = type;
1247 #ifdef CONFIG_MODULES
1248 /* Attempt to load a protocol module if the find failed.
1250 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
1251 * requested real, full-featured networking support upon configuration.
1252 * Otherwise module support will break!
1254 if (rcu_access_pointer(net_families[family]) == NULL)
1255 request_module("net-pf-%d", family);
1256 #endif
1258 rcu_read_lock();
1259 pf = rcu_dereference(net_families[family]);
1260 err = -EAFNOSUPPORT;
1261 if (!pf)
1262 goto out_release;
1265 * We will call the ->create function, that possibly is in a loadable
1266 * module, so we have to bump that loadable module refcnt first.
1268 if (!try_module_get(pf->owner))
1269 goto out_release;
1271 /* Now protected by module ref count */
1272 rcu_read_unlock();
1274 err = pf->create(net, sock, protocol, kern);
1275 if (err < 0)
1276 goto out_module_put;
1279 * Now to bump the refcnt of the [loadable] module that owns this
1280 * socket at sock_release time we decrement its refcnt.
1282 if (!try_module_get(sock->ops->owner))
1283 goto out_module_busy;
1286 * Now that we're done with the ->create function, the [loadable]
1287 * module can have its refcnt decremented
1289 module_put(pf->owner);
1290 err = security_socket_post_create(sock, family, type, protocol, kern);
1291 if (err)
1292 goto out_sock_release;
1293 *res = sock;
1295 return 0;
1297 out_module_busy:
1298 err = -EAFNOSUPPORT;
1299 out_module_put:
1300 sock->ops = NULL;
1301 module_put(pf->owner);
1302 out_sock_release:
1303 sock_release(sock);
1304 return err;
1306 out_release:
1307 rcu_read_unlock();
1308 goto out_sock_release;
1310 EXPORT_SYMBOL(__sock_create);
1312 int sock_create(int family, int type, int protocol, struct socket **res)
1314 return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0);
1316 EXPORT_SYMBOL(sock_create);
1318 int sock_create_kern(struct net *net, int family, int type, int protocol, struct socket **res)
1320 return __sock_create(net, family, type, protocol, res, 1);
1322 EXPORT_SYMBOL(sock_create_kern);
1324 SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol)
1326 int retval;
1327 struct socket *sock;
1328 int flags;
1330 /* Check the SOCK_* constants for consistency. */
1331 BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC);
1332 BUILD_BUG_ON((SOCK_MAX | SOCK_TYPE_MASK) != SOCK_TYPE_MASK);
1333 BUILD_BUG_ON(SOCK_CLOEXEC & SOCK_TYPE_MASK);
1334 BUILD_BUG_ON(SOCK_NONBLOCK & SOCK_TYPE_MASK);
1336 flags = type & ~SOCK_TYPE_MASK;
1337 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1338 return -EINVAL;
1339 type &= SOCK_TYPE_MASK;
1341 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1342 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1344 retval = sock_create(family, type, protocol, &sock);
1345 if (retval < 0)
1346 goto out;
1348 retval = sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK));
1349 if (retval < 0)
1350 goto out_release;
1352 out:
1353 /* It may be already another descriptor 8) Not kernel problem. */
1354 return retval;
1356 out_release:
1357 sock_release(sock);
1358 return retval;
1362 * Create a pair of connected sockets.
1365 SYSCALL_DEFINE4(socketpair, int, family, int, type, int, protocol,
1366 int __user *, usockvec)
1368 struct socket *sock1, *sock2;
1369 int fd1, fd2, err;
1370 struct file *newfile1, *newfile2;
1371 int flags;
1373 flags = type & ~SOCK_TYPE_MASK;
1374 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1375 return -EINVAL;
1376 type &= SOCK_TYPE_MASK;
1378 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1379 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1382 * Obtain the first socket and check if the underlying protocol
1383 * supports the socketpair call.
1386 err = sock_create(family, type, protocol, &sock1);
1387 if (err < 0)
1388 goto out;
1390 err = sock_create(family, type, protocol, &sock2);
1391 if (err < 0)
1392 goto out_release_1;
1394 err = sock1->ops->socketpair(sock1, sock2);
1395 if (err < 0)
1396 goto out_release_both;
1398 fd1 = get_unused_fd_flags(flags);
1399 if (unlikely(fd1 < 0)) {
1400 err = fd1;
1401 goto out_release_both;
1404 fd2 = get_unused_fd_flags(flags);
1405 if (unlikely(fd2 < 0)) {
1406 err = fd2;
1407 goto out_put_unused_1;
1410 newfile1 = sock_alloc_file(sock1, flags, NULL);
1411 if (IS_ERR(newfile1)) {
1412 err = PTR_ERR(newfile1);
1413 goto out_put_unused_both;
1416 newfile2 = sock_alloc_file(sock2, flags, NULL);
1417 if (IS_ERR(newfile2)) {
1418 err = PTR_ERR(newfile2);
1419 goto out_fput_1;
1422 err = put_user(fd1, &usockvec[0]);
1423 if (err)
1424 goto out_fput_both;
1426 err = put_user(fd2, &usockvec[1]);
1427 if (err)
1428 goto out_fput_both;
1430 audit_fd_pair(fd1, fd2);
1432 fd_install(fd1, newfile1);
1433 fd_install(fd2, newfile2);
1434 /* fd1 and fd2 may be already another descriptors.
1435 * Not kernel problem.
1438 return 0;
1440 out_fput_both:
1441 fput(newfile2);
1442 fput(newfile1);
1443 put_unused_fd(fd2);
1444 put_unused_fd(fd1);
1445 goto out;
1447 out_fput_1:
1448 fput(newfile1);
1449 put_unused_fd(fd2);
1450 put_unused_fd(fd1);
1451 sock_release(sock2);
1452 goto out;
1454 out_put_unused_both:
1455 put_unused_fd(fd2);
1456 out_put_unused_1:
1457 put_unused_fd(fd1);
1458 out_release_both:
1459 sock_release(sock2);
1460 out_release_1:
1461 sock_release(sock1);
1462 out:
1463 return err;
1467 * Bind a name to a socket. Nothing much to do here since it's
1468 * the protocol's responsibility to handle the local address.
1470 * We move the socket address to kernel space before we call
1471 * the protocol layer (having also checked the address is ok).
1474 SYSCALL_DEFINE3(bind, int, fd, struct sockaddr __user *, umyaddr, int, addrlen)
1476 struct socket *sock;
1477 struct sockaddr_storage address;
1478 int err, fput_needed;
1480 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1481 if (sock) {
1482 err = move_addr_to_kernel(umyaddr, addrlen, &address);
1483 if (err >= 0) {
1484 err = security_socket_bind(sock,
1485 (struct sockaddr *)&address,
1486 addrlen);
1487 if (!err)
1488 err = sock->ops->bind(sock,
1489 (struct sockaddr *)
1490 &address, addrlen);
1492 fput_light(sock->file, fput_needed);
1494 return err;
1498 * Perform a listen. Basically, we allow the protocol to do anything
1499 * necessary for a listen, and if that works, we mark the socket as
1500 * ready for listening.
1503 SYSCALL_DEFINE2(listen, int, fd, int, backlog)
1505 struct socket *sock;
1506 int err, fput_needed;
1507 int somaxconn;
1509 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1510 if (sock) {
1511 somaxconn = sock_net(sock->sk)->core.sysctl_somaxconn;
1512 if ((unsigned int)backlog > somaxconn)
1513 backlog = somaxconn;
1515 err = security_socket_listen(sock, backlog);
1516 if (!err)
1517 err = sock->ops->listen(sock, backlog);
1519 fput_light(sock->file, fput_needed);
1521 return err;
1525 * For accept, we attempt to create a new socket, set up the link
1526 * with the client, wake up the client, then return the new
1527 * connected fd. We collect the address of the connector in kernel
1528 * space and move it to user at the very end. This is unclean because
1529 * we open the socket then return an error.
1531 * 1003.1g adds the ability to recvmsg() to query connection pending
1532 * status to recvmsg. We need to add that support in a way thats
1533 * clean when we restucture accept also.
1536 SYSCALL_DEFINE4(accept4, int, fd, struct sockaddr __user *, upeer_sockaddr,
1537 int __user *, upeer_addrlen, int, flags)
1539 struct socket *sock, *newsock;
1540 struct file *newfile;
1541 int err, len, newfd, fput_needed;
1542 struct sockaddr_storage address;
1544 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1545 return -EINVAL;
1547 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1548 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1550 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1551 if (!sock)
1552 goto out;
1554 err = -ENFILE;
1555 newsock = sock_alloc();
1556 if (!newsock)
1557 goto out_put;
1559 newsock->type = sock->type;
1560 newsock->ops = sock->ops;
1563 * We don't need try_module_get here, as the listening socket (sock)
1564 * has the protocol module (sock->ops->owner) held.
1566 __module_get(newsock->ops->owner);
1568 newfd = get_unused_fd_flags(flags);
1569 if (unlikely(newfd < 0)) {
1570 err = newfd;
1571 sock_release(newsock);
1572 goto out_put;
1574 newfile = sock_alloc_file(newsock, flags, sock->sk->sk_prot_creator->name);
1575 if (IS_ERR(newfile)) {
1576 err = PTR_ERR(newfile);
1577 put_unused_fd(newfd);
1578 sock_release(newsock);
1579 goto out_put;
1582 err = security_socket_accept(sock, newsock);
1583 if (err)
1584 goto out_fd;
1586 err = sock->ops->accept(sock, newsock, sock->file->f_flags, false);
1587 if (err < 0)
1588 goto out_fd;
1590 if (upeer_sockaddr) {
1591 if (newsock->ops->getname(newsock, (struct sockaddr *)&address,
1592 &len, 2) < 0) {
1593 err = -ECONNABORTED;
1594 goto out_fd;
1596 err = move_addr_to_user(&address,
1597 len, upeer_sockaddr, upeer_addrlen);
1598 if (err < 0)
1599 goto out_fd;
1602 /* File flags are not inherited via accept() unlike another OSes. */
1604 fd_install(newfd, newfile);
1605 err = newfd;
1607 out_put:
1608 fput_light(sock->file, fput_needed);
1609 out:
1610 return err;
1611 out_fd:
1612 fput(newfile);
1613 put_unused_fd(newfd);
1614 goto out_put;
1617 SYSCALL_DEFINE3(accept, int, fd, struct sockaddr __user *, upeer_sockaddr,
1618 int __user *, upeer_addrlen)
1620 return sys_accept4(fd, upeer_sockaddr, upeer_addrlen, 0);
1624 * Attempt to connect to a socket with the server address. The address
1625 * is in user space so we verify it is OK and move it to kernel space.
1627 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
1628 * break bindings
1630 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
1631 * other SEQPACKET protocols that take time to connect() as it doesn't
1632 * include the -EINPROGRESS status for such sockets.
1635 SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
1636 int, addrlen)
1638 struct socket *sock;
1639 struct sockaddr_storage address;
1640 int err, fput_needed;
1642 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1643 if (!sock)
1644 goto out;
1645 err = move_addr_to_kernel(uservaddr, addrlen, &address);
1646 if (err < 0)
1647 goto out_put;
1649 err =
1650 security_socket_connect(sock, (struct sockaddr *)&address, addrlen);
1651 if (err)
1652 goto out_put;
1654 err = sock->ops->connect(sock, (struct sockaddr *)&address, addrlen,
1655 sock->file->f_flags);
1656 out_put:
1657 fput_light(sock->file, fput_needed);
1658 out:
1659 return err;
1663 * Get the local address ('name') of a socket object. Move the obtained
1664 * name to user space.
1667 SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr,
1668 int __user *, usockaddr_len)
1670 struct socket *sock;
1671 struct sockaddr_storage address;
1672 int len, err, fput_needed;
1674 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1675 if (!sock)
1676 goto out;
1678 err = security_socket_getsockname(sock);
1679 if (err)
1680 goto out_put;
1682 err = sock->ops->getname(sock, (struct sockaddr *)&address, &len, 0);
1683 if (err)
1684 goto out_put;
1685 err = move_addr_to_user(&address, len, usockaddr, usockaddr_len);
1687 out_put:
1688 fput_light(sock->file, fput_needed);
1689 out:
1690 return err;
1694 * Get the remote address ('name') of a socket object. Move the obtained
1695 * name to user space.
1698 SYSCALL_DEFINE3(getpeername, int, fd, struct sockaddr __user *, usockaddr,
1699 int __user *, usockaddr_len)
1701 struct socket *sock;
1702 struct sockaddr_storage address;
1703 int len, err, fput_needed;
1705 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1706 if (sock != NULL) {
1707 err = security_socket_getpeername(sock);
1708 if (err) {
1709 fput_light(sock->file, fput_needed);
1710 return err;
1713 err =
1714 sock->ops->getname(sock, (struct sockaddr *)&address, &len,
1716 if (!err)
1717 err = move_addr_to_user(&address, len, usockaddr,
1718 usockaddr_len);
1719 fput_light(sock->file, fput_needed);
1721 return err;
1725 * Send a datagram to a given address. We move the address into kernel
1726 * space and check the user space data area is readable before invoking
1727 * the protocol.
1730 SYSCALL_DEFINE6(sendto, int, fd, void __user *, buff, size_t, len,
1731 unsigned int, flags, struct sockaddr __user *, addr,
1732 int, addr_len)
1734 struct socket *sock;
1735 struct sockaddr_storage address;
1736 int err;
1737 struct msghdr msg;
1738 struct iovec iov;
1739 int fput_needed;
1741 err = import_single_range(WRITE, buff, len, &iov, &msg.msg_iter);
1742 if (unlikely(err))
1743 return err;
1744 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1745 if (!sock)
1746 goto out;
1748 msg.msg_name = NULL;
1749 msg.msg_control = NULL;
1750 msg.msg_controllen = 0;
1751 msg.msg_namelen = 0;
1752 if (addr) {
1753 err = move_addr_to_kernel(addr, addr_len, &address);
1754 if (err < 0)
1755 goto out_put;
1756 msg.msg_name = (struct sockaddr *)&address;
1757 msg.msg_namelen = addr_len;
1759 if (sock->file->f_flags & O_NONBLOCK)
1760 flags |= MSG_DONTWAIT;
1761 msg.msg_flags = flags;
1762 err = sock_sendmsg(sock, &msg);
1764 out_put:
1765 fput_light(sock->file, fput_needed);
1766 out:
1767 return err;
1771 * Send a datagram down a socket.
1774 SYSCALL_DEFINE4(send, int, fd, void __user *, buff, size_t, len,
1775 unsigned int, flags)
1777 return sys_sendto(fd, buff, len, flags, NULL, 0);
1781 * Receive a frame from the socket and optionally record the address of the
1782 * sender. We verify the buffers are writable and if needed move the
1783 * sender address from kernel to user space.
1786 SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, ubuf, size_t, size,
1787 unsigned int, flags, struct sockaddr __user *, addr,
1788 int __user *, addr_len)
1790 struct socket *sock;
1791 struct iovec iov;
1792 struct msghdr msg;
1793 struct sockaddr_storage address;
1794 int err, err2;
1795 int fput_needed;
1797 err = import_single_range(READ, ubuf, size, &iov, &msg.msg_iter);
1798 if (unlikely(err))
1799 return err;
1800 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1801 if (!sock)
1802 goto out;
1804 msg.msg_control = NULL;
1805 msg.msg_controllen = 0;
1806 /* Save some cycles and don't copy the address if not needed */
1807 msg.msg_name = addr ? (struct sockaddr *)&address : NULL;
1808 /* We assume all kernel code knows the size of sockaddr_storage */
1809 msg.msg_namelen = 0;
1810 msg.msg_iocb = NULL;
1811 msg.msg_flags = 0;
1812 if (sock->file->f_flags & O_NONBLOCK)
1813 flags |= MSG_DONTWAIT;
1814 err = sock_recvmsg(sock, &msg, flags);
1816 if (err >= 0 && addr != NULL) {
1817 err2 = move_addr_to_user(&address,
1818 msg.msg_namelen, addr, addr_len);
1819 if (err2 < 0)
1820 err = err2;
1823 fput_light(sock->file, fput_needed);
1824 out:
1825 return err;
1829 * Receive a datagram from a socket.
1832 SYSCALL_DEFINE4(recv, int, fd, void __user *, ubuf, size_t, size,
1833 unsigned int, flags)
1835 return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
1839 * Set a socket option. Because we don't know the option lengths we have
1840 * to pass the user mode parameter for the protocols to sort out.
1843 SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,
1844 char __user *, optval, int, optlen)
1846 int err, fput_needed;
1847 struct socket *sock;
1849 if (optlen < 0)
1850 return -EINVAL;
1852 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1853 if (sock != NULL) {
1854 err = security_socket_setsockopt(sock, level, optname);
1855 if (err)
1856 goto out_put;
1858 if (level == SOL_SOCKET)
1859 err =
1860 sock_setsockopt(sock, level, optname, optval,
1861 optlen);
1862 else
1863 err =
1864 sock->ops->setsockopt(sock, level, optname, optval,
1865 optlen);
1866 out_put:
1867 fput_light(sock->file, fput_needed);
1869 return err;
1873 * Get a socket option. Because we don't know the option lengths we have
1874 * to pass a user mode parameter for the protocols to sort out.
1877 SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname,
1878 char __user *, optval, int __user *, optlen)
1880 int err, fput_needed;
1881 struct socket *sock;
1883 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1884 if (sock != NULL) {
1885 err = security_socket_getsockopt(sock, level, optname);
1886 if (err)
1887 goto out_put;
1889 if (level == SOL_SOCKET)
1890 err =
1891 sock_getsockopt(sock, level, optname, optval,
1892 optlen);
1893 else
1894 err =
1895 sock->ops->getsockopt(sock, level, optname, optval,
1896 optlen);
1897 out_put:
1898 fput_light(sock->file, fput_needed);
1900 return err;
1904 * Shutdown a socket.
1907 SYSCALL_DEFINE2(shutdown, int, fd, int, how)
1909 int err, fput_needed;
1910 struct socket *sock;
1912 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1913 if (sock != NULL) {
1914 err = security_socket_shutdown(sock, how);
1915 if (!err)
1916 err = sock->ops->shutdown(sock, how);
1917 fput_light(sock->file, fput_needed);
1919 return err;
1922 /* A couple of helpful macros for getting the address of the 32/64 bit
1923 * fields which are the same type (int / unsigned) on our platforms.
1925 #define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
1926 #define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
1927 #define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
1929 struct used_address {
1930 struct sockaddr_storage name;
1931 unsigned int name_len;
1934 static int copy_msghdr_from_user(struct msghdr *kmsg,
1935 struct user_msghdr __user *umsg,
1936 struct sockaddr __user **save_addr,
1937 struct iovec **iov)
1939 struct user_msghdr msg;
1940 ssize_t err;
1942 if (copy_from_user(&msg, umsg, sizeof(*umsg)))
1943 return -EFAULT;
1945 kmsg->msg_control = (void __force *)msg.msg_control;
1946 kmsg->msg_controllen = msg.msg_controllen;
1947 kmsg->msg_flags = msg.msg_flags;
1949 kmsg->msg_namelen = msg.msg_namelen;
1950 if (!msg.msg_name)
1951 kmsg->msg_namelen = 0;
1953 if (kmsg->msg_namelen < 0)
1954 return -EINVAL;
1956 if (kmsg->msg_namelen > sizeof(struct sockaddr_storage))
1957 kmsg->msg_namelen = sizeof(struct sockaddr_storage);
1959 if (save_addr)
1960 *save_addr = msg.msg_name;
1962 if (msg.msg_name && kmsg->msg_namelen) {
1963 if (!save_addr) {
1964 err = move_addr_to_kernel(msg.msg_name,
1965 kmsg->msg_namelen,
1966 kmsg->msg_name);
1967 if (err < 0)
1968 return err;
1970 } else {
1971 kmsg->msg_name = NULL;
1972 kmsg->msg_namelen = 0;
1975 if (msg.msg_iovlen > UIO_MAXIOV)
1976 return -EMSGSIZE;
1978 kmsg->msg_iocb = NULL;
1980 return import_iovec(save_addr ? READ : WRITE,
1981 msg.msg_iov, msg.msg_iovlen,
1982 UIO_FASTIOV, iov, &kmsg->msg_iter);
1985 static int ___sys_sendmsg(struct socket *sock, struct user_msghdr __user *msg,
1986 struct msghdr *msg_sys, unsigned int flags,
1987 struct used_address *used_address,
1988 unsigned int allowed_msghdr_flags)
1990 struct compat_msghdr __user *msg_compat =
1991 (struct compat_msghdr __user *)msg;
1992 struct sockaddr_storage address;
1993 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
1994 unsigned char ctl[sizeof(struct cmsghdr) + 20]
1995 __aligned(sizeof(__kernel_size_t));
1996 /* 20 is size of ipv6_pktinfo */
1997 unsigned char *ctl_buf = ctl;
1998 int ctl_len;
1999 ssize_t err;
2001 msg_sys->msg_name = &address;
2003 if (MSG_CMSG_COMPAT & flags)
2004 err = get_compat_msghdr(msg_sys, msg_compat, NULL, &iov);
2005 else
2006 err = copy_msghdr_from_user(msg_sys, msg, NULL, &iov);
2007 if (err < 0)
2008 return err;
2010 err = -ENOBUFS;
2012 if (msg_sys->msg_controllen > INT_MAX)
2013 goto out_freeiov;
2014 flags |= (msg_sys->msg_flags & allowed_msghdr_flags);
2015 ctl_len = msg_sys->msg_controllen;
2016 if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
2017 err =
2018 cmsghdr_from_user_compat_to_kern(msg_sys, sock->sk, ctl,
2019 sizeof(ctl));
2020 if (err)
2021 goto out_freeiov;
2022 ctl_buf = msg_sys->msg_control;
2023 ctl_len = msg_sys->msg_controllen;
2024 } else if (ctl_len) {
2025 BUILD_BUG_ON(sizeof(struct cmsghdr) !=
2026 CMSG_ALIGN(sizeof(struct cmsghdr)));
2027 if (ctl_len > sizeof(ctl)) {
2028 ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
2029 if (ctl_buf == NULL)
2030 goto out_freeiov;
2032 err = -EFAULT;
2034 * Careful! Before this, msg_sys->msg_control contains a user pointer.
2035 * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
2036 * checking falls down on this.
2038 if (copy_from_user(ctl_buf,
2039 (void __user __force *)msg_sys->msg_control,
2040 ctl_len))
2041 goto out_freectl;
2042 msg_sys->msg_control = ctl_buf;
2044 msg_sys->msg_flags = flags;
2046 if (sock->file->f_flags & O_NONBLOCK)
2047 msg_sys->msg_flags |= MSG_DONTWAIT;
2049 * If this is sendmmsg() and current destination address is same as
2050 * previously succeeded address, omit asking LSM's decision.
2051 * used_address->name_len is initialized to UINT_MAX so that the first
2052 * destination address never matches.
2054 if (used_address && msg_sys->msg_name &&
2055 used_address->name_len == msg_sys->msg_namelen &&
2056 !memcmp(&used_address->name, msg_sys->msg_name,
2057 used_address->name_len)) {
2058 err = sock_sendmsg_nosec(sock, msg_sys);
2059 goto out_freectl;
2061 err = sock_sendmsg(sock, msg_sys);
2063 * If this is sendmmsg() and sending to current destination address was
2064 * successful, remember it.
2066 if (used_address && err >= 0) {
2067 used_address->name_len = msg_sys->msg_namelen;
2068 if (msg_sys->msg_name)
2069 memcpy(&used_address->name, msg_sys->msg_name,
2070 used_address->name_len);
2073 out_freectl:
2074 if (ctl_buf != ctl)
2075 sock_kfree_s(sock->sk, ctl_buf, ctl_len);
2076 out_freeiov:
2077 kfree(iov);
2078 return err;
2082 * BSD sendmsg interface
2085 long __sys_sendmsg(int fd, struct user_msghdr __user *msg, unsigned flags)
2087 int fput_needed, err;
2088 struct msghdr msg_sys;
2089 struct socket *sock;
2091 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2092 if (!sock)
2093 goto out;
2095 err = ___sys_sendmsg(sock, msg, &msg_sys, flags, NULL, 0);
2097 fput_light(sock->file, fput_needed);
2098 out:
2099 return err;
2102 SYSCALL_DEFINE3(sendmsg, int, fd, struct user_msghdr __user *, msg, unsigned int, flags)
2104 if (flags & MSG_CMSG_COMPAT)
2105 return -EINVAL;
2106 return __sys_sendmsg(fd, msg, flags);
2110 * Linux sendmmsg interface
2113 int __sys_sendmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
2114 unsigned int flags)
2116 int fput_needed, err, datagrams;
2117 struct socket *sock;
2118 struct mmsghdr __user *entry;
2119 struct compat_mmsghdr __user *compat_entry;
2120 struct msghdr msg_sys;
2121 struct used_address used_address;
2122 unsigned int oflags = flags;
2124 if (vlen > UIO_MAXIOV)
2125 vlen = UIO_MAXIOV;
2127 datagrams = 0;
2129 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2130 if (!sock)
2131 return err;
2133 used_address.name_len = UINT_MAX;
2134 entry = mmsg;
2135 compat_entry = (struct compat_mmsghdr __user *)mmsg;
2136 err = 0;
2137 flags |= MSG_BATCH;
2139 while (datagrams < vlen) {
2140 if (datagrams == vlen - 1)
2141 flags = oflags;
2143 if (MSG_CMSG_COMPAT & flags) {
2144 err = ___sys_sendmsg(sock, (struct user_msghdr __user *)compat_entry,
2145 &msg_sys, flags, &used_address, MSG_EOR);
2146 if (err < 0)
2147 break;
2148 err = __put_user(err, &compat_entry->msg_len);
2149 ++compat_entry;
2150 } else {
2151 err = ___sys_sendmsg(sock,
2152 (struct user_msghdr __user *)entry,
2153 &msg_sys, flags, &used_address, MSG_EOR);
2154 if (err < 0)
2155 break;
2156 err = put_user(err, &entry->msg_len);
2157 ++entry;
2160 if (err)
2161 break;
2162 ++datagrams;
2163 if (msg_data_left(&msg_sys))
2164 break;
2165 cond_resched();
2168 fput_light(sock->file, fput_needed);
2170 /* We only return an error if no datagrams were able to be sent */
2171 if (datagrams != 0)
2172 return datagrams;
2174 return err;
2177 SYSCALL_DEFINE4(sendmmsg, int, fd, struct mmsghdr __user *, mmsg,
2178 unsigned int, vlen, unsigned int, flags)
2180 if (flags & MSG_CMSG_COMPAT)
2181 return -EINVAL;
2182 return __sys_sendmmsg(fd, mmsg, vlen, flags);
2185 static int ___sys_recvmsg(struct socket *sock, struct user_msghdr __user *msg,
2186 struct msghdr *msg_sys, unsigned int flags, int nosec)
2188 struct compat_msghdr __user *msg_compat =
2189 (struct compat_msghdr __user *)msg;
2190 struct iovec iovstack[UIO_FASTIOV];
2191 struct iovec *iov = iovstack;
2192 unsigned long cmsg_ptr;
2193 int len;
2194 ssize_t err;
2196 /* kernel mode address */
2197 struct sockaddr_storage addr;
2199 /* user mode address pointers */
2200 struct sockaddr __user *uaddr;
2201 int __user *uaddr_len = COMPAT_NAMELEN(msg);
2203 msg_sys->msg_name = &addr;
2205 if (MSG_CMSG_COMPAT & flags)
2206 err = get_compat_msghdr(msg_sys, msg_compat, &uaddr, &iov);
2207 else
2208 err = copy_msghdr_from_user(msg_sys, msg, &uaddr, &iov);
2209 if (err < 0)
2210 return err;
2212 cmsg_ptr = (unsigned long)msg_sys->msg_control;
2213 msg_sys->msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
2215 /* We assume all kernel code knows the size of sockaddr_storage */
2216 msg_sys->msg_namelen = 0;
2218 if (sock->file->f_flags & O_NONBLOCK)
2219 flags |= MSG_DONTWAIT;
2220 err = (nosec ? sock_recvmsg_nosec : sock_recvmsg)(sock, msg_sys, flags);
2221 if (err < 0)
2222 goto out_freeiov;
2223 len = err;
2225 if (uaddr != NULL) {
2226 err = move_addr_to_user(&addr,
2227 msg_sys->msg_namelen, uaddr,
2228 uaddr_len);
2229 if (err < 0)
2230 goto out_freeiov;
2232 err = __put_user((msg_sys->msg_flags & ~MSG_CMSG_COMPAT),
2233 COMPAT_FLAGS(msg));
2234 if (err)
2235 goto out_freeiov;
2236 if (MSG_CMSG_COMPAT & flags)
2237 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
2238 &msg_compat->msg_controllen);
2239 else
2240 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
2241 &msg->msg_controllen);
2242 if (err)
2243 goto out_freeiov;
2244 err = len;
2246 out_freeiov:
2247 kfree(iov);
2248 return err;
2252 * BSD recvmsg interface
2255 long __sys_recvmsg(int fd, struct user_msghdr __user *msg, unsigned flags)
2257 int fput_needed, err;
2258 struct msghdr msg_sys;
2259 struct socket *sock;
2261 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2262 if (!sock)
2263 goto out;
2265 err = ___sys_recvmsg(sock, msg, &msg_sys, flags, 0);
2267 fput_light(sock->file, fput_needed);
2268 out:
2269 return err;
2272 SYSCALL_DEFINE3(recvmsg, int, fd, struct user_msghdr __user *, msg,
2273 unsigned int, flags)
2275 if (flags & MSG_CMSG_COMPAT)
2276 return -EINVAL;
2277 return __sys_recvmsg(fd, msg, flags);
2281 * Linux recvmmsg interface
2284 int __sys_recvmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
2285 unsigned int flags, struct timespec *timeout)
2287 int fput_needed, err, datagrams;
2288 struct socket *sock;
2289 struct mmsghdr __user *entry;
2290 struct compat_mmsghdr __user *compat_entry;
2291 struct msghdr msg_sys;
2292 struct timespec64 end_time;
2293 struct timespec64 timeout64;
2295 if (timeout &&
2296 poll_select_set_timeout(&end_time, timeout->tv_sec,
2297 timeout->tv_nsec))
2298 return -EINVAL;
2300 datagrams = 0;
2302 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2303 if (!sock)
2304 return err;
2306 err = sock_error(sock->sk);
2307 if (err) {
2308 datagrams = err;
2309 goto out_put;
2312 entry = mmsg;
2313 compat_entry = (struct compat_mmsghdr __user *)mmsg;
2315 while (datagrams < vlen) {
2317 * No need to ask LSM for more than the first datagram.
2319 if (MSG_CMSG_COMPAT & flags) {
2320 err = ___sys_recvmsg(sock, (struct user_msghdr __user *)compat_entry,
2321 &msg_sys, flags & ~MSG_WAITFORONE,
2322 datagrams);
2323 if (err < 0)
2324 break;
2325 err = __put_user(err, &compat_entry->msg_len);
2326 ++compat_entry;
2327 } else {
2328 err = ___sys_recvmsg(sock,
2329 (struct user_msghdr __user *)entry,
2330 &msg_sys, flags & ~MSG_WAITFORONE,
2331 datagrams);
2332 if (err < 0)
2333 break;
2334 err = put_user(err, &entry->msg_len);
2335 ++entry;
2338 if (err)
2339 break;
2340 ++datagrams;
2342 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
2343 if (flags & MSG_WAITFORONE)
2344 flags |= MSG_DONTWAIT;
2346 if (timeout) {
2347 ktime_get_ts64(&timeout64);
2348 *timeout = timespec64_to_timespec(
2349 timespec64_sub(end_time, timeout64));
2350 if (timeout->tv_sec < 0) {
2351 timeout->tv_sec = timeout->tv_nsec = 0;
2352 break;
2355 /* Timeout, return less than vlen datagrams */
2356 if (timeout->tv_nsec == 0 && timeout->tv_sec == 0)
2357 break;
2360 /* Out of band data, return right away */
2361 if (msg_sys.msg_flags & MSG_OOB)
2362 break;
2363 cond_resched();
2366 if (err == 0)
2367 goto out_put;
2369 if (datagrams == 0) {
2370 datagrams = err;
2371 goto out_put;
2375 * We may return less entries than requested (vlen) if the
2376 * sock is non block and there aren't enough datagrams...
2378 if (err != -EAGAIN) {
2380 * ... or if recvmsg returns an error after we
2381 * received some datagrams, where we record the
2382 * error to return on the next call or if the
2383 * app asks about it using getsockopt(SO_ERROR).
2385 sock->sk->sk_err = -err;
2387 out_put:
2388 fput_light(sock->file, fput_needed);
2390 return datagrams;
2393 SYSCALL_DEFINE5(recvmmsg, int, fd, struct mmsghdr __user *, mmsg,
2394 unsigned int, vlen, unsigned int, flags,
2395 struct timespec __user *, timeout)
2397 int datagrams;
2398 struct timespec timeout_sys;
2400 if (flags & MSG_CMSG_COMPAT)
2401 return -EINVAL;
2403 if (!timeout)
2404 return __sys_recvmmsg(fd, mmsg, vlen, flags, NULL);
2406 if (copy_from_user(&timeout_sys, timeout, sizeof(timeout_sys)))
2407 return -EFAULT;
2409 datagrams = __sys_recvmmsg(fd, mmsg, vlen, flags, &timeout_sys);
2411 if (datagrams > 0 &&
2412 copy_to_user(timeout, &timeout_sys, sizeof(timeout_sys)))
2413 datagrams = -EFAULT;
2415 return datagrams;
2418 #ifdef __ARCH_WANT_SYS_SOCKETCALL
2419 /* Argument list sizes for sys_socketcall */
2420 #define AL(x) ((x) * sizeof(unsigned long))
2421 static const unsigned char nargs[21] = {
2422 AL(0), AL(3), AL(3), AL(3), AL(2), AL(3),
2423 AL(3), AL(3), AL(4), AL(4), AL(4), AL(6),
2424 AL(6), AL(2), AL(5), AL(5), AL(3), AL(3),
2425 AL(4), AL(5), AL(4)
2428 #undef AL
2431 * System call vectors.
2433 * Argument checking cleaned up. Saved 20% in size.
2434 * This function doesn't need to set the kernel lock because
2435 * it is set by the callees.
2438 SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args)
2440 unsigned long a[AUDITSC_ARGS];
2441 unsigned long a0, a1;
2442 int err;
2443 unsigned int len;
2445 if (call < 1 || call > SYS_SENDMMSG)
2446 return -EINVAL;
2447 call = array_index_nospec(call, SYS_SENDMMSG + 1);
2449 len = nargs[call];
2450 if (len > sizeof(a))
2451 return -EINVAL;
2453 /* copy_from_user should be SMP safe. */
2454 if (copy_from_user(a, args, len))
2455 return -EFAULT;
2457 err = audit_socketcall(nargs[call] / sizeof(unsigned long), a);
2458 if (err)
2459 return err;
2461 a0 = a[0];
2462 a1 = a[1];
2464 switch (call) {
2465 case SYS_SOCKET:
2466 err = sys_socket(a0, a1, a[2]);
2467 break;
2468 case SYS_BIND:
2469 err = sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
2470 break;
2471 case SYS_CONNECT:
2472 err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
2473 break;
2474 case SYS_LISTEN:
2475 err = sys_listen(a0, a1);
2476 break;
2477 case SYS_ACCEPT:
2478 err = sys_accept4(a0, (struct sockaddr __user *)a1,
2479 (int __user *)a[2], 0);
2480 break;
2481 case SYS_GETSOCKNAME:
2482 err =
2483 sys_getsockname(a0, (struct sockaddr __user *)a1,
2484 (int __user *)a[2]);
2485 break;
2486 case SYS_GETPEERNAME:
2487 err =
2488 sys_getpeername(a0, (struct sockaddr __user *)a1,
2489 (int __user *)a[2]);
2490 break;
2491 case SYS_SOCKETPAIR:
2492 err = sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
2493 break;
2494 case SYS_SEND:
2495 err = sys_send(a0, (void __user *)a1, a[2], a[3]);
2496 break;
2497 case SYS_SENDTO:
2498 err = sys_sendto(a0, (void __user *)a1, a[2], a[3],
2499 (struct sockaddr __user *)a[4], a[5]);
2500 break;
2501 case SYS_RECV:
2502 err = sys_recv(a0, (void __user *)a1, a[2], a[3]);
2503 break;
2504 case SYS_RECVFROM:
2505 err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
2506 (struct sockaddr __user *)a[4],
2507 (int __user *)a[5]);
2508 break;
2509 case SYS_SHUTDOWN:
2510 err = sys_shutdown(a0, a1);
2511 break;
2512 case SYS_SETSOCKOPT:
2513 err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]);
2514 break;
2515 case SYS_GETSOCKOPT:
2516 err =
2517 sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
2518 (int __user *)a[4]);
2519 break;
2520 case SYS_SENDMSG:
2521 err = sys_sendmsg(a0, (struct user_msghdr __user *)a1, a[2]);
2522 break;
2523 case SYS_SENDMMSG:
2524 err = sys_sendmmsg(a0, (struct mmsghdr __user *)a1, a[2], a[3]);
2525 break;
2526 case SYS_RECVMSG:
2527 err = sys_recvmsg(a0, (struct user_msghdr __user *)a1, a[2]);
2528 break;
2529 case SYS_RECVMMSG:
2530 err = sys_recvmmsg(a0, (struct mmsghdr __user *)a1, a[2], a[3],
2531 (struct timespec __user *)a[4]);
2532 break;
2533 case SYS_ACCEPT4:
2534 err = sys_accept4(a0, (struct sockaddr __user *)a1,
2535 (int __user *)a[2], a[3]);
2536 break;
2537 default:
2538 err = -EINVAL;
2539 break;
2541 return err;
2544 #endif /* __ARCH_WANT_SYS_SOCKETCALL */
2547 * sock_register - add a socket protocol handler
2548 * @ops: description of protocol
2550 * This function is called by a protocol handler that wants to
2551 * advertise its address family, and have it linked into the
2552 * socket interface. The value ops->family corresponds to the
2553 * socket system call protocol family.
2555 int sock_register(const struct net_proto_family *ops)
2557 int err;
2559 if (ops->family >= NPROTO) {
2560 pr_crit("protocol %d >= NPROTO(%d)\n", ops->family, NPROTO);
2561 return -ENOBUFS;
2564 spin_lock(&net_family_lock);
2565 if (rcu_dereference_protected(net_families[ops->family],
2566 lockdep_is_held(&net_family_lock)))
2567 err = -EEXIST;
2568 else {
2569 rcu_assign_pointer(net_families[ops->family], ops);
2570 err = 0;
2572 spin_unlock(&net_family_lock);
2574 pr_info("NET: Registered protocol family %d\n", ops->family);
2575 return err;
2577 EXPORT_SYMBOL(sock_register);
2580 * sock_unregister - remove a protocol handler
2581 * @family: protocol family to remove
2583 * This function is called by a protocol handler that wants to
2584 * remove its address family, and have it unlinked from the
2585 * new socket creation.
2587 * If protocol handler is a module, then it can use module reference
2588 * counts to protect against new references. If protocol handler is not
2589 * a module then it needs to provide its own protection in
2590 * the ops->create routine.
2592 void sock_unregister(int family)
2594 BUG_ON(family < 0 || family >= NPROTO);
2596 spin_lock(&net_family_lock);
2597 RCU_INIT_POINTER(net_families[family], NULL);
2598 spin_unlock(&net_family_lock);
2600 synchronize_rcu();
2602 pr_info("NET: Unregistered protocol family %d\n", family);
2604 EXPORT_SYMBOL(sock_unregister);
2606 static int __init sock_init(void)
2608 int err;
2610 * Initialize the network sysctl infrastructure.
2612 err = net_sysctl_init();
2613 if (err)
2614 goto out;
2617 * Initialize skbuff SLAB cache
2619 skb_init();
2622 * Initialize the protocols module.
2625 init_inodecache();
2627 err = register_filesystem(&sock_fs_type);
2628 if (err)
2629 goto out_fs;
2630 sock_mnt = kern_mount(&sock_fs_type);
2631 if (IS_ERR(sock_mnt)) {
2632 err = PTR_ERR(sock_mnt);
2633 goto out_mount;
2636 /* The real protocol initialization is performed in later initcalls.
2639 #ifdef CONFIG_NETFILTER
2640 err = netfilter_init();
2641 if (err)
2642 goto out;
2643 #endif
2645 ptp_classifier_init();
2647 out:
2648 return err;
2650 out_mount:
2651 unregister_filesystem(&sock_fs_type);
2652 out_fs:
2653 goto out;
2656 core_initcall(sock_init); /* early initcall */
2658 static int __init jit_init(void)
2660 #ifdef CONFIG_BPF_JIT_ALWAYS_ON
2661 bpf_jit_enable = 1;
2662 #endif
2663 return 0;
2665 pure_initcall(jit_init);
2667 #ifdef CONFIG_PROC_FS
2668 void socket_seq_show(struct seq_file *seq)
2670 int cpu;
2671 int counter = 0;
2673 for_each_possible_cpu(cpu)
2674 counter += per_cpu(sockets_in_use, cpu);
2676 /* It can be negative, by the way. 8) */
2677 if (counter < 0)
2678 counter = 0;
2680 seq_printf(seq, "sockets: used %d\n", counter);
2682 #endif /* CONFIG_PROC_FS */
2684 #ifdef CONFIG_COMPAT
2685 static int do_siocgstamp(struct net *net, struct socket *sock,
2686 unsigned int cmd, void __user *up)
2688 mm_segment_t old_fs = get_fs();
2689 struct timeval ktv;
2690 int err;
2692 set_fs(KERNEL_DS);
2693 err = sock_do_ioctl(net, sock, cmd, (unsigned long)&ktv);
2694 set_fs(old_fs);
2695 if (!err)
2696 err = compat_put_timeval(&ktv, up);
2698 return err;
2701 static int do_siocgstampns(struct net *net, struct socket *sock,
2702 unsigned int cmd, void __user *up)
2704 mm_segment_t old_fs = get_fs();
2705 struct timespec kts;
2706 int err;
2708 set_fs(KERNEL_DS);
2709 err = sock_do_ioctl(net, sock, cmd, (unsigned long)&kts);
2710 set_fs(old_fs);
2711 if (!err)
2712 err = compat_put_timespec(&kts, up);
2714 return err;
2717 static int dev_ifname32(struct net *net, struct compat_ifreq __user *uifr32)
2719 struct ifreq __user *uifr;
2720 int err;
2722 uifr = compat_alloc_user_space(sizeof(struct ifreq));
2723 if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
2724 return -EFAULT;
2726 err = dev_ioctl(net, SIOCGIFNAME, uifr);
2727 if (err)
2728 return err;
2730 if (copy_in_user(uifr32, uifr, sizeof(struct compat_ifreq)))
2731 return -EFAULT;
2733 return 0;
2736 static int dev_ifconf(struct net *net, struct compat_ifconf __user *uifc32)
2738 struct compat_ifconf ifc32;
2739 struct ifconf ifc;
2740 struct ifconf __user *uifc;
2741 struct compat_ifreq __user *ifr32;
2742 struct ifreq __user *ifr;
2743 unsigned int i, j;
2744 int err;
2746 if (copy_from_user(&ifc32, uifc32, sizeof(struct compat_ifconf)))
2747 return -EFAULT;
2749 memset(&ifc, 0, sizeof(ifc));
2750 if (ifc32.ifcbuf == 0) {
2751 ifc32.ifc_len = 0;
2752 ifc.ifc_len = 0;
2753 ifc.ifc_req = NULL;
2754 uifc = compat_alloc_user_space(sizeof(struct ifconf));
2755 } else {
2756 size_t len = ((ifc32.ifc_len / sizeof(struct compat_ifreq)) + 1) *
2757 sizeof(struct ifreq);
2758 uifc = compat_alloc_user_space(sizeof(struct ifconf) + len);
2759 ifc.ifc_len = len;
2760 ifr = ifc.ifc_req = (void __user *)(uifc + 1);
2761 ifr32 = compat_ptr(ifc32.ifcbuf);
2762 for (i = 0; i < ifc32.ifc_len; i += sizeof(struct compat_ifreq)) {
2763 if (copy_in_user(ifr, ifr32, sizeof(struct compat_ifreq)))
2764 return -EFAULT;
2765 ifr++;
2766 ifr32++;
2769 if (copy_to_user(uifc, &ifc, sizeof(struct ifconf)))
2770 return -EFAULT;
2772 err = dev_ioctl(net, SIOCGIFCONF, uifc);
2773 if (err)
2774 return err;
2776 if (copy_from_user(&ifc, uifc, sizeof(struct ifconf)))
2777 return -EFAULT;
2779 ifr = ifc.ifc_req;
2780 ifr32 = compat_ptr(ifc32.ifcbuf);
2781 for (i = 0, j = 0;
2782 i + sizeof(struct compat_ifreq) <= ifc32.ifc_len && j < ifc.ifc_len;
2783 i += sizeof(struct compat_ifreq), j += sizeof(struct ifreq)) {
2784 if (copy_in_user(ifr32, ifr, sizeof(struct compat_ifreq)))
2785 return -EFAULT;
2786 ifr32++;
2787 ifr++;
2790 if (ifc32.ifcbuf == 0) {
2791 /* Translate from 64-bit structure multiple to
2792 * a 32-bit one.
2794 i = ifc.ifc_len;
2795 i = ((i / sizeof(struct ifreq)) * sizeof(struct compat_ifreq));
2796 ifc32.ifc_len = i;
2797 } else {
2798 ifc32.ifc_len = i;
2800 if (copy_to_user(uifc32, &ifc32, sizeof(struct compat_ifconf)))
2801 return -EFAULT;
2803 return 0;
2806 static int ethtool_ioctl(struct net *net, struct compat_ifreq __user *ifr32)
2808 struct compat_ethtool_rxnfc __user *compat_rxnfc;
2809 bool convert_in = false, convert_out = false;
2810 size_t buf_size = ALIGN(sizeof(struct ifreq), 8);
2811 struct ethtool_rxnfc __user *rxnfc;
2812 struct ifreq __user *ifr;
2813 u32 rule_cnt = 0, actual_rule_cnt;
2814 u32 ethcmd;
2815 u32 data;
2816 int ret;
2818 if (get_user(data, &ifr32->ifr_ifru.ifru_data))
2819 return -EFAULT;
2821 compat_rxnfc = compat_ptr(data);
2823 if (get_user(ethcmd, &compat_rxnfc->cmd))
2824 return -EFAULT;
2826 /* Most ethtool structures are defined without padding.
2827 * Unfortunately struct ethtool_rxnfc is an exception.
2829 switch (ethcmd) {
2830 default:
2831 break;
2832 case ETHTOOL_GRXCLSRLALL:
2833 /* Buffer size is variable */
2834 if (get_user(rule_cnt, &compat_rxnfc->rule_cnt))
2835 return -EFAULT;
2836 if (rule_cnt > KMALLOC_MAX_SIZE / sizeof(u32))
2837 return -ENOMEM;
2838 buf_size += rule_cnt * sizeof(u32);
2839 /* fall through */
2840 case ETHTOOL_GRXRINGS:
2841 case ETHTOOL_GRXCLSRLCNT:
2842 case ETHTOOL_GRXCLSRULE:
2843 case ETHTOOL_SRXCLSRLINS:
2844 convert_out = true;
2845 /* fall through */
2846 case ETHTOOL_SRXCLSRLDEL:
2847 buf_size += sizeof(struct ethtool_rxnfc);
2848 convert_in = true;
2849 break;
2852 ifr = compat_alloc_user_space(buf_size);
2853 rxnfc = (void __user *)ifr + ALIGN(sizeof(struct ifreq), 8);
2855 if (copy_in_user(&ifr->ifr_name, &ifr32->ifr_name, IFNAMSIZ))
2856 return -EFAULT;
2858 if (put_user(convert_in ? rxnfc : compat_ptr(data),
2859 &ifr->ifr_ifru.ifru_data))
2860 return -EFAULT;
2862 if (convert_in) {
2863 /* We expect there to be holes between fs.m_ext and
2864 * fs.ring_cookie and at the end of fs, but nowhere else.
2866 BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc, fs.m_ext) +
2867 sizeof(compat_rxnfc->fs.m_ext) !=
2868 offsetof(struct ethtool_rxnfc, fs.m_ext) +
2869 sizeof(rxnfc->fs.m_ext));
2870 BUILD_BUG_ON(
2871 offsetof(struct compat_ethtool_rxnfc, fs.location) -
2872 offsetof(struct compat_ethtool_rxnfc, fs.ring_cookie) !=
2873 offsetof(struct ethtool_rxnfc, fs.location) -
2874 offsetof(struct ethtool_rxnfc, fs.ring_cookie));
2876 if (copy_in_user(rxnfc, compat_rxnfc,
2877 (void __user *)(&rxnfc->fs.m_ext + 1) -
2878 (void __user *)rxnfc) ||
2879 copy_in_user(&rxnfc->fs.ring_cookie,
2880 &compat_rxnfc->fs.ring_cookie,
2881 (void __user *)(&rxnfc->fs.location + 1) -
2882 (void __user *)&rxnfc->fs.ring_cookie) ||
2883 copy_in_user(&rxnfc->rule_cnt, &compat_rxnfc->rule_cnt,
2884 sizeof(rxnfc->rule_cnt)))
2885 return -EFAULT;
2888 ret = dev_ioctl(net, SIOCETHTOOL, ifr);
2889 if (ret)
2890 return ret;
2892 if (convert_out) {
2893 if (copy_in_user(compat_rxnfc, rxnfc,
2894 (const void __user *)(&rxnfc->fs.m_ext + 1) -
2895 (const void __user *)rxnfc) ||
2896 copy_in_user(&compat_rxnfc->fs.ring_cookie,
2897 &rxnfc->fs.ring_cookie,
2898 (const void __user *)(&rxnfc->fs.location + 1) -
2899 (const void __user *)&rxnfc->fs.ring_cookie) ||
2900 copy_in_user(&compat_rxnfc->rule_cnt, &rxnfc->rule_cnt,
2901 sizeof(rxnfc->rule_cnt)))
2902 return -EFAULT;
2904 if (ethcmd == ETHTOOL_GRXCLSRLALL) {
2905 /* As an optimisation, we only copy the actual
2906 * number of rules that the underlying
2907 * function returned. Since Mallory might
2908 * change the rule count in user memory, we
2909 * check that it is less than the rule count
2910 * originally given (as the user buffer size),
2911 * which has been range-checked.
2913 if (get_user(actual_rule_cnt, &rxnfc->rule_cnt))
2914 return -EFAULT;
2915 if (actual_rule_cnt < rule_cnt)
2916 rule_cnt = actual_rule_cnt;
2917 if (copy_in_user(&compat_rxnfc->rule_locs[0],
2918 &rxnfc->rule_locs[0],
2919 rule_cnt * sizeof(u32)))
2920 return -EFAULT;
2924 return 0;
2927 static int compat_siocwandev(struct net *net, struct compat_ifreq __user *uifr32)
2929 void __user *uptr;
2930 compat_uptr_t uptr32;
2931 struct ifreq __user *uifr;
2933 uifr = compat_alloc_user_space(sizeof(*uifr));
2934 if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
2935 return -EFAULT;
2937 if (get_user(uptr32, &uifr32->ifr_settings.ifs_ifsu))
2938 return -EFAULT;
2940 uptr = compat_ptr(uptr32);
2942 if (put_user(uptr, &uifr->ifr_settings.ifs_ifsu.raw_hdlc))
2943 return -EFAULT;
2945 return dev_ioctl(net, SIOCWANDEV, uifr);
2948 static int bond_ioctl(struct net *net, unsigned int cmd,
2949 struct compat_ifreq __user *ifr32)
2951 struct ifreq kifr;
2952 mm_segment_t old_fs;
2953 int err;
2955 switch (cmd) {
2956 case SIOCBONDENSLAVE:
2957 case SIOCBONDRELEASE:
2958 case SIOCBONDSETHWADDR:
2959 case SIOCBONDCHANGEACTIVE:
2960 if (copy_from_user(&kifr, ifr32, sizeof(struct compat_ifreq)))
2961 return -EFAULT;
2963 old_fs = get_fs();
2964 set_fs(KERNEL_DS);
2965 err = dev_ioctl(net, cmd,
2966 (struct ifreq __user __force *) &kifr);
2967 set_fs(old_fs);
2969 return err;
2970 default:
2971 return -ENOIOCTLCMD;
2975 /* Handle ioctls that use ifreq::ifr_data and just need struct ifreq converted */
2976 static int compat_ifr_data_ioctl(struct net *net, unsigned int cmd,
2977 struct compat_ifreq __user *u_ifreq32)
2979 struct ifreq __user *u_ifreq64;
2980 char tmp_buf[IFNAMSIZ];
2981 void __user *data64;
2982 u32 data32;
2984 if (copy_from_user(&tmp_buf[0], &(u_ifreq32->ifr_ifrn.ifrn_name[0]),
2985 IFNAMSIZ))
2986 return -EFAULT;
2987 if (get_user(data32, &u_ifreq32->ifr_ifru.ifru_data))
2988 return -EFAULT;
2989 data64 = compat_ptr(data32);
2991 u_ifreq64 = compat_alloc_user_space(sizeof(*u_ifreq64));
2993 if (copy_to_user(&u_ifreq64->ifr_ifrn.ifrn_name[0], &tmp_buf[0],
2994 IFNAMSIZ))
2995 return -EFAULT;
2996 if (put_user(data64, &u_ifreq64->ifr_ifru.ifru_data))
2997 return -EFAULT;
2999 return dev_ioctl(net, cmd, u_ifreq64);
3002 static int dev_ifsioc(struct net *net, struct socket *sock,
3003 unsigned int cmd, struct compat_ifreq __user *uifr32)
3005 struct ifreq __user *uifr;
3006 int err;
3008 uifr = compat_alloc_user_space(sizeof(*uifr));
3009 if (copy_in_user(uifr, uifr32, sizeof(*uifr32)))
3010 return -EFAULT;
3012 err = sock_do_ioctl(net, sock, cmd, (unsigned long)uifr);
3014 if (!err) {
3015 switch (cmd) {
3016 case SIOCGIFFLAGS:
3017 case SIOCGIFMETRIC:
3018 case SIOCGIFMTU:
3019 case SIOCGIFMEM:
3020 case SIOCGIFHWADDR:
3021 case SIOCGIFINDEX:
3022 case SIOCGIFADDR:
3023 case SIOCGIFBRDADDR:
3024 case SIOCGIFDSTADDR:
3025 case SIOCGIFNETMASK:
3026 case SIOCGIFPFLAGS:
3027 case SIOCGIFTXQLEN:
3028 case SIOCGMIIPHY:
3029 case SIOCGMIIREG:
3030 if (copy_in_user(uifr32, uifr, sizeof(*uifr32)))
3031 err = -EFAULT;
3032 break;
3035 return err;
3038 static int compat_sioc_ifmap(struct net *net, unsigned int cmd,
3039 struct compat_ifreq __user *uifr32)
3041 struct ifreq ifr;
3042 struct compat_ifmap __user *uifmap32;
3043 mm_segment_t old_fs;
3044 int err;
3046 uifmap32 = &uifr32->ifr_ifru.ifru_map;
3047 err = copy_from_user(&ifr, uifr32, sizeof(ifr.ifr_name));
3048 err |= get_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
3049 err |= get_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
3050 err |= get_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
3051 err |= get_user(ifr.ifr_map.irq, &uifmap32->irq);
3052 err |= get_user(ifr.ifr_map.dma, &uifmap32->dma);
3053 err |= get_user(ifr.ifr_map.port, &uifmap32->port);
3054 if (err)
3055 return -EFAULT;
3057 old_fs = get_fs();
3058 set_fs(KERNEL_DS);
3059 err = dev_ioctl(net, cmd, (void __user __force *)&ifr);
3060 set_fs(old_fs);
3062 if (cmd == SIOCGIFMAP && !err) {
3063 err = copy_to_user(uifr32, &ifr, sizeof(ifr.ifr_name));
3064 err |= put_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
3065 err |= put_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
3066 err |= put_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
3067 err |= put_user(ifr.ifr_map.irq, &uifmap32->irq);
3068 err |= put_user(ifr.ifr_map.dma, &uifmap32->dma);
3069 err |= put_user(ifr.ifr_map.port, &uifmap32->port);
3070 if (err)
3071 err = -EFAULT;
3073 return err;
3076 struct rtentry32 {
3077 u32 rt_pad1;
3078 struct sockaddr rt_dst; /* target address */
3079 struct sockaddr rt_gateway; /* gateway addr (RTF_GATEWAY) */
3080 struct sockaddr rt_genmask; /* target network mask (IP) */
3081 unsigned short rt_flags;
3082 short rt_pad2;
3083 u32 rt_pad3;
3084 unsigned char rt_tos;
3085 unsigned char rt_class;
3086 short rt_pad4;
3087 short rt_metric; /* +1 for binary compatibility! */
3088 /* char * */ u32 rt_dev; /* forcing the device at add */
3089 u32 rt_mtu; /* per route MTU/Window */
3090 u32 rt_window; /* Window clamping */
3091 unsigned short rt_irtt; /* Initial RTT */
3094 struct in6_rtmsg32 {
3095 struct in6_addr rtmsg_dst;
3096 struct in6_addr rtmsg_src;
3097 struct in6_addr rtmsg_gateway;
3098 u32 rtmsg_type;
3099 u16 rtmsg_dst_len;
3100 u16 rtmsg_src_len;
3101 u32 rtmsg_metric;
3102 u32 rtmsg_info;
3103 u32 rtmsg_flags;
3104 s32 rtmsg_ifindex;
3107 static int routing_ioctl(struct net *net, struct socket *sock,
3108 unsigned int cmd, void __user *argp)
3110 int ret;
3111 void *r = NULL;
3112 struct in6_rtmsg r6;
3113 struct rtentry r4;
3114 char devname[16];
3115 u32 rtdev;
3116 mm_segment_t old_fs = get_fs();
3118 if (sock && sock->sk && sock->sk->sk_family == AF_INET6) { /* ipv6 */
3119 struct in6_rtmsg32 __user *ur6 = argp;
3120 ret = copy_from_user(&r6.rtmsg_dst, &(ur6->rtmsg_dst),
3121 3 * sizeof(struct in6_addr));
3122 ret |= get_user(r6.rtmsg_type, &(ur6->rtmsg_type));
3123 ret |= get_user(r6.rtmsg_dst_len, &(ur6->rtmsg_dst_len));
3124 ret |= get_user(r6.rtmsg_src_len, &(ur6->rtmsg_src_len));
3125 ret |= get_user(r6.rtmsg_metric, &(ur6->rtmsg_metric));
3126 ret |= get_user(r6.rtmsg_info, &(ur6->rtmsg_info));
3127 ret |= get_user(r6.rtmsg_flags, &(ur6->rtmsg_flags));
3128 ret |= get_user(r6.rtmsg_ifindex, &(ur6->rtmsg_ifindex));
3130 r = (void *) &r6;
3131 } else { /* ipv4 */
3132 struct rtentry32 __user *ur4 = argp;
3133 ret = copy_from_user(&r4.rt_dst, &(ur4->rt_dst),
3134 3 * sizeof(struct sockaddr));
3135 ret |= get_user(r4.rt_flags, &(ur4->rt_flags));
3136 ret |= get_user(r4.rt_metric, &(ur4->rt_metric));
3137 ret |= get_user(r4.rt_mtu, &(ur4->rt_mtu));
3138 ret |= get_user(r4.rt_window, &(ur4->rt_window));
3139 ret |= get_user(r4.rt_irtt, &(ur4->rt_irtt));
3140 ret |= get_user(rtdev, &(ur4->rt_dev));
3141 if (rtdev) {
3142 ret |= copy_from_user(devname, compat_ptr(rtdev), 15);
3143 r4.rt_dev = (char __user __force *)devname;
3144 devname[15] = 0;
3145 } else
3146 r4.rt_dev = NULL;
3148 r = (void *) &r4;
3151 if (ret) {
3152 ret = -EFAULT;
3153 goto out;
3156 set_fs(KERNEL_DS);
3157 ret = sock_do_ioctl(net, sock, cmd, (unsigned long) r);
3158 set_fs(old_fs);
3160 out:
3161 return ret;
3164 /* Since old style bridge ioctl's endup using SIOCDEVPRIVATE
3165 * for some operations; this forces use of the newer bridge-utils that
3166 * use compatible ioctls
3168 static int old_bridge_ioctl(compat_ulong_t __user *argp)
3170 compat_ulong_t tmp;
3172 if (get_user(tmp, argp))
3173 return -EFAULT;
3174 if (tmp == BRCTL_GET_VERSION)
3175 return BRCTL_VERSION + 1;
3176 return -EINVAL;
3179 static int compat_sock_ioctl_trans(struct file *file, struct socket *sock,
3180 unsigned int cmd, unsigned long arg)
3182 void __user *argp = compat_ptr(arg);
3183 struct sock *sk = sock->sk;
3184 struct net *net = sock_net(sk);
3186 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))
3187 return compat_ifr_data_ioctl(net, cmd, argp);
3189 switch (cmd) {
3190 case SIOCSIFBR:
3191 case SIOCGIFBR:
3192 return old_bridge_ioctl(argp);
3193 case SIOCGIFNAME:
3194 return dev_ifname32(net, argp);
3195 case SIOCGIFCONF:
3196 return dev_ifconf(net, argp);
3197 case SIOCETHTOOL:
3198 return ethtool_ioctl(net, argp);
3199 case SIOCWANDEV:
3200 return compat_siocwandev(net, argp);
3201 case SIOCGIFMAP:
3202 case SIOCSIFMAP:
3203 return compat_sioc_ifmap(net, cmd, argp);
3204 case SIOCBONDENSLAVE:
3205 case SIOCBONDRELEASE:
3206 case SIOCBONDSETHWADDR:
3207 case SIOCBONDCHANGEACTIVE:
3208 return bond_ioctl(net, cmd, argp);
3209 case SIOCADDRT:
3210 case SIOCDELRT:
3211 return routing_ioctl(net, sock, cmd, argp);
3212 case SIOCGSTAMP:
3213 return do_siocgstamp(net, sock, cmd, argp);
3214 case SIOCGSTAMPNS:
3215 return do_siocgstampns(net, sock, cmd, argp);
3216 case SIOCBONDSLAVEINFOQUERY:
3217 case SIOCBONDINFOQUERY:
3218 case SIOCSHWTSTAMP:
3219 case SIOCGHWTSTAMP:
3220 return compat_ifr_data_ioctl(net, cmd, argp);
3222 case FIOSETOWN:
3223 case SIOCSPGRP:
3224 case FIOGETOWN:
3225 case SIOCGPGRP:
3226 case SIOCBRADDBR:
3227 case SIOCBRDELBR:
3228 case SIOCGIFVLAN:
3229 case SIOCSIFVLAN:
3230 case SIOCADDDLCI:
3231 case SIOCDELDLCI:
3232 case SIOCGSKNS:
3233 return sock_ioctl(file, cmd, arg);
3235 case SIOCGIFFLAGS:
3236 case SIOCSIFFLAGS:
3237 case SIOCGIFMETRIC:
3238 case SIOCSIFMETRIC:
3239 case SIOCGIFMTU:
3240 case SIOCSIFMTU:
3241 case SIOCGIFMEM:
3242 case SIOCSIFMEM:
3243 case SIOCGIFHWADDR:
3244 case SIOCSIFHWADDR:
3245 case SIOCADDMULTI:
3246 case SIOCDELMULTI:
3247 case SIOCGIFINDEX:
3248 case SIOCGIFADDR:
3249 case SIOCSIFADDR:
3250 case SIOCSIFHWBROADCAST:
3251 case SIOCDIFADDR:
3252 case SIOCGIFBRDADDR:
3253 case SIOCSIFBRDADDR:
3254 case SIOCGIFDSTADDR:
3255 case SIOCSIFDSTADDR:
3256 case SIOCGIFNETMASK:
3257 case SIOCSIFNETMASK:
3258 case SIOCSIFPFLAGS:
3259 case SIOCGIFPFLAGS:
3260 case SIOCGIFTXQLEN:
3261 case SIOCSIFTXQLEN:
3262 case SIOCBRADDIF:
3263 case SIOCBRDELIF:
3264 case SIOCSIFNAME:
3265 case SIOCGMIIPHY:
3266 case SIOCGMIIREG:
3267 case SIOCSMIIREG:
3268 return dev_ifsioc(net, sock, cmd, argp);
3270 case SIOCSARP:
3271 case SIOCGARP:
3272 case SIOCDARP:
3273 case SIOCATMARK:
3274 return sock_do_ioctl(net, sock, cmd, arg);
3277 return -ENOIOCTLCMD;
3280 static long compat_sock_ioctl(struct file *file, unsigned int cmd,
3281 unsigned long arg)
3283 struct socket *sock = file->private_data;
3284 int ret = -ENOIOCTLCMD;
3285 struct sock *sk;
3286 struct net *net;
3288 sk = sock->sk;
3289 net = sock_net(sk);
3291 if (sock->ops->compat_ioctl)
3292 ret = sock->ops->compat_ioctl(sock, cmd, arg);
3294 if (ret == -ENOIOCTLCMD &&
3295 (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST))
3296 ret = compat_wext_handle_ioctl(net, cmd, arg);
3298 if (ret == -ENOIOCTLCMD)
3299 ret = compat_sock_ioctl_trans(file, sock, cmd, arg);
3301 return ret;
3303 #endif
3305 int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
3307 return sock->ops->bind(sock, addr, addrlen);
3309 EXPORT_SYMBOL(kernel_bind);
3311 int kernel_listen(struct socket *sock, int backlog)
3313 return sock->ops->listen(sock, backlog);
3315 EXPORT_SYMBOL(kernel_listen);
3317 int kernel_accept(struct socket *sock, struct socket **newsock, int flags)
3319 struct sock *sk = sock->sk;
3320 int err;
3322 err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
3323 newsock);
3324 if (err < 0)
3325 goto done;
3327 err = sock->ops->accept(sock, *newsock, flags, true);
3328 if (err < 0) {
3329 sock_release(*newsock);
3330 *newsock = NULL;
3331 goto done;
3334 (*newsock)->ops = sock->ops;
3335 __module_get((*newsock)->ops->owner);
3337 done:
3338 return err;
3340 EXPORT_SYMBOL(kernel_accept);
3342 int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
3343 int flags)
3345 return sock->ops->connect(sock, addr, addrlen, flags);
3347 EXPORT_SYMBOL(kernel_connect);
3349 int kernel_getsockname(struct socket *sock, struct sockaddr *addr,
3350 int *addrlen)
3352 return sock->ops->getname(sock, addr, addrlen, 0);
3354 EXPORT_SYMBOL(kernel_getsockname);
3356 int kernel_getpeername(struct socket *sock, struct sockaddr *addr,
3357 int *addrlen)
3359 return sock->ops->getname(sock, addr, addrlen, 1);
3361 EXPORT_SYMBOL(kernel_getpeername);
3363 int kernel_getsockopt(struct socket *sock, int level, int optname,
3364 char *optval, int *optlen)
3366 mm_segment_t oldfs = get_fs();
3367 char __user *uoptval;
3368 int __user *uoptlen;
3369 int err;
3371 uoptval = (char __user __force *) optval;
3372 uoptlen = (int __user __force *) optlen;
3374 set_fs(KERNEL_DS);
3375 if (level == SOL_SOCKET)
3376 err = sock_getsockopt(sock, level, optname, uoptval, uoptlen);
3377 else
3378 err = sock->ops->getsockopt(sock, level, optname, uoptval,
3379 uoptlen);
3380 set_fs(oldfs);
3381 return err;
3383 EXPORT_SYMBOL(kernel_getsockopt);
3385 int kernel_setsockopt(struct socket *sock, int level, int optname,
3386 char *optval, unsigned int optlen)
3388 mm_segment_t oldfs = get_fs();
3389 char __user *uoptval;
3390 int err;
3392 uoptval = (char __user __force *) optval;
3394 set_fs(KERNEL_DS);
3395 if (level == SOL_SOCKET)
3396 err = sock_setsockopt(sock, level, optname, uoptval, optlen);
3397 else
3398 err = sock->ops->setsockopt(sock, level, optname, uoptval,
3399 optlen);
3400 set_fs(oldfs);
3401 return err;
3403 EXPORT_SYMBOL(kernel_setsockopt);
3405 int kernel_sendpage(struct socket *sock, struct page *page, int offset,
3406 size_t size, int flags)
3408 if (sock->ops->sendpage)
3409 return sock->ops->sendpage(sock, page, offset, size, flags);
3411 return sock_no_sendpage(sock, page, offset, size, flags);
3413 EXPORT_SYMBOL(kernel_sendpage);
3415 int kernel_sendpage_locked(struct sock *sk, struct page *page, int offset,
3416 size_t size, int flags)
3418 struct socket *sock = sk->sk_socket;
3420 if (sock->ops->sendpage_locked)
3421 return sock->ops->sendpage_locked(sk, page, offset, size,
3422 flags);
3424 return sock_no_sendpage_locked(sk, page, offset, size, flags);
3426 EXPORT_SYMBOL(kernel_sendpage_locked);
3428 int kernel_sock_ioctl(struct socket *sock, int cmd, unsigned long arg)
3430 mm_segment_t oldfs = get_fs();
3431 int err;
3433 set_fs(KERNEL_DS);
3434 err = sock->ops->ioctl(sock, cmd, arg);
3435 set_fs(oldfs);
3437 return err;
3439 EXPORT_SYMBOL(kernel_sock_ioctl);
3441 int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how)
3443 return sock->ops->shutdown(sock, how);
3445 EXPORT_SYMBOL(kernel_sock_shutdown);
3447 /* This routine returns the IP overhead imposed by a socket i.e.
3448 * the length of the underlying IP header, depending on whether
3449 * this is an IPv4 or IPv6 socket and the length from IP options turned
3450 * on at the socket. Assumes that the caller has a lock on the socket.
3452 u32 kernel_sock_ip_overhead(struct sock *sk)
3454 struct inet_sock *inet;
3455 struct ip_options_rcu *opt;
3456 u32 overhead = 0;
3457 #if IS_ENABLED(CONFIG_IPV6)
3458 struct ipv6_pinfo *np;
3459 struct ipv6_txoptions *optv6 = NULL;
3460 #endif /* IS_ENABLED(CONFIG_IPV6) */
3462 if (!sk)
3463 return overhead;
3465 switch (sk->sk_family) {
3466 case AF_INET:
3467 inet = inet_sk(sk);
3468 overhead += sizeof(struct iphdr);
3469 opt = rcu_dereference_protected(inet->inet_opt,
3470 sock_owned_by_user(sk));
3471 if (opt)
3472 overhead += opt->opt.optlen;
3473 return overhead;
3474 #if IS_ENABLED(CONFIG_IPV6)
3475 case AF_INET6:
3476 np = inet6_sk(sk);
3477 overhead += sizeof(struct ipv6hdr);
3478 if (np)
3479 optv6 = rcu_dereference_protected(np->opt,
3480 sock_owned_by_user(sk));
3481 if (optv6)
3482 overhead += (optv6->opt_flen + optv6->opt_nflen);
3483 return overhead;
3484 #endif /* IS_ENABLED(CONFIG_IPV6) */
3485 default: /* Returns 0 overhead if the socket is not ipv4 or ipv6 */
3486 return overhead;
3489 EXPORT_SYMBOL(kernel_sock_ip_overhead);