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