[S390] drivers/s390/char: Use kmemdup
[linux-2.6/next.git] / net / socket.c
blob5e8d0af3c0e73b3537c1852eee2c15a917e4a605
1 /*
2 * NET An implementation of the SOCKET network access protocol.
4 * Version: @(#)socket.c 1.1.93 18/02/95
6 * Authors: Orest Zborowski, <obz@Kodak.COM>
7 * Ross Biro
8 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
10 * Fixes:
11 * Anonymous : NOTSOCK/BADF cleanup. Error fix in
12 * shutdown()
13 * Alan Cox : verify_area() fixes
14 * Alan Cox : Removed DDI
15 * Jonathan Kamens : SOCK_DGRAM reconnect bug
16 * Alan Cox : Moved a load of checks to the very
17 * top level.
18 * Alan Cox : Move address structures to/from user
19 * mode above the protocol layers.
20 * Rob Janssen : Allow 0 length sends.
21 * Alan Cox : Asynchronous I/O support (cribbed from the
22 * tty drivers).
23 * Niibe Yutaka : Asynchronous I/O for writes (4.4BSD style)
24 * Jeff Uphoff : Made max number of sockets command-line
25 * configurable.
26 * Matti Aarnio : Made the number of sockets dynamic,
27 * to be allocated when needed, and mr.
28 * Uphoff's max is used as max to be
29 * allowed to allocate.
30 * Linus : Argh. removed all the socket allocation
31 * altogether: it's in the inode now.
32 * Alan Cox : Made sock_alloc()/sock_release() public
33 * for NetROM and future kernel nfsd type
34 * stuff.
35 * Alan Cox : sendmsg/recvmsg basics.
36 * Tom Dyas : Export net symbols.
37 * Marcin Dalecki : Fixed problems with CONFIG_NET="n".
38 * Alan Cox : Added thread locking to sys_* calls
39 * for sockets. May have errors at the
40 * moment.
41 * Kevin Buhr : Fixed the dumb errors in the above.
42 * Andi Kleen : Some small cleanups, optimizations,
43 * and fixed a copy_from_user() bug.
44 * Tigran Aivazian : sys_send(args) calls sys_sendto(args, NULL, 0)
45 * Tigran Aivazian : Made listen(2) backlog sanity checks
46 * protocol-independent
49 * This program is free software; you can redistribute it and/or
50 * modify it under the terms of the GNU General Public License
51 * as published by the Free Software Foundation; either version
52 * 2 of the License, or (at your option) any later version.
55 * This module is effectively the top level interface to the BSD socket
56 * paradigm.
58 * Based upon Swansea University Computer Society NET3.039
61 #include <linux/mm.h>
62 #include <linux/socket.h>
63 #include <linux/file.h>
64 #include <linux/net.h>
65 #include <linux/interrupt.h>
66 #include <linux/thread_info.h>
67 #include <linux/rcupdate.h>
68 #include <linux/netdevice.h>
69 #include <linux/proc_fs.h>
70 #include <linux/seq_file.h>
71 #include <linux/mutex.h>
72 #include <linux/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 #include <linux/magic.h>
90 #include <linux/slab.h>
92 #include <asm/uaccess.h>
93 #include <asm/unistd.h>
95 #include <net/compat.h>
96 #include <net/wext.h>
98 #include <net/sock.h>
99 #include <linux/netfilter.h>
101 #include <linux/if_tun.h>
102 #include <linux/ipv6_route.h>
103 #include <linux/route.h>
104 #include <linux/sockios.h>
105 #include <linux/atalk.h>
107 static int sock_no_open(struct inode *irrelevant, struct file *dontcare);
108 static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
109 unsigned long nr_segs, loff_t pos);
110 static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov,
111 unsigned long nr_segs, loff_t pos);
112 static int sock_mmap(struct file *file, struct vm_area_struct *vma);
114 static int sock_close(struct inode *inode, struct file *file);
115 static unsigned int sock_poll(struct file *file,
116 struct poll_table_struct *wait);
117 static long sock_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
118 #ifdef CONFIG_COMPAT
119 static long compat_sock_ioctl(struct file *file,
120 unsigned int cmd, unsigned long arg);
121 #endif
122 static int sock_fasync(int fd, struct file *filp, int on);
123 static ssize_t sock_sendpage(struct file *file, struct page *page,
124 int offset, size_t size, loff_t *ppos, int more);
125 static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
126 struct pipe_inode_info *pipe, size_t len,
127 unsigned int flags);
130 * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
131 * in the operation structures but are done directly via the socketcall() multiplexor.
134 static const struct file_operations socket_file_ops = {
135 .owner = THIS_MODULE,
136 .llseek = no_llseek,
137 .aio_read = sock_aio_read,
138 .aio_write = sock_aio_write,
139 .poll = sock_poll,
140 .unlocked_ioctl = sock_ioctl,
141 #ifdef CONFIG_COMPAT
142 .compat_ioctl = compat_sock_ioctl,
143 #endif
144 .mmap = sock_mmap,
145 .open = sock_no_open, /* special open code to disallow open via /proc */
146 .release = sock_close,
147 .fasync = sock_fasync,
148 .sendpage = sock_sendpage,
149 .splice_write = generic_splice_sendpage,
150 .splice_read = sock_splice_read,
154 * The protocol list. Each protocol is registered in here.
157 static DEFINE_SPINLOCK(net_family_lock);
158 static const struct net_proto_family *net_families[NPROTO] __read_mostly;
161 * Statistics counters of the socket lists
164 static DEFINE_PER_CPU(int, sockets_in_use) = 0;
167 * Support routines.
168 * Move socket addresses back and forth across the kernel/user
169 * divide and look after the messy bits.
172 #define MAX_SOCK_ADDR 128 /* 108 for Unix domain -
173 16 for IP, 16 for IPX,
174 24 for IPv6,
175 about 80 for AX.25
176 must be at least one bigger than
177 the AF_UNIX size (see net/unix/af_unix.c
178 :unix_mkname()).
182 * move_addr_to_kernel - copy a socket address into kernel space
183 * @uaddr: Address in user space
184 * @kaddr: Address in kernel space
185 * @ulen: Length in user space
187 * The address is copied into kernel space. If the provided address is
188 * too long an error code of -EINVAL is returned. If the copy gives
189 * invalid addresses -EFAULT is returned. On a success 0 is returned.
192 int move_addr_to_kernel(void __user *uaddr, int ulen, struct sockaddr *kaddr)
194 if (ulen < 0 || ulen > sizeof(struct sockaddr_storage))
195 return -EINVAL;
196 if (ulen == 0)
197 return 0;
198 if (copy_from_user(kaddr, uaddr, ulen))
199 return -EFAULT;
200 return audit_sockaddr(ulen, kaddr);
204 * move_addr_to_user - copy an address to user space
205 * @kaddr: kernel space address
206 * @klen: length of address in kernel
207 * @uaddr: user space address
208 * @ulen: pointer to user length field
210 * The value pointed to by ulen on entry is the buffer length available.
211 * This is overwritten with the buffer space used. -EINVAL is returned
212 * if an overlong buffer is specified or a negative buffer size. -EFAULT
213 * is returned if either the buffer or the length field are not
214 * accessible.
215 * After copying the data up to the limit the user specifies, the true
216 * length of the data is written over the length limit the user
217 * specified. Zero is returned for a success.
220 int move_addr_to_user(struct sockaddr *kaddr, int klen, void __user *uaddr,
221 int __user *ulen)
223 int err;
224 int len;
226 err = get_user(len, ulen);
227 if (err)
228 return err;
229 if (len > klen)
230 len = klen;
231 if (len < 0 || len > sizeof(struct sockaddr_storage))
232 return -EINVAL;
233 if (len) {
234 if (audit_sockaddr(klen, kaddr))
235 return -ENOMEM;
236 if (copy_to_user(uaddr, kaddr, len))
237 return -EFAULT;
240 * "fromlen shall refer to the value before truncation.."
241 * 1003.1g
243 return __put_user(klen, ulen);
246 static struct kmem_cache *sock_inode_cachep __read_mostly;
248 static struct inode *sock_alloc_inode(struct super_block *sb)
250 struct socket_alloc *ei;
252 ei = kmem_cache_alloc(sock_inode_cachep, GFP_KERNEL);
253 if (!ei)
254 return NULL;
255 init_waitqueue_head(&ei->socket.wait);
257 ei->socket.fasync_list = NULL;
258 ei->socket.state = SS_UNCONNECTED;
259 ei->socket.flags = 0;
260 ei->socket.ops = NULL;
261 ei->socket.sk = NULL;
262 ei->socket.file = NULL;
264 return &ei->vfs_inode;
267 static void sock_destroy_inode(struct inode *inode)
269 kmem_cache_free(sock_inode_cachep,
270 container_of(inode, struct socket_alloc, vfs_inode));
273 static void init_once(void *foo)
275 struct socket_alloc *ei = (struct socket_alloc *)foo;
277 inode_init_once(&ei->vfs_inode);
280 static int init_inodecache(void)
282 sock_inode_cachep = kmem_cache_create("sock_inode_cache",
283 sizeof(struct socket_alloc),
285 (SLAB_HWCACHE_ALIGN |
286 SLAB_RECLAIM_ACCOUNT |
287 SLAB_MEM_SPREAD),
288 init_once);
289 if (sock_inode_cachep == NULL)
290 return -ENOMEM;
291 return 0;
294 static const struct super_operations sockfs_ops = {
295 .alloc_inode = sock_alloc_inode,
296 .destroy_inode =sock_destroy_inode,
297 .statfs = simple_statfs,
300 static int sockfs_get_sb(struct file_system_type *fs_type,
301 int flags, const char *dev_name, void *data,
302 struct vfsmount *mnt)
304 return get_sb_pseudo(fs_type, "socket:", &sockfs_ops, SOCKFS_MAGIC,
305 mnt);
308 static struct vfsmount *sock_mnt __read_mostly;
310 static struct file_system_type sock_fs_type = {
311 .name = "sockfs",
312 .get_sb = sockfs_get_sb,
313 .kill_sb = kill_anon_super,
317 * sockfs_dname() is called from d_path().
319 static char *sockfs_dname(struct dentry *dentry, char *buffer, int buflen)
321 return dynamic_dname(dentry, buffer, buflen, "socket:[%lu]",
322 dentry->d_inode->i_ino);
325 static const struct dentry_operations sockfs_dentry_operations = {
326 .d_dname = sockfs_dname,
330 * Obtains the first available file descriptor and sets it up for use.
332 * These functions create file structures and maps them to fd space
333 * of the current process. On success it returns file descriptor
334 * and file struct implicitly stored in sock->file.
335 * Note that another thread may close file descriptor before we return
336 * from this function. We use the fact that now we do not refer
337 * to socket after mapping. If one day we will need it, this
338 * function will increment ref. count on file by 1.
340 * In any case returned fd MAY BE not valid!
341 * This race condition is unavoidable
342 * with shared fd spaces, we cannot solve it inside kernel,
343 * but we take care of internal coherence yet.
346 static int sock_alloc_file(struct socket *sock, struct file **f, int flags)
348 struct qstr name = { .name = "" };
349 struct path path;
350 struct file *file;
351 int fd;
353 fd = get_unused_fd_flags(flags);
354 if (unlikely(fd < 0))
355 return fd;
357 path.dentry = d_alloc(sock_mnt->mnt_sb->s_root, &name);
358 if (unlikely(!path.dentry)) {
359 put_unused_fd(fd);
360 return -ENOMEM;
362 path.mnt = mntget(sock_mnt);
364 path.dentry->d_op = &sockfs_dentry_operations;
365 d_instantiate(path.dentry, SOCK_INODE(sock));
366 SOCK_INODE(sock)->i_fop = &socket_file_ops;
368 file = alloc_file(&path, FMODE_READ | FMODE_WRITE,
369 &socket_file_ops);
370 if (unlikely(!file)) {
371 /* drop dentry, keep inode */
372 atomic_inc(&path.dentry->d_inode->i_count);
373 path_put(&path);
374 put_unused_fd(fd);
375 return -ENFILE;
378 sock->file = file;
379 file->f_flags = O_RDWR | (flags & O_NONBLOCK);
380 file->f_pos = 0;
381 file->private_data = sock;
383 *f = file;
384 return fd;
387 int sock_map_fd(struct socket *sock, int flags)
389 struct file *newfile;
390 int fd = sock_alloc_file(sock, &newfile, flags);
392 if (likely(fd >= 0))
393 fd_install(fd, newfile);
395 return fd;
398 static struct socket *sock_from_file(struct file *file, int *err)
400 if (file->f_op == &socket_file_ops)
401 return file->private_data; /* set in sock_map_fd */
403 *err = -ENOTSOCK;
404 return NULL;
408 * sockfd_lookup - Go from a file number to its socket slot
409 * @fd: file handle
410 * @err: pointer to an error code return
412 * The file handle passed in is locked and the socket it is bound
413 * too is returned. If an error occurs the err pointer is overwritten
414 * with a negative errno code and NULL is returned. The function checks
415 * for both invalid handles and passing a handle which is not a socket.
417 * On a success the socket object pointer is returned.
420 struct socket *sockfd_lookup(int fd, int *err)
422 struct file *file;
423 struct socket *sock;
425 file = fget(fd);
426 if (!file) {
427 *err = -EBADF;
428 return NULL;
431 sock = sock_from_file(file, err);
432 if (!sock)
433 fput(file);
434 return sock;
437 static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)
439 struct file *file;
440 struct socket *sock;
442 *err = -EBADF;
443 file = fget_light(fd, fput_needed);
444 if (file) {
445 sock = sock_from_file(file, err);
446 if (sock)
447 return sock;
448 fput_light(file, *fput_needed);
450 return NULL;
454 * sock_alloc - allocate a socket
456 * Allocate a new inode and socket object. The two are bound together
457 * and initialised. The socket is then returned. If we are out of inodes
458 * NULL is returned.
461 static struct socket *sock_alloc(void)
463 struct inode *inode;
464 struct socket *sock;
466 inode = new_inode(sock_mnt->mnt_sb);
467 if (!inode)
468 return NULL;
470 sock = SOCKET_I(inode);
472 kmemcheck_annotate_bitfield(sock, type);
473 inode->i_mode = S_IFSOCK | S_IRWXUGO;
474 inode->i_uid = current_fsuid();
475 inode->i_gid = current_fsgid();
477 percpu_add(sockets_in_use, 1);
478 return sock;
482 * In theory you can't get an open on this inode, but /proc provides
483 * a back door. Remember to keep it shut otherwise you'll let the
484 * creepy crawlies in.
487 static int sock_no_open(struct inode *irrelevant, struct file *dontcare)
489 return -ENXIO;
492 const struct file_operations bad_sock_fops = {
493 .owner = THIS_MODULE,
494 .open = sock_no_open,
498 * sock_release - close a socket
499 * @sock: socket to close
501 * The socket is released from the protocol stack if it has a release
502 * callback, and the inode is then released if the socket is bound to
503 * an inode not a file.
506 void sock_release(struct socket *sock)
508 if (sock->ops) {
509 struct module *owner = sock->ops->owner;
511 sock->ops->release(sock);
512 sock->ops = NULL;
513 module_put(owner);
516 if (sock->fasync_list)
517 printk(KERN_ERR "sock_release: fasync list not empty!\n");
519 percpu_sub(sockets_in_use, 1);
520 if (!sock->file) {
521 iput(SOCK_INODE(sock));
522 return;
524 sock->file = NULL;
527 int sock_tx_timestamp(struct msghdr *msg, struct sock *sk,
528 union skb_shared_tx *shtx)
530 shtx->flags = 0;
531 if (sock_flag(sk, SOCK_TIMESTAMPING_TX_HARDWARE))
532 shtx->hardware = 1;
533 if (sock_flag(sk, SOCK_TIMESTAMPING_TX_SOFTWARE))
534 shtx->software = 1;
535 return 0;
537 EXPORT_SYMBOL(sock_tx_timestamp);
539 static inline int __sock_sendmsg(struct kiocb *iocb, struct socket *sock,
540 struct msghdr *msg, size_t size)
542 struct sock_iocb *si = kiocb_to_siocb(iocb);
543 int err;
545 si->sock = sock;
546 si->scm = NULL;
547 si->msg = msg;
548 si->size = size;
550 err = security_socket_sendmsg(sock, msg, size);
551 if (err)
552 return err;
554 return sock->ops->sendmsg(iocb, sock, msg, size);
557 int sock_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
559 struct kiocb iocb;
560 struct sock_iocb siocb;
561 int ret;
563 init_sync_kiocb(&iocb, NULL);
564 iocb.private = &siocb;
565 ret = __sock_sendmsg(&iocb, sock, msg, size);
566 if (-EIOCBQUEUED == ret)
567 ret = wait_on_sync_kiocb(&iocb);
568 return ret;
571 int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
572 struct kvec *vec, size_t num, size_t size)
574 mm_segment_t oldfs = get_fs();
575 int result;
577 set_fs(KERNEL_DS);
579 * the following is safe, since for compiler definitions of kvec and
580 * iovec are identical, yielding the same in-core layout and alignment
582 msg->msg_iov = (struct iovec *)vec;
583 msg->msg_iovlen = num;
584 result = sock_sendmsg(sock, msg, size);
585 set_fs(oldfs);
586 return result;
589 static int ktime2ts(ktime_t kt, struct timespec *ts)
591 if (kt.tv64) {
592 *ts = ktime_to_timespec(kt);
593 return 1;
594 } else {
595 return 0;
600 * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
602 void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
603 struct sk_buff *skb)
605 int need_software_tstamp = sock_flag(sk, SOCK_RCVTSTAMP);
606 struct timespec ts[3];
607 int empty = 1;
608 struct skb_shared_hwtstamps *shhwtstamps =
609 skb_hwtstamps(skb);
611 /* Race occurred between timestamp enabling and packet
612 receiving. Fill in the current time for now. */
613 if (need_software_tstamp && skb->tstamp.tv64 == 0)
614 __net_timestamp(skb);
616 if (need_software_tstamp) {
617 if (!sock_flag(sk, SOCK_RCVTSTAMPNS)) {
618 struct timeval tv;
619 skb_get_timestamp(skb, &tv);
620 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMP,
621 sizeof(tv), &tv);
622 } else {
623 struct timespec ts;
624 skb_get_timestampns(skb, &ts);
625 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPNS,
626 sizeof(ts), &ts);
631 memset(ts, 0, sizeof(ts));
632 if (skb->tstamp.tv64 &&
633 sock_flag(sk, SOCK_TIMESTAMPING_SOFTWARE)) {
634 skb_get_timestampns(skb, ts + 0);
635 empty = 0;
637 if (shhwtstamps) {
638 if (sock_flag(sk, SOCK_TIMESTAMPING_SYS_HARDWARE) &&
639 ktime2ts(shhwtstamps->syststamp, ts + 1))
640 empty = 0;
641 if (sock_flag(sk, SOCK_TIMESTAMPING_RAW_HARDWARE) &&
642 ktime2ts(shhwtstamps->hwtstamp, ts + 2))
643 empty = 0;
645 if (!empty)
646 put_cmsg(msg, SOL_SOCKET,
647 SCM_TIMESTAMPING, sizeof(ts), &ts);
650 EXPORT_SYMBOL_GPL(__sock_recv_timestamp);
652 inline void sock_recv_drops(struct msghdr *msg, struct sock *sk, struct sk_buff *skb)
654 if (sock_flag(sk, SOCK_RXQ_OVFL) && skb && skb->dropcount)
655 put_cmsg(msg, SOL_SOCKET, SO_RXQ_OVFL,
656 sizeof(__u32), &skb->dropcount);
659 void sock_recv_ts_and_drops(struct msghdr *msg, struct sock *sk,
660 struct sk_buff *skb)
662 sock_recv_timestamp(msg, sk, skb);
663 sock_recv_drops(msg, sk, skb);
665 EXPORT_SYMBOL_GPL(sock_recv_ts_and_drops);
667 static inline int __sock_recvmsg_nosec(struct kiocb *iocb, struct socket *sock,
668 struct msghdr *msg, size_t size, int flags)
670 struct sock_iocb *si = kiocb_to_siocb(iocb);
672 si->sock = sock;
673 si->scm = NULL;
674 si->msg = msg;
675 si->size = size;
676 si->flags = flags;
678 return sock->ops->recvmsg(iocb, sock, msg, size, flags);
681 static inline int __sock_recvmsg(struct kiocb *iocb, struct socket *sock,
682 struct msghdr *msg, size_t size, int flags)
684 int err = security_socket_recvmsg(sock, msg, size, flags);
686 return err ?: __sock_recvmsg_nosec(iocb, sock, msg, size, flags);
689 int sock_recvmsg(struct socket *sock, struct msghdr *msg,
690 size_t size, int flags)
692 struct kiocb iocb;
693 struct sock_iocb siocb;
694 int ret;
696 init_sync_kiocb(&iocb, NULL);
697 iocb.private = &siocb;
698 ret = __sock_recvmsg(&iocb, sock, msg, size, flags);
699 if (-EIOCBQUEUED == ret)
700 ret = wait_on_sync_kiocb(&iocb);
701 return ret;
704 static int sock_recvmsg_nosec(struct socket *sock, struct msghdr *msg,
705 size_t size, int flags)
707 struct kiocb iocb;
708 struct sock_iocb siocb;
709 int ret;
711 init_sync_kiocb(&iocb, NULL);
712 iocb.private = &siocb;
713 ret = __sock_recvmsg_nosec(&iocb, sock, msg, size, flags);
714 if (-EIOCBQUEUED == ret)
715 ret = wait_on_sync_kiocb(&iocb);
716 return ret;
719 int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
720 struct kvec *vec, size_t num, size_t size, int flags)
722 mm_segment_t oldfs = get_fs();
723 int result;
725 set_fs(KERNEL_DS);
727 * the following is safe, since for compiler definitions of kvec and
728 * iovec are identical, yielding the same in-core layout and alignment
730 msg->msg_iov = (struct iovec *)vec, msg->msg_iovlen = num;
731 result = sock_recvmsg(sock, msg, size, flags);
732 set_fs(oldfs);
733 return result;
736 static void sock_aio_dtor(struct kiocb *iocb)
738 kfree(iocb->private);
741 static ssize_t sock_sendpage(struct file *file, struct page *page,
742 int offset, size_t size, loff_t *ppos, int more)
744 struct socket *sock;
745 int flags;
747 sock = file->private_data;
749 flags = !(file->f_flags & O_NONBLOCK) ? 0 : MSG_DONTWAIT;
750 if (more)
751 flags |= MSG_MORE;
753 return kernel_sendpage(sock, page, offset, size, flags);
756 static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
757 struct pipe_inode_info *pipe, size_t len,
758 unsigned int flags)
760 struct socket *sock = file->private_data;
762 if (unlikely(!sock->ops->splice_read))
763 return -EINVAL;
765 return sock->ops->splice_read(sock, ppos, pipe, len, flags);
768 static struct sock_iocb *alloc_sock_iocb(struct kiocb *iocb,
769 struct sock_iocb *siocb)
771 if (!is_sync_kiocb(iocb)) {
772 siocb = kmalloc(sizeof(*siocb), GFP_KERNEL);
773 if (!siocb)
774 return NULL;
775 iocb->ki_dtor = sock_aio_dtor;
778 siocb->kiocb = iocb;
779 iocb->private = siocb;
780 return siocb;
783 static ssize_t do_sock_read(struct msghdr *msg, struct kiocb *iocb,
784 struct file *file, const struct iovec *iov,
785 unsigned long nr_segs)
787 struct socket *sock = file->private_data;
788 size_t size = 0;
789 int i;
791 for (i = 0; i < nr_segs; i++)
792 size += iov[i].iov_len;
794 msg->msg_name = NULL;
795 msg->msg_namelen = 0;
796 msg->msg_control = NULL;
797 msg->msg_controllen = 0;
798 msg->msg_iov = (struct iovec *)iov;
799 msg->msg_iovlen = nr_segs;
800 msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
802 return __sock_recvmsg(iocb, sock, msg, size, msg->msg_flags);
805 static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
806 unsigned long nr_segs, loff_t pos)
808 struct sock_iocb siocb, *x;
810 if (pos != 0)
811 return -ESPIPE;
813 if (iocb->ki_left == 0) /* Match SYS5 behaviour */
814 return 0;
817 x = alloc_sock_iocb(iocb, &siocb);
818 if (!x)
819 return -ENOMEM;
820 return do_sock_read(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
823 static ssize_t do_sock_write(struct msghdr *msg, struct kiocb *iocb,
824 struct file *file, const struct iovec *iov,
825 unsigned long nr_segs)
827 struct socket *sock = file->private_data;
828 size_t size = 0;
829 int i;
831 for (i = 0; i < nr_segs; i++)
832 size += iov[i].iov_len;
834 msg->msg_name = NULL;
835 msg->msg_namelen = 0;
836 msg->msg_control = NULL;
837 msg->msg_controllen = 0;
838 msg->msg_iov = (struct iovec *)iov;
839 msg->msg_iovlen = nr_segs;
840 msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
841 if (sock->type == SOCK_SEQPACKET)
842 msg->msg_flags |= MSG_EOR;
844 return __sock_sendmsg(iocb, sock, msg, size);
847 static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov,
848 unsigned long nr_segs, loff_t pos)
850 struct sock_iocb siocb, *x;
852 if (pos != 0)
853 return -ESPIPE;
855 x = alloc_sock_iocb(iocb, &siocb);
856 if (!x)
857 return -ENOMEM;
859 return do_sock_write(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
863 * Atomic setting of ioctl hooks to avoid race
864 * with module unload.
867 static DEFINE_MUTEX(br_ioctl_mutex);
868 static int (*br_ioctl_hook) (struct net *, unsigned int cmd, void __user *arg) = NULL;
870 void brioctl_set(int (*hook) (struct net *, unsigned int, void __user *))
872 mutex_lock(&br_ioctl_mutex);
873 br_ioctl_hook = hook;
874 mutex_unlock(&br_ioctl_mutex);
877 EXPORT_SYMBOL(brioctl_set);
879 static DEFINE_MUTEX(vlan_ioctl_mutex);
880 static int (*vlan_ioctl_hook) (struct net *, void __user *arg);
882 void vlan_ioctl_set(int (*hook) (struct net *, void __user *))
884 mutex_lock(&vlan_ioctl_mutex);
885 vlan_ioctl_hook = hook;
886 mutex_unlock(&vlan_ioctl_mutex);
889 EXPORT_SYMBOL(vlan_ioctl_set);
891 static DEFINE_MUTEX(dlci_ioctl_mutex);
892 static int (*dlci_ioctl_hook) (unsigned int, void __user *);
894 void dlci_ioctl_set(int (*hook) (unsigned int, void __user *))
896 mutex_lock(&dlci_ioctl_mutex);
897 dlci_ioctl_hook = hook;
898 mutex_unlock(&dlci_ioctl_mutex);
901 EXPORT_SYMBOL(dlci_ioctl_set);
903 static long sock_do_ioctl(struct net *net, struct socket *sock,
904 unsigned int cmd, unsigned long arg)
906 int err;
907 void __user *argp = (void __user *)arg;
909 err = sock->ops->ioctl(sock, cmd, arg);
912 * If this ioctl is unknown try to hand it down
913 * to the NIC driver.
915 if (err == -ENOIOCTLCMD)
916 err = dev_ioctl(net, cmd, argp);
918 return err;
922 * With an ioctl, arg may well be a user mode pointer, but we don't know
923 * what to do with it - that's up to the protocol still.
926 static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
928 struct socket *sock;
929 struct sock *sk;
930 void __user *argp = (void __user *)arg;
931 int pid, err;
932 struct net *net;
934 sock = file->private_data;
935 sk = sock->sk;
936 net = sock_net(sk);
937 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
938 err = dev_ioctl(net, cmd, argp);
939 } else
940 #ifdef CONFIG_WEXT_CORE
941 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
942 err = dev_ioctl(net, cmd, argp);
943 } else
944 #endif
945 switch (cmd) {
946 case FIOSETOWN:
947 case SIOCSPGRP:
948 err = -EFAULT;
949 if (get_user(pid, (int __user *)argp))
950 break;
951 err = f_setown(sock->file, pid, 1);
952 break;
953 case FIOGETOWN:
954 case SIOCGPGRP:
955 err = put_user(f_getown(sock->file),
956 (int __user *)argp);
957 break;
958 case SIOCGIFBR:
959 case SIOCSIFBR:
960 case SIOCBRADDBR:
961 case SIOCBRDELBR:
962 err = -ENOPKG;
963 if (!br_ioctl_hook)
964 request_module("bridge");
966 mutex_lock(&br_ioctl_mutex);
967 if (br_ioctl_hook)
968 err = br_ioctl_hook(net, cmd, argp);
969 mutex_unlock(&br_ioctl_mutex);
970 break;
971 case SIOCGIFVLAN:
972 case SIOCSIFVLAN:
973 err = -ENOPKG;
974 if (!vlan_ioctl_hook)
975 request_module("8021q");
977 mutex_lock(&vlan_ioctl_mutex);
978 if (vlan_ioctl_hook)
979 err = vlan_ioctl_hook(net, argp);
980 mutex_unlock(&vlan_ioctl_mutex);
981 break;
982 case SIOCADDDLCI:
983 case SIOCDELDLCI:
984 err = -ENOPKG;
985 if (!dlci_ioctl_hook)
986 request_module("dlci");
988 mutex_lock(&dlci_ioctl_mutex);
989 if (dlci_ioctl_hook)
990 err = dlci_ioctl_hook(cmd, argp);
991 mutex_unlock(&dlci_ioctl_mutex);
992 break;
993 default:
994 err = sock_do_ioctl(net, sock, cmd, arg);
995 break;
997 return err;
1000 int sock_create_lite(int family, int type, int protocol, struct socket **res)
1002 int err;
1003 struct socket *sock = NULL;
1005 err = security_socket_create(family, type, protocol, 1);
1006 if (err)
1007 goto out;
1009 sock = sock_alloc();
1010 if (!sock) {
1011 err = -ENOMEM;
1012 goto out;
1015 sock->type = type;
1016 err = security_socket_post_create(sock, family, type, protocol, 1);
1017 if (err)
1018 goto out_release;
1020 out:
1021 *res = sock;
1022 return err;
1023 out_release:
1024 sock_release(sock);
1025 sock = NULL;
1026 goto out;
1029 /* No kernel lock held - perfect */
1030 static unsigned int sock_poll(struct file *file, poll_table *wait)
1032 struct socket *sock;
1035 * We can't return errors to poll, so it's either yes or no.
1037 sock = file->private_data;
1038 return sock->ops->poll(file, sock, wait);
1041 static int sock_mmap(struct file *file, struct vm_area_struct *vma)
1043 struct socket *sock = file->private_data;
1045 return sock->ops->mmap(file, sock, vma);
1048 static int sock_close(struct inode *inode, struct file *filp)
1051 * It was possible the inode is NULL we were
1052 * closing an unfinished socket.
1055 if (!inode) {
1056 printk(KERN_DEBUG "sock_close: NULL inode\n");
1057 return 0;
1059 sock_release(SOCKET_I(inode));
1060 return 0;
1064 * Update the socket async list
1066 * Fasync_list locking strategy.
1068 * 1. fasync_list is modified only under process context socket lock
1069 * i.e. under semaphore.
1070 * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
1071 * or under socket lock.
1072 * 3. fasync_list can be used from softirq context, so that
1073 * modification under socket lock have to be enhanced with
1074 * write_lock_bh(&sk->sk_callback_lock).
1075 * --ANK (990710)
1078 static int sock_fasync(int fd, struct file *filp, int on)
1080 struct fasync_struct *fa, *fna = NULL, **prev;
1081 struct socket *sock;
1082 struct sock *sk;
1084 if (on) {
1085 fna = kmalloc(sizeof(struct fasync_struct), GFP_KERNEL);
1086 if (fna == NULL)
1087 return -ENOMEM;
1090 sock = filp->private_data;
1092 sk = sock->sk;
1093 if (sk == NULL) {
1094 kfree(fna);
1095 return -EINVAL;
1098 lock_sock(sk);
1100 spin_lock(&filp->f_lock);
1101 if (on)
1102 filp->f_flags |= FASYNC;
1103 else
1104 filp->f_flags &= ~FASYNC;
1105 spin_unlock(&filp->f_lock);
1107 prev = &(sock->fasync_list);
1109 for (fa = *prev; fa != NULL; prev = &fa->fa_next, fa = *prev)
1110 if (fa->fa_file == filp)
1111 break;
1113 if (on) {
1114 if (fa != NULL) {
1115 write_lock_bh(&sk->sk_callback_lock);
1116 fa->fa_fd = fd;
1117 write_unlock_bh(&sk->sk_callback_lock);
1119 kfree(fna);
1120 goto out;
1122 fna->fa_file = filp;
1123 fna->fa_fd = fd;
1124 fna->magic = FASYNC_MAGIC;
1125 fna->fa_next = sock->fasync_list;
1126 write_lock_bh(&sk->sk_callback_lock);
1127 sock->fasync_list = fna;
1128 sock_set_flag(sk, SOCK_FASYNC);
1129 write_unlock_bh(&sk->sk_callback_lock);
1130 } else {
1131 if (fa != NULL) {
1132 write_lock_bh(&sk->sk_callback_lock);
1133 *prev = fa->fa_next;
1134 if (!sock->fasync_list)
1135 sock_reset_flag(sk, SOCK_FASYNC);
1136 write_unlock_bh(&sk->sk_callback_lock);
1137 kfree(fa);
1141 out:
1142 release_sock(sock->sk);
1143 return 0;
1146 /* This function may be called only under socket lock or callback_lock */
1148 int sock_wake_async(struct socket *sock, int how, int band)
1150 if (!sock || !sock->fasync_list)
1151 return -1;
1152 switch (how) {
1153 case SOCK_WAKE_WAITD:
1154 if (test_bit(SOCK_ASYNC_WAITDATA, &sock->flags))
1155 break;
1156 goto call_kill;
1157 case SOCK_WAKE_SPACE:
1158 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags))
1159 break;
1160 /* fall through */
1161 case SOCK_WAKE_IO:
1162 call_kill:
1163 __kill_fasync(sock->fasync_list, SIGIO, band);
1164 break;
1165 case SOCK_WAKE_URG:
1166 __kill_fasync(sock->fasync_list, SIGURG, band);
1168 return 0;
1171 static int __sock_create(struct net *net, int family, int type, int protocol,
1172 struct socket **res, int kern)
1174 int err;
1175 struct socket *sock;
1176 const struct net_proto_family *pf;
1179 * Check protocol is in range
1181 if (family < 0 || family >= NPROTO)
1182 return -EAFNOSUPPORT;
1183 if (type < 0 || type >= SOCK_MAX)
1184 return -EINVAL;
1186 /* Compatibility.
1188 This uglymoron is moved from INET layer to here to avoid
1189 deadlock in module load.
1191 if (family == PF_INET && type == SOCK_PACKET) {
1192 static int warned;
1193 if (!warned) {
1194 warned = 1;
1195 printk(KERN_INFO "%s uses obsolete (PF_INET,SOCK_PACKET)\n",
1196 current->comm);
1198 family = PF_PACKET;
1201 err = security_socket_create(family, type, protocol, kern);
1202 if (err)
1203 return err;
1206 * Allocate the socket and allow the family to set things up. if
1207 * the protocol is 0, the family is instructed to select an appropriate
1208 * default.
1210 sock = sock_alloc();
1211 if (!sock) {
1212 if (net_ratelimit())
1213 printk(KERN_WARNING "socket: no more sockets\n");
1214 return -ENFILE; /* Not exactly a match, but its the
1215 closest posix thing */
1218 sock->type = type;
1220 #ifdef CONFIG_MODULES
1221 /* Attempt to load a protocol module if the find failed.
1223 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
1224 * requested real, full-featured networking support upon configuration.
1225 * Otherwise module support will break!
1227 if (net_families[family] == NULL)
1228 request_module("net-pf-%d", family);
1229 #endif
1231 rcu_read_lock();
1232 pf = rcu_dereference(net_families[family]);
1233 err = -EAFNOSUPPORT;
1234 if (!pf)
1235 goto out_release;
1238 * We will call the ->create function, that possibly is in a loadable
1239 * module, so we have to bump that loadable module refcnt first.
1241 if (!try_module_get(pf->owner))
1242 goto out_release;
1244 /* Now protected by module ref count */
1245 rcu_read_unlock();
1247 err = pf->create(net, sock, protocol, kern);
1248 if (err < 0)
1249 goto out_module_put;
1252 * Now to bump the refcnt of the [loadable] module that owns this
1253 * socket at sock_release time we decrement its refcnt.
1255 if (!try_module_get(sock->ops->owner))
1256 goto out_module_busy;
1259 * Now that we're done with the ->create function, the [loadable]
1260 * module can have its refcnt decremented
1262 module_put(pf->owner);
1263 err = security_socket_post_create(sock, family, type, protocol, kern);
1264 if (err)
1265 goto out_sock_release;
1266 *res = sock;
1268 return 0;
1270 out_module_busy:
1271 err = -EAFNOSUPPORT;
1272 out_module_put:
1273 sock->ops = NULL;
1274 module_put(pf->owner);
1275 out_sock_release:
1276 sock_release(sock);
1277 return err;
1279 out_release:
1280 rcu_read_unlock();
1281 goto out_sock_release;
1284 int sock_create(int family, int type, int protocol, struct socket **res)
1286 return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0);
1289 int sock_create_kern(int family, int type, int protocol, struct socket **res)
1291 return __sock_create(&init_net, family, type, protocol, res, 1);
1294 SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol)
1296 int retval;
1297 struct socket *sock;
1298 int flags;
1300 /* Check the SOCK_* constants for consistency. */
1301 BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC);
1302 BUILD_BUG_ON((SOCK_MAX | SOCK_TYPE_MASK) != SOCK_TYPE_MASK);
1303 BUILD_BUG_ON(SOCK_CLOEXEC & SOCK_TYPE_MASK);
1304 BUILD_BUG_ON(SOCK_NONBLOCK & SOCK_TYPE_MASK);
1306 flags = type & ~SOCK_TYPE_MASK;
1307 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1308 return -EINVAL;
1309 type &= SOCK_TYPE_MASK;
1311 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1312 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1314 retval = sock_create(family, type, protocol, &sock);
1315 if (retval < 0)
1316 goto out;
1318 retval = sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK));
1319 if (retval < 0)
1320 goto out_release;
1322 out:
1323 /* It may be already another descriptor 8) Not kernel problem. */
1324 return retval;
1326 out_release:
1327 sock_release(sock);
1328 return retval;
1332 * Create a pair of connected sockets.
1335 SYSCALL_DEFINE4(socketpair, int, family, int, type, int, protocol,
1336 int __user *, usockvec)
1338 struct socket *sock1, *sock2;
1339 int fd1, fd2, err;
1340 struct file *newfile1, *newfile2;
1341 int flags;
1343 flags = type & ~SOCK_TYPE_MASK;
1344 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1345 return -EINVAL;
1346 type &= SOCK_TYPE_MASK;
1348 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1349 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1352 * Obtain the first socket and check if the underlying protocol
1353 * supports the socketpair call.
1356 err = sock_create(family, type, protocol, &sock1);
1357 if (err < 0)
1358 goto out;
1360 err = sock_create(family, type, protocol, &sock2);
1361 if (err < 0)
1362 goto out_release_1;
1364 err = sock1->ops->socketpair(sock1, sock2);
1365 if (err < 0)
1366 goto out_release_both;
1368 fd1 = sock_alloc_file(sock1, &newfile1, flags);
1369 if (unlikely(fd1 < 0)) {
1370 err = fd1;
1371 goto out_release_both;
1374 fd2 = sock_alloc_file(sock2, &newfile2, flags);
1375 if (unlikely(fd2 < 0)) {
1376 err = fd2;
1377 fput(newfile1);
1378 put_unused_fd(fd1);
1379 sock_release(sock2);
1380 goto out;
1383 audit_fd_pair(fd1, fd2);
1384 fd_install(fd1, newfile1);
1385 fd_install(fd2, newfile2);
1386 /* fd1 and fd2 may be already another descriptors.
1387 * Not kernel problem.
1390 err = put_user(fd1, &usockvec[0]);
1391 if (!err)
1392 err = put_user(fd2, &usockvec[1]);
1393 if (!err)
1394 return 0;
1396 sys_close(fd2);
1397 sys_close(fd1);
1398 return err;
1400 out_release_both:
1401 sock_release(sock2);
1402 out_release_1:
1403 sock_release(sock1);
1404 out:
1405 return err;
1409 * Bind a name to a socket. Nothing much to do here since it's
1410 * the protocol's responsibility to handle the local address.
1412 * We move the socket address to kernel space before we call
1413 * the protocol layer (having also checked the address is ok).
1416 SYSCALL_DEFINE3(bind, int, fd, struct sockaddr __user *, umyaddr, int, addrlen)
1418 struct socket *sock;
1419 struct sockaddr_storage address;
1420 int err, fput_needed;
1422 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1423 if (sock) {
1424 err = move_addr_to_kernel(umyaddr, addrlen, (struct sockaddr *)&address);
1425 if (err >= 0) {
1426 err = security_socket_bind(sock,
1427 (struct sockaddr *)&address,
1428 addrlen);
1429 if (!err)
1430 err = sock->ops->bind(sock,
1431 (struct sockaddr *)
1432 &address, addrlen);
1434 fput_light(sock->file, fput_needed);
1436 return err;
1440 * Perform a listen. Basically, we allow the protocol to do anything
1441 * necessary for a listen, and if that works, we mark the socket as
1442 * ready for listening.
1445 SYSCALL_DEFINE2(listen, int, fd, int, backlog)
1447 struct socket *sock;
1448 int err, fput_needed;
1449 int somaxconn;
1451 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1452 if (sock) {
1453 somaxconn = sock_net(sock->sk)->core.sysctl_somaxconn;
1454 if ((unsigned)backlog > somaxconn)
1455 backlog = somaxconn;
1457 err = security_socket_listen(sock, backlog);
1458 if (!err)
1459 err = sock->ops->listen(sock, backlog);
1461 fput_light(sock->file, fput_needed);
1463 return err;
1467 * For accept, we attempt to create a new socket, set up the link
1468 * with the client, wake up the client, then return the new
1469 * connected fd. We collect the address of the connector in kernel
1470 * space and move it to user at the very end. This is unclean because
1471 * we open the socket then return an error.
1473 * 1003.1g adds the ability to recvmsg() to query connection pending
1474 * status to recvmsg. We need to add that support in a way thats
1475 * clean when we restucture accept also.
1478 SYSCALL_DEFINE4(accept4, int, fd, struct sockaddr __user *, upeer_sockaddr,
1479 int __user *, upeer_addrlen, int, flags)
1481 struct socket *sock, *newsock;
1482 struct file *newfile;
1483 int err, len, newfd, fput_needed;
1484 struct sockaddr_storage address;
1486 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1487 return -EINVAL;
1489 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1490 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1492 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1493 if (!sock)
1494 goto out;
1496 err = -ENFILE;
1497 if (!(newsock = sock_alloc()))
1498 goto out_put;
1500 newsock->type = sock->type;
1501 newsock->ops = sock->ops;
1504 * We don't need try_module_get here, as the listening socket (sock)
1505 * has the protocol module (sock->ops->owner) held.
1507 __module_get(newsock->ops->owner);
1509 newfd = sock_alloc_file(newsock, &newfile, flags);
1510 if (unlikely(newfd < 0)) {
1511 err = newfd;
1512 sock_release(newsock);
1513 goto out_put;
1516 err = security_socket_accept(sock, newsock);
1517 if (err)
1518 goto out_fd;
1520 err = sock->ops->accept(sock, newsock, sock->file->f_flags);
1521 if (err < 0)
1522 goto out_fd;
1524 if (upeer_sockaddr) {
1525 if (newsock->ops->getname(newsock, (struct sockaddr *)&address,
1526 &len, 2) < 0) {
1527 err = -ECONNABORTED;
1528 goto out_fd;
1530 err = move_addr_to_user((struct sockaddr *)&address,
1531 len, upeer_sockaddr, upeer_addrlen);
1532 if (err < 0)
1533 goto out_fd;
1536 /* File flags are not inherited via accept() unlike another OSes. */
1538 fd_install(newfd, newfile);
1539 err = newfd;
1541 out_put:
1542 fput_light(sock->file, fput_needed);
1543 out:
1544 return err;
1545 out_fd:
1546 fput(newfile);
1547 put_unused_fd(newfd);
1548 goto out_put;
1551 SYSCALL_DEFINE3(accept, int, fd, struct sockaddr __user *, upeer_sockaddr,
1552 int __user *, upeer_addrlen)
1554 return sys_accept4(fd, upeer_sockaddr, upeer_addrlen, 0);
1558 * Attempt to connect to a socket with the server address. The address
1559 * is in user space so we verify it is OK and move it to kernel space.
1561 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
1562 * break bindings
1564 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
1565 * other SEQPACKET protocols that take time to connect() as it doesn't
1566 * include the -EINPROGRESS status for such sockets.
1569 SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
1570 int, addrlen)
1572 struct socket *sock;
1573 struct sockaddr_storage address;
1574 int err, fput_needed;
1576 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1577 if (!sock)
1578 goto out;
1579 err = move_addr_to_kernel(uservaddr, addrlen, (struct sockaddr *)&address);
1580 if (err < 0)
1581 goto out_put;
1583 err =
1584 security_socket_connect(sock, (struct sockaddr *)&address, addrlen);
1585 if (err)
1586 goto out_put;
1588 err = sock->ops->connect(sock, (struct sockaddr *)&address, addrlen,
1589 sock->file->f_flags);
1590 out_put:
1591 fput_light(sock->file, fput_needed);
1592 out:
1593 return err;
1597 * Get the local address ('name') of a socket object. Move the obtained
1598 * name to user space.
1601 SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr,
1602 int __user *, usockaddr_len)
1604 struct socket *sock;
1605 struct sockaddr_storage address;
1606 int len, err, fput_needed;
1608 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1609 if (!sock)
1610 goto out;
1612 err = security_socket_getsockname(sock);
1613 if (err)
1614 goto out_put;
1616 err = sock->ops->getname(sock, (struct sockaddr *)&address, &len, 0);
1617 if (err)
1618 goto out_put;
1619 err = move_addr_to_user((struct sockaddr *)&address, len, usockaddr, usockaddr_len);
1621 out_put:
1622 fput_light(sock->file, fput_needed);
1623 out:
1624 return err;
1628 * Get the remote address ('name') of a socket object. Move the obtained
1629 * name to user space.
1632 SYSCALL_DEFINE3(getpeername, int, fd, struct sockaddr __user *, usockaddr,
1633 int __user *, usockaddr_len)
1635 struct socket *sock;
1636 struct sockaddr_storage address;
1637 int len, err, fput_needed;
1639 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1640 if (sock != NULL) {
1641 err = security_socket_getpeername(sock);
1642 if (err) {
1643 fput_light(sock->file, fput_needed);
1644 return err;
1647 err =
1648 sock->ops->getname(sock, (struct sockaddr *)&address, &len,
1650 if (!err)
1651 err = move_addr_to_user((struct sockaddr *)&address, len, usockaddr,
1652 usockaddr_len);
1653 fput_light(sock->file, fput_needed);
1655 return err;
1659 * Send a datagram to a given address. We move the address into kernel
1660 * space and check the user space data area is readable before invoking
1661 * the protocol.
1664 SYSCALL_DEFINE6(sendto, int, fd, void __user *, buff, size_t, len,
1665 unsigned, flags, struct sockaddr __user *, addr,
1666 int, addr_len)
1668 struct socket *sock;
1669 struct sockaddr_storage address;
1670 int err;
1671 struct msghdr msg;
1672 struct iovec iov;
1673 int fput_needed;
1675 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1676 if (!sock)
1677 goto out;
1679 iov.iov_base = buff;
1680 iov.iov_len = len;
1681 msg.msg_name = NULL;
1682 msg.msg_iov = &iov;
1683 msg.msg_iovlen = 1;
1684 msg.msg_control = NULL;
1685 msg.msg_controllen = 0;
1686 msg.msg_namelen = 0;
1687 if (addr) {
1688 err = move_addr_to_kernel(addr, addr_len, (struct sockaddr *)&address);
1689 if (err < 0)
1690 goto out_put;
1691 msg.msg_name = (struct sockaddr *)&address;
1692 msg.msg_namelen = addr_len;
1694 if (sock->file->f_flags & O_NONBLOCK)
1695 flags |= MSG_DONTWAIT;
1696 msg.msg_flags = flags;
1697 err = sock_sendmsg(sock, &msg, len);
1699 out_put:
1700 fput_light(sock->file, fput_needed);
1701 out:
1702 return err;
1706 * Send a datagram down a socket.
1709 SYSCALL_DEFINE4(send, int, fd, void __user *, buff, size_t, len,
1710 unsigned, flags)
1712 return sys_sendto(fd, buff, len, flags, NULL, 0);
1716 * Receive a frame from the socket and optionally record the address of the
1717 * sender. We verify the buffers are writable and if needed move the
1718 * sender address from kernel to user space.
1721 SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, ubuf, size_t, size,
1722 unsigned, flags, struct sockaddr __user *, addr,
1723 int __user *, addr_len)
1725 struct socket *sock;
1726 struct iovec iov;
1727 struct msghdr msg;
1728 struct sockaddr_storage address;
1729 int err, err2;
1730 int fput_needed;
1732 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1733 if (!sock)
1734 goto out;
1736 msg.msg_control = NULL;
1737 msg.msg_controllen = 0;
1738 msg.msg_iovlen = 1;
1739 msg.msg_iov = &iov;
1740 iov.iov_len = size;
1741 iov.iov_base = ubuf;
1742 msg.msg_name = (struct sockaddr *)&address;
1743 msg.msg_namelen = sizeof(address);
1744 if (sock->file->f_flags & O_NONBLOCK)
1745 flags |= MSG_DONTWAIT;
1746 err = sock_recvmsg(sock, &msg, size, flags);
1748 if (err >= 0 && addr != NULL) {
1749 err2 = move_addr_to_user((struct sockaddr *)&address,
1750 msg.msg_namelen, addr, addr_len);
1751 if (err2 < 0)
1752 err = err2;
1755 fput_light(sock->file, fput_needed);
1756 out:
1757 return err;
1761 * Receive a datagram from a socket.
1764 asmlinkage long sys_recv(int fd, void __user *ubuf, size_t size,
1765 unsigned flags)
1767 return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
1771 * Set a socket option. Because we don't know the option lengths we have
1772 * to pass the user mode parameter for the protocols to sort out.
1775 SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,
1776 char __user *, optval, int, optlen)
1778 int err, fput_needed;
1779 struct socket *sock;
1781 if (optlen < 0)
1782 return -EINVAL;
1784 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1785 if (sock != NULL) {
1786 err = security_socket_setsockopt(sock, level, optname);
1787 if (err)
1788 goto out_put;
1790 if (level == SOL_SOCKET)
1791 err =
1792 sock_setsockopt(sock, level, optname, optval,
1793 optlen);
1794 else
1795 err =
1796 sock->ops->setsockopt(sock, level, optname, optval,
1797 optlen);
1798 out_put:
1799 fput_light(sock->file, fput_needed);
1801 return err;
1805 * Get a socket option. Because we don't know the option lengths we have
1806 * to pass a user mode parameter for the protocols to sort out.
1809 SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname,
1810 char __user *, optval, int __user *, optlen)
1812 int err, fput_needed;
1813 struct socket *sock;
1815 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1816 if (sock != NULL) {
1817 err = security_socket_getsockopt(sock, level, optname);
1818 if (err)
1819 goto out_put;
1821 if (level == SOL_SOCKET)
1822 err =
1823 sock_getsockopt(sock, level, optname, optval,
1824 optlen);
1825 else
1826 err =
1827 sock->ops->getsockopt(sock, level, optname, optval,
1828 optlen);
1829 out_put:
1830 fput_light(sock->file, fput_needed);
1832 return err;
1836 * Shutdown a socket.
1839 SYSCALL_DEFINE2(shutdown, int, fd, int, how)
1841 int err, fput_needed;
1842 struct socket *sock;
1844 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1845 if (sock != NULL) {
1846 err = security_socket_shutdown(sock, how);
1847 if (!err)
1848 err = sock->ops->shutdown(sock, how);
1849 fput_light(sock->file, fput_needed);
1851 return err;
1854 /* A couple of helpful macros for getting the address of the 32/64 bit
1855 * fields which are the same type (int / unsigned) on our platforms.
1857 #define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
1858 #define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
1859 #define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
1862 * BSD sendmsg interface
1865 SYSCALL_DEFINE3(sendmsg, int, fd, struct msghdr __user *, msg, unsigned, flags)
1867 struct compat_msghdr __user *msg_compat =
1868 (struct compat_msghdr __user *)msg;
1869 struct socket *sock;
1870 struct sockaddr_storage address;
1871 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
1872 unsigned char ctl[sizeof(struct cmsghdr) + 20]
1873 __attribute__ ((aligned(sizeof(__kernel_size_t))));
1874 /* 20 is size of ipv6_pktinfo */
1875 unsigned char *ctl_buf = ctl;
1876 struct msghdr msg_sys;
1877 int err, ctl_len, iov_size, total_len;
1878 int fput_needed;
1880 err = -EFAULT;
1881 if (MSG_CMSG_COMPAT & flags) {
1882 if (get_compat_msghdr(&msg_sys, msg_compat))
1883 return -EFAULT;
1885 else if (copy_from_user(&msg_sys, msg, sizeof(struct msghdr)))
1886 return -EFAULT;
1888 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1889 if (!sock)
1890 goto out;
1892 /* do not move before msg_sys is valid */
1893 err = -EMSGSIZE;
1894 if (msg_sys.msg_iovlen > UIO_MAXIOV)
1895 goto out_put;
1897 /* Check whether to allocate the iovec area */
1898 err = -ENOMEM;
1899 iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
1900 if (msg_sys.msg_iovlen > UIO_FASTIOV) {
1901 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
1902 if (!iov)
1903 goto out_put;
1906 /* This will also move the address data into kernel space */
1907 if (MSG_CMSG_COMPAT & flags) {
1908 err = verify_compat_iovec(&msg_sys, iov,
1909 (struct sockaddr *)&address,
1910 VERIFY_READ);
1911 } else
1912 err = verify_iovec(&msg_sys, iov,
1913 (struct sockaddr *)&address,
1914 VERIFY_READ);
1915 if (err < 0)
1916 goto out_freeiov;
1917 total_len = err;
1919 err = -ENOBUFS;
1921 if (msg_sys.msg_controllen > INT_MAX)
1922 goto out_freeiov;
1923 ctl_len = msg_sys.msg_controllen;
1924 if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
1925 err =
1926 cmsghdr_from_user_compat_to_kern(&msg_sys, sock->sk, ctl,
1927 sizeof(ctl));
1928 if (err)
1929 goto out_freeiov;
1930 ctl_buf = msg_sys.msg_control;
1931 ctl_len = msg_sys.msg_controllen;
1932 } else if (ctl_len) {
1933 if (ctl_len > sizeof(ctl)) {
1934 ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
1935 if (ctl_buf == NULL)
1936 goto out_freeiov;
1938 err = -EFAULT;
1940 * Careful! Before this, msg_sys.msg_control contains a user pointer.
1941 * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
1942 * checking falls down on this.
1944 if (copy_from_user(ctl_buf, (void __user *)msg_sys.msg_control,
1945 ctl_len))
1946 goto out_freectl;
1947 msg_sys.msg_control = ctl_buf;
1949 msg_sys.msg_flags = flags;
1951 if (sock->file->f_flags & O_NONBLOCK)
1952 msg_sys.msg_flags |= MSG_DONTWAIT;
1953 err = sock_sendmsg(sock, &msg_sys, total_len);
1955 out_freectl:
1956 if (ctl_buf != ctl)
1957 sock_kfree_s(sock->sk, ctl_buf, ctl_len);
1958 out_freeiov:
1959 if (iov != iovstack)
1960 sock_kfree_s(sock->sk, iov, iov_size);
1961 out_put:
1962 fput_light(sock->file, fput_needed);
1963 out:
1964 return err;
1967 static int __sys_recvmsg(struct socket *sock, struct msghdr __user *msg,
1968 struct msghdr *msg_sys, unsigned flags, int nosec)
1970 struct compat_msghdr __user *msg_compat =
1971 (struct compat_msghdr __user *)msg;
1972 struct iovec iovstack[UIO_FASTIOV];
1973 struct iovec *iov = iovstack;
1974 unsigned long cmsg_ptr;
1975 int err, iov_size, total_len, len;
1977 /* kernel mode address */
1978 struct sockaddr_storage addr;
1980 /* user mode address pointers */
1981 struct sockaddr __user *uaddr;
1982 int __user *uaddr_len;
1984 if (MSG_CMSG_COMPAT & flags) {
1985 if (get_compat_msghdr(msg_sys, msg_compat))
1986 return -EFAULT;
1988 else if (copy_from_user(msg_sys, msg, sizeof(struct msghdr)))
1989 return -EFAULT;
1991 err = -EMSGSIZE;
1992 if (msg_sys->msg_iovlen > UIO_MAXIOV)
1993 goto out;
1995 /* Check whether to allocate the iovec area */
1996 err = -ENOMEM;
1997 iov_size = msg_sys->msg_iovlen * sizeof(struct iovec);
1998 if (msg_sys->msg_iovlen > UIO_FASTIOV) {
1999 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
2000 if (!iov)
2001 goto out;
2005 * Save the user-mode address (verify_iovec will change the
2006 * kernel msghdr to use the kernel address space)
2009 uaddr = (__force void __user *)msg_sys->msg_name;
2010 uaddr_len = COMPAT_NAMELEN(msg);
2011 if (MSG_CMSG_COMPAT & flags) {
2012 err = verify_compat_iovec(msg_sys, iov,
2013 (struct sockaddr *)&addr,
2014 VERIFY_WRITE);
2015 } else
2016 err = verify_iovec(msg_sys, iov,
2017 (struct sockaddr *)&addr,
2018 VERIFY_WRITE);
2019 if (err < 0)
2020 goto out_freeiov;
2021 total_len = err;
2023 cmsg_ptr = (unsigned long)msg_sys->msg_control;
2024 msg_sys->msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
2026 if (sock->file->f_flags & O_NONBLOCK)
2027 flags |= MSG_DONTWAIT;
2028 err = (nosec ? sock_recvmsg_nosec : sock_recvmsg)(sock, msg_sys,
2029 total_len, flags);
2030 if (err < 0)
2031 goto out_freeiov;
2032 len = err;
2034 if (uaddr != NULL) {
2035 err = move_addr_to_user((struct sockaddr *)&addr,
2036 msg_sys->msg_namelen, uaddr,
2037 uaddr_len);
2038 if (err < 0)
2039 goto out_freeiov;
2041 err = __put_user((msg_sys->msg_flags & ~MSG_CMSG_COMPAT),
2042 COMPAT_FLAGS(msg));
2043 if (err)
2044 goto out_freeiov;
2045 if (MSG_CMSG_COMPAT & flags)
2046 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
2047 &msg_compat->msg_controllen);
2048 else
2049 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
2050 &msg->msg_controllen);
2051 if (err)
2052 goto out_freeiov;
2053 err = len;
2055 out_freeiov:
2056 if (iov != iovstack)
2057 sock_kfree_s(sock->sk, iov, iov_size);
2058 out:
2059 return err;
2063 * BSD recvmsg interface
2066 SYSCALL_DEFINE3(recvmsg, int, fd, struct msghdr __user *, msg,
2067 unsigned int, flags)
2069 int fput_needed, err;
2070 struct msghdr msg_sys;
2071 struct socket *sock = sockfd_lookup_light(fd, &err, &fput_needed);
2073 if (!sock)
2074 goto out;
2076 err = __sys_recvmsg(sock, msg, &msg_sys, flags, 0);
2078 fput_light(sock->file, fput_needed);
2079 out:
2080 return err;
2084 * Linux recvmmsg interface
2087 int __sys_recvmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
2088 unsigned int flags, struct timespec *timeout)
2090 int fput_needed, err, datagrams;
2091 struct socket *sock;
2092 struct mmsghdr __user *entry;
2093 struct compat_mmsghdr __user *compat_entry;
2094 struct msghdr msg_sys;
2095 struct timespec end_time;
2097 if (timeout &&
2098 poll_select_set_timeout(&end_time, timeout->tv_sec,
2099 timeout->tv_nsec))
2100 return -EINVAL;
2102 datagrams = 0;
2104 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2105 if (!sock)
2106 return err;
2108 err = sock_error(sock->sk);
2109 if (err)
2110 goto out_put;
2112 entry = mmsg;
2113 compat_entry = (struct compat_mmsghdr __user *)mmsg;
2115 while (datagrams < vlen) {
2117 * No need to ask LSM for more than the first datagram.
2119 if (MSG_CMSG_COMPAT & flags) {
2120 err = __sys_recvmsg(sock, (struct msghdr __user *)compat_entry,
2121 &msg_sys, flags, datagrams);
2122 if (err < 0)
2123 break;
2124 err = __put_user(err, &compat_entry->msg_len);
2125 ++compat_entry;
2126 } else {
2127 err = __sys_recvmsg(sock, (struct msghdr __user *)entry,
2128 &msg_sys, flags, datagrams);
2129 if (err < 0)
2130 break;
2131 err = put_user(err, &entry->msg_len);
2132 ++entry;
2135 if (err)
2136 break;
2137 ++datagrams;
2139 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
2140 if (flags & MSG_WAITFORONE)
2141 flags |= MSG_DONTWAIT;
2143 if (timeout) {
2144 ktime_get_ts(timeout);
2145 *timeout = timespec_sub(end_time, *timeout);
2146 if (timeout->tv_sec < 0) {
2147 timeout->tv_sec = timeout->tv_nsec = 0;
2148 break;
2151 /* Timeout, return less than vlen datagrams */
2152 if (timeout->tv_nsec == 0 && timeout->tv_sec == 0)
2153 break;
2156 /* Out of band data, return right away */
2157 if (msg_sys.msg_flags & MSG_OOB)
2158 break;
2161 out_put:
2162 fput_light(sock->file, fput_needed);
2164 if (err == 0)
2165 return datagrams;
2167 if (datagrams != 0) {
2169 * We may return less entries than requested (vlen) if the
2170 * sock is non block and there aren't enough datagrams...
2172 if (err != -EAGAIN) {
2174 * ... or if recvmsg returns an error after we
2175 * received some datagrams, where we record the
2176 * error to return on the next call or if the
2177 * app asks about it using getsockopt(SO_ERROR).
2179 sock->sk->sk_err = -err;
2182 return datagrams;
2185 return err;
2188 SYSCALL_DEFINE5(recvmmsg, int, fd, struct mmsghdr __user *, mmsg,
2189 unsigned int, vlen, unsigned int, flags,
2190 struct timespec __user *, timeout)
2192 int datagrams;
2193 struct timespec timeout_sys;
2195 if (!timeout)
2196 return __sys_recvmmsg(fd, mmsg, vlen, flags, NULL);
2198 if (copy_from_user(&timeout_sys, timeout, sizeof(timeout_sys)))
2199 return -EFAULT;
2201 datagrams = __sys_recvmmsg(fd, mmsg, vlen, flags, &timeout_sys);
2203 if (datagrams > 0 &&
2204 copy_to_user(timeout, &timeout_sys, sizeof(timeout_sys)))
2205 datagrams = -EFAULT;
2207 return datagrams;
2210 #ifdef __ARCH_WANT_SYS_SOCKETCALL
2211 /* Argument list sizes for sys_socketcall */
2212 #define AL(x) ((x) * sizeof(unsigned long))
2213 static const unsigned char nargs[20] = {
2214 AL(0),AL(3),AL(3),AL(3),AL(2),AL(3),
2215 AL(3),AL(3),AL(4),AL(4),AL(4),AL(6),
2216 AL(6),AL(2),AL(5),AL(5),AL(3),AL(3),
2217 AL(4),AL(5)
2220 #undef AL
2223 * System call vectors.
2225 * Argument checking cleaned up. Saved 20% in size.
2226 * This function doesn't need to set the kernel lock because
2227 * it is set by the callees.
2230 SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args)
2232 unsigned long a[6];
2233 unsigned long a0, a1;
2234 int err;
2235 unsigned int len;
2237 if (call < 1 || call > SYS_RECVMMSG)
2238 return -EINVAL;
2240 len = nargs[call];
2241 if (len > sizeof(a))
2242 return -EINVAL;
2244 /* copy_from_user should be SMP safe. */
2245 if (copy_from_user(a, args, len))
2246 return -EFAULT;
2248 audit_socketcall(nargs[call] / sizeof(unsigned long), a);
2250 a0 = a[0];
2251 a1 = a[1];
2253 switch (call) {
2254 case SYS_SOCKET:
2255 err = sys_socket(a0, a1, a[2]);
2256 break;
2257 case SYS_BIND:
2258 err = sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
2259 break;
2260 case SYS_CONNECT:
2261 err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
2262 break;
2263 case SYS_LISTEN:
2264 err = sys_listen(a0, a1);
2265 break;
2266 case SYS_ACCEPT:
2267 err = sys_accept4(a0, (struct sockaddr __user *)a1,
2268 (int __user *)a[2], 0);
2269 break;
2270 case SYS_GETSOCKNAME:
2271 err =
2272 sys_getsockname(a0, (struct sockaddr __user *)a1,
2273 (int __user *)a[2]);
2274 break;
2275 case SYS_GETPEERNAME:
2276 err =
2277 sys_getpeername(a0, (struct sockaddr __user *)a1,
2278 (int __user *)a[2]);
2279 break;
2280 case SYS_SOCKETPAIR:
2281 err = sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
2282 break;
2283 case SYS_SEND:
2284 err = sys_send(a0, (void __user *)a1, a[2], a[3]);
2285 break;
2286 case SYS_SENDTO:
2287 err = sys_sendto(a0, (void __user *)a1, a[2], a[3],
2288 (struct sockaddr __user *)a[4], a[5]);
2289 break;
2290 case SYS_RECV:
2291 err = sys_recv(a0, (void __user *)a1, a[2], a[3]);
2292 break;
2293 case SYS_RECVFROM:
2294 err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
2295 (struct sockaddr __user *)a[4],
2296 (int __user *)a[5]);
2297 break;
2298 case SYS_SHUTDOWN:
2299 err = sys_shutdown(a0, a1);
2300 break;
2301 case SYS_SETSOCKOPT:
2302 err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]);
2303 break;
2304 case SYS_GETSOCKOPT:
2305 err =
2306 sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
2307 (int __user *)a[4]);
2308 break;
2309 case SYS_SENDMSG:
2310 err = sys_sendmsg(a0, (struct msghdr __user *)a1, a[2]);
2311 break;
2312 case SYS_RECVMSG:
2313 err = sys_recvmsg(a0, (struct msghdr __user *)a1, a[2]);
2314 break;
2315 case SYS_RECVMMSG:
2316 err = sys_recvmmsg(a0, (struct mmsghdr __user *)a1, a[2], a[3],
2317 (struct timespec __user *)a[4]);
2318 break;
2319 case SYS_ACCEPT4:
2320 err = sys_accept4(a0, (struct sockaddr __user *)a1,
2321 (int __user *)a[2], a[3]);
2322 break;
2323 default:
2324 err = -EINVAL;
2325 break;
2327 return err;
2330 #endif /* __ARCH_WANT_SYS_SOCKETCALL */
2333 * sock_register - add a socket protocol handler
2334 * @ops: description of protocol
2336 * This function is called by a protocol handler that wants to
2337 * advertise its address family, and have it linked into the
2338 * socket interface. The value ops->family coresponds to the
2339 * socket system call protocol family.
2341 int sock_register(const struct net_proto_family *ops)
2343 int err;
2345 if (ops->family >= NPROTO) {
2346 printk(KERN_CRIT "protocol %d >= NPROTO(%d)\n", ops->family,
2347 NPROTO);
2348 return -ENOBUFS;
2351 spin_lock(&net_family_lock);
2352 if (net_families[ops->family])
2353 err = -EEXIST;
2354 else {
2355 net_families[ops->family] = ops;
2356 err = 0;
2358 spin_unlock(&net_family_lock);
2360 printk(KERN_INFO "NET: Registered protocol family %d\n", ops->family);
2361 return err;
2365 * sock_unregister - remove a protocol handler
2366 * @family: protocol family to remove
2368 * This function is called by a protocol handler that wants to
2369 * remove its address family, and have it unlinked from the
2370 * new socket creation.
2372 * If protocol handler is a module, then it can use module reference
2373 * counts to protect against new references. If protocol handler is not
2374 * a module then it needs to provide its own protection in
2375 * the ops->create routine.
2377 void sock_unregister(int family)
2379 BUG_ON(family < 0 || family >= NPROTO);
2381 spin_lock(&net_family_lock);
2382 net_families[family] = NULL;
2383 spin_unlock(&net_family_lock);
2385 synchronize_rcu();
2387 printk(KERN_INFO "NET: Unregistered protocol family %d\n", family);
2390 static int __init sock_init(void)
2393 * Initialize sock SLAB cache.
2396 sk_init();
2399 * Initialize skbuff SLAB cache
2401 skb_init();
2404 * Initialize the protocols module.
2407 init_inodecache();
2408 register_filesystem(&sock_fs_type);
2409 sock_mnt = kern_mount(&sock_fs_type);
2411 /* The real protocol initialization is performed in later initcalls.
2414 #ifdef CONFIG_NETFILTER
2415 netfilter_init();
2416 #endif
2418 return 0;
2421 core_initcall(sock_init); /* early initcall */
2423 #ifdef CONFIG_PROC_FS
2424 void socket_seq_show(struct seq_file *seq)
2426 int cpu;
2427 int counter = 0;
2429 for_each_possible_cpu(cpu)
2430 counter += per_cpu(sockets_in_use, cpu);
2432 /* It can be negative, by the way. 8) */
2433 if (counter < 0)
2434 counter = 0;
2436 seq_printf(seq, "sockets: used %d\n", counter);
2438 #endif /* CONFIG_PROC_FS */
2440 #ifdef CONFIG_COMPAT
2441 static int do_siocgstamp(struct net *net, struct socket *sock,
2442 unsigned int cmd, struct compat_timeval __user *up)
2444 mm_segment_t old_fs = get_fs();
2445 struct timeval ktv;
2446 int err;
2448 set_fs(KERNEL_DS);
2449 err = sock_do_ioctl(net, sock, cmd, (unsigned long)&ktv);
2450 set_fs(old_fs);
2451 if (!err) {
2452 err = put_user(ktv.tv_sec, &up->tv_sec);
2453 err |= __put_user(ktv.tv_usec, &up->tv_usec);
2455 return err;
2458 static int do_siocgstampns(struct net *net, struct socket *sock,
2459 unsigned int cmd, struct compat_timespec __user *up)
2461 mm_segment_t old_fs = get_fs();
2462 struct timespec kts;
2463 int err;
2465 set_fs(KERNEL_DS);
2466 err = sock_do_ioctl(net, sock, cmd, (unsigned long)&kts);
2467 set_fs(old_fs);
2468 if (!err) {
2469 err = put_user(kts.tv_sec, &up->tv_sec);
2470 err |= __put_user(kts.tv_nsec, &up->tv_nsec);
2472 return err;
2475 static int dev_ifname32(struct net *net, struct compat_ifreq __user *uifr32)
2477 struct ifreq __user *uifr;
2478 int err;
2480 uifr = compat_alloc_user_space(sizeof(struct ifreq));
2481 if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
2482 return -EFAULT;
2484 err = dev_ioctl(net, SIOCGIFNAME, uifr);
2485 if (err)
2486 return err;
2488 if (copy_in_user(uifr32, uifr, sizeof(struct compat_ifreq)))
2489 return -EFAULT;
2491 return 0;
2494 static int dev_ifconf(struct net *net, struct compat_ifconf __user *uifc32)
2496 struct compat_ifconf ifc32;
2497 struct ifconf ifc;
2498 struct ifconf __user *uifc;
2499 struct compat_ifreq __user *ifr32;
2500 struct ifreq __user *ifr;
2501 unsigned int i, j;
2502 int err;
2504 if (copy_from_user(&ifc32, uifc32, sizeof(struct compat_ifconf)))
2505 return -EFAULT;
2507 if (ifc32.ifcbuf == 0) {
2508 ifc32.ifc_len = 0;
2509 ifc.ifc_len = 0;
2510 ifc.ifc_req = NULL;
2511 uifc = compat_alloc_user_space(sizeof(struct ifconf));
2512 } else {
2513 size_t len =((ifc32.ifc_len / sizeof (struct compat_ifreq)) + 1) *
2514 sizeof (struct ifreq);
2515 uifc = compat_alloc_user_space(sizeof(struct ifconf) + len);
2516 ifc.ifc_len = len;
2517 ifr = ifc.ifc_req = (void __user *)(uifc + 1);
2518 ifr32 = compat_ptr(ifc32.ifcbuf);
2519 for (i = 0; i < ifc32.ifc_len; i += sizeof (struct compat_ifreq)) {
2520 if (copy_in_user(ifr, ifr32, sizeof(struct compat_ifreq)))
2521 return -EFAULT;
2522 ifr++;
2523 ifr32++;
2526 if (copy_to_user(uifc, &ifc, sizeof(struct ifconf)))
2527 return -EFAULT;
2529 err = dev_ioctl(net, SIOCGIFCONF, uifc);
2530 if (err)
2531 return err;
2533 if (copy_from_user(&ifc, uifc, sizeof(struct ifconf)))
2534 return -EFAULT;
2536 ifr = ifc.ifc_req;
2537 ifr32 = compat_ptr(ifc32.ifcbuf);
2538 for (i = 0, j = 0;
2539 i + sizeof (struct compat_ifreq) <= ifc32.ifc_len && j < ifc.ifc_len;
2540 i += sizeof (struct compat_ifreq), j += sizeof (struct ifreq)) {
2541 if (copy_in_user(ifr32, ifr, sizeof (struct compat_ifreq)))
2542 return -EFAULT;
2543 ifr32++;
2544 ifr++;
2547 if (ifc32.ifcbuf == 0) {
2548 /* Translate from 64-bit structure multiple to
2549 * a 32-bit one.
2551 i = ifc.ifc_len;
2552 i = ((i / sizeof(struct ifreq)) * sizeof(struct compat_ifreq));
2553 ifc32.ifc_len = i;
2554 } else {
2555 ifc32.ifc_len = i;
2557 if (copy_to_user(uifc32, &ifc32, sizeof(struct compat_ifconf)))
2558 return -EFAULT;
2560 return 0;
2563 static int ethtool_ioctl(struct net *net, struct compat_ifreq __user *ifr32)
2565 struct ifreq __user *ifr;
2566 u32 data;
2567 void __user *datap;
2569 ifr = compat_alloc_user_space(sizeof(*ifr));
2571 if (copy_in_user(&ifr->ifr_name, &ifr32->ifr_name, IFNAMSIZ))
2572 return -EFAULT;
2574 if (get_user(data, &ifr32->ifr_ifru.ifru_data))
2575 return -EFAULT;
2577 datap = compat_ptr(data);
2578 if (put_user(datap, &ifr->ifr_ifru.ifru_data))
2579 return -EFAULT;
2581 return dev_ioctl(net, SIOCETHTOOL, ifr);
2584 static int compat_siocwandev(struct net *net, struct compat_ifreq __user *uifr32)
2586 void __user *uptr;
2587 compat_uptr_t uptr32;
2588 struct ifreq __user *uifr;
2590 uifr = compat_alloc_user_space(sizeof (*uifr));
2591 if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
2592 return -EFAULT;
2594 if (get_user(uptr32, &uifr32->ifr_settings.ifs_ifsu))
2595 return -EFAULT;
2597 uptr = compat_ptr(uptr32);
2599 if (put_user(uptr, &uifr->ifr_settings.ifs_ifsu.raw_hdlc))
2600 return -EFAULT;
2602 return dev_ioctl(net, SIOCWANDEV, uifr);
2605 static int bond_ioctl(struct net *net, unsigned int cmd,
2606 struct compat_ifreq __user *ifr32)
2608 struct ifreq kifr;
2609 struct ifreq __user *uifr;
2610 mm_segment_t old_fs;
2611 int err;
2612 u32 data;
2613 void __user *datap;
2615 switch (cmd) {
2616 case SIOCBONDENSLAVE:
2617 case SIOCBONDRELEASE:
2618 case SIOCBONDSETHWADDR:
2619 case SIOCBONDCHANGEACTIVE:
2620 if (copy_from_user(&kifr, ifr32, sizeof(struct compat_ifreq)))
2621 return -EFAULT;
2623 old_fs = get_fs();
2624 set_fs (KERNEL_DS);
2625 err = dev_ioctl(net, cmd, &kifr);
2626 set_fs (old_fs);
2628 return err;
2629 case SIOCBONDSLAVEINFOQUERY:
2630 case SIOCBONDINFOQUERY:
2631 uifr = compat_alloc_user_space(sizeof(*uifr));
2632 if (copy_in_user(&uifr->ifr_name, &ifr32->ifr_name, IFNAMSIZ))
2633 return -EFAULT;
2635 if (get_user(data, &ifr32->ifr_ifru.ifru_data))
2636 return -EFAULT;
2638 datap = compat_ptr(data);
2639 if (put_user(datap, &uifr->ifr_ifru.ifru_data))
2640 return -EFAULT;
2642 return dev_ioctl(net, cmd, uifr);
2643 default:
2644 return -EINVAL;
2648 static int siocdevprivate_ioctl(struct net *net, unsigned int cmd,
2649 struct compat_ifreq __user *u_ifreq32)
2651 struct ifreq __user *u_ifreq64;
2652 char tmp_buf[IFNAMSIZ];
2653 void __user *data64;
2654 u32 data32;
2656 if (copy_from_user(&tmp_buf[0], &(u_ifreq32->ifr_ifrn.ifrn_name[0]),
2657 IFNAMSIZ))
2658 return -EFAULT;
2659 if (__get_user(data32, &u_ifreq32->ifr_ifru.ifru_data))
2660 return -EFAULT;
2661 data64 = compat_ptr(data32);
2663 u_ifreq64 = compat_alloc_user_space(sizeof(*u_ifreq64));
2665 /* Don't check these user accesses, just let that get trapped
2666 * in the ioctl handler instead.
2668 if (copy_to_user(&u_ifreq64->ifr_ifrn.ifrn_name[0], &tmp_buf[0],
2669 IFNAMSIZ))
2670 return -EFAULT;
2671 if (__put_user(data64, &u_ifreq64->ifr_ifru.ifru_data))
2672 return -EFAULT;
2674 return dev_ioctl(net, cmd, u_ifreq64);
2677 static int dev_ifsioc(struct net *net, struct socket *sock,
2678 unsigned int cmd, struct compat_ifreq __user *uifr32)
2680 struct ifreq __user *uifr;
2681 int err;
2683 uifr = compat_alloc_user_space(sizeof(*uifr));
2684 if (copy_in_user(uifr, uifr32, sizeof(*uifr32)))
2685 return -EFAULT;
2687 err = sock_do_ioctl(net, sock, cmd, (unsigned long)uifr);
2689 if (!err) {
2690 switch (cmd) {
2691 case SIOCGIFFLAGS:
2692 case SIOCGIFMETRIC:
2693 case SIOCGIFMTU:
2694 case SIOCGIFMEM:
2695 case SIOCGIFHWADDR:
2696 case SIOCGIFINDEX:
2697 case SIOCGIFADDR:
2698 case SIOCGIFBRDADDR:
2699 case SIOCGIFDSTADDR:
2700 case SIOCGIFNETMASK:
2701 case SIOCGIFPFLAGS:
2702 case SIOCGIFTXQLEN:
2703 case SIOCGMIIPHY:
2704 case SIOCGMIIREG:
2705 if (copy_in_user(uifr32, uifr, sizeof(*uifr32)))
2706 err = -EFAULT;
2707 break;
2710 return err;
2713 static int compat_sioc_ifmap(struct net *net, unsigned int cmd,
2714 struct compat_ifreq __user *uifr32)
2716 struct ifreq ifr;
2717 struct compat_ifmap __user *uifmap32;
2718 mm_segment_t old_fs;
2719 int err;
2721 uifmap32 = &uifr32->ifr_ifru.ifru_map;
2722 err = copy_from_user(&ifr, uifr32, sizeof(ifr.ifr_name));
2723 err |= __get_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
2724 err |= __get_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
2725 err |= __get_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
2726 err |= __get_user(ifr.ifr_map.irq, &uifmap32->irq);
2727 err |= __get_user(ifr.ifr_map.dma, &uifmap32->dma);
2728 err |= __get_user(ifr.ifr_map.port, &uifmap32->port);
2729 if (err)
2730 return -EFAULT;
2732 old_fs = get_fs();
2733 set_fs (KERNEL_DS);
2734 err = dev_ioctl(net, cmd, (void __user *)&ifr);
2735 set_fs (old_fs);
2737 if (cmd == SIOCGIFMAP && !err) {
2738 err = copy_to_user(uifr32, &ifr, sizeof(ifr.ifr_name));
2739 err |= __put_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
2740 err |= __put_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
2741 err |= __put_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
2742 err |= __put_user(ifr.ifr_map.irq, &uifmap32->irq);
2743 err |= __put_user(ifr.ifr_map.dma, &uifmap32->dma);
2744 err |= __put_user(ifr.ifr_map.port, &uifmap32->port);
2745 if (err)
2746 err = -EFAULT;
2748 return err;
2751 static int compat_siocshwtstamp(struct net *net, struct compat_ifreq __user *uifr32)
2753 void __user *uptr;
2754 compat_uptr_t uptr32;
2755 struct ifreq __user *uifr;
2757 uifr = compat_alloc_user_space(sizeof (*uifr));
2758 if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
2759 return -EFAULT;
2761 if (get_user(uptr32, &uifr32->ifr_data))
2762 return -EFAULT;
2764 uptr = compat_ptr(uptr32);
2766 if (put_user(uptr, &uifr->ifr_data))
2767 return -EFAULT;
2769 return dev_ioctl(net, SIOCSHWTSTAMP, uifr);
2772 struct rtentry32 {
2773 u32 rt_pad1;
2774 struct sockaddr rt_dst; /* target address */
2775 struct sockaddr rt_gateway; /* gateway addr (RTF_GATEWAY) */
2776 struct sockaddr rt_genmask; /* target network mask (IP) */
2777 unsigned short rt_flags;
2778 short rt_pad2;
2779 u32 rt_pad3;
2780 unsigned char rt_tos;
2781 unsigned char rt_class;
2782 short rt_pad4;
2783 short rt_metric; /* +1 for binary compatibility! */
2784 /* char * */ u32 rt_dev; /* forcing the device at add */
2785 u32 rt_mtu; /* per route MTU/Window */
2786 u32 rt_window; /* Window clamping */
2787 unsigned short rt_irtt; /* Initial RTT */
2790 struct in6_rtmsg32 {
2791 struct in6_addr rtmsg_dst;
2792 struct in6_addr rtmsg_src;
2793 struct in6_addr rtmsg_gateway;
2794 u32 rtmsg_type;
2795 u16 rtmsg_dst_len;
2796 u16 rtmsg_src_len;
2797 u32 rtmsg_metric;
2798 u32 rtmsg_info;
2799 u32 rtmsg_flags;
2800 s32 rtmsg_ifindex;
2803 static int routing_ioctl(struct net *net, struct socket *sock,
2804 unsigned int cmd, void __user *argp)
2806 int ret;
2807 void *r = NULL;
2808 struct in6_rtmsg r6;
2809 struct rtentry r4;
2810 char devname[16];
2811 u32 rtdev;
2812 mm_segment_t old_fs = get_fs();
2814 if (sock && sock->sk && sock->sk->sk_family == AF_INET6) { /* ipv6 */
2815 struct in6_rtmsg32 __user *ur6 = argp;
2816 ret = copy_from_user (&r6.rtmsg_dst, &(ur6->rtmsg_dst),
2817 3 * sizeof(struct in6_addr));
2818 ret |= __get_user (r6.rtmsg_type, &(ur6->rtmsg_type));
2819 ret |= __get_user (r6.rtmsg_dst_len, &(ur6->rtmsg_dst_len));
2820 ret |= __get_user (r6.rtmsg_src_len, &(ur6->rtmsg_src_len));
2821 ret |= __get_user (r6.rtmsg_metric, &(ur6->rtmsg_metric));
2822 ret |= __get_user (r6.rtmsg_info, &(ur6->rtmsg_info));
2823 ret |= __get_user (r6.rtmsg_flags, &(ur6->rtmsg_flags));
2824 ret |= __get_user (r6.rtmsg_ifindex, &(ur6->rtmsg_ifindex));
2826 r = (void *) &r6;
2827 } else { /* ipv4 */
2828 struct rtentry32 __user *ur4 = argp;
2829 ret = copy_from_user (&r4.rt_dst, &(ur4->rt_dst),
2830 3 * sizeof(struct sockaddr));
2831 ret |= __get_user (r4.rt_flags, &(ur4->rt_flags));
2832 ret |= __get_user (r4.rt_metric, &(ur4->rt_metric));
2833 ret |= __get_user (r4.rt_mtu, &(ur4->rt_mtu));
2834 ret |= __get_user (r4.rt_window, &(ur4->rt_window));
2835 ret |= __get_user (r4.rt_irtt, &(ur4->rt_irtt));
2836 ret |= __get_user (rtdev, &(ur4->rt_dev));
2837 if (rtdev) {
2838 ret |= copy_from_user (devname, compat_ptr(rtdev), 15);
2839 r4.rt_dev = devname; devname[15] = 0;
2840 } else
2841 r4.rt_dev = NULL;
2843 r = (void *) &r4;
2846 if (ret) {
2847 ret = -EFAULT;
2848 goto out;
2851 set_fs (KERNEL_DS);
2852 ret = sock_do_ioctl(net, sock, cmd, (unsigned long) r);
2853 set_fs (old_fs);
2855 out:
2856 return ret;
2859 /* Since old style bridge ioctl's endup using SIOCDEVPRIVATE
2860 * for some operations; this forces use of the newer bridge-utils that
2861 * use compatiable ioctls
2863 static int old_bridge_ioctl(compat_ulong_t __user *argp)
2865 compat_ulong_t tmp;
2867 if (get_user(tmp, argp))
2868 return -EFAULT;
2869 if (tmp == BRCTL_GET_VERSION)
2870 return BRCTL_VERSION + 1;
2871 return -EINVAL;
2874 static int compat_sock_ioctl_trans(struct file *file, struct socket *sock,
2875 unsigned int cmd, unsigned long arg)
2877 void __user *argp = compat_ptr(arg);
2878 struct sock *sk = sock->sk;
2879 struct net *net = sock_net(sk);
2881 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))
2882 return siocdevprivate_ioctl(net, cmd, argp);
2884 switch (cmd) {
2885 case SIOCSIFBR:
2886 case SIOCGIFBR:
2887 return old_bridge_ioctl(argp);
2888 case SIOCGIFNAME:
2889 return dev_ifname32(net, argp);
2890 case SIOCGIFCONF:
2891 return dev_ifconf(net, argp);
2892 case SIOCETHTOOL:
2893 return ethtool_ioctl(net, argp);
2894 case SIOCWANDEV:
2895 return compat_siocwandev(net, argp);
2896 case SIOCGIFMAP:
2897 case SIOCSIFMAP:
2898 return compat_sioc_ifmap(net, cmd, argp);
2899 case SIOCBONDENSLAVE:
2900 case SIOCBONDRELEASE:
2901 case SIOCBONDSETHWADDR:
2902 case SIOCBONDSLAVEINFOQUERY:
2903 case SIOCBONDINFOQUERY:
2904 case SIOCBONDCHANGEACTIVE:
2905 return bond_ioctl(net, cmd, argp);
2906 case SIOCADDRT:
2907 case SIOCDELRT:
2908 return routing_ioctl(net, sock, cmd, argp);
2909 case SIOCGSTAMP:
2910 return do_siocgstamp(net, sock, cmd, argp);
2911 case SIOCGSTAMPNS:
2912 return do_siocgstampns(net, sock, cmd, argp);
2913 case SIOCSHWTSTAMP:
2914 return compat_siocshwtstamp(net, argp);
2916 case FIOSETOWN:
2917 case SIOCSPGRP:
2918 case FIOGETOWN:
2919 case SIOCGPGRP:
2920 case SIOCBRADDBR:
2921 case SIOCBRDELBR:
2922 case SIOCGIFVLAN:
2923 case SIOCSIFVLAN:
2924 case SIOCADDDLCI:
2925 case SIOCDELDLCI:
2926 return sock_ioctl(file, cmd, arg);
2928 case SIOCGIFFLAGS:
2929 case SIOCSIFFLAGS:
2930 case SIOCGIFMETRIC:
2931 case SIOCSIFMETRIC:
2932 case SIOCGIFMTU:
2933 case SIOCSIFMTU:
2934 case SIOCGIFMEM:
2935 case SIOCSIFMEM:
2936 case SIOCGIFHWADDR:
2937 case SIOCSIFHWADDR:
2938 case SIOCADDMULTI:
2939 case SIOCDELMULTI:
2940 case SIOCGIFINDEX:
2941 case SIOCGIFADDR:
2942 case SIOCSIFADDR:
2943 case SIOCSIFHWBROADCAST:
2944 case SIOCDIFADDR:
2945 case SIOCGIFBRDADDR:
2946 case SIOCSIFBRDADDR:
2947 case SIOCGIFDSTADDR:
2948 case SIOCSIFDSTADDR:
2949 case SIOCGIFNETMASK:
2950 case SIOCSIFNETMASK:
2951 case SIOCSIFPFLAGS:
2952 case SIOCGIFPFLAGS:
2953 case SIOCGIFTXQLEN:
2954 case SIOCSIFTXQLEN:
2955 case SIOCBRADDIF:
2956 case SIOCBRDELIF:
2957 case SIOCSIFNAME:
2958 case SIOCGMIIPHY:
2959 case SIOCGMIIREG:
2960 case SIOCSMIIREG:
2961 return dev_ifsioc(net, sock, cmd, argp);
2963 case SIOCSARP:
2964 case SIOCGARP:
2965 case SIOCDARP:
2966 case SIOCATMARK:
2967 return sock_do_ioctl(net, sock, cmd, arg);
2970 /* Prevent warning from compat_sys_ioctl, these always
2971 * result in -EINVAL in the native case anyway. */
2972 switch (cmd) {
2973 case SIOCRTMSG:
2974 case SIOCGIFCOUNT:
2975 case SIOCSRARP:
2976 case SIOCGRARP:
2977 case SIOCDRARP:
2978 case SIOCSIFLINK:
2979 case SIOCGIFSLAVE:
2980 case SIOCSIFSLAVE:
2981 return -EINVAL;
2984 return -ENOIOCTLCMD;
2987 static long compat_sock_ioctl(struct file *file, unsigned cmd,
2988 unsigned long arg)
2990 struct socket *sock = file->private_data;
2991 int ret = -ENOIOCTLCMD;
2992 struct sock *sk;
2993 struct net *net;
2995 sk = sock->sk;
2996 net = sock_net(sk);
2998 if (sock->ops->compat_ioctl)
2999 ret = sock->ops->compat_ioctl(sock, cmd, arg);
3001 if (ret == -ENOIOCTLCMD &&
3002 (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST))
3003 ret = compat_wext_handle_ioctl(net, cmd, arg);
3005 if (ret == -ENOIOCTLCMD)
3006 ret = compat_sock_ioctl_trans(file, sock, cmd, arg);
3008 return ret;
3010 #endif
3012 int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
3014 return sock->ops->bind(sock, addr, addrlen);
3017 int kernel_listen(struct socket *sock, int backlog)
3019 return sock->ops->listen(sock, backlog);
3022 int kernel_accept(struct socket *sock, struct socket **newsock, int flags)
3024 struct sock *sk = sock->sk;
3025 int err;
3027 err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
3028 newsock);
3029 if (err < 0)
3030 goto done;
3032 err = sock->ops->accept(sock, *newsock, flags);
3033 if (err < 0) {
3034 sock_release(*newsock);
3035 *newsock = NULL;
3036 goto done;
3039 (*newsock)->ops = sock->ops;
3040 __module_get((*newsock)->ops->owner);
3042 done:
3043 return err;
3046 int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
3047 int flags)
3049 return sock->ops->connect(sock, addr, addrlen, flags);
3052 int kernel_getsockname(struct socket *sock, struct sockaddr *addr,
3053 int *addrlen)
3055 return sock->ops->getname(sock, addr, addrlen, 0);
3058 int kernel_getpeername(struct socket *sock, struct sockaddr *addr,
3059 int *addrlen)
3061 return sock->ops->getname(sock, addr, addrlen, 1);
3064 int kernel_getsockopt(struct socket *sock, int level, int optname,
3065 char *optval, int *optlen)
3067 mm_segment_t oldfs = get_fs();
3068 int err;
3070 set_fs(KERNEL_DS);
3071 if (level == SOL_SOCKET)
3072 err = sock_getsockopt(sock, level, optname, optval, optlen);
3073 else
3074 err = sock->ops->getsockopt(sock, level, optname, optval,
3075 optlen);
3076 set_fs(oldfs);
3077 return err;
3080 int kernel_setsockopt(struct socket *sock, int level, int optname,
3081 char *optval, unsigned int optlen)
3083 mm_segment_t oldfs = get_fs();
3084 int err;
3086 set_fs(KERNEL_DS);
3087 if (level == SOL_SOCKET)
3088 err = sock_setsockopt(sock, level, optname, optval, optlen);
3089 else
3090 err = sock->ops->setsockopt(sock, level, optname, optval,
3091 optlen);
3092 set_fs(oldfs);
3093 return err;
3096 int kernel_sendpage(struct socket *sock, struct page *page, int offset,
3097 size_t size, int flags)
3099 if (sock->ops->sendpage)
3100 return sock->ops->sendpage(sock, page, offset, size, flags);
3102 return sock_no_sendpage(sock, page, offset, size, flags);
3105 int kernel_sock_ioctl(struct socket *sock, int cmd, unsigned long arg)
3107 mm_segment_t oldfs = get_fs();
3108 int err;
3110 set_fs(KERNEL_DS);
3111 err = sock->ops->ioctl(sock, cmd, arg);
3112 set_fs(oldfs);
3114 return err;
3117 int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how)
3119 return sock->ops->shutdown(sock, how);
3122 EXPORT_SYMBOL(sock_create);
3123 EXPORT_SYMBOL(sock_create_kern);
3124 EXPORT_SYMBOL(sock_create_lite);
3125 EXPORT_SYMBOL(sock_map_fd);
3126 EXPORT_SYMBOL(sock_recvmsg);
3127 EXPORT_SYMBOL(sock_register);
3128 EXPORT_SYMBOL(sock_release);
3129 EXPORT_SYMBOL(sock_sendmsg);
3130 EXPORT_SYMBOL(sock_unregister);
3131 EXPORT_SYMBOL(sock_wake_async);
3132 EXPORT_SYMBOL(sockfd_lookup);
3133 EXPORT_SYMBOL(kernel_sendmsg);
3134 EXPORT_SYMBOL(kernel_recvmsg);
3135 EXPORT_SYMBOL(kernel_bind);
3136 EXPORT_SYMBOL(kernel_listen);
3137 EXPORT_SYMBOL(kernel_accept);
3138 EXPORT_SYMBOL(kernel_connect);
3139 EXPORT_SYMBOL(kernel_getsockname);
3140 EXPORT_SYMBOL(kernel_getpeername);
3141 EXPORT_SYMBOL(kernel_getsockopt);
3142 EXPORT_SYMBOL(kernel_setsockopt);
3143 EXPORT_SYMBOL(kernel_sendpage);
3144 EXPORT_SYMBOL(kernel_sock_ioctl);
3145 EXPORT_SYMBOL(kernel_sock_shutdown);