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