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/wanrouter.h>
73 #include <linux/if_bridge.h>
74 #include <linux/if_frad.h>
75 #include <linux/if_vlan.h>
76 #include <linux/init.h>
77 #include <linux/poll.h>
78 #include <linux/cache.h>
79 #include <linux/module.h>
80 #include <linux/highmem.h>
81 #include <linux/mount.h>
82 #include <linux/security.h>
83 #include <linux/syscalls.h>
84 #include <linux/compat.h>
85 #include <linux/kmod.h>
86 #include <linux/audit.h>
87 #include <linux/wireless.h>
88 #include <linux/nsproxy.h>
89
90 #include <asm/uaccess.h>
91 #include <asm/unistd.h>
92
93 #include <net/compat.h>
94 #include <net/wext.h>
95
96 #include <net/sock.h>
97 #include <linux/netfilter.h>
98
99 #ifdef CONFIG_UID_STAT
100 #include <linux/uid_stat.h>
101 #endif
102
103 static int sock_no_open(struct inode *irrelevant, struct file *dontcare);
104 static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
105 unsigned long nr_segs, loff_t pos);
106 static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov,
107 unsigned long nr_segs, loff_t pos);
108 static int sock_mmap(struct file *file, struct vm_area_struct *vma);
109
110 static int sock_close(struct inode *inode, struct file *file);
111 static unsigned int sock_poll(struct file *file,
112 struct poll_table_struct *wait);
113 static long sock_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
114 #ifdef CONFIG_COMPAT
115 static long compat_sock_ioctl(struct file *file,
116 unsigned int cmd, unsigned long arg);
117 #endif
118 static int sock_fasync(int fd, struct file *filp, int on);
119 static ssize_t sock_sendpage(struct file *file, struct page *page,
120 int offset, size_t size, loff_t *ppos, int more);
121 static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
122 struct pipe_inode_info *pipe, size_t len,
123 unsigned int flags);
124
125 /*
126 * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
127 * in the operation structures but are done directly via the socketcall() multiplexor.
128 */
129
130 static const struct file_operations socket_file_ops = {
131 .owner = THIS_MODULE,
132 .llseek = no_llseek,
133 .aio_read = sock_aio_read,
134 .aio_write = sock_aio_write,
135 .poll = sock_poll,
136 .unlocked_ioctl = sock_ioctl,
137 #ifdef CONFIG_COMPAT
138 .compat_ioctl = compat_sock_ioctl,
139 #endif
140 .mmap = sock_mmap,
141 .open = sock_no_open, /* special open code to disallow open via /proc */
142 .release = sock_close,
143 .fasync = sock_fasync,
144 .sendpage = sock_sendpage,
145 .splice_write = generic_splice_sendpage,
146 .splice_read = sock_splice_read,
147 };
148
149 /*
150 * The protocol list. Each protocol is registered in here.
151 */
152
153 static DEFINE_SPINLOCK(net_family_lock);
154 static const struct net_proto_family *net_families[NPROTO] __read_mostly;
155
156 /*
157 * Statistics counters of the socket lists
158 */
159
160 static DEFINE_PER_CPU(int, sockets_in_use) = 0;
161
162 /*
163 * Support routines.
164 * Move socket addresses back and forth across the kernel/user
165 * divide and look after the messy bits.
166 */
167
168 #define MAX_SOCK_ADDR 128 /* 108 for Unix domain -
169 16 for IP, 16 for IPX,
170 24 for IPv6,
171 about 80 for AX.25
172 must be at least one bigger than
173 the AF_UNIX size (see net/unix/af_unix.c
174 :unix_mkname()).
175 */
176
177 /**
178 * move_addr_to_kernel - copy a socket address into kernel space
179 * @uaddr: Address in user space
180 * @kaddr: Address in kernel space
181 * @ulen: Length in user space
182 *
183 * The address is copied into kernel space. If the provided address is
184 * too long an error code of -EINVAL is returned. If the copy gives
185 * invalid addresses -EFAULT is returned. On a success 0 is returned.
186 */
187
move_addr_to_kernel(void __user * uaddr,int ulen,struct sockaddr * kaddr)188 int move_addr_to_kernel(void __user *uaddr, int ulen, struct sockaddr *kaddr)
189 {
190 if (ulen < 0 || ulen > sizeof(struct sockaddr_storage))
191 return -EINVAL;
192 if (ulen == 0)
193 return 0;
194 if (copy_from_user(kaddr, uaddr, ulen))
195 return -EFAULT;
196 return audit_sockaddr(ulen, kaddr);
197 }
198
199 /**
200 * move_addr_to_user - copy an address to user space
201 * @kaddr: kernel space address
202 * @klen: length of address in kernel
203 * @uaddr: user space address
204 * @ulen: pointer to user length field
205 *
206 * The value pointed to by ulen on entry is the buffer length available.
207 * This is overwritten with the buffer space used. -EINVAL is returned
208 * if an overlong buffer is specified or a negative buffer size. -EFAULT
209 * is returned if either the buffer or the length field are not
210 * accessible.
211 * After copying the data up to the limit the user specifies, the true
212 * length of the data is written over the length limit the user
213 * specified. Zero is returned for a success.
214 */
215
move_addr_to_user(struct sockaddr * kaddr,int klen,void __user * uaddr,int __user * ulen)216 int move_addr_to_user(struct sockaddr *kaddr, int klen, void __user *uaddr,
217 int __user *ulen)
218 {
219 int err;
220 int len;
221
222 err = get_user(len, ulen);
223 if (err)
224 return err;
225 if (len > klen)
226 len = klen;
227 if (len < 0 || len > sizeof(struct sockaddr_storage))
228 return -EINVAL;
229 if (len) {
230 if (audit_sockaddr(klen, kaddr))
231 return -ENOMEM;
232 if (copy_to_user(uaddr, kaddr, len))
233 return -EFAULT;
234 }
235 /*
236 * "fromlen shall refer to the value before truncation.."
237 * 1003.1g
238 */
239 return __put_user(klen, ulen);
240 }
241
242 #define SOCKFS_MAGIC 0x534F434B
243
244 static struct kmem_cache *sock_inode_cachep __read_mostly;
245
sock_alloc_inode(struct super_block * sb)246 static struct inode *sock_alloc_inode(struct super_block *sb)
247 {
248 struct socket_alloc *ei;
249
250 ei = kmem_cache_alloc(sock_inode_cachep, GFP_KERNEL);
251 if (!ei)
252 return NULL;
253 init_waitqueue_head(&ei->socket.wait);
254
255 ei->socket.fasync_list = NULL;
256 ei->socket.state = SS_UNCONNECTED;
257 ei->socket.flags = 0;
258 ei->socket.ops = NULL;
259 ei->socket.sk = NULL;
260 ei->socket.file = NULL;
261
262 return &ei->vfs_inode;
263 }
264
sock_destroy_inode(struct inode * inode)265 static void sock_destroy_inode(struct inode *inode)
266 {
267 kmem_cache_free(sock_inode_cachep,
268 container_of(inode, struct socket_alloc, vfs_inode));
269 }
270
init_once(void * foo)271 static void init_once(void *foo)
272 {
273 struct socket_alloc *ei = (struct socket_alloc *)foo;
274
275 inode_init_once(&ei->vfs_inode);
276 }
277
init_inodecache(void)278 static int init_inodecache(void)
279 {
280 sock_inode_cachep = kmem_cache_create("sock_inode_cache",
281 sizeof(struct socket_alloc),
282 0,
283 (SLAB_HWCACHE_ALIGN |
284 SLAB_RECLAIM_ACCOUNT |
285 SLAB_MEM_SPREAD),
286 init_once);
287 if (sock_inode_cachep == NULL)
288 return -ENOMEM;
289 return 0;
290 }
291
292 static struct super_operations sockfs_ops = {
293 .alloc_inode = sock_alloc_inode,
294 .destroy_inode =sock_destroy_inode,
295 .statfs = simple_statfs,
296 };
297
sockfs_get_sb(struct file_system_type * fs_type,int flags,const char * dev_name,void * data,struct vfsmount * mnt)298 static int sockfs_get_sb(struct file_system_type *fs_type,
299 int flags, const char *dev_name, void *data,
300 struct vfsmount *mnt)
301 {
302 return get_sb_pseudo(fs_type, "socket:", &sockfs_ops, SOCKFS_MAGIC,
303 mnt);
304 }
305
306 static struct vfsmount *sock_mnt __read_mostly;
307
308 static struct file_system_type sock_fs_type = {
309 .name = "sockfs",
310 .get_sb = sockfs_get_sb,
311 .kill_sb = kill_anon_super,
312 };
313
sockfs_delete_dentry(struct dentry * dentry)314 static int sockfs_delete_dentry(struct dentry *dentry)
315 {
316 /*
317 * At creation time, we pretended this dentry was hashed
318 * (by clearing DCACHE_UNHASHED bit in d_flags)
319 * At delete time, we restore the truth : not hashed.
320 * (so that dput() can proceed correctly)
321 */
322 dentry->d_flags |= DCACHE_UNHASHED;
323 return 0;
324 }
325
326 /*
327 * sockfs_dname() is called from d_path().
328 */
sockfs_dname(struct dentry * dentry,char * buffer,int buflen)329 static char *sockfs_dname(struct dentry *dentry, char *buffer, int buflen)
330 {
331 return dynamic_dname(dentry, buffer, buflen, "socket:[%lu]",
332 dentry->d_inode->i_ino);
333 }
334
335 static struct dentry_operations sockfs_dentry_operations = {
336 .d_delete = sockfs_delete_dentry,
337 .d_dname = sockfs_dname,
338 };
339
340 /*
341 * Obtains the first available file descriptor and sets it up for use.
342 *
343 * These functions create file structures and maps them to fd space
344 * of the current process. On success it returns file descriptor
345 * and file struct implicitly stored in sock->file.
346 * Note that another thread may close file descriptor before we return
347 * from this function. We use the fact that now we do not refer
348 * to socket after mapping. If one day we will need it, this
349 * function will increment ref. count on file by 1.
350 *
351 * In any case returned fd MAY BE not valid!
352 * This race condition is unavoidable
353 * with shared fd spaces, we cannot solve it inside kernel,
354 * but we take care of internal coherence yet.
355 */
356
sock_alloc_fd(struct file ** filep,int flags)357 static int sock_alloc_fd(struct file **filep, int flags)
358 {
359 int fd;
360
361 fd = get_unused_fd_flags(flags);
362 if (likely(fd >= 0)) {
363 struct file *file = get_empty_filp();
364
365 *filep = file;
366 if (unlikely(!file)) {
367 put_unused_fd(fd);
368 return -ENFILE;
369 }
370 } else
371 *filep = NULL;
372 return fd;
373 }
374
sock_attach_fd(struct socket * sock,struct file * file,int flags)375 static int sock_attach_fd(struct socket *sock, struct file *file, int flags)
376 {
377 struct dentry *dentry;
378 struct qstr name = { .name = "" };
379
380 dentry = d_alloc(sock_mnt->mnt_sb->s_root, &name);
381 if (unlikely(!dentry))
382 return -ENOMEM;
383
384 dentry->d_op = &sockfs_dentry_operations;
385 /*
386 * We dont want to push this dentry into global dentry hash table.
387 * We pretend dentry is already hashed, by unsetting DCACHE_UNHASHED
388 * This permits a working /proc/$pid/fd/XXX on sockets
389 */
390 dentry->d_flags &= ~DCACHE_UNHASHED;
391 d_instantiate(dentry, SOCK_INODE(sock));
392
393 sock->file = file;
394 init_file(file, sock_mnt, dentry, FMODE_READ | FMODE_WRITE,
395 &socket_file_ops);
396 SOCK_INODE(sock)->i_fop = &socket_file_ops;
397 file->f_flags = O_RDWR | (flags & O_NONBLOCK);
398 file->f_pos = 0;
399 file->private_data = sock;
400
401 return 0;
402 }
403
sock_map_fd(struct socket * sock,int flags)404 int sock_map_fd(struct socket *sock, int flags)
405 {
406 struct file *newfile;
407 int fd = sock_alloc_fd(&newfile, flags);
408
409 if (likely(fd >= 0)) {
410 int err = sock_attach_fd(sock, newfile, flags);
411
412 if (unlikely(err < 0)) {
413 put_filp(newfile);
414 put_unused_fd(fd);
415 return err;
416 }
417 fd_install(fd, newfile);
418 }
419 return fd;
420 }
421
sock_from_file(struct file * file,int * err)422 static struct socket *sock_from_file(struct file *file, int *err)
423 {
424 if (file->f_op == &socket_file_ops)
425 return file->private_data; /* set in sock_map_fd */
426
427 *err = -ENOTSOCK;
428 return NULL;
429 }
430
431 /**
432 * sockfd_lookup - Go from a file number to its socket slot
433 * @fd: file handle
434 * @err: pointer to an error code return
435 *
436 * The file handle passed in is locked and the socket it is bound
437 * too is returned. If an error occurs the err pointer is overwritten
438 * with a negative errno code and NULL is returned. The function checks
439 * for both invalid handles and passing a handle which is not a socket.
440 *
441 * On a success the socket object pointer is returned.
442 */
443
sockfd_lookup(int fd,int * err)444 struct socket *sockfd_lookup(int fd, int *err)
445 {
446 struct file *file;
447 struct socket *sock;
448
449 file = fget(fd);
450 if (!file) {
451 *err = -EBADF;
452 return NULL;
453 }
454
455 sock = sock_from_file(file, err);
456 if (!sock)
457 fput(file);
458 return sock;
459 }
460
sockfd_lookup_light(int fd,int * err,int * fput_needed)461 static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)
462 {
463 struct file *file;
464 struct socket *sock;
465
466 *err = -EBADF;
467 file = fget_light(fd, fput_needed);
468 if (file) {
469 sock = sock_from_file(file, err);
470 if (sock)
471 return sock;
472 fput_light(file, *fput_needed);
473 }
474 return NULL;
475 }
476
477 /**
478 * sock_alloc - allocate a socket
479 *
480 * Allocate a new inode and socket object. The two are bound together
481 * and initialised. The socket is then returned. If we are out of inodes
482 * NULL is returned.
483 */
484
sock_alloc(void)485 static struct socket *sock_alloc(void)
486 {
487 struct inode *inode;
488 struct socket *sock;
489
490 inode = new_inode(sock_mnt->mnt_sb);
491 if (!inode)
492 return NULL;
493
494 sock = SOCKET_I(inode);
495
496 inode->i_mode = S_IFSOCK | S_IRWXUGO;
497 inode->i_uid = current_fsuid();
498 inode->i_gid = current_fsgid();
499
500 get_cpu_var(sockets_in_use)++;
501 put_cpu_var(sockets_in_use);
502 return sock;
503 }
504
505 /*
506 * In theory you can't get an open on this inode, but /proc provides
507 * a back door. Remember to keep it shut otherwise you'll let the
508 * creepy crawlies in.
509 */
510
sock_no_open(struct inode * irrelevant,struct file * dontcare)511 static int sock_no_open(struct inode *irrelevant, struct file *dontcare)
512 {
513 return -ENXIO;
514 }
515
516 const struct file_operations bad_sock_fops = {
517 .owner = THIS_MODULE,
518 .open = sock_no_open,
519 };
520
521 /**
522 * sock_release - close a socket
523 * @sock: socket to close
524 *
525 * The socket is released from the protocol stack if it has a release
526 * callback, and the inode is then released if the socket is bound to
527 * an inode not a file.
528 */
529
sock_release(struct socket * sock)530 void sock_release(struct socket *sock)
531 {
532 if (sock->ops) {
533 struct module *owner = sock->ops->owner;
534
535 sock->ops->release(sock);
536 sock->ops = NULL;
537 module_put(owner);
538 }
539
540 if (sock->fasync_list)
541 printk(KERN_ERR "sock_release: fasync list not empty!\n");
542
543 get_cpu_var(sockets_in_use)--;
544 put_cpu_var(sockets_in_use);
545 if (!sock->file) {
546 iput(SOCK_INODE(sock));
547 return;
548 }
549 sock->file = NULL;
550 }
551
__sock_sendmsg(struct kiocb * iocb,struct socket * sock,struct msghdr * msg,size_t size)552 static inline int __sock_sendmsg(struct kiocb *iocb, struct socket *sock,
553 struct msghdr *msg, size_t size)
554 {
555 struct sock_iocb *si = kiocb_to_siocb(iocb);
556 int err;
557
558 si->sock = sock;
559 si->scm = NULL;
560 si->msg = msg;
561 si->size = size;
562
563 err = security_socket_sendmsg(sock, msg, size);
564 if (err)
565 return err;
566
567 err = sock->ops->sendmsg(iocb, sock, msg, size);
568 #ifdef CONFIG_UID_STAT
569 if (err > 0)
570 update_tcp_snd(current_uid(), err);
571 #endif
572 return err;
573 }
574
sock_sendmsg(struct socket * sock,struct msghdr * msg,size_t size)575 int sock_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
576 {
577 struct kiocb iocb;
578 struct sock_iocb siocb;
579 int ret;
580
581 init_sync_kiocb(&iocb, NULL);
582 iocb.private = &siocb;
583 ret = __sock_sendmsg(&iocb, sock, msg, size);
584 if (-EIOCBQUEUED == ret)
585 ret = wait_on_sync_kiocb(&iocb);
586 return ret;
587 }
588
kernel_sendmsg(struct socket * sock,struct msghdr * msg,struct kvec * vec,size_t num,size_t size)589 int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
590 struct kvec *vec, size_t num, size_t size)
591 {
592 mm_segment_t oldfs = get_fs();
593 int result;
594
595 set_fs(KERNEL_DS);
596 /*
597 * the following is safe, since for compiler definitions of kvec and
598 * iovec are identical, yielding the same in-core layout and alignment
599 */
600 msg->msg_iov = (struct iovec *)vec;
601 msg->msg_iovlen = num;
602 result = sock_sendmsg(sock, msg, size);
603 set_fs(oldfs);
604 return result;
605 }
606
607 /*
608 * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
609 */
__sock_recv_timestamp(struct msghdr * msg,struct sock * sk,struct sk_buff * skb)610 void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
611 struct sk_buff *skb)
612 {
613 ktime_t kt = skb->tstamp;
614
615 if (!sock_flag(sk, SOCK_RCVTSTAMPNS)) {
616 struct timeval tv;
617 /* Race occurred between timestamp enabling and packet
618 receiving. Fill in the current time for now. */
619 if (kt.tv64 == 0)
620 kt = ktime_get_real();
621 skb->tstamp = kt;
622 tv = ktime_to_timeval(kt);
623 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMP, sizeof(tv), &tv);
624 } else {
625 struct timespec ts;
626 /* Race occurred between timestamp enabling and packet
627 receiving. Fill in the current time for now. */
628 if (kt.tv64 == 0)
629 kt = ktime_get_real();
630 skb->tstamp = kt;
631 ts = ktime_to_timespec(kt);
632 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPNS, sizeof(ts), &ts);
633 }
634 }
635
636 EXPORT_SYMBOL_GPL(__sock_recv_timestamp);
637
__sock_recvmsg(struct kiocb * iocb,struct socket * sock,struct msghdr * msg,size_t size,int flags)638 static inline int __sock_recvmsg(struct kiocb *iocb, struct socket *sock,
639 struct msghdr *msg, size_t size, int flags)
640 {
641 int err;
642 struct sock_iocb *si = kiocb_to_siocb(iocb);
643
644 si->sock = sock;
645 si->scm = NULL;
646 si->msg = msg;
647 si->size = size;
648 si->flags = flags;
649
650 err = security_socket_recvmsg(sock, msg, size, flags);
651 if (err)
652 return err;
653
654 err = sock->ops->recvmsg(iocb, sock, msg, size, flags);
655 #ifdef CONFIG_UID_STAT
656 if (err > 0)
657 update_tcp_rcv(current_uid(), err);
658 #endif
659 return err;
660 }
661
sock_recvmsg(struct socket * sock,struct msghdr * msg,size_t size,int flags)662 int sock_recvmsg(struct socket *sock, struct msghdr *msg,
663 size_t size, int flags)
664 {
665 struct kiocb iocb;
666 struct sock_iocb siocb;
667 int ret;
668
669 init_sync_kiocb(&iocb, NULL);
670 iocb.private = &siocb;
671 ret = __sock_recvmsg(&iocb, sock, msg, size, flags);
672 if (-EIOCBQUEUED == ret)
673 ret = wait_on_sync_kiocb(&iocb);
674 return ret;
675 }
676
kernel_recvmsg(struct socket * sock,struct msghdr * msg,struct kvec * vec,size_t num,size_t size,int flags)677 int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
678 struct kvec *vec, size_t num, size_t size, int flags)
679 {
680 mm_segment_t oldfs = get_fs();
681 int result;
682
683 set_fs(KERNEL_DS);
684 /*
685 * the following is safe, since for compiler definitions of kvec and
686 * iovec are identical, yielding the same in-core layout and alignment
687 */
688 msg->msg_iov = (struct iovec *)vec, msg->msg_iovlen = num;
689 result = sock_recvmsg(sock, msg, size, flags);
690 set_fs(oldfs);
691 return result;
692 }
693
sock_aio_dtor(struct kiocb * iocb)694 static void sock_aio_dtor(struct kiocb *iocb)
695 {
696 kfree(iocb->private);
697 }
698
sock_sendpage(struct file * file,struct page * page,int offset,size_t size,loff_t * ppos,int more)699 static ssize_t sock_sendpage(struct file *file, struct page *page,
700 int offset, size_t size, loff_t *ppos, int more)
701 {
702 struct socket *sock;
703 int flags;
704
705 sock = file->private_data;
706
707 flags = !(file->f_flags & O_NONBLOCK) ? 0 : MSG_DONTWAIT;
708 if (more)
709 flags |= MSG_MORE;
710
711 return kernel_sendpage(sock, page, offset, size, flags);
712 }
713
sock_splice_read(struct file * file,loff_t * ppos,struct pipe_inode_info * pipe,size_t len,unsigned int flags)714 static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
715 struct pipe_inode_info *pipe, size_t len,
716 unsigned int flags)
717 {
718 struct socket *sock = file->private_data;
719
720 if (unlikely(!sock->ops->splice_read))
721 return -EINVAL;
722
723 return sock->ops->splice_read(sock, ppos, pipe, len, flags);
724 }
725
alloc_sock_iocb(struct kiocb * iocb,struct sock_iocb * siocb)726 static struct sock_iocb *alloc_sock_iocb(struct kiocb *iocb,
727 struct sock_iocb *siocb)
728 {
729 if (!is_sync_kiocb(iocb)) {
730 siocb = kmalloc(sizeof(*siocb), GFP_KERNEL);
731 if (!siocb)
732 return NULL;
733 iocb->ki_dtor = sock_aio_dtor;
734 }
735
736 siocb->kiocb = iocb;
737 iocb->private = siocb;
738 return siocb;
739 }
740
do_sock_read(struct msghdr * msg,struct kiocb * iocb,struct file * file,const struct iovec * iov,unsigned long nr_segs)741 static ssize_t do_sock_read(struct msghdr *msg, struct kiocb *iocb,
742 struct file *file, const struct iovec *iov,
743 unsigned long nr_segs)
744 {
745 struct socket *sock = file->private_data;
746 size_t size = 0;
747 int i;
748
749 for (i = 0; i < nr_segs; i++)
750 size += iov[i].iov_len;
751
752 msg->msg_name = NULL;
753 msg->msg_namelen = 0;
754 msg->msg_control = NULL;
755 msg->msg_controllen = 0;
756 msg->msg_iov = (struct iovec *)iov;
757 msg->msg_iovlen = nr_segs;
758 msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
759
760 return __sock_recvmsg(iocb, sock, msg, size, msg->msg_flags);
761 }
762
sock_aio_read(struct kiocb * iocb,const struct iovec * iov,unsigned long nr_segs,loff_t pos)763 static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
764 unsigned long nr_segs, loff_t pos)
765 {
766 struct sock_iocb siocb, *x;
767
768 if (pos != 0)
769 return -ESPIPE;
770
771 if (iocb->ki_left == 0) /* Match SYS5 behaviour */
772 return 0;
773
774
775 x = alloc_sock_iocb(iocb, &siocb);
776 if (!x)
777 return -ENOMEM;
778 return do_sock_read(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
779 }
780
do_sock_write(struct msghdr * msg,struct kiocb * iocb,struct file * file,const struct iovec * iov,unsigned long nr_segs)781 static ssize_t do_sock_write(struct msghdr *msg, struct kiocb *iocb,
782 struct file *file, const struct iovec *iov,
783 unsigned long nr_segs)
784 {
785 struct socket *sock = file->private_data;
786 size_t size = 0;
787 int i;
788
789 for (i = 0; i < nr_segs; i++)
790 size += iov[i].iov_len;
791
792 msg->msg_name = NULL;
793 msg->msg_namelen = 0;
794 msg->msg_control = NULL;
795 msg->msg_controllen = 0;
796 msg->msg_iov = (struct iovec *)iov;
797 msg->msg_iovlen = nr_segs;
798 msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
799 if (sock->type == SOCK_SEQPACKET)
800 msg->msg_flags |= MSG_EOR;
801
802 return __sock_sendmsg(iocb, sock, msg, size);
803 }
804
sock_aio_write(struct kiocb * iocb,const struct iovec * iov,unsigned long nr_segs,loff_t pos)805 static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov,
806 unsigned long nr_segs, loff_t pos)
807 {
808 struct sock_iocb siocb, *x;
809
810 if (pos != 0)
811 return -ESPIPE;
812
813 x = alloc_sock_iocb(iocb, &siocb);
814 if (!x)
815 return -ENOMEM;
816
817 return do_sock_write(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
818 }
819
820 /*
821 * Atomic setting of ioctl hooks to avoid race
822 * with module unload.
823 */
824
825 static DEFINE_MUTEX(br_ioctl_mutex);
826 static int (*br_ioctl_hook) (struct net *, unsigned int cmd, void __user *arg) = NULL;
827
brioctl_set(int (* hook)(struct net *,unsigned int,void __user *))828 void brioctl_set(int (*hook) (struct net *, unsigned int, void __user *))
829 {
830 mutex_lock(&br_ioctl_mutex);
831 br_ioctl_hook = hook;
832 mutex_unlock(&br_ioctl_mutex);
833 }
834
835 EXPORT_SYMBOL(brioctl_set);
836
837 static DEFINE_MUTEX(vlan_ioctl_mutex);
838 static int (*vlan_ioctl_hook) (struct net *, void __user *arg);
839
vlan_ioctl_set(int (* hook)(struct net *,void __user *))840 void vlan_ioctl_set(int (*hook) (struct net *, void __user *))
841 {
842 mutex_lock(&vlan_ioctl_mutex);
843 vlan_ioctl_hook = hook;
844 mutex_unlock(&vlan_ioctl_mutex);
845 }
846
847 EXPORT_SYMBOL(vlan_ioctl_set);
848
849 static DEFINE_MUTEX(dlci_ioctl_mutex);
850 static int (*dlci_ioctl_hook) (unsigned int, void __user *);
851
dlci_ioctl_set(int (* hook)(unsigned int,void __user *))852 void dlci_ioctl_set(int (*hook) (unsigned int, void __user *))
853 {
854 mutex_lock(&dlci_ioctl_mutex);
855 dlci_ioctl_hook = hook;
856 mutex_unlock(&dlci_ioctl_mutex);
857 }
858
859 EXPORT_SYMBOL(dlci_ioctl_set);
860
861 /*
862 * With an ioctl, arg may well be a user mode pointer, but we don't know
863 * what to do with it - that's up to the protocol still.
864 */
865
sock_ioctl(struct file * file,unsigned cmd,unsigned long arg)866 static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
867 {
868 struct socket *sock;
869 struct sock *sk;
870 void __user *argp = (void __user *)arg;
871 int pid, err;
872 struct net *net;
873
874 sock = file->private_data;
875 sk = sock->sk;
876 net = sock_net(sk);
877 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
878 err = dev_ioctl(net, cmd, argp);
879 } else
880 #ifdef CONFIG_WIRELESS_EXT
881 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
882 err = dev_ioctl(net, cmd, argp);
883 } else
884 #endif /* CONFIG_WIRELESS_EXT */
885 switch (cmd) {
886 case FIOSETOWN:
887 case SIOCSPGRP:
888 err = -EFAULT;
889 if (get_user(pid, (int __user *)argp))
890 break;
891 err = f_setown(sock->file, pid, 1);
892 break;
893 case FIOGETOWN:
894 case SIOCGPGRP:
895 err = put_user(f_getown(sock->file),
896 (int __user *)argp);
897 break;
898 case SIOCGIFBR:
899 case SIOCSIFBR:
900 case SIOCBRADDBR:
901 case SIOCBRDELBR:
902 err = -ENOPKG;
903 if (!br_ioctl_hook)
904 request_module("bridge");
905
906 mutex_lock(&br_ioctl_mutex);
907 if (br_ioctl_hook)
908 err = br_ioctl_hook(net, cmd, argp);
909 mutex_unlock(&br_ioctl_mutex);
910 break;
911 case SIOCGIFVLAN:
912 case SIOCSIFVLAN:
913 err = -ENOPKG;
914 if (!vlan_ioctl_hook)
915 request_module("8021q");
916
917 mutex_lock(&vlan_ioctl_mutex);
918 if (vlan_ioctl_hook)
919 err = vlan_ioctl_hook(net, argp);
920 mutex_unlock(&vlan_ioctl_mutex);
921 break;
922 case SIOCADDDLCI:
923 case SIOCDELDLCI:
924 err = -ENOPKG;
925 if (!dlci_ioctl_hook)
926 request_module("dlci");
927
928 mutex_lock(&dlci_ioctl_mutex);
929 if (dlci_ioctl_hook)
930 err = dlci_ioctl_hook(cmd, argp);
931 mutex_unlock(&dlci_ioctl_mutex);
932 break;
933 default:
934 err = sock->ops->ioctl(sock, cmd, arg);
935
936 /*
937 * If this ioctl is unknown try to hand it down
938 * to the NIC driver.
939 */
940 if (err == -ENOIOCTLCMD)
941 err = dev_ioctl(net, cmd, argp);
942 break;
943 }
944 return err;
945 }
946
sock_create_lite(int family,int type,int protocol,struct socket ** res)947 int sock_create_lite(int family, int type, int protocol, struct socket **res)
948 {
949 int err;
950 struct socket *sock = NULL;
951
952 err = security_socket_create(family, type, protocol, 1);
953 if (err)
954 goto out;
955
956 sock = sock_alloc();
957 if (!sock) {
958 err = -ENOMEM;
959 goto out;
960 }
961
962 sock->type = type;
963 err = security_socket_post_create(sock, family, type, protocol, 1);
964 if (err)
965 goto out_release;
966
967 out:
968 *res = sock;
969 return err;
970 out_release:
971 sock_release(sock);
972 sock = NULL;
973 goto out;
974 }
975
976 /* No kernel lock held - perfect */
sock_poll(struct file * file,poll_table * wait)977 static unsigned int sock_poll(struct file *file, poll_table *wait)
978 {
979 struct socket *sock;
980
981 /*
982 * We can't return errors to poll, so it's either yes or no.
983 */
984 sock = file->private_data;
985 return sock->ops->poll(file, sock, wait);
986 }
987
sock_mmap(struct file * file,struct vm_area_struct * vma)988 static int sock_mmap(struct file *file, struct vm_area_struct *vma)
989 {
990 struct socket *sock = file->private_data;
991
992 return sock->ops->mmap(file, sock, vma);
993 }
994
sock_close(struct inode * inode,struct file * filp)995 static int sock_close(struct inode *inode, struct file *filp)
996 {
997 /*
998 * It was possible the inode is NULL we were
999 * closing an unfinished socket.
1000 */
1001
1002 if (!inode) {
1003 printk(KERN_DEBUG "sock_close: NULL inode\n");
1004 return 0;
1005 }
1006 sock_release(SOCKET_I(inode));
1007 return 0;
1008 }
1009
1010 /*
1011 * Update the socket async list
1012 *
1013 * Fasync_list locking strategy.
1014 *
1015 * 1. fasync_list is modified only under process context socket lock
1016 * i.e. under semaphore.
1017 * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
1018 * or under socket lock.
1019 * 3. fasync_list can be used from softirq context, so that
1020 * modification under socket lock have to be enhanced with
1021 * write_lock_bh(&sk->sk_callback_lock).
1022 * --ANK (990710)
1023 */
1024
sock_fasync(int fd,struct file * filp,int on)1025 static int sock_fasync(int fd, struct file *filp, int on)
1026 {
1027 struct fasync_struct *fa, *fna = NULL, **prev;
1028 struct socket *sock;
1029 struct sock *sk;
1030
1031 if (on) {
1032 fna = kmalloc(sizeof(struct fasync_struct), GFP_KERNEL);
1033 if (fna == NULL)
1034 return -ENOMEM;
1035 }
1036
1037 sock = filp->private_data;
1038
1039 sk = sock->sk;
1040 if (sk == NULL) {
1041 kfree(fna);
1042 return -EINVAL;
1043 }
1044
1045 lock_sock(sk);
1046
1047 prev = &(sock->fasync_list);
1048
1049 for (fa = *prev; fa != NULL; prev = &fa->fa_next, fa = *prev)
1050 if (fa->fa_file == filp)
1051 break;
1052
1053 if (on) {
1054 if (fa != NULL) {
1055 write_lock_bh(&sk->sk_callback_lock);
1056 fa->fa_fd = fd;
1057 write_unlock_bh(&sk->sk_callback_lock);
1058
1059 kfree(fna);
1060 goto out;
1061 }
1062 fna->fa_file = filp;
1063 fna->fa_fd = fd;
1064 fna->magic = FASYNC_MAGIC;
1065 fna->fa_next = sock->fasync_list;
1066 write_lock_bh(&sk->sk_callback_lock);
1067 sock->fasync_list = fna;
1068 write_unlock_bh(&sk->sk_callback_lock);
1069 } else {
1070 if (fa != NULL) {
1071 write_lock_bh(&sk->sk_callback_lock);
1072 *prev = fa->fa_next;
1073 write_unlock_bh(&sk->sk_callback_lock);
1074 kfree(fa);
1075 }
1076 }
1077
1078 out:
1079 release_sock(sock->sk);
1080 return 0;
1081 }
1082
1083 /* This function may be called only under socket lock or callback_lock */
1084
sock_wake_async(struct socket * sock,int how,int band)1085 int sock_wake_async(struct socket *sock, int how, int band)
1086 {
1087 if (!sock || !sock->fasync_list)
1088 return -1;
1089 switch (how) {
1090 case SOCK_WAKE_WAITD:
1091 if (test_bit(SOCK_ASYNC_WAITDATA, &sock->flags))
1092 break;
1093 goto call_kill;
1094 case SOCK_WAKE_SPACE:
1095 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags))
1096 break;
1097 /* fall through */
1098 case SOCK_WAKE_IO:
1099 call_kill:
1100 __kill_fasync(sock->fasync_list, SIGIO, band);
1101 break;
1102 case SOCK_WAKE_URG:
1103 __kill_fasync(sock->fasync_list, SIGURG, band);
1104 }
1105 return 0;
1106 }
1107
__sock_create(struct net * net,int family,int type,int protocol,struct socket ** res,int kern)1108 static int __sock_create(struct net *net, int family, int type, int protocol,
1109 struct socket **res, int kern)
1110 {
1111 int err;
1112 struct socket *sock;
1113 const struct net_proto_family *pf;
1114
1115 /*
1116 * Check protocol is in range
1117 */
1118 if (family < 0 || family >= NPROTO)
1119 return -EAFNOSUPPORT;
1120 if (type < 0 || type >= SOCK_MAX)
1121 return -EINVAL;
1122
1123 /* Compatibility.
1124
1125 This uglymoron is moved from INET layer to here to avoid
1126 deadlock in module load.
1127 */
1128 if (family == PF_INET && type == SOCK_PACKET) {
1129 static int warned;
1130 if (!warned) {
1131 warned = 1;
1132 printk(KERN_INFO "%s uses obsolete (PF_INET,SOCK_PACKET)\n",
1133 current->comm);
1134 }
1135 family = PF_PACKET;
1136 }
1137
1138 err = security_socket_create(family, type, protocol, kern);
1139 if (err)
1140 return err;
1141
1142 /*
1143 * Allocate the socket and allow the family to set things up. if
1144 * the protocol is 0, the family is instructed to select an appropriate
1145 * default.
1146 */
1147 sock = sock_alloc();
1148 if (!sock) {
1149 if (net_ratelimit())
1150 printk(KERN_WARNING "socket: no more sockets\n");
1151 return -ENFILE; /* Not exactly a match, but its the
1152 closest posix thing */
1153 }
1154
1155 sock->type = type;
1156
1157 #ifdef CONFIG_MODULES
1158 /* Attempt to load a protocol module if the find failed.
1159 *
1160 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
1161 * requested real, full-featured networking support upon configuration.
1162 * Otherwise module support will break!
1163 */
1164 if (net_families[family] == NULL)
1165 request_module("net-pf-%d", family);
1166 #endif
1167
1168 rcu_read_lock();
1169 pf = rcu_dereference(net_families[family]);
1170 err = -EAFNOSUPPORT;
1171 if (!pf)
1172 goto out_release;
1173
1174 /*
1175 * We will call the ->create function, that possibly is in a loadable
1176 * module, so we have to bump that loadable module refcnt first.
1177 */
1178 if (!try_module_get(pf->owner))
1179 goto out_release;
1180
1181 /* Now protected by module ref count */
1182 rcu_read_unlock();
1183
1184 err = pf->create(net, sock, protocol);
1185 if (err < 0)
1186 goto out_module_put;
1187
1188 /*
1189 * Now to bump the refcnt of the [loadable] module that owns this
1190 * socket at sock_release time we decrement its refcnt.
1191 */
1192 if (!try_module_get(sock->ops->owner))
1193 goto out_module_busy;
1194
1195 /*
1196 * Now that we're done with the ->create function, the [loadable]
1197 * module can have its refcnt decremented
1198 */
1199 module_put(pf->owner);
1200 err = security_socket_post_create(sock, family, type, protocol, kern);
1201 if (err)
1202 goto out_sock_release;
1203 *res = sock;
1204
1205 return 0;
1206
1207 out_module_busy:
1208 err = -EAFNOSUPPORT;
1209 out_module_put:
1210 sock->ops = NULL;
1211 module_put(pf->owner);
1212 out_sock_release:
1213 sock_release(sock);
1214 return err;
1215
1216 out_release:
1217 rcu_read_unlock();
1218 goto out_sock_release;
1219 }
1220
sock_create(int family,int type,int protocol,struct socket ** res)1221 int sock_create(int family, int type, int protocol, struct socket **res)
1222 {
1223 return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0);
1224 }
1225
sock_create_kern(int family,int type,int protocol,struct socket ** res)1226 int sock_create_kern(int family, int type, int protocol, struct socket **res)
1227 {
1228 return __sock_create(&init_net, family, type, protocol, res, 1);
1229 }
1230
SYSCALL_DEFINE3(socket,int,family,int,type,int,protocol)1231 SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol)
1232 {
1233 int retval;
1234 struct socket *sock;
1235 int flags;
1236
1237 /* Check the SOCK_* constants for consistency. */
1238 BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC);
1239 BUILD_BUG_ON((SOCK_MAX | SOCK_TYPE_MASK) != SOCK_TYPE_MASK);
1240 BUILD_BUG_ON(SOCK_CLOEXEC & SOCK_TYPE_MASK);
1241 BUILD_BUG_ON(SOCK_NONBLOCK & SOCK_TYPE_MASK);
1242
1243 flags = type & ~SOCK_TYPE_MASK;
1244 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1245 return -EINVAL;
1246 type &= SOCK_TYPE_MASK;
1247
1248 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1249 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1250
1251 retval = sock_create(family, type, protocol, &sock);
1252 if (retval < 0)
1253 goto out;
1254
1255 retval = sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK));
1256 if (retval < 0)
1257 goto out_release;
1258
1259 out:
1260 /* It may be already another descriptor 8) Not kernel problem. */
1261 return retval;
1262
1263 out_release:
1264 sock_release(sock);
1265 return retval;
1266 }
1267
1268 /*
1269 * Create a pair of connected sockets.
1270 */
1271
SYSCALL_DEFINE4(socketpair,int,family,int,type,int,protocol,int __user *,usockvec)1272 SYSCALL_DEFINE4(socketpair, int, family, int, type, int, protocol,
1273 int __user *, usockvec)
1274 {
1275 struct socket *sock1, *sock2;
1276 int fd1, fd2, err;
1277 struct file *newfile1, *newfile2;
1278 int flags;
1279
1280 flags = type & ~SOCK_TYPE_MASK;
1281 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1282 return -EINVAL;
1283 type &= SOCK_TYPE_MASK;
1284
1285 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1286 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1287
1288 /*
1289 * Obtain the first socket and check if the underlying protocol
1290 * supports the socketpair call.
1291 */
1292
1293 err = sock_create(family, type, protocol, &sock1);
1294 if (err < 0)
1295 goto out;
1296
1297 err = sock_create(family, type, protocol, &sock2);
1298 if (err < 0)
1299 goto out_release_1;
1300
1301 err = sock1->ops->socketpair(sock1, sock2);
1302 if (err < 0)
1303 goto out_release_both;
1304
1305 fd1 = sock_alloc_fd(&newfile1, flags & O_CLOEXEC);
1306 if (unlikely(fd1 < 0)) {
1307 err = fd1;
1308 goto out_release_both;
1309 }
1310
1311 fd2 = sock_alloc_fd(&newfile2, flags & O_CLOEXEC);
1312 if (unlikely(fd2 < 0)) {
1313 err = fd2;
1314 put_filp(newfile1);
1315 put_unused_fd(fd1);
1316 goto out_release_both;
1317 }
1318
1319 err = sock_attach_fd(sock1, newfile1, flags & O_NONBLOCK);
1320 if (unlikely(err < 0)) {
1321 goto out_fd2;
1322 }
1323
1324 err = sock_attach_fd(sock2, newfile2, flags & O_NONBLOCK);
1325 if (unlikely(err < 0)) {
1326 fput(newfile1);
1327 goto out_fd1;
1328 }
1329
1330 audit_fd_pair(fd1, fd2);
1331 fd_install(fd1, newfile1);
1332 fd_install(fd2, newfile2);
1333 /* fd1 and fd2 may be already another descriptors.
1334 * Not kernel problem.
1335 */
1336
1337 err = put_user(fd1, &usockvec[0]);
1338 if (!err)
1339 err = put_user(fd2, &usockvec[1]);
1340 if (!err)
1341 return 0;
1342
1343 sys_close(fd2);
1344 sys_close(fd1);
1345 return err;
1346
1347 out_release_both:
1348 sock_release(sock2);
1349 out_release_1:
1350 sock_release(sock1);
1351 out:
1352 return err;
1353
1354 out_fd2:
1355 put_filp(newfile1);
1356 sock_release(sock1);
1357 out_fd1:
1358 put_filp(newfile2);
1359 sock_release(sock2);
1360 put_unused_fd(fd1);
1361 put_unused_fd(fd2);
1362 goto out;
1363 }
1364
1365 /*
1366 * Bind a name to a socket. Nothing much to do here since it's
1367 * the protocol's responsibility to handle the local address.
1368 *
1369 * We move the socket address to kernel space before we call
1370 * the protocol layer (having also checked the address is ok).
1371 */
1372
SYSCALL_DEFINE3(bind,int,fd,struct sockaddr __user *,umyaddr,int,addrlen)1373 SYSCALL_DEFINE3(bind, int, fd, struct sockaddr __user *, umyaddr, int, addrlen)
1374 {
1375 struct socket *sock;
1376 struct sockaddr_storage address;
1377 int err, fput_needed;
1378
1379 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1380 if (sock) {
1381 err = move_addr_to_kernel(umyaddr, addrlen, (struct sockaddr *)&address);
1382 if (err >= 0) {
1383 err = security_socket_bind(sock,
1384 (struct sockaddr *)&address,
1385 addrlen);
1386 if (!err)
1387 err = sock->ops->bind(sock,
1388 (struct sockaddr *)
1389 &address, addrlen);
1390 }
1391 fput_light(sock->file, fput_needed);
1392 }
1393 return err;
1394 }
1395
1396 /*
1397 * Perform a listen. Basically, we allow the protocol to do anything
1398 * necessary for a listen, and if that works, we mark the socket as
1399 * ready for listening.
1400 */
1401
SYSCALL_DEFINE2(listen,int,fd,int,backlog)1402 SYSCALL_DEFINE2(listen, int, fd, int, backlog)
1403 {
1404 struct socket *sock;
1405 int err, fput_needed;
1406 int somaxconn;
1407
1408 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1409 if (sock) {
1410 somaxconn = sock_net(sock->sk)->core.sysctl_somaxconn;
1411 if ((unsigned)backlog > somaxconn)
1412 backlog = somaxconn;
1413
1414 err = security_socket_listen(sock, backlog);
1415 if (!err)
1416 err = sock->ops->listen(sock, backlog);
1417
1418 fput_light(sock->file, fput_needed);
1419 }
1420 return err;
1421 }
1422
1423 /*
1424 * For accept, we attempt to create a new socket, set up the link
1425 * with the client, wake up the client, then return the new
1426 * connected fd. We collect the address of the connector in kernel
1427 * space and move it to user at the very end. This is unclean because
1428 * we open the socket then return an error.
1429 *
1430 * 1003.1g adds the ability to recvmsg() to query connection pending
1431 * status to recvmsg. We need to add that support in a way thats
1432 * clean when we restucture accept also.
1433 */
1434
SYSCALL_DEFINE4(accept4,int,fd,struct sockaddr __user *,upeer_sockaddr,int __user *,upeer_addrlen,int,flags)1435 SYSCALL_DEFINE4(accept4, int, fd, struct sockaddr __user *, upeer_sockaddr,
1436 int __user *, upeer_addrlen, int, flags)
1437 {
1438 struct socket *sock, *newsock;
1439 struct file *newfile;
1440 int err, len, newfd, fput_needed;
1441 struct sockaddr_storage address;
1442
1443 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1444 return -EINVAL;
1445
1446 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1447 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1448
1449 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1450 if (!sock)
1451 goto out;
1452
1453 err = -ENFILE;
1454 if (!(newsock = sock_alloc()))
1455 goto out_put;
1456
1457 newsock->type = sock->type;
1458 newsock->ops = sock->ops;
1459
1460 /*
1461 * We don't need try_module_get here, as the listening socket (sock)
1462 * has the protocol module (sock->ops->owner) held.
1463 */
1464 __module_get(newsock->ops->owner);
1465
1466 newfd = sock_alloc_fd(&newfile, flags & O_CLOEXEC);
1467 if (unlikely(newfd < 0)) {
1468 err = newfd;
1469 sock_release(newsock);
1470 goto out_put;
1471 }
1472
1473 err = sock_attach_fd(newsock, newfile, flags & O_NONBLOCK);
1474 if (err < 0)
1475 goto out_fd_simple;
1476
1477 err = security_socket_accept(sock, newsock);
1478 if (err)
1479 goto out_fd;
1480
1481 err = sock->ops->accept(sock, newsock, sock->file->f_flags);
1482 if (err < 0)
1483 goto out_fd;
1484
1485 if (upeer_sockaddr) {
1486 if (newsock->ops->getname(newsock, (struct sockaddr *)&address,
1487 &len, 2) < 0) {
1488 err = -ECONNABORTED;
1489 goto out_fd;
1490 }
1491 err = move_addr_to_user((struct sockaddr *)&address,
1492 len, upeer_sockaddr, upeer_addrlen);
1493 if (err < 0)
1494 goto out_fd;
1495 }
1496
1497 /* File flags are not inherited via accept() unlike another OSes. */
1498
1499 fd_install(newfd, newfile);
1500 err = newfd;
1501
1502 security_socket_post_accept(sock, newsock);
1503
1504 out_put:
1505 fput_light(sock->file, fput_needed);
1506 out:
1507 return err;
1508 out_fd_simple:
1509 sock_release(newsock);
1510 put_filp(newfile);
1511 put_unused_fd(newfd);
1512 goto out_put;
1513 out_fd:
1514 fput(newfile);
1515 put_unused_fd(newfd);
1516 goto out_put;
1517 }
1518
SYSCALL_DEFINE3(accept,int,fd,struct sockaddr __user *,upeer_sockaddr,int __user *,upeer_addrlen)1519 SYSCALL_DEFINE3(accept, int, fd, struct sockaddr __user *, upeer_sockaddr,
1520 int __user *, upeer_addrlen)
1521 {
1522 return sys_accept4(fd, upeer_sockaddr, upeer_addrlen, 0);
1523 }
1524
1525 /*
1526 * Attempt to connect to a socket with the server address. The address
1527 * is in user space so we verify it is OK and move it to kernel space.
1528 *
1529 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
1530 * break bindings
1531 *
1532 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
1533 * other SEQPACKET protocols that take time to connect() as it doesn't
1534 * include the -EINPROGRESS status for such sockets.
1535 */
1536
SYSCALL_DEFINE3(connect,int,fd,struct sockaddr __user *,uservaddr,int,addrlen)1537 SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
1538 int, addrlen)
1539 {
1540 struct socket *sock;
1541 struct sockaddr_storage address;
1542 int err, fput_needed;
1543
1544 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1545 if (!sock)
1546 goto out;
1547 err = move_addr_to_kernel(uservaddr, addrlen, (struct sockaddr *)&address);
1548 if (err < 0)
1549 goto out_put;
1550
1551 err =
1552 security_socket_connect(sock, (struct sockaddr *)&address, addrlen);
1553 if (err)
1554 goto out_put;
1555
1556 err = sock->ops->connect(sock, (struct sockaddr *)&address, addrlen,
1557 sock->file->f_flags);
1558 out_put:
1559 fput_light(sock->file, fput_needed);
1560 out:
1561 return err;
1562 }
1563
1564 /*
1565 * Get the local address ('name') of a socket object. Move the obtained
1566 * name to user space.
1567 */
1568
SYSCALL_DEFINE3(getsockname,int,fd,struct sockaddr __user *,usockaddr,int __user *,usockaddr_len)1569 SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr,
1570 int __user *, usockaddr_len)
1571 {
1572 struct socket *sock;
1573 struct sockaddr_storage address;
1574 int len, err, fput_needed;
1575
1576 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1577 if (!sock)
1578 goto out;
1579
1580 err = security_socket_getsockname(sock);
1581 if (err)
1582 goto out_put;
1583
1584 err = sock->ops->getname(sock, (struct sockaddr *)&address, &len, 0);
1585 if (err)
1586 goto out_put;
1587 err = move_addr_to_user((struct sockaddr *)&address, len, usockaddr, usockaddr_len);
1588
1589 out_put:
1590 fput_light(sock->file, fput_needed);
1591 out:
1592 return err;
1593 }
1594
1595 /*
1596 * Get the remote address ('name') of a socket object. Move the obtained
1597 * name to user space.
1598 */
1599
SYSCALL_DEFINE3(getpeername,int,fd,struct sockaddr __user *,usockaddr,int __user *,usockaddr_len)1600 SYSCALL_DEFINE3(getpeername, int, fd, struct sockaddr __user *, usockaddr,
1601 int __user *, usockaddr_len)
1602 {
1603 struct socket *sock;
1604 struct sockaddr_storage address;
1605 int len, err, fput_needed;
1606
1607 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1608 if (sock != NULL) {
1609 err = security_socket_getpeername(sock);
1610 if (err) {
1611 fput_light(sock->file, fput_needed);
1612 return err;
1613 }
1614
1615 err =
1616 sock->ops->getname(sock, (struct sockaddr *)&address, &len,
1617 1);
1618 if (!err)
1619 err = move_addr_to_user((struct sockaddr *)&address, len, usockaddr,
1620 usockaddr_len);
1621 fput_light(sock->file, fput_needed);
1622 }
1623 return err;
1624 }
1625
1626 /*
1627 * Send a datagram to a given address. We move the address into kernel
1628 * space and check the user space data area is readable before invoking
1629 * the protocol.
1630 */
1631
SYSCALL_DEFINE6(sendto,int,fd,void __user *,buff,size_t,len,unsigned,flags,struct sockaddr __user *,addr,int,addr_len)1632 SYSCALL_DEFINE6(sendto, int, fd, void __user *, buff, size_t, len,
1633 unsigned, flags, struct sockaddr __user *, addr,
1634 int, addr_len)
1635 {
1636 struct socket *sock;
1637 struct sockaddr_storage address;
1638 int err;
1639 struct msghdr msg;
1640 struct iovec iov;
1641 int fput_needed;
1642
1643 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1644 if (!sock)
1645 goto out;
1646
1647 iov.iov_base = buff;
1648 iov.iov_len = len;
1649 msg.msg_name = NULL;
1650 msg.msg_iov = &iov;
1651 msg.msg_iovlen = 1;
1652 msg.msg_control = NULL;
1653 msg.msg_controllen = 0;
1654 msg.msg_namelen = 0;
1655 if (addr) {
1656 err = move_addr_to_kernel(addr, addr_len, (struct sockaddr *)&address);
1657 if (err < 0)
1658 goto out_put;
1659 msg.msg_name = (struct sockaddr *)&address;
1660 msg.msg_namelen = addr_len;
1661 }
1662 if (sock->file->f_flags & O_NONBLOCK)
1663 flags |= MSG_DONTWAIT;
1664 msg.msg_flags = flags;
1665 err = sock_sendmsg(sock, &msg, len);
1666
1667 out_put:
1668 fput_light(sock->file, fput_needed);
1669 out:
1670 return err;
1671 }
1672
1673 /*
1674 * Send a datagram down a socket.
1675 */
1676
SYSCALL_DEFINE4(send,int,fd,void __user *,buff,size_t,len,unsigned,flags)1677 SYSCALL_DEFINE4(send, int, fd, void __user *, buff, size_t, len,
1678 unsigned, flags)
1679 {
1680 return sys_sendto(fd, buff, len, flags, NULL, 0);
1681 }
1682
1683 /*
1684 * Receive a frame from the socket and optionally record the address of the
1685 * sender. We verify the buffers are writable and if needed move the
1686 * sender address from kernel to user space.
1687 */
1688
SYSCALL_DEFINE6(recvfrom,int,fd,void __user *,ubuf,size_t,size,unsigned,flags,struct sockaddr __user *,addr,int __user *,addr_len)1689 SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, ubuf, size_t, size,
1690 unsigned, flags, struct sockaddr __user *, addr,
1691 int __user *, addr_len)
1692 {
1693 struct socket *sock;
1694 struct iovec iov;
1695 struct msghdr msg;
1696 struct sockaddr_storage address;
1697 int err, err2;
1698 int fput_needed;
1699
1700 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1701 if (!sock)
1702 goto out;
1703
1704 msg.msg_control = NULL;
1705 msg.msg_controllen = 0;
1706 msg.msg_iovlen = 1;
1707 msg.msg_iov = &iov;
1708 iov.iov_len = size;
1709 iov.iov_base = ubuf;
1710 msg.msg_name = (struct sockaddr *)&address;
1711 msg.msg_namelen = sizeof(address);
1712 if (sock->file->f_flags & O_NONBLOCK)
1713 flags |= MSG_DONTWAIT;
1714 err = sock_recvmsg(sock, &msg, size, flags);
1715
1716 if (err >= 0 && addr != NULL) {
1717 err2 = move_addr_to_user((struct sockaddr *)&address,
1718 msg.msg_namelen, addr, addr_len);
1719 if (err2 < 0)
1720 err = err2;
1721 }
1722
1723 fput_light(sock->file, fput_needed);
1724 out:
1725 return err;
1726 }
1727
1728 /*
1729 * Receive a datagram from a socket.
1730 */
1731
sys_recv(int fd,void __user * ubuf,size_t size,unsigned flags)1732 asmlinkage long sys_recv(int fd, void __user *ubuf, size_t size,
1733 unsigned flags)
1734 {
1735 return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
1736 }
1737
1738 /*
1739 * Set a socket option. Because we don't know the option lengths we have
1740 * to pass the user mode parameter for the protocols to sort out.
1741 */
1742
SYSCALL_DEFINE5(setsockopt,int,fd,int,level,int,optname,char __user *,optval,int,optlen)1743 SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,
1744 char __user *, optval, int, optlen)
1745 {
1746 int err, fput_needed;
1747 struct socket *sock;
1748
1749 if (optlen < 0)
1750 return -EINVAL;
1751
1752 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1753 if (sock != NULL) {
1754 err = security_socket_setsockopt(sock, level, optname);
1755 if (err)
1756 goto out_put;
1757
1758 if (level == SOL_SOCKET)
1759 err =
1760 sock_setsockopt(sock, level, optname, optval,
1761 optlen);
1762 else
1763 err =
1764 sock->ops->setsockopt(sock, level, optname, optval,
1765 optlen);
1766 out_put:
1767 fput_light(sock->file, fput_needed);
1768 }
1769 return err;
1770 }
1771
1772 /*
1773 * Get a socket option. Because we don't know the option lengths we have
1774 * to pass a user mode parameter for the protocols to sort out.
1775 */
1776
SYSCALL_DEFINE5(getsockopt,int,fd,int,level,int,optname,char __user *,optval,int __user *,optlen)1777 SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname,
1778 char __user *, optval, int __user *, optlen)
1779 {
1780 int err, fput_needed;
1781 struct socket *sock;
1782
1783 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1784 if (sock != NULL) {
1785 err = security_socket_getsockopt(sock, level, optname);
1786 if (err)
1787 goto out_put;
1788
1789 if (level == SOL_SOCKET)
1790 err =
1791 sock_getsockopt(sock, level, optname, optval,
1792 optlen);
1793 else
1794 err =
1795 sock->ops->getsockopt(sock, level, optname, optval,
1796 optlen);
1797 out_put:
1798 fput_light(sock->file, fput_needed);
1799 }
1800 return err;
1801 }
1802
1803 /*
1804 * Shutdown a socket.
1805 */
1806
SYSCALL_DEFINE2(shutdown,int,fd,int,how)1807 SYSCALL_DEFINE2(shutdown, int, fd, int, how)
1808 {
1809 int err, fput_needed;
1810 struct socket *sock;
1811
1812 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1813 if (sock != NULL) {
1814 err = security_socket_shutdown(sock, how);
1815 if (!err)
1816 err = sock->ops->shutdown(sock, how);
1817 fput_light(sock->file, fput_needed);
1818 }
1819 return err;
1820 }
1821
1822 /* A couple of helpful macros for getting the address of the 32/64 bit
1823 * fields which are the same type (int / unsigned) on our platforms.
1824 */
1825 #define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
1826 #define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
1827 #define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
1828
1829 /*
1830 * BSD sendmsg interface
1831 */
1832
SYSCALL_DEFINE3(sendmsg,int,fd,struct msghdr __user *,msg,unsigned,flags)1833 SYSCALL_DEFINE3(sendmsg, int, fd, struct msghdr __user *, msg, unsigned, flags)
1834 {
1835 struct compat_msghdr __user *msg_compat =
1836 (struct compat_msghdr __user *)msg;
1837 struct socket *sock;
1838 struct sockaddr_storage address;
1839 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
1840 unsigned char ctl[sizeof(struct cmsghdr) + 20]
1841 __attribute__ ((aligned(sizeof(__kernel_size_t))));
1842 /* 20 is size of ipv6_pktinfo */
1843 unsigned char *ctl_buf = ctl;
1844 struct msghdr msg_sys;
1845 int err, ctl_len, iov_size, total_len;
1846 int fput_needed;
1847
1848 err = -EFAULT;
1849 if (MSG_CMSG_COMPAT & flags) {
1850 if (get_compat_msghdr(&msg_sys, msg_compat))
1851 return -EFAULT;
1852 }
1853 else if (copy_from_user(&msg_sys, msg, sizeof(struct msghdr)))
1854 return -EFAULT;
1855
1856 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1857 if (!sock)
1858 goto out;
1859
1860 /* do not move before msg_sys is valid */
1861 err = -EMSGSIZE;
1862 if (msg_sys.msg_iovlen > UIO_MAXIOV)
1863 goto out_put;
1864
1865 /* Check whether to allocate the iovec area */
1866 err = -ENOMEM;
1867 iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
1868 if (msg_sys.msg_iovlen > UIO_FASTIOV) {
1869 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
1870 if (!iov)
1871 goto out_put;
1872 }
1873
1874 /* This will also move the address data into kernel space */
1875 if (MSG_CMSG_COMPAT & flags) {
1876 err = verify_compat_iovec(&msg_sys, iov,
1877 (struct sockaddr *)&address,
1878 VERIFY_READ);
1879 } else
1880 err = verify_iovec(&msg_sys, iov,
1881 (struct sockaddr *)&address,
1882 VERIFY_READ);
1883 if (err < 0)
1884 goto out_freeiov;
1885 total_len = err;
1886
1887 err = -ENOBUFS;
1888
1889 if (msg_sys.msg_controllen > INT_MAX)
1890 goto out_freeiov;
1891 ctl_len = msg_sys.msg_controllen;
1892 if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
1893 err =
1894 cmsghdr_from_user_compat_to_kern(&msg_sys, sock->sk, ctl,
1895 sizeof(ctl));
1896 if (err)
1897 goto out_freeiov;
1898 ctl_buf = msg_sys.msg_control;
1899 ctl_len = msg_sys.msg_controllen;
1900 } else if (ctl_len) {
1901 if (ctl_len > sizeof(ctl)) {
1902 ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
1903 if (ctl_buf == NULL)
1904 goto out_freeiov;
1905 }
1906 err = -EFAULT;
1907 /*
1908 * Careful! Before this, msg_sys.msg_control contains a user pointer.
1909 * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
1910 * checking falls down on this.
1911 */
1912 if (copy_from_user(ctl_buf, (void __user *)msg_sys.msg_control,
1913 ctl_len))
1914 goto out_freectl;
1915 msg_sys.msg_control = ctl_buf;
1916 }
1917 msg_sys.msg_flags = flags;
1918
1919 if (sock->file->f_flags & O_NONBLOCK)
1920 msg_sys.msg_flags |= MSG_DONTWAIT;
1921 err = sock_sendmsg(sock, &msg_sys, total_len);
1922
1923 out_freectl:
1924 if (ctl_buf != ctl)
1925 sock_kfree_s(sock->sk, ctl_buf, ctl_len);
1926 out_freeiov:
1927 if (iov != iovstack)
1928 sock_kfree_s(sock->sk, iov, iov_size);
1929 out_put:
1930 fput_light(sock->file, fput_needed);
1931 out:
1932 return err;
1933 }
1934
1935 /*
1936 * BSD recvmsg interface
1937 */
1938
SYSCALL_DEFINE3(recvmsg,int,fd,struct msghdr __user *,msg,unsigned int,flags)1939 SYSCALL_DEFINE3(recvmsg, int, fd, struct msghdr __user *, msg,
1940 unsigned int, flags)
1941 {
1942 struct compat_msghdr __user *msg_compat =
1943 (struct compat_msghdr __user *)msg;
1944 struct socket *sock;
1945 struct iovec iovstack[UIO_FASTIOV];
1946 struct iovec *iov = iovstack;
1947 struct msghdr msg_sys;
1948 unsigned long cmsg_ptr;
1949 int err, iov_size, total_len, len;
1950 int fput_needed;
1951
1952 /* kernel mode address */
1953 struct sockaddr_storage addr;
1954
1955 /* user mode address pointers */
1956 struct sockaddr __user *uaddr;
1957 int __user *uaddr_len;
1958
1959 if (MSG_CMSG_COMPAT & flags) {
1960 if (get_compat_msghdr(&msg_sys, msg_compat))
1961 return -EFAULT;
1962 }
1963 else if (copy_from_user(&msg_sys, msg, sizeof(struct msghdr)))
1964 return -EFAULT;
1965
1966 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1967 if (!sock)
1968 goto out;
1969
1970 err = -EMSGSIZE;
1971 if (msg_sys.msg_iovlen > UIO_MAXIOV)
1972 goto out_put;
1973
1974 /* Check whether to allocate the iovec area */
1975 err = -ENOMEM;
1976 iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
1977 if (msg_sys.msg_iovlen > UIO_FASTIOV) {
1978 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
1979 if (!iov)
1980 goto out_put;
1981 }
1982
1983 /*
1984 * Save the user-mode address (verify_iovec will change the
1985 * kernel msghdr to use the kernel address space)
1986 */
1987
1988 uaddr = (__force void __user *)msg_sys.msg_name;
1989 uaddr_len = COMPAT_NAMELEN(msg);
1990 if (MSG_CMSG_COMPAT & flags) {
1991 err = verify_compat_iovec(&msg_sys, iov,
1992 (struct sockaddr *)&addr,
1993 VERIFY_WRITE);
1994 } else
1995 err = verify_iovec(&msg_sys, iov,
1996 (struct sockaddr *)&addr,
1997 VERIFY_WRITE);
1998 if (err < 0)
1999 goto out_freeiov;
2000 total_len = err;
2001
2002 cmsg_ptr = (unsigned long)msg_sys.msg_control;
2003 msg_sys.msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
2004
2005 if (sock->file->f_flags & O_NONBLOCK)
2006 flags |= MSG_DONTWAIT;
2007 err = sock_recvmsg(sock, &msg_sys, total_len, flags);
2008 if (err < 0)
2009 goto out_freeiov;
2010 len = err;
2011
2012 if (uaddr != NULL) {
2013 err = move_addr_to_user((struct sockaddr *)&addr,
2014 msg_sys.msg_namelen, uaddr,
2015 uaddr_len);
2016 if (err < 0)
2017 goto out_freeiov;
2018 }
2019 err = __put_user((msg_sys.msg_flags & ~MSG_CMSG_COMPAT),
2020 COMPAT_FLAGS(msg));
2021 if (err)
2022 goto out_freeiov;
2023 if (MSG_CMSG_COMPAT & flags)
2024 err = __put_user((unsigned long)msg_sys.msg_control - cmsg_ptr,
2025 &msg_compat->msg_controllen);
2026 else
2027 err = __put_user((unsigned long)msg_sys.msg_control - cmsg_ptr,
2028 &msg->msg_controllen);
2029 if (err)
2030 goto out_freeiov;
2031 err = len;
2032
2033 out_freeiov:
2034 if (iov != iovstack)
2035 sock_kfree_s(sock->sk, iov, iov_size);
2036 out_put:
2037 fput_light(sock->file, fput_needed);
2038 out:
2039 return err;
2040 }
2041
2042 #ifdef __ARCH_WANT_SYS_SOCKETCALL
2043
2044 /* Argument list sizes for sys_socketcall */
2045 #define AL(x) ((x) * sizeof(unsigned long))
2046 static const unsigned char nargs[19]={
2047 AL(0),AL(3),AL(3),AL(3),AL(2),AL(3),
2048 AL(3),AL(3),AL(4),AL(4),AL(4),AL(6),
2049 AL(6),AL(2),AL(5),AL(5),AL(3),AL(3),
2050 AL(4)
2051 };
2052
2053 #undef AL
2054
2055 /*
2056 * System call vectors.
2057 *
2058 * Argument checking cleaned up. Saved 20% in size.
2059 * This function doesn't need to set the kernel lock because
2060 * it is set by the callees.
2061 */
2062
SYSCALL_DEFINE2(socketcall,int,call,unsigned long __user *,args)2063 SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args)
2064 {
2065 unsigned long a[6];
2066 unsigned long a0, a1;
2067 int err;
2068
2069 if (call < 1 || call > SYS_ACCEPT4)
2070 return -EINVAL;
2071
2072 /* copy_from_user should be SMP safe. */
2073 if (copy_from_user(a, args, nargs[call]))
2074 return -EFAULT;
2075
2076 audit_socketcall(nargs[call] / sizeof(unsigned long), a);
2077
2078 a0 = a[0];
2079 a1 = a[1];
2080
2081 switch (call) {
2082 case SYS_SOCKET:
2083 err = sys_socket(a0, a1, a[2]);
2084 break;
2085 case SYS_BIND:
2086 err = sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
2087 break;
2088 case SYS_CONNECT:
2089 err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
2090 break;
2091 case SYS_LISTEN:
2092 err = sys_listen(a0, a1);
2093 break;
2094 case SYS_ACCEPT:
2095 err = sys_accept4(a0, (struct sockaddr __user *)a1,
2096 (int __user *)a[2], 0);
2097 break;
2098 case SYS_GETSOCKNAME:
2099 err =
2100 sys_getsockname(a0, (struct sockaddr __user *)a1,
2101 (int __user *)a[2]);
2102 break;
2103 case SYS_GETPEERNAME:
2104 err =
2105 sys_getpeername(a0, (struct sockaddr __user *)a1,
2106 (int __user *)a[2]);
2107 break;
2108 case SYS_SOCKETPAIR:
2109 err = sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
2110 break;
2111 case SYS_SEND:
2112 err = sys_send(a0, (void __user *)a1, a[2], a[3]);
2113 break;
2114 case SYS_SENDTO:
2115 err = sys_sendto(a0, (void __user *)a1, a[2], a[3],
2116 (struct sockaddr __user *)a[4], a[5]);
2117 break;
2118 case SYS_RECV:
2119 err = sys_recv(a0, (void __user *)a1, a[2], a[3]);
2120 break;
2121 case SYS_RECVFROM:
2122 err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
2123 (struct sockaddr __user *)a[4],
2124 (int __user *)a[5]);
2125 break;
2126 case SYS_SHUTDOWN:
2127 err = sys_shutdown(a0, a1);
2128 break;
2129 case SYS_SETSOCKOPT:
2130 err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]);
2131 break;
2132 case SYS_GETSOCKOPT:
2133 err =
2134 sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
2135 (int __user *)a[4]);
2136 break;
2137 case SYS_SENDMSG:
2138 err = sys_sendmsg(a0, (struct msghdr __user *)a1, a[2]);
2139 break;
2140 case SYS_RECVMSG:
2141 err = sys_recvmsg(a0, (struct msghdr __user *)a1, a[2]);
2142 break;
2143 case SYS_ACCEPT4:
2144 err = sys_accept4(a0, (struct sockaddr __user *)a1,
2145 (int __user *)a[2], a[3]);
2146 break;
2147 default:
2148 err = -EINVAL;
2149 break;
2150 }
2151 return err;
2152 }
2153
2154 #endif /* __ARCH_WANT_SYS_SOCKETCALL */
2155
2156 /**
2157 * sock_register - add a socket protocol handler
2158 * @ops: description of protocol
2159 *
2160 * This function is called by a protocol handler that wants to
2161 * advertise its address family, and have it linked into the
2162 * socket interface. The value ops->family coresponds to the
2163 * socket system call protocol family.
2164 */
sock_register(const struct net_proto_family * ops)2165 int sock_register(const struct net_proto_family *ops)
2166 {
2167 int err;
2168
2169 if (ops->family >= NPROTO) {
2170 printk(KERN_CRIT "protocol %d >= NPROTO(%d)\n", ops->family,
2171 NPROTO);
2172 return -ENOBUFS;
2173 }
2174
2175 spin_lock(&net_family_lock);
2176 if (net_families[ops->family])
2177 err = -EEXIST;
2178 else {
2179 net_families[ops->family] = ops;
2180 err = 0;
2181 }
2182 spin_unlock(&net_family_lock);
2183
2184 printk(KERN_INFO "NET: Registered protocol family %d\n", ops->family);
2185 return err;
2186 }
2187
2188 /**
2189 * sock_unregister - remove a protocol handler
2190 * @family: protocol family to remove
2191 *
2192 * This function is called by a protocol handler that wants to
2193 * remove its address family, and have it unlinked from the
2194 * new socket creation.
2195 *
2196 * If protocol handler is a module, then it can use module reference
2197 * counts to protect against new references. If protocol handler is not
2198 * a module then it needs to provide its own protection in
2199 * the ops->create routine.
2200 */
sock_unregister(int family)2201 void sock_unregister(int family)
2202 {
2203 BUG_ON(family < 0 || family >= NPROTO);
2204
2205 spin_lock(&net_family_lock);
2206 net_families[family] = NULL;
2207 spin_unlock(&net_family_lock);
2208
2209 synchronize_rcu();
2210
2211 printk(KERN_INFO "NET: Unregistered protocol family %d\n", family);
2212 }
2213
sock_init(void)2214 static int __init sock_init(void)
2215 {
2216 /*
2217 * Initialize sock SLAB cache.
2218 */
2219
2220 sk_init();
2221
2222 /*
2223 * Initialize skbuff SLAB cache
2224 */
2225 skb_init();
2226
2227 /*
2228 * Initialize the protocols module.
2229 */
2230
2231 init_inodecache();
2232 register_filesystem(&sock_fs_type);
2233 sock_mnt = kern_mount(&sock_fs_type);
2234
2235 /* The real protocol initialization is performed in later initcalls.
2236 */
2237
2238 #ifdef CONFIG_NETFILTER
2239 netfilter_init();
2240 #endif
2241
2242 return 0;
2243 }
2244
2245 core_initcall(sock_init); /* early initcall */
2246
2247 #ifdef CONFIG_PROC_FS
socket_seq_show(struct seq_file * seq)2248 void socket_seq_show(struct seq_file *seq)
2249 {
2250 int cpu;
2251 int counter = 0;
2252
2253 for_each_possible_cpu(cpu)
2254 counter += per_cpu(sockets_in_use, cpu);
2255
2256 /* It can be negative, by the way. 8) */
2257 if (counter < 0)
2258 counter = 0;
2259
2260 seq_printf(seq, "sockets: used %d\n", counter);
2261 }
2262 #endif /* CONFIG_PROC_FS */
2263
2264 #ifdef CONFIG_COMPAT
compat_sock_ioctl(struct file * file,unsigned cmd,unsigned long arg)2265 static long compat_sock_ioctl(struct file *file, unsigned cmd,
2266 unsigned long arg)
2267 {
2268 struct socket *sock = file->private_data;
2269 int ret = -ENOIOCTLCMD;
2270 struct sock *sk;
2271 struct net *net;
2272
2273 sk = sock->sk;
2274 net = sock_net(sk);
2275
2276 if (sock->ops->compat_ioctl)
2277 ret = sock->ops->compat_ioctl(sock, cmd, arg);
2278
2279 if (ret == -ENOIOCTLCMD &&
2280 (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST))
2281 ret = compat_wext_handle_ioctl(net, cmd, arg);
2282
2283 return ret;
2284 }
2285 #endif
2286
kernel_bind(struct socket * sock,struct sockaddr * addr,int addrlen)2287 int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
2288 {
2289 return sock->ops->bind(sock, addr, addrlen);
2290 }
2291
kernel_listen(struct socket * sock,int backlog)2292 int kernel_listen(struct socket *sock, int backlog)
2293 {
2294 return sock->ops->listen(sock, backlog);
2295 }
2296
kernel_accept(struct socket * sock,struct socket ** newsock,int flags)2297 int kernel_accept(struct socket *sock, struct socket **newsock, int flags)
2298 {
2299 struct sock *sk = sock->sk;
2300 int err;
2301
2302 err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
2303 newsock);
2304 if (err < 0)
2305 goto done;
2306
2307 err = sock->ops->accept(sock, *newsock, flags);
2308 if (err < 0) {
2309 sock_release(*newsock);
2310 *newsock = NULL;
2311 goto done;
2312 }
2313
2314 (*newsock)->ops = sock->ops;
2315 __module_get((*newsock)->ops->owner);
2316
2317 done:
2318 return err;
2319 }
2320
kernel_connect(struct socket * sock,struct sockaddr * addr,int addrlen,int flags)2321 int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
2322 int flags)
2323 {
2324 return sock->ops->connect(sock, addr, addrlen, flags);
2325 }
2326
kernel_getsockname(struct socket * sock,struct sockaddr * addr,int * addrlen)2327 int kernel_getsockname(struct socket *sock, struct sockaddr *addr,
2328 int *addrlen)
2329 {
2330 return sock->ops->getname(sock, addr, addrlen, 0);
2331 }
2332
kernel_getpeername(struct socket * sock,struct sockaddr * addr,int * addrlen)2333 int kernel_getpeername(struct socket *sock, struct sockaddr *addr,
2334 int *addrlen)
2335 {
2336 return sock->ops->getname(sock, addr, addrlen, 1);
2337 }
2338
kernel_getsockopt(struct socket * sock,int level,int optname,char * optval,int * optlen)2339 int kernel_getsockopt(struct socket *sock, int level, int optname,
2340 char *optval, int *optlen)
2341 {
2342 mm_segment_t oldfs = get_fs();
2343 int err;
2344
2345 set_fs(KERNEL_DS);
2346 if (level == SOL_SOCKET)
2347 err = sock_getsockopt(sock, level, optname, optval, optlen);
2348 else
2349 err = sock->ops->getsockopt(sock, level, optname, optval,
2350 optlen);
2351 set_fs(oldfs);
2352 return err;
2353 }
2354
kernel_setsockopt(struct socket * sock,int level,int optname,char * optval,int optlen)2355 int kernel_setsockopt(struct socket *sock, int level, int optname,
2356 char *optval, int optlen)
2357 {
2358 mm_segment_t oldfs = get_fs();
2359 int err;
2360
2361 set_fs(KERNEL_DS);
2362 if (level == SOL_SOCKET)
2363 err = sock_setsockopt(sock, level, optname, optval, optlen);
2364 else
2365 err = sock->ops->setsockopt(sock, level, optname, optval,
2366 optlen);
2367 set_fs(oldfs);
2368 return err;
2369 }
2370
kernel_sendpage(struct socket * sock,struct page * page,int offset,size_t size,int flags)2371 int kernel_sendpage(struct socket *sock, struct page *page, int offset,
2372 size_t size, int flags)
2373 {
2374 if (sock->ops->sendpage)
2375 return sock->ops->sendpage(sock, page, offset, size, flags);
2376
2377 return sock_no_sendpage(sock, page, offset, size, flags);
2378 }
2379
kernel_sock_ioctl(struct socket * sock,int cmd,unsigned long arg)2380 int kernel_sock_ioctl(struct socket *sock, int cmd, unsigned long arg)
2381 {
2382 mm_segment_t oldfs = get_fs();
2383 int err;
2384
2385 set_fs(KERNEL_DS);
2386 err = sock->ops->ioctl(sock, cmd, arg);
2387 set_fs(oldfs);
2388
2389 return err;
2390 }
2391
kernel_sock_shutdown(struct socket * sock,enum sock_shutdown_cmd how)2392 int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how)
2393 {
2394 return sock->ops->shutdown(sock, how);
2395 }
2396
2397 EXPORT_SYMBOL(sock_create);
2398 EXPORT_SYMBOL(sock_create_kern);
2399 EXPORT_SYMBOL(sock_create_lite);
2400 EXPORT_SYMBOL(sock_map_fd);
2401 EXPORT_SYMBOL(sock_recvmsg);
2402 EXPORT_SYMBOL(sock_register);
2403 EXPORT_SYMBOL(sock_release);
2404 EXPORT_SYMBOL(sock_sendmsg);
2405 EXPORT_SYMBOL(sock_unregister);
2406 EXPORT_SYMBOL(sock_wake_async);
2407 EXPORT_SYMBOL(sockfd_lookup);
2408 EXPORT_SYMBOL(kernel_sendmsg);
2409 EXPORT_SYMBOL(kernel_recvmsg);
2410 EXPORT_SYMBOL(kernel_bind);
2411 EXPORT_SYMBOL(kernel_listen);
2412 EXPORT_SYMBOL(kernel_accept);
2413 EXPORT_SYMBOL(kernel_connect);
2414 EXPORT_SYMBOL(kernel_getsockname);
2415 EXPORT_SYMBOL(kernel_getpeername);
2416 EXPORT_SYMBOL(kernel_getsockopt);
2417 EXPORT_SYMBOL(kernel_setsockopt);
2418 EXPORT_SYMBOL(kernel_sendpage);
2419 EXPORT_SYMBOL(kernel_sock_ioctl);
2420 EXPORT_SYMBOL(kernel_sock_shutdown);
2421