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