1 /*
2 * NET An implementation of the SOCKET network access protocol.
3 *
4 * Version: @(#)socket.c 1.1.93 18/02/95
5 *
6 * Authors: Orest Zborowski, <obz@Kodak.COM>
7 * Ross Biro
8 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
9 *
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
47 *
48 *
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.
53 *
54 *
55 * This module is effectively the top level interface to the BSD socket
56 * paradigm.
57 *
58 * Based upon Swansea University Computer Society NET3.039
59 */
60
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>
93
94 #include <asm/uaccess.h>
95 #include <asm/unistd.h>
96
97 #include <net/compat.h>
98 #include <net/wext.h>
99 #include <net/cls_cgroup.h>
100
101 #include <net/sock.h>
102 #include <linux/netfilter.h>
103
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>
111
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
116
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);
120
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);
135
136 /*
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.
139 */
140
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,
157 };
158
159 /*
160 * The protocol list. Each protocol is registered in here.
161 */
162
163 static DEFINE_SPINLOCK(net_family_lock);
164 static const struct net_proto_family __rcu *net_families[NPROTO] __read_mostly;
165
166 /*
167 * Statistics counters of the socket lists
168 */
169
170 static DEFINE_PER_CPU(int, sockets_in_use);
171
172 /*
173 * Support routines.
174 * Move socket addresses back and forth across the kernel/user
175 * divide and look after the messy bits.
176 */
177
178 /**
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
183 *
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.
187 */
188
move_addr_to_kernel(void __user * uaddr,int ulen,struct sockaddr_storage * kaddr)189 int move_addr_to_kernel(void __user *uaddr, int ulen, struct sockaddr_storage *kaddr)
190 {
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);
198 }
199
200 /**
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
206 *
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.
215 */
216
move_addr_to_user(struct sockaddr_storage * kaddr,int klen,void __user * uaddr,int __user * ulen)217 static int move_addr_to_user(struct sockaddr_storage *kaddr, int klen,
218 void __user *uaddr, int __user *ulen)
219 {
220 int err;
221 int len;
222
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;
236 }
237 /*
238 * "fromlen shall refer to the value before truncation.."
239 * 1003.1g
240 */
241 return __put_user(klen, ulen);
242 }
243
244 static struct kmem_cache *sock_inode_cachep __read_mostly;
245
sock_alloc_inode(struct super_block * sb)246 static struct inode *sock_alloc_inode(struct super_block *sb)
247 {
248 struct socket_alloc *ei;
249 struct socket_wq *wq;
250
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;
258 }
259 init_waitqueue_head(&wq->wait);
260 wq->fasync_list = NULL;
261 wq->flags = 0;
262 RCU_INIT_POINTER(ei->socket.wq, wq);
263
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;
269
270 return &ei->vfs_inode;
271 }
272
sock_destroy_inode(struct inode * inode)273 static void sock_destroy_inode(struct inode *inode)
274 {
275 struct socket_alloc *ei;
276 struct socket_wq *wq;
277
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);
282 }
283
init_once(void * foo)284 static void init_once(void *foo)
285 {
286 struct socket_alloc *ei = (struct socket_alloc *)foo;
287
288 inode_init_once(&ei->vfs_inode);
289 }
290
init_inodecache(void)291 static int init_inodecache(void)
292 {
293 sock_inode_cachep = kmem_cache_create("sock_inode_cache",
294 sizeof(struct socket_alloc),
295 0,
296 (SLAB_HWCACHE_ALIGN |
297 SLAB_RECLAIM_ACCOUNT |
298 SLAB_MEM_SPREAD),
299 init_once);
300 if (sock_inode_cachep == NULL)
301 return -ENOMEM;
302 return 0;
303 }
304
305 static const struct super_operations sockfs_ops = {
306 .alloc_inode = sock_alloc_inode,
307 .destroy_inode = sock_destroy_inode,
308 .statfs = simple_statfs,
309 };
310
311 /*
312 * sockfs_dname() is called from d_path().
313 */
sockfs_dname(struct dentry * dentry,char * buffer,int buflen)314 static char *sockfs_dname(struct dentry *dentry, char *buffer, int buflen)
315 {
316 return dynamic_dname(dentry, buffer, buflen, "socket:[%lu]",
317 d_inode(dentry)->i_ino);
318 }
319
320 static const struct dentry_operations sockfs_dentry_operations = {
321 .d_dname = sockfs_dname,
322 };
323
sockfs_mount(struct file_system_type * fs_type,int flags,const char * dev_name,void * data)324 static struct dentry *sockfs_mount(struct file_system_type *fs_type,
325 int flags, const char *dev_name, void *data)
326 {
327 return mount_pseudo(fs_type, "socket:", &sockfs_ops,
328 &sockfs_dentry_operations, SOCKFS_MAGIC);
329 }
330
331 static struct vfsmount *sock_mnt __read_mostly;
332
333 static struct file_system_type sock_fs_type = {
334 .name = "sockfs",
335 .mount = sockfs_mount,
336 .kill_sb = kill_anon_super,
337 };
338
339 /*
340 * Obtains the first available file descriptor and sets it up for use.
341 *
342 * These functions create file structures and maps them to fd space
343 * of the current process. On success it returns file descriptor
344 * and file struct implicitly stored in sock->file.
345 * Note that another thread may close file descriptor before we return
346 * from this function. We use the fact that now we do not refer
347 * to socket after mapping. If one day we will need it, this
348 * function will increment ref. count on file by 1.
349 *
350 * In any case returned fd MAY BE not valid!
351 * This race condition is unavoidable
352 * with shared fd spaces, we cannot solve it inside kernel,
353 * but we take care of internal coherence yet.
354 */
355
sock_alloc_file(struct socket * sock,int flags,const char * dname)356 struct file *sock_alloc_file(struct socket *sock, int flags, const char *dname)
357 {
358 struct qstr name = { .name = "" };
359 struct path path;
360 struct file *file;
361
362 if (dname) {
363 name.name = dname;
364 name.len = strlen(name.name);
365 } else if (sock->sk) {
366 name.name = sock->sk->sk_prot_creator->name;
367 name.len = strlen(name.name);
368 }
369 path.dentry = d_alloc_pseudo(sock_mnt->mnt_sb, &name);
370 if (unlikely(!path.dentry))
371 return ERR_PTR(-ENOMEM);
372 path.mnt = mntget(sock_mnt);
373
374 d_instantiate(path.dentry, SOCK_INODE(sock));
375
376 file = alloc_file(&path, FMODE_READ | FMODE_WRITE,
377 &socket_file_ops);
378 if (IS_ERR(file)) {
379 /* drop dentry, keep inode */
380 ihold(d_inode(path.dentry));
381 path_put(&path);
382 return file;
383 }
384
385 sock->file = file;
386 file->f_flags = O_RDWR | (flags & O_NONBLOCK);
387 file->private_data = sock;
388 return file;
389 }
390 EXPORT_SYMBOL(sock_alloc_file);
391
sock_map_fd(struct socket * sock,int flags)392 static int sock_map_fd(struct socket *sock, int flags)
393 {
394 struct file *newfile;
395 int fd = get_unused_fd_flags(flags);
396 if (unlikely(fd < 0))
397 return fd;
398
399 newfile = sock_alloc_file(sock, flags, NULL);
400 if (likely(!IS_ERR(newfile))) {
401 fd_install(fd, newfile);
402 return fd;
403 }
404
405 put_unused_fd(fd);
406 return PTR_ERR(newfile);
407 }
408
sock_from_file(struct file * file,int * err)409 struct socket *sock_from_file(struct file *file, int *err)
410 {
411 if (file->f_op == &socket_file_ops)
412 return file->private_data; /* set in sock_map_fd */
413
414 *err = -ENOTSOCK;
415 return NULL;
416 }
417 EXPORT_SYMBOL(sock_from_file);
418
419 /**
420 * sockfd_lookup - Go from a file number to its socket slot
421 * @fd: file handle
422 * @err: pointer to an error code return
423 *
424 * The file handle passed in is locked and the socket it is bound
425 * too is returned. If an error occurs the err pointer is overwritten
426 * with a negative errno code and NULL is returned. The function checks
427 * for both invalid handles and passing a handle which is not a socket.
428 *
429 * On a success the socket object pointer is returned.
430 */
431
sockfd_lookup(int fd,int * err)432 struct socket *sockfd_lookup(int fd, int *err)
433 {
434 struct file *file;
435 struct socket *sock;
436
437 file = fget(fd);
438 if (!file) {
439 *err = -EBADF;
440 return NULL;
441 }
442
443 sock = sock_from_file(file, err);
444 if (!sock)
445 fput(file);
446 return sock;
447 }
448 EXPORT_SYMBOL(sockfd_lookup);
449
sockfd_lookup_light(int fd,int * err,int * fput_needed)450 static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)
451 {
452 struct fd f = fdget(fd);
453 struct socket *sock;
454
455 *err = -EBADF;
456 if (f.file) {
457 sock = sock_from_file(f.file, err);
458 if (likely(sock)) {
459 *fput_needed = f.flags & FDPUT_FPUT;
460 return sock;
461 }
462 fdput(f);
463 }
464 return NULL;
465 }
466
467 #define XATTR_SOCKPROTONAME_SUFFIX "sockprotoname"
468 #define XATTR_NAME_SOCKPROTONAME (XATTR_SYSTEM_PREFIX XATTR_SOCKPROTONAME_SUFFIX)
469 #define XATTR_NAME_SOCKPROTONAME_LEN (sizeof(XATTR_NAME_SOCKPROTONAME)-1)
sockfs_getxattr(struct dentry * dentry,const char * name,void * value,size_t size)470 static ssize_t sockfs_getxattr(struct dentry *dentry,
471 const char *name, void *value, size_t size)
472 {
473 if (!strcmp(name, XATTR_NAME_SOCKPROTONAME)) {
474 if (value) {
475 if (dentry->d_name.len + 1 > size)
476 return -ERANGE;
477 memcpy(value, dentry->d_name.name, dentry->d_name.len + 1);
478 }
479 return dentry->d_name.len + 1;
480 }
481 return -EOPNOTSUPP;
482 }
483
sockfs_listxattr(struct dentry * dentry,char * buffer,size_t size)484 static ssize_t sockfs_listxattr(struct dentry *dentry, char *buffer,
485 size_t size)
486 {
487 ssize_t len;
488 ssize_t used = 0;
489
490 len = security_inode_listsecurity(d_inode(dentry), buffer, size);
491 if (len < 0)
492 return len;
493 used += len;
494 if (buffer) {
495 if (size < used)
496 return -ERANGE;
497 buffer += len;
498 }
499
500 len = (XATTR_NAME_SOCKPROTONAME_LEN + 1);
501 used += len;
502 if (buffer) {
503 if (size < used)
504 return -ERANGE;
505 memcpy(buffer, XATTR_NAME_SOCKPROTONAME, len);
506 buffer += len;
507 }
508
509 return used;
510 }
511
sockfs_setattr(struct dentry * dentry,struct iattr * iattr)512 static int sockfs_setattr(struct dentry *dentry, struct iattr *iattr)
513 {
514 int err = simple_setattr(dentry, iattr);
515
516 if (!err && (iattr->ia_valid & ATTR_UID)) {
517 struct socket *sock = SOCKET_I(d_inode(dentry));
518
519 if (sock->sk)
520 sock->sk->sk_uid = iattr->ia_uid;
521 else
522 err = -ENOENT;
523 }
524
525 return err;
526 }
527
528 static const struct inode_operations sockfs_inode_ops = {
529 .getxattr = sockfs_getxattr,
530 .listxattr = sockfs_listxattr,
531 .setattr = sockfs_setattr,
532 };
533
534 /**
535 * sock_alloc - allocate a socket
536 *
537 * Allocate a new inode and socket object. The two are bound together
538 * and initialised. The socket is then returned. If we are out of inodes
539 * NULL is returned.
540 */
541
sock_alloc(void)542 static struct socket *sock_alloc(void)
543 {
544 struct inode *inode;
545 struct socket *sock;
546
547 inode = new_inode_pseudo(sock_mnt->mnt_sb);
548 if (!inode)
549 return NULL;
550
551 sock = SOCKET_I(inode);
552
553 kmemcheck_annotate_bitfield(sock, type);
554 inode->i_ino = get_next_ino();
555 inode->i_mode = S_IFSOCK | S_IRWXUGO;
556 inode->i_uid = current_fsuid();
557 inode->i_gid = current_fsgid();
558 inode->i_op = &sockfs_inode_ops;
559
560 this_cpu_add(sockets_in_use, 1);
561 return sock;
562 }
563
564 /**
565 * sock_release - close a socket
566 * @sock: socket to close
567 *
568 * The socket is released from the protocol stack if it has a release
569 * callback, and the inode is then released if the socket is bound to
570 * an inode not a file.
571 */
572
__sock_release(struct socket * sock,struct inode * inode)573 static void __sock_release(struct socket *sock, struct inode *inode)
574 {
575 if (sock->ops) {
576 struct module *owner = sock->ops->owner;
577
578 if (inode)
579 inode_lock(inode);
580 sock->ops->release(sock);
581 sock->sk = NULL;
582 if (inode)
583 inode_unlock(inode);
584 sock->ops = NULL;
585 module_put(owner);
586 }
587
588 if (rcu_dereference_protected(sock->wq, 1)->fasync_list)
589 pr_err("%s: fasync list not empty!\n", __func__);
590
591 this_cpu_sub(sockets_in_use, 1);
592 if (!sock->file) {
593 iput(SOCK_INODE(sock));
594 return;
595 }
596 sock->file = NULL;
597 }
598
sock_release(struct socket * sock)599 void sock_release(struct socket *sock)
600 {
601 __sock_release(sock, NULL);
602 }
603 EXPORT_SYMBOL(sock_release);
604
__sock_tx_timestamp(const struct sock * sk,__u8 * tx_flags)605 void __sock_tx_timestamp(const struct sock *sk, __u8 *tx_flags)
606 {
607 u8 flags = *tx_flags;
608
609 if (sk->sk_tsflags & SOF_TIMESTAMPING_TX_HARDWARE)
610 flags |= SKBTX_HW_TSTAMP;
611
612 if (sk->sk_tsflags & SOF_TIMESTAMPING_TX_SOFTWARE)
613 flags |= SKBTX_SW_TSTAMP;
614
615 if (sk->sk_tsflags & SOF_TIMESTAMPING_TX_SCHED)
616 flags |= SKBTX_SCHED_TSTAMP;
617
618 if (sk->sk_tsflags & SOF_TIMESTAMPING_TX_ACK)
619 flags |= SKBTX_ACK_TSTAMP;
620
621 *tx_flags = flags;
622 }
623 EXPORT_SYMBOL(__sock_tx_timestamp);
624
sock_sendmsg_nosec(struct socket * sock,struct msghdr * msg)625 static inline int sock_sendmsg_nosec(struct socket *sock, struct msghdr *msg)
626 {
627 int ret = sock->ops->sendmsg(sock, msg, msg_data_left(msg));
628 BUG_ON(ret == -EIOCBQUEUED);
629 return ret;
630 }
631
sock_sendmsg(struct socket * sock,struct msghdr * msg)632 int sock_sendmsg(struct socket *sock, struct msghdr *msg)
633 {
634 int err = security_socket_sendmsg(sock, msg,
635 msg_data_left(msg));
636
637 return err ?: sock_sendmsg_nosec(sock, msg);
638 }
639 EXPORT_SYMBOL(sock_sendmsg);
640
kernel_sendmsg(struct socket * sock,struct msghdr * msg,struct kvec * vec,size_t num,size_t size)641 int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
642 struct kvec *vec, size_t num, size_t size)
643 {
644 iov_iter_kvec(&msg->msg_iter, WRITE | ITER_KVEC, vec, num, size);
645 return sock_sendmsg(sock, msg);
646 }
647 EXPORT_SYMBOL(kernel_sendmsg);
648
649 /*
650 * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
651 */
__sock_recv_timestamp(struct msghdr * msg,struct sock * sk,struct sk_buff * skb)652 void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
653 struct sk_buff *skb)
654 {
655 int need_software_tstamp = sock_flag(sk, SOCK_RCVTSTAMP);
656 struct scm_timestamping tss;
657 int empty = 1;
658 struct skb_shared_hwtstamps *shhwtstamps =
659 skb_hwtstamps(skb);
660
661 /* Race occurred between timestamp enabling and packet
662 receiving. Fill in the current time for now. */
663 if (need_software_tstamp && skb->tstamp.tv64 == 0)
664 __net_timestamp(skb);
665
666 if (need_software_tstamp) {
667 if (!sock_flag(sk, SOCK_RCVTSTAMPNS)) {
668 struct timeval tv;
669 skb_get_timestamp(skb, &tv);
670 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMP,
671 sizeof(tv), &tv);
672 } else {
673 struct timespec ts;
674 skb_get_timestampns(skb, &ts);
675 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPNS,
676 sizeof(ts), &ts);
677 }
678 }
679
680 memset(&tss, 0, sizeof(tss));
681 if ((sk->sk_tsflags & SOF_TIMESTAMPING_SOFTWARE) &&
682 ktime_to_timespec_cond(skb->tstamp, tss.ts + 0))
683 empty = 0;
684 if (shhwtstamps &&
685 (sk->sk_tsflags & SOF_TIMESTAMPING_RAW_HARDWARE) &&
686 ktime_to_timespec_cond(shhwtstamps->hwtstamp, tss.ts + 2))
687 empty = 0;
688 if (!empty)
689 put_cmsg(msg, SOL_SOCKET,
690 SCM_TIMESTAMPING, sizeof(tss), &tss);
691 }
692 EXPORT_SYMBOL_GPL(__sock_recv_timestamp);
693
__sock_recv_wifi_status(struct msghdr * msg,struct sock * sk,struct sk_buff * skb)694 void __sock_recv_wifi_status(struct msghdr *msg, struct sock *sk,
695 struct sk_buff *skb)
696 {
697 int ack;
698
699 if (!sock_flag(sk, SOCK_WIFI_STATUS))
700 return;
701 if (!skb->wifi_acked_valid)
702 return;
703
704 ack = skb->wifi_acked;
705
706 put_cmsg(msg, SOL_SOCKET, SCM_WIFI_STATUS, sizeof(ack), &ack);
707 }
708 EXPORT_SYMBOL_GPL(__sock_recv_wifi_status);
709
sock_recv_drops(struct msghdr * msg,struct sock * sk,struct sk_buff * skb)710 static inline void sock_recv_drops(struct msghdr *msg, struct sock *sk,
711 struct sk_buff *skb)
712 {
713 if (sock_flag(sk, SOCK_RXQ_OVFL) && skb && SOCK_SKB_CB(skb)->dropcount)
714 put_cmsg(msg, SOL_SOCKET, SO_RXQ_OVFL,
715 sizeof(__u32), &SOCK_SKB_CB(skb)->dropcount);
716 }
717
__sock_recv_ts_and_drops(struct msghdr * msg,struct sock * sk,struct sk_buff * skb)718 void __sock_recv_ts_and_drops(struct msghdr *msg, struct sock *sk,
719 struct sk_buff *skb)
720 {
721 sock_recv_timestamp(msg, sk, skb);
722 sock_recv_drops(msg, sk, skb);
723 }
724 EXPORT_SYMBOL_GPL(__sock_recv_ts_and_drops);
725
sock_recvmsg_nosec(struct socket * sock,struct msghdr * msg,size_t size,int flags)726 static inline int sock_recvmsg_nosec(struct socket *sock, struct msghdr *msg,
727 size_t size, int flags)
728 {
729 return sock->ops->recvmsg(sock, msg, size, flags);
730 }
731
sock_recvmsg(struct socket * sock,struct msghdr * msg,size_t size,int flags)732 int sock_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
733 int flags)
734 {
735 int err = security_socket_recvmsg(sock, msg, size, flags);
736
737 return err ?: sock_recvmsg_nosec(sock, msg, size, flags);
738 }
739 EXPORT_SYMBOL(sock_recvmsg);
740
741 /**
742 * kernel_recvmsg - Receive a message from a socket (kernel space)
743 * @sock: The socket to receive the message from
744 * @msg: Received message
745 * @vec: Input s/g array for message data
746 * @num: Size of input s/g array
747 * @size: Number of bytes to read
748 * @flags: Message flags (MSG_DONTWAIT, etc...)
749 *
750 * On return the msg structure contains the scatter/gather array passed in the
751 * vec argument. The array is modified so that it consists of the unfilled
752 * portion of the original array.
753 *
754 * The returned value is the total number of bytes received, or an error.
755 */
kernel_recvmsg(struct socket * sock,struct msghdr * msg,struct kvec * vec,size_t num,size_t size,int flags)756 int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
757 struct kvec *vec, size_t num, size_t size, int flags)
758 {
759 mm_segment_t oldfs = get_fs();
760 int result;
761
762 iov_iter_kvec(&msg->msg_iter, READ | ITER_KVEC, vec, num, size);
763 set_fs(KERNEL_DS);
764 result = sock_recvmsg(sock, msg, size, flags);
765 set_fs(oldfs);
766 return result;
767 }
768 EXPORT_SYMBOL(kernel_recvmsg);
769
sock_sendpage(struct file * file,struct page * page,int offset,size_t size,loff_t * ppos,int more)770 static ssize_t sock_sendpage(struct file *file, struct page *page,
771 int offset, size_t size, loff_t *ppos, int more)
772 {
773 struct socket *sock;
774 int flags;
775
776 sock = file->private_data;
777
778 flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
779 /* more is a combination of MSG_MORE and MSG_SENDPAGE_NOTLAST */
780 flags |= more;
781
782 return kernel_sendpage(sock, page, offset, size, flags);
783 }
784
sock_splice_read(struct file * file,loff_t * ppos,struct pipe_inode_info * pipe,size_t len,unsigned int flags)785 static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
786 struct pipe_inode_info *pipe, size_t len,
787 unsigned int flags)
788 {
789 struct socket *sock = file->private_data;
790
791 if (unlikely(!sock->ops->splice_read))
792 return -EINVAL;
793
794 return sock->ops->splice_read(sock, ppos, pipe, len, flags);
795 }
796
sock_read_iter(struct kiocb * iocb,struct iov_iter * to)797 static ssize_t sock_read_iter(struct kiocb *iocb, struct iov_iter *to)
798 {
799 struct file *file = iocb->ki_filp;
800 struct socket *sock = file->private_data;
801 struct msghdr msg = {.msg_iter = *to,
802 .msg_iocb = iocb};
803 ssize_t res;
804
805 if (file->f_flags & O_NONBLOCK)
806 msg.msg_flags = MSG_DONTWAIT;
807
808 if (iocb->ki_pos != 0)
809 return -ESPIPE;
810
811 if (!iov_iter_count(to)) /* Match SYS5 behaviour */
812 return 0;
813
814 res = sock_recvmsg(sock, &msg, iov_iter_count(to), msg.msg_flags);
815 *to = msg.msg_iter;
816 return res;
817 }
818
sock_write_iter(struct kiocb * iocb,struct iov_iter * from)819 static ssize_t sock_write_iter(struct kiocb *iocb, struct iov_iter *from)
820 {
821 struct file *file = iocb->ki_filp;
822 struct socket *sock = file->private_data;
823 struct msghdr msg = {.msg_iter = *from,
824 .msg_iocb = iocb};
825 ssize_t res;
826
827 if (iocb->ki_pos != 0)
828 return -ESPIPE;
829
830 if (file->f_flags & O_NONBLOCK)
831 msg.msg_flags = MSG_DONTWAIT;
832
833 if (sock->type == SOCK_SEQPACKET)
834 msg.msg_flags |= MSG_EOR;
835
836 res = sock_sendmsg(sock, &msg);
837 *from = msg.msg_iter;
838 return res;
839 }
840
841 /*
842 * Atomic setting of ioctl hooks to avoid race
843 * with module unload.
844 */
845
846 static DEFINE_MUTEX(br_ioctl_mutex);
847 static int (*br_ioctl_hook) (struct net *, unsigned int cmd, void __user *arg);
848
brioctl_set(int (* hook)(struct net *,unsigned int,void __user *))849 void brioctl_set(int (*hook) (struct net *, unsigned int, void __user *))
850 {
851 mutex_lock(&br_ioctl_mutex);
852 br_ioctl_hook = hook;
853 mutex_unlock(&br_ioctl_mutex);
854 }
855 EXPORT_SYMBOL(brioctl_set);
856
857 static DEFINE_MUTEX(vlan_ioctl_mutex);
858 static int (*vlan_ioctl_hook) (struct net *, void __user *arg);
859
vlan_ioctl_set(int (* hook)(struct net *,void __user *))860 void vlan_ioctl_set(int (*hook) (struct net *, void __user *))
861 {
862 mutex_lock(&vlan_ioctl_mutex);
863 vlan_ioctl_hook = hook;
864 mutex_unlock(&vlan_ioctl_mutex);
865 }
866 EXPORT_SYMBOL(vlan_ioctl_set);
867
868 static DEFINE_MUTEX(dlci_ioctl_mutex);
869 static int (*dlci_ioctl_hook) (unsigned int, void __user *);
870
dlci_ioctl_set(int (* hook)(unsigned int,void __user *))871 void dlci_ioctl_set(int (*hook) (unsigned int, void __user *))
872 {
873 mutex_lock(&dlci_ioctl_mutex);
874 dlci_ioctl_hook = hook;
875 mutex_unlock(&dlci_ioctl_mutex);
876 }
877 EXPORT_SYMBOL(dlci_ioctl_set);
878
sock_do_ioctl(struct net * net,struct socket * sock,unsigned int cmd,unsigned long arg)879 static long sock_do_ioctl(struct net *net, struct socket *sock,
880 unsigned int cmd, unsigned long arg)
881 {
882 int err;
883 void __user *argp = (void __user *)arg;
884
885 err = sock->ops->ioctl(sock, cmd, arg);
886
887 /*
888 * If this ioctl is unknown try to hand it down
889 * to the NIC driver.
890 */
891 if (err == -ENOIOCTLCMD)
892 err = dev_ioctl(net, cmd, argp);
893
894 return err;
895 }
896
897 /*
898 * With an ioctl, arg may well be a user mode pointer, but we don't know
899 * what to do with it - that's up to the protocol still.
900 */
901
sock_ioctl(struct file * file,unsigned cmd,unsigned long arg)902 static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
903 {
904 struct socket *sock;
905 struct sock *sk;
906 void __user *argp = (void __user *)arg;
907 int pid, err;
908 struct net *net;
909
910 sock = file->private_data;
911 sk = sock->sk;
912 net = sock_net(sk);
913 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
914 err = dev_ioctl(net, cmd, argp);
915 } else
916 #ifdef CONFIG_WEXT_CORE
917 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
918 err = dev_ioctl(net, cmd, argp);
919 } else
920 #endif
921 switch (cmd) {
922 case FIOSETOWN:
923 case SIOCSPGRP:
924 err = -EFAULT;
925 if (get_user(pid, (int __user *)argp))
926 break;
927 f_setown(sock->file, pid, 1);
928 err = 0;
929 break;
930 case FIOGETOWN:
931 case SIOCGPGRP:
932 err = put_user(f_getown(sock->file),
933 (int __user *)argp);
934 break;
935 case SIOCGIFBR:
936 case SIOCSIFBR:
937 case SIOCBRADDBR:
938 case SIOCBRDELBR:
939 err = -ENOPKG;
940 if (!br_ioctl_hook)
941 request_module("bridge");
942
943 mutex_lock(&br_ioctl_mutex);
944 if (br_ioctl_hook)
945 err = br_ioctl_hook(net, cmd, argp);
946 mutex_unlock(&br_ioctl_mutex);
947 break;
948 case SIOCGIFVLAN:
949 case SIOCSIFVLAN:
950 err = -ENOPKG;
951 if (!vlan_ioctl_hook)
952 request_module("8021q");
953
954 mutex_lock(&vlan_ioctl_mutex);
955 if (vlan_ioctl_hook)
956 err = vlan_ioctl_hook(net, argp);
957 mutex_unlock(&vlan_ioctl_mutex);
958 break;
959 case SIOCADDDLCI:
960 case SIOCDELDLCI:
961 err = -ENOPKG;
962 if (!dlci_ioctl_hook)
963 request_module("dlci");
964
965 mutex_lock(&dlci_ioctl_mutex);
966 if (dlci_ioctl_hook)
967 err = dlci_ioctl_hook(cmd, argp);
968 mutex_unlock(&dlci_ioctl_mutex);
969 break;
970 default:
971 err = sock_do_ioctl(net, sock, cmd, arg);
972 break;
973 }
974 return err;
975 }
976
sock_create_lite(int family,int type,int protocol,struct socket ** res)977 int sock_create_lite(int family, int type, int protocol, struct socket **res)
978 {
979 int err;
980 struct socket *sock = NULL;
981
982 err = security_socket_create(family, type, protocol, 1);
983 if (err)
984 goto out;
985
986 sock = sock_alloc();
987 if (!sock) {
988 err = -ENOMEM;
989 goto out;
990 }
991
992 sock->type = type;
993 err = security_socket_post_create(sock, family, type, protocol, 1);
994 if (err)
995 goto out_release;
996
997 out:
998 *res = sock;
999 return err;
1000 out_release:
1001 sock_release(sock);
1002 sock = NULL;
1003 goto out;
1004 }
1005 EXPORT_SYMBOL(sock_create_lite);
1006
1007 /* No kernel lock held - perfect */
sock_poll(struct file * file,poll_table * wait)1008 static unsigned int sock_poll(struct file *file, poll_table *wait)
1009 {
1010 unsigned int busy_flag = 0;
1011 struct socket *sock;
1012
1013 /*
1014 * We can't return errors to poll, so it's either yes or no.
1015 */
1016 sock = file->private_data;
1017
1018 if (sk_can_busy_loop(sock->sk)) {
1019 /* this socket can poll_ll so tell the system call */
1020 busy_flag = POLL_BUSY_LOOP;
1021
1022 /* once, only if requested by syscall */
1023 if (wait && (wait->_key & POLL_BUSY_LOOP))
1024 sk_busy_loop(sock->sk, 1);
1025 }
1026
1027 return busy_flag | sock->ops->poll(file, sock, wait);
1028 }
1029
sock_mmap(struct file * file,struct vm_area_struct * vma)1030 static int sock_mmap(struct file *file, struct vm_area_struct *vma)
1031 {
1032 struct socket *sock = file->private_data;
1033
1034 return sock->ops->mmap(file, sock, vma);
1035 }
1036
sock_close(struct inode * inode,struct file * filp)1037 static int sock_close(struct inode *inode, struct file *filp)
1038 {
1039 __sock_release(SOCKET_I(inode), inode);
1040 return 0;
1041 }
1042
1043 /*
1044 * Update the socket async list
1045 *
1046 * Fasync_list locking strategy.
1047 *
1048 * 1. fasync_list is modified only under process context socket lock
1049 * i.e. under semaphore.
1050 * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
1051 * or under socket lock
1052 */
1053
sock_fasync(int fd,struct file * filp,int on)1054 static int sock_fasync(int fd, struct file *filp, int on)
1055 {
1056 struct socket *sock = filp->private_data;
1057 struct sock *sk = sock->sk;
1058 struct socket_wq *wq;
1059
1060 if (sk == NULL)
1061 return -EINVAL;
1062
1063 lock_sock(sk);
1064 wq = rcu_dereference_protected(sock->wq, sock_owned_by_user(sk));
1065 fasync_helper(fd, filp, on, &wq->fasync_list);
1066
1067 if (!wq->fasync_list)
1068 sock_reset_flag(sk, SOCK_FASYNC);
1069 else
1070 sock_set_flag(sk, SOCK_FASYNC);
1071
1072 release_sock(sk);
1073 return 0;
1074 }
1075
1076 /* This function may be called only under rcu_lock */
1077
sock_wake_async(struct socket_wq * wq,int how,int band)1078 int sock_wake_async(struct socket_wq *wq, int how, int band)
1079 {
1080 if (!wq || !wq->fasync_list)
1081 return -1;
1082
1083 switch (how) {
1084 case SOCK_WAKE_WAITD:
1085 if (test_bit(SOCKWQ_ASYNC_WAITDATA, &wq->flags))
1086 break;
1087 goto call_kill;
1088 case SOCK_WAKE_SPACE:
1089 if (!test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags))
1090 break;
1091 /* fall through */
1092 case SOCK_WAKE_IO:
1093 call_kill:
1094 kill_fasync(&wq->fasync_list, SIGIO, band);
1095 break;
1096 case SOCK_WAKE_URG:
1097 kill_fasync(&wq->fasync_list, SIGURG, band);
1098 }
1099
1100 return 0;
1101 }
1102 EXPORT_SYMBOL(sock_wake_async);
1103
__sock_create(struct net * net,int family,int type,int protocol,struct socket ** res,int kern)1104 int __sock_create(struct net *net, int family, int type, int protocol,
1105 struct socket **res, int kern)
1106 {
1107 int err;
1108 struct socket *sock;
1109 const struct net_proto_family *pf;
1110
1111 /*
1112 * Check protocol is in range
1113 */
1114 if (family < 0 || family >= NPROTO)
1115 return -EAFNOSUPPORT;
1116 if (type < 0 || type >= SOCK_MAX)
1117 return -EINVAL;
1118
1119 /* Compatibility.
1120
1121 This uglymoron is moved from INET layer to here to avoid
1122 deadlock in module load.
1123 */
1124 if (family == PF_INET && type == SOCK_PACKET) {
1125 static int warned;
1126 if (!warned) {
1127 warned = 1;
1128 pr_info("%s uses obsolete (PF_INET,SOCK_PACKET)\n",
1129 current->comm);
1130 }
1131 family = PF_PACKET;
1132 }
1133
1134 err = security_socket_create(family, type, protocol, kern);
1135 if (err)
1136 return err;
1137
1138 /*
1139 * Allocate the socket and allow the family to set things up. if
1140 * the protocol is 0, the family is instructed to select an appropriate
1141 * default.
1142 */
1143 sock = sock_alloc();
1144 if (!sock) {
1145 net_warn_ratelimited("socket: no more sockets\n");
1146 return -ENFILE; /* Not exactly a match, but its the
1147 closest posix thing */
1148 }
1149
1150 sock->type = type;
1151
1152 #ifdef CONFIG_MODULES
1153 /* Attempt to load a protocol module if the find failed.
1154 *
1155 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
1156 * requested real, full-featured networking support upon configuration.
1157 * Otherwise module support will break!
1158 */
1159 if (rcu_access_pointer(net_families[family]) == NULL)
1160 request_module("net-pf-%d", family);
1161 #endif
1162
1163 rcu_read_lock();
1164 pf = rcu_dereference(net_families[family]);
1165 err = -EAFNOSUPPORT;
1166 if (!pf)
1167 goto out_release;
1168
1169 /*
1170 * We will call the ->create function, that possibly is in a loadable
1171 * module, so we have to bump that loadable module refcnt first.
1172 */
1173 if (!try_module_get(pf->owner))
1174 goto out_release;
1175
1176 /* Now protected by module ref count */
1177 rcu_read_unlock();
1178
1179 err = pf->create(net, sock, protocol, kern);
1180 if (err < 0)
1181 goto out_module_put;
1182
1183 /*
1184 * Now to bump the refcnt of the [loadable] module that owns this
1185 * socket at sock_release time we decrement its refcnt.
1186 */
1187 if (!try_module_get(sock->ops->owner))
1188 goto out_module_busy;
1189
1190 /*
1191 * Now that we're done with the ->create function, the [loadable]
1192 * module can have its refcnt decremented
1193 */
1194 module_put(pf->owner);
1195 err = security_socket_post_create(sock, family, type, protocol, kern);
1196 if (err)
1197 goto out_sock_release;
1198 *res = sock;
1199
1200 return 0;
1201
1202 out_module_busy:
1203 err = -EAFNOSUPPORT;
1204 out_module_put:
1205 sock->ops = NULL;
1206 module_put(pf->owner);
1207 out_sock_release:
1208 sock_release(sock);
1209 return err;
1210
1211 out_release:
1212 rcu_read_unlock();
1213 goto out_sock_release;
1214 }
1215 EXPORT_SYMBOL(__sock_create);
1216
sock_create(int family,int type,int protocol,struct socket ** res)1217 int sock_create(int family, int type, int protocol, struct socket **res)
1218 {
1219 return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0);
1220 }
1221 EXPORT_SYMBOL(sock_create);
1222
sock_create_kern(struct net * net,int family,int type,int protocol,struct socket ** res)1223 int sock_create_kern(struct net *net, int family, int type, int protocol, struct socket **res)
1224 {
1225 return __sock_create(net, family, type, protocol, res, 1);
1226 }
1227 EXPORT_SYMBOL(sock_create_kern);
1228
SYSCALL_DEFINE3(socket,int,family,int,type,int,protocol)1229 SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol)
1230 {
1231 int retval;
1232 struct socket *sock;
1233 int flags;
1234
1235 /* Check the SOCK_* constants for consistency. */
1236 BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC);
1237 BUILD_BUG_ON((SOCK_MAX | SOCK_TYPE_MASK) != SOCK_TYPE_MASK);
1238 BUILD_BUG_ON(SOCK_CLOEXEC & SOCK_TYPE_MASK);
1239 BUILD_BUG_ON(SOCK_NONBLOCK & SOCK_TYPE_MASK);
1240
1241 flags = type & ~SOCK_TYPE_MASK;
1242 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1243 return -EINVAL;
1244 type &= SOCK_TYPE_MASK;
1245
1246 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1247 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1248
1249 retval = sock_create(family, type, protocol, &sock);
1250 if (retval < 0)
1251 goto out;
1252
1253 retval = sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK));
1254 if (retval < 0)
1255 goto out_release;
1256
1257 out:
1258 /* It may be already another descriptor 8) Not kernel problem. */
1259 return retval;
1260
1261 out_release:
1262 sock_release(sock);
1263 return retval;
1264 }
1265
1266 /*
1267 * Create a pair of connected sockets.
1268 */
1269
SYSCALL_DEFINE4(socketpair,int,family,int,type,int,protocol,int __user *,usockvec)1270 SYSCALL_DEFINE4(socketpair, int, family, int, type, int, protocol,
1271 int __user *, usockvec)
1272 {
1273 struct socket *sock1, *sock2;
1274 int fd1, fd2, err;
1275 struct file *newfile1, *newfile2;
1276 int flags;
1277
1278 flags = type & ~SOCK_TYPE_MASK;
1279 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1280 return -EINVAL;
1281 type &= SOCK_TYPE_MASK;
1282
1283 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1284 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1285
1286 /*
1287 * Obtain the first socket and check if the underlying protocol
1288 * supports the socketpair call.
1289 */
1290
1291 err = sock_create(family, type, protocol, &sock1);
1292 if (err < 0)
1293 goto out;
1294
1295 err = sock_create(family, type, protocol, &sock2);
1296 if (err < 0)
1297 goto out_release_1;
1298
1299 err = sock1->ops->socketpair(sock1, sock2);
1300 if (err < 0)
1301 goto out_release_both;
1302
1303 fd1 = get_unused_fd_flags(flags);
1304 if (unlikely(fd1 < 0)) {
1305 err = fd1;
1306 goto out_release_both;
1307 }
1308
1309 fd2 = get_unused_fd_flags(flags);
1310 if (unlikely(fd2 < 0)) {
1311 err = fd2;
1312 goto out_put_unused_1;
1313 }
1314
1315 newfile1 = sock_alloc_file(sock1, flags, NULL);
1316 if (IS_ERR(newfile1)) {
1317 err = PTR_ERR(newfile1);
1318 goto out_put_unused_both;
1319 }
1320
1321 newfile2 = sock_alloc_file(sock2, flags, NULL);
1322 if (IS_ERR(newfile2)) {
1323 err = PTR_ERR(newfile2);
1324 goto out_fput_1;
1325 }
1326
1327 err = put_user(fd1, &usockvec[0]);
1328 if (err)
1329 goto out_fput_both;
1330
1331 err = put_user(fd2, &usockvec[1]);
1332 if (err)
1333 goto out_fput_both;
1334
1335 audit_fd_pair(fd1, fd2);
1336
1337 fd_install(fd1, newfile1);
1338 fd_install(fd2, newfile2);
1339 /* fd1 and fd2 may be already another descriptors.
1340 * Not kernel problem.
1341 */
1342
1343 return 0;
1344
1345 out_fput_both:
1346 fput(newfile2);
1347 fput(newfile1);
1348 put_unused_fd(fd2);
1349 put_unused_fd(fd1);
1350 goto out;
1351
1352 out_fput_1:
1353 fput(newfile1);
1354 put_unused_fd(fd2);
1355 put_unused_fd(fd1);
1356 sock_release(sock2);
1357 goto out;
1358
1359 out_put_unused_both:
1360 put_unused_fd(fd2);
1361 out_put_unused_1:
1362 put_unused_fd(fd1);
1363 out_release_both:
1364 sock_release(sock2);
1365 out_release_1:
1366 sock_release(sock1);
1367 out:
1368 return err;
1369 }
1370
1371 /*
1372 * Bind a name to a socket. Nothing much to do here since it's
1373 * the protocol's responsibility to handle the local address.
1374 *
1375 * We move the socket address to kernel space before we call
1376 * the protocol layer (having also checked the address is ok).
1377 */
1378
SYSCALL_DEFINE3(bind,int,fd,struct sockaddr __user *,umyaddr,int,addrlen)1379 SYSCALL_DEFINE3(bind, int, fd, struct sockaddr __user *, umyaddr, int, addrlen)
1380 {
1381 struct socket *sock;
1382 struct sockaddr_storage address;
1383 int err, fput_needed;
1384
1385 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1386 if (sock) {
1387 err = move_addr_to_kernel(umyaddr, addrlen, &address);
1388 if (err >= 0) {
1389 err = security_socket_bind(sock,
1390 (struct sockaddr *)&address,
1391 addrlen);
1392 if (!err)
1393 err = sock->ops->bind(sock,
1394 (struct sockaddr *)
1395 &address, addrlen);
1396 }
1397 fput_light(sock->file, fput_needed);
1398 }
1399 return err;
1400 }
1401
1402 /*
1403 * Perform a listen. Basically, we allow the protocol to do anything
1404 * necessary for a listen, and if that works, we mark the socket as
1405 * ready for listening.
1406 */
1407
SYSCALL_DEFINE2(listen,int,fd,int,backlog)1408 SYSCALL_DEFINE2(listen, int, fd, int, backlog)
1409 {
1410 struct socket *sock;
1411 int err, fput_needed;
1412 int somaxconn;
1413
1414 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1415 if (sock) {
1416 somaxconn = sock_net(sock->sk)->core.sysctl_somaxconn;
1417 if ((unsigned int)backlog > somaxconn)
1418 backlog = somaxconn;
1419
1420 err = security_socket_listen(sock, backlog);
1421 if (!err)
1422 err = sock->ops->listen(sock, backlog);
1423
1424 fput_light(sock->file, fput_needed);
1425 }
1426 return err;
1427 }
1428
1429 /*
1430 * For accept, we attempt to create a new socket, set up the link
1431 * with the client, wake up the client, then return the new
1432 * connected fd. We collect the address of the connector in kernel
1433 * space and move it to user at the very end. This is unclean because
1434 * we open the socket then return an error.
1435 *
1436 * 1003.1g adds the ability to recvmsg() to query connection pending
1437 * status to recvmsg. We need to add that support in a way thats
1438 * clean when we restucture accept also.
1439 */
1440
SYSCALL_DEFINE4(accept4,int,fd,struct sockaddr __user *,upeer_sockaddr,int __user *,upeer_addrlen,int,flags)1441 SYSCALL_DEFINE4(accept4, int, fd, struct sockaddr __user *, upeer_sockaddr,
1442 int __user *, upeer_addrlen, int, flags)
1443 {
1444 struct socket *sock, *newsock;
1445 struct file *newfile;
1446 int err, len, newfd, fput_needed;
1447 struct sockaddr_storage address;
1448
1449 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1450 return -EINVAL;
1451
1452 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1453 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1454
1455 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1456 if (!sock)
1457 goto out;
1458
1459 err = -ENFILE;
1460 newsock = sock_alloc();
1461 if (!newsock)
1462 goto out_put;
1463
1464 newsock->type = sock->type;
1465 newsock->ops = sock->ops;
1466
1467 /*
1468 * We don't need try_module_get here, as the listening socket (sock)
1469 * has the protocol module (sock->ops->owner) held.
1470 */
1471 __module_get(newsock->ops->owner);
1472
1473 newfd = get_unused_fd_flags(flags);
1474 if (unlikely(newfd < 0)) {
1475 err = newfd;
1476 sock_release(newsock);
1477 goto out_put;
1478 }
1479 newfile = sock_alloc_file(newsock, flags, sock->sk->sk_prot_creator->name);
1480 if (IS_ERR(newfile)) {
1481 err = PTR_ERR(newfile);
1482 put_unused_fd(newfd);
1483 sock_release(newsock);
1484 goto out_put;
1485 }
1486
1487 err = security_socket_accept(sock, newsock);
1488 if (err)
1489 goto out_fd;
1490
1491 err = sock->ops->accept(sock, newsock, sock->file->f_flags);
1492 if (err < 0)
1493 goto out_fd;
1494
1495 if (upeer_sockaddr) {
1496 if (newsock->ops->getname(newsock, (struct sockaddr *)&address,
1497 &len, 2) < 0) {
1498 err = -ECONNABORTED;
1499 goto out_fd;
1500 }
1501 err = move_addr_to_user(&address,
1502 len, upeer_sockaddr, upeer_addrlen);
1503 if (err < 0)
1504 goto out_fd;
1505 }
1506
1507 /* File flags are not inherited via accept() unlike another OSes. */
1508
1509 fd_install(newfd, newfile);
1510 err = newfd;
1511
1512 out_put:
1513 fput_light(sock->file, fput_needed);
1514 out:
1515 return err;
1516 out_fd:
1517 fput(newfile);
1518 put_unused_fd(newfd);
1519 goto out_put;
1520 }
1521
SYSCALL_DEFINE3(accept,int,fd,struct sockaddr __user *,upeer_sockaddr,int __user *,upeer_addrlen)1522 SYSCALL_DEFINE3(accept, int, fd, struct sockaddr __user *, upeer_sockaddr,
1523 int __user *, upeer_addrlen)
1524 {
1525 return sys_accept4(fd, upeer_sockaddr, upeer_addrlen, 0);
1526 }
1527
1528 /*
1529 * Attempt to connect to a socket with the server address. The address
1530 * is in user space so we verify it is OK and move it to kernel space.
1531 *
1532 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
1533 * break bindings
1534 *
1535 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
1536 * other SEQPACKET protocols that take time to connect() as it doesn't
1537 * include the -EINPROGRESS status for such sockets.
1538 */
1539
SYSCALL_DEFINE3(connect,int,fd,struct sockaddr __user *,uservaddr,int,addrlen)1540 SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
1541 int, addrlen)
1542 {
1543 struct socket *sock;
1544 struct sockaddr_storage address;
1545 int err, fput_needed;
1546
1547 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1548 if (!sock)
1549 goto out;
1550 err = move_addr_to_kernel(uservaddr, addrlen, &address);
1551 if (err < 0)
1552 goto out_put;
1553
1554 err =
1555 security_socket_connect(sock, (struct sockaddr *)&address, addrlen);
1556 if (err)
1557 goto out_put;
1558
1559 err = sock->ops->connect(sock, (struct sockaddr *)&address, addrlen,
1560 sock->file->f_flags);
1561 out_put:
1562 fput_light(sock->file, fput_needed);
1563 out:
1564 return err;
1565 }
1566
1567 /*
1568 * Get the local address ('name') of a socket object. Move the obtained
1569 * name to user space.
1570 */
1571
SYSCALL_DEFINE3(getsockname,int,fd,struct sockaddr __user *,usockaddr,int __user *,usockaddr_len)1572 SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr,
1573 int __user *, usockaddr_len)
1574 {
1575 struct socket *sock;
1576 struct sockaddr_storage address;
1577 int len, err, fput_needed;
1578
1579 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1580 if (!sock)
1581 goto out;
1582
1583 err = security_socket_getsockname(sock);
1584 if (err)
1585 goto out_put;
1586
1587 err = sock->ops->getname(sock, (struct sockaddr *)&address, &len, 0);
1588 if (err)
1589 goto out_put;
1590 err = move_addr_to_user(&address, len, usockaddr, usockaddr_len);
1591
1592 out_put:
1593 fput_light(sock->file, fput_needed);
1594 out:
1595 return err;
1596 }
1597
1598 /*
1599 * Get the remote address ('name') of a socket object. Move the obtained
1600 * name to user space.
1601 */
1602
SYSCALL_DEFINE3(getpeername,int,fd,struct sockaddr __user *,usockaddr,int __user *,usockaddr_len)1603 SYSCALL_DEFINE3(getpeername, int, fd, struct sockaddr __user *, usockaddr,
1604 int __user *, usockaddr_len)
1605 {
1606 struct socket *sock;
1607 struct sockaddr_storage address;
1608 int len, err, fput_needed;
1609
1610 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1611 if (sock != NULL) {
1612 err = security_socket_getpeername(sock);
1613 if (err) {
1614 fput_light(sock->file, fput_needed);
1615 return err;
1616 }
1617
1618 err =
1619 sock->ops->getname(sock, (struct sockaddr *)&address, &len,
1620 1);
1621 if (!err)
1622 err = move_addr_to_user(&address, len, usockaddr,
1623 usockaddr_len);
1624 fput_light(sock->file, fput_needed);
1625 }
1626 return err;
1627 }
1628
1629 /*
1630 * Send a datagram to a given address. We move the address into kernel
1631 * space and check the user space data area is readable before invoking
1632 * the protocol.
1633 */
1634
SYSCALL_DEFINE6(sendto,int,fd,void __user *,buff,size_t,len,unsigned int,flags,struct sockaddr __user *,addr,int,addr_len)1635 SYSCALL_DEFINE6(sendto, int, fd, void __user *, buff, size_t, len,
1636 unsigned int, flags, struct sockaddr __user *, addr,
1637 int, addr_len)
1638 {
1639 struct socket *sock;
1640 struct sockaddr_storage address;
1641 int err;
1642 struct msghdr msg;
1643 struct iovec iov;
1644 int fput_needed;
1645
1646 err = import_single_range(WRITE, buff, len, &iov, &msg.msg_iter);
1647 if (unlikely(err))
1648 return err;
1649 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1650 if (!sock)
1651 goto out;
1652
1653 msg.msg_name = NULL;
1654 msg.msg_control = NULL;
1655 msg.msg_controllen = 0;
1656 msg.msg_namelen = 0;
1657 if (addr) {
1658 err = move_addr_to_kernel(addr, addr_len, &address);
1659 if (err < 0)
1660 goto out_put;
1661 msg.msg_name = (struct sockaddr *)&address;
1662 msg.msg_namelen = addr_len;
1663 }
1664 if (sock->file->f_flags & O_NONBLOCK)
1665 flags |= MSG_DONTWAIT;
1666 msg.msg_flags = flags;
1667 err = sock_sendmsg(sock, &msg);
1668
1669 out_put:
1670 fput_light(sock->file, fput_needed);
1671 out:
1672 return err;
1673 }
1674
1675 /*
1676 * Send a datagram down a socket.
1677 */
1678
SYSCALL_DEFINE4(send,int,fd,void __user *,buff,size_t,len,unsigned int,flags)1679 SYSCALL_DEFINE4(send, int, fd, void __user *, buff, size_t, len,
1680 unsigned int, flags)
1681 {
1682 return sys_sendto(fd, buff, len, flags, NULL, 0);
1683 }
1684
1685 /*
1686 * Receive a frame from the socket and optionally record the address of the
1687 * sender. We verify the buffers are writable and if needed move the
1688 * sender address from kernel to user space.
1689 */
1690
SYSCALL_DEFINE6(recvfrom,int,fd,void __user *,ubuf,size_t,size,unsigned int,flags,struct sockaddr __user *,addr,int __user *,addr_len)1691 SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, ubuf, size_t, size,
1692 unsigned int, flags, struct sockaddr __user *, addr,
1693 int __user *, addr_len)
1694 {
1695 struct socket *sock;
1696 struct iovec iov;
1697 struct msghdr msg;
1698 struct sockaddr_storage address;
1699 int err, err2;
1700 int fput_needed;
1701
1702 err = import_single_range(READ, ubuf, size, &iov, &msg.msg_iter);
1703 if (unlikely(err))
1704 return err;
1705 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1706 if (!sock)
1707 goto out;
1708
1709 msg.msg_control = NULL;
1710 msg.msg_controllen = 0;
1711 /* Save some cycles and don't copy the address if not needed */
1712 msg.msg_name = addr ? (struct sockaddr *)&address : NULL;
1713 /* We assume all kernel code knows the size of sockaddr_storage */
1714 msg.msg_namelen = 0;
1715 msg.msg_iocb = NULL;
1716 msg.msg_flags = 0;
1717 if (sock->file->f_flags & O_NONBLOCK)
1718 flags |= MSG_DONTWAIT;
1719 err = sock_recvmsg(sock, &msg, iov_iter_count(&msg.msg_iter), flags);
1720
1721 if (err >= 0 && addr != NULL) {
1722 err2 = move_addr_to_user(&address,
1723 msg.msg_namelen, addr, addr_len);
1724 if (err2 < 0)
1725 err = err2;
1726 }
1727
1728 fput_light(sock->file, fput_needed);
1729 out:
1730 return err;
1731 }
1732
1733 /*
1734 * Receive a datagram from a socket.
1735 */
1736
SYSCALL_DEFINE4(recv,int,fd,void __user *,ubuf,size_t,size,unsigned int,flags)1737 SYSCALL_DEFINE4(recv, int, fd, void __user *, ubuf, size_t, size,
1738 unsigned int, flags)
1739 {
1740 return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
1741 }
1742
1743 /*
1744 * Set a socket option. Because we don't know the option lengths we have
1745 * to pass the user mode parameter for the protocols to sort out.
1746 */
1747
SYSCALL_DEFINE5(setsockopt,int,fd,int,level,int,optname,char __user *,optval,int,optlen)1748 SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,
1749 char __user *, optval, int, optlen)
1750 {
1751 int err, fput_needed;
1752 struct socket *sock;
1753
1754 if (optlen < 0)
1755 return -EINVAL;
1756
1757 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1758 if (sock != NULL) {
1759 err = security_socket_setsockopt(sock, level, optname);
1760 if (err)
1761 goto out_put;
1762
1763 if (level == SOL_SOCKET)
1764 err =
1765 sock_setsockopt(sock, level, optname, optval,
1766 optlen);
1767 else
1768 err =
1769 sock->ops->setsockopt(sock, level, optname, optval,
1770 optlen);
1771 out_put:
1772 fput_light(sock->file, fput_needed);
1773 }
1774 return err;
1775 }
1776
1777 /*
1778 * Get a socket option. Because we don't know the option lengths we have
1779 * to pass a user mode parameter for the protocols to sort out.
1780 */
1781
SYSCALL_DEFINE5(getsockopt,int,fd,int,level,int,optname,char __user *,optval,int __user *,optlen)1782 SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname,
1783 char __user *, optval, int __user *, optlen)
1784 {
1785 int err, fput_needed;
1786 struct socket *sock;
1787
1788 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1789 if (sock != NULL) {
1790 err = security_socket_getsockopt(sock, level, optname);
1791 if (err)
1792 goto out_put;
1793
1794 if (level == SOL_SOCKET)
1795 err =
1796 sock_getsockopt(sock, level, optname, optval,
1797 optlen);
1798 else
1799 err =
1800 sock->ops->getsockopt(sock, level, optname, optval,
1801 optlen);
1802 out_put:
1803 fput_light(sock->file, fput_needed);
1804 }
1805 return err;
1806 }
1807
1808 /*
1809 * Shutdown a socket.
1810 */
1811
SYSCALL_DEFINE2(shutdown,int,fd,int,how)1812 SYSCALL_DEFINE2(shutdown, int, fd, int, how)
1813 {
1814 int err, fput_needed;
1815 struct socket *sock;
1816
1817 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1818 if (sock != NULL) {
1819 err = security_socket_shutdown(sock, how);
1820 if (!err)
1821 err = sock->ops->shutdown(sock, how);
1822 fput_light(sock->file, fput_needed);
1823 }
1824 return err;
1825 }
1826
1827 /* A couple of helpful macros for getting the address of the 32/64 bit
1828 * fields which are the same type (int / unsigned) on our platforms.
1829 */
1830 #define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
1831 #define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
1832 #define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
1833
1834 struct used_address {
1835 struct sockaddr_storage name;
1836 unsigned int name_len;
1837 };
1838
copy_msghdr_from_user(struct msghdr * kmsg,struct user_msghdr __user * umsg,struct sockaddr __user ** save_addr,struct iovec ** iov)1839 static int copy_msghdr_from_user(struct msghdr *kmsg,
1840 struct user_msghdr __user *umsg,
1841 struct sockaddr __user **save_addr,
1842 struct iovec **iov)
1843 {
1844 struct sockaddr __user *uaddr;
1845 struct iovec __user *uiov;
1846 size_t nr_segs;
1847 ssize_t err;
1848
1849 if (!access_ok(VERIFY_READ, umsg, sizeof(*umsg)) ||
1850 __get_user(uaddr, &umsg->msg_name) ||
1851 __get_user(kmsg->msg_namelen, &umsg->msg_namelen) ||
1852 __get_user(uiov, &umsg->msg_iov) ||
1853 __get_user(nr_segs, &umsg->msg_iovlen) ||
1854 __get_user(kmsg->msg_control, &umsg->msg_control) ||
1855 __get_user(kmsg->msg_controllen, &umsg->msg_controllen) ||
1856 __get_user(kmsg->msg_flags, &umsg->msg_flags))
1857 return -EFAULT;
1858
1859 if (!uaddr)
1860 kmsg->msg_namelen = 0;
1861
1862 if (kmsg->msg_namelen < 0)
1863 return -EINVAL;
1864
1865 if (kmsg->msg_namelen > sizeof(struct sockaddr_storage))
1866 kmsg->msg_namelen = sizeof(struct sockaddr_storage);
1867
1868 if (save_addr)
1869 *save_addr = uaddr;
1870
1871 if (uaddr && kmsg->msg_namelen) {
1872 if (!save_addr) {
1873 err = move_addr_to_kernel(uaddr, kmsg->msg_namelen,
1874 kmsg->msg_name);
1875 if (err < 0)
1876 return err;
1877 }
1878 } else {
1879 kmsg->msg_name = NULL;
1880 kmsg->msg_namelen = 0;
1881 }
1882
1883 if (nr_segs > UIO_MAXIOV)
1884 return -EMSGSIZE;
1885
1886 kmsg->msg_iocb = NULL;
1887
1888 return import_iovec(save_addr ? READ : WRITE, uiov, nr_segs,
1889 UIO_FASTIOV, iov, &kmsg->msg_iter);
1890 }
1891
___sys_sendmsg(struct socket * sock,struct user_msghdr __user * msg,struct msghdr * msg_sys,unsigned int flags,struct used_address * used_address)1892 static int ___sys_sendmsg(struct socket *sock, struct user_msghdr __user *msg,
1893 struct msghdr *msg_sys, unsigned int flags,
1894 struct used_address *used_address)
1895 {
1896 struct compat_msghdr __user *msg_compat =
1897 (struct compat_msghdr __user *)msg;
1898 struct sockaddr_storage address;
1899 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
1900 unsigned char ctl[sizeof(struct cmsghdr) + 20]
1901 __attribute__ ((aligned(sizeof(__kernel_size_t))));
1902 /* 20 is size of ipv6_pktinfo */
1903 unsigned char *ctl_buf = ctl;
1904 int ctl_len;
1905 ssize_t err;
1906
1907 msg_sys->msg_name = &address;
1908
1909 if (MSG_CMSG_COMPAT & flags)
1910 err = get_compat_msghdr(msg_sys, msg_compat, NULL, &iov);
1911 else
1912 err = copy_msghdr_from_user(msg_sys, msg, NULL, &iov);
1913 if (err < 0)
1914 return err;
1915
1916 err = -ENOBUFS;
1917
1918 if (msg_sys->msg_controllen > INT_MAX)
1919 goto out_freeiov;
1920 ctl_len = msg_sys->msg_controllen;
1921 if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
1922 err =
1923 cmsghdr_from_user_compat_to_kern(msg_sys, sock->sk, ctl,
1924 sizeof(ctl));
1925 if (err)
1926 goto out_freeiov;
1927 ctl_buf = msg_sys->msg_control;
1928 ctl_len = msg_sys->msg_controllen;
1929 } else if (ctl_len) {
1930 if (ctl_len > sizeof(ctl)) {
1931 ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
1932 if (ctl_buf == NULL)
1933 goto out_freeiov;
1934 }
1935 err = -EFAULT;
1936 /*
1937 * Careful! Before this, msg_sys->msg_control contains a user pointer.
1938 * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
1939 * checking falls down on this.
1940 */
1941 if (copy_from_user(ctl_buf,
1942 (void __user __force *)msg_sys->msg_control,
1943 ctl_len))
1944 goto out_freectl;
1945 msg_sys->msg_control = ctl_buf;
1946 }
1947 msg_sys->msg_flags = flags;
1948
1949 if (sock->file->f_flags & O_NONBLOCK)
1950 msg_sys->msg_flags |= MSG_DONTWAIT;
1951 /*
1952 * If this is sendmmsg() and current destination address is same as
1953 * previously succeeded address, omit asking LSM's decision.
1954 * used_address->name_len is initialized to UINT_MAX so that the first
1955 * destination address never matches.
1956 */
1957 if (used_address && msg_sys->msg_name &&
1958 used_address->name_len == msg_sys->msg_namelen &&
1959 !memcmp(&used_address->name, msg_sys->msg_name,
1960 used_address->name_len)) {
1961 err = sock_sendmsg_nosec(sock, msg_sys);
1962 goto out_freectl;
1963 }
1964 err = sock_sendmsg(sock, msg_sys);
1965 /*
1966 * If this is sendmmsg() and sending to current destination address was
1967 * successful, remember it.
1968 */
1969 if (used_address && err >= 0) {
1970 used_address->name_len = msg_sys->msg_namelen;
1971 if (msg_sys->msg_name)
1972 memcpy(&used_address->name, msg_sys->msg_name,
1973 used_address->name_len);
1974 }
1975
1976 out_freectl:
1977 if (ctl_buf != ctl)
1978 sock_kfree_s(sock->sk, ctl_buf, ctl_len);
1979 out_freeiov:
1980 kfree(iov);
1981 return err;
1982 }
1983
1984 /*
1985 * BSD sendmsg interface
1986 */
1987
__sys_sendmsg(int fd,struct user_msghdr __user * msg,unsigned flags)1988 long __sys_sendmsg(int fd, struct user_msghdr __user *msg, unsigned flags)
1989 {
1990 int fput_needed, err;
1991 struct msghdr msg_sys;
1992 struct socket *sock;
1993
1994 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1995 if (!sock)
1996 goto out;
1997
1998 err = ___sys_sendmsg(sock, msg, &msg_sys, flags, NULL);
1999
2000 fput_light(sock->file, fput_needed);
2001 out:
2002 return err;
2003 }
2004
SYSCALL_DEFINE3(sendmsg,int,fd,struct user_msghdr __user *,msg,unsigned int,flags)2005 SYSCALL_DEFINE3(sendmsg, int, fd, struct user_msghdr __user *, msg, unsigned int, flags)
2006 {
2007 if (flags & MSG_CMSG_COMPAT)
2008 return -EINVAL;
2009 return __sys_sendmsg(fd, msg, flags);
2010 }
2011
2012 /*
2013 * Linux sendmmsg interface
2014 */
2015
__sys_sendmmsg(int fd,struct mmsghdr __user * mmsg,unsigned int vlen,unsigned int flags)2016 int __sys_sendmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
2017 unsigned int flags)
2018 {
2019 int fput_needed, err, datagrams;
2020 struct socket *sock;
2021 struct mmsghdr __user *entry;
2022 struct compat_mmsghdr __user *compat_entry;
2023 struct msghdr msg_sys;
2024 struct used_address used_address;
2025
2026 if (vlen > UIO_MAXIOV)
2027 vlen = UIO_MAXIOV;
2028
2029 datagrams = 0;
2030
2031 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2032 if (!sock)
2033 return err;
2034
2035 used_address.name_len = UINT_MAX;
2036 entry = mmsg;
2037 compat_entry = (struct compat_mmsghdr __user *)mmsg;
2038 err = 0;
2039
2040 while (datagrams < vlen) {
2041 if (MSG_CMSG_COMPAT & flags) {
2042 err = ___sys_sendmsg(sock, (struct user_msghdr __user *)compat_entry,
2043 &msg_sys, flags, &used_address);
2044 if (err < 0)
2045 break;
2046 err = __put_user(err, &compat_entry->msg_len);
2047 ++compat_entry;
2048 } else {
2049 err = ___sys_sendmsg(sock,
2050 (struct user_msghdr __user *)entry,
2051 &msg_sys, flags, &used_address);
2052 if (err < 0)
2053 break;
2054 err = put_user(err, &entry->msg_len);
2055 ++entry;
2056 }
2057
2058 if (err)
2059 break;
2060 ++datagrams;
2061 if (msg_data_left(&msg_sys))
2062 break;
2063 }
2064
2065 fput_light(sock->file, fput_needed);
2066
2067 /* We only return an error if no datagrams were able to be sent */
2068 if (datagrams != 0)
2069 return datagrams;
2070
2071 return err;
2072 }
2073
SYSCALL_DEFINE4(sendmmsg,int,fd,struct mmsghdr __user *,mmsg,unsigned int,vlen,unsigned int,flags)2074 SYSCALL_DEFINE4(sendmmsg, int, fd, struct mmsghdr __user *, mmsg,
2075 unsigned int, vlen, unsigned int, flags)
2076 {
2077 if (flags & MSG_CMSG_COMPAT)
2078 return -EINVAL;
2079 return __sys_sendmmsg(fd, mmsg, vlen, flags);
2080 }
2081
___sys_recvmsg(struct socket * sock,struct user_msghdr __user * msg,struct msghdr * msg_sys,unsigned int flags,int nosec)2082 static int ___sys_recvmsg(struct socket *sock, struct user_msghdr __user *msg,
2083 struct msghdr *msg_sys, unsigned int flags, int nosec)
2084 {
2085 struct compat_msghdr __user *msg_compat =
2086 (struct compat_msghdr __user *)msg;
2087 struct iovec iovstack[UIO_FASTIOV];
2088 struct iovec *iov = iovstack;
2089 unsigned long cmsg_ptr;
2090 int total_len, len;
2091 ssize_t err;
2092
2093 /* kernel mode address */
2094 struct sockaddr_storage addr;
2095
2096 /* user mode address pointers */
2097 struct sockaddr __user *uaddr;
2098 int __user *uaddr_len = COMPAT_NAMELEN(msg);
2099
2100 msg_sys->msg_name = &addr;
2101
2102 if (MSG_CMSG_COMPAT & flags)
2103 err = get_compat_msghdr(msg_sys, msg_compat, &uaddr, &iov);
2104 else
2105 err = copy_msghdr_from_user(msg_sys, msg, &uaddr, &iov);
2106 if (err < 0)
2107 return err;
2108 total_len = iov_iter_count(&msg_sys->msg_iter);
2109
2110 cmsg_ptr = (unsigned long)msg_sys->msg_control;
2111 msg_sys->msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
2112
2113 /* We assume all kernel code knows the size of sockaddr_storage */
2114 msg_sys->msg_namelen = 0;
2115
2116 if (sock->file->f_flags & O_NONBLOCK)
2117 flags |= MSG_DONTWAIT;
2118 err = (nosec ? sock_recvmsg_nosec : sock_recvmsg)(sock, msg_sys,
2119 total_len, flags);
2120 if (err < 0)
2121 goto out_freeiov;
2122 len = err;
2123
2124 if (uaddr != NULL) {
2125 err = move_addr_to_user(&addr,
2126 msg_sys->msg_namelen, uaddr,
2127 uaddr_len);
2128 if (err < 0)
2129 goto out_freeiov;
2130 }
2131 err = __put_user((msg_sys->msg_flags & ~MSG_CMSG_COMPAT),
2132 COMPAT_FLAGS(msg));
2133 if (err)
2134 goto out_freeiov;
2135 if (MSG_CMSG_COMPAT & flags)
2136 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
2137 &msg_compat->msg_controllen);
2138 else
2139 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
2140 &msg->msg_controllen);
2141 if (err)
2142 goto out_freeiov;
2143 err = len;
2144
2145 out_freeiov:
2146 kfree(iov);
2147 return err;
2148 }
2149
2150 /*
2151 * BSD recvmsg interface
2152 */
2153
__sys_recvmsg(int fd,struct user_msghdr __user * msg,unsigned flags)2154 long __sys_recvmsg(int fd, struct user_msghdr __user *msg, unsigned flags)
2155 {
2156 int fput_needed, err;
2157 struct msghdr msg_sys;
2158 struct socket *sock;
2159
2160 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2161 if (!sock)
2162 goto out;
2163
2164 err = ___sys_recvmsg(sock, msg, &msg_sys, flags, 0);
2165
2166 fput_light(sock->file, fput_needed);
2167 out:
2168 return err;
2169 }
2170
SYSCALL_DEFINE3(recvmsg,int,fd,struct user_msghdr __user *,msg,unsigned int,flags)2171 SYSCALL_DEFINE3(recvmsg, int, fd, struct user_msghdr __user *, msg,
2172 unsigned int, flags)
2173 {
2174 if (flags & MSG_CMSG_COMPAT)
2175 return -EINVAL;
2176 return __sys_recvmsg(fd, msg, flags);
2177 }
2178
2179 /*
2180 * Linux recvmmsg interface
2181 */
2182
__sys_recvmmsg(int fd,struct mmsghdr __user * mmsg,unsigned int vlen,unsigned int flags,struct timespec * timeout)2183 int __sys_recvmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
2184 unsigned int flags, struct timespec *timeout)
2185 {
2186 int fput_needed, err, datagrams;
2187 struct socket *sock;
2188 struct mmsghdr __user *entry;
2189 struct compat_mmsghdr __user *compat_entry;
2190 struct msghdr msg_sys;
2191 struct timespec end_time;
2192
2193 if (timeout &&
2194 poll_select_set_timeout(&end_time, timeout->tv_sec,
2195 timeout->tv_nsec))
2196 return -EINVAL;
2197
2198 datagrams = 0;
2199
2200 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2201 if (!sock)
2202 return err;
2203
2204 err = sock_error(sock->sk);
2205 if (err) {
2206 datagrams = err;
2207 goto out_put;
2208 }
2209
2210 entry = mmsg;
2211 compat_entry = (struct compat_mmsghdr __user *)mmsg;
2212
2213 while (datagrams < vlen) {
2214 /*
2215 * No need to ask LSM for more than the first datagram.
2216 */
2217 if (MSG_CMSG_COMPAT & flags) {
2218 err = ___sys_recvmsg(sock, (struct user_msghdr __user *)compat_entry,
2219 &msg_sys, flags & ~MSG_WAITFORONE,
2220 datagrams);
2221 if (err < 0)
2222 break;
2223 err = __put_user(err, &compat_entry->msg_len);
2224 ++compat_entry;
2225 } else {
2226 err = ___sys_recvmsg(sock,
2227 (struct user_msghdr __user *)entry,
2228 &msg_sys, flags & ~MSG_WAITFORONE,
2229 datagrams);
2230 if (err < 0)
2231 break;
2232 err = put_user(err, &entry->msg_len);
2233 ++entry;
2234 }
2235
2236 if (err)
2237 break;
2238 ++datagrams;
2239
2240 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
2241 if (flags & MSG_WAITFORONE)
2242 flags |= MSG_DONTWAIT;
2243
2244 if (timeout) {
2245 ktime_get_ts(timeout);
2246 *timeout = timespec_sub(end_time, *timeout);
2247 if (timeout->tv_sec < 0) {
2248 timeout->tv_sec = timeout->tv_nsec = 0;
2249 break;
2250 }
2251
2252 /* Timeout, return less than vlen datagrams */
2253 if (timeout->tv_nsec == 0 && timeout->tv_sec == 0)
2254 break;
2255 }
2256
2257 /* Out of band data, return right away */
2258 if (msg_sys.msg_flags & MSG_OOB)
2259 break;
2260 }
2261
2262 if (err == 0)
2263 goto out_put;
2264
2265 if (datagrams == 0) {
2266 datagrams = err;
2267 goto out_put;
2268 }
2269
2270 /*
2271 * We may return less entries than requested (vlen) if the
2272 * sock is non block and there aren't enough datagrams...
2273 */
2274 if (err != -EAGAIN) {
2275 /*
2276 * ... or if recvmsg returns an error after we
2277 * received some datagrams, where we record the
2278 * error to return on the next call or if the
2279 * app asks about it using getsockopt(SO_ERROR).
2280 */
2281 sock->sk->sk_err = -err;
2282 }
2283 out_put:
2284 fput_light(sock->file, fput_needed);
2285
2286 return datagrams;
2287 }
2288
SYSCALL_DEFINE5(recvmmsg,int,fd,struct mmsghdr __user *,mmsg,unsigned int,vlen,unsigned int,flags,struct timespec __user *,timeout)2289 SYSCALL_DEFINE5(recvmmsg, int, fd, struct mmsghdr __user *, mmsg,
2290 unsigned int, vlen, unsigned int, flags,
2291 struct timespec __user *, timeout)
2292 {
2293 int datagrams;
2294 struct timespec timeout_sys;
2295
2296 if (flags & MSG_CMSG_COMPAT)
2297 return -EINVAL;
2298
2299 if (!timeout)
2300 return __sys_recvmmsg(fd, mmsg, vlen, flags, NULL);
2301
2302 if (copy_from_user(&timeout_sys, timeout, sizeof(timeout_sys)))
2303 return -EFAULT;
2304
2305 datagrams = __sys_recvmmsg(fd, mmsg, vlen, flags, &timeout_sys);
2306
2307 if (datagrams > 0 &&
2308 copy_to_user(timeout, &timeout_sys, sizeof(timeout_sys)))
2309 datagrams = -EFAULT;
2310
2311 return datagrams;
2312 }
2313
2314 #ifdef __ARCH_WANT_SYS_SOCKETCALL
2315 /* Argument list sizes for sys_socketcall */
2316 #define AL(x) ((x) * sizeof(unsigned long))
2317 static const unsigned char nargs[21] = {
2318 AL(0), AL(3), AL(3), AL(3), AL(2), AL(3),
2319 AL(3), AL(3), AL(4), AL(4), AL(4), AL(6),
2320 AL(6), AL(2), AL(5), AL(5), AL(3), AL(3),
2321 AL(4), AL(5), AL(4)
2322 };
2323
2324 #undef AL
2325
2326 /*
2327 * System call vectors.
2328 *
2329 * Argument checking cleaned up. Saved 20% in size.
2330 * This function doesn't need to set the kernel lock because
2331 * it is set by the callees.
2332 */
2333
SYSCALL_DEFINE2(socketcall,int,call,unsigned long __user *,args)2334 SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args)
2335 {
2336 unsigned long a[AUDITSC_ARGS];
2337 unsigned long a0, a1;
2338 int err;
2339 unsigned int len;
2340
2341 if (call < 1 || call > SYS_SENDMMSG)
2342 return -EINVAL;
2343 call = array_index_nospec(call, SYS_SENDMMSG + 1);
2344
2345 len = nargs[call];
2346 if (len > sizeof(a))
2347 return -EINVAL;
2348
2349 /* copy_from_user should be SMP safe. */
2350 if (copy_from_user(a, args, len))
2351 return -EFAULT;
2352
2353 err = audit_socketcall(nargs[call] / sizeof(unsigned long), a);
2354 if (err)
2355 return err;
2356
2357 a0 = a[0];
2358 a1 = a[1];
2359
2360 switch (call) {
2361 case SYS_SOCKET:
2362 err = sys_socket(a0, a1, a[2]);
2363 break;
2364 case SYS_BIND:
2365 err = sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
2366 break;
2367 case SYS_CONNECT:
2368 err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
2369 break;
2370 case SYS_LISTEN:
2371 err = sys_listen(a0, a1);
2372 break;
2373 case SYS_ACCEPT:
2374 err = sys_accept4(a0, (struct sockaddr __user *)a1,
2375 (int __user *)a[2], 0);
2376 break;
2377 case SYS_GETSOCKNAME:
2378 err =
2379 sys_getsockname(a0, (struct sockaddr __user *)a1,
2380 (int __user *)a[2]);
2381 break;
2382 case SYS_GETPEERNAME:
2383 err =
2384 sys_getpeername(a0, (struct sockaddr __user *)a1,
2385 (int __user *)a[2]);
2386 break;
2387 case SYS_SOCKETPAIR:
2388 err = sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
2389 break;
2390 case SYS_SEND:
2391 err = sys_send(a0, (void __user *)a1, a[2], a[3]);
2392 break;
2393 case SYS_SENDTO:
2394 err = sys_sendto(a0, (void __user *)a1, a[2], a[3],
2395 (struct sockaddr __user *)a[4], a[5]);
2396 break;
2397 case SYS_RECV:
2398 err = sys_recv(a0, (void __user *)a1, a[2], a[3]);
2399 break;
2400 case SYS_RECVFROM:
2401 err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
2402 (struct sockaddr __user *)a[4],
2403 (int __user *)a[5]);
2404 break;
2405 case SYS_SHUTDOWN:
2406 err = sys_shutdown(a0, a1);
2407 break;
2408 case SYS_SETSOCKOPT:
2409 err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]);
2410 break;
2411 case SYS_GETSOCKOPT:
2412 err =
2413 sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
2414 (int __user *)a[4]);
2415 break;
2416 case SYS_SENDMSG:
2417 err = sys_sendmsg(a0, (struct user_msghdr __user *)a1, a[2]);
2418 break;
2419 case SYS_SENDMMSG:
2420 err = sys_sendmmsg(a0, (struct mmsghdr __user *)a1, a[2], a[3]);
2421 break;
2422 case SYS_RECVMSG:
2423 err = sys_recvmsg(a0, (struct user_msghdr __user *)a1, a[2]);
2424 break;
2425 case SYS_RECVMMSG:
2426 err = sys_recvmmsg(a0, (struct mmsghdr __user *)a1, a[2], a[3],
2427 (struct timespec __user *)a[4]);
2428 break;
2429 case SYS_ACCEPT4:
2430 err = sys_accept4(a0, (struct sockaddr __user *)a1,
2431 (int __user *)a[2], a[3]);
2432 break;
2433 default:
2434 err = -EINVAL;
2435 break;
2436 }
2437 return err;
2438 }
2439
2440 #endif /* __ARCH_WANT_SYS_SOCKETCALL */
2441
2442 /**
2443 * sock_register - add a socket protocol handler
2444 * @ops: description of protocol
2445 *
2446 * This function is called by a protocol handler that wants to
2447 * advertise its address family, and have it linked into the
2448 * socket interface. The value ops->family corresponds to the
2449 * socket system call protocol family.
2450 */
sock_register(const struct net_proto_family * ops)2451 int sock_register(const struct net_proto_family *ops)
2452 {
2453 int err;
2454
2455 if (ops->family >= NPROTO) {
2456 pr_crit("protocol %d >= NPROTO(%d)\n", ops->family, NPROTO);
2457 return -ENOBUFS;
2458 }
2459
2460 spin_lock(&net_family_lock);
2461 if (rcu_dereference_protected(net_families[ops->family],
2462 lockdep_is_held(&net_family_lock)))
2463 err = -EEXIST;
2464 else {
2465 rcu_assign_pointer(net_families[ops->family], ops);
2466 err = 0;
2467 }
2468 spin_unlock(&net_family_lock);
2469
2470 pr_info("NET: Registered protocol family %d\n", ops->family);
2471 return err;
2472 }
2473 EXPORT_SYMBOL(sock_register);
2474
2475 /**
2476 * sock_unregister - remove a protocol handler
2477 * @family: protocol family to remove
2478 *
2479 * This function is called by a protocol handler that wants to
2480 * remove its address family, and have it unlinked from the
2481 * new socket creation.
2482 *
2483 * If protocol handler is a module, then it can use module reference
2484 * counts to protect against new references. If protocol handler is not
2485 * a module then it needs to provide its own protection in
2486 * the ops->create routine.
2487 */
sock_unregister(int family)2488 void sock_unregister(int family)
2489 {
2490 BUG_ON(family < 0 || family >= NPROTO);
2491
2492 spin_lock(&net_family_lock);
2493 RCU_INIT_POINTER(net_families[family], NULL);
2494 spin_unlock(&net_family_lock);
2495
2496 synchronize_rcu();
2497
2498 pr_info("NET: Unregistered protocol family %d\n", family);
2499 }
2500 EXPORT_SYMBOL(sock_unregister);
2501
sock_init(void)2502 static int __init sock_init(void)
2503 {
2504 int err;
2505 /*
2506 * Initialize the network sysctl infrastructure.
2507 */
2508 err = net_sysctl_init();
2509 if (err)
2510 goto out;
2511
2512 /*
2513 * Initialize skbuff SLAB cache
2514 */
2515 skb_init();
2516
2517 /*
2518 * Initialize the protocols module.
2519 */
2520
2521 init_inodecache();
2522
2523 err = register_filesystem(&sock_fs_type);
2524 if (err)
2525 goto out_fs;
2526 sock_mnt = kern_mount(&sock_fs_type);
2527 if (IS_ERR(sock_mnt)) {
2528 err = PTR_ERR(sock_mnt);
2529 goto out_mount;
2530 }
2531
2532 /* The real protocol initialization is performed in later initcalls.
2533 */
2534
2535 #ifdef CONFIG_NETFILTER
2536 err = netfilter_init();
2537 if (err)
2538 goto out;
2539 #endif
2540
2541 ptp_classifier_init();
2542
2543 out:
2544 return err;
2545
2546 out_mount:
2547 unregister_filesystem(&sock_fs_type);
2548 out_fs:
2549 goto out;
2550 }
2551
2552 core_initcall(sock_init); /* early initcall */
2553
jit_init(void)2554 static int __init jit_init(void)
2555 {
2556 #ifdef CONFIG_BPF_JIT_ALWAYS_ON
2557 bpf_jit_enable = 1;
2558 #endif
2559 return 0;
2560 }
2561 pure_initcall(jit_init);
2562
2563 #ifdef CONFIG_PROC_FS
socket_seq_show(struct seq_file * seq)2564 void socket_seq_show(struct seq_file *seq)
2565 {
2566 int cpu;
2567 int counter = 0;
2568
2569 for_each_possible_cpu(cpu)
2570 counter += per_cpu(sockets_in_use, cpu);
2571
2572 /* It can be negative, by the way. 8) */
2573 if (counter < 0)
2574 counter = 0;
2575
2576 seq_printf(seq, "sockets: used %d\n", counter);
2577 }
2578 #endif /* CONFIG_PROC_FS */
2579
2580 #ifdef CONFIG_COMPAT
do_siocgstamp(struct net * net,struct socket * sock,unsigned int cmd,void __user * up)2581 static int do_siocgstamp(struct net *net, struct socket *sock,
2582 unsigned int cmd, void __user *up)
2583 {
2584 mm_segment_t old_fs = get_fs();
2585 struct timeval ktv;
2586 int err;
2587
2588 set_fs(KERNEL_DS);
2589 err = sock_do_ioctl(net, sock, cmd, (unsigned long)&ktv);
2590 set_fs(old_fs);
2591 if (!err)
2592 err = compat_put_timeval(&ktv, up);
2593
2594 return err;
2595 }
2596
do_siocgstampns(struct net * net,struct socket * sock,unsigned int cmd,void __user * up)2597 static int do_siocgstampns(struct net *net, struct socket *sock,
2598 unsigned int cmd, void __user *up)
2599 {
2600 mm_segment_t old_fs = get_fs();
2601 struct timespec kts;
2602 int err;
2603
2604 set_fs(KERNEL_DS);
2605 err = sock_do_ioctl(net, sock, cmd, (unsigned long)&kts);
2606 set_fs(old_fs);
2607 if (!err)
2608 err = compat_put_timespec(&kts, up);
2609
2610 return err;
2611 }
2612
dev_ifname32(struct net * net,struct compat_ifreq __user * uifr32)2613 static int dev_ifname32(struct net *net, struct compat_ifreq __user *uifr32)
2614 {
2615 struct ifreq __user *uifr;
2616 int err;
2617
2618 uifr = compat_alloc_user_space(sizeof(struct ifreq));
2619 if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
2620 return -EFAULT;
2621
2622 err = dev_ioctl(net, SIOCGIFNAME, uifr);
2623 if (err)
2624 return err;
2625
2626 if (copy_in_user(uifr32, uifr, sizeof(struct compat_ifreq)))
2627 return -EFAULT;
2628
2629 return 0;
2630 }
2631
dev_ifconf(struct net * net,struct compat_ifconf __user * uifc32)2632 static int dev_ifconf(struct net *net, struct compat_ifconf __user *uifc32)
2633 {
2634 struct compat_ifconf ifc32;
2635 struct ifconf ifc;
2636 struct ifconf __user *uifc;
2637 struct compat_ifreq __user *ifr32;
2638 struct ifreq __user *ifr;
2639 unsigned int i, j;
2640 int err;
2641
2642 if (copy_from_user(&ifc32, uifc32, sizeof(struct compat_ifconf)))
2643 return -EFAULT;
2644
2645 memset(&ifc, 0, sizeof(ifc));
2646 if (ifc32.ifcbuf == 0) {
2647 ifc32.ifc_len = 0;
2648 ifc.ifc_len = 0;
2649 ifc.ifc_req = NULL;
2650 uifc = compat_alloc_user_space(sizeof(struct ifconf));
2651 } else {
2652 size_t len = ((ifc32.ifc_len / sizeof(struct compat_ifreq)) + 1) *
2653 sizeof(struct ifreq);
2654 uifc = compat_alloc_user_space(sizeof(struct ifconf) + len);
2655 ifc.ifc_len = len;
2656 ifr = ifc.ifc_req = (void __user *)(uifc + 1);
2657 ifr32 = compat_ptr(ifc32.ifcbuf);
2658 for (i = 0; i < ifc32.ifc_len; i += sizeof(struct compat_ifreq)) {
2659 if (copy_in_user(ifr, ifr32, sizeof(struct compat_ifreq)))
2660 return -EFAULT;
2661 ifr++;
2662 ifr32++;
2663 }
2664 }
2665 if (copy_to_user(uifc, &ifc, sizeof(struct ifconf)))
2666 return -EFAULT;
2667
2668 err = dev_ioctl(net, SIOCGIFCONF, uifc);
2669 if (err)
2670 return err;
2671
2672 if (copy_from_user(&ifc, uifc, sizeof(struct ifconf)))
2673 return -EFAULT;
2674
2675 ifr = ifc.ifc_req;
2676 ifr32 = compat_ptr(ifc32.ifcbuf);
2677 for (i = 0, j = 0;
2678 i + sizeof(struct compat_ifreq) <= ifc32.ifc_len && j < ifc.ifc_len;
2679 i += sizeof(struct compat_ifreq), j += sizeof(struct ifreq)) {
2680 if (copy_in_user(ifr32, ifr, sizeof(struct compat_ifreq)))
2681 return -EFAULT;
2682 ifr32++;
2683 ifr++;
2684 }
2685
2686 if (ifc32.ifcbuf == 0) {
2687 /* Translate from 64-bit structure multiple to
2688 * a 32-bit one.
2689 */
2690 i = ifc.ifc_len;
2691 i = ((i / sizeof(struct ifreq)) * sizeof(struct compat_ifreq));
2692 ifc32.ifc_len = i;
2693 } else {
2694 ifc32.ifc_len = i;
2695 }
2696 if (copy_to_user(uifc32, &ifc32, sizeof(struct compat_ifconf)))
2697 return -EFAULT;
2698
2699 return 0;
2700 }
2701
ethtool_ioctl(struct net * net,struct compat_ifreq __user * ifr32)2702 static int ethtool_ioctl(struct net *net, struct compat_ifreq __user *ifr32)
2703 {
2704 struct compat_ethtool_rxnfc __user *compat_rxnfc;
2705 bool convert_in = false, convert_out = false;
2706 size_t buf_size = ALIGN(sizeof(struct ifreq), 8);
2707 struct ethtool_rxnfc __user *rxnfc;
2708 struct ifreq __user *ifr;
2709 u32 rule_cnt = 0, actual_rule_cnt;
2710 u32 ethcmd;
2711 u32 data;
2712 int ret;
2713
2714 if (get_user(data, &ifr32->ifr_ifru.ifru_data))
2715 return -EFAULT;
2716
2717 compat_rxnfc = compat_ptr(data);
2718
2719 if (get_user(ethcmd, &compat_rxnfc->cmd))
2720 return -EFAULT;
2721
2722 /* Most ethtool structures are defined without padding.
2723 * Unfortunately struct ethtool_rxnfc is an exception.
2724 */
2725 switch (ethcmd) {
2726 default:
2727 break;
2728 case ETHTOOL_GRXCLSRLALL:
2729 /* Buffer size is variable */
2730 if (get_user(rule_cnt, &compat_rxnfc->rule_cnt))
2731 return -EFAULT;
2732 if (rule_cnt > KMALLOC_MAX_SIZE / sizeof(u32))
2733 return -ENOMEM;
2734 buf_size += rule_cnt * sizeof(u32);
2735 /* fall through */
2736 case ETHTOOL_GRXRINGS:
2737 case ETHTOOL_GRXCLSRLCNT:
2738 case ETHTOOL_GRXCLSRULE:
2739 case ETHTOOL_SRXCLSRLINS:
2740 convert_out = true;
2741 /* fall through */
2742 case ETHTOOL_SRXCLSRLDEL:
2743 buf_size += sizeof(struct ethtool_rxnfc);
2744 convert_in = true;
2745 break;
2746 }
2747
2748 ifr = compat_alloc_user_space(buf_size);
2749 rxnfc = (void __user *)ifr + ALIGN(sizeof(struct ifreq), 8);
2750
2751 if (copy_in_user(&ifr->ifr_name, &ifr32->ifr_name, IFNAMSIZ))
2752 return -EFAULT;
2753
2754 if (put_user(convert_in ? rxnfc : compat_ptr(data),
2755 &ifr->ifr_ifru.ifru_data))
2756 return -EFAULT;
2757
2758 if (convert_in) {
2759 /* We expect there to be holes between fs.m_ext and
2760 * fs.ring_cookie and at the end of fs, but nowhere else.
2761 */
2762 BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc, fs.m_ext) +
2763 sizeof(compat_rxnfc->fs.m_ext) !=
2764 offsetof(struct ethtool_rxnfc, fs.m_ext) +
2765 sizeof(rxnfc->fs.m_ext));
2766 BUILD_BUG_ON(
2767 offsetof(struct compat_ethtool_rxnfc, fs.location) -
2768 offsetof(struct compat_ethtool_rxnfc, fs.ring_cookie) !=
2769 offsetof(struct ethtool_rxnfc, fs.location) -
2770 offsetof(struct ethtool_rxnfc, fs.ring_cookie));
2771
2772 if (copy_in_user(rxnfc, compat_rxnfc,
2773 (void __user *)(&rxnfc->fs.m_ext + 1) -
2774 (void __user *)rxnfc) ||
2775 copy_in_user(&rxnfc->fs.ring_cookie,
2776 &compat_rxnfc->fs.ring_cookie,
2777 (void __user *)(&rxnfc->fs.location + 1) -
2778 (void __user *)&rxnfc->fs.ring_cookie))
2779 return -EFAULT;
2780 if (ethcmd == ETHTOOL_GRXCLSRLALL) {
2781 if (put_user(rule_cnt, &rxnfc->rule_cnt))
2782 return -EFAULT;
2783 } else if (copy_in_user(&rxnfc->rule_cnt,
2784 &compat_rxnfc->rule_cnt,
2785 sizeof(rxnfc->rule_cnt)))
2786 return -EFAULT;
2787 }
2788
2789 ret = dev_ioctl(net, SIOCETHTOOL, ifr);
2790 if (ret)
2791 return ret;
2792
2793 if (convert_out) {
2794 if (copy_in_user(compat_rxnfc, rxnfc,
2795 (const void __user *)(&rxnfc->fs.m_ext + 1) -
2796 (const void __user *)rxnfc) ||
2797 copy_in_user(&compat_rxnfc->fs.ring_cookie,
2798 &rxnfc->fs.ring_cookie,
2799 (const void __user *)(&rxnfc->fs.location + 1) -
2800 (const void __user *)&rxnfc->fs.ring_cookie) ||
2801 copy_in_user(&compat_rxnfc->rule_cnt, &rxnfc->rule_cnt,
2802 sizeof(rxnfc->rule_cnt)))
2803 return -EFAULT;
2804
2805 if (ethcmd == ETHTOOL_GRXCLSRLALL) {
2806 /* As an optimisation, we only copy the actual
2807 * number of rules that the underlying
2808 * function returned. Since Mallory might
2809 * change the rule count in user memory, we
2810 * check that it is less than the rule count
2811 * originally given (as the user buffer size),
2812 * which has been range-checked.
2813 */
2814 if (get_user(actual_rule_cnt, &rxnfc->rule_cnt))
2815 return -EFAULT;
2816 if (actual_rule_cnt < rule_cnt)
2817 rule_cnt = actual_rule_cnt;
2818 if (copy_in_user(&compat_rxnfc->rule_locs[0],
2819 &rxnfc->rule_locs[0],
2820 rule_cnt * sizeof(u32)))
2821 return -EFAULT;
2822 }
2823 }
2824
2825 return 0;
2826 }
2827
compat_siocwandev(struct net * net,struct compat_ifreq __user * uifr32)2828 static int compat_siocwandev(struct net *net, struct compat_ifreq __user *uifr32)
2829 {
2830 void __user *uptr;
2831 compat_uptr_t uptr32;
2832 struct ifreq __user *uifr;
2833
2834 uifr = compat_alloc_user_space(sizeof(*uifr));
2835 if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
2836 return -EFAULT;
2837
2838 if (get_user(uptr32, &uifr32->ifr_settings.ifs_ifsu))
2839 return -EFAULT;
2840
2841 uptr = compat_ptr(uptr32);
2842
2843 if (put_user(uptr, &uifr->ifr_settings.ifs_ifsu.raw_hdlc))
2844 return -EFAULT;
2845
2846 return dev_ioctl(net, SIOCWANDEV, uifr);
2847 }
2848
bond_ioctl(struct net * net,unsigned int cmd,struct compat_ifreq __user * ifr32)2849 static int bond_ioctl(struct net *net, unsigned int cmd,
2850 struct compat_ifreq __user *ifr32)
2851 {
2852 struct ifreq kifr;
2853 mm_segment_t old_fs;
2854 int err;
2855
2856 switch (cmd) {
2857 case SIOCBONDENSLAVE:
2858 case SIOCBONDRELEASE:
2859 case SIOCBONDSETHWADDR:
2860 case SIOCBONDCHANGEACTIVE:
2861 if (copy_from_user(&kifr, ifr32, sizeof(struct compat_ifreq)))
2862 return -EFAULT;
2863
2864 old_fs = get_fs();
2865 set_fs(KERNEL_DS);
2866 err = dev_ioctl(net, cmd,
2867 (struct ifreq __user __force *) &kifr);
2868 set_fs(old_fs);
2869
2870 return err;
2871 default:
2872 return -ENOIOCTLCMD;
2873 }
2874 }
2875
2876 /* Handle ioctls that use ifreq::ifr_data and just need struct ifreq converted */
compat_ifr_data_ioctl(struct net * net,unsigned int cmd,struct compat_ifreq __user * u_ifreq32)2877 static int compat_ifr_data_ioctl(struct net *net, unsigned int cmd,
2878 struct compat_ifreq __user *u_ifreq32)
2879 {
2880 struct ifreq __user *u_ifreq64;
2881 char tmp_buf[IFNAMSIZ];
2882 void __user *data64;
2883 u32 data32;
2884
2885 if (copy_from_user(&tmp_buf[0], &(u_ifreq32->ifr_ifrn.ifrn_name[0]),
2886 IFNAMSIZ))
2887 return -EFAULT;
2888 if (get_user(data32, &u_ifreq32->ifr_ifru.ifru_data))
2889 return -EFAULT;
2890 data64 = compat_ptr(data32);
2891
2892 u_ifreq64 = compat_alloc_user_space(sizeof(*u_ifreq64));
2893
2894 if (copy_to_user(&u_ifreq64->ifr_ifrn.ifrn_name[0], &tmp_buf[0],
2895 IFNAMSIZ))
2896 return -EFAULT;
2897 if (put_user(data64, &u_ifreq64->ifr_ifru.ifru_data))
2898 return -EFAULT;
2899
2900 return dev_ioctl(net, cmd, u_ifreq64);
2901 }
2902
dev_ifsioc(struct net * net,struct socket * sock,unsigned int cmd,struct compat_ifreq __user * uifr32)2903 static int dev_ifsioc(struct net *net, struct socket *sock,
2904 unsigned int cmd, struct compat_ifreq __user *uifr32)
2905 {
2906 struct ifreq __user *uifr;
2907 int err;
2908
2909 uifr = compat_alloc_user_space(sizeof(*uifr));
2910 if (copy_in_user(uifr, uifr32, sizeof(*uifr32)))
2911 return -EFAULT;
2912
2913 err = sock_do_ioctl(net, sock, cmd, (unsigned long)uifr);
2914
2915 if (!err) {
2916 switch (cmd) {
2917 case SIOCGIFFLAGS:
2918 case SIOCGIFMETRIC:
2919 case SIOCGIFMTU:
2920 case SIOCGIFMEM:
2921 case SIOCGIFHWADDR:
2922 case SIOCGIFINDEX:
2923 case SIOCGIFADDR:
2924 case SIOCGIFBRDADDR:
2925 case SIOCGIFDSTADDR:
2926 case SIOCGIFNETMASK:
2927 case SIOCGIFPFLAGS:
2928 case SIOCGIFTXQLEN:
2929 case SIOCGMIIPHY:
2930 case SIOCGMIIREG:
2931 if (copy_in_user(uifr32, uifr, sizeof(*uifr32)))
2932 err = -EFAULT;
2933 break;
2934 }
2935 }
2936 return err;
2937 }
2938
compat_sioc_ifmap(struct net * net,unsigned int cmd,struct compat_ifreq __user * uifr32)2939 static int compat_sioc_ifmap(struct net *net, unsigned int cmd,
2940 struct compat_ifreq __user *uifr32)
2941 {
2942 struct ifreq ifr;
2943 struct compat_ifmap __user *uifmap32;
2944 mm_segment_t old_fs;
2945 int err;
2946
2947 uifmap32 = &uifr32->ifr_ifru.ifru_map;
2948 err = copy_from_user(&ifr, uifr32, sizeof(ifr.ifr_name));
2949 err |= get_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
2950 err |= get_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
2951 err |= get_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
2952 err |= get_user(ifr.ifr_map.irq, &uifmap32->irq);
2953 err |= get_user(ifr.ifr_map.dma, &uifmap32->dma);
2954 err |= get_user(ifr.ifr_map.port, &uifmap32->port);
2955 if (err)
2956 return -EFAULT;
2957
2958 old_fs = get_fs();
2959 set_fs(KERNEL_DS);
2960 err = dev_ioctl(net, cmd, (void __user __force *)&ifr);
2961 set_fs(old_fs);
2962
2963 if (cmd == SIOCGIFMAP && !err) {
2964 err = copy_to_user(uifr32, &ifr, sizeof(ifr.ifr_name));
2965 err |= put_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
2966 err |= put_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
2967 err |= put_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
2968 err |= put_user(ifr.ifr_map.irq, &uifmap32->irq);
2969 err |= put_user(ifr.ifr_map.dma, &uifmap32->dma);
2970 err |= put_user(ifr.ifr_map.port, &uifmap32->port);
2971 if (err)
2972 err = -EFAULT;
2973 }
2974 return err;
2975 }
2976
2977 struct rtentry32 {
2978 u32 rt_pad1;
2979 struct sockaddr rt_dst; /* target address */
2980 struct sockaddr rt_gateway; /* gateway addr (RTF_GATEWAY) */
2981 struct sockaddr rt_genmask; /* target network mask (IP) */
2982 unsigned short rt_flags;
2983 short rt_pad2;
2984 u32 rt_pad3;
2985 unsigned char rt_tos;
2986 unsigned char rt_class;
2987 short rt_pad4;
2988 short rt_metric; /* +1 for binary compatibility! */
2989 /* char * */ u32 rt_dev; /* forcing the device at add */
2990 u32 rt_mtu; /* per route MTU/Window */
2991 u32 rt_window; /* Window clamping */
2992 unsigned short rt_irtt; /* Initial RTT */
2993 };
2994
2995 struct in6_rtmsg32 {
2996 struct in6_addr rtmsg_dst;
2997 struct in6_addr rtmsg_src;
2998 struct in6_addr rtmsg_gateway;
2999 u32 rtmsg_type;
3000 u16 rtmsg_dst_len;
3001 u16 rtmsg_src_len;
3002 u32 rtmsg_metric;
3003 u32 rtmsg_info;
3004 u32 rtmsg_flags;
3005 s32 rtmsg_ifindex;
3006 };
3007
routing_ioctl(struct net * net,struct socket * sock,unsigned int cmd,void __user * argp)3008 static int routing_ioctl(struct net *net, struct socket *sock,
3009 unsigned int cmd, void __user *argp)
3010 {
3011 int ret;
3012 void *r = NULL;
3013 struct in6_rtmsg r6;
3014 struct rtentry r4;
3015 char devname[16];
3016 u32 rtdev;
3017 mm_segment_t old_fs = get_fs();
3018
3019 if (sock && sock->sk && sock->sk->sk_family == AF_INET6) { /* ipv6 */
3020 struct in6_rtmsg32 __user *ur6 = argp;
3021 ret = copy_from_user(&r6.rtmsg_dst, &(ur6->rtmsg_dst),
3022 3 * sizeof(struct in6_addr));
3023 ret |= get_user(r6.rtmsg_type, &(ur6->rtmsg_type));
3024 ret |= get_user(r6.rtmsg_dst_len, &(ur6->rtmsg_dst_len));
3025 ret |= get_user(r6.rtmsg_src_len, &(ur6->rtmsg_src_len));
3026 ret |= get_user(r6.rtmsg_metric, &(ur6->rtmsg_metric));
3027 ret |= get_user(r6.rtmsg_info, &(ur6->rtmsg_info));
3028 ret |= get_user(r6.rtmsg_flags, &(ur6->rtmsg_flags));
3029 ret |= get_user(r6.rtmsg_ifindex, &(ur6->rtmsg_ifindex));
3030
3031 r = (void *) &r6;
3032 } else { /* ipv4 */
3033 struct rtentry32 __user *ur4 = argp;
3034 ret = copy_from_user(&r4.rt_dst, &(ur4->rt_dst),
3035 3 * sizeof(struct sockaddr));
3036 ret |= get_user(r4.rt_flags, &(ur4->rt_flags));
3037 ret |= get_user(r4.rt_metric, &(ur4->rt_metric));
3038 ret |= get_user(r4.rt_mtu, &(ur4->rt_mtu));
3039 ret |= get_user(r4.rt_window, &(ur4->rt_window));
3040 ret |= get_user(r4.rt_irtt, &(ur4->rt_irtt));
3041 ret |= get_user(rtdev, &(ur4->rt_dev));
3042 if (rtdev) {
3043 ret |= copy_from_user(devname, compat_ptr(rtdev), 15);
3044 r4.rt_dev = (char __user __force *)devname;
3045 devname[15] = 0;
3046 } else
3047 r4.rt_dev = NULL;
3048
3049 r = (void *) &r4;
3050 }
3051
3052 if (ret) {
3053 ret = -EFAULT;
3054 goto out;
3055 }
3056
3057 set_fs(KERNEL_DS);
3058 ret = sock_do_ioctl(net, sock, cmd, (unsigned long) r);
3059 set_fs(old_fs);
3060
3061 out:
3062 return ret;
3063 }
3064
3065 /* Since old style bridge ioctl's endup using SIOCDEVPRIVATE
3066 * for some operations; this forces use of the newer bridge-utils that
3067 * use compatible ioctls
3068 */
old_bridge_ioctl(compat_ulong_t __user * argp)3069 static int old_bridge_ioctl(compat_ulong_t __user *argp)
3070 {
3071 compat_ulong_t tmp;
3072
3073 if (get_user(tmp, argp))
3074 return -EFAULT;
3075 if (tmp == BRCTL_GET_VERSION)
3076 return BRCTL_VERSION + 1;
3077 return -EINVAL;
3078 }
3079
compat_sock_ioctl_trans(struct file * file,struct socket * sock,unsigned int cmd,unsigned long arg)3080 static int compat_sock_ioctl_trans(struct file *file, struct socket *sock,
3081 unsigned int cmd, unsigned long arg)
3082 {
3083 void __user *argp = compat_ptr(arg);
3084 struct sock *sk = sock->sk;
3085 struct net *net = sock_net(sk);
3086
3087 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))
3088 return compat_ifr_data_ioctl(net, cmd, argp);
3089
3090 switch (cmd) {
3091 case SIOCSIFBR:
3092 case SIOCGIFBR:
3093 return old_bridge_ioctl(argp);
3094 case SIOCGIFNAME:
3095 return dev_ifname32(net, argp);
3096 case SIOCGIFCONF:
3097 return dev_ifconf(net, argp);
3098 case SIOCETHTOOL:
3099 return ethtool_ioctl(net, argp);
3100 case SIOCWANDEV:
3101 return compat_siocwandev(net, argp);
3102 case SIOCGIFMAP:
3103 case SIOCSIFMAP:
3104 return compat_sioc_ifmap(net, cmd, argp);
3105 case SIOCBONDENSLAVE:
3106 case SIOCBONDRELEASE:
3107 case SIOCBONDSETHWADDR:
3108 case SIOCBONDCHANGEACTIVE:
3109 return bond_ioctl(net, cmd, argp);
3110 case SIOCADDRT:
3111 case SIOCDELRT:
3112 return routing_ioctl(net, sock, cmd, argp);
3113 case SIOCGSTAMP:
3114 return do_siocgstamp(net, sock, cmd, argp);
3115 case SIOCGSTAMPNS:
3116 return do_siocgstampns(net, sock, cmd, argp);
3117 case SIOCBONDSLAVEINFOQUERY:
3118 case SIOCBONDINFOQUERY:
3119 case SIOCSHWTSTAMP:
3120 case SIOCGHWTSTAMP:
3121 return compat_ifr_data_ioctl(net, cmd, argp);
3122
3123 case FIOSETOWN:
3124 case SIOCSPGRP:
3125 case FIOGETOWN:
3126 case SIOCGPGRP:
3127 case SIOCBRADDBR:
3128 case SIOCBRDELBR:
3129 case SIOCGIFVLAN:
3130 case SIOCSIFVLAN:
3131 case SIOCADDDLCI:
3132 case SIOCDELDLCI:
3133 return sock_ioctl(file, cmd, arg);
3134
3135 case SIOCGIFFLAGS:
3136 case SIOCSIFFLAGS:
3137 case SIOCGIFMETRIC:
3138 case SIOCSIFMETRIC:
3139 case SIOCGIFMTU:
3140 case SIOCSIFMTU:
3141 case SIOCGIFMEM:
3142 case SIOCSIFMEM:
3143 case SIOCGIFHWADDR:
3144 case SIOCSIFHWADDR:
3145 case SIOCADDMULTI:
3146 case SIOCDELMULTI:
3147 case SIOCGIFINDEX:
3148 case SIOCGIFADDR:
3149 case SIOCSIFADDR:
3150 case SIOCSIFHWBROADCAST:
3151 case SIOCDIFADDR:
3152 case SIOCGIFBRDADDR:
3153 case SIOCSIFBRDADDR:
3154 case SIOCGIFDSTADDR:
3155 case SIOCSIFDSTADDR:
3156 case SIOCGIFNETMASK:
3157 case SIOCSIFNETMASK:
3158 case SIOCSIFPFLAGS:
3159 case SIOCGIFPFLAGS:
3160 case SIOCGIFTXQLEN:
3161 case SIOCSIFTXQLEN:
3162 case SIOCBRADDIF:
3163 case SIOCBRDELIF:
3164 case SIOCSIFNAME:
3165 case SIOCGMIIPHY:
3166 case SIOCGMIIREG:
3167 case SIOCSMIIREG:
3168 return dev_ifsioc(net, sock, cmd, argp);
3169
3170 case SIOCSARP:
3171 case SIOCGARP:
3172 case SIOCDARP:
3173 case SIOCOUTQNSD:
3174 case SIOCATMARK:
3175 return sock_do_ioctl(net, sock, cmd, arg);
3176 }
3177
3178 return -ENOIOCTLCMD;
3179 }
3180
compat_sock_ioctl(struct file * file,unsigned int cmd,unsigned long arg)3181 static long compat_sock_ioctl(struct file *file, unsigned int cmd,
3182 unsigned long arg)
3183 {
3184 struct socket *sock = file->private_data;
3185 int ret = -ENOIOCTLCMD;
3186 struct sock *sk;
3187 struct net *net;
3188
3189 sk = sock->sk;
3190 net = sock_net(sk);
3191
3192 if (sock->ops->compat_ioctl)
3193 ret = sock->ops->compat_ioctl(sock, cmd, arg);
3194
3195 if (ret == -ENOIOCTLCMD &&
3196 (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST))
3197 ret = compat_wext_handle_ioctl(net, cmd, arg);
3198
3199 if (ret == -ENOIOCTLCMD)
3200 ret = compat_sock_ioctl_trans(file, sock, cmd, arg);
3201
3202 return ret;
3203 }
3204 #endif
3205
kernel_bind(struct socket * sock,struct sockaddr * addr,int addrlen)3206 int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
3207 {
3208 return sock->ops->bind(sock, addr, addrlen);
3209 }
3210 EXPORT_SYMBOL(kernel_bind);
3211
kernel_listen(struct socket * sock,int backlog)3212 int kernel_listen(struct socket *sock, int backlog)
3213 {
3214 return sock->ops->listen(sock, backlog);
3215 }
3216 EXPORT_SYMBOL(kernel_listen);
3217
kernel_accept(struct socket * sock,struct socket ** newsock,int flags)3218 int kernel_accept(struct socket *sock, struct socket **newsock, int flags)
3219 {
3220 struct sock *sk = sock->sk;
3221 int err;
3222
3223 err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
3224 newsock);
3225 if (err < 0)
3226 goto done;
3227
3228 err = sock->ops->accept(sock, *newsock, flags);
3229 if (err < 0) {
3230 sock_release(*newsock);
3231 *newsock = NULL;
3232 goto done;
3233 }
3234
3235 (*newsock)->ops = sock->ops;
3236 __module_get((*newsock)->ops->owner);
3237
3238 done:
3239 return err;
3240 }
3241 EXPORT_SYMBOL(kernel_accept);
3242
kernel_connect(struct socket * sock,struct sockaddr * addr,int addrlen,int flags)3243 int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
3244 int flags)
3245 {
3246 return sock->ops->connect(sock, addr, addrlen, flags);
3247 }
3248 EXPORT_SYMBOL(kernel_connect);
3249
kernel_getsockname(struct socket * sock,struct sockaddr * addr,int * addrlen)3250 int kernel_getsockname(struct socket *sock, struct sockaddr *addr,
3251 int *addrlen)
3252 {
3253 return sock->ops->getname(sock, addr, addrlen, 0);
3254 }
3255 EXPORT_SYMBOL(kernel_getsockname);
3256
kernel_getpeername(struct socket * sock,struct sockaddr * addr,int * addrlen)3257 int kernel_getpeername(struct socket *sock, struct sockaddr *addr,
3258 int *addrlen)
3259 {
3260 return sock->ops->getname(sock, addr, addrlen, 1);
3261 }
3262 EXPORT_SYMBOL(kernel_getpeername);
3263
kernel_getsockopt(struct socket * sock,int level,int optname,char * optval,int * optlen)3264 int kernel_getsockopt(struct socket *sock, int level, int optname,
3265 char *optval, int *optlen)
3266 {
3267 mm_segment_t oldfs = get_fs();
3268 char __user *uoptval;
3269 int __user *uoptlen;
3270 int err;
3271
3272 uoptval = (char __user __force *) optval;
3273 uoptlen = (int __user __force *) optlen;
3274
3275 set_fs(KERNEL_DS);
3276 if (level == SOL_SOCKET)
3277 err = sock_getsockopt(sock, level, optname, uoptval, uoptlen);
3278 else
3279 err = sock->ops->getsockopt(sock, level, optname, uoptval,
3280 uoptlen);
3281 set_fs(oldfs);
3282 return err;
3283 }
3284 EXPORT_SYMBOL(kernel_getsockopt);
3285
kernel_setsockopt(struct socket * sock,int level,int optname,char * optval,unsigned int optlen)3286 int kernel_setsockopt(struct socket *sock, int level, int optname,
3287 char *optval, unsigned int optlen)
3288 {
3289 mm_segment_t oldfs = get_fs();
3290 char __user *uoptval;
3291 int err;
3292
3293 uoptval = (char __user __force *) optval;
3294
3295 set_fs(KERNEL_DS);
3296 if (level == SOL_SOCKET)
3297 err = sock_setsockopt(sock, level, optname, uoptval, optlen);
3298 else
3299 err = sock->ops->setsockopt(sock, level, optname, uoptval,
3300 optlen);
3301 set_fs(oldfs);
3302 return err;
3303 }
3304 EXPORT_SYMBOL(kernel_setsockopt);
3305
kernel_sendpage(struct socket * sock,struct page * page,int offset,size_t size,int flags)3306 int kernel_sendpage(struct socket *sock, struct page *page, int offset,
3307 size_t size, int flags)
3308 {
3309 if (sock->ops->sendpage)
3310 return sock->ops->sendpage(sock, page, offset, size, flags);
3311
3312 return sock_no_sendpage(sock, page, offset, size, flags);
3313 }
3314 EXPORT_SYMBOL(kernel_sendpage);
3315
kernel_sock_ioctl(struct socket * sock,int cmd,unsigned long arg)3316 int kernel_sock_ioctl(struct socket *sock, int cmd, unsigned long arg)
3317 {
3318 mm_segment_t oldfs = get_fs();
3319 int err;
3320
3321 set_fs(KERNEL_DS);
3322 err = sock->ops->ioctl(sock, cmd, arg);
3323 set_fs(oldfs);
3324
3325 return err;
3326 }
3327 EXPORT_SYMBOL(kernel_sock_ioctl);
3328
kernel_sock_shutdown(struct socket * sock,enum sock_shutdown_cmd how)3329 int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how)
3330 {
3331 return sock->ops->shutdown(sock, how);
3332 }
3333 EXPORT_SYMBOL(kernel_sock_shutdown);
3334
3335 /* This routine returns the IP overhead imposed by a socket i.e.
3336 * the length of the underlying IP header, depending on whether
3337 * this is an IPv4 or IPv6 socket and the length from IP options turned
3338 * on at the socket. Assumes that the caller has a lock on the socket.
3339 */
kernel_sock_ip_overhead(struct sock * sk)3340 u32 kernel_sock_ip_overhead(struct sock *sk)
3341 {
3342 struct inet_sock *inet;
3343 struct ip_options_rcu *opt;
3344 u32 overhead = 0;
3345 bool owned_by_user;
3346 #if IS_ENABLED(CONFIG_IPV6)
3347 struct ipv6_pinfo *np;
3348 struct ipv6_txoptions *optv6 = NULL;
3349 #endif /* IS_ENABLED(CONFIG_IPV6) */
3350
3351 if (!sk)
3352 return overhead;
3353
3354 owned_by_user = sock_owned_by_user(sk);
3355 switch (sk->sk_family) {
3356 case AF_INET:
3357 inet = inet_sk(sk);
3358 overhead += sizeof(struct iphdr);
3359 opt = rcu_dereference_protected(inet->inet_opt,
3360 owned_by_user);
3361 if (opt)
3362 overhead += opt->opt.optlen;
3363 return overhead;
3364 #if IS_ENABLED(CONFIG_IPV6)
3365 case AF_INET6:
3366 np = inet6_sk(sk);
3367 overhead += sizeof(struct ipv6hdr);
3368 if (np)
3369 optv6 = rcu_dereference_protected(np->opt,
3370 owned_by_user);
3371 if (optv6)
3372 overhead += (optv6->opt_flen + optv6->opt_nflen);
3373 return overhead;
3374 #endif /* IS_ENABLED(CONFIG_IPV6) */
3375 default: /* Returns 0 overhead if the socket is not ipv4 or ipv6 */
3376 return overhead;
3377 }
3378 }
3379 EXPORT_SYMBOL(kernel_sock_ip_overhead);
3380