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